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