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@whistle.com>
40 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
43 #define AAA printf("pppoe: %s\n", __FUNCTION__ );
44 #define BBB printf("-%d-", __LINE__ );
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
56 #include <net/ethernet.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_pppoe.h>
62 #define SIGNOFF "session closed"
65 * This section contains the netgraph method declarations for the
66 * sample node. These methods define the netgraph 'type'.
69 static ng_constructor_t ng_pppoe_constructor;
70 static ng_rcvmsg_t ng_pppoe_rcvmsg;
71 static ng_shutdown_t ng_pppoe_rmnode;
72 static ng_newhook_t ng_pppoe_newhook;
73 static ng_connect_t ng_pppoe_connect;
74 static ng_rcvdata_t ng_pppoe_rcvdata;
75 static ng_disconnect_t ng_pppoe_disconnect;
77 /* Netgraph node type descriptor */
78 static struct ng_type typestruct = {
93 NETGRAPH_INIT(pppoe, &typestruct);
96 * States for the session state machine.
97 * These have no meaning if there is no hook attached yet.
100 PPPOE_SNONE=0, /* [both] Initial state */
101 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
102 PPPOE_SINIT, /* [Client] Sent discovery initiation */
103 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
104 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
105 PPPOE_SREQ, /* [Client] Sent a Request */
106 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
107 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
108 PPPOE_DEAD /* [Both] */
111 #define NUMTAGS 20 /* number of tags we are set up to work with */
114 * Information we store for each hook on each node for negotiating the
115 * session. The mbuf and cluster are freed once negotiation has completed.
116 * The whole negotiation block is then discarded.
120 struct mbuf *m; /* holds cluster with last sent packet */
121 union packet *pkt; /* points within the above cluster */
122 struct callout_handle timeout_handle; /* see timeout(9) */
123 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
125 struct pppoe_tag *tags[NUMTAGS];
129 struct datatag service;
130 struct datatag ac_name;
132 typedef struct sess_neg *negp;
135 * Session information that is needed after connection.
139 u_int16_t Session_ID;
140 struct session *hash_next; /* not yet uesed */
142 char creator[NG_NODELEN + 1]; /* who to notify */
143 struct pppoe_full_hdr pkt_hdr; /* used when connected */
144 negp neg; /* used when negotiating */
146 typedef struct session *sessp;
149 * Information we store for each node
152 node_p node; /* back pointer to node */
153 hook_p ethernet_hook;
155 u_int packets_in; /* packets in from ethernet */
156 u_int packets_out; /* packets out towards ethernet */
158 /*struct session *buckets[HASH_SIZE];*/ /* not yet used */
160 typedef struct PPPOE *priv_p;
162 const struct ether_header eh_prototype =
163 {{0xff,0xff,0xff,0xff,0xff,0xff},
164 {0x00,0x00,0x00,0x00,0x00,0x00},
165 ETHERTYPE_PPPOE_DISC};
168 char bytes[sizeof(void *)];
172 #define LEAVE(x) do { error = x; goto quit; } while(0)
173 static void pppoe_start(sessp sp);
174 static void sendpacket(sessp sp);
175 static void pppoe_ticker(void *arg);
176 static struct pppoe_tag* scan_tags(sessp sp, struct pppoe_hdr* ph);
177 static int pppoe_send_event(sessp sp, enum cmd cmdid);
179 /*************************************************************************
180 * Some basic utilities from the Linux version with author's permission.*
181 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
182 ************************************************************************/
185 * Generate a new session id
186 * XXX find out the FreeBSD locking scheme.
189 get_new_sid(node_p node)
191 static int pppoe_sid = 10;
195 priv_p privp = node->private;
201 * Spec says 0xFFFF is reserved.
202 * Also don't use 0x0000
209 /* Check it isn't already in use */
210 LIST_FOREACH(hook, &node->hooks, hooks) {
211 /* don't check special hooks */
212 if ((hook->private == &privp->debug_hook)
213 || (hook->private == &privp->ethernet_hook))
216 if (sp->Session_ID == val)
225 * Return the location where the next tag can be put
227 static __inline struct pppoe_tag*
228 next_tag(struct pppoe_hdr* ph)
230 return (struct pppoe_tag*)(((char*)&ph->tag[0]) + ntohs(ph->length));
234 * Look for a tag of a specific type
235 * Don't trust any length the other end says.
236 * but assume we already sanity checked ph->length.
238 static struct pppoe_tag*
239 get_tag(struct pppoe_hdr* ph, u_int16_t idx)
241 char *end = (char *)next_tag(ph);
243 struct pppoe_tag *pt = &ph->tag[0];
245 * Keep processing tags while a tag header will still fit.
248 while((char*)(pt + 1) <= end) {
250 * If the tag data would go past the end of the packet, abort.
252 ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
256 if(pt->tag_type == idx)
259 pt = (struct pppoe_tag*)ptn;
264 /**************************************************************************
265 * inlines to initialise or add tags to a session's tag list,
266 **************************************************************************/
268 * Initialise the session's tag list
274 if(sp->neg == NULL) {
275 printf("pppoe: asked to init NULL neg pointer\n");
278 sp->neg->numtags = 0;
282 insert_tag(sessp sp, struct pppoe_tag *tp)
288 if((neg = sp->neg) == NULL) {
289 printf("pppoe: asked to use NULL neg pointer\n");
292 if ((i = neg->numtags++) < NUMTAGS) {
295 printf("pppoe: asked to add too many tags to packet\n");
301 * Make up a packet, using the tags filled out for the session.
303 * Assume that the actual pppoe header and ethernet header
304 * are filled out externally to this routine.
305 * Also assume that neg->wh points to the correct
306 * location at the front of the buffer space.
309 make_packet(sessp sp) {
310 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
311 struct pppoe_tag **tag;
315 u_int16_t length = 0;
318 if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
319 printf("pppoe: make_packet called from wrong state\n");
321 dp = (char *)wh->ph.tag;
322 for (count = 0, tag = sp->neg->tags;
323 ((count < sp->neg->numtags) && (count < NUMTAGS));
325 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
326 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
327 printf("pppoe: tags too long\n");
328 sp->neg->numtags = count;
329 break; /* XXX chop off what's too long */
331 bcopy((char *)*tag, (char *)dp, tlen);
335 wh->ph.length = htons(length);
336 sp->neg->m->m_len = length + sizeof(*wh);
337 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
340 /**************************************************************************
341 * Routine to match a service offered *
342 **************************************************************************/
344 * Find a hook that has a service string that matches that
345 * we are seeking. for now use a simple string.
346 * In the future we may need something like regexp().
347 * for testing allow a null string to match 1st found and a null service
348 * to match all requests. Also make '*' do the same.
351 pppoe_match_svc(node_p node, char *svc_name, int svc_len)
355 priv_p privp = node->private;
359 LIST_FOREACH(hook, &node->hooks, hooks) {
361 /* skip any hook that is debug or ethernet */
362 if ((hook->private == &privp->debug_hook)
363 || (hook->private == &privp->ethernet_hook))
367 /* Skip any sessions which are not in LISTEN mode. */
368 if ( sp->state != PPPOE_LISTENING)
372 /* XXX check validity of this */
373 /* special case, NULL request. match 1st found. */
377 /* XXX check validity of this */
378 /* Special case for a blank or "*" service name (wildcard) */
379 if ((neg->service_len == 0)
380 || ((neg->service_len == 1)
381 && (neg->service.data[0] == '*'))) {
385 /* If the lengths don't match, that aint it. */
386 if (neg->service_len != svc_len)
389 /* An exact match? */
390 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
395 /**************************************************************************
396 * Routine to find a particular session that matches an incoming packet *
397 **************************************************************************/
399 pppoe_findsession(node_p node, struct pppoe_full_hdr *wh)
403 priv_p privp = node->private;
404 u_int16_t session = ntohs(wh->ph.sid);
407 * find matching peer/session combination.
410 LIST_FOREACH(hook, &node->hooks, hooks) {
411 /* don't check special hooks */
412 if ((hook->private == &privp->debug_hook)
413 || (hook->private == &privp->ethernet_hook)) {
417 if ( ( (sp->state == PPPOE_CONNECTED)
418 || (sp->state == PPPOE_NEWCONNECTED) )
419 && (sp->Session_ID == session)
420 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
422 ETHER_ADDR_LEN)) == 0) {
430 pppoe_finduniq(node_p node, struct pppoe_tag *tag)
433 priv_p privp = node->private;
437 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
438 /* cycle through all known hooks */
439 LIST_FOREACH(hook, &node->hooks, hooks) {
440 /* don't check special hooks */
441 if ((hook->private == &privp->debug_hook)
442 || (hook->private == &privp->ethernet_hook))
444 if (uniq.pointer == hook->private)
450 /**************************************************************************
451 * start of Netgraph entrypoints *
452 **************************************************************************/
455 * Allocate the private data structure and the generic node
456 * and link them together.
458 * ng_make_node_common() returns with a generic node struct
459 * with a single reference for us.. we transfer it to the
460 * private structure.. when we free the private struct we must
461 * unref the node so it gets freed too.
463 * If this were a device node than this work would be done in the attach()
464 * routine and the constructor would return EINVAL as you should not be able
465 * to creatednodes that depend on hardware (unless you can add the hardware :)
468 ng_pppoe_constructor(node_p *nodep)
474 /* Initialize private descriptor */
475 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK);
476 if (privdata == NULL)
478 bzero(privdata, sizeof(*privdata));
480 /* Call the 'generic' (ie, superclass) node constructor */
481 if ((error = ng_make_node_common(&typestruct, nodep))) {
482 FREE(privdata, M_NETGRAPH);
486 /* Link structs together; this counts as our one reference to *nodep */
487 (*nodep)->private = privdata;
488 privdata->node = *nodep;
493 * Give our ok for a hook to be added...
494 * point the hook's private info to the hook structure.
496 * The following hook names are special:
497 * Ethernet: the hook that should be connected to a NIC.
498 * debug: copies of data sent out here (when I write the code).
501 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
503 const priv_p privp = node->private;
507 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
508 privp->ethernet_hook = hook;
509 hook->private = &privp->ethernet_hook;
510 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
511 privp->debug_hook = hook;
512 hook->private = &privp->debug_hook;
515 * Any other unique name is OK.
516 * The infrastructure has already checked that it's unique,
517 * so just allocate it and hook it in.
519 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_WAITOK);
523 bzero(sp, sizeof(*sp));
532 * Get a netgraph control message.
533 * Check it is one we understand. If needed, send a response.
534 * We sometimes save the address for an async action later.
535 * Always free the message.
538 ng_pppoe_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
539 struct ng_mesg **rptr, hook_p lasthook)
541 priv_p privp = node->private;
542 struct ngpppoe_init_data *ourmsg = NULL;
543 struct ng_mesg *resp = NULL;
550 /* Deal with message according to cookie and command */
551 switch (msg->header.typecookie) {
552 case NGM_PPPOE_COOKIE:
553 switch (msg->header.cmd) {
554 case NGM_PPPOE_CONNECT:
555 case NGM_PPPOE_LISTEN:
556 case NGM_PPPOE_OFFER:
557 ourmsg = (struct ngpppoe_init_data *)msg->data;
558 if (( sizeof(*ourmsg) > msg->header.arglen)
559 || ((sizeof(*ourmsg) + ourmsg->data_len)
560 > msg->header.arglen)) {
561 printf("pppoe_rcvmsg: bad arg size");
564 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
565 printf("pppoe: init data too long (%d)\n",
569 /* make sure strcmp will terminate safely */
570 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
572 /* cycle through all known hooks */
573 LIST_FOREACH(hook, &node->hooks, hooks) {
575 && strcmp(hook->name, ourmsg->hook) == 0)
581 if ((hook->private == &privp->debug_hook)
582 || (hook->private == &privp->ethernet_hook)) {
586 if (sp->state |= PPPOE_SNONE) {
587 printf("pppoe: Session already active\n");
592 * set up prototype header
594 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_WAITOK);
597 printf("pppoe: Session out of memory\n");
600 bzero(neg, sizeof(*neg));
601 MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
603 printf("pppoe: Session out of mbufs\n");
604 FREE(neg, M_NETGRAPH);
607 neg->m->m_pkthdr.rcvif = NULL;
608 MCLGET(neg->m, M_DONTWAIT);
609 if ((neg->m->m_flags & M_EXT) == 0) {
610 printf("pppoe: Session out of mcls\n");
612 FREE(neg, M_NETGRAPH);
616 callout_handle_init( &neg->timeout_handle);
617 neg->m->m_len = sizeof(struct pppoe_full_hdr);
618 neg->pkt = mtod(neg->m, union packet*);
619 neg->pkt->pkt_header.eh = eh_prototype;
620 neg->pkt->pkt_header.ph.ver = 0x1;
621 neg->pkt->pkt_header.ph.type = 0x1;
622 neg->pkt->pkt_header.ph.sid = 0x0000;
625 strncpy(sp->creator, retaddr, NG_NODELEN);
626 sp->creator[NG_NODELEN] = '\0';
628 switch (msg->header.cmd) {
629 case NGM_PPPOE_GET_STATUS:
631 struct ngpppoestat *stats;
633 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
637 stats = (struct ngpppoestat *) resp->data;
638 stats->packets_in = privp->packets_in;
639 stats->packets_out = privp->packets_out;
642 case NGM_PPPOE_CONNECT:
644 * Check the hook exists and is Uninitialised.
645 * Send a PADI request, and start the timeout logic.
646 * Store the originator of this message so we can send
647 * a success of fail message to them later.
648 * Move the session to SINIT
649 * Set up the session to the correct state and
652 neg->service.hdr.tag_type = PTT_SRV_NAME;
653 neg->service.hdr.tag_len =
654 htons((u_int16_t)ourmsg->data_len);
655 if (ourmsg->data_len) {
657 neg->service.data, ourmsg->data_len);
659 neg->service_len = ourmsg->data_len;
662 case NGM_PPPOE_LISTEN:
664 * Check the hook exists and is Uninitialised.
665 * Install the service matching string.
666 * Store the originator of this message so we can send
667 * a success of fail message to them later.
668 * Move the hook to 'LISTENING'
671 neg->service.hdr.tag_type = PTT_SRV_NAME;
672 neg->service.hdr.tag_len =
673 htons((u_int16_t)ourmsg->data_len);
675 if (ourmsg->data_len) {
677 neg->service.data, ourmsg->data_len);
679 neg->service_len = ourmsg->data_len;
680 neg->pkt->pkt_header.ph.code = PADT_CODE;
682 * wait for PADI packet coming from ethernet
684 sp->state = PPPOE_LISTENING;
686 case NGM_PPPOE_OFFER:
688 * Check the hook exists and is Uninitialised.
689 * Store the originator of this message so we can send
690 * a success of fail message to them later.
691 * Store the AC-Name given and go to PRIMED.
693 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
694 neg->ac_name.hdr.tag_len =
695 htons((u_int16_t)ourmsg->data_len);
696 if (ourmsg->data_len) {
698 neg->ac_name.data, ourmsg->data_len);
700 neg->ac_name_len = ourmsg->data_len;
701 neg->pkt->pkt_header.ph.code = PADO_CODE;
703 * Wait for PADI packet coming from hook
705 sp->state = PPPOE_PRIMED;
715 /* Take care of synchronous response, if any */
719 FREE(resp, M_NETGRAPH);
721 /* Free the message and return */
723 FREE(msg, M_NETGRAPH);
728 * Start a client into the first state. A separate function because
729 * it can be needed if the negotiation times out.
732 pppoe_start(sessp sp)
735 struct pppoe_tag hdr;
740 * kick the state machine into starting up
743 sp->state = PPPOE_SINIT;
744 /* reset the packet header to broadcast */
745 sp->neg->pkt->pkt_header.eh = eh_prototype;
746 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
747 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
748 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
749 uniqtag.data.pointer = sp;
751 insert_tag(sp, &sp->neg->service.hdr);
752 insert_tag(sp, &uniqtag.hdr);
758 * Receive data, and do something with it.
759 * The caller will never free m or meta, so
760 * if we use up this data or abort we must free BOTH of these.
763 ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
764 struct mbuf **ret_m, meta_p *ret_meta)
766 node_p node = hook->node;
767 const priv_p privp = node->private;
768 sessp sp = hook->private;
769 struct pppoe_full_hdr *wh;
770 struct pppoe_hdr *ph;
775 struct pppoe_tag *utag = NULL, *tag = NULL;
778 struct pppoe_tag hdr;
784 if (hook->private == &privp->debug_hook) {
786 * Data from the debug hook gets sent without modification
787 * straight to the ethernet.
789 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
790 privp->packets_out++;
791 } else if (hook->private == &privp->ethernet_hook) {
794 * Dig out various fields from the packet.
795 * use them to decide where to send it.
799 if( m->m_len < sizeof(*wh)) {
800 m = m_pullup(m, sizeof(*wh)); /* Checks length */
802 printf("couldn't m_pullup\n");
806 wh = mtod(m, struct pppoe_full_hdr *);
808 session = ntohs(wh->ph.sid);
809 length = ntohs(wh->ph.length);
811 switch(wh->eh.ether_type) {
812 case ETHERTYPE_PPPOE_DISC:
814 * We need to try to make sure that the tag area
815 * is contiguous, or we could wander off the end
816 * of a buffer and make a mess.
817 * (Linux wouldn't have this problem).
819 /*XXX fix this mess */
821 if (m->m_pkthdr.len <= MHLEN) {
822 if( m->m_len < m->m_pkthdr.len) {
823 m = m_pullup(m, m->m_pkthdr.len);
825 printf("couldn't m_pullup\n");
830 if (m->m_len != m->m_pkthdr.len) {
832 * It's not all in one piece.
833 * We need to do extra work.
835 printf("packet fragmented\n");
843 * Look for a hook with the required service
844 * and send the ENTIRE packet up there.
845 * It should come back to a new hook in
846 * PRIMED state. Look there for further
849 tag = get_tag(ph, PTT_SRV_NAME);
851 printf("no service tag\n");
854 sendhook = pppoe_match_svc(hook->node,
855 tag->tag_data, ntohs(tag->tag_len));
857 NG_SEND_DATA(error, sendhook, m, meta);
859 printf("no such service\n");
866 * Use the host_uniq tag to find the
867 * hook this is in response to.
868 * Received #2, now send #3
869 * For now simply accept the first we receive.
871 utag = get_tag(ph, PTT_HOST_UNIQ);
873 || (ntohs(utag->tag_len) != sizeof(sp))) {
874 printf("no host unique field\n");
878 sendhook = pppoe_finduniq(node, utag);
879 if (sendhook == NULL) {
880 printf("no matching session\n");
885 * Check the session is in the right state.
886 * It needs to be in PPPOE_SINIT.
888 sp = sendhook->private;
889 if (sp->state != PPPOE_SINIT) {
890 printf("session in wrong state\n");
894 untimeout(pppoe_ticker, sendhook,
895 neg->timeout_handle);
898 * This is the first time we hear
899 * from the server, so note it's
900 * unicast address, replacing the
901 * broadcast address .
903 bcopy(wh->eh.ether_shost,
904 neg->pkt->pkt_header.eh.ether_dhost,
907 neg->pkt->pkt_header.ph.code = PADR_CODE;
909 insert_tag(sp, &neg->service.hdr); /* Service */
910 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
911 insert_tag(sp, tag); /* return cookie */
912 if ((tag = get_tag(ph, PTT_AC_NAME)))
913 insert_tag(sp, tag); /* return it */
914 insert_tag(sp, utag); /* Host Unique */
917 sp->state = PPPOE_SREQ;
924 * Use the ac_cookie tag to find the
925 * hook this is in response to.
927 utag = get_tag(ph, PTT_AC_COOKIE);
929 || (ntohs(utag->tag_len) != sizeof(sp))) {
933 sendhook = pppoe_finduniq(node, utag);
934 if (sendhook == NULL) {
939 * Check the session is in the right state.
940 * It needs to be in PPPOE_SOFFER
941 * or PPPOE_NEWCONNECTED. If the latter,
942 * then this is a retry by the client.
943 * so be nice, and resend.
945 sp = sendhook->private;
946 if (sp->state == PPPOE_NEWCONNECTED) {
948 * Whoa! drop back to resend that
950 * We should still have a copy of it.
952 sp->state = PPPOE_SOFFER;
954 if (sp->state != PPPOE_SOFFER) {
959 untimeout(pppoe_ticker, sendhook,
960 neg->timeout_handle);
961 neg->pkt->pkt_header.ph.code = PADS_CODE;
962 if (sp->Session_ID == 0)
963 neg->pkt->pkt_header.ph.sid =
965 = get_new_sid(node));
968 * start working out the tags to respond with.
971 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
972 if ((tag = get_tag(ph, PTT_SRV_NAME)))
973 insert_tag(sp, tag);/* return service */
974 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
975 insert_tag(sp, tag); /* return it */
976 insert_tag(sp, utag); /* ac_cookie */
979 sp->state = PPPOE_NEWCONNECTED;
982 * Having sent the last Negotiation header,
983 * Set up the stored packet header to
984 * be correct for the actual session.
985 * But keep the negotialtion stuff
986 * around in case we need to resend this last
987 * packet. We'll discard it when we move
988 * from NEWCONNECTED to CONNECTED
990 sp->pkt_hdr = neg->pkt->pkt_header;
991 sp->pkt_hdr.eh.ether_type
992 = ETHERTYPE_PPPOE_SESS;
993 sp->pkt_hdr.ph.code = 0;
994 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
999 * Use the host_uniq tag to find the
1000 * hook this is in response to.
1001 * take the session ID and store it away.
1002 * Also make sure the pre-made header is
1003 * correct and set us into Session mode.
1005 utag = get_tag(ph, PTT_HOST_UNIQ);
1007 || (ntohs(utag->tag_len) != sizeof(sp))) {
1008 LEAVE (ENETUNREACH);
1011 sendhook = pppoe_finduniq(node, utag);
1012 if (sendhook == NULL) {
1017 * Check the session is in the right state.
1018 * It needs to be in PPPOE_SREQ.
1020 sp = sendhook->private;
1021 if (sp->state != PPPOE_SREQ) {
1025 untimeout(pppoe_ticker, sendhook,
1026 neg->timeout_handle);
1027 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1028 sp->Session_ID = ntohs(wh->ph.sid);
1030 sp->state = PPPOE_CONNECTED;
1032 * Now we have gone to Connected mode,
1033 * Free all resources needed for
1035 * Keep a copy of the header we will be using.
1037 sp->pkt_hdr = neg->pkt->pkt_header;
1038 sp->pkt_hdr.eh.ether_type
1039 = ETHERTYPE_PPPOE_SESS;
1040 sp->pkt_hdr.ph.code = 0;
1042 FREE(sp->neg, M_NETGRAPH);
1044 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1048 * Send a 'close' message to the controlling
1049 * process (the one that set us up);
1050 * And then tear everything down.
1052 * Find matching peer/session combination.
1054 sendhook = pppoe_findsession(node, wh);
1055 NG_FREE_DATA(m, meta); /* no longer needed */
1056 if (sendhook == NULL) {
1059 /* send message to creator */
1062 ng_destroy_hook(sendhook);
1066 LEAVE(EPFNOSUPPORT);
1069 case ETHERTYPE_PPPOE_SESS:
1071 * find matching peer/session combination.
1073 sendhook = pppoe_findsession(node, wh);
1074 if (sendhook == NULL) {
1075 LEAVE (ENETUNREACH);
1078 sp = sendhook->private;
1079 m_adj(m, sizeof(*wh));
1080 if (m->m_pkthdr.len < length) {
1081 /* Packet too short, dump it */
1085 /* Also need to trim excess at the end */
1086 if (m->m_pkthdr.len > length) {
1087 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1089 if ( sp->state != PPPOE_CONNECTED) {
1090 if (sp->state == PPPOE_NEWCONNECTED) {
1091 sp->state = PPPOE_CONNECTED;
1093 * Now we have gone to Connected mode,
1094 * Free all resources needed for
1095 * negotiation. Be paranoid about
1096 * whether there may be a timeout.
1098 m_freem(sp->neg->m);
1099 untimeout(pppoe_ticker, sendhook,
1100 sp->neg->timeout_handle);
1101 FREE(sp->neg, M_NETGRAPH);
1104 LEAVE (ENETUNREACH);
1108 NG_SEND_DATA( error, sendhook, m, meta);
1111 LEAVE(EPFNOSUPPORT);
1115 * Not ethernet or debug hook..
1117 * The packet has come in on a normal hook.
1118 * We need to find out what kind of hook,
1119 * So we can decide how to handle it.
1120 * Check the hook's state.
1123 switch (sp->state) {
1124 case PPPOE_NEWCONNECTED:
1125 case PPPOE_CONNECTED: {
1126 struct pppoe_full_hdr *wh;
1128 * Bang in a pre-made header, and set the length up
1129 * to be correct. Then send it to the ethernet driver.
1130 * But first correct the length.
1132 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1133 M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1137 wh = mtod(m, struct pppoe_full_hdr *);
1138 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1139 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1140 privp->packets_out++;
1145 * A PADI packet is being returned by the application
1146 * that has set up this hook. This indicates that it
1147 * wants us to offer service.
1150 if (m->m_len < sizeof(*wh)) {
1151 m = m_pullup(m, sizeof(*wh));
1156 wh = mtod(m, struct pppoe_full_hdr *);
1158 session = ntohs(wh->ph.sid);
1159 length = ntohs(wh->ph.length);
1161 if ( code != PADI_CODE) {
1164 untimeout(pppoe_ticker, hook,
1165 neg->timeout_handle);
1168 * This is the first time we hear
1169 * from the client, so note it's
1170 * unicast address, replacing the
1171 * broadcast address.
1173 bcopy(wh->eh.ether_shost,
1174 neg->pkt->pkt_header.eh.ether_dhost,
1176 sp->state = PPPOE_SOFFER;
1178 neg->pkt->pkt_header.ph.code = PADO_CODE;
1181 * start working out the tags to respond with.
1183 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1184 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1185 uniqtag.data.pointer = sp;
1187 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1188 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1189 insert_tag(sp, tag); /* return service */
1190 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1191 insert_tag(sp, tag); /* returned hostunique */
1192 insert_tag(sp, &uniqtag.hdr);
1193 /* XXX maybe put the tag in the session store */
1200 * Packets coming from the hook make no sense
1201 * to sessions in these states. Throw them away.
1207 case PPPOE_LISTENING:
1214 NG_FREE_DATA(m, meta);
1219 * Do local shutdown processing..
1220 * If we are a persistant device, we might refuse to go away, and
1221 * we'd only remove our links and reset ourself.
1224 ng_pppoe_rmnode(node_p node)
1226 const priv_p privdata = node->private;
1229 node->flags |= NG_INVALID;
1232 node->private = NULL;
1233 ng_unref(privdata->node);
1234 FREE(privdata, M_NETGRAPH);
1239 * This is called once we've already connected a new hook to the other node.
1240 * It gives us a chance to balk at the last minute.
1243 ng_pppoe_connect(hook_p hook)
1245 /* be really amiable and just say "YUP that's OK by me! " */
1250 * Hook disconnection
1252 * Clean up all dangling links and information about the session/hook.
1253 * For this type, removal of the last link destroys the node
1256 ng_pppoe_disconnect(hook_p hook)
1258 node_p node = hook->node;
1259 priv_p privp = node->private;
1264 hooks = node->numhooks; /* this one already not counted */
1265 if (hook->private == &privp->debug_hook) {
1266 privp->debug_hook = NULL;
1267 } else if (hook->private == &privp->ethernet_hook) {
1268 privp->ethernet_hook = NULL;
1272 if (sp->state != PPPOE_SNONE ) {
1273 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1276 * According to the spec, if we are connected,
1277 * we should send a DISC packet if we are shutting down
1280 if ((privp->ethernet_hook)
1281 && ((sp->state == PPPOE_CONNECTED)
1282 || (sp->state == PPPOE_NEWCONNECTED))) {
1284 struct pppoe_full_hdr *wh;
1285 struct pppoe_tag *tag;
1286 int msglen = strlen(SIGNOFF);
1290 /* revert the stored header to DISC/PADT mode */
1292 wh->ph.code = PADT_CODE;
1293 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1295 /* generate a packet of that type */
1296 MGETHDR(m, M_DONTWAIT, MT_DATA);
1298 printf("pppoe: Session out of mbufs\n");
1300 m->m_pkthdr.rcvif = NULL;
1301 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1302 bcopy((caddr_t)wh, mtod(m, caddr_t),
1305 * Add a General error message and adjust
1308 wh = mtod(m, struct pppoe_full_hdr *);
1310 tag->tag_type = PTT_GEN_ERR;
1311 tag->tag_len = htons((u_int16_t)msglen);
1312 strncpy(tag->tag_data, SIGNOFF, msglen);
1313 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1315 wh->ph.length = htons(sizeof(*tag) + msglen);
1316 NG_SEND_DATA(error, privp->ethernet_hook, m,
1321 * As long as we have somewhere to store teh timeout handle,
1322 * we may have a timeout pending.. get rid of it.
1325 untimeout(pppoe_ticker, hook, sp->neg->timeout_handle);
1327 m_freem(sp->neg->m);
1328 FREE(sp->neg, M_NETGRAPH);
1330 FREE(sp, M_NETGRAPH);
1331 hook->private = NULL;
1332 /* work out how many session hooks there are */
1333 /* Node goes away on last session hook removal */
1334 if (privp->ethernet_hook) hooks -= 1;
1335 if (privp->debug_hook) hooks -= 1;
1337 if (node->numhooks == 0)
1343 * timeouts come here.
1346 pppoe_ticker(void *arg)
1350 sessp sp = hook->private;
1353 struct mbuf *m0 = NULL;
1354 priv_p privp = hook->node->private;
1355 meta_p dummy = NULL;
1360 * resend the last packet, using an exponential backoff.
1361 * After a period of time, stop growing the backoff,
1362 * and either leave it, or revert to the start.
1366 /* timeouts on these produce resends */
1367 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1368 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1369 neg->timeout_handle = timeout(pppoe_ticker,
1370 hook, neg->timeout * hz);
1371 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1372 if (sp->state == PPPOE_SREQ) {
1373 /* revert to SINIT mode */
1376 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1382 /* a timeout on these says "give up" */
1383 ng_destroy_hook(hook);
1386 /* timeouts have no meaning in other states */
1387 printf("pppoe: unexpected timeout\n");
1394 sendpacket(sessp sp)
1397 struct mbuf *m0 = NULL;
1398 hook_p hook = sp->hook;
1400 priv_p privp = hook->node->private;
1401 meta_p dummy = NULL;
1405 case PPPOE_LISTENING:
1408 case PPPOE_CONNECTED:
1409 printf("pppoe: sendpacket: unexpected state\n");
1412 case PPPOE_NEWCONNECTED:
1413 /* send the PADS without a timeout - we're now connected */
1414 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1415 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1419 /* No packet to send, but set up the timeout */
1420 neg->timeout_handle = timeout(pppoe_ticker,
1421 hook, PPPOE_OFFER_TIMEOUT * hz);
1426 * send the offer but if they don't respond
1427 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1429 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1430 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1431 neg->timeout_handle = timeout(pppoe_ticker,
1432 hook, PPPOE_OFFER_TIMEOUT * hz);
1437 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1438 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1439 neg->timeout_handle = timeout(pppoe_ticker, hook,
1440 (hz * PPPOE_INITIAL_TIMEOUT));
1441 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1446 printf("pppoe: timeout: bad state\n");
1448 /* return (error); */
1452 * Parse an incoming packet to see if any tags should be copied to the
1453 * output packet. Don't do any tags that have been handled in the main
1456 static struct pppoe_tag*
1457 scan_tags(sessp sp, struct pppoe_hdr* ph)
1459 char *end = (char *)next_tag(ph);
1461 struct pppoe_tag *pt = &ph->tag[0];
1463 * Keep processing tags while a tag header will still fit.
1466 while((char*)(pt + 1) <= end) {
1468 * If the tag data would go past the end of the packet, abort.
1470 ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len));
1474 switch (pt->tag_type) {
1490 pt = (struct pppoe_tag*)ptn;
1496 pppoe_send_event(sessp sp, enum cmd cmdid)
1499 struct ng_mesg *msg;
1500 struct ngpppoe_sts *sts;
1503 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1504 sizeof(struct ngpppoe_sts), M_NOWAIT);
1505 sts = (struct ngpppoe_sts *)msg->data;
1506 strncpy(sts->hook, sp->hook->name, NG_HOOKLEN + 1);
1507 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);