ifq: Switch to drop-head for default enqueue method.
[dragonfly.git] / sys / netgraph / pppoe / ng_pppoe.c
blobadeb2bbaa5ed8a0c763fc60184df147fbf0edbf4
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 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
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
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>
60 #include <net/if_var.h>
61 #include <net/ethernet.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include "ng_pppoe.h"
67 #define SIGNOFF "session closed"
70 * This section contains the netgraph method declarations for the
71 * pppoe node. These methods define the netgraph pppoe 'type'.
74 static ng_constructor_t ng_pppoe_constructor;
75 static ng_rcvmsg_t ng_pppoe_rcvmsg;
76 static ng_shutdown_t ng_pppoe_rmnode;
77 static ng_newhook_t ng_pppoe_newhook;
78 static ng_connect_t ng_pppoe_connect;
79 static ng_rcvdata_t ng_pppoe_rcvdata;
80 static ng_disconnect_t ng_pppoe_disconnect;
82 /* Netgraph node type descriptor */
83 static struct ng_type typestruct = {
84 NG_VERSION,
85 NG_PPPOE_NODE_TYPE,
86 NULL,
87 ng_pppoe_constructor,
88 ng_pppoe_rcvmsg,
89 ng_pppoe_rmnode,
90 ng_pppoe_newhook,
91 NULL,
92 ng_pppoe_connect,
93 ng_pppoe_rcvdata,
94 ng_pppoe_rcvdata,
95 ng_pppoe_disconnect,
96 NULL
98 NETGRAPH_INIT(pppoe, &typestruct);
101 * States for the session state machine.
102 * These have no meaning if there is no hook attached yet.
104 enum state {
105 PPPOE_SNONE=0, /* [both] Initial state */
106 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
107 PPPOE_SINIT, /* [Client] Sent discovery initiation */
108 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
109 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
110 PPPOE_SREQ, /* [Client] Sent a Request */
111 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
112 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
113 PPPOE_DEAD /* [Both] */
116 #define NUMTAGS 20 /* number of tags we are set up to work with */
119 * Information we store for each hook on each node for negotiating the
120 * session. The mbuf and cluster are freed once negotiation has completed.
121 * The whole negotiation block is then discarded.
124 struct sess_neg {
125 struct mbuf *m; /* holds cluster with last sent packet */
126 union packet *pkt; /* points within the above cluster */
127 struct callout timeout_ch;
128 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
129 u_int numtags;
130 const struct pppoe_tag *tags[NUMTAGS];
131 u_int service_len;
132 u_int ac_name_len;
134 struct datatag service;
135 struct datatag ac_name;
137 typedef struct sess_neg *negp;
140 * Session information that is needed after connection.
142 struct sess_con {
143 hook_p hook;
144 u_int16_t Session_ID;
145 enum state state;
146 char creator[NG_NODESIZ]; /* who to notify */
147 struct pppoe_full_hdr pkt_hdr; /* used when connected */
148 negp neg; /* used when negotiating */
149 /*struct sess_con *hash_next;*/ /* not yet used */
151 typedef struct sess_con *sessp;
154 * Information we store for each node
156 struct PPPOE {
157 node_p node; /* back pointer to node */
158 hook_p ethernet_hook;
159 hook_p debug_hook;
160 u_int packets_in; /* packets in from ethernet */
161 u_int packets_out; /* packets out towards ethernet */
162 u_int32_t flags;
163 /*struct sess_con *buckets[HASH_SIZE];*/ /* not yet used */
165 typedef struct PPPOE *priv_p;
167 struct ether_header eh_prototype =
168 {{0xff,0xff,0xff,0xff,0xff,0xff},
169 {0x00,0x00,0x00,0x00,0x00,0x00},
170 ETHERTYPE_PPPOE_DISC};
172 #define PPPOE_KEEPSTANDARD -1 /* never switch to nonstandard mode */
173 #define PPPOE_STANDARD 0 /* try standard mode (dangerous!) */
174 #define PPPOE_NONSTANDARD 1 /* just be in nonstandard mode */
175 static int pppoe_mode = PPPOE_KEEPSTANDARD;
177 static int
178 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS)
180 int error;
181 int val;
183 val = pppoe_mode;
184 error = sysctl_handle_int(oidp, &val, 0, req);
185 if (error != 0 || req->newptr == NULL)
186 return (error);
187 switch (val) {
188 case PPPOE_NONSTANDARD:
189 pppoe_mode = PPPOE_NONSTANDARD;
190 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
191 break;
192 case PPPOE_STANDARD:
193 pppoe_mode = PPPOE_STANDARD;
194 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
195 break;
196 case PPPOE_KEEPSTANDARD:
197 pppoe_mode = PPPOE_KEEPSTANDARD;
198 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
199 break;
200 default:
201 return (EINVAL);
203 return (0);
206 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW,
207 0, sizeof(int), ngpppoe_set_ethertype, "I", "nonstandard ethertype");
209 union uniq {
210 char bytes[sizeof(void *)];
211 void * pointer;
214 #define LEAVE(x) do { error = x; goto quit; } while(0)
215 static void pppoe_start(sessp sp);
216 static void sendpacket(sessp sp);
217 static void pppoe_ticker(void *arg);
218 static const struct pppoe_tag *scan_tags(sessp sp,
219 const struct pppoe_hdr* ph);
220 static int pppoe_send_event(sessp sp, enum cmd cmdid);
222 /*************************************************************************
223 * Some basic utilities from the Linux version with author's permission.*
224 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
225 ************************************************************************/
228 * Generate a new session id
229 * XXX find out the FreeBSD locking scheme.
231 static u_int16_t
232 get_new_sid(node_p node)
234 static int pppoe_sid = 10;
235 sessp sp;
236 hook_p hook;
237 u_int16_t val;
238 priv_p privp = node->private;
241 restart:
242 val = pppoe_sid++;
244 * Spec says 0xFFFF is reserved.
245 * Also don't use 0x0000
247 if (val == 0xffff) {
248 pppoe_sid = 20;
249 goto restart;
252 /* Check it isn't already in use */
253 LIST_FOREACH(hook, &node->hooks, hooks) {
254 /* don't check special hooks */
255 if ((hook->private == &privp->debug_hook)
256 || (hook->private == &privp->ethernet_hook))
257 continue;
258 sp = hook->private;
259 if (sp->Session_ID == val)
260 goto restart;
263 return val;
268 * Return the location where the next tag can be put
270 static __inline const struct pppoe_tag*
271 next_tag(const struct pppoe_hdr* ph)
273 return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
274 + ntohs(ph->length));
278 * Look for a tag of a specific type
279 * Don't trust any length the other end says.
280 * but assume we already sanity checked ph->length.
282 static const struct pppoe_tag*
283 get_tag(const struct pppoe_hdr* ph, u_int16_t idx)
285 const char *const end = (const char *)next_tag(ph);
286 const char *ptn;
287 const struct pppoe_tag *pt = &ph->tag[0];
289 * Keep processing tags while a tag header will still fit.
292 while((const char*)(pt + 1) <= end) {
294 * If the tag data would go past the end of the packet, abort.
296 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
297 if(ptn > end)
298 return NULL;
300 if(pt->tag_type == idx)
301 return pt;
303 pt = (const struct pppoe_tag*)ptn;
305 return NULL;
308 /**************************************************************************
309 * inlines to initialise or add tags to a session's tag list,
310 **************************************************************************/
312 * Initialise the session's tag list
314 static void
315 init_tags(sessp sp)
318 if(sp->neg == NULL) {
319 kprintf("pppoe: asked to init NULL neg pointer\n");
320 return;
322 sp->neg->numtags = 0;
325 static void
326 insert_tag(sessp sp, const struct pppoe_tag *tp)
328 int i;
329 negp neg;
332 if((neg = sp->neg) == NULL) {
333 kprintf("pppoe: asked to use NULL neg pointer\n");
334 return;
336 if ((i = neg->numtags++) < NUMTAGS) {
337 neg->tags[i] = tp;
338 } else {
339 kprintf("pppoe: asked to add too many tags to packet\n");
340 neg->numtags--;
345 * Make up a packet, using the tags filled out for the session.
347 * Assume that the actual pppoe header and ethernet header
348 * are filled out externally to this routine.
349 * Also assume that neg->wh points to the correct
350 * location at the front of the buffer space.
352 static void
353 make_packet(sessp sp) {
354 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
355 const struct pppoe_tag **tag;
356 char *dp;
357 int count;
358 int tlen;
359 u_int16_t length = 0;
362 if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
363 kprintf("pppoe: make_packet called from wrong state\n");
365 dp = (char *)wh->ph.tag;
366 for (count = 0, tag = sp->neg->tags;
367 ((count < sp->neg->numtags) && (count < NUMTAGS));
368 tag++, count++) {
369 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
370 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
371 kprintf("pppoe: tags too long\n");
372 sp->neg->numtags = count;
373 break; /* XXX chop off what's too long */
375 bcopy(*tag, dp, tlen);
376 length += tlen;
377 dp += tlen;
379 wh->ph.length = htons(length);
380 sp->neg->m->m_len = length + sizeof(*wh);
381 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
384 /**************************************************************************
385 * Routine to match a service offered *
386 **************************************************************************/
388 * Find a hook that has a service string that matches that
389 * we are seeking. for now use a simple string.
390 * In the future we may need something like regexp().
391 * for testing allow a null string to match 1st found and a null service
392 * to match all requests. Also make '*' do the same.
395 #define NG_MATCH_EXACT 1
396 #define NG_MATCH_ANY 2
398 static hook_p
399 pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match)
401 sessp sp = NULL;
402 negp neg = NULL;
403 priv_p privp = node->private;
404 hook_p allhook = NULL;
405 hook_p hook;
408 LIST_FOREACH(hook, &node->hooks, hooks) {
410 /* skip any hook that is debug or ethernet */
411 if ((hook->private == &privp->debug_hook)
412 || (hook->private == &privp->ethernet_hook))
413 continue;
414 sp = hook->private;
416 /* Skip any sessions which are not in LISTEN mode. */
417 if ( sp->state != PPPOE_LISTENING)
418 continue;
420 neg = sp->neg;
422 /* Special case for a blank or "*" service name (wildcard) */
423 if (match == NG_MATCH_ANY && neg->service_len == 1 &&
424 neg->service.data[0] == '*') {
425 allhook = hook;
426 continue;
429 /* If the lengths don't match, that aint it. */
430 if (neg->service_len != svc_len)
431 continue;
433 /* An exact match? */
434 if (svc_len == 0)
435 break;
437 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
438 break;
440 return (hook ? hook : allhook);
442 /**************************************************************************
443 * Routine to find a particular session that matches an incoming packet *
444 **************************************************************************/
445 static hook_p
446 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
448 sessp sp = NULL;
449 hook_p hook = NULL;
450 priv_p privp = node->private;
451 u_int16_t session = ntohs(wh->ph.sid);
454 * find matching peer/session combination.
457 LIST_FOREACH(hook, &node->hooks, hooks) {
458 /* don't check special hooks */
459 if ((hook->private == &privp->debug_hook)
460 || (hook->private == &privp->ethernet_hook)) {
461 continue;
463 sp = hook->private;
464 if ( ( (sp->state == PPPOE_CONNECTED)
465 || (sp->state == PPPOE_NEWCONNECTED) )
466 && (sp->Session_ID == session)
467 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
468 wh->eh.ether_shost,
469 ETHER_ADDR_LEN)) == 0) {
470 break;
473 return (hook);
476 static hook_p
477 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
479 hook_p hook = NULL;
480 priv_p privp = node->private;
481 union uniq uniq;
484 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
485 /* cycle through all known hooks */
486 LIST_FOREACH(hook, &node->hooks, hooks) {
487 /* don't check special hooks */
488 if ((hook->private == &privp->debug_hook)
489 || (hook->private == &privp->ethernet_hook))
490 continue;
491 if (uniq.pointer == hook->private)
492 break;
494 return (hook);
497 /**************************************************************************
498 * start of Netgraph entrypoints *
499 **************************************************************************/
502 * Allocate the private data structure and the generic node
503 * and link them together.
505 * ng_make_node_common() returns with a generic node struct
506 * with a single reference for us.. we transfer it to the
507 * private structure.. when we free the private struct we must
508 * unref the node so it gets freed too.
510 static int
511 ng_pppoe_constructor(node_p *nodep)
513 priv_p privdata;
514 int error;
517 /* Initialize private descriptor */
518 privdata = kmalloc(sizeof(*privdata), M_NETGRAPH, M_NOWAIT | M_ZERO);
519 if (privdata == NULL)
520 return (ENOMEM);
522 /* Call the 'generic' (ie, superclass) node constructor */
523 if ((error = ng_make_node_common(&typestruct, nodep))) {
524 kfree(privdata, M_NETGRAPH);
525 return (error);
528 /* Link structs together; this counts as our one reference to *nodep */
529 (*nodep)->private = privdata;
530 privdata->node = *nodep;
531 return (0);
535 * Give our ok for a hook to be added...
536 * point the hook's private info to the hook structure.
538 * The following hook names are special:
539 * Ethernet: the hook that should be connected to a NIC.
540 * debug: copies of data sent out here (when I write the code).
541 * All other hook names need only be unique. (the framework checks this).
543 static int
544 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
546 const priv_p privp = node->private;
547 sessp sp;
550 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
551 privp->ethernet_hook = hook;
552 hook->private = &privp->ethernet_hook;
553 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
554 privp->debug_hook = hook;
555 hook->private = &privp->debug_hook;
556 } else {
558 * Any other unique name is OK.
559 * The infrastructure has already checked that it's unique,
560 * so just allocate it and hook it in.
562 sp = kmalloc(sizeof(*sp), M_NETGRAPH, M_NOWAIT | M_ZERO);
563 if (sp == NULL)
564 return (ENOMEM);
566 hook->private = sp;
567 sp->hook = hook;
569 return(0);
573 * Get a netgraph control message.
574 * Check it is one we understand. If needed, send a response.
575 * We sometimes save the address for an async action later.
576 * Always free the message.
578 static int
579 ng_pppoe_rcvmsg(node_p node,
580 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
582 priv_p privp = node->private;
583 struct ngpppoe_init_data *ourmsg = NULL;
584 struct ng_mesg *resp = NULL;
585 int error = 0;
586 hook_p hook = NULL;
587 sessp sp = NULL;
588 negp neg = NULL;
591 /* Deal with message according to cookie and command */
592 switch (msg->header.typecookie) {
593 case NGM_PPPOE_COOKIE:
594 switch (msg->header.cmd) {
595 case NGM_PPPOE_CONNECT:
596 case NGM_PPPOE_LISTEN:
597 case NGM_PPPOE_OFFER:
598 case NGM_PPPOE_SERVICE:
599 ourmsg = (struct ngpppoe_init_data *)msg->data;
600 if (( sizeof(*ourmsg) > msg->header.arglen)
601 || ((sizeof(*ourmsg) + ourmsg->data_len)
602 > msg->header.arglen)) {
603 kprintf("pppoe_rcvmsg: bad arg size");
604 LEAVE(EMSGSIZE);
606 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
607 kprintf("pppoe: init data too long (%d)\n",
608 ourmsg->data_len);
609 LEAVE(EMSGSIZE);
611 /* make sure strcmp will terminate safely */
612 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
614 /* cycle through all known hooks */
615 LIST_FOREACH(hook, &node->hooks, hooks) {
616 if (hook->name
617 && strcmp(hook->name, ourmsg->hook) == 0)
618 break;
620 if (hook == NULL) {
621 LEAVE(ENOENT);
623 if ((hook->private == &privp->debug_hook)
624 || (hook->private == &privp->ethernet_hook)) {
625 LEAVE(EINVAL);
627 sp = hook->private;
629 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
631 * Ensure we aren't already listening for this
632 * service.
634 if (pppoe_match_svc(node, ourmsg->data,
635 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
636 LEAVE(EEXIST);
641 * PPPOE_SERVICE advertisments are set up
642 * on sessions that are in PRIMED state.
644 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
645 break;
647 if (sp->state != PPPOE_SNONE) {
648 kprintf("pppoe: Session already active\n");
649 LEAVE(EISCONN);
653 * set up prototype header
655 neg = kmalloc(sizeof(*neg), M_NETGRAPH,
656 M_NOWAIT | M_ZERO);
658 if (neg == NULL) {
659 kprintf("pppoe: Session out of memory\n");
660 LEAVE(ENOMEM);
662 MGETHDR(neg->m, M_NOWAIT, MT_DATA);
663 if(neg->m == NULL) {
664 kprintf("pppoe: Session out of mbufs\n");
665 kfree(neg, M_NETGRAPH);
666 LEAVE(ENOBUFS);
668 neg->m->m_pkthdr.rcvif = NULL;
669 MCLGET(neg->m, M_NOWAIT);
670 if ((neg->m->m_flags & M_EXT) == 0) {
671 kprintf("pppoe: Session out of mcls\n");
672 m_freem(neg->m);
673 kfree(neg, M_NETGRAPH);
674 LEAVE(ENOBUFS);
676 sp->neg = neg;
677 callout_init(&neg->timeout_ch);
678 neg->m->m_len = sizeof(struct pppoe_full_hdr);
679 neg->pkt = mtod(neg->m, union packet*);
680 neg->pkt->pkt_header.eh = eh_prototype;
681 neg->pkt->pkt_header.ph.ver = 0x1;
682 neg->pkt->pkt_header.ph.type = 0x1;
683 neg->pkt->pkt_header.ph.sid = 0x0000;
684 neg->timeout = 0;
686 strlcpy(sp->creator, retaddr, NG_NODESIZ);
688 switch (msg->header.cmd) {
689 case NGM_PPPOE_GET_STATUS:
691 struct ngpppoestat *stats;
693 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
694 if (!resp) {
695 LEAVE(ENOMEM);
697 stats = (struct ngpppoestat *) resp->data;
698 stats->packets_in = privp->packets_in;
699 stats->packets_out = privp->packets_out;
700 break;
702 case NGM_PPPOE_CONNECT:
704 * Check the hook exists and is Uninitialised.
705 * Send a PADI request, and start the timeout logic.
706 * Store the originator of this message so we can send
707 * a success of fail message to them later.
708 * Move the session to SINIT
709 * Set up the session to the correct state and
710 * start it.
712 neg->service.hdr.tag_type = PTT_SRV_NAME;
713 neg->service.hdr.tag_len =
714 htons((u_int16_t)ourmsg->data_len);
715 if (ourmsg->data_len) {
716 bcopy(ourmsg->data,
717 neg->service.data, ourmsg->data_len);
719 neg->service_len = ourmsg->data_len;
720 pppoe_start(sp);
721 break;
722 case NGM_PPPOE_LISTEN:
724 * Check the hook exists and is Uninitialised.
725 * Install the service matching string.
726 * Store the originator of this message so we can send
727 * a success of fail message to them later.
728 * Move the hook to 'LISTENING'
730 neg->service.hdr.tag_type = PTT_SRV_NAME;
731 neg->service.hdr.tag_len =
732 htons((u_int16_t)ourmsg->data_len);
734 if (ourmsg->data_len) {
735 bcopy(ourmsg->data,
736 neg->service.data, ourmsg->data_len);
738 neg->service_len = ourmsg->data_len;
739 neg->pkt->pkt_header.ph.code = PADT_CODE;
741 * wait for PADI packet coming from ethernet
743 sp->state = PPPOE_LISTENING;
744 break;
745 case NGM_PPPOE_OFFER:
747 * Check the hook exists and is Uninitialised.
748 * Store the originator of this message so we can send
749 * a success of fail message to them later.
750 * Store the AC-Name given and go to PRIMED.
752 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
753 neg->ac_name.hdr.tag_len =
754 htons((u_int16_t)ourmsg->data_len);
755 if (ourmsg->data_len) {
756 bcopy(ourmsg->data,
757 neg->ac_name.data, ourmsg->data_len);
759 neg->ac_name_len = ourmsg->data_len;
760 neg->pkt->pkt_header.ph.code = PADO_CODE;
762 * Wait for PADI packet coming from hook
764 sp->state = PPPOE_PRIMED;
765 break;
766 case NGM_PPPOE_SERVICE:
768 * Check the session is primed.
769 * for now just allow ONE service to be advertised.
770 * If you do it twice you just overwrite.
772 if (sp->state != PPPOE_PRIMED) {
773 kprintf("pppoe: Session not primed\n");
774 LEAVE(EISCONN);
776 neg = sp->neg;
777 neg->service.hdr.tag_type = PTT_SRV_NAME;
778 neg->service.hdr.tag_len =
779 htons((u_int16_t)ourmsg->data_len);
781 if (ourmsg->data_len)
782 bcopy(ourmsg->data, neg->service.data,
783 ourmsg->data_len);
784 neg->service_len = ourmsg->data_len;
785 break;
786 default:
787 LEAVE(EINVAL);
789 break;
790 default:
791 LEAVE(EINVAL);
794 /* Take care of synchronous response, if any */
795 if (rptr)
796 *rptr = resp;
797 else if (resp)
798 kfree(resp, M_NETGRAPH);
800 /* Free the message and return */
801 quit:
802 kfree(msg, M_NETGRAPH);
803 return(error);
807 * Start a client into the first state. A separate function because
808 * it can be needed if the negotiation times out.
810 static void
811 pppoe_start(sessp sp)
813 struct {
814 struct pppoe_tag hdr;
815 union uniq data;
816 } __attribute ((packed)) uniqtag;
819 * kick the state machine into starting up
822 sp->state = PPPOE_SINIT;
823 /* reset the packet header to broadcast */
824 sp->neg->pkt->pkt_header.eh = eh_prototype;
825 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
826 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
827 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
828 uniqtag.data.pointer = sp;
829 init_tags(sp);
830 insert_tag(sp, &uniqtag.hdr);
831 insert_tag(sp, &sp->neg->service.hdr);
832 make_packet(sp);
833 sendpacket(sp);
836 static int
837 send_acname(sessp sp, const struct pppoe_tag *tag)
839 int error, tlen;
840 struct ng_mesg *msg;
841 struct ngpppoe_sts *sts;
843 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
844 sizeof(struct ngpppoe_sts), M_NOWAIT);
845 if (msg == NULL)
846 return (ENOMEM);
848 sts = (struct ngpppoe_sts *)msg->data;
849 tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
850 strncpy(sts->hook, tag->tag_data, tlen);
851 sts->hook[tlen] = '\0';
852 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
854 return (error);
857 static int
858 send_sessionid(sessp sp)
860 int error;
861 struct ng_mesg *msg;
863 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
864 sizeof(u_int16_t), M_NOWAIT);
865 if (msg == NULL)
866 return (ENOMEM);
868 *(u_int16_t *)msg->data = sp->Session_ID;
869 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
871 return (error);
875 * Receive data, and do something with it.
876 * The caller will never free m or meta, so
877 * if we use up this data or abort we must free BOTH of these.
879 static int
880 ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
882 node_p node = hook->node;
883 const priv_p privp = node->private;
884 sessp sp = hook->private;
885 const struct pppoe_full_hdr *wh;
886 const struct pppoe_hdr *ph;
887 int error = 0;
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;
897 char ethstr[ETHER_ADDRSTRLEN + 1];
900 if (hook->private == &privp->debug_hook) {
902 * Data from the debug hook gets sent without modification
903 * straight to the ethernet.
905 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
906 privp->packets_out++;
907 } else if (hook->private == &privp->ethernet_hook) {
909 * Incoming data.
910 * Dig out various fields from the packet.
911 * use them to decide where to send it.
914 privp->packets_in++;
915 if( m->m_len < sizeof(*wh)) {
916 m = m_pullup(m, sizeof(*wh)); /* Checks length */
917 if (m == NULL) {
918 kprintf("couldn't m_pullup\n");
919 LEAVE(ENOBUFS);
922 wh = mtod(m, struct pppoe_full_hdr *);
923 ph = &wh->ph;
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 %s\n",
935 kether_ntoa(wh->eh.ether_shost, ethstr));
936 } else if (pppoe_mode == PPPOE_KEEPSTANDARD)
937 log(LOG_NOTICE,
938 "Ignored nonstandard PPPoE packet "
939 "from %s\n",
940 kether_ntoa(wh->eh.ether_shost, ethstr));
941 /* fall through */
942 case ETHERTYPE_PPPOE_DISC:
944 * We need to try to make sure that the tag area
945 * is contiguous, or we could wander off the end
946 * of a buffer and make a mess.
947 * (Linux wouldn't have this problem).
949 /*XXX fix this mess */
951 if (m->m_pkthdr.len <= MHLEN) {
952 if( m->m_len < m->m_pkthdr.len) {
953 m = m_pullup(m, m->m_pkthdr.len);
954 if (m == NULL) {
955 kprintf("couldn't m_pullup\n");
956 LEAVE(ENOBUFS);
960 if (m->m_len != m->m_pkthdr.len) {
962 * It's not all in one piece.
963 * We need to do extra work.
965 kprintf("packet fragmented\n");
966 LEAVE(EMSGSIZE);
969 switch(code) {
970 case PADI_CODE:
972 * We are a server:
973 * Look for a hook with the required service
974 * and send the ENTIRE packet up there.
975 * It should come back to a new hook in
976 * PRIMED state. Look there for further
977 * processing.
979 tag = get_tag(ph, PTT_SRV_NAME);
980 if (tag == NULL) {
981 kprintf("no service tag\n");
982 LEAVE(ENETUNREACH);
984 sendhook = pppoe_match_svc(hook->node,
985 tag->tag_data, ntohs(tag->tag_len),
986 NG_MATCH_ANY);
987 if (sendhook) {
988 NG_SEND_DATA(error, sendhook, m, meta);
989 } else {
990 LEAVE(ENETUNREACH);
992 break;
993 case PADO_CODE:
995 * We are a client:
996 * Use the host_uniq tag to find the
997 * hook this is in response to.
998 * Received #2, now send #3
999 * For now simply accept the first we receive.
1001 utag = get_tag(ph, PTT_HOST_UNIQ);
1002 if ((utag == NULL)
1003 || (ntohs(utag->tag_len) != sizeof(sp))) {
1004 kprintf("no host unique field\n");
1005 LEAVE(ENETUNREACH);
1008 sendhook = pppoe_finduniq(node, utag);
1009 if (sendhook == NULL) {
1010 kprintf("no matching session\n");
1011 LEAVE(ENETUNREACH);
1015 * Check the session is in the right state.
1016 * It needs to be in PPPOE_SINIT.
1018 sp = sendhook->private;
1019 if (sp->state != PPPOE_SINIT) {
1020 kprintf("session in wrong state\n");
1021 LEAVE(ENETUNREACH);
1023 neg = sp->neg;
1024 callout_stop(&neg->timeout_ch);
1027 * This is the first time we hear
1028 * from the server, so note it's
1029 * unicast address, replacing the
1030 * broadcast address .
1032 bcopy(wh->eh.ether_shost,
1033 neg->pkt->pkt_header.eh.ether_dhost,
1034 ETHER_ADDR_LEN);
1035 neg->timeout = 0;
1036 neg->pkt->pkt_header.ph.code = PADR_CODE;
1037 init_tags(sp);
1038 insert_tag(sp, utag); /* Host Unique */
1039 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1040 insert_tag(sp, tag); /* return cookie */
1041 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1042 insert_tag(sp, tag); /* return it */
1043 send_acname(sp, tag);
1045 insert_tag(sp, &neg->service.hdr); /* Service */
1046 scan_tags(sp, ph);
1047 make_packet(sp);
1048 sp->state = PPPOE_SREQ;
1049 sendpacket(sp);
1050 break;
1051 case PADR_CODE:
1054 * We are a server:
1055 * Use the ac_cookie tag to find the
1056 * hook this is in response to.
1058 utag = get_tag(ph, PTT_AC_COOKIE);
1059 if ((utag == NULL)
1060 || (ntohs(utag->tag_len) != sizeof(sp))) {
1061 LEAVE(ENETUNREACH);
1064 sendhook = pppoe_finduniq(node, utag);
1065 if (sendhook == NULL) {
1066 LEAVE(ENETUNREACH);
1070 * Check the session is in the right state.
1071 * It needs to be in PPPOE_SOFFER
1072 * or PPPOE_NEWCONNECTED. If the latter,
1073 * then this is a retry by the client.
1074 * so be nice, and resend.
1076 sp = sendhook->private;
1077 if (sp->state == PPPOE_NEWCONNECTED) {
1079 * Whoa! drop back to resend that
1080 * PADS packet.
1081 * We should still have a copy of it.
1083 sp->state = PPPOE_SOFFER;
1085 if (sp->state != PPPOE_SOFFER) {
1086 LEAVE (ENETUNREACH);
1087 break;
1089 neg = sp->neg;
1090 callout_stop(&neg->timeout_ch);
1091 neg->pkt->pkt_header.ph.code = PADS_CODE;
1092 if (sp->Session_ID == 0)
1093 neg->pkt->pkt_header.ph.sid =
1094 htons(sp->Session_ID
1095 = get_new_sid(node));
1096 send_sessionid(sp);
1097 neg->timeout = 0;
1099 * start working out the tags to respond with.
1101 init_tags(sp);
1102 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1103 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1104 insert_tag(sp, tag);/* return service */
1105 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1106 insert_tag(sp, tag); /* return it */
1107 insert_tag(sp, utag); /* ac_cookie */
1108 scan_tags(sp, ph);
1109 make_packet(sp);
1110 sp->state = PPPOE_NEWCONNECTED;
1111 sendpacket(sp);
1113 * Having sent the last Negotiation header,
1114 * Set up the stored packet header to
1115 * be correct for the actual session.
1116 * But keep the negotialtion stuff
1117 * around in case we need to resend this last
1118 * packet. We'll discard it when we move
1119 * from NEWCONNECTED to CONNECTED
1121 sp->pkt_hdr = neg->pkt->pkt_header;
1122 if (pppoe_mode == PPPOE_NONSTANDARD)
1123 sp->pkt_hdr.eh.ether_type
1124 = ETHERTYPE_PPPOE_STUPID_SESS;
1125 else
1126 sp->pkt_hdr.eh.ether_type
1127 = ETHERTYPE_PPPOE_SESS;
1128 sp->pkt_hdr.ph.code = 0;
1129 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1130 break;
1131 case PADS_CODE:
1133 * We are a client:
1134 * Use the host_uniq tag to find the
1135 * hook this is in response to.
1136 * take the session ID and store it away.
1137 * Also make sure the pre-made header is
1138 * correct and set us into Session mode.
1140 utag = get_tag(ph, PTT_HOST_UNIQ);
1141 if ((utag == NULL)
1142 || (ntohs(utag->tag_len) != sizeof(sp))) {
1143 LEAVE (ENETUNREACH);
1144 break;
1146 sendhook = pppoe_finduniq(node, utag);
1147 if (sendhook == NULL) {
1148 LEAVE(ENETUNREACH);
1152 * Check the session is in the right state.
1153 * It needs to be in PPPOE_SREQ.
1155 sp = sendhook->private;
1156 if (sp->state != PPPOE_SREQ) {
1157 LEAVE(ENETUNREACH);
1159 neg = sp->neg;
1160 callout_stop(&neg->timeout_ch);
1161 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1162 sp->Session_ID = ntohs(wh->ph.sid);
1163 send_sessionid(sp);
1164 neg->timeout = 0;
1165 sp->state = PPPOE_CONNECTED;
1167 * Now we have gone to Connected mode,
1168 * Free all resources needed for
1169 * negotiation.
1170 * Keep a copy of the header we will be using.
1172 sp->pkt_hdr = neg->pkt->pkt_header;
1173 if (pppoe_mode == PPPOE_NONSTANDARD)
1174 sp->pkt_hdr.eh.ether_type
1175 = ETHERTYPE_PPPOE_STUPID_SESS;
1176 else
1177 sp->pkt_hdr.eh.ether_type
1178 = ETHERTYPE_PPPOE_SESS;
1179 sp->pkt_hdr.ph.code = 0;
1180 m_freem(neg->m);
1181 kfree(sp->neg, M_NETGRAPH);
1182 sp->neg = NULL;
1183 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1184 break;
1185 case PADT_CODE:
1187 * Send a 'close' message to the controlling
1188 * process (the one that set us up);
1189 * And then tear everything down.
1191 * Find matching peer/session combination.
1193 sendhook = pppoe_findsession(node, wh);
1194 NG_FREE_DATA(m, meta); /* no longer needed */
1195 if (sendhook == NULL) {
1196 LEAVE(ENETUNREACH);
1198 /* send message to creator */
1199 /* close hook */
1200 if (sendhook) {
1201 ng_destroy_hook(sendhook);
1203 break;
1204 default:
1205 LEAVE(EPFNOSUPPORT);
1207 break;
1208 case ETHERTYPE_PPPOE_STUPID_SESS:
1209 case ETHERTYPE_PPPOE_SESS:
1211 * find matching peer/session combination.
1213 sendhook = pppoe_findsession(node, wh);
1214 if (sendhook == NULL) {
1215 LEAVE (ENETUNREACH);
1216 break;
1218 sp = sendhook->private;
1219 m_adj(m, sizeof(*wh));
1220 if (m->m_pkthdr.len < length) {
1221 /* Packet too short, dump it */
1222 LEAVE(EMSGSIZE);
1225 /* Also need to trim excess at the end */
1226 if (m->m_pkthdr.len > length) {
1227 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1229 if ( sp->state != PPPOE_CONNECTED) {
1230 if (sp->state == PPPOE_NEWCONNECTED) {
1231 sp->state = PPPOE_CONNECTED;
1233 * Now we have gone to Connected mode,
1234 * Free all resources needed for
1235 * negotiation. Be paranoid about
1236 * whether there may be a timeout.
1238 m_freem(sp->neg->m);
1239 callout_stop(&sp->neg->timeout_ch);
1240 kfree(sp->neg, M_NETGRAPH);
1241 sp->neg = NULL;
1242 } else {
1243 LEAVE (ENETUNREACH);
1244 break;
1247 NG_SEND_DATA( error, sendhook, m, meta);
1248 break;
1249 default:
1250 LEAVE(EPFNOSUPPORT);
1252 } else {
1254 * Not ethernet or debug hook..
1256 * The packet has come in on a normal hook.
1257 * We need to find out what kind of hook,
1258 * So we can decide how to handle it.
1259 * Check the hook's state.
1261 sp = hook->private;
1262 switch (sp->state) {
1263 case PPPOE_NEWCONNECTED:
1264 case PPPOE_CONNECTED: {
1265 static const u_char addrctrl[] = { 0xff, 0x03 };
1266 struct pppoe_full_hdr *wh;
1269 * Remove PPP address and control fields, if any.
1270 * For example, ng_ppp(4) always sends LCP packets
1271 * with address and control fields as required by
1272 * generic PPP. PPPoE is an exception to the rule.
1274 if (m->m_pkthdr.len >= 2) {
1275 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1276 LEAVE(ENOBUFS);
1277 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1278 m_adj(m, 2);
1281 * Bang in a pre-made header, and set the length up
1282 * to be correct. Then send it to the ethernet driver.
1283 * But first correct the length.
1285 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1286 M_PREPEND(m, sizeof(*wh), M_NOWAIT);
1287 if (m == NULL) {
1288 LEAVE(ENOBUFS);
1290 wh = mtod(m, struct pppoe_full_hdr *);
1291 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1292 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1293 privp->packets_out++;
1294 break;
1296 case PPPOE_PRIMED:
1298 * A PADI packet is being returned by the application
1299 * that has set up this hook. This indicates that it
1300 * wants us to offer service.
1302 neg = sp->neg;
1303 if (m->m_len < sizeof(*wh)) {
1304 m = m_pullup(m, sizeof(*wh));
1305 if (m == NULL) {
1306 LEAVE(ENOBUFS);
1309 wh = mtod(m, struct pppoe_full_hdr *);
1310 ph = &wh->ph;
1311 length = ntohs(wh->ph.length);
1312 code = wh->ph.code;
1313 if ( code != PADI_CODE) {
1314 LEAVE(EINVAL);
1316 callout_stop(&neg->timeout_ch);
1319 * This is the first time we hear
1320 * from the client, so note it's
1321 * unicast address, replacing the
1322 * broadcast address.
1324 bcopy(wh->eh.ether_shost,
1325 neg->pkt->pkt_header.eh.ether_dhost,
1326 ETHER_ADDR_LEN);
1327 sp->state = PPPOE_SOFFER;
1328 neg->timeout = 0;
1329 neg->pkt->pkt_header.ph.code = PADO_CODE;
1332 * start working out the tags to respond with.
1334 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1335 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1336 uniqtag.data.pointer = sp;
1337 init_tags(sp);
1338 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1339 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1340 insert_tag(sp, tag); /* return service */
1342 * If we have a NULL service request
1343 * and have an extra service defined in this hook,
1344 * then also add a tag for the extra service.
1345 * XXX this is a hack. eventually we should be able
1346 * to support advertising many services, not just one
1348 if (((tag == NULL) || (tag->tag_len == 0))
1349 && (neg->service.hdr.tag_len != 0)) {
1350 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1352 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1353 insert_tag(sp, tag); /* returned hostunique */
1354 insert_tag(sp, &uniqtag.hdr);
1355 scan_tags(sp, ph);
1356 make_packet(sp);
1357 sendpacket(sp);
1358 break;
1361 * Packets coming from the hook make no sense
1362 * to sessions in these states. Throw them away.
1364 case PPPOE_SINIT:
1365 case PPPOE_SREQ:
1366 case PPPOE_SOFFER:
1367 case PPPOE_SNONE:
1368 case PPPOE_LISTENING:
1369 case PPPOE_DEAD:
1370 default:
1371 LEAVE(ENETUNREACH);
1374 quit:
1375 NG_FREE_DATA(m, meta);
1376 return error;
1380 * Do local shutdown processing..
1381 * If we are a persistant device, we might refuse to go away, and
1382 * we'd only remove our links and reset ourself.
1384 static int
1385 ng_pppoe_rmnode(node_p node)
1387 const priv_p privdata = node->private;
1390 node->flags |= NG_INVALID;
1391 ng_cutlinks(node);
1392 ng_unname(node);
1393 node->private = NULL;
1394 ng_unref(privdata->node);
1395 kfree(privdata, M_NETGRAPH);
1396 return (0);
1400 * This is called once we've already connected a new hook to the other node.
1401 * It gives us a chance to balk at the last minute.
1403 static int
1404 ng_pppoe_connect(hook_p hook)
1406 /* be really amiable and just say "YUP that's OK by me! " */
1407 return (0);
1411 * Hook disconnection
1413 * Clean up all dangling links and information about the session/hook.
1414 * For this type, removal of the last link destroys the node
1416 static int
1417 ng_pppoe_disconnect(hook_p hook)
1419 node_p node = hook->node;
1420 priv_p privp = node->private;
1421 sessp sp;
1422 int hooks;
1425 hooks = node->numhooks; /* this one already not counted */
1426 if (hook->private == &privp->debug_hook) {
1427 privp->debug_hook = NULL;
1428 } else if (hook->private == &privp->ethernet_hook) {
1429 privp->ethernet_hook = NULL;
1430 ng_rmnode(node);
1431 } else {
1432 sp = hook->private;
1433 if (sp->state != PPPOE_SNONE ) {
1434 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1437 * According to the spec, if we are connected,
1438 * we should send a DISC packet if we are shutting down
1439 * a session.
1441 if ((privp->ethernet_hook)
1442 && ((sp->state == PPPOE_CONNECTED)
1443 || (sp->state == PPPOE_NEWCONNECTED))) {
1444 struct mbuf *m;
1445 struct pppoe_full_hdr *wh;
1446 struct pppoe_tag *tag;
1447 int msglen = strlen(SIGNOFF);
1448 void *dummy = NULL;
1449 int error = 0;
1451 /* revert the stored header to DISC/PADT mode */
1452 wh = &sp->pkt_hdr;
1453 wh->ph.code = PADT_CODE;
1454 if (pppoe_mode == PPPOE_NONSTANDARD)
1455 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1456 else
1457 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1459 /* generate a packet of that type */
1460 MGETHDR(m, M_NOWAIT, MT_DATA);
1461 if(m == NULL)
1462 kprintf("pppoe: Session out of mbufs\n");
1463 else {
1464 m->m_pkthdr.rcvif = NULL;
1465 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1466 bcopy((caddr_t)wh, mtod(m, caddr_t),
1467 sizeof(*wh));
1469 * Add a General error message and adjust
1470 * sizes
1472 wh = mtod(m, struct pppoe_full_hdr *);
1473 tag = wh->ph.tag;
1474 tag->tag_type = PTT_GEN_ERR;
1475 tag->tag_len = htons((u_int16_t)msglen);
1476 strncpy(tag->tag_data, SIGNOFF, msglen);
1477 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1478 msglen);
1479 wh->ph.length = htons(sizeof(*tag) + msglen);
1480 NG_SEND_DATA(error, privp->ethernet_hook, m,
1481 dummy);
1485 * As long as we have somewhere to store the timeout handle,
1486 * we may have a timeout pending.. get rid of it.
1488 if (sp->neg) {
1489 callout_stop(&sp->neg->timeout_ch);
1490 if (sp->neg->m)
1491 m_freem(sp->neg->m);
1492 kfree(sp->neg, M_NETGRAPH);
1494 kfree(sp, M_NETGRAPH);
1495 hook->private = NULL;
1496 /* work out how many session hooks there are */
1497 /* Node goes away on last session hook removal */
1498 if (privp->ethernet_hook) hooks -= 1;
1499 if (privp->debug_hook) hooks -= 1;
1501 if (node->numhooks == 0)
1502 ng_rmnode(node);
1503 return (0);
1507 * timeouts come here.
1509 static void
1510 pppoe_ticker(void *arg)
1512 hook_p hook = arg;
1513 sessp sp = hook->private;
1514 negp neg = sp->neg;
1515 int error = 0;
1516 struct mbuf *m0 = NULL;
1517 priv_p privp = hook->node->private;
1518 meta_p dummy = NULL;
1520 crit_enter();
1522 switch(sp->state) {
1524 * resend the last packet, using an exponential backoff.
1525 * After a period of time, stop growing the backoff,
1526 * and either leave it, or revert to the start.
1528 case PPPOE_SINIT:
1529 case PPPOE_SREQ:
1530 /* timeouts on these produce resends */
1531 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1532 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1533 callout_reset(&neg->timeout_ch, neg->timeout * hz,
1534 pppoe_ticker, hook);
1535 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1536 if (sp->state == PPPOE_SREQ) {
1537 /* revert to SINIT mode */
1538 pppoe_start(sp);
1539 } else {
1540 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1543 break;
1544 case PPPOE_PRIMED:
1545 case PPPOE_SOFFER:
1546 /* a timeout on these says "give up" */
1547 ng_destroy_hook(hook);
1548 break;
1549 default:
1550 /* timeouts have no meaning in other states */
1551 kprintf("pppoe: unexpected timeout\n");
1553 crit_exit();
1557 static void
1558 sendpacket(sessp sp)
1560 int error = 0;
1561 struct mbuf *m0 = NULL;
1562 hook_p hook = sp->hook;
1563 negp neg = sp->neg;
1564 priv_p privp = hook->node->private;
1565 meta_p dummy = NULL;
1568 switch(sp->state) {
1569 case PPPOE_LISTENING:
1570 case PPPOE_DEAD:
1571 case PPPOE_SNONE:
1572 case PPPOE_CONNECTED:
1573 kprintf("pppoe: sendpacket: unexpected state\n");
1574 break;
1576 case PPPOE_NEWCONNECTED:
1577 /* send the PADS without a timeout - we're now connected */
1578 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1579 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1580 break;
1582 case PPPOE_PRIMED:
1583 /* No packet to send, but set up the timeout */
1584 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1585 pppoe_ticker, hook);
1586 break;
1588 case PPPOE_SOFFER:
1590 * send the offer but if they don't respond
1591 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1593 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1594 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1595 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1596 pppoe_ticker, hook);
1597 break;
1599 case PPPOE_SINIT:
1600 case PPPOE_SREQ:
1601 m0 = m_copypacket(sp->neg->m, M_NOWAIT);
1602 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1603 callout_reset(&neg->timeout_ch, PPPOE_INITIAL_TIMEOUT * hz,
1604 pppoe_ticker, hook);
1605 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1606 break;
1608 default:
1609 error = EINVAL;
1610 kprintf("pppoe: timeout: bad state\n");
1612 /* return (error); */
1616 * Parse an incoming packet to see if any tags should be copied to the
1617 * output packet. Don't do any tags that have been handled in the main
1618 * state machine.
1620 static const struct pppoe_tag*
1621 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1623 const char *const end = (const char *)next_tag(ph);
1624 const char *ptn;
1625 const struct pppoe_tag *pt = &ph->tag[0];
1627 * Keep processing tags while a tag header will still fit.
1630 while((const char*)(pt + 1) <= end) {
1632 * If the tag data would go past the end of the packet, abort.
1634 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1635 if(ptn > end)
1636 return NULL;
1638 switch (pt->tag_type) {
1639 case PTT_RELAY_SID:
1640 insert_tag(sp, pt);
1641 break;
1642 case PTT_EOL:
1643 return NULL;
1644 case PTT_SRV_NAME:
1645 case PTT_AC_NAME:
1646 case PTT_HOST_UNIQ:
1647 case PTT_AC_COOKIE:
1648 case PTT_VENDOR:
1649 case PTT_SRV_ERR:
1650 case PTT_SYS_ERR:
1651 case PTT_GEN_ERR:
1652 break;
1654 pt = (const struct pppoe_tag*)ptn;
1656 return NULL;
1659 static int
1660 pppoe_send_event(sessp sp, enum cmd cmdid)
1662 int error;
1663 struct ng_mesg *msg;
1664 struct ngpppoe_sts *sts;
1667 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1668 sizeof(struct ngpppoe_sts), M_NOWAIT);
1669 if (msg == NULL)
1670 return (ENOMEM);
1671 sts = (struct ngpppoe_sts *)msg->data;
1672 strlcpy(sts->hook, sp->hook->name, NG_HOOKSIZ);
1673 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
1674 return (error);