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