1 /*
2  *	NET3:	A (fairly minimal) implementation of synchronous PPP for Linux
3  *		as well as a CISCO HDLC implementation. See the copyright
4  *		message below for the original source.
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License
8  *	as published by the Free Software Foundation; either version
9  *	2 of the license, or (at your option) any later version.
10  *
11  *	Note however. This code is also used in a different form by FreeBSD.
12  *	Therefore when making any non OS specific change please consider
13  *	contributing it back to the original author under the terms
14  *	below in addition.
15  *		-- Alan
16  *
17  *	Port for Linux-2.1 by Jan "Yenya" Kasprzak <kas@fi.muni.cz>
18  */
19 
20 /*
21  * Synchronous PPP/Cisco link level subroutines.
22  * Keepalive protocol implemented in both Cisco and PPP modes.
23  *
24  * Copyright (C) 1994 Cronyx Ltd.
25  * Author: Serge Vakulenko, <vak@zebub.msk.su>
26  *
27  * This software is distributed with NO WARRANTIES, not even the implied
28  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Authors grant any other persons or organisations permission to use
31  * or modify this software as long as this message is kept with the software,
32  * all derivative works or modified versions.
33  *
34  * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
35  *
36  * $Id: syncppp.c,v 1.18 2000/04/11 05:25:31 asj Exp $
37  */
38 #undef DEBUG
39 
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/errno.h>
44 #include <linux/sched.h>
45 #include <linux/init.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
48 #include <linux/route.h>
49 #include <linux/netdevice.h>
50 #include <linux/inetdevice.h>
51 #include <linux/random.h>
52 #include <linux/pkt_sched.h>
53 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
55 #include <linux/spinlock.h>
56 #include <net/syncppp.h>
57 
58 #define MAXALIVECNT     6               /* max. alive packets */
59 
60 #define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
61 #define PPP_UI          0x03            /* Unnumbered Information */
62 #define PPP_IP          0x0021          /* Internet Protocol */
63 #define PPP_ISO         0x0023          /* ISO OSI Protocol */
64 #define PPP_XNS         0x0025          /* Xerox NS Protocol */
65 #define PPP_IPX         0x002b          /* Novell IPX Protocol */
66 #define PPP_LCP         0xc021          /* Link Control Protocol */
67 #define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
68 
69 #define LCP_CONF_REQ    1               /* PPP LCP configure request */
70 #define LCP_CONF_ACK    2               /* PPP LCP configure acknowledge */
71 #define LCP_CONF_NAK    3               /* PPP LCP configure negative ack */
72 #define LCP_CONF_REJ    4               /* PPP LCP configure reject */
73 #define LCP_TERM_REQ    5               /* PPP LCP terminate request */
74 #define LCP_TERM_ACK    6               /* PPP LCP terminate acknowledge */
75 #define LCP_CODE_REJ    7               /* PPP LCP code reject */
76 #define LCP_PROTO_REJ   8               /* PPP LCP protocol reject */
77 #define LCP_ECHO_REQ    9               /* PPP LCP echo request */
78 #define LCP_ECHO_REPLY  10              /* PPP LCP echo reply */
79 #define LCP_DISC_REQ    11              /* PPP LCP discard request */
80 
81 #define LCP_OPT_MRU             1       /* maximum receive unit */
82 #define LCP_OPT_ASYNC_MAP       2       /* async control character map */
83 #define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
84 #define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
85 #define LCP_OPT_MAGIC           5       /* magic number */
86 #define LCP_OPT_RESERVED        6       /* reserved */
87 #define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
88 #define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
89 
90 #define IPCP_CONF_REQ   LCP_CONF_REQ    /* PPP IPCP configure request */
91 #define IPCP_CONF_ACK   LCP_CONF_ACK    /* PPP IPCP configure acknowledge */
92 #define IPCP_CONF_NAK   LCP_CONF_NAK    /* PPP IPCP configure negative ack */
93 #define IPCP_CONF_REJ   LCP_CONF_REJ    /* PPP IPCP configure reject */
94 #define IPCP_TERM_REQ   LCP_TERM_REQ    /* PPP IPCP terminate request */
95 #define IPCP_TERM_ACK   LCP_TERM_ACK    /* PPP IPCP terminate acknowledge */
96 #define IPCP_CODE_REJ   LCP_CODE_REJ    /* PPP IPCP code reject */
97 
98 #define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
99 #define CISCO_UNICAST           0x0f    /* Cisco unicast address */
100 #define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
101 #define CISCO_ADDR_REQ          0       /* Cisco address request */
102 #define CISCO_ADDR_REPLY        1       /* Cisco address reply */
103 #define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
104 
105 struct ppp_header {
106 	u8 address;
107 	u8 control;
108 	u16 protocol;
109 };
110 #define PPP_HEADER_LEN          sizeof (struct ppp_header)
111 
112 struct lcp_header {
113 	u8 type;
114 	u8 ident;
115 	u16 len;
116 };
117 #define LCP_HEADER_LEN          sizeof (struct lcp_header)
118 
119 struct cisco_packet {
120 	u32 type;
121 	u32 par1;
122 	u32 par2;
123 	u16 rel;
124 	u16 time0;
125 	u16 time1;
126 };
127 #define CISCO_PACKET_LEN 18
128 #define CISCO_BIG_PACKET_LEN 20
129 
130 static struct sppp *spppq;
131 static struct timer_list sppp_keepalive_timer;
132 static spinlock_t spppq_lock = SPIN_LOCK_UNLOCKED;
133 
134 static void sppp_keepalive (unsigned long dummy);
135 static void sppp_cp_send (struct sppp *sp, u16 proto, u8 type,
136 	u8 ident, u16 len, void *data);
137 static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2);
138 static void sppp_lcp_input (struct sppp *sp, struct sk_buff *m);
139 static void sppp_cisco_input (struct sppp *sp, struct sk_buff *m);
140 static void sppp_ipcp_input (struct sppp *sp, struct sk_buff *m);
141 static void sppp_lcp_open (struct sppp *sp);
142 static void sppp_ipcp_open (struct sppp *sp);
143 static int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
144 	int len, u32 *magic);
145 static void sppp_cp_timeout (unsigned long arg);
146 static char *sppp_lcp_type_name (u8 type);
147 static char *sppp_ipcp_type_name (u8 type);
148 static void sppp_print_bytes (u8 *p, u16 len);
149 
150 static int debug;
151 
152 
153 /*
154  *	Interface down stub
155  */
156 
if_down(struct net_device * dev)157 static void if_down(struct net_device *dev)
158 {
159 	struct sppp *sp = (struct sppp *)sppp_of(dev);
160 
161 	sp->pp_link_state=SPPP_LINK_DOWN;
162 }
163 
164 /*
165  * Timeout routine activations.
166  */
167 
sppp_set_timeout(struct sppp * p,int s)168 static void sppp_set_timeout(struct sppp *p,int s)
169 {
170 	if (! (p->pp_flags & PP_TIMO))
171 	{
172 		init_timer(&p->pp_timer);
173 		p->pp_timer.function=sppp_cp_timeout;
174 		p->pp_timer.expires=jiffies+s*HZ;
175 		p->pp_timer.data=(unsigned long)p;
176 		p->pp_flags |= PP_TIMO;
177 		add_timer(&p->pp_timer);
178 	}
179 }
180 
sppp_clear_timeout(struct sppp * p)181 static void sppp_clear_timeout(struct sppp *p)
182 {
183 	if (p->pp_flags & PP_TIMO)
184 	{
185 		del_timer(&p->pp_timer);
186 		p->pp_flags &= ~PP_TIMO;
187 	}
188 }
189 
190 /**
191  *	sppp_input -	receive and process a WAN PPP frame
192  *	@skb:	The buffer to process
193  *	@dev:	The device it arrived on
194  *
195  *	This can be called directly by cards that do not have
196  *	timing constraints but is normally called from the network layer
197  *	after interrupt servicing to process frames queued via netif_rx().
198  *
199  *	We process the options in the card. If the frame is destined for
200  *	the protocol stacks then it requeues the frame for the upper level
201  *	protocol. If it is a control from it is processed and discarded
202  *	here.
203  */
204 
sppp_input(struct net_device * dev,struct sk_buff * skb)205 void sppp_input (struct net_device *dev, struct sk_buff *skb)
206 {
207 	struct ppp_header *h;
208 	struct sppp *sp = (struct sppp *)sppp_of(dev);
209 
210 	skb->dev=dev;
211 	skb->mac.raw=skb->data;
212 
213 	if (dev->flags & IFF_RUNNING)
214 	{
215 		/* Count received bytes, add FCS and one flag */
216 		sp->ibytes+= skb->len + 3;
217 		sp->ipkts++;
218 	}
219 
220 	if (skb->len <= PPP_HEADER_LEN) {
221 		/* Too small packet, drop it. */
222 		if (sp->pp_flags & PP_DEBUG)
223 			printk (KERN_DEBUG "%s: input packet is too small, %d bytes\n",
224 				dev->name, skb->len);
225 drop:           kfree_skb(skb);
226 		return;
227 	}
228 
229 	/* Get PPP header. */
230 	h = (struct ppp_header *)skb->data;
231 	skb_pull(skb,sizeof(struct ppp_header));
232 
233 	switch (h->address) {
234 	default:        /* Invalid PPP packet. */
235 invalid:        if (sp->pp_flags & PP_DEBUG)
236 			printk (KERN_WARNING "%s: invalid input packet <0x%x 0x%x 0x%x>\n",
237 				dev->name,
238 				h->address, h->control, ntohs (h->protocol));
239 		goto drop;
240 	case PPP_ALLSTATIONS:
241 		if (h->control != PPP_UI)
242 			goto invalid;
243 		if (sp->pp_flags & PP_CISCO) {
244 			if (sp->pp_flags & PP_DEBUG)
245 				printk (KERN_WARNING "%s: PPP packet in Cisco mode <0x%x 0x%x 0x%x>\n",
246 					dev->name,
247 					h->address, h->control, ntohs (h->protocol));
248 			goto drop;
249 		}
250 		switch (ntohs (h->protocol)) {
251 		default:
252 			if (sp->lcp.state == LCP_STATE_OPENED)
253 				sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ,
254 					++sp->pp_seq, skb->len + 2,
255 					&h->protocol);
256 			if (sp->pp_flags & PP_DEBUG)
257 				printk (KERN_WARNING "%s: invalid input protocol <0x%x 0x%x 0x%x>\n",
258 					dev->name,
259 					h->address, h->control, ntohs (h->protocol));
260 			goto drop;
261 		case PPP_LCP:
262 			sppp_lcp_input (sp, skb);
263 			kfree_skb(skb);
264 			return;
265 		case PPP_IPCP:
266 			if (sp->lcp.state == LCP_STATE_OPENED)
267 				sppp_ipcp_input (sp, skb);
268 			else
269 				printk(KERN_DEBUG "IPCP when still waiting LCP finish.\n");
270 			kfree_skb(skb);
271 			return;
272 		case PPP_IP:
273 			if (sp->ipcp.state == IPCP_STATE_OPENED) {
274 				if(sp->pp_flags&PP_DEBUG)
275 					printk(KERN_DEBUG "Yow an IP frame.\n");
276 				skb->protocol=htons(ETH_P_IP);
277 				netif_rx(skb);
278 				return;
279 			}
280 			break;
281 #ifdef IPX
282 		case PPP_IPX:
283 			/* IPX IPXCP not implemented yet */
284 			if (sp->lcp.state == LCP_STATE_OPENED) {
285 				skb->protocol=htons(ETH_P_IPX);
286 				netif_rx(skb);
287 				return;
288 			}
289 			break;
290 #endif
291 		}
292 		break;
293 	case CISCO_MULTICAST:
294 	case CISCO_UNICAST:
295 		/* Don't check the control field here (RFC 1547). */
296 		if (! (sp->pp_flags & PP_CISCO)) {
297 			if (sp->pp_flags & PP_DEBUG)
298 				printk (KERN_WARNING "%s: Cisco packet in PPP mode <0x%x 0x%x 0x%x>\n",
299 					dev->name,
300 					h->address, h->control, ntohs (h->protocol));
301 			goto drop;
302 		}
303 		switch (ntohs (h->protocol)) {
304 		default:
305 			goto invalid;
306 		case CISCO_KEEPALIVE:
307 			sppp_cisco_input (sp, skb);
308 			kfree_skb(skb);
309 			return;
310 #ifdef CONFIG_INET
311 		case ETH_P_IP:
312 			skb->protocol=htons(ETH_P_IP);
313 			netif_rx(skb);
314 			return;
315 #endif
316 #ifdef CONFIG_IPX
317 		case ETH_P_IPX:
318 			skb->protocol=htons(ETH_P_IPX);
319 			netif_rx(skb);
320 			return;
321 #endif
322 		}
323 		break;
324 	}
325 	kfree_skb(skb);
326 }
327 
328 EXPORT_SYMBOL(sppp_input);
329 
330 /*
331  *	Handle transmit packets.
332  */
333 
sppp_hard_header(struct sk_buff * skb,struct net_device * dev,__u16 type,void * daddr,void * saddr,unsigned int len)334 static int sppp_hard_header(struct sk_buff *skb, struct net_device *dev, __u16 type,
335 		void *daddr, void *saddr, unsigned int len)
336 {
337 	struct sppp *sp = (struct sppp *)sppp_of(dev);
338 	struct ppp_header *h;
339 	skb_push(skb,sizeof(struct ppp_header));
340 	h=(struct ppp_header *)skb->data;
341 	if(sp->pp_flags&PP_CISCO)
342 	{
343 		h->address = CISCO_UNICAST;
344 		h->control = 0;
345 	}
346 	else
347 	{
348 		h->address = PPP_ALLSTATIONS;
349 		h->control = PPP_UI;
350 	}
351 	if(sp->pp_flags & PP_CISCO)
352 	{
353 		h->protocol = htons(type);
354 	}
355 	else switch(type)
356 	{
357 		case ETH_P_IP:
358 			h->protocol = htons(PPP_IP);
359 			break;
360 		case ETH_P_IPX:
361 			h->protocol = htons(PPP_IPX);
362 			break;
363 	}
364 	return sizeof(struct ppp_header);
365 }
366 
sppp_rebuild_header(struct sk_buff * skb)367 static int sppp_rebuild_header(struct sk_buff *skb)
368 {
369 	return 0;
370 }
371 
372 /*
373  * Send keepalive packets, every 10 seconds.
374  */
375 
sppp_keepalive(unsigned long dummy)376 static void sppp_keepalive (unsigned long dummy)
377 {
378 	struct sppp *sp;
379 	unsigned long flags;
380 
381 	spin_lock_irqsave(&spppq_lock, flags);
382 
383 	for (sp=spppq; sp; sp=sp->pp_next)
384 	{
385 		struct net_device *dev = sp->pp_if;
386 
387 		/* Keepalive mode disabled or channel down? */
388 		if (! (sp->pp_flags & PP_KEEPALIVE) ||
389 		    ! (dev->flags & IFF_UP))
390 			continue;
391 
392 		/* No keepalive in PPP mode if LCP not opened yet. */
393 		if (! (sp->pp_flags & PP_CISCO) &&
394 		    sp->lcp.state != LCP_STATE_OPENED)
395 			continue;
396 
397 		if (sp->pp_alivecnt == MAXALIVECNT) {
398 			/* No keepalive packets got.  Stop the interface. */
399 			printk (KERN_WARNING "%s: protocol down\n", dev->name);
400 			if_down (dev);
401 			if (! (sp->pp_flags & PP_CISCO)) {
402 				/* Shut down the PPP link. */
403 				sp->lcp.magic = jiffies;
404 				sp->lcp.state = LCP_STATE_CLOSED;
405 				sp->ipcp.state = IPCP_STATE_CLOSED;
406 				sppp_clear_timeout (sp);
407 				/* Initiate negotiation. */
408 				sppp_lcp_open (sp);
409 			}
410 		}
411 		if (sp->pp_alivecnt <= MAXALIVECNT)
412 			++sp->pp_alivecnt;
413 		if (sp->pp_flags & PP_CISCO)
414 			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
415 				sp->pp_rseq);
416 		else if (sp->lcp.state == LCP_STATE_OPENED) {
417 			long nmagic = htonl (sp->lcp.magic);
418 			sp->lcp.echoid = ++sp->pp_seq;
419 			sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ,
420 				sp->lcp.echoid, 4, &nmagic);
421 		}
422 	}
423 	spin_unlock_irqrestore(&spppq_lock, flags);
424 	sppp_keepalive_timer.expires=jiffies+10*HZ;
425 	add_timer(&sppp_keepalive_timer);
426 }
427 
428 /*
429  * Handle incoming PPP Link Control Protocol packets.
430  */
431 
sppp_lcp_input(struct sppp * sp,struct sk_buff * skb)432 static void sppp_lcp_input (struct sppp *sp, struct sk_buff *skb)
433 {
434 	struct lcp_header *h;
435 	struct net_device *dev = sp->pp_if;
436 	int len = skb->len;
437 	u8 *p, opt[6];
438 	u32 rmagic;
439 
440 	if (len < 4) {
441 		if (sp->pp_flags & PP_DEBUG)
442 			printk (KERN_WARNING "%s: invalid lcp packet length: %d bytes\n",
443 				dev->name, len);
444 		return;
445 	}
446 	h = (struct lcp_header *)skb->data;
447 	skb_pull(skb,sizeof(struct lcp_header *));
448 
449 	if (sp->pp_flags & PP_DEBUG)
450 	{
451 		char state = '?';
452 		switch (sp->lcp.state) {
453 		case LCP_STATE_CLOSED:   state = 'C'; break;
454 		case LCP_STATE_ACK_RCVD: state = 'R'; break;
455 		case LCP_STATE_ACK_SENT: state = 'S'; break;
456 		case LCP_STATE_OPENED:   state = 'O'; break;
457 		}
458 		printk (KERN_WARNING "%s: lcp input(%c): %d bytes <%s id=%xh len=%xh",
459 			dev->name, state, len,
460 			sppp_lcp_type_name (h->type), h->ident, ntohs (h->len));
461 		if (len > 4)
462 			sppp_print_bytes ((u8*) (h+1), len-4);
463 		printk (">\n");
464 	}
465 	if (len > ntohs (h->len))
466 		len = ntohs (h->len);
467 	switch (h->type) {
468 	default:
469 		/* Unknown packet type -- send Code-Reject packet. */
470 		sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq,
471 			skb->len, h);
472 		break;
473 	case LCP_CONF_REQ:
474 		if (len < 4) {
475 			if (sp->pp_flags & PP_DEBUG)
476 				printk (KERN_DEBUG"%s: invalid lcp configure request packet length: %d bytes\n",
477 					dev->name, len);
478 			break;
479 		}
480 		if (len>4 && !sppp_lcp_conf_parse_options (sp, h, len, &rmagic))
481 			goto badreq;
482 		if (rmagic == sp->lcp.magic) {
483 			/* Local and remote magics equal -- loopback? */
484 			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
485 				printk (KERN_WARNING "%s: loopback\n",
486 					dev->name);
487 				sp->pp_loopcnt = 0;
488 				if (dev->flags & IFF_UP) {
489 					if_down (dev);
490 				}
491 			} else if (sp->pp_flags & PP_DEBUG)
492 				printk (KERN_DEBUG "%s: conf req: magic glitch\n",
493 					dev->name);
494 			++sp->pp_loopcnt;
495 
496 			/* MUST send Conf-Nack packet. */
497 			rmagic = ~sp->lcp.magic;
498 			opt[0] = LCP_OPT_MAGIC;
499 			opt[1] = sizeof (opt);
500 			opt[2] = rmagic >> 24;
501 			opt[3] = rmagic >> 16;
502 			opt[4] = rmagic >> 8;
503 			opt[5] = rmagic;
504 			sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK,
505 				h->ident, sizeof (opt), &opt);
506 badreq:
507 			switch (sp->lcp.state) {
508 			case LCP_STATE_OPENED:
509 				/* Initiate renegotiation. */
510 				sppp_lcp_open (sp);
511 				/* fall through... */
512 			case LCP_STATE_ACK_SENT:
513 				/* Go to closed state. */
514 				sp->lcp.state = LCP_STATE_CLOSED;
515 				sp->ipcp.state = IPCP_STATE_CLOSED;
516 			}
517 			break;
518 		}
519 		/* Send Configure-Ack packet. */
520 		sp->pp_loopcnt = 0;
521 		if (sp->lcp.state != LCP_STATE_OPENED) {
522 			sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
523 					h->ident, len-4, h+1);
524 		}
525 		/* Change the state. */
526 		switch (sp->lcp.state) {
527 		case LCP_STATE_CLOSED:
528 			sp->lcp.state = LCP_STATE_ACK_SENT;
529 			break;
530 		case LCP_STATE_ACK_RCVD:
531 			sp->lcp.state = LCP_STATE_OPENED;
532 			sppp_ipcp_open (sp);
533 			break;
534 		case LCP_STATE_OPENED:
535 			/* Remote magic changed -- close session. */
536 			sp->lcp.state = LCP_STATE_CLOSED;
537 			sp->ipcp.state = IPCP_STATE_CLOSED;
538 			/* Initiate renegotiation. */
539 			sppp_lcp_open (sp);
540 			/* Send ACK after our REQ in attempt to break loop */
541 			sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
542 					h->ident, len-4, h+1);
543 			sp->lcp.state = LCP_STATE_ACK_SENT;
544 			break;
545 		}
546 		break;
547 	case LCP_CONF_ACK:
548 		if (h->ident != sp->lcp.confid)
549 			break;
550 		sppp_clear_timeout (sp);
551 		if ((sp->pp_link_state != SPPP_LINK_UP) &&
552 		    (dev->flags & IFF_UP)) {
553 			/* Coming out of loopback mode. */
554 			sp->pp_link_state=SPPP_LINK_UP;
555 			printk (KERN_INFO "%s: protocol up\n", dev->name);
556 		}
557 		switch (sp->lcp.state) {
558 		case LCP_STATE_CLOSED:
559 			sp->lcp.state = LCP_STATE_ACK_RCVD;
560 			sppp_set_timeout (sp, 5);
561 			break;
562 		case LCP_STATE_ACK_SENT:
563 			sp->lcp.state = LCP_STATE_OPENED;
564 			sppp_ipcp_open (sp);
565 			break;
566 		}
567 		break;
568 	case LCP_CONF_NAK:
569 		if (h->ident != sp->lcp.confid)
570 			break;
571 		p = (u8*) (h+1);
572 		if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) {
573 			rmagic = (u32)p[2] << 24 |
574 				(u32)p[3] << 16 | p[4] << 8 | p[5];
575 			if (rmagic == ~sp->lcp.magic) {
576 				int newmagic;
577 				if (sp->pp_flags & PP_DEBUG)
578 					printk (KERN_DEBUG "%s: conf nak: magic glitch\n",
579 						dev->name);
580 				get_random_bytes(&newmagic, sizeof(newmagic));
581 				sp->lcp.magic += newmagic;
582 			} else
583 				sp->lcp.magic = rmagic;
584 			}
585 		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
586 			/* Go to closed state. */
587 			sp->lcp.state = LCP_STATE_CLOSED;
588 			sp->ipcp.state = IPCP_STATE_CLOSED;
589 		}
590 		/* The link will be renegotiated after timeout,
591 		 * to avoid endless req-nack loop. */
592 		sppp_clear_timeout (sp);
593 		sppp_set_timeout (sp, 2);
594 		break;
595 	case LCP_CONF_REJ:
596 		if (h->ident != sp->lcp.confid)
597 			break;
598 		sppp_clear_timeout (sp);
599 		/* Initiate renegotiation. */
600 		sppp_lcp_open (sp);
601 		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
602 			/* Go to closed state. */
603 			sp->lcp.state = LCP_STATE_CLOSED;
604 			sp->ipcp.state = IPCP_STATE_CLOSED;
605 		}
606 		break;
607 	case LCP_TERM_REQ:
608 		sppp_clear_timeout (sp);
609 		/* Send Terminate-Ack packet. */
610 		sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0);
611 		/* Go to closed state. */
612 		sp->lcp.state = LCP_STATE_CLOSED;
613 		sp->ipcp.state = IPCP_STATE_CLOSED;
614 		/* Initiate renegotiation. */
615 		sppp_lcp_open (sp);
616 		break;
617 	case LCP_TERM_ACK:
618 	case LCP_CODE_REJ:
619 	case LCP_PROTO_REJ:
620 		/* Ignore for now. */
621 		break;
622 	case LCP_DISC_REQ:
623 		/* Discard the packet. */
624 		break;
625 	case LCP_ECHO_REQ:
626 		if (sp->lcp.state != LCP_STATE_OPENED)
627 			break;
628 		if (len < 8) {
629 			if (sp->pp_flags & PP_DEBUG)
630 				printk (KERN_WARNING "%s: invalid lcp echo request packet length: %d bytes\n",
631 					dev->name, len);
632 			break;
633 		}
634 		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
635 			/* Line loopback mode detected. */
636 			printk (KERN_WARNING "%s: loopback\n", dev->name);
637 			if_down (dev);
638 
639 			/* Shut down the PPP link. */
640 			sp->lcp.state = LCP_STATE_CLOSED;
641 			sp->ipcp.state = IPCP_STATE_CLOSED;
642 			sppp_clear_timeout (sp);
643 			/* Initiate negotiation. */
644 			sppp_lcp_open (sp);
645 			break;
646 		}
647 		*(long*)(h+1) = htonl (sp->lcp.magic);
648 		sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1);
649 		break;
650 	case LCP_ECHO_REPLY:
651 		if (h->ident != sp->lcp.echoid)
652 			break;
653 		if (len < 8) {
654 			if (sp->pp_flags & PP_DEBUG)
655 				printk (KERN_WARNING "%s: invalid lcp echo reply packet length: %d bytes\n",
656 					dev->name, len);
657 			break;
658 		}
659 		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
660 		sp->pp_alivecnt = 0;
661 		break;
662 	}
663 }
664 
665 /*
666  * Handle incoming Cisco keepalive protocol packets.
667  */
668 
sppp_cisco_input(struct sppp * sp,struct sk_buff * skb)669 static void sppp_cisco_input (struct sppp *sp, struct sk_buff *skb)
670 {
671 	struct cisco_packet *h;
672 	struct net_device *dev = sp->pp_if;
673 
674 	if (skb->len != CISCO_PACKET_LEN && skb->len != CISCO_BIG_PACKET_LEN) {
675 		if (sp->pp_flags & PP_DEBUG)
676 			printk (KERN_WARNING "%s: invalid cisco packet length: %d bytes\n",
677 				dev->name,  skb->len);
678 		return;
679 	}
680 	h = (struct cisco_packet *)skb->data;
681 	skb_pull(skb, sizeof(struct cisco_packet*));
682 	if (sp->pp_flags & PP_DEBUG)
683 		printk (KERN_WARNING "%s: cisco input: %d bytes <%xh %xh %xh %xh %xh-%xh>\n",
684 			dev->name,  skb->len,
685 			ntohl (h->type), h->par1, h->par2, h->rel,
686 			h->time0, h->time1);
687 	switch (ntohl (h->type)) {
688 	default:
689 		if (sp->pp_flags & PP_DEBUG)
690 			printk (KERN_WARNING "%s: unknown cisco packet type: 0x%x\n",
691 				dev->name,  ntohl (h->type));
692 		break;
693 	case CISCO_ADDR_REPLY:
694 		/* Reply on address request, ignore */
695 		break;
696 	case CISCO_KEEPALIVE_REQ:
697 		sp->pp_alivecnt = 0;
698 		sp->pp_rseq = ntohl (h->par1);
699 		if (sp->pp_seq == sp->pp_rseq) {
700 			/* Local and remote sequence numbers are equal.
701 			 * Probably, the line is in loopback mode. */
702 			int newseq;
703 			if (sp->pp_loopcnt >= MAXALIVECNT) {
704 				printk (KERN_WARNING "%s: loopback\n",
705 					dev->name);
706 				sp->pp_loopcnt = 0;
707 				if (dev->flags & IFF_UP) {
708 					if_down (dev);
709 				}
710 			}
711 			++sp->pp_loopcnt;
712 
713 			/* Generate new local sequence number */
714 			get_random_bytes(&newseq, sizeof(newseq));
715 			sp->pp_seq ^= newseq;
716 			break;
717 		}
718 		sp->pp_loopcnt = 0;
719 		if (sp->pp_link_state==SPPP_LINK_DOWN &&
720 		    (dev->flags & IFF_UP)) {
721 			sp->pp_link_state=SPPP_LINK_UP;
722 			printk (KERN_INFO "%s: protocol up\n", dev->name);
723 		}
724 		break;
725 	case CISCO_ADDR_REQ:
726 		/* Stolen from net/ipv4/devinet.c -- SIOCGIFADDR ioctl */
727 		{
728 		struct in_device *in_dev;
729 		struct in_ifaddr *ifa;
730 		u32 addr = 0, mask = ~0; /* FIXME: is the mask correct? */
731 #ifdef CONFIG_INET
732 		if ((in_dev=in_dev_get(dev)) != NULL)
733 		{
734 			read_lock(&in_dev->lock);
735 			for (ifa=in_dev->ifa_list; ifa != NULL;
736 				ifa=ifa->ifa_next) {
737 				if (strcmp(dev->name, ifa->ifa_label) == 0)
738 				{
739 					addr = ifa->ifa_local;
740 					mask = ifa->ifa_mask;
741 					break;
742 				}
743 			}
744 			read_unlock(&in_dev->lock);
745 			in_dev_put(in_dev);
746 		}
747 #endif
748 		/* I hope both addr and mask are in the net order */
749 		sppp_cisco_send (sp, CISCO_ADDR_REPLY, addr, mask);
750 		break;
751 		}
752 	}
753 }
754 
755 /*
756  * Send PPP LCP packet.
757  */
758 
sppp_cp_send(struct sppp * sp,u16 proto,u8 type,u8 ident,u16 len,void * data)759 static void sppp_cp_send (struct sppp *sp, u16 proto, u8 type,
760 	u8 ident, u16 len, void *data)
761 {
762 	struct ppp_header *h;
763 	struct lcp_header *lh;
764 	struct sk_buff *skb;
765 	struct net_device *dev = sp->pp_if;
766 
767 	skb=alloc_skb(dev->hard_header_len+PPP_HEADER_LEN+LCP_HEADER_LEN+len,
768 		GFP_ATOMIC);
769 	if (skb==NULL)
770 		return;
771 
772 	skb_reserve(skb,dev->hard_header_len);
773 
774 	h = (struct ppp_header *)skb_put(skb, sizeof(struct ppp_header));
775 	h->address = PPP_ALLSTATIONS;        /* broadcast address */
776 	h->control = PPP_UI;                 /* Unnumbered Info */
777 	h->protocol = htons (proto);         /* Link Control Protocol */
778 
779 	lh = (struct lcp_header *)skb_put(skb, sizeof(struct lcp_header));
780 	lh->type = type;
781 	lh->ident = ident;
782 	lh->len = htons (LCP_HEADER_LEN + len);
783 
784 	if (len)
785 		memcpy(skb_put(skb,len),data, len);
786 
787 	if (sp->pp_flags & PP_DEBUG) {
788 		printk (KERN_WARNING "%s: %s output <%s id=%xh len=%xh",
789 			dev->name,
790 			proto==PPP_LCP ? "lcp" : "ipcp",
791 			proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
792 			sppp_ipcp_type_name (lh->type), lh->ident,
793 			ntohs (lh->len));
794 		if (len)
795 			sppp_print_bytes ((u8*) (lh+1), len);
796 		printk (">\n");
797 	}
798 	sp->obytes += skb->len;
799 	/* Control is high priority so it doesn't get queued behind data */
800 	skb->priority=TC_PRIO_CONTROL;
801 	skb->dev = dev;
802 	dev_queue_xmit(skb);
803 }
804 
805 /*
806  * Send Cisco keepalive packet.
807  */
808 
sppp_cisco_send(struct sppp * sp,int type,long par1,long par2)809 static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2)
810 {
811 	struct ppp_header *h;
812 	struct cisco_packet *ch;
813 	struct sk_buff *skb;
814 	struct net_device *dev = sp->pp_if;
815 	u32 t = jiffies * 1000/HZ;
816 
817 	skb=alloc_skb(dev->hard_header_len+PPP_HEADER_LEN+CISCO_PACKET_LEN,
818 		GFP_ATOMIC);
819 
820 	if(skb==NULL)
821 		return;
822 
823 	skb_reserve(skb, dev->hard_header_len);
824 	h = (struct ppp_header *)skb_put (skb, sizeof(struct ppp_header));
825 	h->address = CISCO_MULTICAST;
826 	h->control = 0;
827 	h->protocol = htons (CISCO_KEEPALIVE);
828 
829 	ch = (struct cisco_packet*)skb_put(skb, CISCO_PACKET_LEN);
830 	ch->type = htonl (type);
831 	ch->par1 = htonl (par1);
832 	ch->par2 = htonl (par2);
833 	ch->rel = -1;
834 	ch->time0 = htons ((u16) (t >> 16));
835 	ch->time1 = htons ((u16) t);
836 
837 	if (sp->pp_flags & PP_DEBUG)
838 		printk (KERN_WARNING "%s: cisco output: <%xh %xh %xh %xh %xh-%xh>\n",
839 			dev->name,  ntohl (ch->type), ch->par1,
840 			ch->par2, ch->rel, ch->time0, ch->time1);
841 	sp->obytes += skb->len;
842 	skb->priority=TC_PRIO_CONTROL;
843 	skb->dev = dev;
844 	dev_queue_xmit(skb);
845 }
846 
847 /**
848  *	sppp_close - close down a synchronous PPP or Cisco HDLC link
849  *	@dev: The network device to drop the link of
850  *
851  *	This drops the logical interface to the channel. It is not
852  *	done politely as we assume we will also be dropping DTR. Any
853  *	timeouts are killed.
854  */
855 
sppp_close(struct net_device * dev)856 int sppp_close (struct net_device *dev)
857 {
858 	struct sppp *sp = (struct sppp *)sppp_of(dev);
859 	sp->pp_link_state = SPPP_LINK_DOWN;
860 	sp->lcp.state = LCP_STATE_CLOSED;
861 	sp->ipcp.state = IPCP_STATE_CLOSED;
862 	sppp_clear_timeout (sp);
863 	return 0;
864 }
865 
866 EXPORT_SYMBOL(sppp_close);
867 
868 /**
869  *	sppp_open - open a synchronous PPP or Cisco HDLC link
870  *	@dev:	Network device to activate
871  *
872  *	Close down any existing synchronous session and commence
873  *	from scratch. In the PPP case this means negotiating LCP/IPCP
874  *	and friends, while for Cisco HDLC we simply need to start sending
875  *	keepalives
876  */
877 
sppp_open(struct net_device * dev)878 int sppp_open (struct net_device *dev)
879 {
880 	struct sppp *sp = (struct sppp *)sppp_of(dev);
881 	sppp_close(dev);
882 	if (!(sp->pp_flags & PP_CISCO)) {
883 		sppp_lcp_open (sp);
884 	}
885 	sp->pp_link_state = SPPP_LINK_DOWN;
886 	return 0;
887 }
888 
889 EXPORT_SYMBOL(sppp_open);
890 
891 /**
892  *	sppp_reopen - notify of physical link loss
893  *	@dev: Device that lost the link
894  *
895  *	This function informs the synchronous protocol code that
896  *	the underlying link died (for example a carrier drop on X.21)
897  *
898  *	We increment the magic numbers to ensure that if the other end
899  *	failed to notice we will correctly start a new session. It happens
900  *	do to the nature of telco circuits is that you can lose carrier on
901  *	one endonly.
902  *
903  *	Having done this we go back to negotiating. This function may
904  *	be called from an interrupt context.
905  */
906 
sppp_reopen(struct net_device * dev)907 int sppp_reopen (struct net_device *dev)
908 {
909 	struct sppp *sp = (struct sppp *)sppp_of(dev);
910 	sppp_close(dev);
911 	if (!(sp->pp_flags & PP_CISCO))
912 	{
913 		sp->lcp.magic = jiffies;
914 		++sp->pp_seq;
915 		sp->lcp.state = LCP_STATE_CLOSED;
916 		sp->ipcp.state = IPCP_STATE_CLOSED;
917 		/* Give it a moment for the line to settle then go */
918 		sppp_set_timeout (sp, 1);
919 	}
920 	sp->pp_link_state=SPPP_LINK_DOWN;
921 	return 0;
922 }
923 
924 EXPORT_SYMBOL(sppp_reopen);
925 
926 /**
927  *	sppp_change_mtu - Change the link MTU
928  *	@dev:	Device to change MTU on
929  *	@new_mtu: New MTU
930  *
931  *	Change the MTU on the link. This can only be called with
932  *	the link down. It returns an error if the link is up or
933  *	the mtu is out of range.
934  */
935 
sppp_change_mtu(struct net_device * dev,int new_mtu)936 int sppp_change_mtu(struct net_device *dev, int new_mtu)
937 {
938 	if(new_mtu<128||new_mtu>PPP_MTU||(dev->flags&IFF_UP))
939 		return -EINVAL;
940 	dev->mtu=new_mtu;
941 	return 0;
942 }
943 
944 EXPORT_SYMBOL(sppp_change_mtu);
945 
946 /**
947  *	sppp_do_ioctl - Ioctl handler for ppp/hdlc
948  *	@dev: Device subject to ioctl
949  *	@ifr: Interface request block from the user
950  *	@cmd: Command that is being issued
951  *
952  *	This function handles the ioctls that may be issued by the user
953  *	to control the settings of a PPP/HDLC link. It does both busy
954  *	and security checks. This function is intended to be wrapped by
955  *	callers who wish to add additional ioctl calls of their own.
956  */
957 
sppp_do_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)958 int sppp_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
959 {
960 	struct sppp *sp = (struct sppp *)sppp_of(dev);
961 
962 	if(dev->flags&IFF_UP)
963 		return -EBUSY;
964 
965 	if(!capable(CAP_NET_ADMIN))
966 		return -EPERM;
967 
968 	switch(cmd)
969 	{
970 		case SPPPIOCCISCO:
971 			sp->pp_flags|=PP_CISCO;
972 			dev->type = ARPHRD_HDLC;
973 			break;
974 		case SPPPIOCPPP:
975 			sp->pp_flags&=~PP_CISCO;
976 			dev->type = ARPHRD_PPP;
977 			break;
978 		case SPPPIOCDEBUG:
979 			sp->pp_flags&=~PP_DEBUG;
980 			if(ifr->ifr_flags)
981 				sp->pp_flags|=PP_DEBUG;
982 			break;
983 		case SPPPIOCGFLAGS:
984 			if(copy_to_user(ifr->ifr_data, &sp->pp_flags, sizeof(sp->pp_flags)))
985 				return -EFAULT;
986 			break;
987 		case SPPPIOCSFLAGS:
988 			if(copy_from_user(&sp->pp_flags, ifr->ifr_data, sizeof(sp->pp_flags)))
989 				return -EFAULT;
990 			break;
991 		default:
992 			return -EINVAL;
993 	}
994 	return 0;
995 }
996 
997 EXPORT_SYMBOL(sppp_do_ioctl);
998 
999 /**
1000  *	sppp_attach - attach synchronous PPP/HDLC to a device
1001  *	@pd:	PPP device to initialise
1002  *
1003  *	This initialises the PPP/HDLC support on an interface. At the
1004  *	time of calling the dev element must point to the network device
1005  *	that this interface is attached to. The interface should not yet
1006  *	be registered.
1007  */
1008 
sppp_attach(struct ppp_device * pd)1009 void sppp_attach(struct ppp_device *pd)
1010 {
1011 	struct net_device *dev = pd->dev;
1012 	struct sppp *sp = &pd->sppp;
1013 	unsigned long flags;
1014 
1015 	spin_lock_irqsave(&spppq_lock, flags);
1016 	/* Initialize keepalive handler. */
1017 	if (! spppq)
1018 	{
1019 		init_timer(&sppp_keepalive_timer);
1020 		sppp_keepalive_timer.expires=jiffies+10*HZ;
1021 		sppp_keepalive_timer.function=sppp_keepalive;
1022 		add_timer(&sppp_keepalive_timer);
1023 	}
1024 	/* Insert new entry into the keepalive list. */
1025 	sp->pp_next = spppq;
1026 	spppq = sp;
1027 	spin_unlock_irqrestore(&spppq_lock, flags);
1028 
1029 	sp->pp_loopcnt = 0;
1030 	sp->pp_alivecnt = 0;
1031 	sp->pp_seq = 0;
1032 	sp->pp_rseq = 0;
1033 	sp->pp_flags = PP_KEEPALIVE|PP_CISCO|debug;/*PP_DEBUG;*/
1034 	sp->lcp.magic = 0;
1035 	sp->lcp.state = LCP_STATE_CLOSED;
1036 	sp->ipcp.state = IPCP_STATE_CLOSED;
1037 	sp->pp_if = dev;
1038 
1039 	/*
1040 	 *	Device specific setup. All but interrupt handler and
1041 	 *	hard_start_xmit.
1042 	 */
1043 
1044 	dev->hard_header = sppp_hard_header;
1045 	dev->rebuild_header = sppp_rebuild_header;
1046 	dev->tx_queue_len = 10;
1047 	dev->type = ARPHRD_HDLC;
1048 	dev->addr_len = 0;
1049 	dev->hard_header_len = sizeof(struct ppp_header);
1050 	dev->mtu = PPP_MTU;
1051 	/*
1052 	 *	These 4 are callers but MUST also call sppp_ functions
1053 	 */
1054 	dev->do_ioctl = sppp_do_ioctl;
1055 #if 0
1056 	dev->get_stats = NULL;		/* Let the driver override these */
1057 	dev->open = sppp_open;
1058 	dev->stop = sppp_close;
1059 #endif
1060 	dev->change_mtu = sppp_change_mtu;
1061 	dev->hard_header_cache = NULL;
1062 	dev->header_cache_update = NULL;
1063 	dev->flags = IFF_MULTICAST|IFF_POINTOPOINT|IFF_NOARP;
1064 }
1065 
1066 EXPORT_SYMBOL(sppp_attach);
1067 
1068 /**
1069  *	sppp_detach - release PPP resources from a device
1070  *	@dev:	Network device to release
1071  *
1072  *	Stop and free up any PPP/HDLC resources used by this
1073  *	interface. This must be called before the device is
1074  *	freed.
1075  */
1076 
sppp_detach(struct net_device * dev)1077 void sppp_detach (struct net_device *dev)
1078 {
1079 	struct sppp **q, *p, *sp = (struct sppp *)sppp_of(dev);
1080 	unsigned long flags;
1081 
1082 	spin_lock_irqsave(&spppq_lock, flags);
1083 	/* Remove the entry from the keepalive list. */
1084 	for (q = &spppq; (p = *q); q = &p->pp_next)
1085 		if (p == sp) {
1086 			*q = p->pp_next;
1087 			break;
1088 		}
1089 
1090 	/* Stop keepalive handler. */
1091 	if (! spppq)
1092 		del_timer(&sppp_keepalive_timer);
1093 	sppp_clear_timeout (sp);
1094 	spin_unlock_irqrestore(&spppq_lock, flags);
1095 }
1096 
1097 EXPORT_SYMBOL(sppp_detach);
1098 
1099 /*
1100  * Analyze the LCP Configure-Request options list
1101  * for the presence of unknown options.
1102  * If the request contains unknown options, build and
1103  * send Configure-reject packet, containing only unknown options.
1104  */
1105 static int
sppp_lcp_conf_parse_options(struct sppp * sp,struct lcp_header * h,int len,u32 * magic)1106 sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
1107 	int len, u32 *magic)
1108 {
1109 	u8 *buf, *r, *p;
1110 	int rlen;
1111 
1112 	len -= 4;
1113 	buf = r = kmalloc (len, GFP_ATOMIC);
1114 	if (! buf)
1115 		return (0);
1116 
1117 	p = (void*) (h+1);
1118 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1119 		switch (*p) {
1120 		case LCP_OPT_MAGIC:
1121 			/* Magic number -- extract. */
1122 			if (len >= 6 && p[1] == 6) {
1123 				*magic = (u32)p[2] << 24 |
1124 					(u32)p[3] << 16 | p[4] << 8 | p[5];
1125 				continue;
1126 			}
1127 			break;
1128 		case LCP_OPT_ASYNC_MAP:
1129 			/* Async control character map -- check to be zero. */
1130 			if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
1131 			    ! p[4] && ! p[5])
1132 				continue;
1133 			break;
1134 		case LCP_OPT_MRU:
1135 			/* Maximum receive unit -- always OK. */
1136 			continue;
1137 		default:
1138 			/* Others not supported. */
1139 			break;
1140 		}
1141 		/* Add the option to rejected list. */
1142 		memcpy(r, p, p[1]);
1143 		r += p[1];
1144 		rlen += p[1];
1145 	}
1146 	if (rlen)
1147 		sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
1148 	kfree(buf);
1149 	return (rlen == 0);
1150 }
1151 
sppp_ipcp_input(struct sppp * sp,struct sk_buff * skb)1152 static void sppp_ipcp_input (struct sppp *sp, struct sk_buff *skb)
1153 {
1154 	struct lcp_header *h;
1155 	struct net_device *dev = sp->pp_if;
1156 	int len = skb->len;
1157 
1158 	if (len < 4)
1159 	{
1160 		if (sp->pp_flags & PP_DEBUG)
1161 			printk (KERN_WARNING "%s: invalid ipcp packet length: %d bytes\n",
1162 				dev->name,  len);
1163 		return;
1164 	}
1165 	h = (struct lcp_header *)skb->data;
1166 	skb_pull(skb,sizeof(struct lcp_header));
1167 	if (sp->pp_flags & PP_DEBUG) {
1168 		printk (KERN_WARNING "%s: ipcp input: %d bytes <%s id=%xh len=%xh",
1169 			dev->name,  len,
1170 			sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
1171 		if (len > 4)
1172 			sppp_print_bytes ((u8*) (h+1), len-4);
1173 		printk (">\n");
1174 	}
1175 	if (len > ntohs (h->len))
1176 		len = ntohs (h->len);
1177 	switch (h->type) {
1178 	default:
1179 		/* Unknown packet type -- send Code-Reject packet. */
1180 		sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
1181 		break;
1182 	case IPCP_CONF_REQ:
1183 		if (len < 4) {
1184 			if (sp->pp_flags & PP_DEBUG)
1185 				printk (KERN_WARNING "%s: invalid ipcp configure request packet length: %d bytes\n",
1186 					dev->name, len);
1187 			return;
1188 		}
1189 		if (len > 4) {
1190 			sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
1191 				len-4, h+1);
1192 
1193 			switch (sp->ipcp.state) {
1194 			case IPCP_STATE_OPENED:
1195 				/* Initiate renegotiation. */
1196 				sppp_ipcp_open (sp);
1197 				/* fall through... */
1198 			case IPCP_STATE_ACK_SENT:
1199 				/* Go to closed state. */
1200 				sp->ipcp.state = IPCP_STATE_CLOSED;
1201 			}
1202 		} else {
1203 			/* Send Configure-Ack packet. */
1204 			sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
1205 				0, 0);
1206 			/* Change the state. */
1207 			if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
1208 				sp->ipcp.state = IPCP_STATE_OPENED;
1209 			else
1210 				sp->ipcp.state = IPCP_STATE_ACK_SENT;
1211 		}
1212 		break;
1213 	case IPCP_CONF_ACK:
1214 		if (h->ident != sp->ipcp.confid)
1215 			break;
1216 		sppp_clear_timeout (sp);
1217 		switch (sp->ipcp.state) {
1218 		case IPCP_STATE_CLOSED:
1219 			sp->ipcp.state = IPCP_STATE_ACK_RCVD;
1220 			sppp_set_timeout (sp, 5);
1221 			break;
1222 		case IPCP_STATE_ACK_SENT:
1223 			sp->ipcp.state = IPCP_STATE_OPENED;
1224 			break;
1225 		}
1226 		break;
1227 	case IPCP_CONF_NAK:
1228 	case IPCP_CONF_REJ:
1229 		if (h->ident != sp->ipcp.confid)
1230 			break;
1231 		sppp_clear_timeout (sp);
1232 			/* Initiate renegotiation. */
1233 		sppp_ipcp_open (sp);
1234 		if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
1235 			/* Go to closed state. */
1236 			sp->ipcp.state = IPCP_STATE_CLOSED;
1237 		break;
1238 	case IPCP_TERM_REQ:
1239 		/* Send Terminate-Ack packet. */
1240 		sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
1241 		/* Go to closed state. */
1242 		sp->ipcp.state = IPCP_STATE_CLOSED;
1243 		/* Initiate renegotiation. */
1244 		sppp_ipcp_open (sp);
1245 		break;
1246 	case IPCP_TERM_ACK:
1247 		/* Ignore for now. */
1248 	case IPCP_CODE_REJ:
1249 		/* Ignore for now. */
1250 		break;
1251 	}
1252 }
1253 
sppp_lcp_open(struct sppp * sp)1254 static void sppp_lcp_open (struct sppp *sp)
1255 {
1256 	char opt[6];
1257 
1258 	if (! sp->lcp.magic)
1259 		sp->lcp.magic = jiffies;
1260 	opt[0] = LCP_OPT_MAGIC;
1261 	opt[1] = sizeof (opt);
1262 	opt[2] = sp->lcp.magic >> 24;
1263 	opt[3] = sp->lcp.magic >> 16;
1264 	opt[4] = sp->lcp.magic >> 8;
1265 	opt[5] = sp->lcp.magic;
1266 	sp->lcp.confid = ++sp->pp_seq;
1267 	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
1268 		sizeof (opt), &opt);
1269 	sppp_set_timeout (sp, 2);
1270 }
1271 
sppp_ipcp_open(struct sppp * sp)1272 static void sppp_ipcp_open (struct sppp *sp)
1273 {
1274 	sp->ipcp.confid = ++sp->pp_seq;
1275 	sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
1276 	sppp_set_timeout (sp, 2);
1277 }
1278 
1279 /*
1280  * Process PPP control protocol timeouts.
1281  */
1282 
sppp_cp_timeout(unsigned long arg)1283 static void sppp_cp_timeout (unsigned long arg)
1284 {
1285 	struct sppp *sp = (struct sppp*) arg;
1286 	unsigned long flags;
1287 	save_flags(flags);
1288 	cli();
1289 
1290 	sp->pp_flags &= ~PP_TIMO;
1291 	if (! (sp->pp_if->flags & IFF_UP) || (sp->pp_flags & PP_CISCO)) {
1292 		restore_flags(flags);
1293 		return;
1294 	}
1295 	switch (sp->lcp.state) {
1296 	case LCP_STATE_CLOSED:
1297 		/* No ACK for Configure-Request, retry. */
1298 		sppp_lcp_open (sp);
1299 		break;
1300 	case LCP_STATE_ACK_RCVD:
1301 		/* ACK got, but no Configure-Request for peer, retry. */
1302 		sppp_lcp_open (sp);
1303 		sp->lcp.state = LCP_STATE_CLOSED;
1304 		break;
1305 	case LCP_STATE_ACK_SENT:
1306 		/* ACK sent but no ACK for Configure-Request, retry. */
1307 		sppp_lcp_open (sp);
1308 		break;
1309 	case LCP_STATE_OPENED:
1310 		/* LCP is already OK, try IPCP. */
1311 		switch (sp->ipcp.state) {
1312 		case IPCP_STATE_CLOSED:
1313 			/* No ACK for Configure-Request, retry. */
1314 			sppp_ipcp_open (sp);
1315 			break;
1316 		case IPCP_STATE_ACK_RCVD:
1317 			/* ACK got, but no Configure-Request for peer, retry. */
1318 			sppp_ipcp_open (sp);
1319 			sp->ipcp.state = IPCP_STATE_CLOSED;
1320 			break;
1321 		case IPCP_STATE_ACK_SENT:
1322 			/* ACK sent but no ACK for Configure-Request, retry. */
1323 			sppp_ipcp_open (sp);
1324 			break;
1325 		case IPCP_STATE_OPENED:
1326 			/* IPCP is OK. */
1327 			break;
1328 		}
1329 		break;
1330 	}
1331 	restore_flags(flags);
1332 }
1333 
sppp_lcp_type_name(u8 type)1334 static char *sppp_lcp_type_name (u8 type)
1335 {
1336 	static char buf [8];
1337 	switch (type) {
1338 	case LCP_CONF_REQ:   return ("conf-req");
1339 	case LCP_CONF_ACK:   return ("conf-ack");
1340 	case LCP_CONF_NAK:   return ("conf-nack");
1341 	case LCP_CONF_REJ:   return ("conf-rej");
1342 	case LCP_TERM_REQ:   return ("term-req");
1343 	case LCP_TERM_ACK:   return ("term-ack");
1344 	case LCP_CODE_REJ:   return ("code-rej");
1345 	case LCP_PROTO_REJ:  return ("proto-rej");
1346 	case LCP_ECHO_REQ:   return ("echo-req");
1347 	case LCP_ECHO_REPLY: return ("echo-reply");
1348 	case LCP_DISC_REQ:   return ("discard-req");
1349 	}
1350 	sprintf (buf, "%xh", type);
1351 	return (buf);
1352 }
1353 
sppp_ipcp_type_name(u8 type)1354 static char *sppp_ipcp_type_name (u8 type)
1355 {
1356 	static char buf [8];
1357 	switch (type) {
1358 	case IPCP_CONF_REQ:   return ("conf-req");
1359 	case IPCP_CONF_ACK:   return ("conf-ack");
1360 	case IPCP_CONF_NAK:   return ("conf-nack");
1361 	case IPCP_CONF_REJ:   return ("conf-rej");
1362 	case IPCP_TERM_REQ:   return ("term-req");
1363 	case IPCP_TERM_ACK:   return ("term-ack");
1364 	case IPCP_CODE_REJ:   return ("code-rej");
1365 	}
1366 	sprintf (buf, "%xh", type);
1367 	return (buf);
1368 }
1369 
sppp_print_bytes(u_char * p,u16 len)1370 static void sppp_print_bytes (u_char *p, u16 len)
1371 {
1372 	printk (" %x", *p++);
1373 	while (--len > 0)
1374 		printk ("-%x", *p++);
1375 }
1376 
1377 /**
1378  *	sppp_rcv -	receive and process a WAN PPP frame
1379  *	@skb:	The buffer to process
1380  *	@dev:	The device it arrived on
1381  *	@p: Unused
1382  *
1383  *	Protocol glue. This drives the deferred processing mode the poorer
1384  *	cards use. This can be called directly by cards that do not have
1385  *	timing constraints but is normally called from the network layer
1386  *	after interrupt servicing to process frames queued via netif_rx.
1387  */
1388 
sppp_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * p)1389 static int sppp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *p)
1390 {
1391 	sppp_input(dev,skb);
1392 	return 0;
1393 }
1394 
1395 struct packet_type sppp_packet_type = {
1396 	type:	__constant_htons(ETH_P_WAN_PPP),
1397 	func:	sppp_rcv,
1398 };
1399 
1400 static char banner[] __initdata =
1401 	KERN_INFO "Cronyx Ltd, Synchronous PPP and CISCO HDLC (c) 1994\n"
1402 	KERN_INFO "Linux port (c) 1998 Building Number Three Ltd & "
1403 		  "Jan \"Yenya\" Kasprzak.\n";
1404 
sync_ppp_init(void)1405 static int __init sync_ppp_init(void)
1406 {
1407 	if(debug)
1408 		debug=PP_DEBUG;
1409 	printk(banner);
1410 	dev_add_pack(&sppp_packet_type);
1411 	return 0;
1412 }
1413 
1414 
sync_ppp_cleanup(void)1415 static void __exit sync_ppp_cleanup(void)
1416 {
1417 	dev_remove_pack(&sppp_packet_type);
1418 }
1419 
1420 module_init(sync_ppp_init);
1421 module_exit(sync_ppp_cleanup);
1422 MODULE_PARM(debug,"1i");
1423 MODULE_LICENSE("GPL");
1424 
1425