5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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
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 $
44 #define AAA kprintf("pppoe: %s\n", __func__ );
45 #define BBB kprintf("-%d-", __LINE__ );
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.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>
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 = {
97 NETGRAPH_INIT(pppoe, &typestruct);
100 * States for the session state machine.
101 * These have no meaning if there is no hook attached yet.
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.
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 */
129 const struct pppoe_tag *tags[NUMTAGS];
133 struct datatag service;
134 struct datatag ac_name;
136 typedef struct sess_neg *negp;
139 * Session information that is needed after connection.
143 u_int16_t Session_ID;
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
156 node_p node; /* back pointer to node */
157 hook_p ethernet_hook;
159 u_int packets_in; /* packets in from ethernet */
160 u_int packets_out; /* packets out towards ethernet */
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;
177 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS)
183 error = sysctl_handle_int(oidp, &val, sizeof(int), req);
184 if (error != 0 || req->newptr == NULL)
187 case PPPOE_NONSTANDARD:
188 pppoe_mode = PPPOE_NONSTANDARD;
189 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
192 pppoe_mode = PPPOE_STANDARD;
193 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
195 case PPPOE_KEEPSTANDARD:
196 pppoe_mode = PPPOE_KEEPSTANDARD;
197 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
205 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW,
206 0, sizeof(int), ngpppoe_set_ethertype, "I", "nonstandard ethertype");
209 char bytes[sizeof(void *)];
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.
231 get_new_sid(node_p node)
233 static int pppoe_sid = 10;
237 priv_p privp = node->private;
243 * Spec says 0xFFFF is reserved.
244 * Also don't use 0x0000
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))
258 if (sp->Session_ID == 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);
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));
299 if(pt->tag_type == idx)
302 pt = (const struct pppoe_tag*)ptn;
307 /**************************************************************************
308 * inlines to initialise or add tags to a session's tag list,
309 **************************************************************************/
311 * Initialise the session's tag list
317 if(sp->neg == NULL) {
318 kprintf("pppoe: asked to init NULL neg pointer\n");
321 sp->neg->numtags = 0;
325 insert_tag(sessp sp, const struct pppoe_tag *tp)
331 if((neg = sp->neg) == NULL) {
332 kprintf("pppoe: asked to use NULL neg pointer\n");
335 if ((i = neg->numtags++) < NUMTAGS) {
338 kprintf("pppoe: asked to add too many tags to packet\n");
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.
352 make_packet(sessp sp) {
353 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
354 const struct pppoe_tag **tag;
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));
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);
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
398 pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match)
402 priv_p privp = node->private;
403 hook_p allhook = NULL;
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))
415 /* Skip any sessions which are not in LISTEN mode. */
416 if ( sp->state != PPPOE_LISTENING)
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] == '*') {
428 /* If the lengths don't match, that aint it. */
429 if (neg->service_len != svc_len)
432 /* An exact match? */
436 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
439 return (hook ? hook : allhook);
441 /**************************************************************************
442 * Routine to find a particular session that matches an incoming packet *
443 **************************************************************************/
445 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
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)) {
463 if ( ( (sp->state == PPPOE_CONNECTED)
464 || (sp->state == PPPOE_NEWCONNECTED) )
465 && (sp->Session_ID == session)
466 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
468 ETHER_ADDR_LEN)) == 0) {
476 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
479 priv_p privp = node->private;
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))
490 if (uniq.pointer == hook->private)
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.
510 ng_pppoe_constructor(node_p *nodep)
516 /* Initialize private descriptor */
517 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT | M_ZERO);
518 if (privdata == NULL)
521 /* Call the 'generic' (ie, superclass) node constructor */
522 if ((error = ng_make_node_common(&typestruct, nodep))) {
523 FREE(privdata, M_NETGRAPH);
527 /* Link structs together; this counts as our one reference to *nodep */
528 (*nodep)->private = privdata;
529 privdata->node = *nodep;
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).
543 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
545 const priv_p privp = node->private;
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;
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);
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.
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;
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");
605 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
606 kprintf("pppoe: init data too long (%d)\n",
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) {
616 && strcmp(hook->name, ourmsg->hook) == 0)
622 if ((hook->private == &privp->debug_hook)
623 || (hook->private == &privp->ethernet_hook)) {
628 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
630 * Ensure we aren't already listening for this
633 if (pppoe_match_svc(node, ourmsg->data,
634 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
640 * PPPOE_SERVICE advertisments are set up
641 * on sessions that are in PRIMED state.
643 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
646 if (sp->state != PPPOE_SNONE) {
647 kprintf("pppoe: Session already active\n");
652 * set up prototype header
654 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH,
658 kprintf("pppoe: Session out of memory\n");
661 MGETHDR(neg->m, MB_DONTWAIT, MT_DATA);
663 kprintf("pppoe: Session out of mbufs\n");
664 FREE(neg, M_NETGRAPH);
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");
672 FREE(neg, M_NETGRAPH);
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;
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);
696 stats = (struct ngpppoestat *) resp->data;
697 stats->packets_in = privp->packets_in;
698 stats->packets_out = privp->packets_out;
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
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) {
716 neg->service.data, ourmsg->data_len);
718 neg->service_len = ourmsg->data_len;
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) {
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;
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) {
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;
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");
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,
783 neg->service_len = ourmsg->data_len;
793 /* Take care of synchronous response, if any */
797 FREE(resp, M_NETGRAPH);
799 /* Free the message and return */
801 FREE(msg, M_NETGRAPH);
806 * Start a client into the first state. A separate function because
807 * it can be needed if the negotiation times out.
810 pppoe_start(sessp sp)
813 struct pppoe_tag hdr;
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;
829 insert_tag(sp, &uniqtag.hdr);
830 insert_tag(sp, &sp->neg->service.hdr);
836 send_acname(sessp sp, const struct pppoe_tag *tag)
840 struct ngpppoe_sts *sts;
842 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
843 sizeof(struct ngpppoe_sts), M_NOWAIT);
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);
857 send_sessionid(sessp sp)
862 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
863 sizeof(u_int16_t), M_NOWAIT);
867 *(u_int16_t *)msg->data = sp->Session_ID;
868 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
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.
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;
890 const struct pppoe_tag *utag = NULL, *tag = NULL;
893 struct pppoe_tag hdr;
895 } __attribute ((packed)) uniqtag;
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) {
909 * Dig out various fields from the packet.
910 * use them to decide where to send it.
914 if( m->m_len < sizeof(*wh)) {
915 m = m_pullup(m, sizeof(*wh)); /* Checks length */
917 kprintf("couldn't m_pullup\n");
921 wh = mtod(m, struct pppoe_full_hdr *);
923 session = ntohs(wh->ph.sid);
924 length = ntohs(wh->ph.length);
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;
933 "Switched to nonstandard PPPoE mode due to "
936 wh->eh.ether_shost, ":");
937 } else if (pppoe_mode == PPPOE_KEEPSTANDARD)
939 "Ignored nonstandard PPPoE packet "
942 wh->eh.ether_shost, ":");
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);
957 kprintf("couldn't m_pullup\n");
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");
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
981 tag = get_tag(ph, PTT_SRV_NAME);
983 kprintf("no service tag\n");
986 sendhook = pppoe_match_svc(hook->node,
987 tag->tag_data, ntohs(tag->tag_len),
990 NG_SEND_DATA(error, sendhook, m, meta);
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);
1005 || (ntohs(utag->tag_len) != sizeof(sp))) {
1006 kprintf("no host unique field\n");
1010 sendhook = pppoe_finduniq(node, utag);
1011 if (sendhook == NULL) {
1012 kprintf("no matching session\n");
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");
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,
1038 neg->pkt->pkt_header.ph.code = PADR_CODE;
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 */
1050 sp->state = PPPOE_SREQ;
1057 * Use the ac_cookie tag to find the
1058 * hook this is in response to.
1060 utag = get_tag(ph, PTT_AC_COOKIE);
1062 || (ntohs(utag->tag_len) != sizeof(sp))) {
1066 sendhook = pppoe_finduniq(node, utag);
1067 if (sendhook == NULL) {
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
1083 * We should still have a copy of it.
1085 sp->state = PPPOE_SOFFER;
1087 if (sp->state != PPPOE_SOFFER) {
1088 LEAVE (ENETUNREACH);
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));
1101 * start working out the tags to respond with.
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 */
1112 sp->state = PPPOE_NEWCONNECTED;
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;
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);
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);
1144 || (ntohs(utag->tag_len) != sizeof(sp))) {
1145 LEAVE (ENETUNREACH);
1148 sendhook = pppoe_finduniq(node, utag);
1149 if (sendhook == NULL) {
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) {
1162 callout_stop(&neg->timeout_ch);
1163 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1164 sp->Session_ID = ntohs(wh->ph.sid);
1167 sp->state = PPPOE_CONNECTED;
1169 * Now we have gone to Connected mode,
1170 * Free all resources needed for
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;
1179 sp->pkt_hdr.eh.ether_type
1180 = ETHERTYPE_PPPOE_SESS;
1181 sp->pkt_hdr.ph.code = 0;
1183 FREE(sp->neg, M_NETGRAPH);
1185 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
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) {
1200 /* send message to creator */
1203 ng_destroy_hook(sendhook);
1207 LEAVE(EPFNOSUPPORT);
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);
1220 sp = sendhook->private;
1221 m_adj(m, sizeof(*wh));
1222 if (m->m_pkthdr.len < length) {
1223 /* Packet too short, dump it */
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);
1245 LEAVE (ENETUNREACH);
1249 NG_SEND_DATA( error, sendhook, m, meta);
1252 LEAVE(EPFNOSUPPORT);
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.
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)))
1279 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
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);
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++;
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.
1305 if (m->m_len < sizeof(*wh)) {
1306 m = m_pullup(m, sizeof(*wh));
1311 wh = mtod(m, struct pppoe_full_hdr *);
1313 session = ntohs(wh->ph.sid);
1314 length = ntohs(wh->ph.length);
1316 if ( code != PADI_CODE) {
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,
1330 sp->state = PPPOE_SOFFER;
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;
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);
1364 * Packets coming from the hook make no sense
1365 * to sessions in these states. Throw them away.
1371 case PPPOE_LISTENING:
1378 NG_FREE_DATA(m, meta);
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.
1388 ng_pppoe_rmnode(node_p node)
1390 const priv_p privdata = node->private;
1393 node->flags |= NG_INVALID;
1396 node->private = NULL;
1397 ng_unref(privdata->node);
1398 FREE(privdata, M_NETGRAPH);
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.
1407 ng_pppoe_connect(hook_p hook)
1409 /* be really amiable and just say "YUP that's OK by me! " */
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
1420 ng_pppoe_disconnect(hook_p hook)
1422 node_p node = hook->node;
1423 priv_p privp = node->private;
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;
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
1444 if ((privp->ethernet_hook)
1445 && ((sp->state == PPPOE_CONNECTED)
1446 || (sp->state == PPPOE_NEWCONNECTED))) {
1448 struct pppoe_full_hdr *wh;
1449 struct pppoe_tag *tag;
1450 int msglen = strlen(SIGNOFF);
1454 /* revert the stored header to DISC/PADT mode */
1456 wh->ph.code = PADT_CODE;
1457 if (pppoe_mode == PPPOE_NONSTANDARD)
1458 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1460 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1462 /* generate a packet of that type */
1463 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1465 kprintf("pppoe: Session out of mbufs\n");
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),
1472 * Add a General error message and adjust
1475 wh = mtod(m, struct pppoe_full_hdr *);
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) +
1482 wh->ph.length = htons(sizeof(*tag) + msglen);
1483 NG_SEND_DATA(error, privp->ethernet_hook, m,
1488 * As long as we have somewhere to store the timeout handle,
1489 * we may have a timeout pending.. get rid of it.
1492 callout_stop(&sp->neg->timeout_ch);
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)
1510 * timeouts come here.
1513 pppoe_ticker(void *arg)
1516 sessp sp = hook->private;
1519 struct mbuf *m0 = NULL;
1520 priv_p privp = hook->node->private;
1521 meta_p dummy = NULL;
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.
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 */
1543 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1549 /* a timeout on these says "give up" */
1550 ng_destroy_hook(hook);
1553 /* timeouts have no meaning in other states */
1554 kprintf("pppoe: unexpected timeout\n");
1561 sendpacket(sessp sp)
1564 struct mbuf *m0 = NULL;
1565 hook_p hook = sp->hook;
1567 priv_p privp = hook->node->private;
1568 meta_p dummy = NULL;
1572 case PPPOE_LISTENING:
1575 case PPPOE_CONNECTED:
1576 kprintf("pppoe: sendpacket: unexpected state\n");
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);
1586 /* No packet to send, but set up the timeout */
1587 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1588 pppoe_ticker, hook);
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);
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;
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
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);
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));
1641 switch (pt->tag_type) {
1657 pt = (const struct pppoe_tag*)ptn;
1663 pppoe_send_event(sessp sp, enum cmd cmdid)
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);
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);