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