19fcb3d83SJulian Elischer #define SIGNOFF "session closed" 24cf49a43SJulian Elischer /* 34cf49a43SJulian Elischer * ng_pppoe.c 44cf49a43SJulian Elischer * 54cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc. 64cf49a43SJulian Elischer * All rights reserved. 74cf49a43SJulian Elischer * 84cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 94cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 104cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 114cf49a43SJulian Elischer * provided, however, that: 124cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 134cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 144cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 154cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 164cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 174cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 184cf49a43SJulian Elischer * 194cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 204cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 214cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 224cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 234cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 244cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 254cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 264cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 274cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 284cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 294cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 304cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 314cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 324cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 334cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 344cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 354cf49a43SJulian Elischer * OF SUCH DAMAGE. 364cf49a43SJulian Elischer * 374cf49a43SJulian Elischer * Author: Julian Elischer <julian@whistle.com> 384cf49a43SJulian Elischer * 394cf49a43SJulian Elischer * $FreeBSD$ 4074f5c6aaSJulian Elischer * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $ 414cf49a43SJulian Elischer */ 421e2510f8SJulian Elischer #if 0 431e2510f8SJulian Elischer #define AAA printf("pppoe: %s\n", __FUNCTION__ ); 440c65c135SJulian Elischer #define BBB printf("-%d-", __LINE__ ); 451e2510f8SJulian Elischer #else 461e2510f8SJulian Elischer #define AAA 470c65c135SJulian Elischer #define BBB 481e2510f8SJulian Elischer #endif 494cf49a43SJulian Elischer 504cf49a43SJulian Elischer #include <sys/param.h> 514cf49a43SJulian Elischer #include <sys/systm.h> 524cf49a43SJulian Elischer #include <sys/kernel.h> 534cf49a43SJulian Elischer #include <sys/mbuf.h> 544cf49a43SJulian Elischer #include <sys/malloc.h> 554cf49a43SJulian Elischer #include <sys/errno.h> 564cf49a43SJulian Elischer #include <sys/syslog.h> 574cf49a43SJulian Elischer #include <net/ethernet.h> 584cf49a43SJulian Elischer 594cf49a43SJulian Elischer #include <netgraph/ng_message.h> 604cf49a43SJulian Elischer #include <netgraph/netgraph.h> 614cf49a43SJulian Elischer #include <netgraph/ng_pppoe.h> 624cf49a43SJulian Elischer 634cf49a43SJulian Elischer /* 644cf49a43SJulian Elischer * This section contains the netgraph method declarations for the 654cf49a43SJulian Elischer * sample node. These methods define the netgraph 'type'. 664cf49a43SJulian Elischer */ 674cf49a43SJulian Elischer 6874f5c6aaSJulian Elischer static ng_constructor_t ng_pppoe_constructor; 6974f5c6aaSJulian Elischer static ng_rcvmsg_t ng_pppoe_rcvmsg; 7074f5c6aaSJulian Elischer static ng_shutdown_t ng_pppoe_rmnode; 7174f5c6aaSJulian Elischer static ng_newhook_t ng_pppoe_newhook; 7274f5c6aaSJulian Elischer static ng_connect_t ng_pppoe_connect; 7374f5c6aaSJulian Elischer static ng_rcvdata_t ng_pppoe_rcvdata; 7474f5c6aaSJulian Elischer static ng_disconnect_t ng_pppoe_disconnect; 754cf49a43SJulian Elischer 764cf49a43SJulian Elischer /* Netgraph node type descriptor */ 774cf49a43SJulian Elischer static struct ng_type typestruct = { 784cf49a43SJulian Elischer NG_VERSION, 794cf49a43SJulian Elischer NG_PPPOE_NODE_TYPE, 804cf49a43SJulian Elischer NULL, 818876b55dSJulian Elischer ng_pppoe_constructor, 828876b55dSJulian Elischer ng_pppoe_rcvmsg, 838876b55dSJulian Elischer ng_pppoe_rmnode, 848876b55dSJulian Elischer ng_pppoe_newhook, 854cf49a43SJulian Elischer NULL, 868876b55dSJulian Elischer ng_pppoe_connect, 878876b55dSJulian Elischer ng_pppoe_rcvdata, 888876b55dSJulian Elischer ng_pppoe_rcvdata, 898876b55dSJulian Elischer ng_pppoe_disconnect 904cf49a43SJulian Elischer }; 918876b55dSJulian Elischer NETGRAPH_INIT(pppoe, &typestruct); 924cf49a43SJulian Elischer 934cf49a43SJulian Elischer /* 944cf49a43SJulian Elischer * States for the session state machine. 954cf49a43SJulian Elischer * These have no meaning if there is no hook attached yet. 964cf49a43SJulian Elischer */ 974cf49a43SJulian Elischer enum state { 984cf49a43SJulian Elischer PPPOE_SNONE=0, /* [both] Initial state */ 994cf49a43SJulian Elischer PPPOE_SINIT, /* [Client] Sent discovery initiation */ 1004cf49a43SJulian Elischer PPPOE_PRIMED, /* [Server] Sent offer message */ 1014cf49a43SJulian Elischer PPPOE_SOFFER, /* [Server] Sent offer message */ 1024cf49a43SJulian Elischer PPPOE_SREQ, /* [Client] Sent a Request */ 1034cf49a43SJulian Elischer PPPOE_LISTENING, /* [Server] Listening for discover initiation msg */ 1044cf49a43SJulian Elischer PPPOE_NEWCONNECTED, /* [Both] Connection established, No data received */ 1054cf49a43SJulian Elischer PPPOE_CONNECTED, /* [Both] Connection established, Data received */ 1064cf49a43SJulian Elischer PPPOE_DEAD /* [Both] */ 1074cf49a43SJulian Elischer }; 1084cf49a43SJulian Elischer 1094cf49a43SJulian Elischer #define NUMTAGS 20 /* number of tags we are set up to work with */ 1104cf49a43SJulian Elischer 1114cf49a43SJulian Elischer /* 1124cf49a43SJulian Elischer * Information we store for each hook on each node for negotiating the 1134cf49a43SJulian Elischer * session. The mbuf and cluster are freed once negotiation has completed. 1144cf49a43SJulian Elischer * The whole negotiation block is then discarded. 1154cf49a43SJulian Elischer */ 1164cf49a43SJulian Elischer 1174cf49a43SJulian Elischer struct sess_neg { 1184cf49a43SJulian Elischer struct mbuf *m; /* holds cluster with last sent packet */ 1194cf49a43SJulian Elischer union packet *pkt; /* points within the above cluster */ 1204cf49a43SJulian Elischer struct callout_handle timeout_handle; /* see timeout(9) */ 1214cf49a43SJulian Elischer u_int timeout; /* 0,1,2,4,8,16 etc. seconds */ 1224cf49a43SJulian Elischer u_int numtags; 1234cf49a43SJulian Elischer struct pppoe_tag *tags[NUMTAGS]; 1244cf49a43SJulian Elischer u_int service_len; 1254cf49a43SJulian Elischer u_int ac_name_len; 1264cf49a43SJulian Elischer 1274cf49a43SJulian Elischer struct datatag service; 1284cf49a43SJulian Elischer struct datatag ac_name; 1294cf49a43SJulian Elischer }; 1304cf49a43SJulian Elischer typedef struct sess_neg *negp; 1314cf49a43SJulian Elischer 1324cf49a43SJulian Elischer /* 1334cf49a43SJulian Elischer * Session information that is needed after connection. 1344cf49a43SJulian Elischer */ 1354cf49a43SJulian Elischer struct session { 1364cf49a43SJulian Elischer hook_p hook; 1374cf49a43SJulian Elischer u_int16_t Session_ID; 1384cf49a43SJulian Elischer struct session *hash_next; /* not yet uesed */ 1394cf49a43SJulian Elischer enum state state; 1404cf49a43SJulian Elischer char creator[NG_NODELEN + 1]; /* who to notify */ 1414cf49a43SJulian Elischer struct pppoe_full_hdr pkt_hdr; /* used when connected */ 1424cf49a43SJulian Elischer negp neg; /* used when negotiating */ 1434cf49a43SJulian Elischer }; 1444cf49a43SJulian Elischer typedef struct session *sessp; 1454cf49a43SJulian Elischer 1464cf49a43SJulian Elischer /* 1474cf49a43SJulian Elischer * Information we store for each node 1484cf49a43SJulian Elischer */ 1494cf49a43SJulian Elischer struct PPPOE { 1504cf49a43SJulian Elischer node_p node; /* back pointer to node */ 1514cf49a43SJulian Elischer hook_p ethernet_hook; 1524cf49a43SJulian Elischer hook_p debug_hook; 1534cf49a43SJulian Elischer u_int packets_in; /* packets in from ethernet */ 1544cf49a43SJulian Elischer u_int packets_out; /* packets out towards ethernet */ 1554cf49a43SJulian Elischer u_int32_t flags; 1564cf49a43SJulian Elischer /*struct session *buckets[HASH_SIZE];*/ /* not yet used */ 1574cf49a43SJulian Elischer }; 1584cf49a43SJulian Elischer typedef struct PPPOE *priv_p; 1594cf49a43SJulian Elischer 1604cf49a43SJulian Elischer const struct ether_header eh_prototype = 1614cf49a43SJulian Elischer {{0xff,0xff,0xff,0xff,0xff,0xff}, 1624cf49a43SJulian Elischer {0x00,0x00,0x00,0x00,0x00,0x00}, 1634cf49a43SJulian Elischer ETHERTYPE_PPPOE_DISC}; 1644cf49a43SJulian Elischer 1654cf49a43SJulian Elischer union uniq { 1664cf49a43SJulian Elischer char bytes[sizeof(void *)]; 1674cf49a43SJulian Elischer void * pointer; 1684cf49a43SJulian Elischer }; 1694cf49a43SJulian Elischer 1704cf49a43SJulian Elischer #define LEAVE(x) do { error = x; goto quit; } while(0) 1714cf49a43SJulian Elischer static void pppoe_start(sessp sp); 1724cf49a43SJulian Elischer static void sendpacket(sessp sp); 1734cf49a43SJulian Elischer static void pppoe_ticker(void *arg); 1744cf49a43SJulian Elischer static struct pppoe_tag* scan_tags(sessp sp, struct pppoe_hdr* ph); 175b58a8a3bSJulian Elischer static int pppoe_send_event(sessp sp, enum cmd cmdid); 1764cf49a43SJulian Elischer 1774cf49a43SJulian Elischer /************************************************************************* 1784cf49a43SJulian Elischer * Some basic utilities from the Linux version with author's permission.* 1794cf49a43SJulian Elischer * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> * 1804cf49a43SJulian Elischer ************************************************************************/ 1814cf49a43SJulian Elischer 1824cf49a43SJulian Elischer /* 1834cf49a43SJulian Elischer * Generate a new session id 1844cf49a43SJulian Elischer * XXX find out the freeBSD locking scheme. 1854cf49a43SJulian Elischer */ 1864cf49a43SJulian Elischer static u_int16_t 1874cf49a43SJulian Elischer get_new_sid(node_p node) 1884cf49a43SJulian Elischer { 1894cf49a43SJulian Elischer static int pppoe_sid = 10; 1904cf49a43SJulian Elischer sessp sp; 1914cf49a43SJulian Elischer hook_p hook; 1924cf49a43SJulian Elischer u_int16_t val; 1934cf49a43SJulian Elischer priv_p privp = node->private; 1944cf49a43SJulian Elischer 1951e2510f8SJulian Elischer AAA 1964cf49a43SJulian Elischer restart: 1974cf49a43SJulian Elischer val = pppoe_sid++; 1984cf49a43SJulian Elischer /* 1994cf49a43SJulian Elischer * Spec says 0xFFFF is reserved. 2004cf49a43SJulian Elischer * Also don't use 0x0000 2014cf49a43SJulian Elischer */ 2024cf49a43SJulian Elischer if (val == 0xffff) { 2034cf49a43SJulian Elischer pppoe_sid = 20; 2044cf49a43SJulian Elischer goto restart; 2054cf49a43SJulian Elischer } 2064cf49a43SJulian Elischer 2074cf49a43SJulian Elischer /* Check it isn't already in use */ 2084cf49a43SJulian Elischer LIST_FOREACH(hook, &node->hooks, hooks) { 2094cf49a43SJulian Elischer /* don't check special hooks */ 2104cf49a43SJulian Elischer if ((hook->private == &privp->debug_hook) 2114cf49a43SJulian Elischer || (hook->private == &privp->ethernet_hook)) 2124cf49a43SJulian Elischer continue; 2134cf49a43SJulian Elischer sp = hook->private; 2144cf49a43SJulian Elischer if (sp->Session_ID == val) 2154cf49a43SJulian Elischer goto restart; 2164cf49a43SJulian Elischer } 2174cf49a43SJulian Elischer 2184cf49a43SJulian Elischer return val; 2194cf49a43SJulian Elischer } 2204cf49a43SJulian Elischer 2214cf49a43SJulian Elischer 2224cf49a43SJulian Elischer /* 2234cf49a43SJulian Elischer * Return the location where the next tag can be put 2244cf49a43SJulian Elischer */ 2254cf49a43SJulian Elischer static __inline struct pppoe_tag* 2264cf49a43SJulian Elischer next_tag(struct pppoe_hdr* ph) 2274cf49a43SJulian Elischer { 2284cf49a43SJulian Elischer return (struct pppoe_tag*)(((char*)&ph->tag[0]) + ntohs(ph->length)); 2294cf49a43SJulian Elischer } 2304cf49a43SJulian Elischer 2314cf49a43SJulian Elischer /* 2324cf49a43SJulian Elischer * Look for a tag of a specific type 2334cf49a43SJulian Elischer * Don't trust any length the other end says. 2344cf49a43SJulian Elischer * but assume we already sanity checked ph->length. 2354cf49a43SJulian Elischer */ 2364cf49a43SJulian Elischer static struct pppoe_tag* 2374cf49a43SJulian Elischer get_tag(struct pppoe_hdr* ph, u_int16_t idx) 2384cf49a43SJulian Elischer { 2394cf49a43SJulian Elischer char *end = (char *)next_tag(ph); 2404cf49a43SJulian Elischer char *ptn; 2414cf49a43SJulian Elischer struct pppoe_tag *pt = &ph->tag[0]; 2424cf49a43SJulian Elischer /* 2434cf49a43SJulian Elischer * Keep processing tags while a tag header will still fit. 2444cf49a43SJulian Elischer */ 2451e2510f8SJulian Elischer AAA 2464cf49a43SJulian Elischer while((char*)(pt + 1) <= end) { 2474cf49a43SJulian Elischer /* 2484cf49a43SJulian Elischer * If the tag data would go past the end of the packet, abort. 2494cf49a43SJulian Elischer */ 2504cf49a43SJulian Elischer ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len)); 2514cf49a43SJulian Elischer if(ptn > end) 2524cf49a43SJulian Elischer return NULL; 2534cf49a43SJulian Elischer 2544cf49a43SJulian Elischer if(pt->tag_type == idx) 2554cf49a43SJulian Elischer return pt; 2564cf49a43SJulian Elischer 2574cf49a43SJulian Elischer pt = (struct pppoe_tag*)ptn; 2584cf49a43SJulian Elischer } 2594cf49a43SJulian Elischer return NULL; 2604cf49a43SJulian Elischer } 2614cf49a43SJulian Elischer 2624cf49a43SJulian Elischer /************************************************************************** 2634cf49a43SJulian Elischer * inlines to initialise or add tags to a session's tag list, 2644cf49a43SJulian Elischer **************************************************************************/ 2654cf49a43SJulian Elischer /* 2664cf49a43SJulian Elischer * Initialise the session's tag list 2674cf49a43SJulian Elischer */ 2684cf49a43SJulian Elischer static void 2694cf49a43SJulian Elischer init_tags(sessp sp) 2704cf49a43SJulian Elischer { 2711e2510f8SJulian Elischer AAA 2724cf49a43SJulian Elischer if(sp->neg == NULL) { 2734cf49a43SJulian Elischer printf("pppoe: asked to init NULL neg pointer\n"); 2744cf49a43SJulian Elischer return; 2754cf49a43SJulian Elischer } 2764cf49a43SJulian Elischer sp->neg->numtags = 0; 2774cf49a43SJulian Elischer } 2784cf49a43SJulian Elischer 2794cf49a43SJulian Elischer static void 2804cf49a43SJulian Elischer insert_tag(sessp sp, struct pppoe_tag *tp) 2814cf49a43SJulian Elischer { 2824cf49a43SJulian Elischer int i; 2834cf49a43SJulian Elischer negp neg; 2844cf49a43SJulian Elischer 2851e2510f8SJulian Elischer AAA 2864cf49a43SJulian Elischer if((neg = sp->neg) == NULL) { 2874cf49a43SJulian Elischer printf("pppoe: asked to use NULL neg pointer\n"); 2884cf49a43SJulian Elischer return; 2894cf49a43SJulian Elischer } 2904cf49a43SJulian Elischer if ((i = neg->numtags++) < NUMTAGS) { 2914cf49a43SJulian Elischer neg->tags[i] = tp; 2924cf49a43SJulian Elischer } else { 2934cf49a43SJulian Elischer printf("pppoe: asked to add too many tags to packet\n"); 2944cf49a43SJulian Elischer } 2954cf49a43SJulian Elischer } 2964cf49a43SJulian Elischer 2974cf49a43SJulian Elischer /* 2984cf49a43SJulian Elischer * Make up a packet, using the tags filled out for the session. 2994cf49a43SJulian Elischer * 3004cf49a43SJulian Elischer * Assume that the actual pppoe header and ethernet header 3014cf49a43SJulian Elischer * are filled out externally to this routine. 3024cf49a43SJulian Elischer * Also assume that neg->wh points to the correct 3034cf49a43SJulian Elischer * location at the front of the buffer space. 3044cf49a43SJulian Elischer */ 3054cf49a43SJulian Elischer static void 3064cf49a43SJulian Elischer make_packet(sessp sp) { 3074cf49a43SJulian Elischer struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header; 3084cf49a43SJulian Elischer struct pppoe_tag **tag; 3094cf49a43SJulian Elischer char *dp; 3104cf49a43SJulian Elischer int count; 3114cf49a43SJulian Elischer int tlen; 3124cf49a43SJulian Elischer u_int16_t length = 0; 3134cf49a43SJulian Elischer 3141e2510f8SJulian Elischer AAA 3151e2510f8SJulian Elischer if ((sp->neg == NULL) || (sp->neg->m == NULL)) { 3164cf49a43SJulian Elischer printf("pppoe: make_packet called from wrong state\n"); 3174cf49a43SJulian Elischer } 3184cf49a43SJulian Elischer dp = (char *)wh->ph.tag; 3194cf49a43SJulian Elischer for (count = 0, tag = sp->neg->tags; 3204cf49a43SJulian Elischer ((count < sp->neg->numtags) && (count < NUMTAGS)); 3214cf49a43SJulian Elischer tag++, count++) { 3224cf49a43SJulian Elischer tlen = ntohs((*tag)->tag_len) + sizeof(**tag); 3234cf49a43SJulian Elischer if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) { 3244cf49a43SJulian Elischer printf("pppoe: tags too long\n"); 3254cf49a43SJulian Elischer sp->neg->numtags = count; 3264cf49a43SJulian Elischer break; /* XXX chop off what's too long */ 3274cf49a43SJulian Elischer } 3284cf49a43SJulian Elischer bcopy((char *)*tag, (char *)dp, tlen); 3294cf49a43SJulian Elischer length += tlen; 3304cf49a43SJulian Elischer dp += tlen; 3314cf49a43SJulian Elischer } 3324cf49a43SJulian Elischer wh->ph.length = htons(length); 3334cf49a43SJulian Elischer sp->neg->m->m_len = length + sizeof(*wh); 3344cf49a43SJulian Elischer sp->neg->m->m_pkthdr.len = length + sizeof(*wh); 3354cf49a43SJulian Elischer } 3364cf49a43SJulian Elischer 3374cf49a43SJulian Elischer /************************************************************************** 3384cf49a43SJulian Elischer * Routine to match a service offered * 3394cf49a43SJulian Elischer **************************************************************************/ 3404cf49a43SJulian Elischer /* 3414cf49a43SJulian Elischer * Find a hook that has a service string that matches that 3424cf49a43SJulian Elischer * we are seeking. for now use a simple string. 3434cf49a43SJulian Elischer * In the future we may need something like regexp(). 3444cf49a43SJulian Elischer * for testing allow a null string to match 1st found and a null service 3454cf49a43SJulian Elischer * to match all requests. Also make '*' do the same. 3464cf49a43SJulian Elischer */ 3474cf49a43SJulian Elischer static hook_p 3484cf49a43SJulian Elischer pppoe_match_svc(node_p node, char *svc_name, int svc_len) 3494cf49a43SJulian Elischer { 3504cf49a43SJulian Elischer sessp sp = NULL; 3514cf49a43SJulian Elischer negp neg = NULL; 3524cf49a43SJulian Elischer priv_p privp = node->private; 3534cf49a43SJulian Elischer hook_p hook; 3544cf49a43SJulian Elischer 3551e2510f8SJulian Elischer AAA 3564cf49a43SJulian Elischer LIST_FOREACH(hook, &node->hooks, hooks) { 3574cf49a43SJulian Elischer 3584cf49a43SJulian Elischer /* skip any hook that is debug or ethernet */ 3594cf49a43SJulian Elischer if ((hook->private == &privp->debug_hook) 3604cf49a43SJulian Elischer || (hook->private == &privp->ethernet_hook)) 3614cf49a43SJulian Elischer continue; 3624cf49a43SJulian Elischer sp = hook->private; 3634cf49a43SJulian Elischer 3644cf49a43SJulian Elischer /* Skip any sessions which are not in LISTEN mode. */ 3654cf49a43SJulian Elischer if ( sp->state != PPPOE_LISTENING) 3664cf49a43SJulian Elischer continue; 3674cf49a43SJulian Elischer 3684cf49a43SJulian Elischer neg = sp->neg; 3694cf49a43SJulian Elischer /* XXX check validity of this */ 3704cf49a43SJulian Elischer /* special case, NULL request. match 1st found. */ 3714cf49a43SJulian Elischer if (svc_len == 0) 3724cf49a43SJulian Elischer break; 3734cf49a43SJulian Elischer 3744cf49a43SJulian Elischer /* XXX check validity of this */ 3754cf49a43SJulian Elischer /* Special case for a blank or "*" service name (wildcard) */ 3764cf49a43SJulian Elischer if ((neg->service_len == 0) 3774cf49a43SJulian Elischer || ((neg->service_len == 1) 3784cf49a43SJulian Elischer && (neg->service.data[0] == '*'))) { 3794cf49a43SJulian Elischer break; 3804cf49a43SJulian Elischer } 3814cf49a43SJulian Elischer 3824cf49a43SJulian Elischer /* If the lengths don't match, that aint it. */ 3834cf49a43SJulian Elischer if (neg->service_len != svc_len) 3844cf49a43SJulian Elischer continue; 3854cf49a43SJulian Elischer 3864cf49a43SJulian Elischer /* An exact match? */ 3874cf49a43SJulian Elischer if (strncmp(svc_name, neg->service.data, svc_len) == 0) 3884cf49a43SJulian Elischer break; 3894cf49a43SJulian Elischer } 3904cf49a43SJulian Elischer return (hook); 3914cf49a43SJulian Elischer } 3924cf49a43SJulian Elischer /************************************************************************** 3934cf49a43SJulian Elischer * Routine to find a particular session that matches an incoming packet * 3944cf49a43SJulian Elischer **************************************************************************/ 3954cf49a43SJulian Elischer static hook_p 3964cf49a43SJulian Elischer pppoe_findsession(node_p node, struct pppoe_full_hdr *wh) 3974cf49a43SJulian Elischer { 3984cf49a43SJulian Elischer sessp sp = NULL; 3994cf49a43SJulian Elischer hook_p hook = NULL; 4004cf49a43SJulian Elischer priv_p privp = node->private; 401b86d0a9eSJulian Elischer u_int16_t session = ntohs(wh->ph.sid); 4024cf49a43SJulian Elischer 4034cf49a43SJulian Elischer /* 4044cf49a43SJulian Elischer * find matching peer/session combination. 4054cf49a43SJulian Elischer */ 4061e2510f8SJulian Elischer AAA 4074cf49a43SJulian Elischer LIST_FOREACH(hook, &node->hooks, hooks) { 4084cf49a43SJulian Elischer /* don't check special hooks */ 4094cf49a43SJulian Elischer if ((hook->private == &privp->debug_hook) 4104cf49a43SJulian Elischer || (hook->private == &privp->ethernet_hook)) { 4114cf49a43SJulian Elischer continue; 4124cf49a43SJulian Elischer } 4134cf49a43SJulian Elischer sp = hook->private; 4144cf49a43SJulian Elischer if ( ( (sp->state == PPPOE_CONNECTED) 4154cf49a43SJulian Elischer || (sp->state == PPPOE_NEWCONNECTED) ) 4164cf49a43SJulian Elischer && (sp->Session_ID == session) 4174cf49a43SJulian Elischer && (bcmp(sp->pkt_hdr.eh.ether_dhost, 4184cf49a43SJulian Elischer wh->eh.ether_shost, 4194cf49a43SJulian Elischer ETHER_ADDR_LEN)) == 0) { 4204cf49a43SJulian Elischer break; 4214cf49a43SJulian Elischer } 4224cf49a43SJulian Elischer } 4234cf49a43SJulian Elischer return (hook); 4244cf49a43SJulian Elischer } 4254cf49a43SJulian Elischer 4264cf49a43SJulian Elischer static hook_p 4274cf49a43SJulian Elischer pppoe_finduniq(node_p node, struct pppoe_tag *tag) 4284cf49a43SJulian Elischer { 4294cf49a43SJulian Elischer hook_p hook = NULL; 4304cf49a43SJulian Elischer priv_p privp = node->private; 4314cf49a43SJulian Elischer union uniq uniq; 4324cf49a43SJulian Elischer 4331e2510f8SJulian Elischer AAA 4344cf49a43SJulian Elischer bcopy(tag->tag_data, uniq.bytes, sizeof(void *)); 4354cf49a43SJulian Elischer /* cycle through all known hooks */ 4364cf49a43SJulian Elischer LIST_FOREACH(hook, &node->hooks, hooks) { 4374cf49a43SJulian Elischer /* don't check special hooks */ 4384cf49a43SJulian Elischer if ((hook->private == &privp->debug_hook) 4394cf49a43SJulian Elischer || (hook->private == &privp->ethernet_hook)) 4404cf49a43SJulian Elischer continue; 4414cf49a43SJulian Elischer if (uniq.pointer == hook->private) 4424cf49a43SJulian Elischer break; 4434cf49a43SJulian Elischer } 4444cf49a43SJulian Elischer return (hook); 4454cf49a43SJulian Elischer } 4464cf49a43SJulian Elischer 4474cf49a43SJulian Elischer /************************************************************************** 4484cf49a43SJulian Elischer * start of Netgraph entrypoints * 4494cf49a43SJulian Elischer **************************************************************************/ 4504cf49a43SJulian Elischer 4514cf49a43SJulian Elischer /* 4524cf49a43SJulian Elischer * Allocate the private data structure and the generic node 4534cf49a43SJulian Elischer * and link them together. 4544cf49a43SJulian Elischer * 4554cf49a43SJulian Elischer * ng_make_node_common() returns with a generic node struct 4564cf49a43SJulian Elischer * with a single reference for us.. we transfer it to the 4574cf49a43SJulian Elischer * private structure.. when we free the private struct we must 4584cf49a43SJulian Elischer * unref the node so it gets freed too. 4594cf49a43SJulian Elischer * 4604cf49a43SJulian Elischer * If this were a device node than this work would be done in the attach() 4614cf49a43SJulian Elischer * routine and the constructor would return EINVAL as you should not be able 4624cf49a43SJulian Elischer * to creatednodes that depend on hardware (unless you can add the hardware :) 4634cf49a43SJulian Elischer */ 4644cf49a43SJulian Elischer static int 4658876b55dSJulian Elischer ng_pppoe_constructor(node_p *nodep) 4664cf49a43SJulian Elischer { 4674cf49a43SJulian Elischer priv_p privdata; 4684cf49a43SJulian Elischer int error; 4694cf49a43SJulian Elischer 4701e2510f8SJulian Elischer AAA 4714cf49a43SJulian Elischer /* Initialize private descriptor */ 4724cf49a43SJulian Elischer MALLOC(privdata, priv_p, sizeof(*privdata), M_NETGRAPH, M_WAITOK); 4734cf49a43SJulian Elischer if (privdata == NULL) 4744cf49a43SJulian Elischer return (ENOMEM); 4754cf49a43SJulian Elischer bzero(privdata, sizeof(*privdata)); 4764cf49a43SJulian Elischer 4774cf49a43SJulian Elischer /* Call the 'generic' (ie, superclass) node constructor */ 4784cf49a43SJulian Elischer if ((error = ng_make_node_common(&typestruct, nodep))) { 4794cf49a43SJulian Elischer FREE(privdata, M_NETGRAPH); 4804cf49a43SJulian Elischer return (error); 4814cf49a43SJulian Elischer } 4824cf49a43SJulian Elischer 4834cf49a43SJulian Elischer /* Link structs together; this counts as our one reference to *nodep */ 4844cf49a43SJulian Elischer (*nodep)->private = privdata; 4854cf49a43SJulian Elischer privdata->node = *nodep; 4864cf49a43SJulian Elischer return (0); 4874cf49a43SJulian Elischer } 4884cf49a43SJulian Elischer 4894cf49a43SJulian Elischer /* 4904cf49a43SJulian Elischer * Give our ok for a hook to be added... 4914cf49a43SJulian Elischer * point the hook's private info to the hook structure. 4924cf49a43SJulian Elischer * 4934cf49a43SJulian Elischer * The following hook names are special: 4944cf49a43SJulian Elischer * Ethernet: the hook that should be connected to a NIC. 4954cf49a43SJulian Elischer * debug: copies of data sent out here (when I write the code). 4964cf49a43SJulian Elischer */ 4974cf49a43SJulian Elischer static int 4988876b55dSJulian Elischer ng_pppoe_newhook(node_p node, hook_p hook, const char *name) 4994cf49a43SJulian Elischer { 5004cf49a43SJulian Elischer const priv_p privp = node->private; 5014cf49a43SJulian Elischer sessp sp; 5024cf49a43SJulian Elischer 5031e2510f8SJulian Elischer AAA 5044cf49a43SJulian Elischer if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) { 5054cf49a43SJulian Elischer privp->ethernet_hook = hook; 5064cf49a43SJulian Elischer hook->private = &privp->ethernet_hook; 5074cf49a43SJulian Elischer } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) { 5084cf49a43SJulian Elischer privp->debug_hook = hook; 5094cf49a43SJulian Elischer hook->private = &privp->debug_hook; 5104cf49a43SJulian Elischer } else { 5114cf49a43SJulian Elischer /* 5124cf49a43SJulian Elischer * Any other unique name is OK. 5134cf49a43SJulian Elischer * The infrastructure has already checked that it's unique, 5144cf49a43SJulian Elischer * so just allocate it and hook it in. 5154cf49a43SJulian Elischer */ 5164cf49a43SJulian Elischer MALLOC(sp, sessp, sizeof(*sp), M_NETGRAPH, M_WAITOK); 5174cf49a43SJulian Elischer if (sp == NULL) { 5184cf49a43SJulian Elischer return (ENOMEM); 5194cf49a43SJulian Elischer } 5204cf49a43SJulian Elischer bzero(sp, sizeof(*sp)); 5214cf49a43SJulian Elischer 5224cf49a43SJulian Elischer hook->private = sp; 5234cf49a43SJulian Elischer sp->hook = hook; 5244cf49a43SJulian Elischer } 5254cf49a43SJulian Elischer return(0); 5264cf49a43SJulian Elischer } 5274cf49a43SJulian Elischer 5284cf49a43SJulian Elischer /* 5294cf49a43SJulian Elischer * Get a netgraph control message. 5304cf49a43SJulian Elischer * Check it is one we understand. If needed, send a response. 5314cf49a43SJulian Elischer * We sometimes save the address for an async action later. 5324cf49a43SJulian Elischer * Always free the message. 5334cf49a43SJulian Elischer */ 5344cf49a43SJulian Elischer static int 5358876b55dSJulian Elischer ng_pppoe_rcvmsg(node_p node, 5364cf49a43SJulian Elischer struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr) 5374cf49a43SJulian Elischer { 5384cf49a43SJulian Elischer priv_p privp = node->private; 5398876b55dSJulian Elischer struct ngpppoe_init_data *ourmsg = NULL; 5404cf49a43SJulian Elischer struct ng_mesg *resp = NULL; 5414cf49a43SJulian Elischer int error = 0; 5424cf49a43SJulian Elischer hook_p hook = NULL; 5434cf49a43SJulian Elischer sessp sp = NULL; 5444cf49a43SJulian Elischer negp neg = NULL; 5454cf49a43SJulian Elischer 5461e2510f8SJulian Elischer AAA 5474cf49a43SJulian Elischer /* Deal with message according to cookie and command */ 5484cf49a43SJulian Elischer switch (msg->header.typecookie) { 5494cf49a43SJulian Elischer case NGM_PPPOE_COOKIE: 5504cf49a43SJulian Elischer switch (msg->header.cmd) { 5514cf49a43SJulian Elischer case NGM_PPPOE_CONNECT: 5524cf49a43SJulian Elischer case NGM_PPPOE_LISTEN: 5534cf49a43SJulian Elischer case NGM_PPPOE_OFFER: 5548876b55dSJulian Elischer ourmsg = (struct ngpppoe_init_data *)msg->data; 5554cf49a43SJulian Elischer if (( sizeof(*ourmsg) > msg->header.arglen) 5564cf49a43SJulian Elischer || ((sizeof(*ourmsg) + ourmsg->data_len) 5574cf49a43SJulian Elischer > msg->header.arglen)) { 5588876b55dSJulian Elischer printf("pppoe_rcvmsg: bad arg size"); 5594cf49a43SJulian Elischer LEAVE(EMSGSIZE); 5604cf49a43SJulian Elischer } 5614cf49a43SJulian Elischer if (ourmsg->data_len > PPPOE_SERVICE_NAME_SIZE) { 5624cf49a43SJulian Elischer printf("pppoe: init data too long (%d)\n", 5634cf49a43SJulian Elischer ourmsg->data_len); 5644cf49a43SJulian Elischer LEAVE(EMSGSIZE); 5654cf49a43SJulian Elischer } 5664cf49a43SJulian Elischer /* make sure strcmp will terminate safely */ 5674cf49a43SJulian Elischer ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0'; 5684cf49a43SJulian Elischer 5694cf49a43SJulian Elischer /* cycle through all known hooks */ 5704cf49a43SJulian Elischer LIST_FOREACH(hook, &node->hooks, hooks) { 5714cf49a43SJulian Elischer if (hook->name 5724cf49a43SJulian Elischer && strcmp(hook->name, ourmsg->hook) == 0) 5734cf49a43SJulian Elischer break; 5744cf49a43SJulian Elischer } 5754cf49a43SJulian Elischer if (hook == NULL) { 5764cf49a43SJulian Elischer LEAVE(ENOENT); 5774cf49a43SJulian Elischer } 5784cf49a43SJulian Elischer if ((hook->private == &privp->debug_hook) 5794cf49a43SJulian Elischer || (hook->private == &privp->ethernet_hook)) { 5804cf49a43SJulian Elischer LEAVE(EINVAL); 5814cf49a43SJulian Elischer } 5824cf49a43SJulian Elischer sp = hook->private; 5834cf49a43SJulian Elischer if (sp->state |= PPPOE_SNONE) { 5844cf49a43SJulian Elischer printf("pppoe: Session already active\n"); 5854cf49a43SJulian Elischer LEAVE(EISCONN); 5864cf49a43SJulian Elischer } 5871e2510f8SJulian Elischer 5884cf49a43SJulian Elischer /* 5894cf49a43SJulian Elischer * set up prototype header 5904cf49a43SJulian Elischer */ 5914cf49a43SJulian Elischer MALLOC(neg, negp, sizeof(*neg), M_NETGRAPH, M_WAITOK); 5924cf49a43SJulian Elischer 5934cf49a43SJulian Elischer if (neg == NULL) { 5944cf49a43SJulian Elischer printf("pppoe: Session out of memory\n"); 5954cf49a43SJulian Elischer LEAVE(ENOMEM); 5964cf49a43SJulian Elischer } 5974cf49a43SJulian Elischer bzero(neg, sizeof(*neg)); 5984cf49a43SJulian Elischer MGETHDR(neg->m, M_DONTWAIT, MT_DATA); 5994cf49a43SJulian Elischer if(neg->m == NULL) { 6001e2510f8SJulian Elischer printf("pppoe: Session out of mbufs\n"); 6014cf49a43SJulian Elischer FREE(neg, M_NETGRAPH); 6024cf49a43SJulian Elischer LEAVE(ENOBUFS); 6034cf49a43SJulian Elischer } 6044cf49a43SJulian Elischer neg->m->m_pkthdr.rcvif = NULL; 6054cf49a43SJulian Elischer MCLGET(neg->m, M_DONTWAIT); 6064cf49a43SJulian Elischer if ((neg->m->m_flags & M_EXT) == 0) { 6071e2510f8SJulian Elischer printf("pppoe: Session out of mcls\n"); 6084cf49a43SJulian Elischer m_freem(neg->m); 6094cf49a43SJulian Elischer FREE(neg, M_NETGRAPH); 6104cf49a43SJulian Elischer LEAVE(ENOBUFS); 6114cf49a43SJulian Elischer } 6124cf49a43SJulian Elischer sp->neg = neg; 6131e2510f8SJulian Elischer callout_handle_init( &neg->timeout_handle); 6144cf49a43SJulian Elischer neg->m->m_len = sizeof(struct pppoe_full_hdr); 6154cf49a43SJulian Elischer neg->pkt = mtod(neg->m, union packet*); 6164cf49a43SJulian Elischer neg->pkt->pkt_header.eh = eh_prototype; 6174cf49a43SJulian Elischer neg->pkt->pkt_header.ph.ver = 0x1; 6184cf49a43SJulian Elischer neg->pkt->pkt_header.ph.type = 0x1; 6194cf49a43SJulian Elischer neg->pkt->pkt_header.ph.sid = 0x0000; 6204cf49a43SJulian Elischer neg->timeout = 0; 6214cf49a43SJulian Elischer 6224cf49a43SJulian Elischer strncpy(sp->creator, retaddr, NG_NODELEN); 6234cf49a43SJulian Elischer sp->creator[NG_NODELEN] = '\0'; 6244cf49a43SJulian Elischer } 6254cf49a43SJulian Elischer switch (msg->header.cmd) { 6264cf49a43SJulian Elischer case NGM_PPPOE_GET_STATUS: 6274cf49a43SJulian Elischer { 6288876b55dSJulian Elischer struct ngpppoestat *stats; 6294cf49a43SJulian Elischer 6304cf49a43SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 6314cf49a43SJulian Elischer if (!resp) { 6324cf49a43SJulian Elischer LEAVE(ENOMEM); 6334cf49a43SJulian Elischer } 6348876b55dSJulian Elischer stats = (struct ngpppoestat *) resp->data; 6354cf49a43SJulian Elischer stats->packets_in = privp->packets_in; 6364cf49a43SJulian Elischer stats->packets_out = privp->packets_out; 6374cf49a43SJulian Elischer break; 6384cf49a43SJulian Elischer } 6394cf49a43SJulian Elischer case NGM_PPPOE_CONNECT: 6404cf49a43SJulian Elischer /* 6414cf49a43SJulian Elischer * Check the hook exists and is Uninitialised. 6424cf49a43SJulian Elischer * Send a PADI request, and start the timeout logic. 6434cf49a43SJulian Elischer * Store the originator of this message so we can send 6444cf49a43SJulian Elischer * a success of fail message to them later. 6454cf49a43SJulian Elischer * Move the session to SINIT 6464cf49a43SJulian Elischer * Set up the session to the correct state and 6474cf49a43SJulian Elischer * start it. 6484cf49a43SJulian Elischer */ 6494cf49a43SJulian Elischer neg->service.hdr.tag_type = PTT_SRV_NAME; 6504cf49a43SJulian Elischer neg->service.hdr.tag_len = 6514cf49a43SJulian Elischer htons((u_int16_t)ourmsg->data_len); 6521e2510f8SJulian Elischer if (ourmsg->data_len) { 6534cf49a43SJulian Elischer bcopy(ourmsg->data, 6544cf49a43SJulian Elischer neg->service.data, ourmsg->data_len); 6551e2510f8SJulian Elischer } 6564cf49a43SJulian Elischer neg->service_len = ourmsg->data_len; 6574cf49a43SJulian Elischer pppoe_start(sp); 6584cf49a43SJulian Elischer break; 6594cf49a43SJulian Elischer case NGM_PPPOE_LISTEN: 6604cf49a43SJulian Elischer /* 6614cf49a43SJulian Elischer * Check the hook exists and is Uninitialised. 6624cf49a43SJulian Elischer * Install the service matching string. 6634cf49a43SJulian Elischer * Store the originator of this message so we can send 6644cf49a43SJulian Elischer * a success of fail message to them later. 6654cf49a43SJulian Elischer * Move the hook to 'LISTENING' 6661e2510f8SJulian Elischer 6674cf49a43SJulian Elischer */ 6684cf49a43SJulian Elischer neg->service.hdr.tag_type = PTT_SRV_NAME; 6694cf49a43SJulian Elischer neg->service.hdr.tag_len = 6704cf49a43SJulian Elischer htons((u_int16_t)ourmsg->data_len); 6711e2510f8SJulian Elischer 6721e2510f8SJulian Elischer if (ourmsg->data_len) { 6734cf49a43SJulian Elischer bcopy(ourmsg->data, 6744cf49a43SJulian Elischer neg->service.data, ourmsg->data_len); 6751e2510f8SJulian Elischer } 6764cf49a43SJulian Elischer neg->service_len = ourmsg->data_len; 6774cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADT_CODE; 6784cf49a43SJulian Elischer /* 6794cf49a43SJulian Elischer * wait for PADI packet coming from ethernet 6804cf49a43SJulian Elischer */ 6814cf49a43SJulian Elischer sp->state = PPPOE_LISTENING; 6824cf49a43SJulian Elischer break; 6834cf49a43SJulian Elischer case NGM_PPPOE_OFFER: 6844cf49a43SJulian Elischer /* 6854cf49a43SJulian Elischer * Check the hook exists and is Uninitialised. 6864cf49a43SJulian Elischer * Store the originator of this message so we can send 6874cf49a43SJulian Elischer * a success of fail message to them later. 6884cf49a43SJulian Elischer * Store the AC-Name given and go to PRIMED. 6894cf49a43SJulian Elischer */ 6904cf49a43SJulian Elischer neg->ac_name.hdr.tag_type = PTT_AC_NAME; 6914cf49a43SJulian Elischer neg->ac_name.hdr.tag_len = 6924cf49a43SJulian Elischer htons((u_int16_t)ourmsg->data_len); 6931e2510f8SJulian Elischer if (ourmsg->data_len) { 6944cf49a43SJulian Elischer bcopy(ourmsg->data, 6954cf49a43SJulian Elischer neg->ac_name.data, ourmsg->data_len); 6961e2510f8SJulian Elischer } 6974cf49a43SJulian Elischer neg->ac_name_len = ourmsg->data_len; 6984cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADO_CODE; 6994cf49a43SJulian Elischer /* 7004cf49a43SJulian Elischer * Wait for PADI packet coming from hook 7014cf49a43SJulian Elischer */ 7024cf49a43SJulian Elischer sp->state = PPPOE_PRIMED; 7034cf49a43SJulian Elischer break; 7044cf49a43SJulian Elischer default: 7054cf49a43SJulian Elischer LEAVE(EINVAL); 7064cf49a43SJulian Elischer } 7074cf49a43SJulian Elischer break; 7084cf49a43SJulian Elischer default: 7094cf49a43SJulian Elischer LEAVE(EINVAL); 7104cf49a43SJulian Elischer } 7114cf49a43SJulian Elischer 7124cf49a43SJulian Elischer /* Take care of synchronous response, if any */ 7134cf49a43SJulian Elischer if (rptr) 7144cf49a43SJulian Elischer *rptr = resp; 7154cf49a43SJulian Elischer else if (resp) 7164cf49a43SJulian Elischer FREE(resp, M_NETGRAPH); 7174cf49a43SJulian Elischer 7184cf49a43SJulian Elischer /* Free the message and return */ 7194cf49a43SJulian Elischer quit: 7204cf49a43SJulian Elischer FREE(msg, M_NETGRAPH); 7214cf49a43SJulian Elischer return(error); 7224cf49a43SJulian Elischer } 7234cf49a43SJulian Elischer 7241e2510f8SJulian Elischer /* 7251e2510f8SJulian Elischer * Start a client into the first state. A separate function because 7261e2510f8SJulian Elischer * it can be needed if the negotiation times out. 7271e2510f8SJulian Elischer */ 7284cf49a43SJulian Elischer static void 7294cf49a43SJulian Elischer pppoe_start(sessp sp) 7304cf49a43SJulian Elischer { 7314cf49a43SJulian Elischer struct { 7324cf49a43SJulian Elischer struct pppoe_tag hdr; 7334cf49a43SJulian Elischer union uniq data; 7344cf49a43SJulian Elischer } uniqtag; 7354cf49a43SJulian Elischer 7364cf49a43SJulian Elischer /* 7374cf49a43SJulian Elischer * kick the state machine into starting up 7384cf49a43SJulian Elischer */ 7391e2510f8SJulian Elischer AAA 7404cf49a43SJulian Elischer sp->state = PPPOE_SINIT; 7411e2510f8SJulian Elischer /* reset the packet header to broadcast */ 7421e2510f8SJulian Elischer sp->neg->pkt->pkt_header.eh = eh_prototype; 7431e2510f8SJulian Elischer sp->neg->pkt->pkt_header.ph.code = PADI_CODE; 7444cf49a43SJulian Elischer uniqtag.hdr.tag_type = PTT_HOST_UNIQ; 7454cf49a43SJulian Elischer uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(uniqtag.data)); 7464cf49a43SJulian Elischer uniqtag.data.pointer = sp; 7474cf49a43SJulian Elischer init_tags(sp); 7484cf49a43SJulian Elischer insert_tag(sp, &uniqtag.hdr); 7494cf49a43SJulian Elischer insert_tag(sp, &sp->neg->service.hdr); 7504cf49a43SJulian Elischer make_packet(sp); 7514cf49a43SJulian Elischer sendpacket(sp); 7524cf49a43SJulian Elischer } 7534cf49a43SJulian Elischer 7544cf49a43SJulian Elischer /* 7554cf49a43SJulian Elischer * Receive data, and do something with it. 7564cf49a43SJulian Elischer * The caller will never free m or meta, so 7574cf49a43SJulian Elischer * if we use up this data or abort we must free BOTH of these. 7584cf49a43SJulian Elischer */ 7594cf49a43SJulian Elischer static int 7608876b55dSJulian Elischer ng_pppoe_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 7614cf49a43SJulian Elischer { 7624cf49a43SJulian Elischer node_p node = hook->node; 7634cf49a43SJulian Elischer const priv_p privp = node->private; 7644cf49a43SJulian Elischer sessp sp = hook->private; 7654cf49a43SJulian Elischer struct pppoe_full_hdr *wh; 7664cf49a43SJulian Elischer struct pppoe_hdr *ph; 7674cf49a43SJulian Elischer int error = 0; 7684cf49a43SJulian Elischer u_int16_t session; 7694cf49a43SJulian Elischer u_int16_t length; 7704cf49a43SJulian Elischer u_int8_t code; 7714cf49a43SJulian Elischer struct pppoe_tag *tag = NULL; 7724cf49a43SJulian Elischer hook_p sendhook; 7734cf49a43SJulian Elischer struct { 7744cf49a43SJulian Elischer struct pppoe_tag hdr; 7754cf49a43SJulian Elischer union uniq data; 7764cf49a43SJulian Elischer } uniqtag; 7774cf49a43SJulian Elischer negp neg = NULL; 7784cf49a43SJulian Elischer 7791e2510f8SJulian Elischer AAA 7804cf49a43SJulian Elischer if (hook->private == &privp->debug_hook) { 7814cf49a43SJulian Elischer /* 7824cf49a43SJulian Elischer * Data from the debug hook gets sent without modification 7834cf49a43SJulian Elischer * straight to the ethernet. 7844cf49a43SJulian Elischer */ 7854cf49a43SJulian Elischer NG_SEND_DATA( error, privp->ethernet_hook, m, meta); 7864cf49a43SJulian Elischer privp->packets_out++; 7874cf49a43SJulian Elischer } else if (hook->private == &privp->ethernet_hook) { 7884cf49a43SJulian Elischer /* 7894cf49a43SJulian Elischer * Incoming data. 7904cf49a43SJulian Elischer * Dig out various fields from the packet. 7914cf49a43SJulian Elischer * use them to decide where to send it. 7924cf49a43SJulian Elischer */ 7934cf49a43SJulian Elischer 7944cf49a43SJulian Elischer privp->packets_in++; 7950c65c135SJulian Elischer if( m->m_len < sizeof(*wh)) { 7960c65c135SJulian Elischer m = m_pullup(m, sizeof(*wh)); /* Checks length */ 7974cf49a43SJulian Elischer if (m == NULL) { 798b86d0a9eSJulian Elischer printf("couldn't m_pullup\n"); 7994cf49a43SJulian Elischer LEAVE(ENOBUFS); 8004cf49a43SJulian Elischer } 8010c65c135SJulian Elischer } 8024cf49a43SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *); 8034cf49a43SJulian Elischer ph = &wh->ph; 8044cf49a43SJulian Elischer session = ntohs(wh->ph.sid); 8054cf49a43SJulian Elischer length = ntohs(wh->ph.length); 8064cf49a43SJulian Elischer code = wh->ph.code; 8070c65c135SJulian Elischer switch(wh->eh.ether_type) { 8084cf49a43SJulian Elischer case ETHERTYPE_PPPOE_DISC: 8094cf49a43SJulian Elischer /* 8104cf49a43SJulian Elischer * We need to try make sure that the tag area 8114cf49a43SJulian Elischer * is contiguous, or we could wander of the end 8124cf49a43SJulian Elischer * of a buffer and make a mess. 8134cf49a43SJulian Elischer * (Linux wouldn't have this problem). 8144cf49a43SJulian Elischer */ 8150c65c135SJulian Elischer /*XXX fix this mess */ 8160c65c135SJulian Elischer 8170c65c135SJulian Elischer if (m->m_pkthdr.len <= MHLEN) { 8180c65c135SJulian Elischer if( m->m_len < m->m_pkthdr.len) { 8190c65c135SJulian Elischer m = m_pullup(m, m->m_pkthdr.len); 8200c65c135SJulian Elischer if (m == NULL) { 8210c65c135SJulian Elischer printf("couldn't m_pullup\n"); 8220c65c135SJulian Elischer LEAVE(ENOBUFS); 8230c65c135SJulian Elischer } 8240c65c135SJulian Elischer } 8250c65c135SJulian Elischer } 8264cf49a43SJulian Elischer if (m->m_len != m->m_pkthdr.len) { 8274cf49a43SJulian Elischer /* 8284cf49a43SJulian Elischer * It's not all in one piece. 8294cf49a43SJulian Elischer * We need to do extra work. 8304cf49a43SJulian Elischer */ 8314cf49a43SJulian Elischer printf("packet fragmented\n"); 832b86d0a9eSJulian Elischer LEAVE(EMSGSIZE); 8334cf49a43SJulian Elischer } 8344cf49a43SJulian Elischer 8354cf49a43SJulian Elischer switch(code) { 8364cf49a43SJulian Elischer case PADI_CODE: 8374cf49a43SJulian Elischer /* 8384cf49a43SJulian Elischer * We are a server: 8394cf49a43SJulian Elischer * Look for a hook with the required service 8404cf49a43SJulian Elischer * and send the ENTIRE packet up there. 8414cf49a43SJulian Elischer * It should come back to a new hook in 8424cf49a43SJulian Elischer * PRIMED state. Look there for further 8434cf49a43SJulian Elischer * processing. 8444cf49a43SJulian Elischer */ 8454cf49a43SJulian Elischer tag = get_tag(ph, PTT_SRV_NAME); 8464cf49a43SJulian Elischer if (tag == NULL) { 847b86d0a9eSJulian Elischer printf("no service tag\n"); 8484cf49a43SJulian Elischer LEAVE(ENETUNREACH); 8494cf49a43SJulian Elischer } 8504cf49a43SJulian Elischer sendhook = pppoe_match_svc(hook->node, 8514cf49a43SJulian Elischer tag->tag_data, ntohs(tag->tag_len)); 8524cf49a43SJulian Elischer if (sendhook) { 8534cf49a43SJulian Elischer NG_SEND_DATA(error, sendhook, m, meta); 8544cf49a43SJulian Elischer } else { 855b86d0a9eSJulian Elischer printf("no such service\n"); 8564cf49a43SJulian Elischer LEAVE(ENETUNREACH); 8574cf49a43SJulian Elischer } 8584cf49a43SJulian Elischer break; 8594cf49a43SJulian Elischer case PADO_CODE: 8604cf49a43SJulian Elischer /* 8614cf49a43SJulian Elischer * We are a client: 8624cf49a43SJulian Elischer * Use the host_uniq tag to find the 8634cf49a43SJulian Elischer * hook this is in response to. 864b86d0a9eSJulian Elischer * Received #2, now send #3 8654cf49a43SJulian Elischer * For now simply accept the first we receive. 8664cf49a43SJulian Elischer */ 8674cf49a43SJulian Elischer tag = get_tag(ph, PTT_HOST_UNIQ); 8684cf49a43SJulian Elischer if ((tag == NULL) 8694cf49a43SJulian Elischer || (ntohs(tag->tag_len) != sizeof(sp))) { 870b86d0a9eSJulian Elischer printf("no host unique field\n"); 8714cf49a43SJulian Elischer LEAVE(ENETUNREACH); 8724cf49a43SJulian Elischer } 8734cf49a43SJulian Elischer 8744cf49a43SJulian Elischer sendhook = pppoe_finduniq(node, tag); 8754cf49a43SJulian Elischer if (sendhook == NULL) { 876b86d0a9eSJulian Elischer printf("no matching session\n"); 8774cf49a43SJulian Elischer LEAVE(ENETUNREACH); 8784cf49a43SJulian Elischer } 8794cf49a43SJulian Elischer 8804cf49a43SJulian Elischer /* 8814cf49a43SJulian Elischer * Check the session is in the right state. 8824cf49a43SJulian Elischer * It needs to be in PPPOE_SINIT. 8834cf49a43SJulian Elischer */ 8844cf49a43SJulian Elischer sp = sendhook->private; 8854cf49a43SJulian Elischer if (sp->state != PPPOE_SINIT) { 886b86d0a9eSJulian Elischer printf("session in wrong state\n"); 8874cf49a43SJulian Elischer LEAVE(ENETUNREACH); 8884cf49a43SJulian Elischer } 8894cf49a43SJulian Elischer neg = sp->neg; 8904cf49a43SJulian Elischer untimeout(pppoe_ticker, sendhook, 8914cf49a43SJulian Elischer neg->timeout_handle); 8924cf49a43SJulian Elischer 8934cf49a43SJulian Elischer /* 8944cf49a43SJulian Elischer * This is the first time we hear 8954cf49a43SJulian Elischer * from the server, so note it's 8964cf49a43SJulian Elischer * unicast address, replacing the 8974cf49a43SJulian Elischer * broadcast address . 8984cf49a43SJulian Elischer */ 8994cf49a43SJulian Elischer bcopy(wh->eh.ether_shost, 9004cf49a43SJulian Elischer neg->pkt->pkt_header.eh.ether_dhost, 9014cf49a43SJulian Elischer ETHER_ADDR_LEN); 9024cf49a43SJulian Elischer neg->timeout = 0; 9034cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADR_CODE; 9044cf49a43SJulian Elischer init_tags(sp); 9054cf49a43SJulian Elischer insert_tag(sp, &neg->service.hdr); /* Service */ 9064cf49a43SJulian Elischer insert_tag(sp, tag); /* Host Unique */ 9074cf49a43SJulian Elischer tag = get_tag(ph, PTT_AC_COOKIE); 908b86d0a9eSJulian Elischer if (tag) 909b86d0a9eSJulian Elischer insert_tag(sp, tag); /* return cookie */ 9104b276f90SJulian Elischer tag = get_tag(ph, PTT_AC_NAME); 9114b276f90SJulian Elischer if (tag) 9124b276f90SJulian Elischer insert_tag(sp, tag); /* return cookie */ 9134cf49a43SJulian Elischer scan_tags(sp, ph); 9144cf49a43SJulian Elischer make_packet(sp); 9154cf49a43SJulian Elischer sp->state = PPPOE_SREQ; 9164cf49a43SJulian Elischer sendpacket(sp); 9174cf49a43SJulian Elischer break; 9184cf49a43SJulian Elischer case PADR_CODE: 9194cf49a43SJulian Elischer 9204cf49a43SJulian Elischer /* 9214cf49a43SJulian Elischer * We are a server: 9224cf49a43SJulian Elischer * Use the ac_cookie tag to find the 9234cf49a43SJulian Elischer * hook this is in response to. 9244cf49a43SJulian Elischer */ 9254cf49a43SJulian Elischer tag = get_tag(ph, PTT_AC_COOKIE); 9264cf49a43SJulian Elischer if ((tag == NULL) 9274cf49a43SJulian Elischer || (ntohs(tag->tag_len) != sizeof(sp))) { 9284cf49a43SJulian Elischer LEAVE(ENETUNREACH); 9294cf49a43SJulian Elischer } 9304cf49a43SJulian Elischer 9314cf49a43SJulian Elischer sendhook = pppoe_finduniq(node, tag); 9324cf49a43SJulian Elischer if (sendhook == NULL) { 9334cf49a43SJulian Elischer LEAVE(ENETUNREACH); 9344cf49a43SJulian Elischer } 9354cf49a43SJulian Elischer 9364cf49a43SJulian Elischer /* 9374cf49a43SJulian Elischer * Check the session is in the right state. 9384cf49a43SJulian Elischer * It needs to be in PPPOE_SOFFER 9394cf49a43SJulian Elischer * or PPPOE_NEWCONNECTED. If the latter, 9404cf49a43SJulian Elischer * then this is a retry by the client. 9414cf49a43SJulian Elischer * so be nice, and resend. 9424cf49a43SJulian Elischer */ 9434cf49a43SJulian Elischer sp = sendhook->private; 9444cf49a43SJulian Elischer if (sp->state == PPPOE_NEWCONNECTED) { 9454cf49a43SJulian Elischer /* 9464cf49a43SJulian Elischer * Whoa! drop back to resend that 9474cf49a43SJulian Elischer * PADS packet. 9484cf49a43SJulian Elischer * We should still have a copy of it. 9494cf49a43SJulian Elischer */ 9504cf49a43SJulian Elischer sp->state = PPPOE_SOFFER; 9514cf49a43SJulian Elischer } 9524cf49a43SJulian Elischer if (sp->state != PPPOE_SOFFER) { 9534cf49a43SJulian Elischer LEAVE (ENETUNREACH); 9544cf49a43SJulian Elischer break; 9554cf49a43SJulian Elischer } 9564cf49a43SJulian Elischer neg = sp->neg; 9574cf49a43SJulian Elischer untimeout(pppoe_ticker, sendhook, 9584cf49a43SJulian Elischer neg->timeout_handle); 9594cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADS_CODE; 9604cf49a43SJulian Elischer if (sp->Session_ID == 0) 9614cf49a43SJulian Elischer neg->pkt->pkt_header.ph.sid = 962b86d0a9eSJulian Elischer htons(sp->Session_ID 963b86d0a9eSJulian Elischer = get_new_sid(node)); 9644cf49a43SJulian Elischer neg->timeout = 0; 9654cf49a43SJulian Elischer /* 9664cf49a43SJulian Elischer * start working out the tags to respond with. 9674cf49a43SJulian Elischer */ 9684cf49a43SJulian Elischer init_tags(sp); 9694cf49a43SJulian Elischer insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 9704cf49a43SJulian Elischer insert_tag(sp, tag); /* ac_cookie */ 9714cf49a43SJulian Elischer tag = get_tag(ph, PTT_SRV_NAME); 9724cf49a43SJulian Elischer insert_tag(sp, tag); /* returned service */ 9734cf49a43SJulian Elischer tag = get_tag(ph, PTT_HOST_UNIQ); 9744cf49a43SJulian Elischer insert_tag(sp, tag); /* returned hostuniq */ 9754cf49a43SJulian Elischer scan_tags(sp, ph); 9764cf49a43SJulian Elischer make_packet(sp); 9774cf49a43SJulian Elischer sp->state = PPPOE_NEWCONNECTED; 9784cf49a43SJulian Elischer sendpacket(sp); 979b58a8a3bSJulian Elischer pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 9804cf49a43SJulian Elischer /* 9814cf49a43SJulian Elischer * Having sent the last Negotiation header, 9824cf49a43SJulian Elischer * Set up the stored packet header to 9834cf49a43SJulian Elischer * be correct for the actual session. 9844cf49a43SJulian Elischer * But keep the negotialtion stuff 9854cf49a43SJulian Elischer * around in case we need to resend this last 9864cf49a43SJulian Elischer * packet. We'll discard it when we move 9874cf49a43SJulian Elischer * from NEWCONNECTED to CONNECTED 9884cf49a43SJulian Elischer */ 9894cf49a43SJulian Elischer sp->pkt_hdr = neg->pkt->pkt_header; 9904cf49a43SJulian Elischer sp->pkt_hdr.eh.ether_type 9914cf49a43SJulian Elischer = ETHERTYPE_PPPOE_SESS; 9924cf49a43SJulian Elischer sp->pkt_hdr.ph.code = 0; 993b58a8a3bSJulian Elischer pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 9944cf49a43SJulian Elischer break; 9954cf49a43SJulian Elischer case PADS_CODE: 9964cf49a43SJulian Elischer /* 9974cf49a43SJulian Elischer * We are a client: 9984cf49a43SJulian Elischer * Use the host_uniq tag to find the 9994cf49a43SJulian Elischer * hook this is in response to. 10004cf49a43SJulian Elischer * take the session ID and store it away. 10014cf49a43SJulian Elischer * Also make sure the pre-made header is 10024cf49a43SJulian Elischer * correct and set us into Session mode. 10034cf49a43SJulian Elischer */ 10044cf49a43SJulian Elischer tag = get_tag(ph, PTT_HOST_UNIQ); 10054cf49a43SJulian Elischer if ((tag == NULL) 10064cf49a43SJulian Elischer || (ntohs(tag->tag_len) != sizeof(sp))) { 10074cf49a43SJulian Elischer LEAVE (ENETUNREACH); 10084cf49a43SJulian Elischer break; 10094cf49a43SJulian Elischer } 10104cf49a43SJulian Elischer 10114cf49a43SJulian Elischer sendhook = pppoe_finduniq(node, tag); 10124cf49a43SJulian Elischer if (sendhook == NULL) { 10134cf49a43SJulian Elischer LEAVE(ENETUNREACH); 10144cf49a43SJulian Elischer } 10154cf49a43SJulian Elischer 10164cf49a43SJulian Elischer /* 10174cf49a43SJulian Elischer * Check the session is in the right state. 10184cf49a43SJulian Elischer * It needs to be in PPPOE_SREQ. 10194cf49a43SJulian Elischer */ 10204cf49a43SJulian Elischer sp = sendhook->private; 10214cf49a43SJulian Elischer if (sp->state != PPPOE_SREQ) { 10224cf49a43SJulian Elischer LEAVE(ENETUNREACH); 10234cf49a43SJulian Elischer } 10244cf49a43SJulian Elischer neg = sp->neg; 10254cf49a43SJulian Elischer untimeout(pppoe_ticker, sendhook, 10264cf49a43SJulian Elischer neg->timeout_handle); 1027cfbcfe62SJulian Elischer neg->pkt->pkt_header.ph.sid = wh->ph.sid; 1028b86d0a9eSJulian Elischer sp->Session_ID = ntohs(wh->ph.sid); 10294cf49a43SJulian Elischer neg->timeout = 0; 10304cf49a43SJulian Elischer sp->state = PPPOE_CONNECTED; 10314cf49a43SJulian Elischer /* 10324cf49a43SJulian Elischer * Now we have gone to Connected mode, 10334cf49a43SJulian Elischer * Free all resources needed for 10344cf49a43SJulian Elischer * negotiation. 10354cf49a43SJulian Elischer * Keep a copy of the header we will be using. 10364cf49a43SJulian Elischer */ 10374cf49a43SJulian Elischer sp->pkt_hdr = neg->pkt->pkt_header; 10384cf49a43SJulian Elischer sp->pkt_hdr.eh.ether_type 10394cf49a43SJulian Elischer = ETHERTYPE_PPPOE_SESS; 10404cf49a43SJulian Elischer sp->pkt_hdr.ph.code = 0; 10414cf49a43SJulian Elischer m_freem(neg->m); 10424cf49a43SJulian Elischer FREE(sp->neg, M_NETGRAPH); 10434cf49a43SJulian Elischer sp->neg = NULL; 1044b58a8a3bSJulian Elischer pppoe_send_event(sp, NGM_PPPOE_SUCCESS); 10454cf49a43SJulian Elischer break; 10464cf49a43SJulian Elischer case PADT_CODE: 10474cf49a43SJulian Elischer /* 10484cf49a43SJulian Elischer * Send a 'close' message to the controlling 10494cf49a43SJulian Elischer * process (the one that set us up); 10504cf49a43SJulian Elischer * And then tear everything down. 10514cf49a43SJulian Elischer * 10524cf49a43SJulian Elischer * Find matching peer/session combination. 10534cf49a43SJulian Elischer */ 10544cf49a43SJulian Elischer sendhook = pppoe_findsession(node, wh); 10554cf49a43SJulian Elischer NG_FREE_DATA(m, meta); /* no longer needed */ 10564cf49a43SJulian Elischer if (sendhook == NULL) { 10574cf49a43SJulian Elischer LEAVE(ENETUNREACH); 10584cf49a43SJulian Elischer } 10594cf49a43SJulian Elischer /* send message to creator */ 10604cf49a43SJulian Elischer /* close hook */ 1061b58a8a3bSJulian Elischer if (sendhook) { 10624cf49a43SJulian Elischer ng_destroy_hook(sendhook); 1063b58a8a3bSJulian Elischer } 10644cf49a43SJulian Elischer break; 10654cf49a43SJulian Elischer default: 10664cf49a43SJulian Elischer LEAVE(EPFNOSUPPORT); 10674cf49a43SJulian Elischer } 10684cf49a43SJulian Elischer break; 10694cf49a43SJulian Elischer case ETHERTYPE_PPPOE_SESS: 10704cf49a43SJulian Elischer /* 10714cf49a43SJulian Elischer * find matching peer/session combination. 10724cf49a43SJulian Elischer */ 10734cf49a43SJulian Elischer sendhook = pppoe_findsession(node, wh); 10744cf49a43SJulian Elischer if (sendhook == NULL) { 10754cf49a43SJulian Elischer LEAVE (ENETUNREACH); 10764cf49a43SJulian Elischer break; 10774cf49a43SJulian Elischer } 10784b276f90SJulian Elischer sp = sendhook->private; 10794cf49a43SJulian Elischer m_adj(m, sizeof(*wh)); 10804cf49a43SJulian Elischer if (m->m_pkthdr.len < length) { 10814cf49a43SJulian Elischer /* Packet too short, dump it */ 10824cf49a43SJulian Elischer LEAVE(EMSGSIZE); 10834cf49a43SJulian Elischer } 10849fcb3d83SJulian Elischer 10859fcb3d83SJulian Elischer if (m->m_pkthdr.len > length) { 10869fcb3d83SJulian Elischer m_adj(m, -((int)(m->m_pkthdr.len - length))); 10879fcb3d83SJulian Elischer } 10884cf49a43SJulian Elischer /* XXX also need to trim excess at end I should think */ 10894cf49a43SJulian Elischer if ( sp->state != PPPOE_CONNECTED) { 10904cf49a43SJulian Elischer if (sp->state == PPPOE_NEWCONNECTED) { 10914cf49a43SJulian Elischer sp->state = PPPOE_CONNECTED; 10924cf49a43SJulian Elischer /* 10934cf49a43SJulian Elischer * Now we have gone to Connected mode, 10944cf49a43SJulian Elischer * Free all resources needed for 10954cf49a43SJulian Elischer * negotiation. 10964cf49a43SJulian Elischer */ 10974cf49a43SJulian Elischer m_freem(sp->neg->m); 10984cf49a43SJulian Elischer FREE(sp->neg, M_NETGRAPH); 10994cf49a43SJulian Elischer sp->neg = NULL; 11004cf49a43SJulian Elischer } else { 11014cf49a43SJulian Elischer LEAVE (ENETUNREACH); 11024cf49a43SJulian Elischer break; 11034cf49a43SJulian Elischer } 11044cf49a43SJulian Elischer } 11054cf49a43SJulian Elischer NG_SEND_DATA( error, sendhook, m, meta); 11064cf49a43SJulian Elischer break; 11074cf49a43SJulian Elischer default: 11084b276f90SJulian Elischer LEAVE(EPFNOSUPPORT); 11094cf49a43SJulian Elischer } 11104cf49a43SJulian Elischer } else { 11114cf49a43SJulian Elischer /* 11124cf49a43SJulian Elischer * Not ethernet or debug hook.. 11134cf49a43SJulian Elischer * 11144cf49a43SJulian Elischer * The packet has come in on a normal hook. 11154cf49a43SJulian Elischer * We need to find out what kind of hook, 11164cf49a43SJulian Elischer * So we can decide how to handle it. 11174cf49a43SJulian Elischer * Check the hook's state. 11184cf49a43SJulian Elischer */ 11194cf49a43SJulian Elischer sp = hook->private; 11204cf49a43SJulian Elischer switch (sp->state) { 11214cf49a43SJulian Elischer case PPPOE_NEWCONNECTED: 11224cf49a43SJulian Elischer case PPPOE_CONNECTED: { 11234cf49a43SJulian Elischer struct pppoe_full_hdr *wh; 11244cf49a43SJulian Elischer /* 11254cf49a43SJulian Elischer * Bang in a pre-made header, and set the length up 11264cf49a43SJulian Elischer * to be correct. Then send it to the ethernet driver. 1127d9da9cbaSJulian Elischer * But first correct the length. 11284cf49a43SJulian Elischer */ 1129d9da9cbaSJulian Elischer sp->pkt_hdr.ph.length = htons((short)(m->m_pkthdr.len)); 11304cf49a43SJulian Elischer M_PREPEND(m, sizeof(*wh), M_DONTWAIT); 11314cf49a43SJulian Elischer if (m == NULL) { 11324cf49a43SJulian Elischer LEAVE(ENOBUFS); 11334cf49a43SJulian Elischer } 11344cf49a43SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *); 11354cf49a43SJulian Elischer bcopy(&sp->pkt_hdr, wh, sizeof(*wh)); 11364cf49a43SJulian Elischer NG_SEND_DATA( error, privp->ethernet_hook, m, meta); 11374cf49a43SJulian Elischer privp->packets_out++; 11384cf49a43SJulian Elischer break; 11394cf49a43SJulian Elischer } 11404cf49a43SJulian Elischer case PPPOE_PRIMED: 11414cf49a43SJulian Elischer /* 11424cf49a43SJulian Elischer * A PADI packet is being returned by the application 11434cf49a43SJulian Elischer * that has set up this hook. This indicates that it 11444cf49a43SJulian Elischer * wants us to offer service. 11454cf49a43SJulian Elischer */ 11464cf49a43SJulian Elischer neg = sp->neg; 1147bef9dae0SJulian Elischer if (m->m_len < sizeof(*wh)) 1148bef9dae0SJulian Elischer m_pullup(m, sizeof(*wh)); 11494cf49a43SJulian Elischer if (m == NULL) { 11504cf49a43SJulian Elischer LEAVE(ENOBUFS); 11514cf49a43SJulian Elischer } 11524cf49a43SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *); 11534cf49a43SJulian Elischer ph = &wh->ph; 11544cf49a43SJulian Elischer session = ntohs(wh->ph.sid); 11554cf49a43SJulian Elischer length = ntohs(wh->ph.length); 11564cf49a43SJulian Elischer code = wh->ph.code; 11571e2510f8SJulian Elischer if ( code != PADI_CODE) { 11581e2510f8SJulian Elischer LEAVE(EINVAL); 11591e2510f8SJulian Elischer }; 11601e2510f8SJulian Elischer untimeout(pppoe_ticker, hook, 11611e2510f8SJulian Elischer neg->timeout_handle); 11624cf49a43SJulian Elischer 11634cf49a43SJulian Elischer /* 11644cf49a43SJulian Elischer * This is the first time we hear 11654cf49a43SJulian Elischer * from the client, so note it's 11664cf49a43SJulian Elischer * unicast address, replacing the 11674cf49a43SJulian Elischer * broadcast address . 11684cf49a43SJulian Elischer */ 11694cf49a43SJulian Elischer bcopy(wh->eh.ether_shost, 11704cf49a43SJulian Elischer neg->pkt->pkt_header.eh.ether_dhost, 11714cf49a43SJulian Elischer ETHER_ADDR_LEN); 11724cf49a43SJulian Elischer sp->state = PPPOE_SOFFER; 11734cf49a43SJulian Elischer neg->timeout = 0; 11744cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADO_CODE; 11754cf49a43SJulian Elischer 11764cf49a43SJulian Elischer /* 11774cf49a43SJulian Elischer * start working out the tags to respond with. 11784cf49a43SJulian Elischer */ 11794cf49a43SJulian Elischer uniqtag.hdr.tag_type = PTT_AC_COOKIE; 11804cf49a43SJulian Elischer uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp)); 11814cf49a43SJulian Elischer uniqtag.data.pointer = sp; 11824cf49a43SJulian Elischer init_tags(sp); 11834cf49a43SJulian Elischer insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */ 11844cf49a43SJulian Elischer insert_tag(sp, tag); /* returned hostunique */ 11854cf49a43SJulian Elischer insert_tag(sp, &uniqtag.hdr); /* AC cookie */ 11864cf49a43SJulian Elischer tag = get_tag(ph, PTT_SRV_NAME); 11874cf49a43SJulian Elischer insert_tag(sp, tag); /* returned service */ 11884cf49a43SJulian Elischer /* XXX maybe put the tag in the session store */ 11894cf49a43SJulian Elischer scan_tags(sp, ph); 11904cf49a43SJulian Elischer make_packet(sp); 11914cf49a43SJulian Elischer sendpacket(sp); 11924cf49a43SJulian Elischer break; 11934cf49a43SJulian Elischer 11944cf49a43SJulian Elischer /* 11954cf49a43SJulian Elischer * Packets coming from the hook make no sense 11964cf49a43SJulian Elischer * to sessions in these states. Throw them away. 11974cf49a43SJulian Elischer */ 11984cf49a43SJulian Elischer case PPPOE_SINIT: 11994cf49a43SJulian Elischer case PPPOE_SREQ: 12004cf49a43SJulian Elischer case PPPOE_SOFFER: 12014cf49a43SJulian Elischer case PPPOE_SNONE: 12024cf49a43SJulian Elischer case PPPOE_LISTENING: 12034cf49a43SJulian Elischer case PPPOE_DEAD: 12044cf49a43SJulian Elischer default: 12054cf49a43SJulian Elischer LEAVE(ENETUNREACH); 12064cf49a43SJulian Elischer } 12074cf49a43SJulian Elischer } 12084cf49a43SJulian Elischer quit: 12094cf49a43SJulian Elischer NG_FREE_DATA(m, meta); 12104cf49a43SJulian Elischer return error; 12114cf49a43SJulian Elischer } 12124cf49a43SJulian Elischer 12134cf49a43SJulian Elischer /* 12144cf49a43SJulian Elischer * Do local shutdown processing.. 12154cf49a43SJulian Elischer * If we are a persistant device, we might refuse to go away, and 12164cf49a43SJulian Elischer * we'd only remove our links and reset ourself. 12174cf49a43SJulian Elischer */ 12184cf49a43SJulian Elischer static int 12198876b55dSJulian Elischer ng_pppoe_rmnode(node_p node) 12204cf49a43SJulian Elischer { 12214cf49a43SJulian Elischer const priv_p privdata = node->private; 12224cf49a43SJulian Elischer 12231e2510f8SJulian Elischer AAA 12244cf49a43SJulian Elischer node->flags |= NG_INVALID; 12254cf49a43SJulian Elischer ng_cutlinks(node); 12264cf49a43SJulian Elischer ng_unname(node); 12274cf49a43SJulian Elischer node->private = NULL; 12284cf49a43SJulian Elischer ng_unref(privdata->node); 12294cf49a43SJulian Elischer FREE(privdata, M_NETGRAPH); 12304cf49a43SJulian Elischer return (0); 12314cf49a43SJulian Elischer } 12324cf49a43SJulian Elischer 12334cf49a43SJulian Elischer /* 12344cf49a43SJulian Elischer * This is called once we've already connected a new hook to the other node. 12354cf49a43SJulian Elischer * It gives us a chance to balk at the last minute. 12364cf49a43SJulian Elischer */ 12374cf49a43SJulian Elischer static int 12388876b55dSJulian Elischer ng_pppoe_connect(hook_p hook) 12394cf49a43SJulian Elischer { 12404cf49a43SJulian Elischer /* be really amiable and just say "YUP that's OK by me! " */ 12414cf49a43SJulian Elischer return (0); 12424cf49a43SJulian Elischer } 12434cf49a43SJulian Elischer 12444cf49a43SJulian Elischer /* 12454cf49a43SJulian Elischer * Hook disconnection 12464cf49a43SJulian Elischer * 12474cf49a43SJulian Elischer * Clean up all dangling links and infirmation about the session/hook. 12484cf49a43SJulian Elischer * For this type, removal of the last link destroys the node 12494cf49a43SJulian Elischer */ 12504cf49a43SJulian Elischer static int 12518876b55dSJulian Elischer ng_pppoe_disconnect(hook_p hook) 12524cf49a43SJulian Elischer { 12534cf49a43SJulian Elischer node_p node = hook->node; 12544cf49a43SJulian Elischer priv_p privp = node->private; 12554cf49a43SJulian Elischer sessp sp; 125604853d8aSJulian Elischer int hooks; 12574cf49a43SJulian Elischer 12581e2510f8SJulian Elischer AAA 12594cf49a43SJulian Elischer if (hook->private == &privp->debug_hook) { 12604cf49a43SJulian Elischer privp->debug_hook = NULL; 12614cf49a43SJulian Elischer } else if (hook->private == &privp->ethernet_hook) { 12624cf49a43SJulian Elischer privp->ethernet_hook = NULL; 126304853d8aSJulian Elischer ng_rmnode(node); 12644cf49a43SJulian Elischer } else { 12654cf49a43SJulian Elischer sp = hook->private; 1266b58a8a3bSJulian Elischer if (sp->state != PPPOE_SNONE ) { 1267b58a8a3bSJulian Elischer pppoe_send_event(sp, NGM_PPPOE_CLOSE); 1268b58a8a3bSJulian Elischer } 12699fcb3d83SJulian Elischer if ((privp->ethernet_hook) 12709fcb3d83SJulian Elischer && ((sp->state == PPPOE_CONNECTED) 12719fcb3d83SJulian Elischer || (sp->state == PPPOE_NEWCONNECTED))) { 12729fcb3d83SJulian Elischer struct mbuf *m; 12739fcb3d83SJulian Elischer struct pppoe_full_hdr *wh; 12749fcb3d83SJulian Elischer struct pppoe_tag *tag; 12759fcb3d83SJulian Elischer int msglen = strlen(SIGNOFF); 12769fcb3d83SJulian Elischer void *dummy = NULL; 12779fcb3d83SJulian Elischer int error = 0; 12789fcb3d83SJulian Elischer 12799fcb3d83SJulian Elischer /* revert the stored header to DISC/PADT mode */ 12809fcb3d83SJulian Elischer wh = &sp->pkt_hdr; 12819fcb3d83SJulian Elischer wh->ph.code = PADT_CODE; 12829fcb3d83SJulian Elischer wh->eh.ether_type = ETHERTYPE_PPPOE_DISC; 12839fcb3d83SJulian Elischer 12849fcb3d83SJulian Elischer /* generate a packet of that type */ 12859fcb3d83SJulian Elischer MGETHDR(m, M_DONTWAIT, MT_DATA); 12869fcb3d83SJulian Elischer m->m_pkthdr.rcvif = NULL; 12879fcb3d83SJulian Elischer m->m_pkthdr.len = m->m_len = sizeof(*wh); 12889fcb3d83SJulian Elischer bcopy((caddr_t)wh, mtod(m, caddr_t), sizeof(*wh)); 12899fcb3d83SJulian Elischer /* Add a General error message and adjust sizes */ 12909fcb3d83SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *); 12919fcb3d83SJulian Elischer tag = wh->ph.tag; 12929fcb3d83SJulian Elischer tag->tag_type = PTT_GEN_ERR; 12939fcb3d83SJulian Elischer tag->tag_len = htons((u_int16_t)msglen); 12949fcb3d83SJulian Elischer strncpy(tag->tag_data, SIGNOFF, msglen); 12959fcb3d83SJulian Elischer m->m_pkthdr.len = (m->m_len += sizeof(*tag) + msglen); 12969fcb3d83SJulian Elischer wh->ph.length = htons(sizeof(*tag) + msglen); 1297cfbcfe62SJulian Elischer NG_SEND_DATA(error, privp->ethernet_hook, m, dummy); 12989fcb3d83SJulian Elischer } 12991e2510f8SJulian Elischer if (sp->neg) { 13004cf49a43SJulian Elischer untimeout(pppoe_ticker, hook, sp->neg->timeout_handle); 13011e2510f8SJulian Elischer if (sp->neg->m) 13021e2510f8SJulian Elischer m_freem(sp->neg->m); 13031e2510f8SJulian Elischer FREE(sp->neg, M_NETGRAPH); 13041e2510f8SJulian Elischer } 13054cf49a43SJulian Elischer FREE(sp, M_NETGRAPH); 13061e2510f8SJulian Elischer hook->private = NULL; 1307ed52f174SJulian Elischer /* work out how many session hooks there are */ 130804853d8aSJulian Elischer /* Node goes away on last session hook removal */ 130904853d8aSJulian Elischer hooks = node->numhooks; /* this one already not counted */ 131004853d8aSJulian Elischer if (privp->ethernet_hook) hooks -= 1; 1311ed52f174SJulian Elischer if (privp->debug_hook) hooks -= 1; 131204853d8aSJulian Elischer if (hooks == 0) 131304853d8aSJulian Elischer ng_rmnode(node); 13144cf49a43SJulian Elischer } 13154cf49a43SJulian Elischer if (node->numhooks == 0) 13164cf49a43SJulian Elischer ng_rmnode(node); 13174cf49a43SJulian Elischer return (0); 13184cf49a43SJulian Elischer } 13194cf49a43SJulian Elischer 13204cf49a43SJulian Elischer /* 13214cf49a43SJulian Elischer * timeouts come here. 13224cf49a43SJulian Elischer */ 13234cf49a43SJulian Elischer static void 13244cf49a43SJulian Elischer pppoe_ticker(void *arg) 13254cf49a43SJulian Elischer { 13264cf49a43SJulian Elischer int s = splnet(); 13274cf49a43SJulian Elischer hook_p hook = arg; 13284cf49a43SJulian Elischer sessp sp = hook->private; 13294cf49a43SJulian Elischer negp neg = sp->neg; 13304cf49a43SJulian Elischer int error = 0; 13314cf49a43SJulian Elischer struct mbuf *m0 = NULL; 13324cf49a43SJulian Elischer priv_p privp = hook->node->private; 13334cf49a43SJulian Elischer meta_p dummy = NULL; 13344cf49a43SJulian Elischer 13351e2510f8SJulian Elischer AAA 13364cf49a43SJulian Elischer switch(sp->state) { 13374cf49a43SJulian Elischer /* 13384cf49a43SJulian Elischer * resend the last packet, using an exponential backoff. 13394cf49a43SJulian Elischer * After a period of time, stop growing the backoff, 13404cf49a43SJulian Elischer * and either leave it, or reverst to the start. 13414cf49a43SJulian Elischer */ 13424cf49a43SJulian Elischer case PPPOE_SINIT: 13434cf49a43SJulian Elischer case PPPOE_SREQ: 13444cf49a43SJulian Elischer /* timeouts on these produce resends */ 13454cf49a43SJulian Elischer m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 13464cf49a43SJulian Elischer NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 13474cf49a43SJulian Elischer neg->timeout_handle = timeout(pppoe_ticker, 13484cf49a43SJulian Elischer hook, neg->timeout * hz); 13494cf49a43SJulian Elischer if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) { 13504cf49a43SJulian Elischer if (sp->state == PPPOE_SREQ) { 13514cf49a43SJulian Elischer /* revert to SINIT mode */ 1352b58a8a3bSJulian Elischer pppoe_start(sp); 13534cf49a43SJulian Elischer } else { 13544cf49a43SJulian Elischer neg->timeout = PPPOE_TIMEOUT_LIMIT; 13554cf49a43SJulian Elischer } 13564cf49a43SJulian Elischer } 13574cf49a43SJulian Elischer break; 13584cf49a43SJulian Elischer case PPPOE_PRIMED: 13594cf49a43SJulian Elischer case PPPOE_SOFFER: 13604cf49a43SJulian Elischer /* a timeout on these says "give up" */ 13614cf49a43SJulian Elischer ng_destroy_hook(hook); 13624cf49a43SJulian Elischer break; 13634cf49a43SJulian Elischer default: 13644cf49a43SJulian Elischer /* timeouts have no meaning in other states */ 13654cf49a43SJulian Elischer printf("pppoe: unexpected timeout\n"); 13664cf49a43SJulian Elischer } 13674cf49a43SJulian Elischer splx(s); 13684cf49a43SJulian Elischer } 13694cf49a43SJulian Elischer 13704cf49a43SJulian Elischer 13714cf49a43SJulian Elischer static void 13724cf49a43SJulian Elischer sendpacket(sessp sp) 13734cf49a43SJulian Elischer { 13744cf49a43SJulian Elischer int error = 0; 13754cf49a43SJulian Elischer struct mbuf *m0 = NULL; 13764cf49a43SJulian Elischer hook_p hook = sp->hook; 13774cf49a43SJulian Elischer negp neg = sp->neg; 13784cf49a43SJulian Elischer priv_p privp = hook->node->private; 13794cf49a43SJulian Elischer meta_p dummy = NULL; 13804cf49a43SJulian Elischer 13811e2510f8SJulian Elischer AAA 13824cf49a43SJulian Elischer switch(sp->state) { 13834cf49a43SJulian Elischer case PPPOE_LISTENING: 13844cf49a43SJulian Elischer case PPPOE_DEAD: 13854cf49a43SJulian Elischer case PPPOE_SNONE: 13864cf49a43SJulian Elischer case PPPOE_NEWCONNECTED: 13874cf49a43SJulian Elischer case PPPOE_CONNECTED: 1388b86d0a9eSJulian Elischer printf("pppoe: sendpacket: unexpected state\n"); 13894cf49a43SJulian Elischer break; 13904cf49a43SJulian Elischer 13914cf49a43SJulian Elischer case PPPOE_PRIMED: 13924cf49a43SJulian Elischer /* No packet to send, but set up the timeout */ 13934cf49a43SJulian Elischer neg->timeout_handle = timeout(pppoe_ticker, 13944cf49a43SJulian Elischer hook, PPPOE_OFFER_TIMEOUT * hz); 13954cf49a43SJulian Elischer break; 13964cf49a43SJulian Elischer 13974cf49a43SJulian Elischer case PPPOE_SOFFER: 13984cf49a43SJulian Elischer /* 13994cf49a43SJulian Elischer * send the offer but if they don't respond 14004cf49a43SJulian Elischer * in PPPOE_OFFER_TIMEOUT seconds, forget about it. 14014cf49a43SJulian Elischer */ 14024cf49a43SJulian Elischer m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 14034cf49a43SJulian Elischer NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 14044cf49a43SJulian Elischer neg->timeout_handle = timeout(pppoe_ticker, 14054cf49a43SJulian Elischer hook, PPPOE_OFFER_TIMEOUT * hz); 14064cf49a43SJulian Elischer break; 14074cf49a43SJulian Elischer 14084cf49a43SJulian Elischer case PPPOE_SINIT: 14094cf49a43SJulian Elischer case PPPOE_SREQ: 14104cf49a43SJulian Elischer m0 = m_copypacket(sp->neg->m, M_DONTWAIT); 1411b86d0a9eSJulian Elischer NG_SEND_DATA( error, privp->ethernet_hook, m0, dummy); 14124cf49a43SJulian Elischer neg->timeout_handle = timeout(pppoe_ticker, hook, hz); 14134cf49a43SJulian Elischer neg->timeout = 2; 14144cf49a43SJulian Elischer break; 14154cf49a43SJulian Elischer 14164cf49a43SJulian Elischer default: 14174cf49a43SJulian Elischer error = EINVAL; 14184cf49a43SJulian Elischer printf("pppoe: timeout: bad state\n"); 14194cf49a43SJulian Elischer } 14204cf49a43SJulian Elischer /* return (error); */ 14214cf49a43SJulian Elischer } 14224cf49a43SJulian Elischer 14234cf49a43SJulian Elischer /* 14244cf49a43SJulian Elischer * Parse an incoming packet to see if any tags should be copied to the 14254cf49a43SJulian Elischer * output packet. DOon't do any tags that are likely to have been 14264cf49a43SJulian Elischer * handles a the main state machine. 14274cf49a43SJulian Elischer */ 14284cf49a43SJulian Elischer static struct pppoe_tag* 14294cf49a43SJulian Elischer scan_tags(sessp sp, struct pppoe_hdr* ph) 14304cf49a43SJulian Elischer { 14314cf49a43SJulian Elischer char *end = (char *)next_tag(ph); 14324cf49a43SJulian Elischer char *ptn; 14334cf49a43SJulian Elischer struct pppoe_tag *pt = &ph->tag[0]; 14344cf49a43SJulian Elischer /* 14354cf49a43SJulian Elischer * Keep processing tags while a tag header will still fit. 14364cf49a43SJulian Elischer */ 14371e2510f8SJulian Elischer AAA 14384cf49a43SJulian Elischer while((char*)(pt + 1) <= end) { 14394cf49a43SJulian Elischer /* 14404cf49a43SJulian Elischer * If the tag data would go past the end of the packet, abort. 14414cf49a43SJulian Elischer */ 14424cf49a43SJulian Elischer ptn = (((char *)(pt + 1)) + ntohs(pt->tag_len)); 14434cf49a43SJulian Elischer if(ptn > end) 14444cf49a43SJulian Elischer return NULL; 14454cf49a43SJulian Elischer 14464cf49a43SJulian Elischer switch (pt->tag_type) { 14474cf49a43SJulian Elischer case PTT_RELAY_SID: 14484cf49a43SJulian Elischer insert_tag(sp, pt); 14494cf49a43SJulian Elischer break; 14504cf49a43SJulian Elischer case PTT_EOL: 14514cf49a43SJulian Elischer return NULL; 14524cf49a43SJulian Elischer case PTT_SRV_NAME: 14534cf49a43SJulian Elischer case PTT_AC_NAME: 14544cf49a43SJulian Elischer case PTT_HOST_UNIQ: 14554cf49a43SJulian Elischer case PTT_AC_COOKIE: 14564cf49a43SJulian Elischer case PTT_VENDOR: 14574cf49a43SJulian Elischer case PTT_SRV_ERR: 14584cf49a43SJulian Elischer case PTT_SYS_ERR: 14594cf49a43SJulian Elischer case PTT_GEN_ERR: 14604cf49a43SJulian Elischer break; 14614cf49a43SJulian Elischer } 14624cf49a43SJulian Elischer pt = (struct pppoe_tag*)ptn; 14634cf49a43SJulian Elischer } 14644cf49a43SJulian Elischer return NULL; 14654cf49a43SJulian Elischer } 14664cf49a43SJulian Elischer 1467b58a8a3bSJulian Elischer static int 1468b58a8a3bSJulian Elischer pppoe_send_event(sessp sp, enum cmd cmdid) 1469b58a8a3bSJulian Elischer { 1470b58a8a3bSJulian Elischer int error; 1471b58a8a3bSJulian Elischer struct ng_mesg *msg; 14728876b55dSJulian Elischer struct ngpppoe_sts *sts; 1473b58a8a3bSJulian Elischer 14741e2510f8SJulian Elischer AAA 1475b58a8a3bSJulian Elischer NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid, 14768876b55dSJulian Elischer sizeof(struct ngpppoe_sts), M_NOWAIT); 14778876b55dSJulian Elischer sts = (struct ngpppoe_sts *)msg->data; 1478b58a8a3bSJulian Elischer strncpy(sts->hook, sp->hook->name, NG_HOOKLEN + 1); 1479b58a8a3bSJulian Elischer error = ng_send_msg(sp->hook->node, msg, sp->creator, NULL); 1480b58a8a3bSJulian Elischer return (error); 1481b58a8a3bSJulian Elischer } 1482