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 
38 enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
39 enum {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
43 static const char *const code_names[CP_CODES] = {
44 	"0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
45 	"TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
46 };
47 static char debug_buffer[64 + 3 * DEBUG_CP];
48 #endif
49 
50 enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
51 
52 struct hdlc_header {
53 	u8 address;
54 	u8 control;
55 	__be16 protocol;
56 };
57 
58 struct cp_header {
59 	u8 code;
60 	u8 id;
61 	__be16 len;
62 };
63 
64 
65 struct 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 
75 struct 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 
85 enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
86       STATES, STATE_MASK = 0xF};
87 enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
88       RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
89 enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
90       SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
91 
92 #if DEBUG_STATE
93 static const char *const state_names[STATES] = {
94 	"Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
95 	"Opened"
96 };
97 static 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 
103 static struct sk_buff_head tx_queue; /* used when holding the spin lock */
104 
105 static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
106 
get_ppp(struct net_device * dev)107 static inline struct ppp* get_ppp(struct net_device *dev)
108 {
109 	return (struct ppp *)dev_to_hdlc(dev)->state;
110 }
111 
get_proto(struct net_device * dev,u16 pid)112 static 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 
proto_name(u16 pid)128 static 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 
ppp_type_trans(struct sk_buff * skb,struct net_device * dev)142 static __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 
ppp_hard_header(struct sk_buff * skb,struct net_device * dev,u16 type,const void * daddr,const void * saddr,unsigned int len)167 static 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 
ppp_tx_flush(void)200 static 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 
ppp_tx_cp(struct net_device * dev,u16 pid,u8 code,u8 id,unsigned int len,const void * data)207 static 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 		printk(KERN_WARNING "%s: out of memory in ppp_tx_cp()\n",
227 		       dev->name);
228 		return;
229 	}
230 	skb_reserve(skb, sizeof(struct hdlc_header));
231 
232 	cp = (struct cp_header *)skb_put(skb, sizeof(struct cp_header));
233 	cp->code = code;
234 	cp->id = id;
235 	cp->len = htons(sizeof(struct cp_header) + magic_len + len);
236 
237 	if (magic_len)
238 		memcpy(skb_put(skb, magic_len), &magic, magic_len);
239 	if (len)
240 		memcpy(skb_put(skb, len), data, len);
241 
242 #if DEBUG_CP
243 	BUG_ON(code >= CP_CODES);
244 	ptr = debug_buffer;
245 	*ptr = '\x0';
246 	for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
247 		sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
248 		ptr += strlen(ptr);
249 	}
250 	printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
251 	       proto_name(pid), code_names[code], id, debug_buffer);
252 #endif
253 
254 	ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
255 
256 	skb->priority = TC_PRIO_CONTROL;
257 	skb->dev = dev;
258 	skb_reset_network_header(skb);
259 	skb_queue_tail(&tx_queue, skb);
260 }
261 
262 
263 /* State transition table (compare STD-51)
264    Events                                   Actions
265    TO+  = Timeout with counter > 0          irc = Initialize-Restart-Count
266    TO-  = Timeout with counter expired      zrc = Zero-Restart-Count
267 
268    RCR+ = Receive-Configure-Request (Good)  scr = Send-Configure-Request
269    RCR- = Receive-Configure-Request (Bad)
270    RCA  = Receive-Configure-Ack             sca = Send-Configure-Ack
271    RCN  = Receive-Configure-Nak/Rej         scn = Send-Configure-Nak/Rej
272 
273    RTR  = Receive-Terminate-Request         str = Send-Terminate-Request
274    RTA  = Receive-Terminate-Ack             sta = Send-Terminate-Ack
275 
276    RUC  = Receive-Unknown-Code              scj = Send-Code-Reject
277    RXJ+ = Receive-Code-Reject (permitted)
278        or Receive-Protocol-Reject
279    RXJ- = Receive-Code-Reject (catastrophic)
280        or Receive-Protocol-Reject
281 */
282 static int cp_table[EVENTS][STATES] = {
283 	/* CLOSED     STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
284 	     0           1         2       3       4      5          6    */
285 	{IRC|SCR|3,     INV     , INV ,   INV   , INV ,  INV    ,   INV   }, /* START */
286 	{   INV   ,      0      ,  0  ,    0    ,  0  ,   0     ,    0    }, /* STOP */
287 	{   INV   ,     INV     ,STR|2,  SCR|3  ,SCR|3,  SCR|5  ,   INV   }, /* TO+ */
288 	{   INV   ,     INV     ,  1  ,    1    ,  1  ,    1    ,   INV   }, /* TO- */
289 	{  STA|0  ,IRC|SCR|SCA|5,  2  ,  SCA|5  ,SCA|6,  SCA|5  ,SCR|SCA|5}, /* RCR+ */
290 	{  STA|0  ,IRC|SCR|SCN|3,  2  ,  SCN|3  ,SCN|4,  SCN|3  ,SCR|SCN|3}, /* RCR- */
291 	{  STA|0  ,    STA|1    ,  2  ,  IRC|4  ,SCR|3,    6    , SCR|3   }, /* RCA */
292 	{  STA|0  ,    STA|1    ,  2  ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3   }, /* RCN */
293 	{  STA|0  ,    STA|1    ,STA|2,  STA|3  ,STA|3,  STA|3  ,ZRC|STA|2}, /* RTR */
294 	{    0    ,      1      ,  1  ,    3    ,  3  ,    5    ,  SCR|3  }, /* RTA */
295 	{  SCJ|0  ,    SCJ|1    ,SCJ|2,  SCJ|3  ,SCJ|4,  SCJ|5  ,  SCJ|6  }, /* RUC */
296 	{    0    ,      1      ,  2  ,    3    ,  3  ,    5    ,    6    }, /* RXJ+ */
297 	{    0    ,      1      ,  1  ,    1    ,  1  ,    1    ,IRC|STR|2}, /* RXJ- */
298 };
299 
300 
301 /* SCA: RCR+ must supply id, len and data
302    SCN: RCR- must supply code, id, len and data
303    STA: RTR must supply id
304    SCJ: RUC must supply CP packet len and data */
ppp_cp_event(struct net_device * dev,u16 pid,u16 event,u8 code,u8 id,unsigned int len,const void * data)305 static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
306 			 u8 id, unsigned int len, const void *data)
307 {
308 	int old_state, action;
309 	struct ppp *ppp = get_ppp(dev);
310 	struct proto *proto = get_proto(dev, pid);
311 
312 	old_state = proto->state;
313 	BUG_ON(old_state >= STATES);
314 	BUG_ON(event >= EVENTS);
315 
316 #if DEBUG_STATE
317 	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
318 	       proto_name(pid), event_names[event], state_names[proto->state]);
319 #endif
320 
321 	action = cp_table[event][old_state];
322 
323 	proto->state = action & STATE_MASK;
324 	if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
325 		mod_timer(&proto->timer, proto->timeout =
326 			  jiffies + ppp->req_timeout * HZ);
327 	if (action & ZRC)
328 		proto->restart_counter = 0;
329 	if (action & IRC)
330 		proto->restart_counter = (proto->state == STOPPING) ?
331 			ppp->term_retries : ppp->cr_retries;
332 
333 	if (action & SCR)	/* send Configure-Request */
334 		ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
335 			  0, NULL);
336 	if (action & SCA)	/* send Configure-Ack */
337 		ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
338 	if (action & SCN)	/* send Configure-Nak/Reject */
339 		ppp_tx_cp(dev, pid, code, id, len, data);
340 	if (action & STR)	/* send Terminate-Request */
341 		ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
342 	if (action & STA)	/* send Terminate-Ack */
343 		ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
344 	if (action & SCJ)	/* send Code-Reject */
345 		ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
346 
347 	if (old_state != OPENED && proto->state == OPENED) {
348 		printk(KERN_INFO "%s: %s up\n", dev->name, proto_name(pid));
349 		if (pid == PID_LCP) {
350 			netif_dormant_off(dev);
351 			ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
352 			ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
353 			ppp->last_pong = jiffies;
354 			mod_timer(&proto->timer, proto->timeout =
355 				  jiffies + ppp->keepalive_interval * HZ);
356 		}
357 	}
358 	if (old_state == OPENED && proto->state != OPENED) {
359 		printk(KERN_INFO "%s: %s down\n", dev->name, proto_name(pid));
360 		if (pid == PID_LCP) {
361 			netif_dormant_on(dev);
362 			ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
363 			ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
364 		}
365 	}
366 	if (old_state != CLOSED && proto->state == CLOSED)
367 		del_timer(&proto->timer);
368 
369 #if DEBUG_STATE
370 	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
371 	       proto_name(pid), event_names[event], state_names[proto->state]);
372 #endif
373 }
374 
375 
ppp_cp_parse_cr(struct net_device * dev,u16 pid,u8 id,unsigned int req_len,const u8 * data)376 static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
377 			    unsigned int req_len, const u8 *data)
378 {
379 	static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
380 	const u8 *opt;
381 	u8 *out;
382 	unsigned int len = req_len, nak_len = 0, rej_len = 0;
383 
384 	if (!(out = kmalloc(len, GFP_ATOMIC))) {
385 		dev->stats.rx_dropped++;
386 		return;	/* out of memory, ignore CR packet */
387 	}
388 
389 	for (opt = data; len; len -= opt[1], opt += opt[1]) {
390 		if (len < 2 || len < opt[1]) {
391 			dev->stats.rx_errors++;
392 			kfree(out);
393 			return; /* bad packet, drop silently */
394 		}
395 
396 		if (pid == PID_LCP)
397 			switch (opt[0]) {
398 			case LCP_OPTION_MRU:
399 				continue; /* MRU always OK and > 1500 bytes? */
400 
401 			case LCP_OPTION_ACCM: /* async control character map */
402 				if (!memcmp(opt, valid_accm,
403 					    sizeof(valid_accm)))
404 					continue;
405 				if (!rej_len) { /* NAK it */
406 					memcpy(out + nak_len, valid_accm,
407 					       sizeof(valid_accm));
408 					nak_len += sizeof(valid_accm);
409 					continue;
410 				}
411 				break;
412 			case LCP_OPTION_MAGIC:
413 				if (opt[1] != 6 || (!opt[2] && !opt[3] &&
414 						    !opt[4] && !opt[5]))
415 					break; /* reject invalid magic number */
416 				continue;
417 			}
418 		/* reject this option */
419 		memcpy(out + rej_len, opt, opt[1]);
420 		rej_len += opt[1];
421 	}
422 
423 	if (rej_len)
424 		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
425 	else if (nak_len)
426 		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
427 	else
428 		ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data);
429 
430 	kfree(out);
431 }
432 
ppp_rx(struct sk_buff * skb)433 static 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 = (struct cp_header*)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 		goto out;
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 		goto out;
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 		goto out;
531 
532 	case CP_TERM_REQ:
533 		ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
534 		goto out;
535 
536 	case CP_TERM_ACK:
537 		ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
538 		goto out;
539 
540 	case CP_CODE_REJ:
541 		ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
542 		goto out;
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 		goto out;
550 	}
551 	goto out;
552 
553 rx_error:
554 	dev->stats.rx_errors++;
555 out:
556 	spin_unlock_irqrestore(&ppp->lock, flags);
557 	dev_kfree_skb_any(skb);
558 	ppp_tx_flush();
559 	return NET_RX_DROP;
560 }
561 
ppp_timer(unsigned long arg)562 static void ppp_timer(unsigned long arg)
563 {
564 	struct proto *proto = (struct proto *)arg;
565 	struct ppp *ppp = get_ppp(proto->dev);
566 	unsigned long flags;
567 
568 	spin_lock_irqsave(&ppp->lock, flags);
569 	switch (proto->state) {
570 	case STOPPING:
571 	case REQ_SENT:
572 	case ACK_RECV:
573 	case ACK_SENT:
574 		if (proto->restart_counter) {
575 			ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
576 				     0, NULL);
577 			proto->restart_counter--;
578 		} else
579 			ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
580 				     0, NULL);
581 		break;
582 
583 	case OPENED:
584 		if (proto->pid != PID_LCP)
585 			break;
586 		if (time_after(jiffies, ppp->last_pong +
587 			       ppp->keepalive_timeout * HZ)) {
588 			printk(KERN_INFO "%s: Link down\n", proto->dev->name);
589 			ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
590 			ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
591 		} else {	/* send keep-alive packet */
592 			ppp->echo_id = ++ppp->seq;
593 			ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
594 				  ppp->echo_id, 0, NULL);
595 			proto->timer.expires = jiffies +
596 				ppp->keepalive_interval * HZ;
597 			add_timer(&proto->timer);
598 		}
599 		break;
600 	}
601 	spin_unlock_irqrestore(&ppp->lock, flags);
602 	ppp_tx_flush();
603 }
604 
605 
ppp_start(struct net_device * dev)606 static void ppp_start(struct net_device *dev)
607 {
608 	struct ppp *ppp = get_ppp(dev);
609 	int i;
610 
611 	for (i = 0; i < IDX_COUNT; i++) {
612 		struct proto *proto = &ppp->protos[i];
613 		proto->dev = dev;
614 		init_timer(&proto->timer);
615 		proto->timer.function = ppp_timer;
616 		proto->timer.data = (unsigned long)proto;
617 		proto->state = CLOSED;
618 	}
619 	ppp->protos[IDX_LCP].pid = PID_LCP;
620 	ppp->protos[IDX_IPCP].pid = PID_IPCP;
621 	ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
622 
623 	ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
624 }
625 
ppp_stop(struct net_device * dev)626 static void ppp_stop(struct net_device *dev)
627 {
628 	ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
629 }
630 
ppp_close(struct net_device * dev)631 static void ppp_close(struct net_device *dev)
632 {
633 	ppp_tx_flush();
634 }
635 
636 static struct hdlc_proto proto = {
637 	.start		= ppp_start,
638 	.stop		= ppp_stop,
639 	.close		= ppp_close,
640 	.type_trans	= ppp_type_trans,
641 	.ioctl		= ppp_ioctl,
642 	.netif_rx	= ppp_rx,
643 	.module		= THIS_MODULE,
644 };
645 
646 static const struct header_ops ppp_header_ops = {
647 	.create = ppp_hard_header,
648 };
649 
ppp_ioctl(struct net_device * dev,struct ifreq * ifr)650 static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
651 {
652 	hdlc_device *hdlc = dev_to_hdlc(dev);
653 	struct ppp *ppp;
654 	int result;
655 
656 	switch (ifr->ifr_settings.type) {
657 	case IF_GET_PROTO:
658 		if (dev_to_hdlc(dev)->proto != &proto)
659 			return -EINVAL;
660 		ifr->ifr_settings.type = IF_PROTO_PPP;
661 		return 0; /* return protocol only, no settable parameters */
662 
663 	case IF_PROTO_PPP:
664 		if (!capable(CAP_NET_ADMIN))
665 			return -EPERM;
666 
667 		if (dev->flags & IFF_UP)
668 			return -EBUSY;
669 
670 		/* no settable parameters */
671 
672 		result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
673 		if (result)
674 			return result;
675 
676 		result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
677 		if (result)
678 			return result;
679 
680 		ppp = get_ppp(dev);
681 		spin_lock_init(&ppp->lock);
682 		ppp->req_timeout = 2;
683 		ppp->cr_retries = 10;
684 		ppp->term_retries = 2;
685 		ppp->keepalive_interval = 10;
686 		ppp->keepalive_timeout = 60;
687 
688 		dev->hard_header_len = sizeof(struct hdlc_header);
689 		dev->header_ops = &ppp_header_ops;
690 		dev->type = ARPHRD_PPP;
691 		netif_dormant_on(dev);
692 		return 0;
693 	}
694 
695 	return -EINVAL;
696 }
697 
698 
mod_init(void)699 static int __init mod_init(void)
700 {
701 	skb_queue_head_init(&tx_queue);
702 	register_hdlc_protocol(&proto);
703 	return 0;
704 }
705 
mod_exit(void)706 static void __exit mod_exit(void)
707 {
708 	unregister_hdlc_protocol(&proto);
709 }
710 
711 
712 module_init(mod_init);
713 module_exit(mod_exit);
714 
715 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
716 MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
717 MODULE_LICENSE("GPL v2");
718