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.10 2007/06/03 20:51:13 dillon Exp $ 41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $ 42 */ 43 #if 0 44 #define AAA kprintf("pppoe: %s\n", __func__ ); 45 #define BBB kprintf("-%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 <sys/thread2.h> 60 #include <net/ethernet.h> 61 62 #include <netgraph/ng_message.h> 63 #include <netgraph/netgraph.h> 64 #include "ng_pppoe.h" 65 66 #define SIGNOFF "session closed" 67 68 /* 69 * This section contains the netgraph method declarations for the 70 * pppoe node. These methods define the netgraph pppoe 'type'. 71 */ 72 73 static ng_constructor_t ng_pppoe_constructor; 74 static ng_rcvmsg_t ng_pppoe_rcvmsg; 75 static ng_shutdown_t ng_pppoe_rmnode; 76 static ng_newhook_t ng_pppoe_newhook; 77 static ng_connect_t ng_pppoe_connect; 78 static ng_rcvdata_t ng_pppoe_rcvdata; 79 static ng_disconnect_t ng_pppoe_disconnect; 80 81 /* Netgraph node type descriptor */ 82 static struct ng_type typestruct = { 83 NG_VERSION, 84 NG_PPPOE_NODE_TYPE, 85 NULL, 86 ng_pppoe_constructor, 87 ng_pppoe_rcvmsg, 88 ng_pppoe_rmnode, 89 ng_pppoe_newhook, 90 NULL, 91 ng_pppoe_connect, 92 ng_pppoe_rcvdata, 93 ng_pppoe_rcvdata, 94 ng_pppoe_disconnect, 95 NULL 96 }; 97 NETGRAPH_INIT(pppoe, &typestruct); 98 99 /* 100 * States for the session state machine. 101 * These have no meaning if there is no hook attached yet. 102 */ 103 enum state { 104 PPPOE_SNONE=0, /* [both] Initial state */ 105 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */ 106 PPPOE_SINIT, /* [Client] Sent discovery initiation */ 107 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */ 108 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/ 109 PPPOE_SREQ, /* [Client] Sent a Request */ 110 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */ 111 PPPOE_CONNECTED, /* [Both] Connection established, Data received */ 112 PPPOE_DEAD /* [Both] */ 113 }; 114 115 #define NUMTAGS 20 /* number of tags we are set up to work with */ 116 117 /* 118 * Information we store for each hook on each node for negotiating the 119 * session. The mbuf and cluster are freed once negotiation has completed. 120 * The whole negotiation block is then discarded. 121 */ 122 123 struct sess_neg { 124 struct mbuf *m; /* holds cluster with last sent packet */ 125 union packet *pkt; /* points within the above cluster */ 126 struct callout timeout_ch; 127 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */ 128 u_int numtags; 129 const struct pppoe_tag *tags[NUMTAGS]; 130 u_int service_len; 131 u_int ac_name_len; 132 133 struct datatag service; 134 struct datatag ac_name; 135 }; 136 typedef struct sess_neg *negp; 137 138 /* 139 * Session information that is needed after connection. 140 */ 141 struct sess_con { 142 hook_p hook; 143 u_int16_t Session_ID; 144 enum state state; 145 char creator[NG_NODESIZ]; /* who to notify */ 146 struct pppoe_full_hdr pkt_hdr; /* used when connected */ 147 negp neg; /* used when negotiating */ 148 /*struct sess_con *hash_next;*/ /* not yet used */ 149 }; 150 typedef struct sess_con *sessp; 151 152 /* 153 * Information we store for each node 154 */ 155 struct PPPOE { 156 node_p node; /* back pointer to node */ 157 hook_p ethernet_hook; 158 hook_p debug_hook; 159 u_int packets_in; /* packets in from ethernet */ 160 u_int packets_out; /* packets out towards ethernet */ 161 u_int32_t flags; 162 /*struct sess_con *buckets[HASH_SIZE];*/ /* not yet used */ 163 }; 164 typedef struct PPPOE *priv_p; 165 166 struct ether_header eh_prototype = 167 {{0xff,0xff,0xff,0xff,0xff,0xff}, 168 {0x00,0x00,0x00,0x00,0x00,0x00}, 169 ETHERTYPE_PPPOE_DISC}; 170 171 #define PPPOE_KEEPSTANDARD -1 /* never switch to nonstandard mode */ 172 #define PPPOE_STANDARD 0 /* try standard mode (dangerous!) */ 173 #define PPPOE_NONSTANDARD 1 /* just be in nonstandard mode */ 174 static int pppoe_mode = PPPOE_KEEPSTANDARD; 175 176 static int 177 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS) 178 { 179 int error; 180 int val; 181 182 val = pppoe_mode; 183 error = sysctl_handle_int(oidp, &val, sizeof(int), req); 184 if (error != 0 || req->newptr == NULL) 185 return (error); 186 switch (val) { 187 case PPPOE_NONSTANDARD: 188 pppoe_mode = PPPOE_NONSTANDARD; 189 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC; 190 break; 191 case PPPOE_STANDARD: 192 pppoe_mode = PPPOE_STANDARD; 193 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC; 194 break; 195 case PPPOE_KEEPSTANDARD: 196 pppoe_mode = PPPOE_KEEPSTANDARD; 197 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC; 198 break; 199 default: 200 return (EINVAL); 201 } 202 return (0); 203 } 204 205 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW, 206 0, sizeof(int), ngpppoe_set_ethertype, "I", "nonstandard ethertype"); 207 208 union uniq { 209 char bytes[sizeof(void *)]; 210 void * pointer; 211 }; 212 213 #define LEAVE(x) do { error = x; goto quit; } while(0) 214 static void pppoe_start(sessp sp); 215 static void sendpacket(sessp sp); 216 static void pppoe_ticker(void *arg); 217 static const struct pppoe_tag *scan_tags(sessp sp, 218 const struct pppoe_hdr* ph); 219 static int pppoe_send_event(sessp sp, enum cmd cmdid); 220 221 /************************************************************************* 222 * Some basic utilities from the Linux version with author's permission.* 223 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> * 224 ************************************************************************/ 225 226 /* 227 * Generate a new session id 228 * XXX find out the FreeBSD locking scheme. 229 */ 230 static u_int16_t 231 get_new_sid(node_p node) 232 { 233 static int pppoe_sid = 10; 234 sessp sp; 235 hook_p hook; 236 u_int16_t val; 237 priv_p privp = node->private; 238 239 AAA 240 restart: 241 val = pppoe_sid++; 242 /* 243 * Spec says 0xFFFF is reserved. 244 * Also don't use 0x0000 245 */ 246 if (val == 0xffff) { 247 pppoe_sid = 20; 248 goto restart; 249 } 250 251 /* Check it isn't already in use */ 252 LIST_FOREACH(hook, &node->hooks, hooks) { 253 /* don't check special hooks */ 254 if ((hook->private == &privp->debug_hook) 255 || (hook->private == &privp->ethernet_hook)) 256 continue; 257 sp = hook->private; 258 if (sp->Session_ID == val) 259 goto restart; 260 } 261 262 return val; 263 } 264 265 266 /* 267 * Return the location where the next tag can be put 268 */ 269 static __inline const struct pppoe_tag* 270 next_tag(const struct pppoe_hdr* ph) 271 { 272 return (const struct pppoe_tag*)(((const char*)&ph->tag[0]) 273 + ntohs(ph->length)); 274 } 275 276 /* 277 * Look for a tag of a specific type 278 * Don't trust any length the other end says. 279 * but assume we already sanity checked ph->length. 280 */ 281 static const struct pppoe_tag* 282 get_tag(const struct pppoe_hdr* ph, u_int16_t idx) 283 { 284 const char *const end = (const char *)next_tag(ph); 285 const char *ptn; 286 const struct pppoe_tag *pt = &ph->tag[0]; 287 /* 288 * Keep processing tags while a tag header will still fit. 289 */ 290 AAA 291 while((const char*)(pt + 1) <= end) { 292 /* 293 * If the tag data would go past the end of the packet, abort. 294 */ 295 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len)); 296 if(ptn > end) 297 return NULL; 298 299 if(pt->tag_type == idx) 300 return pt; 301 302 pt = (const struct pppoe_tag*)ptn; 303 } 304 return NULL; 305 } 306 307 /************************************************************************** 308 * inlines to initialise or add tags to a session's tag list, 309 **************************************************************************/ 310 /* 311 * Initialise the session's tag list 312 */ 313 static void 314 init_tags(sessp sp) 315 { 316 AAA 317 if(sp->neg == NULL) { 318 kprintf("pppoe: asked to init NULL neg pointer\n"); 319 return; 320 } 321 sp->neg->numtags = 0; 322 } 323 324 static void 325 insert_tag(sessp sp, const struct pppoe_tag *tp) 326 { 327 int i; 328 negp neg; 329 330 AAA 331 if((neg = sp->neg) == NULL) { 332 kprintf("pppoe: asked to use NULL neg pointer\n"); 333 return; 334 } 335 if ((i = neg->numtags++) < NUMTAGS) { 336 neg->tags[i] = tp; 337 } else { 338 kprintf("pppoe: asked to add too many tags to packet\n"); 339 neg->numtags--; 340 } 341 } 342 343 /* 344 * Make up a packet, using the tags filled out for the session. 345 * 346 * Assume that the actual pppoe header and ethernet header 347 * are filled out externally to this routine. 348 * Also assume that neg->wh points to the correct 349 * location at the front of the buffer space. 350 */ 351 static void 352 make_packet(sessp sp) { 353 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header; 354 const struct pppoe_tag **tag; 355 char *dp; 356 int count; 357 int tlen; 358 u_int16_t length = 0; 359 360 AAA 361 if ((sp->neg == NULL) || (sp->neg->m == NULL)) { 362 kprintf("pppoe: make_packet called from wrong state\n"); 363 } 364 dp = (char *)wh->ph.tag; 365 for (count = 0, tag = sp->neg->tags; 366 ((count < sp->neg->numtags) && (count < NUMTAGS)); 367 tag++, count++) { 368 tlen = ntohs((*tag)->tag_len) + sizeof(**tag); 369 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) { 370 kprintf("pppoe: tags too long\n"); 371 sp->neg->numtags = count; 372 break; /* XXX chop off what's too long */ 373 } 374 bcopy(*tag, (char *)dp, tlen); 375 length += tlen; 376 dp += tlen; 377 } 378 wh->ph.length = htons(length); 379 sp->neg->m->m_len = length + sizeof(*wh); 380 sp->neg->m->m_pkthdr.len = length + sizeof(*wh); 381 } 382 383 /************************************************************************** 384 * Routine to match a service offered * 385 **************************************************************************/ 386 /* 387 * Find a hook that has a service string that matches that 388 * we are seeking. for now use a simple string. 389 * In the future we may need something like regexp(). 390 * for testing allow a null string to match 1st found and a null service 391 * to match all requests. Also make '*' do the same. 392 */ 393 394 #define NG_MATCH_EXACT 1 395 #define NG_MATCH_ANY 2 396 397 static hook_p 398 pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match) 399 { 400 sessp sp = NULL; 401 negp neg = NULL; 402 priv_p privp = node->private; 403 hook_p allhook = NULL; 404 hook_p hook; 405 406 AAA 407 LIST_FOREACH(hook, &node->hooks, hooks) { 408 409 /* skip any hook that is debug or ethernet */ 410 if ((hook->private == &privp->debug_hook) 411 || (hook->private == &privp->ethernet_hook)) 412 continue; 413 sp = hook->private; 414 415 /* Skip any sessions which are not in LISTEN mode. */ 416 if ( sp->state != PPPOE_LISTENING) 417 continue; 418 419 neg = sp->neg; 420 421 /* Special case for a blank or "*" service name (wildcard) */ 422 if (match == NG_MATCH_ANY && neg->service_len == 1 && 423 neg->service.data[0] == '*') { 424 allhook = hook; 425 continue; 426 } 427 428 /* If the lengths don't match, that aint it. */ 429 if (neg->service_len != svc_len) 430 continue; 431 432 /* An exact match? */ 433 if (svc_len == 0) 434 break; 435 436 if (strncmp(svc_name, neg->service.data, svc_len) == 0) 437 break; 438 } 439 return (hook ? hook : allhook); 440 } 441 /************************************************************************** 442 * Routine to find a particular session that matches an incoming packet * 443 **************************************************************************/ 444 static hook_p 445 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh) 446 { 447 sessp sp = NULL; 448 hook_p hook = NULL; 449 priv_p privp = node->private; 450 u_int16_t session = ntohs(wh->ph.sid); 451 452 /* 453 * find matching peer/session combination. 454 */ 455 AAA 456 LIST_FOREACH(hook, &node->hooks, hooks) { 457 /* don't check special hooks */ 458 if ((hook->private == &privp->debug_hook) 459 || (hook->private == &privp->ethernet_hook)) { 460 continue; 461 } 462 sp = hook->private; 463 if ( ( (sp->state == PPPOE_CONNECTED) 464 || (sp->state == PPPOE_NEWCONNECTED) ) 465 && (sp->Session_ID == session) 466 && (bcmp(sp->pkt_hdr.eh.ether_dhost, 467 wh->eh.ether_shost, 468 ETHER_ADDR_LEN)) == 0) { 469 break; 470 } 471 } 472 return (hook); 473 } 474 475 static hook_p 476 pppoe_finduniq(node_p node, const struct pppoe_tag *tag) 477 { 478 hook_p hook = NULL; 479 priv_p privp = node->private; 480 union uniq uniq; 481 482 AAA 483 bcopy(tag->tag_data, uniq.bytes, sizeof(void *)); 484 /* cycle through all known hooks */ 485 LIST_FOREACH(hook, &node->hooks, hooks) { 486 /* don't check special hooks */ 487 if ((hook->private == &privp->debug_hook) 488 || (hook->private == &privp->ethernet_hook)) 489 continue; 490 if (uniq.pointer == hook->private) 491 break; 492 } 493 return (hook); 494 } 495 496 /************************************************************************** 497 * start of Netgraph entrypoints * 498 **************************************************************************/ 499 500 /* 501 * Allocate the private data structure and the generic node 502 * and link them together. 503 * 504 * ng_make_node_common() returns with a generic node struct 505 * with a single reference for us.. we transfer it to the 506 * private structure.. when we free the private struct we must 507 * unref the node so it gets freed too. 508 */ 509 static int 510 ng_pppoe_constructor(node_p *nodep) 511 { 512 priv_p privdata; 513 int error; 514 515 AAA 516 /* Initialize private descriptor */ 517 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT); 518 if (privdata == NULL) 519 return (ENOMEM); 520 bzero(privdata, sizeof(*privdata)); 521 522 /* Call the 'generic' (ie, superclass) node constructor */ 523 if ((error = ng_make_node_common(&typestruct, nodep))) { 524 FREE(privdata, M_NETGRAPH); 525 return (error); 526 } 527 528 /* Link structs together; this counts as our one reference to *nodep */ 529 (*nodep)->private = privdata; 530 privdata->node = *nodep; 531 return (0); 532 } 533 534 /* 535 * Give our ok for a hook to be added... 536 * point the hook's private info to the hook structure. 537 * 538 * The following hook names are special: 539 * Ethernet: the hook that should be connected to a NIC. 540 * debug: copies of data sent out here (when I write the code). 541 * All other hook names need only be unique. (the framework checks this). 542 */ 543 static int 544 ng_pppoe_newhook(node_p node, hook_p hook, const char *name) 545 { 546 const priv_p privp = node->private; 547 sessp sp; 548 549 AAA 550 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) { 551 privp->ethernet_hook = hook; 552 hook->private = &privp->ethernet_hook; 553 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) { 554 privp->debug_hook = hook; 555 hook->private = &privp->debug_hook; 556 } else { 557 /* 558 * Any other unique name is OK. 559 * The infrastructure has already checked that it's unique, 560 * so just allocate it and hook it in. 561 */ 562 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_NOWAIT); 563 if (sp == NULL) { 564 return (ENOMEM); 565 } 566 bzero(sp, sizeof(*sp)); 567 568 hook->private = sp; 569 sp->hook = hook; 570 } 571 return(0); 572 } 573 574 /* 575 * Get a netgraph control message. 576 * Check it is one we understand. If needed, send a response. 577 * We sometimes save the address for an async action later. 578 * Always free the message. 579 */ 580 static int 581 ng_pppoe_rcvmsg(node_p node, 582 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr) 583 { 584 priv_p privp = node->private; 585 struct ngpppoe_init_data *ourmsg = NULL; 586 struct ng_mesg *resp = NULL; 587 int error = 0; 588 hook_p hook = NULL; 589 sessp sp = NULL; 590 negp neg = NULL; 591 592 AAA 593 /* Deal with message according to cookie and command */ 594 switch (msg->header.typecookie) { 595 case NGM_PPPOE_COOKIE: 596 switch (msg->header.cmd) { 597 case NGM_PPPOE_CONNECT: 598 case NGM_PPPOE_LISTEN: 599 case NGM_PPPOE_OFFER: 600 case NGM_PPPOE_SERVICE: 601 ourmsg = (struct ngpppoe_init_data *)msg->data; 602 if (( sizeof(*ourmsg) > msg->header.arglen) 603 || ((sizeof(*ourmsg) + ourmsg->data_len) 604 > msg->header.arglen)) { 605 kprintf("pppoe_rcvmsg: bad arg size"); 606 LEAVE(EMSGSIZE); 607 } 608 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) { 609 kprintf("pppoe: init data too long (%d)\n", 610 ourmsg->data_len); 611 LEAVE(EMSGSIZE); 612 } 613 /* make sure strcmp will terminate safely */ 614 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0'; 615 616 /* cycle through all known hooks */ 617 LIST_FOREACH(hook, &node->hooks, hooks) { 618 if (hook->name 619 && strcmp(hook->name, ourmsg->hook) == 0) 620 break; 621 } 622 if (hook == NULL) { 623 LEAVE(ENOENT); 624 } 625 if ((hook->private == &privp->debug_hook) 626 || (hook->private == &privp->ethernet_hook)) { 627 LEAVE(EINVAL); 628 } 629 sp = hook->private; 630 631 if (msg->header.cmd == NGM_PPPOE_LISTEN) { 632 /* 633 * Ensure we aren't already listening for this 634 * service. 635 */ 636 if (pppoe_match_svc(node, ourmsg->data, 637 ourmsg->data_len, NG_MATCH_EXACT) != NULL) { 638 LEAVE(EEXIST); 639 } 640 } 641 642 /* 643 * PPPOE_SERVICE advertisments are set up 644 * on sessions that are in PRIMED state. 645 */ 646 if (msg->header.cmd == NGM_PPPOE_SERVICE) { 647 break; 648 } 649 if (sp->state |= PPPOE_SNONE) { 650 kprintf("pppoe: Session already active\n"); 651 LEAVE(EISCONN); 652 } 653 654 /* 655 * set up prototype header 656 */ 657 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_NOWAIT); 658 659 if (neg == NULL) { 660 kprintf("pppoe: Session out of memory\n"); 661 LEAVE(ENOMEM); 662 } 663 bzero(neg, sizeof(*neg)); 664 MGETHDR(neg->m, MB_DONTWAIT, MT_DATA); 665 if(neg->m == NULL) { 666 kprintf("pppoe: Session out of mbufs\n"); 667 FREE(neg, M_NETGRAPH); 668 LEAVE(ENOBUFS); 669 } 670 neg->m->m_pkthdr.rcvif = NULL; 671 MCLGET(neg->m, MB_DONTWAIT); 672 if ((neg->m->m_flags & M_EXT) == 0) { 673 kprintf("pppoe: Session out of mcls\n"); 674 m_freem(neg->m); 675 FREE(neg, M_NETGRAPH); 676 LEAVE(ENOBUFS); 677 } 678 sp->neg = neg; 679 callout_init(&neg->timeout_ch); 680 neg->m->m_len = sizeof(struct pppoe_full_hdr); 681 neg->pkt = mtod(neg->m, union packet*); 682 neg->pkt->pkt_header.eh = eh_prototype; 683 neg->pkt->pkt_header.ph.ver = 0x1; 684 neg->pkt->pkt_header.ph.type = 0x1; 685 neg->pkt->pkt_header.ph.sid = 0x0000; 686 neg->timeout = 0; 687 688 strlcpy(sp->creator, retaddr, NG_NODESIZ); 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 kprintf("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_HOOKSIZ - 1, 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 kprintf("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 kprintf("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 kprintf("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 kprintf("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 kprintf("no host unique field\n"); 1010 LEAVE(ENETUNREACH); 1011 } 1012 1013 sendhook = pppoe_finduniq(node, utag); 1014 if (sendhook == NULL) { 1015 kprintf("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 kprintf("session in wrong state\n"); 1026 LEAVE(ENETUNREACH); 1027 } 1028 neg = sp->neg; 1029 callout_stop(&neg->timeout_ch); 1030 1031 /* 1032 * This is the first time we hear 1033 * from the server, so note it's 1034 * unicast address, replacing the 1035 * broadcast address . 1036 */ 1037 bcopy(wh->eh.ether_shost, 1038 neg->pkt->pkt_header.eh.ether_dhost, 1039 ETHER_ADDR_LEN); 1040 neg->timeout = 0; 1041 neg->pkt->pkt_header.ph.code = PADR_CODE; 1042 init_tags(sp); 1043 insert_tag(sp, utag); /* Host Unique */ 1044 if ((tag = get_tag(ph, PTT_AC_COOKIE))) 1045 insert_tag(sp, tag); /* return cookie */ 1046 if ((tag = get_tag(ph, PTT_AC_NAME))) { 1047 insert_tag(sp, tag); /* return it */ 1048 send_acname(sp, tag); 1049 } 1050 insert_tag(sp, &neg->service.hdr); /* Service */ 1051 scan_tags(sp, ph); 1052 make_packet(sp); 1053 sp->state = PPPOE_SREQ; 1054 sendpacket(sp); 1055 break; 1056 case PADR_CODE: 1057 1058 /* 1059 * We are a server: 1060 * Use the ac_cookie tag to find the 1061 * hook this is in response to. 1062 */ 1063 utag = get_tag(ph, PTT_AC_COOKIE); 1064 if ((utag == NULL) 1065 || (ntohs(utag->tag_len) != sizeof(sp))) { 1066 LEAVE(ENETUNREACH); 1067 } 1068 1069 sendhook = pppoe_finduniq(node, utag); 1070 if (sendhook == NULL) { 1071 LEAVE(ENETUNREACH); 1072 } 1073 1074 /* 1075 * Check the session is in the right state. 1076 * It needs to be in PPPOE_SOFFER 1077 * or PPPOE_NEWCONNECTED. If the latter, 1078 * then this is a retry by the client. 1079 * so be nice, and resend. 1080 */ 1081 sp = sendhook->private; 1082 if (sp->state == PPPOE_NEWCONNECTED) { 1083 /* 1084 * Whoa! drop back to resend that 1085 * PADS packet. 1086 * We should still have a copy of it. 1087 */ 1088 sp->state = PPPOE_SOFFER; 1089 } 1090 if (sp->state != PPPOE_SOFFER) { 1091 LEAVE (ENETUNREACH); 1092 break; 1093 } 1094 neg = sp->neg; 1095 callout_stop(&neg->timeout_ch); 1096 neg->pkt->pkt_header.ph.code = PADS_CODE; 1097 if (sp->Session_ID == 0) 1098 neg->pkt->pkt_header.ph.sid = 1099 htons(sp->Session_ID 1100 = get_new_sid(node)); 1101 send_sessionid(sp); 1102 neg->timeout = 0; 1103 /* 1104 * start working out the tags to respond with. 1105 */ 1106 init_tags(sp); 1107 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 1108 if ((tag = get_tag(ph, PTT_SRV_NAME))) 1109 insert_tag(sp, tag);/* return service */ 1110 if ((tag = get_tag(ph, PTT_HOST_UNIQ))) 1111 insert_tag(sp, tag); /* return it */ 1112 insert_tag(sp, utag); /* ac_cookie */ 1113 scan_tags(sp, ph); 1114 make_packet(sp); 1115 sp->state = PPPOE_NEWCONNECTED; 1116 sendpacket(sp); 1117 /* 1118 * Having sent the last Negotiation header, 1119 * Set up the stored packet header to 1120 * be correct for the actual session. 1121 * But keep the negotialtion stuff 1122 * around in case we need to resend this last 1123 * packet. We'll discard it when we move 1124 * from NEWCONNECTED to CONNECTED 1125 */ 1126 sp->pkt_hdr = neg->pkt->pkt_header; 1127 if (pppoe_mode == PPPOE_NONSTANDARD) 1128 sp->pkt_hdr.eh.ether_type 1129 = ETHERTYPE_PPPOE_STUPID_SESS; 1130 else 1131 sp->pkt_hdr.eh.ether_type 1132 = ETHERTYPE_PPPOE_SESS; 1133 sp->pkt_hdr.ph.code = 0; 1134 pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 1135 break; 1136 case PADS_CODE: 1137 /* 1138 * We are a client: 1139 * Use the host_uniq tag to find the 1140 * hook this is in response to. 1141 * take the session ID and store it away. 1142 * Also make sure the pre-made header is 1143 * correct and set us into Session mode. 1144 */ 1145 utag = get_tag(ph, PTT_HOST_UNIQ); 1146 if ((utag == NULL) 1147 || (ntohs(utag->tag_len) != sizeof(sp))) { 1148 LEAVE (ENETUNREACH); 1149 break; 1150 } 1151 sendhook = pppoe_finduniq(node, utag); 1152 if (sendhook == NULL) { 1153 LEAVE(ENETUNREACH); 1154 } 1155 1156 /* 1157 * Check the session is in the right state. 1158 * It needs to be in PPPOE_SREQ. 1159 */ 1160 sp = sendhook->private; 1161 if (sp->state != PPPOE_SREQ) { 1162 LEAVE(ENETUNREACH); 1163 } 1164 neg = sp->neg; 1165 callout_stop(&neg->timeout_ch); 1166 neg->pkt->pkt_header.ph.sid = wh->ph.sid; 1167 sp->Session_ID = ntohs(wh->ph.sid); 1168 send_sessionid(sp); 1169 neg->timeout = 0; 1170 sp->state = PPPOE_CONNECTED; 1171 /* 1172 * Now we have gone to Connected mode, 1173 * Free all resources needed for 1174 * negotiation. 1175 * Keep a copy of the header we will be using. 1176 */ 1177 sp->pkt_hdr = neg->pkt->pkt_header; 1178 if (pppoe_mode == PPPOE_NONSTANDARD) 1179 sp->pkt_hdr.eh.ether_type 1180 = ETHERTYPE_PPPOE_STUPID_SESS; 1181 else 1182 sp->pkt_hdr.eh.ether_type 1183 = ETHERTYPE_PPPOE_SESS; 1184 sp->pkt_hdr.ph.code = 0; 1185 m_freem(neg->m); 1186 FREE(sp->neg, M_NETGRAPH); 1187 sp->neg = NULL; 1188 pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 1189 break; 1190 case PADT_CODE: 1191 /* 1192 * Send a 'close' message to the controlling 1193 * process (the one that set us up); 1194 * And then tear everything down. 1195 * 1196 * Find matching peer/session combination. 1197 */ 1198 sendhook = pppoe_findsession(node, wh); 1199 NG_FREE_DATA(m, meta); /* no longer needed */ 1200 if (sendhook == NULL) { 1201 LEAVE(ENETUNREACH); 1202 } 1203 /* send message to creator */ 1204 /* close hook */ 1205 if (sendhook) { 1206 ng_destroy_hook(sendhook); 1207 } 1208 break; 1209 default: 1210 LEAVE(EPFNOSUPPORT); 1211 } 1212 break; 1213 case ETHERTYPE_PPPOE_STUPID_SESS: 1214 case ETHERTYPE_PPPOE_SESS: 1215 /* 1216 * find matching peer/session combination. 1217 */ 1218 sendhook = pppoe_findsession(node, wh); 1219 if (sendhook == NULL) { 1220 LEAVE (ENETUNREACH); 1221 break; 1222 } 1223 sp = sendhook->private; 1224 m_adj(m, sizeof(*wh)); 1225 if (m->m_pkthdr.len < length) { 1226 /* Packet too short, dump it */ 1227 LEAVE(EMSGSIZE); 1228 } 1229 1230 /* Also need to trim excess at the end */ 1231 if (m->m_pkthdr.len > length) { 1232 m_adj(m, -((int)(m->m_pkthdr.len - length))); 1233 } 1234 if ( sp->state != PPPOE_CONNECTED) { 1235 if (sp->state == PPPOE_NEWCONNECTED) { 1236 sp->state = PPPOE_CONNECTED; 1237 /* 1238 * Now we have gone to Connected mode, 1239 * Free all resources needed for 1240 * negotiation. Be paranoid about 1241 * whether there may be a timeout. 1242 */ 1243 m_freem(sp->neg->m); 1244 callout_stop(&sp->neg->timeout_ch); 1245 FREE(sp->neg, M_NETGRAPH); 1246 sp->neg = NULL; 1247 } else { 1248 LEAVE (ENETUNREACH); 1249 break; 1250 } 1251 } 1252 NG_SEND_DATA( error, sendhook, m, meta); 1253 break; 1254 default: 1255 LEAVE(EPFNOSUPPORT); 1256 } 1257 } else { 1258 /* 1259 * Not ethernet or debug hook.. 1260 * 1261 * The packet has come in on a normal hook. 1262 * We need to find out what kind of hook, 1263 * So we can decide how to handle it. 1264 * Check the hook's state. 1265 */ 1266 sp = hook->private; 1267 switch (sp->state) { 1268 case PPPOE_NEWCONNECTED: 1269 case PPPOE_CONNECTED: { 1270 static const u_char addrctrl[] = { 0xff, 0x03 }; 1271 struct pppoe_full_hdr *wh; 1272 1273 /* 1274 * Remove PPP address and control fields, if any. 1275 * For example, ng_ppp(4) always sends LCP packets 1276 * with address and control fields as required by 1277 * generic PPP. PPPoE is an exception to the rule. 1278 */ 1279 if (m->m_pkthdr.len >= 2) { 1280 if (m->m_len < 2 && !(m = m_pullup(m, 2))) 1281 LEAVE(ENOBUFS); 1282 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0) 1283 m_adj(m, 2); 1284 } 1285 /* 1286 * Bang in a pre-made header, and set the length up 1287 * to be correct. Then send it to the ethernet driver. 1288 * But first correct the length. 1289 */ 1290 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len)); 1291 M_PREPEND(m, sizeof(*wh), MB_DONTWAIT); 1292 if (m == NULL) { 1293 LEAVE(ENOBUFS); 1294 } 1295 wh = mtod(m, struct pppoe_full_hdr *); 1296 bcopy(&sp->pkt_hdr, wh, sizeof(*wh)); 1297 NG_SEND_DATA( error, privp->ethernet_hook, m, meta); 1298 privp->packets_out++; 1299 break; 1300 } 1301 case PPPOE_PRIMED: 1302 /* 1303 * A PADI packet is being returned by the application 1304 * that has set up this hook. This indicates that it 1305 * wants us to offer service. 1306 */ 1307 neg = sp->neg; 1308 if (m->m_len < sizeof(*wh)) { 1309 m = m_pullup(m, sizeof(*wh)); 1310 if (m == NULL) { 1311 LEAVE(ENOBUFS); 1312 } 1313 } 1314 wh = mtod(m, struct pppoe_full_hdr *); 1315 ph = &wh->ph; 1316 session = ntohs(wh->ph.sid); 1317 length = ntohs(wh->ph.length); 1318 code = wh->ph.code; 1319 if ( code != PADI_CODE) { 1320 LEAVE(EINVAL); 1321 }; 1322 callout_stop(&neg->timeout_ch); 1323 1324 /* 1325 * This is the first time we hear 1326 * from the client, so note it's 1327 * unicast address, replacing the 1328 * broadcast address. 1329 */ 1330 bcopy(wh->eh.ether_shost, 1331 neg->pkt->pkt_header.eh.ether_dhost, 1332 ETHER_ADDR_LEN); 1333 sp->state = PPPOE_SOFFER; 1334 neg->timeout = 0; 1335 neg->pkt->pkt_header.ph.code = PADO_CODE; 1336 1337 /* 1338 * start working out the tags to respond with. 1339 */ 1340 uniqtag.hdr.tag_type = PTT_AC_COOKIE; 1341 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp)); 1342 uniqtag.data.pointer = sp; 1343 init_tags(sp); 1344 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 1345 if ((tag = get_tag(ph, PTT_SRV_NAME))) 1346 insert_tag(sp, tag); /* return service */ 1347 /* 1348 * If we have a NULL service request 1349 * and have an extra service defined in this hook, 1350 * then also add a tag for the extra service. 1351 * XXX this is a hack. eventually we should be able 1352 * to support advertising many services, not just one 1353 */ 1354 if (((tag == NULL) || (tag->tag_len == 0)) 1355 && (neg->service.hdr.tag_len != 0)) { 1356 insert_tag(sp, &neg->service.hdr); /* SERVICE */ 1357 } 1358 if ((tag = get_tag(ph, PTT_HOST_UNIQ))) 1359 insert_tag(sp, tag); /* returned hostunique */ 1360 insert_tag(sp, &uniqtag.hdr); 1361 scan_tags(sp, ph); 1362 make_packet(sp); 1363 sendpacket(sp); 1364 break; 1365 1366 /* 1367 * Packets coming from the hook make no sense 1368 * to sessions in these states. Throw them away. 1369 */ 1370 case PPPOE_SINIT: 1371 case PPPOE_SREQ: 1372 case PPPOE_SOFFER: 1373 case PPPOE_SNONE: 1374 case PPPOE_LISTENING: 1375 case PPPOE_DEAD: 1376 default: 1377 LEAVE(ENETUNREACH); 1378 } 1379 } 1380 quit: 1381 NG_FREE_DATA(m, meta); 1382 return error; 1383 } 1384 1385 /* 1386 * Do local shutdown processing.. 1387 * If we are a persistant device, we might refuse to go away, and 1388 * we'd only remove our links and reset ourself. 1389 */ 1390 static int 1391 ng_pppoe_rmnode(node_p node) 1392 { 1393 const priv_p privdata = node->private; 1394 1395 AAA 1396 node->flags |= NG_INVALID; 1397 ng_cutlinks(node); 1398 ng_unname(node); 1399 node->private = NULL; 1400 ng_unref(privdata->node); 1401 FREE(privdata, M_NETGRAPH); 1402 return (0); 1403 } 1404 1405 /* 1406 * This is called once we've already connected a new hook to the other node. 1407 * It gives us a chance to balk at the last minute. 1408 */ 1409 static int 1410 ng_pppoe_connect(hook_p hook) 1411 { 1412 /* be really amiable and just say "YUP that's OK by me! " */ 1413 return (0); 1414 } 1415 1416 /* 1417 * Hook disconnection 1418 * 1419 * Clean up all dangling links and information about the session/hook. 1420 * For this type, removal of the last link destroys the node 1421 */ 1422 static int 1423 ng_pppoe_disconnect(hook_p hook) 1424 { 1425 node_p node = hook->node; 1426 priv_p privp = node->private; 1427 sessp sp; 1428 int hooks; 1429 1430 AAA 1431 hooks = node->numhooks; /* this one already not counted */ 1432 if (hook->private == &privp->debug_hook) { 1433 privp->debug_hook = NULL; 1434 } else if (hook->private == &privp->ethernet_hook) { 1435 privp->ethernet_hook = NULL; 1436 ng_rmnode(node); 1437 } else { 1438 sp = hook->private; 1439 if (sp->state != PPPOE_SNONE ) { 1440 pppoe_send_event(sp, NGM_PPPOE_CLOSE); 1441 } 1442 /* 1443 * According to the spec, if we are connected, 1444 * we should send a DISC packet if we are shutting down 1445 * a session. 1446 */ 1447 if ((privp->ethernet_hook) 1448 && ((sp->state == PPPOE_CONNECTED) 1449 || (sp->state == PPPOE_NEWCONNECTED))) { 1450 struct mbuf *m; 1451 struct pppoe_full_hdr *wh; 1452 struct pppoe_tag *tag; 1453 int msglen = strlen(SIGNOFF); 1454 void *dummy = NULL; 1455 int error = 0; 1456 1457 /* revert the stored header to DISC/PADT mode */ 1458 wh = &sp->pkt_hdr; 1459 wh->ph.code = PADT_CODE; 1460 if (pppoe_mode == PPPOE_NONSTANDARD) 1461 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC; 1462 else 1463 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC; 1464 1465 /* generate a packet of that type */ 1466 MGETHDR(m, MB_DONTWAIT, MT_DATA); 1467 if(m == NULL) 1468 kprintf("pppoe: Session out of mbufs\n"); 1469 else { 1470 m->m_pkthdr.rcvif = NULL; 1471 m->m_pkthdr.len = m->m_len = sizeof(*wh); 1472 bcopy((caddr_t)wh, mtod(m, caddr_t), 1473 sizeof(*wh)); 1474 /* 1475 * Add a General error message and adjust 1476 * sizes 1477 */ 1478 wh = mtod(m, struct pppoe_full_hdr *); 1479 tag = wh->ph.tag; 1480 tag->tag_type = PTT_GEN_ERR; 1481 tag->tag_len = htons((u_int16_t)msglen); 1482 strncpy(tag->tag_data, SIGNOFF, msglen); 1483 m->m_pkthdr.len = (m->m_len += sizeof(*tag) + 1484 msglen); 1485 wh->ph.length = htons(sizeof(*tag) + msglen); 1486 NG_SEND_DATA(error, privp->ethernet_hook, m, 1487 dummy); 1488 } 1489 } 1490 /* 1491 * As long as we have somewhere to store the timeout handle, 1492 * we may have a timeout pending.. get rid of it. 1493 */ 1494 if (sp->neg) { 1495 callout_stop(&sp->neg->timeout_ch); 1496 if (sp->neg->m) 1497 m_freem(sp->neg->m); 1498 FREE(sp->neg, M_NETGRAPH); 1499 } 1500 FREE(sp, M_NETGRAPH); 1501 hook->private = NULL; 1502 /* work out how many session hooks there are */ 1503 /* Node goes away on last session hook removal */ 1504 if (privp->ethernet_hook) hooks -= 1; 1505 if (privp->debug_hook) hooks -= 1; 1506 } 1507 if (node->numhooks == 0) 1508 ng_rmnode(node); 1509 return (0); 1510 } 1511 1512 /* 1513 * timeouts come here. 1514 */ 1515 static void 1516 pppoe_ticker(void *arg) 1517 { 1518 hook_p hook = arg; 1519 sessp sp = hook->private; 1520 negp neg = sp->neg; 1521 int error = 0; 1522 struct mbuf *m0 = NULL; 1523 priv_p privp = hook->node->private; 1524 meta_p dummy = NULL; 1525 1526 crit_enter(); 1527 AAA 1528 switch(sp->state) { 1529 /* 1530 * resend the last packet, using an exponential backoff. 1531 * After a period of time, stop growing the backoff, 1532 * and either leave it, or revert to the start. 1533 */ 1534 case PPPOE_SINIT: 1535 case PPPOE_SREQ: 1536 /* timeouts on these produce resends */ 1537 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT); 1538 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 1539 callout_reset(&neg->timeout_ch, neg->timeout * hz, 1540 pppoe_ticker, hook); 1541 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) { 1542 if (sp->state == PPPOE_SREQ) { 1543 /* revert to SINIT mode */ 1544 pppoe_start(sp); 1545 } else { 1546 neg->timeout = PPPOE_TIMEOUT_LIMIT; 1547 } 1548 } 1549 break; 1550 case PPPOE_PRIMED: 1551 case PPPOE_SOFFER: 1552 /* a timeout on these says "give up" */ 1553 ng_destroy_hook(hook); 1554 break; 1555 default: 1556 /* timeouts have no meaning in other states */ 1557 kprintf("pppoe: unexpected timeout\n"); 1558 } 1559 crit_exit(); 1560 } 1561 1562 1563 static void 1564 sendpacket(sessp sp) 1565 { 1566 int error = 0; 1567 struct mbuf *m0 = NULL; 1568 hook_p hook = sp->hook; 1569 negp neg = sp->neg; 1570 priv_p privp = hook->node->private; 1571 meta_p dummy = NULL; 1572 1573 AAA 1574 switch(sp->state) { 1575 case PPPOE_LISTENING: 1576 case PPPOE_DEAD: 1577 case PPPOE_SNONE: 1578 case PPPOE_CONNECTED: 1579 kprintf("pppoe: sendpacket: unexpected state\n"); 1580 break; 1581 1582 case PPPOE_NEWCONNECTED: 1583 /* send the PADS without a timeout - we're now connected */ 1584 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT); 1585 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 1586 break; 1587 1588 case PPPOE_PRIMED: 1589 /* No packet to send, but set up the timeout */ 1590 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz, 1591 pppoe_ticker, hook); 1592 break; 1593 1594 case PPPOE_SOFFER: 1595 /* 1596 * send the offer but if they don't respond 1597 * in PPPOE_OFFER_TIMEOUT seconds, forget about it. 1598 */ 1599 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT); 1600 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 1601 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz, 1602 pppoe_ticker, hook); 1603 break; 1604 1605 case PPPOE_SINIT: 1606 case PPPOE_SREQ: 1607 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT); 1608 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 1609 callout_reset(&neg->timeout_ch, PPPOE_INITIAL_TIMEOUT * hz, 1610 pppoe_ticker, hook); 1611 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2; 1612 break; 1613 1614 default: 1615 error = EINVAL; 1616 kprintf("pppoe: timeout: bad state\n"); 1617 } 1618 /* return (error); */ 1619 } 1620 1621 /* 1622 * Parse an incoming packet to see if any tags should be copied to the 1623 * output packet. Don't do any tags that have been handled in the main 1624 * state machine. 1625 */ 1626 static const struct pppoe_tag* 1627 scan_tags(sessp sp, const struct pppoe_hdr* ph) 1628 { 1629 const char *const end = (const char *)next_tag(ph); 1630 const char *ptn; 1631 const struct pppoe_tag *pt = &ph->tag[0]; 1632 /* 1633 * Keep processing tags while a tag header will still fit. 1634 */ 1635 AAA 1636 while((const char*)(pt + 1) <= end) { 1637 /* 1638 * If the tag data would go past the end of the packet, abort. 1639 */ 1640 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len)); 1641 if(ptn > end) 1642 return NULL; 1643 1644 switch (pt->tag_type) { 1645 case PTT_RELAY_SID: 1646 insert_tag(sp, pt); 1647 break; 1648 case PTT_EOL: 1649 return NULL; 1650 case PTT_SRV_NAME: 1651 case PTT_AC_NAME: 1652 case PTT_HOST_UNIQ: 1653 case PTT_AC_COOKIE: 1654 case PTT_VENDOR: 1655 case PTT_SRV_ERR: 1656 case PTT_SYS_ERR: 1657 case PTT_GEN_ERR: 1658 break; 1659 } 1660 pt = (const struct pppoe_tag*)ptn; 1661 } 1662 return NULL; 1663 } 1664 1665 static int 1666 pppoe_send_event(sessp sp, enum cmd cmdid) 1667 { 1668 int error; 1669 struct ng_mesg *msg; 1670 struct ngpppoe_sts *sts; 1671 1672 AAA 1673 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid, 1674 sizeof(struct ngpppoe_sts), M_NOWAIT); 1675 if (msg == NULL) 1676 return (ENOMEM); 1677 sts = (struct ngpppoe_sts *)msg->data; 1678 strlcpy(sts->hook, sp->hook->name, NG_HOOKSIZ); 1679 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL); 1680 return (error); 1681 } 1682