Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Generic HDLC support routines for Linux
4 * Point-to-point protocol support
5 *
6 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
7 */
8
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>
21
22#define DEBUG_CP 0 /* also bytes# to dump */
23#define DEBUG_STATE 0
24#define DEBUG_HARD_HEADER 0
25
26#define HDLC_ADDR_ALLSTATIONS 0xFF
27#define HDLC_CTRL_UI 0x03
28
29#define PID_LCP 0xC021
30#define PID_IP 0x0021
31#define PID_IPCP 0x8021
32#define PID_IPV6 0x0057
33#define PID_IPV6CP 0x8057
34
35enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
36enum {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};
39#if DEBUG_CP
40static const char *const code_names[CP_CODES] = {
41 "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
42 "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
43};
44
45static char debug_buffer[64 + 3 * DEBUG_CP];
46#endif
47
48enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
49
50struct hdlc_header {
51 u8 address;
52 u8 control;
53 __be16 protocol;
54};
55
56struct cp_header {
57 u8 code;
58 u8 id;
59 __be16 len;
60};
61
62struct proto {
63 struct net_device *dev;
64 struct timer_list timer;
65 unsigned long timeout;
66 u16 pid; /* protocol ID */
67 u8 state;
68 u8 cr_id; /* ID of last Configuration-Request */
69 u8 restart_counter;
70};
71
72struct ppp {
73 struct proto protos[IDX_COUNT];
74 spinlock_t lock;
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) */
80};
81
82enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
83 STATES, STATE_MASK = 0xF};
84enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
85 RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
86enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
87 SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
88
89#if DEBUG_STATE
90static const char *const state_names[STATES] = {
91 "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
92 "Opened"
93};
94
95static const char *const event_names[EVENTS] = {
96 "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
97 "RTR", "RTA", "RUC", "RXJ+", "RXJ-"
98};
99#endif
100
101static struct sk_buff_head tx_queue; /* used when holding the spin lock */
102
103static int ppp_ioctl(struct net_device *dev, struct if_settings *ifs);
104
105static inline struct ppp *get_ppp(struct net_device *dev)
106{
107 return (struct ppp *)dev_to_hdlc(dev)->state;
108}
109
110static inline struct proto *get_proto(struct net_device *dev, u16 pid)
111{
112 struct ppp *ppp = get_ppp(dev);
113
114 switch (pid) {
115 case PID_LCP:
116 return &ppp->protos[IDX_LCP];
117 case PID_IPCP:
118 return &ppp->protos[IDX_IPCP];
119 case PID_IPV6CP:
120 return &ppp->protos[IDX_IPV6CP];
121 default:
122 return NULL;
123 }
124}
125
126static inline const char *proto_name(u16 pid)
127{
128 switch (pid) {
129 case PID_LCP:
130 return "LCP";
131 case PID_IPCP:
132 return "IPCP";
133 case PID_IPV6CP:
134 return "IPV6CP";
135 default:
136 return NULL;
137 }
138}
139
140static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
141{
142 struct hdlc_header *data = (struct hdlc_header *)skb->data;
143
144 if (skb->len < sizeof(struct hdlc_header))
145 return htons(ETH_P_HDLC);
146 if (data->address != HDLC_ADDR_ALLSTATIONS ||
147 data->control != HDLC_CTRL_UI)
148 return htons(ETH_P_HDLC);
149
150 switch (data->protocol) {
151 case cpu_to_be16(PID_IP):
152 skb_pull(skb, sizeof(struct hdlc_header));
153 return htons(ETH_P_IP);
154
155 case cpu_to_be16(PID_IPV6):
156 skb_pull(skb, sizeof(struct hdlc_header));
157 return htons(ETH_P_IPV6);
158
159 default:
160 return htons(ETH_P_HDLC);
161 }
162}
163
164static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev,
165 u16 type, const void *daddr, const void *saddr,
166 unsigned int len)
167{
168 struct hdlc_header *data;
169#if DEBUG_HARD_HEADER
170 printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name);
171#endif
172
173 skb_push(skb, sizeof(struct hdlc_header));
174 data = (struct hdlc_header *)skb->data;
175
176 data->address = HDLC_ADDR_ALLSTATIONS;
177 data->control = HDLC_CTRL_UI;
178 switch (type) {
179 case ETH_P_IP:
180 data->protocol = htons(PID_IP);
181 break;
182 case ETH_P_IPV6:
183 data->protocol = htons(PID_IPV6);
184 break;
185 case PID_LCP:
186 case PID_IPCP:
187 case PID_IPV6CP:
188 data->protocol = htons(type);
189 break;
190 default: /* unknown protocol */
191 data->protocol = 0;
192 }
193 return sizeof(struct hdlc_header);
194}
195
196static void ppp_tx_flush(void)
197{
198 struct sk_buff *skb;
199
200 while ((skb = skb_dequeue(&tx_queue)) != NULL)
201 dev_queue_xmit(skb);
202}
203
204static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code,
205 u8 id, unsigned int len, const void *data)
206{
207 struct sk_buff *skb;
208 struct cp_header *cp;
209 unsigned int magic_len = 0;
210 static u32 magic;
211
212#if DEBUG_CP
213 int i;
214 char *ptr;
215#endif
216
217 if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY))
218 magic_len = sizeof(magic);
219
220 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
221 sizeof(struct cp_header) + magic_len + len);
222 if (!skb)
223 return;
224
225 skb_reserve(skb, sizeof(struct hdlc_header));
226
227 cp = skb_put(skb, sizeof(struct cp_header));
228 cp->code = code;
229 cp->id = id;
230 cp->len = htons(sizeof(struct cp_header) + magic_len + len);
231
232 if (magic_len)
233 skb_put_data(skb, &magic, magic_len);
234 if (len)
235 skb_put_data(skb, data, len);
236
237#if DEBUG_CP
238 BUG_ON(code >= CP_CODES);
239 ptr = debug_buffer;
240 *ptr = '\x0';
241 for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
242 sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
243 ptr += strlen(ptr);
244 }
245 printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
246 proto_name(pid), code_names[code], id, debug_buffer);
247#endif
248
249 ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
250
251 skb->priority = TC_PRIO_CONTROL;
252 skb->dev = dev;
253 skb->protocol = htons(ETH_P_HDLC);
254 skb_reset_network_header(skb);
255 skb_queue_tail(&tx_queue, skb);
256}
257
258/* State transition table (compare STD-51)
259 Events Actions
260 TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count
261 TO- = Timeout with counter expired zrc = Zero-Restart-Count
262
263 RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request
264 RCR- = Receive-Configure-Request (Bad)
265 RCA = Receive-Configure-Ack sca = Send-Configure-Ack
266 RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej
267
268 RTR = Receive-Terminate-Request str = Send-Terminate-Request
269 RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack
270
271 RUC = Receive-Unknown-Code scj = Send-Code-Reject
272 RXJ+ = Receive-Code-Reject (permitted)
273 or Receive-Protocol-Reject
274 RXJ- = Receive-Code-Reject (catastrophic)
275 or Receive-Protocol-Reject
276*/
277static int cp_table[EVENTS][STATES] = {
278 /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
279 0 1 2 3 4 5 6 */
280 {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */
281 { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */
282 { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */
283 { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */
284 { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */
285 { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */
286 { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */
287 { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */
288 { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */
289 { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */
290 { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */
291 { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */
292 { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */
293};
294
295/* SCA: RCR+ must supply id, len and data
296 SCN: RCR- must supply code, id, len and data
297 STA: RTR must supply id
298 SCJ: RUC must supply CP packet len and data */
299static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
300 u8 id, unsigned int len, const void *data)
301{
302 int old_state, action;
303 struct ppp *ppp = get_ppp(dev);
304 struct proto *proto = get_proto(dev, pid);
305
306 old_state = proto->state;
307 BUG_ON(old_state >= STATES);
308 BUG_ON(event >= EVENTS);
309
310#if DEBUG_STATE
311 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
312 proto_name(pid), event_names[event], state_names[proto->state]);
313#endif
314
315 action = cp_table[event][old_state];
316
317 proto->state = action & STATE_MASK;
318 if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
319 mod_timer(&proto->timer, proto->timeout =
320 jiffies + ppp->req_timeout * HZ);
321 if (action & ZRC)
322 proto->restart_counter = 0;
323 if (action & IRC)
324 proto->restart_counter = (proto->state == STOPPING) ?
325 ppp->term_retries : ppp->cr_retries;
326
327 if (action & SCR) /* send Configure-Request */
328 ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
329 0, NULL);
330 if (action & SCA) /* send Configure-Ack */
331 ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
332 if (action & SCN) /* send Configure-Nak/Reject */
333 ppp_tx_cp(dev, pid, code, id, len, data);
334 if (action & STR) /* send Terminate-Request */
335 ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
336 if (action & STA) /* send Terminate-Ack */
337 ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
338 if (action & SCJ) /* send Code-Reject */
339 ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
340
341 if (old_state != OPENED && proto->state == OPENED) {
342 netdev_info(dev, "%s up\n", proto_name(pid));
343 if (pid == PID_LCP) {
344 netif_dormant_off(dev);
345 ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
346 ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
347 ppp->last_pong = jiffies;
348 mod_timer(&proto->timer, proto->timeout =
349 jiffies + ppp->keepalive_interval * HZ);
350 }
351 }
352 if (old_state == OPENED && proto->state != OPENED) {
353 netdev_info(dev, "%s down\n", proto_name(pid));
354 if (pid == PID_LCP) {
355 netif_dormant_on(dev);
356 ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
357 ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
358 }
359 }
360 if (old_state != CLOSED && proto->state == CLOSED)
361 del_timer(&proto->timer);
362
363#if DEBUG_STATE
364 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
365 proto_name(pid), event_names[event], state_names[proto->state]);
366#endif
367}
368
369static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
370 unsigned int req_len, const u8 *data)
371{
372 static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
373 const u8 *opt;
374 u8 *out;
375 unsigned int len = req_len, nak_len = 0, rej_len = 0;
376
377 out = kmalloc(len, GFP_ATOMIC);
378 if (!out) {
379 dev->stats.rx_dropped++;
380 return; /* out of memory, ignore CR packet */
381 }
382
383 for (opt = data; len; len -= opt[1], opt += opt[1]) {
384 if (len < 2 || opt[1] < 2 || len < opt[1])
385 goto err_out;
386
387 if (pid == PID_LCP)
388 switch (opt[0]) {
389 case LCP_OPTION_MRU:
390 continue; /* MRU always OK and > 1500 bytes? */
391
392 case LCP_OPTION_ACCM: /* async control character map */
393 if (opt[1] < sizeof(valid_accm))
394 goto err_out;
395 if (!memcmp(opt, valid_accm,
396 sizeof(valid_accm)))
397 continue;
398 if (!rej_len) { /* NAK it */
399 memcpy(out + nak_len, valid_accm,
400 sizeof(valid_accm));
401 nak_len += sizeof(valid_accm);
402 continue;
403 }
404 break;
405 case LCP_OPTION_MAGIC:
406 if (len < 6)
407 goto err_out;
408 if (opt[1] != 6 || (!opt[2] && !opt[3] &&
409 !opt[4] && !opt[5]))
410 break; /* reject invalid magic number */
411 continue;
412 }
413 /* reject this option */
414 memcpy(out + rej_len, opt, opt[1]);
415 rej_len += opt[1];
416 }
417
418 if (rej_len)
419 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
420 else if (nak_len)
421 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
422 else
423 ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data);
424
425 kfree(out);
426 return;
427
428err_out:
429 dev->stats.rx_errors++;
430 kfree(out);
431}
432
433static int ppp_rx(struct sk_buff *skb)
434{
435 struct hdlc_header *hdr = (struct hdlc_header *)skb->data;
436 struct net_device *dev = skb->dev;
437 struct ppp *ppp = get_ppp(dev);
438 struct proto *proto;
439 struct cp_header *cp;
440 unsigned long flags;
441 unsigned int len;
442 u16 pid;
443#if DEBUG_CP
444 int i;
445 char *ptr;
446#endif
447
448 spin_lock_irqsave(&ppp->lock, flags);
449 /* Check HDLC header */
450 if (skb->len < sizeof(struct hdlc_header))
451 goto rx_error;
452 cp = skb_pull(skb, sizeof(struct hdlc_header));
453 if (hdr->address != HDLC_ADDR_ALLSTATIONS ||
454 hdr->control != HDLC_CTRL_UI)
455 goto rx_error;
456
457 pid = ntohs(hdr->protocol);
458 proto = get_proto(dev, pid);
459 if (!proto) {
460 if (ppp->protos[IDX_LCP].state == OPENED)
461 ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ,
462 ++ppp->seq, skb->len + 2, &hdr->protocol);
463 goto rx_error;
464 }
465
466 len = ntohs(cp->len);
467 if (len < sizeof(struct cp_header) /* no complete CP header? */ ||
468 skb->len < len /* truncated packet? */)
469 goto rx_error;
470 skb_pull(skb, sizeof(struct cp_header));
471 len -= sizeof(struct cp_header);
472
473 /* HDLC and CP headers stripped from skb */
474#if DEBUG_CP
475 if (cp->code < CP_CODES)
476 sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code],
477 cp->id);
478 else
479 sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id);
480 ptr = debug_buffer + strlen(debug_buffer);
481 for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) {
482 sprintf(ptr, " %02X", skb->data[i]);
483 ptr += strlen(ptr);
484 }
485 printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid),
486 debug_buffer);
487#endif
488
489 /* LCP only */
490 if (pid == PID_LCP)
491 switch (cp->code) {
492 case LCP_PROTO_REJ:
493 pid = ntohs(*(__be16 *)skb->data);
494 if (pid == PID_LCP || pid == PID_IPCP ||
495 pid == PID_IPV6CP)
496 ppp_cp_event(dev, pid, RXJ_BAD, 0, 0,
497 0, NULL);
498 goto out;
499
500 case LCP_ECHO_REQ: /* send Echo-Reply */
501 if (len >= 4 && proto->state == OPENED)
502 ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY,
503 cp->id, len - 4, skb->data + 4);
504 goto out;
505
506 case LCP_ECHO_REPLY:
507 if (cp->id == ppp->echo_id)
508 ppp->last_pong = jiffies;
509 goto out;
510
511 case LCP_DISC_REQ: /* discard */
512 goto out;
513 }
514
515 /* LCP, IPCP and IPV6CP */
516 switch (cp->code) {
517 case CP_CONF_REQ:
518 ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data);
519 break;
520
521 case CP_CONF_ACK:
522 if (cp->id == proto->cr_id)
523 ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL);
524 break;
525
526 case CP_CONF_REJ:
527 case CP_CONF_NAK:
528 if (cp->id == proto->cr_id)
529 ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL);
530 break;
531
532 case CP_TERM_REQ:
533 ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
534 break;
535
536 case CP_TERM_ACK:
537 ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
538 break;
539
540 case CP_CODE_REJ:
541 ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
542 break;
543
544 default:
545 len += sizeof(struct cp_header);
546 if (len > dev->mtu)
547 len = dev->mtu;
548 ppp_cp_event(dev, pid, RUC, 0, 0, len, cp);
549 break;
550 }
551 goto out;
552
553rx_error:
554 dev->stats.rx_errors++;
555out:
556 spin_unlock_irqrestore(&ppp->lock, flags);
557 dev_kfree_skb_any(skb);
558 ppp_tx_flush();
559 return NET_RX_DROP;
560}
561
562static void ppp_timer(struct timer_list *t)
563{
564 struct proto *proto = from_timer(proto, t, timer);
565 struct ppp *ppp = get_ppp(proto->dev);
566 unsigned long flags;
567
568 spin_lock_irqsave(&ppp->lock, flags);
569 /* mod_timer could be called after we entered this function but
570 * before we got the lock.
571 */
572 if (timer_pending(&proto->timer)) {
573 spin_unlock_irqrestore(&ppp->lock, flags);
574 return;
575 }
576 switch (proto->state) {
577 case STOPPING:
578 case REQ_SENT:
579 case ACK_RECV:
580 case ACK_SENT:
581 if (proto->restart_counter) {
582 ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
583 0, NULL);
584 proto->restart_counter--;
585 } else if (netif_carrier_ok(proto->dev))
586 ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
587 0, NULL);
588 else
589 ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
590 0, NULL);
591 break;
592
593 case OPENED:
594 if (proto->pid != PID_LCP)
595 break;
596 if (time_after(jiffies, ppp->last_pong +
597 ppp->keepalive_timeout * HZ)) {
598 netdev_info(proto->dev, "Link down\n");
599 ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
600 ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
601 } else { /* send keep-alive packet */
602 ppp->echo_id = ++ppp->seq;
603 ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
604 ppp->echo_id, 0, NULL);
605 proto->timer.expires = jiffies +
606 ppp->keepalive_interval * HZ;
607 add_timer(&proto->timer);
608 }
609 break;
610 }
611 spin_unlock_irqrestore(&ppp->lock, flags);
612 ppp_tx_flush();
613}
614
615static void ppp_start(struct net_device *dev)
616{
617 struct ppp *ppp = get_ppp(dev);
618 int i;
619
620 for (i = 0; i < IDX_COUNT; i++) {
621 struct proto *proto = &ppp->protos[i];
622
623 proto->dev = dev;
624 timer_setup(&proto->timer, ppp_timer, 0);
625 proto->state = CLOSED;
626 }
627 ppp->protos[IDX_LCP].pid = PID_LCP;
628 ppp->protos[IDX_IPCP].pid = PID_IPCP;
629 ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
630
631 ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
632}
633
634static void ppp_stop(struct net_device *dev)
635{
636 ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
637}
638
639static void ppp_close(struct net_device *dev)
640{
641 ppp_tx_flush();
642}
643
644static struct hdlc_proto proto = {
645 .start = ppp_start,
646 .stop = ppp_stop,
647 .close = ppp_close,
648 .type_trans = ppp_type_trans,
649 .ioctl = ppp_ioctl,
650 .netif_rx = ppp_rx,
651 .module = THIS_MODULE,
652};
653
654static const struct header_ops ppp_header_ops = {
655 .create = ppp_hard_header,
656};
657
658static int ppp_ioctl(struct net_device *dev, struct if_settings *ifs)
659{
660 hdlc_device *hdlc = dev_to_hdlc(dev);
661 struct ppp *ppp;
662 int result;
663
664 switch (ifs->type) {
665 case IF_GET_PROTO:
666 if (dev_to_hdlc(dev)->proto != &proto)
667 return -EINVAL;
668 ifs->type = IF_PROTO_PPP;
669 return 0; /* return protocol only, no settable parameters */
670
671 case IF_PROTO_PPP:
672 if (!capable(CAP_NET_ADMIN))
673 return -EPERM;
674
675 if (dev->flags & IFF_UP)
676 return -EBUSY;
677
678 /* no settable parameters */
679
680 result = hdlc->attach(dev, ENCODING_NRZ,
681 PARITY_CRC16_PR1_CCITT);
682 if (result)
683 return result;
684
685 result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
686 if (result)
687 return result;
688
689 ppp = get_ppp(dev);
690 spin_lock_init(&ppp->lock);
691 ppp->req_timeout = 2;
692 ppp->cr_retries = 10;
693 ppp->term_retries = 2;
694 ppp->keepalive_interval = 10;
695 ppp->keepalive_timeout = 60;
696
697 dev->hard_header_len = sizeof(struct hdlc_header);
698 dev->header_ops = &ppp_header_ops;
699 dev->type = ARPHRD_PPP;
700 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
701 netif_dormant_on(dev);
702 return 0;
703 }
704
705 return -EINVAL;
706}
707
708static int __init hdlc_ppp_init(void)
709{
710 skb_queue_head_init(&tx_queue);
711 register_hdlc_protocol(&proto);
712 return 0;
713}
714
715static void __exit hdlc_ppp_exit(void)
716{
717 unregister_hdlc_protocol(&proto);
718}
719
720module_init(hdlc_ppp_init);
721module_exit(hdlc_ppp_exit);
722
723MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
724MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
725MODULE_LICENSE("GPL v2");
1/*
2 * Generic HDLC support routines for Linux
3 * Point-to-point protocol support
4 *
5 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
10 */
11
12#include <linux/errno.h>
13#include <linux/hdlc.h>
14#include <linux/if_arp.h>
15#include <linux/inetdevice.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/pkt_sched.h>
20#include <linux/poll.h>
21#include <linux/skbuff.h>
22#include <linux/slab.h>
23#include <linux/spinlock.h>
24
25#define DEBUG_CP 0 /* also bytes# to dump */
26#define DEBUG_STATE 0
27#define DEBUG_HARD_HEADER 0
28
29#define HDLC_ADDR_ALLSTATIONS 0xFF
30#define HDLC_CTRL_UI 0x03
31
32#define PID_LCP 0xC021
33#define PID_IP 0x0021
34#define PID_IPCP 0x8021
35#define PID_IPV6 0x0057
36#define PID_IPV6CP 0x8057
37
38enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
39enum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ,
40 CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY,
41 LCP_DISC_REQ, CP_CODES};
42#if DEBUG_CP
43static const char *const code_names[CP_CODES] = {
44 "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
45 "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
46};
47static char debug_buffer[64 + 3 * DEBUG_CP];
48#endif
49
50enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
51
52struct hdlc_header {
53 u8 address;
54 u8 control;
55 __be16 protocol;
56};
57
58struct cp_header {
59 u8 code;
60 u8 id;
61 __be16 len;
62};
63
64
65struct proto {
66 struct net_device *dev;
67 struct timer_list timer;
68 unsigned long timeout;
69 u16 pid; /* protocol ID */
70 u8 state;
71 u8 cr_id; /* ID of last Configuration-Request */
72 u8 restart_counter;
73};
74
75struct ppp {
76 struct proto protos[IDX_COUNT];
77 spinlock_t lock;
78 unsigned long last_pong;
79 unsigned int req_timeout, cr_retries, term_retries;
80 unsigned int keepalive_interval, keepalive_timeout;
81 u8 seq; /* local sequence number for requests */
82 u8 echo_id; /* ID of last Echo-Request (LCP) */
83};
84
85enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
86 STATES, STATE_MASK = 0xF};
87enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
88 RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
89enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
90 SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
91
92#if DEBUG_STATE
93static const char *const state_names[STATES] = {
94 "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
95 "Opened"
96};
97static const char *const event_names[EVENTS] = {
98 "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
99 "RTR", "RTA", "RUC", "RXJ+", "RXJ-"
100};
101#endif
102
103static struct sk_buff_head tx_queue; /* used when holding the spin lock */
104
105static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
106
107static inline struct ppp* get_ppp(struct net_device *dev)
108{
109 return (struct ppp *)dev_to_hdlc(dev)->state;
110}
111
112static inline struct proto* get_proto(struct net_device *dev, u16 pid)
113{
114 struct ppp *ppp = get_ppp(dev);
115
116 switch (pid) {
117 case PID_LCP:
118 return &ppp->protos[IDX_LCP];
119 case PID_IPCP:
120 return &ppp->protos[IDX_IPCP];
121 case PID_IPV6CP:
122 return &ppp->protos[IDX_IPV6CP];
123 default:
124 return NULL;
125 }
126}
127
128static inline const char* proto_name(u16 pid)
129{
130 switch (pid) {
131 case PID_LCP:
132 return "LCP";
133 case PID_IPCP:
134 return "IPCP";
135 case PID_IPV6CP:
136 return "IPV6CP";
137 default:
138 return NULL;
139 }
140}
141
142static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
143{
144 struct hdlc_header *data = (struct hdlc_header*)skb->data;
145
146 if (skb->len < sizeof(struct hdlc_header))
147 return htons(ETH_P_HDLC);
148 if (data->address != HDLC_ADDR_ALLSTATIONS ||
149 data->control != HDLC_CTRL_UI)
150 return htons(ETH_P_HDLC);
151
152 switch (data->protocol) {
153 case cpu_to_be16(PID_IP):
154 skb_pull(skb, sizeof(struct hdlc_header));
155 return htons(ETH_P_IP);
156
157 case cpu_to_be16(PID_IPV6):
158 skb_pull(skb, sizeof(struct hdlc_header));
159 return htons(ETH_P_IPV6);
160
161 default:
162 return htons(ETH_P_HDLC);
163 }
164}
165
166
167static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev,
168 u16 type, const void *daddr, const void *saddr,
169 unsigned int len)
170{
171 struct hdlc_header *data;
172#if DEBUG_HARD_HEADER
173 printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name);
174#endif
175
176 skb_push(skb, sizeof(struct hdlc_header));
177 data = (struct hdlc_header*)skb->data;
178
179 data->address = HDLC_ADDR_ALLSTATIONS;
180 data->control = HDLC_CTRL_UI;
181 switch (type) {
182 case ETH_P_IP:
183 data->protocol = htons(PID_IP);
184 break;
185 case ETH_P_IPV6:
186 data->protocol = htons(PID_IPV6);
187 break;
188 case PID_LCP:
189 case PID_IPCP:
190 case PID_IPV6CP:
191 data->protocol = htons(type);
192 break;
193 default: /* unknown protocol */
194 data->protocol = 0;
195 }
196 return sizeof(struct hdlc_header);
197}
198
199
200static void ppp_tx_flush(void)
201{
202 struct sk_buff *skb;
203 while ((skb = skb_dequeue(&tx_queue)) != NULL)
204 dev_queue_xmit(skb);
205}
206
207static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code,
208 u8 id, unsigned int len, const void *data)
209{
210 struct sk_buff *skb;
211 struct cp_header *cp;
212 unsigned int magic_len = 0;
213 static u32 magic;
214
215#if DEBUG_CP
216 int i;
217 char *ptr;
218#endif
219
220 if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY))
221 magic_len = sizeof(magic);
222
223 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
224 sizeof(struct cp_header) + magic_len + len);
225 if (!skb) {
226 netdev_warn(dev, "out of memory in ppp_tx_cp()\n");
227 return;
228 }
229 skb_reserve(skb, sizeof(struct hdlc_header));
230
231 cp = (struct cp_header *)skb_put(skb, sizeof(struct cp_header));
232 cp->code = code;
233 cp->id = id;
234 cp->len = htons(sizeof(struct cp_header) + magic_len + len);
235
236 if (magic_len)
237 memcpy(skb_put(skb, magic_len), &magic, magic_len);
238 if (len)
239 memcpy(skb_put(skb, len), data, len);
240
241#if DEBUG_CP
242 BUG_ON(code >= CP_CODES);
243 ptr = debug_buffer;
244 *ptr = '\x0';
245 for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
246 sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
247 ptr += strlen(ptr);
248 }
249 printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
250 proto_name(pid), code_names[code], id, debug_buffer);
251#endif
252
253 ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
254
255 skb->priority = TC_PRIO_CONTROL;
256 skb->dev = dev;
257 skb_reset_network_header(skb);
258 skb_queue_tail(&tx_queue, skb);
259}
260
261
262/* State transition table (compare STD-51)
263 Events Actions
264 TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count
265 TO- = Timeout with counter expired zrc = Zero-Restart-Count
266
267 RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request
268 RCR- = Receive-Configure-Request (Bad)
269 RCA = Receive-Configure-Ack sca = Send-Configure-Ack
270 RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej
271
272 RTR = Receive-Terminate-Request str = Send-Terminate-Request
273 RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack
274
275 RUC = Receive-Unknown-Code scj = Send-Code-Reject
276 RXJ+ = Receive-Code-Reject (permitted)
277 or Receive-Protocol-Reject
278 RXJ- = Receive-Code-Reject (catastrophic)
279 or Receive-Protocol-Reject
280*/
281static int cp_table[EVENTS][STATES] = {
282 /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
283 0 1 2 3 4 5 6 */
284 {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */
285 { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */
286 { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */
287 { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */
288 { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */
289 { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */
290 { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */
291 { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */
292 { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */
293 { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */
294 { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */
295 { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */
296 { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */
297};
298
299
300/* SCA: RCR+ must supply id, len and data
301 SCN: RCR- must supply code, id, len and data
302 STA: RTR must supply id
303 SCJ: RUC must supply CP packet len and data */
304static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
305 u8 id, unsigned int len, const void *data)
306{
307 int old_state, action;
308 struct ppp *ppp = get_ppp(dev);
309 struct proto *proto = get_proto(dev, pid);
310
311 old_state = proto->state;
312 BUG_ON(old_state >= STATES);
313 BUG_ON(event >= EVENTS);
314
315#if DEBUG_STATE
316 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
317 proto_name(pid), event_names[event], state_names[proto->state]);
318#endif
319
320 action = cp_table[event][old_state];
321
322 proto->state = action & STATE_MASK;
323 if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
324 mod_timer(&proto->timer, proto->timeout =
325 jiffies + ppp->req_timeout * HZ);
326 if (action & ZRC)
327 proto->restart_counter = 0;
328 if (action & IRC)
329 proto->restart_counter = (proto->state == STOPPING) ?
330 ppp->term_retries : ppp->cr_retries;
331
332 if (action & SCR) /* send Configure-Request */
333 ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
334 0, NULL);
335 if (action & SCA) /* send Configure-Ack */
336 ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
337 if (action & SCN) /* send Configure-Nak/Reject */
338 ppp_tx_cp(dev, pid, code, id, len, data);
339 if (action & STR) /* send Terminate-Request */
340 ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
341 if (action & STA) /* send Terminate-Ack */
342 ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
343 if (action & SCJ) /* send Code-Reject */
344 ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
345
346 if (old_state != OPENED && proto->state == OPENED) {
347 netdev_info(dev, "%s up\n", proto_name(pid));
348 if (pid == PID_LCP) {
349 netif_dormant_off(dev);
350 ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
351 ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
352 ppp->last_pong = jiffies;
353 mod_timer(&proto->timer, proto->timeout =
354 jiffies + ppp->keepalive_interval * HZ);
355 }
356 }
357 if (old_state == OPENED && proto->state != OPENED) {
358 netdev_info(dev, "%s down\n", proto_name(pid));
359 if (pid == PID_LCP) {
360 netif_dormant_on(dev);
361 ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
362 ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
363 }
364 }
365 if (old_state != CLOSED && proto->state == CLOSED)
366 del_timer(&proto->timer);
367
368#if DEBUG_STATE
369 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
370 proto_name(pid), event_names[event], state_names[proto->state]);
371#endif
372}
373
374
375static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
376 unsigned int req_len, const u8 *data)
377{
378 static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
379 const u8 *opt;
380 u8 *out;
381 unsigned int len = req_len, nak_len = 0, rej_len = 0;
382
383 if (!(out = kmalloc(len, GFP_ATOMIC))) {
384 dev->stats.rx_dropped++;
385 return; /* out of memory, ignore CR packet */
386 }
387
388 for (opt = data; len; len -= opt[1], opt += opt[1]) {
389 if (len < 2 || len < opt[1]) {
390 dev->stats.rx_errors++;
391 kfree(out);
392 return; /* bad packet, drop silently */
393 }
394
395 if (pid == PID_LCP)
396 switch (opt[0]) {
397 case LCP_OPTION_MRU:
398 continue; /* MRU always OK and > 1500 bytes? */
399
400 case LCP_OPTION_ACCM: /* async control character map */
401 if (!memcmp(opt, valid_accm,
402 sizeof(valid_accm)))
403 continue;
404 if (!rej_len) { /* NAK it */
405 memcpy(out + nak_len, valid_accm,
406 sizeof(valid_accm));
407 nak_len += sizeof(valid_accm);
408 continue;
409 }
410 break;
411 case LCP_OPTION_MAGIC:
412 if (opt[1] != 6 || (!opt[2] && !opt[3] &&
413 !opt[4] && !opt[5]))
414 break; /* reject invalid magic number */
415 continue;
416 }
417 /* reject this option */
418 memcpy(out + rej_len, opt, opt[1]);
419 rej_len += opt[1];
420 }
421
422 if (rej_len)
423 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
424 else if (nak_len)
425 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
426 else
427 ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data);
428
429 kfree(out);
430}
431
432static int ppp_rx(struct sk_buff *skb)
433{
434 struct hdlc_header *hdr = (struct hdlc_header*)skb->data;
435 struct net_device *dev = skb->dev;
436 struct ppp *ppp = get_ppp(dev);
437 struct proto *proto;
438 struct cp_header *cp;
439 unsigned long flags;
440 unsigned int len;
441 u16 pid;
442#if DEBUG_CP
443 int i;
444 char *ptr;
445#endif
446
447 spin_lock_irqsave(&ppp->lock, flags);
448 /* Check HDLC header */
449 if (skb->len < sizeof(struct hdlc_header))
450 goto rx_error;
451 cp = (struct cp_header*)skb_pull(skb, sizeof(struct hdlc_header));
452 if (hdr->address != HDLC_ADDR_ALLSTATIONS ||
453 hdr->control != HDLC_CTRL_UI)
454 goto rx_error;
455
456 pid = ntohs(hdr->protocol);
457 proto = get_proto(dev, pid);
458 if (!proto) {
459 if (ppp->protos[IDX_LCP].state == OPENED)
460 ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ,
461 ++ppp->seq, skb->len + 2, &hdr->protocol);
462 goto rx_error;
463 }
464
465 len = ntohs(cp->len);
466 if (len < sizeof(struct cp_header) /* no complete CP header? */ ||
467 skb->len < len /* truncated packet? */)
468 goto rx_error;
469 skb_pull(skb, sizeof(struct cp_header));
470 len -= sizeof(struct cp_header);
471
472 /* HDLC and CP headers stripped from skb */
473#if DEBUG_CP
474 if (cp->code < CP_CODES)
475 sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code],
476 cp->id);
477 else
478 sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id);
479 ptr = debug_buffer + strlen(debug_buffer);
480 for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) {
481 sprintf(ptr, " %02X", skb->data[i]);
482 ptr += strlen(ptr);
483 }
484 printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid),
485 debug_buffer);
486#endif
487
488 /* LCP only */
489 if (pid == PID_LCP)
490 switch (cp->code) {
491 case LCP_PROTO_REJ:
492 pid = ntohs(*(__be16*)skb->data);
493 if (pid == PID_LCP || pid == PID_IPCP ||
494 pid == PID_IPV6CP)
495 ppp_cp_event(dev, pid, RXJ_BAD, 0, 0,
496 0, NULL);
497 goto out;
498
499 case LCP_ECHO_REQ: /* send Echo-Reply */
500 if (len >= 4 && proto->state == OPENED)
501 ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY,
502 cp->id, len - 4, skb->data + 4);
503 goto out;
504
505 case LCP_ECHO_REPLY:
506 if (cp->id == ppp->echo_id)
507 ppp->last_pong = jiffies;
508 goto out;
509
510 case LCP_DISC_REQ: /* discard */
511 goto out;
512 }
513
514 /* LCP, IPCP and IPV6CP */
515 switch (cp->code) {
516 case CP_CONF_REQ:
517 ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data);
518 goto out;
519
520 case CP_CONF_ACK:
521 if (cp->id == proto->cr_id)
522 ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL);
523 goto out;
524
525 case CP_CONF_REJ:
526 case CP_CONF_NAK:
527 if (cp->id == proto->cr_id)
528 ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL);
529 goto out;
530
531 case CP_TERM_REQ:
532 ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
533 goto out;
534
535 case CP_TERM_ACK:
536 ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
537 goto out;
538
539 case CP_CODE_REJ:
540 ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
541 goto out;
542
543 default:
544 len += sizeof(struct cp_header);
545 if (len > dev->mtu)
546 len = dev->mtu;
547 ppp_cp_event(dev, pid, RUC, 0, 0, len, cp);
548 goto out;
549 }
550 goto out;
551
552rx_error:
553 dev->stats.rx_errors++;
554out:
555 spin_unlock_irqrestore(&ppp->lock, flags);
556 dev_kfree_skb_any(skb);
557 ppp_tx_flush();
558 return NET_RX_DROP;
559}
560
561static void ppp_timer(unsigned long arg)
562{
563 struct proto *proto = (struct proto *)arg;
564 struct ppp *ppp = get_ppp(proto->dev);
565 unsigned long flags;
566
567 spin_lock_irqsave(&ppp->lock, flags);
568 switch (proto->state) {
569 case STOPPING:
570 case REQ_SENT:
571 case ACK_RECV:
572 case ACK_SENT:
573 if (proto->restart_counter) {
574 ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
575 0, NULL);
576 proto->restart_counter--;
577 } else
578 ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
579 0, NULL);
580 break;
581
582 case OPENED:
583 if (proto->pid != PID_LCP)
584 break;
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);
597 }
598 break;
599 }
600 spin_unlock_irqrestore(&ppp->lock, flags);
601 ppp_tx_flush();
602}
603
604
605static void ppp_start(struct net_device *dev)
606{
607 struct ppp *ppp = get_ppp(dev);
608 int i;
609
610 for (i = 0; i < IDX_COUNT; i++) {
611 struct proto *proto = &ppp->protos[i];
612 proto->dev = dev;
613 init_timer(&proto->timer);
614 proto->timer.function = ppp_timer;
615 proto->timer.data = (unsigned long)proto;
616 proto->state = CLOSED;
617 }
618 ppp->protos[IDX_LCP].pid = PID_LCP;
619 ppp->protos[IDX_IPCP].pid = PID_IPCP;
620 ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
621
622 ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
623}
624
625static void ppp_stop(struct net_device *dev)
626{
627 ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
628}
629
630static void ppp_close(struct net_device *dev)
631{
632 ppp_tx_flush();
633}
634
635static struct hdlc_proto proto = {
636 .start = ppp_start,
637 .stop = ppp_stop,
638 .close = ppp_close,
639 .type_trans = ppp_type_trans,
640 .ioctl = ppp_ioctl,
641 .netif_rx = ppp_rx,
642 .module = THIS_MODULE,
643};
644
645static const struct header_ops ppp_header_ops = {
646 .create = ppp_hard_header,
647};
648
649static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
650{
651 hdlc_device *hdlc = dev_to_hdlc(dev);
652 struct ppp *ppp;
653 int result;
654
655 switch (ifr->ifr_settings.type) {
656 case IF_GET_PROTO:
657 if (dev_to_hdlc(dev)->proto != &proto)
658 return -EINVAL;
659 ifr->ifr_settings.type = IF_PROTO_PPP;
660 return 0; /* return protocol only, no settable parameters */
661
662 case IF_PROTO_PPP:
663 if (!capable(CAP_NET_ADMIN))
664 return -EPERM;
665
666 if (dev->flags & IFF_UP)
667 return -EBUSY;
668
669 /* no settable parameters */
670
671 result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
672 if (result)
673 return result;
674
675 result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
676 if (result)
677 return result;
678
679 ppp = get_ppp(dev);
680 spin_lock_init(&ppp->lock);
681 ppp->req_timeout = 2;
682 ppp->cr_retries = 10;
683 ppp->term_retries = 2;
684 ppp->keepalive_interval = 10;
685 ppp->keepalive_timeout = 60;
686
687 dev->hard_header_len = sizeof(struct hdlc_header);
688 dev->header_ops = &ppp_header_ops;
689 dev->type = ARPHRD_PPP;
690 netif_dormant_on(dev);
691 return 0;
692 }
693
694 return -EINVAL;
695}
696
697
698static int __init mod_init(void)
699{
700 skb_queue_head_init(&tx_queue);
701 register_hdlc_protocol(&proto);
702 return 0;
703}
704
705static void __exit mod_exit(void)
706{
707 unregister_hdlc_protocol(&proto);
708}
709
710
711module_init(mod_init);
712module_exit(mod_exit);
713
714MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
715MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
716MODULE_LICENSE("GPL v2");