xref: /freebsd/sys/netgraph/ng_pppoe.c (revision bef9dae05a72866e79d97d9bff3fc5a2538b4421)
1 #define SIGNOFF "session closed"
2 /*
3  * ng_pppoe.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_pppoe.c,v 1.7 1999/10/16 10:16:43 julian Exp $
41  */
42 #if 0
43 #define AAA printf("pppoe: %s\n", __FUNCTION__ );
44 #define BBB printf("-%d-", __LINE__ );
45 #else
46 #define AAA
47 #define BBB
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/mbuf.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <net/ethernet.h>
58 
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_pppoe.h>
62 
63 /*
64  * This section contains the netgraph method declarations for the
65  * sample node. These methods define the netgraph 'type'.
66  */
67 
68 static int	ng_PPPoE_constructor(node_p *node);
69 static int	ng_PPPoE_rcvmsg(node_p node, struct ng_mesg *msg,
70 		  const char *retaddr, struct ng_mesg **resp);
71 static int	ng_PPPoE_rmnode(node_p node);
72 static int	ng_PPPoE_newhook(node_p node, hook_p hook, const char *name);
73 static int	ng_PPPoE_connect(hook_p hook);
74 static int	ng_PPPoE_rcvdata(hook_p hook, struct mbuf *m, meta_p meta);
75 static int	ng_PPPoE_disconnect(hook_p hook);
76 
77 /* Netgraph node type descriptor */
78 static struct ng_type typestruct = {
79 	NG_VERSION,
80 	NG_PPPOE_NODE_TYPE,
81 	NULL,
82 	ng_PPPoE_constructor,
83 	ng_PPPoE_rcvmsg,
84 	ng_PPPoE_rmnode,
85 	ng_PPPoE_newhook,
86 	NULL,
87 	ng_PPPoE_connect,
88 	ng_PPPoE_rcvdata,
89 	ng_PPPoE_rcvdata,
90 	ng_PPPoE_disconnect
91 };
92 NETGRAPH_INIT(PPPoE, &typestruct);
93 
94 /*
95  * States for the session state machine.
96  * These have no meaning if there is no hook attached yet.
97  */
98 enum state {
99     PPPOE_SNONE=0,	/* [both] Initial state */
100     PPPOE_SINIT,	/* [Client] Sent discovery initiation */
101     PPPOE_PRIMED,	/* [Server] Sent offer message */
102     PPPOE_SOFFER,	/* [Server] Sent offer message */
103     PPPOE_SREQ,		/* [Client] Sent a Request */
104     PPPOE_LISTENING,	/* [Server] Listening for discover initiation msg */
105     PPPOE_NEWCONNECTED,	/* [Both] Connection established, No data received */
106     PPPOE_CONNECTED,	/* [Both] Connection established, Data received */
107     PPPOE_DEAD		/* [Both] */
108 };
109 
110 #define NUMTAGS 20 /* number of tags we are set up to work with */
111 
112 /*
113  * Information we store for each hook on each node for negotiating the
114  * session. The mbuf and cluster are freed once negotiation has completed.
115  * The whole negotiation block is then discarded.
116  */
117 
118 struct sess_neg {
119 	struct mbuf 		*m; /* holds cluster with last sent packet */
120 	union	packet		*pkt; /* points within the above cluster */
121 	struct callout_handle	timeout_handle;   /* see timeout(9) */
122 	u_int			timeout; /* 0,1,2,4,8,16 etc. seconds */
123 	u_int			numtags;
124 	struct pppoe_tag	*tags[NUMTAGS];
125 	u_int			service_len;
126 	u_int			ac_name_len;
127 
128 	struct datatag		service;
129 	struct datatag		ac_name;
130 };
131 typedef struct sess_neg *negp;
132 
133 /*
134  * Session information that is needed after connection.
135  */
136 struct session {
137 	hook_p  		hook;
138 	u_int16_t		Session_ID;
139 	struct session		*hash_next; /* not yet uesed */
140 	enum state		state;
141 	char			creator[NG_NODELEN + 1]; /* who to notify */
142 	struct pppoe_full_hdr	pkt_hdr;	/* used when connected */
143 	negp			neg;		/* used when negotiating */
144 };
145 typedef struct session *sessp;
146 
147 /*
148  * Information we store for each node
149  */
150 struct PPPOE {
151 	node_p		node;		/* back pointer to node */
152 	hook_p  	ethernet_hook;
153 	hook_p  	debug_hook;
154 	u_int   	packets_in;	/* packets in from ethernet */
155 	u_int   	packets_out;	/* packets out towards ethernet */
156 	u_int32_t	flags;
157 	/*struct session *buckets[HASH_SIZE];*/	/* not yet used */
158 };
159 typedef struct PPPOE *priv_p;
160 
161 const struct ether_header eh_prototype =
162 	{{0xff,0xff,0xff,0xff,0xff,0xff},
163 	 {0x00,0x00,0x00,0x00,0x00,0x00},
164 	 ETHERTYPE_PPPOE_DISC};
165 
166 union uniq {
167 	char bytes[sizeof(void *)];
168 	void * pointer;
169 	};
170 
171 #define	LEAVE(x) do { error = x; goto quit; } while(0)
172 static void	pppoe_start(sessp sp);
173 static void	sendpacket(sessp sp);
174 static void	pppoe_ticker(void *arg);
175 static struct pppoe_tag* scan_tags(sessp	sp, struct pppoe_hdr* ph);
176 static	int	pppoe_send_event(sessp sp, enum cmd cmdid);
177 
178 /*************************************************************************
179  * Some basic utilities  from the Linux version with author's permission.*
180  * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
181  ************************************************************************/
182 
183 /*
184  * Generate a new session id
185  * XXX find out the freeBSD locking scheme.
186  */
187 static u_int16_t
188 get_new_sid(node_p node)
189 {
190 	static int pppoe_sid = 10;
191 	sessp sp;
192 	hook_p	hook;
193 	u_int16_t val;
194 	priv_p privp = node->private;
195 
196 AAA
197 restart:
198 	val = pppoe_sid++;
199 	/*
200 	 * Spec says 0xFFFF is reserved.
201 	 * Also don't use 0x0000
202 	 */
203 	if (val == 0xffff) {
204 		pppoe_sid = 20;
205 		goto restart;
206 	}
207 
208 	/* Check it isn't already in use */
209 	LIST_FOREACH(hook, &node->hooks, hooks) {
210 		/* don't check special hooks */
211 		if ((hook->private == &privp->debug_hook)
212 		||  (hook->private == &privp->ethernet_hook))
213 			continue;
214 		sp = hook->private;
215 		if (sp->Session_ID == val)
216 			goto restart;
217 	}
218 
219 	return val;
220 }
221 
222 
223 /*
224  * Return the location where the next tag can be put
225  */
226 static __inline struct pppoe_tag*
227 next_tag(struct pppoe_hdr* ph)
228 {
229 	return (struct pppoe_tag*)(((char*)&ph->tag[0]) + ntohs(ph->length));
230 }
231 
232 /*
233  * Look for a tag of a specific type
234  * Don't trust any length the other end says.
235  * but assume we already sanity checked ph->length.
236  */
237 static struct pppoe_tag*
238 get_tag(struct pppoe_hdr* ph, u_int16_t idx)
239 {
240 	char *end = (char *)next_tag(ph);
241 	char *ptn;
242 	struct pppoe_tag *pt = &ph->tag[0];
243 	/*
244 	 * Keep processing tags while a tag header will still fit.
245 	 */
246 AAA
247 	while((char*)(pt + 1) <= end) {
248 	    /*
249 	     * If the tag data would go past the end of the packet, abort.
250 	     */
251 	    ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
252 	    if(ptn > end)
253 		return NULL;
254 
255 	    if(pt->tag_type == idx)
256 		return pt;
257 
258 	    pt = (struct pppoe_tag*)ptn;
259 	}
260 	return NULL;
261 }
262 
263 /**************************************************************************
264  * inlines to initialise or add tags to a session's tag list,
265  **************************************************************************/
266 /*
267  * Initialise the session's tag list
268  */
269 static void
270 init_tags(sessp sp)
271 {
272 AAA
273 	if(sp->neg == NULL) {
274 		printf("pppoe: asked to init NULL neg pointer\n");
275 		return;
276 	}
277 	sp->neg->numtags = 0;
278 }
279 
280 static void
281 insert_tag(sessp sp, struct pppoe_tag *tp)
282 {
283 	int	i;
284 	negp neg;
285 
286 AAA
287 	if((neg = sp->neg) == NULL) {
288 		printf("pppoe: asked to use NULL neg pointer\n");
289 		return;
290 	}
291 	if ((i = neg->numtags++) < NUMTAGS) {
292 		neg->tags[i] = tp;
293 	} else {
294 		printf("pppoe: asked to add too many tags to packet\n");
295 	}
296 }
297 
298 /*
299  * Make up a packet, using the tags filled out for the session.
300  *
301  * Assume that the actual pppoe header and ethernet header
302  * are filled out externally to this routine.
303  * Also assume that neg->wh points to the correct
304  * location at the front of the buffer space.
305  */
306 static void
307 make_packet(sessp sp) {
308 	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
309 	struct pppoe_tag **tag;
310 	char *dp;
311 	int count;
312 	int tlen;
313 	u_int16_t length = 0;
314 
315 AAA
316 	if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
317 		printf("pppoe: make_packet called from wrong state\n");
318 	}
319 	dp = (char *)wh->ph.tag;
320 	for (count = 0, tag = sp->neg->tags;
321 	    ((count < sp->neg->numtags) && (count < NUMTAGS));
322 	    tag++, count++) {
323 		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
324 		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
325 			printf("pppoe: tags too long\n");
326 			sp->neg->numtags = count;
327 			break;	/* XXX chop off what's too long */
328 		}
329 		bcopy((char *)*tag, (char *)dp, tlen);
330 		length += tlen;
331 		dp += tlen;
332 	}
333  	wh->ph.length = htons(length);
334 	sp->neg->m->m_len = length + sizeof(*wh);
335 	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
336 }
337 
338 /**************************************************************************
339  * Routine to match a service offered					  *
340  **************************************************************************/
341 /*
342  * Find a hook that has a service string that matches that
343  * we are seeking. for now use a simple string.
344  * In the future we may need something like regexp().
345  * for testing allow a null string to match 1st found and a null service
346  * to match all requests. Also make '*' do the same.
347  */
348 static hook_p
349 pppoe_match_svc(node_p node, char *svc_name, int svc_len)
350 {
351 	sessp	sp	= NULL;
352 	negp	neg	= NULL;
353 	priv_p	privp	= node->private;
354 	hook_p hook;
355 
356 AAA
357 	LIST_FOREACH(hook, &node->hooks, hooks) {
358 
359 		/* skip any hook that is debug or ethernet */
360 		if ((hook->private == &privp->debug_hook)
361 		||  (hook->private == &privp->ethernet_hook))
362 			continue;
363 		sp = hook->private;
364 
365 		/* Skip any sessions which are not in LISTEN mode. */
366 		if ( sp->state != PPPOE_LISTENING)
367 			continue;
368 
369 		neg = sp->neg;
370 		/* XXX check validity of this */
371 		/* special case, NULL request. match 1st found. */
372 		if (svc_len == 0)
373 			break;
374 
375 		/* XXX check validity of this */
376 		/* Special case for a blank or "*" service name (wildcard) */
377 		if ((neg->service_len == 0)
378 		||  ((neg->service_len == 1)
379 		  && (neg->service.data[0] == '*'))) {
380 			break;
381 		}
382 
383 		/* If the lengths don't match, that aint it. */
384 		if (neg->service_len != svc_len)
385 			continue;
386 
387 		/* An exact match? */
388 		if (strncmp(svc_name, neg->service.data, svc_len) == 0)
389 			break;
390 	}
391 	return (hook);
392 }
393 /**************************************************************************
394  * Routine to find a particular session that matches an incoming packet	  *
395  **************************************************************************/
396 static hook_p
397 pppoe_findsession(node_p node, struct pppoe_full_hdr *wh)
398 {
399 	sessp	sp = NULL;
400 	hook_p hook = NULL;
401 	priv_p	privp = node->private;
402 	u_int16_t	session = ntohs(wh->ph.sid);
403 
404 	/*
405 	 * find matching peer/session combination.
406 	 */
407 AAA
408 	LIST_FOREACH(hook, &node->hooks, hooks) {
409 		/* don't check special hooks */
410 		if ((hook->private == &privp->debug_hook)
411 		||  (hook->private == &privp->ethernet_hook)) {
412 			continue;
413 		}
414 		sp = hook->private;
415 		if ( ( (sp->state == PPPOE_CONNECTED)
416 		    || (sp->state == PPPOE_NEWCONNECTED) )
417 		&& (sp->Session_ID == session)
418 		&& (bcmp(sp->pkt_hdr.eh.ether_dhost,
419 		    wh->eh.ether_shost,
420 		    ETHER_ADDR_LEN)) == 0) {
421 			break;
422 		}
423 	}
424 	return (hook);
425 }
426 
427 static hook_p
428 pppoe_finduniq(node_p node, struct pppoe_tag *tag)
429 {
430 	hook_p hook = NULL;
431 	priv_p	privp = node->private;
432 	union uniq		uniq;
433 
434 AAA
435 	bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
436 	/* cycle through all known hooks */
437 	LIST_FOREACH(hook, &node->hooks, hooks) {
438 		/* don't check special hooks */
439 		if ((hook->private == &privp->debug_hook)
440 		||  (hook->private == &privp->ethernet_hook))
441 			continue;
442 		if (uniq.pointer == hook->private)
443 			break;
444 	}
445 	return (hook);
446 }
447 
448 /**************************************************************************
449  * start of Netgraph entrypoints					  *
450  **************************************************************************/
451 
452 /*
453  * Allocate the private data structure and the generic node
454  * and link them together.
455  *
456  * ng_make_node_common() returns with a generic node struct
457  * with a single reference for us.. we transfer it to the
458  * private structure.. when we free the private struct we must
459  * unref the node so it gets freed too.
460  *
461  * If this were a device node than this work would be done in the attach()
462  * routine and the constructor would return EINVAL as you should not be able
463  * to creatednodes that depend on hardware (unless you can add the hardware :)
464  */
465 static int
466 ng_PPPoE_constructor(node_p *nodep)
467 {
468 	priv_p privdata;
469 	int error;
470 
471 AAA
472 	/* Initialize private descriptor */
473 	MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK);
474 	if (privdata == NULL)
475 		return (ENOMEM);
476 	bzero(privdata, sizeof(*privdata));
477 
478 	/* Call the 'generic' (ie, superclass) node constructor */
479 	if ((error = ng_make_node_common(&typestruct, nodep))) {
480 		FREE(privdata, M_NETGRAPH);
481 		return (error);
482 	}
483 
484 	/* Link structs together; this counts as our one reference to *nodep */
485 	(*nodep)->private = privdata;
486 	privdata->node = *nodep;
487 	return (0);
488 }
489 
490 /*
491  * Give our ok for a hook to be added...
492  * point the hook's private info to the hook structure.
493  *
494  * The following hook names are special:
495  *  Ethernet:  the hook that should be connected to a NIC.
496  *  debug:	copies of data sent out here  (when I write the code).
497  */
498 static int
499 ng_PPPoE_newhook(node_p node, hook_p hook, const char *name)
500 {
501 	const priv_p privp = node->private;
502 	sessp sp;
503 
504 AAA
505 	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
506 		privp->ethernet_hook = hook;
507 		hook->private = &privp->ethernet_hook;
508 	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
509 		privp->debug_hook = hook;
510 		hook->private = &privp->debug_hook;
511 	} else {
512 		/*
513 		 * Any other unique name is OK.
514 		 * The infrastructure has already checked that it's unique,
515 		 * so just allocate it and hook it in.
516 		 */
517 		MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_WAITOK);
518 		if (sp == NULL) {
519 				return (ENOMEM);
520 		}
521 		bzero(sp, sizeof(*sp));
522 
523 		hook->private = sp;
524 		sp->hook = hook;
525 	}
526 	return(0);
527 }
528 
529 /*
530  * Get a netgraph control message.
531  * Check it is one we understand. If needed, send a response.
532  * We sometimes save the address for an async action later.
533  * Always free the message.
534  */
535 static int
536 ng_PPPoE_rcvmsg(node_p node,
537 	   struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
538 {
539 	priv_p privp = node->private;
540 	struct ngPPPoE_init_data *ourmsg = NULL;
541 	struct ng_mesg *resp = NULL;
542 	int error = 0;
543 	hook_p hook = NULL;
544 	sessp sp = NULL;
545 	negp neg = NULL;
546 
547 AAA
548 	/* Deal with message according to cookie and command */
549 	switch (msg->header.typecookie) {
550 	case NGM_PPPOE_COOKIE:
551 		switch (msg->header.cmd) {
552 		case NGM_PPPOE_CONNECT:
553 		case NGM_PPPOE_LISTEN:
554 		case NGM_PPPOE_OFFER:
555 			ourmsg = (struct ngPPPoE_init_data *)msg->data;
556 			if (( sizeof(*ourmsg) > msg->header.arglen)
557 			|| ((sizeof(*ourmsg) + ourmsg->data_len)
558 			    > msg->header.arglen)) {
559 				printf("PPPoE_rcvmsg: bad arg size");
560 				LEAVE(EMSGSIZE);
561 			}
562 			if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
563 				printf("pppoe: init data too long (%d)\n",
564 							ourmsg->data_len);
565 				LEAVE(EMSGSIZE);
566 			}
567 			/* make sure strcmp will terminate safely */
568 			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
569 
570 			/* cycle through all known hooks */
571 			LIST_FOREACH(hook, &node->hooks, hooks) {
572 				if (hook->name
573 				&& strcmp(hook->name, ourmsg->hook) == 0)
574 					break;
575 			}
576 			if (hook == NULL) {
577 				LEAVE(ENOENT);
578 			}
579 			if ((hook->private == &privp->debug_hook)
580 			||  (hook->private == &privp->ethernet_hook)) {
581 				LEAVE(EINVAL);
582 			}
583 			sp = hook->private;
584 			if (sp->state |= PPPOE_SNONE) {
585 				printf("pppoe: Session already active\n");
586 				LEAVE(EISCONN);
587 			}
588 
589 			/*
590 			 * set up prototype header
591 			 */
592 			MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_WAITOK);
593 
594 			if (neg == NULL) {
595 				printf("pppoe: Session out of memory\n");
596 				LEAVE(ENOMEM);
597 			}
598 			bzero(neg, sizeof(*neg));
599 			MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
600 			if(neg->m == NULL) {
601 				printf("pppoe: Session out of mbufs\n");
602 				FREE(neg, M_NETGRAPH);
603 				LEAVE(ENOBUFS);
604 			}
605 			neg->m->m_pkthdr.rcvif = NULL;
606 			MCLGET(neg->m, M_DONTWAIT);
607 			if ((neg->m->m_flags & M_EXT) == 0) {
608 				printf("pppoe: Session out of mcls\n");
609 				m_freem(neg->m);
610 				FREE(neg, M_NETGRAPH);
611 				LEAVE(ENOBUFS);
612 			}
613 			sp->neg = neg;
614 			callout_handle_init( &neg->timeout_handle);
615 			neg->m->m_len = sizeof(struct pppoe_full_hdr);
616 			neg->pkt = mtod(neg->m, union packet*);
617 			neg->pkt->pkt_header.eh = eh_prototype;
618 			neg->pkt->pkt_header.ph.ver = 0x1;
619 			neg->pkt->pkt_header.ph.type = 0x1;
620 			neg->pkt->pkt_header.ph.sid = 0x0000;
621 			neg->timeout = 0;
622 
623 			strncpy(sp->creator, retaddr, NG_NODELEN);
624 			sp->creator[NG_NODELEN] = '\0';
625 		}
626 		switch (msg->header.cmd) {
627 		case NGM_PPPOE_GET_STATUS:
628 		    {
629 			struct ngPPPoEstat *stats;
630 
631 			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
632 			if (!resp) {
633 				LEAVE(ENOMEM);
634 			}
635 			stats = (struct ngPPPoEstat *) resp->data;
636 			stats->packets_in = privp->packets_in;
637 			stats->packets_out = privp->packets_out;
638 			break;
639 		    }
640 		case NGM_PPPOE_CONNECT:
641 			/*
642 			 * Check the hook exists and is Uninitialised.
643 			 * Send a PADI request, and start the timeout logic.
644 			 * Store the originator of this message so we can send
645 			 * a success of fail message to them later.
646 			 * Move the session to SINIT
647 			 * Set up the session to the correct state and
648 			 * start it.
649 			 */
650 			neg->service.hdr.tag_type = PTT_SRV_NAME;
651 			neg->service.hdr.tag_len =
652 					htons((u_int16_t)ourmsg->data_len);
653 			if (ourmsg->data_len) {
654 				bcopy(ourmsg->data,
655 					neg->service.data, ourmsg->data_len);
656 			}
657 			neg->service_len = ourmsg->data_len;
658 			pppoe_start(sp);
659 			break;
660 		case NGM_PPPOE_LISTEN:
661 			/*
662 			 * Check the hook exists and is Uninitialised.
663 			 * Install the service matching string.
664 			 * Store the originator of this message so we can send
665 			 * a success of fail message to them later.
666 			 * Move the hook to 'LISTENING'
667 
668 			 */
669 			neg->service.hdr.tag_type = PTT_SRV_NAME;
670 			neg->service.hdr.tag_len =
671 					htons((u_int16_t)ourmsg->data_len);
672 
673 			if (ourmsg->data_len) {
674 				bcopy(ourmsg->data,
675 					neg->service.data, ourmsg->data_len);
676 			}
677 			neg->service_len = ourmsg->data_len;
678 			neg->pkt->pkt_header.ph.code = PADT_CODE;
679 			/*
680 			 * wait for PADI packet coming from ethernet
681 			 */
682 			sp->state = PPPOE_LISTENING;
683 			break;
684 		case NGM_PPPOE_OFFER:
685 			/*
686 			 * Check the hook exists and is Uninitialised.
687 			 * Store the originator of this message so we can send
688 			 * a success of fail message to them later.
689 			 * Store the AC-Name given and go to PRIMED.
690 			 */
691 			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
692 			neg->ac_name.hdr.tag_len =
693 					htons((u_int16_t)ourmsg->data_len);
694 			if (ourmsg->data_len) {
695 				bcopy(ourmsg->data,
696 					neg->ac_name.data, ourmsg->data_len);
697 			}
698 			neg->ac_name_len = ourmsg->data_len;
699 			neg->pkt->pkt_header.ph.code = PADO_CODE;
700 			/*
701 			 * Wait for PADI packet coming from hook
702 			 */
703 			sp->state = PPPOE_PRIMED;
704 			break;
705 		default:
706 			LEAVE(EINVAL);
707 		}
708 		break;
709 	default:
710 		LEAVE(EINVAL);
711 	}
712 
713 	/* Take care of synchronous response, if any */
714 	if (rptr)
715 		*rptr = resp;
716 	else if (resp)
717 		FREE(resp, M_NETGRAPH);
718 
719 	/* Free the message and return */
720 quit:
721 	FREE(msg, M_NETGRAPH);
722 	return(error);
723 }
724 
725 /*
726  * Start a client into the first state. A separate function because
727  * it can be needed if the negotiation times out.
728  */
729 static void
730 pppoe_start(sessp sp)
731 {
732 	struct {
733 		struct pppoe_tag hdr;
734 		union	uniq	data;
735 	} uniqtag;
736 
737 	/*
738 	 * kick the state machine into starting up
739 	 */
740 AAA
741 	sp->state = PPPOE_SINIT;
742 	/* reset the packet header to broadcast */
743 	sp->neg->pkt->pkt_header.eh = eh_prototype;
744 	sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
745 	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
746 	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
747 	uniqtag.data.pointer = sp;
748 	init_tags(sp);
749 	insert_tag(sp, &uniqtag.hdr);
750 	insert_tag(sp, &sp->neg->service.hdr);
751 	make_packet(sp);
752 	sendpacket(sp);
753 }
754 
755 /*
756  * Receive data, and do something with it.
757  * The caller will never free m or meta, so
758  * if we use up this data or abort we must free BOTH of these.
759  */
760 static int
761 ng_PPPoE_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
762 {
763 	node_p			node = hook->node;
764 	const priv_p		privp = node->private;
765 	sessp			sp = hook->private;
766 	struct pppoe_full_hdr	*wh;
767 	struct pppoe_hdr	*ph;
768 	int			error = 0;
769 	u_int16_t		session;
770 	u_int16_t		length;
771 	u_int8_t		code;
772 	struct pppoe_tag	*tag = NULL;
773 	hook_p 			sendhook;
774 	struct {
775 		struct pppoe_tag hdr;
776 		union	uniq	data;
777 	} uniqtag;
778 	negp			neg = NULL;
779 
780 AAA
781 	if (hook->private == &privp->debug_hook) {
782 		/*
783 		 * Data from the debug hook gets sent without modification
784 		 * straight to the ethernet.
785 		 */
786 		NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
787 	 	privp->packets_out++;
788 	} else if (hook->private == &privp->ethernet_hook) {
789 		/*
790 		 * Incoming data.
791 		 * Dig out various fields from the packet.
792 		 * use them to decide where to send it.
793 		 */
794 
795  		privp->packets_in++;
796 		if( m->m_len < sizeof(*wh)) {
797 			m = m_pullup(m, sizeof(*wh)); /* Checks length */
798 			if (m == NULL) {
799 				printf("couldn't m_pullup\n");
800 				LEAVE(ENOBUFS);
801 			}
802 		}
803 		wh = mtod(m, struct pppoe_full_hdr *);
804 		ph = &wh->ph;
805 		session = ntohs(wh->ph.sid);
806 		length = ntohs(wh->ph.length);
807 		code = wh->ph.code;
808 		switch(wh->eh.ether_type) {
809 		case	ETHERTYPE_PPPOE_DISC:
810 			/*
811 			 * We need to try make sure that the tag area
812 			 * is contiguous, or we could wander of the end
813 			 * of a buffer and make a mess.
814 			 * (Linux wouldn't have this problem).
815 			 */
816 /*XXX fix this mess */
817 
818 			if (m->m_pkthdr.len <= MHLEN) {
819 				if( m->m_len < m->m_pkthdr.len) {
820 					m = m_pullup(m, m->m_pkthdr.len);
821 					if (m == NULL) {
822 						printf("couldn't m_pullup\n");
823 						LEAVE(ENOBUFS);
824 					}
825 				}
826 			}
827 			if (m->m_len != m->m_pkthdr.len) {
828 				/*
829 				 * It's not all in one piece.
830 				 * We need to do extra work.
831 				 */
832 				printf("packet fragmented\n");
833 				LEAVE(EMSGSIZE);
834 			 }
835 
836 			switch(code) {
837 			case	PADI_CODE:
838 				/*
839 				 * We are a server:
840 				 * Look for a hook with the required service
841 				 * and send the ENTIRE packet up there.
842 				 * It should come back to a new hook in
843 				 * PRIMED state. Look there for further
844 				 * processing.
845 				 */
846 				tag = get_tag(ph, PTT_SRV_NAME);
847 				if (tag == NULL) {
848 					printf("no service tag\n");
849 					LEAVE(ENETUNREACH);
850 				}
851 				sendhook = pppoe_match_svc(hook->node,
852 			    		tag->tag_data, ntohs(tag->tag_len));
853 				if (sendhook) {
854 					NG_SEND_DATA(error, sendhook, m, meta);
855 				} else {
856 					printf("no such service\n");
857 					LEAVE(ENETUNREACH);
858 				}
859 				break;
860 			case	PADO_CODE:
861 				/*
862 				 * We are a client:
863 				 * Use the host_uniq tag to find the
864 				 * hook this is in response to.
865 				 * Received #2, now send #3
866 				 * For now simply accept the first we receive.
867 				 */
868 				tag = get_tag(ph, PTT_HOST_UNIQ);
869 				if ((tag == NULL)
870 				|| (ntohs(tag->tag_len) != sizeof(sp))) {
871 					printf("no host unique field\n");
872 					LEAVE(ENETUNREACH);
873 				}
874 
875 				sendhook = pppoe_finduniq(node, tag);
876 				if (sendhook == NULL) {
877 					printf("no matching session\n");
878 					LEAVE(ENETUNREACH);
879 				}
880 
881 				/*
882 				 * Check the session is in the right state.
883 				 * It needs to be in PPPOE_SINIT.
884 				 */
885 				sp = sendhook->private;
886 				if (sp->state != PPPOE_SINIT) {
887 					printf("session in wrong state\n");
888 					LEAVE(ENETUNREACH);
889 				}
890 				neg = sp->neg;
891 				untimeout(pppoe_ticker, sendhook,
892 				    neg->timeout_handle);
893 
894 				/*
895 				 * This is the first time we hear
896 				 * from the server, so note it's
897 				 * unicast address, replacing the
898 				 * broadcast address .
899 				 */
900 				bcopy(wh->eh.ether_shost,
901 					neg->pkt->pkt_header.eh.ether_dhost,
902 					ETHER_ADDR_LEN);
903 				neg->timeout = 0;
904 				neg->pkt->pkt_header.ph.code = PADR_CODE;
905 				init_tags(sp);
906 				insert_tag(sp, &neg->service.hdr); /* Service */
907 				insert_tag(sp, tag);	      /* Host Unique */
908 				tag = get_tag(ph, PTT_AC_COOKIE);
909 				if (tag)
910 					insert_tag(sp, tag); /* return cookie */
911 				tag = get_tag(ph, PTT_AC_NAME);
912 				if (tag)
913 					insert_tag(sp, tag); /* return cookie */
914 				scan_tags(sp, ph);
915 				make_packet(sp);
916 				sp->state = PPPOE_SREQ;
917 				sendpacket(sp);
918 				break;
919 			case	PADR_CODE:
920 
921 				/*
922 				 * We are a server:
923 				 * Use the ac_cookie tag to find the
924 				 * hook this is in response to.
925 				 */
926 				tag = get_tag(ph, PTT_AC_COOKIE);
927 				if ((tag == NULL)
928 				|| (ntohs(tag->tag_len) != sizeof(sp))) {
929 					LEAVE(ENETUNREACH);
930 				}
931 
932 				sendhook = pppoe_finduniq(node, tag);
933 				if (sendhook == NULL) {
934 					LEAVE(ENETUNREACH);
935 				}
936 
937 				/*
938 				 * Check the session is in the right state.
939 				 * It needs to be in PPPOE_SOFFER
940 				 * or PPPOE_NEWCONNECTED. If the latter,
941 				 * then this is a retry by the client.
942 				 * so be nice, and resend.
943 				 */
944 				sp = sendhook->private;
945 				if (sp->state == PPPOE_NEWCONNECTED) {
946 					/*
947 					 * Whoa! drop back to resend that
948 					 * PADS packet.
949 					 * We should still have a copy of it.
950 					 */
951 					sp->state = PPPOE_SOFFER;
952 				}
953 				if (sp->state != PPPOE_SOFFER) {
954 					LEAVE (ENETUNREACH);
955 					break;
956 				}
957 				neg = sp->neg;
958 				untimeout(pppoe_ticker, sendhook,
959 				    neg->timeout_handle);
960 				neg->pkt->pkt_header.ph.code = PADS_CODE;
961 				if (sp->Session_ID == 0)
962 					neg->pkt->pkt_header.ph.sid =
963 					    htons(sp->Session_ID
964 						= get_new_sid(node));
965 				neg->timeout = 0;
966 				/*
967 				 * start working out the tags to respond with.
968 				 */
969 				init_tags(sp);
970 				insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
971 				insert_tag(sp, tag);	/* ac_cookie */
972 				tag = get_tag(ph, PTT_SRV_NAME);
973 				insert_tag(sp, tag);	/* returned service */
974 				tag = get_tag(ph, PTT_HOST_UNIQ);
975 				insert_tag(sp, tag);    /* returned hostuniq */
976 				scan_tags(sp, ph);
977 				make_packet(sp);
978 				sp->state = PPPOE_NEWCONNECTED;
979 				sendpacket(sp);
980 				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
981 				/*
982 				 * Having sent the last Negotiation header,
983 				 * Set up the stored packet header to
984 				 * be correct for the actual session.
985 				 * But keep the negotialtion stuff
986 				 * around in case we need to resend this last
987 				 * packet. We'll discard it when we move
988 				 * from NEWCONNECTED to CONNECTED
989 				 */
990 				sp->pkt_hdr = neg->pkt->pkt_header;
991 				sp->pkt_hdr.eh.ether_type
992 						= ETHERTYPE_PPPOE_SESS;
993 				sp->pkt_hdr.ph.code = 0;
994 				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
995 				break;
996 			case	PADS_CODE:
997 				/*
998 				 * We are a client:
999 				 * Use the host_uniq tag to find the
1000 				 * hook this is in response to.
1001 				 * take the session ID and store it away.
1002 				 * Also make sure the pre-made header is
1003 				 * correct and set us into Session mode.
1004 				 */
1005 				tag = get_tag(ph, PTT_HOST_UNIQ);
1006 				if ((tag == NULL)
1007 				|| (ntohs(tag->tag_len) != sizeof(sp))) {
1008 					LEAVE (ENETUNREACH);
1009 					break;
1010 				}
1011 
1012 				sendhook = pppoe_finduniq(node, tag);
1013 				if (sendhook == NULL) {
1014 					LEAVE(ENETUNREACH);
1015 				}
1016 
1017 				/*
1018 				 * Check the session is in the right state.
1019 				 * It needs to be in PPPOE_SREQ.
1020 				 */
1021 				sp = sendhook->private;
1022 				if (sp->state != PPPOE_SREQ) {
1023 					LEAVE(ENETUNREACH);
1024 				}
1025 				neg = sp->neg;
1026 				untimeout(pppoe_ticker, sendhook,
1027 				    neg->timeout_handle);
1028 				neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1029 				sp->Session_ID = ntohs(wh->ph.sid);
1030 				neg->timeout = 0;
1031 				sp->state = PPPOE_CONNECTED;
1032 				/*
1033 				 * Now we have gone to Connected mode,
1034 				 * Free all resources needed for
1035 				 * negotiation.
1036 				 * Keep a copy of the header we will be using.
1037 				 */
1038 				sp->pkt_hdr = neg->pkt->pkt_header;
1039 				sp->pkt_hdr.eh.ether_type
1040 						= ETHERTYPE_PPPOE_SESS;
1041 				sp->pkt_hdr.ph.code = 0;
1042 				m_freem(neg->m);
1043 				FREE(sp->neg, M_NETGRAPH);
1044 				sp->neg = NULL;
1045 				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1046 				break;
1047 			case	PADT_CODE:
1048 				/*
1049 				 * Send a 'close' message to the controlling
1050 				 * process (the one that set us up);
1051 				 * And then tear everything down.
1052 				 *
1053 				 * Find matching peer/session combination.
1054 				 */
1055 				sendhook = pppoe_findsession(node, wh);
1056 				NG_FREE_DATA(m, meta); /* no longer needed */
1057 				if (sendhook == NULL) {
1058 					LEAVE(ENETUNREACH);
1059 				}
1060 				/* send message to creator */
1061 				/* close hook */
1062 				if (sendhook) {
1063 					ng_destroy_hook(sendhook);
1064 				}
1065 				break;
1066 			default:
1067 				LEAVE(EPFNOSUPPORT);
1068 			}
1069 			break;
1070 		case	ETHERTYPE_PPPOE_SESS:
1071 			/*
1072 			 * find matching peer/session combination.
1073 			 */
1074 			sendhook = pppoe_findsession(node, wh);
1075 			if (sendhook == NULL) {
1076 				LEAVE (ENETUNREACH);
1077 				break;
1078 			}
1079 			sp = sendhook->private;
1080 			m_adj(m, sizeof(*wh));
1081 			if (m->m_pkthdr.len < length) {
1082 				/* Packet too short, dump it */
1083 				LEAVE(EMSGSIZE);
1084 			}
1085 
1086 			if (m->m_pkthdr.len > length) {
1087 				m_adj(m, -((int)(m->m_pkthdr.len - length)));
1088 			}
1089 			/* XXX also need to trim excess at end I should think */
1090 			if ( sp->state != PPPOE_CONNECTED) {
1091 				if (sp->state == PPPOE_NEWCONNECTED) {
1092 					sp->state = PPPOE_CONNECTED;
1093 					/*
1094 					 * Now we have gone to Connected mode,
1095 					 * Free all resources needed for
1096 					 * negotiation.
1097 					 */
1098 					m_freem(sp->neg->m);
1099 					FREE(sp->neg, M_NETGRAPH);
1100 					sp->neg = NULL;
1101 				} else {
1102 					LEAVE (ENETUNREACH);
1103 					break;
1104 				}
1105 			}
1106 			NG_SEND_DATA( error, sendhook, m, meta);
1107 			break;
1108 		default:
1109 			LEAVE(EPFNOSUPPORT);
1110 		}
1111 	} else {
1112 		/*
1113 		 * 	Not ethernet or debug hook..
1114 		 *
1115 		 * The packet has come in on a normal hook.
1116 		 * We need to find out what kind of hook,
1117 		 * So we can decide how to handle it.
1118 		 * Check the hook's state.
1119 		 */
1120 		sp = hook->private;
1121 		switch (sp->state) {
1122 		case	PPPOE_NEWCONNECTED:
1123 		case	PPPOE_CONNECTED: {
1124 			struct pppoe_full_hdr *wh;
1125 			/*
1126 			 * Bang in a pre-made header, and set the length up
1127 			 * to be correct. Then send it to the ethernet driver.
1128 			 */
1129 			M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1130 			if (m == NULL) {
1131 				LEAVE(ENOBUFS);
1132 			}
1133 			wh = mtod(m, struct pppoe_full_hdr *);
1134 			bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1135 			wh->ph.length = htons((short)(m->m_pkthdr.len));
1136 			NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1137 			privp->packets_out++;
1138 			break;
1139 			}
1140 		case	PPPOE_PRIMED:
1141 			/*
1142 			 * A PADI packet is being returned by the application
1143 			 * that has set up this hook. This indicates that it
1144 			 * wants us to offer service.
1145 			 */
1146 			neg = sp->neg;
1147 			if (m->m_len < sizeof(*wh))
1148 				m_pullup(m, sizeof(*wh));
1149 			if (m == NULL) {
1150 				LEAVE(ENOBUFS);
1151 			}
1152 			wh = mtod(m, struct pppoe_full_hdr *);
1153 			ph = &wh->ph;
1154 			session = ntohs(wh->ph.sid);
1155 			length = ntohs(wh->ph.length);
1156 			code = wh->ph.code;
1157 			if ( code != PADI_CODE) {
1158 				LEAVE(EINVAL);
1159 			};
1160 			untimeout(pppoe_ticker, hook,
1161 				    neg->timeout_handle);
1162 
1163 			/*
1164 			 * This is the first time we hear
1165 			 * from the client, so note it's
1166 			 * unicast address, replacing the
1167 			 * broadcast address .
1168 			 */
1169 			bcopy(wh->eh.ether_shost,
1170 				neg->pkt->pkt_header.eh.ether_dhost,
1171 				ETHER_ADDR_LEN);
1172 			sp->state = PPPOE_SOFFER;
1173 			neg->timeout = 0;
1174 			neg->pkt->pkt_header.ph.code = PADO_CODE;
1175 
1176 			/*
1177 			 * start working out the tags to respond with.
1178 			 */
1179 			uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1180 			uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1181 			uniqtag.data.pointer = sp;
1182 			init_tags(sp);
1183 			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1184 			insert_tag(sp, tag);	      /* returned hostunique */
1185 			insert_tag(sp, &uniqtag.hdr);      /* AC cookie */
1186 			tag = get_tag(ph, PTT_SRV_NAME);
1187 			insert_tag(sp, tag);	      /* returned service */
1188 			/* XXX maybe put the tag in the session store */
1189 			scan_tags(sp, ph);
1190 			make_packet(sp);
1191 			sendpacket(sp);
1192 			break;
1193 
1194 		/*
1195 		 * Packets coming from the hook make no sense
1196 		 * to sessions in these states. Throw them away.
1197 		 */
1198 		case	PPPOE_SINIT:
1199 		case	PPPOE_SREQ:
1200 		case	PPPOE_SOFFER:
1201 		case	PPPOE_SNONE:
1202 		case	PPPOE_LISTENING:
1203 		case	PPPOE_DEAD:
1204 		default:
1205 			LEAVE(ENETUNREACH);
1206 		}
1207 	}
1208 quit:
1209 	NG_FREE_DATA(m, meta);
1210 	return error;
1211 }
1212 
1213 /*
1214  * Do local shutdown processing..
1215  * If we are a persistant device, we might refuse to go away, and
1216  * we'd only remove our links and reset ourself.
1217  */
1218 static int
1219 ng_PPPoE_rmnode(node_p node)
1220 {
1221 	const priv_p privdata = node->private;
1222 
1223 AAA
1224 	node->flags |= NG_INVALID;
1225 	ng_cutlinks(node);
1226 	ng_unname(node);
1227 	node->private = NULL;
1228 	ng_unref(privdata->node);
1229 	FREE(privdata, M_NETGRAPH);
1230 	return (0);
1231 }
1232 
1233 /*
1234  * This is called once we've already connected a new hook to the other node.
1235  * It gives us a chance to balk at the last minute.
1236  */
1237 static int
1238 ng_PPPoE_connect(hook_p hook)
1239 {
1240 	/* be really amiable and just say "YUP that's OK by me! " */
1241 	return (0);
1242 }
1243 
1244 /*
1245  * Hook disconnection
1246  *
1247  * Clean up all dangling links and infirmation about the session/hook.
1248  * For this type, removal of the last link destroys the node
1249  */
1250 static int
1251 ng_PPPoE_disconnect(hook_p hook)
1252 {
1253 	node_p node = hook->node;
1254 	priv_p privp = node->private;
1255 	sessp	sp;
1256 
1257 AAA
1258 	if (hook->private == &privp->debug_hook) {
1259 		privp->debug_hook = NULL;
1260 	} else if (hook->private == &privp->ethernet_hook) {
1261 		privp->ethernet_hook = NULL;
1262 	} else {
1263 		sp = hook->private;
1264 		if (sp->state != PPPOE_SNONE ) {
1265 			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1266 		}
1267 		if ((privp->ethernet_hook)
1268 		&& ((sp->state == PPPOE_CONNECTED)
1269 		 || (sp->state == PPPOE_NEWCONNECTED))) {
1270 			struct mbuf *m;
1271 			struct pppoe_full_hdr *wh;
1272 			struct pppoe_tag *tag;
1273 			int	msglen = strlen(SIGNOFF);
1274 			void *dummy = NULL;
1275 			int error = 0;
1276 
1277 			/* revert the stored header to DISC/PADT mode */
1278 		 	wh = &sp->pkt_hdr;
1279 			wh->ph.code = PADT_CODE;
1280 			wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1281 
1282 			/* generate a packet of that type */
1283 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1284 			m->m_pkthdr.rcvif = NULL;
1285 			m->m_pkthdr.len = m->m_len = sizeof(*wh);
1286 			bcopy((caddr_t)wh, mtod(m, caddr_t), sizeof(*wh));
1287 			/* Add a General error message and adjust sizes */
1288 			wh = mtod(m, struct pppoe_full_hdr *);
1289 			tag = wh->ph.tag;
1290 			tag->tag_type = PTT_GEN_ERR;
1291 			tag->tag_len = htons((u_int16_t)msglen);
1292 			strncpy(tag->tag_data, SIGNOFF, msglen);
1293 			m->m_pkthdr.len = (m->m_len += sizeof(*tag) + msglen);
1294 			wh->ph.length = htons(sizeof(*tag) + msglen);
1295 			NG_SEND_DATA(error, privp->ethernet_hook, m, dummy);
1296 		}
1297 		if (sp->neg) {
1298 			untimeout(pppoe_ticker, hook, sp->neg->timeout_handle);
1299 			if (sp->neg->m)
1300 				m_freem(sp->neg->m);
1301 			FREE(sp->neg, M_NETGRAPH);
1302 		}
1303 		FREE(sp, M_NETGRAPH);
1304 		hook->private = NULL;
1305 	}
1306 	if (node->numhooks == 0)
1307 		ng_rmnode(node);
1308 	return (0);
1309 }
1310 
1311 /*
1312  * timeouts come here.
1313  */
1314 static void
1315 pppoe_ticker(void *arg)
1316 {
1317 	int s = splnet();
1318 	hook_p hook = arg;
1319 	sessp	sp = hook->private;
1320 	negp	neg = sp->neg;
1321 	int	error = 0;
1322 	struct mbuf *m0 = NULL;
1323 	priv_p privp = hook->node->private;
1324 	meta_p dummy = NULL;
1325 
1326 AAA
1327 	switch(sp->state) {
1328 		/*
1329 		 * resend the last packet, using an exponential backoff.
1330 		 * After a period of time, stop growing the backoff,
1331 		 * and either leave it, or reverst to the start.
1332 		 */
1333 	case	PPPOE_SINIT:
1334 	case	PPPOE_SREQ:
1335 		/* timeouts on these produce resends */
1336 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1337 		NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1338 		neg->timeout_handle = timeout(pppoe_ticker,
1339 					hook, neg->timeout * hz);
1340 		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1341 			if (sp->state == PPPOE_SREQ) {
1342 				/* revert to SINIT mode */
1343 				pppoe_start(sp);
1344 			} else {
1345 				neg->timeout = PPPOE_TIMEOUT_LIMIT;
1346 			}
1347 		}
1348 		break;
1349 	case	PPPOE_PRIMED:
1350 	case	PPPOE_SOFFER:
1351 		/* a timeout on these says "give up" */
1352 		ng_destroy_hook(hook);
1353 		break;
1354 	default:
1355 		/* timeouts have no meaning in other states */
1356 		printf("pppoe: unexpected timeout\n");
1357 	}
1358 	splx(s);
1359 }
1360 
1361 
1362 static void
1363 sendpacket(sessp sp)
1364 {
1365 	int	error = 0;
1366 	struct mbuf *m0 = NULL;
1367 	hook_p hook = sp->hook;
1368 	negp	neg = sp->neg;
1369 	priv_p	privp = hook->node->private;
1370 	meta_p dummy = NULL;
1371 
1372 AAA
1373 	switch(sp->state) {
1374 	case	PPPOE_LISTENING:
1375 	case	PPPOE_DEAD:
1376 	case	PPPOE_SNONE:
1377 	case	PPPOE_NEWCONNECTED:
1378 	case	PPPOE_CONNECTED:
1379 		printf("pppoe: sendpacket: unexpected state\n");
1380 		break;
1381 
1382 	case	PPPOE_PRIMED:
1383 		/* No packet to send, but set up the timeout */
1384 		neg->timeout_handle = timeout(pppoe_ticker,
1385 					hook, PPPOE_OFFER_TIMEOUT * hz);
1386 		break;
1387 
1388 	case	PPPOE_SOFFER:
1389 		/*
1390 		 * send the offer but if they don't respond
1391 		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1392 		 */
1393 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1394 		NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1395 		neg->timeout_handle = timeout(pppoe_ticker,
1396 					hook, PPPOE_OFFER_TIMEOUT * hz);
1397 		break;
1398 
1399 	case	PPPOE_SINIT:
1400 	case	PPPOE_SREQ:
1401 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1402 		NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1403 		neg->timeout_handle = timeout(pppoe_ticker, hook, hz);
1404 		neg->timeout = 2;
1405 		break;
1406 
1407 	default:
1408 		error = EINVAL;
1409 		printf("pppoe: timeout: bad state\n");
1410 	}
1411 	/* return (error); */
1412 }
1413 
1414 /*
1415  * Parse an incoming packet to see if any tags should be copied to the
1416  * output packet. DOon't do any tags that are likely to have been
1417  * handles a the main state machine.
1418  */
1419 static struct pppoe_tag*
1420 scan_tags(sessp	sp, struct pppoe_hdr* ph)
1421 {
1422 	char *end = (char *)next_tag(ph);
1423 	char *ptn;
1424 	struct pppoe_tag *pt = &ph->tag[0];
1425 	/*
1426 	 * Keep processing tags while a tag header will still fit.
1427 	 */
1428 AAA
1429 	while((char*)(pt + 1) <= end) {
1430 		/*
1431 		 * If the tag data would go past the end of the packet, abort.
1432 		 */
1433 		ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
1434 		if(ptn > end)
1435 			return NULL;
1436 
1437 		switch (pt->tag_type) {
1438 		case	PTT_RELAY_SID:
1439 			insert_tag(sp, pt);
1440 			break;
1441 		case	PTT_EOL:
1442 			return NULL;
1443 		case	PTT_SRV_NAME:
1444 		case	PTT_AC_NAME:
1445 		case	PTT_HOST_UNIQ:
1446 		case	PTT_AC_COOKIE:
1447 		case	PTT_VENDOR:
1448 		case	PTT_SRV_ERR:
1449 		case	PTT_SYS_ERR:
1450 		case	PTT_GEN_ERR:
1451 			break;
1452 		}
1453 		pt = (struct pppoe_tag*)ptn;
1454 	}
1455 	return NULL;
1456 }
1457 
1458 static	int
1459 pppoe_send_event(sessp sp, enum cmd cmdid)
1460 {
1461 	int error;
1462 	struct ng_mesg *msg;
1463 	struct ngPPPoE_sts *sts;
1464 
1465 AAA
1466 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1467 			sizeof(struct ngPPPoE_sts), M_NOWAIT);
1468 	sts = (struct ngPPPoE_sts *)msg->data;
1469 	strncpy(sts->hook, sp->hook->name, NG_HOOKLEN + 1);
1470 	error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
1471 	return (error);
1472 }
1473