1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic HDLC support routines for Linux
4 * Point-to-point protocol support
6 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
9 #include <linux/errno.h>
10 #include <linux/hdlc.h>
11 #include <linux/if_arp.h>
12 #include <linux/inetdevice.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pkt_sched.h>
17 #include <linux/poll.h>
18 #include <linux/skbuff.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
22 #define DEBUG_CP 0 /* also bytes# to dump */
24 #define DEBUG_HARD_HEADER 0
26 #define HDLC_ADDR_ALLSTATIONS 0xFF
27 #define HDLC_CTRL_UI 0x03
29 #define PID_LCP 0xC021
31 #define PID_IPCP 0x8021
32 #define PID_IPV6 0x0057
33 #define PID_IPV6CP 0x8057
35 enum {IDX_LCP
= 0, IDX_IPCP
, IDX_IPV6CP
, IDX_COUNT
};
36 enum {CP_CONF_REQ
= 1, CP_CONF_ACK
, CP_CONF_NAK
, CP_CONF_REJ
, CP_TERM_REQ
,
37 CP_TERM_ACK
, CP_CODE_REJ
, LCP_PROTO_REJ
, LCP_ECHO_REQ
, LCP_ECHO_REPLY
,
38 LCP_DISC_REQ
, CP_CODES
};
40 static const char *const code_names
[CP_CODES
] = {
41 "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
42 "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
44 static char debug_buffer
[64 + 3 * DEBUG_CP
];
47 enum {LCP_OPTION_MRU
= 1, LCP_OPTION_ACCM
, LCP_OPTION_MAGIC
= 5};
63 struct net_device
*dev
;
64 struct timer_list timer
;
65 unsigned long timeout
;
66 u16 pid
; /* protocol ID */
68 u8 cr_id
; /* ID of last Configuration-Request */
73 struct proto protos
[IDX_COUNT
];
75 unsigned long last_pong
;
76 unsigned int req_timeout
, cr_retries
, term_retries
;
77 unsigned int keepalive_interval
, keepalive_timeout
;
78 u8 seq
; /* local sequence number for requests */
79 u8 echo_id
; /* ID of last Echo-Request (LCP) */
82 enum {CLOSED
= 0, STOPPED
, STOPPING
, REQ_SENT
, ACK_RECV
, ACK_SENT
, OPENED
,
83 STATES
, STATE_MASK
= 0xF};
84 enum {START
= 0, STOP
, TO_GOOD
, TO_BAD
, RCR_GOOD
, RCR_BAD
, RCA
, RCN
, RTR
, RTA
,
85 RUC
, RXJ_GOOD
, RXJ_BAD
, EVENTS
};
86 enum {INV
= 0x10, IRC
= 0x20, ZRC
= 0x40, SCR
= 0x80, SCA
= 0x100,
87 SCN
= 0x200, STR
= 0x400, STA
= 0x800, SCJ
= 0x1000};
90 static const char *const state_names
[STATES
] = {
91 "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
94 static const char *const event_names
[EVENTS
] = {
95 "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
96 "RTR", "RTA", "RUC", "RXJ+", "RXJ-"
100 static struct sk_buff_head tx_queue
; /* used when holding the spin lock */
102 static int ppp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
);
104 static inline struct ppp
* get_ppp(struct net_device
*dev
)
106 return (struct ppp
*)dev_to_hdlc(dev
)->state
;
109 static inline struct proto
* get_proto(struct net_device
*dev
, u16 pid
)
111 struct ppp
*ppp
= get_ppp(dev
);
115 return &ppp
->protos
[IDX_LCP
];
117 return &ppp
->protos
[IDX_IPCP
];
119 return &ppp
->protos
[IDX_IPV6CP
];
125 static inline const char* proto_name(u16 pid
)
139 static __be16
ppp_type_trans(struct sk_buff
*skb
, struct net_device
*dev
)
141 struct hdlc_header
*data
= (struct hdlc_header
*)skb
->data
;
143 if (skb
->len
< sizeof(struct hdlc_header
))
144 return htons(ETH_P_HDLC
);
145 if (data
->address
!= HDLC_ADDR_ALLSTATIONS
||
146 data
->control
!= HDLC_CTRL_UI
)
147 return htons(ETH_P_HDLC
);
149 switch (data
->protocol
) {
150 case cpu_to_be16(PID_IP
):
151 skb_pull(skb
, sizeof(struct hdlc_header
));
152 return htons(ETH_P_IP
);
154 case cpu_to_be16(PID_IPV6
):
155 skb_pull(skb
, sizeof(struct hdlc_header
));
156 return htons(ETH_P_IPV6
);
159 return htons(ETH_P_HDLC
);
164 static int ppp_hard_header(struct sk_buff
*skb
, struct net_device
*dev
,
165 u16 type
, const void *daddr
, const void *saddr
,
168 struct hdlc_header
*data
;
169 #if DEBUG_HARD_HEADER
170 printk(KERN_DEBUG
"%s: ppp_hard_header() called\n", dev
->name
);
173 skb_push(skb
, sizeof(struct hdlc_header
));
174 data
= (struct hdlc_header
*)skb
->data
;
176 data
->address
= HDLC_ADDR_ALLSTATIONS
;
177 data
->control
= HDLC_CTRL_UI
;
180 data
->protocol
= htons(PID_IP
);
183 data
->protocol
= htons(PID_IPV6
);
188 data
->protocol
= htons(type
);
190 default: /* unknown protocol */
193 return sizeof(struct hdlc_header
);
197 static void ppp_tx_flush(void)
200 while ((skb
= skb_dequeue(&tx_queue
)) != NULL
)
204 static void ppp_tx_cp(struct net_device
*dev
, u16 pid
, u8 code
,
205 u8 id
, unsigned int len
, const void *data
)
208 struct cp_header
*cp
;
209 unsigned int magic_len
= 0;
217 if (pid
== PID_LCP
&& (code
== LCP_ECHO_REQ
|| code
== LCP_ECHO_REPLY
))
218 magic_len
= sizeof(magic
);
220 skb
= dev_alloc_skb(sizeof(struct hdlc_header
) +
221 sizeof(struct cp_header
) + magic_len
+ len
);
223 netdev_warn(dev
, "out of memory in ppp_tx_cp()\n");
226 skb_reserve(skb
, sizeof(struct hdlc_header
));
228 cp
= skb_put(skb
, sizeof(struct cp_header
));
231 cp
->len
= htons(sizeof(struct cp_header
) + magic_len
+ len
);
234 skb_put_data(skb
, &magic
, magic_len
);
236 skb_put_data(skb
, data
, len
);
239 BUG_ON(code
>= CP_CODES
);
242 for (i
= 0; i
< min_t(unsigned int, magic_len
+ len
, DEBUG_CP
); i
++) {
243 sprintf(ptr
, " %02X", skb
->data
[sizeof(struct cp_header
) + i
]);
246 printk(KERN_DEBUG
"%s: TX %s [%s id 0x%X]%s\n", dev
->name
,
247 proto_name(pid
), code_names
[code
], id
, debug_buffer
);
250 ppp_hard_header(skb
, dev
, pid
, NULL
, NULL
, 0);
252 skb
->priority
= TC_PRIO_CONTROL
;
254 skb_reset_network_header(skb
);
255 skb_queue_tail(&tx_queue
, skb
);
259 /* State transition table (compare STD-51)
261 TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count
262 TO- = Timeout with counter expired zrc = Zero-Restart-Count
264 RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request
265 RCR- = Receive-Configure-Request (Bad)
266 RCA = Receive-Configure-Ack sca = Send-Configure-Ack
267 RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej
269 RTR = Receive-Terminate-Request str = Send-Terminate-Request
270 RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack
272 RUC = Receive-Unknown-Code scj = Send-Code-Reject
273 RXJ+ = Receive-Code-Reject (permitted)
274 or Receive-Protocol-Reject
275 RXJ- = Receive-Code-Reject (catastrophic)
276 or Receive-Protocol-Reject
278 static int cp_table
[EVENTS
][STATES
] = {
279 /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
281 {IRC
|SCR
|3, INV
, INV
, INV
, INV
, INV
, INV
}, /* START */
282 { INV
, 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */
283 { INV
, INV
,STR
|2, SCR
|3 ,SCR
|3, SCR
|5 , INV
}, /* TO+ */
284 { INV
, INV
, 1 , 1 , 1 , 1 , INV
}, /* TO- */
285 { STA
|0 ,IRC
|SCR
|SCA
|5, 2 , SCA
|5 ,SCA
|6, SCA
|5 ,SCR
|SCA
|5}, /* RCR+ */
286 { STA
|0 ,IRC
|SCR
|SCN
|3, 2 , SCN
|3 ,SCN
|4, SCN
|3 ,SCR
|SCN
|3}, /* RCR- */
287 { STA
|0 , STA
|1 , 2 , IRC
|4 ,SCR
|3, 6 , SCR
|3 }, /* RCA */
288 { STA
|0 , STA
|1 , 2 ,IRC
|SCR
|3,SCR
|3,IRC
|SCR
|5, SCR
|3 }, /* RCN */
289 { STA
|0 , STA
|1 ,STA
|2, STA
|3 ,STA
|3, STA
|3 ,ZRC
|STA
|2}, /* RTR */
290 { 0 , 1 , 1 , 3 , 3 , 5 , SCR
|3 }, /* RTA */
291 { SCJ
|0 , SCJ
|1 ,SCJ
|2, SCJ
|3 ,SCJ
|4, SCJ
|5 , SCJ
|6 }, /* RUC */
292 { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */
293 { 0 , 1 , 1 , 1 , 1 , 1 ,IRC
|STR
|2}, /* RXJ- */
297 /* SCA: RCR+ must supply id, len and data
298 SCN: RCR- must supply code, id, len and data
299 STA: RTR must supply id
300 SCJ: RUC must supply CP packet len and data */
301 static void ppp_cp_event(struct net_device
*dev
, u16 pid
, u16 event
, u8 code
,
302 u8 id
, unsigned int len
, const void *data
)
304 int old_state
, action
;
305 struct ppp
*ppp
= get_ppp(dev
);
306 struct proto
*proto
= get_proto(dev
, pid
);
308 old_state
= proto
->state
;
309 BUG_ON(old_state
>= STATES
);
310 BUG_ON(event
>= EVENTS
);
313 printk(KERN_DEBUG
"%s: %s ppp_cp_event(%s) %s ...\n", dev
->name
,
314 proto_name(pid
), event_names
[event
], state_names
[proto
->state
]);
317 action
= cp_table
[event
][old_state
];
319 proto
->state
= action
& STATE_MASK
;
320 if (action
& (SCR
| STR
)) /* set Configure-Req/Terminate-Req timer */
321 mod_timer(&proto
->timer
, proto
->timeout
=
322 jiffies
+ ppp
->req_timeout
* HZ
);
324 proto
->restart_counter
= 0;
326 proto
->restart_counter
= (proto
->state
== STOPPING
) ?
327 ppp
->term_retries
: ppp
->cr_retries
;
329 if (action
& SCR
) /* send Configure-Request */
330 ppp_tx_cp(dev
, pid
, CP_CONF_REQ
, proto
->cr_id
= ++ppp
->seq
,
332 if (action
& SCA
) /* send Configure-Ack */
333 ppp_tx_cp(dev
, pid
, CP_CONF_ACK
, id
, len
, data
);
334 if (action
& SCN
) /* send Configure-Nak/Reject */
335 ppp_tx_cp(dev
, pid
, code
, id
, len
, data
);
336 if (action
& STR
) /* send Terminate-Request */
337 ppp_tx_cp(dev
, pid
, CP_TERM_REQ
, ++ppp
->seq
, 0, NULL
);
338 if (action
& STA
) /* send Terminate-Ack */
339 ppp_tx_cp(dev
, pid
, CP_TERM_ACK
, id
, 0, NULL
);
340 if (action
& SCJ
) /* send Code-Reject */
341 ppp_tx_cp(dev
, pid
, CP_CODE_REJ
, ++ppp
->seq
, len
, data
);
343 if (old_state
!= OPENED
&& proto
->state
== OPENED
) {
344 netdev_info(dev
, "%s up\n", proto_name(pid
));
345 if (pid
== PID_LCP
) {
346 netif_dormant_off(dev
);
347 ppp_cp_event(dev
, PID_IPCP
, START
, 0, 0, 0, NULL
);
348 ppp_cp_event(dev
, PID_IPV6CP
, START
, 0, 0, 0, NULL
);
349 ppp
->last_pong
= jiffies
;
350 mod_timer(&proto
->timer
, proto
->timeout
=
351 jiffies
+ ppp
->keepalive_interval
* HZ
);
354 if (old_state
== OPENED
&& proto
->state
!= OPENED
) {
355 netdev_info(dev
, "%s down\n", proto_name(pid
));
356 if (pid
== PID_LCP
) {
357 netif_dormant_on(dev
);
358 ppp_cp_event(dev
, PID_IPCP
, STOP
, 0, 0, 0, NULL
);
359 ppp_cp_event(dev
, PID_IPV6CP
, STOP
, 0, 0, 0, NULL
);
362 if (old_state
!= CLOSED
&& proto
->state
== CLOSED
)
363 del_timer(&proto
->timer
);
366 printk(KERN_DEBUG
"%s: %s ppp_cp_event(%s) ... %s\n", dev
->name
,
367 proto_name(pid
), event_names
[event
], state_names
[proto
->state
]);
372 static void ppp_cp_parse_cr(struct net_device
*dev
, u16 pid
, u8 id
,
373 unsigned int req_len
, const u8
*data
)
375 static u8
const valid_accm
[6] = { LCP_OPTION_ACCM
, 6, 0, 0, 0, 0 };
378 unsigned int len
= req_len
, nak_len
= 0, rej_len
= 0;
380 if (!(out
= kmalloc(len
, GFP_ATOMIC
))) {
381 dev
->stats
.rx_dropped
++;
382 return; /* out of memory, ignore CR packet */
385 for (opt
= data
; len
; len
-= opt
[1], opt
+= opt
[1]) {
386 if (len
< 2 || len
< opt
[1]) {
387 dev
->stats
.rx_errors
++;
389 return; /* bad packet, drop silently */
395 continue; /* MRU always OK and > 1500 bytes? */
397 case LCP_OPTION_ACCM
: /* async control character map */
398 if (!memcmp(opt
, valid_accm
,
401 if (!rej_len
) { /* NAK it */
402 memcpy(out
+ nak_len
, valid_accm
,
404 nak_len
+= sizeof(valid_accm
);
408 case LCP_OPTION_MAGIC
:
409 if (opt
[1] != 6 || (!opt
[2] && !opt
[3] &&
411 break; /* reject invalid magic number */
414 /* reject this option */
415 memcpy(out
+ rej_len
, opt
, opt
[1]);
420 ppp_cp_event(dev
, pid
, RCR_BAD
, CP_CONF_REJ
, id
, rej_len
, out
);
422 ppp_cp_event(dev
, pid
, RCR_BAD
, CP_CONF_NAK
, id
, nak_len
, out
);
424 ppp_cp_event(dev
, pid
, RCR_GOOD
, CP_CONF_ACK
, id
, req_len
, data
);
429 static int ppp_rx(struct sk_buff
*skb
)
431 struct hdlc_header
*hdr
= (struct hdlc_header
*)skb
->data
;
432 struct net_device
*dev
= skb
->dev
;
433 struct ppp
*ppp
= get_ppp(dev
);
435 struct cp_header
*cp
;
444 spin_lock_irqsave(&ppp
->lock
, flags
);
445 /* Check HDLC header */
446 if (skb
->len
< sizeof(struct hdlc_header
))
448 cp
= skb_pull(skb
, sizeof(struct hdlc_header
));
449 if (hdr
->address
!= HDLC_ADDR_ALLSTATIONS
||
450 hdr
->control
!= HDLC_CTRL_UI
)
453 pid
= ntohs(hdr
->protocol
);
454 proto
= get_proto(dev
, pid
);
456 if (ppp
->protos
[IDX_LCP
].state
== OPENED
)
457 ppp_tx_cp(dev
, PID_LCP
, LCP_PROTO_REJ
,
458 ++ppp
->seq
, skb
->len
+ 2, &hdr
->protocol
);
462 len
= ntohs(cp
->len
);
463 if (len
< sizeof(struct cp_header
) /* no complete CP header? */ ||
464 skb
->len
< len
/* truncated packet? */)
466 skb_pull(skb
, sizeof(struct cp_header
));
467 len
-= sizeof(struct cp_header
);
469 /* HDLC and CP headers stripped from skb */
471 if (cp
->code
< CP_CODES
)
472 sprintf(debug_buffer
, "[%s id 0x%X]", code_names
[cp
->code
],
475 sprintf(debug_buffer
, "[code %u id 0x%X]", cp
->code
, cp
->id
);
476 ptr
= debug_buffer
+ strlen(debug_buffer
);
477 for (i
= 0; i
< min_t(unsigned int, len
, DEBUG_CP
); i
++) {
478 sprintf(ptr
, " %02X", skb
->data
[i
]);
481 printk(KERN_DEBUG
"%s: RX %s %s\n", dev
->name
, proto_name(pid
),
489 pid
= ntohs(*(__be16
*)skb
->data
);
490 if (pid
== PID_LCP
|| pid
== PID_IPCP
||
492 ppp_cp_event(dev
, pid
, RXJ_BAD
, 0, 0,
496 case LCP_ECHO_REQ
: /* send Echo-Reply */
497 if (len
>= 4 && proto
->state
== OPENED
)
498 ppp_tx_cp(dev
, PID_LCP
, LCP_ECHO_REPLY
,
499 cp
->id
, len
- 4, skb
->data
+ 4);
503 if (cp
->id
== ppp
->echo_id
)
504 ppp
->last_pong
= jiffies
;
507 case LCP_DISC_REQ
: /* discard */
511 /* LCP, IPCP and IPV6CP */
514 ppp_cp_parse_cr(dev
, pid
, cp
->id
, len
, skb
->data
);
518 if (cp
->id
== proto
->cr_id
)
519 ppp_cp_event(dev
, pid
, RCA
, 0, 0, 0, NULL
);
524 if (cp
->id
== proto
->cr_id
)
525 ppp_cp_event(dev
, pid
, RCN
, 0, 0, 0, NULL
);
529 ppp_cp_event(dev
, pid
, RTR
, 0, cp
->id
, 0, NULL
);
533 ppp_cp_event(dev
, pid
, RTA
, 0, 0, 0, NULL
);
537 ppp_cp_event(dev
, pid
, RXJ_BAD
, 0, 0, 0, NULL
);
541 len
+= sizeof(struct cp_header
);
544 ppp_cp_event(dev
, pid
, RUC
, 0, 0, len
, cp
);
550 dev
->stats
.rx_errors
++;
552 spin_unlock_irqrestore(&ppp
->lock
, flags
);
553 dev_kfree_skb_any(skb
);
558 static void ppp_timer(struct timer_list
*t
)
560 struct proto
*proto
= from_timer(proto
, t
, timer
);
561 struct ppp
*ppp
= get_ppp(proto
->dev
);
564 spin_lock_irqsave(&ppp
->lock
, flags
);
565 switch (proto
->state
) {
570 if (proto
->restart_counter
) {
571 ppp_cp_event(proto
->dev
, proto
->pid
, TO_GOOD
, 0, 0,
573 proto
->restart_counter
--;
574 } else if (netif_carrier_ok(proto
->dev
))
575 ppp_cp_event(proto
->dev
, proto
->pid
, TO_GOOD
, 0, 0,
578 ppp_cp_event(proto
->dev
, proto
->pid
, TO_BAD
, 0, 0,
583 if (proto
->pid
!= PID_LCP
)
585 if (time_after(jiffies
, ppp
->last_pong
+
586 ppp
->keepalive_timeout
* HZ
)) {
587 netdev_info(proto
->dev
, "Link down\n");
588 ppp_cp_event(proto
->dev
, PID_LCP
, STOP
, 0, 0, 0, NULL
);
589 ppp_cp_event(proto
->dev
, PID_LCP
, START
, 0, 0, 0, NULL
);
590 } else { /* send keep-alive packet */
591 ppp
->echo_id
= ++ppp
->seq
;
592 ppp_tx_cp(proto
->dev
, PID_LCP
, LCP_ECHO_REQ
,
593 ppp
->echo_id
, 0, NULL
);
594 proto
->timer
.expires
= jiffies
+
595 ppp
->keepalive_interval
* HZ
;
596 add_timer(&proto
->timer
);
600 spin_unlock_irqrestore(&ppp
->lock
, flags
);
605 static void ppp_start(struct net_device
*dev
)
607 struct ppp
*ppp
= get_ppp(dev
);
610 for (i
= 0; i
< IDX_COUNT
; i
++) {
611 struct proto
*proto
= &ppp
->protos
[i
];
613 timer_setup(&proto
->timer
, ppp_timer
, 0);
614 proto
->state
= CLOSED
;
616 ppp
->protos
[IDX_LCP
].pid
= PID_LCP
;
617 ppp
->protos
[IDX_IPCP
].pid
= PID_IPCP
;
618 ppp
->protos
[IDX_IPV6CP
].pid
= PID_IPV6CP
;
620 ppp_cp_event(dev
, PID_LCP
, START
, 0, 0, 0, NULL
);
623 static void ppp_stop(struct net_device
*dev
)
625 ppp_cp_event(dev
, PID_LCP
, STOP
, 0, 0, 0, NULL
);
628 static void ppp_close(struct net_device
*dev
)
633 static struct hdlc_proto proto
= {
637 .type_trans
= ppp_type_trans
,
640 .module
= THIS_MODULE
,
643 static const struct header_ops ppp_header_ops
= {
644 .create
= ppp_hard_header
,
647 static int ppp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
649 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
653 switch (ifr
->ifr_settings
.type
) {
655 if (dev_to_hdlc(dev
)->proto
!= &proto
)
657 ifr
->ifr_settings
.type
= IF_PROTO_PPP
;
658 return 0; /* return protocol only, no settable parameters */
661 if (!capable(CAP_NET_ADMIN
))
664 if (dev
->flags
& IFF_UP
)
667 /* no settable parameters */
669 result
= hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
673 result
= attach_hdlc_protocol(dev
, &proto
, sizeof(struct ppp
));
678 spin_lock_init(&ppp
->lock
);
679 ppp
->req_timeout
= 2;
680 ppp
->cr_retries
= 10;
681 ppp
->term_retries
= 2;
682 ppp
->keepalive_interval
= 10;
683 ppp
->keepalive_timeout
= 60;
685 dev
->hard_header_len
= sizeof(struct hdlc_header
);
686 dev
->header_ops
= &ppp_header_ops
;
687 dev
->type
= ARPHRD_PPP
;
688 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE
, dev
);
689 netif_dormant_on(dev
);
697 static int __init
mod_init(void)
699 skb_queue_head_init(&tx_queue
);
700 register_hdlc_protocol(&proto
);
704 static void __exit
mod_exit(void)
706 unregister_hdlc_protocol(&proto
);
710 module_init(mod_init
);
711 module_exit(mod_exit
);
713 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
714 MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
715 MODULE_LICENSE("GPL v2");