HAMMER Utilities: Remove time/transaction-id conversion directives.
[dragonfly.git] / sys / netgraph / pppoe / ng_pppoe.c
blobc5cdfe6ae82679ae3d10c30d552f55c7e9e33788
2 /*
3 * ng_pppoe.c
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.
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.
37 * Author: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_pppoe.c,v 1.23.2.17 2002/07/02 22:17:18 archie Exp $
40 * $DragonFly: src/sys/netgraph/pppoe/ng_pppoe.c,v 1.11 2008/01/05 14:02:39 swildner Exp $
41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
43 #if 0
44 #define AAA kprintf("pppoe: %s\n", __func__ );
45 #define BBB kprintf("-%d-", __LINE__ );
46 #else
47 #define AAA
48 #define BBB
49 #endif
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/mbuf.h>
55 #include <sys/malloc.h>
56 #include <sys/errno.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/thread2.h>
60 #include <net/ethernet.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include "ng_pppoe.h"
66 #define SIGNOFF "session closed"
69 * This section contains the netgraph method declarations for the
70 * pppoe node. These methods define the netgraph pppoe 'type'.
73 static ng_constructor_t ng_pppoe_constructor;
74 static ng_rcvmsg_t ng_pppoe_rcvmsg;
75 static ng_shutdown_t ng_pppoe_rmnode;
76 static ng_newhook_t ng_pppoe_newhook;
77 static ng_connect_t ng_pppoe_connect;
78 static ng_rcvdata_t ng_pppoe_rcvdata;
79 static ng_disconnect_t ng_pppoe_disconnect;
81 /* Netgraph node type descriptor */
82 static struct ng_type typestruct = {
83 NG_VERSION,
84 NG_PPPOE_NODE_TYPE,
85 NULL,
86 ng_pppoe_constructor,
87 ng_pppoe_rcvmsg,
88 ng_pppoe_rmnode,
89 ng_pppoe_newhook,
90 NULL,
91 ng_pppoe_connect,
92 ng_pppoe_rcvdata,
93 ng_pppoe_rcvdata,
94 ng_pppoe_disconnect,
95 NULL
97 NETGRAPH_INIT(pppoe, &typestruct);
100 * States for the session state machine.
101 * These have no meaning if there is no hook attached yet.
103 enum state {
104 PPPOE_SNONE=0, /* [both] Initial state */
105 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
106 PPPOE_SINIT, /* [Client] Sent discovery initiation */
107 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
108 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
109 PPPOE_SREQ, /* [Client] Sent a Request */
110 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
111 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
112 PPPOE_DEAD /* [Both] */
115 #define NUMTAGS 20 /* number of tags we are set up to work with */
118 * Information we store for each hook on each node for negotiating the
119 * session. The mbuf and cluster are freed once negotiation has completed.
120 * The whole negotiation block is then discarded.
123 struct sess_neg {
124 struct mbuf *m; /* holds cluster with last sent packet */
125 union packet *pkt; /* points within the above cluster */
126 struct callout timeout_ch;
127 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
128 u_int numtags;
129 const struct pppoe_tag *tags[NUMTAGS];
130 u_int service_len;
131 u_int ac_name_len;
133 struct datatag service;
134 struct datatag ac_name;
136 typedef struct sess_neg *negp;
139 * Session information that is needed after connection.
141 struct sess_con {
142 hook_p hook;
143 u_int16_t Session_ID;
144 enum state state;
145 char creator[NG_NODESIZ]; /* who to notify */
146 struct pppoe_full_hdr pkt_hdr; /* used when connected */
147 negp neg; /* used when negotiating */
148 /*struct sess_con *hash_next;*/ /* not yet used */
150 typedef struct sess_con *sessp;
153 * Information we store for each node
155 struct PPPOE {
156 node_p node; /* back pointer to node */
157 hook_p ethernet_hook;
158 hook_p debug_hook;
159 u_int packets_in; /* packets in from ethernet */
160 u_int packets_out; /* packets out towards ethernet */
161 u_int32_t flags;
162 /*struct sess_con *buckets[HASH_SIZE];*/ /* not yet used */
164 typedef struct PPPOE *priv_p;
166 struct ether_header eh_prototype =
167 {{0xff,0xff,0xff,0xff,0xff,0xff},
168 {0x00,0x00,0x00,0x00,0x00,0x00},
169 ETHERTYPE_PPPOE_DISC};
171 #define PPPOE_KEEPSTANDARD -1 /* never switch to nonstandard mode */
172 #define PPPOE_STANDARD 0 /* try standard mode (dangerous!) */
173 #define PPPOE_NONSTANDARD 1 /* just be in nonstandard mode */
174 static int pppoe_mode = PPPOE_KEEPSTANDARD;
176 static int
177 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS)
179 int error;
180 int val;
182 val = pppoe_mode;
183 error = sysctl_handle_int(oidp, &val, sizeof(int), req);
184 if (error != 0 || req->newptr == NULL)
185 return (error);
186 switch (val) {
187 case PPPOE_NONSTANDARD:
188 pppoe_mode = PPPOE_NONSTANDARD;
189 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
190 break;
191 case PPPOE_STANDARD:
192 pppoe_mode = PPPOE_STANDARD;
193 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
194 break;
195 case PPPOE_KEEPSTANDARD:
196 pppoe_mode = PPPOE_KEEPSTANDARD;
197 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
198 break;
199 default:
200 return (EINVAL);
202 return (0);
205 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW,
206 0, sizeof(int), ngpppoe_set_ethertype, "I", "nonstandard ethertype");
208 union uniq {
209 char bytes[sizeof(void *)];
210 void * pointer;
213 #define LEAVE(x) do { error = x; goto quit; } while(0)
214 static void pppoe_start(sessp sp);
215 static void sendpacket(sessp sp);
216 static void pppoe_ticker(void *arg);
217 static const struct pppoe_tag *scan_tags(sessp sp,
218 const struct pppoe_hdr* ph);
219 static int pppoe_send_event(sessp sp, enum cmd cmdid);
221 /*************************************************************************
222 * Some basic utilities from the Linux version with author's permission.*
223 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
224 ************************************************************************/
227 * Generate a new session id
228 * XXX find out the FreeBSD locking scheme.
230 static u_int16_t
231 get_new_sid(node_p node)
233 static int pppoe_sid = 10;
234 sessp sp;
235 hook_p hook;
236 u_int16_t val;
237 priv_p privp = node->private;
240 restart:
241 val = pppoe_sid++;
243 * Spec says 0xFFFF is reserved.
244 * Also don't use 0x0000
246 if (val == 0xffff) {
247 pppoe_sid = 20;
248 goto restart;
251 /* Check it isn't already in use */
252 LIST_FOREACH(hook, &node->hooks, hooks) {
253 /* don't check special hooks */
254 if ((hook->private == &privp->debug_hook)
255 || (hook->private == &privp->ethernet_hook))
256 continue;
257 sp = hook->private;
258 if (sp->Session_ID == val)
259 goto restart;
262 return val;
267 * Return the location where the next tag can be put
269 static __inline const struct pppoe_tag*
270 next_tag(const struct pppoe_hdr* ph)
272 return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
273 + ntohs(ph->length));
277 * Look for a tag of a specific type
278 * Don't trust any length the other end says.
279 * but assume we already sanity checked ph->length.
281 static const struct pppoe_tag*
282 get_tag(const struct pppoe_hdr* ph, u_int16_t idx)
284 const char *const end = (const char *)next_tag(ph);
285 const char *ptn;
286 const struct pppoe_tag *pt = &ph->tag[0];
288 * Keep processing tags while a tag header will still fit.
291 while((const char*)(pt + 1) <= end) {
293 * If the tag data would go past the end of the packet, abort.
295 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
296 if(ptn > end)
297 return NULL;
299 if(pt->tag_type == idx)
300 return pt;
302 pt = (const struct pppoe_tag*)ptn;
304 return NULL;
307 /**************************************************************************
308 * inlines to initialise or add tags to a session's tag list,
309 **************************************************************************/
311 * Initialise the session's tag list
313 static void
314 init_tags(sessp sp)
317 if(sp->neg == NULL) {
318 kprintf("pppoe: asked to init NULL neg pointer\n");
319 return;
321 sp->neg->numtags = 0;
324 static void
325 insert_tag(sessp sp, const struct pppoe_tag *tp)
327 int i;
328 negp neg;
331 if((neg = sp->neg) == NULL) {
332 kprintf("pppoe: asked to use NULL neg pointer\n");
333 return;
335 if ((i = neg->numtags++) < NUMTAGS) {
336 neg->tags[i] = tp;
337 } else {
338 kprintf("pppoe: asked to add too many tags to packet\n");
339 neg->numtags--;
344 * Make up a packet, using the tags filled out for the session.
346 * Assume that the actual pppoe header and ethernet header
347 * are filled out externally to this routine.
348 * Also assume that neg->wh points to the correct
349 * location at the front of the buffer space.
351 static void
352 make_packet(sessp sp) {
353 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
354 const struct pppoe_tag **tag;
355 char *dp;
356 int count;
357 int tlen;
358 u_int16_t length = 0;
361 if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
362 kprintf("pppoe: make_packet called from wrong state\n");
364 dp = (char *)wh->ph.tag;
365 for (count = 0, tag = sp->neg->tags;
366 ((count < sp->neg->numtags) && (count < NUMTAGS));
367 tag++, count++) {
368 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
369 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
370 kprintf("pppoe: tags too long\n");
371 sp->neg->numtags = count;
372 break; /* XXX chop off what's too long */
374 bcopy(*tag, (char *)dp, tlen);
375 length += tlen;
376 dp += tlen;
378 wh->ph.length = htons(length);
379 sp->neg->m->m_len = length + sizeof(*wh);
380 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
383 /**************************************************************************
384 * Routine to match a service offered *
385 **************************************************************************/
387 * Find a hook that has a service string that matches that
388 * we are seeking. for now use a simple string.
389 * In the future we may need something like regexp().
390 * for testing allow a null string to match 1st found and a null service
391 * to match all requests. Also make '*' do the same.
394 #define NG_MATCH_EXACT 1
395 #define NG_MATCH_ANY 2
397 static hook_p
398 pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match)
400 sessp sp = NULL;
401 negp neg = NULL;
402 priv_p privp = node->private;
403 hook_p allhook = NULL;
404 hook_p hook;
407 LIST_FOREACH(hook, &node->hooks, hooks) {
409 /* skip any hook that is debug or ethernet */
410 if ((hook->private == &privp->debug_hook)
411 || (hook->private == &privp->ethernet_hook))
412 continue;
413 sp = hook->private;
415 /* Skip any sessions which are not in LISTEN mode. */
416 if ( sp->state != PPPOE_LISTENING)
417 continue;
419 neg = sp->neg;
421 /* Special case for a blank or "*" service name (wildcard) */
422 if (match == NG_MATCH_ANY && neg->service_len == 1 &&
423 neg->service.data[0] == '*') {
424 allhook = hook;
425 continue;
428 /* If the lengths don't match, that aint it. */
429 if (neg->service_len != svc_len)
430 continue;
432 /* An exact match? */
433 if (svc_len == 0)
434 break;
436 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
437 break;
439 return (hook ? hook : allhook);
441 /**************************************************************************
442 * Routine to find a particular session that matches an incoming packet *
443 **************************************************************************/
444 static hook_p
445 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
447 sessp sp = NULL;
448 hook_p hook = NULL;
449 priv_p privp = node->private;
450 u_int16_t session = ntohs(wh->ph.sid);
453 * find matching peer/session combination.
456 LIST_FOREACH(hook, &node->hooks, hooks) {
457 /* don't check special hooks */
458 if ((hook->private == &privp->debug_hook)
459 || (hook->private == &privp->ethernet_hook)) {
460 continue;
462 sp = hook->private;
463 if ( ( (sp->state == PPPOE_CONNECTED)
464 || (sp->state == PPPOE_NEWCONNECTED) )
465 && (sp->Session_ID == session)
466 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
467 wh->eh.ether_shost,
468 ETHER_ADDR_LEN)) == 0) {
469 break;
472 return (hook);
475 static hook_p
476 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
478 hook_p hook = NULL;
479 priv_p privp = node->private;
480 union uniq uniq;
483 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
484 /* cycle through all known hooks */
485 LIST_FOREACH(hook, &node->hooks, hooks) {
486 /* don't check special hooks */
487 if ((hook->private == &privp->debug_hook)
488 || (hook->private == &privp->ethernet_hook))
489 continue;
490 if (uniq.pointer == hook->private)
491 break;
493 return (hook);
496 /**************************************************************************
497 * start of Netgraph entrypoints *
498 **************************************************************************/
501 * Allocate the private data structure and the generic node
502 * and link them together.
504 * ng_make_node_common() returns with a generic node struct
505 * with a single reference for us.. we transfer it to the
506 * private structure.. when we free the private struct we must
507 * unref the node so it gets freed too.
509 static int
510 ng_pppoe_constructor(node_p *nodep)
512 priv_p privdata;
513 int error;
516 /* Initialize private descriptor */
517 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT | M_ZERO);
518 if (privdata == NULL)
519 return (ENOMEM);
521 /* Call the 'generic' (ie, superclass) node constructor */
522 if ((error = ng_make_node_common(&typestruct, nodep))) {
523 FREE(privdata, M_NETGRAPH);
524 return (error);
527 /* Link structs together; this counts as our one reference to *nodep */
528 (*nodep)->private = privdata;
529 privdata->node = *nodep;
530 return (0);
534 * Give our ok for a hook to be added...
535 * point the hook's private info to the hook structure.
537 * The following hook names are special:
538 * Ethernet: the hook that should be connected to a NIC.
539 * debug: copies of data sent out here (when I write the code).
540 * All other hook names need only be unique. (the framework checks this).
542 static int
543 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
545 const priv_p privp = node->private;
546 sessp sp;
549 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
550 privp->ethernet_hook = hook;
551 hook->private = &privp->ethernet_hook;
552 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
553 privp->debug_hook = hook;
554 hook->private = &privp->debug_hook;
555 } else {
557 * Any other unique name is OK.
558 * The infrastructure has already checked that it's unique,
559 * so just allocate it and hook it in.
561 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_NOWAIT | M_ZERO);
562 if (sp == NULL)
563 return (ENOMEM);
565 hook->private = sp;
566 sp->hook = hook;
568 return(0);
572 * Get a netgraph control message.
573 * Check it is one we understand. If needed, send a response.
574 * We sometimes save the address for an async action later.
575 * Always free the message.
577 static int
578 ng_pppoe_rcvmsg(node_p node,
579 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
581 priv_p privp = node->private;
582 struct ngpppoe_init_data *ourmsg = NULL;
583 struct ng_mesg *resp = NULL;
584 int error = 0;
585 hook_p hook = NULL;
586 sessp sp = NULL;
587 negp neg = NULL;
590 /* Deal with message according to cookie and command */
591 switch (msg->header.typecookie) {
592 case NGM_PPPOE_COOKIE:
593 switch (msg->header.cmd) {
594 case NGM_PPPOE_CONNECT:
595 case NGM_PPPOE_LISTEN:
596 case NGM_PPPOE_OFFER:
597 case NGM_PPPOE_SERVICE:
598 ourmsg = (struct ngpppoe_init_data *)msg->data;
599 if (( sizeof(*ourmsg) > msg->header.arglen)
600 || ((sizeof(*ourmsg) + ourmsg->data_len)
601 > msg->header.arglen)) {
602 kprintf("pppoe_rcvmsg: bad arg size");
603 LEAVE(EMSGSIZE);
605 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
606 kprintf("pppoe: init data too long (%d)\n",
607 ourmsg->data_len);
608 LEAVE(EMSGSIZE);
610 /* make sure strcmp will terminate safely */
611 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
613 /* cycle through all known hooks */
614 LIST_FOREACH(hook, &node->hooks, hooks) {
615 if (hook->name
616 && strcmp(hook->name, ourmsg->hook) == 0)
617 break;
619 if (hook == NULL) {
620 LEAVE(ENOENT);
622 if ((hook->private == &privp->debug_hook)
623 || (hook->private == &privp->ethernet_hook)) {
624 LEAVE(EINVAL);
626 sp = hook->private;
628 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
630 * Ensure we aren't already listening for this
631 * service.
633 if (pppoe_match_svc(node, ourmsg->data,
634 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
635 LEAVE(EEXIST);
640 * PPPOE_SERVICE advertisments are set up
641 * on sessions that are in PRIMED state.
643 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
644 break;
646 if (sp->state |= PPPOE_SNONE) {
647 kprintf("pppoe: Session already active\n");
648 LEAVE(EISCONN);
652 * set up prototype header
654 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH,
655 M_NOWAIT | M_ZERO);
657 if (neg == NULL) {
658 kprintf("pppoe: Session out of memory\n");
659 LEAVE(ENOMEM);
661 MGETHDR(neg->m, MB_DONTWAIT, MT_DATA);
662 if(neg->m == NULL) {
663 kprintf("pppoe: Session out of mbufs\n");
664 FREE(neg, M_NETGRAPH);
665 LEAVE(ENOBUFS);
667 neg->m->m_pkthdr.rcvif = NULL;
668 MCLGET(neg->m, MB_DONTWAIT);
669 if ((neg->m->m_flags & M_EXT) == 0) {
670 kprintf("pppoe: Session out of mcls\n");
671 m_freem(neg->m);
672 FREE(neg, M_NETGRAPH);
673 LEAVE(ENOBUFS);
675 sp->neg = neg;
676 callout_init(&neg->timeout_ch);
677 neg->m->m_len = sizeof(struct pppoe_full_hdr);
678 neg->pkt = mtod(neg->m, union packet*);
679 neg->pkt->pkt_header.eh = eh_prototype;
680 neg->pkt->pkt_header.ph.ver = 0x1;
681 neg->pkt->pkt_header.ph.type = 0x1;
682 neg->pkt->pkt_header.ph.sid = 0x0000;
683 neg->timeout = 0;
685 strlcpy(sp->creator, retaddr, NG_NODESIZ);
687 switch (msg->header.cmd) {
688 case NGM_PPPOE_GET_STATUS:
690 struct ngpppoestat *stats;
692 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
693 if (!resp) {
694 LEAVE(ENOMEM);
696 stats = (struct ngpppoestat *) resp->data;
697 stats->packets_in = privp->packets_in;
698 stats->packets_out = privp->packets_out;
699 break;
701 case NGM_PPPOE_CONNECT:
703 * Check the hook exists and is Uninitialised.
704 * Send a PADI request, and start the timeout logic.
705 * Store the originator of this message so we can send
706 * a success of fail message to them later.
707 * Move the session to SINIT
708 * Set up the session to the correct state and
709 * start it.
711 neg->service.hdr.tag_type = PTT_SRV_NAME;
712 neg->service.hdr.tag_len =
713 htons((u_int16_t)ourmsg->data_len);
714 if (ourmsg->data_len) {
715 bcopy(ourmsg->data,
716 neg->service.data, ourmsg->data_len);
718 neg->service_len = ourmsg->data_len;
719 pppoe_start(sp);
720 break;
721 case NGM_PPPOE_LISTEN:
723 * Check the hook exists and is Uninitialised.
724 * Install the service matching string.
725 * Store the originator of this message so we can send
726 * a success of fail message to them later.
727 * Move the hook to 'LISTENING'
729 neg->service.hdr.tag_type = PTT_SRV_NAME;
730 neg->service.hdr.tag_len =
731 htons((u_int16_t)ourmsg->data_len);
733 if (ourmsg->data_len) {
734 bcopy(ourmsg->data,
735 neg->service.data, ourmsg->data_len);
737 neg->service_len = ourmsg->data_len;
738 neg->pkt->pkt_header.ph.code = PADT_CODE;
740 * wait for PADI packet coming from ethernet
742 sp->state = PPPOE_LISTENING;
743 break;
744 case NGM_PPPOE_OFFER:
746 * Check the hook exists and is Uninitialised.
747 * Store the originator of this message so we can send
748 * a success of fail message to them later.
749 * Store the AC-Name given and go to PRIMED.
751 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
752 neg->ac_name.hdr.tag_len =
753 htons((u_int16_t)ourmsg->data_len);
754 if (ourmsg->data_len) {
755 bcopy(ourmsg->data,
756 neg->ac_name.data, ourmsg->data_len);
758 neg->ac_name_len = ourmsg->data_len;
759 neg->pkt->pkt_header.ph.code = PADO_CODE;
761 * Wait for PADI packet coming from hook
763 sp->state = PPPOE_PRIMED;
764 break;
765 case NGM_PPPOE_SERVICE:
767 * Check the session is primed.
768 * for now just allow ONE service to be advertised.
769 * If you do it twice you just overwrite.
771 if (sp->state != PPPOE_PRIMED) {
772 kprintf("pppoe: Session not primed\n");
773 LEAVE(EISCONN);
775 neg = sp->neg;
776 neg->service.hdr.tag_type = PTT_SRV_NAME;
777 neg->service.hdr.tag_len =
778 htons((u_int16_t)ourmsg->data_len);
780 if (ourmsg->data_len)
781 bcopy(ourmsg->data, neg->service.data,
782 ourmsg->data_len);
783 neg->service_len = ourmsg->data_len;
784 break;
785 default:
786 LEAVE(EINVAL);
788 break;
789 default:
790 LEAVE(EINVAL);
793 /* Take care of synchronous response, if any */
794 if (rptr)
795 *rptr = resp;
796 else if (resp)
797 FREE(resp, M_NETGRAPH);
799 /* Free the message and return */
800 quit:
801 FREE(msg, M_NETGRAPH);
802 return(error);
806 * Start a client into the first state. A separate function because
807 * it can be needed if the negotiation times out.
809 static void
810 pppoe_start(sessp sp)
812 struct {
813 struct pppoe_tag hdr;
814 union uniq data;
815 } __attribute ((packed)) uniqtag;
818 * kick the state machine into starting up
821 sp->state = PPPOE_SINIT;
822 /* reset the packet header to broadcast */
823 sp->neg->pkt->pkt_header.eh = eh_prototype;
824 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
825 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
826 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
827 uniqtag.data.pointer = sp;
828 init_tags(sp);
829 insert_tag(sp, &uniqtag.hdr);
830 insert_tag(sp, &sp->neg->service.hdr);
831 make_packet(sp);
832 sendpacket(sp);
835 static int
836 send_acname(sessp sp, const struct pppoe_tag *tag)
838 int error, tlen;
839 struct ng_mesg *msg;
840 struct ngpppoe_sts *sts;
842 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
843 sizeof(struct ngpppoe_sts), M_NOWAIT);
844 if (msg == NULL)
845 return (ENOMEM);
847 sts = (struct ngpppoe_sts *)msg->data;
848 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
849 strncpy(sts->hook, tag->tag_data, tlen);
850 sts->hook[tlen] = '\0';
851 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
853 return (error);
856 static int
857 send_sessionid(sessp sp)
859 int error;
860 struct ng_mesg *msg;
862 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
863 sizeof(u_int16_t), M_NOWAIT);
864 if (msg == NULL)
865 return (ENOMEM);
867 *(u_int16_t *)msg->data = sp->Session_ID;
868 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
870 return (error);
874 * Receive data, and do something with it.
875 * The caller will never free m or meta, so
876 * if we use up this data or abort we must free BOTH of these.
878 static int
879 ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
881 node_p node = hook->node;
882 const priv_p privp = node->private;
883 sessp sp = hook->private;
884 const struct pppoe_full_hdr *wh;
885 const struct pppoe_hdr *ph;
886 int error = 0;
887 u_int16_t session;
888 u_int16_t length;
889 u_int8_t code;
890 const struct pppoe_tag *utag = NULL, *tag = NULL;
891 hook_p sendhook;
892 struct {
893 struct pppoe_tag hdr;
894 union uniq data;
895 } __attribute ((packed)) uniqtag;
896 negp neg = NULL;
899 if (hook->private == &privp->debug_hook) {
901 * Data from the debug hook gets sent without modification
902 * straight to the ethernet.
904 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
905 privp->packets_out++;
906 } else if (hook->private == &privp->ethernet_hook) {
908 * Incoming data.
909 * Dig out various fields from the packet.
910 * use them to decide where to send it.
913 privp->packets_in++;
914 if( m->m_len < sizeof(*wh)) {
915 m = m_pullup(m, sizeof(*wh)); /* Checks length */
916 if (m == NULL) {
917 kprintf("couldn't m_pullup\n");
918 LEAVE(ENOBUFS);
921 wh = mtod(m, struct pppoe_full_hdr *);
922 ph = &wh->ph;
923 session = ntohs(wh->ph.sid);
924 length = ntohs(wh->ph.length);
925 code = wh->ph.code;
926 switch(wh->eh.ether_type) {
927 case ETHERTYPE_PPPOE_STUPID_DISC:
928 if (pppoe_mode == PPPOE_STANDARD) {
929 pppoe_mode = PPPOE_NONSTANDARD;
930 eh_prototype.ether_type =
931 ETHERTYPE_PPPOE_STUPID_DISC;
932 log(LOG_NOTICE,
933 "Switched to nonstandard PPPoE mode due to "
934 "packet from %*D\n",
935 ETHER_ADDR_LEN,
936 wh->eh.ether_shost, ":");
937 } else if (pppoe_mode == PPPOE_KEEPSTANDARD)
938 log(LOG_NOTICE,
939 "Ignored nonstandard PPPoE packet "
940 "from %*D\n",
941 ETHER_ADDR_LEN,
942 wh->eh.ether_shost, ":");
943 /* fall through */
944 case ETHERTYPE_PPPOE_DISC:
946 * We need to try to make sure that the tag area
947 * is contiguous, or we could wander off the end
948 * of a buffer and make a mess.
949 * (Linux wouldn't have this problem).
951 /*XXX fix this mess */
953 if (m->m_pkthdr.len <= MHLEN) {
954 if( m->m_len < m->m_pkthdr.len) {
955 m = m_pullup(m, m->m_pkthdr.len);
956 if (m == NULL) {
957 kprintf("couldn't m_pullup\n");
958 LEAVE(ENOBUFS);
962 if (m->m_len != m->m_pkthdr.len) {
964 * It's not all in one piece.
965 * We need to do extra work.
967 kprintf("packet fragmented\n");
968 LEAVE(EMSGSIZE);
971 switch(code) {
972 case PADI_CODE:
974 * We are a server:
975 * Look for a hook with the required service
976 * and send the ENTIRE packet up there.
977 * It should come back to a new hook in
978 * PRIMED state. Look there for further
979 * processing.
981 tag = get_tag(ph, PTT_SRV_NAME);
982 if (tag == NULL) {
983 kprintf("no service tag\n");
984 LEAVE(ENETUNREACH);
986 sendhook = pppoe_match_svc(hook->node,
987 tag->tag_data, ntohs(tag->tag_len),
988 NG_MATCH_ANY);
989 if (sendhook) {
990 NG_SEND_DATA(error, sendhook, m, meta);
991 } else {
992 LEAVE(ENETUNREACH);
994 break;
995 case PADO_CODE:
997 * We are a client:
998 * Use the host_uniq tag to find the
999 * hook this is in response to.
1000 * Received #2, now send #3
1001 * For now simply accept the first we receive.
1003 utag = get_tag(ph, PTT_HOST_UNIQ);
1004 if ((utag == NULL)
1005 || (ntohs(utag->tag_len) != sizeof(sp))) {
1006 kprintf("no host unique field\n");
1007 LEAVE(ENETUNREACH);
1010 sendhook = pppoe_finduniq(node, utag);
1011 if (sendhook == NULL) {
1012 kprintf("no matching session\n");
1013 LEAVE(ENETUNREACH);
1017 * Check the session is in the right state.
1018 * It needs to be in PPPOE_SINIT.
1020 sp = sendhook->private;
1021 if (sp->state != PPPOE_SINIT) {
1022 kprintf("session in wrong state\n");
1023 LEAVE(ENETUNREACH);
1025 neg = sp->neg;
1026 callout_stop(&neg->timeout_ch);
1029 * This is the first time we hear
1030 * from the server, so note it's
1031 * unicast address, replacing the
1032 * broadcast address .
1034 bcopy(wh->eh.ether_shost,
1035 neg->pkt->pkt_header.eh.ether_dhost,
1036 ETHER_ADDR_LEN);
1037 neg->timeout = 0;
1038 neg->pkt->pkt_header.ph.code = PADR_CODE;
1039 init_tags(sp);
1040 insert_tag(sp, utag); /* Host Unique */
1041 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1042 insert_tag(sp, tag); /* return cookie */
1043 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1044 insert_tag(sp, tag); /* return it */
1045 send_acname(sp, tag);
1047 insert_tag(sp, &neg->service.hdr); /* Service */
1048 scan_tags(sp, ph);
1049 make_packet(sp);
1050 sp->state = PPPOE_SREQ;
1051 sendpacket(sp);
1052 break;
1053 case PADR_CODE:
1056 * We are a server:
1057 * Use the ac_cookie tag to find the
1058 * hook this is in response to.
1060 utag = get_tag(ph, PTT_AC_COOKIE);
1061 if ((utag == NULL)
1062 || (ntohs(utag->tag_len) != sizeof(sp))) {
1063 LEAVE(ENETUNREACH);
1066 sendhook = pppoe_finduniq(node, utag);
1067 if (sendhook == NULL) {
1068 LEAVE(ENETUNREACH);
1072 * Check the session is in the right state.
1073 * It needs to be in PPPOE_SOFFER
1074 * or PPPOE_NEWCONNECTED. If the latter,
1075 * then this is a retry by the client.
1076 * so be nice, and resend.
1078 sp = sendhook->private;
1079 if (sp->state == PPPOE_NEWCONNECTED) {
1081 * Whoa! drop back to resend that
1082 * PADS packet.
1083 * We should still have a copy of it.
1085 sp->state = PPPOE_SOFFER;
1087 if (sp->state != PPPOE_SOFFER) {
1088 LEAVE (ENETUNREACH);
1089 break;
1091 neg = sp->neg;
1092 callout_stop(&neg->timeout_ch);
1093 neg->pkt->pkt_header.ph.code = PADS_CODE;
1094 if (sp->Session_ID == 0)
1095 neg->pkt->pkt_header.ph.sid =
1096 htons(sp->Session_ID
1097 = get_new_sid(node));
1098 send_sessionid(sp);
1099 neg->timeout = 0;
1101 * start working out the tags to respond with.
1103 init_tags(sp);
1104 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1105 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1106 insert_tag(sp, tag);/* return service */
1107 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1108 insert_tag(sp, tag); /* return it */
1109 insert_tag(sp, utag); /* ac_cookie */
1110 scan_tags(sp, ph);
1111 make_packet(sp);
1112 sp->state = PPPOE_NEWCONNECTED;
1113 sendpacket(sp);
1115 * Having sent the last Negotiation header,
1116 * Set up the stored packet header to
1117 * be correct for the actual session.
1118 * But keep the negotialtion stuff
1119 * around in case we need to resend this last
1120 * packet. We'll discard it when we move
1121 * from NEWCONNECTED to CONNECTED
1123 sp->pkt_hdr = neg->pkt->pkt_header;
1124 if (pppoe_mode == PPPOE_NONSTANDARD)
1125 sp->pkt_hdr.eh.ether_type
1126 = ETHERTYPE_PPPOE_STUPID_SESS;
1127 else
1128 sp->pkt_hdr.eh.ether_type
1129 = ETHERTYPE_PPPOE_SESS;
1130 sp->pkt_hdr.ph.code = 0;
1131 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1132 break;
1133 case PADS_CODE:
1135 * We are a client:
1136 * Use the host_uniq tag to find the
1137 * hook this is in response to.
1138 * take the session ID and store it away.
1139 * Also make sure the pre-made header is
1140 * correct and set us into Session mode.
1142 utag = get_tag(ph, PTT_HOST_UNIQ);
1143 if ((utag == NULL)
1144 || (ntohs(utag->tag_len) != sizeof(sp))) {
1145 LEAVE (ENETUNREACH);
1146 break;
1148 sendhook = pppoe_finduniq(node, utag);
1149 if (sendhook == NULL) {
1150 LEAVE(ENETUNREACH);
1154 * Check the session is in the right state.
1155 * It needs to be in PPPOE_SREQ.
1157 sp = sendhook->private;
1158 if (sp->state != PPPOE_SREQ) {
1159 LEAVE(ENETUNREACH);
1161 neg = sp->neg;
1162 callout_stop(&neg->timeout_ch);
1163 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1164 sp->Session_ID = ntohs(wh->ph.sid);
1165 send_sessionid(sp);
1166 neg->timeout = 0;
1167 sp->state = PPPOE_CONNECTED;
1169 * Now we have gone to Connected mode,
1170 * Free all resources needed for
1171 * negotiation.
1172 * Keep a copy of the header we will be using.
1174 sp->pkt_hdr = neg->pkt->pkt_header;
1175 if (pppoe_mode == PPPOE_NONSTANDARD)
1176 sp->pkt_hdr.eh.ether_type
1177 = ETHERTYPE_PPPOE_STUPID_SESS;
1178 else
1179 sp->pkt_hdr.eh.ether_type
1180 = ETHERTYPE_PPPOE_SESS;
1181 sp->pkt_hdr.ph.code = 0;
1182 m_freem(neg->m);
1183 FREE(sp->neg, M_NETGRAPH);
1184 sp->neg = NULL;
1185 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1186 break;
1187 case PADT_CODE:
1189 * Send a 'close' message to the controlling
1190 * process (the one that set us up);
1191 * And then tear everything down.
1193 * Find matching peer/session combination.
1195 sendhook = pppoe_findsession(node, wh);
1196 NG_FREE_DATA(m, meta); /* no longer needed */
1197 if (sendhook == NULL) {
1198 LEAVE(ENETUNREACH);
1200 /* send message to creator */
1201 /* close hook */
1202 if (sendhook) {
1203 ng_destroy_hook(sendhook);
1205 break;
1206 default:
1207 LEAVE(EPFNOSUPPORT);
1209 break;
1210 case ETHERTYPE_PPPOE_STUPID_SESS:
1211 case ETHERTYPE_PPPOE_SESS:
1213 * find matching peer/session combination.
1215 sendhook = pppoe_findsession(node, wh);
1216 if (sendhook == NULL) {
1217 LEAVE (ENETUNREACH);
1218 break;
1220 sp = sendhook->private;
1221 m_adj(m, sizeof(*wh));
1222 if (m->m_pkthdr.len < length) {
1223 /* Packet too short, dump it */
1224 LEAVE(EMSGSIZE);
1227 /* Also need to trim excess at the end */
1228 if (m->m_pkthdr.len > length) {
1229 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1231 if ( sp->state != PPPOE_CONNECTED) {
1232 if (sp->state == PPPOE_NEWCONNECTED) {
1233 sp->state = PPPOE_CONNECTED;
1235 * Now we have gone to Connected mode,
1236 * Free all resources needed for
1237 * negotiation. Be paranoid about
1238 * whether there may be a timeout.
1240 m_freem(sp->neg->m);
1241 callout_stop(&sp->neg->timeout_ch);
1242 FREE(sp->neg, M_NETGRAPH);
1243 sp->neg = NULL;
1244 } else {
1245 LEAVE (ENETUNREACH);
1246 break;
1249 NG_SEND_DATA( error, sendhook, m, meta);
1250 break;
1251 default:
1252 LEAVE(EPFNOSUPPORT);
1254 } else {
1256 * Not ethernet or debug hook..
1258 * The packet has come in on a normal hook.
1259 * We need to find out what kind of hook,
1260 * So we can decide how to handle it.
1261 * Check the hook's state.
1263 sp = hook->private;
1264 switch (sp->state) {
1265 case PPPOE_NEWCONNECTED:
1266 case PPPOE_CONNECTED: {
1267 static const u_char addrctrl[] = { 0xff, 0x03 };
1268 struct pppoe_full_hdr *wh;
1271 * Remove PPP address and control fields, if any.
1272 * For example, ng_ppp(4) always sends LCP packets
1273 * with address and control fields as required by
1274 * generic PPP. PPPoE is an exception to the rule.
1276 if (m->m_pkthdr.len >= 2) {
1277 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1278 LEAVE(ENOBUFS);
1279 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1280 m_adj(m, 2);
1283 * Bang in a pre-made header, and set the length up
1284 * to be correct. Then send it to the ethernet driver.
1285 * But first correct the length.
1287 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1288 M_PREPEND(m, sizeof(*wh), MB_DONTWAIT);
1289 if (m == NULL) {
1290 LEAVE(ENOBUFS);
1292 wh = mtod(m, struct pppoe_full_hdr *);
1293 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1294 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1295 privp->packets_out++;
1296 break;
1298 case PPPOE_PRIMED:
1300 * A PADI packet is being returned by the application
1301 * that has set up this hook. This indicates that it
1302 * wants us to offer service.
1304 neg = sp->neg;
1305 if (m->m_len < sizeof(*wh)) {
1306 m = m_pullup(m, sizeof(*wh));
1307 if (m == NULL) {
1308 LEAVE(ENOBUFS);
1311 wh = mtod(m, struct pppoe_full_hdr *);
1312 ph = &wh->ph;
1313 session = ntohs(wh->ph.sid);
1314 length = ntohs(wh->ph.length);
1315 code = wh->ph.code;
1316 if ( code != PADI_CODE) {
1317 LEAVE(EINVAL);
1319 callout_stop(&neg->timeout_ch);
1322 * This is the first time we hear
1323 * from the client, so note it's
1324 * unicast address, replacing the
1325 * broadcast address.
1327 bcopy(wh->eh.ether_shost,
1328 neg->pkt->pkt_header.eh.ether_dhost,
1329 ETHER_ADDR_LEN);
1330 sp->state = PPPOE_SOFFER;
1331 neg->timeout = 0;
1332 neg->pkt->pkt_header.ph.code = PADO_CODE;
1335 * start working out the tags to respond with.
1337 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1338 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1339 uniqtag.data.pointer = sp;
1340 init_tags(sp);
1341 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1342 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1343 insert_tag(sp, tag); /* return service */
1345 * If we have a NULL service request
1346 * and have an extra service defined in this hook,
1347 * then also add a tag for the extra service.
1348 * XXX this is a hack. eventually we should be able
1349 * to support advertising many services, not just one
1351 if (((tag == NULL) || (tag->tag_len == 0))
1352 && (neg->service.hdr.tag_len != 0)) {
1353 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1355 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1356 insert_tag(sp, tag); /* returned hostunique */
1357 insert_tag(sp, &uniqtag.hdr);
1358 scan_tags(sp, ph);
1359 make_packet(sp);
1360 sendpacket(sp);
1361 break;
1364 * Packets coming from the hook make no sense
1365 * to sessions in these states. Throw them away.
1367 case PPPOE_SINIT:
1368 case PPPOE_SREQ:
1369 case PPPOE_SOFFER:
1370 case PPPOE_SNONE:
1371 case PPPOE_LISTENING:
1372 case PPPOE_DEAD:
1373 default:
1374 LEAVE(ENETUNREACH);
1377 quit:
1378 NG_FREE_DATA(m, meta);
1379 return error;
1383 * Do local shutdown processing..
1384 * If we are a persistant device, we might refuse to go away, and
1385 * we'd only remove our links and reset ourself.
1387 static int
1388 ng_pppoe_rmnode(node_p node)
1390 const priv_p privdata = node->private;
1393 node->flags |= NG_INVALID;
1394 ng_cutlinks(node);
1395 ng_unname(node);
1396 node->private = NULL;
1397 ng_unref(privdata->node);
1398 FREE(privdata, M_NETGRAPH);
1399 return (0);
1403 * This is called once we've already connected a new hook to the other node.
1404 * It gives us a chance to balk at the last minute.
1406 static int
1407 ng_pppoe_connect(hook_p hook)
1409 /* be really amiable and just say "YUP that's OK by me! " */
1410 return (0);
1414 * Hook disconnection
1416 * Clean up all dangling links and information about the session/hook.
1417 * For this type, removal of the last link destroys the node
1419 static int
1420 ng_pppoe_disconnect(hook_p hook)
1422 node_p node = hook->node;
1423 priv_p privp = node->private;
1424 sessp sp;
1425 int hooks;
1428 hooks = node->numhooks; /* this one already not counted */
1429 if (hook->private == &privp->debug_hook) {
1430 privp->debug_hook = NULL;
1431 } else if (hook->private == &privp->ethernet_hook) {
1432 privp->ethernet_hook = NULL;
1433 ng_rmnode(node);
1434 } else {
1435 sp = hook->private;
1436 if (sp->state != PPPOE_SNONE ) {
1437 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1440 * According to the spec, if we are connected,
1441 * we should send a DISC packet if we are shutting down
1442 * a session.
1444 if ((privp->ethernet_hook)
1445 && ((sp->state == PPPOE_CONNECTED)
1446 || (sp->state == PPPOE_NEWCONNECTED))) {
1447 struct mbuf *m;
1448 struct pppoe_full_hdr *wh;
1449 struct pppoe_tag *tag;
1450 int msglen = strlen(SIGNOFF);
1451 void *dummy = NULL;
1452 int error = 0;
1454 /* revert the stored header to DISC/PADT mode */
1455 wh = &sp->pkt_hdr;
1456 wh->ph.code = PADT_CODE;
1457 if (pppoe_mode == PPPOE_NONSTANDARD)
1458 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1459 else
1460 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1462 /* generate a packet of that type */
1463 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1464 if(m == NULL)
1465 kprintf("pppoe: Session out of mbufs\n");
1466 else {
1467 m->m_pkthdr.rcvif = NULL;
1468 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1469 bcopy((caddr_t)wh, mtod(m, caddr_t),
1470 sizeof(*wh));
1472 * Add a General error message and adjust
1473 * sizes
1475 wh = mtod(m, struct pppoe_full_hdr *);
1476 tag = wh->ph.tag;
1477 tag->tag_type = PTT_GEN_ERR;
1478 tag->tag_len = htons((u_int16_t)msglen);
1479 strncpy(tag->tag_data, SIGNOFF, msglen);
1480 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1481 msglen);
1482 wh->ph.length = htons(sizeof(*tag) + msglen);
1483 NG_SEND_DATA(error, privp->ethernet_hook, m,
1484 dummy);
1488 * As long as we have somewhere to store the timeout handle,
1489 * we may have a timeout pending.. get rid of it.
1491 if (sp->neg) {
1492 callout_stop(&sp->neg->timeout_ch);
1493 if (sp->neg->m)
1494 m_freem(sp->neg->m);
1495 FREE(sp->neg, M_NETGRAPH);
1497 FREE(sp, M_NETGRAPH);
1498 hook->private = NULL;
1499 /* work out how many session hooks there are */
1500 /* Node goes away on last session hook removal */
1501 if (privp->ethernet_hook) hooks -= 1;
1502 if (privp->debug_hook) hooks -= 1;
1504 if (node->numhooks == 0)
1505 ng_rmnode(node);
1506 return (0);
1510 * timeouts come here.
1512 static void
1513 pppoe_ticker(void *arg)
1515 hook_p hook = arg;
1516 sessp sp = hook->private;
1517 negp neg = sp->neg;
1518 int error = 0;
1519 struct mbuf *m0 = NULL;
1520 priv_p privp = hook->node->private;
1521 meta_p dummy = NULL;
1523 crit_enter();
1525 switch(sp->state) {
1527 * resend the last packet, using an exponential backoff.
1528 * After a period of time, stop growing the backoff,
1529 * and either leave it, or revert to the start.
1531 case PPPOE_SINIT:
1532 case PPPOE_SREQ:
1533 /* timeouts on these produce resends */
1534 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1535 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1536 callout_reset(&neg->timeout_ch, neg->timeout * hz,
1537 pppoe_ticker, hook);
1538 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1539 if (sp->state == PPPOE_SREQ) {
1540 /* revert to SINIT mode */
1541 pppoe_start(sp);
1542 } else {
1543 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1546 break;
1547 case PPPOE_PRIMED:
1548 case PPPOE_SOFFER:
1549 /* a timeout on these says "give up" */
1550 ng_destroy_hook(hook);
1551 break;
1552 default:
1553 /* timeouts have no meaning in other states */
1554 kprintf("pppoe: unexpected timeout\n");
1556 crit_exit();
1560 static void
1561 sendpacket(sessp sp)
1563 int error = 0;
1564 struct mbuf *m0 = NULL;
1565 hook_p hook = sp->hook;
1566 negp neg = sp->neg;
1567 priv_p privp = hook->node->private;
1568 meta_p dummy = NULL;
1571 switch(sp->state) {
1572 case PPPOE_LISTENING:
1573 case PPPOE_DEAD:
1574 case PPPOE_SNONE:
1575 case PPPOE_CONNECTED:
1576 kprintf("pppoe: sendpacket: unexpected state\n");
1577 break;
1579 case PPPOE_NEWCONNECTED:
1580 /* send the PADS without a timeout - we're now connected */
1581 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1582 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1583 break;
1585 case PPPOE_PRIMED:
1586 /* No packet to send, but set up the timeout */
1587 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1588 pppoe_ticker, hook);
1589 break;
1591 case PPPOE_SOFFER:
1593 * send the offer but if they don't respond
1594 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1596 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1597 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1598 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1599 pppoe_ticker, hook);
1600 break;
1602 case PPPOE_SINIT:
1603 case PPPOE_SREQ:
1604 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1605 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1606 callout_reset(&neg->timeout_ch, PPPOE_INITIAL_TIMEOUT * hz,
1607 pppoe_ticker, hook);
1608 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1609 break;
1611 default:
1612 error = EINVAL;
1613 kprintf("pppoe: timeout: bad state\n");
1615 /* return (error); */
1619 * Parse an incoming packet to see if any tags should be copied to the
1620 * output packet. Don't do any tags that have been handled in the main
1621 * state machine.
1623 static const struct pppoe_tag*
1624 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1626 const char *const end = (const char *)next_tag(ph);
1627 const char *ptn;
1628 const struct pppoe_tag *pt = &ph->tag[0];
1630 * Keep processing tags while a tag header will still fit.
1633 while((const char*)(pt + 1) <= end) {
1635 * If the tag data would go past the end of the packet, abort.
1637 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1638 if(ptn > end)
1639 return NULL;
1641 switch (pt->tag_type) {
1642 case PTT_RELAY_SID:
1643 insert_tag(sp, pt);
1644 break;
1645 case PTT_EOL:
1646 return NULL;
1647 case PTT_SRV_NAME:
1648 case PTT_AC_NAME:
1649 case PTT_HOST_UNIQ:
1650 case PTT_AC_COOKIE:
1651 case PTT_VENDOR:
1652 case PTT_SRV_ERR:
1653 case PTT_SYS_ERR:
1654 case PTT_GEN_ERR:
1655 break;
1657 pt = (const struct pppoe_tag*)ptn;
1659 return NULL;
1662 static int
1663 pppoe_send_event(sessp sp, enum cmd cmdid)
1665 int error;
1666 struct ng_mesg *msg;
1667 struct ngpppoe_sts *sts;
1670 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1671 sizeof(struct ngpppoe_sts), M_NOWAIT);
1672 if (msg == NULL)
1673 return (ENOMEM);
1674 sts = (struct ngpppoe_sts *)msg->data;
1675 strlcpy(sts->hook, sp->hook->name, NG_HOOKSIZ);
1676 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
1677 return (error);