5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elischer <julian@freebsd.org>
40 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
43 #define AAA printf("pppoe: %s\n", __func__ );
44 #define BBB printf("-%d-", __LINE__ );
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
56 #include <sys/sysctl.h>
57 #include <net/ethernet.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_pppoe.h>
64 #ifdef NG_SEPARATE_MALLOC
65 MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
67 #define M_NETGRAPH_PPPOE M_NETGRAPH
70 #define SIGNOFF "session closed"
71 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
74 * This section contains the netgraph method declarations for the
75 * pppoe node. These methods define the netgraph pppoe 'type'.
78 static ng_constructor_t ng_pppoe_constructor;
79 static ng_rcvmsg_t ng_pppoe_rcvmsg;
80 static ng_shutdown_t ng_pppoe_shutdown;
81 static ng_newhook_t ng_pppoe_newhook;
82 static ng_connect_t ng_pppoe_connect;
83 static ng_rcvdata_t ng_pppoe_rcvdata;
84 static ng_disconnect_t ng_pppoe_disconnect;
86 /* Parse type for struct ngpppoe_init_data */
87 static const struct ng_parse_struct_info ngpppoe_init_data_type_info
88 = NG_PPPOE_INIT_DATA_TYPE_INFO;
89 static const struct ng_parse_type ngpppoe_init_data_state_type = {
90 &ng_parse_struct_type,
91 &ngpppoe_init_data_type_info
94 /* Parse type for struct ngpppoe_sts */
95 static const struct ng_parse_struct_info ng_pppoe_sts_type_info
96 = NG_PPPOE_STS_TYPE_INFO;
97 static const struct ng_parse_type ng_pppoe_sts_state_type = {
98 &ng_parse_struct_type,
99 &ng_pppoe_sts_type_info
102 /* List of commands and how to convert arguments to/from ASCII */
103 static const struct ng_cmdlist ng_pppoe_cmds[] = {
108 &ngpppoe_init_data_state_type,
115 &ngpppoe_init_data_state_type,
122 &ngpppoe_init_data_state_type,
129 &ngpppoe_init_data_state_type,
136 &ng_pppoe_sts_state_type,
143 &ng_pppoe_sts_state_type,
150 &ng_pppoe_sts_state_type,
156 /* Netgraph node type descriptor */
157 static struct ng_type typestruct = {
161 ng_pppoe_constructor,
171 NETGRAPH_INIT(pppoe, &typestruct);
172 /* Depend on ng_ether so we can use the Ethernet parse type */
173 MODULE_DEPEND(ng_pppoe, ng_ether, 1, 1, 1);
176 * States for the session state machine.
177 * These have no meaning if there is no hook attached yet.
180 PPPOE_SNONE=0, /* [both] Initial state */
181 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
182 PPPOE_SINIT, /* [Client] Sent discovery initiation */
183 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
184 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
185 PPPOE_SREQ, /* [Client] Sent a Request */
186 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
187 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
188 PPPOE_DEAD /* [Both] */
191 #define NUMTAGS 20 /* number of tags we are set up to work with */
194 * Information we store for each hook on each node for negotiating the
195 * session. The mbuf and cluster are freed once negotiation has completed.
196 * The whole negotiation block is then discarded.
200 struct mbuf *m; /* holds cluster with last sent packet */
201 union packet *pkt; /* points within the above cluster */
202 struct callout_handle timeout_handle; /* see timeout(9) */
203 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
205 struct pppoe_tag *tags[NUMTAGS];
209 struct datatag service;
210 struct datatag ac_name;
212 typedef struct sess_neg *negp;
215 * Session information that is needed after connection.
219 u_int16_t Session_ID;
221 ng_ID_t creator; /* who to notify */
222 struct pppoe_full_hdr pkt_hdr; /* used when connected */
223 negp neg; /* used when negotiating */
224 /*struct sess_con *hash_next;*/ /* not yet used */
226 typedef struct sess_con *sessp;
229 * Information we store for each node
232 node_p node; /* back pointer to node */
233 hook_p ethernet_hook;
235 u_int packets_in; /* packets in from ethernet */
236 u_int packets_out; /* packets out towards ethernet */
238 /*struct sess_con *buckets[HASH_SIZE];*/ /* not yet used */
240 typedef struct PPPOE *priv_p;
242 struct ether_header eh_prototype =
243 {{0xff,0xff,0xff,0xff,0xff,0xff},
244 {0x00,0x00,0x00,0x00,0x00,0x00},
245 ETHERTYPE_PPPOE_DISC};
247 static int nonstandard;
249 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS)
255 error = sysctl_handle_int(oidp, &val, sizeof(int), req);
256 if (error != 0 || req->newptr == NULL)
260 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
263 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
268 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW,
269 0, sizeof(int), ngpppoe_set_ethertype, "I", "select normal or stupid ISP");
272 char bytes[sizeof(void *)];
276 #define LEAVE(x) do { error = x; goto quit; } while(0)
277 static void pppoe_start(sessp sp);
278 static void sendpacket(sessp sp);
279 static void pppoe_ticker(void *arg);
280 static struct pppoe_tag* scan_tags(sessp sp, struct pppoe_hdr* ph);
281 static int pppoe_send_event(sessp sp, enum cmd cmdid);
283 /*************************************************************************
284 * Some basic utilities from the Linux version with author's permission.*
285 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
286 ************************************************************************/
289 * Generate a new session id
290 * XXX find out the FreeBSD locking scheme.
293 get_new_sid(node_p node)
295 static int pppoe_sid = 10;
299 priv_p privp = NG_NODE_PRIVATE(node);
305 * Spec says 0xFFFF is reserved.
306 * Also don't use 0x0000
313 /* Check it isn't already in use */
314 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
315 /* don't check special hooks */
316 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
317 || (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
319 sp = NG_HOOK_PRIVATE(hook);
320 if (sp->Session_ID == val)
329 * Return the location where the next tag can be put
331 static __inline struct pppoe_tag*
332 next_tag(struct pppoe_hdr* ph)
334 return (struct pppoe_tag*)(((char*)&ph->tag[0]) + ntohs(ph->length));
338 * Look for a tag of a specific type
339 * Don't trust any length the other end says.
340 * but assume we already sanity checked ph->length.
342 static struct pppoe_tag*
343 get_tag(struct pppoe_hdr* ph, u_int16_t idx)
345 char *end = (char *)next_tag(ph);
347 struct pppoe_tag *pt = &ph->tag[0];
349 * Keep processing tags while a tag header will still fit.
352 while((char*)(pt + 1) <= end) {
354 * If the tag data would go past the end of the packet, abort.
356 ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
360 if(pt->tag_type == idx)
363 pt = (struct pppoe_tag*)ptn;
368 /**************************************************************************
369 * inlines to initialise or add tags to a session's tag list,
370 **************************************************************************/
372 * Initialise the session's tag list
378 if(sp->neg == NULL) {
379 printf("pppoe: asked to init NULL neg pointer\n");
382 sp->neg->numtags = 0;
386 insert_tag(sessp sp, struct pppoe_tag *tp)
392 if((neg = sp->neg) == NULL) {
393 printf("pppoe: asked to use NULL neg pointer\n");
396 if ((i = neg->numtags++) < NUMTAGS) {
399 printf("pppoe: asked to add too many tags to packet\n");
405 * Make up a packet, using the tags filled out for the session.
407 * Assume that the actual pppoe header and ethernet header
408 * are filled out externally to this routine.
409 * Also assume that neg->wh points to the correct
410 * location at the front of the buffer space.
413 make_packet(sessp sp) {
414 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
415 struct pppoe_tag **tag;
419 u_int16_t length = 0;
422 if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
423 printf("pppoe: make_packet called from wrong state\n");
425 dp = (char *)wh->ph.tag;
426 for (count = 0, tag = sp->neg->tags;
427 ((count < sp->neg->numtags) && (count < NUMTAGS));
429 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
430 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
431 printf("pppoe: tags too long\n");
432 sp->neg->numtags = count;
433 break; /* XXX chop off what's too long */
435 bcopy((char *)*tag, (char *)dp, tlen);
439 wh->ph.length = htons(length);
440 sp->neg->m->m_len = length + sizeof(*wh);
441 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
444 /**************************************************************************
445 * Routine to match a service offered *
446 **************************************************************************/
448 * Find a hook that has a service string that matches that
449 * we are seeking. for now use a simple string.
450 * In the future we may need something like regexp().
451 * for testing allow a null string to match 1st found and a null service
452 * to match all requests. Also make '*' do the same.
455 #define NG_MATCH_EXACT 1
456 #define NG_MATCH_ANY 2
459 pppoe_match_svc(node_p node, char *svc_name, int svc_len, int match)
463 priv_p privp = NG_NODE_PRIVATE(node);
464 hook_p allhook = NULL;
468 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
470 /* skip any hook that is debug or ethernet */
471 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
472 || (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
474 sp = NG_HOOK_PRIVATE(hook);
476 /* Skip any sessions which are not in LISTEN mode. */
477 if ( sp->state != PPPOE_LISTENING)
482 /* Special case for a blank or "*" service name (wildcard) */
483 if (match == NG_MATCH_ANY && neg->service_len == 1 &&
484 neg->service.data[0] == '*') {
489 /* If the lengths don't match, that aint it. */
490 if (neg->service_len != svc_len)
493 /* An exact match? */
497 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
500 return (hook ? hook : allhook);
502 /**************************************************************************
503 * Routine to find a particular session that matches an incoming packet *
504 **************************************************************************/
506 pppoe_findsession(node_p node, struct pppoe_full_hdr *wh)
510 priv_p privp = NG_NODE_PRIVATE(node);
511 u_int16_t session = ntohs(wh->ph.sid);
514 * find matching peer/session combination.
517 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
518 /* don't check special hooks */
519 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
520 || (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
523 sp = NG_HOOK_PRIVATE(hook);
524 if ( ( (sp->state == PPPOE_CONNECTED)
525 || (sp->state == PPPOE_NEWCONNECTED) )
526 && (sp->Session_ID == session)
527 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
529 ETHER_ADDR_LEN)) == 0) {
537 pppoe_finduniq(node_p node, struct pppoe_tag *tag)
540 priv_p privp = NG_NODE_PRIVATE(node);
544 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
545 /* cycle through all known hooks */
546 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
547 /* don't check special hooks */
548 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
549 || (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
551 if (uniq.pointer == NG_HOOK_PRIVATE(hook))
557 /**************************************************************************
558 * start of Netgraph entrypoints *
559 **************************************************************************/
562 * Allocate the private data structure and the generic node
563 * and link them together.
565 * ng_make_node_common() returns with a generic node struct
566 * with a single reference for us.. we transfer it to the
567 * private structure.. when we free the private struct we must
568 * unref the node so it gets freed too.
571 ng_pppoe_constructor(node_p node)
576 /* Initialize private descriptor */
577 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH_PPPOE,
579 if (privdata == NULL)
582 /* Link structs together; this counts as our one reference to *nodep */
583 NG_NODE_SET_PRIVATE(node, privdata);
584 privdata->node = node;
589 * Give our ok for a hook to be added...
590 * point the hook's private info to the hook structure.
592 * The following hook names are special:
593 * Ethernet: the hook that should be connected to a NIC.
594 * debug: copies of data sent out here (when I write the code).
595 * All other hook names need only be unique. (the framework checks this).
598 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
600 const priv_p privp = NG_NODE_PRIVATE(node);
604 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
605 privp->ethernet_hook = hook;
606 NG_HOOK_SET_PRIVATE(hook, &privp->ethernet_hook);
607 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
608 privp->debug_hook = hook;
609 NG_HOOK_SET_PRIVATE(hook, &privp->debug_hook);
612 * Any other unique name is OK.
613 * The infrastructure has already checked that it's unique,
614 * so just allocate it and hook it in.
616 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
621 NG_HOOK_SET_PRIVATE(hook, sp);
628 * Get a netgraph control message.
629 * Check it is one we understand. If needed, send a response.
630 * We sometimes save the address for an async action later.
631 * Always free the message.
634 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
636 priv_p privp = NG_NODE_PRIVATE(node);
637 struct ngpppoe_init_data *ourmsg = NULL;
638 struct ng_mesg *resp = NULL;
646 NGI_GET_MSG(item, msg);
647 /* Deal with message according to cookie and command */
648 switch (msg->header.typecookie) {
649 case NGM_PPPOE_COOKIE:
650 switch (msg->header.cmd) {
651 case NGM_PPPOE_CONNECT:
652 case NGM_PPPOE_LISTEN:
653 case NGM_PPPOE_OFFER:
654 case NGM_PPPOE_SERVICE:
655 ourmsg = (struct ngpppoe_init_data *)msg->data;
656 if (msg->header.arglen < sizeof(*ourmsg)) {
657 printf("pppoe: init data too small\n");
660 if (msg->header.arglen - sizeof(*ourmsg) >
661 PPPOE_SERVICE_NAME_SIZE) {
662 printf("pppoe_rcvmsg: service name too big");
665 if (msg->header.arglen - sizeof(*ourmsg) <
667 printf("pppoe: init data has bad length,"
668 " %d should be %d\n", ourmsg->data_len,
669 msg->header.arglen - sizeof (*ourmsg));
673 /* make sure strcmp will terminate safely */
674 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
676 /* cycle through all known hooks */
677 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
678 if (NG_HOOK_NAME(hook)
679 && strcmp(NG_HOOK_NAME(hook), ourmsg->hook) == 0)
685 if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
686 || (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
689 sp = NG_HOOK_PRIVATE(hook);
691 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
693 * Ensure we aren't already listening for this
696 if (pppoe_match_svc(node, ourmsg->data,
697 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
703 * PPPOE_SERVICE advertisments are set up
704 * on sessions that are in PRIMED state.
706 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
709 if (sp->state |= PPPOE_SNONE) {
710 printf("pppoe: Session already active\n");
715 * set up prototype header
717 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH_PPPOE,
721 printf("pppoe: Session out of memory\n");
724 MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
726 printf("pppoe: Session out of mbufs\n");
727 FREE(neg, M_NETGRAPH_PPPOE);
730 neg->m->m_pkthdr.rcvif = NULL;
731 MCLGET(neg->m, M_DONTWAIT);
732 if ((neg->m->m_flags & M_EXT) == 0) {
733 printf("pppoe: Session out of mcls\n");
735 FREE(neg, M_NETGRAPH_PPPOE);
739 callout_handle_init( &neg->timeout_handle);
740 neg->m->m_len = sizeof(struct pppoe_full_hdr);
741 neg->pkt = mtod(neg->m, union packet*);
742 neg->pkt->pkt_header.eh = eh_prototype;
743 neg->pkt->pkt_header.ph.ver = 0x1;
744 neg->pkt->pkt_header.ph.type = 0x1;
745 neg->pkt->pkt_header.ph.sid = 0x0000;
748 sp->creator = NGI_RETADDR(item);
750 switch (msg->header.cmd) {
751 case NGM_PPPOE_GET_STATUS:
753 struct ngpppoestat *stats;
755 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
759 stats = (struct ngpppoestat *) resp->data;
760 stats->packets_in = privp->packets_in;
761 stats->packets_out = privp->packets_out;
764 case NGM_PPPOE_CONNECT:
766 * Check the hook exists and is Uninitialised.
767 * Send a PADI request, and start the timeout logic.
768 * Store the originator of this message so we can send
769 * a success of fail message to them later.
770 * Move the session to SINIT
771 * Set up the session to the correct state and
774 neg->service.hdr.tag_type = PTT_SRV_NAME;
775 neg->service.hdr.tag_len =
776 htons((u_int16_t)ourmsg->data_len);
777 if (ourmsg->data_len)
778 bcopy(ourmsg->data, neg->service.data,
780 neg->service_len = ourmsg->data_len;
783 case NGM_PPPOE_LISTEN:
785 * Check the hook exists and is Uninitialised.
786 * Install the service matching string.
787 * Store the originator of this message so we can send
788 * a success of fail message to them later.
789 * Move the hook to 'LISTENING'
791 neg->service.hdr.tag_type = PTT_SRV_NAME;
792 neg->service.hdr.tag_len =
793 htons((u_int16_t)ourmsg->data_len);
795 if (ourmsg->data_len)
796 bcopy(ourmsg->data, neg->service.data,
798 neg->service_len = ourmsg->data_len;
799 neg->pkt->pkt_header.ph.code = PADT_CODE;
801 * wait for PADI packet coming from ethernet
803 sp->state = PPPOE_LISTENING;
805 case NGM_PPPOE_OFFER:
807 * Check the hook exists and is Uninitialised.
808 * Store the originator of this message so we can send
809 * a success of fail message to them later.
810 * Store the AC-Name given and go to PRIMED.
812 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
813 neg->ac_name.hdr.tag_len =
814 htons((u_int16_t)ourmsg->data_len);
815 if (ourmsg->data_len)
816 bcopy(ourmsg->data, neg->ac_name.data,
818 neg->ac_name_len = ourmsg->data_len;
819 neg->pkt->pkt_header.ph.code = PADO_CODE;
821 * Wait for PADI packet coming from hook
823 sp->state = PPPOE_PRIMED;
825 case NGM_PPPOE_SERVICE:
827 * Check the session is primed.
828 * for now just allow ONE service to be advertised.
829 * If you do it twice you just overwrite.
831 if (sp->state != PPPOE_PRIMED) {
832 printf("pppoe: Session not primed\n");
836 neg->service.hdr.tag_type = PTT_SRV_NAME;
837 neg->service.hdr.tag_len =
838 htons((u_int16_t)ourmsg->data_len);
840 if (ourmsg->data_len)
841 bcopy(ourmsg->data, neg->service.data,
843 neg->service_len = ourmsg->data_len;
853 /* Take care of synchronous response, if any */
855 NG_RESPOND_MSG(error, node, item, resp);
856 /* Free the message and return */
862 * Start a client into the first state. A separate function because
863 * it can be needed if the negotiation times out.
866 pppoe_start(sessp sp)
869 struct pppoe_tag hdr;
871 } __attribute ((packed)) uniqtag;
874 * kick the state machine into starting up
877 sp->state = PPPOE_SINIT;
878 /* reset the packet header to broadcast */
879 sp->neg->pkt->pkt_header.eh = eh_prototype;
880 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
881 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
882 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
883 uniqtag.data.pointer = sp;
885 insert_tag(sp, &uniqtag.hdr);
886 insert_tag(sp, &sp->neg->service.hdr);
892 send_acname(sessp sp, struct pppoe_tag *tag)
896 struct ngpppoe_sts *sts;
898 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
899 sizeof(struct ngpppoe_sts), M_NOWAIT);
903 sts = (struct ngpppoe_sts *)msg->data;
904 strncpy(sts->hook, tag->tag_data,
905 min(NG_HOOKLEN + 1, ntohs(tag->tag_len)));
906 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, NULL);
912 send_sessionid(sessp sp)
917 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
918 sizeof(u_int16_t), M_NOWAIT);
922 *(u_int16_t *)msg->data = sp->Session_ID;
923 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, NULL);
929 * Receive data, and do something with it.
930 * The caller will never free m or meta, so
931 * if we use up this data or abort we must free BOTH of these.
934 ng_pppoe_rcvdata(hook_p hook, item_p item)
936 node_p node = NG_HOOK_NODE(hook);
937 const priv_p privp = NG_NODE_PRIVATE(node);
938 sessp sp = NG_HOOK_PRIVATE(hook);
939 struct pppoe_full_hdr *wh;
940 struct pppoe_hdr *ph;
945 struct pppoe_tag *utag = NULL, *tag = NULL;
948 struct pppoe_tag hdr;
950 } __attribute ((packed)) uniqtag;
956 if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
958 * Data from the debug hook gets sent without modification
959 * straight to the ethernet.
961 NG_FWD_ITEM_HOOK( error, item, privp->ethernet_hook);
962 privp->packets_out++;
963 } else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
966 * Dig out various fields from the packet.
967 * use them to decide where to send it.
971 if( m->m_len < sizeof(*wh)) {
972 m = m_pullup(m, sizeof(*wh)); /* Checks length */
974 printf("couldn't m_pullup\n");
978 wh = mtod(m, struct pppoe_full_hdr *);
979 length = ntohs(wh->ph.length);
980 switch(wh->eh.ether_type) {
981 case ETHERTYPE_PPPOE_STUPID_DISC:
983 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
985 case ETHERTYPE_PPPOE_DISC:
987 * We need to try to make sure that the tag area
988 * is contiguous, or we could wander off the end
989 * of a buffer and make a mess.
990 * (Linux wouldn't have this problem).
992 if (m->m_pkthdr.len <= MHLEN) {
993 if( m->m_len < m->m_pkthdr.len) {
994 m = m_pullup(m, m->m_pkthdr.len);
996 printf("couldn't m_pullup\n");
1001 if (m->m_len != m->m_pkthdr.len) {
1003 * It's not all in one piece.
1004 * We need to do extra work.
1005 * Put it into a cluster.
1008 n = m_dup(m, M_DONTWAIT);
1012 /* just check we got a cluster */
1013 if (m->m_len != m->m_pkthdr.len) {
1019 printf("packet fragmented\n");
1023 wh = mtod(m, struct pppoe_full_hdr *);
1024 length = ntohs(wh->ph.length);
1026 session = ntohs(wh->ph.sid);
1033 * Look for a hook with the required service
1034 * and send the ENTIRE packet up there.
1035 * It should come back to a new hook in
1036 * PRIMED state. Look there for further
1039 tag = get_tag(ph, PTT_SRV_NAME);
1041 printf("no service tag\n");
1044 sendhook = pppoe_match_svc(NG_HOOK_NODE(hook),
1045 tag->tag_data, ntohs(tag->tag_len),
1048 NG_FWD_NEW_DATA(error, item,
1057 * Use the host_uniq tag to find the
1058 * hook this is in response to.
1059 * Received #2, now send #3
1060 * For now simply accept the first we receive.
1062 utag = get_tag(ph, PTT_HOST_UNIQ);
1064 || (ntohs(utag->tag_len) != sizeof(sp))) {
1065 printf("no host unique field\n");
1069 sendhook = pppoe_finduniq(node, utag);
1070 if (sendhook == NULL) {
1071 printf("no matching session\n");
1076 * Check the session is in the right state.
1077 * It needs to be in PPPOE_SINIT.
1079 sp = NG_HOOK_PRIVATE(sendhook);
1080 if (sp->state != PPPOE_SINIT) {
1081 printf("session in wrong state\n");
1085 untimeout(pppoe_ticker, sendhook,
1086 neg->timeout_handle);
1089 * This is the first time we hear
1090 * from the server, so note it's
1091 * unicast address, replacing the
1092 * broadcast address .
1094 bcopy(wh->eh.ether_shost,
1095 neg->pkt->pkt_header.eh.ether_dhost,
1098 neg->pkt->pkt_header.ph.code = PADR_CODE;
1100 insert_tag(sp, utag); /* Host Unique */
1101 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1102 insert_tag(sp, tag); /* return cookie */
1103 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1104 insert_tag(sp, tag); /* return it */
1105 send_acname(sp, tag);
1107 insert_tag(sp, &neg->service.hdr); /* Service */
1110 sp->state = PPPOE_SREQ;
1117 * Use the ac_cookie tag to find the
1118 * hook this is in response to.
1120 utag = get_tag(ph, PTT_AC_COOKIE);
1122 || (ntohs(utag->tag_len) != sizeof(sp))) {
1126 sendhook = pppoe_finduniq(node, utag);
1127 if (sendhook == NULL) {
1132 * Check the session is in the right state.
1133 * It needs to be in PPPOE_SOFFER
1134 * or PPPOE_NEWCONNECTED. If the latter,
1135 * then this is a retry by the client.
1136 * so be nice, and resend.
1138 sp = NG_HOOK_PRIVATE(sendhook);
1139 if (sp->state == PPPOE_NEWCONNECTED) {
1141 * Whoa! drop back to resend that
1143 * We should still have a copy of it.
1145 sp->state = PPPOE_SOFFER;
1147 if (sp->state != PPPOE_SOFFER) {
1148 LEAVE (ENETUNREACH);
1152 untimeout(pppoe_ticker, sendhook,
1153 neg->timeout_handle);
1154 neg->pkt->pkt_header.ph.code = PADS_CODE;
1155 if (sp->Session_ID == 0)
1156 neg->pkt->pkt_header.ph.sid =
1157 htons(sp->Session_ID
1158 = get_new_sid(node));
1162 * start working out the tags to respond with.
1165 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1166 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1167 insert_tag(sp, tag);/* return service */
1168 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1169 insert_tag(sp, tag); /* return it */
1170 insert_tag(sp, utag); /* ac_cookie */
1173 sp->state = PPPOE_NEWCONNECTED;
1176 * Having sent the last Negotiation header,
1177 * Set up the stored packet header to
1178 * be correct for the actual session.
1179 * But keep the negotialtion stuff
1180 * around in case we need to resend this last
1181 * packet. We'll discard it when we move
1182 * from NEWCONNECTED to CONNECTED
1184 sp->pkt_hdr = neg->pkt->pkt_header;
1186 sp->pkt_hdr.eh.ether_type
1187 = ETHERTYPE_PPPOE_STUPID_SESS;
1189 sp->pkt_hdr.eh.ether_type
1190 = ETHERTYPE_PPPOE_SESS;
1191 sp->pkt_hdr.ph.code = 0;
1192 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1197 * Use the host_uniq tag to find the
1198 * hook this is in response to.
1199 * take the session ID and store it away.
1200 * Also make sure the pre-made header is
1201 * correct and set us into Session mode.
1203 utag = get_tag(ph, PTT_HOST_UNIQ);
1205 || (ntohs(utag->tag_len) != sizeof(sp))) {
1206 LEAVE (ENETUNREACH);
1209 sendhook = pppoe_finduniq(node, utag);
1210 if (sendhook == NULL) {
1215 * Check the session is in the right state.
1216 * It needs to be in PPPOE_SREQ.
1218 sp = NG_HOOK_PRIVATE(sendhook);
1219 if (sp->state != PPPOE_SREQ) {
1223 untimeout(pppoe_ticker, sendhook,
1224 neg->timeout_handle);
1225 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1226 sp->Session_ID = ntohs(wh->ph.sid);
1229 sp->state = PPPOE_CONNECTED;
1231 * Now we have gone to Connected mode,
1232 * Free all resources needed for
1234 * Keep a copy of the header we will be using.
1236 sp->pkt_hdr = neg->pkt->pkt_header;
1238 sp->pkt_hdr.eh.ether_type
1239 = ETHERTYPE_PPPOE_STUPID_SESS;
1241 sp->pkt_hdr.eh.ether_type
1242 = ETHERTYPE_PPPOE_SESS;
1243 sp->pkt_hdr.ph.code = 0;
1245 FREE(sp->neg, M_NETGRAPH_PPPOE);
1247 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1251 * Send a 'close' message to the controlling
1252 * process (the one that set us up);
1253 * And then tear everything down.
1255 * Find matching peer/session combination.
1257 sendhook = pppoe_findsession(node, wh);
1258 if (sendhook == NULL) {
1261 /* send message to creator */
1264 ng_rmhook_self(sendhook);
1268 LEAVE(EPFNOSUPPORT);
1271 case ETHERTYPE_PPPOE_STUPID_SESS:
1272 case ETHERTYPE_PPPOE_SESS:
1274 * find matching peer/session combination.
1276 sendhook = pppoe_findsession(node, wh);
1277 if (sendhook == NULL) {
1278 LEAVE (ENETUNREACH);
1281 sp = NG_HOOK_PRIVATE(sendhook);
1282 m_adj(m, sizeof(*wh));
1283 if (m->m_pkthdr.len < length) {
1284 /* Packet too short, dump it */
1288 /* Also need to trim excess at the end */
1289 if (m->m_pkthdr.len > length) {
1290 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1292 if ( sp->state != PPPOE_CONNECTED) {
1293 if (sp->state == PPPOE_NEWCONNECTED) {
1294 sp->state = PPPOE_CONNECTED;
1296 * Now we have gone to Connected mode,
1297 * Free all resources needed for
1298 * negotiation. Be paranoid about
1299 * whether there may be a timeout.
1301 m_freem(sp->neg->m);
1302 untimeout(pppoe_ticker, sendhook,
1303 sp->neg->timeout_handle);
1304 FREE(sp->neg, M_NETGRAPH_PPPOE);
1307 LEAVE (ENETUNREACH);
1311 NG_FWD_NEW_DATA( error, item, sendhook, m);
1314 LEAVE(EPFNOSUPPORT);
1318 * Not ethernet or debug hook..
1320 * The packet has come in on a normal hook.
1321 * We need to find out what kind of hook,
1322 * So we can decide how to handle it.
1323 * Check the hook's state.
1325 sp = NG_HOOK_PRIVATE(hook);
1326 switch (sp->state) {
1327 case PPPOE_NEWCONNECTED:
1328 case PPPOE_CONNECTED: {
1329 static const u_char addrctrl[] = { 0xff, 0x03 };
1330 struct pppoe_full_hdr *wh;
1333 * Remove PPP address and control fields, if any.
1334 * For example, ng_ppp(4) always sends LCP packets
1335 * with address and control fields as required by
1336 * generic PPP. PPPoE is an exception to the rule.
1338 if (m->m_pkthdr.len >= 2) {
1339 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1341 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1345 * Bang in a pre-made header, and set the length up
1346 * to be correct. Then send it to the ethernet driver.
1347 * But first correct the length.
1349 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1350 M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1354 wh = mtod(m, struct pppoe_full_hdr *);
1355 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1356 NG_FWD_NEW_DATA( error, item, privp->ethernet_hook, m);
1357 privp->packets_out++;
1362 * A PADI packet is being returned by the application
1363 * that has set up this hook. This indicates that it
1364 * wants us to offer service.
1367 if (m->m_len < sizeof(*wh)) {
1368 m = m_pullup(m, sizeof(*wh));
1373 wh = mtod(m, struct pppoe_full_hdr *);
1375 session = ntohs(wh->ph.sid);
1376 length = ntohs(wh->ph.length);
1378 if ( code != PADI_CODE) {
1381 untimeout(pppoe_ticker, hook,
1382 neg->timeout_handle);
1385 * This is the first time we hear
1386 * from the client, so note it's
1387 * unicast address, replacing the
1388 * broadcast address.
1390 bcopy(wh->eh.ether_shost,
1391 neg->pkt->pkt_header.eh.ether_dhost,
1393 sp->state = PPPOE_SOFFER;
1395 neg->pkt->pkt_header.ph.code = PADO_CODE;
1398 * start working out the tags to respond with.
1400 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1401 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1402 uniqtag.data.pointer = sp;
1404 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1405 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1406 insert_tag(sp, tag); /* return service */
1408 * If we have a NULL service request
1409 * and have an extra service defined in this hook,
1410 * then also add a tag for the extra service.
1411 * XXX this is a hack. eventually we should be able
1412 * to support advertising many services, not just one
1414 if (((tag == NULL) || (tag->tag_len == 0))
1415 && (neg->service.hdr.tag_len != 0)) {
1416 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1418 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1419 insert_tag(sp, tag); /* returned hostunique */
1420 insert_tag(sp, &uniqtag.hdr);
1427 * Packets coming from the hook make no sense
1428 * to sessions in these states. Throw them away.
1434 case PPPOE_LISTENING:
1448 * Do local shutdown processing..
1449 * If we are a persistant device, we might refuse to go away, and
1450 * we'd only remove our links and reset ourself.
1453 ng_pppoe_shutdown(node_p node)
1455 const priv_p privdata = NG_NODE_PRIVATE(node);
1458 NG_NODE_SET_PRIVATE(node, NULL);
1459 NG_NODE_UNREF(privdata->node);
1460 FREE(privdata, M_NETGRAPH_PPPOE);
1465 * This is called once we've already connected a new hook to the other node.
1466 * It gives us a chance to balk at the last minute.
1469 ng_pppoe_connect(hook_p hook)
1471 /* be really amiable and just say "YUP that's OK by me! " */
1476 * Hook disconnection
1478 * Clean up all dangling links and information about the session/hook.
1479 * For this type, removal of the last link destroys the node
1482 ng_pppoe_disconnect(hook_p hook)
1484 node_p node = NG_HOOK_NODE(hook);
1485 priv_p privp = NG_NODE_PRIVATE(node);
1490 hooks = NG_NODE_NUMHOOKS(node); /* this one already not counted */
1491 if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
1492 privp->debug_hook = NULL;
1493 } else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
1494 privp->ethernet_hook = NULL;
1495 if (NG_NODE_IS_VALID(node))
1496 ng_rmnode_self(node);
1498 sp = NG_HOOK_PRIVATE(hook);
1499 if (sp->state != PPPOE_SNONE ) {
1500 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1503 * According to the spec, if we are connected,
1504 * we should send a DISC packet if we are shutting down
1507 if ((privp->ethernet_hook)
1508 && ((sp->state == PPPOE_CONNECTED)
1509 || (sp->state == PPPOE_NEWCONNECTED))) {
1511 struct pppoe_full_hdr *wh;
1512 struct pppoe_tag *tag;
1513 int msglen = strlen(SIGNOFF);
1516 /* revert the stored header to DISC/PADT mode */
1518 wh->ph.code = PADT_CODE;
1520 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1522 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1524 /* generate a packet of that type */
1525 MGETHDR(m, M_DONTWAIT, MT_DATA);
1527 printf("pppoe: Session out of mbufs\n");
1529 m->m_pkthdr.rcvif = NULL;
1530 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1531 bcopy((caddr_t)wh, mtod(m, caddr_t),
1534 * Add a General error message and adjust
1537 wh = mtod(m, struct pppoe_full_hdr *);
1539 tag->tag_type = PTT_GEN_ERR;
1540 tag->tag_len = htons((u_int16_t)msglen);
1541 strncpy(tag->tag_data, SIGNOFF, msglen);
1542 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1544 wh->ph.length = htons(sizeof(*tag) + msglen);
1545 NG_SEND_DATA_ONLY(error,
1546 privp->ethernet_hook, m);
1550 * As long as we have somewhere to store the timeout handle,
1551 * we may have a timeout pending.. get rid of it.
1554 untimeout(pppoe_ticker, hook, sp->neg->timeout_handle);
1556 m_freem(sp->neg->m);
1557 FREE(sp->neg, M_NETGRAPH_PPPOE);
1559 FREE(sp, M_NETGRAPH_PPPOE);
1560 NG_HOOK_SET_PRIVATE(hook, NULL);
1561 /* work out how many session hooks there are */
1562 /* Node goes away on last session hook removal */
1563 if (privp->ethernet_hook) hooks -= 1;
1564 if (privp->debug_hook) hooks -= 1;
1566 if ((NG_NODE_NUMHOOKS(node) == 0)
1567 && (NG_NODE_IS_VALID(node)))
1568 ng_rmnode_self(node);
1573 * timeouts come here.
1576 pppoe_ticker(void *arg)
1580 sessp sp = NG_HOOK_PRIVATE(hook);
1583 struct mbuf *m0 = NULL;
1584 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1589 * resend the last packet, using an exponential backoff.
1590 * After a period of time, stop growing the backoff,
1591 * and either leave it, or revert to the start.
1595 /* timeouts on these produce resends */
1596 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1597 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1598 neg->timeout_handle = timeout(pppoe_ticker,
1599 hook, neg->timeout * hz);
1600 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1601 if (sp->state == PPPOE_SREQ) {
1602 /* revert to SINIT mode */
1605 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1611 /* a timeout on these says "give up" */
1612 ng_rmhook_self(hook);
1615 /* timeouts have no meaning in other states */
1616 printf("pppoe: unexpected timeout\n");
1623 sendpacket(sessp sp)
1626 struct mbuf *m0 = NULL;
1627 hook_p hook = sp->hook;
1629 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1633 case PPPOE_LISTENING:
1636 case PPPOE_CONNECTED:
1637 printf("pppoe: sendpacket: unexpected state\n");
1640 case PPPOE_NEWCONNECTED:
1641 /* send the PADS without a timeout - we're now connected */
1642 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1643 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1647 /* No packet to send, but set up the timeout */
1648 neg->timeout_handle = timeout(pppoe_ticker,
1649 hook, PPPOE_OFFER_TIMEOUT * hz);
1654 * send the offer but if they don't respond
1655 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1657 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1658 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1659 neg->timeout_handle = timeout(pppoe_ticker,
1660 hook, PPPOE_OFFER_TIMEOUT * hz);
1665 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1666 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1667 neg->timeout_handle = timeout(pppoe_ticker, hook,
1668 (hz * PPPOE_INITIAL_TIMEOUT));
1669 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1674 printf("pppoe: timeout: bad state\n");
1676 /* return (error); */
1680 * Parse an incoming packet to see if any tags should be copied to the
1681 * output packet. Don't do any tags that have been handled in the main
1684 static struct pppoe_tag*
1685 scan_tags(sessp sp, struct pppoe_hdr* ph)
1687 char *end = (char *)next_tag(ph);
1689 struct pppoe_tag *pt = &ph->tag[0];
1691 * Keep processing tags while a tag header will still fit.
1694 while((char*)(pt + 1) <= end) {
1696 * If the tag data would go past the end of the packet, abort.
1698 ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
1702 switch (pt->tag_type) {
1718 pt = (struct pppoe_tag*)ptn;
1724 pppoe_send_event(sessp sp, enum cmd cmdid)
1727 struct ng_mesg *msg;
1728 struct ngpppoe_sts *sts;
1731 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1732 sizeof(struct ngpppoe_sts), M_NOWAIT);
1735 sts = (struct ngpppoe_sts *)msg->data;
1736 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKLEN + 1);
1737 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, NULL);