Merge branch 'splice' of git://brick.kernel.dk/data/git/linux-2.6-block
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wan / hdlc_fr.c
blob523afe17564e20f1c728b98a9d148ec9d309aced
1 /*
2 * Generic HDLC support routines for Linux
3 * Frame Relay support
5 * Copyright (C) 1999 - 2005 Krzysztof Halasa <khc@pm.waw.pl>
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.
12 Theory of PVC state
14 DCE mode:
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
20 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
21 -> 1 when "PVC up" and (exist,new) = 1,0
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
30 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
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>
52 #undef DEBUG_PKT
53 #undef DEBUG_ECN
54 #undef DEBUG_LINK
56 #define FR_UI 0x03
57 #define FR_PAD 0x00
59 #define NLPID_IP 0xCC
60 #define NLPID_IPV6 0x8E
61 #define NLPID_SNAP 0x80
62 #define NLPID_PAD 0x00
63 #define NLPID_CCITT_ANSI_LMI 0x08
64 #define NLPID_CISCO_LMI 0x09
67 #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
68 #define LMI_CISCO_DLCI 1023
70 #define LMI_CALLREF 0x00 /* Call Reference */
71 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
72 #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
73 #define LMI_CCITT_REPTYPE 0x51
74 #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
75 #define LMI_CCITT_ALIVE 0x53
76 #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
77 #define LMI_CCITT_PVCSTAT 0x57
79 #define LMI_FULLREP 0x00 /* full report */
80 #define LMI_INTEGRITY 0x01 /* link integrity report */
81 #define LMI_SINGLE 0x02 /* single PVC report */
83 #define LMI_STATUS_ENQUIRY 0x75
84 #define LMI_STATUS 0x7D /* reply */
86 #define LMI_REPT_LEN 1 /* report type element length */
87 #define LMI_INTEG_LEN 2 /* link integrity element length */
89 #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
90 #define LMI_ANSI_LENGTH 14
93 typedef struct {
94 #if defined(__LITTLE_ENDIAN_BITFIELD)
95 unsigned ea1: 1;
96 unsigned cr: 1;
97 unsigned dlcih: 6;
99 unsigned ea2: 1;
100 unsigned de: 1;
101 unsigned becn: 1;
102 unsigned fecn: 1;
103 unsigned dlcil: 4;
104 #else
105 unsigned dlcih: 6;
106 unsigned cr: 1;
107 unsigned ea1: 1;
109 unsigned dlcil: 4;
110 unsigned fecn: 1;
111 unsigned becn: 1;
112 unsigned de: 1;
113 unsigned ea2: 1;
114 #endif
115 }__attribute__ ((packed)) fr_hdr;
118 static inline u16 q922_to_dlci(u8 *hdr)
120 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
125 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
127 hdr[0] = (dlci >> 2) & 0xFC;
128 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
133 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
135 pvc_device *pvc = hdlc->state.fr.first_pvc;
137 while (pvc) {
138 if (pvc->dlci == dlci)
139 return pvc;
140 if (pvc->dlci > dlci)
141 return NULL; /* the listed is sorted */
142 pvc = pvc->next;
145 return NULL;
149 static inline pvc_device* add_pvc(struct net_device *dev, u16 dlci)
151 hdlc_device *hdlc = dev_to_hdlc(dev);
152 pvc_device *pvc, **pvc_p = &hdlc->state.fr.first_pvc;
154 while (*pvc_p) {
155 if ((*pvc_p)->dlci == dlci)
156 return *pvc_p;
157 if ((*pvc_p)->dlci > dlci)
158 break; /* the list is sorted */
159 pvc_p = &(*pvc_p)->next;
162 pvc = kmalloc(sizeof(pvc_device), GFP_ATOMIC);
163 if (!pvc)
164 return NULL;
166 memset(pvc, 0, sizeof(pvc_device));
167 pvc->dlci = dlci;
168 pvc->master = dev;
169 pvc->next = *pvc_p; /* Put it in the chain */
170 *pvc_p = pvc;
171 return pvc;
175 static inline int pvc_is_used(pvc_device *pvc)
177 return pvc->main != NULL || pvc->ether != NULL;
181 static inline void pvc_carrier(int on, pvc_device *pvc)
183 if (on) {
184 if (pvc->main)
185 if (!netif_carrier_ok(pvc->main))
186 netif_carrier_on(pvc->main);
187 if (pvc->ether)
188 if (!netif_carrier_ok(pvc->ether))
189 netif_carrier_on(pvc->ether);
190 } else {
191 if (pvc->main)
192 if (netif_carrier_ok(pvc->main))
193 netif_carrier_off(pvc->main);
194 if (pvc->ether)
195 if (netif_carrier_ok(pvc->ether))
196 netif_carrier_off(pvc->ether);
201 static inline void delete_unused_pvcs(hdlc_device *hdlc)
203 pvc_device **pvc_p = &hdlc->state.fr.first_pvc;
205 while (*pvc_p) {
206 if (!pvc_is_used(*pvc_p)) {
207 pvc_device *pvc = *pvc_p;
208 *pvc_p = pvc->next;
209 kfree(pvc);
210 continue;
212 pvc_p = &(*pvc_p)->next;
217 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
219 if (type == ARPHRD_ETHER)
220 return &pvc->ether;
221 else
222 return &pvc->main;
226 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
228 u16 head_len;
229 struct sk_buff *skb = *skb_p;
231 switch (skb->protocol) {
232 case __constant_ntohs(NLPID_CCITT_ANSI_LMI):
233 head_len = 4;
234 skb_push(skb, head_len);
235 skb->data[3] = NLPID_CCITT_ANSI_LMI;
236 break;
238 case __constant_ntohs(NLPID_CISCO_LMI):
239 head_len = 4;
240 skb_push(skb, head_len);
241 skb->data[3] = NLPID_CISCO_LMI;
242 break;
244 case __constant_ntohs(ETH_P_IP):
245 head_len = 4;
246 skb_push(skb, head_len);
247 skb->data[3] = NLPID_IP;
248 break;
250 case __constant_ntohs(ETH_P_IPV6):
251 head_len = 4;
252 skb_push(skb, head_len);
253 skb->data[3] = NLPID_IPV6;
254 break;
256 case __constant_ntohs(ETH_P_802_3):
257 head_len = 10;
258 if (skb_headroom(skb) < head_len) {
259 struct sk_buff *skb2 = skb_realloc_headroom(skb,
260 head_len);
261 if (!skb2)
262 return -ENOBUFS;
263 dev_kfree_skb(skb);
264 skb = *skb_p = skb2;
266 skb_push(skb, head_len);
267 skb->data[3] = FR_PAD;
268 skb->data[4] = NLPID_SNAP;
269 skb->data[5] = FR_PAD;
270 skb->data[6] = 0x80;
271 skb->data[7] = 0xC2;
272 skb->data[8] = 0x00;
273 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
274 break;
276 default:
277 head_len = 10;
278 skb_push(skb, head_len);
279 skb->data[3] = FR_PAD;
280 skb->data[4] = NLPID_SNAP;
281 skb->data[5] = FR_PAD;
282 skb->data[6] = FR_PAD;
283 skb->data[7] = FR_PAD;
284 *(u16*)(skb->data + 8) = skb->protocol;
287 dlci_to_q922(skb->data, dlci);
288 skb->data[2] = FR_UI;
289 return 0;
294 static int pvc_open(struct net_device *dev)
296 pvc_device *pvc = dev_to_pvc(dev);
298 if ((pvc->master->flags & IFF_UP) == 0)
299 return -EIO; /* Master must be UP in order to activate PVC */
301 if (pvc->open_count++ == 0) {
302 hdlc_device *hdlc = dev_to_hdlc(pvc->master);
303 if (hdlc->state.fr.settings.lmi == LMI_NONE)
304 pvc->state.active = hdlc->carrier;
306 pvc_carrier(pvc->state.active, pvc);
307 hdlc->state.fr.dce_changed = 1;
309 return 0;
314 static int pvc_close(struct net_device *dev)
316 pvc_device *pvc = dev_to_pvc(dev);
318 if (--pvc->open_count == 0) {
319 hdlc_device *hdlc = dev_to_hdlc(pvc->master);
320 if (hdlc->state.fr.settings.lmi == LMI_NONE)
321 pvc->state.active = 0;
323 if (hdlc->state.fr.settings.dce) {
324 hdlc->state.fr.dce_changed = 1;
325 pvc->state.active = 0;
328 return 0;
333 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
335 pvc_device *pvc = dev_to_pvc(dev);
336 fr_proto_pvc_info info;
338 if (ifr->ifr_settings.type == IF_GET_PROTO) {
339 if (dev->type == ARPHRD_ETHER)
340 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
341 else
342 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
344 if (ifr->ifr_settings.size < sizeof(info)) {
345 /* data size wanted */
346 ifr->ifr_settings.size = sizeof(info);
347 return -ENOBUFS;
350 info.dlci = pvc->dlci;
351 memcpy(info.master, pvc->master->name, IFNAMSIZ);
352 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
353 &info, sizeof(info)))
354 return -EFAULT;
355 return 0;
358 return -EINVAL;
362 static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
364 return netdev_priv(dev);
369 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
371 pvc_device *pvc = dev_to_pvc(dev);
372 struct net_device_stats *stats = pvc_get_stats(dev);
374 if (pvc->state.active) {
375 if (dev->type == ARPHRD_ETHER) {
376 int pad = ETH_ZLEN - skb->len;
377 if (pad > 0) { /* Pad the frame with zeros */
378 int len = skb->len;
379 if (skb_tailroom(skb) < pad)
380 if (pskb_expand_head(skb, 0, pad,
381 GFP_ATOMIC)) {
382 stats->tx_dropped++;
383 dev_kfree_skb(skb);
384 return 0;
386 skb_put(skb, pad);
387 memset(skb->data + len, 0, pad);
389 skb->protocol = __constant_htons(ETH_P_802_3);
391 if (!fr_hard_header(&skb, pvc->dlci)) {
392 stats->tx_bytes += skb->len;
393 stats->tx_packets++;
394 if (pvc->state.fecn) /* TX Congestion counter */
395 stats->tx_compressed++;
396 skb->dev = pvc->master;
397 dev_queue_xmit(skb);
398 return 0;
402 stats->tx_dropped++;
403 dev_kfree_skb(skb);
404 return 0;
409 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
411 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
412 return -EINVAL;
413 dev->mtu = new_mtu;
414 return 0;
419 static inline void fr_log_dlci_active(pvc_device *pvc)
421 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
422 pvc->master->name,
423 pvc->dlci,
424 pvc->main ? pvc->main->name : "",
425 pvc->main && pvc->ether ? " " : "",
426 pvc->ether ? pvc->ether->name : "",
427 pvc->state.new ? " new" : "",
428 !pvc->state.exist ? "deleted" :
429 pvc->state.active ? "active" : "inactive");
434 static inline u8 fr_lmi_nextseq(u8 x)
436 x++;
437 return x ? x : 1;
442 static void fr_lmi_send(struct net_device *dev, int fullrep)
444 hdlc_device *hdlc = dev_to_hdlc(dev);
445 struct sk_buff *skb;
446 pvc_device *pvc = hdlc->state.fr.first_pvc;
447 int lmi = hdlc->state.fr.settings.lmi;
448 int dce = hdlc->state.fr.settings.dce;
449 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
450 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
451 u8 *data;
452 int i = 0;
454 if (dce && fullrep) {
455 len += hdlc->state.fr.dce_pvc_count * (2 + stat_len);
456 if (len > HDLC_MAX_MRU) {
457 printk(KERN_WARNING "%s: Too many PVCs while sending "
458 "LMI full report\n", dev->name);
459 return;
463 skb = dev_alloc_skb(len);
464 if (!skb) {
465 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
466 dev->name);
467 return;
469 memset(skb->data, 0, len);
470 skb_reserve(skb, 4);
471 if (lmi == LMI_CISCO) {
472 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
473 fr_hard_header(&skb, LMI_CISCO_DLCI);
474 } else {
475 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
476 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
478 data = skb->tail;
479 data[i++] = LMI_CALLREF;
480 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
481 if (lmi == LMI_ANSI)
482 data[i++] = LMI_ANSI_LOCKSHIFT;
483 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
484 LMI_ANSI_CISCO_REPTYPE;
485 data[i++] = LMI_REPT_LEN;
486 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
487 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
488 data[i++] = LMI_INTEG_LEN;
489 data[i++] = hdlc->state.fr.txseq =fr_lmi_nextseq(hdlc->state.fr.txseq);
490 data[i++] = hdlc->state.fr.rxseq;
492 if (dce && fullrep) {
493 while (pvc) {
494 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
495 LMI_ANSI_CISCO_PVCSTAT;
496 data[i++] = stat_len;
498 /* LMI start/restart */
499 if (hdlc->state.fr.reliable && !pvc->state.exist) {
500 pvc->state.exist = pvc->state.new = 1;
501 fr_log_dlci_active(pvc);
504 /* ifconfig PVC up */
505 if (pvc->open_count && !pvc->state.active &&
506 pvc->state.exist && !pvc->state.new) {
507 pvc_carrier(1, pvc);
508 pvc->state.active = 1;
509 fr_log_dlci_active(pvc);
512 if (lmi == LMI_CISCO) {
513 data[i] = pvc->dlci >> 8;
514 data[i + 1] = pvc->dlci & 0xFF;
515 } else {
516 data[i] = (pvc->dlci >> 4) & 0x3F;
517 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
518 data[i + 2] = 0x80;
521 if (pvc->state.new)
522 data[i + 2] |= 0x08;
523 else if (pvc->state.active)
524 data[i + 2] |= 0x02;
526 i += stat_len;
527 pvc = pvc->next;
531 skb_put(skb, i);
532 skb->priority = TC_PRIO_CONTROL;
533 skb->dev = dev;
534 skb->nh.raw = skb->data;
536 dev_queue_xmit(skb);
541 static void fr_set_link_state(int reliable, struct net_device *dev)
543 hdlc_device *hdlc = dev_to_hdlc(dev);
544 pvc_device *pvc = hdlc->state.fr.first_pvc;
546 hdlc->state.fr.reliable = reliable;
547 if (reliable) {
548 #if 0
549 if (!netif_carrier_ok(dev))
550 netif_carrier_on(dev);
551 #endif
553 hdlc->state.fr.n391cnt = 0; /* Request full status */
554 hdlc->state.fr.dce_changed = 1;
556 if (hdlc->state.fr.settings.lmi == LMI_NONE) {
557 while (pvc) { /* Activate all PVCs */
558 pvc_carrier(1, pvc);
559 pvc->state.exist = pvc->state.active = 1;
560 pvc->state.new = 0;
561 pvc = pvc->next;
564 } else {
565 #if 0
566 if (netif_carrier_ok(dev))
567 netif_carrier_off(dev);
568 #endif
570 while (pvc) { /* Deactivate all PVCs */
571 pvc_carrier(0, pvc);
572 pvc->state.exist = pvc->state.active = 0;
573 pvc->state.new = 0;
574 if (!hdlc->state.fr.settings.dce)
575 pvc->state.bandwidth = 0;
576 pvc = pvc->next;
583 static void fr_timer(unsigned long arg)
585 struct net_device *dev = (struct net_device *)arg;
586 hdlc_device *hdlc = dev_to_hdlc(dev);
587 int i, cnt = 0, reliable;
588 u32 list;
590 if (hdlc->state.fr.settings.dce) {
591 reliable = hdlc->state.fr.request &&
592 time_before(jiffies, hdlc->state.fr.last_poll +
593 hdlc->state.fr.settings.t392 * HZ);
594 hdlc->state.fr.request = 0;
595 } else {
596 hdlc->state.fr.last_errors <<= 1; /* Shift the list */
597 if (hdlc->state.fr.request) {
598 if (hdlc->state.fr.reliable)
599 printk(KERN_INFO "%s: No LMI status reply "
600 "received\n", dev->name);
601 hdlc->state.fr.last_errors |= 1;
604 list = hdlc->state.fr.last_errors;
605 for (i = 0; i < hdlc->state.fr.settings.n393; i++, list >>= 1)
606 cnt += (list & 1); /* errors count */
608 reliable = (cnt < hdlc->state.fr.settings.n392);
611 if (hdlc->state.fr.reliable != reliable) {
612 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
613 reliable ? "" : "un");
614 fr_set_link_state(reliable, dev);
617 if (hdlc->state.fr.settings.dce)
618 hdlc->state.fr.timer.expires = jiffies +
619 hdlc->state.fr.settings.t392 * HZ;
620 else {
621 if (hdlc->state.fr.n391cnt)
622 hdlc->state.fr.n391cnt--;
624 fr_lmi_send(dev, hdlc->state.fr.n391cnt == 0);
626 hdlc->state.fr.last_poll = jiffies;
627 hdlc->state.fr.request = 1;
628 hdlc->state.fr.timer.expires = jiffies +
629 hdlc->state.fr.settings.t391 * HZ;
632 hdlc->state.fr.timer.function = fr_timer;
633 hdlc->state.fr.timer.data = arg;
634 add_timer(&hdlc->state.fr.timer);
639 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
641 hdlc_device *hdlc = dev_to_hdlc(dev);
642 pvc_device *pvc;
643 u8 rxseq, txseq;
644 int lmi = hdlc->state.fr.settings.lmi;
645 int dce = hdlc->state.fr.settings.dce;
646 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
648 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
649 LMI_CCITT_CISCO_LENGTH)) {
650 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
651 return 1;
654 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
655 NLPID_CCITT_ANSI_LMI)) {
656 printk(KERN_INFO "%s: Received non-LMI frame with LMI"
657 " DLCI\n", dev->name);
658 return 1;
661 if (skb->data[4] != LMI_CALLREF) {
662 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
663 dev->name, skb->data[4]);
664 return 1;
667 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
668 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
669 dev->name, skb->data[5]);
670 return 1;
673 if (lmi == LMI_ANSI) {
674 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
675 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
676 " message (0x%02X)\n", dev->name, skb->data[6]);
677 return 1;
679 i = 7;
680 } else
681 i = 6;
683 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
684 LMI_ANSI_CISCO_REPTYPE)) {
685 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
686 dev->name, skb->data[i]);
687 return 1;
690 if (skb->data[++i] != LMI_REPT_LEN) {
691 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
692 " (%u)\n", dev->name, skb->data[i]);
693 return 1;
696 reptype = skb->data[++i];
697 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
698 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
699 dev->name, reptype);
700 return 1;
703 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
704 LMI_ANSI_CISCO_ALIVE)) {
705 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
706 " IE (0x%02X)\n", dev->name, skb->data[i]);
707 return 1;
710 if (skb->data[++i] != LMI_INTEG_LEN) {
711 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
712 " IE length (%u)\n", dev->name, skb->data[i]);
713 return 1;
715 i++;
717 hdlc->state.fr.rxseq = skb->data[i++]; /* TX sequence from peer */
718 rxseq = skb->data[i++]; /* Should confirm our sequence */
720 txseq = hdlc->state.fr.txseq;
722 if (dce)
723 hdlc->state.fr.last_poll = jiffies;
725 error = 0;
726 if (!hdlc->state.fr.reliable)
727 error = 1;
729 if (rxseq == 0 || rxseq != txseq) {
730 hdlc->state.fr.n391cnt = 0; /* Ask for full report next time */
731 error = 1;
734 if (dce) {
735 if (hdlc->state.fr.fullrep_sent && !error) {
736 /* Stop sending full report - the last one has been confirmed by DTE */
737 hdlc->state.fr.fullrep_sent = 0;
738 pvc = hdlc->state.fr.first_pvc;
739 while (pvc) {
740 if (pvc->state.new) {
741 pvc->state.new = 0;
743 /* Tell DTE that new PVC is now active */
744 hdlc->state.fr.dce_changed = 1;
746 pvc = pvc->next;
750 if (hdlc->state.fr.dce_changed) {
751 reptype = LMI_FULLREP;
752 hdlc->state.fr.fullrep_sent = 1;
753 hdlc->state.fr.dce_changed = 0;
756 hdlc->state.fr.request = 1; /* got request */
757 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
758 return 0;
761 /* DTE */
763 hdlc->state.fr.request = 0; /* got response, no request pending */
765 if (error)
766 return 0;
768 if (reptype != LMI_FULLREP)
769 return 0;
771 pvc = hdlc->state.fr.first_pvc;
773 while (pvc) {
774 pvc->state.deleted = 1;
775 pvc = pvc->next;
778 no_ram = 0;
779 while (skb->len >= i + 2 + stat_len) {
780 u16 dlci;
781 u32 bw;
782 unsigned int active, new;
784 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
785 LMI_ANSI_CISCO_PVCSTAT)) {
786 printk(KERN_INFO "%s: Not an LMI PVC status IE"
787 " (0x%02X)\n", dev->name, skb->data[i]);
788 return 1;
791 if (skb->data[++i] != stat_len) {
792 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
793 " (%u)\n", dev->name, skb->data[i]);
794 return 1;
796 i++;
798 new = !! (skb->data[i + 2] & 0x08);
799 active = !! (skb->data[i + 2] & 0x02);
800 if (lmi == LMI_CISCO) {
801 dlci = (skb->data[i] << 8) | skb->data[i + 1];
802 bw = (skb->data[i + 3] << 16) |
803 (skb->data[i + 4] << 8) |
804 (skb->data[i + 5]);
805 } else {
806 dlci = ((skb->data[i] & 0x3F) << 4) |
807 ((skb->data[i + 1] & 0x78) >> 3);
808 bw = 0;
811 pvc = add_pvc(dev, dlci);
813 if (!pvc && !no_ram) {
814 printk(KERN_WARNING
815 "%s: Memory squeeze on fr_lmi_recv()\n",
816 dev->name);
817 no_ram = 1;
820 if (pvc) {
821 pvc->state.exist = 1;
822 pvc->state.deleted = 0;
823 if (active != pvc->state.active ||
824 new != pvc->state.new ||
825 bw != pvc->state.bandwidth ||
826 !pvc->state.exist) {
827 pvc->state.new = new;
828 pvc->state.active = active;
829 pvc->state.bandwidth = bw;
830 pvc_carrier(active, pvc);
831 fr_log_dlci_active(pvc);
835 i += stat_len;
838 pvc = hdlc->state.fr.first_pvc;
840 while (pvc) {
841 if (pvc->state.deleted && pvc->state.exist) {
842 pvc_carrier(0, pvc);
843 pvc->state.active = pvc->state.new = 0;
844 pvc->state.exist = 0;
845 pvc->state.bandwidth = 0;
846 fr_log_dlci_active(pvc);
848 pvc = pvc->next;
851 /* Next full report after N391 polls */
852 hdlc->state.fr.n391cnt = hdlc->state.fr.settings.n391;
854 return 0;
859 static int fr_rx(struct sk_buff *skb)
861 struct net_device *ndev = skb->dev;
862 hdlc_device *hdlc = dev_to_hdlc(ndev);
863 fr_hdr *fh = (fr_hdr*)skb->data;
864 u8 *data = skb->data;
865 u16 dlci;
866 pvc_device *pvc;
867 struct net_device *dev = NULL;
869 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
870 goto rx_error;
872 dlci = q922_to_dlci(skb->data);
874 if ((dlci == LMI_CCITT_ANSI_DLCI &&
875 (hdlc->state.fr.settings.lmi == LMI_ANSI ||
876 hdlc->state.fr.settings.lmi == LMI_CCITT)) ||
877 (dlci == LMI_CISCO_DLCI &&
878 hdlc->state.fr.settings.lmi == LMI_CISCO)) {
879 if (fr_lmi_recv(ndev, skb))
880 goto rx_error;
881 dev_kfree_skb_any(skb);
882 return NET_RX_SUCCESS;
885 pvc = find_pvc(hdlc, dlci);
886 if (!pvc) {
887 #ifdef DEBUG_PKT
888 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
889 ndev->name, dlci);
890 #endif
891 dev_kfree_skb_any(skb);
892 return NET_RX_DROP;
895 if (pvc->state.fecn != fh->fecn) {
896 #ifdef DEBUG_ECN
897 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", ndev->name,
898 dlci, fh->fecn ? "N" : "FF");
899 #endif
900 pvc->state.fecn ^= 1;
903 if (pvc->state.becn != fh->becn) {
904 #ifdef DEBUG_ECN
905 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", ndev->name,
906 dlci, fh->becn ? "N" : "FF");
907 #endif
908 pvc->state.becn ^= 1;
912 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
913 hdlc->stats.rx_dropped++;
914 return NET_RX_DROP;
917 if (data[3] == NLPID_IP) {
918 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
919 dev = pvc->main;
920 skb->protocol = htons(ETH_P_IP);
922 } else if (data[3] == NLPID_IPV6) {
923 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
924 dev = pvc->main;
925 skb->protocol = htons(ETH_P_IPV6);
927 } else if (skb->len > 10 && data[3] == FR_PAD &&
928 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
929 u16 oui = ntohs(*(u16*)(data + 6));
930 u16 pid = ntohs(*(u16*)(data + 8));
931 skb_pull(skb, 10);
933 switch ((((u32)oui) << 16) | pid) {
934 case ETH_P_ARP: /* routed frame with SNAP */
935 case ETH_P_IPX:
936 case ETH_P_IP: /* a long variant */
937 case ETH_P_IPV6:
938 dev = pvc->main;
939 skb->protocol = htons(pid);
940 break;
942 case 0x80C20007: /* bridged Ethernet frame */
943 if ((dev = pvc->ether) != NULL)
944 skb->protocol = eth_type_trans(skb, dev);
945 break;
947 default:
948 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
949 "PID=%x\n", ndev->name, oui, pid);
950 dev_kfree_skb_any(skb);
951 return NET_RX_DROP;
953 } else {
954 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
955 "length = %i\n", ndev->name, data[3], skb->len);
956 dev_kfree_skb_any(skb);
957 return NET_RX_DROP;
960 if (dev) {
961 struct net_device_stats *stats = pvc_get_stats(dev);
962 stats->rx_packets++; /* PVC traffic */
963 stats->rx_bytes += skb->len;
964 if (pvc->state.becn)
965 stats->rx_compressed++;
966 skb->dev = dev;
967 netif_rx(skb);
968 return NET_RX_SUCCESS;
969 } else {
970 dev_kfree_skb_any(skb);
971 return NET_RX_DROP;
974 rx_error:
975 hdlc->stats.rx_errors++; /* Mark error */
976 dev_kfree_skb_any(skb);
977 return NET_RX_DROP;
982 static void fr_start(struct net_device *dev)
984 hdlc_device *hdlc = dev_to_hdlc(dev);
985 #ifdef DEBUG_LINK
986 printk(KERN_DEBUG "fr_start\n");
987 #endif
988 if (hdlc->state.fr.settings.lmi != LMI_NONE) {
989 hdlc->state.fr.reliable = 0;
990 hdlc->state.fr.dce_changed = 1;
991 hdlc->state.fr.request = 0;
992 hdlc->state.fr.fullrep_sent = 0;
993 hdlc->state.fr.last_errors = 0xFFFFFFFF;
994 hdlc->state.fr.n391cnt = 0;
995 hdlc->state.fr.txseq = hdlc->state.fr.rxseq = 0;
997 init_timer(&hdlc->state.fr.timer);
998 /* First poll after 1 s */
999 hdlc->state.fr.timer.expires = jiffies + HZ;
1000 hdlc->state.fr.timer.function = fr_timer;
1001 hdlc->state.fr.timer.data = (unsigned long)dev;
1002 add_timer(&hdlc->state.fr.timer);
1003 } else
1004 fr_set_link_state(1, dev);
1009 static void fr_stop(struct net_device *dev)
1011 hdlc_device *hdlc = dev_to_hdlc(dev);
1012 #ifdef DEBUG_LINK
1013 printk(KERN_DEBUG "fr_stop\n");
1014 #endif
1015 if (hdlc->state.fr.settings.lmi != LMI_NONE)
1016 del_timer_sync(&hdlc->state.fr.timer);
1017 fr_set_link_state(0, dev);
1022 static void fr_close(struct net_device *dev)
1024 hdlc_device *hdlc = dev_to_hdlc(dev);
1025 pvc_device *pvc = hdlc->state.fr.first_pvc;
1027 while (pvc) { /* Shutdown all PVCs for this FRAD */
1028 if (pvc->main)
1029 dev_close(pvc->main);
1030 if (pvc->ether)
1031 dev_close(pvc->ether);
1032 pvc = pvc->next;
1036 static void dlci_setup(struct net_device *dev)
1038 dev->type = ARPHRD_DLCI;
1039 dev->flags = IFF_POINTOPOINT;
1040 dev->hard_header_len = 10;
1041 dev->addr_len = 2;
1044 static int fr_add_pvc(struct net_device *master, unsigned int dlci, int type)
1046 hdlc_device *hdlc = dev_to_hdlc(master);
1047 pvc_device *pvc = NULL;
1048 struct net_device *dev;
1049 int result, used;
1050 char * prefix = "pvc%d";
1052 if (type == ARPHRD_ETHER)
1053 prefix = "pvceth%d";
1055 if ((pvc = add_pvc(master, dlci)) == NULL) {
1056 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1057 master->name);
1058 return -ENOBUFS;
1061 if (*get_dev_p(pvc, type))
1062 return -EEXIST;
1064 used = pvc_is_used(pvc);
1066 if (type == ARPHRD_ETHER)
1067 dev = alloc_netdev(sizeof(struct net_device_stats),
1068 "pvceth%d", ether_setup);
1069 else
1070 dev = alloc_netdev(sizeof(struct net_device_stats),
1071 "pvc%d", dlci_setup);
1073 if (!dev) {
1074 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1075 master->name);
1076 delete_unused_pvcs(hdlc);
1077 return -ENOBUFS;
1080 if (type == ARPHRD_ETHER) {
1081 memcpy(dev->dev_addr, "\x00\x01", 2);
1082 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1083 } else {
1084 *(u16*)dev->dev_addr = htons(dlci);
1085 dlci_to_q922(dev->broadcast, dlci);
1087 dev->hard_start_xmit = pvc_xmit;
1088 dev->get_stats = pvc_get_stats;
1089 dev->open = pvc_open;
1090 dev->stop = pvc_close;
1091 dev->do_ioctl = pvc_ioctl;
1092 dev->change_mtu = pvc_change_mtu;
1093 dev->mtu = HDLC_MAX_MTU;
1094 dev->tx_queue_len = 0;
1095 dev->priv = pvc;
1097 result = dev_alloc_name(dev, dev->name);
1098 if (result < 0) {
1099 free_netdev(dev);
1100 delete_unused_pvcs(hdlc);
1101 return result;
1104 if (register_netdevice(dev) != 0) {
1105 free_netdev(dev);
1106 delete_unused_pvcs(hdlc);
1107 return -EIO;
1110 dev->destructor = free_netdev;
1111 *get_dev_p(pvc, type) = dev;
1112 if (!used) {
1113 hdlc->state.fr.dce_changed = 1;
1114 hdlc->state.fr.dce_pvc_count++;
1116 return 0;
1121 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1123 pvc_device *pvc;
1124 struct net_device *dev;
1126 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1127 return -ENOENT;
1129 if ((dev = *get_dev_p(pvc, type)) == NULL)
1130 return -ENOENT;
1132 if (dev->flags & IFF_UP)
1133 return -EBUSY; /* PVC in use */
1135 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1136 *get_dev_p(pvc, type) = NULL;
1138 if (!pvc_is_used(pvc)) {
1139 hdlc->state.fr.dce_pvc_count--;
1140 hdlc->state.fr.dce_changed = 1;
1142 delete_unused_pvcs(hdlc);
1143 return 0;
1148 static void fr_destroy(hdlc_device *hdlc)
1150 pvc_device *pvc;
1152 pvc = hdlc->state.fr.first_pvc;
1153 hdlc->state.fr.first_pvc = NULL; /* All PVCs destroyed */
1154 hdlc->state.fr.dce_pvc_count = 0;
1155 hdlc->state.fr.dce_changed = 1;
1157 while (pvc) {
1158 pvc_device *next = pvc->next;
1159 /* destructors will free_netdev() main and ether */
1160 if (pvc->main)
1161 unregister_netdevice(pvc->main);
1163 if (pvc->ether)
1164 unregister_netdevice(pvc->ether);
1166 kfree(pvc);
1167 pvc = next;
1173 int hdlc_fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1175 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1176 const size_t size = sizeof(fr_proto);
1177 fr_proto new_settings;
1178 hdlc_device *hdlc = dev_to_hdlc(dev);
1179 fr_proto_pvc pvc;
1180 int result;
1182 switch (ifr->ifr_settings.type) {
1183 case IF_GET_PROTO:
1184 ifr->ifr_settings.type = IF_PROTO_FR;
1185 if (ifr->ifr_settings.size < size) {
1186 ifr->ifr_settings.size = size; /* data size wanted */
1187 return -ENOBUFS;
1189 if (copy_to_user(fr_s, &hdlc->state.fr.settings, size))
1190 return -EFAULT;
1191 return 0;
1193 case IF_PROTO_FR:
1194 if(!capable(CAP_NET_ADMIN))
1195 return -EPERM;
1197 if(dev->flags & IFF_UP)
1198 return -EBUSY;
1200 if (copy_from_user(&new_settings, fr_s, size))
1201 return -EFAULT;
1203 if (new_settings.lmi == LMI_DEFAULT)
1204 new_settings.lmi = LMI_ANSI;
1206 if ((new_settings.lmi != LMI_NONE &&
1207 new_settings.lmi != LMI_ANSI &&
1208 new_settings.lmi != LMI_CCITT &&
1209 new_settings.lmi != LMI_CISCO) ||
1210 new_settings.t391 < 1 ||
1211 new_settings.t392 < 2 ||
1212 new_settings.n391 < 1 ||
1213 new_settings.n392 < 1 ||
1214 new_settings.n393 < new_settings.n392 ||
1215 new_settings.n393 > 32 ||
1216 (new_settings.dce != 0 &&
1217 new_settings.dce != 1))
1218 return -EINVAL;
1220 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1221 if (result)
1222 return result;
1224 if (hdlc->proto.id != IF_PROTO_FR) {
1225 hdlc_proto_detach(hdlc);
1226 hdlc->state.fr.first_pvc = NULL;
1227 hdlc->state.fr.dce_pvc_count = 0;
1229 memcpy(&hdlc->state.fr.settings, &new_settings, size);
1230 memset(&hdlc->proto, 0, sizeof(hdlc->proto));
1232 hdlc->proto.close = fr_close;
1233 hdlc->proto.start = fr_start;
1234 hdlc->proto.stop = fr_stop;
1235 hdlc->proto.detach = fr_destroy;
1236 hdlc->proto.netif_rx = fr_rx;
1237 hdlc->proto.id = IF_PROTO_FR;
1238 dev->hard_start_xmit = hdlc->xmit;
1239 dev->hard_header = NULL;
1240 dev->type = ARPHRD_FRAD;
1241 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
1242 dev->addr_len = 0;
1243 return 0;
1245 case IF_PROTO_FR_ADD_PVC:
1246 case IF_PROTO_FR_DEL_PVC:
1247 case IF_PROTO_FR_ADD_ETH_PVC:
1248 case IF_PROTO_FR_DEL_ETH_PVC:
1249 if(!capable(CAP_NET_ADMIN))
1250 return -EPERM;
1252 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1253 sizeof(fr_proto_pvc)))
1254 return -EFAULT;
1256 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1257 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1259 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1260 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1261 result = ARPHRD_ETHER; /* bridged Ethernet device */
1262 else
1263 result = ARPHRD_DLCI;
1265 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1266 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1267 return fr_add_pvc(dev, pvc.dlci, result);
1268 else
1269 return fr_del_pvc(hdlc, pvc.dlci, result);
1272 return -EINVAL;