2 * Generic HDLC support routines for Linux
5 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
16 (exist,new) -> 0,0 when "PVC create" or if "link unreliable"
17 0,x -> 1,1 if "link reliable" when sending FULL STATUS
18 1,1 -> 1,0 if received FULL STATUS ACK
20 (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create"
21 -> 1 when "PVC up" and (exist,new) = 1,0
24 (exist,new,active) = FULL STATUS if "link reliable"
25 = 0, 0, 0 if "link unreliable"
27 active = open and "link reliable"
28 exist = new = not used
30 CCITT LMI: ITU-T Q.933 Annex A
31 ANSI LMI: ANSI T1.617 Annex D
32 CISCO LMI: the original, aka "Gang of Four" LMI
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/slab.h>
39 #include <linux/poll.h>
40 #include <linux/errno.h>
41 #include <linux/if_arp.h>
42 #include <linux/init.h>
43 #include <linux/skbuff.h>
44 #include <linux/pkt_sched.h>
45 #include <linux/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>
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
96 #if defined(__LITTLE_ENDIAN_BITFIELD)
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 */
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 */
142 pvc_device
*first_pvc
;
145 struct timer_list timer
;
146 unsigned long last_poll
;
151 u32 last_errors
; /* last errors bit list */
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
)
186 static inline pvc_device
* find_pvc(hdlc_device
*hdlc
, u16 dlci
)
188 pvc_device
*pvc
= state(hdlc
)->first_pvc
;
191 if (pvc
->dlci
== dlci
)
193 if (pvc
->dlci
> dlci
)
194 return NULL
; /* the listed is sorted */
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
;
208 if ((*pvc_p
)->dlci
== dlci
)
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
);
217 printk(KERN_DEBUG
"add_pvc: allocated pvc %p, frad %p\n", pvc
, dev
);
224 pvc
->next
= *pvc_p
; /* Put it in the chain */
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
)
240 if (!netif_carrier_ok(pvc
->main
))
241 netif_carrier_on(pvc
->main
);
243 if (!netif_carrier_ok(pvc
->ether
))
244 netif_carrier_on(pvc
->ether
);
247 if (netif_carrier_ok(pvc
->main
))
248 netif_carrier_off(pvc
->main
);
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
;
261 if (!pvc_is_used(*pvc_p
)) {
262 pvc_device
*pvc
= *pvc_p
;
264 printk(KERN_DEBUG
"freeing unused pvc: %p\n", pvc
);
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
)
284 static int fr_hard_header(struct sk_buff
**skb_p
, u16 dlci
)
287 struct sk_buff
*skb
= *skb_p
;
289 switch (skb
->protocol
) {
290 case __constant_htons(NLPID_CCITT_ANSI_LMI
):
292 skb_push(skb
, head_len
);
293 skb
->data
[3] = NLPID_CCITT_ANSI_LMI
;
296 case __constant_htons(NLPID_CISCO_LMI
):
298 skb_push(skb
, head_len
);
299 skb
->data
[3] = NLPID_CISCO_LMI
;
302 case __constant_htons(ETH_P_IP
):
304 skb_push(skb
, head_len
);
305 skb
->data
[3] = NLPID_IP
;
308 case __constant_htons(ETH_P_IPV6
):
310 skb_push(skb
, head_len
);
311 skb
->data
[3] = NLPID_IPV6
;
314 case __constant_htons(ETH_P_802_3
):
316 if (skb_headroom(skb
) < head_len
) {
317 struct sk_buff
*skb2
= skb_realloc_headroom(skb
,
324 skb_push(skb
, head_len
);
325 skb
->data
[3] = FR_PAD
;
326 skb
->data
[4] = NLPID_SNAP
;
327 skb
->data
[5] = FR_PAD
;
331 skb
->data
[9] = 0x07; /* bridged Ethernet frame w/out FCS */
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
;
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;
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;
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
;
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
);
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
)))
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 */
437 if (skb_tailroom(skb
) < pad
)
438 if (pskb_expand_head(skb
, 0, 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
;
452 if (pvc
->state
.fecn
) /* TX Congestion counter */
453 stats
->tx_compressed
++;
454 skb
->dev
= pvc
->frad
;
467 static int pvc_change_mtu(struct net_device
*dev
, int new_mtu
)
469 if ((new_mtu
< 68) || (new_mtu
> HDLC_MAX_MTU
))
477 static inline void fr_log_dlci_active(pvc_device
*pvc
)
479 printk(KERN_INFO
"%s: DLCI %d [%s%s%s]%s %s\n",
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
)
499 static void fr_lmi_send(struct net_device
*dev
, int fullrep
)
501 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
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;
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
);
520 skb
= dev_alloc_skb(len
);
522 printk(KERN_WARNING
"%s: Memory squeeze on fr_lmi_send()\n",
526 memset(skb
->data
, 0, len
);
528 if (lmi
== LMI_CISCO
) {
529 skb
->protocol
= __constant_htons(NLPID_CISCO_LMI
);
530 fr_hard_header(&skb
, LMI_CISCO_DLCI
);
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
;
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
) {
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) {
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;
574 data
[i
] = (pvc
->dlci
>> 4) & 0x3F;
575 data
[i
+ 1] = ((pvc
->dlci
<< 3) & 0x78) | 0x80;
581 else if (pvc
->state
.active
)
590 skb
->priority
= TC_PRIO_CONTROL
;
592 skb_reset_network_header(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
;
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 */
613 pvc
->state
.exist
= pvc
->state
.active
= 1;
619 netif_dormant_on(dev
);
620 while (pvc
) { /* Deactivate all PVCs */
622 pvc
->state
.exist
= pvc
->state
.active
= 0;
624 if (!state(hdlc
)->settings
.dce
)
625 pvc
->state
.bandwidth
= 0;
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
;
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;
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
;
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
);
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
);
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",
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]);
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]);
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]);
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
]);
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
]);
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",
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
]);
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
]);
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
;
771 state(hdlc
)->last_poll
= jiffies
;
774 if (!state(hdlc
)->reliable
)
777 if (rxseq
== 0 || rxseq
!= txseq
) { /* Ask for full report next time */
778 state(hdlc
)->n391cnt
= 0;
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
;
788 if (pvc
->state
.new) {
791 /* Tell DTE that new PVC is now active */
792 state(hdlc
)->dce_changed
= 1;
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);
811 state(hdlc
)->request
= 0; /* got response, no request pending */
816 if (reptype
!= LMI_FULLREP
)
819 pvc
= state(hdlc
)->first_pvc
;
822 pvc
->state
.deleted
= 1;
827 while (skb
->len
>= i
+ 2 + stat_len
) {
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
]);
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
]);
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) |
854 dlci
= ((skb
->data
[i
] & 0x3F) << 4) |
855 ((skb
->data
[i
+ 1] & 0x78) >> 3);
859 pvc
= add_pvc(dev
, dlci
);
861 if (!pvc
&& !no_ram
) {
863 "%s: Memory squeeze on fr_lmi_recv()\n",
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
||
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
);
886 pvc
= state(hdlc
)->first_pvc
;
889 if (pvc
->state
.deleted
&& pvc
->state
.exist
) {
891 pvc
->state
.active
= pvc
->state
.new = 0;
892 pvc
->state
.exist
= 0;
893 pvc
->state
.bandwidth
= 0;
894 fr_log_dlci_active(pvc
);
899 /* Next full report after N391 polls */
900 state(hdlc
)->n391cnt
= state(hdlc
)->settings
.n391
;
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
;
914 struct net_device
*dev
= NULL
;
916 if (skb
->len
<= 4 || fh
->ea1
|| data
[2] != FR_UI
)
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
))
928 dev_kfree_skb_any(skb
);
929 return NET_RX_SUCCESS
;
932 pvc
= find_pvc(hdlc
, dlci
);
935 printk(KERN_INFO
"%s: No PVC for received frame's DLCI %d\n",
938 dev_kfree_skb_any(skb
);
942 if (pvc
->state
.fecn
!= fh
->fecn
) {
944 printk(KERN_DEBUG
"%s: DLCI %d FECN O%s\n", frad
->name
,
945 dlci
, fh
->fecn
? "N" : "FF");
947 pvc
->state
.fecn
^= 1;
950 if (pvc
->state
.becn
!= fh
->becn
) {
952 printk(KERN_DEBUG
"%s: DLCI %d BECN O%s\n", frad
->name
,
953 dlci
, fh
->becn
? "N" : "FF");
955 pvc
->state
.becn
^= 1;
959 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
960 dev_to_desc(frad
)->stats
.rx_dropped
++;
964 if (data
[3] == NLPID_IP
) {
965 skb_pull(skb
, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
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) */
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));
980 switch ((((u32
)oui
) << 16) | pid
) {
981 case ETH_P_ARP
: /* routed frame with SNAP */
983 case ETH_P_IP
: /* a long variant */
986 skb
->protocol
= htons(pid
);
989 case 0x80C20007: /* bridged Ethernet frame */
990 if ((dev
= pvc
->ether
) != NULL
)
991 skb
->protocol
= eth_type_trans(skb
, dev
);
995 printk(KERN_INFO
"%s: Unsupported protocol, OUI=%x "
996 "PID=%x\n", frad
->name
, oui
, pid
);
997 dev_kfree_skb_any(skb
);
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
);
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
++;
1014 return NET_RX_SUCCESS
;
1016 dev_kfree_skb_any(skb
);
1021 dev_to_desc(frad
)->stats
.rx_errors
++; /* Mark error */
1022 dev_kfree_skb_any(skb
);
1028 static void fr_start(struct net_device
*dev
)
1030 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1032 printk(KERN_DEBUG
"fr_start\n");
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
);
1050 fr_set_link_state(1, dev
);
1054 static void fr_stop(struct net_device
*dev
)
1056 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1058 printk(KERN_DEBUG
"fr_stop\n");
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 */
1073 dev_close(pvc
->main
);
1075 dev_close(pvc
->ether
);
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;
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
;
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",
1106 if (*get_dev_p(pvc
, type
))
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
);
1115 dev
= alloc_netdev(sizeof(struct net_device_stats
),
1116 "pvc%d", pvc_setup
);
1119 printk(KERN_WARNING
"%s: Memory squeeze on fr_pvc()\n",
1121 delete_unused_pvcs(hdlc
);
1125 if (type
== ARPHRD_ETHER
) {
1126 memcpy(dev
->dev_addr
, "\x00\x01", 2);
1127 get_random_bytes(dev
->dev_addr
+ 2, ETH_ALEN
- 2);
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;
1142 result
= dev_alloc_name(dev
, dev
->name
);
1145 delete_unused_pvcs(hdlc
);
1149 if (register_netdevice(dev
) != 0) {
1151 delete_unused_pvcs(hdlc
);
1155 dev
->destructor
= free_netdev
;
1156 *get_dev_p(pvc
, type
) = dev
;
1158 state(hdlc
)->dce_changed
= 1;
1159 state(hdlc
)->dce_pvc_count
++;
1166 static int fr_del_pvc(hdlc_device
*hdlc
, unsigned int dlci
, int type
)
1169 struct net_device
*dev
;
1171 if ((pvc
= find_pvc(hdlc
, dlci
)) == NULL
)
1174 if ((dev
= *get_dev_p(pvc
, type
)) == NULL
)
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
);
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;
1202 pvc_device
*next
= pvc
->next
;
1203 /* destructors will free_netdev() main and ether */
1205 unregister_netdevice(pvc
->main
);
1208 unregister_netdevice(pvc
->ether
);
1216 static struct hdlc_proto proto
= {
1220 .detach
= fr_destroy
,
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
);
1235 switch (ifr
->ifr_settings
.type
) {
1237 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1239 ifr
->ifr_settings
.type
= IF_PROTO_FR
;
1240 if (ifr
->ifr_settings
.size
< size
) {
1241 ifr
->ifr_settings
.size
= size
; /* data size wanted */
1244 if (copy_to_user(fr_s
, &state(hdlc
)->settings
, size
))
1249 if(!capable(CAP_NET_ADMIN
))
1252 if(dev
->flags
& IFF_UP
)
1255 if (copy_from_user(&new_settings
, fr_s
, size
))
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))
1275 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
1279 if (dev_to_hdlc(dev
)->proto
!= &proto
) { /* Different proto */
1280 result
= attach_hdlc_protocol(dev
, &proto
, fr_rx
,
1281 sizeof(struct frad_state
));
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
;
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 */
1300 if(!capable(CAP_NET_ADMIN
))
1303 if (copy_from_user(&pvc
, ifr
->ifr_settings
.ifs_ifsu
.fr_pvc
,
1304 sizeof(fr_proto_pvc
)))
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 */
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
);
1320 return fr_del_pvc(hdlc
, pvc
.dlci
, result
);
1327 static int __init
mod_init(void)
1329 register_hdlc_protocol(&proto
);
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");