Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wan / hdlc_fr.c
blob0720f5f92caa7b0bfcb80ffb432340356ea7034c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic HDLC support routines for Linux
4 * Frame Relay support
6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
9 Theory of PVC state
11 DCE mode:
13 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
14 0,x -> 1,1 if "link reliable" when sending FULL STATUS
15 1,1 -> 1,0 if received FULL STATUS ACK
17 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
18 -> 1 when "PVC up" and (exist,new) = 1,0
20 DTE mode:
21 (exist,new,active) = FULL STATUS if "link reliable"
22 = 0, 0, 0 if "link unreliable"
23 No LMI:
24 active = open and "link reliable"
25 exist = new = not used
27 CCITT LMI: ITU-T Q.933 Annex A
28 ANSI LMI: ANSI T1.617 Annex D
29 CISCO LMI: the original, aka "Gang of Four" LMI
33 #include <linux/errno.h>
34 #include <linux/etherdevice.h>
35 #include <linux/hdlc.h>
36 #include <linux/if_arp.h>
37 #include <linux/inetdevice.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/pkt_sched.h>
42 #include <linux/poll.h>
43 #include <linux/rtnetlink.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
47 #undef DEBUG_PKT
48 #undef DEBUG_ECN
49 #undef DEBUG_LINK
50 #undef DEBUG_PROTO
51 #undef DEBUG_PVC
53 #define FR_UI 0x03
54 #define FR_PAD 0x00
56 #define NLPID_IP 0xCC
57 #define NLPID_IPV6 0x8E
58 #define NLPID_SNAP 0x80
59 #define NLPID_PAD 0x00
60 #define NLPID_CCITT_ANSI_LMI 0x08
61 #define NLPID_CISCO_LMI 0x09
64 #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
65 #define LMI_CISCO_DLCI 1023
67 #define LMI_CALLREF 0x00 /* Call Reference */
68 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
69 #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
70 #define LMI_CCITT_REPTYPE 0x51
71 #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
72 #define LMI_CCITT_ALIVE 0x53
73 #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
74 #define LMI_CCITT_PVCSTAT 0x57
76 #define LMI_FULLREP 0x00 /* full report */
77 #define LMI_INTEGRITY 0x01 /* link integrity report */
78 #define LMI_SINGLE 0x02 /* single PVC report */
80 #define LMI_STATUS_ENQUIRY 0x75
81 #define LMI_STATUS 0x7D /* reply */
83 #define LMI_REPT_LEN 1 /* report type element length */
84 #define LMI_INTEG_LEN 2 /* link integrity element length */
86 #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
87 #define LMI_ANSI_LENGTH 14
90 struct fr_hdr {
91 #if defined(__LITTLE_ENDIAN_BITFIELD)
92 unsigned ea1: 1;
93 unsigned cr: 1;
94 unsigned dlcih: 6;
96 unsigned ea2: 1;
97 unsigned de: 1;
98 unsigned becn: 1;
99 unsigned fecn: 1;
100 unsigned dlcil: 4;
101 #else
102 unsigned dlcih: 6;
103 unsigned cr: 1;
104 unsigned ea1: 1;
106 unsigned dlcil: 4;
107 unsigned fecn: 1;
108 unsigned becn: 1;
109 unsigned de: 1;
110 unsigned ea2: 1;
111 #endif
112 } __packed;
115 struct pvc_device {
116 struct net_device *frad;
117 struct net_device *main;
118 struct net_device *ether; /* bridged Ethernet interface */
119 struct pvc_device *next; /* Sorted in ascending DLCI order */
120 int dlci;
121 int open_count;
123 struct {
124 unsigned int new: 1;
125 unsigned int active: 1;
126 unsigned int exist: 1;
127 unsigned int deleted: 1;
128 unsigned int fecn: 1;
129 unsigned int becn: 1;
130 unsigned int bandwidth; /* Cisco LMI reporting only */
131 }state;
134 struct frad_state {
135 fr_proto settings;
136 struct pvc_device *first_pvc;
137 int dce_pvc_count;
139 struct timer_list timer;
140 struct net_device *dev;
141 unsigned long last_poll;
142 int reliable;
143 int dce_changed;
144 int request;
145 int fullrep_sent;
146 u32 last_errors; /* last errors bit list */
147 u8 n391cnt;
148 u8 txseq; /* TX sequence number */
149 u8 rxseq; /* RX sequence number */
153 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
156 static inline u16 q922_to_dlci(u8 *hdr)
158 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
162 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
164 hdr[0] = (dlci >> 2) & 0xFC;
165 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
169 static inline struct frad_state* state(hdlc_device *hdlc)
171 return(struct frad_state *)(hdlc->state);
175 static inline struct pvc_device *find_pvc(hdlc_device *hdlc, u16 dlci)
177 struct pvc_device *pvc = state(hdlc)->first_pvc;
179 while (pvc) {
180 if (pvc->dlci == dlci)
181 return pvc;
182 if (pvc->dlci > dlci)
183 return NULL; /* the list is sorted */
184 pvc = pvc->next;
187 return NULL;
191 static struct pvc_device *add_pvc(struct net_device *dev, u16 dlci)
193 hdlc_device *hdlc = dev_to_hdlc(dev);
194 struct pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
196 while (*pvc_p) {
197 if ((*pvc_p)->dlci == dlci)
198 return *pvc_p;
199 if ((*pvc_p)->dlci > dlci)
200 break; /* the list is sorted */
201 pvc_p = &(*pvc_p)->next;
204 pvc = kzalloc(sizeof(*pvc), GFP_ATOMIC);
205 #ifdef DEBUG_PVC
206 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
207 #endif
208 if (!pvc)
209 return NULL;
211 pvc->dlci = dlci;
212 pvc->frad = dev;
213 pvc->next = *pvc_p; /* Put it in the chain */
214 *pvc_p = pvc;
215 return pvc;
219 static inline int pvc_is_used(struct pvc_device *pvc)
221 return pvc->main || pvc->ether;
225 static inline void pvc_carrier(int on, struct pvc_device *pvc)
227 if (on) {
228 if (pvc->main)
229 if (!netif_carrier_ok(pvc->main))
230 netif_carrier_on(pvc->main);
231 if (pvc->ether)
232 if (!netif_carrier_ok(pvc->ether))
233 netif_carrier_on(pvc->ether);
234 } else {
235 if (pvc->main)
236 if (netif_carrier_ok(pvc->main))
237 netif_carrier_off(pvc->main);
238 if (pvc->ether)
239 if (netif_carrier_ok(pvc->ether))
240 netif_carrier_off(pvc->ether);
245 static inline void delete_unused_pvcs(hdlc_device *hdlc)
247 struct pvc_device **pvc_p = &state(hdlc)->first_pvc;
249 while (*pvc_p) {
250 if (!pvc_is_used(*pvc_p)) {
251 struct pvc_device *pvc = *pvc_p;
252 #ifdef DEBUG_PVC
253 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
254 #endif
255 *pvc_p = pvc->next;
256 kfree(pvc);
257 continue;
259 pvc_p = &(*pvc_p)->next;
264 static inline struct net_device **get_dev_p(struct pvc_device *pvc,
265 int type)
267 if (type == ARPHRD_ETHER)
268 return &pvc->ether;
269 else
270 return &pvc->main;
274 static int fr_hard_header(struct sk_buff *skb, u16 dlci)
276 if (!skb->dev) { /* Control packets */
277 switch (dlci) {
278 case LMI_CCITT_ANSI_DLCI:
279 skb_push(skb, 4);
280 skb->data[3] = NLPID_CCITT_ANSI_LMI;
281 break;
283 case LMI_CISCO_DLCI:
284 skb_push(skb, 4);
285 skb->data[3] = NLPID_CISCO_LMI;
286 break;
288 default:
289 return -EINVAL;
292 } else if (skb->dev->type == ARPHRD_DLCI) {
293 switch (skb->protocol) {
294 case htons(ETH_P_IP):
295 skb_push(skb, 4);
296 skb->data[3] = NLPID_IP;
297 break;
299 case htons(ETH_P_IPV6):
300 skb_push(skb, 4);
301 skb->data[3] = NLPID_IPV6;
302 break;
304 default:
305 skb_push(skb, 10);
306 skb->data[3] = FR_PAD;
307 skb->data[4] = NLPID_SNAP;
308 /* OUI 00-00-00 indicates an Ethertype follows */
309 skb->data[5] = 0x00;
310 skb->data[6] = 0x00;
311 skb->data[7] = 0x00;
312 /* This should be an Ethertype: */
313 *(__be16 *)(skb->data + 8) = skb->protocol;
316 } else if (skb->dev->type == ARPHRD_ETHER) {
317 skb_push(skb, 10);
318 skb->data[3] = FR_PAD;
319 skb->data[4] = NLPID_SNAP;
320 /* OUI 00-80-C2 stands for the 802.1 organization */
321 skb->data[5] = 0x00;
322 skb->data[6] = 0x80;
323 skb->data[7] = 0xC2;
324 /* PID 00-07 stands for Ethernet frames without FCS */
325 skb->data[8] = 0x00;
326 skb->data[9] = 0x07;
328 } else {
329 return -EINVAL;
332 dlci_to_q922(skb->data, dlci);
333 skb->data[2] = FR_UI;
334 return 0;
339 static int pvc_open(struct net_device *dev)
341 struct pvc_device *pvc = dev->ml_priv;
343 if ((pvc->frad->flags & IFF_UP) == 0)
344 return -EIO; /* Frad must be UP in order to activate PVC */
346 if (pvc->open_count++ == 0) {
347 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
348 if (state(hdlc)->settings.lmi == LMI_NONE)
349 pvc->state.active = netif_carrier_ok(pvc->frad);
351 pvc_carrier(pvc->state.active, pvc);
352 state(hdlc)->dce_changed = 1;
354 return 0;
359 static int pvc_close(struct net_device *dev)
361 struct pvc_device *pvc = dev->ml_priv;
363 if (--pvc->open_count == 0) {
364 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
365 if (state(hdlc)->settings.lmi == LMI_NONE)
366 pvc->state.active = 0;
368 if (state(hdlc)->settings.dce) {
369 state(hdlc)->dce_changed = 1;
370 pvc->state.active = 0;
373 return 0;
378 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
380 struct pvc_device *pvc = dev->ml_priv;
381 fr_proto_pvc_info info;
383 if (ifr->ifr_settings.type == IF_GET_PROTO) {
384 if (dev->type == ARPHRD_ETHER)
385 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
386 else
387 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
389 if (ifr->ifr_settings.size < sizeof(info)) {
390 /* data size wanted */
391 ifr->ifr_settings.size = sizeof(info);
392 return -ENOBUFS;
395 info.dlci = pvc->dlci;
396 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
397 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
398 &info, sizeof(info)))
399 return -EFAULT;
400 return 0;
403 return -EINVAL;
406 static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev)
408 struct pvc_device *pvc = dev->ml_priv;
410 if (!pvc->state.active)
411 goto drop;
413 if (dev->type == ARPHRD_ETHER) {
414 int pad = ETH_ZLEN - skb->len;
416 if (pad > 0) { /* Pad the frame with zeros */
417 if (__skb_pad(skb, pad, false))
418 goto drop;
419 skb_put(skb, pad);
423 /* We already requested the header space with dev->needed_headroom.
424 * So this is just a protection in case the upper layer didn't take
425 * dev->needed_headroom into consideration.
427 if (skb_headroom(skb) < 10) {
428 struct sk_buff *skb2 = skb_realloc_headroom(skb, 10);
430 if (!skb2)
431 goto drop;
432 dev_kfree_skb(skb);
433 skb = skb2;
436 skb->dev = dev;
437 if (fr_hard_header(skb, pvc->dlci))
438 goto drop;
440 dev->stats.tx_bytes += skb->len;
441 dev->stats.tx_packets++;
442 if (pvc->state.fecn) /* TX Congestion counter */
443 dev->stats.tx_compressed++;
444 skb->dev = pvc->frad;
445 skb->protocol = htons(ETH_P_HDLC);
446 skb_reset_network_header(skb);
447 dev_queue_xmit(skb);
448 return NETDEV_TX_OK;
450 drop:
451 dev->stats.tx_dropped++;
452 kfree_skb(skb);
453 return NETDEV_TX_OK;
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",
459 pvc->dlci,
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)
472 x++;
473 return x ? x : 1;
477 static void fr_lmi_send(struct net_device *dev, int fullrep)
479 hdlc_device *hdlc = dev_to_hdlc(dev);
480 struct sk_buff *skb;
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;
486 u8 *data;
487 int i = 0;
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");
493 return;
497 skb = dev_alloc_skb(len);
498 if (!skb) {
499 netdev_warn(dev, "Memory squeeze on fr_lmi_send()\n");
500 return;
502 memset(skb->data, 0, len);
503 skb_reserve(skb, 4);
504 if (lmi == LMI_CISCO) {
505 fr_hard_header(skb, LMI_CISCO_DLCI);
506 } else {
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;
512 if (lmi == LMI_ANSI)
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) {
525 while (pvc) {
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) {
539 pvc_carrier(1, pvc);
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;
547 } else {
548 data[i] = (pvc->dlci >> 4) & 0x3F;
549 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
550 data[i + 2] = 0x80;
553 if (pvc->state.new)
554 data[i + 2] |= 0x08;
555 else if (pvc->state.active)
556 data[i + 2] |= 0x02;
558 i += stat_len;
559 pvc = pvc->next;
563 skb_put(skb, i);
564 skb->priority = TC_PRIO_CONTROL;
565 skb->dev = dev;
566 skb->protocol = htons(ETH_P_HDLC);
567 skb_reset_network_header(skb);
569 dev_queue_xmit(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;
580 if (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 */
587 pvc_carrier(1, pvc);
588 pvc->state.exist = pvc->state.active = 1;
589 pvc->state.new = 0;
590 pvc = pvc->next;
593 } else {
594 netif_dormant_on(dev);
595 while (pvc) { /* Deactivate all PVCs */
596 pvc_carrier(0, pvc);
597 pvc->state.exist = pvc->state.active = 0;
598 pvc->state.new = 0;
599 if (!state(hdlc)->settings.dce)
600 pvc->state.bandwidth = 0;
601 pvc = pvc->next;
607 static void fr_timer(struct timer_list *t)
609 struct frad_state *st = from_timer(st, t, timer);
610 struct net_device *dev = st->dev;
611 hdlc_device *hdlc = dev_to_hdlc(dev);
612 int i, cnt = 0, reliable;
613 u32 list;
615 if (state(hdlc)->settings.dce) {
616 reliable = state(hdlc)->request &&
617 time_before(jiffies, state(hdlc)->last_poll +
618 state(hdlc)->settings.t392 * HZ);
619 state(hdlc)->request = 0;
620 } else {
621 state(hdlc)->last_errors <<= 1; /* Shift the list */
622 if (state(hdlc)->request) {
623 if (state(hdlc)->reliable)
624 netdev_info(dev, "No LMI status reply received\n");
625 state(hdlc)->last_errors |= 1;
628 list = state(hdlc)->last_errors;
629 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
630 cnt += (list & 1); /* errors count */
632 reliable = (cnt < state(hdlc)->settings.n392);
635 if (state(hdlc)->reliable != reliable) {
636 netdev_info(dev, "Link %sreliable\n", reliable ? "" : "un");
637 fr_set_link_state(reliable, dev);
640 if (state(hdlc)->settings.dce)
641 state(hdlc)->timer.expires = jiffies +
642 state(hdlc)->settings.t392 * HZ;
643 else {
644 if (state(hdlc)->n391cnt)
645 state(hdlc)->n391cnt--;
647 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
649 state(hdlc)->last_poll = jiffies;
650 state(hdlc)->request = 1;
651 state(hdlc)->timer.expires = jiffies +
652 state(hdlc)->settings.t391 * HZ;
655 add_timer(&state(hdlc)->timer);
659 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
661 hdlc_device *hdlc = dev_to_hdlc(dev);
662 struct pvc_device *pvc;
663 u8 rxseq, txseq;
664 int lmi = state(hdlc)->settings.lmi;
665 int dce = state(hdlc)->settings.dce;
666 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
668 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
669 LMI_CCITT_CISCO_LENGTH)) {
670 netdev_info(dev, "Short LMI frame\n");
671 return 1;
674 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
675 NLPID_CCITT_ANSI_LMI)) {
676 netdev_info(dev, "Received non-LMI frame with LMI DLCI\n");
677 return 1;
680 if (skb->data[4] != LMI_CALLREF) {
681 netdev_info(dev, "Invalid LMI Call reference (0x%02X)\n",
682 skb->data[4]);
683 return 1;
686 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
687 netdev_info(dev, "Invalid LMI Message type (0x%02X)\n",
688 skb->data[5]);
689 return 1;
692 if (lmi == LMI_ANSI) {
693 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
694 netdev_info(dev, "Not ANSI locking shift in LMI message (0x%02X)\n",
695 skb->data[6]);
696 return 1;
698 i = 7;
699 } else
700 i = 6;
702 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
703 LMI_ANSI_CISCO_REPTYPE)) {
704 netdev_info(dev, "Not an LMI Report type IE (0x%02X)\n",
705 skb->data[i]);
706 return 1;
709 if (skb->data[++i] != LMI_REPT_LEN) {
710 netdev_info(dev, "Invalid LMI Report type IE length (%u)\n",
711 skb->data[i]);
712 return 1;
715 reptype = skb->data[++i];
716 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
717 netdev_info(dev, "Unsupported LMI Report type (0x%02X)\n",
718 reptype);
719 return 1;
722 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
723 LMI_ANSI_CISCO_ALIVE)) {
724 netdev_info(dev, "Not an LMI Link integrity verification IE (0x%02X)\n",
725 skb->data[i]);
726 return 1;
729 if (skb->data[++i] != LMI_INTEG_LEN) {
730 netdev_info(dev, "Invalid LMI Link integrity verification IE length (%u)\n",
731 skb->data[i]);
732 return 1;
734 i++;
736 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
737 rxseq = skb->data[i++]; /* Should confirm our sequence */
739 txseq = state(hdlc)->txseq;
741 if (dce)
742 state(hdlc)->last_poll = jiffies;
744 error = 0;
745 if (!state(hdlc)->reliable)
746 error = 1;
748 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
749 state(hdlc)->n391cnt = 0;
750 error = 1;
753 if (dce) {
754 if (state(hdlc)->fullrep_sent && !error) {
755 /* Stop sending full report - the last one has been confirmed by DTE */
756 state(hdlc)->fullrep_sent = 0;
757 pvc = state(hdlc)->first_pvc;
758 while (pvc) {
759 if (pvc->state.new) {
760 pvc->state.new = 0;
762 /* Tell DTE that new PVC is now active */
763 state(hdlc)->dce_changed = 1;
765 pvc = pvc->next;
769 if (state(hdlc)->dce_changed) {
770 reptype = LMI_FULLREP;
771 state(hdlc)->fullrep_sent = 1;
772 state(hdlc)->dce_changed = 0;
775 state(hdlc)->request = 1; /* got request */
776 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
777 return 0;
780 /* DTE */
782 state(hdlc)->request = 0; /* got response, no request pending */
784 if (error)
785 return 0;
787 if (reptype != LMI_FULLREP)
788 return 0;
790 pvc = state(hdlc)->first_pvc;
792 while (pvc) {
793 pvc->state.deleted = 1;
794 pvc = pvc->next;
797 no_ram = 0;
798 while (skb->len >= i + 2 + stat_len) {
799 u16 dlci;
800 u32 bw;
801 unsigned int active, new;
803 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
804 LMI_ANSI_CISCO_PVCSTAT)) {
805 netdev_info(dev, "Not an LMI PVC status IE (0x%02X)\n",
806 skb->data[i]);
807 return 1;
810 if (skb->data[++i] != stat_len) {
811 netdev_info(dev, "Invalid LMI PVC status IE length (%u)\n",
812 skb->data[i]);
813 return 1;
815 i++;
817 new = !! (skb->data[i + 2] & 0x08);
818 active = !! (skb->data[i + 2] & 0x02);
819 if (lmi == LMI_CISCO) {
820 dlci = (skb->data[i] << 8) | skb->data[i + 1];
821 bw = (skb->data[i + 3] << 16) |
822 (skb->data[i + 4] << 8) |
823 (skb->data[i + 5]);
824 } else {
825 dlci = ((skb->data[i] & 0x3F) << 4) |
826 ((skb->data[i + 1] & 0x78) >> 3);
827 bw = 0;
830 pvc = add_pvc(dev, dlci);
832 if (!pvc && !no_ram) {
833 netdev_warn(dev, "Memory squeeze on fr_lmi_recv()\n");
834 no_ram = 1;
837 if (pvc) {
838 pvc->state.exist = 1;
839 pvc->state.deleted = 0;
840 if (active != pvc->state.active ||
841 new != pvc->state.new ||
842 bw != pvc->state.bandwidth ||
843 !pvc->state.exist) {
844 pvc->state.new = new;
845 pvc->state.active = active;
846 pvc->state.bandwidth = bw;
847 pvc_carrier(active, pvc);
848 fr_log_dlci_active(pvc);
852 i += stat_len;
855 pvc = state(hdlc)->first_pvc;
857 while (pvc) {
858 if (pvc->state.deleted && pvc->state.exist) {
859 pvc_carrier(0, pvc);
860 pvc->state.active = pvc->state.new = 0;
861 pvc->state.exist = 0;
862 pvc->state.bandwidth = 0;
863 fr_log_dlci_active(pvc);
865 pvc = pvc->next;
868 /* Next full report after N391 polls */
869 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
871 return 0;
874 static int fr_snap_parse(struct sk_buff *skb, struct pvc_device *pvc)
876 /* OUI 00-00-00 indicates an Ethertype follows */
877 if (skb->data[0] == 0x00 &&
878 skb->data[1] == 0x00 &&
879 skb->data[2] == 0x00) {
880 if (!pvc->main)
881 return -1;
882 skb->dev = pvc->main;
883 skb->protocol = *(__be16 *)(skb->data + 3); /* Ethertype */
884 skb_pull(skb, 5);
885 skb_reset_mac_header(skb);
886 return 0;
888 /* OUI 00-80-C2 stands for the 802.1 organization */
889 } else if (skb->data[0] == 0x00 &&
890 skb->data[1] == 0x80 &&
891 skb->data[2] == 0xC2) {
892 /* PID 00-07 stands for Ethernet frames without FCS */
893 if (skb->data[3] == 0x00 &&
894 skb->data[4] == 0x07) {
895 if (!pvc->ether)
896 return -1;
897 skb_pull(skb, 5);
898 if (skb->len < ETH_HLEN)
899 return -1;
900 skb->protocol = eth_type_trans(skb, pvc->ether);
901 return 0;
903 /* PID unsupported */
904 } else {
905 return -1;
908 /* OUI unsupported */
909 } else {
910 return -1;
914 static int fr_rx(struct sk_buff *skb)
916 struct net_device *frad = skb->dev;
917 hdlc_device *hdlc = dev_to_hdlc(frad);
918 struct fr_hdr *fh = (struct fr_hdr *)skb->data;
919 u8 *data = skb->data;
920 u16 dlci;
921 struct pvc_device *pvc;
922 struct net_device *dev;
924 if (skb->len < 4 || fh->ea1 || !fh->ea2 || data[2] != FR_UI)
925 goto rx_error;
927 dlci = q922_to_dlci(skb->data);
929 if ((dlci == LMI_CCITT_ANSI_DLCI &&
930 (state(hdlc)->settings.lmi == LMI_ANSI ||
931 state(hdlc)->settings.lmi == LMI_CCITT)) ||
932 (dlci == LMI_CISCO_DLCI &&
933 state(hdlc)->settings.lmi == LMI_CISCO)) {
934 if (fr_lmi_recv(frad, skb))
935 goto rx_error;
936 dev_kfree_skb_any(skb);
937 return NET_RX_SUCCESS;
940 pvc = find_pvc(hdlc, dlci);
941 if (!pvc) {
942 #ifdef DEBUG_PKT
943 netdev_info(frad, "No PVC for received frame's DLCI %d\n",
944 dlci);
945 #endif
946 goto rx_drop;
949 if (pvc->state.fecn != fh->fecn) {
950 #ifdef DEBUG_ECN
951 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
952 dlci, fh->fecn ? "N" : "FF");
953 #endif
954 pvc->state.fecn ^= 1;
957 if (pvc->state.becn != fh->becn) {
958 #ifdef DEBUG_ECN
959 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
960 dlci, fh->becn ? "N" : "FF");
961 #endif
962 pvc->state.becn ^= 1;
966 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
967 frad->stats.rx_dropped++;
968 return NET_RX_DROP;
971 if (data[3] == NLPID_IP) {
972 if (!pvc->main)
973 goto rx_drop;
974 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
975 skb->dev = pvc->main;
976 skb->protocol = htons(ETH_P_IP);
977 skb_reset_mac_header(skb);
979 } else if (data[3] == NLPID_IPV6) {
980 if (!pvc->main)
981 goto rx_drop;
982 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
983 skb->dev = pvc->main;
984 skb->protocol = htons(ETH_P_IPV6);
985 skb_reset_mac_header(skb);
987 } else if (data[3] == FR_PAD) {
988 if (skb->len < 5)
989 goto rx_error;
990 if (data[4] == NLPID_SNAP) { /* A SNAP header follows */
991 skb_pull(skb, 5);
992 if (skb->len < 5) /* Incomplete SNAP header */
993 goto rx_error;
994 if (fr_snap_parse(skb, pvc))
995 goto rx_drop;
996 } else {
997 goto rx_drop;
1000 } else {
1001 netdev_info(frad, "Unsupported protocol, NLPID=%x length=%i\n",
1002 data[3], skb->len);
1003 goto rx_drop;
1006 dev = skb->dev;
1007 dev->stats.rx_packets++; /* PVC traffic */
1008 dev->stats.rx_bytes += skb->len;
1009 if (pvc->state.becn)
1010 dev->stats.rx_compressed++;
1011 netif_rx(skb);
1012 return NET_RX_SUCCESS;
1014 rx_error:
1015 frad->stats.rx_errors++; /* Mark error */
1016 rx_drop:
1017 dev_kfree_skb_any(skb);
1018 return NET_RX_DROP;
1023 static void fr_start(struct net_device *dev)
1025 hdlc_device *hdlc = dev_to_hdlc(dev);
1026 #ifdef DEBUG_LINK
1027 printk(KERN_DEBUG "fr_start\n");
1028 #endif
1029 if (state(hdlc)->settings.lmi != LMI_NONE) {
1030 state(hdlc)->reliable = 0;
1031 state(hdlc)->dce_changed = 1;
1032 state(hdlc)->request = 0;
1033 state(hdlc)->fullrep_sent = 0;
1034 state(hdlc)->last_errors = 0xFFFFFFFF;
1035 state(hdlc)->n391cnt = 0;
1036 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1038 state(hdlc)->dev = dev;
1039 timer_setup(&state(hdlc)->timer, fr_timer, 0);
1040 /* First poll after 1 s */
1041 state(hdlc)->timer.expires = jiffies + HZ;
1042 add_timer(&state(hdlc)->timer);
1043 } else
1044 fr_set_link_state(1, dev);
1048 static void fr_stop(struct net_device *dev)
1050 hdlc_device *hdlc = dev_to_hdlc(dev);
1051 #ifdef DEBUG_LINK
1052 printk(KERN_DEBUG "fr_stop\n");
1053 #endif
1054 if (state(hdlc)->settings.lmi != LMI_NONE)
1055 del_timer_sync(&state(hdlc)->timer);
1056 fr_set_link_state(0, dev);
1060 static void fr_close(struct net_device *dev)
1062 hdlc_device *hdlc = dev_to_hdlc(dev);
1063 struct pvc_device *pvc = state(hdlc)->first_pvc;
1065 while (pvc) { /* Shutdown all PVCs for this FRAD */
1066 if (pvc->main)
1067 dev_close(pvc->main);
1068 if (pvc->ether)
1069 dev_close(pvc->ether);
1070 pvc = pvc->next;
1075 static void pvc_setup(struct net_device *dev)
1077 dev->type = ARPHRD_DLCI;
1078 dev->flags = IFF_POINTOPOINT;
1079 dev->hard_header_len = 0;
1080 dev->addr_len = 2;
1081 netif_keep_dst(dev);
1084 static const struct net_device_ops pvc_ops = {
1085 .ndo_open = pvc_open,
1086 .ndo_stop = pvc_close,
1087 .ndo_start_xmit = pvc_xmit,
1088 .ndo_do_ioctl = pvc_ioctl,
1091 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1093 hdlc_device *hdlc = dev_to_hdlc(frad);
1094 struct pvc_device *pvc;
1095 struct net_device *dev;
1096 int used;
1098 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1099 netdev_warn(frad, "Memory squeeze on fr_add_pvc()\n");
1100 return -ENOBUFS;
1103 if (*get_dev_p(pvc, type))
1104 return -EEXIST;
1106 used = pvc_is_used(pvc);
1108 if (type == ARPHRD_ETHER)
1109 dev = alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN,
1110 ether_setup);
1111 else
1112 dev = alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN, pvc_setup);
1114 if (!dev) {
1115 netdev_warn(frad, "Memory squeeze on fr_pvc()\n");
1116 delete_unused_pvcs(hdlc);
1117 return -ENOBUFS;
1120 if (type == ARPHRD_ETHER) {
1121 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1122 eth_hw_addr_random(dev);
1123 } else {
1124 *(__be16*)dev->dev_addr = htons(dlci);
1125 dlci_to_q922(dev->broadcast, dlci);
1127 dev->netdev_ops = &pvc_ops;
1128 dev->mtu = HDLC_MAX_MTU;
1129 dev->min_mtu = 68;
1130 dev->max_mtu = HDLC_MAX_MTU;
1131 dev->needed_headroom = 10;
1132 dev->priv_flags |= IFF_NO_QUEUE;
1133 dev->ml_priv = pvc;
1135 if (register_netdevice(dev) != 0) {
1136 free_netdev(dev);
1137 delete_unused_pvcs(hdlc);
1138 return -EIO;
1141 dev->needs_free_netdev = true;
1142 *get_dev_p(pvc, type) = dev;
1143 if (!used) {
1144 state(hdlc)->dce_changed = 1;
1145 state(hdlc)->dce_pvc_count++;
1147 return 0;
1152 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1154 struct pvc_device *pvc;
1155 struct net_device *dev;
1157 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1158 return -ENOENT;
1160 if ((dev = *get_dev_p(pvc, type)) == NULL)
1161 return -ENOENT;
1163 if (dev->flags & IFF_UP)
1164 return -EBUSY; /* PVC in use */
1166 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1167 *get_dev_p(pvc, type) = NULL;
1169 if (!pvc_is_used(pvc)) {
1170 state(hdlc)->dce_pvc_count--;
1171 state(hdlc)->dce_changed = 1;
1173 delete_unused_pvcs(hdlc);
1174 return 0;
1179 static void fr_destroy(struct net_device *frad)
1181 hdlc_device *hdlc = dev_to_hdlc(frad);
1182 struct pvc_device *pvc = state(hdlc)->first_pvc;
1183 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1184 state(hdlc)->dce_pvc_count = 0;
1185 state(hdlc)->dce_changed = 1;
1187 while (pvc) {
1188 struct pvc_device *next = pvc->next;
1189 /* destructors will free_netdev() main and ether */
1190 if (pvc->main)
1191 unregister_netdevice(pvc->main);
1193 if (pvc->ether)
1194 unregister_netdevice(pvc->ether);
1196 kfree(pvc);
1197 pvc = next;
1202 static struct hdlc_proto proto = {
1203 .close = fr_close,
1204 .start = fr_start,
1205 .stop = fr_stop,
1206 .detach = fr_destroy,
1207 .ioctl = fr_ioctl,
1208 .netif_rx = fr_rx,
1209 .module = THIS_MODULE,
1213 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1215 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1216 const size_t size = sizeof(fr_proto);
1217 fr_proto new_settings;
1218 hdlc_device *hdlc = dev_to_hdlc(dev);
1219 fr_proto_pvc pvc;
1220 int result;
1222 switch (ifr->ifr_settings.type) {
1223 case IF_GET_PROTO:
1224 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1225 return -EINVAL;
1226 ifr->ifr_settings.type = IF_PROTO_FR;
1227 if (ifr->ifr_settings.size < size) {
1228 ifr->ifr_settings.size = size; /* data size wanted */
1229 return -ENOBUFS;
1231 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1232 return -EFAULT;
1233 return 0;
1235 case IF_PROTO_FR:
1236 if (!capable(CAP_NET_ADMIN))
1237 return -EPERM;
1239 if (dev->flags & IFF_UP)
1240 return -EBUSY;
1242 if (copy_from_user(&new_settings, fr_s, size))
1243 return -EFAULT;
1245 if (new_settings.lmi == LMI_DEFAULT)
1246 new_settings.lmi = LMI_ANSI;
1248 if ((new_settings.lmi != LMI_NONE &&
1249 new_settings.lmi != LMI_ANSI &&
1250 new_settings.lmi != LMI_CCITT &&
1251 new_settings.lmi != LMI_CISCO) ||
1252 new_settings.t391 < 1 ||
1253 new_settings.t392 < 2 ||
1254 new_settings.n391 < 1 ||
1255 new_settings.n392 < 1 ||
1256 new_settings.n393 < new_settings.n392 ||
1257 new_settings.n393 > 32 ||
1258 (new_settings.dce != 0 &&
1259 new_settings.dce != 1))
1260 return -EINVAL;
1262 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1263 if (result)
1264 return result;
1266 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1267 result = attach_hdlc_protocol(dev, &proto,
1268 sizeof(struct frad_state));
1269 if (result)
1270 return result;
1271 state(hdlc)->first_pvc = NULL;
1272 state(hdlc)->dce_pvc_count = 0;
1274 memcpy(&state(hdlc)->settings, &new_settings, size);
1275 dev->type = ARPHRD_FRAD;
1276 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1277 return 0;
1279 case IF_PROTO_FR_ADD_PVC:
1280 case IF_PROTO_FR_DEL_PVC:
1281 case IF_PROTO_FR_ADD_ETH_PVC:
1282 case IF_PROTO_FR_DEL_ETH_PVC:
1283 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1284 return -EINVAL;
1286 if (!capable(CAP_NET_ADMIN))
1287 return -EPERM;
1289 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1290 sizeof(fr_proto_pvc)))
1291 return -EFAULT;
1293 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1294 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1296 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1297 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1298 result = ARPHRD_ETHER; /* bridged Ethernet device */
1299 else
1300 result = ARPHRD_DLCI;
1302 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1303 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1304 return fr_add_pvc(dev, pvc.dlci, result);
1305 else
1306 return fr_del_pvc(hdlc, pvc.dlci, result);
1309 return -EINVAL;
1313 static int __init mod_init(void)
1315 register_hdlc_protocol(&proto);
1316 return 0;
1320 static void __exit mod_exit(void)
1322 unregister_hdlc_protocol(&proto);
1326 module_init(mod_init);
1327 module_exit(mod_exit);
1329 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1330 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1331 MODULE_LICENSE("GPL v2");