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