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