14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer * ng_pppoe.c
3c398230bSWarner Losh */
4c398230bSWarner Losh
5c398230bSWarner Losh /*-
64cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc.
74cf49a43SJulian Elischer * All rights reserved.
84cf49a43SJulian Elischer *
94cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and
104cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or
114cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications;
124cf49a43SJulian Elischer * provided, however, that:
134cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the
144cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and
154cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle
164cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE
174cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
184cf49a43SJulian Elischer * such appears in the above copyright notice or in the software.
194cf49a43SJulian Elischer *
204cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
214cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
224cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
234cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
244cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
254cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
264cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
274cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
284cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
294cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
314cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
324cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
334cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
354cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
364cf49a43SJulian Elischer * OF SUCH DAMAGE.
374cf49a43SJulian Elischer *
38cc3bbd68SJulian Elischer * Author: Julian Elischer <julian@freebsd.org>
3974f5c6aaSJulian Elischer * $Whistle: ng_pppoe.c,v 1.10 1999/11/01 09:24:52 julian Exp $
404cf49a43SJulian Elischer */
414cf49a43SJulian Elischer
424cf49a43SJulian Elischer #include <sys/param.h>
434cf49a43SJulian Elischer #include <sys/systm.h>
444cf49a43SJulian Elischer #include <sys/kernel.h>
457762e8c6SGleb Smirnoff #include <sys/ktr.h>
464cf49a43SJulian Elischer #include <sys/mbuf.h>
474cf49a43SJulian Elischer #include <sys/malloc.h>
484cf49a43SJulian Elischer #include <sys/errno.h>
4951e805c4SKristof Provost #include <sys/epoch.h>
50*28903f39SEugene Grosbein #include <sys/socket.h>
51*28903f39SEugene Grosbein #include <sys/sysctl.h>
522e87c3ccSGleb Smirnoff #include <sys/syslog.h>
534cf49a43SJulian Elischer #include <net/ethernet.h>
54*28903f39SEugene Grosbein #include <net/if.h>
55*28903f39SEugene Grosbein #include <net/if_vlan_var.h>
56*28903f39SEugene Grosbein #include <net/vnet.h>
574cf49a43SJulian Elischer
584cf49a43SJulian Elischer #include <netgraph/ng_message.h>
594cf49a43SJulian Elischer #include <netgraph/netgraph.h>
6076a70671SBrian Somers #include <netgraph/ng_parse.h>
614cf49a43SJulian Elischer #include <netgraph/ng_pppoe.h>
62b1ba28dfSGleb Smirnoff #include <netgraph/ng_ether.h>
634cf49a43SJulian Elischer
649c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
65d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_PPPOE, "netgraph_pppoe", "netgraph pppoe node");
669c8c302fSJulian Elischer #else
679c8c302fSJulian Elischer #define M_NETGRAPH_PPPOE M_NETGRAPH
689c8c302fSJulian Elischer #endif
699c8c302fSJulian Elischer
70*28903f39SEugene Grosbein /* Some PPP protocol numbers we're interested in */
71*28903f39SEugene Grosbein #define PROT_LCP 0xc021
72*28903f39SEugene Grosbein
73da092930SArchie Cobbs #define SIGNOFF "session closed"
74da092930SArchie Cobbs
75*28903f39SEugene Grosbein VNET_DEFINE_STATIC(u_int32_t, ng_pppoe_lcp_pcp) = 0;
76*28903f39SEugene Grosbein #define V_ng_pppoe_lcp_pcp VNET(ng_pppoe_lcp_pcp)
77*28903f39SEugene Grosbein
78*28903f39SEugene Grosbein SYSCTL_NODE(_net_graph, OID_AUTO, pppoe, CTLFLAG_RW, 0, "PPPoE");
79*28903f39SEugene Grosbein SYSCTL_UINT(_net_graph_pppoe, OID_AUTO, lcp_pcp,
80*28903f39SEugene Grosbein CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ng_pppoe_lcp_pcp), 0,
81*28903f39SEugene Grosbein "Set PCP for LCP");
82*28903f39SEugene Grosbein
834cf49a43SJulian Elischer /*
844cf49a43SJulian Elischer * This section contains the netgraph method declarations for the
85bfa7e882SJulian Elischer * pppoe node. These methods define the netgraph pppoe 'type'.
864cf49a43SJulian Elischer */
874cf49a43SJulian Elischer
8874f5c6aaSJulian Elischer static ng_constructor_t ng_pppoe_constructor;
8974f5c6aaSJulian Elischer static ng_rcvmsg_t ng_pppoe_rcvmsg;
90069154d5SJulian Elischer static ng_shutdown_t ng_pppoe_shutdown;
9174f5c6aaSJulian Elischer static ng_newhook_t ng_pppoe_newhook;
92b1ba28dfSGleb Smirnoff static ng_connect_t ng_pppoe_connect;
9374f5c6aaSJulian Elischer static ng_rcvdata_t ng_pppoe_rcvdata;
9498e7b753SAlexander Motin static ng_rcvdata_t ng_pppoe_rcvdata_ether;
9598e7b753SAlexander Motin static ng_rcvdata_t ng_pppoe_rcvdata_debug;
9674f5c6aaSJulian Elischer static ng_disconnect_t ng_pppoe_disconnect;
974cf49a43SJulian Elischer
9876a70671SBrian Somers /* Parse type for struct ngpppoe_init_data */
99f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ngpppoe_init_data_type_fields[]
10076a70671SBrian Somers = NG_PPPOE_INIT_DATA_TYPE_INFO;
10127121ab1SBrian Somers static const struct ng_parse_type ngpppoe_init_data_state_type = {
10276a70671SBrian Somers &ng_parse_struct_type,
103f0184ff8SArchie Cobbs &ngpppoe_init_data_type_fields
10476a70671SBrian Somers };
10576a70671SBrian Somers
10676a70671SBrian Somers /* Parse type for struct ngpppoe_sts */
107f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_pppoe_sts_type_fields[]
10876a70671SBrian Somers = NG_PPPOE_STS_TYPE_INFO;
10976a70671SBrian Somers static const struct ng_parse_type ng_pppoe_sts_state_type = {
11076a70671SBrian Somers &ng_parse_struct_type,
111f0184ff8SArchie Cobbs &ng_pppoe_sts_type_fields
11276a70671SBrian Somers };
11376a70671SBrian Somers
11476a70671SBrian Somers /* List of commands and how to convert arguments to/from ASCII */
11576a70671SBrian Somers static const struct ng_cmdlist ng_pppoe_cmds[] = {
11676a70671SBrian Somers {
11776a70671SBrian Somers NGM_PPPOE_COOKIE,
11876a70671SBrian Somers NGM_PPPOE_CONNECT,
11976a70671SBrian Somers "pppoe_connect",
12027121ab1SBrian Somers &ngpppoe_init_data_state_type,
12176a70671SBrian Somers NULL
12276a70671SBrian Somers },
12376a70671SBrian Somers {
12476a70671SBrian Somers NGM_PPPOE_COOKIE,
12576a70671SBrian Somers NGM_PPPOE_LISTEN,
12676a70671SBrian Somers "pppoe_listen",
12727121ab1SBrian Somers &ngpppoe_init_data_state_type,
12876a70671SBrian Somers NULL
12976a70671SBrian Somers },
13076a70671SBrian Somers {
13176a70671SBrian Somers NGM_PPPOE_COOKIE,
13276a70671SBrian Somers NGM_PPPOE_OFFER,
13376a70671SBrian Somers "pppoe_offer",
13427121ab1SBrian Somers &ngpppoe_init_data_state_type,
13576a70671SBrian Somers NULL
13676a70671SBrian Somers },
13776a70671SBrian Somers {
13876a70671SBrian Somers NGM_PPPOE_COOKIE,
139859a4d16SJulian Elischer NGM_PPPOE_SERVICE,
140859a4d16SJulian Elischer "pppoe_service",
141859a4d16SJulian Elischer &ngpppoe_init_data_state_type,
142859a4d16SJulian Elischer NULL
143859a4d16SJulian Elischer },
144859a4d16SJulian Elischer {
145859a4d16SJulian Elischer NGM_PPPOE_COOKIE,
14676a70671SBrian Somers NGM_PPPOE_SUCCESS,
14776a70671SBrian Somers "pppoe_success",
14876a70671SBrian Somers &ng_pppoe_sts_state_type,
14976a70671SBrian Somers NULL
15076a70671SBrian Somers },
15176a70671SBrian Somers {
15276a70671SBrian Somers NGM_PPPOE_COOKIE,
15376a70671SBrian Somers NGM_PPPOE_FAIL,
15476a70671SBrian Somers "pppoe_fail",
15576a70671SBrian Somers &ng_pppoe_sts_state_type,
15676a70671SBrian Somers NULL
15776a70671SBrian Somers },
15876a70671SBrian Somers {
15976a70671SBrian Somers NGM_PPPOE_COOKIE,
16076a70671SBrian Somers NGM_PPPOE_CLOSE,
16176a70671SBrian Somers "pppoe_close",
16276a70671SBrian Somers &ng_pppoe_sts_state_type,
16376a70671SBrian Somers NULL
16476a70671SBrian Somers },
165fdc755d1SGleb Smirnoff {
166fdc755d1SGleb Smirnoff NGM_PPPOE_COOKIE,
167fdc755d1SGleb Smirnoff NGM_PPPOE_SETMODE,
168fdc755d1SGleb Smirnoff "pppoe_setmode",
169fdc755d1SGleb Smirnoff &ng_parse_string_type,
170fdc755d1SGleb Smirnoff NULL
171fdc755d1SGleb Smirnoff },
172fdc755d1SGleb Smirnoff {
173fdc755d1SGleb Smirnoff NGM_PPPOE_COOKIE,
174fdc755d1SGleb Smirnoff NGM_PPPOE_GETMODE,
175fdc755d1SGleb Smirnoff "pppoe_getmode",
176fdc755d1SGleb Smirnoff NULL,
177fdc755d1SGleb Smirnoff &ng_parse_string_type
178fdc755d1SGleb Smirnoff },
179b1ba28dfSGleb Smirnoff {
180b1ba28dfSGleb Smirnoff NGM_PPPOE_COOKIE,
181b1ba28dfSGleb Smirnoff NGM_PPPOE_SETENADDR,
182b1ba28dfSGleb Smirnoff "setenaddr",
183b1ba28dfSGleb Smirnoff &ng_parse_enaddr_type,
184b1ba28dfSGleb Smirnoff NULL
185b1ba28dfSGleb Smirnoff },
1865b363c09SAlexander Motin {
1875b363c09SAlexander Motin NGM_PPPOE_COOKIE,
1885b363c09SAlexander Motin NGM_PPPOE_SETMAXP,
1895b363c09SAlexander Motin "setmaxp",
1905b363c09SAlexander Motin &ng_parse_uint16_type,
1915b363c09SAlexander Motin NULL
1925b363c09SAlexander Motin },
1938be8c756SEugene Grosbein {
1948be8c756SEugene Grosbein NGM_PPPOE_COOKIE,
1958be8c756SEugene Grosbein NGM_PPPOE_SEND_HURL,
1968be8c756SEugene Grosbein "send_hurl",
1978be8c756SEugene Grosbein &ngpppoe_init_data_state_type,
1988be8c756SEugene Grosbein NULL
1998be8c756SEugene Grosbein },
2008be8c756SEugene Grosbein {
2018be8c756SEugene Grosbein NGM_PPPOE_COOKIE,
2028be8c756SEugene Grosbein NGM_PPPOE_SEND_MOTM,
2038be8c756SEugene Grosbein "send_motm",
2048be8c756SEugene Grosbein &ngpppoe_init_data_state_type,
2058be8c756SEugene Grosbein NULL
2068be8c756SEugene Grosbein },
20776a70671SBrian Somers { 0 }
20876a70671SBrian Somers };
20976a70671SBrian Somers
2104cf49a43SJulian Elischer /* Netgraph node type descriptor */
2114cf49a43SJulian Elischer static struct ng_type typestruct = {
212f8aae777SJulian Elischer .version = NG_ABI_VERSION,
213f8aae777SJulian Elischer .name = NG_PPPOE_NODE_TYPE,
214f8aae777SJulian Elischer .constructor = ng_pppoe_constructor,
215f8aae777SJulian Elischer .rcvmsg = ng_pppoe_rcvmsg,
216f8aae777SJulian Elischer .shutdown = ng_pppoe_shutdown,
217f8aae777SJulian Elischer .newhook = ng_pppoe_newhook,
218b1ba28dfSGleb Smirnoff .connect = ng_pppoe_connect,
219f8aae777SJulian Elischer .rcvdata = ng_pppoe_rcvdata,
220f8aae777SJulian Elischer .disconnect = ng_pppoe_disconnect,
221f8aae777SJulian Elischer .cmdlist = ng_pppoe_cmds,
2224cf49a43SJulian Elischer };
2238876b55dSJulian Elischer NETGRAPH_INIT(pppoe, &typestruct);
2244cf49a43SJulian Elischer
2254cf49a43SJulian Elischer /*
2264cf49a43SJulian Elischer * States for the session state machine.
2274cf49a43SJulian Elischer * These have no meaning if there is no hook attached yet.
2284cf49a43SJulian Elischer */
2294cf49a43SJulian Elischer enum state {
2304cf49a43SJulian Elischer PPPOE_SNONE=0, /* [both] Initial state */
2316faf164cSJulian Elischer PPPOE_LISTENING, /* [Daemon] Listening for discover initiation pkt */
2324cf49a43SJulian Elischer PPPOE_SINIT, /* [Client] Sent discovery initiation */
2336faf164cSJulian Elischer PPPOE_PRIMED, /* [Server] Awaiting PADI from daemon */
2346faf164cSJulian Elischer PPPOE_SOFFER, /* [Server] Sent offer message (got PADI)*/
2354cf49a43SJulian Elischer PPPOE_SREQ, /* [Client] Sent a Request */
2366faf164cSJulian Elischer PPPOE_NEWCONNECTED, /* [Server] Connection established, No data received */
2374cf49a43SJulian Elischer PPPOE_CONNECTED, /* [Both] Connection established, Data received */
2384cf49a43SJulian Elischer PPPOE_DEAD /* [Both] */
2394cf49a43SJulian Elischer };
2404cf49a43SJulian Elischer
2414cf49a43SJulian Elischer #define NUMTAGS 20 /* number of tags we are set up to work with */
2424cf49a43SJulian Elischer
2434cf49a43SJulian Elischer /*
2444cf49a43SJulian Elischer * Information we store for each hook on each node for negotiating the
2454cf49a43SJulian Elischer * session. The mbuf and cluster are freed once negotiation has completed.
2464cf49a43SJulian Elischer * The whole negotiation block is then discarded.
2474cf49a43SJulian Elischer */
2484cf49a43SJulian Elischer
2494cf49a43SJulian Elischer struct sess_neg {
2504cf49a43SJulian Elischer struct mbuf *m; /* holds cluster with last sent packet */
2514cf49a43SJulian Elischer union packet *pkt; /* points within the above cluster */
252ef237c7fSGleb Smirnoff struct callout handle; /* see timeout(9) */
2534cf49a43SJulian Elischer u_int timeout; /* 0,1,2,4,8,16 etc. seconds */
2544cf49a43SJulian Elischer u_int numtags;
255816b834fSArchie Cobbs const struct pppoe_tag *tags[NUMTAGS];
2564cf49a43SJulian Elischer u_int service_len;
2574cf49a43SJulian Elischer u_int ac_name_len;
2588be8c756SEugene Grosbein u_int host_uniq_len;
2594cf49a43SJulian Elischer
2604cf49a43SJulian Elischer struct datatag service;
2614cf49a43SJulian Elischer struct datatag ac_name;
2628be8c756SEugene Grosbein struct datatag host_uniq;
2634cf49a43SJulian Elischer };
2644cf49a43SJulian Elischer typedef struct sess_neg *negp;
2654cf49a43SJulian Elischer
2664cf49a43SJulian Elischer /*
2674cf49a43SJulian Elischer * Session information that is needed after connection.
2684cf49a43SJulian Elischer */
2692b9cf2f7SArchie Cobbs struct sess_con {
2704cf49a43SJulian Elischer hook_p hook;
2717762e8c6SGleb Smirnoff uint16_t Session_ID;
2724cf49a43SJulian Elischer enum state state;
273069154d5SJulian Elischer ng_ID_t creator; /* who to notify */
2744cf49a43SJulian Elischer struct pppoe_full_hdr pkt_hdr; /* used when connected */
2754cf49a43SJulian Elischer negp neg; /* used when negotiating */
276dda30f12SAlexander Motin LIST_ENTRY(sess_con) sessions;
2774cf49a43SJulian Elischer };
2782b9cf2f7SArchie Cobbs typedef struct sess_con *sessp;
2794cf49a43SJulian Elischer
280b2b5279bSAlexander Motin #define SESSHASHSIZE 0x0100
281b2b5279bSAlexander Motin #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
282b2b5279bSAlexander Motin
283b2b5279bSAlexander Motin struct sess_hash_entry {
284b2b5279bSAlexander Motin struct mtx mtx;
285dda30f12SAlexander Motin LIST_HEAD(hhead, sess_con) head;
286b2b5279bSAlexander Motin };
287b2b5279bSAlexander Motin
2884cf49a43SJulian Elischer /*
2894cf49a43SJulian Elischer * Information we store for each node
2904cf49a43SJulian Elischer */
2917762e8c6SGleb Smirnoff struct PPPoE {
2924cf49a43SJulian Elischer node_p node; /* back pointer to node */
2934cf49a43SJulian Elischer hook_p ethernet_hook;
2944cf49a43SJulian Elischer hook_p debug_hook;
2954cf49a43SJulian Elischer u_int packets_in; /* packets in from ethernet */
2964cf49a43SJulian Elischer u_int packets_out; /* packets out towards ethernet */
2977762e8c6SGleb Smirnoff uint32_t flags;
2981c8aa594SGleb Smirnoff #define COMPAT_3COM 0x00000001
2991c8aa594SGleb Smirnoff #define COMPAT_DLINK 0x00000002
300b1ba28dfSGleb Smirnoff struct ether_header eh;
301dda30f12SAlexander Motin LIST_HEAD(, sess_con) listeners;
302b2b5279bSAlexander Motin struct sess_hash_entry sesshash[SESSHASHSIZE];
3035b363c09SAlexander Motin struct maxptag max_payload; /* PPP-Max-Payload (RFC4638) */
3044cf49a43SJulian Elischer };
3057762e8c6SGleb Smirnoff typedef struct PPPoE *priv_p;
3064cf49a43SJulian Elischer
3074cf49a43SJulian Elischer union uniq {
3084cf49a43SJulian Elischer char bytes[sizeof(void *)];
3094cf49a43SJulian Elischer void *pointer;
3104cf49a43SJulian Elischer };
3114cf49a43SJulian Elischer
3124cf49a43SJulian Elischer #define LEAVE(x) do { error = x; goto quit; } while(0)
3134cf49a43SJulian Elischer static void pppoe_start(sessp sp);
314ef237c7fSGleb Smirnoff static void pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2);
315816b834fSArchie Cobbs static const struct pppoe_tag *scan_tags(sessp sp,
316816b834fSArchie Cobbs const struct pppoe_hdr* ph);
317b58a8a3bSJulian Elischer static int pppoe_send_event(sessp sp, enum cmd cmdid);
3184cf49a43SJulian Elischer
3194cf49a43SJulian Elischer /*************************************************************************
3204cf49a43SJulian Elischer * Some basic utilities from the Linux version with author's permission.*
3214cf49a43SJulian Elischer * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca> *
3224cf49a43SJulian Elischer ************************************************************************/
3234cf49a43SJulian Elischer
3244cf49a43SJulian Elischer /*
3254cf49a43SJulian Elischer * Return the location where the next tag can be put
3264cf49a43SJulian Elischer */
327816b834fSArchie Cobbs static __inline const struct pppoe_tag*
next_tag(const struct pppoe_hdr * ph)328816b834fSArchie Cobbs next_tag(const struct pppoe_hdr* ph)
3294cf49a43SJulian Elischer {
33060d234c5SEd Schouten return (const struct pppoe_tag*)(((const char*)(ph + 1))
331816b834fSArchie Cobbs + ntohs(ph->length));
3324cf49a43SJulian Elischer }
3334cf49a43SJulian Elischer
3344cf49a43SJulian Elischer /*
3357762e8c6SGleb Smirnoff * Look for a tag of a specific type.
3367762e8c6SGleb Smirnoff * Don't trust any length the other end says,
3374cf49a43SJulian Elischer * but assume we already sanity checked ph->length.
3384cf49a43SJulian Elischer */
339816b834fSArchie Cobbs static const struct pppoe_tag*
get_tag(const struct pppoe_hdr * ph,uint16_t idx)3407762e8c6SGleb Smirnoff get_tag(const struct pppoe_hdr* ph, uint16_t idx)
3414cf49a43SJulian Elischer {
342816b834fSArchie Cobbs const char *const end = (const char *)next_tag(ph);
34360d234c5SEd Schouten const struct pppoe_tag *pt = (const void *)(ph + 1);
3447762e8c6SGleb Smirnoff const char *ptn;
3457762e8c6SGleb Smirnoff
3464cf49a43SJulian Elischer /*
3474cf49a43SJulian Elischer * Keep processing tags while a tag header will still fit.
3484cf49a43SJulian Elischer */
349816b834fSArchie Cobbs while((const char*)(pt + 1) <= end) {
3504cf49a43SJulian Elischer /*
3514cf49a43SJulian Elischer * If the tag data would go past the end of the packet, abort.
3524cf49a43SJulian Elischer */
353816b834fSArchie Cobbs ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
3547762e8c6SGleb Smirnoff if (ptn > end) {
3557762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: invalid length for tag %d",
3567762e8c6SGleb Smirnoff __func__, idx);
3577762e8c6SGleb Smirnoff return (NULL);
3587762e8c6SGleb Smirnoff }
3597762e8c6SGleb Smirnoff if (pt->tag_type == idx) {
3607762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: found tag %d", __func__, idx);
3617762e8c6SGleb Smirnoff return (pt);
3627762e8c6SGleb Smirnoff }
3634cf49a43SJulian Elischer
364816b834fSArchie Cobbs pt = (const struct pppoe_tag*)ptn;
3654cf49a43SJulian Elischer }
3667762e8c6SGleb Smirnoff
3677762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: not found tag %d", __func__, idx);
3687762e8c6SGleb Smirnoff return (NULL);
3694cf49a43SJulian Elischer }
3704cf49a43SJulian Elischer
3714cf49a43SJulian Elischer /**************************************************************************
3727762e8c6SGleb Smirnoff * Inlines to initialise or add tags to a session's tag list.
3734cf49a43SJulian Elischer **************************************************************************/
3744cf49a43SJulian Elischer /*
3757762e8c6SGleb Smirnoff * Initialise the session's tag list.
3764cf49a43SJulian Elischer */
3774cf49a43SJulian Elischer static void
init_tags(sessp sp)3784cf49a43SJulian Elischer init_tags(sessp sp)
3794cf49a43SJulian Elischer {
3807762e8c6SGleb Smirnoff KASSERT(sp->neg != NULL, ("%s: no neg", __func__));
3814cf49a43SJulian Elischer sp->neg->numtags = 0;
3824cf49a43SJulian Elischer }
3834cf49a43SJulian Elischer
3844cf49a43SJulian Elischer static void
insert_tag(sessp sp,const struct pppoe_tag * tp)385816b834fSArchie Cobbs insert_tag(sessp sp, const struct pppoe_tag *tp)
3864cf49a43SJulian Elischer {
3877762e8c6SGleb Smirnoff negp neg = sp->neg;
3884cf49a43SJulian Elischer int i;
3894cf49a43SJulian Elischer
3907762e8c6SGleb Smirnoff KASSERT(neg != NULL, ("%s: no neg", __func__));
3914cf49a43SJulian Elischer if ((i = neg->numtags++) < NUMTAGS) {
3924cf49a43SJulian Elischer neg->tags[i] = tp;
3934cf49a43SJulian Elischer } else {
3942e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe: asked to add too many tags to "
3952e87c3ccSGleb Smirnoff "packet\n");
39612f035e0SJulian Elischer neg->numtags--;
3974cf49a43SJulian Elischer }
3984cf49a43SJulian Elischer }
3994cf49a43SJulian Elischer
4004cf49a43SJulian Elischer /*
4014cf49a43SJulian Elischer * Make up a packet, using the tags filled out for the session.
4024cf49a43SJulian Elischer *
4034cf49a43SJulian Elischer * Assume that the actual pppoe header and ethernet header
4044cf49a43SJulian Elischer * are filled out externally to this routine.
4054cf49a43SJulian Elischer * Also assume that neg->wh points to the correct
4064cf49a43SJulian Elischer * location at the front of the buffer space.
4074cf49a43SJulian Elischer */
4084cf49a43SJulian Elischer static void
make_packet(sessp sp)4094cf49a43SJulian Elischer make_packet(sessp sp) {
4104cf49a43SJulian Elischer struct pppoe_full_hdr *wh = &sp->neg->pkt->pkt_header;
411816b834fSArchie Cobbs const struct pppoe_tag **tag;
4124cf49a43SJulian Elischer char *dp;
4134cf49a43SJulian Elischer int count;
4144cf49a43SJulian Elischer int tlen;
4157762e8c6SGleb Smirnoff uint16_t length = 0;
4164cf49a43SJulian Elischer
4177762e8c6SGleb Smirnoff KASSERT((sp->neg != NULL) && (sp->neg->m != NULL),
4182e87c3ccSGleb Smirnoff ("%s: called from wrong state", __func__));
4197762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
4207762e8c6SGleb Smirnoff
42160d234c5SEd Schouten dp = (char *)(&wh->ph + 1);
4224cf49a43SJulian Elischer for (count = 0, tag = sp->neg->tags;
4234cf49a43SJulian Elischer ((count < sp->neg->numtags) && (count < NUMTAGS));
4244cf49a43SJulian Elischer tag++, count++) {
4254cf49a43SJulian Elischer tlen = ntohs((*tag)->tag_len) + sizeof(**tag);
4264cf49a43SJulian Elischer if ((length + tlen) > (ETHER_MAX_LEN - 4 - sizeof(*wh))) {
4272e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe: tags too long\n");
4284cf49a43SJulian Elischer sp->neg->numtags = count;
4294cf49a43SJulian Elischer break; /* XXX chop off what's too long */
4304cf49a43SJulian Elischer }
431816b834fSArchie Cobbs bcopy(*tag, (char *)dp, tlen);
4324cf49a43SJulian Elischer length += tlen;
4334cf49a43SJulian Elischer dp += tlen;
4344cf49a43SJulian Elischer }
4354cf49a43SJulian Elischer wh->ph.length = htons(length);
4364cf49a43SJulian Elischer sp->neg->m->m_len = length + sizeof(*wh);
4374cf49a43SJulian Elischer sp->neg->m->m_pkthdr.len = length + sizeof(*wh);
4384cf49a43SJulian Elischer }
4394cf49a43SJulian Elischer
4404cf49a43SJulian Elischer /**************************************************************************
44168b789b2SGleb Smirnoff * Routines to match a service. *
4424cf49a43SJulian Elischer **************************************************************************/
44368b789b2SGleb Smirnoff
4444cf49a43SJulian Elischer /*
4454cf49a43SJulian Elischer * Find a hook that has a service string that matches that
44668b789b2SGleb Smirnoff * we are seeking. For now use a simple string.
4474cf49a43SJulian Elischer * In the future we may need something like regexp().
44868b789b2SGleb Smirnoff *
44968b789b2SGleb Smirnoff * Null string is a wildcard (ANY service), according to RFC2516.
45068b789b2SGleb Smirnoff * And historical FreeBSD wildcard is also "*".
4514cf49a43SJulian Elischer */
4529088fa05SBrian Somers
4534cf49a43SJulian Elischer static hook_p
pppoe_match_svc(node_p node,const struct pppoe_tag * tag)45468b789b2SGleb Smirnoff pppoe_match_svc(node_p node, const struct pppoe_tag *tag)
4554cf49a43SJulian Elischer {
456dda30f12SAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
457dda30f12SAlexander Motin sessp sp;
4584cf49a43SJulian Elischer
459dda30f12SAlexander Motin LIST_FOREACH(sp, &privp->listeners, sessions) {
460dda30f12SAlexander Motin negp neg = sp->neg;
4614cf49a43SJulian Elischer
46268b789b2SGleb Smirnoff /* Empty Service-Name matches any service. */
46368b789b2SGleb Smirnoff if (neg->service_len == 0)
46468b789b2SGleb Smirnoff break;
46568b789b2SGleb Smirnoff
46668b789b2SGleb Smirnoff /* Special case for a blank or "*" service name (wildcard). */
46768b789b2SGleb Smirnoff if (neg->service_len == 1 && neg->service.data[0] == '*')
46868b789b2SGleb Smirnoff break;
4694cf49a43SJulian Elischer
4704cf49a43SJulian Elischer /* If the lengths don't match, that aint it. */
47168b789b2SGleb Smirnoff if (neg->service_len != ntohs(tag->tag_len))
4724cf49a43SJulian Elischer continue;
4734cf49a43SJulian Elischer
47460d234c5SEd Schouten if (strncmp((const char *)(tag + 1), neg->service.data,
47568b789b2SGleb Smirnoff ntohs(tag->tag_len)) == 0)
4764cf49a43SJulian Elischer break;
4774cf49a43SJulian Elischer }
478dda30f12SAlexander Motin CTR3(KTR_NET, "%20s: matched %p for %s", __func__,
47960d234c5SEd Schouten sp?sp->hook:NULL, (const char *)(tag + 1));
4807762e8c6SGleb Smirnoff
481dda30f12SAlexander Motin return (sp?sp->hook:NULL);
48268b789b2SGleb Smirnoff }
48368b789b2SGleb Smirnoff
48468b789b2SGleb Smirnoff /*
48568b789b2SGleb Smirnoff * Broadcast the PADI packet in m0 to all listening hooks.
48668b789b2SGleb Smirnoff * This routine is called when a PADI with empty Service-Name
48768b789b2SGleb Smirnoff * tag is received. Client should receive PADOs with all
48868b789b2SGleb Smirnoff * available services.
48968b789b2SGleb Smirnoff */
49068b789b2SGleb Smirnoff static int
pppoe_broadcast_padi(node_p node,struct mbuf * m0)49168b789b2SGleb Smirnoff pppoe_broadcast_padi(node_p node, struct mbuf *m0)
49268b789b2SGleb Smirnoff {
493dda30f12SAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
494dda30f12SAlexander Motin sessp sp;
49568b789b2SGleb Smirnoff int error = 0;
49668b789b2SGleb Smirnoff
497dda30f12SAlexander Motin LIST_FOREACH(sp, &privp->listeners, sessions) {
49868b789b2SGleb Smirnoff struct mbuf *m;
49968b789b2SGleb Smirnoff
500eb1b1807SGleb Smirnoff m = m_dup(m0, M_NOWAIT);
50168b789b2SGleb Smirnoff if (m == NULL)
50268b789b2SGleb Smirnoff return (ENOMEM);
503dda30f12SAlexander Motin NG_SEND_DATA_ONLY(error, sp->hook, m);
50468b789b2SGleb Smirnoff if (error)
50568b789b2SGleb Smirnoff return (error);
50668b789b2SGleb Smirnoff }
50768b789b2SGleb Smirnoff
50868b789b2SGleb Smirnoff return (0);
50968b789b2SGleb Smirnoff }
51068b789b2SGleb Smirnoff
51168b789b2SGleb Smirnoff /*
51268b789b2SGleb Smirnoff * Find a hook, which name equals to given service.
51368b789b2SGleb Smirnoff */
51468b789b2SGleb Smirnoff static hook_p
pppoe_find_svc(node_p node,const char * svc_name,int svc_len)51568b789b2SGleb Smirnoff pppoe_find_svc(node_p node, const char *svc_name, int svc_len)
51668b789b2SGleb Smirnoff {
517dda30f12SAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
518dda30f12SAlexander Motin sessp sp;
51968b789b2SGleb Smirnoff
520dda30f12SAlexander Motin LIST_FOREACH(sp, &privp->listeners, sessions) {
521dda30f12SAlexander Motin negp neg = sp->neg;
52268b789b2SGleb Smirnoff
52368b789b2SGleb Smirnoff if (neg->service_len == svc_len &&
52497b4f83bSAlexander Motin strncmp(svc_name, neg->service.data, svc_len) == 0)
525dda30f12SAlexander Motin return (sp->hook);
52668b789b2SGleb Smirnoff }
52768b789b2SGleb Smirnoff
52868b789b2SGleb Smirnoff return (NULL);
5294cf49a43SJulian Elischer }
5307762e8c6SGleb Smirnoff
5314cf49a43SJulian Elischer /**************************************************************************
532b2b5279bSAlexander Motin * Routines to find a particular session that matches an incoming packet. *
5334cf49a43SJulian Elischer **************************************************************************/
534bd500dabSAlexander Motin /* Find free session and add to hash. */
535bd500dabSAlexander Motin static uint16_t
pppoe_getnewsession(sessp sp)536bd500dabSAlexander Motin pppoe_getnewsession(sessp sp)
537bd500dabSAlexander Motin {
538bd500dabSAlexander Motin const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
539bd500dabSAlexander Motin static uint16_t pppoe_sid = 1;
540bd500dabSAlexander Motin sessp tsp;
541bd500dabSAlexander Motin uint16_t val, hash;
542bd500dabSAlexander Motin
543bd500dabSAlexander Motin restart:
544bd500dabSAlexander Motin /* Atomicity is not needed here as value will be checked. */
545bd500dabSAlexander Motin val = pppoe_sid++;
546bd500dabSAlexander Motin /* Spec says 0xFFFF is reserved, also don't use 0x0000. */
547bd500dabSAlexander Motin if (val == 0xffff || val == 0x0000)
548bd500dabSAlexander Motin val = pppoe_sid = 1;
549bd500dabSAlexander Motin
550bd500dabSAlexander Motin /* Check it isn't already in use. */
551bd500dabSAlexander Motin hash = SESSHASH(val);
552bd500dabSAlexander Motin mtx_lock(&privp->sesshash[hash].mtx);
553dda30f12SAlexander Motin LIST_FOREACH(tsp, &privp->sesshash[hash].head, sessions) {
554bd500dabSAlexander Motin if (tsp->Session_ID == val)
555bd500dabSAlexander Motin break;
556bd500dabSAlexander Motin }
557bd500dabSAlexander Motin if (!tsp) {
558bd500dabSAlexander Motin sp->Session_ID = val;
559dda30f12SAlexander Motin LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
560bd500dabSAlexander Motin }
561bd500dabSAlexander Motin mtx_unlock(&privp->sesshash[hash].mtx);
562bd500dabSAlexander Motin if (tsp)
563bd500dabSAlexander Motin goto restart;
564bd500dabSAlexander Motin
565bd500dabSAlexander Motin CTR2(KTR_NET, "%20s: new sid %d", __func__, val);
566bd500dabSAlexander Motin
567bd500dabSAlexander Motin return (val);
568bd500dabSAlexander Motin }
569bd500dabSAlexander Motin
570b2b5279bSAlexander Motin /* Add specified session to hash. */
571b2b5279bSAlexander Motin static void
pppoe_addsession(sessp sp)572b2b5279bSAlexander Motin pppoe_addsession(sessp sp)
5734cf49a43SJulian Elischer {
574b2b5279bSAlexander Motin const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
575b2b5279bSAlexander Motin uint16_t hash = SESSHASH(sp->Session_ID);
576b2b5279bSAlexander Motin
577b2b5279bSAlexander Motin mtx_lock(&privp->sesshash[hash].mtx);
578dda30f12SAlexander Motin LIST_INSERT_HEAD(&privp->sesshash[hash].head, sp, sessions);
579b2b5279bSAlexander Motin mtx_unlock(&privp->sesshash[hash].mtx);
580b2b5279bSAlexander Motin }
581b2b5279bSAlexander Motin
582b2b5279bSAlexander Motin /* Delete specified session from hash. */
583b2b5279bSAlexander Motin static void
pppoe_delsession(sessp sp)584b2b5279bSAlexander Motin pppoe_delsession(sessp sp)
585b2b5279bSAlexander Motin {
586b2b5279bSAlexander Motin const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(sp->hook));
587b2b5279bSAlexander Motin uint16_t hash = SESSHASH(sp->Session_ID);
588b2b5279bSAlexander Motin
589b2b5279bSAlexander Motin mtx_lock(&privp->sesshash[hash].mtx);
590dda30f12SAlexander Motin LIST_REMOVE(sp, sessions);
591b2b5279bSAlexander Motin mtx_unlock(&privp->sesshash[hash].mtx);
592b2b5279bSAlexander Motin }
593b2b5279bSAlexander Motin
594b2b5279bSAlexander Motin /* Find matching peer/session combination. */
595b2b5279bSAlexander Motin static sessp
pppoe_findsession(priv_p privp,const struct pppoe_full_hdr * wh)596b2b5279bSAlexander Motin pppoe_findsession(priv_p privp, const struct pppoe_full_hdr *wh)
597b2b5279bSAlexander Motin {
5987762e8c6SGleb Smirnoff uint16_t session = ntohs(wh->ph.sid);
599b2b5279bSAlexander Motin uint16_t hash = SESSHASH(session);
600b2b5279bSAlexander Motin sessp sp = NULL;
6014cf49a43SJulian Elischer
602b2b5279bSAlexander Motin mtx_lock(&privp->sesshash[hash].mtx);
603dda30f12SAlexander Motin LIST_FOREACH(sp, &privp->sesshash[hash].head, sessions) {
60499f4de90SAlexander Motin if (sp->Session_ID == session &&
60599f4de90SAlexander Motin bcmp(sp->pkt_hdr.eh.ether_dhost,
60699f4de90SAlexander Motin wh->eh.ether_shost, ETHER_ADDR_LEN) == 0) {
6074cf49a43SJulian Elischer break;
6084cf49a43SJulian Elischer }
6094cf49a43SJulian Elischer }
610b2b5279bSAlexander Motin mtx_unlock(&privp->sesshash[hash].mtx);
611b1a3358bSAlexander Motin CTR3(KTR_NET, "%20s: matched %p for %d", __func__, sp?sp->hook:NULL,
612b1a3358bSAlexander Motin session);
6137762e8c6SGleb Smirnoff
614b2b5279bSAlexander Motin return (sp);
6154cf49a43SJulian Elischer }
6164cf49a43SJulian Elischer
6174cf49a43SJulian Elischer static hook_p
pppoe_finduniq(node_p node,const struct pppoe_tag * tag)618816b834fSArchie Cobbs pppoe_finduniq(node_p node, const struct pppoe_tag *tag)
6194cf49a43SJulian Elischer {
6207762e8c6SGleb Smirnoff hook_p hook = NULL;
6218be8c756SEugene Grosbein sessp sp;
6224cf49a43SJulian Elischer
6237762e8c6SGleb Smirnoff /* Cycle through all known hooks. */
62430400f03SJulian Elischer LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
62599f4de90SAlexander Motin /* Skip any nonsession hook. */
62699f4de90SAlexander Motin if (NG_HOOK_PRIVATE(hook) == NULL)
6274cf49a43SJulian Elischer continue;
6288be8c756SEugene Grosbein sp = NG_HOOK_PRIVATE(hook);
6298be8c756SEugene Grosbein /* Skip already connected sessions. */
6308be8c756SEugene Grosbein if (sp->neg == NULL)
6318be8c756SEugene Grosbein continue;
6328be8c756SEugene Grosbein if (sp->neg->host_uniq_len == ntohs(tag->tag_len) &&
6338be8c756SEugene Grosbein bcmp(sp->neg->host_uniq.data, (const char *)(tag + 1),
6348be8c756SEugene Grosbein sp->neg->host_uniq_len) == 0)
6354cf49a43SJulian Elischer break;
6364cf49a43SJulian Elischer }
6378be8c756SEugene Grosbein CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, sp);
6388be8c756SEugene Grosbein
6398be8c756SEugene Grosbein return (hook);
6408be8c756SEugene Grosbein }
6418be8c756SEugene Grosbein
6428be8c756SEugene Grosbein static hook_p
pppoe_findcookie(node_p node,const struct pppoe_tag * tag)6438be8c756SEugene Grosbein pppoe_findcookie(node_p node, const struct pppoe_tag *tag)
6448be8c756SEugene Grosbein {
6458be8c756SEugene Grosbein hook_p hook = NULL;
6468be8c756SEugene Grosbein union uniq cookie;
6478be8c756SEugene Grosbein
6488be8c756SEugene Grosbein bcopy(tag + 1, cookie.bytes, sizeof(void *));
6498be8c756SEugene Grosbein /* Cycle through all known hooks. */
6508be8c756SEugene Grosbein LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
6518be8c756SEugene Grosbein /* Skip any nonsession hook. */
6528be8c756SEugene Grosbein if (NG_HOOK_PRIVATE(hook) == NULL)
6538be8c756SEugene Grosbein continue;
6548be8c756SEugene Grosbein if (cookie.pointer == NG_HOOK_PRIVATE(hook))
6558be8c756SEugene Grosbein break;
6568be8c756SEugene Grosbein }
6578be8c756SEugene Grosbein CTR3(KTR_NET, "%20s: matched %p for %p", __func__, hook, cookie.pointer);
6587762e8c6SGleb Smirnoff
6594cf49a43SJulian Elischer return (hook);
6604cf49a43SJulian Elischer }
6614cf49a43SJulian Elischer
6624cf49a43SJulian Elischer /**************************************************************************
6637762e8c6SGleb Smirnoff * Start of Netgraph entrypoints. *
6644cf49a43SJulian Elischer **************************************************************************/
6654cf49a43SJulian Elischer
6664cf49a43SJulian Elischer /*
6677762e8c6SGleb Smirnoff * Allocate the private data structure and link it with node.
6684cf49a43SJulian Elischer */
6694cf49a43SJulian Elischer static int
ng_pppoe_constructor(node_p node)670069154d5SJulian Elischer ng_pppoe_constructor(node_p node)
6714cf49a43SJulian Elischer {
6727762e8c6SGleb Smirnoff priv_p privp;
673b2b5279bSAlexander Motin int i;
6744cf49a43SJulian Elischer
6757762e8c6SGleb Smirnoff /* Initialize private descriptor. */
676674d86bfSGleb Smirnoff privp = malloc(sizeof(*privp), M_NETGRAPH_PPPOE, M_WAITOK | M_ZERO);
6774cf49a43SJulian Elischer
6787762e8c6SGleb Smirnoff /* Link structs together; this counts as our one reference to *node. */
6797762e8c6SGleb Smirnoff NG_NODE_SET_PRIVATE(node, privp);
6807762e8c6SGleb Smirnoff privp->node = node;
681fdc755d1SGleb Smirnoff
6821c8aa594SGleb Smirnoff /* Initialize to standard mode. */
683b1ba28dfSGleb Smirnoff memset(&privp->eh.ether_dhost, 0xff, ETHER_ADDR_LEN);
684b1ba28dfSGleb Smirnoff privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
6857762e8c6SGleb Smirnoff
686dda30f12SAlexander Motin LIST_INIT(&privp->listeners);
687b2b5279bSAlexander Motin for (i = 0; i < SESSHASHSIZE; i++) {
688b2b5279bSAlexander Motin mtx_init(&privp->sesshash[i].mtx, "PPPoE hash mutex", NULL, MTX_DEF);
689dda30f12SAlexander Motin LIST_INIT(&privp->sesshash[i].head);
690b2b5279bSAlexander Motin }
691b2b5279bSAlexander Motin
6927762e8c6SGleb Smirnoff CTR3(KTR_NET, "%20s: created node [%x] (%p)",
6937762e8c6SGleb Smirnoff __func__, node->nd_ID, node);
694fdc755d1SGleb Smirnoff
6954cf49a43SJulian Elischer return (0);
6964cf49a43SJulian Elischer }
6974cf49a43SJulian Elischer
6984cf49a43SJulian Elischer /*
6994cf49a43SJulian Elischer * Give our ok for a hook to be added...
7004cf49a43SJulian Elischer * point the hook's private info to the hook structure.
7014cf49a43SJulian Elischer *
7024cf49a43SJulian Elischer * The following hook names are special:
7037762e8c6SGleb Smirnoff * "ethernet": the hook that should be connected to a NIC.
7047762e8c6SGleb Smirnoff * "debug": copies of data sent out here (when I write the code).
705859a4d16SJulian Elischer * All other hook names need only be unique. (the framework checks this).
7064cf49a43SJulian Elischer */
7074cf49a43SJulian Elischer static int
ng_pppoe_newhook(node_p node,hook_p hook,const char * name)7088876b55dSJulian Elischer ng_pppoe_newhook(node_p node, hook_p hook, const char *name)
7094cf49a43SJulian Elischer {
71030400f03SJulian Elischer const priv_p privp = NG_NODE_PRIVATE(node);
7114cf49a43SJulian Elischer sessp sp;
7124cf49a43SJulian Elischer
7134cf49a43SJulian Elischer if (strcmp(name, NG_PPPOE_HOOK_ETHERNET) == 0) {
7144cf49a43SJulian Elischer privp->ethernet_hook = hook;
71598e7b753SAlexander Motin NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_ether);
7164cf49a43SJulian Elischer } else if (strcmp(name, NG_PPPOE_HOOK_DEBUG) == 0) {
7174cf49a43SJulian Elischer privp->debug_hook = hook;
71898e7b753SAlexander Motin NG_HOOK_SET_RCVDATA(hook, ng_pppoe_rcvdata_debug);
7194cf49a43SJulian Elischer } else {
7204cf49a43SJulian Elischer /*
7214cf49a43SJulian Elischer * Any other unique name is OK.
7224cf49a43SJulian Elischer * The infrastructure has already checked that it's unique,
7234cf49a43SJulian Elischer * so just allocate it and hook it in.
7244cf49a43SJulian Elischer */
7257762e8c6SGleb Smirnoff sp = malloc(sizeof(*sp), M_NETGRAPH_PPPOE, M_NOWAIT | M_ZERO);
7267762e8c6SGleb Smirnoff if (sp == NULL)
7274cf49a43SJulian Elischer return (ENOMEM);
7284cf49a43SJulian Elischer
72930400f03SJulian Elischer NG_HOOK_SET_PRIVATE(hook, sp);
7304cf49a43SJulian Elischer sp->hook = hook;
7314cf49a43SJulian Elischer }
7327762e8c6SGleb Smirnoff CTR5(KTR_NET, "%20s: node [%x] (%p) connected hook %s (%p)",
7337762e8c6SGleb Smirnoff __func__, node->nd_ID, node, name, hook);
7347762e8c6SGleb Smirnoff
7354cf49a43SJulian Elischer return(0);
7364cf49a43SJulian Elischer }
7374cf49a43SJulian Elischer
7384cf49a43SJulian Elischer /*
739b1ba28dfSGleb Smirnoff * Hook has been added successfully. Request the MAC address of
740b1ba28dfSGleb Smirnoff * the underlying Ethernet node.
741b1ba28dfSGleb Smirnoff */
742b1ba28dfSGleb Smirnoff static int
ng_pppoe_connect(hook_p hook)743b1ba28dfSGleb Smirnoff ng_pppoe_connect(hook_p hook)
744b1ba28dfSGleb Smirnoff {
745b1ba28dfSGleb Smirnoff const priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
746b1ba28dfSGleb Smirnoff struct ng_mesg *msg;
747f366efa9SGleb Smirnoff int error;
748b1ba28dfSGleb Smirnoff
749b1ba28dfSGleb Smirnoff if (hook != privp->ethernet_hook)
750b1ba28dfSGleb Smirnoff return (0);
751b1ba28dfSGleb Smirnoff
752b1ba28dfSGleb Smirnoff /*
753b1ba28dfSGleb Smirnoff * If this is Ethernet hook, then request MAC address
754b1ba28dfSGleb Smirnoff * from our downstream.
755b1ba28dfSGleb Smirnoff */
756b1ba28dfSGleb Smirnoff NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_ENADDR, 0, M_NOWAIT);
757b1ba28dfSGleb Smirnoff if (msg == NULL)
758b1ba28dfSGleb Smirnoff return (ENOBUFS);
759b1ba28dfSGleb Smirnoff
760b1ba28dfSGleb Smirnoff /*
761b1ba28dfSGleb Smirnoff * Our hook and peer hook have HK_INVALID flag set,
762b1ba28dfSGleb Smirnoff * so we can't use NG_SEND_MSG_HOOK() macro here.
763b1ba28dfSGleb Smirnoff */
764b1ba28dfSGleb Smirnoff NG_SEND_MSG_ID(error, privp->node, msg,
765b1ba28dfSGleb Smirnoff NG_NODE_ID(NG_PEER_NODE(privp->ethernet_hook)),
766b1ba28dfSGleb Smirnoff NG_NODE_ID(privp->node));
767b1ba28dfSGleb Smirnoff
768b1ba28dfSGleb Smirnoff return (error);
769b1ba28dfSGleb Smirnoff }
770b1ba28dfSGleb Smirnoff /*
7714cf49a43SJulian Elischer * Get a netgraph control message.
7724cf49a43SJulian Elischer * Check it is one we understand. If needed, send a response.
7734cf49a43SJulian Elischer * We sometimes save the address for an async action later.
7744cf49a43SJulian Elischer * Always free the message.
7754cf49a43SJulian Elischer */
7764cf49a43SJulian Elischer static int
ng_pppoe_rcvmsg(node_p node,item_p item,hook_p lasthook)777069154d5SJulian Elischer ng_pppoe_rcvmsg(node_p node, item_p item, hook_p lasthook)
7784cf49a43SJulian Elischer {
77951e805c4SKristof Provost struct epoch_tracker et;
78030400f03SJulian Elischer priv_p privp = NG_NODE_PRIVATE(node);
7818876b55dSJulian Elischer struct ngpppoe_init_data *ourmsg = NULL;
7824cf49a43SJulian Elischer struct ng_mesg *resp = NULL;
7834cf49a43SJulian Elischer int error = 0;
7844cf49a43SJulian Elischer hook_p hook = NULL;
7854cf49a43SJulian Elischer sessp sp = NULL;
7864cf49a43SJulian Elischer negp neg = NULL;
787069154d5SJulian Elischer struct ng_mesg *msg;
7884cf49a43SJulian Elischer
789069154d5SJulian Elischer NGI_GET_MSG(item, msg);
7907762e8c6SGleb Smirnoff CTR5(KTR_NET, "%20s: node [%x] (%p) got message %d with cookie %d",
7917762e8c6SGleb Smirnoff __func__, node->nd_ID, node, msg->header.cmd,
7927762e8c6SGleb Smirnoff msg->header.typecookie);
7937762e8c6SGleb Smirnoff
7947762e8c6SGleb Smirnoff /* Deal with message according to cookie and command. */
7954cf49a43SJulian Elischer switch (msg->header.typecookie) {
7964cf49a43SJulian Elischer case NGM_PPPOE_COOKIE:
7974cf49a43SJulian Elischer switch (msg->header.cmd) {
7984cf49a43SJulian Elischer case NGM_PPPOE_CONNECT:
7994cf49a43SJulian Elischer case NGM_PPPOE_LISTEN:
8004cf49a43SJulian Elischer case NGM_PPPOE_OFFER:
801859a4d16SJulian Elischer case NGM_PPPOE_SERVICE:
8028be8c756SEugene Grosbein case NGM_PPPOE_SEND_HURL:
8038be8c756SEugene Grosbein case NGM_PPPOE_SEND_MOTM:
80427121ab1SBrian Somers ourmsg = (struct ngpppoe_init_data *)msg->data;
80527121ab1SBrian Somers if (msg->header.arglen < sizeof(*ourmsg)) {
8062e87c3ccSGleb Smirnoff log(LOG_ERR, "ng_pppoe[%x]: init data too "
8072e87c3ccSGleb Smirnoff "small\n", node->nd_ID);
8084cf49a43SJulian Elischer LEAVE(EMSGSIZE);
8094cf49a43SJulian Elischer }
8108be8c756SEugene Grosbein if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
8118be8c756SEugene Grosbein msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
8128be8c756SEugene Grosbein if (msg->header.arglen - sizeof(*ourmsg) >
8138be8c756SEugene Grosbein PPPOE_PADM_VALUE_SIZE) {
8148be8c756SEugene Grosbein log(LOG_ERR, "ng_pppoe[%x]: message "
8158be8c756SEugene Grosbein "too big\n", node->nd_ID);
8168be8c756SEugene Grosbein LEAVE(EMSGSIZE);
8178be8c756SEugene Grosbein }
8188be8c756SEugene Grosbein } else {
81976a70671SBrian Somers if (msg->header.arglen - sizeof(*ourmsg) >
82076a70671SBrian Somers PPPOE_SERVICE_NAME_SIZE) {
8212e87c3ccSGleb Smirnoff log(LOG_ERR, "ng_pppoe[%x]: service name "
8222e87c3ccSGleb Smirnoff "too big\n", node->nd_ID);
8234cf49a43SJulian Elischer LEAVE(EMSGSIZE);
8244cf49a43SJulian Elischer }
8258be8c756SEugene Grosbein }
82627121ab1SBrian Somers if (msg->header.arglen - sizeof(*ourmsg) <
82727121ab1SBrian Somers ourmsg->data_len) {
8282e87c3ccSGleb Smirnoff log(LOG_ERR, "ng_pppoe[%x]: init data has bad "
8292e87c3ccSGleb Smirnoff "length, %d should be %zd\n", node->nd_ID,
8302e87c3ccSGleb Smirnoff ourmsg->data_len,
83127121ab1SBrian Somers msg->header.arglen - sizeof (*ourmsg));
83276a70671SBrian Somers LEAVE(EMSGSIZE);
83376a70671SBrian Somers }
83476a70671SBrian Somers
8357762e8c6SGleb Smirnoff /* Make sure strcmp will terminate safely. */
8364cf49a43SJulian Elischer ourmsg->hook[sizeof(ourmsg->hook) - 1] = '\0';
8374cf49a43SJulian Elischer
838dda30f12SAlexander Motin /* Find hook by name. */
839dda30f12SAlexander Motin hook = ng_findhook(node, ourmsg->hook);
8407762e8c6SGleb Smirnoff if (hook == NULL)
8414cf49a43SJulian Elischer LEAVE(ENOENT);
8427762e8c6SGleb Smirnoff
84330400f03SJulian Elischer sp = NG_HOOK_PRIVATE(hook);
84499f4de90SAlexander Motin if (sp == NULL)
84599f4de90SAlexander Motin LEAVE(EINVAL);
84699f4de90SAlexander Motin
8479088fa05SBrian Somers if (msg->header.cmd == NGM_PPPOE_LISTEN) {
8489088fa05SBrian Somers /*
8499088fa05SBrian Somers * Ensure we aren't already listening for this
8509088fa05SBrian Somers * service.
8519088fa05SBrian Somers */
85268b789b2SGleb Smirnoff if (pppoe_find_svc(node, ourmsg->data,
85368b789b2SGleb Smirnoff ourmsg->data_len) != NULL)
8549088fa05SBrian Somers LEAVE(EEXIST);
8559088fa05SBrian Somers }
8569088fa05SBrian Somers
857859a4d16SJulian Elischer /*
858053359b7SPedro F. Giffuni * PPPOE_SERVICE advertisements are set up
859859a4d16SJulian Elischer * on sessions that are in PRIMED state.
860859a4d16SJulian Elischer */
8617762e8c6SGleb Smirnoff if (msg->header.cmd == NGM_PPPOE_SERVICE)
862859a4d16SJulian Elischer break;
8637762e8c6SGleb Smirnoff
8648be8c756SEugene Grosbein /*
8658be8c756SEugene Grosbein * PADM messages are set up on active sessions.
8668be8c756SEugene Grosbein */
8678be8c756SEugene Grosbein if (msg->header.cmd == NGM_PPPOE_SEND_HURL ||
8688be8c756SEugene Grosbein msg->header.cmd == NGM_PPPOE_SEND_MOTM) {
8698be8c756SEugene Grosbein if (sp->state != PPPOE_NEWCONNECTED &&
8708be8c756SEugene Grosbein sp->state != PPPOE_CONNECTED) {
8718be8c756SEugene Grosbein log(LOG_NOTICE, "ng_pppoe[%x]: session is not "
8728be8c756SEugene Grosbein "active\n", node->nd_ID);
8738be8c756SEugene Grosbein LEAVE(EISCONN);
8748be8c756SEugene Grosbein }
8758be8c756SEugene Grosbein break;
8768be8c756SEugene Grosbein }
8778be8c756SEugene Grosbein
878f795fd00SGleb Smirnoff if (sp->state != PPPOE_SNONE) {
8792e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe[%x]: Session already "
8802e87c3ccSGleb Smirnoff "active\n", node->nd_ID);
8814cf49a43SJulian Elischer LEAVE(EISCONN);
8824cf49a43SJulian Elischer }
8831e2510f8SJulian Elischer
8844cf49a43SJulian Elischer /*
8857762e8c6SGleb Smirnoff * Set up prototype header.
8864cf49a43SJulian Elischer */
8877762e8c6SGleb Smirnoff neg = malloc(sizeof(*neg), M_NETGRAPH_PPPOE,
88899cdf4ccSDavid Malone M_NOWAIT | M_ZERO);
8894cf49a43SJulian Elischer
8907762e8c6SGleb Smirnoff if (neg == NULL)
8914cf49a43SJulian Elischer LEAVE(ENOMEM);
8927762e8c6SGleb Smirnoff
893eb1b1807SGleb Smirnoff neg->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
8944cf49a43SJulian Elischer if (neg->m == NULL) {
8957762e8c6SGleb Smirnoff free(neg, M_NETGRAPH_PPPOE);
8964cf49a43SJulian Elischer LEAVE(ENOBUFS);
8974cf49a43SJulian Elischer }
8984cf49a43SJulian Elischer neg->m->m_pkthdr.rcvif = NULL;
8994cf49a43SJulian Elischer sp->neg = neg;
900ef237c7fSGleb Smirnoff ng_callout_init(&neg->handle);
9014cf49a43SJulian Elischer neg->m->m_len = sizeof(struct pppoe_full_hdr);
9024cf49a43SJulian Elischer neg->pkt = mtod(neg->m, union packet*);
903fdc755d1SGleb Smirnoff memcpy((void *)&neg->pkt->pkt_header.eh,
904b1ba28dfSGleb Smirnoff &privp->eh, sizeof(struct ether_header));
9054cf49a43SJulian Elischer neg->pkt->pkt_header.ph.ver = 0x1;
9064cf49a43SJulian Elischer neg->pkt->pkt_header.ph.type = 0x1;
9074cf49a43SJulian Elischer neg->pkt->pkt_header.ph.sid = 0x0000;
9084cf49a43SJulian Elischer neg->timeout = 0;
9094cf49a43SJulian Elischer
910069154d5SJulian Elischer sp->creator = NGI_RETADDR(item);
9114cf49a43SJulian Elischer }
9124cf49a43SJulian Elischer switch (msg->header.cmd) {
9134cf49a43SJulian Elischer case NGM_PPPOE_GET_STATUS:
9144cf49a43SJulian Elischer {
9158876b55dSJulian Elischer struct ngpppoestat *stats;
9164cf49a43SJulian Elischer
9174cf49a43SJulian Elischer NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
9187762e8c6SGleb Smirnoff if (!resp)
9194cf49a43SJulian Elischer LEAVE(ENOMEM);
9207762e8c6SGleb Smirnoff
9218876b55dSJulian Elischer stats = (struct ngpppoestat *) resp->data;
9224cf49a43SJulian Elischer stats->packets_in = privp->packets_in;
9234cf49a43SJulian Elischer stats->packets_out = privp->packets_out;
9244cf49a43SJulian Elischer break;
9254cf49a43SJulian Elischer }
9264cf49a43SJulian Elischer case NGM_PPPOE_CONNECT:
927098ff746SAlexander Motin {
9284cf49a43SJulian Elischer /*
9294cf49a43SJulian Elischer * Check the hook exists and is Uninitialised.
9304cf49a43SJulian Elischer * Send a PADI request, and start the timeout logic.
9314cf49a43SJulian Elischer * Store the originator of this message so we can send
9328be8c756SEugene Grosbein * a success or fail message to them later.
9337762e8c6SGleb Smirnoff * Move the session to SINIT.
9344cf49a43SJulian Elischer * Set up the session to the correct state and
9354cf49a43SJulian Elischer * start it.
9364cf49a43SJulian Elischer */
9378be8c756SEugene Grosbein int acnpos, acnlen = 0, acnsep = 0;
9388be8c756SEugene Grosbein int hupos, hulen = 0, husep = 0;
9398be8c756SEugene Grosbein int i, srvpos, srvlen;
9408be8c756SEugene Grosbein acnpos = 0;
941098ff746SAlexander Motin for (i = 0; i < ourmsg->data_len; i++) {
942098ff746SAlexander Motin if (ourmsg->data[i] == '\\') {
943098ff746SAlexander Motin acnlen = i;
944098ff746SAlexander Motin acnsep = 1;
945098ff746SAlexander Motin break;
946098ff746SAlexander Motin }
947098ff746SAlexander Motin }
9488be8c756SEugene Grosbein hupos = acnlen + acnsep;
9498be8c756SEugene Grosbein for (i = hupos; i < ourmsg->data_len; i++) {
9508be8c756SEugene Grosbein if (ourmsg->data[i] == '|') {
9518be8c756SEugene Grosbein hulen = i - hupos;
9528be8c756SEugene Grosbein husep = 1;
9538be8c756SEugene Grosbein break;
9548be8c756SEugene Grosbein }
9558be8c756SEugene Grosbein }
9568be8c756SEugene Grosbein srvpos = hupos + hulen + husep;
9578be8c756SEugene Grosbein srvlen = ourmsg->data_len - srvpos;
958098ff746SAlexander Motin
9598be8c756SEugene Grosbein bcopy(ourmsg->data + acnpos, neg->ac_name.data, acnlen);
960098ff746SAlexander Motin neg->ac_name_len = acnlen;
961098ff746SAlexander Motin
9628be8c756SEugene Grosbein neg->host_uniq.hdr.tag_type = PTT_HOST_UNIQ;
9638be8c756SEugene Grosbein if (hulen == 0) {
9648be8c756SEugene Grosbein /* Not provided, generate one */
9658be8c756SEugene Grosbein neg->host_uniq.hdr.tag_len = htons(sizeof(sp));
9668be8c756SEugene Grosbein bcopy(&sp, neg->host_uniq.data, sizeof(sp));
9678be8c756SEugene Grosbein neg->host_uniq_len = sizeof(sp);
9688be8c756SEugene Grosbein } else if (hulen > 2 && ourmsg->data[hupos] == '0' &&
9698be8c756SEugene Grosbein ourmsg->data[hupos + 1] == 'x' && hulen % 2 == 0) {
9708be8c756SEugene Grosbein /* Hex encoded */
9718be8c756SEugene Grosbein static const char hexdig[16] = "0123456789abcdef";
9728be8c756SEugene Grosbein int j;
9738be8c756SEugene Grosbein
9748be8c756SEugene Grosbein neg->host_uniq.hdr.tag_len = htons((uint16_t)(hulen / 2 - 1));
9758be8c756SEugene Grosbein for (i = 0; i < hulen - 2; i++) {
9768be8c756SEugene Grosbein for (j = 0;
9778be8c756SEugene Grosbein j < 16 &&
9788be8c756SEugene Grosbein ourmsg->data[hupos + 2 + i] != hexdig[j];
9798be8c756SEugene Grosbein j++);
9808be8c756SEugene Grosbein if (j == 16)
9818be8c756SEugene Grosbein LEAVE(EINVAL);
9828be8c756SEugene Grosbein if (i % 2 == 0)
9838be8c756SEugene Grosbein neg->host_uniq.data[i / 2] = j << 4;
9848be8c756SEugene Grosbein else
9858be8c756SEugene Grosbein neg->host_uniq.data[i / 2] |= j;
9868be8c756SEugene Grosbein }
9878be8c756SEugene Grosbein neg->host_uniq_len = hulen / 2 - 1;
9888be8c756SEugene Grosbein } else {
9898be8c756SEugene Grosbein /* Plain string */
9908be8c756SEugene Grosbein neg->host_uniq.hdr.tag_len = htons((uint16_t)hulen);
9918be8c756SEugene Grosbein bcopy(ourmsg->data + hupos, neg->host_uniq.data, hulen);
9928be8c756SEugene Grosbein neg->host_uniq_len = hulen;
9938be8c756SEugene Grosbein }
9948be8c756SEugene Grosbein
9954cf49a43SJulian Elischer neg->service.hdr.tag_type = PTT_SRV_NAME;
996098ff746SAlexander Motin neg->service.hdr.tag_len = htons((uint16_t)srvlen);
9978be8c756SEugene Grosbein bcopy(ourmsg->data + srvpos, neg->service.data, srvlen);
998098ff746SAlexander Motin neg->service_len = srvlen;
99951e805c4SKristof Provost NET_EPOCH_ENTER(et);
10004cf49a43SJulian Elischer pppoe_start(sp);
100151e805c4SKristof Provost NET_EPOCH_EXIT(et);
10024cf49a43SJulian Elischer break;
1003098ff746SAlexander Motin }
10044cf49a43SJulian Elischer case NGM_PPPOE_LISTEN:
10054cf49a43SJulian Elischer /*
10064cf49a43SJulian Elischer * Check the hook exists and is Uninitialised.
10074cf49a43SJulian Elischer * Install the service matching string.
10084cf49a43SJulian Elischer * Store the originator of this message so we can send
10098be8c756SEugene Grosbein * a success or fail message to them later.
10104cf49a43SJulian Elischer * Move the hook to 'LISTENING'
10114cf49a43SJulian Elischer */
10124cf49a43SJulian Elischer neg->service.hdr.tag_type = PTT_SRV_NAME;
101327121ab1SBrian Somers neg->service.hdr.tag_len =
10147762e8c6SGleb Smirnoff htons((uint16_t)ourmsg->data_len);
10151e2510f8SJulian Elischer
101627121ab1SBrian Somers if (ourmsg->data_len)
101727121ab1SBrian Somers bcopy(ourmsg->data, neg->service.data,
101827121ab1SBrian Somers ourmsg->data_len);
101927121ab1SBrian Somers neg->service_len = ourmsg->data_len;
10204cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADT_CODE;
10214cf49a43SJulian Elischer /*
10227762e8c6SGleb Smirnoff * Wait for PADI packet coming from Ethernet.
10234cf49a43SJulian Elischer */
10244cf49a43SJulian Elischer sp->state = PPPOE_LISTENING;
1025dda30f12SAlexander Motin LIST_INSERT_HEAD(&privp->listeners, sp, sessions);
10264cf49a43SJulian Elischer break;
10274cf49a43SJulian Elischer case NGM_PPPOE_OFFER:
10284cf49a43SJulian Elischer /*
10294cf49a43SJulian Elischer * Check the hook exists and is Uninitialised.
10304cf49a43SJulian Elischer * Store the originator of this message so we can send
10314cf49a43SJulian Elischer * a success of fail message to them later.
10324cf49a43SJulian Elischer * Store the AC-Name given and go to PRIMED.
10334cf49a43SJulian Elischer */
10344cf49a43SJulian Elischer neg->ac_name.hdr.tag_type = PTT_AC_NAME;
103527121ab1SBrian Somers neg->ac_name.hdr.tag_len =
10367762e8c6SGleb Smirnoff htons((uint16_t)ourmsg->data_len);
103727121ab1SBrian Somers if (ourmsg->data_len)
103827121ab1SBrian Somers bcopy(ourmsg->data, neg->ac_name.data,
103927121ab1SBrian Somers ourmsg->data_len);
104027121ab1SBrian Somers neg->ac_name_len = ourmsg->data_len;
10414cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADO_CODE;
10424cf49a43SJulian Elischer /*
10437762e8c6SGleb Smirnoff * Wait for PADI packet coming from hook.
10444cf49a43SJulian Elischer */
10454cf49a43SJulian Elischer sp->state = PPPOE_PRIMED;
10464cf49a43SJulian Elischer break;
1047859a4d16SJulian Elischer case NGM_PPPOE_SERVICE:
1048859a4d16SJulian Elischer /*
1049859a4d16SJulian Elischer * Check the session is primed.
1050859a4d16SJulian Elischer * for now just allow ONE service to be advertised.
1051859a4d16SJulian Elischer * If you do it twice you just overwrite.
1052859a4d16SJulian Elischer */
10535078fb0bSJulian Elischer if (sp->state != PPPOE_PRIMED) {
10542e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe[%x]: session not "
10552e87c3ccSGleb Smirnoff "primed\n", node->nd_ID);
1056859a4d16SJulian Elischer LEAVE(EISCONN);
1057859a4d16SJulian Elischer }
10580069b9cbSJulian Elischer neg = sp->neg;
1059859a4d16SJulian Elischer neg->service.hdr.tag_type = PTT_SRV_NAME;
1060859a4d16SJulian Elischer neg->service.hdr.tag_len =
10617762e8c6SGleb Smirnoff htons((uint16_t)ourmsg->data_len);
1062859a4d16SJulian Elischer
1063859a4d16SJulian Elischer if (ourmsg->data_len)
1064859a4d16SJulian Elischer bcopy(ourmsg->data, neg->service.data,
1065859a4d16SJulian Elischer ourmsg->data_len);
1066859a4d16SJulian Elischer neg->service_len = ourmsg->data_len;
1067859a4d16SJulian Elischer break;
1068fdc755d1SGleb Smirnoff case NGM_PPPOE_SETMODE:
1069fdc755d1SGleb Smirnoff {
1070fdc755d1SGleb Smirnoff char *s;
1071fdc755d1SGleb Smirnoff size_t len;
1072fdc755d1SGleb Smirnoff
1073fdc755d1SGleb Smirnoff if (msg->header.arglen == 0)
1074fdc755d1SGleb Smirnoff LEAVE(EINVAL);
1075fdc755d1SGleb Smirnoff
1076fdc755d1SGleb Smirnoff s = (char *)msg->data;
1077fdc755d1SGleb Smirnoff len = msg->header.arglen - 1;
1078fdc755d1SGleb Smirnoff
10797762e8c6SGleb Smirnoff /* Search for matching mode string. */
10801c8aa594SGleb Smirnoff if (len == strlen(NG_PPPOE_STANDARD) &&
10811c8aa594SGleb Smirnoff (strncmp(NG_PPPOE_STANDARD, s, len) == 0)) {
10821c8aa594SGleb Smirnoff privp->flags = 0;
1083b1ba28dfSGleb Smirnoff privp->eh.ether_type = ETHERTYPE_PPPOE_DISC;
10841c8aa594SGleb Smirnoff break;
10851c8aa594SGleb Smirnoff }
10861c8aa594SGleb Smirnoff if (len == strlen(NG_PPPOE_3COM) &&
10871c8aa594SGleb Smirnoff (strncmp(NG_PPPOE_3COM, s, len) == 0)) {
10881c8aa594SGleb Smirnoff privp->flags |= COMPAT_3COM;
1089b1ba28dfSGleb Smirnoff privp->eh.ether_type =
1090b1ba28dfSGleb Smirnoff ETHERTYPE_PPPOE_3COM_DISC;
10911c8aa594SGleb Smirnoff break;
10921c8aa594SGleb Smirnoff }
10931c8aa594SGleb Smirnoff if (len == strlen(NG_PPPOE_DLINK) &&
10941c8aa594SGleb Smirnoff (strncmp(NG_PPPOE_DLINK, s, len) == 0)) {
10951c8aa594SGleb Smirnoff privp->flags |= COMPAT_DLINK;
10961c8aa594SGleb Smirnoff break;
10971c8aa594SGleb Smirnoff }
10981c8aa594SGleb Smirnoff error = EINVAL;
1099fdc755d1SGleb Smirnoff break;
1100fdc755d1SGleb Smirnoff }
1101fdc755d1SGleb Smirnoff case NGM_PPPOE_GETMODE:
11021c8aa594SGleb Smirnoff {
11031c8aa594SGleb Smirnoff char *s;
11041c8aa594SGleb Smirnoff size_t len = 0;
11051c8aa594SGleb Smirnoff
11061c8aa594SGleb Smirnoff if (privp->flags == 0)
11071c8aa594SGleb Smirnoff len += strlen(NG_PPPOE_STANDARD) + 1;
11081c8aa594SGleb Smirnoff if (privp->flags & COMPAT_3COM)
11091c8aa594SGleb Smirnoff len += strlen(NG_PPPOE_3COM) + 1;
11101c8aa594SGleb Smirnoff if (privp->flags & COMPAT_DLINK)
11111c8aa594SGleb Smirnoff len += strlen(NG_PPPOE_DLINK) + 1;
11121c8aa594SGleb Smirnoff
11131c8aa594SGleb Smirnoff NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
1114fdc755d1SGleb Smirnoff if (resp == NULL)
1115fdc755d1SGleb Smirnoff LEAVE(ENOMEM);
11164cf49a43SJulian Elischer
11171c8aa594SGleb Smirnoff s = (char *)resp->data;
11181c8aa594SGleb Smirnoff if (privp->flags == 0) {
11191c8aa594SGleb Smirnoff len = strlen(NG_PPPOE_STANDARD);
11201c8aa594SGleb Smirnoff strlcpy(s, NG_PPPOE_STANDARD, len + 1);
11211c8aa594SGleb Smirnoff break;
11221c8aa594SGleb Smirnoff }
11231c8aa594SGleb Smirnoff if (privp->flags & COMPAT_3COM) {
11241c8aa594SGleb Smirnoff len = strlen(NG_PPPOE_3COM);
11251c8aa594SGleb Smirnoff strlcpy(s, NG_PPPOE_3COM, len + 1);
11261c8aa594SGleb Smirnoff s += len;
11271c8aa594SGleb Smirnoff }
11281c8aa594SGleb Smirnoff if (privp->flags & COMPAT_DLINK) {
11291c8aa594SGleb Smirnoff if (s != resp->data)
11301c8aa594SGleb Smirnoff *s++ = '|';
11311c8aa594SGleb Smirnoff len = strlen(NG_PPPOE_DLINK);
11321c8aa594SGleb Smirnoff strlcpy(s, NG_PPPOE_DLINK, len + 1);
11331c8aa594SGleb Smirnoff }
11341c8aa594SGleb Smirnoff break;
11351c8aa594SGleb Smirnoff }
1136b1ba28dfSGleb Smirnoff case NGM_PPPOE_SETENADDR:
1137b1ba28dfSGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN)
1138b1ba28dfSGleb Smirnoff LEAVE(EINVAL);
1139b1ba28dfSGleb Smirnoff bcopy(msg->data, &privp->eh.ether_shost,
1140b1ba28dfSGleb Smirnoff ETHER_ADDR_LEN);
1141b1ba28dfSGleb Smirnoff break;
11425b363c09SAlexander Motin case NGM_PPPOE_SETMAXP:
11435b363c09SAlexander Motin if (msg->header.arglen != sizeof(uint16_t))
11445b363c09SAlexander Motin LEAVE(EINVAL);
11455b363c09SAlexander Motin privp->max_payload.hdr.tag_type = PTT_MAX_PAYL;
11465b363c09SAlexander Motin privp->max_payload.hdr.tag_len = htons(sizeof(uint16_t));
11475b363c09SAlexander Motin privp->max_payload.data = htons(*((uint16_t *)msg->data));
11485b363c09SAlexander Motin break;
11498be8c756SEugene Grosbein case NGM_PPPOE_SEND_HURL:
11508be8c756SEugene Grosbein {
11518be8c756SEugene Grosbein struct mbuf *m;
11528be8c756SEugene Grosbein
11538be8c756SEugene Grosbein /* Generate a packet of that type. */
11548be8c756SEugene Grosbein m = m_gethdr(M_NOWAIT, MT_DATA);
11558be8c756SEugene Grosbein if (m == NULL)
11568be8c756SEugene Grosbein log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
11578be8c756SEugene Grosbein "mbufs\n", node->nd_ID);
11588be8c756SEugene Grosbein else {
11598be8c756SEugene Grosbein struct pppoe_full_hdr *wh;
11608be8c756SEugene Grosbein struct pppoe_tag *tag;
11618be8c756SEugene Grosbein int error = 0;
11628be8c756SEugene Grosbein
11638be8c756SEugene Grosbein wh = mtod(m, struct pppoe_full_hdr *);
11648be8c756SEugene Grosbein bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
11658be8c756SEugene Grosbein
11668be8c756SEugene Grosbein /* Revert the stored header to DISC/PADM mode. */
11678be8c756SEugene Grosbein wh->ph.code = PADM_CODE;
11688be8c756SEugene Grosbein /*
11698be8c756SEugene Grosbein * Configure ethertype depending on what
11708be8c756SEugene Grosbein * was used during sessions stage.
11718be8c756SEugene Grosbein */
11728be8c756SEugene Grosbein if (wh->eh.ether_type ==
11738be8c756SEugene Grosbein ETHERTYPE_PPPOE_3COM_SESS)
11748be8c756SEugene Grosbein wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
11758be8c756SEugene Grosbein else
11768be8c756SEugene Grosbein wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
11778be8c756SEugene Grosbein /*
11788be8c756SEugene Grosbein * Add PADM message and adjust sizes.
11798be8c756SEugene Grosbein */
11808be8c756SEugene Grosbein tag = (void *)(&wh->ph + 1);
11818be8c756SEugene Grosbein tag->tag_type = PTT_HURL;
11828be8c756SEugene Grosbein tag->tag_len = htons(ourmsg->data_len);
11838be8c756SEugene Grosbein strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
11848be8c756SEugene Grosbein m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
11858be8c756SEugene Grosbein ourmsg->data_len;
11868be8c756SEugene Grosbein wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
118751e805c4SKristof Provost NET_EPOCH_ENTER(et);
11888be8c756SEugene Grosbein NG_SEND_DATA_ONLY(error,
11898be8c756SEugene Grosbein privp->ethernet_hook, m);
119051e805c4SKristof Provost NET_EPOCH_EXIT(et);
11918be8c756SEugene Grosbein }
11928be8c756SEugene Grosbein break;
11938be8c756SEugene Grosbein }
11948be8c756SEugene Grosbein case NGM_PPPOE_SEND_MOTM:
11958be8c756SEugene Grosbein {
11968be8c756SEugene Grosbein struct mbuf *m;
11978be8c756SEugene Grosbein
11988be8c756SEugene Grosbein /* Generate a packet of that type. */
11998be8c756SEugene Grosbein m = m_gethdr(M_NOWAIT, MT_DATA);
12008be8c756SEugene Grosbein if (m == NULL)
12018be8c756SEugene Grosbein log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
12028be8c756SEugene Grosbein "mbufs\n", node->nd_ID);
12038be8c756SEugene Grosbein else {
12048be8c756SEugene Grosbein struct pppoe_full_hdr *wh;
12058be8c756SEugene Grosbein struct pppoe_tag *tag;
12068be8c756SEugene Grosbein int error = 0;
12078be8c756SEugene Grosbein
12088be8c756SEugene Grosbein wh = mtod(m, struct pppoe_full_hdr *);
12098be8c756SEugene Grosbein bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
12108be8c756SEugene Grosbein
12118be8c756SEugene Grosbein /* Revert the stored header to DISC/PADM mode. */
12128be8c756SEugene Grosbein wh->ph.code = PADM_CODE;
12138be8c756SEugene Grosbein /*
12148be8c756SEugene Grosbein * Configure ethertype depending on what
12158be8c756SEugene Grosbein * was used during sessions stage.
12168be8c756SEugene Grosbein */
12178be8c756SEugene Grosbein if (wh->eh.ether_type ==
12188be8c756SEugene Grosbein ETHERTYPE_PPPOE_3COM_SESS)
12198be8c756SEugene Grosbein wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
12208be8c756SEugene Grosbein else
12218be8c756SEugene Grosbein wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
12228be8c756SEugene Grosbein /*
12238be8c756SEugene Grosbein * Add PADM message and adjust sizes.
12248be8c756SEugene Grosbein */
12258be8c756SEugene Grosbein tag = (void *)(&wh->ph + 1);
12268be8c756SEugene Grosbein tag->tag_type = PTT_MOTM;
12278be8c756SEugene Grosbein tag->tag_len = htons(ourmsg->data_len);
12288be8c756SEugene Grosbein strncpy((char *)(tag + 1), ourmsg->data, ourmsg->data_len);
12298be8c756SEugene Grosbein m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
12308be8c756SEugene Grosbein ourmsg->data_len;
12318be8c756SEugene Grosbein wh->ph.length = htons(sizeof(*tag) + ourmsg->data_len);
123251e805c4SKristof Provost NET_EPOCH_ENTER(et);
12338be8c756SEugene Grosbein NG_SEND_DATA_ONLY(error,
12348be8c756SEugene Grosbein privp->ethernet_hook, m);
123551e805c4SKristof Provost NET_EPOCH_EXIT(et);
12368be8c756SEugene Grosbein }
12378be8c756SEugene Grosbein break;
12388be8c756SEugene Grosbein }
12391c8aa594SGleb Smirnoff default:
12401c8aa594SGleb Smirnoff LEAVE(EINVAL);
12411c8aa594SGleb Smirnoff }
12421c8aa594SGleb Smirnoff break;
1243b1ba28dfSGleb Smirnoff case NGM_ETHER_COOKIE:
1244b1ba28dfSGleb Smirnoff if (!(msg->header.flags & NGF_RESP))
1245b1ba28dfSGleb Smirnoff LEAVE(EINVAL);
1246b1ba28dfSGleb Smirnoff switch (msg->header.cmd) {
1247b1ba28dfSGleb Smirnoff case NGM_ETHER_GET_ENADDR:
1248b1ba28dfSGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN)
1249b1ba28dfSGleb Smirnoff LEAVE(EINVAL);
1250b1ba28dfSGleb Smirnoff bcopy(msg->data, &privp->eh.ether_shost,
1251b1ba28dfSGleb Smirnoff ETHER_ADDR_LEN);
1252b1ba28dfSGleb Smirnoff break;
1253b1ba28dfSGleb Smirnoff default:
1254f366efa9SGleb Smirnoff LEAVE(EINVAL);
1255b1ba28dfSGleb Smirnoff }
1256b1ba28dfSGleb Smirnoff break;
12571c8aa594SGleb Smirnoff default:
12581c8aa594SGleb Smirnoff LEAVE(EINVAL);
12591c8aa594SGleb Smirnoff }
12607762e8c6SGleb Smirnoff
12617762e8c6SGleb Smirnoff /* Take care of synchronous response, if any. */
12624cf49a43SJulian Elischer quit:
12631c8aa594SGleb Smirnoff CTR2(KTR_NET, "%20s: returning %d", __func__, error);
1264069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp);
12657762e8c6SGleb Smirnoff /* Free the message and return. */
1266069154d5SJulian Elischer NG_FREE_MSG(msg);
12674cf49a43SJulian Elischer return(error);
12684cf49a43SJulian Elischer }
12694cf49a43SJulian Elischer
12701e2510f8SJulian Elischer /*
12711e2510f8SJulian Elischer * Start a client into the first state. A separate function because
12721e2510f8SJulian Elischer * it can be needed if the negotiation times out.
12731e2510f8SJulian Elischer */
12744cf49a43SJulian Elischer static void
pppoe_start(sessp sp)12754cf49a43SJulian Elischer pppoe_start(sessp sp)
12764cf49a43SJulian Elischer {
12778cfaad5fSAlexander Motin hook_p hook = sp->hook;
12788cfaad5fSAlexander Motin node_p node = NG_HOOK_NODE(hook);
12798cfaad5fSAlexander Motin priv_p privp = NG_NODE_PRIVATE(node);
12808cfaad5fSAlexander Motin negp neg = sp->neg;
12818cfaad5fSAlexander Motin struct mbuf *m0;
12828cfaad5fSAlexander Motin int error;
12834cf49a43SJulian Elischer
12844cf49a43SJulian Elischer /*
12857762e8c6SGleb Smirnoff * Kick the state machine into starting up.
12864cf49a43SJulian Elischer */
12877762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
12884cf49a43SJulian Elischer sp->state = PPPOE_SINIT;
12897762e8c6SGleb Smirnoff /*
12907762e8c6SGleb Smirnoff * Reset the packet header to broadcast. Since we are
1291b1ba28dfSGleb Smirnoff * in a client mode use configured ethertype.
12927762e8c6SGleb Smirnoff */
12938cfaad5fSAlexander Motin memcpy((void *)&neg->pkt->pkt_header.eh, &privp->eh,
1294b1ba28dfSGleb Smirnoff sizeof(struct ether_header));
12958cfaad5fSAlexander Motin neg->pkt->pkt_header.ph.code = PADI_CODE;
12964cf49a43SJulian Elischer init_tags(sp);
12978be8c756SEugene Grosbein insert_tag(sp, &neg->host_uniq.hdr);
12988cfaad5fSAlexander Motin insert_tag(sp, &neg->service.hdr);
12995b363c09SAlexander Motin if (privp->max_payload.data != 0)
13005b363c09SAlexander Motin insert_tag(sp, &privp->max_payload.hdr);
13014cf49a43SJulian Elischer make_packet(sp);
13028cfaad5fSAlexander Motin /*
13038cfaad5fSAlexander Motin * Send packet and prepare to retransmit it after timeout.
13048cfaad5fSAlexander Motin */
13058cfaad5fSAlexander Motin ng_callout(&neg->handle, node, hook, PPPOE_INITIAL_TIMEOUT * hz,
13068cfaad5fSAlexander Motin pppoe_ticker, NULL, 0);
13078cfaad5fSAlexander Motin neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1308eb1b1807SGleb Smirnoff m0 = m_copypacket(neg->m, M_NOWAIT);
13098cfaad5fSAlexander Motin NG_SEND_DATA_ONLY(error, privp->ethernet_hook, m0);
13104cf49a43SJulian Elischer }
13114cf49a43SJulian Elischer
1312c48a0b5fSBrian Somers static int
send_acname(sessp sp,const struct pppoe_tag * tag)1313816b834fSArchie Cobbs send_acname(sessp sp, const struct pppoe_tag *tag)
1314c48a0b5fSBrian Somers {
13159e6798e7SBrian Somers int error, tlen;
1316c48a0b5fSBrian Somers struct ng_mesg *msg;
1317c48a0b5fSBrian Somers struct ngpppoe_sts *sts;
1318c48a0b5fSBrian Somers
13197762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
13207762e8c6SGleb Smirnoff
1321c48a0b5fSBrian Somers NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_ACNAME,
1322c48a0b5fSBrian Somers sizeof(struct ngpppoe_sts), M_NOWAIT);
1323c48a0b5fSBrian Somers if (msg == NULL)
1324c48a0b5fSBrian Somers return (ENOMEM);
1325c48a0b5fSBrian Somers
1326c48a0b5fSBrian Somers sts = (struct ngpppoe_sts *)msg->data;
132787e2c66aSHartmut Brandt tlen = min(NG_HOOKSIZ - 1, ntohs(tag->tag_len));
132860d234c5SEd Schouten strncpy(sts->hook, (const char *)(tag + 1), tlen);
13299e6798e7SBrian Somers sts->hook[tlen] = '\0';
1330facfd889SArchie Cobbs NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
1331c48a0b5fSBrian Somers
1332c48a0b5fSBrian Somers return (error);
1333c48a0b5fSBrian Somers }
1334c48a0b5fSBrian Somers
133587c4cce0SBrian Somers static int
send_sessionid(sessp sp)133687c4cce0SBrian Somers send_sessionid(sessp sp)
133787c4cce0SBrian Somers {
133887c4cce0SBrian Somers int error;
133987c4cce0SBrian Somers struct ng_mesg *msg;
134087c4cce0SBrian Somers
13417762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
13427762e8c6SGleb Smirnoff
134387c4cce0SBrian Somers NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SESSIONID,
13447762e8c6SGleb Smirnoff sizeof(uint16_t), M_NOWAIT);
134587c4cce0SBrian Somers if (msg == NULL)
134687c4cce0SBrian Somers return (ENOMEM);
134787c4cce0SBrian Somers
13487762e8c6SGleb Smirnoff *(uint16_t *)msg->data = sp->Session_ID;
1349facfd889SArchie Cobbs NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
135087c4cce0SBrian Somers
135187c4cce0SBrian Somers return (error);
135287c4cce0SBrian Somers }
135387c4cce0SBrian Somers
13545b363c09SAlexander Motin static int
send_maxp(sessp sp,const struct pppoe_tag * tag)13555b363c09SAlexander Motin send_maxp(sessp sp, const struct pppoe_tag *tag)
13565b363c09SAlexander Motin {
13575b363c09SAlexander Motin int error;
13585b363c09SAlexander Motin struct ng_mesg *msg;
13595b363c09SAlexander Motin struct ngpppoe_maxp *maxp;
13605b363c09SAlexander Motin
13615b363c09SAlexander Motin CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
13625b363c09SAlexander Motin
13635b363c09SAlexander Motin NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_SETMAXP,
13645b363c09SAlexander Motin sizeof(struct ngpppoe_maxp), M_NOWAIT);
13655b363c09SAlexander Motin if (msg == NULL)
13665b363c09SAlexander Motin return (ENOMEM);
13675b363c09SAlexander Motin
13685b363c09SAlexander Motin maxp = (struct ngpppoe_maxp *)msg->data;
13695b363c09SAlexander Motin strncpy(maxp->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
13705b363c09SAlexander Motin maxp->data = ntohs(((const struct maxptag *)tag)->data);
13715b363c09SAlexander Motin NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
13725b363c09SAlexander Motin
13735b363c09SAlexander Motin return (error);
13745b363c09SAlexander Motin }
13755b363c09SAlexander Motin
13768be8c756SEugene Grosbein static int
send_hurl(sessp sp,const struct pppoe_tag * tag)13778be8c756SEugene Grosbein send_hurl(sessp sp, const struct pppoe_tag *tag)
13788be8c756SEugene Grosbein {
13798be8c756SEugene Grosbein int error, tlen;
13808be8c756SEugene Grosbein struct ng_mesg *msg;
13818be8c756SEugene Grosbein struct ngpppoe_padm *padm;
13828be8c756SEugene Grosbein
13838be8c756SEugene Grosbein CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
13848be8c756SEugene Grosbein
13858be8c756SEugene Grosbein NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_HURL,
13868be8c756SEugene Grosbein sizeof(struct ngpppoe_padm), M_NOWAIT);
13878be8c756SEugene Grosbein if (msg == NULL)
13888be8c756SEugene Grosbein return (ENOMEM);
13898be8c756SEugene Grosbein
13908be8c756SEugene Grosbein padm = (struct ngpppoe_padm *)msg->data;
13918be8c756SEugene Grosbein tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
13928be8c756SEugene Grosbein strncpy(padm->msg, (const char *)(tag + 1), tlen);
13938be8c756SEugene Grosbein padm->msg[tlen] = '\0';
13948be8c756SEugene Grosbein NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
13958be8c756SEugene Grosbein
13968be8c756SEugene Grosbein return (error);
13978be8c756SEugene Grosbein }
13988be8c756SEugene Grosbein
13998be8c756SEugene Grosbein static int
send_motm(sessp sp,const struct pppoe_tag * tag)14008be8c756SEugene Grosbein send_motm(sessp sp, const struct pppoe_tag *tag)
14018be8c756SEugene Grosbein {
14028be8c756SEugene Grosbein int error, tlen;
14038be8c756SEugene Grosbein struct ng_mesg *msg;
14048be8c756SEugene Grosbein struct ngpppoe_padm *padm;
14058be8c756SEugene Grosbein
14068be8c756SEugene Grosbein CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
14078be8c756SEugene Grosbein
14088be8c756SEugene Grosbein NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, NGM_PPPOE_MOTM,
14098be8c756SEugene Grosbein sizeof(struct ngpppoe_padm), M_NOWAIT);
14108be8c756SEugene Grosbein if (msg == NULL)
14118be8c756SEugene Grosbein return (ENOMEM);
14128be8c756SEugene Grosbein
14138be8c756SEugene Grosbein padm = (struct ngpppoe_padm *)msg->data;
14148be8c756SEugene Grosbein tlen = min(PPPOE_PADM_VALUE_SIZE - 1, ntohs(tag->tag_len));
14158be8c756SEugene Grosbein strncpy(padm->msg, (const char *)(tag + 1), tlen);
14168be8c756SEugene Grosbein padm->msg[tlen] = '\0';
14178be8c756SEugene Grosbein NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
14188be8c756SEugene Grosbein
14198be8c756SEugene Grosbein return (error);
14208be8c756SEugene Grosbein }
14218be8c756SEugene Grosbein
14224cf49a43SJulian Elischer /*
142398e7b753SAlexander Motin * Receive data from session hook and do something with it.
14244cf49a43SJulian Elischer */
14254cf49a43SJulian Elischer static int
ng_pppoe_rcvdata(hook_p hook,item_p item)1426069154d5SJulian Elischer ng_pppoe_rcvdata(hook_p hook, item_p item)
14274cf49a43SJulian Elischer {
142830400f03SJulian Elischer node_p node = NG_HOOK_NODE(hook);
142930400f03SJulian Elischer const priv_p privp = NG_NODE_PRIVATE(node);
143030400f03SJulian Elischer sessp sp = NG_HOOK_PRIVATE(hook);
143198e7b753SAlexander Motin struct pppoe_full_hdr *wh;
14327762e8c6SGleb Smirnoff struct mbuf *m;
143398e7b753SAlexander Motin int error;
14344cf49a43SJulian Elischer
14357762e8c6SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
14367762e8c6SGleb Smirnoff __func__, node->nd_ID, node, item, hook->hk_name, hook);
14377762e8c6SGleb Smirnoff
1438069154d5SJulian Elischer NGI_GET_M(item, m);
14394cf49a43SJulian Elischer switch (sp->state) {
14404cf49a43SJulian Elischer case PPPOE_NEWCONNECTED:
14414cf49a43SJulian Elischer case PPPOE_CONNECTED: {
14427b38c4e4SArchie Cobbs /*
14437b38c4e4SArchie Cobbs * Remove PPP address and control fields, if any.
14447b38c4e4SArchie Cobbs * For example, ng_ppp(4) always sends LCP packets
14457b38c4e4SArchie Cobbs * with address and control fields as required by
14467b38c4e4SArchie Cobbs * generic PPP. PPPoE is an exception to the rule.
14477b38c4e4SArchie Cobbs */
14487b38c4e4SArchie Cobbs if (m->m_pkthdr.len >= 2) {
14497b38c4e4SArchie Cobbs if (m->m_len < 2 && !(m = m_pullup(m, 2)))
14507b38c4e4SArchie Cobbs LEAVE(ENOBUFS);
1451b4d0be22SAlexander Motin if (mtod(m, u_char *)[0] == 0xff &&
1452b4d0be22SAlexander Motin mtod(m, u_char *)[1] == 0x03)
14537b38c4e4SArchie Cobbs m_adj(m, 2);
14547b38c4e4SArchie Cobbs }
1455*28903f39SEugene Grosbein
1456*28903f39SEugene Grosbein if (V_ng_pppoe_lcp_pcp && m->m_pkthdr.len >= 2 &&
1457*28903f39SEugene Grosbein m->m_len >= 2 && (m = m_pullup(m, 2)) &&
1458*28903f39SEugene Grosbein mtod(m, uint16_t *)[0] == htons(PROT_LCP))
1459*28903f39SEugene Grosbein EVL_APPLY_PRI(m, (uint8_t)(V_ng_pppoe_lcp_pcp & 0x7));
1460*28903f39SEugene Grosbein
14614cf49a43SJulian Elischer /*
14624cf49a43SJulian Elischer * Bang in a pre-made header, and set the length up
14634cf49a43SJulian Elischer * to be correct. Then send it to the ethernet driver.
14644cf49a43SJulian Elischer */
1465eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(*wh), M_NOWAIT);
14667762e8c6SGleb Smirnoff if (m == NULL)
14674cf49a43SJulian Elischer LEAVE(ENOBUFS);
14687762e8c6SGleb Smirnoff
14694cf49a43SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *);
14704cf49a43SJulian Elischer bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
14711e7d84b0SAlexander Motin wh->ph.length = htons(m->m_pkthdr.len - sizeof(*wh));
1472069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m);
14734cf49a43SJulian Elischer privp->packets_out++;
14744cf49a43SJulian Elischer break;
14754cf49a43SJulian Elischer }
147698e7b753SAlexander Motin case PPPOE_PRIMED: {
147798e7b753SAlexander Motin struct {
147898e7b753SAlexander Motin struct pppoe_tag hdr;
147998e7b753SAlexander Motin union uniq data;
148098e7b753SAlexander Motin } __packed uniqtag;
148198e7b753SAlexander Motin const struct pppoe_tag *tag;
148298e7b753SAlexander Motin struct mbuf *m0;
148398e7b753SAlexander Motin const struct pppoe_hdr *ph;
148498e7b753SAlexander Motin negp neg = sp->neg;
148598e7b753SAlexander Motin uint8_t code;
148698e7b753SAlexander Motin
14874cf49a43SJulian Elischer /*
14884cf49a43SJulian Elischer * A PADI packet is being returned by the application
14894cf49a43SJulian Elischer * that has set up this hook. This indicates that it
14904cf49a43SJulian Elischer * wants us to offer service.
14914cf49a43SJulian Elischer */
1492bdaf2e81SJulian Elischer if (m->m_len < sizeof(*wh)) {
1493bdaf2e81SJulian Elischer m = m_pullup(m, sizeof(*wh));
14947762e8c6SGleb Smirnoff if (m == NULL)
14954cf49a43SJulian Elischer LEAVE(ENOBUFS);
14964cf49a43SJulian Elischer }
14974cf49a43SJulian Elischer wh = mtod(m, struct pppoe_full_hdr *);
14984cf49a43SJulian Elischer ph = &wh->ph;
14994cf49a43SJulian Elischer code = wh->ph.code;
15007762e8c6SGleb Smirnoff /* Use peers mode in session. */
1501fdc755d1SGleb Smirnoff neg->pkt->pkt_header.eh.ether_type = wh->eh.ether_type;
15027762e8c6SGleb Smirnoff if (code != PADI_CODE)
15031e2510f8SJulian Elischer LEAVE(EINVAL);
1504ef237c7fSGleb Smirnoff ng_uncallout(&neg->handle, node);
15054cf49a43SJulian Elischer
15064cf49a43SJulian Elischer /*
15074cf49a43SJulian Elischer * This is the first time we hear
15084cf49a43SJulian Elischer * from the client, so note it's
15094cf49a43SJulian Elischer * unicast address, replacing the
15104cf49a43SJulian Elischer * broadcast address.
15114cf49a43SJulian Elischer */
15124cf49a43SJulian Elischer bcopy(wh->eh.ether_shost,
15134cf49a43SJulian Elischer neg->pkt->pkt_header.eh.ether_dhost,
15144cf49a43SJulian Elischer ETHER_ADDR_LEN);
15154cf49a43SJulian Elischer sp->state = PPPOE_SOFFER;
15164cf49a43SJulian Elischer neg->timeout = 0;
15174cf49a43SJulian Elischer neg->pkt->pkt_header.ph.code = PADO_CODE;
15184cf49a43SJulian Elischer
15194cf49a43SJulian Elischer /*
15207762e8c6SGleb Smirnoff * Start working out the tags to respond with.
15214cf49a43SJulian Elischer */
15224cf49a43SJulian Elischer uniqtag.hdr.tag_type = PTT_AC_COOKIE;
15234cf49a43SJulian Elischer uniqtag.hdr.tag_len = htons((u_int16_t)sizeof(sp));
15244cf49a43SJulian Elischer uniqtag.data.pointer = sp;
15254cf49a43SJulian Elischer init_tags(sp);
15264cf49a43SJulian Elischer insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
15271f89d938SJulian Elischer if ((tag = get_tag(ph, PTT_SRV_NAME)))
15284adb13fdSJulian Elischer insert_tag(sp, tag); /* return service */
1529859a4d16SJulian Elischer /*
1530859a4d16SJulian Elischer * If we have a NULL service request
1531859a4d16SJulian Elischer * and have an extra service defined in this hook,
1532859a4d16SJulian Elischer * then also add a tag for the extra service.
1533859a4d16SJulian Elischer * XXX this is a hack. eventually we should be able
1534859a4d16SJulian Elischer * to support advertising many services, not just one
1535859a4d16SJulian Elischer */
15367762e8c6SGleb Smirnoff if (((tag == NULL) || (tag->tag_len == 0)) &&
15377762e8c6SGleb Smirnoff (neg->service.hdr.tag_len != 0)) {
1538859a4d16SJulian Elischer insert_tag(sp, &neg->service.hdr); /* SERVICE */
1539859a4d16SJulian Elischer }
15401f89d938SJulian Elischer if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
15411f89d938SJulian Elischer insert_tag(sp, tag); /* returned hostunique */
15421f89d938SJulian Elischer insert_tag(sp, &uniqtag.hdr);
15434cf49a43SJulian Elischer scan_tags(sp, ph);
15444cf49a43SJulian Elischer make_packet(sp);
15458cfaad5fSAlexander Motin /*
15468cfaad5fSAlexander Motin * Send the offer but if they don't respond
15478cfaad5fSAlexander Motin * in PPPOE_OFFER_TIMEOUT seconds, forget about it.
15488cfaad5fSAlexander Motin */
15498cfaad5fSAlexander Motin ng_callout(&neg->handle, node, hook, PPPOE_OFFER_TIMEOUT * hz,
15508cfaad5fSAlexander Motin pppoe_ticker, NULL, 0);
1551eb1b1807SGleb Smirnoff m0 = m_copypacket(sp->neg->m, M_NOWAIT);
155298e7b753SAlexander Motin NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
155398e7b753SAlexander Motin privp->packets_out++;
15544cf49a43SJulian Elischer break;
155598e7b753SAlexander Motin }
15564cf49a43SJulian Elischer
15574cf49a43SJulian Elischer /*
15584cf49a43SJulian Elischer * Packets coming from the hook make no sense
155998e7b753SAlexander Motin * to sessions in the rest of states. Throw them away.
15604cf49a43SJulian Elischer */
15614cf49a43SJulian Elischer default:
15624cf49a43SJulian Elischer LEAVE(ENETUNREACH);
15634cf49a43SJulian Elischer }
15644cf49a43SJulian Elischer quit:
1565f5856029SJulian Elischer if (item)
1566069154d5SJulian Elischer NG_FREE_ITEM(item);
1567069154d5SJulian Elischer NG_FREE_M(m);
156898e7b753SAlexander Motin return (error);
156998e7b753SAlexander Motin }
157098e7b753SAlexander Motin
157198e7b753SAlexander Motin /*
157298e7b753SAlexander Motin * Receive data from ether and do something with it.
157398e7b753SAlexander Motin */
157498e7b753SAlexander Motin static int
ng_pppoe_rcvdata_ether(hook_p hook,item_p item)157598e7b753SAlexander Motin ng_pppoe_rcvdata_ether(hook_p hook, item_p item)
157698e7b753SAlexander Motin {
157798e7b753SAlexander Motin node_p node = NG_HOOK_NODE(hook);
157898e7b753SAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
15795a73d193SAlexander Motin sessp sp;
158098e7b753SAlexander Motin const struct pppoe_tag *utag = NULL, *tag = NULL;
15818be8c756SEugene Grosbein const struct pppoe_tag sntag = { PTT_SRV_NAME, 0 };
158298e7b753SAlexander Motin const struct pppoe_full_hdr *wh;
158398e7b753SAlexander Motin const struct pppoe_hdr *ph;
158498e7b753SAlexander Motin negp neg = NULL;
158598e7b753SAlexander Motin struct mbuf *m;
158698e7b753SAlexander Motin hook_p sendhook;
158798e7b753SAlexander Motin int error = 0;
158898e7b753SAlexander Motin uint16_t length;
158998e7b753SAlexander Motin uint8_t code;
159098e7b753SAlexander Motin struct mbuf *m0;
159198e7b753SAlexander Motin
159298e7b753SAlexander Motin CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
159398e7b753SAlexander Motin __func__, node->nd_ID, node, item, hook->hk_name, hook);
159498e7b753SAlexander Motin
159598e7b753SAlexander Motin NGI_GET_M(item, m);
159698e7b753SAlexander Motin /*
159798e7b753SAlexander Motin * Dig out various fields from the packet.
159898e7b753SAlexander Motin * Use them to decide where to send it.
159998e7b753SAlexander Motin */
160098e7b753SAlexander Motin privp->packets_in++;
160198e7b753SAlexander Motin if( m->m_len < sizeof(*wh)) {
160298e7b753SAlexander Motin m = m_pullup(m, sizeof(*wh)); /* Checks length */
160398e7b753SAlexander Motin if (m == NULL) {
160498e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: couldn't "
160598e7b753SAlexander Motin "m_pullup(wh)\n", node->nd_ID);
160698e7b753SAlexander Motin LEAVE(ENOBUFS);
160798e7b753SAlexander Motin }
160898e7b753SAlexander Motin }
160998e7b753SAlexander Motin wh = mtod(m, struct pppoe_full_hdr *);
161098e7b753SAlexander Motin length = ntohs(wh->ph.length);
161198e7b753SAlexander Motin switch(wh->eh.ether_type) {
161298e7b753SAlexander Motin case ETHERTYPE_PPPOE_3COM_DISC: /* fall through */
161398e7b753SAlexander Motin case ETHERTYPE_PPPOE_DISC:
161498e7b753SAlexander Motin /*
161598e7b753SAlexander Motin * We need to try to make sure that the tag area
161698e7b753SAlexander Motin * is contiguous, or we could wander off the end
161798e7b753SAlexander Motin * of a buffer and make a mess.
161898e7b753SAlexander Motin * (Linux wouldn't have this problem).
161998e7b753SAlexander Motin */
162098e7b753SAlexander Motin if (m->m_pkthdr.len <= MHLEN) {
162198e7b753SAlexander Motin if( m->m_len < m->m_pkthdr.len) {
162298e7b753SAlexander Motin m = m_pullup(m, m->m_pkthdr.len);
162398e7b753SAlexander Motin if (m == NULL) {
162498e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: "
162598e7b753SAlexander Motin "couldn't m_pullup(pkthdr)\n",
162698e7b753SAlexander Motin node->nd_ID);
162798e7b753SAlexander Motin LEAVE(ENOBUFS);
162898e7b753SAlexander Motin }
162998e7b753SAlexander Motin }
163098e7b753SAlexander Motin }
163198e7b753SAlexander Motin if (m->m_len != m->m_pkthdr.len) {
163298e7b753SAlexander Motin /*
163398e7b753SAlexander Motin * It's not all in one piece.
163498e7b753SAlexander Motin * We need to do extra work.
163598e7b753SAlexander Motin * Put it into a cluster.
163698e7b753SAlexander Motin */
163798e7b753SAlexander Motin struct mbuf *n;
1638eb1b1807SGleb Smirnoff n = m_dup(m, M_NOWAIT);
163998e7b753SAlexander Motin m_freem(m);
164098e7b753SAlexander Motin m = n;
164198e7b753SAlexander Motin if (m) {
164298e7b753SAlexander Motin /* just check we got a cluster */
164398e7b753SAlexander Motin if (m->m_len != m->m_pkthdr.len) {
164498e7b753SAlexander Motin m_freem(m);
164598e7b753SAlexander Motin m = NULL;
164698e7b753SAlexander Motin }
164798e7b753SAlexander Motin }
164898e7b753SAlexander Motin if (m == NULL) {
164998e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: packet "
165098e7b753SAlexander Motin "fragmented\n", node->nd_ID);
165198e7b753SAlexander Motin LEAVE(EMSGSIZE);
165298e7b753SAlexander Motin }
165398e7b753SAlexander Motin }
165498e7b753SAlexander Motin wh = mtod(m, struct pppoe_full_hdr *);
165598e7b753SAlexander Motin length = ntohs(wh->ph.length);
165698e7b753SAlexander Motin ph = &wh->ph;
165798e7b753SAlexander Motin code = wh->ph.code;
165898e7b753SAlexander Motin
165998e7b753SAlexander Motin switch(code) {
166098e7b753SAlexander Motin case PADI_CODE:
166198e7b753SAlexander Motin /*
166298e7b753SAlexander Motin * We are a server:
166398e7b753SAlexander Motin * Look for a hook with the required service and send
166498e7b753SAlexander Motin * the ENTIRE packet up there. It should come back to
166598e7b753SAlexander Motin * a new hook in PRIMED state. Look there for further
166698e7b753SAlexander Motin * processing.
166798e7b753SAlexander Motin */
166898e7b753SAlexander Motin tag = get_tag(ph, PTT_SRV_NAME);
16698be8c756SEugene Grosbein if (tag == NULL)
16708be8c756SEugene Grosbein tag = &sntag;
167198e7b753SAlexander Motin
167298e7b753SAlexander Motin /*
167398e7b753SAlexander Motin * First, try to match Service-Name against our
167498e7b753SAlexander Motin * listening hooks. If no success and we are in D-Link
167598e7b753SAlexander Motin * compat mode and Service-Name is empty, then we
167698e7b753SAlexander Motin * broadcast the PADI to all listening hooks.
167798e7b753SAlexander Motin */
167898e7b753SAlexander Motin sendhook = pppoe_match_svc(node, tag);
167998e7b753SAlexander Motin if (sendhook != NULL)
168098e7b753SAlexander Motin NG_FWD_NEW_DATA(error, item, sendhook, m);
168198e7b753SAlexander Motin else if (privp->flags & COMPAT_DLINK &&
168298e7b753SAlexander Motin ntohs(tag->tag_len) == 0)
168398e7b753SAlexander Motin error = pppoe_broadcast_padi(node, m);
168498e7b753SAlexander Motin else
168598e7b753SAlexander Motin error = ENETUNREACH;
168698e7b753SAlexander Motin break;
168798e7b753SAlexander Motin case PADO_CODE:
168898e7b753SAlexander Motin /*
168998e7b753SAlexander Motin * We are a client:
169098e7b753SAlexander Motin * Use the host_uniq tag to find the hook this is in
169198e7b753SAlexander Motin * response to. Received #2, now send #3
169298e7b753SAlexander Motin * For now simply accept the first we receive.
169398e7b753SAlexander Motin */
169498e7b753SAlexander Motin utag = get_tag(ph, PTT_HOST_UNIQ);
16958be8c756SEugene Grosbein if (utag == NULL) {
169698e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: no host "
169798e7b753SAlexander Motin "unique field\n", node->nd_ID);
169898e7b753SAlexander Motin LEAVE(ENETUNREACH);
169998e7b753SAlexander Motin }
170098e7b753SAlexander Motin
170198e7b753SAlexander Motin sendhook = pppoe_finduniq(node, utag);
170298e7b753SAlexander Motin if (sendhook == NULL) {
170398e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: no "
170498e7b753SAlexander Motin "matching session\n", node->nd_ID);
170598e7b753SAlexander Motin LEAVE(ENETUNREACH);
170698e7b753SAlexander Motin }
170798e7b753SAlexander Motin
170898e7b753SAlexander Motin /*
170998e7b753SAlexander Motin * Check the session is in the right state.
171098e7b753SAlexander Motin * It needs to be in PPPOE_SINIT.
171198e7b753SAlexander Motin */
171298e7b753SAlexander Motin sp = NG_HOOK_PRIVATE(sendhook);
1713098ff746SAlexander Motin if (sp->state == PPPOE_SREQ ||
1714098ff746SAlexander Motin sp->state == PPPOE_CONNECTED) {
1715098ff746SAlexander Motin break; /* Multiple PADO is OK. */
1716098ff746SAlexander Motin }
171798e7b753SAlexander Motin if (sp->state != PPPOE_SINIT) {
171898e7b753SAlexander Motin log(LOG_NOTICE, "ng_pppoe[%x]: session "
171998e7b753SAlexander Motin "in wrong state\n", node->nd_ID);
172098e7b753SAlexander Motin LEAVE(ENETUNREACH);
172198e7b753SAlexander Motin }
172298e7b753SAlexander Motin neg = sp->neg;
1723098ff746SAlexander Motin /* If requested specific AC-name, check it. */
1724098ff746SAlexander Motin if (neg->ac_name_len) {
1725098ff746SAlexander Motin tag = get_tag(ph, PTT_AC_NAME);
1726098ff746SAlexander Motin if (!tag) {
1727098ff746SAlexander Motin /* No PTT_AC_NAME in PADO */
1728098ff746SAlexander Motin break;
1729098ff746SAlexander Motin }
1730098ff746SAlexander Motin if (neg->ac_name_len != htons(tag->tag_len) ||
173160d234c5SEd Schouten strncmp(neg->ac_name.data,
173260d234c5SEd Schouten (const char *)(tag + 1),
1733098ff746SAlexander Motin neg->ac_name_len) != 0) {
1734098ff746SAlexander Motin break;
1735098ff746SAlexander Motin }
1736098ff746SAlexander Motin }
1737098ff746SAlexander Motin sp->state = PPPOE_SREQ;
173898e7b753SAlexander Motin ng_uncallout(&neg->handle, node);
173998e7b753SAlexander Motin
174098e7b753SAlexander Motin /*
174198e7b753SAlexander Motin * This is the first time we hear
174298e7b753SAlexander Motin * from the server, so note it's
174398e7b753SAlexander Motin * unicast address, replacing the
174498e7b753SAlexander Motin * broadcast address .
174598e7b753SAlexander Motin */
174698e7b753SAlexander Motin bcopy(wh->eh.ether_shost,
174798e7b753SAlexander Motin neg->pkt->pkt_header.eh.ether_dhost,
174898e7b753SAlexander Motin ETHER_ADDR_LEN);
174998e7b753SAlexander Motin neg->timeout = 0;
175098e7b753SAlexander Motin neg->pkt->pkt_header.ph.code = PADR_CODE;
175198e7b753SAlexander Motin init_tags(sp);
175298e7b753SAlexander Motin insert_tag(sp, utag); /* Host Unique */
175398e7b753SAlexander Motin if ((tag = get_tag(ph, PTT_AC_COOKIE)))
175498e7b753SAlexander Motin insert_tag(sp, tag); /* return cookie */
175598e7b753SAlexander Motin if ((tag = get_tag(ph, PTT_AC_NAME))) {
175698e7b753SAlexander Motin insert_tag(sp, tag); /* return it */
175798e7b753SAlexander Motin send_acname(sp, tag);
175898e7b753SAlexander Motin }
17595b363c09SAlexander Motin if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
17605b363c09SAlexander Motin (privp->max_payload.data != 0))
17615b363c09SAlexander Motin insert_tag(sp, tag); /* return it */
176298e7b753SAlexander Motin insert_tag(sp, &neg->service.hdr); /* Service */
176398e7b753SAlexander Motin scan_tags(sp, ph);
176498e7b753SAlexander Motin make_packet(sp);
176598e7b753SAlexander Motin sp->state = PPPOE_SREQ;
176698e7b753SAlexander Motin ng_callout(&neg->handle, node, sp->hook,
176798e7b753SAlexander Motin PPPOE_INITIAL_TIMEOUT * hz,
176898e7b753SAlexander Motin pppoe_ticker, NULL, 0);
176998e7b753SAlexander Motin neg->timeout = PPPOE_INITIAL_TIMEOUT * 2;
1770eb1b1807SGleb Smirnoff m0 = m_copypacket(neg->m, M_NOWAIT);
177198e7b753SAlexander Motin NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
177298e7b753SAlexander Motin break;
177398e7b753SAlexander Motin case PADR_CODE:
177498e7b753SAlexander Motin /*
177598e7b753SAlexander Motin * We are a server:
177698e7b753SAlexander Motin * Use the ac_cookie tag to find the
177798e7b753SAlexander Motin * hook this is in response to.
177898e7b753SAlexander Motin */
177998e7b753SAlexander Motin utag = get_tag(ph, PTT_AC_COOKIE);
178098e7b753SAlexander Motin if ((utag == NULL) ||
178198e7b753SAlexander Motin (ntohs(utag->tag_len) != sizeof(sp))) {
178298e7b753SAlexander Motin LEAVE(ENETUNREACH);
178398e7b753SAlexander Motin }
178498e7b753SAlexander Motin
17858be8c756SEugene Grosbein sendhook = pppoe_findcookie(node, utag);
178698e7b753SAlexander Motin if (sendhook == NULL)
178798e7b753SAlexander Motin LEAVE(ENETUNREACH);
178898e7b753SAlexander Motin
178998e7b753SAlexander Motin /*
179098e7b753SAlexander Motin * Check the session is in the right state.
179198e7b753SAlexander Motin * It needs to be in PPPOE_SOFFER or PPPOE_NEWCONNECTED.
179298e7b753SAlexander Motin * If the latter, then this is a retry by the client,
179398e7b753SAlexander Motin * so be nice, and resend.
179498e7b753SAlexander Motin */
179598e7b753SAlexander Motin sp = NG_HOOK_PRIVATE(sendhook);
179698e7b753SAlexander Motin if (sp->state == PPPOE_NEWCONNECTED) {
179798e7b753SAlexander Motin /*
179898e7b753SAlexander Motin * Whoa! drop back to resend that PADS packet.
179998e7b753SAlexander Motin * We should still have a copy of it.
180098e7b753SAlexander Motin */
180198e7b753SAlexander Motin sp->state = PPPOE_SOFFER;
1802b2b5279bSAlexander Motin } else if (sp->state != PPPOE_SOFFER)
180398e7b753SAlexander Motin LEAVE (ENETUNREACH);
180498e7b753SAlexander Motin neg = sp->neg;
180598e7b753SAlexander Motin ng_uncallout(&neg->handle, node);
180698e7b753SAlexander Motin neg->pkt->pkt_header.ph.code = PADS_CODE;
1807b2b5279bSAlexander Motin if (sp->Session_ID == 0) {
180898e7b753SAlexander Motin neg->pkt->pkt_header.ph.sid =
1809bd500dabSAlexander Motin htons(pppoe_getnewsession(sp));
1810b2b5279bSAlexander Motin }
181198e7b753SAlexander Motin send_sessionid(sp);
181298e7b753SAlexander Motin neg->timeout = 0;
181398e7b753SAlexander Motin /*
181498e7b753SAlexander Motin * start working out the tags to respond with.
181598e7b753SAlexander Motin */
181698e7b753SAlexander Motin init_tags(sp);
181798e7b753SAlexander Motin insert_tag(sp, &neg->ac_name.hdr); /* AC_NAME */
181898e7b753SAlexander Motin if ((tag = get_tag(ph, PTT_SRV_NAME)))
181998e7b753SAlexander Motin insert_tag(sp, tag);/* return service */
182098e7b753SAlexander Motin if ((tag = get_tag(ph, PTT_HOST_UNIQ)))
182198e7b753SAlexander Motin insert_tag(sp, tag); /* return it */
182298e7b753SAlexander Motin insert_tag(sp, utag); /* ac_cookie */
182398e7b753SAlexander Motin scan_tags(sp, ph);
182498e7b753SAlexander Motin make_packet(sp);
182598e7b753SAlexander Motin sp->state = PPPOE_NEWCONNECTED;
182698e7b753SAlexander Motin
182798e7b753SAlexander Motin /* Send the PADS without a timeout - we're now connected. */
1828eb1b1807SGleb Smirnoff m0 = m_copypacket(sp->neg->m, M_NOWAIT);
182998e7b753SAlexander Motin NG_FWD_NEW_DATA(error, item, privp->ethernet_hook, m0);
183098e7b753SAlexander Motin
183198e7b753SAlexander Motin /*
183298e7b753SAlexander Motin * Having sent the last Negotiation header,
183398e7b753SAlexander Motin * Set up the stored packet header to be correct for
183498e7b753SAlexander Motin * the actual session. But keep the negotialtion stuff
183598e7b753SAlexander Motin * around in case we need to resend this last packet.
183698e7b753SAlexander Motin * We'll discard it when we move from NEWCONNECTED
183798e7b753SAlexander Motin * to CONNECTED
183898e7b753SAlexander Motin */
183998e7b753SAlexander Motin sp->pkt_hdr = neg->pkt->pkt_header;
184098e7b753SAlexander Motin /* Configure ethertype depending on what
184198e7b753SAlexander Motin * ethertype was used at discovery phase */
184298e7b753SAlexander Motin if (sp->pkt_hdr.eh.ether_type ==
184398e7b753SAlexander Motin ETHERTYPE_PPPOE_3COM_DISC)
184498e7b753SAlexander Motin sp->pkt_hdr.eh.ether_type
184598e7b753SAlexander Motin = ETHERTYPE_PPPOE_3COM_SESS;
184698e7b753SAlexander Motin else
184798e7b753SAlexander Motin sp->pkt_hdr.eh.ether_type
184898e7b753SAlexander Motin = ETHERTYPE_PPPOE_SESS;
184998e7b753SAlexander Motin sp->pkt_hdr.ph.code = 0;
185098e7b753SAlexander Motin pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
185198e7b753SAlexander Motin break;
185298e7b753SAlexander Motin case PADS_CODE:
185398e7b753SAlexander Motin /*
185498e7b753SAlexander Motin * We are a client:
185598e7b753SAlexander Motin * Use the host_uniq tag to find the hook this is in
185698e7b753SAlexander Motin * response to. Take the session ID and store it away.
185798e7b753SAlexander Motin * Also make sure the pre-made header is correct and
185898e7b753SAlexander Motin * set us into Session mode.
185998e7b753SAlexander Motin */
186098e7b753SAlexander Motin utag = get_tag(ph, PTT_HOST_UNIQ);
18618be8c756SEugene Grosbein if (utag == NULL) {
186298e7b753SAlexander Motin LEAVE (ENETUNREACH);
186398e7b753SAlexander Motin }
186498e7b753SAlexander Motin sendhook = pppoe_finduniq(node, utag);
186598e7b753SAlexander Motin if (sendhook == NULL)
186698e7b753SAlexander Motin LEAVE(ENETUNREACH);
186798e7b753SAlexander Motin
186898e7b753SAlexander Motin /*
186998e7b753SAlexander Motin * Check the session is in the right state.
187098e7b753SAlexander Motin * It needs to be in PPPOE_SREQ.
187198e7b753SAlexander Motin */
187298e7b753SAlexander Motin sp = NG_HOOK_PRIVATE(sendhook);
187398e7b753SAlexander Motin if (sp->state != PPPOE_SREQ)
187498e7b753SAlexander Motin LEAVE(ENETUNREACH);
187598e7b753SAlexander Motin neg = sp->neg;
187698e7b753SAlexander Motin ng_uncallout(&neg->handle, node);
187798e7b753SAlexander Motin neg->pkt->pkt_header.ph.sid = wh->ph.sid;
187898e7b753SAlexander Motin sp->Session_ID = ntohs(wh->ph.sid);
1879b2b5279bSAlexander Motin pppoe_addsession(sp);
188098e7b753SAlexander Motin send_sessionid(sp);
188198e7b753SAlexander Motin neg->timeout = 0;
188298e7b753SAlexander Motin sp->state = PPPOE_CONNECTED;
188398e7b753SAlexander Motin /*
188498e7b753SAlexander Motin * Now we have gone to Connected mode,
188598e7b753SAlexander Motin * Free all resources needed for negotiation.
188698e7b753SAlexander Motin * Keep a copy of the header we will be using.
188798e7b753SAlexander Motin */
188898e7b753SAlexander Motin sp->pkt_hdr = neg->pkt->pkt_header;
188998e7b753SAlexander Motin if (privp->flags & COMPAT_3COM)
189098e7b753SAlexander Motin sp->pkt_hdr.eh.ether_type
189198e7b753SAlexander Motin = ETHERTYPE_PPPOE_3COM_SESS;
189298e7b753SAlexander Motin else
189398e7b753SAlexander Motin sp->pkt_hdr.eh.ether_type
189498e7b753SAlexander Motin = ETHERTYPE_PPPOE_SESS;
189598e7b753SAlexander Motin sp->pkt_hdr.ph.code = 0;
189698e7b753SAlexander Motin m_freem(neg->m);
189798e7b753SAlexander Motin free(sp->neg, M_NETGRAPH_PPPOE);
189898e7b753SAlexander Motin sp->neg = NULL;
18995b363c09SAlexander Motin if ((tag = get_tag(ph, PTT_MAX_PAYL)) &&
19005b363c09SAlexander Motin (privp->max_payload.data != 0))
19015b363c09SAlexander Motin send_maxp(sp, tag);
190298e7b753SAlexander Motin pppoe_send_event(sp, NGM_PPPOE_SUCCESS);
190398e7b753SAlexander Motin break;
190498e7b753SAlexander Motin case PADT_CODE:
190598e7b753SAlexander Motin /*
190698e7b753SAlexander Motin * Find matching peer/session combination.
190798e7b753SAlexander Motin */
1908b2b5279bSAlexander Motin sp = pppoe_findsession(privp, wh);
1909b2b5279bSAlexander Motin if (sp == NULL)
191098e7b753SAlexander Motin LEAVE(ENETUNREACH);
191198e7b753SAlexander Motin /* Disconnect that hook. */
1912b2b5279bSAlexander Motin ng_rmhook_self(sp->hook);
191398e7b753SAlexander Motin break;
19148be8c756SEugene Grosbein case PADM_CODE:
19158be8c756SEugene Grosbein /*
19168be8c756SEugene Grosbein * We are a client:
19178be8c756SEugene Grosbein * find matching peer/session combination.
19188be8c756SEugene Grosbein */
19198be8c756SEugene Grosbein sp = pppoe_findsession(privp, wh);
19208be8c756SEugene Grosbein if (sp == NULL)
19218be8c756SEugene Grosbein LEAVE (ENETUNREACH);
19228be8c756SEugene Grosbein if ((tag = get_tag(ph, PTT_HURL)))
19238be8c756SEugene Grosbein send_hurl(sp, tag);
19248be8c756SEugene Grosbein if ((tag = get_tag(ph, PTT_MOTM)))
19258be8c756SEugene Grosbein send_motm(sp, tag);
19268be8c756SEugene Grosbein break;
192798e7b753SAlexander Motin default:
192898e7b753SAlexander Motin LEAVE(EPFNOSUPPORT);
192998e7b753SAlexander Motin }
193098e7b753SAlexander Motin break;
193198e7b753SAlexander Motin case ETHERTYPE_PPPOE_3COM_SESS:
193298e7b753SAlexander Motin case ETHERTYPE_PPPOE_SESS:
193398e7b753SAlexander Motin /*
193498e7b753SAlexander Motin * Find matching peer/session combination.
193598e7b753SAlexander Motin */
1936b2b5279bSAlexander Motin sp = pppoe_findsession(privp, wh);
1937b2b5279bSAlexander Motin if (sp == NULL)
193898e7b753SAlexander Motin LEAVE (ENETUNREACH);
193998e7b753SAlexander Motin m_adj(m, sizeof(*wh));
194098e7b753SAlexander Motin
194198e7b753SAlexander Motin /* If packet too short, dump it. */
194298e7b753SAlexander Motin if (m->m_pkthdr.len < length)
194398e7b753SAlexander Motin LEAVE(EMSGSIZE);
194498e7b753SAlexander Motin /* Also need to trim excess at the end */
194598e7b753SAlexander Motin if (m->m_pkthdr.len > length) {
194698e7b753SAlexander Motin m_adj(m, -((int)(m->m_pkthdr.len - length)));
194798e7b753SAlexander Motin }
194898e7b753SAlexander Motin if ( sp->state != PPPOE_CONNECTED) {
194998e7b753SAlexander Motin if (sp->state == PPPOE_NEWCONNECTED) {
195098e7b753SAlexander Motin sp->state = PPPOE_CONNECTED;
195198e7b753SAlexander Motin /*
195298e7b753SAlexander Motin * Now we have gone to Connected mode,
195398e7b753SAlexander Motin * Free all resources needed for negotiation.
195498e7b753SAlexander Motin * Be paranoid about whether there may be
195598e7b753SAlexander Motin * a timeout.
195698e7b753SAlexander Motin */
195798e7b753SAlexander Motin m_freem(sp->neg->m);
195898e7b753SAlexander Motin ng_uncallout(&sp->neg->handle, node);
195998e7b753SAlexander Motin free(sp->neg, M_NETGRAPH_PPPOE);
196098e7b753SAlexander Motin sp->neg = NULL;
196198e7b753SAlexander Motin } else {
196298e7b753SAlexander Motin LEAVE (ENETUNREACH);
196398e7b753SAlexander Motin }
196498e7b753SAlexander Motin }
1965b2b5279bSAlexander Motin NG_FWD_NEW_DATA(error, item, sp->hook, m);
196698e7b753SAlexander Motin break;
196798e7b753SAlexander Motin default:
196898e7b753SAlexander Motin LEAVE(EPFNOSUPPORT);
196998e7b753SAlexander Motin }
197098e7b753SAlexander Motin quit:
197198e7b753SAlexander Motin if (item)
197298e7b753SAlexander Motin NG_FREE_ITEM(item);
197398e7b753SAlexander Motin NG_FREE_M(m);
197498e7b753SAlexander Motin return (error);
197598e7b753SAlexander Motin }
197698e7b753SAlexander Motin
197798e7b753SAlexander Motin /*
197898e7b753SAlexander Motin * Receive data from debug hook and bypass it to ether.
197998e7b753SAlexander Motin */
198098e7b753SAlexander Motin static int
ng_pppoe_rcvdata_debug(hook_p hook,item_p item)198198e7b753SAlexander Motin ng_pppoe_rcvdata_debug(hook_p hook, item_p item)
198298e7b753SAlexander Motin {
198398e7b753SAlexander Motin node_p node = NG_HOOK_NODE(hook);
198498e7b753SAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
198598e7b753SAlexander Motin int error;
198698e7b753SAlexander Motin
198798e7b753SAlexander Motin CTR6(KTR_NET, "%20s: node [%x] (%p) received %p on \"%s\" (%p)",
198898e7b753SAlexander Motin __func__, node->nd_ID, node, item, hook->hk_name, hook);
198998e7b753SAlexander Motin
199098e7b753SAlexander Motin NG_FWD_ITEM_HOOK(error, item, privp->ethernet_hook);
199198e7b753SAlexander Motin privp->packets_out++;
199298e7b753SAlexander Motin return (error);
19934cf49a43SJulian Elischer }
19944cf49a43SJulian Elischer
19954cf49a43SJulian Elischer /*
19964cf49a43SJulian Elischer * Do local shutdown processing..
1997053359b7SPedro F. Giffuni * If we are a persistent device, we might refuse to go away, and
19984cf49a43SJulian Elischer * we'd only remove our links and reset ourself.
19994cf49a43SJulian Elischer */
20004cf49a43SJulian Elischer static int
ng_pppoe_shutdown(node_p node)2001069154d5SJulian Elischer ng_pppoe_shutdown(node_p node)
20024cf49a43SJulian Elischer {
2003b2b5279bSAlexander Motin const priv_p privp = NG_NODE_PRIVATE(node);
2004b2b5279bSAlexander Motin int i;
20054cf49a43SJulian Elischer
2006b2b5279bSAlexander Motin for (i = 0; i < SESSHASHSIZE; i++)
2007b2b5279bSAlexander Motin mtx_destroy(&privp->sesshash[i].mtx);
200830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL);
2009b2b5279bSAlexander Motin NG_NODE_UNREF(privp->node);
2010b2b5279bSAlexander Motin free(privp, M_NETGRAPH_PPPOE);
20114cf49a43SJulian Elischer return (0);
20124cf49a43SJulian Elischer }
20134cf49a43SJulian Elischer
20144cf49a43SJulian Elischer /*
20154cf49a43SJulian Elischer * Hook disconnection
20164cf49a43SJulian Elischer *
20176faf164cSJulian Elischer * Clean up all dangling links and information about the session/hook.
20187762e8c6SGleb Smirnoff * For this type, removal of the last link destroys the node.
20194cf49a43SJulian Elischer */
20204cf49a43SJulian Elischer static int
ng_pppoe_disconnect(hook_p hook)20218876b55dSJulian Elischer ng_pppoe_disconnect(hook_p hook)
20224cf49a43SJulian Elischer {
202330400f03SJulian Elischer node_p node = NG_HOOK_NODE(hook);
202430400f03SJulian Elischer priv_p privp = NG_NODE_PRIVATE(node);
20254cf49a43SJulian Elischer sessp sp;
20264cf49a43SJulian Elischer
202799f4de90SAlexander Motin if (hook == privp->debug_hook) {
20284cf49a43SJulian Elischer privp->debug_hook = NULL;
202999f4de90SAlexander Motin } else if (hook == privp->ethernet_hook) {
20304cf49a43SJulian Elischer privp->ethernet_hook = NULL;
203130400f03SJulian Elischer if (NG_NODE_IS_VALID(node))
2032069154d5SJulian Elischer ng_rmnode_self(node);
20334cf49a43SJulian Elischer } else {
203430400f03SJulian Elischer sp = NG_HOOK_PRIVATE(hook);
2035b58a8a3bSJulian Elischer if (sp->state != PPPOE_SNONE ) {
2036b58a8a3bSJulian Elischer pppoe_send_event(sp, NGM_PPPOE_CLOSE);
2037b58a8a3bSJulian Elischer }
2038a4ec03cfSJulian Elischer /*
2039a4ec03cfSJulian Elischer * According to the spec, if we are connected,
2040a4ec03cfSJulian Elischer * we should send a DISC packet if we are shutting down
2041a4ec03cfSJulian Elischer * a session.
2042a4ec03cfSJulian Elischer */
20439fcb3d83SJulian Elischer if ((privp->ethernet_hook)
20449fcb3d83SJulian Elischer && ((sp->state == PPPOE_CONNECTED)
20459fcb3d83SJulian Elischer || (sp->state == PPPOE_NEWCONNECTED))) {
20469fcb3d83SJulian Elischer struct mbuf *m;
20479fcb3d83SJulian Elischer
20487762e8c6SGleb Smirnoff /* Generate a packet of that type. */
20498be8c756SEugene Grosbein m = m_gethdr(M_NOWAIT, MT_DATA);
20506faf164cSJulian Elischer if (m == NULL)
20512e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe[%x]: session out of "
20522e87c3ccSGleb Smirnoff "mbufs\n", node->nd_ID);
20536faf164cSJulian Elischer else {
2054b27e6e91SAleksandr Fedorov struct epoch_tracker et;
20551e7d84b0SAlexander Motin struct pppoe_full_hdr *wh;
20561e7d84b0SAlexander Motin struct pppoe_tag *tag;
20571e7d84b0SAlexander Motin int msglen = strlen(SIGNOFF);
20581e7d84b0SAlexander Motin int error = 0;
20591e7d84b0SAlexander Motin
20601e7d84b0SAlexander Motin wh = mtod(m, struct pppoe_full_hdr *);
20611e7d84b0SAlexander Motin bcopy(&sp->pkt_hdr, wh, sizeof(*wh));
20621e7d84b0SAlexander Motin
20631e7d84b0SAlexander Motin /* Revert the stored header to DISC/PADT mode. */
20641e7d84b0SAlexander Motin wh->ph.code = PADT_CODE;
20651e7d84b0SAlexander Motin /*
20661e7d84b0SAlexander Motin * Configure ethertype depending on what
20671e7d84b0SAlexander Motin * was used during sessions stage.
20681e7d84b0SAlexander Motin */
20691e7d84b0SAlexander Motin if (wh->eh.ether_type ==
20701e7d84b0SAlexander Motin ETHERTYPE_PPPOE_3COM_SESS)
20711e7d84b0SAlexander Motin wh->eh.ether_type = ETHERTYPE_PPPOE_3COM_DISC;
20721e7d84b0SAlexander Motin else
20731e7d84b0SAlexander Motin wh->eh.ether_type = ETHERTYPE_PPPOE_DISC;
20746faf164cSJulian Elischer /*
20756faf164cSJulian Elischer * Add a General error message and adjust
20767762e8c6SGleb Smirnoff * sizes.
20776faf164cSJulian Elischer */
207860d234c5SEd Schouten tag = (void *)(&wh->ph + 1);
20799fcb3d83SJulian Elischer tag->tag_type = PTT_GEN_ERR;
20809fcb3d83SJulian Elischer tag->tag_len = htons((u_int16_t)msglen);
208160d234c5SEd Schouten strncpy((char *)(tag + 1), SIGNOFF, msglen);
20828be8c756SEugene Grosbein m->m_pkthdr.len = m->m_len = sizeof(*wh) + sizeof(*tag) +
20838be8c756SEugene Grosbein msglen;
20849fcb3d83SJulian Elischer wh->ph.length = htons(sizeof(*tag) + msglen);
2085b27e6e91SAleksandr Fedorov
2086b27e6e91SAleksandr Fedorov NET_EPOCH_ENTER(et);
2087069154d5SJulian Elischer NG_SEND_DATA_ONLY(error,
2088069154d5SJulian Elischer privp->ethernet_hook, m);
2089b27e6e91SAleksandr Fedorov NET_EPOCH_EXIT(et);
20906faf164cSJulian Elischer }
20919fcb3d83SJulian Elischer }
2092dda30f12SAlexander Motin if (sp->state == PPPOE_LISTENING)
2093dda30f12SAlexander Motin LIST_REMOVE(sp, sessions);
2094dda30f12SAlexander Motin else if (sp->Session_ID)
2095b2b5279bSAlexander Motin pppoe_delsession(sp);
2096a4ec03cfSJulian Elischer /*
2097514baf3fSJeroen Ruigrok van der Werven * As long as we have somewhere to store the timeout handle,
2098a4ec03cfSJulian Elischer * we may have a timeout pending.. get rid of it.
2099a4ec03cfSJulian Elischer */
21001e2510f8SJulian Elischer if (sp->neg) {
2101ef237c7fSGleb Smirnoff ng_uncallout(&sp->neg->handle, node);
21021e2510f8SJulian Elischer if (sp->neg->m)
21031e2510f8SJulian Elischer m_freem(sp->neg->m);
21047762e8c6SGleb Smirnoff free(sp->neg, M_NETGRAPH_PPPOE);
21051e2510f8SJulian Elischer }
21067762e8c6SGleb Smirnoff free(sp, M_NETGRAPH_PPPOE);
210730400f03SJulian Elischer NG_HOOK_SET_PRIVATE(hook, NULL);
21084cf49a43SJulian Elischer }
21097762e8c6SGleb Smirnoff if ((NG_NODE_NUMHOOKS(node) == 0) &&
21107762e8c6SGleb Smirnoff (NG_NODE_IS_VALID(node)))
2111069154d5SJulian Elischer ng_rmnode_self(node);
21124cf49a43SJulian Elischer return (0);
21134cf49a43SJulian Elischer }
21144cf49a43SJulian Elischer
21154cf49a43SJulian Elischer /*
21167762e8c6SGleb Smirnoff * Timeouts come here.
21174cf49a43SJulian Elischer */
21184cf49a43SJulian Elischer static void
pppoe_ticker(node_p node,hook_p hook,void * arg1,int arg2)2119ef237c7fSGleb Smirnoff pppoe_ticker(node_p node, hook_p hook, void *arg1, int arg2)
21204cf49a43SJulian Elischer {
21217762e8c6SGleb Smirnoff priv_p privp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
212230400f03SJulian Elischer sessp sp = NG_HOOK_PRIVATE(hook);
21234cf49a43SJulian Elischer negp neg = sp->neg;
21244cf49a43SJulian Elischer struct mbuf *m0 = NULL;
21257762e8c6SGleb Smirnoff int error = 0;
21264cf49a43SJulian Elischer
21277762e8c6SGleb Smirnoff CTR6(KTR_NET, "%20s: node [%x] (%p) hook \"%s\" (%p) session %d",
21287762e8c6SGleb Smirnoff __func__, node->nd_ID, node, hook->hk_name, hook, sp->Session_ID);
21294cf49a43SJulian Elischer switch(sp->state) {
21304cf49a43SJulian Elischer /*
21317762e8c6SGleb Smirnoff * Resend the last packet, using an exponential backoff.
21324cf49a43SJulian Elischer * After a period of time, stop growing the backoff,
21337762e8c6SGleb Smirnoff * And either leave it, or revert to the start.
21344cf49a43SJulian Elischer */
21354cf49a43SJulian Elischer case PPPOE_SINIT:
21364cf49a43SJulian Elischer case PPPOE_SREQ:
21377762e8c6SGleb Smirnoff /* Timeouts on these produce resends. */
2138eb1b1807SGleb Smirnoff m0 = m_copypacket(sp->neg->m, M_NOWAIT);
2139069154d5SJulian Elischer NG_SEND_DATA_ONLY( error, privp->ethernet_hook, m0);
2140ef237c7fSGleb Smirnoff ng_callout(&neg->handle, node, hook, neg->timeout * hz,
2141ef237c7fSGleb Smirnoff pppoe_ticker, NULL, 0);
21424cf49a43SJulian Elischer if ((neg->timeout <<= 1) > PPPOE_TIMEOUT_LIMIT) {
21434cf49a43SJulian Elischer if (sp->state == PPPOE_SREQ) {
21447762e8c6SGleb Smirnoff /* Revert to SINIT mode. */
2145b58a8a3bSJulian Elischer pppoe_start(sp);
21464cf49a43SJulian Elischer } else {
21474cf49a43SJulian Elischer neg->timeout = PPPOE_TIMEOUT_LIMIT;
21484cf49a43SJulian Elischer }
21494cf49a43SJulian Elischer }
21504cf49a43SJulian Elischer break;
21514cf49a43SJulian Elischer case PPPOE_PRIMED:
21524cf49a43SJulian Elischer case PPPOE_SOFFER:
21537762e8c6SGleb Smirnoff /* A timeout on these says "give up" */
2154954c4772SJulian Elischer ng_rmhook_self(hook);
21554cf49a43SJulian Elischer break;
21564cf49a43SJulian Elischer default:
21577762e8c6SGleb Smirnoff /* Timeouts have no meaning in other states. */
21582e87c3ccSGleb Smirnoff log(LOG_NOTICE, "ng_pppoe[%x]: unexpected timeout\n",
21592e87c3ccSGleb Smirnoff node->nd_ID);
21604cf49a43SJulian Elischer }
21614cf49a43SJulian Elischer }
21624cf49a43SJulian Elischer
21634cf49a43SJulian Elischer /*
21644cf49a43SJulian Elischer * Parse an incoming packet to see if any tags should be copied to the
21654adb13fdSJulian Elischer * output packet. Don't do any tags that have been handled in the main
21664adb13fdSJulian Elischer * state machine.
21674cf49a43SJulian Elischer */
2168816b834fSArchie Cobbs static const struct pppoe_tag*
scan_tags(sessp sp,const struct pppoe_hdr * ph)2169816b834fSArchie Cobbs scan_tags(sessp sp, const struct pppoe_hdr* ph)
21704cf49a43SJulian Elischer {
2171816b834fSArchie Cobbs const char *const end = (const char *)next_tag(ph);
2172816b834fSArchie Cobbs const char *ptn;
217360d234c5SEd Schouten const struct pppoe_tag *pt = (const void *)(ph + 1);
21747762e8c6SGleb Smirnoff
21754cf49a43SJulian Elischer /*
21764cf49a43SJulian Elischer * Keep processing tags while a tag header will still fit.
21774cf49a43SJulian Elischer */
21787762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
21797762e8c6SGleb Smirnoff
2180816b834fSArchie Cobbs while((const char*)(pt + 1) <= end) {
21814cf49a43SJulian Elischer /*
21824cf49a43SJulian Elischer * If the tag data would go past the end of the packet, abort.
21834cf49a43SJulian Elischer */
2184816b834fSArchie Cobbs ptn = (((const char *)(pt + 1)) + ntohs(pt->tag_len));
21854cf49a43SJulian Elischer if(ptn > end)
21864cf49a43SJulian Elischer return NULL;
21874cf49a43SJulian Elischer
21884cf49a43SJulian Elischer switch (pt->tag_type) {
21894cf49a43SJulian Elischer case PTT_RELAY_SID:
21904cf49a43SJulian Elischer insert_tag(sp, pt);
21914cf49a43SJulian Elischer break;
21924cf49a43SJulian Elischer case PTT_EOL:
21934cf49a43SJulian Elischer return NULL;
21944cf49a43SJulian Elischer case PTT_SRV_NAME:
21954cf49a43SJulian Elischer case PTT_AC_NAME:
21964cf49a43SJulian Elischer case PTT_HOST_UNIQ:
21974cf49a43SJulian Elischer case PTT_AC_COOKIE:
21984cf49a43SJulian Elischer case PTT_VENDOR:
21994cf49a43SJulian Elischer case PTT_SRV_ERR:
22004cf49a43SJulian Elischer case PTT_SYS_ERR:
22014cf49a43SJulian Elischer case PTT_GEN_ERR:
22025c6d5d55SGleb Smirnoff case PTT_MAX_PAYL:
22038be8c756SEugene Grosbein case PTT_HURL:
22048be8c756SEugene Grosbein case PTT_MOTM:
22054cf49a43SJulian Elischer break;
22064cf49a43SJulian Elischer }
2207816b834fSArchie Cobbs pt = (const struct pppoe_tag*)ptn;
22084cf49a43SJulian Elischer }
22094cf49a43SJulian Elischer return NULL;
22104cf49a43SJulian Elischer }
22114cf49a43SJulian Elischer
2212b58a8a3bSJulian Elischer static int
pppoe_send_event(sessp sp,enum cmd cmdid)2213b58a8a3bSJulian Elischer pppoe_send_event(sessp sp, enum cmd cmdid)
2214b58a8a3bSJulian Elischer {
2215b58a8a3bSJulian Elischer int error;
2216b58a8a3bSJulian Elischer struct ng_mesg *msg;
22178876b55dSJulian Elischer struct ngpppoe_sts *sts;
2218b58a8a3bSJulian Elischer
22197762e8c6SGleb Smirnoff CTR2(KTR_NET, "%20s: called %d", __func__, sp->Session_ID);
22207762e8c6SGleb Smirnoff
222127121ab1SBrian Somers NG_MKMESSAGE(msg, NGM_PPPOE_COOKIE, cmdid,
22228876b55dSJulian Elischer sizeof(struct ngpppoe_sts), M_NOWAIT);
2223859a4d16SJulian Elischer if (msg == NULL)
2224859a4d16SJulian Elischer return (ENOMEM);
22258876b55dSJulian Elischer sts = (struct ngpppoe_sts *)msg->data;
222687e2c66aSHartmut Brandt strncpy(sts->hook, NG_HOOK_NAME(sp->hook), NG_HOOKSIZ);
2227facfd889SArchie Cobbs NG_SEND_MSG_ID(error, NG_HOOK_NODE(sp->hook), msg, sp->creator, 0);
2228b58a8a3bSJulian Elischer return (error);
2229b58a8a3bSJulian Elischer }
2230