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