1 /* 2 * ng_pppoe.c 3 */ 4 5 /*- 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Julian Elischer <julian@freebsd.org> 39 * 40 * $FreeBSD$ 41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/ktr.h> 48 #include <sys/mbuf.h> 49 #include <sys/malloc.h> 50 #include <sys/errno.h> 51 #include <sys/syslog.h> 52 #include <net/ethernet.h> 53 54 #include <netgraph/ng_message.h> 55 #include <netgraph/netgraph.h> 56 #include <netgraph/ng_parse.h> 57 #include <netgraph/ng_pppoe.h> 58 #include <netgraph/ng_ether.h> 59 60 #ifdef NG_SEPARATE_MALLOC 61 MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node"); 62 #else 63 #define M_NETGRAPH_PPPOE M_NETGRAPH 64 #endif 65 66 #define SIGNOFF "session closed" 67 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 68 69 /* 70 * This section contains the netgraph method declarations for the 71 * pppoe node. These methods define the netgraph pppoe 'type'. 72 */ 73 74 static ng_constructor_t ng_pppoe_constructor; 75 static ng_rcvmsg_t ng_pppoe_rcvmsg; 76 static ng_shutdown_t ng_pppoe_shutdown; 77 static ng_newhook_t ng_pppoe_newhook; 78 static ng_connect_t ng_pppoe_connect; 79 static ng_rcvdata_t ng_pppoe_rcvdata; 80 static ng_rcvdata_t ng_pppoe_rcvdata_ether; 81 static ng_rcvdata_t ng_pppoe_rcvdata_debug; 82 static ng_disconnect_t ng_pppoe_disconnect; 83 84 /* Parse type for struct ngpppoe_init_data */ 85 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[] 86 = NG_PPPOE_INIT_DATA_TYPE_INFO; 87 static const struct ng_parse_type ngpppoe_init_data_state_type = { 88 &ng_parse_struct_type, 89 &ngpppoe_init_data_type_fields 90 }; 91 92 /* Parse type for struct ngpppoe_sts */ 93 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[] 94 = NG_PPPOE_STS_TYPE_INFO; 95 static const struct ng_parse_type ng_pppoe_sts_state_type = { 96 &ng_parse_struct_type, 97 &ng_pppoe_sts_type_fields 98 }; 99 100 /* List of commands and how to convert arguments to/from ASCII */ 101 static const struct ng_cmdlist ng_pppoe_cmds[] = { 102 { 103 NGM_PPPOE_COOKIE, 104 NGM_PPPOE_CONNECT, 105 "pppoe_connect", 106 &ngpppoe_init_data_state_type, 107 NULL 108 }, 109 { 110 NGM_PPPOE_COOKIE, 111 NGM_PPPOE_LISTEN, 112 "pppoe_listen", 113 &ngpppoe_init_data_state_type, 114 NULL 115 }, 116 { 117 NGM_PPPOE_COOKIE, 118 NGM_PPPOE_OFFER, 119 "pppoe_offer", 120 &ngpppoe_init_data_state_type, 121 NULL 122 }, 123 { 124 NGM_PPPOE_COOKIE, 125 NGM_PPPOE_SERVICE, 126 "pppoe_service", 127 &ngpppoe_init_data_state_type, 128 NULL 129 }, 130 { 131 NGM_PPPOE_COOKIE, 132 NGM_PPPOE_SUCCESS, 133 "pppoe_success", 134 &ng_pppoe_sts_state_type, 135 NULL 136 }, 137 { 138 NGM_PPPOE_COOKIE, 139 NGM_PPPOE_FAIL, 140 "pppoe_fail", 141 &ng_pppoe_sts_state_type, 142 NULL 143 }, 144 { 145 NGM_PPPOE_COOKIE, 146 NGM_PPPOE_CLOSE, 147 "pppoe_close", 148 &ng_pppoe_sts_state_type, 149 NULL 150 }, 151 { 152 NGM_PPPOE_COOKIE, 153 NGM_PPPOE_SETMODE, 154 "pppoe_setmode", 155 &ng_parse_string_type, 156 NULL 157 }, 158 { 159 NGM_PPPOE_COOKIE, 160 NGM_PPPOE_GETMODE, 161 "pppoe_getmode", 162 NULL, 163 &ng_parse_string_type 164 }, 165 { 166 NGM_PPPOE_COOKIE, 167 NGM_PPPOE_SETENADDR, 168 "setenaddr", 169 &ng_parse_enaddr_type, 170 NULL 171 }, 172 { 0 } 173 }; 174 175 /* Netgraph node type descriptor */ 176 static struct ng_type typestruct = { 177 .version = NG_ABI_VERSION, 178 .name = NG_PPPOE_NODE_TYPE, 179 .constructor = ng_pppoe_constructor, 180 .rcvmsg = ng_pppoe_rcvmsg, 181 .shutdown = ng_pppoe_shutdown, 182 .newhook = ng_pppoe_newhook, 183 .connect = ng_pppoe_connect, 184 .rcvdata = ng_pppoe_rcvdata, 185 .disconnect = ng_pppoe_disconnect, 186 .cmdlist = ng_pppoe_cmds, 187 }; 188 NETGRAPH_INIT(pppoe, &typestruct); 189 190 /* 191 * States for the session state machine. 192 * These have no meaning if there is no hook attached yet. 193 */ 194 enum state { 195 PPPOE_SNONE=0, /* [both] Initial state */ 196 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */ 197 PPPOE_SINIT, /* [Client] Sent discovery initiation */ 198 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */ 199 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/ 200 PPPOE_SREQ, /* [Client] Sent a Request */ 201 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */ 202 PPPOE_CONNECTED, /* [Both] Connection established, Data received */ 203 PPPOE_DEAD /* [Both] */ 204 }; 205 206 #define NUMTAGS 20 /* number of tags we are set up to work with */ 207 208 /* 209 * Information we store for each hook on each node for negotiating the 210 * session. The mbuf and cluster are freed once negotiation has completed. 211 * The whole negotiation block is then discarded. 212 */ 213 214 struct sess_neg { 215 struct mbuf *m; /* holds cluster with last sent packet */ 216 union packet *pkt; /* points within the above cluster */ 217 struct callout handle; /* see timeout(9) */ 218 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */ 219 u_int numtags; 220 const struct pppoe_tag *tags[NUMTAGS]; 221 u_int service_len; 222 u_int ac_name_len; 223 224 struct datatag service; 225 struct datatag ac_name; 226 }; 227 typedef struct sess_neg *negp; 228 229 /* 230 * Session information that is needed after connection. 231 */ 232 struct sess_con { 233 hook_p hook; 234 uint16_t Session_ID; 235 enum state state; 236 ng_ID_t creator; /* who to notify */ 237 struct pppoe_full_hdr pkt_hdr; /* used when connected */ 238 negp neg; /* used when negotiating */ 239 }; 240 typedef struct sess_con *sessp; 241 242 /* 243 * Information we store for each node 244 */ 245 struct PPPoE { 246 node_p node; /* back pointer to node */ 247 hook_p ethernet_hook; 248 hook_p debug_hook; 249 u_int packets_in; /* packets in from ethernet */ 250 u_int packets_out; /* packets out towards ethernet */ 251 uint32_t flags; 252 #define COMPAT_3COM 0x00000001 253 #define COMPAT_DLINK 0x00000002 254 struct ether_header eh; 255 }; 256 typedef struct PPPoE *priv_p; 257 258 union uniq { 259 char bytes[sizeof(void *)]; 260 void *pointer; 261 }; 262 263 #define LEAVE(x) do { error = x; goto quit; } while(0) 264 static void pppoe_start(sessp sp); 265 static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2); 266 static const struct pppoe_tag *scan_tags(sessp sp, 267 const struct pppoe_hdr* ph); 268 static int pppoe_send_event(sessp sp, enum cmd cmdid); 269 270 /************************************************************************* 271 * Some basic utilities from the Linux version with author's permission.* 272 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> * 273 ************************************************************************/ 274 275 /* 276 * Generate a new session id 277 * XXX find out the FreeBSD locking scheme. 278 */ 279 static uint16_t 280 get_new_sid(node_p node) 281 { 282 static int pppoe_sid = 10; 283 hook_p hook; 284 uint16_t val; 285 286 restart: 287 val = pppoe_sid++; 288 /* 289 * Spec says 0xFFFF is reserved. 290 * Also don't use 0x0000 291 */ 292 if (val == 0xffff) { 293 pppoe_sid = 20; 294 goto restart; 295 } 296 297 /* Check it isn't already in use. */ 298 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 299 sessp sp = NG_HOOK_PRIVATE(hook); 300 301 /* Skip any nonsession hook. */ 302 if (sp == NULL) 303 continue; 304 if (sp->Session_ID == val) 305 goto restart; 306 } 307 308 CTR2(KTR_NET, "%20s: new sid %d", __func__, val); 309 310 return (val); 311 } 312 313 314 /* 315 * Return the location where the next tag can be put 316 */ 317 static __inline const struct pppoe_tag* 318 next_tag(const struct pppoe_hdr* ph) 319 { 320 return (const struct pppoe_tag*)(((const char*)&ph->tag[0]) 321 + ntohs(ph->length)); 322 } 323 324 /* 325 * Look for a tag of a specific type. 326 * Don't trust any length the other end says, 327 * but assume we already sanity checked ph->length. 328 */ 329 static const struct pppoe_tag* 330 get_tag(const struct pppoe_hdr* ph, uint16_t idx) 331 { 332 const char *const end = (const char *)next_tag(ph); 333 const struct pppoe_tag *pt = &ph->tag[0]; 334 const char *ptn; 335 336 /* 337 * Keep processing tags while a tag header will still fit. 338 */ 339 while((const char*)(pt + 1) <= end) { 340 /* 341 * If the tag data would go past the end of the packet, abort. 342 */ 343 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len)); 344 if (ptn > end) { 345 CTR2(KTR_NET, "%20s: invalid length for tag %d", 346 __func__, idx); 347 return (NULL); 348 } 349 if (pt->tag_type == idx) { 350 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx); 351 return (pt); 352 } 353 354 pt = (const struct pppoe_tag*)ptn; 355 } 356 357 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx); 358 return (NULL); 359 } 360 361 /************************************************************************** 362 * Inlines to initialise or add tags to a session's tag list. 363 **************************************************************************/ 364 /* 365 * Initialise the session's tag list. 366 */ 367 static void 368 init_tags(sessp sp) 369 { 370 KASSERT(sp->neg != NULL, ("%s: no neg", __func__)); 371 sp->neg->numtags = 0; 372 } 373 374 static void 375 insert_tag(sessp sp, const struct pppoe_tag *tp) 376 { 377 negp neg = sp->neg; 378 int i; 379 380 KASSERT(neg != NULL, ("%s: no neg", __func__)); 381 if ((i = neg->numtags++) < NUMTAGS) { 382 neg->tags[i] = tp; 383 } else { 384 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to " 385 "packet\n"); 386 neg->numtags--; 387 } 388 } 389 390 /* 391 * Make up a packet, using the tags filled out for the session. 392 * 393 * Assume that the actual pppoe header and ethernet header 394 * are filled out externally to this routine. 395 * Also assume that neg->wh points to the correct 396 * location at the front of the buffer space. 397 */ 398 static void 399 make_packet(sessp sp) { 400 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header; 401 const struct pppoe_tag **tag; 402 char *dp; 403 int count; 404 int tlen; 405 uint16_t length = 0; 406 407 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL), 408 ("%s: called from wrong state", __func__)); 409 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 410 411 dp = (char *)wh->ph.tag; 412 for (count = 0, tag = sp->neg->tags; 413 ((count < sp->neg->numtags) && (count < NUMTAGS)); 414 tag++, count++) { 415 tlen = ntohs((*tag)->tag_len) + sizeof(**tag); 416 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) { 417 log(LOG_NOTICE, "ng_pppoe: tags too long\n"); 418 sp->neg->numtags = count; 419 break; /* XXX chop off what's too long */ 420 } 421 bcopy(*tag, (char *)dp, tlen); 422 length += tlen; 423 dp += tlen; 424 } 425 wh->ph.length = htons(length); 426 sp->neg->m->m_len = length + sizeof(*wh); 427 sp->neg->m->m_pkthdr.len = length + sizeof(*wh); 428 } 429 430 /************************************************************************** 431 * Routines to match a service. * 432 **************************************************************************/ 433 434 /* 435 * Find a hook that has a service string that matches that 436 * we are seeking. For now use a simple string. 437 * In the future we may need something like regexp(). 438 * 439 * Null string is a wildcard (ANY service), according to RFC2516. 440 * And historical FreeBSD wildcard is also "*". 441 */ 442 443 static hook_p 444 pppoe_match_svc(node_p node, const struct pppoe_tag *tag) 445 { 446 hook_p hook; 447 448 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 449 sessp sp = NG_HOOK_PRIVATE(hook); 450 negp neg; 451 452 /* Skip any nonsession hook. */ 453 if (sp == NULL) 454 continue; 455 456 /* Skip any sessions which are not in LISTEN mode. */ 457 if (sp->state != PPPOE_LISTENING) 458 continue; 459 460 neg = sp->neg; 461 462 /* Empty Service-Name matches any service. */ 463 if (neg->service_len == 0) 464 break; 465 466 /* Special case for a blank or "*" service name (wildcard). */ 467 if (neg->service_len == 1 && neg->service.data[0] == '*') 468 break; 469 470 /* If the lengths don't match, that aint it. */ 471 if (neg->service_len != ntohs(tag->tag_len)) 472 continue; 473 474 if (strncmp(tag->tag_data, neg->service.data, 475 ntohs(tag->tag_len)) == 0) 476 break; 477 } 478 CTR3(KTR_NET, "%20s: matched %p for %s", __func__, hook, tag->tag_data); 479 480 return (hook); 481 } 482 483 /* 484 * Broadcast the PADI packet in m0 to all listening hooks. 485 * This routine is called when a PADI with empty Service-Name 486 * tag is received. Client should receive PADOs with all 487 * available services. 488 */ 489 static int 490 pppoe_broadcast_padi(node_p node, struct mbuf *m0) 491 { 492 hook_p hook; 493 int error = 0; 494 495 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 496 sessp sp = NG_HOOK_PRIVATE(hook); 497 struct mbuf *m; 498 499 /* 500 * Go through all listening hooks and 501 * broadcast the PADI packet up there 502 */ 503 if (sp == NULL) 504 continue; 505 506 if (sp->state != PPPOE_LISTENING) 507 continue; 508 509 m = m_dup(m0, M_DONTWAIT); 510 if (m == NULL) 511 return (ENOMEM); 512 NG_SEND_DATA_ONLY(error, hook, m); 513 if (error) 514 return (error); 515 } 516 517 return (0); 518 } 519 520 /* 521 * Find a hook, which name equals to given service. 522 */ 523 static hook_p 524 pppoe_find_svc(node_p node, const char *svc_name, int svc_len) 525 { 526 hook_p hook; 527 528 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 529 sessp sp = NG_HOOK_PRIVATE(hook); 530 negp neg; 531 532 /* Skip any nonsession hook. */ 533 if (sp == NULL) 534 continue; 535 536 /* Skip any sessions which are not in LISTEN mode. */ 537 if (sp->state != PPPOE_LISTENING) 538 continue; 539 540 neg = sp->neg; 541 542 if (neg->service_len == svc_len && 543 strncmp(svc_name, neg->service.data, svc_len == 0)) 544 return (hook); 545 } 546 547 return (NULL); 548 } 549 550 /************************************************************************** 551 * Routine to find a particular session that matches an incoming packet. * 552 **************************************************************************/ 553 static hook_p 554 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh) 555 { 556 hook_p hook = NULL; 557 uint16_t session = ntohs(wh->ph.sid); 558 559 /* 560 * Find matching peer/session combination. 561 */ 562 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 563 sessp sp = NG_HOOK_PRIVATE(hook); 564 565 /* Skip any nonsession hook. */ 566 if (sp == NULL) 567 continue; 568 if (sp->Session_ID == session && 569 (sp->state == PPPOE_CONNECTED || 570 sp->state == PPPOE_NEWCONNECTED) && 571 bcmp(sp->pkt_hdr.eh.ether_dhost, 572 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) { 573 break; 574 } 575 } 576 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, hook, session); 577 578 return (hook); 579 } 580 581 static hook_p 582 pppoe_finduniq(node_p node, const struct pppoe_tag *tag) 583 { 584 hook_p hook = NULL; 585 union uniq uniq; 586 587 bcopy(tag->tag_data, uniq.bytes, sizeof(void *)); 588 /* Cycle through all known hooks. */ 589 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 590 /* Skip any nonsession hook. */ 591 if (NG_HOOK_PRIVATE(hook) == NULL) 592 continue; 593 if (uniq.pointer == NG_HOOK_PRIVATE(hook)) 594 break; 595 } 596 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer); 597 598 return (hook); 599 } 600 601 /************************************************************************** 602 * Start of Netgraph entrypoints. * 603 **************************************************************************/ 604 605 /* 606 * Allocate the private data structure and link it with node. 607 */ 608 static int 609 ng_pppoe_constructor(node_p node) 610 { 611 priv_p privp; 612 613 /* Initialize private descriptor. */ 614 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO); 615 if (privp == NULL) 616 return (ENOMEM); 617 618 /* Link structs together; this counts as our one reference to *node. */ 619 NG_NODE_SET_PRIVATE(node, privp); 620 privp->node = node; 621 622 /* Initialize to standard mode. */ 623 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN); 624 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC; 625 626 CTR3(KTR_NET, "%20s: created node [%x] (%p)", 627 __func__, node->nd_ID, node); 628 629 return (0); 630 } 631 632 /* 633 * Give our ok for a hook to be added... 634 * point the hook's private info to the hook structure. 635 * 636 * The following hook names are special: 637 * "ethernet": the hook that should be connected to a NIC. 638 * "debug": copies of data sent out here (when I write the code). 639 * All other hook names need only be unique. (the framework checks this). 640 */ 641 static int 642 ng_pppoe_newhook(node_p node, hook_p hook, const char *name) 643 { 644 const priv_p privp = NG_NODE_PRIVATE(node); 645 sessp sp; 646 647 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) { 648 privp->ethernet_hook = hook; 649 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether); 650 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) { 651 privp->debug_hook = hook; 652 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug); 653 } else { 654 /* 655 * Any other unique name is OK. 656 * The infrastructure has already checked that it's unique, 657 * so just allocate it and hook it in. 658 */ 659 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO); 660 if (sp == NULL) 661 return (ENOMEM); 662 663 NG_HOOK_SET_PRIVATE(hook, sp); 664 sp->hook = hook; 665 } 666 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)", 667 __func__, node->nd_ID, node, name, hook); 668 669 return(0); 670 } 671 672 /* 673 * Hook has been added successfully. Request the MAC address of 674 * the underlying Ethernet node. 675 */ 676 static int 677 ng_pppoe_connect(hook_p hook) 678 { 679 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 680 struct ng_mesg *msg; 681 int error; 682 683 if (hook != privp->ethernet_hook) 684 return (0); 685 686 /* 687 * If this is Ethernet hook, then request MAC address 688 * from our downstream. 689 */ 690 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT); 691 if (msg == NULL) 692 return (ENOBUFS); 693 694 /* 695 * Our hook and peer hook have HK_INVALID flag set, 696 * so we can't use NG_SEND_MSG_HOOK() macro here. 697 */ 698 NG_SEND_MSG_ID(error, privp->node, msg, 699 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)), 700 NG_NODE_ID(privp->node)); 701 702 return (error); 703 } 704 /* 705 * Get a netgraph control message. 706 * Check it is one we understand. If needed, send a response. 707 * We sometimes save the address for an async action later. 708 * Always free the message. 709 */ 710 static int 711 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook) 712 { 713 priv_p privp = NG_NODE_PRIVATE(node); 714 struct ngpppoe_init_data *ourmsg = NULL; 715 struct ng_mesg *resp = NULL; 716 int error = 0; 717 hook_p hook = NULL; 718 sessp sp = NULL; 719 negp neg = NULL; 720 struct ng_mesg *msg; 721 722 NGI_GET_MSG(item, msg); 723 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d", 724 __func__, node->nd_ID, node, msg->header.cmd, 725 msg->header.typecookie); 726 727 /* Deal with message according to cookie and command. */ 728 switch (msg->header.typecookie) { 729 case NGM_PPPOE_COOKIE: 730 switch (msg->header.cmd) { 731 case NGM_PPPOE_CONNECT: 732 case NGM_PPPOE_LISTEN: 733 case NGM_PPPOE_OFFER: 734 case NGM_PPPOE_SERVICE: 735 ourmsg = (struct ngpppoe_init_data *)msg->data; 736 if (msg->header.arglen < sizeof(*ourmsg)) { 737 log(LOG_ERR, "ng_pppoe[%x]: init data too " 738 "small\n", node->nd_ID); 739 LEAVE(EMSGSIZE); 740 } 741 if (msg->header.arglen - sizeof(*ourmsg) > 742 PPPOE_SERVICE_NAME_SIZE) { 743 log(LOG_ERR, "ng_pppoe[%x]: service name " 744 "too big\n", node->nd_ID); 745 LEAVE(EMSGSIZE); 746 } 747 if (msg->header.arglen - sizeof(*ourmsg) < 748 ourmsg->data_len) { 749 log(LOG_ERR, "ng_pppoe[%x]: init data has bad " 750 "length, %d should be %zd\n", node->nd_ID, 751 ourmsg->data_len, 752 msg->header.arglen - sizeof (*ourmsg)); 753 LEAVE(EMSGSIZE); 754 } 755 756 /* Make sure strcmp will terminate safely. */ 757 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0'; 758 759 /* Cycle through all known hooks. */ 760 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 761 if (NG_HOOK_NAME(hook) && 762 strcmp(NG_HOOK_NAME(hook), ourmsg->hook) == 763 0) 764 break; 765 } 766 if (hook == NULL) 767 LEAVE(ENOENT); 768 769 sp = NG_HOOK_PRIVATE(hook); 770 771 if (sp == NULL) 772 LEAVE(EINVAL); 773 774 if (msg->header.cmd == NGM_PPPOE_LISTEN) { 775 /* 776 * Ensure we aren't already listening for this 777 * service. 778 */ 779 if (pppoe_find_svc(node, ourmsg->data, 780 ourmsg->data_len) != NULL) 781 LEAVE(EEXIST); 782 } 783 784 /* 785 * PPPOE_SERVICE advertisments are set up 786 * on sessions that are in PRIMED state. 787 */ 788 if (msg->header.cmd == NGM_PPPOE_SERVICE) 789 break; 790 791 if (sp->state != PPPOE_SNONE) { 792 log(LOG_NOTICE, "ng_pppoe[%x]: Session already " 793 "active\n", node->nd_ID); 794 LEAVE(EISCONN); 795 } 796 797 /* 798 * Set up prototype header. 799 */ 800 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE, 801 M_NOWAIT | M_ZERO); 802 803 if (neg == NULL) 804 LEAVE(ENOMEM); 805 806 neg->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 807 if (neg->m == NULL) { 808 free(neg, M_NETGRAPH_PPPOE); 809 LEAVE(ENOBUFS); 810 } 811 neg->m->m_pkthdr.rcvif = NULL; 812 sp->neg = neg; 813 ng_callout_init(&neg->handle); 814 neg->m->m_len = sizeof(struct pppoe_full_hdr); 815 neg->pkt = mtod(neg->m, union packet*); 816 memcpy((void *)&neg->pkt->pkt_header.eh, 817 &privp->eh, sizeof(struct ether_header)); 818 neg->pkt->pkt_header.ph.ver = 0x1; 819 neg->pkt->pkt_header.ph.type = 0x1; 820 neg->pkt->pkt_header.ph.sid = 0x0000; 821 neg->timeout = 0; 822 823 sp->creator = NGI_RETADDR(item); 824 } 825 switch (msg->header.cmd) { 826 case NGM_PPPOE_GET_STATUS: 827 { 828 struct ngpppoestat *stats; 829 830 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 831 if (!resp) 832 LEAVE(ENOMEM); 833 834 stats = (struct ngpppoestat *) resp->data; 835 stats->packets_in = privp->packets_in; 836 stats->packets_out = privp->packets_out; 837 break; 838 } 839 case NGM_PPPOE_CONNECT: 840 /* 841 * Check the hook exists and is Uninitialised. 842 * Send a PADI request, and start the timeout logic. 843 * Store the originator of this message so we can send 844 * a success of fail message to them later. 845 * Move the session to SINIT. 846 * Set up the session to the correct state and 847 * start it. 848 */ 849 neg->service.hdr.tag_type = PTT_SRV_NAME; 850 neg->service.hdr.tag_len = 851 htons((uint16_t)ourmsg->data_len); 852 if (ourmsg->data_len) 853 bcopy(ourmsg->data, neg->service.data, 854 ourmsg->data_len); 855 neg->service_len = ourmsg->data_len; 856 pppoe_start(sp); 857 break; 858 case NGM_PPPOE_LISTEN: 859 /* 860 * Check the hook exists and is Uninitialised. 861 * Install the service matching string. 862 * Store the originator of this message so we can send 863 * a success of fail message to them later. 864 * Move the hook to 'LISTENING' 865 */ 866 neg->service.hdr.tag_type = PTT_SRV_NAME; 867 neg->service.hdr.tag_len = 868 htons((uint16_t)ourmsg->data_len); 869 870 if (ourmsg->data_len) 871 bcopy(ourmsg->data, neg->service.data, 872 ourmsg->data_len); 873 neg->service_len = ourmsg->data_len; 874 neg->pkt->pkt_header.ph.code = PADT_CODE; 875 /* 876 * Wait for PADI packet coming from Ethernet. 877 */ 878 sp->state = PPPOE_LISTENING; 879 break; 880 case NGM_PPPOE_OFFER: 881 /* 882 * Check the hook exists and is Uninitialised. 883 * Store the originator of this message so we can send 884 * a success of fail message to them later. 885 * Store the AC-Name given and go to PRIMED. 886 */ 887 neg->ac_name.hdr.tag_type = PTT_AC_NAME; 888 neg->ac_name.hdr.tag_len = 889 htons((uint16_t)ourmsg->data_len); 890 if (ourmsg->data_len) 891 bcopy(ourmsg->data, neg->ac_name.data, 892 ourmsg->data_len); 893 neg->ac_name_len = ourmsg->data_len; 894 neg->pkt->pkt_header.ph.code = PADO_CODE; 895 /* 896 * Wait for PADI packet coming from hook. 897 */ 898 sp->state = PPPOE_PRIMED; 899 break; 900 case NGM_PPPOE_SERVICE: 901 /* 902 * Check the session is primed. 903 * for now just allow ONE service to be advertised. 904 * If you do it twice you just overwrite. 905 */ 906 if (sp->state != PPPOE_PRIMED) { 907 log(LOG_NOTICE, "ng_pppoe[%x]: session not " 908 "primed\n", node->nd_ID); 909 LEAVE(EISCONN); 910 } 911 neg = sp->neg; 912 neg->service.hdr.tag_type = PTT_SRV_NAME; 913 neg->service.hdr.tag_len = 914 htons((uint16_t)ourmsg->data_len); 915 916 if (ourmsg->data_len) 917 bcopy(ourmsg->data, neg->service.data, 918 ourmsg->data_len); 919 neg->service_len = ourmsg->data_len; 920 break; 921 case NGM_PPPOE_SETMODE: 922 { 923 char *s; 924 size_t len; 925 926 if (msg->header.arglen == 0) 927 LEAVE(EINVAL); 928 929 s = (char *)msg->data; 930 len = msg->header.arglen - 1; 931 932 /* Search for matching mode string. */ 933 if (len == strlen(NG_PPPOE_STANDARD) && 934 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) { 935 privp->flags = 0; 936 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC; 937 break; 938 } 939 if (len == strlen(NG_PPPOE_3COM) && 940 (strncmp(NG_PPPOE_3COM, s, len) == 0)) { 941 privp->flags |= COMPAT_3COM; 942 privp->eh.ether_type = 943 ETHERTYPE_PPPOE_3COM_DISC; 944 break; 945 } 946 if (len == strlen(NG_PPPOE_DLINK) && 947 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) { 948 privp->flags |= COMPAT_DLINK; 949 break; 950 } 951 error = EINVAL; 952 break; 953 } 954 case NGM_PPPOE_GETMODE: 955 { 956 char *s; 957 size_t len = 0; 958 959 if (privp->flags == 0) 960 len += strlen(NG_PPPOE_STANDARD) + 1; 961 if (privp->flags & COMPAT_3COM) 962 len += strlen(NG_PPPOE_3COM) + 1; 963 if (privp->flags & COMPAT_DLINK) 964 len += strlen(NG_PPPOE_DLINK) + 1; 965 966 NG_MKRESPONSE(resp, msg, len, M_NOWAIT); 967 if (resp == NULL) 968 LEAVE(ENOMEM); 969 970 s = (char *)resp->data; 971 if (privp->flags == 0) { 972 len = strlen(NG_PPPOE_STANDARD); 973 strlcpy(s, NG_PPPOE_STANDARD, len + 1); 974 break; 975 } 976 if (privp->flags & COMPAT_3COM) { 977 len = strlen(NG_PPPOE_3COM); 978 strlcpy(s, NG_PPPOE_3COM, len + 1); 979 s += len; 980 } 981 if (privp->flags & COMPAT_DLINK) { 982 if (s != resp->data) 983 *s++ = '|'; 984 len = strlen(NG_PPPOE_DLINK); 985 strlcpy(s, NG_PPPOE_DLINK, len + 1); 986 } 987 break; 988 } 989 case NGM_PPPOE_SETENADDR: 990 if (msg->header.arglen != ETHER_ADDR_LEN) 991 LEAVE(EINVAL); 992 bcopy(msg->data, &privp->eh.ether_shost, 993 ETHER_ADDR_LEN); 994 break; 995 default: 996 LEAVE(EINVAL); 997 } 998 break; 999 case NGM_ETHER_COOKIE: 1000 if (!(msg->header.flags & NGF_RESP)) 1001 LEAVE(EINVAL); 1002 switch (msg->header.cmd) { 1003 case NGM_ETHER_GET_ENADDR: 1004 if (msg->header.arglen != ETHER_ADDR_LEN) 1005 LEAVE(EINVAL); 1006 bcopy(msg->data, &privp->eh.ether_shost, 1007 ETHER_ADDR_LEN); 1008 break; 1009 default: 1010 LEAVE(EINVAL); 1011 } 1012 break; 1013 default: 1014 LEAVE(EINVAL); 1015 } 1016 1017 /* Take care of synchronous response, if any. */ 1018 quit: 1019 CTR2(KTR_NET, "%20s: returning %d", __func__, error); 1020 NG_RESPOND_MSG(error, node, item, resp); 1021 /* Free the message and return. */ 1022 NG_FREE_MSG(msg); 1023 return(error); 1024 } 1025 1026 /* 1027 * Start a client into the first state. A separate function because 1028 * it can be needed if the negotiation times out. 1029 */ 1030 static void 1031 pppoe_start(sessp sp) 1032 { 1033 hook_p hook = sp->hook; 1034 node_p node = NG_HOOK_NODE(hook); 1035 priv_p privp = NG_NODE_PRIVATE(node); 1036 negp neg = sp->neg; 1037 struct { 1038 struct pppoe_tag hdr; 1039 union uniq data; 1040 } __packed uniqtag; 1041 struct mbuf *m0; 1042 int error; 1043 1044 /* 1045 * Kick the state machine into starting up. 1046 */ 1047 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 1048 sp->state = PPPOE_SINIT; 1049 /* 1050 * Reset the packet header to broadcast. Since we are 1051 * in a client mode use configured ethertype. 1052 */ 1053 memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh, 1054 sizeof(struct ether_header)); 1055 neg->pkt->pkt_header.ph.code = PADI_CODE; 1056 uniqtag.hdr.tag_type = PTT_HOST_UNIQ; 1057 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data)); 1058 uniqtag.data.pointer = sp; 1059 init_tags(sp); 1060 insert_tag(sp, &uniqtag.hdr); 1061 insert_tag(sp, &neg->service.hdr); 1062 make_packet(sp); 1063 /* 1064 * Send packet and prepare to retransmit it after timeout. 1065 */ 1066 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz, 1067 pppoe_ticker, NULL, 0); 1068 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2; 1069 m0 = m_copypacket(neg->m, M_DONTWAIT); 1070 NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0); 1071 } 1072 1073 static int 1074 send_acname(sessp sp, const struct pppoe_tag *tag) 1075 { 1076 int error, tlen; 1077 struct ng_mesg *msg; 1078 struct ngpppoe_sts *sts; 1079 1080 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 1081 1082 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME, 1083 sizeof(struct ngpppoe_sts), M_NOWAIT); 1084 if (msg == NULL) 1085 return (ENOMEM); 1086 1087 sts = (struct ngpppoe_sts *)msg->data; 1088 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len)); 1089 strncpy(sts->hook, tag->tag_data, tlen); 1090 sts->hook[tlen] = '\0'; 1091 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0); 1092 1093 return (error); 1094 } 1095 1096 static int 1097 send_sessionid(sessp sp) 1098 { 1099 int error; 1100 struct ng_mesg *msg; 1101 1102 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 1103 1104 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID, 1105 sizeof(uint16_t), M_NOWAIT); 1106 if (msg == NULL) 1107 return (ENOMEM); 1108 1109 *(uint16_t *)msg->data = sp->Session_ID; 1110 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0); 1111 1112 return (error); 1113 } 1114 1115 /* 1116 * Receive data from session hook and do something with it. 1117 */ 1118 static int 1119 ng_pppoe_rcvdata(hook_p hook, item_p item) 1120 { 1121 node_p node = NG_HOOK_NODE(hook); 1122 const priv_p privp = NG_NODE_PRIVATE(node); 1123 sessp sp = NG_HOOK_PRIVATE(hook); 1124 struct pppoe_full_hdr *wh; 1125 struct mbuf *m; 1126 int error; 1127 1128 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)", 1129 __func__, node->nd_ID, node, item, hook->hk_name, hook); 1130 1131 NGI_GET_M(item, m); 1132 switch (sp->state) { 1133 case PPPOE_NEWCONNECTED: 1134 case PPPOE_CONNECTED: { 1135 static const u_char addrctrl[] = { 0xff, 0x03 }; 1136 1137 /* 1138 * Remove PPP address and control fields, if any. 1139 * For example, ng_ppp(4) always sends LCP packets 1140 * with address and control fields as required by 1141 * generic PPP. PPPoE is an exception to the rule. 1142 */ 1143 if (m->m_pkthdr.len >= 2) { 1144 if (m->m_len < 2 && !(m = m_pullup(m, 2))) 1145 LEAVE(ENOBUFS); 1146 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0) 1147 m_adj(m, 2); 1148 } 1149 /* 1150 * Bang in a pre-made header, and set the length up 1151 * to be correct. Then send it to the ethernet driver. 1152 */ 1153 M_PREPEND(m, sizeof(*wh), M_DONTWAIT); 1154 if (m == NULL) 1155 LEAVE(ENOBUFS); 1156 1157 wh = mtod(m, struct pppoe_full_hdr *); 1158 bcopy(&sp->pkt_hdr, wh, sizeof(*wh)); 1159 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh)); 1160 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m); 1161 privp->packets_out++; 1162 break; 1163 } 1164 case PPPOE_PRIMED: { 1165 struct { 1166 struct pppoe_tag hdr; 1167 union uniq data; 1168 } __packed uniqtag; 1169 const struct pppoe_tag *tag; 1170 struct mbuf *m0; 1171 const struct pppoe_hdr *ph; 1172 negp neg = sp->neg; 1173 uint16_t session; 1174 uint16_t length; 1175 uint8_t code; 1176 1177 /* 1178 * A PADI packet is being returned by the application 1179 * that has set up this hook. This indicates that it 1180 * wants us to offer service. 1181 */ 1182 if (m->m_len < sizeof(*wh)) { 1183 m = m_pullup(m, sizeof(*wh)); 1184 if (m == NULL) 1185 LEAVE(ENOBUFS); 1186 } 1187 wh = mtod(m, struct pppoe_full_hdr *); 1188 ph = &wh->ph; 1189 session = ntohs(wh->ph.sid); 1190 length = ntohs(wh->ph.length); 1191 code = wh->ph.code; 1192 /* Use peers mode in session. */ 1193 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type; 1194 if (code != PADI_CODE) 1195 LEAVE(EINVAL); 1196 ng_uncallout(&neg->handle, node); 1197 1198 /* 1199 * This is the first time we hear 1200 * from the client, so note it's 1201 * unicast address, replacing the 1202 * broadcast address. 1203 */ 1204 bcopy(wh->eh.ether_shost, 1205 neg->pkt->pkt_header.eh.ether_dhost, 1206 ETHER_ADDR_LEN); 1207 sp->state = PPPOE_SOFFER; 1208 neg->timeout = 0; 1209 neg->pkt->pkt_header.ph.code = PADO_CODE; 1210 1211 /* 1212 * Start working out the tags to respond with. 1213 */ 1214 uniqtag.hdr.tag_type = PTT_AC_COOKIE; 1215 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp)); 1216 uniqtag.data.pointer = sp; 1217 init_tags(sp); 1218 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 1219 if ((tag = get_tag(ph, PTT_SRV_NAME))) 1220 insert_tag(sp, tag); /* return service */ 1221 /* 1222 * If we have a NULL service request 1223 * and have an extra service defined in this hook, 1224 * then also add a tag for the extra service. 1225 * XXX this is a hack. eventually we should be able 1226 * to support advertising many services, not just one 1227 */ 1228 if (((tag == NULL) || (tag->tag_len == 0)) && 1229 (neg->service.hdr.tag_len != 0)) { 1230 insert_tag(sp, &neg->service.hdr); /* SERVICE */ 1231 } 1232 if ((tag = get_tag(ph, PTT_HOST_UNIQ))) 1233 insert_tag(sp, tag); /* returned hostunique */ 1234 insert_tag(sp, &uniqtag.hdr); 1235 scan_tags(sp, ph); 1236 make_packet(sp); 1237 /* 1238 * Send the offer but if they don't respond 1239 * in PPPOE_OFFER_TIMEOUT seconds, forget about it. 1240 */ 1241 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz, 1242 pppoe_ticker, NULL, 0); 1243 m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 1244 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0); 1245 privp->packets_out++; 1246 break; 1247 } 1248 1249 /* 1250 * Packets coming from the hook make no sense 1251 * to sessions in the rest of states. Throw them away. 1252 */ 1253 default: 1254 LEAVE(ENETUNREACH); 1255 } 1256 quit: 1257 if (item) 1258 NG_FREE_ITEM(item); 1259 NG_FREE_M(m); 1260 return (error); 1261 } 1262 1263 /* 1264 * Receive data from ether and do something with it. 1265 */ 1266 static int 1267 ng_pppoe_rcvdata_ether(hook_p hook, item_p item) 1268 { 1269 node_p node = NG_HOOK_NODE(hook); 1270 const priv_p privp = NG_NODE_PRIVATE(node); 1271 sessp sp = NG_HOOK_PRIVATE(hook); 1272 const struct pppoe_tag *utag = NULL, *tag = NULL; 1273 const struct pppoe_full_hdr *wh; 1274 const struct pppoe_hdr *ph; 1275 negp neg = NULL; 1276 struct mbuf *m; 1277 hook_p sendhook; 1278 int error = 0; 1279 uint16_t session; 1280 uint16_t length; 1281 uint8_t code; 1282 struct mbuf *m0; 1283 1284 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)", 1285 __func__, node->nd_ID, node, item, hook->hk_name, hook); 1286 1287 NGI_GET_M(item, m); 1288 /* 1289 * Dig out various fields from the packet. 1290 * Use them to decide where to send it. 1291 */ 1292 privp->packets_in++; 1293 if( m->m_len < sizeof(*wh)) { 1294 m = m_pullup(m, sizeof(*wh)); /* Checks length */ 1295 if (m == NULL) { 1296 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't " 1297 "m_pullup(wh)\n", node->nd_ID); 1298 LEAVE(ENOBUFS); 1299 } 1300 } 1301 wh = mtod(m, struct pppoe_full_hdr *); 1302 length = ntohs(wh->ph.length); 1303 switch(wh->eh.ether_type) { 1304 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */ 1305 case ETHERTYPE_PPPOE_DISC: 1306 /* 1307 * We need to try to make sure that the tag area 1308 * is contiguous, or we could wander off the end 1309 * of a buffer and make a mess. 1310 * (Linux wouldn't have this problem). 1311 */ 1312 if (m->m_pkthdr.len <= MHLEN) { 1313 if( m->m_len < m->m_pkthdr.len) { 1314 m = m_pullup(m, m->m_pkthdr.len); 1315 if (m == NULL) { 1316 log(LOG_NOTICE, "ng_pppoe[%x]: " 1317 "couldn't m_pullup(pkthdr)\n", 1318 node->nd_ID); 1319 LEAVE(ENOBUFS); 1320 } 1321 } 1322 } 1323 if (m->m_len != m->m_pkthdr.len) { 1324 /* 1325 * It's not all in one piece. 1326 * We need to do extra work. 1327 * Put it into a cluster. 1328 */ 1329 struct mbuf *n; 1330 n = m_dup(m, M_DONTWAIT); 1331 m_freem(m); 1332 m = n; 1333 if (m) { 1334 /* just check we got a cluster */ 1335 if (m->m_len != m->m_pkthdr.len) { 1336 m_freem(m); 1337 m = NULL; 1338 } 1339 } 1340 if (m == NULL) { 1341 log(LOG_NOTICE, "ng_pppoe[%x]: packet " 1342 "fragmented\n", node->nd_ID); 1343 LEAVE(EMSGSIZE); 1344 } 1345 } 1346 wh = mtod(m, struct pppoe_full_hdr *); 1347 length = ntohs(wh->ph.length); 1348 ph = &wh->ph; 1349 session = ntohs(wh->ph.sid); 1350 code = wh->ph.code; 1351 1352 switch(code) { 1353 case PADI_CODE: 1354 /* 1355 * We are a server: 1356 * Look for a hook with the required service and send 1357 * the ENTIRE packet up there. It should come back to 1358 * a new hook in PRIMED state. Look there for further 1359 * processing. 1360 */ 1361 tag = get_tag(ph, PTT_SRV_NAME); 1362 if (tag == NULL) { 1363 CTR1(KTR_NET, "%20s: PADI w/o Service-Name", 1364 __func__); 1365 LEAVE(ENETUNREACH); 1366 } 1367 1368 /* 1369 * First, try to match Service-Name against our 1370 * listening hooks. If no success and we are in D-Link 1371 * compat mode and Service-Name is empty, then we 1372 * broadcast the PADI to all listening hooks. 1373 */ 1374 sendhook = pppoe_match_svc(node, tag); 1375 if (sendhook != NULL) 1376 NG_FWD_NEW_DATA(error, item, sendhook, m); 1377 else if (privp->flags & COMPAT_DLINK && 1378 ntohs(tag->tag_len) == 0) 1379 error = pppoe_broadcast_padi(node, m); 1380 else 1381 error = ENETUNREACH; 1382 break; 1383 case PADO_CODE: 1384 /* 1385 * We are a client: 1386 * Use the host_uniq tag to find the hook this is in 1387 * response to. Received #2, now send #3 1388 * For now simply accept the first we receive. 1389 */ 1390 utag = get_tag(ph, PTT_HOST_UNIQ); 1391 if ((utag == NULL) || 1392 (ntohs(utag->tag_len) != sizeof(sp))) { 1393 log(LOG_NOTICE, "ng_pppoe[%x]: no host " 1394 "unique field\n", node->nd_ID); 1395 LEAVE(ENETUNREACH); 1396 } 1397 1398 sendhook = pppoe_finduniq(node, utag); 1399 if (sendhook == NULL) { 1400 log(LOG_NOTICE, "ng_pppoe[%x]: no " 1401 "matching session\n", node->nd_ID); 1402 LEAVE(ENETUNREACH); 1403 } 1404 1405 /* 1406 * Check the session is in the right state. 1407 * It needs to be in PPPOE_SINIT. 1408 */ 1409 sp = NG_HOOK_PRIVATE(sendhook); 1410 if (sp->state != PPPOE_SINIT) { 1411 log(LOG_NOTICE, "ng_pppoe[%x]: session " 1412 "in wrong state\n", node->nd_ID); 1413 LEAVE(ENETUNREACH); 1414 } 1415 neg = sp->neg; 1416 ng_uncallout(&neg->handle, node); 1417 1418 /* 1419 * This is the first time we hear 1420 * from the server, so note it's 1421 * unicast address, replacing the 1422 * broadcast address . 1423 */ 1424 bcopy(wh->eh.ether_shost, 1425 neg->pkt->pkt_header.eh.ether_dhost, 1426 ETHER_ADDR_LEN); 1427 neg->timeout = 0; 1428 neg->pkt->pkt_header.ph.code = PADR_CODE; 1429 init_tags(sp); 1430 insert_tag(sp, utag); /* Host Unique */ 1431 if ((tag = get_tag(ph, PTT_AC_COOKIE))) 1432 insert_tag(sp, tag); /* return cookie */ 1433 if ((tag = get_tag(ph, PTT_AC_NAME))) { 1434 insert_tag(sp, tag); /* return it */ 1435 send_acname(sp, tag); 1436 } 1437 insert_tag(sp, &neg->service.hdr); /* Service */ 1438 scan_tags(sp, ph); 1439 make_packet(sp); 1440 sp->state = PPPOE_SREQ; 1441 ng_callout(&neg->handle, node, sp->hook, 1442 PPPOE_INITIAL_TIMEOUT * hz, 1443 pppoe_ticker, NULL, 0); 1444 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2; 1445 m0 = m_copypacket(neg->m, M_DONTWAIT); 1446 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0); 1447 break; 1448 case PADR_CODE: 1449 /* 1450 * We are a server: 1451 * Use the ac_cookie tag to find the 1452 * hook this is in response to. 1453 */ 1454 utag = get_tag(ph, PTT_AC_COOKIE); 1455 if ((utag == NULL) || 1456 (ntohs(utag->tag_len) != sizeof(sp))) { 1457 LEAVE(ENETUNREACH); 1458 } 1459 1460 sendhook = pppoe_finduniq(node, utag); 1461 if (sendhook == NULL) 1462 LEAVE(ENETUNREACH); 1463 1464 /* 1465 * Check the session is in the right state. 1466 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED. 1467 * If the latter, then this is a retry by the client, 1468 * so be nice, and resend. 1469 */ 1470 sp = NG_HOOK_PRIVATE(sendhook); 1471 if (sp->state == PPPOE_NEWCONNECTED) { 1472 /* 1473 * Whoa! drop back to resend that PADS packet. 1474 * We should still have a copy of it. 1475 */ 1476 sp->state = PPPOE_SOFFER; 1477 } 1478 if (sp->state != PPPOE_SOFFER) 1479 LEAVE (ENETUNREACH); 1480 neg = sp->neg; 1481 ng_uncallout(&neg->handle, node); 1482 neg->pkt->pkt_header.ph.code = PADS_CODE; 1483 if (sp->Session_ID == 0) 1484 neg->pkt->pkt_header.ph.sid = 1485 htons(sp->Session_ID 1486 = get_new_sid(node)); 1487 send_sessionid(sp); 1488 neg->timeout = 0; 1489 /* 1490 * start working out the tags to respond with. 1491 */ 1492 init_tags(sp); 1493 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 1494 if ((tag = get_tag(ph, PTT_SRV_NAME))) 1495 insert_tag(sp, tag);/* return service */ 1496 if ((tag = get_tag(ph, PTT_HOST_UNIQ))) 1497 insert_tag(sp, tag); /* return it */ 1498 insert_tag(sp, utag); /* ac_cookie */ 1499 scan_tags(sp, ph); 1500 make_packet(sp); 1501 sp->state = PPPOE_NEWCONNECTED; 1502 1503 /* Send the PADS without a timeout - we're now connected. */ 1504 m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 1505 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0); 1506 1507 /* 1508 * Having sent the last Negotiation header, 1509 * Set up the stored packet header to be correct for 1510 * the actual session. But keep the negotialtion stuff 1511 * around in case we need to resend this last packet. 1512 * We'll discard it when we move from NEWCONNECTED 1513 * to CONNECTED 1514 */ 1515 sp->pkt_hdr = neg->pkt->pkt_header; 1516 /* Configure ethertype depending on what 1517 * ethertype was used at discovery phase */ 1518 if (sp->pkt_hdr.eh.ether_type == 1519 ETHERTYPE_PPPOE_3COM_DISC) 1520 sp->pkt_hdr.eh.ether_type 1521 = ETHERTYPE_PPPOE_3COM_SESS; 1522 else 1523 sp->pkt_hdr.eh.ether_type 1524 = ETHERTYPE_PPPOE_SESS; 1525 sp->pkt_hdr.ph.code = 0; 1526 pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 1527 break; 1528 case PADS_CODE: 1529 /* 1530 * We are a client: 1531 * Use the host_uniq tag to find the hook this is in 1532 * response to. Take the session ID and store it away. 1533 * Also make sure the pre-made header is correct and 1534 * set us into Session mode. 1535 */ 1536 utag = get_tag(ph, PTT_HOST_UNIQ); 1537 if ((utag == NULL) || 1538 (ntohs(utag->tag_len) != sizeof(sp))) { 1539 LEAVE (ENETUNREACH); 1540 } 1541 sendhook = pppoe_finduniq(node, utag); 1542 if (sendhook == NULL) 1543 LEAVE(ENETUNREACH); 1544 1545 /* 1546 * Check the session is in the right state. 1547 * It needs to be in PPPOE_SREQ. 1548 */ 1549 sp = NG_HOOK_PRIVATE(sendhook); 1550 if (sp->state != PPPOE_SREQ) 1551 LEAVE(ENETUNREACH); 1552 neg = sp->neg; 1553 ng_uncallout(&neg->handle, node); 1554 neg->pkt->pkt_header.ph.sid = wh->ph.sid; 1555 sp->Session_ID = ntohs(wh->ph.sid); 1556 send_sessionid(sp); 1557 neg->timeout = 0; 1558 sp->state = PPPOE_CONNECTED; 1559 /* 1560 * Now we have gone to Connected mode, 1561 * Free all resources needed for negotiation. 1562 * Keep a copy of the header we will be using. 1563 */ 1564 sp->pkt_hdr = neg->pkt->pkt_header; 1565 if (privp->flags & COMPAT_3COM) 1566 sp->pkt_hdr.eh.ether_type 1567 = ETHERTYPE_PPPOE_3COM_SESS; 1568 else 1569 sp->pkt_hdr.eh.ether_type 1570 = ETHERTYPE_PPPOE_SESS; 1571 sp->pkt_hdr.ph.code = 0; 1572 m_freem(neg->m); 1573 free(sp->neg, M_NETGRAPH_PPPOE); 1574 sp->neg = NULL; 1575 pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 1576 break; 1577 case PADT_CODE: 1578 /* 1579 * Find matching peer/session combination. 1580 */ 1581 sendhook = pppoe_findsession(node, wh); 1582 if (sendhook == NULL) 1583 LEAVE(ENETUNREACH); 1584 /* Disconnect that hook. */ 1585 ng_rmhook_self(sendhook); 1586 break; 1587 default: 1588 LEAVE(EPFNOSUPPORT); 1589 } 1590 break; 1591 case ETHERTYPE_PPPOE_3COM_SESS: 1592 case ETHERTYPE_PPPOE_SESS: 1593 /* 1594 * Find matching peer/session combination. 1595 */ 1596 sendhook = pppoe_findsession(node, wh); 1597 if (sendhook == NULL) 1598 LEAVE (ENETUNREACH); 1599 sp = NG_HOOK_PRIVATE(sendhook); 1600 m_adj(m, sizeof(*wh)); 1601 1602 /* If packet too short, dump it. */ 1603 if (m->m_pkthdr.len < length) 1604 LEAVE(EMSGSIZE); 1605 /* Also need to trim excess at the end */ 1606 if (m->m_pkthdr.len > length) { 1607 m_adj(m, -((int)(m->m_pkthdr.len - length))); 1608 } 1609 if ( sp->state != PPPOE_CONNECTED) { 1610 if (sp->state == PPPOE_NEWCONNECTED) { 1611 sp->state = PPPOE_CONNECTED; 1612 /* 1613 * Now we have gone to Connected mode, 1614 * Free all resources needed for negotiation. 1615 * Be paranoid about whether there may be 1616 * a timeout. 1617 */ 1618 m_freem(sp->neg->m); 1619 ng_uncallout(&sp->neg->handle, node); 1620 free(sp->neg, M_NETGRAPH_PPPOE); 1621 sp->neg = NULL; 1622 } else { 1623 LEAVE (ENETUNREACH); 1624 } 1625 } 1626 NG_FWD_NEW_DATA(error, item, sendhook, m); 1627 break; 1628 default: 1629 LEAVE(EPFNOSUPPORT); 1630 } 1631 quit: 1632 if (item) 1633 NG_FREE_ITEM(item); 1634 NG_FREE_M(m); 1635 return (error); 1636 } 1637 1638 /* 1639 * Receive data from debug hook and bypass it to ether. 1640 */ 1641 static int 1642 ng_pppoe_rcvdata_debug(hook_p hook, item_p item) 1643 { 1644 node_p node = NG_HOOK_NODE(hook); 1645 const priv_p privp = NG_NODE_PRIVATE(node); 1646 int error; 1647 1648 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)", 1649 __func__, node->nd_ID, node, item, hook->hk_name, hook); 1650 1651 NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook); 1652 privp->packets_out++; 1653 return (error); 1654 } 1655 1656 /* 1657 * Do local shutdown processing.. 1658 * If we are a persistant device, we might refuse to go away, and 1659 * we'd only remove our links and reset ourself. 1660 */ 1661 static int 1662 ng_pppoe_shutdown(node_p node) 1663 { 1664 const priv_p privdata = NG_NODE_PRIVATE(node); 1665 1666 NG_NODE_SET_PRIVATE(node, NULL); 1667 NG_NODE_UNREF(privdata->node); 1668 free(privdata, M_NETGRAPH_PPPOE); 1669 return (0); 1670 } 1671 1672 /* 1673 * Hook disconnection 1674 * 1675 * Clean up all dangling links and information about the session/hook. 1676 * For this type, removal of the last link destroys the node. 1677 */ 1678 static int 1679 ng_pppoe_disconnect(hook_p hook) 1680 { 1681 node_p node = NG_HOOK_NODE(hook); 1682 priv_p privp = NG_NODE_PRIVATE(node); 1683 sessp sp; 1684 1685 if (hook == privp->debug_hook) { 1686 privp->debug_hook = NULL; 1687 } else if (hook == privp->ethernet_hook) { 1688 privp->ethernet_hook = NULL; 1689 if (NG_NODE_IS_VALID(node)) 1690 ng_rmnode_self(node); 1691 } else { 1692 sp = NG_HOOK_PRIVATE(hook); 1693 if (sp->state != PPPOE_SNONE ) { 1694 pppoe_send_event(sp, NGM_PPPOE_CLOSE); 1695 } 1696 /* 1697 * According to the spec, if we are connected, 1698 * we should send a DISC packet if we are shutting down 1699 * a session. 1700 */ 1701 if ((privp->ethernet_hook) 1702 && ((sp->state == PPPOE_CONNECTED) 1703 || (sp->state == PPPOE_NEWCONNECTED))) { 1704 struct mbuf *m; 1705 1706 /* Generate a packet of that type. */ 1707 MGETHDR(m, M_DONTWAIT, MT_DATA); 1708 if (m == NULL) 1709 log(LOG_NOTICE, "ng_pppoe[%x]: session out of " 1710 "mbufs\n", node->nd_ID); 1711 else { 1712 struct pppoe_full_hdr *wh; 1713 struct pppoe_tag *tag; 1714 int msglen = strlen(SIGNOFF); 1715 int error = 0; 1716 1717 m->m_pkthdr.rcvif = NULL; 1718 m->m_pkthdr.len = m->m_len = sizeof(*wh); 1719 wh = mtod(m, struct pppoe_full_hdr *); 1720 bcopy(&sp->pkt_hdr, wh, sizeof(*wh)); 1721 1722 /* Revert the stored header to DISC/PADT mode. */ 1723 wh->ph.code = PADT_CODE; 1724 /* 1725 * Configure ethertype depending on what 1726 * was used during sessions stage. 1727 */ 1728 if (wh->eh.ether_type == 1729 ETHERTYPE_PPPOE_3COM_SESS) 1730 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC; 1731 else 1732 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC; 1733 /* 1734 * Add a General error message and adjust 1735 * sizes. 1736 */ 1737 tag = wh->ph.tag; 1738 tag->tag_type = PTT_GEN_ERR; 1739 tag->tag_len = htons((u_int16_t)msglen); 1740 strncpy(tag->tag_data, SIGNOFF, msglen); 1741 m->m_pkthdr.len = (m->m_len += sizeof(*tag) + 1742 msglen); 1743 wh->ph.length = htons(sizeof(*tag) + msglen); 1744 NG_SEND_DATA_ONLY(error, 1745 privp->ethernet_hook, m); 1746 } 1747 } 1748 /* 1749 * As long as we have somewhere to store the timeout handle, 1750 * we may have a timeout pending.. get rid of it. 1751 */ 1752 if (sp->neg) { 1753 ng_uncallout(&sp->neg->handle, node); 1754 if (sp->neg->m) 1755 m_freem(sp->neg->m); 1756 free(sp->neg, M_NETGRAPH_PPPOE); 1757 } 1758 free(sp, M_NETGRAPH_PPPOE); 1759 NG_HOOK_SET_PRIVATE(hook, NULL); 1760 } 1761 if ((NG_NODE_NUMHOOKS(node) == 0) && 1762 (NG_NODE_IS_VALID(node))) 1763 ng_rmnode_self(node); 1764 return (0); 1765 } 1766 1767 /* 1768 * Timeouts come here. 1769 */ 1770 static void 1771 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2) 1772 { 1773 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 1774 sessp sp = NG_HOOK_PRIVATE(hook); 1775 negp neg = sp->neg; 1776 struct mbuf *m0 = NULL; 1777 int error = 0; 1778 1779 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d", 1780 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID); 1781 switch(sp->state) { 1782 /* 1783 * Resend the last packet, using an exponential backoff. 1784 * After a period of time, stop growing the backoff, 1785 * And either leave it, or revert to the start. 1786 */ 1787 case PPPOE_SINIT: 1788 case PPPOE_SREQ: 1789 /* Timeouts on these produce resends. */ 1790 m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 1791 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0); 1792 ng_callout(&neg->handle, node, hook, neg->timeout * hz, 1793 pppoe_ticker, NULL, 0); 1794 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) { 1795 if (sp->state == PPPOE_SREQ) { 1796 /* Revert to SINIT mode. */ 1797 pppoe_start(sp); 1798 } else { 1799 neg->timeout = PPPOE_TIMEOUT_LIMIT; 1800 } 1801 } 1802 break; 1803 case PPPOE_PRIMED: 1804 case PPPOE_SOFFER: 1805 /* A timeout on these says "give up" */ 1806 ng_rmhook_self(hook); 1807 break; 1808 default: 1809 /* Timeouts have no meaning in other states. */ 1810 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n", 1811 node->nd_ID); 1812 } 1813 } 1814 1815 /* 1816 * Parse an incoming packet to see if any tags should be copied to the 1817 * output packet. Don't do any tags that have been handled in the main 1818 * state machine. 1819 */ 1820 static const struct pppoe_tag* 1821 scan_tags(sessp sp, const struct pppoe_hdr* ph) 1822 { 1823 const char *const end = (const char *)next_tag(ph); 1824 const char *ptn; 1825 const struct pppoe_tag *pt = &ph->tag[0]; 1826 1827 /* 1828 * Keep processing tags while a tag header will still fit. 1829 */ 1830 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 1831 1832 while((const char*)(pt + 1) <= end) { 1833 /* 1834 * If the tag data would go past the end of the packet, abort. 1835 */ 1836 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len)); 1837 if(ptn > end) 1838 return NULL; 1839 1840 switch (pt->tag_type) { 1841 case PTT_RELAY_SID: 1842 insert_tag(sp, pt); 1843 break; 1844 case PTT_EOL: 1845 return NULL; 1846 case PTT_SRV_NAME: 1847 case PTT_AC_NAME: 1848 case PTT_HOST_UNIQ: 1849 case PTT_AC_COOKIE: 1850 case PTT_VENDOR: 1851 case PTT_SRV_ERR: 1852 case PTT_SYS_ERR: 1853 case PTT_GEN_ERR: 1854 break; 1855 } 1856 pt = (const struct pppoe_tag*)ptn; 1857 } 1858 return NULL; 1859 } 1860 1861 static int 1862 pppoe_send_event(sessp sp, enum cmd cmdid) 1863 { 1864 int error; 1865 struct ng_mesg *msg; 1866 struct ngpppoe_sts *sts; 1867 1868 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID); 1869 1870 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid, 1871 sizeof(struct ngpppoe_sts), M_NOWAIT); 1872 if (msg == NULL) 1873 return (ENOMEM); 1874 sts = (struct ngpppoe_sts *)msg->data; 1875 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ); 1876 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0); 1877 return (error); 1878 } 1879