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.3 2003/08/07 21:17:32 dillon Exp $
41 * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
44 #define AAA printf("pppoe: %s\n", __FUNCTION__ );
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 <net/ethernet.h>
60 #include <netgraph/ng_message.h>
61 #include <netgraph/netgraph.h>
64 #define SIGNOFF "session closed"
67 * This section contains the netgraph method declarations for the
68 * pppoe node. These methods define the netgraph pppoe 'type'.
71 static ng_constructor_t ng_pppoe_constructor;
72 static ng_rcvmsg_t ng_pppoe_rcvmsg;
73 static ng_shutdown_t ng_pppoe_rmnode;
74 static ng_newhook_t ng_pppoe_newhook;
75 static ng_connect_t ng_pppoe_connect;
76 static ng_rcvdata_t ng_pppoe_rcvdata;
77 static ng_disconnect_t ng_pppoe_disconnect;
79 /* Netgraph node type descriptor */
80 static struct ng_type typestruct = {
95 NETGRAPH_INIT(pppoe, &typestruct);
98 * States for the session state machine.
99 * These have no meaning if there is no hook attached yet.
102 PPPOE_SNONE=0, /* [both] Initial state */
103 PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
104 PPPOE_SINIT, /* [Client] Sent discovery initiation */
105 PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
106 PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
107 PPPOE_SREQ, /* [Client] Sent a Request */
108 PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
109 PPPOE_CONNECTED, /* [Both] Connection established, Data received */
110 PPPOE_DEAD /* [Both] */
113 #define NUMTAGS 20 /* number of tags we are set up to work with */
116 * Information we store for each hook on each node for negotiating the
117 * session. The mbuf and cluster are freed once negotiation has completed.
118 * The whole negotiation block is then discarded.
122 struct mbuf *m; /* holds cluster with last sent packet */
123 union packet *pkt; /* points within the above cluster */
124 struct callout_handle timeout_handle; /* see timeout(9) */
125 u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
127 const struct pppoe_tag *tags[NUMTAGS];
131 struct datatag service;
132 struct datatag ac_name;
134 typedef struct sess_neg *negp;
137 * Session information that is needed after connection.
141 u_int16_t Session_ID;
143 char creator[NG_NODELEN + 1]; /* who to notify */
144 struct pppoe_full_hdr pkt_hdr; /* used when connected */
145 negp neg; /* used when negotiating */
146 /*struct sess_con *hash_next;*/ /* not yet used */
148 typedef struct sess_con *sessp;
151 * Information we store for each node
154 node_p node; /* back pointer to node */
155 hook_p ethernet_hook;
157 u_int packets_in; /* packets in from ethernet */
158 u_int packets_out; /* packets out towards ethernet */
160 /*struct sess_con *buckets[HASH_SIZE];*/ /* not yet used */
162 typedef struct PPPOE *priv_p;
164 struct ether_header eh_prototype =
165 {{0xff,0xff,0xff,0xff,0xff,0xff},
166 {0x00,0x00,0x00,0x00,0x00,0x00},
167 ETHERTYPE_PPPOE_DISC};
169 static int nonstandard;
171 ngpppoe_set_ethertype(SYSCTL_HANDLER_ARGS)
177 error = sysctl_handle_int(oidp, &val, sizeof(int), req);
178 if (error != 0 || req->newptr == NULL)
182 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
185 eh_prototype.ether_type = ETHERTYPE_PPPOE_DISC;
190 SYSCTL_PROC(_net_graph, OID_AUTO, nonstandard_pppoe, CTLTYPE_INT | CTLFLAG_RW,
191 0, sizeof(int), ngpppoe_set_ethertype, "I", "nonstandard ethertype");
194 char bytes[sizeof(void *)];
198 #define LEAVE(x) do { error = x; goto quit; } while(0)
199 static void pppoe_start(sessp sp);
200 static void sendpacket(sessp sp);
201 static void pppoe_ticker(void *arg);
202 static const struct pppoe_tag *scan_tags(sessp sp,
203 const struct pppoe_hdr* ph);
204 static int pppoe_send_event(sessp sp, enum cmd cmdid);
206 /*************************************************************************
207 * Some basic utilities from the Linux version with author's permission.*
208 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
209 ************************************************************************/
212 * Generate a new session id
213 * XXX find out the FreeBSD locking scheme.
216 get_new_sid(node_p node)
218 static int pppoe_sid = 10;
222 priv_p privp = node->private;
228 * Spec says 0xFFFF is reserved.
229 * Also don't use 0x0000
236 /* Check it isn't already in use */
237 LIST_FOREACH(hook, &node->hooks, hooks) {
238 /* don't check special hooks */
239 if ((hook->private == &privp->debug_hook)
240 || (hook->private == &privp->ethernet_hook))
243 if (sp->Session_ID == val)
252 * Return the location where the next tag can be put
254 static __inline const struct pppoe_tag*
255 next_tag(const struct pppoe_hdr* ph)
257 return (const struct pppoe_tag*)(((const char*)&ph->tag[0])
258 + ntohs(ph->length));
262 * Look for a tag of a specific type
263 * Don't trust any length the other end says.
264 * but assume we already sanity checked ph->length.
266 static const struct pppoe_tag*
267 get_tag(const struct pppoe_hdr* ph, u_int16_t idx)
269 const char *const end = (const char *)next_tag(ph);
271 const struct pppoe_tag *pt = &ph->tag[0];
273 * Keep processing tags while a tag header will still fit.
276 while((const char*)(pt + 1) <= end) {
278 * If the tag data would go past the end of the packet, abort.
280 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
284 if(pt->tag_type == idx)
287 pt = (const struct pppoe_tag*)ptn;
292 /**************************************************************************
293 * inlines to initialise or add tags to a session's tag list,
294 **************************************************************************/
296 * Initialise the session's tag list
302 if(sp->neg == NULL) {
303 printf("pppoe: asked to init NULL neg pointer\n");
306 sp->neg->numtags = 0;
310 insert_tag(sessp sp, const struct pppoe_tag *tp)
316 if((neg = sp->neg) == NULL) {
317 printf("pppoe: asked to use NULL neg pointer\n");
320 if ((i = neg->numtags++) < NUMTAGS) {
323 printf("pppoe: asked to add too many tags to packet\n");
329 * Make up a packet, using the tags filled out for the session.
331 * Assume that the actual pppoe header and ethernet header
332 * are filled out externally to this routine.
333 * Also assume that neg->wh points to the correct
334 * location at the front of the buffer space.
337 make_packet(sessp sp) {
338 struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
339 const struct pppoe_tag **tag;
343 u_int16_t length = 0;
346 if ((sp->neg == NULL) || (sp->neg->m == NULL)) {
347 printf("pppoe: make_packet called from wrong state\n");
349 dp = (char *)wh->ph.tag;
350 for (count = 0, tag = sp->neg->tags;
351 ((count < sp->neg->numtags) && (count < NUMTAGS));
353 tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
354 if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
355 printf("pppoe: tags too long\n");
356 sp->neg->numtags = count;
357 break; /* XXX chop off what's too long */
359 bcopy(*tag, (char *)dp, tlen);
363 wh->ph.length = htons(length);
364 sp->neg->m->m_len = length + sizeof(*wh);
365 sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
368 /**************************************************************************
369 * Routine to match a service offered *
370 **************************************************************************/
372 * Find a hook that has a service string that matches that
373 * we are seeking. for now use a simple string.
374 * In the future we may need something like regexp().
375 * for testing allow a null string to match 1st found and a null service
376 * to match all requests. Also make '*' do the same.
379 #define NG_MATCH_EXACT 1
380 #define NG_MATCH_ANY 2
383 pppoe_match_svc(node_p node, const char *svc_name, int svc_len, int match)
387 priv_p privp = node->private;
388 hook_p allhook = NULL;
392 LIST_FOREACH(hook, &node->hooks, hooks) {
394 /* skip any hook that is debug or ethernet */
395 if ((hook->private == &privp->debug_hook)
396 || (hook->private == &privp->ethernet_hook))
400 /* Skip any sessions which are not in LISTEN mode. */
401 if ( sp->state != PPPOE_LISTENING)
406 /* Special case for a blank or "*" service name (wildcard) */
407 if (match == NG_MATCH_ANY && neg->service_len == 1 &&
408 neg->service.data[0] == '*') {
413 /* If the lengths don't match, that aint it. */
414 if (neg->service_len != svc_len)
417 /* An exact match? */
421 if (strncmp(svc_name, neg->service.data, svc_len) == 0)
424 return (hook ? hook : allhook);
426 /**************************************************************************
427 * Routine to find a particular session that matches an incoming packet *
428 **************************************************************************/
430 pppoe_findsession(node_p node, const struct pppoe_full_hdr *wh)
434 priv_p privp = node->private;
435 u_int16_t session = ntohs(wh->ph.sid);
438 * find matching peer/session combination.
441 LIST_FOREACH(hook, &node->hooks, hooks) {
442 /* don't check special hooks */
443 if ((hook->private == &privp->debug_hook)
444 || (hook->private == &privp->ethernet_hook)) {
448 if ( ( (sp->state == PPPOE_CONNECTED)
449 || (sp->state == PPPOE_NEWCONNECTED) )
450 && (sp->Session_ID == session)
451 && (bcmp(sp->pkt_hdr.eh.ether_dhost,
453 ETHER_ADDR_LEN)) == 0) {
461 pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
464 priv_p privp = node->private;
468 bcopy(tag->tag_data, uniq.bytes, sizeof(void *));
469 /* cycle through all known hooks */
470 LIST_FOREACH(hook, &node->hooks, hooks) {
471 /* don't check special hooks */
472 if ((hook->private == &privp->debug_hook)
473 || (hook->private == &privp->ethernet_hook))
475 if (uniq.pointer == hook->private)
481 /**************************************************************************
482 * start of Netgraph entrypoints *
483 **************************************************************************/
486 * Allocate the private data structure and the generic node
487 * and link them together.
489 * ng_make_node_common() returns with a generic node struct
490 * with a single reference for us.. we transfer it to the
491 * private structure.. when we free the private struct we must
492 * unref the node so it gets freed too.
495 ng_pppoe_constructor(node_p *nodep)
501 /* Initialize private descriptor */
502 MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_NOWAIT);
503 if (privdata == NULL)
505 bzero(privdata, sizeof(*privdata));
507 /* Call the 'generic' (ie, superclass) node constructor */
508 if ((error = ng_make_node_common(&typestruct, nodep))) {
509 FREE(privdata, M_NETGRAPH);
513 /* Link structs together; this counts as our one reference to *nodep */
514 (*nodep)->private = privdata;
515 privdata->node = *nodep;
520 * Give our ok for a hook to be added...
521 * point the hook's private info to the hook structure.
523 * The following hook names are special:
524 * Ethernet: the hook that should be connected to a NIC.
525 * debug: copies of data sent out here (when I write the code).
526 * All other hook names need only be unique. (the framework checks this).
529 ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
531 const priv_p privp = node->private;
535 if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
536 privp->ethernet_hook = hook;
537 hook->private = &privp->ethernet_hook;
538 } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
539 privp->debug_hook = hook;
540 hook->private = &privp->debug_hook;
543 * Any other unique name is OK.
544 * The infrastructure has already checked that it's unique,
545 * so just allocate it and hook it in.
547 MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_NOWAIT);
551 bzero(sp, sizeof(*sp));
560 * Get a netgraph control message.
561 * Check it is one we understand. If needed, send a response.
562 * We sometimes save the address for an async action later.
563 * Always free the message.
566 ng_pppoe_rcvmsg(node_p node,
567 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
569 priv_p privp = node->private;
570 struct ngpppoe_init_data *ourmsg = NULL;
571 struct ng_mesg *resp = NULL;
578 /* Deal with message according to cookie and command */
579 switch (msg->header.typecookie) {
580 case NGM_PPPOE_COOKIE:
581 switch (msg->header.cmd) {
582 case NGM_PPPOE_CONNECT:
583 case NGM_PPPOE_LISTEN:
584 case NGM_PPPOE_OFFER:
585 case NGM_PPPOE_SERVICE:
586 ourmsg = (struct ngpppoe_init_data *)msg->data;
587 if (( sizeof(*ourmsg) > msg->header.arglen)
588 || ((sizeof(*ourmsg) + ourmsg->data_len)
589 > msg->header.arglen)) {
590 printf("pppoe_rcvmsg: bad arg size");
593 if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) {
594 printf("pppoe: init data too long (%d)\n",
598 /* make sure strcmp will terminate safely */
599 ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
601 /* cycle through all known hooks */
602 LIST_FOREACH(hook, &node->hooks, hooks) {
604 && strcmp(hook->name, ourmsg->hook) == 0)
610 if ((hook->private == &privp->debug_hook)
611 || (hook->private == &privp->ethernet_hook)) {
616 if (msg->header.cmd == NGM_PPPOE_LISTEN) {
618 * Ensure we aren't already listening for this
621 if (pppoe_match_svc(node, ourmsg->data,
622 ourmsg->data_len, NG_MATCH_EXACT) != NULL) {
628 * PPPOE_SERVICE advertisments are set up
629 * on sessions that are in PRIMED state.
631 if (msg->header.cmd == NGM_PPPOE_SERVICE) {
634 if (sp->state |= PPPOE_SNONE) {
635 printf("pppoe: Session already active\n");
640 * set up prototype header
642 MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_NOWAIT);
645 printf("pppoe: Session out of memory\n");
648 bzero(neg, sizeof(*neg));
649 MGETHDR(neg->m, M_DONTWAIT, MT_DATA);
651 printf("pppoe: Session out of mbufs\n");
652 FREE(neg, M_NETGRAPH);
655 neg->m->m_pkthdr.rcvif = NULL;
656 MCLGET(neg->m, M_DONTWAIT);
657 if ((neg->m->m_flags & M_EXT) == 0) {
658 printf("pppoe: Session out of mcls\n");
660 FREE(neg, M_NETGRAPH);
664 callout_handle_init( &neg->timeout_handle);
665 neg->m->m_len = sizeof(struct pppoe_full_hdr);
666 neg->pkt = mtod(neg->m, union packet*);
667 neg->pkt->pkt_header.eh = eh_prototype;
668 neg->pkt->pkt_header.ph.ver = 0x1;
669 neg->pkt->pkt_header.ph.type = 0x1;
670 neg->pkt->pkt_header.ph.sid = 0x0000;
673 strncpy(sp->creator, retaddr, NG_NODELEN);
674 sp->creator[NG_NODELEN] = '\0';
676 switch (msg->header.cmd) {
677 case NGM_PPPOE_GET_STATUS:
679 struct ngpppoestat *stats;
681 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
685 stats = (struct ngpppoestat *) resp->data;
686 stats->packets_in = privp->packets_in;
687 stats->packets_out = privp->packets_out;
690 case NGM_PPPOE_CONNECT:
692 * Check the hook exists and is Uninitialised.
693 * Send a PADI request, and start the timeout logic.
694 * Store the originator of this message so we can send
695 * a success of fail message to them later.
696 * Move the session to SINIT
697 * Set up the session to the correct state and
700 neg->service.hdr.tag_type = PTT_SRV_NAME;
701 neg->service.hdr.tag_len =
702 htons((u_int16_t)ourmsg->data_len);
703 if (ourmsg->data_len) {
705 neg->service.data, ourmsg->data_len);
707 neg->service_len = ourmsg->data_len;
710 case NGM_PPPOE_LISTEN:
712 * Check the hook exists and is Uninitialised.
713 * Install the service matching string.
714 * Store the originator of this message so we can send
715 * a success of fail message to them later.
716 * Move the hook to 'LISTENING'
718 neg->service.hdr.tag_type = PTT_SRV_NAME;
719 neg->service.hdr.tag_len =
720 htons((u_int16_t)ourmsg->data_len);
722 if (ourmsg->data_len) {
724 neg->service.data, ourmsg->data_len);
726 neg->service_len = ourmsg->data_len;
727 neg->pkt->pkt_header.ph.code = PADT_CODE;
729 * wait for PADI packet coming from ethernet
731 sp->state = PPPOE_LISTENING;
733 case NGM_PPPOE_OFFER:
735 * Check the hook exists and is Uninitialised.
736 * Store the originator of this message so we can send
737 * a success of fail message to them later.
738 * Store the AC-Name given and go to PRIMED.
740 neg->ac_name.hdr.tag_type = PTT_AC_NAME;
741 neg->ac_name.hdr.tag_len =
742 htons((u_int16_t)ourmsg->data_len);
743 if (ourmsg->data_len) {
745 neg->ac_name.data, ourmsg->data_len);
747 neg->ac_name_len = ourmsg->data_len;
748 neg->pkt->pkt_header.ph.code = PADO_CODE;
750 * Wait for PADI packet coming from hook
752 sp->state = PPPOE_PRIMED;
754 case NGM_PPPOE_SERVICE:
756 * Check the session is primed.
757 * for now just allow ONE service to be advertised.
758 * If you do it twice you just overwrite.
760 if (sp->state != PPPOE_PRIMED) {
761 printf("pppoe: Session not primed\n");
765 neg->service.hdr.tag_type = PTT_SRV_NAME;
766 neg->service.hdr.tag_len =
767 htons((u_int16_t)ourmsg->data_len);
769 if (ourmsg->data_len)
770 bcopy(ourmsg->data, neg->service.data,
772 neg->service_len = ourmsg->data_len;
782 /* Take care of synchronous response, if any */
786 FREE(resp, M_NETGRAPH);
788 /* Free the message and return */
790 FREE(msg, M_NETGRAPH);
795 * Start a client into the first state. A separate function because
796 * it can be needed if the negotiation times out.
799 pppoe_start(sessp sp)
802 struct pppoe_tag hdr;
804 } __attribute ((packed)) uniqtag;
807 * kick the state machine into starting up
810 sp->state = PPPOE_SINIT;
811 /* reset the packet header to broadcast */
812 sp->neg->pkt->pkt_header.eh = eh_prototype;
813 sp->neg->pkt->pkt_header.ph.code = PADI_CODE;
814 uniqtag.hdr.tag_type = PTT_HOST_UNIQ;
815 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data));
816 uniqtag.data.pointer = sp;
818 insert_tag(sp, &uniqtag.hdr);
819 insert_tag(sp, &sp->neg->service.hdr);
825 send_acname(sessp sp, const struct pppoe_tag *tag)
829 struct ngpppoe_sts *sts;
831 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
832 sizeof(struct ngpppoe_sts), M_NOWAIT);
836 sts = (struct ngpppoe_sts *)msg->data;
837 tlen = min(NG_HOOKLEN, ntohs(tag->tag_len));
838 strncpy(sts->hook, tag->tag_data, tlen);
839 sts->hook[tlen] = '\0';
840 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
846 send_sessionid(sessp sp)
851 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
852 sizeof(u_int16_t), M_NOWAIT);
856 *(u_int16_t *)msg->data = sp->Session_ID;
857 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);
863 * Receive data, and do something with it.
864 * The caller will never free m or meta, so
865 * if we use up this data or abort we must free BOTH of these.
868 ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
870 node_p node = hook->node;
871 const priv_p privp = node->private;
872 sessp sp = hook->private;
873 const struct pppoe_full_hdr *wh;
874 const struct pppoe_hdr *ph;
879 const struct pppoe_tag *utag = NULL, *tag = NULL;
882 struct pppoe_tag hdr;
884 } __attribute ((packed)) uniqtag;
888 if (hook->private == &privp->debug_hook) {
890 * Data from the debug hook gets sent without modification
891 * straight to the ethernet.
893 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
894 privp->packets_out++;
895 } else if (hook->private == &privp->ethernet_hook) {
898 * Dig out various fields from the packet.
899 * use them to decide where to send it.
903 if( m->m_len < sizeof(*wh)) {
904 m = m_pullup(m, sizeof(*wh)); /* Checks length */
906 printf("couldn't m_pullup\n");
910 wh = mtod(m, struct pppoe_full_hdr *);
912 session = ntohs(wh->ph.sid);
913 length = ntohs(wh->ph.length);
915 switch(wh->eh.ether_type) {
916 case ETHERTYPE_PPPOE_STUPID_DISC:
918 eh_prototype.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
920 case ETHERTYPE_PPPOE_DISC:
922 * We need to try to make sure that the tag area
923 * is contiguous, or we could wander off the end
924 * of a buffer and make a mess.
925 * (Linux wouldn't have this problem).
927 /*XXX fix this mess */
929 if (m->m_pkthdr.len <= MHLEN) {
930 if( m->m_len < m->m_pkthdr.len) {
931 m = m_pullup(m, m->m_pkthdr.len);
933 printf("couldn't m_pullup\n");
938 if (m->m_len != m->m_pkthdr.len) {
940 * It's not all in one piece.
941 * We need to do extra work.
943 printf("packet fragmented\n");
951 * Look for a hook with the required service
952 * and send the ENTIRE packet up there.
953 * It should come back to a new hook in
954 * PRIMED state. Look there for further
957 tag = get_tag(ph, PTT_SRV_NAME);
959 printf("no service tag\n");
962 sendhook = pppoe_match_svc(hook->node,
963 tag->tag_data, ntohs(tag->tag_len),
966 NG_SEND_DATA(error, sendhook, m, meta);
974 * Use the host_uniq tag to find the
975 * hook this is in response to.
976 * Received #2, now send #3
977 * For now simply accept the first we receive.
979 utag = get_tag(ph, PTT_HOST_UNIQ);
981 || (ntohs(utag->tag_len) != sizeof(sp))) {
982 printf("no host unique field\n");
986 sendhook = pppoe_finduniq(node, utag);
987 if (sendhook == NULL) {
988 printf("no matching session\n");
993 * Check the session is in the right state.
994 * It needs to be in PPPOE_SINIT.
996 sp = sendhook->private;
997 if (sp->state != PPPOE_SINIT) {
998 printf("session in wrong state\n");
1002 untimeout(pppoe_ticker, sendhook,
1003 neg->timeout_handle);
1006 * This is the first time we hear
1007 * from the server, so note it's
1008 * unicast address, replacing the
1009 * broadcast address .
1011 bcopy(wh->eh.ether_shost,
1012 neg->pkt->pkt_header.eh.ether_dhost,
1015 neg->pkt->pkt_header.ph.code = PADR_CODE;
1017 insert_tag(sp, utag); /* Host Unique */
1018 if ((tag = get_tag(ph, PTT_AC_COOKIE)))
1019 insert_tag(sp, tag); /* return cookie */
1020 if ((tag = get_tag(ph, PTT_AC_NAME))) {
1021 insert_tag(sp, tag); /* return it */
1022 send_acname(sp, tag);
1024 insert_tag(sp, &neg->service.hdr); /* Service */
1027 sp->state = PPPOE_SREQ;
1034 * Use the ac_cookie tag to find the
1035 * hook this is in response to.
1037 utag = get_tag(ph, PTT_AC_COOKIE);
1039 || (ntohs(utag->tag_len) != sizeof(sp))) {
1043 sendhook = pppoe_finduniq(node, utag);
1044 if (sendhook == NULL) {
1049 * Check the session is in the right state.
1050 * It needs to be in PPPOE_SOFFER
1051 * or PPPOE_NEWCONNECTED. If the latter,
1052 * then this is a retry by the client.
1053 * so be nice, and resend.
1055 sp = sendhook->private;
1056 if (sp->state == PPPOE_NEWCONNECTED) {
1058 * Whoa! drop back to resend that
1060 * We should still have a copy of it.
1062 sp->state = PPPOE_SOFFER;
1064 if (sp->state != PPPOE_SOFFER) {
1065 LEAVE (ENETUNREACH);
1069 untimeout(pppoe_ticker, sendhook,
1070 neg->timeout_handle);
1071 neg->pkt->pkt_header.ph.code = PADS_CODE;
1072 if (sp->Session_ID == 0)
1073 neg->pkt->pkt_header.ph.sid =
1074 htons(sp->Session_ID
1075 = get_new_sid(node));
1079 * start working out the tags to respond with.
1082 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1083 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1084 insert_tag(sp, tag);/* return service */
1085 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1086 insert_tag(sp, tag); /* return it */
1087 insert_tag(sp, utag); /* ac_cookie */
1090 sp->state = PPPOE_NEWCONNECTED;
1093 * Having sent the last Negotiation header,
1094 * Set up the stored packet header to
1095 * be correct for the actual session.
1096 * But keep the negotialtion stuff
1097 * around in case we need to resend this last
1098 * packet. We'll discard it when we move
1099 * from NEWCONNECTED to CONNECTED
1101 sp->pkt_hdr = neg->pkt->pkt_header;
1103 sp->pkt_hdr.eh.ether_type
1104 = ETHERTYPE_PPPOE_STUPID_SESS;
1106 sp->pkt_hdr.eh.ether_type
1107 = ETHERTYPE_PPPOE_SESS;
1108 sp->pkt_hdr.ph.code = 0;
1109 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1114 * Use the host_uniq tag to find the
1115 * hook this is in response to.
1116 * take the session ID and store it away.
1117 * Also make sure the pre-made header is
1118 * correct and set us into Session mode.
1120 utag = get_tag(ph, PTT_HOST_UNIQ);
1122 || (ntohs(utag->tag_len) != sizeof(sp))) {
1123 LEAVE (ENETUNREACH);
1126 sendhook = pppoe_finduniq(node, utag);
1127 if (sendhook == NULL) {
1132 * Check the session is in the right state.
1133 * It needs to be in PPPOE_SREQ.
1135 sp = sendhook->private;
1136 if (sp->state != PPPOE_SREQ) {
1140 untimeout(pppoe_ticker, sendhook,
1141 neg->timeout_handle);
1142 neg->pkt->pkt_header.ph.sid = wh->ph.sid;
1143 sp->Session_ID = ntohs(wh->ph.sid);
1146 sp->state = PPPOE_CONNECTED;
1148 * Now we have gone to Connected mode,
1149 * Free all resources needed for
1151 * Keep a copy of the header we will be using.
1153 sp->pkt_hdr = neg->pkt->pkt_header;
1155 sp->pkt_hdr.eh.ether_type
1156 = ETHERTYPE_PPPOE_STUPID_SESS;
1158 sp->pkt_hdr.eh.ether_type
1159 = ETHERTYPE_PPPOE_SESS;
1160 sp->pkt_hdr.ph.code = 0;
1162 FREE(sp->neg, M_NETGRAPH);
1164 pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
1168 * Send a 'close' message to the controlling
1169 * process (the one that set us up);
1170 * And then tear everything down.
1172 * Find matching peer/session combination.
1174 sendhook = pppoe_findsession(node, wh);
1175 NG_FREE_DATA(m, meta); /* no longer needed */
1176 if (sendhook == NULL) {
1179 /* send message to creator */
1182 ng_destroy_hook(sendhook);
1186 LEAVE(EPFNOSUPPORT);
1189 case ETHERTYPE_PPPOE_STUPID_SESS:
1190 case ETHERTYPE_PPPOE_SESS:
1192 * find matching peer/session combination.
1194 sendhook = pppoe_findsession(node, wh);
1195 if (sendhook == NULL) {
1196 LEAVE (ENETUNREACH);
1199 sp = sendhook->private;
1200 m_adj(m, sizeof(*wh));
1201 if (m->m_pkthdr.len < length) {
1202 /* Packet too short, dump it */
1206 /* Also need to trim excess at the end */
1207 if (m->m_pkthdr.len > length) {
1208 m_adj(m, -((int)(m->m_pkthdr.len - length)));
1210 if ( sp->state != PPPOE_CONNECTED) {
1211 if (sp->state == PPPOE_NEWCONNECTED) {
1212 sp->state = PPPOE_CONNECTED;
1214 * Now we have gone to Connected mode,
1215 * Free all resources needed for
1216 * negotiation. Be paranoid about
1217 * whether there may be a timeout.
1219 m_freem(sp->neg->m);
1220 untimeout(pppoe_ticker, sendhook,
1221 sp->neg->timeout_handle);
1222 FREE(sp->neg, M_NETGRAPH);
1225 LEAVE (ENETUNREACH);
1229 NG_SEND_DATA( error, sendhook, m, meta);
1232 LEAVE(EPFNOSUPPORT);
1236 * Not ethernet or debug hook..
1238 * The packet has come in on a normal hook.
1239 * We need to find out what kind of hook,
1240 * So we can decide how to handle it.
1241 * Check the hook's state.
1244 switch (sp->state) {
1245 case PPPOE_NEWCONNECTED:
1246 case PPPOE_CONNECTED: {
1247 static const u_char addrctrl[] = { 0xff, 0x03 };
1248 struct pppoe_full_hdr *wh;
1251 * Remove PPP address and control fields, if any.
1252 * For example, ng_ppp(4) always sends LCP packets
1253 * with address and control fields as required by
1254 * generic PPP. PPPoE is an exception to the rule.
1256 if (m->m_pkthdr.len >= 2) {
1257 if (m->m_len < 2 && !(m = m_pullup(m, 2)))
1259 if (bcmp(mtod(m, u_char *), addrctrl, 2) == 0)
1263 * Bang in a pre-made header, and set the length up
1264 * to be correct. Then send it to the ethernet driver.
1265 * But first correct the length.
1267 sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len));
1268 M_PREPEND(m, sizeof(*wh), M_DONTWAIT);
1272 wh = mtod(m, struct pppoe_full_hdr *);
1273 bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
1274 NG_SEND_DATA( error, privp->ethernet_hook, m, meta);
1275 privp->packets_out++;
1280 * A PADI packet is being returned by the application
1281 * that has set up this hook. This indicates that it
1282 * wants us to offer service.
1285 if (m->m_len < sizeof(*wh)) {
1286 m = m_pullup(m, sizeof(*wh));
1291 wh = mtod(m, struct pppoe_full_hdr *);
1293 session = ntohs(wh->ph.sid);
1294 length = ntohs(wh->ph.length);
1296 if ( code != PADI_CODE) {
1299 untimeout(pppoe_ticker, hook,
1300 neg->timeout_handle);
1303 * This is the first time we hear
1304 * from the client, so note it's
1305 * unicast address, replacing the
1306 * broadcast address.
1308 bcopy(wh->eh.ether_shost,
1309 neg->pkt->pkt_header.eh.ether_dhost,
1311 sp->state = PPPOE_SOFFER;
1313 neg->pkt->pkt_header.ph.code = PADO_CODE;
1316 * start working out the tags to respond with.
1318 uniqtag.hdr.tag_type = PTT_AC_COOKIE;
1319 uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
1320 uniqtag.data.pointer = sp;
1322 insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
1323 if ((tag = get_tag(ph, PTT_SRV_NAME)))
1324 insert_tag(sp, tag); /* return service */
1326 * If we have a NULL service request
1327 * and have an extra service defined in this hook,
1328 * then also add a tag for the extra service.
1329 * XXX this is a hack. eventually we should be able
1330 * to support advertising many services, not just one
1332 if (((tag == NULL) || (tag->tag_len == 0))
1333 && (neg->service.hdr.tag_len != 0)) {
1334 insert_tag(sp, &neg->service.hdr); /* SERVICE */
1336 if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
1337 insert_tag(sp, tag); /* returned hostunique */
1338 insert_tag(sp, &uniqtag.hdr);
1345 * Packets coming from the hook make no sense
1346 * to sessions in these states. Throw them away.
1352 case PPPOE_LISTENING:
1359 NG_FREE_DATA(m, meta);
1364 * Do local shutdown processing..
1365 * If we are a persistant device, we might refuse to go away, and
1366 * we'd only remove our links and reset ourself.
1369 ng_pppoe_rmnode(node_p node)
1371 const priv_p privdata = node->private;
1374 node->flags |= NG_INVALID;
1377 node->private = NULL;
1378 ng_unref(privdata->node);
1379 FREE(privdata, M_NETGRAPH);
1384 * This is called once we've already connected a new hook to the other node.
1385 * It gives us a chance to balk at the last minute.
1388 ng_pppoe_connect(hook_p hook)
1390 /* be really amiable and just say "YUP that's OK by me! " */
1395 * Hook disconnection
1397 * Clean up all dangling links and information about the session/hook.
1398 * For this type, removal of the last link destroys the node
1401 ng_pppoe_disconnect(hook_p hook)
1403 node_p node = hook->node;
1404 priv_p privp = node->private;
1409 hooks = node->numhooks; /* this one already not counted */
1410 if (hook->private == &privp->debug_hook) {
1411 privp->debug_hook = NULL;
1412 } else if (hook->private == &privp->ethernet_hook) {
1413 privp->ethernet_hook = NULL;
1417 if (sp->state != PPPOE_SNONE ) {
1418 pppoe_send_event(sp, NGM_PPPOE_CLOSE);
1421 * According to the spec, if we are connected,
1422 * we should send a DISC packet if we are shutting down
1425 if ((privp->ethernet_hook)
1426 && ((sp->state == PPPOE_CONNECTED)
1427 || (sp->state == PPPOE_NEWCONNECTED))) {
1429 struct pppoe_full_hdr *wh;
1430 struct pppoe_tag *tag;
1431 int msglen = strlen(SIGNOFF);
1435 /* revert the stored header to DISC/PADT mode */
1437 wh->ph.code = PADT_CODE;
1439 wh->eh.ether_type = ETHERTYPE_PPPOE_STUPID_DISC;
1441 wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
1443 /* generate a packet of that type */
1444 MGETHDR(m, M_DONTWAIT, MT_DATA);
1446 printf("pppoe: Session out of mbufs\n");
1448 m->m_pkthdr.rcvif = NULL;
1449 m->m_pkthdr.len = m->m_len = sizeof(*wh);
1450 bcopy((caddr_t)wh, mtod(m, caddr_t),
1453 * Add a General error message and adjust
1456 wh = mtod(m, struct pppoe_full_hdr *);
1458 tag->tag_type = PTT_GEN_ERR;
1459 tag->tag_len = htons((u_int16_t)msglen);
1460 strncpy(tag->tag_data, SIGNOFF, msglen);
1461 m->m_pkthdr.len = (m->m_len += sizeof(*tag) +
1463 wh->ph.length = htons(sizeof(*tag) + msglen);
1464 NG_SEND_DATA(error, privp->ethernet_hook, m,
1469 * As long as we have somewhere to store the timeout handle,
1470 * we may have a timeout pending.. get rid of it.
1473 untimeout(pppoe_ticker, hook, sp->neg->timeout_handle);
1475 m_freem(sp->neg->m);
1476 FREE(sp->neg, M_NETGRAPH);
1478 FREE(sp, M_NETGRAPH);
1479 hook->private = NULL;
1480 /* work out how many session hooks there are */
1481 /* Node goes away on last session hook removal */
1482 if (privp->ethernet_hook) hooks -= 1;
1483 if (privp->debug_hook) hooks -= 1;
1485 if (node->numhooks == 0)
1491 * timeouts come here.
1494 pppoe_ticker(void *arg)
1498 sessp sp = hook->private;
1501 struct mbuf *m0 = NULL;
1502 priv_p privp = hook->node->private;
1503 meta_p dummy = NULL;
1508 * resend the last packet, using an exponential backoff.
1509 * After a period of time, stop growing the backoff,
1510 * and either leave it, or revert to the start.
1514 /* timeouts on these produce resends */
1515 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1516 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1517 neg->timeout_handle = timeout(pppoe_ticker,
1518 hook, neg->timeout * hz);
1519 if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
1520 if (sp->state == PPPOE_SREQ) {
1521 /* revert to SINIT mode */
1524 neg->timeout = PPPOE_TIMEOUT_LIMIT;
1530 /* a timeout on these says "give up" */
1531 ng_destroy_hook(hook);
1534 /* timeouts have no meaning in other states */
1535 printf("pppoe: unexpected timeout\n");
1542 sendpacket(sessp sp)
1545 struct mbuf *m0 = NULL;
1546 hook_p hook = sp->hook;
1548 priv_p privp = hook->node->private;
1549 meta_p dummy = NULL;
1553 case PPPOE_LISTENING:
1556 case PPPOE_CONNECTED:
1557 printf("pppoe: sendpacket: unexpected state\n");
1560 case PPPOE_NEWCONNECTED:
1561 /* send the PADS without a timeout - we're now connected */
1562 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1563 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1567 /* No packet to send, but set up the timeout */
1568 neg->timeout_handle = timeout(pppoe_ticker,
1569 hook, PPPOE_OFFER_TIMEOUT * hz);
1574 * send the offer but if they don't respond
1575 * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
1577 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1578 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1579 neg->timeout_handle = timeout(pppoe_ticker,
1580 hook, PPPOE_OFFER_TIMEOUT * hz);
1585 m0 = m_copypacket(sp->neg->m, M_DONTWAIT);
1586 NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy);
1587 neg->timeout_handle = timeout(pppoe_ticker, hook,
1588 (hz * PPPOE_INITIAL_TIMEOUT));
1589 neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1594 printf("pppoe: timeout: bad state\n");
1596 /* return (error); */
1600 * Parse an incoming packet to see if any tags should be copied to the
1601 * output packet. Don't do any tags that have been handled in the main
1604 static const struct pppoe_tag*
1605 scan_tags(sessp sp, const struct pppoe_hdr* ph)
1607 const char *const end = (const char *)next_tag(ph);
1609 const struct pppoe_tag *pt = &ph->tag[0];
1611 * Keep processing tags while a tag header will still fit.
1614 while((const char*)(pt + 1) <= end) {
1616 * If the tag data would go past the end of the packet, abort.
1618 ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
1622 switch (pt->tag_type) {
1638 pt = (const struct pppoe_tag*)ptn;
1644 pppoe_send_event(sessp sp, enum cmd cmdid)
1647 struct ng_mesg *msg;
1648 struct ngpppoe_sts *sts;
1651 NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
1652 sizeof(struct ngpppoe_sts), M_NOWAIT);
1655 sts = (struct ngpppoe_sts *)msg->data;
1656 strncpy(sts->hook, sp->hook->name, NG_HOOKLEN + 1);
1657 error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL);