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/epoch.h>
52 #include <sys/socket.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <net/ethernet.h>
57 #include <net/if_vlan_var.h>
60 #include <netgraph/ng_message.h>
61 #include <netgraph/netgraph.h>
62 #include <netgraph/ng_parse.h>
63 #include <netgraph/ng_pppoe.h>
64 #include <netgraph/ng_ether.h>
66 #ifdef NG_SEPARATE_MALLOC
67 static MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
69 #define M_NETGRAPH_PPPOE M_NETGRAPH
72 /* Some PPP protocol numbers we're interested in */
73 #define PROT_LCP 0xc021
75 #define SIGNOFF "session closed"
77 VNET_DEFINE_STATIC(u_int32_t, ng_pppoe_lcp_pcp) = 0;
78 #define V_ng_pppoe_lcp_pcp VNET(ng_pppoe_lcp_pcp)
80 SYSCTL_NODE(_net_graph, OID_AUTO, pppoe, CTLFLAG_RW, 0, "PPPoE");
81 SYSCTL_UINT(_net_graph_pppoe, OID_AUTO, lcp_pcp,
82 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ng_pppoe_lcp_pcp), 0,
86 * This section contains the netgraph method declarations for the
87 * pppoe node. These methods define the netgraph pppoe 'type'.
90 static ng_constructor_t ng_pppoe_constructor;
91 static ng_rcvmsg_t ng_pppoe_rcvmsg;
92 static ng_shutdown_t ng_pppoe_shutdown;
93 static ng_newhook_t ng_pppoe_newhook;
94 static ng_connect_t ng_pppoe_connect;
95 static ng_rcvdata_t ng_pppoe_rcvdata;
96 static ng_rcvdata_t ng_pppoe_rcvdata_ether;
97 static ng_rcvdata_t ng_pppoe_rcvdata_debug;
98 static ng_disconnect_t ng_pppoe_disconnect;
100 /* Parse type for struct ngpppoe_init_data */
101 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
102 = NG_PPPOE_INIT_DATA_TYPE_INFO;
103 static const struct ng_parse_type ngpppoe_init_data_state_type = {
104 &ng_parse_struct_type,
105 &ngpppoe_init_data_type_fields
108 /* Parse type for struct ngpppoe_sts */
109 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
110 = NG_PPPOE_STS_TYPE_INFO;
111 static const struct ng_parse_type ng_pppoe_sts_state_type = {
112 &ng_parse_struct_type,
113 &ng_pppoe_sts_type_fields
116 /* List of commands and how to convert arguments to/from ASCII */
117 static const struct ng_cmdlist ng_pppoe_cmds[] = {
122 &ngpppoe_init_data_state_type,
129 &ngpppoe_init_data_state_type,
136 &ngpppoe_init_data_state_type,
143 &ngpppoe_init_data_state_type,
150 &ng_pppoe_sts_state_type,
157 &ng_pppoe_sts_state_type,
164 &ng_pppoe_sts_state_type,
171 &ng_parse_string_type,
179 &ng_parse_string_type
185 &ng_parse_enaddr_type,
192 &ng_parse_uint16_type,
199 &ngpppoe_init_data_state_type,
206 &ngpppoe_init_data_state_type,
212 /* Netgraph node type descriptor */
213 static struct ng_type typestruct = {
214 .version = NG_ABI_VERSION,
215 .name = NG_PPPOE_NODE_TYPE,
216 .constructor = ng_pppoe_constructor,
217 .rcvmsg = ng_pppoe_rcvmsg,
218 .shutdown = ng_pppoe_shutdown,
219 .newhook = ng_pppoe_newhook,
220 .connect = ng_pppoe_connect,
221 .rcvdata = ng_pppoe_rcvdata,
222 .disconnect = ng_pppoe_disconnect,
223 .cmdlist = ng_pppoe_cmds,
225 NETGRAPH_INIT(pppoe, &typestruct);
228 * States for the session state machine.
229 * These have no meaning if there is no hook attached yet.
232 PPPOE_SNONE=0, /* [both] Initial state */
233 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
234 PPPOE_SINIT, /* [Client] Sent discovery initiation */
235 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
236 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
237 PPPOE_SREQ, /* [Client] Sent a Request */
238 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
239 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
240 PPPOE_DEAD /* [Both] */
243 #define NUMTAGS 20 /* number of tags we are set up to work with */
246 * Information we store for each hook on each node for negotiating the
247 * session. The mbuf and cluster are freed once negotiation has completed.
248 * The whole negotiation block is then discarded.
252 struct mbuf *m; /* holds cluster with last sent packet */
253 union packet *pkt; /* points within the above cluster */
254 struct callout handle; /* see timeout(9) */
255 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
257 const struct pppoe_tag *tags[NUMTAGS];
262 struct datatag service;
263 struct datatag ac_name;
264 struct datatag host_uniq;
266 typedef struct sess_neg *negp;
269 * Session information that is needed after connection.
275 ng_ID_t creator; /* who to notify */
276 struct pppoe_full_hdr pkt_hdr; /* used when connected */
277 negp neg; /* used when negotiating */
278 LIST_ENTRY(sess_con) sessions;
280 typedef struct sess_con *sessp;
282 #define SESSHASHSIZE 0x0100
283 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
285 struct sess_hash_entry {
287 LIST_HEAD(hhead, sess_con) head;
291 * Information we store for each node
294 node_p node; /* back pointer to node */
295 hook_p ethernet_hook;
297 u_int packets_in; /* packets in from ethernet */
298 u_int packets_out; /* packets out towards ethernet */
300 #define COMPAT_3COM 0x00000001
301 #define COMPAT_DLINK 0x00000002
302 struct ether_header eh;
303 LIST_HEAD(, sess_con) listeners;
304 struct sess_hash_entry sesshash[SESSHASHSIZE];
305 struct maxptag max_payload; /* PPP-Max-Payload (RFC4638) */
307 typedef struct PPPoE *priv_p;
310 char bytes[sizeof(void *)];
314 #define LEAVE(x) do { error = x; goto quit; } while(0)
315 static void pppoe_start(sessp sp);
316 static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
317 static const struct pppoe_tag *scan_tags(sessp sp,
318 const struct pppoe_hdr* ph);
319 static int pppoe_send_event(sessp sp, enum cmd cmdid);
321 /*************************************************************************
322 * Some basic utilities from the Linux version with author's permission.*
323 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
324 ************************************************************************/
327 * Return the location where the next tag can be put
329 static __inline const struct pppoe_tag*
330 next_tag(const struct pppoe_hdr* ph)
332 return (const struct pppoe_tag*)(((const char*)(ph + 1))
333 + ntohs(ph->length));
337 * Look for a tag of a specific type.
338 * Don't trust any length the other end says,
339 * but assume we already sanity checked ph->length.
341 static const struct pppoe_tag*
342 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
344 const char *const end = (const char *)next_tag(ph);
345 const struct pppoe_tag *pt = (const void *)(ph + 1);
349 * Keep processing tags while a tag header will still fit.
351 while((const char*)(pt + 1) <= end) {
353 * If the tag data would go past the end of the packet, abort.
355 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
357 CTR2(KTR_NET, "%20s: invalid length for tag %d",
361 if (pt->tag_type == idx) {
362 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
366 pt = (const struct pppoe_tag*)ptn;
369 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
373 /**************************************************************************
374 * Inlines to initialise or add tags to a session's tag list.
375 **************************************************************************/
377 * Initialise the session's tag list.
382 KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
383 sp->neg->numtags = 0;
387 insert_tag(sessp sp, const struct pppoe_tag *tp)
392 KASSERT(neg != NULL, ("%s: no neg", __func__));
393 if ((i = neg->numtags++) < NUMTAGS) {
396 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
403 * Make up a packet, using the tags filled out for the session.
405 * Assume that the actual pppoe header and ethernet header
406 * are filled out externally to this routine.
407 * Also assume that neg->wh points to the correct
408 * location at the front of the buffer space.
411 make_packet(sessp sp) {
412 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
413 const struct pppoe_tag **tag;
419 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
420 ("%s: called from wrong state", __func__));
421 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
423 dp = (char *)(&wh->ph + 1);
424 for (count = 0, tag = sp->neg->tags;
425 ((count < sp->neg->numtags) && (count < NUMTAGS));
427 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
428 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
429 log(LOG_NOTICE, "ng_pppoe: tags too long\n");
430 sp->neg->numtags = count;
431 break; /* XXX chop off what's too long */
433 bcopy(*tag, (char *)dp, tlen);
437 wh->ph.length = htons(length);
438 sp->neg->m->m_len = length + sizeof(*wh);
439 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
442 /**************************************************************************
443 * Routines to match a service. *
444 **************************************************************************/
447 * Find a hook that has a service string that matches that
448 * we are seeking. For now use a simple string.
449 * In the future we may need something like regexp().
451 * Null string is a wildcard (ANY service), according to RFC2516.
452 * And historical FreeBSD wildcard is also "*".
456 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
458 const priv_p privp = NG_NODE_PRIVATE(node);
461 LIST_FOREACH(sp, &privp->listeners, sessions) {
464 /* Empty Service-Name matches any service. */
465 if (neg->service_len == 0)
468 /* Special case for a blank or "*" service name (wildcard). */
469 if (neg->service_len == 1 && neg->service.data[0] == '*')
472 /* If the lengths don't match, that aint it. */
473 if (neg->service_len != ntohs(tag->tag_len))
476 if (strncmp((const char *)(tag + 1), neg->service.data,
477 ntohs(tag->tag_len)) == 0)
480 CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
481 sp?sp->hook:NULL, (const char *)(tag + 1));
483 return (sp?sp->hook:NULL);
487 * Broadcast the PADI packet in m0 to all listening hooks.
488 * This routine is called when a PADI with empty Service-Name
489 * tag is received. Client should receive PADOs with all
490 * available services.
493 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
495 const priv_p privp = NG_NODE_PRIVATE(node);
499 LIST_FOREACH(sp, &privp->listeners, sessions) {
502 m = m_dup(m0, M_NOWAIT);
505 NG_SEND_DATA_ONLY(error, sp->hook, m);
514 * Find a hook, which name equals to given service.
517 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
519 const priv_p privp = NG_NODE_PRIVATE(node);
522 LIST_FOREACH(sp, &privp->listeners, sessions) {
525 if (neg->service_len == svc_len &&
526 strncmp(svc_name, neg->service.data, svc_len) == 0)
533 /**************************************************************************
534 * Routines to find a particular session that matches an incoming packet. *
535 **************************************************************************/
536 /* Find free session and add to hash. */
538 pppoe_getnewsession(sessp sp)
540 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
541 static uint16_t pppoe_sid = 1;
546 /* Atomicity is not needed here as value will be checked. */
548 /* Spec says 0xFFFF is reserved, also don't use 0x0000. */
549 if (val == 0xffff || val == 0x0000)
552 /* Check it isn't already in use. */
553 hash = SESSHASH(val);
554 mtx_lock(&privp->sesshash[hash].mtx);
555 LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
556 if (tsp->Session_ID == val)
560 sp->Session_ID = val;
561 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
563 mtx_unlock(&privp->sesshash[hash].mtx);
567 CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
572 /* Add specified session to hash. */
574 pppoe_addsession(sessp sp)
576 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
577 uint16_t hash = SESSHASH(sp->Session_ID);
579 mtx_lock(&privp->sesshash[hash].mtx);
580 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
581 mtx_unlock(&privp->sesshash[hash].mtx);
584 /* Delete specified session from hash. */
586 pppoe_delsession(sessp sp)
588 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
589 uint16_t hash = SESSHASH(sp->Session_ID);
591 mtx_lock(&privp->sesshash[hash].mtx);
592 LIST_REMOVE(sp, sessions);
593 mtx_unlock(&privp->sesshash[hash].mtx);
596 /* Find matching peer/session combination. */
598 pppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
600 uint16_t session = ntohs(wh->ph.sid);
601 uint16_t hash = SESSHASH(session);
604 mtx_lock(&privp->sesshash[hash].mtx);
605 LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
606 if (sp->Session_ID == session &&
607 bcmp(sp->pkt_hdr.eh.ether_dhost,
608 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
612 mtx_unlock(&privp->sesshash[hash].mtx);
613 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
620 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
625 /* Cycle through all known hooks. */
626 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
627 /* Skip any nonsession hook. */
628 if (NG_HOOK_PRIVATE(hook) == NULL)
630 sp = NG_HOOK_PRIVATE(hook);
631 /* Skip already connected sessions. */
634 if (sp->neg->host_uniq_len == ntohs(tag->tag_len) &&
635 bcmp(sp->neg->host_uniq.data, (const char *)(tag + 1),
636 sp->neg->host_uniq_len) == 0)
639 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, sp);
645 pppoe_findcookie(node_p node, const struct pppoe_tag *tag)
650 bcopy(tag + 1, cookie.bytes, sizeof(void *));
651 /* Cycle through all known hooks. */
652 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
653 /* Skip any nonsession hook. */
654 if (NG_HOOK_PRIVATE(hook) == NULL)
656 if (cookie.pointer == NG_HOOK_PRIVATE(hook))
659 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, cookie.pointer);
664 /**************************************************************************
665 * Start of Netgraph entrypoints. *
666 **************************************************************************/
669 * Allocate the private data structure and link it with node.
672 ng_pppoe_constructor(node_p node)
677 /* Initialize private descriptor. */
678 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_ZERO);
680 /* Link structs together; this counts as our one reference to *node. */
681 NG_NODE_SET_PRIVATE(node, privp);
684 /* Initialize to standard mode. */
685 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
686 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
688 LIST_INIT(&privp->listeners);
689 for (i = 0; i < SESSHASHSIZE; i++) {
690 mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
691 LIST_INIT(&privp->sesshash[i].head);
694 CTR3(KTR_NET, "%20s: created node [%x] (%p)",
695 __func__, node->nd_ID, node);
701 * Give our ok for a hook to be added...
702 * point the hook's private info to the hook structure.
704 * The following hook names are special:
705 * "ethernet": the hook that should be connected to a NIC.
706 * "debug": copies of data sent out here (when I write the code).
707 * All other hook names need only be unique. (the framework checks this).
710 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
712 const priv_p privp = NG_NODE_PRIVATE(node);
715 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
716 privp->ethernet_hook = hook;
717 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
718 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
719 privp->debug_hook = hook;
720 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
723 * Any other unique name is OK.
724 * The infrastructure has already checked that it's unique,
725 * so just allocate it and hook it in.
727 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
731 NG_HOOK_SET_PRIVATE(hook, sp);
734 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
735 __func__, node->nd_ID, node, name, hook);
741 * Hook has been added successfully. Request the MAC address of
742 * the underlying Ethernet node.
745 ng_pppoe_connect(hook_p hook)
747 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
751 if (hook != privp->ethernet_hook)
755 * If this is Ethernet hook, then request MAC address
756 * from our downstream.
758 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
763 * Our hook and peer hook have HK_INVALID flag set,
764 * so we can't use NG_SEND_MSG_HOOK() macro here.
766 NG_SEND_MSG_ID(error, privp->node, msg,
767 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
768 NG_NODE_ID(privp->node));
773 * Get a netgraph control message.
774 * Check it is one we understand. If needed, send a response.
775 * We sometimes save the address for an async action later.
776 * Always free the message.
779 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
781 struct epoch_tracker et;
782 priv_p privp = NG_NODE_PRIVATE(node);
783 struct ngpppoe_init_data *ourmsg = NULL;
784 struct ng_mesg *resp = NULL;
791 NGI_GET_MSG(item, msg);
792 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
793 __func__, node->nd_ID, node, msg->header.cmd,
794 msg->header.typecookie);
796 /* Deal with message according to cookie and command. */
797 switch (msg->header.typecookie) {
798 case NGM_PPPOE_COOKIE:
799 switch (msg->header.cmd) {
800 case NGM_PPPOE_CONNECT:
801 case NGM_PPPOE_LISTEN:
802 case NGM_PPPOE_OFFER:
803 case NGM_PPPOE_SERVICE:
804 case NGM_PPPOE_SEND_HURL:
805 case NGM_PPPOE_SEND_MOTM:
806 ourmsg = (struct ngpppoe_init_data *)msg->data;
807 if (msg->header.arglen < sizeof(*ourmsg)) {
808 log(LOG_ERR, "ng_pppoe[%x]: init data too "
809 "small\n", node->nd_ID);
812 if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
813 msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
814 if (msg->header.arglen - sizeof(*ourmsg) >
815 PPPOE_PADM_VALUE_SIZE) {
816 log(LOG_ERR, "ng_pppoe[%x]: message "
817 "too big\n", node->nd_ID);
821 if (msg->header.arglen - sizeof(*ourmsg) >
822 PPPOE_SERVICE_NAME_SIZE) {
823 log(LOG_ERR, "ng_pppoe[%x]: service name "
824 "too big\n", node->nd_ID);
828 if (msg->header.arglen - sizeof(*ourmsg) <
830 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
831 "length, %d should be %zd\n", node->nd_ID,
833 msg->header.arglen - sizeof (*ourmsg));
837 /* Make sure strcmp will terminate safely. */
838 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
840 /* Find hook by name. */
841 hook = ng_findhook(node, ourmsg->hook);
845 sp = NG_HOOK_PRIVATE(hook);
849 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
851 * Ensure we aren't already listening for this
854 if (pppoe_find_svc(node, ourmsg->data,
855 ourmsg->data_len) != NULL)
860 * PPPOE_SERVICE advertisements are set up
861 * on sessions that are in PRIMED state.
863 if (msg->header.cmd == NGM_PPPOE_SERVICE)
867 * PADM messages are set up on active sessions.
869 if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
870 msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
871 if (sp->state != PPPOE_NEWCONNECTED &&
872 sp->state != PPPOE_CONNECTED) {
873 log(LOG_NOTICE, "ng_pppoe[%x]: session is not "
874 "active\n", node->nd_ID);
880 if (sp->state != PPPOE_SNONE) {
881 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
882 "active\n", node->nd_ID);
887 * Set up prototype header.
889 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
895 neg->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
896 if (neg->m == NULL) {
897 free(neg, M_NETGRAPH_PPPOE);
900 neg->m->m_pkthdr.rcvif = NULL;
902 ng_callout_init(&neg->handle);
903 neg->m->m_len = sizeof(struct pppoe_full_hdr);
904 neg->pkt = mtod(neg->m, union packet*);
905 memcpy((void *)&neg->pkt->pkt_header.eh,
906 &privp->eh, sizeof(struct ether_header));
907 neg->pkt->pkt_header.ph.ver = 0x1;
908 neg->pkt->pkt_header.ph.type = 0x1;
909 neg->pkt->pkt_header.ph.sid = 0x0000;
912 sp->creator = NGI_RETADDR(item);
914 switch (msg->header.cmd) {
915 case NGM_PPPOE_GET_STATUS:
917 struct ngpppoestat *stats;
919 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
923 stats = (struct ngpppoestat *) resp->data;
924 stats->packets_in = privp->packets_in;
925 stats->packets_out = privp->packets_out;
928 case NGM_PPPOE_CONNECT:
931 * Check the hook exists and is Uninitialised.
932 * Send a PADI request, and start the timeout logic.
933 * Store the originator of this message so we can send
934 * a success or fail message to them later.
935 * Move the session to SINIT.
936 * Set up the session to the correct state and
939 int acnpos, acnlen = 0, acnsep = 0;
940 int hupos, hulen = 0, husep = 0;
941 int i, srvpos, srvlen;
943 for (i = 0; i < ourmsg->data_len; i++) {
944 if (ourmsg->data[i] == '\\') {
950 hupos = acnlen + acnsep;
951 for (i = hupos; i < ourmsg->data_len; i++) {
952 if (ourmsg->data[i] == '|') {
958 srvpos = hupos + hulen + husep;
959 srvlen = ourmsg->data_len - srvpos;
961 bcopy(ourmsg->data + acnpos, neg->ac_name.data, acnlen);
962 neg->ac_name_len = acnlen;
964 neg->host_uniq.hdr.tag_type = PTT_HOST_UNIQ;
966 /* Not provided, generate one */
967 neg->host_uniq.hdr.tag_len = htons(sizeof(sp));
968 bcopy(&sp, neg->host_uniq.data, sizeof(sp));
969 neg->host_uniq_len = sizeof(sp);
970 } else if (hulen > 2 && ourmsg->data[hupos] == '0' &&
971 ourmsg->data[hupos + 1] == 'x' && hulen % 2 == 0) {
973 static const char hexdig[16] = "0123456789abcdef";
976 neg->host_uniq.hdr.tag_len = htons((uint16_t)(hulen / 2 - 1));
977 for (i = 0; i < hulen - 2; i++) {
980 ourmsg->data[hupos + 2 + i] != hexdig[j];
985 neg->host_uniq.data[i / 2] = j << 4;
987 neg->host_uniq.data[i / 2] |= j;
989 neg->host_uniq_len = hulen / 2 - 1;
992 neg->host_uniq.hdr.tag_len = htons((uint16_t)hulen);
993 bcopy(ourmsg->data + hupos, neg->host_uniq.data, hulen);
994 neg->host_uniq_len = hulen;
997 neg->service.hdr.tag_type = PTT_SRV_NAME;
998 neg->service.hdr.tag_len = htons((uint16_t)srvlen);
999 bcopy(ourmsg->data + srvpos, neg->service.data, srvlen);
1000 neg->service_len = srvlen;
1001 NET_EPOCH_ENTER(et);
1006 case NGM_PPPOE_LISTEN:
1008 * Check the hook exists and is Uninitialised.
1009 * Install the service matching string.
1010 * Store the originator of this message so we can send
1011 * a success or fail message to them later.
1012 * Move the hook to 'LISTENING'
1014 neg->service.hdr.tag_type = PTT_SRV_NAME;
1015 neg->service.hdr.tag_len =
1016 htons((uint16_t)ourmsg->data_len);
1018 if (ourmsg->data_len)
1019 bcopy(ourmsg->data, neg->service.data,
1021 neg->service_len = ourmsg->data_len;
1022 neg->pkt->pkt_header.ph.code = PADT_CODE;
1024 * Wait for PADI packet coming from Ethernet.
1026 sp->state = PPPOE_LISTENING;
1027 LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
1029 case NGM_PPPOE_OFFER:
1031 * Check the hook exists and is Uninitialised.
1032 * Store the originator of this message so we can send
1033 * a success of fail message to them later.
1034 * Store the AC-Name given and go to PRIMED.
1036 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
1037 neg->ac_name.hdr.tag_len =
1038 htons((uint16_t)ourmsg->data_len);
1039 if (ourmsg->data_len)
1040 bcopy(ourmsg->data, neg->ac_name.data,
1042 neg->ac_name_len = ourmsg->data_len;
1043 neg->pkt->pkt_header.ph.code = PADO_CODE;
1045 * Wait for PADI packet coming from hook.
1047 sp->state = PPPOE_PRIMED;
1049 case NGM_PPPOE_SERVICE:
1051 * Check the session is primed.
1052 * for now just allow ONE service to be advertised.
1053 * If you do it twice you just overwrite.
1055 if (sp->state != PPPOE_PRIMED) {
1056 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
1057 "primed\n", node->nd_ID);
1061 neg->service.hdr.tag_type = PTT_SRV_NAME;
1062 neg->service.hdr.tag_len =
1063 htons((uint16_t)ourmsg->data_len);
1065 if (ourmsg->data_len)
1066 bcopy(ourmsg->data, neg->service.data,
1068 neg->service_len = ourmsg->data_len;
1070 case NGM_PPPOE_SETMODE:
1075 if (msg->header.arglen == 0)
1078 s = (char *)msg->data;
1079 len = msg->header.arglen - 1;
1081 /* Search for matching mode string. */
1082 if (len == strlen(NG_PPPOE_STANDARD) &&
1083 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
1085 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1088 if (len == strlen(NG_PPPOE_3COM) &&
1089 (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
1090 privp->flags |= COMPAT_3COM;
1091 privp->eh.ether_type =
1092 ETHERTYPE_PPPOE_3COM_DISC;
1095 if (len == strlen(NG_PPPOE_DLINK) &&
1096 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
1097 privp->flags |= COMPAT_DLINK;
1103 case NGM_PPPOE_GETMODE:
1108 if (privp->flags == 0)
1109 len += strlen(NG_PPPOE_STANDARD) + 1;
1110 if (privp->flags & COMPAT_3COM)
1111 len += strlen(NG_PPPOE_3COM) + 1;
1112 if (privp->flags & COMPAT_DLINK)
1113 len += strlen(NG_PPPOE_DLINK) + 1;
1115 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
1119 s = (char *)resp->data;
1120 if (privp->flags == 0) {
1121 len = strlen(NG_PPPOE_STANDARD);
1122 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
1125 if (privp->flags & COMPAT_3COM) {
1126 len = strlen(NG_PPPOE_3COM);
1127 strlcpy(s, NG_PPPOE_3COM, len + 1);
1130 if (privp->flags & COMPAT_DLINK) {
1131 if (s != resp->data)
1133 len = strlen(NG_PPPOE_DLINK);
1134 strlcpy(s, NG_PPPOE_DLINK, len + 1);
1138 case NGM_PPPOE_SETENADDR:
1139 if (msg->header.arglen != ETHER_ADDR_LEN)
1141 bcopy(msg->data, &privp->eh.ether_shost,
1144 case NGM_PPPOE_SETMAXP:
1145 if (msg->header.arglen != sizeof(uint16_t))
1147 privp->max_payload.hdr.tag_type = PTT_MAX_PAYL;
1148 privp->max_payload.hdr.tag_len = htons(sizeof(uint16_t));
1149 privp->max_payload.data = htons(*((uint16_t *)msg->data));
1151 case NGM_PPPOE_SEND_HURL:
1155 /* Generate a packet of that type. */
1156 m = m_gethdr(M_NOWAIT, MT_DATA);
1158 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1159 "mbufs\n", node->nd_ID);
1161 struct pppoe_full_hdr *wh;
1162 struct pppoe_tag *tag;
1165 wh = mtod(m, struct pppoe_full_hdr *);
1166 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1168 /* Revert the stored header to DISC/PADM mode. */
1169 wh->ph.code = PADM_CODE;
1171 * Configure ethertype depending on what
1172 * was used during sessions stage.
1174 if (wh->eh.ether_type ==
1175 ETHERTYPE_PPPOE_3COM_SESS)
1176 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1178 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1180 * Add PADM message and adjust sizes.
1182 tag = (void *)(&wh->ph + 1);
1183 tag->tag_type = PTT_HURL;
1184 tag->tag_len = htons(ourmsg->data_len);
1185 strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
1186 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
1188 wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
1189 NET_EPOCH_ENTER(et);
1190 NG_SEND_DATA_ONLY(error,
1191 privp->ethernet_hook, m);
1196 case NGM_PPPOE_SEND_MOTM:
1200 /* Generate a packet of that type. */
1201 m = m_gethdr(M_NOWAIT, MT_DATA);
1203 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1204 "mbufs\n", node->nd_ID);
1206 struct pppoe_full_hdr *wh;
1207 struct pppoe_tag *tag;
1210 wh = mtod(m, struct pppoe_full_hdr *);
1211 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1213 /* Revert the stored header to DISC/PADM mode. */
1214 wh->ph.code = PADM_CODE;
1216 * Configure ethertype depending on what
1217 * was used during sessions stage.
1219 if (wh->eh.ether_type ==
1220 ETHERTYPE_PPPOE_3COM_SESS)
1221 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1223 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1225 * Add PADM message and adjust sizes.
1227 tag = (void *)(&wh->ph + 1);
1228 tag->tag_type = PTT_MOTM;
1229 tag->tag_len = htons(ourmsg->data_len);
1230 strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
1231 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
1233 wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
1234 NET_EPOCH_ENTER(et);
1235 NG_SEND_DATA_ONLY(error,
1236 privp->ethernet_hook, m);
1245 case NGM_ETHER_COOKIE:
1246 if (!(msg->header.flags & NGF_RESP))
1248 switch (msg->header.cmd) {
1249 case NGM_ETHER_GET_ENADDR:
1250 if (msg->header.arglen != ETHER_ADDR_LEN)
1252 bcopy(msg->data, &privp->eh.ether_shost,
1263 /* Take care of synchronous response, if any. */
1265 CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1266 NG_RESPOND_MSG(error, node, item, resp);
1267 /* Free the message and return. */
1273 * Start a client into the first state. A separate function because
1274 * it can be needed if the negotiation times out.
1277 pppoe_start(sessp sp)
1279 hook_p hook = sp->hook;
1280 node_p node = NG_HOOK_NODE(hook);
1281 priv_p privp = NG_NODE_PRIVATE(node);
1287 * Kick the state machine into starting up.
1289 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1290 sp->state = PPPOE_SINIT;
1292 * Reset the packet header to broadcast. Since we are
1293 * in a client mode use configured ethertype.
1295 memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1296 sizeof(struct ether_header));
1297 neg->pkt->pkt_header.ph.code = PADI_CODE;
1299 insert_tag(sp, &neg->host_uniq.hdr);
1300 insert_tag(sp, &neg->service.hdr);
1301 if (privp->max_payload.data != 0)
1302 insert_tag(sp, &privp->max_payload.hdr);
1305 * Send packet and prepare to retransmit it after timeout.
1307 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1308 pppoe_ticker, NULL, 0);
1309 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1310 m0 = m_copypacket(neg->m, M_NOWAIT);
1311 NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
1315 send_acname(sessp sp, const struct pppoe_tag *tag)
1318 struct ng_mesg *msg;
1319 struct ngpppoe_sts *sts;
1321 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1323 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1324 sizeof(struct ngpppoe_sts), M_NOWAIT);
1328 sts = (struct ngpppoe_sts *)msg->data;
1329 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1330 strncpy(sts->hook, (const char *)(tag + 1), tlen);
1331 sts->hook[tlen] = '\0';
1332 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1338 send_sessionid(sessp sp)
1341 struct ng_mesg *msg;
1343 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1345 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1346 sizeof(uint16_t), M_NOWAIT);
1350 *(uint16_t *)msg->data = sp->Session_ID;
1351 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1357 send_maxp(sessp sp, const struct pppoe_tag *tag)
1360 struct ng_mesg *msg;
1361 struct ngpppoe_maxp *maxp;
1363 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1365 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SETMAXP,
1366 sizeof(struct ngpppoe_maxp), M_NOWAIT);
1370 maxp = (struct ngpppoe_maxp *)msg->data;
1371 strncpy(maxp->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1372 maxp->data = ntohs(((const struct maxptag *)tag)->data);
1373 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1379 send_hurl(sessp sp, const struct pppoe_tag *tag)
1382 struct ng_mesg *msg;
1383 struct ngpppoe_padm *padm;
1385 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1387 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_HURL,
1388 sizeof(struct ngpppoe_padm), M_NOWAIT);
1392 padm = (struct ngpppoe_padm *)msg->data;
1393 tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
1394 strncpy(padm->msg, (const char *)(tag + 1), tlen);
1395 padm->msg[tlen] = '\0';
1396 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1402 send_motm(sessp sp, const struct pppoe_tag *tag)
1405 struct ng_mesg *msg;
1406 struct ngpppoe_padm *padm;
1408 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1410 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_MOTM,
1411 sizeof(struct ngpppoe_padm), M_NOWAIT);
1415 padm = (struct ngpppoe_padm *)msg->data;
1416 tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
1417 strncpy(padm->msg, (const char *)(tag + 1), tlen);
1418 padm->msg[tlen] = '\0';
1419 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1425 * Receive data from session hook and do something with it.
1428 ng_pppoe_rcvdata(hook_p hook, item_p item)
1430 node_p node = NG_HOOK_NODE(hook);
1431 const priv_p privp = NG_NODE_PRIVATE(node);
1432 sessp sp = NG_HOOK_PRIVATE(hook);
1433 struct pppoe_full_hdr *wh;
1437 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1438 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1441 switch (sp->state) {
1442 case PPPOE_NEWCONNECTED:
1443 case PPPOE_CONNECTED: {
1445 * Remove PPP address and control fields, if any.
1446 * For example, ng_ppp(4) always sends LCP packets
1447 * with address and control fields as required by
1448 * generic PPP. PPPoE is an exception to the rule.
1450 if (m->m_pkthdr.len >= 2) {
1451 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1453 if (mtod(m, u_char *)[0] == 0xff &&
1454 mtod(m, u_char *)[1] == 0x03)
1458 if (V_ng_pppoe_lcp_pcp && m->m_pkthdr.len >= 2 &&
1459 m->m_len >= 2 && (m = m_pullup(m, 2)) &&
1460 mtod(m, uint16_t *)[0] == htons(PROT_LCP))
1461 EVL_APPLY_PRI(m, (uint8_t)(V_ng_pppoe_lcp_pcp & 0x7));
1464 * Bang in a pre-made header, and set the length up
1465 * to be correct. Then send it to the ethernet driver.
1467 M_PREPEND(m, sizeof(*wh), M_NOWAIT);
1471 wh = mtod(m, struct pppoe_full_hdr *);
1472 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1473 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1474 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1475 privp->packets_out++;
1478 case PPPOE_PRIMED: {
1480 struct pppoe_tag hdr;
1483 const struct pppoe_tag *tag;
1485 const struct pppoe_hdr *ph;
1492 * A PADI packet is being returned by the application
1493 * that has set up this hook. This indicates that it
1494 * wants us to offer service.
1496 if (m->m_len < sizeof(*wh)) {
1497 m = m_pullup(m, sizeof(*wh));
1501 wh = mtod(m, struct pppoe_full_hdr *);
1503 session = ntohs(wh->ph.sid);
1504 length = ntohs(wh->ph.length);
1506 /* Use peers mode in session. */
1507 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1508 if (code != PADI_CODE)
1510 ng_uncallout(&neg->handle, node);
1513 * This is the first time we hear
1514 * from the client, so note it's
1515 * unicast address, replacing the
1516 * broadcast address.
1518 bcopy(wh->eh.ether_shost,
1519 neg->pkt->pkt_header.eh.ether_dhost,
1521 sp->state = PPPOE_SOFFER;
1523 neg->pkt->pkt_header.ph.code = PADO_CODE;
1526 * Start working out the tags to respond with.
1528 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1529 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1530 uniqtag.data.pointer = sp;
1532 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1533 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1534 insert_tag(sp, tag); /* return service */
1536 * If we have a NULL service request
1537 * and have an extra service defined in this hook,
1538 * then also add a tag for the extra service.
1539 * XXX this is a hack. eventually we should be able
1540 * to support advertising many services, not just one
1542 if (((tag == NULL) || (tag->tag_len == 0)) &&
1543 (neg->service.hdr.tag_len != 0)) {
1544 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1546 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1547 insert_tag(sp, tag); /* returned hostunique */
1548 insert_tag(sp, &uniqtag.hdr);
1552 * Send the offer but if they don't respond
1553 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1555 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1556 pppoe_ticker, NULL, 0);
1557 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1558 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1559 privp->packets_out++;
1564 * Packets coming from the hook make no sense
1565 * to sessions in the rest of states. Throw them away.
1578 * Receive data from ether and do something with it.
1581 ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1583 node_p node = NG_HOOK_NODE(hook);
1584 const priv_p privp = NG_NODE_PRIVATE(node);
1586 const struct pppoe_tag *utag = NULL, *tag = NULL;
1587 const struct pppoe_tag sntag = { PTT_SRV_NAME, 0 };
1588 const struct pppoe_full_hdr *wh;
1589 const struct pppoe_hdr *ph;
1599 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1600 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1604 * Dig out various fields from the packet.
1605 * Use them to decide where to send it.
1607 privp->packets_in++;
1608 if( m->m_len < sizeof(*wh)) {
1609 m = m_pullup(m, sizeof(*wh)); /* Checks length */
1611 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1612 "m_pullup(wh)\n", node->nd_ID);
1616 wh = mtod(m, struct pppoe_full_hdr *);
1617 length = ntohs(wh->ph.length);
1618 switch(wh->eh.ether_type) {
1619 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1620 case ETHERTYPE_PPPOE_DISC:
1622 * We need to try to make sure that the tag area
1623 * is contiguous, or we could wander off the end
1624 * of a buffer and make a mess.
1625 * (Linux wouldn't have this problem).
1627 if (m->m_pkthdr.len <= MHLEN) {
1628 if( m->m_len < m->m_pkthdr.len) {
1629 m = m_pullup(m, m->m_pkthdr.len);
1631 log(LOG_NOTICE, "ng_pppoe[%x]: "
1632 "couldn't m_pullup(pkthdr)\n",
1638 if (m->m_len != m->m_pkthdr.len) {
1640 * It's not all in one piece.
1641 * We need to do extra work.
1642 * Put it into a cluster.
1645 n = m_dup(m, M_NOWAIT);
1649 /* just check we got a cluster */
1650 if (m->m_len != m->m_pkthdr.len) {
1656 log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1657 "fragmented\n", node->nd_ID);
1661 wh = mtod(m, struct pppoe_full_hdr *);
1662 length = ntohs(wh->ph.length);
1664 session = ntohs(wh->ph.sid);
1671 * Look for a hook with the required service and send
1672 * the ENTIRE packet up there. It should come back to
1673 * a new hook in PRIMED state. Look there for further
1676 tag = get_tag(ph, PTT_SRV_NAME);
1681 * First, try to match Service-Name against our
1682 * listening hooks. If no success and we are in D-Link
1683 * compat mode and Service-Name is empty, then we
1684 * broadcast the PADI to all listening hooks.
1686 sendhook = pppoe_match_svc(node, tag);
1687 if (sendhook != NULL)
1688 NG_FWD_NEW_DATA(error, item, sendhook, m);
1689 else if (privp->flags & COMPAT_DLINK &&
1690 ntohs(tag->tag_len) == 0)
1691 error = pppoe_broadcast_padi(node, m);
1693 error = ENETUNREACH;
1698 * Use the host_uniq tag to find the hook this is in
1699 * response to. Received #2, now send #3
1700 * For now simply accept the first we receive.
1702 utag = get_tag(ph, PTT_HOST_UNIQ);
1704 log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1705 "unique field\n", node->nd_ID);
1709 sendhook = pppoe_finduniq(node, utag);
1710 if (sendhook == NULL) {
1711 log(LOG_NOTICE, "ng_pppoe[%x]: no "
1712 "matching session\n", node->nd_ID);
1717 * Check the session is in the right state.
1718 * It needs to be in PPPOE_SINIT.
1720 sp = NG_HOOK_PRIVATE(sendhook);
1721 if (sp->state == PPPOE_SREQ ||
1722 sp->state == PPPOE_CONNECTED) {
1723 break; /* Multiple PADO is OK. */
1725 if (sp->state != PPPOE_SINIT) {
1726 log(LOG_NOTICE, "ng_pppoe[%x]: session "
1727 "in wrong state\n", node->nd_ID);
1731 /* If requested specific AC-name, check it. */
1732 if (neg->ac_name_len) {
1733 tag = get_tag(ph, PTT_AC_NAME);
1735 /* No PTT_AC_NAME in PADO */
1738 if (neg->ac_name_len != htons(tag->tag_len) ||
1739 strncmp(neg->ac_name.data,
1740 (const char *)(tag + 1),
1741 neg->ac_name_len) != 0) {
1745 sp->state = PPPOE_SREQ;
1746 ng_uncallout(&neg->handle, node);
1749 * This is the first time we hear
1750 * from the server, so note it's
1751 * unicast address, replacing the
1752 * broadcast address .
1754 bcopy(wh->eh.ether_shost,
1755 neg->pkt->pkt_header.eh.ether_dhost,
1758 neg->pkt->pkt_header.ph.code = PADR_CODE;
1760 insert_tag(sp, utag); /* Host Unique */
1761 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1762 insert_tag(sp, tag); /* return cookie */
1763 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1764 insert_tag(sp, tag); /* return it */
1765 send_acname(sp, tag);
1767 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1768 (privp->max_payload.data != 0))
1769 insert_tag(sp, tag); /* return it */
1770 insert_tag(sp, &neg->service.hdr); /* Service */
1773 sp->state = PPPOE_SREQ;
1774 ng_callout(&neg->handle, node, sp->hook,
1775 PPPOE_INITIAL_TIMEOUT * hz,
1776 pppoe_ticker, NULL, 0);
1777 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1778 m0 = m_copypacket(neg->m, M_NOWAIT);
1779 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1784 * Use the ac_cookie tag to find the
1785 * hook this is in response to.
1787 utag = get_tag(ph, PTT_AC_COOKIE);
1788 if ((utag == NULL) ||
1789 (ntohs(utag->tag_len) != sizeof(sp))) {
1793 sendhook = pppoe_findcookie(node, utag);
1794 if (sendhook == NULL)
1798 * Check the session is in the right state.
1799 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1800 * If the latter, then this is a retry by the client,
1801 * so be nice, and resend.
1803 sp = NG_HOOK_PRIVATE(sendhook);
1804 if (sp->state == PPPOE_NEWCONNECTED) {
1806 * Whoa! drop back to resend that PADS packet.
1807 * We should still have a copy of it.
1809 sp->state = PPPOE_SOFFER;
1810 } else if (sp->state != PPPOE_SOFFER)
1811 LEAVE (ENETUNREACH);
1813 ng_uncallout(&neg->handle, node);
1814 neg->pkt->pkt_header.ph.code = PADS_CODE;
1815 if (sp->Session_ID == 0) {
1816 neg->pkt->pkt_header.ph.sid =
1817 htons(pppoe_getnewsession(sp));
1822 * start working out the tags to respond with.
1825 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1826 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1827 insert_tag(sp, tag);/* return service */
1828 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1829 insert_tag(sp, tag); /* return it */
1830 insert_tag(sp, utag); /* ac_cookie */
1833 sp->state = PPPOE_NEWCONNECTED;
1835 /* Send the PADS without a timeout - we're now connected. */
1836 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1837 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1840 * Having sent the last Negotiation header,
1841 * Set up the stored packet header to be correct for
1842 * the actual session. But keep the negotialtion stuff
1843 * around in case we need to resend this last packet.
1844 * We'll discard it when we move from NEWCONNECTED
1847 sp->pkt_hdr = neg->pkt->pkt_header;
1848 /* Configure ethertype depending on what
1849 * ethertype was used at discovery phase */
1850 if (sp->pkt_hdr.eh.ether_type ==
1851 ETHERTYPE_PPPOE_3COM_DISC)
1852 sp->pkt_hdr.eh.ether_type
1853 = ETHERTYPE_PPPOE_3COM_SESS;
1855 sp->pkt_hdr.eh.ether_type
1856 = ETHERTYPE_PPPOE_SESS;
1857 sp->pkt_hdr.ph.code = 0;
1858 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1863 * Use the host_uniq tag to find the hook this is in
1864 * response to. Take the session ID and store it away.
1865 * Also make sure the pre-made header is correct and
1866 * set us into Session mode.
1868 utag = get_tag(ph, PTT_HOST_UNIQ);
1870 LEAVE (ENETUNREACH);
1872 sendhook = pppoe_finduniq(node, utag);
1873 if (sendhook == NULL)
1877 * Check the session is in the right state.
1878 * It needs to be in PPPOE_SREQ.
1880 sp = NG_HOOK_PRIVATE(sendhook);
1881 if (sp->state != PPPOE_SREQ)
1884 ng_uncallout(&neg->handle, node);
1885 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1886 sp->Session_ID = ntohs(wh->ph.sid);
1887 pppoe_addsession(sp);
1890 sp->state = PPPOE_CONNECTED;
1892 * Now we have gone to Connected mode,
1893 * Free all resources needed for negotiation.
1894 * Keep a copy of the header we will be using.
1896 sp->pkt_hdr = neg->pkt->pkt_header;
1897 if (privp->flags & COMPAT_3COM)
1898 sp->pkt_hdr.eh.ether_type
1899 = ETHERTYPE_PPPOE_3COM_SESS;
1901 sp->pkt_hdr.eh.ether_type
1902 = ETHERTYPE_PPPOE_SESS;
1903 sp->pkt_hdr.ph.code = 0;
1905 free(sp->neg, M_NETGRAPH_PPPOE);
1907 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1908 (privp->max_payload.data != 0))
1910 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1914 * Find matching peer/session combination.
1916 sp = pppoe_findsession(privp, wh);
1919 /* Disconnect that hook. */
1920 ng_rmhook_self(sp->hook);
1925 * find matching peer/session combination.
1927 sp = pppoe_findsession(privp, wh);
1929 LEAVE (ENETUNREACH);
1930 if ((tag = get_tag(ph, PTT_HURL)))
1932 if ((tag = get_tag(ph, PTT_MOTM)))
1936 LEAVE(EPFNOSUPPORT);
1939 case ETHERTYPE_PPPOE_3COM_SESS:
1940 case ETHERTYPE_PPPOE_SESS:
1942 * Find matching peer/session combination.
1944 sp = pppoe_findsession(privp, wh);
1946 LEAVE (ENETUNREACH);
1947 m_adj(m, sizeof(*wh));
1949 /* If packet too short, dump it. */
1950 if (m->m_pkthdr.len < length)
1952 /* Also need to trim excess at the end */
1953 if (m->m_pkthdr.len > length) {
1954 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1956 if ( sp->state != PPPOE_CONNECTED) {
1957 if (sp->state == PPPOE_NEWCONNECTED) {
1958 sp->state = PPPOE_CONNECTED;
1960 * Now we have gone to Connected mode,
1961 * Free all resources needed for negotiation.
1962 * Be paranoid about whether there may be
1965 m_freem(sp->neg->m);
1966 ng_uncallout(&sp->neg->handle, node);
1967 free(sp->neg, M_NETGRAPH_PPPOE);
1970 LEAVE (ENETUNREACH);
1973 NG_FWD_NEW_DATA(error, item, sp->hook, m);
1976 LEAVE(EPFNOSUPPORT);
1986 * Receive data from debug hook and bypass it to ether.
1989 ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1991 node_p node = NG_HOOK_NODE(hook);
1992 const priv_p privp = NG_NODE_PRIVATE(node);
1995 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1996 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1998 NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1999 privp->packets_out++;
2004 * Do local shutdown processing..
2005 * If we are a persistent device, we might refuse to go away, and
2006 * we'd only remove our links and reset ourself.
2009 ng_pppoe_shutdown(node_p node)
2011 const priv_p privp = NG_NODE_PRIVATE(node);
2014 for (i = 0; i < SESSHASHSIZE; i++)
2015 mtx_destroy(&privp->sesshash[i].mtx);
2016 NG_NODE_SET_PRIVATE(node, NULL);
2017 NG_NODE_UNREF(privp->node);
2018 free(privp, M_NETGRAPH_PPPOE);
2023 * Hook disconnection
2025 * Clean up all dangling links and information about the session/hook.
2026 * For this type, removal of the last link destroys the node.
2029 ng_pppoe_disconnect(hook_p hook)
2031 node_p node = NG_HOOK_NODE(hook);
2032 priv_p privp = NG_NODE_PRIVATE(node);
2035 if (hook == privp->debug_hook) {
2036 privp->debug_hook = NULL;
2037 } else if (hook == privp->ethernet_hook) {
2038 privp->ethernet_hook = NULL;
2039 if (NG_NODE_IS_VALID(node))
2040 ng_rmnode_self(node);
2042 sp = NG_HOOK_PRIVATE(hook);
2043 if (sp->state != PPPOE_SNONE ) {
2044 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
2047 * According to the spec, if we are connected,
2048 * we should send a DISC packet if we are shutting down
2051 if ((privp->ethernet_hook)
2052 && ((sp->state == PPPOE_CONNECTED)
2053 || (sp->state == PPPOE_NEWCONNECTED))) {
2056 /* Generate a packet of that type. */
2057 m = m_gethdr(M_NOWAIT, MT_DATA);
2059 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
2060 "mbufs\n", node->nd_ID);
2062 struct epoch_tracker et;
2063 struct pppoe_full_hdr *wh;
2064 struct pppoe_tag *tag;
2065 int msglen = strlen(SIGNOFF);
2068 wh = mtod(m, struct pppoe_full_hdr *);
2069 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
2071 /* Revert the stored header to DISC/PADT mode. */
2072 wh->ph.code = PADT_CODE;
2074 * Configure ethertype depending on what
2075 * was used during sessions stage.
2077 if (wh->eh.ether_type ==
2078 ETHERTYPE_PPPOE_3COM_SESS)
2079 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
2081 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
2083 * Add a General error message and adjust
2086 tag = (void *)(&wh->ph + 1);
2087 tag->tag_type = PTT_GEN_ERR;
2088 tag->tag_len = htons((u_int16_t)msglen);
2089 strncpy((char *)(tag + 1), SIGNOFF, msglen);
2090 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
2092 wh->ph.length = htons(sizeof(*tag) + msglen);
2094 NET_EPOCH_ENTER(et);
2095 NG_SEND_DATA_ONLY(error,
2096 privp->ethernet_hook, m);
2100 if (sp->state == PPPOE_LISTENING)
2101 LIST_REMOVE(sp, sessions);
2102 else if (sp->Session_ID)
2103 pppoe_delsession(sp);
2105 * As long as we have somewhere to store the timeout handle,
2106 * we may have a timeout pending.. get rid of it.
2109 ng_uncallout(&sp->neg->handle, node);
2111 m_freem(sp->neg->m);
2112 free(sp->neg, M_NETGRAPH_PPPOE);
2114 free(sp, M_NETGRAPH_PPPOE);
2115 NG_HOOK_SET_PRIVATE(hook, NULL);
2117 if ((NG_NODE_NUMHOOKS(node) == 0) &&
2118 (NG_NODE_IS_VALID(node)))
2119 ng_rmnode_self(node);
2124 * Timeouts come here.
2127 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
2129 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
2130 sessp sp = NG_HOOK_PRIVATE(hook);
2132 struct mbuf *m0 = NULL;
2135 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
2136 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
2139 * Resend the last packet, using an exponential backoff.
2140 * After a period of time, stop growing the backoff,
2141 * And either leave it, or revert to the start.
2145 /* Timeouts on these produce resends. */
2146 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
2147 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
2148 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
2149 pppoe_ticker, NULL, 0);
2150 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
2151 if (sp->state == PPPOE_SREQ) {
2152 /* Revert to SINIT mode. */
2155 neg->timeout = PPPOE_TIMEOUT_LIMIT;
2161 /* A timeout on these says "give up" */
2162 ng_rmhook_self(hook);
2165 /* Timeouts have no meaning in other states. */
2166 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
2172 * Parse an incoming packet to see if any tags should be copied to the
2173 * output packet. Don't do any tags that have been handled in the main
2176 static const struct pppoe_tag*
2177 scan_tags(sessp sp, const struct pppoe_hdr* ph)
2179 const char *const end = (const char *)next_tag(ph);
2181 const struct pppoe_tag *pt = (const void *)(ph + 1);
2184 * Keep processing tags while a tag header will still fit.
2186 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
2188 while((const char*)(pt + 1) <= end) {
2190 * If the tag data would go past the end of the packet, abort.
2192 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
2196 switch (pt->tag_type) {
2215 pt = (const struct pppoe_tag*)ptn;
2221 pppoe_send_event(sessp sp, enum cmd cmdid)
2224 struct ng_mesg *msg;
2225 struct ngpppoe_sts *sts;
2227 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
2229 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
2230 sizeof(struct ngpppoe_sts), M_NOWAIT);
2233 sts = (struct ngpppoe_sts *)msg->data;
2234 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
2235 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);