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