Import 2.3.18pre1
[davej-history.git] / drivers / net / syncppp.c
blobeee3fceb86c5fc73e65130255c6f20fe4327adb8
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.
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.
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
17 * Port for Linux-2.1 by Jan "Yenya" Kasprzak <kas@fi.muni.cz>
21 * Synchronous PPP/Cisco link level subroutines.
22 * Keepalive protocol implemented in both Cisco and PPP modes.
24 * Copyright (C) 1994 Cronyx Ltd.
25 * Author: Serge Vakulenko, <vak@zebub.msk.su>
27 * This software is distributed with NO WARRANTIES, not even the implied
28 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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.
34 * Version 1.9, Wed Oct 4 18:58:15 MSK 1995
36 * $Id: if_spppsubr.c,v 1.12 1996/06/10 23:17:45 gpalmer Exp $
38 #undef DEBUG
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/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 <asm/byteorder.h>
53 #include "syncppp.h"
55 #define MAXALIVECNT 6 /* max. alive packets */
57 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
58 #define PPP_UI 0x03 /* Unnumbered Information */
59 #define PPP_IP 0x0021 /* Internet Protocol */
60 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
61 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
62 #define PPP_IPX 0x002b /* Novell IPX Protocol */
63 #define PPP_LCP 0xc021 /* Link Control Protocol */
64 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
66 #define LCP_CONF_REQ 1 /* PPP LCP configure request */
67 #define LCP_CONF_ACK 2 /* PPP LCP configure acknowledge */
68 #define LCP_CONF_NAK 3 /* PPP LCP configure negative ack */
69 #define LCP_CONF_REJ 4 /* PPP LCP configure reject */
70 #define LCP_TERM_REQ 5 /* PPP LCP terminate request */
71 #define LCP_TERM_ACK 6 /* PPP LCP terminate acknowledge */
72 #define LCP_CODE_REJ 7 /* PPP LCP code reject */
73 #define LCP_PROTO_REJ 8 /* PPP LCP protocol reject */
74 #define LCP_ECHO_REQ 9 /* PPP LCP echo request */
75 #define LCP_ECHO_REPLY 10 /* PPP LCP echo reply */
76 #define LCP_DISC_REQ 11 /* PPP LCP discard request */
78 #define LCP_OPT_MRU 1 /* maximum receive unit */
79 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
80 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
81 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
82 #define LCP_OPT_MAGIC 5 /* magic number */
83 #define LCP_OPT_RESERVED 6 /* reserved */
84 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
85 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
87 #define IPCP_CONF_REQ LCP_CONF_REQ /* PPP IPCP configure request */
88 #define IPCP_CONF_ACK LCP_CONF_ACK /* PPP IPCP configure acknowledge */
89 #define IPCP_CONF_NAK LCP_CONF_NAK /* PPP IPCP configure negative ack */
90 #define IPCP_CONF_REJ LCP_CONF_REJ /* PPP IPCP configure reject */
91 #define IPCP_TERM_REQ LCP_TERM_REQ /* PPP IPCP terminate request */
92 #define IPCP_TERM_ACK LCP_TERM_ACK /* PPP IPCP terminate acknowledge */
93 #define IPCP_CODE_REJ LCP_CODE_REJ /* PPP IPCP code reject */
95 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
96 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
97 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
98 #define CISCO_ADDR_REQ 0 /* Cisco address request */
99 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
100 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
102 struct ppp_header {
103 u8 address;
104 u8 control;
105 u16 protocol;
107 #define PPP_HEADER_LEN sizeof (struct ppp_header)
109 struct lcp_header {
110 u8 type;
111 u8 ident;
112 u16 len;
114 #define LCP_HEADER_LEN sizeof (struct lcp_header)
116 struct cisco_packet {
117 u32 type;
118 u32 par1;
119 u32 par2;
120 u16 rel;
121 u16 time0;
122 u16 time1;
124 #define CISCO_PACKET_LEN 18
125 #define CISCO_BIG_PACKET_LEN 20
127 static struct sppp *spppq;
128 static struct timer_list sppp_keepalive_timer;
130 static void sppp_keepalive (unsigned long dummy);
131 static void sppp_cp_send (struct sppp *sp, u16 proto, u8 type,
132 u8 ident, u16 len, void *data);
133 static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2);
134 static void sppp_lcp_input (struct sppp *sp, struct sk_buff *m);
135 static void sppp_cisco_input (struct sppp *sp, struct sk_buff *m);
136 static void sppp_ipcp_input (struct sppp *sp, struct sk_buff *m);
137 static void sppp_lcp_open (struct sppp *sp);
138 static void sppp_ipcp_open (struct sppp *sp);
139 static int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
140 int len, u32 *magic);
141 static void sppp_cp_timeout (unsigned long arg);
142 static char *sppp_lcp_type_name (u8 type);
143 static char *sppp_ipcp_type_name (u8 type);
144 static void sppp_print_bytes (u8 *p, u16 len);
146 static int debug = 0;
149 * Interface down stub
152 static void if_down(struct net_device *dev)
158 * Timeout routine activations.
161 static void sppp_set_timeout(struct sppp *p,int s)
163 if (! (p->pp_flags & PP_TIMO))
165 init_timer(&p->pp_timer);
166 p->pp_timer.function=sppp_cp_timeout;
167 p->pp_timer.expires=jiffies+s*HZ;
168 p->pp_timer.data=(unsigned long)p;
169 p->pp_flags |= PP_TIMO;
170 add_timer(&p->pp_timer);
174 static void sppp_clear_timeout(struct sppp *p)
176 if (p->pp_flags & PP_TIMO)
178 del_timer(&p->pp_timer);
179 p->pp_flags &= ~PP_TIMO;
184 * Process the received packet.
187 void sppp_input (struct net_device *dev, struct sk_buff *skb)
189 struct ppp_header *h;
190 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
192 skb->dev=dev;
193 skb->mac.raw=skb->data;
195 if (dev->flags & IFF_UP)
197 /* Count received bytes, add FCS and one flag */
198 sp->ibytes+= skb->len + 3;
199 sp->ipkts++;
202 if (skb->len <= PPP_HEADER_LEN) {
203 /* Too small packet, drop it. */
204 if (sp->pp_flags & PP_DEBUG)
205 printk (KERN_DEBUG "%s: input packet is too small, %d bytes\n",
206 dev->name, skb->len);
207 drop: kfree_skb(skb);
208 return;
211 /* Get PPP header. */
212 h = (struct ppp_header *)skb->data;
213 skb_pull(skb,sizeof(struct ppp_header));
215 switch (h->address) {
216 default: /* Invalid PPP packet. */
217 invalid: if (sp->pp_flags & PP_DEBUG)
218 printk (KERN_WARNING "%s: invalid input packet <0x%x 0x%x 0x%x>\n",
219 dev->name,
220 h->address, h->control, ntohs (h->protocol));
221 goto drop;
222 case PPP_ALLSTATIONS:
223 if (h->control != PPP_UI)
224 goto invalid;
225 if (sp->pp_flags & PP_CISCO) {
226 if (sp->pp_flags & PP_DEBUG)
227 printk (KERN_WARNING "%s: PPP packet in Cisco mode <0x%x 0x%x 0x%x>\n",
228 dev->name,
229 h->address, h->control, ntohs (h->protocol));
230 goto drop;
232 switch (ntohs (h->protocol)) {
233 default:
234 if (sp->lcp.state == LCP_STATE_OPENED)
235 sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ,
236 ++sp->pp_seq, skb->len + 2,
237 &h->protocol);
238 if (sp->pp_flags & PP_DEBUG)
239 printk (KERN_WARNING "%s: invalid input protocol <0x%x 0x%x 0x%x>\n",
240 dev->name,
241 h->address, h->control, ntohs (h->protocol));
242 goto drop;
243 case PPP_LCP:
244 sppp_lcp_input (sp, skb);
245 kfree_skb(skb);
246 return;
247 case PPP_IPCP:
248 if (sp->lcp.state == LCP_STATE_OPENED)
249 sppp_ipcp_input (sp, skb);
250 else
251 printk(KERN_DEBUG "IPCP when still waiting LCP finish.\n");
252 kfree_skb(skb);
253 return;
254 case PPP_IP:
255 if (sp->ipcp.state == IPCP_STATE_OPENED) {
256 if(sp->pp_flags&PP_DEBUG)
257 printk(KERN_DEBUG "Yow an IP frame.\n");
258 skb->protocol=htons(ETH_P_IP);
259 netif_rx(skb);
260 return;
262 break;
263 #ifdef IPX
264 case PPP_IPX:
265 /* IPX IPXCP not implemented yet */
266 if (sp->lcp.state == LCP_STATE_OPENED) {
267 skb->protocol=htons(ETH_P_IPX);
268 netif_rx(skb);
269 return;
271 break;
272 #endif
274 break;
275 case CISCO_MULTICAST:
276 case CISCO_UNICAST:
277 /* Don't check the control field here (RFC 1547). */
278 if (! (sp->pp_flags & PP_CISCO)) {
279 if (sp->pp_flags & PP_DEBUG)
280 printk (KERN_WARNING "%s: Cisco packet in PPP mode <0x%x 0x%x 0x%x>\n",
281 dev->name,
282 h->address, h->control, ntohs (h->protocol));
283 goto drop;
285 switch (ntohs (h->protocol)) {
286 default:
287 goto invalid;
288 case CISCO_KEEPALIVE:
289 sppp_cisco_input (sp, skb);
290 kfree_skb(skb);
291 return;
292 #ifdef CONFIG_INET
293 case ETH_P_IP:
294 skb->protocol=htons(ETH_P_IP);
295 netif_rx(skb);
296 return;
297 #endif
298 #ifdef CONFIG_IPX
299 case ETH_P_IPX:
300 skb->protocol=htons(ETH_P_IPX);
301 netif_rx(skb);
302 return;
303 #endif
305 break;
307 kfree_skb(skb);
310 EXPORT_SYMBOL(sppp_input);
313 * Handle transmit packets.
316 static int sppp_hard_header(struct sk_buff *skb, struct net_device *dev, __u16 type,
317 void *daddr, void *saddr, unsigned int len)
319 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
320 struct ppp_header *h;
321 skb_push(skb,sizeof(struct ppp_header));
322 h=(struct ppp_header *)skb->data;
323 if(sp->pp_flags&PP_CISCO)
325 h->address = CISCO_MULTICAST;
326 h->control = 0;
328 else
330 h->address = PPP_ALLSTATIONS;
331 h->control = PPP_UI;
333 if(sp->pp_flags & PP_CISCO)
335 h->protocol = htons(type);
337 else switch(type)
339 case ETH_P_IP:
340 h->protocol = htons(PPP_IP);
341 break;
342 case ETH_P_IPX:
343 h->protocol = htons(PPP_IPX);
344 break;
346 return sizeof(struct ppp_header);
349 static int sppp_rebuild_header(struct sk_buff *skb)
351 return 0;
355 * Send keepalive packets, every 10 seconds.
358 static void sppp_keepalive (unsigned long dummy)
360 struct sppp *sp;
361 unsigned long flags;
362 save_flags(flags);
363 cli();
365 for (sp=spppq; sp; sp=sp->pp_next)
367 struct net_device *dev = sp->pp_if;
369 /* Keepalive mode disabled or channel down? */
370 if (! (sp->pp_flags & PP_KEEPALIVE) ||
371 ! (dev->flags & IFF_RUNNING))
372 continue;
374 /* No keepalive in PPP mode if LCP not opened yet. */
375 if (! (sp->pp_flags & PP_CISCO) &&
376 sp->lcp.state != LCP_STATE_OPENED)
377 continue;
379 if (sp->pp_alivecnt == MAXALIVECNT) {
380 /* No keepalive packets got. Stop the interface. */
381 printk (KERN_WARNING "%s: down\n", dev->name);
382 if_down (dev);
383 if (! (sp->pp_flags & PP_CISCO)) {
384 /* Shut down the PPP link. */
385 sp->lcp.magic = jiffies;
386 sp->lcp.state = LCP_STATE_CLOSED;
387 sp->ipcp.state = IPCP_STATE_CLOSED;
388 sppp_clear_timeout (sp);
389 /* Initiate negotiation. */
390 sppp_lcp_open (sp);
393 if (sp->pp_alivecnt <= MAXALIVECNT)
394 ++sp->pp_alivecnt;
395 if (sp->pp_flags & PP_CISCO)
396 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
397 sp->pp_rseq);
398 else if (sp->lcp.state == LCP_STATE_OPENED) {
399 long nmagic = htonl (sp->lcp.magic);
400 sp->lcp.echoid = ++sp->pp_seq;
401 sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ,
402 sp->lcp.echoid, 4, &nmagic);
405 restore_flags(flags);
406 sppp_keepalive_timer.expires=jiffies+10*HZ;
407 add_timer(&sppp_keepalive_timer);
411 * Handle incoming PPP Link Control Protocol packets.
414 static void sppp_lcp_input (struct sppp *sp, struct sk_buff *skb)
416 struct lcp_header *h;
417 struct net_device *dev = sp->pp_if;
418 int len = skb->len;
419 u8 *p, opt[6];
420 u32 rmagic;
422 if (len < 4) {
423 if (sp->pp_flags & PP_DEBUG)
424 printk (KERN_WARNING "%s: invalid lcp packet length: %d bytes\n",
425 dev->name, len);
426 return;
428 h = (struct lcp_header *)skb->data;
429 skb_pull(skb,sizeof(struct lcp_header *));
431 if (sp->pp_flags & PP_DEBUG)
433 char state = '?';
434 switch (sp->lcp.state) {
435 case LCP_STATE_CLOSED: state = 'C'; break;
436 case LCP_STATE_ACK_RCVD: state = 'R'; break;
437 case LCP_STATE_ACK_SENT: state = 'S'; break;
438 case LCP_STATE_OPENED: state = 'O'; break;
440 printk (KERN_WARNING "%s: lcp input(%c): %d bytes <%s id=%xh len=%xh",
441 dev->name, state, len,
442 sppp_lcp_type_name (h->type), h->ident, ntohs (h->len));
443 if (len > 4)
444 sppp_print_bytes ((u8*) (h+1), len-4);
445 printk (">\n");
447 if (len > ntohs (h->len))
448 len = ntohs (h->len);
449 switch (h->type) {
450 default:
451 /* Unknown packet type -- send Code-Reject packet. */
452 sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq,
453 skb->len, h);
454 break;
455 case LCP_CONF_REQ:
456 if (len < 4) {
457 if (sp->pp_flags & PP_DEBUG)
458 printk (KERN_DEBUG"%s: invalid lcp configure request packet length: %d bytes\n",
459 dev->name, len);
460 break;
462 if (len>4 && !sppp_lcp_conf_parse_options (sp, h, len, &rmagic))
463 goto badreq;
464 if (rmagic == sp->lcp.magic) {
465 /* Local and remote magics equal -- loopback? */
466 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
467 printk (KERN_WARNING "%s: loopback\n",
468 dev->name);
469 sp->pp_loopcnt = 0;
470 if (dev->flags & IFF_UP) {
471 if_down (dev);
473 } else if (sp->pp_flags & PP_DEBUG)
474 printk (KERN_DEBUG "%s: conf req: magic glitch\n",
475 dev->name);
476 ++sp->pp_loopcnt;
478 /* MUST send Conf-Nack packet. */
479 rmagic = ~sp->lcp.magic;
480 opt[0] = LCP_OPT_MAGIC;
481 opt[1] = sizeof (opt);
482 opt[2] = rmagic >> 24;
483 opt[3] = rmagic >> 16;
484 opt[4] = rmagic >> 8;
485 opt[5] = rmagic;
486 sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK,
487 h->ident, sizeof (opt), &opt);
488 badreq:
489 switch (sp->lcp.state) {
490 case LCP_STATE_OPENED:
491 /* Initiate renegotiation. */
492 sppp_lcp_open (sp);
493 /* fall through... */
494 case LCP_STATE_ACK_SENT:
495 /* Go to closed state. */
496 sp->lcp.state = LCP_STATE_CLOSED;
497 sp->ipcp.state = IPCP_STATE_CLOSED;
499 break;
501 /* Send Configure-Ack packet. */
502 sp->pp_loopcnt = 0;
503 sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
504 h->ident, len-4, h+1);
505 /* Change the state. */
506 switch (sp->lcp.state) {
507 case LCP_STATE_CLOSED:
508 sp->lcp.state = LCP_STATE_ACK_SENT;
509 break;
510 case LCP_STATE_ACK_RCVD:
511 sp->lcp.state = LCP_STATE_OPENED;
512 sppp_ipcp_open (sp);
513 break;
514 case LCP_STATE_OPENED:
515 #if 0
516 /* Remote magic changed -- close session. */
517 sp->lcp.state = LCP_STATE_CLOSED;
518 sp->ipcp.state = IPCP_STATE_CLOSED;
519 /* Initiate renegotiation. */
520 sppp_lcp_open (sp);
521 /* An ACK has already been sent. */
522 sp->lcp.state = LCP_STATE_ACK_SENT;
523 #endif
524 break;
526 break;
527 case LCP_CONF_ACK:
528 if (h->ident != sp->lcp.confid)
529 break;
530 sppp_clear_timeout (sp);
531 if (! (dev->flags & IFF_UP) &&
532 (dev->flags & IFF_RUNNING)) {
533 /* Coming out of loopback mode. */
534 dev->flags |= IFF_UP;
535 printk (KERN_INFO "%s: up\n", dev->name);
537 switch (sp->lcp.state) {
538 case LCP_STATE_CLOSED:
539 sp->lcp.state = LCP_STATE_ACK_RCVD;
540 sppp_set_timeout (sp, 5);
541 break;
542 case LCP_STATE_ACK_SENT:
543 sp->lcp.state = LCP_STATE_OPENED;
544 sppp_ipcp_open (sp);
545 break;
547 break;
548 case LCP_CONF_NAK:
549 if (h->ident != sp->lcp.confid)
550 break;
551 p = (u8*) (h+1);
552 if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) {
553 rmagic = (u32)p[2] << 24 |
554 (u32)p[3] << 16 | p[4] << 8 | p[5];
555 if (rmagic == ~sp->lcp.magic) {
556 int newmagic;
557 if (sp->pp_flags & PP_DEBUG)
558 printk (KERN_DEBUG "%s: conf nak: magic glitch\n",
559 dev->name);
560 get_random_bytes(&newmagic, sizeof(newmagic));
561 sp->lcp.magic += newmagic;
562 } else
563 sp->lcp.magic = rmagic;
565 if (sp->lcp.state != LCP_STATE_ACK_SENT) {
566 /* Go to closed state. */
567 sp->lcp.state = LCP_STATE_CLOSED;
568 sp->ipcp.state = IPCP_STATE_CLOSED;
570 /* The link will be renegotiated after timeout,
571 * to avoid endless req-nack loop. */
572 sppp_clear_timeout (sp);
573 sppp_set_timeout (sp, 2);
574 break;
575 case LCP_CONF_REJ:
576 if (h->ident != sp->lcp.confid)
577 break;
578 sppp_clear_timeout (sp);
579 /* Initiate renegotiation. */
580 sppp_lcp_open (sp);
581 if (sp->lcp.state != LCP_STATE_ACK_SENT) {
582 /* Go to closed state. */
583 sp->lcp.state = LCP_STATE_CLOSED;
584 sp->ipcp.state = IPCP_STATE_CLOSED;
586 break;
587 case LCP_TERM_REQ:
588 sppp_clear_timeout (sp);
589 /* Send Terminate-Ack packet. */
590 sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0);
591 /* Go to closed state. */
592 sp->lcp.state = LCP_STATE_CLOSED;
593 sp->ipcp.state = IPCP_STATE_CLOSED;
594 /* Initiate renegotiation. */
595 sppp_lcp_open (sp);
596 break;
597 case LCP_TERM_ACK:
598 case LCP_CODE_REJ:
599 case LCP_PROTO_REJ:
600 /* Ignore for now. */
601 break;
602 case LCP_DISC_REQ:
603 /* Discard the packet. */
604 break;
605 case LCP_ECHO_REQ:
606 if (sp->lcp.state != LCP_STATE_OPENED)
607 break;
608 if (len < 8) {
609 if (sp->pp_flags & PP_DEBUG)
610 printk (KERN_WARNING "%s: invalid lcp echo request packet length: %d bytes\n",
611 dev->name, len);
612 break;
614 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
615 /* Line loopback mode detected. */
616 printk (KERN_WARNING "%s: loopback\n", dev->name);
617 if_down (dev);
619 /* Shut down the PPP link. */
620 sp->lcp.state = LCP_STATE_CLOSED;
621 sp->ipcp.state = IPCP_STATE_CLOSED;
622 sppp_clear_timeout (sp);
623 /* Initiate negotiation. */
624 sppp_lcp_open (sp);
625 break;
627 *(long*)(h+1) = htonl (sp->lcp.magic);
628 sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1);
629 break;
630 case LCP_ECHO_REPLY:
631 if (h->ident != sp->lcp.echoid)
632 break;
633 if (len < 8) {
634 if (sp->pp_flags & PP_DEBUG)
635 printk (KERN_WARNING "%s: invalid lcp echo reply packet length: %d bytes\n",
636 dev->name, len);
637 break;
639 if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
640 sp->pp_alivecnt = 0;
641 break;
646 * Handle incoming Cisco keepalive protocol packets.
649 static void sppp_cisco_input (struct sppp *sp, struct sk_buff *skb)
651 struct cisco_packet *h;
652 struct net_device *dev = sp->pp_if;
654 if (skb->len != CISCO_PACKET_LEN && skb->len != CISCO_BIG_PACKET_LEN) {
655 if (sp->pp_flags & PP_DEBUG)
656 printk (KERN_WARNING "%s: invalid cisco packet length: %d bytes\n",
657 dev->name, skb->len);
658 return;
660 h = (struct cisco_packet *)skb->data;
661 skb_pull(skb, sizeof(struct cisco_packet*));
662 if (sp->pp_flags & PP_DEBUG)
663 printk (KERN_WARNING "%s: cisco input: %d bytes <%lxh %xh %xh %xh %xh-%xh>\n",
664 dev->name, skb->len,
665 ntohl (h->type), h->par1, h->par2, h->rel,
666 h->time0, h->time1);
667 switch (ntohl (h->type)) {
668 default:
669 if (sp->pp_flags & PP_DEBUG)
670 printk (KERN_WARNING "%s: unknown cisco packet type: 0x%lx\n",
671 dev->name, ntohl (h->type));
672 break;
673 case CISCO_ADDR_REPLY:
674 /* Reply on address request, ignore */
675 break;
676 case CISCO_KEEPALIVE_REQ:
677 sp->pp_alivecnt = 0;
678 sp->pp_rseq = ntohl (h->par1);
679 if (sp->pp_seq == sp->pp_rseq) {
680 /* Local and remote sequence numbers are equal.
681 * Probably, the line is in loopback mode. */
682 int newseq;
683 if (sp->pp_loopcnt >= MAXALIVECNT) {
684 printk (KERN_WARNING "%s: loopback\n",
685 dev->name);
686 sp->pp_loopcnt = 0;
687 if (dev->flags & IFF_UP) {
688 if_down (dev);
691 ++sp->pp_loopcnt;
693 /* Generate new local sequence number */
694 get_random_bytes(&newseq, sizeof(newseq));
695 sp->pp_seq ^= newseq;
696 break;
698 sp->pp_loopcnt = 0;
699 if (! (dev->flags & IFF_UP) &&
700 (dev->flags & IFF_RUNNING)) {
701 dev->flags |= IFF_UP;
702 printk (KERN_INFO "%s: up\n", dev->name);
704 break;
705 case CISCO_ADDR_REQ:
706 /* Stolen from net/ipv4/devinet.c -- SIOCGIFADDR ioctl */
708 struct in_device *in_dev;
709 struct in_ifaddr *ifa;
710 u32 addr = 0, mask = ~0; /* FIXME: is the mask correct? */
712 if ((in_dev=in_dev_get(dev)) != NULL)
714 read_lock(&in_dev->lock);
715 for (ifa=in_dev->ifa_list; ifa != NULL;
716 ifa=ifa->ifa_next) {
717 if (strcmp(dev->name, ifa->ifa_label) == 0)
719 addr = ifa->ifa_local;
720 mask = ifa->ifa_mask;
721 break;
724 read_unlock(&in_dev->lock);
725 in_dev_put(in_dev);
727 /* I hope both addr and mask are in the net order */
728 sppp_cisco_send (sp, CISCO_ADDR_REPLY, addr, mask);
729 break;
735 * Send PPP LCP packet.
738 static void sppp_cp_send (struct sppp *sp, u16 proto, u8 type,
739 u8 ident, u16 len, void *data)
741 struct ppp_header *h;
742 struct lcp_header *lh;
743 struct sk_buff *skb;
744 struct net_device *dev = sp->pp_if;
746 skb=alloc_skb(dev->hard_header_len+PPP_HEADER_LEN+LCP_HEADER_LEN+len,
747 GFP_ATOMIC);
748 if (skb==NULL)
749 return;
751 skb_reserve(skb,dev->hard_header_len);
753 h = (struct ppp_header *)skb_put(skb, sizeof(struct ppp_header));
754 h->address = PPP_ALLSTATIONS; /* broadcast address */
755 h->control = PPP_UI; /* Unnumbered Info */
756 h->protocol = htons (proto); /* Link Control Protocol */
758 lh = (struct lcp_header *)skb_put(skb, sizeof(struct lcp_header));
759 lh->type = type;
760 lh->ident = ident;
761 lh->len = htons (LCP_HEADER_LEN + len);
763 if (len)
764 memcpy(skb_put(skb,len),data, len);
766 if (sp->pp_flags & PP_DEBUG) {
767 printk (KERN_WARNING "%s: %s output <%s id=%xh len=%xh",
768 dev->name,
769 proto==PPP_LCP ? "lcp" : "ipcp",
770 proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
771 sppp_ipcp_type_name (lh->type), lh->ident,
772 ntohs (lh->len));
773 if (len)
774 sppp_print_bytes ((u8*) (lh+1), len);
775 printk (">\n");
777 sp->obytes += skb->len;
778 /* Control is high priority so it doesnt get queued behind data */
779 skb->priority=TC_PRIO_CONTROL;
780 skb->dev = dev;
781 dev_queue_xmit(skb);
785 * Send Cisco keepalive packet.
788 static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2)
790 struct ppp_header *h;
791 struct cisco_packet *ch;
792 struct sk_buff *skb;
793 struct net_device *dev = sp->pp_if;
794 u32 t = jiffies * 1000/HZ;
796 skb=alloc_skb(dev->hard_header_len+PPP_HEADER_LEN+CISCO_PACKET_LEN,
797 GFP_ATOMIC);
799 if(skb==NULL)
800 return;
802 skb_reserve(skb, dev->hard_header_len);
803 h = (struct ppp_header *)skb_put (skb, sizeof(struct ppp_header));
804 h->address = CISCO_MULTICAST;
805 h->control = 0;
806 h->protocol = htons (CISCO_KEEPALIVE);
808 ch = (struct cisco_packet*)skb_put(skb, CISCO_PACKET_LEN);
809 ch->type = htonl (type);
810 ch->par1 = htonl (par1);
811 ch->par2 = htonl (par2);
812 ch->rel = -1;
813 ch->time0 = htons ((u16) (t >> 16));
814 ch->time1 = htons ((u16) t);
816 if (sp->pp_flags & PP_DEBUG)
817 printk (KERN_WARNING "%s: cisco output: <%lxh %xh %xh %xh %xh-%xh>\n",
818 dev->name, ntohl (ch->type), ch->par1,
819 ch->par2, ch->rel, ch->time0, ch->time1);
820 sp->obytes += skb->len;
821 skb->priority=TC_PRIO_CONTROL;
822 skb->dev = dev;
823 dev_queue_xmit(skb);
827 int sppp_close (struct net_device *dev)
829 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
830 dev->flags &= ~IFF_RUNNING;
831 sp->lcp.state = LCP_STATE_CLOSED;
832 sp->ipcp.state = IPCP_STATE_CLOSED;
833 sppp_clear_timeout (sp);
834 return 0;
837 EXPORT_SYMBOL(sppp_close);
840 int sppp_open (struct net_device *dev)
842 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
843 sppp_close(dev);
844 dev->flags |= IFF_RUNNING;
845 if (!(sp->pp_flags & PP_CISCO))
846 sppp_lcp_open (sp);
847 return 0;
850 EXPORT_SYMBOL(sppp_open);
852 int sppp_reopen (struct net_device *dev)
854 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
855 sppp_close(dev);
856 dev->flags |= IFF_RUNNING;
857 if (!(sp->pp_flags & PP_CISCO))
859 sp->lcp.magic = jiffies;
860 ++sp->pp_seq;
861 sp->lcp.state = LCP_STATE_CLOSED;
862 sp->ipcp.state = IPCP_STATE_CLOSED;
863 /* Give it a moment for the line to settle then go */
864 sppp_set_timeout (sp, 1);
866 return 0;
869 EXPORT_SYMBOL(sppp_reopen);
871 int sppp_change_mtu(struct net_device *dev, int new_mtu)
873 if(new_mtu<128||new_mtu>PPP_MTU||(dev->flags&IFF_UP))
874 return -EINVAL;
875 dev->mtu=new_mtu;
876 return 0;
879 EXPORT_SYMBOL(sppp_change_mtu);
881 int sppp_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
883 struct sppp *sp = &((struct ppp_device *)dev)->sppp;
885 if(dev->flags&IFF_UP)
886 return -EBUSY;
888 if(!capable(CAP_NET_ADMIN))
889 return -EPERM;
891 switch(cmd)
893 case SPPPIOCCISCO:
894 sp->pp_flags|=PP_CISCO;
895 dev->type = ARPHRD_HDLC;
896 break;
897 case SPPPIOCPPP:
898 sp->pp_flags&=~PP_CISCO;
899 dev->type = ARPHRD_PPP;
900 break;
901 case SPPPIOCDEBUG:
902 sp->pp_flags&=~PP_DEBUG;
903 if(ifr->ifr_flags)
904 sp->pp_flags|=PP_DEBUG;
905 break;
906 default:
907 return -EINVAL;
909 return 0;
912 EXPORT_SYMBOL(sppp_do_ioctl);
914 void sppp_attach(struct ppp_device *pd)
916 struct net_device *dev=&pd->dev;
917 struct sppp *sp = &pd->sppp;
919 /* Initialize keepalive handler. */
920 if (! spppq)
922 init_timer(&sppp_keepalive_timer);
923 sppp_keepalive_timer.expires=jiffies+10*HZ;
924 sppp_keepalive_timer.function=sppp_keepalive;
925 add_timer(&sppp_keepalive_timer);
927 /* Insert new entry into the keepalive list. */
928 sp->pp_next = spppq;
929 spppq = sp;
931 sp->pp_loopcnt = 0;
932 sp->pp_alivecnt = 0;
933 sp->pp_seq = 0;
934 sp->pp_rseq = 0;
935 sp->pp_flags = PP_KEEPALIVE|PP_CISCO|debug;/*PP_DEBUG;*/
936 sp->lcp.magic = 0;
937 sp->lcp.state = LCP_STATE_CLOSED;
938 sp->ipcp.state = IPCP_STATE_CLOSED;
939 sp->pp_if = dev;
942 * Device specific setup. All but interrupt handler and
943 * hard_start_xmit.
946 dev->hard_header = sppp_hard_header;
947 dev->rebuild_header = sppp_rebuild_header;
948 dev->tx_queue_len = 10;
949 dev->type = ARPHRD_HDLC;
950 dev->addr_len = 0;
951 dev->hard_header_len = sizeof(struct ppp_header);
952 dev->mtu = PPP_MTU;
954 * These 4 are callers but MUST also call sppp_ functions
956 dev->do_ioctl = sppp_do_ioctl;
957 #if 0
958 dev->get_stats = NULL; /* Let the driver override these */
959 dev->open = sppp_open;
960 dev->stop = sppp_close;
961 #endif
962 dev->change_mtu = sppp_change_mtu;
963 dev->hard_header_cache = NULL;
964 dev->header_cache_update = NULL;
965 dev->flags = IFF_MULTICAST|IFF_POINTOPOINT|IFF_NOARP;
966 dev_init_buffers(dev);
969 EXPORT_SYMBOL(sppp_attach);
971 void sppp_detach (struct net_device *dev)
973 struct sppp **q, *p, *sp = &((struct ppp_device *)dev)->sppp;
976 /* Remove the entry from the keepalive list. */
977 for (q = &spppq; (p = *q); q = &p->pp_next)
978 if (p == sp) {
979 *q = p->pp_next;
980 break;
983 /* Stop keepalive handler. */
984 if (! spppq)
985 del_timer(&sppp_keepalive_timer);
986 sppp_clear_timeout (sp);
989 EXPORT_SYMBOL(sppp_detach);
992 * Analyze the LCP Configure-Request options list
993 * for the presence of unknown options.
994 * If the request contains unknown options, build and
995 * send Configure-reject packet, containing only unknown options.
997 static int
998 sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
999 int len, u32 *magic)
1001 u8 *buf, *r, *p;
1002 int rlen;
1004 len -= 4;
1005 buf = r = kmalloc (len, GFP_ATOMIC);
1006 if (! buf)
1007 return (0);
1009 p = (void*) (h+1);
1010 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1011 switch (*p) {
1012 case LCP_OPT_MAGIC:
1013 /* Magic number -- extract. */
1014 if (len >= 6 && p[1] == 6) {
1015 *magic = (u32)p[2] << 24 |
1016 (u32)p[3] << 16 | p[4] << 8 | p[5];
1017 continue;
1019 break;
1020 case LCP_OPT_ASYNC_MAP:
1021 /* Async control character map -- check to be zero. */
1022 if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
1023 ! p[4] && ! p[5])
1024 continue;
1025 break;
1026 case LCP_OPT_MRU:
1027 /* Maximum receive unit -- always OK. */
1028 continue;
1029 default:
1030 /* Others not supported. */
1031 break;
1033 /* Add the option to rejected list. */
1034 memcpy(r, p, p[1]);
1035 r += p[1];
1036 rlen += p[1];
1038 if (rlen)
1039 sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
1040 kfree(buf);
1041 return (rlen == 0);
1044 static void sppp_ipcp_input (struct sppp *sp, struct sk_buff *skb)
1046 struct lcp_header *h;
1047 struct net_device *dev = sp->pp_if;
1048 int len = skb->len;
1050 if (len < 4)
1052 if (sp->pp_flags & PP_DEBUG)
1053 printk (KERN_WARNING "%s: invalid ipcp packet length: %d bytes\n",
1054 dev->name, len);
1055 return;
1057 h = (struct lcp_header *)skb->data;
1058 skb_pull(skb,sizeof(struct lcp_header));
1059 if (sp->pp_flags & PP_DEBUG) {
1060 printk (KERN_WARNING "%s: ipcp input: %d bytes <%s id=%xh len=%xh",
1061 dev->name, len,
1062 sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
1063 if (len > 4)
1064 sppp_print_bytes ((u8*) (h+1), len-4);
1065 printk (">\n");
1067 if (len > ntohs (h->len))
1068 len = ntohs (h->len);
1069 switch (h->type) {
1070 default:
1071 /* Unknown packet type -- send Code-Reject packet. */
1072 sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
1073 break;
1074 case IPCP_CONF_REQ:
1075 if (len < 4) {
1076 if (sp->pp_flags & PP_DEBUG)
1077 printk (KERN_WARNING "%s: invalid ipcp configure request packet length: %d bytes\n",
1078 dev->name, len);
1079 return;
1081 if (len > 4) {
1082 sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
1083 len-4, h+1);
1085 switch (sp->ipcp.state) {
1086 case IPCP_STATE_OPENED:
1087 /* Initiate renegotiation. */
1088 sppp_ipcp_open (sp);
1089 /* fall through... */
1090 case IPCP_STATE_ACK_SENT:
1091 /* Go to closed state. */
1092 sp->ipcp.state = IPCP_STATE_CLOSED;
1094 } else {
1095 /* Send Configure-Ack packet. */
1096 sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
1097 0, 0);
1098 /* Change the state. */
1099 if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
1100 sp->ipcp.state = IPCP_STATE_OPENED;
1101 else
1102 sp->ipcp.state = IPCP_STATE_ACK_SENT;
1104 break;
1105 case IPCP_CONF_ACK:
1106 if (h->ident != sp->ipcp.confid)
1107 break;
1108 sppp_clear_timeout (sp);
1109 switch (sp->ipcp.state) {
1110 case IPCP_STATE_CLOSED:
1111 sp->ipcp.state = IPCP_STATE_ACK_RCVD;
1112 sppp_set_timeout (sp, 5);
1113 break;
1114 case IPCP_STATE_ACK_SENT:
1115 sp->ipcp.state = IPCP_STATE_OPENED;
1116 break;
1118 break;
1119 case IPCP_CONF_NAK:
1120 case IPCP_CONF_REJ:
1121 if (h->ident != sp->ipcp.confid)
1122 break;
1123 sppp_clear_timeout (sp);
1124 /* Initiate renegotiation. */
1125 sppp_ipcp_open (sp);
1126 if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
1127 /* Go to closed state. */
1128 sp->ipcp.state = IPCP_STATE_CLOSED;
1129 break;
1130 case IPCP_TERM_REQ:
1131 /* Send Terminate-Ack packet. */
1132 sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
1133 /* Go to closed state. */
1134 sp->ipcp.state = IPCP_STATE_CLOSED;
1135 /* Initiate renegotiation. */
1136 sppp_ipcp_open (sp);
1137 break;
1138 case IPCP_TERM_ACK:
1139 /* Ignore for now. */
1140 case IPCP_CODE_REJ:
1141 /* Ignore for now. */
1142 break;
1146 static void sppp_lcp_open (struct sppp *sp)
1148 char opt[6];
1150 if (! sp->lcp.magic)
1151 sp->lcp.magic = jiffies;
1152 opt[0] = LCP_OPT_MAGIC;
1153 opt[1] = sizeof (opt);
1154 opt[2] = sp->lcp.magic >> 24;
1155 opt[3] = sp->lcp.magic >> 16;
1156 opt[4] = sp->lcp.magic >> 8;
1157 opt[5] = sp->lcp.magic;
1158 sp->lcp.confid = ++sp->pp_seq;
1159 sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
1160 sizeof (opt), &opt);
1161 sppp_set_timeout (sp, 2);
1164 static void sppp_ipcp_open (struct sppp *sp)
1166 sp->ipcp.confid = ++sp->pp_seq;
1167 sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
1168 sppp_set_timeout (sp, 2);
1172 * Process PPP control protocol timeouts.
1175 static void sppp_cp_timeout (unsigned long arg)
1177 struct sppp *sp = (struct sppp*) arg;
1178 unsigned long flags;
1179 save_flags(flags);
1180 cli();
1182 sp->pp_flags &= ~PP_TIMO;
1183 if (! (sp->pp_if->flags & IFF_RUNNING) || (sp->pp_flags & PP_CISCO)) {
1184 restore_flags(flags);
1185 return;
1187 switch (sp->lcp.state) {
1188 case LCP_STATE_CLOSED:
1189 /* No ACK for Configure-Request, retry. */
1190 sppp_lcp_open (sp);
1191 break;
1192 case LCP_STATE_ACK_RCVD:
1193 /* ACK got, but no Configure-Request for peer, retry. */
1194 sppp_lcp_open (sp);
1195 sp->lcp.state = LCP_STATE_CLOSED;
1196 break;
1197 case LCP_STATE_ACK_SENT:
1198 /* ACK sent but no ACK for Configure-Request, retry. */
1199 sppp_lcp_open (sp);
1200 break;
1201 case LCP_STATE_OPENED:
1202 /* LCP is already OK, try IPCP. */
1203 switch (sp->ipcp.state) {
1204 case IPCP_STATE_CLOSED:
1205 /* No ACK for Configure-Request, retry. */
1206 sppp_ipcp_open (sp);
1207 break;
1208 case IPCP_STATE_ACK_RCVD:
1209 /* ACK got, but no Configure-Request for peer, retry. */
1210 sppp_ipcp_open (sp);
1211 sp->ipcp.state = IPCP_STATE_CLOSED;
1212 break;
1213 case IPCP_STATE_ACK_SENT:
1214 /* ACK sent but no ACK for Configure-Request, retry. */
1215 sppp_ipcp_open (sp);
1216 break;
1217 case IPCP_STATE_OPENED:
1218 /* IPCP is OK. */
1219 break;
1221 break;
1223 restore_flags(flags);
1226 static char *sppp_lcp_type_name (u8 type)
1228 static char buf [8];
1229 switch (type) {
1230 case LCP_CONF_REQ: return ("conf-req");
1231 case LCP_CONF_ACK: return ("conf-ack");
1232 case LCP_CONF_NAK: return ("conf-nack");
1233 case LCP_CONF_REJ: return ("conf-rej");
1234 case LCP_TERM_REQ: return ("term-req");
1235 case LCP_TERM_ACK: return ("term-ack");
1236 case LCP_CODE_REJ: return ("code-rej");
1237 case LCP_PROTO_REJ: return ("proto-rej");
1238 case LCP_ECHO_REQ: return ("echo-req");
1239 case LCP_ECHO_REPLY: return ("echo-reply");
1240 case LCP_DISC_REQ: return ("discard-req");
1242 sprintf (buf, "%xh", type);
1243 return (buf);
1246 static char *sppp_ipcp_type_name (u8 type)
1248 static char buf [8];
1249 switch (type) {
1250 case IPCP_CONF_REQ: return ("conf-req");
1251 case IPCP_CONF_ACK: return ("conf-ack");
1252 case IPCP_CONF_NAK: return ("conf-nack");
1253 case IPCP_CONF_REJ: return ("conf-rej");
1254 case IPCP_TERM_REQ: return ("term-req");
1255 case IPCP_TERM_ACK: return ("term-ack");
1256 case IPCP_CODE_REJ: return ("code-rej");
1258 sprintf (buf, "%xh", type);
1259 return (buf);
1262 static void sppp_print_bytes (u_char *p, u16 len)
1264 printk (" %x", *p++);
1265 while (--len > 0)
1266 printk ("-%x", *p++);
1270 * Protocol glue. This drives the deferred processing mode the poorer
1271 * cards use.
1274 int sppp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *p)
1276 sppp_input(dev,skb);
1277 return 0;
1280 EXPORT_SYMBOL(sppp_rcv);
1282 struct packet_type sppp_packet_type=
1285 NULL,
1286 sppp_rcv,
1287 NULL,
1288 NULL
1292 void sync_ppp_init(void)
1294 printk(KERN_INFO "Cronyx Ltd, Synchronous PPP and CISCO HDLC (c) 1994\n");
1295 printk(KERN_INFO "Linux port (c) 1998 Building Number Three Ltd & Jan \"Yenya\" Kasprzak.\n");
1296 sppp_packet_type.type=htons(ETH_P_WAN_PPP);
1297 dev_add_pack(&sppp_packet_type);
1300 #ifdef MODULE
1302 int init_module(void)
1304 if(debug)
1305 debug=PP_DEBUG;
1306 sync_ppp_init();
1307 return 0;
1310 void cleanup_module(void)
1312 dev_remove_pack(&sppp_packet_type);
1315 #endif