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