ng_pppoe.c revision 175867
152419Sjulian/*
252419Sjulian * ng_pppoe.c
3139823Simp */
4139823Simp
5139823Simp/*-
652419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
752419Sjulian * All rights reserved.
852419Sjulian *
952419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
1052419Sjulian * redistribution of this software, in source or object code forms, with or
1152419Sjulian * without modifications are expressly permitted by Whistle Communications;
1252419Sjulian * provided, however, that:
1352419Sjulian * 1. Any and all reproductions of the source or object code must include the
1452419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1552419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1652419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1752419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1852419Sjulian *    such appears in the above copyright notice or in the software.
1952419Sjulian *
2052419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2152419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2252419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2352419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2452419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2552419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2652419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2752419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2852419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2952419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
3052419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3152419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3252419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3352419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3452419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3552419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3652419Sjulian * OF SUCH DAMAGE.
3752419Sjulian *
3867506Sjulian * Author: Julian Elischer <julian@freebsd.org>
3952419Sjulian *
4052419Sjulian * $FreeBSD: head/sys/netgraph/ng_pppoe.c 175867 2008-02-01 07:25:06Z mav $
4152752Sjulian * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
4252419Sjulian */
4352419Sjulian
4452419Sjulian#include <sys/param.h>
4552419Sjulian#include <sys/systm.h>
4652419Sjulian#include <sys/kernel.h>
47154604Sglebius#include <sys/ktr.h>
4852419Sjulian#include <sys/mbuf.h>
4952419Sjulian#include <sys/malloc.h>
5052419Sjulian#include <sys/errno.h>
51161034Sglebius#include <sys/syslog.h>
5252419Sjulian#include <net/ethernet.h>
5352419Sjulian
5452419Sjulian#include <netgraph/ng_message.h>
5552419Sjulian#include <netgraph/netgraph.h>
5668031Sbrian#include <netgraph/ng_parse.h>
5752419Sjulian#include <netgraph/ng_pppoe.h>
58161117Sglebius#include <netgraph/ng_ether.h>
5952419Sjulian
6070870Sjulian#ifdef NG_SEPARATE_MALLOC
6170870SjulianMALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
6270870Sjulian#else
6370870Sjulian#define M_NETGRAPH_PPPOE M_NETGRAPH
6470870Sjulian#endif
6570870Sjulian
6653405Sarchie#define SIGNOFF "session closed"
6753405Sarchie
6852419Sjulian/*
6952419Sjulian * This section contains the netgraph method declarations for the
7072946Sjulian * pppoe node. These methods define the netgraph pppoe 'type'.
7152419Sjulian */
7252419Sjulian
7352752Sjulianstatic ng_constructor_t	ng_pppoe_constructor;
7452752Sjulianstatic ng_rcvmsg_t	ng_pppoe_rcvmsg;
7570700Sjulianstatic ng_shutdown_t	ng_pppoe_shutdown;
7652752Sjulianstatic ng_newhook_t	ng_pppoe_newhook;
77161117Sglebiusstatic ng_connect_t	ng_pppoe_connect;
7852752Sjulianstatic ng_rcvdata_t	ng_pppoe_rcvdata;
79172629Smavstatic ng_rcvdata_t	ng_pppoe_rcvdata_ether;
80172629Smavstatic ng_rcvdata_t	ng_pppoe_rcvdata_debug;
8152752Sjulianstatic ng_disconnect_t	ng_pppoe_disconnect;
8252419Sjulian
8368031Sbrian/* Parse type for struct ngpppoe_init_data */
8497685Sarchiestatic const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
8568031Sbrian	= NG_PPPOE_INIT_DATA_TYPE_INFO;
8668845Sbrianstatic const struct ng_parse_type ngpppoe_init_data_state_type = {
8768031Sbrian	&ng_parse_struct_type,
8897685Sarchie	&ngpppoe_init_data_type_fields
8968031Sbrian};
9068031Sbrian
9168031Sbrian/* Parse type for struct ngpppoe_sts */
9297685Sarchiestatic const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
9368031Sbrian	= NG_PPPOE_STS_TYPE_INFO;
9468031Sbrianstatic const struct ng_parse_type ng_pppoe_sts_state_type = {
9568031Sbrian	&ng_parse_struct_type,
9697685Sarchie	&ng_pppoe_sts_type_fields
9768031Sbrian};
9868031Sbrian
9968031Sbrian/* List of commands and how to convert arguments to/from ASCII */
10068031Sbrianstatic const struct ng_cmdlist ng_pppoe_cmds[] = {
10168031Sbrian	{
10268031Sbrian	  NGM_PPPOE_COOKIE,
10368031Sbrian	  NGM_PPPOE_CONNECT,
10468031Sbrian	  "pppoe_connect",
10568845Sbrian	  &ngpppoe_init_data_state_type,
10668031Sbrian	  NULL
10768031Sbrian	},
10868031Sbrian	{
10968031Sbrian	  NGM_PPPOE_COOKIE,
11068031Sbrian	  NGM_PPPOE_LISTEN,
11168031Sbrian	  "pppoe_listen",
11268845Sbrian	  &ngpppoe_init_data_state_type,
11368031Sbrian	  NULL
11468031Sbrian	},
11568031Sbrian	{
11668031Sbrian	  NGM_PPPOE_COOKIE,
11768031Sbrian	  NGM_PPPOE_OFFER,
11868031Sbrian	  "pppoe_offer",
11968845Sbrian	  &ngpppoe_init_data_state_type,
12068031Sbrian	  NULL
12168031Sbrian	},
12268031Sbrian	{
12368031Sbrian	  NGM_PPPOE_COOKIE,
12469922Sjulian	  NGM_PPPOE_SERVICE,
12569922Sjulian	  "pppoe_service",
12669922Sjulian	  &ngpppoe_init_data_state_type,
12769922Sjulian	  NULL
12869922Sjulian	},
12969922Sjulian	{
13069922Sjulian	  NGM_PPPOE_COOKIE,
13168031Sbrian	  NGM_PPPOE_SUCCESS,
13268031Sbrian	  "pppoe_success",
13368031Sbrian	  &ng_pppoe_sts_state_type,
13468031Sbrian	  NULL
13568031Sbrian	},
13668031Sbrian	{
13768031Sbrian	  NGM_PPPOE_COOKIE,
13868031Sbrian	  NGM_PPPOE_FAIL,
13968031Sbrian	  "pppoe_fail",
14068031Sbrian	  &ng_pppoe_sts_state_type,
14168031Sbrian	  NULL
14268031Sbrian	},
14368031Sbrian	{
14468031Sbrian	  NGM_PPPOE_COOKIE,
14568031Sbrian	  NGM_PPPOE_CLOSE,
14668031Sbrian	  "pppoe_close",
14768031Sbrian	  &ng_pppoe_sts_state_type,
14868031Sbrian	  NULL
14968031Sbrian	},
150132703Sglebius	{
151132703Sglebius	  NGM_PPPOE_COOKIE,
152132703Sglebius	  NGM_PPPOE_SETMODE,
153132703Sglebius	  "pppoe_setmode",
154132703Sglebius	  &ng_parse_string_type,
155132703Sglebius	  NULL
156132703Sglebius	},
157132703Sglebius	{
158132703Sglebius	  NGM_PPPOE_COOKIE,
159132703Sglebius	  NGM_PPPOE_GETMODE,
160132703Sglebius	  "pppoe_getmode",
161132703Sglebius	  NULL,
162132703Sglebius	  &ng_parse_string_type
163132703Sglebius	},
164161117Sglebius	{
165161117Sglebius	  NGM_PPPOE_COOKIE,
166161117Sglebius	  NGM_PPPOE_SETENADDR,
167161117Sglebius	  "setenaddr",
168161117Sglebius	  &ng_parse_enaddr_type,
169161117Sglebius	  NULL
170161117Sglebius	},
17168031Sbrian	{ 0 }
17268031Sbrian};
17368031Sbrian
17452419Sjulian/* Netgraph node type descriptor */
17552419Sjulianstatic struct ng_type typestruct = {
176129823Sjulian	.version =	NG_ABI_VERSION,
177129823Sjulian	.name =		NG_PPPOE_NODE_TYPE,
178129823Sjulian	.constructor =	ng_pppoe_constructor,
179129823Sjulian	.rcvmsg =	ng_pppoe_rcvmsg,
180129823Sjulian	.shutdown =	ng_pppoe_shutdown,
181129823Sjulian	.newhook =	ng_pppoe_newhook,
182161117Sglebius	.connect =	ng_pppoe_connect,
183129823Sjulian	.rcvdata =	ng_pppoe_rcvdata,
184129823Sjulian	.disconnect =	ng_pppoe_disconnect,
185129823Sjulian	.cmdlist =	ng_pppoe_cmds,
18652419Sjulian};
18752562SjulianNETGRAPH_INIT(pppoe, &typestruct);
18852419Sjulian
18952419Sjulian/*
19052419Sjulian * States for the session state machine.
19152419Sjulian * These have no meaning if there is no hook attached yet.
19252419Sjulian */
19352419Sjulianenum state {
19452419Sjulian    PPPOE_SNONE=0,	/* [both] Initial state */
19553498Sjulian    PPPOE_LISTENING,	/* [Daemon] Listening for discover initiation pkt */
19652419Sjulian    PPPOE_SINIT,	/* [Client] Sent discovery initiation */
19753498Sjulian    PPPOE_PRIMED,	/* [Server] Awaiting PADI from daemon */
19853498Sjulian    PPPOE_SOFFER,	/* [Server] Sent offer message  (got PADI)*/
19952419Sjulian    PPPOE_SREQ,		/* [Client] Sent a Request */
20053498Sjulian    PPPOE_NEWCONNECTED,	/* [Server] Connection established, No data received */
20152419Sjulian    PPPOE_CONNECTED,	/* [Both] Connection established, Data received */
20252419Sjulian    PPPOE_DEAD		/* [Both] */
20352419Sjulian};
20452419Sjulian
20552419Sjulian#define NUMTAGS 20 /* number of tags we are set up to work with */
20652419Sjulian
20752419Sjulian/*
208154604Sglebius * Information we store for each hook on each node for negotiating the
20952419Sjulian * session. The mbuf and cluster are freed once negotiation has completed.
21052419Sjulian * The whole negotiation block is then discarded.
21152419Sjulian */
21252419Sjulian
21352419Sjulianstruct sess_neg {
21452419Sjulian	struct mbuf 		*m; /* holds cluster with last sent packet */
21552419Sjulian	union	packet		*pkt; /* points within the above cluster */
216138562Sglebius	struct callout		handle;   /* see timeout(9) */
21752419Sjulian	u_int			timeout; /* 0,1,2,4,8,16 etc. seconds */
21852419Sjulian	u_int			numtags;
21997897Sarchie	const struct pppoe_tag	*tags[NUMTAGS];
22052419Sjulian	u_int			service_len;
22152419Sjulian	u_int			ac_name_len;
22252419Sjulian
22352419Sjulian	struct datatag		service;
22452419Sjulian	struct datatag		ac_name;
22552419Sjulian};
22652419Sjuliantypedef struct sess_neg *negp;
22752419Sjulian
22852419Sjulian/*
22952419Sjulian * Session information that is needed after connection.
23052419Sjulian */
23166052Sarchiestruct sess_con {
23252419Sjulian	hook_p  		hook;
233154604Sglebius	uint16_t		Session_ID;
23452419Sjulian	enum state		state;
235154604Sglebius	ng_ID_t			creator;	/* who to notify */
23652419Sjulian	struct pppoe_full_hdr	pkt_hdr;	/* used when connected */
23752419Sjulian	negp			neg;		/* used when negotiating */
238175865Smav	TAILQ_ENTRY(sess_con)	sessions;
23952419Sjulian};
24066052Sarchietypedef struct sess_con *sessp;
24152419Sjulian
242175865Smav#define SESSHASHSIZE	0x0100
243175865Smav#define SESSHASH(x)	(((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
244175865Smav
245175865Smavstruct sess_hash_entry {
246175865Smav	struct mtx	mtx;
247175865Smav	TAILQ_HEAD(hhead, sess_con) head;
248175865Smav};
249175865Smav
25052419Sjulian/*
25152419Sjulian * Information we store for each node
25252419Sjulian */
253154604Sglebiusstruct PPPoE {
25452419Sjulian	node_p		node;		/* back pointer to node */
25552419Sjulian	hook_p  	ethernet_hook;
25652419Sjulian	hook_p  	debug_hook;
25752419Sjulian	u_int   	packets_in;	/* packets in from ethernet */
25852419Sjulian	u_int   	packets_out;	/* packets out towards ethernet */
259154604Sglebius	uint32_t	flags;
260154901Sglebius#define	COMPAT_3COM	0x00000001
261154901Sglebius#define	COMPAT_DLINK	0x00000002
262161117Sglebius	struct ether_header	eh;
263175865Smav	struct sess_hash_entry	sesshash[SESSHASHSIZE];
26452419Sjulian};
265154604Sglebiustypedef struct PPPoE *priv_p;
26652419Sjulian
26752419Sjulianunion uniq {
26852419Sjulian	char bytes[sizeof(void *)];
269154604Sglebius	void *pointer;
270154604Sglebius};
27152419Sjulian
27252419Sjulian#define	LEAVE(x) do { error = x; goto quit; } while(0)
27352419Sjulianstatic void	pppoe_start(sessp sp);
274138562Sglebiusstatic void	pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
27597897Sarchiestatic const	struct pppoe_tag *scan_tags(sessp sp,
27697897Sarchie			const struct pppoe_hdr* ph);
27752441Sjulianstatic	int	pppoe_send_event(sessp sp, enum cmd cmdid);
27852419Sjulian
27952419Sjulian/*************************************************************************
28052419Sjulian * Some basic utilities  from the Linux version with author's permission.*
28152419Sjulian * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
28252419Sjulian ************************************************************************/
28352419Sjulian
28452419Sjulian/*
28552419Sjulian * Generate a new session id
28653145Sjulian * XXX find out the FreeBSD locking scheme.
28752419Sjulian */
288154604Sglebiusstatic uint16_t
28952419Sjulianget_new_sid(node_p node)
29052419Sjulian{
29152419Sjulian	static int pppoe_sid = 10;
29252419Sjulian	hook_p	hook;
293154604Sglebius	uint16_t val;
29452419Sjulian
29552419Sjulianrestart:
29652419Sjulian	val = pppoe_sid++;
29752419Sjulian	/*
29852419Sjulian	 * Spec says 0xFFFF is reserved.
29952419Sjulian	 * Also don't use 0x0000
30052419Sjulian	 */
30152419Sjulian	if (val == 0xffff) {
30252419Sjulian		pppoe_sid = 20;
30352419Sjulian		goto restart;
30452419Sjulian	}
30552419Sjulian
306154604Sglebius	/* Check it isn't already in use. */
30770784Sjulian	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
308172270Smav		sessp sp = NG_HOOK_PRIVATE(hook);
309172270Smav
310172270Smav		/* Skip any nonsession hook. */
311172270Smav		if (sp == NULL)
31252419Sjulian			continue;
31352419Sjulian		if (sp->Session_ID == val)
31452419Sjulian			goto restart;
31552419Sjulian	}
31652419Sjulian
317154604Sglebius	CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
318154604Sglebius
319154604Sglebius	return (val);
32052419Sjulian}
32152419Sjulian
32252419Sjulian
32352419Sjulian/*
324154604Sglebius * Return the location where the next tag can be put
32552419Sjulian */
32697897Sarchiestatic __inline const struct pppoe_tag*
32797897Sarchienext_tag(const struct pppoe_hdr* ph)
32852419Sjulian{
32997897Sarchie	return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
33097897Sarchie	    + ntohs(ph->length));
33152419Sjulian}
33252419Sjulian
33352419Sjulian/*
334154604Sglebius * Look for a tag of a specific type.
335154604Sglebius * Don't trust any length the other end says,
33652419Sjulian * but assume we already sanity checked ph->length.
33752419Sjulian */
33897897Sarchiestatic const struct pppoe_tag*
339154604Sglebiusget_tag(const struct pppoe_hdr* ph, uint16_t idx)
34052419Sjulian{
34197897Sarchie	const char *const end = (const char *)next_tag(ph);
342154604Sglebius	const struct pppoe_tag *pt = &ph->tag[0];
34397897Sarchie	const char *ptn;
344154604Sglebius
34552419Sjulian	/*
34652419Sjulian	 * Keep processing tags while a tag header will still fit.
34752419Sjulian	 */
34897897Sarchie	while((const char*)(pt + 1) <= end) {
349154604Sglebius		/*
350154604Sglebius		 * If the tag data would go past the end of the packet, abort.
351154604Sglebius		 */
352154604Sglebius		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
353154604Sglebius		if (ptn > end) {
354154604Sglebius			CTR2(KTR_NET, "%20s: invalid length for tag %d",
355154604Sglebius			    __func__, idx);
356154604Sglebius			return (NULL);
357154604Sglebius		}
358154604Sglebius		if (pt->tag_type == idx) {
359154604Sglebius			CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
360154604Sglebius			return (pt);
361154604Sglebius		}
36252419Sjulian
363154604Sglebius		pt = (const struct pppoe_tag*)ptn;
364154604Sglebius	}
36552419Sjulian
366154604Sglebius	CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
367154604Sglebius	return (NULL);
36852419Sjulian}
36952419Sjulian
37052419Sjulian/**************************************************************************
371154604Sglebius * Inlines to initialise or add tags to a session's tag list.
37252419Sjulian **************************************************************************/
37352419Sjulian/*
374154604Sglebius * Initialise the session's tag list.
37552419Sjulian */
37652419Sjulianstatic void
37752419Sjulianinit_tags(sessp sp)
37852419Sjulian{
379154604Sglebius	KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
38052419Sjulian	sp->neg->numtags = 0;
38152419Sjulian}
38252419Sjulian
38352419Sjulianstatic void
38497897Sarchieinsert_tag(sessp sp, const struct pppoe_tag *tp)
38552419Sjulian{
386154604Sglebius	negp neg = sp->neg;
387154604Sglebius	int i;
38852419Sjulian
389154604Sglebius	KASSERT(neg != NULL, ("%s: no neg", __func__));
39052419Sjulian	if ((i = neg->numtags++) < NUMTAGS) {
39152419Sjulian		neg->tags[i] = tp;
39252419Sjulian	} else {
393161034Sglebius		log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
394161034Sglebius		    "packet\n");
39553042Sjulian		neg->numtags--;
39652419Sjulian	}
39752419Sjulian}
39852419Sjulian
39952419Sjulian/*
40052419Sjulian * Make up a packet, using the tags filled out for the session.
40152419Sjulian *
402154604Sglebius * Assume that the actual pppoe header and ethernet header
40352419Sjulian * are filled out externally to this routine.
404154604Sglebius * Also assume that neg->wh points to the correct
40552419Sjulian * location at the front of the buffer space.
40652419Sjulian */
40752419Sjulianstatic void
40852419Sjulianmake_packet(sessp sp) {
40952419Sjulian	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
41097897Sarchie	const struct pppoe_tag **tag;
41152419Sjulian	char *dp;
41252419Sjulian	int count;
41352419Sjulian	int tlen;
414154604Sglebius	uint16_t length = 0;
41552419Sjulian
416154604Sglebius	KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
417161034Sglebius	    ("%s: called from wrong state", __func__));
418154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
419154604Sglebius
42052419Sjulian	dp = (char *)wh->ph.tag;
42152419Sjulian	for (count = 0, tag = sp->neg->tags;
422154604Sglebius	    ((count < sp->neg->numtags) && (count < NUMTAGS));
42352419Sjulian	    tag++, count++) {
42452419Sjulian		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
42552419Sjulian		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
426161034Sglebius			log(LOG_NOTICE, "ng_pppoe: tags too long\n");
42752419Sjulian			sp->neg->numtags = count;
42852419Sjulian			break;	/* XXX chop off what's too long */
42952419Sjulian		}
43097897Sarchie		bcopy(*tag, (char *)dp, tlen);
43152419Sjulian		length += tlen;
43252419Sjulian		dp += tlen;
43352419Sjulian	}
43452419Sjulian 	wh->ph.length = htons(length);
43552419Sjulian	sp->neg->m->m_len = length + sizeof(*wh);
43652419Sjulian	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
43752419Sjulian}
43852419Sjulian
43952419Sjulian/**************************************************************************
440154862Sglebius * Routines to match a service.						  *
44152419Sjulian **************************************************************************/
442154862Sglebius
443154604Sglebius/*
44452419Sjulian * Find a hook that has a service string that matches that
445154862Sglebius * we are seeking. For now use a simple string.
44652419Sjulian * In the future we may need something like regexp().
447154862Sglebius *
448154862Sglebius * Null string is a wildcard (ANY service), according to RFC2516.
449154862Sglebius * And historical FreeBSD wildcard is also "*".
45052419Sjulian */
45180311Sbrian
45252419Sjulianstatic hook_p
453154862Sglebiuspppoe_match_svc(node_p node, const struct pppoe_tag *tag)
45452419Sjulian{
455154862Sglebius	hook_p hook;
45652419Sjulian
45770784Sjulian	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
458154862Sglebius		sessp sp = NG_HOOK_PRIVATE(hook);
459154862Sglebius		negp neg;
46052419Sjulian
461172270Smav		/* Skip any nonsession hook. */
462172270Smav		if (sp == NULL)
46352419Sjulian			continue;
46452419Sjulian
46552419Sjulian		/* Skip any sessions which are not in LISTEN mode. */
466154862Sglebius		if (sp->state != PPPOE_LISTENING)
46752419Sjulian			continue;
46852419Sjulian
46952419Sjulian		neg = sp->neg;
47052419Sjulian
471154862Sglebius		/* Empty Service-Name matches any service. */
472154862Sglebius		if (neg->service_len == 0)
473154862Sglebius			break;
47452419Sjulian
475154862Sglebius		/* Special case for a blank or "*" service name (wildcard). */
476154862Sglebius		if (neg->service_len == 1 && neg->service.data[0] == '*')
477154862Sglebius			break;
478154862Sglebius
47952419Sjulian		/* If the lengths don't match, that aint it. */
480154862Sglebius		if (neg->service_len != ntohs(tag->tag_len))
48152419Sjulian			continue;
48252419Sjulian
483154862Sglebius		if (strncmp(tag->tag_data, neg->service.data,
484154862Sglebius		    ntohs(tag->tag_len)) == 0)
48580311Sbrian			break;
486154862Sglebius	}
487154862Sglebius	CTR3(KTR_NET, "%20s: matched %p for %s", __func__, hook, tag->tag_data);
48880311Sbrian
489154862Sglebius	return (hook);
490154862Sglebius}
491154862Sglebius
492154862Sglebius/*
493154862Sglebius * Broadcast the PADI packet in m0 to all listening hooks.
494154862Sglebius * This routine is called when a PADI with empty Service-Name
495154862Sglebius * tag is received. Client should receive PADOs with all
496154862Sglebius * available services.
497154862Sglebius */
498154862Sglebiusstatic int
499154862Sglebiuspppoe_broadcast_padi(node_p node, struct mbuf *m0)
500154862Sglebius{
501154862Sglebius	hook_p hook;
502154862Sglebius	int error = 0;
503154862Sglebius
504154862Sglebius	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
505154862Sglebius		sessp sp = NG_HOOK_PRIVATE(hook);
506154862Sglebius		struct mbuf *m;
507154862Sglebius
508154862Sglebius		/*
509154862Sglebius		 * Go through all listening hooks and
510154862Sglebius		 * broadcast the PADI packet up there
511154862Sglebius		 */
512172270Smav		if (sp == NULL)
513154862Sglebius			continue;
514154862Sglebius
515154862Sglebius		if (sp->state != PPPOE_LISTENING)
516154862Sglebius			continue;
517154862Sglebius
518154862Sglebius		m = m_dup(m0, M_DONTWAIT);
519154862Sglebius		if (m == NULL)
520154862Sglebius			return (ENOMEM);
521154862Sglebius		NG_SEND_DATA_ONLY(error, hook, m);
522154862Sglebius		if (error)
523154862Sglebius			return (error);
52452419Sjulian	}
525154604Sglebius
526154862Sglebius	return (0);
52752419Sjulian}
528154604Sglebius
529154862Sglebius/*
530154862Sglebius * Find a hook, which name equals to given service.
531154862Sglebius */
532154862Sglebiusstatic hook_p
533154862Sglebiuspppoe_find_svc(node_p node, const char *svc_name, int svc_len)
534154862Sglebius{
535154862Sglebius	hook_p	hook;
536154862Sglebius
537154862Sglebius	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
538154862Sglebius		sessp sp = NG_HOOK_PRIVATE(hook);
539154862Sglebius		negp neg;
540154862Sglebius
541172270Smav		/* Skip any nonsession hook. */
542172270Smav		if (sp == NULL)
543154862Sglebius			continue;
544154862Sglebius
545154862Sglebius		/* Skip any sessions which are not in LISTEN mode. */
546154862Sglebius		if (sp->state != PPPOE_LISTENING)
547154862Sglebius			continue;
548154862Sglebius
549154862Sglebius		neg = sp->neg;
550154862Sglebius
551154862Sglebius		if (neg->service_len == svc_len &&
552174931Smav		    strncmp(svc_name, neg->service.data, svc_len) == 0)
553154862Sglebius			return (hook);
554154862Sglebius	}
555154862Sglebius
556154862Sglebius	return (NULL);
557154862Sglebius}
558154862Sglebius
55952419Sjulian/**************************************************************************
560175865Smav * Routines to find a particular session that matches an incoming packet. *
56152419Sjulian **************************************************************************/
562175865Smav/* Add specified session to hash. */
563175865Smavstatic void
564175865Smavpppoe_addsession(sessp sp)
56552419Sjulian{
566175865Smav	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
567175865Smav	uint16_t	hash = SESSHASH(sp->Session_ID);
56852419Sjulian
569175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
570175865Smav	TAILQ_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
571175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
572175865Smav}
573172270Smav
574175865Smav/* Delete specified session from hash. */
575175865Smavstatic void
576175865Smavpppoe_delsession(sessp sp)
577175865Smav{
578175865Smav	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
579175865Smav	uint16_t	hash = SESSHASH(sp->Session_ID);
580175865Smav
581175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
582175865Smav	TAILQ_REMOVE(&privp->sesshash[hash].head, sp, sessions);
583175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
584175865Smav}
585175865Smav
586175865Smav/* Find matching peer/session combination. */
587175865Smavstatic sessp
588175865Smavpppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
589175865Smav{
590175865Smav	uint16_t 	session = ntohs(wh->ph.sid);
591175865Smav	uint16_t	hash = SESSHASH(session);
592175865Smav	sessp		sp = NULL;
593175865Smav
594175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
595175865Smav	TAILQ_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
596172270Smav		if (sp->Session_ID == session &&
597172270Smav		    bcmp(sp->pkt_hdr.eh.ether_dhost,
598172270Smav		     wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
59952419Sjulian			break;
60052419Sjulian		}
60152419Sjulian	}
602175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
603175867Smav	CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
604175867Smav	    session);
605154604Sglebius
606175865Smav	return (sp);
60752419Sjulian}
60852419Sjulian
60952419Sjulianstatic hook_p
61097897Sarchiepppoe_finduniq(node_p node, const struct pppoe_tag *tag)
61152419Sjulian{
612154604Sglebius	hook_p	hook = NULL;
613154604Sglebius	union uniq uniq;
61452419Sjulian
61552419Sjulian	bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
616154604Sglebius	/* Cycle through all known hooks. */
61770784Sjulian	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
618172270Smav		/* Skip any nonsession hook. */
619172270Smav		if (NG_HOOK_PRIVATE(hook) == NULL)
62052419Sjulian			continue;
62170784Sjulian		if (uniq.pointer == NG_HOOK_PRIVATE(hook))
62252419Sjulian			break;
62352419Sjulian	}
624154604Sglebius	CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
625154604Sglebius
62652419Sjulian	return (hook);
62752419Sjulian}
62852419Sjulian
62952419Sjulian/**************************************************************************
630154604Sglebius * Start of Netgraph entrypoints.					  *
63152419Sjulian **************************************************************************/
63252419Sjulian
63352419Sjulian/*
634154604Sglebius * Allocate the private data structure and link it with node.
63552419Sjulian */
63652419Sjulianstatic int
63770700Sjulianng_pppoe_constructor(node_p node)
63852419Sjulian{
639175865Smav	priv_p	privp;
640175865Smav	int	i;
64152419Sjulian
642154604Sglebius	/* Initialize private descriptor. */
643154604Sglebius	privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
644154604Sglebius	if (privp == NULL)
64552419Sjulian		return (ENOMEM);
64652419Sjulian
647154604Sglebius	/* Link structs together; this counts as our one reference to *node. */
648154604Sglebius	NG_NODE_SET_PRIVATE(node, privp);
649154604Sglebius	privp->node = node;
650132703Sglebius
651154901Sglebius	/* Initialize to standard mode. */
652161117Sglebius	memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
653161117Sglebius	privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
654154604Sglebius
655175865Smav	for (i = 0; i < SESSHASHSIZE; i++) {
656175865Smav	    mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
657175865Smav	    TAILQ_INIT(&privp->sesshash[i].head);
658175865Smav	}
659175865Smav
660154604Sglebius	CTR3(KTR_NET, "%20s: created node [%x] (%p)",
661154604Sglebius	    __func__, node->nd_ID, node);
662154604Sglebius
66352419Sjulian	return (0);
66452419Sjulian}
66552419Sjulian
66652419Sjulian/*
66752419Sjulian * Give our ok for a hook to be added...
66852419Sjulian * point the hook's private info to the hook structure.
66952419Sjulian *
67052419Sjulian * The following hook names are special:
671154604Sglebius *  "ethernet":  the hook that should be connected to a NIC.
672154604Sglebius *  "debug":	copies of data sent out here  (when I write the code).
67369922Sjulian * All other hook names need only be unique. (the framework checks this).
67452419Sjulian */
67552419Sjulianstatic int
67652562Sjulianng_pppoe_newhook(node_p node, hook_p hook, const char *name)
67752419Sjulian{
67870784Sjulian	const priv_p privp = NG_NODE_PRIVATE(node);
67952419Sjulian	sessp sp;
68052419Sjulian
68152419Sjulian	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
68252419Sjulian		privp->ethernet_hook = hook;
683172629Smav		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
68452419Sjulian	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
68552419Sjulian		privp->debug_hook = hook;
686172629Smav		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
68752419Sjulian	} else {
68852419Sjulian		/*
68952419Sjulian		 * Any other unique name is OK.
69052419Sjulian		 * The infrastructure has already checked that it's unique,
69152419Sjulian		 * so just allocate it and hook it in.
69252419Sjulian		 */
693154604Sglebius		sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
694154604Sglebius		if (sp == NULL)
695154604Sglebius			return (ENOMEM);
69652419Sjulian
69770784Sjulian		NG_HOOK_SET_PRIVATE(hook, sp);
69852419Sjulian		sp->hook = hook;
69952419Sjulian	}
700154604Sglebius	CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
701154604Sglebius	    __func__, node->nd_ID, node, name, hook);
702154604Sglebius
70352419Sjulian	return(0);
70452419Sjulian}
70552419Sjulian
70652419Sjulian/*
707161117Sglebius * Hook has been added successfully. Request the MAC address of
708161117Sglebius * the underlying Ethernet node.
709161117Sglebius */
710161117Sglebiusstatic int
711161117Sglebiusng_pppoe_connect(hook_p hook)
712161117Sglebius{
713161117Sglebius	const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
714161117Sglebius	struct ng_mesg *msg;
715161181Sglebius	int error;
716161117Sglebius
717161117Sglebius	if (hook != privp->ethernet_hook)
718161117Sglebius		return (0);
719161117Sglebius
720161117Sglebius	/*
721161117Sglebius	 * If this is Ethernet hook, then request MAC address
722161117Sglebius	 * from our downstream.
723161117Sglebius	 */
724161117Sglebius	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
725161117Sglebius	if (msg == NULL)
726161117Sglebius		return (ENOBUFS);
727161117Sglebius
728161117Sglebius	/*
729161117Sglebius	 * Our hook and peer hook have HK_INVALID flag set,
730161117Sglebius	 * so we can't use NG_SEND_MSG_HOOK() macro here.
731161117Sglebius	 */
732161117Sglebius	NG_SEND_MSG_ID(error, privp->node, msg,
733161117Sglebius	    NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
734161117Sglebius	    NG_NODE_ID(privp->node));
735161117Sglebius
736161117Sglebius	return (error);
737161117Sglebius}
738161117Sglebius/*
73952419Sjulian * Get a netgraph control message.
74052419Sjulian * Check it is one we understand. If needed, send a response.
74152419Sjulian * We sometimes save the address for an async action later.
74252419Sjulian * Always free the message.
74352419Sjulian */
74452419Sjulianstatic int
74570700Sjulianng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
74652419Sjulian{
74770784Sjulian	priv_p privp = NG_NODE_PRIVATE(node);
74852562Sjulian	struct ngpppoe_init_data *ourmsg = NULL;
74952419Sjulian	struct ng_mesg *resp = NULL;
75052419Sjulian	int error = 0;
75152419Sjulian	hook_p hook = NULL;
75252419Sjulian	sessp sp = NULL;
75352419Sjulian	negp neg = NULL;
75470700Sjulian	struct ng_mesg *msg;
75552419Sjulian
75670700Sjulian	NGI_GET_MSG(item, msg);
757154604Sglebius	CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
758154604Sglebius	    __func__, node->nd_ID, node, msg->header.cmd,
759154604Sglebius	    msg->header.typecookie);
760154604Sglebius
761154604Sglebius	/* Deal with message according to cookie and command. */
76252419Sjulian	switch (msg->header.typecookie) {
763154604Sglebius	case NGM_PPPOE_COOKIE:
76452419Sjulian		switch (msg->header.cmd) {
76552419Sjulian		case NGM_PPPOE_CONNECT:
766154604Sglebius		case NGM_PPPOE_LISTEN:
767154604Sglebius		case NGM_PPPOE_OFFER:
768154604Sglebius		case NGM_PPPOE_SERVICE:
76968845Sbrian			ourmsg = (struct ngpppoe_init_data *)msg->data;
77068845Sbrian			if (msg->header.arglen < sizeof(*ourmsg)) {
771161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: init data too "
772161034Sglebius				    "small\n", node->nd_ID);
77368845Sbrian				LEAVE(EMSGSIZE);
77468031Sbrian			}
775154604Sglebius			if (msg->header.arglen - sizeof(*ourmsg) >
77668031Sbrian			    PPPOE_SERVICE_NAME_SIZE) {
777161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: service name "
778161034Sglebius				    "too big\n", node->nd_ID);
77952419Sjulian				LEAVE(EMSGSIZE);
78052419Sjulian			}
78168845Sbrian			if (msg->header.arglen - sizeof(*ourmsg) <
78268845Sbrian			    ourmsg->data_len) {
783161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
784161034Sglebius				    "length, %d should be %zd\n", node->nd_ID,
785161034Sglebius				    ourmsg->data_len,
78668845Sbrian				    msg->header.arglen - sizeof (*ourmsg));
78752419Sjulian				LEAVE(EMSGSIZE);
78852419Sjulian			}
78968031Sbrian
790154604Sglebius			/* Make sure strcmp will terminate safely. */
79152419Sjulian			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
79252419Sjulian
793154604Sglebius			/* Cycle through all known hooks. */
79470784Sjulian			LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
795154604Sglebius				if (NG_HOOK_NAME(hook) &&
796154604Sglebius				    strcmp(NG_HOOK_NAME(hook), ourmsg->hook) ==
797154604Sglebius				    0)
79852419Sjulian					break;
79952419Sjulian			}
800154604Sglebius			if (hook == NULL)
80152419Sjulian				LEAVE(ENOENT);
802154604Sglebius
803172270Smav			sp = NG_HOOK_PRIVATE(hook);
804172270Smav
805172270Smav			if (sp == NULL)
80652419Sjulian				LEAVE(EINVAL);
807154604Sglebius
80880311Sbrian			if (msg->header.cmd == NGM_PPPOE_LISTEN) {
80980311Sbrian				/*
81080311Sbrian				 * Ensure we aren't already listening for this
81180311Sbrian				 * service.
81280311Sbrian				 */
813154862Sglebius				if (pppoe_find_svc(node, ourmsg->data,
814154862Sglebius				    ourmsg->data_len) != NULL)
81580311Sbrian					LEAVE(EEXIST);
81680311Sbrian			}
81780311Sbrian
81869922Sjulian			/*
81969922Sjulian			 * PPPOE_SERVICE advertisments are set up
82069922Sjulian			 * on sessions that are in PRIMED state.
82169922Sjulian			 */
822154604Sglebius			if (msg->header.cmd == NGM_PPPOE_SERVICE)
82369922Sjulian				break;
824154604Sglebius
825150319Sglebius			if (sp->state != PPPOE_SNONE) {
826161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
827161034Sglebius				    "active\n", node->nd_ID);
82852419Sjulian				LEAVE(EISCONN);
82952419Sjulian			}
83052443Sjulian
83152419Sjulian			/*
832154604Sglebius			 * Set up prototype header.
83352419Sjulian			 */
834154604Sglebius			neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
83568876Sdwmalone			    M_NOWAIT | M_ZERO);
83652419Sjulian
837154604Sglebius			if (neg == NULL)
83852419Sjulian				LEAVE(ENOMEM);
839154604Sglebius
840154604Sglebius			neg->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
841154604Sglebius			if (neg->m == NULL) {
842154604Sglebius				free(neg, M_NETGRAPH_PPPOE);
84352419Sjulian				LEAVE(ENOBUFS);
84452419Sjulian			}
84552419Sjulian			neg->m->m_pkthdr.rcvif = NULL;
84652419Sjulian			sp->neg = neg;
847138562Sglebius			ng_callout_init(&neg->handle);
84852419Sjulian			neg->m->m_len = sizeof(struct pppoe_full_hdr);
84952419Sjulian			neg->pkt = mtod(neg->m, union packet*);
850132703Sglebius			memcpy((void *)&neg->pkt->pkt_header.eh,
851161117Sglebius			    &privp->eh, sizeof(struct ether_header));
85252419Sjulian			neg->pkt->pkt_header.ph.ver = 0x1;
85352419Sjulian			neg->pkt->pkt_header.ph.type = 0x1;
85452419Sjulian			neg->pkt->pkt_header.ph.sid = 0x0000;
85552419Sjulian			neg->timeout = 0;
85652419Sjulian
85770700Sjulian			sp->creator = NGI_RETADDR(item);
85852419Sjulian		}
85952419Sjulian		switch (msg->header.cmd) {
86052419Sjulian		case NGM_PPPOE_GET_STATUS:
86152419Sjulian		    {
86252562Sjulian			struct ngpppoestat *stats;
86352419Sjulian
86452419Sjulian			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
865154604Sglebius			if (!resp)
86652419Sjulian				LEAVE(ENOMEM);
867154604Sglebius
86852562Sjulian			stats = (struct ngpppoestat *) resp->data;
86952419Sjulian			stats->packets_in = privp->packets_in;
87052419Sjulian			stats->packets_out = privp->packets_out;
87152419Sjulian			break;
87252419Sjulian		    }
87352419Sjulian		case NGM_PPPOE_CONNECT:
874174981Smav		    {
87552419Sjulian			/*
87652419Sjulian			 * Check the hook exists and is Uninitialised.
87752419Sjulian			 * Send a PADI request, and start the timeout logic.
87852419Sjulian			 * Store the originator of this message so we can send
87952419Sjulian			 * a success of fail message to them later.
880154604Sglebius			 * Move the session to SINIT.
88152419Sjulian			 * Set up the session to the correct state and
88252419Sjulian			 * start it.
88352419Sjulian			 */
884174981Smav			int	i, acnlen = 0, acnsep = 0, srvlen;
885174981Smav			for (i = 0; i < ourmsg->data_len; i++) {
886174981Smav				if (ourmsg->data[i] == '\\') {
887174981Smav					acnlen = i;
888174981Smav					acnsep = 1;
889174981Smav					break;
890174981Smav				}
891174981Smav			}
892174981Smav			srvlen = ourmsg->data_len - acnlen - acnsep;
893174981Smav
894174981Smav			bcopy(ourmsg->data, neg->ac_name.data, acnlen);
895174981Smav			neg->ac_name_len = acnlen;
896174981Smav
89752419Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
898174981Smav			neg->service.hdr.tag_len = htons((uint16_t)srvlen);
899174981Smav			bcopy(ourmsg->data + acnlen + acnsep,
900174981Smav			    neg->service.data, srvlen);
901174981Smav			neg->service_len = srvlen;
90252419Sjulian			pppoe_start(sp);
90352419Sjulian			break;
904174981Smav		    }
90552419Sjulian		case NGM_PPPOE_LISTEN:
90652419Sjulian			/*
90752419Sjulian			 * Check the hook exists and is Uninitialised.
90852419Sjulian			 * Install the service matching string.
90952419Sjulian			 * Store the originator of this message so we can send
91052419Sjulian			 * a success of fail message to them later.
91152419Sjulian			 * Move the hook to 'LISTENING'
91252419Sjulian			 */
91352419Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
91468845Sbrian			neg->service.hdr.tag_len =
915154604Sglebius			    htons((uint16_t)ourmsg->data_len);
91652443Sjulian
91768845Sbrian			if (ourmsg->data_len)
91868845Sbrian				bcopy(ourmsg->data, neg->service.data,
91968845Sbrian				    ourmsg->data_len);
92068845Sbrian			neg->service_len = ourmsg->data_len;
92152419Sjulian			neg->pkt->pkt_header.ph.code = PADT_CODE;
92252419Sjulian			/*
923154604Sglebius			 * Wait for PADI packet coming from Ethernet.
92452419Sjulian			 */
92552419Sjulian			sp->state = PPPOE_LISTENING;
92652419Sjulian			break;
92752419Sjulian		case NGM_PPPOE_OFFER:
92852419Sjulian			/*
92952419Sjulian			 * Check the hook exists and is Uninitialised.
93052419Sjulian			 * Store the originator of this message so we can send
93152419Sjulian			 * a success of fail message to them later.
93252419Sjulian			 * Store the AC-Name given and go to PRIMED.
93352419Sjulian			 */
93452419Sjulian			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
93568845Sbrian			neg->ac_name.hdr.tag_len =
936154604Sglebius			    htons((uint16_t)ourmsg->data_len);
93768845Sbrian			if (ourmsg->data_len)
93868845Sbrian				bcopy(ourmsg->data, neg->ac_name.data,
93968845Sbrian				    ourmsg->data_len);
94068845Sbrian			neg->ac_name_len = ourmsg->data_len;
94152419Sjulian			neg->pkt->pkt_header.ph.code = PADO_CODE;
94252419Sjulian			/*
943154604Sglebius			 * Wait for PADI packet coming from hook.
94452419Sjulian			 */
94552419Sjulian			sp->state = PPPOE_PRIMED;
94652419Sjulian			break;
947154604Sglebius		case NGM_PPPOE_SERVICE:
948154604Sglebius			/*
94969922Sjulian			 * Check the session is primed.
95069922Sjulian			 * for now just allow ONE service to be advertised.
95169922Sjulian			 * If you do it twice you just overwrite.
95269922Sjulian			 */
95370148Sjulian			if (sp->state != PPPOE_PRIMED) {
954161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: session not "
955161034Sglebius				    "primed\n", node->nd_ID);
95669922Sjulian				LEAVE(EISCONN);
95769922Sjulian			}
95870931Sjulian			neg = sp->neg;
95969922Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
96069922Sjulian			neg->service.hdr.tag_len =
961154604Sglebius			    htons((uint16_t)ourmsg->data_len);
96269922Sjulian
96369922Sjulian			if (ourmsg->data_len)
96469922Sjulian				bcopy(ourmsg->data, neg->service.data,
96569922Sjulian				    ourmsg->data_len);
96669922Sjulian			neg->service_len = ourmsg->data_len;
96769922Sjulian			break;
968132703Sglebius		case NGM_PPPOE_SETMODE:
969132703Sglebius		    {
970132703Sglebius			char *s;
971132703Sglebius			size_t len;
972132703Sglebius
973132703Sglebius			if (msg->header.arglen == 0)
974132703Sglebius				LEAVE(EINVAL);
975132703Sglebius
976132703Sglebius			s = (char *)msg->data;
977132703Sglebius			len = msg->header.arglen - 1;
978132703Sglebius
979154604Sglebius			/* Search for matching mode string. */
980154901Sglebius			if (len == strlen(NG_PPPOE_STANDARD) &&
981154901Sglebius			    (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
982154901Sglebius				privp->flags = 0;
983161117Sglebius				privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
984154901Sglebius				break;
985154901Sglebius			}
986154901Sglebius			if (len == strlen(NG_PPPOE_3COM) &&
987154901Sglebius			    (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
988154901Sglebius				privp->flags |= COMPAT_3COM;
989161117Sglebius				privp->eh.ether_type =
990161117Sglebius				    ETHERTYPE_PPPOE_3COM_DISC;
991154901Sglebius				break;
992154901Sglebius			}
993154901Sglebius			if (len == strlen(NG_PPPOE_DLINK) &&
994154901Sglebius			    (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
995154901Sglebius				privp->flags |= COMPAT_DLINK;
996154901Sglebius				break;
997154901Sglebius			}
998154901Sglebius			error = EINVAL;
999132703Sglebius			break;
1000132703Sglebius		    }
1001132703Sglebius		case NGM_PPPOE_GETMODE:
1002154901Sglebius		    {
1003154901Sglebius			char *s;
1004154901Sglebius			size_t len = 0;
1005154901Sglebius
1006154901Sglebius			if (privp->flags == 0)
1007154901Sglebius				len += strlen(NG_PPPOE_STANDARD) + 1;
1008154901Sglebius			if (privp->flags & COMPAT_3COM)
1009154901Sglebius				len += strlen(NG_PPPOE_3COM) + 1;
1010154901Sglebius			if (privp->flags & COMPAT_DLINK)
1011154901Sglebius				len += strlen(NG_PPPOE_DLINK) + 1;
1012154901Sglebius
1013154901Sglebius			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
1014132703Sglebius			if (resp == NULL)
1015132703Sglebius				LEAVE(ENOMEM);
1016154901Sglebius
1017154901Sglebius			s = (char *)resp->data;
1018154901Sglebius			if (privp->flags == 0) {
1019154901Sglebius				len = strlen(NG_PPPOE_STANDARD);
1020154901Sglebius				strlcpy(s, NG_PPPOE_STANDARD, len + 1);
1021154901Sglebius				break;
1022154901Sglebius			}
1023154901Sglebius			if (privp->flags & COMPAT_3COM) {
1024154901Sglebius				len = strlen(NG_PPPOE_3COM);
1025154901Sglebius				strlcpy(s, NG_PPPOE_3COM, len + 1);
1026154901Sglebius				s += len;
1027154901Sglebius			}
1028154901Sglebius			if (privp->flags & COMPAT_DLINK) {
1029154901Sglebius				if (s != resp->data)
1030154901Sglebius					*s++ = '|';
1031154901Sglebius				len = strlen(NG_PPPOE_DLINK);
1032154901Sglebius				strlcpy(s, NG_PPPOE_DLINK, len + 1);
1033154901Sglebius			}
1034132703Sglebius			break;
1035154901Sglebius		    }
1036161117Sglebius		case NGM_PPPOE_SETENADDR:
1037161117Sglebius			if (msg->header.arglen != ETHER_ADDR_LEN)
1038161117Sglebius				LEAVE(EINVAL);
1039161117Sglebius			bcopy(msg->data, &privp->eh.ether_shost,
1040161117Sglebius			    ETHER_ADDR_LEN);
1041161117Sglebius			break;
104252419Sjulian		default:
104352419Sjulian			LEAVE(EINVAL);
104452419Sjulian		}
104552419Sjulian		break;
1046161117Sglebius	case NGM_ETHER_COOKIE:
1047161117Sglebius		if (!(msg->header.flags & NGF_RESP))
1048161117Sglebius			LEAVE(EINVAL);
1049161117Sglebius		switch (msg->header.cmd) {
1050161117Sglebius		case NGM_ETHER_GET_ENADDR:
1051161117Sglebius			if (msg->header.arglen != ETHER_ADDR_LEN)
1052161117Sglebius				LEAVE(EINVAL);
1053161117Sglebius			bcopy(msg->data, &privp->eh.ether_shost,
1054161117Sglebius			    ETHER_ADDR_LEN);
1055161117Sglebius			break;
1056161117Sglebius		default:
1057161181Sglebius			LEAVE(EINVAL);
1058161117Sglebius		}
1059161117Sglebius		break;
106052419Sjulian	default:
106152419Sjulian		LEAVE(EINVAL);
106252419Sjulian	}
106352419Sjulian
1064154604Sglebius	/* Take care of synchronous response, if any. */
106570700Sjulianquit:
1066154901Sglebius	CTR2(KTR_NET, "%20s: returning %d", __func__, error);
106770700Sjulian	NG_RESPOND_MSG(error, node, item, resp);
1068154604Sglebius	/* Free the message and return. */
106970700Sjulian	NG_FREE_MSG(msg);
107052419Sjulian	return(error);
107152419Sjulian}
107252419Sjulian
107352443Sjulian/*
107452443Sjulian * Start a client into the first state. A separate function because
107552443Sjulian * it can be needed if the negotiation times out.
107652443Sjulian */
107752419Sjulianstatic void
107852419Sjulianpppoe_start(sessp sp)
107952419Sjulian{
1080172628Smav	hook_p	hook = sp->hook;
1081172628Smav	node_p	node = NG_HOOK_NODE(hook);
1082172628Smav	priv_p	privp = NG_NODE_PRIVATE(node);
1083172628Smav	negp	neg = sp->neg;
108452419Sjulian	struct {
108552419Sjulian		struct pppoe_tag hdr;
108652419Sjulian		union	uniq	data;
1087103870Salfred	} __packed uniqtag;
1088172628Smav	struct  mbuf *m0;
1089172628Smav	int	error;
109052419Sjulian
1091154604Sglebius	/*
1092154604Sglebius	 * Kick the state machine into starting up.
109352419Sjulian	 */
1094154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
109552419Sjulian	sp->state = PPPOE_SINIT;
1096154604Sglebius	/*
1097154604Sglebius	 * Reset the packet header to broadcast. Since we are
1098161117Sglebius	 * in a client mode use configured ethertype.
1099154604Sglebius	 */
1100172628Smav	memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1101161117Sglebius	    sizeof(struct ether_header));
1102172628Smav	neg->pkt->pkt_header.ph.code = PADI_CODE;
110352419Sjulian	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
110452419Sjulian	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
110552419Sjulian	uniqtag.data.pointer = sp;
110652419Sjulian	init_tags(sp);
110768079Sjulian	insert_tag(sp, &uniqtag.hdr);
1108172628Smav	insert_tag(sp, &neg->service.hdr);
110952419Sjulian	make_packet(sp);
1110172628Smav	/*
1111172628Smav	 * Send packet and prepare to retransmit it after timeout.
1112172628Smav	 */
1113172628Smav	ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1114172628Smav	    pppoe_ticker, NULL, 0);
1115172628Smav	neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1116172628Smav	m0 = m_copypacket(neg->m, M_DONTWAIT);
1117172628Smav	NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
111852419Sjulian}
111952419Sjulian
112090973Sbrianstatic int
112197897Sarchiesend_acname(sessp sp, const struct pppoe_tag *tag)
112290973Sbrian{
112398636Sbrian	int error, tlen;
112490973Sbrian	struct ng_mesg *msg;
112590973Sbrian	struct ngpppoe_sts *sts;
112690973Sbrian
1127154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1128154604Sglebius
112990973Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
113090973Sbrian	    sizeof(struct ngpppoe_sts), M_NOWAIT);
113190973Sbrian	if (msg == NULL)
113290973Sbrian		return (ENOMEM);
113390973Sbrian
113490973Sbrian	sts = (struct ngpppoe_sts *)msg->data;
1135125028Sharti	tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
113698636Sbrian	strncpy(sts->hook, tag->tag_data, tlen);
113798636Sbrian	sts->hook[tlen] = '\0';
1138102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
113990973Sbrian
114090973Sbrian	return (error);
114190973Sbrian}
114290973Sbrian
114396578Sbrianstatic int
114496578Sbriansend_sessionid(sessp sp)
114596578Sbrian{
114696578Sbrian	int error;
114796578Sbrian	struct ng_mesg *msg;
114896578Sbrian
1149154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1150154604Sglebius
115196578Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1152154604Sglebius	    sizeof(uint16_t), M_NOWAIT);
115396578Sbrian	if (msg == NULL)
115496578Sbrian		return (ENOMEM);
115596578Sbrian
1156154604Sglebius	*(uint16_t *)msg->data = sp->Session_ID;
1157102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
115896578Sbrian
115996578Sbrian	return (error);
116096578Sbrian}
116196578Sbrian
116252419Sjulian/*
1163172629Smav * Receive data from session hook and do something with it.
116452419Sjulian */
116552419Sjulianstatic int
116670700Sjulianng_pppoe_rcvdata(hook_p hook, item_p item)
116752419Sjulian{
116870784Sjulian	node_p			node = NG_HOOK_NODE(hook);
116970784Sjulian	const priv_p		privp = NG_NODE_PRIVATE(node);
117070784Sjulian	sessp			sp = NG_HOOK_PRIVATE(hook);
1171172629Smav	struct pppoe_full_hdr	*wh;
1172154604Sglebius	struct mbuf		*m;
1173172629Smav	int			error;
117452419Sjulian
1175154604Sglebius	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1176154604Sglebius	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1177154604Sglebius
117870700Sjulian	NGI_GET_M(item, m);
1179172629Smav	switch (sp->state) {
1180172629Smav	case	PPPOE_NEWCONNECTED:
1181172629Smav	case	PPPOE_CONNECTED: {
1182172629Smav		static const u_char addrctrl[] = { 0xff, 0x03 };
1183172629Smav
118452419Sjulian		/*
1185172629Smav		 * Remove PPP address and control fields, if any.
1186172629Smav		 * For example, ng_ppp(4) always sends LCP packets
1187172629Smav		 * with address and control fields as required by
1188172629Smav		 * generic PPP. PPPoE is an exception to the rule.
118952419Sjulian		 */
1190172629Smav		if (m->m_pkthdr.len >= 2) {
1191172629Smav			if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1192172629Smav				LEAVE(ENOBUFS);
1193172629Smav			if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1194172629Smav				m_adj(m, 2);
1195172629Smav		}
119652419Sjulian		/*
1197172629Smav		 * Bang in a pre-made header, and set the length up
1198172629Smav		 * to be correct. Then send it to the ethernet driver.
119952419Sjulian		 */
1200172629Smav		M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1201172629Smav		if (m == NULL)
1202172629Smav			LEAVE(ENOBUFS);
1203172629Smav
1204172629Smav		wh = mtod(m, struct pppoe_full_hdr *);
1205172629Smav		bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1206172629Smav		wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1207172629Smav		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1208172629Smav		privp->packets_out++;
1209172629Smav		break;
1210172629Smav		}
1211172629Smav	case	PPPOE_PRIMED: {
1212172629Smav		struct {
1213172629Smav			struct pppoe_tag hdr;
1214172629Smav			union	uniq	data;
1215172629Smav		} __packed 	uniqtag;
1216172629Smav		const struct pppoe_tag	*tag;
1217172629Smav		struct mbuf 	*m0;
1218172629Smav		const struct pppoe_hdr	*ph;
1219172629Smav		negp		neg = sp->neg;
1220172629Smav	        uint16_t	session;
1221172629Smav		uint16_t	length;
1222172629Smav		uint8_t		code;
1223172629Smav
1224172629Smav		/*
1225172629Smav		 * A PADI packet is being returned by the application
1226172629Smav		 * that has set up this hook. This indicates that it
1227172629Smav		 * wants us to offer service.
1228172629Smav		 */
1229172629Smav		if (m->m_len < sizeof(*wh)) {
1230172629Smav			m = m_pullup(m, sizeof(*wh));
1231172629Smav			if (m == NULL)
123252510Sjulian				LEAVE(ENOBUFS);
123352419Sjulian		}
123452419Sjulian		wh = mtod(m, struct pppoe_full_hdr *);
1235172629Smav		ph = &wh->ph;
1236172629Smav		session = ntohs(wh->ph.sid);
123752419Sjulian		length = ntohs(wh->ph.length);
1238172629Smav		code = wh->ph.code;
1239172629Smav		/* Use peers mode in session. */
1240172629Smav		neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1241172629Smav		if (code != PADI_CODE)
1242172629Smav			LEAVE(EINVAL);
1243172629Smav		ng_uncallout(&neg->handle, node);
124452419Sjulian
1245172629Smav		/*
1246172629Smav		 * This is the first time we hear
1247172629Smav		 * from the client, so note it's
1248172629Smav		 * unicast address, replacing the
1249172629Smav		 * broadcast address.
1250172629Smav		 */
1251172629Smav		bcopy(wh->eh.ether_shost,
1252172629Smav			neg->pkt->pkt_header.eh.ether_dhost,
1253172629Smav			ETHER_ADDR_LEN);
1254172629Smav		sp->state = PPPOE_SOFFER;
1255172629Smav		neg->timeout = 0;
1256172629Smav		neg->pkt->pkt_header.ph.code = PADO_CODE;
1257154862Sglebius
1258172629Smav		/*
1259172629Smav		 * Start working out the tags to respond with.
1260172629Smav		 */
1261172629Smav		uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1262172629Smav		uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1263172629Smav		uniqtag.data.pointer = sp;
1264172629Smav		init_tags(sp);
1265172629Smav		insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1266172629Smav		if ((tag = get_tag(ph, PTT_SRV_NAME)))
1267172629Smav			insert_tag(sp, tag);	  /* return service */
1268172629Smav		/*
1269172629Smav		 * If we have a NULL service request
1270172629Smav		 * and have an extra service defined in this hook,
1271172629Smav		 * then also add a tag for the extra service.
1272172629Smav		 * XXX this is a hack. eventually we should be able
1273172629Smav		 * to support advertising many services, not just one
1274172629Smav		 */
1275172629Smav		if (((tag == NULL) || (tag->tag_len == 0)) &&
1276172629Smav		    (neg->service.hdr.tag_len != 0)) {
1277172629Smav			insert_tag(sp, &neg->service.hdr); /* SERVICE */
1278172629Smav		}
1279172629Smav		if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1280172629Smav			insert_tag(sp, tag); /* returned hostunique */
1281172629Smav		insert_tag(sp, &uniqtag.hdr);
1282172629Smav		scan_tags(sp, ph);
1283172629Smav		make_packet(sp);
1284172629Smav		/*
1285172629Smav		 * Send the offer but if they don't respond
1286172629Smav		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1287172629Smav		 */
1288172629Smav		ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1289172629Smav		    pppoe_ticker, NULL, 0);
1290172629Smav		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1291172629Smav		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1292172629Smav		privp->packets_out++;
1293172629Smav		break;
1294172629Smav		}
129552419Sjulian
1296172629Smav	/*
1297172629Smav	 * Packets coming from the hook make no sense
1298172629Smav	 * to sessions in the rest of states. Throw them away.
1299172629Smav	 */
1300172629Smav	default:
1301172629Smav		LEAVE(ENETUNREACH);
1302172629Smav	}
1303172629Smavquit:
1304172629Smav	if (item)
1305172629Smav		NG_FREE_ITEM(item);
1306172629Smav	NG_FREE_M(m);
1307172629Smav	return (error);
1308172629Smav}
130952419Sjulian
1310172629Smav/*
1311172629Smav * Receive data from ether and do something with it.
1312172629Smav */
1313172629Smavstatic int
1314172629Smavng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1315172629Smav{
1316172629Smav	node_p			node = NG_HOOK_NODE(hook);
1317172629Smav	const priv_p		privp = NG_NODE_PRIVATE(node);
1318172629Smav	sessp			sp = NG_HOOK_PRIVATE(hook);
1319172629Smav	const struct pppoe_tag	*utag = NULL, *tag = NULL;
1320172629Smav	const struct pppoe_full_hdr *wh;
1321172629Smav	const struct pppoe_hdr	*ph;
1322172629Smav	negp			neg = NULL;
1323172629Smav	struct mbuf		*m;
1324172629Smav	hook_p 			sendhook;
1325172629Smav	int			error = 0;
1326172629Smav	uint16_t		session;
1327172629Smav	uint16_t		length;
1328172629Smav	uint8_t			code;
1329172629Smav	struct	mbuf 		*m0;
133052419Sjulian
1331172629Smav	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1332172629Smav	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
133352419Sjulian
1334172629Smav	NGI_GET_M(item, m);
1335172629Smav	/*
1336172629Smav	 * Dig out various fields from the packet.
1337172629Smav	 * Use them to decide where to send it.
1338172629Smav	 */
1339172629Smav	privp->packets_in++;
1340172629Smav	if( m->m_len < sizeof(*wh)) {
1341172629Smav		m = m_pullup(m, sizeof(*wh)); /* Checks length */
1342172629Smav		if (m == NULL) {
1343172629Smav			log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1344172629Smav			    "m_pullup(wh)\n", node->nd_ID);
1345172629Smav			LEAVE(ENOBUFS);
1346172629Smav		}
1347172629Smav	}
1348172629Smav	wh = mtod(m, struct pppoe_full_hdr *);
1349172629Smav	length = ntohs(wh->ph.length);
1350172629Smav	switch(wh->eh.ether_type) {
1351172629Smav	case	ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1352172629Smav	case	ETHERTYPE_PPPOE_DISC:
1353172629Smav		/*
1354172629Smav		 * We need to try to make sure that the tag area
1355172629Smav		 * is contiguous, or we could wander off the end
1356172629Smav		 * of a buffer and make a mess.
1357172629Smav		 * (Linux wouldn't have this problem).
1358172629Smav		 */
1359172629Smav		if (m->m_pkthdr.len <= MHLEN) {
1360172629Smav			if( m->m_len < m->m_pkthdr.len) {
1361172629Smav				m = m_pullup(m, m->m_pkthdr.len);
1362172629Smav				if (m == NULL) {
1363172629Smav					log(LOG_NOTICE, "ng_pppoe[%x]: "
1364172629Smav					    "couldn't m_pullup(pkthdr)\n",
1365172629Smav					    node->nd_ID);
1366172629Smav					LEAVE(ENOBUFS);
136752419Sjulian				}
136852419Sjulian			}
1369172629Smav		}
1370172629Smav		if (m->m_len != m->m_pkthdr.len) {
137152419Sjulian			/*
1372172629Smav			 * It's not all in one piece.
1373172629Smav			 * We need to do extra work.
1374172629Smav			 * Put it into a cluster.
137552419Sjulian			 */
1376172629Smav			struct mbuf *n;
1377172629Smav			n = m_dup(m, M_DONTWAIT);
1378172629Smav			m_freem(m);
1379172629Smav			m = n;
1380172629Smav			if (m) {
1381172629Smav				/* just check we got a cluster */
1382172629Smav				if (m->m_len != m->m_pkthdr.len) {
1383172629Smav					m_freem(m);
1384172629Smav					m = NULL;
1385172629Smav				}
138652419Sjulian			}
1387172629Smav			if (m == NULL) {
1388172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1389172629Smav				    "fragmented\n", node->nd_ID);
139052419Sjulian				LEAVE(EMSGSIZE);
139152419Sjulian			}
139252419Sjulian		}
1393172629Smav		wh = mtod(m, struct pppoe_full_hdr *);
1394172629Smav		length = ntohs(wh->ph.length);
1395172629Smav		ph = &wh->ph;
1396172629Smav		session = ntohs(wh->ph.sid);
1397172629Smav		code = wh->ph.code;
139864502Sarchie
1399172629Smav		switch(code) {
1400172629Smav		case	PADI_CODE:
140152419Sjulian			/*
1402172629Smav			 * We are a server:
1403172629Smav			 * Look for a hook with the required service and send
1404172629Smav			 * the ENTIRE packet up there. It should come back to
1405172629Smav			 * a new hook in PRIMED state. Look there for further
1406172629Smav			 * processing.
140764502Sarchie			 */
1408172629Smav			tag = get_tag(ph, PTT_SRV_NAME);
1409172629Smav			if (tag == NULL) {
1410172629Smav				CTR1(KTR_NET, "%20s: PADI w/o Service-Name",
1411172629Smav				    __func__);
1412172629Smav				LEAVE(ENETUNREACH);
141364502Sarchie			}
1414172629Smav
141564502Sarchie			/*
1416172629Smav			 * First, try to match Service-Name against our
1417172629Smav			 * listening hooks. If no success and we are in D-Link
1418172629Smav			 * compat mode and Service-Name is empty, then we
1419172629Smav			 * broadcast the PADI to all listening hooks.
142052419Sjulian			 */
1421172629Smav			sendhook = pppoe_match_svc(node, tag);
1422172629Smav			if (sendhook != NULL)
1423172629Smav				NG_FWD_NEW_DATA(error, item, sendhook, m);
1424172629Smav			else if (privp->flags & COMPAT_DLINK &&
1425172629Smav				 ntohs(tag->tag_len) == 0)
1426172629Smav				error = pppoe_broadcast_padi(node, m);
1427172629Smav			else
1428172629Smav				error = ENETUNREACH;
142952419Sjulian			break;
1430172629Smav		case	PADO_CODE:
1431172629Smav			/*
1432172629Smav			 * We are a client:
1433172629Smav			 * Use the host_uniq tag to find the hook this is in
1434172629Smav			 * response to. Received #2, now send #3
1435172629Smav			 * For now simply accept the first we receive.
1436172629Smav			 */
1437172629Smav			utag = get_tag(ph, PTT_HOST_UNIQ);
1438172629Smav			if ((utag == NULL) ||
1439172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1440172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1441172629Smav				    "unique field\n", node->nd_ID);
1442172629Smav				LEAVE(ENETUNREACH);
144352419Sjulian			}
1444172629Smav
1445172629Smav			sendhook = pppoe_finduniq(node, utag);
1446172629Smav			if (sendhook == NULL) {
1447172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: no "
1448172629Smav				    "matching session\n", node->nd_ID);
1449172629Smav				LEAVE(ENETUNREACH);
1450172629Smav			}
1451172629Smav
145252419Sjulian			/*
1453172629Smav			 * Check the session is in the right state.
1454172629Smav			 * It needs to be in PPPOE_SINIT.
145552419Sjulian			 */
1456172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1457174981Smav			if (sp->state == PPPOE_SREQ ||
1458174981Smav			    sp->state == PPPOE_CONNECTED) {
1459174981Smav				break;	/* Multiple PADO is OK. */
1460174981Smav			}
1461172629Smav			if (sp->state != PPPOE_SINIT) {
1462172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: session "
1463172629Smav				    "in wrong state\n", node->nd_ID);
1464172629Smav				LEAVE(ENETUNREACH);
1465172629Smav			}
146652419Sjulian			neg = sp->neg;
1467174981Smav			/* If requested specific AC-name, check it. */
1468174981Smav			if (neg->ac_name_len) {
1469174981Smav				tag = get_tag(ph, PTT_AC_NAME);
1470174981Smav				if (!tag) {
1471174981Smav					/* No PTT_AC_NAME in PADO */
1472174981Smav					break;
1473174981Smav				}
1474174981Smav				if (neg->ac_name_len != htons(tag->tag_len) ||
1475174981Smav				    strncmp(neg->ac_name.data, tag->tag_data,
1476174981Smav				    neg->ac_name_len) != 0) {
1477174981Smav					break;
1478174981Smav				}
1479174981Smav			}
1480174981Smav			sp->state = PPPOE_SREQ;
1481138562Sglebius			ng_uncallout(&neg->handle, node);
148252419Sjulian
148352419Sjulian			/*
148452419Sjulian			 * This is the first time we hear
1485172629Smav			 * from the server, so note it's
148652419Sjulian			 * unicast address, replacing the
1487172629Smav			 * broadcast address .
148852419Sjulian			 */
148952419Sjulian			bcopy(wh->eh.ether_shost,
149052419Sjulian				neg->pkt->pkt_header.eh.ether_dhost,
149152419Sjulian				ETHER_ADDR_LEN);
149252419Sjulian			neg->timeout = 0;
1493172629Smav			neg->pkt->pkt_header.ph.code = PADR_CODE;
1494172629Smav			init_tags(sp);
1495172629Smav			insert_tag(sp, utag);      	/* Host Unique */
1496172629Smav			if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1497172629Smav				insert_tag(sp, tag); 	/* return cookie */
1498172629Smav			if ((tag = get_tag(ph, PTT_AC_NAME))) {
1499172629Smav				insert_tag(sp, tag); 	/* return it */
1500172629Smav				send_acname(sp, tag);
1501172629Smav			}
1502172629Smav			insert_tag(sp, &neg->service.hdr); /* Service */
1503172629Smav			scan_tags(sp, ph);
1504172629Smav			make_packet(sp);
1505172629Smav			sp->state = PPPOE_SREQ;
1506172629Smav			ng_callout(&neg->handle, node, sp->hook,
1507172629Smav			    PPPOE_INITIAL_TIMEOUT * hz,
1508172629Smav			    pppoe_ticker, NULL, 0);
1509172629Smav			neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1510172629Smav			m0 = m_copypacket(neg->m, M_DONTWAIT);
1511172629Smav			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1512172629Smav			break;
1513172629Smav		case	PADR_CODE:
1514172629Smav			/*
1515172629Smav			 * We are a server:
1516172629Smav			 * Use the ac_cookie tag to find the
1517172629Smav			 * hook this is in response to.
1518172629Smav			 */
1519172629Smav			utag = get_tag(ph, PTT_AC_COOKIE);
1520172629Smav			if ((utag == NULL) ||
1521172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1522172629Smav				LEAVE(ENETUNREACH);
1523172629Smav			}
152452419Sjulian
1525172629Smav			sendhook = pppoe_finduniq(node, utag);
1526172629Smav			if (sendhook == NULL)
1527172629Smav				LEAVE(ENETUNREACH);
1528172629Smav
152952419Sjulian			/*
1530172629Smav			 * Check the session is in the right state.
1531172629Smav			 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1532172629Smav			 * If the latter, then this is a retry by the client,
1533172629Smav			 * so be nice, and resend.
153452419Sjulian			 */
1535172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1536172629Smav			if (sp->state == PPPOE_NEWCONNECTED) {
1537172629Smav				/*
1538172629Smav				 * Whoa! drop back to resend that PADS packet.
1539172629Smav				 * We should still have a copy of it.
1540172629Smav				 */
1541172629Smav				sp->state = PPPOE_SOFFER;
1542175865Smav			} else if (sp->state != PPPOE_SOFFER)
1543172629Smav				LEAVE (ENETUNREACH);
1544172629Smav			neg = sp->neg;
1545172629Smav			ng_uncallout(&neg->handle, node);
1546172629Smav			neg->pkt->pkt_header.ph.code = PADS_CODE;
1547175865Smav			if (sp->Session_ID == 0) {
1548172629Smav				neg->pkt->pkt_header.ph.sid =
1549172629Smav				    htons(sp->Session_ID
1550172629Smav					= get_new_sid(node));
1551175865Smav				pppoe_addsession(sp);
1552175865Smav			}
1553172629Smav			send_sessionid(sp);
1554172629Smav			neg->timeout = 0;
1555172629Smav			/*
1556172629Smav			 * start working out the tags to respond with.
1557172629Smav			 */
155852419Sjulian			init_tags(sp);
155952419Sjulian			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
156053154Sjulian			if ((tag = get_tag(ph, PTT_SRV_NAME)))
1561172629Smav				insert_tag(sp, tag);/* return service */
156253154Sjulian			if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1563172629Smav				insert_tag(sp, tag); /* return it */
1564172629Smav			insert_tag(sp, utag);	/* ac_cookie */
156552419Sjulian			scan_tags(sp, ph);
156652419Sjulian			make_packet(sp);
1567172629Smav			sp->state = PPPOE_NEWCONNECTED;
1568172629Smav
1569172629Smav			/* Send the PADS without a timeout - we're now connected. */
1570172629Smav			m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1571172629Smav			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1572172629Smav
1573172628Smav			/*
1574172629Smav			 * Having sent the last Negotiation header,
1575172629Smav			 * Set up the stored packet header to be correct for
1576172629Smav			 * the actual session. But keep the negotialtion stuff
1577172629Smav			 * around in case we need to resend this last packet.
1578172629Smav			 * We'll discard it when we move from NEWCONNECTED
1579172629Smav			 * to CONNECTED
1580172628Smav			 */
1581172629Smav			sp->pkt_hdr = neg->pkt->pkt_header;
1582172629Smav			/* Configure ethertype depending on what
1583172629Smav			 * ethertype was used at discovery phase */
1584172629Smav			if (sp->pkt_hdr.eh.ether_type ==
1585172629Smav			    ETHERTYPE_PPPOE_3COM_DISC)
1586172629Smav				sp->pkt_hdr.eh.ether_type
1587172629Smav					= ETHERTYPE_PPPOE_3COM_SESS;
1588172629Smav			else
1589172629Smav				sp->pkt_hdr.eh.ether_type
1590172629Smav					= ETHERTYPE_PPPOE_SESS;
1591172629Smav			sp->pkt_hdr.ph.code = 0;
1592172629Smav			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
159352419Sjulian			break;
1594172629Smav		case	PADS_CODE:
1595172629Smav			/*
1596172629Smav			 * We are a client:
1597172629Smav			 * Use the host_uniq tag to find the hook this is in
1598172629Smav			 * response to. Take the session ID and store it away.
1599172629Smav			 * Also make sure the pre-made header is correct and
1600172629Smav			 * set us into Session mode.
1601172629Smav			 */
1602172629Smav			utag = get_tag(ph, PTT_HOST_UNIQ);
1603172629Smav			if ((utag == NULL) ||
1604172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1605172629Smav				LEAVE (ENETUNREACH);
1606172629Smav			}
1607172629Smav			sendhook = pppoe_finduniq(node, utag);
1608172629Smav			if (sendhook == NULL)
1609172629Smav				LEAVE(ENETUNREACH);
161052419Sjulian
1611172629Smav			/*
1612172629Smav			 * Check the session is in the right state.
1613172629Smav			 * It needs to be in PPPOE_SREQ.
1614172629Smav			 */
1615172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1616172629Smav			if (sp->state != PPPOE_SREQ)
1617172629Smav				LEAVE(ENETUNREACH);
1618172629Smav			neg = sp->neg;
1619172629Smav			ng_uncallout(&neg->handle, node);
1620172629Smav			neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1621172629Smav			sp->Session_ID = ntohs(wh->ph.sid);
1622175865Smav			pppoe_addsession(sp);
1623172629Smav			send_sessionid(sp);
1624172629Smav			neg->timeout = 0;
1625172629Smav			sp->state = PPPOE_CONNECTED;
1626172629Smav			/*
1627172629Smav			 * Now we have gone to Connected mode,
1628172629Smav			 * Free all resources needed for negotiation.
1629172629Smav			 * Keep a copy of the header we will be using.
1630172629Smav			 */
1631172629Smav			sp->pkt_hdr = neg->pkt->pkt_header;
1632172629Smav			if (privp->flags & COMPAT_3COM)
1633172629Smav				sp->pkt_hdr.eh.ether_type
1634172629Smav					= ETHERTYPE_PPPOE_3COM_SESS;
1635172629Smav			else
1636172629Smav				sp->pkt_hdr.eh.ether_type
1637172629Smav					= ETHERTYPE_PPPOE_SESS;
1638172629Smav			sp->pkt_hdr.ph.code = 0;
1639172629Smav			m_freem(neg->m);
1640172629Smav			free(sp->neg, M_NETGRAPH_PPPOE);
1641172629Smav			sp->neg = NULL;
1642172629Smav			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1643172629Smav			break;
1644172629Smav		case	PADT_CODE:
1645172629Smav			/*
1646172629Smav			 * Find matching peer/session combination.
1647172629Smav			 */
1648175865Smav			sp = pppoe_findsession(privp, wh);
1649175865Smav			if (sp == NULL)
1650172629Smav				LEAVE(ENETUNREACH);
1651172629Smav			/* Disconnect that hook. */
1652175865Smav			ng_rmhook_self(sp->hook);
1653172629Smav			break;
1654172629Smav		default:
1655172629Smav			LEAVE(EPFNOSUPPORT);
1656172629Smav		}
1657172629Smav		break;
1658172629Smav	case	ETHERTYPE_PPPOE_3COM_SESS:
1659172629Smav	case	ETHERTYPE_PPPOE_SESS:
166052419Sjulian		/*
1661172629Smav		 * Find matching peer/session combination.
166252419Sjulian		 */
1663175865Smav		sp = pppoe_findsession(privp, wh);
1664175865Smav		if (sp == NULL)
1665172629Smav			LEAVE (ENETUNREACH);
1666172629Smav		m_adj(m, sizeof(*wh));
1667172629Smav
1668172629Smav		/* If packet too short, dump it. */
1669172629Smav		if (m->m_pkthdr.len < length)
1670172629Smav			LEAVE(EMSGSIZE);
1671172629Smav		/* Also need to trim excess at the end */
1672172629Smav		if (m->m_pkthdr.len > length) {
1673172629Smav			m_adj(m, -((int)(m->m_pkthdr.len - length)));
167452419Sjulian		}
1675172629Smav		if ( sp->state != PPPOE_CONNECTED) {
1676172629Smav			if (sp->state == PPPOE_NEWCONNECTED) {
1677172629Smav				sp->state = PPPOE_CONNECTED;
1678172629Smav				/*
1679172629Smav				 * Now we have gone to Connected mode,
1680172629Smav				 * Free all resources needed for negotiation.
1681172629Smav				 * Be paranoid about whether there may be
1682172629Smav				 * a timeout.
1683172629Smav				 */
1684172629Smav				m_freem(sp->neg->m);
1685172629Smav				ng_uncallout(&sp->neg->handle, node);
1686172629Smav				free(sp->neg, M_NETGRAPH_PPPOE);
1687172629Smav				sp->neg = NULL;
1688172629Smav			} else {
1689172629Smav				LEAVE (ENETUNREACH);
1690172629Smav			}
1691172629Smav		}
1692175865Smav		NG_FWD_NEW_DATA(error, item, sp->hook, m);
1693172629Smav		break;
1694172629Smav	default:
1695172629Smav		LEAVE(EPFNOSUPPORT);
169652419Sjulian	}
169752419Sjulianquit:
169870914Sjulian	if (item)
169970914Sjulian		NG_FREE_ITEM(item);
170070700Sjulian	NG_FREE_M(m);
1701172629Smav	return (error);
170252419Sjulian}
170352419Sjulian
170452419Sjulian/*
1705172629Smav * Receive data from debug hook and bypass it to ether.
1706172629Smav */
1707172629Smavstatic int
1708172629Smavng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1709172629Smav{
1710172629Smav	node_p		node = NG_HOOK_NODE(hook);
1711172629Smav	const priv_p	privp = NG_NODE_PRIVATE(node);
1712172629Smav	int		error;
1713172629Smav
1714172629Smav	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1715172629Smav	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1716172629Smav
1717172629Smav	NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1718172629Smav	privp->packets_out++;
1719172629Smav	return (error);
1720172629Smav}
1721172629Smav
1722172629Smav/*
172352419Sjulian * Do local shutdown processing..
172452419Sjulian * If we are a persistant device, we might refuse to go away, and
172552419Sjulian * we'd only remove our links and reset ourself.
172652419Sjulian */
172752419Sjulianstatic int
172870700Sjulianng_pppoe_shutdown(node_p node)
172952419Sjulian{
1730175865Smav	const priv_p privp = NG_NODE_PRIVATE(node);
1731175865Smav	int	i;
173252419Sjulian
1733175865Smav	for (i = 0; i < SESSHASHSIZE; i++)
1734175865Smav	    mtx_destroy(&privp->sesshash[i].mtx);
173570784Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
1736175865Smav	NG_NODE_UNREF(privp->node);
1737175865Smav	free(privp, M_NETGRAPH_PPPOE);
173852419Sjulian	return (0);
173952419Sjulian}
174052419Sjulian
174152419Sjulian/*
174252419Sjulian * Hook disconnection
174352419Sjulian *
174453498Sjulian * Clean up all dangling links and information about the session/hook.
1745154604Sglebius * For this type, removal of the last link destroys the node.
174652419Sjulian */
174752419Sjulianstatic int
174852562Sjulianng_pppoe_disconnect(hook_p hook)
174952419Sjulian{
175070784Sjulian	node_p node = NG_HOOK_NODE(hook);
175170784Sjulian	priv_p privp = NG_NODE_PRIVATE(node);
175252419Sjulian	sessp	sp;
175352419Sjulian
1754172270Smav	if (hook == privp->debug_hook) {
175552419Sjulian		privp->debug_hook = NULL;
1756172270Smav	} else if (hook == privp->ethernet_hook) {
175752419Sjulian		privp->ethernet_hook = NULL;
175870784Sjulian		if (NG_NODE_IS_VALID(node))
175970700Sjulian			ng_rmnode_self(node);
176052419Sjulian	} else {
176170784Sjulian		sp = NG_HOOK_PRIVATE(hook);
176252441Sjulian		if (sp->state != PPPOE_SNONE ) {
176352441Sjulian			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
176452441Sjulian		}
176559728Sjulian		/*
176659728Sjulian		 * According to the spec, if we are connected,
176759728Sjulian		 * we should send a DISC packet if we are shutting down
176859728Sjulian		 * a session.
176959728Sjulian		 */
177052523Sjulian		if ((privp->ethernet_hook)
177152523Sjulian		&& ((sp->state == PPPOE_CONNECTED)
177252523Sjulian		 || (sp->state == PPPOE_NEWCONNECTED))) {
177352523Sjulian			struct mbuf *m;
177452523Sjulian
1775154604Sglebius			/* Generate a packet of that type. */
1776111119Simp			MGETHDR(m, M_DONTWAIT, MT_DATA);
1777161034Sglebius			if (m == NULL)
1778161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1779161034Sglebius				    "mbufs\n", node->nd_ID);
178053498Sjulian			else {
1781172269Smav				struct pppoe_full_hdr *wh;
1782172269Smav				struct pppoe_tag *tag;
1783172269Smav				int	msglen = strlen(SIGNOFF);
1784172269Smav				int	error = 0;
1785172269Smav
178653498Sjulian				m->m_pkthdr.rcvif = NULL;
178753498Sjulian				m->m_pkthdr.len = m->m_len = sizeof(*wh);
1788172269Smav				wh = mtod(m, struct pppoe_full_hdr *);
1789172269Smav				bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1790172269Smav
1791172269Smav				/* Revert the stored header to DISC/PADT mode. */
1792172269Smav				wh->ph.code = PADT_CODE;
179353498Sjulian				/*
1794172269Smav				 * Configure ethertype depending on what
1795172269Smav				 * was used during sessions stage.
1796172269Smav				 */
1797172269Smav				if (wh->eh.ether_type ==
1798172269Smav				    ETHERTYPE_PPPOE_3COM_SESS)
1799172269Smav					wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1800172269Smav				else
1801172269Smav					wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1802172269Smav				/*
180353498Sjulian				 * Add a General error message and adjust
1804154604Sglebius				 * sizes.
180553498Sjulian				 */
180653498Sjulian				tag = wh->ph.tag;
180753498Sjulian				tag->tag_type = PTT_GEN_ERR;
180853498Sjulian				tag->tag_len = htons((u_int16_t)msglen);
180953498Sjulian				strncpy(tag->tag_data, SIGNOFF, msglen);
181053498Sjulian				m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
181153498Sjulian				    msglen);
181253498Sjulian				wh->ph.length = htons(sizeof(*tag) + msglen);
181370700Sjulian				NG_SEND_DATA_ONLY(error,
181470700Sjulian					privp->ethernet_hook, m);
181553498Sjulian			}
181652523Sjulian		}
1817175865Smav		if (sp->Session_ID)
1818175865Smav			pppoe_delsession(sp);
181959728Sjulian		/*
182063138Sasmodai		 * As long as we have somewhere to store the timeout handle,
182159728Sjulian		 * we may have a timeout pending.. get rid of it.
182259728Sjulian		 */
182352443Sjulian		if (sp->neg) {
1824138562Sglebius			ng_uncallout(&sp->neg->handle, node);
182552443Sjulian			if (sp->neg->m)
182652443Sjulian				m_freem(sp->neg->m);
1827154604Sglebius			free(sp->neg, M_NETGRAPH_PPPOE);
182852443Sjulian		}
1829154604Sglebius		free(sp, M_NETGRAPH_PPPOE);
183070784Sjulian		NG_HOOK_SET_PRIVATE(hook, NULL);
183152419Sjulian	}
1832154604Sglebius	if ((NG_NODE_NUMHOOKS(node) == 0) &&
1833154604Sglebius	    (NG_NODE_IS_VALID(node)))
183470700Sjulian		ng_rmnode_self(node);
183552419Sjulian	return (0);
183652419Sjulian}
183752419Sjulian
183852419Sjulian/*
1839154604Sglebius * Timeouts come here.
184052419Sjulian */
184152419Sjulianstatic void
1842138562Sglebiuspppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
184352419Sjulian{
1844154604Sglebius	priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
184570784Sjulian	sessp	sp = NG_HOOK_PRIVATE(hook);
184652419Sjulian	negp	neg = sp->neg;
1847154604Sglebius	struct mbuf *m0 = NULL;
184852419Sjulian	int	error = 0;
184952419Sjulian
1850154604Sglebius	CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1851154604Sglebius	    __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
185252419Sjulian	switch(sp->state) {
185352419Sjulian		/*
1854154604Sglebius		 * Resend the last packet, using an exponential backoff.
185552419Sjulian		 * After a period of time, stop growing the backoff,
1856154604Sglebius		 * And either leave it, or revert to the start.
185752419Sjulian		 */
185852419Sjulian	case	PPPOE_SINIT:
185952419Sjulian	case	PPPOE_SREQ:
1860154604Sglebius		/* Timeouts on these produce resends. */
1861111119Simp		m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
186270700Sjulian		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1863138562Sglebius		ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1864138562Sglebius		    pppoe_ticker, NULL, 0);
186552419Sjulian		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
186652419Sjulian			if (sp->state == PPPOE_SREQ) {
1867154604Sglebius				/* Revert to SINIT mode. */
186852441Sjulian				pppoe_start(sp);
186952419Sjulian			} else {
187052419Sjulian				neg->timeout = PPPOE_TIMEOUT_LIMIT;
187152419Sjulian			}
187252419Sjulian		}
187352419Sjulian		break;
187452419Sjulian	case	PPPOE_PRIMED:
187552419Sjulian	case	PPPOE_SOFFER:
1876154604Sglebius		/* A timeout on these says "give up" */
187770935Sjulian		ng_rmhook_self(hook);
187852419Sjulian		break;
187952419Sjulian	default:
1880154604Sglebius		/* Timeouts have no meaning in other states. */
1881161034Sglebius		log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1882161034Sglebius		    node->nd_ID);
188352419Sjulian	}
188452419Sjulian}
188552419Sjulian
188652419Sjulian/*
188752419Sjulian * Parse an incoming packet to see if any tags should be copied to the
188853145Sjulian * output packet. Don't do any tags that have been handled in the main
188953145Sjulian * state machine.
189052419Sjulian */
1891154604Sglebiusstatic const struct pppoe_tag*
189297897Sarchiescan_tags(sessp	sp, const struct pppoe_hdr* ph)
189352419Sjulian{
189497897Sarchie	const char *const end = (const char *)next_tag(ph);
189597897Sarchie	const char *ptn;
189697897Sarchie	const struct pppoe_tag *pt = &ph->tag[0];
1897154604Sglebius
189852419Sjulian	/*
189952419Sjulian	 * Keep processing tags while a tag header will still fit.
190052419Sjulian	 */
1901154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1902154604Sglebius
190397897Sarchie	while((const char*)(pt + 1) <= end) {
190452419Sjulian		/*
190552419Sjulian		 * If the tag data would go past the end of the packet, abort.
190652419Sjulian		 */
190797897Sarchie		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
190852419Sjulian		if(ptn > end)
190952419Sjulian			return NULL;
191052419Sjulian
191152419Sjulian		switch (pt->tag_type) {
191252419Sjulian		case	PTT_RELAY_SID:
191352419Sjulian			insert_tag(sp, pt);
191452419Sjulian			break;
191552419Sjulian		case	PTT_EOL:
191652419Sjulian			return NULL;
191752419Sjulian		case	PTT_SRV_NAME:
191852419Sjulian		case	PTT_AC_NAME:
191952419Sjulian		case	PTT_HOST_UNIQ:
192052419Sjulian		case	PTT_AC_COOKIE:
192152419Sjulian		case	PTT_VENDOR:
192252419Sjulian		case	PTT_SRV_ERR:
192352419Sjulian		case	PTT_SYS_ERR:
192452419Sjulian		case	PTT_GEN_ERR:
192552419Sjulian			break;
192652419Sjulian		}
192797897Sarchie		pt = (const struct pppoe_tag*)ptn;
192852419Sjulian	}
192952419Sjulian	return NULL;
193052419Sjulian}
193152419Sjulian
193252441Sjulianstatic	int
193352441Sjulianpppoe_send_event(sessp sp, enum cmd cmdid)
193452441Sjulian{
193552441Sjulian	int error;
193652441Sjulian	struct ng_mesg *msg;
193752562Sjulian	struct ngpppoe_sts *sts;
193852441Sjulian
1939154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1940154604Sglebius
194168845Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
194252562Sjulian			sizeof(struct ngpppoe_sts), M_NOWAIT);
194369922Sjulian	if (msg == NULL)
194469922Sjulian		return (ENOMEM);
194552562Sjulian	sts = (struct ngpppoe_sts *)msg->data;
1946125028Sharti	strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1947102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
194852441Sjulian	return (error);
194952441Sjulian}
1950