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