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