1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic HDLC support routines for Linux
6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
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
21 (exist,new,active) = FULL STATUS if "link reliable"
22 = 0, 0, 0 if "link unreliable"
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>
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
91 #if defined(__LITTLE_ENDIAN_BITFIELD)
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 */
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 */
136 struct pvc_device
*first_pvc
;
139 struct timer_list timer
;
140 struct net_device
*dev
;
141 unsigned long last_poll
;
146 u32 last_errors
; /* last errors bit list */
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
;
180 if (pvc
->dlci
== dlci
)
182 if (pvc
->dlci
> dlci
)
183 return NULL
; /* the list is sorted */
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
;
197 if ((*pvc_p
)->dlci
== dlci
)
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
);
206 printk(KERN_DEBUG
"add_pvc: allocated pvc %p, frad %p\n", pvc
, dev
);
213 pvc
->next
= *pvc_p
; /* Put it in the chain */
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
)
229 if (!netif_carrier_ok(pvc
->main
))
230 netif_carrier_on(pvc
->main
);
232 if (!netif_carrier_ok(pvc
->ether
))
233 netif_carrier_on(pvc
->ether
);
236 if (netif_carrier_ok(pvc
->main
))
237 netif_carrier_off(pvc
->main
);
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
;
250 if (!pvc_is_used(*pvc_p
)) {
251 struct pvc_device
*pvc
= *pvc_p
;
253 printk(KERN_DEBUG
"freeing unused pvc: %p\n", pvc
);
259 pvc_p
= &(*pvc_p
)->next
;
264 static inline struct net_device
**get_dev_p(struct pvc_device
*pvc
,
267 if (type
== ARPHRD_ETHER
)
274 static int fr_hard_header(struct sk_buff
*skb
, u16 dlci
)
276 if (!skb
->dev
) { /* Control packets */
278 case LMI_CCITT_ANSI_DLCI
:
280 skb
->data
[3] = NLPID_CCITT_ANSI_LMI
;
285 skb
->data
[3] = NLPID_CISCO_LMI
;
292 } else if (skb
->dev
->type
== ARPHRD_DLCI
) {
293 switch (skb
->protocol
) {
294 case htons(ETH_P_IP
):
296 skb
->data
[3] = NLPID_IP
;
299 case htons(ETH_P_IPV6
):
301 skb
->data
[3] = NLPID_IPV6
;
306 skb
->data
[3] = FR_PAD
;
307 skb
->data
[4] = NLPID_SNAP
;
308 /* OUI 00-00-00 indicates an Ethertype follows */
312 /* This should be an Ethertype: */
313 *(__be16
*)(skb
->data
+ 8) = skb
->protocol
;
316 } else if (skb
->dev
->type
== ARPHRD_ETHER
) {
318 skb
->data
[3] = FR_PAD
;
319 skb
->data
[4] = NLPID_SNAP
;
320 /* OUI 00-80-C2 stands for the 802.1 organization */
324 /* PID 00-07 stands for Ethernet frames without FCS */
332 dlci_to_q922(skb
->data
, dlci
);
333 skb
->data
[2] = FR_UI
;
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;
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;
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
;
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
);
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
)))
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
)
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))
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);
437 if (fr_hard_header(skb
, pvc
->dlci
))
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
);
451 dev
->stats
.tx_dropped
++;
456 static inline void fr_log_dlci_active(struct pvc_device
*pvc
)
458 netdev_info(pvc
->frad
, "DLCI %d [%s%s%s]%s %s\n",
460 pvc
->main
? pvc
->main
->name
: "",
461 pvc
->main
&& pvc
->ether
? " " : "",
462 pvc
->ether
? pvc
->ether
->name
: "",
463 pvc
->state
.new ? " new" : "",
464 !pvc
->state
.exist
? "deleted" :
465 pvc
->state
.active
? "active" : "inactive");
470 static inline u8
fr_lmi_nextseq(u8 x
)
477 static void fr_lmi_send(struct net_device
*dev
, int fullrep
)
479 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
481 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
482 int lmi
= state(hdlc
)->settings
.lmi
;
483 int dce
= state(hdlc
)->settings
.dce
;
484 int len
= lmi
== LMI_ANSI
? LMI_ANSI_LENGTH
: LMI_CCITT_CISCO_LENGTH
;
485 int stat_len
= (lmi
== LMI_CISCO
) ? 6 : 3;
489 if (dce
&& fullrep
) {
490 len
+= state(hdlc
)->dce_pvc_count
* (2 + stat_len
);
491 if (len
> HDLC_MAX_MRU
) {
492 netdev_warn(dev
, "Too many PVCs while sending LMI full report\n");
497 skb
= dev_alloc_skb(len
);
499 netdev_warn(dev
, "Memory squeeze on fr_lmi_send()\n");
502 memset(skb
->data
, 0, len
);
504 if (lmi
== LMI_CISCO
) {
505 fr_hard_header(skb
, LMI_CISCO_DLCI
);
507 fr_hard_header(skb
, LMI_CCITT_ANSI_DLCI
);
509 data
= skb_tail_pointer(skb
);
510 data
[i
++] = LMI_CALLREF
;
511 data
[i
++] = dce
? LMI_STATUS
: LMI_STATUS_ENQUIRY
;
513 data
[i
++] = LMI_ANSI_LOCKSHIFT
;
514 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_REPTYPE
:
515 LMI_ANSI_CISCO_REPTYPE
;
516 data
[i
++] = LMI_REPT_LEN
;
517 data
[i
++] = fullrep
? LMI_FULLREP
: LMI_INTEGRITY
;
518 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_ALIVE
: LMI_ANSI_CISCO_ALIVE
;
519 data
[i
++] = LMI_INTEG_LEN
;
520 data
[i
++] = state(hdlc
)->txseq
=
521 fr_lmi_nextseq(state(hdlc
)->txseq
);
522 data
[i
++] = state(hdlc
)->rxseq
;
524 if (dce
&& fullrep
) {
526 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_PVCSTAT
:
527 LMI_ANSI_CISCO_PVCSTAT
;
528 data
[i
++] = stat_len
;
530 /* LMI start/restart */
531 if (state(hdlc
)->reliable
&& !pvc
->state
.exist
) {
532 pvc
->state
.exist
= pvc
->state
.new = 1;
533 fr_log_dlci_active(pvc
);
536 /* ifconfig PVC up */
537 if (pvc
->open_count
&& !pvc
->state
.active
&&
538 pvc
->state
.exist
&& !pvc
->state
.new) {
540 pvc
->state
.active
= 1;
541 fr_log_dlci_active(pvc
);
544 if (lmi
== LMI_CISCO
) {
545 data
[i
] = pvc
->dlci
>> 8;
546 data
[i
+ 1] = pvc
->dlci
& 0xFF;
548 data
[i
] = (pvc
->dlci
>> 4) & 0x3F;
549 data
[i
+ 1] = ((pvc
->dlci
<< 3) & 0x78) | 0x80;
555 else if (pvc
->state
.active
)
564 skb
->priority
= TC_PRIO_CONTROL
;
566 skb
->protocol
= htons(ETH_P_HDLC
);
567 skb_reset_network_header(skb
);
574 static void fr_set_link_state(int reliable
, struct net_device
*dev
)
576 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
577 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
579 state(hdlc
)->reliable
= reliable
;
581 netif_dormant_off(dev
);
582 state(hdlc
)->n391cnt
= 0; /* Request full status */
583 state(hdlc
)->dce_changed
= 1;
585 if (state(hdlc
)->settings
.lmi
== LMI_NONE
) {
586 while (pvc
) { /* Activate all PVCs */
588 pvc
->state
.exist
= pvc
->state
.active
= 1;
594 netif_dormant_on(dev
);
595 while (pvc
) { /* Deactivate all PVCs */
597 pvc
->state
.exist
= pvc
->state
.active
= 0;
599 if (!state(hdlc
)->settings
.dce
)
600 pvc
->state
.bandwidth
= 0;
607 static void fr_timer(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
;
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;
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
;
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
;
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");
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");
680 if (skb
->data
[4] != LMI_CALLREF
) {
681 netdev_info(dev
, "Invalid LMI Call reference (0x%02X)\n",
686 if (skb
->data
[5] != (dce
? LMI_STATUS_ENQUIRY
: LMI_STATUS
)) {
687 netdev_info(dev
, "Invalid LMI Message type (0x%02X)\n",
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",
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",
709 if (skb
->data
[++i
] != LMI_REPT_LEN
) {
710 netdev_info(dev
, "Invalid LMI Report type IE length (%u)\n",
715 reptype
= skb
->data
[++i
];
716 if (reptype
!= LMI_INTEGRITY
&& reptype
!= LMI_FULLREP
) {
717 netdev_info(dev
, "Unsupported LMI Report type (0x%02X)\n",
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",
729 if (skb
->data
[++i
] != LMI_INTEG_LEN
) {
730 netdev_info(dev
, "Invalid LMI Link integrity verification IE length (%u)\n",
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
;
742 state(hdlc
)->last_poll
= jiffies
;
745 if (!state(hdlc
)->reliable
)
748 if (rxseq
== 0 || rxseq
!= txseq
) { /* Ask for full report next time */
749 state(hdlc
)->n391cnt
= 0;
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
;
759 if (pvc
->state
.new) {
762 /* Tell DTE that new PVC is now active */
763 state(hdlc
)->dce_changed
= 1;
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);
782 state(hdlc
)->request
= 0; /* got response, no request pending */
787 if (reptype
!= LMI_FULLREP
)
790 pvc
= state(hdlc
)->first_pvc
;
793 pvc
->state
.deleted
= 1;
798 while (skb
->len
>= i
+ 2 + stat_len
) {
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",
810 if (skb
->data
[++i
] != stat_len
) {
811 netdev_info(dev
, "Invalid LMI PVC status IE length (%u)\n",
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) |
825 dlci
= ((skb
->data
[i
] & 0x3F) << 4) |
826 ((skb
->data
[i
+ 1] & 0x78) >> 3);
830 pvc
= add_pvc(dev
, dlci
);
832 if (!pvc
&& !no_ram
) {
833 netdev_warn(dev
, "Memory squeeze on fr_lmi_recv()\n");
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
||
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
);
855 pvc
= state(hdlc
)->first_pvc
;
858 if (pvc
->state
.deleted
&& pvc
->state
.exist
) {
860 pvc
->state
.active
= pvc
->state
.new = 0;
861 pvc
->state
.exist
= 0;
862 pvc
->state
.bandwidth
= 0;
863 fr_log_dlci_active(pvc
);
868 /* Next full report after N391 polls */
869 state(hdlc
)->n391cnt
= state(hdlc
)->settings
.n391
;
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) {
882 skb
->dev
= pvc
->main
;
883 skb
->protocol
= *(__be16
*)(skb
->data
+ 3); /* Ethertype */
885 skb_reset_mac_header(skb
);
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) {
898 if (skb
->len
< ETH_HLEN
)
900 skb
->protocol
= eth_type_trans(skb
, pvc
->ether
);
903 /* PID unsupported */
908 /* OUI unsupported */
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
;
921 struct pvc_device
*pvc
;
922 struct net_device
*dev
;
924 if (skb
->len
< 4 || fh
->ea1
|| !fh
->ea2
|| data
[2] != FR_UI
)
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
))
936 dev_kfree_skb_any(skb
);
937 return NET_RX_SUCCESS
;
940 pvc
= find_pvc(hdlc
, dlci
);
943 netdev_info(frad
, "No PVC for received frame's DLCI %d\n",
949 if (pvc
->state
.fecn
!= fh
->fecn
) {
951 printk(KERN_DEBUG
"%s: DLCI %d FECN O%s\n", frad
->name
,
952 dlci
, fh
->fecn
? "N" : "FF");
954 pvc
->state
.fecn
^= 1;
957 if (pvc
->state
.becn
!= fh
->becn
) {
959 printk(KERN_DEBUG
"%s: DLCI %d BECN O%s\n", frad
->name
,
960 dlci
, fh
->becn
? "N" : "FF");
962 pvc
->state
.becn
^= 1;
966 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
967 frad
->stats
.rx_dropped
++;
971 if (data
[3] == NLPID_IP
) {
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
) {
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
) {
990 if (data
[4] == NLPID_SNAP
) { /* A SNAP header follows */
992 if (skb
->len
< 5) /* Incomplete SNAP header */
994 if (fr_snap_parse(skb
, pvc
))
1001 netdev_info(frad
, "Unsupported protocol, NLPID=%x length=%i\n",
1007 dev
->stats
.rx_packets
++; /* PVC traffic */
1008 dev
->stats
.rx_bytes
+= skb
->len
;
1009 if (pvc
->state
.becn
)
1010 dev
->stats
.rx_compressed
++;
1012 return NET_RX_SUCCESS
;
1015 frad
->stats
.rx_errors
++; /* Mark error */
1017 dev_kfree_skb_any(skb
);
1023 static void fr_start(struct net_device
*dev
)
1025 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1027 printk(KERN_DEBUG
"fr_start\n");
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
);
1044 fr_set_link_state(1, dev
);
1048 static void fr_stop(struct net_device
*dev
)
1050 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1052 printk(KERN_DEBUG
"fr_stop\n");
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 */
1067 dev_close(pvc
->main
);
1069 dev_close(pvc
->ether
);
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;
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
;
1098 if ((pvc
= add_pvc(frad
, dlci
)) == NULL
) {
1099 netdev_warn(frad
, "Memory squeeze on fr_add_pvc()\n");
1103 if (*get_dev_p(pvc
, type
))
1106 used
= pvc_is_used(pvc
);
1108 if (type
== ARPHRD_ETHER
)
1109 dev
= alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN
,
1112 dev
= alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN
, pvc_setup
);
1115 netdev_warn(frad
, "Memory squeeze on fr_pvc()\n");
1116 delete_unused_pvcs(hdlc
);
1120 if (type
== ARPHRD_ETHER
) {
1121 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1122 eth_hw_addr_random(dev
);
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
;
1130 dev
->max_mtu
= HDLC_MAX_MTU
;
1131 dev
->needed_headroom
= 10;
1132 dev
->priv_flags
|= IFF_NO_QUEUE
;
1135 if (register_netdevice(dev
) != 0) {
1137 delete_unused_pvcs(hdlc
);
1141 dev
->needs_free_netdev
= true;
1142 *get_dev_p(pvc
, type
) = dev
;
1144 state(hdlc
)->dce_changed
= 1;
1145 state(hdlc
)->dce_pvc_count
++;
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
)
1160 if ((dev
= *get_dev_p(pvc
, type
)) == NULL
)
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
);
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;
1188 struct pvc_device
*next
= pvc
->next
;
1189 /* destructors will free_netdev() main and ether */
1191 unregister_netdevice(pvc
->main
);
1194 unregister_netdevice(pvc
->ether
);
1202 static struct hdlc_proto proto
= {
1206 .detach
= fr_destroy
,
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
);
1222 switch (ifr
->ifr_settings
.type
) {
1224 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1226 ifr
->ifr_settings
.type
= IF_PROTO_FR
;
1227 if (ifr
->ifr_settings
.size
< size
) {
1228 ifr
->ifr_settings
.size
= size
; /* data size wanted */
1231 if (copy_to_user(fr_s
, &state(hdlc
)->settings
, size
))
1236 if (!capable(CAP_NET_ADMIN
))
1239 if (dev
->flags
& IFF_UP
)
1242 if (copy_from_user(&new_settings
, fr_s
, size
))
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))
1262 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
1266 if (dev_to_hdlc(dev
)->proto
!= &proto
) { /* Different proto */
1267 result
= attach_hdlc_protocol(dev
, &proto
,
1268 sizeof(struct frad_state
));
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
);
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 */
1286 if (!capable(CAP_NET_ADMIN
))
1289 if (copy_from_user(&pvc
, ifr
->ifr_settings
.ifs_ifsu
.fr_pvc
,
1290 sizeof(fr_proto_pvc
)))
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 */
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
);
1306 return fr_del_pvc(hdlc
, pvc
.dlci
, result
);
1313 static int __init
mod_init(void)
1315 register_hdlc_protocol(&proto
);
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");