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