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