6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <net/ethernet.h>
54 #include <netgraph/ng_message.h>
55 #include <netgraph/netgraph.h>
56 #include <netgraph/ng_parse.h>
57 #include <netgraph/ng_pppoe.h>
58 #include <netgraph/ng_ether.h>
60 #ifdef NG_SEPARATE_MALLOC
61 MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
63 #define M_NETGRAPH_PPPOE M_NETGRAPH
66 #define SIGNOFF "session closed"
67 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
70 * This section contains the netgraph method declarations for the
71 * pppoe node. These methods define the netgraph pppoe 'type'.
74 static ng_constructor_t ng_pppoe_constructor;
75 static ng_rcvmsg_t ng_pppoe_rcvmsg;
76 static ng_shutdown_t ng_pppoe_shutdown;
77 static ng_newhook_t ng_pppoe_newhook;
78 static ng_connect_t ng_pppoe_connect;
79 static ng_rcvdata_t ng_pppoe_rcvdata;
80 static ng_disconnect_t ng_pppoe_disconnect;
82 /* Parse type for struct ngpppoe_init_data */
83 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
84 = NG_PPPOE_INIT_DATA_TYPE_INFO;
85 static const struct ng_parse_type ngpppoe_init_data_state_type = {
86 &ng_parse_struct_type,
87 &ngpppoe_init_data_type_fields
90 /* Parse type for struct ngpppoe_sts */
91 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
92 = NG_PPPOE_STS_TYPE_INFO;
93 static const struct ng_parse_type ng_pppoe_sts_state_type = {
94 &ng_parse_struct_type,
95 &ng_pppoe_sts_type_fields
98 /* List of commands and how to convert arguments to/from ASCII */
99 static const struct ng_cmdlist ng_pppoe_cmds[] = {
104 &ngpppoe_init_data_state_type,
111 &ngpppoe_init_data_state_type,
118 &ngpppoe_init_data_state_type,
125 &ngpppoe_init_data_state_type,
132 &ng_pppoe_sts_state_type,
139 &ng_pppoe_sts_state_type,
146 &ng_pppoe_sts_state_type,
153 &ng_parse_string_type,
161 &ng_parse_string_type
167 &ng_parse_enaddr_type,
173 /* Netgraph node type descriptor */
174 static struct ng_type typestruct = {
175 .version = NG_ABI_VERSION,
176 .name = NG_PPPOE_NODE_TYPE,
177 .constructor = ng_pppoe_constructor,
178 .rcvmsg = ng_pppoe_rcvmsg,
179 .shutdown = ng_pppoe_shutdown,
180 .newhook = ng_pppoe_newhook,
181 .connect = ng_pppoe_connect,
182 .rcvdata = ng_pppoe_rcvdata,
183 .disconnect = ng_pppoe_disconnect,
184 .cmdlist = ng_pppoe_cmds,
186 NETGRAPH_INIT(pppoe, &typestruct);
189 * States for the session state machine.
190 * These have no meaning if there is no hook attached yet.
193 PPPOE_SNONE=0, /* [both] Initial state */
194 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
195 PPPOE_SINIT, /* [Client] Sent discovery initiation */
196 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
197 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
198 PPPOE_SREQ, /* [Client] Sent a Request */
199 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
200 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
201 PPPOE_DEAD /* [Both] */
204 #define NUMTAGS 20 /* number of tags we are set up to work with */
207 * Information we store for each hook on each node for negotiating the
208 * session. The mbuf and cluster are freed once negotiation has completed.
209 * The whole negotiation block is then discarded.
213 struct mbuf *m; /* holds cluster with last sent packet */
214 union packet *pkt; /* points within the above cluster */
215 struct callout handle; /* see timeout(9) */
216 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
218 const struct pppoe_tag *tags[NUMTAGS];
222 struct datatag service;
223 struct datatag ac_name;
225 typedef struct sess_neg *negp;
228 * Session information that is needed after connection.
234 ng_ID_t creator; /* who to notify */
235 struct pppoe_full_hdr pkt_hdr; /* used when connected */
236 negp neg; /* used when negotiating */
238 typedef struct sess_con *sessp;
240 #define NG_PPPOE_SESSION_NODE(sp) NG_HOOK_NODE(sp->hook)
243 * Information we store for each node
246 node_p node; /* back pointer to node */
247 hook_p ethernet_hook;
249 u_int packets_in; /* packets in from ethernet */
250 u_int packets_out; /* packets out towards ethernet */
252 #define COMPAT_3COM 0x00000001
253 #define COMPAT_DLINK 0x00000002
254 struct ether_header eh;
256 typedef struct PPPoE *priv_p;
259 char bytes[sizeof(void *)];
263 #define LEAVE(x) do { error = x; goto quit; } while(0)
264 static void pppoe_start(sessp sp);
265 static void ng_pppoe_sendpacket(sessp sp);
266 static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
267 static const struct pppoe_tag *scan_tags(sessp sp,
268 const struct pppoe_hdr* ph);
269 static int pppoe_send_event(sessp sp, enum cmd cmdid);
271 /*************************************************************************
272 * Some basic utilities from the Linux version with author's permission.*
273 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
274 ************************************************************************/
277 * Generate a new session id
278 * XXX find out the FreeBSD locking scheme.
281 get_new_sid(node_p node)
283 static int pppoe_sid = 10;
290 * Spec says 0xFFFF is reserved.
291 * Also don't use 0x0000
298 /* Check it isn't already in use. */
299 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
300 sessp sp = NG_HOOK_PRIVATE(hook);
302 /* Skip any nonsession hook. */
305 if (sp->Session_ID == val)
309 CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
316 * Return the location where the next tag can be put
318 static __inline const struct pppoe_tag*
319 next_tag(const struct pppoe_hdr* ph)
321 return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
322 + ntohs(ph->length));
326 * Look for a tag of a specific type.
327 * Don't trust any length the other end says,
328 * but assume we already sanity checked ph->length.
330 static const struct pppoe_tag*
331 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
333 const char *const end = (const char *)next_tag(ph);
334 const struct pppoe_tag *pt = &ph->tag[0];
338 * Keep processing tags while a tag header will still fit.
340 while((const char*)(pt + 1) <= end) {
342 * If the tag data would go past the end of the packet, abort.
344 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
346 CTR2(KTR_NET, "%20s: invalid length for tag %d",
350 if (pt->tag_type == idx) {
351 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
355 pt = (const struct pppoe_tag*)ptn;
358 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
362 /**************************************************************************
363 * Inlines to initialise or add tags to a session's tag list.
364 **************************************************************************/
366 * Initialise the session's tag list.
371 KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
372 sp->neg->numtags = 0;
376 insert_tag(sessp sp, const struct pppoe_tag *tp)
381 KASSERT(neg != NULL, ("%s: no neg", __func__));
382 if ((i = neg->numtags++) < NUMTAGS) {
385 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
392 * Make up a packet, using the tags filled out for the session.
394 * Assume that the actual pppoe header and ethernet header
395 * are filled out externally to this routine.
396 * Also assume that neg->wh points to the correct
397 * location at the front of the buffer space.
400 make_packet(sessp sp) {
401 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
402 const struct pppoe_tag **tag;
408 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
409 ("%s: called from wrong state", __func__));
410 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
412 dp = (char *)wh->ph.tag;
413 for (count = 0, tag = sp->neg->tags;
414 ((count < sp->neg->numtags) && (count < NUMTAGS));
416 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
417 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
418 log(LOG_NOTICE, "ng_pppoe: tags too long\n");
419 sp->neg->numtags = count;
420 break; /* XXX chop off what's too long */
422 bcopy(*tag, (char *)dp, tlen);
426 wh->ph.length = htons(length);
427 sp->neg->m->m_len = length + sizeof(*wh);
428 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
431 /**************************************************************************
432 * Routines to match a service. *
433 **************************************************************************/
436 * Find a hook that has a service string that matches that
437 * we are seeking. For now use a simple string.
438 * In the future we may need something like regexp().
440 * Null string is a wildcard (ANY service), according to RFC2516.
441 * And historical FreeBSD wildcard is also "*".
445 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
449 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
450 sessp sp = NG_HOOK_PRIVATE(hook);
453 /* Skip any nonsession hook. */
457 /* Skip any sessions which are not in LISTEN mode. */
458 if (sp->state != PPPOE_LISTENING)
463 /* Empty Service-Name matches any service. */
464 if (neg->service_len == 0)
467 /* Special case for a blank or "*" service name (wildcard). */
468 if (neg->service_len == 1 && neg->service.data[0] == '*')
471 /* If the lengths don't match, that aint it. */
472 if (neg->service_len != ntohs(tag->tag_len))
475 if (strncmp(tag->tag_data, neg->service.data,
476 ntohs(tag->tag_len)) == 0)
479 CTR3(KTR_NET, "%20s: matched %p for %s", __func__, hook, tag->tag_data);
485 * Broadcast the PADI packet in m0 to all listening hooks.
486 * This routine is called when a PADI with empty Service-Name
487 * tag is received. Client should receive PADOs with all
488 * available services.
491 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
496 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
497 sessp sp = NG_HOOK_PRIVATE(hook);
501 * Go through all listening hooks and
502 * broadcast the PADI packet up there
507 if (sp->state != PPPOE_LISTENING)
510 m = m_dup(m0, M_DONTWAIT);
513 NG_SEND_DATA_ONLY(error, hook, m);
522 * Find a hook, which name equals to given service.
525 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
529 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
530 sessp sp = NG_HOOK_PRIVATE(hook);
533 /* Skip any nonsession hook. */
537 /* Skip any sessions which are not in LISTEN mode. */
538 if (sp->state != PPPOE_LISTENING)
543 if (neg->service_len == svc_len &&
544 strncmp(svc_name, neg->service.data, svc_len == 0))
551 /**************************************************************************
552 * Routine to find a particular session that matches an incoming packet. *
553 **************************************************************************/
555 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
558 uint16_t session = ntohs(wh->ph.sid);
561 * Find matching peer/session combination.
563 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
564 sessp sp = NG_HOOK_PRIVATE(hook);
566 /* Skip any nonsession hook. */
569 if (sp->Session_ID == session &&
570 (sp->state == PPPOE_CONNECTED ||
571 sp->state == PPPOE_NEWCONNECTED) &&
572 bcmp(sp->pkt_hdr.eh.ether_dhost,
573 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
577 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, hook, session);
583 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
588 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
589 /* Cycle through all known hooks. */
590 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
591 /* Skip any nonsession hook. */
592 if (NG_HOOK_PRIVATE(hook) == NULL)
594 if (uniq.pointer == NG_HOOK_PRIVATE(hook))
597 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
602 /**************************************************************************
603 * Start of Netgraph entrypoints. *
604 **************************************************************************/
607 * Allocate the private data structure and link it with node.
610 ng_pppoe_constructor(node_p node)
614 /* Initialize private descriptor. */
615 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
619 /* Link structs together; this counts as our one reference to *node. */
620 NG_NODE_SET_PRIVATE(node, privp);
623 /* Initialize to standard mode. */
624 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
625 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
627 CTR3(KTR_NET, "%20s: created node [%x] (%p)",
628 __func__, node->nd_ID, node);
634 * Give our ok for a hook to be added...
635 * point the hook's private info to the hook structure.
637 * The following hook names are special:
638 * "ethernet": the hook that should be connected to a NIC.
639 * "debug": copies of data sent out here (when I write the code).
640 * All other hook names need only be unique. (the framework checks this).
643 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
645 const priv_p privp = NG_NODE_PRIVATE(node);
648 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
649 privp->ethernet_hook = hook;
650 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
651 privp->debug_hook = hook;
654 * Any other unique name is OK.
655 * The infrastructure has already checked that it's unique,
656 * so just allocate it and hook it in.
658 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
662 NG_HOOK_SET_PRIVATE(hook, sp);
665 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
666 __func__, node->nd_ID, node, name, hook);
672 * Hook has been added successfully. Request the MAC address of
673 * the underlying Ethernet node.
676 ng_pppoe_connect(hook_p hook)
678 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
682 if (hook != privp->ethernet_hook)
686 * If this is Ethernet hook, then request MAC address
687 * from our downstream.
689 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
694 * Our hook and peer hook have HK_INVALID flag set,
695 * so we can't use NG_SEND_MSG_HOOK() macro here.
697 NG_SEND_MSG_ID(error, privp->node, msg,
698 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
699 NG_NODE_ID(privp->node));
704 * Get a netgraph control message.
705 * Check it is one we understand. If needed, send a response.
706 * We sometimes save the address for an async action later.
707 * Always free the message.
710 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
712 priv_p privp = NG_NODE_PRIVATE(node);
713 struct ngpppoe_init_data *ourmsg = NULL;
714 struct ng_mesg *resp = NULL;
721 NGI_GET_MSG(item, msg);
722 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
723 __func__, node->nd_ID, node, msg->header.cmd,
724 msg->header.typecookie);
726 /* Deal with message according to cookie and command. */
727 switch (msg->header.typecookie) {
728 case NGM_PPPOE_COOKIE:
729 switch (msg->header.cmd) {
730 case NGM_PPPOE_CONNECT:
731 case NGM_PPPOE_LISTEN:
732 case NGM_PPPOE_OFFER:
733 case NGM_PPPOE_SERVICE:
734 ourmsg = (struct ngpppoe_init_data *)msg->data;
735 if (msg->header.arglen < sizeof(*ourmsg)) {
736 log(LOG_ERR, "ng_pppoe[%x]: init data too "
737 "small\n", node->nd_ID);
740 if (msg->header.arglen - sizeof(*ourmsg) >
741 PPPOE_SERVICE_NAME_SIZE) {
742 log(LOG_ERR, "ng_pppoe[%x]: service name "
743 "too big\n", node->nd_ID);
746 if (msg->header.arglen - sizeof(*ourmsg) <
748 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
749 "length, %d should be %zd\n", node->nd_ID,
751 msg->header.arglen - sizeof (*ourmsg));
755 /* Make sure strcmp will terminate safely. */
756 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
758 /* Cycle through all known hooks. */
759 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
760 if (NG_HOOK_NAME(hook) &&
761 strcmp(NG_HOOK_NAME(hook), ourmsg->hook) ==
768 sp = NG_HOOK_PRIVATE(hook);
773 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
775 * Ensure we aren't already listening for this
778 if (pppoe_find_svc(node, ourmsg->data,
779 ourmsg->data_len) != NULL)
784 * PPPOE_SERVICE advertisments are set up
785 * on sessions that are in PRIMED state.
787 if (msg->header.cmd == NGM_PPPOE_SERVICE)
790 if (sp->state != PPPOE_SNONE) {
791 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
792 "active\n", node->nd_ID);
797 * Set up prototype header.
799 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
805 neg->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
806 if (neg->m == NULL) {
807 free(neg, M_NETGRAPH_PPPOE);
810 neg->m->m_pkthdr.rcvif = NULL;
812 ng_callout_init(&neg->handle);
813 neg->m->m_len = sizeof(struct pppoe_full_hdr);
814 neg->pkt = mtod(neg->m, union packet*);
815 memcpy((void *)&neg->pkt->pkt_header.eh,
816 &privp->eh, sizeof(struct ether_header));
817 neg->pkt->pkt_header.ph.ver = 0x1;
818 neg->pkt->pkt_header.ph.type = 0x1;
819 neg->pkt->pkt_header.ph.sid = 0x0000;
822 sp->creator = NGI_RETADDR(item);
824 switch (msg->header.cmd) {
825 case NGM_PPPOE_GET_STATUS:
827 struct ngpppoestat *stats;
829 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
833 stats = (struct ngpppoestat *) resp->data;
834 stats->packets_in = privp->packets_in;
835 stats->packets_out = privp->packets_out;
838 case NGM_PPPOE_CONNECT:
840 * Check the hook exists and is Uninitialised.
841 * Send a PADI request, and start the timeout logic.
842 * Store the originator of this message so we can send
843 * a success of fail message to them later.
844 * Move the session to SINIT.
845 * Set up the session to the correct state and
848 neg->service.hdr.tag_type = PTT_SRV_NAME;
849 neg->service.hdr.tag_len =
850 htons((uint16_t)ourmsg->data_len);
851 if (ourmsg->data_len)
852 bcopy(ourmsg->data, neg->service.data,
854 neg->service_len = ourmsg->data_len;
857 case NGM_PPPOE_LISTEN:
859 * Check the hook exists and is Uninitialised.
860 * Install the service matching string.
861 * Store the originator of this message so we can send
862 * a success of fail message to them later.
863 * Move the hook to 'LISTENING'
865 neg->service.hdr.tag_type = PTT_SRV_NAME;
866 neg->service.hdr.tag_len =
867 htons((uint16_t)ourmsg->data_len);
869 if (ourmsg->data_len)
870 bcopy(ourmsg->data, neg->service.data,
872 neg->service_len = ourmsg->data_len;
873 neg->pkt->pkt_header.ph.code = PADT_CODE;
875 * Wait for PADI packet coming from Ethernet.
877 sp->state = PPPOE_LISTENING;
879 case NGM_PPPOE_OFFER:
881 * Check the hook exists and is Uninitialised.
882 * Store the originator of this message so we can send
883 * a success of fail message to them later.
884 * Store the AC-Name given and go to PRIMED.
886 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
887 neg->ac_name.hdr.tag_len =
888 htons((uint16_t)ourmsg->data_len);
889 if (ourmsg->data_len)
890 bcopy(ourmsg->data, neg->ac_name.data,
892 neg->ac_name_len = ourmsg->data_len;
893 neg->pkt->pkt_header.ph.code = PADO_CODE;
895 * Wait for PADI packet coming from hook.
897 sp->state = PPPOE_PRIMED;
899 case NGM_PPPOE_SERVICE:
901 * Check the session is primed.
902 * for now just allow ONE service to be advertised.
903 * If you do it twice you just overwrite.
905 if (sp->state != PPPOE_PRIMED) {
906 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
907 "primed\n", node->nd_ID);
911 neg->service.hdr.tag_type = PTT_SRV_NAME;
912 neg->service.hdr.tag_len =
913 htons((uint16_t)ourmsg->data_len);
915 if (ourmsg->data_len)
916 bcopy(ourmsg->data, neg->service.data,
918 neg->service_len = ourmsg->data_len;
920 case NGM_PPPOE_SETMODE:
925 if (msg->header.arglen == 0)
928 s = (char *)msg->data;
929 len = msg->header.arglen - 1;
931 /* Search for matching mode string. */
932 if (len == strlen(NG_PPPOE_STANDARD) &&
933 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
935 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
938 if (len == strlen(NG_PPPOE_3COM) &&
939 (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
940 privp->flags |= COMPAT_3COM;
941 privp->eh.ether_type =
942 ETHERTYPE_PPPOE_3COM_DISC;
945 if (len == strlen(NG_PPPOE_DLINK) &&
946 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
947 privp->flags |= COMPAT_DLINK;
953 case NGM_PPPOE_GETMODE:
958 if (privp->flags == 0)
959 len += strlen(NG_PPPOE_STANDARD) + 1;
960 if (privp->flags & COMPAT_3COM)
961 len += strlen(NG_PPPOE_3COM) + 1;
962 if (privp->flags & COMPAT_DLINK)
963 len += strlen(NG_PPPOE_DLINK) + 1;
965 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
969 s = (char *)resp->data;
970 if (privp->flags == 0) {
971 len = strlen(NG_PPPOE_STANDARD);
972 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
975 if (privp->flags & COMPAT_3COM) {
976 len = strlen(NG_PPPOE_3COM);
977 strlcpy(s, NG_PPPOE_3COM, len + 1);
980 if (privp->flags & COMPAT_DLINK) {
983 len = strlen(NG_PPPOE_DLINK);
984 strlcpy(s, NG_PPPOE_DLINK, len + 1);
988 case NGM_PPPOE_SETENADDR:
989 if (msg->header.arglen != ETHER_ADDR_LEN)
991 bcopy(msg->data, &privp->eh.ether_shost,
998 case NGM_ETHER_COOKIE:
999 if (!(msg->header.flags & NGF_RESP))
1001 switch (msg->header.cmd) {
1002 case NGM_ETHER_GET_ENADDR:
1003 if (msg->header.arglen != ETHER_ADDR_LEN)
1005 bcopy(msg->data, &privp->eh.ether_shost,
1016 /* Take care of synchronous response, if any. */
1018 CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1019 NG_RESPOND_MSG(error, node, item, resp);
1020 /* Free the message and return. */
1026 * Start a client into the first state. A separate function because
1027 * it can be needed if the negotiation times out.
1030 pppoe_start(sessp sp)
1032 priv_p privp = NG_NODE_PRIVATE(NG_PPPOE_SESSION_NODE(sp));
1034 struct pppoe_tag hdr;
1039 * Kick the state machine into starting up.
1041 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1042 sp->state = PPPOE_SINIT;
1044 * Reset the packet header to broadcast. Since we are
1045 * in a client mode use configured ethertype.
1047 memcpy((void *)&sp->neg->pkt->pkt_header.eh, &privp->eh,
1048 sizeof(struct ether_header));
1049 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
1050 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
1051 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
1052 uniqtag.data.pointer = sp;
1054 insert_tag(sp, &uniqtag.hdr);
1055 insert_tag(sp, &sp->neg->service.hdr);
1057 ng_pppoe_sendpacket(sp);
1061 send_acname(sessp sp, const struct pppoe_tag *tag)
1064 struct ng_mesg *msg;
1065 struct ngpppoe_sts *sts;
1067 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1069 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1070 sizeof(struct ngpppoe_sts), M_NOWAIT);
1074 sts = (struct ngpppoe_sts *)msg->data;
1075 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1076 strncpy(sts->hook, tag->tag_data, tlen);
1077 sts->hook[tlen] = '\0';
1078 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1084 send_sessionid(sessp sp)
1087 struct ng_mesg *msg;
1089 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1091 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1092 sizeof(uint16_t), M_NOWAIT);
1096 *(uint16_t *)msg->data = sp->Session_ID;
1097 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1103 * Receive data, and do something with it.
1104 * The caller will never free m, so if we use up this data
1105 * or abort we must free it.
1108 ng_pppoe_rcvdata(hook_p hook, item_p item)
1110 node_p node = NG_HOOK_NODE(hook);
1111 const priv_p privp = NG_NODE_PRIVATE(node);
1112 sessp sp = NG_HOOK_PRIVATE(hook);
1113 const struct pppoe_tag *utag = NULL, *tag = NULL;
1114 const struct pppoe_full_hdr *wh;
1115 const struct pppoe_hdr *ph;
1124 struct pppoe_tag hdr;
1128 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1129 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1132 if (hook == privp->debug_hook) {
1134 * Data from the debug hook gets sent without modification
1135 * straight to the ethernet.
1137 NG_FWD_ITEM_HOOK( error, item, privp->ethernet_hook);
1138 privp->packets_out++;
1139 } else if (hook == privp->ethernet_hook) {
1142 * Dig out various fields from the packet.
1143 * Use them to decide where to send it.
1146 privp->packets_in++;
1147 if( m->m_len < sizeof(*wh)) {
1148 m = m_pullup(m, sizeof(*wh)); /* Checks length */
1150 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1151 "m_pullup(wh)\n", node->nd_ID);
1155 wh = mtod(m, struct pppoe_full_hdr *);
1156 length = ntohs(wh->ph.length);
1157 switch(wh->eh.ether_type) {
1158 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1159 case ETHERTYPE_PPPOE_DISC:
1161 * We need to try to make sure that the tag area
1162 * is contiguous, or we could wander off the end
1163 * of a buffer and make a mess.
1164 * (Linux wouldn't have this problem).
1166 if (m->m_pkthdr.len <= MHLEN) {
1167 if( m->m_len < m->m_pkthdr.len) {
1168 m = m_pullup(m, m->m_pkthdr.len);
1170 log(LOG_NOTICE, "ng_pppoe[%x]: "
1172 "m_pullup(pkthdr)\n",
1178 if (m->m_len != m->m_pkthdr.len) {
1180 * It's not all in one piece.
1181 * We need to do extra work.
1182 * Put it into a cluster.
1185 n = m_dup(m, M_DONTWAIT);
1189 /* just check we got a cluster */
1190 if (m->m_len != m->m_pkthdr.len) {
1196 log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1197 "fragmented\n", node->nd_ID);
1201 wh = mtod(m, struct pppoe_full_hdr *);
1202 length = ntohs(wh->ph.length);
1204 session = ntohs(wh->ph.sid);
1211 * Look for a hook with the required service
1212 * and send the ENTIRE packet up there.
1213 * It should come back to a new hook in
1214 * PRIMED state. Look there for further
1217 tag = get_tag(ph, PTT_SRV_NAME);
1220 "%20s: PADI w/o Service-Name",
1226 * First, try to match Service-Name
1227 * against our listening hooks. If
1228 * no success and we are in D-Link
1229 * compat mode and Service-Name is
1230 * empty, then we broadcast the PADI
1231 * to all listening hooks.
1233 sendhook = pppoe_match_svc(node, tag);
1234 if (sendhook != NULL)
1235 NG_FWD_NEW_DATA(error, item,
1237 else if (privp->flags & COMPAT_DLINK &&
1238 ntohs(tag->tag_len) == 0)
1239 error = pppoe_broadcast_padi(node, m);
1241 error = ENETUNREACH;
1246 * Use the host_uniq tag to find the
1247 * hook this is in response to.
1248 * Received #2, now send #3
1249 * For now simply accept the first we receive.
1251 utag = get_tag(ph, PTT_HOST_UNIQ);
1253 || (ntohs(utag->tag_len) != sizeof(sp))) {
1254 log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1255 "unique field\n", node->nd_ID);
1259 sendhook = pppoe_finduniq(node, utag);
1260 if (sendhook == NULL) {
1261 log(LOG_NOTICE, "ng_pppoe[%x]: no "
1262 "matching session\n", node->nd_ID);
1267 * Check the session is in the right state.
1268 * It needs to be in PPPOE_SINIT.
1270 sp = NG_HOOK_PRIVATE(sendhook);
1271 if (sp->state != PPPOE_SINIT) {
1272 log(LOG_NOTICE, "ng_pppoe[%x]: session "
1273 "in wrong state\n", node->nd_ID);
1277 ng_uncallout(&neg->handle, node);
1280 * This is the first time we hear
1281 * from the server, so note it's
1282 * unicast address, replacing the
1283 * broadcast address .
1285 bcopy(wh->eh.ether_shost,
1286 neg->pkt->pkt_header.eh.ether_dhost,
1289 neg->pkt->pkt_header.ph.code = PADR_CODE;
1291 insert_tag(sp, utag); /* Host Unique */
1292 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1293 insert_tag(sp, tag); /* return cookie */
1294 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1295 insert_tag(sp, tag); /* return it */
1296 send_acname(sp, tag);
1298 insert_tag(sp, &neg->service.hdr); /* Service */
1301 sp->state = PPPOE_SREQ;
1302 ng_pppoe_sendpacket(sp);
1308 * Use the ac_cookie tag to find the
1309 * hook this is in response to.
1311 utag = get_tag(ph, PTT_AC_COOKIE);
1313 || (ntohs(utag->tag_len) != sizeof(sp))) {
1317 sendhook = pppoe_finduniq(node, utag);
1318 if (sendhook == NULL) {
1323 * Check the session is in the right state.
1324 * It needs to be in PPPOE_SOFFER
1325 * or PPPOE_NEWCONNECTED. If the latter,
1326 * then this is a retry by the client.
1327 * so be nice, and resend.
1329 sp = NG_HOOK_PRIVATE(sendhook);
1330 if (sp->state == PPPOE_NEWCONNECTED) {
1332 * Whoa! drop back to resend that
1334 * We should still have a copy of it.
1336 sp->state = PPPOE_SOFFER;
1338 if (sp->state != PPPOE_SOFFER) {
1339 LEAVE (ENETUNREACH);
1343 ng_uncallout(&neg->handle, node);
1344 neg->pkt->pkt_header.ph.code = PADS_CODE;
1345 if (sp->Session_ID == 0)
1346 neg->pkt->pkt_header.ph.sid =
1347 htons(sp->Session_ID
1348 = get_new_sid(node));
1352 * start working out the tags to respond with.
1355 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1356 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1357 insert_tag(sp, tag);/* return service */
1358 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1359 insert_tag(sp, tag); /* return it */
1360 insert_tag(sp, utag); /* ac_cookie */
1363 sp->state = PPPOE_NEWCONNECTED;
1364 ng_pppoe_sendpacket(sp);
1366 * Having sent the last Negotiation header,
1367 * Set up the stored packet header to
1368 * be correct for the actual session.
1369 * But keep the negotialtion stuff
1370 * around in case we need to resend this last
1371 * packet. We'll discard it when we move
1372 * from NEWCONNECTED to CONNECTED
1374 sp->pkt_hdr = neg->pkt->pkt_header;
1375 /* Configure ethertype depending on what
1376 * ethertype was used at discovery phase */
1377 if (sp->pkt_hdr.eh.ether_type ==
1378 ETHERTYPE_PPPOE_3COM_DISC)
1379 sp->pkt_hdr.eh.ether_type
1380 = ETHERTYPE_PPPOE_3COM_SESS;
1382 sp->pkt_hdr.eh.ether_type
1383 = ETHERTYPE_PPPOE_SESS;
1384 sp->pkt_hdr.ph.code = 0;
1385 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1390 * Use the host_uniq tag to find the
1391 * hook this is in response to.
1392 * take the session ID and store it away.
1393 * Also make sure the pre-made header is
1394 * correct and set us into Session mode.
1396 utag = get_tag(ph, PTT_HOST_UNIQ);
1398 || (ntohs(utag->tag_len) != sizeof(sp))) {
1399 LEAVE (ENETUNREACH);
1402 sendhook = pppoe_finduniq(node, utag);
1403 if (sendhook == NULL) {
1408 * Check the session is in the right state.
1409 * It needs to be in PPPOE_SREQ.
1411 sp = NG_HOOK_PRIVATE(sendhook);
1412 if (sp->state != PPPOE_SREQ) {
1416 ng_uncallout(&neg->handle, node);
1417 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1418 sp->Session_ID = ntohs(wh->ph.sid);
1421 sp->state = PPPOE_CONNECTED;
1423 * Now we have gone to Connected mode,
1424 * Free all resources needed for
1426 * Keep a copy of the header we will be using.
1428 sp->pkt_hdr = neg->pkt->pkt_header;
1429 if (privp->flags & COMPAT_3COM)
1430 sp->pkt_hdr.eh.ether_type
1431 = ETHERTYPE_PPPOE_3COM_SESS;
1433 sp->pkt_hdr.eh.ether_type
1434 = ETHERTYPE_PPPOE_SESS;
1435 sp->pkt_hdr.ph.code = 0;
1437 free(sp->neg, M_NETGRAPH_PPPOE);
1439 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1443 * Send a 'close' message to the controlling
1444 * process (the one that set us up);
1445 * And then tear everything down.
1447 * Find matching peer/session combination.
1449 sendhook = pppoe_findsession(node, wh);
1450 if (sendhook == NULL) {
1453 /* send message to creator */
1456 ng_rmhook_self(sendhook);
1460 LEAVE(EPFNOSUPPORT);
1463 case ETHERTYPE_PPPOE_3COM_SESS:
1464 case ETHERTYPE_PPPOE_SESS:
1466 * Find matching peer/session combination.
1468 sendhook = pppoe_findsession(node, wh);
1469 if (sendhook == NULL) {
1470 LEAVE (ENETUNREACH);
1473 sp = NG_HOOK_PRIVATE(sendhook);
1474 m_adj(m, sizeof(*wh));
1475 if (m->m_pkthdr.len < length) {
1476 /* Packet too short, dump it */
1480 /* Also need to trim excess at the end */
1481 if (m->m_pkthdr.len > length) {
1482 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1484 if ( sp->state != PPPOE_CONNECTED) {
1485 if (sp->state == PPPOE_NEWCONNECTED) {
1486 sp->state = PPPOE_CONNECTED;
1488 * Now we have gone to Connected mode,
1489 * Free all resources needed for
1490 * negotiation. Be paranoid about
1491 * whether there may be a timeout.
1493 m_freem(sp->neg->m);
1494 ng_uncallout(&sp->neg->handle, node);
1495 free(sp->neg, M_NETGRAPH_PPPOE);
1498 LEAVE (ENETUNREACH);
1502 NG_FWD_NEW_DATA( error, item, sendhook, m);
1505 LEAVE(EPFNOSUPPORT);
1509 * Not ethernet or debug hook..
1511 * The packet has come in on a normal hook.
1512 * We need to find out what kind of hook,
1513 * So we can decide how to handle it.
1514 * Check the hook's state.
1516 sp = NG_HOOK_PRIVATE(hook);
1517 switch (sp->state) {
1518 case PPPOE_NEWCONNECTED:
1519 case PPPOE_CONNECTED: {
1520 static const u_char addrctrl[] = { 0xff, 0x03 };
1521 struct pppoe_full_hdr *wh;
1524 * Remove PPP address and control fields, if any.
1525 * For example, ng_ppp(4) always sends LCP packets
1526 * with address and control fields as required by
1527 * generic PPP. PPPoE is an exception to the rule.
1529 if (m->m_pkthdr.len >= 2) {
1530 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1532 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1536 * Bang in a pre-made header, and set the length up
1537 * to be correct. Then send it to the ethernet driver.
1539 M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1543 wh = mtod(m, struct pppoe_full_hdr *);
1544 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1545 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1546 NG_FWD_NEW_DATA( error, item, privp->ethernet_hook, m);
1547 privp->packets_out++;
1552 * A PADI packet is being returned by the application
1553 * that has set up this hook. This indicates that it
1554 * wants us to offer service.
1557 if (m->m_len < sizeof(*wh)) {
1558 m = m_pullup(m, sizeof(*wh));
1562 wh = mtod(m, struct pppoe_full_hdr *);
1564 session = ntohs(wh->ph.sid);
1565 length = ntohs(wh->ph.length);
1567 /* Use peers mode in session. */
1568 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1569 if (code != PADI_CODE)
1571 ng_uncallout(&neg->handle, node);
1574 * This is the first time we hear
1575 * from the client, so note it's
1576 * unicast address, replacing the
1577 * broadcast address.
1579 bcopy(wh->eh.ether_shost,
1580 neg->pkt->pkt_header.eh.ether_dhost,
1582 sp->state = PPPOE_SOFFER;
1584 neg->pkt->pkt_header.ph.code = PADO_CODE;
1587 * Start working out the tags to respond with.
1589 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1590 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1591 uniqtag.data.pointer = sp;
1593 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1594 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1595 insert_tag(sp, tag); /* return service */
1597 * If we have a NULL service request
1598 * and have an extra service defined in this hook,
1599 * then also add a tag for the extra service.
1600 * XXX this is a hack. eventually we should be able
1601 * to support advertising many services, not just one
1603 if (((tag == NULL) || (tag->tag_len == 0)) &&
1604 (neg->service.hdr.tag_len != 0)) {
1605 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1607 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1608 insert_tag(sp, tag); /* returned hostunique */
1609 insert_tag(sp, &uniqtag.hdr);
1612 ng_pppoe_sendpacket(sp);
1616 * Packets coming from the hook make no sense
1617 * to sessions in these states. Throw them away.
1623 case PPPOE_LISTENING:
1637 * Do local shutdown processing..
1638 * If we are a persistant device, we might refuse to go away, and
1639 * we'd only remove our links and reset ourself.
1642 ng_pppoe_shutdown(node_p node)
1644 const priv_p privdata = NG_NODE_PRIVATE(node);
1646 NG_NODE_SET_PRIVATE(node, NULL);
1647 NG_NODE_UNREF(privdata->node);
1648 free(privdata, M_NETGRAPH_PPPOE);
1653 * Hook disconnection
1655 * Clean up all dangling links and information about the session/hook.
1656 * For this type, removal of the last link destroys the node.
1659 ng_pppoe_disconnect(hook_p hook)
1661 node_p node = NG_HOOK_NODE(hook);
1662 priv_p privp = NG_NODE_PRIVATE(node);
1666 hooks = NG_NODE_NUMHOOKS(node); /* This one already not counted. */
1667 if (hook == privp->debug_hook) {
1668 privp->debug_hook = NULL;
1669 } else if (hook == privp->ethernet_hook) {
1670 privp->ethernet_hook = NULL;
1671 if (NG_NODE_IS_VALID(node))
1672 ng_rmnode_self(node);
1674 sp = NG_HOOK_PRIVATE(hook);
1675 if (sp->state != PPPOE_SNONE ) {
1676 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1679 * According to the spec, if we are connected,
1680 * we should send a DISC packet if we are shutting down
1683 if ((privp->ethernet_hook)
1684 && ((sp->state == PPPOE_CONNECTED)
1685 || (sp->state == PPPOE_NEWCONNECTED))) {
1688 /* Generate a packet of that type. */
1689 MGETHDR(m, M_DONTWAIT, MT_DATA);
1691 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1692 "mbufs\n", node->nd_ID);
1694 struct pppoe_full_hdr *wh;
1695 struct pppoe_tag *tag;
1696 int msglen = strlen(SIGNOFF);
1699 m->m_pkthdr.rcvif = NULL;
1700 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1701 wh = mtod(m, struct pppoe_full_hdr *);
1702 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1704 /* Revert the stored header to DISC/PADT mode. */
1705 wh->ph.code = PADT_CODE;
1707 * Configure ethertype depending on what
1708 * was used during sessions stage.
1710 if (wh->eh.ether_type ==
1711 ETHERTYPE_PPPOE_3COM_SESS)
1712 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1714 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1716 * Add a General error message and adjust
1720 tag->tag_type = PTT_GEN_ERR;
1721 tag->tag_len = htons((u_int16_t)msglen);
1722 strncpy(tag->tag_data, SIGNOFF, msglen);
1723 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1725 wh->ph.length = htons(sizeof(*tag) + msglen);
1726 NG_SEND_DATA_ONLY(error,
1727 privp->ethernet_hook, m);
1731 * As long as we have somewhere to store the timeout handle,
1732 * we may have a timeout pending.. get rid of it.
1735 ng_uncallout(&sp->neg->handle, node);
1737 m_freem(sp->neg->m);
1738 free(sp->neg, M_NETGRAPH_PPPOE);
1740 free(sp, M_NETGRAPH_PPPOE);
1741 NG_HOOK_SET_PRIVATE(hook, NULL);
1744 * Work out how many session hooks there are.
1745 * Node goes away on last session hook removal.
1747 if (privp->ethernet_hook)
1749 if (privp->debug_hook)
1752 if ((NG_NODE_NUMHOOKS(node) == 0) &&
1753 (NG_NODE_IS_VALID(node)))
1754 ng_rmnode_self(node);
1759 * Timeouts come here.
1762 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
1764 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1765 sessp sp = NG_HOOK_PRIVATE(hook);
1767 struct mbuf *m0 = NULL;
1770 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1771 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
1774 * Resend the last packet, using an exponential backoff.
1775 * After a period of time, stop growing the backoff,
1776 * And either leave it, or revert to the start.
1780 /* Timeouts on these produce resends. */
1781 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1782 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1783 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1784 pppoe_ticker, NULL, 0);
1785 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1786 if (sp->state == PPPOE_SREQ) {
1787 /* Revert to SINIT mode. */
1790 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1796 /* A timeout on these says "give up" */
1797 ng_rmhook_self(hook);
1800 /* Timeouts have no meaning in other states. */
1801 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1808 ng_pppoe_sendpacket(sessp sp)
1810 struct mbuf *m0 = NULL;
1811 hook_p hook = sp->hook;
1812 node_p node = NG_HOOK_NODE(hook);
1813 priv_p privp = NG_NODE_PRIVATE(node);
1817 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1819 case PPPOE_LISTENING:
1822 case PPPOE_CONNECTED:
1823 log(LOG_NOTICE, "%s: unexpected state %d\n",
1824 __func__, sp->state);
1827 case PPPOE_NEWCONNECTED:
1828 /* Send the PADS without a timeout - we're now connected. */
1829 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1830 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1834 /* No packet to send, but set up the timeout. */
1835 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1836 pppoe_ticker, NULL, 0);
1841 * Send the offer but if they don't respond
1842 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1844 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1845 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1846 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1847 pppoe_ticker, NULL, 0);
1852 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1853 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1854 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1855 pppoe_ticker, NULL, 0);
1856 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1861 log(LOG_NOTICE, "%s: bad state %d\n", __func__, sp->state);
1866 * Parse an incoming packet to see if any tags should be copied to the
1867 * output packet. Don't do any tags that have been handled in the main
1870 static const struct pppoe_tag*
1871 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1873 const char *const end = (const char *)next_tag(ph);
1875 const struct pppoe_tag *pt = &ph->tag[0];
1878 * Keep processing tags while a tag header will still fit.
1880 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1882 while((const char*)(pt + 1) <= end) {
1884 * If the tag data would go past the end of the packet, abort.
1886 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1890 switch (pt->tag_type) {
1906 pt = (const struct pppoe_tag*)ptn;
1912 pppoe_send_event(sessp sp, enum cmd cmdid)
1915 struct ng_mesg *msg;
1916 struct ngpppoe_sts *sts;
1918 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1920 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1921 sizeof(struct ngpppoe_sts), M_NOWAIT);
1924 sts = (struct ngpppoe_sts *)msg->data;
1925 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1926 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);