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$
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
61227293Sedstatic MALLOC_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 */
238176775Smav	LIST_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;
247176775Smav	LIST_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;
263176775Smav	LIST_HEAD(, sess_con) listeners;
264175865Smav	struct sess_hash_entry	sesshash[SESSHASHSIZE];
26552419Sjulian};
266154604Sglebiustypedef struct PPPoE *priv_p;
26752419Sjulian
26852419Sjulianunion uniq {
26952419Sjulian	char bytes[sizeof(void *)];
270154604Sglebius	void *pointer;
271154604Sglebius};
27252419Sjulian
27352419Sjulian#define	LEAVE(x) do { error = x; goto quit; } while(0)
27452419Sjulianstatic void	pppoe_start(sessp sp);
275138562Sglebiusstatic void	pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
27697897Sarchiestatic const	struct pppoe_tag *scan_tags(sessp sp,
27797897Sarchie			const struct pppoe_hdr* ph);
27852441Sjulianstatic	int	pppoe_send_event(sessp sp, enum cmd cmdid);
27952419Sjulian
28052419Sjulian/*************************************************************************
28152419Sjulian * Some basic utilities  from the Linux version with author's permission.*
28252419Sjulian * Author:	Michal Ostrowski <mostrows@styx.uwaterloo.ca>		 *
28352419Sjulian ************************************************************************/
28452419Sjulian
28552419Sjulian
28652419Sjulian
28752419Sjulian/*
288154604Sglebius * Return the location where the next tag can be put
28952419Sjulian */
29097897Sarchiestatic __inline const struct pppoe_tag*
29197897Sarchienext_tag(const struct pppoe_hdr* ph)
29252419Sjulian{
293189315Sed	return (const struct pppoe_tag*)(((const char*)(ph + 1))
29497897Sarchie	    + ntohs(ph->length));
29552419Sjulian}
29652419Sjulian
29752419Sjulian/*
298154604Sglebius * Look for a tag of a specific type.
299154604Sglebius * Don't trust any length the other end says,
30052419Sjulian * but assume we already sanity checked ph->length.
30152419Sjulian */
30297897Sarchiestatic const struct pppoe_tag*
303154604Sglebiusget_tag(const struct pppoe_hdr* ph, uint16_t idx)
30452419Sjulian{
30597897Sarchie	const char *const end = (const char *)next_tag(ph);
306189315Sed	const struct pppoe_tag *pt = (const void *)(ph + 1);
30797897Sarchie	const char *ptn;
308154604Sglebius
30952419Sjulian	/*
31052419Sjulian	 * Keep processing tags while a tag header will still fit.
31152419Sjulian	 */
31297897Sarchie	while((const char*)(pt + 1) <= end) {
313154604Sglebius		/*
314154604Sglebius		 * If the tag data would go past the end of the packet, abort.
315154604Sglebius		 */
316154604Sglebius		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
317154604Sglebius		if (ptn > end) {
318154604Sglebius			CTR2(KTR_NET, "%20s: invalid length for tag %d",
319154604Sglebius			    __func__, idx);
320154604Sglebius			return (NULL);
321154604Sglebius		}
322154604Sglebius		if (pt->tag_type == idx) {
323154604Sglebius			CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
324154604Sglebius			return (pt);
325154604Sglebius		}
32652419Sjulian
327154604Sglebius		pt = (const struct pppoe_tag*)ptn;
328154604Sglebius	}
32952419Sjulian
330154604Sglebius	CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
331154604Sglebius	return (NULL);
33252419Sjulian}
33352419Sjulian
33452419Sjulian/**************************************************************************
335154604Sglebius * Inlines to initialise or add tags to a session's tag list.
33652419Sjulian **************************************************************************/
33752419Sjulian/*
338154604Sglebius * Initialise the session's tag list.
33952419Sjulian */
34052419Sjulianstatic void
34152419Sjulianinit_tags(sessp sp)
34252419Sjulian{
343154604Sglebius	KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
34452419Sjulian	sp->neg->numtags = 0;
34552419Sjulian}
34652419Sjulian
34752419Sjulianstatic void
34897897Sarchieinsert_tag(sessp sp, const struct pppoe_tag *tp)
34952419Sjulian{
350154604Sglebius	negp neg = sp->neg;
351154604Sglebius	int i;
35252419Sjulian
353154604Sglebius	KASSERT(neg != NULL, ("%s: no neg", __func__));
35452419Sjulian	if ((i = neg->numtags++) < NUMTAGS) {
35552419Sjulian		neg->tags[i] = tp;
35652419Sjulian	} else {
357161034Sglebius		log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
358161034Sglebius		    "packet\n");
35953042Sjulian		neg->numtags--;
36052419Sjulian	}
36152419Sjulian}
36252419Sjulian
36352419Sjulian/*
36452419Sjulian * Make up a packet, using the tags filled out for the session.
36552419Sjulian *
366154604Sglebius * Assume that the actual pppoe header and ethernet header
36752419Sjulian * are filled out externally to this routine.
368154604Sglebius * Also assume that neg->wh points to the correct
36952419Sjulian * location at the front of the buffer space.
37052419Sjulian */
37152419Sjulianstatic void
37252419Sjulianmake_packet(sessp sp) {
37352419Sjulian	struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
37497897Sarchie	const struct pppoe_tag **tag;
37552419Sjulian	char *dp;
37652419Sjulian	int count;
37752419Sjulian	int tlen;
378154604Sglebius	uint16_t length = 0;
37952419Sjulian
380154604Sglebius	KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
381161034Sglebius	    ("%s: called from wrong state", __func__));
382154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
383154604Sglebius
384189315Sed	dp = (char *)(&wh->ph + 1);
38552419Sjulian	for (count = 0, tag = sp->neg->tags;
386154604Sglebius	    ((count < sp->neg->numtags) && (count < NUMTAGS));
38752419Sjulian	    tag++, count++) {
38852419Sjulian		tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
38952419Sjulian		if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
390161034Sglebius			log(LOG_NOTICE, "ng_pppoe: tags too long\n");
39152419Sjulian			sp->neg->numtags = count;
39252419Sjulian			break;	/* XXX chop off what's too long */
39352419Sjulian		}
39497897Sarchie		bcopy(*tag, (char *)dp, tlen);
39552419Sjulian		length += tlen;
39652419Sjulian		dp += tlen;
39752419Sjulian	}
39852419Sjulian 	wh->ph.length = htons(length);
39952419Sjulian	sp->neg->m->m_len = length + sizeof(*wh);
40052419Sjulian	sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
40152419Sjulian}
40252419Sjulian
40352419Sjulian/**************************************************************************
404154862Sglebius * Routines to match a service.						  *
40552419Sjulian **************************************************************************/
406154862Sglebius
407154604Sglebius/*
40852419Sjulian * Find a hook that has a service string that matches that
409154862Sglebius * we are seeking. For now use a simple string.
41052419Sjulian * In the future we may need something like regexp().
411154862Sglebius *
412154862Sglebius * Null string is a wildcard (ANY service), according to RFC2516.
413154862Sglebius * And historical FreeBSD wildcard is also "*".
41452419Sjulian */
41580311Sbrian
41652419Sjulianstatic hook_p
417154862Sglebiuspppoe_match_svc(node_p node, const struct pppoe_tag *tag)
41852419Sjulian{
419176775Smav	const priv_p privp = NG_NODE_PRIVATE(node);
420176775Smav	sessp sp;
42152419Sjulian
422176775Smav	LIST_FOREACH(sp, &privp->listeners, sessions) {
423176775Smav		negp neg = sp->neg;
42452419Sjulian
425154862Sglebius		/* Empty Service-Name matches any service. */
426154862Sglebius		if (neg->service_len == 0)
427154862Sglebius			break;
42852419Sjulian
429154862Sglebius		/* Special case for a blank or "*" service name (wildcard). */
430154862Sglebius		if (neg->service_len == 1 && neg->service.data[0] == '*')
431154862Sglebius			break;
432154862Sglebius
43352419Sjulian		/* If the lengths don't match, that aint it. */
434154862Sglebius		if (neg->service_len != ntohs(tag->tag_len))
43552419Sjulian			continue;
43652419Sjulian
437189315Sed		if (strncmp((const char *)(tag + 1), neg->service.data,
438154862Sglebius		    ntohs(tag->tag_len)) == 0)
43980311Sbrian			break;
440154862Sglebius	}
441176775Smav	CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
442189315Sed	    sp?sp->hook:NULL, (const char *)(tag + 1));
44380311Sbrian
444176775Smav	return (sp?sp->hook:NULL);
445154862Sglebius}
446154862Sglebius
447154862Sglebius/*
448154862Sglebius * Broadcast the PADI packet in m0 to all listening hooks.
449154862Sglebius * This routine is called when a PADI with empty Service-Name
450154862Sglebius * tag is received. Client should receive PADOs with all
451154862Sglebius * available services.
452154862Sglebius */
453154862Sglebiusstatic int
454154862Sglebiuspppoe_broadcast_padi(node_p node, struct mbuf *m0)
455154862Sglebius{
456176775Smav	const priv_p privp = NG_NODE_PRIVATE(node);
457176775Smav	sessp sp;
458154862Sglebius	int error = 0;
459154862Sglebius
460176775Smav	LIST_FOREACH(sp, &privp->listeners, sessions) {
461154862Sglebius		struct mbuf *m;
462154862Sglebius
463243882Sglebius		m = m_dup(m0, M_NOWAIT);
464154862Sglebius		if (m == NULL)
465154862Sglebius			return (ENOMEM);
466176775Smav		NG_SEND_DATA_ONLY(error, sp->hook, m);
467154862Sglebius		if (error)
468154862Sglebius			return (error);
46952419Sjulian	}
470154604Sglebius
471154862Sglebius	return (0);
47252419Sjulian}
473154604Sglebius
474154862Sglebius/*
475154862Sglebius * Find a hook, which name equals to given service.
476154862Sglebius */
477154862Sglebiusstatic hook_p
478154862Sglebiuspppoe_find_svc(node_p node, const char *svc_name, int svc_len)
479154862Sglebius{
480176775Smav	const priv_p privp = NG_NODE_PRIVATE(node);
481176775Smav	sessp sp;
482154862Sglebius
483176775Smav	LIST_FOREACH(sp, &privp->listeners, sessions) {
484176775Smav		negp neg = sp->neg;
485154862Sglebius
486154862Sglebius		if (neg->service_len == svc_len &&
487174931Smav		    strncmp(svc_name, neg->service.data, svc_len) == 0)
488176775Smav			return (sp->hook);
489154862Sglebius	}
490154862Sglebius
491154862Sglebius	return (NULL);
492154862Sglebius}
493154862Sglebius
49452419Sjulian/**************************************************************************
495175865Smav * Routines to find a particular session that matches an incoming packet. *
49652419Sjulian **************************************************************************/
497176753Smav/* Find free session and add to hash. */
498176753Smavstatic uint16_t
499176753Smavpppoe_getnewsession(sessp sp)
500176753Smav{
501176753Smav	const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
502176753Smav	static uint16_t pppoe_sid = 1;
503176753Smav	sessp	tsp;
504176753Smav	uint16_t val, hash;
505176753Smav
506176753Smavrestart:
507176753Smav	/* Atomicity is not needed here as value will be checked. */
508176753Smav	val = pppoe_sid++;
509176753Smav	/* Spec says 0xFFFF is reserved, also don't use 0x0000. */
510176753Smav	if (val == 0xffff || val == 0x0000)
511176753Smav		val = pppoe_sid = 1;
512176753Smav
513176753Smav	/* Check it isn't already in use. */
514176753Smav	hash = SESSHASH(val);
515176753Smav	mtx_lock(&privp->sesshash[hash].mtx);
516176775Smav	LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
517176753Smav		if (tsp->Session_ID == val)
518176753Smav			break;
519176753Smav	}
520176753Smav	if (!tsp) {
521176753Smav		sp->Session_ID = val;
522176775Smav		LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
523176753Smav	}
524176753Smav	mtx_unlock(&privp->sesshash[hash].mtx);
525176753Smav	if (tsp)
526176753Smav		goto restart;
527176753Smav
528176753Smav	CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
529176753Smav
530176753Smav	return (val);
531176753Smav}
532176753Smav
533175865Smav/* Add specified session to hash. */
534175865Smavstatic void
535175865Smavpppoe_addsession(sessp sp)
53652419Sjulian{
537175865Smav	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
538175865Smav	uint16_t	hash = SESSHASH(sp->Session_ID);
53952419Sjulian
540175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
541176775Smav	LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
542175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
543175865Smav}
544172270Smav
545175865Smav/* Delete specified session from hash. */
546175865Smavstatic void
547175865Smavpppoe_delsession(sessp sp)
548175865Smav{
549175865Smav	const priv_p	privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
550175865Smav	uint16_t	hash = SESSHASH(sp->Session_ID);
551175865Smav
552175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
553176775Smav	LIST_REMOVE(sp, sessions);
554175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
555175865Smav}
556175865Smav
557175865Smav/* Find matching peer/session combination. */
558175865Smavstatic sessp
559175865Smavpppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
560175865Smav{
561175865Smav	uint16_t 	session = ntohs(wh->ph.sid);
562175865Smav	uint16_t	hash = SESSHASH(session);
563175865Smav	sessp		sp = NULL;
564175865Smav
565175865Smav	mtx_lock(&privp->sesshash[hash].mtx);
566176775Smav	LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
567172270Smav		if (sp->Session_ID == session &&
568172270Smav		    bcmp(sp->pkt_hdr.eh.ether_dhost,
569172270Smav		     wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
57052419Sjulian			break;
57152419Sjulian		}
57252419Sjulian	}
573175865Smav	mtx_unlock(&privp->sesshash[hash].mtx);
574175867Smav	CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
575175867Smav	    session);
576154604Sglebius
577175865Smav	return (sp);
57852419Sjulian}
57952419Sjulian
58052419Sjulianstatic hook_p
58197897Sarchiepppoe_finduniq(node_p node, const struct pppoe_tag *tag)
58252419Sjulian{
583154604Sglebius	hook_p	hook = NULL;
584154604Sglebius	union uniq uniq;
58552419Sjulian
586189315Sed	bcopy(tag + 1, uniq.bytes, sizeof(void *));
587154604Sglebius	/* Cycle through all known hooks. */
58870784Sjulian	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
589172270Smav		/* Skip any nonsession hook. */
590172270Smav		if (NG_HOOK_PRIVATE(hook) == NULL)
59152419Sjulian			continue;
59270784Sjulian		if (uniq.pointer == NG_HOOK_PRIVATE(hook))
59352419Sjulian			break;
59452419Sjulian	}
595154604Sglebius	CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, uniq.pointer);
596154604Sglebius
59752419Sjulian	return (hook);
59852419Sjulian}
59952419Sjulian
60052419Sjulian/**************************************************************************
601154604Sglebius * Start of Netgraph entrypoints.					  *
60252419Sjulian **************************************************************************/
60352419Sjulian
60452419Sjulian/*
605154604Sglebius * Allocate the private data structure and link it with node.
60652419Sjulian */
60752419Sjulianstatic int
60870700Sjulianng_pppoe_constructor(node_p node)
60952419Sjulian{
610175865Smav	priv_p	privp;
611175865Smav	int	i;
61252419Sjulian
613154604Sglebius	/* Initialize private descriptor. */
614220768Sglebius	privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_ZERO);
61552419Sjulian
616154604Sglebius	/* Link structs together; this counts as our one reference to *node. */
617154604Sglebius	NG_NODE_SET_PRIVATE(node, privp);
618154604Sglebius	privp->node = node;
619132703Sglebius
620154901Sglebius	/* Initialize to standard mode. */
621161117Sglebius	memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
622161117Sglebius	privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
623154604Sglebius
624176775Smav	LIST_INIT(&privp->listeners);
625175865Smav	for (i = 0; i < SESSHASHSIZE; i++) {
626175865Smav	    mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
627176775Smav	    LIST_INIT(&privp->sesshash[i].head);
628175865Smav	}
629175865Smav
630154604Sglebius	CTR3(KTR_NET, "%20s: created node [%x] (%p)",
631154604Sglebius	    __func__, node->nd_ID, node);
632154604Sglebius
63352419Sjulian	return (0);
63452419Sjulian}
63552419Sjulian
63652419Sjulian/*
63752419Sjulian * Give our ok for a hook to be added...
63852419Sjulian * point the hook's private info to the hook structure.
63952419Sjulian *
64052419Sjulian * The following hook names are special:
641154604Sglebius *  "ethernet":  the hook that should be connected to a NIC.
642154604Sglebius *  "debug":	copies of data sent out here  (when I write the code).
64369922Sjulian * All other hook names need only be unique. (the framework checks this).
64452419Sjulian */
64552419Sjulianstatic int
64652562Sjulianng_pppoe_newhook(node_p node, hook_p hook, const char *name)
64752419Sjulian{
64870784Sjulian	const priv_p privp = NG_NODE_PRIVATE(node);
64952419Sjulian	sessp sp;
65052419Sjulian
65152419Sjulian	if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
65252419Sjulian		privp->ethernet_hook = hook;
653172629Smav		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
65452419Sjulian	} else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
65552419Sjulian		privp->debug_hook = hook;
656172629Smav		NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
65752419Sjulian	} else {
65852419Sjulian		/*
65952419Sjulian		 * Any other unique name is OK.
66052419Sjulian		 * The infrastructure has already checked that it's unique,
66152419Sjulian		 * so just allocate it and hook it in.
66252419Sjulian		 */
663154604Sglebius		sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
664154604Sglebius		if (sp == NULL)
665154604Sglebius			return (ENOMEM);
66652419Sjulian
66770784Sjulian		NG_HOOK_SET_PRIVATE(hook, sp);
66852419Sjulian		sp->hook = hook;
66952419Sjulian	}
670154604Sglebius	CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
671154604Sglebius	    __func__, node->nd_ID, node, name, hook);
672154604Sglebius
67352419Sjulian	return(0);
67452419Sjulian}
67552419Sjulian
67652419Sjulian/*
677161117Sglebius * Hook has been added successfully. Request the MAC address of
678161117Sglebius * the underlying Ethernet node.
679161117Sglebius */
680161117Sglebiusstatic int
681161117Sglebiusng_pppoe_connect(hook_p hook)
682161117Sglebius{
683161117Sglebius	const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
684161117Sglebius	struct ng_mesg *msg;
685161181Sglebius	int error;
686161117Sglebius
687161117Sglebius	if (hook != privp->ethernet_hook)
688161117Sglebius		return (0);
689161117Sglebius
690161117Sglebius	/*
691161117Sglebius	 * If this is Ethernet hook, then request MAC address
692161117Sglebius	 * from our downstream.
693161117Sglebius	 */
694161117Sglebius	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
695161117Sglebius	if (msg == NULL)
696161117Sglebius		return (ENOBUFS);
697161117Sglebius
698161117Sglebius	/*
699161117Sglebius	 * Our hook and peer hook have HK_INVALID flag set,
700161117Sglebius	 * so we can't use NG_SEND_MSG_HOOK() macro here.
701161117Sglebius	 */
702161117Sglebius	NG_SEND_MSG_ID(error, privp->node, msg,
703161117Sglebius	    NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
704161117Sglebius	    NG_NODE_ID(privp->node));
705161117Sglebius
706161117Sglebius	return (error);
707161117Sglebius}
708161117Sglebius/*
70952419Sjulian * Get a netgraph control message.
71052419Sjulian * Check it is one we understand. If needed, send a response.
71152419Sjulian * We sometimes save the address for an async action later.
71252419Sjulian * Always free the message.
71352419Sjulian */
71452419Sjulianstatic int
71570700Sjulianng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
71652419Sjulian{
71770784Sjulian	priv_p privp = NG_NODE_PRIVATE(node);
71852562Sjulian	struct ngpppoe_init_data *ourmsg = NULL;
71952419Sjulian	struct ng_mesg *resp = NULL;
72052419Sjulian	int error = 0;
72152419Sjulian	hook_p hook = NULL;
72252419Sjulian	sessp sp = NULL;
72352419Sjulian	negp neg = NULL;
72470700Sjulian	struct ng_mesg *msg;
72552419Sjulian
72670700Sjulian	NGI_GET_MSG(item, msg);
727154604Sglebius	CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
728154604Sglebius	    __func__, node->nd_ID, node, msg->header.cmd,
729154604Sglebius	    msg->header.typecookie);
730154604Sglebius
731154604Sglebius	/* Deal with message according to cookie and command. */
73252419Sjulian	switch (msg->header.typecookie) {
733154604Sglebius	case NGM_PPPOE_COOKIE:
73452419Sjulian		switch (msg->header.cmd) {
73552419Sjulian		case NGM_PPPOE_CONNECT:
736154604Sglebius		case NGM_PPPOE_LISTEN:
737154604Sglebius		case NGM_PPPOE_OFFER:
738154604Sglebius		case NGM_PPPOE_SERVICE:
73968845Sbrian			ourmsg = (struct ngpppoe_init_data *)msg->data;
74068845Sbrian			if (msg->header.arglen < sizeof(*ourmsg)) {
741161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: init data too "
742161034Sglebius				    "small\n", node->nd_ID);
74368845Sbrian				LEAVE(EMSGSIZE);
74468031Sbrian			}
745154604Sglebius			if (msg->header.arglen - sizeof(*ourmsg) >
74668031Sbrian			    PPPOE_SERVICE_NAME_SIZE) {
747161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: service name "
748161034Sglebius				    "too big\n", node->nd_ID);
74952419Sjulian				LEAVE(EMSGSIZE);
75052419Sjulian			}
75168845Sbrian			if (msg->header.arglen - sizeof(*ourmsg) <
75268845Sbrian			    ourmsg->data_len) {
753161034Sglebius				log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
754161034Sglebius				    "length, %d should be %zd\n", node->nd_ID,
755161034Sglebius				    ourmsg->data_len,
75668845Sbrian				    msg->header.arglen - sizeof (*ourmsg));
75752419Sjulian				LEAVE(EMSGSIZE);
75852419Sjulian			}
75968031Sbrian
760154604Sglebius			/* Make sure strcmp will terminate safely. */
76152419Sjulian			ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
76252419Sjulian
763176775Smav			/* Find hook by name. */
764176775Smav			hook = ng_findhook(node, ourmsg->hook);
765154604Sglebius			if (hook == NULL)
76652419Sjulian				LEAVE(ENOENT);
767154604Sglebius
768172270Smav			sp = NG_HOOK_PRIVATE(hook);
769172270Smav			if (sp == NULL)
77052419Sjulian				LEAVE(EINVAL);
771154604Sglebius
77280311Sbrian			if (msg->header.cmd == NGM_PPPOE_LISTEN) {
77380311Sbrian				/*
77480311Sbrian				 * Ensure we aren't already listening for this
77580311Sbrian				 * service.
77680311Sbrian				 */
777154862Sglebius				if (pppoe_find_svc(node, ourmsg->data,
778154862Sglebius				    ourmsg->data_len) != NULL)
77980311Sbrian					LEAVE(EEXIST);
78080311Sbrian			}
78180311Sbrian
78269922Sjulian			/*
78369922Sjulian			 * PPPOE_SERVICE advertisments are set up
78469922Sjulian			 * on sessions that are in PRIMED state.
78569922Sjulian			 */
786154604Sglebius			if (msg->header.cmd == NGM_PPPOE_SERVICE)
78769922Sjulian				break;
788154604Sglebius
789150319Sglebius			if (sp->state != PPPOE_SNONE) {
790161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
791161034Sglebius				    "active\n", node->nd_ID);
79252419Sjulian				LEAVE(EISCONN);
79352419Sjulian			}
79452443Sjulian
79552419Sjulian			/*
796154604Sglebius			 * Set up prototype header.
79752419Sjulian			 */
798154604Sglebius			neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
79968876Sdwmalone			    M_NOWAIT | M_ZERO);
80052419Sjulian
801154604Sglebius			if (neg == NULL)
80252419Sjulian				LEAVE(ENOMEM);
803154604Sglebius
804243882Sglebius			neg->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
805154604Sglebius			if (neg->m == NULL) {
806154604Sglebius				free(neg, M_NETGRAPH_PPPOE);
80752419Sjulian				LEAVE(ENOBUFS);
80852419Sjulian			}
80952419Sjulian			neg->m->m_pkthdr.rcvif = NULL;
81052419Sjulian			sp->neg = neg;
811138562Sglebius			ng_callout_init(&neg->handle);
81252419Sjulian			neg->m->m_len = sizeof(struct pppoe_full_hdr);
81352419Sjulian			neg->pkt = mtod(neg->m, union packet*);
814132703Sglebius			memcpy((void *)&neg->pkt->pkt_header.eh,
815161117Sglebius			    &privp->eh, sizeof(struct ether_header));
81652419Sjulian			neg->pkt->pkt_header.ph.ver = 0x1;
81752419Sjulian			neg->pkt->pkt_header.ph.type = 0x1;
81852419Sjulian			neg->pkt->pkt_header.ph.sid = 0x0000;
81952419Sjulian			neg->timeout = 0;
82052419Sjulian
82170700Sjulian			sp->creator = NGI_RETADDR(item);
82252419Sjulian		}
82352419Sjulian		switch (msg->header.cmd) {
82452419Sjulian		case NGM_PPPOE_GET_STATUS:
82552419Sjulian		    {
82652562Sjulian			struct ngpppoestat *stats;
82752419Sjulian
82852419Sjulian			NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
829154604Sglebius			if (!resp)
83052419Sjulian				LEAVE(ENOMEM);
831154604Sglebius
83252562Sjulian			stats = (struct ngpppoestat *) resp->data;
83352419Sjulian			stats->packets_in = privp->packets_in;
83452419Sjulian			stats->packets_out = privp->packets_out;
83552419Sjulian			break;
83652419Sjulian		    }
83752419Sjulian		case NGM_PPPOE_CONNECT:
838174981Smav		    {
83952419Sjulian			/*
84052419Sjulian			 * Check the hook exists and is Uninitialised.
84152419Sjulian			 * Send a PADI request, and start the timeout logic.
84252419Sjulian			 * Store the originator of this message so we can send
84352419Sjulian			 * a success of fail message to them later.
844154604Sglebius			 * Move the session to SINIT.
84552419Sjulian			 * Set up the session to the correct state and
84652419Sjulian			 * start it.
84752419Sjulian			 */
848174981Smav			int	i, acnlen = 0, acnsep = 0, srvlen;
849174981Smav			for (i = 0; i < ourmsg->data_len; i++) {
850174981Smav				if (ourmsg->data[i] == '\\') {
851174981Smav					acnlen = i;
852174981Smav					acnsep = 1;
853174981Smav					break;
854174981Smav				}
855174981Smav			}
856174981Smav			srvlen = ourmsg->data_len - acnlen - acnsep;
857174981Smav
858174981Smav			bcopy(ourmsg->data, neg->ac_name.data, acnlen);
859174981Smav			neg->ac_name_len = acnlen;
860174981Smav
86152419Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
862174981Smav			neg->service.hdr.tag_len = htons((uint16_t)srvlen);
863174981Smav			bcopy(ourmsg->data + acnlen + acnsep,
864174981Smav			    neg->service.data, srvlen);
865174981Smav			neg->service_len = srvlen;
86652419Sjulian			pppoe_start(sp);
86752419Sjulian			break;
868174981Smav		    }
86952419Sjulian		case NGM_PPPOE_LISTEN:
87052419Sjulian			/*
87152419Sjulian			 * Check the hook exists and is Uninitialised.
87252419Sjulian			 * Install the service matching string.
87352419Sjulian			 * Store the originator of this message so we can send
87452419Sjulian			 * a success of fail message to them later.
87552419Sjulian			 * Move the hook to 'LISTENING'
87652419Sjulian			 */
87752419Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
87868845Sbrian			neg->service.hdr.tag_len =
879154604Sglebius			    htons((uint16_t)ourmsg->data_len);
88052443Sjulian
88168845Sbrian			if (ourmsg->data_len)
88268845Sbrian				bcopy(ourmsg->data, neg->service.data,
88368845Sbrian				    ourmsg->data_len);
88468845Sbrian			neg->service_len = ourmsg->data_len;
88552419Sjulian			neg->pkt->pkt_header.ph.code = PADT_CODE;
88652419Sjulian			/*
887154604Sglebius			 * Wait for PADI packet coming from Ethernet.
88852419Sjulian			 */
88952419Sjulian			sp->state = PPPOE_LISTENING;
890176775Smav			LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
89152419Sjulian			break;
89252419Sjulian		case NGM_PPPOE_OFFER:
89352419Sjulian			/*
89452419Sjulian			 * Check the hook exists and is Uninitialised.
89552419Sjulian			 * Store the originator of this message so we can send
89652419Sjulian			 * a success of fail message to them later.
89752419Sjulian			 * Store the AC-Name given and go to PRIMED.
89852419Sjulian			 */
89952419Sjulian			neg->ac_name.hdr.tag_type = PTT_AC_NAME;
90068845Sbrian			neg->ac_name.hdr.tag_len =
901154604Sglebius			    htons((uint16_t)ourmsg->data_len);
90268845Sbrian			if (ourmsg->data_len)
90368845Sbrian				bcopy(ourmsg->data, neg->ac_name.data,
90468845Sbrian				    ourmsg->data_len);
90568845Sbrian			neg->ac_name_len = ourmsg->data_len;
90652419Sjulian			neg->pkt->pkt_header.ph.code = PADO_CODE;
90752419Sjulian			/*
908154604Sglebius			 * Wait for PADI packet coming from hook.
90952419Sjulian			 */
91052419Sjulian			sp->state = PPPOE_PRIMED;
91152419Sjulian			break;
912154604Sglebius		case NGM_PPPOE_SERVICE:
913154604Sglebius			/*
91469922Sjulian			 * Check the session is primed.
91569922Sjulian			 * for now just allow ONE service to be advertised.
91669922Sjulian			 * If you do it twice you just overwrite.
91769922Sjulian			 */
91870148Sjulian			if (sp->state != PPPOE_PRIMED) {
919161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: session not "
920161034Sglebius				    "primed\n", node->nd_ID);
92169922Sjulian				LEAVE(EISCONN);
92269922Sjulian			}
92370931Sjulian			neg = sp->neg;
92469922Sjulian			neg->service.hdr.tag_type = PTT_SRV_NAME;
92569922Sjulian			neg->service.hdr.tag_len =
926154604Sglebius			    htons((uint16_t)ourmsg->data_len);
92769922Sjulian
92869922Sjulian			if (ourmsg->data_len)
92969922Sjulian				bcopy(ourmsg->data, neg->service.data,
93069922Sjulian				    ourmsg->data_len);
93169922Sjulian			neg->service_len = ourmsg->data_len;
93269922Sjulian			break;
933132703Sglebius		case NGM_PPPOE_SETMODE:
934132703Sglebius		    {
935132703Sglebius			char *s;
936132703Sglebius			size_t len;
937132703Sglebius
938132703Sglebius			if (msg->header.arglen == 0)
939132703Sglebius				LEAVE(EINVAL);
940132703Sglebius
941132703Sglebius			s = (char *)msg->data;
942132703Sglebius			len = msg->header.arglen - 1;
943132703Sglebius
944154604Sglebius			/* Search for matching mode string. */
945154901Sglebius			if (len == strlen(NG_PPPOE_STANDARD) &&
946154901Sglebius			    (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
947154901Sglebius				privp->flags = 0;
948161117Sglebius				privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
949154901Sglebius				break;
950154901Sglebius			}
951154901Sglebius			if (len == strlen(NG_PPPOE_3COM) &&
952154901Sglebius			    (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
953154901Sglebius				privp->flags |= COMPAT_3COM;
954161117Sglebius				privp->eh.ether_type =
955161117Sglebius				    ETHERTYPE_PPPOE_3COM_DISC;
956154901Sglebius				break;
957154901Sglebius			}
958154901Sglebius			if (len == strlen(NG_PPPOE_DLINK) &&
959154901Sglebius			    (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
960154901Sglebius				privp->flags |= COMPAT_DLINK;
961154901Sglebius				break;
962154901Sglebius			}
963154901Sglebius			error = EINVAL;
964132703Sglebius			break;
965132703Sglebius		    }
966132703Sglebius		case NGM_PPPOE_GETMODE:
967154901Sglebius		    {
968154901Sglebius			char *s;
969154901Sglebius			size_t len = 0;
970154901Sglebius
971154901Sglebius			if (privp->flags == 0)
972154901Sglebius				len += strlen(NG_PPPOE_STANDARD) + 1;
973154901Sglebius			if (privp->flags & COMPAT_3COM)
974154901Sglebius				len += strlen(NG_PPPOE_3COM) + 1;
975154901Sglebius			if (privp->flags & COMPAT_DLINK)
976154901Sglebius				len += strlen(NG_PPPOE_DLINK) + 1;
977154901Sglebius
978154901Sglebius			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
979132703Sglebius			if (resp == NULL)
980132703Sglebius				LEAVE(ENOMEM);
981154901Sglebius
982154901Sglebius			s = (char *)resp->data;
983154901Sglebius			if (privp->flags == 0) {
984154901Sglebius				len = strlen(NG_PPPOE_STANDARD);
985154901Sglebius				strlcpy(s, NG_PPPOE_STANDARD, len + 1);
986154901Sglebius				break;
987154901Sglebius			}
988154901Sglebius			if (privp->flags & COMPAT_3COM) {
989154901Sglebius				len = strlen(NG_PPPOE_3COM);
990154901Sglebius				strlcpy(s, NG_PPPOE_3COM, len + 1);
991154901Sglebius				s += len;
992154901Sglebius			}
993154901Sglebius			if (privp->flags & COMPAT_DLINK) {
994154901Sglebius				if (s != resp->data)
995154901Sglebius					*s++ = '|';
996154901Sglebius				len = strlen(NG_PPPOE_DLINK);
997154901Sglebius				strlcpy(s, NG_PPPOE_DLINK, len + 1);
998154901Sglebius			}
999132703Sglebius			break;
1000154901Sglebius		    }
1001161117Sglebius		case NGM_PPPOE_SETENADDR:
1002161117Sglebius			if (msg->header.arglen != ETHER_ADDR_LEN)
1003161117Sglebius				LEAVE(EINVAL);
1004161117Sglebius			bcopy(msg->data, &privp->eh.ether_shost,
1005161117Sglebius			    ETHER_ADDR_LEN);
1006161117Sglebius			break;
100752419Sjulian		default:
100852419Sjulian			LEAVE(EINVAL);
100952419Sjulian		}
101052419Sjulian		break;
1011161117Sglebius	case NGM_ETHER_COOKIE:
1012161117Sglebius		if (!(msg->header.flags & NGF_RESP))
1013161117Sglebius			LEAVE(EINVAL);
1014161117Sglebius		switch (msg->header.cmd) {
1015161117Sglebius		case NGM_ETHER_GET_ENADDR:
1016161117Sglebius			if (msg->header.arglen != ETHER_ADDR_LEN)
1017161117Sglebius				LEAVE(EINVAL);
1018161117Sglebius			bcopy(msg->data, &privp->eh.ether_shost,
1019161117Sglebius			    ETHER_ADDR_LEN);
1020161117Sglebius			break;
1021161117Sglebius		default:
1022161181Sglebius			LEAVE(EINVAL);
1023161117Sglebius		}
1024161117Sglebius		break;
102552419Sjulian	default:
102652419Sjulian		LEAVE(EINVAL);
102752419Sjulian	}
102852419Sjulian
1029154604Sglebius	/* Take care of synchronous response, if any. */
103070700Sjulianquit:
1031154901Sglebius	CTR2(KTR_NET, "%20s: returning %d", __func__, error);
103270700Sjulian	NG_RESPOND_MSG(error, node, item, resp);
1033154604Sglebius	/* Free the message and return. */
103470700Sjulian	NG_FREE_MSG(msg);
103552419Sjulian	return(error);
103652419Sjulian}
103752419Sjulian
103852443Sjulian/*
103952443Sjulian * Start a client into the first state. A separate function because
104052443Sjulian * it can be needed if the negotiation times out.
104152443Sjulian */
104252419Sjulianstatic void
104352419Sjulianpppoe_start(sessp sp)
104452419Sjulian{
1045172628Smav	hook_p	hook = sp->hook;
1046172628Smav	node_p	node = NG_HOOK_NODE(hook);
1047172628Smav	priv_p	privp = NG_NODE_PRIVATE(node);
1048172628Smav	negp	neg = sp->neg;
104952419Sjulian	struct {
105052419Sjulian		struct pppoe_tag hdr;
105152419Sjulian		union	uniq	data;
1052103870Salfred	} __packed uniqtag;
1053172628Smav	struct  mbuf *m0;
1054172628Smav	int	error;
105552419Sjulian
1056154604Sglebius	/*
1057154604Sglebius	 * Kick the state machine into starting up.
105852419Sjulian	 */
1059154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
106052419Sjulian	sp->state = PPPOE_SINIT;
1061154604Sglebius	/*
1062154604Sglebius	 * Reset the packet header to broadcast. Since we are
1063161117Sglebius	 * in a client mode use configured ethertype.
1064154604Sglebius	 */
1065172628Smav	memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1066161117Sglebius	    sizeof(struct ether_header));
1067172628Smav	neg->pkt->pkt_header.ph.code = PADI_CODE;
106852419Sjulian	uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
106952419Sjulian	uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
107052419Sjulian	uniqtag.data.pointer = sp;
107152419Sjulian	init_tags(sp);
107268079Sjulian	insert_tag(sp, &uniqtag.hdr);
1073172628Smav	insert_tag(sp, &neg->service.hdr);
107452419Sjulian	make_packet(sp);
1075172628Smav	/*
1076172628Smav	 * Send packet and prepare to retransmit it after timeout.
1077172628Smav	 */
1078172628Smav	ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
1079172628Smav	    pppoe_ticker, NULL, 0);
1080172628Smav	neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1081243882Sglebius	m0 = m_copypacket(neg->m, M_NOWAIT);
1082172628Smav	NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
108352419Sjulian}
108452419Sjulian
108590973Sbrianstatic int
108697897Sarchiesend_acname(sessp sp, const struct pppoe_tag *tag)
108790973Sbrian{
108898636Sbrian	int error, tlen;
108990973Sbrian	struct ng_mesg *msg;
109090973Sbrian	struct ngpppoe_sts *sts;
109190973Sbrian
1092154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1093154604Sglebius
109490973Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
109590973Sbrian	    sizeof(struct ngpppoe_sts), M_NOWAIT);
109690973Sbrian	if (msg == NULL)
109790973Sbrian		return (ENOMEM);
109890973Sbrian
109990973Sbrian	sts = (struct ngpppoe_sts *)msg->data;
1100125028Sharti	tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
1101189315Sed	strncpy(sts->hook, (const char *)(tag + 1), tlen);
110298636Sbrian	sts->hook[tlen] = '\0';
1103102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
110490973Sbrian
110590973Sbrian	return (error);
110690973Sbrian}
110790973Sbrian
110896578Sbrianstatic int
110996578Sbriansend_sessionid(sessp sp)
111096578Sbrian{
111196578Sbrian	int error;
111296578Sbrian	struct ng_mesg *msg;
111396578Sbrian
1114154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1115154604Sglebius
111696578Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
1117154604Sglebius	    sizeof(uint16_t), M_NOWAIT);
111896578Sbrian	if (msg == NULL)
111996578Sbrian		return (ENOMEM);
112096578Sbrian
1121154604Sglebius	*(uint16_t *)msg->data = sp->Session_ID;
1122102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
112396578Sbrian
112496578Sbrian	return (error);
112596578Sbrian}
112696578Sbrian
112752419Sjulian/*
1128172629Smav * Receive data from session hook and do something with it.
112952419Sjulian */
113052419Sjulianstatic int
113170700Sjulianng_pppoe_rcvdata(hook_p hook, item_p item)
113252419Sjulian{
113370784Sjulian	node_p			node = NG_HOOK_NODE(hook);
113470784Sjulian	const priv_p		privp = NG_NODE_PRIVATE(node);
113570784Sjulian	sessp			sp = NG_HOOK_PRIVATE(hook);
1136172629Smav	struct pppoe_full_hdr	*wh;
1137154604Sglebius	struct mbuf		*m;
1138172629Smav	int			error;
113952419Sjulian
1140154604Sglebius	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1141154604Sglebius	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1142154604Sglebius
114370700Sjulian	NGI_GET_M(item, m);
1144172629Smav	switch (sp->state) {
1145172629Smav	case	PPPOE_NEWCONNECTED:
1146172629Smav	case	PPPOE_CONNECTED: {
114752419Sjulian		/*
1148172629Smav		 * Remove PPP address and control fields, if any.
1149172629Smav		 * For example, ng_ppp(4) always sends LCP packets
1150172629Smav		 * with address and control fields as required by
1151172629Smav		 * generic PPP. PPPoE is an exception to the rule.
115252419Sjulian		 */
1153172629Smav		if (m->m_pkthdr.len >= 2) {
1154172629Smav			if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1155172629Smav				LEAVE(ENOBUFS);
1156176057Smav			if (mtod(m, u_char *)[0] == 0xff &&
1157176057Smav			    mtod(m, u_char *)[1] == 0x03)
1158172629Smav				m_adj(m, 2);
1159172629Smav		}
116052419Sjulian		/*
1161172629Smav		 * Bang in a pre-made header, and set the length up
1162172629Smav		 * to be correct. Then send it to the ethernet driver.
116352419Sjulian		 */
1164243882Sglebius		M_PREPEND(m, sizeof(*wh), M_NOWAIT);
1165172629Smav		if (m == NULL)
1166172629Smav			LEAVE(ENOBUFS);
1167172629Smav
1168172629Smav		wh = mtod(m, struct pppoe_full_hdr *);
1169172629Smav		bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1170172629Smav		wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1171172629Smav		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
1172172629Smav		privp->packets_out++;
1173172629Smav		break;
1174172629Smav		}
1175172629Smav	case	PPPOE_PRIMED: {
1176172629Smav		struct {
1177172629Smav			struct pppoe_tag hdr;
1178172629Smav			union	uniq	data;
1179172629Smav		} __packed 	uniqtag;
1180172629Smav		const struct pppoe_tag	*tag;
1181172629Smav		struct mbuf 	*m0;
1182172629Smav		const struct pppoe_hdr	*ph;
1183172629Smav		negp		neg = sp->neg;
1184172629Smav	        uint16_t	session;
1185172629Smav		uint16_t	length;
1186172629Smav		uint8_t		code;
1187172629Smav
1188172629Smav		/*
1189172629Smav		 * A PADI packet is being returned by the application
1190172629Smav		 * that has set up this hook. This indicates that it
1191172629Smav		 * wants us to offer service.
1192172629Smav		 */
1193172629Smav		if (m->m_len < sizeof(*wh)) {
1194172629Smav			m = m_pullup(m, sizeof(*wh));
1195172629Smav			if (m == NULL)
119652510Sjulian				LEAVE(ENOBUFS);
119752419Sjulian		}
119852419Sjulian		wh = mtod(m, struct pppoe_full_hdr *);
1199172629Smav		ph = &wh->ph;
1200172629Smav		session = ntohs(wh->ph.sid);
120152419Sjulian		length = ntohs(wh->ph.length);
1202172629Smav		code = wh->ph.code;
1203172629Smav		/* Use peers mode in session. */
1204172629Smav		neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
1205172629Smav		if (code != PADI_CODE)
1206172629Smav			LEAVE(EINVAL);
1207172629Smav		ng_uncallout(&neg->handle, node);
120852419Sjulian
1209172629Smav		/*
1210172629Smav		 * This is the first time we hear
1211172629Smav		 * from the client, so note it's
1212172629Smav		 * unicast address, replacing the
1213172629Smav		 * broadcast address.
1214172629Smav		 */
1215172629Smav		bcopy(wh->eh.ether_shost,
1216172629Smav			neg->pkt->pkt_header.eh.ether_dhost,
1217172629Smav			ETHER_ADDR_LEN);
1218172629Smav		sp->state = PPPOE_SOFFER;
1219172629Smav		neg->timeout = 0;
1220172629Smav		neg->pkt->pkt_header.ph.code = PADO_CODE;
1221154862Sglebius
1222172629Smav		/*
1223172629Smav		 * Start working out the tags to respond with.
1224172629Smav		 */
1225172629Smav		uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1226172629Smav		uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1227172629Smav		uniqtag.data.pointer = sp;
1228172629Smav		init_tags(sp);
1229172629Smav		insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1230172629Smav		if ((tag = get_tag(ph, PTT_SRV_NAME)))
1231172629Smav			insert_tag(sp, tag);	  /* return service */
1232172629Smav		/*
1233172629Smav		 * If we have a NULL service request
1234172629Smav		 * and have an extra service defined in this hook,
1235172629Smav		 * then also add a tag for the extra service.
1236172629Smav		 * XXX this is a hack. eventually we should be able
1237172629Smav		 * to support advertising many services, not just one
1238172629Smav		 */
1239172629Smav		if (((tag == NULL) || (tag->tag_len == 0)) &&
1240172629Smav		    (neg->service.hdr.tag_len != 0)) {
1241172629Smav			insert_tag(sp, &neg->service.hdr); /* SERVICE */
1242172629Smav		}
1243172629Smav		if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1244172629Smav			insert_tag(sp, tag); /* returned hostunique */
1245172629Smav		insert_tag(sp, &uniqtag.hdr);
1246172629Smav		scan_tags(sp, ph);
1247172629Smav		make_packet(sp);
1248172629Smav		/*
1249172629Smav		 * Send the offer but if they don't respond
1250172629Smav		 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1251172629Smav		 */
1252172629Smav		ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
1253172629Smav		    pppoe_ticker, NULL, 0);
1254243882Sglebius		m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1255172629Smav		NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1256172629Smav		privp->packets_out++;
1257172629Smav		break;
1258172629Smav		}
125952419Sjulian
1260172629Smav	/*
1261172629Smav	 * Packets coming from the hook make no sense
1262172629Smav	 * to sessions in the rest of states. Throw them away.
1263172629Smav	 */
1264172629Smav	default:
1265172629Smav		LEAVE(ENETUNREACH);
1266172629Smav	}
1267172629Smavquit:
1268172629Smav	if (item)
1269172629Smav		NG_FREE_ITEM(item);
1270172629Smav	NG_FREE_M(m);
1271172629Smav	return (error);
1272172629Smav}
127352419Sjulian
1274172629Smav/*
1275172629Smav * Receive data from ether and do something with it.
1276172629Smav */
1277172629Smavstatic int
1278172629Smavng_pppoe_rcvdata_ether(hook_p hook, item_p item)
1279172629Smav{
1280172629Smav	node_p			node = NG_HOOK_NODE(hook);
1281172629Smav	const priv_p		privp = NG_NODE_PRIVATE(node);
1282208824Smav	sessp			sp;
1283172629Smav	const struct pppoe_tag	*utag = NULL, *tag = NULL;
1284172629Smav	const struct pppoe_full_hdr *wh;
1285172629Smav	const struct pppoe_hdr	*ph;
1286172629Smav	negp			neg = NULL;
1287172629Smav	struct mbuf		*m;
1288172629Smav	hook_p 			sendhook;
1289172629Smav	int			error = 0;
1290172629Smav	uint16_t		session;
1291172629Smav	uint16_t		length;
1292172629Smav	uint8_t			code;
1293172629Smav	struct	mbuf 		*m0;
129452419Sjulian
1295172629Smav	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1296172629Smav	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
129752419Sjulian
1298172629Smav	NGI_GET_M(item, m);
1299172629Smav	/*
1300172629Smav	 * Dig out various fields from the packet.
1301172629Smav	 * Use them to decide where to send it.
1302172629Smav	 */
1303172629Smav	privp->packets_in++;
1304172629Smav	if( m->m_len < sizeof(*wh)) {
1305172629Smav		m = m_pullup(m, sizeof(*wh)); /* Checks length */
1306172629Smav		if (m == NULL) {
1307172629Smav			log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
1308172629Smav			    "m_pullup(wh)\n", node->nd_ID);
1309172629Smav			LEAVE(ENOBUFS);
1310172629Smav		}
1311172629Smav	}
1312172629Smav	wh = mtod(m, struct pppoe_full_hdr *);
1313172629Smav	length = ntohs(wh->ph.length);
1314172629Smav	switch(wh->eh.ether_type) {
1315172629Smav	case	ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
1316172629Smav	case	ETHERTYPE_PPPOE_DISC:
1317172629Smav		/*
1318172629Smav		 * We need to try to make sure that the tag area
1319172629Smav		 * is contiguous, or we could wander off the end
1320172629Smav		 * of a buffer and make a mess.
1321172629Smav		 * (Linux wouldn't have this problem).
1322172629Smav		 */
1323172629Smav		if (m->m_pkthdr.len <= MHLEN) {
1324172629Smav			if( m->m_len < m->m_pkthdr.len) {
1325172629Smav				m = m_pullup(m, m->m_pkthdr.len);
1326172629Smav				if (m == NULL) {
1327172629Smav					log(LOG_NOTICE, "ng_pppoe[%x]: "
1328172629Smav					    "couldn't m_pullup(pkthdr)\n",
1329172629Smav					    node->nd_ID);
1330172629Smav					LEAVE(ENOBUFS);
133152419Sjulian				}
133252419Sjulian			}
1333172629Smav		}
1334172629Smav		if (m->m_len != m->m_pkthdr.len) {
133552419Sjulian			/*
1336172629Smav			 * It's not all in one piece.
1337172629Smav			 * We need to do extra work.
1338172629Smav			 * Put it into a cluster.
133952419Sjulian			 */
1340172629Smav			struct mbuf *n;
1341243882Sglebius			n = m_dup(m, M_NOWAIT);
1342172629Smav			m_freem(m);
1343172629Smav			m = n;
1344172629Smav			if (m) {
1345172629Smav				/* just check we got a cluster */
1346172629Smav				if (m->m_len != m->m_pkthdr.len) {
1347172629Smav					m_freem(m);
1348172629Smav					m = NULL;
1349172629Smav				}
135052419Sjulian			}
1351172629Smav			if (m == NULL) {
1352172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: packet "
1353172629Smav				    "fragmented\n", node->nd_ID);
135452419Sjulian				LEAVE(EMSGSIZE);
135552419Sjulian			}
135652419Sjulian		}
1357172629Smav		wh = mtod(m, struct pppoe_full_hdr *);
1358172629Smav		length = ntohs(wh->ph.length);
1359172629Smav		ph = &wh->ph;
1360172629Smav		session = ntohs(wh->ph.sid);
1361172629Smav		code = wh->ph.code;
136264502Sarchie
1363172629Smav		switch(code) {
1364172629Smav		case	PADI_CODE:
136552419Sjulian			/*
1366172629Smav			 * We are a server:
1367172629Smav			 * Look for a hook with the required service and send
1368172629Smav			 * the ENTIRE packet up there. It should come back to
1369172629Smav			 * a new hook in PRIMED state. Look there for further
1370172629Smav			 * processing.
137164502Sarchie			 */
1372172629Smav			tag = get_tag(ph, PTT_SRV_NAME);
1373172629Smav			if (tag == NULL) {
1374172629Smav				CTR1(KTR_NET, "%20s: PADI w/o Service-Name",
1375172629Smav				    __func__);
1376172629Smav				LEAVE(ENETUNREACH);
137764502Sarchie			}
1378172629Smav
137964502Sarchie			/*
1380172629Smav			 * First, try to match Service-Name against our
1381172629Smav			 * listening hooks. If no success and we are in D-Link
1382172629Smav			 * compat mode and Service-Name is empty, then we
1383172629Smav			 * broadcast the PADI to all listening hooks.
138452419Sjulian			 */
1385172629Smav			sendhook = pppoe_match_svc(node, tag);
1386172629Smav			if (sendhook != NULL)
1387172629Smav				NG_FWD_NEW_DATA(error, item, sendhook, m);
1388172629Smav			else if (privp->flags & COMPAT_DLINK &&
1389172629Smav				 ntohs(tag->tag_len) == 0)
1390172629Smav				error = pppoe_broadcast_padi(node, m);
1391172629Smav			else
1392172629Smav				error = ENETUNREACH;
139352419Sjulian			break;
1394172629Smav		case	PADO_CODE:
1395172629Smav			/*
1396172629Smav			 * We are a client:
1397172629Smav			 * Use the host_uniq tag to find the hook this is in
1398172629Smav			 * response to. Received #2, now send #3
1399172629Smav			 * For now simply accept the first we receive.
1400172629Smav			 */
1401172629Smav			utag = get_tag(ph, PTT_HOST_UNIQ);
1402172629Smav			if ((utag == NULL) ||
1403172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1404172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: no host "
1405172629Smav				    "unique field\n", node->nd_ID);
1406172629Smav				LEAVE(ENETUNREACH);
140752419Sjulian			}
1408172629Smav
1409172629Smav			sendhook = pppoe_finduniq(node, utag);
1410172629Smav			if (sendhook == NULL) {
1411172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: no "
1412172629Smav				    "matching session\n", node->nd_ID);
1413172629Smav				LEAVE(ENETUNREACH);
1414172629Smav			}
1415172629Smav
141652419Sjulian			/*
1417172629Smav			 * Check the session is in the right state.
1418172629Smav			 * It needs to be in PPPOE_SINIT.
141952419Sjulian			 */
1420172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1421174981Smav			if (sp->state == PPPOE_SREQ ||
1422174981Smav			    sp->state == PPPOE_CONNECTED) {
1423174981Smav				break;	/* Multiple PADO is OK. */
1424174981Smav			}
1425172629Smav			if (sp->state != PPPOE_SINIT) {
1426172629Smav				log(LOG_NOTICE, "ng_pppoe[%x]: session "
1427172629Smav				    "in wrong state\n", node->nd_ID);
1428172629Smav				LEAVE(ENETUNREACH);
1429172629Smav			}
143052419Sjulian			neg = sp->neg;
1431174981Smav			/* If requested specific AC-name, check it. */
1432174981Smav			if (neg->ac_name_len) {
1433174981Smav				tag = get_tag(ph, PTT_AC_NAME);
1434174981Smav				if (!tag) {
1435174981Smav					/* No PTT_AC_NAME in PADO */
1436174981Smav					break;
1437174981Smav				}
1438174981Smav				if (neg->ac_name_len != htons(tag->tag_len) ||
1439189315Sed				    strncmp(neg->ac_name.data,
1440189315Sed				    (const char *)(tag + 1),
1441174981Smav				    neg->ac_name_len) != 0) {
1442174981Smav					break;
1443174981Smav				}
1444174981Smav			}
1445174981Smav			sp->state = PPPOE_SREQ;
1446138562Sglebius			ng_uncallout(&neg->handle, node);
144752419Sjulian
144852419Sjulian			/*
144952419Sjulian			 * This is the first time we hear
1450172629Smav			 * from the server, so note it's
145152419Sjulian			 * unicast address, replacing the
1452172629Smav			 * broadcast address .
145352419Sjulian			 */
145452419Sjulian			bcopy(wh->eh.ether_shost,
145552419Sjulian				neg->pkt->pkt_header.eh.ether_dhost,
145652419Sjulian				ETHER_ADDR_LEN);
145752419Sjulian			neg->timeout = 0;
1458172629Smav			neg->pkt->pkt_header.ph.code = PADR_CODE;
1459172629Smav			init_tags(sp);
1460172629Smav			insert_tag(sp, utag);      	/* Host Unique */
1461172629Smav			if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1462172629Smav				insert_tag(sp, tag); 	/* return cookie */
1463172629Smav			if ((tag = get_tag(ph, PTT_AC_NAME))) {
1464172629Smav				insert_tag(sp, tag); 	/* return it */
1465172629Smav				send_acname(sp, tag);
1466172629Smav			}
1467172629Smav			insert_tag(sp, &neg->service.hdr); /* Service */
1468172629Smav			scan_tags(sp, ph);
1469172629Smav			make_packet(sp);
1470172629Smav			sp->state = PPPOE_SREQ;
1471172629Smav			ng_callout(&neg->handle, node, sp->hook,
1472172629Smav			    PPPOE_INITIAL_TIMEOUT * hz,
1473172629Smav			    pppoe_ticker, NULL, 0);
1474172629Smav			neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1475243882Sglebius			m0 = m_copypacket(neg->m, M_NOWAIT);
1476172629Smav			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1477172629Smav			break;
1478172629Smav		case	PADR_CODE:
1479172629Smav			/*
1480172629Smav			 * We are a server:
1481172629Smav			 * Use the ac_cookie tag to find the
1482172629Smav			 * hook this is in response to.
1483172629Smav			 */
1484172629Smav			utag = get_tag(ph, PTT_AC_COOKIE);
1485172629Smav			if ((utag == NULL) ||
1486172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1487172629Smav				LEAVE(ENETUNREACH);
1488172629Smav			}
148952419Sjulian
1490172629Smav			sendhook = pppoe_finduniq(node, utag);
1491172629Smav			if (sendhook == NULL)
1492172629Smav				LEAVE(ENETUNREACH);
1493172629Smav
149452419Sjulian			/*
1495172629Smav			 * Check the session is in the right state.
1496172629Smav			 * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
1497172629Smav			 * If the latter, then this is a retry by the client,
1498172629Smav			 * so be nice, and resend.
149952419Sjulian			 */
1500172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1501172629Smav			if (sp->state == PPPOE_NEWCONNECTED) {
1502172629Smav				/*
1503172629Smav				 * Whoa! drop back to resend that PADS packet.
1504172629Smav				 * We should still have a copy of it.
1505172629Smav				 */
1506172629Smav				sp->state = PPPOE_SOFFER;
1507175865Smav			} else if (sp->state != PPPOE_SOFFER)
1508172629Smav				LEAVE (ENETUNREACH);
1509172629Smav			neg = sp->neg;
1510172629Smav			ng_uncallout(&neg->handle, node);
1511172629Smav			neg->pkt->pkt_header.ph.code = PADS_CODE;
1512175865Smav			if (sp->Session_ID == 0) {
1513172629Smav				neg->pkt->pkt_header.ph.sid =
1514176753Smav				    htons(pppoe_getnewsession(sp));
1515175865Smav			}
1516172629Smav			send_sessionid(sp);
1517172629Smav			neg->timeout = 0;
1518172629Smav			/*
1519172629Smav			 * start working out the tags to respond with.
1520172629Smav			 */
152152419Sjulian			init_tags(sp);
152252419Sjulian			insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
152353154Sjulian			if ((tag = get_tag(ph, PTT_SRV_NAME)))
1524172629Smav				insert_tag(sp, tag);/* return service */
152553154Sjulian			if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1526172629Smav				insert_tag(sp, tag); /* return it */
1527172629Smav			insert_tag(sp, utag);	/* ac_cookie */
152852419Sjulian			scan_tags(sp, ph);
152952419Sjulian			make_packet(sp);
1530172629Smav			sp->state = PPPOE_NEWCONNECTED;
1531172629Smav
1532172629Smav			/* Send the PADS without a timeout - we're now connected. */
1533243882Sglebius			m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1534172629Smav			NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
1535172629Smav
1536172628Smav			/*
1537172629Smav			 * Having sent the last Negotiation header,
1538172629Smav			 * Set up the stored packet header to be correct for
1539172629Smav			 * the actual session. But keep the negotialtion stuff
1540172629Smav			 * around in case we need to resend this last packet.
1541172629Smav			 * We'll discard it when we move from NEWCONNECTED
1542172629Smav			 * to CONNECTED
1543172628Smav			 */
1544172629Smav			sp->pkt_hdr = neg->pkt->pkt_header;
1545172629Smav			/* Configure ethertype depending on what
1546172629Smav			 * ethertype was used at discovery phase */
1547172629Smav			if (sp->pkt_hdr.eh.ether_type ==
1548172629Smav			    ETHERTYPE_PPPOE_3COM_DISC)
1549172629Smav				sp->pkt_hdr.eh.ether_type
1550172629Smav					= ETHERTYPE_PPPOE_3COM_SESS;
1551172629Smav			else
1552172629Smav				sp->pkt_hdr.eh.ether_type
1553172629Smav					= ETHERTYPE_PPPOE_SESS;
1554172629Smav			sp->pkt_hdr.ph.code = 0;
1555172629Smav			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
155652419Sjulian			break;
1557172629Smav		case	PADS_CODE:
1558172629Smav			/*
1559172629Smav			 * We are a client:
1560172629Smav			 * Use the host_uniq tag to find the hook this is in
1561172629Smav			 * response to. Take the session ID and store it away.
1562172629Smav			 * Also make sure the pre-made header is correct and
1563172629Smav			 * set us into Session mode.
1564172629Smav			 */
1565172629Smav			utag = get_tag(ph, PTT_HOST_UNIQ);
1566172629Smav			if ((utag == NULL) ||
1567172629Smav			    (ntohs(utag->tag_len) != sizeof(sp))) {
1568172629Smav				LEAVE (ENETUNREACH);
1569172629Smav			}
1570172629Smav			sendhook = pppoe_finduniq(node, utag);
1571172629Smav			if (sendhook == NULL)
1572172629Smav				LEAVE(ENETUNREACH);
157352419Sjulian
1574172629Smav			/*
1575172629Smav			 * Check the session is in the right state.
1576172629Smav			 * It needs to be in PPPOE_SREQ.
1577172629Smav			 */
1578172629Smav			sp = NG_HOOK_PRIVATE(sendhook);
1579172629Smav			if (sp->state != PPPOE_SREQ)
1580172629Smav				LEAVE(ENETUNREACH);
1581172629Smav			neg = sp->neg;
1582172629Smav			ng_uncallout(&neg->handle, node);
1583172629Smav			neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1584172629Smav			sp->Session_ID = ntohs(wh->ph.sid);
1585175865Smav			pppoe_addsession(sp);
1586172629Smav			send_sessionid(sp);
1587172629Smav			neg->timeout = 0;
1588172629Smav			sp->state = PPPOE_CONNECTED;
1589172629Smav			/*
1590172629Smav			 * Now we have gone to Connected mode,
1591172629Smav			 * Free all resources needed for negotiation.
1592172629Smav			 * Keep a copy of the header we will be using.
1593172629Smav			 */
1594172629Smav			sp->pkt_hdr = neg->pkt->pkt_header;
1595172629Smav			if (privp->flags & COMPAT_3COM)
1596172629Smav				sp->pkt_hdr.eh.ether_type
1597172629Smav					= ETHERTYPE_PPPOE_3COM_SESS;
1598172629Smav			else
1599172629Smav				sp->pkt_hdr.eh.ether_type
1600172629Smav					= ETHERTYPE_PPPOE_SESS;
1601172629Smav			sp->pkt_hdr.ph.code = 0;
1602172629Smav			m_freem(neg->m);
1603172629Smav			free(sp->neg, M_NETGRAPH_PPPOE);
1604172629Smav			sp->neg = NULL;
1605172629Smav			pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1606172629Smav			break;
1607172629Smav		case	PADT_CODE:
1608172629Smav			/*
1609172629Smav			 * Find matching peer/session combination.
1610172629Smav			 */
1611175865Smav			sp = pppoe_findsession(privp, wh);
1612175865Smav			if (sp == NULL)
1613172629Smav				LEAVE(ENETUNREACH);
1614172629Smav			/* Disconnect that hook. */
1615175865Smav			ng_rmhook_self(sp->hook);
1616172629Smav			break;
1617172629Smav		default:
1618172629Smav			LEAVE(EPFNOSUPPORT);
1619172629Smav		}
1620172629Smav		break;
1621172629Smav	case	ETHERTYPE_PPPOE_3COM_SESS:
1622172629Smav	case	ETHERTYPE_PPPOE_SESS:
162352419Sjulian		/*
1624172629Smav		 * Find matching peer/session combination.
162552419Sjulian		 */
1626175865Smav		sp = pppoe_findsession(privp, wh);
1627175865Smav		if (sp == NULL)
1628172629Smav			LEAVE (ENETUNREACH);
1629172629Smav		m_adj(m, sizeof(*wh));
1630172629Smav
1631172629Smav		/* If packet too short, dump it. */
1632172629Smav		if (m->m_pkthdr.len < length)
1633172629Smav			LEAVE(EMSGSIZE);
1634172629Smav		/* Also need to trim excess at the end */
1635172629Smav		if (m->m_pkthdr.len > length) {
1636172629Smav			m_adj(m, -((int)(m->m_pkthdr.len - length)));
163752419Sjulian		}
1638172629Smav		if ( sp->state != PPPOE_CONNECTED) {
1639172629Smav			if (sp->state == PPPOE_NEWCONNECTED) {
1640172629Smav				sp->state = PPPOE_CONNECTED;
1641172629Smav				/*
1642172629Smav				 * Now we have gone to Connected mode,
1643172629Smav				 * Free all resources needed for negotiation.
1644172629Smav				 * Be paranoid about whether there may be
1645172629Smav				 * a timeout.
1646172629Smav				 */
1647172629Smav				m_freem(sp->neg->m);
1648172629Smav				ng_uncallout(&sp->neg->handle, node);
1649172629Smav				free(sp->neg, M_NETGRAPH_PPPOE);
1650172629Smav				sp->neg = NULL;
1651172629Smav			} else {
1652172629Smav				LEAVE (ENETUNREACH);
1653172629Smav			}
1654172629Smav		}
1655175865Smav		NG_FWD_NEW_DATA(error, item, sp->hook, m);
1656172629Smav		break;
1657172629Smav	default:
1658172629Smav		LEAVE(EPFNOSUPPORT);
165952419Sjulian	}
166052419Sjulianquit:
166170914Sjulian	if (item)
166270914Sjulian		NG_FREE_ITEM(item);
166370700Sjulian	NG_FREE_M(m);
1664172629Smav	return (error);
166552419Sjulian}
166652419Sjulian
166752419Sjulian/*
1668172629Smav * Receive data from debug hook and bypass it to ether.
1669172629Smav */
1670172629Smavstatic int
1671172629Smavng_pppoe_rcvdata_debug(hook_p hook, item_p item)
1672172629Smav{
1673172629Smav	node_p		node = NG_HOOK_NODE(hook);
1674172629Smav	const priv_p	privp = NG_NODE_PRIVATE(node);
1675172629Smav	int		error;
1676172629Smav
1677172629Smav	CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
1678172629Smav	    __func__, node->nd_ID, node, item, hook->hk_name, hook);
1679172629Smav
1680172629Smav	NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
1681172629Smav	privp->packets_out++;
1682172629Smav	return (error);
1683172629Smav}
1684172629Smav
1685172629Smav/*
168652419Sjulian * Do local shutdown processing..
168752419Sjulian * If we are a persistant device, we might refuse to go away, and
168852419Sjulian * we'd only remove our links and reset ourself.
168952419Sjulian */
169052419Sjulianstatic int
169170700Sjulianng_pppoe_shutdown(node_p node)
169252419Sjulian{
1693175865Smav	const priv_p privp = NG_NODE_PRIVATE(node);
1694175865Smav	int	i;
169552419Sjulian
1696175865Smav	for (i = 0; i < SESSHASHSIZE; i++)
1697175865Smav	    mtx_destroy(&privp->sesshash[i].mtx);
169870784Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
1699175865Smav	NG_NODE_UNREF(privp->node);
1700175865Smav	free(privp, M_NETGRAPH_PPPOE);
170152419Sjulian	return (0);
170252419Sjulian}
170352419Sjulian
170452419Sjulian/*
170552419Sjulian * Hook disconnection
170652419Sjulian *
170753498Sjulian * Clean up all dangling links and information about the session/hook.
1708154604Sglebius * For this type, removal of the last link destroys the node.
170952419Sjulian */
171052419Sjulianstatic int
171152562Sjulianng_pppoe_disconnect(hook_p hook)
171252419Sjulian{
171370784Sjulian	node_p node = NG_HOOK_NODE(hook);
171470784Sjulian	priv_p privp = NG_NODE_PRIVATE(node);
171552419Sjulian	sessp	sp;
171652419Sjulian
1717172270Smav	if (hook == privp->debug_hook) {
171852419Sjulian		privp->debug_hook = NULL;
1719172270Smav	} else if (hook == privp->ethernet_hook) {
172052419Sjulian		privp->ethernet_hook = NULL;
172170784Sjulian		if (NG_NODE_IS_VALID(node))
172270700Sjulian			ng_rmnode_self(node);
172352419Sjulian	} else {
172470784Sjulian		sp = NG_HOOK_PRIVATE(hook);
172552441Sjulian		if (sp->state != PPPOE_SNONE ) {
172652441Sjulian			pppoe_send_event(sp, NGM_PPPOE_CLOSE);
172752441Sjulian		}
172859728Sjulian		/*
172959728Sjulian		 * According to the spec, if we are connected,
173059728Sjulian		 * we should send a DISC packet if we are shutting down
173159728Sjulian		 * a session.
173259728Sjulian		 */
173352523Sjulian		if ((privp->ethernet_hook)
173452523Sjulian		&& ((sp->state == PPPOE_CONNECTED)
173552523Sjulian		 || (sp->state == PPPOE_NEWCONNECTED))) {
173652523Sjulian			struct mbuf *m;
173752523Sjulian
1738154604Sglebius			/* Generate a packet of that type. */
1739243882Sglebius			MGETHDR(m, M_NOWAIT, MT_DATA);
1740161034Sglebius			if (m == NULL)
1741161034Sglebius				log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
1742161034Sglebius				    "mbufs\n", node->nd_ID);
174353498Sjulian			else {
1744172269Smav				struct pppoe_full_hdr *wh;
1745172269Smav				struct pppoe_tag *tag;
1746172269Smav				int	msglen = strlen(SIGNOFF);
1747172269Smav				int	error = 0;
1748172269Smav
174953498Sjulian				m->m_pkthdr.rcvif = NULL;
175053498Sjulian				m->m_pkthdr.len = m->m_len = sizeof(*wh);
1751172269Smav				wh = mtod(m, struct pppoe_full_hdr *);
1752172269Smav				bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1753172269Smav
1754172269Smav				/* Revert the stored header to DISC/PADT mode. */
1755172269Smav				wh->ph.code = PADT_CODE;
175653498Sjulian				/*
1757172269Smav				 * Configure ethertype depending on what
1758172269Smav				 * was used during sessions stage.
1759172269Smav				 */
1760172269Smav				if (wh->eh.ether_type ==
1761172269Smav				    ETHERTYPE_PPPOE_3COM_SESS)
1762172269Smav					wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
1763172269Smav				else
1764172269Smav					wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1765172269Smav				/*
176653498Sjulian				 * Add a General error message and adjust
1767154604Sglebius				 * sizes.
176853498Sjulian				 */
1769189315Sed				tag = (void *)(&wh->ph + 1);
177053498Sjulian				tag->tag_type = PTT_GEN_ERR;
177153498Sjulian				tag->tag_len = htons((u_int16_t)msglen);
1772189315Sed				strncpy((char *)(tag + 1), SIGNOFF, msglen);
177353498Sjulian				m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
177453498Sjulian				    msglen);
177553498Sjulian				wh->ph.length = htons(sizeof(*tag) + msglen);
177670700Sjulian				NG_SEND_DATA_ONLY(error,
177770700Sjulian					privp->ethernet_hook, m);
177853498Sjulian			}
177952523Sjulian		}
1780176775Smav		if (sp->state == PPPOE_LISTENING)
1781176775Smav			LIST_REMOVE(sp, sessions);
1782176775Smav		else if (sp->Session_ID)
1783175865Smav			pppoe_delsession(sp);
178459728Sjulian		/*
178563138Sasmodai		 * As long as we have somewhere to store the timeout handle,
178659728Sjulian		 * we may have a timeout pending.. get rid of it.
178759728Sjulian		 */
178852443Sjulian		if (sp->neg) {
1789138562Sglebius			ng_uncallout(&sp->neg->handle, node);
179052443Sjulian			if (sp->neg->m)
179152443Sjulian				m_freem(sp->neg->m);
1792154604Sglebius			free(sp->neg, M_NETGRAPH_PPPOE);
179352443Sjulian		}
1794154604Sglebius		free(sp, M_NETGRAPH_PPPOE);
179570784Sjulian		NG_HOOK_SET_PRIVATE(hook, NULL);
179652419Sjulian	}
1797154604Sglebius	if ((NG_NODE_NUMHOOKS(node) == 0) &&
1798154604Sglebius	    (NG_NODE_IS_VALID(node)))
179970700Sjulian		ng_rmnode_self(node);
180052419Sjulian	return (0);
180152419Sjulian}
180252419Sjulian
180352419Sjulian/*
1804154604Sglebius * Timeouts come here.
180552419Sjulian */
180652419Sjulianstatic void
1807138562Sglebiuspppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
180852419Sjulian{
1809154604Sglebius	priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
181070784Sjulian	sessp	sp = NG_HOOK_PRIVATE(hook);
181152419Sjulian	negp	neg = sp->neg;
1812154604Sglebius	struct mbuf *m0 = NULL;
181352419Sjulian	int	error = 0;
181452419Sjulian
1815154604Sglebius	CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
1816154604Sglebius	    __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
181752419Sjulian	switch(sp->state) {
181852419Sjulian		/*
1819154604Sglebius		 * Resend the last packet, using an exponential backoff.
182052419Sjulian		 * After a period of time, stop growing the backoff,
1821154604Sglebius		 * And either leave it, or revert to the start.
182252419Sjulian		 */
182352419Sjulian	case	PPPOE_SINIT:
182452419Sjulian	case	PPPOE_SREQ:
1825154604Sglebius		/* Timeouts on these produce resends. */
1826243882Sglebius		m0 = m_copypacket(sp->neg->m, M_NOWAIT);
182770700Sjulian		NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
1828138562Sglebius		ng_callout(&neg->handle, node, hook, neg->timeout * hz,
1829138562Sglebius		    pppoe_ticker, NULL, 0);
183052419Sjulian		if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
183152419Sjulian			if (sp->state == PPPOE_SREQ) {
1832154604Sglebius				/* Revert to SINIT mode. */
183352441Sjulian				pppoe_start(sp);
183452419Sjulian			} else {
183552419Sjulian				neg->timeout = PPPOE_TIMEOUT_LIMIT;
183652419Sjulian			}
183752419Sjulian		}
183852419Sjulian		break;
183952419Sjulian	case	PPPOE_PRIMED:
184052419Sjulian	case	PPPOE_SOFFER:
1841154604Sglebius		/* A timeout on these says "give up" */
184270935Sjulian		ng_rmhook_self(hook);
184352419Sjulian		break;
184452419Sjulian	default:
1845154604Sglebius		/* Timeouts have no meaning in other states. */
1846161034Sglebius		log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
1847161034Sglebius		    node->nd_ID);
184852419Sjulian	}
184952419Sjulian}
185052419Sjulian
185152419Sjulian/*
185252419Sjulian * Parse an incoming packet to see if any tags should be copied to the
185353145Sjulian * output packet. Don't do any tags that have been handled in the main
185453145Sjulian * state machine.
185552419Sjulian */
1856154604Sglebiusstatic const struct pppoe_tag*
185797897Sarchiescan_tags(sessp	sp, const struct pppoe_hdr* ph)
185852419Sjulian{
185997897Sarchie	const char *const end = (const char *)next_tag(ph);
186097897Sarchie	const char *ptn;
1861189315Sed	const struct pppoe_tag *pt = (const void *)(ph + 1);
1862154604Sglebius
186352419Sjulian	/*
186452419Sjulian	 * Keep processing tags while a tag header will still fit.
186552419Sjulian	 */
1866154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1867154604Sglebius
186897897Sarchie	while((const char*)(pt + 1) <= end) {
186952419Sjulian		/*
187052419Sjulian		 * If the tag data would go past the end of the packet, abort.
187152419Sjulian		 */
187297897Sarchie		ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
187352419Sjulian		if(ptn > end)
187452419Sjulian			return NULL;
187552419Sjulian
187652419Sjulian		switch (pt->tag_type) {
187752419Sjulian		case	PTT_RELAY_SID:
187852419Sjulian			insert_tag(sp, pt);
187952419Sjulian			break;
188052419Sjulian		case	PTT_EOL:
188152419Sjulian			return NULL;
188252419Sjulian		case	PTT_SRV_NAME:
188352419Sjulian		case	PTT_AC_NAME:
188452419Sjulian		case	PTT_HOST_UNIQ:
188552419Sjulian		case	PTT_AC_COOKIE:
188652419Sjulian		case	PTT_VENDOR:
188752419Sjulian		case	PTT_SRV_ERR:
188852419Sjulian		case	PTT_SYS_ERR:
188952419Sjulian		case	PTT_GEN_ERR:
1890253564Sglebius		case	PTT_MAX_PAYL:
189152419Sjulian			break;
189252419Sjulian		}
189397897Sarchie		pt = (const struct pppoe_tag*)ptn;
189452419Sjulian	}
189552419Sjulian	return NULL;
189652419Sjulian}
189752419Sjulian
189852441Sjulianstatic	int
189952441Sjulianpppoe_send_event(sessp sp, enum cmd cmdid)
190052441Sjulian{
190152441Sjulian	int error;
190252441Sjulian	struct ng_mesg *msg;
190352562Sjulian	struct ngpppoe_sts *sts;
190452441Sjulian
1905154604Sglebius	CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
1906154604Sglebius
190768845Sbrian	NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
190852562Sjulian			sizeof(struct ngpppoe_sts), M_NOWAIT);
190969922Sjulian	if (msg == NULL)
191069922Sjulian		return (ENOMEM);
191152562Sjulian	sts = (struct ngpppoe_sts *)msg->data;
1912125028Sharti	strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
1913102244Sarchie	NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
191452441Sjulian	return (error);
191552441Sjulian}
1916