blob: b747228c719898e0e3e5d73ae0b580ffe3e22bc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * Frame Relay support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
10 *
11
12 Theory of PVC state
13
14 DCE mode:
15
16 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
17 0,x -> 1,1 if "link reliable" when sending FULL STATUS
18 1,1 -> 1,0 if received FULL STATUS ACK
19
20 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
21 -> 1 when "PVC up" and (exist,new) = 1,0
22
23 DTE mode:
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
26 No LMI:
27 active = open and "link reliable"
28 exist = new = not used
29
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020030 CCITT LMI: ITU-T Q.933 Annex A
31 ANSI LMI: ANSI T1.617 Annex D
32 CISCO LMI: the original, aka "Gang of Four" LMI
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034*/
35
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/slab.h>
39#include <linux/poll.h>
40#include <linux/errno.h>
41#include <linux/if_arp.h>
42#include <linux/init.h>
43#include <linux/skbuff.h>
44#include <linux/pkt_sched.h>
45#include <linux/random.h>
46#include <linux/inetdevice.h>
47#include <linux/lapb.h>
48#include <linux/rtnetlink.h>
49#include <linux/etherdevice.h>
50#include <linux/hdlc.h>
51
52#undef DEBUG_PKT
53#undef DEBUG_ECN
54#undef DEBUG_LINK
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020055#undef DEBUG_PROTO
56#undef DEBUG_PVC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020058#define FR_UI 0x03
59#define FR_PAD 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020061#define NLPID_IP 0xCC
62#define NLPID_IPV6 0x8E
63#define NLPID_SNAP 0x80
64#define NLPID_PAD 0x00
65#define NLPID_CCITT_ANSI_LMI 0x08
66#define NLPID_CISCO_LMI 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020069#define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
70#define LMI_CISCO_DLCI 1023
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020072#define LMI_CALLREF 0x00 /* Call Reference */
73#define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
74#define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
75#define LMI_CCITT_REPTYPE 0x51
76#define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
77#define LMI_CCITT_ALIVE 0x53
78#define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
79#define LMI_CCITT_PVCSTAT 0x57
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020081#define LMI_FULLREP 0x00 /* full report */
82#define LMI_INTEGRITY 0x01 /* link integrity report */
83#define LMI_SINGLE 0x02 /* single PVC report */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define LMI_STATUS_ENQUIRY 0x75
86#define LMI_STATUS 0x7D /* reply */
87
88#define LMI_REPT_LEN 1 /* report type element length */
89#define LMI_INTEG_LEN 2 /* link integrity element length */
90
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +020091#define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
92#define LMI_ANSI_LENGTH 14
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94
95typedef struct {
96#if defined(__LITTLE_ENDIAN_BITFIELD)
97 unsigned ea1: 1;
98 unsigned cr: 1;
99 unsigned dlcih: 6;
100
101 unsigned ea2: 1;
102 unsigned de: 1;
103 unsigned becn: 1;
104 unsigned fecn: 1;
105 unsigned dlcil: 4;
106#else
107 unsigned dlcih: 6;
108 unsigned cr: 1;
109 unsigned ea1: 1;
110
111 unsigned dlcil: 4;
112 unsigned fecn: 1;
113 unsigned becn: 1;
114 unsigned de: 1;
115 unsigned ea2: 1;
116#endif
117}__attribute__ ((packed)) fr_hdr;
118
119
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200120typedef struct pvc_device_struct {
121 struct net_device *frad;
122 struct net_device *main;
123 struct net_device *ether; /* bridged Ethernet interface */
124 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
125 int dlci;
126 int open_count;
127
128 struct {
129 unsigned int new: 1;
130 unsigned int active: 1;
131 unsigned int exist: 1;
132 unsigned int deleted: 1;
133 unsigned int fecn: 1;
134 unsigned int becn: 1;
135 unsigned int bandwidth; /* Cisco LMI reporting only */
136 }state;
137}pvc_device;
138
139
140struct frad_state {
141 fr_proto settings;
142 pvc_device *first_pvc;
143 int dce_pvc_count;
144
145 struct timer_list timer;
146 unsigned long last_poll;
147 int reliable;
148 int dce_changed;
149 int request;
150 int fullrep_sent;
151 u32 last_errors; /* last errors bit list */
152 u8 n391cnt;
153 u8 txseq; /* TX sequence number */
154 u8 rxseq; /* RX sequence number */
155};
156
157
158static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
159
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static inline u16 q922_to_dlci(u8 *hdr)
162{
163 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
164}
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline void dlci_to_q922(u8 *hdr, u16 dlci)
168{
169 hdr[0] = (dlci >> 2) & 0xFC;
170 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
171}
172
173
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200174static inline struct frad_state * state(hdlc_device *hdlc)
175{
176 return(struct frad_state *)(hdlc->state);
177}
178
179
180static __inline__ pvc_device* dev_to_pvc(struct net_device *dev)
181{
182 return dev->priv;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
187{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200188 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 while (pvc) {
191 if (pvc->dlci == dlci)
192 return pvc;
193 if (pvc->dlci > dlci)
194 return NULL; /* the listed is sorted */
195 pvc = pvc->next;
196 }
197
198 return NULL;
199}
200
201
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200202static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200205 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 while (*pvc_p) {
208 if ((*pvc_p)->dlci == dlci)
209 return *pvc_p;
210 if ((*pvc_p)->dlci > dlci)
211 break; /* the list is sorted */
212 pvc_p = &(*pvc_p)->next;
213 }
214
215 pvc = kmalloc(sizeof(pvc_device), GFP_ATOMIC);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200216#ifdef DEBUG_PVC
217 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!pvc)
220 return NULL;
221
222 memset(pvc, 0, sizeof(pvc_device));
223 pvc->dlci = dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200224 pvc->frad = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 pvc->next = *pvc_p; /* Put it in the chain */
226 *pvc_p = pvc;
227 return pvc;
228}
229
230
231static inline int pvc_is_used(pvc_device *pvc)
232{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200233 return pvc->main || pvc->ether;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236
237static inline void pvc_carrier(int on, pvc_device *pvc)
238{
239 if (on) {
240 if (pvc->main)
241 if (!netif_carrier_ok(pvc->main))
242 netif_carrier_on(pvc->main);
243 if (pvc->ether)
244 if (!netif_carrier_ok(pvc->ether))
245 netif_carrier_on(pvc->ether);
246 } else {
247 if (pvc->main)
248 if (netif_carrier_ok(pvc->main))
249 netif_carrier_off(pvc->main);
250 if (pvc->ether)
251 if (netif_carrier_ok(pvc->ether))
252 netif_carrier_off(pvc->ether);
253 }
254}
255
256
257static inline void delete_unused_pvcs(hdlc_device *hdlc)
258{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200259 pvc_device **pvc_p = &state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 while (*pvc_p) {
262 if (!pvc_is_used(*pvc_p)) {
263 pvc_device *pvc = *pvc_p;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200264#ifdef DEBUG_PVC
265 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *pvc_p = pvc->next;
268 kfree(pvc);
269 continue;
270 }
271 pvc_p = &(*pvc_p)->next;
272 }
273}
274
275
276static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
277{
278 if (type == ARPHRD_ETHER)
279 return &pvc->ether;
280 else
281 return &pvc->main;
282}
283
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
286{
287 u16 head_len;
288 struct sk_buff *skb = *skb_p;
289
290 switch (skb->protocol) {
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200291 case __constant_ntohs(NLPID_CCITT_ANSI_LMI):
292 head_len = 4;
293 skb_push(skb, head_len);
294 skb->data[3] = NLPID_CCITT_ANSI_LMI;
295 break;
296
297 case __constant_ntohs(NLPID_CISCO_LMI):
298 head_len = 4;
299 skb_push(skb, head_len);
300 skb->data[3] = NLPID_CISCO_LMI;
301 break;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case __constant_ntohs(ETH_P_IP):
304 head_len = 4;
305 skb_push(skb, head_len);
306 skb->data[3] = NLPID_IP;
307 break;
308
309 case __constant_ntohs(ETH_P_IPV6):
310 head_len = 4;
311 skb_push(skb, head_len);
312 skb->data[3] = NLPID_IPV6;
313 break;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 case __constant_ntohs(ETH_P_802_3):
316 head_len = 10;
317 if (skb_headroom(skb) < head_len) {
318 struct sk_buff *skb2 = skb_realloc_headroom(skb,
319 head_len);
320 if (!skb2)
321 return -ENOBUFS;
322 dev_kfree_skb(skb);
323 skb = *skb_p = skb2;
324 }
325 skb_push(skb, head_len);
326 skb->data[3] = FR_PAD;
327 skb->data[4] = NLPID_SNAP;
328 skb->data[5] = FR_PAD;
329 skb->data[6] = 0x80;
330 skb->data[7] = 0xC2;
331 skb->data[8] = 0x00;
332 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
333 break;
334
335 default:
336 head_len = 10;
337 skb_push(skb, head_len);
338 skb->data[3] = FR_PAD;
339 skb->data[4] = NLPID_SNAP;
340 skb->data[5] = FR_PAD;
341 skb->data[6] = FR_PAD;
342 skb->data[7] = FR_PAD;
343 *(u16*)(skb->data + 8) = skb->protocol;
344 }
345
346 dlci_to_q922(skb->data, dlci);
347 skb->data[2] = FR_UI;
348 return 0;
349}
350
351
352
353static int pvc_open(struct net_device *dev)
354{
355 pvc_device *pvc = dev_to_pvc(dev);
356
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200357 if ((pvc->frad->flags & IFF_UP) == 0)
358 return -EIO; /* Frad must be UP in order to activate PVC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (pvc->open_count++ == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200361 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
362 if (state(hdlc)->settings.lmi == LMI_NONE)
363 pvc->state.active = netif_carrier_ok(pvc->frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 pvc_carrier(pvc->state.active, pvc);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200366 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 return 0;
369}
370
371
372
373static int pvc_close(struct net_device *dev)
374{
375 pvc_device *pvc = dev_to_pvc(dev);
376
377 if (--pvc->open_count == 0) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200378 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
379 if (state(hdlc)->settings.lmi == LMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 pvc->state.active = 0;
381
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200382 if (state(hdlc)->settings.dce) {
383 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pvc->state.active = 0;
385 }
386 }
387 return 0;
388}
389
390
391
Adrian Bunk7665a082005-09-09 23:17:28 -0700392static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 pvc_device *pvc = dev_to_pvc(dev);
395 fr_proto_pvc_info info;
396
397 if (ifr->ifr_settings.type == IF_GET_PROTO) {
398 if (dev->type == ARPHRD_ETHER)
399 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
400 else
401 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
402
403 if (ifr->ifr_settings.size < sizeof(info)) {
404 /* data size wanted */
405 ifr->ifr_settings.size = sizeof(info);
406 return -ENOBUFS;
407 }
408
409 info.dlci = pvc->dlci;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200410 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
412 &info, sizeof(info)))
413 return -EFAULT;
414 return 0;
415 }
416
417 return -EINVAL;
418}
419
420
421static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
422{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200423 return &dev_to_desc(dev)->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426
427
428static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
429{
430 pvc_device *pvc = dev_to_pvc(dev);
431 struct net_device_stats *stats = pvc_get_stats(dev);
432
433 if (pvc->state.active) {
434 if (dev->type == ARPHRD_ETHER) {
435 int pad = ETH_ZLEN - skb->len;
436 if (pad > 0) { /* Pad the frame with zeros */
437 int len = skb->len;
438 if (skb_tailroom(skb) < pad)
439 if (pskb_expand_head(skb, 0, pad,
440 GFP_ATOMIC)) {
441 stats->tx_dropped++;
442 dev_kfree_skb(skb);
443 return 0;
444 }
445 skb_put(skb, pad);
446 memset(skb->data + len, 0, pad);
447 }
448 skb->protocol = __constant_htons(ETH_P_802_3);
449 }
450 if (!fr_hard_header(&skb, pvc->dlci)) {
451 stats->tx_bytes += skb->len;
452 stats->tx_packets++;
453 if (pvc->state.fecn) /* TX Congestion counter */
454 stats->tx_compressed++;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200455 skb->dev = pvc->frad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 dev_queue_xmit(skb);
457 return 0;
458 }
459 }
460
461 stats->tx_dropped++;
462 dev_kfree_skb(skb);
463 return 0;
464}
465
466
467
468static int pvc_change_mtu(struct net_device *dev, int new_mtu)
469{
470 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
471 return -EINVAL;
472 dev->mtu = new_mtu;
473 return 0;
474}
475
476
477
478static inline void fr_log_dlci_active(pvc_device *pvc)
479{
480 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200481 pvc->frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 pvc->dlci,
483 pvc->main ? pvc->main->name : "",
484 pvc->main && pvc->ether ? " " : "",
485 pvc->ether ? pvc->ether->name : "",
486 pvc->state.new ? " new" : "",
487 !pvc->state.exist ? "deleted" :
488 pvc->state.active ? "active" : "inactive");
489}
490
491
492
493static inline u8 fr_lmi_nextseq(u8 x)
494{
495 x++;
496 return x ? x : 1;
497}
498
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static void fr_lmi_send(struct net_device *dev, int fullrep)
501{
502 hdlc_device *hdlc = dev_to_hdlc(dev);
503 struct sk_buff *skb;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200504 pvc_device *pvc = state(hdlc)->first_pvc;
505 int lmi = state(hdlc)->settings.lmi;
506 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200507 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
508 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 u8 *data;
510 int i = 0;
511
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200512 if (dce && fullrep) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200513 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (len > HDLC_MAX_MRU) {
515 printk(KERN_WARNING "%s: Too many PVCs while sending "
516 "LMI full report\n", dev->name);
517 return;
518 }
519 }
520
521 skb = dev_alloc_skb(len);
522 if (!skb) {
523 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
524 dev->name);
525 return;
526 }
527 memset(skb->data, 0, len);
528 skb_reserve(skb, 4);
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200529 if (lmi == LMI_CISCO) {
530 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
531 fr_hard_header(&skb, LMI_CISCO_DLCI);
532 } else {
533 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
534 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 data = skb->tail;
537 data[i++] = LMI_CALLREF;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200538 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
539 if (lmi == LMI_ANSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 data[i++] = LMI_ANSI_LOCKSHIFT;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200541 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
542 LMI_ANSI_CISCO_REPTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 data[i++] = LMI_REPT_LEN;
544 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200545 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 data[i++] = LMI_INTEG_LEN;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200547 data[i++] = state(hdlc)->txseq =
548 fr_lmi_nextseq(state(hdlc)->txseq);
549 data[i++] = state(hdlc)->rxseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200551 if (dce && fullrep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 while (pvc) {
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200553 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
554 LMI_ANSI_CISCO_PVCSTAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 data[i++] = stat_len;
556
557 /* LMI start/restart */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200558 if (state(hdlc)->reliable && !pvc->state.exist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 pvc->state.exist = pvc->state.new = 1;
560 fr_log_dlci_active(pvc);
561 }
562
563 /* ifconfig PVC up */
564 if (pvc->open_count && !pvc->state.active &&
565 pvc->state.exist && !pvc->state.new) {
566 pvc_carrier(1, pvc);
567 pvc->state.active = 1;
568 fr_log_dlci_active(pvc);
569 }
570
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200571 if (lmi == LMI_CISCO) {
572 data[i] = pvc->dlci >> 8;
573 data[i + 1] = pvc->dlci & 0xFF;
574 } else {
575 data[i] = (pvc->dlci >> 4) & 0x3F;
576 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
577 data[i + 2] = 0x80;
578 }
579
580 if (pvc->state.new)
581 data[i + 2] |= 0x08;
582 else if (pvc->state.active)
583 data[i + 2] |= 0x02;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 i += stat_len;
586 pvc = pvc->next;
587 }
588 }
589
590 skb_put(skb, i);
591 skb->priority = TC_PRIO_CONTROL;
592 skb->dev = dev;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700593 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 dev_queue_xmit(skb);
596}
597
598
599
600static void fr_set_link_state(int reliable, struct net_device *dev)
601{
602 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200603 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200605 state(hdlc)->reliable = reliable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (reliable) {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700607 netif_dormant_off(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200608 state(hdlc)->n391cnt = 0; /* Request full status */
609 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200611 if (state(hdlc)->settings.lmi == LMI_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 while (pvc) { /* Activate all PVCs */
613 pvc_carrier(1, pvc);
614 pvc->state.exist = pvc->state.active = 1;
615 pvc->state.new = 0;
616 pvc = pvc->next;
617 }
618 }
619 } else {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700620 netif_dormant_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 while (pvc) { /* Deactivate all PVCs */
622 pvc_carrier(0, pvc);
623 pvc->state.exist = pvc->state.active = 0;
624 pvc->state.new = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200625 if (!state(hdlc)->settings.dce)
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200626 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 pvc = pvc->next;
628 }
629 }
630}
631
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static void fr_timer(unsigned long arg)
634{
635 struct net_device *dev = (struct net_device *)arg;
636 hdlc_device *hdlc = dev_to_hdlc(dev);
637 int i, cnt = 0, reliable;
638 u32 list;
639
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200640 if (state(hdlc)->settings.dce) {
641 reliable = state(hdlc)->request &&
642 time_before(jiffies, state(hdlc)->last_poll +
643 state(hdlc)->settings.t392 * HZ);
644 state(hdlc)->request = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200645 } else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200646 state(hdlc)->last_errors <<= 1; /* Shift the list */
647 if (state(hdlc)->request) {
648 if (state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 printk(KERN_INFO "%s: No LMI status reply "
650 "received\n", dev->name);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200651 state(hdlc)->last_errors |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200654 list = state(hdlc)->last_errors;
655 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 cnt += (list & 1); /* errors count */
657
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200658 reliable = (cnt < state(hdlc)->settings.n392);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200661 if (state(hdlc)->reliable != reliable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
663 reliable ? "" : "un");
664 fr_set_link_state(reliable, dev);
665 }
666
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200667 if (state(hdlc)->settings.dce)
668 state(hdlc)->timer.expires = jiffies +
669 state(hdlc)->settings.t392 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 else {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200671 if (state(hdlc)->n391cnt)
672 state(hdlc)->n391cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200674 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200676 state(hdlc)->last_poll = jiffies;
677 state(hdlc)->request = 1;
678 state(hdlc)->timer.expires = jiffies +
679 state(hdlc)->settings.t391 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200682 state(hdlc)->timer.function = fr_timer;
683 state(hdlc)->timer.data = arg;
684 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
689{
690 hdlc_device *hdlc = dev_to_hdlc(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 pvc_device *pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 u8 rxseq, txseq;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200693 int lmi = state(hdlc)->settings.lmi;
694 int dce = state(hdlc)->settings.dce;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200695 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200697 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
698 LMI_CCITT_CISCO_LENGTH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
700 return 1;
701 }
702
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200703 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
704 NLPID_CCITT_ANSI_LMI)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200705 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
706 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 1;
708 }
709
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200710 if (skb->data[4] != LMI_CALLREF) {
711 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
712 dev->name, skb->data[4]);
713 return 1;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200716 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
717 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
718 dev->name, skb->data[5]);
719 return 1;
720 }
721
722 if (lmi == LMI_ANSI) {
723 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
724 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
725 " message (0x%02X)\n", dev->name, skb->data[6]);
726 return 1;
727 }
728 i = 7;
729 } else
730 i = 6;
731
732 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
733 LMI_ANSI_CISCO_REPTYPE)) {
734 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 dev->name, skb->data[i]);
736 return 1;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200739 if (skb->data[++i] != LMI_REPT_LEN) {
740 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
741 " (%u)\n", dev->name, skb->data[i]);
742 return 1;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200745 reptype = skb->data[++i];
746 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
747 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
748 dev->name, reptype);
749 return 1;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200752 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
753 LMI_ANSI_CISCO_ALIVE)) {
754 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
755 " IE (0x%02X)\n", dev->name, skb->data[i]);
756 return 1;
757 }
758
759 if (skb->data[++i] != LMI_INTEG_LEN) {
760 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
761 " IE length (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
763 }
764 i++;
765
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200766 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 rxseq = skb->data[i++]; /* Should confirm our sequence */
768
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200769 txseq = state(hdlc)->txseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200771 if (dce)
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200772 state(hdlc)->last_poll = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 error = 0;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200775 if (!state(hdlc)->reliable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 error = 1;
777
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200778 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
779 state(hdlc)->n391cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 error = 1;
781 }
782
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200783 if (dce) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200784 if (state(hdlc)->fullrep_sent && !error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/* Stop sending full report - the last one has been confirmed by DTE */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200786 state(hdlc)->fullrep_sent = 0;
787 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 while (pvc) {
789 if (pvc->state.new) {
790 pvc->state.new = 0;
791
792/* Tell DTE that new PVC is now active */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200793 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 pvc = pvc->next;
796 }
797 }
798
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200799 if (state(hdlc)->dce_changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 reptype = LMI_FULLREP;
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200801 state(hdlc)->fullrep_sent = 1;
802 state(hdlc)->dce_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200805 state(hdlc)->request = 1; /* got request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
807 return 0;
808 }
809
810 /* DTE */
811
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200812 state(hdlc)->request = 0; /* got response, no request pending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (error)
815 return 0;
816
817 if (reptype != LMI_FULLREP)
818 return 0;
819
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200820 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 while (pvc) {
823 pvc->state.deleted = 1;
824 pvc = pvc->next;
825 }
826
827 no_ram = 0;
828 while (skb->len >= i + 2 + stat_len) {
829 u16 dlci;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200830 u32 bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int active, new;
832
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200833 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
834 LMI_ANSI_CISCO_PVCSTAT)) {
835 printk(KERN_INFO "%s: Not an LMI PVC status IE"
836 " (0x%02X)\n", dev->name, skb->data[i]);
837 return 1;
838 }
839
840 if (skb->data[++i] != stat_len) {
841 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
842 " (%u)\n", dev->name, skb->data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 1;
844 }
845 i++;
846
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200847 new = !! (skb->data[i + 2] & 0x08);
848 active = !! (skb->data[i + 2] & 0x02);
849 if (lmi == LMI_CISCO) {
850 dlci = (skb->data[i] << 8) | skb->data[i + 1];
851 bw = (skb->data[i + 3] << 16) |
852 (skb->data[i + 4] << 8) |
853 (skb->data[i + 5]);
854 } else {
855 dlci = ((skb->data[i] & 0x3F) << 4) |
856 ((skb->data[i + 1] & 0x78) >> 3);
857 bw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 pvc = add_pvc(dev, dlci);
861
862 if (!pvc && !no_ram) {
863 printk(KERN_WARNING
864 "%s: Memory squeeze on fr_lmi_recv()\n",
865 dev->name);
866 no_ram = 1;
867 }
868
869 if (pvc) {
870 pvc->state.exist = 1;
871 pvc->state.deleted = 0;
872 if (active != pvc->state.active ||
873 new != pvc->state.new ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200874 bw != pvc->state.bandwidth ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 !pvc->state.exist) {
876 pvc->state.new = new;
877 pvc->state.active = active;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200878 pvc->state.bandwidth = bw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 pvc_carrier(active, pvc);
880 fr_log_dlci_active(pvc);
881 }
882 }
883
884 i += stat_len;
885 }
886
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200887 pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 while (pvc) {
890 if (pvc->state.deleted && pvc->state.exist) {
891 pvc_carrier(0, pvc);
892 pvc->state.active = pvc->state.new = 0;
893 pvc->state.exist = 0;
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200894 pvc->state.bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 fr_log_dlci_active(pvc);
896 }
897 pvc = pvc->next;
898 }
899
900 /* Next full report after N391 polls */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200901 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return 0;
904}
905
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int fr_rx(struct sk_buff *skb)
908{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200909 struct net_device *frad = skb->dev;
910 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 fr_hdr *fh = (fr_hdr*)skb->data;
912 u8 *data = skb->data;
913 u16 dlci;
914 pvc_device *pvc;
915 struct net_device *dev = NULL;
916
917 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
918 goto rx_error;
919
920 dlci = q922_to_dlci(skb->data);
921
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200922 if ((dlci == LMI_CCITT_ANSI_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200923 (state(hdlc)->settings.lmi == LMI_ANSI ||
924 state(hdlc)->settings.lmi == LMI_CCITT)) ||
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200925 (dlci == LMI_CISCO_DLCI &&
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200926 state(hdlc)->settings.lmi == LMI_CISCO)) {
927 if (fr_lmi_recv(frad, skb))
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +0200928 goto rx_error;
929 dev_kfree_skb_any(skb);
930 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 pvc = find_pvc(hdlc, dlci);
934 if (!pvc) {
935#ifdef DEBUG_PKT
936 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200937 frad->name, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#endif
939 dev_kfree_skb_any(skb);
940 return NET_RX_DROP;
941 }
942
943 if (pvc->state.fecn != fh->fecn) {
944#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200945 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 dlci, fh->fecn ? "N" : "FF");
947#endif
948 pvc->state.fecn ^= 1;
949 }
950
951 if (pvc->state.becn != fh->becn) {
952#ifdef DEBUG_ECN
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200953 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 dlci, fh->becn ? "N" : "FF");
955#endif
956 pvc->state.becn ^= 1;
957 }
958
959
960 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200961 dev_to_desc(frad)->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return NET_RX_DROP;
963 }
964
965 if (data[3] == NLPID_IP) {
966 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
967 dev = pvc->main;
968 skb->protocol = htons(ETH_P_IP);
969
970 } else if (data[3] == NLPID_IPV6) {
971 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
972 dev = pvc->main;
973 skb->protocol = htons(ETH_P_IPV6);
974
975 } else if (skb->len > 10 && data[3] == FR_PAD &&
976 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
977 u16 oui = ntohs(*(u16*)(data + 6));
978 u16 pid = ntohs(*(u16*)(data + 8));
979 skb_pull(skb, 10);
980
981 switch ((((u32)oui) << 16) | pid) {
982 case ETH_P_ARP: /* routed frame with SNAP */
983 case ETH_P_IPX:
984 case ETH_P_IP: /* a long variant */
985 case ETH_P_IPV6:
986 dev = pvc->main;
987 skb->protocol = htons(pid);
988 break;
989
990 case 0x80C20007: /* bridged Ethernet frame */
991 if ((dev = pvc->ether) != NULL)
992 skb->protocol = eth_type_trans(skb, dev);
993 break;
994
995 default:
996 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200997 "PID=%x\n", frad->name, oui, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dev_kfree_skb_any(skb);
999 return NET_RX_DROP;
1000 }
1001 } else {
1002 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001003 "length = %i\n", frad->name, data[3], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 dev_kfree_skb_any(skb);
1005 return NET_RX_DROP;
1006 }
1007
1008 if (dev) {
1009 struct net_device_stats *stats = pvc_get_stats(dev);
1010 stats->rx_packets++; /* PVC traffic */
1011 stats->rx_bytes += skb->len;
1012 if (pvc->state.becn)
1013 stats->rx_compressed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 netif_rx(skb);
1015 return NET_RX_SUCCESS;
1016 } else {
1017 dev_kfree_skb_any(skb);
1018 return NET_RX_DROP;
1019 }
1020
1021 rx_error:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001022 dev_to_desc(frad)->stats.rx_errors++; /* Mark error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dev_kfree_skb_any(skb);
1024 return NET_RX_DROP;
1025}
1026
1027
1028
1029static void fr_start(struct net_device *dev)
1030{
1031 hdlc_device *hdlc = dev_to_hdlc(dev);
1032#ifdef DEBUG_LINK
1033 printk(KERN_DEBUG "fr_start\n");
1034#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001035 if (state(hdlc)->settings.lmi != LMI_NONE) {
1036 state(hdlc)->reliable = 0;
1037 state(hdlc)->dce_changed = 1;
1038 state(hdlc)->request = 0;
1039 state(hdlc)->fullrep_sent = 0;
1040 state(hdlc)->last_errors = 0xFFFFFFFF;
1041 state(hdlc)->n391cnt = 0;
1042 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001044 init_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* First poll after 1 s */
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001046 state(hdlc)->timer.expires = jiffies + HZ;
1047 state(hdlc)->timer.function = fr_timer;
1048 state(hdlc)->timer.data = (unsigned long)dev;
1049 add_timer(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else
1051 fr_set_link_state(1, dev);
1052}
1053
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static void fr_stop(struct net_device *dev)
1056{
1057 hdlc_device *hdlc = dev_to_hdlc(dev);
1058#ifdef DEBUG_LINK
1059 printk(KERN_DEBUG "fr_stop\n");
1060#endif
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001061 if (state(hdlc)->settings.lmi != LMI_NONE)
1062 del_timer_sync(&state(hdlc)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 fr_set_link_state(0, dev);
1064}
1065
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static void fr_close(struct net_device *dev)
1068{
1069 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001070 pvc_device *pvc = state(hdlc)->first_pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 while (pvc) { /* Shutdown all PVCs for this FRAD */
1073 if (pvc->main)
1074 dev_close(pvc->main);
1075 if (pvc->ether)
1076 dev_close(pvc->ether);
1077 pvc = pvc->next;
1078 }
1079}
1080
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001081
1082static void pvc_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 dev->type = ARPHRD_DLCI;
1085 dev->flags = IFF_POINTOPOINT;
1086 dev->hard_header_len = 10;
1087 dev->addr_len = 2;
1088}
1089
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001090static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001092 hdlc_device *hdlc = dev_to_hdlc(frad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 pvc_device *pvc = NULL;
1094 struct net_device *dev;
1095 int result, used;
1096 char * prefix = "pvc%d";
1097
1098 if (type == ARPHRD_ETHER)
1099 prefix = "pvceth%d";
1100
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001101 if ((pvc = add_pvc(frad, dlci)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001103 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -ENOBUFS;
1105 }
1106
1107 if (*get_dev_p(pvc, type))
1108 return -EEXIST;
1109
1110 used = pvc_is_used(pvc);
1111
1112 if (type == ARPHRD_ETHER)
1113 dev = alloc_netdev(sizeof(struct net_device_stats),
1114 "pvceth%d", ether_setup);
1115 else
1116 dev = alloc_netdev(sizeof(struct net_device_stats),
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001117 "pvc%d", pvc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (!dev) {
1120 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001121 frad->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 delete_unused_pvcs(hdlc);
1123 return -ENOBUFS;
1124 }
1125
1126 if (type == ARPHRD_ETHER) {
1127 memcpy(dev->dev_addr, "\x00\x01", 2);
1128 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1129 } else {
1130 *(u16*)dev->dev_addr = htons(dlci);
1131 dlci_to_q922(dev->broadcast, dlci);
1132 }
1133 dev->hard_start_xmit = pvc_xmit;
1134 dev->get_stats = pvc_get_stats;
1135 dev->open = pvc_open;
1136 dev->stop = pvc_close;
1137 dev->do_ioctl = pvc_ioctl;
1138 dev->change_mtu = pvc_change_mtu;
1139 dev->mtu = HDLC_MAX_MTU;
1140 dev->tx_queue_len = 0;
1141 dev->priv = pvc;
1142
1143 result = dev_alloc_name(dev, dev->name);
1144 if (result < 0) {
1145 free_netdev(dev);
1146 delete_unused_pvcs(hdlc);
1147 return result;
1148 }
1149
1150 if (register_netdevice(dev) != 0) {
1151 free_netdev(dev);
1152 delete_unused_pvcs(hdlc);
1153 return -EIO;
1154 }
1155
1156 dev->destructor = free_netdev;
1157 *get_dev_p(pvc, type) = dev;
1158 if (!used) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001159 state(hdlc)->dce_changed = 1;
1160 state(hdlc)->dce_pvc_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 return 0;
1163}
1164
1165
1166
1167static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1168{
1169 pvc_device *pvc;
1170 struct net_device *dev;
1171
1172 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1173 return -ENOENT;
1174
1175 if ((dev = *get_dev_p(pvc, type)) == NULL)
1176 return -ENOENT;
1177
1178 if (dev->flags & IFF_UP)
1179 return -EBUSY; /* PVC in use */
1180
1181 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1182 *get_dev_p(pvc, type) = NULL;
1183
1184 if (!pvc_is_used(pvc)) {
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001185 state(hdlc)->dce_pvc_count--;
1186 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 delete_unused_pvcs(hdlc);
1189 return 0;
1190}
1191
1192
1193
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001194static void fr_destroy(struct net_device *frad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001196 hdlc_device *hdlc = dev_to_hdlc(frad);
1197 pvc_device *pvc = state(hdlc)->first_pvc;
1198 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1199 state(hdlc)->dce_pvc_count = 0;
1200 state(hdlc)->dce_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 while (pvc) {
1203 pvc_device *next = pvc->next;
1204 /* destructors will free_netdev() main and ether */
1205 if (pvc->main)
1206 unregister_netdevice(pvc->main);
1207
1208 if (pvc->ether)
1209 unregister_netdevice(pvc->ether);
1210
1211 kfree(pvc);
1212 pvc = next;
1213 }
1214}
1215
1216
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001217static struct hdlc_proto proto = {
1218 .close = fr_close,
1219 .start = fr_start,
1220 .stop = fr_stop,
1221 .detach = fr_destroy,
1222 .ioctl = fr_ioctl,
1223 .module = THIS_MODULE,
1224};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001226
1227static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1230 const size_t size = sizeof(fr_proto);
1231 fr_proto new_settings;
1232 hdlc_device *hdlc = dev_to_hdlc(dev);
1233 fr_proto_pvc pvc;
1234 int result;
1235
1236 switch (ifr->ifr_settings.type) {
1237 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001238 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1239 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ifr->ifr_settings.type = IF_PROTO_FR;
1241 if (ifr->ifr_settings.size < size) {
1242 ifr->ifr_settings.size = size; /* data size wanted */
1243 return -ENOBUFS;
1244 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001245 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -EFAULT;
1247 return 0;
1248
1249 case IF_PROTO_FR:
1250 if(!capable(CAP_NET_ADMIN))
1251 return -EPERM;
1252
1253 if(dev->flags & IFF_UP)
1254 return -EBUSY;
1255
1256 if (copy_from_user(&new_settings, fr_s, size))
1257 return -EFAULT;
1258
1259 if (new_settings.lmi == LMI_DEFAULT)
1260 new_settings.lmi = LMI_ANSI;
1261
1262 if ((new_settings.lmi != LMI_NONE &&
1263 new_settings.lmi != LMI_ANSI &&
Krzysztof Halasab3dd65f2005-04-21 15:57:25 +02001264 new_settings.lmi != LMI_CCITT &&
1265 new_settings.lmi != LMI_CISCO) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 new_settings.t391 < 1 ||
1267 new_settings.t392 < 2 ||
1268 new_settings.n391 < 1 ||
1269 new_settings.n392 < 1 ||
1270 new_settings.n393 < new_settings.n392 ||
1271 new_settings.n393 > 32 ||
1272 (new_settings.dce != 0 &&
1273 new_settings.dce != 1))
1274 return -EINVAL;
1275
1276 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1277 if (result)
1278 return result;
1279
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001280 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1281 result = attach_hdlc_protocol(dev, &proto, fr_rx,
1282 sizeof(struct frad_state));
1283 if (result)
1284 return result;
1285 state(hdlc)->first_pvc = NULL;
1286 state(hdlc)->dce_pvc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001288 memcpy(&state(hdlc)->settings, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 dev->hard_start_xmit = hdlc->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 dev->type = ARPHRD_FRAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293
1294 case IF_PROTO_FR_ADD_PVC:
1295 case IF_PROTO_FR_DEL_PVC:
1296 case IF_PROTO_FR_ADD_ETH_PVC:
1297 case IF_PROTO_FR_DEL_ETH_PVC:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001298 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1299 return -EINVAL;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if(!capable(CAP_NET_ADMIN))
1302 return -EPERM;
1303
1304 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1305 sizeof(fr_proto_pvc)))
1306 return -EFAULT;
1307
1308 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1309 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1310
1311 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1312 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1313 result = ARPHRD_ETHER; /* bridged Ethernet device */
1314 else
1315 result = ARPHRD_DLCI;
1316
1317 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1318 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1319 return fr_add_pvc(dev, pvc.dlci, result);
1320 else
1321 return fr_del_pvc(hdlc, pvc.dlci, result);
1322 }
1323
1324 return -EINVAL;
1325}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02001326
1327
1328static int __init mod_init(void)
1329{
1330 register_hdlc_protocol(&proto);
1331 return 0;
1332}
1333
1334
1335static void __exit mod_exit(void)
1336{
1337 unregister_hdlc_protocol(&proto);
1338}
1339
1340
1341module_init(mod_init);
1342module_exit(mod_exit);
1343
1344MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1345MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1346MODULE_LICENSE("GPL v2");