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 static MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
63 #define M_NETGRAPH_PPPOE M_NETGRAPH
66 #define SIGNOFF "session closed"
69 * This section contains the netgraph method declarations for the
70 * pppoe node. These methods define the netgraph pppoe 'type'.
73 static ng_constructor_t ng_pppoe_constructor;
74 static ng_rcvmsg_t ng_pppoe_rcvmsg;
75 static ng_shutdown_t ng_pppoe_shutdown;
76 static ng_newhook_t ng_pppoe_newhook;
77 static ng_connect_t ng_pppoe_connect;
78 static ng_rcvdata_t ng_pppoe_rcvdata;
79 static ng_rcvdata_t ng_pppoe_rcvdata_ether;
80 static ng_rcvdata_t ng_pppoe_rcvdata_debug;
81 static ng_disconnect_t ng_pppoe_disconnect;
83 /* Parse type for struct ngpppoe_init_data */
84 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
85 = NG_PPPOE_INIT_DATA_TYPE_INFO;
86 static const struct ng_parse_type ngpppoe_init_data_state_type = {
87 &ng_parse_struct_type,
88 &ngpppoe_init_data_type_fields
91 /* Parse type for struct ngpppoe_sts */
92 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
93 = NG_PPPOE_STS_TYPE_INFO;
94 static const struct ng_parse_type ng_pppoe_sts_state_type = {
95 &ng_parse_struct_type,
96 &ng_pppoe_sts_type_fields
99 /* List of commands and how to convert arguments to/from ASCII */
100 static const struct ng_cmdlist ng_pppoe_cmds[] = {
105 &ngpppoe_init_data_state_type,
112 &ngpppoe_init_data_state_type,
119 &ngpppoe_init_data_state_type,
126 &ngpppoe_init_data_state_type,
133 &ng_pppoe_sts_state_type,
140 &ng_pppoe_sts_state_type,
147 &ng_pppoe_sts_state_type,
154 &ng_parse_string_type,
162 &ng_parse_string_type
168 &ng_parse_enaddr_type,
175 &ng_parse_uint16_type,
181 /* Netgraph node type descriptor */
182 static struct ng_type typestruct = {
183 .version = NG_ABI_VERSION,
184 .name = NG_PPPOE_NODE_TYPE,
185 .constructor = ng_pppoe_constructor,
186 .rcvmsg = ng_pppoe_rcvmsg,
187 .shutdown = ng_pppoe_shutdown,
188 .newhook = ng_pppoe_newhook,
189 .connect = ng_pppoe_connect,
190 .rcvdata = ng_pppoe_rcvdata,
191 .disconnect = ng_pppoe_disconnect,
192 .cmdlist = ng_pppoe_cmds,
194 NETGRAPH_INIT(pppoe, &typestruct);
197 * States for the session state machine.
198 * These have no meaning if there is no hook attached yet.
201 PPPOE_SNONE=0, /* [both] Initial state */
202 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
203 PPPOE_SINIT, /* [Client] Sent discovery initiation */
204 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
205 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
206 PPPOE_SREQ, /* [Client] Sent a Request */
207 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
208 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
209 PPPOE_DEAD /* [Both] */
212 #define NUMTAGS 20 /* number of tags we are set up to work with */
215 * Information we store for each hook on each node for negotiating the
216 * session. The mbuf and cluster are freed once negotiation has completed.
217 * The whole negotiation block is then discarded.
221 struct mbuf *m; /* holds cluster with last sent packet */
222 union packet *pkt; /* points within the above cluster */
223 struct callout handle; /* see timeout(9) */
224 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
226 const struct pppoe_tag *tags[NUMTAGS];
230 struct datatag service;
231 struct datatag ac_name;
233 typedef struct sess_neg *negp;
236 * Session information that is needed after connection.
242 ng_ID_t creator; /* who to notify */
243 struct pppoe_full_hdr pkt_hdr; /* used when connected */
244 negp neg; /* used when negotiating */
245 LIST_ENTRY(sess_con) sessions;
247 typedef struct sess_con *sessp;
249 #define SESSHASHSIZE 0x0100
250 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
252 struct sess_hash_entry {
254 LIST_HEAD(hhead, sess_con) head;
258 * Information we store for each node
261 node_p node; /* back pointer to node */
262 hook_p ethernet_hook;
264 u_int packets_in; /* packets in from ethernet */
265 u_int packets_out; /* packets out towards ethernet */
267 #define COMPAT_3COM 0x00000001
268 #define COMPAT_DLINK 0x00000002
269 struct ether_header eh;
270 LIST_HEAD(, sess_con) listeners;
271 struct sess_hash_entry sesshash[SESSHASHSIZE];
272 struct maxptag max_payload; /* PPP-Max-Payload (RFC4638) */
274 typedef struct PPPoE *priv_p;
277 char bytes[sizeof(void *)];
281 #define LEAVE(x) do { error = x; goto quit; } while(0)
282 static void pppoe_start(sessp sp);
283 static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
284 static const struct pppoe_tag *scan_tags(sessp sp,
285 const struct pppoe_hdr* ph);
286 static int pppoe_send_event(sessp sp, enum cmd cmdid);
288 /*************************************************************************
289 * Some basic utilities from the Linux version with author's permission.*
290 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
291 ************************************************************************/
296 * Return the location where the next tag can be put
298 static __inline const struct pppoe_tag*
299 next_tag(const struct pppoe_hdr* ph)
301 return (const struct pppoe_tag*)(((const char*)(ph + 1))
302 + ntohs(ph->length));
306 * Look for a tag of a specific type.
307 * Don't trust any length the other end says,
308 * but assume we already sanity checked ph->length.
310 static const struct pppoe_tag*
311 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
313 const char *const end = (const char *)next_tag(ph);
314 const struct pppoe_tag *pt = (const void *)(ph + 1);
318 * Keep processing tags while a tag header will still fit.
320 while((const char*)(pt + 1) <= end) {
322 * If the tag data would go past the end of the packet, abort.
324 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
326 CTR2(KTR_NET, "%20s: invalid length for tag %d",
330 if (pt->tag_type == idx) {
331 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
335 pt = (const struct pppoe_tag*)ptn;
338 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
342 /**************************************************************************
343 * Inlines to initialise or add tags to a session's tag list.
344 **************************************************************************/
346 * Initialise the session's tag list.
351 KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
352 sp->neg->numtags = 0;
356 insert_tag(sessp sp, const struct pppoe_tag *tp)
361 KASSERT(neg != NULL, ("%s: no neg", __func__));
362 if ((i = neg->numtags++) < NUMTAGS) {
365 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
372 * Make up a packet, using the tags filled out for the session.
374 * Assume that the actual pppoe header and ethernet header
375 * are filled out externally to this routine.
376 * Also assume that neg->wh points to the correct
377 * location at the front of the buffer space.
380 make_packet(sessp sp) {
381 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
382 const struct pppoe_tag **tag;
388 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
389 ("%s: called from wrong state", __func__));
390 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
392 dp = (char *)(&wh->ph + 1);
393 for (count = 0, tag = sp->neg->tags;
394 ((count < sp->neg->numtags) && (count < NUMTAGS));
396 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
397 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
398 log(LOG_NOTICE, "ng_pppoe: tags too long\n");
399 sp->neg->numtags = count;
400 break; /* XXX chop off what's too long */
402 bcopy(*tag, (char *)dp, tlen);
406 wh->ph.length = htons(length);
407 sp->neg->m->m_len = length + sizeof(*wh);
408 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
411 /**************************************************************************
412 * Routines to match a service. *
413 **************************************************************************/
416 * Find a hook that has a service string that matches that
417 * we are seeking. For now use a simple string.
418 * In the future we may need something like regexp().
420 * Null string is a wildcard (ANY service), according to RFC2516.
421 * And historical FreeBSD wildcard is also "*".
425 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
427 const priv_p privp = NG_NODE_PRIVATE(node);
430 LIST_FOREACH(sp, &privp->listeners, sessions) {
433 /* Empty Service-Name matches any service. */
434 if (neg->service_len == 0)
437 /* Special case for a blank or "*" service name (wildcard). */
438 if (neg->service_len == 1 && neg->service.data[0] == '*')
441 /* If the lengths don't match, that aint it. */
442 if (neg->service_len != ntohs(tag->tag_len))
445 if (strncmp((const char *)(tag + 1), neg->service.data,
446 ntohs(tag->tag_len)) == 0)
449 CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
450 sp?sp->hook:NULL, (const char *)(tag + 1));
452 return (sp?sp->hook:NULL);
456 * Broadcast the PADI packet in m0 to all listening hooks.
457 * This routine is called when a PADI with empty Service-Name
458 * tag is received. Client should receive PADOs with all
459 * available services.
462 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
464 const priv_p privp = NG_NODE_PRIVATE(node);
468 LIST_FOREACH(sp, &privp->listeners, sessions) {
471 m = m_dup(m0, M_NOWAIT);
474 NG_SEND_DATA_ONLY(error, sp->hook, m);
483 * Find a hook, which name equals to given service.
486 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
488 const priv_p privp = NG_NODE_PRIVATE(node);
491 LIST_FOREACH(sp, &privp->listeners, sessions) {
494 if (neg->service_len == svc_len &&
495 strncmp(svc_name, neg->service.data, svc_len) == 0)
502 /**************************************************************************
503 * Routines to find a particular session that matches an incoming packet. *
504 **************************************************************************/
505 /* Find free session and add to hash. */
507 pppoe_getnewsession(sessp sp)
509 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
510 static uint16_t pppoe_sid = 1;
515 /* Atomicity is not needed here as value will be checked. */
517 /* Spec says 0xFFFF is reserved, also don't use 0x0000. */
518 if (val == 0xffff || val == 0x0000)
521 /* Check it isn't already in use. */
522 hash = SESSHASH(val);
523 mtx_lock(&privp->sesshash[hash].mtx);
524 LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
525 if (tsp->Session_ID == val)
529 sp->Session_ID = val;
530 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
532 mtx_unlock(&privp->sesshash[hash].mtx);
536 CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
541 /* Add specified session to hash. */
543 pppoe_addsession(sessp sp)
545 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
546 uint16_t hash = SESSHASH(sp->Session_ID);
548 mtx_lock(&privp->sesshash[hash].mtx);
549 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
550 mtx_unlock(&privp->sesshash[hash].mtx);
553 /* Delete specified session from hash. */
555 pppoe_delsession(sessp sp)
557 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
558 uint16_t hash = SESSHASH(sp->Session_ID);
560 mtx_lock(&privp->sesshash[hash].mtx);
561 LIST_REMOVE(sp, sessions);
562 mtx_unlock(&privp->sesshash[hash].mtx);
565 /* Find matching peer/session combination. */
567 pppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
569 uint16_t session = ntohs(wh->ph.sid);
570 uint16_t hash = SESSHASH(session);
573 mtx_lock(&privp->sesshash[hash].mtx);
574 LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
575 if (sp->Session_ID == session &&
576 bcmp(sp->pkt_hdr.eh.ether_dhost,
577 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
581 mtx_unlock(&privp->sesshash[hash].mtx);
582 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
589 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
594 bcopy(tag + 1, uniq.bytes, sizeof(void *));
595 /* Cycle through all known hooks. */
596 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
597 /* Skip any nonsession hook. */
598 if (NG_HOOK_PRIVATE(hook) == NULL)
600 if (uniq.pointer == NG_HOOK_PRIVATE(hook))
603 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
608 /**************************************************************************
609 * Start of Netgraph entrypoints. *
610 **************************************************************************/
613 * Allocate the private data structure and link it with node.
616 ng_pppoe_constructor(node_p node)
621 /* Initialize private descriptor. */
622 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_ZERO);
624 /* Link structs together; this counts as our one reference to *node. */
625 NG_NODE_SET_PRIVATE(node, privp);
628 /* Initialize to standard mode. */
629 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
630 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
632 LIST_INIT(&privp->listeners);
633 for (i = 0; i < SESSHASHSIZE; i++) {
634 mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
635 LIST_INIT(&privp->sesshash[i].head);
638 CTR3(KTR_NET, "%20s: created node [%x] (%p)",
639 __func__, node->nd_ID, node);
645 * Give our ok for a hook to be added...
646 * point the hook's private info to the hook structure.
648 * The following hook names are special:
649 * "ethernet": the hook that should be connected to a NIC.
650 * "debug": copies of data sent out here (when I write the code).
651 * All other hook names need only be unique. (the framework checks this).
654 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
656 const priv_p privp = NG_NODE_PRIVATE(node);
659 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
660 privp->ethernet_hook = hook;
661 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
662 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
663 privp->debug_hook = hook;
664 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
667 * Any other unique name is OK.
668 * The infrastructure has already checked that it's unique,
669 * so just allocate it and hook it in.
671 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
675 NG_HOOK_SET_PRIVATE(hook, sp);
678 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
679 __func__, node->nd_ID, node, name, hook);
685 * Hook has been added successfully. Request the MAC address of
686 * the underlying Ethernet node.
689 ng_pppoe_connect(hook_p hook)
691 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
695 if (hook != privp->ethernet_hook)
699 * If this is Ethernet hook, then request MAC address
700 * from our downstream.
702 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
707 * Our hook and peer hook have HK_INVALID flag set,
708 * so we can't use NG_SEND_MSG_HOOK() macro here.
710 NG_SEND_MSG_ID(error, privp->node, msg,
711 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
712 NG_NODE_ID(privp->node));
717 * Get a netgraph control message.
718 * Check it is one we understand. If needed, send a response.
719 * We sometimes save the address for an async action later.
720 * Always free the message.
723 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
725 priv_p privp = NG_NODE_PRIVATE(node);
726 struct ngpppoe_init_data *ourmsg = NULL;
727 struct ng_mesg *resp = NULL;
734 NGI_GET_MSG(item, msg);
735 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
736 __func__, node->nd_ID, node, msg->header.cmd,
737 msg->header.typecookie);
739 /* Deal with message according to cookie and command. */
740 switch (msg->header.typecookie) {
741 case NGM_PPPOE_COOKIE:
742 switch (msg->header.cmd) {
743 case NGM_PPPOE_CONNECT:
744 case NGM_PPPOE_LISTEN:
745 case NGM_PPPOE_OFFER:
746 case NGM_PPPOE_SERVICE:
747 ourmsg = (struct ngpppoe_init_data *)msg->data;
748 if (msg->header.arglen < sizeof(*ourmsg)) {
749 log(LOG_ERR, "ng_pppoe[%x]: init data too "
750 "small\n", node->nd_ID);
753 if (msg->header.arglen - sizeof(*ourmsg) >
754 PPPOE_SERVICE_NAME_SIZE) {
755 log(LOG_ERR, "ng_pppoe[%x]: service name "
756 "too big\n", node->nd_ID);
759 if (msg->header.arglen - sizeof(*ourmsg) <
761 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
762 "length, %d should be %zd\n", node->nd_ID,
764 msg->header.arglen - sizeof (*ourmsg));
768 /* Make sure strcmp will terminate safely. */
769 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
771 /* Find hook by name. */
772 hook = ng_findhook(node, ourmsg->hook);
776 sp = NG_HOOK_PRIVATE(hook);
780 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
782 * Ensure we aren't already listening for this
785 if (pppoe_find_svc(node, ourmsg->data,
786 ourmsg->data_len) != NULL)
791 * PPPOE_SERVICE advertisments are set up
792 * on sessions that are in PRIMED state.
794 if (msg->header.cmd == NGM_PPPOE_SERVICE)
797 if (sp->state != PPPOE_SNONE) {
798 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
799 "active\n", node->nd_ID);
804 * Set up prototype header.
806 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
812 neg->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
813 if (neg->m == NULL) {
814 free(neg, M_NETGRAPH_PPPOE);
817 neg->m->m_pkthdr.rcvif = NULL;
819 ng_callout_init(&neg->handle);
820 neg->m->m_len = sizeof(struct pppoe_full_hdr);
821 neg->pkt = mtod(neg->m, union packet*);
822 memcpy((void *)&neg->pkt->pkt_header.eh,
823 &privp->eh, sizeof(struct ether_header));
824 neg->pkt->pkt_header.ph.ver = 0x1;
825 neg->pkt->pkt_header.ph.type = 0x1;
826 neg->pkt->pkt_header.ph.sid = 0x0000;
829 sp->creator = NGI_RETADDR(item);
831 switch (msg->header.cmd) {
832 case NGM_PPPOE_GET_STATUS:
834 struct ngpppoestat *stats;
836 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
840 stats = (struct ngpppoestat *) resp->data;
841 stats->packets_in = privp->packets_in;
842 stats->packets_out = privp->packets_out;
845 case NGM_PPPOE_CONNECT:
848 * Check the hook exists and is Uninitialised.
849 * Send a PADI request, and start the timeout logic.
850 * Store the originator of this message so we can send
851 * a success of fail message to them later.
852 * Move the session to SINIT.
853 * Set up the session to the correct state and
856 int i, acnlen = 0, acnsep = 0, srvlen;
857 for (i = 0; i < ourmsg->data_len; i++) {
858 if (ourmsg->data[i] == '\\') {
864 srvlen = ourmsg->data_len - acnlen - acnsep;
866 bcopy(ourmsg->data, neg->ac_name.data, acnlen);
867 neg->ac_name_len = acnlen;
869 neg->service.hdr.tag_type = PTT_SRV_NAME;
870 neg->service.hdr.tag_len = htons((uint16_t)srvlen);
871 bcopy(ourmsg->data + acnlen + acnsep,
872 neg->service.data, srvlen);
873 neg->service_len = srvlen;
877 case NGM_PPPOE_LISTEN:
879 * Check the hook exists and is Uninitialised.
880 * Install the service matching string.
881 * Store the originator of this message so we can send
882 * a success of fail message to them later.
883 * Move the hook to 'LISTENING'
885 neg->service.hdr.tag_type = PTT_SRV_NAME;
886 neg->service.hdr.tag_len =
887 htons((uint16_t)ourmsg->data_len);
889 if (ourmsg->data_len)
890 bcopy(ourmsg->data, neg->service.data,
892 neg->service_len = ourmsg->data_len;
893 neg->pkt->pkt_header.ph.code = PADT_CODE;
895 * Wait for PADI packet coming from Ethernet.
897 sp->state = PPPOE_LISTENING;
898 LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
900 case NGM_PPPOE_OFFER:
902 * Check the hook exists and is Uninitialised.
903 * Store the originator of this message so we can send
904 * a success of fail message to them later.
905 * Store the AC-Name given and go to PRIMED.
907 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
908 neg->ac_name.hdr.tag_len =
909 htons((uint16_t)ourmsg->data_len);
910 if (ourmsg->data_len)
911 bcopy(ourmsg->data, neg->ac_name.data,
913 neg->ac_name_len = ourmsg->data_len;
914 neg->pkt->pkt_header.ph.code = PADO_CODE;
916 * Wait for PADI packet coming from hook.
918 sp->state = PPPOE_PRIMED;
920 case NGM_PPPOE_SERVICE:
922 * Check the session is primed.
923 * for now just allow ONE service to be advertised.
924 * If you do it twice you just overwrite.
926 if (sp->state != PPPOE_PRIMED) {
927 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
928 "primed\n", node->nd_ID);
932 neg->service.hdr.tag_type = PTT_SRV_NAME;
933 neg->service.hdr.tag_len =
934 htons((uint16_t)ourmsg->data_len);
936 if (ourmsg->data_len)
937 bcopy(ourmsg->data, neg->service.data,
939 neg->service_len = ourmsg->data_len;
941 case NGM_PPPOE_SETMODE:
946 if (msg->header.arglen == 0)
949 s = (char *)msg->data;
950 len = msg->header.arglen - 1;
952 /* Search for matching mode string. */
953 if (len == strlen(NG_PPPOE_STANDARD) &&
954 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
956 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
959 if (len == strlen(NG_PPPOE_3COM) &&
960 (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
961 privp->flags |= COMPAT_3COM;
962 privp->eh.ether_type =
963 ETHERTYPE_PPPOE_3COM_DISC;
966 if (len == strlen(NG_PPPOE_DLINK) &&
967 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
968 privp->flags |= COMPAT_DLINK;
974 case NGM_PPPOE_GETMODE:
979 if (privp->flags == 0)
980 len += strlen(NG_PPPOE_STANDARD) + 1;
981 if (privp->flags & COMPAT_3COM)
982 len += strlen(NG_PPPOE_3COM) + 1;
983 if (privp->flags & COMPAT_DLINK)
984 len += strlen(NG_PPPOE_DLINK) + 1;
986 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
990 s = (char *)resp->data;
991 if (privp->flags == 0) {
992 len = strlen(NG_PPPOE_STANDARD);
993 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
996 if (privp->flags & COMPAT_3COM) {
997 len = strlen(NG_PPPOE_3COM);
998 strlcpy(s, NG_PPPOE_3COM, len + 1);
1001 if (privp->flags & COMPAT_DLINK) {
1002 if (s != resp->data)
1004 len = strlen(NG_PPPOE_DLINK);
1005 strlcpy(s, NG_PPPOE_DLINK, len + 1);
1009 case NGM_PPPOE_SETENADDR:
1010 if (msg->header.arglen != ETHER_ADDR_LEN)
1012 bcopy(msg->data, &privp->eh.ether_shost,
1015 case NGM_PPPOE_SETMAXP:
1016 if (msg->header.arglen != sizeof(uint16_t))
1018 privp->max_payload.hdr.tag_type = PTT_MAX_PAYL;
1019 privp->max_payload.hdr.tag_len = htons(sizeof(uint16_t));
1020 privp->max_payload.data = htons(*((uint16_t *)msg->data));
1026 case NGM_ETHER_COOKIE:
1027 if (!(msg->header.flags & NGF_RESP))
1029 switch (msg->header.cmd) {
1030 case NGM_ETHER_GET_ENADDR:
1031 if (msg->header.arglen != ETHER_ADDR_LEN)
1033 bcopy(msg->data, &privp->eh.ether_shost,
1044 /* Take care of synchronous response, if any. */
1046 CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1047 NG_RESPOND_MSG(error, node, item, resp);
1048 /* Free the message and return. */
1054 * Start a client into the first state. A separate function because
1055 * it can be needed if the negotiation times out.
1058 pppoe_start(sessp sp)
1060 hook_p hook = sp->hook;
1061 node_p node = NG_HOOK_NODE(hook);
1062 priv_p privp = NG_NODE_PRIVATE(node);
1065 struct pppoe_tag hdr;
1072 * Kick the state machine into starting up.
1074 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1075 sp->state = PPPOE_SINIT;
1077 * Reset the packet header to broadcast. Since we are
1078 * in a client mode use configured ethertype.
1080 memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1081 sizeof(struct ether_header));
1082 neg->pkt->pkt_header.ph.code = PADI_CODE;
1083 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
1084 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
1085 uniqtag.data.pointer = sp;
1087 insert_tag(sp, &uniqtag.hdr);
1088 insert_tag(sp, &neg->service.hdr);
1089 if (privp->max_payload.data != 0)
1090 insert_tag(sp, &privp->max_payload.hdr);
1093 * Send packet and prepare to retransmit it after timeout.
1095 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1096 pppoe_ticker, NULL, 0);
1097 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1098 m0 = m_copypacket(neg->m, M_NOWAIT);
1099 NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
1103 send_acname(sessp sp, const struct pppoe_tag *tag)
1106 struct ng_mesg *msg;
1107 struct ngpppoe_sts *sts;
1109 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1111 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1112 sizeof(struct ngpppoe_sts), M_NOWAIT);
1116 sts = (struct ngpppoe_sts *)msg->data;
1117 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1118 strncpy(sts->hook, (const char *)(tag + 1), tlen);
1119 sts->hook[tlen] = '\0';
1120 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1126 send_sessionid(sessp sp)
1129 struct ng_mesg *msg;
1131 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1133 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1134 sizeof(uint16_t), M_NOWAIT);
1138 *(uint16_t *)msg->data = sp->Session_ID;
1139 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1145 send_maxp(sessp sp, const struct pppoe_tag *tag)
1148 struct ng_mesg *msg;
1149 struct ngpppoe_maxp *maxp;
1151 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1153 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SETMAXP,
1154 sizeof(struct ngpppoe_maxp), M_NOWAIT);
1158 maxp = (struct ngpppoe_maxp *)msg->data;
1159 strncpy(maxp->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1160 maxp->data = ntohs(((const struct maxptag *)tag)->data);
1161 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1167 * Receive data from session hook and do something with it.
1170 ng_pppoe_rcvdata(hook_p hook, item_p item)
1172 node_p node = NG_HOOK_NODE(hook);
1173 const priv_p privp = NG_NODE_PRIVATE(node);
1174 sessp sp = NG_HOOK_PRIVATE(hook);
1175 struct pppoe_full_hdr *wh;
1179 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1180 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1183 switch (sp->state) {
1184 case PPPOE_NEWCONNECTED:
1185 case PPPOE_CONNECTED: {
1187 * Remove PPP address and control fields, if any.
1188 * For example, ng_ppp(4) always sends LCP packets
1189 * with address and control fields as required by
1190 * generic PPP. PPPoE is an exception to the rule.
1192 if (m->m_pkthdr.len >= 2) {
1193 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1195 if (mtod(m, u_char *)[0] == 0xff &&
1196 mtod(m, u_char *)[1] == 0x03)
1200 * Bang in a pre-made header, and set the length up
1201 * to be correct. Then send it to the ethernet driver.
1203 M_PREPEND(m, sizeof(*wh), M_NOWAIT);
1207 wh = mtod(m, struct pppoe_full_hdr *);
1208 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1209 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1210 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1211 privp->packets_out++;
1214 case PPPOE_PRIMED: {
1216 struct pppoe_tag hdr;
1219 const struct pppoe_tag *tag;
1221 const struct pppoe_hdr *ph;
1228 * A PADI packet is being returned by the application
1229 * that has set up this hook. This indicates that it
1230 * wants us to offer service.
1232 if (m->m_len < sizeof(*wh)) {
1233 m = m_pullup(m, sizeof(*wh));
1237 wh = mtod(m, struct pppoe_full_hdr *);
1239 session = ntohs(wh->ph.sid);
1240 length = ntohs(wh->ph.length);
1242 /* Use peers mode in session. */
1243 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1244 if (code != PADI_CODE)
1246 ng_uncallout(&neg->handle, node);
1249 * This is the first time we hear
1250 * from the client, so note it's
1251 * unicast address, replacing the
1252 * broadcast address.
1254 bcopy(wh->eh.ether_shost,
1255 neg->pkt->pkt_header.eh.ether_dhost,
1257 sp->state = PPPOE_SOFFER;
1259 neg->pkt->pkt_header.ph.code = PADO_CODE;
1262 * Start working out the tags to respond with.
1264 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1265 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1266 uniqtag.data.pointer = sp;
1268 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1269 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1270 insert_tag(sp, tag); /* return service */
1272 * If we have a NULL service request
1273 * and have an extra service defined in this hook,
1274 * then also add a tag for the extra service.
1275 * XXX this is a hack. eventually we should be able
1276 * to support advertising many services, not just one
1278 if (((tag == NULL) || (tag->tag_len == 0)) &&
1279 (neg->service.hdr.tag_len != 0)) {
1280 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1282 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1283 insert_tag(sp, tag); /* returned hostunique */
1284 insert_tag(sp, &uniqtag.hdr);
1288 * Send the offer but if they don't respond
1289 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1291 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1292 pppoe_ticker, NULL, 0);
1293 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1294 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1295 privp->packets_out++;
1300 * Packets coming from the hook make no sense
1301 * to sessions in the rest of states. Throw them away.
1314 * Receive data from ether and do something with it.
1317 ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1319 node_p node = NG_HOOK_NODE(hook);
1320 const priv_p privp = NG_NODE_PRIVATE(node);
1322 const struct pppoe_tag *utag = NULL, *tag = NULL;
1323 const struct pppoe_full_hdr *wh;
1324 const struct pppoe_hdr *ph;
1334 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1335 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1339 * Dig out various fields from the packet.
1340 * Use them to decide where to send it.
1342 privp->packets_in++;
1343 if( m->m_len < sizeof(*wh)) {
1344 m = m_pullup(m, sizeof(*wh)); /* Checks length */
1346 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1347 "m_pullup(wh)\n", node->nd_ID);
1351 wh = mtod(m, struct pppoe_full_hdr *);
1352 length = ntohs(wh->ph.length);
1353 switch(wh->eh.ether_type) {
1354 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1355 case ETHERTYPE_PPPOE_DISC:
1357 * We need to try to make sure that the tag area
1358 * is contiguous, or we could wander off the end
1359 * of a buffer and make a mess.
1360 * (Linux wouldn't have this problem).
1362 if (m->m_pkthdr.len <= MHLEN) {
1363 if( m->m_len < m->m_pkthdr.len) {
1364 m = m_pullup(m, m->m_pkthdr.len);
1366 log(LOG_NOTICE, "ng_pppoe[%x]: "
1367 "couldn't m_pullup(pkthdr)\n",
1373 if (m->m_len != m->m_pkthdr.len) {
1375 * It's not all in one piece.
1376 * We need to do extra work.
1377 * Put it into a cluster.
1380 n = m_dup(m, M_NOWAIT);
1384 /* just check we got a cluster */
1385 if (m->m_len != m->m_pkthdr.len) {
1391 log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1392 "fragmented\n", node->nd_ID);
1396 wh = mtod(m, struct pppoe_full_hdr *);
1397 length = ntohs(wh->ph.length);
1399 session = ntohs(wh->ph.sid);
1406 * Look for a hook with the required service and send
1407 * the ENTIRE packet up there. It should come back to
1408 * a new hook in PRIMED state. Look there for further
1411 tag = get_tag(ph, PTT_SRV_NAME);
1413 CTR1(KTR_NET, "%20s: PADI w/o Service-Name",
1419 * First, try to match Service-Name against our
1420 * listening hooks. If no success and we are in D-Link
1421 * compat mode and Service-Name is empty, then we
1422 * broadcast the PADI to all listening hooks.
1424 sendhook = pppoe_match_svc(node, tag);
1425 if (sendhook != NULL)
1426 NG_FWD_NEW_DATA(error, item, sendhook, m);
1427 else if (privp->flags & COMPAT_DLINK &&
1428 ntohs(tag->tag_len) == 0)
1429 error = pppoe_broadcast_padi(node, m);
1431 error = ENETUNREACH;
1436 * Use the host_uniq tag to find the hook this is in
1437 * response to. Received #2, now send #3
1438 * For now simply accept the first we receive.
1440 utag = get_tag(ph, PTT_HOST_UNIQ);
1441 if ((utag == NULL) ||
1442 (ntohs(utag->tag_len) != sizeof(sp))) {
1443 log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1444 "unique field\n", node->nd_ID);
1448 sendhook = pppoe_finduniq(node, utag);
1449 if (sendhook == NULL) {
1450 log(LOG_NOTICE, "ng_pppoe[%x]: no "
1451 "matching session\n", node->nd_ID);
1456 * Check the session is in the right state.
1457 * It needs to be in PPPOE_SINIT.
1459 sp = NG_HOOK_PRIVATE(sendhook);
1460 if (sp->state == PPPOE_SREQ ||
1461 sp->state == PPPOE_CONNECTED) {
1462 break; /* Multiple PADO is OK. */
1464 if (sp->state != PPPOE_SINIT) {
1465 log(LOG_NOTICE, "ng_pppoe[%x]: session "
1466 "in wrong state\n", node->nd_ID);
1470 /* If requested specific AC-name, check it. */
1471 if (neg->ac_name_len) {
1472 tag = get_tag(ph, PTT_AC_NAME);
1474 /* No PTT_AC_NAME in PADO */
1477 if (neg->ac_name_len != htons(tag->tag_len) ||
1478 strncmp(neg->ac_name.data,
1479 (const char *)(tag + 1),
1480 neg->ac_name_len) != 0) {
1484 sp->state = PPPOE_SREQ;
1485 ng_uncallout(&neg->handle, node);
1488 * This is the first time we hear
1489 * from the server, so note it's
1490 * unicast address, replacing the
1491 * broadcast address .
1493 bcopy(wh->eh.ether_shost,
1494 neg->pkt->pkt_header.eh.ether_dhost,
1497 neg->pkt->pkt_header.ph.code = PADR_CODE;
1499 insert_tag(sp, utag); /* Host Unique */
1500 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1501 insert_tag(sp, tag); /* return cookie */
1502 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1503 insert_tag(sp, tag); /* return it */
1504 send_acname(sp, tag);
1506 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1507 (privp->max_payload.data != 0))
1508 insert_tag(sp, tag); /* return it */
1509 insert_tag(sp, &neg->service.hdr); /* Service */
1512 sp->state = PPPOE_SREQ;
1513 ng_callout(&neg->handle, node, sp->hook,
1514 PPPOE_INITIAL_TIMEOUT * hz,
1515 pppoe_ticker, NULL, 0);
1516 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1517 m0 = m_copypacket(neg->m, M_NOWAIT);
1518 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1523 * Use the ac_cookie tag to find the
1524 * hook this is in response to.
1526 utag = get_tag(ph, PTT_AC_COOKIE);
1527 if ((utag == NULL) ||
1528 (ntohs(utag->tag_len) != sizeof(sp))) {
1532 sendhook = pppoe_finduniq(node, utag);
1533 if (sendhook == NULL)
1537 * Check the session is in the right state.
1538 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1539 * If the latter, then this is a retry by the client,
1540 * so be nice, and resend.
1542 sp = NG_HOOK_PRIVATE(sendhook);
1543 if (sp->state == PPPOE_NEWCONNECTED) {
1545 * Whoa! drop back to resend that PADS packet.
1546 * We should still have a copy of it.
1548 sp->state = PPPOE_SOFFER;
1549 } else if (sp->state != PPPOE_SOFFER)
1550 LEAVE (ENETUNREACH);
1552 ng_uncallout(&neg->handle, node);
1553 neg->pkt->pkt_header.ph.code = PADS_CODE;
1554 if (sp->Session_ID == 0) {
1555 neg->pkt->pkt_header.ph.sid =
1556 htons(pppoe_getnewsession(sp));
1561 * start working out the tags to respond with.
1564 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1565 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1566 insert_tag(sp, tag);/* return service */
1567 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1568 insert_tag(sp, tag); /* return it */
1569 insert_tag(sp, utag); /* ac_cookie */
1572 sp->state = PPPOE_NEWCONNECTED;
1574 /* Send the PADS without a timeout - we're now connected. */
1575 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1576 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1579 * Having sent the last Negotiation header,
1580 * Set up the stored packet header to be correct for
1581 * the actual session. But keep the negotialtion stuff
1582 * around in case we need to resend this last packet.
1583 * We'll discard it when we move from NEWCONNECTED
1586 sp->pkt_hdr = neg->pkt->pkt_header;
1587 /* Configure ethertype depending on what
1588 * ethertype was used at discovery phase */
1589 if (sp->pkt_hdr.eh.ether_type ==
1590 ETHERTYPE_PPPOE_3COM_DISC)
1591 sp->pkt_hdr.eh.ether_type
1592 = ETHERTYPE_PPPOE_3COM_SESS;
1594 sp->pkt_hdr.eh.ether_type
1595 = ETHERTYPE_PPPOE_SESS;
1596 sp->pkt_hdr.ph.code = 0;
1597 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1602 * Use the host_uniq tag to find the hook this is in
1603 * response to. Take the session ID and store it away.
1604 * Also make sure the pre-made header is correct and
1605 * set us into Session mode.
1607 utag = get_tag(ph, PTT_HOST_UNIQ);
1608 if ((utag == NULL) ||
1609 (ntohs(utag->tag_len) != sizeof(sp))) {
1610 LEAVE (ENETUNREACH);
1612 sendhook = pppoe_finduniq(node, utag);
1613 if (sendhook == NULL)
1617 * Check the session is in the right state.
1618 * It needs to be in PPPOE_SREQ.
1620 sp = NG_HOOK_PRIVATE(sendhook);
1621 if (sp->state != PPPOE_SREQ)
1624 ng_uncallout(&neg->handle, node);
1625 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1626 sp->Session_ID = ntohs(wh->ph.sid);
1627 pppoe_addsession(sp);
1630 sp->state = PPPOE_CONNECTED;
1632 * Now we have gone to Connected mode,
1633 * Free all resources needed for negotiation.
1634 * Keep a copy of the header we will be using.
1636 sp->pkt_hdr = neg->pkt->pkt_header;
1637 if (privp->flags & COMPAT_3COM)
1638 sp->pkt_hdr.eh.ether_type
1639 = ETHERTYPE_PPPOE_3COM_SESS;
1641 sp->pkt_hdr.eh.ether_type
1642 = ETHERTYPE_PPPOE_SESS;
1643 sp->pkt_hdr.ph.code = 0;
1645 free(sp->neg, M_NETGRAPH_PPPOE);
1647 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1648 (privp->max_payload.data != 0))
1650 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1654 * Find matching peer/session combination.
1656 sp = pppoe_findsession(privp, wh);
1659 /* Disconnect that hook. */
1660 ng_rmhook_self(sp->hook);
1663 LEAVE(EPFNOSUPPORT);
1666 case ETHERTYPE_PPPOE_3COM_SESS:
1667 case ETHERTYPE_PPPOE_SESS:
1669 * Find matching peer/session combination.
1671 sp = pppoe_findsession(privp, wh);
1673 LEAVE (ENETUNREACH);
1674 m_adj(m, sizeof(*wh));
1676 /* If packet too short, dump it. */
1677 if (m->m_pkthdr.len < length)
1679 /* Also need to trim excess at the end */
1680 if (m->m_pkthdr.len > length) {
1681 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1683 if ( sp->state != PPPOE_CONNECTED) {
1684 if (sp->state == PPPOE_NEWCONNECTED) {
1685 sp->state = PPPOE_CONNECTED;
1687 * Now we have gone to Connected mode,
1688 * Free all resources needed for negotiation.
1689 * Be paranoid about whether there may be
1692 m_freem(sp->neg->m);
1693 ng_uncallout(&sp->neg->handle, node);
1694 free(sp->neg, M_NETGRAPH_PPPOE);
1697 LEAVE (ENETUNREACH);
1700 NG_FWD_NEW_DATA(error, item, sp->hook, m);
1703 LEAVE(EPFNOSUPPORT);
1713 * Receive data from debug hook and bypass it to ether.
1716 ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1718 node_p node = NG_HOOK_NODE(hook);
1719 const priv_p privp = NG_NODE_PRIVATE(node);
1722 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1723 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1725 NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1726 privp->packets_out++;
1731 * Do local shutdown processing..
1732 * If we are a persistant device, we might refuse to go away, and
1733 * we'd only remove our links and reset ourself.
1736 ng_pppoe_shutdown(node_p node)
1738 const priv_p privp = NG_NODE_PRIVATE(node);
1741 for (i = 0; i < SESSHASHSIZE; i++)
1742 mtx_destroy(&privp->sesshash[i].mtx);
1743 NG_NODE_SET_PRIVATE(node, NULL);
1744 NG_NODE_UNREF(privp->node);
1745 free(privp, M_NETGRAPH_PPPOE);
1750 * Hook disconnection
1752 * Clean up all dangling links and information about the session/hook.
1753 * For this type, removal of the last link destroys the node.
1756 ng_pppoe_disconnect(hook_p hook)
1758 node_p node = NG_HOOK_NODE(hook);
1759 priv_p privp = NG_NODE_PRIVATE(node);
1762 if (hook == privp->debug_hook) {
1763 privp->debug_hook = NULL;
1764 } else if (hook == privp->ethernet_hook) {
1765 privp->ethernet_hook = NULL;
1766 if (NG_NODE_IS_VALID(node))
1767 ng_rmnode_self(node);
1769 sp = NG_HOOK_PRIVATE(hook);
1770 if (sp->state != PPPOE_SNONE ) {
1771 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1774 * According to the spec, if we are connected,
1775 * we should send a DISC packet if we are shutting down
1778 if ((privp->ethernet_hook)
1779 && ((sp->state == PPPOE_CONNECTED)
1780 || (sp->state == PPPOE_NEWCONNECTED))) {
1783 /* Generate a packet of that type. */
1784 MGETHDR(m, M_NOWAIT, MT_DATA);
1786 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1787 "mbufs\n", node->nd_ID);
1789 struct pppoe_full_hdr *wh;
1790 struct pppoe_tag *tag;
1791 int msglen = strlen(SIGNOFF);
1794 m->m_pkthdr.rcvif = NULL;
1795 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1796 wh = mtod(m, struct pppoe_full_hdr *);
1797 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1799 /* Revert the stored header to DISC/PADT mode. */
1800 wh->ph.code = PADT_CODE;
1802 * Configure ethertype depending on what
1803 * was used during sessions stage.
1805 if (wh->eh.ether_type ==
1806 ETHERTYPE_PPPOE_3COM_SESS)
1807 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1809 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1811 * Add a General error message and adjust
1814 tag = (void *)(&wh->ph + 1);
1815 tag->tag_type = PTT_GEN_ERR;
1816 tag->tag_len = htons((u_int16_t)msglen);
1817 strncpy((char *)(tag + 1), SIGNOFF, msglen);
1818 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1820 wh->ph.length = htons(sizeof(*tag) + msglen);
1821 NG_SEND_DATA_ONLY(error,
1822 privp->ethernet_hook, m);
1825 if (sp->state == PPPOE_LISTENING)
1826 LIST_REMOVE(sp, sessions);
1827 else if (sp->Session_ID)
1828 pppoe_delsession(sp);
1830 * As long as we have somewhere to store the timeout handle,
1831 * we may have a timeout pending.. get rid of it.
1834 ng_uncallout(&sp->neg->handle, node);
1836 m_freem(sp->neg->m);
1837 free(sp->neg, M_NETGRAPH_PPPOE);
1839 free(sp, M_NETGRAPH_PPPOE);
1840 NG_HOOK_SET_PRIVATE(hook, NULL);
1842 if ((NG_NODE_NUMHOOKS(node) == 0) &&
1843 (NG_NODE_IS_VALID(node)))
1844 ng_rmnode_self(node);
1849 * Timeouts come here.
1852 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
1854 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1855 sessp sp = NG_HOOK_PRIVATE(hook);
1857 struct mbuf *m0 = NULL;
1860 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1861 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
1864 * Resend the last packet, using an exponential backoff.
1865 * After a period of time, stop growing the backoff,
1866 * And either leave it, or revert to the start.
1870 /* Timeouts on these produce resends. */
1871 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1872 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1873 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1874 pppoe_ticker, NULL, 0);
1875 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1876 if (sp->state == PPPOE_SREQ) {
1877 /* Revert to SINIT mode. */
1880 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1886 /* A timeout on these says "give up" */
1887 ng_rmhook_self(hook);
1890 /* Timeouts have no meaning in other states. */
1891 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1897 * Parse an incoming packet to see if any tags should be copied to the
1898 * output packet. Don't do any tags that have been handled in the main
1901 static const struct pppoe_tag*
1902 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1904 const char *const end = (const char *)next_tag(ph);
1906 const struct pppoe_tag *pt = (const void *)(ph + 1);
1909 * Keep processing tags while a tag header will still fit.
1911 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1913 while((const char*)(pt + 1) <= end) {
1915 * If the tag data would go past the end of the packet, abort.
1917 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1921 switch (pt->tag_type) {
1938 pt = (const struct pppoe_tag*)ptn;
1944 pppoe_send_event(sessp sp, enum cmd cmdid)
1947 struct ng_mesg *msg;
1948 struct ngpppoe_sts *sts;
1950 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1952 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1953 sizeof(struct ngpppoe_sts), M_NOWAIT);
1956 sts = (struct ngpppoe_sts *)msg->data;
1957 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1958 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);