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