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 */
144 pvc_device *first_pvc;
147 struct timer_list timer;
148 unsigned long last_poll;
153 u32 last_errors; /* last errors bit list */
155 u8 txseq; /* TX sequence number */
156 u8 rxseq; /* RX sequence number */
160 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
163 static inline u16 q922_to_dlci(u8 *hdr)
165 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
169 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
171 hdr[0] = (dlci >> 2) & 0xFC;
172 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
176 static inline struct frad_state* state(hdlc_device *hdlc)
178 return(struct frad_state *)(hdlc->state);
181 static inline struct pvc_desc* pvcdev_to_desc(struct net_device *dev)
186 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
188 pvc_device *pvc = state(hdlc)->first_pvc;
191 if (pvc->dlci == dlci)
193 if (pvc->dlci > dlci)
194 return NULL; /* the listed is sorted */
202 static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
204 hdlc_device *hdlc = dev_to_hdlc(dev);
205 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
208 if ((*pvc_p)->dlci == dlci)
210 if ((*pvc_p)->dlci > dlci)
211 break; /* the list is sorted */
212 pvc_p = &(*pvc_p)->next;
215 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
217 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
224 pvc->next = *pvc_p; /* Put it in the chain */
230 static inline int pvc_is_used(pvc_device *pvc)
232 return pvc->main || pvc->ether;
236 static inline void pvc_carrier(int on, pvc_device *pvc)
240 if (!netif_carrier_ok(pvc->main))
241 netif_carrier_on(pvc->main);
243 if (!netif_carrier_ok(pvc->ether))
244 netif_carrier_on(pvc->ether);
247 if (netif_carrier_ok(pvc->main))
248 netif_carrier_off(pvc->main);
250 if (netif_carrier_ok(pvc->ether))
251 netif_carrier_off(pvc->ether);
256 static inline void delete_unused_pvcs(hdlc_device *hdlc)
258 pvc_device **pvc_p = &state(hdlc)->first_pvc;
261 if (!pvc_is_used(*pvc_p)) {
262 pvc_device *pvc = *pvc_p;
264 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
270 pvc_p = &(*pvc_p)->next;
275 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
277 if (type == ARPHRD_ETHER)
284 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
287 struct sk_buff *skb = *skb_p;
289 switch (skb->protocol) {
290 case __constant_htons(NLPID_CCITT_ANSI_LMI):
292 skb_push(skb, head_len);
293 skb->data[3] = NLPID_CCITT_ANSI_LMI;
296 case __constant_htons(NLPID_CISCO_LMI):
298 skb_push(skb, head_len);
299 skb->data[3] = NLPID_CISCO_LMI;
302 case __constant_htons(ETH_P_IP):
304 skb_push(skb, head_len);
305 skb->data[3] = NLPID_IP;
308 case __constant_htons(ETH_P_IPV6):
310 skb_push(skb, head_len);
311 skb->data[3] = NLPID_IPV6;
314 case __constant_htons(ETH_P_802_3):
316 if (skb_headroom(skb) < head_len) {
317 struct sk_buff *skb2 = skb_realloc_headroom(skb,
324 skb_push(skb, head_len);
325 skb->data[3] = FR_PAD;
326 skb->data[4] = NLPID_SNAP;
327 skb->data[5] = FR_PAD;
331 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
336 skb_push(skb, head_len);
337 skb->data[3] = FR_PAD;
338 skb->data[4] = NLPID_SNAP;
339 skb->data[5] = FR_PAD;
340 skb->data[6] = FR_PAD;
341 skb->data[7] = FR_PAD;
342 *(__be16*)(skb->data + 8) = skb->protocol;
345 dlci_to_q922(skb->data, dlci);
346 skb->data[2] = FR_UI;
352 static int pvc_open(struct net_device *dev)
354 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
356 if ((pvc->frad->flags & IFF_UP) == 0)
357 return -EIO; /* Frad must be UP in order to activate PVC */
359 if (pvc->open_count++ == 0) {
360 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
361 if (state(hdlc)->settings.lmi == LMI_NONE)
362 pvc->state.active = netif_carrier_ok(pvc->frad);
364 pvc_carrier(pvc->state.active, pvc);
365 state(hdlc)->dce_changed = 1;
372 static int pvc_close(struct net_device *dev)
374 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
376 if (--pvc->open_count == 0) {
377 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
378 if (state(hdlc)->settings.lmi == LMI_NONE)
379 pvc->state.active = 0;
381 if (state(hdlc)->settings.dce) {
382 state(hdlc)->dce_changed = 1;
383 pvc->state.active = 0;
391 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
393 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
394 fr_proto_pvc_info info;
396 if (ifr->ifr_settings.type == IF_GET_PROTO) {
397 if (dev->type == ARPHRD_ETHER)
398 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
400 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
402 if (ifr->ifr_settings.size < sizeof(info)) {
403 /* data size wanted */
404 ifr->ifr_settings.size = sizeof(info);
408 info.dlci = pvc->dlci;
409 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
410 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
411 &info, sizeof(info)))
419 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
421 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
423 if (pvc->state.active) {
424 if (dev->type == ARPHRD_ETHER) {
425 int pad = ETH_ZLEN - skb->len;
426 if (pad > 0) { /* Pad the frame with zeros */
428 if (skb_tailroom(skb) < pad)
429 if (pskb_expand_head(skb, 0, pad,
431 dev->stats.tx_dropped++;
436 memset(skb->data + len, 0, pad);
438 skb->protocol = __constant_htons(ETH_P_802_3);
440 if (!fr_hard_header(&skb, pvc->dlci)) {
441 dev->stats.tx_bytes += skb->len;
442 dev->stats.tx_packets++;
443 if (pvc->state.fecn) /* TX Congestion counter */
444 dev->stats.tx_compressed++;
445 skb->dev = pvc->frad;
451 dev->stats.tx_dropped++;
458 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
460 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
468 static inline void fr_log_dlci_active(pvc_device *pvc)
470 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
473 pvc->main ? pvc->main->name : "",
474 pvc->main && pvc->ether ? " " : "",
475 pvc->ether ? pvc->ether->name : "",
476 pvc->state.new ? " new" : "",
477 !pvc->state.exist ? "deleted" :
478 pvc->state.active ? "active" : "inactive");
483 static inline u8 fr_lmi_nextseq(u8 x)
490 static void fr_lmi_send(struct net_device *dev, int fullrep)
492 hdlc_device *hdlc = dev_to_hdlc(dev);
494 pvc_device *pvc = state(hdlc)->first_pvc;
495 int lmi = state(hdlc)->settings.lmi;
496 int dce = state(hdlc)->settings.dce;
497 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
498 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
502 if (dce && fullrep) {
503 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
504 if (len > HDLC_MAX_MRU) {
505 printk(KERN_WARNING "%s: Too many PVCs while sending "
506 "LMI full report\n", dev->name);
511 skb = dev_alloc_skb(len);
513 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
517 memset(skb->data, 0, len);
519 if (lmi == LMI_CISCO) {
520 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
521 fr_hard_header(&skb, LMI_CISCO_DLCI);
523 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
524 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
526 data = skb_tail_pointer(skb);
527 data[i++] = LMI_CALLREF;
528 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
530 data[i++] = LMI_ANSI_LOCKSHIFT;
531 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
532 LMI_ANSI_CISCO_REPTYPE;
533 data[i++] = LMI_REPT_LEN;
534 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
535 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
536 data[i++] = LMI_INTEG_LEN;
537 data[i++] = state(hdlc)->txseq =
538 fr_lmi_nextseq(state(hdlc)->txseq);
539 data[i++] = state(hdlc)->rxseq;
541 if (dce && fullrep) {
543 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
544 LMI_ANSI_CISCO_PVCSTAT;
545 data[i++] = stat_len;
547 /* LMI start/restart */
548 if (state(hdlc)->reliable && !pvc->state.exist) {
549 pvc->state.exist = pvc->state.new = 1;
550 fr_log_dlci_active(pvc);
553 /* ifconfig PVC up */
554 if (pvc->open_count && !pvc->state.active &&
555 pvc->state.exist && !pvc->state.new) {
557 pvc->state.active = 1;
558 fr_log_dlci_active(pvc);
561 if (lmi == LMI_CISCO) {
562 data[i] = pvc->dlci >> 8;
563 data[i + 1] = pvc->dlci & 0xFF;
565 data[i] = (pvc->dlci >> 4) & 0x3F;
566 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
572 else if (pvc->state.active)
581 skb->priority = TC_PRIO_CONTROL;
583 skb_reset_network_header(skb);
590 static void fr_set_link_state(int reliable, struct net_device *dev)
592 hdlc_device *hdlc = dev_to_hdlc(dev);
593 pvc_device *pvc = state(hdlc)->first_pvc;
595 state(hdlc)->reliable = reliable;
597 netif_dormant_off(dev);
598 state(hdlc)->n391cnt = 0; /* Request full status */
599 state(hdlc)->dce_changed = 1;
601 if (state(hdlc)->settings.lmi == LMI_NONE) {
602 while (pvc) { /* Activate all PVCs */
604 pvc->state.exist = pvc->state.active = 1;
610 netif_dormant_on(dev);
611 while (pvc) { /* Deactivate all PVCs */
613 pvc->state.exist = pvc->state.active = 0;
615 if (!state(hdlc)->settings.dce)
616 pvc->state.bandwidth = 0;
623 static void fr_timer(unsigned long arg)
625 struct net_device *dev = (struct net_device *)arg;
626 hdlc_device *hdlc = dev_to_hdlc(dev);
627 int i, cnt = 0, reliable;
630 if (state(hdlc)->settings.dce) {
631 reliable = state(hdlc)->request &&
632 time_before(jiffies, state(hdlc)->last_poll +
633 state(hdlc)->settings.t392 * HZ);
634 state(hdlc)->request = 0;
636 state(hdlc)->last_errors <<= 1; /* Shift the list */
637 if (state(hdlc)->request) {
638 if (state(hdlc)->reliable)
639 printk(KERN_INFO "%s: No LMI status reply "
640 "received\n", dev->name);
641 state(hdlc)->last_errors |= 1;
644 list = state(hdlc)->last_errors;
645 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
646 cnt += (list & 1); /* errors count */
648 reliable = (cnt < state(hdlc)->settings.n392);
651 if (state(hdlc)->reliable != reliable) {
652 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
653 reliable ? "" : "un");
654 fr_set_link_state(reliable, dev);
657 if (state(hdlc)->settings.dce)
658 state(hdlc)->timer.expires = jiffies +
659 state(hdlc)->settings.t392 * HZ;
661 if (state(hdlc)->n391cnt)
662 state(hdlc)->n391cnt--;
664 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
666 state(hdlc)->last_poll = jiffies;
667 state(hdlc)->request = 1;
668 state(hdlc)->timer.expires = jiffies +
669 state(hdlc)->settings.t391 * HZ;
672 state(hdlc)->timer.function = fr_timer;
673 state(hdlc)->timer.data = arg;
674 add_timer(&state(hdlc)->timer);
678 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
680 hdlc_device *hdlc = dev_to_hdlc(dev);
683 int lmi = state(hdlc)->settings.lmi;
684 int dce = state(hdlc)->settings.dce;
685 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
687 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
688 LMI_CCITT_CISCO_LENGTH)) {
689 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
693 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
694 NLPID_CCITT_ANSI_LMI)) {
695 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
700 if (skb->data[4] != LMI_CALLREF) {
701 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
702 dev->name, skb->data[4]);
706 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
707 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
708 dev->name, skb->data[5]);
712 if (lmi == LMI_ANSI) {
713 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
714 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
715 " message (0x%02X)\n", dev->name, skb->data[6]);
722 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
723 LMI_ANSI_CISCO_REPTYPE)) {
724 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
725 dev->name, skb->data[i]);
729 if (skb->data[++i] != LMI_REPT_LEN) {
730 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
731 " (%u)\n", dev->name, skb->data[i]);
735 reptype = skb->data[++i];
736 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
737 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
742 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
743 LMI_ANSI_CISCO_ALIVE)) {
744 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
745 " IE (0x%02X)\n", dev->name, skb->data[i]);
749 if (skb->data[++i] != LMI_INTEG_LEN) {
750 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
751 " IE length (%u)\n", dev->name, skb->data[i]);
756 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
757 rxseq = skb->data[i++]; /* Should confirm our sequence */
759 txseq = state(hdlc)->txseq;
762 state(hdlc)->last_poll = jiffies;
765 if (!state(hdlc)->reliable)
768 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
769 state(hdlc)->n391cnt = 0;
774 if (state(hdlc)->fullrep_sent && !error) {
775 /* Stop sending full report - the last one has been confirmed by DTE */
776 state(hdlc)->fullrep_sent = 0;
777 pvc = state(hdlc)->first_pvc;
779 if (pvc->state.new) {
782 /* Tell DTE that new PVC is now active */
783 state(hdlc)->dce_changed = 1;
789 if (state(hdlc)->dce_changed) {
790 reptype = LMI_FULLREP;
791 state(hdlc)->fullrep_sent = 1;
792 state(hdlc)->dce_changed = 0;
795 state(hdlc)->request = 1; /* got request */
796 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
802 state(hdlc)->request = 0; /* got response, no request pending */
807 if (reptype != LMI_FULLREP)
810 pvc = state(hdlc)->first_pvc;
813 pvc->state.deleted = 1;
818 while (skb->len >= i + 2 + stat_len) {
821 unsigned int active, new;
823 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
824 LMI_ANSI_CISCO_PVCSTAT)) {
825 printk(KERN_INFO "%s: Not an LMI PVC status IE"
826 " (0x%02X)\n", dev->name, skb->data[i]);
830 if (skb->data[++i] != stat_len) {
831 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
832 " (%u)\n", dev->name, skb->data[i]);
837 new = !! (skb->data[i + 2] & 0x08);
838 active = !! (skb->data[i + 2] & 0x02);
839 if (lmi == LMI_CISCO) {
840 dlci = (skb->data[i] << 8) | skb->data[i + 1];
841 bw = (skb->data[i + 3] << 16) |
842 (skb->data[i + 4] << 8) |
845 dlci = ((skb->data[i] & 0x3F) << 4) |
846 ((skb->data[i + 1] & 0x78) >> 3);
850 pvc = add_pvc(dev, dlci);
852 if (!pvc && !no_ram) {
854 "%s: Memory squeeze on fr_lmi_recv()\n",
860 pvc->state.exist = 1;
861 pvc->state.deleted = 0;
862 if (active != pvc->state.active ||
863 new != pvc->state.new ||
864 bw != pvc->state.bandwidth ||
866 pvc->state.new = new;
867 pvc->state.active = active;
868 pvc->state.bandwidth = bw;
869 pvc_carrier(active, pvc);
870 fr_log_dlci_active(pvc);
877 pvc = state(hdlc)->first_pvc;
880 if (pvc->state.deleted && pvc->state.exist) {
882 pvc->state.active = pvc->state.new = 0;
883 pvc->state.exist = 0;
884 pvc->state.bandwidth = 0;
885 fr_log_dlci_active(pvc);
890 /* Next full report after N391 polls */
891 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
897 static int fr_rx(struct sk_buff *skb)
899 struct net_device *frad = skb->dev;
900 hdlc_device *hdlc = dev_to_hdlc(frad);
901 fr_hdr *fh = (fr_hdr*)skb->data;
902 u8 *data = skb->data;
905 struct net_device *dev = NULL;
907 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
910 dlci = q922_to_dlci(skb->data);
912 if ((dlci == LMI_CCITT_ANSI_DLCI &&
913 (state(hdlc)->settings.lmi == LMI_ANSI ||
914 state(hdlc)->settings.lmi == LMI_CCITT)) ||
915 (dlci == LMI_CISCO_DLCI &&
916 state(hdlc)->settings.lmi == LMI_CISCO)) {
917 if (fr_lmi_recv(frad, skb))
919 dev_kfree_skb_any(skb);
920 return NET_RX_SUCCESS;
923 pvc = find_pvc(hdlc, dlci);
926 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
929 dev_kfree_skb_any(skb);
933 if (pvc->state.fecn != fh->fecn) {
935 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
936 dlci, fh->fecn ? "N" : "FF");
938 pvc->state.fecn ^= 1;
941 if (pvc->state.becn != fh->becn) {
943 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
944 dlci, fh->becn ? "N" : "FF");
946 pvc->state.becn ^= 1;
950 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
951 frad->stats.rx_dropped++;
955 if (data[3] == NLPID_IP) {
956 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
958 skb->protocol = htons(ETH_P_IP);
960 } else if (data[3] == NLPID_IPV6) {
961 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
963 skb->protocol = htons(ETH_P_IPV6);
965 } else if (skb->len > 10 && data[3] == FR_PAD &&
966 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
967 u16 oui = ntohs(*(__be16*)(data + 6));
968 u16 pid = ntohs(*(__be16*)(data + 8));
971 switch ((((u32)oui) << 16) | pid) {
972 case ETH_P_ARP: /* routed frame with SNAP */
974 case ETH_P_IP: /* a long variant */
977 skb->protocol = htons(pid);
980 case 0x80C20007: /* bridged Ethernet frame */
981 if ((dev = pvc->ether) != NULL)
982 skb->protocol = eth_type_trans(skb, dev);
986 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
987 "PID=%x\n", frad->name, oui, pid);
988 dev_kfree_skb_any(skb);
992 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
993 "length = %i\n", frad->name, data[3], skb->len);
994 dev_kfree_skb_any(skb);
999 dev->stats.rx_packets++; /* PVC traffic */
1000 dev->stats.rx_bytes += skb->len;
1001 if (pvc->state.becn)
1002 dev->stats.rx_compressed++;
1004 return NET_RX_SUCCESS;
1006 dev_kfree_skb_any(skb);
1011 frad->stats.rx_errors++; /* Mark error */
1012 dev_kfree_skb_any(skb);
1018 static void fr_start(struct net_device *dev)
1020 hdlc_device *hdlc = dev_to_hdlc(dev);
1022 printk(KERN_DEBUG "fr_start\n");
1024 if (state(hdlc)->settings.lmi != LMI_NONE) {
1025 state(hdlc)->reliable = 0;
1026 state(hdlc)->dce_changed = 1;
1027 state(hdlc)->request = 0;
1028 state(hdlc)->fullrep_sent = 0;
1029 state(hdlc)->last_errors = 0xFFFFFFFF;
1030 state(hdlc)->n391cnt = 0;
1031 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1033 init_timer(&state(hdlc)->timer);
1034 /* First poll after 1 s */
1035 state(hdlc)->timer.expires = jiffies + HZ;
1036 state(hdlc)->timer.function = fr_timer;
1037 state(hdlc)->timer.data = (unsigned long)dev;
1038 add_timer(&state(hdlc)->timer);
1040 fr_set_link_state(1, dev);
1044 static void fr_stop(struct net_device *dev)
1046 hdlc_device *hdlc = dev_to_hdlc(dev);
1048 printk(KERN_DEBUG "fr_stop\n");
1050 if (state(hdlc)->settings.lmi != LMI_NONE)
1051 del_timer_sync(&state(hdlc)->timer);
1052 fr_set_link_state(0, dev);
1056 static void fr_close(struct net_device *dev)
1058 hdlc_device *hdlc = dev_to_hdlc(dev);
1059 pvc_device *pvc = state(hdlc)->first_pvc;
1061 while (pvc) { /* Shutdown all PVCs for this FRAD */
1063 dev_close(pvc->main);
1065 dev_close(pvc->ether);
1071 static void pvc_setup(struct net_device *dev)
1073 dev->type = ARPHRD_DLCI;
1074 dev->flags = IFF_POINTOPOINT;
1075 dev->hard_header_len = 10;
1079 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1081 hdlc_device *hdlc = dev_to_hdlc(frad);
1082 pvc_device *pvc = NULL;
1083 struct net_device *dev;
1086 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1087 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1092 if (*get_dev_p(pvc, type))
1095 used = pvc_is_used(pvc);
1097 if (type == ARPHRD_ETHER)
1098 dev = alloc_netdev(sizeof(struct pvc_desc), "pvceth%d",
1101 dev = alloc_netdev(sizeof(struct pvc_desc), "pvc%d", pvc_setup);
1104 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1106 delete_unused_pvcs(hdlc);
1110 if (type == ARPHRD_ETHER)
1111 random_ether_addr(dev->dev_addr);
1113 *(__be16*)dev->dev_addr = htons(dlci);
1114 dlci_to_q922(dev->broadcast, dlci);
1116 dev->hard_start_xmit = pvc_xmit;
1117 dev->open = pvc_open;
1118 dev->stop = pvc_close;
1119 dev->do_ioctl = pvc_ioctl;
1120 dev->change_mtu = pvc_change_mtu;
1121 dev->mtu = HDLC_MAX_MTU;
1122 dev->tx_queue_len = 0;
1123 pvcdev_to_desc(dev)->pvc = pvc;
1125 result = dev_alloc_name(dev, dev->name);
1128 delete_unused_pvcs(hdlc);
1132 if (register_netdevice(dev) != 0) {
1134 delete_unused_pvcs(hdlc);
1138 dev->destructor = free_netdev;
1139 *get_dev_p(pvc, type) = dev;
1141 state(hdlc)->dce_changed = 1;
1142 state(hdlc)->dce_pvc_count++;
1149 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1152 struct net_device *dev;
1154 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1157 if ((dev = *get_dev_p(pvc, type)) == NULL)
1160 if (dev->flags & IFF_UP)
1161 return -EBUSY; /* PVC in use */
1163 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1164 *get_dev_p(pvc, type) = NULL;
1166 if (!pvc_is_used(pvc)) {
1167 state(hdlc)->dce_pvc_count--;
1168 state(hdlc)->dce_changed = 1;
1170 delete_unused_pvcs(hdlc);
1176 static void fr_destroy(struct net_device *frad)
1178 hdlc_device *hdlc = dev_to_hdlc(frad);
1179 pvc_device *pvc = state(hdlc)->first_pvc;
1180 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1181 state(hdlc)->dce_pvc_count = 0;
1182 state(hdlc)->dce_changed = 1;
1185 pvc_device *next = pvc->next;
1186 /* destructors will free_netdev() main and ether */
1188 unregister_netdevice(pvc->main);
1191 unregister_netdevice(pvc->ether);
1199 static struct hdlc_proto proto = {
1203 .detach = fr_destroy,
1206 .module = THIS_MODULE,
1210 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1212 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1213 const size_t size = sizeof(fr_proto);
1214 fr_proto new_settings;
1215 hdlc_device *hdlc = dev_to_hdlc(dev);
1219 switch (ifr->ifr_settings.type) {
1221 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1223 ifr->ifr_settings.type = IF_PROTO_FR;
1224 if (ifr->ifr_settings.size < size) {
1225 ifr->ifr_settings.size = size; /* data size wanted */
1228 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1233 if(!capable(CAP_NET_ADMIN))
1236 if(dev->flags & IFF_UP)
1239 if (copy_from_user(&new_settings, fr_s, size))
1242 if (new_settings.lmi == LMI_DEFAULT)
1243 new_settings.lmi = LMI_ANSI;
1245 if ((new_settings.lmi != LMI_NONE &&
1246 new_settings.lmi != LMI_ANSI &&
1247 new_settings.lmi != LMI_CCITT &&
1248 new_settings.lmi != LMI_CISCO) ||
1249 new_settings.t391 < 1 ||
1250 new_settings.t392 < 2 ||
1251 new_settings.n391 < 1 ||
1252 new_settings.n392 < 1 ||
1253 new_settings.n393 < new_settings.n392 ||
1254 new_settings.n393 > 32 ||
1255 (new_settings.dce != 0 &&
1256 new_settings.dce != 1))
1259 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1263 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1264 result = attach_hdlc_protocol(dev, &proto,
1265 sizeof(struct frad_state));
1268 state(hdlc)->first_pvc = NULL;
1269 state(hdlc)->dce_pvc_count = 0;
1271 memcpy(&state(hdlc)->settings, &new_settings, size);
1273 dev->hard_start_xmit = hdlc->xmit;
1274 dev->type = ARPHRD_FRAD;
1277 case IF_PROTO_FR_ADD_PVC:
1278 case IF_PROTO_FR_DEL_PVC:
1279 case IF_PROTO_FR_ADD_ETH_PVC:
1280 case IF_PROTO_FR_DEL_ETH_PVC:
1281 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1284 if(!capable(CAP_NET_ADMIN))
1287 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1288 sizeof(fr_proto_pvc)))
1291 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1292 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1294 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1295 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1296 result = ARPHRD_ETHER; /* bridged Ethernet device */
1298 result = ARPHRD_DLCI;
1300 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1301 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1302 return fr_add_pvc(dev, pvc.dlci, result);
1304 return fr_del_pvc(hdlc, pvc.dlci, result);
1311 static int __init mod_init(void)
1313 register_hdlc_protocol(&proto);
1318 static void __exit mod_exit(void)
1320 unregister_hdlc_protocol(&proto);
1324 module_init(mod_init);
1325 module_exit(mod_exit);
1327 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1328 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1329 MODULE_LICENSE("GPL v2");