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.8 2005/06/02 22:11:46 swildner Exp $
41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
44 #define AAA printf("pppoe: %s\n", __func__ );
45 #define BBB printf("-%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_NODELEN + 1]; /* 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 printf("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 printf("pppoe: asked to use NULL neg pointer\n");
335 if ((i = neg->numtags++) < NUMTAGS) {
338 printf("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 printf("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 printf("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);
518 if (privdata == NULL)
520 bzero(privdata, sizeof(*privdata));
522 /* Call the 'generic' (ie, superclass) node constructor */
523 if ((error = ng_make_node_common(&typestruct, nodep))) {
524 FREE(privdata, M_NETGRAPH);
528 /* Link structs together; this counts as our one reference to *nodep */
529 (*nodep)->private = privdata;
530 privdata->node = *nodep;
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).
544 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
546 const priv_p privp = node->private;
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;
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 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_NOWAIT);
566 bzero(sp, sizeof(*sp));
575 * Get a netgraph control message.
576 * Check it is one we understand. If needed, send a response.
577 * We sometimes save the address for an async action later.
578 * Always free the message.
581 ng_pppoe_rcvmsg(node_p node,
582 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
584 priv_p privp = node->private;
585 struct ngpppoe_init_data *ourmsg = NULL;
586 struct ng_mesg *resp = NULL;
593 /* Deal with message according to cookie and command */
594 switch (msg->header.typecookie) {
595 case NGM_PPPOE_COOKIE:
596 switch (msg->header.cmd) {
597 case NGM_PPPOE_CONNECT:
598 case NGM_PPPOE_LISTEN:
599 case NGM_PPPOE_OFFER:
600 case NGM_PPPOE_SERVICE:
601 ourmsg = (struct ngpppoe_init_data *)msg->data;
602 if (( sizeof(*ourmsg) > msg->header.arglen)
603 || ((sizeof(*ourmsg) + ourmsg->data_len)
604 > msg->header.arglen)) {
605 printf("pppoe_rcvmsg: bad arg size");
608 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
609 printf("pppoe: init data too long (%d)\n",
613 /* make sure strcmp will terminate safely */
614 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
616 /* cycle through all known hooks */
617 LIST_FOREACH(hook, &node->hooks, hooks) {
619 && strcmp(hook->name, ourmsg->hook) == 0)
625 if ((hook->private == &privp->debug_hook)
626 || (hook->private == &privp->ethernet_hook)) {
631 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
633 * Ensure we aren't already listening for this
636 if (pppoe_match_svc(node, ourmsg->data,
637 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
643 * PPPOE_SERVICE advertisments are set up
644 * on sessions that are in PRIMED state.
646 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
649 if (sp->state |= PPPOE_SNONE) {
650 printf("pppoe: Session already active\n");
655 * set up prototype header
657 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_NOWAIT);
660 printf("pppoe: Session out of memory\n");
663 bzero(neg, sizeof(*neg));
664 MGETHDR(neg->m, MB_DONTWAIT, MT_DATA);
666 printf("pppoe: Session out of mbufs\n");
667 FREE(neg, M_NETGRAPH);
670 neg->m->m_pkthdr.rcvif = NULL;
671 MCLGET(neg->m, MB_DONTWAIT);
672 if ((neg->m->m_flags & M_EXT) == 0) {
673 printf("pppoe: Session out of mcls\n");
675 FREE(neg, M_NETGRAPH);
679 callout_init(&neg->timeout_ch);
680 neg->m->m_len = sizeof(struct pppoe_full_hdr);
681 neg->pkt = mtod(neg->m, union packet*);
682 neg->pkt->pkt_header.eh = eh_prototype;
683 neg->pkt->pkt_header.ph.ver = 0x1;
684 neg->pkt->pkt_header.ph.type = 0x1;
685 neg->pkt->pkt_header.ph.sid = 0x0000;
688 strncpy(sp->creator, retaddr, NG_NODELEN);
689 sp->creator[NG_NODELEN] = '\0';
691 switch (msg->header.cmd) {
692 case NGM_PPPOE_GET_STATUS:
694 struct ngpppoestat *stats;
696 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
700 stats = (struct ngpppoestat *) resp->data;
701 stats->packets_in = privp->packets_in;
702 stats->packets_out = privp->packets_out;
705 case NGM_PPPOE_CONNECT:
707 * Check the hook exists and is Uninitialised.
708 * Send a PADI request, and start the timeout logic.
709 * Store the originator of this message so we can send
710 * a success of fail message to them later.
711 * Move the session to SINIT
712 * Set up the session to the correct state and
715 neg->service.hdr.tag_type = PTT_SRV_NAME;
716 neg->service.hdr.tag_len =
717 htons((u_int16_t)ourmsg->data_len);
718 if (ourmsg->data_len) {
720 neg->service.data, ourmsg->data_len);
722 neg->service_len = ourmsg->data_len;
725 case NGM_PPPOE_LISTEN:
727 * Check the hook exists and is Uninitialised.
728 * Install the service matching string.
729 * Store the originator of this message so we can send
730 * a success of fail message to them later.
731 * Move the hook to 'LISTENING'
733 neg->service.hdr.tag_type = PTT_SRV_NAME;
734 neg->service.hdr.tag_len =
735 htons((u_int16_t)ourmsg->data_len);
737 if (ourmsg->data_len) {
739 neg->service.data, ourmsg->data_len);
741 neg->service_len = ourmsg->data_len;
742 neg->pkt->pkt_header.ph.code = PADT_CODE;
744 * wait for PADI packet coming from ethernet
746 sp->state = PPPOE_LISTENING;
748 case NGM_PPPOE_OFFER:
750 * Check the hook exists and is Uninitialised.
751 * Store the originator of this message so we can send
752 * a success of fail message to them later.
753 * Store the AC-Name given and go to PRIMED.
755 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
756 neg->ac_name.hdr.tag_len =
757 htons((u_int16_t)ourmsg->data_len);
758 if (ourmsg->data_len) {
760 neg->ac_name.data, ourmsg->data_len);
762 neg->ac_name_len = ourmsg->data_len;
763 neg->pkt->pkt_header.ph.code = PADO_CODE;
765 * Wait for PADI packet coming from hook
767 sp->state = PPPOE_PRIMED;
769 case NGM_PPPOE_SERVICE:
771 * Check the session is primed.
772 * for now just allow ONE service to be advertised.
773 * If you do it twice you just overwrite.
775 if (sp->state != PPPOE_PRIMED) {
776 printf("pppoe: Session not primed\n");
780 neg->service.hdr.tag_type = PTT_SRV_NAME;
781 neg->service.hdr.tag_len =
782 htons((u_int16_t)ourmsg->data_len);
784 if (ourmsg->data_len)
785 bcopy(ourmsg->data, neg->service.data,
787 neg->service_len = ourmsg->data_len;
797 /* Take care of synchronous response, if any */
801 FREE(resp, M_NETGRAPH);
803 /* Free the message and return */
805 FREE(msg, M_NETGRAPH);
810 * Start a client into the first state. A separate function because
811 * it can be needed if the negotiation times out.
814 pppoe_start(sessp sp)
817 struct pppoe_tag hdr;
819 } __attribute ((packed)) uniqtag;
822 * kick the state machine into starting up
825 sp->state = PPPOE_SINIT;
826 /* reset the packet header to broadcast */
827 sp->neg->pkt->pkt_header.eh = eh_prototype;
828 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
829 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
830 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
831 uniqtag.data.pointer = sp;
833 insert_tag(sp, &uniqtag.hdr);
834 insert_tag(sp, &sp->neg->service.hdr);
840 send_acname(sessp sp, const struct pppoe_tag *tag)
844 struct ngpppoe_sts *sts;
846 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
847 sizeof(struct ngpppoe_sts), M_NOWAIT);
851 sts = (struct ngpppoe_sts *)msg->data;
852 tlen = min(NG_HOOKLEN, ntohs(tag->tag_len));
853 strncpy(sts->hook, tag->tag_data, tlen);
854 sts->hook[tlen] = '\0';
855 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
861 send_sessionid(sessp sp)
866 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
867 sizeof(u_int16_t), M_NOWAIT);
871 *(u_int16_t *)msg->data = sp->Session_ID;
872 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
878 * Receive data, and do something with it.
879 * The caller will never free m or meta, so
880 * if we use up this data or abort we must free BOTH of these.
883 ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
885 node_p node = hook->node;
886 const priv_p privp = node->private;
887 sessp sp = hook->private;
888 const struct pppoe_full_hdr *wh;
889 const struct pppoe_hdr *ph;
894 const struct pppoe_tag *utag = NULL, *tag = NULL;
897 struct pppoe_tag hdr;
899 } __attribute ((packed)) uniqtag;
903 if (hook->private == &privp->debug_hook) {
905 * Data from the debug hook gets sent without modification
906 * straight to the ethernet.
908 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
909 privp->packets_out++;
910 } else if (hook->private == &privp->ethernet_hook) {
913 * Dig out various fields from the packet.
914 * use them to decide where to send it.
918 if( m->m_len < sizeof(*wh)) {
919 m = m_pullup(m, sizeof(*wh)); /* Checks length */
921 printf("couldn't m_pullup\n");
925 wh = mtod(m, struct pppoe_full_hdr *);
927 session = ntohs(wh->ph.sid);
928 length = ntohs(wh->ph.length);
930 switch(wh->eh.ether_type) {
931 case ETHERTYPE_PPPOE_STUPID_DISC:
932 if (pppoe_mode == PPPOE_STANDARD) {
933 pppoe_mode = PPPOE_NONSTANDARD;
934 eh_prototype.ether_type =
935 ETHERTYPE_PPPOE_STUPID_DISC;
937 "Switched to nonstandard PPPoE mode due to "
940 wh->eh.ether_shost, ":");
941 } else if (pppoe_mode == PPPOE_KEEPSTANDARD)
943 "Ignored nonstandard PPPoE packet "
946 wh->eh.ether_shost, ":");
948 case ETHERTYPE_PPPOE_DISC:
950 * We need to try to make sure that the tag area
951 * is contiguous, or we could wander off the end
952 * of a buffer and make a mess.
953 * (Linux wouldn't have this problem).
955 /*XXX fix this mess */
957 if (m->m_pkthdr.len <= MHLEN) {
958 if( m->m_len < m->m_pkthdr.len) {
959 m = m_pullup(m, m->m_pkthdr.len);
961 printf("couldn't m_pullup\n");
966 if (m->m_len != m->m_pkthdr.len) {
968 * It's not all in one piece.
969 * We need to do extra work.
971 printf("packet fragmented\n");
979 * Look for a hook with the required service
980 * and send the ENTIRE packet up there.
981 * It should come back to a new hook in
982 * PRIMED state. Look there for further
985 tag = get_tag(ph, PTT_SRV_NAME);
987 printf("no service tag\n");
990 sendhook = pppoe_match_svc(hook->node,
991 tag->tag_data, ntohs(tag->tag_len),
994 NG_SEND_DATA(error, sendhook, m, meta);
1002 * Use the host_uniq tag to find the
1003 * hook this is in response to.
1004 * Received #2, now send #3
1005 * For now simply accept the first we receive.
1007 utag = get_tag(ph, PTT_HOST_UNIQ);
1009 || (ntohs(utag->tag_len) != sizeof(sp))) {
1010 printf("no host unique field\n");
1014 sendhook = pppoe_finduniq(node, utag);
1015 if (sendhook == NULL) {
1016 printf("no matching session\n");
1021 * Check the session is in the right state.
1022 * It needs to be in PPPOE_SINIT.
1024 sp = sendhook->private;
1025 if (sp->state != PPPOE_SINIT) {
1026 printf("session in wrong state\n");
1030 callout_stop(&neg->timeout_ch);
1033 * This is the first time we hear
1034 * from the server, so note it's
1035 * unicast address, replacing the
1036 * broadcast address .
1038 bcopy(wh->eh.ether_shost,
1039 neg->pkt->pkt_header.eh.ether_dhost,
1042 neg->pkt->pkt_header.ph.code = PADR_CODE;
1044 insert_tag(sp, utag); /* Host Unique */
1045 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1046 insert_tag(sp, tag); /* return cookie */
1047 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1048 insert_tag(sp, tag); /* return it */
1049 send_acname(sp, tag);
1051 insert_tag(sp, &neg->service.hdr); /* Service */
1054 sp->state = PPPOE_SREQ;
1061 * Use the ac_cookie tag to find the
1062 * hook this is in response to.
1064 utag = get_tag(ph, PTT_AC_COOKIE);
1066 || (ntohs(utag->tag_len) != sizeof(sp))) {
1070 sendhook = pppoe_finduniq(node, utag);
1071 if (sendhook == NULL) {
1076 * Check the session is in the right state.
1077 * It needs to be in PPPOE_SOFFER
1078 * or PPPOE_NEWCONNECTED. If the latter,
1079 * then this is a retry by the client.
1080 * so be nice, and resend.
1082 sp = sendhook->private;
1083 if (sp->state == PPPOE_NEWCONNECTED) {
1085 * Whoa! drop back to resend that
1087 * We should still have a copy of it.
1089 sp->state = PPPOE_SOFFER;
1091 if (sp->state != PPPOE_SOFFER) {
1092 LEAVE (ENETUNREACH);
1096 callout_stop(&neg->timeout_ch);
1097 neg->pkt->pkt_header.ph.code = PADS_CODE;
1098 if (sp->Session_ID == 0)
1099 neg->pkt->pkt_header.ph.sid =
1100 htons(sp->Session_ID
1101 = get_new_sid(node));
1105 * start working out the tags to respond with.
1108 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1109 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1110 insert_tag(sp, tag);/* return service */
1111 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1112 insert_tag(sp, tag); /* return it */
1113 insert_tag(sp, utag); /* ac_cookie */
1116 sp->state = PPPOE_NEWCONNECTED;
1119 * Having sent the last Negotiation header,
1120 * Set up the stored packet header to
1121 * be correct for the actual session.
1122 * But keep the negotialtion stuff
1123 * around in case we need to resend this last
1124 * packet. We'll discard it when we move
1125 * from NEWCONNECTED to CONNECTED
1127 sp->pkt_hdr = neg->pkt->pkt_header;
1128 if (pppoe_mode == PPPOE_NONSTANDARD)
1129 sp->pkt_hdr.eh.ether_type
1130 = ETHERTYPE_PPPOE_STUPID_SESS;
1132 sp->pkt_hdr.eh.ether_type
1133 = ETHERTYPE_PPPOE_SESS;
1134 sp->pkt_hdr.ph.code = 0;
1135 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1140 * Use the host_uniq tag to find the
1141 * hook this is in response to.
1142 * take the session ID and store it away.
1143 * Also make sure the pre-made header is
1144 * correct and set us into Session mode.
1146 utag = get_tag(ph, PTT_HOST_UNIQ);
1148 || (ntohs(utag->tag_len) != sizeof(sp))) {
1149 LEAVE (ENETUNREACH);
1152 sendhook = pppoe_finduniq(node, utag);
1153 if (sendhook == NULL) {
1158 * Check the session is in the right state.
1159 * It needs to be in PPPOE_SREQ.
1161 sp = sendhook->private;
1162 if (sp->state != PPPOE_SREQ) {
1166 callout_stop(&neg->timeout_ch);
1167 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1168 sp->Session_ID = ntohs(wh->ph.sid);
1171 sp->state = PPPOE_CONNECTED;
1173 * Now we have gone to Connected mode,
1174 * Free all resources needed for
1176 * Keep a copy of the header we will be using.
1178 sp->pkt_hdr = neg->pkt->pkt_header;
1179 if (pppoe_mode == PPPOE_NONSTANDARD)
1180 sp->pkt_hdr.eh.ether_type
1181 = ETHERTYPE_PPPOE_STUPID_SESS;
1183 sp->pkt_hdr.eh.ether_type
1184 = ETHERTYPE_PPPOE_SESS;
1185 sp->pkt_hdr.ph.code = 0;
1187 FREE(sp->neg, M_NETGRAPH);
1189 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1193 * Send a 'close' message to the controlling
1194 * process (the one that set us up);
1195 * And then tear everything down.
1197 * Find matching peer/session combination.
1199 sendhook = pppoe_findsession(node, wh);
1200 NG_FREE_DATA(m, meta); /* no longer needed */
1201 if (sendhook == NULL) {
1204 /* send message to creator */
1207 ng_destroy_hook(sendhook);
1211 LEAVE(EPFNOSUPPORT);
1214 case ETHERTYPE_PPPOE_STUPID_SESS:
1215 case ETHERTYPE_PPPOE_SESS:
1217 * find matching peer/session combination.
1219 sendhook = pppoe_findsession(node, wh);
1220 if (sendhook == NULL) {
1221 LEAVE (ENETUNREACH);
1224 sp = sendhook->private;
1225 m_adj(m, sizeof(*wh));
1226 if (m->m_pkthdr.len < length) {
1227 /* Packet too short, dump it */
1231 /* Also need to trim excess at the end */
1232 if (m->m_pkthdr.len > length) {
1233 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1235 if ( sp->state != PPPOE_CONNECTED) {
1236 if (sp->state == PPPOE_NEWCONNECTED) {
1237 sp->state = PPPOE_CONNECTED;
1239 * Now we have gone to Connected mode,
1240 * Free all resources needed for
1241 * negotiation. Be paranoid about
1242 * whether there may be a timeout.
1244 m_freem(sp->neg->m);
1245 callout_stop(&sp->neg->timeout_ch);
1246 FREE(sp->neg, M_NETGRAPH);
1249 LEAVE (ENETUNREACH);
1253 NG_SEND_DATA( error, sendhook, m, meta);
1256 LEAVE(EPFNOSUPPORT);
1260 * Not ethernet or debug hook..
1262 * The packet has come in on a normal hook.
1263 * We need to find out what kind of hook,
1264 * So we can decide how to handle it.
1265 * Check the hook's state.
1268 switch (sp->state) {
1269 case PPPOE_NEWCONNECTED:
1270 case PPPOE_CONNECTED: {
1271 static const u_char addrctrl[] = { 0xff, 0x03 };
1272 struct pppoe_full_hdr *wh;
1275 * Remove PPP address and control fields, if any.
1276 * For example, ng_ppp(4) always sends LCP packets
1277 * with address and control fields as required by
1278 * generic PPP. PPPoE is an exception to the rule.
1280 if (m->m_pkthdr.len >= 2) {
1281 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1283 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1287 * Bang in a pre-made header, and set the length up
1288 * to be correct. Then send it to the ethernet driver.
1289 * But first correct the length.
1291 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1292 M_PREPEND(m, sizeof(*wh), MB_DONTWAIT);
1296 wh = mtod(m, struct pppoe_full_hdr *);
1297 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1298 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1299 privp->packets_out++;
1304 * A PADI packet is being returned by the application
1305 * that has set up this hook. This indicates that it
1306 * wants us to offer service.
1309 if (m->m_len < sizeof(*wh)) {
1310 m = m_pullup(m, sizeof(*wh));
1315 wh = mtod(m, struct pppoe_full_hdr *);
1317 session = ntohs(wh->ph.sid);
1318 length = ntohs(wh->ph.length);
1320 if ( code != PADI_CODE) {
1323 callout_stop(&neg->timeout_ch);
1326 * This is the first time we hear
1327 * from the client, so note it's
1328 * unicast address, replacing the
1329 * broadcast address.
1331 bcopy(wh->eh.ether_shost,
1332 neg->pkt->pkt_header.eh.ether_dhost,
1334 sp->state = PPPOE_SOFFER;
1336 neg->pkt->pkt_header.ph.code = PADO_CODE;
1339 * start working out the tags to respond with.
1341 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1342 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1343 uniqtag.data.pointer = sp;
1345 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1346 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1347 insert_tag(sp, tag); /* return service */
1349 * If we have a NULL service request
1350 * and have an extra service defined in this hook,
1351 * then also add a tag for the extra service.
1352 * XXX this is a hack. eventually we should be able
1353 * to support advertising many services, not just one
1355 if (((tag == NULL) || (tag->tag_len == 0))
1356 && (neg->service.hdr.tag_len != 0)) {
1357 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1359 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1360 insert_tag(sp, tag); /* returned hostunique */
1361 insert_tag(sp, &uniqtag.hdr);
1368 * Packets coming from the hook make no sense
1369 * to sessions in these states. Throw them away.
1375 case PPPOE_LISTENING:
1382 NG_FREE_DATA(m, meta);
1387 * Do local shutdown processing..
1388 * If we are a persistant device, we might refuse to go away, and
1389 * we'd only remove our links and reset ourself.
1392 ng_pppoe_rmnode(node_p node)
1394 const priv_p privdata = node->private;
1397 node->flags |= NG_INVALID;
1400 node->private = NULL;
1401 ng_unref(privdata->node);
1402 FREE(privdata, M_NETGRAPH);
1407 * This is called once we've already connected a new hook to the other node.
1408 * It gives us a chance to balk at the last minute.
1411 ng_pppoe_connect(hook_p hook)
1413 /* be really amiable and just say "YUP that's OK by me! " */
1418 * Hook disconnection
1420 * Clean up all dangling links and information about the session/hook.
1421 * For this type, removal of the last link destroys the node
1424 ng_pppoe_disconnect(hook_p hook)
1426 node_p node = hook->node;
1427 priv_p privp = node->private;
1432 hooks = node->numhooks; /* this one already not counted */
1433 if (hook->private == &privp->debug_hook) {
1434 privp->debug_hook = NULL;
1435 } else if (hook->private == &privp->ethernet_hook) {
1436 privp->ethernet_hook = NULL;
1440 if (sp->state != PPPOE_SNONE ) {
1441 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1444 * According to the spec, if we are connected,
1445 * we should send a DISC packet if we are shutting down
1448 if ((privp->ethernet_hook)
1449 && ((sp->state == PPPOE_CONNECTED)
1450 || (sp->state == PPPOE_NEWCONNECTED))) {
1452 struct pppoe_full_hdr *wh;
1453 struct pppoe_tag *tag;
1454 int msglen = strlen(SIGNOFF);
1458 /* revert the stored header to DISC/PADT mode */
1460 wh->ph.code = PADT_CODE;
1461 if (pppoe_mode == PPPOE_NONSTANDARD)
1462 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1464 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1466 /* generate a packet of that type */
1467 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1469 printf("pppoe: Session out of mbufs\n");
1471 m->m_pkthdr.rcvif = NULL;
1472 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1473 bcopy((caddr_t)wh, mtod(m, caddr_t),
1476 * Add a General error message and adjust
1479 wh = mtod(m, struct pppoe_full_hdr *);
1481 tag->tag_type = PTT_GEN_ERR;
1482 tag->tag_len = htons((u_int16_t)msglen);
1483 strncpy(tag->tag_data, SIGNOFF, msglen);
1484 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1486 wh->ph.length = htons(sizeof(*tag) + msglen);
1487 NG_SEND_DATA(error, privp->ethernet_hook, m,
1492 * As long as we have somewhere to store the timeout handle,
1493 * we may have a timeout pending.. get rid of it.
1496 callout_stop(&sp->neg->timeout_ch);
1498 m_freem(sp->neg->m);
1499 FREE(sp->neg, M_NETGRAPH);
1501 FREE(sp, M_NETGRAPH);
1502 hook->private = NULL;
1503 /* work out how many session hooks there are */
1504 /* Node goes away on last session hook removal */
1505 if (privp->ethernet_hook) hooks -= 1;
1506 if (privp->debug_hook) hooks -= 1;
1508 if (node->numhooks == 0)
1514 * timeouts come here.
1517 pppoe_ticker(void *arg)
1520 sessp sp = hook->private;
1523 struct mbuf *m0 = NULL;
1524 priv_p privp = hook->node->private;
1525 meta_p dummy = NULL;
1531 * resend the last packet, using an exponential backoff.
1532 * After a period of time, stop growing the backoff,
1533 * and either leave it, or revert to the start.
1537 /* timeouts on these produce resends */
1538 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1539 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1540 callout_reset(&neg->timeout_ch, neg->timeout * hz,
1541 pppoe_ticker, hook);
1542 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1543 if (sp->state == PPPOE_SREQ) {
1544 /* revert to SINIT mode */
1547 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1553 /* a timeout on these says "give up" */
1554 ng_destroy_hook(hook);
1557 /* timeouts have no meaning in other states */
1558 printf("pppoe: unexpected timeout\n");
1565 sendpacket(sessp sp)
1568 struct mbuf *m0 = NULL;
1569 hook_p hook = sp->hook;
1571 priv_p privp = hook->node->private;
1572 meta_p dummy = NULL;
1576 case PPPOE_LISTENING:
1579 case PPPOE_CONNECTED:
1580 printf("pppoe: sendpacket: unexpected state\n");
1583 case PPPOE_NEWCONNECTED:
1584 /* send the PADS without a timeout - we're now connected */
1585 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1586 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1590 /* No packet to send, but set up the timeout */
1591 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1592 pppoe_ticker, hook);
1597 * send the offer but if they don't respond
1598 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1600 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1601 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1602 callout_reset(&neg->timeout_ch, PPPOE_OFFER_TIMEOUT * hz,
1603 pppoe_ticker, hook);
1608 m0 = m_copypacket(sp->neg->m, MB_DONTWAIT);
1609 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1610 callout_reset(&neg->timeout_ch, PPPOE_INITIAL_TIMEOUT * hz,
1611 pppoe_ticker, hook);
1612 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1617 printf("pppoe: timeout: bad state\n");
1619 /* return (error); */
1623 * Parse an incoming packet to see if any tags should be copied to the
1624 * output packet. Don't do any tags that have been handled in the main
1627 static const struct pppoe_tag*
1628 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1630 const char *const end = (const char *)next_tag(ph);
1632 const struct pppoe_tag *pt = &ph->tag[0];
1634 * Keep processing tags while a tag header will still fit.
1637 while((const char*)(pt + 1) <= end) {
1639 * If the tag data would go past the end of the packet, abort.
1641 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1645 switch (pt->tag_type) {
1661 pt = (const struct pppoe_tag*)ptn;
1667 pppoe_send_event(sessp sp, enum cmd cmdid)
1670 struct ng_mesg *msg;
1671 struct ngpppoe_sts *sts;
1674 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1675 sizeof(struct ngpppoe_sts), M_NOWAIT);
1678 sts = (struct ngpppoe_sts *)msg->data;
1679 strncpy(sts->hook, sp->hook->name, NG_HOOKLEN + 1);
1680 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);