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 */
139 struct net_device_stats stats;
145 pvc_device *first_pvc;
148 struct timer_list timer;
149 unsigned long last_poll;
154 u32 last_errors; /* last errors bit list */
156 u8 txseq; /* TX sequence number */
157 u8 rxseq; /* RX sequence number */
161 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
164 static inline u16 q922_to_dlci(u8 *hdr)
166 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
170 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
172 hdr[0] = (dlci >> 2) & 0xFC;
173 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
177 static inline struct frad_state* state(hdlc_device *hdlc)
179 return(struct frad_state *)(hdlc->state);
182 static inline struct pvc_desc* pvcdev_to_desc(struct net_device *dev)
187 static inline struct net_device_stats* pvc_get_stats(struct net_device *dev)
189 return &pvcdev_to_desc(dev)->stats;
192 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
194 pvc_device *pvc = state(hdlc)->first_pvc;
197 if (pvc->dlci == dlci)
199 if (pvc->dlci > dlci)
200 return NULL; /* the listed is sorted */
208 static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
210 hdlc_device *hdlc = dev_to_hdlc(dev);
211 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
214 if ((*pvc_p)->dlci == dlci)
216 if ((*pvc_p)->dlci > dlci)
217 break; /* the list is sorted */
218 pvc_p = &(*pvc_p)->next;
221 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
223 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
230 pvc->next = *pvc_p; /* Put it in the chain */
236 static inline int pvc_is_used(pvc_device *pvc)
238 return pvc->main || pvc->ether;
242 static inline void pvc_carrier(int on, pvc_device *pvc)
246 if (!netif_carrier_ok(pvc->main))
247 netif_carrier_on(pvc->main);
249 if (!netif_carrier_ok(pvc->ether))
250 netif_carrier_on(pvc->ether);
253 if (netif_carrier_ok(pvc->main))
254 netif_carrier_off(pvc->main);
256 if (netif_carrier_ok(pvc->ether))
257 netif_carrier_off(pvc->ether);
262 static inline void delete_unused_pvcs(hdlc_device *hdlc)
264 pvc_device **pvc_p = &state(hdlc)->first_pvc;
267 if (!pvc_is_used(*pvc_p)) {
268 pvc_device *pvc = *pvc_p;
270 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
276 pvc_p = &(*pvc_p)->next;
281 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
283 if (type == ARPHRD_ETHER)
290 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
293 struct sk_buff *skb = *skb_p;
295 switch (skb->protocol) {
296 case __constant_htons(NLPID_CCITT_ANSI_LMI):
298 skb_push(skb, head_len);
299 skb->data[3] = NLPID_CCITT_ANSI_LMI;
302 case __constant_htons(NLPID_CISCO_LMI):
304 skb_push(skb, head_len);
305 skb->data[3] = NLPID_CISCO_LMI;
308 case __constant_htons(ETH_P_IP):
310 skb_push(skb, head_len);
311 skb->data[3] = NLPID_IP;
314 case __constant_htons(ETH_P_IPV6):
316 skb_push(skb, head_len);
317 skb->data[3] = NLPID_IPV6;
320 case __constant_htons(ETH_P_802_3):
322 if (skb_headroom(skb) < head_len) {
323 struct sk_buff *skb2 = skb_realloc_headroom(skb,
330 skb_push(skb, head_len);
331 skb->data[3] = FR_PAD;
332 skb->data[4] = NLPID_SNAP;
333 skb->data[5] = FR_PAD;
337 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
342 skb_push(skb, head_len);
343 skb->data[3] = FR_PAD;
344 skb->data[4] = NLPID_SNAP;
345 skb->data[5] = FR_PAD;
346 skb->data[6] = FR_PAD;
347 skb->data[7] = FR_PAD;
348 *(__be16*)(skb->data + 8) = skb->protocol;
351 dlci_to_q922(skb->data, dlci);
352 skb->data[2] = FR_UI;
358 static int pvc_open(struct net_device *dev)
360 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
362 if ((pvc->frad->flags & IFF_UP) == 0)
363 return -EIO; /* Frad must be UP in order to activate PVC */
365 if (pvc->open_count++ == 0) {
366 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
367 if (state(hdlc)->settings.lmi == LMI_NONE)
368 pvc->state.active = netif_carrier_ok(pvc->frad);
370 pvc_carrier(pvc->state.active, pvc);
371 state(hdlc)->dce_changed = 1;
378 static int pvc_close(struct net_device *dev)
380 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
382 if (--pvc->open_count == 0) {
383 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
384 if (state(hdlc)->settings.lmi == LMI_NONE)
385 pvc->state.active = 0;
387 if (state(hdlc)->settings.dce) {
388 state(hdlc)->dce_changed = 1;
389 pvc->state.active = 0;
397 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
399 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
400 fr_proto_pvc_info info;
402 if (ifr->ifr_settings.type == IF_GET_PROTO) {
403 if (dev->type == ARPHRD_ETHER)
404 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
406 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
408 if (ifr->ifr_settings.size < sizeof(info)) {
409 /* data size wanted */
410 ifr->ifr_settings.size = sizeof(info);
414 info.dlci = pvc->dlci;
415 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
416 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
417 &info, sizeof(info)))
425 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
427 pvc_device *pvc = pvcdev_to_desc(dev)->pvc;
428 struct net_device_stats *stats = pvc_get_stats(dev);
430 if (pvc->state.active) {
431 if (dev->type == ARPHRD_ETHER) {
432 int pad = ETH_ZLEN - skb->len;
433 if (pad > 0) { /* Pad the frame with zeros */
435 if (skb_tailroom(skb) < pad)
436 if (pskb_expand_head(skb, 0, pad,
443 memset(skb->data + len, 0, pad);
445 skb->protocol = __constant_htons(ETH_P_802_3);
447 if (!fr_hard_header(&skb, pvc->dlci)) {
448 stats->tx_bytes += skb->len;
450 if (pvc->state.fecn) /* TX Congestion counter */
451 stats->tx_compressed++;
452 skb->dev = pvc->frad;
465 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
467 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
475 static inline void fr_log_dlci_active(pvc_device *pvc)
477 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
480 pvc->main ? pvc->main->name : "",
481 pvc->main && pvc->ether ? " " : "",
482 pvc->ether ? pvc->ether->name : "",
483 pvc->state.new ? " new" : "",
484 !pvc->state.exist ? "deleted" :
485 pvc->state.active ? "active" : "inactive");
490 static inline u8 fr_lmi_nextseq(u8 x)
497 static void fr_lmi_send(struct net_device *dev, int fullrep)
499 hdlc_device *hdlc = dev_to_hdlc(dev);
501 pvc_device *pvc = state(hdlc)->first_pvc;
502 int lmi = state(hdlc)->settings.lmi;
503 int dce = state(hdlc)->settings.dce;
504 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
505 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
509 if (dce && fullrep) {
510 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
511 if (len > HDLC_MAX_MRU) {
512 printk(KERN_WARNING "%s: Too many PVCs while sending "
513 "LMI full report\n", dev->name);
518 skb = dev_alloc_skb(len);
520 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
524 memset(skb->data, 0, len);
526 if (lmi == LMI_CISCO) {
527 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
528 fr_hard_header(&skb, LMI_CISCO_DLCI);
530 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
531 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
533 data = skb_tail_pointer(skb);
534 data[i++] = LMI_CALLREF;
535 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
537 data[i++] = LMI_ANSI_LOCKSHIFT;
538 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
539 LMI_ANSI_CISCO_REPTYPE;
540 data[i++] = LMI_REPT_LEN;
541 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
542 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
543 data[i++] = LMI_INTEG_LEN;
544 data[i++] = state(hdlc)->txseq =
545 fr_lmi_nextseq(state(hdlc)->txseq);
546 data[i++] = state(hdlc)->rxseq;
548 if (dce && fullrep) {
550 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
551 LMI_ANSI_CISCO_PVCSTAT;
552 data[i++] = stat_len;
554 /* LMI start/restart */
555 if (state(hdlc)->reliable && !pvc->state.exist) {
556 pvc->state.exist = pvc->state.new = 1;
557 fr_log_dlci_active(pvc);
560 /* ifconfig PVC up */
561 if (pvc->open_count && !pvc->state.active &&
562 pvc->state.exist && !pvc->state.new) {
564 pvc->state.active = 1;
565 fr_log_dlci_active(pvc);
568 if (lmi == LMI_CISCO) {
569 data[i] = pvc->dlci >> 8;
570 data[i + 1] = pvc->dlci & 0xFF;
572 data[i] = (pvc->dlci >> 4) & 0x3F;
573 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
579 else if (pvc->state.active)
588 skb->priority = TC_PRIO_CONTROL;
590 skb_reset_network_header(skb);
597 static void fr_set_link_state(int reliable, struct net_device *dev)
599 hdlc_device *hdlc = dev_to_hdlc(dev);
600 pvc_device *pvc = state(hdlc)->first_pvc;
602 state(hdlc)->reliable = reliable;
604 netif_dormant_off(dev);
605 state(hdlc)->n391cnt = 0; /* Request full status */
606 state(hdlc)->dce_changed = 1;
608 if (state(hdlc)->settings.lmi == LMI_NONE) {
609 while (pvc) { /* Activate all PVCs */
611 pvc->state.exist = pvc->state.active = 1;
617 netif_dormant_on(dev);
618 while (pvc) { /* Deactivate all PVCs */
620 pvc->state.exist = pvc->state.active = 0;
622 if (!state(hdlc)->settings.dce)
623 pvc->state.bandwidth = 0;
630 static void fr_timer(unsigned long arg)
632 struct net_device *dev = (struct net_device *)arg;
633 hdlc_device *hdlc = dev_to_hdlc(dev);
634 int i, cnt = 0, reliable;
637 if (state(hdlc)->settings.dce) {
638 reliable = state(hdlc)->request &&
639 time_before(jiffies, state(hdlc)->last_poll +
640 state(hdlc)->settings.t392 * HZ);
641 state(hdlc)->request = 0;
643 state(hdlc)->last_errors <<= 1; /* Shift the list */
644 if (state(hdlc)->request) {
645 if (state(hdlc)->reliable)
646 printk(KERN_INFO "%s: No LMI status reply "
647 "received\n", dev->name);
648 state(hdlc)->last_errors |= 1;
651 list = state(hdlc)->last_errors;
652 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
653 cnt += (list & 1); /* errors count */
655 reliable = (cnt < state(hdlc)->settings.n392);
658 if (state(hdlc)->reliable != reliable) {
659 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
660 reliable ? "" : "un");
661 fr_set_link_state(reliable, dev);
664 if (state(hdlc)->settings.dce)
665 state(hdlc)->timer.expires = jiffies +
666 state(hdlc)->settings.t392 * HZ;
668 if (state(hdlc)->n391cnt)
669 state(hdlc)->n391cnt--;
671 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
673 state(hdlc)->last_poll = jiffies;
674 state(hdlc)->request = 1;
675 state(hdlc)->timer.expires = jiffies +
676 state(hdlc)->settings.t391 * HZ;
679 state(hdlc)->timer.function = fr_timer;
680 state(hdlc)->timer.data = arg;
681 add_timer(&state(hdlc)->timer);
685 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
687 hdlc_device *hdlc = dev_to_hdlc(dev);
690 int lmi = state(hdlc)->settings.lmi;
691 int dce = state(hdlc)->settings.dce;
692 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
694 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
695 LMI_CCITT_CISCO_LENGTH)) {
696 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
700 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
701 NLPID_CCITT_ANSI_LMI)) {
702 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
707 if (skb->data[4] != LMI_CALLREF) {
708 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
709 dev->name, skb->data[4]);
713 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
714 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
715 dev->name, skb->data[5]);
719 if (lmi == LMI_ANSI) {
720 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
721 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
722 " message (0x%02X)\n", dev->name, skb->data[6]);
729 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
730 LMI_ANSI_CISCO_REPTYPE)) {
731 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
732 dev->name, skb->data[i]);
736 if (skb->data[++i] != LMI_REPT_LEN) {
737 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
738 " (%u)\n", dev->name, skb->data[i]);
742 reptype = skb->data[++i];
743 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
744 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
749 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
750 LMI_ANSI_CISCO_ALIVE)) {
751 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
752 " IE (0x%02X)\n", dev->name, skb->data[i]);
756 if (skb->data[++i] != LMI_INTEG_LEN) {
757 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
758 " IE length (%u)\n", dev->name, skb->data[i]);
763 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
764 rxseq = skb->data[i++]; /* Should confirm our sequence */
766 txseq = state(hdlc)->txseq;
769 state(hdlc)->last_poll = jiffies;
772 if (!state(hdlc)->reliable)
775 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
776 state(hdlc)->n391cnt = 0;
781 if (state(hdlc)->fullrep_sent && !error) {
782 /* Stop sending full report - the last one has been confirmed by DTE */
783 state(hdlc)->fullrep_sent = 0;
784 pvc = state(hdlc)->first_pvc;
786 if (pvc->state.new) {
789 /* Tell DTE that new PVC is now active */
790 state(hdlc)->dce_changed = 1;
796 if (state(hdlc)->dce_changed) {
797 reptype = LMI_FULLREP;
798 state(hdlc)->fullrep_sent = 1;
799 state(hdlc)->dce_changed = 0;
802 state(hdlc)->request = 1; /* got request */
803 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
809 state(hdlc)->request = 0; /* got response, no request pending */
814 if (reptype != LMI_FULLREP)
817 pvc = state(hdlc)->first_pvc;
820 pvc->state.deleted = 1;
825 while (skb->len >= i + 2 + stat_len) {
828 unsigned int active, new;
830 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
831 LMI_ANSI_CISCO_PVCSTAT)) {
832 printk(KERN_INFO "%s: Not an LMI PVC status IE"
833 " (0x%02X)\n", dev->name, skb->data[i]);
837 if (skb->data[++i] != stat_len) {
838 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
839 " (%u)\n", dev->name, skb->data[i]);
844 new = !! (skb->data[i + 2] & 0x08);
845 active = !! (skb->data[i + 2] & 0x02);
846 if (lmi == LMI_CISCO) {
847 dlci = (skb->data[i] << 8) | skb->data[i + 1];
848 bw = (skb->data[i + 3] << 16) |
849 (skb->data[i + 4] << 8) |
852 dlci = ((skb->data[i] & 0x3F) << 4) |
853 ((skb->data[i + 1] & 0x78) >> 3);
857 pvc = add_pvc(dev, dlci);
859 if (!pvc && !no_ram) {
861 "%s: Memory squeeze on fr_lmi_recv()\n",
867 pvc->state.exist = 1;
868 pvc->state.deleted = 0;
869 if (active != pvc->state.active ||
870 new != pvc->state.new ||
871 bw != pvc->state.bandwidth ||
873 pvc->state.new = new;
874 pvc->state.active = active;
875 pvc->state.bandwidth = bw;
876 pvc_carrier(active, pvc);
877 fr_log_dlci_active(pvc);
884 pvc = state(hdlc)->first_pvc;
887 if (pvc->state.deleted && pvc->state.exist) {
889 pvc->state.active = pvc->state.new = 0;
890 pvc->state.exist = 0;
891 pvc->state.bandwidth = 0;
892 fr_log_dlci_active(pvc);
897 /* Next full report after N391 polls */
898 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
904 static int fr_rx(struct sk_buff *skb)
906 struct net_device *frad = skb->dev;
907 hdlc_device *hdlc = dev_to_hdlc(frad);
908 fr_hdr *fh = (fr_hdr*)skb->data;
909 u8 *data = skb->data;
912 struct net_device *dev = NULL;
914 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
917 dlci = q922_to_dlci(skb->data);
919 if ((dlci == LMI_CCITT_ANSI_DLCI &&
920 (state(hdlc)->settings.lmi == LMI_ANSI ||
921 state(hdlc)->settings.lmi == LMI_CCITT)) ||
922 (dlci == LMI_CISCO_DLCI &&
923 state(hdlc)->settings.lmi == LMI_CISCO)) {
924 if (fr_lmi_recv(frad, skb))
926 dev_kfree_skb_any(skb);
927 return NET_RX_SUCCESS;
930 pvc = find_pvc(hdlc, dlci);
933 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
936 dev_kfree_skb_any(skb);
940 if (pvc->state.fecn != fh->fecn) {
942 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
943 dlci, fh->fecn ? "N" : "FF");
945 pvc->state.fecn ^= 1;
948 if (pvc->state.becn != fh->becn) {
950 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
951 dlci, fh->becn ? "N" : "FF");
953 pvc->state.becn ^= 1;
957 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
958 dev_to_hdlc(frad)->stats.rx_dropped++;
962 if (data[3] == NLPID_IP) {
963 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
965 skb->protocol = htons(ETH_P_IP);
967 } else if (data[3] == NLPID_IPV6) {
968 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
970 skb->protocol = htons(ETH_P_IPV6);
972 } else if (skb->len > 10 && data[3] == FR_PAD &&
973 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
974 u16 oui = ntohs(*(__be16*)(data + 6));
975 u16 pid = ntohs(*(__be16*)(data + 8));
978 switch ((((u32)oui) << 16) | pid) {
979 case ETH_P_ARP: /* routed frame with SNAP */
981 case ETH_P_IP: /* a long variant */
984 skb->protocol = htons(pid);
987 case 0x80C20007: /* bridged Ethernet frame */
988 if ((dev = pvc->ether) != NULL)
989 skb->protocol = eth_type_trans(skb, dev);
993 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
994 "PID=%x\n", frad->name, oui, pid);
995 dev_kfree_skb_any(skb);
999 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
1000 "length = %i\n", frad->name, data[3], skb->len);
1001 dev_kfree_skb_any(skb);
1006 struct net_device_stats *stats = pvc_get_stats(dev);
1007 stats->rx_packets++; /* PVC traffic */
1008 stats->rx_bytes += skb->len;
1009 if (pvc->state.becn)
1010 stats->rx_compressed++;
1012 return NET_RX_SUCCESS;
1014 dev_kfree_skb_any(skb);
1019 dev_to_hdlc(frad)->stats.rx_errors++; /* Mark error */
1020 dev_kfree_skb_any(skb);
1026 static void fr_start(struct net_device *dev)
1028 hdlc_device *hdlc = dev_to_hdlc(dev);
1030 printk(KERN_DEBUG "fr_start\n");
1032 if (state(hdlc)->settings.lmi != LMI_NONE) {
1033 state(hdlc)->reliable = 0;
1034 state(hdlc)->dce_changed = 1;
1035 state(hdlc)->request = 0;
1036 state(hdlc)->fullrep_sent = 0;
1037 state(hdlc)->last_errors = 0xFFFFFFFF;
1038 state(hdlc)->n391cnt = 0;
1039 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1041 init_timer(&state(hdlc)->timer);
1042 /* First poll after 1 s */
1043 state(hdlc)->timer.expires = jiffies + HZ;
1044 state(hdlc)->timer.function = fr_timer;
1045 state(hdlc)->timer.data = (unsigned long)dev;
1046 add_timer(&state(hdlc)->timer);
1048 fr_set_link_state(1, dev);
1052 static void fr_stop(struct net_device *dev)
1054 hdlc_device *hdlc = dev_to_hdlc(dev);
1056 printk(KERN_DEBUG "fr_stop\n");
1058 if (state(hdlc)->settings.lmi != LMI_NONE)
1059 del_timer_sync(&state(hdlc)->timer);
1060 fr_set_link_state(0, dev);
1064 static void fr_close(struct net_device *dev)
1066 hdlc_device *hdlc = dev_to_hdlc(dev);
1067 pvc_device *pvc = state(hdlc)->first_pvc;
1069 while (pvc) { /* Shutdown all PVCs for this FRAD */
1071 dev_close(pvc->main);
1073 dev_close(pvc->ether);
1079 static void pvc_setup(struct net_device *dev)
1081 dev->type = ARPHRD_DLCI;
1082 dev->flags = IFF_POINTOPOINT;
1083 dev->hard_header_len = 10;
1087 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1089 hdlc_device *hdlc = dev_to_hdlc(frad);
1090 pvc_device *pvc = NULL;
1091 struct net_device *dev;
1093 char * prefix = "pvc%d";
1095 if (type == ARPHRD_ETHER)
1096 prefix = "pvceth%d";
1098 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1099 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1104 if (*get_dev_p(pvc, type))
1107 used = pvc_is_used(pvc);
1109 if (type == ARPHRD_ETHER)
1110 dev = alloc_netdev(sizeof(struct pvc_desc), "pvceth%d",
1113 dev = alloc_netdev(sizeof(struct pvc_desc), "pvc%d", pvc_setup);
1116 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1118 delete_unused_pvcs(hdlc);
1122 if (type == ARPHRD_ETHER)
1123 random_ether_addr(dev->dev_addr);
1125 *(__be16*)dev->dev_addr = htons(dlci);
1126 dlci_to_q922(dev->broadcast, dlci);
1128 dev->hard_start_xmit = pvc_xmit;
1129 dev->get_stats = pvc_get_stats;
1130 dev->open = pvc_open;
1131 dev->stop = pvc_close;
1132 dev->do_ioctl = pvc_ioctl;
1133 dev->change_mtu = pvc_change_mtu;
1134 dev->mtu = HDLC_MAX_MTU;
1135 dev->tx_queue_len = 0;
1136 pvcdev_to_desc(dev)->pvc = pvc;
1138 result = dev_alloc_name(dev, dev->name);
1141 delete_unused_pvcs(hdlc);
1145 if (register_netdevice(dev) != 0) {
1147 delete_unused_pvcs(hdlc);
1151 dev->destructor = free_netdev;
1152 *get_dev_p(pvc, type) = dev;
1154 state(hdlc)->dce_changed = 1;
1155 state(hdlc)->dce_pvc_count++;
1162 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1165 struct net_device *dev;
1167 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1170 if ((dev = *get_dev_p(pvc, type)) == NULL)
1173 if (dev->flags & IFF_UP)
1174 return -EBUSY; /* PVC in use */
1176 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1177 *get_dev_p(pvc, type) = NULL;
1179 if (!pvc_is_used(pvc)) {
1180 state(hdlc)->dce_pvc_count--;
1181 state(hdlc)->dce_changed = 1;
1183 delete_unused_pvcs(hdlc);
1189 static void fr_destroy(struct net_device *frad)
1191 hdlc_device *hdlc = dev_to_hdlc(frad);
1192 pvc_device *pvc = state(hdlc)->first_pvc;
1193 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1194 state(hdlc)->dce_pvc_count = 0;
1195 state(hdlc)->dce_changed = 1;
1198 pvc_device *next = pvc->next;
1199 /* destructors will free_netdev() main and ether */
1201 unregister_netdevice(pvc->main);
1204 unregister_netdevice(pvc->ether);
1212 static struct hdlc_proto proto = {
1216 .detach = fr_destroy,
1219 .module = THIS_MODULE,
1223 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1225 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1226 const size_t size = sizeof(fr_proto);
1227 fr_proto new_settings;
1228 hdlc_device *hdlc = dev_to_hdlc(dev);
1232 switch (ifr->ifr_settings.type) {
1234 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1236 ifr->ifr_settings.type = IF_PROTO_FR;
1237 if (ifr->ifr_settings.size < size) {
1238 ifr->ifr_settings.size = size; /* data size wanted */
1241 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1246 if(!capable(CAP_NET_ADMIN))
1249 if(dev->flags & IFF_UP)
1252 if (copy_from_user(&new_settings, fr_s, size))
1255 if (new_settings.lmi == LMI_DEFAULT)
1256 new_settings.lmi = LMI_ANSI;
1258 if ((new_settings.lmi != LMI_NONE &&
1259 new_settings.lmi != LMI_ANSI &&
1260 new_settings.lmi != LMI_CCITT &&
1261 new_settings.lmi != LMI_CISCO) ||
1262 new_settings.t391 < 1 ||
1263 new_settings.t392 < 2 ||
1264 new_settings.n391 < 1 ||
1265 new_settings.n392 < 1 ||
1266 new_settings.n393 < new_settings.n392 ||
1267 new_settings.n393 > 32 ||
1268 (new_settings.dce != 0 &&
1269 new_settings.dce != 1))
1272 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1276 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1277 result = attach_hdlc_protocol(dev, &proto,
1278 sizeof(struct frad_state));
1281 state(hdlc)->first_pvc = NULL;
1282 state(hdlc)->dce_pvc_count = 0;
1284 memcpy(&state(hdlc)->settings, &new_settings, size);
1286 dev->hard_start_xmit = hdlc->xmit;
1287 dev->type = ARPHRD_FRAD;
1290 case IF_PROTO_FR_ADD_PVC:
1291 case IF_PROTO_FR_DEL_PVC:
1292 case IF_PROTO_FR_ADD_ETH_PVC:
1293 case IF_PROTO_FR_DEL_ETH_PVC:
1294 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1297 if(!capable(CAP_NET_ADMIN))
1300 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1301 sizeof(fr_proto_pvc)))
1304 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1305 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1307 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1308 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1309 result = ARPHRD_ETHER; /* bridged Ethernet device */
1311 result = ARPHRD_DLCI;
1313 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1314 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1315 return fr_add_pvc(dev, pvc.dlci, result);
1317 return fr_del_pvc(hdlc, pvc.dlci, result);
1324 static int __init mod_init(void)
1326 register_hdlc_protocol(&proto);
1331 static void __exit mod_exit(void)
1333 unregister_hdlc_protocol(&proto);
1337 module_init(mod_init);
1338 module_exit(mod_exit);
1340 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1341 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1342 MODULE_LICENSE("GPL v2");