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
63 #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */
64 #define LMI_CISCO_DLCI 1023
66 #define LMI_CALLREF 0x00 /* Call Reference */
67 #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */
68 #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */
69 #define LMI_CCITT_REPTYPE 0x51
70 #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */
71 #define LMI_CCITT_ALIVE 0x53
72 #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */
73 #define LMI_CCITT_PVCSTAT 0x57
75 #define LMI_FULLREP 0x00 /* full report */
76 #define LMI_INTEGRITY 0x01 /* link integrity report */
77 #define LMI_SINGLE 0x02 /* single PVC report */
79 #define LMI_STATUS_ENQUIRY 0x75
80 #define LMI_STATUS 0x7D /* reply */
82 #define LMI_REPT_LEN 1 /* report type element length */
83 #define LMI_INTEG_LEN 2 /* link integrity element length */
85 #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */
86 #define LMI_ANSI_LENGTH 14
89 #if defined(__LITTLE_ENDIAN_BITFIELD)
113 struct net_device
*frad
;
114 struct net_device
*main
;
115 struct net_device
*ether
; /* bridged Ethernet interface */
116 struct pvc_device
*next
; /* Sorted in ascending DLCI order */
122 unsigned int active
: 1;
123 unsigned int exist
: 1;
124 unsigned int deleted
: 1;
125 unsigned int fecn
: 1;
126 unsigned int becn
: 1;
127 unsigned int bandwidth
; /* Cisco LMI reporting only */
133 struct pvc_device
*first_pvc
;
136 struct timer_list timer
;
137 struct net_device
*dev
;
138 unsigned long last_poll
;
143 u32 last_errors
; /* last errors bit list */
145 u8 txseq
; /* TX sequence number */
146 u8 rxseq
; /* RX sequence number */
149 static int fr_ioctl(struct net_device
*dev
, struct if_settings
*ifs
);
151 static inline u16
q922_to_dlci(u8
*hdr
)
153 return ((hdr
[0] & 0xFC) << 2) | ((hdr
[1] & 0xF0) >> 4);
156 static inline void dlci_to_q922(u8
*hdr
, u16 dlci
)
158 hdr
[0] = (dlci
>> 2) & 0xFC;
159 hdr
[1] = ((dlci
<< 4) & 0xF0) | 0x01;
162 static inline struct frad_state
*state(hdlc_device
*hdlc
)
164 return (struct frad_state
*)(hdlc
->state
);
167 static inline struct pvc_device
*find_pvc(hdlc_device
*hdlc
, u16 dlci
)
169 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
172 if (pvc
->dlci
== dlci
)
174 if (pvc
->dlci
> dlci
)
175 return NULL
; /* the list is sorted */
182 static struct pvc_device
*add_pvc(struct net_device
*dev
, u16 dlci
)
184 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
185 struct pvc_device
*pvc
, **pvc_p
= &state(hdlc
)->first_pvc
;
188 if ((*pvc_p
)->dlci
== dlci
)
190 if ((*pvc_p
)->dlci
> dlci
)
191 break; /* the list is sorted */
192 pvc_p
= &(*pvc_p
)->next
;
195 pvc
= kzalloc(sizeof(*pvc
), GFP_ATOMIC
);
197 printk(KERN_DEBUG
"add_pvc: allocated pvc %p, frad %p\n", pvc
, dev
);
204 pvc
->next
= *pvc_p
; /* Put it in the chain */
209 static inline int pvc_is_used(struct pvc_device
*pvc
)
211 return pvc
->main
|| pvc
->ether
;
214 static inline void pvc_carrier(int on
, struct pvc_device
*pvc
)
218 if (!netif_carrier_ok(pvc
->main
))
219 netif_carrier_on(pvc
->main
);
221 if (!netif_carrier_ok(pvc
->ether
))
222 netif_carrier_on(pvc
->ether
);
225 if (netif_carrier_ok(pvc
->main
))
226 netif_carrier_off(pvc
->main
);
228 if (netif_carrier_ok(pvc
->ether
))
229 netif_carrier_off(pvc
->ether
);
233 static inline void delete_unused_pvcs(hdlc_device
*hdlc
)
235 struct pvc_device
**pvc_p
= &state(hdlc
)->first_pvc
;
238 if (!pvc_is_used(*pvc_p
)) {
239 struct pvc_device
*pvc
= *pvc_p
;
241 printk(KERN_DEBUG
"freeing unused pvc: %p\n", pvc
);
247 pvc_p
= &(*pvc_p
)->next
;
251 static inline struct net_device
**get_dev_p(struct pvc_device
*pvc
,
254 if (type
== ARPHRD_ETHER
)
260 static int fr_hard_header(struct sk_buff
*skb
, u16 dlci
)
262 if (!skb
->dev
) { /* Control packets */
264 case LMI_CCITT_ANSI_DLCI
:
266 skb
->data
[3] = NLPID_CCITT_ANSI_LMI
;
271 skb
->data
[3] = NLPID_CISCO_LMI
;
278 } else if (skb
->dev
->type
== ARPHRD_DLCI
) {
279 switch (skb
->protocol
) {
280 case htons(ETH_P_IP
):
282 skb
->data
[3] = NLPID_IP
;
285 case htons(ETH_P_IPV6
):
287 skb
->data
[3] = NLPID_IPV6
;
292 skb
->data
[3] = FR_PAD
;
293 skb
->data
[4] = NLPID_SNAP
;
294 /* OUI 00-00-00 indicates an Ethertype follows */
298 /* This should be an Ethertype: */
299 *(__be16
*)(skb
->data
+ 8) = skb
->protocol
;
302 } else if (skb
->dev
->type
== ARPHRD_ETHER
) {
304 skb
->data
[3] = FR_PAD
;
305 skb
->data
[4] = NLPID_SNAP
;
306 /* OUI 00-80-C2 stands for the 802.1 organization */
310 /* PID 00-07 stands for Ethernet frames without FCS */
318 dlci_to_q922(skb
->data
, dlci
);
319 skb
->data
[2] = FR_UI
;
323 static int pvc_open(struct net_device
*dev
)
325 struct pvc_device
*pvc
= dev
->ml_priv
;
327 if ((pvc
->frad
->flags
& IFF_UP
) == 0)
328 return -EIO
; /* Frad must be UP in order to activate PVC */
330 if (pvc
->open_count
++ == 0) {
331 hdlc_device
*hdlc
= dev_to_hdlc(pvc
->frad
);
333 if (state(hdlc
)->settings
.lmi
== LMI_NONE
)
334 pvc
->state
.active
= netif_carrier_ok(pvc
->frad
);
336 pvc_carrier(pvc
->state
.active
, pvc
);
337 state(hdlc
)->dce_changed
= 1;
342 static int pvc_close(struct net_device
*dev
)
344 struct pvc_device
*pvc
= dev
->ml_priv
;
346 if (--pvc
->open_count
== 0) {
347 hdlc_device
*hdlc
= dev_to_hdlc(pvc
->frad
);
349 if (state(hdlc
)->settings
.lmi
== LMI_NONE
)
350 pvc
->state
.active
= 0;
352 if (state(hdlc
)->settings
.dce
) {
353 state(hdlc
)->dce_changed
= 1;
354 pvc
->state
.active
= 0;
360 static int pvc_ioctl(struct net_device
*dev
, struct if_settings
*ifs
)
362 struct pvc_device
*pvc
= dev
->ml_priv
;
363 fr_proto_pvc_info info
;
365 if (ifs
->type
== IF_GET_PROTO
) {
366 if (dev
->type
== ARPHRD_ETHER
)
367 ifs
->type
= IF_PROTO_FR_ETH_PVC
;
369 ifs
->type
= IF_PROTO_FR_PVC
;
371 if (ifs
->size
< sizeof(info
)) {
372 /* data size wanted */
373 ifs
->size
= sizeof(info
);
377 info
.dlci
= pvc
->dlci
;
378 memcpy(info
.master
, pvc
->frad
->name
, IFNAMSIZ
);
379 if (copy_to_user(ifs
->ifs_ifsu
.fr_pvc_info
,
380 &info
, sizeof(info
)))
388 static netdev_tx_t
pvc_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
390 struct pvc_device
*pvc
= dev
->ml_priv
;
392 if (!pvc
->state
.active
)
395 if (dev
->type
== ARPHRD_ETHER
) {
396 int pad
= ETH_ZLEN
- skb
->len
;
398 if (pad
> 0) { /* Pad the frame with zeros */
399 if (__skb_pad(skb
, pad
, false))
405 /* We already requested the header space with dev->needed_headroom.
406 * So this is just a protection in case the upper layer didn't take
407 * dev->needed_headroom into consideration.
409 if (skb_headroom(skb
) < 10) {
410 struct sk_buff
*skb2
= skb_realloc_headroom(skb
, 10);
419 if (fr_hard_header(skb
, pvc
->dlci
))
422 dev
->stats
.tx_bytes
+= skb
->len
;
423 dev
->stats
.tx_packets
++;
424 if (pvc
->state
.fecn
) /* TX Congestion counter */
425 dev
->stats
.tx_compressed
++;
426 skb
->dev
= pvc
->frad
;
427 skb
->protocol
= htons(ETH_P_HDLC
);
428 skb_reset_network_header(skb
);
433 dev
->stats
.tx_dropped
++;
438 static inline void fr_log_dlci_active(struct pvc_device
*pvc
)
440 netdev_info(pvc
->frad
, "DLCI %d [%s%s%s]%s %s\n",
442 pvc
->main
? pvc
->main
->name
: "",
443 pvc
->main
&& pvc
->ether
? " " : "",
444 pvc
->ether
? pvc
->ether
->name
: "",
445 pvc
->state
.new ? " new" : "",
446 !pvc
->state
.exist
? "deleted" :
447 pvc
->state
.active
? "active" : "inactive");
450 static inline u8
fr_lmi_nextseq(u8 x
)
456 static void fr_lmi_send(struct net_device
*dev
, int fullrep
)
458 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
460 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
461 int lmi
= state(hdlc
)->settings
.lmi
;
462 int dce
= state(hdlc
)->settings
.dce
;
463 int len
= lmi
== LMI_ANSI
? LMI_ANSI_LENGTH
: LMI_CCITT_CISCO_LENGTH
;
464 int stat_len
= (lmi
== LMI_CISCO
) ? 6 : 3;
468 if (dce
&& fullrep
) {
469 len
+= state(hdlc
)->dce_pvc_count
* (2 + stat_len
);
470 if (len
> HDLC_MAX_MRU
) {
471 netdev_warn(dev
, "Too many PVCs while sending LMI full report\n");
476 skb
= dev_alloc_skb(len
);
480 memset(skb
->data
, 0, len
);
482 if (lmi
== LMI_CISCO
)
483 fr_hard_header(skb
, LMI_CISCO_DLCI
);
485 fr_hard_header(skb
, LMI_CCITT_ANSI_DLCI
);
487 data
= skb_tail_pointer(skb
);
488 data
[i
++] = LMI_CALLREF
;
489 data
[i
++] = dce
? LMI_STATUS
: LMI_STATUS_ENQUIRY
;
491 data
[i
++] = LMI_ANSI_LOCKSHIFT
;
492 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_REPTYPE
:
493 LMI_ANSI_CISCO_REPTYPE
;
494 data
[i
++] = LMI_REPT_LEN
;
495 data
[i
++] = fullrep
? LMI_FULLREP
: LMI_INTEGRITY
;
496 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_ALIVE
: LMI_ANSI_CISCO_ALIVE
;
497 data
[i
++] = LMI_INTEG_LEN
;
498 data
[i
++] = state(hdlc
)->txseq
=
499 fr_lmi_nextseq(state(hdlc
)->txseq
);
500 data
[i
++] = state(hdlc
)->rxseq
;
502 if (dce
&& fullrep
) {
504 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_PVCSTAT
:
505 LMI_ANSI_CISCO_PVCSTAT
;
506 data
[i
++] = stat_len
;
508 /* LMI start/restart */
509 if (state(hdlc
)->reliable
&& !pvc
->state
.exist
) {
510 pvc
->state
.exist
= pvc
->state
.new = 1;
511 fr_log_dlci_active(pvc
);
514 /* ifconfig PVC up */
515 if (pvc
->open_count
&& !pvc
->state
.active
&&
516 pvc
->state
.exist
&& !pvc
->state
.new) {
518 pvc
->state
.active
= 1;
519 fr_log_dlci_active(pvc
);
522 if (lmi
== LMI_CISCO
) {
523 data
[i
] = pvc
->dlci
>> 8;
524 data
[i
+ 1] = pvc
->dlci
& 0xFF;
526 data
[i
] = (pvc
->dlci
>> 4) & 0x3F;
527 data
[i
+ 1] = ((pvc
->dlci
<< 3) & 0x78) | 0x80;
533 else if (pvc
->state
.active
)
542 skb
->priority
= TC_PRIO_CONTROL
;
544 skb
->protocol
= htons(ETH_P_HDLC
);
545 skb_reset_network_header(skb
);
550 static void fr_set_link_state(int reliable
, struct net_device
*dev
)
552 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
553 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
555 state(hdlc
)->reliable
= reliable
;
557 netif_dormant_off(dev
);
558 state(hdlc
)->n391cnt
= 0; /* Request full status */
559 state(hdlc
)->dce_changed
= 1;
561 if (state(hdlc
)->settings
.lmi
== LMI_NONE
) {
562 while (pvc
) { /* Activate all PVCs */
564 pvc
->state
.exist
= pvc
->state
.active
= 1;
570 netif_dormant_on(dev
);
571 while (pvc
) { /* Deactivate all PVCs */
573 pvc
->state
.exist
= pvc
->state
.active
= 0;
575 if (!state(hdlc
)->settings
.dce
)
576 pvc
->state
.bandwidth
= 0;
582 static void fr_timer(struct timer_list
*t
)
584 struct frad_state
*st
= from_timer(st
, t
, timer
);
585 struct net_device
*dev
= st
->dev
;
586 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
587 int i
, cnt
= 0, reliable
;
590 if (state(hdlc
)->settings
.dce
) {
591 reliable
= state(hdlc
)->request
&&
592 time_before(jiffies
, state(hdlc
)->last_poll
+
593 state(hdlc
)->settings
.t392
* HZ
);
594 state(hdlc
)->request
= 0;
596 state(hdlc
)->last_errors
<<= 1; /* Shift the list */
597 if (state(hdlc
)->request
) {
598 if (state(hdlc
)->reliable
)
599 netdev_info(dev
, "No LMI status reply received\n");
600 state(hdlc
)->last_errors
|= 1;
603 list
= state(hdlc
)->last_errors
;
604 for (i
= 0; i
< state(hdlc
)->settings
.n393
; i
++, list
>>= 1)
605 cnt
+= (list
& 1); /* errors count */
607 reliable
= (cnt
< state(hdlc
)->settings
.n392
);
610 if (state(hdlc
)->reliable
!= reliable
) {
611 netdev_info(dev
, "Link %sreliable\n", reliable
? "" : "un");
612 fr_set_link_state(reliable
, dev
);
615 if (state(hdlc
)->settings
.dce
) {
616 state(hdlc
)->timer
.expires
= jiffies
+
617 state(hdlc
)->settings
.t392
* HZ
;
619 if (state(hdlc
)->n391cnt
)
620 state(hdlc
)->n391cnt
--;
622 fr_lmi_send(dev
, state(hdlc
)->n391cnt
== 0);
624 state(hdlc
)->last_poll
= jiffies
;
625 state(hdlc
)->request
= 1;
626 state(hdlc
)->timer
.expires
= jiffies
+
627 state(hdlc
)->settings
.t391
* HZ
;
630 add_timer(&state(hdlc
)->timer
);
633 static int fr_lmi_recv(struct net_device
*dev
, struct sk_buff
*skb
)
635 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
636 struct pvc_device
*pvc
;
638 int lmi
= state(hdlc
)->settings
.lmi
;
639 int dce
= state(hdlc
)->settings
.dce
;
640 int stat_len
= (lmi
== LMI_CISCO
) ? 6 : 3, reptype
, error
, no_ram
, i
;
642 if (skb
->len
< (lmi
== LMI_ANSI
? LMI_ANSI_LENGTH
:
643 LMI_CCITT_CISCO_LENGTH
)) {
644 netdev_info(dev
, "Short LMI frame\n");
648 if (skb
->data
[3] != (lmi
== LMI_CISCO
? NLPID_CISCO_LMI
:
649 NLPID_CCITT_ANSI_LMI
)) {
650 netdev_info(dev
, "Received non-LMI frame with LMI DLCI\n");
654 if (skb
->data
[4] != LMI_CALLREF
) {
655 netdev_info(dev
, "Invalid LMI Call reference (0x%02X)\n",
660 if (skb
->data
[5] != (dce
? LMI_STATUS_ENQUIRY
: LMI_STATUS
)) {
661 netdev_info(dev
, "Invalid LMI Message type (0x%02X)\n",
666 if (lmi
== LMI_ANSI
) {
667 if (skb
->data
[6] != LMI_ANSI_LOCKSHIFT
) {
668 netdev_info(dev
, "Not ANSI locking shift in LMI message (0x%02X)\n",
677 if (skb
->data
[i
] != (lmi
== LMI_CCITT
? LMI_CCITT_REPTYPE
:
678 LMI_ANSI_CISCO_REPTYPE
)) {
679 netdev_info(dev
, "Not an LMI Report type IE (0x%02X)\n",
684 if (skb
->data
[++i
] != LMI_REPT_LEN
) {
685 netdev_info(dev
, "Invalid LMI Report type IE length (%u)\n",
690 reptype
= skb
->data
[++i
];
691 if (reptype
!= LMI_INTEGRITY
&& reptype
!= LMI_FULLREP
) {
692 netdev_info(dev
, "Unsupported LMI Report type (0x%02X)\n",
697 if (skb
->data
[++i
] != (lmi
== LMI_CCITT
? LMI_CCITT_ALIVE
:
698 LMI_ANSI_CISCO_ALIVE
)) {
699 netdev_info(dev
, "Not an LMI Link integrity verification IE (0x%02X)\n",
704 if (skb
->data
[++i
] != LMI_INTEG_LEN
) {
705 netdev_info(dev
, "Invalid LMI Link integrity verification IE length (%u)\n",
711 state(hdlc
)->rxseq
= skb
->data
[i
++]; /* TX sequence from peer */
712 rxseq
= skb
->data
[i
++]; /* Should confirm our sequence */
714 txseq
= state(hdlc
)->txseq
;
717 state(hdlc
)->last_poll
= jiffies
;
720 if (!state(hdlc
)->reliable
)
723 if (rxseq
== 0 || rxseq
!= txseq
) { /* Ask for full report next time */
724 state(hdlc
)->n391cnt
= 0;
729 if (state(hdlc
)->fullrep_sent
&& !error
) {
730 /* Stop sending full report - the last one has been confirmed by DTE */
731 state(hdlc
)->fullrep_sent
= 0;
732 pvc
= state(hdlc
)->first_pvc
;
734 if (pvc
->state
.new) {
737 /* Tell DTE that new PVC is now active */
738 state(hdlc
)->dce_changed
= 1;
744 if (state(hdlc
)->dce_changed
) {
745 reptype
= LMI_FULLREP
;
746 state(hdlc
)->fullrep_sent
= 1;
747 state(hdlc
)->dce_changed
= 0;
750 state(hdlc
)->request
= 1; /* got request */
751 fr_lmi_send(dev
, reptype
== LMI_FULLREP
? 1 : 0);
757 state(hdlc
)->request
= 0; /* got response, no request pending */
762 if (reptype
!= LMI_FULLREP
)
765 pvc
= state(hdlc
)->first_pvc
;
768 pvc
->state
.deleted
= 1;
773 while (skb
->len
>= i
+ 2 + stat_len
) {
776 unsigned int active
, new;
778 if (skb
->data
[i
] != (lmi
== LMI_CCITT
? LMI_CCITT_PVCSTAT
:
779 LMI_ANSI_CISCO_PVCSTAT
)) {
780 netdev_info(dev
, "Not an LMI PVC status IE (0x%02X)\n",
785 if (skb
->data
[++i
] != stat_len
) {
786 netdev_info(dev
, "Invalid LMI PVC status IE length (%u)\n",
792 new = !!(skb
->data
[i
+ 2] & 0x08);
793 active
= !!(skb
->data
[i
+ 2] & 0x02);
794 if (lmi
== LMI_CISCO
) {
795 dlci
= (skb
->data
[i
] << 8) | skb
->data
[i
+ 1];
796 bw
= (skb
->data
[i
+ 3] << 16) |
797 (skb
->data
[i
+ 4] << 8) |
800 dlci
= ((skb
->data
[i
] & 0x3F) << 4) |
801 ((skb
->data
[i
+ 1] & 0x78) >> 3);
805 pvc
= add_pvc(dev
, dlci
);
807 if (!pvc
&& !no_ram
) {
808 netdev_warn(dev
, "Memory squeeze on fr_lmi_recv()\n");
813 pvc
->state
.exist
= 1;
814 pvc
->state
.deleted
= 0;
815 if (active
!= pvc
->state
.active
||
816 new != pvc
->state
.new ||
817 bw
!= pvc
->state
.bandwidth
||
819 pvc
->state
.new = new;
820 pvc
->state
.active
= active
;
821 pvc
->state
.bandwidth
= bw
;
822 pvc_carrier(active
, pvc
);
823 fr_log_dlci_active(pvc
);
830 pvc
= state(hdlc
)->first_pvc
;
833 if (pvc
->state
.deleted
&& pvc
->state
.exist
) {
835 pvc
->state
.active
= pvc
->state
.new = 0;
836 pvc
->state
.exist
= 0;
837 pvc
->state
.bandwidth
= 0;
838 fr_log_dlci_active(pvc
);
843 /* Next full report after N391 polls */
844 state(hdlc
)->n391cnt
= state(hdlc
)->settings
.n391
;
849 static int fr_snap_parse(struct sk_buff
*skb
, struct pvc_device
*pvc
)
851 /* OUI 00-00-00 indicates an Ethertype follows */
852 if (skb
->data
[0] == 0x00 &&
853 skb
->data
[1] == 0x00 &&
854 skb
->data
[2] == 0x00) {
857 skb
->dev
= pvc
->main
;
858 skb
->protocol
= *(__be16
*)(skb
->data
+ 3); /* Ethertype */
860 skb_reset_mac_header(skb
);
863 /* OUI 00-80-C2 stands for the 802.1 organization */
864 } else if (skb
->data
[0] == 0x00 &&
865 skb
->data
[1] == 0x80 &&
866 skb
->data
[2] == 0xC2) {
867 /* PID 00-07 stands for Ethernet frames without FCS */
868 if (skb
->data
[3] == 0x00 &&
869 skb
->data
[4] == 0x07) {
873 if (skb
->len
< ETH_HLEN
)
875 skb
->protocol
= eth_type_trans(skb
, pvc
->ether
);
878 /* PID unsupported */
883 /* OUI unsupported */
889 static int fr_rx(struct sk_buff
*skb
)
891 struct net_device
*frad
= skb
->dev
;
892 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
893 struct fr_hdr
*fh
= (struct fr_hdr
*)skb
->data
;
894 u8
*data
= skb
->data
;
896 struct pvc_device
*pvc
;
897 struct net_device
*dev
;
899 if (skb
->len
< 4 || fh
->ea1
|| !fh
->ea2
|| data
[2] != FR_UI
)
902 dlci
= q922_to_dlci(skb
->data
);
904 if ((dlci
== LMI_CCITT_ANSI_DLCI
&&
905 (state(hdlc
)->settings
.lmi
== LMI_ANSI
||
906 state(hdlc
)->settings
.lmi
== LMI_CCITT
)) ||
907 (dlci
== LMI_CISCO_DLCI
&&
908 state(hdlc
)->settings
.lmi
== LMI_CISCO
)) {
909 if (fr_lmi_recv(frad
, skb
))
911 dev_kfree_skb_any(skb
);
912 return NET_RX_SUCCESS
;
915 pvc
= find_pvc(hdlc
, dlci
);
918 netdev_info(frad
, "No PVC for received frame's DLCI %d\n",
924 if (pvc
->state
.fecn
!= fh
->fecn
) {
926 printk(KERN_DEBUG
"%s: DLCI %d FECN O%s\n", frad
->name
,
927 dlci
, fh
->fecn
? "N" : "FF");
929 pvc
->state
.fecn
^= 1;
932 if (pvc
->state
.becn
!= fh
->becn
) {
934 printk(KERN_DEBUG
"%s: DLCI %d BECN O%s\n", frad
->name
,
935 dlci
, fh
->becn
? "N" : "FF");
937 pvc
->state
.becn
^= 1;
940 skb
= skb_share_check(skb
, GFP_ATOMIC
);
942 frad
->stats
.rx_dropped
++;
946 if (data
[3] == NLPID_IP
) {
949 skb_pull(skb
, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
950 skb
->dev
= pvc
->main
;
951 skb
->protocol
= htons(ETH_P_IP
);
952 skb_reset_mac_header(skb
);
954 } else if (data
[3] == NLPID_IPV6
) {
957 skb_pull(skb
, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
958 skb
->dev
= pvc
->main
;
959 skb
->protocol
= htons(ETH_P_IPV6
);
960 skb_reset_mac_header(skb
);
962 } else if (data
[3] == FR_PAD
) {
965 if (data
[4] == NLPID_SNAP
) { /* A SNAP header follows */
967 if (skb
->len
< 5) /* Incomplete SNAP header */
969 if (fr_snap_parse(skb
, pvc
))
976 netdev_info(frad
, "Unsupported protocol, NLPID=%x length=%i\n",
982 dev
->stats
.rx_packets
++; /* PVC traffic */
983 dev
->stats
.rx_bytes
+= skb
->len
;
985 dev
->stats
.rx_compressed
++;
987 return NET_RX_SUCCESS
;
990 frad
->stats
.rx_errors
++; /* Mark error */
992 dev_kfree_skb_any(skb
);
996 static void fr_start(struct net_device
*dev
)
998 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1000 printk(KERN_DEBUG
"fr_start\n");
1002 if (state(hdlc
)->settings
.lmi
!= LMI_NONE
) {
1003 state(hdlc
)->reliable
= 0;
1004 state(hdlc
)->dce_changed
= 1;
1005 state(hdlc
)->request
= 0;
1006 state(hdlc
)->fullrep_sent
= 0;
1007 state(hdlc
)->last_errors
= 0xFFFFFFFF;
1008 state(hdlc
)->n391cnt
= 0;
1009 state(hdlc
)->txseq
= state(hdlc
)->rxseq
= 0;
1011 state(hdlc
)->dev
= dev
;
1012 timer_setup(&state(hdlc
)->timer
, fr_timer
, 0);
1013 /* First poll after 1 s */
1014 state(hdlc
)->timer
.expires
= jiffies
+ HZ
;
1015 add_timer(&state(hdlc
)->timer
);
1017 fr_set_link_state(1, dev
);
1021 static void fr_stop(struct net_device
*dev
)
1023 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1025 printk(KERN_DEBUG
"fr_stop\n");
1027 if (state(hdlc
)->settings
.lmi
!= LMI_NONE
)
1028 del_timer_sync(&state(hdlc
)->timer
);
1029 fr_set_link_state(0, dev
);
1032 static void fr_close(struct net_device
*dev
)
1034 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1035 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
1037 while (pvc
) { /* Shutdown all PVCs for this FRAD */
1039 dev_close(pvc
->main
);
1041 dev_close(pvc
->ether
);
1046 static void pvc_setup(struct net_device
*dev
)
1048 dev
->type
= ARPHRD_DLCI
;
1049 dev
->flags
= IFF_POINTOPOINT
;
1050 dev
->hard_header_len
= 0;
1052 netif_keep_dst(dev
);
1055 static const struct net_device_ops pvc_ops
= {
1056 .ndo_open
= pvc_open
,
1057 .ndo_stop
= pvc_close
,
1058 .ndo_start_xmit
= pvc_xmit
,
1059 .ndo_siocwandev
= pvc_ioctl
,
1062 static int fr_add_pvc(struct net_device
*frad
, unsigned int dlci
, int type
)
1064 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
1065 struct pvc_device
*pvc
;
1066 struct net_device
*dev
;
1069 pvc
= add_pvc(frad
, dlci
);
1071 netdev_warn(frad
, "Memory squeeze on fr_add_pvc()\n");
1075 if (*get_dev_p(pvc
, type
))
1078 used
= pvc_is_used(pvc
);
1080 if (type
== ARPHRD_ETHER
)
1081 dev
= alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN
,
1084 dev
= alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN
, pvc_setup
);
1087 netdev_warn(frad
, "Memory squeeze on fr_pvc()\n");
1088 delete_unused_pvcs(hdlc
);
1092 if (type
== ARPHRD_ETHER
) {
1093 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1094 eth_hw_addr_random(dev
);
1096 __be16 addr
= htons(dlci
);
1098 dev_addr_set(dev
, (u8
*)&addr
);
1099 dlci_to_q922(dev
->broadcast
, dlci
);
1101 dev
->netdev_ops
= &pvc_ops
;
1102 dev
->mtu
= HDLC_MAX_MTU
;
1104 dev
->max_mtu
= HDLC_MAX_MTU
;
1105 dev
->needed_headroom
= 10;
1106 dev
->priv_flags
|= IFF_NO_QUEUE
;
1109 if (register_netdevice(dev
) != 0) {
1111 delete_unused_pvcs(hdlc
);
1115 dev
->needs_free_netdev
= true;
1116 *get_dev_p(pvc
, type
) = dev
;
1118 state(hdlc
)->dce_changed
= 1;
1119 state(hdlc
)->dce_pvc_count
++;
1124 static int fr_del_pvc(hdlc_device
*hdlc
, unsigned int dlci
, int type
)
1126 struct pvc_device
*pvc
;
1127 struct net_device
*dev
;
1129 pvc
= find_pvc(hdlc
, dlci
);
1133 dev
= *get_dev_p(pvc
, type
);
1137 if (dev
->flags
& IFF_UP
)
1138 return -EBUSY
; /* PVC in use */
1140 unregister_netdevice(dev
); /* the destructor will free_netdev(dev) */
1141 *get_dev_p(pvc
, type
) = NULL
;
1143 if (!pvc_is_used(pvc
)) {
1144 state(hdlc
)->dce_pvc_count
--;
1145 state(hdlc
)->dce_changed
= 1;
1147 delete_unused_pvcs(hdlc
);
1151 static void fr_destroy(struct net_device
*frad
)
1153 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
1154 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
1156 state(hdlc
)->first_pvc
= NULL
; /* All PVCs destroyed */
1157 state(hdlc
)->dce_pvc_count
= 0;
1158 state(hdlc
)->dce_changed
= 1;
1161 struct pvc_device
*next
= pvc
->next
;
1162 /* destructors will free_netdev() main and ether */
1164 unregister_netdevice(pvc
->main
);
1167 unregister_netdevice(pvc
->ether
);
1174 static struct hdlc_proto proto
= {
1178 .detach
= fr_destroy
,
1181 .module
= THIS_MODULE
,
1184 static int fr_ioctl(struct net_device
*dev
, struct if_settings
*ifs
)
1186 fr_proto __user
*fr_s
= ifs
->ifs_ifsu
.fr
;
1187 const size_t size
= sizeof(fr_proto
);
1188 fr_proto new_settings
;
1189 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1193 switch (ifs
->type
) {
1195 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1197 ifs
->type
= IF_PROTO_FR
;
1198 if (ifs
->size
< size
) {
1199 ifs
->size
= size
; /* data size wanted */
1202 if (copy_to_user(fr_s
, &state(hdlc
)->settings
, size
))
1207 if (!capable(CAP_NET_ADMIN
))
1210 if (dev
->flags
& IFF_UP
)
1213 if (copy_from_user(&new_settings
, fr_s
, size
))
1216 if (new_settings
.lmi
== LMI_DEFAULT
)
1217 new_settings
.lmi
= LMI_ANSI
;
1219 if ((new_settings
.lmi
!= LMI_NONE
&&
1220 new_settings
.lmi
!= LMI_ANSI
&&
1221 new_settings
.lmi
!= LMI_CCITT
&&
1222 new_settings
.lmi
!= LMI_CISCO
) ||
1223 new_settings
.t391
< 1 ||
1224 new_settings
.t392
< 2 ||
1225 new_settings
.n391
< 1 ||
1226 new_settings
.n392
< 1 ||
1227 new_settings
.n393
< new_settings
.n392
||
1228 new_settings
.n393
> 32 ||
1229 (new_settings
.dce
!= 0 &&
1230 new_settings
.dce
!= 1))
1233 result
= hdlc
->attach(dev
, ENCODING_NRZ
,
1234 PARITY_CRC16_PR1_CCITT
);
1238 if (dev_to_hdlc(dev
)->proto
!= &proto
) { /* Different proto */
1239 result
= attach_hdlc_protocol(dev
, &proto
,
1240 sizeof(struct frad_state
));
1243 state(hdlc
)->first_pvc
= NULL
;
1244 state(hdlc
)->dce_pvc_count
= 0;
1246 memcpy(&state(hdlc
)->settings
, &new_settings
, size
);
1247 dev
->type
= ARPHRD_FRAD
;
1248 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE
, dev
);
1251 case IF_PROTO_FR_ADD_PVC
:
1252 case IF_PROTO_FR_DEL_PVC
:
1253 case IF_PROTO_FR_ADD_ETH_PVC
:
1254 case IF_PROTO_FR_DEL_ETH_PVC
:
1255 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1258 if (!capable(CAP_NET_ADMIN
))
1261 if (copy_from_user(&pvc
, ifs
->ifs_ifsu
.fr_pvc
,
1262 sizeof(fr_proto_pvc
)))
1265 if (pvc
.dlci
<= 0 || pvc
.dlci
>= 1024)
1266 return -EINVAL
; /* Only 10 bits, DLCI 0 reserved */
1268 if (ifs
->type
== IF_PROTO_FR_ADD_ETH_PVC
||
1269 ifs
->type
== IF_PROTO_FR_DEL_ETH_PVC
)
1270 result
= ARPHRD_ETHER
; /* bridged Ethernet device */
1272 result
= ARPHRD_DLCI
;
1274 if (ifs
->type
== IF_PROTO_FR_ADD_PVC
||
1275 ifs
->type
== IF_PROTO_FR_ADD_ETH_PVC
)
1276 return fr_add_pvc(dev
, pvc
.dlci
, result
);
1278 return fr_del_pvc(hdlc
, pvc
.dlci
, result
);
1284 static int __init
hdlc_fr_init(void)
1286 register_hdlc_protocol(&proto
);
1290 static void __exit
hdlc_fr_exit(void)
1292 unregister_hdlc_protocol(&proto
);
1295 module_init(hdlc_fr_init
);
1296 module_exit(hdlc_fr_exit
);
1298 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1299 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1300 MODULE_LICENSE("GPL v2");