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
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 $
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>
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 */
107 #define PPP_HEADER_LEN sizeof (struct ppp_header)
114 #define LCP_HEADER_LEN sizeof (struct lcp_header)
116 struct cisco_packet
{
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
;
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;
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
);
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",
220 h
->address
, h
->control
, ntohs (h
->protocol
));
222 case PPP_ALLSTATIONS
:
223 if (h
->control
!= PPP_UI
)
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",
229 h
->address
, h
->control
, ntohs (h
->protocol
));
232 switch (ntohs (h
->protocol
)) {
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,
238 if (sp
->pp_flags
& PP_DEBUG
)
239 printk (KERN_WARNING
"%s: invalid input protocol <0x%x 0x%x 0x%x>\n",
241 h
->address
, h
->control
, ntohs (h
->protocol
));
244 sppp_lcp_input (sp
, skb
);
248 if (sp
->lcp
.state
== LCP_STATE_OPENED
)
249 sppp_ipcp_input (sp
, skb
);
251 printk(KERN_DEBUG
"IPCP when still waiting LCP finish.\n");
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
);
265 /* IPX IPXCP not implemented yet */
266 if (sp
->lcp
.state
== LCP_STATE_OPENED
) {
267 skb
->protocol
=htons(ETH_P_IPX
);
275 case CISCO_MULTICAST
:
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",
282 h
->address
, h
->control
, ntohs (h
->protocol
));
285 switch (ntohs (h
->protocol
)) {
288 case CISCO_KEEPALIVE
:
289 sppp_cisco_input (sp
, skb
);
294 skb
->protocol
=htons(ETH_P_IP
);
300 skb
->protocol
=htons(ETH_P_IPX
);
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
;
330 h
->address
= PPP_ALLSTATIONS
;
333 if(sp
->pp_flags
& PP_CISCO
)
335 h
->protocol
= htons(type
);
340 h
->protocol
= htons(PPP_IP
);
343 h
->protocol
= htons(PPP_IPX
);
346 return sizeof(struct ppp_header
);
349 static int sppp_rebuild_header(struct sk_buff
*skb
)
355 * Send keepalive packets, every 10 seconds.
358 static void sppp_keepalive (unsigned long dummy
)
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
))
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
)
379 if (sp
->pp_alivecnt
== MAXALIVECNT
) {
380 /* No keepalive packets got. Stop the interface. */
381 printk (KERN_WARNING
"%s: down\n", dev
->name
);
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. */
393 if (sp
->pp_alivecnt
<= MAXALIVECNT
)
395 if (sp
->pp_flags
& PP_CISCO
)
396 sppp_cisco_send (sp
, CISCO_KEEPALIVE_REQ
, ++sp
->pp_seq
,
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
;
423 if (sp
->pp_flags
& PP_DEBUG
)
424 printk (KERN_WARNING
"%s: invalid lcp packet length: %d bytes\n",
428 h
= (struct lcp_header
*)skb
->data
;
429 skb_pull(skb
,sizeof(struct lcp_header
*));
431 if (sp
->pp_flags
& PP_DEBUG
)
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
));
444 sppp_print_bytes ((u8
*) (h
+1), len
-4);
447 if (len
> ntohs (h
->len
))
448 len
= ntohs (h
->len
);
451 /* Unknown packet type -- send Code-Reject packet. */
452 sppp_cp_send (sp
, PPP_LCP
, LCP_CODE_REJ
, ++sp
->pp_seq
,
457 if (sp
->pp_flags
& PP_DEBUG
)
458 printk (KERN_DEBUG
"%s: invalid lcp configure request packet length: %d bytes\n",
462 if (len
>4 && !sppp_lcp_conf_parse_options (sp
, h
, len
, &rmagic
))
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",
470 if (dev
->flags
& IFF_UP
) {
473 } else if (sp
->pp_flags
& PP_DEBUG
)
474 printk (KERN_DEBUG
"%s: conf req: magic glitch\n",
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;
486 sppp_cp_send (sp
, PPP_LCP
, LCP_CONF_NAK
,
487 h
->ident
, sizeof (opt
), &opt
);
489 switch (sp
->lcp
.state
) {
490 case LCP_STATE_OPENED
:
491 /* Initiate renegotiation. */
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
;
501 /* Send Configure-Ack packet. */
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
;
510 case LCP_STATE_ACK_RCVD
:
511 sp
->lcp
.state
= LCP_STATE_OPENED
;
514 case LCP_STATE_OPENED
:
516 /* Remote magic changed -- close session. */
517 sp
->lcp
.state
= LCP_STATE_CLOSED
;
518 sp
->ipcp
.state
= IPCP_STATE_CLOSED
;
519 /* Initiate renegotiation. */
521 /* An ACK has already been sent. */
522 sp
->lcp
.state
= LCP_STATE_ACK_SENT
;
528 if (h
->ident
!= sp
->lcp
.confid
)
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);
542 case LCP_STATE_ACK_SENT
:
543 sp
->lcp
.state
= LCP_STATE_OPENED
;
549 if (h
->ident
!= sp
->lcp
.confid
)
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
) {
557 if (sp
->pp_flags
& PP_DEBUG
)
558 printk (KERN_DEBUG
"%s: conf nak: magic glitch\n",
560 get_random_bytes(&newmagic
, sizeof(newmagic
));
561 sp
->lcp
.magic
+= newmagic
;
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);
576 if (h
->ident
!= sp
->lcp
.confid
)
578 sppp_clear_timeout (sp
);
579 /* Initiate renegotiation. */
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
;
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. */
600 /* Ignore for now. */
603 /* Discard the packet. */
606 if (sp
->lcp
.state
!= LCP_STATE_OPENED
)
609 if (sp
->pp_flags
& PP_DEBUG
)
610 printk (KERN_WARNING
"%s: invalid lcp echo request packet length: %d bytes\n",
614 if (ntohl (*(long*)(h
+1)) == sp
->lcp
.magic
) {
615 /* Line loopback mode detected. */
616 printk (KERN_WARNING
"%s: loopback\n", dev
->name
);
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. */
627 *(long*)(h
+1) = htonl (sp
->lcp
.magic
);
628 sppp_cp_send (sp
, PPP_LCP
, LCP_ECHO_REPLY
, h
->ident
, len
-4, h
+1);
631 if (h
->ident
!= sp
->lcp
.echoid
)
634 if (sp
->pp_flags
& PP_DEBUG
)
635 printk (KERN_WARNING
"%s: invalid lcp echo reply packet length: %d bytes\n",
639 if (ntohl (*(long*)(h
+1)) != sp
->lcp
.magic
)
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
);
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",
665 ntohl (h
->type
), h
->par1
, h
->par2
, h
->rel
,
667 switch (ntohl (h
->type
)) {
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
));
673 case CISCO_ADDR_REPLY
:
674 /* Reply on address request, ignore */
676 case CISCO_KEEPALIVE_REQ
:
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. */
683 if (sp
->pp_loopcnt
>= MAXALIVECNT
) {
684 printk (KERN_WARNING
"%s: loopback\n",
687 if (dev
->flags
& IFF_UP
) {
693 /* Generate new local sequence number */
694 get_random_bytes(&newseq
, sizeof(newseq
));
695 sp
->pp_seq
^= newseq
;
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
);
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
;
717 if (strcmp(dev
->name
, ifa
->ifa_label
) == 0)
719 addr
= ifa
->ifa_local
;
720 mask
= ifa
->ifa_mask
;
724 read_unlock(&in_dev
->lock
);
727 /* I hope both addr and mask are in the net order */
728 sppp_cisco_send (sp
, CISCO_ADDR_REPLY
, addr
, mask
);
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
;
744 struct net_device
*dev
= sp
->pp_if
;
746 skb
=alloc_skb(dev
->hard_header_len
+PPP_HEADER_LEN
+LCP_HEADER_LEN
+len
,
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
));
761 lh
->len
= htons (LCP_HEADER_LEN
+ 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",
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
,
774 sppp_print_bytes ((u8
*) (lh
+1), len
);
777 sp
->obytes
+= skb
->len
;
778 /* Control is high priority so it doesnt get queued behind data */
779 skb
->priority
=TC_PRIO_CONTROL
;
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
;
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
,
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
;
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
);
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
;
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
);
837 EXPORT_SYMBOL(sppp_close
);
840 int sppp_open (struct net_device
*dev
)
842 struct sppp
*sp
= &((struct ppp_device
*)dev
)->sppp
;
844 dev
->flags
|= IFF_RUNNING
;
845 if (!(sp
->pp_flags
& PP_CISCO
))
850 EXPORT_SYMBOL(sppp_open
);
852 int sppp_reopen (struct net_device
*dev
)
854 struct sppp
*sp
= &((struct ppp_device
*)dev
)->sppp
;
856 dev
->flags
|= IFF_RUNNING
;
857 if (!(sp
->pp_flags
& PP_CISCO
))
859 sp
->lcp
.magic
= jiffies
;
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);
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
))
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
)
888 if(!capable(CAP_NET_ADMIN
))
894 sp
->pp_flags
|=PP_CISCO
;
895 dev
->type
= ARPHRD_HDLC
;
898 sp
->pp_flags
&=~PP_CISCO
;
899 dev
->type
= ARPHRD_PPP
;
902 sp
->pp_flags
&=~PP_DEBUG
;
904 sp
->pp_flags
|=PP_DEBUG
;
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. */
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. */
935 sp
->pp_flags
= PP_KEEPALIVE
|PP_CISCO
|debug
;/*PP_DEBUG;*/
937 sp
->lcp
.state
= LCP_STATE_CLOSED
;
938 sp
->ipcp
.state
= IPCP_STATE_CLOSED
;
942 * Device specific setup. All but interrupt handler and
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
;
951 dev
->hard_header_len
= sizeof(struct ppp_header
);
954 * These 4 are callers but MUST also call sppp_ functions
956 dev
->do_ioctl
= sppp_do_ioctl
;
958 dev
->get_stats
= NULL
; /* Let the driver override these */
959 dev
->open
= sppp_open
;
960 dev
->stop
= sppp_close
;
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
)
983 /* Stop keepalive handler. */
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.
998 sppp_lcp_conf_parse_options (struct sppp
*sp
, struct lcp_header
*h
,
1005 buf
= r
= kmalloc (len
, GFP_ATOMIC
);
1010 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
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];
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] &&
1027 /* Maximum receive unit -- always OK. */
1030 /* Others not supported. */
1033 /* Add the option to rejected list. */
1039 sppp_cp_send (sp
, PPP_LCP
, LCP_CONF_REJ
, h
->ident
, rlen
, buf
);
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
;
1052 if (sp
->pp_flags
& PP_DEBUG
)
1053 printk (KERN_WARNING
"%s: invalid ipcp packet length: %d bytes\n",
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",
1062 sppp_ipcp_type_name (h
->type
), h
->ident
, ntohs (h
->len
));
1064 sppp_print_bytes ((u8
*) (h
+1), len
-4);
1067 if (len
> ntohs (h
->len
))
1068 len
= ntohs (h
->len
);
1071 /* Unknown packet type -- send Code-Reject packet. */
1072 sppp_cp_send (sp
, PPP_IPCP
, IPCP_CODE_REJ
, ++sp
->pp_seq
, len
, h
);
1076 if (sp
->pp_flags
& PP_DEBUG
)
1077 printk (KERN_WARNING
"%s: invalid ipcp configure request packet length: %d bytes\n",
1082 sppp_cp_send (sp
, PPP_IPCP
, LCP_CONF_REJ
, h
->ident
,
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
;
1095 /* Send Configure-Ack packet. */
1096 sppp_cp_send (sp
, PPP_IPCP
, IPCP_CONF_ACK
, h
->ident
,
1098 /* Change the state. */
1099 if (sp
->ipcp
.state
== IPCP_STATE_ACK_RCVD
)
1100 sp
->ipcp
.state
= IPCP_STATE_OPENED
;
1102 sp
->ipcp
.state
= IPCP_STATE_ACK_SENT
;
1106 if (h
->ident
!= sp
->ipcp
.confid
)
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);
1114 case IPCP_STATE_ACK_SENT
:
1115 sp
->ipcp
.state
= IPCP_STATE_OPENED
;
1121 if (h
->ident
!= sp
->ipcp
.confid
)
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
;
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
);
1139 /* Ignore for now. */
1141 /* Ignore for now. */
1146 static void sppp_lcp_open (struct sppp
*sp
)
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
;
1182 sp
->pp_flags
&= ~PP_TIMO
;
1183 if (! (sp
->pp_if
->flags
& IFF_RUNNING
) || (sp
->pp_flags
& PP_CISCO
)) {
1184 restore_flags(flags
);
1187 switch (sp
->lcp
.state
) {
1188 case LCP_STATE_CLOSED
:
1189 /* No ACK for Configure-Request, retry. */
1192 case LCP_STATE_ACK_RCVD
:
1193 /* ACK got, but no Configure-Request for peer, retry. */
1195 sp
->lcp
.state
= LCP_STATE_CLOSED
;
1197 case LCP_STATE_ACK_SENT
:
1198 /* ACK sent but no ACK for Configure-Request, retry. */
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
);
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
;
1213 case IPCP_STATE_ACK_SENT
:
1214 /* ACK sent but no ACK for Configure-Request, retry. */
1215 sppp_ipcp_open (sp
);
1217 case IPCP_STATE_OPENED
:
1223 restore_flags(flags
);
1226 static char *sppp_lcp_type_name (u8 type
)
1228 static char buf
[8];
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
);
1246 static char *sppp_ipcp_type_name (u8 type
)
1248 static char buf
[8];
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
);
1262 static void sppp_print_bytes (u_char
*p
, u16 len
)
1264 printk (" %x", *p
++);
1266 printk ("-%x", *p
++);
1270 * Protocol glue. This drives the deferred processing mode the poorer
1274 int sppp_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*p
)
1276 sppp_input(dev
,skb
);
1280 EXPORT_SYMBOL(sppp_rcv
);
1282 struct packet_type sppp_packet_type
=
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
);
1302 int init_module(void)
1310 void cleanup_module(void)
1312 dev_remove_pack(&sppp_packet_type
);