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