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/errno.h>
37 #include <linux/etherdevice.h>
38 #include <linux/hdlc.h>
39 #include <linux/if_arp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/init.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/pkt_sched.h>
45 #include <linux/poll.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/skbuff.h>
48 #include <linux/slab.h>
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
94 #if defined(__LITTLE_ENDIAN_BITFIELD)
119 struct net_device *frad;
120 struct net_device *main;
121 struct net_device *ether; /* bridged Ethernet interface */
122 struct pvc_device *next; /* Sorted in ascending DLCI order */
128 unsigned int active: 1;
129 unsigned int exist: 1;
130 unsigned int deleted: 1;
131 unsigned int fecn: 1;
132 unsigned int becn: 1;
133 unsigned int bandwidth; /* Cisco LMI reporting only */
139 struct pvc_device *first_pvc;
142 struct timer_list timer;
143 unsigned long last_poll;
148 u32 last_errors; /* last errors bit list */
150 u8 txseq; /* TX sequence number */
151 u8 rxseq; /* RX sequence number */
155 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
158 static inline u16 q922_to_dlci(u8 *hdr)
160 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
164 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
166 hdr[0] = (dlci >> 2) & 0xFC;
167 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
171 static inline struct frad_state* state(hdlc_device *hdlc)
173 return(struct frad_state *)(hdlc->state);
177 static inline struct pvc_device *find_pvc(hdlc_device *hdlc, u16 dlci)
179 struct pvc_device *pvc = state(hdlc)->first_pvc;
182 if (pvc->dlci == dlci)
184 if (pvc->dlci > dlci)
185 return NULL; /* the list is sorted */
193 static struct pvc_device *add_pvc(struct net_device *dev, u16 dlci)
195 hdlc_device *hdlc = dev_to_hdlc(dev);
196 struct pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
199 if ((*pvc_p)->dlci == dlci)
201 if ((*pvc_p)->dlci > dlci)
202 break; /* the list is sorted */
203 pvc_p = &(*pvc_p)->next;
206 pvc = kzalloc(sizeof(*pvc), GFP_ATOMIC);
208 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
215 pvc->next = *pvc_p; /* Put it in the chain */
221 static inline int pvc_is_used(struct pvc_device *pvc)
223 return pvc->main || pvc->ether;
227 static inline void pvc_carrier(int on, struct pvc_device *pvc)
231 if (!netif_carrier_ok(pvc->main))
232 netif_carrier_on(pvc->main);
234 if (!netif_carrier_ok(pvc->ether))
235 netif_carrier_on(pvc->ether);
238 if (netif_carrier_ok(pvc->main))
239 netif_carrier_off(pvc->main);
241 if (netif_carrier_ok(pvc->ether))
242 netif_carrier_off(pvc->ether);
247 static inline void delete_unused_pvcs(hdlc_device *hdlc)
249 struct pvc_device **pvc_p = &state(hdlc)->first_pvc;
252 if (!pvc_is_used(*pvc_p)) {
253 struct pvc_device *pvc = *pvc_p;
255 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
261 pvc_p = &(*pvc_p)->next;
266 static inline struct net_device **get_dev_p(struct pvc_device *pvc,
269 if (type == ARPHRD_ETHER)
276 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
278 struct sk_buff *skb = *skb_p;
280 if (!skb->dev) { /* Control packets */
282 case LMI_CCITT_ANSI_DLCI:
284 skb->data[3] = NLPID_CCITT_ANSI_LMI;
289 skb->data[3] = NLPID_CISCO_LMI;
296 } else if (skb->dev->type == ARPHRD_DLCI) {
297 switch (skb->protocol) {
298 case htons(ETH_P_IP):
300 skb->data[3] = NLPID_IP;
303 case htons(ETH_P_IPV6):
305 skb->data[3] = NLPID_IPV6;
310 skb->data[3] = FR_PAD;
311 skb->data[4] = NLPID_SNAP;
312 /* OUI 00-00-00 indicates an Ethertype follows */
316 /* This should be an Ethertype: */
317 *(__be16 *)(skb->data + 8) = skb->protocol;
320 } else if (skb->dev->type == ARPHRD_ETHER) {
321 if (skb_headroom(skb) < 10) {
322 struct sk_buff *skb2 = skb_realloc_headroom(skb, 10);
329 skb->data[3] = FR_PAD;
330 skb->data[4] = NLPID_SNAP;
331 /* OUI 00-80-C2 stands for the 802.1 organization */
335 /* PID 00-07 stands for Ethernet frames without FCS */
343 dlci_to_q922(skb->data, dlci);
344 skb->data[2] = FR_UI;
350 static int pvc_open(struct net_device *dev)
352 struct pvc_device *pvc = dev->ml_priv;
354 if ((pvc->frad->flags & IFF_UP) == 0)
355 return -EIO; /* Frad must be UP in order to activate PVC */
357 if (pvc->open_count++ == 0) {
358 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
359 if (state(hdlc)->settings.lmi == LMI_NONE)
360 pvc->state.active = netif_carrier_ok(pvc->frad);
362 pvc_carrier(pvc->state.active, pvc);
363 state(hdlc)->dce_changed = 1;
370 static int pvc_close(struct net_device *dev)
372 struct pvc_device *pvc = dev->ml_priv;
374 if (--pvc->open_count == 0) {
375 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
376 if (state(hdlc)->settings.lmi == LMI_NONE)
377 pvc->state.active = 0;
379 if (state(hdlc)->settings.dce) {
380 state(hdlc)->dce_changed = 1;
381 pvc->state.active = 0;
389 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
391 struct pvc_device *pvc = dev->ml_priv;
392 fr_proto_pvc_info info;
394 if (ifr->ifr_settings.type == IF_GET_PROTO) {
395 if (dev->type == ARPHRD_ETHER)
396 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
398 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
400 if (ifr->ifr_settings.size < sizeof(info)) {
401 /* data size wanted */
402 ifr->ifr_settings.size = sizeof(info);
406 info.dlci = pvc->dlci;
407 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
408 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
409 &info, sizeof(info)))
417 static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
419 struct pvc_device *pvc = dev->ml_priv;
421 if (pvc->state.active) {
422 if (dev->type == ARPHRD_ETHER) {
423 int pad = ETH_ZLEN - skb->len;
424 if (pad > 0) { /* Pad the frame with zeros */
426 if (skb_tailroom(skb) < pad)
427 if (pskb_expand_head(skb, 0, pad,
429 dev->stats.tx_dropped++;
434 memset(skb->data + len, 0, pad);
438 if (!fr_hard_header(&skb, pvc->dlci)) {
439 dev->stats.tx_bytes += skb->len;
440 dev->stats.tx_packets++;
441 if (pvc->state.fecn) /* TX Congestion counter */
442 dev->stats.tx_compressed++;
443 skb->dev = pvc->frad;
444 skb->protocol = htons(ETH_P_HDLC);
445 skb_reset_network_header(skb);
451 dev->stats.tx_dropped++;
456 static inline void fr_log_dlci_active(struct pvc_device *pvc)
458 netdev_info(pvc->frad, "DLCI %d [%s%s%s]%s %s\n",
460 pvc->main ? pvc->main->name : "",
461 pvc->main && pvc->ether ? " " : "",
462 pvc->ether ? pvc->ether->name : "",
463 pvc->state.new ? " new" : "",
464 !pvc->state.exist ? "deleted" :
465 pvc->state.active ? "active" : "inactive");
470 static inline u8 fr_lmi_nextseq(u8 x)
477 static void fr_lmi_send(struct net_device *dev, int fullrep)
479 hdlc_device *hdlc = dev_to_hdlc(dev);
481 struct pvc_device *pvc = state(hdlc)->first_pvc;
482 int lmi = state(hdlc)->settings.lmi;
483 int dce = state(hdlc)->settings.dce;
484 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
485 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
489 if (dce && fullrep) {
490 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
491 if (len > HDLC_MAX_MRU) {
492 netdev_warn(dev, "Too many PVCs while sending LMI full report\n");
497 skb = dev_alloc_skb(len);
499 netdev_warn(dev, "Memory squeeze on fr_lmi_send()\n");
502 memset(skb->data, 0, len);
504 if (lmi == LMI_CISCO) {
505 fr_hard_header(&skb, LMI_CISCO_DLCI);
507 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
509 data = skb_tail_pointer(skb);
510 data[i++] = LMI_CALLREF;
511 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
513 data[i++] = LMI_ANSI_LOCKSHIFT;
514 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
515 LMI_ANSI_CISCO_REPTYPE;
516 data[i++] = LMI_REPT_LEN;
517 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
518 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
519 data[i++] = LMI_INTEG_LEN;
520 data[i++] = state(hdlc)->txseq =
521 fr_lmi_nextseq(state(hdlc)->txseq);
522 data[i++] = state(hdlc)->rxseq;
524 if (dce && fullrep) {
526 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
527 LMI_ANSI_CISCO_PVCSTAT;
528 data[i++] = stat_len;
530 /* LMI start/restart */
531 if (state(hdlc)->reliable && !pvc->state.exist) {
532 pvc->state.exist = pvc->state.new = 1;
533 fr_log_dlci_active(pvc);
536 /* ifconfig PVC up */
537 if (pvc->open_count && !pvc->state.active &&
538 pvc->state.exist && !pvc->state.new) {
540 pvc->state.active = 1;
541 fr_log_dlci_active(pvc);
544 if (lmi == LMI_CISCO) {
545 data[i] = pvc->dlci >> 8;
546 data[i + 1] = pvc->dlci & 0xFF;
548 data[i] = (pvc->dlci >> 4) & 0x3F;
549 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
555 else if (pvc->state.active)
564 skb->priority = TC_PRIO_CONTROL;
566 skb->protocol = htons(ETH_P_HDLC);
567 skb_reset_network_header(skb);
574 static void fr_set_link_state(int reliable, struct net_device *dev)
576 hdlc_device *hdlc = dev_to_hdlc(dev);
577 struct pvc_device *pvc = state(hdlc)->first_pvc;
579 state(hdlc)->reliable = reliable;
581 netif_dormant_off(dev);
582 state(hdlc)->n391cnt = 0; /* Request full status */
583 state(hdlc)->dce_changed = 1;
585 if (state(hdlc)->settings.lmi == LMI_NONE) {
586 while (pvc) { /* Activate all PVCs */
588 pvc->state.exist = pvc->state.active = 1;
594 netif_dormant_on(dev);
595 while (pvc) { /* Deactivate all PVCs */
597 pvc->state.exist = pvc->state.active = 0;
599 if (!state(hdlc)->settings.dce)
600 pvc->state.bandwidth = 0;
607 static void fr_timer(unsigned long arg)
609 struct net_device *dev = (struct net_device *)arg;
610 hdlc_device *hdlc = dev_to_hdlc(dev);
611 int i, cnt = 0, reliable;
614 if (state(hdlc)->settings.dce) {
615 reliable = state(hdlc)->request &&
616 time_before(jiffies, state(hdlc)->last_poll +
617 state(hdlc)->settings.t392 * HZ);
618 state(hdlc)->request = 0;
620 state(hdlc)->last_errors <<= 1; /* Shift the list */
621 if (state(hdlc)->request) {
622 if (state(hdlc)->reliable)
623 netdev_info(dev, "No LMI status reply received\n");
624 state(hdlc)->last_errors |= 1;
627 list = state(hdlc)->last_errors;
628 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
629 cnt += (list & 1); /* errors count */
631 reliable = (cnt < state(hdlc)->settings.n392);
634 if (state(hdlc)->reliable != reliable) {
635 netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
636 fr_set_link_state(reliable, dev);
639 if (state(hdlc)->settings.dce)
640 state(hdlc)->timer.expires = jiffies +
641 state(hdlc)->settings.t392 * HZ;
643 if (state(hdlc)->n391cnt)
644 state(hdlc)->n391cnt--;
646 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
648 state(hdlc)->last_poll = jiffies;
649 state(hdlc)->request = 1;
650 state(hdlc)->timer.expires = jiffies +
651 state(hdlc)->settings.t391 * HZ;
654 state(hdlc)->timer.function = fr_timer;
655 state(hdlc)->timer.data = arg;
656 add_timer(&state(hdlc)->timer);
660 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
662 hdlc_device *hdlc = dev_to_hdlc(dev);
663 struct pvc_device *pvc;
665 int lmi = state(hdlc)->settings.lmi;
666 int dce = state(hdlc)->settings.dce;
667 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
669 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
670 LMI_CCITT_CISCO_LENGTH)) {
671 netdev_info(dev, "Short LMI frame\n");
675 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
676 NLPID_CCITT_ANSI_LMI)) {
677 netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
681 if (skb->data[4] != LMI_CALLREF) {
682 netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
687 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
688 netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
693 if (lmi == LMI_ANSI) {
694 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
695 netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
703 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
704 LMI_ANSI_CISCO_REPTYPE)) {
705 netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
710 if (skb->data[++i] != LMI_REPT_LEN) {
711 netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
716 reptype = skb->data[++i];
717 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
718 netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
723 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
724 LMI_ANSI_CISCO_ALIVE)) {
725 netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
730 if (skb->data[++i] != LMI_INTEG_LEN) {
731 netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
737 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
738 rxseq = skb->data[i++]; /* Should confirm our sequence */
740 txseq = state(hdlc)->txseq;
743 state(hdlc)->last_poll = jiffies;
746 if (!state(hdlc)->reliable)
749 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
750 state(hdlc)->n391cnt = 0;
755 if (state(hdlc)->fullrep_sent && !error) {
756 /* Stop sending full report - the last one has been confirmed by DTE */
757 state(hdlc)->fullrep_sent = 0;
758 pvc = state(hdlc)->first_pvc;
760 if (pvc->state.new) {
763 /* Tell DTE that new PVC is now active */
764 state(hdlc)->dce_changed = 1;
770 if (state(hdlc)->dce_changed) {
771 reptype = LMI_FULLREP;
772 state(hdlc)->fullrep_sent = 1;
773 state(hdlc)->dce_changed = 0;
776 state(hdlc)->request = 1; /* got request */
777 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
783 state(hdlc)->request = 0; /* got response, no request pending */
788 if (reptype != LMI_FULLREP)
791 pvc = state(hdlc)->first_pvc;
794 pvc->state.deleted = 1;
799 while (skb->len >= i + 2 + stat_len) {
802 unsigned int active, new;
804 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
805 LMI_ANSI_CISCO_PVCSTAT)) {
806 netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
811 if (skb->data[++i] != stat_len) {
812 netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
818 new = !! (skb->data[i + 2] & 0x08);
819 active = !! (skb->data[i + 2] & 0x02);
820 if (lmi == LMI_CISCO) {
821 dlci = (skb->data[i] << 8) | skb->data[i + 1];
822 bw = (skb->data[i + 3] << 16) |
823 (skb->data[i + 4] << 8) |
826 dlci = ((skb->data[i] & 0x3F) << 4) |
827 ((skb->data[i + 1] & 0x78) >> 3);
831 pvc = add_pvc(dev, dlci);
833 if (!pvc && !no_ram) {
834 netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
839 pvc->state.exist = 1;
840 pvc->state.deleted = 0;
841 if (active != pvc->state.active ||
842 new != pvc->state.new ||
843 bw != pvc->state.bandwidth ||
845 pvc->state.new = new;
846 pvc->state.active = active;
847 pvc->state.bandwidth = bw;
848 pvc_carrier(active, pvc);
849 fr_log_dlci_active(pvc);
856 pvc = state(hdlc)->first_pvc;
859 if (pvc->state.deleted && pvc->state.exist) {
861 pvc->state.active = pvc->state.new = 0;
862 pvc->state.exist = 0;
863 pvc->state.bandwidth = 0;
864 fr_log_dlci_active(pvc);
869 /* Next full report after N391 polls */
870 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
876 static int fr_rx(struct sk_buff *skb)
878 struct net_device *frad = skb->dev;
879 hdlc_device *hdlc = dev_to_hdlc(frad);
880 struct fr_hdr *fh = (struct fr_hdr *)skb->data;
881 u8 *data = skb->data;
883 struct pvc_device *pvc;
884 struct net_device *dev = NULL;
886 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
889 dlci = q922_to_dlci(skb->data);
891 if ((dlci == LMI_CCITT_ANSI_DLCI &&
892 (state(hdlc)->settings.lmi == LMI_ANSI ||
893 state(hdlc)->settings.lmi == LMI_CCITT)) ||
894 (dlci == LMI_CISCO_DLCI &&
895 state(hdlc)->settings.lmi == LMI_CISCO)) {
896 if (fr_lmi_recv(frad, skb))
898 dev_kfree_skb_any(skb);
899 return NET_RX_SUCCESS;
902 pvc = find_pvc(hdlc, dlci);
905 netdev_info(frad, "No PVC for received frame's DLCI %d\n",
908 dev_kfree_skb_any(skb);
912 if (pvc->state.fecn != fh->fecn) {
914 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
915 dlci, fh->fecn ? "N" : "FF");
917 pvc->state.fecn ^= 1;
920 if (pvc->state.becn != fh->becn) {
922 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
923 dlci, fh->becn ? "N" : "FF");
925 pvc->state.becn ^= 1;
929 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
930 frad->stats.rx_dropped++;
934 if (data[3] == NLPID_IP) {
935 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
937 skb->protocol = htons(ETH_P_IP);
939 } else if (data[3] == NLPID_IPV6) {
940 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
942 skb->protocol = htons(ETH_P_IPV6);
944 } else if (skb->len > 10 && data[3] == FR_PAD &&
945 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
946 u16 oui = ntohs(*(__be16*)(data + 6));
947 u16 pid = ntohs(*(__be16*)(data + 8));
950 switch ((((u32)oui) << 16) | pid) {
951 case ETH_P_ARP: /* routed frame with SNAP */
953 case ETH_P_IP: /* a long variant */
956 skb->protocol = htons(pid);
959 case 0x80C20007: /* bridged Ethernet frame */
960 if ((dev = pvc->ether) != NULL)
961 skb->protocol = eth_type_trans(skb, dev);
965 netdev_info(frad, "Unsupported protocol, OUI=%x PID=%x\n",
967 dev_kfree_skb_any(skb);
971 netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
973 dev_kfree_skb_any(skb);
978 dev->stats.rx_packets++; /* PVC traffic */
979 dev->stats.rx_bytes += skb->len;
981 dev->stats.rx_compressed++;
984 return NET_RX_SUCCESS;
986 dev_kfree_skb_any(skb);
991 frad->stats.rx_errors++; /* Mark error */
992 dev_kfree_skb_any(skb);
998 static void fr_start(struct net_device *dev)
1000 hdlc_device *hdlc = dev_to_hdlc(dev);
1002 printk(KERN_DEBUG "fr_start\n");
1004 if (state(hdlc)->settings.lmi != LMI_NONE) {
1005 state(hdlc)->reliable = 0;
1006 state(hdlc)->dce_changed = 1;
1007 state(hdlc)->request = 0;
1008 state(hdlc)->fullrep_sent = 0;
1009 state(hdlc)->last_errors = 0xFFFFFFFF;
1010 state(hdlc)->n391cnt = 0;
1011 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1013 init_timer(&state(hdlc)->timer);
1014 /* First poll after 1 s */
1015 state(hdlc)->timer.expires = jiffies + HZ;
1016 state(hdlc)->timer.function = fr_timer;
1017 state(hdlc)->timer.data = (unsigned long)dev;
1018 add_timer(&state(hdlc)->timer);
1020 fr_set_link_state(1, dev);
1024 static void fr_stop(struct net_device *dev)
1026 hdlc_device *hdlc = dev_to_hdlc(dev);
1028 printk(KERN_DEBUG "fr_stop\n");
1030 if (state(hdlc)->settings.lmi != LMI_NONE)
1031 del_timer_sync(&state(hdlc)->timer);
1032 fr_set_link_state(0, dev);
1036 static void fr_close(struct net_device *dev)
1038 hdlc_device *hdlc = dev_to_hdlc(dev);
1039 struct pvc_device *pvc = state(hdlc)->first_pvc;
1041 while (pvc) { /* Shutdown all PVCs for this FRAD */
1043 dev_close(pvc->main);
1045 dev_close(pvc->ether);
1051 static void pvc_setup(struct net_device *dev)
1053 dev->type = ARPHRD_DLCI;
1054 dev->flags = IFF_POINTOPOINT;
1055 dev->hard_header_len = 0;
1057 netif_keep_dst(dev);
1060 static const struct net_device_ops pvc_ops = {
1061 .ndo_open = pvc_open,
1062 .ndo_stop = pvc_close,
1063 .ndo_start_xmit = pvc_xmit,
1064 .ndo_do_ioctl = pvc_ioctl,
1067 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1069 hdlc_device *hdlc = dev_to_hdlc(frad);
1070 struct pvc_device *pvc;
1071 struct net_device *dev;
1074 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1075 netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
1079 if (*get_dev_p(pvc, type))
1082 used = pvc_is_used(pvc);
1084 if (type == ARPHRD_ETHER)
1085 dev = alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN,
1088 dev = alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN, pvc_setup);
1091 netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
1092 delete_unused_pvcs(hdlc);
1096 if (type == ARPHRD_ETHER) {
1097 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1098 eth_hw_addr_random(dev);
1100 *(__be16*)dev->dev_addr = htons(dlci);
1101 dlci_to_q922(dev->broadcast, dlci);
1103 dev->netdev_ops = &pvc_ops;
1104 dev->mtu = HDLC_MAX_MTU;
1106 dev->max_mtu = HDLC_MAX_MTU;
1107 dev->needed_headroom = 10;
1108 dev->priv_flags |= IFF_NO_QUEUE;
1111 if (register_netdevice(dev) != 0) {
1113 delete_unused_pvcs(hdlc);
1117 dev->needs_free_netdev = true;
1118 *get_dev_p(pvc, type) = dev;
1120 state(hdlc)->dce_changed = 1;
1121 state(hdlc)->dce_pvc_count++;
1128 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1130 struct pvc_device *pvc;
1131 struct net_device *dev;
1133 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1136 if ((dev = *get_dev_p(pvc, type)) == NULL)
1139 if (dev->flags & IFF_UP)
1140 return -EBUSY; /* PVC in use */
1142 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1143 *get_dev_p(pvc, type) = NULL;
1145 if (!pvc_is_used(pvc)) {
1146 state(hdlc)->dce_pvc_count--;
1147 state(hdlc)->dce_changed = 1;
1149 delete_unused_pvcs(hdlc);
1155 static void fr_destroy(struct net_device *frad)
1157 hdlc_device *hdlc = dev_to_hdlc(frad);
1158 struct pvc_device *pvc = state(hdlc)->first_pvc;
1159 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1160 state(hdlc)->dce_pvc_count = 0;
1161 state(hdlc)->dce_changed = 1;
1164 struct pvc_device *next = pvc->next;
1165 /* destructors will free_netdev() main and ether */
1167 unregister_netdevice(pvc->main);
1170 unregister_netdevice(pvc->ether);
1178 static struct hdlc_proto proto = {
1182 .detach = fr_destroy,
1185 .module = THIS_MODULE,
1189 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1191 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1192 const size_t size = sizeof(fr_proto);
1193 fr_proto new_settings;
1194 hdlc_device *hdlc = dev_to_hdlc(dev);
1198 switch (ifr->ifr_settings.type) {
1200 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1202 ifr->ifr_settings.type = IF_PROTO_FR;
1203 if (ifr->ifr_settings.size < size) {
1204 ifr->ifr_settings.size = size; /* data size wanted */
1207 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1212 if (!capable(CAP_NET_ADMIN))
1215 if (dev->flags & IFF_UP)
1218 if (copy_from_user(&new_settings, fr_s, size))
1221 if (new_settings.lmi == LMI_DEFAULT)
1222 new_settings.lmi = LMI_ANSI;
1224 if ((new_settings.lmi != LMI_NONE &&
1225 new_settings.lmi != LMI_ANSI &&
1226 new_settings.lmi != LMI_CCITT &&
1227 new_settings.lmi != LMI_CISCO) ||
1228 new_settings.t391 < 1 ||
1229 new_settings.t392 < 2 ||
1230 new_settings.n391 < 1 ||
1231 new_settings.n392 < 1 ||
1232 new_settings.n393 < new_settings.n392 ||
1233 new_settings.n393 > 32 ||
1234 (new_settings.dce != 0 &&
1235 new_settings.dce != 1))
1238 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1242 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1243 result = attach_hdlc_protocol(dev, &proto,
1244 sizeof(struct frad_state));
1247 state(hdlc)->first_pvc = NULL;
1248 state(hdlc)->dce_pvc_count = 0;
1250 memcpy(&state(hdlc)->settings, &new_settings, size);
1251 dev->type = ARPHRD_FRAD;
1252 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1255 case IF_PROTO_FR_ADD_PVC:
1256 case IF_PROTO_FR_DEL_PVC:
1257 case IF_PROTO_FR_ADD_ETH_PVC:
1258 case IF_PROTO_FR_DEL_ETH_PVC:
1259 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1262 if (!capable(CAP_NET_ADMIN))
1265 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1266 sizeof(fr_proto_pvc)))
1269 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1270 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1272 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1273 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1274 result = ARPHRD_ETHER; /* bridged Ethernet device */
1276 result = ARPHRD_DLCI;
1278 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1279 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1280 return fr_add_pvc(dev, pvc.dlci, result);
1282 return fr_del_pvc(hdlc, pvc.dlci, result);
1289 static int __init mod_init(void)
1291 register_hdlc_protocol(&proto);
1296 static void __exit mod_exit(void)
1298 unregister_hdlc_protocol(&proto);
1302 module_init(mod_init);
1303 module_exit(mod_exit);
1305 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1306 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1307 MODULE_LICENSE("GPL v2");