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