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_p
, u16 dlci
)
277 struct sk_buff
*skb
= *skb_p
;
279 switch (skb
->protocol
) {
280 case cpu_to_be16(NLPID_CCITT_ANSI_LMI
):
282 skb_push(skb
, head_len
);
283 skb
->data
[3] = NLPID_CCITT_ANSI_LMI
;
286 case cpu_to_be16(NLPID_CISCO_LMI
):
288 skb_push(skb
, head_len
);
289 skb
->data
[3] = NLPID_CISCO_LMI
;
292 case cpu_to_be16(ETH_P_IP
):
294 skb_push(skb
, head_len
);
295 skb
->data
[3] = NLPID_IP
;
298 case cpu_to_be16(ETH_P_IPV6
):
300 skb_push(skb
, head_len
);
301 skb
->data
[3] = NLPID_IPV6
;
304 case cpu_to_be16(ETH_P_802_3
):
306 if (skb_headroom(skb
) < head_len
) {
307 struct sk_buff
*skb2
= skb_realloc_headroom(skb
,
314 skb_push(skb
, head_len
);
315 skb
->data
[3] = FR_PAD
;
316 skb
->data
[4] = NLPID_SNAP
;
317 skb
->data
[5] = FR_PAD
;
321 skb
->data
[9] = 0x07; /* bridged Ethernet frame w/out FCS */
326 skb_push(skb
, head_len
);
327 skb
->data
[3] = FR_PAD
;
328 skb
->data
[4] = NLPID_SNAP
;
329 skb
->data
[5] = FR_PAD
;
330 skb
->data
[6] = FR_PAD
;
331 skb
->data
[7] = FR_PAD
;
332 *(__be16
*)(skb
->data
+ 8) = skb
->protocol
;
335 dlci_to_q922(skb
->data
, dlci
);
336 skb
->data
[2] = FR_UI
;
342 static int pvc_open(struct net_device
*dev
)
344 struct pvc_device
*pvc
= dev
->ml_priv
;
346 if ((pvc
->frad
->flags
& IFF_UP
) == 0)
347 return -EIO
; /* Frad must be UP in order to activate PVC */
349 if (pvc
->open_count
++ == 0) {
350 hdlc_device
*hdlc
= dev_to_hdlc(pvc
->frad
);
351 if (state(hdlc
)->settings
.lmi
== LMI_NONE
)
352 pvc
->state
.active
= netif_carrier_ok(pvc
->frad
);
354 pvc_carrier(pvc
->state
.active
, pvc
);
355 state(hdlc
)->dce_changed
= 1;
362 static int pvc_close(struct net_device
*dev
)
364 struct pvc_device
*pvc
= dev
->ml_priv
;
366 if (--pvc
->open_count
== 0) {
367 hdlc_device
*hdlc
= dev_to_hdlc(pvc
->frad
);
368 if (state(hdlc
)->settings
.lmi
== LMI_NONE
)
369 pvc
->state
.active
= 0;
371 if (state(hdlc
)->settings
.dce
) {
372 state(hdlc
)->dce_changed
= 1;
373 pvc
->state
.active
= 0;
381 static int pvc_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
383 struct pvc_device
*pvc
= dev
->ml_priv
;
384 fr_proto_pvc_info info
;
386 if (ifr
->ifr_settings
.type
== IF_GET_PROTO
) {
387 if (dev
->type
== ARPHRD_ETHER
)
388 ifr
->ifr_settings
.type
= IF_PROTO_FR_ETH_PVC
;
390 ifr
->ifr_settings
.type
= IF_PROTO_FR_PVC
;
392 if (ifr
->ifr_settings
.size
< sizeof(info
)) {
393 /* data size wanted */
394 ifr
->ifr_settings
.size
= sizeof(info
);
398 info
.dlci
= pvc
->dlci
;
399 memcpy(info
.master
, pvc
->frad
->name
, IFNAMSIZ
);
400 if (copy_to_user(ifr
->ifr_settings
.ifs_ifsu
.fr_pvc_info
,
401 &info
, sizeof(info
)))
409 static netdev_tx_t
pvc_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
411 struct pvc_device
*pvc
= dev
->ml_priv
;
413 if (pvc
->state
.active
) {
414 if (dev
->type
== ARPHRD_ETHER
) {
415 int pad
= ETH_ZLEN
- skb
->len
;
416 if (pad
> 0) { /* Pad the frame with zeros */
418 if (skb_tailroom(skb
) < pad
)
419 if (pskb_expand_head(skb
, 0, pad
,
421 dev
->stats
.tx_dropped
++;
426 memset(skb
->data
+ len
, 0, pad
);
428 skb
->protocol
= cpu_to_be16(ETH_P_802_3
);
430 if (!fr_hard_header(&skb
, pvc
->dlci
)) {
431 dev
->stats
.tx_bytes
+= skb
->len
;
432 dev
->stats
.tx_packets
++;
433 if (pvc
->state
.fecn
) /* TX Congestion counter */
434 dev
->stats
.tx_compressed
++;
435 skb
->dev
= pvc
->frad
;
441 dev
->stats
.tx_dropped
++;
446 static inline void fr_log_dlci_active(struct pvc_device
*pvc
)
448 netdev_info(pvc
->frad
, "DLCI %d [%s%s%s]%s %s\n",
450 pvc
->main
? pvc
->main
->name
: "",
451 pvc
->main
&& pvc
->ether
? " " : "",
452 pvc
->ether
? pvc
->ether
->name
: "",
453 pvc
->state
.new ? " new" : "",
454 !pvc
->state
.exist
? "deleted" :
455 pvc
->state
.active
? "active" : "inactive");
460 static inline u8
fr_lmi_nextseq(u8 x
)
467 static void fr_lmi_send(struct net_device
*dev
, int fullrep
)
469 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
471 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
472 int lmi
= state(hdlc
)->settings
.lmi
;
473 int dce
= state(hdlc
)->settings
.dce
;
474 int len
= lmi
== LMI_ANSI
? LMI_ANSI_LENGTH
: LMI_CCITT_CISCO_LENGTH
;
475 int stat_len
= (lmi
== LMI_CISCO
) ? 6 : 3;
479 if (dce
&& fullrep
) {
480 len
+= state(hdlc
)->dce_pvc_count
* (2 + stat_len
);
481 if (len
> HDLC_MAX_MRU
) {
482 netdev_warn(dev
, "Too many PVCs while sending LMI full report\n");
487 skb
= dev_alloc_skb(len
);
489 netdev_warn(dev
, "Memory squeeze on fr_lmi_send()\n");
492 memset(skb
->data
, 0, len
);
494 if (lmi
== LMI_CISCO
) {
495 skb
->protocol
= cpu_to_be16(NLPID_CISCO_LMI
);
496 fr_hard_header(&skb
, LMI_CISCO_DLCI
);
498 skb
->protocol
= cpu_to_be16(NLPID_CCITT_ANSI_LMI
);
499 fr_hard_header(&skb
, LMI_CCITT_ANSI_DLCI
);
501 data
= skb_tail_pointer(skb
);
502 data
[i
++] = LMI_CALLREF
;
503 data
[i
++] = dce
? LMI_STATUS
: LMI_STATUS_ENQUIRY
;
505 data
[i
++] = LMI_ANSI_LOCKSHIFT
;
506 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_REPTYPE
:
507 LMI_ANSI_CISCO_REPTYPE
;
508 data
[i
++] = LMI_REPT_LEN
;
509 data
[i
++] = fullrep
? LMI_FULLREP
: LMI_INTEGRITY
;
510 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_ALIVE
: LMI_ANSI_CISCO_ALIVE
;
511 data
[i
++] = LMI_INTEG_LEN
;
512 data
[i
++] = state(hdlc
)->txseq
=
513 fr_lmi_nextseq(state(hdlc
)->txseq
);
514 data
[i
++] = state(hdlc
)->rxseq
;
516 if (dce
&& fullrep
) {
518 data
[i
++] = lmi
== LMI_CCITT
? LMI_CCITT_PVCSTAT
:
519 LMI_ANSI_CISCO_PVCSTAT
;
520 data
[i
++] = stat_len
;
522 /* LMI start/restart */
523 if (state(hdlc
)->reliable
&& !pvc
->state
.exist
) {
524 pvc
->state
.exist
= pvc
->state
.new = 1;
525 fr_log_dlci_active(pvc
);
528 /* ifconfig PVC up */
529 if (pvc
->open_count
&& !pvc
->state
.active
&&
530 pvc
->state
.exist
&& !pvc
->state
.new) {
532 pvc
->state
.active
= 1;
533 fr_log_dlci_active(pvc
);
536 if (lmi
== LMI_CISCO
) {
537 data
[i
] = pvc
->dlci
>> 8;
538 data
[i
+ 1] = pvc
->dlci
& 0xFF;
540 data
[i
] = (pvc
->dlci
>> 4) & 0x3F;
541 data
[i
+ 1] = ((pvc
->dlci
<< 3) & 0x78) | 0x80;
547 else if (pvc
->state
.active
)
556 skb
->priority
= TC_PRIO_CONTROL
;
558 skb_reset_network_header(skb
);
565 static void fr_set_link_state(int reliable
, struct net_device
*dev
)
567 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
568 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
570 state(hdlc
)->reliable
= reliable
;
572 netif_dormant_off(dev
);
573 state(hdlc
)->n391cnt
= 0; /* Request full status */
574 state(hdlc
)->dce_changed
= 1;
576 if (state(hdlc
)->settings
.lmi
== LMI_NONE
) {
577 while (pvc
) { /* Activate all PVCs */
579 pvc
->state
.exist
= pvc
->state
.active
= 1;
585 netif_dormant_on(dev
);
586 while (pvc
) { /* Deactivate all PVCs */
588 pvc
->state
.exist
= pvc
->state
.active
= 0;
590 if (!state(hdlc
)->settings
.dce
)
591 pvc
->state
.bandwidth
= 0;
598 static void fr_timer(struct timer_list
*t
)
600 struct frad_state
*st
= from_timer(st
, t
, timer
);
601 struct net_device
*dev
= st
->dev
;
602 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
603 int i
, cnt
= 0, reliable
;
606 if (state(hdlc
)->settings
.dce
) {
607 reliable
= state(hdlc
)->request
&&
608 time_before(jiffies
, state(hdlc
)->last_poll
+
609 state(hdlc
)->settings
.t392
* HZ
);
610 state(hdlc
)->request
= 0;
612 state(hdlc
)->last_errors
<<= 1; /* Shift the list */
613 if (state(hdlc
)->request
) {
614 if (state(hdlc
)->reliable
)
615 netdev_info(dev
, "No LMI status reply received\n");
616 state(hdlc
)->last_errors
|= 1;
619 list
= state(hdlc
)->last_errors
;
620 for (i
= 0; i
< state(hdlc
)->settings
.n393
; i
++, list
>>= 1)
621 cnt
+= (list
& 1); /* errors count */
623 reliable
= (cnt
< state(hdlc
)->settings
.n392
);
626 if (state(hdlc
)->reliable
!= reliable
) {
627 netdev_info(dev
, "Link %sreliable\n", reliable
? "" : "un");
628 fr_set_link_state(reliable
, dev
);
631 if (state(hdlc
)->settings
.dce
)
632 state(hdlc
)->timer
.expires
= jiffies
+
633 state(hdlc
)->settings
.t392
* HZ
;
635 if (state(hdlc
)->n391cnt
)
636 state(hdlc
)->n391cnt
--;
638 fr_lmi_send(dev
, state(hdlc
)->n391cnt
== 0);
640 state(hdlc
)->last_poll
= jiffies
;
641 state(hdlc
)->request
= 1;
642 state(hdlc
)->timer
.expires
= jiffies
+
643 state(hdlc
)->settings
.t391
* HZ
;
646 add_timer(&state(hdlc
)->timer
);
650 static int fr_lmi_recv(struct net_device
*dev
, struct sk_buff
*skb
)
652 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
653 struct pvc_device
*pvc
;
655 int lmi
= state(hdlc
)->settings
.lmi
;
656 int dce
= state(hdlc
)->settings
.dce
;
657 int stat_len
= (lmi
== LMI_CISCO
) ? 6 : 3, reptype
, error
, no_ram
, i
;
659 if (skb
->len
< (lmi
== LMI_ANSI
? LMI_ANSI_LENGTH
:
660 LMI_CCITT_CISCO_LENGTH
)) {
661 netdev_info(dev
, "Short LMI frame\n");
665 if (skb
->data
[3] != (lmi
== LMI_CISCO
? NLPID_CISCO_LMI
:
666 NLPID_CCITT_ANSI_LMI
)) {
667 netdev_info(dev
, "Received non-LMI frame with LMI DLCI\n");
671 if (skb
->data
[4] != LMI_CALLREF
) {
672 netdev_info(dev
, "Invalid LMI Call reference (0x%02X)\n",
677 if (skb
->data
[5] != (dce
? LMI_STATUS_ENQUIRY
: LMI_STATUS
)) {
678 netdev_info(dev
, "Invalid LMI Message type (0x%02X)\n",
683 if (lmi
== LMI_ANSI
) {
684 if (skb
->data
[6] != LMI_ANSI_LOCKSHIFT
) {
685 netdev_info(dev
, "Not ANSI locking shift in LMI message (0x%02X)\n",
693 if (skb
->data
[i
] != (lmi
== LMI_CCITT
? LMI_CCITT_REPTYPE
:
694 LMI_ANSI_CISCO_REPTYPE
)) {
695 netdev_info(dev
, "Not an LMI Report type IE (0x%02X)\n",
700 if (skb
->data
[++i
] != LMI_REPT_LEN
) {
701 netdev_info(dev
, "Invalid LMI Report type IE length (%u)\n",
706 reptype
= skb
->data
[++i
];
707 if (reptype
!= LMI_INTEGRITY
&& reptype
!= LMI_FULLREP
) {
708 netdev_info(dev
, "Unsupported LMI Report type (0x%02X)\n",
713 if (skb
->data
[++i
] != (lmi
== LMI_CCITT
? LMI_CCITT_ALIVE
:
714 LMI_ANSI_CISCO_ALIVE
)) {
715 netdev_info(dev
, "Not an LMI Link integrity verification IE (0x%02X)\n",
720 if (skb
->data
[++i
] != LMI_INTEG_LEN
) {
721 netdev_info(dev
, "Invalid LMI Link integrity verification IE length (%u)\n",
727 state(hdlc
)->rxseq
= skb
->data
[i
++]; /* TX sequence from peer */
728 rxseq
= skb
->data
[i
++]; /* Should confirm our sequence */
730 txseq
= state(hdlc
)->txseq
;
733 state(hdlc
)->last_poll
= jiffies
;
736 if (!state(hdlc
)->reliable
)
739 if (rxseq
== 0 || rxseq
!= txseq
) { /* Ask for full report next time */
740 state(hdlc
)->n391cnt
= 0;
745 if (state(hdlc
)->fullrep_sent
&& !error
) {
746 /* Stop sending full report - the last one has been confirmed by DTE */
747 state(hdlc
)->fullrep_sent
= 0;
748 pvc
= state(hdlc
)->first_pvc
;
750 if (pvc
->state
.new) {
753 /* Tell DTE that new PVC is now active */
754 state(hdlc
)->dce_changed
= 1;
760 if (state(hdlc
)->dce_changed
) {
761 reptype
= LMI_FULLREP
;
762 state(hdlc
)->fullrep_sent
= 1;
763 state(hdlc
)->dce_changed
= 0;
766 state(hdlc
)->request
= 1; /* got request */
767 fr_lmi_send(dev
, reptype
== LMI_FULLREP
? 1 : 0);
773 state(hdlc
)->request
= 0; /* got response, no request pending */
778 if (reptype
!= LMI_FULLREP
)
781 pvc
= state(hdlc
)->first_pvc
;
784 pvc
->state
.deleted
= 1;
789 while (skb
->len
>= i
+ 2 + stat_len
) {
792 unsigned int active
, new;
794 if (skb
->data
[i
] != (lmi
== LMI_CCITT
? LMI_CCITT_PVCSTAT
:
795 LMI_ANSI_CISCO_PVCSTAT
)) {
796 netdev_info(dev
, "Not an LMI PVC status IE (0x%02X)\n",
801 if (skb
->data
[++i
] != stat_len
) {
802 netdev_info(dev
, "Invalid LMI PVC status IE length (%u)\n",
808 new = !! (skb
->data
[i
+ 2] & 0x08);
809 active
= !! (skb
->data
[i
+ 2] & 0x02);
810 if (lmi
== LMI_CISCO
) {
811 dlci
= (skb
->data
[i
] << 8) | skb
->data
[i
+ 1];
812 bw
= (skb
->data
[i
+ 3] << 16) |
813 (skb
->data
[i
+ 4] << 8) |
816 dlci
= ((skb
->data
[i
] & 0x3F) << 4) |
817 ((skb
->data
[i
+ 1] & 0x78) >> 3);
821 pvc
= add_pvc(dev
, dlci
);
823 if (!pvc
&& !no_ram
) {
824 netdev_warn(dev
, "Memory squeeze on fr_lmi_recv()\n");
829 pvc
->state
.exist
= 1;
830 pvc
->state
.deleted
= 0;
831 if (active
!= pvc
->state
.active
||
832 new != pvc
->state
.new ||
833 bw
!= pvc
->state
.bandwidth
||
835 pvc
->state
.new = new;
836 pvc
->state
.active
= active
;
837 pvc
->state
.bandwidth
= bw
;
838 pvc_carrier(active
, pvc
);
839 fr_log_dlci_active(pvc
);
846 pvc
= state(hdlc
)->first_pvc
;
849 if (pvc
->state
.deleted
&& pvc
->state
.exist
) {
851 pvc
->state
.active
= pvc
->state
.new = 0;
852 pvc
->state
.exist
= 0;
853 pvc
->state
.bandwidth
= 0;
854 fr_log_dlci_active(pvc
);
859 /* Next full report after N391 polls */
860 state(hdlc
)->n391cnt
= state(hdlc
)->settings
.n391
;
866 static int fr_rx(struct sk_buff
*skb
)
868 struct net_device
*frad
= skb
->dev
;
869 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
870 struct fr_hdr
*fh
= (struct fr_hdr
*)skb
->data
;
871 u8
*data
= skb
->data
;
873 struct pvc_device
*pvc
;
874 struct net_device
*dev
= NULL
;
876 if (skb
->len
<= 4 || fh
->ea1
|| data
[2] != FR_UI
)
879 dlci
= q922_to_dlci(skb
->data
);
881 if ((dlci
== LMI_CCITT_ANSI_DLCI
&&
882 (state(hdlc
)->settings
.lmi
== LMI_ANSI
||
883 state(hdlc
)->settings
.lmi
== LMI_CCITT
)) ||
884 (dlci
== LMI_CISCO_DLCI
&&
885 state(hdlc
)->settings
.lmi
== LMI_CISCO
)) {
886 if (fr_lmi_recv(frad
, skb
))
888 dev_kfree_skb_any(skb
);
889 return NET_RX_SUCCESS
;
892 pvc
= find_pvc(hdlc
, dlci
);
895 netdev_info(frad
, "No PVC for received frame's DLCI %d\n",
898 dev_kfree_skb_any(skb
);
902 if (pvc
->state
.fecn
!= fh
->fecn
) {
904 printk(KERN_DEBUG
"%s: DLCI %d FECN O%s\n", frad
->name
,
905 dlci
, fh
->fecn
? "N" : "FF");
907 pvc
->state
.fecn
^= 1;
910 if (pvc
->state
.becn
!= fh
->becn
) {
912 printk(KERN_DEBUG
"%s: DLCI %d BECN O%s\n", frad
->name
,
913 dlci
, fh
->becn
? "N" : "FF");
915 pvc
->state
.becn
^= 1;
919 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
920 frad
->stats
.rx_dropped
++;
924 if (data
[3] == NLPID_IP
) {
925 skb_pull(skb
, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
927 skb
->protocol
= htons(ETH_P_IP
);
929 } else if (data
[3] == NLPID_IPV6
) {
930 skb_pull(skb
, 4); /* Remove 4-byte header (hdr, UI, NLPID) */
932 skb
->protocol
= htons(ETH_P_IPV6
);
934 } else if (skb
->len
> 10 && data
[3] == FR_PAD
&&
935 data
[4] == NLPID_SNAP
&& data
[5] == FR_PAD
) {
936 u16 oui
= ntohs(*(__be16
*)(data
+ 6));
937 u16 pid
= ntohs(*(__be16
*)(data
+ 8));
940 switch ((((u32
)oui
) << 16) | pid
) {
941 case ETH_P_ARP
: /* routed frame with SNAP */
943 case ETH_P_IP
: /* a long variant */
946 skb
->protocol
= htons(pid
);
949 case 0x80C20007: /* bridged Ethernet frame */
950 if ((dev
= pvc
->ether
) != NULL
)
951 skb
->protocol
= eth_type_trans(skb
, dev
);
955 netdev_info(frad
, "Unsupported protocol, OUI=%x PID=%x\n",
957 dev_kfree_skb_any(skb
);
961 netdev_info(frad
, "Unsupported protocol, NLPID=%x length=%i\n",
963 dev_kfree_skb_any(skb
);
968 dev
->stats
.rx_packets
++; /* PVC traffic */
969 dev
->stats
.rx_bytes
+= skb
->len
;
971 dev
->stats
.rx_compressed
++;
974 return NET_RX_SUCCESS
;
976 dev_kfree_skb_any(skb
);
981 frad
->stats
.rx_errors
++; /* Mark error */
982 dev_kfree_skb_any(skb
);
988 static void fr_start(struct net_device
*dev
)
990 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
992 printk(KERN_DEBUG
"fr_start\n");
994 if (state(hdlc
)->settings
.lmi
!= LMI_NONE
) {
995 state(hdlc
)->reliable
= 0;
996 state(hdlc
)->dce_changed
= 1;
997 state(hdlc
)->request
= 0;
998 state(hdlc
)->fullrep_sent
= 0;
999 state(hdlc
)->last_errors
= 0xFFFFFFFF;
1000 state(hdlc
)->n391cnt
= 0;
1001 state(hdlc
)->txseq
= state(hdlc
)->rxseq
= 0;
1003 state(hdlc
)->dev
= dev
;
1004 timer_setup(&state(hdlc
)->timer
, fr_timer
, 0);
1005 /* First poll after 1 s */
1006 state(hdlc
)->timer
.expires
= jiffies
+ HZ
;
1007 add_timer(&state(hdlc
)->timer
);
1009 fr_set_link_state(1, dev
);
1013 static void fr_stop(struct net_device
*dev
)
1015 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1017 printk(KERN_DEBUG
"fr_stop\n");
1019 if (state(hdlc
)->settings
.lmi
!= LMI_NONE
)
1020 del_timer_sync(&state(hdlc
)->timer
);
1021 fr_set_link_state(0, dev
);
1025 static void fr_close(struct net_device
*dev
)
1027 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1028 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
1030 while (pvc
) { /* Shutdown all PVCs for this FRAD */
1032 dev_close(pvc
->main
);
1034 dev_close(pvc
->ether
);
1040 static void pvc_setup(struct net_device
*dev
)
1042 dev
->type
= ARPHRD_DLCI
;
1043 dev
->flags
= IFF_POINTOPOINT
;
1044 dev
->hard_header_len
= 10;
1046 netif_keep_dst(dev
);
1049 static const struct net_device_ops pvc_ops
= {
1050 .ndo_open
= pvc_open
,
1051 .ndo_stop
= pvc_close
,
1052 .ndo_start_xmit
= pvc_xmit
,
1053 .ndo_do_ioctl
= pvc_ioctl
,
1056 static int fr_add_pvc(struct net_device
*frad
, unsigned int dlci
, int type
)
1058 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
1059 struct pvc_device
*pvc
;
1060 struct net_device
*dev
;
1063 if ((pvc
= add_pvc(frad
, dlci
)) == NULL
) {
1064 netdev_warn(frad
, "Memory squeeze on fr_add_pvc()\n");
1068 if (*get_dev_p(pvc
, type
))
1071 used
= pvc_is_used(pvc
);
1073 if (type
== ARPHRD_ETHER
)
1074 dev
= alloc_netdev(0, "pvceth%d", NET_NAME_UNKNOWN
,
1077 dev
= alloc_netdev(0, "pvc%d", NET_NAME_UNKNOWN
, pvc_setup
);
1080 netdev_warn(frad
, "Memory squeeze on fr_pvc()\n");
1081 delete_unused_pvcs(hdlc
);
1085 if (type
== ARPHRD_ETHER
) {
1086 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1087 eth_hw_addr_random(dev
);
1089 *(__be16
*)dev
->dev_addr
= htons(dlci
);
1090 dlci_to_q922(dev
->broadcast
, dlci
);
1092 dev
->netdev_ops
= &pvc_ops
;
1093 dev
->mtu
= HDLC_MAX_MTU
;
1095 dev
->max_mtu
= HDLC_MAX_MTU
;
1096 dev
->priv_flags
|= IFF_NO_QUEUE
;
1099 if (register_netdevice(dev
) != 0) {
1101 delete_unused_pvcs(hdlc
);
1105 dev
->needs_free_netdev
= true;
1106 *get_dev_p(pvc
, type
) = dev
;
1108 state(hdlc
)->dce_changed
= 1;
1109 state(hdlc
)->dce_pvc_count
++;
1116 static int fr_del_pvc(hdlc_device
*hdlc
, unsigned int dlci
, int type
)
1118 struct pvc_device
*pvc
;
1119 struct net_device
*dev
;
1121 if ((pvc
= find_pvc(hdlc
, dlci
)) == NULL
)
1124 if ((dev
= *get_dev_p(pvc
, type
)) == NULL
)
1127 if (dev
->flags
& IFF_UP
)
1128 return -EBUSY
; /* PVC in use */
1130 unregister_netdevice(dev
); /* the destructor will free_netdev(dev) */
1131 *get_dev_p(pvc
, type
) = NULL
;
1133 if (!pvc_is_used(pvc
)) {
1134 state(hdlc
)->dce_pvc_count
--;
1135 state(hdlc
)->dce_changed
= 1;
1137 delete_unused_pvcs(hdlc
);
1143 static void fr_destroy(struct net_device
*frad
)
1145 hdlc_device
*hdlc
= dev_to_hdlc(frad
);
1146 struct pvc_device
*pvc
= state(hdlc
)->first_pvc
;
1147 state(hdlc
)->first_pvc
= NULL
; /* All PVCs destroyed */
1148 state(hdlc
)->dce_pvc_count
= 0;
1149 state(hdlc
)->dce_changed
= 1;
1152 struct pvc_device
*next
= pvc
->next
;
1153 /* destructors will free_netdev() main and ether */
1155 unregister_netdevice(pvc
->main
);
1158 unregister_netdevice(pvc
->ether
);
1166 static struct hdlc_proto proto
= {
1170 .detach
= fr_destroy
,
1173 .module
= THIS_MODULE
,
1177 static int fr_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
1179 fr_proto __user
*fr_s
= ifr
->ifr_settings
.ifs_ifsu
.fr
;
1180 const size_t size
= sizeof(fr_proto
);
1181 fr_proto new_settings
;
1182 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
1186 switch (ifr
->ifr_settings
.type
) {
1188 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1190 ifr
->ifr_settings
.type
= IF_PROTO_FR
;
1191 if (ifr
->ifr_settings
.size
< size
) {
1192 ifr
->ifr_settings
.size
= size
; /* data size wanted */
1195 if (copy_to_user(fr_s
, &state(hdlc
)->settings
, size
))
1200 if (!capable(CAP_NET_ADMIN
))
1203 if (dev
->flags
& IFF_UP
)
1206 if (copy_from_user(&new_settings
, fr_s
, size
))
1209 if (new_settings
.lmi
== LMI_DEFAULT
)
1210 new_settings
.lmi
= LMI_ANSI
;
1212 if ((new_settings
.lmi
!= LMI_NONE
&&
1213 new_settings
.lmi
!= LMI_ANSI
&&
1214 new_settings
.lmi
!= LMI_CCITT
&&
1215 new_settings
.lmi
!= LMI_CISCO
) ||
1216 new_settings
.t391
< 1 ||
1217 new_settings
.t392
< 2 ||
1218 new_settings
.n391
< 1 ||
1219 new_settings
.n392
< 1 ||
1220 new_settings
.n393
< new_settings
.n392
||
1221 new_settings
.n393
> 32 ||
1222 (new_settings
.dce
!= 0 &&
1223 new_settings
.dce
!= 1))
1226 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
1230 if (dev_to_hdlc(dev
)->proto
!= &proto
) { /* Different proto */
1231 result
= attach_hdlc_protocol(dev
, &proto
,
1232 sizeof(struct frad_state
));
1235 state(hdlc
)->first_pvc
= NULL
;
1236 state(hdlc
)->dce_pvc_count
= 0;
1238 memcpy(&state(hdlc
)->settings
, &new_settings
, size
);
1239 dev
->type
= ARPHRD_FRAD
;
1240 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE
, dev
);
1243 case IF_PROTO_FR_ADD_PVC
:
1244 case IF_PROTO_FR_DEL_PVC
:
1245 case IF_PROTO_FR_ADD_ETH_PVC
:
1246 case IF_PROTO_FR_DEL_ETH_PVC
:
1247 if (dev_to_hdlc(dev
)->proto
!= &proto
) /* Different proto */
1250 if (!capable(CAP_NET_ADMIN
))
1253 if (copy_from_user(&pvc
, ifr
->ifr_settings
.ifs_ifsu
.fr_pvc
,
1254 sizeof(fr_proto_pvc
)))
1257 if (pvc
.dlci
<= 0 || pvc
.dlci
>= 1024)
1258 return -EINVAL
; /* Only 10 bits, DLCI 0 reserved */
1260 if (ifr
->ifr_settings
.type
== IF_PROTO_FR_ADD_ETH_PVC
||
1261 ifr
->ifr_settings
.type
== IF_PROTO_FR_DEL_ETH_PVC
)
1262 result
= ARPHRD_ETHER
; /* bridged Ethernet device */
1264 result
= ARPHRD_DLCI
;
1266 if (ifr
->ifr_settings
.type
== IF_PROTO_FR_ADD_PVC
||
1267 ifr
->ifr_settings
.type
== IF_PROTO_FR_ADD_ETH_PVC
)
1268 return fr_add_pvc(dev
, pvc
.dlci
, result
);
1270 return fr_del_pvc(hdlc
, pvc
.dlci
, result
);
1277 static int __init
mod_init(void)
1279 register_hdlc_protocol(&proto
);
1284 static void __exit
mod_exit(void)
1286 unregister_hdlc_protocol(&proto
);
1290 module_init(mod_init
);
1291 module_exit(mod_exit
);
1293 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
1294 MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC");
1295 MODULE_LICENSE("GPL v2");