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