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