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