if_spppsubr.c revision 1.33 1 /* $NetBSD: if_spppsubr.c,v 1.33 2001/12/15 20:40:37 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.33 2001/12/15 20:40:37 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 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2014 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2015 ifp->if_flags |= IFF_RUNNING;
2016 lcp.Open(sp);
2017 }
2018
2019 sppp_up_event(&lcp, sp);
2020 }
2021
2022 static void
2023 sppp_lcp_down(struct sppp *sp)
2024 {
2025 STDDCL;
2026
2027 sppp_down_event(&lcp, sp);
2028
2029 /*
2030 * If this is neither a dial-on-demand nor a passive
2031 * interface, simulate an ``ifconfig down'' action, so the
2032 * administrator can force a redial by another ``ifconfig
2033 * up''. XXX For leased line operation, should we immediately
2034 * try to reopen the connection here?
2035 */
2036 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2037 log(LOG_INFO,
2038 SPP_FMT "Down event (carrier loss), taking interface down.\n",
2039 SPP_ARGS(ifp));
2040 if_down(ifp);
2041 } else {
2042 if (debug)
2043 log(LOG_DEBUG,
2044 SPP_FMT "Down event (carrier loss)\n",
2045 SPP_ARGS(ifp));
2046 }
2047 sp->pp_flags &= ~PP_CALLIN;
2048 if (sp->state[IDX_LCP] != STATE_INITIAL)
2049 lcp.Close(sp);
2050 ifp->if_flags &= ~IFF_RUNNING;
2051 }
2052
2053 static void
2054 sppp_lcp_open(struct sppp *sp)
2055 {
2056 /*
2057 * If we are authenticator, negotiate LCP_AUTH
2058 */
2059 if (sp->hisauth.proto != 0)
2060 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2061 else
2062 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2063 sp->pp_flags &= ~PP_NEEDAUTH;
2064 sppp_open_event(&lcp, sp);
2065 }
2066
2067 static void
2068 sppp_lcp_close(struct sppp *sp)
2069 {
2070 sppp_close_event(&lcp, sp);
2071 }
2072
2073 static void
2074 sppp_lcp_TO(void *cookie)
2075 {
2076 sppp_to_event(&lcp, (struct sppp *)cookie);
2077 }
2078
2079 /*
2080 * Analyze a configure request. Return true if it was agreeable, and
2081 * caused action sca, false if it has been rejected or nak'ed, and
2082 * caused action scn. (The return value is used to make the state
2083 * transition decision in the state automaton.)
2084 */
2085 static int
2086 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2087 {
2088 STDDCL;
2089 u_char *buf, *r, *p;
2090 int origlen, rlen;
2091 u_int32_t nmagic;
2092 u_short authproto;
2093
2094 len -= 4;
2095 origlen = len;
2096 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2097 if (! buf)
2098 return (0);
2099
2100 if (debug)
2101 log(LOG_DEBUG, SPP_FMT "lcp parse opts:",
2102 SPP_ARGS(ifp));
2103
2104 /* pass 1: check for things that need to be rejected */
2105 p = (void*) (h+1);
2106 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2107 if (debug)
2108 addlog(" %s", sppp_lcp_opt_name(*p));
2109 switch (*p) {
2110 case LCP_OPT_MAGIC:
2111 /* Magic number. */
2112 /* fall through, both are same length */
2113 case LCP_OPT_ASYNC_MAP:
2114 /* Async control character map. */
2115 if (len >= 6 || p[1] == 6)
2116 continue;
2117 if (debug)
2118 addlog(" [invalid]");
2119 break;
2120 case LCP_OPT_MRU:
2121 /* Maximum receive unit. */
2122 if (len >= 4 && p[1] == 4)
2123 continue;
2124 if (debug)
2125 addlog(" [invalid]");
2126 break;
2127 case LCP_OPT_AUTH_PROTO:
2128 if (len < 4) {
2129 if (debug)
2130 addlog(" [invalid]");
2131 break;
2132 }
2133 authproto = (p[2] << 8) + p[3];
2134 if (authproto == PPP_CHAP && p[1] != 5) {
2135 if (debug)
2136 addlog(" [invalid chap len]");
2137 break;
2138 }
2139 if (sp->myauth.proto == 0) {
2140 /* we are not configured to do auth */
2141 if (debug)
2142 addlog(" [not configured]");
2143 break;
2144 }
2145 /*
2146 * Remote want us to authenticate, remember this,
2147 * so we stay in PHASE_AUTHENTICATE after LCP got
2148 * up.
2149 */
2150 sp->pp_flags |= PP_NEEDAUTH;
2151 continue;
2152 default:
2153 /* Others not supported. */
2154 if (debug)
2155 addlog(" [rej]");
2156 break;
2157 }
2158 /* Add the option to rejected list. */
2159 bcopy (p, r, p[1]);
2160 r += p[1];
2161 rlen += p[1];
2162 }
2163 if (rlen) {
2164 if (debug)
2165 addlog(" send conf-rej\n");
2166 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2167 goto end;
2168 } else if (debug)
2169 addlog("\n");
2170
2171 /*
2172 * pass 2: check for option values that are unacceptable and
2173 * thus require to be nak'ed.
2174 */
2175 if (debug)
2176 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2177 SPP_ARGS(ifp));
2178
2179 p = (void*) (h+1);
2180 len = origlen;
2181 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2182 if (debug)
2183 addlog(" %s", sppp_lcp_opt_name(*p));
2184 switch (*p) {
2185 case LCP_OPT_MAGIC:
2186 /* Magic number -- extract. */
2187 nmagic = (u_int32_t)p[2] << 24 |
2188 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2189 if (nmagic != sp->lcp.magic) {
2190 if (debug)
2191 addlog(" 0x%x", nmagic);
2192 continue;
2193 }
2194 /*
2195 * Local and remote magics equal -- loopback?
2196 */
2197 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2198 printf (SPP_FMT "loopback\n",
2199 SPP_ARGS(ifp));
2200 sp->pp_loopcnt = 0;
2201 if (ifp->if_flags & IFF_UP) {
2202 if_down(ifp);
2203 IF_PURGE(&sp->pp_cpq);
2204 /* XXX ? */
2205 lcp.Down(sp);
2206 lcp.Up(sp);
2207 }
2208 } else if (debug)
2209 addlog(" [glitch]");
2210 ++sp->pp_loopcnt;
2211 /*
2212 * We negate our magic here, and NAK it. If
2213 * we see it later in an NAK packet, we
2214 * suggest a new one.
2215 */
2216 nmagic = ~sp->lcp.magic;
2217 /* Gonna NAK it. */
2218 p[2] = nmagic >> 24;
2219 p[3] = nmagic >> 16;
2220 p[4] = nmagic >> 8;
2221 p[5] = nmagic;
2222 break;
2223
2224 case LCP_OPT_ASYNC_MAP:
2225 /* Async control character map -- check to be zero. */
2226 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
2227 if (debug)
2228 addlog(" [empty]");
2229 continue;
2230 }
2231 if (debug)
2232 addlog(" [non-empty]");
2233 /* suggest a zero one */
2234 p[2] = p[3] = p[4] = p[5] = 0;
2235 break;
2236
2237 case LCP_OPT_MRU:
2238 /*
2239 * Maximum receive unit. Always agreeable,
2240 * but ignored by now.
2241 */
2242 sp->lcp.their_mru = p[2] * 256 + p[3];
2243 if (debug)
2244 addlog(" %ld", sp->lcp.their_mru);
2245 continue;
2246
2247 case LCP_OPT_AUTH_PROTO:
2248 authproto = (p[2] << 8) + p[3];
2249 if (sp->myauth.proto != authproto) {
2250 /* not agreed, nak */
2251 if (debug)
2252 addlog(" [mine %s != his %s]",
2253 sppp_proto_name(sp->hisauth.proto),
2254 sppp_proto_name(authproto));
2255 p[2] = sp->myauth.proto >> 8;
2256 p[3] = sp->myauth.proto;
2257 break;
2258 }
2259 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2260 if (debug)
2261 addlog(" [chap not MD5]");
2262 p[4] = CHAP_MD5;
2263 break;
2264 }
2265 continue;
2266 }
2267 /* Add the option to nak'ed list. */
2268 bcopy (p, r, p[1]);
2269 r += p[1];
2270 rlen += p[1];
2271 }
2272 if (rlen) {
2273 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2274 if (debug)
2275 addlog(" max_failure (%d) exceeded, "
2276 "send conf-rej\n",
2277 sp->lcp.max_failure);
2278 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2279 } else {
2280 if (debug)
2281 addlog(" send conf-nak\n");
2282 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2283 }
2284 goto end;
2285 } else {
2286 if (debug)
2287 addlog(" send conf-ack\n");
2288 sp->fail_counter[IDX_LCP] = 0;
2289 sp->pp_loopcnt = 0;
2290 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2291 h->ident, origlen, h+1);
2292 }
2293
2294 end:
2295 free (buf, M_TEMP);
2296 return (rlen == 0);
2297 }
2298
2299 /*
2300 * Analyze the LCP Configure-Reject option list, and adjust our
2301 * negotiation.
2302 */
2303 static void
2304 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2305 {
2306 STDDCL;
2307 u_char *buf, *p;
2308
2309 len -= 4;
2310 buf = malloc (len, M_TEMP, M_NOWAIT);
2311 if (!buf)
2312 return;
2313
2314 if (debug)
2315 log(LOG_DEBUG, SPP_FMT "lcp rej opts:",
2316 SPP_ARGS(ifp));
2317
2318 p = (void*) (h+1);
2319 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2320 if (debug)
2321 addlog(" %s", sppp_lcp_opt_name(*p));
2322 switch (*p) {
2323 case LCP_OPT_MAGIC:
2324 /* Magic number -- can't use it, use 0 */
2325 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2326 sp->lcp.magic = 0;
2327 break;
2328 case LCP_OPT_MRU:
2329 /*
2330 * Should not be rejected anyway, since we only
2331 * negotiate a MRU if explicitly requested by
2332 * peer.
2333 */
2334 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2335 break;
2336 case LCP_OPT_AUTH_PROTO:
2337 /*
2338 * Peer doesn't want to authenticate himself,
2339 * deny unless this is a dialout call, and
2340 * AUTHFLAG_NOCALLOUT is set.
2341 */
2342 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2343 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2344 if (debug)
2345 addlog(" [don't insist on auth "
2346 "for callout]");
2347 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2348 break;
2349 }
2350 if (debug)
2351 addlog("[access denied]\n");
2352 lcp.Close(sp);
2353 break;
2354 }
2355 }
2356 if (debug)
2357 addlog("\n");
2358 free (buf, M_TEMP);
2359 return;
2360 }
2361
2362 /*
2363 * Analyze the LCP Configure-NAK option list, and adjust our
2364 * negotiation.
2365 */
2366 static void
2367 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2368 {
2369 STDDCL;
2370 u_char *buf, *p;
2371 u_int32_t magic;
2372
2373 len -= 4;
2374 buf = malloc (len, M_TEMP, M_NOWAIT);
2375 if (!buf)
2376 return;
2377
2378 if (debug)
2379 log(LOG_DEBUG, SPP_FMT "lcp nak opts:",
2380 SPP_ARGS(ifp));
2381
2382 p = (void*) (h+1);
2383 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2384 if (debug)
2385 addlog(" %s", sppp_lcp_opt_name(*p));
2386 switch (*p) {
2387 case LCP_OPT_MAGIC:
2388 /* Magic number -- renegotiate */
2389 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2390 len >= 6 && p[1] == 6) {
2391 magic = (u_int32_t)p[2] << 24 |
2392 (u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
2393 /*
2394 * If the remote magic is our negated one,
2395 * this looks like a loopback problem.
2396 * Suggest a new magic to make sure.
2397 */
2398 if (magic == ~sp->lcp.magic) {
2399 if (debug)
2400 addlog(" magic glitch");
2401 sp->lcp.magic = random();
2402 } else {
2403 sp->lcp.magic = magic;
2404 if (debug)
2405 addlog(" %d", magic);
2406 }
2407 }
2408 break;
2409 case LCP_OPT_MRU:
2410 /*
2411 * Peer wants to advise us to negotiate an MRU.
2412 * Agree on it if it's reasonable, or use
2413 * default otherwise.
2414 */
2415 if (len >= 4 && p[1] == 4) {
2416 u_int mru = p[2] * 256 + p[3];
2417 if (debug)
2418 addlog(" %d", mru);
2419 if (mru < PP_MTU || mru > PP_MAX_MRU)
2420 mru = PP_MTU;
2421 sp->lcp.mru = mru;
2422 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2423 }
2424 break;
2425 case LCP_OPT_AUTH_PROTO:
2426 /*
2427 * Peer doesn't like our authentication method,
2428 * deny.
2429 */
2430 if (debug)
2431 addlog("[access denied]\n");
2432 lcp.Close(sp);
2433 break;
2434 }
2435 }
2436 if (debug)
2437 addlog("\n");
2438 free (buf, M_TEMP);
2439 return;
2440 }
2441
2442 static void
2443 sppp_lcp_tlu(struct sppp *sp)
2444 {
2445 STDDCL;
2446 int i;
2447 u_int32_t mask;
2448
2449 /* XXX ? */
2450 if (! (ifp->if_flags & IFF_UP) &&
2451 (ifp->if_flags & IFF_RUNNING)) {
2452 /* Coming out of loopback mode. */
2453 if_up(ifp);
2454 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2455 }
2456
2457 for (i = 0; i < IDX_COUNT; i++)
2458 if ((cps[i])->flags & CP_QUAL)
2459 (cps[i])->Open(sp);
2460
2461 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2462 (sp->pp_flags & PP_NEEDAUTH) != 0)
2463 sp->pp_phase = PHASE_AUTHENTICATE;
2464 else
2465 sp->pp_phase = PHASE_NETWORK;
2466
2467 if(debug)
2468 {
2469 log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2470 sppp_phase_name(sp->pp_phase));
2471 }
2472
2473 /*
2474 * Open all authentication protocols. This is even required
2475 * if we already proceeded to network phase, since it might be
2476 * that remote wants us to authenticate, so we might have to
2477 * send a PAP request. Undesired authentication protocols
2478 * don't do anything when they get an Open event.
2479 */
2480 for (i = 0; i < IDX_COUNT; i++)
2481 if ((cps[i])->flags & CP_AUTH)
2482 (cps[i])->Open(sp);
2483
2484 if (sp->pp_phase == PHASE_NETWORK) {
2485 /* Notify all NCPs. */
2486 for (i = 0; i < IDX_COUNT; i++)
2487 if ((cps[i])->flags & CP_NCP)
2488 (cps[i])->Open(sp);
2489 }
2490
2491 /* Send Up events to all started protos. */
2492 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2493 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2494 (cps[i])->Up(sp);
2495
2496 /* notify low-level driver of state change */
2497 if (sp->pp_chg)
2498 sp->pp_chg(sp, (int)sp->pp_phase);
2499
2500 if (sp->pp_phase == PHASE_NETWORK)
2501 /* if no NCP is starting, close down */
2502 sppp_lcp_check_and_close(sp);
2503 }
2504
2505 static void
2506 sppp_lcp_tld(struct sppp *sp)
2507 {
2508 STDDCL;
2509 int i;
2510 u_int32_t mask;
2511
2512 sp->pp_phase = PHASE_TERMINATE;
2513
2514 if(debug)
2515 {
2516 log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2517 sppp_phase_name(sp->pp_phase));
2518 }
2519
2520 /*
2521 * Take upper layers down. We send the Down event first and
2522 * the Close second to prevent the upper layers from sending
2523 * ``a flurry of terminate-request packets'', as the RFC
2524 * describes it.
2525 */
2526 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2527 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2528 (cps[i])->Down(sp);
2529 (cps[i])->Close(sp);
2530 }
2531 }
2532
2533 static void
2534 sppp_lcp_tls(struct sppp *sp)
2535 {
2536 STDDCL;
2537
2538 sp->pp_phase = PHASE_ESTABLISH;
2539
2540 if(debug)
2541 {
2542 log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2543 sppp_phase_name(sp->pp_phase));
2544 }
2545
2546 /* Notify lower layer if desired. */
2547 if (sp->pp_tls)
2548 (sp->pp_tls)(sp);
2549 }
2550
2551 static void
2552 sppp_lcp_tlf(struct sppp *sp)
2553 {
2554 STDDCL;
2555
2556 sp->pp_phase = PHASE_DEAD;
2557
2558 if(debug)
2559 {
2560 log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2561 sppp_phase_name(sp->pp_phase));
2562 }
2563
2564 /* Notify lower layer if desired. */
2565 if (sp->pp_tlf)
2566 (sp->pp_tlf)(sp);
2567 }
2568
2569 static void
2570 sppp_lcp_scr(struct sppp *sp)
2571 {
2572 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2573 int i = 0;
2574 u_short authproto;
2575
2576 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2577 if (! sp->lcp.magic)
2578 sp->lcp.magic = random();
2579 opt[i++] = LCP_OPT_MAGIC;
2580 opt[i++] = 6;
2581 opt[i++] = sp->lcp.magic >> 24;
2582 opt[i++] = sp->lcp.magic >> 16;
2583 opt[i++] = sp->lcp.magic >> 8;
2584 opt[i++] = sp->lcp.magic;
2585 }
2586
2587 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2588 opt[i++] = LCP_OPT_MRU;
2589 opt[i++] = 4;
2590 opt[i++] = sp->lcp.mru >> 8;
2591 opt[i++] = sp->lcp.mru;
2592 }
2593
2594 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2595 authproto = sp->hisauth.proto;
2596 opt[i++] = LCP_OPT_AUTH_PROTO;
2597 opt[i++] = authproto == PPP_CHAP? 5: 4;
2598 opt[i++] = authproto >> 8;
2599 opt[i++] = authproto;
2600 if (authproto == PPP_CHAP)
2601 opt[i++] = CHAP_MD5;
2602 }
2603
2604 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2605 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2606 }
2607
2608 /*
2609 * Check the open NCPs, return true if at least one NCP is open.
2610 */
2611 static int
2612 sppp_ncp_check(struct sppp *sp)
2613 {
2614 int i, mask;
2615
2616 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2617 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2618 return 1;
2619 return 0;
2620 }
2621
2622 /*
2623 * Re-check the open NCPs and see if we should terminate the link.
2624 * Called by the NCPs during their tlf action handling.
2625 */
2626 static void
2627 sppp_lcp_check_and_close(struct sppp *sp)
2628 {
2629
2630 if (sp->pp_phase < PHASE_NETWORK)
2631 /* don't bother, we are already going down */
2632 return;
2633
2634 if (sppp_ncp_check(sp))
2635 return;
2636
2637 lcp.Close(sp);
2638 }
2639
2640
2641 /*
2643 *--------------------------------------------------------------------------*
2644 * *
2645 * The IPCP implementation. *
2646 * *
2647 *--------------------------------------------------------------------------*
2648 */
2649
2650 static void
2651 sppp_ipcp_init(struct sppp *sp)
2652 {
2653 sp->ipcp.opts = 0;
2654 sp->ipcp.flags = 0;
2655 sp->state[IDX_IPCP] = STATE_INITIAL;
2656 sp->fail_counter[IDX_IPCP] = 0;
2657 sp->pp_seq[IDX_IPCP] = 0;
2658 sp->pp_rseq[IDX_IPCP] = 0;
2659 callout_init(&sp->ch[IDX_IPCP]);
2660 }
2661
2662 static void
2663 sppp_ipcp_up(struct sppp *sp)
2664 {
2665 sppp_up_event(&ipcp, sp);
2666 }
2667
2668 static void
2669 sppp_ipcp_down(struct sppp *sp)
2670 {
2671 sppp_down_event(&ipcp, sp);
2672 }
2673
2674 static void
2675 sppp_ipcp_open(struct sppp *sp)
2676 {
2677 STDDCL;
2678 u_int32_t myaddr, hisaddr;
2679
2680 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
2681 sp->ipcp.req_myaddr = 0;
2682 sp->ipcp.req_hisaddr = 0;
2683
2684 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2685 /*
2686 * If we don't have his address, this probably means our
2687 * interface doesn't want to talk IP at all. (This could
2688 * be the case if somebody wants to speak only IPX, for
2689 * example.) Don't open IPCP in this case.
2690 */
2691 if (hisaddr == 0L) {
2692 /* XXX this message should go away */
2693 if (debug)
2694 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2695 SPP_ARGS(ifp));
2696 return;
2697 }
2698
2699 if (myaddr == 0) {
2700 /*
2701 * I don't have an assigned address, so i need to
2702 * negotiate my address.
2703 */
2704 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2705 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2706 }
2707 if (hisaddr == 1) {
2708 /*
2709 * XXX - remove this hack!
2710 * remote has no valid adress, we need to get one assigned.
2711 */
2712 sp->ipcp.flags |= IPCP_HISADDR_DYN;
2713 }
2714 sppp_open_event(&ipcp, sp);
2715 }
2716
2717 static void
2718 sppp_ipcp_close(struct sppp *sp)
2719 {
2720 sppp_close_event(&ipcp, sp);
2721 if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
2722 /*
2723 * Some address was dynamic, clear it again.
2724 */
2725 sppp_clear_ip_addrs(sp);
2726 }
2727
2728 static void
2729 sppp_ipcp_TO(void *cookie)
2730 {
2731 sppp_to_event(&ipcp, (struct sppp *)cookie);
2732 }
2733
2734 /*
2735 * Analyze a configure request. Return true if it was agreeable, and
2736 * caused action sca, false if it has been rejected or nak'ed, and
2737 * caused action scn. (The return value is used to make the state
2738 * transition decision in the state automaton.)
2739 */
2740 static int
2741 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2742 {
2743 u_char *buf, *r, *p;
2744 struct ifnet *ifp = &sp->pp_if;
2745 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2746 u_int32_t hisaddr, desiredaddr;
2747
2748 len -= 4;
2749 origlen = len;
2750 /*
2751 * Make sure to allocate a buf that can at least hold a
2752 * conf-nak with an `address' option. We might need it below.
2753 */
2754 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2755 if (! buf)
2756 return (0);
2757
2758 /* pass 1: see if we can recognize them */
2759 if (debug)
2760 log(LOG_DEBUG, SPP_FMT "ipcp parse opts:",
2761 SPP_ARGS(ifp));
2762 p = (void*) (h+1);
2763 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2764 if (debug)
2765 addlog(" %s", sppp_ipcp_opt_name(*p));
2766 switch (*p) {
2767 #ifdef notyet
2768 case IPCP_OPT_COMPRESSION:
2769 if (len >= 6 && p[1] >= 6) {
2770 /* correctly formed compress option */
2771 continue;
2772 }
2773 if (debug)
2774 addlog(" [invalid]");
2775 break;
2776 #endif
2777 case IPCP_OPT_ADDRESS:
2778 if (len >= 6 && p[1] == 6) {
2779 /* correctly formed address option */
2780 continue;
2781 }
2782 if (debug)
2783 addlog(" [invalid]");
2784 break;
2785 default:
2786 /* Others not supported. */
2787 if (debug)
2788 addlog(" [rej]");
2789 break;
2790 }
2791 /* Add the option to rejected list. */
2792 bcopy (p, r, p[1]);
2793 r += p[1];
2794 rlen += p[1];
2795 }
2796 if (rlen) {
2797 if (debug)
2798 addlog(" send conf-rej\n");
2799 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2800 goto end;
2801 } else if (debug)
2802 addlog("\n");
2803
2804 /* pass 2: parse option values */
2805 if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
2806 hisaddr = sp->ipcp.req_hisaddr; /* we already aggreed on that */
2807 else
2808 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); /* user configuration */
2809 if (debug)
2810 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2811 SPP_ARGS(ifp));
2812 p = (void*) (h+1);
2813 len = origlen;
2814 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2815 if (debug)
2816 addlog(" %s", sppp_ipcp_opt_name(*p));
2817 switch (*p) {
2818 #ifdef notyet
2819 case IPCP_OPT_COMPRESSION:
2820 continue;
2821 #endif
2822 case IPCP_OPT_ADDRESS:
2823 desiredaddr = p[2] << 24 | p[3] << 16 |
2824 p[4] << 8 | p[5];
2825 if (desiredaddr == hisaddr ||
2826 ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
2827 /*
2828 * Peer's address is same as our value,
2829 * this is agreeable. Gonna conf-ack
2830 * it.
2831 */
2832 if (debug)
2833 addlog(" %s [ack]",
2834 sppp_dotted_quad(hisaddr));
2835 /* record that we've seen it already */
2836 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2837 sp->ipcp.req_hisaddr = desiredaddr;
2838 hisaddr = desiredaddr;
2839 continue;
2840 }
2841 /*
2842 * The address wasn't agreeable. This is either
2843 * he sent us 0.0.0.0, asking to assign him an
2844 * address, or he send us another address not
2845 * matching our value. Either case, we gonna
2846 * conf-nak it with our value.
2847 */
2848 if (debug) {
2849 if (desiredaddr == 0)
2850 addlog(" [addr requested]");
2851 else
2852 addlog(" %s [not agreed]",
2853 sppp_dotted_quad(desiredaddr));
2854 }
2855
2856 p[2] = hisaddr >> 24;
2857 p[3] = hisaddr >> 16;
2858 p[4] = hisaddr >> 8;
2859 p[5] = hisaddr;
2860 break;
2861 }
2862 /* Add the option to nak'ed list. */
2863 bcopy (p, r, p[1]);
2864 r += p[1];
2865 rlen += p[1];
2866 }
2867
2868 /*
2869 * If we are about to conf-ack the request, but haven't seen
2870 * his address so far, gonna conf-nak it instead, with the
2871 * `address' option present and our idea of his address being
2872 * filled in there, to request negotiation of both addresses.
2873 *
2874 * XXX This can result in an endless req - nak loop if peer
2875 * doesn't want to send us his address. Q: What should we do
2876 * about it? XXX A: implement the max-failure counter.
2877 */
2878 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2879 buf[0] = IPCP_OPT_ADDRESS;
2880 buf[1] = 6;
2881 buf[2] = hisaddr >> 24;
2882 buf[3] = hisaddr >> 16;
2883 buf[4] = hisaddr >> 8;
2884 buf[5] = hisaddr;
2885 rlen = 6;
2886 if (debug)
2887 addlog(" still need hisaddr");
2888 }
2889
2890 if (rlen) {
2891 if (debug)
2892 addlog(" send conf-nak\n");
2893 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2894 } else {
2895 if (debug)
2896 addlog(" send conf-ack\n");
2897 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2898 h->ident, origlen, h+1);
2899 }
2900
2901 end:
2902 free (buf, M_TEMP);
2903 return (rlen == 0);
2904 }
2905
2906 /*
2907 * Analyze the IPCP Configure-Reject option list, and adjust our
2908 * negotiation.
2909 */
2910 static void
2911 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2912 {
2913 u_char *buf, *p;
2914 struct ifnet *ifp = &sp->pp_if;
2915 int debug = ifp->if_flags & IFF_DEBUG;
2916
2917 len -= 4;
2918 buf = malloc (len, M_TEMP, M_NOWAIT);
2919 if (!buf)
2920 return;
2921
2922 if (debug)
2923 log(LOG_DEBUG, SPP_FMT "ipcp rej opts:",
2924 SPP_ARGS(ifp));
2925
2926 p = (void*) (h+1);
2927 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2928 if (debug)
2929 addlog(" %s", sppp_ipcp_opt_name(*p));
2930 switch (*p) {
2931 case IPCP_OPT_ADDRESS:
2932 /*
2933 * Peer doesn't grok address option. This is
2934 * bad. XXX Should we better give up here?
2935 */
2936 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2937 break;
2938 #ifdef notyet
2939 case IPCP_OPT_COMPRESS:
2940 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2941 break;
2942 #endif
2943 }
2944 }
2945 if (debug)
2946 addlog("\n");
2947 free (buf, M_TEMP);
2948 return;
2949 }
2950
2951 /*
2952 * Analyze the IPCP Configure-NAK option list, and adjust our
2953 * negotiation.
2954 */
2955 static void
2956 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2957 {
2958 u_char *buf, *p;
2959 struct ifnet *ifp = &sp->pp_if;
2960 int debug = ifp->if_flags & IFF_DEBUG;
2961 u_int32_t wantaddr;
2962
2963 len -= 4;
2964 buf = malloc (len, M_TEMP, M_NOWAIT);
2965 if (!buf)
2966 return;
2967
2968 if (debug)
2969 log(LOG_DEBUG, SPP_FMT "ipcp nak opts:",
2970 SPP_ARGS(ifp));
2971
2972 p = (void*) (h+1);
2973 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2974 if (debug)
2975 addlog(" %s", sppp_ipcp_opt_name(*p));
2976 switch (*p) {
2977 case IPCP_OPT_ADDRESS:
2978 /*
2979 * Peer doesn't like our local IP address. See
2980 * if we can do something for him. We'll drop
2981 * him our address then.
2982 */
2983 if (len >= 6 && p[1] == 6) {
2984 wantaddr = p[2] << 24 | p[3] << 16 |
2985 p[4] << 8 | p[5];
2986 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2987 if (debug)
2988 addlog(" [wantaddr %s]",
2989 sppp_dotted_quad(wantaddr));
2990 /*
2991 * When doing dynamic address assignment,
2992 * we accept his offer. Otherwise, we
2993 * ignore it and thus continue to negotiate
2994 * our already existing value.
2995 */
2996 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2997 if (debug)
2998 addlog(" [agree]");
2999 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3000 sp->ipcp.req_myaddr = wantaddr;
3001 }
3002 }
3003 break;
3004 #ifdef notyet
3005 case IPCP_OPT_COMPRESS:
3006 /*
3007 * Peer wants different compression parameters.
3008 */
3009 break;
3010 #endif
3011 }
3012 }
3013 if (debug)
3014 addlog("\n");
3015 free (buf, M_TEMP);
3016 return;
3017 }
3018
3019 static void
3020 sppp_ipcp_tlu(struct sppp *sp)
3021 {
3022 /* we are up. Set addresses and notify anyone interested */
3023 u_int32_t myaddr, hisaddr;
3024 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
3025 if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
3026 myaddr = sp->ipcp.req_myaddr;
3027 if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
3028 hisaddr = sp->ipcp.req_hisaddr;
3029 sppp_set_ip_addrs(sp, myaddr, hisaddr);
3030 if (sp->pp_con)
3031 sp->pp_con(sp);
3032 }
3033
3034 static void
3035 sppp_ipcp_tld(struct sppp *sp)
3036 {
3037 }
3038
3039 static void
3040 sppp_ipcp_tls(struct sppp *sp)
3041 {
3042 /* indicate to LCP that it must stay alive */
3043 sp->lcp.protos |= (1 << IDX_IPCP);
3044 }
3045
3046 static void
3047 sppp_ipcp_tlf(struct sppp *sp)
3048 {
3049 /* we no longer need LCP */
3050 sp->lcp.protos &= ~(1 << IDX_IPCP);
3051 }
3052
3053 static void
3054 sppp_ipcp_scr(struct sppp *sp)
3055 {
3056 char opt[6 /* compression */ + 6 /* address */];
3057 u_int32_t ouraddr;
3058 int i = 0;
3059
3060 #ifdef notyet
3061 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3062 opt[i++] = IPCP_OPT_COMPRESSION;
3063 opt[i++] = 6;
3064 opt[i++] = 0; /* VJ header compression */
3065 opt[i++] = 0x2d; /* VJ header compression */
3066 opt[i++] = max_slot_id;
3067 opt[i++] = comp_slot_id;
3068 }
3069 #endif
3070
3071 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3072 if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
3073 ouraddr = sp->ipcp.req_myaddr; /* not sure if this can ever happen */
3074 else
3075 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3076 opt[i++] = IPCP_OPT_ADDRESS;
3077 opt[i++] = 6;
3078 opt[i++] = ouraddr >> 24;
3079 opt[i++] = ouraddr >> 16;
3080 opt[i++] = ouraddr >> 8;
3081 opt[i++] = ouraddr;
3082 }
3083
3084 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3085 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3086 }
3087
3088
3089 /*
3091 *--------------------------------------------------------------------------*
3092 * *
3093 * The IPv6CP implementation. *
3094 * *
3095 *--------------------------------------------------------------------------*
3096 */
3097
3098 #ifdef INET6
3099 static void
3100 sppp_ipv6cp_init(struct sppp *sp)
3101 {
3102 sp->ipv6cp.opts = 0;
3103 sp->ipv6cp.flags = 0;
3104 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3105 sp->fail_counter[IDX_IPV6CP] = 0;
3106 sp->pp_seq[IDX_IPV6CP] = 0;
3107 sp->pp_rseq[IDX_IPV6CP] = 0;
3108 callout_init(&sp->ch[IDX_IPV6CP]);
3109 }
3110
3111 static void
3112 sppp_ipv6cp_up(struct sppp *sp)
3113 {
3114 sppp_up_event(&ipv6cp, sp);
3115 }
3116
3117 static void
3118 sppp_ipv6cp_down(struct sppp *sp)
3119 {
3120 sppp_down_event(&ipv6cp, sp);
3121 }
3122
3123 static void
3124 sppp_ipv6cp_open(struct sppp *sp)
3125 {
3126 STDDCL;
3127 struct in6_addr myaddr, hisaddr;
3128
3129 #ifdef IPV6CP_MYIFID_DYN
3130 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3131 #else
3132 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3133 #endif
3134
3135 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3136 /*
3137 * If we don't have our address, this probably means our
3138 * interface doesn't want to talk IPv6 at all. (This could
3139 * be the case if somebody wants to speak only IPX, for
3140 * example.) Don't open IPv6CP in this case.
3141 */
3142 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3143 /* XXX this message should go away */
3144 if (debug)
3145 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3146 SPP_ARGS(ifp));
3147 return;
3148 }
3149
3150 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3151 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3152 sppp_open_event(&ipv6cp, sp);
3153 }
3154
3155 static void
3156 sppp_ipv6cp_close(struct sppp *sp)
3157 {
3158 sppp_close_event(&ipv6cp, sp);
3159 }
3160
3161 static void
3162 sppp_ipv6cp_TO(void *cookie)
3163 {
3164 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3165 }
3166
3167 /*
3168 * Analyze a configure request. Return true if it was agreeable, and
3169 * caused action sca, false if it has been rejected or nak'ed, and
3170 * caused action scn. (The return value is used to make the state
3171 * transition decision in the state automaton.)
3172 */
3173 static int
3174 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3175 {
3176 u_char *buf, *r, *p;
3177 struct ifnet *ifp = &sp->pp_if;
3178 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3179 struct in6_addr myaddr, desiredaddr, suggestaddr;
3180 int ifidcount;
3181 int type;
3182 int collision, nohisaddr;
3183
3184 len -= 4;
3185 origlen = len;
3186 /*
3187 * Make sure to allocate a buf that can at least hold a
3188 * conf-nak with an `address' option. We might need it below.
3189 */
3190 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3191 if (! buf)
3192 return (0);
3193
3194 /* pass 1: see if we can recognize them */
3195 if (debug)
3196 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3197 SPP_ARGS(ifp));
3198 p = (void*) (h+1);
3199 ifidcount = 0;
3200 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3201 if (debug)
3202 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3203 switch (*p) {
3204 case IPV6CP_OPT_IFID:
3205 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3206 /* correctly formed address option */
3207 ifidcount++;
3208 continue;
3209 }
3210 if (debug)
3211 addlog(" [invalid]");
3212 break;
3213 #ifdef notyet
3214 case IPV6CP_OPT_COMPRESSION:
3215 if (len >= 4 && p[1] >= 4) {
3216 /* correctly formed compress option */
3217 continue;
3218 }
3219 if (debug)
3220 addlog(" [invalid]");
3221 break;
3222 #endif
3223 default:
3224 /* Others not supported. */
3225 if (debug)
3226 addlog(" [rej]");
3227 break;
3228 }
3229 /* Add the option to rejected list. */
3230 bcopy (p, r, p[1]);
3231 r += p[1];
3232 rlen += p[1];
3233 }
3234 if (rlen) {
3235 if (debug)
3236 addlog(" send conf-rej\n");
3237 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3238 goto end;
3239 } else if (debug)
3240 addlog("\n");
3241
3242 /* pass 2: parse option values */
3243 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3244 if (debug)
3245 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3246 SPP_ARGS(ifp));
3247 p = (void*) (h+1);
3248 len = origlen;
3249 type = CONF_ACK;
3250 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3251 if (debug)
3252 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3253 switch (*p) {
3254 #ifdef notyet
3255 case IPV6CP_OPT_COMPRESSION:
3256 continue;
3257 #endif
3258 case IPV6CP_OPT_IFID:
3259 memset(&desiredaddr, 0, sizeof(desiredaddr));
3260 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3261 collision = (bcmp(&desiredaddr.s6_addr[8],
3262 &myaddr.s6_addr[8], 8) == 0);
3263 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3264
3265 desiredaddr.s6_addr16[0] = htons(0xfe80);
3266 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3267
3268 if (!collision && !nohisaddr) {
3269 /* no collision, hisaddr known - Conf-Ack */
3270 type = CONF_ACK;
3271
3272 if (debug) {
3273 addlog(" %s [%s]",
3274 ip6_sprintf(&desiredaddr),
3275 sppp_cp_type_name(type));
3276 }
3277 continue;
3278 }
3279
3280 memset(&suggestaddr, 0, sizeof(&suggestaddr));
3281 if (collision && nohisaddr) {
3282 /* collision, hisaddr unknown - Conf-Rej */
3283 type = CONF_REJ;
3284 memset(&p[2], 0, 8);
3285 } else {
3286 /*
3287 * - no collision, hisaddr unknown, or
3288 * - collision, hisaddr known
3289 * Conf-Nak, suggest hisaddr
3290 */
3291 type = CONF_NAK;
3292 sppp_suggest_ip6_addr(sp, &suggestaddr);
3293 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3294 }
3295 if (debug)
3296 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3297 sppp_cp_type_name(type));
3298 break;
3299 }
3300 /* Add the option to nak'ed list. */
3301 bcopy (p, r, p[1]);
3302 r += p[1];
3303 rlen += p[1];
3304 }
3305
3306 if (rlen == 0 && type == CONF_ACK) {
3307 if (debug)
3308 addlog(" send %s\n", sppp_cp_type_name(type));
3309 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3310 } else {
3311 #ifdef DIAGNOSTIC
3312 if (type == CONF_ACK)
3313 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3314 #endif
3315
3316 if (debug) {
3317 addlog(" send %s suggest %s\n",
3318 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3319 }
3320 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3321 }
3322
3323 end:
3324 free (buf, M_TEMP);
3325 return (rlen == 0);
3326 }
3327
3328 /*
3329 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3330 * negotiation.
3331 */
3332 static void
3333 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3334 {
3335 u_char *buf, *p;
3336 struct ifnet *ifp = &sp->pp_if;
3337 int debug = ifp->if_flags & IFF_DEBUG;
3338
3339 len -= 4;
3340 buf = malloc (len, M_TEMP, M_NOWAIT);
3341 if (!buf)
3342 return;
3343
3344 if (debug)
3345 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3346 SPP_ARGS(ifp));
3347
3348 p = (void*) (h+1);
3349 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3350 if (debug)
3351 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3352 switch (*p) {
3353 case IPV6CP_OPT_IFID:
3354 /*
3355 * Peer doesn't grok address option. This is
3356 * bad. XXX Should we better give up here?
3357 */
3358 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3359 break;
3360 #ifdef notyet
3361 case IPV6CP_OPT_COMPRESS:
3362 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3363 break;
3364 #endif
3365 }
3366 }
3367 if (debug)
3368 addlog("\n");
3369 free (buf, M_TEMP);
3370 return;
3371 }
3372
3373 /*
3374 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3375 * negotiation.
3376 */
3377 static void
3378 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3379 {
3380 u_char *buf, *p;
3381 struct ifnet *ifp = &sp->pp_if;
3382 int debug = ifp->if_flags & IFF_DEBUG;
3383 struct in6_addr suggestaddr;
3384
3385 len -= 4;
3386 buf = malloc (len, M_TEMP, M_NOWAIT);
3387 if (!buf)
3388 return;
3389
3390 if (debug)
3391 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3392 SPP_ARGS(ifp));
3393
3394 p = (void*) (h+1);
3395 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3396 if (debug)
3397 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3398 switch (*p) {
3399 case IPV6CP_OPT_IFID:
3400 /*
3401 * Peer doesn't like our local ifid. See
3402 * if we can do something for him. We'll drop
3403 * him our address then.
3404 */
3405 if (len < 10 || p[1] != 10)
3406 break;
3407 memset(&suggestaddr, 0, sizeof(suggestaddr));
3408 suggestaddr.s6_addr16[0] = htons(0xfe80);
3409 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3410 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3411
3412 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3413 if (debug)
3414 addlog(" [suggestaddr %s]",
3415 ip6_sprintf(&suggestaddr));
3416 #ifdef IPV6CP_MYIFID_DYN
3417 /*
3418 * When doing dynamic address assignment,
3419 * we accept his offer.
3420 */
3421 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3422 struct in6_addr lastsuggest;
3423 /*
3424 * If <suggested myaddr from peer> equals to
3425 * <hisaddr we have suggested last time>,
3426 * we have a collision. generate new random
3427 * ifid.
3428 */
3429 sppp_suggest_ip6_addr(&lastsuggest);
3430 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3431 lastsuggest)) {
3432 if (debug)
3433 addlog(" [random]");
3434 sppp_gen_ip6_addr(sp, &suggestaddr);
3435 }
3436 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3437 if (debug)
3438 addlog(" [agree]");
3439 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3440 }
3441 #else
3442 /*
3443 * Since we do not do dynamic address assignment,
3444 * we ignore it and thus continue to negotiate
3445 * our already existing value. This can possibly
3446 * go into infinite request-reject loop.
3447 *
3448 * This is not likely because we normally use
3449 * ifid based on MAC-address.
3450 * If you have no ethernet card on the node, too bad.
3451 * XXX should we use fail_counter?
3452 */
3453 #endif
3454 break;
3455 #ifdef notyet
3456 case IPV6CP_OPT_COMPRESS:
3457 /*
3458 * Peer wants different compression parameters.
3459 */
3460 break;
3461 #endif
3462 }
3463 }
3464 if (debug)
3465 addlog("\n");
3466 free (buf, M_TEMP);
3467 return;
3468 }
3469
3470 static void
3471 sppp_ipv6cp_tlu(struct sppp *sp)
3472 {
3473 /* we are up - notify isdn daemon */
3474 if (sp->pp_con)
3475 sp->pp_con(sp);
3476 }
3477
3478 static void
3479 sppp_ipv6cp_tld(struct sppp *sp)
3480 {
3481 }
3482
3483 static void
3484 sppp_ipv6cp_tls(struct sppp *sp)
3485 {
3486 /* indicate to LCP that it must stay alive */
3487 sp->lcp.protos |= (1 << IDX_IPV6CP);
3488 }
3489
3490 static void
3491 sppp_ipv6cp_tlf(struct sppp *sp)
3492 {
3493 /* we no longer need LCP */
3494 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3495 }
3496
3497 static void
3498 sppp_ipv6cp_scr(struct sppp *sp)
3499 {
3500 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3501 struct in6_addr ouraddr;
3502 int i = 0;
3503
3504 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3505 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3506 opt[i++] = IPV6CP_OPT_IFID;
3507 opt[i++] = 10;
3508 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3509 i += 8;
3510 }
3511
3512 #ifdef notyet
3513 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3514 opt[i++] = IPV6CP_OPT_COMPRESSION;
3515 opt[i++] = 4;
3516 opt[i++] = 0; /* TBD */
3517 opt[i++] = 0; /* TBD */
3518 /* variable length data may follow */
3519 }
3520 #endif
3521
3522 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3523 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3524 }
3525 #else /*INET6*/
3526 static void sppp_ipv6cp_init(struct sppp *sp)
3527 {
3528 }
3529
3530 static void sppp_ipv6cp_up(struct sppp *sp)
3531 {
3532 }
3533
3534 static void sppp_ipv6cp_down(struct sppp *sp)
3535 {
3536 }
3537
3538
3539 static void sppp_ipv6cp_open(struct sppp *sp)
3540 {
3541 }
3542
3543 static void sppp_ipv6cp_close(struct sppp *sp)
3544 {
3545 }
3546
3547 static void sppp_ipv6cp_TO(void *sp)
3548 {
3549 }
3550
3551 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3552 {
3553 return 0;
3554 }
3555
3556 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3557 {
3558 }
3559
3560 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3561 {
3562 }
3563
3564 static void sppp_ipv6cp_tlu(struct sppp *sp)
3565 {
3566 }
3567
3568 static void sppp_ipv6cp_tld(struct sppp *sp)
3569 {
3570 }
3571
3572 static void sppp_ipv6cp_tls(struct sppp *sp)
3573 {
3574 }
3575
3576 static void sppp_ipv6cp_tlf(struct sppp *sp)
3577 {
3578 }
3579
3580 static void sppp_ipv6cp_scr(struct sppp *sp)
3581 {
3582 }
3583 #endif /*INET6*/
3584
3585
3586 /*
3588 *--------------------------------------------------------------------------*
3589 * *
3590 * The CHAP implementation. *
3591 * *
3592 *--------------------------------------------------------------------------*
3593 */
3594
3595 /*
3596 * The authentication protocols don't employ a full-fledged state machine as
3597 * the control protocols do, since they do have Open and Close events, but
3598 * not Up and Down, nor are they explicitly terminated. Also, use of the
3599 * authentication protocols may be different in both directions (this makes
3600 * sense, think of a machine that never accepts incoming calls but only
3601 * calls out, it doesn't require the called party to authenticate itself).
3602 *
3603 * Our state machine for the local authentication protocol (we are requesting
3604 * the peer to authenticate) looks like:
3605 *
3606 * RCA-
3607 * +--------------------------------------------+
3608 * V scn,tld|
3609 * +--------+ Close +---------+ RCA+
3610 * | |<----------------------------------| |------+
3611 * +--->| Closed | TO* | Opened | sca |
3612 * | | |-----+ +-------| |<-----+
3613 * | +--------+ irc | | +---------+
3614 * | ^ | | ^
3615 * | | | | |
3616 * | | | | |
3617 * | TO-| | | |
3618 * | |tld TO+ V | |
3619 * | | +------->+ | |
3620 * | | | | | |
3621 * | +--------+ V | |
3622 * | | |<----+<--------------------+ |
3623 * | | Req- | scr |
3624 * | | Sent | |
3625 * | | | |
3626 * | +--------+ |
3627 * | RCA- | | RCA+ |
3628 * +------+ +------------------------------------------+
3629 * scn,tld sca,irc,ict,tlu
3630 *
3631 *
3632 * with:
3633 *
3634 * Open: LCP reached authentication phase
3635 * Close: LCP reached terminate phase
3636 *
3637 * RCA+: received reply (pap-req, chap-response), acceptable
3638 * RCN: received reply (pap-req, chap-response), not acceptable
3639 * TO+: timeout with restart counter >= 0
3640 * TO-: timeout with restart counter < 0
3641 * TO*: reschedule timeout for CHAP
3642 *
3643 * scr: send request packet (none for PAP, chap-challenge)
3644 * sca: send ack packet (pap-ack, chap-success)
3645 * scn: send nak packet (pap-nak, chap-failure)
3646 * ict: initialize re-challenge timer (CHAP only)
3647 *
3648 * tlu: this-layer-up, LCP reaches network phase
3649 * tld: this-layer-down, LCP enters terminate phase
3650 *
3651 * Note that in CHAP mode, after sending a new challenge, while the state
3652 * automaton falls back into Req-Sent state, it doesn't signal a tld
3653 * event to LCP, so LCP remains in network phase. Only after not getting
3654 * any response (or after getting an unacceptable response), CHAP closes,
3655 * causing LCP to enter terminate phase.
3656 *
3657 * With PAP, there is no initial request that can be sent. The peer is
3658 * expected to send one based on the successful negotiation of PAP as
3659 * the authentication protocol during the LCP option negotiation.
3660 *
3661 * Incoming authentication protocol requests (remote requests
3662 * authentication, we are peer) don't employ a state machine at all,
3663 * they are simply answered. Some peers [Ascend P50 firmware rev
3664 * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
3665 * still in authentication phase (thereby violating the standard that
3666 * demands that these NCP packets are to be discarded), so we keep
3667 * track of the peer demanding us to authenticate, and only proceed to
3668 * phase network once we've seen a positive acknowledge for the
3669 * authentication.
3670 */
3671
3672 /*
3673 * Handle incoming CHAP packets.
3674 */
3675 void
3676 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3677 {
3678 STDDCL;
3679 struct lcp_header *h;
3680 int len, x;
3681 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3682 int value_len, name_len;
3683 MD5_CTX ctx;
3684
3685 len = m->m_pkthdr.len;
3686 if (len < 4) {
3687 if (debug)
3688 log(LOG_DEBUG,
3689 SPP_FMT "chap invalid packet length: %d bytes\n",
3690 SPP_ARGS(ifp), len);
3691 return;
3692 }
3693 h = mtod (m, struct lcp_header*);
3694 if (len > ntohs (h->len))
3695 len = ntohs (h->len);
3696
3697 switch (h->type) {
3698 /* challenge, failure and success are his authproto */
3699 case CHAP_CHALLENGE:
3700 value = 1 + (u_char*)(h+1);
3701 value_len = value[-1];
3702 name = value + value_len;
3703 name_len = len - value_len - 5;
3704 if (name_len < 0) {
3705 if (debug) {
3706 log(LOG_DEBUG,
3707 SPP_FMT "chap corrupted challenge "
3708 "<%s id=0x%x len=%d",
3709 SPP_ARGS(ifp),
3710 sppp_auth_type_name(PPP_CHAP, h->type),
3711 h->ident, ntohs(h->len));
3712 if (len > 4)
3713 sppp_print_bytes((u_char*) (h+1), len-4);
3714 addlog(">\n");
3715 }
3716 break;
3717 }
3718
3719 if (debug) {
3720 log(LOG_DEBUG,
3721 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3722 SPP_ARGS(ifp),
3723 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3724 ntohs(h->len));
3725 sppp_print_string((char*) name, name_len);
3726 addlog(" value-size=%d value=", value_len);
3727 sppp_print_bytes(value, value_len);
3728 addlog(">\n");
3729 }
3730
3731 /* Compute reply value. */
3732 MD5Init(&ctx);
3733 MD5Update(&ctx, &h->ident, 1);
3734 MD5Update(&ctx, sp->myauth.secret,
3735 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3736 MD5Update(&ctx, value, value_len);
3737 MD5Final(digest, &ctx);
3738 dsize = sizeof digest;
3739
3740 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3741 sizeof dsize, (const char *)&dsize,
3742 sizeof digest, digest,
3743 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3744 sp->myauth.name,
3745 0);
3746 break;
3747
3748 case CHAP_SUCCESS:
3749 if (debug) {
3750 log(LOG_DEBUG, SPP_FMT "chap success",
3751 SPP_ARGS(ifp));
3752 if (len > 4) {
3753 addlog(": ");
3754 sppp_print_string((char*)(h + 1), len - 4);
3755 }
3756 addlog("\n");
3757 }
3758 x = splnet();
3759 sp->pp_flags &= ~PP_NEEDAUTH;
3760 if (sp->myauth.proto == PPP_CHAP &&
3761 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3762 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3763 /*
3764 * We are authenticator for CHAP but didn't
3765 * complete yet. Leave it to tlu to proceed
3766 * to network phase.
3767 */
3768 splx(x);
3769 break;
3770 }
3771 splx(x);
3772 sppp_phase_network(sp);
3773 break;
3774
3775 case CHAP_FAILURE:
3776 if (debug) {
3777 log(LOG_INFO, SPP_FMT "chap failure",
3778 SPP_ARGS(ifp));
3779 if (len > 4) {
3780 addlog(": ");
3781 sppp_print_string((char*)(h + 1), len - 4);
3782 }
3783 addlog("\n");
3784 } else
3785 log(LOG_INFO, SPP_FMT "chap failure\n",
3786 SPP_ARGS(ifp));
3787 /* await LCP shutdown by authenticator */
3788 break;
3789
3790 /* response is my authproto */
3791 case CHAP_RESPONSE:
3792 value = 1 + (u_char*)(h+1);
3793 value_len = value[-1];
3794 name = value + value_len;
3795 name_len = len - value_len - 5;
3796 if (name_len < 0) {
3797 if (debug) {
3798 log(LOG_DEBUG,
3799 SPP_FMT "chap corrupted response "
3800 "<%s id=0x%x len=%d",
3801 SPP_ARGS(ifp),
3802 sppp_auth_type_name(PPP_CHAP, h->type),
3803 h->ident, ntohs(h->len));
3804 if (len > 4)
3805 sppp_print_bytes((u_char*)(h+1), len-4);
3806 addlog(">\n");
3807 }
3808 break;
3809 }
3810 if (h->ident != sp->confid[IDX_CHAP]) {
3811 if (debug)
3812 log(LOG_DEBUG,
3813 SPP_FMT "chap dropping response for old ID "
3814 "(got %d, expected %d)\n",
3815 SPP_ARGS(ifp),
3816 h->ident, sp->confid[IDX_CHAP]);
3817 break;
3818 }
3819 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
3820 || bcmp(name, sp->hisauth.name, name_len) != 0) {
3821 log(LOG_INFO, SPP_FMT "chap response, his name ",
3822 SPP_ARGS(ifp));
3823 sppp_print_string(name, name_len);
3824 addlog(" != expected ");
3825 sppp_print_string(sp->hisauth.name,
3826 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
3827 addlog("\n");
3828 }
3829 if (debug) {
3830 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
3831 "<%s id=0x%x len=%d name=",
3832 SPP_ARGS(ifp),
3833 sppp_state_name(sp->state[IDX_CHAP]),
3834 sppp_auth_type_name(PPP_CHAP, h->type),
3835 h->ident, ntohs (h->len));
3836 sppp_print_string((char*)name, name_len);
3837 addlog(" value-size=%d value=", value_len);
3838 sppp_print_bytes(value, value_len);
3839 addlog(">\n");
3840 }
3841 if (value_len != AUTHKEYLEN) {
3842 if (debug)
3843 log(LOG_DEBUG,
3844 SPP_FMT "chap bad hash value length: "
3845 "%d bytes, should be %d\n",
3846 SPP_ARGS(ifp), value_len,
3847 AUTHKEYLEN);
3848 break;
3849 }
3850
3851 MD5Init(&ctx);
3852 MD5Update(&ctx, &h->ident, 1);
3853 MD5Update(&ctx, sp->hisauth.secret,
3854 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3855 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3856 MD5Final(digest, &ctx);
3857
3858 #define FAILMSG "Failed..."
3859 #define SUCCMSG "Welcome!"
3860
3861 if (value_len != sizeof digest ||
3862 bcmp(digest, value, value_len) != 0) {
3863 /* action scn, tld */
3864 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3865 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3866 0);
3867 chap.tld(sp);
3868 break;
3869 }
3870 /* action sca, perhaps tlu */
3871 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3872 sp->state[IDX_CHAP] == STATE_OPENED)
3873 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3874 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3875 0);
3876 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3877 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3878 chap.tlu(sp);
3879 }
3880 break;
3881
3882 default:
3883 /* Unknown CHAP packet type -- ignore. */
3884 if (debug) {
3885 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
3886 "<0x%x id=0x%xh len=%d",
3887 SPP_ARGS(ifp),
3888 sppp_state_name(sp->state[IDX_CHAP]),
3889 h->type, h->ident, ntohs(h->len));
3890 if (len > 4)
3891 sppp_print_bytes((u_char*)(h+1), len-4);
3892 addlog(">\n");
3893 }
3894 break;
3895
3896 }
3897 }
3898
3899 static void
3900 sppp_chap_init(struct sppp *sp)
3901 {
3902 /* Chap doesn't have STATE_INITIAL at all. */
3903 sp->state[IDX_CHAP] = STATE_CLOSED;
3904 sp->fail_counter[IDX_CHAP] = 0;
3905 sp->pp_seq[IDX_CHAP] = 0;
3906 sp->pp_rseq[IDX_CHAP] = 0;
3907 callout_init(&sp->ch[IDX_CHAP]);
3908 }
3909
3910 static void
3911 sppp_chap_open(struct sppp *sp)
3912 {
3913 if (sp->myauth.proto == PPP_CHAP &&
3914 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3915 /* we are authenticator for CHAP, start it */
3916 chap.scr(sp);
3917 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3918 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3919 }
3920 /* nothing to be done if we are peer, await a challenge */
3921 }
3922
3923 static void
3924 sppp_chap_close(struct sppp *sp)
3925 {
3926 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3927 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3928 }
3929
3930 static void
3931 sppp_chap_TO(void *cookie)
3932 {
3933 struct sppp *sp = (struct sppp *)cookie;
3934 STDDCL;
3935 int s;
3936
3937 s = splnet();
3938 if (debug)
3939 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
3940 SPP_ARGS(ifp),
3941 sppp_state_name(sp->state[IDX_CHAP]),
3942 sp->rst_counter[IDX_CHAP]);
3943
3944 if (--sp->rst_counter[IDX_CHAP] < 0)
3945 /* TO- event */
3946 switch (sp->state[IDX_CHAP]) {
3947 case STATE_REQ_SENT:
3948 chap.tld(sp);
3949 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3950 break;
3951 }
3952 else
3953 /* TO+ (or TO*) event */
3954 switch (sp->state[IDX_CHAP]) {
3955 case STATE_OPENED:
3956 /* TO* event */
3957 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3958 /* fall through */
3959 case STATE_REQ_SENT:
3960 chap.scr(sp);
3961 /* sppp_cp_change_state() will restart the timer */
3962 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3963 break;
3964 }
3965
3966 splx(s);
3967 }
3968
3969 static void
3970 sppp_chap_tlu(struct sppp *sp)
3971 {
3972 STDDCL;
3973 int i, x;
3974
3975 i = 0;
3976 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3977
3978 /*
3979 * Some broken CHAP implementations (Conware CoNet, firmware
3980 * 4.0.?) don't want to re-authenticate their CHAP once the
3981 * initial challenge-response exchange has taken place.
3982 * Provide for an option to avoid rechallenges.
3983 */
3984 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3985 /*
3986 * Compute the re-challenge timeout. This will yield
3987 * a number between 300 and 810 seconds.
3988 */
3989 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3990
3991 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, sp);
3992 }
3993
3994 if (debug) {
3995 log(LOG_DEBUG,
3996 SPP_FMT "chap %s, ",
3997 SPP_ARGS(ifp),
3998 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3999 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4000 addlog("next re-challenge in %d seconds\n", i);
4001 else
4002 addlog("re-challenging supressed\n");
4003 }
4004
4005 x = splnet();
4006 /* indicate to LCP that we need to be closed down */
4007 sp->lcp.protos |= (1 << IDX_CHAP);
4008
4009 if (sp->pp_flags & PP_NEEDAUTH) {
4010 /*
4011 * Remote is authenticator, but his auth proto didn't
4012 * complete yet. Defer the transition to network
4013 * phase.
4014 */
4015 splx(x);
4016 return;
4017 }
4018 splx(x);
4019
4020 /*
4021 * If we are already in phase network, we are done here. This
4022 * is the case if this is a dummy tlu event after a re-challenge.
4023 */
4024 if (sp->pp_phase != PHASE_NETWORK)
4025 sppp_phase_network(sp);
4026 }
4027
4028 static void
4029 sppp_chap_tld(struct sppp *sp)
4030 {
4031 STDDCL;
4032
4033 if (debug)
4034 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4035 callout_stop(&sp->ch[IDX_CHAP]);
4036 sp->lcp.protos &= ~(1 << IDX_CHAP);
4037
4038 lcp.Close(sp);
4039 }
4040
4041 static void
4042 sppp_chap_scr(struct sppp *sp)
4043 {
4044 struct timeval tv;
4045 u_int32_t *ch, seed;
4046 u_char clen;
4047
4048 /* Compute random challenge. */
4049 ch = (u_int32_t *)sp->myauth.challenge;
4050 microtime(&tv);
4051 seed = tv.tv_sec ^ tv.tv_usec;
4052 ch[0] = seed ^ random();
4053 ch[1] = seed ^ random();
4054 ch[2] = seed ^ random();
4055 ch[3] = seed ^ random();
4056 clen = AUTHKEYLEN;
4057
4058 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4059
4060 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4061 sizeof clen, (const char *)&clen,
4062 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4063 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4064 sp->myauth.name,
4065 0);
4066 }
4067 /*
4069 *--------------------------------------------------------------------------*
4070 * *
4071 * The PAP implementation. *
4072 * *
4073 *--------------------------------------------------------------------------*
4074 */
4075 /*
4076 * For PAP, we need to keep a little state also if we are the peer, not the
4077 * authenticator. This is since we don't get a request to authenticate, but
4078 * have to repeatedly authenticate ourself until we got a response (or the
4079 * retry counter is expired).
4080 */
4081
4082 /*
4083 * Handle incoming PAP packets. */
4084 static void
4085 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4086 {
4087 STDDCL;
4088 struct lcp_header *h;
4089 int len, x;
4090 u_char *name, *passwd, mlen;
4091 int name_len, passwd_len;
4092
4093 len = m->m_pkthdr.len;
4094 if (len < 5) {
4095 if (debug)
4096 log(LOG_DEBUG,
4097 SPP_FMT "pap invalid packet length: %d bytes\n",
4098 SPP_ARGS(ifp), len);
4099 return;
4100 }
4101 h = mtod (m, struct lcp_header*);
4102 if (len > ntohs (h->len))
4103 len = ntohs (h->len);
4104 switch (h->type) {
4105 /* PAP request is my authproto */
4106 case PAP_REQ:
4107 name = 1 + (u_char*)(h+1);
4108 name_len = name[-1];
4109 passwd = name + name_len + 1;
4110 if (name_len > len - 6 ||
4111 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4112 if (debug) {
4113 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4114 "<%s id=0x%x len=%d",
4115 SPP_ARGS(ifp),
4116 sppp_auth_type_name(PPP_PAP, h->type),
4117 h->ident, ntohs(h->len));
4118 if (len > 4)
4119 sppp_print_bytes((u_char*)(h+1), len-4);
4120 addlog(">\n");
4121 }
4122 break;
4123 }
4124 if (debug) {
4125 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4126 "<%s id=0x%x len=%d name=",
4127 SPP_ARGS(ifp),
4128 sppp_state_name(sp->state[IDX_PAP]),
4129 sppp_auth_type_name(PPP_PAP, h->type),
4130 h->ident, ntohs(h->len));
4131 sppp_print_string((char*)name, name_len);
4132 addlog(" passwd=");
4133 sppp_print_string((char*)passwd, passwd_len);
4134 addlog(">\n");
4135 }
4136 if (name_len > AUTHNAMELEN ||
4137 passwd_len > AUTHKEYLEN ||
4138 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4139 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4140 /* action scn, tld */
4141 mlen = sizeof(FAILMSG) - 1;
4142 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4143 sizeof mlen, (const char *)&mlen,
4144 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4145 0);
4146 pap.tld(sp);
4147 break;
4148 }
4149 /* action sca, perhaps tlu */
4150 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4151 sp->state[IDX_PAP] == STATE_OPENED) {
4152 mlen = sizeof(SUCCMSG) - 1;
4153 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4154 sizeof mlen, (const char *)&mlen,
4155 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4156 0);
4157 }
4158 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4159 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4160 pap.tlu(sp);
4161 }
4162 break;
4163
4164 /* ack and nak are his authproto */
4165 case PAP_ACK:
4166 callout_stop(&sp->pap_my_to_ch);
4167 if (debug) {
4168 log(LOG_DEBUG, SPP_FMT "pap success",
4169 SPP_ARGS(ifp));
4170 name_len = *(char *)h;
4171 if (len > 5 && name_len) {
4172 addlog(": ");
4173 sppp_print_string((char*)(h+1), name_len);
4174 }
4175 addlog("\n");
4176 }
4177 x = splnet();
4178 sp->pp_flags &= ~PP_NEEDAUTH;
4179 if (sp->myauth.proto == PPP_PAP &&
4180 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4181 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4182 /*
4183 * We are authenticator for PAP but didn't
4184 * complete yet. Leave it to tlu to proceed
4185 * to network phase.
4186 */
4187 splx(x);
4188 break;
4189 }
4190 splx(x);
4191 sppp_phase_network(sp);
4192 break;
4193
4194 case PAP_NAK:
4195 callout_stop(&sp->pap_my_to_ch);
4196 if (debug) {
4197 log(LOG_INFO, SPP_FMT "pap failure",
4198 SPP_ARGS(ifp));
4199 name_len = *(char *)h;
4200 if (len > 5 && name_len) {
4201 addlog(": ");
4202 sppp_print_string((char*)(h+1), name_len);
4203 }
4204 addlog("\n");
4205 } else
4206 log(LOG_INFO, SPP_FMT "pap failure\n",
4207 SPP_ARGS(ifp));
4208 /* await LCP shutdown by authenticator */
4209 break;
4210
4211 default:
4212 /* Unknown PAP packet type -- ignore. */
4213 if (debug) {
4214 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4215 "<0x%x id=0x%x len=%d",
4216 SPP_ARGS(ifp),
4217 h->type, h->ident, ntohs(h->len));
4218 if (len > 4)
4219 sppp_print_bytes((u_char*)(h+1), len-4);
4220 addlog(">\n");
4221 }
4222 break;
4223
4224 }
4225 }
4226
4227 static void
4228 sppp_pap_init(struct sppp *sp)
4229 {
4230 /* PAP doesn't have STATE_INITIAL at all. */
4231 sp->state[IDX_PAP] = STATE_CLOSED;
4232 sp->fail_counter[IDX_PAP] = 0;
4233 sp->pp_seq[IDX_PAP] = 0;
4234 sp->pp_rseq[IDX_PAP] = 0;
4235 callout_init(&sp->ch[IDX_PAP]);
4236 callout_init(&sp->pap_my_to_ch);
4237 }
4238
4239 static void
4240 sppp_pap_open(struct sppp *sp)
4241 {
4242 if (sp->hisauth.proto == PPP_PAP &&
4243 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4244 /* we are authenticator for PAP, start our timer */
4245 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4246 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4247 }
4248 if (sp->myauth.proto == PPP_PAP) {
4249 /* we are peer, send a request, and start a timer */
4250 pap.scr(sp);
4251 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4252 sppp_pap_my_TO, sp);
4253 }
4254 }
4255
4256 static void
4257 sppp_pap_close(struct sppp *sp)
4258 {
4259 if (sp->state[IDX_PAP] != STATE_CLOSED)
4260 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4261 }
4262
4263 /*
4264 * That's the timeout routine if we are authenticator. Since the
4265 * authenticator is basically passive in PAP, we can't do much here.
4266 */
4267 static void
4268 sppp_pap_TO(void *cookie)
4269 {
4270 struct sppp *sp = (struct sppp *)cookie;
4271 STDDCL;
4272 int s;
4273
4274 s = splnet();
4275 if (debug)
4276 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4277 SPP_ARGS(ifp),
4278 sppp_state_name(sp->state[IDX_PAP]),
4279 sp->rst_counter[IDX_PAP]);
4280
4281 if (--sp->rst_counter[IDX_PAP] < 0)
4282 /* TO- event */
4283 switch (sp->state[IDX_PAP]) {
4284 case STATE_REQ_SENT:
4285 pap.tld(sp);
4286 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4287 break;
4288 }
4289 else
4290 /* TO+ event, not very much we could do */
4291 switch (sp->state[IDX_PAP]) {
4292 case STATE_REQ_SENT:
4293 /* sppp_cp_change_state() will restart the timer */
4294 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4295 break;
4296 }
4297
4298 splx(s);
4299 }
4300
4301 /*
4302 * That's the timeout handler if we are peer. Since the peer is active,
4303 * we need to retransmit our PAP request since it is apparently lost.
4304 * XXX We should impose a max counter.
4305 */
4306 static void
4307 sppp_pap_my_TO(void *cookie)
4308 {
4309 struct sppp *sp = (struct sppp *)cookie;
4310 STDDCL;
4311
4312 if (debug)
4313 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4314 SPP_ARGS(ifp));
4315
4316 pap.scr(sp);
4317 }
4318
4319 static void
4320 sppp_pap_tlu(struct sppp *sp)
4321 {
4322 STDDCL;
4323 int x;
4324
4325 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4326
4327 if (debug)
4328 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4329 SPP_ARGS(ifp), pap.name);
4330
4331 x = splnet();
4332 /* indicate to LCP that we need to be closed down */
4333 sp->lcp.protos |= (1 << IDX_PAP);
4334
4335 if (sp->pp_flags & PP_NEEDAUTH) {
4336 /*
4337 * Remote is authenticator, but his auth proto didn't
4338 * complete yet. Defer the transition to network
4339 * phase.
4340 */
4341 splx(x);
4342 return;
4343 }
4344 splx(x);
4345 sppp_phase_network(sp);
4346 }
4347
4348 static void
4349 sppp_pap_tld(struct sppp *sp)
4350 {
4351 STDDCL;
4352
4353 if (debug)
4354 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4355 callout_stop(&sp->ch[IDX_PAP]);
4356 callout_stop(&sp->pap_my_to_ch);
4357 sp->lcp.protos &= ~(1 << IDX_PAP);
4358
4359 lcp.Close(sp);
4360 }
4361
4362 static void
4363 sppp_pap_scr(struct sppp *sp)
4364 {
4365 u_char idlen, pwdlen;
4366
4367 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4368 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4369 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4370
4371 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4372 sizeof idlen, (const char *)&idlen,
4373 (size_t)idlen, sp->myauth.name,
4374 sizeof pwdlen, (const char *)&pwdlen,
4375 (size_t)pwdlen, sp->myauth.secret,
4376 0);
4377 }
4378 /*
4380 * Random miscellaneous functions.
4381 */
4382
4383 /*
4384 * Send a PAP or CHAP proto packet.
4385 *
4386 * Varadic function, each of the elements for the ellipsis is of type
4387 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4388 * mlen == 0.
4389 * NOTE: never declare variadic functions with types subject to type
4390 * promotion (i.e. u_char). This is asking for big trouble depending
4391 * on the architecture you are on...
4392 */
4393
4394 static void
4395 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4396 unsigned int type, unsigned int id,
4397 ...)
4398 {
4399 STDDCL;
4400 struct lcp_header *lh;
4401 struct mbuf *m;
4402 u_char *p;
4403 int len;
4404 size_t pkthdrlen;
4405 unsigned int mlen;
4406 const char *msg;
4407 va_list ap;
4408
4409 MGETHDR (m, M_DONTWAIT, MT_DATA);
4410 if (! m)
4411 return;
4412 m->m_pkthdr.rcvif = 0;
4413
4414 if (sp->pp_flags & PP_NOFRAMING) {
4415 *mtod(m, u_int16_t*) = htons(cp->proto);
4416 pkthdrlen = 2;
4417 lh = (struct lcp_header*)(mtod(m, u_int8_t*)+2);
4418 } else {
4419 struct ppp_header *h;
4420 h = mtod (m, struct ppp_header*);
4421 h->address = PPP_ALLSTATIONS; /* broadcast address */
4422 h->control = PPP_UI; /* Unnumbered Info */
4423 h->protocol = htons(cp->proto);
4424 pkthdrlen = PPP_HEADER_LEN;
4425
4426 lh = (struct lcp_header*)(h + 1);
4427 }
4428
4429 lh->type = type;
4430 lh->ident = id;
4431 p = (u_char*) (lh+1);
4432
4433 va_start(ap, id);
4434 len = 0;
4435
4436 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4437 msg = va_arg(ap, const char *);
4438 len += mlen;
4439 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
4440 va_end(ap);
4441 m_freem(m);
4442 return;
4443 }
4444
4445 bcopy(msg, p, mlen);
4446 p += mlen;
4447 }
4448 va_end(ap);
4449
4450 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
4451 lh->len = htons (LCP_HEADER_LEN + len);
4452
4453 if (debug) {
4454 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4455 SPP_ARGS(ifp), cp->name,
4456 sppp_auth_type_name(cp->proto, lh->type),
4457 lh->ident, ntohs(lh->len));
4458 if (len)
4459 sppp_print_bytes((u_char*) (lh+1), len);
4460 addlog(">\n");
4461 }
4462 if (IF_QFULL (&sp->pp_cpq)) {
4463 IF_DROP (&sp->pp_fastq);
4464 IF_DROP (&ifp->if_snd);
4465 m_freem (m);
4466 ++ifp->if_oerrors;
4467 } else
4468 IF_ENQUEUE (&sp->pp_cpq, m);
4469 if (! (ifp->if_flags & IFF_OACTIVE))
4470 (*ifp->if_start) (ifp);
4471 ifp->if_obytes += m->m_pkthdr.len + 3;
4472 }
4473
4474 /*
4475 * Send keepalive packets, every 10 seconds.
4476 */
4477 static void
4478 sppp_keepalive(void *dummy)
4479 {
4480 struct sppp *sp;
4481 int s;
4482
4483 s = splnet();
4484 for (sp=spppq; sp; sp=sp->pp_next) {
4485 struct ifnet *ifp = &sp->pp_if;
4486
4487 /* Keepalive mode disabled or channel down? */
4488 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4489 ! (ifp->if_flags & IFF_RUNNING))
4490 continue;
4491
4492 /* No keepalive in PPP mode if LCP not opened yet. */
4493 if (! (sp->pp_flags & PP_CISCO) &&
4494 sp->pp_phase < PHASE_AUTHENTICATE)
4495 continue;
4496
4497 if (sp->pp_alivecnt == MAXALIVECNT) {
4498 /* No keepalive packets got. Stop the interface. */
4499 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4500 if_down (ifp);
4501 IF_PURGE (&sp->pp_cpq);
4502 if (! (sp->pp_flags & PP_CISCO)) {
4503 printf("%s: LCP keepalive timed out, going to restart the connection\n",
4504 ifp->if_xname);
4505 sp->pp_alivecnt = 0;
4506
4507 /* we are down, close all open protocols */
4508 lcp.Close(sp);
4509
4510 /* And now prepare LCP to reestablish the link, if configured to do so. */
4511 sppp_cp_change_state(&lcp, sp, STATE_STOPPED);
4512
4513 /* Close connection imediatly, completition of this
4514 * will summon the magic needed to reestablish it. */
4515 sp->pp_tlf(sp);
4516 continue;
4517 }
4518 }
4519 if (sp->pp_alivecnt <= MAXALIVECNT)
4520 ++sp->pp_alivecnt;
4521 if (sp->pp_flags & PP_CISCO)
4522 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4523 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4524 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4525 int32_t nmagic = htonl (sp->lcp.magic);
4526 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4527 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4528 sp->lcp.echoid, 4, &nmagic);
4529 }
4530 }
4531 splx(s);
4532 callout_reset(&keepalive_ch, hz * 10, sppp_keepalive, NULL);
4533 }
4534
4535 /*
4536 * Get both IP addresses.
4537 */
4538 static void
4539 sppp_get_ip_addrs(struct sppp *sp, u_int32_t *src, u_int32_t *dst, u_int32_t *srcmask)
4540 {
4541 struct ifnet *ifp = &sp->pp_if;
4542 struct ifaddr *ifa;
4543 struct sockaddr_in *si, *sm;
4544 u_int32_t ssrc, ddst;
4545
4546 sm = NULL;
4547 ssrc = ddst = 0;
4548 /*
4549 * Pick the first AF_INET address from the list,
4550 * aliases don't make any sense on a p2p link anyway.
4551 */
4552 si = 0;
4553 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
4554 if (ifa->ifa_addr->sa_family == AF_INET) {
4555 si = (struct sockaddr_in *)ifa->ifa_addr;
4556 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4557 if (si)
4558 break;
4559 }
4560 }
4561 if (ifa) {
4562 if (si && si->sin_addr.s_addr) {
4563 ssrc = si->sin_addr.s_addr;
4564 if (srcmask)
4565 *srcmask = ntohl(sm->sin_addr.s_addr);
4566 }
4567
4568 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4569 if (si && si->sin_addr.s_addr)
4570 ddst = si->sin_addr.s_addr;
4571 }
4572
4573 if (dst) *dst = ntohl(ddst);
4574 if (src) *src = ntohl(ssrc);
4575 }
4576
4577 /*
4578 * Set IP addresses. Must be called at splnet.
4579 * If an address is 0, leave it the way it is.
4580 */
4581 static void
4582 sppp_set_ip_addrs(struct sppp *sp, u_int32_t myaddr, u_int32_t hisaddr)
4583 {
4584 STDDCL;
4585 struct ifaddr *ifa;
4586 struct sockaddr_in *si;
4587 struct sockaddr_in *dest;
4588
4589 /*
4590 * Pick the first AF_INET address from the list,
4591 * aliases don't make any sense on a p2p link anyway.
4592 */
4593
4594 si = 0;
4595 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
4596 {
4597 if (ifa->ifa_addr->sa_family == AF_INET)
4598 {
4599 si = (struct sockaddr_in *)ifa->ifa_addr;
4600 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4601 if (si)
4602 break;
4603 }
4604 }
4605
4606 if (ifa && si)
4607 {
4608 int error;
4609 struct sockaddr_in new_sin = *si;
4610 struct sockaddr_in new_dst = *dest;
4611
4612 /*
4613 * Scrub old routes now instead of calling in_ifinit with
4614 * scrub=1, because we may change the dstaddr
4615 * before the call to in_ifinit.
4616 */
4617 in_ifscrub(ifp, ifatoia(ifa));
4618
4619 if (myaddr != 0)
4620 new_sin.sin_addr.s_addr = htonl(myaddr);
4621 if (hisaddr != 0) {
4622 new_dst.sin_addr.s_addr = htonl(hisaddr);
4623 if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr) {
4624 sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
4625 *dest = new_dst; /* fix dstaddr in place */
4626 }
4627 }
4628 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4629 if(debug && error)
4630 {
4631 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addrs: in_ifinit "
4632 " failed, error=%d\n", SPP_ARGS(ifp), error);
4633 }
4634 }
4635 }
4636
4637 /*
4638 * Clear IP addresses. Must be called at splnet.
4639 */
4640 static void
4641 sppp_clear_ip_addrs(struct sppp *sp)
4642 {
4643 struct ifnet *ifp = &sp->pp_if;
4644 struct ifaddr *ifa;
4645 struct sockaddr_in *si;
4646 struct sockaddr_in *dest;
4647
4648 u_int32_t remote;
4649 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4650 remote = sp->ipcp.saved_hisaddr;
4651 else
4652 sppp_get_ip_addrs(sp, 0, &remote, 0);
4653
4654 /*
4655 * Pick the first AF_INET address from the list,
4656 * aliases don't make any sense on a p2p link anyway.
4657 */
4658
4659 si = 0;
4660 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
4661 {
4662 if (ifa->ifa_addr->sa_family == AF_INET)
4663 {
4664 si = (struct sockaddr_in *)ifa->ifa_addr;
4665 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
4666 if (si)
4667 break;
4668 }
4669 }
4670
4671 if (ifa && si)
4672 {
4673 struct sockaddr_in new_sin = *si;
4674
4675 in_ifscrub(ifp, ifatoia(ifa));
4676 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
4677 new_sin.sin_addr.s_addr = 0;
4678 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
4679 /* replace peer addr in place */
4680 dest->sin_addr.s_addr = sp->ipcp.saved_hisaddr;
4681 in_ifinit(ifp, ifatoia(ifa), &new_sin, 0);
4682 }
4683 }
4684
4685 #ifdef INET6
4686 /*
4687 * Get both IPv6 addresses.
4688 */
4689 static void
4690 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4691 struct in6_addr *srcmask)
4692 {
4693 struct ifnet *ifp = &sp->pp_if;
4694 struct ifaddr *ifa;
4695 struct sockaddr_in6 *si, *sm;
4696 struct in6_addr ssrc, ddst;
4697
4698 sm = NULL;
4699 memset(&ssrc, 0, sizeof(ssrc));
4700 memset(&ddst, 0, sizeof(ddst));
4701 /*
4702 * Pick the first link-local AF_INET6 address from the list,
4703 * aliases don't make any sense on a p2p link anyway.
4704 */
4705 si = 0;
4706 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
4707 if (ifa->ifa_addr->sa_family == AF_INET6) {
4708 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4709 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4710 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4711 break;
4712 }
4713 if (ifa) {
4714 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4715 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4716 if (srcmask) {
4717 bcopy(&sm->sin6_addr, srcmask,
4718 sizeof(*srcmask));
4719 }
4720 }
4721
4722 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4723 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4724 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4725 }
4726
4727 if (dst)
4728 bcopy(&ddst, dst, sizeof(*dst));
4729 if (src)
4730 bcopy(&ssrc, src, sizeof(*src));
4731 }
4732
4733 #ifdef IPV6CP_MYIFID_DYN
4734 /*
4735 * Generate random ifid.
4736 */
4737 static void
4738 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4739 {
4740 /* TBD */
4741 }
4742
4743 /*
4744 * Set my IPv6 address. Must be called at splnet.
4745 */
4746 static void
4747 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4748 {
4749 STDDCL;
4750 struct ifaddr *ifa;
4751 struct sockaddr_in6 *sin6;
4752
4753 /*
4754 * Pick the first link-local AF_INET6 address from the list,
4755 * aliases don't make any sense on a p2p link anyway.
4756 */
4757
4758 sin6 = NULL;
4759 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
4760 {
4761 if (ifa->ifa_addr->sa_family == AF_INET6)
4762 {
4763 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4764 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4765 break;
4766 }
4767 }
4768
4769 if (ifa && sin6)
4770 {
4771 int error;
4772 struct sockaddr_in6 new_sin6 = *sin6;
4773
4774 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4775 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4776 if (debug && error)
4777 {
4778 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
4779 " failed, error=%d\n", SPP_ARGS(ifp), error);
4780 }
4781 }
4782 }
4783 #endif
4784
4785 /*
4786 * Suggest a candidate address to be used by peer.
4787 */
4788 static void
4789 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4790 {
4791 struct in6_addr myaddr;
4792 struct timeval tv;
4793
4794 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4795
4796 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4797 microtime(&tv);
4798 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4799 myaddr.s6_addr[14] ^= 0xff;
4800 myaddr.s6_addr[15] ^= 0xff;
4801 } else {
4802 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4803 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
4804 }
4805 if (suggest)
4806 bcopy(&myaddr, suggest, sizeof(myaddr));
4807 }
4808 #endif /*INET6*/
4809
4810 static int
4811 sppp_params(struct sppp *sp, int cmd, void *data)
4812 {
4813 struct ifreq *ifr = (struct ifreq *)data;
4814 struct spppreq spr;
4815
4816 #if 0
4817 /*
4818 * ifr->ifr_data is supposed to point to a struct spppreq.
4819 * Check the cmd word first before attempting to fetch all the
4820 * data.
4821 */
4822 if ((subcmd = fuword(ifr->ifr_data)) == -1)
4823 return EFAULT;
4824 #endif
4825
4826 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
4827 return EFAULT;
4828
4829 switch (spr.cmd) {
4830 case SPPPIOGDEFS:
4831 if (cmd != (int)SIOCGIFGENERIC)
4832 return EINVAL;
4833 /*
4834 * We copy over the entire current state, but clean
4835 * out some of the stuff we don't wanna pass up.
4836 * Remember, SIOCGIFGENERIC is unprotected, and can be
4837 * called by any user. No need to ever get PAP or
4838 * CHAP secrets back to userland anyway.
4839 */
4840 bcopy(sp, &spr.defs, sizeof(struct sppp));
4841 memset(spr.defs.myauth.secret, 0, AUTHKEYLEN);
4842 memset(spr.defs.myauth.challenge, 0, AUTHKEYLEN);
4843 memset(spr.defs.hisauth.secret, 0, AUTHKEYLEN);
4844 memset(spr.defs.hisauth.challenge, 0, AUTHKEYLEN);
4845 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
4846
4847 case SPPPIOSDEFS:
4848 if (cmd != (int)SIOCSIFGENERIC)
4849 return EINVAL;
4850 /*
4851 * We have a very specific idea of which fields we allow
4852 * being passed back from userland, so to not clobber our
4853 * current state. For one, we only allow setting
4854 * anything if LCP is in dead phase. Once the LCP
4855 * negotiations started, the authentication settings must
4856 * not be changed again. (The administrator can force an
4857 * ifconfig down in order to get LCP back into dead
4858 * phase.)
4859 *
4860 * Also, we only allow for authentication parameters to be
4861 * specified.
4862 *
4863 * XXX Should allow to set or clear pp_flags.
4864 *
4865 * Finally, if the respective authentication protocol to
4866 * be used is set differently than 0, but the secret is
4867 * passed as all zeros, we don't trash the existing secret.
4868 * This allows an administrator to change the system name
4869 * only without clobbering the secret (which he didn't get
4870 * back in a previous SPPPIOGDEFS call). However, the
4871 * secrets are cleared if the authentication protocol is
4872 * reset to 0.
4873 */
4874 if (sp->pp_phase != PHASE_DEAD)
4875 return EBUSY;
4876
4877 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
4878 spr.defs.myauth.proto != PPP_CHAP) ||
4879 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
4880 spr.defs.hisauth.proto != PPP_CHAP))
4881 return EINVAL;
4882
4883 if (spr.defs.myauth.proto == 0)
4884 /* resetting myauth */
4885 memset(&sp->myauth, 0, sizeof sp->myauth);
4886 else {
4887 /* setting/changing myauth */
4888 sp->myauth.proto = spr.defs.myauth.proto;
4889 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
4890 if (spr.defs.myauth.secret[0] != '\0')
4891 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
4892 AUTHKEYLEN);
4893 }
4894 if (spr.defs.hisauth.proto == 0)
4895 /* resetting hisauth */
4896 memset(&sp->hisauth, 0, sizeof sp->hisauth);
4897 else {
4898 /* setting/changing hisauth */
4899 sp->hisauth.proto = spr.defs.hisauth.proto;
4900 sp->hisauth.flags = spr.defs.hisauth.flags;
4901 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
4902 if (spr.defs.hisauth.secret[0] != '\0')
4903 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
4904 AUTHKEYLEN);
4905 }
4906 break;
4907
4908 default:
4909 return EINVAL;
4910 }
4911
4912 return 0;
4913 }
4914
4915 static void
4916 sppp_phase_network(struct sppp *sp)
4917 {
4918 STDDCL;
4919 int i;
4920 u_int32_t mask;
4921
4922 sp->pp_phase = PHASE_NETWORK;
4923
4924 if(debug)
4925 {
4926 log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
4927 sppp_phase_name(sp->pp_phase));
4928 }
4929
4930 /* Notify NCPs now. */
4931 for (i = 0; i < IDX_COUNT; i++)
4932 if ((cps[i])->flags & CP_NCP)
4933 (cps[i])->Open(sp);
4934
4935 /* Send Up events to all NCPs. */
4936 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
4937 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
4938 (cps[i])->Up(sp);
4939
4940 /* if no NCP is starting, all this was in vain, close down */
4941 sppp_lcp_check_and_close(sp);
4942 }
4943
4944
4945 static const char *
4946 sppp_cp_type_name(u_char type)
4947 {
4948 static char buf[12];
4949 switch (type) {
4950 case CONF_REQ: return "conf-req";
4951 case CONF_ACK: return "conf-ack";
4952 case CONF_NAK: return "conf-nak";
4953 case CONF_REJ: return "conf-rej";
4954 case TERM_REQ: return "term-req";
4955 case TERM_ACK: return "term-ack";
4956 case CODE_REJ: return "code-rej";
4957 case PROTO_REJ: return "proto-rej";
4958 case ECHO_REQ: return "echo-req";
4959 case ECHO_REPLY: return "echo-reply";
4960 case DISC_REQ: return "discard-req";
4961 }
4962 sprintf (buf, "0x%x", type);
4963 return buf;
4964 }
4965
4966 static const char *
4967 sppp_auth_type_name(u_short proto, u_char type)
4968 {
4969 static char buf[12];
4970 switch (proto) {
4971 case PPP_CHAP:
4972 switch (type) {
4973 case CHAP_CHALLENGE: return "challenge";
4974 case CHAP_RESPONSE: return "response";
4975 case CHAP_SUCCESS: return "success";
4976 case CHAP_FAILURE: return "failure";
4977 }
4978 case PPP_PAP:
4979 switch (type) {
4980 case PAP_REQ: return "req";
4981 case PAP_ACK: return "ack";
4982 case PAP_NAK: return "nak";
4983 }
4984 }
4985 sprintf (buf, "0x%x", type);
4986 return buf;
4987 }
4988
4989 static const char *
4990 sppp_lcp_opt_name(u_char opt)
4991 {
4992 static char buf[12];
4993 switch (opt) {
4994 case LCP_OPT_MRU: return "mru";
4995 case LCP_OPT_ASYNC_MAP: return "async-map";
4996 case LCP_OPT_AUTH_PROTO: return "auth-proto";
4997 case LCP_OPT_QUAL_PROTO: return "qual-proto";
4998 case LCP_OPT_MAGIC: return "magic";
4999 case LCP_OPT_PROTO_COMP: return "proto-comp";
5000 case LCP_OPT_ADDR_COMP: return "addr-comp";
5001 }
5002 sprintf (buf, "0x%x", opt);
5003 return buf;
5004 }
5005
5006 static const char *
5007 sppp_ipcp_opt_name(u_char opt)
5008 {
5009 static char buf[12];
5010 switch (opt) {
5011 case IPCP_OPT_ADDRESSES: return "addresses";
5012 case IPCP_OPT_COMPRESSION: return "compression";
5013 case IPCP_OPT_ADDRESS: return "address";
5014 }
5015 sprintf (buf, "0x%x", opt);
5016 return buf;
5017 }
5018
5019 #ifdef INET6
5020 static const char *
5021 sppp_ipv6cp_opt_name(u_char opt)
5022 {
5023 static char buf[12];
5024 switch (opt) {
5025 case IPV6CP_OPT_IFID: return "ifid";
5026 case IPV6CP_OPT_COMPRESSION: return "compression";
5027 }
5028 sprintf (buf, "0x%x", opt);
5029 return buf;
5030 }
5031 #endif
5032
5033 static const char *
5034 sppp_state_name(int state)
5035 {
5036 switch (state) {
5037 case STATE_INITIAL: return "initial";
5038 case STATE_STARTING: return "starting";
5039 case STATE_CLOSED: return "closed";
5040 case STATE_STOPPED: return "stopped";
5041 case STATE_CLOSING: return "closing";
5042 case STATE_STOPPING: return "stopping";
5043 case STATE_REQ_SENT: return "req-sent";
5044 case STATE_ACK_RCVD: return "ack-rcvd";
5045 case STATE_ACK_SENT: return "ack-sent";
5046 case STATE_OPENED: return "opened";
5047 }
5048 return "illegal";
5049 }
5050
5051 static const char *
5052 sppp_phase_name(enum ppp_phase phase)
5053 {
5054 switch (phase) {
5055 case PHASE_DEAD: return "dead";
5056 case PHASE_ESTABLISH: return "establish";
5057 case PHASE_TERMINATE: return "terminate";
5058 case PHASE_AUTHENTICATE: return "authenticate";
5059 case PHASE_NETWORK: return "network";
5060 }
5061 return "illegal";
5062 }
5063
5064 static const char *
5065 sppp_proto_name(u_short proto)
5066 {
5067 static char buf[12];
5068 switch (proto) {
5069 case PPP_LCP: return "lcp";
5070 case PPP_IPCP: return "ipcp";
5071 case PPP_PAP: return "pap";
5072 case PPP_CHAP: return "chap";
5073 case PPP_IPV6CP: return "ipv6cp";
5074 }
5075 sprintf(buf, "0x%x", (unsigned)proto);
5076 return buf;
5077 }
5078
5079 static void
5080 sppp_print_bytes(const u_char *p, u_short len)
5081 {
5082 addlog(" %02x", *p++);
5083 while (--len > 0)
5084 addlog("-%02x", *p++);
5085 }
5086
5087 static void
5088 sppp_print_string(const char *p, u_short len)
5089 {
5090 u_char c;
5091
5092 while (len-- > 0) {
5093 c = *p++;
5094 /*
5095 * Print only ASCII chars directly. RFC 1994 recommends
5096 * using only them, but we don't rely on it. */
5097 if (c < ' ' || c > '~')
5098 addlog("\\x%x", c);
5099 else
5100 addlog("%c", c);
5101 }
5102 }
5103
5104 static const char *
5105 sppp_dotted_quad(u_int32_t addr)
5106 {
5107 static char s[16];
5108 sprintf(s, "%d.%d.%d.%d",
5109 (int)((addr >> 24) & 0xff),
5110 (int)((addr >> 16) & 0xff),
5111 (int)((addr >> 8) & 0xff),
5112 (int)(addr & 0xff));
5113 return s;
5114 }
5115
5116 static int
5117 sppp_strnlen(u_char *p, int max)
5118 {
5119 int len;
5120
5121 for (len = 0; len < max && *p; ++p)
5122 ++len;
5123 return len;
5124 }
5125
5126 /* a dummy, used to drop uninteresting events */
5127 static void
5128 sppp_null(struct sppp *unused)
5129 {
5130 /* do just nothing */
5131 }
5132 /*
5133 * This file is large. Tell emacs to highlight it nevertheless.
5134 *
5135 * Local Variables:
5136 * hilit-auto-highlight-maxout: 120000
5137 * End:
5138 */
5139