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