2 * Generic HDLC support routines for Linux
5 * Copyright (C) 1999 - 2006 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.
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
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
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/inetdevice.h>
46 #include <linux/lapb.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/etherdevice.h>
49 #include <linux/hdlc.h>
61 #define NLPID_IPV6 0x8E
62 #define NLPID_SNAP 0x80
63 #define NLPID_PAD 0x00
64 #define NLPID_CCITT_ANSI_LMI 0x08
65 #define NLPID_CISCO_LMI 0x09
68 #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
69 #define LMI_CISCO_DLCI 1023
71 #define LMI_CALLREF 0x00 /* Call Reference */
72 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
73 #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
74 #define LMI_CCITT_REPTYPE 0x51
75 #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
76 #define LMI_CCITT_ALIVE 0x53
77 #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
78 #define LMI_CCITT_PVCSTAT 0x57
80 #define LMI_FULLREP 0x00 /* full report */
81 #define LMI_INTEGRITY 0x01 /* link integrity report */
82 #define LMI_SINGLE 0x02 /* single PVC report */
84 #define LMI_STATUS_ENQUIRY 0x75
85 #define LMI_STATUS 0x7D /* reply */
87 #define LMI_REPT_LEN 1 /* report type element length */
88 #define LMI_INTEG_LEN 2 /* link integrity element length */
90 #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
91 #define LMI_ANSI_LENGTH 14
95 #if defined(__LITTLE_ENDIAN_BITFIELD)
116 }__attribute__ ((packed)) fr_hdr;
119 typedef struct pvc_device_struct {
120 struct net_device *frad;
121 struct net_device *main;
122 struct net_device *ether; /* bridged Ethernet interface */
123 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
129 unsigned int active: 1;
130 unsigned int exist: 1;
131 unsigned int deleted: 1;
132 unsigned int fecn: 1;
133 unsigned int becn: 1;
134 unsigned int bandwidth; /* Cisco LMI reporting only */
140 pvc_device *first_pvc;
143 struct timer_list timer;
144 unsigned long last_poll;
149 u32 last_errors; /* last errors bit list */
151 u8 txseq; /* TX sequence number */
152 u8 rxseq; /* RX sequence number */
156 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
159 static inline u16 q922_to_dlci(u8 *hdr)
161 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
165 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
167 hdr[0] = (dlci >> 2) & 0xFC;
168 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
172 static inline struct frad_state* state(hdlc_device *hdlc)
174 return(struct frad_state *)(hdlc->state);
178 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
180 pvc_device *pvc = state(hdlc)->first_pvc;
183 if (pvc->dlci == dlci)
185 if (pvc->dlci > dlci)
186 return NULL; /* the listed is sorted */
194 static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
196 hdlc_device *hdlc = dev_to_hdlc(dev);
197 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
200 if ((*pvc_p)->dlci == dlci)
202 if ((*pvc_p)->dlci > dlci)
203 break; /* the list is sorted */
204 pvc_p = &(*pvc_p)->next;
207 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
209 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
216 pvc->next = *pvc_p; /* Put it in the chain */
222 static inline int pvc_is_used(pvc_device *pvc)
224 return pvc->main || pvc->ether;
228 static inline void pvc_carrier(int on, pvc_device *pvc)
232 if (!netif_carrier_ok(pvc->main))
233 netif_carrier_on(pvc->main);
235 if (!netif_carrier_ok(pvc->ether))
236 netif_carrier_on(pvc->ether);
239 if (netif_carrier_ok(pvc->main))
240 netif_carrier_off(pvc->main);
242 if (netif_carrier_ok(pvc->ether))
243 netif_carrier_off(pvc->ether);
248 static inline void delete_unused_pvcs(hdlc_device *hdlc)
250 pvc_device **pvc_p = &state(hdlc)->first_pvc;
253 if (!pvc_is_used(*pvc_p)) {
254 pvc_device *pvc = *pvc_p;
256 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
262 pvc_p = &(*pvc_p)->next;
267 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
269 if (type == ARPHRD_ETHER)
276 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
279 struct sk_buff *skb = *skb_p;
281 switch (skb->protocol) {
282 case __constant_htons(NLPID_CCITT_ANSI_LMI):
284 skb_push(skb, head_len);
285 skb->data[3] = NLPID_CCITT_ANSI_LMI;
288 case __constant_htons(NLPID_CISCO_LMI):
290 skb_push(skb, head_len);
291 skb->data[3] = NLPID_CISCO_LMI;
294 case __constant_htons(ETH_P_IP):
296 skb_push(skb, head_len);
297 skb->data[3] = NLPID_IP;
300 case __constant_htons(ETH_P_IPV6):
302 skb_push(skb, head_len);
303 skb->data[3] = NLPID_IPV6;
306 case __constant_htons(ETH_P_802_3):
308 if (skb_headroom(skb) < head_len) {
309 struct sk_buff *skb2 = skb_realloc_headroom(skb,
316 skb_push(skb, head_len);
317 skb->data[3] = FR_PAD;
318 skb->data[4] = NLPID_SNAP;
319 skb->data[5] = FR_PAD;
323 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
328 skb_push(skb, head_len);
329 skb->data[3] = FR_PAD;
330 skb->data[4] = NLPID_SNAP;
331 skb->data[5] = FR_PAD;
332 skb->data[6] = FR_PAD;
333 skb->data[7] = FR_PAD;
334 *(__be16*)(skb->data + 8) = skb->protocol;
337 dlci_to_q922(skb->data, dlci);
338 skb->data[2] = FR_UI;
344 static int pvc_open(struct net_device *dev)
346 pvc_device *pvc = dev->priv;
348 if ((pvc->frad->flags & IFF_UP) == 0)
349 return -EIO; /* Frad must be UP in order to activate PVC */
351 if (pvc->open_count++ == 0) {
352 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
353 if (state(hdlc)->settings.lmi == LMI_NONE)
354 pvc->state.active = netif_carrier_ok(pvc->frad);
356 pvc_carrier(pvc->state.active, pvc);
357 state(hdlc)->dce_changed = 1;
364 static int pvc_close(struct net_device *dev)
366 pvc_device *pvc = dev->priv;
368 if (--pvc->open_count == 0) {
369 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
370 if (state(hdlc)->settings.lmi == LMI_NONE)
371 pvc->state.active = 0;
373 if (state(hdlc)->settings.dce) {
374 state(hdlc)->dce_changed = 1;
375 pvc->state.active = 0;
383 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
385 pvc_device *pvc = dev->priv;
386 fr_proto_pvc_info info;
388 if (ifr->ifr_settings.type == IF_GET_PROTO) {
389 if (dev->type == ARPHRD_ETHER)
390 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
392 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
394 if (ifr->ifr_settings.size < sizeof(info)) {
395 /* data size wanted */
396 ifr->ifr_settings.size = sizeof(info);
400 info.dlci = pvc->dlci;
401 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
402 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
403 &info, sizeof(info)))
411 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
413 pvc_device *pvc = dev->priv;
415 if (pvc->state.active) {
416 if (dev->type == ARPHRD_ETHER) {
417 int pad = ETH_ZLEN - skb->len;
418 if (pad > 0) { /* Pad the frame with zeros */
420 if (skb_tailroom(skb) < pad)
421 if (pskb_expand_head(skb, 0, pad,
423 dev->stats.tx_dropped++;
428 memset(skb->data + len, 0, pad);
430 skb->protocol = __constant_htons(ETH_P_802_3);
432 if (!fr_hard_header(&skb, pvc->dlci)) {
433 dev->stats.tx_bytes += skb->len;
434 dev->stats.tx_packets++;
435 if (pvc->state.fecn) /* TX Congestion counter */
436 dev->stats.tx_compressed++;
437 skb->dev = pvc->frad;
443 dev->stats.tx_dropped++;
450 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
452 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
460 static inline void fr_log_dlci_active(pvc_device *pvc)
462 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
465 pvc->main ? pvc->main->name : "",
466 pvc->main && pvc->ether ? " " : "",
467 pvc->ether ? pvc->ether->name : "",
468 pvc->state.new ? " new" : "",
469 !pvc->state.exist ? "deleted" :
470 pvc->state.active ? "active" : "inactive");
475 static inline u8 fr_lmi_nextseq(u8 x)
482 static void fr_lmi_send(struct net_device *dev, int fullrep)
484 hdlc_device *hdlc = dev_to_hdlc(dev);
486 pvc_device *pvc = state(hdlc)->first_pvc;
487 int lmi = state(hdlc)->settings.lmi;
488 int dce = state(hdlc)->settings.dce;
489 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
490 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
494 if (dce && fullrep) {
495 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
496 if (len > HDLC_MAX_MRU) {
497 printk(KERN_WARNING "%s: Too many PVCs while sending "
498 "LMI full report\n", dev->name);
503 skb = dev_alloc_skb(len);
505 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
509 memset(skb->data, 0, len);
511 if (lmi == LMI_CISCO) {
512 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
513 fr_hard_header(&skb, LMI_CISCO_DLCI);
515 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
516 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
518 data = skb_tail_pointer(skb);
519 data[i++] = LMI_CALLREF;
520 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
522 data[i++] = LMI_ANSI_LOCKSHIFT;
523 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
524 LMI_ANSI_CISCO_REPTYPE;
525 data[i++] = LMI_REPT_LEN;
526 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
527 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
528 data[i++] = LMI_INTEG_LEN;
529 data[i++] = state(hdlc)->txseq =
530 fr_lmi_nextseq(state(hdlc)->txseq);
531 data[i++] = state(hdlc)->rxseq;
533 if (dce && fullrep) {
535 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
536 LMI_ANSI_CISCO_PVCSTAT;
537 data[i++] = stat_len;
539 /* LMI start/restart */
540 if (state(hdlc)->reliable && !pvc->state.exist) {
541 pvc->state.exist = pvc->state.new = 1;
542 fr_log_dlci_active(pvc);
545 /* ifconfig PVC up */
546 if (pvc->open_count && !pvc->state.active &&
547 pvc->state.exist && !pvc->state.new) {
549 pvc->state.active = 1;
550 fr_log_dlci_active(pvc);
553 if (lmi == LMI_CISCO) {
554 data[i] = pvc->dlci >> 8;
555 data[i + 1] = pvc->dlci & 0xFF;
557 data[i] = (pvc->dlci >> 4) & 0x3F;
558 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
564 else if (pvc->state.active)
573 skb->priority = TC_PRIO_CONTROL;
575 skb_reset_network_header(skb);
582 static void fr_set_link_state(int reliable, struct net_device *dev)
584 hdlc_device *hdlc = dev_to_hdlc(dev);
585 pvc_device *pvc = state(hdlc)->first_pvc;
587 state(hdlc)->reliable = reliable;
589 netif_dormant_off(dev);
590 state(hdlc)->n391cnt = 0; /* Request full status */
591 state(hdlc)->dce_changed = 1;
593 if (state(hdlc)->settings.lmi == LMI_NONE) {
594 while (pvc) { /* Activate all PVCs */
596 pvc->state.exist = pvc->state.active = 1;
602 netif_dormant_on(dev);
603 while (pvc) { /* Deactivate all PVCs */
605 pvc->state.exist = pvc->state.active = 0;
607 if (!state(hdlc)->settings.dce)
608 pvc->state.bandwidth = 0;
615 static void fr_timer(unsigned long arg)
617 struct net_device *dev = (struct net_device *)arg;
618 hdlc_device *hdlc = dev_to_hdlc(dev);
619 int i, cnt = 0, reliable;
622 if (state(hdlc)->settings.dce) {
623 reliable = state(hdlc)->request &&
624 time_before(jiffies, state(hdlc)->last_poll +
625 state(hdlc)->settings.t392 * HZ);
626 state(hdlc)->request = 0;
628 state(hdlc)->last_errors <<= 1; /* Shift the list */
629 if (state(hdlc)->request) {
630 if (state(hdlc)->reliable)
631 printk(KERN_INFO "%s: No LMI status reply "
632 "received\n", dev->name);
633 state(hdlc)->last_errors |= 1;
636 list = state(hdlc)->last_errors;
637 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
638 cnt += (list & 1); /* errors count */
640 reliable = (cnt < state(hdlc)->settings.n392);
643 if (state(hdlc)->reliable != reliable) {
644 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
645 reliable ? "" : "un");
646 fr_set_link_state(reliable, dev);
649 if (state(hdlc)->settings.dce)
650 state(hdlc)->timer.expires = jiffies +
651 state(hdlc)->settings.t392 * HZ;
653 if (state(hdlc)->n391cnt)
654 state(hdlc)->n391cnt--;
656 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
658 state(hdlc)->last_poll = jiffies;
659 state(hdlc)->request = 1;
660 state(hdlc)->timer.expires = jiffies +
661 state(hdlc)->settings.t391 * HZ;
664 state(hdlc)->timer.function = fr_timer;
665 state(hdlc)->timer.data = arg;
666 add_timer(&state(hdlc)->timer);
670 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
672 hdlc_device *hdlc = dev_to_hdlc(dev);
675 int lmi = state(hdlc)->settings.lmi;
676 int dce = state(hdlc)->settings.dce;
677 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
679 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
680 LMI_CCITT_CISCO_LENGTH)) {
681 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
685 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
686 NLPID_CCITT_ANSI_LMI)) {
687 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
692 if (skb->data[4] != LMI_CALLREF) {
693 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
694 dev->name, skb->data[4]);
698 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
699 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
700 dev->name, skb->data[5]);
704 if (lmi == LMI_ANSI) {
705 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
706 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
707 " message (0x%02X)\n", dev->name, skb->data[6]);
714 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
715 LMI_ANSI_CISCO_REPTYPE)) {
716 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
717 dev->name, skb->data[i]);
721 if (skb->data[++i] != LMI_REPT_LEN) {
722 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
723 " (%u)\n", dev->name, skb->data[i]);
727 reptype = skb->data[++i];
728 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
729 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
734 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
735 LMI_ANSI_CISCO_ALIVE)) {
736 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
737 " IE (0x%02X)\n", dev->name, skb->data[i]);
741 if (skb->data[++i] != LMI_INTEG_LEN) {
742 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
743 " IE length (%u)\n", dev->name, skb->data[i]);
748 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
749 rxseq = skb->data[i++]; /* Should confirm our sequence */
751 txseq = state(hdlc)->txseq;
754 state(hdlc)->last_poll = jiffies;
757 if (!state(hdlc)->reliable)
760 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
761 state(hdlc)->n391cnt = 0;
766 if (state(hdlc)->fullrep_sent && !error) {
767 /* Stop sending full report - the last one has been confirmed by DTE */
768 state(hdlc)->fullrep_sent = 0;
769 pvc = state(hdlc)->first_pvc;
771 if (pvc->state.new) {
774 /* Tell DTE that new PVC is now active */
775 state(hdlc)->dce_changed = 1;
781 if (state(hdlc)->dce_changed) {
782 reptype = LMI_FULLREP;
783 state(hdlc)->fullrep_sent = 1;
784 state(hdlc)->dce_changed = 0;
787 state(hdlc)->request = 1; /* got request */
788 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
794 state(hdlc)->request = 0; /* got response, no request pending */
799 if (reptype != LMI_FULLREP)
802 pvc = state(hdlc)->first_pvc;
805 pvc->state.deleted = 1;
810 while (skb->len >= i + 2 + stat_len) {
813 unsigned int active, new;
815 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
816 LMI_ANSI_CISCO_PVCSTAT)) {
817 printk(KERN_INFO "%s: Not an LMI PVC status IE"
818 " (0x%02X)\n", dev->name, skb->data[i]);
822 if (skb->data[++i] != stat_len) {
823 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
824 " (%u)\n", dev->name, skb->data[i]);
829 new = !! (skb->data[i + 2] & 0x08);
830 active = !! (skb->data[i + 2] & 0x02);
831 if (lmi == LMI_CISCO) {
832 dlci = (skb->data[i] << 8) | skb->data[i + 1];
833 bw = (skb->data[i + 3] << 16) |
834 (skb->data[i + 4] << 8) |
837 dlci = ((skb->data[i] & 0x3F) << 4) |
838 ((skb->data[i + 1] & 0x78) >> 3);
842 pvc = add_pvc(dev, dlci);
844 if (!pvc && !no_ram) {
846 "%s: Memory squeeze on fr_lmi_recv()\n",
852 pvc->state.exist = 1;
853 pvc->state.deleted = 0;
854 if (active != pvc->state.active ||
855 new != pvc->state.new ||
856 bw != pvc->state.bandwidth ||
858 pvc->state.new = new;
859 pvc->state.active = active;
860 pvc->state.bandwidth = bw;
861 pvc_carrier(active, pvc);
862 fr_log_dlci_active(pvc);
869 pvc = state(hdlc)->first_pvc;
872 if (pvc->state.deleted && pvc->state.exist) {
874 pvc->state.active = pvc->state.new = 0;
875 pvc->state.exist = 0;
876 pvc->state.bandwidth = 0;
877 fr_log_dlci_active(pvc);
882 /* Next full report after N391 polls */
883 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
889 static int fr_rx(struct sk_buff *skb)
891 struct net_device *frad = skb->dev;
892 hdlc_device *hdlc = dev_to_hdlc(frad);
893 fr_hdr *fh = (fr_hdr*)skb->data;
894 u8 *data = skb->data;
897 struct net_device *dev = NULL;
899 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
902 dlci = q922_to_dlci(skb->data);
904 if ((dlci == LMI_CCITT_ANSI_DLCI &&
905 (state(hdlc)->settings.lmi == LMI_ANSI ||
906 state(hdlc)->settings.lmi == LMI_CCITT)) ||
907 (dlci == LMI_CISCO_DLCI &&
908 state(hdlc)->settings.lmi == LMI_CISCO)) {
909 if (fr_lmi_recv(frad, skb))
911 dev_kfree_skb_any(skb);
912 return NET_RX_SUCCESS;
915 pvc = find_pvc(hdlc, dlci);
918 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
921 dev_kfree_skb_any(skb);
925 if (pvc->state.fecn != fh->fecn) {
927 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
928 dlci, fh->fecn ? "N" : "FF");
930 pvc->state.fecn ^= 1;
933 if (pvc->state.becn != fh->becn) {
935 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
936 dlci, fh->becn ? "N" : "FF");
938 pvc->state.becn ^= 1;
942 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
943 frad->stats.rx_dropped++;
947 if (data[3] == NLPID_IP) {
948 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
950 skb->protocol = htons(ETH_P_IP);
952 } else if (data[3] == NLPID_IPV6) {
953 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
955 skb->protocol = htons(ETH_P_IPV6);
957 } else if (skb->len > 10 && data[3] == FR_PAD &&
958 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
959 u16 oui = ntohs(*(__be16*)(data + 6));
960 u16 pid = ntohs(*(__be16*)(data + 8));
963 switch ((((u32)oui) << 16) | pid) {
964 case ETH_P_ARP: /* routed frame with SNAP */
966 case ETH_P_IP: /* a long variant */
969 skb->protocol = htons(pid);
972 case 0x80C20007: /* bridged Ethernet frame */
973 if ((dev = pvc->ether) != NULL)
974 skb->protocol = eth_type_trans(skb, dev);
978 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
979 "PID=%x\n", frad->name, oui, pid);
980 dev_kfree_skb_any(skb);
984 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
985 "length = %i\n", frad->name, data[3], skb->len);
986 dev_kfree_skb_any(skb);
991 dev->stats.rx_packets++; /* PVC traffic */
992 dev->stats.rx_bytes += skb->len;
994 dev->stats.rx_compressed++;
997 return NET_RX_SUCCESS;
999 dev_kfree_skb_any(skb);
1004 frad->stats.rx_errors++; /* Mark error */
1005 dev_kfree_skb_any(skb);
1011 static void fr_start(struct net_device *dev)
1013 hdlc_device *hdlc = dev_to_hdlc(dev);
1015 printk(KERN_DEBUG "fr_start\n");
1017 if (state(hdlc)->settings.lmi != LMI_NONE) {
1018 state(hdlc)->reliable = 0;
1019 state(hdlc)->dce_changed = 1;
1020 state(hdlc)->request = 0;
1021 state(hdlc)->fullrep_sent = 0;
1022 state(hdlc)->last_errors = 0xFFFFFFFF;
1023 state(hdlc)->n391cnt = 0;
1024 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1026 init_timer(&state(hdlc)->timer);
1027 /* First poll after 1 s */
1028 state(hdlc)->timer.expires = jiffies + HZ;
1029 state(hdlc)->timer.function = fr_timer;
1030 state(hdlc)->timer.data = (unsigned long)dev;
1031 add_timer(&state(hdlc)->timer);
1033 fr_set_link_state(1, dev);
1037 static void fr_stop(struct net_device *dev)
1039 hdlc_device *hdlc = dev_to_hdlc(dev);
1041 printk(KERN_DEBUG "fr_stop\n");
1043 if (state(hdlc)->settings.lmi != LMI_NONE)
1044 del_timer_sync(&state(hdlc)->timer);
1045 fr_set_link_state(0, dev);
1049 static void fr_close(struct net_device *dev)
1051 hdlc_device *hdlc = dev_to_hdlc(dev);
1052 pvc_device *pvc = state(hdlc)->first_pvc;
1054 while (pvc) { /* Shutdown all PVCs for this FRAD */
1056 dev_close(pvc->main);
1058 dev_close(pvc->ether);
1064 static void pvc_setup(struct net_device *dev)
1066 dev->type = ARPHRD_DLCI;
1067 dev->flags = IFF_POINTOPOINT;
1068 dev->hard_header_len = 10;
1072 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1074 hdlc_device *hdlc = dev_to_hdlc(frad);
1076 struct net_device *dev;
1079 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1080 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1085 if (*get_dev_p(pvc, type))
1088 used = pvc_is_used(pvc);
1090 if (type == ARPHRD_ETHER)
1091 dev = alloc_netdev(0, "pvceth%d", ether_setup);
1093 dev = alloc_netdev(0, "pvc%d", pvc_setup);
1096 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1098 delete_unused_pvcs(hdlc);
1102 if (type == ARPHRD_ETHER)
1103 random_ether_addr(dev->dev_addr);
1105 *(__be16*)dev->dev_addr = htons(dlci);
1106 dlci_to_q922(dev->broadcast, dlci);
1108 dev->hard_start_xmit = pvc_xmit;
1109 dev->open = pvc_open;
1110 dev->stop = pvc_close;
1111 dev->do_ioctl = pvc_ioctl;
1112 dev->change_mtu = pvc_change_mtu;
1113 dev->mtu = HDLC_MAX_MTU;
1114 dev->tx_queue_len = 0;
1117 result = dev_alloc_name(dev, dev->name);
1120 delete_unused_pvcs(hdlc);
1124 if (register_netdevice(dev) != 0) {
1126 delete_unused_pvcs(hdlc);
1130 dev->destructor = free_netdev;
1131 *get_dev_p(pvc, type) = dev;
1133 state(hdlc)->dce_changed = 1;
1134 state(hdlc)->dce_pvc_count++;
1141 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1144 struct net_device *dev;
1146 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1149 if ((dev = *get_dev_p(pvc, type)) == NULL)
1152 if (dev->flags & IFF_UP)
1153 return -EBUSY; /* PVC in use */
1155 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1156 *get_dev_p(pvc, type) = NULL;
1158 if (!pvc_is_used(pvc)) {
1159 state(hdlc)->dce_pvc_count--;
1160 state(hdlc)->dce_changed = 1;
1162 delete_unused_pvcs(hdlc);
1168 static void fr_destroy(struct net_device *frad)
1170 hdlc_device *hdlc = dev_to_hdlc(frad);
1171 pvc_device *pvc = state(hdlc)->first_pvc;
1172 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1173 state(hdlc)->dce_pvc_count = 0;
1174 state(hdlc)->dce_changed = 1;
1177 pvc_device *next = pvc->next;
1178 /* destructors will free_netdev() main and ether */
1180 unregister_netdevice(pvc->main);
1183 unregister_netdevice(pvc->ether);
1191 static struct hdlc_proto proto = {
1195 .detach = fr_destroy,
1198 .module = THIS_MODULE,
1202 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1204 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1205 const size_t size = sizeof(fr_proto);
1206 fr_proto new_settings;
1207 hdlc_device *hdlc = dev_to_hdlc(dev);
1211 switch (ifr->ifr_settings.type) {
1213 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1215 ifr->ifr_settings.type = IF_PROTO_FR;
1216 if (ifr->ifr_settings.size < size) {
1217 ifr->ifr_settings.size = size; /* data size wanted */
1220 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1225 if(!capable(CAP_NET_ADMIN))
1228 if(dev->flags & IFF_UP)
1231 if (copy_from_user(&new_settings, fr_s, size))
1234 if (new_settings.lmi == LMI_DEFAULT)
1235 new_settings.lmi = LMI_ANSI;
1237 if ((new_settings.lmi != LMI_NONE &&
1238 new_settings.lmi != LMI_ANSI &&
1239 new_settings.lmi != LMI_CCITT &&
1240 new_settings.lmi != LMI_CISCO) ||
1241 new_settings.t391 < 1 ||
1242 new_settings.t392 < 2 ||
1243 new_settings.n391 < 1 ||
1244 new_settings.n392 < 1 ||
1245 new_settings.n393 < new_settings.n392 ||
1246 new_settings.n393 > 32 ||
1247 (new_settings.dce != 0 &&
1248 new_settings.dce != 1))
1251 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1255 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1256 result = attach_hdlc_protocol(dev, &proto,
1257 sizeof(struct frad_state));
1260 state(hdlc)->first_pvc = NULL;
1261 state(hdlc)->dce_pvc_count = 0;
1263 memcpy(&state(hdlc)->settings, &new_settings, size);
1265 dev->hard_start_xmit = hdlc->xmit;
1266 dev->type = ARPHRD_FRAD;
1269 case IF_PROTO_FR_ADD_PVC:
1270 case IF_PROTO_FR_DEL_PVC:
1271 case IF_PROTO_FR_ADD_ETH_PVC:
1272 case IF_PROTO_FR_DEL_ETH_PVC:
1273 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1276 if(!capable(CAP_NET_ADMIN))
1279 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1280 sizeof(fr_proto_pvc)))
1283 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1284 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1286 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1287 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1288 result = ARPHRD_ETHER; /* bridged Ethernet device */
1290 result = ARPHRD_DLCI;
1292 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1293 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1294 return fr_add_pvc(dev, pvc.dlci, result);
1296 return fr_del_pvc(hdlc, pvc.dlci, result);
1303 static int __init mod_init(void)
1305 register_hdlc_protocol(&proto);
1310 static void __exit mod_exit(void)
1312 unregister_hdlc_protocol(&proto);
1316 module_init(mod_init);
1317 module_exit(mod_exit);
1319 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1320 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1321 MODULE_LICENSE("GPL v2");