if_spppsubr.c revision 1.94 1 /* $NetBSD: if_spppsubr.c,v 1.94 2006/07/13 23:43:13 martin Exp $ */
2
3 /*
4 * Synchronous PPP/Cisco link level subroutines.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
6 *
7 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
8 * Author: Serge Vakulenko, <vak (at) cronyx.ru>
9 *
10 * Heavily revamped to conform to RFC 1661.
11 * Copyright (C) 1997, Joerg Wunsch.
12 *
13 * RFC2472 IPv6CP support.
14 * Copyright (C) 2000, Jun-ichiro itojun Hagino <itojun (at) iijlab.net>.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
37 *
38 * From: if_spppsubr.c,v 1.39 1998/04/04 13:26:03 phk Exp
39 *
40 * From: Id: if_spppsubr.c,v 1.23 1999/02/23 14:47:50 hm Exp
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.94 2006/07/13 23:43:13 martin Exp $");
45
46 #include "opt_inet.h"
47 #include "opt_ipx.h"
48 #include "opt_iso.h"
49 #include "opt_ns.h"
50 #include "opt_pfil_hooks.h"
51
52 #include <sys/param.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/sockio.h>
57 #include <sys/socket.h>
58 #include <sys/syslog.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/callout.h>
62 #include <sys/md5.h>
63 #include <sys/inttypes.h>
64 #include <sys/kauth.h>
65
66 #include <net/if.h>
67 #include <net/netisr.h>
68 #include <net/if_types.h>
69 #include <net/route.h>
70 #include <net/ppp_defs.h>
71
72 #include <machine/stdarg.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #ifdef INET
78 #include <netinet/ip.h>
79 #include <netinet/tcp.h>
80 #endif
81 #include <net/ethertypes.h>
82
83 #ifdef INET6
84 #include <netinet6/scope6_var.h>
85 #endif
86
87 #ifdef IPX
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
90 #endif
91
92 #ifdef NS
93 #include <netns/ns.h>
94 #include <netns/ns_if.h>
95 #endif
96
97 #ifdef ISO
98 #include <netiso/argo_debug.h>
99 #include <netiso/iso.h>
100 #include <netiso/iso_var.h>
101 #include <netiso/iso_snpac.h>
102 #endif
103
104 #include <net/if_sppp.h>
105 #include <net/if_spppvar.h>
106
107 #define LCP_KEEPALIVE_INTERVAL 10 /* seconds between checks */
108 #define LOOPALIVECNT 3 /* loopback detection tries */
109 #define DEFAULT_MAXALIVECNT 3 /* max. missed alive packets */
110 #define DEFAULT_NORECV_TIME 15 /* before we get worried */
111 #define DEFAULT_MAX_AUTH_FAILURES 5 /* max. auth. failures */
112
113 /*
114 * Interface flags that can be set in an ifconfig command.
115 *
116 * Setting link0 will make the link passive, i.e. it will be marked
117 * as being administrative openable, but won't be opened to begin
118 * with. Incoming calls will be answered, or subsequent calls with
119 * -link1 will cause the administrative open of the LCP layer.
120 *
121 * Setting link1 will cause the link to auto-dial only as packets
122 * arrive to be sent.
123 *
124 * Setting IFF_DEBUG will syslog the option negotiation and state
125 * transitions at level kern.debug. Note: all logs consistently look
126 * like
127 *
128 * <if-name><unit>: <proto-name> <additional info...>
129 *
130 * with <if-name><unit> being something like "bppp0", and <proto-name>
131 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
132 */
133
134 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
135 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
136
137 #define CONF_REQ 1 /* PPP configure request */
138 #define CONF_ACK 2 /* PPP configure acknowledge */
139 #define CONF_NAK 3 /* PPP configure negative ack */
140 #define CONF_REJ 4 /* PPP configure reject */
141 #define TERM_REQ 5 /* PPP terminate request */
142 #define TERM_ACK 6 /* PPP terminate acknowledge */
143 #define CODE_REJ 7 /* PPP code reject */
144 #define PROTO_REJ 8 /* PPP protocol reject */
145 #define ECHO_REQ 9 /* PPP echo request */
146 #define ECHO_REPLY 10 /* PPP echo reply */
147 #define DISC_REQ 11 /* PPP discard request */
148
149 #define LCP_OPT_MRU 1 /* maximum receive unit */
150 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
151 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
152 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
153 #define LCP_OPT_MAGIC 5 /* magic number */
154 #define LCP_OPT_RESERVED 6 /* reserved */
155 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
156 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
157
158 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
159 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol */
160 #define IPCP_OPT_ADDRESS 3 /* local IP address */
161 #define IPCP_OPT_PRIMDNS 129 /* primary remote dns address */
162 #define IPCP_OPT_SECDNS 131 /* secondary remote dns address */
163
164 #define IPV6CP_OPT_IFID 1 /* interface identifier */
165 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
166
167 #define PAP_REQ 1 /* PAP name/password request */
168 #define PAP_ACK 2 /* PAP acknowledge */
169 #define PAP_NAK 3 /* PAP fail */
170
171 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
172 #define CHAP_RESPONSE 2 /* CHAP challenge response */
173 #define CHAP_SUCCESS 3 /* CHAP response ok */
174 #define CHAP_FAILURE 4 /* CHAP response failed */
175
176 #define CHAP_MD5 5 /* hash algorithm - MD5 */
177
178 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
179 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
180 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
181 #define CISCO_ADDR_REQ 0 /* Cisco address request */
182 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
183 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
184
185 /* states are named and numbered according to RFC 1661 */
186 #define STATE_INITIAL 0
187 #define STATE_STARTING 1
188 #define STATE_CLOSED 2
189 #define STATE_STOPPED 3
190 #define STATE_CLOSING 4
191 #define STATE_STOPPING 5
192 #define STATE_REQ_SENT 6
193 #define STATE_ACK_RCVD 7
194 #define STATE_ACK_SENT 8
195 #define STATE_OPENED 9
196
197 struct ppp_header {
198 u_int8_t address;
199 u_int8_t control;
200 u_int16_t protocol;
201 } __attribute__((__packed__));
202 #define PPP_HEADER_LEN sizeof (struct ppp_header)
203
204 struct lcp_header {
205 u_int8_t type;
206 u_int8_t ident;
207 u_int16_t len;
208 } __attribute__((__packed__));
209 #define LCP_HEADER_LEN sizeof (struct lcp_header)
210
211 struct cisco_packet {
212 u_int32_t type;
213 u_int32_t par1;
214 u_int32_t par2;
215 u_int16_t rel;
216 u_int16_t time0;
217 u_int16_t time1;
218 } __attribute__((__packed__));
219 #define CISCO_PACKET_LEN 18
220
221 /*
222 * We follow the spelling and capitalization of RFC 1661 here, to make
223 * it easier comparing with the standard. Please refer to this RFC in
224 * case you can't make sense out of these abbreviation; it will also
225 * explain the semantics related to the various events and actions.
226 */
227 struct cp {
228 u_short proto; /* PPP control protocol number */
229 u_char protoidx; /* index into state table in struct sppp */
230 u_char flags;
231 #define CP_LCP 0x01 /* this is the LCP */
232 #define CP_AUTH 0x02 /* this is an authentication protocol */
233 #define CP_NCP 0x04 /* this is a NCP */
234 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
235 const char *name; /* name of this control protocol */
236 /* event handlers */
237 void (*Up)(struct sppp *sp);
238 void (*Down)(struct sppp *sp);
239 void (*Open)(struct sppp *sp);
240 void (*Close)(struct sppp *sp);
241 void (*TO)(void *sp);
242 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
243 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
244 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
245 /* actions */
246 void (*tlu)(struct sppp *sp);
247 void (*tld)(struct sppp *sp);
248 void (*tls)(struct sppp *sp);
249 void (*tlf)(struct sppp *sp);
250 void (*scr)(struct sppp *sp);
251 };
252
253 static struct sppp *spppq;
254 static struct callout keepalive_ch;
255
256 #ifdef INET
257 /*
258 * The following disgusting hack gets around the problem that IP TOS
259 * can't be set yet. We want to put "interactive" traffic on a high
260 * priority queue. To decide if traffic is interactive, we check that
261 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
262 *
263 * XXX is this really still necessary? - joerg -
264 */
265 static u_short interactive_ports[8] = {
266 0, 513, 0, 0,
267 0, 21, 0, 23,
268 };
269 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
270 #endif
271
272 /* almost every function needs these */
273 #define STDDCL \
274 struct ifnet *ifp = &sp->pp_if; \
275 int debug = ifp->if_flags & IFF_DEBUG
276
277 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
278 struct sockaddr *dst, struct rtentry *rt);
279
280 static void sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2);
281 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
282
283 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
284 struct mbuf *m);
285 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
286 u_char ident, u_short len, void *data);
287 /* static void sppp_cp_timeout(void *arg); */
288 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
289 int newstate);
290 static void sppp_auth_send(const struct cp *cp,
291 struct sppp *sp, unsigned int type, unsigned int id,
292 ...);
293
294 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
295 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
296 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
297 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
298 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
299
300 static void sppp_null(struct sppp *sp);
301
302 static void sppp_lcp_init(struct sppp *sp);
303 static void sppp_lcp_up(struct sppp *sp);
304 static void sppp_lcp_down(struct sppp *sp);
305 static void sppp_lcp_open(struct sppp *sp);
306 static void sppp_lcp_close(struct sppp *sp);
307 static void sppp_lcp_TO(void *sp);
308 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
309 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
310 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
311 static void sppp_lcp_tlu(struct sppp *sp);
312 static void sppp_lcp_tld(struct sppp *sp);
313 static void sppp_lcp_tls(struct sppp *sp);
314 static void sppp_lcp_tlf(struct sppp *sp);
315 static void sppp_lcp_scr(struct sppp *sp);
316 static void sppp_lcp_check_and_close(struct sppp *sp);
317 static int sppp_ncp_check(struct sppp *sp);
318
319 static void sppp_ipcp_init(struct sppp *sp);
320 static void sppp_ipcp_up(struct sppp *sp);
321 static void sppp_ipcp_down(struct sppp *sp);
322 static void sppp_ipcp_open(struct sppp *sp);
323 static void sppp_ipcp_close(struct sppp *sp);
324 static void sppp_ipcp_TO(void *sp);
325 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
326 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
327 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
328 static void sppp_ipcp_tlu(struct sppp *sp);
329 static void sppp_ipcp_tld(struct sppp *sp);
330 static void sppp_ipcp_tls(struct sppp *sp);
331 static void sppp_ipcp_tlf(struct sppp *sp);
332 static void sppp_ipcp_scr(struct sppp *sp);
333
334 static void sppp_ipv6cp_init(struct sppp *sp);
335 static void sppp_ipv6cp_up(struct sppp *sp);
336 static void sppp_ipv6cp_down(struct sppp *sp);
337 static void sppp_ipv6cp_open(struct sppp *sp);
338 static void sppp_ipv6cp_close(struct sppp *sp);
339 static void sppp_ipv6cp_TO(void *sp);
340 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
341 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
342 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
343 static void sppp_ipv6cp_tlu(struct sppp *sp);
344 static void sppp_ipv6cp_tld(struct sppp *sp);
345 static void sppp_ipv6cp_tls(struct sppp *sp);
346 static void sppp_ipv6cp_tlf(struct sppp *sp);
347 static void sppp_ipv6cp_scr(struct sppp *sp);
348
349 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
350 static void sppp_pap_init(struct sppp *sp);
351 static void sppp_pap_open(struct sppp *sp);
352 static void sppp_pap_close(struct sppp *sp);
353 static void sppp_pap_TO(void *sp);
354 static void sppp_pap_my_TO(void *sp);
355 static void sppp_pap_tlu(struct sppp *sp);
356 static void sppp_pap_tld(struct sppp *sp);
357 static void sppp_pap_scr(struct sppp *sp);
358
359 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
360 static void sppp_chap_init(struct sppp *sp);
361 static void sppp_chap_open(struct sppp *sp);
362 static void sppp_chap_close(struct sppp *sp);
363 static void sppp_chap_TO(void *sp);
364 static void sppp_chap_tlu(struct sppp *sp);
365 static void sppp_chap_tld(struct sppp *sp);
366 static void sppp_chap_scr(struct sppp *sp);
367
368 static const char *sppp_auth_type_name(u_short proto, u_char type);
369 static const char *sppp_cp_type_name(u_char type);
370 static const char *sppp_dotted_quad(u_int32_t addr);
371 static const char *sppp_ipcp_opt_name(u_char opt);
372 #ifdef INET6
373 static const char *sppp_ipv6cp_opt_name(u_char opt);
374 #endif
375 static const char *sppp_lcp_opt_name(u_char opt);
376 static const char *sppp_phase_name(int phase);
377 static const char *sppp_proto_name(u_short proto);
378 static const char *sppp_state_name(int state);
379 static int sppp_params(struct sppp *sp, int cmd, void *data);
380 #ifdef INET
381 static void sppp_get_ip_addrs(struct sppp *sp, u_int32_t *src, u_int32_t *dst,
382 u_int32_t *srcmask);
383 static void sppp_set_ip_addrs(struct sppp *sp, u_int32_t myaddr, u_int32_t hisaddr);
384 static void sppp_clear_ip_addrs(struct sppp *sp);
385 #endif
386 static void sppp_keepalive(void *dummy);
387 static void sppp_phase_network(struct sppp *sp);
388 static void sppp_print_bytes(const u_char *p, u_short len);
389 static void sppp_print_string(const char *p, u_short len);
390 #ifdef INET6
391 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
392 struct in6_addr *dst, struct in6_addr *srcmask);
393 #ifdef IPV6CP_MYIFID_DYN
394 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
395 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
396 #endif
397 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
398 #endif
399
400 /* our control protocol descriptors */
401 static const struct cp lcp = {
402 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
403 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
404 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
405 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
406 sppp_lcp_scr
407 };
408
409 static const struct cp ipcp = {
410 PPP_IPCP, IDX_IPCP,
411 #ifdef INET
412 CP_NCP, /*don't run IPCP if there's no IPv4 support*/
413 #else
414 0,
415 #endif
416 "ipcp",
417 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
418 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
419 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
420 sppp_ipcp_scr
421 };
422
423 static const struct cp ipv6cp = {
424 PPP_IPV6CP, IDX_IPV6CP,
425 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
426 CP_NCP,
427 #else
428 0,
429 #endif
430 "ipv6cp",
431 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
432 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
433 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
434 sppp_ipv6cp_scr
435 };
436
437 static const struct cp pap = {
438 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
439 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
440 sppp_pap_TO, 0, 0, 0,
441 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
442 sppp_pap_scr
443 };
444
445 static const struct cp chap = {
446 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
447 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
448 sppp_chap_TO, 0, 0, 0,
449 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
450 sppp_chap_scr
451 };
452
453 static const struct cp *cps[IDX_COUNT] = {
454 &lcp, /* IDX_LCP */
455 &ipcp, /* IDX_IPCP */
456 &ipv6cp, /* IDX_IPV6CP */
457 &pap, /* IDX_PAP */
458 &chap, /* IDX_CHAP */
459 };
460
461
462 void spppattach(int);
463 void
464 /*ARGSUSED*/
465 spppattach(int count)
466 {
467 }
468
469 /*
470 * Exported functions, comprising our interface to the lower layer.
471 */
472
473 /*
474 * Process the received packet.
475 */
476 void
477 sppp_input(struct ifnet *ifp, struct mbuf *m)
478 {
479 struct ppp_header *h = NULL;
480 struct ifqueue *inq = 0;
481 u_int16_t protocol;
482 int s;
483 struct sppp *sp = (struct sppp *)ifp;
484 int debug = ifp->if_flags & IFF_DEBUG;
485
486 if (ifp->if_flags & IFF_UP) {
487 /* Count received bytes, add hardware framing */
488 ifp->if_ibytes += m->m_pkthdr.len + sp->pp_framebytes;
489 /* Note time of last receive */
490 sp->pp_last_receive = time_uptime;
491 }
492
493 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
494 /* Too small packet, drop it. */
495 if (debug)
496 log(LOG_DEBUG,
497 "%s: input packet is too small, %d bytes\n",
498 ifp->if_xname, m->m_pkthdr.len);
499 drop:
500 ++ifp->if_ierrors;
501 ++ifp->if_iqdrops;
502 m_freem(m);
503 return;
504 }
505
506 if (sp->pp_flags & PP_NOFRAMING) {
507 memcpy(&protocol, mtod(m, void *), 2);
508 protocol = ntohs(protocol);
509 m_adj(m, 2);
510 } else {
511
512 /* Get PPP header. */
513 h = mtod(m, struct ppp_header *);
514 m_adj(m, PPP_HEADER_LEN);
515
516 switch (h->address) {
517 case PPP_ALLSTATIONS:
518 if (h->control != PPP_UI)
519 goto invalid;
520 if (sp->pp_flags & PP_CISCO) {
521 if (debug)
522 log(LOG_DEBUG,
523 "%s: PPP packet in Cisco mode "
524 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
525 ifp->if_xname,
526 h->address, h->control, ntohs(h->protocol));
527 goto drop;
528 }
529 break;
530 case CISCO_MULTICAST:
531 case CISCO_UNICAST:
532 /* Don't check the control field here (RFC 1547). */
533 if (! (sp->pp_flags & PP_CISCO)) {
534 if (debug)
535 log(LOG_DEBUG,
536 "%s: Cisco packet in PPP mode "
537 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
538 ifp->if_xname,
539 h->address, h->control, ntohs(h->protocol));
540 goto drop;
541 }
542 switch (ntohs(h->protocol)) {
543 default:
544 ++ifp->if_noproto;
545 goto invalid;
546 case CISCO_KEEPALIVE:
547 sppp_cisco_input((struct sppp *) ifp, m);
548 m_freem(m);
549 return;
550 #ifdef INET
551 case ETHERTYPE_IP:
552 schednetisr(NETISR_IP);
553 inq = &ipintrq;
554 break;
555 #endif
556 #ifdef INET6
557 case ETHERTYPE_IPV6:
558 schednetisr(NETISR_IPV6);
559 inq = &ip6intrq;
560 break;
561 #endif
562 #ifdef IPX
563 case ETHERTYPE_IPX:
564 schednetisr(NETISR_IPX);
565 inq = &ipxintrq;
566 break;
567 #endif
568 #ifdef NS
569 case ETHERTYPE_NS:
570 schednetisr(NETISR_NS);
571 inq = &nsintrq;
572 break;
573 #endif
574 }
575 goto queue_pkt;
576 default: /* Invalid PPP packet. */
577 invalid:
578 if (debug)
579 log(LOG_DEBUG,
580 "%s: invalid input packet "
581 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
582 ifp->if_xname,
583 h->address, h->control, ntohs(h->protocol));
584 goto drop;
585 }
586 protocol = ntohs(h->protocol);
587 }
588
589 switch (protocol) {
590 default:
591 if (sp->state[IDX_LCP] == STATE_OPENED) {
592 u_int16_t prot = htons(protocol);
593 sppp_cp_send(sp, PPP_LCP, PROTO_REJ,
594 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
595 &prot);
596 }
597 if (debug)
598 log(LOG_DEBUG,
599 "%s: invalid input protocol "
600 "<proto=0x%x>\n", ifp->if_xname, ntohs(protocol));
601 ++ifp->if_noproto;
602 goto drop;
603 case PPP_LCP:
604 sppp_cp_input(&lcp, sp, m);
605 m_freem(m);
606 return;
607 case PPP_PAP:
608 if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE)
609 sppp_pap_input(sp, m);
610 m_freem(m);
611 return;
612 case PPP_CHAP:
613 if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE)
614 sppp_chap_input(sp, m);
615 m_freem(m);
616 return;
617 #ifdef INET
618 case PPP_IPCP:
619 if (sp->pp_phase == SPPP_PHASE_NETWORK)
620 sppp_cp_input(&ipcp, sp, m);
621 m_freem(m);
622 return;
623 case PPP_IP:
624 if (sp->state[IDX_IPCP] == STATE_OPENED) {
625 schednetisr(NETISR_IP);
626 inq = &ipintrq;
627 sp->pp_last_activity = time_uptime;
628 }
629 break;
630 #endif
631 #ifdef INET6
632 case PPP_IPV6CP:
633 if (sp->pp_phase == SPPP_PHASE_NETWORK)
634 sppp_cp_input(&ipv6cp, sp, m);
635 m_freem(m);
636 return;
637
638 case PPP_IPV6:
639 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
640 schednetisr(NETISR_IPV6);
641 inq = &ip6intrq;
642 sp->pp_last_activity = time_uptime;
643 }
644 break;
645 #endif
646 #ifdef IPX
647 case PPP_IPX:
648 /* IPX IPXCP not implemented yet */
649 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
650 schednetisr(NETISR_IPX);
651 inq = &ipxintrq;
652 }
653 break;
654 #endif
655 #ifdef NS
656 case PPP_XNS:
657 /* XNS IDPCP not implemented yet */
658 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
659 schednetisr(NETISR_NS);
660 inq = &nsintrq;
661 }
662 break;
663 #endif
664 #ifdef ISO
665 case PPP_ISO:
666 /* OSI NLCP not implemented yet */
667 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
668 schednetisr(NETISR_ISO);
669 inq = &clnlintrq;
670 }
671 break;
672 #endif
673 }
674
675 queue_pkt:
676 if (! (ifp->if_flags & IFF_UP) || ! inq)
677 goto drop;
678
679 /* Check queue. */
680 s = splnet();
681 if (IF_QFULL(inq)) {
682 /* Queue overflow. */
683 IF_DROP(inq);
684 splx(s);
685 if (debug)
686 log(LOG_DEBUG, "%s: protocol queue overflow\n",
687 ifp->if_xname);
688 goto drop;
689 }
690 IF_ENQUEUE(inq, m);
691 splx(s);
692 }
693
694 /*
695 * Enqueue transmit packet.
696 */
697 static int
698 sppp_output(struct ifnet *ifp, struct mbuf *m,
699 struct sockaddr *dst, struct rtentry *rt)
700 {
701 struct sppp *sp = (struct sppp *) ifp;
702 struct ppp_header *h = NULL;
703 struct ifqueue *ifq = NULL; /* XXX */
704 int s, error = 0;
705 u_int16_t protocol;
706 ALTQ_DECL(struct altq_pktattr pktattr;)
707
708 s = splnet();
709
710 sp->pp_last_activity = time_uptime;
711
712 if ((ifp->if_flags & IFF_UP) == 0 ||
713 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
714 m_freem(m);
715 splx(s);
716 return (ENETDOWN);
717 }
718
719 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
720 /*
721 * Interface is not yet running, but auto-dial. Need
722 * to start LCP for it.
723 */
724 ifp->if_flags |= IFF_RUNNING;
725 splx(s);
726 lcp.Open(sp);
727 s = splnet();
728 }
729
730 /*
731 * If the queueing discipline needs packet classification,
732 * do it before prepending link headers.
733 */
734 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
735
736 #ifdef INET
737 if (dst->sa_family == AF_INET) {
738 struct ip *ip = NULL;
739 struct tcphdr *th = NULL;
740
741 if (m->m_len >= sizeof(struct ip)) {
742 ip = mtod(m, struct ip *);
743 if (ip->ip_p == IPPROTO_TCP &&
744 m->m_len >= sizeof(struct ip) + (ip->ip_hl << 2) +
745 sizeof(struct tcphdr)) {
746 th = (struct tcphdr *)
747 ((caddr_t)ip + (ip->ip_hl << 2));
748 }
749 } else
750 ip = NULL;
751
752 /*
753 * When using dynamic local IP address assignment by using
754 * 0.0.0.0 as a local address, the first TCP session will
755 * not connect because the local TCP checksum is computed
756 * using 0.0.0.0 which will later become our real IP address
757 * so the TCP checksum computed at the remote end will
758 * become invalid. So we
759 * - don't let packets with src ip addr 0 thru
760 * - we flag TCP packets with src ip 0 as an error
761 */
762 if (ip && ip->ip_src.s_addr == INADDR_ANY) {
763 u_int8_t proto = ip->ip_p;
764
765 m_freem(m);
766 splx(s);
767 if (proto == IPPROTO_TCP)
768 return (EADDRNOTAVAIL);
769 else
770 return (0);
771 }
772
773 /*
774 * Put low delay, telnet, rlogin and ftp control packets
775 * in front of the queue.
776 */
777
778 if (!IF_QFULL(&sp->pp_fastq) &&
779 ((ip && (ip->ip_tos & IPTOS_LOWDELAY)) ||
780 (th && (INTERACTIVE(ntohs(th->th_sport)) ||
781 INTERACTIVE(ntohs(th->th_dport))))))
782 ifq = &sp->pp_fastq;
783 }
784 #endif
785
786 #ifdef INET6
787 if (dst->sa_family == AF_INET6) {
788 /* XXX do something tricky here? */
789 }
790 #endif
791
792 if ((sp->pp_flags & PP_NOFRAMING) == 0) {
793 /*
794 * Prepend general data packet PPP header. For now, IP only.
795 */
796 M_PREPEND(m, PPP_HEADER_LEN, M_DONTWAIT);
797 if (! m) {
798 if (ifp->if_flags & IFF_DEBUG)
799 log(LOG_DEBUG, "%s: no memory for transmit header\n",
800 ifp->if_xname);
801 ++ifp->if_oerrors;
802 splx(s);
803 return (ENOBUFS);
804 }
805 /*
806 * May want to check size of packet
807 * (albeit due to the implementation it's always enough)
808 */
809 h = mtod(m, struct ppp_header *);
810 if (sp->pp_flags & PP_CISCO) {
811 h->address = CISCO_UNICAST; /* unicast address */
812 h->control = 0;
813 } else {
814 h->address = PPP_ALLSTATIONS; /* broadcast address */
815 h->control = PPP_UI; /* Unnumbered Info */
816 }
817 }
818
819 switch (dst->sa_family) {
820 #ifdef INET
821 case AF_INET: /* Internet Protocol */
822 if (sp->pp_flags & PP_CISCO)
823 protocol = htons(ETHERTYPE_IP);
824 else {
825 /*
826 * Don't choke with an ENETDOWN early. It's
827 * possible that we just started dialing out,
828 * so don't drop the packet immediately. If
829 * we notice that we run out of buffer space
830 * below, we will however remember that we are
831 * not ready to carry IP packets, and return
832 * ENETDOWN, as opposed to ENOBUFS.
833 */
834 protocol = htons(PPP_IP);
835 if (sp->state[IDX_IPCP] != STATE_OPENED)
836 error = ENETDOWN;
837 }
838 break;
839 #endif
840 #ifdef INET6
841 case AF_INET6: /* Internet Protocol version 6 */
842 if (sp->pp_flags & PP_CISCO)
843 protocol = htons(ETHERTYPE_IPV6);
844 else {
845 /*
846 * Don't choke with an ENETDOWN early. It's
847 * possible that we just started dialing out,
848 * so don't drop the packet immediately. If
849 * we notice that we run out of buffer space
850 * below, we will however remember that we are
851 * not ready to carry IP packets, and return
852 * ENETDOWN, as opposed to ENOBUFS.
853 */
854 protocol = htons(PPP_IPV6);
855 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
856 error = ENETDOWN;
857 }
858 break;
859 #endif
860 #ifdef NS
861 case AF_NS: /* Xerox NS Protocol */
862 protocol = htons((sp->pp_flags & PP_CISCO) ?
863 ETHERTYPE_NS : PPP_XNS);
864 break;
865 #endif
866 #ifdef IPX
867 case AF_IPX: /* Novell IPX Protocol */
868 protocol = htons((sp->pp_flags & PP_CISCO) ?
869 ETHERTYPE_IPX : PPP_IPX);
870 break;
871 #endif
872 #ifdef ISO
873 case AF_ISO: /* ISO OSI Protocol */
874 if (sp->pp_flags & PP_CISCO)
875 goto nosupport;
876 protocol = htons(PPP_ISO);
877 break;
878 nosupport:
879 #endif
880 default:
881 m_freem(m);
882 ++ifp->if_oerrors;
883 splx(s);
884 return (EAFNOSUPPORT);
885 }
886
887 if (sp->pp_flags & PP_NOFRAMING) {
888 M_PREPEND(m, 2, M_DONTWAIT);
889 if (m == NULL) {
890 if (ifp->if_flags & IFF_DEBUG)
891 log(LOG_DEBUG, "%s: no memory for transmit header\n",
892 ifp->if_xname);
893 ++ifp->if_oerrors;
894 splx(s);
895 return (ENOBUFS);
896 }
897 *mtod(m, u_int16_t *) = protocol;
898 } else {
899 h->protocol = protocol;
900 }
901
902
903 error = ifq_enqueue2(ifp, ifq, m ALTQ_COMMA ALTQ_DECL(&pktattr));
904
905 if (error == 0) {
906 /*
907 * Count output packets and bytes.
908 * The packet length includes header + additional hardware
909 * framing according to RFC 1333.
910 */
911 if (!(ifp->if_flags & IFF_OACTIVE))
912 (*ifp->if_start)(ifp);
913 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
914 }
915 splx(s);
916 return error;
917 }
918
919 void
920 sppp_attach(struct ifnet *ifp)
921 {
922 struct sppp *sp = (struct sppp *) ifp;
923
924 /* Initialize keepalive handler. */
925 if (! spppq) {
926 callout_init(&keepalive_ch);
927 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
928 }
929
930 /* Insert new entry into the keepalive list. */
931 sp->pp_next = spppq;
932 spppq = sp;
933
934 sp->pp_if.if_type = IFT_PPP;
935 sp->pp_if.if_output = sppp_output;
936 sp->pp_fastq.ifq_maxlen = 32;
937 sp->pp_cpq.ifq_maxlen = 20;
938 sp->pp_loopcnt = 0;
939 sp->pp_alivecnt = 0;
940 sp->pp_last_activity = 0;
941 sp->pp_last_receive = 0;
942 sp->pp_maxalive = DEFAULT_MAXALIVECNT;
943 sp->pp_max_noreceive = DEFAULT_NORECV_TIME;
944 sp->pp_idle_timeout = 0;
945 memset(&sp->pp_seq[0], 0, sizeof(sp->pp_seq));
946 memset(&sp->pp_rseq[0], 0, sizeof(sp->pp_rseq));
947 sp->pp_auth_failures = 0;
948 sp->pp_max_auth_fail = DEFAULT_MAX_AUTH_FAILURES;
949 sp->pp_phase = SPPP_PHASE_DEAD;
950 sp->pp_up = lcp.Up;
951 sp->pp_down = lcp.Down;
952
953 if_alloc_sadl(ifp);
954
955 memset(&sp->myauth, 0, sizeof sp->myauth);
956 memset(&sp->hisauth, 0, sizeof sp->hisauth);
957 sppp_lcp_init(sp);
958 sppp_ipcp_init(sp);
959 sppp_ipv6cp_init(sp);
960 sppp_pap_init(sp);
961 sppp_chap_init(sp);
962 }
963
964 void
965 sppp_detach(struct ifnet *ifp)
966 {
967 struct sppp **q, *p, *sp = (struct sppp *) ifp;
968 int i;
969
970 /* Remove the entry from the keepalive list. */
971 for (q = &spppq; (p = *q); q = &p->pp_next)
972 if (p == sp) {
973 *q = p->pp_next;
974 break;
975 }
976
977 /* Stop keepalive handler. */
978 if (! spppq) {
979 callout_stop(&keepalive_ch);
980 }
981
982 for (i = 0; i < IDX_COUNT; i++) {
983 callout_stop(&sp->ch[i]);
984 }
985 callout_stop(&sp->pap_my_to_ch);
986
987 /* free authentication info */
988 if (sp->myauth.name) free(sp->myauth.name, M_DEVBUF);
989 if (sp->myauth.secret) free(sp->myauth.secret, M_DEVBUF);
990 if (sp->hisauth.name) free(sp->hisauth.name, M_DEVBUF);
991 if (sp->hisauth.secret) free(sp->hisauth.secret, M_DEVBUF);
992
993 #if 0 /* done in if_detach() */
994 if_free_sadl(ifp);
995 #endif
996 }
997
998 /*
999 * Flush the interface output queue.
1000 */
1001 void
1002 sppp_flush(struct ifnet *ifp)
1003 {
1004 struct sppp *sp = (struct sppp *) ifp;
1005
1006 IFQ_PURGE(&sp->pp_if.if_snd);
1007 IF_PURGE(&sp->pp_fastq);
1008 IF_PURGE(&sp->pp_cpq);
1009 }
1010
1011 /*
1012 * Check if the output queue is empty.
1013 */
1014 int
1015 sppp_isempty(struct ifnet *ifp)
1016 {
1017 struct sppp *sp = (struct sppp *) ifp;
1018 int empty, s;
1019
1020 s = splnet();
1021 empty = IF_IS_EMPTY(&sp->pp_fastq) && IF_IS_EMPTY(&sp->pp_cpq) &&
1022 IFQ_IS_EMPTY(&sp->pp_if.if_snd);
1023 splx(s);
1024 return (empty);
1025 }
1026
1027 /*
1028 * Get next packet to send.
1029 */
1030 struct mbuf *
1031 sppp_dequeue(struct ifnet *ifp)
1032 {
1033 struct sppp *sp = (struct sppp *) ifp;
1034 struct mbuf *m;
1035 int s;
1036
1037 s = splnet();
1038 /*
1039 * Process only the control protocol queue until we have at
1040 * least one NCP open.
1041 *
1042 * Do always serve all three queues in Cisco mode.
1043 */
1044 IF_DEQUEUE(&sp->pp_cpq, m);
1045 if (m == NULL &&
1046 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
1047 IF_DEQUEUE(&sp->pp_fastq, m);
1048 if (m == NULL)
1049 IFQ_DEQUEUE(&sp->pp_if.if_snd, m);
1050 }
1051 splx(s);
1052 return m;
1053 }
1054
1055 /*
1056 * Process an ioctl request. Called on low priority level.
1057 */
1058 int
1059 sppp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1060 {
1061 struct ifreq *ifr = (struct ifreq *) data;
1062 struct sppp *sp = (struct sppp *) ifp;
1063 int s, error=0, going_up, going_down, newmode;
1064
1065 s = splnet();
1066 switch (cmd) {
1067 case SIOCAIFADDR:
1068 case SIOCSIFDSTADDR:
1069 case SIOCSIFADDR:
1070 break;
1071
1072 case SIOCSIFFLAGS:
1073 going_up = ifp->if_flags & IFF_UP &&
1074 (ifp->if_flags & IFF_RUNNING) == 0;
1075 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1076 ifp->if_flags & IFF_RUNNING;
1077 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
1078 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
1079 /* sanity */
1080 newmode = IFF_PASSIVE;
1081 ifp->if_flags &= ~IFF_AUTO;
1082 }
1083
1084 if (going_up || going_down)
1085 lcp.Close(sp);
1086 if (going_up && newmode == 0) {
1087 /* neither auto-dial nor passive */
1088 ifp->if_flags |= IFF_RUNNING;
1089 if (!(sp->pp_flags & PP_CISCO))
1090 lcp.Open(sp);
1091 } else if (going_down) {
1092 sppp_flush(ifp);
1093 ifp->if_flags &= ~IFF_RUNNING;
1094 }
1095
1096 break;
1097
1098 #ifdef SIOCSIFMTU
1099 #ifndef ifr_mtu
1100 #define ifr_mtu ifr_metric
1101 #endif
1102 case SIOCSIFMTU:
1103 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru) {
1104 error = EINVAL;
1105 break;
1106 }
1107
1108 ifp->if_mtu = ifr->ifr_mtu;
1109 break;
1110 #endif
1111 #ifdef SLIOCSETMTU
1112 case SLIOCSETMTU:
1113 if (*(short *)data < 128 || *(short *)data > sp->lcp.their_mru)
1114 {
1115 error = EINVAL;
1116 break;
1117 }
1118
1119 ifp->if_mtu = *(short *)data;
1120 break;
1121 #endif
1122 #ifdef SIOCGIFMTU
1123 case SIOCGIFMTU:
1124 ifr->ifr_mtu = ifp->if_mtu;
1125 break;
1126 #endif
1127 #ifdef SLIOCGETMTU
1128 case SLIOCGETMTU:
1129 *(short *)data = ifp->if_mtu;
1130 break;
1131 #endif
1132 case SIOCADDMULTI:
1133 case SIOCDELMULTI:
1134 break;
1135
1136 case SPPPSETAUTHCFG:
1137 case SPPPGETAUTHCFG:
1138 case SPPPSETLCPCFG:
1139 case SPPPGETLCPCFG:
1140 case SPPPSETIDLETO:
1141 case SPPPSETAUTHFAILURE:
1142 case SPPPGETAUTHFAILURES:
1143 case SPPPSETDNSOPTS:
1144 case SPPPSETKEEPALIVE:
1145 {
1146 struct proc *p = curproc; /* XXX */
1147
1148 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
1149 break;
1150 }
1151 /* FALLTHROUGH */
1152 case SPPPGETSTATUS:
1153 case SPPPGETSTATUSNCP:
1154 case SPPPGETIDLETO:
1155 case SPPPGETDNSOPTS:
1156 case SPPPGETDNSADDRS:
1157 case SPPPGETKEEPALIVE:
1158 error = sppp_params(sp, cmd, data);
1159 break;
1160
1161 default:
1162 error = ENOTTY;
1163 }
1164 splx(s);
1165 return (error);
1166 }
1167
1168
1169 /*
1170 * Cisco framing implementation.
1171 */
1172
1173 /*
1174 * Handle incoming Cisco keepalive protocol packets.
1175 */
1176 static void
1177 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1178 {
1179 STDDCL;
1180 struct cisco_packet *h;
1181 #ifdef INET
1182 u_int32_t me, mymask = 0; /* XXX: GCC */
1183 #endif
1184
1185 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1186 if (debug)
1187 log(LOG_DEBUG,
1188 "%s: cisco invalid packet length: %d bytes\n",
1189 ifp->if_xname, m->m_pkthdr.len);
1190 return;
1191 }
1192 h = mtod(m, struct cisco_packet *);
1193 if (debug)
1194 log(LOG_DEBUG,
1195 "%s: cisco input: %d bytes "
1196 "<0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1197 ifp->if_xname, m->m_pkthdr.len,
1198 ntohl(h->type), h->par1, h->par2, (u_int)h->rel,
1199 (u_int)h->time0, (u_int)h->time1);
1200 switch (ntohl(h->type)) {
1201 default:
1202 if (debug)
1203 addlog("%s: cisco unknown packet type: 0x%x\n",
1204 ifp->if_xname, ntohl(h->type));
1205 break;
1206 case CISCO_ADDR_REPLY:
1207 /* Reply on address request, ignore */
1208 break;
1209 case CISCO_KEEPALIVE_REQ:
1210 sp->pp_alivecnt = 0;
1211 sp->pp_rseq[IDX_LCP] = ntohl(h->par1);
1212 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1213 /* Local and remote sequence numbers are equal.
1214 * Probably, the line is in loopback mode. */
1215 if (sp->pp_loopcnt >= LOOPALIVECNT) {
1216 printf ("%s: loopback\n",
1217 ifp->if_xname);
1218 sp->pp_loopcnt = 0;
1219 if (ifp->if_flags & IFF_UP) {
1220 if_down(ifp);
1221 IF_PURGE(&sp->pp_cpq);
1222 }
1223 }
1224 ++sp->pp_loopcnt;
1225
1226 /* Generate new local sequence number */
1227 sp->pp_seq[IDX_LCP] = arc4random();
1228 break;
1229 }
1230 sp->pp_loopcnt = 0;
1231 if (! (ifp->if_flags & IFF_UP) &&
1232 (ifp->if_flags & IFF_RUNNING)) {
1233 if_up(ifp);
1234 }
1235 break;
1236 case CISCO_ADDR_REQ:
1237 #ifdef INET
1238 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1239 if (me != 0L)
1240 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1241 #endif
1242 break;
1243 }
1244 }
1245
1246 /*
1247 * Send Cisco keepalive packet.
1248 */
1249 static void
1250 sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2)
1251 {
1252 STDDCL;
1253 struct ppp_header *h;
1254 struct cisco_packet *ch;
1255 struct mbuf *m;
1256 u_int32_t t;
1257
1258 t = time_uptime * 1000;
1259 MGETHDR(m, M_DONTWAIT, MT_DATA);
1260 if (! m)
1261 return;
1262 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1263 m->m_pkthdr.rcvif = 0;
1264
1265 h = mtod(m, struct ppp_header *);
1266 h->address = CISCO_MULTICAST;
1267 h->control = 0;
1268 h->protocol = htons(CISCO_KEEPALIVE);
1269
1270 ch = (struct cisco_packet *)(h + 1);
1271 ch->type = htonl(type);
1272 ch->par1 = htonl(par1);
1273 ch->par2 = htonl(par2);
1274 ch->rel = -1;
1275
1276 ch->time0 = htons((u_short)(t >> 16));
1277 ch->time1 = htons((u_short) t);
1278
1279 if (debug)
1280 log(LOG_DEBUG,
1281 "%s: cisco output: <0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1282 ifp->if_xname, ntohl(ch->type), ch->par1,
1283 ch->par2, (u_int)ch->rel, (u_int)ch->time0,
1284 (u_int)ch->time1);
1285
1286 if (IF_QFULL(&sp->pp_cpq)) {
1287 IF_DROP(&sp->pp_fastq);
1288 IF_DROP(&ifp->if_snd);
1289 m_freem(m);
1290 ++ifp->if_oerrors;
1291 return;
1292 } else
1293 IF_ENQUEUE(&sp->pp_cpq, m);
1294 if (! (ifp->if_flags & IFF_OACTIVE))
1295 (*ifp->if_start)(ifp);
1296 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1297 }
1298
1299 /*
1300 * PPP protocol implementation.
1301 */
1302
1303 /*
1304 * Send PPP control protocol packet.
1305 */
1306 static void
1307 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1308 u_char ident, u_short len, void *data)
1309 {
1310 STDDCL;
1311 struct lcp_header *lh;
1312 struct mbuf *m;
1313 size_t pkthdrlen;
1314
1315 pkthdrlen = (sp->pp_flags & PP_NOFRAMING) ? 2 : PPP_HEADER_LEN;
1316
1317 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN)
1318 len = MHLEN - pkthdrlen - LCP_HEADER_LEN;
1319 MGETHDR(m, M_DONTWAIT, MT_DATA);
1320 if (! m)
1321 return;
1322 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
1323 m->m_pkthdr.rcvif = 0;
1324
1325 if (sp->pp_flags & PP_NOFRAMING) {
1326 *mtod(m, u_int16_t *) = htons(proto);
1327 lh = (struct lcp_header *)(mtod(m, u_int8_t *) + 2);
1328 } else {
1329 struct ppp_header *h;
1330 h = mtod(m, struct ppp_header *);
1331 h->address = PPP_ALLSTATIONS; /* broadcast address */
1332 h->control = PPP_UI; /* Unnumbered Info */
1333 h->protocol = htons(proto); /* Link Control Protocol */
1334 lh = (struct lcp_header *)(h + 1);
1335 }
1336 lh->type = type;
1337 lh->ident = ident;
1338 lh->len = htons(LCP_HEADER_LEN + len);
1339 if (len)
1340 bcopy (data, lh + 1, len);
1341
1342 if (debug) {
1343 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
1344 ifp->if_xname,
1345 sppp_proto_name(proto),
1346 sppp_cp_type_name(lh->type), lh->ident, ntohs(lh->len));
1347 if (len)
1348 sppp_print_bytes((u_char *)(lh + 1), len);
1349 addlog(">\n");
1350 }
1351 if (IF_QFULL(&sp->pp_cpq)) {
1352 IF_DROP(&sp->pp_fastq);
1353 IF_DROP(&ifp->if_snd);
1354 m_freem(m);
1355 ++ifp->if_oerrors;
1356 return;
1357 } else
1358 IF_ENQUEUE(&sp->pp_cpq, m);
1359 if (! (ifp->if_flags & IFF_OACTIVE))
1360 (*ifp->if_start)(ifp);
1361 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1362 }
1363
1364 /*
1365 * Handle incoming PPP control protocol packets.
1366 */
1367 static void
1368 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1369 {
1370 STDDCL;
1371 struct lcp_header *h;
1372 int len = m->m_pkthdr.len;
1373 int rv;
1374 u_char *p;
1375 u_int32_t u32;
1376
1377 if (len < 4) {
1378 if (debug)
1379 log(LOG_DEBUG,
1380 "%s: %s invalid packet length: %d bytes\n",
1381 ifp->if_xname, cp->name, len);
1382 return;
1383 }
1384 h = mtod(m, struct lcp_header *);
1385 if (debug) {
1386 log(LOG_DEBUG,
1387 "%s: %s input(%s): <%s id=0x%x len=%d",
1388 ifp->if_xname, cp->name,
1389 sppp_state_name(sp->state[cp->protoidx]),
1390 sppp_cp_type_name(h->type), h->ident, ntohs(h->len));
1391 if (len > 4)
1392 sppp_print_bytes((u_char *)(h + 1), len - 4);
1393 addlog(">\n");
1394 }
1395 if (len > ntohs(h->len))
1396 len = ntohs(h->len);
1397 p = (u_char *)(h + 1);
1398 switch (h->type) {
1399 case CONF_REQ:
1400 if (len < 4) {
1401 if (debug)
1402 addlog("%s: %s invalid conf-req length %d\n",
1403 ifp->if_xname, cp->name,
1404 len);
1405 ++ifp->if_ierrors;
1406 break;
1407 }
1408 /* handle states where RCR doesn't get a SCA/SCN */
1409 switch (sp->state[cp->protoidx]) {
1410 case STATE_CLOSING:
1411 case STATE_STOPPING:
1412 return;
1413 case STATE_CLOSED:
1414 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1415 0, 0);
1416 return;
1417 }
1418 rv = (cp->RCR)(sp, h, len);
1419 switch (sp->state[cp->protoidx]) {
1420 case STATE_OPENED:
1421 (cp->tld)(sp);
1422 (cp->scr)(sp);
1423 /* fall through... */
1424 case STATE_ACK_SENT:
1425 case STATE_REQ_SENT:
1426 sppp_cp_change_state(cp, sp, rv?
1427 STATE_ACK_SENT: STATE_REQ_SENT);
1428 break;
1429 case STATE_STOPPED:
1430 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1431 (cp->scr)(sp);
1432 sppp_cp_change_state(cp, sp, rv?
1433 STATE_ACK_SENT: STATE_REQ_SENT);
1434 break;
1435 case STATE_ACK_RCVD:
1436 if (rv) {
1437 sppp_cp_change_state(cp, sp, STATE_OPENED);
1438 if (debug)
1439 log(LOG_DEBUG, "%s: %s tlu\n",
1440 ifp->if_xname,
1441 cp->name);
1442 (cp->tlu)(sp);
1443 } else
1444 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1445 break;
1446 default:
1447 printf("%s: %s illegal %s in state %s\n",
1448 ifp->if_xname, cp->name,
1449 sppp_cp_type_name(h->type),
1450 sppp_state_name(sp->state[cp->protoidx]));
1451 ++ifp->if_ierrors;
1452 }
1453 break;
1454 case CONF_ACK:
1455 if (h->ident != sp->confid[cp->protoidx]) {
1456 if (debug)
1457 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1458 ifp->if_xname, cp->name,
1459 h->ident, sp->confid[cp->protoidx]);
1460 ++ifp->if_ierrors;
1461 break;
1462 }
1463 switch (sp->state[cp->protoidx]) {
1464 case STATE_CLOSED:
1465 case STATE_STOPPED:
1466 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1467 break;
1468 case STATE_CLOSING:
1469 case STATE_STOPPING:
1470 break;
1471 case STATE_REQ_SENT:
1472 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1473 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1474 break;
1475 case STATE_OPENED:
1476 (cp->tld)(sp);
1477 /* fall through */
1478 case STATE_ACK_RCVD:
1479 (cp->scr)(sp);
1480 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1481 break;
1482 case STATE_ACK_SENT:
1483 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1484 sppp_cp_change_state(cp, sp, STATE_OPENED);
1485 if (debug)
1486 log(LOG_DEBUG, "%s: %s tlu\n",
1487 ifp->if_xname, cp->name);
1488 (cp->tlu)(sp);
1489 break;
1490 default:
1491 printf("%s: %s illegal %s in state %s\n",
1492 ifp->if_xname, cp->name,
1493 sppp_cp_type_name(h->type),
1494 sppp_state_name(sp->state[cp->protoidx]));
1495 ++ifp->if_ierrors;
1496 }
1497 break;
1498 case CONF_NAK:
1499 case CONF_REJ:
1500 if (h->ident != sp->confid[cp->protoidx]) {
1501 if (debug)
1502 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1503 ifp->if_xname, cp->name,
1504 h->ident, sp->confid[cp->protoidx]);
1505 ++ifp->if_ierrors;
1506 break;
1507 }
1508 if (h->type == CONF_NAK)
1509 (cp->RCN_nak)(sp, h, len);
1510 else /* CONF_REJ */
1511 (cp->RCN_rej)(sp, h, len);
1512
1513 switch (sp->state[cp->protoidx]) {
1514 case STATE_CLOSED:
1515 case STATE_STOPPED:
1516 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1517 break;
1518 case STATE_REQ_SENT:
1519 case STATE_ACK_SENT:
1520 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1521 (cp->scr)(sp);
1522 break;
1523 case STATE_OPENED:
1524 (cp->tld)(sp);
1525 /* fall through */
1526 case STATE_ACK_RCVD:
1527 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1528 (cp->scr)(sp);
1529 break;
1530 case STATE_CLOSING:
1531 case STATE_STOPPING:
1532 break;
1533 default:
1534 printf("%s: %s illegal %s in state %s\n",
1535 ifp->if_xname, cp->name,
1536 sppp_cp_type_name(h->type),
1537 sppp_state_name(sp->state[cp->protoidx]));
1538 ++ifp->if_ierrors;
1539 }
1540 break;
1541
1542 case TERM_REQ:
1543 switch (sp->state[cp->protoidx]) {
1544 case STATE_ACK_RCVD:
1545 case STATE_ACK_SENT:
1546 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1547 /* fall through */
1548 case STATE_CLOSED:
1549 case STATE_STOPPED:
1550 case STATE_CLOSING:
1551 case STATE_STOPPING:
1552 case STATE_REQ_SENT:
1553 sta:
1554 /* Send Terminate-Ack packet. */
1555 if (debug)
1556 log(LOG_DEBUG, "%s: %s send terminate-ack\n",
1557 ifp->if_xname, cp->name);
1558 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1559 break;
1560 case STATE_OPENED:
1561 (cp->tld)(sp);
1562 sp->rst_counter[cp->protoidx] = 0;
1563 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1564 goto sta;
1565 default:
1566 printf("%s: %s illegal %s in state %s\n",
1567 ifp->if_xname, cp->name,
1568 sppp_cp_type_name(h->type),
1569 sppp_state_name(sp->state[cp->protoidx]));
1570 ++ifp->if_ierrors;
1571 }
1572 break;
1573 case TERM_ACK:
1574 switch (sp->state[cp->protoidx]) {
1575 case STATE_CLOSED:
1576 case STATE_STOPPED:
1577 case STATE_REQ_SENT:
1578 case STATE_ACK_SENT:
1579 break;
1580 case STATE_CLOSING:
1581 (cp->tlf)(sp);
1582 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1583 sppp_lcp_check_and_close(sp);
1584 break;
1585 case STATE_STOPPING:
1586 (cp->tlf)(sp);
1587 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1588 sppp_lcp_check_and_close(sp);
1589 break;
1590 case STATE_ACK_RCVD:
1591 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1592 break;
1593 case STATE_OPENED:
1594 (cp->tld)(sp);
1595 (cp->scr)(sp);
1596 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1597 break;
1598 default:
1599 printf("%s: %s illegal %s in state %s\n",
1600 ifp->if_xname, cp->name,
1601 sppp_cp_type_name(h->type),
1602 sppp_state_name(sp->state[cp->protoidx]));
1603 ++ifp->if_ierrors;
1604 }
1605 break;
1606 case CODE_REJ:
1607 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1608 log(LOG_INFO,
1609 "%s: %s: ignoring RXJ (%s) for code ?, "
1610 "danger will robinson\n",
1611 ifp->if_xname, cp->name,
1612 sppp_cp_type_name(h->type));
1613 switch (sp->state[cp->protoidx]) {
1614 case STATE_CLOSED:
1615 case STATE_STOPPED:
1616 case STATE_REQ_SENT:
1617 case STATE_ACK_SENT:
1618 case STATE_CLOSING:
1619 case STATE_STOPPING:
1620 case STATE_OPENED:
1621 break;
1622 case STATE_ACK_RCVD:
1623 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1624 break;
1625 default:
1626 printf("%s: %s illegal %s in state %s\n",
1627 ifp->if_xname, cp->name,
1628 sppp_cp_type_name(h->type),
1629 sppp_state_name(sp->state[cp->protoidx]));
1630 ++ifp->if_ierrors;
1631 }
1632 break;
1633 case PROTO_REJ:
1634 {
1635 int catastrophic;
1636 const struct cp *upper;
1637 int i;
1638 u_int16_t proto;
1639
1640 catastrophic = 0;
1641 upper = NULL;
1642 proto = p[0] << 8 | p[1];
1643 for (i = 0; i < IDX_COUNT; i++) {
1644 if (cps[i]->proto == proto) {
1645 upper = cps[i];
1646 break;
1647 }
1648 }
1649 if (upper == NULL)
1650 catastrophic++;
1651
1652 if (debug)
1653 log(LOG_INFO,
1654 "%s: %s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1655 ifp->if_xname, cp->name, catastrophic ? '-' : '+',
1656 sppp_cp_type_name(h->type), proto,
1657 upper ? upper->name : "unknown",
1658 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1659
1660 /*
1661 * if we got RXJ+ against conf-req, the peer does not implement
1662 * this particular protocol type. terminate the protocol.
1663 */
1664 if (upper && !catastrophic) {
1665 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1666 upper->Close(sp);
1667 break;
1668 }
1669 }
1670
1671 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1672 switch (sp->state[cp->protoidx]) {
1673 case STATE_CLOSED:
1674 case STATE_STOPPED:
1675 case STATE_REQ_SENT:
1676 case STATE_ACK_SENT:
1677 case STATE_CLOSING:
1678 case STATE_STOPPING:
1679 case STATE_OPENED:
1680 break;
1681 case STATE_ACK_RCVD:
1682 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1683 break;
1684 default:
1685 printf("%s: %s illegal %s in state %s\n",
1686 ifp->if_xname, cp->name,
1687 sppp_cp_type_name(h->type),
1688 sppp_state_name(sp->state[cp->protoidx]));
1689 ++ifp->if_ierrors;
1690 }
1691 break;
1692 }
1693 case DISC_REQ:
1694 if (cp->proto != PPP_LCP)
1695 goto illegal;
1696 /* Discard the packet. */
1697 break;
1698 case ECHO_REQ:
1699 if (cp->proto != PPP_LCP)
1700 goto illegal;
1701 if (sp->state[cp->protoidx] != STATE_OPENED) {
1702 if (debug)
1703 addlog("%s: lcp echo req but lcp closed\n",
1704 ifp->if_xname);
1705 ++ifp->if_ierrors;
1706 break;
1707 }
1708 if (len < 8) {
1709 if (debug)
1710 addlog("%s: invalid lcp echo request "
1711 "packet length: %d bytes\n",
1712 ifp->if_xname, len);
1713 break;
1714 }
1715 memcpy(&u32, h + 1, sizeof u32);
1716 if (ntohl(u32) == sp->lcp.magic) {
1717 /* Line loopback mode detected. */
1718 printf("%s: loopback\n", ifp->if_xname);
1719 if_down(ifp);
1720 IF_PURGE(&sp->pp_cpq);
1721
1722 /* Shut down the PPP link. */
1723 /* XXX */
1724 lcp.Down(sp);
1725 lcp.Up(sp);
1726 break;
1727 }
1728 u32 = htonl(sp->lcp.magic);
1729 memcpy(h + 1, &u32, sizeof u32);
1730 if (debug)
1731 addlog("%s: got lcp echo req, sending echo rep\n",
1732 ifp->if_xname);
1733 sppp_cp_send(sp, PPP_LCP, ECHO_REPLY, h->ident, len - 4,
1734 h + 1);
1735 break;
1736 case ECHO_REPLY:
1737 if (cp->proto != PPP_LCP)
1738 goto illegal;
1739 if (h->ident != sp->lcp.echoid) {
1740 ++ifp->if_ierrors;
1741 break;
1742 }
1743 if (len < 8) {
1744 if (debug)
1745 addlog("%s: lcp invalid echo reply "
1746 "packet length: %d bytes\n",
1747 ifp->if_xname, len);
1748 break;
1749 }
1750 if (debug)
1751 addlog("%s: lcp got echo rep\n",
1752 ifp->if_xname);
1753 memcpy(&u32, h + 1, sizeof u32);
1754 if (ntohl(u32) != sp->lcp.magic)
1755 sp->pp_alivecnt = 0;
1756 break;
1757 default:
1758 /* Unknown packet type -- send Code-Reject packet. */
1759 illegal:
1760 if (debug)
1761 addlog("%s: %s send code-rej for 0x%x\n",
1762 ifp->if_xname, cp->name, h->type);
1763 sppp_cp_send(sp, cp->proto, CODE_REJ,
1764 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1765 ++ifp->if_ierrors;
1766 }
1767 }
1768
1769
1770 /*
1771 * The generic part of all Up/Down/Open/Close/TO event handlers.
1772 * Basically, the state transition handling in the automaton.
1773 */
1774 static void
1775 sppp_up_event(const struct cp *cp, struct sppp *sp)
1776 {
1777 STDDCL;
1778
1779 if (debug)
1780 log(LOG_DEBUG, "%s: %s up(%s)\n",
1781 ifp->if_xname, cp->name,
1782 sppp_state_name(sp->state[cp->protoidx]));
1783
1784 switch (sp->state[cp->protoidx]) {
1785 case STATE_INITIAL:
1786 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1787 break;
1788 case STATE_STARTING:
1789 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1790 (cp->scr)(sp);
1791 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1792 break;
1793 default:
1794 printf("%s: %s illegal up in state %s\n",
1795 ifp->if_xname, cp->name,
1796 sppp_state_name(sp->state[cp->protoidx]));
1797 }
1798 }
1799
1800 static void
1801 sppp_down_event(const struct cp *cp, struct sppp *sp)
1802 {
1803 STDDCL;
1804
1805 if (debug)
1806 log(LOG_DEBUG, "%s: %s down(%s)\n",
1807 ifp->if_xname, cp->name,
1808 sppp_state_name(sp->state[cp->protoidx]));
1809
1810 switch (sp->state[cp->protoidx]) {
1811 case STATE_CLOSED:
1812 case STATE_CLOSING:
1813 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1814 break;
1815 case STATE_STOPPED:
1816 (cp->tls)(sp);
1817 /* fall through */
1818 case STATE_STOPPING:
1819 case STATE_REQ_SENT:
1820 case STATE_ACK_RCVD:
1821 case STATE_ACK_SENT:
1822 sppp_cp_change_state(cp, sp, STATE_STARTING);
1823 break;
1824 case STATE_OPENED:
1825 (cp->tld)(sp);
1826 sppp_cp_change_state(cp, sp, STATE_STARTING);
1827 break;
1828 default:
1829 printf("%s: %s illegal down in state %s\n",
1830 ifp->if_xname, cp->name,
1831 sppp_state_name(sp->state[cp->protoidx]));
1832 }
1833 }
1834
1835
1836 static void
1837 sppp_open_event(const struct cp *cp, struct sppp *sp)
1838 {
1839 STDDCL;
1840
1841 if (debug)
1842 log(LOG_DEBUG, "%s: %s open(%s)\n",
1843 ifp->if_xname, cp->name,
1844 sppp_state_name(sp->state[cp->protoidx]));
1845
1846 switch (sp->state[cp->protoidx]) {
1847 case STATE_INITIAL:
1848 sppp_cp_change_state(cp, sp, STATE_STARTING);
1849 (cp->tls)(sp);
1850 break;
1851 case STATE_STARTING:
1852 break;
1853 case STATE_CLOSED:
1854 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1855 (cp->scr)(sp);
1856 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1857 break;
1858 case STATE_STOPPED:
1859 case STATE_STOPPING:
1860 case STATE_REQ_SENT:
1861 case STATE_ACK_RCVD:
1862 case STATE_ACK_SENT:
1863 case STATE_OPENED:
1864 break;
1865 case STATE_CLOSING:
1866 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1867 break;
1868 }
1869 }
1870
1871
1872 static void
1873 sppp_close_event(const struct cp *cp, struct sppp *sp)
1874 {
1875 STDDCL;
1876
1877 if (debug)
1878 log(LOG_DEBUG, "%s: %s close(%s)\n",
1879 ifp->if_xname, cp->name,
1880 sppp_state_name(sp->state[cp->protoidx]));
1881
1882 switch (sp->state[cp->protoidx]) {
1883 case STATE_INITIAL:
1884 case STATE_CLOSED:
1885 case STATE_CLOSING:
1886 break;
1887 case STATE_STARTING:
1888 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1889 (cp->tlf)(sp);
1890 break;
1891 case STATE_STOPPED:
1892 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1893 break;
1894 case STATE_STOPPING:
1895 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1896 break;
1897 case STATE_OPENED:
1898 (cp->tld)(sp);
1899 /* fall through */
1900 case STATE_REQ_SENT:
1901 case STATE_ACK_RCVD:
1902 case STATE_ACK_SENT:
1903 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1904 sppp_cp_send(sp, cp->proto, TERM_REQ,
1905 ++sp->pp_seq[cp->protoidx], 0, 0);
1906 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1907 break;
1908 }
1909 }
1910
1911 static void
1912 sppp_to_event(const struct cp *cp, struct sppp *sp)
1913 {
1914 STDDCL;
1915 int s;
1916
1917 s = splnet();
1918 if (debug)
1919 log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
1920 ifp->if_xname, cp->name,
1921 sppp_state_name(sp->state[cp->protoidx]),
1922 sp->rst_counter[cp->protoidx]);
1923
1924 if (--sp->rst_counter[cp->protoidx] < 0)
1925 /* TO- event */
1926 switch (sp->state[cp->protoidx]) {
1927 case STATE_CLOSING:
1928 (cp->tlf)(sp);
1929 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1930 sppp_lcp_check_and_close(sp);
1931 break;
1932 case STATE_STOPPING:
1933 (cp->tlf)(sp);
1934 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1935 sppp_lcp_check_and_close(sp);
1936 break;
1937 case STATE_REQ_SENT:
1938 case STATE_ACK_RCVD:
1939 case STATE_ACK_SENT:
1940 (cp->tlf)(sp);
1941 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1942 sppp_lcp_check_and_close(sp);
1943 break;
1944 }
1945 else
1946 /* TO+ event */
1947 switch (sp->state[cp->protoidx]) {
1948 case STATE_CLOSING:
1949 case STATE_STOPPING:
1950 sppp_cp_send(sp, cp->proto, TERM_REQ,
1951 ++sp->pp_seq[cp->protoidx], 0, 0);
1952 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1953 cp->TO, sp);
1954 break;
1955 case STATE_REQ_SENT:
1956 case STATE_ACK_RCVD:
1957 (cp->scr)(sp);
1958 /* sppp_cp_change_state() will restart the timer */
1959 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1960 break;
1961 case STATE_ACK_SENT:
1962 (cp->scr)(sp);
1963 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1964 cp->TO, sp);
1965 break;
1966 }
1967
1968 splx(s);
1969 }
1970
1971 /*
1972 * Change the state of a control protocol in the state automaton.
1973 * Takes care of starting/stopping the restart timer.
1974 */
1975 void
1976 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1977 {
1978 sp->state[cp->protoidx] = newstate;
1979 callout_stop(&sp->ch[cp->protoidx]);
1980 switch (newstate) {
1981 case STATE_INITIAL:
1982 case STATE_STARTING:
1983 case STATE_CLOSED:
1984 case STATE_STOPPED:
1985 case STATE_OPENED:
1986 break;
1987 case STATE_CLOSING:
1988 case STATE_STOPPING:
1989 case STATE_REQ_SENT:
1990 case STATE_ACK_RCVD:
1991 case STATE_ACK_SENT:
1992 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1993 cp->TO, sp);
1994 break;
1995 }
1996 }
1997
1998 /*
1999 *--------------------------------------------------------------------------*
2000 * *
2001 * The LCP implementation. *
2002 * *
2003 *--------------------------------------------------------------------------*
2004 */
2005 static void
2006 sppp_lcp_init(struct sppp *sp)
2007 {
2008 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2009 sp->lcp.magic = 0;
2010 sp->state[IDX_LCP] = STATE_INITIAL;
2011 sp->fail_counter[IDX_LCP] = 0;
2012 sp->pp_seq[IDX_LCP] = 0;
2013 sp->pp_rseq[IDX_LCP] = 0;
2014 sp->lcp.protos = 0;
2015 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2016
2017 /*
2018 * Initialize counters and timeout values. Note that we don't
2019 * use the 3 seconds suggested in RFC 1661 since we are likely
2020 * running on a fast link. XXX We should probably implement
2021 * the exponential backoff option. Note that these values are
2022 * relevant for all control protocols, not just LCP only.
2023 */
2024 sp->lcp.timeout = 1 * hz;
2025 sp->lcp.max_terminate = 2;
2026 sp->lcp.max_configure = 10;
2027 sp->lcp.max_failure = 10;
2028 callout_init(&sp->ch[IDX_LCP]);
2029 }
2030
2031 static void
2032 sppp_lcp_up(struct sppp *sp)
2033 {
2034 STDDCL;
2035
2036 /* Initialize activity timestamp: opening a connection is an activity */
2037 sp->pp_last_receive = sp->pp_last_activity = time_uptime;
2038
2039 /*
2040 * If this interface is passive or dial-on-demand, and we are
2041 * still in Initial state, it means we've got an incoming
2042 * call. Activate the interface.
2043 */
2044 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2045 if (debug)
2046 log(LOG_DEBUG,
2047 "%s: Up event", ifp->if_xname);
2048 ifp->if_flags |= IFF_RUNNING;
2049 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2050 if (debug)
2051 addlog("(incoming call)\n");
2052 sp->pp_flags |= PP_CALLIN;
2053 lcp.Open(sp);
2054 } else if (debug)
2055 addlog("\n");
2056 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2057 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2058 ifp->if_flags |= IFF_RUNNING;
2059 lcp.Open(sp);
2060 }
2061
2062 sppp_up_event(&lcp, sp);
2063 }
2064
2065 static void
2066 sppp_lcp_down(struct sppp *sp)
2067 {
2068 STDDCL;
2069
2070 sppp_down_event(&lcp, sp);
2071
2072 /*
2073 * If this is neither a dial-on-demand nor a passive
2074 * interface, simulate an ``ifconfig down'' action, so the
2075 * administrator can force a redial by another ``ifconfig
2076 * up''. XXX For leased line operation, should we immediately
2077 * try to reopen the connection here?
2078 */
2079 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2080 if (debug)
2081 log(LOG_INFO,
2082 "%s: Down event (carrier loss), taking interface down.\n",
2083 ifp->if_xname);
2084 if_down(ifp);
2085 } else {
2086 if (debug)
2087 log(LOG_DEBUG,
2088 "%s: Down event (carrier loss)\n",
2089 ifp->if_xname);
2090 }
2091 sp->pp_flags &= ~PP_CALLIN;
2092 if (sp->state[IDX_LCP] != STATE_INITIAL)
2093 lcp.Close(sp);
2094 ifp->if_flags &= ~IFF_RUNNING;
2095 }
2096
2097 static void
2098 sppp_lcp_open(struct sppp *sp)
2099 {
2100 /*
2101 * If we are authenticator, negotiate LCP_AUTH
2102 */
2103 if (sp->hisauth.proto != 0)
2104 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2105 else
2106 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2107 sp->pp_flags &= ~PP_NEEDAUTH;
2108 sppp_open_event(&lcp, sp);
2109 }
2110
2111 static void
2112 sppp_lcp_close(struct sppp *sp)
2113 {
2114 sppp_close_event(&lcp, sp);
2115 }
2116
2117 static void
2118 sppp_lcp_TO(void *cookie)
2119 {
2120 sppp_to_event(&lcp, (struct sppp *)cookie);
2121 }
2122
2123 /*
2124 * Analyze a configure request. Return true if it was agreeable, and
2125 * caused action sca, false if it has been rejected or nak'ed, and
2126 * caused action scn. (The return value is used to make the state
2127 * transition decision in the state automaton.)
2128 */
2129 static int
2130 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2131 {
2132 STDDCL;
2133 u_char *buf, *r, *p;
2134 int origlen, rlen;
2135 u_int32_t nmagic;
2136 u_short authproto;
2137
2138 len -= 4;
2139 origlen = len;
2140 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2141 if (! buf)
2142 return (0);
2143
2144 if (debug)
2145 log(LOG_DEBUG, "%s: lcp parse opts:",
2146 ifp->if_xname);
2147
2148 /* pass 1: check for things that need to be rejected */
2149 p = (void *)(h + 1);
2150 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2151 if (debug)
2152 addlog(" %s", sppp_lcp_opt_name(*p));
2153 switch (*p) {
2154 case LCP_OPT_MAGIC:
2155 /* Magic number. */
2156 /* fall through, both are same length */
2157 case LCP_OPT_ASYNC_MAP:
2158 /* Async control character map. */
2159 if (len >= 6 || p[1] == 6)
2160 continue;
2161 if (debug)
2162 addlog(" [invalid]");
2163 break;
2164 case LCP_OPT_MRU:
2165 /* Maximum receive unit. */
2166 if (len >= 4 && p[1] == 4)
2167 continue;
2168 if (debug)
2169 addlog(" [invalid]");
2170 break;
2171 case LCP_OPT_AUTH_PROTO:
2172 if (len < 4) {
2173 if (debug)
2174 addlog(" [invalid]");
2175 break;
2176 }
2177 authproto = (p[2] << 8) + p[3];
2178 if (authproto == PPP_CHAP && p[1] != 5) {
2179 if (debug)
2180 addlog(" [invalid chap len]");
2181 break;
2182 }
2183 if (sp->myauth.proto == 0) {
2184 /* we are not configured to do auth */
2185 if (debug)
2186 addlog(" [not configured]");
2187 break;
2188 }
2189 /*
2190 * Remote want us to authenticate, remember this,
2191 * so we stay in SPPP_PHASE_AUTHENTICATE after LCP got
2192 * up.
2193 */
2194 sp->pp_flags |= PP_NEEDAUTH;
2195 continue;
2196 default:
2197 /* Others not supported. */
2198 if (debug)
2199 addlog(" [rej]");
2200 break;
2201 }
2202 /* Add the option to rejected list. */
2203 bcopy (p, r, p[1]);
2204 r += p[1];
2205 rlen += p[1];
2206 }
2207 if (rlen) {
2208 if (debug)
2209 addlog(" send conf-rej\n");
2210 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2211 goto end;
2212 } else if (debug)
2213 addlog("\n");
2214
2215 /*
2216 * pass 2: check for option values that are unacceptable and
2217 * thus require to be nak'ed.
2218 */
2219 if (debug)
2220 log(LOG_DEBUG, "%s: lcp parse opt values: ",
2221 ifp->if_xname);
2222
2223 p = (void *)(h + 1);
2224 len = origlen;
2225 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2226 if (debug)
2227 addlog(" %s", sppp_lcp_opt_name(*p));
2228 switch (*p) {
2229 case LCP_OPT_MAGIC:
2230 /* Magic number -- extract. */
2231 nmagic = (u_int32_t)p[2] << 24 |
2232 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2233 if (nmagic != sp->lcp.magic) {
2234 if (debug)
2235 addlog(" 0x%x", nmagic);
2236 continue;
2237 }
2238 /*
2239 * Local and remote magics equal -- loopback?
2240 */
2241 if (sp->pp_loopcnt >= LOOPALIVECNT*5) {
2242 printf ("%s: loopback\n",
2243 ifp->if_xname);
2244 sp->pp_loopcnt = 0;
2245 if (ifp->if_flags & IFF_UP) {
2246 if_down(ifp);
2247 IF_PURGE(&sp->pp_cpq);
2248 /* XXX ? */
2249 lcp.Down(sp);
2250 lcp.Up(sp);
2251 }
2252 } else if (debug)
2253 addlog(" [glitch]");
2254 ++sp->pp_loopcnt;
2255 /*
2256 * We negate our magic here, and NAK it. If
2257 * we see it later in an NAK packet, we
2258 * suggest a new one.
2259 */
2260 nmagic = ~sp->lcp.magic;
2261 /* Gonna NAK it. */
2262 p[2] = nmagic >> 24;
2263 p[3] = nmagic >> 16;
2264 p[4] = nmagic >> 8;
2265 p[5] = nmagic;
2266 break;
2267
2268 case LCP_OPT_ASYNC_MAP:
2269 /*
2270 * Async control character map -- just ignore it.
2271 *
2272 * Quote from RFC 1662, chapter 6:
2273 * To enable this functionality, synchronous PPP
2274 * implementations MUST always respond to the
2275 * Async-Control-Character-Map Configuration
2276 * Option with the LCP Configure-Ack. However,
2277 * acceptance of the Configuration Option does
2278 * not imply that the synchronous implementation
2279 * will do any ACCM mapping. Instead, all such
2280 * octet mapping will be performed by the
2281 * asynchronous-to-synchronous converter.
2282 */
2283 continue;
2284
2285 case LCP_OPT_MRU:
2286 /*
2287 * Maximum receive unit. Always agreeable,
2288 * but ignored by now.
2289 */
2290 sp->lcp.their_mru = p[2] * 256 + p[3];
2291 if (debug)
2292 addlog(" %ld", sp->lcp.their_mru);
2293 continue;
2294
2295 case LCP_OPT_AUTH_PROTO:
2296 authproto = (p[2] << 8) + p[3];
2297 if (sp->myauth.proto != authproto) {
2298 /* not agreed, nak */
2299 if (debug)
2300 addlog(" [mine %s != his %s]",
2301 sppp_proto_name(sp->myauth.proto),
2302 sppp_proto_name(authproto));
2303 p[2] = sp->myauth.proto >> 8;
2304 p[3] = sp->myauth.proto;
2305 break;
2306 }
2307 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2308 if (debug)
2309 addlog(" [chap not MD5]");
2310 p[4] = CHAP_MD5;
2311 break;
2312 }
2313 continue;
2314 }
2315 /* Add the option to nak'ed list. */
2316 bcopy (p, r, p[1]);
2317 r += p[1];
2318 rlen += p[1];
2319 }
2320 if (rlen) {
2321 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2322 if (debug)
2323 addlog(" max_failure (%d) exceeded, "
2324 "send conf-rej\n",
2325 sp->lcp.max_failure);
2326 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2327 } else {
2328 if (debug)
2329 addlog(" send conf-nak\n");
2330 sppp_cp_send(sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2331 }
2332 goto end;
2333 } else {
2334 if (debug)
2335 addlog(" send conf-ack\n");
2336 sp->fail_counter[IDX_LCP] = 0;
2337 sp->pp_loopcnt = 0;
2338 sppp_cp_send(sp, PPP_LCP, CONF_ACK, h->ident, origlen, h + 1);
2339 }
2340
2341 end:
2342 free(buf, M_TEMP);
2343 return (rlen == 0);
2344 }
2345
2346 /*
2347 * Analyze the LCP Configure-Reject option list, and adjust our
2348 * negotiation.
2349 */
2350 static void
2351 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2352 {
2353 STDDCL;
2354 u_char *buf, *p;
2355
2356 len -= 4;
2357 buf = malloc (len, M_TEMP, M_NOWAIT);
2358 if (!buf)
2359 return;
2360
2361 if (debug)
2362 log(LOG_DEBUG, "%s: lcp rej opts:",
2363 ifp->if_xname);
2364
2365 p = (void *)(h + 1);
2366 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2367 if (debug)
2368 addlog(" %s", sppp_lcp_opt_name(*p));
2369 switch (*p) {
2370 case LCP_OPT_MAGIC:
2371 /* Magic number -- can't use it, use 0 */
2372 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2373 sp->lcp.magic = 0;
2374 break;
2375 case LCP_OPT_MRU:
2376 /*
2377 * Should not be rejected anyway, since we only
2378 * negotiate a MRU if explicitly requested by
2379 * peer.
2380 */
2381 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2382 break;
2383 case LCP_OPT_AUTH_PROTO:
2384 /*
2385 * Peer doesn't want to authenticate himself,
2386 * deny unless this is a dialout call, and
2387 * SPPP_AUTHFLAG_NOCALLOUT is set.
2388 */
2389 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2390 (sp->hisauth.flags & SPPP_AUTHFLAG_NOCALLOUT) != 0) {
2391 if (debug)
2392 addlog(" [don't insist on auth "
2393 "for callout]");
2394 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2395 break;
2396 }
2397 if (debug)
2398 addlog("[access denied]\n");
2399 lcp.Close(sp);
2400 break;
2401 }
2402 }
2403 if (debug)
2404 addlog("\n");
2405 free(buf, M_TEMP);
2406 return;
2407 }
2408
2409 /*
2410 * Analyze the LCP Configure-NAK option list, and adjust our
2411 * negotiation.
2412 */
2413 static void
2414 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2415 {
2416 STDDCL;
2417 u_char *buf, *p;
2418 u_int32_t magic;
2419
2420 len -= 4;
2421 buf = malloc (len, M_TEMP, M_NOWAIT);
2422 if (!buf)
2423 return;
2424
2425 if (debug)
2426 log(LOG_DEBUG, "%s: lcp nak opts:",
2427 ifp->if_xname);
2428
2429 p = (void *)(h + 1);
2430 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2431 if (debug)
2432 addlog(" %s", sppp_lcp_opt_name(*p));
2433 switch (*p) {
2434 case LCP_OPT_MAGIC:
2435 /* Magic number -- renegotiate */
2436 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2437 len >= 6 && p[1] == 6) {
2438 magic = (u_int32_t)p[2] << 24 |
2439 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2440 /*
2441 * If the remote magic is our negated one,
2442 * this looks like a loopback problem.
2443 * Suggest a new magic to make sure.
2444 */
2445 if (magic == ~sp->lcp.magic) {
2446 if (debug)
2447 addlog(" magic glitch");
2448 sp->lcp.magic = arc4random();
2449 } else {
2450 sp->lcp.magic = magic;
2451 if (debug)
2452 addlog(" %d", magic);
2453 }
2454 }
2455 break;
2456 case LCP_OPT_MRU:
2457 /*
2458 * Peer wants to advise us to negotiate an MRU.
2459 * Agree on it if it's reasonable, or use
2460 * default otherwise.
2461 */
2462 if (len >= 4 && p[1] == 4) {
2463 u_int mru = p[2] * 256 + p[3];
2464 if (debug)
2465 addlog(" %d", mru);
2466 if (mru < PP_MTU || mru > PP_MAX_MRU)
2467 mru = PP_MTU;
2468 sp->lcp.mru = mru;
2469 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2470 }
2471 break;
2472 case LCP_OPT_AUTH_PROTO:
2473 /*
2474 * Peer doesn't like our authentication method,
2475 * deny.
2476 */
2477 if (debug)
2478 addlog("[access denied]\n");
2479 lcp.Close(sp);
2480 break;
2481 }
2482 }
2483 if (debug)
2484 addlog("\n");
2485 free(buf, M_TEMP);
2486 return;
2487 }
2488
2489 static void
2490 sppp_lcp_tlu(struct sppp *sp)
2491 {
2492 STDDCL;
2493 int i;
2494 u_int32_t mask;
2495
2496 /* XXX ? */
2497 if (! (ifp->if_flags & IFF_UP) &&
2498 (ifp->if_flags & IFF_RUNNING)) {
2499 /* Coming out of loopback mode. */
2500 if_up(ifp);
2501 }
2502
2503 for (i = 0; i < IDX_COUNT; i++)
2504 if ((cps[i])->flags & CP_QUAL)
2505 (cps[i])->Open(sp);
2506
2507 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2508 (sp->pp_flags & PP_NEEDAUTH) != 0)
2509 sp->pp_phase = SPPP_PHASE_AUTHENTICATE;
2510 else
2511 sp->pp_phase = SPPP_PHASE_NETWORK;
2512
2513 if (debug)
2514 {
2515 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2516 sppp_phase_name(sp->pp_phase));
2517 }
2518
2519 /*
2520 * Open all authentication protocols. This is even required
2521 * if we already proceeded to network phase, since it might be
2522 * that remote wants us to authenticate, so we might have to
2523 * send a PAP request. Undesired authentication protocols
2524 * don't do anything when they get an Open event.
2525 */
2526 for (i = 0; i < IDX_COUNT; i++)
2527 if ((cps[i])->flags & CP_AUTH)
2528 (cps[i])->Open(sp);
2529
2530 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
2531 /* Notify all NCPs. */
2532 for (i = 0; i < IDX_COUNT; i++)
2533 if ((cps[i])->flags & CP_NCP)
2534 (cps[i])->Open(sp);
2535 }
2536
2537 /* Send Up events to all started protos. */
2538 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2539 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2540 (cps[i])->Up(sp);
2541
2542 /* notify low-level driver of state change */
2543 if (sp->pp_chg)
2544 sp->pp_chg(sp, (int)sp->pp_phase);
2545
2546 if (sp->pp_phase == SPPP_PHASE_NETWORK)
2547 /* if no NCP is starting, close down */
2548 sppp_lcp_check_and_close(sp);
2549 }
2550
2551 static void
2552 sppp_lcp_tld(struct sppp *sp)
2553 {
2554 STDDCL;
2555 int i;
2556 u_int32_t mask;
2557
2558 sp->pp_phase = SPPP_PHASE_TERMINATE;
2559
2560 if (debug)
2561 {
2562 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2563 sppp_phase_name(sp->pp_phase));
2564 }
2565
2566 /*
2567 * Take upper layers down. We send the Down event first and
2568 * the Close second to prevent the upper layers from sending
2569 * ``a flurry of terminate-request packets'', as the RFC
2570 * describes it.
2571 */
2572 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2573 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2574 (cps[i])->Down(sp);
2575 (cps[i])->Close(sp);
2576 }
2577 }
2578
2579 static void
2580 sppp_lcp_tls(struct sppp *sp)
2581 {
2582 STDDCL;
2583
2584 if (sp->pp_max_auth_fail != 0 && sp->pp_auth_failures >= sp->pp_max_auth_fail) {
2585 printf("%s: authentication failed %d times, not retrying again\n",
2586 sp->pp_if.if_xname, sp->pp_auth_failures);
2587 if_down(&sp->pp_if);
2588 return;
2589 }
2590
2591 sp->pp_phase = SPPP_PHASE_ESTABLISH;
2592
2593 if (debug)
2594 {
2595 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2596 sppp_phase_name(sp->pp_phase));
2597 }
2598
2599 /* Notify lower layer if desired. */
2600 if (sp->pp_tls)
2601 (sp->pp_tls)(sp);
2602 }
2603
2604 static void
2605 sppp_lcp_tlf(struct sppp *sp)
2606 {
2607 STDDCL;
2608
2609 sp->pp_phase = SPPP_PHASE_DEAD;
2610
2611 if (debug)
2612 {
2613 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2614 sppp_phase_name(sp->pp_phase));
2615 }
2616
2617 /* Notify lower layer if desired. */
2618 if (sp->pp_tlf)
2619 (sp->pp_tlf)(sp);
2620 }
2621
2622 static void
2623 sppp_lcp_scr(struct sppp *sp)
2624 {
2625 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2626 int i = 0;
2627 u_short authproto;
2628
2629 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2630 if (! sp->lcp.magic)
2631 sp->lcp.magic = arc4random();
2632 opt[i++] = LCP_OPT_MAGIC;
2633 opt[i++] = 6;
2634 opt[i++] = sp->lcp.magic >> 24;
2635 opt[i++] = sp->lcp.magic >> 16;
2636 opt[i++] = sp->lcp.magic >> 8;
2637 opt[i++] = sp->lcp.magic;
2638 }
2639
2640 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2641 opt[i++] = LCP_OPT_MRU;
2642 opt[i++] = 4;
2643 opt[i++] = sp->lcp.mru >> 8;
2644 opt[i++] = sp->lcp.mru;
2645 }
2646
2647 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2648 authproto = sp->hisauth.proto;
2649 opt[i++] = LCP_OPT_AUTH_PROTO;
2650 opt[i++] = authproto == PPP_CHAP? 5: 4;
2651 opt[i++] = authproto >> 8;
2652 opt[i++] = authproto;
2653 if (authproto == PPP_CHAP)
2654 opt[i++] = CHAP_MD5;
2655 }
2656
2657 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2658 sppp_cp_send(sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2659 }
2660
2661 /*
2662 * Check the open NCPs, return true if at least one NCP is open.
2663 */
2664 static int
2665 sppp_ncp_check(struct sppp *sp)
2666 {
2667 int i, mask;
2668
2669 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2670 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2671 return 1;
2672 return 0;
2673 }
2674
2675 /*
2676 * Re-check the open NCPs and see if we should terminate the link.
2677 * Called by the NCPs during their tlf action handling.
2678 */
2679 static void
2680 sppp_lcp_check_and_close(struct sppp *sp)
2681 {
2682
2683 if (sp->pp_phase < SPPP_PHASE_NETWORK)
2684 /* don't bother, we are already going down */
2685 return;
2686
2687 if (sppp_ncp_check(sp))
2688 return;
2689
2690 lcp.Close(sp);
2691 }
2692
2693
2694 /*
2695 *--------------------------------------------------------------------------*
2696 * *
2697 * The IPCP implementation. *
2698 * *
2699 *--------------------------------------------------------------------------*
2700 */
2701
2702 static void
2703 sppp_ipcp_init(struct sppp *sp)
2704 {
2705 sp->ipcp.opts = 0;
2706 sp->ipcp.flags = 0;
2707 sp->state[IDX_IPCP] = STATE_INITIAL;
2708 sp->fail_counter[IDX_IPCP] = 0;
2709 sp->pp_seq[IDX_IPCP] = 0;
2710 sp->pp_rseq[IDX_IPCP] = 0;
2711 callout_init(&sp->ch[IDX_IPCP]);
2712 }
2713
2714 static void
2715 sppp_ipcp_up(struct sppp *sp)
2716 {
2717 sppp_up_event(&ipcp, sp);
2718 }
2719
2720 static void
2721 sppp_ipcp_down(struct sppp *sp)
2722 {
2723 sppp_down_event(&ipcp, sp);
2724 }
2725
2726 static void
2727 sppp_ipcp_open(struct sppp *sp)
2728 {
2729 STDDCL;
2730 u_int32_t myaddr, hisaddr;
2731
2732 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
2733 sp->ipcp.req_myaddr = 0;
2734 sp->ipcp.req_hisaddr = 0;
2735 memset(&sp->dns_addrs, 0, sizeof sp->dns_addrs);
2736
2737 #ifdef INET
2738 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2739 #else
2740 myaddr = hisaddr = 0;
2741 #endif
2742 /*
2743 * If we don't have his address, this probably means our
2744 * interface doesn't want to talk IP at all. (This could
2745 * be the case if somebody wants to speak only IPX, for
2746 * example.) Don't open IPCP in this case.
2747 */
2748 if (hisaddr == 0) {
2749 /* XXX this message should go away */
2750 if (debug)
2751 log(LOG_DEBUG, "%s: ipcp_open(): no IP interface\n",
2752 ifp->if_xname);
2753 return;
2754 }
2755
2756 if (myaddr == 0) {
2757 /*
2758 * I don't have an assigned address, so i need to
2759 * negotiate my address.
2760 */
2761 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2762 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2763 }
2764 if (hisaddr == 1) {
2765 /*
2766 * XXX - remove this hack!
2767 * remote has no valid address, we need to get one assigned.
2768 */
2769 sp->ipcp.flags |= IPCP_HISADDR_DYN;
2770 }
2771 sppp_open_event(&ipcp, sp);
2772 }
2773
2774 static void
2775 sppp_ipcp_close(struct sppp *sp)
2776 {
2777 STDDCL;
2778
2779 sppp_close_event(&ipcp, sp);
2780 #ifdef INET
2781 if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
2782 /*
2783 * Some address was dynamic, clear it again.
2784 */
2785 sppp_clear_ip_addrs(sp);
2786 #endif
2787
2788 if (sp->pp_saved_mtu > 0) {
2789 ifp->if_mtu = sp->pp_saved_mtu;
2790 sp->pp_saved_mtu = 0;
2791 if (debug)
2792 log(LOG_DEBUG,
2793 "%s: resetting MTU to %" PRIu64 " bytes\n",
2794 ifp->if_xname, ifp->if_mtu);
2795 }
2796 }
2797
2798 static void
2799 sppp_ipcp_TO(void *cookie)
2800 {
2801 sppp_to_event(&ipcp, (struct sppp *)cookie);
2802 }
2803
2804 /*
2805 * Analyze a configure request. Return true if it was agreeable, and
2806 * caused action sca, false if it has been rejected or nak'ed, and
2807 * caused action scn. (The return value is used to make the state
2808 * transition decision in the state automaton.)
2809 */
2810 static int
2811 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2812 {
2813 u_char *buf, *r, *p;
2814 struct ifnet *ifp = &sp->pp_if;
2815 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2816 u_int32_t hisaddr, desiredaddr;
2817
2818 len -= 4;
2819 origlen = len;
2820 /*
2821 * Make sure to allocate a buf that can at least hold a
2822 * conf-nak with an `address' option. We might need it below.
2823 */
2824 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2825 if (! buf)
2826 return (0);
2827
2828 /* pass 1: see if we can recognize them */
2829 if (debug)
2830 log(LOG_DEBUG, "%s: ipcp parse opts:",
2831 ifp->if_xname);
2832 p = (void *)(h + 1);
2833 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2834 if (debug)
2835 addlog(" %s", sppp_ipcp_opt_name(*p));
2836 switch (*p) {
2837 #ifdef notyet
2838 case IPCP_OPT_COMPRESSION:
2839 if (len >= 6 && p[1] >= 6) {
2840 /* correctly formed compress option */
2841 continue;
2842 }
2843 if (debug)
2844 addlog(" [invalid]");
2845 break;
2846 #endif
2847 case IPCP_OPT_ADDRESS:
2848 if (len >= 6 && p[1] == 6) {
2849 /* correctly formed address option */
2850 continue;
2851 }
2852 if (debug)
2853 addlog(" [invalid]");
2854 break;
2855 default:
2856 /* Others not supported. */
2857 if (debug)
2858 addlog(" [rej]");
2859 break;
2860 }
2861 /* Add the option to rejected list. */
2862 bcopy (p, r, p[1]);
2863 r += p[1];
2864 rlen += p[1];
2865 }
2866 if (rlen) {
2867 if (debug)
2868 addlog(" send conf-rej\n");
2869 sppp_cp_send(sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2870 goto end;
2871 } else if (debug)
2872 addlog("\n");
2873
2874 /* pass 2: parse option values */
2875 if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
2876 hisaddr = sp->ipcp.req_hisaddr; /* we already aggreed on that */
2877 else
2878 #ifdef INET
2879 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); /* user configuration */
2880 #else
2881 hisaddr = 0;
2882 #endif
2883 if (debug)
2884 log(LOG_DEBUG, "%s: ipcp parse opt values: ",
2885 ifp->if_xname);
2886 p = (void *)(h + 1);
2887 len = origlen;
2888 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2889 if (debug)
2890 addlog(" %s", sppp_ipcp_opt_name(*p));
2891 switch (*p) {
2892 #ifdef notyet
2893 case IPCP_OPT_COMPRESSION:
2894 continue;
2895 #endif
2896 case IPCP_OPT_ADDRESS:
2897 desiredaddr = p[2] << 24 | p[3] << 16 |
2898 p[4] << 8 | p[5];
2899 if (desiredaddr == hisaddr ||
2900 ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
2901 /*
2902 * Peer's address is same as our value,
2903 * this is agreeable. Gonna conf-ack
2904 * it.
2905 */
2906 if (debug)
2907 addlog(" %s [ack]",
2908 sppp_dotted_quad(hisaddr));
2909 /* record that we've seen it already */
2910 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2911 sp->ipcp.req_hisaddr = desiredaddr;
2912 hisaddr = desiredaddr;
2913 continue;
2914 }
2915 /*
2916 * The address wasn't agreeable. This is either
2917 * he sent us 0.0.0.0, asking to assign him an
2918 * address, or he send us another address not
2919 * matching our value. Either case, we gonna
2920 * conf-nak it with our value.
2921 */
2922 if (debug) {
2923 if (desiredaddr == 0)
2924 addlog(" [addr requested]");
2925 else
2926 addlog(" %s [not agreed]",
2927 sppp_dotted_quad(desiredaddr));
2928 }
2929
2930 p[2] = hisaddr >> 24;
2931 p[3] = hisaddr >> 16;
2932 p[4] = hisaddr >> 8;
2933 p[5] = hisaddr;
2934 break;
2935 }
2936 /* Add the option to nak'ed list. */
2937 bcopy (p, r, p[1]);
2938 r += p[1];
2939 rlen += p[1];
2940 }
2941
2942 /*
2943 * If we are about to conf-ack the request, but haven't seen
2944 * his address so far, gonna conf-nak it instead, with the
2945 * `address' option present and our idea of his address being
2946 * filled in there, to request negotiation of both addresses.
2947 *
2948 * XXX This can result in an endless req - nak loop if peer
2949 * doesn't want to send us his address. Q: What should we do
2950 * about it? XXX A: implement the max-failure counter.
2951 */
2952 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2953 buf[0] = IPCP_OPT_ADDRESS;
2954 buf[1] = 6;
2955 buf[2] = hisaddr >> 24;
2956 buf[3] = hisaddr >> 16;
2957 buf[4] = hisaddr >> 8;
2958 buf[5] = hisaddr;
2959 rlen = 6;
2960 if (debug)
2961 addlog(" still need hisaddr");
2962 }
2963
2964 if (rlen) {
2965 if (debug)
2966 addlog(" send conf-nak\n");
2967 sppp_cp_send(sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2968 } else {
2969 if (debug)
2970 addlog(" send conf-ack\n");
2971 sppp_cp_send(sp, PPP_IPCP, CONF_ACK, h->ident, origlen, h + 1);
2972 }
2973
2974 end:
2975 free(buf, M_TEMP);
2976 return (rlen == 0);
2977 }
2978
2979 /*
2980 * Analyze the IPCP Configure-Reject option list, and adjust our
2981 * negotiation.
2982 */
2983 static void
2984 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2985 {
2986 u_char *buf, *p;
2987 struct ifnet *ifp = &sp->pp_if;
2988 int debug = ifp->if_flags & IFF_DEBUG;
2989
2990 len -= 4;
2991 buf = malloc (len, M_TEMP, M_NOWAIT);
2992 if (!buf)
2993 return;
2994
2995 if (debug)
2996 log(LOG_DEBUG, "%s: ipcp rej opts:",
2997 ifp->if_xname);
2998
2999 p = (void *)(h + 1);
3000 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3001 if (debug)
3002 addlog(" %s", sppp_ipcp_opt_name(*p));
3003 switch (*p) {
3004 case IPCP_OPT_ADDRESS:
3005 /*
3006 * Peer doesn't grok address option. This is
3007 * bad. XXX Should we better give up here?
3008 */
3009 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3010 break;
3011 #ifdef notyet
3012 case IPCP_OPT_COMPRESS:
3013 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
3014 break;
3015 #endif
3016 }
3017 }
3018 if (debug)
3019 addlog("\n");
3020 free(buf, M_TEMP);
3021 return;
3022 }
3023
3024 /*
3025 * Analyze the IPCP Configure-NAK option list, and adjust our
3026 * negotiation.
3027 */
3028 static void
3029 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3030 {
3031 u_char *p;
3032 struct ifnet *ifp = &sp->pp_if;
3033 int debug = ifp->if_flags & IFF_DEBUG;
3034 u_int32_t wantaddr;
3035
3036 len -= 4;
3037
3038 if (debug)
3039 log(LOG_DEBUG, "%s: ipcp nak opts:",
3040 ifp->if_xname);
3041
3042 p = (void *)(h + 1);
3043 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3044 if (debug)
3045 addlog(" %s", sppp_ipcp_opt_name(*p));
3046 switch (*p) {
3047 case IPCP_OPT_ADDRESS:
3048 /*
3049 * Peer doesn't like our local IP address. See
3050 * if we can do something for him. We'll drop
3051 * him our address then.
3052 */
3053 if (len >= 6 && p[1] == 6) {
3054 wantaddr = p[2] << 24 | p[3] << 16 |
3055 p[4] << 8 | p[5];
3056 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3057 if (debug)
3058 addlog(" [wantaddr %s]",
3059 sppp_dotted_quad(wantaddr));
3060 /*
3061 * When doing dynamic address assignment,
3062 * we accept his offer. Otherwise, we
3063 * ignore it and thus continue to negotiate
3064 * our already existing value.
3065 */
3066 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3067 if (debug)
3068 addlog(" [agree]");
3069 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3070 sp->ipcp.req_myaddr = wantaddr;
3071 }
3072 }
3073 break;
3074
3075 case IPCP_OPT_PRIMDNS:
3076 if (len >= 6 && p[1] == 6) {
3077 sp->dns_addrs[0] = p[2] << 24 | p[3] << 16 |
3078 p[4] << 8 | p[5];
3079 }
3080 break;
3081
3082 case IPCP_OPT_SECDNS:
3083 if (len >= 6 && p[1] == 6) {
3084 sp->dns_addrs[1] = p[2] << 24 | p[3] << 16 |
3085 p[4] << 8 | p[5];
3086 }
3087 break;
3088 #ifdef notyet
3089 case IPCP_OPT_COMPRESS:
3090 /*
3091 * Peer wants different compression parameters.
3092 */
3093 break;
3094 #endif
3095 }
3096 }
3097 if (debug)
3098 addlog("\n");
3099 return;
3100 }
3101
3102 static void
3103 sppp_ipcp_tlu(struct sppp *sp)
3104 {
3105 #ifdef INET
3106 /* we are up. Set addresses and notify anyone interested */
3107 STDDCL;
3108 u_int32_t myaddr, hisaddr;
3109
3110 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
3111 if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
3112 myaddr = sp->ipcp.req_myaddr;
3113 if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
3114 hisaddr = sp->ipcp.req_hisaddr;
3115 sppp_set_ip_addrs(sp, myaddr, hisaddr);
3116
3117 if (ifp->if_mtu > sp->lcp.their_mru) {
3118 sp->pp_saved_mtu = ifp->if_mtu;
3119 ifp->if_mtu = sp->lcp.their_mru;
3120 if (debug)
3121 log(LOG_DEBUG,
3122 "%s: setting MTU to %" PRIu64 " bytes\n",
3123 ifp->if_xname, ifp->if_mtu);
3124 }
3125
3126 if (sp->pp_con)
3127 sp->pp_con(sp);
3128 #endif
3129 }
3130
3131 static void
3132 sppp_ipcp_tld(struct sppp *sp)
3133 {
3134 }
3135
3136 static void
3137 sppp_ipcp_tls(struct sppp *sp)
3138 {
3139 /* indicate to LCP that it must stay alive */
3140 sp->lcp.protos |= (1 << IDX_IPCP);
3141 }
3142
3143 static void
3144 sppp_ipcp_tlf(struct sppp *sp)
3145 {
3146 /* we no longer need LCP */
3147 sp->lcp.protos &= ~(1 << IDX_IPCP);
3148 }
3149
3150 static void
3151 sppp_ipcp_scr(struct sppp *sp)
3152 {
3153 char opt[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
3154 #ifdef INET
3155 u_int32_t ouraddr;
3156 #endif
3157 int i = 0;
3158
3159 #ifdef notyet
3160 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3161 opt[i++] = IPCP_OPT_COMPRESSION;
3162 opt[i++] = 6;
3163 opt[i++] = 0; /* VJ header compression */
3164 opt[i++] = 0x2d; /* VJ header compression */
3165 opt[i++] = max_slot_id;
3166 opt[i++] = comp_slot_id;
3167 }
3168 #endif
3169
3170 #ifdef INET
3171 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3172 if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
3173 ouraddr = sp->ipcp.req_myaddr; /* not sure if this can ever happen */
3174 else
3175 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3176 opt[i++] = IPCP_OPT_ADDRESS;
3177 opt[i++] = 6;
3178 opt[i++] = ouraddr >> 24;
3179 opt[i++] = ouraddr >> 16;
3180 opt[i++] = ouraddr >> 8;
3181 opt[i++] = ouraddr;
3182 }
3183 #endif
3184
3185 if (sp->query_dns & 1) {
3186 opt[i++] = IPCP_OPT_PRIMDNS;
3187 opt[i++] = 6;
3188 opt[i++] = sp->dns_addrs[0] >> 24;
3189 opt[i++] = sp->dns_addrs[0] >> 16;
3190 opt[i++] = sp->dns_addrs[0] >> 8;
3191 opt[i++] = sp->dns_addrs[0];
3192 }
3193 if (sp->query_dns & 2) {
3194 opt[i++] = IPCP_OPT_SECDNS;
3195 opt[i++] = 6;
3196 opt[i++] = sp->dns_addrs[1] >> 24;
3197 opt[i++] = sp->dns_addrs[1] >> 16;
3198 opt[i++] = sp->dns_addrs[1] >> 8;
3199 opt[i++] = sp->dns_addrs[1];
3200 }
3201
3202 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3203 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3204 }
3205
3206
3207 /*
3208 *--------------------------------------------------------------------------*
3209 * *
3210 * The IPv6CP implementation. *
3211 * *
3212 *--------------------------------------------------------------------------*
3213 */
3214
3215 #ifdef INET6
3216 static void
3217 sppp_ipv6cp_init(struct sppp *sp)
3218 {
3219 sp->ipv6cp.opts = 0;
3220 sp->ipv6cp.flags = 0;
3221 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3222 sp->fail_counter[IDX_IPV6CP] = 0;
3223 sp->pp_seq[IDX_IPV6CP] = 0;
3224 sp->pp_rseq[IDX_IPV6CP] = 0;
3225 callout_init(&sp->ch[IDX_IPV6CP]);
3226 }
3227
3228 static void
3229 sppp_ipv6cp_up(struct sppp *sp)
3230 {
3231 sppp_up_event(&ipv6cp, sp);
3232 }
3233
3234 static void
3235 sppp_ipv6cp_down(struct sppp *sp)
3236 {
3237 sppp_down_event(&ipv6cp, sp);
3238 }
3239
3240 static void
3241 sppp_ipv6cp_open(struct sppp *sp)
3242 {
3243 STDDCL;
3244 struct in6_addr myaddr, hisaddr;
3245
3246 #ifdef IPV6CP_MYIFID_DYN
3247 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3248 #else
3249 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3250 #endif
3251
3252 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3253 /*
3254 * If we don't have our address, this probably means our
3255 * interface doesn't want to talk IPv6 at all. (This could
3256 * be the case if somebody wants to speak only IPX, for
3257 * example.) Don't open IPv6CP in this case.
3258 */
3259 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3260 /* XXX this message should go away */
3261 if (debug)
3262 log(LOG_DEBUG, "%s: ipv6cp_open(): no IPv6 interface\n",
3263 ifp->if_xname);
3264 return;
3265 }
3266
3267 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3268 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3269 sppp_open_event(&ipv6cp, sp);
3270 }
3271
3272 static void
3273 sppp_ipv6cp_close(struct sppp *sp)
3274 {
3275 sppp_close_event(&ipv6cp, sp);
3276 }
3277
3278 static void
3279 sppp_ipv6cp_TO(void *cookie)
3280 {
3281 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3282 }
3283
3284 /*
3285 * Analyze a configure request. Return true if it was agreeable, and
3286 * caused action sca, false if it has been rejected or nak'ed, and
3287 * caused action scn. (The return value is used to make the state
3288 * transition decision in the state automaton.)
3289 */
3290 static int
3291 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3292 {
3293 u_char *buf, *r, *p;
3294 struct ifnet *ifp = &sp->pp_if;
3295 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3296 struct in6_addr myaddr, desiredaddr, suggestaddr;
3297 int ifidcount;
3298 int type;
3299 int collision, nohisaddr;
3300
3301 len -= 4;
3302 origlen = len;
3303 /*
3304 * Make sure to allocate a buf that can at least hold a
3305 * conf-nak with an `address' option. We might need it below.
3306 */
3307 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3308 if (! buf)
3309 return (0);
3310
3311 /* pass 1: see if we can recognize them */
3312 if (debug)
3313 log(LOG_DEBUG, "%s: ipv6cp parse opts:",
3314 ifp->if_xname);
3315 p = (void *)(h + 1);
3316 ifidcount = 0;
3317 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3318 if (debug)
3319 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3320 switch (*p) {
3321 case IPV6CP_OPT_IFID:
3322 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3323 /* correctly formed address option */
3324 ifidcount++;
3325 continue;
3326 }
3327 if (debug)
3328 addlog(" [invalid]");
3329 break;
3330 #ifdef notyet
3331 case IPV6CP_OPT_COMPRESSION:
3332 if (len >= 4 && p[1] >= 4) {
3333 /* correctly formed compress option */
3334 continue;
3335 }
3336 if (debug)
3337 addlog(" [invalid]");
3338 break;
3339 #endif
3340 default:
3341 /* Others not supported. */
3342 if (debug)
3343 addlog(" [rej]");
3344 break;
3345 }
3346 /* Add the option to rejected list. */
3347 bcopy (p, r, p[1]);
3348 r += p[1];
3349 rlen += p[1];
3350 }
3351 if (rlen) {
3352 if (debug)
3353 addlog(" send conf-rej\n");
3354 sppp_cp_send(sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3355 goto end;
3356 } else if (debug)
3357 addlog("\n");
3358
3359 /* pass 2: parse option values */
3360 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3361 if (debug)
3362 log(LOG_DEBUG, "%s: ipv6cp parse opt values: ",
3363 ifp->if_xname);
3364 p = (void *)(h + 1);
3365 len = origlen;
3366 type = CONF_ACK;
3367 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3368 if (debug)
3369 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3370 switch (*p) {
3371 #ifdef notyet
3372 case IPV6CP_OPT_COMPRESSION:
3373 continue;
3374 #endif
3375 case IPV6CP_OPT_IFID:
3376 memset(&desiredaddr, 0, sizeof(desiredaddr));
3377 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3378 collision = (memcmp(&desiredaddr.s6_addr[8],
3379 &myaddr.s6_addr[8], 8) == 0);
3380 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3381
3382 desiredaddr.s6_addr16[0] = htons(0xfe80);
3383 (void)in6_setscope(&desiredaddr, &sp->pp_if, NULL);
3384
3385 if (!collision && !nohisaddr) {
3386 /* no collision, hisaddr known - Conf-Ack */
3387 type = CONF_ACK;
3388
3389 if (debug) {
3390 addlog(" %s [%s]",
3391 ip6_sprintf(&desiredaddr),
3392 sppp_cp_type_name(type));
3393 }
3394 continue;
3395 }
3396
3397 memset(&suggestaddr, 0, sizeof(&suggestaddr));
3398 if (collision && nohisaddr) {
3399 /* collision, hisaddr unknown - Conf-Rej */
3400 type = CONF_REJ;
3401 memset(&p[2], 0, 8);
3402 } else {
3403 /*
3404 * - no collision, hisaddr unknown, or
3405 * - collision, hisaddr known
3406 * Conf-Nak, suggest hisaddr
3407 */
3408 type = CONF_NAK;
3409 sppp_suggest_ip6_addr(sp, &suggestaddr);
3410 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3411 }
3412 if (debug)
3413 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3414 sppp_cp_type_name(type));
3415 break;
3416 }
3417 /* Add the option to nak'ed list. */
3418 bcopy (p, r, p[1]);
3419 r += p[1];
3420 rlen += p[1];
3421 }
3422
3423 if (rlen == 0 && type == CONF_ACK) {
3424 if (debug)
3425 addlog(" send %s\n", sppp_cp_type_name(type));
3426 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, origlen, h + 1);
3427 } else {
3428 #ifdef notdef
3429 if (type == CONF_ACK)
3430 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3431 #endif
3432
3433 if (debug) {
3434 addlog(" send %s suggest %s\n",
3435 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3436 }
3437 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3438 }
3439
3440 end:
3441 free(buf, M_TEMP);
3442 return (rlen == 0);
3443 }
3444
3445 /*
3446 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3447 * negotiation.
3448 */
3449 static void
3450 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3451 {
3452 u_char *buf, *p;
3453 struct ifnet *ifp = &sp->pp_if;
3454 int debug = ifp->if_flags & IFF_DEBUG;
3455
3456 len -= 4;
3457 buf = malloc (len, M_TEMP, M_NOWAIT);
3458 if (!buf)
3459 return;
3460
3461 if (debug)
3462 log(LOG_DEBUG, "%s: ipv6cp rej opts:",
3463 ifp->if_xname);
3464
3465 p = (void *)(h + 1);
3466 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3467 if (debug)
3468 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3469 switch (*p) {
3470 case IPV6CP_OPT_IFID:
3471 /*
3472 * Peer doesn't grok address option. This is
3473 * bad. XXX Should we better give up here?
3474 */
3475 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3476 break;
3477 #ifdef notyet
3478 case IPV6CP_OPT_COMPRESS:
3479 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3480 break;
3481 #endif
3482 }
3483 }
3484 if (debug)
3485 addlog("\n");
3486 free(buf, M_TEMP);
3487 return;
3488 }
3489
3490 /*
3491 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3492 * negotiation.
3493 */
3494 static void
3495 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3496 {
3497 u_char *buf, *p;
3498 struct ifnet *ifp = &sp->pp_if;
3499 int debug = ifp->if_flags & IFF_DEBUG;
3500 struct in6_addr suggestaddr;
3501
3502 len -= 4;
3503 buf = malloc (len, M_TEMP, M_NOWAIT);
3504 if (!buf)
3505 return;
3506
3507 if (debug)
3508 log(LOG_DEBUG, "%s: ipv6cp nak opts:",
3509 ifp->if_xname);
3510
3511 p = (void *)(h + 1);
3512 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3513 if (debug)
3514 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3515 switch (*p) {
3516 case IPV6CP_OPT_IFID:
3517 /*
3518 * Peer doesn't like our local ifid. See
3519 * if we can do something for him. We'll drop
3520 * him our address then.
3521 */
3522 if (len < 10 || p[1] != 10)
3523 break;
3524 memset(&suggestaddr, 0, sizeof(suggestaddr));
3525 suggestaddr.s6_addr16[0] = htons(0xfe80);
3526 (void)in6_setscope(&suggestaddr, &sp->pp_if, NULL);
3527 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3528
3529 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3530 if (debug)
3531 addlog(" [suggestaddr %s]",
3532 ip6_sprintf(&suggestaddr));
3533 #ifdef IPV6CP_MYIFID_DYN
3534 /*
3535 * When doing dynamic address assignment,
3536 * we accept his offer.
3537 */
3538 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3539 struct in6_addr lastsuggest;
3540 /*
3541 * If <suggested myaddr from peer> equals to
3542 * <hisaddr we have suggested last time>,
3543 * we have a collision. generate new random
3544 * ifid.
3545 */
3546 sppp_suggest_ip6_addr(&lastsuggest);
3547 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3548 lastsuggest)) {
3549 if (debug)
3550 addlog(" [random]");
3551 sppp_gen_ip6_addr(sp, &suggestaddr);
3552 }
3553 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3554 if (debug)
3555 addlog(" [agree]");
3556 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3557 }
3558 #else
3559 /*
3560 * Since we do not do dynamic address assignment,
3561 * we ignore it and thus continue to negotiate
3562 * our already existing value. This can possibly
3563 * go into infinite request-reject loop.
3564 *
3565 * This is not likely because we normally use
3566 * ifid based on MAC-address.
3567 * If you have no ethernet card on the node, too bad.
3568 * XXX should we use fail_counter?
3569 */
3570 #endif
3571 break;
3572 #ifdef notyet
3573 case IPV6CP_OPT_COMPRESS:
3574 /*
3575 * Peer wants different compression parameters.
3576 */
3577 break;
3578 #endif
3579 }
3580 }
3581 if (debug)
3582 addlog("\n");
3583 free(buf, M_TEMP);
3584 return;
3585 }
3586
3587 static void
3588 sppp_ipv6cp_tlu(struct sppp *sp)
3589 {
3590 /* we are up - notify isdn daemon */
3591 if (sp->pp_con)
3592 sp->pp_con(sp);
3593 }
3594
3595 static void
3596 sppp_ipv6cp_tld(struct sppp *sp)
3597 {
3598 }
3599
3600 static void
3601 sppp_ipv6cp_tls(struct sppp *sp)
3602 {
3603 /* indicate to LCP that it must stay alive */
3604 sp->lcp.protos |= (1 << IDX_IPV6CP);
3605 }
3606
3607 static void
3608 sppp_ipv6cp_tlf(struct sppp *sp)
3609 {
3610 /* we no longer need LCP */
3611 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3612 }
3613
3614 static void
3615 sppp_ipv6cp_scr(struct sppp *sp)
3616 {
3617 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3618 struct in6_addr ouraddr;
3619 int i = 0;
3620
3621 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3622 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3623 opt[i++] = IPV6CP_OPT_IFID;
3624 opt[i++] = 10;
3625 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3626 i += 8;
3627 }
3628
3629 #ifdef notyet
3630 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3631 opt[i++] = IPV6CP_OPT_COMPRESSION;
3632 opt[i++] = 4;
3633 opt[i++] = 0; /* TBD */
3634 opt[i++] = 0; /* TBD */
3635 /* variable length data may follow */
3636 }
3637 #endif
3638
3639 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3640 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3641 }
3642 #else /*INET6*/
3643 static void sppp_ipv6cp_init(struct sppp *sp)
3644 {
3645 }
3646
3647 static void sppp_ipv6cp_up(struct sppp *sp)
3648 {
3649 }
3650
3651 static void sppp_ipv6cp_down(struct sppp *sp)
3652 {
3653 }
3654
3655
3656 static void sppp_ipv6cp_open(struct sppp *sp)
3657 {
3658 }
3659
3660 static void sppp_ipv6cp_close(struct sppp *sp)
3661 {
3662 }
3663
3664 static void sppp_ipv6cp_TO(void *sp)
3665 {
3666 }
3667
3668 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3669 {
3670 return 0;
3671 }
3672
3673 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3674 {
3675 }
3676
3677 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3678 {
3679 }
3680
3681 static void sppp_ipv6cp_tlu(struct sppp *sp)
3682 {
3683 }
3684
3685 static void sppp_ipv6cp_tld(struct sppp *sp)
3686 {
3687 }
3688
3689 static void sppp_ipv6cp_tls(struct sppp *sp)
3690 {
3691 }
3692
3693 static void sppp_ipv6cp_tlf(struct sppp *sp)
3694 {
3695 }
3696
3697 static void sppp_ipv6cp_scr(struct sppp *sp)
3698 {
3699 }
3700 #endif /*INET6*/
3701
3702
3703 /*
3704 *--------------------------------------------------------------------------*
3705 * *
3706 * The CHAP implementation. *
3707 * *
3708 *--------------------------------------------------------------------------*
3709 */
3710
3711 /*
3712 * The authentication protocols don't employ a full-fledged state machine as
3713 * the control protocols do, since they do have Open and Close events, but
3714 * not Up and Down, nor are they explicitly terminated. Also, use of the
3715 * authentication protocols may be different in both directions (this makes
3716 * sense, think of a machine that never accepts incoming calls but only
3717 * calls out, it doesn't require the called party to authenticate itself).
3718 *
3719 * Our state machine for the local authentication protocol (we are requesting
3720 * the peer to authenticate) looks like:
3721 *
3722 * RCA-
3723 * +--------------------------------------------+
3724 * V scn,tld|
3725 * +--------+ Close +---------+ RCA+
3726 * | |<----------------------------------| |------+
3727 * +--->| Closed | TO* | Opened | sca |
3728 * | | |-----+ +-------| |<-----+
3729 * | +--------+ irc | | +---------+
3730 * | ^ | | ^
3731 * | | | | |
3732 * | | | | |
3733 * | TO-| | | |
3734 * | |tld TO+ V | |
3735 * | | +------->+ | |
3736 * | | | | | |
3737 * | +--------+ V | |
3738 * | | |<----+<--------------------+ |
3739 * | | Req- | scr |
3740 * | | Sent | |
3741 * | | | |
3742 * | +--------+ |
3743 * | RCA- | | RCA+ |
3744 * +------+ +------------------------------------------+
3745 * scn,tld sca,irc,ict,tlu
3746 *
3747 *
3748 * with:
3749 *
3750 * Open: LCP reached authentication phase
3751 * Close: LCP reached terminate phase
3752 *
3753 * RCA+: received reply (pap-req, chap-response), acceptable
3754 * RCN: received reply (pap-req, chap-response), not acceptable
3755 * TO+: timeout with restart counter >= 0
3756 * TO-: timeout with restart counter < 0
3757 * TO*: reschedule timeout for CHAP
3758 *
3759 * scr: send request packet (none for PAP, chap-challenge)
3760 * sca: send ack packet (pap-ack, chap-success)
3761 * scn: send nak packet (pap-nak, chap-failure)
3762 * ict: initialize re-challenge timer (CHAP only)
3763 *
3764 * tlu: this-layer-up, LCP reaches network phase
3765 * tld: this-layer-down, LCP enters terminate phase
3766 *
3767 * Note that in CHAP mode, after sending a new challenge, while the state
3768 * automaton falls back into Req-Sent state, it doesn't signal a tld
3769 * event to LCP, so LCP remains in network phase. Only after not getting
3770 * any response (or after getting an unacceptable response), CHAP closes,
3771 * causing LCP to enter terminate phase.
3772 *
3773 * With PAP, there is no initial request that can be sent. The peer is
3774 * expected to send one based on the successful negotiation of PAP as
3775 * the authentication protocol during the LCP option negotiation.
3776 *
3777 * Incoming authentication protocol requests (remote requests
3778 * authentication, we are peer) don't employ a state machine at all,
3779 * they are simply answered. Some peers [Ascend P50 firmware rev
3780 * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
3781 * still in authentication phase (thereby violating the standard that
3782 * demands that these NCP packets are to be discarded), so we keep
3783 * track of the peer demanding us to authenticate, and only proceed to
3784 * phase network once we've seen a positive acknowledge for the
3785 * authentication.
3786 */
3787
3788 /*
3789 * Handle incoming CHAP packets.
3790 */
3791 void
3792 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3793 {
3794 STDDCL;
3795 struct lcp_header *h;
3796 int len, x;
3797 u_char *value, *name, digest[sizeof(sp->myauth.challenge)], dsize;
3798 int value_len, name_len;
3799 MD5_CTX ctx;
3800
3801 len = m->m_pkthdr.len;
3802 if (len < 4) {
3803 if (debug)
3804 log(LOG_DEBUG,
3805 "%s: chap invalid packet length: %d bytes\n",
3806 ifp->if_xname, len);
3807 return;
3808 }
3809 h = mtod(m, struct lcp_header *);
3810 if (len > ntohs(h->len))
3811 len = ntohs(h->len);
3812
3813 switch (h->type) {
3814 /* challenge, failure and success are his authproto */
3815 case CHAP_CHALLENGE:
3816 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
3817 /* can't do anything usefull */
3818 sp->pp_auth_failures++;
3819 printf("%s: chap input without my name and my secret being set\n",
3820 ifp->if_xname);
3821 break;
3822 }
3823 value = 1 + (u_char *)(h + 1);
3824 value_len = value[-1];
3825 name = value + value_len;
3826 name_len = len - value_len - 5;
3827 if (name_len < 0) {
3828 if (debug) {
3829 log(LOG_DEBUG,
3830 "%s: chap corrupted challenge "
3831 "<%s id=0x%x len=%d",
3832 ifp->if_xname,
3833 sppp_auth_type_name(PPP_CHAP, h->type),
3834 h->ident, ntohs(h->len));
3835 if (len > 4)
3836 sppp_print_bytes((u_char *)(h + 1),
3837 len - 4);
3838 addlog(">\n");
3839 }
3840 break;
3841 }
3842
3843 if (debug) {
3844 log(LOG_DEBUG,
3845 "%s: chap input <%s id=0x%x len=%d name=",
3846 ifp->if_xname,
3847 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3848 ntohs(h->len));
3849 sppp_print_string((char *) name, name_len);
3850 addlog(" value-size=%d value=", value_len);
3851 sppp_print_bytes(value, value_len);
3852 addlog(">\n");
3853 }
3854
3855 /* Compute reply value. */
3856 MD5Init(&ctx);
3857 MD5Update(&ctx, &h->ident, 1);
3858 MD5Update(&ctx, sp->myauth.secret, sp->myauth.secret_len);
3859 MD5Update(&ctx, value, value_len);
3860 MD5Final(digest, &ctx);
3861 dsize = sizeof digest;
3862
3863 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3864 sizeof dsize, (const char *)&dsize,
3865 sizeof digest, digest,
3866 sp->myauth.name_len,
3867 sp->myauth.name,
3868 0);
3869 break;
3870
3871 case CHAP_SUCCESS:
3872 if (debug) {
3873 log(LOG_DEBUG, "%s: chap success",
3874 ifp->if_xname);
3875 if (len > 4) {
3876 addlog(": ");
3877 sppp_print_string((char *)(h + 1), len - 4);
3878 }
3879 addlog("\n");
3880 }
3881 x = splnet();
3882 sp->pp_auth_failures = 0;
3883 sp->pp_flags &= ~PP_NEEDAUTH;
3884 if (sp->myauth.proto == PPP_CHAP &&
3885 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3886 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3887 /*
3888 * We are authenticator for CHAP but didn't
3889 * complete yet. Leave it to tlu to proceed
3890 * to network phase.
3891 */
3892 splx(x);
3893 break;
3894 }
3895 splx(x);
3896 sppp_phase_network(sp);
3897 break;
3898
3899 case CHAP_FAILURE:
3900 x = splnet();
3901 sp->pp_auth_failures++;
3902 splx(x);
3903 if (debug) {
3904 log(LOG_INFO, "%s: chap failure",
3905 ifp->if_xname);
3906 if (len > 4) {
3907 addlog(": ");
3908 sppp_print_string((char *)(h + 1), len - 4);
3909 }
3910 addlog("\n");
3911 } else
3912 log(LOG_INFO, "%s: chap failure\n",
3913 ifp->if_xname);
3914 /* await LCP shutdown by authenticator */
3915 break;
3916
3917 /* response is my authproto */
3918 case CHAP_RESPONSE:
3919 if (sp->hisauth.secret == NULL) {
3920 /* can't do anything usefull */
3921 printf("%s: chap input without his secret being set\n",
3922 ifp->if_xname);
3923 break;
3924 }
3925 value = 1 + (u_char *)(h + 1);
3926 value_len = value[-1];
3927 name = value + value_len;
3928 name_len = len - value_len - 5;
3929 if (name_len < 0) {
3930 if (debug) {
3931 log(LOG_DEBUG,
3932 "%s: chap corrupted response "
3933 "<%s id=0x%x len=%d",
3934 ifp->if_xname,
3935 sppp_auth_type_name(PPP_CHAP, h->type),
3936 h->ident, ntohs(h->len));
3937 if (len > 4)
3938 sppp_print_bytes((u_char *)(h + 1),
3939 len - 4);
3940 addlog(">\n");
3941 }
3942 break;
3943 }
3944 if (h->ident != sp->confid[IDX_CHAP]) {
3945 if (debug)
3946 log(LOG_DEBUG,
3947 "%s: chap dropping response for old ID "
3948 "(got %d, expected %d)\n",
3949 ifp->if_xname,
3950 h->ident, sp->confid[IDX_CHAP]);
3951 break;
3952 }
3953 if (sp->hisauth.name != NULL &&
3954 (name_len != sp->hisauth.name_len
3955 || memcmp(name, sp->hisauth.name, name_len) != 0)) {
3956 log(LOG_INFO, "%s: chap response, his name ",
3957 ifp->if_xname);
3958 sppp_print_string(name, name_len);
3959 addlog(" != expected ");
3960 sppp_print_string(sp->hisauth.name,
3961 sp->hisauth.name_len);
3962 addlog("\n");
3963 goto chap_failure;
3964 }
3965 if (debug) {
3966 log(LOG_DEBUG, "%s: chap input(%s) "
3967 "<%s id=0x%x len=%d name=",
3968 ifp->if_xname,
3969 sppp_state_name(sp->state[IDX_CHAP]),
3970 sppp_auth_type_name(PPP_CHAP, h->type),
3971 h->ident, ntohs(h->len));
3972 sppp_print_string((char *)name, name_len);
3973 addlog(" value-size=%d value=", value_len);
3974 sppp_print_bytes(value, value_len);
3975 addlog(">\n");
3976 }
3977 if (value_len != sizeof(sp->myauth.challenge)) {
3978 if (debug)
3979 log(LOG_DEBUG,
3980 "%s: chap bad hash value length: "
3981 "%d bytes, should be %ld\n",
3982 ifp->if_xname, value_len,
3983 (long) sizeof(sp->myauth.challenge));
3984 goto chap_failure;
3985 }
3986
3987 MD5Init(&ctx);
3988 MD5Update(&ctx, &h->ident, 1);
3989 MD5Update(&ctx, sp->hisauth.secret, sp->hisauth.secret_len);
3990 MD5Update(&ctx, sp->myauth.challenge, sizeof(sp->myauth.challenge));
3991 MD5Final(digest, &ctx);
3992
3993 #define FAILMSG "Failed..."
3994 #define SUCCMSG "Welcome!"
3995
3996 if (value_len != sizeof digest ||
3997 memcmp(digest, value, value_len) != 0) {
3998 chap_failure:
3999 /* action scn, tld */
4000 x = splnet();
4001 sp->pp_auth_failures++;
4002 splx(x);
4003 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4004 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4005 0);
4006 chap.tld(sp);
4007 break;
4008 }
4009 sp->pp_auth_failures = 0;
4010 /* action sca, perhaps tlu */
4011 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4012 sp->state[IDX_CHAP] == STATE_OPENED)
4013 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4014 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4015 0);
4016 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4017 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4018 chap.tlu(sp);
4019 }
4020 break;
4021
4022 default:
4023 /* Unknown CHAP packet type -- ignore. */
4024 if (debug) {
4025 log(LOG_DEBUG, "%s: chap unknown input(%s) "
4026 "<0x%x id=0x%xh len=%d",
4027 ifp->if_xname,
4028 sppp_state_name(sp->state[IDX_CHAP]),
4029 h->type, h->ident, ntohs(h->len));
4030 if (len > 4)
4031 sppp_print_bytes((u_char *)(h + 1), len - 4);
4032 addlog(">\n");
4033 }
4034 break;
4035
4036 }
4037 }
4038
4039 static void
4040 sppp_chap_init(struct sppp *sp)
4041 {
4042 /* Chap doesn't have STATE_INITIAL at all. */
4043 sp->state[IDX_CHAP] = STATE_CLOSED;
4044 sp->fail_counter[IDX_CHAP] = 0;
4045 sp->pp_seq[IDX_CHAP] = 0;
4046 sp->pp_rseq[IDX_CHAP] = 0;
4047 callout_init(&sp->ch[IDX_CHAP]);
4048 }
4049
4050 static void
4051 sppp_chap_open(struct sppp *sp)
4052 {
4053 if (sp->myauth.proto == PPP_CHAP &&
4054 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4055 /* we are authenticator for CHAP, start it */
4056 chap.scr(sp);
4057 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4058 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4059 }
4060 /* nothing to be done if we are peer, await a challenge */
4061 }
4062
4063 static void
4064 sppp_chap_close(struct sppp *sp)
4065 {
4066 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4067 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4068 }
4069
4070 static void
4071 sppp_chap_TO(void *cookie)
4072 {
4073 struct sppp *sp = (struct sppp *)cookie;
4074 STDDCL;
4075 int s;
4076
4077 s = splnet();
4078 if (debug)
4079 log(LOG_DEBUG, "%s: chap TO(%s) rst_counter = %d\n",
4080 ifp->if_xname,
4081 sppp_state_name(sp->state[IDX_CHAP]),
4082 sp->rst_counter[IDX_CHAP]);
4083
4084 if (--sp->rst_counter[IDX_CHAP] < 0)
4085 /* TO- event */
4086 switch (sp->state[IDX_CHAP]) {
4087 case STATE_REQ_SENT:
4088 chap.tld(sp);
4089 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4090 break;
4091 }
4092 else
4093 /* TO+ (or TO*) event */
4094 switch (sp->state[IDX_CHAP]) {
4095 case STATE_OPENED:
4096 /* TO* event */
4097 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4098 /* fall through */
4099 case STATE_REQ_SENT:
4100 chap.scr(sp);
4101 /* sppp_cp_change_state() will restart the timer */
4102 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4103 break;
4104 }
4105
4106 splx(s);
4107 }
4108
4109 static void
4110 sppp_chap_tlu(struct sppp *sp)
4111 {
4112 STDDCL;
4113 int i, x;
4114
4115 i = 0;
4116 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4117
4118 /*
4119 * Some broken CHAP implementations (Conware CoNet, firmware
4120 * 4.0.?) don't want to re-authenticate their CHAP once the
4121 * initial challenge-response exchange has taken place.
4122 * Provide for an option to avoid rechallenges.
4123 */
4124 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
4125 /*
4126 * Compute the re-challenge timeout. This will yield
4127 * a number between 300 and 810 seconds.
4128 */
4129 i = 300 + ((unsigned)(arc4random() & 0xff00) >> 7);
4130
4131 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, sp);
4132 }
4133
4134 if (debug) {
4135 log(LOG_DEBUG,
4136 "%s: chap %s, ",
4137 ifp->if_xname,
4138 sp->pp_phase == SPPP_PHASE_NETWORK? "reconfirmed": "tlu");
4139 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0)
4140 addlog("next re-challenge in %d seconds\n", i);
4141 else
4142 addlog("re-challenging supressed\n");
4143 }
4144
4145 x = splnet();
4146 sp->pp_auth_failures = 0;
4147 /* indicate to LCP that we need to be closed down */
4148 sp->lcp.protos |= (1 << IDX_CHAP);
4149
4150 if (sp->pp_flags & PP_NEEDAUTH) {
4151 /*
4152 * Remote is authenticator, but his auth proto didn't
4153 * complete yet. Defer the transition to network
4154 * phase.
4155 */
4156 splx(x);
4157 return;
4158 }
4159 splx(x);
4160
4161 /*
4162 * If we are already in phase network, we are done here. This
4163 * is the case if this is a dummy tlu event after a re-challenge.
4164 */
4165 if (sp->pp_phase != SPPP_PHASE_NETWORK)
4166 sppp_phase_network(sp);
4167 }
4168
4169 static void
4170 sppp_chap_tld(struct sppp *sp)
4171 {
4172 STDDCL;
4173
4174 if (debug)
4175 log(LOG_DEBUG, "%s: chap tld\n", ifp->if_xname);
4176 callout_stop(&sp->ch[IDX_CHAP]);
4177 sp->lcp.protos &= ~(1 << IDX_CHAP);
4178
4179 lcp.Close(sp);
4180 }
4181
4182 static void
4183 sppp_chap_scr(struct sppp *sp)
4184 {
4185 u_int32_t *ch;
4186 u_char clen;
4187
4188 if (sp->myauth.name == NULL) {
4189 /* can't do anything usefull */
4190 printf("%s: chap starting without my name being set\n",
4191 sp->pp_if.if_xname);
4192 return;
4193 }
4194
4195 /* Compute random challenge. */
4196 ch = (u_int32_t *)sp->myauth.challenge;
4197 ch[0] = arc4random();
4198 ch[1] = arc4random();
4199 ch[2] = arc4random();
4200 ch[3] = arc4random();
4201 clen = 16; /* 4 * sizeof(u_int32_t) */
4202
4203 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4204
4205 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4206 sizeof clen, (const char *)&clen,
4207 sizeof(sp->myauth.challenge), sp->myauth.challenge,
4208 sp->myauth.name_len,
4209 sp->myauth.name,
4210 0);
4211 }
4212
4213 /*
4214 *--------------------------------------------------------------------------*
4215 * *
4216 * The PAP implementation. *
4217 * *
4218 *--------------------------------------------------------------------------*
4219 */
4220 /*
4221 * For PAP, we need to keep a little state also if we are the peer, not the
4222 * authenticator. This is since we don't get a request to authenticate, but
4223 * have to repeatedly authenticate ourself until we got a response (or the
4224 * retry counter is expired).
4225 */
4226
4227 /*
4228 * Handle incoming PAP packets. */
4229 static void
4230 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4231 {
4232 STDDCL;
4233 struct lcp_header *h;
4234 int len, x;
4235 u_char mlen;
4236 char *name, *secret;
4237 int name_len, secret_len;
4238
4239 len = m->m_pkthdr.len;
4240 if (len < 5) {
4241 if (debug)
4242 log(LOG_DEBUG,
4243 "%s: pap invalid packet length: %d bytes\n",
4244 ifp->if_xname, len);
4245 return;
4246 }
4247 h = mtod(m, struct lcp_header *);
4248 if (len > ntohs(h->len))
4249 len = ntohs(h->len);
4250 switch (h->type) {
4251 /* PAP request is my authproto */
4252 case PAP_REQ:
4253 if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
4254 /* can't do anything usefull */
4255 printf("%s: pap request without his name and his secret being set\n",
4256 ifp->if_xname);
4257 break;
4258 }
4259 name = 1 + (u_char *)(h + 1);
4260 name_len = name[-1];
4261 secret = name + name_len + 1;
4262 if (name_len > len - 6 ||
4263 (secret_len = secret[-1]) > len - 6 - name_len) {
4264 if (debug) {
4265 log(LOG_DEBUG, "%s: pap corrupted input "
4266 "<%s id=0x%x len=%d",
4267 ifp->if_xname,
4268 sppp_auth_type_name(PPP_PAP, h->type),
4269 h->ident, ntohs(h->len));
4270 if (len > 4)
4271 sppp_print_bytes((u_char *)(h + 1),
4272 len - 4);
4273 addlog(">\n");
4274 }
4275 break;
4276 }
4277 if (debug) {
4278 log(LOG_DEBUG, "%s: pap input(%s) "
4279 "<%s id=0x%x len=%d name=",
4280 ifp->if_xname,
4281 sppp_state_name(sp->state[IDX_PAP]),
4282 sppp_auth_type_name(PPP_PAP, h->type),
4283 h->ident, ntohs(h->len));
4284 sppp_print_string((char *)name, name_len);
4285 addlog(" secret=");
4286 sppp_print_string((char *)secret, secret_len);
4287 addlog(">\n");
4288 }
4289 if (name_len != sp->hisauth.name_len ||
4290 secret_len != sp->hisauth.secret_len ||
4291 memcmp(name, sp->hisauth.name, name_len) != 0 ||
4292 memcmp(secret, sp->hisauth.secret, secret_len) != 0) {
4293 /* action scn, tld */
4294 sp->pp_auth_failures++;
4295 mlen = sizeof(FAILMSG) - 1;
4296 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4297 sizeof mlen, (const char *)&mlen,
4298 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4299 0);
4300 pap.tld(sp);
4301 break;
4302 }
4303 /* action sca, perhaps tlu */
4304 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4305 sp->state[IDX_PAP] == STATE_OPENED) {
4306 mlen = sizeof(SUCCMSG) - 1;
4307 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4308 sizeof mlen, (const char *)&mlen,
4309 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4310 0);
4311 }
4312 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4313 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4314 pap.tlu(sp);
4315 }
4316 break;
4317
4318 /* ack and nak are his authproto */
4319 case PAP_ACK:
4320 callout_stop(&sp->pap_my_to_ch);
4321 if (debug) {
4322 log(LOG_DEBUG, "%s: pap success",
4323 ifp->if_xname);
4324 name_len = *(char *)h;
4325 if (len > 5 && name_len) {
4326 addlog(": ");
4327 sppp_print_string((char *)(h + 1), name_len);
4328 }
4329 addlog("\n");
4330 }
4331 x = splnet();
4332 sp->pp_auth_failures = 0;
4333 sp->pp_flags &= ~PP_NEEDAUTH;
4334 if (sp->myauth.proto == PPP_PAP &&
4335 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4336 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4337 /*
4338 * We are authenticator for PAP but didn't
4339 * complete yet. Leave it to tlu to proceed
4340 * to network phase.
4341 */
4342 splx(x);
4343 break;
4344 }
4345 splx(x);
4346 sppp_phase_network(sp);
4347 break;
4348
4349 case PAP_NAK:
4350 callout_stop(&sp->pap_my_to_ch);
4351 sp->pp_auth_failures++;
4352 if (debug) {
4353 log(LOG_INFO, "%s: pap failure",
4354 ifp->if_xname);
4355 name_len = *(char *)h;
4356 if (len > 5 && name_len) {
4357 addlog(": ");
4358 sppp_print_string((char *)(h + 1), name_len);
4359 }
4360 addlog("\n");
4361 } else
4362 log(LOG_INFO, "%s: pap failure\n",
4363 ifp->if_xname);
4364 /* await LCP shutdown by authenticator */
4365 break;
4366
4367 default:
4368 /* Unknown PAP packet type -- ignore. */
4369 if (debug) {
4370 log(LOG_DEBUG, "%s: pap corrupted input "
4371 "<0x%x id=0x%x len=%d",
4372 ifp->if_xname,
4373 h->type, h->ident, ntohs(h->len));
4374 if (len > 4)
4375 sppp_print_bytes((u_char *)(h + 1), len - 4);
4376 addlog(">\n");
4377 }
4378 break;
4379
4380 }
4381 }
4382
4383 static void
4384 sppp_pap_init(struct sppp *sp)
4385 {
4386 /* PAP doesn't have STATE_INITIAL at all. */
4387 sp->state[IDX_PAP] = STATE_CLOSED;
4388 sp->fail_counter[IDX_PAP] = 0;
4389 sp->pp_seq[IDX_PAP] = 0;
4390 sp->pp_rseq[IDX_PAP] = 0;
4391 callout_init(&sp->ch[IDX_PAP]);
4392 callout_init(&sp->pap_my_to_ch);
4393 }
4394
4395 static void
4396 sppp_pap_open(struct sppp *sp)
4397 {
4398 if (sp->hisauth.proto == PPP_PAP &&
4399 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4400 /* we are authenticator for PAP, start our timer */
4401 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4402 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4403 }
4404 if (sp->myauth.proto == PPP_PAP) {
4405 /* we are peer, send a request, and start a timer */
4406 pap.scr(sp);
4407 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4408 sppp_pap_my_TO, sp);
4409 }
4410 }
4411
4412 static void
4413 sppp_pap_close(struct sppp *sp)
4414 {
4415 if (sp->state[IDX_PAP] != STATE_CLOSED)
4416 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4417 }
4418
4419 /*
4420 * That's the timeout routine if we are authenticator. Since the
4421 * authenticator is basically passive in PAP, we can't do much here.
4422 */
4423 static void
4424 sppp_pap_TO(void *cookie)
4425 {
4426 struct sppp *sp = (struct sppp *)cookie;
4427 STDDCL;
4428 int s;
4429
4430 s = splnet();
4431 if (debug)
4432 log(LOG_DEBUG, "%s: pap TO(%s) rst_counter = %d\n",
4433 ifp->if_xname,
4434 sppp_state_name(sp->state[IDX_PAP]),
4435 sp->rst_counter[IDX_PAP]);
4436
4437 if (--sp->rst_counter[IDX_PAP] < 0)
4438 /* TO- event */
4439 switch (sp->state[IDX_PAP]) {
4440 case STATE_REQ_SENT:
4441 pap.tld(sp);
4442 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4443 break;
4444 }
4445 else
4446 /* TO+ event, not very much we could do */
4447 switch (sp->state[IDX_PAP]) {
4448 case STATE_REQ_SENT:
4449 /* sppp_cp_change_state() will restart the timer */
4450 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4451 break;
4452 }
4453
4454 splx(s);
4455 }
4456
4457 /*
4458 * That's the timeout handler if we are peer. Since the peer is active,
4459 * we need to retransmit our PAP request since it is apparently lost.
4460 * XXX We should impose a max counter.
4461 */
4462 static void
4463 sppp_pap_my_TO(void *cookie)
4464 {
4465 struct sppp *sp = (struct sppp *)cookie;
4466 STDDCL;
4467
4468 if (debug)
4469 log(LOG_DEBUG, "%s: pap peer TO\n",
4470 ifp->if_xname);
4471
4472 pap.scr(sp);
4473 }
4474
4475 static void
4476 sppp_pap_tlu(struct sppp *sp)
4477 {
4478 STDDCL;
4479 int x;
4480
4481 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4482
4483 if (debug)
4484 log(LOG_DEBUG, "%s: %s tlu\n",
4485 ifp->if_xname, pap.name);
4486
4487 x = splnet();
4488 sp->pp_auth_failures = 0;
4489 /* indicate to LCP that we need to be closed down */
4490 sp->lcp.protos |= (1 << IDX_PAP);
4491
4492 if (sp->pp_flags & PP_NEEDAUTH) {
4493 /*
4494 * Remote is authenticator, but his auth proto didn't
4495 * complete yet. Defer the transition to network
4496 * phase.
4497 */
4498 splx(x);
4499 return;
4500 }
4501 splx(x);
4502 sppp_phase_network(sp);
4503 }
4504
4505 static void
4506 sppp_pap_tld(struct sppp *sp)
4507 {
4508 STDDCL;
4509
4510 if (debug)
4511 log(LOG_DEBUG, "%s: pap tld\n", ifp->if_xname);
4512 callout_stop(&sp->ch[IDX_PAP]);
4513 callout_stop(&sp->pap_my_to_ch);
4514 sp->lcp.protos &= ~(1 << IDX_PAP);
4515
4516 lcp.Close(sp);
4517 }
4518
4519 static void
4520 sppp_pap_scr(struct sppp *sp)
4521 {
4522 u_char idlen, pwdlen;
4523
4524 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4525 /* can't do anything usefull */
4526 printf("%s: pap starting without my name and secret being set\n",
4527 sp->pp_if.if_xname);
4528 return;
4529 }
4530
4531 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4532 pwdlen = sp->myauth.secret_len;
4533 idlen = sp->myauth.name_len;
4534
4535 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4536 sizeof idlen, (const char *)&idlen,
4537 idlen, sp->myauth.name,
4538 sizeof pwdlen, (const char *)&pwdlen,
4539 pwdlen, sp->myauth.secret,
4540 0);
4541 }
4542
4543 /*
4544 * Random miscellaneous functions.
4545 */
4546
4547 /*
4548 * Send a PAP or CHAP proto packet.
4549 *
4550 * Varadic function, each of the elements for the ellipsis is of type
4551 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4552 * mlen == 0.
4553 * NOTE: never declare variadic functions with types subject to type
4554 * promotion (i.e. u_char). This is asking for big trouble depending
4555 * on the architecture you are on...
4556 */
4557
4558 static void
4559 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4560 unsigned int type, unsigned int id,
4561 ...)
4562 {
4563 STDDCL;
4564 struct lcp_header *lh;
4565 struct mbuf *m;
4566 u_char *p;
4567 int len;
4568 size_t pkthdrlen;
4569 unsigned int mlen;
4570 const char *msg;
4571 va_list ap;
4572
4573 MGETHDR(m, M_DONTWAIT, MT_DATA);
4574 if (! m)
4575 return;
4576 m->m_pkthdr.rcvif = 0;
4577
4578 if (sp->pp_flags & PP_NOFRAMING) {
4579 *mtod(m, u_int16_t *) = htons(cp->proto);
4580 pkthdrlen = 2;
4581 lh = (struct lcp_header *)(mtod(m, u_int8_t *)+2);
4582 } else {
4583 struct ppp_header *h;
4584 h = mtod(m, struct ppp_header *);
4585 h->address = PPP_ALLSTATIONS; /* broadcast address */
4586 h->control = PPP_UI; /* Unnumbered Info */
4587 h->protocol = htons(cp->proto);
4588 pkthdrlen = PPP_HEADER_LEN;
4589
4590 lh = (struct lcp_header *)(h + 1);
4591 }
4592
4593 lh->type = type;
4594 lh->ident = id;
4595 p = (u_char *)(lh + 1);
4596
4597 va_start(ap, id);
4598 len = 0;
4599
4600 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4601 msg = va_arg(ap, const char *);
4602 len += mlen;
4603 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
4604 va_end(ap);
4605 m_freem(m);
4606 return;
4607 }
4608
4609 bcopy(msg, p, mlen);
4610 p += mlen;
4611 }
4612 va_end(ap);
4613
4614 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
4615 lh->len = htons(LCP_HEADER_LEN + len);
4616
4617 if (debug) {
4618 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
4619 ifp->if_xname, cp->name,
4620 sppp_auth_type_name(cp->proto, lh->type),
4621 lh->ident, ntohs(lh->len));
4622 if (len)
4623 sppp_print_bytes((u_char *)(lh + 1), len);
4624 addlog(">\n");
4625 }
4626 if (IF_QFULL(&sp->pp_cpq)) {
4627 IF_DROP(&sp->pp_fastq);
4628 IF_DROP(&ifp->if_snd);
4629 m_freem(m);
4630 ++ifp->if_oerrors;
4631 return;
4632 } else
4633 IF_ENQUEUE(&sp->pp_cpq, m);
4634 if (! (ifp->if_flags & IFF_OACTIVE))
4635 (*ifp->if_start)(ifp);
4636 ifp->if_obytes += m->m_pkthdr.len + 3;
4637 }
4638
4639 /*
4640 * Send keepalive packets, every 10 seconds.
4641 */
4642 static void
4643 sppp_keepalive(void *dummy)
4644 {
4645 struct sppp *sp;
4646 int s;
4647 time_t now;
4648
4649 s = splnet();
4650 now = time_uptime;
4651 for (sp=spppq; sp; sp=sp->pp_next) {
4652 struct ifnet *ifp = &sp->pp_if;
4653
4654 /* check idle timeout */
4655 if ((sp->pp_idle_timeout != 0) && (ifp->if_flags & IFF_RUNNING)
4656 && (sp->pp_phase == SPPP_PHASE_NETWORK)) {
4657 /* idle timeout is enabled for this interface */
4658 if ((now-sp->pp_last_activity) >= sp->pp_idle_timeout) {
4659 if (ifp->if_flags & IFF_DEBUG)
4660 printf("%s: no activity for %lu seconds\n",
4661 sp->pp_if.if_xname,
4662 (unsigned long)(now-sp->pp_last_activity));
4663 lcp.Close(sp);
4664 continue;
4665 }
4666 }
4667
4668 /* Keepalive mode disabled or channel down? */
4669 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4670 ! (ifp->if_flags & IFF_RUNNING))
4671 continue;
4672
4673 /* No keepalive in PPP mode if LCP not opened yet. */
4674 if (! (sp->pp_flags & PP_CISCO) &&
4675 sp->pp_phase < SPPP_PHASE_AUTHENTICATE)
4676 continue;
4677
4678 /* No echo reply, but maybe user data passed through? */
4679 if ((now - sp->pp_last_receive) < sp->pp_max_noreceive) {
4680 sp->pp_alivecnt = 0;
4681 continue;
4682 }
4683
4684 if (sp->pp_alivecnt >= sp->pp_maxalive) {
4685 /* No keepalive packets got. Stop the interface. */
4686 if_down (ifp);
4687 IF_PURGE(&sp->pp_cpq);
4688 if (! (sp->pp_flags & PP_CISCO)) {
4689 printf("%s: LCP keepalive timed out, going to restart the connection\n",
4690 ifp->if_xname);
4691 sp->pp_alivecnt = 0;
4692
4693 /* we are down, close all open protocols */
4694 lcp.Close(sp);
4695
4696 /* And now prepare LCP to reestablish the link, if configured to do so. */
4697 sppp_cp_change_state(&lcp, sp, STATE_STOPPED);
4698
4699 /* Close connection imediatly, completition of this
4700 * will summon the magic needed to reestablish it. */
4701 if (sp->pp_tlf)
4702 sp->pp_tlf(sp);
4703 continue;
4704 }
4705 }
4706 if (sp->pp_alivecnt < sp->pp_maxalive)
4707 ++sp->pp_alivecnt;
4708 if (sp->pp_flags & PP_CISCO)
4709 sppp_cisco_send(sp, CISCO_KEEPALIVE_REQ,
4710 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4711 else if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
4712 int32_t nmagic = htonl(sp->lcp.magic);
4713 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4714 sppp_cp_send(sp, PPP_LCP, ECHO_REQ,
4715 sp->lcp.echoid, 4, &nmagic);
4716 }
4717 }
4718 splx(s);
4719 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
4720 }
4721
4722 #ifdef INET
4723 /*
4724 * Get both IP addresses.
4725 */
4726 static void
4727 sppp_get_ip_addrs(struct sppp *sp, u_int32_t *src, u_int32_t *dst, u_int32_t *srcmask)
4728 {
4729 struct ifnet *ifp = &sp->pp_if;
4730 struct ifaddr *ifa;
4731 struct sockaddr_in *si, *sm;
4732 u_int32_t ssrc, ddst;
4733
4734 sm = NULL;
4735 ssrc = ddst = 0;
4736 /*
4737 * Pick the first AF_INET address from the list,
4738 * aliases don't make any sense on a p2p link anyway.
4739 */
4740 si = 0;
4741 IFADDR_FOREACH(ifa, ifp) {
4742 if (ifa->ifa_addr->sa_family == AF_INET) {
4743 si = (struct sockaddr_in *)ifa->ifa_addr;
4744 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4745 if (si)
4746 break;
4747 }
4748 }
4749 if (ifa) {
4750 if (si && si->sin_addr.s_addr) {
4751 ssrc = si->sin_addr.s_addr;
4752 if (srcmask)
4753 *srcmask = ntohl(sm->sin_addr.s_addr);
4754 }
4755
4756 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4757 if (si && si->sin_addr.s_addr)
4758 ddst = si->sin_addr.s_addr;
4759 }
4760
4761 if (dst) *dst = ntohl(ddst);
4762 if (src) *src = ntohl(ssrc);
4763 }
4764
4765 /*
4766 * Set IP addresses. Must be called at splnet.
4767 * If an address is 0, leave it the way it is.
4768 */
4769 static void
4770 sppp_set_ip_addrs(struct sppp *sp, u_int32_t myaddr, u_int32_t hisaddr)
4771 {
4772 STDDCL;
4773 struct ifaddr *ifa;
4774 struct sockaddr_in *si, *dest;
4775
4776 /*
4777 * Pick the first AF_INET address from the list,
4778 * aliases don't make any sense on a p2p link anyway.
4779 */
4780
4781 IFADDR_FOREACH(ifa, ifp) {
4782 if (ifa->ifa_addr->sa_family == AF_INET) {
4783 si = (struct sockaddr_in *)ifa->ifa_addr;
4784 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4785 goto found;
4786 }
4787 }
4788 return;
4789
4790 found:
4791 {
4792 int error;
4793 struct sockaddr_in new_sin = *si;
4794 struct sockaddr_in new_dst = *dest;
4795
4796 /*
4797 * Scrub old routes now instead of calling in_ifinit with
4798 * scrub=1, because we may change the dstaddr
4799 * before the call to in_ifinit.
4800 */
4801 in_ifscrub(ifp, ifatoia(ifa));
4802
4803 if (myaddr != 0)
4804 new_sin.sin_addr.s_addr = htonl(myaddr);
4805 if (hisaddr != 0) {
4806 new_dst.sin_addr.s_addr = htonl(hisaddr);
4807 if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr) {
4808 sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
4809 *dest = new_dst; /* fix dstaddr in place */
4810 }
4811 }
4812 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4813 if (debug && error)
4814 {
4815 log(LOG_DEBUG, "%s: sppp_set_ip_addrs: in_ifinit "
4816 " failed, error=%d\n", ifp->if_xname, error);
4817 }
4818 #ifdef PFIL_HOOKS
4819 if (!error)
4820 (void)pfil_run_hooks(&if_pfil,
4821 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
4822 #endif
4823 }
4824 }
4825
4826 /*
4827 * Clear IP addresses. Must be called at splnet.
4828 */
4829 static void
4830 sppp_clear_ip_addrs(struct sppp *sp)
4831 {
4832 struct ifnet *ifp = &sp->pp_if;
4833 struct ifaddr *ifa;
4834 struct sockaddr_in *si, *dest;
4835
4836 u_int32_t remote;
4837 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4838 remote = sp->ipcp.saved_hisaddr;
4839 else
4840 sppp_get_ip_addrs(sp, 0, &remote, 0);
4841
4842 /*
4843 * Pick the first AF_INET address from the list,
4844 * aliases don't make any sense on a p2p link anyway.
4845 */
4846
4847 IFADDR_FOREACH(ifa, ifp) {
4848 if (ifa->ifa_addr->sa_family == AF_INET) {
4849 si = (struct sockaddr_in *)ifa->ifa_addr;
4850 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4851 goto found;
4852 }
4853 }
4854 return;
4855
4856 found:
4857 {
4858 struct sockaddr_in new_sin = *si;
4859
4860 in_ifscrub(ifp, ifatoia(ifa));
4861 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
4862 new_sin.sin_addr.s_addr = 0;
4863 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4864 /* replace peer addr in place */
4865 dest->sin_addr.s_addr = sp->ipcp.saved_hisaddr;
4866 in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4867 #ifdef PFIL_HOOKS
4868 (void)pfil_run_hooks(&if_pfil,
4869 (struct mbuf **)SIOCDIFADDR, ifp, PFIL_IFADDR);
4870 #endif
4871 }
4872 }
4873 #endif
4874
4875 #ifdef INET6
4876 /*
4877 * Get both IPv6 addresses.
4878 */
4879 static void
4880 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4881 struct in6_addr *srcmask)
4882 {
4883 struct ifnet *ifp = &sp->pp_if;
4884 struct ifaddr *ifa;
4885 struct sockaddr_in6 *si, *sm;
4886 struct in6_addr ssrc, ddst;
4887
4888 sm = NULL;
4889 memset(&ssrc, 0, sizeof(ssrc));
4890 memset(&ddst, 0, sizeof(ddst));
4891 /*
4892 * Pick the first link-local AF_INET6 address from the list,
4893 * aliases don't make any sense on a p2p link anyway.
4894 */
4895 si = 0;
4896 IFADDR_FOREACH(ifa, ifp)
4897 if (ifa->ifa_addr->sa_family == AF_INET6) {
4898 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4899 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4900 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4901 break;
4902 }
4903 if (ifa) {
4904 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4905 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4906 if (srcmask) {
4907 bcopy(&sm->sin6_addr, srcmask,
4908 sizeof(*srcmask));
4909 }
4910 }
4911
4912 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4913 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4914 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4915 }
4916
4917 if (dst)
4918 bcopy(&ddst, dst, sizeof(*dst));
4919 if (src)
4920 bcopy(&ssrc, src, sizeof(*src));
4921 }
4922
4923 #ifdef IPV6CP_MYIFID_DYN
4924 /*
4925 * Generate random ifid.
4926 */
4927 static void
4928 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4929 {
4930 /* TBD */
4931 }
4932
4933 /*
4934 * Set my IPv6 address. Must be called at splnet.
4935 */
4936 static void
4937 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4938 {
4939 STDDCL;
4940 struct ifaddr *ifa;
4941 struct sockaddr_in6 *sin6;
4942
4943 /*
4944 * Pick the first link-local AF_INET6 address from the list,
4945 * aliases don't make any sense on a p2p link anyway.
4946 */
4947
4948 sin6 = NULL;
4949 IFADDR_FOREACH(ifa, ifp)
4950 {
4951 if (ifa->ifa_addr->sa_family == AF_INET6)
4952 {
4953 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4954 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4955 break;
4956 }
4957 }
4958
4959 if (ifa && sin6)
4960 {
4961 int error;
4962 struct sockaddr_in6 new_sin6 = *sin6;
4963
4964 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4965 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4966 if (debug && error)
4967 {
4968 log(LOG_DEBUG, "%s: sppp_set_ip6_addr: in6_ifinit "
4969 " failed, error=%d\n", ifp->if_xname, error);
4970 }
4971 #ifdef PFIL_HOOKS
4972 if (!error)
4973 (void)pfil_run_hooks(&if_pfil,
4974 (struct mbuf **)SIOCAIFADDR_IN6, ifp, PFIL_IFADDR);
4975 #endif
4976 }
4977 }
4978 #endif
4979
4980 /*
4981 * Suggest a candidate address to be used by peer.
4982 */
4983 static void
4984 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4985 {
4986 struct in6_addr myaddr;
4987 struct timeval tv;
4988
4989 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4990
4991 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4992 microtime(&tv);
4993 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4994 myaddr.s6_addr[14] ^= 0xff;
4995 myaddr.s6_addr[15] ^= 0xff;
4996 } else {
4997 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4998 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
4999 }
5000 if (suggest)
5001 bcopy(&myaddr, suggest, sizeof(myaddr));
5002 }
5003 #endif /*INET6*/
5004
5005 /*
5006 * Process ioctl requests specific to the PPP interface.
5007 * Permissions have already been checked.
5008 */
5009 static int
5010 sppp_params(struct sppp *sp, int cmd, void *data)
5011 {
5012 switch (cmd) {
5013 case SPPPGETAUTHCFG:
5014 {
5015 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5016 int error;
5017 size_t len;
5018
5019 cfg->myauthflags = sp->myauth.flags;
5020 cfg->hisauthflags = sp->hisauth.flags;
5021 strncpy(cfg->ifname, sp->pp_if.if_xname, IFNAMSIZ);
5022 cfg->hisauth = 0;
5023 if (sp->hisauth.proto)
5024 cfg->hisauth = (sp->hisauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5025 cfg->myauth = 0;
5026 if (sp->myauth.proto)
5027 cfg->myauth = (sp->myauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5028 if (cfg->myname_length == 0) {
5029 if (sp->myauth.name != NULL)
5030 cfg->myname_length = sp->myauth.name_len + 1;
5031 } else {
5032 if (sp->myauth.name == NULL) {
5033 cfg->myname_length = 0;
5034 } else {
5035 len = sp->myauth.name_len + 1;
5036 if (cfg->myname_length < len)
5037 return (ENAMETOOLONG);
5038 error = copyout(sp->myauth.name, cfg->myname, len);
5039 if (error) return error;
5040 }
5041 }
5042 if (cfg->hisname_length == 0) {
5043 if (sp->hisauth.name != NULL)
5044 cfg->hisname_length = sp->hisauth.name_len + 1;
5045 } else {
5046 if (sp->hisauth.name == NULL) {
5047 cfg->hisname_length = 0;
5048 } else {
5049 len = sp->hisauth.name_len + 1;
5050 if (cfg->hisname_length < len)
5051 return (ENAMETOOLONG);
5052 error = copyout(sp->hisauth.name, cfg->hisname, len);
5053 if (error) return error;
5054 }
5055 }
5056 }
5057 break;
5058 case SPPPSETAUTHCFG:
5059 {
5060 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5061 int error;
5062
5063 if (sp->myauth.name) {
5064 free(sp->myauth.name, M_DEVBUF);
5065 sp->myauth.name = NULL;
5066 }
5067 if (sp->myauth.secret) {
5068 free(sp->myauth.secret, M_DEVBUF);
5069 sp->myauth.secret = NULL;
5070 }
5071 if (sp->hisauth.name) {
5072 free(sp->hisauth.name, M_DEVBUF);
5073 sp->hisauth.name = NULL;
5074 }
5075 if (sp->hisauth.secret) {
5076 free(sp->hisauth.secret, M_DEVBUF);
5077 sp->hisauth.secret = NULL;
5078 }
5079
5080 if (cfg->hisname != NULL && cfg->hisname_length > 0) {
5081 if (cfg->hisname_length >= MCLBYTES)
5082 return (ENAMETOOLONG);
5083 sp->hisauth.name = malloc(cfg->hisname_length, M_DEVBUF, M_WAITOK);
5084 error = copyin(cfg->hisname, sp->hisauth.name, cfg->hisname_length);
5085 if (error) {
5086 free(sp->hisauth.name, M_DEVBUF);
5087 sp->hisauth.name = NULL;
5088 return error;
5089 }
5090 sp->hisauth.name_len = cfg->hisname_length - 1;
5091 sp->hisauth.name[sp->hisauth.name_len] = 0;
5092 }
5093 if (cfg->hissecret != NULL && cfg->hissecret_length > 0) {
5094 if (cfg->hissecret_length >= MCLBYTES)
5095 return (ENAMETOOLONG);
5096 sp->hisauth.secret = malloc(cfg->hissecret_length, M_DEVBUF, M_WAITOK);
5097 error = copyin(cfg->hissecret, sp->hisauth.secret, cfg->hissecret_length);
5098 if (error) {
5099 free(sp->hisauth.secret, M_DEVBUF);
5100 sp->hisauth.secret = NULL;
5101 return error;
5102 }
5103 sp->hisauth.secret_len = cfg->hissecret_length - 1;
5104 sp->hisauth.secret[sp->hisauth.secret_len] = 0;
5105 }
5106 if (cfg->myname != NULL && cfg->myname_length > 0) {
5107 if (cfg->myname_length >= MCLBYTES)
5108 return (ENAMETOOLONG);
5109 sp->myauth.name = malloc(cfg->myname_length, M_DEVBUF, M_WAITOK);
5110 error = copyin(cfg->myname, sp->myauth.name, cfg->myname_length);
5111 if (error) {
5112 free(sp->myauth.name, M_DEVBUF);
5113 sp->myauth.name = NULL;
5114 return error;
5115 }
5116 sp->myauth.name_len = cfg->myname_length - 1;
5117 sp->myauth.name[sp->myauth.name_len] = 0;
5118 }
5119 if (cfg->mysecret != NULL && cfg->mysecret_length > 0) {
5120 if (cfg->mysecret_length >= MCLBYTES)
5121 return (ENAMETOOLONG);
5122 sp->myauth.secret = malloc(cfg->mysecret_length, M_DEVBUF, M_WAITOK);
5123 error = copyin(cfg->mysecret, sp->myauth.secret, cfg->mysecret_length);
5124 if (error) {
5125 free(sp->myauth.secret, M_DEVBUF);
5126 sp->myauth.secret = NULL;
5127 return error;
5128 }
5129 sp->myauth.secret_len = cfg->mysecret_length - 1;
5130 sp->myauth.secret[sp->myauth.secret_len] = 0;
5131 }
5132 sp->myauth.flags = cfg->myauthflags;
5133 if (cfg->myauth)
5134 sp->myauth.proto = (cfg->myauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5135 sp->hisauth.flags = cfg->hisauthflags;
5136 if (cfg->hisauth)
5137 sp->hisauth.proto = (cfg->hisauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5138 sp->pp_auth_failures = 0;
5139 if (sp->hisauth.proto != 0)
5140 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
5141 else
5142 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
5143 }
5144 break;
5145 case SPPPGETLCPCFG:
5146 {
5147 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5148 lcpp->lcp_timeout = sp->lcp.timeout;
5149 }
5150 break;
5151 case SPPPSETLCPCFG:
5152 {
5153 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5154 sp->lcp.timeout = lcpp->lcp_timeout;
5155 }
5156 break;
5157 case SPPPGETSTATUS:
5158 {
5159 struct spppstatus *status = (struct spppstatus *)data;
5160 status->phase = sp->pp_phase;
5161 }
5162 break;
5163 case SPPPGETSTATUSNCP:
5164 {
5165 struct spppstatusncp *status = (struct spppstatusncp *)data;
5166 status->phase = sp->pp_phase;
5167 status->ncpup = sppp_ncp_check(sp);
5168 }
5169 break;
5170 case SPPPGETIDLETO:
5171 {
5172 struct spppidletimeout *to = (struct spppidletimeout *)data;
5173 to->idle_seconds = sp->pp_idle_timeout;
5174 }
5175 break;
5176 case SPPPSETIDLETO:
5177 {
5178 struct spppidletimeout *to = (struct spppidletimeout *)data;
5179 sp->pp_idle_timeout = to->idle_seconds;
5180 }
5181 break;
5182 case SPPPSETAUTHFAILURE:
5183 {
5184 struct spppauthfailuresettings *afsettings = (struct spppauthfailuresettings *)data;
5185 sp->pp_max_auth_fail = afsettings->max_failures;
5186 sp->pp_auth_failures = 0;
5187 }
5188 break;
5189 case SPPPGETAUTHFAILURES:
5190 {
5191 struct spppauthfailurestats *stats = (struct spppauthfailurestats *)data;
5192 stats->auth_failures = sp->pp_auth_failures;
5193 stats->max_failures = sp->pp_max_auth_fail;
5194 }
5195 break;
5196 case SPPPSETDNSOPTS:
5197 {
5198 struct spppdnssettings *req = (struct spppdnssettings *)data;
5199 sp->query_dns = req->query_dns & 3;
5200 }
5201 break;
5202 case SPPPGETDNSOPTS:
5203 {
5204 struct spppdnssettings *req = (struct spppdnssettings *)data;
5205 req->query_dns = sp->query_dns;
5206 }
5207 break;
5208 case SPPPGETDNSADDRS:
5209 {
5210 struct spppdnsaddrs *addrs = (struct spppdnsaddrs *)data;
5211 memcpy(&addrs->dns, &sp->dns_addrs, sizeof addrs->dns);
5212 }
5213 break;
5214 case SPPPGETKEEPALIVE:
5215 {
5216 struct spppkeepalivesettings *settings =
5217 (struct spppkeepalivesettings*)data;
5218 settings->maxalive = sp->pp_maxalive;
5219 settings->max_noreceive = sp->pp_max_noreceive;
5220 }
5221 break;
5222 case SPPPSETKEEPALIVE:
5223 {
5224 struct spppkeepalivesettings *settings =
5225 (struct spppkeepalivesettings*)data;
5226 sp->pp_maxalive = settings->maxalive;
5227 sp->pp_max_noreceive = settings->max_noreceive;
5228 }
5229 break;
5230 default:
5231 return (EINVAL);
5232 }
5233
5234 return (0);
5235 }
5236
5237 static void
5238 sppp_phase_network(struct sppp *sp)
5239 {
5240 STDDCL;
5241 int i;
5242 u_int32_t mask;
5243
5244 sp->pp_phase = SPPP_PHASE_NETWORK;
5245
5246 if (debug)
5247 {
5248 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
5249 sppp_phase_name(sp->pp_phase));
5250 }
5251
5252 /* Notify NCPs now. */
5253 for (i = 0; i < IDX_COUNT; i++)
5254 if ((cps[i])->flags & CP_NCP)
5255 (cps[i])->Open(sp);
5256
5257 /* Send Up events to all NCPs. */
5258 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5259 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5260 (cps[i])->Up(sp);
5261
5262 /* if no NCP is starting, all this was in vain, close down */
5263 sppp_lcp_check_and_close(sp);
5264 }
5265
5266
5267 static const char *
5268 sppp_cp_type_name(u_char type)
5269 {
5270 static char buf[12];
5271 switch (type) {
5272 case CONF_REQ: return "conf-req";
5273 case CONF_ACK: return "conf-ack";
5274 case CONF_NAK: return "conf-nak";
5275 case CONF_REJ: return "conf-rej";
5276 case TERM_REQ: return "term-req";
5277 case TERM_ACK: return "term-ack";
5278 case CODE_REJ: return "code-rej";
5279 case PROTO_REJ: return "proto-rej";
5280 case ECHO_REQ: return "echo-req";
5281 case ECHO_REPLY: return "echo-reply";
5282 case DISC_REQ: return "discard-req";
5283 }
5284 snprintf(buf, sizeof(buf), "0x%x", type);
5285 return buf;
5286 }
5287
5288 static const char *
5289 sppp_auth_type_name(u_short proto, u_char type)
5290 {
5291 static char buf[12];
5292 switch (proto) {
5293 case PPP_CHAP:
5294 switch (type) {
5295 case CHAP_CHALLENGE: return "challenge";
5296 case CHAP_RESPONSE: return "response";
5297 case CHAP_SUCCESS: return "success";
5298 case CHAP_FAILURE: return "failure";
5299 }
5300 case PPP_PAP:
5301 switch (type) {
5302 case PAP_REQ: return "req";
5303 case PAP_ACK: return "ack";
5304 case PAP_NAK: return "nak";
5305 }
5306 }
5307 snprintf(buf, sizeof(buf), "0x%x", type);
5308 return buf;
5309 }
5310
5311 static const char *
5312 sppp_lcp_opt_name(u_char opt)
5313 {
5314 static char buf[12];
5315 switch (opt) {
5316 case LCP_OPT_MRU: return "mru";
5317 case LCP_OPT_ASYNC_MAP: return "async-map";
5318 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5319 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5320 case LCP_OPT_MAGIC: return "magic";
5321 case LCP_OPT_PROTO_COMP: return "proto-comp";
5322 case LCP_OPT_ADDR_COMP: return "addr-comp";
5323 }
5324 snprintf(buf, sizeof(buf), "0x%x", opt);
5325 return buf;
5326 }
5327
5328 static const char *
5329 sppp_ipcp_opt_name(u_char opt)
5330 {
5331 static char buf[12];
5332 switch (opt) {
5333 case IPCP_OPT_ADDRESSES: return "addresses";
5334 case IPCP_OPT_COMPRESSION: return "compression";
5335 case IPCP_OPT_ADDRESS: return "address";
5336 }
5337 snprintf(buf, sizeof(buf), "0x%x", opt);
5338 return buf;
5339 }
5340
5341 #ifdef INET6
5342 static const char *
5343 sppp_ipv6cp_opt_name(u_char opt)
5344 {
5345 static char buf[12];
5346 switch (opt) {
5347 case IPV6CP_OPT_IFID: return "ifid";
5348 case IPV6CP_OPT_COMPRESSION: return "compression";
5349 }
5350 snprintf(buf, sizeof(buf), "0x%x", opt);
5351 return buf;
5352 }
5353 #endif
5354
5355 static const char *
5356 sppp_state_name(int state)
5357 {
5358 switch (state) {
5359 case STATE_INITIAL: return "initial";
5360 case STATE_STARTING: return "starting";
5361 case STATE_CLOSED: return "closed";
5362 case STATE_STOPPED: return "stopped";
5363 case STATE_CLOSING: return "closing";
5364 case STATE_STOPPING: return "stopping";
5365 case STATE_REQ_SENT: return "req-sent";
5366 case STATE_ACK_RCVD: return "ack-rcvd";
5367 case STATE_ACK_SENT: return "ack-sent";
5368 case STATE_OPENED: return "opened";
5369 }
5370 return "illegal";
5371 }
5372
5373 static const char *
5374 sppp_phase_name(int phase)
5375 {
5376 switch (phase) {
5377 case SPPP_PHASE_DEAD: return "dead";
5378 case SPPP_PHASE_ESTABLISH: return "establish";
5379 case SPPP_PHASE_TERMINATE: return "terminate";
5380 case SPPP_PHASE_AUTHENTICATE: return "authenticate";
5381 case SPPP_PHASE_NETWORK: return "network";
5382 }
5383 return "illegal";
5384 }
5385
5386 static const char *
5387 sppp_proto_name(u_short proto)
5388 {
5389 static char buf[12];
5390 switch (proto) {
5391 case PPP_LCP: return "lcp";
5392 case PPP_IPCP: return "ipcp";
5393 case PPP_PAP: return "pap";
5394 case PPP_CHAP: return "chap";
5395 case PPP_IPV6CP: return "ipv6cp";
5396 }
5397 snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
5398 return buf;
5399 }
5400
5401 static void
5402 sppp_print_bytes(const u_char *p, u_short len)
5403 {
5404 addlog(" %02x", *p++);
5405 while (--len > 0)
5406 addlog("-%02x", *p++);
5407 }
5408
5409 static void
5410 sppp_print_string(const char *p, u_short len)
5411 {
5412 u_char c;
5413
5414 while (len-- > 0) {
5415 c = *p++;
5416 /*
5417 * Print only ASCII chars directly. RFC 1994 recommends
5418 * using only them, but we don't rely on it. */
5419 if (c < ' ' || c > '~')
5420 addlog("\\x%x", c);
5421 else
5422 addlog("%c", c);
5423 }
5424 }
5425
5426 static const char *
5427 sppp_dotted_quad(u_int32_t addr)
5428 {
5429 static char s[16];
5430 snprintf(s, sizeof(s), "%d.%d.%d.%d",
5431 (int)((addr >> 24) & 0xff),
5432 (int)((addr >> 16) & 0xff),
5433 (int)((addr >> 8) & 0xff),
5434 (int)(addr & 0xff));
5435 return s;
5436 }
5437
5438 /* a dummy, used to drop uninteresting events */
5439 static void
5440 sppp_null(struct sppp *unused)
5441 {
5442 /* do just nothing */
5443 }
5444 /*
5445 * This file is large. Tell emacs to highlight it nevertheless.
5446 *
5447 * Local Variables:
5448 * hilit-auto-highlight-maxout: 120000
5449 * End:
5450 */
5451