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