Debian Codegraph @

drivers/dahdi/datamods/syncppp.c

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