xref: /freebsd/sys/netgraph/ng_pppoe.c (revision f795fd004dbeb41e083d9b105d88fb0fce974542)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * ng_pppoe.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
64cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
74cf49a43SJulian Elischer  * All rights reserved.
84cf49a43SJulian Elischer  *
94cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
104cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
114cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
124cf49a43SJulian Elischer  * provided, however, that:
134cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
144cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
154cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
164cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
174cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
184cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
194cf49a43SJulian Elischer  *
204cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
214cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
224cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
234cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
244cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
254cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
264cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
274cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
284cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
294cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
314cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
324cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
334cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
354cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
364cf49a43SJulian Elischer  * OF SUCH DAMAGE.
374cf49a43SJulian Elischer  *
38cc3bbd68SJulian Elischer  * Author: Julian Elischer <julian@freebsd.org>
394cf49a43SJulian Elischer  *
404cf49a43SJulian Elischer  * $FreeBSD$
4174f5c6aaSJulian Elischer  * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
424cf49a43SJulian Elischer  */
431e2510f8SJulian Elischer #if 0
44ec774932SGleb Smirnoff #define	DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
451e2510f8SJulian Elischer #else
46ec774932SGleb Smirnoff #define	DBG do {} while (0)
471e2510f8SJulian Elischer #endif
484cf49a43SJulian Elischer 
494cf49a43SJulian Elischer #include <sys/param.h>
504cf49a43SJulian Elischer #include <sys/systm.h>
514cf49a43SJulian Elischer #include <sys/kernel.h>
524cf49a43SJulian Elischer #include <sys/mbuf.h>
534cf49a43SJulian Elischer #include <sys/malloc.h>
544cf49a43SJulian Elischer #include <sys/errno.h>
55875467faSYaroslav Tykhiy #include <sys/syslog.h>
564cf49a43SJulian Elischer #include <net/ethernet.h>
574cf49a43SJulian Elischer 
584cf49a43SJulian Elischer #include <netgraph/ng_message.h>
594cf49a43SJulian Elischer #include <netgraph/netgraph.h>
6076a70671SBrian Somers #include <netgraph/ng_parse.h>
614cf49a43SJulian Elischer #include <netgraph/ng_pppoe.h>
624cf49a43SJulian Elischer 
639c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
649c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
659c8c302fSJulian Elischer #else
669c8c302fSJulian Elischer #define M_NETGRAPH_PPPOE M_NETGRAPH
679c8c302fSJulian Elischer #endif
689c8c302fSJulian Elischer 
69da092930SArchie Cobbs #define SIGNOFF "session closed"
7076a70671SBrian Somers #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
71da092930SArchie Cobbs 
724cf49a43SJulian Elischer /*
734cf49a43SJulian Elischer  * This section contains the netgraph method declarations for the
74bfa7e882SJulian Elischer  * pppoe node. These methods define the netgraph pppoe 'type'.
754cf49a43SJulian Elischer  */
764cf49a43SJulian Elischer 
7774f5c6aaSJulian Elischer static ng_constructor_t	ng_pppoe_constructor;
7874f5c6aaSJulian Elischer static ng_rcvmsg_t	ng_pppoe_rcvmsg;
79069154d5SJulian Elischer static ng_shutdown_t	ng_pppoe_shutdown;
8074f5c6aaSJulian Elischer static ng_newhook_t	ng_pppoe_newhook;
8174f5c6aaSJulian Elischer static ng_rcvdata_t	ng_pppoe_rcvdata;
8274f5c6aaSJulian Elischer static ng_disconnect_t	ng_pppoe_disconnect;
834cf49a43SJulian Elischer 
8476a70671SBrian Somers /* Parse type for struct ngpppoe_init_data */
85f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
8676a70671SBrian Somers 	= NG_PPPOE_INIT_DATA_TYPE_INFO;
8727121ab1SBrian Somers static const struct ng_parse_type ngpppoe_init_data_state_type = {
8876a70671SBrian Somers 	&ng_parse_struct_type,
89f0184ff8SArchie Cobbs 	&ngpppoe_init_data_type_fields
9076a70671SBrian Somers };
9176a70671SBrian Somers 
9276a70671SBrian Somers /* Parse type for struct ngpppoe_sts */
93f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
9476a70671SBrian Somers 	= NG_PPPOE_STS_TYPE_INFO;
9576a70671SBrian Somers static const struct ng_parse_type ng_pppoe_sts_state_type = {
9676a70671SBrian Somers 	&ng_parse_struct_type,
97f0184ff8SArchie Cobbs 	&ng_pppoe_sts_type_fields
9876a70671SBrian Somers };
9976a70671SBrian Somers 
10076a70671SBrian Somers /* List of commands and how to convert arguments to/from ASCII */
10176a70671SBrian Somers static const struct ng_cmdlist ng_pppoe_cmds[] = {
10276a70671SBrian Somers 	{
10376a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
10476a70671SBrian Somers 	  NGM_PPPOE_CONNECT,
10576a70671SBrian Somers 	  "pppoe_connect",
10627121ab1SBrian Somers 	  &ngpppoe_init_data_state_type,
10776a70671SBrian Somers 	  NULL
10876a70671SBrian Somers 	},
10976a70671SBrian Somers 	{
11076a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
11176a70671SBrian Somers 	  NGM_PPPOE_LISTEN,
11276a70671SBrian Somers 	  "pppoe_listen",
11327121ab1SBrian Somers 	  &ngpppoe_init_data_state_type,
11476a70671SBrian Somers 	  NULL
11576a70671SBrian Somers 	},
11676a70671SBrian Somers 	{
11776a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
11876a70671SBrian Somers 	  NGM_PPPOE_OFFER,
11976a70671SBrian Somers 	  "pppoe_offer",
12027121ab1SBrian Somers 	  &ngpppoe_init_data_state_type,
12176a70671SBrian Somers 	  NULL
12276a70671SBrian Somers 	},
12376a70671SBrian Somers 	{
12476a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
125859a4d16SJulian Elischer 	  NGM_PPPOE_SERVICE,
126859a4d16SJulian Elischer 	  "pppoe_service",
127859a4d16SJulian Elischer 	  &ngpppoe_init_data_state_type,
128859a4d16SJulian Elischer 	  NULL
129859a4d16SJulian Elischer 	},
130859a4d16SJulian Elischer 	{
131859a4d16SJulian Elischer 	  NGM_PPPOE_COOKIE,
13276a70671SBrian Somers 	  NGM_PPPOE_SUCCESS,
13376a70671SBrian Somers 	  "pppoe_success",
13476a70671SBrian Somers 	  &ng_pppoe_sts_state_type,
13576a70671SBrian Somers 	  NULL
13676a70671SBrian Somers 	},
13776a70671SBrian Somers 	{
13876a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
13976a70671SBrian Somers 	  NGM_PPPOE_FAIL,
14076a70671SBrian Somers 	  "pppoe_fail",
14176a70671SBrian Somers 	  &ng_pppoe_sts_state_type,
14276a70671SBrian Somers 	  NULL
14376a70671SBrian Somers 	},
14476a70671SBrian Somers 	{
14576a70671SBrian Somers 	  NGM_PPPOE_COOKIE,
14676a70671SBrian Somers 	  NGM_PPPOE_CLOSE,
14776a70671SBrian Somers 	  "pppoe_close",
14876a70671SBrian Somers 	  &ng_pppoe_sts_state_type,
14976a70671SBrian Somers 	  NULL
15076a70671SBrian Somers 	},
151fdc755d1SGleb Smirnoff 	{
152fdc755d1SGleb Smirnoff 	  NGM_PPPOE_COOKIE,
153fdc755d1SGleb Smirnoff 	  NGM_PPPOE_SETMODE,
154fdc755d1SGleb Smirnoff 	  "pppoe_setmode",
155fdc755d1SGleb Smirnoff 	  &ng_parse_string_type,
156fdc755d1SGleb Smirnoff 	  NULL
157fdc755d1SGleb Smirnoff 	},
158fdc755d1SGleb Smirnoff 	{
159fdc755d1SGleb Smirnoff 	  NGM_PPPOE_COOKIE,
160fdc755d1SGleb Smirnoff 	  NGM_PPPOE_GETMODE,
161fdc755d1SGleb Smirnoff 	  "pppoe_getmode",
162fdc755d1SGleb Smirnoff 	  NULL,
163fdc755d1SGleb Smirnoff 	  &ng_parse_string_type
164fdc755d1SGleb Smirnoff 	},
16576a70671SBrian Somers 	{ 0 }
16676a70671SBrian Somers };
16776a70671SBrian Somers 
1684cf49a43SJulian Elischer /* Netgraph node type descriptor */
1694cf49a43SJulian Elischer static struct ng_type typestruct = {
170f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
171f8aae777SJulian Elischer 	.name =		NG_PPPOE_NODE_TYPE,
172f8aae777SJulian Elischer 	.constructor =	ng_pppoe_constructor,
173f8aae777SJulian Elischer 	.rcvmsg =	ng_pppoe_rcvmsg,
174f8aae777SJulian Elischer 	.shutdown =	ng_pppoe_shutdown,
175f8aae777SJulian Elischer 	.newhook =	ng_pppoe_newhook,
176f8aae777SJulian Elischer 	.rcvdata =	ng_pppoe_rcvdata,
177f8aae777SJulian Elischer 	.disconnect =	ng_pppoe_disconnect,
178f8aae777SJulian Elischer 	.cmdlist =	ng_pppoe_cmds,
1794cf49a43SJulian Elischer };
1808876b55dSJulian Elischer NETGRAPH_INIT(pppoe, &typestruct);
1814cf49a43SJulian Elischer 
1824cf49a43SJulian Elischer /*
1834cf49a43SJulian Elischer  * States for the session state machine.
1844cf49a43SJulian Elischer  * These have no meaning if there is no hook attached yet.
1854cf49a43SJulian Elischer  */
1864cf49a43SJulian Elischer enum state {
1874cf49a43SJulian Elischer     PPPOE_SNONE=0,	/* [both] Initial state */
1886faf164cSJulian Elischer     PPPOE_LISTENING,	/* [Daemon] Listening for discover initiation pkt */
1894cf49a43SJulian Elischer     PPPOE_SINIT,	/* [Client] Sent discovery initiation */
1906faf164cSJulian Elischer     PPPOE_PRIMED,	/* [Server] Awaiting PADI from daemon */
1916faf164cSJulian Elischer     PPPOE_SOFFER,	/* [Server] Sent offer message  (got PADI)*/
1924cf49a43SJulian Elischer     PPPOE_SREQ,		/* [Client] Sent a Request */
1936faf164cSJulian Elischer     PPPOE_NEWCONNECTED,	/* [Server] Connection established, No data received */
1944cf49a43SJulian Elischer     PPPOE_CONNECTED,	/* [Both] Connection established, Data received */
1954cf49a43SJulian Elischer     PPPOE_DEAD		/* [Both] */
1964cf49a43SJulian Elischer };
1974cf49a43SJulian Elischer 
1984cf49a43SJulian Elischer #define NUMTAGS 20 /* number of tags we are set up to work with */
1994cf49a43SJulian Elischer 
2004cf49a43SJulian Elischer /*
2014cf49a43SJulian Elischer  * Information we store for each hook on each node for negotiating the
2024cf49a43SJulian Elischer  * session. The mbuf and cluster are freed once negotiation has completed.
2034cf49a43SJulian Elischer  * The whole negotiation block is then discarded.
2044cf49a43SJulian Elischer  */
2054cf49a43SJulian Elischer 
2064cf49a43SJulian Elischer struct sess_neg {
2074cf49a43SJulian Elischer 	struct mbuf 		*m; /* holds cluster with last sent packet */
2084cf49a43SJulian Elischer 	union	packet		*pkt; /* points within the above cluster */
209ef237c7fSGleb Smirnoff 	struct callout		handle;   /* see timeout(9) */
2104cf49a43SJulian Elischer 	u_int			timeout; /* 0,1,2,4,8,16 etc. seconds */
2114cf49a43SJulian Elischer 	u_int			numtags;
212816b834fSArchie Cobbs 	const struct pppoe_tag	*tags[NUMTAGS];
2134cf49a43SJulian Elischer 	u_int			service_len;
2144cf49a43SJulian Elischer 	u_int			ac_name_len;
2154cf49a43SJulian Elischer 
2164cf49a43SJulian Elischer 	struct datatag		service;
2174cf49a43SJulian Elischer 	struct datatag		ac_name;
2184cf49a43SJulian Elischer };
2194cf49a43SJulian Elischer typedef struct sess_neg *negp;
2204cf49a43SJulian Elischer 
2214cf49a43SJulian Elischer /*
2224cf49a43SJulian Elischer  * Session information that is needed after connection.
2234cf49a43SJulian Elischer  */
2242b9cf2f7SArchie Cobbs struct sess_con {
2254cf49a43SJulian Elischer 	hook_p  		hook;
2264cf49a43SJulian Elischer 	u_int16_t		Session_ID;
2274cf49a43SJulian Elischer 	enum state		state;
228069154d5SJulian Elischer 	ng_ID_t			creator;		/* who to notify */
2294cf49a43SJulian Elischer 	struct pppoe_full_hdr	pkt_hdr;	/* used when connected */
2304cf49a43SJulian Elischer 	negp			neg;		/* used when negotiating */
2312b9cf2f7SArchie Cobbs 	/*struct sess_con	*hash_next;*/	/* not yet used */
2324cf49a43SJulian Elischer };
2332b9cf2f7SArchie Cobbs typedef struct sess_con *sessp;
2344cf49a43SJulian Elischer 
235fdc755d1SGleb Smirnoff #define	NG_PPPOE_SESSION_NODE(sp) NG_HOOK_NODE(sp->hook)
236fdc755d1SGleb Smirnoff 
237fdc755d1SGleb Smirnoff enum {
238fdc755d1SGleb Smirnoff 	PPPOE_STANDARD	= 1,	/* standard RFC2516 mode */
239fdc755d1SGleb Smirnoff 	PPPOE_NONSTANDARD,	/* 3Com proprietary mode */
240fdc755d1SGleb Smirnoff };
241fdc755d1SGleb Smirnoff 
242fdc755d1SGleb Smirnoff struct ng_pppoe_mode_t {
243fdc755d1SGleb Smirnoff 	u_int8_t        		id;
244fdc755d1SGleb Smirnoff 	const struct ether_header	*eh_prototype;
245fdc755d1SGleb Smirnoff 	const char      		*name;
246fdc755d1SGleb Smirnoff };
247fdc755d1SGleb Smirnoff 
248fdc755d1SGleb Smirnoff static const struct ether_header eh_standard =
249fdc755d1SGleb Smirnoff 	{{0xff,0xff,0xff,0xff,0xff,0xff},
250fdc755d1SGleb Smirnoff 	{0x00,0x00,0x00,0x00,0x00,0x00},
251fdc755d1SGleb Smirnoff 	ETHERTYPE_PPPOE_DISC};
252fdc755d1SGleb Smirnoff 
253fdc755d1SGleb Smirnoff static const struct ether_header eh_3Com =
254fdc755d1SGleb Smirnoff 	{{0xff,0xff,0xff,0xff,0xff,0xff},
255fdc755d1SGleb Smirnoff 	{0x00,0x00,0x00,0x00,0x00,0x00},
256fdc755d1SGleb Smirnoff 	ETHERTYPE_PPPOE_STUPID_DISC};
257fdc755d1SGleb Smirnoff 
258fdc755d1SGleb Smirnoff static const struct ng_pppoe_mode_t ng_pppoe_modes[] = {
259fdc755d1SGleb Smirnoff 	{ PPPOE_STANDARD,	&eh_standard,	NG_PPPOE_STANDARD },
260fdc755d1SGleb Smirnoff 	{ PPPOE_NONSTANDARD,	&eh_3Com,	NG_PPPOE_NONSTANDARD },
261fdc755d1SGleb Smirnoff 	{ 0, NULL},
262fdc755d1SGleb Smirnoff };
263fdc755d1SGleb Smirnoff 
2644cf49a43SJulian Elischer /*
2654cf49a43SJulian Elischer  * Information we store for each node
2664cf49a43SJulian Elischer  */
2674cf49a43SJulian Elischer struct PPPOE {
2684cf49a43SJulian Elischer 	node_p		node;		/* back pointer to node */
2694cf49a43SJulian Elischer 	hook_p  	ethernet_hook;
2704cf49a43SJulian Elischer 	hook_p  	debug_hook;
2714cf49a43SJulian Elischer 	u_int   	packets_in;	/* packets in from ethernet */
2724cf49a43SJulian Elischer 	u_int   	packets_out;	/* packets out towards ethernet */
2734cf49a43SJulian Elischer 	u_int32_t	flags;
274fdc755d1SGleb Smirnoff 	const struct ng_pppoe_mode_t	*mode;	/* standard PPPoE or 3Com? */
2752b9cf2f7SArchie Cobbs 	/*struct sess_con *buckets[HASH_SIZE];*/	/* not yet used */
2764cf49a43SJulian Elischer };
2774cf49a43SJulian Elischer typedef struct PPPOE *priv_p;
2784cf49a43SJulian Elischer 
2794cf49a43SJulian Elischer union uniq {
2804cf49a43SJulian Elischer 	char bytes[sizeof(void *)];
2814cf49a43SJulian Elischer 	void * pointer;
2824cf49a43SJulian Elischer 	};
2834cf49a43SJulian Elischer 
2844cf49a43SJulian Elischer #define	LEAVE(x) do { error = x; goto quit; } while(0)
2854cf49a43SJulian Elischer static void	pppoe_start(sessp sp);
2864cf49a43SJulian Elischer static void	sendpacket(sessp sp);
287ef237c7fSGleb Smirnoff static void	pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
288816b834fSArchie Cobbs static const	struct pppoe_tag *scan_tags(sessp sp,
289816b834fSArchie Cobbs 			const struct pppoe_hdr* ph);
290b58a8a3bSJulian Elischer static	int	pppoe_send_event(sessp sp, enum cmd cmdid);
2914cf49a43SJulian Elischer 
2924cf49a43SJulian Elischer /*************************************************************************
2934cf49a43SJulian Elischer  * Some basic utilities  from the Linux version with author's permission.*
2944cf49a43SJulian Elischer  * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
2954cf49a43SJulian Elischer  ************************************************************************/
2964cf49a43SJulian Elischer 
2974cf49a43SJulian Elischer /*
2984cf49a43SJulian Elischer  * Generate a new session id
2994adb13fdSJulian Elischer  * XXX find out the FreeBSD locking scheme.
3004cf49a43SJulian Elischer  */
3014cf49a43SJulian Elischer static u_int16_t
3024cf49a43SJulian Elischer get_new_sid(node_p node)
3034cf49a43SJulian Elischer {
3044cf49a43SJulian Elischer 	static int pppoe_sid = 10;
3054cf49a43SJulian Elischer 	sessp sp;
3064cf49a43SJulian Elischer 	hook_p	hook;
3074cf49a43SJulian Elischer 	u_int16_t val;
30830400f03SJulian Elischer 	priv_p privp = NG_NODE_PRIVATE(node);
3094cf49a43SJulian Elischer 
310f2b9562cSGleb Smirnoff 	DBG;
3114cf49a43SJulian Elischer restart:
3124cf49a43SJulian Elischer 	val = pppoe_sid++;
3134cf49a43SJulian Elischer 	/*
3144cf49a43SJulian Elischer 	 * Spec says 0xFFFF is reserved.
3154cf49a43SJulian Elischer 	 * Also don't use 0x0000
3164cf49a43SJulian Elischer 	 */
3174cf49a43SJulian Elischer 	if (val == 0xffff) {
3184cf49a43SJulian Elischer 		pppoe_sid = 20;
3194cf49a43SJulian Elischer 		goto restart;
3204cf49a43SJulian Elischer 	}
3214cf49a43SJulian Elischer 
3224cf49a43SJulian Elischer 	/* Check it isn't already in use */
32330400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
3244cf49a43SJulian Elischer 		/* don't check special hooks */
32530400f03SJulian Elischer 		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
32630400f03SJulian Elischer 		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
3274cf49a43SJulian Elischer 			continue;
32830400f03SJulian Elischer 		sp = NG_HOOK_PRIVATE(hook);
3294cf49a43SJulian Elischer 		if (sp->Session_ID == val)
3304cf49a43SJulian Elischer 			goto restart;
3314cf49a43SJulian Elischer 	}
3324cf49a43SJulian Elischer 
3334cf49a43SJulian Elischer 	return val;
3344cf49a43SJulian Elischer }
3354cf49a43SJulian Elischer 
3364cf49a43SJulian Elischer 
3374cf49a43SJulian Elischer /*
3384cf49a43SJulian Elischer  * Return the location where the next tag can be put
3394cf49a43SJulian Elischer  */
340816b834fSArchie Cobbs static __inline const struct pppoe_tag*
341816b834fSArchie Cobbs next_tag(const struct pppoe_hdr* ph)
3424cf49a43SJulian Elischer {
343816b834fSArchie Cobbs 	return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
344816b834fSArchie Cobbs 	    + ntohs(ph->length));
3454cf49a43SJulian Elischer }
3464cf49a43SJulian Elischer 
3474cf49a43SJulian Elischer /*
3484cf49a43SJulian Elischer  * Look for a tag of a specific type
3494cf49a43SJulian Elischer  * Don't trust any length the other end says.
3504cf49a43SJulian Elischer  * but assume we already sanity checked ph->length.
3514cf49a43SJulian Elischer  */
352816b834fSArchie Cobbs static const struct pppoe_tag*
353816b834fSArchie Cobbs get_tag(const struct pppoe_hdr* ph, u_int16_t idx)
3544cf49a43SJulian Elischer {
355816b834fSArchie Cobbs 	const char *const end = (const char *)next_tag(ph);
356816b834fSArchie Cobbs 	const char *ptn;
357816b834fSArchie Cobbs 	const struct pppoe_tag *pt = &ph->tag[0];
3584cf49a43SJulian Elischer 	/*
3594cf49a43SJulian Elischer 	 * Keep processing tags while a tag header will still fit.
3604cf49a43SJulian Elischer 	 */
361f2b9562cSGleb Smirnoff 	DBG;
362816b834fSArchie Cobbs 	while((const char*)(pt + 1) <= end) {
3634cf49a43SJulian Elischer 	    /*
3644cf49a43SJulian Elischer 	     * If the tag data would go past the end of the packet, abort.
3654cf49a43SJulian Elischer 	     */
366816b834fSArchie Cobbs 	    ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
3674cf49a43SJulian Elischer 	    if(ptn > end)
3684cf49a43SJulian Elischer 		return NULL;
3694cf49a43SJulian Elischer 
3704cf49a43SJulian Elischer 	    if(pt->tag_type == idx)
3714cf49a43SJulian Elischer 		return pt;
3724cf49a43SJulian Elischer 
373816b834fSArchie Cobbs 	    pt = (const struct pppoe_tag*)ptn;
3744cf49a43SJulian Elischer 	}
3754cf49a43SJulian Elischer 	return NULL;
3764cf49a43SJulian Elischer }
3774cf49a43SJulian Elischer 
3784cf49a43SJulian Elischer /**************************************************************************
3794cf49a43SJulian Elischer  * inlines to initialise or add tags to a session's tag list,
3804cf49a43SJulian Elischer  **************************************************************************/
3814cf49a43SJulian Elischer /*
3824cf49a43SJulian Elischer  * Initialise the session's tag list
3834cf49a43SJulian Elischer  */
3844cf49a43SJulian Elischer static void
3854cf49a43SJulian Elischer init_tags(sessp sp)
3864cf49a43SJulian Elischer {
387f2b9562cSGleb Smirnoff 	DBG;
3884cf49a43SJulian Elischer 	if(sp->neg == NULL) {
3894cf49a43SJulian Elischer 		printf("pppoe: asked to init NULL neg pointer\n");
3904cf49a43SJulian Elischer 		return;
3914cf49a43SJulian Elischer 	}
3924cf49a43SJulian Elischer 	sp->neg->numtags = 0;
3934cf49a43SJulian Elischer }
3944cf49a43SJulian Elischer 
3954cf49a43SJulian Elischer static void
396816b834fSArchie Cobbs insert_tag(sessp sp, const struct pppoe_tag *tp)
3974cf49a43SJulian Elischer {
3984cf49a43SJulian Elischer 	int	i;
3994cf49a43SJulian Elischer 	negp neg;
4004cf49a43SJulian Elischer 
401f2b9562cSGleb Smirnoff 	DBG;
4024cf49a43SJulian Elischer 	if((neg = sp->neg) == NULL) {
4034cf49a43SJulian Elischer 		printf("pppoe: asked to use NULL neg pointer\n");
4044cf49a43SJulian Elischer 		return;
4054cf49a43SJulian Elischer 	}
4064cf49a43SJulian Elischer 	if ((i = neg->numtags++) < NUMTAGS) {
4074cf49a43SJulian Elischer 		neg->tags[i] = tp;
4084cf49a43SJulian Elischer 	} else {
4094cf49a43SJulian Elischer 		printf("pppoe: asked to add too many tags to packet\n");
41012f035e0SJulian Elischer 		neg->numtags--;
4114cf49a43SJulian Elischer 	}
4124cf49a43SJulian Elischer }
4134cf49a43SJulian Elischer 
4144cf49a43SJulian Elischer /*
4154cf49a43SJulian Elischer  * Make up a packet, using the tags filled out for the session.
4164cf49a43SJulian Elischer  *
4174cf49a43SJulian Elischer  * Assume that the actual pppoe header and ethernet header
4184cf49a43SJulian Elischer  * are filled out externally to this routine.
4194cf49a43SJulian Elischer  * Also assume that neg->wh points to the correct
4204cf49a43SJulian Elischer  * location at the front of the buffer space.
4214cf49a43SJulian Elischer  */
4224cf49a43SJulian Elischer static void
4234cf49a43SJulian Elischer make_packet(sessp sp) {
4244cf49a43SJulian Elischer 	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
425816b834fSArchie Cobbs 	const struct pppoe_tag **tag;
4264cf49a43SJulian Elischer 	char *dp;
4274cf49a43SJulian Elischer 	int count;
4284cf49a43SJulian Elischer 	int tlen;
4294cf49a43SJulian Elischer 	u_int16_t length = 0;
4304cf49a43SJulian Elischer 
431f2b9562cSGleb Smirnoff 	DBG;
4321e2510f8SJulian Elischer 	if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
4334cf49a43SJulian Elischer 		printf("pppoe: make_packet called from wrong state\n");
4344cf49a43SJulian Elischer 	}
4354cf49a43SJulian Elischer 	dp = (char *)wh->ph.tag;
4364cf49a43SJulian Elischer 	for (count = 0, tag = sp->neg->tags;
4374cf49a43SJulian Elischer 	    ((count < sp->neg->numtags) && (count < NUMTAGS));
4384cf49a43SJulian Elischer 	    tag++, count++) {
4394cf49a43SJulian Elischer 		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
4404cf49a43SJulian Elischer 		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
4414cf49a43SJulian Elischer 			printf("pppoe: tags too long\n");
4424cf49a43SJulian Elischer 			sp->neg->numtags = count;
4434cf49a43SJulian Elischer 			break;	/* XXX chop off what's too long */
4444cf49a43SJulian Elischer 		}
445816b834fSArchie Cobbs 		bcopy(*tag, (char *)dp, tlen);
4464cf49a43SJulian Elischer 		length += tlen;
4474cf49a43SJulian Elischer 		dp += tlen;
4484cf49a43SJulian Elischer 	}
4494cf49a43SJulian Elischer  	wh->ph.length = htons(length);
4504cf49a43SJulian Elischer 	sp->neg->m->m_len = length + sizeof(*wh);
4514cf49a43SJulian Elischer 	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
4524cf49a43SJulian Elischer }
4534cf49a43SJulian Elischer 
4544cf49a43SJulian Elischer /**************************************************************************
4554cf49a43SJulian Elischer  * Routine to match a service offered					  *
4564cf49a43SJulian Elischer  **************************************************************************/
4574cf49a43SJulian Elischer /*
4584cf49a43SJulian Elischer  * Find a hook that has a service string that matches that
4594cf49a43SJulian Elischer  * we are seeking. for now use a simple string.
4604cf49a43SJulian Elischer  * In the future we may need something like regexp().
4614cf49a43SJulian Elischer  * for testing allow a null string to match 1st found and a null service
4624cf49a43SJulian Elischer  * to match all requests. Also make '*' do the same.
4634cf49a43SJulian Elischer  */
4649088fa05SBrian Somers 
4659088fa05SBrian Somers #define NG_MATCH_EXACT	1
4669088fa05SBrian Somers #define NG_MATCH_ANY	2
4679088fa05SBrian Somers 
4684cf49a43SJulian Elischer static hook_p
469816b834fSArchie Cobbs pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match)
4704cf49a43SJulian Elischer {
4714cf49a43SJulian Elischer 	sessp	sp	= NULL;
4724cf49a43SJulian Elischer 	negp	neg	= NULL;
47330400f03SJulian Elischer 	priv_p	privp	= NG_NODE_PRIVATE(node);
4749088fa05SBrian Somers 	hook_p	allhook	= NULL;
4754cf49a43SJulian Elischer 	hook_p	hook;
4764cf49a43SJulian Elischer 
477f2b9562cSGleb Smirnoff 	DBG;
47830400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
4794cf49a43SJulian Elischer 
4804cf49a43SJulian Elischer 		/* skip any hook that is debug or ethernet */
48130400f03SJulian Elischer 		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
48230400f03SJulian Elischer 		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
4834cf49a43SJulian Elischer 			continue;
48430400f03SJulian Elischer 		sp = NG_HOOK_PRIVATE(hook);
4854cf49a43SJulian Elischer 
4864cf49a43SJulian Elischer 		/* Skip any sessions which are not in LISTEN mode. */
4874cf49a43SJulian Elischer 		if ( sp->state != PPPOE_LISTENING)
4884cf49a43SJulian Elischer 			continue;
4894cf49a43SJulian Elischer 
4904cf49a43SJulian Elischer 		neg = sp->neg;
4914cf49a43SJulian Elischer 
4924cf49a43SJulian Elischer 		/* Special case for a blank or "*" service name (wildcard) */
4939088fa05SBrian Somers 		if (match == NG_MATCH_ANY && neg->service_len == 1 &&
4949088fa05SBrian Somers 		    neg->service.data[0] == '*') {
4959088fa05SBrian Somers 			allhook = hook;
4969088fa05SBrian Somers 			continue;
4974cf49a43SJulian Elischer 		}
4984cf49a43SJulian Elischer 
4994cf49a43SJulian Elischer 		/* If the lengths don't match, that aint it. */
5004cf49a43SJulian Elischer 		if (neg->service_len != svc_len)
5014cf49a43SJulian Elischer 			continue;
5024cf49a43SJulian Elischer 
5034cf49a43SJulian Elischer 		/* An exact match? */
5049088fa05SBrian Somers 		if (svc_len == 0)
5059088fa05SBrian Somers 			break;
5069088fa05SBrian Somers 
5074cf49a43SJulian Elischer 		if (strncmp(svc_name, neg->service.data, svc_len) == 0)
5084cf49a43SJulian Elischer 			break;
5094cf49a43SJulian Elischer 	}
5109088fa05SBrian Somers 	return (hook ? hook : allhook);
5114cf49a43SJulian Elischer }
5124cf49a43SJulian Elischer /**************************************************************************
5134cf49a43SJulian Elischer  * Routine to find a particular session that matches an incoming packet	  *
5144cf49a43SJulian Elischer  **************************************************************************/
5154cf49a43SJulian Elischer static hook_p
516816b834fSArchie Cobbs pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
5174cf49a43SJulian Elischer {
5184cf49a43SJulian Elischer 	sessp	sp = NULL;
5194cf49a43SJulian Elischer 	hook_p hook = NULL;
52030400f03SJulian Elischer 	priv_p	privp = NG_NODE_PRIVATE(node);
521b86d0a9eSJulian Elischer 	u_int16_t	session = ntohs(wh->ph.sid);
5224cf49a43SJulian Elischer 
5234cf49a43SJulian Elischer 	/*
5244cf49a43SJulian Elischer 	 * find matching peer/session combination.
5254cf49a43SJulian Elischer 	 */
526f2b9562cSGleb Smirnoff 	DBG;
52730400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
5284cf49a43SJulian Elischer 		/* don't check special hooks */
52930400f03SJulian Elischer 		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
53030400f03SJulian Elischer 		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
5314cf49a43SJulian Elischer 			continue;
5324cf49a43SJulian Elischer 		}
53330400f03SJulian Elischer 		sp = NG_HOOK_PRIVATE(hook);
5344cf49a43SJulian Elischer 		if ( ( (sp->state == PPPOE_CONNECTED)
5354cf49a43SJulian Elischer 		    || (sp->state == PPPOE_NEWCONNECTED) )
5364cf49a43SJulian Elischer 		&& (sp->Session_ID == session)
5374cf49a43SJulian Elischer 		&& (bcmp(sp->pkt_hdr.eh.ether_dhost,
5384cf49a43SJulian Elischer 		    wh->eh.ether_shost,
5394cf49a43SJulian Elischer 		    ETHER_ADDR_LEN)) == 0) {
5404cf49a43SJulian Elischer 			break;
5414cf49a43SJulian Elischer 		}
5424cf49a43SJulian Elischer 	}
5434cf49a43SJulian Elischer 	return (hook);
5444cf49a43SJulian Elischer }
5454cf49a43SJulian Elischer 
5464cf49a43SJulian Elischer static hook_p
547816b834fSArchie Cobbs pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
5484cf49a43SJulian Elischer {
5494cf49a43SJulian Elischer 	hook_p hook = NULL;
55030400f03SJulian Elischer 	priv_p	privp = NG_NODE_PRIVATE(node);
5514cf49a43SJulian Elischer 	union uniq		uniq;
5524cf49a43SJulian Elischer 
553f2b9562cSGleb Smirnoff 	DBG;
5544cf49a43SJulian Elischer 	bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
5554cf49a43SJulian Elischer 	/* cycle through all known hooks */
55630400f03SJulian Elischer 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
5574cf49a43SJulian Elischer 		/* don't check special hooks */
55830400f03SJulian Elischer 		if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
55930400f03SJulian Elischer 		||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook))
5604cf49a43SJulian Elischer 			continue;
56130400f03SJulian Elischer 		if (uniq.pointer == NG_HOOK_PRIVATE(hook))
5624cf49a43SJulian Elischer 			break;
5634cf49a43SJulian Elischer 	}
5644cf49a43SJulian Elischer 	return (hook);
5654cf49a43SJulian Elischer }
5664cf49a43SJulian Elischer 
5674cf49a43SJulian Elischer /**************************************************************************
5684cf49a43SJulian Elischer  * start of Netgraph entrypoints					  *
5694cf49a43SJulian Elischer  **************************************************************************/
5704cf49a43SJulian Elischer 
5714cf49a43SJulian Elischer /*
5724cf49a43SJulian Elischer  * Allocate the private data structure and the generic node
5734cf49a43SJulian Elischer  * and link them together.
5744cf49a43SJulian Elischer  *
5754cf49a43SJulian Elischer  * ng_make_node_common() returns with a generic node struct
5764cf49a43SJulian Elischer  * with a single reference for us.. we transfer it to the
5774cf49a43SJulian Elischer  * private structure.. when we free the private struct we must
5784cf49a43SJulian Elischer  * unref the node so it gets freed too.
5794cf49a43SJulian Elischer  */
5804cf49a43SJulian Elischer static int
581069154d5SJulian Elischer ng_pppoe_constructor(node_p node)
5824cf49a43SJulian Elischer {
5834cf49a43SJulian Elischer 	priv_p privdata;
5844cf49a43SJulian Elischer 
585f2b9562cSGleb Smirnoff 	DBG;
5864cf49a43SJulian Elischer 	/* Initialize private descriptor */
5879c8c302fSJulian Elischer 	MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH_PPPOE,
58899cdf4ccSDavid Malone 	    M_NOWAIT | M_ZERO);
5894cf49a43SJulian Elischer 	if (privdata == NULL)
5904cf49a43SJulian Elischer 		return (ENOMEM);
5914cf49a43SJulian Elischer 
5924cf49a43SJulian Elischer 	/* Link structs together; this counts as our one reference to *nodep */
59330400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, privdata);
594069154d5SJulian Elischer 	privdata->node = node;
595fdc755d1SGleb Smirnoff 
596fdc755d1SGleb Smirnoff 	/* Initialize to standard mode (the first one in ng_pppoe_modes[]). */
597ae1ff8e3SGleb Smirnoff 	privdata->mode = &ng_pppoe_modes[0];
598fdc755d1SGleb Smirnoff 
5994cf49a43SJulian Elischer 	return (0);
6004cf49a43SJulian Elischer }
6014cf49a43SJulian Elischer 
6024cf49a43SJulian Elischer /*
6034cf49a43SJulian Elischer  * Give our ok for a hook to be added...
6044cf49a43SJulian Elischer  * point the hook's private info to the hook structure.
6054cf49a43SJulian Elischer  *
6064cf49a43SJulian Elischer  * The following hook names are special:
6074cf49a43SJulian Elischer  *  Ethernet:  the hook that should be connected to a NIC.
6084cf49a43SJulian Elischer  *  debug:	copies of data sent out here  (when I write the code).
609859a4d16SJulian Elischer  * All other hook names need only be unique. (the framework checks this).
6104cf49a43SJulian Elischer  */
6114cf49a43SJulian Elischer static int
6128876b55dSJulian Elischer ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
6134cf49a43SJulian Elischer {
61430400f03SJulian Elischer 	const priv_p privp = NG_NODE_PRIVATE(node);
6154cf49a43SJulian Elischer 	sessp sp;
6164cf49a43SJulian Elischer 
617f2b9562cSGleb Smirnoff 	DBG;
6184cf49a43SJulian Elischer 	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
6194cf49a43SJulian Elischer 		privp->ethernet_hook = hook;
62030400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, &privp->ethernet_hook);
6214cf49a43SJulian Elischer 	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
6224cf49a43SJulian Elischer 		privp->debug_hook = hook;
62330400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, &privp->debug_hook);
6244cf49a43SJulian Elischer 	} else {
6254cf49a43SJulian Elischer 		/*
6264cf49a43SJulian Elischer 		 * Any other unique name is OK.
6274cf49a43SJulian Elischer 		 * The infrastructure has already checked that it's unique,
6284cf49a43SJulian Elischer 		 * so just allocate it and hook it in.
6294cf49a43SJulian Elischer 		 */
6309c8c302fSJulian Elischer 		MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
6314cf49a43SJulian Elischer 		if (sp == NULL) {
6324cf49a43SJulian Elischer 				return (ENOMEM);
6334cf49a43SJulian Elischer 		}
6344cf49a43SJulian Elischer 
63530400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, sp);
6364cf49a43SJulian Elischer 		sp->hook = hook;
6374cf49a43SJulian Elischer 	}
6384cf49a43SJulian Elischer 	return(0);
6394cf49a43SJulian Elischer }
6404cf49a43SJulian Elischer 
6414cf49a43SJulian Elischer /*
6424cf49a43SJulian Elischer  * Get a netgraph control message.
6434cf49a43SJulian Elischer  * Check it is one we understand. If needed, send a response.
6444cf49a43SJulian Elischer  * We sometimes save the address for an async action later.
6454cf49a43SJulian Elischer  * Always free the message.
6464cf49a43SJulian Elischer  */
6474cf49a43SJulian Elischer static int
648069154d5SJulian Elischer ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
6494cf49a43SJulian Elischer {
65030400f03SJulian Elischer 	priv_p privp = NG_NODE_PRIVATE(node);
6518876b55dSJulian Elischer 	struct ngpppoe_init_data *ourmsg = NULL;
6524cf49a43SJulian Elischer 	struct ng_mesg *resp = NULL;
6534cf49a43SJulian Elischer 	int error = 0;
6544cf49a43SJulian Elischer 	hook_p hook = NULL;
6554cf49a43SJulian Elischer 	sessp sp = NULL;
6564cf49a43SJulian Elischer 	negp neg = NULL;
657069154d5SJulian Elischer 	struct ng_mesg *msg;
6584cf49a43SJulian Elischer 
659f2b9562cSGleb Smirnoff 	DBG;
660069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
6614cf49a43SJulian Elischer 	/* Deal with message according to cookie and command */
6624cf49a43SJulian Elischer 	switch (msg->header.typecookie) {
6634cf49a43SJulian Elischer 	case NGM_PPPOE_COOKIE:
6644cf49a43SJulian Elischer 		switch (msg->header.cmd) {
6654cf49a43SJulian Elischer 		case NGM_PPPOE_CONNECT:
6664cf49a43SJulian Elischer 		case NGM_PPPOE_LISTEN:
6674cf49a43SJulian Elischer 		case NGM_PPPOE_OFFER:
668859a4d16SJulian Elischer 		case NGM_PPPOE_SERVICE:
66927121ab1SBrian Somers 			ourmsg = (struct ngpppoe_init_data *)msg->data;
67027121ab1SBrian Somers 			if (msg->header.arglen < sizeof(*ourmsg)) {
67127121ab1SBrian Somers 				printf("pppoe: init data too small\n");
6724cf49a43SJulian Elischer 				LEAVE(EMSGSIZE);
6734cf49a43SJulian Elischer 			}
67476a70671SBrian Somers 			if (msg->header.arglen - sizeof(*ourmsg) >
67576a70671SBrian Somers 			    PPPOE_SERVICE_NAME_SIZE) {
67676a70671SBrian Somers 				printf("pppoe_rcvmsg: service name too big");
6774cf49a43SJulian Elischer 				LEAVE(EMSGSIZE);
6784cf49a43SJulian Elischer 			}
67927121ab1SBrian Somers 			if (msg->header.arglen - sizeof(*ourmsg) <
68027121ab1SBrian Somers 			    ourmsg->data_len) {
68127121ab1SBrian Somers 				printf("pppoe: init data has bad length,"
682a2a6abd5SJohn Baldwin 				    " %d should be %zd\n", ourmsg->data_len,
68327121ab1SBrian Somers 				    msg->header.arglen - sizeof (*ourmsg));
68476a70671SBrian Somers 				LEAVE(EMSGSIZE);
68576a70671SBrian Somers 			}
68676a70671SBrian Somers 
6874cf49a43SJulian Elischer 			/* make sure strcmp will terminate safely */
6884cf49a43SJulian Elischer 			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
6894cf49a43SJulian Elischer 
6904cf49a43SJulian Elischer 			/* cycle through all known hooks */
69130400f03SJulian Elischer 			LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
69230400f03SJulian Elischer 				if (NG_HOOK_NAME(hook)
69330400f03SJulian Elischer 				&& strcmp(NG_HOOK_NAME(hook), ourmsg->hook) == 0)
6944cf49a43SJulian Elischer 					break;
6954cf49a43SJulian Elischer 			}
6964cf49a43SJulian Elischer 			if (hook == NULL) {
6974cf49a43SJulian Elischer 				LEAVE(ENOENT);
6984cf49a43SJulian Elischer 			}
69930400f03SJulian Elischer 			if ((NG_HOOK_PRIVATE(hook) == &privp->debug_hook)
70030400f03SJulian Elischer 			||  (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook)) {
7014cf49a43SJulian Elischer 				LEAVE(EINVAL);
7024cf49a43SJulian Elischer 			}
70330400f03SJulian Elischer 			sp = NG_HOOK_PRIVATE(hook);
704859a4d16SJulian Elischer 
7059088fa05SBrian Somers 			if (msg->header.cmd == NGM_PPPOE_LISTEN) {
7069088fa05SBrian Somers 				/*
7079088fa05SBrian Somers 				 * Ensure we aren't already listening for this
7089088fa05SBrian Somers 				 * service.
7099088fa05SBrian Somers 				 */
7109088fa05SBrian Somers 				if (pppoe_match_svc(node, ourmsg->data,
7119088fa05SBrian Somers 				    ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
7129088fa05SBrian Somers 					LEAVE(EEXIST);
7139088fa05SBrian Somers 				}
7149088fa05SBrian Somers 			}
7159088fa05SBrian Somers 
716859a4d16SJulian Elischer 			/*
717859a4d16SJulian Elischer 			 * PPPOE_SERVICE advertisments are set up
718859a4d16SJulian Elischer 			 * on sessions that are in PRIMED state.
719859a4d16SJulian Elischer 			 */
720859a4d16SJulian Elischer 			if (msg->header.cmd == NGM_PPPOE_SERVICE) {
721859a4d16SJulian Elischer 				break;
722859a4d16SJulian Elischer 			}
723f795fd00SGleb Smirnoff 			if (sp->state != PPPOE_SNONE) {
7244cf49a43SJulian Elischer 				printf("pppoe: Session already active\n");
7254cf49a43SJulian Elischer 				LEAVE(EISCONN);
7264cf49a43SJulian Elischer 			}
7271e2510f8SJulian Elischer 
7284cf49a43SJulian Elischer 			/*
7294cf49a43SJulian Elischer 			 * set up prototype header
7304cf49a43SJulian Elischer 			 */
7319c8c302fSJulian Elischer 			MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH_PPPOE,
73299cdf4ccSDavid Malone 			    M_NOWAIT | M_ZERO);
7334cf49a43SJulian Elischer 
7344cf49a43SJulian Elischer 			if (neg == NULL) {
7354cf49a43SJulian Elischer 				printf("pppoe: Session out of memory\n");
7364cf49a43SJulian Elischer 				LEAVE(ENOMEM);
7374cf49a43SJulian Elischer 			}
738a163d034SWarner Losh 			MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
7394cf49a43SJulian Elischer 			if(neg->m == NULL) {
7401e2510f8SJulian Elischer 				printf("pppoe: Session out of mbufs\n");
7419c8c302fSJulian Elischer 				FREE(neg, M_NETGRAPH_PPPOE);
7424cf49a43SJulian Elischer 				LEAVE(ENOBUFS);
7434cf49a43SJulian Elischer 			}
7444cf49a43SJulian Elischer 			neg->m->m_pkthdr.rcvif = NULL;
745a163d034SWarner Losh 			MCLGET(neg->m, M_DONTWAIT);
7464cf49a43SJulian Elischer 			if ((neg->m->m_flags & M_EXT) == 0) {
7471e2510f8SJulian Elischer 				printf("pppoe: Session out of mcls\n");
7484cf49a43SJulian Elischer 				m_freem(neg->m);
7499c8c302fSJulian Elischer 				FREE(neg, M_NETGRAPH_PPPOE);
7504cf49a43SJulian Elischer 				LEAVE(ENOBUFS);
7514cf49a43SJulian Elischer 			}
7524cf49a43SJulian Elischer 			sp->neg = neg;
753ef237c7fSGleb Smirnoff 			ng_callout_init(&neg->handle);
7544cf49a43SJulian Elischer 			neg->m->m_len = sizeof(struct pppoe_full_hdr);
7554cf49a43SJulian Elischer 			neg->pkt = mtod(neg->m, union packet*);
756fdc755d1SGleb Smirnoff 			memcpy((void *)&neg->pkt->pkt_header.eh,
757fdc755d1SGleb Smirnoff 			    (const void *)privp->mode->eh_prototype,
758fdc755d1SGleb Smirnoff 			    sizeof(struct ether_header));
7594cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.ver = 0x1;
7604cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.type = 0x1;
7614cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.sid = 0x0000;
7624cf49a43SJulian Elischer 			neg->timeout = 0;
7634cf49a43SJulian Elischer 
764069154d5SJulian Elischer 			sp->creator = NGI_RETADDR(item);
7654cf49a43SJulian Elischer 		}
7664cf49a43SJulian Elischer 		switch (msg->header.cmd) {
7674cf49a43SJulian Elischer 		case NGM_PPPOE_GET_STATUS:
7684cf49a43SJulian Elischer 		    {
7698876b55dSJulian Elischer 			struct ngpppoestat *stats;
7704cf49a43SJulian Elischer 
7714cf49a43SJulian Elischer 			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
7724cf49a43SJulian Elischer 			if (!resp) {
7734cf49a43SJulian Elischer 				LEAVE(ENOMEM);
7744cf49a43SJulian Elischer 			}
7758876b55dSJulian Elischer 			stats = (struct ngpppoestat *) resp->data;
7764cf49a43SJulian Elischer 			stats->packets_in = privp->packets_in;
7774cf49a43SJulian Elischer 			stats->packets_out = privp->packets_out;
7784cf49a43SJulian Elischer 			break;
7794cf49a43SJulian Elischer 		    }
7804cf49a43SJulian Elischer 		case NGM_PPPOE_CONNECT:
7814cf49a43SJulian Elischer 			/*
7824cf49a43SJulian Elischer 			 * Check the hook exists and is Uninitialised.
7834cf49a43SJulian Elischer 			 * Send a PADI request, and start the timeout logic.
7844cf49a43SJulian Elischer 			 * Store the originator of this message so we can send
7854cf49a43SJulian Elischer 			 * a success of fail message to them later.
7864cf49a43SJulian Elischer 			 * Move the session to SINIT
7874cf49a43SJulian Elischer 			 * Set up the session to the correct state and
7884cf49a43SJulian Elischer 			 * start it.
7894cf49a43SJulian Elischer 			 */
7904cf49a43SJulian Elischer 			neg->service.hdr.tag_type = PTT_SRV_NAME;
79127121ab1SBrian Somers 			neg->service.hdr.tag_len =
79227121ab1SBrian Somers 			    htons((u_int16_t)ourmsg->data_len);
79327121ab1SBrian Somers 			if (ourmsg->data_len)
79427121ab1SBrian Somers 				bcopy(ourmsg->data, neg->service.data,
79527121ab1SBrian Somers 				    ourmsg->data_len);
79627121ab1SBrian Somers 			neg->service_len = ourmsg->data_len;
7974cf49a43SJulian Elischer 			pppoe_start(sp);
7984cf49a43SJulian Elischer 			break;
7994cf49a43SJulian Elischer 		case NGM_PPPOE_LISTEN:
8004cf49a43SJulian Elischer 			/*
8014cf49a43SJulian Elischer 			 * Check the hook exists and is Uninitialised.
8024cf49a43SJulian Elischer 			 * Install the service matching string.
8034cf49a43SJulian Elischer 			 * Store the originator of this message so we can send
8044cf49a43SJulian Elischer 			 * a success of fail message to them later.
8054cf49a43SJulian Elischer 			 * Move the hook to 'LISTENING'
8064cf49a43SJulian Elischer 			 */
8074cf49a43SJulian Elischer 			neg->service.hdr.tag_type = PTT_SRV_NAME;
80827121ab1SBrian Somers 			neg->service.hdr.tag_len =
80927121ab1SBrian Somers 			    htons((u_int16_t)ourmsg->data_len);
8101e2510f8SJulian Elischer 
81127121ab1SBrian Somers 			if (ourmsg->data_len)
81227121ab1SBrian Somers 				bcopy(ourmsg->data, neg->service.data,
81327121ab1SBrian Somers 				    ourmsg->data_len);
81427121ab1SBrian Somers 			neg->service_len = ourmsg->data_len;
8154cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.code = PADT_CODE;
8164cf49a43SJulian Elischer 			/*
8174cf49a43SJulian Elischer 			 * wait for PADI packet coming from ethernet
8184cf49a43SJulian Elischer 			 */
8194cf49a43SJulian Elischer 			sp->state = PPPOE_LISTENING;
8204cf49a43SJulian Elischer 			break;
8214cf49a43SJulian Elischer 		case NGM_PPPOE_OFFER:
8224cf49a43SJulian Elischer 			/*
8234cf49a43SJulian Elischer 			 * Check the hook exists and is Uninitialised.
8244cf49a43SJulian Elischer 			 * Store the originator of this message so we can send
8254cf49a43SJulian Elischer 			 * a success of fail message to them later.
8264cf49a43SJulian Elischer 			 * Store the AC-Name given and go to PRIMED.
8274cf49a43SJulian Elischer 			 */
8284cf49a43SJulian Elischer 			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
82927121ab1SBrian Somers 			neg->ac_name.hdr.tag_len =
83027121ab1SBrian Somers 			    htons((u_int16_t)ourmsg->data_len);
83127121ab1SBrian Somers 			if (ourmsg->data_len)
83227121ab1SBrian Somers 				bcopy(ourmsg->data, neg->ac_name.data,
83327121ab1SBrian Somers 				    ourmsg->data_len);
83427121ab1SBrian Somers 			neg->ac_name_len = ourmsg->data_len;
8354cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.code = PADO_CODE;
8364cf49a43SJulian Elischer 			/*
8374cf49a43SJulian Elischer 			 * Wait for PADI packet coming from hook
8384cf49a43SJulian Elischer 			 */
8394cf49a43SJulian Elischer 			sp->state = PPPOE_PRIMED;
8404cf49a43SJulian Elischer 			break;
841859a4d16SJulian Elischer 		case NGM_PPPOE_SERVICE:
842859a4d16SJulian Elischer 			/*
843859a4d16SJulian Elischer 			 * Check the session is primed.
844859a4d16SJulian Elischer 			 * for now just allow ONE service to be advertised.
845859a4d16SJulian Elischer 			 * If you do it twice you just overwrite.
846859a4d16SJulian Elischer 			 */
8475078fb0bSJulian Elischer 			if (sp->state != PPPOE_PRIMED) {
848859a4d16SJulian Elischer 				printf("pppoe: Session not primed\n");
849859a4d16SJulian Elischer 				LEAVE(EISCONN);
850859a4d16SJulian Elischer 			}
8510069b9cbSJulian Elischer 			neg = sp->neg;
852859a4d16SJulian Elischer 			neg->service.hdr.tag_type = PTT_SRV_NAME;
853859a4d16SJulian Elischer 			neg->service.hdr.tag_len =
854859a4d16SJulian Elischer 			    htons((u_int16_t)ourmsg->data_len);
855859a4d16SJulian Elischer 
856859a4d16SJulian Elischer 			if (ourmsg->data_len)
857859a4d16SJulian Elischer 				bcopy(ourmsg->data, neg->service.data,
858859a4d16SJulian Elischer 				    ourmsg->data_len);
859859a4d16SJulian Elischer 			neg->service_len = ourmsg->data_len;
860859a4d16SJulian Elischer 			break;
861fdc755d1SGleb Smirnoff 		case NGM_PPPOE_SETMODE:
862fdc755d1SGleb Smirnoff 		    {
863fdc755d1SGleb Smirnoff 			const struct ng_pppoe_mode_t *mode;
864fdc755d1SGleb Smirnoff 			char *s;
865fdc755d1SGleb Smirnoff 			size_t len;
866fdc755d1SGleb Smirnoff 
867fdc755d1SGleb Smirnoff 			if (msg->header.arglen == 0)
868fdc755d1SGleb Smirnoff 				LEAVE(EINVAL);
869fdc755d1SGleb Smirnoff 
870fdc755d1SGleb Smirnoff 			s = (char *)msg->data;
871fdc755d1SGleb Smirnoff 			len = msg->header.arglen - 1;
872fdc755d1SGleb Smirnoff 
873fdc755d1SGleb Smirnoff 			/* Search for matching mode string */
874fdc755d1SGleb Smirnoff 			for (mode = ng_pppoe_modes; mode->id != 0; mode++ )
875fdc755d1SGleb Smirnoff 				if ((strlen(mode->name) == len) &&
876fdc755d1SGleb Smirnoff 				    !strncmp(mode->name, s, len))
877fdc755d1SGleb Smirnoff 					break;	/* found */
878fdc755d1SGleb Smirnoff 
879fdc755d1SGleb Smirnoff 			if (mode->id != 0)
880fdc755d1SGleb Smirnoff 				privp->mode = mode;
881fdc755d1SGleb Smirnoff 			else
882fdc755d1SGleb Smirnoff 				LEAVE(EINVAL);
883fdc755d1SGleb Smirnoff 			break;
884fdc755d1SGleb Smirnoff 		    }
885fdc755d1SGleb Smirnoff 		case NGM_PPPOE_GETMODE:
886fdc755d1SGleb Smirnoff 			NG_MKRESPONSE(resp, msg, strlen(privp->mode->name) + 1,
887fdc755d1SGleb Smirnoff 			    M_NOWAIT);
888fdc755d1SGleb Smirnoff 			if (resp == NULL)
889fdc755d1SGleb Smirnoff 				LEAVE(ENOMEM);
890fdc755d1SGleb Smirnoff 			strlcpy((char *)resp->data, privp->mode->name,
891fdc755d1SGleb Smirnoff 			    strlen(privp->mode->name) + 1);
892fdc755d1SGleb Smirnoff 			break;
8934cf49a43SJulian Elischer 		default:
8944cf49a43SJulian Elischer 			LEAVE(EINVAL);
8954cf49a43SJulian Elischer 		}
8964cf49a43SJulian Elischer 		break;
8974cf49a43SJulian Elischer 	default:
8984cf49a43SJulian Elischer 		LEAVE(EINVAL);
8994cf49a43SJulian Elischer 	}
9004cf49a43SJulian Elischer 
9014cf49a43SJulian Elischer 	/* Take care of synchronous response, if any */
9024cf49a43SJulian Elischer quit:
903069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
904069154d5SJulian Elischer 	/* Free the message and return */
905069154d5SJulian Elischer 	NG_FREE_MSG(msg);
9064cf49a43SJulian Elischer 	return(error);
9074cf49a43SJulian Elischer }
9084cf49a43SJulian Elischer 
9091e2510f8SJulian Elischer /*
9101e2510f8SJulian Elischer  * Start a client into the first state. A separate function because
9111e2510f8SJulian Elischer  * it can be needed if the negotiation times out.
9121e2510f8SJulian Elischer  */
9134cf49a43SJulian Elischer static void
9144cf49a43SJulian Elischer pppoe_start(sessp sp)
9154cf49a43SJulian Elischer {
916fdc755d1SGleb Smirnoff 	priv_p	privp = NG_NODE_PRIVATE(NG_PPPOE_SESSION_NODE(sp));
9174cf49a43SJulian Elischer 	struct {
9184cf49a43SJulian Elischer 		struct pppoe_tag hdr;
9194cf49a43SJulian Elischer 		union	uniq	data;
9204f492bfaSAlfred Perlstein 	} __packed uniqtag;
9214cf49a43SJulian Elischer 
9224cf49a43SJulian Elischer 	/*
9234cf49a43SJulian Elischer 	 * kick the state machine into starting up
9244cf49a43SJulian Elischer 	 */
925f2b9562cSGleb Smirnoff 	DBG;
9264cf49a43SJulian Elischer 	sp->state = PPPOE_SINIT;
927fdc755d1SGleb Smirnoff 	/* Reset the packet header to broadcast. Since we are in a client
928fdc755d1SGleb Smirnoff 	 * mode use configured ethertype. */
929fdc755d1SGleb Smirnoff 	memcpy((void *)&sp->neg->pkt->pkt_header.eh,
930fdc755d1SGleb Smirnoff 	    (const void *)privp->mode->eh_prototype,
931fdc755d1SGleb Smirnoff 	    sizeof(struct ether_header));
9321e2510f8SJulian Elischer 	sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
9334cf49a43SJulian Elischer 	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
9344cf49a43SJulian Elischer 	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
9354cf49a43SJulian Elischer 	uniqtag.data.pointer = sp;
9364cf49a43SJulian Elischer 	init_tags(sp);
9371f89d938SJulian Elischer 	insert_tag(sp, &uniqtag.hdr);
9387ccbb17bSJulian Elischer 	insert_tag(sp, &sp->neg->service.hdr);
9394cf49a43SJulian Elischer 	make_packet(sp);
9404cf49a43SJulian Elischer 	sendpacket(sp);
9414cf49a43SJulian Elischer }
9424cf49a43SJulian Elischer 
943c48a0b5fSBrian Somers static int
944816b834fSArchie Cobbs send_acname(sessp sp, const struct pppoe_tag *tag)
945c48a0b5fSBrian Somers {
9469e6798e7SBrian Somers 	int error, tlen;
947c48a0b5fSBrian Somers 	struct ng_mesg *msg;
948c48a0b5fSBrian Somers 	struct ngpppoe_sts *sts;
949c48a0b5fSBrian Somers 
950c48a0b5fSBrian Somers 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
951c48a0b5fSBrian Somers 	    sizeof(struct ngpppoe_sts), M_NOWAIT);
952c48a0b5fSBrian Somers 	if (msg == NULL)
953c48a0b5fSBrian Somers 		return (ENOMEM);
954c48a0b5fSBrian Somers 
955c48a0b5fSBrian Somers 	sts = (struct ngpppoe_sts *)msg->data;
95687e2c66aSHartmut Brandt 	tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
9579e6798e7SBrian Somers 	strncpy(sts->hook, tag->tag_data, tlen);
9589e6798e7SBrian Somers 	sts->hook[tlen] = '\0';
959facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
960c48a0b5fSBrian Somers 
961c48a0b5fSBrian Somers 	return (error);
962c48a0b5fSBrian Somers }
963c48a0b5fSBrian Somers 
96487c4cce0SBrian Somers static int
96587c4cce0SBrian Somers send_sessionid(sessp sp)
96687c4cce0SBrian Somers {
96787c4cce0SBrian Somers 	int error;
96887c4cce0SBrian Somers 	struct ng_mesg *msg;
96987c4cce0SBrian Somers 
97087c4cce0SBrian Somers 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
97187c4cce0SBrian Somers 	    sizeof(u_int16_t), M_NOWAIT);
97287c4cce0SBrian Somers 	if (msg == NULL)
97387c4cce0SBrian Somers 		return (ENOMEM);
97487c4cce0SBrian Somers 
97587c4cce0SBrian Somers 	*(u_int16_t *)msg->data = sp->Session_ID;
976facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
97787c4cce0SBrian Somers 
97887c4cce0SBrian Somers 	return (error);
97987c4cce0SBrian Somers }
98087c4cce0SBrian Somers 
9814cf49a43SJulian Elischer /*
9824cf49a43SJulian Elischer  * Receive data, and do something with it.
9833ca24c28SJulian Elischer  * The caller will never free m, so if we use up this data
9843ca24c28SJulian Elischer  * or abort we must free it.
9854cf49a43SJulian Elischer  */
9864cf49a43SJulian Elischer static int
987069154d5SJulian Elischer ng_pppoe_rcvdata(hook_p hook, item_p item)
9884cf49a43SJulian Elischer {
98930400f03SJulian Elischer 	node_p			node = NG_HOOK_NODE(hook);
99030400f03SJulian Elischer 	const priv_p		privp = NG_NODE_PRIVATE(node);
99130400f03SJulian Elischer 	sessp			sp = NG_HOOK_PRIVATE(hook);
992816b834fSArchie Cobbs 	const struct pppoe_full_hdr *wh;
993816b834fSArchie Cobbs 	const struct pppoe_hdr	*ph;
9944cf49a43SJulian Elischer 	int			error = 0;
9954cf49a43SJulian Elischer 	u_int16_t		session;
9964cf49a43SJulian Elischer 	u_int16_t		length;
9974cf49a43SJulian Elischer 	u_int8_t		code;
998816b834fSArchie Cobbs 	const struct pppoe_tag	*utag = NULL, *tag = NULL;
9994cf49a43SJulian Elischer 	hook_p 			sendhook;
10004cf49a43SJulian Elischer 	struct {
10014cf49a43SJulian Elischer 		struct pppoe_tag hdr;
10024cf49a43SJulian Elischer 		union	uniq	data;
10034f492bfaSAlfred Perlstein 	} __packed uniqtag;
10044cf49a43SJulian Elischer 	negp			neg = NULL;
1005069154d5SJulian Elischer 	struct mbuf		*m;
10064cf49a43SJulian Elischer 
1007f2b9562cSGleb Smirnoff 	DBG;
1008069154d5SJulian Elischer 	NGI_GET_M(item, m);
100930400f03SJulian Elischer 	if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
10104cf49a43SJulian Elischer 		/*
10114cf49a43SJulian Elischer 		 * Data from the debug hook gets sent without modification
10124cf49a43SJulian Elischer 		 * straight to the ethernet.
10134cf49a43SJulian Elischer 		 */
101430400f03SJulian Elischer 		NG_FWD_ITEM_HOOK( error, item, privp->ethernet_hook);
10154cf49a43SJulian Elischer 	 	privp->packets_out++;
101630400f03SJulian Elischer 	} else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
10174cf49a43SJulian Elischer 		/*
10184cf49a43SJulian Elischer 		 * Incoming data.
10194cf49a43SJulian Elischer 		 * Dig out various fields from the packet.
10204cf49a43SJulian Elischer 		 * use them to decide where to send it.
10214cf49a43SJulian Elischer 		 */
10224cf49a43SJulian Elischer 
10234cf49a43SJulian Elischer  		privp->packets_in++;
10240c65c135SJulian Elischer 		if( m->m_len < sizeof(*wh)) {
10250c65c135SJulian Elischer 			m = m_pullup(m, sizeof(*wh)); /* Checks length */
10264cf49a43SJulian Elischer 			if (m == NULL) {
1027b86d0a9eSJulian Elischer 				printf("couldn't m_pullup\n");
10284cf49a43SJulian Elischer 				LEAVE(ENOBUFS);
10294cf49a43SJulian Elischer 			}
10300c65c135SJulian Elischer 		}
10314cf49a43SJulian Elischer 		wh = mtod(m, struct pppoe_full_hdr *);
10324cf49a43SJulian Elischer 		length = ntohs(wh->ph.length);
10330c65c135SJulian Elischer 		switch(wh->eh.ether_type) {
1034fdc755d1SGleb Smirnoff 		case	ETHERTYPE_PPPOE_STUPID_DISC: /* fall through */
10354cf49a43SJulian Elischer 		case	ETHERTYPE_PPPOE_DISC:
10364cf49a43SJulian Elischer 			/*
1037bdaf2e81SJulian Elischer 			 * We need to try to make sure that the tag area
1038bdaf2e81SJulian Elischer 			 * is contiguous, or we could wander off the end
10394cf49a43SJulian Elischer 			 * of a buffer and make a mess.
10404cf49a43SJulian Elischer 			 * (Linux wouldn't have this problem).
10414cf49a43SJulian Elischer 			 */
10420c65c135SJulian Elischer 			if (m->m_pkthdr.len <= MHLEN) {
10430c65c135SJulian Elischer 				if( m->m_len < m->m_pkthdr.len) {
10440c65c135SJulian Elischer 					m = m_pullup(m, m->m_pkthdr.len);
10450c65c135SJulian Elischer 					if (m == NULL) {
10460c65c135SJulian Elischer 						printf("couldn't m_pullup\n");
10470c65c135SJulian Elischer 						LEAVE(ENOBUFS);
10480c65c135SJulian Elischer 					}
10490c65c135SJulian Elischer 				}
10500c65c135SJulian Elischer 			}
10514cf49a43SJulian Elischer 			if (m->m_len != m->m_pkthdr.len) {
10524cf49a43SJulian Elischer 				/*
10534cf49a43SJulian Elischer 				 * It's not all in one piece.
10544cf49a43SJulian Elischer 				 * We need to do extra work.
1055069154d5SJulian Elischer 				 * Put it into a cluster.
10564cf49a43SJulian Elischer 				 */
1057069154d5SJulian Elischer 				struct mbuf *n;
1058a163d034SWarner Losh 				n = m_dup(m, M_DONTWAIT);
1059069154d5SJulian Elischer 				m_freem(m);
1060069154d5SJulian Elischer 				m = n;
1061069154d5SJulian Elischer 				if (m) {
1062069154d5SJulian Elischer 					/* just check we got a cluster */
1063069154d5SJulian Elischer 					if (m->m_len != m->m_pkthdr.len) {
1064069154d5SJulian Elischer 						m_freem(m);
1065069154d5SJulian Elischer 						m = NULL;
1066069154d5SJulian Elischer 					}
1067069154d5SJulian Elischer 				}
1068069154d5SJulian Elischer 				if (m == NULL) {
10694cf49a43SJulian Elischer 					printf("packet fragmented\n");
1070b86d0a9eSJulian Elischer 					LEAVE(EMSGSIZE);
10714cf49a43SJulian Elischer 				}
1072069154d5SJulian Elischer 			}
1073069154d5SJulian Elischer 			wh = mtod(m, struct pppoe_full_hdr *);
1074069154d5SJulian Elischer 			length = ntohs(wh->ph.length);
1075069154d5SJulian Elischer 			ph = &wh->ph;
1076069154d5SJulian Elischer 			session = ntohs(wh->ph.sid);
1077069154d5SJulian Elischer 			code = wh->ph.code;
10784cf49a43SJulian Elischer 
10794cf49a43SJulian Elischer 			switch(code) {
10804cf49a43SJulian Elischer 			case	PADI_CODE:
10814cf49a43SJulian Elischer 				/*
10824cf49a43SJulian Elischer 				 * We are a server:
10834cf49a43SJulian Elischer 				 * Look for a hook with the required service
10844cf49a43SJulian Elischer 				 * and send the ENTIRE packet up there.
10854cf49a43SJulian Elischer 				 * It should come back to a new hook in
10864cf49a43SJulian Elischer 				 * PRIMED state. Look there for further
10874cf49a43SJulian Elischer 				 * processing.
10884cf49a43SJulian Elischer 				 */
10894cf49a43SJulian Elischer 				tag = get_tag(ph, PTT_SRV_NAME);
10904cf49a43SJulian Elischer 				if (tag == NULL) {
1091b86d0a9eSJulian Elischer 					printf("no service tag\n");
10924cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
10934cf49a43SJulian Elischer 				}
109430400f03SJulian Elischer 				sendhook = pppoe_match_svc(NG_HOOK_NODE(hook),
10959088fa05SBrian Somers 			    		tag->tag_data, ntohs(tag->tag_len),
10969088fa05SBrian Somers 					NG_MATCH_ANY);
10974cf49a43SJulian Elischer 				if (sendhook) {
1098069154d5SJulian Elischer 					NG_FWD_NEW_DATA(error, item,
1099069154d5SJulian Elischer 								sendhook, m);
11004cf49a43SJulian Elischer 				} else {
11014cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11024cf49a43SJulian Elischer 				}
11034cf49a43SJulian Elischer 				break;
11044cf49a43SJulian Elischer 			case	PADO_CODE:
11054cf49a43SJulian Elischer 				/*
11064cf49a43SJulian Elischer 				 * We are a client:
11074cf49a43SJulian Elischer 				 * Use the host_uniq tag to find the
11084cf49a43SJulian Elischer 				 * hook this is in response to.
1109b86d0a9eSJulian Elischer 				 * Received #2, now send #3
11104cf49a43SJulian Elischer 				 * For now simply accept the first we receive.
11114cf49a43SJulian Elischer 				 */
11121f89d938SJulian Elischer 				utag = get_tag(ph, PTT_HOST_UNIQ);
11131f89d938SJulian Elischer 				if ((utag == NULL)
11141f89d938SJulian Elischer 				|| (ntohs(utag->tag_len) != sizeof(sp))) {
1115b86d0a9eSJulian Elischer 					printf("no host unique field\n");
11164cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11174cf49a43SJulian Elischer 				}
11184cf49a43SJulian Elischer 
11191f89d938SJulian Elischer 				sendhook = pppoe_finduniq(node, utag);
11204cf49a43SJulian Elischer 				if (sendhook == NULL) {
1121b86d0a9eSJulian Elischer 					printf("no matching session\n");
11224cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11234cf49a43SJulian Elischer 				}
11244cf49a43SJulian Elischer 
11254cf49a43SJulian Elischer 				/*
11264cf49a43SJulian Elischer 				 * Check the session is in the right state.
11274cf49a43SJulian Elischer 				 * It needs to be in PPPOE_SINIT.
11284cf49a43SJulian Elischer 				 */
112930400f03SJulian Elischer 				sp = NG_HOOK_PRIVATE(sendhook);
11304cf49a43SJulian Elischer 				if (sp->state != PPPOE_SINIT) {
1131b86d0a9eSJulian Elischer 					printf("session in wrong state\n");
11324cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11334cf49a43SJulian Elischer 				}
11344cf49a43SJulian Elischer 				neg = sp->neg;
1135ef237c7fSGleb Smirnoff 				ng_uncallout(&neg->handle, node);
11364cf49a43SJulian Elischer 
11374cf49a43SJulian Elischer 				/*
11384cf49a43SJulian Elischer 				 * This is the first time we hear
11394cf49a43SJulian Elischer 				 * from the server, so note it's
11404cf49a43SJulian Elischer 				 * unicast address, replacing the
11414cf49a43SJulian Elischer 				 * broadcast address .
11424cf49a43SJulian Elischer 				 */
11434cf49a43SJulian Elischer 				bcopy(wh->eh.ether_shost,
11444cf49a43SJulian Elischer 					neg->pkt->pkt_header.eh.ether_dhost,
11454cf49a43SJulian Elischer 					ETHER_ADDR_LEN);
11464cf49a43SJulian Elischer 				neg->timeout = 0;
11474cf49a43SJulian Elischer 				neg->pkt->pkt_header.ph.code = PADR_CODE;
11484cf49a43SJulian Elischer 				init_tags(sp);
11497ccbb17bSJulian Elischer 				insert_tag(sp, utag);      /* Host Unique */
11501f89d938SJulian Elischer 				if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1151b86d0a9eSJulian Elischer 					insert_tag(sp, tag); /* return cookie */
1152c48a0b5fSBrian Somers 				if ((tag = get_tag(ph, PTT_AC_NAME))) {
11531f89d938SJulian Elischer 					insert_tag(sp, tag); /* return it */
1154c48a0b5fSBrian Somers 					send_acname(sp, tag);
1155c48a0b5fSBrian Somers 				}
11567ccbb17bSJulian Elischer 				insert_tag(sp, &neg->service.hdr); /* Service */
11574cf49a43SJulian Elischer 				scan_tags(sp, ph);
11584cf49a43SJulian Elischer 				make_packet(sp);
11594cf49a43SJulian Elischer 				sp->state = PPPOE_SREQ;
11604cf49a43SJulian Elischer 				sendpacket(sp);
11614cf49a43SJulian Elischer 				break;
11624cf49a43SJulian Elischer 			case	PADR_CODE:
11634cf49a43SJulian Elischer 
11644cf49a43SJulian Elischer 				/*
11654cf49a43SJulian Elischer 				 * We are a server:
11664cf49a43SJulian Elischer 				 * Use the ac_cookie tag to find the
11674cf49a43SJulian Elischer 				 * hook this is in response to.
11684cf49a43SJulian Elischer 				 */
11691f89d938SJulian Elischer 				utag = get_tag(ph, PTT_AC_COOKIE);
11701f89d938SJulian Elischer 				if ((utag == NULL)
11711f89d938SJulian Elischer 				|| (ntohs(utag->tag_len) != sizeof(sp))) {
11724cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11734cf49a43SJulian Elischer 				}
11744cf49a43SJulian Elischer 
11751f89d938SJulian Elischer 				sendhook = pppoe_finduniq(node, utag);
11764cf49a43SJulian Elischer 				if (sendhook == NULL) {
11774cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
11784cf49a43SJulian Elischer 				}
11794cf49a43SJulian Elischer 
11804cf49a43SJulian Elischer 				/*
11814cf49a43SJulian Elischer 				 * Check the session is in the right state.
11824cf49a43SJulian Elischer 				 * It needs to be in PPPOE_SOFFER
11834cf49a43SJulian Elischer 				 * or PPPOE_NEWCONNECTED. If the latter,
11844cf49a43SJulian Elischer 				 * then this is a retry by the client.
11854cf49a43SJulian Elischer 				 * so be nice, and resend.
11864cf49a43SJulian Elischer 				 */
118730400f03SJulian Elischer 				sp = NG_HOOK_PRIVATE(sendhook);
11884cf49a43SJulian Elischer 				if (sp->state == PPPOE_NEWCONNECTED) {
11894cf49a43SJulian Elischer 					/*
11904cf49a43SJulian Elischer 					 * Whoa! drop back to resend that
11914cf49a43SJulian Elischer 					 * PADS packet.
11924cf49a43SJulian Elischer 					 * We should still have a copy of it.
11934cf49a43SJulian Elischer 					 */
11944cf49a43SJulian Elischer 					sp->state = PPPOE_SOFFER;
11954cf49a43SJulian Elischer 				}
11964cf49a43SJulian Elischer 				if (sp->state != PPPOE_SOFFER) {
11974cf49a43SJulian Elischer 					LEAVE (ENETUNREACH);
11984cf49a43SJulian Elischer 					break;
11994cf49a43SJulian Elischer 				}
12004cf49a43SJulian Elischer 				neg = sp->neg;
1201ef237c7fSGleb Smirnoff 				ng_uncallout(&neg->handle, node);
12024cf49a43SJulian Elischer 				neg->pkt->pkt_header.ph.code = PADS_CODE;
12034cf49a43SJulian Elischer 				if (sp->Session_ID == 0)
12044cf49a43SJulian Elischer 					neg->pkt->pkt_header.ph.sid =
1205b86d0a9eSJulian Elischer 					    htons(sp->Session_ID
1206b86d0a9eSJulian Elischer 						= get_new_sid(node));
120787c4cce0SBrian Somers 				send_sessionid(sp);
12084cf49a43SJulian Elischer 				neg->timeout = 0;
12094cf49a43SJulian Elischer 				/*
12104cf49a43SJulian Elischer 				 * start working out the tags to respond with.
12114cf49a43SJulian Elischer 				 */
12124cf49a43SJulian Elischer 				init_tags(sp);
12134cf49a43SJulian Elischer 				insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1214bdaf2e81SJulian Elischer 				if ((tag = get_tag(ph, PTT_SRV_NAME)))
12154adb13fdSJulian Elischer 					insert_tag(sp, tag);/* return service */
12161f89d938SJulian Elischer 				if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
12174adb13fdSJulian Elischer 					insert_tag(sp, tag); /* return it */
12181f89d938SJulian Elischer 				insert_tag(sp, utag);	/* ac_cookie */
12194cf49a43SJulian Elischer 				scan_tags(sp, ph);
12204cf49a43SJulian Elischer 				make_packet(sp);
1221bdaf2e81SJulian Elischer 				sp->state = PPPOE_NEWCONNECTED;
12226faf164cSJulian Elischer 				sendpacket(sp);
12234cf49a43SJulian Elischer 				/*
12244cf49a43SJulian Elischer 				 * Having sent the last Negotiation header,
12254cf49a43SJulian Elischer 				 * Set up the stored packet header to
12264cf49a43SJulian Elischer 				 * be correct for the actual session.
12274cf49a43SJulian Elischer 				 * But keep the negotialtion stuff
12284cf49a43SJulian Elischer 				 * around in case we need to resend this last
12294cf49a43SJulian Elischer 				 * packet. We'll discard it when we move
12304cf49a43SJulian Elischer 				 * from NEWCONNECTED to CONNECTED
12314cf49a43SJulian Elischer 				 */
12324cf49a43SJulian Elischer 				sp->pkt_hdr = neg->pkt->pkt_header;
1233fdc755d1SGleb Smirnoff 				/* Configure ethertype depending on what
1234fdc755d1SGleb Smirnoff 				 * ethertype was used at discovery phase */
1235fdc755d1SGleb Smirnoff 				if (sp->pkt_hdr.eh.ether_type ==
1236fdc755d1SGleb Smirnoff 				    ETHERTYPE_PPPOE_STUPID_DISC)
1237bfa7e882SJulian Elischer 					sp->pkt_hdr.eh.ether_type
1238bfa7e882SJulian Elischer 						= ETHERTYPE_PPPOE_STUPID_SESS;
1239bfa7e882SJulian Elischer 				else
12404cf49a43SJulian Elischer 					sp->pkt_hdr.eh.ether_type
12414cf49a43SJulian Elischer 						= ETHERTYPE_PPPOE_SESS;
12424cf49a43SJulian Elischer 				sp->pkt_hdr.ph.code = 0;
1243b58a8a3bSJulian Elischer 				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
12444cf49a43SJulian Elischer 				break;
12454cf49a43SJulian Elischer 			case	PADS_CODE:
12464cf49a43SJulian Elischer 				/*
12474cf49a43SJulian Elischer 				 * We are a client:
12484cf49a43SJulian Elischer 				 * Use the host_uniq tag to find the
12494cf49a43SJulian Elischer 				 * hook this is in response to.
12504cf49a43SJulian Elischer 				 * take the session ID and store it away.
12514cf49a43SJulian Elischer 				 * Also make sure the pre-made header is
12524cf49a43SJulian Elischer 				 * correct and set us into Session mode.
12534cf49a43SJulian Elischer 				 */
12541f89d938SJulian Elischer 				utag = get_tag(ph, PTT_HOST_UNIQ);
12551f89d938SJulian Elischer 				if ((utag == NULL)
12561f89d938SJulian Elischer 				|| (ntohs(utag->tag_len) != sizeof(sp))) {
12574cf49a43SJulian Elischer 					LEAVE (ENETUNREACH);
12584cf49a43SJulian Elischer 					break;
12594cf49a43SJulian Elischer 				}
12601f89d938SJulian Elischer 				sendhook = pppoe_finduniq(node, utag);
12614cf49a43SJulian Elischer 				if (sendhook == NULL) {
12624cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
12634cf49a43SJulian Elischer 				}
12644cf49a43SJulian Elischer 
12654cf49a43SJulian Elischer 				/*
12664cf49a43SJulian Elischer 				 * Check the session is in the right state.
12674cf49a43SJulian Elischer 				 * It needs to be in PPPOE_SREQ.
12684cf49a43SJulian Elischer 				 */
126930400f03SJulian Elischer 				sp = NG_HOOK_PRIVATE(sendhook);
12704cf49a43SJulian Elischer 				if (sp->state != PPPOE_SREQ) {
12714cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
12724cf49a43SJulian Elischer 				}
12734cf49a43SJulian Elischer 				neg = sp->neg;
1274ef237c7fSGleb Smirnoff 				ng_uncallout(&neg->handle, node);
1275cfbcfe62SJulian Elischer 				neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1276b86d0a9eSJulian Elischer 				sp->Session_ID = ntohs(wh->ph.sid);
127787c4cce0SBrian Somers 				send_sessionid(sp);
12784cf49a43SJulian Elischer 				neg->timeout = 0;
12794cf49a43SJulian Elischer 				sp->state = PPPOE_CONNECTED;
12804cf49a43SJulian Elischer 				/*
12814cf49a43SJulian Elischer 				 * Now we have gone to Connected mode,
12824cf49a43SJulian Elischer 				 * Free all resources needed for
12834cf49a43SJulian Elischer 				 * negotiation.
12844cf49a43SJulian Elischer 				 * Keep a copy of the header we will be using.
12854cf49a43SJulian Elischer 				 */
12864cf49a43SJulian Elischer 				sp->pkt_hdr = neg->pkt->pkt_header;
1287fdc755d1SGleb Smirnoff 				if (privp->mode->id == PPPOE_NONSTANDARD)
1288bfa7e882SJulian Elischer 					sp->pkt_hdr.eh.ether_type
1289bfa7e882SJulian Elischer 						= ETHERTYPE_PPPOE_STUPID_SESS;
1290bfa7e882SJulian Elischer 				else
12914cf49a43SJulian Elischer 					sp->pkt_hdr.eh.ether_type
12924cf49a43SJulian Elischer 						= ETHERTYPE_PPPOE_SESS;
12934cf49a43SJulian Elischer 				sp->pkt_hdr.ph.code = 0;
12944cf49a43SJulian Elischer 				m_freem(neg->m);
12959c8c302fSJulian Elischer 				FREE(sp->neg, M_NETGRAPH_PPPOE);
12964cf49a43SJulian Elischer 				sp->neg = NULL;
1297b58a8a3bSJulian Elischer 				pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
12984cf49a43SJulian Elischer 				break;
12994cf49a43SJulian Elischer 			case	PADT_CODE:
13004cf49a43SJulian Elischer 				/*
13014cf49a43SJulian Elischer 				 * Send a 'close' message to the controlling
13024cf49a43SJulian Elischer 				 * process (the one that set us up);
13034cf49a43SJulian Elischer 				 * And then tear everything down.
13044cf49a43SJulian Elischer 				 *
13054cf49a43SJulian Elischer 				 * Find matching peer/session combination.
13064cf49a43SJulian Elischer 				 */
13074cf49a43SJulian Elischer 				sendhook = pppoe_findsession(node, wh);
13084cf49a43SJulian Elischer 				if (sendhook == NULL) {
13094cf49a43SJulian Elischer 					LEAVE(ENETUNREACH);
13104cf49a43SJulian Elischer 				}
13114cf49a43SJulian Elischer 				/* send message to creator */
13124cf49a43SJulian Elischer 				/* close hook */
1313b58a8a3bSJulian Elischer 				if (sendhook) {
1314954c4772SJulian Elischer 					ng_rmhook_self(sendhook);
1315b58a8a3bSJulian Elischer 				}
13164cf49a43SJulian Elischer 				break;
13174cf49a43SJulian Elischer 			default:
13184cf49a43SJulian Elischer 				LEAVE(EPFNOSUPPORT);
13194cf49a43SJulian Elischer 			}
13204cf49a43SJulian Elischer 			break;
1321bfa7e882SJulian Elischer 		case	ETHERTYPE_PPPOE_STUPID_SESS:
13224cf49a43SJulian Elischer 		case	ETHERTYPE_PPPOE_SESS:
13234cf49a43SJulian Elischer 			/*
13244cf49a43SJulian Elischer 			 * find matching peer/session combination.
13254cf49a43SJulian Elischer 			 */
13264cf49a43SJulian Elischer 			sendhook = pppoe_findsession(node, wh);
13274cf49a43SJulian Elischer 			if (sendhook == NULL) {
13284cf49a43SJulian Elischer 				LEAVE (ENETUNREACH);
13294cf49a43SJulian Elischer 				break;
13304cf49a43SJulian Elischer 			}
133130400f03SJulian Elischer 			sp = NG_HOOK_PRIVATE(sendhook);
13324cf49a43SJulian Elischer 			m_adj(m, sizeof(*wh));
13334cf49a43SJulian Elischer 			if (m->m_pkthdr.len < length) {
13344cf49a43SJulian Elischer 				/* Packet too short, dump it */
13354cf49a43SJulian Elischer 				LEAVE(EMSGSIZE);
13364cf49a43SJulian Elischer 			}
13379fcb3d83SJulian Elischer 
13384adb13fdSJulian Elischer 			/* Also need to trim excess at the end */
13399fcb3d83SJulian Elischer 			if (m->m_pkthdr.len > length) {
13409fcb3d83SJulian Elischer 				m_adj(m, -((int)(m->m_pkthdr.len - length)));
13419fcb3d83SJulian Elischer 			}
13424cf49a43SJulian Elischer 			if ( sp->state != PPPOE_CONNECTED) {
13434cf49a43SJulian Elischer 				if (sp->state == PPPOE_NEWCONNECTED) {
13444cf49a43SJulian Elischer 					sp->state = PPPOE_CONNECTED;
13454cf49a43SJulian Elischer 					/*
13464cf49a43SJulian Elischer 					 * Now we have gone to Connected mode,
13474cf49a43SJulian Elischer 					 * Free all resources needed for
1348a4ec03cfSJulian Elischer 					 * negotiation. Be paranoid about
1349a4ec03cfSJulian Elischer 					 * whether there may be a timeout.
13504cf49a43SJulian Elischer 					 */
13514cf49a43SJulian Elischer 					m_freem(sp->neg->m);
1352ef237c7fSGleb Smirnoff 					ng_uncallout(&sp->neg->handle, node);
13539c8c302fSJulian Elischer 					FREE(sp->neg, M_NETGRAPH_PPPOE);
13544cf49a43SJulian Elischer 					sp->neg = NULL;
13554cf49a43SJulian Elischer 				} else {
13564cf49a43SJulian Elischer 					LEAVE (ENETUNREACH);
13574cf49a43SJulian Elischer 					break;
13584cf49a43SJulian Elischer 				}
13594cf49a43SJulian Elischer 			}
1360069154d5SJulian Elischer 			NG_FWD_NEW_DATA( error, item, sendhook, m);
13614cf49a43SJulian Elischer 			break;
13624cf49a43SJulian Elischer 		default:
13634b276f90SJulian Elischer 			LEAVE(EPFNOSUPPORT);
13644cf49a43SJulian Elischer 		}
13654cf49a43SJulian Elischer 	} else {
13664cf49a43SJulian Elischer 		/*
13674cf49a43SJulian Elischer 		 * 	Not ethernet or debug hook..
13684cf49a43SJulian Elischer 		 *
13694cf49a43SJulian Elischer 		 * The packet has come in on a normal hook.
13704cf49a43SJulian Elischer 		 * We need to find out what kind of hook,
13714cf49a43SJulian Elischer 		 * So we can decide how to handle it.
13724cf49a43SJulian Elischer 		 * Check the hook's state.
13734cf49a43SJulian Elischer 		 */
137430400f03SJulian Elischer 		sp = NG_HOOK_PRIVATE(hook);
13754cf49a43SJulian Elischer 		switch (sp->state) {
13764cf49a43SJulian Elischer 		case	PPPOE_NEWCONNECTED:
13774cf49a43SJulian Elischer 		case	PPPOE_CONNECTED: {
13787b38c4e4SArchie Cobbs 			static const u_char addrctrl[] = { 0xff, 0x03 };
13794cf49a43SJulian Elischer 			struct pppoe_full_hdr *wh;
13807b38c4e4SArchie Cobbs 
13817b38c4e4SArchie Cobbs 			/*
13827b38c4e4SArchie Cobbs 			 * Remove PPP address and control fields, if any.
13837b38c4e4SArchie Cobbs 			 * For example, ng_ppp(4) always sends LCP packets
13847b38c4e4SArchie Cobbs 			 * with address and control fields as required by
13857b38c4e4SArchie Cobbs 			 * generic PPP. PPPoE is an exception to the rule.
13867b38c4e4SArchie Cobbs 			 */
13877b38c4e4SArchie Cobbs 			if (m->m_pkthdr.len >= 2) {
13887b38c4e4SArchie Cobbs 				if (m->m_len < 2 && !(m = m_pullup(m, 2)))
13897b38c4e4SArchie Cobbs 					LEAVE(ENOBUFS);
13907b38c4e4SArchie Cobbs 				if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
13917b38c4e4SArchie Cobbs 					m_adj(m, 2);
13927b38c4e4SArchie Cobbs 			}
13934cf49a43SJulian Elischer 			/*
13944cf49a43SJulian Elischer 			 * Bang in a pre-made header, and set the length up
13954cf49a43SJulian Elischer 			 * to be correct. Then send it to the ethernet driver.
1396d9da9cbaSJulian Elischer 			 * But first correct the length.
13974cf49a43SJulian Elischer 			 */
1398d9da9cbaSJulian Elischer 			sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1399a163d034SWarner Losh 			M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
14004cf49a43SJulian Elischer 			if (m == NULL) {
14014cf49a43SJulian Elischer 				LEAVE(ENOBUFS);
14024cf49a43SJulian Elischer 			}
14034cf49a43SJulian Elischer 			wh = mtod(m, struct pppoe_full_hdr *);
14044cf49a43SJulian Elischer 			bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1405069154d5SJulian Elischer 			NG_FWD_NEW_DATA( error, item, privp->ethernet_hook, m);
14064cf49a43SJulian Elischer 			privp->packets_out++;
14074cf49a43SJulian Elischer 			break;
14084cf49a43SJulian Elischer 			}
14094cf49a43SJulian Elischer 		case	PPPOE_PRIMED:
14104cf49a43SJulian Elischer 			/*
14114cf49a43SJulian Elischer 			 * A PADI packet is being returned by the application
14124cf49a43SJulian Elischer 			 * that has set up this hook. This indicates that it
14134cf49a43SJulian Elischer 			 * wants us to offer service.
14144cf49a43SJulian Elischer 			 */
14154cf49a43SJulian Elischer 			neg = sp->neg;
1416bdaf2e81SJulian Elischer 			if (m->m_len < sizeof(*wh)) {
1417bdaf2e81SJulian Elischer 				m = m_pullup(m, sizeof(*wh));
14184cf49a43SJulian Elischer 				if (m == NULL) {
14194cf49a43SJulian Elischer 					LEAVE(ENOBUFS);
14204cf49a43SJulian Elischer 				}
1421bdaf2e81SJulian Elischer 			}
14224cf49a43SJulian Elischer 			wh = mtod(m, struct pppoe_full_hdr *);
14234cf49a43SJulian Elischer 			ph = &wh->ph;
14244cf49a43SJulian Elischer 			session = ntohs(wh->ph.sid);
14254cf49a43SJulian Elischer 			length = ntohs(wh->ph.length);
14264cf49a43SJulian Elischer 			code = wh->ph.code;
1427fdc755d1SGleb Smirnoff 			/* Use peers mode in session */
1428fdc755d1SGleb Smirnoff 			neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
14291e2510f8SJulian Elischer 			if ( code != PADI_CODE) {
14301e2510f8SJulian Elischer 				LEAVE(EINVAL);
14311e2510f8SJulian Elischer 			};
1432ef237c7fSGleb Smirnoff 			ng_uncallout(&neg->handle, node);
14334cf49a43SJulian Elischer 
14344cf49a43SJulian Elischer 			/*
14354cf49a43SJulian Elischer 			 * This is the first time we hear
14364cf49a43SJulian Elischer 			 * from the client, so note it's
14374cf49a43SJulian Elischer 			 * unicast address, replacing the
14384cf49a43SJulian Elischer 			 * broadcast address.
14394cf49a43SJulian Elischer 			 */
14404cf49a43SJulian Elischer 			bcopy(wh->eh.ether_shost,
14414cf49a43SJulian Elischer 				neg->pkt->pkt_header.eh.ether_dhost,
14424cf49a43SJulian Elischer 				ETHER_ADDR_LEN);
14434cf49a43SJulian Elischer 			sp->state = PPPOE_SOFFER;
14444cf49a43SJulian Elischer 			neg->timeout = 0;
14454cf49a43SJulian Elischer 			neg->pkt->pkt_header.ph.code = PADO_CODE;
14464cf49a43SJulian Elischer 
14474cf49a43SJulian Elischer 			/*
14484cf49a43SJulian Elischer 			 * start working out the tags to respond with.
14494cf49a43SJulian Elischer 			 */
14504cf49a43SJulian Elischer 			uniqtag.hdr.tag_type = PTT_AC_COOKIE;
14514cf49a43SJulian Elischer 			uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
14524cf49a43SJulian Elischer 			uniqtag.data.pointer = sp;
14534cf49a43SJulian Elischer 			init_tags(sp);
14544cf49a43SJulian Elischer 			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
14551f89d938SJulian Elischer 			if ((tag = get_tag(ph, PTT_SRV_NAME)))
14564adb13fdSJulian Elischer 				insert_tag(sp, tag);	  /* return service */
1457859a4d16SJulian Elischer 			/*
1458859a4d16SJulian Elischer 			 * If we have a NULL service request
1459859a4d16SJulian Elischer 			 * and have an extra service defined in this hook,
1460859a4d16SJulian Elischer 			 * then also add a tag for the extra service.
1461859a4d16SJulian Elischer 			 * XXX this is a hack. eventually we should be able
1462859a4d16SJulian Elischer 			 * to support advertising many services, not just one
1463859a4d16SJulian Elischer 			 */
1464859a4d16SJulian Elischer 			if (((tag == NULL) || (tag->tag_len == 0))
1465859a4d16SJulian Elischer 			&& (neg->service.hdr.tag_len != 0)) {
1466859a4d16SJulian Elischer 				insert_tag(sp, &neg->service.hdr); /* SERVICE */
1467859a4d16SJulian Elischer 			}
14681f89d938SJulian Elischer 			if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
14691f89d938SJulian Elischer 				insert_tag(sp, tag); /* returned hostunique */
14701f89d938SJulian Elischer 			insert_tag(sp, &uniqtag.hdr);
14714cf49a43SJulian Elischer 			scan_tags(sp, ph);
14724cf49a43SJulian Elischer 			make_packet(sp);
14734cf49a43SJulian Elischer 			sendpacket(sp);
14744cf49a43SJulian Elischer 			break;
14754cf49a43SJulian Elischer 
14764cf49a43SJulian Elischer 		/*
14774cf49a43SJulian Elischer 		 * Packets coming from the hook make no sense
14784cf49a43SJulian Elischer 		 * to sessions in these states. Throw them away.
14794cf49a43SJulian Elischer 		 */
14804cf49a43SJulian Elischer 		case	PPPOE_SINIT:
14814cf49a43SJulian Elischer 		case	PPPOE_SREQ:
14824cf49a43SJulian Elischer 		case	PPPOE_SOFFER:
14834cf49a43SJulian Elischer 		case	PPPOE_SNONE:
14844cf49a43SJulian Elischer 		case	PPPOE_LISTENING:
14854cf49a43SJulian Elischer 		case	PPPOE_DEAD:
14864cf49a43SJulian Elischer 		default:
14874cf49a43SJulian Elischer 			LEAVE(ENETUNREACH);
14884cf49a43SJulian Elischer 		}
14894cf49a43SJulian Elischer 	}
14904cf49a43SJulian Elischer quit:
1491f5856029SJulian Elischer 	if (item)
1492069154d5SJulian Elischer 		NG_FREE_ITEM(item);
1493069154d5SJulian Elischer 	NG_FREE_M(m);
14944cf49a43SJulian Elischer 	return error;
14954cf49a43SJulian Elischer }
14964cf49a43SJulian Elischer 
14974cf49a43SJulian Elischer /*
14984cf49a43SJulian Elischer  * Do local shutdown processing..
14994cf49a43SJulian Elischer  * If we are a persistant device, we might refuse to go away, and
15004cf49a43SJulian Elischer  * we'd only remove our links and reset ourself.
15014cf49a43SJulian Elischer  */
15024cf49a43SJulian Elischer static int
1503069154d5SJulian Elischer ng_pppoe_shutdown(node_p node)
15044cf49a43SJulian Elischer {
150530400f03SJulian Elischer 	const priv_p privdata = NG_NODE_PRIVATE(node);
15064cf49a43SJulian Elischer 
1507f2b9562cSGleb Smirnoff 	DBG;
150830400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
150930400f03SJulian Elischer 	NG_NODE_UNREF(privdata->node);
15109c8c302fSJulian Elischer 	FREE(privdata, M_NETGRAPH_PPPOE);
15114cf49a43SJulian Elischer 	return (0);
15124cf49a43SJulian Elischer }
15134cf49a43SJulian Elischer 
15144cf49a43SJulian Elischer /*
15154cf49a43SJulian Elischer  * Hook disconnection
15164cf49a43SJulian Elischer  *
15176faf164cSJulian Elischer  * Clean up all dangling links and information about the session/hook.
15184cf49a43SJulian Elischer  * For this type, removal of the last link destroys the node
15194cf49a43SJulian Elischer  */
15204cf49a43SJulian Elischer static int
15218876b55dSJulian Elischer ng_pppoe_disconnect(hook_p hook)
15224cf49a43SJulian Elischer {
152330400f03SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
152430400f03SJulian Elischer 	priv_p privp = NG_NODE_PRIVATE(node);
15254cf49a43SJulian Elischer 	sessp	sp;
152604853d8aSJulian Elischer 	int 	hooks;
15274cf49a43SJulian Elischer 
1528f2b9562cSGleb Smirnoff 	DBG;
152930400f03SJulian Elischer 	hooks = NG_NODE_NUMHOOKS(node); /* this one already not counted */
153030400f03SJulian Elischer 	if (NG_HOOK_PRIVATE(hook) == &privp->debug_hook) {
15314cf49a43SJulian Elischer 		privp->debug_hook = NULL;
153230400f03SJulian Elischer 	} else if (NG_HOOK_PRIVATE(hook) == &privp->ethernet_hook) {
15334cf49a43SJulian Elischer 		privp->ethernet_hook = NULL;
153430400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node))
1535069154d5SJulian Elischer 			ng_rmnode_self(node);
15364cf49a43SJulian Elischer 	} else {
153730400f03SJulian Elischer 		sp = NG_HOOK_PRIVATE(hook);
1538b58a8a3bSJulian Elischer 		if (sp->state != PPPOE_SNONE ) {
1539b58a8a3bSJulian Elischer 			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1540b58a8a3bSJulian Elischer 		}
1541a4ec03cfSJulian Elischer 		/*
1542a4ec03cfSJulian Elischer 		 * According to the spec, if we are connected,
1543a4ec03cfSJulian Elischer 		 * we should send a DISC packet if we are shutting down
1544a4ec03cfSJulian Elischer 		 * a session.
1545a4ec03cfSJulian Elischer 		 */
15469fcb3d83SJulian Elischer 		if ((privp->ethernet_hook)
15479fcb3d83SJulian Elischer 		&& ((sp->state == PPPOE_CONNECTED)
15489fcb3d83SJulian Elischer 		 || (sp->state == PPPOE_NEWCONNECTED))) {
15499fcb3d83SJulian Elischer 			struct mbuf *m;
15509fcb3d83SJulian Elischer 			struct pppoe_full_hdr *wh;
15519fcb3d83SJulian Elischer 			struct pppoe_tag *tag;
15529fcb3d83SJulian Elischer 			int	msglen = strlen(SIGNOFF);
15539fcb3d83SJulian Elischer 			int error = 0;
15549fcb3d83SJulian Elischer 
15559fcb3d83SJulian Elischer 			/* revert the stored header to DISC/PADT mode */
15569fcb3d83SJulian Elischer 		 	wh = &sp->pkt_hdr;
15579fcb3d83SJulian Elischer 			wh->ph.code = PADT_CODE;
1558fdc755d1SGleb Smirnoff 			/* Configure ethertype depending on what was used during
1559fdc755d1SGleb Smirnoff 			 * sessions stage. */
1560fdc755d1SGleb Smirnoff 			if (sp->pkt_hdr.eh.ether_type ==
1561fdc755d1SGleb Smirnoff 			    ETHERTYPE_PPPOE_STUPID_SESS)
1562bfa7e882SJulian Elischer 				wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1563bfa7e882SJulian Elischer 			else
15649fcb3d83SJulian Elischer 				wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
15659fcb3d83SJulian Elischer 
15669fcb3d83SJulian Elischer 			/* generate a packet of that type */
1567a163d034SWarner Losh 			MGETHDR(m, M_DONTWAIT, MT_DATA);
15686faf164cSJulian Elischer 			if(m == NULL)
15696faf164cSJulian Elischer 				printf("pppoe: Session out of mbufs\n");
15706faf164cSJulian Elischer 			else {
15719fcb3d83SJulian Elischer 				m->m_pkthdr.rcvif = NULL;
15729fcb3d83SJulian Elischer 				m->m_pkthdr.len = m->m_len = sizeof(*wh);
15736faf164cSJulian Elischer 				bcopy((caddr_t)wh, mtod(m, caddr_t),
15746faf164cSJulian Elischer 				    sizeof(*wh));
15756faf164cSJulian Elischer 				/*
15766faf164cSJulian Elischer 				 * Add a General error message and adjust
15776faf164cSJulian Elischer 				 * sizes
15786faf164cSJulian Elischer 				 */
15799fcb3d83SJulian Elischer 				wh = mtod(m, struct pppoe_full_hdr *);
15809fcb3d83SJulian Elischer 				tag = wh->ph.tag;
15819fcb3d83SJulian Elischer 				tag->tag_type = PTT_GEN_ERR;
15829fcb3d83SJulian Elischer 				tag->tag_len = htons((u_int16_t)msglen);
15839fcb3d83SJulian Elischer 				strncpy(tag->tag_data, SIGNOFF, msglen);
15846faf164cSJulian Elischer 				m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
15856faf164cSJulian Elischer 				    msglen);
15869fcb3d83SJulian Elischer 				wh->ph.length = htons(sizeof(*tag) + msglen);
1587069154d5SJulian Elischer 				NG_SEND_DATA_ONLY(error,
1588069154d5SJulian Elischer 					privp->ethernet_hook, m);
15896faf164cSJulian Elischer 			}
15909fcb3d83SJulian Elischer 		}
1591a4ec03cfSJulian Elischer 		/*
1592514baf3fSJeroen Ruigrok van der Werven 		 * As long as we have somewhere to store the timeout handle,
1593a4ec03cfSJulian Elischer 		 * we may have a timeout pending.. get rid of it.
1594a4ec03cfSJulian Elischer 		 */
15951e2510f8SJulian Elischer 		if (sp->neg) {
1596ef237c7fSGleb Smirnoff 			ng_uncallout(&sp->neg->handle, node);
15971e2510f8SJulian Elischer 			if (sp->neg->m)
15981e2510f8SJulian Elischer 				m_freem(sp->neg->m);
15999c8c302fSJulian Elischer 			FREE(sp->neg, M_NETGRAPH_PPPOE);
16001e2510f8SJulian Elischer 		}
16019c8c302fSJulian Elischer 		FREE(sp, M_NETGRAPH_PPPOE);
160230400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, NULL);
1603ed52f174SJulian Elischer 		/* work out how many session hooks there are */
160404853d8aSJulian Elischer 		/* Node goes away on last session hook removal */
160504853d8aSJulian Elischer 		if (privp->ethernet_hook) hooks -= 1;
1606ed52f174SJulian Elischer 		if (privp->debug_hook) hooks -= 1;
16074cf49a43SJulian Elischer 	}
160830400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(node) == 0)
160930400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(node)))
1610069154d5SJulian Elischer 		ng_rmnode_self(node);
16114cf49a43SJulian Elischer 	return (0);
16124cf49a43SJulian Elischer }
16134cf49a43SJulian Elischer 
16144cf49a43SJulian Elischer /*
16154cf49a43SJulian Elischer  * timeouts come here.
16164cf49a43SJulian Elischer  */
16174cf49a43SJulian Elischer static void
1618ef237c7fSGleb Smirnoff pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
16194cf49a43SJulian Elischer {
162030400f03SJulian Elischer 	sessp	sp = NG_HOOK_PRIVATE(hook);
16214cf49a43SJulian Elischer 	negp	neg = sp->neg;
16224cf49a43SJulian Elischer 	int	error = 0;
16234cf49a43SJulian Elischer 	struct mbuf *m0 = NULL;
162430400f03SJulian Elischer 	priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
16254cf49a43SJulian Elischer 
1626f2b9562cSGleb Smirnoff 	DBG;
16274cf49a43SJulian Elischer 	switch(sp->state) {
16284cf49a43SJulian Elischer 		/*
16294cf49a43SJulian Elischer 		 * resend the last packet, using an exponential backoff.
16304cf49a43SJulian Elischer 		 * After a period of time, stop growing the backoff,
16314adb13fdSJulian Elischer 		 * and either leave it, or revert to the start.
16324cf49a43SJulian Elischer 		 */
16334cf49a43SJulian Elischer 	case	PPPOE_SINIT:
16344cf49a43SJulian Elischer 	case	PPPOE_SREQ:
16354cf49a43SJulian Elischer 		/* timeouts on these produce resends */
1636a163d034SWarner Losh 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1637069154d5SJulian Elischer 		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1638ef237c7fSGleb Smirnoff 		ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1639ef237c7fSGleb Smirnoff 		    pppoe_ticker, NULL, 0);
16404cf49a43SJulian Elischer 		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
16414cf49a43SJulian Elischer 			if (sp->state == PPPOE_SREQ) {
16424cf49a43SJulian Elischer 				/* revert to SINIT mode */
1643b58a8a3bSJulian Elischer 				pppoe_start(sp);
16444cf49a43SJulian Elischer 			} else {
16454cf49a43SJulian Elischer 				neg->timeout = PPPOE_TIMEOUT_LIMIT;
16464cf49a43SJulian Elischer 			}
16474cf49a43SJulian Elischer 		}
16484cf49a43SJulian Elischer 		break;
16494cf49a43SJulian Elischer 	case	PPPOE_PRIMED:
16504cf49a43SJulian Elischer 	case	PPPOE_SOFFER:
16514cf49a43SJulian Elischer 		/* a timeout on these says "give up" */
1652954c4772SJulian Elischer 		ng_rmhook_self(hook);
16534cf49a43SJulian Elischer 		break;
16544cf49a43SJulian Elischer 	default:
16554cf49a43SJulian Elischer 		/* timeouts have no meaning in other states */
16564cf49a43SJulian Elischer 		printf("pppoe: unexpected timeout\n");
16574cf49a43SJulian Elischer 	}
16584cf49a43SJulian Elischer }
16594cf49a43SJulian Elischer 
16604cf49a43SJulian Elischer 
16614cf49a43SJulian Elischer static void
16624cf49a43SJulian Elischer sendpacket(sessp sp)
16634cf49a43SJulian Elischer {
16644cf49a43SJulian Elischer 	struct	mbuf *m0 = NULL;
16654cf49a43SJulian Elischer 	hook_p	hook = sp->hook;
1666ef237c7fSGleb Smirnoff 	node_p	node = NG_HOOK_NODE(hook);
1667ef237c7fSGleb Smirnoff 	priv_p	privp = NG_NODE_PRIVATE(node);
16684cf49a43SJulian Elischer 	negp	neg = sp->neg;
1669ef237c7fSGleb Smirnoff 	int	error = 0;
16704cf49a43SJulian Elischer 
1671f2b9562cSGleb Smirnoff 	DBG;
16724cf49a43SJulian Elischer 	switch(sp->state) {
16734cf49a43SJulian Elischer 	case	PPPOE_LISTENING:
16744cf49a43SJulian Elischer 	case	PPPOE_DEAD:
16754cf49a43SJulian Elischer 	case	PPPOE_SNONE:
16764cf49a43SJulian Elischer 	case	PPPOE_CONNECTED:
1677b86d0a9eSJulian Elischer 		printf("pppoe: sendpacket: unexpected state\n");
16784cf49a43SJulian Elischer 		break;
16794cf49a43SJulian Elischer 
16806faf164cSJulian Elischer 	case	PPPOE_NEWCONNECTED:
16816faf164cSJulian Elischer 		/* send the PADS without a timeout - we're now connected */
1682a163d034SWarner Losh 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1683069154d5SJulian Elischer 		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
16846faf164cSJulian Elischer 		break;
16856faf164cSJulian Elischer 
16864cf49a43SJulian Elischer 	case	PPPOE_PRIMED:
16874cf49a43SJulian Elischer 		/* No packet to send, but set up the timeout */
1688ef237c7fSGleb Smirnoff 		ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1689ef237c7fSGleb Smirnoff 		    pppoe_ticker, NULL, 0);
16904cf49a43SJulian Elischer 		break;
16914cf49a43SJulian Elischer 
16924cf49a43SJulian Elischer 	case	PPPOE_SOFFER:
16934cf49a43SJulian Elischer 		/*
16944cf49a43SJulian Elischer 		 * send the offer but if they don't respond
16954cf49a43SJulian Elischer 		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
16964cf49a43SJulian Elischer 		 */
1697a163d034SWarner Losh 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1698069154d5SJulian Elischer 		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1699ef237c7fSGleb Smirnoff 		ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1700ef237c7fSGleb Smirnoff 		    pppoe_ticker, NULL, 0);
17014cf49a43SJulian Elischer 		break;
17024cf49a43SJulian Elischer 
17034cf49a43SJulian Elischer 	case	PPPOE_SINIT:
17044cf49a43SJulian Elischer 	case	PPPOE_SREQ:
1705a163d034SWarner Losh 		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1706069154d5SJulian Elischer 		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1707ef237c7fSGleb Smirnoff 		ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1708ef237c7fSGleb Smirnoff 		    pppoe_ticker, NULL, 0);
1709d0fef808SJulian Elischer 		neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
17104cf49a43SJulian Elischer 		break;
17114cf49a43SJulian Elischer 
17124cf49a43SJulian Elischer 	default:
17134cf49a43SJulian Elischer 		error = EINVAL;
17144cf49a43SJulian Elischer 		printf("pppoe: timeout: bad state\n");
17154cf49a43SJulian Elischer 	}
17164cf49a43SJulian Elischer 	/* return (error); */
17174cf49a43SJulian Elischer }
17184cf49a43SJulian Elischer 
17194cf49a43SJulian Elischer /*
17204cf49a43SJulian Elischer  * Parse an incoming packet to see if any tags should be copied to the
17214adb13fdSJulian Elischer  * output packet. Don't do any tags that have been handled in the main
17224adb13fdSJulian Elischer  * state machine.
17234cf49a43SJulian Elischer  */
1724816b834fSArchie Cobbs static const struct pppoe_tag*
1725816b834fSArchie Cobbs scan_tags(sessp	sp, const struct pppoe_hdr* ph)
17264cf49a43SJulian Elischer {
1727816b834fSArchie Cobbs 	const char *const end = (const char *)next_tag(ph);
1728816b834fSArchie Cobbs 	const char *ptn;
1729816b834fSArchie Cobbs 	const struct pppoe_tag *pt = &ph->tag[0];
17304cf49a43SJulian Elischer 	/*
17314cf49a43SJulian Elischer 	 * Keep processing tags while a tag header will still fit.
17324cf49a43SJulian Elischer 	 */
1733f2b9562cSGleb Smirnoff 	DBG;
1734816b834fSArchie Cobbs 	while((const char*)(pt + 1) <= end) {
17354cf49a43SJulian Elischer 		/*
17364cf49a43SJulian Elischer 		 * If the tag data would go past the end of the packet, abort.
17374cf49a43SJulian Elischer 		 */
1738816b834fSArchie Cobbs 		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
17394cf49a43SJulian Elischer 		if(ptn > end)
17404cf49a43SJulian Elischer 			return NULL;
17414cf49a43SJulian Elischer 
17424cf49a43SJulian Elischer 		switch (pt->tag_type) {
17434cf49a43SJulian Elischer 		case	PTT_RELAY_SID:
17444cf49a43SJulian Elischer 			insert_tag(sp, pt);
17454cf49a43SJulian Elischer 			break;
17464cf49a43SJulian Elischer 		case	PTT_EOL:
17474cf49a43SJulian Elischer 			return NULL;
17484cf49a43SJulian Elischer 		case	PTT_SRV_NAME:
17494cf49a43SJulian Elischer 		case	PTT_AC_NAME:
17504cf49a43SJulian Elischer 		case	PTT_HOST_UNIQ:
17514cf49a43SJulian Elischer 		case	PTT_AC_COOKIE:
17524cf49a43SJulian Elischer 		case	PTT_VENDOR:
17534cf49a43SJulian Elischer 		case	PTT_SRV_ERR:
17544cf49a43SJulian Elischer 		case	PTT_SYS_ERR:
17554cf49a43SJulian Elischer 		case	PTT_GEN_ERR:
17564cf49a43SJulian Elischer 			break;
17574cf49a43SJulian Elischer 		}
1758816b834fSArchie Cobbs 		pt = (const struct pppoe_tag*)ptn;
17594cf49a43SJulian Elischer 	}
17604cf49a43SJulian Elischer 	return NULL;
17614cf49a43SJulian Elischer }
17624cf49a43SJulian Elischer 
1763b58a8a3bSJulian Elischer static	int
1764b58a8a3bSJulian Elischer pppoe_send_event(sessp sp, enum cmd cmdid)
1765b58a8a3bSJulian Elischer {
1766b58a8a3bSJulian Elischer 	int error;
1767b58a8a3bSJulian Elischer 	struct ng_mesg *msg;
17688876b55dSJulian Elischer 	struct ngpppoe_sts *sts;
1769b58a8a3bSJulian Elischer 
1770f2b9562cSGleb Smirnoff 	DBG;
177127121ab1SBrian Somers 	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
17728876b55dSJulian Elischer 			sizeof(struct ngpppoe_sts), M_NOWAIT);
1773859a4d16SJulian Elischer 	if (msg == NULL)
1774859a4d16SJulian Elischer 		return (ENOMEM);
17758876b55dSJulian Elischer 	sts = (struct ngpppoe_sts *)msg->data;
177687e2c66aSHartmut Brandt 	strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1777facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1778b58a8a3bSJulian Elischer 	return (error);
1779b58a8a3bSJulian Elischer }
1780