ng_pppoe.c revision 70784
11556Srgrimes
21556Srgrimes/*
31556Srgrimes * ng_pppoe.c
41556Srgrimes *
51556Srgrimes * Copyright (c) 1996-1999 Whistle Communications, Inc.
61556Srgrimes * All rights reserved.
71556Srgrimes *
81556Srgrimes * Subject to the following obligations and disclaimer of warranty, use and
91556Srgrimes * redistribution of this software, in source or object code forms, with or
101556Srgrimes * without modifications are expressly permitted by Whistle Communications;
111556Srgrimes * provided, however, that:
121556Srgrimes * 1. Any and all reproductions of the source or object code must include the
131556Srgrimes *    copyright notice above and the following disclaimer of warranties; and
141556Srgrimes * 2. No rights are granted, in any manner or form, to use Whistle
151556Srgrimes *    Communications, Inc. trademarks, including the mark "WHISTLE
161556Srgrimes *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
171556Srgrimes *    such appears in the above copyright notice or in the software.
181556Srgrimes *
191556Srgrimes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
201556Srgrimes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
211556Srgrimes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
221556Srgrimes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
231556Srgrimes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
241556Srgrimes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
251556Srgrimes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
261556Srgrimes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
271556Srgrimes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
281556Srgrimes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
291556Srgrimes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
301556Srgrimes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
311556Srgrimes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
321556Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
331556Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
341556Srgrimes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
351556Srgrimes * OF SUCH DAMAGE.
361556Srgrimes *
371556Srgrimes * Author: Julian Elischer <julian@freebsd.org>
3836150Scharnier *
3936150Scharnier * $FreeBSD: head/sys/netgraph/ng_pppoe.c 70784 2001-01-08 05:34:06Z julian $
4036150Scharnier * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
4136150Scharnier */
4236150Scharnier#if 0
431556Srgrimes#define AAA printf("pppoe: %s\n", __FUNCTION__ );
441556Srgrimes#define BBB printf("-%d-", __LINE__ );
451556Srgrimes#else
461556Srgrimes#define AAA
471556Srgrimes#define BBB
481556Srgrimes#endif
491556Srgrimes
501556Srgrimes#include <sys/param.h>
511556Srgrimes#include <sys/systm.h>
521556Srgrimes#include <sys/kernel.h>
531556Srgrimes#include <sys/mbuf.h>
5417987Speter#include <sys/malloc.h>
551556Srgrimes#include <sys/errno.h>
5617987Speter#include <net/ethernet.h>
571556Srgrimes
581556Srgrimes#include <netgraph/ng_message.h>
591556Srgrimes#include <netgraph/netgraph.h>
601556Srgrimes#include <netgraph/ng_parse.h>
611556Srgrimes#include <netgraph/ng_pppoe.h>
621556Srgrimes
631556Srgrimes#define SIGNOFF "session closed"
641556Srgrimes#define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
651556Srgrimes
661556Srgrimes/*
671556Srgrimes * This section contains the netgraph method declarations for the
681556Srgrimes * sample node. These methods define the netgraph 'type'.
691556Srgrimes */
701556Srgrimes
7120425Sstevestatic ng_constructor_t	ng_pppoe_constructor;
7220425Sstevestatic ng_rcvmsg_t	ng_pppoe_rcvmsg;
731556Srgrimesstatic ng_shutdown_t	ng_pppoe_shutdown;
741556Srgrimesstatic ng_newhook_t	ng_pppoe_newhook;
751556Srgrimesstatic ng_connect_t	ng_pppoe_connect;
761556Srgrimesstatic ng_rcvdata_t	ng_pppoe_rcvdata;
771556Srgrimesstatic ng_disconnect_t	ng_pppoe_disconnect;
781556Srgrimes
791556Srgrimes/* Parse type for struct ngpppoe_init_data */
8020425Sstevestatic const struct ng_parse_struct_info ngpppoe_init_data_type_info
8117987Speter	= NG_PPPOE_INIT_DATA_TYPE_INFO;
8217987Speterstatic const struct ng_parse_type ngpppoe_init_data_state_type = {
831556Srgrimes	&ng_parse_struct_type,
841556Srgrimes	&ngpppoe_init_data_type_info
851556Srgrimes};
861556Srgrimes
871556Srgrimes/* Parse type for struct ngpppoe_sts */
881556Srgrimesstatic const struct ng_parse_struct_info ng_pppoe_sts_type_info
891556Srgrimes	= NG_PPPOE_STS_TYPE_INFO;
901556Srgrimesstatic const struct ng_parse_type ng_pppoe_sts_state_type = {
911556Srgrimes	&ng_parse_struct_type,
921556Srgrimes	&ng_pppoe_sts_type_info
931556Srgrimes};
941556Srgrimes
951556Srgrimes/* List of commands and how to convert arguments to/from ASCII */
961556Srgrimesstatic const struct ng_cmdlist ng_pppoe_cmds[] = {
971556Srgrimes	{
981556Srgrimes	  NGM_PPPOE_COOKIE,
991556Srgrimes	  NGM_PPPOE_CONNECT,
1001556Srgrimes	  "pppoe_connect",
1011556Srgrimes	  &ngpppoe_init_data_state_type,
10217987Speter	  NULL
10317987Speter	},
1041556Srgrimes	{
1051556Srgrimes	  NGM_PPPOE_COOKIE,
1061556Srgrimes	  NGM_PPPOE_LISTEN,
1071556Srgrimes	  "pppoe_listen",
1081556Srgrimes	  &ngpppoe_init_data_state_type,
10917987Speter	  NULL
11017987Speter	},
11120425Ssteve	{
1121556Srgrimes	  NGM_PPPOE_COOKIE,
1131556Srgrimes	  NGM_PPPOE_OFFER,
1141556Srgrimes	  "pppoe_offer",
1151556Srgrimes	  &ngpppoe_init_data_state_type,
1161556Srgrimes	  NULL
1171556Srgrimes	},
1181556Srgrimes	{
1191556Srgrimes	  NGM_PPPOE_COOKIE,
12020425Ssteve	  NGM_PPPOE_SERVICE,
1211556Srgrimes	  "pppoe_service",
1221556Srgrimes	  &ngpppoe_init_data_state_type,
1231556Srgrimes	  NULL
12420425Ssteve	},
12520425Ssteve	{
12620425Ssteve	  NGM_PPPOE_COOKIE,
12720425Ssteve	  NGM_PPPOE_SUCCESS,
12820425Ssteve	  "pppoe_success",
12920425Ssteve	  &ng_pppoe_sts_state_type,
13020425Ssteve	  NULL
13120425Ssteve	},
1321556Srgrimes	{
13320425Ssteve	  NGM_PPPOE_COOKIE,
13420425Ssteve	  NGM_PPPOE_FAIL,
13520425Ssteve	  "pppoe_fail",
13620425Ssteve	  &ng_pppoe_sts_state_type,
13720425Ssteve	  NULL
13820425Ssteve	},
13920425Ssteve	{
14020425Ssteve	  NGM_PPPOE_COOKIE,
14120425Ssteve	  NGM_PPPOE_CLOSE,
14220425Ssteve	  "pppoe_close",
14320425Ssteve	  &ng_pppoe_sts_state_type,
14420425Ssteve	  NULL
14520425Ssteve	},
14620425Ssteve	{ 0 }
14720425Ssteve};
14820425Ssteve
14920425Ssteve/* Netgraph node type descriptor */
15025222Sstevestatic struct ng_type typestruct = {
1511556Srgrimes	NG_ABI_VERSION,
15217987Speter	NG_PPPOE_NODE_TYPE,
1531556Srgrimes	NULL,
1541556Srgrimes	ng_pppoe_constructor,
1551556Srgrimes	ng_pppoe_rcvmsg,
1561556Srgrimes	ng_pppoe_shutdown,
15717987Speter	ng_pppoe_newhook,
15817987Speter	NULL,
15925222Ssteve	ng_pppoe_connect,
1601556Srgrimes	ng_pppoe_rcvdata,
1611556Srgrimes	ng_pppoe_disconnect,
1621556Srgrimes	ng_pppoe_cmds
16325222Ssteve};
16420425SsteveNETGRAPH_INIT(pppoe, &typestruct);
16520425Ssteve
16620425Ssteve/*
16720425Ssteve * States for the session state machine.
16820425Ssteve * These have no meaning if there is no hook attached yet.
16920425Ssteve */
17020425Ssteveenum state {
17120425Ssteve    PPPOE_SNONE=0,	/* [both] Initial state */
17217987Speter    PPPOE_LISTENING,	/* [Daemon] Listening for discover initiation pkt */
17320425Ssteve    PPPOE_SINIT,	/* [Client] Sent discovery initiation */
17425222Ssteve    PPPOE_PRIMED,	/* [Server] Awaiting PADI from daemon */
17520425Ssteve    PPPOE_SOFFER,	/* [Server] Sent offer message  (got PADI)*/
17620425Ssteve    PPPOE_SREQ,		/* [Client] Sent a Request */
17720425Ssteve    PPPOE_NEWCONNECTED,	/* [Server] Connection established, No data received */
17820425Ssteve    PPPOE_CONNECTED,	/* [Both] Connection established, Data received */
17920425Ssteve    PPPOE_DEAD		/* [Both] */
18020425Ssteve};
18120425Ssteve
18220425Ssteve#define NUMTAGS 20 /* number of tags we are set up to work with */
18325222Ssteve
18420425Ssteve/*
18520425Ssteve * Information we store for each hook on each node for negotiating the
18620425Ssteve * session. The mbuf and cluster are freed once negotiation has completed.
18720425Ssteve * The whole negotiation block is then discarded.
18825222Ssteve */
1891556Srgrimes
1901556Srgrimesstruct sess_neg {
1911556Srgrimes	struct mbuf 		*m; /* holds cluster with last sent packet */
19220425Ssteve	union	packet		*pkt; /* points within the above cluster */
1931556Srgrimes	struct callout_handle	timeout_handle;   /* see timeout(9) */
1941556Srgrimes	u_int			timeout; /* 0,1,2,4,8,16 etc. seconds */
19520425Ssteve	u_int			numtags;
1961556Srgrimes	struct pppoe_tag	*tags[NUMTAGS];
1971556Srgrimes	u_int			service_len;
1981556Srgrimes	u_int			ac_name_len;
1991556Srgrimes
2001556Srgrimes	struct datatag		service;
2011556Srgrimes	struct datatag		ac_name;
2021556Srgrimes};
2031556Srgrimestypedef struct sess_neg *negp;
2041556Srgrimes
2051556Srgrimes/*
2061556Srgrimes * Session information that is needed after connection.
2071556Srgrimes */
2081556Srgrimesstruct sess_con {
2091556Srgrimes	hook_p  		hook;
2101556Srgrimes	u_int16_t		Session_ID;
2111556Srgrimes	enum state		state;
2121556Srgrimes	ng_ID_t			creator;		/* who to notify */
2131556Srgrimes	struct pppoe_full_hdr	pkt_hdr;	/* used when connected */
2141556Srgrimes	negp			neg;		/* used when negotiating */
21517987Speter	/*struct sess_con	*hash_next;*/	/* not yet used */
21617987Speter};
21717987Spetertypedef struct sess_con *sessp;
21817987Speter
21917987Speter/*
22017987Speter * Information we store for each node
22117987Speter */
22217987Speterstruct PPPOE {
22317987Speter	node_p		node;		/* back pointer to node */
22417987Speter	hook_p  	ethernet_hook;
22517987Speter	hook_p  	debug_hook;
22617987Speter	u_int   	packets_in;	/* packets in from ethernet */
22717987Speter	u_int   	packets_out;	/* packets out towards ethernet */
22817987Speter	u_int32_t	flags;
22917987Speter	/*struct sess_con *buckets[HASH_SIZE];*/	/* not yet used */
2301556Srgrimes};
23117987Spetertypedef struct PPPOE *priv_p;
2321556Srgrimes
2331556Srgrimesconst struct ether_header eh_prototype =
23417987Speter	{{0xff,0xff,0xff,0xff,0xff,0xff},
2351556Srgrimes	 {0x00,0x00,0x00,0x00,0x00,0x00},
23617987Speter	 ETHERTYPE_PPPOE_DISC};
23717987Speter
23817987Speterunion uniq {
2391556Srgrimes	char bytes[sizeof(void *)];
24017987Speter	void * pointer;
2411556Srgrimes	};
24217987Speter
2431556Srgrimes#define	LEAVE(x) do { error = x; goto quit; } while(0)
24417987Speterstatic void	pppoe_start(sessp sp);
2451556Srgrimesstatic void	sendpacket(sessp sp);
2461556Srgrimesstatic void	pppoe_ticker(void *arg);
24717987Speterstatic struct pppoe_tag* scan_tags(sessp	sp, struct pppoe_hdr* ph);
2481556Srgrimesstatic	int	pppoe_send_event(sessp sp, enum cmd cmdid);
2491556Srgrimes
25017987Speter/*************************************************************************
2511556Srgrimes * Some basic utilities  from the Linux version with author's permission.*
2521556Srgrimes * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
25317987Speter ************************************************************************/
2541556Srgrimes
2551556Srgrimes/*
25617987Speter * Generate a new session id
2571556Srgrimes * XXX find out the FreeBSD locking scheme.
2581556Srgrimes */
25917987Speterstatic u_int16_t
2601556Srgrimesget_new_sid(node_p node)
26117987Speter{
2621556Srgrimes	static int pppoe_sid = 10;
26317987Speter	sessp sp;
2641556Srgrimes	hook_p	hook;
26517987Speter	u_int16_t val;
2661556Srgrimes	priv_p privp = NG_NODE_PRIVATE(node);
2671556Srgrimes
2681556SrgrimesAAA
2691556Srgrimesrestart:
2701556Srgrimes	val = pppoe_sid++;
2711556Srgrimes	/*
2721556Srgrimes	 * Spec says 0xFFFF is reserved.
2731556Srgrimes	 * Also don't use 0x0000
2741556Srgrimes	 */
2751556Srgrimes	if (val == 0xffff) {
27620425Ssteve		pppoe_sid = 20;
27717987Speter		goto restart;
27817987Speter	}
27917987Speter
2801556Srgrimes	/* Check it isn't already in use */
2811556Srgrimes	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
2821556Srgrimes		/* don't check special hooks */
2831556Srgrimes		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
2841556Srgrimes		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
2851556Srgrimes			continue;
2861556Srgrimes		sp = NG_HOOK_PRIVATE(hook);
2871556Srgrimes		if (sp->Session_ID == val)
2881556Srgrimes			goto restart;
2891556Srgrimes	}
290
291	return val;
292}
293
294
295/*
296 * Return the location where the next tag can be put
297 */
298static __inline struct pppoe_tag*
299next_tag(struct pppoe_hdr* ph)
300{
301	return (struct pppoe_tag*)(((char*)&ph->tag[0]) + ntohs(ph->length));
302}
303
304/*
305 * Look for a tag of a specific type
306 * Don't trust any length the other end says.
307 * but assume we already sanity checked ph->length.
308 */
309static struct pppoe_tag*
310get_tag(struct pppoe_hdr* ph, u_int16_t idx)
311{
312	char *end = (char *)next_tag(ph);
313	char *ptn;
314	struct pppoe_tag *pt = &ph->tag[0];
315	/*
316	 * Keep processing tags while a tag header will still fit.
317	 */
318AAA
319	while((char*)(pt + 1) <= end) {
320	    /*
321	     * If the tag data would go past the end of the packet, abort.
322	     */
323	    ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
324	    if(ptn > end)
325		return NULL;
326
327	    if(pt->tag_type == idx)
328		return pt;
329
330	    pt = (struct pppoe_tag*)ptn;
331	}
332	return NULL;
333}
334
335/**************************************************************************
336 * inlines to initialise or add tags to a session's tag list,
337 **************************************************************************/
338/*
339 * Initialise the session's tag list
340 */
341static void
342init_tags(sessp sp)
343{
344AAA
345	if(sp->neg == NULL) {
346		printf("pppoe: asked to init NULL neg pointer\n");
347		return;
348	}
349	sp->neg->numtags = 0;
350}
351
352static void
353insert_tag(sessp sp, struct pppoe_tag *tp)
354{
355	int	i;
356	negp neg;
357
358AAA
359	if((neg = sp->neg) == NULL) {
360		printf("pppoe: asked to use NULL neg pointer\n");
361		return;
362	}
363	if ((i = neg->numtags++) < NUMTAGS) {
364		neg->tags[i] = tp;
365	} else {
366		printf("pppoe: asked to add too many tags to packet\n");
367		neg->numtags--;
368	}
369}
370
371/*
372 * Make up a packet, using the tags filled out for the session.
373 *
374 * Assume that the actual pppoe header and ethernet header
375 * are filled out externally to this routine.
376 * Also assume that neg->wh points to the correct
377 * location at the front of the buffer space.
378 */
379static void
380make_packet(sessp sp) {
381	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
382	struct pppoe_tag **tag;
383	char *dp;
384	int count;
385	int tlen;
386	u_int16_t length = 0;
387
388AAA
389	if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
390		printf("pppoe: make_packet called from wrong state\n");
391	}
392	dp = (char *)wh->ph.tag;
393	for (count = 0, tag = sp->neg->tags;
394	    ((count < sp->neg->numtags) && (count < NUMTAGS));
395	    tag++, count++) {
396		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
397		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
398			printf("pppoe: tags too long\n");
399			sp->neg->numtags = count;
400			break;	/* XXX chop off what's too long */
401		}
402		bcopy((char *)*tag, (char *)dp, tlen);
403		length += tlen;
404		dp += tlen;
405	}
406 	wh->ph.length = htons(length);
407	sp->neg->m->m_len = length + sizeof(*wh);
408	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
409}
410
411/**************************************************************************
412 * Routine to match a service offered					  *
413 **************************************************************************/
414/*
415 * Find a hook that has a service string that matches that
416 * we are seeking. for now use a simple string.
417 * In the future we may need something like regexp().
418 * for testing allow a null string to match 1st found and a null service
419 * to match all requests. Also make '*' do the same.
420 */
421static hook_p
422pppoe_match_svc(node_p node, char *svc_name, int svc_len)
423{
424	sessp	sp	= NULL;
425	negp	neg	= NULL;
426	priv_p	privp	= NG_NODE_PRIVATE(node);
427	hook_p hook;
428
429AAA
430	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
431
432		/* skip any hook that is debug or ethernet */
433		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
434		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
435			continue;
436		sp = NG_HOOK_PRIVATE(hook);
437
438		/* Skip any sessions which are not in LISTEN mode. */
439		if ( sp->state != PPPOE_LISTENING)
440			continue;
441
442		neg = sp->neg;
443		/* XXX check validity of this */
444		/* special case, NULL request. match 1st found. */
445		if (svc_len == 0)
446			break;
447
448		/* XXX check validity of this */
449		/* Special case for a blank or "*" service name (wildcard) */
450		if ((neg->service_len == 0)
451		||  ((neg->service_len == 1)
452		  && (neg->service.data[0] == '*'))) {
453			break;
454		}
455
456		/* If the lengths don't match, that aint it. */
457		if (neg->service_len != svc_len)
458			continue;
459
460		/* An exact match? */
461		if (strncmp(svc_name, neg->service.data, svc_len) == 0)
462			break;
463	}
464	return (hook);
465}
466/**************************************************************************
467 * Routine to find a particular session that matches an incoming packet	  *
468 **************************************************************************/
469static hook_p
470pppoe_findsession(node_p node, struct pppoe_full_hdr *wh)
471{
472	sessp	sp = NULL;
473	hook_p hook = NULL;
474	priv_p	privp = NG_NODE_PRIVATE(node);
475	u_int16_t	session = ntohs(wh->ph.sid);
476
477	/*
478	 * find matching peer/session combination.
479	 */
480AAA
481	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
482		/* don't check special hooks */
483		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
484		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
485			continue;
486		}
487		sp = NG_HOOK_PRIVATE(hook);
488		if ( ( (sp->state == PPPOE_CONNECTED)
489		    || (sp->state == PPPOE_NEWCONNECTED) )
490		&& (sp->Session_ID == session)
491		&& (bcmp(sp->pkt_hdr.eh.ether_dhost,
492		    wh->eh.ether_shost,
493		    ETHER_ADDR_LEN)) == 0) {
494			break;
495		}
496	}
497	return (hook);
498}
499
500static hook_p
501pppoe_finduniq(node_p node, struct pppoe_tag *tag)
502{
503	hook_p hook = NULL;
504	priv_p	privp = NG_NODE_PRIVATE(node);
505	union uniq		uniq;
506
507AAA
508	bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
509	/* cycle through all known hooks */
510	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
511		/* don't check special hooks */
512		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
513		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
514			continue;
515		if (uniq.pointer == NG_HOOK_PRIVATE(hook))
516			break;
517	}
518	return (hook);
519}
520
521/**************************************************************************
522 * start of Netgraph entrypoints					  *
523 **************************************************************************/
524
525/*
526 * Allocate the private data structure and the generic node
527 * and link them together.
528 *
529 * ng_make_node_common() returns with a generic node struct
530 * with a single reference for us.. we transfer it to the
531 * private structure.. when we free the private struct we must
532 * unref the node so it gets freed too.
533 */
534static int
535ng_pppoe_constructor(node_p node)
536{
537	priv_p privdata;
538
539AAA
540	/* Initialize private descriptor */
541	MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH,
542	    M_NOWAIT | M_ZERO);
543	if (privdata == NULL)
544		return (ENOMEM);
545
546	/* Link structs together; this counts as our one reference to *nodep */
547	NG_NODE_SET_PRIVATE(node, privdata);
548	privdata->node = node;
549	return (0);
550}
551
552/*
553 * Give our ok for a hook to be added...
554 * point the hook's private info to the hook structure.
555 *
556 * The following hook names are special:
557 *  Ethernet:  the hook that should be connected to a NIC.
558 *  debug:	copies of data sent out here  (when I write the code).
559 * All other hook names need only be unique. (the framework checks this).
560 */
561static int
562ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
563{
564	const priv_p privp = NG_NODE_PRIVATE(node);
565	sessp sp;
566
567AAA
568	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
569		privp->ethernet_hook = hook;
570		NG_HOOK_SET_PRIVATE(hook, &privp->ethernet_hook);
571	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
572		privp->debug_hook = hook;
573		NG_HOOK_SET_PRIVATE(hook, &privp->debug_hook);
574	} else {
575		/*
576		 * Any other unique name is OK.
577		 * The infrastructure has already checked that it's unique,
578		 * so just allocate it and hook it in.
579		 */
580		MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_NOWAIT | M_ZERO);
581		if (sp == NULL) {
582				return (ENOMEM);
583		}
584
585		NG_HOOK_SET_PRIVATE(hook, sp);
586		sp->hook = hook;
587	}
588	return(0);
589}
590
591/*
592 * Get a netgraph control message.
593 * Check it is one we understand. If needed, send a response.
594 * We sometimes save the address for an async action later.
595 * Always free the message.
596 */
597static int
598ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
599{
600	priv_p privp = NG_NODE_PRIVATE(node);
601	struct ngpppoe_init_data *ourmsg = NULL;
602	struct ng_mesg *resp = NULL;
603	int error = 0;
604	hook_p hook = NULL;
605	sessp sp = NULL;
606	negp neg = NULL;
607	struct ng_mesg *msg;
608
609AAA
610	NGI_GET_MSG(item, msg);
611	/* Deal with message according to cookie and command */
612	switch (msg->header.typecookie) {
613	case NGM_PPPOE_COOKIE:
614		switch (msg->header.cmd) {
615		case NGM_PPPOE_CONNECT:
616		case NGM_PPPOE_LISTEN:
617		case NGM_PPPOE_OFFER:
618		case NGM_PPPOE_SERVICE:
619			ourmsg = (struct ngpppoe_init_data *)msg->data;
620			if (msg->header.arglen < sizeof(*ourmsg)) {
621				printf("pppoe: init data too small\n");
622				LEAVE(EMSGSIZE);
623			}
624			if (msg->header.arglen - sizeof(*ourmsg) >
625			    PPPOE_SERVICE_NAME_SIZE) {
626				printf("pppoe_rcvmsg: service name too big");
627				LEAVE(EMSGSIZE);
628			}
629			if (msg->header.arglen - sizeof(*ourmsg) <
630			    ourmsg->data_len) {
631				printf("pppoe: init data has bad length,"
632				    " %d should be %d\n", ourmsg->data_len,
633				    msg->header.arglen - sizeof (*ourmsg));
634				LEAVE(EMSGSIZE);
635			}
636
637			/* make sure strcmp will terminate safely */
638			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
639
640			/* cycle through all known hooks */
641			LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
642				if (NG_HOOK_NAME(hook)
643				&& strcmp(NG_HOOK_NAME(hook), ourmsg->hook) == 0)
644					break;
645			}
646			if (hook == NULL) {
647				LEAVE(ENOENT);
648			}
649			if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
650			||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
651				LEAVE(EINVAL);
652			}
653			sp = NG_HOOK_PRIVATE(hook);
654
655			/*
656			 * PPPOE_SERVICE advertisments are set up
657			 * on sessions that are in PRIMED state.
658			 */
659			if (msg->header.cmd == NGM_PPPOE_SERVICE) {
660				break;
661			}
662			if (sp->state |= PPPOE_SNONE) {
663				printf("pppoe: Session already active\n");
664				LEAVE(EISCONN);
665			}
666
667			/*
668			 * set up prototype header
669			 */
670			MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH,
671			    M_NOWAIT | M_ZERO);
672
673			if (neg == NULL) {
674				printf("pppoe: Session out of memory\n");
675				LEAVE(ENOMEM);
676			}
677			MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
678			if(neg->m == NULL) {
679				printf("pppoe: Session out of mbufs\n");
680				FREE(neg, M_NETGRAPH);
681				LEAVE(ENOBUFS);
682			}
683			neg->m->m_pkthdr.rcvif = NULL;
684			MCLGET(neg->m, M_DONTWAIT);
685			if ((neg->m->m_flags & M_EXT) == 0) {
686				printf("pppoe: Session out of mcls\n");
687				m_freem(neg->m);
688				FREE(neg, M_NETGRAPH);
689				LEAVE(ENOBUFS);
690			}
691			sp->neg = neg;
692			callout_handle_init( &neg->timeout_handle);
693			neg->m->m_len = sizeof(struct pppoe_full_hdr);
694			neg->pkt = mtod(neg->m, union packet*);
695			neg->pkt->pkt_header.eh = eh_prototype;
696			neg->pkt->pkt_header.ph.ver = 0x1;
697			neg->pkt->pkt_header.ph.type = 0x1;
698			neg->pkt->pkt_header.ph.sid = 0x0000;
699			neg->timeout = 0;
700
701			sp->creator = NGI_RETADDR(item);
702		}
703		switch (msg->header.cmd) {
704		case NGM_PPPOE_GET_STATUS:
705		    {
706			struct ngpppoestat *stats;
707
708			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
709			if (!resp) {
710				LEAVE(ENOMEM);
711			}
712			stats = (struct ngpppoestat *) resp->data;
713			stats->packets_in = privp->packets_in;
714			stats->packets_out = privp->packets_out;
715			break;
716		    }
717		case NGM_PPPOE_CONNECT:
718			/*
719			 * Check the hook exists and is Uninitialised.
720			 * Send a PADI request, and start the timeout logic.
721			 * Store the originator of this message so we can send
722			 * a success of fail message to them later.
723			 * Move the session to SINIT
724			 * Set up the session to the correct state and
725			 * start it.
726			 */
727			neg->service.hdr.tag_type = PTT_SRV_NAME;
728			neg->service.hdr.tag_len =
729			    htons((u_int16_t)ourmsg->data_len);
730			if (ourmsg->data_len)
731				bcopy(ourmsg->data, neg->service.data,
732				    ourmsg->data_len);
733			neg->service_len = ourmsg->data_len;
734			pppoe_start(sp);
735			break;
736		case NGM_PPPOE_LISTEN:
737			/*
738			 * Check the hook exists and is Uninitialised.
739			 * Install the service matching string.
740			 * Store the originator of this message so we can send
741			 * a success of fail message to them later.
742			 * Move the hook to 'LISTENING'
743			 */
744			neg->service.hdr.tag_type = PTT_SRV_NAME;
745			neg->service.hdr.tag_len =
746			    htons((u_int16_t)ourmsg->data_len);
747
748			if (ourmsg->data_len)
749				bcopy(ourmsg->data, neg->service.data,
750				    ourmsg->data_len);
751			neg->service_len = ourmsg->data_len;
752			neg->pkt->pkt_header.ph.code = PADT_CODE;
753			/*
754			 * wait for PADI packet coming from ethernet
755			 */
756			sp->state = PPPOE_LISTENING;
757			break;
758		case NGM_PPPOE_OFFER:
759			/*
760			 * Check the hook exists and is Uninitialised.
761			 * Store the originator of this message so we can send
762			 * a success of fail message to them later.
763			 * Store the AC-Name given and go to PRIMED.
764			 */
765			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
766			neg->ac_name.hdr.tag_len =
767			    htons((u_int16_t)ourmsg->data_len);
768			if (ourmsg->data_len)
769				bcopy(ourmsg->data, neg->ac_name.data,
770				    ourmsg->data_len);
771			neg->ac_name_len = ourmsg->data_len;
772			neg->pkt->pkt_header.ph.code = PADO_CODE;
773			/*
774			 * Wait for PADI packet coming from hook
775			 */
776			sp->state = PPPOE_PRIMED;
777			break;
778		case NGM_PPPOE_SERVICE:
779			/*
780			 * Check the session is primed.
781			 * for now just allow ONE service to be advertised.
782			 * If you do it twice you just overwrite.
783			 */
784			if (sp->state != PPPOE_PRIMED) {
785				printf("pppoe: Session not primed\n");
786				LEAVE(EISCONN);
787			}
788			neg->service.hdr.tag_type = PTT_SRV_NAME;
789			neg->service.hdr.tag_len =
790			    htons((u_int16_t)ourmsg->data_len);
791
792			if (ourmsg->data_len)
793				bcopy(ourmsg->data, neg->service.data,
794				    ourmsg->data_len);
795			neg->service_len = ourmsg->data_len;
796			break;
797		default:
798			LEAVE(EINVAL);
799		}
800		break;
801	default:
802		LEAVE(EINVAL);
803	}
804
805	/* Take care of synchronous response, if any */
806quit:
807	NG_RESPOND_MSG(error, node, item, resp);
808	/* Free the message and return */
809	NG_FREE_MSG(msg);
810	return(error);
811}
812
813/*
814 * Start a client into the first state. A separate function because
815 * it can be needed if the negotiation times out.
816 */
817static void
818pppoe_start(sessp sp)
819{
820	struct {
821		struct pppoe_tag hdr;
822		union	uniq	data;
823	} uniqtag;
824
825	/*
826	 * kick the state machine into starting up
827	 */
828AAA
829	sp->state = PPPOE_SINIT;
830	/* reset the packet header to broadcast */
831	sp->neg->pkt->pkt_header.eh = eh_prototype;
832	sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
833	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
834	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
835	uniqtag.data.pointer = sp;
836	init_tags(sp);
837	insert_tag(sp, &uniqtag.hdr);
838	insert_tag(sp, &sp->neg->service.hdr);
839	make_packet(sp);
840	sendpacket(sp);
841}
842
843/*
844 * Receive data, and do something with it.
845 * The caller will never free m or meta, so
846 * if we use up this data or abort we must free BOTH of these.
847 */
848static int
849ng_pppoe_rcvdata(hook_p hook, item_p item)
850{
851	node_p			node = NG_HOOK_NODE(hook);
852	const priv_p		privp = NG_NODE_PRIVATE(node);
853	sessp			sp = NG_HOOK_PRIVATE(hook);
854	struct pppoe_full_hdr	*wh;
855	struct pppoe_hdr	*ph;
856	int			error = 0;
857	u_int16_t		session;
858	u_int16_t		length;
859	u_int8_t		code;
860	struct pppoe_tag	*utag = NULL, *tag = NULL;
861	hook_p 			sendhook;
862	struct {
863		struct pppoe_tag hdr;
864		union	uniq	data;
865	} uniqtag;
866	negp			neg = NULL;
867	struct mbuf		*m;
868
869AAA
870	NGI_GET_M(item, m);
871	if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
872		/*
873		 * Data from the debug hook gets sent without modification
874		 * straight to the ethernet.
875		 */
876		NG_FWD_ITEM_HOOK( error, item, privp->ethernet_hook);
877	 	privp->packets_out++;
878	} else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
879		/*
880		 * Incoming data.
881		 * Dig out various fields from the packet.
882		 * use them to decide where to send it.
883		 */
884
885 		privp->packets_in++;
886		if( m->m_len < sizeof(*wh)) {
887			m = m_pullup(m, sizeof(*wh)); /* Checks length */
888			if (m == NULL) {
889				printf("couldn't m_pullup\n");
890				LEAVE(ENOBUFS);
891			}
892		}
893		wh = mtod(m, struct pppoe_full_hdr *);
894		length = ntohs(wh->ph.length);
895		switch(wh->eh.ether_type) {
896		case	ETHERTYPE_PPPOE_DISC:
897			/*
898			 * We need to try to make sure that the tag area
899			 * is contiguous, or we could wander off the end
900			 * of a buffer and make a mess.
901			 * (Linux wouldn't have this problem).
902			 */
903			if (m->m_pkthdr.len <= MHLEN) {
904				if( m->m_len < m->m_pkthdr.len) {
905					m = m_pullup(m, m->m_pkthdr.len);
906					if (m == NULL) {
907						printf("couldn't m_pullup\n");
908						LEAVE(ENOBUFS);
909					}
910				}
911			}
912			if (m->m_len != m->m_pkthdr.len) {
913				/*
914				 * It's not all in one piece.
915				 * We need to do extra work.
916				 * Put it into a cluster.
917				 */
918				struct mbuf *n;
919				n = m_dup(m, M_DONTWAIT);
920				m_freem(m);
921				m = n;
922				if (m) {
923					/* just check we got a cluster */
924					if (m->m_len != m->m_pkthdr.len) {
925						m_freem(m);
926						m = NULL;
927					}
928				}
929				if (m == NULL) {
930					printf("packet fragmented\n");
931					LEAVE(EMSGSIZE);
932				}
933			}
934			wh = mtod(m, struct pppoe_full_hdr *);
935			length = ntohs(wh->ph.length);
936			ph = &wh->ph;
937			session = ntohs(wh->ph.sid);
938			code = wh->ph.code;
939
940			switch(code) {
941			case	PADI_CODE:
942				/*
943				 * We are a server:
944				 * Look for a hook with the required service
945				 * and send the ENTIRE packet up there.
946				 * It should come back to a new hook in
947				 * PRIMED state. Look there for further
948				 * processing.
949				 */
950				tag = get_tag(ph, PTT_SRV_NAME);
951				if (tag == NULL) {
952					printf("no service tag\n");
953					LEAVE(ENETUNREACH);
954				}
955				sendhook = pppoe_match_svc(NG_HOOK_NODE(hook),
956			    		tag->tag_data, ntohs(tag->tag_len));
957				if (sendhook) {
958					NG_FWD_NEW_DATA(error, item,
959								sendhook, m);
960				} else {
961					printf("no such service\n");
962					LEAVE(ENETUNREACH);
963				}
964				break;
965			case	PADO_CODE:
966				/*
967				 * We are a client:
968				 * Use the host_uniq tag to find the
969				 * hook this is in response to.
970				 * Received #2, now send #3
971				 * For now simply accept the first we receive.
972				 */
973				utag = get_tag(ph, PTT_HOST_UNIQ);
974				if ((utag == NULL)
975				|| (ntohs(utag->tag_len) != sizeof(sp))) {
976					printf("no host unique field\n");
977					LEAVE(ENETUNREACH);
978				}
979
980				sendhook = pppoe_finduniq(node, utag);
981				if (sendhook == NULL) {
982					printf("no matching session\n");
983					LEAVE(ENETUNREACH);
984				}
985
986				/*
987				 * Check the session is in the right state.
988				 * It needs to be in PPPOE_SINIT.
989				 */
990				sp = NG_HOOK_PRIVATE(sendhook);
991				if (sp->state != PPPOE_SINIT) {
992					printf("session in wrong state\n");
993					LEAVE(ENETUNREACH);
994				}
995				neg = sp->neg;
996				untimeout(pppoe_ticker, sendhook,
997				    neg->timeout_handle);
998
999				/*
1000				 * This is the first time we hear
1001				 * from the server, so note it's
1002				 * unicast address, replacing the
1003				 * broadcast address .
1004				 */
1005				bcopy(wh->eh.ether_shost,
1006					neg->pkt->pkt_header.eh.ether_dhost,
1007					ETHER_ADDR_LEN);
1008				neg->timeout = 0;
1009				neg->pkt->pkt_header.ph.code = PADR_CODE;
1010				init_tags(sp);
1011				insert_tag(sp, utag);      /* Host Unique */
1012				if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1013					insert_tag(sp, tag); /* return cookie */
1014				if ((tag = get_tag(ph, PTT_AC_NAME)))
1015					insert_tag(sp, tag); /* return it */
1016				insert_tag(sp, &neg->service.hdr); /* Service */
1017				scan_tags(sp, ph);
1018				make_packet(sp);
1019				sp->state = PPPOE_SREQ;
1020				sendpacket(sp);
1021				break;
1022			case	PADR_CODE:
1023
1024				/*
1025				 * We are a server:
1026				 * Use the ac_cookie tag to find the
1027				 * hook this is in response to.
1028				 */
1029				utag = get_tag(ph, PTT_AC_COOKIE);
1030				if ((utag == NULL)
1031				|| (ntohs(utag->tag_len) != sizeof(sp))) {
1032					LEAVE(ENETUNREACH);
1033				}
1034
1035				sendhook = pppoe_finduniq(node, utag);
1036				if (sendhook == NULL) {
1037					LEAVE(ENETUNREACH);
1038				}
1039
1040				/*
1041				 * Check the session is in the right state.
1042				 * It needs to be in PPPOE_SOFFER
1043				 * or PPPOE_NEWCONNECTED. If the latter,
1044				 * then this is a retry by the client.
1045				 * so be nice, and resend.
1046				 */
1047				sp = NG_HOOK_PRIVATE(sendhook);
1048				if (sp->state == PPPOE_NEWCONNECTED) {
1049					/*
1050					 * Whoa! drop back to resend that
1051					 * PADS packet.
1052					 * We should still have a copy of it.
1053					 */
1054					sp->state = PPPOE_SOFFER;
1055				}
1056				if (sp->state != PPPOE_SOFFER) {
1057					LEAVE (ENETUNREACH);
1058					break;
1059				}
1060				neg = sp->neg;
1061				untimeout(pppoe_ticker, sendhook,
1062				    neg->timeout_handle);
1063				neg->pkt->pkt_header.ph.code = PADS_CODE;
1064				if (sp->Session_ID == 0)
1065					neg->pkt->pkt_header.ph.sid =
1066					    htons(sp->Session_ID
1067						= get_new_sid(node));
1068				neg->timeout = 0;
1069				/*
1070				 * start working out the tags to respond with.
1071				 */
1072				init_tags(sp);
1073				insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1074				if ((tag = get_tag(ph, PTT_SRV_NAME)))
1075					insert_tag(sp, tag);/* return service */
1076				if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1077					insert_tag(sp, tag); /* return it */
1078				insert_tag(sp, utag);	/* ac_cookie */
1079				scan_tags(sp, ph);
1080				make_packet(sp);
1081				sp->state = PPPOE_NEWCONNECTED;
1082				sendpacket(sp);
1083				/*
1084				 * Having sent the last Negotiation header,
1085				 * Set up the stored packet header to
1086				 * be correct for the actual session.
1087				 * But keep the negotialtion stuff
1088				 * around in case we need to resend this last
1089				 * packet. We'll discard it when we move
1090				 * from NEWCONNECTED to CONNECTED
1091				 */
1092				sp->pkt_hdr = neg->pkt->pkt_header;
1093				sp->pkt_hdr.eh.ether_type
1094						= ETHERTYPE_PPPOE_SESS;
1095				sp->pkt_hdr.ph.code = 0;
1096				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1097				break;
1098			case	PADS_CODE:
1099				/*
1100				 * We are a client:
1101				 * Use the host_uniq tag to find the
1102				 * hook this is in response to.
1103				 * take the session ID and store it away.
1104				 * Also make sure the pre-made header is
1105				 * correct and set us into Session mode.
1106				 */
1107				utag = get_tag(ph, PTT_HOST_UNIQ);
1108				if ((utag == NULL)
1109				|| (ntohs(utag->tag_len) != sizeof(sp))) {
1110					LEAVE (ENETUNREACH);
1111					break;
1112				}
1113				sendhook = pppoe_finduniq(node, utag);
1114				if (sendhook == NULL) {
1115					LEAVE(ENETUNREACH);
1116				}
1117
1118				/*
1119				 * Check the session is in the right state.
1120				 * It needs to be in PPPOE_SREQ.
1121				 */
1122				sp = NG_HOOK_PRIVATE(sendhook);
1123				if (sp->state != PPPOE_SREQ) {
1124					LEAVE(ENETUNREACH);
1125				}
1126				neg = sp->neg;
1127				untimeout(pppoe_ticker, sendhook,
1128				    neg->timeout_handle);
1129				neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1130				sp->Session_ID = ntohs(wh->ph.sid);
1131				neg->timeout = 0;
1132				sp->state = PPPOE_CONNECTED;
1133				/*
1134				 * Now we have gone to Connected mode,
1135				 * Free all resources needed for
1136				 * negotiation.
1137				 * Keep a copy of the header we will be using.
1138				 */
1139				sp->pkt_hdr = neg->pkt->pkt_header;
1140				sp->pkt_hdr.eh.ether_type
1141						= ETHERTYPE_PPPOE_SESS;
1142				sp->pkt_hdr.ph.code = 0;
1143				m_freem(neg->m);
1144				FREE(sp->neg, M_NETGRAPH);
1145				sp->neg = NULL;
1146				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1147				break;
1148			case	PADT_CODE:
1149				/*
1150				 * Send a 'close' message to the controlling
1151				 * process (the one that set us up);
1152				 * And then tear everything down.
1153				 *
1154				 * Find matching peer/session combination.
1155				 */
1156				sendhook = pppoe_findsession(node, wh);
1157				if (sendhook == NULL) {
1158					LEAVE(ENETUNREACH);
1159				}
1160				/* send message to creator */
1161				/* close hook */
1162				if (sendhook) {
1163					ng_destroy_hook(sendhook);
1164				}
1165				break;
1166			default:
1167				LEAVE(EPFNOSUPPORT);
1168			}
1169			break;
1170		case	ETHERTYPE_PPPOE_SESS:
1171			/*
1172			 * find matching peer/session combination.
1173			 */
1174			sendhook = pppoe_findsession(node, wh);
1175			if (sendhook == NULL) {
1176				LEAVE (ENETUNREACH);
1177				break;
1178			}
1179			sp = NG_HOOK_PRIVATE(sendhook);
1180			m_adj(m, sizeof(*wh));
1181			if (m->m_pkthdr.len < length) {
1182				/* Packet too short, dump it */
1183				LEAVE(EMSGSIZE);
1184			}
1185
1186			/* Also need to trim excess at the end */
1187			if (m->m_pkthdr.len > length) {
1188				m_adj(m, -((int)(m->m_pkthdr.len - length)));
1189			}
1190			if ( sp->state != PPPOE_CONNECTED) {
1191				if (sp->state == PPPOE_NEWCONNECTED) {
1192					sp->state = PPPOE_CONNECTED;
1193					/*
1194					 * Now we have gone to Connected mode,
1195					 * Free all resources needed for
1196					 * negotiation. Be paranoid about
1197					 * whether there may be a timeout.
1198					 */
1199					m_freem(sp->neg->m);
1200					untimeout(pppoe_ticker, sendhook,
1201				    		sp->neg->timeout_handle);
1202					FREE(sp->neg, M_NETGRAPH);
1203					sp->neg = NULL;
1204				} else {
1205					LEAVE (ENETUNREACH);
1206					break;
1207				}
1208			}
1209			NG_FWD_NEW_DATA( error, item, sendhook, m);
1210			break;
1211		default:
1212			LEAVE(EPFNOSUPPORT);
1213		}
1214	} else {
1215		/*
1216		 * 	Not ethernet or debug hook..
1217		 *
1218		 * The packet has come in on a normal hook.
1219		 * We need to find out what kind of hook,
1220		 * So we can decide how to handle it.
1221		 * Check the hook's state.
1222		 */
1223		sp = NG_HOOK_PRIVATE(hook);
1224		switch (sp->state) {
1225		case	PPPOE_NEWCONNECTED:
1226		case	PPPOE_CONNECTED: {
1227			static const u_char addrctrl[] = { 0xff, 0x03 };
1228			struct pppoe_full_hdr *wh;
1229
1230			/*
1231			 * Remove PPP address and control fields, if any.
1232			 * For example, ng_ppp(4) always sends LCP packets
1233			 * with address and control fields as required by
1234			 * generic PPP. PPPoE is an exception to the rule.
1235			 */
1236			if (m->m_pkthdr.len >= 2) {
1237				if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1238					LEAVE(ENOBUFS);
1239				if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1240					m_adj(m, 2);
1241			}
1242			/*
1243			 * Bang in a pre-made header, and set the length up
1244			 * to be correct. Then send it to the ethernet driver.
1245			 * But first correct the length.
1246			 */
1247			sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1248			M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1249			if (m == NULL) {
1250				LEAVE(ENOBUFS);
1251			}
1252			wh = mtod(m, struct pppoe_full_hdr *);
1253			bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1254			NG_FWD_NEW_DATA( error, item, privp->ethernet_hook, m);
1255			privp->packets_out++;
1256			break;
1257			}
1258		case	PPPOE_PRIMED:
1259			/*
1260			 * A PADI packet is being returned by the application
1261			 * that has set up this hook. This indicates that it
1262			 * wants us to offer service.
1263			 */
1264			neg = sp->neg;
1265			if (m->m_len < sizeof(*wh)) {
1266				m = m_pullup(m, sizeof(*wh));
1267				if (m == NULL) {
1268					LEAVE(ENOBUFS);
1269				}
1270			}
1271			wh = mtod(m, struct pppoe_full_hdr *);
1272			ph = &wh->ph;
1273			session = ntohs(wh->ph.sid);
1274			length = ntohs(wh->ph.length);
1275			code = wh->ph.code;
1276			if ( code != PADI_CODE) {
1277				LEAVE(EINVAL);
1278			};
1279			untimeout(pppoe_ticker, hook,
1280				    neg->timeout_handle);
1281
1282			/*
1283			 * This is the first time we hear
1284			 * from the client, so note it's
1285			 * unicast address, replacing the
1286			 * broadcast address.
1287			 */
1288			bcopy(wh->eh.ether_shost,
1289				neg->pkt->pkt_header.eh.ether_dhost,
1290				ETHER_ADDR_LEN);
1291			sp->state = PPPOE_SOFFER;
1292			neg->timeout = 0;
1293			neg->pkt->pkt_header.ph.code = PADO_CODE;
1294
1295			/*
1296			 * start working out the tags to respond with.
1297			 */
1298			uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1299			uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1300			uniqtag.data.pointer = sp;
1301			init_tags(sp);
1302			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1303			if ((tag = get_tag(ph, PTT_SRV_NAME)))
1304				insert_tag(sp, tag);	  /* return service */
1305			/*
1306			 * If we have a NULL service request
1307			 * and have an extra service defined in this hook,
1308			 * then also add a tag for the extra service.
1309			 * XXX this is a hack. eventually we should be able
1310			 * to support advertising many services, not just one
1311			 */
1312			if (((tag == NULL) || (tag->tag_len == 0))
1313			&& (neg->service.hdr.tag_len != 0)) {
1314				insert_tag(sp, &neg->service.hdr); /* SERVICE */
1315			}
1316			if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1317				insert_tag(sp, tag); /* returned hostunique */
1318			insert_tag(sp, &uniqtag.hdr);
1319			scan_tags(sp, ph);
1320			make_packet(sp);
1321			sendpacket(sp);
1322			break;
1323
1324		/*
1325		 * Packets coming from the hook make no sense
1326		 * to sessions in these states. Throw them away.
1327		 */
1328		case	PPPOE_SINIT:
1329		case	PPPOE_SREQ:
1330		case	PPPOE_SOFFER:
1331		case	PPPOE_SNONE:
1332		case	PPPOE_LISTENING:
1333		case	PPPOE_DEAD:
1334		default:
1335			LEAVE(ENETUNREACH);
1336		}
1337	}
1338quit:
1339	NG_FREE_ITEM(item);
1340	NG_FREE_M(m);
1341	return error;
1342}
1343
1344/*
1345 * Do local shutdown processing..
1346 * If we are a persistant device, we might refuse to go away, and
1347 * we'd only remove our links and reset ourself.
1348 */
1349static int
1350ng_pppoe_shutdown(node_p node)
1351{
1352	const priv_p privdata = NG_NODE_PRIVATE(node);
1353
1354AAA
1355	NG_NODE_SET_PRIVATE(node, NULL);
1356	NG_NODE_UNREF(privdata->node);
1357	FREE(privdata, M_NETGRAPH);
1358	return (0);
1359}
1360
1361/*
1362 * This is called once we've already connected a new hook to the other node.
1363 * It gives us a chance to balk at the last minute.
1364 */
1365static int
1366ng_pppoe_connect(hook_p hook)
1367{
1368	/* be really amiable and just say "YUP that's OK by me! " */
1369	return (0);
1370}
1371
1372/*
1373 * Hook disconnection
1374 *
1375 * Clean up all dangling links and information about the session/hook.
1376 * For this type, removal of the last link destroys the node
1377 */
1378static int
1379ng_pppoe_disconnect(hook_p hook)
1380{
1381	node_p node = NG_HOOK_NODE(hook);
1382	priv_p privp = NG_NODE_PRIVATE(node);
1383	sessp	sp;
1384	int 	hooks;
1385
1386AAA
1387	hooks = NG_NODE_NUMHOOKS(node); /* this one already not counted */
1388	if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
1389		privp->debug_hook = NULL;
1390	} else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
1391		privp->ethernet_hook = NULL;
1392		if (NG_NODE_IS_VALID(node))
1393			ng_rmnode_self(node);
1394	} else {
1395		sp = NG_HOOK_PRIVATE(hook);
1396		if (sp->state != PPPOE_SNONE ) {
1397			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1398		}
1399		/*
1400		 * According to the spec, if we are connected,
1401		 * we should send a DISC packet if we are shutting down
1402		 * a session.
1403		 */
1404		if ((privp->ethernet_hook)
1405		&& ((sp->state == PPPOE_CONNECTED)
1406		 || (sp->state == PPPOE_NEWCONNECTED))) {
1407			struct mbuf *m;
1408			struct pppoe_full_hdr *wh;
1409			struct pppoe_tag *tag;
1410			int	msglen = strlen(SIGNOFF);
1411			int error = 0;
1412
1413			/* revert the stored header to DISC/PADT mode */
1414		 	wh = &sp->pkt_hdr;
1415			wh->ph.code = PADT_CODE;
1416			wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1417
1418			/* generate a packet of that type */
1419			MGETHDR(m, M_DONTWAIT, MT_DATA);
1420			if(m == NULL)
1421				printf("pppoe: Session out of mbufs\n");
1422			else {
1423				m->m_pkthdr.rcvif = NULL;
1424				m->m_pkthdr.len = m->m_len = sizeof(*wh);
1425				bcopy((caddr_t)wh, mtod(m, caddr_t),
1426				    sizeof(*wh));
1427				/*
1428				 * Add a General error message and adjust
1429				 * sizes
1430				 */
1431				wh = mtod(m, struct pppoe_full_hdr *);
1432				tag = wh->ph.tag;
1433				tag->tag_type = PTT_GEN_ERR;
1434				tag->tag_len = htons((u_int16_t)msglen);
1435				strncpy(tag->tag_data, SIGNOFF, msglen);
1436				m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1437				    msglen);
1438				wh->ph.length = htons(sizeof(*tag) + msglen);
1439				NG_SEND_DATA_ONLY(error,
1440					privp->ethernet_hook, m);
1441			}
1442		}
1443		/*
1444		 * As long as we have somewhere to store the timeout handle,
1445		 * we may have a timeout pending.. get rid of it.
1446		 */
1447		if (sp->neg) {
1448			untimeout(pppoe_ticker, hook, sp->neg->timeout_handle);
1449			if (sp->neg->m)
1450				m_freem(sp->neg->m);
1451			FREE(sp->neg, M_NETGRAPH);
1452		}
1453		FREE(sp, M_NETGRAPH);
1454		NG_HOOK_SET_PRIVATE(hook, NULL);
1455		/* work out how many session hooks there are */
1456		/* Node goes away on last session hook removal */
1457		if (privp->ethernet_hook) hooks -= 1;
1458		if (privp->debug_hook) hooks -= 1;
1459	}
1460	if ((NG_NODE_NUMHOOKS(node) == 0)
1461	&& (NG_NODE_IS_VALID(node)))
1462		ng_rmnode_self(node);
1463	return (0);
1464}
1465
1466/*
1467 * timeouts come here.
1468 */
1469static void
1470pppoe_ticker(void *arg)
1471{
1472	int s = splnet();
1473	hook_p hook = arg;
1474	sessp	sp = NG_HOOK_PRIVATE(hook);
1475	negp	neg = sp->neg;
1476	int	error = 0;
1477	struct mbuf *m0 = NULL;
1478	priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1479
1480AAA
1481	switch(sp->state) {
1482		/*
1483		 * resend the last packet, using an exponential backoff.
1484		 * After a period of time, stop growing the backoff,
1485		 * and either leave it, or revert to the start.
1486		 */
1487	case	PPPOE_SINIT:
1488	case	PPPOE_SREQ:
1489		/* timeouts on these produce resends */
1490		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1491		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1492		neg->timeout_handle = timeout(pppoe_ticker,
1493					hook, neg->timeout * hz);
1494		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1495			if (sp->state == PPPOE_SREQ) {
1496				/* revert to SINIT mode */
1497				pppoe_start(sp);
1498			} else {
1499				neg->timeout = PPPOE_TIMEOUT_LIMIT;
1500			}
1501		}
1502		break;
1503	case	PPPOE_PRIMED:
1504	case	PPPOE_SOFFER:
1505		/* a timeout on these says "give up" */
1506		ng_destroy_hook(hook);
1507		break;
1508	default:
1509		/* timeouts have no meaning in other states */
1510		printf("pppoe: unexpected timeout\n");
1511	}
1512	splx(s);
1513}
1514
1515
1516static void
1517sendpacket(sessp sp)
1518{
1519	int	error = 0;
1520	struct mbuf *m0 = NULL;
1521	hook_p hook = sp->hook;
1522	negp	neg = sp->neg;
1523	priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1524
1525AAA
1526	switch(sp->state) {
1527	case	PPPOE_LISTENING:
1528	case	PPPOE_DEAD:
1529	case	PPPOE_SNONE:
1530	case	PPPOE_CONNECTED:
1531		printf("pppoe: sendpacket: unexpected state\n");
1532		break;
1533
1534	case	PPPOE_NEWCONNECTED:
1535		/* send the PADS without a timeout - we're now connected */
1536		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1537		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1538		break;
1539
1540	case	PPPOE_PRIMED:
1541		/* No packet to send, but set up the timeout */
1542		neg->timeout_handle = timeout(pppoe_ticker,
1543					hook, PPPOE_OFFER_TIMEOUT * hz);
1544		break;
1545
1546	case	PPPOE_SOFFER:
1547		/*
1548		 * send the offer but if they don't respond
1549		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1550		 */
1551		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1552		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1553		neg->timeout_handle = timeout(pppoe_ticker,
1554					hook, PPPOE_OFFER_TIMEOUT * hz);
1555		break;
1556
1557	case	PPPOE_SINIT:
1558	case	PPPOE_SREQ:
1559		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1560		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1561		neg->timeout_handle = timeout(pppoe_ticker, hook,
1562					(hz * PPPOE_INITIAL_TIMEOUT));
1563		neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1564		break;
1565
1566	default:
1567		error = EINVAL;
1568		printf("pppoe: timeout: bad state\n");
1569	}
1570	/* return (error); */
1571}
1572
1573/*
1574 * Parse an incoming packet to see if any tags should be copied to the
1575 * output packet. Don't do any tags that have been handled in the main
1576 * state machine.
1577 */
1578static struct pppoe_tag*
1579scan_tags(sessp	sp, struct pppoe_hdr* ph)
1580{
1581	char *end = (char *)next_tag(ph);
1582	char *ptn;
1583	struct pppoe_tag *pt = &ph->tag[0];
1584	/*
1585	 * Keep processing tags while a tag header will still fit.
1586	 */
1587AAA
1588	while((char*)(pt + 1) <= end) {
1589		/*
1590		 * If the tag data would go past the end of the packet, abort.
1591		 */
1592		ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
1593		if(ptn > end)
1594			return NULL;
1595
1596		switch (pt->tag_type) {
1597		case	PTT_RELAY_SID:
1598			insert_tag(sp, pt);
1599			break;
1600		case	PTT_EOL:
1601			return NULL;
1602		case	PTT_SRV_NAME:
1603		case	PTT_AC_NAME:
1604		case	PTT_HOST_UNIQ:
1605		case	PTT_AC_COOKIE:
1606		case	PTT_VENDOR:
1607		case	PTT_SRV_ERR:
1608		case	PTT_SYS_ERR:
1609		case	PTT_GEN_ERR:
1610			break;
1611		}
1612		pt = (struct pppoe_tag*)ptn;
1613	}
1614	return NULL;
1615}
1616
1617static	int
1618pppoe_send_event(sessp sp, enum cmd cmdid)
1619{
1620	int error;
1621	struct ng_mesg *msg;
1622	struct ngpppoe_sts *sts;
1623
1624AAA
1625	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1626			sizeof(struct ngpppoe_sts), M_NOWAIT);
1627	if (msg == NULL)
1628		return (ENOMEM);
1629	sts = (struct ngpppoe_sts *)msg->data;
1630	strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKLEN + 1);
1631	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, NULL);
1632	return (error);
1633}
1634