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