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