if_spppsubr.c revision 1.95 1 /* $NetBSD: if_spppsubr.c,v 1.95 2006/07/23 22:06:12 ad 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.95 2006/07/23 22:06:12 ad 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 lwp *l = curlwp; /* XXX */
1147
1148 if ((error = kauth_authorize_generic(l->l_cred,
1149 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
1150 break;
1151 }
1152 /* FALLTHROUGH */
1153 case SPPPGETSTATUS:
1154 case SPPPGETSTATUSNCP:
1155 case SPPPGETIDLETO:
1156 case SPPPGETDNSOPTS:
1157 case SPPPGETDNSADDRS:
1158 case SPPPGETKEEPALIVE:
1159 error = sppp_params(sp, cmd, data);
1160 break;
1161
1162 default:
1163 error = ENOTTY;
1164 }
1165 splx(s);
1166 return (error);
1167 }
1168
1169
1170 /*
1171 * Cisco framing implementation.
1172 */
1173
1174 /*
1175 * Handle incoming Cisco keepalive protocol packets.
1176 */
1177 static void
1178 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1179 {
1180 STDDCL;
1181 struct cisco_packet *h;
1182 #ifdef INET
1183 u_int32_t me, mymask = 0; /* XXX: GCC */
1184 #endif
1185
1186 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1187 if (debug)
1188 log(LOG_DEBUG,
1189 "%s: cisco invalid packet length: %d bytes\n",
1190 ifp->if_xname, m->m_pkthdr.len);
1191 return;
1192 }
1193 h = mtod(m, struct cisco_packet *);
1194 if (debug)
1195 log(LOG_DEBUG,
1196 "%s: cisco input: %d bytes "
1197 "<0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1198 ifp->if_xname, m->m_pkthdr.len,
1199 ntohl(h->type), h->par1, h->par2, (u_int)h->rel,
1200 (u_int)h->time0, (u_int)h->time1);
1201 switch (ntohl(h->type)) {
1202 default:
1203 if (debug)
1204 addlog("%s: cisco unknown packet type: 0x%x\n",
1205 ifp->if_xname, ntohl(h->type));
1206 break;
1207 case CISCO_ADDR_REPLY:
1208 /* Reply on address request, ignore */
1209 break;
1210 case CISCO_KEEPALIVE_REQ:
1211 sp->pp_alivecnt = 0;
1212 sp->pp_rseq[IDX_LCP] = ntohl(h->par1);
1213 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1214 /* Local and remote sequence numbers are equal.
1215 * Probably, the line is in loopback mode. */
1216 if (sp->pp_loopcnt >= LOOPALIVECNT) {
1217 printf ("%s: loopback\n",
1218 ifp->if_xname);
1219 sp->pp_loopcnt = 0;
1220 if (ifp->if_flags & IFF_UP) {
1221 if_down(ifp);
1222 IF_PURGE(&sp->pp_cpq);
1223 }
1224 }
1225 ++sp->pp_loopcnt;
1226
1227 /* Generate new local sequence number */
1228 sp->pp_seq[IDX_LCP] = arc4random();
1229 break;
1230 }
1231 sp->pp_loopcnt = 0;
1232 if (! (ifp->if_flags & IFF_UP) &&
1233 (ifp->if_flags & IFF_RUNNING)) {
1234 if_up(ifp);
1235 }
1236 break;
1237 case CISCO_ADDR_REQ:
1238 #ifdef INET
1239 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1240 if (me != 0L)
1241 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1242 #endif
1243 break;
1244 }
1245 }
1246
1247 /*
1248 * Send Cisco keepalive packet.
1249 */
1250 static void
1251 sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2)
1252 {
1253 STDDCL;
1254 struct ppp_header *h;
1255 struct cisco_packet *ch;
1256 struct mbuf *m;
1257 u_int32_t t;
1258
1259 t = time_uptime * 1000;
1260 MGETHDR(m, M_DONTWAIT, MT_DATA);
1261 if (! m)
1262 return;
1263 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1264 m->m_pkthdr.rcvif = 0;
1265
1266 h = mtod(m, struct ppp_header *);
1267 h->address = CISCO_MULTICAST;
1268 h->control = 0;
1269 h->protocol = htons(CISCO_KEEPALIVE);
1270
1271 ch = (struct cisco_packet *)(h + 1);
1272 ch->type = htonl(type);
1273 ch->par1 = htonl(par1);
1274 ch->par2 = htonl(par2);
1275 ch->rel = -1;
1276
1277 ch->time0 = htons((u_short)(t >> 16));
1278 ch->time1 = htons((u_short) t);
1279
1280 if (debug)
1281 log(LOG_DEBUG,
1282 "%s: cisco output: <0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1283 ifp->if_xname, ntohl(ch->type), ch->par1,
1284 ch->par2, (u_int)ch->rel, (u_int)ch->time0,
1285 (u_int)ch->time1);
1286
1287 if (IF_QFULL(&sp->pp_cpq)) {
1288 IF_DROP(&sp->pp_fastq);
1289 IF_DROP(&ifp->if_snd);
1290 m_freem(m);
1291 ++ifp->if_oerrors;
1292 return;
1293 } else
1294 IF_ENQUEUE(&sp->pp_cpq, m);
1295 if (! (ifp->if_flags & IFF_OACTIVE))
1296 (*ifp->if_start)(ifp);
1297 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1298 }
1299
1300 /*
1301 * PPP protocol implementation.
1302 */
1303
1304 /*
1305 * Send PPP control protocol packet.
1306 */
1307 static void
1308 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1309 u_char ident, u_short len, void *data)
1310 {
1311 STDDCL;
1312 struct lcp_header *lh;
1313 struct mbuf *m;
1314 size_t pkthdrlen;
1315
1316 pkthdrlen = (sp->pp_flags & PP_NOFRAMING) ? 2 : PPP_HEADER_LEN;
1317
1318 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN)
1319 len = MHLEN - pkthdrlen - LCP_HEADER_LEN;
1320 MGETHDR(m, M_DONTWAIT, MT_DATA);
1321 if (! m)
1322 return;
1323 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
1324 m->m_pkthdr.rcvif = 0;
1325
1326 if (sp->pp_flags & PP_NOFRAMING) {
1327 *mtod(m, u_int16_t *) = htons(proto);
1328 lh = (struct lcp_header *)(mtod(m, u_int8_t *) + 2);
1329 } else {
1330 struct ppp_header *h;
1331 h = mtod(m, struct ppp_header *);
1332 h->address = PPP_ALLSTATIONS; /* broadcast address */
1333 h->control = PPP_UI; /* Unnumbered Info */
1334 h->protocol = htons(proto); /* Link Control Protocol */
1335 lh = (struct lcp_header *)(h + 1);
1336 }
1337 lh->type = type;
1338 lh->ident = ident;
1339 lh->len = htons(LCP_HEADER_LEN + len);
1340 if (len)
1341 bcopy (data, lh + 1, len);
1342
1343 if (debug) {
1344 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
1345 ifp->if_xname,
1346 sppp_proto_name(proto),
1347 sppp_cp_type_name(lh->type), lh->ident, ntohs(lh->len));
1348 if (len)
1349 sppp_print_bytes((u_char *)(lh + 1), len);
1350 addlog(">\n");
1351 }
1352 if (IF_QFULL(&sp->pp_cpq)) {
1353 IF_DROP(&sp->pp_fastq);
1354 IF_DROP(&ifp->if_snd);
1355 m_freem(m);
1356 ++ifp->if_oerrors;
1357 return;
1358 } else
1359 IF_ENQUEUE(&sp->pp_cpq, m);
1360 if (! (ifp->if_flags & IFF_OACTIVE))
1361 (*ifp->if_start)(ifp);
1362 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1363 }
1364
1365 /*
1366 * Handle incoming PPP control protocol packets.
1367 */
1368 static void
1369 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1370 {
1371 STDDCL;
1372 struct lcp_header *h;
1373 int len = m->m_pkthdr.len;
1374 int rv;
1375 u_char *p;
1376 u_int32_t u32;
1377
1378 if (len < 4) {
1379 if (debug)
1380 log(LOG_DEBUG,
1381 "%s: %s invalid packet length: %d bytes\n",
1382 ifp->if_xname, cp->name, len);
1383 return;
1384 }
1385 h = mtod(m, struct lcp_header *);
1386 if (debug) {
1387 log(LOG_DEBUG,
1388 "%s: %s input(%s): <%s id=0x%x len=%d",
1389 ifp->if_xname, cp->name,
1390 sppp_state_name(sp->state[cp->protoidx]),
1391 sppp_cp_type_name(h->type), h->ident, ntohs(h->len));
1392 if (len > 4)
1393 sppp_print_bytes((u_char *)(h + 1), len - 4);
1394 addlog(">\n");
1395 }
1396 if (len > ntohs(h->len))
1397 len = ntohs(h->len);
1398 p = (u_char *)(h + 1);
1399 switch (h->type) {
1400 case CONF_REQ:
1401 if (len < 4) {
1402 if (debug)
1403 addlog("%s: %s invalid conf-req length %d\n",
1404 ifp->if_xname, cp->name,
1405 len);
1406 ++ifp->if_ierrors;
1407 break;
1408 }
1409 /* handle states where RCR doesn't get a SCA/SCN */
1410 switch (sp->state[cp->protoidx]) {
1411 case STATE_CLOSING:
1412 case STATE_STOPPING:
1413 return;
1414 case STATE_CLOSED:
1415 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1416 0, 0);
1417 return;
1418 }
1419 rv = (cp->RCR)(sp, h, len);
1420 switch (sp->state[cp->protoidx]) {
1421 case STATE_OPENED:
1422 (cp->tld)(sp);
1423 (cp->scr)(sp);
1424 /* fall through... */
1425 case STATE_ACK_SENT:
1426 case STATE_REQ_SENT:
1427 sppp_cp_change_state(cp, sp, rv?
1428 STATE_ACK_SENT: STATE_REQ_SENT);
1429 break;
1430 case STATE_STOPPED:
1431 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1432 (cp->scr)(sp);
1433 sppp_cp_change_state(cp, sp, rv?
1434 STATE_ACK_SENT: STATE_REQ_SENT);
1435 break;
1436 case STATE_ACK_RCVD:
1437 if (rv) {
1438 sppp_cp_change_state(cp, sp, STATE_OPENED);
1439 if (debug)
1440 log(LOG_DEBUG, "%s: %s tlu\n",
1441 ifp->if_xname,
1442 cp->name);
1443 (cp->tlu)(sp);
1444 } else
1445 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1446 break;
1447 default:
1448 printf("%s: %s illegal %s in state %s\n",
1449 ifp->if_xname, cp->name,
1450 sppp_cp_type_name(h->type),
1451 sppp_state_name(sp->state[cp->protoidx]));
1452 ++ifp->if_ierrors;
1453 }
1454 break;
1455 case CONF_ACK:
1456 if (h->ident != sp->confid[cp->protoidx]) {
1457 if (debug)
1458 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1459 ifp->if_xname, cp->name,
1460 h->ident, sp->confid[cp->protoidx]);
1461 ++ifp->if_ierrors;
1462 break;
1463 }
1464 switch (sp->state[cp->protoidx]) {
1465 case STATE_CLOSED:
1466 case STATE_STOPPED:
1467 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1468 break;
1469 case STATE_CLOSING:
1470 case STATE_STOPPING:
1471 break;
1472 case STATE_REQ_SENT:
1473 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1474 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1475 break;
1476 case STATE_OPENED:
1477 (cp->tld)(sp);
1478 /* fall through */
1479 case STATE_ACK_RCVD:
1480 (cp->scr)(sp);
1481 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1482 break;
1483 case STATE_ACK_SENT:
1484 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1485 sppp_cp_change_state(cp, sp, STATE_OPENED);
1486 if (debug)
1487 log(LOG_DEBUG, "%s: %s tlu\n",
1488 ifp->if_xname, cp->name);
1489 (cp->tlu)(sp);
1490 break;
1491 default:
1492 printf("%s: %s illegal %s in state %s\n",
1493 ifp->if_xname, cp->name,
1494 sppp_cp_type_name(h->type),
1495 sppp_state_name(sp->state[cp->protoidx]));
1496 ++ifp->if_ierrors;
1497 }
1498 break;
1499 case CONF_NAK:
1500 case CONF_REJ:
1501 if (h->ident != sp->confid[cp->protoidx]) {
1502 if (debug)
1503 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1504 ifp->if_xname, cp->name,
1505 h->ident, sp->confid[cp->protoidx]);
1506 ++ifp->if_ierrors;
1507 break;
1508 }
1509 if (h->type == CONF_NAK)
1510 (cp->RCN_nak)(sp, h, len);
1511 else /* CONF_REJ */
1512 (cp->RCN_rej)(sp, h, len);
1513
1514 switch (sp->state[cp->protoidx]) {
1515 case STATE_CLOSED:
1516 case STATE_STOPPED:
1517 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1518 break;
1519 case STATE_REQ_SENT:
1520 case STATE_ACK_SENT:
1521 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1522 (cp->scr)(sp);
1523 break;
1524 case STATE_OPENED:
1525 (cp->tld)(sp);
1526 /* fall through */
1527 case STATE_ACK_RCVD:
1528 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1529 (cp->scr)(sp);
1530 break;
1531 case STATE_CLOSING:
1532 case STATE_STOPPING:
1533 break;
1534 default:
1535 printf("%s: %s illegal %s in state %s\n",
1536 ifp->if_xname, cp->name,
1537 sppp_cp_type_name(h->type),
1538 sppp_state_name(sp->state[cp->protoidx]));
1539 ++ifp->if_ierrors;
1540 }
1541 break;
1542
1543 case TERM_REQ:
1544 switch (sp->state[cp->protoidx]) {
1545 case STATE_ACK_RCVD:
1546 case STATE_ACK_SENT:
1547 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1548 /* fall through */
1549 case STATE_CLOSED:
1550 case STATE_STOPPED:
1551 case STATE_CLOSING:
1552 case STATE_STOPPING:
1553 case STATE_REQ_SENT:
1554 sta:
1555 /* Send Terminate-Ack packet. */
1556 if (debug)
1557 log(LOG_DEBUG, "%s: %s send terminate-ack\n",
1558 ifp->if_xname, cp->name);
1559 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1560 break;
1561 case STATE_OPENED:
1562 (cp->tld)(sp);
1563 sp->rst_counter[cp->protoidx] = 0;
1564 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1565 goto sta;
1566 default:
1567 printf("%s: %s illegal %s in state %s\n",
1568 ifp->if_xname, cp->name,
1569 sppp_cp_type_name(h->type),
1570 sppp_state_name(sp->state[cp->protoidx]));
1571 ++ifp->if_ierrors;
1572 }
1573 break;
1574 case TERM_ACK:
1575 switch (sp->state[cp->protoidx]) {
1576 case STATE_CLOSED:
1577 case STATE_STOPPED:
1578 case STATE_REQ_SENT:
1579 case STATE_ACK_SENT:
1580 break;
1581 case STATE_CLOSING:
1582 (cp->tlf)(sp);
1583 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1584 sppp_lcp_check_and_close(sp);
1585 break;
1586 case STATE_STOPPING:
1587 (cp->tlf)(sp);
1588 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1589 sppp_lcp_check_and_close(sp);
1590 break;
1591 case STATE_ACK_RCVD:
1592 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1593 break;
1594 case STATE_OPENED:
1595 (cp->tld)(sp);
1596 (cp->scr)(sp);
1597 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1598 break;
1599 default:
1600 printf("%s: %s illegal %s in state %s\n",
1601 ifp->if_xname, cp->name,
1602 sppp_cp_type_name(h->type),
1603 sppp_state_name(sp->state[cp->protoidx]));
1604 ++ifp->if_ierrors;
1605 }
1606 break;
1607 case CODE_REJ:
1608 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1609 log(LOG_INFO,
1610 "%s: %s: ignoring RXJ (%s) for code ?, "
1611 "danger will robinson\n",
1612 ifp->if_xname, cp->name,
1613 sppp_cp_type_name(h->type));
1614 switch (sp->state[cp->protoidx]) {
1615 case STATE_CLOSED:
1616 case STATE_STOPPED:
1617 case STATE_REQ_SENT:
1618 case STATE_ACK_SENT:
1619 case STATE_CLOSING:
1620 case STATE_STOPPING:
1621 case STATE_OPENED:
1622 break;
1623 case STATE_ACK_RCVD:
1624 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1625 break;
1626 default:
1627 printf("%s: %s illegal %s in state %s\n",
1628 ifp->if_xname, cp->name,
1629 sppp_cp_type_name(h->type),
1630 sppp_state_name(sp->state[cp->protoidx]));
1631 ++ifp->if_ierrors;
1632 }
1633 break;
1634 case PROTO_REJ:
1635 {
1636 int catastrophic;
1637 const struct cp *upper;
1638 int i;
1639 u_int16_t proto;
1640
1641 catastrophic = 0;
1642 upper = NULL;
1643 proto = p[0] << 8 | p[1];
1644 for (i = 0; i < IDX_COUNT; i++) {
1645 if (cps[i]->proto == proto) {
1646 upper = cps[i];
1647 break;
1648 }
1649 }
1650 if (upper == NULL)
1651 catastrophic++;
1652
1653 if (debug)
1654 log(LOG_INFO,
1655 "%s: %s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1656 ifp->if_xname, cp->name, catastrophic ? '-' : '+',
1657 sppp_cp_type_name(h->type), proto,
1658 upper ? upper->name : "unknown",
1659 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1660
1661 /*
1662 * if we got RXJ+ against conf-req, the peer does not implement
1663 * this particular protocol type. terminate the protocol.
1664 */
1665 if (upper && !catastrophic) {
1666 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1667 upper->Close(sp);
1668 break;
1669 }
1670 }
1671
1672 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1673 switch (sp->state[cp->protoidx]) {
1674 case STATE_CLOSED:
1675 case STATE_STOPPED:
1676 case STATE_REQ_SENT:
1677 case STATE_ACK_SENT:
1678 case STATE_CLOSING:
1679 case STATE_STOPPING:
1680 case STATE_OPENED:
1681 break;
1682 case STATE_ACK_RCVD:
1683 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1684 break;
1685 default:
1686 printf("%s: %s illegal %s in state %s\n",
1687 ifp->if_xname, cp->name,
1688 sppp_cp_type_name(h->type),
1689 sppp_state_name(sp->state[cp->protoidx]));
1690 ++ifp->if_ierrors;
1691 }
1692 break;
1693 }
1694 case DISC_REQ:
1695 if (cp->proto != PPP_LCP)
1696 goto illegal;
1697 /* Discard the packet. */
1698 break;
1699 case ECHO_REQ:
1700 if (cp->proto != PPP_LCP)
1701 goto illegal;
1702 if (sp->state[cp->protoidx] != STATE_OPENED) {
1703 if (debug)
1704 addlog("%s: lcp echo req but lcp closed\n",
1705 ifp->if_xname);
1706 ++ifp->if_ierrors;
1707 break;
1708 }
1709 if (len < 8) {
1710 if (debug)
1711 addlog("%s: invalid lcp echo request "
1712 "packet length: %d bytes\n",
1713 ifp->if_xname, len);
1714 break;
1715 }
1716 memcpy(&u32, h + 1, sizeof u32);
1717 if (ntohl(u32) == sp->lcp.magic) {
1718 /* Line loopback mode detected. */
1719 printf("%s: loopback\n", ifp->if_xname);
1720 if_down(ifp);
1721 IF_PURGE(&sp->pp_cpq);
1722
1723 /* Shut down the PPP link. */
1724 /* XXX */
1725 lcp.Down(sp);
1726 lcp.Up(sp);
1727 break;
1728 }
1729 u32 = htonl(sp->lcp.magic);
1730 memcpy(h + 1, &u32, sizeof u32);
1731 if (debug)
1732 addlog("%s: got lcp echo req, sending echo rep\n",
1733 ifp->if_xname);
1734 sppp_cp_send(sp, PPP_LCP, ECHO_REPLY, h->ident, len - 4,
1735 h + 1);
1736 break;
1737 case ECHO_REPLY:
1738 if (cp->proto != PPP_LCP)
1739 goto illegal;
1740 if (h->ident != sp->lcp.echoid) {
1741 ++ifp->if_ierrors;
1742 break;
1743 }
1744 if (len < 8) {
1745 if (debug)
1746 addlog("%s: lcp invalid echo reply "
1747 "packet length: %d bytes\n",
1748 ifp->if_xname, len);
1749 break;
1750 }
1751 if (debug)
1752 addlog("%s: lcp got echo rep\n",
1753 ifp->if_xname);
1754 memcpy(&u32, h + 1, sizeof u32);
1755 if (ntohl(u32) != sp->lcp.magic)
1756 sp->pp_alivecnt = 0;
1757 break;
1758 default:
1759 /* Unknown packet type -- send Code-Reject packet. */
1760 illegal:
1761 if (debug)
1762 addlog("%s: %s send code-rej for 0x%x\n",
1763 ifp->if_xname, cp->name, h->type);
1764 sppp_cp_send(sp, cp->proto, CODE_REJ,
1765 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1766 ++ifp->if_ierrors;
1767 }
1768 }
1769
1770
1771 /*
1772 * The generic part of all Up/Down/Open/Close/TO event handlers.
1773 * Basically, the state transition handling in the automaton.
1774 */
1775 static void
1776 sppp_up_event(const struct cp *cp, struct sppp *sp)
1777 {
1778 STDDCL;
1779
1780 if (debug)
1781 log(LOG_DEBUG, "%s: %s up(%s)\n",
1782 ifp->if_xname, cp->name,
1783 sppp_state_name(sp->state[cp->protoidx]));
1784
1785 switch (sp->state[cp->protoidx]) {
1786 case STATE_INITIAL:
1787 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1788 break;
1789 case STATE_STARTING:
1790 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1791 (cp->scr)(sp);
1792 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1793 break;
1794 default:
1795 printf("%s: %s illegal up in state %s\n",
1796 ifp->if_xname, cp->name,
1797 sppp_state_name(sp->state[cp->protoidx]));
1798 }
1799 }
1800
1801 static void
1802 sppp_down_event(const struct cp *cp, struct sppp *sp)
1803 {
1804 STDDCL;
1805
1806 if (debug)
1807 log(LOG_DEBUG, "%s: %s down(%s)\n",
1808 ifp->if_xname, cp->name,
1809 sppp_state_name(sp->state[cp->protoidx]));
1810
1811 switch (sp->state[cp->protoidx]) {
1812 case STATE_CLOSED:
1813 case STATE_CLOSING:
1814 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1815 break;
1816 case STATE_STOPPED:
1817 (cp->tls)(sp);
1818 /* fall through */
1819 case STATE_STOPPING:
1820 case STATE_REQ_SENT:
1821 case STATE_ACK_RCVD:
1822 case STATE_ACK_SENT:
1823 sppp_cp_change_state(cp, sp, STATE_STARTING);
1824 break;
1825 case STATE_OPENED:
1826 (cp->tld)(sp);
1827 sppp_cp_change_state(cp, sp, STATE_STARTING);
1828 break;
1829 default:
1830 printf("%s: %s illegal down in state %s\n",
1831 ifp->if_xname, cp->name,
1832 sppp_state_name(sp->state[cp->protoidx]));
1833 }
1834 }
1835
1836
1837 static void
1838 sppp_open_event(const struct cp *cp, struct sppp *sp)
1839 {
1840 STDDCL;
1841
1842 if (debug)
1843 log(LOG_DEBUG, "%s: %s open(%s)\n",
1844 ifp->if_xname, cp->name,
1845 sppp_state_name(sp->state[cp->protoidx]));
1846
1847 switch (sp->state[cp->protoidx]) {
1848 case STATE_INITIAL:
1849 sppp_cp_change_state(cp, sp, STATE_STARTING);
1850 (cp->tls)(sp);
1851 break;
1852 case STATE_STARTING:
1853 break;
1854 case STATE_CLOSED:
1855 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1856 (cp->scr)(sp);
1857 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1858 break;
1859 case STATE_STOPPED:
1860 case STATE_STOPPING:
1861 case STATE_REQ_SENT:
1862 case STATE_ACK_RCVD:
1863 case STATE_ACK_SENT:
1864 case STATE_OPENED:
1865 break;
1866 case STATE_CLOSING:
1867 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1868 break;
1869 }
1870 }
1871
1872
1873 static void
1874 sppp_close_event(const struct cp *cp, struct sppp *sp)
1875 {
1876 STDDCL;
1877
1878 if (debug)
1879 log(LOG_DEBUG, "%s: %s close(%s)\n",
1880 ifp->if_xname, cp->name,
1881 sppp_state_name(sp->state[cp->protoidx]));
1882
1883 switch (sp->state[cp->protoidx]) {
1884 case STATE_INITIAL:
1885 case STATE_CLOSED:
1886 case STATE_CLOSING:
1887 break;
1888 case STATE_STARTING:
1889 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1890 (cp->tlf)(sp);
1891 break;
1892 case STATE_STOPPED:
1893 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1894 break;
1895 case STATE_STOPPING:
1896 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1897 break;
1898 case STATE_OPENED:
1899 (cp->tld)(sp);
1900 /* fall through */
1901 case STATE_REQ_SENT:
1902 case STATE_ACK_RCVD:
1903 case STATE_ACK_SENT:
1904 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1905 sppp_cp_send(sp, cp->proto, TERM_REQ,
1906 ++sp->pp_seq[cp->protoidx], 0, 0);
1907 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1908 break;
1909 }
1910 }
1911
1912 static void
1913 sppp_to_event(const struct cp *cp, struct sppp *sp)
1914 {
1915 STDDCL;
1916 int s;
1917
1918 s = splnet();
1919 if (debug)
1920 log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
1921 ifp->if_xname, cp->name,
1922 sppp_state_name(sp->state[cp->protoidx]),
1923 sp->rst_counter[cp->protoidx]);
1924
1925 if (--sp->rst_counter[cp->protoidx] < 0)
1926 /* TO- event */
1927 switch (sp->state[cp->protoidx]) {
1928 case STATE_CLOSING:
1929 (cp->tlf)(sp);
1930 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1931 sppp_lcp_check_and_close(sp);
1932 break;
1933 case STATE_STOPPING:
1934 (cp->tlf)(sp);
1935 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1936 sppp_lcp_check_and_close(sp);
1937 break;
1938 case STATE_REQ_SENT:
1939 case STATE_ACK_RCVD:
1940 case STATE_ACK_SENT:
1941 (cp->tlf)(sp);
1942 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1943 sppp_lcp_check_and_close(sp);
1944 break;
1945 }
1946 else
1947 /* TO+ event */
1948 switch (sp->state[cp->protoidx]) {
1949 case STATE_CLOSING:
1950 case STATE_STOPPING:
1951 sppp_cp_send(sp, cp->proto, TERM_REQ,
1952 ++sp->pp_seq[cp->protoidx], 0, 0);
1953 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1954 cp->TO, sp);
1955 break;
1956 case STATE_REQ_SENT:
1957 case STATE_ACK_RCVD:
1958 (cp->scr)(sp);
1959 /* sppp_cp_change_state() will restart the timer */
1960 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1961 break;
1962 case STATE_ACK_SENT:
1963 (cp->scr)(sp);
1964 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1965 cp->TO, sp);
1966 break;
1967 }
1968
1969 splx(s);
1970 }
1971
1972 /*
1973 * Change the state of a control protocol in the state automaton.
1974 * Takes care of starting/stopping the restart timer.
1975 */
1976 void
1977 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1978 {
1979 sp->state[cp->protoidx] = newstate;
1980 callout_stop(&sp->ch[cp->protoidx]);
1981 switch (newstate) {
1982 case STATE_INITIAL:
1983 case STATE_STARTING:
1984 case STATE_CLOSED:
1985 case STATE_STOPPED:
1986 case STATE_OPENED:
1987 break;
1988 case STATE_CLOSING:
1989 case STATE_STOPPING:
1990 case STATE_REQ_SENT:
1991 case STATE_ACK_RCVD:
1992 case STATE_ACK_SENT:
1993 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
1994 cp->TO, sp);
1995 break;
1996 }
1997 }
1998
1999 /*
2000 *--------------------------------------------------------------------------*
2001 * *
2002 * The LCP implementation. *
2003 * *
2004 *--------------------------------------------------------------------------*
2005 */
2006 static void
2007 sppp_lcp_init(struct sppp *sp)
2008 {
2009 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2010 sp->lcp.magic = 0;
2011 sp->state[IDX_LCP] = STATE_INITIAL;
2012 sp->fail_counter[IDX_LCP] = 0;
2013 sp->pp_seq[IDX_LCP] = 0;
2014 sp->pp_rseq[IDX_LCP] = 0;
2015 sp->lcp.protos = 0;
2016 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2017
2018 /*
2019 * Initialize counters and timeout values. Note that we don't
2020 * use the 3 seconds suggested in RFC 1661 since we are likely
2021 * running on a fast link. XXX We should probably implement
2022 * the exponential backoff option. Note that these values are
2023 * relevant for all control protocols, not just LCP only.
2024 */
2025 sp->lcp.timeout = 1 * hz;
2026 sp->lcp.max_terminate = 2;
2027 sp->lcp.max_configure = 10;
2028 sp->lcp.max_failure = 10;
2029 callout_init(&sp->ch[IDX_LCP]);
2030 }
2031
2032 static void
2033 sppp_lcp_up(struct sppp *sp)
2034 {
2035 STDDCL;
2036
2037 /* Initialize activity timestamp: opening a connection is an activity */
2038 sp->pp_last_receive = sp->pp_last_activity = time_uptime;
2039
2040 /*
2041 * If this interface is passive or dial-on-demand, and we are
2042 * still in Initial state, it means we've got an incoming
2043 * call. Activate the interface.
2044 */
2045 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2046 if (debug)
2047 log(LOG_DEBUG,
2048 "%s: Up event", ifp->if_xname);
2049 ifp->if_flags |= IFF_RUNNING;
2050 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2051 if (debug)
2052 addlog("(incoming call)\n");
2053 sp->pp_flags |= PP_CALLIN;
2054 lcp.Open(sp);
2055 } else if (debug)
2056 addlog("\n");
2057 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2058 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2059 ifp->if_flags |= IFF_RUNNING;
2060 lcp.Open(sp);
2061 }
2062
2063 sppp_up_event(&lcp, sp);
2064 }
2065
2066 static void
2067 sppp_lcp_down(struct sppp *sp)
2068 {
2069 STDDCL;
2070
2071 sppp_down_event(&lcp, sp);
2072
2073 /*
2074 * If this is neither a dial-on-demand nor a passive
2075 * interface, simulate an ``ifconfig down'' action, so the
2076 * administrator can force a redial by another ``ifconfig
2077 * up''. XXX For leased line operation, should we immediately
2078 * try to reopen the connection here?
2079 */
2080 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2081 if (debug)
2082 log(LOG_INFO,
2083 "%s: Down event (carrier loss), taking interface down.\n",
2084 ifp->if_xname);
2085 if_down(ifp);
2086 } else {
2087 if (debug)
2088 log(LOG_DEBUG,
2089 "%s: Down event (carrier loss)\n",
2090 ifp->if_xname);
2091 }
2092 sp->pp_flags &= ~PP_CALLIN;
2093 if (sp->state[IDX_LCP] != STATE_INITIAL)
2094 lcp.Close(sp);
2095 ifp->if_flags &= ~IFF_RUNNING;
2096 }
2097
2098 static void
2099 sppp_lcp_open(struct sppp *sp)
2100 {
2101 /*
2102 * If we are authenticator, negotiate LCP_AUTH
2103 */
2104 if (sp->hisauth.proto != 0)
2105 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2106 else
2107 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2108 sp->pp_flags &= ~PP_NEEDAUTH;
2109 sppp_open_event(&lcp, sp);
2110 }
2111
2112 static void
2113 sppp_lcp_close(struct sppp *sp)
2114 {
2115 sppp_close_event(&lcp, sp);
2116 }
2117
2118 static void
2119 sppp_lcp_TO(void *cookie)
2120 {
2121 sppp_to_event(&lcp, (struct sppp *)cookie);
2122 }
2123
2124 /*
2125 * Analyze a configure request. Return true if it was agreeable, and
2126 * caused action sca, false if it has been rejected or nak'ed, and
2127 * caused action scn. (The return value is used to make the state
2128 * transition decision in the state automaton.)
2129 */
2130 static int
2131 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2132 {
2133 STDDCL;
2134 u_char *buf, *r, *p;
2135 int origlen, rlen;
2136 u_int32_t nmagic;
2137 u_short authproto;
2138
2139 len -= 4;
2140 origlen = len;
2141 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2142 if (! buf)
2143 return (0);
2144
2145 if (debug)
2146 log(LOG_DEBUG, "%s: lcp parse opts:",
2147 ifp->if_xname);
2148
2149 /* pass 1: check for things that need to be rejected */
2150 p = (void *)(h + 1);
2151 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2152 if (debug)
2153 addlog(" %s", sppp_lcp_opt_name(*p));
2154 switch (*p) {
2155 case LCP_OPT_MAGIC:
2156 /* Magic number. */
2157 /* fall through, both are same length */
2158 case LCP_OPT_ASYNC_MAP:
2159 /* Async control character map. */
2160 if (len >= 6 || p[1] == 6)
2161 continue;
2162 if (debug)
2163 addlog(" [invalid]");
2164 break;
2165 case LCP_OPT_MRU:
2166 /* Maximum receive unit. */
2167 if (len >= 4 && p[1] == 4)
2168 continue;
2169 if (debug)
2170 addlog(" [invalid]");
2171 break;
2172 case LCP_OPT_AUTH_PROTO:
2173 if (len < 4) {
2174 if (debug)
2175 addlog(" [invalid]");
2176 break;
2177 }
2178 authproto = (p[2] << 8) + p[3];
2179 if (authproto == PPP_CHAP && p[1] != 5) {
2180 if (debug)
2181 addlog(" [invalid chap len]");
2182 break;
2183 }
2184 if (sp->myauth.proto == 0) {
2185 /* we are not configured to do auth */
2186 if (debug)
2187 addlog(" [not configured]");
2188 break;
2189 }
2190 /*
2191 * Remote want us to authenticate, remember this,
2192 * so we stay in SPPP_PHASE_AUTHENTICATE after LCP got
2193 * up.
2194 */
2195 sp->pp_flags |= PP_NEEDAUTH;
2196 continue;
2197 default:
2198 /* Others not supported. */
2199 if (debug)
2200 addlog(" [rej]");
2201 break;
2202 }
2203 /* Add the option to rejected list. */
2204 bcopy (p, r, p[1]);
2205 r += p[1];
2206 rlen += p[1];
2207 }
2208 if (rlen) {
2209 if (debug)
2210 addlog(" send conf-rej\n");
2211 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2212 goto end;
2213 } else if (debug)
2214 addlog("\n");
2215
2216 /*
2217 * pass 2: check for option values that are unacceptable and
2218 * thus require to be nak'ed.
2219 */
2220 if (debug)
2221 log(LOG_DEBUG, "%s: lcp parse opt values: ",
2222 ifp->if_xname);
2223
2224 p = (void *)(h + 1);
2225 len = origlen;
2226 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2227 if (debug)
2228 addlog(" %s", sppp_lcp_opt_name(*p));
2229 switch (*p) {
2230 case LCP_OPT_MAGIC:
2231 /* Magic number -- extract. */
2232 nmagic = (u_int32_t)p[2] << 24 |
2233 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2234 if (nmagic != sp->lcp.magic) {
2235 if (debug)
2236 addlog(" 0x%x", nmagic);
2237 continue;
2238 }
2239 /*
2240 * Local and remote magics equal -- loopback?
2241 */
2242 if (sp->pp_loopcnt >= LOOPALIVECNT*5) {
2243 printf ("%s: loopback\n",
2244 ifp->if_xname);
2245 sp->pp_loopcnt = 0;
2246 if (ifp->if_flags & IFF_UP) {
2247 if_down(ifp);
2248 IF_PURGE(&sp->pp_cpq);
2249 /* XXX ? */
2250 lcp.Down(sp);
2251 lcp.Up(sp);
2252 }
2253 } else if (debug)
2254 addlog(" [glitch]");
2255 ++sp->pp_loopcnt;
2256 /*
2257 * We negate our magic here, and NAK it. If
2258 * we see it later in an NAK packet, we
2259 * suggest a new one.
2260 */
2261 nmagic = ~sp->lcp.magic;
2262 /* Gonna NAK it. */
2263 p[2] = nmagic >> 24;
2264 p[3] = nmagic >> 16;
2265 p[4] = nmagic >> 8;
2266 p[5] = nmagic;
2267 break;
2268
2269 case LCP_OPT_ASYNC_MAP:
2270 /*
2271 * Async control character map -- just ignore it.
2272 *
2273 * Quote from RFC 1662, chapter 6:
2274 * To enable this functionality, synchronous PPP
2275 * implementations MUST always respond to the
2276 * Async-Control-Character-Map Configuration
2277 * Option with the LCP Configure-Ack. However,
2278 * acceptance of the Configuration Option does
2279 * not imply that the synchronous implementation
2280 * will do any ACCM mapping. Instead, all such
2281 * octet mapping will be performed by the
2282 * asynchronous-to-synchronous converter.
2283 */
2284 continue;
2285
2286 case LCP_OPT_MRU:
2287 /*
2288 * Maximum receive unit. Always agreeable,
2289 * but ignored by now.
2290 */
2291 sp->lcp.their_mru = p[2] * 256 + p[3];
2292 if (debug)
2293 addlog(" %ld", sp->lcp.their_mru);
2294 continue;
2295
2296 case LCP_OPT_AUTH_PROTO:
2297 authproto = (p[2] << 8) + p[3];
2298 if (sp->myauth.proto != authproto) {
2299 /* not agreed, nak */
2300 if (debug)
2301 addlog(" [mine %s != his %s]",
2302 sppp_proto_name(sp->myauth.proto),
2303 sppp_proto_name(authproto));
2304 p[2] = sp->myauth.proto >> 8;
2305 p[3] = sp->myauth.proto;
2306 break;
2307 }
2308 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2309 if (debug)
2310 addlog(" [chap not MD5]");
2311 p[4] = CHAP_MD5;
2312 break;
2313 }
2314 continue;
2315 }
2316 /* Add the option to nak'ed list. */
2317 bcopy (p, r, p[1]);
2318 r += p[1];
2319 rlen += p[1];
2320 }
2321 if (rlen) {
2322 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2323 if (debug)
2324 addlog(" max_failure (%d) exceeded, "
2325 "send conf-rej\n",
2326 sp->lcp.max_failure);
2327 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2328 } else {
2329 if (debug)
2330 addlog(" send conf-nak\n");
2331 sppp_cp_send(sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2332 }
2333 goto end;
2334 } else {
2335 if (debug)
2336 addlog(" send conf-ack\n");
2337 sp->fail_counter[IDX_LCP] = 0;
2338 sp->pp_loopcnt = 0;
2339 sppp_cp_send(sp, PPP_LCP, CONF_ACK, h->ident, origlen, h + 1);
2340 }
2341
2342 end:
2343 free(buf, M_TEMP);
2344 return (rlen == 0);
2345 }
2346
2347 /*
2348 * Analyze the LCP Configure-Reject option list, and adjust our
2349 * negotiation.
2350 */
2351 static void
2352 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2353 {
2354 STDDCL;
2355 u_char *buf, *p;
2356
2357 len -= 4;
2358 buf = malloc (len, M_TEMP, M_NOWAIT);
2359 if (!buf)
2360 return;
2361
2362 if (debug)
2363 log(LOG_DEBUG, "%s: lcp rej opts:",
2364 ifp->if_xname);
2365
2366 p = (void *)(h + 1);
2367 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2368 if (debug)
2369 addlog(" %s", sppp_lcp_opt_name(*p));
2370 switch (*p) {
2371 case LCP_OPT_MAGIC:
2372 /* Magic number -- can't use it, use 0 */
2373 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2374 sp->lcp.magic = 0;
2375 break;
2376 case LCP_OPT_MRU:
2377 /*
2378 * Should not be rejected anyway, since we only
2379 * negotiate a MRU if explicitly requested by
2380 * peer.
2381 */
2382 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2383 break;
2384 case LCP_OPT_AUTH_PROTO:
2385 /*
2386 * Peer doesn't want to authenticate himself,
2387 * deny unless this is a dialout call, and
2388 * SPPP_AUTHFLAG_NOCALLOUT is set.
2389 */
2390 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2391 (sp->hisauth.flags & SPPP_AUTHFLAG_NOCALLOUT) != 0) {
2392 if (debug)
2393 addlog(" [don't insist on auth "
2394 "for callout]");
2395 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2396 break;
2397 }
2398 if (debug)
2399 addlog("[access denied]\n");
2400 lcp.Close(sp);
2401 break;
2402 }
2403 }
2404 if (debug)
2405 addlog("\n");
2406 free(buf, M_TEMP);
2407 return;
2408 }
2409
2410 /*
2411 * Analyze the LCP Configure-NAK option list, and adjust our
2412 * negotiation.
2413 */
2414 static void
2415 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2416 {
2417 STDDCL;
2418 u_char *buf, *p;
2419 u_int32_t magic;
2420
2421 len -= 4;
2422 buf = malloc (len, M_TEMP, M_NOWAIT);
2423 if (!buf)
2424 return;
2425
2426 if (debug)
2427 log(LOG_DEBUG, "%s: lcp nak opts:",
2428 ifp->if_xname);
2429
2430 p = (void *)(h + 1);
2431 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2432 if (debug)
2433 addlog(" %s", sppp_lcp_opt_name(*p));
2434 switch (*p) {
2435 case LCP_OPT_MAGIC:
2436 /* Magic number -- renegotiate */
2437 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2438 len >= 6 && p[1] == 6) {
2439 magic = (u_int32_t)p[2] << 24 |
2440 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2441 /*
2442 * If the remote magic is our negated one,
2443 * this looks like a loopback problem.
2444 * Suggest a new magic to make sure.
2445 */
2446 if (magic == ~sp->lcp.magic) {
2447 if (debug)
2448 addlog(" magic glitch");
2449 sp->lcp.magic = arc4random();
2450 } else {
2451 sp->lcp.magic = magic;
2452 if (debug)
2453 addlog(" %d", magic);
2454 }
2455 }
2456 break;
2457 case LCP_OPT_MRU:
2458 /*
2459 * Peer wants to advise us to negotiate an MRU.
2460 * Agree on it if it's reasonable, or use
2461 * default otherwise.
2462 */
2463 if (len >= 4 && p[1] == 4) {
2464 u_int mru = p[2] * 256 + p[3];
2465 if (debug)
2466 addlog(" %d", mru);
2467 if (mru < PP_MTU || mru > PP_MAX_MRU)
2468 mru = PP_MTU;
2469 sp->lcp.mru = mru;
2470 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2471 }
2472 break;
2473 case LCP_OPT_AUTH_PROTO:
2474 /*
2475 * Peer doesn't like our authentication method,
2476 * deny.
2477 */
2478 if (debug)
2479 addlog("[access denied]\n");
2480 lcp.Close(sp);
2481 break;
2482 }
2483 }
2484 if (debug)
2485 addlog("\n");
2486 free(buf, M_TEMP);
2487 return;
2488 }
2489
2490 static void
2491 sppp_lcp_tlu(struct sppp *sp)
2492 {
2493 STDDCL;
2494 int i;
2495 u_int32_t mask;
2496
2497 /* XXX ? */
2498 if (! (ifp->if_flags & IFF_UP) &&
2499 (ifp->if_flags & IFF_RUNNING)) {
2500 /* Coming out of loopback mode. */
2501 if_up(ifp);
2502 }
2503
2504 for (i = 0; i < IDX_COUNT; i++)
2505 if ((cps[i])->flags & CP_QUAL)
2506 (cps[i])->Open(sp);
2507
2508 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2509 (sp->pp_flags & PP_NEEDAUTH) != 0)
2510 sp->pp_phase = SPPP_PHASE_AUTHENTICATE;
2511 else
2512 sp->pp_phase = SPPP_PHASE_NETWORK;
2513
2514 if (debug)
2515 {
2516 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2517 sppp_phase_name(sp->pp_phase));
2518 }
2519
2520 /*
2521 * Open all authentication protocols. This is even required
2522 * if we already proceeded to network phase, since it might be
2523 * that remote wants us to authenticate, so we might have to
2524 * send a PAP request. Undesired authentication protocols
2525 * don't do anything when they get an Open event.
2526 */
2527 for (i = 0; i < IDX_COUNT; i++)
2528 if ((cps[i])->flags & CP_AUTH)
2529 (cps[i])->Open(sp);
2530
2531 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
2532 /* Notify all NCPs. */
2533 for (i = 0; i < IDX_COUNT; i++)
2534 if ((cps[i])->flags & CP_NCP)
2535 (cps[i])->Open(sp);
2536 }
2537
2538 /* Send Up events to all started protos. */
2539 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2540 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2541 (cps[i])->Up(sp);
2542
2543 /* notify low-level driver of state change */
2544 if (sp->pp_chg)
2545 sp->pp_chg(sp, (int)sp->pp_phase);
2546
2547 if (sp->pp_phase == SPPP_PHASE_NETWORK)
2548 /* if no NCP is starting, close down */
2549 sppp_lcp_check_and_close(sp);
2550 }
2551
2552 static void
2553 sppp_lcp_tld(struct sppp *sp)
2554 {
2555 STDDCL;
2556 int i;
2557 u_int32_t mask;
2558
2559 sp->pp_phase = SPPP_PHASE_TERMINATE;
2560
2561 if (debug)
2562 {
2563 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2564 sppp_phase_name(sp->pp_phase));
2565 }
2566
2567 /*
2568 * Take upper layers down. We send the Down event first and
2569 * the Close second to prevent the upper layers from sending
2570 * ``a flurry of terminate-request packets'', as the RFC
2571 * describes it.
2572 */
2573 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2574 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2575 (cps[i])->Down(sp);
2576 (cps[i])->Close(sp);
2577 }
2578 }
2579
2580 static void
2581 sppp_lcp_tls(struct sppp *sp)
2582 {
2583 STDDCL;
2584
2585 if (sp->pp_max_auth_fail != 0 && sp->pp_auth_failures >= sp->pp_max_auth_fail) {
2586 printf("%s: authentication failed %d times, not retrying again\n",
2587 sp->pp_if.if_xname, sp->pp_auth_failures);
2588 if_down(&sp->pp_if);
2589 return;
2590 }
2591
2592 sp->pp_phase = SPPP_PHASE_ESTABLISH;
2593
2594 if (debug)
2595 {
2596 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2597 sppp_phase_name(sp->pp_phase));
2598 }
2599
2600 /* Notify lower layer if desired. */
2601 if (sp->pp_tls)
2602 (sp->pp_tls)(sp);
2603 }
2604
2605 static void
2606 sppp_lcp_tlf(struct sppp *sp)
2607 {
2608 STDDCL;
2609
2610 sp->pp_phase = SPPP_PHASE_DEAD;
2611
2612 if (debug)
2613 {
2614 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
2615 sppp_phase_name(sp->pp_phase));
2616 }
2617
2618 /* Notify lower layer if desired. */
2619 if (sp->pp_tlf)
2620 (sp->pp_tlf)(sp);
2621 }
2622
2623 static void
2624 sppp_lcp_scr(struct sppp *sp)
2625 {
2626 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2627 int i = 0;
2628 u_short authproto;
2629
2630 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2631 if (! sp->lcp.magic)
2632 sp->lcp.magic = arc4random();
2633 opt[i++] = LCP_OPT_MAGIC;
2634 opt[i++] = 6;
2635 opt[i++] = sp->lcp.magic >> 24;
2636 opt[i++] = sp->lcp.magic >> 16;
2637 opt[i++] = sp->lcp.magic >> 8;
2638 opt[i++] = sp->lcp.magic;
2639 }
2640
2641 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2642 opt[i++] = LCP_OPT_MRU;
2643 opt[i++] = 4;
2644 opt[i++] = sp->lcp.mru >> 8;
2645 opt[i++] = sp->lcp.mru;
2646 }
2647
2648 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2649 authproto = sp->hisauth.proto;
2650 opt[i++] = LCP_OPT_AUTH_PROTO;
2651 opt[i++] = authproto == PPP_CHAP? 5: 4;
2652 opt[i++] = authproto >> 8;
2653 opt[i++] = authproto;
2654 if (authproto == PPP_CHAP)
2655 opt[i++] = CHAP_MD5;
2656 }
2657
2658 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2659 sppp_cp_send(sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2660 }
2661
2662 /*
2663 * Check the open NCPs, return true if at least one NCP is open.
2664 */
2665 static int
2666 sppp_ncp_check(struct sppp *sp)
2667 {
2668 int i, mask;
2669
2670 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2671 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2672 return 1;
2673 return 0;
2674 }
2675
2676 /*
2677 * Re-check the open NCPs and see if we should terminate the link.
2678 * Called by the NCPs during their tlf action handling.
2679 */
2680 static void
2681 sppp_lcp_check_and_close(struct sppp *sp)
2682 {
2683
2684 if (sp->pp_phase < SPPP_PHASE_NETWORK)
2685 /* don't bother, we are already going down */
2686 return;
2687
2688 if (sppp_ncp_check(sp))
2689 return;
2690
2691 lcp.Close(sp);
2692 }
2693
2694
2695 /*
2696 *--------------------------------------------------------------------------*
2697 * *
2698 * The IPCP implementation. *
2699 * *
2700 *--------------------------------------------------------------------------*
2701 */
2702
2703 static void
2704 sppp_ipcp_init(struct sppp *sp)
2705 {
2706 sp->ipcp.opts = 0;
2707 sp->ipcp.flags = 0;
2708 sp->state[IDX_IPCP] = STATE_INITIAL;
2709 sp->fail_counter[IDX_IPCP] = 0;
2710 sp->pp_seq[IDX_IPCP] = 0;
2711 sp->pp_rseq[IDX_IPCP] = 0;
2712 callout_init(&sp->ch[IDX_IPCP]);
2713 }
2714
2715 static void
2716 sppp_ipcp_up(struct sppp *sp)
2717 {
2718 sppp_up_event(&ipcp, sp);
2719 }
2720
2721 static void
2722 sppp_ipcp_down(struct sppp *sp)
2723 {
2724 sppp_down_event(&ipcp, sp);
2725 }
2726
2727 static void
2728 sppp_ipcp_open(struct sppp *sp)
2729 {
2730 STDDCL;
2731 u_int32_t myaddr, hisaddr;
2732
2733 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
2734 sp->ipcp.req_myaddr = 0;
2735 sp->ipcp.req_hisaddr = 0;
2736 memset(&sp->dns_addrs, 0, sizeof sp->dns_addrs);
2737
2738 #ifdef INET
2739 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2740 #else
2741 myaddr = hisaddr = 0;
2742 #endif
2743 /*
2744 * If we don't have his address, this probably means our
2745 * interface doesn't want to talk IP at all. (This could
2746 * be the case if somebody wants to speak only IPX, for
2747 * example.) Don't open IPCP in this case.
2748 */
2749 if (hisaddr == 0) {
2750 /* XXX this message should go away */
2751 if (debug)
2752 log(LOG_DEBUG, "%s: ipcp_open(): no IP interface\n",
2753 ifp->if_xname);
2754 return;
2755 }
2756
2757 if (myaddr == 0) {
2758 /*
2759 * I don't have an assigned address, so i need to
2760 * negotiate my address.
2761 */
2762 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2763 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2764 }
2765 if (hisaddr == 1) {
2766 /*
2767 * XXX - remove this hack!
2768 * remote has no valid address, we need to get one assigned.
2769 */
2770 sp->ipcp.flags |= IPCP_HISADDR_DYN;
2771 }
2772 sppp_open_event(&ipcp, sp);
2773 }
2774
2775 static void
2776 sppp_ipcp_close(struct sppp *sp)
2777 {
2778 STDDCL;
2779
2780 sppp_close_event(&ipcp, sp);
2781 #ifdef INET
2782 if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
2783 /*
2784 * Some address was dynamic, clear it again.
2785 */
2786 sppp_clear_ip_addrs(sp);
2787 #endif
2788
2789 if (sp->pp_saved_mtu > 0) {
2790 ifp->if_mtu = sp->pp_saved_mtu;
2791 sp->pp_saved_mtu = 0;
2792 if (debug)
2793 log(LOG_DEBUG,
2794 "%s: resetting MTU to %" PRIu64 " bytes\n",
2795 ifp->if_xname, ifp->if_mtu);
2796 }
2797 }
2798
2799 static void
2800 sppp_ipcp_TO(void *cookie)
2801 {
2802 sppp_to_event(&ipcp, (struct sppp *)cookie);
2803 }
2804
2805 /*
2806 * Analyze a configure request. Return true if it was agreeable, and
2807 * caused action sca, false if it has been rejected or nak'ed, and
2808 * caused action scn. (The return value is used to make the state
2809 * transition decision in the state automaton.)
2810 */
2811 static int
2812 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2813 {
2814 u_char *buf, *r, *p;
2815 struct ifnet *ifp = &sp->pp_if;
2816 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2817 u_int32_t hisaddr, desiredaddr;
2818
2819 len -= 4;
2820 origlen = len;
2821 /*
2822 * Make sure to allocate a buf that can at least hold a
2823 * conf-nak with an `address' option. We might need it below.
2824 */
2825 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2826 if (! buf)
2827 return (0);
2828
2829 /* pass 1: see if we can recognize them */
2830 if (debug)
2831 log(LOG_DEBUG, "%s: ipcp parse opts:",
2832 ifp->if_xname);
2833 p = (void *)(h + 1);
2834 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2835 if (debug)
2836 addlog(" %s", sppp_ipcp_opt_name(*p));
2837 switch (*p) {
2838 #ifdef notyet
2839 case IPCP_OPT_COMPRESSION:
2840 if (len >= 6 && p[1] >= 6) {
2841 /* correctly formed compress option */
2842 continue;
2843 }
2844 if (debug)
2845 addlog(" [invalid]");
2846 break;
2847 #endif
2848 case IPCP_OPT_ADDRESS:
2849 if (len >= 6 && p[1] == 6) {
2850 /* correctly formed address option */
2851 continue;
2852 }
2853 if (debug)
2854 addlog(" [invalid]");
2855 break;
2856 default:
2857 /* Others not supported. */
2858 if (debug)
2859 addlog(" [rej]");
2860 break;
2861 }
2862 /* Add the option to rejected list. */
2863 bcopy (p, r, p[1]);
2864 r += p[1];
2865 rlen += p[1];
2866 }
2867 if (rlen) {
2868 if (debug)
2869 addlog(" send conf-rej\n");
2870 sppp_cp_send(sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2871 goto end;
2872 } else if (debug)
2873 addlog("\n");
2874
2875 /* pass 2: parse option values */
2876 if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
2877 hisaddr = sp->ipcp.req_hisaddr; /* we already aggreed on that */
2878 else
2879 #ifdef INET
2880 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); /* user configuration */
2881 #else
2882 hisaddr = 0;
2883 #endif
2884 if (debug)
2885 log(LOG_DEBUG, "%s: ipcp parse opt values: ",
2886 ifp->if_xname);
2887 p = (void *)(h + 1);
2888 len = origlen;
2889 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2890 if (debug)
2891 addlog(" %s", sppp_ipcp_opt_name(*p));
2892 switch (*p) {
2893 #ifdef notyet
2894 case IPCP_OPT_COMPRESSION:
2895 continue;
2896 #endif
2897 case IPCP_OPT_ADDRESS:
2898 desiredaddr = p[2] << 24 | p[3] << 16 |
2899 p[4] << 8 | p[5];
2900 if (desiredaddr == hisaddr ||
2901 ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
2902 /*
2903 * Peer's address is same as our value,
2904 * this is agreeable. Gonna conf-ack
2905 * it.
2906 */
2907 if (debug)
2908 addlog(" %s [ack]",
2909 sppp_dotted_quad(hisaddr));
2910 /* record that we've seen it already */
2911 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2912 sp->ipcp.req_hisaddr = desiredaddr;
2913 hisaddr = desiredaddr;
2914 continue;
2915 }
2916 /*
2917 * The address wasn't agreeable. This is either
2918 * he sent us 0.0.0.0, asking to assign him an
2919 * address, or he send us another address not
2920 * matching our value. Either case, we gonna
2921 * conf-nak it with our value.
2922 */
2923 if (debug) {
2924 if (desiredaddr == 0)
2925 addlog(" [addr requested]");
2926 else
2927 addlog(" %s [not agreed]",
2928 sppp_dotted_quad(desiredaddr));
2929 }
2930
2931 p[2] = hisaddr >> 24;
2932 p[3] = hisaddr >> 16;
2933 p[4] = hisaddr >> 8;
2934 p[5] = hisaddr;
2935 break;
2936 }
2937 /* Add the option to nak'ed list. */
2938 bcopy (p, r, p[1]);
2939 r += p[1];
2940 rlen += p[1];
2941 }
2942
2943 /*
2944 * If we are about to conf-ack the request, but haven't seen
2945 * his address so far, gonna conf-nak it instead, with the
2946 * `address' option present and our idea of his address being
2947 * filled in there, to request negotiation of both addresses.
2948 *
2949 * XXX This can result in an endless req - nak loop if peer
2950 * doesn't want to send us his address. Q: What should we do
2951 * about it? XXX A: implement the max-failure counter.
2952 */
2953 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2954 buf[0] = IPCP_OPT_ADDRESS;
2955 buf[1] = 6;
2956 buf[2] = hisaddr >> 24;
2957 buf[3] = hisaddr >> 16;
2958 buf[4] = hisaddr >> 8;
2959 buf[5] = hisaddr;
2960 rlen = 6;
2961 if (debug)
2962 addlog(" still need hisaddr");
2963 }
2964
2965 if (rlen) {
2966 if (debug)
2967 addlog(" send conf-nak\n");
2968 sppp_cp_send(sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2969 } else {
2970 if (debug)
2971 addlog(" send conf-ack\n");
2972 sppp_cp_send(sp, PPP_IPCP, CONF_ACK, h->ident, origlen, h + 1);
2973 }
2974
2975 end:
2976 free(buf, M_TEMP);
2977 return (rlen == 0);
2978 }
2979
2980 /*
2981 * Analyze the IPCP Configure-Reject option list, and adjust our
2982 * negotiation.
2983 */
2984 static void
2985 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2986 {
2987 u_char *buf, *p;
2988 struct ifnet *ifp = &sp->pp_if;
2989 int debug = ifp->if_flags & IFF_DEBUG;
2990
2991 len -= 4;
2992 buf = malloc (len, M_TEMP, M_NOWAIT);
2993 if (!buf)
2994 return;
2995
2996 if (debug)
2997 log(LOG_DEBUG, "%s: ipcp rej opts:",
2998 ifp->if_xname);
2999
3000 p = (void *)(h + 1);
3001 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3002 if (debug)
3003 addlog(" %s", sppp_ipcp_opt_name(*p));
3004 switch (*p) {
3005 case IPCP_OPT_ADDRESS:
3006 /*
3007 * Peer doesn't grok address option. This is
3008 * bad. XXX Should we better give up here?
3009 */
3010 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3011 break;
3012 #ifdef notyet
3013 case IPCP_OPT_COMPRESS:
3014 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
3015 break;
3016 #endif
3017 }
3018 }
3019 if (debug)
3020 addlog("\n");
3021 free(buf, M_TEMP);
3022 return;
3023 }
3024
3025 /*
3026 * Analyze the IPCP Configure-NAK option list, and adjust our
3027 * negotiation.
3028 */
3029 static void
3030 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3031 {
3032 u_char *p;
3033 struct ifnet *ifp = &sp->pp_if;
3034 int debug = ifp->if_flags & IFF_DEBUG;
3035 u_int32_t wantaddr;
3036
3037 len -= 4;
3038
3039 if (debug)
3040 log(LOG_DEBUG, "%s: ipcp nak opts:",
3041 ifp->if_xname);
3042
3043 p = (void *)(h + 1);
3044 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3045 if (debug)
3046 addlog(" %s", sppp_ipcp_opt_name(*p));
3047 switch (*p) {
3048 case IPCP_OPT_ADDRESS:
3049 /*
3050 * Peer doesn't like our local IP address. See
3051 * if we can do something for him. We'll drop
3052 * him our address then.
3053 */
3054 if (len >= 6 && p[1] == 6) {
3055 wantaddr = p[2] << 24 | p[3] << 16 |
3056 p[4] << 8 | p[5];
3057 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3058 if (debug)
3059 addlog(" [wantaddr %s]",
3060 sppp_dotted_quad(wantaddr));
3061 /*
3062 * When doing dynamic address assignment,
3063 * we accept his offer. Otherwise, we
3064 * ignore it and thus continue to negotiate
3065 * our already existing value.
3066 */
3067 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3068 if (debug)
3069 addlog(" [agree]");
3070 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3071 sp->ipcp.req_myaddr = wantaddr;
3072 }
3073 }
3074 break;
3075
3076 case IPCP_OPT_PRIMDNS:
3077 if (len >= 6 && p[1] == 6) {
3078 sp->dns_addrs[0] = p[2] << 24 | p[3] << 16 |
3079 p[4] << 8 | p[5];
3080 }
3081 break;
3082
3083 case IPCP_OPT_SECDNS:
3084 if (len >= 6 && p[1] == 6) {
3085 sp->dns_addrs[1] = p[2] << 24 | p[3] << 16 |
3086 p[4] << 8 | p[5];
3087 }
3088 break;
3089 #ifdef notyet
3090 case IPCP_OPT_COMPRESS:
3091 /*
3092 * Peer wants different compression parameters.
3093 */
3094 break;
3095 #endif
3096 }
3097 }
3098 if (debug)
3099 addlog("\n");
3100 return;
3101 }
3102
3103 static void
3104 sppp_ipcp_tlu(struct sppp *sp)
3105 {
3106 #ifdef INET
3107 /* we are up. Set addresses and notify anyone interested */
3108 STDDCL;
3109 u_int32_t myaddr, hisaddr;
3110
3111 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
3112 if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
3113 myaddr = sp->ipcp.req_myaddr;
3114 if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
3115 hisaddr = sp->ipcp.req_hisaddr;
3116 sppp_set_ip_addrs(sp, myaddr, hisaddr);
3117
3118 if (ifp->if_mtu > sp->lcp.their_mru) {
3119 sp->pp_saved_mtu = ifp->if_mtu;
3120 ifp->if_mtu = sp->lcp.their_mru;
3121 if (debug)
3122 log(LOG_DEBUG,
3123 "%s: setting MTU to %" PRIu64 " bytes\n",
3124 ifp->if_xname, ifp->if_mtu);
3125 }
3126
3127 if (sp->pp_con)
3128 sp->pp_con(sp);
3129 #endif
3130 }
3131
3132 static void
3133 sppp_ipcp_tld(struct sppp *sp)
3134 {
3135 }
3136
3137 static void
3138 sppp_ipcp_tls(struct sppp *sp)
3139 {
3140 /* indicate to LCP that it must stay alive */
3141 sp->lcp.protos |= (1 << IDX_IPCP);
3142 }
3143
3144 static void
3145 sppp_ipcp_tlf(struct sppp *sp)
3146 {
3147 /* we no longer need LCP */
3148 sp->lcp.protos &= ~(1 << IDX_IPCP);
3149 }
3150
3151 static void
3152 sppp_ipcp_scr(struct sppp *sp)
3153 {
3154 char opt[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
3155 #ifdef INET
3156 u_int32_t ouraddr;
3157 #endif
3158 int i = 0;
3159
3160 #ifdef notyet
3161 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3162 opt[i++] = IPCP_OPT_COMPRESSION;
3163 opt[i++] = 6;
3164 opt[i++] = 0; /* VJ header compression */
3165 opt[i++] = 0x2d; /* VJ header compression */
3166 opt[i++] = max_slot_id;
3167 opt[i++] = comp_slot_id;
3168 }
3169 #endif
3170
3171 #ifdef INET
3172 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3173 if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
3174 ouraddr = sp->ipcp.req_myaddr; /* not sure if this can ever happen */
3175 else
3176 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3177 opt[i++] = IPCP_OPT_ADDRESS;
3178 opt[i++] = 6;
3179 opt[i++] = ouraddr >> 24;
3180 opt[i++] = ouraddr >> 16;
3181 opt[i++] = ouraddr >> 8;
3182 opt[i++] = ouraddr;
3183 }
3184 #endif
3185
3186 if (sp->query_dns & 1) {
3187 opt[i++] = IPCP_OPT_PRIMDNS;
3188 opt[i++] = 6;
3189 opt[i++] = sp->dns_addrs[0] >> 24;
3190 opt[i++] = sp->dns_addrs[0] >> 16;
3191 opt[i++] = sp->dns_addrs[0] >> 8;
3192 opt[i++] = sp->dns_addrs[0];
3193 }
3194 if (sp->query_dns & 2) {
3195 opt[i++] = IPCP_OPT_SECDNS;
3196 opt[i++] = 6;
3197 opt[i++] = sp->dns_addrs[1] >> 24;
3198 opt[i++] = sp->dns_addrs[1] >> 16;
3199 opt[i++] = sp->dns_addrs[1] >> 8;
3200 opt[i++] = sp->dns_addrs[1];
3201 }
3202
3203 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3204 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3205 }
3206
3207
3208 /*
3209 *--------------------------------------------------------------------------*
3210 * *
3211 * The IPv6CP implementation. *
3212 * *
3213 *--------------------------------------------------------------------------*
3214 */
3215
3216 #ifdef INET6
3217 static void
3218 sppp_ipv6cp_init(struct sppp *sp)
3219 {
3220 sp->ipv6cp.opts = 0;
3221 sp->ipv6cp.flags = 0;
3222 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3223 sp->fail_counter[IDX_IPV6CP] = 0;
3224 sp->pp_seq[IDX_IPV6CP] = 0;
3225 sp->pp_rseq[IDX_IPV6CP] = 0;
3226 callout_init(&sp->ch[IDX_IPV6CP]);
3227 }
3228
3229 static void
3230 sppp_ipv6cp_up(struct sppp *sp)
3231 {
3232 sppp_up_event(&ipv6cp, sp);
3233 }
3234
3235 static void
3236 sppp_ipv6cp_down(struct sppp *sp)
3237 {
3238 sppp_down_event(&ipv6cp, sp);
3239 }
3240
3241 static void
3242 sppp_ipv6cp_open(struct sppp *sp)
3243 {
3244 STDDCL;
3245 struct in6_addr myaddr, hisaddr;
3246
3247 #ifdef IPV6CP_MYIFID_DYN
3248 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3249 #else
3250 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3251 #endif
3252
3253 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3254 /*
3255 * If we don't have our address, this probably means our
3256 * interface doesn't want to talk IPv6 at all. (This could
3257 * be the case if somebody wants to speak only IPX, for
3258 * example.) Don't open IPv6CP in this case.
3259 */
3260 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3261 /* XXX this message should go away */
3262 if (debug)
3263 log(LOG_DEBUG, "%s: ipv6cp_open(): no IPv6 interface\n",
3264 ifp->if_xname);
3265 return;
3266 }
3267
3268 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3269 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3270 sppp_open_event(&ipv6cp, sp);
3271 }
3272
3273 static void
3274 sppp_ipv6cp_close(struct sppp *sp)
3275 {
3276 sppp_close_event(&ipv6cp, sp);
3277 }
3278
3279 static void
3280 sppp_ipv6cp_TO(void *cookie)
3281 {
3282 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3283 }
3284
3285 /*
3286 * Analyze a configure request. Return true if it was agreeable, and
3287 * caused action sca, false if it has been rejected or nak'ed, and
3288 * caused action scn. (The return value is used to make the state
3289 * transition decision in the state automaton.)
3290 */
3291 static int
3292 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3293 {
3294 u_char *buf, *r, *p;
3295 struct ifnet *ifp = &sp->pp_if;
3296 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3297 struct in6_addr myaddr, desiredaddr, suggestaddr;
3298 int ifidcount;
3299 int type;
3300 int collision, nohisaddr;
3301
3302 len -= 4;
3303 origlen = len;
3304 /*
3305 * Make sure to allocate a buf that can at least hold a
3306 * conf-nak with an `address' option. We might need it below.
3307 */
3308 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3309 if (! buf)
3310 return (0);
3311
3312 /* pass 1: see if we can recognize them */
3313 if (debug)
3314 log(LOG_DEBUG, "%s: ipv6cp parse opts:",
3315 ifp->if_xname);
3316 p = (void *)(h + 1);
3317 ifidcount = 0;
3318 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3319 if (debug)
3320 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3321 switch (*p) {
3322 case IPV6CP_OPT_IFID:
3323 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3324 /* correctly formed address option */
3325 ifidcount++;
3326 continue;
3327 }
3328 if (debug)
3329 addlog(" [invalid]");
3330 break;
3331 #ifdef notyet
3332 case IPV6CP_OPT_COMPRESSION:
3333 if (len >= 4 && p[1] >= 4) {
3334 /* correctly formed compress option */
3335 continue;
3336 }
3337 if (debug)
3338 addlog(" [invalid]");
3339 break;
3340 #endif
3341 default:
3342 /* Others not supported. */
3343 if (debug)
3344 addlog(" [rej]");
3345 break;
3346 }
3347 /* Add the option to rejected list. */
3348 bcopy (p, r, p[1]);
3349 r += p[1];
3350 rlen += p[1];
3351 }
3352 if (rlen) {
3353 if (debug)
3354 addlog(" send conf-rej\n");
3355 sppp_cp_send(sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3356 goto end;
3357 } else if (debug)
3358 addlog("\n");
3359
3360 /* pass 2: parse option values */
3361 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3362 if (debug)
3363 log(LOG_DEBUG, "%s: ipv6cp parse opt values: ",
3364 ifp->if_xname);
3365 p = (void *)(h + 1);
3366 len = origlen;
3367 type = CONF_ACK;
3368 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3369 if (debug)
3370 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3371 switch (*p) {
3372 #ifdef notyet
3373 case IPV6CP_OPT_COMPRESSION:
3374 continue;
3375 #endif
3376 case IPV6CP_OPT_IFID:
3377 memset(&desiredaddr, 0, sizeof(desiredaddr));
3378 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3379 collision = (memcmp(&desiredaddr.s6_addr[8],
3380 &myaddr.s6_addr[8], 8) == 0);
3381 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3382
3383 desiredaddr.s6_addr16[0] = htons(0xfe80);
3384 (void)in6_setscope(&desiredaddr, &sp->pp_if, NULL);
3385
3386 if (!collision && !nohisaddr) {
3387 /* no collision, hisaddr known - Conf-Ack */
3388 type = CONF_ACK;
3389
3390 if (debug) {
3391 addlog(" %s [%s]",
3392 ip6_sprintf(&desiredaddr),
3393 sppp_cp_type_name(type));
3394 }
3395 continue;
3396 }
3397
3398 memset(&suggestaddr, 0, sizeof(&suggestaddr));
3399 if (collision && nohisaddr) {
3400 /* collision, hisaddr unknown - Conf-Rej */
3401 type = CONF_REJ;
3402 memset(&p[2], 0, 8);
3403 } else {
3404 /*
3405 * - no collision, hisaddr unknown, or
3406 * - collision, hisaddr known
3407 * Conf-Nak, suggest hisaddr
3408 */
3409 type = CONF_NAK;
3410 sppp_suggest_ip6_addr(sp, &suggestaddr);
3411 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3412 }
3413 if (debug)
3414 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3415 sppp_cp_type_name(type));
3416 break;
3417 }
3418 /* Add the option to nak'ed list. */
3419 bcopy (p, r, p[1]);
3420 r += p[1];
3421 rlen += p[1];
3422 }
3423
3424 if (rlen == 0 && type == CONF_ACK) {
3425 if (debug)
3426 addlog(" send %s\n", sppp_cp_type_name(type));
3427 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, origlen, h + 1);
3428 } else {
3429 #ifdef notdef
3430 if (type == CONF_ACK)
3431 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3432 #endif
3433
3434 if (debug) {
3435 addlog(" send %s suggest %s\n",
3436 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3437 }
3438 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3439 }
3440
3441 end:
3442 free(buf, M_TEMP);
3443 return (rlen == 0);
3444 }
3445
3446 /*
3447 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3448 * negotiation.
3449 */
3450 static void
3451 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3452 {
3453 u_char *buf, *p;
3454 struct ifnet *ifp = &sp->pp_if;
3455 int debug = ifp->if_flags & IFF_DEBUG;
3456
3457 len -= 4;
3458 buf = malloc (len, M_TEMP, M_NOWAIT);
3459 if (!buf)
3460 return;
3461
3462 if (debug)
3463 log(LOG_DEBUG, "%s: ipv6cp rej opts:",
3464 ifp->if_xname);
3465
3466 p = (void *)(h + 1);
3467 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3468 if (debug)
3469 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3470 switch (*p) {
3471 case IPV6CP_OPT_IFID:
3472 /*
3473 * Peer doesn't grok address option. This is
3474 * bad. XXX Should we better give up here?
3475 */
3476 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3477 break;
3478 #ifdef notyet
3479 case IPV6CP_OPT_COMPRESS:
3480 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3481 break;
3482 #endif
3483 }
3484 }
3485 if (debug)
3486 addlog("\n");
3487 free(buf, M_TEMP);
3488 return;
3489 }
3490
3491 /*
3492 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3493 * negotiation.
3494 */
3495 static void
3496 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3497 {
3498 u_char *buf, *p;
3499 struct ifnet *ifp = &sp->pp_if;
3500 int debug = ifp->if_flags & IFF_DEBUG;
3501 struct in6_addr suggestaddr;
3502
3503 len -= 4;
3504 buf = malloc (len, M_TEMP, M_NOWAIT);
3505 if (!buf)
3506 return;
3507
3508 if (debug)
3509 log(LOG_DEBUG, "%s: ipv6cp nak opts:",
3510 ifp->if_xname);
3511
3512 p = (void *)(h + 1);
3513 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3514 if (debug)
3515 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3516 switch (*p) {
3517 case IPV6CP_OPT_IFID:
3518 /*
3519 * Peer doesn't like our local ifid. See
3520 * if we can do something for him. We'll drop
3521 * him our address then.
3522 */
3523 if (len < 10 || p[1] != 10)
3524 break;
3525 memset(&suggestaddr, 0, sizeof(suggestaddr));
3526 suggestaddr.s6_addr16[0] = htons(0xfe80);
3527 (void)in6_setscope(&suggestaddr, &sp->pp_if, NULL);
3528 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3529
3530 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3531 if (debug)
3532 addlog(" [suggestaddr %s]",
3533 ip6_sprintf(&suggestaddr));
3534 #ifdef IPV6CP_MYIFID_DYN
3535 /*
3536 * When doing dynamic address assignment,
3537 * we accept his offer.
3538 */
3539 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3540 struct in6_addr lastsuggest;
3541 /*
3542 * If <suggested myaddr from peer> equals to
3543 * <hisaddr we have suggested last time>,
3544 * we have a collision. generate new random
3545 * ifid.
3546 */
3547 sppp_suggest_ip6_addr(&lastsuggest);
3548 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3549 lastsuggest)) {
3550 if (debug)
3551 addlog(" [random]");
3552 sppp_gen_ip6_addr(sp, &suggestaddr);
3553 }
3554 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3555 if (debug)
3556 addlog(" [agree]");
3557 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3558 }
3559 #else
3560 /*
3561 * Since we do not do dynamic address assignment,
3562 * we ignore it and thus continue to negotiate
3563 * our already existing value. This can possibly
3564 * go into infinite request-reject loop.
3565 *
3566 * This is not likely because we normally use
3567 * ifid based on MAC-address.
3568 * If you have no ethernet card on the node, too bad.
3569 * XXX should we use fail_counter?
3570 */
3571 #endif
3572 break;
3573 #ifdef notyet
3574 case IPV6CP_OPT_COMPRESS:
3575 /*
3576 * Peer wants different compression parameters.
3577 */
3578 break;
3579 #endif
3580 }
3581 }
3582 if (debug)
3583 addlog("\n");
3584 free(buf, M_TEMP);
3585 return;
3586 }
3587
3588 static void
3589 sppp_ipv6cp_tlu(struct sppp *sp)
3590 {
3591 /* we are up - notify isdn daemon */
3592 if (sp->pp_con)
3593 sp->pp_con(sp);
3594 }
3595
3596 static void
3597 sppp_ipv6cp_tld(struct sppp *sp)
3598 {
3599 }
3600
3601 static void
3602 sppp_ipv6cp_tls(struct sppp *sp)
3603 {
3604 /* indicate to LCP that it must stay alive */
3605 sp->lcp.protos |= (1 << IDX_IPV6CP);
3606 }
3607
3608 static void
3609 sppp_ipv6cp_tlf(struct sppp *sp)
3610 {
3611 /* we no longer need LCP */
3612 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3613 }
3614
3615 static void
3616 sppp_ipv6cp_scr(struct sppp *sp)
3617 {
3618 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3619 struct in6_addr ouraddr;
3620 int i = 0;
3621
3622 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3623 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3624 opt[i++] = IPV6CP_OPT_IFID;
3625 opt[i++] = 10;
3626 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3627 i += 8;
3628 }
3629
3630 #ifdef notyet
3631 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3632 opt[i++] = IPV6CP_OPT_COMPRESSION;
3633 opt[i++] = 4;
3634 opt[i++] = 0; /* TBD */
3635 opt[i++] = 0; /* TBD */
3636 /* variable length data may follow */
3637 }
3638 #endif
3639
3640 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3641 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3642 }
3643 #else /*INET6*/
3644 static void sppp_ipv6cp_init(struct sppp *sp)
3645 {
3646 }
3647
3648 static void sppp_ipv6cp_up(struct sppp *sp)
3649 {
3650 }
3651
3652 static void sppp_ipv6cp_down(struct sppp *sp)
3653 {
3654 }
3655
3656
3657 static void sppp_ipv6cp_open(struct sppp *sp)
3658 {
3659 }
3660
3661 static void sppp_ipv6cp_close(struct sppp *sp)
3662 {
3663 }
3664
3665 static void sppp_ipv6cp_TO(void *sp)
3666 {
3667 }
3668
3669 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3670 {
3671 return 0;
3672 }
3673
3674 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3675 {
3676 }
3677
3678 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3679 {
3680 }
3681
3682 static void sppp_ipv6cp_tlu(struct sppp *sp)
3683 {
3684 }
3685
3686 static void sppp_ipv6cp_tld(struct sppp *sp)
3687 {
3688 }
3689
3690 static void sppp_ipv6cp_tls(struct sppp *sp)
3691 {
3692 }
3693
3694 static void sppp_ipv6cp_tlf(struct sppp *sp)
3695 {
3696 }
3697
3698 static void sppp_ipv6cp_scr(struct sppp *sp)
3699 {
3700 }
3701 #endif /*INET6*/
3702
3703
3704 /*
3705 *--------------------------------------------------------------------------*
3706 * *
3707 * The CHAP implementation. *
3708 * *
3709 *--------------------------------------------------------------------------*
3710 */
3711
3712 /*
3713 * The authentication protocols don't employ a full-fledged state machine as
3714 * the control protocols do, since they do have Open and Close events, but
3715 * not Up and Down, nor are they explicitly terminated. Also, use of the
3716 * authentication protocols may be different in both directions (this makes
3717 * sense, think of a machine that never accepts incoming calls but only
3718 * calls out, it doesn't require the called party to authenticate itself).
3719 *
3720 * Our state machine for the local authentication protocol (we are requesting
3721 * the peer to authenticate) looks like:
3722 *
3723 * RCA-
3724 * +--------------------------------------------+
3725 * V scn,tld|
3726 * +--------+ Close +---------+ RCA+
3727 * | |<----------------------------------| |------+
3728 * +--->| Closed | TO* | Opened | sca |
3729 * | | |-----+ +-------| |<-----+
3730 * | +--------+ irc | | +---------+
3731 * | ^ | | ^
3732 * | | | | |
3733 * | | | | |
3734 * | TO-| | | |
3735 * | |tld TO+ V | |
3736 * | | +------->+ | |
3737 * | | | | | |
3738 * | +--------+ V | |
3739 * | | |<----+<--------------------+ |
3740 * | | Req- | scr |
3741 * | | Sent | |
3742 * | | | |
3743 * | +--------+ |
3744 * | RCA- | | RCA+ |
3745 * +------+ +------------------------------------------+
3746 * scn,tld sca,irc,ict,tlu
3747 *
3748 *
3749 * with:
3750 *
3751 * Open: LCP reached authentication phase
3752 * Close: LCP reached terminate phase
3753 *
3754 * RCA+: received reply (pap-req, chap-response), acceptable
3755 * RCN: received reply (pap-req, chap-response), not acceptable
3756 * TO+: timeout with restart counter >= 0
3757 * TO-: timeout with restart counter < 0
3758 * TO*: reschedule timeout for CHAP
3759 *
3760 * scr: send request packet (none for PAP, chap-challenge)
3761 * sca: send ack packet (pap-ack, chap-success)
3762 * scn: send nak packet (pap-nak, chap-failure)
3763 * ict: initialize re-challenge timer (CHAP only)
3764 *
3765 * tlu: this-layer-up, LCP reaches network phase
3766 * tld: this-layer-down, LCP enters terminate phase
3767 *
3768 * Note that in CHAP mode, after sending a new challenge, while the state
3769 * automaton falls back into Req-Sent state, it doesn't signal a tld
3770 * event to LCP, so LCP remains in network phase. Only after not getting
3771 * any response (or after getting an unacceptable response), CHAP closes,
3772 * causing LCP to enter terminate phase.
3773 *
3774 * With PAP, there is no initial request that can be sent. The peer is
3775 * expected to send one based on the successful negotiation of PAP as
3776 * the authentication protocol during the LCP option negotiation.
3777 *
3778 * Incoming authentication protocol requests (remote requests
3779 * authentication, we are peer) don't employ a state machine at all,
3780 * they are simply answered. Some peers [Ascend P50 firmware rev
3781 * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
3782 * still in authentication phase (thereby violating the standard that
3783 * demands that these NCP packets are to be discarded), so we keep
3784 * track of the peer demanding us to authenticate, and only proceed to
3785 * phase network once we've seen a positive acknowledge for the
3786 * authentication.
3787 */
3788
3789 /*
3790 * Handle incoming CHAP packets.
3791 */
3792 void
3793 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3794 {
3795 STDDCL;
3796 struct lcp_header *h;
3797 int len, x;
3798 u_char *value, *name, digest[sizeof(sp->myauth.challenge)], dsize;
3799 int value_len, name_len;
3800 MD5_CTX ctx;
3801
3802 len = m->m_pkthdr.len;
3803 if (len < 4) {
3804 if (debug)
3805 log(LOG_DEBUG,
3806 "%s: chap invalid packet length: %d bytes\n",
3807 ifp->if_xname, len);
3808 return;
3809 }
3810 h = mtod(m, struct lcp_header *);
3811 if (len > ntohs(h->len))
3812 len = ntohs(h->len);
3813
3814 switch (h->type) {
3815 /* challenge, failure and success are his authproto */
3816 case CHAP_CHALLENGE:
3817 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
3818 /* can't do anything usefull */
3819 sp->pp_auth_failures++;
3820 printf("%s: chap input without my name and my secret being set\n",
3821 ifp->if_xname);
3822 break;
3823 }
3824 value = 1 + (u_char *)(h + 1);
3825 value_len = value[-1];
3826 name = value + value_len;
3827 name_len = len - value_len - 5;
3828 if (name_len < 0) {
3829 if (debug) {
3830 log(LOG_DEBUG,
3831 "%s: chap corrupted challenge "
3832 "<%s id=0x%x len=%d",
3833 ifp->if_xname,
3834 sppp_auth_type_name(PPP_CHAP, h->type),
3835 h->ident, ntohs(h->len));
3836 if (len > 4)
3837 sppp_print_bytes((u_char *)(h + 1),
3838 len - 4);
3839 addlog(">\n");
3840 }
3841 break;
3842 }
3843
3844 if (debug) {
3845 log(LOG_DEBUG,
3846 "%s: chap input <%s id=0x%x len=%d name=",
3847 ifp->if_xname,
3848 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3849 ntohs(h->len));
3850 sppp_print_string((char *) name, name_len);
3851 addlog(" value-size=%d value=", value_len);
3852 sppp_print_bytes(value, value_len);
3853 addlog(">\n");
3854 }
3855
3856 /* Compute reply value. */
3857 MD5Init(&ctx);
3858 MD5Update(&ctx, &h->ident, 1);
3859 MD5Update(&ctx, sp->myauth.secret, sp->myauth.secret_len);
3860 MD5Update(&ctx, value, value_len);
3861 MD5Final(digest, &ctx);
3862 dsize = sizeof digest;
3863
3864 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3865 sizeof dsize, (const char *)&dsize,
3866 sizeof digest, digest,
3867 sp->myauth.name_len,
3868 sp->myauth.name,
3869 0);
3870 break;
3871
3872 case CHAP_SUCCESS:
3873 if (debug) {
3874 log(LOG_DEBUG, "%s: chap success",
3875 ifp->if_xname);
3876 if (len > 4) {
3877 addlog(": ");
3878 sppp_print_string((char *)(h + 1), len - 4);
3879 }
3880 addlog("\n");
3881 }
3882 x = splnet();
3883 sp->pp_auth_failures = 0;
3884 sp->pp_flags &= ~PP_NEEDAUTH;
3885 if (sp->myauth.proto == PPP_CHAP &&
3886 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3887 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3888 /*
3889 * We are authenticator for CHAP but didn't
3890 * complete yet. Leave it to tlu to proceed
3891 * to network phase.
3892 */
3893 splx(x);
3894 break;
3895 }
3896 splx(x);
3897 sppp_phase_network(sp);
3898 break;
3899
3900 case CHAP_FAILURE:
3901 x = splnet();
3902 sp->pp_auth_failures++;
3903 splx(x);
3904 if (debug) {
3905 log(LOG_INFO, "%s: chap failure",
3906 ifp->if_xname);
3907 if (len > 4) {
3908 addlog(": ");
3909 sppp_print_string((char *)(h + 1), len - 4);
3910 }
3911 addlog("\n");
3912 } else
3913 log(LOG_INFO, "%s: chap failure\n",
3914 ifp->if_xname);
3915 /* await LCP shutdown by authenticator */
3916 break;
3917
3918 /* response is my authproto */
3919 case CHAP_RESPONSE:
3920 if (sp->hisauth.secret == NULL) {
3921 /* can't do anything usefull */
3922 printf("%s: chap input without his secret being set\n",
3923 ifp->if_xname);
3924 break;
3925 }
3926 value = 1 + (u_char *)(h + 1);
3927 value_len = value[-1];
3928 name = value + value_len;
3929 name_len = len - value_len - 5;
3930 if (name_len < 0) {
3931 if (debug) {
3932 log(LOG_DEBUG,
3933 "%s: chap corrupted response "
3934 "<%s id=0x%x len=%d",
3935 ifp->if_xname,
3936 sppp_auth_type_name(PPP_CHAP, h->type),
3937 h->ident, ntohs(h->len));
3938 if (len > 4)
3939 sppp_print_bytes((u_char *)(h + 1),
3940 len - 4);
3941 addlog(">\n");
3942 }
3943 break;
3944 }
3945 if (h->ident != sp->confid[IDX_CHAP]) {
3946 if (debug)
3947 log(LOG_DEBUG,
3948 "%s: chap dropping response for old ID "
3949 "(got %d, expected %d)\n",
3950 ifp->if_xname,
3951 h->ident, sp->confid[IDX_CHAP]);
3952 break;
3953 }
3954 if (sp->hisauth.name != NULL &&
3955 (name_len != sp->hisauth.name_len
3956 || memcmp(name, sp->hisauth.name, name_len) != 0)) {
3957 log(LOG_INFO, "%s: chap response, his name ",
3958 ifp->if_xname);
3959 sppp_print_string(name, name_len);
3960 addlog(" != expected ");
3961 sppp_print_string(sp->hisauth.name,
3962 sp->hisauth.name_len);
3963 addlog("\n");
3964 goto chap_failure;
3965 }
3966 if (debug) {
3967 log(LOG_DEBUG, "%s: chap input(%s) "
3968 "<%s id=0x%x len=%d name=",
3969 ifp->if_xname,
3970 sppp_state_name(sp->state[IDX_CHAP]),
3971 sppp_auth_type_name(PPP_CHAP, h->type),
3972 h->ident, ntohs(h->len));
3973 sppp_print_string((char *)name, name_len);
3974 addlog(" value-size=%d value=", value_len);
3975 sppp_print_bytes(value, value_len);
3976 addlog(">\n");
3977 }
3978 if (value_len != sizeof(sp->myauth.challenge)) {
3979 if (debug)
3980 log(LOG_DEBUG,
3981 "%s: chap bad hash value length: "
3982 "%d bytes, should be %ld\n",
3983 ifp->if_xname, value_len,
3984 (long) sizeof(sp->myauth.challenge));
3985 goto chap_failure;
3986 }
3987
3988 MD5Init(&ctx);
3989 MD5Update(&ctx, &h->ident, 1);
3990 MD5Update(&ctx, sp->hisauth.secret, sp->hisauth.secret_len);
3991 MD5Update(&ctx, sp->myauth.challenge, sizeof(sp->myauth.challenge));
3992 MD5Final(digest, &ctx);
3993
3994 #define FAILMSG "Failed..."
3995 #define SUCCMSG "Welcome!"
3996
3997 if (value_len != sizeof digest ||
3998 memcmp(digest, value, value_len) != 0) {
3999 chap_failure:
4000 /* action scn, tld */
4001 x = splnet();
4002 sp->pp_auth_failures++;
4003 splx(x);
4004 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4005 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4006 0);
4007 chap.tld(sp);
4008 break;
4009 }
4010 sp->pp_auth_failures = 0;
4011 /* action sca, perhaps tlu */
4012 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4013 sp->state[IDX_CHAP] == STATE_OPENED)
4014 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4015 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4016 0);
4017 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4018 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4019 chap.tlu(sp);
4020 }
4021 break;
4022
4023 default:
4024 /* Unknown CHAP packet type -- ignore. */
4025 if (debug) {
4026 log(LOG_DEBUG, "%s: chap unknown input(%s) "
4027 "<0x%x id=0x%xh len=%d",
4028 ifp->if_xname,
4029 sppp_state_name(sp->state[IDX_CHAP]),
4030 h->type, h->ident, ntohs(h->len));
4031 if (len > 4)
4032 sppp_print_bytes((u_char *)(h + 1), len - 4);
4033 addlog(">\n");
4034 }
4035 break;
4036
4037 }
4038 }
4039
4040 static void
4041 sppp_chap_init(struct sppp *sp)
4042 {
4043 /* Chap doesn't have STATE_INITIAL at all. */
4044 sp->state[IDX_CHAP] = STATE_CLOSED;
4045 sp->fail_counter[IDX_CHAP] = 0;
4046 sp->pp_seq[IDX_CHAP] = 0;
4047 sp->pp_rseq[IDX_CHAP] = 0;
4048 callout_init(&sp->ch[IDX_CHAP]);
4049 }
4050
4051 static void
4052 sppp_chap_open(struct sppp *sp)
4053 {
4054 if (sp->myauth.proto == PPP_CHAP &&
4055 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4056 /* we are authenticator for CHAP, start it */
4057 chap.scr(sp);
4058 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4059 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4060 }
4061 /* nothing to be done if we are peer, await a challenge */
4062 }
4063
4064 static void
4065 sppp_chap_close(struct sppp *sp)
4066 {
4067 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4068 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4069 }
4070
4071 static void
4072 sppp_chap_TO(void *cookie)
4073 {
4074 struct sppp *sp = (struct sppp *)cookie;
4075 STDDCL;
4076 int s;
4077
4078 s = splnet();
4079 if (debug)
4080 log(LOG_DEBUG, "%s: chap TO(%s) rst_counter = %d\n",
4081 ifp->if_xname,
4082 sppp_state_name(sp->state[IDX_CHAP]),
4083 sp->rst_counter[IDX_CHAP]);
4084
4085 if (--sp->rst_counter[IDX_CHAP] < 0)
4086 /* TO- event */
4087 switch (sp->state[IDX_CHAP]) {
4088 case STATE_REQ_SENT:
4089 chap.tld(sp);
4090 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4091 break;
4092 }
4093 else
4094 /* TO+ (or TO*) event */
4095 switch (sp->state[IDX_CHAP]) {
4096 case STATE_OPENED:
4097 /* TO* event */
4098 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4099 /* fall through */
4100 case STATE_REQ_SENT:
4101 chap.scr(sp);
4102 /* sppp_cp_change_state() will restart the timer */
4103 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4104 break;
4105 }
4106
4107 splx(s);
4108 }
4109
4110 static void
4111 sppp_chap_tlu(struct sppp *sp)
4112 {
4113 STDDCL;
4114 int i, x;
4115
4116 i = 0;
4117 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4118
4119 /*
4120 * Some broken CHAP implementations (Conware CoNet, firmware
4121 * 4.0.?) don't want to re-authenticate their CHAP once the
4122 * initial challenge-response exchange has taken place.
4123 * Provide for an option to avoid rechallenges.
4124 */
4125 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
4126 /*
4127 * Compute the re-challenge timeout. This will yield
4128 * a number between 300 and 810 seconds.
4129 */
4130 i = 300 + ((unsigned)(arc4random() & 0xff00) >> 7);
4131
4132 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, sp);
4133 }
4134
4135 if (debug) {
4136 log(LOG_DEBUG,
4137 "%s: chap %s, ",
4138 ifp->if_xname,
4139 sp->pp_phase == SPPP_PHASE_NETWORK? "reconfirmed": "tlu");
4140 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0)
4141 addlog("next re-challenge in %d seconds\n", i);
4142 else
4143 addlog("re-challenging supressed\n");
4144 }
4145
4146 x = splnet();
4147 sp->pp_auth_failures = 0;
4148 /* indicate to LCP that we need to be closed down */
4149 sp->lcp.protos |= (1 << IDX_CHAP);
4150
4151 if (sp->pp_flags & PP_NEEDAUTH) {
4152 /*
4153 * Remote is authenticator, but his auth proto didn't
4154 * complete yet. Defer the transition to network
4155 * phase.
4156 */
4157 splx(x);
4158 return;
4159 }
4160 splx(x);
4161
4162 /*
4163 * If we are already in phase network, we are done here. This
4164 * is the case if this is a dummy tlu event after a re-challenge.
4165 */
4166 if (sp->pp_phase != SPPP_PHASE_NETWORK)
4167 sppp_phase_network(sp);
4168 }
4169
4170 static void
4171 sppp_chap_tld(struct sppp *sp)
4172 {
4173 STDDCL;
4174
4175 if (debug)
4176 log(LOG_DEBUG, "%s: chap tld\n", ifp->if_xname);
4177 callout_stop(&sp->ch[IDX_CHAP]);
4178 sp->lcp.protos &= ~(1 << IDX_CHAP);
4179
4180 lcp.Close(sp);
4181 }
4182
4183 static void
4184 sppp_chap_scr(struct sppp *sp)
4185 {
4186 u_int32_t *ch;
4187 u_char clen;
4188
4189 if (sp->myauth.name == NULL) {
4190 /* can't do anything usefull */
4191 printf("%s: chap starting without my name being set\n",
4192 sp->pp_if.if_xname);
4193 return;
4194 }
4195
4196 /* Compute random challenge. */
4197 ch = (u_int32_t *)sp->myauth.challenge;
4198 ch[0] = arc4random();
4199 ch[1] = arc4random();
4200 ch[2] = arc4random();
4201 ch[3] = arc4random();
4202 clen = 16; /* 4 * sizeof(u_int32_t) */
4203
4204 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4205
4206 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4207 sizeof clen, (const char *)&clen,
4208 sizeof(sp->myauth.challenge), sp->myauth.challenge,
4209 sp->myauth.name_len,
4210 sp->myauth.name,
4211 0);
4212 }
4213
4214 /*
4215 *--------------------------------------------------------------------------*
4216 * *
4217 * The PAP implementation. *
4218 * *
4219 *--------------------------------------------------------------------------*
4220 */
4221 /*
4222 * For PAP, we need to keep a little state also if we are the peer, not the
4223 * authenticator. This is since we don't get a request to authenticate, but
4224 * have to repeatedly authenticate ourself until we got a response (or the
4225 * retry counter is expired).
4226 */
4227
4228 /*
4229 * Handle incoming PAP packets. */
4230 static void
4231 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4232 {
4233 STDDCL;
4234 struct lcp_header *h;
4235 int len, x;
4236 u_char mlen;
4237 char *name, *secret;
4238 int name_len, secret_len;
4239
4240 len = m->m_pkthdr.len;
4241 if (len < 5) {
4242 if (debug)
4243 log(LOG_DEBUG,
4244 "%s: pap invalid packet length: %d bytes\n",
4245 ifp->if_xname, len);
4246 return;
4247 }
4248 h = mtod(m, struct lcp_header *);
4249 if (len > ntohs(h->len))
4250 len = ntohs(h->len);
4251 switch (h->type) {
4252 /* PAP request is my authproto */
4253 case PAP_REQ:
4254 if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
4255 /* can't do anything usefull */
4256 printf("%s: pap request without his name and his secret being set\n",
4257 ifp->if_xname);
4258 break;
4259 }
4260 name = 1 + (u_char *)(h + 1);
4261 name_len = name[-1];
4262 secret = name + name_len + 1;
4263 if (name_len > len - 6 ||
4264 (secret_len = secret[-1]) > len - 6 - name_len) {
4265 if (debug) {
4266 log(LOG_DEBUG, "%s: pap corrupted input "
4267 "<%s id=0x%x len=%d",
4268 ifp->if_xname,
4269 sppp_auth_type_name(PPP_PAP, h->type),
4270 h->ident, ntohs(h->len));
4271 if (len > 4)
4272 sppp_print_bytes((u_char *)(h + 1),
4273 len - 4);
4274 addlog(">\n");
4275 }
4276 break;
4277 }
4278 if (debug) {
4279 log(LOG_DEBUG, "%s: pap input(%s) "
4280 "<%s id=0x%x len=%d name=",
4281 ifp->if_xname,
4282 sppp_state_name(sp->state[IDX_PAP]),
4283 sppp_auth_type_name(PPP_PAP, h->type),
4284 h->ident, ntohs(h->len));
4285 sppp_print_string((char *)name, name_len);
4286 addlog(" secret=");
4287 sppp_print_string((char *)secret, secret_len);
4288 addlog(">\n");
4289 }
4290 if (name_len != sp->hisauth.name_len ||
4291 secret_len != sp->hisauth.secret_len ||
4292 memcmp(name, sp->hisauth.name, name_len) != 0 ||
4293 memcmp(secret, sp->hisauth.secret, secret_len) != 0) {
4294 /* action scn, tld */
4295 sp->pp_auth_failures++;
4296 mlen = sizeof(FAILMSG) - 1;
4297 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4298 sizeof mlen, (const char *)&mlen,
4299 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4300 0);
4301 pap.tld(sp);
4302 break;
4303 }
4304 /* action sca, perhaps tlu */
4305 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4306 sp->state[IDX_PAP] == STATE_OPENED) {
4307 mlen = sizeof(SUCCMSG) - 1;
4308 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4309 sizeof mlen, (const char *)&mlen,
4310 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4311 0);
4312 }
4313 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4314 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4315 pap.tlu(sp);
4316 }
4317 break;
4318
4319 /* ack and nak are his authproto */
4320 case PAP_ACK:
4321 callout_stop(&sp->pap_my_to_ch);
4322 if (debug) {
4323 log(LOG_DEBUG, "%s: pap success",
4324 ifp->if_xname);
4325 name_len = *(char *)h;
4326 if (len > 5 && name_len) {
4327 addlog(": ");
4328 sppp_print_string((char *)(h + 1), name_len);
4329 }
4330 addlog("\n");
4331 }
4332 x = splnet();
4333 sp->pp_auth_failures = 0;
4334 sp->pp_flags &= ~PP_NEEDAUTH;
4335 if (sp->myauth.proto == PPP_PAP &&
4336 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4337 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4338 /*
4339 * We are authenticator for PAP but didn't
4340 * complete yet. Leave it to tlu to proceed
4341 * to network phase.
4342 */
4343 splx(x);
4344 break;
4345 }
4346 splx(x);
4347 sppp_phase_network(sp);
4348 break;
4349
4350 case PAP_NAK:
4351 callout_stop(&sp->pap_my_to_ch);
4352 sp->pp_auth_failures++;
4353 if (debug) {
4354 log(LOG_INFO, "%s: pap failure",
4355 ifp->if_xname);
4356 name_len = *(char *)h;
4357 if (len > 5 && name_len) {
4358 addlog(": ");
4359 sppp_print_string((char *)(h + 1), name_len);
4360 }
4361 addlog("\n");
4362 } else
4363 log(LOG_INFO, "%s: pap failure\n",
4364 ifp->if_xname);
4365 /* await LCP shutdown by authenticator */
4366 break;
4367
4368 default:
4369 /* Unknown PAP packet type -- ignore. */
4370 if (debug) {
4371 log(LOG_DEBUG, "%s: pap corrupted input "
4372 "<0x%x id=0x%x len=%d",
4373 ifp->if_xname,
4374 h->type, h->ident, ntohs(h->len));
4375 if (len > 4)
4376 sppp_print_bytes((u_char *)(h + 1), len - 4);
4377 addlog(">\n");
4378 }
4379 break;
4380
4381 }
4382 }
4383
4384 static void
4385 sppp_pap_init(struct sppp *sp)
4386 {
4387 /* PAP doesn't have STATE_INITIAL at all. */
4388 sp->state[IDX_PAP] = STATE_CLOSED;
4389 sp->fail_counter[IDX_PAP] = 0;
4390 sp->pp_seq[IDX_PAP] = 0;
4391 sp->pp_rseq[IDX_PAP] = 0;
4392 callout_init(&sp->ch[IDX_PAP]);
4393 callout_init(&sp->pap_my_to_ch);
4394 }
4395
4396 static void
4397 sppp_pap_open(struct sppp *sp)
4398 {
4399 if (sp->hisauth.proto == PPP_PAP &&
4400 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4401 /* we are authenticator for PAP, start our timer */
4402 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4403 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4404 }
4405 if (sp->myauth.proto == PPP_PAP) {
4406 /* we are peer, send a request, and start a timer */
4407 pap.scr(sp);
4408 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4409 sppp_pap_my_TO, sp);
4410 }
4411 }
4412
4413 static void
4414 sppp_pap_close(struct sppp *sp)
4415 {
4416 if (sp->state[IDX_PAP] != STATE_CLOSED)
4417 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4418 }
4419
4420 /*
4421 * That's the timeout routine if we are authenticator. Since the
4422 * authenticator is basically passive in PAP, we can't do much here.
4423 */
4424 static void
4425 sppp_pap_TO(void *cookie)
4426 {
4427 struct sppp *sp = (struct sppp *)cookie;
4428 STDDCL;
4429 int s;
4430
4431 s = splnet();
4432 if (debug)
4433 log(LOG_DEBUG, "%s: pap TO(%s) rst_counter = %d\n",
4434 ifp->if_xname,
4435 sppp_state_name(sp->state[IDX_PAP]),
4436 sp->rst_counter[IDX_PAP]);
4437
4438 if (--sp->rst_counter[IDX_PAP] < 0)
4439 /* TO- event */
4440 switch (sp->state[IDX_PAP]) {
4441 case STATE_REQ_SENT:
4442 pap.tld(sp);
4443 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4444 break;
4445 }
4446 else
4447 /* TO+ event, not very much we could do */
4448 switch (sp->state[IDX_PAP]) {
4449 case STATE_REQ_SENT:
4450 /* sppp_cp_change_state() will restart the timer */
4451 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4452 break;
4453 }
4454
4455 splx(s);
4456 }
4457
4458 /*
4459 * That's the timeout handler if we are peer. Since the peer is active,
4460 * we need to retransmit our PAP request since it is apparently lost.
4461 * XXX We should impose a max counter.
4462 */
4463 static void
4464 sppp_pap_my_TO(void *cookie)
4465 {
4466 struct sppp *sp = (struct sppp *)cookie;
4467 STDDCL;
4468
4469 if (debug)
4470 log(LOG_DEBUG, "%s: pap peer TO\n",
4471 ifp->if_xname);
4472
4473 pap.scr(sp);
4474 }
4475
4476 static void
4477 sppp_pap_tlu(struct sppp *sp)
4478 {
4479 STDDCL;
4480 int x;
4481
4482 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4483
4484 if (debug)
4485 log(LOG_DEBUG, "%s: %s tlu\n",
4486 ifp->if_xname, pap.name);
4487
4488 x = splnet();
4489 sp->pp_auth_failures = 0;
4490 /* indicate to LCP that we need to be closed down */
4491 sp->lcp.protos |= (1 << IDX_PAP);
4492
4493 if (sp->pp_flags & PP_NEEDAUTH) {
4494 /*
4495 * Remote is authenticator, but his auth proto didn't
4496 * complete yet. Defer the transition to network
4497 * phase.
4498 */
4499 splx(x);
4500 return;
4501 }
4502 splx(x);
4503 sppp_phase_network(sp);
4504 }
4505
4506 static void
4507 sppp_pap_tld(struct sppp *sp)
4508 {
4509 STDDCL;
4510
4511 if (debug)
4512 log(LOG_DEBUG, "%s: pap tld\n", ifp->if_xname);
4513 callout_stop(&sp->ch[IDX_PAP]);
4514 callout_stop(&sp->pap_my_to_ch);
4515 sp->lcp.protos &= ~(1 << IDX_PAP);
4516
4517 lcp.Close(sp);
4518 }
4519
4520 static void
4521 sppp_pap_scr(struct sppp *sp)
4522 {
4523 u_char idlen, pwdlen;
4524
4525 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4526 /* can't do anything usefull */
4527 printf("%s: pap starting without my name and secret being set\n",
4528 sp->pp_if.if_xname);
4529 return;
4530 }
4531
4532 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4533 pwdlen = sp->myauth.secret_len;
4534 idlen = sp->myauth.name_len;
4535
4536 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4537 sizeof idlen, (const char *)&idlen,
4538 idlen, sp->myauth.name,
4539 sizeof pwdlen, (const char *)&pwdlen,
4540 pwdlen, sp->myauth.secret,
4541 0);
4542 }
4543
4544 /*
4545 * Random miscellaneous functions.
4546 */
4547
4548 /*
4549 * Send a PAP or CHAP proto packet.
4550 *
4551 * Varadic function, each of the elements for the ellipsis is of type
4552 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4553 * mlen == 0.
4554 * NOTE: never declare variadic functions with types subject to type
4555 * promotion (i.e. u_char). This is asking for big trouble depending
4556 * on the architecture you are on...
4557 */
4558
4559 static void
4560 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4561 unsigned int type, unsigned int id,
4562 ...)
4563 {
4564 STDDCL;
4565 struct lcp_header *lh;
4566 struct mbuf *m;
4567 u_char *p;
4568 int len;
4569 size_t pkthdrlen;
4570 unsigned int mlen;
4571 const char *msg;
4572 va_list ap;
4573
4574 MGETHDR(m, M_DONTWAIT, MT_DATA);
4575 if (! m)
4576 return;
4577 m->m_pkthdr.rcvif = 0;
4578
4579 if (sp->pp_flags & PP_NOFRAMING) {
4580 *mtod(m, u_int16_t *) = htons(cp->proto);
4581 pkthdrlen = 2;
4582 lh = (struct lcp_header *)(mtod(m, u_int8_t *)+2);
4583 } else {
4584 struct ppp_header *h;
4585 h = mtod(m, struct ppp_header *);
4586 h->address = PPP_ALLSTATIONS; /* broadcast address */
4587 h->control = PPP_UI; /* Unnumbered Info */
4588 h->protocol = htons(cp->proto);
4589 pkthdrlen = PPP_HEADER_LEN;
4590
4591 lh = (struct lcp_header *)(h + 1);
4592 }
4593
4594 lh->type = type;
4595 lh->ident = id;
4596 p = (u_char *)(lh + 1);
4597
4598 va_start(ap, id);
4599 len = 0;
4600
4601 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4602 msg = va_arg(ap, const char *);
4603 len += mlen;
4604 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
4605 va_end(ap);
4606 m_freem(m);
4607 return;
4608 }
4609
4610 bcopy(msg, p, mlen);
4611 p += mlen;
4612 }
4613 va_end(ap);
4614
4615 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
4616 lh->len = htons(LCP_HEADER_LEN + len);
4617
4618 if (debug) {
4619 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
4620 ifp->if_xname, cp->name,
4621 sppp_auth_type_name(cp->proto, lh->type),
4622 lh->ident, ntohs(lh->len));
4623 if (len)
4624 sppp_print_bytes((u_char *)(lh + 1), len);
4625 addlog(">\n");
4626 }
4627 if (IF_QFULL(&sp->pp_cpq)) {
4628 IF_DROP(&sp->pp_fastq);
4629 IF_DROP(&ifp->if_snd);
4630 m_freem(m);
4631 ++ifp->if_oerrors;
4632 return;
4633 } else
4634 IF_ENQUEUE(&sp->pp_cpq, m);
4635 if (! (ifp->if_flags & IFF_OACTIVE))
4636 (*ifp->if_start)(ifp);
4637 ifp->if_obytes += m->m_pkthdr.len + 3;
4638 }
4639
4640 /*
4641 * Send keepalive packets, every 10 seconds.
4642 */
4643 static void
4644 sppp_keepalive(void *dummy)
4645 {
4646 struct sppp *sp;
4647 int s;
4648 time_t now;
4649
4650 s = splnet();
4651 now = time_uptime;
4652 for (sp=spppq; sp; sp=sp->pp_next) {
4653 struct ifnet *ifp = &sp->pp_if;
4654
4655 /* check idle timeout */
4656 if ((sp->pp_idle_timeout != 0) && (ifp->if_flags & IFF_RUNNING)
4657 && (sp->pp_phase == SPPP_PHASE_NETWORK)) {
4658 /* idle timeout is enabled for this interface */
4659 if ((now-sp->pp_last_activity) >= sp->pp_idle_timeout) {
4660 if (ifp->if_flags & IFF_DEBUG)
4661 printf("%s: no activity for %lu seconds\n",
4662 sp->pp_if.if_xname,
4663 (unsigned long)(now-sp->pp_last_activity));
4664 lcp.Close(sp);
4665 continue;
4666 }
4667 }
4668
4669 /* Keepalive mode disabled or channel down? */
4670 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4671 ! (ifp->if_flags & IFF_RUNNING))
4672 continue;
4673
4674 /* No keepalive in PPP mode if LCP not opened yet. */
4675 if (! (sp->pp_flags & PP_CISCO) &&
4676 sp->pp_phase < SPPP_PHASE_AUTHENTICATE)
4677 continue;
4678
4679 /* No echo reply, but maybe user data passed through? */
4680 if ((now - sp->pp_last_receive) < sp->pp_max_noreceive) {
4681 sp->pp_alivecnt = 0;
4682 continue;
4683 }
4684
4685 if (sp->pp_alivecnt >= sp->pp_maxalive) {
4686 /* No keepalive packets got. Stop the interface. */
4687 if_down (ifp);
4688 IF_PURGE(&sp->pp_cpq);
4689 if (! (sp->pp_flags & PP_CISCO)) {
4690 printf("%s: LCP keepalive timed out, going to restart the connection\n",
4691 ifp->if_xname);
4692 sp->pp_alivecnt = 0;
4693
4694 /* we are down, close all open protocols */
4695 lcp.Close(sp);
4696
4697 /* And now prepare LCP to reestablish the link, if configured to do so. */
4698 sppp_cp_change_state(&lcp, sp, STATE_STOPPED);
4699
4700 /* Close connection imediatly, completition of this
4701 * will summon the magic needed to reestablish it. */
4702 if (sp->pp_tlf)
4703 sp->pp_tlf(sp);
4704 continue;
4705 }
4706 }
4707 if (sp->pp_alivecnt < sp->pp_maxalive)
4708 ++sp->pp_alivecnt;
4709 if (sp->pp_flags & PP_CISCO)
4710 sppp_cisco_send(sp, CISCO_KEEPALIVE_REQ,
4711 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4712 else if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
4713 int32_t nmagic = htonl(sp->lcp.magic);
4714 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4715 sppp_cp_send(sp, PPP_LCP, ECHO_REQ,
4716 sp->lcp.echoid, 4, &nmagic);
4717 }
4718 }
4719 splx(s);
4720 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
4721 }
4722
4723 #ifdef INET
4724 /*
4725 * Get both IP addresses.
4726 */
4727 static void
4728 sppp_get_ip_addrs(struct sppp *sp, u_int32_t *src, u_int32_t *dst, u_int32_t *srcmask)
4729 {
4730 struct ifnet *ifp = &sp->pp_if;
4731 struct ifaddr *ifa;
4732 struct sockaddr_in *si, *sm;
4733 u_int32_t ssrc, ddst;
4734
4735 sm = NULL;
4736 ssrc = ddst = 0;
4737 /*
4738 * Pick the first AF_INET address from the list,
4739 * aliases don't make any sense on a p2p link anyway.
4740 */
4741 si = 0;
4742 IFADDR_FOREACH(ifa, ifp) {
4743 if (ifa->ifa_addr->sa_family == AF_INET) {
4744 si = (struct sockaddr_in *)ifa->ifa_addr;
4745 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4746 if (si)
4747 break;
4748 }
4749 }
4750 if (ifa) {
4751 if (si && si->sin_addr.s_addr) {
4752 ssrc = si->sin_addr.s_addr;
4753 if (srcmask)
4754 *srcmask = ntohl(sm->sin_addr.s_addr);
4755 }
4756
4757 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4758 if (si && si->sin_addr.s_addr)
4759 ddst = si->sin_addr.s_addr;
4760 }
4761
4762 if (dst) *dst = ntohl(ddst);
4763 if (src) *src = ntohl(ssrc);
4764 }
4765
4766 /*
4767 * Set IP addresses. Must be called at splnet.
4768 * If an address is 0, leave it the way it is.
4769 */
4770 static void
4771 sppp_set_ip_addrs(struct sppp *sp, u_int32_t myaddr, u_int32_t hisaddr)
4772 {
4773 STDDCL;
4774 struct ifaddr *ifa;
4775 struct sockaddr_in *si, *dest;
4776
4777 /*
4778 * Pick the first AF_INET address from the list,
4779 * aliases don't make any sense on a p2p link anyway.
4780 */
4781
4782 IFADDR_FOREACH(ifa, ifp) {
4783 if (ifa->ifa_addr->sa_family == AF_INET) {
4784 si = (struct sockaddr_in *)ifa->ifa_addr;
4785 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4786 goto found;
4787 }
4788 }
4789 return;
4790
4791 found:
4792 {
4793 int error;
4794 struct sockaddr_in new_sin = *si;
4795 struct sockaddr_in new_dst = *dest;
4796
4797 /*
4798 * Scrub old routes now instead of calling in_ifinit with
4799 * scrub=1, because we may change the dstaddr
4800 * before the call to in_ifinit.
4801 */
4802 in_ifscrub(ifp, ifatoia(ifa));
4803
4804 if (myaddr != 0)
4805 new_sin.sin_addr.s_addr = htonl(myaddr);
4806 if (hisaddr != 0) {
4807 new_dst.sin_addr.s_addr = htonl(hisaddr);
4808 if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr) {
4809 sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
4810 *dest = new_dst; /* fix dstaddr in place */
4811 }
4812 }
4813 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4814 if (debug && error)
4815 {
4816 log(LOG_DEBUG, "%s: sppp_set_ip_addrs: in_ifinit "
4817 " failed, error=%d\n", ifp->if_xname, error);
4818 }
4819 #ifdef PFIL_HOOKS
4820 if (!error)
4821 (void)pfil_run_hooks(&if_pfil,
4822 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
4823 #endif
4824 }
4825 }
4826
4827 /*
4828 * Clear IP addresses. Must be called at splnet.
4829 */
4830 static void
4831 sppp_clear_ip_addrs(struct sppp *sp)
4832 {
4833 struct ifnet *ifp = &sp->pp_if;
4834 struct ifaddr *ifa;
4835 struct sockaddr_in *si, *dest;
4836
4837 u_int32_t remote;
4838 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4839 remote = sp->ipcp.saved_hisaddr;
4840 else
4841 sppp_get_ip_addrs(sp, 0, &remote, 0);
4842
4843 /*
4844 * Pick the first AF_INET address from the list,
4845 * aliases don't make any sense on a p2p link anyway.
4846 */
4847
4848 IFADDR_FOREACH(ifa, ifp) {
4849 if (ifa->ifa_addr->sa_family == AF_INET) {
4850 si = (struct sockaddr_in *)ifa->ifa_addr;
4851 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4852 goto found;
4853 }
4854 }
4855 return;
4856
4857 found:
4858 {
4859 struct sockaddr_in new_sin = *si;
4860
4861 in_ifscrub(ifp, ifatoia(ifa));
4862 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
4863 new_sin.sin_addr.s_addr = 0;
4864 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4865 /* replace peer addr in place */
4866 dest->sin_addr.s_addr = sp->ipcp.saved_hisaddr;
4867 in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4868 #ifdef PFIL_HOOKS
4869 (void)pfil_run_hooks(&if_pfil,
4870 (struct mbuf **)SIOCDIFADDR, ifp, PFIL_IFADDR);
4871 #endif
4872 }
4873 }
4874 #endif
4875
4876 #ifdef INET6
4877 /*
4878 * Get both IPv6 addresses.
4879 */
4880 static void
4881 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4882 struct in6_addr *srcmask)
4883 {
4884 struct ifnet *ifp = &sp->pp_if;
4885 struct ifaddr *ifa;
4886 struct sockaddr_in6 *si, *sm;
4887 struct in6_addr ssrc, ddst;
4888
4889 sm = NULL;
4890 memset(&ssrc, 0, sizeof(ssrc));
4891 memset(&ddst, 0, sizeof(ddst));
4892 /*
4893 * Pick the first link-local AF_INET6 address from the list,
4894 * aliases don't make any sense on a p2p link anyway.
4895 */
4896 si = 0;
4897 IFADDR_FOREACH(ifa, ifp)
4898 if (ifa->ifa_addr->sa_family == AF_INET6) {
4899 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4900 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4901 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4902 break;
4903 }
4904 if (ifa) {
4905 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4906 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4907 if (srcmask) {
4908 bcopy(&sm->sin6_addr, srcmask,
4909 sizeof(*srcmask));
4910 }
4911 }
4912
4913 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4914 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4915 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4916 }
4917
4918 if (dst)
4919 bcopy(&ddst, dst, sizeof(*dst));
4920 if (src)
4921 bcopy(&ssrc, src, sizeof(*src));
4922 }
4923
4924 #ifdef IPV6CP_MYIFID_DYN
4925 /*
4926 * Generate random ifid.
4927 */
4928 static void
4929 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4930 {
4931 /* TBD */
4932 }
4933
4934 /*
4935 * Set my IPv6 address. Must be called at splnet.
4936 */
4937 static void
4938 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4939 {
4940 STDDCL;
4941 struct ifaddr *ifa;
4942 struct sockaddr_in6 *sin6;
4943
4944 /*
4945 * Pick the first link-local AF_INET6 address from the list,
4946 * aliases don't make any sense on a p2p link anyway.
4947 */
4948
4949 sin6 = NULL;
4950 IFADDR_FOREACH(ifa, ifp)
4951 {
4952 if (ifa->ifa_addr->sa_family == AF_INET6)
4953 {
4954 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4955 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4956 break;
4957 }
4958 }
4959
4960 if (ifa && sin6)
4961 {
4962 int error;
4963 struct sockaddr_in6 new_sin6 = *sin6;
4964
4965 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4966 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4967 if (debug && error)
4968 {
4969 log(LOG_DEBUG, "%s: sppp_set_ip6_addr: in6_ifinit "
4970 " failed, error=%d\n", ifp->if_xname, error);
4971 }
4972 #ifdef PFIL_HOOKS
4973 if (!error)
4974 (void)pfil_run_hooks(&if_pfil,
4975 (struct mbuf **)SIOCAIFADDR_IN6, ifp, PFIL_IFADDR);
4976 #endif
4977 }
4978 }
4979 #endif
4980
4981 /*
4982 * Suggest a candidate address to be used by peer.
4983 */
4984 static void
4985 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4986 {
4987 struct in6_addr myaddr;
4988 struct timeval tv;
4989
4990 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4991
4992 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4993 microtime(&tv);
4994 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4995 myaddr.s6_addr[14] ^= 0xff;
4996 myaddr.s6_addr[15] ^= 0xff;
4997 } else {
4998 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4999 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5000 }
5001 if (suggest)
5002 bcopy(&myaddr, suggest, sizeof(myaddr));
5003 }
5004 #endif /*INET6*/
5005
5006 /*
5007 * Process ioctl requests specific to the PPP interface.
5008 * Permissions have already been checked.
5009 */
5010 static int
5011 sppp_params(struct sppp *sp, int cmd, void *data)
5012 {
5013 switch (cmd) {
5014 case SPPPGETAUTHCFG:
5015 {
5016 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5017 int error;
5018 size_t len;
5019
5020 cfg->myauthflags = sp->myauth.flags;
5021 cfg->hisauthflags = sp->hisauth.flags;
5022 strncpy(cfg->ifname, sp->pp_if.if_xname, IFNAMSIZ);
5023 cfg->hisauth = 0;
5024 if (sp->hisauth.proto)
5025 cfg->hisauth = (sp->hisauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5026 cfg->myauth = 0;
5027 if (sp->myauth.proto)
5028 cfg->myauth = (sp->myauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5029 if (cfg->myname_length == 0) {
5030 if (sp->myauth.name != NULL)
5031 cfg->myname_length = sp->myauth.name_len + 1;
5032 } else {
5033 if (sp->myauth.name == NULL) {
5034 cfg->myname_length = 0;
5035 } else {
5036 len = sp->myauth.name_len + 1;
5037 if (cfg->myname_length < len)
5038 return (ENAMETOOLONG);
5039 error = copyout(sp->myauth.name, cfg->myname, len);
5040 if (error) return error;
5041 }
5042 }
5043 if (cfg->hisname_length == 0) {
5044 if (sp->hisauth.name != NULL)
5045 cfg->hisname_length = sp->hisauth.name_len + 1;
5046 } else {
5047 if (sp->hisauth.name == NULL) {
5048 cfg->hisname_length = 0;
5049 } else {
5050 len = sp->hisauth.name_len + 1;
5051 if (cfg->hisname_length < len)
5052 return (ENAMETOOLONG);
5053 error = copyout(sp->hisauth.name, cfg->hisname, len);
5054 if (error) return error;
5055 }
5056 }
5057 }
5058 break;
5059 case SPPPSETAUTHCFG:
5060 {
5061 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5062 int error;
5063
5064 if (sp->myauth.name) {
5065 free(sp->myauth.name, M_DEVBUF);
5066 sp->myauth.name = NULL;
5067 }
5068 if (sp->myauth.secret) {
5069 free(sp->myauth.secret, M_DEVBUF);
5070 sp->myauth.secret = NULL;
5071 }
5072 if (sp->hisauth.name) {
5073 free(sp->hisauth.name, M_DEVBUF);
5074 sp->hisauth.name = NULL;
5075 }
5076 if (sp->hisauth.secret) {
5077 free(sp->hisauth.secret, M_DEVBUF);
5078 sp->hisauth.secret = NULL;
5079 }
5080
5081 if (cfg->hisname != NULL && cfg->hisname_length > 0) {
5082 if (cfg->hisname_length >= MCLBYTES)
5083 return (ENAMETOOLONG);
5084 sp->hisauth.name = malloc(cfg->hisname_length, M_DEVBUF, M_WAITOK);
5085 error = copyin(cfg->hisname, sp->hisauth.name, cfg->hisname_length);
5086 if (error) {
5087 free(sp->hisauth.name, M_DEVBUF);
5088 sp->hisauth.name = NULL;
5089 return error;
5090 }
5091 sp->hisauth.name_len = cfg->hisname_length - 1;
5092 sp->hisauth.name[sp->hisauth.name_len] = 0;
5093 }
5094 if (cfg->hissecret != NULL && cfg->hissecret_length > 0) {
5095 if (cfg->hissecret_length >= MCLBYTES)
5096 return (ENAMETOOLONG);
5097 sp->hisauth.secret = malloc(cfg->hissecret_length, M_DEVBUF, M_WAITOK);
5098 error = copyin(cfg->hissecret, sp->hisauth.secret, cfg->hissecret_length);
5099 if (error) {
5100 free(sp->hisauth.secret, M_DEVBUF);
5101 sp->hisauth.secret = NULL;
5102 return error;
5103 }
5104 sp->hisauth.secret_len = cfg->hissecret_length - 1;
5105 sp->hisauth.secret[sp->hisauth.secret_len] = 0;
5106 }
5107 if (cfg->myname != NULL && cfg->myname_length > 0) {
5108 if (cfg->myname_length >= MCLBYTES)
5109 return (ENAMETOOLONG);
5110 sp->myauth.name = malloc(cfg->myname_length, M_DEVBUF, M_WAITOK);
5111 error = copyin(cfg->myname, sp->myauth.name, cfg->myname_length);
5112 if (error) {
5113 free(sp->myauth.name, M_DEVBUF);
5114 sp->myauth.name = NULL;
5115 return error;
5116 }
5117 sp->myauth.name_len = cfg->myname_length - 1;
5118 sp->myauth.name[sp->myauth.name_len] = 0;
5119 }
5120 if (cfg->mysecret != NULL && cfg->mysecret_length > 0) {
5121 if (cfg->mysecret_length >= MCLBYTES)
5122 return (ENAMETOOLONG);
5123 sp->myauth.secret = malloc(cfg->mysecret_length, M_DEVBUF, M_WAITOK);
5124 error = copyin(cfg->mysecret, sp->myauth.secret, cfg->mysecret_length);
5125 if (error) {
5126 free(sp->myauth.secret, M_DEVBUF);
5127 sp->myauth.secret = NULL;
5128 return error;
5129 }
5130 sp->myauth.secret_len = cfg->mysecret_length - 1;
5131 sp->myauth.secret[sp->myauth.secret_len] = 0;
5132 }
5133 sp->myauth.flags = cfg->myauthflags;
5134 if (cfg->myauth)
5135 sp->myauth.proto = (cfg->myauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5136 sp->hisauth.flags = cfg->hisauthflags;
5137 if (cfg->hisauth)
5138 sp->hisauth.proto = (cfg->hisauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5139 sp->pp_auth_failures = 0;
5140 if (sp->hisauth.proto != 0)
5141 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
5142 else
5143 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
5144 }
5145 break;
5146 case SPPPGETLCPCFG:
5147 {
5148 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5149 lcpp->lcp_timeout = sp->lcp.timeout;
5150 }
5151 break;
5152 case SPPPSETLCPCFG:
5153 {
5154 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5155 sp->lcp.timeout = lcpp->lcp_timeout;
5156 }
5157 break;
5158 case SPPPGETSTATUS:
5159 {
5160 struct spppstatus *status = (struct spppstatus *)data;
5161 status->phase = sp->pp_phase;
5162 }
5163 break;
5164 case SPPPGETSTATUSNCP:
5165 {
5166 struct spppstatusncp *status = (struct spppstatusncp *)data;
5167 status->phase = sp->pp_phase;
5168 status->ncpup = sppp_ncp_check(sp);
5169 }
5170 break;
5171 case SPPPGETIDLETO:
5172 {
5173 struct spppidletimeout *to = (struct spppidletimeout *)data;
5174 to->idle_seconds = sp->pp_idle_timeout;
5175 }
5176 break;
5177 case SPPPSETIDLETO:
5178 {
5179 struct spppidletimeout *to = (struct spppidletimeout *)data;
5180 sp->pp_idle_timeout = to->idle_seconds;
5181 }
5182 break;
5183 case SPPPSETAUTHFAILURE:
5184 {
5185 struct spppauthfailuresettings *afsettings = (struct spppauthfailuresettings *)data;
5186 sp->pp_max_auth_fail = afsettings->max_failures;
5187 sp->pp_auth_failures = 0;
5188 }
5189 break;
5190 case SPPPGETAUTHFAILURES:
5191 {
5192 struct spppauthfailurestats *stats = (struct spppauthfailurestats *)data;
5193 stats->auth_failures = sp->pp_auth_failures;
5194 stats->max_failures = sp->pp_max_auth_fail;
5195 }
5196 break;
5197 case SPPPSETDNSOPTS:
5198 {
5199 struct spppdnssettings *req = (struct spppdnssettings *)data;
5200 sp->query_dns = req->query_dns & 3;
5201 }
5202 break;
5203 case SPPPGETDNSOPTS:
5204 {
5205 struct spppdnssettings *req = (struct spppdnssettings *)data;
5206 req->query_dns = sp->query_dns;
5207 }
5208 break;
5209 case SPPPGETDNSADDRS:
5210 {
5211 struct spppdnsaddrs *addrs = (struct spppdnsaddrs *)data;
5212 memcpy(&addrs->dns, &sp->dns_addrs, sizeof addrs->dns);
5213 }
5214 break;
5215 case SPPPGETKEEPALIVE:
5216 {
5217 struct spppkeepalivesettings *settings =
5218 (struct spppkeepalivesettings*)data;
5219 settings->maxalive = sp->pp_maxalive;
5220 settings->max_noreceive = sp->pp_max_noreceive;
5221 }
5222 break;
5223 case SPPPSETKEEPALIVE:
5224 {
5225 struct spppkeepalivesettings *settings =
5226 (struct spppkeepalivesettings*)data;
5227 sp->pp_maxalive = settings->maxalive;
5228 sp->pp_max_noreceive = settings->max_noreceive;
5229 }
5230 break;
5231 default:
5232 return (EINVAL);
5233 }
5234
5235 return (0);
5236 }
5237
5238 static void
5239 sppp_phase_network(struct sppp *sp)
5240 {
5241 STDDCL;
5242 int i;
5243 u_int32_t mask;
5244
5245 sp->pp_phase = SPPP_PHASE_NETWORK;
5246
5247 if (debug)
5248 {
5249 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
5250 sppp_phase_name(sp->pp_phase));
5251 }
5252
5253 /* Notify NCPs now. */
5254 for (i = 0; i < IDX_COUNT; i++)
5255 if ((cps[i])->flags & CP_NCP)
5256 (cps[i])->Open(sp);
5257
5258 /* Send Up events to all NCPs. */
5259 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5260 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5261 (cps[i])->Up(sp);
5262
5263 /* if no NCP is starting, all this was in vain, close down */
5264 sppp_lcp_check_and_close(sp);
5265 }
5266
5267
5268 static const char *
5269 sppp_cp_type_name(u_char type)
5270 {
5271 static char buf[12];
5272 switch (type) {
5273 case CONF_REQ: return "conf-req";
5274 case CONF_ACK: return "conf-ack";
5275 case CONF_NAK: return "conf-nak";
5276 case CONF_REJ: return "conf-rej";
5277 case TERM_REQ: return "term-req";
5278 case TERM_ACK: return "term-ack";
5279 case CODE_REJ: return "code-rej";
5280 case PROTO_REJ: return "proto-rej";
5281 case ECHO_REQ: return "echo-req";
5282 case ECHO_REPLY: return "echo-reply";
5283 case DISC_REQ: return "discard-req";
5284 }
5285 snprintf(buf, sizeof(buf), "0x%x", type);
5286 return buf;
5287 }
5288
5289 static const char *
5290 sppp_auth_type_name(u_short proto, u_char type)
5291 {
5292 static char buf[12];
5293 switch (proto) {
5294 case PPP_CHAP:
5295 switch (type) {
5296 case CHAP_CHALLENGE: return "challenge";
5297 case CHAP_RESPONSE: return "response";
5298 case CHAP_SUCCESS: return "success";
5299 case CHAP_FAILURE: return "failure";
5300 }
5301 case PPP_PAP:
5302 switch (type) {
5303 case PAP_REQ: return "req";
5304 case PAP_ACK: return "ack";
5305 case PAP_NAK: return "nak";
5306 }
5307 }
5308 snprintf(buf, sizeof(buf), "0x%x", type);
5309 return buf;
5310 }
5311
5312 static const char *
5313 sppp_lcp_opt_name(u_char opt)
5314 {
5315 static char buf[12];
5316 switch (opt) {
5317 case LCP_OPT_MRU: return "mru";
5318 case LCP_OPT_ASYNC_MAP: return "async-map";
5319 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5320 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5321 case LCP_OPT_MAGIC: return "magic";
5322 case LCP_OPT_PROTO_COMP: return "proto-comp";
5323 case LCP_OPT_ADDR_COMP: return "addr-comp";
5324 }
5325 snprintf(buf, sizeof(buf), "0x%x", opt);
5326 return buf;
5327 }
5328
5329 static const char *
5330 sppp_ipcp_opt_name(u_char opt)
5331 {
5332 static char buf[12];
5333 switch (opt) {
5334 case IPCP_OPT_ADDRESSES: return "addresses";
5335 case IPCP_OPT_COMPRESSION: return "compression";
5336 case IPCP_OPT_ADDRESS: return "address";
5337 }
5338 snprintf(buf, sizeof(buf), "0x%x", opt);
5339 return buf;
5340 }
5341
5342 #ifdef INET6
5343 static const char *
5344 sppp_ipv6cp_opt_name(u_char opt)
5345 {
5346 static char buf[12];
5347 switch (opt) {
5348 case IPV6CP_OPT_IFID: return "ifid";
5349 case IPV6CP_OPT_COMPRESSION: return "compression";
5350 }
5351 snprintf(buf, sizeof(buf), "0x%x", opt);
5352 return buf;
5353 }
5354 #endif
5355
5356 static const char *
5357 sppp_state_name(int state)
5358 {
5359 switch (state) {
5360 case STATE_INITIAL: return "initial";
5361 case STATE_STARTING: return "starting";
5362 case STATE_CLOSED: return "closed";
5363 case STATE_STOPPED: return "stopped";
5364 case STATE_CLOSING: return "closing";
5365 case STATE_STOPPING: return "stopping";
5366 case STATE_REQ_SENT: return "req-sent";
5367 case STATE_ACK_RCVD: return "ack-rcvd";
5368 case STATE_ACK_SENT: return "ack-sent";
5369 case STATE_OPENED: return "opened";
5370 }
5371 return "illegal";
5372 }
5373
5374 static const char *
5375 sppp_phase_name(int phase)
5376 {
5377 switch (phase) {
5378 case SPPP_PHASE_DEAD: return "dead";
5379 case SPPP_PHASE_ESTABLISH: return "establish";
5380 case SPPP_PHASE_TERMINATE: return "terminate";
5381 case SPPP_PHASE_AUTHENTICATE: return "authenticate";
5382 case SPPP_PHASE_NETWORK: return "network";
5383 }
5384 return "illegal";
5385 }
5386
5387 static const char *
5388 sppp_proto_name(u_short proto)
5389 {
5390 static char buf[12];
5391 switch (proto) {
5392 case PPP_LCP: return "lcp";
5393 case PPP_IPCP: return "ipcp";
5394 case PPP_PAP: return "pap";
5395 case PPP_CHAP: return "chap";
5396 case PPP_IPV6CP: return "ipv6cp";
5397 }
5398 snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
5399 return buf;
5400 }
5401
5402 static void
5403 sppp_print_bytes(const u_char *p, u_short len)
5404 {
5405 addlog(" %02x", *p++);
5406 while (--len > 0)
5407 addlog("-%02x", *p++);
5408 }
5409
5410 static void
5411 sppp_print_string(const char *p, u_short len)
5412 {
5413 u_char c;
5414
5415 while (len-- > 0) {
5416 c = *p++;
5417 /*
5418 * Print only ASCII chars directly. RFC 1994 recommends
5419 * using only them, but we don't rely on it. */
5420 if (c < ' ' || c > '~')
5421 addlog("\\x%x", c);
5422 else
5423 addlog("%c", c);
5424 }
5425 }
5426
5427 static const char *
5428 sppp_dotted_quad(u_int32_t addr)
5429 {
5430 static char s[16];
5431 snprintf(s, sizeof(s), "%d.%d.%d.%d",
5432 (int)((addr >> 24) & 0xff),
5433 (int)((addr >> 16) & 0xff),
5434 (int)((addr >> 8) & 0xff),
5435 (int)(addr & 0xff));
5436 return s;
5437 }
5438
5439 /* a dummy, used to drop uninteresting events */
5440 static void
5441 sppp_null(struct sppp *unused)
5442 {
5443 /* do just nothing */
5444 }
5445 /*
5446 * This file is large. Tell emacs to highlight it nevertheless.
5447 *
5448 * Local Variables:
5449 * hilit-auto-highlight-maxout: 120000
5450 * End:
5451 */
5452