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/syslog.h>
53 #include <net/ethernet.h>
55 #include <netgraph/ng_message.h>
56 #include <netgraph/netgraph.h>
57 #include <netgraph/ng_parse.h>
58 #include <netgraph/ng_pppoe.h>
59 #include <netgraph/ng_ether.h>
61 #ifdef NG_SEPARATE_MALLOC
62 static MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
64 #define M_NETGRAPH_PPPOE M_NETGRAPH
67 #define SIGNOFF "session closed"
70 * This section contains the netgraph method declarations for the
71 * pppoe node. These methods define the netgraph pppoe 'type'.
74 static ng_constructor_t ng_pppoe_constructor;
75 static ng_rcvmsg_t ng_pppoe_rcvmsg;
76 static ng_shutdown_t ng_pppoe_shutdown;
77 static ng_newhook_t ng_pppoe_newhook;
78 static ng_connect_t ng_pppoe_connect;
79 static ng_rcvdata_t ng_pppoe_rcvdata;
80 static ng_rcvdata_t ng_pppoe_rcvdata_ether;
81 static ng_rcvdata_t ng_pppoe_rcvdata_debug;
82 static ng_disconnect_t ng_pppoe_disconnect;
84 /* Parse type for struct ngpppoe_init_data */
85 static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
86 = NG_PPPOE_INIT_DATA_TYPE_INFO;
87 static const struct ng_parse_type ngpppoe_init_data_state_type = {
88 &ng_parse_struct_type,
89 &ngpppoe_init_data_type_fields
92 /* Parse type for struct ngpppoe_sts */
93 static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
94 = NG_PPPOE_STS_TYPE_INFO;
95 static const struct ng_parse_type ng_pppoe_sts_state_type = {
96 &ng_parse_struct_type,
97 &ng_pppoe_sts_type_fields
100 /* List of commands and how to convert arguments to/from ASCII */
101 static const struct ng_cmdlist ng_pppoe_cmds[] = {
106 &ngpppoe_init_data_state_type,
113 &ngpppoe_init_data_state_type,
120 &ngpppoe_init_data_state_type,
127 &ngpppoe_init_data_state_type,
134 &ng_pppoe_sts_state_type,
141 &ng_pppoe_sts_state_type,
148 &ng_pppoe_sts_state_type,
155 &ng_parse_string_type,
163 &ng_parse_string_type
169 &ng_parse_enaddr_type,
176 &ng_parse_uint16_type,
183 &ngpppoe_init_data_state_type,
190 &ngpppoe_init_data_state_type,
196 /* Netgraph node type descriptor */
197 static struct ng_type typestruct = {
198 .version = NG_ABI_VERSION,
199 .name = NG_PPPOE_NODE_TYPE,
200 .constructor = ng_pppoe_constructor,
201 .rcvmsg = ng_pppoe_rcvmsg,
202 .shutdown = ng_pppoe_shutdown,
203 .newhook = ng_pppoe_newhook,
204 .connect = ng_pppoe_connect,
205 .rcvdata = ng_pppoe_rcvdata,
206 .disconnect = ng_pppoe_disconnect,
207 .cmdlist = ng_pppoe_cmds,
209 NETGRAPH_INIT(pppoe, &typestruct);
212 * States for the session state machine.
213 * These have no meaning if there is no hook attached yet.
216 PPPOE_SNONE=0, /* [both] Initial state */
217 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
218 PPPOE_SINIT, /* [Client] Sent discovery initiation */
219 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
220 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
221 PPPOE_SREQ, /* [Client] Sent a Request */
222 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
223 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
224 PPPOE_DEAD /* [Both] */
227 #define NUMTAGS 20 /* number of tags we are set up to work with */
230 * Information we store for each hook on each node for negotiating the
231 * session. The mbuf and cluster are freed once negotiation has completed.
232 * The whole negotiation block is then discarded.
236 struct mbuf *m; /* holds cluster with last sent packet */
237 union packet *pkt; /* points within the above cluster */
238 struct callout handle; /* see timeout(9) */
239 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
241 const struct pppoe_tag *tags[NUMTAGS];
246 struct datatag service;
247 struct datatag ac_name;
248 struct datatag host_uniq;
250 typedef struct sess_neg *negp;
253 * Session information that is needed after connection.
259 ng_ID_t creator; /* who to notify */
260 struct pppoe_full_hdr pkt_hdr; /* used when connected */
261 negp neg; /* used when negotiating */
262 LIST_ENTRY(sess_con) sessions;
264 typedef struct sess_con *sessp;
266 #define SESSHASHSIZE 0x0100
267 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
269 struct sess_hash_entry {
271 LIST_HEAD(hhead, sess_con) head;
275 * Information we store for each node
278 node_p node; /* back pointer to node */
279 hook_p ethernet_hook;
281 u_int packets_in; /* packets in from ethernet */
282 u_int packets_out; /* packets out towards ethernet */
284 #define COMPAT_3COM 0x00000001
285 #define COMPAT_DLINK 0x00000002
286 struct ether_header eh;
287 LIST_HEAD(, sess_con) listeners;
288 struct sess_hash_entry sesshash[SESSHASHSIZE];
289 struct maxptag max_payload; /* PPP-Max-Payload (RFC4638) */
291 typedef struct PPPoE *priv_p;
294 char bytes[sizeof(void *)];
298 #define LEAVE(x) do { error = x; goto quit; } while(0)
299 static void pppoe_start(sessp sp);
300 static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
301 static const struct pppoe_tag *scan_tags(sessp sp,
302 const struct pppoe_hdr* ph);
303 static int pppoe_send_event(sessp sp, enum cmd cmdid);
305 /*************************************************************************
306 * Some basic utilities from the Linux version with author's permission.*
307 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
308 ************************************************************************/
311 * Return the location where the next tag can be put
313 static __inline const struct pppoe_tag*
314 next_tag(const struct pppoe_hdr* ph)
316 return (const struct pppoe_tag*)(((const char*)(ph + 1))
317 + ntohs(ph->length));
321 * Look for a tag of a specific type.
322 * Don't trust any length the other end says,
323 * but assume we already sanity checked ph->length.
325 static const struct pppoe_tag*
326 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
328 const char *const end = (const char *)next_tag(ph);
329 const struct pppoe_tag *pt = (const void *)(ph + 1);
333 * Keep processing tags while a tag header will still fit.
335 while((const char*)(pt + 1) <= end) {
337 * If the tag data would go past the end of the packet, abort.
339 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
341 CTR2(KTR_NET, "%20s: invalid length for tag %d",
345 if (pt->tag_type == idx) {
346 CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
350 pt = (const struct pppoe_tag*)ptn;
353 CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
357 /**************************************************************************
358 * Inlines to initialise or add tags to a session's tag list.
359 **************************************************************************/
361 * Initialise the session's tag list.
366 KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
367 sp->neg->numtags = 0;
371 insert_tag(sessp sp, const struct pppoe_tag *tp)
376 KASSERT(neg != NULL, ("%s: no neg", __func__));
377 if ((i = neg->numtags++) < NUMTAGS) {
380 log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
387 * Make up a packet, using the tags filled out for the session.
389 * Assume that the actual pppoe header and ethernet header
390 * are filled out externally to this routine.
391 * Also assume that neg->wh points to the correct
392 * location at the front of the buffer space.
395 make_packet(sessp sp) {
396 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
397 const struct pppoe_tag **tag;
403 KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
404 ("%s: called from wrong state", __func__));
405 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
407 dp = (char *)(&wh->ph + 1);
408 for (count = 0, tag = sp->neg->tags;
409 ((count < sp->neg->numtags) && (count < NUMTAGS));
411 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
412 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
413 log(LOG_NOTICE, "ng_pppoe: tags too long\n");
414 sp->neg->numtags = count;
415 break; /* XXX chop off what's too long */
417 bcopy(*tag, (char *)dp, tlen);
421 wh->ph.length = htons(length);
422 sp->neg->m->m_len = length + sizeof(*wh);
423 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
426 /**************************************************************************
427 * Routines to match a service. *
428 **************************************************************************/
431 * Find a hook that has a service string that matches that
432 * we are seeking. For now use a simple string.
433 * In the future we may need something like regexp().
435 * Null string is a wildcard (ANY service), according to RFC2516.
436 * And historical FreeBSD wildcard is also "*".
440 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
442 const priv_p privp = NG_NODE_PRIVATE(node);
445 LIST_FOREACH(sp, &privp->listeners, sessions) {
448 /* Empty Service-Name matches any service. */
449 if (neg->service_len == 0)
452 /* Special case for a blank or "*" service name (wildcard). */
453 if (neg->service_len == 1 && neg->service.data[0] == '*')
456 /* If the lengths don't match, that aint it. */
457 if (neg->service_len != ntohs(tag->tag_len))
460 if (strncmp((const char *)(tag + 1), neg->service.data,
461 ntohs(tag->tag_len)) == 0)
464 CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
465 sp?sp->hook:NULL, (const char *)(tag + 1));
467 return (sp?sp->hook:NULL);
471 * Broadcast the PADI packet in m0 to all listening hooks.
472 * This routine is called when a PADI with empty Service-Name
473 * tag is received. Client should receive PADOs with all
474 * available services.
477 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
479 const priv_p privp = NG_NODE_PRIVATE(node);
483 LIST_FOREACH(sp, &privp->listeners, sessions) {
486 m = m_dup(m0, M_NOWAIT);
489 NG_SEND_DATA_ONLY(error, sp->hook, m);
498 * Find a hook, which name equals to given service.
501 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
503 const priv_p privp = NG_NODE_PRIVATE(node);
506 LIST_FOREACH(sp, &privp->listeners, sessions) {
509 if (neg->service_len == svc_len &&
510 strncmp(svc_name, neg->service.data, svc_len) == 0)
517 /**************************************************************************
518 * Routines to find a particular session that matches an incoming packet. *
519 **************************************************************************/
520 /* Find free session and add to hash. */
522 pppoe_getnewsession(sessp sp)
524 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
525 static uint16_t pppoe_sid = 1;
530 /* Atomicity is not needed here as value will be checked. */
532 /* Spec says 0xFFFF is reserved, also don't use 0x0000. */
533 if (val == 0xffff || val == 0x0000)
536 /* Check it isn't already in use. */
537 hash = SESSHASH(val);
538 mtx_lock(&privp->sesshash[hash].mtx);
539 LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
540 if (tsp->Session_ID == val)
544 sp->Session_ID = val;
545 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
547 mtx_unlock(&privp->sesshash[hash].mtx);
551 CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
556 /* Add specified session to hash. */
558 pppoe_addsession(sessp sp)
560 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
561 uint16_t hash = SESSHASH(sp->Session_ID);
563 mtx_lock(&privp->sesshash[hash].mtx);
564 LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
565 mtx_unlock(&privp->sesshash[hash].mtx);
568 /* Delete specified session from hash. */
570 pppoe_delsession(sessp sp)
572 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
573 uint16_t hash = SESSHASH(sp->Session_ID);
575 mtx_lock(&privp->sesshash[hash].mtx);
576 LIST_REMOVE(sp, sessions);
577 mtx_unlock(&privp->sesshash[hash].mtx);
580 /* Find matching peer/session combination. */
582 pppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
584 uint16_t session = ntohs(wh->ph.sid);
585 uint16_t hash = SESSHASH(session);
588 mtx_lock(&privp->sesshash[hash].mtx);
589 LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
590 if (sp->Session_ID == session &&
591 bcmp(sp->pkt_hdr.eh.ether_dhost,
592 wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
596 mtx_unlock(&privp->sesshash[hash].mtx);
597 CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
604 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
609 /* Cycle through all known hooks. */
610 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
611 /* Skip any nonsession hook. */
612 if (NG_HOOK_PRIVATE(hook) == NULL)
614 sp = NG_HOOK_PRIVATE(hook);
615 /* Skip already connected sessions. */
618 if (sp->neg->host_uniq_len == ntohs(tag->tag_len) &&
619 bcmp(sp->neg->host_uniq.data, (const char *)(tag + 1),
620 sp->neg->host_uniq_len) == 0)
623 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, sp);
629 pppoe_findcookie(node_p node, const struct pppoe_tag *tag)
634 bcopy(tag + 1, cookie.bytes, sizeof(void *));
635 /* Cycle through all known hooks. */
636 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
637 /* Skip any nonsession hook. */
638 if (NG_HOOK_PRIVATE(hook) == NULL)
640 if (cookie.pointer == NG_HOOK_PRIVATE(hook))
643 CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, cookie.pointer);
648 /**************************************************************************
649 * Start of Netgraph entrypoints. *
650 **************************************************************************/
653 * Allocate the private data structure and link it with node.
656 ng_pppoe_constructor(node_p node)
661 /* Initialize private descriptor. */
662 privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_ZERO);
664 /* Link structs together; this counts as our one reference to *node. */
665 NG_NODE_SET_PRIVATE(node, privp);
668 /* Initialize to standard mode. */
669 memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
670 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
672 LIST_INIT(&privp->listeners);
673 for (i = 0; i < SESSHASHSIZE; i++) {
674 mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
675 LIST_INIT(&privp->sesshash[i].head);
678 CTR3(KTR_NET, "%20s: created node [%x] (%p)",
679 __func__, node->nd_ID, node);
685 * Give our ok for a hook to be added...
686 * point the hook's private info to the hook structure.
688 * The following hook names are special:
689 * "ethernet": the hook that should be connected to a NIC.
690 * "debug": copies of data sent out here (when I write the code).
691 * All other hook names need only be unique. (the framework checks this).
694 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
696 const priv_p privp = NG_NODE_PRIVATE(node);
699 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
700 privp->ethernet_hook = hook;
701 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
702 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
703 privp->debug_hook = hook;
704 NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
707 * Any other unique name is OK.
708 * The infrastructure has already checked that it's unique,
709 * so just allocate it and hook it in.
711 sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
715 NG_HOOK_SET_PRIVATE(hook, sp);
718 CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
719 __func__, node->nd_ID, node, name, hook);
725 * Hook has been added successfully. Request the MAC address of
726 * the underlying Ethernet node.
729 ng_pppoe_connect(hook_p hook)
731 const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
735 if (hook != privp->ethernet_hook)
739 * If this is Ethernet hook, then request MAC address
740 * from our downstream.
742 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
747 * Our hook and peer hook have HK_INVALID flag set,
748 * so we can't use NG_SEND_MSG_HOOK() macro here.
750 NG_SEND_MSG_ID(error, privp->node, msg,
751 NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
752 NG_NODE_ID(privp->node));
757 * Get a netgraph control message.
758 * Check it is one we understand. If needed, send a response.
759 * We sometimes save the address for an async action later.
760 * Always free the message.
763 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
765 struct epoch_tracker et;
766 priv_p privp = NG_NODE_PRIVATE(node);
767 struct ngpppoe_init_data *ourmsg = NULL;
768 struct ng_mesg *resp = NULL;
775 NGI_GET_MSG(item, msg);
776 CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
777 __func__, node->nd_ID, node, msg->header.cmd,
778 msg->header.typecookie);
780 /* Deal with message according to cookie and command. */
781 switch (msg->header.typecookie) {
782 case NGM_PPPOE_COOKIE:
783 switch (msg->header.cmd) {
784 case NGM_PPPOE_CONNECT:
785 case NGM_PPPOE_LISTEN:
786 case NGM_PPPOE_OFFER:
787 case NGM_PPPOE_SERVICE:
788 case NGM_PPPOE_SEND_HURL:
789 case NGM_PPPOE_SEND_MOTM:
790 ourmsg = (struct ngpppoe_init_data *)msg->data;
791 if (msg->header.arglen < sizeof(*ourmsg)) {
792 log(LOG_ERR, "ng_pppoe[%x]: init data too "
793 "small\n", node->nd_ID);
796 if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
797 msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
798 if (msg->header.arglen - sizeof(*ourmsg) >
799 PPPOE_PADM_VALUE_SIZE) {
800 log(LOG_ERR, "ng_pppoe[%x]: message "
801 "too big\n", node->nd_ID);
805 if (msg->header.arglen - sizeof(*ourmsg) >
806 PPPOE_SERVICE_NAME_SIZE) {
807 log(LOG_ERR, "ng_pppoe[%x]: service name "
808 "too big\n", node->nd_ID);
812 if (msg->header.arglen - sizeof(*ourmsg) <
814 log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
815 "length, %d should be %zd\n", node->nd_ID,
817 msg->header.arglen - sizeof (*ourmsg));
821 /* Make sure strcmp will terminate safely. */
822 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
824 /* Find hook by name. */
825 hook = ng_findhook(node, ourmsg->hook);
829 sp = NG_HOOK_PRIVATE(hook);
833 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
835 * Ensure we aren't already listening for this
838 if (pppoe_find_svc(node, ourmsg->data,
839 ourmsg->data_len) != NULL)
844 * PPPOE_SERVICE advertisements are set up
845 * on sessions that are in PRIMED state.
847 if (msg->header.cmd == NGM_PPPOE_SERVICE)
851 * PADM messages are set up on active sessions.
853 if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
854 msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
855 if (sp->state != PPPOE_NEWCONNECTED &&
856 sp->state != PPPOE_CONNECTED) {
857 log(LOG_NOTICE, "ng_pppoe[%x]: session is not "
858 "active\n", node->nd_ID);
864 if (sp->state != PPPOE_SNONE) {
865 log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
866 "active\n", node->nd_ID);
871 * Set up prototype header.
873 neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
879 neg->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
880 if (neg->m == NULL) {
881 free(neg, M_NETGRAPH_PPPOE);
884 neg->m->m_pkthdr.rcvif = NULL;
886 ng_callout_init(&neg->handle);
887 neg->m->m_len = sizeof(struct pppoe_full_hdr);
888 neg->pkt = mtod(neg->m, union packet*);
889 memcpy((void *)&neg->pkt->pkt_header.eh,
890 &privp->eh, sizeof(struct ether_header));
891 neg->pkt->pkt_header.ph.ver = 0x1;
892 neg->pkt->pkt_header.ph.type = 0x1;
893 neg->pkt->pkt_header.ph.sid = 0x0000;
896 sp->creator = NGI_RETADDR(item);
898 switch (msg->header.cmd) {
899 case NGM_PPPOE_GET_STATUS:
901 struct ngpppoestat *stats;
903 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
907 stats = (struct ngpppoestat *) resp->data;
908 stats->packets_in = privp->packets_in;
909 stats->packets_out = privp->packets_out;
912 case NGM_PPPOE_CONNECT:
915 * Check the hook exists and is Uninitialised.
916 * Send a PADI request, and start the timeout logic.
917 * Store the originator of this message so we can send
918 * a success or fail message to them later.
919 * Move the session to SINIT.
920 * Set up the session to the correct state and
923 int acnpos, acnlen = 0, acnsep = 0;
924 int hupos, hulen = 0, husep = 0;
925 int i, srvpos, srvlen;
927 for (i = 0; i < ourmsg->data_len; i++) {
928 if (ourmsg->data[i] == '\\') {
934 hupos = acnlen + acnsep;
935 for (i = hupos; i < ourmsg->data_len; i++) {
936 if (ourmsg->data[i] == '|') {
942 srvpos = hupos + hulen + husep;
943 srvlen = ourmsg->data_len - srvpos;
945 bcopy(ourmsg->data + acnpos, neg->ac_name.data, acnlen);
946 neg->ac_name_len = acnlen;
948 neg->host_uniq.hdr.tag_type = PTT_HOST_UNIQ;
950 /* Not provided, generate one */
951 neg->host_uniq.hdr.tag_len = htons(sizeof(sp));
952 bcopy(&sp, neg->host_uniq.data, sizeof(sp));
953 neg->host_uniq_len = sizeof(sp);
954 } else if (hulen > 2 && ourmsg->data[hupos] == '0' &&
955 ourmsg->data[hupos + 1] == 'x' && hulen % 2 == 0) {
957 static const char hexdig[16] = "0123456789abcdef";
960 neg->host_uniq.hdr.tag_len = htons((uint16_t)(hulen / 2 - 1));
961 for (i = 0; i < hulen - 2; i++) {
964 ourmsg->data[hupos + 2 + i] != hexdig[j];
969 neg->host_uniq.data[i / 2] = j << 4;
971 neg->host_uniq.data[i / 2] |= j;
973 neg->host_uniq_len = hulen / 2 - 1;
976 neg->host_uniq.hdr.tag_len = htons((uint16_t)hulen);
977 bcopy(ourmsg->data + hupos, neg->host_uniq.data, hulen);
978 neg->host_uniq_len = hulen;
981 neg->service.hdr.tag_type = PTT_SRV_NAME;
982 neg->service.hdr.tag_len = htons((uint16_t)srvlen);
983 bcopy(ourmsg->data + srvpos, neg->service.data, srvlen);
984 neg->service_len = srvlen;
990 case NGM_PPPOE_LISTEN:
992 * Check the hook exists and is Uninitialised.
993 * Install the service matching string.
994 * Store the originator of this message so we can send
995 * a success or fail message to them later.
996 * Move the hook to 'LISTENING'
998 neg->service.hdr.tag_type = PTT_SRV_NAME;
999 neg->service.hdr.tag_len =
1000 htons((uint16_t)ourmsg->data_len);
1002 if (ourmsg->data_len)
1003 bcopy(ourmsg->data, neg->service.data,
1005 neg->service_len = ourmsg->data_len;
1006 neg->pkt->pkt_header.ph.code = PADT_CODE;
1008 * Wait for PADI packet coming from Ethernet.
1010 sp->state = PPPOE_LISTENING;
1011 LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
1013 case NGM_PPPOE_OFFER:
1015 * Check the hook exists and is Uninitialised.
1016 * Store the originator of this message so we can send
1017 * a success of fail message to them later.
1018 * Store the AC-Name given and go to PRIMED.
1020 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
1021 neg->ac_name.hdr.tag_len =
1022 htons((uint16_t)ourmsg->data_len);
1023 if (ourmsg->data_len)
1024 bcopy(ourmsg->data, neg->ac_name.data,
1026 neg->ac_name_len = ourmsg->data_len;
1027 neg->pkt->pkt_header.ph.code = PADO_CODE;
1029 * Wait for PADI packet coming from hook.
1031 sp->state = PPPOE_PRIMED;
1033 case NGM_PPPOE_SERVICE:
1035 * Check the session is primed.
1036 * for now just allow ONE service to be advertised.
1037 * If you do it twice you just overwrite.
1039 if (sp->state != PPPOE_PRIMED) {
1040 log(LOG_NOTICE, "ng_pppoe[%x]: session not "
1041 "primed\n", node->nd_ID);
1045 neg->service.hdr.tag_type = PTT_SRV_NAME;
1046 neg->service.hdr.tag_len =
1047 htons((uint16_t)ourmsg->data_len);
1049 if (ourmsg->data_len)
1050 bcopy(ourmsg->data, neg->service.data,
1052 neg->service_len = ourmsg->data_len;
1054 case NGM_PPPOE_SETMODE:
1059 if (msg->header.arglen == 0)
1062 s = (char *)msg->data;
1063 len = msg->header.arglen - 1;
1065 /* Search for matching mode string. */
1066 if (len == strlen(NG_PPPOE_STANDARD) &&
1067 (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
1069 privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1072 if (len == strlen(NG_PPPOE_3COM) &&
1073 (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
1074 privp->flags |= COMPAT_3COM;
1075 privp->eh.ether_type =
1076 ETHERTYPE_PPPOE_3COM_DISC;
1079 if (len == strlen(NG_PPPOE_DLINK) &&
1080 (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
1081 privp->flags |= COMPAT_DLINK;
1087 case NGM_PPPOE_GETMODE:
1092 if (privp->flags == 0)
1093 len += strlen(NG_PPPOE_STANDARD) + 1;
1094 if (privp->flags & COMPAT_3COM)
1095 len += strlen(NG_PPPOE_3COM) + 1;
1096 if (privp->flags & COMPAT_DLINK)
1097 len += strlen(NG_PPPOE_DLINK) + 1;
1099 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
1103 s = (char *)resp->data;
1104 if (privp->flags == 0) {
1105 len = strlen(NG_PPPOE_STANDARD);
1106 strlcpy(s, NG_PPPOE_STANDARD, len + 1);
1109 if (privp->flags & COMPAT_3COM) {
1110 len = strlen(NG_PPPOE_3COM);
1111 strlcpy(s, NG_PPPOE_3COM, len + 1);
1114 if (privp->flags & COMPAT_DLINK) {
1115 if (s != resp->data)
1117 len = strlen(NG_PPPOE_DLINK);
1118 strlcpy(s, NG_PPPOE_DLINK, len + 1);
1122 case NGM_PPPOE_SETENADDR:
1123 if (msg->header.arglen != ETHER_ADDR_LEN)
1125 bcopy(msg->data, &privp->eh.ether_shost,
1128 case NGM_PPPOE_SETMAXP:
1129 if (msg->header.arglen != sizeof(uint16_t))
1131 privp->max_payload.hdr.tag_type = PTT_MAX_PAYL;
1132 privp->max_payload.hdr.tag_len = htons(sizeof(uint16_t));
1133 privp->max_payload.data = htons(*((uint16_t *)msg->data));
1135 case NGM_PPPOE_SEND_HURL:
1139 /* Generate a packet of that type. */
1140 m = m_gethdr(M_NOWAIT, MT_DATA);
1142 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1143 "mbufs\n", node->nd_ID);
1145 struct pppoe_full_hdr *wh;
1146 struct pppoe_tag *tag;
1149 wh = mtod(m, struct pppoe_full_hdr *);
1150 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1152 /* Revert the stored header to DISC/PADM mode. */
1153 wh->ph.code = PADM_CODE;
1155 * Configure ethertype depending on what
1156 * was used during sessions stage.
1158 if (wh->eh.ether_type ==
1159 ETHERTYPE_PPPOE_3COM_SESS)
1160 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1162 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1164 * Add PADM message and adjust sizes.
1166 tag = (void *)(&wh->ph + 1);
1167 tag->tag_type = PTT_HURL;
1168 tag->tag_len = htons(ourmsg->data_len);
1169 strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
1170 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
1172 wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
1173 NET_EPOCH_ENTER(et);
1174 NG_SEND_DATA_ONLY(error,
1175 privp->ethernet_hook, m);
1180 case NGM_PPPOE_SEND_MOTM:
1184 /* Generate a packet of that type. */
1185 m = m_gethdr(M_NOWAIT, MT_DATA);
1187 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1188 "mbufs\n", node->nd_ID);
1190 struct pppoe_full_hdr *wh;
1191 struct pppoe_tag *tag;
1194 wh = mtod(m, struct pppoe_full_hdr *);
1195 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1197 /* Revert the stored header to DISC/PADM mode. */
1198 wh->ph.code = PADM_CODE;
1200 * Configure ethertype depending on what
1201 * was used during sessions stage.
1203 if (wh->eh.ether_type ==
1204 ETHERTYPE_PPPOE_3COM_SESS)
1205 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1207 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1209 * Add PADM message and adjust sizes.
1211 tag = (void *)(&wh->ph + 1);
1212 tag->tag_type = PTT_MOTM;
1213 tag->tag_len = htons(ourmsg->data_len);
1214 strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
1215 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
1217 wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
1218 NET_EPOCH_ENTER(et);
1219 NG_SEND_DATA_ONLY(error,
1220 privp->ethernet_hook, m);
1229 case NGM_ETHER_COOKIE:
1230 if (!(msg->header.flags & NGF_RESP))
1232 switch (msg->header.cmd) {
1233 case NGM_ETHER_GET_ENADDR:
1234 if (msg->header.arglen != ETHER_ADDR_LEN)
1236 bcopy(msg->data, &privp->eh.ether_shost,
1247 /* Take care of synchronous response, if any. */
1249 CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1250 NG_RESPOND_MSG(error, node, item, resp);
1251 /* Free the message and return. */
1257 * Start a client into the first state. A separate function because
1258 * it can be needed if the negotiation times out.
1261 pppoe_start(sessp sp)
1263 hook_p hook = sp->hook;
1264 node_p node = NG_HOOK_NODE(hook);
1265 priv_p privp = NG_NODE_PRIVATE(node);
1271 * Kick the state machine into starting up.
1273 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1274 sp->state = PPPOE_SINIT;
1276 * Reset the packet header to broadcast. Since we are
1277 * in a client mode use configured ethertype.
1279 memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1280 sizeof(struct ether_header));
1281 neg->pkt->pkt_header.ph.code = PADI_CODE;
1283 insert_tag(sp, &neg->host_uniq.hdr);
1284 insert_tag(sp, &neg->service.hdr);
1285 if (privp->max_payload.data != 0)
1286 insert_tag(sp, &privp->max_payload.hdr);
1289 * Send packet and prepare to retransmit it after timeout.
1291 ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1292 pppoe_ticker, NULL, 0);
1293 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1294 m0 = m_copypacket(neg->m, M_NOWAIT);
1295 NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
1299 send_acname(sessp sp, const struct pppoe_tag *tag)
1302 struct ng_mesg *msg;
1303 struct ngpppoe_sts *sts;
1305 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1307 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1308 sizeof(struct ngpppoe_sts), M_NOWAIT);
1312 sts = (struct ngpppoe_sts *)msg->data;
1313 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1314 strncpy(sts->hook, (const char *)(tag + 1), tlen);
1315 sts->hook[tlen] = '\0';
1316 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1322 send_sessionid(sessp sp)
1325 struct ng_mesg *msg;
1327 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1329 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1330 sizeof(uint16_t), M_NOWAIT);
1334 *(uint16_t *)msg->data = sp->Session_ID;
1335 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1341 send_maxp(sessp sp, const struct pppoe_tag *tag)
1344 struct ng_mesg *msg;
1345 struct ngpppoe_maxp *maxp;
1347 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1349 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SETMAXP,
1350 sizeof(struct ngpppoe_maxp), M_NOWAIT);
1354 maxp = (struct ngpppoe_maxp *)msg->data;
1355 strncpy(maxp->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1356 maxp->data = ntohs(((const struct maxptag *)tag)->data);
1357 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1363 send_hurl(sessp sp, const struct pppoe_tag *tag)
1366 struct ng_mesg *msg;
1367 struct ngpppoe_padm *padm;
1369 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1371 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_HURL,
1372 sizeof(struct ngpppoe_padm), M_NOWAIT);
1376 padm = (struct ngpppoe_padm *)msg->data;
1377 tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
1378 strncpy(padm->msg, (const char *)(tag + 1), tlen);
1379 padm->msg[tlen] = '\0';
1380 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1386 send_motm(sessp sp, const struct pppoe_tag *tag)
1389 struct ng_mesg *msg;
1390 struct ngpppoe_padm *padm;
1392 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1394 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_MOTM,
1395 sizeof(struct ngpppoe_padm), M_NOWAIT);
1399 padm = (struct ngpppoe_padm *)msg->data;
1400 tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
1401 strncpy(padm->msg, (const char *)(tag + 1), tlen);
1402 padm->msg[tlen] = '\0';
1403 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1409 * Receive data from session hook and do something with it.
1412 ng_pppoe_rcvdata(hook_p hook, item_p item)
1414 node_p node = NG_HOOK_NODE(hook);
1415 const priv_p privp = NG_NODE_PRIVATE(node);
1416 sessp sp = NG_HOOK_PRIVATE(hook);
1417 struct pppoe_full_hdr *wh;
1421 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1422 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1425 switch (sp->state) {
1426 case PPPOE_NEWCONNECTED:
1427 case PPPOE_CONNECTED: {
1429 * Remove PPP address and control fields, if any.
1430 * For example, ng_ppp(4) always sends LCP packets
1431 * with address and control fields as required by
1432 * generic PPP. PPPoE is an exception to the rule.
1434 if (m->m_pkthdr.len >= 2) {
1435 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1437 if (mtod(m, u_char *)[0] == 0xff &&
1438 mtod(m, u_char *)[1] == 0x03)
1442 * Bang in a pre-made header, and set the length up
1443 * to be correct. Then send it to the ethernet driver.
1445 M_PREPEND(m, sizeof(*wh), M_NOWAIT);
1449 wh = mtod(m, struct pppoe_full_hdr *);
1450 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1451 wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1452 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1453 privp->packets_out++;
1456 case PPPOE_PRIMED: {
1458 struct pppoe_tag hdr;
1461 const struct pppoe_tag *tag;
1463 const struct pppoe_hdr *ph;
1470 * A PADI packet is being returned by the application
1471 * that has set up this hook. This indicates that it
1472 * wants us to offer service.
1474 if (m->m_len < sizeof(*wh)) {
1475 m = m_pullup(m, sizeof(*wh));
1479 wh = mtod(m, struct pppoe_full_hdr *);
1481 session = ntohs(wh->ph.sid);
1482 length = ntohs(wh->ph.length);
1484 /* Use peers mode in session. */
1485 neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1486 if (code != PADI_CODE)
1488 ng_uncallout(&neg->handle, node);
1491 * This is the first time we hear
1492 * from the client, so note it's
1493 * unicast address, replacing the
1494 * broadcast address.
1496 bcopy(wh->eh.ether_shost,
1497 neg->pkt->pkt_header.eh.ether_dhost,
1499 sp->state = PPPOE_SOFFER;
1501 neg->pkt->pkt_header.ph.code = PADO_CODE;
1504 * Start working out the tags to respond with.
1506 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1507 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1508 uniqtag.data.pointer = sp;
1510 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1511 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1512 insert_tag(sp, tag); /* return service */
1514 * If we have a NULL service request
1515 * and have an extra service defined in this hook,
1516 * then also add a tag for the extra service.
1517 * XXX this is a hack. eventually we should be able
1518 * to support advertising many services, not just one
1520 if (((tag == NULL) || (tag->tag_len == 0)) &&
1521 (neg->service.hdr.tag_len != 0)) {
1522 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1524 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1525 insert_tag(sp, tag); /* returned hostunique */
1526 insert_tag(sp, &uniqtag.hdr);
1530 * Send the offer but if they don't respond
1531 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1533 ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1534 pppoe_ticker, NULL, 0);
1535 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1536 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1537 privp->packets_out++;
1542 * Packets coming from the hook make no sense
1543 * to sessions in the rest of states. Throw them away.
1556 * Receive data from ether and do something with it.
1559 ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1561 node_p node = NG_HOOK_NODE(hook);
1562 const priv_p privp = NG_NODE_PRIVATE(node);
1564 const struct pppoe_tag *utag = NULL, *tag = NULL;
1565 const struct pppoe_tag sntag = { PTT_SRV_NAME, 0 };
1566 const struct pppoe_full_hdr *wh;
1567 const struct pppoe_hdr *ph;
1577 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1578 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1582 * Dig out various fields from the packet.
1583 * Use them to decide where to send it.
1585 privp->packets_in++;
1586 if( m->m_len < sizeof(*wh)) {
1587 m = m_pullup(m, sizeof(*wh)); /* Checks length */
1589 log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1590 "m_pullup(wh)\n", node->nd_ID);
1594 wh = mtod(m, struct pppoe_full_hdr *);
1595 length = ntohs(wh->ph.length);
1596 switch(wh->eh.ether_type) {
1597 case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1598 case ETHERTYPE_PPPOE_DISC:
1600 * We need to try to make sure that the tag area
1601 * is contiguous, or we could wander off the end
1602 * of a buffer and make a mess.
1603 * (Linux wouldn't have this problem).
1605 if (m->m_pkthdr.len <= MHLEN) {
1606 if( m->m_len < m->m_pkthdr.len) {
1607 m = m_pullup(m, m->m_pkthdr.len);
1609 log(LOG_NOTICE, "ng_pppoe[%x]: "
1610 "couldn't m_pullup(pkthdr)\n",
1616 if (m->m_len != m->m_pkthdr.len) {
1618 * It's not all in one piece.
1619 * We need to do extra work.
1620 * Put it into a cluster.
1623 n = m_dup(m, M_NOWAIT);
1627 /* just check we got a cluster */
1628 if (m->m_len != m->m_pkthdr.len) {
1634 log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1635 "fragmented\n", node->nd_ID);
1639 wh = mtod(m, struct pppoe_full_hdr *);
1640 length = ntohs(wh->ph.length);
1642 session = ntohs(wh->ph.sid);
1649 * Look for a hook with the required service and send
1650 * the ENTIRE packet up there. It should come back to
1651 * a new hook in PRIMED state. Look there for further
1654 tag = get_tag(ph, PTT_SRV_NAME);
1659 * First, try to match Service-Name against our
1660 * listening hooks. If no success and we are in D-Link
1661 * compat mode and Service-Name is empty, then we
1662 * broadcast the PADI to all listening hooks.
1664 sendhook = pppoe_match_svc(node, tag);
1665 if (sendhook != NULL)
1666 NG_FWD_NEW_DATA(error, item, sendhook, m);
1667 else if (privp->flags & COMPAT_DLINK &&
1668 ntohs(tag->tag_len) == 0)
1669 error = pppoe_broadcast_padi(node, m);
1671 error = ENETUNREACH;
1676 * Use the host_uniq tag to find the hook this is in
1677 * response to. Received #2, now send #3
1678 * For now simply accept the first we receive.
1680 utag = get_tag(ph, PTT_HOST_UNIQ);
1682 log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1683 "unique field\n", node->nd_ID);
1687 sendhook = pppoe_finduniq(node, utag);
1688 if (sendhook == NULL) {
1689 log(LOG_NOTICE, "ng_pppoe[%x]: no "
1690 "matching session\n", node->nd_ID);
1695 * Check the session is in the right state.
1696 * It needs to be in PPPOE_SINIT.
1698 sp = NG_HOOK_PRIVATE(sendhook);
1699 if (sp->state == PPPOE_SREQ ||
1700 sp->state == PPPOE_CONNECTED) {
1701 break; /* Multiple PADO is OK. */
1703 if (sp->state != PPPOE_SINIT) {
1704 log(LOG_NOTICE, "ng_pppoe[%x]: session "
1705 "in wrong state\n", node->nd_ID);
1709 /* If requested specific AC-name, check it. */
1710 if (neg->ac_name_len) {
1711 tag = get_tag(ph, PTT_AC_NAME);
1713 /* No PTT_AC_NAME in PADO */
1716 if (neg->ac_name_len != htons(tag->tag_len) ||
1717 strncmp(neg->ac_name.data,
1718 (const char *)(tag + 1),
1719 neg->ac_name_len) != 0) {
1723 sp->state = PPPOE_SREQ;
1724 ng_uncallout(&neg->handle, node);
1727 * This is the first time we hear
1728 * from the server, so note it's
1729 * unicast address, replacing the
1730 * broadcast address .
1732 bcopy(wh->eh.ether_shost,
1733 neg->pkt->pkt_header.eh.ether_dhost,
1736 neg->pkt->pkt_header.ph.code = PADR_CODE;
1738 insert_tag(sp, utag); /* Host Unique */
1739 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1740 insert_tag(sp, tag); /* return cookie */
1741 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1742 insert_tag(sp, tag); /* return it */
1743 send_acname(sp, tag);
1745 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1746 (privp->max_payload.data != 0))
1747 insert_tag(sp, tag); /* return it */
1748 insert_tag(sp, &neg->service.hdr); /* Service */
1751 sp->state = PPPOE_SREQ;
1752 ng_callout(&neg->handle, node, sp->hook,
1753 PPPOE_INITIAL_TIMEOUT * hz,
1754 pppoe_ticker, NULL, 0);
1755 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1756 m0 = m_copypacket(neg->m, M_NOWAIT);
1757 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1762 * Use the ac_cookie tag to find the
1763 * hook this is in response to.
1765 utag = get_tag(ph, PTT_AC_COOKIE);
1766 if ((utag == NULL) ||
1767 (ntohs(utag->tag_len) != sizeof(sp))) {
1771 sendhook = pppoe_findcookie(node, utag);
1772 if (sendhook == NULL)
1776 * Check the session is in the right state.
1777 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1778 * If the latter, then this is a retry by the client,
1779 * so be nice, and resend.
1781 sp = NG_HOOK_PRIVATE(sendhook);
1782 if (sp->state == PPPOE_NEWCONNECTED) {
1784 * Whoa! drop back to resend that PADS packet.
1785 * We should still have a copy of it.
1787 sp->state = PPPOE_SOFFER;
1788 } else if (sp->state != PPPOE_SOFFER)
1789 LEAVE (ENETUNREACH);
1791 ng_uncallout(&neg->handle, node);
1792 neg->pkt->pkt_header.ph.code = PADS_CODE;
1793 if (sp->Session_ID == 0) {
1794 neg->pkt->pkt_header.ph.sid =
1795 htons(pppoe_getnewsession(sp));
1800 * start working out the tags to respond with.
1803 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1804 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1805 insert_tag(sp, tag);/* return service */
1806 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1807 insert_tag(sp, tag); /* return it */
1808 insert_tag(sp, utag); /* ac_cookie */
1811 sp->state = PPPOE_NEWCONNECTED;
1813 /* Send the PADS without a timeout - we're now connected. */
1814 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1815 NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1818 * Having sent the last Negotiation header,
1819 * Set up the stored packet header to be correct for
1820 * the actual session. But keep the negotialtion stuff
1821 * around in case we need to resend this last packet.
1822 * We'll discard it when we move from NEWCONNECTED
1825 sp->pkt_hdr = neg->pkt->pkt_header;
1826 /* Configure ethertype depending on what
1827 * ethertype was used at discovery phase */
1828 if (sp->pkt_hdr.eh.ether_type ==
1829 ETHERTYPE_PPPOE_3COM_DISC)
1830 sp->pkt_hdr.eh.ether_type
1831 = ETHERTYPE_PPPOE_3COM_SESS;
1833 sp->pkt_hdr.eh.ether_type
1834 = ETHERTYPE_PPPOE_SESS;
1835 sp->pkt_hdr.ph.code = 0;
1836 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1841 * Use the host_uniq tag to find the hook this is in
1842 * response to. Take the session ID and store it away.
1843 * Also make sure the pre-made header is correct and
1844 * set us into Session mode.
1846 utag = get_tag(ph, PTT_HOST_UNIQ);
1848 LEAVE (ENETUNREACH);
1850 sendhook = pppoe_finduniq(node, utag);
1851 if (sendhook == NULL)
1855 * Check the session is in the right state.
1856 * It needs to be in PPPOE_SREQ.
1858 sp = NG_HOOK_PRIVATE(sendhook);
1859 if (sp->state != PPPOE_SREQ)
1862 ng_uncallout(&neg->handle, node);
1863 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1864 sp->Session_ID = ntohs(wh->ph.sid);
1865 pppoe_addsession(sp);
1868 sp->state = PPPOE_CONNECTED;
1870 * Now we have gone to Connected mode,
1871 * Free all resources needed for negotiation.
1872 * Keep a copy of the header we will be using.
1874 sp->pkt_hdr = neg->pkt->pkt_header;
1875 if (privp->flags & COMPAT_3COM)
1876 sp->pkt_hdr.eh.ether_type
1877 = ETHERTYPE_PPPOE_3COM_SESS;
1879 sp->pkt_hdr.eh.ether_type
1880 = ETHERTYPE_PPPOE_SESS;
1881 sp->pkt_hdr.ph.code = 0;
1883 free(sp->neg, M_NETGRAPH_PPPOE);
1885 if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
1886 (privp->max_payload.data != 0))
1888 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1892 * Find matching peer/session combination.
1894 sp = pppoe_findsession(privp, wh);
1897 /* Disconnect that hook. */
1898 ng_rmhook_self(sp->hook);
1903 * find matching peer/session combination.
1905 sp = pppoe_findsession(privp, wh);
1907 LEAVE (ENETUNREACH);
1908 if ((tag = get_tag(ph, PTT_HURL)))
1910 if ((tag = get_tag(ph, PTT_MOTM)))
1914 LEAVE(EPFNOSUPPORT);
1917 case ETHERTYPE_PPPOE_3COM_SESS:
1918 case ETHERTYPE_PPPOE_SESS:
1920 * Find matching peer/session combination.
1922 sp = pppoe_findsession(privp, wh);
1924 LEAVE (ENETUNREACH);
1925 m_adj(m, sizeof(*wh));
1927 /* If packet too short, dump it. */
1928 if (m->m_pkthdr.len < length)
1930 /* Also need to trim excess at the end */
1931 if (m->m_pkthdr.len > length) {
1932 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1934 if ( sp->state != PPPOE_CONNECTED) {
1935 if (sp->state == PPPOE_NEWCONNECTED) {
1936 sp->state = PPPOE_CONNECTED;
1938 * Now we have gone to Connected mode,
1939 * Free all resources needed for negotiation.
1940 * Be paranoid about whether there may be
1943 m_freem(sp->neg->m);
1944 ng_uncallout(&sp->neg->handle, node);
1945 free(sp->neg, M_NETGRAPH_PPPOE);
1948 LEAVE (ENETUNREACH);
1951 NG_FWD_NEW_DATA(error, item, sp->hook, m);
1954 LEAVE(EPFNOSUPPORT);
1964 * Receive data from debug hook and bypass it to ether.
1967 ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1969 node_p node = NG_HOOK_NODE(hook);
1970 const priv_p privp = NG_NODE_PRIVATE(node);
1973 CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1974 __func__, node->nd_ID, node, item, hook->hk_name, hook);
1976 NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1977 privp->packets_out++;
1982 * Do local shutdown processing..
1983 * If we are a persistent device, we might refuse to go away, and
1984 * we'd only remove our links and reset ourself.
1987 ng_pppoe_shutdown(node_p node)
1989 const priv_p privp = NG_NODE_PRIVATE(node);
1992 for (i = 0; i < SESSHASHSIZE; i++)
1993 mtx_destroy(&privp->sesshash[i].mtx);
1994 NG_NODE_SET_PRIVATE(node, NULL);
1995 NG_NODE_UNREF(privp->node);
1996 free(privp, M_NETGRAPH_PPPOE);
2001 * Hook disconnection
2003 * Clean up all dangling links and information about the session/hook.
2004 * For this type, removal of the last link destroys the node.
2007 ng_pppoe_disconnect(hook_p hook)
2009 node_p node = NG_HOOK_NODE(hook);
2010 priv_p privp = NG_NODE_PRIVATE(node);
2013 if (hook == privp->debug_hook) {
2014 privp->debug_hook = NULL;
2015 } else if (hook == privp->ethernet_hook) {
2016 privp->ethernet_hook = NULL;
2017 if (NG_NODE_IS_VALID(node))
2018 ng_rmnode_self(node);
2020 sp = NG_HOOK_PRIVATE(hook);
2021 if (sp->state != PPPOE_SNONE ) {
2022 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
2025 * According to the spec, if we are connected,
2026 * we should send a DISC packet if we are shutting down
2029 if ((privp->ethernet_hook)
2030 && ((sp->state == PPPOE_CONNECTED)
2031 || (sp->state == PPPOE_NEWCONNECTED))) {
2034 /* Generate a packet of that type. */
2035 m = m_gethdr(M_NOWAIT, MT_DATA);
2037 log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
2038 "mbufs\n", node->nd_ID);
2040 struct pppoe_full_hdr *wh;
2041 struct pppoe_tag *tag;
2042 int msglen = strlen(SIGNOFF);
2045 wh = mtod(m, struct pppoe_full_hdr *);
2046 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
2048 /* Revert the stored header to DISC/PADT mode. */
2049 wh->ph.code = PADT_CODE;
2051 * Configure ethertype depending on what
2052 * was used during sessions stage.
2054 if (wh->eh.ether_type ==
2055 ETHERTYPE_PPPOE_3COM_SESS)
2056 wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
2058 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
2060 * Add a General error message and adjust
2063 tag = (void *)(&wh->ph + 1);
2064 tag->tag_type = PTT_GEN_ERR;
2065 tag->tag_len = htons((u_int16_t)msglen);
2066 strncpy((char *)(tag + 1), SIGNOFF, msglen);
2067 m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
2069 wh->ph.length = htons(sizeof(*tag) + msglen);
2070 NG_SEND_DATA_ONLY(error,
2071 privp->ethernet_hook, m);
2074 if (sp->state == PPPOE_LISTENING)
2075 LIST_REMOVE(sp, sessions);
2076 else if (sp->Session_ID)
2077 pppoe_delsession(sp);
2079 * As long as we have somewhere to store the timeout handle,
2080 * we may have a timeout pending.. get rid of it.
2083 ng_uncallout(&sp->neg->handle, node);
2085 m_freem(sp->neg->m);
2086 free(sp->neg, M_NETGRAPH_PPPOE);
2088 free(sp, M_NETGRAPH_PPPOE);
2089 NG_HOOK_SET_PRIVATE(hook, NULL);
2091 if ((NG_NODE_NUMHOOKS(node) == 0) &&
2092 (NG_NODE_IS_VALID(node)))
2093 ng_rmnode_self(node);
2098 * Timeouts come here.
2101 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
2103 priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
2104 sessp sp = NG_HOOK_PRIVATE(hook);
2106 struct mbuf *m0 = NULL;
2109 CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
2110 __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
2113 * Resend the last packet, using an exponential backoff.
2114 * After a period of time, stop growing the backoff,
2115 * And either leave it, or revert to the start.
2119 /* Timeouts on these produce resends. */
2120 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
2121 NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
2122 ng_callout(&neg->handle, node, hook, neg->timeout * hz,
2123 pppoe_ticker, NULL, 0);
2124 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
2125 if (sp->state == PPPOE_SREQ) {
2126 /* Revert to SINIT mode. */
2129 neg->timeout = PPPOE_TIMEOUT_LIMIT;
2135 /* A timeout on these says "give up" */
2136 ng_rmhook_self(hook);
2139 /* Timeouts have no meaning in other states. */
2140 log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
2146 * Parse an incoming packet to see if any tags should be copied to the
2147 * output packet. Don't do any tags that have been handled in the main
2150 static const struct pppoe_tag*
2151 scan_tags(sessp sp, const struct pppoe_hdr* ph)
2153 const char *const end = (const char *)next_tag(ph);
2155 const struct pppoe_tag *pt = (const void *)(ph + 1);
2158 * Keep processing tags while a tag header will still fit.
2160 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
2162 while((const char*)(pt + 1) <= end) {
2164 * If the tag data would go past the end of the packet, abort.
2166 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
2170 switch (pt->tag_type) {
2189 pt = (const struct pppoe_tag*)ptn;
2195 pppoe_send_event(sessp sp, enum cmd cmdid)
2198 struct ng_mesg *msg;
2199 struct ngpppoe_sts *sts;
2201 CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
2203 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
2204 sizeof(struct ngpppoe_sts), M_NOWAIT);
2207 sts = (struct ngpppoe_sts *)msg->data;
2208 strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
2209 NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);