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