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