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)
118 typedef struct pvc_device_struct {
119 struct net_device *frad;
120 struct net_device *main;
121 struct net_device *ether; /* bridged Ethernet interface */
122 struct pvc_device_struct *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 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 pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
179 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 pvc_device* add_pvc(struct net_device *dev, u16 dlci)
195 hdlc_device *hdlc = dev_to_hdlc(dev);
196 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_device), 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(pvc_device *pvc)
223 return pvc->main || pvc->ether;
227 static inline void pvc_carrier(int on, 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 pvc_device **pvc_p = &state(hdlc)->first_pvc;
252 if (!pvc_is_used(*pvc_p)) {
253 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(pvc_device *pvc, int type)
268 if (type == ARPHRD_ETHER)
275 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
278 struct sk_buff *skb = *skb_p;
280 switch (skb->protocol) {
281 case cpu_to_be16(NLPID_CCITT_ANSI_LMI):
283 skb_push(skb, head_len);
284 skb->data[3] = NLPID_CCITT_ANSI_LMI;
287 case cpu_to_be16(NLPID_CISCO_LMI):
289 skb_push(skb, head_len);
290 skb->data[3] = NLPID_CISCO_LMI;
293 case cpu_to_be16(ETH_P_IP):
295 skb_push(skb, head_len);
296 skb->data[3] = NLPID_IP;
299 case cpu_to_be16(ETH_P_IPV6):
301 skb_push(skb, head_len);
302 skb->data[3] = NLPID_IPV6;
305 case cpu_to_be16(ETH_P_802_3):
307 if (skb_headroom(skb) < head_len) {
308 struct sk_buff *skb2 = skb_realloc_headroom(skb,
315 skb_push(skb, head_len);
316 skb->data[3] = FR_PAD;
317 skb->data[4] = NLPID_SNAP;
318 skb->data[5] = FR_PAD;
322 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
327 skb_push(skb, head_len);
328 skb->data[3] = FR_PAD;
329 skb->data[4] = NLPID_SNAP;
330 skb->data[5] = FR_PAD;
331 skb->data[6] = FR_PAD;
332 skb->data[7] = FR_PAD;
333 *(__be16*)(skb->data + 8) = skb->protocol;
336 dlci_to_q922(skb->data, dlci);
337 skb->data[2] = FR_UI;
343 static int pvc_open(struct net_device *dev)
345 pvc_device *pvc = dev->ml_priv;
347 if ((pvc->frad->flags & IFF_UP) == 0)
348 return -EIO; /* Frad must be UP in order to activate PVC */
350 if (pvc->open_count++ == 0) {
351 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
352 if (state(hdlc)->settings.lmi == LMI_NONE)
353 pvc->state.active = netif_carrier_ok(pvc->frad);
355 pvc_carrier(pvc->state.active, pvc);
356 state(hdlc)->dce_changed = 1;
363 static int pvc_close(struct net_device *dev)
365 pvc_device *pvc = dev->ml_priv;
367 if (--pvc->open_count == 0) {
368 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
369 if (state(hdlc)->settings.lmi == LMI_NONE)
370 pvc->state.active = 0;
372 if (state(hdlc)->settings.dce) {
373 state(hdlc)->dce_changed = 1;
374 pvc->state.active = 0;
382 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
384 pvc_device *pvc = dev->ml_priv;
385 fr_proto_pvc_info info;
387 if (ifr->ifr_settings.type == IF_GET_PROTO) {
388 if (dev->type == ARPHRD_ETHER)
389 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
391 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
393 if (ifr->ifr_settings.size < sizeof(info)) {
394 /* data size wanted */
395 ifr->ifr_settings.size = sizeof(info);
399 info.dlci = pvc->dlci;
400 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
401 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
402 &info, sizeof(info)))
410 static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
412 pvc_device *pvc = dev->ml_priv;
414 if (pvc->state.active) {
415 if (dev->type == ARPHRD_ETHER) {
416 int pad = ETH_ZLEN - skb->len;
417 if (pad > 0) { /* Pad the frame with zeros */
419 if (skb_tailroom(skb) < pad)
420 if (pskb_expand_head(skb, 0, pad,
422 dev->stats.tx_dropped++;
427 memset(skb->data + len, 0, pad);
429 skb->protocol = cpu_to_be16(ETH_P_802_3);
431 if (!fr_hard_header(&skb, pvc->dlci)) {
432 dev->stats.tx_bytes += skb->len;
433 dev->stats.tx_packets++;
434 if (pvc->state.fecn) /* TX Congestion counter */
435 dev->stats.tx_compressed++;
436 skb->dev = pvc->frad;
442 dev->stats.tx_dropped++;
447 static inline void fr_log_dlci_active(pvc_device *pvc)
449 netdev_info(pvc->frad, "DLCI %d [%s%s%s]%s %s\n",
451 pvc->main ? pvc->main->name : "",
452 pvc->main && pvc->ether ? " " : "",
453 pvc->ether ? pvc->ether->name : "",
454 pvc->state.new ? " new" : "",
455 !pvc->state.exist ? "deleted" :
456 pvc->state.active ? "active" : "inactive");
461 static inline u8 fr_lmi_nextseq(u8 x)
468 static void fr_lmi_send(struct net_device *dev, int fullrep)
470 hdlc_device *hdlc = dev_to_hdlc(dev);
472 pvc_device *pvc = state(hdlc)->first_pvc;
473 int lmi = state(hdlc)->settings.lmi;
474 int dce = state(hdlc)->settings.dce;
475 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
476 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
480 if (dce && fullrep) {
481 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
482 if (len > HDLC_MAX_MRU) {
483 netdev_warn(dev, "Too many PVCs while sending LMI full report\n");
488 skb = dev_alloc_skb(len);
490 netdev_warn(dev, "Memory squeeze on fr_lmi_send()\n");
493 memset(skb->data, 0, len);
495 if (lmi == LMI_CISCO) {
496 skb->protocol = cpu_to_be16(NLPID_CISCO_LMI);
497 fr_hard_header(&skb, LMI_CISCO_DLCI);
499 skb->protocol = cpu_to_be16(NLPID_CCITT_ANSI_LMI);
500 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
502 data = skb_tail_pointer(skb);
503 data[i++] = LMI_CALLREF;
504 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
506 data[i++] = LMI_ANSI_LOCKSHIFT;
507 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
508 LMI_ANSI_CISCO_REPTYPE;
509 data[i++] = LMI_REPT_LEN;
510 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
511 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
512 data[i++] = LMI_INTEG_LEN;
513 data[i++] = state(hdlc)->txseq =
514 fr_lmi_nextseq(state(hdlc)->txseq);
515 data[i++] = state(hdlc)->rxseq;
517 if (dce && fullrep) {
519 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
520 LMI_ANSI_CISCO_PVCSTAT;
521 data[i++] = stat_len;
523 /* LMI start/restart */
524 if (state(hdlc)->reliable && !pvc->state.exist) {
525 pvc->state.exist = pvc->state.new = 1;
526 fr_log_dlci_active(pvc);
529 /* ifconfig PVC up */
530 if (pvc->open_count && !pvc->state.active &&
531 pvc->state.exist && !pvc->state.new) {
533 pvc->state.active = 1;
534 fr_log_dlci_active(pvc);
537 if (lmi == LMI_CISCO) {
538 data[i] = pvc->dlci >> 8;
539 data[i + 1] = pvc->dlci & 0xFF;
541 data[i] = (pvc->dlci >> 4) & 0x3F;
542 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
548 else if (pvc->state.active)
557 skb->priority = TC_PRIO_CONTROL;
559 skb_reset_network_header(skb);
566 static void fr_set_link_state(int reliable, struct net_device *dev)
568 hdlc_device *hdlc = dev_to_hdlc(dev);
569 pvc_device *pvc = state(hdlc)->first_pvc;
571 state(hdlc)->reliable = reliable;
573 netif_dormant_off(dev);
574 state(hdlc)->n391cnt = 0; /* Request full status */
575 state(hdlc)->dce_changed = 1;
577 if (state(hdlc)->settings.lmi == LMI_NONE) {
578 while (pvc) { /* Activate all PVCs */
580 pvc->state.exist = pvc->state.active = 1;
586 netif_dormant_on(dev);
587 while (pvc) { /* Deactivate all PVCs */
589 pvc->state.exist = pvc->state.active = 0;
591 if (!state(hdlc)->settings.dce)
592 pvc->state.bandwidth = 0;
599 static void fr_timer(unsigned long arg)
601 struct net_device *dev = (struct net_device *)arg;
602 hdlc_device *hdlc = dev_to_hdlc(dev);
603 int i, cnt = 0, reliable;
606 if (state(hdlc)->settings.dce) {
607 reliable = state(hdlc)->request &&
608 time_before(jiffies, state(hdlc)->last_poll +
609 state(hdlc)->settings.t392 * HZ);
610 state(hdlc)->request = 0;
612 state(hdlc)->last_errors <<= 1; /* Shift the list */
613 if (state(hdlc)->request) {
614 if (state(hdlc)->reliable)
615 netdev_info(dev, "No LMI status reply received\n");
616 state(hdlc)->last_errors |= 1;
619 list = state(hdlc)->last_errors;
620 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
621 cnt += (list & 1); /* errors count */
623 reliable = (cnt < state(hdlc)->settings.n392);
626 if (state(hdlc)->reliable != reliable) {
627 netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
628 fr_set_link_state(reliable, dev);
631 if (state(hdlc)->settings.dce)
632 state(hdlc)->timer.expires = jiffies +
633 state(hdlc)->settings.t392 * HZ;
635 if (state(hdlc)->n391cnt)
636 state(hdlc)->n391cnt--;
638 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
640 state(hdlc)->last_poll = jiffies;
641 state(hdlc)->request = 1;
642 state(hdlc)->timer.expires = jiffies +
643 state(hdlc)->settings.t391 * HZ;
646 state(hdlc)->timer.function = fr_timer;
647 state(hdlc)->timer.data = arg;
648 add_timer(&state(hdlc)->timer);
652 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
654 hdlc_device *hdlc = dev_to_hdlc(dev);
657 int lmi = state(hdlc)->settings.lmi;
658 int dce = state(hdlc)->settings.dce;
659 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
661 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
662 LMI_CCITT_CISCO_LENGTH)) {
663 netdev_info(dev, "Short LMI frame\n");
667 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
668 NLPID_CCITT_ANSI_LMI)) {
669 netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
673 if (skb->data[4] != LMI_CALLREF) {
674 netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
679 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
680 netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
685 if (lmi == LMI_ANSI) {
686 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
687 netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
695 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
696 LMI_ANSI_CISCO_REPTYPE)) {
697 netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
702 if (skb->data[++i] != LMI_REPT_LEN) {
703 netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
708 reptype = skb->data[++i];
709 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
710 netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
715 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
716 LMI_ANSI_CISCO_ALIVE)) {
717 netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
722 if (skb->data[++i] != LMI_INTEG_LEN) {
723 netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
729 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
730 rxseq = skb->data[i++]; /* Should confirm our sequence */
732 txseq = state(hdlc)->txseq;
735 state(hdlc)->last_poll = jiffies;
738 if (!state(hdlc)->reliable)
741 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
742 state(hdlc)->n391cnt = 0;
747 if (state(hdlc)->fullrep_sent && !error) {
748 /* Stop sending full report - the last one has been confirmed by DTE */
749 state(hdlc)->fullrep_sent = 0;
750 pvc = state(hdlc)->first_pvc;
752 if (pvc->state.new) {
755 /* Tell DTE that new PVC is now active */
756 state(hdlc)->dce_changed = 1;
762 if (state(hdlc)->dce_changed) {
763 reptype = LMI_FULLREP;
764 state(hdlc)->fullrep_sent = 1;
765 state(hdlc)->dce_changed = 0;
768 state(hdlc)->request = 1; /* got request */
769 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
775 state(hdlc)->request = 0; /* got response, no request pending */
780 if (reptype != LMI_FULLREP)
783 pvc = state(hdlc)->first_pvc;
786 pvc->state.deleted = 1;
791 while (skb->len >= i + 2 + stat_len) {
794 unsigned int active, new;
796 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
797 LMI_ANSI_CISCO_PVCSTAT)) {
798 netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
803 if (skb->data[++i] != stat_len) {
804 netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
810 new = !! (skb->data[i + 2] & 0x08);
811 active = !! (skb->data[i + 2] & 0x02);
812 if (lmi == LMI_CISCO) {
813 dlci = (skb->data[i] << 8) | skb->data[i + 1];
814 bw = (skb->data[i + 3] << 16) |
815 (skb->data[i + 4] << 8) |
818 dlci = ((skb->data[i] & 0x3F) << 4) |
819 ((skb->data[i + 1] & 0x78) >> 3);
823 pvc = add_pvc(dev, dlci);
825 if (!pvc && !no_ram) {
826 netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
831 pvc->state.exist = 1;
832 pvc->state.deleted = 0;
833 if (active != pvc->state.active ||
834 new != pvc->state.new ||
835 bw != pvc->state.bandwidth ||
837 pvc->state.new = new;
838 pvc->state.active = active;
839 pvc->state.bandwidth = bw;
840 pvc_carrier(active, pvc);
841 fr_log_dlci_active(pvc);
848 pvc = state(hdlc)->first_pvc;
851 if (pvc->state.deleted && pvc->state.exist) {
853 pvc->state.active = pvc->state.new = 0;
854 pvc->state.exist = 0;
855 pvc->state.bandwidth = 0;
856 fr_log_dlci_active(pvc);
861 /* Next full report after N391 polls */
862 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
868 static int fr_rx(struct sk_buff *skb)
870 struct net_device *frad = skb->dev;
871 hdlc_device *hdlc = dev_to_hdlc(frad);
872 fr_hdr *fh = (fr_hdr*)skb->data;
873 u8 *data = skb->data;
876 struct net_device *dev = NULL;
878 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
881 dlci = q922_to_dlci(skb->data);
883 if ((dlci == LMI_CCITT_ANSI_DLCI &&
884 (state(hdlc)->settings.lmi == LMI_ANSI ||
885 state(hdlc)->settings.lmi == LMI_CCITT)) ||
886 (dlci == LMI_CISCO_DLCI &&
887 state(hdlc)->settings.lmi == LMI_CISCO)) {
888 if (fr_lmi_recv(frad, skb))
890 dev_kfree_skb_any(skb);
891 return NET_RX_SUCCESS;
894 pvc = find_pvc(hdlc, dlci);
897 netdev_info(frad, "No PVC for received frame's DLCI %d\n",
900 dev_kfree_skb_any(skb);
904 if (pvc->state.fecn != fh->fecn) {
906 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
907 dlci, fh->fecn ? "N" : "FF");
909 pvc->state.fecn ^= 1;
912 if (pvc->state.becn != fh->becn) {
914 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
915 dlci, fh->becn ? "N" : "FF");
917 pvc->state.becn ^= 1;
921 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
922 frad->stats.rx_dropped++;
926 if (data[3] == NLPID_IP) {
927 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
929 skb->protocol = htons(ETH_P_IP);
931 } else if (data[3] == NLPID_IPV6) {
932 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
934 skb->protocol = htons(ETH_P_IPV6);
936 } else if (skb->len > 10 && data[3] == FR_PAD &&
937 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
938 u16 oui = ntohs(*(__be16*)(data + 6));
939 u16 pid = ntohs(*(__be16*)(data + 8));
942 switch ((((u32)oui) << 16) | pid) {
943 case ETH_P_ARP: /* routed frame with SNAP */
945 case ETH_P_IP: /* a long variant */
948 skb->protocol = htons(pid);
951 case 0x80C20007: /* bridged Ethernet frame */
952 if ((dev = pvc->ether) != NULL)
953 skb->protocol = eth_type_trans(skb, dev);
957 netdev_info(frad, "Unsupported protocol, OUI=%x PID=%x\n",
959 dev_kfree_skb_any(skb);
963 netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
965 dev_kfree_skb_any(skb);
970 dev->stats.rx_packets++; /* PVC traffic */
971 dev->stats.rx_bytes += skb->len;
973 dev->stats.rx_compressed++;
976 return NET_RX_SUCCESS;
978 dev_kfree_skb_any(skb);
983 frad->stats.rx_errors++; /* Mark error */
984 dev_kfree_skb_any(skb);
990 static void fr_start(struct net_device *dev)
992 hdlc_device *hdlc = dev_to_hdlc(dev);
994 printk(KERN_DEBUG "fr_start\n");
996 if (state(hdlc)->settings.lmi != LMI_NONE) {
997 state(hdlc)->reliable = 0;
998 state(hdlc)->dce_changed = 1;
999 state(hdlc)->request = 0;
1000 state(hdlc)->fullrep_sent = 0;
1001 state(hdlc)->last_errors = 0xFFFFFFFF;
1002 state(hdlc)->n391cnt = 0;
1003 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1005 init_timer(&state(hdlc)->timer);
1006 /* First poll after 1 s */
1007 state(hdlc)->timer.expires = jiffies + HZ;
1008 state(hdlc)->timer.function = fr_timer;
1009 state(hdlc)->timer.data = (unsigned long)dev;
1010 add_timer(&state(hdlc)->timer);
1012 fr_set_link_state(1, dev);
1016 static void fr_stop(struct net_device *dev)
1018 hdlc_device *hdlc = dev_to_hdlc(dev);
1020 printk(KERN_DEBUG "fr_stop\n");
1022 if (state(hdlc)->settings.lmi != LMI_NONE)
1023 del_timer_sync(&state(hdlc)->timer);
1024 fr_set_link_state(0, dev);
1028 static void fr_close(struct net_device *dev)
1030 hdlc_device *hdlc = dev_to_hdlc(dev);
1031 pvc_device *pvc = state(hdlc)->first_pvc;
1033 while (pvc) { /* Shutdown all PVCs for this FRAD */
1035 dev_close(pvc->main);
1037 dev_close(pvc->ether);
1043 static void pvc_setup(struct net_device *dev)
1045 dev->type = ARPHRD_DLCI;
1046 dev->flags = IFF_POINTOPOINT;
1047 dev->hard_header_len = 10;
1049 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1052 static const struct net_device_ops pvc_ops = {
1053 .ndo_open = pvc_open,
1054 .ndo_stop = pvc_close,
1055 .ndo_change_mtu = hdlc_change_mtu,
1056 .ndo_start_xmit = pvc_xmit,
1057 .ndo_do_ioctl = pvc_ioctl,
1060 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1062 hdlc_device *hdlc = dev_to_hdlc(frad);
1064 struct net_device *dev;
1067 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1068 netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
1072 if (*get_dev_p(pvc, type))
1075 used = pvc_is_used(pvc);
1077 if (type == ARPHRD_ETHER) {
1078 dev = alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN,
1080 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1082 dev = alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN, pvc_setup);
1085 netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
1086 delete_unused_pvcs(hdlc);
1090 if (type == ARPHRD_ETHER)
1091 eth_hw_addr_random(dev);
1093 *(__be16*)dev->dev_addr = htons(dlci);
1094 dlci_to_q922(dev->broadcast, dlci);
1096 dev->netdev_ops = &pvc_ops;
1097 dev->mtu = HDLC_MAX_MTU;
1098 dev->tx_queue_len = 0;
1101 if (register_netdevice(dev) != 0) {
1103 delete_unused_pvcs(hdlc);
1107 dev->destructor = free_netdev;
1108 *get_dev_p(pvc, type) = dev;
1110 state(hdlc)->dce_changed = 1;
1111 state(hdlc)->dce_pvc_count++;
1118 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1121 struct net_device *dev;
1123 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1126 if ((dev = *get_dev_p(pvc, type)) == NULL)
1129 if (dev->flags & IFF_UP)
1130 return -EBUSY; /* PVC in use */
1132 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1133 *get_dev_p(pvc, type) = NULL;
1135 if (!pvc_is_used(pvc)) {
1136 state(hdlc)->dce_pvc_count--;
1137 state(hdlc)->dce_changed = 1;
1139 delete_unused_pvcs(hdlc);
1145 static void fr_destroy(struct net_device *frad)
1147 hdlc_device *hdlc = dev_to_hdlc(frad);
1148 pvc_device *pvc = state(hdlc)->first_pvc;
1149 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1150 state(hdlc)->dce_pvc_count = 0;
1151 state(hdlc)->dce_changed = 1;
1154 pvc_device *next = pvc->next;
1155 /* destructors will free_netdev() main and ether */
1157 unregister_netdevice(pvc->main);
1160 unregister_netdevice(pvc->ether);
1168 static struct hdlc_proto proto = {
1172 .detach = fr_destroy,
1175 .module = THIS_MODULE,
1179 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1181 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1182 const size_t size = sizeof(fr_proto);
1183 fr_proto new_settings;
1184 hdlc_device *hdlc = dev_to_hdlc(dev);
1188 switch (ifr->ifr_settings.type) {
1190 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1192 ifr->ifr_settings.type = IF_PROTO_FR;
1193 if (ifr->ifr_settings.size < size) {
1194 ifr->ifr_settings.size = size; /* data size wanted */
1197 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1202 if (!capable(CAP_NET_ADMIN))
1205 if (dev->flags & IFF_UP)
1208 if (copy_from_user(&new_settings, fr_s, size))
1211 if (new_settings.lmi == LMI_DEFAULT)
1212 new_settings.lmi = LMI_ANSI;
1214 if ((new_settings.lmi != LMI_NONE &&
1215 new_settings.lmi != LMI_ANSI &&
1216 new_settings.lmi != LMI_CCITT &&
1217 new_settings.lmi != LMI_CISCO) ||
1218 new_settings.t391 < 1 ||
1219 new_settings.t392 < 2 ||
1220 new_settings.n391 < 1 ||
1221 new_settings.n392 < 1 ||
1222 new_settings.n393 < new_settings.n392 ||
1223 new_settings.n393 > 32 ||
1224 (new_settings.dce != 0 &&
1225 new_settings.dce != 1))
1228 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1232 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1233 result = attach_hdlc_protocol(dev, &proto,
1234 sizeof(struct frad_state));
1237 state(hdlc)->first_pvc = NULL;
1238 state(hdlc)->dce_pvc_count = 0;
1240 memcpy(&state(hdlc)->settings, &new_settings, size);
1241 dev->type = ARPHRD_FRAD;
1244 case IF_PROTO_FR_ADD_PVC:
1245 case IF_PROTO_FR_DEL_PVC:
1246 case IF_PROTO_FR_ADD_ETH_PVC:
1247 case IF_PROTO_FR_DEL_ETH_PVC:
1248 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1251 if (!capable(CAP_NET_ADMIN))
1254 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1255 sizeof(fr_proto_pvc)))
1258 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1259 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1261 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1262 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1263 result = ARPHRD_ETHER; /* bridged Ethernet device */
1265 result = ARPHRD_DLCI;
1267 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1268 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1269 return fr_add_pvc(dev, pvc.dlci, result);
1271 return fr_del_pvc(hdlc, pvc.dlci, result);
1278 static int __init mod_init(void)
1280 register_hdlc_protocol(&proto);
1285 static void __exit mod_exit(void)
1287 unregister_hdlc_protocol(&proto);
1291 module_init(mod_init);
1292 module_exit(mod_exit);
1294 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1295 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1296 MODULE_LICENSE("GPL v2");