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