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