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_rcvdata_t ng_pppoe_rcvdata_ether;
81 static ng_rcvdata_t ng_pppoe_rcvdata_debug;
82 static ng_disconnect_t ng_pppoe_disconnect;
84 /* Parse type for struct ngpppoe_init_data */
85 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
86 = NG_PPPOE_INIT_DATA_TYPE_INFO;
87 static const struct ng_parse_type ngpppoe_init_data_state_type = {
88 &ng_parse_struct_type,
89 &ngpppoe_init_data_type_fields
92 /* Parse type for struct ngpppoe_sts */
93 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
94 = NG_PPPOE_STS_TYPE_INFO;
95 static const struct ng_parse_type ng_pppoe_sts_state_type = {
96 &ng_parse_struct_type,
97 &ng_pppoe_sts_type_fields
100 /* List of commands and how to convert arguments to/from ASCII */
101 static const struct ng_cmdlist ng_pppoe_cmds[] = {
106 &ngpppoe_init_data_state_type,
113 &ngpppoe_init_data_state_type,
120 &ngpppoe_init_data_state_type,
127 &ngpppoe_init_data_state_type,
134 &ng_pppoe_sts_state_type,
141 &ng_pppoe_sts_state_type,
148 &ng_pppoe_sts_state_type,
155 &ng_parse_string_type,
163 &ng_parse_string_type
169 &ng_parse_enaddr_type,
175 /* Netgraph node type descriptor */
176 static struct ng_type typestruct = {
177 .version = NG_ABI_VERSION,
178 .name = NG_PPPOE_NODE_TYPE,
179 .constructor = ng_pppoe_constructor,
180 .rcvmsg = ng_pppoe_rcvmsg,
181 .shutdown = ng_pppoe_shutdown,
182 .newhook = ng_pppoe_newhook,
183 .connect = ng_pppoe_connect,
184 .rcvdata = ng_pppoe_rcvdata,
185 .disconnect = ng_pppoe_disconnect,
186 .cmdlist = ng_pppoe_cmds,
188 NETGRAPH_INIT(pppoe, &typestruct);
191 * States for the session state machine.
192 * These have no meaning if there is no hook attached yet.
195 PPPOE_SNONE=0, /* [both] Initial state */
196 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
197 PPPOE_SINIT, /* [Client] Sent discovery initiation */
198 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
199 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
200 PPPOE_SREQ, /* [Client] Sent a Request */
201 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
202 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
203 PPPOE_DEAD /* [Both] */
206 #define NUMTAGS 20 /* number of tags we are set up to work with */
209 * Information we store for each hook on each node for negotiating the
210 * session. The mbuf and cluster are freed once negotiation has completed.
211 * The whole negotiation block is then discarded.
215 struct mbuf *m; /* holds cluster with last sent packet */
216 union packet *pkt; /* points within the above cluster */
217 struct callout handle; /* see timeout(9) */
218 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
220 const struct pppoe_tag *tags[NUMTAGS];
224 struct datatag service;
225 struct datatag ac_name;
227 typedef struct sess_neg *negp;
230 * Session information that is needed after connection.
236 ng_ID_t creator; /* who to notify */
237 struct pppoe_full_hdr pkt_hdr; /* used when connected */
238 negp neg; /* used when negotiating */
240 typedef struct sess_con *sessp;
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 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
266 static const struct pppoe_tag *scan_tags(sessp sp,
267 const struct pppoe_hdr* ph);
268 static int pppoe_send_event(sessp sp, enum cmd cmdid);
270 /*************************************************************************
271 * Some basic utilities from the Linux version with author's permission.*
272 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
273 ************************************************************************/
276 * Generate a new session id
277 * XXX find out the FreeBSD locking scheme.
280 get_new_sid(node_p node)
282 static int pppoe_sid = 10;
289 * Spec says 0xFFFF is reserved.
290 * Also don't use 0x0000
297 /* Check it isn't already in use. */
298 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
299 sessp sp = NG_HOOK_PRIVATE(hook);
301 /* Skip any nonsession hook. */
304 if (sp->Session_ID == val)
308 CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
315 * Return the location where the next tag can be put
317 static __inline const struct pppoe_tag*
318 next_tag(const struct pppoe_hdr* ph)
320 return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
321 + ntohs(ph->length));
325 * Look for a tag of a specific type.
326 * Don't trust any length the other end says,
327 * but assume we already sanity checked ph->length.
329 static const struct pppoe_tag*
330 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
332 const char *const end = (const char *)next_tag(ph);
333 const struct pppoe_tag *pt = &ph->tag[0];
337 * Keep processing tags while a tag header will still fit.
339 while((const char*)(pt + 1) <= end) {
341 * If the tag data would go past the end of the packet, abort.
343 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
345 CTR2(KTR_NET, "%20s: invalid length for tag %d",
349 if (pt->tag_type == idx) {
350 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
354 pt = (const struct pppoe_tag*)ptn;
357 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
361 /**************************************************************************
362 * Inlines to initialise or add tags to a session's tag list.
363 **************************************************************************/
365 * Initialise the session's tag list.
370 KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
371 sp->neg->numtags = 0;
375 insert_tag(sessp sp, const struct pppoe_tag *tp)
380 KASSERT(neg != NULL, ("%s: no neg", __func__));
381 if ((i = neg->numtags++) < NUMTAGS) {
384 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
391 * Make up a packet, using the tags filled out for the session.
393 * Assume that the actual pppoe header and ethernet header
394 * are filled out externally to this routine.
395 * Also assume that neg->wh points to the correct
396 * location at the front of the buffer space.
399 make_packet(sessp sp) {
400 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
401 const struct pppoe_tag **tag;
407 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
408 ("%s: called from wrong state", __func__));
409 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
411 dp = (char *)wh->ph.tag;
412 for (count = 0, tag = sp->neg->tags;
413 ((count < sp->neg->numtags) && (count < NUMTAGS));
415 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
416 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
417 log(LOG_NOTICE, "ng_pppoe: tags too long\n");
418 sp->neg->numtags = count;
419 break; /* XXX chop off what's too long */
421 bcopy(*tag, (char *)dp, tlen);
425 wh->ph.length = htons(length);
426 sp->neg->m->m_len = length + sizeof(*wh);
427 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
430 /**************************************************************************
431 * Routines to match a service. *
432 **************************************************************************/
435 * Find a hook that has a service string that matches that
436 * we are seeking. For now use a simple string.
437 * In the future we may need something like regexp().
439 * Null string is a wildcard (ANY service), according to RFC2516.
440 * And historical FreeBSD wildcard is also "*".
444 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
448 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
449 sessp sp = NG_HOOK_PRIVATE(hook);
452 /* Skip any nonsession hook. */
456 /* Skip any sessions which are not in LISTEN mode. */
457 if (sp->state != PPPOE_LISTENING)
462 /* Empty Service-Name matches any service. */
463 if (neg->service_len == 0)
466 /* Special case for a blank or "*" service name (wildcard). */
467 if (neg->service_len == 1 && neg->service.data[0] == '*')
470 /* If the lengths don't match, that aint it. */
471 if (neg->service_len != ntohs(tag->tag_len))
474 if (strncmp(tag->tag_data, neg->service.data,
475 ntohs(tag->tag_len)) == 0)
478 CTR3(KTR_NET, "%20s: matched %p for %s", __func__, hook, tag->tag_data);
484 * Broadcast the PADI packet in m0 to all listening hooks.
485 * This routine is called when a PADI with empty Service-Name
486 * tag is received. Client should receive PADOs with all
487 * available services.
490 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
495 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
496 sessp sp = NG_HOOK_PRIVATE(hook);
500 * Go through all listening hooks and
501 * broadcast the PADI packet up there
506 if (sp->state != PPPOE_LISTENING)
509 m = m_dup(m0, M_DONTWAIT);
512 NG_SEND_DATA_ONLY(error, hook, m);
521 * Find a hook, which name equals to given service.
524 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
528 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
529 sessp sp = NG_HOOK_PRIVATE(hook);
532 /* Skip any nonsession hook. */
536 /* Skip any sessions which are not in LISTEN mode. */
537 if (sp->state != PPPOE_LISTENING)
542 if (neg->service_len == svc_len &&
543 strncmp(svc_name, neg->service.data, svc_len) == 0)
550 /**************************************************************************
551 * Routine to find a particular session that matches an incoming packet. *
552 **************************************************************************/
554 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
557 uint16_t session = ntohs(wh->ph.sid);
560 * Find matching peer/session combination.
562 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
563 sessp sp = NG_HOOK_PRIVATE(hook);
565 /* Skip any nonsession hook. */
568 if (sp->Session_ID == session &&
569 (sp->state == PPPOE_CONNECTED ||
570 sp->state == PPPOE_NEWCONNECTED) &&
571 bcmp(sp->pkt_hdr.eh.ether_dhost,
572 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
576 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, hook, session);
582 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
587 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
588 /* Cycle through all known hooks. */
589 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
590 /* Skip any nonsession hook. */
591 if (NG_HOOK_PRIVATE(hook) == NULL)
593 if (uniq.pointer == NG_HOOK_PRIVATE(hook))
596 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
601 /**************************************************************************
602 * Start of Netgraph entrypoints. *
603 **************************************************************************/
606 * Allocate the private data structure and link it with node.
609 ng_pppoe_constructor(node_p node)
613 /* Initialize private descriptor. */
614 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
618 /* Link structs together; this counts as our one reference to *node. */
619 NG_NODE_SET_PRIVATE(node, privp);
622 /* Initialize to standard mode. */
623 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
624 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
626 CTR3(KTR_NET, "%20s: created node [%x] (%p)",
627 __func__, node->nd_ID, node);
633 * Give our ok for a hook to be added...
634 * point the hook's private info to the hook structure.
636 * The following hook names are special:
637 * "ethernet": the hook that should be connected to a NIC.
638 * "debug": copies of data sent out here (when I write the code).
639 * All other hook names need only be unique. (the framework checks this).
642 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
644 const priv_p privp = NG_NODE_PRIVATE(node);
647 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
648 privp->ethernet_hook = hook;
649 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
650 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
651 privp->debug_hook = hook;
652 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
655 * Any other unique name is OK.
656 * The infrastructure has already checked that it's unique,
657 * so just allocate it and hook it in.
659 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
663 NG_HOOK_SET_PRIVATE(hook, sp);
666 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
667 __func__, node->nd_ID, node, name, hook);
673 * Hook has been added successfully. Request the MAC address of
674 * the underlying Ethernet node.
677 ng_pppoe_connect(hook_p hook)
679 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
683 if (hook != privp->ethernet_hook)
687 * If this is Ethernet hook, then request MAC address
688 * from our downstream.
690 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
695 * Our hook and peer hook have HK_INVALID flag set,
696 * so we can't use NG_SEND_MSG_HOOK() macro here.
698 NG_SEND_MSG_ID(error, privp->node, msg,
699 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
700 NG_NODE_ID(privp->node));
705 * Get a netgraph control message.
706 * Check it is one we understand. If needed, send a response.
707 * We sometimes save the address for an async action later.
708 * Always free the message.
711 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
713 priv_p privp = NG_NODE_PRIVATE(node);
714 struct ngpppoe_init_data *ourmsg = NULL;
715 struct ng_mesg *resp = NULL;
722 NGI_GET_MSG(item, msg);
723 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
724 __func__, node->nd_ID, node, msg->header.cmd,
725 msg->header.typecookie);
727 /* Deal with message according to cookie and command. */
728 switch (msg->header.typecookie) {
729 case NGM_PPPOE_COOKIE:
730 switch (msg->header.cmd) {
731 case NGM_PPPOE_CONNECT:
732 case NGM_PPPOE_LISTEN:
733 case NGM_PPPOE_OFFER:
734 case NGM_PPPOE_SERVICE:
735 ourmsg = (struct ngpppoe_init_data *)msg->data;
736 if (msg->header.arglen < sizeof(*ourmsg)) {
737 log(LOG_ERR, "ng_pppoe[%x]: init data too "
738 "small\n", node->nd_ID);
741 if (msg->header.arglen - sizeof(*ourmsg) >
742 PPPOE_SERVICE_NAME_SIZE) {
743 log(LOG_ERR, "ng_pppoe[%x]: service name "
744 "too big\n", node->nd_ID);
747 if (msg->header.arglen - sizeof(*ourmsg) <
749 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
750 "length, %d should be %zd\n", node->nd_ID,
752 msg->header.arglen - sizeof (*ourmsg));
756 /* Make sure strcmp will terminate safely. */
757 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
759 /* Cycle through all known hooks. */
760 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
761 if (NG_HOOK_NAME(hook) &&
762 strcmp(NG_HOOK_NAME(hook), ourmsg->hook) ==
769 sp = NG_HOOK_PRIVATE(hook);
774 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
776 * Ensure we aren't already listening for this
779 if (pppoe_find_svc(node, ourmsg->data,
780 ourmsg->data_len) != NULL)
785 * PPPOE_SERVICE advertisments are set up
786 * on sessions that are in PRIMED state.
788 if (msg->header.cmd == NGM_PPPOE_SERVICE)
791 if (sp->state != PPPOE_SNONE) {
792 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
793 "active\n", node->nd_ID);
798 * Set up prototype header.
800 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
806 neg->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
807 if (neg->m == NULL) {
808 free(neg, M_NETGRAPH_PPPOE);
811 neg->m->m_pkthdr.rcvif = NULL;
813 ng_callout_init(&neg->handle);
814 neg->m->m_len = sizeof(struct pppoe_full_hdr);
815 neg->pkt = mtod(neg->m, union packet*);
816 memcpy((void *)&neg->pkt->pkt_header.eh,
817 &privp->eh, sizeof(struct ether_header));
818 neg->pkt->pkt_header.ph.ver = 0x1;
819 neg->pkt->pkt_header.ph.type = 0x1;
820 neg->pkt->pkt_header.ph.sid = 0x0000;
823 sp->creator = NGI_RETADDR(item);
825 switch (msg->header.cmd) {
826 case NGM_PPPOE_GET_STATUS:
828 struct ngpppoestat *stats;
830 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
834 stats = (struct ngpppoestat *) resp->data;
835 stats->packets_in = privp->packets_in;
836 stats->packets_out = privp->packets_out;
839 case NGM_PPPOE_CONNECT:
842 * Check the hook exists and is Uninitialised.
843 * Send a PADI request, and start the timeout logic.
844 * Store the originator of this message so we can send
845 * a success of fail message to them later.
846 * Move the session to SINIT.
847 * Set up the session to the correct state and
850 int i, acnlen = 0, acnsep = 0, srvlen;
851 for (i = 0; i < ourmsg->data_len; i++) {
852 if (ourmsg->data[i] == '\\') {
858 srvlen = ourmsg->data_len - acnlen - acnsep;
860 bcopy(ourmsg->data, neg->ac_name.data, acnlen);
861 neg->ac_name_len = acnlen;
863 neg->service.hdr.tag_type = PTT_SRV_NAME;
864 neg->service.hdr.tag_len = htons((uint16_t)srvlen);
865 bcopy(ourmsg->data + acnlen + acnsep,
866 neg->service.data, srvlen);
867 neg->service_len = srvlen;
871 case NGM_PPPOE_LISTEN:
873 * Check the hook exists and is Uninitialised.
874 * Install the service matching string.
875 * Store the originator of this message so we can send
876 * a success of fail message to them later.
877 * Move the hook to 'LISTENING'
879 neg->service.hdr.tag_type = PTT_SRV_NAME;
880 neg->service.hdr.tag_len =
881 htons((uint16_t)ourmsg->data_len);
883 if (ourmsg->data_len)
884 bcopy(ourmsg->data, neg->service.data,
886 neg->service_len = ourmsg->data_len;
887 neg->pkt->pkt_header.ph.code = PADT_CODE;
889 * Wait for PADI packet coming from Ethernet.
891 sp->state = PPPOE_LISTENING;
893 case NGM_PPPOE_OFFER:
895 * Check the hook exists and is Uninitialised.
896 * Store the originator of this message so we can send
897 * a success of fail message to them later.
898 * Store the AC-Name given and go to PRIMED.
900 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
901 neg->ac_name.hdr.tag_len =
902 htons((uint16_t)ourmsg->data_len);
903 if (ourmsg->data_len)
904 bcopy(ourmsg->data, neg->ac_name.data,
906 neg->ac_name_len = ourmsg->data_len;
907 neg->pkt->pkt_header.ph.code = PADO_CODE;
909 * Wait for PADI packet coming from hook.
911 sp->state = PPPOE_PRIMED;
913 case NGM_PPPOE_SERVICE:
915 * Check the session is primed.
916 * for now just allow ONE service to be advertised.
917 * If you do it twice you just overwrite.
919 if (sp->state != PPPOE_PRIMED) {
920 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
921 "primed\n", node->nd_ID);
925 neg->service.hdr.tag_type = PTT_SRV_NAME;
926 neg->service.hdr.tag_len =
927 htons((uint16_t)ourmsg->data_len);
929 if (ourmsg->data_len)
930 bcopy(ourmsg->data, neg->service.data,
932 neg->service_len = ourmsg->data_len;
934 case NGM_PPPOE_SETMODE:
939 if (msg->header.arglen == 0)
942 s = (char *)msg->data;
943 len = msg->header.arglen - 1;
945 /* Search for matching mode string. */
946 if (len == strlen(NG_PPPOE_STANDARD) &&
947 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
949 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
952 if (len == strlen(NG_PPPOE_3COM) &&
953 (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
954 privp->flags |= COMPAT_3COM;
955 privp->eh.ether_type =
956 ETHERTYPE_PPPOE_3COM_DISC;
959 if (len == strlen(NG_PPPOE_DLINK) &&
960 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
961 privp->flags |= COMPAT_DLINK;
967 case NGM_PPPOE_GETMODE:
972 if (privp->flags == 0)
973 len += strlen(NG_PPPOE_STANDARD) + 1;
974 if (privp->flags & COMPAT_3COM)
975 len += strlen(NG_PPPOE_3COM) + 1;
976 if (privp->flags & COMPAT_DLINK)
977 len += strlen(NG_PPPOE_DLINK) + 1;
979 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
983 s = (char *)resp->data;
984 if (privp->flags == 0) {
985 len = strlen(NG_PPPOE_STANDARD);
986 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
989 if (privp->flags & COMPAT_3COM) {
990 len = strlen(NG_PPPOE_3COM);
991 strlcpy(s, NG_PPPOE_3COM, len + 1);
994 if (privp->flags & COMPAT_DLINK) {
997 len = strlen(NG_PPPOE_DLINK);
998 strlcpy(s, NG_PPPOE_DLINK, len + 1);
1002 case NGM_PPPOE_SETENADDR:
1003 if (msg->header.arglen != ETHER_ADDR_LEN)
1005 bcopy(msg->data, &privp->eh.ether_shost,
1012 case NGM_ETHER_COOKIE:
1013 if (!(msg->header.flags & NGF_RESP))
1015 switch (msg->header.cmd) {
1016 case NGM_ETHER_GET_ENADDR:
1017 if (msg->header.arglen != ETHER_ADDR_LEN)
1019 bcopy(msg->data, &privp->eh.ether_shost,
1030 /* Take care of synchronous response, if any. */
1032 CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1033 NG_RESPOND_MSG(error, node, item, resp);
1034 /* Free the message and return. */
1040 * Start a client into the first state. A separate function because
1041 * it can be needed if the negotiation times out.
1044 pppoe_start(sessp sp)
1046 hook_p hook = sp->hook;
1047 node_p node = NG_HOOK_NODE(hook);
1048 priv_p privp = NG_NODE_PRIVATE(node);
1051 struct pppoe_tag hdr;
1058 * Kick the state machine into starting up.
1060 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1061 sp->state = PPPOE_SINIT;
1063 * Reset the packet header to broadcast. Since we are
1064 * in a client mode use configured ethertype.
1066 memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1067 sizeof(struct ether_header));
1068 neg->pkt->pkt_header.ph.code = PADI_CODE;
1069 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
1070 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
1071 uniqtag.data.pointer = sp;
1073 insert_tag(sp, &uniqtag.hdr);
1074 insert_tag(sp, &neg->service.hdr);
1077 * Send packet and prepare to retransmit it after timeout.
1079 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1080 pppoe_ticker, NULL, 0);
1081 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1082 m0 = m_copypacket(neg->m, M_DONTWAIT);
1083 NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
1087 send_acname(sessp sp, const struct pppoe_tag *tag)
1090 struct ng_mesg *msg;
1091 struct ngpppoe_sts *sts;
1093 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1095 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1096 sizeof(struct ngpppoe_sts), M_NOWAIT);
1100 sts = (struct ngpppoe_sts *)msg->data;
1101 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1102 strncpy(sts->hook, tag->tag_data, tlen);
1103 sts->hook[tlen] = '\0';
1104 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1110 send_sessionid(sessp sp)
1113 struct ng_mesg *msg;
1115 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1117 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1118 sizeof(uint16_t), M_NOWAIT);
1122 *(uint16_t *)msg->data = sp->Session_ID;
1123 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1129 * Receive data from session hook and do something with it.
1132 ng_pppoe_rcvdata(hook_p hook, item_p item)
1134 node_p node = NG_HOOK_NODE(hook);
1135 const priv_p privp = NG_NODE_PRIVATE(node);
1136 sessp sp = NG_HOOK_PRIVATE(hook);
1137 struct pppoe_full_hdr *wh;
1141 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1142 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1145 switch (sp->state) {
1146 case PPPOE_NEWCONNECTED:
1147 case PPPOE_CONNECTED: {
1148 static const u_char addrctrl[] = { 0xff, 0x03 };
1151 * Remove PPP address and control fields, if any.
1152 * For example, ng_ppp(4) always sends LCP packets
1153 * with address and control fields as required by
1154 * generic PPP. PPPoE is an exception to the rule.
1156 if (m->m_pkthdr.len >= 2) {
1157 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1159 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1163 * Bang in a pre-made header, and set the length up
1164 * to be correct. Then send it to the ethernet driver.
1166 M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1170 wh = mtod(m, struct pppoe_full_hdr *);
1171 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1172 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1173 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1174 privp->packets_out++;
1177 case PPPOE_PRIMED: {
1179 struct pppoe_tag hdr;
1182 const struct pppoe_tag *tag;
1184 const struct pppoe_hdr *ph;
1191 * A PADI packet is being returned by the application
1192 * that has set up this hook. This indicates that it
1193 * wants us to offer service.
1195 if (m->m_len < sizeof(*wh)) {
1196 m = m_pullup(m, sizeof(*wh));
1200 wh = mtod(m, struct pppoe_full_hdr *);
1202 session = ntohs(wh->ph.sid);
1203 length = ntohs(wh->ph.length);
1205 /* Use peers mode in session. */
1206 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1207 if (code != PADI_CODE)
1209 ng_uncallout(&neg->handle, node);
1212 * This is the first time we hear
1213 * from the client, so note it's
1214 * unicast address, replacing the
1215 * broadcast address.
1217 bcopy(wh->eh.ether_shost,
1218 neg->pkt->pkt_header.eh.ether_dhost,
1220 sp->state = PPPOE_SOFFER;
1222 neg->pkt->pkt_header.ph.code = PADO_CODE;
1225 * Start working out the tags to respond with.
1227 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1228 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1229 uniqtag.data.pointer = sp;
1231 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1232 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1233 insert_tag(sp, tag); /* return service */
1235 * If we have a NULL service request
1236 * and have an extra service defined in this hook,
1237 * then also add a tag for the extra service.
1238 * XXX this is a hack. eventually we should be able
1239 * to support advertising many services, not just one
1241 if (((tag == NULL) || (tag->tag_len == 0)) &&
1242 (neg->service.hdr.tag_len != 0)) {
1243 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1245 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1246 insert_tag(sp, tag); /* returned hostunique */
1247 insert_tag(sp, &uniqtag.hdr);
1251 * Send the offer but if they don't respond
1252 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1254 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1255 pppoe_ticker, NULL, 0);
1256 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1257 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1258 privp->packets_out++;
1263 * Packets coming from the hook make no sense
1264 * to sessions in the rest of states. Throw them away.
1277 * Receive data from ether and do something with it.
1280 ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1282 node_p node = NG_HOOK_NODE(hook);
1283 const priv_p privp = NG_NODE_PRIVATE(node);
1284 sessp sp = NG_HOOK_PRIVATE(hook);
1285 const struct pppoe_tag *utag = NULL, *tag = NULL;
1286 const struct pppoe_full_hdr *wh;
1287 const struct pppoe_hdr *ph;
1297 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1298 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1302 * Dig out various fields from the packet.
1303 * Use them to decide where to send it.
1305 privp->packets_in++;
1306 if( m->m_len < sizeof(*wh)) {
1307 m = m_pullup(m, sizeof(*wh)); /* Checks length */
1309 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1310 "m_pullup(wh)\n", node->nd_ID);
1314 wh = mtod(m, struct pppoe_full_hdr *);
1315 length = ntohs(wh->ph.length);
1316 switch(wh->eh.ether_type) {
1317 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1318 case ETHERTYPE_PPPOE_DISC:
1320 * We need to try to make sure that the tag area
1321 * is contiguous, or we could wander off the end
1322 * of a buffer and make a mess.
1323 * (Linux wouldn't have this problem).
1325 if (m->m_pkthdr.len <= MHLEN) {
1326 if( m->m_len < m->m_pkthdr.len) {
1327 m = m_pullup(m, m->m_pkthdr.len);
1329 log(LOG_NOTICE, "ng_pppoe[%x]: "
1330 "couldn't m_pullup(pkthdr)\n",
1336 if (m->m_len != m->m_pkthdr.len) {
1338 * It's not all in one piece.
1339 * We need to do extra work.
1340 * Put it into a cluster.
1343 n = m_dup(m, M_DONTWAIT);
1347 /* just check we got a cluster */
1348 if (m->m_len != m->m_pkthdr.len) {
1354 log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1355 "fragmented\n", node->nd_ID);
1359 wh = mtod(m, struct pppoe_full_hdr *);
1360 length = ntohs(wh->ph.length);
1362 session = ntohs(wh->ph.sid);
1369 * Look for a hook with the required service and send
1370 * the ENTIRE packet up there. It should come back to
1371 * a new hook in PRIMED state. Look there for further
1374 tag = get_tag(ph, PTT_SRV_NAME);
1376 CTR1(KTR_NET, "%20s: PADI w/o Service-Name",
1382 * First, try to match Service-Name against our
1383 * listening hooks. If no success and we are in D-Link
1384 * compat mode and Service-Name is empty, then we
1385 * broadcast the PADI to all listening hooks.
1387 sendhook = pppoe_match_svc(node, tag);
1388 if (sendhook != NULL)
1389 NG_FWD_NEW_DATA(error, item, sendhook, m);
1390 else if (privp->flags & COMPAT_DLINK &&
1391 ntohs(tag->tag_len) == 0)
1392 error = pppoe_broadcast_padi(node, m);
1394 error = ENETUNREACH;
1399 * Use the host_uniq tag to find the hook this is in
1400 * response to. Received #2, now send #3
1401 * For now simply accept the first we receive.
1403 utag = get_tag(ph, PTT_HOST_UNIQ);
1404 if ((utag == NULL) ||
1405 (ntohs(utag->tag_len) != sizeof(sp))) {
1406 log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1407 "unique field\n", node->nd_ID);
1411 sendhook = pppoe_finduniq(node, utag);
1412 if (sendhook == NULL) {
1413 log(LOG_NOTICE, "ng_pppoe[%x]: no "
1414 "matching session\n", node->nd_ID);
1419 * Check the session is in the right state.
1420 * It needs to be in PPPOE_SINIT.
1422 sp = NG_HOOK_PRIVATE(sendhook);
1423 if (sp->state == PPPOE_SREQ ||
1424 sp->state == PPPOE_CONNECTED) {
1425 break; /* Multiple PADO is OK. */
1427 if (sp->state != PPPOE_SINIT) {
1428 log(LOG_NOTICE, "ng_pppoe[%x]: session "
1429 "in wrong state\n", node->nd_ID);
1433 /* If requested specific AC-name, check it. */
1434 if (neg->ac_name_len) {
1435 tag = get_tag(ph, PTT_AC_NAME);
1437 /* No PTT_AC_NAME in PADO */
1440 if (neg->ac_name_len != htons(tag->tag_len) ||
1441 strncmp(neg->ac_name.data, tag->tag_data,
1442 neg->ac_name_len) != 0) {
1446 sp->state = PPPOE_SREQ;
1447 ng_uncallout(&neg->handle, node);
1450 * This is the first time we hear
1451 * from the server, so note it's
1452 * unicast address, replacing the
1453 * broadcast address .
1455 bcopy(wh->eh.ether_shost,
1456 neg->pkt->pkt_header.eh.ether_dhost,
1459 neg->pkt->pkt_header.ph.code = PADR_CODE;
1461 insert_tag(sp, utag); /* Host Unique */
1462 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1463 insert_tag(sp, tag); /* return cookie */
1464 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1465 insert_tag(sp, tag); /* return it */
1466 send_acname(sp, tag);
1468 insert_tag(sp, &neg->service.hdr); /* Service */
1471 sp->state = PPPOE_SREQ;
1472 ng_callout(&neg->handle, node, sp->hook,
1473 PPPOE_INITIAL_TIMEOUT * hz,
1474 pppoe_ticker, NULL, 0);
1475 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1476 m0 = m_copypacket(neg->m, M_DONTWAIT);
1477 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1482 * Use the ac_cookie tag to find the
1483 * hook this is in response to.
1485 utag = get_tag(ph, PTT_AC_COOKIE);
1486 if ((utag == NULL) ||
1487 (ntohs(utag->tag_len) != sizeof(sp))) {
1491 sendhook = pppoe_finduniq(node, utag);
1492 if (sendhook == NULL)
1496 * Check the session is in the right state.
1497 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1498 * If the latter, then this is a retry by the client,
1499 * so be nice, and resend.
1501 sp = NG_HOOK_PRIVATE(sendhook);
1502 if (sp->state == PPPOE_NEWCONNECTED) {
1504 * Whoa! drop back to resend that PADS packet.
1505 * We should still have a copy of it.
1507 sp->state = PPPOE_SOFFER;
1509 if (sp->state != PPPOE_SOFFER)
1510 LEAVE (ENETUNREACH);
1512 ng_uncallout(&neg->handle, node);
1513 neg->pkt->pkt_header.ph.code = PADS_CODE;
1514 if (sp->Session_ID == 0)
1515 neg->pkt->pkt_header.ph.sid =
1516 htons(sp->Session_ID
1517 = get_new_sid(node));
1521 * start working out the tags to respond with.
1524 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1525 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1526 insert_tag(sp, tag);/* return service */
1527 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1528 insert_tag(sp, tag); /* return it */
1529 insert_tag(sp, utag); /* ac_cookie */
1532 sp->state = PPPOE_NEWCONNECTED;
1534 /* Send the PADS without a timeout - we're now connected. */
1535 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1536 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1539 * Having sent the last Negotiation header,
1540 * Set up the stored packet header to be correct for
1541 * the actual session. But keep the negotialtion stuff
1542 * around in case we need to resend this last packet.
1543 * We'll discard it when we move from NEWCONNECTED
1546 sp->pkt_hdr = neg->pkt->pkt_header;
1547 /* Configure ethertype depending on what
1548 * ethertype was used at discovery phase */
1549 if (sp->pkt_hdr.eh.ether_type ==
1550 ETHERTYPE_PPPOE_3COM_DISC)
1551 sp->pkt_hdr.eh.ether_type
1552 = ETHERTYPE_PPPOE_3COM_SESS;
1554 sp->pkt_hdr.eh.ether_type
1555 = ETHERTYPE_PPPOE_SESS;
1556 sp->pkt_hdr.ph.code = 0;
1557 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1562 * Use the host_uniq tag to find the hook this is in
1563 * response to. Take the session ID and store it away.
1564 * Also make sure the pre-made header is correct and
1565 * set us into Session mode.
1567 utag = get_tag(ph, PTT_HOST_UNIQ);
1568 if ((utag == NULL) ||
1569 (ntohs(utag->tag_len) != sizeof(sp))) {
1570 LEAVE (ENETUNREACH);
1572 sendhook = pppoe_finduniq(node, utag);
1573 if (sendhook == NULL)
1577 * Check the session is in the right state.
1578 * It needs to be in PPPOE_SREQ.
1580 sp = NG_HOOK_PRIVATE(sendhook);
1581 if (sp->state != PPPOE_SREQ)
1584 ng_uncallout(&neg->handle, node);
1585 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1586 sp->Session_ID = ntohs(wh->ph.sid);
1589 sp->state = PPPOE_CONNECTED;
1591 * Now we have gone to Connected mode,
1592 * Free all resources needed for negotiation.
1593 * Keep a copy of the header we will be using.
1595 sp->pkt_hdr = neg->pkt->pkt_header;
1596 if (privp->flags & COMPAT_3COM)
1597 sp->pkt_hdr.eh.ether_type
1598 = ETHERTYPE_PPPOE_3COM_SESS;
1600 sp->pkt_hdr.eh.ether_type
1601 = ETHERTYPE_PPPOE_SESS;
1602 sp->pkt_hdr.ph.code = 0;
1604 free(sp->neg, M_NETGRAPH_PPPOE);
1606 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1610 * Find matching peer/session combination.
1612 sendhook = pppoe_findsession(node, wh);
1613 if (sendhook == NULL)
1615 /* Disconnect that hook. */
1616 ng_rmhook_self(sendhook);
1619 LEAVE(EPFNOSUPPORT);
1622 case ETHERTYPE_PPPOE_3COM_SESS:
1623 case ETHERTYPE_PPPOE_SESS:
1625 * Find matching peer/session combination.
1627 sendhook = pppoe_findsession(node, wh);
1628 if (sendhook == NULL)
1629 LEAVE (ENETUNREACH);
1630 sp = NG_HOOK_PRIVATE(sendhook);
1631 m_adj(m, sizeof(*wh));
1633 /* If packet too short, dump it. */
1634 if (m->m_pkthdr.len < length)
1636 /* Also need to trim excess at the end */
1637 if (m->m_pkthdr.len > length) {
1638 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1640 if ( sp->state != PPPOE_CONNECTED) {
1641 if (sp->state == PPPOE_NEWCONNECTED) {
1642 sp->state = PPPOE_CONNECTED;
1644 * Now we have gone to Connected mode,
1645 * Free all resources needed for negotiation.
1646 * Be paranoid about whether there may be
1649 m_freem(sp->neg->m);
1650 ng_uncallout(&sp->neg->handle, node);
1651 free(sp->neg, M_NETGRAPH_PPPOE);
1654 LEAVE (ENETUNREACH);
1657 NG_FWD_NEW_DATA(error, item, sendhook, m);
1660 LEAVE(EPFNOSUPPORT);
1670 * Receive data from debug hook and bypass it to ether.
1673 ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1675 node_p node = NG_HOOK_NODE(hook);
1676 const priv_p privp = NG_NODE_PRIVATE(node);
1679 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1680 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1682 NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1683 privp->packets_out++;
1688 * Do local shutdown processing..
1689 * If we are a persistant device, we might refuse to go away, and
1690 * we'd only remove our links and reset ourself.
1693 ng_pppoe_shutdown(node_p node)
1695 const priv_p privdata = NG_NODE_PRIVATE(node);
1697 NG_NODE_SET_PRIVATE(node, NULL);
1698 NG_NODE_UNREF(privdata->node);
1699 free(privdata, M_NETGRAPH_PPPOE);
1704 * Hook disconnection
1706 * Clean up all dangling links and information about the session/hook.
1707 * For this type, removal of the last link destroys the node.
1710 ng_pppoe_disconnect(hook_p hook)
1712 node_p node = NG_HOOK_NODE(hook);
1713 priv_p privp = NG_NODE_PRIVATE(node);
1716 if (hook == privp->debug_hook) {
1717 privp->debug_hook = NULL;
1718 } else if (hook == privp->ethernet_hook) {
1719 privp->ethernet_hook = NULL;
1720 if (NG_NODE_IS_VALID(node))
1721 ng_rmnode_self(node);
1723 sp = NG_HOOK_PRIVATE(hook);
1724 if (sp->state != PPPOE_SNONE ) {
1725 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1728 * According to the spec, if we are connected,
1729 * we should send a DISC packet if we are shutting down
1732 if ((privp->ethernet_hook)
1733 && ((sp->state == PPPOE_CONNECTED)
1734 || (sp->state == PPPOE_NEWCONNECTED))) {
1737 /* Generate a packet of that type. */
1738 MGETHDR(m, M_DONTWAIT, MT_DATA);
1740 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1741 "mbufs\n", node->nd_ID);
1743 struct pppoe_full_hdr *wh;
1744 struct pppoe_tag *tag;
1745 int msglen = strlen(SIGNOFF);
1748 m->m_pkthdr.rcvif = NULL;
1749 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1750 wh = mtod(m, struct pppoe_full_hdr *);
1751 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1753 /* Revert the stored header to DISC/PADT mode. */
1754 wh->ph.code = PADT_CODE;
1756 * Configure ethertype depending on what
1757 * was used during sessions stage.
1759 if (wh->eh.ether_type ==
1760 ETHERTYPE_PPPOE_3COM_SESS)
1761 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1763 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1765 * Add a General error message and adjust
1769 tag->tag_type = PTT_GEN_ERR;
1770 tag->tag_len = htons((u_int16_t)msglen);
1771 strncpy(tag->tag_data, SIGNOFF, msglen);
1772 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1774 wh->ph.length = htons(sizeof(*tag) + msglen);
1775 NG_SEND_DATA_ONLY(error,
1776 privp->ethernet_hook, m);
1780 * As long as we have somewhere to store the timeout handle,
1781 * we may have a timeout pending.. get rid of it.
1784 ng_uncallout(&sp->neg->handle, node);
1786 m_freem(sp->neg->m);
1787 free(sp->neg, M_NETGRAPH_PPPOE);
1789 free(sp, M_NETGRAPH_PPPOE);
1790 NG_HOOK_SET_PRIVATE(hook, NULL);
1792 if ((NG_NODE_NUMHOOKS(node) == 0) &&
1793 (NG_NODE_IS_VALID(node)))
1794 ng_rmnode_self(node);
1799 * Timeouts come here.
1802 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
1804 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1805 sessp sp = NG_HOOK_PRIVATE(hook);
1807 struct mbuf *m0 = NULL;
1810 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1811 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
1814 * Resend the last packet, using an exponential backoff.
1815 * After a period of time, stop growing the backoff,
1816 * And either leave it, or revert to the start.
1820 /* Timeouts on these produce resends. */
1821 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1822 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1823 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1824 pppoe_ticker, NULL, 0);
1825 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1826 if (sp->state == PPPOE_SREQ) {
1827 /* Revert to SINIT mode. */
1830 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1836 /* A timeout on these says "give up" */
1837 ng_rmhook_self(hook);
1840 /* Timeouts have no meaning in other states. */
1841 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1847 * Parse an incoming packet to see if any tags should be copied to the
1848 * output packet. Don't do any tags that have been handled in the main
1851 static const struct pppoe_tag*
1852 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1854 const char *const end = (const char *)next_tag(ph);
1856 const struct pppoe_tag *pt = &ph->tag[0];
1859 * Keep processing tags while a tag header will still fit.
1861 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1863 while((const char*)(pt + 1) <= end) {
1865 * If the tag data would go past the end of the packet, abort.
1867 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1871 switch (pt->tag_type) {
1887 pt = (const struct pppoe_tag*)ptn;
1893 pppoe_send_event(sessp sp, enum cmd cmdid)
1896 struct ng_mesg *msg;
1897 struct ngpppoe_sts *sts;
1899 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1901 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1902 sizeof(struct ngpppoe_sts), M_NOWAIT);
1905 sts = (struct ngpppoe_sts *)msg->data;
1906 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1907 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);