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