cmd64x: don't clear the other channel's interrupt
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / net / wan / hdlc_fr.c
blob071a64cacd5c29cdedfc959c922bea885a7a7cc2
1 /*
2 * Generic HDLC support routines for Linux
3 * Frame Relay support
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.
12 Theory of PVC state
14 DCE mode:
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
23 DTE mode:
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
26 No LMI:
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/random.h>
46 #include <linux/inetdevice.h>
47 #include <linux/lapb.h>
48 #include <linux/rtnetlink.h>
49 #include <linux/etherdevice.h>
50 #include <linux/hdlc.h>
52 #undef DEBUG_PKT
53 #undef DEBUG_ECN
54 #undef DEBUG_LINK
55 #undef DEBUG_PROTO
56 #undef DEBUG_PVC
58 #define FR_UI 0x03
59 #define FR_PAD 0x00
61 #define NLPID_IP 0xCC
62 #define NLPID_IPV6 0x8E
63 #define NLPID_SNAP 0x80
64 #define NLPID_PAD 0x00
65 #define NLPID_CCITT_ANSI_LMI 0x08
66 #define NLPID_CISCO_LMI 0x09
69 #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
70 #define LMI_CISCO_DLCI 1023
72 #define LMI_CALLREF 0x00 /* Call Reference */
73 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
74 #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
75 #define LMI_CCITT_REPTYPE 0x51
76 #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
77 #define LMI_CCITT_ALIVE 0x53
78 #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
79 #define LMI_CCITT_PVCSTAT 0x57
81 #define LMI_FULLREP 0x00 /* full report */
82 #define LMI_INTEGRITY 0x01 /* link integrity report */
83 #define LMI_SINGLE 0x02 /* single PVC report */
85 #define LMI_STATUS_ENQUIRY 0x75
86 #define LMI_STATUS 0x7D /* reply */
88 #define LMI_REPT_LEN 1 /* report type element length */
89 #define LMI_INTEG_LEN 2 /* link integrity element length */
91 #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
92 #define LMI_ANSI_LENGTH 14
95 typedef struct {
96 #if defined(__LITTLE_ENDIAN_BITFIELD)
97 unsigned ea1: 1;
98 unsigned cr: 1;
99 unsigned dlcih: 6;
101 unsigned ea2: 1;
102 unsigned de: 1;
103 unsigned becn: 1;
104 unsigned fecn: 1;
105 unsigned dlcil: 4;
106 #else
107 unsigned dlcih: 6;
108 unsigned cr: 1;
109 unsigned ea1: 1;
111 unsigned dlcil: 4;
112 unsigned fecn: 1;
113 unsigned becn: 1;
114 unsigned de: 1;
115 unsigned ea2: 1;
116 #endif
117 }__attribute__ ((packed)) fr_hdr;
120 typedef struct pvc_device_struct {
121 struct net_device *frad;
122 struct net_device *main;
123 struct net_device *ether; /* bridged Ethernet interface */
124 struct pvc_device_struct *next; /* Sorted in ascending DLCI order */
125 int dlci;
126 int open_count;
128 struct {
129 unsigned int new: 1;
130 unsigned int active: 1;
131 unsigned int exist: 1;
132 unsigned int deleted: 1;
133 unsigned int fecn: 1;
134 unsigned int becn: 1;
135 unsigned int bandwidth; /* Cisco LMI reporting only */
136 }state;
137 }pvc_device;
140 struct frad_state {
141 fr_proto settings;
142 pvc_device *first_pvc;
143 int dce_pvc_count;
145 struct timer_list timer;
146 unsigned long last_poll;
147 int reliable;
148 int dce_changed;
149 int request;
150 int fullrep_sent;
151 u32 last_errors; /* last errors bit list */
152 u8 n391cnt;
153 u8 txseq; /* TX sequence number */
154 u8 rxseq; /* RX sequence number */
158 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr);
161 static inline u16 q922_to_dlci(u8 *hdr)
163 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
167 static inline void dlci_to_q922(u8 *hdr, u16 dlci)
169 hdr[0] = (dlci >> 2) & 0xFC;
170 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
174 static inline struct frad_state * state(hdlc_device *hdlc)
176 return(struct frad_state *)(hdlc->state);
180 static __inline__ pvc_device* dev_to_pvc(struct net_device *dev)
182 return dev->priv;
186 static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci)
188 pvc_device *pvc = state(hdlc)->first_pvc;
190 while (pvc) {
191 if (pvc->dlci == dlci)
192 return pvc;
193 if (pvc->dlci > dlci)
194 return NULL; /* the listed is sorted */
195 pvc = pvc->next;
198 return NULL;
202 static pvc_device* add_pvc(struct net_device *dev, u16 dlci)
204 hdlc_device *hdlc = dev_to_hdlc(dev);
205 pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc;
207 while (*pvc_p) {
208 if ((*pvc_p)->dlci == dlci)
209 return *pvc_p;
210 if ((*pvc_p)->dlci > dlci)
211 break; /* the list is sorted */
212 pvc_p = &(*pvc_p)->next;
215 pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC);
216 #ifdef DEBUG_PVC
217 printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev);
218 #endif
219 if (!pvc)
220 return NULL;
222 pvc->dlci = dlci;
223 pvc->frad = dev;
224 pvc->next = *pvc_p; /* Put it in the chain */
225 *pvc_p = pvc;
226 return pvc;
230 static inline int pvc_is_used(pvc_device *pvc)
232 return pvc->main || pvc->ether;
236 static inline void pvc_carrier(int on, pvc_device *pvc)
238 if (on) {
239 if (pvc->main)
240 if (!netif_carrier_ok(pvc->main))
241 netif_carrier_on(pvc->main);
242 if (pvc->ether)
243 if (!netif_carrier_ok(pvc->ether))
244 netif_carrier_on(pvc->ether);
245 } else {
246 if (pvc->main)
247 if (netif_carrier_ok(pvc->main))
248 netif_carrier_off(pvc->main);
249 if (pvc->ether)
250 if (netif_carrier_ok(pvc->ether))
251 netif_carrier_off(pvc->ether);
256 static inline void delete_unused_pvcs(hdlc_device *hdlc)
258 pvc_device **pvc_p = &state(hdlc)->first_pvc;
260 while (*pvc_p) {
261 if (!pvc_is_used(*pvc_p)) {
262 pvc_device *pvc = *pvc_p;
263 #ifdef DEBUG_PVC
264 printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc);
265 #endif
266 *pvc_p = pvc->next;
267 kfree(pvc);
268 continue;
270 pvc_p = &(*pvc_p)->next;
275 static inline struct net_device** get_dev_p(pvc_device *pvc, int type)
277 if (type == ARPHRD_ETHER)
278 return &pvc->ether;
279 else
280 return &pvc->main;
284 static int fr_hard_header(struct sk_buff **skb_p, u16 dlci)
286 u16 head_len;
287 struct sk_buff *skb = *skb_p;
289 switch (skb->protocol) {
290 case __constant_htons(NLPID_CCITT_ANSI_LMI):
291 head_len = 4;
292 skb_push(skb, head_len);
293 skb->data[3] = NLPID_CCITT_ANSI_LMI;
294 break;
296 case __constant_htons(NLPID_CISCO_LMI):
297 head_len = 4;
298 skb_push(skb, head_len);
299 skb->data[3] = NLPID_CISCO_LMI;
300 break;
302 case __constant_htons(ETH_P_IP):
303 head_len = 4;
304 skb_push(skb, head_len);
305 skb->data[3] = NLPID_IP;
306 break;
308 case __constant_htons(ETH_P_IPV6):
309 head_len = 4;
310 skb_push(skb, head_len);
311 skb->data[3] = NLPID_IPV6;
312 break;
314 case __constant_htons(ETH_P_802_3):
315 head_len = 10;
316 if (skb_headroom(skb) < head_len) {
317 struct sk_buff *skb2 = skb_realloc_headroom(skb,
318 head_len);
319 if (!skb2)
320 return -ENOBUFS;
321 dev_kfree_skb(skb);
322 skb = *skb_p = skb2;
324 skb_push(skb, head_len);
325 skb->data[3] = FR_PAD;
326 skb->data[4] = NLPID_SNAP;
327 skb->data[5] = FR_PAD;
328 skb->data[6] = 0x80;
329 skb->data[7] = 0xC2;
330 skb->data[8] = 0x00;
331 skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */
332 break;
334 default:
335 head_len = 10;
336 skb_push(skb, head_len);
337 skb->data[3] = FR_PAD;
338 skb->data[4] = NLPID_SNAP;
339 skb->data[5] = FR_PAD;
340 skb->data[6] = FR_PAD;
341 skb->data[7] = FR_PAD;
342 *(__be16*)(skb->data + 8) = skb->protocol;
345 dlci_to_q922(skb->data, dlci);
346 skb->data[2] = FR_UI;
347 return 0;
352 static int pvc_open(struct net_device *dev)
354 pvc_device *pvc = dev_to_pvc(dev);
356 if ((pvc->frad->flags & IFF_UP) == 0)
357 return -EIO; /* Frad must be UP in order to activate PVC */
359 if (pvc->open_count++ == 0) {
360 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
361 if (state(hdlc)->settings.lmi == LMI_NONE)
362 pvc->state.active = netif_carrier_ok(pvc->frad);
364 pvc_carrier(pvc->state.active, pvc);
365 state(hdlc)->dce_changed = 1;
367 return 0;
372 static int pvc_close(struct net_device *dev)
374 pvc_device *pvc = dev_to_pvc(dev);
376 if (--pvc->open_count == 0) {
377 hdlc_device *hdlc = dev_to_hdlc(pvc->frad);
378 if (state(hdlc)->settings.lmi == LMI_NONE)
379 pvc->state.active = 0;
381 if (state(hdlc)->settings.dce) {
382 state(hdlc)->dce_changed = 1;
383 pvc->state.active = 0;
386 return 0;
391 static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
393 pvc_device *pvc = dev_to_pvc(dev);
394 fr_proto_pvc_info info;
396 if (ifr->ifr_settings.type == IF_GET_PROTO) {
397 if (dev->type == ARPHRD_ETHER)
398 ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC;
399 else
400 ifr->ifr_settings.type = IF_PROTO_FR_PVC;
402 if (ifr->ifr_settings.size < sizeof(info)) {
403 /* data size wanted */
404 ifr->ifr_settings.size = sizeof(info);
405 return -ENOBUFS;
408 info.dlci = pvc->dlci;
409 memcpy(info.master, pvc->frad->name, IFNAMSIZ);
410 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info,
411 &info, sizeof(info)))
412 return -EFAULT;
413 return 0;
416 return -EINVAL;
420 static inline struct net_device_stats *pvc_get_stats(struct net_device *dev)
422 return &dev_to_desc(dev)->stats;
427 static int pvc_xmit(struct sk_buff *skb, struct net_device *dev)
429 pvc_device *pvc = dev_to_pvc(dev);
430 struct net_device_stats *stats = pvc_get_stats(dev);
432 if (pvc->state.active) {
433 if (dev->type == ARPHRD_ETHER) {
434 int pad = ETH_ZLEN - skb->len;
435 if (pad > 0) { /* Pad the frame with zeros */
436 int len = skb->len;
437 if (skb_tailroom(skb) < pad)
438 if (pskb_expand_head(skb, 0, pad,
439 GFP_ATOMIC)) {
440 stats->tx_dropped++;
441 dev_kfree_skb(skb);
442 return 0;
444 skb_put(skb, pad);
445 memset(skb->data + len, 0, pad);
447 skb->protocol = __constant_htons(ETH_P_802_3);
449 if (!fr_hard_header(&skb, pvc->dlci)) {
450 stats->tx_bytes += skb->len;
451 stats->tx_packets++;
452 if (pvc->state.fecn) /* TX Congestion counter */
453 stats->tx_compressed++;
454 skb->dev = pvc->frad;
455 dev_queue_xmit(skb);
456 return 0;
460 stats->tx_dropped++;
461 dev_kfree_skb(skb);
462 return 0;
467 static int pvc_change_mtu(struct net_device *dev, int new_mtu)
469 if ((new_mtu < 68) || (new_mtu > HDLC_MAX_MTU))
470 return -EINVAL;
471 dev->mtu = new_mtu;
472 return 0;
477 static inline void fr_log_dlci_active(pvc_device *pvc)
479 printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n",
480 pvc->frad->name,
481 pvc->dlci,
482 pvc->main ? pvc->main->name : "",
483 pvc->main && pvc->ether ? " " : "",
484 pvc->ether ? pvc->ether->name : "",
485 pvc->state.new ? " new" : "",
486 !pvc->state.exist ? "deleted" :
487 pvc->state.active ? "active" : "inactive");
492 static inline u8 fr_lmi_nextseq(u8 x)
494 x++;
495 return x ? x : 1;
499 static void fr_lmi_send(struct net_device *dev, int fullrep)
501 hdlc_device *hdlc = dev_to_hdlc(dev);
502 struct sk_buff *skb;
503 pvc_device *pvc = state(hdlc)->first_pvc;
504 int lmi = state(hdlc)->settings.lmi;
505 int dce = state(hdlc)->settings.dce;
506 int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH;
507 int stat_len = (lmi == LMI_CISCO) ? 6 : 3;
508 u8 *data;
509 int i = 0;
511 if (dce && fullrep) {
512 len += state(hdlc)->dce_pvc_count * (2 + stat_len);
513 if (len > HDLC_MAX_MRU) {
514 printk(KERN_WARNING "%s: Too many PVCs while sending "
515 "LMI full report\n", dev->name);
516 return;
520 skb = dev_alloc_skb(len);
521 if (!skb) {
522 printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n",
523 dev->name);
524 return;
526 memset(skb->data, 0, len);
527 skb_reserve(skb, 4);
528 if (lmi == LMI_CISCO) {
529 skb->protocol = __constant_htons(NLPID_CISCO_LMI);
530 fr_hard_header(&skb, LMI_CISCO_DLCI);
531 } else {
532 skb->protocol = __constant_htons(NLPID_CCITT_ANSI_LMI);
533 fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI);
535 data = skb_tail_pointer(skb);
536 data[i++] = LMI_CALLREF;
537 data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY;
538 if (lmi == LMI_ANSI)
539 data[i++] = LMI_ANSI_LOCKSHIFT;
540 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
541 LMI_ANSI_CISCO_REPTYPE;
542 data[i++] = LMI_REPT_LEN;
543 data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY;
544 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE;
545 data[i++] = LMI_INTEG_LEN;
546 data[i++] = state(hdlc)->txseq =
547 fr_lmi_nextseq(state(hdlc)->txseq);
548 data[i++] = state(hdlc)->rxseq;
550 if (dce && fullrep) {
551 while (pvc) {
552 data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
553 LMI_ANSI_CISCO_PVCSTAT;
554 data[i++] = stat_len;
556 /* LMI start/restart */
557 if (state(hdlc)->reliable && !pvc->state.exist) {
558 pvc->state.exist = pvc->state.new = 1;
559 fr_log_dlci_active(pvc);
562 /* ifconfig PVC up */
563 if (pvc->open_count && !pvc->state.active &&
564 pvc->state.exist && !pvc->state.new) {
565 pvc_carrier(1, pvc);
566 pvc->state.active = 1;
567 fr_log_dlci_active(pvc);
570 if (lmi == LMI_CISCO) {
571 data[i] = pvc->dlci >> 8;
572 data[i + 1] = pvc->dlci & 0xFF;
573 } else {
574 data[i] = (pvc->dlci >> 4) & 0x3F;
575 data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80;
576 data[i + 2] = 0x80;
579 if (pvc->state.new)
580 data[i + 2] |= 0x08;
581 else if (pvc->state.active)
582 data[i + 2] |= 0x02;
584 i += stat_len;
585 pvc = pvc->next;
589 skb_put(skb, i);
590 skb->priority = TC_PRIO_CONTROL;
591 skb->dev = dev;
592 skb_reset_network_header(skb);
594 dev_queue_xmit(skb);
599 static void fr_set_link_state(int reliable, struct net_device *dev)
601 hdlc_device *hdlc = dev_to_hdlc(dev);
602 pvc_device *pvc = state(hdlc)->first_pvc;
604 state(hdlc)->reliable = reliable;
605 if (reliable) {
606 netif_dormant_off(dev);
607 state(hdlc)->n391cnt = 0; /* Request full status */
608 state(hdlc)->dce_changed = 1;
610 if (state(hdlc)->settings.lmi == LMI_NONE) {
611 while (pvc) { /* Activate all PVCs */
612 pvc_carrier(1, pvc);
613 pvc->state.exist = pvc->state.active = 1;
614 pvc->state.new = 0;
615 pvc = pvc->next;
618 } else {
619 netif_dormant_on(dev);
620 while (pvc) { /* Deactivate all PVCs */
621 pvc_carrier(0, pvc);
622 pvc->state.exist = pvc->state.active = 0;
623 pvc->state.new = 0;
624 if (!state(hdlc)->settings.dce)
625 pvc->state.bandwidth = 0;
626 pvc = pvc->next;
632 static void fr_timer(unsigned long arg)
634 struct net_device *dev = (struct net_device *)arg;
635 hdlc_device *hdlc = dev_to_hdlc(dev);
636 int i, cnt = 0, reliable;
637 u32 list;
639 if (state(hdlc)->settings.dce) {
640 reliable = state(hdlc)->request &&
641 time_before(jiffies, state(hdlc)->last_poll +
642 state(hdlc)->settings.t392 * HZ);
643 state(hdlc)->request = 0;
644 } else {
645 state(hdlc)->last_errors <<= 1; /* Shift the list */
646 if (state(hdlc)->request) {
647 if (state(hdlc)->reliable)
648 printk(KERN_INFO "%s: No LMI status reply "
649 "received\n", dev->name);
650 state(hdlc)->last_errors |= 1;
653 list = state(hdlc)->last_errors;
654 for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1)
655 cnt += (list & 1); /* errors count */
657 reliable = (cnt < state(hdlc)->settings.n392);
660 if (state(hdlc)->reliable != reliable) {
661 printk(KERN_INFO "%s: Link %sreliable\n", dev->name,
662 reliable ? "" : "un");
663 fr_set_link_state(reliable, dev);
666 if (state(hdlc)->settings.dce)
667 state(hdlc)->timer.expires = jiffies +
668 state(hdlc)->settings.t392 * HZ;
669 else {
670 if (state(hdlc)->n391cnt)
671 state(hdlc)->n391cnt--;
673 fr_lmi_send(dev, state(hdlc)->n391cnt == 0);
675 state(hdlc)->last_poll = jiffies;
676 state(hdlc)->request = 1;
677 state(hdlc)->timer.expires = jiffies +
678 state(hdlc)->settings.t391 * HZ;
681 state(hdlc)->timer.function = fr_timer;
682 state(hdlc)->timer.data = arg;
683 add_timer(&state(hdlc)->timer);
687 static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb)
689 hdlc_device *hdlc = dev_to_hdlc(dev);
690 pvc_device *pvc;
691 u8 rxseq, txseq;
692 int lmi = state(hdlc)->settings.lmi;
693 int dce = state(hdlc)->settings.dce;
694 int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i;
696 if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH :
697 LMI_CCITT_CISCO_LENGTH)) {
698 printk(KERN_INFO "%s: Short LMI frame\n", dev->name);
699 return 1;
702 if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI :
703 NLPID_CCITT_ANSI_LMI)) {
704 printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n",
705 dev->name);
706 return 1;
709 if (skb->data[4] != LMI_CALLREF) {
710 printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n",
711 dev->name, skb->data[4]);
712 return 1;
715 if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) {
716 printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n",
717 dev->name, skb->data[5]);
718 return 1;
721 if (lmi == LMI_ANSI) {
722 if (skb->data[6] != LMI_ANSI_LOCKSHIFT) {
723 printk(KERN_INFO "%s: Not ANSI locking shift in LMI"
724 " message (0x%02X)\n", dev->name, skb->data[6]);
725 return 1;
727 i = 7;
728 } else
729 i = 6;
731 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE :
732 LMI_ANSI_CISCO_REPTYPE)) {
733 printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n",
734 dev->name, skb->data[i]);
735 return 1;
738 if (skb->data[++i] != LMI_REPT_LEN) {
739 printk(KERN_INFO "%s: Invalid LMI Report type IE length"
740 " (%u)\n", dev->name, skb->data[i]);
741 return 1;
744 reptype = skb->data[++i];
745 if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) {
746 printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n",
747 dev->name, reptype);
748 return 1;
751 if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE :
752 LMI_ANSI_CISCO_ALIVE)) {
753 printk(KERN_INFO "%s: Not an LMI Link integrity verification"
754 " IE (0x%02X)\n", dev->name, skb->data[i]);
755 return 1;
758 if (skb->data[++i] != LMI_INTEG_LEN) {
759 printk(KERN_INFO "%s: Invalid LMI Link integrity verification"
760 " IE length (%u)\n", dev->name, skb->data[i]);
761 return 1;
763 i++;
765 state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */
766 rxseq = skb->data[i++]; /* Should confirm our sequence */
768 txseq = state(hdlc)->txseq;
770 if (dce)
771 state(hdlc)->last_poll = jiffies;
773 error = 0;
774 if (!state(hdlc)->reliable)
775 error = 1;
777 if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */
778 state(hdlc)->n391cnt = 0;
779 error = 1;
782 if (dce) {
783 if (state(hdlc)->fullrep_sent && !error) {
784 /* Stop sending full report - the last one has been confirmed by DTE */
785 state(hdlc)->fullrep_sent = 0;
786 pvc = state(hdlc)->first_pvc;
787 while (pvc) {
788 if (pvc->state.new) {
789 pvc->state.new = 0;
791 /* Tell DTE that new PVC is now active */
792 state(hdlc)->dce_changed = 1;
794 pvc = pvc->next;
798 if (state(hdlc)->dce_changed) {
799 reptype = LMI_FULLREP;
800 state(hdlc)->fullrep_sent = 1;
801 state(hdlc)->dce_changed = 0;
804 state(hdlc)->request = 1; /* got request */
805 fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0);
806 return 0;
809 /* DTE */
811 state(hdlc)->request = 0; /* got response, no request pending */
813 if (error)
814 return 0;
816 if (reptype != LMI_FULLREP)
817 return 0;
819 pvc = state(hdlc)->first_pvc;
821 while (pvc) {
822 pvc->state.deleted = 1;
823 pvc = pvc->next;
826 no_ram = 0;
827 while (skb->len >= i + 2 + stat_len) {
828 u16 dlci;
829 u32 bw;
830 unsigned int active, new;
832 if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT :
833 LMI_ANSI_CISCO_PVCSTAT)) {
834 printk(KERN_INFO "%s: Not an LMI PVC status IE"
835 " (0x%02X)\n", dev->name, skb->data[i]);
836 return 1;
839 if (skb->data[++i] != stat_len) {
840 printk(KERN_INFO "%s: Invalid LMI PVC status IE length"
841 " (%u)\n", dev->name, skb->data[i]);
842 return 1;
844 i++;
846 new = !! (skb->data[i + 2] & 0x08);
847 active = !! (skb->data[i + 2] & 0x02);
848 if (lmi == LMI_CISCO) {
849 dlci = (skb->data[i] << 8) | skb->data[i + 1];
850 bw = (skb->data[i + 3] << 16) |
851 (skb->data[i + 4] << 8) |
852 (skb->data[i + 5]);
853 } else {
854 dlci = ((skb->data[i] & 0x3F) << 4) |
855 ((skb->data[i + 1] & 0x78) >> 3);
856 bw = 0;
859 pvc = add_pvc(dev, dlci);
861 if (!pvc && !no_ram) {
862 printk(KERN_WARNING
863 "%s: Memory squeeze on fr_lmi_recv()\n",
864 dev->name);
865 no_ram = 1;
868 if (pvc) {
869 pvc->state.exist = 1;
870 pvc->state.deleted = 0;
871 if (active != pvc->state.active ||
872 new != pvc->state.new ||
873 bw != pvc->state.bandwidth ||
874 !pvc->state.exist) {
875 pvc->state.new = new;
876 pvc->state.active = active;
877 pvc->state.bandwidth = bw;
878 pvc_carrier(active, pvc);
879 fr_log_dlci_active(pvc);
883 i += stat_len;
886 pvc = state(hdlc)->first_pvc;
888 while (pvc) {
889 if (pvc->state.deleted && pvc->state.exist) {
890 pvc_carrier(0, pvc);
891 pvc->state.active = pvc->state.new = 0;
892 pvc->state.exist = 0;
893 pvc->state.bandwidth = 0;
894 fr_log_dlci_active(pvc);
896 pvc = pvc->next;
899 /* Next full report after N391 polls */
900 state(hdlc)->n391cnt = state(hdlc)->settings.n391;
902 return 0;
906 static int fr_rx(struct sk_buff *skb)
908 struct net_device *frad = skb->dev;
909 hdlc_device *hdlc = dev_to_hdlc(frad);
910 fr_hdr *fh = (fr_hdr*)skb->data;
911 u8 *data = skb->data;
912 u16 dlci;
913 pvc_device *pvc;
914 struct net_device *dev = NULL;
916 if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI)
917 goto rx_error;
919 dlci = q922_to_dlci(skb->data);
921 if ((dlci == LMI_CCITT_ANSI_DLCI &&
922 (state(hdlc)->settings.lmi == LMI_ANSI ||
923 state(hdlc)->settings.lmi == LMI_CCITT)) ||
924 (dlci == LMI_CISCO_DLCI &&
925 state(hdlc)->settings.lmi == LMI_CISCO)) {
926 if (fr_lmi_recv(frad, skb))
927 goto rx_error;
928 dev_kfree_skb_any(skb);
929 return NET_RX_SUCCESS;
932 pvc = find_pvc(hdlc, dlci);
933 if (!pvc) {
934 #ifdef DEBUG_PKT
935 printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n",
936 frad->name, dlci);
937 #endif
938 dev_kfree_skb_any(skb);
939 return NET_RX_DROP;
942 if (pvc->state.fecn != fh->fecn) {
943 #ifdef DEBUG_ECN
944 printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name,
945 dlci, fh->fecn ? "N" : "FF");
946 #endif
947 pvc->state.fecn ^= 1;
950 if (pvc->state.becn != fh->becn) {
951 #ifdef DEBUG_ECN
952 printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name,
953 dlci, fh->becn ? "N" : "FF");
954 #endif
955 pvc->state.becn ^= 1;
959 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
960 dev_to_desc(frad)->stats.rx_dropped++;
961 return NET_RX_DROP;
964 if (data[3] == NLPID_IP) {
965 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
966 dev = pvc->main;
967 skb->protocol = htons(ETH_P_IP);
969 } else if (data[3] == NLPID_IPV6) {
970 skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
971 dev = pvc->main;
972 skb->protocol = htons(ETH_P_IPV6);
974 } else if (skb->len > 10 && data[3] == FR_PAD &&
975 data[4] == NLPID_SNAP && data[5] == FR_PAD) {
976 u16 oui = ntohs(*(__be16*)(data + 6));
977 u16 pid = ntohs(*(__be16*)(data + 8));
978 skb_pull(skb, 10);
980 switch ((((u32)oui) << 16) | pid) {
981 case ETH_P_ARP: /* routed frame with SNAP */
982 case ETH_P_IPX:
983 case ETH_P_IP: /* a long variant */
984 case ETH_P_IPV6:
985 dev = pvc->main;
986 skb->protocol = htons(pid);
987 break;
989 case 0x80C20007: /* bridged Ethernet frame */
990 if ((dev = pvc->ether) != NULL)
991 skb->protocol = eth_type_trans(skb, dev);
992 break;
994 default:
995 printk(KERN_INFO "%s: Unsupported protocol, OUI=%x "
996 "PID=%x\n", frad->name, oui, pid);
997 dev_kfree_skb_any(skb);
998 return NET_RX_DROP;
1000 } else {
1001 printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x "
1002 "length = %i\n", frad->name, data[3], skb->len);
1003 dev_kfree_skb_any(skb);
1004 return NET_RX_DROP;
1007 if (dev) {
1008 struct net_device_stats *stats = pvc_get_stats(dev);
1009 stats->rx_packets++; /* PVC traffic */
1010 stats->rx_bytes += skb->len;
1011 if (pvc->state.becn)
1012 stats->rx_compressed++;
1013 netif_rx(skb);
1014 return NET_RX_SUCCESS;
1015 } else {
1016 dev_kfree_skb_any(skb);
1017 return NET_RX_DROP;
1020 rx_error:
1021 dev_to_desc(frad)->stats.rx_errors++; /* Mark error */
1022 dev_kfree_skb_any(skb);
1023 return NET_RX_DROP;
1028 static void fr_start(struct net_device *dev)
1030 hdlc_device *hdlc = dev_to_hdlc(dev);
1031 #ifdef DEBUG_LINK
1032 printk(KERN_DEBUG "fr_start\n");
1033 #endif
1034 if (state(hdlc)->settings.lmi != LMI_NONE) {
1035 state(hdlc)->reliable = 0;
1036 state(hdlc)->dce_changed = 1;
1037 state(hdlc)->request = 0;
1038 state(hdlc)->fullrep_sent = 0;
1039 state(hdlc)->last_errors = 0xFFFFFFFF;
1040 state(hdlc)->n391cnt = 0;
1041 state(hdlc)->txseq = state(hdlc)->rxseq = 0;
1043 init_timer(&state(hdlc)->timer);
1044 /* First poll after 1 s */
1045 state(hdlc)->timer.expires = jiffies + HZ;
1046 state(hdlc)->timer.function = fr_timer;
1047 state(hdlc)->timer.data = (unsigned long)dev;
1048 add_timer(&state(hdlc)->timer);
1049 } else
1050 fr_set_link_state(1, dev);
1054 static void fr_stop(struct net_device *dev)
1056 hdlc_device *hdlc = dev_to_hdlc(dev);
1057 #ifdef DEBUG_LINK
1058 printk(KERN_DEBUG "fr_stop\n");
1059 #endif
1060 if (state(hdlc)->settings.lmi != LMI_NONE)
1061 del_timer_sync(&state(hdlc)->timer);
1062 fr_set_link_state(0, dev);
1066 static void fr_close(struct net_device *dev)
1068 hdlc_device *hdlc = dev_to_hdlc(dev);
1069 pvc_device *pvc = state(hdlc)->first_pvc;
1071 while (pvc) { /* Shutdown all PVCs for this FRAD */
1072 if (pvc->main)
1073 dev_close(pvc->main);
1074 if (pvc->ether)
1075 dev_close(pvc->ether);
1076 pvc = pvc->next;
1081 static void pvc_setup(struct net_device *dev)
1083 dev->type = ARPHRD_DLCI;
1084 dev->flags = IFF_POINTOPOINT;
1085 dev->hard_header_len = 10;
1086 dev->addr_len = 2;
1089 static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type)
1091 hdlc_device *hdlc = dev_to_hdlc(frad);
1092 pvc_device *pvc = NULL;
1093 struct net_device *dev;
1094 int result, used;
1095 char * prefix = "pvc%d";
1097 if (type == ARPHRD_ETHER)
1098 prefix = "pvceth%d";
1100 if ((pvc = add_pvc(frad, dlci)) == NULL) {
1101 printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n",
1102 frad->name);
1103 return -ENOBUFS;
1106 if (*get_dev_p(pvc, type))
1107 return -EEXIST;
1109 used = pvc_is_used(pvc);
1111 if (type == ARPHRD_ETHER)
1112 dev = alloc_netdev(sizeof(struct net_device_stats),
1113 "pvceth%d", ether_setup);
1114 else
1115 dev = alloc_netdev(sizeof(struct net_device_stats),
1116 "pvc%d", pvc_setup);
1118 if (!dev) {
1119 printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n",
1120 frad->name);
1121 delete_unused_pvcs(hdlc);
1122 return -ENOBUFS;
1125 if (type == ARPHRD_ETHER) {
1126 memcpy(dev->dev_addr, "\x00\x01", 2);
1127 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
1128 } else {
1129 *(__be16*)dev->dev_addr = htons(dlci);
1130 dlci_to_q922(dev->broadcast, dlci);
1132 dev->hard_start_xmit = pvc_xmit;
1133 dev->get_stats = pvc_get_stats;
1134 dev->open = pvc_open;
1135 dev->stop = pvc_close;
1136 dev->do_ioctl = pvc_ioctl;
1137 dev->change_mtu = pvc_change_mtu;
1138 dev->mtu = HDLC_MAX_MTU;
1139 dev->tx_queue_len = 0;
1140 dev->priv = pvc;
1142 result = dev_alloc_name(dev, dev->name);
1143 if (result < 0) {
1144 free_netdev(dev);
1145 delete_unused_pvcs(hdlc);
1146 return result;
1149 if (register_netdevice(dev) != 0) {
1150 free_netdev(dev);
1151 delete_unused_pvcs(hdlc);
1152 return -EIO;
1155 dev->destructor = free_netdev;
1156 *get_dev_p(pvc, type) = dev;
1157 if (!used) {
1158 state(hdlc)->dce_changed = 1;
1159 state(hdlc)->dce_pvc_count++;
1161 return 0;
1166 static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type)
1168 pvc_device *pvc;
1169 struct net_device *dev;
1171 if ((pvc = find_pvc(hdlc, dlci)) == NULL)
1172 return -ENOENT;
1174 if ((dev = *get_dev_p(pvc, type)) == NULL)
1175 return -ENOENT;
1177 if (dev->flags & IFF_UP)
1178 return -EBUSY; /* PVC in use */
1180 unregister_netdevice(dev); /* the destructor will free_netdev(dev) */
1181 *get_dev_p(pvc, type) = NULL;
1183 if (!pvc_is_used(pvc)) {
1184 state(hdlc)->dce_pvc_count--;
1185 state(hdlc)->dce_changed = 1;
1187 delete_unused_pvcs(hdlc);
1188 return 0;
1193 static void fr_destroy(struct net_device *frad)
1195 hdlc_device *hdlc = dev_to_hdlc(frad);
1196 pvc_device *pvc = state(hdlc)->first_pvc;
1197 state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */
1198 state(hdlc)->dce_pvc_count = 0;
1199 state(hdlc)->dce_changed = 1;
1201 while (pvc) {
1202 pvc_device *next = pvc->next;
1203 /* destructors will free_netdev() main and ether */
1204 if (pvc->main)
1205 unregister_netdevice(pvc->main);
1207 if (pvc->ether)
1208 unregister_netdevice(pvc->ether);
1210 kfree(pvc);
1211 pvc = next;
1216 static struct hdlc_proto proto = {
1217 .close = fr_close,
1218 .start = fr_start,
1219 .stop = fr_stop,
1220 .detach = fr_destroy,
1221 .ioctl = fr_ioctl,
1222 .module = THIS_MODULE,
1226 static int fr_ioctl(struct net_device *dev, struct ifreq *ifr)
1228 fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr;
1229 const size_t size = sizeof(fr_proto);
1230 fr_proto new_settings;
1231 hdlc_device *hdlc = dev_to_hdlc(dev);
1232 fr_proto_pvc pvc;
1233 int result;
1235 switch (ifr->ifr_settings.type) {
1236 case IF_GET_PROTO:
1237 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1238 return -EINVAL;
1239 ifr->ifr_settings.type = IF_PROTO_FR;
1240 if (ifr->ifr_settings.size < size) {
1241 ifr->ifr_settings.size = size; /* data size wanted */
1242 return -ENOBUFS;
1244 if (copy_to_user(fr_s, &state(hdlc)->settings, size))
1245 return -EFAULT;
1246 return 0;
1248 case IF_PROTO_FR:
1249 if(!capable(CAP_NET_ADMIN))
1250 return -EPERM;
1252 if(dev->flags & IFF_UP)
1253 return -EBUSY;
1255 if (copy_from_user(&new_settings, fr_s, size))
1256 return -EFAULT;
1258 if (new_settings.lmi == LMI_DEFAULT)
1259 new_settings.lmi = LMI_ANSI;
1261 if ((new_settings.lmi != LMI_NONE &&
1262 new_settings.lmi != LMI_ANSI &&
1263 new_settings.lmi != LMI_CCITT &&
1264 new_settings.lmi != LMI_CISCO) ||
1265 new_settings.t391 < 1 ||
1266 new_settings.t392 < 2 ||
1267 new_settings.n391 < 1 ||
1268 new_settings.n392 < 1 ||
1269 new_settings.n393 < new_settings.n392 ||
1270 new_settings.n393 > 32 ||
1271 (new_settings.dce != 0 &&
1272 new_settings.dce != 1))
1273 return -EINVAL;
1275 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
1276 if (result)
1277 return result;
1279 if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */
1280 result = attach_hdlc_protocol(dev, &proto, fr_rx,
1281 sizeof(struct frad_state));
1282 if (result)
1283 return result;
1284 state(hdlc)->first_pvc = NULL;
1285 state(hdlc)->dce_pvc_count = 0;
1287 memcpy(&state(hdlc)->settings, &new_settings, size);
1289 dev->hard_start_xmit = hdlc->xmit;
1290 dev->type = ARPHRD_FRAD;
1291 return 0;
1293 case IF_PROTO_FR_ADD_PVC:
1294 case IF_PROTO_FR_DEL_PVC:
1295 case IF_PROTO_FR_ADD_ETH_PVC:
1296 case IF_PROTO_FR_DEL_ETH_PVC:
1297 if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */
1298 return -EINVAL;
1300 if(!capable(CAP_NET_ADMIN))
1301 return -EPERM;
1303 if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc,
1304 sizeof(fr_proto_pvc)))
1305 return -EFAULT;
1307 if (pvc.dlci <= 0 || pvc.dlci >= 1024)
1308 return -EINVAL; /* Only 10 bits, DLCI 0 reserved */
1310 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC ||
1311 ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC)
1312 result = ARPHRD_ETHER; /* bridged Ethernet device */
1313 else
1314 result = ARPHRD_DLCI;
1316 if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC ||
1317 ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC)
1318 return fr_add_pvc(dev, pvc.dlci, result);
1319 else
1320 return fr_del_pvc(hdlc, pvc.dlci, result);
1323 return -EINVAL;
1327 static int __init mod_init(void)
1329 register_hdlc_protocol(&proto);
1330 return 0;
1334 static void __exit mod_exit(void)
1336 unregister_hdlc_protocol(&proto);
1340 module_init(mod_init);
1341 module_exit(mod_exit);
1343 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1344 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1345 MODULE_LICENSE("GPL v2");