Deleted Added
full compact
if_spppsubr.c (29681) if_spppsubr.c (30300)
1/*
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
1/*
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
5 * Copyright (C) 1994 Cronyx Ltd.
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 *
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 *
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * From: Version 1.9, Wed Oct 4 18:58:15 MSK 1995
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
19 *
20 * $Id: if_spppsubr.c,v 1.26 1997/09/02 01:18:37 bde Exp $
21 */
22
23#include <sys/param.h>
24#include <sys/systm.h>
25#include <sys/kernel.h>
26#include <sys/sockio.h>
27#include <sys/socket.h>
28#include <sys/syslog.h>
29#include <sys/malloc.h>
30#include <sys/mbuf.h>
19 *
20 * $Id: if_spppsubr.c,v 1.26 1997/09/02 01:18:37 bde Exp $
21 */
22
23#include <sys/param.h>
24#include <sys/systm.h>
25#include <sys/kernel.h>
26#include <sys/sockio.h>
27#include <sys/socket.h>
28#include <sys/syslog.h>
29#include <sys/malloc.h>
30#include <sys/mbuf.h>
31#include <sys/md5.h>
31
32#include <net/if.h>
33#include <net/netisr.h>
34#include <net/if_types.h>
35
32
33#include <net/if.h>
34#include <net/netisr.h>
35#include <net/if_types.h>
36
37#include <machine/stdarg.h>
38
36#ifdef INET
37#include <netinet/in.h>
38#include <netinet/in_systm.h>
39#include <netinet/in_var.h>
40#include <netinet/ip.h>
41#include <netinet/tcp.h>
42#include <netinet/if_ether.h>
43#endif
44
45#ifdef IPX
46#include <netipx/ipx.h>
47#include <netipx/ipx_if.h>
48#endif
49
50#ifdef NS
51#include <netns/ns.h>
52#include <netns/ns_if.h>
53#endif
54
55#ifdef ISO
56#include <netiso/argo_debug.h>
57#include <netiso/iso.h>
58#include <netiso/iso_var.h>
59#include <netiso/iso_snpac.h>
60#endif
61
62#include <net/if_sppp.h>
63
64#define MAXALIVECNT 3 /* max. alive packets */
65
66/*
67 * Interface flags that can be set in an ifconfig command.
68 *
69 * Setting link0 will make the link passive, i.e. it will be marked
70 * as being administrative openable, but won't be opened to begin
71 * with. Incoming calls will be answered, or subsequent calls with
72 * -link1 will cause the administrative open of the LCP layer.
73 *
74 * Setting link1 will cause the link to auto-dial only as packets
75 * arrive to be sent.
39#ifdef INET
40#include <netinet/in.h>
41#include <netinet/in_systm.h>
42#include <netinet/in_var.h>
43#include <netinet/ip.h>
44#include <netinet/tcp.h>
45#include <netinet/if_ether.h>
46#endif
47
48#ifdef IPX
49#include <netipx/ipx.h>
50#include <netipx/ipx_if.h>
51#endif
52
53#ifdef NS
54#include <netns/ns.h>
55#include <netns/ns_if.h>
56#endif
57
58#ifdef ISO
59#include <netiso/argo_debug.h>
60#include <netiso/iso.h>
61#include <netiso/iso_var.h>
62#include <netiso/iso_snpac.h>
63#endif
64
65#include <net/if_sppp.h>
66
67#define MAXALIVECNT 3 /* max. alive packets */
68
69/*
70 * Interface flags that can be set in an ifconfig command.
71 *
72 * Setting link0 will make the link passive, i.e. it will be marked
73 * as being administrative openable, but won't be opened to begin
74 * with. Incoming calls will be answered, or subsequent calls with
75 * -link1 will cause the administrative open of the LCP layer.
76 *
77 * Setting link1 will cause the link to auto-dial only as packets
78 * arrive to be sent.
79 *
80 * Setting IFF_DEBUG will syslog the option negotiation and state
81 * transitions at level kern.debug. Note: all logs consistently look
82 * like
83 *
84 * <if-name><unit>: <proto-name> <additional info...>
85 *
86 * with <if-name><unit> being something like "bppp0", and <proto-name>
87 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
76 */
77
78#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
79#define IFF_AUTO IFF_LINK1 /* auto-dial on output */
80
88 */
89
90#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
91#define IFF_AUTO IFF_LINK1 /* auto-dial on output */
92
81#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
82#define PPP_UI 0x03 /* Unnumbered Information */
83#define PPP_IP 0x0021 /* Internet Protocol */
84#define PPP_ISO 0x0023 /* ISO OSI Protocol */
85#define PPP_XNS 0x0025 /* Xerox NS Protocol */
86#define PPP_IPX 0x002b /* Novell IPX Protocol */
87#define PPP_LCP 0xc021 /* Link Control Protocol */
88#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
93#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
94#define PPP_UI 0x03 /* Unnumbered Information */
95#define PPP_IP 0x0021 /* Internet Protocol */
96#define PPP_ISO 0x0023 /* ISO OSI Protocol */
97#define PPP_XNS 0x0025 /* Xerox NS Protocol */
98#define PPP_IPX 0x002b /* Novell IPX Protocol */
99#define PPP_LCP 0xc021 /* Link Control Protocol */
100#define PPP_PAP 0xc023 /* Password Authentication Protocol */
101#define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
102#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
89
90#define CONF_REQ 1 /* PPP configure request */
91#define CONF_ACK 2 /* PPP configure acknowledge */
92#define CONF_NAK 3 /* PPP configure negative ack */
93#define CONF_REJ 4 /* PPP configure reject */
94#define TERM_REQ 5 /* PPP terminate request */
95#define TERM_ACK 6 /* PPP terminate acknowledge */
96#define CODE_REJ 7 /* PPP code reject */
97#define PROTO_REJ 8 /* PPP protocol reject */
98#define ECHO_REQ 9 /* PPP echo request */
99#define ECHO_REPLY 10 /* PPP echo reply */
100#define DISC_REQ 11 /* PPP discard request */
101
103
104#define CONF_REQ 1 /* PPP configure request */
105#define CONF_ACK 2 /* PPP configure acknowledge */
106#define CONF_NAK 3 /* PPP configure negative ack */
107#define CONF_REJ 4 /* PPP configure reject */
108#define TERM_REQ 5 /* PPP terminate request */
109#define TERM_ACK 6 /* PPP terminate acknowledge */
110#define CODE_REJ 7 /* PPP code reject */
111#define PROTO_REJ 8 /* PPP protocol reject */
112#define ECHO_REQ 9 /* PPP echo request */
113#define ECHO_REPLY 10 /* PPP echo reply */
114#define DISC_REQ 11 /* PPP discard request */
115
102#define LCP_OPT_MRU 1 /* maximum receive unit */
103#define LCP_OPT_ASYNC_MAP 2 /* async control character map */
104#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
105#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
106#define LCP_OPT_MAGIC 5 /* magic number */
107#define LCP_OPT_RESERVED 6 /* reserved */
108#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
109#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
116#define LCP_OPT_MRU 1 /* maximum receive unit */
117#define LCP_OPT_ASYNC_MAP 2 /* async control character map */
118#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
119#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
120#define LCP_OPT_MAGIC 5 /* magic number */
121#define LCP_OPT_RESERVED 6 /* reserved */
122#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
123#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
110
111#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
112#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
113#define IPCP_OPT_ADDRESS 3 /* local IP address */
114
124
125#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
126#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
127#define IPCP_OPT_ADDRESS 3 /* local IP address */
128
115#define CISCO_MULTICAST 0x8f /* Cisco multicast address */
116#define CISCO_UNICAST 0x0f /* Cisco unicast address */
117#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
118#define CISCO_ADDR_REQ 0 /* Cisco address request */
119#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
120#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
129#define PAP_REQ 1 /* PAP name/password request */
130#define PAP_ACK 2 /* PAP acknowledge */
131#define PAP_NAK 3 /* PAP fail */
121
132
133#define CHAP_CHALLENGE 1 /* CHAP challenge request */
134#define CHAP_RESPONSE 2 /* CHAP challenge response */
135#define CHAP_SUCCESS 3 /* CHAP response ok */
136#define CHAP_FAILURE 4 /* CHAP response failed */
137
138#define CHAP_MD5 5 /* hash algorithm - MD5 */
139
140#define CISCO_MULTICAST 0x8f /* Cisco multicast address */
141#define CISCO_UNICAST 0x0f /* Cisco unicast address */
142#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
143#define CISCO_ADDR_REQ 0 /* Cisco address request */
144#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
145#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
146
122/* states are named and numbered according to RFC 1661 */
123#define STATE_INITIAL 0
124#define STATE_STARTING 1
125#define STATE_CLOSED 2
126#define STATE_STOPPED 3
127#define STATE_CLOSING 4
128#define STATE_STOPPING 5
129#define STATE_REQ_SENT 6
130#define STATE_ACK_RCVD 7
131#define STATE_ACK_SENT 8
132#define STATE_OPENED 9
133
134struct ppp_header {
135 u_char address;
136 u_char control;
137 u_short protocol;
138};
139#define PPP_HEADER_LEN sizeof (struct ppp_header)
140
141struct lcp_header {
142 u_char type;
143 u_char ident;
144 u_short len;
145};
146#define LCP_HEADER_LEN sizeof (struct lcp_header)
147
148struct cisco_packet {
149 u_long type;
150 u_long par1;
151 u_long par2;
152 u_short rel;
153 u_short time0;
154 u_short time1;
155};
156#define CISCO_PACKET_LEN 18
157
158/*
159 * We follow the spelling and capitalization of RFC 1661 here, to make
160 * it easier comparing with the standard. Please refer to this RFC in
161 * case you can't make sense out of these abbreviation; it will also
162 * explain the semantics related to the various events and actions.
163 */
164struct cp {
165 u_short proto; /* PPP control protocol number */
166 u_char protoidx; /* index into state table in struct sppp */
167 u_char flags;
168#define CP_LCP 0x01 /* this is the LCP */
169#define CP_AUTH 0x02 /* this is an authentication protocol */
170#define CP_NCP 0x04 /* this is a NCP */
171#define CP_QUAL 0x08 /* this is a quality reporting protocol */
172 const char *name; /* name of this control protocol */
173 /* event handlers */
174 void (*Up)(struct sppp *sp);
175 void (*Down)(struct sppp *sp);
176 void (*Open)(struct sppp *sp);
177 void (*Close)(struct sppp *sp);
178 void (*TO)(void *sp);
179 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
180 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
181 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
182 /* actions */
183 void (*tlu)(struct sppp *sp);
184 void (*tld)(struct sppp *sp);
185 void (*tls)(struct sppp *sp);
186 void (*tlf)(struct sppp *sp);
187 void (*scr)(struct sppp *sp);
188};
189
190static struct sppp *spppq;
147/* states are named and numbered according to RFC 1661 */
148#define STATE_INITIAL 0
149#define STATE_STARTING 1
150#define STATE_CLOSED 2
151#define STATE_STOPPED 3
152#define STATE_CLOSING 4
153#define STATE_STOPPING 5
154#define STATE_REQ_SENT 6
155#define STATE_ACK_RCVD 7
156#define STATE_ACK_SENT 8
157#define STATE_OPENED 9
158
159struct ppp_header {
160 u_char address;
161 u_char control;
162 u_short protocol;
163};
164#define PPP_HEADER_LEN sizeof (struct ppp_header)
165
166struct lcp_header {
167 u_char type;
168 u_char ident;
169 u_short len;
170};
171#define LCP_HEADER_LEN sizeof (struct lcp_header)
172
173struct cisco_packet {
174 u_long type;
175 u_long par1;
176 u_long par2;
177 u_short rel;
178 u_short time0;
179 u_short time1;
180};
181#define CISCO_PACKET_LEN 18
182
183/*
184 * We follow the spelling and capitalization of RFC 1661 here, to make
185 * it easier comparing with the standard. Please refer to this RFC in
186 * case you can't make sense out of these abbreviation; it will also
187 * explain the semantics related to the various events and actions.
188 */
189struct cp {
190 u_short proto; /* PPP control protocol number */
191 u_char protoidx; /* index into state table in struct sppp */
192 u_char flags;
193#define CP_LCP 0x01 /* this is the LCP */
194#define CP_AUTH 0x02 /* this is an authentication protocol */
195#define CP_NCP 0x04 /* this is a NCP */
196#define CP_QUAL 0x08 /* this is a quality reporting protocol */
197 const char *name; /* name of this control protocol */
198 /* event handlers */
199 void (*Up)(struct sppp *sp);
200 void (*Down)(struct sppp *sp);
201 void (*Open)(struct sppp *sp);
202 void (*Close)(struct sppp *sp);
203 void (*TO)(void *sp);
204 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
205 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
206 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
207 /* actions */
208 void (*tlu)(struct sppp *sp);
209 void (*tld)(struct sppp *sp);
210 void (*tls)(struct sppp *sp);
211 void (*tlf)(struct sppp *sp);
212 void (*scr)(struct sppp *sp);
213};
214
215static struct sppp *spppq;
216static struct callout_handle keepalive_ch;
191
192/*
193 * The following disgusting hack gets around the problem that IP TOS
194 * can't be set yet. We want to put "interactive" traffic on a high
195 * priority queue. To decide if traffic is interactive, we check that
196 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
217
218/*
219 * The following disgusting hack gets around the problem that IP TOS
220 * can't be set yet. We want to put "interactive" traffic on a high
221 * priority queue. To decide if traffic is interactive, we check that
222 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
223 *
224 * XXX is this really still necessary? - joerg -
197 */
198static u_short interactive_ports[8] = {
199 0, 513, 0, 0,
200 0, 21, 0, 23,
201};
202#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
203
204/* almost every function needs these */
205#define STDDCL \
206 struct ifnet *ifp = &sp->pp_if; \
207 int debug = ifp->if_flags & IFF_DEBUG
208
225 */
226static u_short interactive_ports[8] = {
227 0, 513, 0, 0,
228 0, 21, 0, 23,
229};
230#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
231
232/* almost every function needs these */
233#define STDDCL \
234 struct ifnet *ifp = &sp->pp_if; \
235 int debug = ifp->if_flags & IFF_DEBUG
236
209static int sppp_output(struct ifnet *ifp, struct mbuf *m,
237static int sppp_output(struct ifnet *ifp, struct mbuf *m,
210 struct sockaddr *dst, struct rtentry *rt);
211
212static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
213static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
214
215static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
216 struct mbuf *m);
217static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
218 u_char ident, u_short len, void *data);
219static void sppp_cp_timeout(void *arg);
220static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
221 int newstate);
238 struct sockaddr *dst, struct rtentry *rt);
239
240static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
241static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
242
243static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
244 struct mbuf *m);
245static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
246 u_char ident, u_short len, void *data);
247static void sppp_cp_timeout(void *arg);
248static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
249 int newstate);
250static void sppp_auth_send(const struct cp *cp,
251 struct sppp *sp, u_char type, u_char id,
252 ...);
222
223static void sppp_up_event(const struct cp *cp, struct sppp *sp);
224static void sppp_down_event(const struct cp *cp, struct sppp *sp);
225static void sppp_open_event(const struct cp *cp, struct sppp *sp);
226static void sppp_close_event(const struct cp *cp, struct sppp *sp);
227static void sppp_to_event(const struct cp *cp, struct sppp *sp);
228
253
254static void sppp_up_event(const struct cp *cp, struct sppp *sp);
255static void sppp_down_event(const struct cp *cp, struct sppp *sp);
256static void sppp_open_event(const struct cp *cp, struct sppp *sp);
257static void sppp_close_event(const struct cp *cp, struct sppp *sp);
258static void sppp_to_event(const struct cp *cp, struct sppp *sp);
259
260static void sppp_null(struct sppp *sp);
261
229static void sppp_lcp_init(struct sppp *sp);
230static void sppp_lcp_up(struct sppp *sp);
231static void sppp_lcp_down(struct sppp *sp);
232static void sppp_lcp_open(struct sppp *sp);
233static void sppp_lcp_close(struct sppp *sp);
234static void sppp_lcp_TO(void *sp);
235static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
236static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
237static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
238static void sppp_lcp_tlu(struct sppp *sp);
239static void sppp_lcp_tld(struct sppp *sp);
240static void sppp_lcp_tls(struct sppp *sp);
241static void sppp_lcp_tlf(struct sppp *sp);
242static void sppp_lcp_scr(struct sppp *sp);
262static void sppp_lcp_init(struct sppp *sp);
263static void sppp_lcp_up(struct sppp *sp);
264static void sppp_lcp_down(struct sppp *sp);
265static void sppp_lcp_open(struct sppp *sp);
266static void sppp_lcp_close(struct sppp *sp);
267static void sppp_lcp_TO(void *sp);
268static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
269static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
270static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
271static void sppp_lcp_tlu(struct sppp *sp);
272static void sppp_lcp_tld(struct sppp *sp);
273static void sppp_lcp_tls(struct sppp *sp);
274static void sppp_lcp_tlf(struct sppp *sp);
275static void sppp_lcp_scr(struct sppp *sp);
243static void sppp_lcp_check(struct sppp *sp);
276static void sppp_lcp_check_and_close(struct sppp *sp);
277static int sppp_ncp_check(struct sppp *sp);
244
245static void sppp_ipcp_init(struct sppp *sp);
246static void sppp_ipcp_up(struct sppp *sp);
247static void sppp_ipcp_down(struct sppp *sp);
248static void sppp_ipcp_open(struct sppp *sp);
249static void sppp_ipcp_close(struct sppp *sp);
250static void sppp_ipcp_TO(void *sp);
251static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
252static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
253static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
254static void sppp_ipcp_tlu(struct sppp *sp);
255static void sppp_ipcp_tld(struct sppp *sp);
256static void sppp_ipcp_tls(struct sppp *sp);
257static void sppp_ipcp_tlf(struct sppp *sp);
258static void sppp_ipcp_scr(struct sppp *sp);
259
278
279static void sppp_ipcp_init(struct sppp *sp);
280static void sppp_ipcp_up(struct sppp *sp);
281static void sppp_ipcp_down(struct sppp *sp);
282static void sppp_ipcp_open(struct sppp *sp);
283static void sppp_ipcp_close(struct sppp *sp);
284static void sppp_ipcp_TO(void *sp);
285static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
286static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
287static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
288static void sppp_ipcp_tlu(struct sppp *sp);
289static void sppp_ipcp_tld(struct sppp *sp);
290static void sppp_ipcp_tls(struct sppp *sp);
291static void sppp_ipcp_tlf(struct sppp *sp);
292static void sppp_ipcp_scr(struct sppp *sp);
293
294static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
295static void sppp_pap_init(struct sppp *sp);
296static void sppp_pap_open(struct sppp *sp);
297static void sppp_pap_close(struct sppp *sp);
298static void sppp_pap_TO(void *sp);
299static void sppp_pap_my_TO(void *sp);
300static void sppp_pap_tlu(struct sppp *sp);
301static void sppp_pap_tld(struct sppp *sp);
302static void sppp_pap_scr(struct sppp *sp);
303
304static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
305static void sppp_chap_init(struct sppp *sp);
306static void sppp_chap_open(struct sppp *sp);
307static void sppp_chap_close(struct sppp *sp);
308static void sppp_chap_TO(void *sp);
309static void sppp_chap_tlu(struct sppp *sp);
310static void sppp_chap_tld(struct sppp *sp);
311static void sppp_chap_scr(struct sppp *sp);
312
313static const char *sppp_auth_type_name(u_short proto, u_char type);
260static const char *sppp_cp_type_name(u_char type);
314static const char *sppp_cp_type_name(u_char type);
261static const char *sppp_lcp_opt_name(u_char opt);
315static const char *sppp_dotted_quad(u_long addr);
262static const char *sppp_ipcp_opt_name(u_char opt);
316static const char *sppp_ipcp_opt_name(u_char opt);
263static const char *sppp_state_name(int state);
317static const char *sppp_lcp_opt_name(u_char opt);
264static const char *sppp_phase_name(enum ppp_phase phase);
265static const char *sppp_proto_name(u_short proto);
318static const char *sppp_phase_name(enum ppp_phase phase);
319static const char *sppp_proto_name(u_short proto);
266
320static const char *sppp_state_name(int state);
321static int sppp_params(struct sppp *sp, int cmd, void *data);
322static int sppp_strnlen(u_char *p, int max);
323static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
324 u_long *srcmask);
267static void sppp_keepalive(void *dummy);
325static void sppp_keepalive(void *dummy);
268static struct callout_handle keepalive_ch;
326static void sppp_phase_network(struct sppp *sp);
327static void sppp_print_bytes(const u_char *p, u_short len);
328static void sppp_print_string(const char *p, u_short len);
269static void sppp_qflush(struct ifqueue *ifq);
329static void sppp_qflush(struct ifqueue *ifq);
270
271static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst);
272static void sppp_set_ip_addr(struct sppp *sp, u_long src);
273
330static void sppp_set_ip_addr(struct sppp *sp, u_long src);
331
274static void sppp_print_bytes(u_char *p, u_short len);
275
276/* our control protocol descriptors */
277const struct cp lcp = {
278 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
279 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
280 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
281 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
282 sppp_lcp_scr
283};
284
285const struct cp ipcp = {
286 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
287 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
288 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
289 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
290 sppp_ipcp_scr
291};
292
332/* our control protocol descriptors */
333const struct cp lcp = {
334 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
335 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
336 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
337 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
338 sppp_lcp_scr
339};
340
341const struct cp ipcp = {
342 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
343 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
344 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
345 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
346 sppp_ipcp_scr
347};
348
349const struct cp pap = {
350 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
351 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
352 sppp_pap_TO, 0, 0, 0,
353 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
354 sppp_pap_scr
355};
356
357const struct cp chap = {
358 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
359 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
360 sppp_chap_TO, 0, 0, 0,
361 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
362 sppp_chap_scr
363};
364
293const struct cp *cps[IDX_COUNT] = {
294 &lcp, /* IDX_LCP */
295 &ipcp, /* IDX_IPCP */
365const struct cp *cps[IDX_COUNT] = {
366 &lcp, /* IDX_LCP */
367 &ipcp, /* IDX_IPCP */
368 &pap, /* IDX_PAP */
369 &chap, /* IDX_CHAP */
296};
297
298
299 /*
300 * Exported functions, comprising our interface to the lower layer.
301 */
302
303/*
304 * Process the received packet.
305 */
306void
307sppp_input(struct ifnet *ifp, struct mbuf *m)
308{
309 struct ppp_header *h;
310 struct ifqueue *inq = 0;
311 int s;
312 struct sppp *sp = (struct sppp *)ifp;
313 int debug = ifp->if_flags & IFF_DEBUG;
314
315 if (ifp->if_flags & IFF_UP)
316 /* Count received bytes, add FCS and one flag */
317 ifp->if_ibytes += m->m_pkthdr.len + 3;
318
319 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
320 /* Too small packet, drop it. */
321 if (debug)
322 log(LOG_DEBUG,
323 "%s%d: input packet is too small, %d bytes\n",
324 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
325 drop:
326 ++ifp->if_ierrors;
327 ++ifp->if_iqdrops;
328 m_freem (m);
329 return;
330 }
331
332 /* Get PPP header. */
333 h = mtod (m, struct ppp_header*);
334 m_adj (m, PPP_HEADER_LEN);
335
336 switch (h->address) {
337 case PPP_ALLSTATIONS:
338 if (h->control != PPP_UI)
339 goto invalid;
340 if (sp->pp_flags & PP_CISCO) {
341 if (debug)
342 log(LOG_DEBUG,
343 "%s%d: PPP packet in Cisco mode "
344 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
345 ifp->if_name, ifp->if_unit,
346 h->address, h->control, ntohs(h->protocol));
347 goto drop;
348 }
349 switch (ntohs (h->protocol)) {
350 default:
351 if (sp->state[IDX_LCP] == STATE_OPENED)
352 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
353 ++sp->pp_seq, m->m_pkthdr.len + 2,
354 &h->protocol);
355 if (debug)
356 log(LOG_DEBUG,
357 "%s%d: invalid input protocol "
358 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
359 ifp->if_name, ifp->if_unit,
360 h->address, h->control, ntohs(h->protocol));
361 ++ifp->if_noproto;
362 goto drop;
363 case PPP_LCP:
370};
371
372
373 /*
374 * Exported functions, comprising our interface to the lower layer.
375 */
376
377/*
378 * Process the received packet.
379 */
380void
381sppp_input(struct ifnet *ifp, struct mbuf *m)
382{
383 struct ppp_header *h;
384 struct ifqueue *inq = 0;
385 int s;
386 struct sppp *sp = (struct sppp *)ifp;
387 int debug = ifp->if_flags & IFF_DEBUG;
388
389 if (ifp->if_flags & IFF_UP)
390 /* Count received bytes, add FCS and one flag */
391 ifp->if_ibytes += m->m_pkthdr.len + 3;
392
393 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
394 /* Too small packet, drop it. */
395 if (debug)
396 log(LOG_DEBUG,
397 "%s%d: input packet is too small, %d bytes\n",
398 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
399 drop:
400 ++ifp->if_ierrors;
401 ++ifp->if_iqdrops;
402 m_freem (m);
403 return;
404 }
405
406 /* Get PPP header. */
407 h = mtod (m, struct ppp_header*);
408 m_adj (m, PPP_HEADER_LEN);
409
410 switch (h->address) {
411 case PPP_ALLSTATIONS:
412 if (h->control != PPP_UI)
413 goto invalid;
414 if (sp->pp_flags & PP_CISCO) {
415 if (debug)
416 log(LOG_DEBUG,
417 "%s%d: PPP packet in Cisco mode "
418 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
419 ifp->if_name, ifp->if_unit,
420 h->address, h->control, ntohs(h->protocol));
421 goto drop;
422 }
423 switch (ntohs (h->protocol)) {
424 default:
425 if (sp->state[IDX_LCP] == STATE_OPENED)
426 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
427 ++sp->pp_seq, m->m_pkthdr.len + 2,
428 &h->protocol);
429 if (debug)
430 log(LOG_DEBUG,
431 "%s%d: invalid input protocol "
432 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
433 ifp->if_name, ifp->if_unit,
434 h->address, h->control, ntohs(h->protocol));
435 ++ifp->if_noproto;
436 goto drop;
437 case PPP_LCP:
364 sppp_cp_input(&lcp, (struct sppp*)ifp, m);
438 sppp_cp_input(&lcp, sp, m);
365 m_freem (m);
366 return;
439 m_freem (m);
440 return;
441 case PPP_PAP:
442 if (sp->pp_phase >= PHASE_AUTHENTICATE)
443 sppp_pap_input(sp, m);
444 m_freem (m);
445 return;
446 case PPP_CHAP:
447 if (sp->pp_phase >= PHASE_AUTHENTICATE)
448 sppp_chap_input(sp, m);
449 m_freem (m);
450 return;
367#ifdef INET
368 case PPP_IPCP:
369 if (sp->pp_phase == PHASE_NETWORK)
451#ifdef INET
452 case PPP_IPCP:
453 if (sp->pp_phase == PHASE_NETWORK)
370 sppp_cp_input(&ipcp, (struct sppp*) ifp, m);
454 sppp_cp_input(&ipcp, sp, m);
371 m_freem (m);
372 return;
373 case PPP_IP:
374 if (sp->state[IDX_IPCP] == STATE_OPENED) {
375 schednetisr (NETISR_IP);
376 inq = &ipintrq;
377 }
378 break;
379#endif
380#ifdef IPX
381 case PPP_IPX:
382 /* IPX IPXCP not implemented yet */
383 if (sp->pp_phase == PHASE_NETWORK) {
384 schednetisr (NETISR_IPX);
385 inq = &ipxintrq;
386 }
387 break;
388#endif
389#ifdef NS
390 case PPP_XNS:
391 /* XNS IDPCP not implemented yet */
392 if (sp->pp_phase == PHASE_NETWORK) {
393 schednetisr (NETISR_NS);
394 inq = &nsintrq;
395 }
396 break;
397#endif
398#ifdef ISO
399 case PPP_ISO:
400 /* OSI NLCP not implemented yet */
401 if (sp->pp_phase == PHASE_NETWORK) {
402 schednetisr (NETISR_ISO);
403 inq = &clnlintrq;
404 }
405 break;
406#endif
407 }
408 break;
409 case CISCO_MULTICAST:
410 case CISCO_UNICAST:
411 /* Don't check the control field here (RFC 1547). */
412 if (! (sp->pp_flags & PP_CISCO)) {
413 if (debug)
414 log(LOG_DEBUG,
415 "%s%d: Cisco packet in PPP mode "
416 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
417 ifp->if_name, ifp->if_unit,
418 h->address, h->control, ntohs(h->protocol));
419 goto drop;
420 }
421 switch (ntohs (h->protocol)) {
422 default:
423 ++ifp->if_noproto;
424 goto invalid;
425 case CISCO_KEEPALIVE:
426 sppp_cisco_input ((struct sppp*) ifp, m);
427 m_freem (m);
428 return;
429#ifdef INET
430 case ETHERTYPE_IP:
431 schednetisr (NETISR_IP);
432 inq = &ipintrq;
433 break;
434#endif
435#ifdef IPX
436 case ETHERTYPE_IPX:
437 schednetisr (NETISR_IPX);
438 inq = &ipxintrq;
439 break;
440#endif
441#ifdef NS
442 case ETHERTYPE_NS:
443 schednetisr (NETISR_NS);
444 inq = &nsintrq;
445 break;
446#endif
447 }
448 break;
449 default: /* Invalid PPP packet. */
450 invalid:
451 if (debug)
452 log(LOG_DEBUG,
453 "%s%d: invalid input packet "
454 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
455 ifp->if_name, ifp->if_unit,
456 h->address, h->control, ntohs(h->protocol));
457 goto drop;
458 }
459
460 if (! (ifp->if_flags & IFF_UP) || ! inq)
461 goto drop;
462
463 /* Check queue. */
464 s = splimp();
465 if (IF_QFULL (inq)) {
466 /* Queue overflow. */
467 IF_DROP(inq);
468 splx(s);
469 if (debug)
470 log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
471 ifp->if_name, ifp->if_unit);
472 goto drop;
473 }
474 IF_ENQUEUE(inq, m);
475 splx(s);
476}
477
478/*
479 * Enqueue transmit packet.
480 */
481static int
482sppp_output(struct ifnet *ifp, struct mbuf *m,
483 struct sockaddr *dst, struct rtentry *rt)
484{
485 struct sppp *sp = (struct sppp*) ifp;
486 struct ppp_header *h;
487 struct ifqueue *ifq;
488 int s, rv = 0;
489
490 s = splimp();
491
492 if ((ifp->if_flags & IFF_UP) == 0 ||
493 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
494 m_freem (m);
495 splx (s);
496 return (ENETDOWN);
497 }
498
499 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
500 /*
501 * Interface is not yet running, but auto-dial. Need
502 * to start LCP for it.
503 */
504 ifp->if_flags |= IFF_RUNNING;
505 splx(s);
506 lcp.Open(sp);
507 s = splimp();
508 }
509
510 ifq = &ifp->if_snd;
511#ifdef INET
512 /*
513 * Put low delay, telnet, rlogin and ftp control packets
514 * in front of the queue.
515 */
516 if (dst->sa_family == AF_INET) {
517 struct ip *ip = mtod (m, struct ip*);
518 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
519
520 if (! IF_QFULL (&sp->pp_fastq) &&
521 ((ip->ip_tos & IPTOS_LOWDELAY) ||
522 ip->ip_p == IPPROTO_TCP &&
523 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
524 (INTERACTIVE (ntohs (tcp->th_sport)) ||
525 INTERACTIVE (ntohs (tcp->th_dport)))))
526 ifq = &sp->pp_fastq;
527 }
528#endif
529
530 /*
531 * Prepend general data packet PPP header. For now, IP only.
532 */
533 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
534 if (! m) {
535 if (ifp->if_flags & IFF_DEBUG)
536 log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
537 ifp->if_name, ifp->if_unit);
538 ++ifp->if_oerrors;
539 splx (s);
540 return (ENOBUFS);
541 }
542 h = mtod (m, struct ppp_header*);
543 if (sp->pp_flags & PP_CISCO) {
544 h->address = CISCO_UNICAST; /* unicast address */
545 h->control = 0;
546 } else {
547 h->address = PPP_ALLSTATIONS; /* broadcast address */
548 h->control = PPP_UI; /* Unnumbered Info */
549 }
550
551 switch (dst->sa_family) {
552#ifdef INET
553 case AF_INET: /* Internet Protocol */
554 if (sp->pp_flags & PP_CISCO)
555 h->protocol = htons (ETHERTYPE_IP);
556 else {
557 /*
558 * Don't choke with an ENETDOWN early. It's
559 * possible that we just started dialing out,
560 * so don't drop the packet immediately. If
561 * we notice that we run out of buffer space
562 * below, we will however remember that we are
563 * not ready to carry IP packets, and return
564 * ENETDOWN, as opposed to ENOBUFS.
565 */
566 h->protocol = htons(PPP_IP);
567 if (sp->state[IDX_IPCP] != STATE_OPENED)
568 rv = ENETDOWN;
569 }
570 break;
571#endif
572#ifdef NS
573 case AF_NS: /* Xerox NS Protocol */
574 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
575 ETHERTYPE_NS : PPP_XNS);
576 break;
577#endif
578#ifdef IPX
579 case AF_IPX: /* Novell IPX Protocol */
580 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
581 ETHERTYPE_IPX : PPP_IPX);
582 break;
583#endif
584#ifdef ISO
585 case AF_ISO: /* ISO OSI Protocol */
586 if (sp->pp_flags & PP_CISCO)
587 goto nosupport;
588 h->protocol = htons (PPP_ISO);
589 break;
590nosupport:
591#endif
592 default:
593 m_freem (m);
594 ++ifp->if_oerrors;
595 splx (s);
596 return (EAFNOSUPPORT);
597 }
598
599 /*
600 * Queue message on interface, and start output if interface
601 * not yet active.
602 */
603 if (IF_QFULL (ifq)) {
604 IF_DROP (&ifp->if_snd);
605 m_freem (m);
606 ++ifp->if_oerrors;
607 splx (s);
608 return (rv? rv: ENOBUFS);
609 }
610 IF_ENQUEUE (ifq, m);
611 if (! (ifp->if_flags & IFF_OACTIVE))
612 (*ifp->if_start) (ifp);
613
614 /*
615 * Count output packets and bytes.
616 * The packet length includes header, FCS and 1 flag,
617 * according to RFC 1333.
618 */
619 ifp->if_obytes += m->m_pkthdr.len + 3;
620 splx (s);
621 return (0);
622}
623
624void
625sppp_attach(struct ifnet *ifp)
626{
627 struct sppp *sp = (struct sppp*) ifp;
628
629 /* Initialize keepalive handler. */
630 if (! spppq)
455 m_freem (m);
456 return;
457 case PPP_IP:
458 if (sp->state[IDX_IPCP] == STATE_OPENED) {
459 schednetisr (NETISR_IP);
460 inq = &ipintrq;
461 }
462 break;
463#endif
464#ifdef IPX
465 case PPP_IPX:
466 /* IPX IPXCP not implemented yet */
467 if (sp->pp_phase == PHASE_NETWORK) {
468 schednetisr (NETISR_IPX);
469 inq = &ipxintrq;
470 }
471 break;
472#endif
473#ifdef NS
474 case PPP_XNS:
475 /* XNS IDPCP not implemented yet */
476 if (sp->pp_phase == PHASE_NETWORK) {
477 schednetisr (NETISR_NS);
478 inq = &nsintrq;
479 }
480 break;
481#endif
482#ifdef ISO
483 case PPP_ISO:
484 /* OSI NLCP not implemented yet */
485 if (sp->pp_phase == PHASE_NETWORK) {
486 schednetisr (NETISR_ISO);
487 inq = &clnlintrq;
488 }
489 break;
490#endif
491 }
492 break;
493 case CISCO_MULTICAST:
494 case CISCO_UNICAST:
495 /* Don't check the control field here (RFC 1547). */
496 if (! (sp->pp_flags & PP_CISCO)) {
497 if (debug)
498 log(LOG_DEBUG,
499 "%s%d: Cisco packet in PPP mode "
500 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
501 ifp->if_name, ifp->if_unit,
502 h->address, h->control, ntohs(h->protocol));
503 goto drop;
504 }
505 switch (ntohs (h->protocol)) {
506 default:
507 ++ifp->if_noproto;
508 goto invalid;
509 case CISCO_KEEPALIVE:
510 sppp_cisco_input ((struct sppp*) ifp, m);
511 m_freem (m);
512 return;
513#ifdef INET
514 case ETHERTYPE_IP:
515 schednetisr (NETISR_IP);
516 inq = &ipintrq;
517 break;
518#endif
519#ifdef IPX
520 case ETHERTYPE_IPX:
521 schednetisr (NETISR_IPX);
522 inq = &ipxintrq;
523 break;
524#endif
525#ifdef NS
526 case ETHERTYPE_NS:
527 schednetisr (NETISR_NS);
528 inq = &nsintrq;
529 break;
530#endif
531 }
532 break;
533 default: /* Invalid PPP packet. */
534 invalid:
535 if (debug)
536 log(LOG_DEBUG,
537 "%s%d: invalid input packet "
538 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
539 ifp->if_name, ifp->if_unit,
540 h->address, h->control, ntohs(h->protocol));
541 goto drop;
542 }
543
544 if (! (ifp->if_flags & IFF_UP) || ! inq)
545 goto drop;
546
547 /* Check queue. */
548 s = splimp();
549 if (IF_QFULL (inq)) {
550 /* Queue overflow. */
551 IF_DROP(inq);
552 splx(s);
553 if (debug)
554 log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
555 ifp->if_name, ifp->if_unit);
556 goto drop;
557 }
558 IF_ENQUEUE(inq, m);
559 splx(s);
560}
561
562/*
563 * Enqueue transmit packet.
564 */
565static int
566sppp_output(struct ifnet *ifp, struct mbuf *m,
567 struct sockaddr *dst, struct rtentry *rt)
568{
569 struct sppp *sp = (struct sppp*) ifp;
570 struct ppp_header *h;
571 struct ifqueue *ifq;
572 int s, rv = 0;
573
574 s = splimp();
575
576 if ((ifp->if_flags & IFF_UP) == 0 ||
577 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
578 m_freem (m);
579 splx (s);
580 return (ENETDOWN);
581 }
582
583 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
584 /*
585 * Interface is not yet running, but auto-dial. Need
586 * to start LCP for it.
587 */
588 ifp->if_flags |= IFF_RUNNING;
589 splx(s);
590 lcp.Open(sp);
591 s = splimp();
592 }
593
594 ifq = &ifp->if_snd;
595#ifdef INET
596 /*
597 * Put low delay, telnet, rlogin and ftp control packets
598 * in front of the queue.
599 */
600 if (dst->sa_family == AF_INET) {
601 struct ip *ip = mtod (m, struct ip*);
602 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
603
604 if (! IF_QFULL (&sp->pp_fastq) &&
605 ((ip->ip_tos & IPTOS_LOWDELAY) ||
606 ip->ip_p == IPPROTO_TCP &&
607 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
608 (INTERACTIVE (ntohs (tcp->th_sport)) ||
609 INTERACTIVE (ntohs (tcp->th_dport)))))
610 ifq = &sp->pp_fastq;
611 }
612#endif
613
614 /*
615 * Prepend general data packet PPP header. For now, IP only.
616 */
617 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
618 if (! m) {
619 if (ifp->if_flags & IFF_DEBUG)
620 log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
621 ifp->if_name, ifp->if_unit);
622 ++ifp->if_oerrors;
623 splx (s);
624 return (ENOBUFS);
625 }
626 h = mtod (m, struct ppp_header*);
627 if (sp->pp_flags & PP_CISCO) {
628 h->address = CISCO_UNICAST; /* unicast address */
629 h->control = 0;
630 } else {
631 h->address = PPP_ALLSTATIONS; /* broadcast address */
632 h->control = PPP_UI; /* Unnumbered Info */
633 }
634
635 switch (dst->sa_family) {
636#ifdef INET
637 case AF_INET: /* Internet Protocol */
638 if (sp->pp_flags & PP_CISCO)
639 h->protocol = htons (ETHERTYPE_IP);
640 else {
641 /*
642 * Don't choke with an ENETDOWN early. It's
643 * possible that we just started dialing out,
644 * so don't drop the packet immediately. If
645 * we notice that we run out of buffer space
646 * below, we will however remember that we are
647 * not ready to carry IP packets, and return
648 * ENETDOWN, as opposed to ENOBUFS.
649 */
650 h->protocol = htons(PPP_IP);
651 if (sp->state[IDX_IPCP] != STATE_OPENED)
652 rv = ENETDOWN;
653 }
654 break;
655#endif
656#ifdef NS
657 case AF_NS: /* Xerox NS Protocol */
658 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
659 ETHERTYPE_NS : PPP_XNS);
660 break;
661#endif
662#ifdef IPX
663 case AF_IPX: /* Novell IPX Protocol */
664 h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
665 ETHERTYPE_IPX : PPP_IPX);
666 break;
667#endif
668#ifdef ISO
669 case AF_ISO: /* ISO OSI Protocol */
670 if (sp->pp_flags & PP_CISCO)
671 goto nosupport;
672 h->protocol = htons (PPP_ISO);
673 break;
674nosupport:
675#endif
676 default:
677 m_freem (m);
678 ++ifp->if_oerrors;
679 splx (s);
680 return (EAFNOSUPPORT);
681 }
682
683 /*
684 * Queue message on interface, and start output if interface
685 * not yet active.
686 */
687 if (IF_QFULL (ifq)) {
688 IF_DROP (&ifp->if_snd);
689 m_freem (m);
690 ++ifp->if_oerrors;
691 splx (s);
692 return (rv? rv: ENOBUFS);
693 }
694 IF_ENQUEUE (ifq, m);
695 if (! (ifp->if_flags & IFF_OACTIVE))
696 (*ifp->if_start) (ifp);
697
698 /*
699 * Count output packets and bytes.
700 * The packet length includes header, FCS and 1 flag,
701 * according to RFC 1333.
702 */
703 ifp->if_obytes += m->m_pkthdr.len + 3;
704 splx (s);
705 return (0);
706}
707
708void
709sppp_attach(struct ifnet *ifp)
710{
711 struct sppp *sp = (struct sppp*) ifp;
712
713 /* Initialize keepalive handler. */
714 if (! spppq)
631 keepalive_ch = timeout (sppp_keepalive, 0, hz * 10);
715 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
632
633 /* Insert new entry into the keepalive list. */
634 sp->pp_next = spppq;
635 spppq = sp;
636
637 sp->pp_if.if_type = IFT_PPP;
638 sp->pp_if.if_output = sppp_output;
639 sp->pp_fastq.ifq_maxlen = 32;
640 sp->pp_cpq.ifq_maxlen = 20;
641 sp->pp_loopcnt = 0;
642 sp->pp_alivecnt = 0;
643 sp->pp_seq = 0;
644 sp->pp_rseq = 0;
645 sp->pp_phase = PHASE_DEAD;
646 sp->pp_up = lcp.Up;
647 sp->pp_down = lcp.Down;
648
649 sppp_lcp_init(sp);
650 sppp_ipcp_init(sp);
716
717 /* Insert new entry into the keepalive list. */
718 sp->pp_next = spppq;
719 spppq = sp;
720
721 sp->pp_if.if_type = IFT_PPP;
722 sp->pp_if.if_output = sppp_output;
723 sp->pp_fastq.ifq_maxlen = 32;
724 sp->pp_cpq.ifq_maxlen = 20;
725 sp->pp_loopcnt = 0;
726 sp->pp_alivecnt = 0;
727 sp->pp_seq = 0;
728 sp->pp_rseq = 0;
729 sp->pp_phase = PHASE_DEAD;
730 sp->pp_up = lcp.Up;
731 sp->pp_down = lcp.Down;
732
733 sppp_lcp_init(sp);
734 sppp_ipcp_init(sp);
735 sppp_pap_init(sp);
736 sppp_chap_init(sp);
651}
652
737}
738
653void
739void
654sppp_detach(struct ifnet *ifp)
655{
656 struct sppp **q, *p, *sp = (struct sppp*) ifp;
657 int i;
658
659 /* Remove the entry from the keepalive list. */
660 for (q = &spppq; (p = *q); q = &p->pp_next)
661 if (p == sp) {
662 *q = p->pp_next;
663 break;
664 }
665
666 /* Stop keepalive handler. */
667 if (! spppq)
740sppp_detach(struct ifnet *ifp)
741{
742 struct sppp **q, *p, *sp = (struct sppp*) ifp;
743 int i;
744
745 /* Remove the entry from the keepalive list. */
746 for (q = &spppq; (p = *q); q = &p->pp_next)
747 if (p == sp) {
748 *q = p->pp_next;
749 break;
750 }
751
752 /* Stop keepalive handler. */
753 if (! spppq)
668 untimeout (sppp_keepalive, 0, keepalive_ch);
754 untimeout(sppp_keepalive, 0, keepalive_ch);
669
670 for (i = 0; i < IDX_COUNT; i++)
671 untimeout((cps[i])->TO, (void *)sp, sp->ch[i]);
755
756 for (i = 0; i < IDX_COUNT; i++)
757 untimeout((cps[i])->TO, (void *)sp, sp->ch[i]);
758 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
672}
673
674/*
675 * Flush the interface output queue.
676 */
677void
678sppp_flush(struct ifnet *ifp)
679{
680 struct sppp *sp = (struct sppp*) ifp;
681
682 sppp_qflush (&sp->pp_if.if_snd);
683 sppp_qflush (&sp->pp_fastq);
684 sppp_qflush (&sp->pp_cpq);
685}
686
687/*
688 * Check if the output queue is empty.
689 */
690int
691sppp_isempty(struct ifnet *ifp)
692{
693 struct sppp *sp = (struct sppp*) ifp;
694 int empty, s;
695
696 s = splimp();
697 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
698 !sp->pp_if.if_snd.ifq_head;
699 splx(s);
700 return (empty);
701}
702
703/*
704 * Get next packet to send.
705 */
706struct mbuf *
707sppp_dequeue(struct ifnet *ifp)
708{
709 struct sppp *sp = (struct sppp*) ifp;
710 struct mbuf *m;
711 int s;
712
713 s = splimp();
714 /*
759}
760
761/*
762 * Flush the interface output queue.
763 */
764void
765sppp_flush(struct ifnet *ifp)
766{
767 struct sppp *sp = (struct sppp*) ifp;
768
769 sppp_qflush (&sp->pp_if.if_snd);
770 sppp_qflush (&sp->pp_fastq);
771 sppp_qflush (&sp->pp_cpq);
772}
773
774/*
775 * Check if the output queue is empty.
776 */
777int
778sppp_isempty(struct ifnet *ifp)
779{
780 struct sppp *sp = (struct sppp*) ifp;
781 int empty, s;
782
783 s = splimp();
784 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
785 !sp->pp_if.if_snd.ifq_head;
786 splx(s);
787 return (empty);
788}
789
790/*
791 * Get next packet to send.
792 */
793struct mbuf *
794sppp_dequeue(struct ifnet *ifp)
795{
796 struct sppp *sp = (struct sppp*) ifp;
797 struct mbuf *m;
798 int s;
799
800 s = splimp();
801 /*
715 * Process only the control protocol queue until we are in
716 * network phase.
802 * Process only the control protocol queue until we have at
803 * least one NCP open.
717 *
804 *
718 * XXX Network phase itself is still not a sufficient test, we
719 * normally should keep a separate queue for each supported
720 * protocol family, and only serve these queues as the
721 * respective NCPs were opened. The simplistic logic used
722 * here might cause some loss of network traffic while the
723 * NCPs are being negotiated, in particular if the NCPs take a
724 * long time to negotiate.
725 *
726 * Do always serve all three queues in Cisco mode.
727 */
728 IF_DEQUEUE(&sp->pp_cpq, m);
729 if (m == NULL &&
805 * Do always serve all three queues in Cisco mode.
806 */
807 IF_DEQUEUE(&sp->pp_cpq, m);
808 if (m == NULL &&
730 (sp->pp_phase == PHASE_NETWORK ||
731 (sp->pp_flags & PP_CISCO) != 0)) {
809 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
732 IF_DEQUEUE(&sp->pp_fastq, m);
733 if (m == NULL)
734 IF_DEQUEUE (&sp->pp_if.if_snd, m);
735 }
736 splx(s);
737 return m;
738}
739
740/*
810 IF_DEQUEUE(&sp->pp_fastq, m);
811 if (m == NULL)
812 IF_DEQUEUE (&sp->pp_if.if_snd, m);
813 }
814 splx(s);
815 return m;
816}
817
818/*
819 * Pick the next packet, do not remove it from the queue.
820 */
821struct mbuf *
822sppp_pick(struct ifnet *ifp)
823{
824 struct sppp *sp = (struct sppp*)ifp;
825 struct mbuf *m;
826 int s;
827
828 s= splimp ();
829
830 m = sp->pp_cpq.ifq_head;
831 if (m == NULL &&
832 (sp->pp_phase == PHASE_NETWORK ||
833 (sp->pp_flags & PP_CISCO) != 0))
834 if ((m = sp->pp_fastq.ifq_head) == NULL)
835 m = sp->pp_if.if_snd.ifq_head;
836 splx (s);
837 return (m);
838}
839
840/*
741 * Process an ioctl request. Called on low priority level.
742 */
743int
744sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
745{
746 struct ifreq *ifr = (struct ifreq*) data;
747 struct sppp *sp = (struct sppp*) ifp;
841 * Process an ioctl request. Called on low priority level.
842 */
843int
844sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
845{
846 struct ifreq *ifr = (struct ifreq*) data;
847 struct sppp *sp = (struct sppp*) ifp;
748 int s, going_up, going_down, newmode;
848 int s, rv, going_up, going_down, newmode;
749
750 s = splimp();
849
850 s = splimp();
851 rv = 0;
751 switch (cmd) {
752 case SIOCAIFADDR:
753 case SIOCSIFDSTADDR:
754 break;
755
756 case SIOCSIFADDR:
757 if_up(ifp);
758 /* fall through... */
759
760 case SIOCSIFFLAGS:
761 going_up = ifp->if_flags & IFF_UP &&
762 (ifp->if_flags & IFF_RUNNING) == 0;
763 going_down = (ifp->if_flags & IFF_UP) == 0 &&
764 ifp->if_flags & IFF_RUNNING;
765 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
766 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
767 /* sanity */
768 newmode = IFF_PASSIVE;
769 ifp->if_flags &= ~IFF_AUTO;
770 }
771
772 if (going_up || going_down)
773 lcp.Close(sp);
774 if (going_up && newmode == 0) {
775 /* neither auto-dial nor passive */
776 ifp->if_flags |= IFF_RUNNING;
777 if (!(sp->pp_flags & PP_CISCO))
778 lcp.Open(sp);
779 } else if (going_down) {
780 sppp_flush(ifp);
781 ifp->if_flags &= ~IFF_RUNNING;
782 }
783
784 break;
785
786#ifdef SIOCSIFMTU
787#ifndef ifr_mtu
788#define ifr_mtu ifr_metric
789#endif
790 case SIOCSIFMTU:
791 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
792 return (EINVAL);
793 ifp->if_mtu = ifr->ifr_mtu;
794 break;
795#endif
796#ifdef SLIOCSETMTU
797 case SLIOCSETMTU:
798 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
799 return (EINVAL);
800 ifp->if_mtu = *(short*)data;
801 break;
802#endif
803#ifdef SIOCGIFMTU
804 case SIOCGIFMTU:
805 ifr->ifr_mtu = ifp->if_mtu;
806 break;
807#endif
808#ifdef SLIOCGETMTU
809 case SLIOCGETMTU:
810 *(short*)data = ifp->if_mtu;
811 break;
812#endif
813 case SIOCADDMULTI:
814 case SIOCDELMULTI:
815 break;
816
852 switch (cmd) {
853 case SIOCAIFADDR:
854 case SIOCSIFDSTADDR:
855 break;
856
857 case SIOCSIFADDR:
858 if_up(ifp);
859 /* fall through... */
860
861 case SIOCSIFFLAGS:
862 going_up = ifp->if_flags & IFF_UP &&
863 (ifp->if_flags & IFF_RUNNING) == 0;
864 going_down = (ifp->if_flags & IFF_UP) == 0 &&
865 ifp->if_flags & IFF_RUNNING;
866 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
867 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
868 /* sanity */
869 newmode = IFF_PASSIVE;
870 ifp->if_flags &= ~IFF_AUTO;
871 }
872
873 if (going_up || going_down)
874 lcp.Close(sp);
875 if (going_up && newmode == 0) {
876 /* neither auto-dial nor passive */
877 ifp->if_flags |= IFF_RUNNING;
878 if (!(sp->pp_flags & PP_CISCO))
879 lcp.Open(sp);
880 } else if (going_down) {
881 sppp_flush(ifp);
882 ifp->if_flags &= ~IFF_RUNNING;
883 }
884
885 break;
886
887#ifdef SIOCSIFMTU
888#ifndef ifr_mtu
889#define ifr_mtu ifr_metric
890#endif
891 case SIOCSIFMTU:
892 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
893 return (EINVAL);
894 ifp->if_mtu = ifr->ifr_mtu;
895 break;
896#endif
897#ifdef SLIOCSETMTU
898 case SLIOCSETMTU:
899 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
900 return (EINVAL);
901 ifp->if_mtu = *(short*)data;
902 break;
903#endif
904#ifdef SIOCGIFMTU
905 case SIOCGIFMTU:
906 ifr->ifr_mtu = ifp->if_mtu;
907 break;
908#endif
909#ifdef SLIOCGETMTU
910 case SLIOCGETMTU:
911 *(short*)data = ifp->if_mtu;
912 break;
913#endif
914 case SIOCADDMULTI:
915 case SIOCDELMULTI:
916 break;
917
918 case SIOCGIFGENERIC:
919 case SIOCSIFGENERIC:
920 rv = sppp_params(sp, cmd, data);
921 break;
922
817 default:
923 default:
818 splx(s);
819 return (ENOTTY);
924 rv = ENOTTY;
820 }
821 splx(s);
925 }
926 splx(s);
822 return (0);
927 return rv;
823}
824
825
826 /*
827 * Cisco framing implementation.
828 */
829
830/*
831 * Handle incoming Cisco keepalive protocol packets.
832 */
928}
929
930
931 /*
932 * Cisco framing implementation.
933 */
934
935/*
936 * Handle incoming Cisco keepalive protocol packets.
937 */
833static void
938static void
834sppp_cisco_input(struct sppp *sp, struct mbuf *m)
835{
836 STDDCL;
837 struct cisco_packet *h;
939sppp_cisco_input(struct sppp *sp, struct mbuf *m)
940{
941 STDDCL;
942 struct cisco_packet *h;
838 struct ifaddr *ifa;
943 u_long me, mymask;
839
840 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
841 if (debug)
842 log(LOG_DEBUG,
944
945 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
946 if (debug)
947 log(LOG_DEBUG,
843 "%s%d: invalid cisco packet length: %d bytes\n",
948 "%s%d: cisco invalid packet length: %d bytes\n",
844 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
845 return;
846 }
847 h = mtod (m, struct cisco_packet*);
848 if (debug)
849 log(LOG_DEBUG,
850 "%s%d: cisco input: %d bytes "
851 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
852 ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
853 ntohl (h->type), h->par1, h->par2, h->rel,
854 h->time0, h->time1);
855 switch (ntohl (h->type)) {
856 default:
857 if (debug)
949 ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
950 return;
951 }
952 h = mtod (m, struct cisco_packet*);
953 if (debug)
954 log(LOG_DEBUG,
955 "%s%d: cisco input: %d bytes "
956 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
957 ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
958 ntohl (h->type), h->par1, h->par2, h->rel,
959 h->time0, h->time1);
960 switch (ntohl (h->type)) {
961 default:
962 if (debug)
858 addlog("%s%d: unknown cisco packet type: 0x%lx\n",
963 addlog("%s%d: cisco unknown packet type: 0x%lx\n",
859 ifp->if_name, ifp->if_unit, ntohl (h->type));
860 break;
861 case CISCO_ADDR_REPLY:
862 /* Reply on address request, ignore */
863 break;
864 case CISCO_KEEPALIVE_REQ:
865 sp->pp_alivecnt = 0;
866 sp->pp_rseq = ntohl (h->par1);
867 if (sp->pp_seq == sp->pp_rseq) {
868 /* Local and remote sequence numbers are equal.
869 * Probably, the line is in loopback mode. */
870 if (sp->pp_loopcnt >= MAXALIVECNT) {
871 printf ("%s%d: loopback\n",
872 ifp->if_name, ifp->if_unit);
873 sp->pp_loopcnt = 0;
874 if (ifp->if_flags & IFF_UP) {
875 if_down (ifp);
876 sppp_qflush (&sp->pp_cpq);
877 }
878 }
879 ++sp->pp_loopcnt;
880
881 /* Generate new local sequence number */
882 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
883 break;
884 }
964 ifp->if_name, ifp->if_unit, ntohl (h->type));
965 break;
966 case CISCO_ADDR_REPLY:
967 /* Reply on address request, ignore */
968 break;
969 case CISCO_KEEPALIVE_REQ:
970 sp->pp_alivecnt = 0;
971 sp->pp_rseq = ntohl (h->par1);
972 if (sp->pp_seq == sp->pp_rseq) {
973 /* Local and remote sequence numbers are equal.
974 * Probably, the line is in loopback mode. */
975 if (sp->pp_loopcnt >= MAXALIVECNT) {
976 printf ("%s%d: loopback\n",
977 ifp->if_name, ifp->if_unit);
978 sp->pp_loopcnt = 0;
979 if (ifp->if_flags & IFF_UP) {
980 if_down (ifp);
981 sppp_qflush (&sp->pp_cpq);
982 }
983 }
984 ++sp->pp_loopcnt;
985
986 /* Generate new local sequence number */
987 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
988 break;
989 }
885 sp->pp_loopcnt = 0;
990 sp->pp_loopcnt = 0;
886 if (! (ifp->if_flags & IFF_UP) &&
887 (ifp->if_flags & IFF_RUNNING)) {
991 if (! (ifp->if_flags & IFF_UP) &&
992 (ifp->if_flags & IFF_RUNNING)) {
888 ifp->if_flags |= IFF_UP;
993 if_up(ifp);
889 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
890 }
891 break;
892 case CISCO_ADDR_REQ:
994 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
995 }
996 break;
997 case CISCO_ADDR_REQ:
893 for (ifa=ifp->if_addrhead.tqh_first; ifa;
894 ifa=ifa->ifa_link.tqe_next)
895 if (ifa->ifa_addr->sa_family == AF_INET)
896 break;
897 if (! ifa) {
898 if (debug)
899 addlog("%s%d: unknown address for cisco request\n",
900 ifp->if_name, ifp->if_unit);
901 return;
902 }
903 sppp_cisco_send (sp, CISCO_ADDR_REPLY,
904 ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
905 ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
998 sppp_get_ip_addrs(sp, &me, 0, &mymask);
999 if (me != 0L)
1000 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
906 break;
907 }
908}
909
910/*
911 * Send Cisco keepalive packet.
912 */
913static void
914sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
915{
916 STDDCL;
917 struct ppp_header *h;
918 struct cisco_packet *ch;
919 struct mbuf *m;
920 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
921
922 MGETHDR (m, M_DONTWAIT, MT_DATA);
923 if (! m)
924 return;
925 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
926 m->m_pkthdr.rcvif = 0;
927
928 h = mtod (m, struct ppp_header*);
929 h->address = CISCO_MULTICAST;
930 h->control = 0;
931 h->protocol = htons (CISCO_KEEPALIVE);
932
933 ch = (struct cisco_packet*) (h + 1);
934 ch->type = htonl (type);
935 ch->par1 = htonl (par1);
936 ch->par2 = htonl (par2);
937 ch->rel = -1;
938 ch->time0 = htons ((u_short) (t >> 16));
939 ch->time1 = htons ((u_short) t);
940
941 if (debug)
942 log(LOG_DEBUG,
943 "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
944 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
945 ch->par2, ch->rel, ch->time0, ch->time1);
946
947 if (IF_QFULL (&sp->pp_cpq)) {
948 IF_DROP (&sp->pp_fastq);
949 IF_DROP (&ifp->if_snd);
950 m_freem (m);
951 } else
952 IF_ENQUEUE (&sp->pp_cpq, m);
953 if (! (ifp->if_flags & IFF_OACTIVE))
954 (*ifp->if_start) (ifp);
955 ifp->if_obytes += m->m_pkthdr.len + 3;
956}
957
958 /*
959 * PPP protocol implementation.
960 */
961
962/*
963 * Send PPP control protocol packet.
964 */
965static void
966sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
967 u_char ident, u_short len, void *data)
968{
969 STDDCL;
970 struct ppp_header *h;
971 struct lcp_header *lh;
972 struct mbuf *m;
973
974 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
975 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
976 MGETHDR (m, M_DONTWAIT, MT_DATA);
977 if (! m)
978 return;
979 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
980 m->m_pkthdr.rcvif = 0;
981
982 h = mtod (m, struct ppp_header*);
983 h->address = PPP_ALLSTATIONS; /* broadcast address */
984 h->control = PPP_UI; /* Unnumbered Info */
985 h->protocol = htons (proto); /* Link Control Protocol */
986
987 lh = (struct lcp_header*) (h + 1);
988 lh->type = type;
989 lh->ident = ident;
990 lh->len = htons (LCP_HEADER_LEN + len);
991 if (len)
992 bcopy (data, lh+1, len);
993
994 if (debug) {
995 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
996 ifp->if_name, ifp->if_unit,
997 sppp_proto_name(proto),
998 sppp_cp_type_name (lh->type), lh->ident,
999 ntohs (lh->len));
1000 if (len)
1001 sppp_print_bytes ((u_char*) (lh+1), len);
1002 addlog(">\n");
1003 }
1004 if (IF_QFULL (&sp->pp_cpq)) {
1005 IF_DROP (&sp->pp_fastq);
1006 IF_DROP (&ifp->if_snd);
1007 m_freem (m);
1008 ++ifp->if_oerrors;
1009 } else
1010 IF_ENQUEUE (&sp->pp_cpq, m);
1011 if (! (ifp->if_flags & IFF_OACTIVE))
1012 (*ifp->if_start) (ifp);
1013 ifp->if_obytes += m->m_pkthdr.len + 3;
1014}
1015
1016/*
1017 * Handle incoming PPP control protocol packets.
1018 */
1019static void
1020sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1021{
1022 STDDCL;
1023 struct lcp_header *h;
1024 int len = m->m_pkthdr.len;
1025 int rv;
1026 u_char *p;
1027
1028 if (len < 4) {
1029 if (debug)
1030 log(LOG_DEBUG,
1031 "%s%d: %s invalid packet length: %d bytes\n",
1032 ifp->if_name, ifp->if_unit, cp->name, len);
1033 return;
1034 }
1035 h = mtod (m, struct lcp_header*);
1036 if (debug) {
1037 log(LOG_DEBUG,
1038 "%s%d: %s input(%s): <%s id=0x%x len=%d",
1039 ifp->if_name, ifp->if_unit, cp->name,
1040 sppp_state_name(sp->state[cp->protoidx]),
1041 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1042 if (len > 4)
1043 sppp_print_bytes ((u_char*) (h+1), len-4);
1044 addlog(">\n");
1045 }
1046 if (len > ntohs (h->len))
1047 len = ntohs (h->len);
1001 break;
1002 }
1003}
1004
1005/*
1006 * Send Cisco keepalive packet.
1007 */
1008static void
1009sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1010{
1011 STDDCL;
1012 struct ppp_header *h;
1013 struct cisco_packet *ch;
1014 struct mbuf *m;
1015 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1016
1017 MGETHDR (m, M_DONTWAIT, MT_DATA);
1018 if (! m)
1019 return;
1020 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1021 m->m_pkthdr.rcvif = 0;
1022
1023 h = mtod (m, struct ppp_header*);
1024 h->address = CISCO_MULTICAST;
1025 h->control = 0;
1026 h->protocol = htons (CISCO_KEEPALIVE);
1027
1028 ch = (struct cisco_packet*) (h + 1);
1029 ch->type = htonl (type);
1030 ch->par1 = htonl (par1);
1031 ch->par2 = htonl (par2);
1032 ch->rel = -1;
1033 ch->time0 = htons ((u_short) (t >> 16));
1034 ch->time1 = htons ((u_short) t);
1035
1036 if (debug)
1037 log(LOG_DEBUG,
1038 "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1039 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1040 ch->par2, ch->rel, ch->time0, ch->time1);
1041
1042 if (IF_QFULL (&sp->pp_cpq)) {
1043 IF_DROP (&sp->pp_fastq);
1044 IF_DROP (&ifp->if_snd);
1045 m_freem (m);
1046 } else
1047 IF_ENQUEUE (&sp->pp_cpq, m);
1048 if (! (ifp->if_flags & IFF_OACTIVE))
1049 (*ifp->if_start) (ifp);
1050 ifp->if_obytes += m->m_pkthdr.len + 3;
1051}
1052
1053 /*
1054 * PPP protocol implementation.
1055 */
1056
1057/*
1058 * Send PPP control protocol packet.
1059 */
1060static void
1061sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1062 u_char ident, u_short len, void *data)
1063{
1064 STDDCL;
1065 struct ppp_header *h;
1066 struct lcp_header *lh;
1067 struct mbuf *m;
1068
1069 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1070 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1071 MGETHDR (m, M_DONTWAIT, MT_DATA);
1072 if (! m)
1073 return;
1074 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1075 m->m_pkthdr.rcvif = 0;
1076
1077 h = mtod (m, struct ppp_header*);
1078 h->address = PPP_ALLSTATIONS; /* broadcast address */
1079 h->control = PPP_UI; /* Unnumbered Info */
1080 h->protocol = htons (proto); /* Link Control Protocol */
1081
1082 lh = (struct lcp_header*) (h + 1);
1083 lh->type = type;
1084 lh->ident = ident;
1085 lh->len = htons (LCP_HEADER_LEN + len);
1086 if (len)
1087 bcopy (data, lh+1, len);
1088
1089 if (debug) {
1090 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
1091 ifp->if_name, ifp->if_unit,
1092 sppp_proto_name(proto),
1093 sppp_cp_type_name (lh->type), lh->ident,
1094 ntohs (lh->len));
1095 if (len)
1096 sppp_print_bytes ((u_char*) (lh+1), len);
1097 addlog(">\n");
1098 }
1099 if (IF_QFULL (&sp->pp_cpq)) {
1100 IF_DROP (&sp->pp_fastq);
1101 IF_DROP (&ifp->if_snd);
1102 m_freem (m);
1103 ++ifp->if_oerrors;
1104 } else
1105 IF_ENQUEUE (&sp->pp_cpq, m);
1106 if (! (ifp->if_flags & IFF_OACTIVE))
1107 (*ifp->if_start) (ifp);
1108 ifp->if_obytes += m->m_pkthdr.len + 3;
1109}
1110
1111/*
1112 * Handle incoming PPP control protocol packets.
1113 */
1114static void
1115sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1116{
1117 STDDCL;
1118 struct lcp_header *h;
1119 int len = m->m_pkthdr.len;
1120 int rv;
1121 u_char *p;
1122
1123 if (len < 4) {
1124 if (debug)
1125 log(LOG_DEBUG,
1126 "%s%d: %s invalid packet length: %d bytes\n",
1127 ifp->if_name, ifp->if_unit, cp->name, len);
1128 return;
1129 }
1130 h = mtod (m, struct lcp_header*);
1131 if (debug) {
1132 log(LOG_DEBUG,
1133 "%s%d: %s input(%s): <%s id=0x%x len=%d",
1134 ifp->if_name, ifp->if_unit, cp->name,
1135 sppp_state_name(sp->state[cp->protoidx]),
1136 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1137 if (len > 4)
1138 sppp_print_bytes ((u_char*) (h+1), len-4);
1139 addlog(">\n");
1140 }
1141 if (len > ntohs (h->len))
1142 len = ntohs (h->len);
1143 p = (u_char *)(h + 1);
1048 switch (h->type) {
1049 case CONF_REQ:
1050 if (len < 4) {
1051 if (debug)
1052 addlog("%s%d: %s invalid conf-req length %d\n",
1053 ifp->if_name, ifp->if_unit, cp->name,
1054 len);
1055 ++ifp->if_ierrors;
1056 break;
1057 }
1144 switch (h->type) {
1145 case CONF_REQ:
1146 if (len < 4) {
1147 if (debug)
1148 addlog("%s%d: %s invalid conf-req length %d\n",
1149 ifp->if_name, ifp->if_unit, cp->name,
1150 len);
1151 ++ifp->if_ierrors;
1152 break;
1153 }
1154 /* handle states where RCR doesn't get a SCA/SCN */
1155 switch (sp->state[cp->protoidx]) {
1156 case STATE_CLOSING:
1157 case STATE_STOPPING:
1158 return;
1159 case STATE_CLOSED:
1160 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1161 0, 0);
1162 return;
1163 }
1058 rv = (cp->RCR)(sp, h, len);
1059 switch (sp->state[cp->protoidx]) {
1060 case STATE_OPENED:
1061 (cp->tld)(sp);
1062 (cp->scr)(sp);
1063 /* fall through... */
1064 case STATE_ACK_SENT:
1065 case STATE_REQ_SENT:
1066 sppp_cp_change_state(cp, sp, rv?
1067 STATE_ACK_SENT: STATE_REQ_SENT);
1068 break;
1164 rv = (cp->RCR)(sp, h, len);
1165 switch (sp->state[cp->protoidx]) {
1166 case STATE_OPENED:
1167 (cp->tld)(sp);
1168 (cp->scr)(sp);
1169 /* fall through... */
1170 case STATE_ACK_SENT:
1171 case STATE_REQ_SENT:
1172 sppp_cp_change_state(cp, sp, rv?
1173 STATE_ACK_SENT: STATE_REQ_SENT);
1174 break;
1069 case STATE_CLOSING:
1070 case STATE_STOPPING:
1071 break;
1072 case STATE_STOPPED:
1073 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1074 (cp->scr)(sp);
1075 sppp_cp_change_state(cp, sp, rv?
1076 STATE_ACK_SENT: STATE_REQ_SENT);
1077 break;
1175 case STATE_STOPPED:
1176 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1177 (cp->scr)(sp);
1178 sppp_cp_change_state(cp, sp, rv?
1179 STATE_ACK_SENT: STATE_REQ_SENT);
1180 break;
1078 case STATE_CLOSED:
1079 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1080 0, 0);
1081 break;
1082 case STATE_ACK_RCVD:
1083 if (rv) {
1084 sppp_cp_change_state(cp, sp, STATE_OPENED);
1085 if (debug)
1086 log(LOG_DEBUG, "%s%d: %s tlu\n",
1087 ifp->if_name, ifp->if_unit,
1088 cp->name);
1089 (cp->tlu)(sp);
1090 } else
1091 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1092 break;
1093 default:
1094 printf("%s%d: %s illegal %s in state %s\n",
1095 ifp->if_name, ifp->if_unit, cp->name,
1096 sppp_cp_type_name(h->type),
1097 sppp_state_name(sp->state[cp->protoidx]));
1098 ++ifp->if_ierrors;
1099 }
1100 break;
1101 case CONF_ACK:
1102 if (h->ident != sp->confid[cp->protoidx]) {
1103 if (debug)
1104 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1105 ifp->if_name, ifp->if_unit, cp->name,
1106 h->ident, sp->confid[cp->protoidx]);
1107 ++ifp->if_ierrors;
1108 break;
1109 }
1110 switch (sp->state[cp->protoidx]) {
1111 case STATE_CLOSED:
1112 case STATE_STOPPED:
1113 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1114 break;
1115 case STATE_CLOSING:
1116 case STATE_STOPPING:
1117 break;
1118 case STATE_REQ_SENT:
1119 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1120 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1121 break;
1122 case STATE_OPENED:
1123 (cp->tld)(sp);
1124 /* fall through */
1125 case STATE_ACK_RCVD:
1126 (cp->scr)(sp);
1127 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1128 break;
1129 case STATE_ACK_SENT:
1130 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1131 sppp_cp_change_state(cp, sp, STATE_OPENED);
1132 if (debug)
1181 case STATE_ACK_RCVD:
1182 if (rv) {
1183 sppp_cp_change_state(cp, sp, STATE_OPENED);
1184 if (debug)
1185 log(LOG_DEBUG, "%s%d: %s tlu\n",
1186 ifp->if_name, ifp->if_unit,
1187 cp->name);
1188 (cp->tlu)(sp);
1189 } else
1190 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1191 break;
1192 default:
1193 printf("%s%d: %s illegal %s in state %s\n",
1194 ifp->if_name, ifp->if_unit, cp->name,
1195 sppp_cp_type_name(h->type),
1196 sppp_state_name(sp->state[cp->protoidx]));
1197 ++ifp->if_ierrors;
1198 }
1199 break;
1200 case CONF_ACK:
1201 if (h->ident != sp->confid[cp->protoidx]) {
1202 if (debug)
1203 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1204 ifp->if_name, ifp->if_unit, cp->name,
1205 h->ident, sp->confid[cp->protoidx]);
1206 ++ifp->if_ierrors;
1207 break;
1208 }
1209 switch (sp->state[cp->protoidx]) {
1210 case STATE_CLOSED:
1211 case STATE_STOPPED:
1212 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1213 break;
1214 case STATE_CLOSING:
1215 case STATE_STOPPING:
1216 break;
1217 case STATE_REQ_SENT:
1218 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1219 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1220 break;
1221 case STATE_OPENED:
1222 (cp->tld)(sp);
1223 /* fall through */
1224 case STATE_ACK_RCVD:
1225 (cp->scr)(sp);
1226 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1227 break;
1228 case STATE_ACK_SENT:
1229 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1230 sppp_cp_change_state(cp, sp, STATE_OPENED);
1231 if (debug)
1133 addlog("%s%d: %s tlu\n",
1232 log(LOG_DEBUG, "%s%d: %s tlu\n",
1134 ifp->if_name, ifp->if_unit, cp->name);
1135 (cp->tlu)(sp);
1136 break;
1137 default:
1138 printf("%s%d: %s illegal %s in state %s\n",
1139 ifp->if_name, ifp->if_unit, cp->name,
1140 sppp_cp_type_name(h->type),
1141 sppp_state_name(sp->state[cp->protoidx]));
1142 ++ifp->if_ierrors;
1143 }
1144 break;
1145 case CONF_NAK:
1146 case CONF_REJ:
1147 if (h->ident != sp->confid[cp->protoidx]) {
1148 if (debug)
1149 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1150 ifp->if_name, ifp->if_unit, cp->name,
1151 h->ident, sp->confid[cp->protoidx]);
1152 ++ifp->if_ierrors;
1153 break;
1154 }
1155 if (h->type == CONF_NAK)
1156 (cp->RCN_nak)(sp, h, len);
1157 else /* CONF_REJ */
1158 (cp->RCN_rej)(sp, h, len);
1159
1160 switch (sp->state[cp->protoidx]) {
1161 case STATE_CLOSED:
1162 case STATE_STOPPED:
1163 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1164 break;
1165 case STATE_REQ_SENT:
1166 case STATE_ACK_SENT:
1167 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1168 (cp->scr)(sp);
1169 break;
1170 case STATE_OPENED:
1171 (cp->tld)(sp);
1172 /* fall through */
1173 case STATE_ACK_RCVD:
1174 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1175 (cp->scr)(sp);
1176 break;
1177 case STATE_CLOSING:
1178 case STATE_STOPPING:
1179 break;
1180 default:
1181 printf("%s%d: %s illegal %s in state %s\n",
1182 ifp->if_name, ifp->if_unit, cp->name,
1183 sppp_cp_type_name(h->type),
1184 sppp_state_name(sp->state[cp->protoidx]));
1185 ++ifp->if_ierrors;
1186 }
1187 break;
1188
1189 case TERM_REQ:
1190 switch (sp->state[cp->protoidx]) {
1191 case STATE_ACK_RCVD:
1192 case STATE_ACK_SENT:
1193 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1194 /* fall through */
1195 case STATE_CLOSED:
1196 case STATE_STOPPED:
1197 case STATE_CLOSING:
1198 case STATE_STOPPING:
1199 case STATE_REQ_SENT:
1200 sta:
1201 /* Send Terminate-Ack packet. */
1202 if (debug)
1203 log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1204 ifp->if_name, ifp->if_unit, cp->name);
1205 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1206 break;
1207 case STATE_OPENED:
1208 (cp->tld)(sp);
1209 sp->rst_counter[cp->protoidx] = 0;
1210 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1211 goto sta;
1212 break;
1213 default:
1214 printf("%s%d: %s illegal %s in state %s\n",
1215 ifp->if_name, ifp->if_unit, cp->name,
1216 sppp_cp_type_name(h->type),
1217 sppp_state_name(sp->state[cp->protoidx]));
1218 ++ifp->if_ierrors;
1219 }
1220 break;
1221 case TERM_ACK:
1222 switch (sp->state[cp->protoidx]) {
1223 case STATE_CLOSED:
1224 case STATE_STOPPED:
1225 case STATE_REQ_SENT:
1226 case STATE_ACK_SENT:
1227 break;
1228 case STATE_CLOSING:
1229 (cp->tlf)(sp);
1230 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1231 break;
1232 case STATE_STOPPING:
1233 (cp->tlf)(sp);
1234 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1235 break;
1236 case STATE_ACK_RCVD:
1237 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1238 break;
1239 case STATE_OPENED:
1240 (cp->tld)(sp);
1241 (cp->scr)(sp);
1242 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1243 break;
1244 default:
1245 printf("%s%d: %s illegal %s in state %s\n",
1246 ifp->if_name, ifp->if_unit, cp->name,
1247 sppp_cp_type_name(h->type),
1248 sppp_state_name(sp->state[cp->protoidx]));
1249 ++ifp->if_ierrors;
1250 }
1251 break;
1252 case CODE_REJ:
1253 case PROTO_REJ:
1254 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1233 ifp->if_name, ifp->if_unit, cp->name);
1234 (cp->tlu)(sp);
1235 break;
1236 default:
1237 printf("%s%d: %s illegal %s in state %s\n",
1238 ifp->if_name, ifp->if_unit, cp->name,
1239 sppp_cp_type_name(h->type),
1240 sppp_state_name(sp->state[cp->protoidx]));
1241 ++ifp->if_ierrors;
1242 }
1243 break;
1244 case CONF_NAK:
1245 case CONF_REJ:
1246 if (h->ident != sp->confid[cp->protoidx]) {
1247 if (debug)
1248 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1249 ifp->if_name, ifp->if_unit, cp->name,
1250 h->ident, sp->confid[cp->protoidx]);
1251 ++ifp->if_ierrors;
1252 break;
1253 }
1254 if (h->type == CONF_NAK)
1255 (cp->RCN_nak)(sp, h, len);
1256 else /* CONF_REJ */
1257 (cp->RCN_rej)(sp, h, len);
1258
1259 switch (sp->state[cp->protoidx]) {
1260 case STATE_CLOSED:
1261 case STATE_STOPPED:
1262 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1263 break;
1264 case STATE_REQ_SENT:
1265 case STATE_ACK_SENT:
1266 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1267 (cp->scr)(sp);
1268 break;
1269 case STATE_OPENED:
1270 (cp->tld)(sp);
1271 /* fall through */
1272 case STATE_ACK_RCVD:
1273 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1274 (cp->scr)(sp);
1275 break;
1276 case STATE_CLOSING:
1277 case STATE_STOPPING:
1278 break;
1279 default:
1280 printf("%s%d: %s illegal %s in state %s\n",
1281 ifp->if_name, ifp->if_unit, cp->name,
1282 sppp_cp_type_name(h->type),
1283 sppp_state_name(sp->state[cp->protoidx]));
1284 ++ifp->if_ierrors;
1285 }
1286 break;
1287
1288 case TERM_REQ:
1289 switch (sp->state[cp->protoidx]) {
1290 case STATE_ACK_RCVD:
1291 case STATE_ACK_SENT:
1292 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1293 /* fall through */
1294 case STATE_CLOSED:
1295 case STATE_STOPPED:
1296 case STATE_CLOSING:
1297 case STATE_STOPPING:
1298 case STATE_REQ_SENT:
1299 sta:
1300 /* Send Terminate-Ack packet. */
1301 if (debug)
1302 log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1303 ifp->if_name, ifp->if_unit, cp->name);
1304 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1305 break;
1306 case STATE_OPENED:
1307 (cp->tld)(sp);
1308 sp->rst_counter[cp->protoidx] = 0;
1309 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1310 goto sta;
1311 break;
1312 default:
1313 printf("%s%d: %s illegal %s in state %s\n",
1314 ifp->if_name, ifp->if_unit, cp->name,
1315 sppp_cp_type_name(h->type),
1316 sppp_state_name(sp->state[cp->protoidx]));
1317 ++ifp->if_ierrors;
1318 }
1319 break;
1320 case TERM_ACK:
1321 switch (sp->state[cp->protoidx]) {
1322 case STATE_CLOSED:
1323 case STATE_STOPPED:
1324 case STATE_REQ_SENT:
1325 case STATE_ACK_SENT:
1326 break;
1327 case STATE_CLOSING:
1328 (cp->tlf)(sp);
1329 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1330 break;
1331 case STATE_STOPPING:
1332 (cp->tlf)(sp);
1333 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1334 break;
1335 case STATE_ACK_RCVD:
1336 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1337 break;
1338 case STATE_OPENED:
1339 (cp->tld)(sp);
1340 (cp->scr)(sp);
1341 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1342 break;
1343 default:
1344 printf("%s%d: %s illegal %s in state %s\n",
1345 ifp->if_name, ifp->if_unit, cp->name,
1346 sppp_cp_type_name(h->type),
1347 sppp_state_name(sp->state[cp->protoidx]));
1348 ++ifp->if_ierrors;
1349 }
1350 break;
1351 case CODE_REJ:
1352 case PROTO_REJ:
1353 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1354 log(LOG_INFO,
1355 "%s%d: %s: ignoring RXJ (%s) for proto 0x%x, "
1356 "danger will robinson\n",
1357 ifp->if_name, ifp->if_unit, cp->name,
1358 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1255 switch (sp->state[cp->protoidx]) {
1256 case STATE_CLOSED:
1257 case STATE_STOPPED:
1258 case STATE_REQ_SENT:
1259 case STATE_ACK_SENT:
1260 case STATE_CLOSING:
1261 case STATE_STOPPING:
1262 case STATE_OPENED:
1263 break;
1264 case STATE_ACK_RCVD:
1265 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1266 break;
1267 default:
1268 printf("%s%d: %s illegal %s in state %s\n",
1269 ifp->if_name, ifp->if_unit, cp->name,
1270 sppp_cp_type_name(h->type),
1271 sppp_state_name(sp->state[cp->protoidx]));
1272 ++ifp->if_ierrors;
1273 }
1274 break;
1275 case DISC_REQ:
1276 if (cp->proto != PPP_LCP)
1277 goto illegal;
1278 /* Discard the packet. */
1279 break;
1280 case ECHO_REQ:
1281 if (cp->proto != PPP_LCP)
1282 goto illegal;
1283 if (sp->state[cp->protoidx] != STATE_OPENED) {
1284 if (debug)
1285 addlog("%s%d: lcp echo req but lcp closed\n",
1286 ifp->if_name, ifp->if_unit);
1287 ++ifp->if_ierrors;
1288 break;
1289 }
1290 if (len < 8) {
1291 if (debug)
1292 addlog("%s%d: invalid lcp echo request "
1293 "packet length: %d bytes\n",
1294 ifp->if_name, ifp->if_unit, len);
1295 break;
1296 }
1297 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1298 /* Line loopback mode detected. */
1299 printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1300 if_down (ifp);
1301 sppp_qflush (&sp->pp_cpq);
1302
1303 /* Shut down the PPP link. */
1304 /* XXX */
1305 lcp.Down(sp);
1306 lcp.Up(sp);
1307 break;
1308 }
1309 *(long*)(h+1) = htonl (sp->lcp.magic);
1310 if (debug)
1311 addlog("%s%d: got lcp echo req, sending echo rep\n",
1312 ifp->if_name, ifp->if_unit);
1313 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1314 break;
1315 case ECHO_REPLY:
1316 if (cp->proto != PPP_LCP)
1317 goto illegal;
1318 if (h->ident != sp->lcp.echoid) {
1319 ++ifp->if_ierrors;
1320 break;
1321 }
1322 if (len < 8) {
1323 if (debug)
1324 addlog("%s%d: lcp invalid echo reply "
1325 "packet length: %d bytes\n",
1326 ifp->if_name, ifp->if_unit, len);
1327 break;
1328 }
1329 if (debug)
1330 addlog("%s%d: lcp got echo rep\n",
1331 ifp->if_name, ifp->if_unit);
1332 if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1333 sp->pp_alivecnt = 0;
1334 break;
1335 default:
1336 /* Unknown packet type -- send Code-Reject packet. */
1337 illegal:
1338 if (debug)
1339 addlog("%s%d: %c send code-rej for 0x%x\n",
1340 ifp->if_name, ifp->if_unit, cp->name, h->type);
1341 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1342 m->m_pkthdr.len, h);
1343 ++ifp->if_ierrors;
1344 }
1345}
1346
1347
1348/*
1349 * The generic part of all Up/Down/Open/Close/TO event handlers.
1350 * Basically, the state transition handling in the automaton.
1351 */
1352static void
1353sppp_up_event(const struct cp *cp, struct sppp *sp)
1354{
1355 STDDCL;
1356
1357 if (debug)
1358 log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1359 ifp->if_name, ifp->if_unit, cp->name,
1360 sppp_state_name(sp->state[cp->protoidx]));
1361
1362 switch (sp->state[cp->protoidx]) {
1363 case STATE_INITIAL:
1364 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1365 break;
1366 case STATE_STARTING:
1367 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1368 (cp->scr)(sp);
1369 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1370 break;
1371 default:
1372 printf("%s%d: %s illegal up in state %s\n",
1373 ifp->if_name, ifp->if_unit, cp->name,
1374 sppp_state_name(sp->state[cp->protoidx]));
1375 }
1376}
1377
1378static void
1379sppp_down_event(const struct cp *cp, struct sppp *sp)
1380{
1381 STDDCL;
1382
1383 if (debug)
1384 log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1385 ifp->if_name, ifp->if_unit, cp->name,
1386 sppp_state_name(sp->state[cp->protoidx]));
1387
1388 switch (sp->state[cp->protoidx]) {
1389 case STATE_CLOSED:
1390 case STATE_CLOSING:
1391 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1392 break;
1393 case STATE_STOPPED:
1394 (cp->tls)(sp);
1395 /* fall through */
1396 case STATE_STOPPING:
1397 case STATE_REQ_SENT:
1398 case STATE_ACK_RCVD:
1399 case STATE_ACK_SENT:
1400 sppp_cp_change_state(cp, sp, STATE_STARTING);
1401 break;
1402 case STATE_OPENED:
1403 (cp->tld)(sp);
1404 sppp_cp_change_state(cp, sp, STATE_STARTING);
1405 break;
1406 default:
1407 printf("%s%d: %s illegal down in state %s\n",
1408 ifp->if_name, ifp->if_unit, cp->name,
1409 sppp_state_name(sp->state[cp->protoidx]));
1410 }
1411}
1412
1413
1414static void
1415sppp_open_event(const struct cp *cp, struct sppp *sp)
1416{
1417 STDDCL;
1418
1419 if (debug)
1420 log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1421 ifp->if_name, ifp->if_unit, cp->name,
1422 sppp_state_name(sp->state[cp->protoidx]));
1423
1424 switch (sp->state[cp->protoidx]) {
1425 case STATE_INITIAL:
1426 (cp->tls)(sp);
1427 sppp_cp_change_state(cp, sp, STATE_STARTING);
1428 break;
1429 case STATE_STARTING:
1430 break;
1431 case STATE_CLOSED:
1432 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1433 (cp->scr)(sp);
1434 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1435 break;
1436 case STATE_STOPPED:
1437 case STATE_STOPPING:
1438 case STATE_REQ_SENT:
1439 case STATE_ACK_RCVD:
1440 case STATE_ACK_SENT:
1441 case STATE_OPENED:
1442 break;
1443 case STATE_CLOSING:
1444 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1445 break;
1446 }
1447}
1448
1449
1450static void
1451sppp_close_event(const struct cp *cp, struct sppp *sp)
1452{
1453 STDDCL;
1454
1455 if (debug)
1456 log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1457 ifp->if_name, ifp->if_unit, cp->name,
1458 sppp_state_name(sp->state[cp->protoidx]));
1459
1460 switch (sp->state[cp->protoidx]) {
1461 case STATE_INITIAL:
1462 case STATE_CLOSED:
1463 case STATE_CLOSING:
1464 break;
1465 case STATE_STARTING:
1466 (cp->tlf)(sp);
1467 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1468 break;
1469 case STATE_STOPPED:
1470 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1471 break;
1472 case STATE_STOPPING:
1473 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1474 break;
1475 case STATE_OPENED:
1476 (cp->tld)(sp);
1477 /* fall through */
1478 case STATE_REQ_SENT:
1479 case STATE_ACK_RCVD:
1480 case STATE_ACK_SENT:
1481 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1482 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1483 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1484 break;
1485 }
1486}
1487
1488static void
1489sppp_to_event(const struct cp *cp, struct sppp *sp)
1490{
1491 STDDCL;
1492 int s;
1493
1494 s = splimp();
1495 if (debug)
1496 log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1497 ifp->if_name, ifp->if_unit, cp->name,
1498 sppp_state_name(sp->state[cp->protoidx]),
1499 sp->rst_counter[cp->protoidx]);
1500
1501 if (--sp->rst_counter[cp->protoidx] < 0)
1502 /* TO- event */
1503 switch (sp->state[cp->protoidx]) {
1504 case STATE_CLOSING:
1505 (cp->tlf)(sp);
1506 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1507 break;
1508 case STATE_STOPPING:
1509 (cp->tlf)(sp);
1510 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1511 break;
1512 case STATE_REQ_SENT:
1513 case STATE_ACK_RCVD:
1514 case STATE_ACK_SENT:
1515 (cp->tlf)(sp);
1516 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1517 break;
1518 }
1519 else
1520 /* TO+ event */
1521 switch (sp->state[cp->protoidx]) {
1522 case STATE_CLOSING:
1523 case STATE_STOPPING:
1524 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1525 0, 0);
1526 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1527 sp->lcp.timeout);
1528 break;
1529 case STATE_REQ_SENT:
1530 case STATE_ACK_RCVD:
1531 (cp->scr)(sp);
1532 /* sppp_cp_change_state() will restart the timer */
1533 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1534 break;
1535 case STATE_ACK_SENT:
1536 (cp->scr)(sp);
1537 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1538 sp->lcp.timeout);
1539 break;
1540 }
1541
1542 splx(s);
1543}
1544
1545/*
1546 * Change the state of a control protocol in the state automaton.
1547 * Takes care of starting/stopping the restart timer.
1548 */
1549void
1550sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1551{
1552 sp->state[cp->protoidx] = newstate;
1553
1554 untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1555 switch (newstate) {
1556 case STATE_INITIAL:
1557 case STATE_STARTING:
1558 case STATE_CLOSED:
1559 case STATE_STOPPED:
1560 case STATE_OPENED:
1561 break;
1562 case STATE_CLOSING:
1563 case STATE_STOPPING:
1564 case STATE_REQ_SENT:
1565 case STATE_ACK_RCVD:
1566 case STATE_ACK_SENT:
1567 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1568 sp->lcp.timeout);
1569 break;
1570 }
1571}
1572 /*
1573 *--------------------------------------------------------------------------*
1574 * *
1575 * The LCP implementation. *
1576 * *
1577 *--------------------------------------------------------------------------*
1578 */
1579static void
1580sppp_lcp_init(struct sppp *sp)
1581{
1582 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1583 sp->lcp.magic = 0;
1584 sp->state[IDX_LCP] = STATE_INITIAL;
1585 sp->fail_counter[IDX_LCP] = 0;
1586 sp->lcp.protos = 0;
1587 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1359 switch (sp->state[cp->protoidx]) {
1360 case STATE_CLOSED:
1361 case STATE_STOPPED:
1362 case STATE_REQ_SENT:
1363 case STATE_ACK_SENT:
1364 case STATE_CLOSING:
1365 case STATE_STOPPING:
1366 case STATE_OPENED:
1367 break;
1368 case STATE_ACK_RCVD:
1369 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1370 break;
1371 default:
1372 printf("%s%d: %s illegal %s in state %s\n",
1373 ifp->if_name, ifp->if_unit, cp->name,
1374 sppp_cp_type_name(h->type),
1375 sppp_state_name(sp->state[cp->protoidx]));
1376 ++ifp->if_ierrors;
1377 }
1378 break;
1379 case DISC_REQ:
1380 if (cp->proto != PPP_LCP)
1381 goto illegal;
1382 /* Discard the packet. */
1383 break;
1384 case ECHO_REQ:
1385 if (cp->proto != PPP_LCP)
1386 goto illegal;
1387 if (sp->state[cp->protoidx] != STATE_OPENED) {
1388 if (debug)
1389 addlog("%s%d: lcp echo req but lcp closed\n",
1390 ifp->if_name, ifp->if_unit);
1391 ++ifp->if_ierrors;
1392 break;
1393 }
1394 if (len < 8) {
1395 if (debug)
1396 addlog("%s%d: invalid lcp echo request "
1397 "packet length: %d bytes\n",
1398 ifp->if_name, ifp->if_unit, len);
1399 break;
1400 }
1401 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1402 /* Line loopback mode detected. */
1403 printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1404 if_down (ifp);
1405 sppp_qflush (&sp->pp_cpq);
1406
1407 /* Shut down the PPP link. */
1408 /* XXX */
1409 lcp.Down(sp);
1410 lcp.Up(sp);
1411 break;
1412 }
1413 *(long*)(h+1) = htonl (sp->lcp.magic);
1414 if (debug)
1415 addlog("%s%d: got lcp echo req, sending echo rep\n",
1416 ifp->if_name, ifp->if_unit);
1417 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1418 break;
1419 case ECHO_REPLY:
1420 if (cp->proto != PPP_LCP)
1421 goto illegal;
1422 if (h->ident != sp->lcp.echoid) {
1423 ++ifp->if_ierrors;
1424 break;
1425 }
1426 if (len < 8) {
1427 if (debug)
1428 addlog("%s%d: lcp invalid echo reply "
1429 "packet length: %d bytes\n",
1430 ifp->if_name, ifp->if_unit, len);
1431 break;
1432 }
1433 if (debug)
1434 addlog("%s%d: lcp got echo rep\n",
1435 ifp->if_name, ifp->if_unit);
1436 if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1437 sp->pp_alivecnt = 0;
1438 break;
1439 default:
1440 /* Unknown packet type -- send Code-Reject packet. */
1441 illegal:
1442 if (debug)
1443 addlog("%s%d: %c send code-rej for 0x%x\n",
1444 ifp->if_name, ifp->if_unit, cp->name, h->type);
1445 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1446 m->m_pkthdr.len, h);
1447 ++ifp->if_ierrors;
1448 }
1449}
1450
1451
1452/*
1453 * The generic part of all Up/Down/Open/Close/TO event handlers.
1454 * Basically, the state transition handling in the automaton.
1455 */
1456static void
1457sppp_up_event(const struct cp *cp, struct sppp *sp)
1458{
1459 STDDCL;
1460
1461 if (debug)
1462 log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1463 ifp->if_name, ifp->if_unit, cp->name,
1464 sppp_state_name(sp->state[cp->protoidx]));
1465
1466 switch (sp->state[cp->protoidx]) {
1467 case STATE_INITIAL:
1468 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1469 break;
1470 case STATE_STARTING:
1471 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1472 (cp->scr)(sp);
1473 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1474 break;
1475 default:
1476 printf("%s%d: %s illegal up in state %s\n",
1477 ifp->if_name, ifp->if_unit, cp->name,
1478 sppp_state_name(sp->state[cp->protoidx]));
1479 }
1480}
1481
1482static void
1483sppp_down_event(const struct cp *cp, struct sppp *sp)
1484{
1485 STDDCL;
1486
1487 if (debug)
1488 log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1489 ifp->if_name, ifp->if_unit, cp->name,
1490 sppp_state_name(sp->state[cp->protoidx]));
1491
1492 switch (sp->state[cp->protoidx]) {
1493 case STATE_CLOSED:
1494 case STATE_CLOSING:
1495 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1496 break;
1497 case STATE_STOPPED:
1498 (cp->tls)(sp);
1499 /* fall through */
1500 case STATE_STOPPING:
1501 case STATE_REQ_SENT:
1502 case STATE_ACK_RCVD:
1503 case STATE_ACK_SENT:
1504 sppp_cp_change_state(cp, sp, STATE_STARTING);
1505 break;
1506 case STATE_OPENED:
1507 (cp->tld)(sp);
1508 sppp_cp_change_state(cp, sp, STATE_STARTING);
1509 break;
1510 default:
1511 printf("%s%d: %s illegal down in state %s\n",
1512 ifp->if_name, ifp->if_unit, cp->name,
1513 sppp_state_name(sp->state[cp->protoidx]));
1514 }
1515}
1516
1517
1518static void
1519sppp_open_event(const struct cp *cp, struct sppp *sp)
1520{
1521 STDDCL;
1522
1523 if (debug)
1524 log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1525 ifp->if_name, ifp->if_unit, cp->name,
1526 sppp_state_name(sp->state[cp->protoidx]));
1527
1528 switch (sp->state[cp->protoidx]) {
1529 case STATE_INITIAL:
1530 (cp->tls)(sp);
1531 sppp_cp_change_state(cp, sp, STATE_STARTING);
1532 break;
1533 case STATE_STARTING:
1534 break;
1535 case STATE_CLOSED:
1536 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1537 (cp->scr)(sp);
1538 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1539 break;
1540 case STATE_STOPPED:
1541 case STATE_STOPPING:
1542 case STATE_REQ_SENT:
1543 case STATE_ACK_RCVD:
1544 case STATE_ACK_SENT:
1545 case STATE_OPENED:
1546 break;
1547 case STATE_CLOSING:
1548 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1549 break;
1550 }
1551}
1552
1553
1554static void
1555sppp_close_event(const struct cp *cp, struct sppp *sp)
1556{
1557 STDDCL;
1558
1559 if (debug)
1560 log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1561 ifp->if_name, ifp->if_unit, cp->name,
1562 sppp_state_name(sp->state[cp->protoidx]));
1563
1564 switch (sp->state[cp->protoidx]) {
1565 case STATE_INITIAL:
1566 case STATE_CLOSED:
1567 case STATE_CLOSING:
1568 break;
1569 case STATE_STARTING:
1570 (cp->tlf)(sp);
1571 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1572 break;
1573 case STATE_STOPPED:
1574 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1575 break;
1576 case STATE_STOPPING:
1577 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1578 break;
1579 case STATE_OPENED:
1580 (cp->tld)(sp);
1581 /* fall through */
1582 case STATE_REQ_SENT:
1583 case STATE_ACK_RCVD:
1584 case STATE_ACK_SENT:
1585 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1586 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1587 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1588 break;
1589 }
1590}
1591
1592static void
1593sppp_to_event(const struct cp *cp, struct sppp *sp)
1594{
1595 STDDCL;
1596 int s;
1597
1598 s = splimp();
1599 if (debug)
1600 log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1601 ifp->if_name, ifp->if_unit, cp->name,
1602 sppp_state_name(sp->state[cp->protoidx]),
1603 sp->rst_counter[cp->protoidx]);
1604
1605 if (--sp->rst_counter[cp->protoidx] < 0)
1606 /* TO- event */
1607 switch (sp->state[cp->protoidx]) {
1608 case STATE_CLOSING:
1609 (cp->tlf)(sp);
1610 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1611 break;
1612 case STATE_STOPPING:
1613 (cp->tlf)(sp);
1614 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1615 break;
1616 case STATE_REQ_SENT:
1617 case STATE_ACK_RCVD:
1618 case STATE_ACK_SENT:
1619 (cp->tlf)(sp);
1620 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1621 break;
1622 }
1623 else
1624 /* TO+ event */
1625 switch (sp->state[cp->protoidx]) {
1626 case STATE_CLOSING:
1627 case STATE_STOPPING:
1628 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1629 0, 0);
1630 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1631 sp->lcp.timeout);
1632 break;
1633 case STATE_REQ_SENT:
1634 case STATE_ACK_RCVD:
1635 (cp->scr)(sp);
1636 /* sppp_cp_change_state() will restart the timer */
1637 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1638 break;
1639 case STATE_ACK_SENT:
1640 (cp->scr)(sp);
1641 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1642 sp->lcp.timeout);
1643 break;
1644 }
1645
1646 splx(s);
1647}
1648
1649/*
1650 * Change the state of a control protocol in the state automaton.
1651 * Takes care of starting/stopping the restart timer.
1652 */
1653void
1654sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1655{
1656 sp->state[cp->protoidx] = newstate;
1657
1658 untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1659 switch (newstate) {
1660 case STATE_INITIAL:
1661 case STATE_STARTING:
1662 case STATE_CLOSED:
1663 case STATE_STOPPED:
1664 case STATE_OPENED:
1665 break;
1666 case STATE_CLOSING:
1667 case STATE_STOPPING:
1668 case STATE_REQ_SENT:
1669 case STATE_ACK_RCVD:
1670 case STATE_ACK_SENT:
1671 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1672 sp->lcp.timeout);
1673 break;
1674 }
1675}
1676 /*
1677 *--------------------------------------------------------------------------*
1678 * *
1679 * The LCP implementation. *
1680 * *
1681 *--------------------------------------------------------------------------*
1682 */
1683static void
1684sppp_lcp_init(struct sppp *sp)
1685{
1686 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1687 sp->lcp.magic = 0;
1688 sp->state[IDX_LCP] = STATE_INITIAL;
1689 sp->fail_counter[IDX_LCP] = 0;
1690 sp->lcp.protos = 0;
1691 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1588 callout_handle_init(&sp->ch[IDX_LCP]);
1589
1692
1590 /*
1591 * Initialize counters and timeout values. Note that we don't
1592 * use the 3 seconds suggested in RFC 1661 since we are likely
1593 * running on a fast link. XXX We should probably implement
1594 * the exponential backoff option. Note that these values are
1595 * relevant for all control protocols, not just LCP only.
1596 */
1597 sp->lcp.timeout = 1 * hz;
1598 sp->lcp.max_terminate = 2;
1599 sp->lcp.max_configure = 10;
1600 sp->lcp.max_failure = 10;
1693 /*
1694 * Initialize counters and timeout values. Note that we don't
1695 * use the 3 seconds suggested in RFC 1661 since we are likely
1696 * running on a fast link. XXX We should probably implement
1697 * the exponential backoff option. Note that these values are
1698 * relevant for all control protocols, not just LCP only.
1699 */
1700 sp->lcp.timeout = 1 * hz;
1701 sp->lcp.max_terminate = 2;
1702 sp->lcp.max_configure = 10;
1703 sp->lcp.max_failure = 10;
1704 callout_handle_init(&sp->ch[IDX_LCP]);
1601}
1602
1603static void
1604sppp_lcp_up(struct sppp *sp)
1605{
1606 STDDCL;
1607
1608 /*
1705}
1706
1707static void
1708sppp_lcp_up(struct sppp *sp)
1709{
1710 STDDCL;
1711
1712 /*
1609 * If this interface is passive or dial-on-demand, it means
1610 * we've got in incoming call. Activate the interface.
1713 * If this interface is passive or dial-on-demand, and we are
1714 * still in Initial state, it means we've got an incoming
1715 * call. Activate the interface.
1611 */
1612 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1613 if (debug)
1614 log(LOG_DEBUG,
1716 */
1717 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1718 if (debug)
1719 log(LOG_DEBUG,
1615 "%s%d: Up event (incoming call)\n",
1616 ifp->if_name, ifp->if_unit);
1720 "%s%d: Up event", ifp->if_name, ifp->if_unit);
1617 ifp->if_flags |= IFF_RUNNING;
1721 ifp->if_flags |= IFF_RUNNING;
1618 lcp.Open(sp);
1722 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1723 if (debug)
1724 addlog("(incoming call)\n");
1725 sp->pp_flags |= PP_CALLIN;
1726 lcp.Open(sp);
1727 } else if (debug)
1728 addlog("\n");
1619 }
1620
1621 sppp_up_event(&lcp, sp);
1622}
1623
1624static void
1625sppp_lcp_down(struct sppp *sp)
1626{
1627 STDDCL;
1628
1629 sppp_down_event(&lcp, sp);
1630
1631 /*
1632 * If this is neither a dial-on-demand nor a passive
1633 * interface, simulate an ``ifconfig down'' action, so the
1634 * administrator can force a redial by another ``ifconfig
1635 * up''. XXX For leased line operation, should we immediately
1636 * try to reopen the connection here?
1637 */
1638 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1639 log(LOG_INFO,
1640 "%s%d: Down event (carrier loss), taking interface down.\n",
1641 ifp->if_name, ifp->if_unit);
1642 if_down(ifp);
1643 } else {
1644 if (debug)
1645 log(LOG_DEBUG,
1646 "%s%d: Down event (carrier loss)\n",
1647 ifp->if_name, ifp->if_unit);
1648 }
1729 }
1730
1731 sppp_up_event(&lcp, sp);
1732}
1733
1734static void
1735sppp_lcp_down(struct sppp *sp)
1736{
1737 STDDCL;
1738
1739 sppp_down_event(&lcp, sp);
1740
1741 /*
1742 * If this is neither a dial-on-demand nor a passive
1743 * interface, simulate an ``ifconfig down'' action, so the
1744 * administrator can force a redial by another ``ifconfig
1745 * up''. XXX For leased line operation, should we immediately
1746 * try to reopen the connection here?
1747 */
1748 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1749 log(LOG_INFO,
1750 "%s%d: Down event (carrier loss), taking interface down.\n",
1751 ifp->if_name, ifp->if_unit);
1752 if_down(ifp);
1753 } else {
1754 if (debug)
1755 log(LOG_DEBUG,
1756 "%s%d: Down event (carrier loss)\n",
1757 ifp->if_name, ifp->if_unit);
1758 }
1649 lcp.Close(sp);
1759 sp->pp_flags &= ~PP_CALLIN;
1760 if (sp->state[IDX_LCP] != STATE_INITIAL)
1761 lcp.Close(sp);
1650 ifp->if_flags &= ~IFF_RUNNING;
1651}
1652
1653static void
1654sppp_lcp_open(struct sppp *sp)
1655{
1762 ifp->if_flags &= ~IFF_RUNNING;
1763}
1764
1765static void
1766sppp_lcp_open(struct sppp *sp)
1767{
1768 /*
1769 * If we are authenticator, negotiate LCP_AUTH
1770 */
1771 if (sp->hisauth.proto != 0)
1772 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1773 else
1774 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1775 sp->pp_flags &= ~PP_NEEDAUTH;
1656 sppp_open_event(&lcp, sp);
1657}
1658
1659static void
1660sppp_lcp_close(struct sppp *sp)
1661{
1662 sppp_close_event(&lcp, sp);
1663}
1664
1665static void
1666sppp_lcp_TO(void *cookie)
1667{
1668 sppp_to_event(&lcp, (struct sppp *)cookie);
1669}
1670
1671/*
1672 * Analyze a configure request. Return true if it was agreeable, and
1673 * caused action sca, false if it has been rejected or nak'ed, and
1674 * caused action scn. (The return value is used to make the state
1675 * transition decision in the state automaton.)
1676 */
1677static int
1678sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1679{
1680 STDDCL;
1681 u_char *buf, *r, *p;
1682 int origlen, rlen;
1683 u_long nmagic;
1776 sppp_open_event(&lcp, sp);
1777}
1778
1779static void
1780sppp_lcp_close(struct sppp *sp)
1781{
1782 sppp_close_event(&lcp, sp);
1783}
1784
1785static void
1786sppp_lcp_TO(void *cookie)
1787{
1788 sppp_to_event(&lcp, (struct sppp *)cookie);
1789}
1790
1791/*
1792 * Analyze a configure request. Return true if it was agreeable, and
1793 * caused action sca, false if it has been rejected or nak'ed, and
1794 * caused action scn. (The return value is used to make the state
1795 * transition decision in the state automaton.)
1796 */
1797static int
1798sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1799{
1800 STDDCL;
1801 u_char *buf, *r, *p;
1802 int origlen, rlen;
1803 u_long nmagic;
1804 u_short authproto;
1684
1685 len -= 4;
1686 origlen = len;
1687 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1688 if (! buf)
1689 return (0);
1690
1691 if (debug)
1692 log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1693 ifp->if_name, ifp->if_unit);
1694
1695 /* pass 1: check for things that need to be rejected */
1696 p = (void*) (h+1);
1697 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1698 if (debug)
1699 addlog(" %s ", sppp_lcp_opt_name(*p));
1700 switch (*p) {
1701 case LCP_OPT_MAGIC:
1702 /* Magic number. */
1703 /* fall through, both are same length */
1704 case LCP_OPT_ASYNC_MAP:
1705 /* Async control character map. */
1706 if (len >= 6 || p[1] == 6)
1707 continue;
1708 if (debug)
1709 addlog("[invalid] ");
1710 break;
1711 case LCP_OPT_MRU:
1712 /* Maximum receive unit. */
1713 if (len >= 4 && p[1] == 4)
1714 continue;
1715 if (debug)
1716 addlog("[invalid] ");
1717 break;
1805
1806 len -= 4;
1807 origlen = len;
1808 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1809 if (! buf)
1810 return (0);
1811
1812 if (debug)
1813 log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1814 ifp->if_name, ifp->if_unit);
1815
1816 /* pass 1: check for things that need to be rejected */
1817 p = (void*) (h+1);
1818 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1819 if (debug)
1820 addlog(" %s ", sppp_lcp_opt_name(*p));
1821 switch (*p) {
1822 case LCP_OPT_MAGIC:
1823 /* Magic number. */
1824 /* fall through, both are same length */
1825 case LCP_OPT_ASYNC_MAP:
1826 /* Async control character map. */
1827 if (len >= 6 || p[1] == 6)
1828 continue;
1829 if (debug)
1830 addlog("[invalid] ");
1831 break;
1832 case LCP_OPT_MRU:
1833 /* Maximum receive unit. */
1834 if (len >= 4 && p[1] == 4)
1835 continue;
1836 if (debug)
1837 addlog("[invalid] ");
1838 break;
1839 case LCP_OPT_AUTH_PROTO:
1840 if (len < 4) {
1841 if (debug)
1842 addlog("[invalid] ");
1843 break;
1844 }
1845 authproto = (p[2] << 8) + p[3];
1846 if (authproto == PPP_CHAP && p[1] != 5) {
1847 if (debug)
1848 addlog("[invalid chap len] ");
1849 break;
1850 }
1851 if (sp->myauth.proto == 0) {
1852 /* we are not configured to do auth */
1853 if (debug)
1854 addlog("[not configured] ");
1855 break;
1856 }
1857 /*
1858 * Remote want us to authenticate, remember this,
1859 * so we stay in PHASE_AUTHENTICATE after LCP got
1860 * up.
1861 */
1862 sp->pp_flags |= PP_NEEDAUTH;
1863 continue;
1718 default:
1719 /* Others not supported. */
1720 if (debug)
1721 addlog("[rej] ");
1722 break;
1723 }
1724 /* Add the option to rejected list. */
1725 bcopy (p, r, p[1]);
1726 r += p[1];
1727 rlen += p[1];
1728 }
1729 if (rlen) {
1730 if (debug)
1731 addlog(" send conf-rej\n");
1732 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1733 return 0;
1734 } else if (debug)
1735 addlog("\n");
1736
1737 /*
1738 * pass 2: check for option values that are unacceptable and
1739 * thus require to be nak'ed.
1740 */
1741 if (debug)
1742 log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1743 ifp->if_name, ifp->if_unit);
1744
1745 p = (void*) (h+1);
1746 len = origlen;
1747 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1748 if (debug)
1749 addlog(" %s ", sppp_lcp_opt_name(*p));
1750 switch (*p) {
1751 case LCP_OPT_MAGIC:
1752 /* Magic number -- extract. */
1753 nmagic = (u_long)p[2] << 24 |
1754 (u_long)p[3] << 16 | p[4] << 8 | p[5];
1755 if (nmagic != sp->lcp.magic) {
1756 if (debug)
1757 addlog("0x%x ", nmagic);
1758 continue;
1759 }
1760 /*
1761 * Local and remote magics equal -- loopback?
1762 */
1763 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1864 default:
1865 /* Others not supported. */
1866 if (debug)
1867 addlog("[rej] ");
1868 break;
1869 }
1870 /* Add the option to rejected list. */
1871 bcopy (p, r, p[1]);
1872 r += p[1];
1873 rlen += p[1];
1874 }
1875 if (rlen) {
1876 if (debug)
1877 addlog(" send conf-rej\n");
1878 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1879 return 0;
1880 } else if (debug)
1881 addlog("\n");
1882
1883 /*
1884 * pass 2: check for option values that are unacceptable and
1885 * thus require to be nak'ed.
1886 */
1887 if (debug)
1888 log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1889 ifp->if_name, ifp->if_unit);
1890
1891 p = (void*) (h+1);
1892 len = origlen;
1893 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1894 if (debug)
1895 addlog(" %s ", sppp_lcp_opt_name(*p));
1896 switch (*p) {
1897 case LCP_OPT_MAGIC:
1898 /* Magic number -- extract. */
1899 nmagic = (u_long)p[2] << 24 |
1900 (u_long)p[3] << 16 | p[4] << 8 | p[5];
1901 if (nmagic != sp->lcp.magic) {
1902 if (debug)
1903 addlog("0x%x ", nmagic);
1904 continue;
1905 }
1906 /*
1907 * Local and remote magics equal -- loopback?
1908 */
1909 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1764 printf ("\n%s%d: loopback\n",
1910 printf ("%s%d: loopback\n",
1765 ifp->if_name, ifp->if_unit);
1766 sp->pp_loopcnt = 0;
1767 if (ifp->if_flags & IFF_UP) {
1768 if_down(ifp);
1769 sppp_qflush(&sp->pp_cpq);
1770 /* XXX ? */
1771 lcp.Down(sp);
1772 lcp.Up(sp);
1773 }
1774 } else if (debug)
1775 addlog("[glitch] ");
1776 ++sp->pp_loopcnt;
1777 /*
1778 * We negate our magic here, and NAK it. If
1779 * we see it later in an NAK packet, we
1780 * suggest a new one.
1781 */
1782 nmagic = ~sp->lcp.magic;
1783 /* Gonna NAK it. */
1784 p[2] = nmagic >> 24;
1785 p[3] = nmagic >> 16;
1786 p[4] = nmagic >> 8;
1787 p[5] = nmagic;
1788 break;
1789
1790 case LCP_OPT_ASYNC_MAP:
1791 /* Async control character map -- check to be zero. */
1792 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1793 if (debug)
1794 addlog("[empty] ");
1795 continue;
1796 }
1797 if (debug)
1798 addlog("[non-empty] ");
1799 /* suggest a zero one */
1800 p[2] = p[3] = p[4] = p[5] = 0;
1801 break;
1802
1803 case LCP_OPT_MRU:
1804 /*
1805 * Maximum receive unit. Always agreeable,
1806 * but ignored by now.
1807 */
1808 sp->lcp.their_mru = p[2] * 256 + p[3];
1809 if (debug)
1810 addlog("%d ", sp->lcp.their_mru);
1811 continue;
1911 ifp->if_name, ifp->if_unit);
1912 sp->pp_loopcnt = 0;
1913 if (ifp->if_flags & IFF_UP) {
1914 if_down(ifp);
1915 sppp_qflush(&sp->pp_cpq);
1916 /* XXX ? */
1917 lcp.Down(sp);
1918 lcp.Up(sp);
1919 }
1920 } else if (debug)
1921 addlog("[glitch] ");
1922 ++sp->pp_loopcnt;
1923 /*
1924 * We negate our magic here, and NAK it. If
1925 * we see it later in an NAK packet, we
1926 * suggest a new one.
1927 */
1928 nmagic = ~sp->lcp.magic;
1929 /* Gonna NAK it. */
1930 p[2] = nmagic >> 24;
1931 p[3] = nmagic >> 16;
1932 p[4] = nmagic >> 8;
1933 p[5] = nmagic;
1934 break;
1935
1936 case LCP_OPT_ASYNC_MAP:
1937 /* Async control character map -- check to be zero. */
1938 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1939 if (debug)
1940 addlog("[empty] ");
1941 continue;
1942 }
1943 if (debug)
1944 addlog("[non-empty] ");
1945 /* suggest a zero one */
1946 p[2] = p[3] = p[4] = p[5] = 0;
1947 break;
1948
1949 case LCP_OPT_MRU:
1950 /*
1951 * Maximum receive unit. Always agreeable,
1952 * but ignored by now.
1953 */
1954 sp->lcp.their_mru = p[2] * 256 + p[3];
1955 if (debug)
1956 addlog("%d ", sp->lcp.their_mru);
1957 continue;
1958
1959 case LCP_OPT_AUTH_PROTO:
1960 authproto = (p[2] << 8) + p[3];
1961 if (sp->myauth.proto != authproto) {
1962 /* not agreed, nak */
1963 if (debug)
1964 addlog("[mine %s != his %s] ",
1965 sppp_proto_name(sp->hisauth.proto),
1966 sppp_proto_name(authproto));
1967 p[2] = sp->myauth.proto >> 8;
1968 p[3] = sp->myauth.proto;
1969 break;
1970 }
1971 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
1972 if (debug)
1973 addlog("[chap not MD5] ");
1974 p[4] == CHAP_MD5;
1975 break;
1976 }
1977 continue;
1812 }
1813 /* Add the option to nak'ed list. */
1814 bcopy (p, r, p[1]);
1815 r += p[1];
1816 rlen += p[1];
1817 }
1818 if (rlen) {
1819 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1820 if (debug)
1821 addlog(" max_failure (%d) exceeded, "
1822 "send conf-rej\n",
1823 sp->lcp.max_failure);
1824 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1825 } else {
1826 if (debug)
1827 addlog(" send conf-nak\n");
1828 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1829 }
1830 return 0;
1831 } else {
1832 if (debug)
1833 addlog(" send conf-ack\n");
1834 sp->fail_counter[IDX_LCP] = 0;
1835 sp->pp_loopcnt = 0;
1836 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
1837 h->ident, origlen, h+1);
1838 }
1839
1840 free (buf, M_TEMP);
1841 return (rlen == 0);
1842}
1843
1844/*
1845 * Analyze the LCP Configure-Reject option list, and adjust our
1846 * negotiation.
1847 */
1848static void
1849sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
1850{
1851 STDDCL;
1852 u_char *buf, *p;
1853
1854 len -= 4;
1855 buf = malloc (len, M_TEMP, M_NOWAIT);
1856 if (!buf)
1857 return;
1858
1859 if (debug)
1860 log(LOG_DEBUG, "%s%d: lcp rej opts: ",
1861 ifp->if_name, ifp->if_unit);
1862
1863 p = (void*) (h+1);
1864 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1865 if (debug)
1866 addlog(" %s ", sppp_lcp_opt_name(*p));
1867 switch (*p) {
1868 case LCP_OPT_MAGIC:
1869 /* Magic number -- can't use it, use 0 */
1870 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
1871 sp->lcp.magic = 0;
1872 break;
1873 case LCP_OPT_MRU:
1874 /*
1875 * Should not be rejected anyway, since we only
1876 * negotiate a MRU if explicitly requested by
1877 * peer.
1878 */
1879 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
1880 break;
1978 }
1979 /* Add the option to nak'ed list. */
1980 bcopy (p, r, p[1]);
1981 r += p[1];
1982 rlen += p[1];
1983 }
1984 if (rlen) {
1985 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1986 if (debug)
1987 addlog(" max_failure (%d) exceeded, "
1988 "send conf-rej\n",
1989 sp->lcp.max_failure);
1990 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1991 } else {
1992 if (debug)
1993 addlog(" send conf-nak\n");
1994 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1995 }
1996 return 0;
1997 } else {
1998 if (debug)
1999 addlog(" send conf-ack\n");
2000 sp->fail_counter[IDX_LCP] = 0;
2001 sp->pp_loopcnt = 0;
2002 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2003 h->ident, origlen, h+1);
2004 }
2005
2006 free (buf, M_TEMP);
2007 return (rlen == 0);
2008}
2009
2010/*
2011 * Analyze the LCP Configure-Reject option list, and adjust our
2012 * negotiation.
2013 */
2014static void
2015sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2016{
2017 STDDCL;
2018 u_char *buf, *p;
2019
2020 len -= 4;
2021 buf = malloc (len, M_TEMP, M_NOWAIT);
2022 if (!buf)
2023 return;
2024
2025 if (debug)
2026 log(LOG_DEBUG, "%s%d: lcp rej opts: ",
2027 ifp->if_name, ifp->if_unit);
2028
2029 p = (void*) (h+1);
2030 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2031 if (debug)
2032 addlog(" %s ", sppp_lcp_opt_name(*p));
2033 switch (*p) {
2034 case LCP_OPT_MAGIC:
2035 /* Magic number -- can't use it, use 0 */
2036 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2037 sp->lcp.magic = 0;
2038 break;
2039 case LCP_OPT_MRU:
2040 /*
2041 * Should not be rejected anyway, since we only
2042 * negotiate a MRU if explicitly requested by
2043 * peer.
2044 */
2045 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2046 break;
2047 case LCP_OPT_AUTH_PROTO:
2048 /*
2049 * Peer doesn't want to authenticate himself,
2050 * deny unless this is a dialout call, and
2051 * AUTHFLAG_NOCALLOUT is set.
2052 */
2053 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2054 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2055 if (debug)
2056 addlog("[don't insist on auth "
2057 "for callout]");
2058 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2059 break;
2060 }
2061 if (debug)
2062 addlog("[access denied]\n");
2063 lcp.Close(sp);
2064 break;
1881 }
1882 }
1883 if (debug)
1884 addlog("\n");
1885 free (buf, M_TEMP);
1886 return;
1887}
1888
1889/*
1890 * Analyze the LCP Configure-NAK option list, and adjust our
1891 * negotiation.
1892 */
1893static void
1894sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
1895{
1896 STDDCL;
1897 u_char *buf, *p;
1898 u_long magic;
1899
1900 len -= 4;
1901 buf = malloc (len, M_TEMP, M_NOWAIT);
1902 if (!buf)
1903 return;
1904
1905 if (debug)
1906 log(LOG_DEBUG, "%s%d: lcp nak opts: ",
1907 ifp->if_name, ifp->if_unit);
1908
1909 p = (void*) (h+1);
1910 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1911 if (debug)
1912 addlog(" %s ", sppp_lcp_opt_name(*p));
1913 switch (*p) {
1914 case LCP_OPT_MAGIC:
1915 /* Magic number -- renegotiate */
1916 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1917 len >= 6 && p[1] == 6) {
1918 magic = (u_long)p[2] << 24 |
1919 (u_long)p[3] << 16 | p[4] << 8 | p[5];
1920 /*
1921 * If the remote magic is our negated one,
1922 * this looks like a loopback problem.
1923 * Suggest a new magic to make sure.
1924 */
1925 if (magic == ~sp->lcp.magic) {
1926 if (debug)
1927 addlog("magic glitch ");
1928 sp->lcp.magic += time.tv_sec + time.tv_usec;
1929 } else {
1930 sp->lcp.magic = magic;
1931 if (debug)
1932 addlog("%d ");
1933 }
1934 }
1935 break;
1936 case LCP_OPT_MRU:
1937 /*
1938 * Peer wants to advise us to negotiate an MRU.
1939 * Agree on it if it's reasonable, or use
1940 * default otherwise.
1941 */
1942 if (len >= 4 && p[1] == 4) {
1943 u_int mru = p[2] * 256 + p[3];
1944 if (debug)
1945 addlog("%d ", mru);
1946 if (mru < PP_MTU || mru > PP_MAX_MRU)
1947 mru = PP_MTU;
1948 sp->lcp.mru = mru;
1949 sp->lcp.opts |= (1 << LCP_OPT_MRU);
1950 }
1951 break;
2065 }
2066 }
2067 if (debug)
2068 addlog("\n");
2069 free (buf, M_TEMP);
2070 return;
2071}
2072
2073/*
2074 * Analyze the LCP Configure-NAK option list, and adjust our
2075 * negotiation.
2076 */
2077static void
2078sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2079{
2080 STDDCL;
2081 u_char *buf, *p;
2082 u_long magic;
2083
2084 len -= 4;
2085 buf = malloc (len, M_TEMP, M_NOWAIT);
2086 if (!buf)
2087 return;
2088
2089 if (debug)
2090 log(LOG_DEBUG, "%s%d: lcp nak opts: ",
2091 ifp->if_name, ifp->if_unit);
2092
2093 p = (void*) (h+1);
2094 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2095 if (debug)
2096 addlog(" %s ", sppp_lcp_opt_name(*p));
2097 switch (*p) {
2098 case LCP_OPT_MAGIC:
2099 /* Magic number -- renegotiate */
2100 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2101 len >= 6 && p[1] == 6) {
2102 magic = (u_long)p[2] << 24 |
2103 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2104 /*
2105 * If the remote magic is our negated one,
2106 * this looks like a loopback problem.
2107 * Suggest a new magic to make sure.
2108 */
2109 if (magic == ~sp->lcp.magic) {
2110 if (debug)
2111 addlog("magic glitch ");
2112 sp->lcp.magic += time.tv_sec + time.tv_usec;
2113 } else {
2114 sp->lcp.magic = magic;
2115 if (debug)
2116 addlog("%d ");
2117 }
2118 }
2119 break;
2120 case LCP_OPT_MRU:
2121 /*
2122 * Peer wants to advise us to negotiate an MRU.
2123 * Agree on it if it's reasonable, or use
2124 * default otherwise.
2125 */
2126 if (len >= 4 && p[1] == 4) {
2127 u_int mru = p[2] * 256 + p[3];
2128 if (debug)
2129 addlog("%d ", mru);
2130 if (mru < PP_MTU || mru > PP_MAX_MRU)
2131 mru = PP_MTU;
2132 sp->lcp.mru = mru;
2133 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2134 }
2135 break;
2136 case LCP_OPT_AUTH_PROTO:
2137 /*
2138 * Peer doesn't like our authentication method,
2139 * deny.
2140 */
2141 if (debug)
2142 addlog("[access denied]\n");
2143 lcp.Close(sp);
2144 break;
1952 }
1953 }
1954 if (debug)
1955 addlog("\n");
1956 free (buf, M_TEMP);
1957 return;
1958}
1959
1960static void
1961sppp_lcp_tlu(struct sppp *sp)
1962{
1963 STDDCL;
1964 int i;
1965 u_long mask;
1966
1967 /* XXX ? */
1968 if (! (ifp->if_flags & IFF_UP) &&
1969 (ifp->if_flags & IFF_RUNNING)) {
1970 /* Coming out of loopback mode. */
1971 if_up(ifp);
1972 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
1973 }
1974
1975 for (i = 0; i < IDX_COUNT; i++)
1976 if ((cps[i])->flags & CP_QUAL)
1977 (cps[i])->Open(sp);
1978
2145 }
2146 }
2147 if (debug)
2148 addlog("\n");
2149 free (buf, M_TEMP);
2150 return;
2151}
2152
2153static void
2154sppp_lcp_tlu(struct sppp *sp)
2155{
2156 STDDCL;
2157 int i;
2158 u_long mask;
2159
2160 /* XXX ? */
2161 if (! (ifp->if_flags & IFF_UP) &&
2162 (ifp->if_flags & IFF_RUNNING)) {
2163 /* Coming out of loopback mode. */
2164 if_up(ifp);
2165 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
2166 }
2167
2168 for (i = 0; i < IDX_COUNT; i++)
2169 if ((cps[i])->flags & CP_QUAL)
2170 (cps[i])->Open(sp);
2171
1979 if (/* require authentication XXX */ 0)
2172 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2173 (sp->pp_flags & PP_NEEDAUTH) != 0)
1980 sp->pp_phase = PHASE_AUTHENTICATE;
1981 else
1982 sp->pp_phase = PHASE_NETWORK;
1983
1984 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
1985 sppp_phase_name(sp->pp_phase));
1986
2174 sp->pp_phase = PHASE_AUTHENTICATE;
2175 else
2176 sp->pp_phase = PHASE_NETWORK;
2177
2178 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2179 sppp_phase_name(sp->pp_phase));
2180
1987 if (sp->pp_phase == PHASE_AUTHENTICATE) {
1988 for (i = 0; i < IDX_COUNT; i++)
1989 if ((cps[i])->flags & CP_AUTH)
1990 (cps[i])->Open(sp);
1991 } else {
2181 /*
2182 * Open all authentication protocols. This is even required
2183 * if we already proceeded to network phase, since it might be
2184 * that remote wants us to authenticate, so we might have to
2185 * send a PAP request. Undesired authentication protocols
2186 * don't do anything when they get an Open event.
2187 */
2188 for (i = 0; i < IDX_COUNT; i++)
2189 if ((cps[i])->flags & CP_AUTH)
2190 (cps[i])->Open(sp);
2191
2192 if (sp->pp_phase == PHASE_NETWORK) {
1992 /* Notify all NCPs. */
1993 for (i = 0; i < IDX_COUNT; i++)
1994 if ((cps[i])->flags & CP_NCP)
1995 (cps[i])->Open(sp);
1996 }
1997
1998 /* Send Up events to all started protos. */
1999 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2000 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2001 (cps[i])->Up(sp);
2002
2003 if (sp->pp_phase == PHASE_NETWORK)
2004 /* if no NCP is starting, close down */
2193 /* Notify all NCPs. */
2194 for (i = 0; i < IDX_COUNT; i++)
2195 if ((cps[i])->flags & CP_NCP)
2196 (cps[i])->Open(sp);
2197 }
2198
2199 /* Send Up events to all started protos. */
2200 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2201 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2202 (cps[i])->Up(sp);
2203
2204 if (sp->pp_phase == PHASE_NETWORK)
2205 /* if no NCP is starting, close down */
2005 sppp_lcp_check(sp);
2206 sppp_lcp_check_and_close(sp);
2006}
2007
2008static void
2009sppp_lcp_tld(struct sppp *sp)
2010{
2011 STDDCL;
2012 int i;
2013 u_long mask;
2014
2015 sp->pp_phase = PHASE_TERMINATE;
2016
2017 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2018 sppp_phase_name(sp->pp_phase));
2019
2020 /*
2021 * Take upper layers down. We send the Down event first and
2022 * the Close second to prevent the upper layers from sending
2023 * ``a flurry of terminate-request packets'', as the RFC
2024 * describes it.
2025 */
2026 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2027 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2028 (cps[i])->Down(sp);
2029 (cps[i])->Close(sp);
2030 }
2031}
2032
2033static void
2034sppp_lcp_tls(struct sppp *sp)
2035{
2036 STDDCL;
2037
2038 sp->pp_phase = PHASE_ESTABLISH;
2039
2040 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2041 sppp_phase_name(sp->pp_phase));
2042
2043 /* Notify lower layer if desired. */
2044 if (sp->pp_tls)
2045 (sp->pp_tls)(sp);
2046}
2047
2048static void
2049sppp_lcp_tlf(struct sppp *sp)
2050{
2051 STDDCL;
2052
2053 sp->pp_phase = PHASE_DEAD;
2054 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2055 sppp_phase_name(sp->pp_phase));
2056
2057 /* Notify lower layer if desired. */
2058 if (sp->pp_tlf)
2059 (sp->pp_tlf)(sp);
2060}
2061
2062static void
2063sppp_lcp_scr(struct sppp *sp)
2064{
2207}
2208
2209static void
2210sppp_lcp_tld(struct sppp *sp)
2211{
2212 STDDCL;
2213 int i;
2214 u_long mask;
2215
2216 sp->pp_phase = PHASE_TERMINATE;
2217
2218 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2219 sppp_phase_name(sp->pp_phase));
2220
2221 /*
2222 * Take upper layers down. We send the Down event first and
2223 * the Close second to prevent the upper layers from sending
2224 * ``a flurry of terminate-request packets'', as the RFC
2225 * describes it.
2226 */
2227 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2228 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2229 (cps[i])->Down(sp);
2230 (cps[i])->Close(sp);
2231 }
2232}
2233
2234static void
2235sppp_lcp_tls(struct sppp *sp)
2236{
2237 STDDCL;
2238
2239 sp->pp_phase = PHASE_ESTABLISH;
2240
2241 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2242 sppp_phase_name(sp->pp_phase));
2243
2244 /* Notify lower layer if desired. */
2245 if (sp->pp_tls)
2246 (sp->pp_tls)(sp);
2247}
2248
2249static void
2250sppp_lcp_tlf(struct sppp *sp)
2251{
2252 STDDCL;
2253
2254 sp->pp_phase = PHASE_DEAD;
2255 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2256 sppp_phase_name(sp->pp_phase));
2257
2258 /* Notify lower layer if desired. */
2259 if (sp->pp_tlf)
2260 (sp->pp_tlf)(sp);
2261}
2262
2263static void
2264sppp_lcp_scr(struct sppp *sp)
2265{
2065 char opt[6 /* magicnum */ + 4 /* mru */];
2266 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2066 int i = 0;
2267 int i = 0;
2268 u_short authproto;
2067
2068 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2069 if (! sp->lcp.magic)
2070 sp->lcp.magic = time.tv_sec + time.tv_usec;
2071 opt[i++] = LCP_OPT_MAGIC;
2072 opt[i++] = 6;
2073 opt[i++] = sp->lcp.magic >> 24;
2074 opt[i++] = sp->lcp.magic >> 16;
2075 opt[i++] = sp->lcp.magic >> 8;
2076 opt[i++] = sp->lcp.magic;
2077 }
2078
2079 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2080 opt[i++] = LCP_OPT_MRU;
2081 opt[i++] = 4;
2082 opt[i++] = sp->lcp.mru >> 8;
2083 opt[i++] = sp->lcp.mru;
2084 }
2085
2269
2270 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2271 if (! sp->lcp.magic)
2272 sp->lcp.magic = time.tv_sec + time.tv_usec;
2273 opt[i++] = LCP_OPT_MAGIC;
2274 opt[i++] = 6;
2275 opt[i++] = sp->lcp.magic >> 24;
2276 opt[i++] = sp->lcp.magic >> 16;
2277 opt[i++] = sp->lcp.magic >> 8;
2278 opt[i++] = sp->lcp.magic;
2279 }
2280
2281 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2282 opt[i++] = LCP_OPT_MRU;
2283 opt[i++] = 4;
2284 opt[i++] = sp->lcp.mru >> 8;
2285 opt[i++] = sp->lcp.mru;
2286 }
2287
2288 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2289 authproto = sp->hisauth.proto;
2290 opt[i++] = LCP_OPT_AUTH_PROTO;
2291 opt[i++] = authproto == PPP_CHAP? 5: 4;
2292 opt[i++] = authproto >> 8;
2293 opt[i++] = authproto;
2294 if (authproto == PPP_CHAP)
2295 opt[i++] = CHAP_MD5;
2296 }
2297
2086 sp->confid[IDX_LCP] = ++sp->pp_seq;
2087 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2088}
2089
2090/*
2298 sp->confid[IDX_LCP] = ++sp->pp_seq;
2299 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2300}
2301
2302/*
2091 * Re-check the open NCPs and see if we should terminate the link.
2092 * Called by the NCPs during their tlf action handling.
2303 * Check the open NCPs, return true if at least one NCP is open.
2093 */
2304 */
2094static void
2095sppp_lcp_check(struct sppp *sp)
2305static int
2306sppp_ncp_check(struct sppp *sp)
2096{
2097 int i, mask;
2098
2099 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2100 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2307{
2308 int i, mask;
2309
2310 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2311 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2101 return;
2312 return 1;
2313 return 0;
2314}
2315
2316/*
2317 * Re-check the open NCPs and see if we should terminate the link.
2318 * Called by the NCPs during their tlf action handling.
2319 */
2320static void
2321sppp_lcp_check_and_close(struct sppp *sp)
2322{
2323
2324 if (sp->pp_phase < PHASE_NETWORK)
2325 /* don't bother, we are already going down */
2326 return;
2327
2328 if (sppp_ncp_check(sp))
2329 return;
2330
2102 lcp.Close(sp);
2103}
2104 /*
2105 *--------------------------------------------------------------------------*
2106 * *
2107 * The IPCP implementation. *
2108 * *
2109 *--------------------------------------------------------------------------*
2110 */
2111
2112static void
2113sppp_ipcp_init(struct sppp *sp)
2114{
2115 sp->ipcp.opts = 0;
2116 sp->ipcp.flags = 0;
2117 sp->state[IDX_IPCP] = STATE_INITIAL;
2118 sp->fail_counter[IDX_IPCP] = 0;
2119 callout_handle_init(&sp->ch[IDX_IPCP]);
2120}
2121
2122static void
2123sppp_ipcp_up(struct sppp *sp)
2124{
2125 sppp_up_event(&ipcp, sp);
2126}
2127
2128static void
2129sppp_ipcp_down(struct sppp *sp)
2130{
2131 sppp_down_event(&ipcp, sp);
2132}
2133
2134static void
2135sppp_ipcp_open(struct sppp *sp)
2136{
2137 STDDCL;
2138 u_long myaddr, hisaddr;
2139
2331 lcp.Close(sp);
2332}
2333 /*
2334 *--------------------------------------------------------------------------*
2335 * *
2336 * The IPCP implementation. *
2337 * *
2338 *--------------------------------------------------------------------------*
2339 */
2340
2341static void
2342sppp_ipcp_init(struct sppp *sp)
2343{
2344 sp->ipcp.opts = 0;
2345 sp->ipcp.flags = 0;
2346 sp->state[IDX_IPCP] = STATE_INITIAL;
2347 sp->fail_counter[IDX_IPCP] = 0;
2348 callout_handle_init(&sp->ch[IDX_IPCP]);
2349}
2350
2351static void
2352sppp_ipcp_up(struct sppp *sp)
2353{
2354 sppp_up_event(&ipcp, sp);
2355}
2356
2357static void
2358sppp_ipcp_down(struct sppp *sp)
2359{
2360 sppp_down_event(&ipcp, sp);
2361}
2362
2363static void
2364sppp_ipcp_open(struct sppp *sp)
2365{
2366 STDDCL;
2367 u_long myaddr, hisaddr;
2368
2140 sppp_get_ip_addrs(sp, &myaddr, &hisaddr);
2369 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2141 /*
2142 * If we don't have his address, this probably means our
2143 * interface doesn't want to talk IP at all. (This could
2144 * be the case if somebody wants to speak only IPX, for
2145 * example.) Don't open IPCP in this case.
2146 */
2147 if (hisaddr == 0L) {
2148 /* XXX this message should go away */
2149 if (debug)
2150 log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2151 ifp->if_name, ifp->if_unit);
2152 return;
2153 }
2154
2155 if (myaddr == 0L) {
2156 /*
2157 * I don't have an assigned address, so i need to
2158 * negotiate my address.
2159 */
2160 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2161 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2162 }
2163 sppp_open_event(&ipcp, sp);
2164}
2165
2166static void
2167sppp_ipcp_close(struct sppp *sp)
2168{
2169 sppp_close_event(&ipcp, sp);
2170 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2171 /*
2172 * My address was dynamic, clear it again.
2173 */
2174 sppp_set_ip_addr(sp, 0L);
2175}
2176
2177static void
2178sppp_ipcp_TO(void *cookie)
2179{
2180 sppp_to_event(&ipcp, (struct sppp *)cookie);
2181}
2182
2183/*
2184 * Analyze a configure request. Return true if it was agreeable, and
2185 * caused action sca, false if it has been rejected or nak'ed, and
2186 * caused action scn. (The return value is used to make the state
2187 * transition decision in the state automaton.)
2188 */
2189static int
2190sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2191{
2192 u_char *buf, *r, *p;
2193 struct ifnet *ifp = &sp->pp_if;
2194 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2195 u_long hisaddr, desiredaddr;
2196
2197 len -= 4;
2198 origlen = len;
2199 /*
2200 * Make sure to allocate a buf that can at least hold a
2201 * conf-nak with an `address' option. We might need it below.
2202 */
2203 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2204 if (! buf)
2205 return (0);
2206
2207 /* pass 1: see if we can recognize them */
2208 if (debug)
2209 log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2210 ifp->if_name, ifp->if_unit);
2211 p = (void*) (h+1);
2212 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2213 if (debug)
2214 addlog(" %s ", sppp_ipcp_opt_name(*p));
2215 switch (*p) {
2216#ifdef notyet
2217 case IPCP_OPT_COMPRESSION:
2218 if (len >= 6 && p[1] >= 6) {
2219 /* correctly formed compress option */
2220 continue;
2221 }
2222 if (debug)
2223 addlog("[invalid] ");
2224 break;
2225#endif
2226 case IPCP_OPT_ADDRESS:
2227 if (len >= 6 && p[1] == 6) {
2228 /* correctly formed address option */
2229 continue;
2230 }
2231 if (debug)
2232 addlog("[invalid] ");
2233 break;
2234 default:
2235 /* Others not supported. */
2236 if (debug)
2237 addlog("[rej] ");
2238 break;
2239 }
2240 /* Add the option to rejected list. */
2241 bcopy (p, r, p[1]);
2242 r += p[1];
2243 rlen += p[1];
2244 }
2245 if (rlen) {
2246 if (debug)
2247 addlog(" send conf-rej\n");
2248 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2249 return 0;
2250 } else if (debug)
2251 addlog("\n");
2252
2253 /* pass 2: parse option values */
2370 /*
2371 * If we don't have his address, this probably means our
2372 * interface doesn't want to talk IP at all. (This could
2373 * be the case if somebody wants to speak only IPX, for
2374 * example.) Don't open IPCP in this case.
2375 */
2376 if (hisaddr == 0L) {
2377 /* XXX this message should go away */
2378 if (debug)
2379 log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2380 ifp->if_name, ifp->if_unit);
2381 return;
2382 }
2383
2384 if (myaddr == 0L) {
2385 /*
2386 * I don't have an assigned address, so i need to
2387 * negotiate my address.
2388 */
2389 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2390 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2391 }
2392 sppp_open_event(&ipcp, sp);
2393}
2394
2395static void
2396sppp_ipcp_close(struct sppp *sp)
2397{
2398 sppp_close_event(&ipcp, sp);
2399 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2400 /*
2401 * My address was dynamic, clear it again.
2402 */
2403 sppp_set_ip_addr(sp, 0L);
2404}
2405
2406static void
2407sppp_ipcp_TO(void *cookie)
2408{
2409 sppp_to_event(&ipcp, (struct sppp *)cookie);
2410}
2411
2412/*
2413 * Analyze a configure request. Return true if it was agreeable, and
2414 * caused action sca, false if it has been rejected or nak'ed, and
2415 * caused action scn. (The return value is used to make the state
2416 * transition decision in the state automaton.)
2417 */
2418static int
2419sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2420{
2421 u_char *buf, *r, *p;
2422 struct ifnet *ifp = &sp->pp_if;
2423 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2424 u_long hisaddr, desiredaddr;
2425
2426 len -= 4;
2427 origlen = len;
2428 /*
2429 * Make sure to allocate a buf that can at least hold a
2430 * conf-nak with an `address' option. We might need it below.
2431 */
2432 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2433 if (! buf)
2434 return (0);
2435
2436 /* pass 1: see if we can recognize them */
2437 if (debug)
2438 log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2439 ifp->if_name, ifp->if_unit);
2440 p = (void*) (h+1);
2441 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2442 if (debug)
2443 addlog(" %s ", sppp_ipcp_opt_name(*p));
2444 switch (*p) {
2445#ifdef notyet
2446 case IPCP_OPT_COMPRESSION:
2447 if (len >= 6 && p[1] >= 6) {
2448 /* correctly formed compress option */
2449 continue;
2450 }
2451 if (debug)
2452 addlog("[invalid] ");
2453 break;
2454#endif
2455 case IPCP_OPT_ADDRESS:
2456 if (len >= 6 && p[1] == 6) {
2457 /* correctly formed address option */
2458 continue;
2459 }
2460 if (debug)
2461 addlog("[invalid] ");
2462 break;
2463 default:
2464 /* Others not supported. */
2465 if (debug)
2466 addlog("[rej] ");
2467 break;
2468 }
2469 /* Add the option to rejected list. */
2470 bcopy (p, r, p[1]);
2471 r += p[1];
2472 rlen += p[1];
2473 }
2474 if (rlen) {
2475 if (debug)
2476 addlog(" send conf-rej\n");
2477 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2478 return 0;
2479 } else if (debug)
2480 addlog("\n");
2481
2482 /* pass 2: parse option values */
2254 sppp_get_ip_addrs(sp, 0, &hisaddr);
2483 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2255 if (debug)
2484 if (debug)
2256 addlog("%s%d: ipcp parse opt values: ", ifp->if_name, ifp->if_unit);
2485 log(LOG_DEBUG, "%s%d: ipcp parse opt values: ",
2486 ifp->if_name, ifp->if_unit);
2257 p = (void*) (h+1);
2258 len = origlen;
2259 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2260 if (debug)
2261 addlog(" %s ", sppp_ipcp_opt_name(*p));
2262 switch (*p) {
2263#ifdef notyet
2264 case IPCP_OPT_COMPRESSION:
2265 continue;
2266#endif
2267 case IPCP_OPT_ADDRESS:
2268 desiredaddr = p[2] << 24 | p[3] << 16 |
2269 p[4] << 8 | p[5];
2270 if (desiredaddr == hisaddr) {
2271 /*
2272 * Peer's address is same as our value,
2273 * this is agreeable. Gonna conf-ack
2274 * it.
2275 */
2276 if (debug)
2487 p = (void*) (h+1);
2488 len = origlen;
2489 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2490 if (debug)
2491 addlog(" %s ", sppp_ipcp_opt_name(*p));
2492 switch (*p) {
2493#ifdef notyet
2494 case IPCP_OPT_COMPRESSION:
2495 continue;
2496#endif
2497 case IPCP_OPT_ADDRESS:
2498 desiredaddr = p[2] << 24 | p[3] << 16 |
2499 p[4] << 8 | p[5];
2500 if (desiredaddr == hisaddr) {
2501 /*
2502 * Peer's address is same as our value,
2503 * this is agreeable. Gonna conf-ack
2504 * it.
2505 */
2506 if (debug)
2277 addlog("0x%x [ack] ", hisaddr);
2507 addlog("%s [ack] ",
2508 sppp_dotted_quad(hisaddr));
2278 /* record that we've seen it already */
2279 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2280 continue;
2281 }
2282 /*
2283 * The address wasn't agreeable. This is either
2284 * he sent us 0.0.0.0, asking to assign him an
2285 * address, or he send us another address not
2286 * matching our value. Either case, we gonna
2287 * conf-nak it with our value.
2288 */
2289 if (debug) {
2290 if (desiredaddr == 0)
2291 addlog("[addr requested] ");
2292 else
2509 /* record that we've seen it already */
2510 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2511 continue;
2512 }
2513 /*
2514 * The address wasn't agreeable. This is either
2515 * he sent us 0.0.0.0, asking to assign him an
2516 * address, or he send us another address not
2517 * matching our value. Either case, we gonna
2518 * conf-nak it with our value.
2519 */
2520 if (debug) {
2521 if (desiredaddr == 0)
2522 addlog("[addr requested] ");
2523 else
2293 addlog("0x%x [not agreed] ",
2294 desiredaddr);
2524 addlog("%s [not agreed] ",
2525 sppp_dotted_quad(desiredaddr));
2295
2296 p[2] = hisaddr >> 24;
2297 p[3] = hisaddr >> 16;
2298 p[4] = hisaddr >> 8;
2299 p[5] = hisaddr;
2300 }
2301 break;
2302 }
2303 /* Add the option to nak'ed list. */
2304 bcopy (p, r, p[1]);
2305 r += p[1];
2306 rlen += p[1];
2307 }
2308
2309 /*
2310 * If we are about to conf-ack the request, but haven't seen
2311 * his address so far, gonna conf-nak it instead, with the
2312 * `address' option present and our idea of his address being
2313 * filled in there, to request negotiation of both addresses.
2314 *
2315 * XXX This can result in an endless req - nak loop if peer
2316 * doesn't want to send us his address. Q: What should we do
2317 * about it? XXX A: implement the max-failure counter.
2318 */
2319 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2320 buf[0] = IPCP_OPT_ADDRESS;
2321 buf[1] = 6;
2322 buf[2] = hisaddr >> 24;
2323 buf[3] = hisaddr >> 16;
2324 buf[4] = hisaddr >> 8;
2325 buf[5] = hisaddr;
2326 rlen = 6;
2327 if (debug)
2328 addlog("still need hisaddr ");
2329 }
2330
2331 if (rlen) {
2332 if (debug)
2333 addlog(" send conf-nak\n");
2334 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2335 } else {
2336 if (debug)
2337 addlog(" send conf-ack\n");
2338 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2339 h->ident, origlen, h+1);
2340 }
2341
2342 free (buf, M_TEMP);
2343 return (rlen == 0);
2344}
2345
2346/*
2347 * Analyze the IPCP Configure-Reject option list, and adjust our
2348 * negotiation.
2349 */
2350static void
2351sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2352{
2353 u_char *buf, *p;
2354 struct ifnet *ifp = &sp->pp_if;
2355 int debug = ifp->if_flags & IFF_DEBUG;
2356
2357 len -= 4;
2358 buf = malloc (len, M_TEMP, M_NOWAIT);
2359 if (!buf)
2360 return;
2361
2362 if (debug)
2363 log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2364 ifp->if_name, ifp->if_unit);
2365
2366 p = (void*) (h+1);
2367 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2368 if (debug)
2369 addlog(" %s ", sppp_ipcp_opt_name(*p));
2370 switch (*p) {
2371 case IPCP_OPT_ADDRESS:
2372 /*
2373 * Peer doesn't grok address option. This is
2374 * bad. XXX Should we better give up here?
2375 */
2376 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2377 break;
2378#ifdef notyet
2379 case IPCP_OPT_COMPRESS:
2380 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2381 break;
2382#endif
2383 }
2384 }
2385 if (debug)
2386 addlog("\n");
2387 free (buf, M_TEMP);
2388 return;
2389}
2390
2391/*
2392 * Analyze the IPCP Configure-NAK option list, and adjust our
2393 * negotiation.
2394 */
2395static void
2396sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2397{
2398 u_char *buf, *p;
2399 struct ifnet *ifp = &sp->pp_if;
2400 int debug = ifp->if_flags & IFF_DEBUG;
2401 u_long wantaddr;
2402
2403 len -= 4;
2404 buf = malloc (len, M_TEMP, M_NOWAIT);
2405 if (!buf)
2406 return;
2407
2408 if (debug)
2409 log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2410 ifp->if_name, ifp->if_unit);
2411
2412 p = (void*) (h+1);
2413 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2414 if (debug)
2415 addlog(" %s ", sppp_ipcp_opt_name(*p));
2416 switch (*p) {
2417 case IPCP_OPT_ADDRESS:
2418 /*
2419 * Peer doesn't like our local IP address. See
2420 * if we can do something for him. We'll drop
2421 * him our address then.
2422 */
2423 if (len >= 6 && p[1] == 6) {
2424 wantaddr = p[2] << 24 | p[3] << 16 |
2425 p[4] << 8 | p[5];
2426 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2427 if (debug)
2526
2527 p[2] = hisaddr >> 24;
2528 p[3] = hisaddr >> 16;
2529 p[4] = hisaddr >> 8;
2530 p[5] = hisaddr;
2531 }
2532 break;
2533 }
2534 /* Add the option to nak'ed list. */
2535 bcopy (p, r, p[1]);
2536 r += p[1];
2537 rlen += p[1];
2538 }
2539
2540 /*
2541 * If we are about to conf-ack the request, but haven't seen
2542 * his address so far, gonna conf-nak it instead, with the
2543 * `address' option present and our idea of his address being
2544 * filled in there, to request negotiation of both addresses.
2545 *
2546 * XXX This can result in an endless req - nak loop if peer
2547 * doesn't want to send us his address. Q: What should we do
2548 * about it? XXX A: implement the max-failure counter.
2549 */
2550 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2551 buf[0] = IPCP_OPT_ADDRESS;
2552 buf[1] = 6;
2553 buf[2] = hisaddr >> 24;
2554 buf[3] = hisaddr >> 16;
2555 buf[4] = hisaddr >> 8;
2556 buf[5] = hisaddr;
2557 rlen = 6;
2558 if (debug)
2559 addlog("still need hisaddr ");
2560 }
2561
2562 if (rlen) {
2563 if (debug)
2564 addlog(" send conf-nak\n");
2565 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2566 } else {
2567 if (debug)
2568 addlog(" send conf-ack\n");
2569 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2570 h->ident, origlen, h+1);
2571 }
2572
2573 free (buf, M_TEMP);
2574 return (rlen == 0);
2575}
2576
2577/*
2578 * Analyze the IPCP Configure-Reject option list, and adjust our
2579 * negotiation.
2580 */
2581static void
2582sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2583{
2584 u_char *buf, *p;
2585 struct ifnet *ifp = &sp->pp_if;
2586 int debug = ifp->if_flags & IFF_DEBUG;
2587
2588 len -= 4;
2589 buf = malloc (len, M_TEMP, M_NOWAIT);
2590 if (!buf)
2591 return;
2592
2593 if (debug)
2594 log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2595 ifp->if_name, ifp->if_unit);
2596
2597 p = (void*) (h+1);
2598 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2599 if (debug)
2600 addlog(" %s ", sppp_ipcp_opt_name(*p));
2601 switch (*p) {
2602 case IPCP_OPT_ADDRESS:
2603 /*
2604 * Peer doesn't grok address option. This is
2605 * bad. XXX Should we better give up here?
2606 */
2607 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2608 break;
2609#ifdef notyet
2610 case IPCP_OPT_COMPRESS:
2611 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2612 break;
2613#endif
2614 }
2615 }
2616 if (debug)
2617 addlog("\n");
2618 free (buf, M_TEMP);
2619 return;
2620}
2621
2622/*
2623 * Analyze the IPCP Configure-NAK option list, and adjust our
2624 * negotiation.
2625 */
2626static void
2627sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2628{
2629 u_char *buf, *p;
2630 struct ifnet *ifp = &sp->pp_if;
2631 int debug = ifp->if_flags & IFF_DEBUG;
2632 u_long wantaddr;
2633
2634 len -= 4;
2635 buf = malloc (len, M_TEMP, M_NOWAIT);
2636 if (!buf)
2637 return;
2638
2639 if (debug)
2640 log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2641 ifp->if_name, ifp->if_unit);
2642
2643 p = (void*) (h+1);
2644 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2645 if (debug)
2646 addlog(" %s ", sppp_ipcp_opt_name(*p));
2647 switch (*p) {
2648 case IPCP_OPT_ADDRESS:
2649 /*
2650 * Peer doesn't like our local IP address. See
2651 * if we can do something for him. We'll drop
2652 * him our address then.
2653 */
2654 if (len >= 6 && p[1] == 6) {
2655 wantaddr = p[2] << 24 | p[3] << 16 |
2656 p[4] << 8 | p[5];
2657 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2658 if (debug)
2428 addlog("[wantaddr 0x%x] ", wantaddr);
2659 addlog("[wantaddr %s] ",
2660 sppp_dotted_quad(wantaddr));
2429 /*
2430 * When doing dynamic address assignment,
2431 * we accept his offer. Otherwise, we
2432 * ignore it and thus continue to negotiate
2433 * our already existing value.
2434 */
2435 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2436 sppp_set_ip_addr(sp, wantaddr);
2437 if (debug)
2438 addlog("[agree] ");
2439 }
2440 }
2441 break;
2442#ifdef notyet
2443 case IPCP_OPT_COMPRESS:
2444 /*
2445 * Peer wants different compression parameters.
2446 */
2447 break;
2448#endif
2449 }
2450 }
2451 if (debug)
2452 addlog("\n");
2453 free (buf, M_TEMP);
2454 return;
2455}
2456
2457static void
2458sppp_ipcp_tlu(struct sppp *sp)
2459{
2460}
2461
2462static void
2463sppp_ipcp_tld(struct sppp *sp)
2464{
2465}
2466
2467static void
2468sppp_ipcp_tls(struct sppp *sp)
2469{
2470 /* indicate to LCP that it must stay alive */
2471 sp->lcp.protos |= (1 << IDX_IPCP);
2472}
2473
2474static void
2475sppp_ipcp_tlf(struct sppp *sp)
2476{
2477 /* we no longer need LCP */
2478 sp->lcp.protos &= ~(1 << IDX_IPCP);
2661 /*
2662 * When doing dynamic address assignment,
2663 * we accept his offer. Otherwise, we
2664 * ignore it and thus continue to negotiate
2665 * our already existing value.
2666 */
2667 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2668 sppp_set_ip_addr(sp, wantaddr);
2669 if (debug)
2670 addlog("[agree] ");
2671 }
2672 }
2673 break;
2674#ifdef notyet
2675 case IPCP_OPT_COMPRESS:
2676 /*
2677 * Peer wants different compression parameters.
2678 */
2679 break;
2680#endif
2681 }
2682 }
2683 if (debug)
2684 addlog("\n");
2685 free (buf, M_TEMP);
2686 return;
2687}
2688
2689static void
2690sppp_ipcp_tlu(struct sppp *sp)
2691{
2692}
2693
2694static void
2695sppp_ipcp_tld(struct sppp *sp)
2696{
2697}
2698
2699static void
2700sppp_ipcp_tls(struct sppp *sp)
2701{
2702 /* indicate to LCP that it must stay alive */
2703 sp->lcp.protos |= (1 << IDX_IPCP);
2704}
2705
2706static void
2707sppp_ipcp_tlf(struct sppp *sp)
2708{
2709 /* we no longer need LCP */
2710 sp->lcp.protos &= ~(1 << IDX_IPCP);
2479 sppp_lcp_check(sp);
2711 sppp_lcp_check_and_close(sp);
2480}
2481
2482static void
2483sppp_ipcp_scr(struct sppp *sp)
2484{
2485 char opt[6 /* compression */ + 6 /* address */];
2486 u_long ouraddr;
2487 int i = 0;
2488
2489#ifdef notyet
2490 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2491 opt[i++] = IPCP_OPT_COMPRESSION;
2492 opt[i++] = 6;
2493 opt[i++] = 0; /* VJ header compression */
2494 opt[i++] = 0x2d; /* VJ header compression */
2495 opt[i++] = max_slot_id;
2496 opt[i++] = comp_slot_id;
2497 }
2498#endif
2499
2500 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2712}
2713
2714static void
2715sppp_ipcp_scr(struct sppp *sp)
2716{
2717 char opt[6 /* compression */ + 6 /* address */];
2718 u_long ouraddr;
2719 int i = 0;
2720
2721#ifdef notyet
2722 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2723 opt[i++] = IPCP_OPT_COMPRESSION;
2724 opt[i++] = 6;
2725 opt[i++] = 0; /* VJ header compression */
2726 opt[i++] = 0x2d; /* VJ header compression */
2727 opt[i++] = max_slot_id;
2728 opt[i++] = comp_slot_id;
2729 }
2730#endif
2731
2732 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2501 sppp_get_ip_addrs(sp, &ouraddr, 0);
2733 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2502 opt[i++] = IPCP_OPT_ADDRESS;
2503 opt[i++] = 6;
2504 opt[i++] = ouraddr >> 24;
2505 opt[i++] = ouraddr >> 16;
2506 opt[i++] = ouraddr >> 8;
2507 opt[i++] = ouraddr;
2508 }
2509
2510 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2511 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2512}
2513
2514
2515 /*
2734 opt[i++] = IPCP_OPT_ADDRESS;
2735 opt[i++] = 6;
2736 opt[i++] = ouraddr >> 24;
2737 opt[i++] = ouraddr >> 16;
2738 opt[i++] = ouraddr >> 8;
2739 opt[i++] = ouraddr;
2740 }
2741
2742 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2743 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2744}
2745
2746
2747 /*
2748 *--------------------------------------------------------------------------*
2749 * *
2750 * The CHAP implementation. *
2751 * *
2752 *--------------------------------------------------------------------------*
2753 */
2754
2755/*
2756 * The authentication protocols don't employ a full-fledged state machine as
2757 * the control protocols do, since they do have Open and Close events, but
2758 * not Up and Down, nor are they explicitly terminated. Also, use of the
2759 * authentication protocols may be different in both directions (this makes
2760 * sense, think of a machine that never accepts incoming calls but only
2761 * calls out, it doesn't require the called party to authenticate itself).
2762 *
2763 * Our state machine for the local authentication protocol (we are requesting
2764 * the peer to authenticate) looks like:
2765 *
2766 * RCA-
2767 * +--------------------------------------------+
2768 * V scn,tld|
2769 * +--------+ Close +---------+ RCA+
2770 * | |<----------------------------------| |------+
2771 * +--->| Closed | TO* | Opened | sca |
2772 * | | |-----+ +-------| |<-----+
2773 * | +--------+ irc | | +---------+
2774 * | ^ | | ^
2775 * | | | | |
2776 * | | | | |
2777 * | TO-| | | |
2778 * | |tld TO+ V | |
2779 * | | +------->+ | |
2780 * | | | | | |
2781 * | +--------+ V | |
2782 * | | |<----+<--------------------+ |
2783 * | | Req- | scr |
2784 * | | Sent | |
2785 * | | | |
2786 * | +--------+ |
2787 * | RCA- | | RCA+ |
2788 * +------+ +------------------------------------------+
2789 * scn,tld sca,irc,ict,tlu
2790 *
2791 *
2792 * with:
2793 *
2794 * Open: LCP reached authentication phase
2795 * Close: LCP reached terminate phase
2796 *
2797 * RCA+: received reply (pap-req, chap-response), acceptable
2798 * RCN: received reply (pap-req, chap-response), not acceptable
2799 * TO+: timeout with restart counter >= 0
2800 * TO-: timeout with restart counter < 0
2801 * TO*: reschedule timeout for CHAP
2802 *
2803 * scr: send request packet (none for PAP, chap-challenge)
2804 * sca: send ack packet (pap-ack, chap-success)
2805 * scn: send nak packet (pap-nak, chap-failure)
2806 * ict: initialize re-challenge timer (CHAP only)
2807 *
2808 * tlu: this-layer-up, LCP reaches network phase
2809 * tld: this-layer-down, LCP enters terminate phase
2810 *
2811 * Note that in CHAP mode, after sending a new challenge, while the state
2812 * automaton falls back into Req-Sent state, it doesn't signal a tld
2813 * event to LCP, so LCP remains in network phase. Only after not getting
2814 * any response (or after getting an unacceptable response), CHAP closes,
2815 * causing LCP to enter terminate phase.
2816 *
2817 * With PAP, there is no initial request that can be sent. The peer is
2818 * expected to send one based on the successful negotiation of PAP as
2819 * the authentication protocol during the LCP option negotiation.
2820 *
2821 * Incoming authentication protocol requests (remote requests
2822 * authentication, we are peer) don't employ a state machine at all,
2823 * they are simply answered. Some peers [Ascend P50 firmware rev
2824 * 4.50] react allergically when sending IPCP requests while they are
2825 * still in authentication phase (thereby violating the standard that
2826 * demands that these NCP packets are to be discarded), so we keep
2827 * track of the peer demanding us to authenticate, and only proceed to
2828 * phase network once we've seen a positive acknowledge for the
2829 * authentication.
2830 */
2831
2832/*
2833 * Handle incoming CHAP packets.
2834 */
2835void
2836sppp_chap_input(struct sppp *sp, struct mbuf *m)
2837{
2838 STDDCL;
2839 struct lcp_header *h;
2840 int len, x;
2841 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2842 int value_len, name_len;
2843 MD5_CTX ctx;
2844
2845 len = m->m_pkthdr.len;
2846 if (len < 4) {
2847 if (debug)
2848 log(LOG_DEBUG,
2849 "%s%d: chap invalid packet length: %d bytes\n",
2850 ifp->if_name, ifp->if_unit, len);
2851 return;
2852 }
2853 h = mtod (m, struct lcp_header*);
2854 if (len > ntohs (h->len))
2855 len = ntohs (h->len);
2856
2857 switch (h->type) {
2858 /* challenge, failure and success are his authproto */
2859 case CHAP_CHALLENGE:
2860 value = 1 + (u_char*)(h+1);
2861 value_len = value[-1];
2862 name = value + value_len;
2863 name_len = len - value_len - 5;
2864 if (name_len < 0) {
2865 if (debug) {
2866 log(LOG_DEBUG,
2867 "%s%d: chap corrupted challenge "
2868 "<%s id=0x%x len=%d",
2869 ifp->if_name, ifp->if_unit,
2870 sppp_auth_type_name(PPP_CHAP, h->type),
2871 h->ident, ntohs(h->len));
2872 if (len > 4)
2873 sppp_print_bytes((u_char*) (h+1), len-4);
2874 addlog(">\n");
2875 }
2876 break;
2877 }
2878 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2879 || bcmp(name, sp->hisauth.name, name_len) != 0) {
2880 log(LOG_INFO, "%s%d: chap challenge, his name ");
2881 sppp_print_string(name, name_len);
2882 addlog(" != expected ");
2883 sppp_print_string(sp->hisauth.name,
2884 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2885 addlog("\n");
2886 }
2887
2888 if (debug) {
2889 log(LOG_DEBUG,
2890 "%s%d: chap input <%s id=0x%x len=%d name=",
2891 ifp->if_name, ifp->if_unit,
2892 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
2893 ntohs(h->len));
2894 sppp_print_string((char*) name, name_len);
2895 addlog(" value-size=%d value=", value_len);
2896 sppp_print_bytes(value, value_len);
2897 addlog(">\n");
2898 }
2899
2900 /* Compute reply value. */
2901 MD5Init(&ctx);
2902 MD5Update(&ctx, &h->ident, 1);
2903 MD5Update(&ctx, sp->myauth.secret,
2904 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
2905 MD5Update(&ctx, value, value_len);
2906 MD5Final(digest, &ctx);
2907 dsize = sizeof digest;
2908
2909 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
2910 sizeof dsize, (const char *)&dsize,
2911 sizeof digest, digest,
2912 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
2913 sp->myauth.name,
2914 0);
2915 break;
2916
2917 case CHAP_SUCCESS:
2918 if (debug) {
2919 log(LOG_DEBUG, "%s%d: chap success",
2920 ifp->if_name, ifp->if_unit);
2921 if (len > 4) {
2922 addlog(": ");
2923 sppp_print_string((char*)(h + 1), len - 4);
2924 }
2925 addlog("\n");
2926 }
2927 x = splimp();
2928 sp->pp_flags &= ~PP_NEEDAUTH;
2929 if (sp->myauth.proto == PPP_CHAP &&
2930 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
2931 /*
2932 * We are authenticator for CHAP but didn't
2933 * complete yet. Leave it to tlu to proceed
2934 * to network phase.
2935 */
2936 splx(x);
2937 break;
2938 }
2939 splx(x);
2940 sppp_phase_network(sp);
2941 break;
2942
2943 case CHAP_FAILURE:
2944 if (debug) {
2945 log(LOG_INFO, "%s%d: chap failure",
2946 ifp->if_name, ifp->if_unit);
2947 if (len > 4) {
2948 addlog(": ");
2949 sppp_print_string((char*)(h + 1), len - 4);
2950 }
2951 addlog("\n");
2952 } else
2953 log(LOG_INFO, "%s%d: chap failure\n",
2954 ifp->if_name, ifp->if_unit);
2955 /* await LCP shutdown by authenticator */
2956 break;
2957
2958 /* response is my authproto */
2959 case CHAP_RESPONSE:
2960 value = 1 + (u_char*)(h+1);
2961 value_len = value[-1];
2962 name = value + value_len;
2963 name_len = len - value_len - 5;
2964 if (name_len < 0) {
2965 if (debug) {
2966 log(LOG_DEBUG,
2967 "%s%d: chap corrupted response "
2968 "<%s id=0x%x len=%d",
2969 ifp->if_name, ifp->if_unit,
2970 sppp_auth_type_name(PPP_CHAP, h->type),
2971 h->ident, ntohs(h->len));
2972 if (len > 4)
2973 sppp_print_bytes((u_char*)(h+1), len-4);
2974 addlog(">\n");
2975 }
2976 break;
2977 }
2978 if (h->ident != sp->confid[IDX_CHAP]) {
2979 if (debug)
2980 log(LOG_DEBUG,
2981 "%s%d: chap dropping response for old ID "
2982 "(got %d, expected %d)\n",
2983 h->ident, sp->confid[IDX_CHAP]);
2984 break;
2985 }
2986 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2987 || bcmp(name, sp->hisauth.name, name_len) != 0) {
2988 log(LOG_INFO, "%s%d: chap response, his name ",
2989 ifp->if_name, ifp->if_unit);
2990 sppp_print_string(name, name_len);
2991 addlog(" != expected ");
2992 sppp_print_string(sp->hisauth.name,
2993 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2994 addlog("\n");
2995 }
2996 if (debug) {
2997 log(LOG_DEBUG, "%s%d: chap input(%s) "
2998 "<%s id=0x%x len=%d name=",
2999 ifp->if_name, ifp->if_unit,
3000 sppp_state_name(sp->state[IDX_CHAP]),
3001 sppp_auth_type_name(PPP_CHAP, h->type),
3002 h->ident, ntohs (h->len));
3003 sppp_print_string((char*)name, name_len);
3004 addlog(" value-size=%d value=", value_len);
3005 sppp_print_bytes(value, value_len);
3006 addlog(">\n");
3007 }
3008 if (value_len != AUTHKEYLEN) {
3009 if (debug)
3010 log(LOG_DEBUG,
3011 "%s%d: chap bad hash value length: "
3012 "%d bytes, should be %d\n",
3013 ifp->if_name, ifp->if_unit, value_len,
3014 AUTHKEYLEN);
3015 break;
3016 }
3017
3018 MD5Init(&ctx);
3019 MD5Update(&ctx, &h->ident, 1);
3020 MD5Update(&ctx, sp->hisauth.secret,
3021 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3022 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3023 MD5Final(digest, &ctx);
3024
3025#define FAILMSG "Failed..."
3026#define SUCCMSG "Welcome!"
3027
3028 if (value_len != sizeof digest ||
3029 bcmp(digest, value, value_len) != 0) {
3030 /* action scn, tld */
3031 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3032 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3033 0);
3034 chap.tld(sp);
3035 break;
3036 }
3037 /* action sca, perhaps tlu */
3038 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3039 sp->state[IDX_CHAP] == STATE_OPENED)
3040 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3041 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3042 0);
3043 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3044 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3045 chap.tlu(sp);
3046 }
3047 break;
3048
3049 default:
3050 /* Unknown CHAP packet type -- ignore. */
3051 if (debug) {
3052 log(LOG_DEBUG, "%s%d: chap unknown input(%s) "
3053 "<0x%x id=0x%xh len=%d",
3054 ifp->if_name, ifp->if_unit,
3055 sppp_state_name(sp->state[IDX_CHAP]),
3056 h->type, h->ident, ntohs(h->len));
3057 if (len > 4)
3058 sppp_print_bytes((u_char*)(h+1), len-4);
3059 addlog(">\n");
3060 }
3061 break;
3062
3063 }
3064}
3065
3066static void
3067sppp_chap_init(struct sppp *sp)
3068{
3069 /* Chap doesn't have STATE_INITIAL at all. */
3070 sp->state[IDX_CHAP] = STATE_CLOSED;
3071 sp->fail_counter[IDX_CHAP] = 0;
3072 callout_handle_init(&sp->ch[IDX_CHAP]);
3073}
3074
3075static void
3076sppp_chap_open(struct sppp *sp)
3077{
3078 if (sp->myauth.proto == PPP_CHAP &&
3079 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3080 /* we are authenticator for CHAP, start it */
3081 chap.scr(sp);
3082 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3083 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3084 }
3085 /* nothing to be done if we are peer, await a challenge */
3086}
3087
3088static void
3089sppp_chap_close(struct sppp *sp)
3090{
3091 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3092 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3093}
3094
3095static void
3096sppp_chap_TO(void *cookie)
3097{
3098 struct sppp *sp = (struct sppp *)cookie;
3099 STDDCL;
3100 int s;
3101
3102 s = splimp();
3103 if (debug)
3104 log(LOG_DEBUG, "%s%d: chap TO(%s) rst_counter = %d\n",
3105 ifp->if_name, ifp->if_unit,
3106 sppp_state_name(sp->state[IDX_CHAP]),
3107 sp->rst_counter[IDX_CHAP]);
3108
3109 if (--sp->rst_counter[IDX_CHAP] < 0)
3110 /* TO- event */
3111 switch (sp->state[IDX_CHAP]) {
3112 case STATE_REQ_SENT:
3113 chap.tld(sp);
3114 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3115 break;
3116 }
3117 else
3118 /* TO+ (or TO*) event */
3119 switch (sp->state[IDX_CHAP]) {
3120 case STATE_OPENED:
3121 /* TO* event */
3122 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3123 /* fall through */
3124 case STATE_REQ_SENT:
3125 chap.scr(sp);
3126 /* sppp_cp_change_state() will restart the timer */
3127 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3128 break;
3129 }
3130
3131 splx(s);
3132}
3133
3134static void
3135sppp_chap_tlu(struct sppp *sp)
3136{
3137 STDDCL;
3138 int i, x;
3139
3140 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3141
3142 /*
3143 * Some broken CHAP implementations (Conware CoNet, firmware
3144 * 4.0.?) don't want to re-authenticate their CHAP once the
3145 * initial challenge-response exchange has taken place.
3146 * Provide for an option to avoid rechallenges.
3147 */
3148 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3149 /*
3150 * Compute the re-challenge timeout. This will yield
3151 * a number between 300 and 810 seconds.
3152 */
3153 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3154
3155 sp->ch[IDX_CHAP] = timeout(chap.TO, (void *)sp, i * hz);
3156 }
3157
3158 if (debug) {
3159 log(LOG_DEBUG,
3160 "%s%d: chap %s, ",
3161 ifp->if_name, ifp->if_unit,
3162 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3163 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3164 addlog("next re-challenge in %d seconds\n", i);
3165 else
3166 addlog("re-challenging supressed\n");
3167 }
3168
3169 x = splimp();
3170 /* indicate to LCP that we need to be closed down */
3171 sp->lcp.protos |= (1 << IDX_CHAP);
3172
3173 if (sp->pp_flags & PP_NEEDAUTH) {
3174 /*
3175 * Remote is authenticator, but his auth proto didn't
3176 * complete yet. Defer the transition to network
3177 * phase.
3178 */
3179 splx(x);
3180 return;
3181 }
3182 splx(x);
3183
3184 /*
3185 * If we are already in phase network, we are done here. This
3186 * is the case if this is a dummy tlu event after a re-challenge.
3187 */
3188 if (sp->pp_phase != PHASE_NETWORK)
3189 sppp_phase_network(sp);
3190}
3191
3192static void
3193sppp_chap_tld(struct sppp *sp)
3194{
3195 STDDCL;
3196
3197 if (debug)
3198 log(LOG_DEBUG, "%s%d: chap tld\n", ifp->if_name, ifp->if_unit);
3199 untimeout(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3200 sp->lcp.protos &= ~(1 << IDX_CHAP);
3201
3202 lcp.Close(sp);
3203}
3204
3205static void
3206sppp_chap_scr(struct sppp *sp)
3207{
3208 struct timeval tv;
3209 u_long *ch, seed;
3210 u_char clen;
3211
3212 /* Compute random challenge. */
3213 ch = (u_long *)sp->myauth.challenge;
3214 microtime(&tv);
3215 seed = tv.tv_sec ^ tv.tv_usec;
3216 ch[0] = seed ^ random();
3217 ch[1] = seed ^ random();
3218 ch[2] = seed ^ random();
3219 ch[3] = seed ^ random();
3220 clen = AUTHKEYLEN;
3221
3222 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3223
3224 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3225 sizeof clen, (const char *)&clen,
3226 AUTHKEYLEN, sp->myauth.challenge,
3227 sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3228 sp->myauth.name,
3229 0);
3230}
3231 /*
3232 *--------------------------------------------------------------------------*
3233 * *
3234 * The PAP implementation. *
3235 * *
3236 *--------------------------------------------------------------------------*
3237 */
3238/*
3239 * For PAP, we need to keep a little state also if we are the peer, not the
3240 * authenticator. This is since we don't get a request to authenticate, but
3241 * have to repeatedly authenticate ourself until we got a response (or the
3242 * retry counter is expired).
3243 */
3244
3245/*
3246 * Handle incoming PAP packets. */
3247static void
3248sppp_pap_input(struct sppp *sp, struct mbuf *m)
3249{
3250 STDDCL;
3251 struct lcp_header *h;
3252 int len, x;
3253 u_char *name, *passwd, mlen;
3254 int name_len, passwd_len;
3255
3256 len = m->m_pkthdr.len;
3257 if (len < 5) {
3258 if (debug)
3259 log(LOG_DEBUG,
3260 "%s%d: pap invalid packet length: %d bytes\n",
3261 ifp->if_name, ifp->if_unit, len);
3262 return;
3263 }
3264 h = mtod (m, struct lcp_header*);
3265 if (len > ntohs (h->len))
3266 len = ntohs (h->len);
3267 switch (h->type) {
3268 /* PAP request is my authproto */
3269 case PAP_REQ:
3270 name = 1 + (u_char*)(h+1);
3271 name_len = name[-1];
3272 passwd = name + name_len + 1;
3273 if (name_len > len - 6 ||
3274 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3275 if (debug) {
3276 log(LOG_DEBUG, "%s%d: pap corrupted input "
3277 "<%s id=0x%x len=%d",
3278 ifp->if_name, ifp->if_unit,
3279 sppp_auth_type_name(PPP_PAP, h->type),
3280 h->ident, ntohs(h->len));
3281 if (len > 4)
3282 sppp_print_bytes((u_char*)(h+1), len-4);
3283 addlog(">\n");
3284 }
3285 break;
3286 }
3287 if (debug) {
3288 log(LOG_DEBUG, "%s%d: pap input(%s) "
3289 "<%s id=0x%x len=%d name=",
3290 ifp->if_name, ifp->if_unit,
3291 sppp_state_name(sp->state[IDX_PAP]),
3292 sppp_auth_type_name(PPP_PAP, h->type),
3293 h->ident, ntohs(h->len));
3294 sppp_print_string((char*)name, name_len);
3295 addlog(" passwd=");
3296 sppp_print_string((char*)passwd, passwd_len);
3297 addlog(">\n");
3298 }
3299 if (name_len > AUTHNAMELEN ||
3300 passwd_len > AUTHKEYLEN ||
3301 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3302 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3303 /* action scn, tld */
3304 mlen = sizeof(FAILMSG) - 1;
3305 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3306 sizeof mlen, (const char *)&mlen,
3307 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3308 0);
3309 pap.tld(sp);
3310 break;
3311 }
3312 /* action sca, perhaps tlu */
3313 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3314 sp->state[IDX_PAP] == STATE_OPENED) {
3315 mlen = sizeof(SUCCMSG) - 1;
3316 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3317 sizeof mlen, (const char *)&mlen,
3318 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3319 0);
3320 }
3321 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3322 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3323 pap.tlu(sp);
3324 }
3325 break;
3326
3327 /* ack and nak are his authproto */
3328 case PAP_ACK:
3329 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3330 if (debug) {
3331 log(LOG_DEBUG, "%s%d: pap success",
3332 ifp->if_name, ifp->if_unit);
3333 name_len = *((char *)h);
3334 if (len > 5 && name_len) {
3335 addlog(": ");
3336 sppp_print_string((char*)(h+1), name_len);
3337 }
3338 addlog("\n");
3339 }
3340 x = splimp();
3341 sp->pp_flags &= ~PP_NEEDAUTH;
3342 if (sp->myauth.proto == PPP_PAP &&
3343 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3344 /*
3345 * We are authenticator for PAP but didn't
3346 * complete yet. Leave it to tlu to proceed
3347 * to network phase.
3348 */
3349 splx(x);
3350 break;
3351 }
3352 splx(x);
3353 sppp_phase_network(sp);
3354 break;
3355
3356 case PAP_NAK:
3357 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3358 if (debug) {
3359 log(LOG_INFO, "%s%d: pap failure",
3360 ifp->if_name, ifp->if_unit);
3361 name_len = *((char *)h);
3362 if (len > 5 && name_len) {
3363 addlog(": ");
3364 sppp_print_string((char*)(h+1), name_len);
3365 }
3366 addlog("\n");
3367 } else
3368 log(LOG_INFO, "%s%d: pap failure\n",
3369 ifp->if_name, ifp->if_unit);
3370 /* await LCP shutdown by authenticator */
3371 break;
3372
3373 default:
3374 /* Unknown PAP packet type -- ignore. */
3375 if (debug) {
3376 log(LOG_DEBUG, "%s%d: pap corrupted input "
3377 "<0x%x id=0x%x len=%d",
3378 ifp->if_name, ifp->if_unit,
3379 h->type, h->ident, ntohs(h->len));
3380 if (len > 4)
3381 sppp_print_bytes((u_char*)(h+1), len-4);
3382 addlog(">\n");
3383 }
3384 break;
3385
3386 }
3387}
3388
3389static void
3390sppp_pap_init(struct sppp *sp)
3391{
3392 /* PAP doesn't have STATE_INITIAL at all. */
3393 sp->state[IDX_PAP] = STATE_CLOSED;
3394 sp->fail_counter[IDX_PAP] = 0;
3395 callout_handle_init(&sp->ch[IDX_PAP]);
3396 callout_handle_init(&sp->pap_my_to_ch);
3397}
3398
3399static void
3400sppp_pap_open(struct sppp *sp)
3401{
3402 if (sp->hisauth.proto == PPP_PAP &&
3403 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3404 /* we are authenticator for PAP, start our timer */
3405 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3406 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3407 }
3408 if (sp->myauth.proto == PPP_PAP) {
3409 /* we are peer, send a request, and start a timer */
3410 pap.scr(sp);
3411 sp->pap_my_to_ch = timeout(sppp_pap_my_TO, (void *)sp,
3412 sp->lcp.timeout);
3413 }
3414}
3415
3416static void
3417sppp_pap_close(struct sppp *sp)
3418{
3419 if (sp->state[IDX_PAP] != STATE_CLOSED)
3420 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3421}
3422
3423/*
3424 * That's the timeout routine if we are authenticator. Since the
3425 * authenticator is basically passive in PAP, we can't do much here.
3426 */
3427static void
3428sppp_pap_TO(void *cookie)
3429{
3430 struct sppp *sp = (struct sppp *)cookie;
3431 STDDCL;
3432 int s;
3433
3434 s = splimp();
3435 if (debug)
3436 log(LOG_DEBUG, "%s%d: pap TO(%s) rst_counter = %d\n",
3437 ifp->if_name, ifp->if_unit,
3438 sppp_state_name(sp->state[IDX_PAP]),
3439 sp->rst_counter[IDX_PAP]);
3440
3441 if (--sp->rst_counter[IDX_PAP] < 0)
3442 /* TO- event */
3443 switch (sp->state[IDX_PAP]) {
3444 case STATE_REQ_SENT:
3445 pap.tld(sp);
3446 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3447 break;
3448 }
3449 else
3450 /* TO+ event, not very much we could do */
3451 switch (sp->state[IDX_PAP]) {
3452 case STATE_REQ_SENT:
3453 /* sppp_cp_change_state() will restart the timer */
3454 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3455 break;
3456 }
3457
3458 splx(s);
3459}
3460
3461/*
3462 * That's the timeout handler if we are peer. Since the peer is active,
3463 * we need to retransmit our PAP request since it is apparently lost.
3464 * XXX We should impose a max counter.
3465 */
3466static void
3467sppp_pap_my_TO(void *cookie)
3468{
3469 struct sppp *sp = (struct sppp *)cookie;
3470 STDDCL;
3471
3472 if (debug)
3473 log(LOG_DEBUG, "%s%d: pap peer TO\n",
3474 ifp->if_name, ifp->if_unit);
3475
3476 pap.scr(sp);
3477}
3478
3479static void
3480sppp_pap_tlu(struct sppp *sp)
3481{
3482 STDDCL;
3483 int x;
3484
3485 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3486
3487 if (debug)
3488 log(LOG_DEBUG, "%s%d: %s tlu\n",
3489 ifp->if_name, ifp->if_unit, pap.name);
3490
3491 x = splimp();
3492 /* indicate to LCP that we need to be closed down */
3493 sp->lcp.protos |= (1 << IDX_PAP);
3494
3495 if (sp->pp_flags & PP_NEEDAUTH) {
3496 /*
3497 * Remote is authenticator, but his auth proto didn't
3498 * complete yet. Defer the transition to network
3499 * phase.
3500 */
3501 splx(x);
3502 return;
3503 }
3504 splx(x);
3505 sppp_phase_network(sp);
3506}
3507
3508static void
3509sppp_pap_tld(struct sppp *sp)
3510{
3511 STDDCL;
3512
3513 if (debug)
3514 log(LOG_DEBUG, "%s%d: pap tld\n", ifp->if_name, ifp->if_unit);
3515 untimeout(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3516 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3517 sp->lcp.protos &= ~(1 << IDX_PAP);
3518
3519 lcp.Close(sp);
3520}
3521
3522static void
3523sppp_pap_scr(struct sppp *sp)
3524{
3525 STDDCL;
3526 u_char idlen, pwdlen;
3527
3528 sp->confid[IDX_PAP] = ++sp->pp_seq;
3529 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3530 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3531
3532 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3533 sizeof idlen, (const char *)&idlen,
3534 (unsigned)idlen, sp->myauth.name,
3535 sizeof pwdlen, (const char *)&pwdlen,
3536 (unsigned)pwdlen, sp->myauth.secret,
3537 0);
3538}
3539 /*
2516 * Random miscellaneous functions.
2517 */
2518
2519/*
3540 * Random miscellaneous functions.
3541 */
3542
3543/*
3544 * Send a PAP or CHAP proto packet.
3545 *
3546 * Varadic function, each of the elements for the ellipsis is of type
3547 * ``unsigned mlen, const u_char *msg''. Processing will stop iff
3548 * mlen == 0.
3549 */
3550
3551static void
3552sppp_auth_send(const struct cp *cp, struct sppp *sp, u_char type, u_char id,
3553 ...)
3554{
3555 STDDCL;
3556 struct ppp_header *h;
3557 struct lcp_header *lh;
3558 struct mbuf *m;
3559 u_char *p;
3560 int len;
3561 unsigned mlen;
3562 const char *msg;
3563 va_list ap;
3564
3565 MGETHDR (m, M_DONTWAIT, MT_DATA);
3566 if (! m)
3567 return;
3568 m->m_pkthdr.rcvif = 0;
3569
3570 h = mtod (m, struct ppp_header*);
3571 h->address = PPP_ALLSTATIONS; /* broadcast address */
3572 h->control = PPP_UI; /* Unnumbered Info */
3573 h->protocol = htons(cp->proto);
3574
3575 lh = (struct lcp_header*)(h + 1);
3576 lh->type = type;
3577 lh->ident = id;
3578 p = (u_char*) (lh+1);
3579
3580 va_start(ap, id);
3581 len = 0;
3582
3583 while ((mlen = va_arg(ap, unsigned)) != 0) {
3584 msg = va_arg(ap, const char *);
3585 len += mlen;
3586 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3587 va_end(ap);
3588 m_freem(m);
3589 return;
3590 }
3591
3592 bcopy(msg, p, mlen);
3593 p += mlen;
3594 }
3595 va_end(ap);
3596
3597 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3598 lh->len = htons (LCP_HEADER_LEN + len);
3599
3600 if (debug) {
3601 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
3602 ifp->if_name, ifp->if_unit, cp->name,
3603 sppp_auth_type_name(cp->proto, lh->type),
3604 lh->ident, ntohs(lh->len));
3605 if (len)
3606 sppp_print_bytes((u_char*) (lh+1), len);
3607 addlog(">\n");
3608 }
3609 if (IF_QFULL (&sp->pp_cpq)) {
3610 IF_DROP (&sp->pp_fastq);
3611 IF_DROP (&ifp->if_snd);
3612 m_freem (m);
3613 ++ifp->if_oerrors;
3614 } else
3615 IF_ENQUEUE (&sp->pp_cpq, m);
3616 if (! (ifp->if_flags & IFF_OACTIVE))
3617 (*ifp->if_start) (ifp);
3618 ifp->if_obytes += m->m_pkthdr.len + 3;
3619}
3620
3621/*
2520 * Flush interface queue.
2521 */
2522static void
2523sppp_qflush(struct ifqueue *ifq)
2524{
2525 struct mbuf *m, *n;
2526
2527 n = ifq->ifq_head;
2528 while ((m = n)) {
2529 n = m->m_act;
2530 m_freem (m);
2531 }
2532 ifq->ifq_head = 0;
2533 ifq->ifq_tail = 0;
2534 ifq->ifq_len = 0;
2535}
2536
2537/*
2538 * Send keepalive packets, every 10 seconds.
2539 */
2540static void
2541sppp_keepalive(void *dummy)
2542{
2543 struct sppp *sp;
2544 int s;
2545
2546 s = splimp();
2547 for (sp=spppq; sp; sp=sp->pp_next) {
2548 struct ifnet *ifp = &sp->pp_if;
2549
2550 /* Keepalive mode disabled or channel down? */
2551 if (! (sp->pp_flags & PP_KEEPALIVE) ||
2552 ! (ifp->if_flags & IFF_RUNNING))
2553 continue;
2554
2555 /* No keepalive in PPP mode if LCP not opened yet. */
2556 if (! (sp->pp_flags & PP_CISCO) &&
2557 sp->pp_phase < PHASE_AUTHENTICATE)
2558 continue;
2559
2560 if (sp->pp_alivecnt == MAXALIVECNT) {
2561 /* No keepalive packets got. Stop the interface. */
2562 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
2563 if_down (ifp);
2564 sppp_qflush (&sp->pp_cpq);
2565 if (! (sp->pp_flags & PP_CISCO)) {
2566 /* XXX */
2567 /* Shut down the PPP link. */
2568 lcp.Down(sp);
2569 /* Initiate negotiation. XXX */
2570 lcp.Up(sp);
2571 }
2572 }
2573 if (sp->pp_alivecnt <= MAXALIVECNT)
2574 ++sp->pp_alivecnt;
2575 if (sp->pp_flags & PP_CISCO)
2576 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
2577 sp->pp_rseq);
2578 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
2579 long nmagic = htonl (sp->lcp.magic);
2580 sp->lcp.echoid = ++sp->pp_seq;
2581 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
2582 sp->lcp.echoid, 4, &nmagic);
2583 }
2584 }
2585 splx(s);
2586 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
2587}
2588
2589/*
2590 * Get both IP addresses.
2591 */
2592static void
3622 * Flush interface queue.
3623 */
3624static void
3625sppp_qflush(struct ifqueue *ifq)
3626{
3627 struct mbuf *m, *n;
3628
3629 n = ifq->ifq_head;
3630 while ((m = n)) {
3631 n = m->m_act;
3632 m_freem (m);
3633 }
3634 ifq->ifq_head = 0;
3635 ifq->ifq_tail = 0;
3636 ifq->ifq_len = 0;
3637}
3638
3639/*
3640 * Send keepalive packets, every 10 seconds.
3641 */
3642static void
3643sppp_keepalive(void *dummy)
3644{
3645 struct sppp *sp;
3646 int s;
3647
3648 s = splimp();
3649 for (sp=spppq; sp; sp=sp->pp_next) {
3650 struct ifnet *ifp = &sp->pp_if;
3651
3652 /* Keepalive mode disabled or channel down? */
3653 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3654 ! (ifp->if_flags & IFF_RUNNING))
3655 continue;
3656
3657 /* No keepalive in PPP mode if LCP not opened yet. */
3658 if (! (sp->pp_flags & PP_CISCO) &&
3659 sp->pp_phase < PHASE_AUTHENTICATE)
3660 continue;
3661
3662 if (sp->pp_alivecnt == MAXALIVECNT) {
3663 /* No keepalive packets got. Stop the interface. */
3664 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
3665 if_down (ifp);
3666 sppp_qflush (&sp->pp_cpq);
3667 if (! (sp->pp_flags & PP_CISCO)) {
3668 /* XXX */
3669 /* Shut down the PPP link. */
3670 lcp.Down(sp);
3671 /* Initiate negotiation. XXX */
3672 lcp.Up(sp);
3673 }
3674 }
3675 if (sp->pp_alivecnt <= MAXALIVECNT)
3676 ++sp->pp_alivecnt;
3677 if (sp->pp_flags & PP_CISCO)
3678 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3679 sp->pp_rseq);
3680 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3681 long nmagic = htonl (sp->lcp.magic);
3682 sp->lcp.echoid = ++sp->pp_seq;
3683 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3684 sp->lcp.echoid, 4, &nmagic);
3685 }
3686 }
3687 splx(s);
3688 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
3689}
3690
3691/*
3692 * Get both IP addresses.
3693 */
3694static void
2593sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst)
3695sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
2594{
2595 struct ifnet *ifp = &sp->pp_if;
2596 struct ifaddr *ifa;
3696{
3697 struct ifnet *ifp = &sp->pp_if;
3698 struct ifaddr *ifa;
2597 struct sockaddr_in *si;
3699 struct sockaddr_in *si, *sm;
2598 u_long ssrc, ddst;
2599
2600 ssrc = ddst = 0L;
2601 /*
2602 * Pick the first AF_INET address from the list,
2603 * aliases don't make any sense on a p2p link anyway.
2604 */
2605 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3700 u_long ssrc, ddst;
3701
3702 ssrc = ddst = 0L;
3703 /*
3704 * Pick the first AF_INET address from the list,
3705 * aliases don't make any sense on a p2p link anyway.
3706 */
3707 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2606 ifa;
3708 ifa;
2607 ifa = ifa->ifa_link.tqe_next)
2608 if (ifa->ifa_addr->sa_family == AF_INET) {
2609 si = (struct sockaddr_in *)ifa->ifa_addr;
3709 ifa = ifa->ifa_link.tqe_next)
3710 if (ifa->ifa_addr->sa_family == AF_INET) {
3711 si = (struct sockaddr_in *)ifa->ifa_addr;
3712 sm = (struct sockaddr_in *)ifa->ifa_netmask;
2610 if (si)
2611 break;
2612 }
2613 if (ifa) {
3713 if (si)
3714 break;
3715 }
3716 if (ifa) {
2614 if (si && si->sin_addr.s_addr)
3717 if (si && si->sin_addr.s_addr) {
2615 ssrc = si->sin_addr.s_addr;
3718 ssrc = si->sin_addr.s_addr;
3719 if (srcmask)
3720 *srcmask = ntohl(sm->sin_addr.s_addr);
3721 }
2616
2617 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
2618 if (si && si->sin_addr.s_addr)
2619 ddst = si->sin_addr.s_addr;
2620 }
2621
2622 if (dst) *dst = ntohl(ddst);
2623 if (src) *src = ntohl(ssrc);
2624}
2625
2626/*
2627 * Set my IP address. Must be called at splimp.
2628 */
2629static void
2630sppp_set_ip_addr(struct sppp *sp, u_long src)
2631{
2632 struct ifnet *ifp = &sp->pp_if;
2633 struct ifaddr *ifa;
2634 struct sockaddr_in *si;
2635 u_long ssrc, ddst;
2636
2637 /*
2638 * Pick the first AF_INET address from the list,
2639 * aliases don't make any sense on a p2p link anyway.
2640 */
2641 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3722
3723 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3724 if (si && si->sin_addr.s_addr)
3725 ddst = si->sin_addr.s_addr;
3726 }
3727
3728 if (dst) *dst = ntohl(ddst);
3729 if (src) *src = ntohl(ssrc);
3730}
3731
3732/*
3733 * Set my IP address. Must be called at splimp.
3734 */
3735static void
3736sppp_set_ip_addr(struct sppp *sp, u_long src)
3737{
3738 struct ifnet *ifp = &sp->pp_if;
3739 struct ifaddr *ifa;
3740 struct sockaddr_in *si;
3741 u_long ssrc, ddst;
3742
3743 /*
3744 * Pick the first AF_INET address from the list,
3745 * aliases don't make any sense on a p2p link anyway.
3746 */
3747 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2642 ifa;
3748 ifa;
2643 ifa = ifa->ifa_link.tqe_next)
2644 if (ifa->ifa_addr->sa_family == AF_INET) {
2645 si = (struct sockaddr_in *)ifa->ifa_addr;
2646 if (si)
2647 break;
2648 }
2649 if (ifa && si)
2650 si->sin_addr.s_addr = htonl(src);
2651}
2652
3749 ifa = ifa->ifa_link.tqe_next)
3750 if (ifa->ifa_addr->sa_family == AF_INET) {
3751 si = (struct sockaddr_in *)ifa->ifa_addr;
3752 if (si)
3753 break;
3754 }
3755 if (ifa && si)
3756 si->sin_addr.s_addr = htonl(src);
3757}
3758
3759static int
3760sppp_params(struct sppp *sp, int cmd, void *data)
3761{
3762 int subcmd;
3763 struct ifreq *ifr = (struct ifreq *)data;
3764 struct spppreq spr;
3765
3766 /*
3767 * ifr->ifr_data is supposed to point to a struct spppreq.
3768 * Check the cmd word first before attempting to fetch all the
3769 * data.
3770 */
3771 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3772 return EFAULT;
3773
3774 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3775 return EFAULT;
3776
3777 switch (subcmd) {
3778 case SPPPIOGDEFS:
3779 if (cmd != SIOCGIFGENERIC)
3780 return EINVAL;
3781 /*
3782 * We copy over the entire current state, but clean
3783 * out some of the stuff we don't wanna pass up.
3784 * Remember, SIOCGIFGENERIC is unprotected, and can be
3785 * called by any user. No need to ever get PAP or
3786 * CHAP secrets back to userland anyway.
3787 */
3788 bcopy(sp, &spr.defs, sizeof(struct sppp));
3789 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3790 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3791 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3792 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3793 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3794
3795 case SPPPIOSDEFS:
3796 if (cmd != SIOCSIFGENERIC)
3797 return EINVAL;
3798 /*
3799 * We have a very specific idea of which fields we allow
3800 * being passed back from userland, so to not clobber our
3801 * current state. For one, we only allow setting
3802 * anything if LCP is in dead phase. Once the LCP
3803 * negotiations started, the authentication settings must
3804 * not be changed again. (The administrator can force an
3805 * ifconfig down in order to get LCP back into dead
3806 * phase.)
3807 *
3808 * Also, we only allow for authentication parameters to be
3809 * specified.
3810 *
3811 * XXX Should allow to set or clear pp_flags.
3812 *
3813 * Finally, if the respective authentication protocol to
3814 * be used is set differently than 0, but the secret is
3815 * passed as all zeros, we don't trash the existing secret.
3816 * This allows an administrator to change the system name
3817 * only without clobbering the secret (which he didn't get
3818 * back in a previous SPPPIOGDEFS call). However, the
3819 * secrets are cleared if the authentication protocol is
3820 * reset to 0.
3821 */
3822 if (sp->pp_phase != PHASE_DEAD)
3823 return EBUSY;
3824
3825 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
3826 spr.defs.myauth.proto != PPP_CHAP) ||
3827 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
3828 spr.defs.hisauth.proto != PPP_CHAP))
3829 return EINVAL;
3830
3831 if (spr.defs.myauth.proto == 0)
3832 /* resetting myauth */
3833 bzero(&sp->myauth, sizeof sp->myauth);
3834 else {
3835 /* setting/changing myauth */
3836 sp->myauth.proto = spr.defs.myauth.proto;
3837 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
3838 if (spr.defs.myauth.secret[0] != '\0')
3839 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
3840 AUTHKEYLEN);
3841 }
3842 if (spr.defs.hisauth.proto == 0)
3843 /* resetting hisauth */
3844 bzero(&sp->hisauth, sizeof sp->hisauth);
3845 else {
3846 /* setting/changing hisauth */
3847 sp->hisauth.proto = spr.defs.hisauth.proto;
3848 sp->hisauth.flags = spr.defs.hisauth.flags;
3849 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
3850 if (spr.defs.hisauth.secret[0] != '\0')
3851 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
3852 AUTHKEYLEN);
3853 }
3854 break;
3855
3856 default:
3857 return EINVAL;
3858 }
3859
3860 return 0;
3861}
3862
3863static void
3864sppp_phase_network(struct sppp *sp)
3865{
3866 struct ifnet *ifp = &sp->pp_if;
3867 int i;
3868 u_long mask;
3869
3870 sp->pp_phase = PHASE_NETWORK;
3871
3872 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
3873 sppp_phase_name(sp->pp_phase));
3874
3875 /* Notify NCPs now. */
3876 for (i = 0; i < IDX_COUNT; i++)
3877 if ((cps[i])->flags & CP_NCP)
3878 (cps[i])->Open(sp);
3879
3880 /* Send Up events to all NCPs. */
3881 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
3882 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
3883 (cps[i])->Up(sp);
3884
3885 /* if no NCP is starting, all this was in vain, close down */
3886 sppp_lcp_check_and_close(sp);
3887}
3888
3889
2653static const char *
2654sppp_cp_type_name(u_char type)
2655{
3890static const char *
3891sppp_cp_type_name(u_char type)
3892{
2656 static char buf [12];
3893 static char buf[12];
2657 switch (type) {
3894 switch (type) {
2658 case CONF_REQ: return ("conf-req");
2659 case CONF_ACK: return ("conf-ack");
2660 case CONF_NAK: return ("conf-nak");
2661 case CONF_REJ: return ("conf-rej");
2662 case TERM_REQ: return ("term-req");
2663 case TERM_ACK: return ("term-ack");
2664 case CODE_REJ: return ("code-rej");
2665 case PROTO_REJ: return ("proto-rej");
2666 case ECHO_REQ: return ("echo-req");
2667 case ECHO_REPLY: return ("echo-reply");
2668 case DISC_REQ: return ("discard-req");
3895 case CONF_REQ: return "conf-req";
3896 case CONF_ACK: return "conf-ack";
3897 case CONF_NAK: return "conf-nak";
3898 case CONF_REJ: return "conf-rej";
3899 case TERM_REQ: return "term-req";
3900 case TERM_ACK: return "term-ack";
3901 case CODE_REJ: return "code-rej";
3902 case PROTO_REJ: return "proto-rej";
3903 case ECHO_REQ: return "echo-req";
3904 case ECHO_REPLY: return "echo-reply";
3905 case DISC_REQ: return "discard-req";
2669 }
2670 sprintf (buf, "0x%x", type);
3906 }
3907 sprintf (buf, "0x%x", type);
2671 return (buf);
3908 return buf;
2672}
2673
2674static const char *
3909}
3910
3911static const char *
3912sppp_auth_type_name(u_short proto, u_char type)
3913{
3914 static char buf[12];
3915 switch (proto) {
3916 case PPP_CHAP:
3917 switch (type) {
3918 case CHAP_CHALLENGE: return "challenge";
3919 case CHAP_RESPONSE: return "response";
3920 case CHAP_SUCCESS: return "success";
3921 case CHAP_FAILURE: return "failure";
3922 }
3923 case PPP_PAP:
3924 switch (type) {
3925 case PAP_REQ: return "req";
3926 case PAP_ACK: return "ack";
3927 case PAP_NAK: return "nak";
3928 }
3929 }
3930 sprintf (buf, "0x%x", type);
3931 return buf;
3932}
3933
3934static const char *
2675sppp_lcp_opt_name(u_char opt)
2676{
3935sppp_lcp_opt_name(u_char opt)
3936{
2677 static char buf [12];
3937 static char buf[12];
2678 switch (opt) {
3938 switch (opt) {
2679 case LCP_OPT_MRU: return ("mru");
2680 case LCP_OPT_ASYNC_MAP: return ("async-map");
2681 case LCP_OPT_AUTH_PROTO: return ("auth-proto");
2682 case LCP_OPT_QUAL_PROTO: return ("qual-proto");
2683 case LCP_OPT_MAGIC: return ("magic");
2684 case LCP_OPT_PROTO_COMP: return ("proto-comp");
2685 case LCP_OPT_ADDR_COMP: return ("addr-comp");
3939 case LCP_OPT_MRU: return "mru";
3940 case LCP_OPT_ASYNC_MAP: return "async-map";
3941 case LCP_OPT_AUTH_PROTO: return "auth-proto";
3942 case LCP_OPT_QUAL_PROTO: return "qual-proto";
3943 case LCP_OPT_MAGIC: return "magic";
3944 case LCP_OPT_PROTO_COMP: return "proto-comp";
3945 case LCP_OPT_ADDR_COMP: return "addr-comp";
2686 }
2687 sprintf (buf, "0x%x", opt);
3946 }
3947 sprintf (buf, "0x%x", opt);
2688 return (buf);
3948 return buf;
2689}
2690
2691static const char *
2692sppp_ipcp_opt_name(u_char opt)
2693{
3949}
3950
3951static const char *
3952sppp_ipcp_opt_name(u_char opt)
3953{
2694 static char buf [12];
3954 static char buf[12];
2695 switch (opt) {
3955 switch (opt) {
2696 case IPCP_OPT_ADDRESSES: return ("addresses");
2697 case IPCP_OPT_COMPRESSION: return ("compression");
2698 case IPCP_OPT_ADDRESS: return ("address");
3956 case IPCP_OPT_ADDRESSES: return "addresses";
3957 case IPCP_OPT_COMPRESSION: return "compression";
3958 case IPCP_OPT_ADDRESS: return "address";
2699 }
2700 sprintf (buf, "0x%x", opt);
3959 }
3960 sprintf (buf, "0x%x", opt);
2701 return (buf);
3961 return buf;
2702}
2703
2704static const char *
2705sppp_state_name(int state)
2706{
2707 switch (state) {
2708 case STATE_INITIAL: return "initial";
2709 case STATE_STARTING: return "starting";
2710 case STATE_CLOSED: return "closed";
2711 case STATE_STOPPED: return "stopped";
2712 case STATE_CLOSING: return "closing";
2713 case STATE_STOPPING: return "stopping";
2714 case STATE_REQ_SENT: return "req-sent";
2715 case STATE_ACK_RCVD: return "ack-rcvd";
2716 case STATE_ACK_SENT: return "ack-sent";
2717 case STATE_OPENED: return "opened";
2718 }
2719 return "illegal";
2720}
2721
2722static const char *
2723sppp_phase_name(enum ppp_phase phase)
2724{
2725 switch (phase) {
2726 case PHASE_DEAD: return "dead";
2727 case PHASE_ESTABLISH: return "establish";
2728 case PHASE_TERMINATE: return "terminate";
2729 case PHASE_AUTHENTICATE: return "authenticate";
2730 case PHASE_NETWORK: return "network";
2731 }
2732 return "illegal";
2733}
2734
2735static const char *
2736sppp_proto_name(u_short proto)
2737{
2738 static char buf[12];
2739 switch (proto) {
2740 case PPP_LCP: return "lcp";
2741 case PPP_IPCP: return "ipcp";
3962}
3963
3964static const char *
3965sppp_state_name(int state)
3966{
3967 switch (state) {
3968 case STATE_INITIAL: return "initial";
3969 case STATE_STARTING: return "starting";
3970 case STATE_CLOSED: return "closed";
3971 case STATE_STOPPED: return "stopped";
3972 case STATE_CLOSING: return "closing";
3973 case STATE_STOPPING: return "stopping";
3974 case STATE_REQ_SENT: return "req-sent";
3975 case STATE_ACK_RCVD: return "ack-rcvd";
3976 case STATE_ACK_SENT: return "ack-sent";
3977 case STATE_OPENED: return "opened";
3978 }
3979 return "illegal";
3980}
3981
3982static const char *
3983sppp_phase_name(enum ppp_phase phase)
3984{
3985 switch (phase) {
3986 case PHASE_DEAD: return "dead";
3987 case PHASE_ESTABLISH: return "establish";
3988 case PHASE_TERMINATE: return "terminate";
3989 case PHASE_AUTHENTICATE: return "authenticate";
3990 case PHASE_NETWORK: return "network";
3991 }
3992 return "illegal";
3993}
3994
3995static const char *
3996sppp_proto_name(u_short proto)
3997{
3998 static char buf[12];
3999 switch (proto) {
4000 case PPP_LCP: return "lcp";
4001 case PPP_IPCP: return "ipcp";
4002 case PPP_PAP: return "pap";
4003 case PPP_CHAP: return "chap";
2742 }
2743 sprintf(buf, "0x%x", (unsigned)proto);
2744 return buf;
2745}
2746
2747static void
4004 }
4005 sprintf(buf, "0x%x", (unsigned)proto);
4006 return buf;
4007}
4008
4009static void
2748sppp_print_bytes(u_char *p, u_short len)
4010sppp_print_bytes(const u_char *p, u_short len)
2749{
2750 addlog(" %x", *p++);
2751 while (--len > 0)
2752 addlog("-%x", *p++);
2753}
2754
4011{
4012 addlog(" %x", *p++);
4013 while (--len > 0)
4014 addlog("-%x", *p++);
4015}
4016
4017static void
4018sppp_print_string(const char *p, u_short len)
4019{
4020 u_char c;
4021
4022 while (len-- > 0) {
4023 c = *p++;
4024 /*
4025 * Print only ASCII chars directly. RFC 1994 recommends
4026 * using only them, but we don't rely on it. */
4027 if (c < ' ' || c > '~')
4028 addlog("\\x%x", c);
4029 else
4030 addlog("%c", c);
4031 }
4032}
4033
4034static const char *
4035sppp_dotted_quad(u_long addr)
4036{
4037 static char s[16];
4038 sprintf(s, "%d.%d.%d.%d",
4039 (addr >> 24) & 0xff,
4040 (addr >> 16) & 0xff,
4041 (addr >> 8) & 0xff,
4042 addr & 0xff);
4043 return s;
4044}
4045
4046static int
4047sppp_strnlen(u_char *p, int max)
4048{
4049 int len;
4050
4051 for (len = 0; len < max && *p; ++p)
4052 ++len;
4053 return len;
4054}
4055
4056/* a dummy, used to drop uninteresting events */
4057static void
4058sppp_null(struct sppp *unused)
4059{
4060 /* do just nothing */
4061}
2755/*
2756 * This file is large. Tell emacs to highlight it nevertheless.
2757 *
2758 * Local Variables:
4062/*
4063 * This file is large. Tell emacs to highlight it nevertheless.
4064 *
4065 * Local Variables:
2759 * hilit-auto-highlight-maxout: 100000
4066 * hilit-auto-highlight-maxout: 120000
2760 * End:
2761 */
4067 * End:
4068 */