xref: /dragonfly/sys/netgraph7/pppoe/ng_pppoe.c (revision 2020c8fe)
1 /*
2  * ng_pppoe.c
3  */
4 
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
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.
19  *
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
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/ng_pppoe.c,v 1.94 2008/03/03 19:36:03 mav Exp $
41  * $DragonFly: src/sys/netgraph7/ng_pppoe.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42  * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
52 #include <sys/syslog.h>
53 #include <net/ethernet.h>
54 
55 #include <netgraph7/ng_message.h>
56 #include <netgraph7/netgraph.h>
57 #include <netgraph7/ng_parse.h>
58 #include "ng_pppoe.h"
59 #include <netgraph7/ether/ng_ether.h>
60 
61 #ifdef NG_SEPARATE_MALLOC
62 MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
63 #else
64 #define M_NETGRAPH_PPPOE M_NETGRAPH
65 #endif
66 
67 #define SIGNOFF "session closed"
68 
69 /*
70  * This section contains the netgraph method declarations for the
71  * pppoe node. These methods define the netgraph pppoe 'type'.
72  */
73 
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;
83 
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
90 };
91 
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
98 };
99 
100 /* List of commands and how to convert arguments to/from ASCII */
101 static const struct ng_cmdlist ng_pppoe_cmds[] = {
102 	{
103 	  NGM_PPPOE_COOKIE,
104 	  NGM_PPPOE_CONNECT,
105 	  "pppoe_connect",
106 	  &ngpppoe_init_data_state_type,
107 	  NULL
108 	},
109 	{
110 	  NGM_PPPOE_COOKIE,
111 	  NGM_PPPOE_LISTEN,
112 	  "pppoe_listen",
113 	  &ngpppoe_init_data_state_type,
114 	  NULL
115 	},
116 	{
117 	  NGM_PPPOE_COOKIE,
118 	  NGM_PPPOE_OFFER,
119 	  "pppoe_offer",
120 	  &ngpppoe_init_data_state_type,
121 	  NULL
122 	},
123 	{
124 	  NGM_PPPOE_COOKIE,
125 	  NGM_PPPOE_SERVICE,
126 	  "pppoe_service",
127 	  &ngpppoe_init_data_state_type,
128 	  NULL
129 	},
130 	{
131 	  NGM_PPPOE_COOKIE,
132 	  NGM_PPPOE_SUCCESS,
133 	  "pppoe_success",
134 	  &ng_pppoe_sts_state_type,
135 	  NULL
136 	},
137 	{
138 	  NGM_PPPOE_COOKIE,
139 	  NGM_PPPOE_FAIL,
140 	  "pppoe_fail",
141 	  &ng_pppoe_sts_state_type,
142 	  NULL
143 	},
144 	{
145 	  NGM_PPPOE_COOKIE,
146 	  NGM_PPPOE_CLOSE,
147 	  "pppoe_close",
148 	  &ng_pppoe_sts_state_type,
149 	  NULL
150 	},
151 	{
152 	  NGM_PPPOE_COOKIE,
153 	  NGM_PPPOE_SETMODE,
154 	  "pppoe_setmode",
155 	  &ng_parse_string_type,
156 	  NULL
157 	},
158 	{
159 	  NGM_PPPOE_COOKIE,
160 	  NGM_PPPOE_GETMODE,
161 	  "pppoe_getmode",
162 	  NULL,
163 	  &ng_parse_string_type
164 	},
165 	{
166 	  NGM_PPPOE_COOKIE,
167 	  NGM_PPPOE_SETENADDR,
168 	  "setenaddr",
169 	  &ng_parse_enaddr_type,
170 	  NULL
171 	},
172 	{ 0 }
173 };
174 
175 /* Netgraph node type descriptor */
176 static struct ng_type typestruct = {
177 	.version =	NG_ABI_VERSION,
178 	.name =		NG_PPPOE_NODE_TYPE,
179 	.constructor =	ng_pppoe_constructor,
180 	.rcvmsg =	ng_pppoe_rcvmsg,
181 	.shutdown =	ng_pppoe_shutdown,
182 	.newhook =	ng_pppoe_newhook,
183 	.connect =	ng_pppoe_connect,
184 	.rcvdata =	ng_pppoe_rcvdata,
185 	.disconnect =	ng_pppoe_disconnect,
186 	.cmdlist =	ng_pppoe_cmds,
187 };
188 NETGRAPH_INIT(pppoe, &typestruct);
189 
190 /*
191  * States for the session state machine.
192  * These have no meaning if there is no hook attached yet.
193  */
194 enum state {
195     PPPOE_SNONE=0,	/* [both] Initial state */
196     PPPOE_LISTENING,	/* [Daemon] Listening for discover initiation pkt */
197     PPPOE_SINIT,	/* [Client] Sent discovery initiation */
198     PPPOE_PRIMED,	/* [Server] Awaiting PADI from daemon */
199     PPPOE_SOFFER,	/* [Server] Sent offer message  (got PADI)*/
200     PPPOE_SREQ,		/* [Client] Sent a Request */
201     PPPOE_NEWCONNECTED,	/* [Server] Connection established, No data received */
202     PPPOE_CONNECTED,	/* [Both] Connection established, Data received */
203     PPPOE_DEAD		/* [Both] */
204 };
205 
206 #define NUMTAGS 20 /* number of tags we are set up to work with */
207 
208 /*
209  * Information we store for each hook on each node for negotiating the
210  * session. The mbuf and cluster are freed once negotiation has completed.
211  * The whole negotiation block is then discarded.
212  */
213 
214 struct sess_neg {
215 	struct mbuf 		*m; /* holds cluster with last sent packet */
216 	union	packet		*pkt; /* points within the above cluster */
217 	struct callout		handle;   /* see timeout(9) */
218 	u_int			timeout; /* 0,1,2,4,8,16 etc. seconds */
219 	u_int			numtags;
220 	const struct pppoe_tag	*tags[NUMTAGS];
221 	u_int			service_len;
222 	u_int			ac_name_len;
223 
224 	struct datatag		service;
225 	struct datatag		ac_name;
226 };
227 typedef struct sess_neg *negp;
228 
229 /*
230  * Session information that is needed after connection.
231  */
232 struct sess_con {
233 	hook_p  		hook;
234 	uint16_t		Session_ID;
235 	enum state		state;
236 	ng_ID_t			creator;	/* who to notify */
237 	struct pppoe_full_hdr	pkt_hdr;	/* used when connected */
238 	negp			neg;		/* used when negotiating */
239 	LIST_ENTRY(sess_con)	sessions;
240 };
241 typedef struct sess_con *sessp;
242 
243 #define SESSHASHSIZE	0x0100
244 #define SESSHASH(x)	(((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
245 
246 struct sess_hash_entry {
247 	struct mtx	mtx;
248 	LIST_HEAD(hhead, sess_con) head;
249 };
250 
251 /*
252  * Information we store for each node
253  */
254 struct PPPoE {
255 	node_p		node;		/* back pointer to node */
256 	hook_p  	ethernet_hook;
257 	hook_p  	debug_hook;
258 	u_int   	packets_in;	/* packets in from ethernet */
259 	u_int   	packets_out;	/* packets out towards ethernet */
260 	uint32_t	flags;
261 #define	COMPAT_3COM	0x00000001
262 #define	COMPAT_DLINK	0x00000002
263 	struct ether_header	eh;
264 	LIST_HEAD(, sess_con) listeners;
265 	struct sess_hash_entry	sesshash[SESSHASHSIZE];
266 };
267 typedef struct PPPoE *priv_p;
268 
269 union uniq {
270 	char bytes[sizeof(void *)];
271 	void *pointer;
272 };
273 
274 #define	LEAVE(x) do { error = x; goto quit; } while(0)
275 static void	pppoe_start(sessp sp);
276 static void	pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
277 static const	struct pppoe_tag *scan_tags(sessp sp,
278 			const struct pppoe_hdr* ph);
279 static	int	pppoe_send_event(sessp sp, enum cmd cmdid);
280 
281 /*************************************************************************
282  * Some basic utilities  from the Linux version with author's permission.*
283  * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
284  ************************************************************************/
285 
286 
287 
288 /*
289  * Return the location where the next tag can be put
290  */
291 static __inline const struct pppoe_tag*
292 next_tag(const struct pppoe_hdr* ph)
293 {
294 	return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
295 	    + ntohs(ph->length));
296 }
297 
298 /*
299  * Look for a tag of a specific type.
300  * Don't trust any length the other end says,
301  * but assume we already sanity checked ph->length.
302  */
303 static const struct pppoe_tag*
304 get_tag(const struct pppoe_hdr* ph, uint16_t idx)
305 {
306 	const char *const end = (const char *)next_tag(ph);
307 	const struct pppoe_tag *pt = &ph->tag[0];
308 	const char *ptn;
309 
310 	/*
311 	 * Keep processing tags while a tag header will still fit.
312 	 */
313 	while((const char*)(pt + 1) <= end) {
314 		/*
315 		 * If the tag data would go past the end of the packet, abort.
316 		 */
317 		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
318 		if (ptn > end) {
319 			CTR2(KTR_NET, "%20s: invalid length for tag %d",
320 			    __func__, idx);
321 			return (NULL);
322 		}
323 		if (pt->tag_type == idx) {
324 			CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
325 			return (pt);
326 		}
327 
328 		pt = (const struct pppoe_tag*)ptn;
329 	}
330 
331 	CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
332 	return (NULL);
333 }
334 
335 /**************************************************************************
336  * Inlines to initialise or add tags to a session's tag list.
337  **************************************************************************/
338 /*
339  * Initialise the session's tag list.
340  */
341 static void
342 init_tags(sessp sp)
343 {
344 	KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
345 	sp->neg->numtags = 0;
346 }
347 
348 static void
349 insert_tag(sessp sp, const struct pppoe_tag *tp)
350 {
351 	negp neg = sp->neg;
352 	int i;
353 
354 	KASSERT(neg != NULL, ("%s: no neg", __func__));
355 	if ((i = neg->numtags++) < NUMTAGS) {
356 		neg->tags[i] = tp;
357 	} else {
358 		log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
359 		    "packet\n");
360 		neg->numtags--;
361 	}
362 }
363 
364 /*
365  * Make up a packet, using the tags filled out for the session.
366  *
367  * Assume that the actual pppoe header and ethernet header
368  * are filled out externally to this routine.
369  * Also assume that neg->wh points to the correct
370  * location at the front of the buffer space.
371  */
372 static void
373 make_packet(sessp sp) {
374 	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
375 	const struct pppoe_tag **tag;
376 	char *dp;
377 	int count;
378 	int tlen;
379 	uint16_t length = 0;
380 
381 	KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
382 	    ("%s: called from wrong state", __func__));
383 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
384 
385 	dp = (char *)wh->ph.tag;
386 	for (count = 0, tag = sp->neg->tags;
387 	    ((count < sp->neg->numtags) && (count < NUMTAGS));
388 	    tag++, count++) {
389 		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
390 		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
391 			log(LOG_NOTICE, "ng_pppoe: tags too long\n");
392 			sp->neg->numtags = count;
393 			break;	/* XXX chop off what's too long */
394 		}
395 		bcopy(*tag, (char *)dp, tlen);
396 		length += tlen;
397 		dp += tlen;
398 	}
399  	wh->ph.length = htons(length);
400 	sp->neg->m->m_len = length + sizeof(*wh);
401 	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
402 }
403 
404 /**************************************************************************
405  * Routines to match a service.						  *
406  **************************************************************************/
407 
408 /*
409  * Find a hook that has a service string that matches that
410  * we are seeking. For now use a simple string.
411  * In the future we may need something like regexp().
412  *
413  * Null string is a wildcard (ANY service), according to RFC2516.
414  * And historical FreeBSD wildcard is also "*".
415  */
416 
417 static hook_p
418 pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
419 {
420 	const priv_p privp = NG_NODE_PRIVATE(node);
421 	sessp sp;
422 
423 	LIST_FOREACH(sp, &privp->listeners, sessions) {
424 		negp neg = sp->neg;
425 
426 		/* Empty Service-Name matches any service. */
427 		if (neg->service_len == 0)
428 			break;
429 
430 		/* Special case for a blank or "*" service name (wildcard). */
431 		if (neg->service_len == 1 && neg->service.data[0] == '*')
432 			break;
433 
434 		/* If the lengths don't match, that aint it. */
435 		if (neg->service_len != ntohs(tag->tag_len))
436 			continue;
437 
438 		if (strncmp(tag->tag_data, neg->service.data,
439 		    ntohs(tag->tag_len)) == 0)
440 			break;
441 	}
442 	CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
443 	    sp?sp->hook:NULL, tag->tag_data);
444 
445 	return (sp?sp->hook:NULL);
446 }
447 
448 /*
449  * Broadcast the PADI packet in m0 to all listening hooks.
450  * This routine is called when a PADI with empty Service-Name
451  * tag is received. Client should receive PADOs with all
452  * available services.
453  */
454 static int
455 pppoe_broadcast_padi(node_p node, struct mbuf *m0)
456 {
457 	const priv_p privp = NG_NODE_PRIVATE(node);
458 	sessp sp;
459 	int error = 0;
460 
461 	LIST_FOREACH(sp, &privp->listeners, sessions) {
462 		struct mbuf *m;
463 
464 		m = m_dup(m0, MB_DONTWAIT);
465 		if (m == NULL)
466 			return (ENOMEM);
467 		NG_SEND_DATA_ONLY(error, sp->hook, m);
468 		if (error)
469 			return (error);
470 	}
471 
472 	return (0);
473 }
474 
475 /*
476  * Find a hook, which name equals to given service.
477  */
478 static hook_p
479 pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
480 {
481 	const priv_p privp = NG_NODE_PRIVATE(node);
482 	sessp sp;
483 
484 	LIST_FOREACH(sp, &privp->listeners, sessions) {
485 		negp neg = sp->neg;
486 
487 		if (neg->service_len == svc_len &&
488 		    strncmp(svc_name, neg->service.data, svc_len) == 0)
489 			return (sp->hook);
490 	}
491 
492 	return (NULL);
493 }
494 
495 /**************************************************************************
496  * Routines to find a particular session that matches an incoming packet. *
497  **************************************************************************/
498 /* Find free session and add to hash. */
499 static uint16_t
500 pppoe_getnewsession(sessp sp)
501 {
502 	const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
503 	static uint16_t pppoe_sid = 1;
504 	sessp	tsp;
505 	uint16_t val, hash;
506 
507 restart:
508 	/* Atomicity is not needed here as value will be checked. */
509 	val = pppoe_sid++;
510 	/* Spec says 0xFFFF is reserved, also don't use 0x0000. */
511 	if (val == 0xffff || val == 0x0000)
512 		val = pppoe_sid = 1;
513 
514 	/* Check it isn't already in use. */
515 	hash = SESSHASH(val);
516 	mtx_lock(&privp->sesshash[hash].mtx);
517 	LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
518 		if (tsp->Session_ID == val)
519 			break;
520 	}
521 	if (!tsp) {
522 		sp->Session_ID = val;
523 		LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
524 	}
525 	mtx_unlock(&privp->sesshash[hash].mtx);
526 	if (tsp)
527 		goto restart;
528 
529 	CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
530 
531 	return (val);
532 }
533 
534 /* Add specified session to hash. */
535 static void
536 pppoe_addsession(sessp sp)
537 {
538 	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
539 	uint16_t	hash = SESSHASH(sp->Session_ID);
540 
541 	mtx_lock(&privp->sesshash[hash].mtx);
542 	LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
543 	mtx_unlock(&privp->sesshash[hash].mtx);
544 }
545 
546 /* Delete specified session from hash. */
547 static void
548 pppoe_delsession(sessp sp)
549 {
550 	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
551 	uint16_t	hash = SESSHASH(sp->Session_ID);
552 
553 	mtx_lock(&privp->sesshash[hash].mtx);
554 	LIST_REMOVE(sp, sessions);
555 	mtx_unlock(&privp->sesshash[hash].mtx);
556 }
557 
558 /* Find matching peer/session combination. */
559 static sessp
560 pppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
561 {
562 	uint16_t 	session = ntohs(wh->ph.sid);
563 	uint16_t	hash = SESSHASH(session);
564 	sessp		sp = NULL;
565 
566 	mtx_lock(&privp->sesshash[hash].mtx);
567 	LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
568 		if (sp->Session_ID == session &&
569 		    bcmp(sp->pkt_hdr.eh.ether_dhost,
570 		     wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
571 			break;
572 		}
573 	}
574 	mtx_unlock(&privp->sesshash[hash].mtx);
575 	CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
576 	    session);
577 
578 	return (sp);
579 }
580 
581 static hook_p
582 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
583 {
584 	hook_p	hook = NULL;
585 	union uniq uniq;
586 
587 	bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
588 	/* Cycle through all known hooks. */
589 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
590 		/* Skip any nonsession hook. */
591 		if (NG_HOOK_PRIVATE(hook) == NULL)
592 			continue;
593 		if (uniq.pointer == NG_HOOK_PRIVATE(hook))
594 			break;
595 	}
596 	CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
597 
598 	return (hook);
599 }
600 
601 /**************************************************************************
602  * Start of Netgraph entrypoints.					  *
603  **************************************************************************/
604 
605 /*
606  * Allocate the private data structure and link it with node.
607  */
608 static int
609 ng_pppoe_constructor(node_p node)
610 {
611 	priv_p	privp;
612 	int	i;
613 
614 	/* Initialize private descriptor. */
615 	privp = kmalloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_NULLOK | M_ZERO);
616 	if (privp == NULL)
617 		return (ENOMEM);
618 
619 	/* Link structs together; this counts as our one reference to *node. */
620 	NG_NODE_SET_PRIVATE(node, privp);
621 	privp->node = node;
622 
623 	/* Initialize to standard mode. */
624 	memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
625 	privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
626 
627 	LIST_INIT(&privp->listeners);
628 	for (i = 0; i < SESSHASHSIZE; i++) {
629 	    mtx_init(&privp->sesshash[i].mtx);
630 	    LIST_INIT(&privp->sesshash[i].head);
631 	}
632 
633 	CTR3(KTR_NET, "%20s: created node [%x] (%p)",
634 	    __func__, node->nd_ID, node);
635 
636 	return (0);
637 }
638 
639 /*
640  * Give our ok for a hook to be added...
641  * point the hook's private info to the hook structure.
642  *
643  * The following hook names are special:
644  *  "ethernet":  the hook that should be connected to a NIC.
645  *  "debug":	copies of data sent out here  (when I write the code).
646  * All other hook names need only be unique. (the framework checks this).
647  */
648 static int
649 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
650 {
651 	const priv_p privp = NG_NODE_PRIVATE(node);
652 	sessp sp;
653 
654 	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
655 		privp->ethernet_hook = hook;
656 		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
657 	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
658 		privp->debug_hook = hook;
659 		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
660 	} else {
661 		/*
662 		 * Any other unique name is OK.
663 		 * The infrastructure has already checked that it's unique,
664 		 * so just allocate it and hook it in.
665 		 */
666 		sp = kmalloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_WAITOK | M_NULLOK | M_ZERO);
667 		if (sp == NULL)
668 			return (ENOMEM);
669 
670 		NG_HOOK_SET_PRIVATE(hook, sp);
671 		sp->hook = hook;
672 	}
673 	CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
674 	    __func__, node->nd_ID, node, name, hook);
675 
676 	return(0);
677 }
678 
679 /*
680  * Hook has been added successfully. Request the MAC address of
681  * the underlying Ethernet node.
682  */
683 static int
684 ng_pppoe_connect(hook_p hook)
685 {
686 	const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
687 	struct ng_mesg *msg;
688 	int error;
689 
690 	if (hook != privp->ethernet_hook)
691 		return (0);
692 
693 	/*
694 	 * If this is Ethernet hook, then request MAC address
695 	 * from our downstream.
696 	 */
697 	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_WAITOK | M_NULLOK);
698 	if (msg == NULL)
699 		return (ENOBUFS);
700 
701 	/*
702 	 * Our hook and peer hook have HK_INVALID flag set,
703 	 * so we can't use NG_SEND_MSG_HOOK() macro here.
704 	 */
705 	NG_SEND_MSG_ID(error, privp->node, msg,
706 	    NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
707 	    NG_NODE_ID(privp->node));
708 
709 	return (error);
710 }
711 /*
712  * Get a netgraph control message.
713  * Check it is one we understand. If needed, send a response.
714  * We sometimes save the address for an async action later.
715  * Always free the message.
716  */
717 static int
718 ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
719 {
720 	priv_p privp = NG_NODE_PRIVATE(node);
721 	struct ngpppoe_init_data *ourmsg = NULL;
722 	struct ng_mesg *resp = NULL;
723 	int error = 0;
724 	hook_p hook = NULL;
725 	sessp sp = NULL;
726 	negp neg = NULL;
727 	struct ng_mesg *msg;
728 
729 	NGI_GET_MSG(item, msg);
730 	CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
731 	    __func__, node->nd_ID, node, msg->header.cmd,
732 	    msg->header.typecookie);
733 
734 	/* Deal with message according to cookie and command. */
735 	switch (msg->header.typecookie) {
736 	case NGM_PPPOE_COOKIE:
737 		switch (msg->header.cmd) {
738 		case NGM_PPPOE_CONNECT:
739 		case NGM_PPPOE_LISTEN:
740 		case NGM_PPPOE_OFFER:
741 		case NGM_PPPOE_SERVICE:
742 			ourmsg = (struct ngpppoe_init_data *)msg->data;
743 			if (msg->header.arglen < sizeof(*ourmsg)) {
744 				log(LOG_ERR, "ng_pppoe[%x]: init data too "
745 				    "small\n", node->nd_ID);
746 				LEAVE(EMSGSIZE);
747 			}
748 			if (msg->header.arglen - sizeof(*ourmsg) >
749 			    PPPOE_SERVICE_NAME_SIZE) {
750 				log(LOG_ERR, "ng_pppoe[%x]: service name "
751 				    "too big\n", node->nd_ID);
752 				LEAVE(EMSGSIZE);
753 			}
754 			if (msg->header.arglen - sizeof(*ourmsg) <
755 			    ourmsg->data_len) {
756 				log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
757 				    "length, %d should be %zd\n", node->nd_ID,
758 				    ourmsg->data_len,
759 				    msg->header.arglen - sizeof (*ourmsg));
760 				LEAVE(EMSGSIZE);
761 			}
762 
763 			/* Make sure strcmp will terminate safely. */
764 			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
765 
766 			/* Find hook by name. */
767 			hook = ng_findhook(node, ourmsg->hook);
768 			if (hook == NULL)
769 				LEAVE(ENOENT);
770 
771 			sp = NG_HOOK_PRIVATE(hook);
772 			if (sp == NULL)
773 				LEAVE(EINVAL);
774 
775 			if (msg->header.cmd == NGM_PPPOE_LISTEN) {
776 				/*
777 				 * Ensure we aren't already listening for this
778 				 * service.
779 				 */
780 				if (pppoe_find_svc(node, ourmsg->data,
781 				    ourmsg->data_len) != NULL)
782 					LEAVE(EEXIST);
783 			}
784 
785 			/*
786 			 * PPPOE_SERVICE advertisments are set up
787 			 * on sessions that are in PRIMED state.
788 			 */
789 			if (msg->header.cmd == NGM_PPPOE_SERVICE)
790 				break;
791 
792 			if (sp->state != PPPOE_SNONE) {
793 				log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
794 				    "active\n", node->nd_ID);
795 				LEAVE(EISCONN);
796 			}
797 
798 			/*
799 			 * Set up prototype header.
800 			 */
801 			neg = kmalloc(sizeof(*neg), M_NETGRAPH_PPPOE,
802 			    M_WAITOK | M_NULLOK | M_ZERO);
803 
804 			if (neg == NULL)
805 				LEAVE(ENOMEM);
806 
807 			neg->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
808 			if (neg->m == NULL) {
809 				kfree(neg, M_NETGRAPH_PPPOE);
810 				LEAVE(ENOBUFS);
811 			}
812 			neg->m->m_pkthdr.rcvif = NULL;
813 			sp->neg = neg;
814 			ng_callout_init(&neg->handle);
815 			neg->m->m_len = sizeof(struct pppoe_full_hdr);
816 			neg->pkt = mtod(neg->m, union packet*);
817 			memcpy((void *)&neg->pkt->pkt_header.eh,
818 			    &privp->eh, sizeof(struct ether_header));
819 			neg->pkt->pkt_header.ph.ver = 0x1;
820 			neg->pkt->pkt_header.ph.type = 0x1;
821 			neg->pkt->pkt_header.ph.sid = 0x0000;
822 			neg->timeout = 0;
823 
824 			sp->creator = NGI_RETADDR(item);
825 		}
826 		switch (msg->header.cmd) {
827 		case NGM_PPPOE_GET_STATUS:
828 		    {
829 			struct ngpppoestat *stats;
830 
831 			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_WAITOK | M_NULLOK);
832 			if (!resp)
833 				LEAVE(ENOMEM);
834 
835 			stats = (struct ngpppoestat *) resp->data;
836 			stats->packets_in = privp->packets_in;
837 			stats->packets_out = privp->packets_out;
838 			break;
839 		    }
840 		case NGM_PPPOE_CONNECT:
841 		    {
842 			/*
843 			 * Check the hook exists and is Uninitialised.
844 			 * Send a PADI request, and start the timeout logic.
845 			 * Store the originator of this message so we can send
846 			 * a success of fail message to them later.
847 			 * Move the session to SINIT.
848 			 * Set up the session to the correct state and
849 			 * start it.
850 			 */
851 			int	i, acnlen = 0, acnsep = 0, srvlen;
852 			for (i = 0; i < ourmsg->data_len; i++) {
853 				if (ourmsg->data[i] == '\\') {
854 					acnlen = i;
855 					acnsep = 1;
856 					break;
857 				}
858 			}
859 			srvlen = ourmsg->data_len - acnlen - acnsep;
860 
861 			bcopy(ourmsg->data, neg->ac_name.data, acnlen);
862 			neg->ac_name_len = acnlen;
863 
864 			neg->service.hdr.tag_type = PTT_SRV_NAME;
865 			neg->service.hdr.tag_len = htons((uint16_t)srvlen);
866 			bcopy(ourmsg->data + acnlen + acnsep,
867 			    neg->service.data, srvlen);
868 			neg->service_len = srvlen;
869 			pppoe_start(sp);
870 			break;
871 		    }
872 		case NGM_PPPOE_LISTEN:
873 			/*
874 			 * Check the hook exists and is Uninitialised.
875 			 * Install the service matching string.
876 			 * Store the originator of this message so we can send
877 			 * a success of fail message to them later.
878 			 * Move the hook to 'LISTENING'
879 			 */
880 			neg->service.hdr.tag_type = PTT_SRV_NAME;
881 			neg->service.hdr.tag_len =
882 			    htons((uint16_t)ourmsg->data_len);
883 
884 			if (ourmsg->data_len)
885 				bcopy(ourmsg->data, neg->service.data,
886 				    ourmsg->data_len);
887 			neg->service_len = ourmsg->data_len;
888 			neg->pkt->pkt_header.ph.code = PADT_CODE;
889 			/*
890 			 * Wait for PADI packet coming from Ethernet.
891 			 */
892 			sp->state = PPPOE_LISTENING;
893 			LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
894 			break;
895 		case NGM_PPPOE_OFFER:
896 			/*
897 			 * Check the hook exists and is Uninitialised.
898 			 * Store the originator of this message so we can send
899 			 * a success of fail message to them later.
900 			 * Store the AC-Name given and go to PRIMED.
901 			 */
902 			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
903 			neg->ac_name.hdr.tag_len =
904 			    htons((uint16_t)ourmsg->data_len);
905 			if (ourmsg->data_len)
906 				bcopy(ourmsg->data, neg->ac_name.data,
907 				    ourmsg->data_len);
908 			neg->ac_name_len = ourmsg->data_len;
909 			neg->pkt->pkt_header.ph.code = PADO_CODE;
910 			/*
911 			 * Wait for PADI packet coming from hook.
912 			 */
913 			sp->state = PPPOE_PRIMED;
914 			break;
915 		case NGM_PPPOE_SERVICE:
916 			/*
917 			 * Check the session is primed.
918 			 * for now just allow ONE service to be advertised.
919 			 * If you do it twice you just overwrite.
920 			 */
921 			if (sp->state != PPPOE_PRIMED) {
922 				log(LOG_NOTICE, "ng_pppoe[%x]: session not "
923 				    "primed\n", node->nd_ID);
924 				LEAVE(EISCONN);
925 			}
926 			neg = sp->neg;
927 			neg->service.hdr.tag_type = PTT_SRV_NAME;
928 			neg->service.hdr.tag_len =
929 			    htons((uint16_t)ourmsg->data_len);
930 
931 			if (ourmsg->data_len)
932 				bcopy(ourmsg->data, neg->service.data,
933 				    ourmsg->data_len);
934 			neg->service_len = ourmsg->data_len;
935 			break;
936 		case NGM_PPPOE_SETMODE:
937 		    {
938 			char *s;
939 			size_t len;
940 
941 			if (msg->header.arglen == 0)
942 				LEAVE(EINVAL);
943 
944 			s = (char *)msg->data;
945 			len = msg->header.arglen - 1;
946 
947 			/* Search for matching mode string. */
948 			if (len == strlen(NG_PPPOE_STANDARD) &&
949 			    (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
950 				privp->flags = 0;
951 				privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
952 				break;
953 			}
954 			if (len == strlen(NG_PPPOE_3COM) &&
955 			    (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
956 				privp->flags |= COMPAT_3COM;
957 				privp->eh.ether_type =
958 				    ETHERTYPE_PPPOE_3COM_DISC;
959 				break;
960 			}
961 			if (len == strlen(NG_PPPOE_DLINK) &&
962 			    (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
963 				privp->flags |= COMPAT_DLINK;
964 				break;
965 			}
966 			error = EINVAL;
967 			break;
968 		    }
969 		case NGM_PPPOE_GETMODE:
970 		    {
971 			char *s;
972 			size_t len = 0;
973 
974 			if (privp->flags == 0)
975 				len += strlen(NG_PPPOE_STANDARD) + 1;
976 			if (privp->flags & COMPAT_3COM)
977 				len += strlen(NG_PPPOE_3COM) + 1;
978 			if (privp->flags & COMPAT_DLINK)
979 				len += strlen(NG_PPPOE_DLINK) + 1;
980 
981 			NG_MKRESPONSE(resp, msg, len, M_WAITOK | M_NULLOK);
982 			if (resp == NULL)
983 				LEAVE(ENOMEM);
984 
985 			s = (char *)resp->data;
986 			if (privp->flags == 0) {
987 				len = strlen(NG_PPPOE_STANDARD);
988 				strlcpy(s, NG_PPPOE_STANDARD, len + 1);
989 				break;
990 			}
991 			if (privp->flags & COMPAT_3COM) {
992 				len = strlen(NG_PPPOE_3COM);
993 				strlcpy(s, NG_PPPOE_3COM, len + 1);
994 				s += len;
995 			}
996 			if (privp->flags & COMPAT_DLINK) {
997 				if (s != resp->data)
998 					*s++ = '|';
999 				len = strlen(NG_PPPOE_DLINK);
1000 				strlcpy(s, NG_PPPOE_DLINK, len + 1);
1001 			}
1002 			break;
1003 		    }
1004 		case NGM_PPPOE_SETENADDR:
1005 			if (msg->header.arglen != ETHER_ADDR_LEN)
1006 				LEAVE(EINVAL);
1007 			bcopy(msg->data, &privp->eh.ether_shost,
1008 			    ETHER_ADDR_LEN);
1009 			break;
1010 		default:
1011 			LEAVE(EINVAL);
1012 		}
1013 		break;
1014 	case NGM_ETHER_COOKIE:
1015 		if (!(msg->header.flags & NGF_RESP))
1016 			LEAVE(EINVAL);
1017 		switch (msg->header.cmd) {
1018 		case NGM_ETHER_GET_ENADDR:
1019 			if (msg->header.arglen != ETHER_ADDR_LEN)
1020 				LEAVE(EINVAL);
1021 			bcopy(msg->data, &privp->eh.ether_shost,
1022 			    ETHER_ADDR_LEN);
1023 			break;
1024 		default:
1025 			LEAVE(EINVAL);
1026 		}
1027 		break;
1028 	default:
1029 		LEAVE(EINVAL);
1030 	}
1031 
1032 	/* Take care of synchronous response, if any. */
1033 quit:
1034 	CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1035 	NG_RESPOND_MSG(error, node, item, resp);
1036 	/* Free the message and return. */
1037 	NG_FREE_MSG(msg);
1038 	return(error);
1039 }
1040 
1041 /*
1042  * Start a client into the first state. A separate function because
1043  * it can be needed if the negotiation times out.
1044  */
1045 static void
1046 pppoe_start(sessp sp)
1047 {
1048 	hook_p	hook = sp->hook;
1049 	node_p	node = NG_HOOK_NODE(hook);
1050 	priv_p	privp = NG_NODE_PRIVATE(node);
1051 	negp	neg = sp->neg;
1052 	struct {
1053 		struct pppoe_tag hdr;
1054 		union	uniq	data;
1055 	} __packed uniqtag;
1056 	struct  mbuf *m0;
1057 	int	error;
1058 
1059 	/*
1060 	 * Kick the state machine into starting up.
1061 	 */
1062 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1063 	sp->state = PPPOE_SINIT;
1064 	/*
1065 	 * Reset the packet header to broadcast. Since we are
1066 	 * in a client mode use configured ethertype.
1067 	 */
1068 	memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1069 	    sizeof(struct ether_header));
1070 	neg->pkt->pkt_header.ph.code = PADI_CODE;
1071 	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
1072 	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
1073 	uniqtag.data.pointer = sp;
1074 	init_tags(sp);
1075 	insert_tag(sp, &uniqtag.hdr);
1076 	insert_tag(sp, &neg->service.hdr);
1077 	make_packet(sp);
1078 	/*
1079 	 * Send packet and prepare to retransmit it after timeout.
1080 	 */
1081 	ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1082 	    pppoe_ticker, NULL, 0);
1083 	neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1084 	m0 = m_copypacket(neg->m, MB_DONTWAIT);
1085 	NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
1086 }
1087 
1088 static int
1089 send_acname(sessp sp, const struct pppoe_tag *tag)
1090 {
1091 	int error, tlen;
1092 	struct ng_mesg *msg;
1093 	struct ngpppoe_sts *sts;
1094 
1095 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1096 
1097 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1098 	    sizeof(struct ngpppoe_sts), M_WAITOK | M_NULLOK);
1099 	if (msg == NULL)
1100 		return (ENOMEM);
1101 
1102 	sts = (struct ngpppoe_sts *)msg->data;
1103 	tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1104 	strncpy(sts->hook, tag->tag_data, tlen);
1105 	sts->hook[tlen] = '\0';
1106 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1107 
1108 	return (error);
1109 }
1110 
1111 static int
1112 send_sessionid(sessp sp)
1113 {
1114 	int error;
1115 	struct ng_mesg *msg;
1116 
1117 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1118 
1119 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1120 	    sizeof(uint16_t), M_WAITOK | M_NULLOK);
1121 	if (msg == NULL)
1122 		return (ENOMEM);
1123 
1124 	*(uint16_t *)msg->data = sp->Session_ID;
1125 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1126 
1127 	return (error);
1128 }
1129 
1130 /*
1131  * Receive data from session hook and do something with it.
1132  */
1133 static int
1134 ng_pppoe_rcvdata(hook_p hook, item_p item)
1135 {
1136 	node_p			node = NG_HOOK_NODE(hook);
1137 	const priv_p		privp = NG_NODE_PRIVATE(node);
1138 	sessp			sp = NG_HOOK_PRIVATE(hook);
1139 	struct pppoe_full_hdr	*wh;
1140 	struct mbuf		*m;
1141 	int			error;
1142 
1143 	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1144 	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1145 
1146 	NGI_GET_M(item, m);
1147 	switch (sp->state) {
1148 	case	PPPOE_NEWCONNECTED:
1149 	case	PPPOE_CONNECTED: {
1150 		/*
1151 		 * Remove PPP address and control fields, if any.
1152 		 * For example, ng_ppp(4) always sends LCP packets
1153 		 * with address and control fields as required by
1154 		 * generic PPP. PPPoE is an exception to the rule.
1155 		 */
1156 		if (m->m_pkthdr.len >= 2) {
1157 			if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1158 				LEAVE(ENOBUFS);
1159 			if (mtod(m, u_char *)[0] == 0xff &&
1160 			    mtod(m, u_char *)[1] == 0x03)
1161 				m_adj(m, 2);
1162 		}
1163 		/*
1164 		 * Bang in a pre-made header, and set the length up
1165 		 * to be correct. Then send it to the ethernet driver.
1166 		 */
1167 		M_PREPEND(m, sizeof(*wh), MB_DONTWAIT);
1168 		if (m == NULL)
1169 			LEAVE(ENOBUFS);
1170 
1171 		wh = mtod(m, struct pppoe_full_hdr *);
1172 		bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1173 		wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1174 		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1175 		privp->packets_out++;
1176 		break;
1177 		}
1178 	case	PPPOE_PRIMED: {
1179 		struct {
1180 			struct pppoe_tag hdr;
1181 			union	uniq	data;
1182 		} __packed 	uniqtag;
1183 		const struct pppoe_tag	*tag;
1184 		struct mbuf 	*m0;
1185 		const struct pppoe_hdr	*ph;
1186 		negp		neg = sp->neg;
1187 	        uint16_t	session;
1188 		uint16_t	length;
1189 		uint8_t		code;
1190 
1191 		/*
1192 		 * A PADI packet is being returned by the application
1193 		 * that has set up this hook. This indicates that it
1194 		 * wants us to offer service.
1195 		 */
1196 		if (m->m_len < sizeof(*wh)) {
1197 			m = m_pullup(m, sizeof(*wh));
1198 			if (m == NULL)
1199 				LEAVE(ENOBUFS);
1200 		}
1201 		wh = mtod(m, struct pppoe_full_hdr *);
1202 		ph = &wh->ph;
1203 		session = ntohs(wh->ph.sid);
1204 		length = ntohs(wh->ph.length);
1205 		code = wh->ph.code;
1206 		/* Use peers mode in session. */
1207 		neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1208 		if (code != PADI_CODE)
1209 			LEAVE(EINVAL);
1210 		ng_uncallout(&neg->handle, node);
1211 
1212 		/*
1213 		 * This is the first time we hear
1214 		 * from the client, so note it's
1215 		 * unicast address, replacing the
1216 		 * broadcast address.
1217 		 */
1218 		bcopy(wh->eh.ether_shost,
1219 			neg->pkt->pkt_header.eh.ether_dhost,
1220 			ETHER_ADDR_LEN);
1221 		sp->state = PPPOE_SOFFER;
1222 		neg->timeout = 0;
1223 		neg->pkt->pkt_header.ph.code = PADO_CODE;
1224 
1225 		/*
1226 		 * Start working out the tags to respond with.
1227 		 */
1228 		uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1229 		uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1230 		uniqtag.data.pointer = sp;
1231 		init_tags(sp);
1232 		insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1233 		if ((tag = get_tag(ph, PTT_SRV_NAME)))
1234 			insert_tag(sp, tag);	  /* return service */
1235 		/*
1236 		 * If we have a NULL service request
1237 		 * and have an extra service defined in this hook,
1238 		 * then also add a tag for the extra service.
1239 		 * XXX this is a hack. eventually we should be able
1240 		 * to support advertising many services, not just one
1241 		 */
1242 		if (((tag == NULL) || (tag->tag_len == 0)) &&
1243 		    (neg->service.hdr.tag_len != 0)) {
1244 			insert_tag(sp, &neg->service.hdr); /* SERVICE */
1245 		}
1246 		if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1247 			insert_tag(sp, tag); /* returned hostunique */
1248 		insert_tag(sp, &uniqtag.hdr);
1249 		scan_tags(sp, ph);
1250 		make_packet(sp);
1251 		/*
1252 		 * Send the offer but if they don't respond
1253 		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1254 		 */
1255 		ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1256 		    pppoe_ticker, NULL, 0);
1257 		m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1258 		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1259 		privp->packets_out++;
1260 		break;
1261 		}
1262 
1263 	/*
1264 	 * Packets coming from the hook make no sense
1265 	 * to sessions in the rest of states. Throw them away.
1266 	 */
1267 	default:
1268 		LEAVE(ENETUNREACH);
1269 	}
1270 quit:
1271 	if (item)
1272 		NG_FREE_ITEM(item);
1273 	NG_FREE_M(m);
1274 	return (error);
1275 }
1276 
1277 /*
1278  * Receive data from ether and do something with it.
1279  */
1280 static int
1281 ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1282 {
1283 	node_p			node = NG_HOOK_NODE(hook);
1284 	const priv_p		privp = NG_NODE_PRIVATE(node);
1285 	sessp			sp = NG_HOOK_PRIVATE(hook);
1286 	const struct pppoe_tag	*utag = NULL, *tag = NULL;
1287 	const struct pppoe_full_hdr *wh;
1288 	const struct pppoe_hdr	*ph;
1289 	negp			neg = NULL;
1290 	struct mbuf		*m;
1291 	hook_p 			sendhook;
1292 	int			error = 0;
1293 	uint16_t		session;
1294 	uint16_t		length;
1295 	uint8_t			code;
1296 	struct	mbuf 		*m0;
1297 
1298 	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1299 	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1300 
1301 	NGI_GET_M(item, m);
1302 	/*
1303 	 * Dig out various fields from the packet.
1304 	 * Use them to decide where to send it.
1305 	 */
1306 	privp->packets_in++;
1307 	if( m->m_len < sizeof(*wh)) {
1308 		m = m_pullup(m, sizeof(*wh)); /* Checks length */
1309 		if (m == NULL) {
1310 			log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1311 			    "m_pullup(wh)\n", node->nd_ID);
1312 			LEAVE(ENOBUFS);
1313 		}
1314 	}
1315 	wh = mtod(m, struct pppoe_full_hdr *);
1316 	length = ntohs(wh->ph.length);
1317 	switch(wh->eh.ether_type) {
1318 	case	ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1319 	case	ETHERTYPE_PPPOE_DISC:
1320 		/*
1321 		 * We need to try to make sure that the tag area
1322 		 * is contiguous, or we could wander off the end
1323 		 * of a buffer and make a mess.
1324 		 * (Linux wouldn't have this problem).
1325 		 */
1326 		if (m->m_pkthdr.len <= MHLEN) {
1327 			if( m->m_len < m->m_pkthdr.len) {
1328 				m = m_pullup(m, m->m_pkthdr.len);
1329 				if (m == NULL) {
1330 					log(LOG_NOTICE, "ng_pppoe[%x]: "
1331 					    "couldn't m_pullup(pkthdr)\n",
1332 					    node->nd_ID);
1333 					LEAVE(ENOBUFS);
1334 				}
1335 			}
1336 		}
1337 		if (m->m_len != m->m_pkthdr.len) {
1338 			/*
1339 			 * It's not all in one piece.
1340 			 * We need to do extra work.
1341 			 * Put it into a cluster.
1342 			 */
1343 			struct mbuf *n;
1344 			n = m_dup(m, MB_DONTWAIT);
1345 			m_freem(m);
1346 			m = n;
1347 			if (m) {
1348 				/* just check we got a cluster */
1349 				if (m->m_len != m->m_pkthdr.len) {
1350 					m_freem(m);
1351 					m = NULL;
1352 				}
1353 			}
1354 			if (m == NULL) {
1355 				log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1356 				    "fragmented\n", node->nd_ID);
1357 				LEAVE(EMSGSIZE);
1358 			}
1359 		}
1360 		wh = mtod(m, struct pppoe_full_hdr *);
1361 		length = ntohs(wh->ph.length);
1362 		ph = &wh->ph;
1363 		session = ntohs(wh->ph.sid);
1364 		code = wh->ph.code;
1365 
1366 		switch(code) {
1367 		case	PADI_CODE:
1368 			/*
1369 			 * We are a server:
1370 			 * Look for a hook with the required service and send
1371 			 * the ENTIRE packet up there. It should come back to
1372 			 * a new hook in PRIMED state. Look there for further
1373 			 * processing.
1374 			 */
1375 			tag = get_tag(ph, PTT_SRV_NAME);
1376 			if (tag == NULL) {
1377 				CTR1(KTR_NET, "%20s: PADI w/o Service-Name",
1378 				    __func__);
1379 				LEAVE(ENETUNREACH);
1380 			}
1381 
1382 			/*
1383 			 * First, try to match Service-Name against our
1384 			 * listening hooks. If no success and we are in D-Link
1385 			 * compat mode and Service-Name is empty, then we
1386 			 * broadcast the PADI to all listening hooks.
1387 			 */
1388 			sendhook = pppoe_match_svc(node, tag);
1389 			if (sendhook != NULL)
1390 				NG_FWD_NEW_DATA(error, item, sendhook, m);
1391 			else if (privp->flags & COMPAT_DLINK &&
1392 				 ntohs(tag->tag_len) == 0)
1393 				error = pppoe_broadcast_padi(node, m);
1394 			else
1395 				error = ENETUNREACH;
1396 			break;
1397 		case	PADO_CODE:
1398 			/*
1399 			 * We are a client:
1400 			 * Use the host_uniq tag to find the hook this is in
1401 			 * response to. Received #2, now send #3
1402 			 * For now simply accept the first we receive.
1403 			 */
1404 			utag = get_tag(ph, PTT_HOST_UNIQ);
1405 			if ((utag == NULL) ||
1406 			    (ntohs(utag->tag_len) != sizeof(sp))) {
1407 				log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1408 				    "unique field\n", node->nd_ID);
1409 				LEAVE(ENETUNREACH);
1410 			}
1411 
1412 			sendhook = pppoe_finduniq(node, utag);
1413 			if (sendhook == NULL) {
1414 				log(LOG_NOTICE, "ng_pppoe[%x]: no "
1415 				    "matching session\n", node->nd_ID);
1416 				LEAVE(ENETUNREACH);
1417 			}
1418 
1419 			/*
1420 			 * Check the session is in the right state.
1421 			 * It needs to be in PPPOE_SINIT.
1422 			 */
1423 			sp = NG_HOOK_PRIVATE(sendhook);
1424 			if (sp->state == PPPOE_SREQ ||
1425 			    sp->state == PPPOE_CONNECTED) {
1426 				break;	/* Multiple PADO is OK. */
1427 			}
1428 			if (sp->state != PPPOE_SINIT) {
1429 				log(LOG_NOTICE, "ng_pppoe[%x]: session "
1430 				    "in wrong state\n", node->nd_ID);
1431 				LEAVE(ENETUNREACH);
1432 			}
1433 			neg = sp->neg;
1434 			/* If requested specific AC-name, check it. */
1435 			if (neg->ac_name_len) {
1436 				tag = get_tag(ph, PTT_AC_NAME);
1437 				if (!tag) {
1438 					/* No PTT_AC_NAME in PADO */
1439 					break;
1440 				}
1441 				if (neg->ac_name_len != htons(tag->tag_len) ||
1442 				    strncmp(neg->ac_name.data, tag->tag_data,
1443 				    neg->ac_name_len) != 0) {
1444 					break;
1445 				}
1446 			}
1447 			sp->state = PPPOE_SREQ;
1448 			ng_uncallout(&neg->handle, node);
1449 
1450 			/*
1451 			 * This is the first time we hear
1452 			 * from the server, so note it's
1453 			 * unicast address, replacing the
1454 			 * broadcast address .
1455 			 */
1456 			bcopy(wh->eh.ether_shost,
1457 				neg->pkt->pkt_header.eh.ether_dhost,
1458 				ETHER_ADDR_LEN);
1459 			neg->timeout = 0;
1460 			neg->pkt->pkt_header.ph.code = PADR_CODE;
1461 			init_tags(sp);
1462 			insert_tag(sp, utag);      	/* Host Unique */
1463 			if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1464 				insert_tag(sp, tag); 	/* return cookie */
1465 			if ((tag = get_tag(ph, PTT_AC_NAME))) {
1466 				insert_tag(sp, tag); 	/* return it */
1467 				send_acname(sp, tag);
1468 			}
1469 			insert_tag(sp, &neg->service.hdr); /* Service */
1470 			scan_tags(sp, ph);
1471 			make_packet(sp);
1472 			sp->state = PPPOE_SREQ;
1473 			ng_callout(&neg->handle, node, sp->hook,
1474 			    PPPOE_INITIAL_TIMEOUT * hz,
1475 			    pppoe_ticker, NULL, 0);
1476 			neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1477 			m0 = m_copypacket(neg->m, MB_DONTWAIT);
1478 			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1479 			break;
1480 		case	PADR_CODE:
1481 			/*
1482 			 * We are a server:
1483 			 * Use the ac_cookie tag to find the
1484 			 * hook this is in response to.
1485 			 */
1486 			utag = get_tag(ph, PTT_AC_COOKIE);
1487 			if ((utag == NULL) ||
1488 			    (ntohs(utag->tag_len) != sizeof(sp))) {
1489 				LEAVE(ENETUNREACH);
1490 			}
1491 
1492 			sendhook = pppoe_finduniq(node, utag);
1493 			if (sendhook == NULL)
1494 				LEAVE(ENETUNREACH);
1495 
1496 			/*
1497 			 * Check the session is in the right state.
1498 			 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1499 			 * If the latter, then this is a retry by the client,
1500 			 * so be nice, and resend.
1501 			 */
1502 			sp = NG_HOOK_PRIVATE(sendhook);
1503 			if (sp->state == PPPOE_NEWCONNECTED) {
1504 				/*
1505 				 * Whoa! drop back to resend that PADS packet.
1506 				 * We should still have a copy of it.
1507 				 */
1508 				sp->state = PPPOE_SOFFER;
1509 			} else if (sp->state != PPPOE_SOFFER)
1510 				LEAVE (ENETUNREACH);
1511 			neg = sp->neg;
1512 			ng_uncallout(&neg->handle, node);
1513 			neg->pkt->pkt_header.ph.code = PADS_CODE;
1514 			if (sp->Session_ID == 0) {
1515 				neg->pkt->pkt_header.ph.sid =
1516 				    htons(pppoe_getnewsession(sp));
1517 			}
1518 			send_sessionid(sp);
1519 			neg->timeout = 0;
1520 			/*
1521 			 * start working out the tags to respond with.
1522 			 */
1523 			init_tags(sp);
1524 			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1525 			if ((tag = get_tag(ph, PTT_SRV_NAME)))
1526 				insert_tag(sp, tag);/* return service */
1527 			if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1528 				insert_tag(sp, tag); /* return it */
1529 			insert_tag(sp, utag);	/* ac_cookie */
1530 			scan_tags(sp, ph);
1531 			make_packet(sp);
1532 			sp->state = PPPOE_NEWCONNECTED;
1533 
1534 			/* Send the PADS without a timeout - we're now connected. */
1535 			m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1536 			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1537 
1538 			/*
1539 			 * Having sent the last Negotiation header,
1540 			 * Set up the stored packet header to be correct for
1541 			 * the actual session. But keep the negotialtion stuff
1542 			 * around in case we need to resend this last packet.
1543 			 * We'll discard it when we move from NEWCONNECTED
1544 			 * to CONNECTED
1545 			 */
1546 			sp->pkt_hdr = neg->pkt->pkt_header;
1547 			/* Configure ethertype depending on what
1548 			 * ethertype was used at discovery phase */
1549 			if (sp->pkt_hdr.eh.ether_type ==
1550 			    ETHERTYPE_PPPOE_3COM_DISC)
1551 				sp->pkt_hdr.eh.ether_type
1552 					= ETHERTYPE_PPPOE_3COM_SESS;
1553 			else
1554 				sp->pkt_hdr.eh.ether_type
1555 					= ETHERTYPE_PPPOE_SESS;
1556 			sp->pkt_hdr.ph.code = 0;
1557 			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1558 			break;
1559 		case	PADS_CODE:
1560 			/*
1561 			 * We are a client:
1562 			 * Use the host_uniq tag to find the hook this is in
1563 			 * response to. Take the session ID and store it away.
1564 			 * Also make sure the pre-made header is correct and
1565 			 * set us into Session mode.
1566 			 */
1567 			utag = get_tag(ph, PTT_HOST_UNIQ);
1568 			if ((utag == NULL) ||
1569 			    (ntohs(utag->tag_len) != sizeof(sp))) {
1570 				LEAVE (ENETUNREACH);
1571 			}
1572 			sendhook = pppoe_finduniq(node, utag);
1573 			if (sendhook == NULL)
1574 				LEAVE(ENETUNREACH);
1575 
1576 			/*
1577 			 * Check the session is in the right state.
1578 			 * It needs to be in PPPOE_SREQ.
1579 			 */
1580 			sp = NG_HOOK_PRIVATE(sendhook);
1581 			if (sp->state != PPPOE_SREQ)
1582 				LEAVE(ENETUNREACH);
1583 			neg = sp->neg;
1584 			ng_uncallout(&neg->handle, node);
1585 			neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1586 			sp->Session_ID = ntohs(wh->ph.sid);
1587 			pppoe_addsession(sp);
1588 			send_sessionid(sp);
1589 			neg->timeout = 0;
1590 			sp->state = PPPOE_CONNECTED;
1591 			/*
1592 			 * Now we have gone to Connected mode,
1593 			 * Free all resources needed for negotiation.
1594 			 * Keep a copy of the header we will be using.
1595 			 */
1596 			sp->pkt_hdr = neg->pkt->pkt_header;
1597 			if (privp->flags & COMPAT_3COM)
1598 				sp->pkt_hdr.eh.ether_type
1599 					= ETHERTYPE_PPPOE_3COM_SESS;
1600 			else
1601 				sp->pkt_hdr.eh.ether_type
1602 					= ETHERTYPE_PPPOE_SESS;
1603 			sp->pkt_hdr.ph.code = 0;
1604 			m_freem(neg->m);
1605 			kfree(sp->neg, M_NETGRAPH_PPPOE);
1606 			sp->neg = NULL;
1607 			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1608 			break;
1609 		case	PADT_CODE:
1610 			/*
1611 			 * Find matching peer/session combination.
1612 			 */
1613 			sp = pppoe_findsession(privp, wh);
1614 			if (sp == NULL)
1615 				LEAVE(ENETUNREACH);
1616 			/* Disconnect that hook. */
1617 			ng_rmhook_self(sp->hook);
1618 			break;
1619 		default:
1620 			LEAVE(EPFNOSUPPORT);
1621 		}
1622 		break;
1623 	case	ETHERTYPE_PPPOE_3COM_SESS:
1624 	case	ETHERTYPE_PPPOE_SESS:
1625 		/*
1626 		 * Find matching peer/session combination.
1627 		 */
1628 		sp = pppoe_findsession(privp, wh);
1629 		if (sp == NULL)
1630 			LEAVE (ENETUNREACH);
1631 		m_adj(m, sizeof(*wh));
1632 
1633 		/* If packet too short, dump it. */
1634 		if (m->m_pkthdr.len < length)
1635 			LEAVE(EMSGSIZE);
1636 		/* Also need to trim excess at the end */
1637 		if (m->m_pkthdr.len > length) {
1638 			m_adj(m, -((int)(m->m_pkthdr.len - length)));
1639 		}
1640 		if ( sp->state != PPPOE_CONNECTED) {
1641 			if (sp->state == PPPOE_NEWCONNECTED) {
1642 				sp->state = PPPOE_CONNECTED;
1643 				/*
1644 				 * Now we have gone to Connected mode,
1645 				 * Free all resources needed for negotiation.
1646 				 * Be paranoid about whether there may be
1647 				 * a timeout.
1648 				 */
1649 				m_freem(sp->neg->m);
1650 				ng_uncallout(&sp->neg->handle, node);
1651 				kfree(sp->neg, M_NETGRAPH_PPPOE);
1652 				sp->neg = NULL;
1653 			} else {
1654 				LEAVE (ENETUNREACH);
1655 			}
1656 		}
1657 		NG_FWD_NEW_DATA(error, item, sp->hook, m);
1658 		break;
1659 	default:
1660 		LEAVE(EPFNOSUPPORT);
1661 	}
1662 quit:
1663 	if (item)
1664 		NG_FREE_ITEM(item);
1665 	NG_FREE_M(m);
1666 	return (error);
1667 }
1668 
1669 /*
1670  * Receive data from debug hook and bypass it to ether.
1671  */
1672 static int
1673 ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1674 {
1675 	node_p		node = NG_HOOK_NODE(hook);
1676 	const priv_p	privp = NG_NODE_PRIVATE(node);
1677 	int		error;
1678 
1679 	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1680 	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1681 
1682 	NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1683 	privp->packets_out++;
1684 	return (error);
1685 }
1686 
1687 /*
1688  * Do local shutdown processing..
1689  * If we are a persistant device, we might refuse to go away, and
1690  * we'd only remove our links and reset ourself.
1691  */
1692 static int
1693 ng_pppoe_shutdown(node_p node)
1694 {
1695 	const priv_p privp = NG_NODE_PRIVATE(node);
1696 	int	i;
1697 
1698 	for (i = 0; i < SESSHASHSIZE; i++)
1699 	    mtx_uninit(&privp->sesshash[i].mtx);
1700 	NG_NODE_SET_PRIVATE(node, NULL);
1701 	NG_NODE_UNREF(privp->node);
1702 	kfree(privp, M_NETGRAPH_PPPOE);
1703 	return (0);
1704 }
1705 
1706 /*
1707  * Hook disconnection
1708  *
1709  * Clean up all dangling links and information about the session/hook.
1710  * For this type, removal of the last link destroys the node.
1711  */
1712 static int
1713 ng_pppoe_disconnect(hook_p hook)
1714 {
1715 	node_p node = NG_HOOK_NODE(hook);
1716 	priv_p privp = NG_NODE_PRIVATE(node);
1717 	sessp	sp;
1718 
1719 	if (hook == privp->debug_hook) {
1720 		privp->debug_hook = NULL;
1721 	} else if (hook == privp->ethernet_hook) {
1722 		privp->ethernet_hook = NULL;
1723 		if (NG_NODE_IS_VALID(node))
1724 			ng_rmnode_self(node);
1725 	} else {
1726 		sp = NG_HOOK_PRIVATE(hook);
1727 		if (sp->state != PPPOE_SNONE ) {
1728 			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1729 		}
1730 		/*
1731 		 * According to the spec, if we are connected,
1732 		 * we should send a DISC packet if we are shutting down
1733 		 * a session.
1734 		 */
1735 		if ((privp->ethernet_hook)
1736 		&& ((sp->state == PPPOE_CONNECTED)
1737 		 || (sp->state == PPPOE_NEWCONNECTED))) {
1738 			struct mbuf *m;
1739 
1740 			/* Generate a packet of that type. */
1741 			MGETHDR(m, MB_DONTWAIT, MT_DATA);
1742 			if (m == NULL)
1743 				log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1744 				    "mbufs\n", node->nd_ID);
1745 			else {
1746 				struct pppoe_full_hdr *wh;
1747 				struct pppoe_tag *tag;
1748 				int	msglen = strlen(SIGNOFF);
1749 				int	error = 0;
1750 
1751 				m->m_pkthdr.rcvif = NULL;
1752 				m->m_pkthdr.len = m->m_len = sizeof(*wh);
1753 				wh = mtod(m, struct pppoe_full_hdr *);
1754 				bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1755 
1756 				/* Revert the stored header to DISC/PADT mode. */
1757 				wh->ph.code = PADT_CODE;
1758 				/*
1759 				 * Configure ethertype depending on what
1760 				 * was used during sessions stage.
1761 				 */
1762 				if (wh->eh.ether_type ==
1763 				    ETHERTYPE_PPPOE_3COM_SESS)
1764 					wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1765 				else
1766 					wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1767 				/*
1768 				 * Add a General error message and adjust
1769 				 * sizes.
1770 				 */
1771 				tag = wh->ph.tag;
1772 				tag->tag_type = PTT_GEN_ERR;
1773 				tag->tag_len = htons((u_int16_t)msglen);
1774 				strncpy(tag->tag_data, SIGNOFF, msglen);
1775 				m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1776 				    msglen);
1777 				wh->ph.length = htons(sizeof(*tag) + msglen);
1778 				NG_SEND_DATA_ONLY(error,
1779 					privp->ethernet_hook, m);
1780 			}
1781 		}
1782 		if (sp->state == PPPOE_LISTENING)
1783 			LIST_REMOVE(sp, sessions);
1784 		else if (sp->Session_ID)
1785 			pppoe_delsession(sp);
1786 		/*
1787 		 * As long as we have somewhere to store the timeout handle,
1788 		 * we may have a timeout pending.. get rid of it.
1789 		 */
1790 		if (sp->neg) {
1791 			ng_uncallout(&sp->neg->handle, node);
1792 			if (sp->neg->m)
1793 				m_freem(sp->neg->m);
1794 			kfree(sp->neg, M_NETGRAPH_PPPOE);
1795 		}
1796 		kfree(sp, M_NETGRAPH_PPPOE);
1797 		NG_HOOK_SET_PRIVATE(hook, NULL);
1798 	}
1799 	if ((NG_NODE_NUMHOOKS(node) == 0) &&
1800 	    (NG_NODE_IS_VALID(node)))
1801 		ng_rmnode_self(node);
1802 	return (0);
1803 }
1804 
1805 /*
1806  * Timeouts come here.
1807  */
1808 static void
1809 pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
1810 {
1811 	priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1812 	sessp	sp = NG_HOOK_PRIVATE(hook);
1813 	negp	neg = sp->neg;
1814 	struct mbuf *m0 = NULL;
1815 	int	error = 0;
1816 
1817 	CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1818 	    __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
1819 	switch(sp->state) {
1820 		/*
1821 		 * Resend the last packet, using an exponential backoff.
1822 		 * After a period of time, stop growing the backoff,
1823 		 * And either leave it, or revert to the start.
1824 		 */
1825 	case	PPPOE_SINIT:
1826 	case	PPPOE_SREQ:
1827 		/* Timeouts on these produce resends. */
1828 		m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1829 		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1830 		ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1831 		    pppoe_ticker, NULL, 0);
1832 		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1833 			if (sp->state == PPPOE_SREQ) {
1834 				/* Revert to SINIT mode. */
1835 				pppoe_start(sp);
1836 			} else {
1837 				neg->timeout = PPPOE_TIMEOUT_LIMIT;
1838 			}
1839 		}
1840 		break;
1841 	case	PPPOE_PRIMED:
1842 	case	PPPOE_SOFFER:
1843 		/* A timeout on these says "give up" */
1844 		ng_rmhook_self(hook);
1845 		break;
1846 	default:
1847 		/* Timeouts have no meaning in other states. */
1848 		log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1849 		    node->nd_ID);
1850 	}
1851 }
1852 
1853 /*
1854  * Parse an incoming packet to see if any tags should be copied to the
1855  * output packet. Don't do any tags that have been handled in the main
1856  * state machine.
1857  */
1858 static const struct pppoe_tag*
1859 scan_tags(sessp	sp, const struct pppoe_hdr* ph)
1860 {
1861 	const char *const end = (const char *)next_tag(ph);
1862 	const char *ptn;
1863 	const struct pppoe_tag *pt = &ph->tag[0];
1864 
1865 	/*
1866 	 * Keep processing tags while a tag header will still fit.
1867 	 */
1868 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1869 
1870 	while((const char*)(pt + 1) <= end) {
1871 		/*
1872 		 * If the tag data would go past the end of the packet, abort.
1873 		 */
1874 		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1875 		if(ptn > end)
1876 			return NULL;
1877 
1878 		switch (pt->tag_type) {
1879 		case	PTT_RELAY_SID:
1880 			insert_tag(sp, pt);
1881 			break;
1882 		case	PTT_EOL:
1883 			return NULL;
1884 		case	PTT_SRV_NAME:
1885 		case	PTT_AC_NAME:
1886 		case	PTT_HOST_UNIQ:
1887 		case	PTT_AC_COOKIE:
1888 		case	PTT_VENDOR:
1889 		case	PTT_SRV_ERR:
1890 		case	PTT_SYS_ERR:
1891 		case	PTT_GEN_ERR:
1892 			break;
1893 		}
1894 		pt = (const struct pppoe_tag*)ptn;
1895 	}
1896 	return NULL;
1897 }
1898 
1899 static	int
1900 pppoe_send_event(sessp sp, enum cmd cmdid)
1901 {
1902 	int error;
1903 	struct ng_mesg *msg;
1904 	struct ngpppoe_sts *sts;
1905 
1906 	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1907 
1908 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1909 			sizeof(struct ngpppoe_sts), M_WAITOK | M_NULLOK);
1910 	if (msg == NULL)
1911 		return (ENOMEM);
1912 	sts = (struct ngpppoe_sts *)msg->data;
1913 	strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1914 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1915 	return (error);
1916 }
1917