xref: /freebsd/sys/netgraph/ng_l2tp.c (revision 89042ff77668555e77c88549e6ba697088ee72f9)
1c398230bSWarner Losh /*-
2901fadf7SArchie Cobbs  * Copyright (c) 2001-2002 Packet Design, LLC.
3901fadf7SArchie Cobbs  * All rights reserved.
4901fadf7SArchie Cobbs  *
5901fadf7SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty,
6901fadf7SArchie Cobbs  * use and redistribution of this software, in source or object code
7901fadf7SArchie Cobbs  * forms, with or without modifications are expressly permitted by
8901fadf7SArchie Cobbs  * Packet Design; provided, however, that:
9901fadf7SArchie Cobbs  *
10901fadf7SArchie Cobbs  *    (i)  Any and all reproductions of the source or object code
11901fadf7SArchie Cobbs  *         must include the copyright notice above and the following
12901fadf7SArchie Cobbs  *         disclaimer of warranties; and
13901fadf7SArchie Cobbs  *    (ii) No rights are granted, in any manner or form, to use
14901fadf7SArchie Cobbs  *         Packet Design trademarks, including the mark "PACKET DESIGN"
15901fadf7SArchie Cobbs  *         on advertising, endorsements, or otherwise except as such
16901fadf7SArchie Cobbs  *         appears in the above copyright notice or in the software.
17901fadf7SArchie Cobbs  *
18901fadf7SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
19901fadf7SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
20901fadf7SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
21901fadf7SArchie Cobbs  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
22901fadf7SArchie Cobbs  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
23901fadf7SArchie Cobbs  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
24901fadf7SArchie Cobbs  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
25901fadf7SArchie Cobbs  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
26901fadf7SArchie Cobbs  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
27901fadf7SArchie Cobbs  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
28901fadf7SArchie Cobbs  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
29901fadf7SArchie Cobbs  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
30901fadf7SArchie Cobbs  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
31901fadf7SArchie Cobbs  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
32901fadf7SArchie Cobbs  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33901fadf7SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34901fadf7SArchie Cobbs  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
35901fadf7SArchie Cobbs  * THE POSSIBILITY OF SUCH DAMAGE.
36901fadf7SArchie Cobbs  *
37901fadf7SArchie Cobbs  * Author: Archie Cobbs <archie@freebsd.org>
38901fadf7SArchie Cobbs  *
39901fadf7SArchie Cobbs  * $FreeBSD$
40901fadf7SArchie Cobbs  */
41901fadf7SArchie Cobbs 
42901fadf7SArchie Cobbs /*
43901fadf7SArchie Cobbs  * L2TP netgraph node type.
44901fadf7SArchie Cobbs  *
45901fadf7SArchie Cobbs  * This node type implements the lower layer of the
46901fadf7SArchie Cobbs  * L2TP protocol as specified in RFC 2661.
47901fadf7SArchie Cobbs  */
48901fadf7SArchie Cobbs 
49901fadf7SArchie Cobbs #include <sys/param.h>
50901fadf7SArchie Cobbs #include <sys/systm.h>
51901fadf7SArchie Cobbs #include <sys/kernel.h>
52901fadf7SArchie Cobbs #include <sys/time.h>
53901fadf7SArchie Cobbs #include <sys/conf.h>
54901fadf7SArchie Cobbs #include <sys/mbuf.h>
55901fadf7SArchie Cobbs #include <sys/malloc.h>
56901fadf7SArchie Cobbs #include <sys/errno.h>
5786fea6beSBosko Milekic #include <sys/libkern.h>
58901fadf7SArchie Cobbs 
59901fadf7SArchie Cobbs #include <netgraph/ng_message.h>
60901fadf7SArchie Cobbs #include <netgraph/netgraph.h>
61901fadf7SArchie Cobbs #include <netgraph/ng_parse.h>
62901fadf7SArchie Cobbs #include <netgraph/ng_l2tp.h>
63901fadf7SArchie Cobbs 
64901fadf7SArchie Cobbs #ifdef NG_SEPARATE_MALLOC
65d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
66901fadf7SArchie Cobbs #else
67901fadf7SArchie Cobbs #define M_NETGRAPH_L2TP M_NETGRAPH
68901fadf7SArchie Cobbs #endif
69901fadf7SArchie Cobbs 
70901fadf7SArchie Cobbs /* L2TP header format (first 2 bytes only) */
71901fadf7SArchie Cobbs #define L2TP_HDR_CTRL		0x8000			/* control packet */
72901fadf7SArchie Cobbs #define L2TP_HDR_LEN		0x4000			/* has length field */
73901fadf7SArchie Cobbs #define L2TP_HDR_SEQ		0x0800			/* has ns, nr fields */
74901fadf7SArchie Cobbs #define L2TP_HDR_OFF		0x0200			/* has offset field */
75901fadf7SArchie Cobbs #define L2TP_HDR_PRIO		0x0100			/* give priority */
76901fadf7SArchie Cobbs #define L2TP_HDR_VERS_MASK	0x000f			/* version field mask */
77901fadf7SArchie Cobbs #define L2TP_HDR_VERSION	0x0002			/* version field */
78901fadf7SArchie Cobbs 
79901fadf7SArchie Cobbs /* Bits that must be zero or one in first two bytes of header */
80901fadf7SArchie Cobbs #define L2TP_CTRL_0BITS		0x030d			/* ctrl: must be 0 */
81901fadf7SArchie Cobbs #define L2TP_CTRL_1BITS		0xc802			/* ctrl: must be 1 */
82901fadf7SArchie Cobbs #define L2TP_DATA_0BITS		0x800d			/* data: must be 0 */
83901fadf7SArchie Cobbs #define L2TP_DATA_1BITS		0x0002			/* data: must be 1 */
84901fadf7SArchie Cobbs 
85901fadf7SArchie Cobbs /* Standard xmit ctrl and data header bits */
86901fadf7SArchie Cobbs #define L2TP_CTRL_HDR		(L2TP_HDR_CTRL | L2TP_HDR_LEN \
87901fadf7SArchie Cobbs 				    | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
88901fadf7SArchie Cobbs #define L2TP_DATA_HDR		(L2TP_HDR_VERSION)	/* optional: len, seq */
89901fadf7SArchie Cobbs 
90901fadf7SArchie Cobbs /* Some hard coded values */
9152b9b77fSAlexander Motin #define L2TP_MAX_XWIN		128			/* my max xmit window */
92901fadf7SArchie Cobbs #define L2TP_MAX_REXMIT		5			/* default max rexmit */
93901fadf7SArchie Cobbs #define L2TP_MAX_REXMIT_TO	30			/* default rexmit to */
94901fadf7SArchie Cobbs #define L2TP_DELAYED_ACK	((hz + 19) / 20)	/* delayed ack: 50 ms */
95901fadf7SArchie Cobbs 
96901fadf7SArchie Cobbs /* Default data sequence number configuration for new sessions */
97901fadf7SArchie Cobbs #define L2TP_CONTROL_DSEQ	1			/* we are the lns */
98901fadf7SArchie Cobbs #define L2TP_ENABLE_DSEQ	1			/* enable data seq # */
99901fadf7SArchie Cobbs 
100901fadf7SArchie Cobbs /* Compare sequence numbers using circular math */
101e5d72e64SGleb Smirnoff #define L2TP_SEQ_DIFF(x, y)	((int16_t)((x) - (y)))
102901fadf7SArchie Cobbs 
1034e759763SAlexander Motin #define SESSHASHSIZE		0x0020
1044e759763SAlexander Motin #define SESSHASH(x)		(((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
1054e759763SAlexander Motin 
1064e759763SAlexander Motin /* Hook private data (data session hooks only) */
1074e759763SAlexander Motin struct ng_l2tp_hook_private {
1084e759763SAlexander Motin 	struct ng_l2tp_sess_config	conf;	/* hook/session config */
1094e759763SAlexander Motin 	struct ng_l2tp_session_stats	stats;	/* per sessions statistics */
1104e759763SAlexander Motin 	hook_p				hook;	/* hook reference */
1114e759763SAlexander Motin 	u_int16_t			ns;	/* data ns sequence number */
1124e759763SAlexander Motin 	u_int16_t			nr;	/* data nr sequence number */
1134e759763SAlexander Motin 	LIST_ENTRY(ng_l2tp_hook_private) sessions;
1144e759763SAlexander Motin };
1154e759763SAlexander Motin typedef struct ng_l2tp_hook_private *hookpriv_p;
1164e759763SAlexander Motin 
117901fadf7SArchie Cobbs /*
118901fadf7SArchie Cobbs  * Sequence number state
119901fadf7SArchie Cobbs  *
120901fadf7SArchie Cobbs  * Invariants:
121901fadf7SArchie Cobbs  *    - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
122901fadf7SArchie Cobbs  *    - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
123901fadf7SArchie Cobbs  *    - The first (ns - rack) mbuf's in xwin[] array are copies of these
124901fadf7SArchie Cobbs  *	unacknowledged packets; the remainder of xwin[] consists first of
125901fadf7SArchie Cobbs  *	zero or more further untransmitted packets in the transmit queue
126901fadf7SArchie Cobbs  *    - We try to keep the peer's receive window as full as possible.
127901fadf7SArchie Cobbs  *	Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
128901fadf7SArchie Cobbs  *    - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
129901fadf7SArchie Cobbs  *    - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
130901fadf7SArchie Cobbs  *    - xack_timer is running iff xack != nr (unack'd rec'd pkts)
131901fadf7SArchie Cobbs  */
132901fadf7SArchie Cobbs struct l2tp_seq {
133901fadf7SArchie Cobbs 	u_int16_t		ns;		/* next xmit seq we send */
134901fadf7SArchie Cobbs 	u_int16_t		nr;		/* next recv seq we expect */
135af63939cSAlexander Motin 	u_int16_t		inproc;		/* packet is in processing */
136901fadf7SArchie Cobbs 	u_int16_t		rack;		/* last 'nr' we rec'd */
137901fadf7SArchie Cobbs 	u_int16_t		xack;		/* last 'nr' we sent */
138901fadf7SArchie Cobbs 	u_int16_t		wmax;		/* peer's max recv window */
139901fadf7SArchie Cobbs 	u_int16_t		cwnd;		/* current congestion window */
140901fadf7SArchie Cobbs 	u_int16_t		ssth;		/* slow start threshold */
141901fadf7SArchie Cobbs 	u_int16_t		acks;		/* # consecutive acks rec'd */
142901fadf7SArchie Cobbs 	u_int16_t		rexmits;	/* # retransmits sent */
143901fadf7SArchie Cobbs 	struct callout		rack_timer;	/* retransmit timer */
144901fadf7SArchie Cobbs 	struct callout		xack_timer;	/* delayed ack timer */
145901fadf7SArchie Cobbs 	struct mbuf		*xwin[L2TP_MAX_XWIN];	/* transmit window */
146702f9895SAlexander Motin 	struct mtx		mtx;			/* seq mutex */
147901fadf7SArchie Cobbs };
148901fadf7SArchie Cobbs 
149901fadf7SArchie Cobbs /* Node private data */
150901fadf7SArchie Cobbs struct ng_l2tp_private {
151901fadf7SArchie Cobbs 	node_p			node;		/* back pointer to node */
152901fadf7SArchie Cobbs 	hook_p			ctrl;		/* hook to upper layers */
153901fadf7SArchie Cobbs 	hook_p			lower;		/* hook to lower layers */
154901fadf7SArchie Cobbs 	struct ng_l2tp_config	conf;		/* node configuration */
155901fadf7SArchie Cobbs 	struct ng_l2tp_stats	stats;		/* node statistics */
156901fadf7SArchie Cobbs 	struct l2tp_seq		seq;		/* ctrl sequence number state */
157901fadf7SArchie Cobbs 	ng_ID_t			ftarget;	/* failure message target */
1584e759763SAlexander Motin 	LIST_HEAD(, ng_l2tp_hook_private) sesshash[SESSHASHSIZE];
159901fadf7SArchie Cobbs };
160901fadf7SArchie Cobbs typedef struct ng_l2tp_private *priv_p;
161901fadf7SArchie Cobbs 
162901fadf7SArchie Cobbs /* Netgraph node methods */
163901fadf7SArchie Cobbs static ng_constructor_t	ng_l2tp_constructor;
164901fadf7SArchie Cobbs static ng_rcvmsg_t	ng_l2tp_rcvmsg;
165901fadf7SArchie Cobbs static ng_shutdown_t	ng_l2tp_shutdown;
166901fadf7SArchie Cobbs static ng_newhook_t	ng_l2tp_newhook;
167901fadf7SArchie Cobbs static ng_rcvdata_t	ng_l2tp_rcvdata;
168bf741e4dSAlexander Motin static ng_rcvdata_t	ng_l2tp_rcvdata_lower;
169bf741e4dSAlexander Motin static ng_rcvdata_t	ng_l2tp_rcvdata_ctrl;
170901fadf7SArchie Cobbs static ng_disconnect_t	ng_l2tp_disconnect;
171901fadf7SArchie Cobbs 
172901fadf7SArchie Cobbs /* Internal functions */
173901fadf7SArchie Cobbs static int	ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
174901fadf7SArchie Cobbs 
175901fadf7SArchie Cobbs static void	ng_l2tp_seq_init(priv_p priv);
1761e031324SBjoern A. Zeeb static int	ng_l2tp_seq_set(priv_p priv,
1771e031324SBjoern A. Zeeb 			const struct ng_l2tp_seq_config *conf);
178901fadf7SArchie Cobbs static int	ng_l2tp_seq_adjust(priv_p priv,
179901fadf7SArchie Cobbs 			const struct ng_l2tp_config *conf);
180901fadf7SArchie Cobbs static void	ng_l2tp_seq_reset(priv_p priv);
181901fadf7SArchie Cobbs static void	ng_l2tp_seq_failure(priv_p priv);
182901fadf7SArchie Cobbs static void	ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
1830ef8db8fSGleb Smirnoff static void	ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
1840ef8db8fSGleb Smirnoff 		    void *arg1, int arg2);
1850ef8db8fSGleb Smirnoff static void	ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
1860ef8db8fSGleb Smirnoff 		    void *arg1, int arg2);
187901fadf7SArchie Cobbs 
1884e759763SAlexander Motin static hookpriv_p	ng_l2tp_find_session(priv_p privp, u_int16_t sid);
189901fadf7SArchie Cobbs static ng_fn_eachhook	ng_l2tp_reset_session;
190901fadf7SArchie Cobbs 
1911e031324SBjoern A. Zeeb /* Parse type for struct ng_l2tp_seq_config. */
1921e031324SBjoern A. Zeeb static const struct ng_parse_struct_field
1931e031324SBjoern A. Zeeb 	ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
1941e031324SBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_seq_config_type = {
1951e031324SBjoern A. Zeeb 	&ng_parse_struct_type,
1961e031324SBjoern A. Zeeb 	&ng_l2tp_seq_config_fields
1971e031324SBjoern A. Zeeb };
1981e031324SBjoern A. Zeeb 
199901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_config */
200901fadf7SArchie Cobbs static const struct ng_parse_struct_field
201901fadf7SArchie Cobbs 	ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
202901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_config_type = {
203901fadf7SArchie Cobbs 	&ng_parse_struct_type,
204901fadf7SArchie Cobbs 	&ng_l2tp_config_type_fields,
205901fadf7SArchie Cobbs };
206901fadf7SArchie Cobbs 
207901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_sess_config */
208901fadf7SArchie Cobbs static const struct ng_parse_struct_field
209901fadf7SArchie Cobbs 	ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
210901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_sess_config_type = {
211901fadf7SArchie Cobbs 	&ng_parse_struct_type,
212901fadf7SArchie Cobbs 	&ng_l2tp_sess_config_type_fields,
213901fadf7SArchie Cobbs };
214901fadf7SArchie Cobbs 
215901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_stats */
216901fadf7SArchie Cobbs static const struct ng_parse_struct_field
217901fadf7SArchie Cobbs 	ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
2182c9027fcSArchie Cobbs static const struct ng_parse_type ng_l2tp_stats_type = {
219901fadf7SArchie Cobbs 	&ng_parse_struct_type,
220901fadf7SArchie Cobbs 	&ng_l2tp_stats_type_fields
221901fadf7SArchie Cobbs };
222901fadf7SArchie Cobbs 
2234807330cSBjoern A. Zeeb /* Parse type for struct ng_l2tp_session_stats. */
2244807330cSBjoern A. Zeeb static const struct ng_parse_struct_field
2254807330cSBjoern A. Zeeb 	ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
2264807330cSBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_session_stats_type = {
2274807330cSBjoern A. Zeeb 	&ng_parse_struct_type,
2284807330cSBjoern A. Zeeb 	&ng_l2tp_session_stats_type_fields
2294807330cSBjoern A. Zeeb };
2304807330cSBjoern A. Zeeb 
231901fadf7SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
232901fadf7SArchie Cobbs static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
233901fadf7SArchie Cobbs 	{
234901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
235901fadf7SArchie Cobbs 	  NGM_L2TP_SET_CONFIG,
236901fadf7SArchie Cobbs 	  "setconfig",
237901fadf7SArchie Cobbs 	  &ng_l2tp_config_type,
238901fadf7SArchie Cobbs 	  NULL
239901fadf7SArchie Cobbs 	},
240901fadf7SArchie Cobbs 	{
241901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
242901fadf7SArchie Cobbs 	  NGM_L2TP_GET_CONFIG,
243901fadf7SArchie Cobbs 	  "getconfig",
244901fadf7SArchie Cobbs 	  NULL,
245901fadf7SArchie Cobbs 	  &ng_l2tp_config_type
246901fadf7SArchie Cobbs 	},
247901fadf7SArchie Cobbs 	{
248901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
249901fadf7SArchie Cobbs 	  NGM_L2TP_SET_SESS_CONFIG,
250901fadf7SArchie Cobbs 	  "setsessconfig",
251901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type,
252901fadf7SArchie Cobbs 	  NULL
253901fadf7SArchie Cobbs 	},
254901fadf7SArchie Cobbs 	{
255901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
256901fadf7SArchie Cobbs 	  NGM_L2TP_GET_SESS_CONFIG,
257901fadf7SArchie Cobbs 	  "getsessconfig",
258901fadf7SArchie Cobbs 	  &ng_parse_hint16_type,
259901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type
260901fadf7SArchie Cobbs 	},
261901fadf7SArchie Cobbs 	{
262901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
263901fadf7SArchie Cobbs 	  NGM_L2TP_GET_STATS,
264901fadf7SArchie Cobbs 	  "getstats",
265901fadf7SArchie Cobbs 	  NULL,
2662c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
267901fadf7SArchie Cobbs 	},
268901fadf7SArchie Cobbs 	{
269901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
270901fadf7SArchie Cobbs 	  NGM_L2TP_CLR_STATS,
271901fadf7SArchie Cobbs 	  "clrstats",
272901fadf7SArchie Cobbs 	  NULL,
273901fadf7SArchie Cobbs 	  NULL
274901fadf7SArchie Cobbs 	},
275901fadf7SArchie Cobbs 	{
276901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
277901fadf7SArchie Cobbs 	  NGM_L2TP_GETCLR_STATS,
278901fadf7SArchie Cobbs 	  "getclrstats",
279901fadf7SArchie Cobbs 	  NULL,
2802c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
281901fadf7SArchie Cobbs 	},
282901fadf7SArchie Cobbs 	{
283901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
2844807330cSBjoern A. Zeeb 	  NGM_L2TP_GET_SESSION_STATS,
2854807330cSBjoern A. Zeeb 	  "getsessstats",
2864807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2874807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
2884807330cSBjoern A. Zeeb 	},
2894807330cSBjoern A. Zeeb 	{
2904807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
2914807330cSBjoern A. Zeeb 	  NGM_L2TP_CLR_SESSION_STATS,
2924807330cSBjoern A. Zeeb 	  "clrsessstats",
2934807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2944807330cSBjoern A. Zeeb 	  NULL
2954807330cSBjoern A. Zeeb 	},
2964807330cSBjoern A. Zeeb 	{
2974807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
2984807330cSBjoern A. Zeeb 	  NGM_L2TP_GETCLR_SESSION_STATS,
2994807330cSBjoern A. Zeeb 	  "getclrsessstats",
3004807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
3014807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
3024807330cSBjoern A. Zeeb 	},
3034807330cSBjoern A. Zeeb 	{
3044807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
305901fadf7SArchie Cobbs 	  NGM_L2TP_ACK_FAILURE,
306901fadf7SArchie Cobbs 	  "ackfailure",
307901fadf7SArchie Cobbs 	  NULL,
308901fadf7SArchie Cobbs 	  NULL
309901fadf7SArchie Cobbs 	},
3101e031324SBjoern A. Zeeb 	{
3111e031324SBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
3121e031324SBjoern A. Zeeb 	  NGM_L2TP_SET_SEQ,
3131e031324SBjoern A. Zeeb 	  "setsequence",
3141e031324SBjoern A. Zeeb 	  &ng_l2tp_seq_config_type,
3151e031324SBjoern A. Zeeb 	  NULL
3161e031324SBjoern A. Zeeb 	},
317901fadf7SArchie Cobbs 	{ 0 }
318901fadf7SArchie Cobbs };
319901fadf7SArchie Cobbs 
320901fadf7SArchie Cobbs /* Node type descriptor */
321901fadf7SArchie Cobbs static struct ng_type ng_l2tp_typestruct = {
322f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
323f8aae777SJulian Elischer 	.name =		NG_L2TP_NODE_TYPE,
324f8aae777SJulian Elischer 	.constructor =	ng_l2tp_constructor,
325f8aae777SJulian Elischer 	.rcvmsg =	ng_l2tp_rcvmsg,
326f8aae777SJulian Elischer 	.shutdown =	ng_l2tp_shutdown,
327f8aae777SJulian Elischer 	.newhook =	ng_l2tp_newhook,
328f8aae777SJulian Elischer 	.rcvdata =	ng_l2tp_rcvdata,
329f8aae777SJulian Elischer 	.disconnect =	ng_l2tp_disconnect,
330f8aae777SJulian Elischer 	.cmdlist =	ng_l2tp_cmdlist,
331901fadf7SArchie Cobbs };
332901fadf7SArchie Cobbs NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
333901fadf7SArchie Cobbs 
3340a76c63dSGleb Smirnoff /* Sequence number state locking & sanity checking */
335901fadf7SArchie Cobbs #ifdef INVARIANTS
3360a76c63dSGleb Smirnoff static void	ng_l2tp_seq_check(struct l2tp_seq *seq);
3370a76c63dSGleb Smirnoff #define SEQ_LOCK(seq)	do {					\
3380a76c63dSGleb Smirnoff 				mtx_lock(&(seq)->mtx);		\
3390a76c63dSGleb Smirnoff 				ng_l2tp_seq_check(seq);		\
3400a76c63dSGleb Smirnoff } while (0)
3410a76c63dSGleb Smirnoff #define	SEQ_UNLOCK(seq)	do {					\
3420a76c63dSGleb Smirnoff 				ng_l2tp_seq_check(seq);		\
3430a76c63dSGleb Smirnoff 				mtx_unlock(&(seq)->mtx);	\
3440a76c63dSGleb Smirnoff } while (0)
345901fadf7SArchie Cobbs #else
3460a76c63dSGleb Smirnoff #define SEQ_LOCK(seq)		mtx_lock(&(seq)->mtx)
3470a76c63dSGleb Smirnoff #define SEQ_UNLOCK(seq)		mtx_unlock(&(seq)->mtx)
348901fadf7SArchie Cobbs #endif
3490a76c63dSGleb Smirnoff #define	SEQ_LOCK_ASSERT(seq)	mtx_assert(&(seq)->mtx, MA_OWNED)
350901fadf7SArchie Cobbs 
351901fadf7SArchie Cobbs /* Whether to use m_copypacket() or m_dup() */
352901fadf7SArchie Cobbs #define L2TP_COPY_MBUF		m_copypacket
353901fadf7SArchie Cobbs 
354bf741e4dSAlexander Motin #define ERROUT(x)	do { error = (x); goto done; } while (0)
355bf741e4dSAlexander Motin 
356901fadf7SArchie Cobbs /************************************************************************
357901fadf7SArchie Cobbs 			NETGRAPH NODE STUFF
358901fadf7SArchie Cobbs ************************************************************************/
359901fadf7SArchie Cobbs 
360901fadf7SArchie Cobbs /*
361901fadf7SArchie Cobbs  * Node type constructor
362901fadf7SArchie Cobbs  */
363901fadf7SArchie Cobbs static int
364901fadf7SArchie Cobbs ng_l2tp_constructor(node_p node)
365901fadf7SArchie Cobbs {
366901fadf7SArchie Cobbs 	priv_p priv;
3674e759763SAlexander Motin 	int	i;
368901fadf7SArchie Cobbs 
369901fadf7SArchie Cobbs 	/* Allocate private structure */
370674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_L2TP, M_WAITOK | M_ZERO);
371901fadf7SArchie Cobbs 	NG_NODE_SET_PRIVATE(node, priv);
372901fadf7SArchie Cobbs 	priv->node = node;
373901fadf7SArchie Cobbs 
374901fadf7SArchie Cobbs 	/* Apply a semi-reasonable default configuration */
375901fadf7SArchie Cobbs 	priv->conf.peer_win = 1;
376901fadf7SArchie Cobbs 	priv->conf.rexmit_max = L2TP_MAX_REXMIT;
377901fadf7SArchie Cobbs 	priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
378901fadf7SArchie Cobbs 
379901fadf7SArchie Cobbs 	/* Initialize sequence number state */
380901fadf7SArchie Cobbs 	ng_l2tp_seq_init(priv);
381901fadf7SArchie Cobbs 
3824e759763SAlexander Motin 	for (i = 0; i < SESSHASHSIZE; i++)
3834e759763SAlexander Motin 	    LIST_INIT(&priv->sesshash[i]);
3844e759763SAlexander Motin 
385901fadf7SArchie Cobbs 	/* Done */
386901fadf7SArchie Cobbs 	return (0);
387901fadf7SArchie Cobbs }
388901fadf7SArchie Cobbs 
389901fadf7SArchie Cobbs /*
390901fadf7SArchie Cobbs  * Give our OK for a hook to be added.
391901fadf7SArchie Cobbs  */
392901fadf7SArchie Cobbs static int
393901fadf7SArchie Cobbs ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
394901fadf7SArchie Cobbs {
395901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
396901fadf7SArchie Cobbs 
397901fadf7SArchie Cobbs 	/* Check hook name */
398901fadf7SArchie Cobbs 	if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
399901fadf7SArchie Cobbs 		if (priv->ctrl != NULL)
400901fadf7SArchie Cobbs 			return (EISCONN);
401901fadf7SArchie Cobbs 		priv->ctrl = hook;
402bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
403901fadf7SArchie Cobbs 	} else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
404901fadf7SArchie Cobbs 		if (priv->lower != NULL)
405901fadf7SArchie Cobbs 			return (EISCONN);
406901fadf7SArchie Cobbs 		priv->lower = hook;
407bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
408901fadf7SArchie Cobbs 	} else {
409901fadf7SArchie Cobbs 		static const char hexdig[16] = "0123456789abcdef";
410901fadf7SArchie Cobbs 		u_int16_t session_id;
411901fadf7SArchie Cobbs 		hookpriv_p hpriv;
4124e759763SAlexander Motin 		uint16_t hash;
413901fadf7SArchie Cobbs 		const char *hex;
414901fadf7SArchie Cobbs 		int i;
415901fadf7SArchie Cobbs 		int j;
416901fadf7SArchie Cobbs 
417901fadf7SArchie Cobbs 		/* Parse hook name to get session ID */
418901fadf7SArchie Cobbs 		if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
419901fadf7SArchie Cobbs 		    sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
420901fadf7SArchie Cobbs 			return (EINVAL);
421901fadf7SArchie Cobbs 		hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
422901fadf7SArchie Cobbs 		for (session_id = i = 0; i < 4; i++) {
423901fadf7SArchie Cobbs 			for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
424901fadf7SArchie Cobbs 			if (j == 16)
425901fadf7SArchie Cobbs 				return (EINVAL);
426901fadf7SArchie Cobbs 			session_id = (session_id << 4) | j;
427901fadf7SArchie Cobbs 		}
428901fadf7SArchie Cobbs 		if (hex[i] != '\0')
429901fadf7SArchie Cobbs 			return (EINVAL);
430901fadf7SArchie Cobbs 
431901fadf7SArchie Cobbs 		/* Create hook private structure */
432e11e3f18SDag-Erling Smørgrav 		hpriv = malloc(sizeof(*hpriv),
433e11e3f18SDag-Erling Smørgrav 		    M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
434901fadf7SArchie Cobbs 		if (hpriv == NULL)
435901fadf7SArchie Cobbs 			return (ENOMEM);
436280d6bd7SAlexander Motin 		hpriv->conf.session_id = session_id;
437901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
438901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
4394e759763SAlexander Motin 		hpriv->hook = hook;
440901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, hpriv);
4414e759763SAlexander Motin 		hash = SESSHASH(hpriv->conf.session_id);
4424e759763SAlexander Motin 		LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
443901fadf7SArchie Cobbs 	}
444901fadf7SArchie Cobbs 
445901fadf7SArchie Cobbs 	/* Done */
446901fadf7SArchie Cobbs 	return (0);
447901fadf7SArchie Cobbs }
448901fadf7SArchie Cobbs 
449901fadf7SArchie Cobbs /*
450901fadf7SArchie Cobbs  * Receive a control message.
451901fadf7SArchie Cobbs  */
452901fadf7SArchie Cobbs static int
453901fadf7SArchie Cobbs ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
454901fadf7SArchie Cobbs {
455901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
456901fadf7SArchie Cobbs 	struct ng_mesg *resp = NULL;
457901fadf7SArchie Cobbs 	struct ng_mesg *msg;
458901fadf7SArchie Cobbs 	int error = 0;
459901fadf7SArchie Cobbs 
460901fadf7SArchie Cobbs 	NGI_GET_MSG(item, msg);
461901fadf7SArchie Cobbs 	switch (msg->header.typecookie) {
462901fadf7SArchie Cobbs 	case NGM_L2TP_COOKIE:
463901fadf7SArchie Cobbs 		switch (msg->header.cmd) {
464901fadf7SArchie Cobbs 		case NGM_L2TP_SET_CONFIG:
465901fadf7SArchie Cobbs 		    {
466901fadf7SArchie Cobbs 			struct ng_l2tp_config *const conf =
467901fadf7SArchie Cobbs 				(struct ng_l2tp_config *)msg->data;
468901fadf7SArchie Cobbs 
469901fadf7SArchie Cobbs 			/* Check for invalid or illegal config */
470901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
471901fadf7SArchie Cobbs 				error = EINVAL;
472901fadf7SArchie Cobbs 				break;
473901fadf7SArchie Cobbs 			}
474901fadf7SArchie Cobbs 			conf->enabled = !!conf->enabled;
475901fadf7SArchie Cobbs 			conf->match_id = !!conf->match_id;
476901fadf7SArchie Cobbs 			if (priv->conf.enabled
477901fadf7SArchie Cobbs 			    && ((priv->conf.tunnel_id != 0
478901fadf7SArchie Cobbs 			       && conf->tunnel_id != priv->conf.tunnel_id)
479901fadf7SArchie Cobbs 			      || ((priv->conf.peer_id != 0
480901fadf7SArchie Cobbs 			       && conf->peer_id != priv->conf.peer_id)))) {
481901fadf7SArchie Cobbs 				error = EBUSY;
482901fadf7SArchie Cobbs 				break;
483901fadf7SArchie Cobbs 			}
484901fadf7SArchie Cobbs 
485901fadf7SArchie Cobbs 			/* Save calling node as failure target */
486901fadf7SArchie Cobbs 			priv->ftarget = NGI_RETADDR(item);
487901fadf7SArchie Cobbs 
488901fadf7SArchie Cobbs 			/* Adjust sequence number state */
489901fadf7SArchie Cobbs 			if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
490901fadf7SArchie Cobbs 				break;
491901fadf7SArchie Cobbs 
492901fadf7SArchie Cobbs 			/* Update node's config */
493901fadf7SArchie Cobbs 			priv->conf = *conf;
494901fadf7SArchie Cobbs 			break;
495901fadf7SArchie Cobbs 		    }
496901fadf7SArchie Cobbs 		case NGM_L2TP_GET_CONFIG:
497901fadf7SArchie Cobbs 		    {
498901fadf7SArchie Cobbs 			struct ng_l2tp_config *conf;
499901fadf7SArchie Cobbs 
500901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
501901fadf7SArchie Cobbs 			if (resp == NULL) {
502901fadf7SArchie Cobbs 				error = ENOMEM;
503901fadf7SArchie Cobbs 				break;
504901fadf7SArchie Cobbs 			}
505901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_config *)resp->data;
506901fadf7SArchie Cobbs 			*conf = priv->conf;
507901fadf7SArchie Cobbs 			break;
508901fadf7SArchie Cobbs 		    }
509901fadf7SArchie Cobbs 		case NGM_L2TP_SET_SESS_CONFIG:
510901fadf7SArchie Cobbs 		    {
511901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *const conf =
512901fadf7SArchie Cobbs 			    (struct ng_l2tp_sess_config *)msg->data;
513901fadf7SArchie Cobbs 			hookpriv_p hpriv;
514901fadf7SArchie Cobbs 
5151e031324SBjoern A. Zeeb 			/* Check for invalid or illegal config. */
516901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
517901fadf7SArchie Cobbs 				error = EINVAL;
518901fadf7SArchie Cobbs 				break;
519901fadf7SArchie Cobbs 			}
520901fadf7SArchie Cobbs 
521901fadf7SArchie Cobbs 			/* Find matching hook */
5224e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, conf->session_id);
5234e759763SAlexander Motin 			if (hpriv == NULL) {
524901fadf7SArchie Cobbs 				error = ENOENT;
525901fadf7SArchie Cobbs 				break;
526901fadf7SArchie Cobbs 			}
527901fadf7SArchie Cobbs 
528901fadf7SArchie Cobbs 			/* Update hook's config */
529901fadf7SArchie Cobbs 			hpriv->conf = *conf;
530901fadf7SArchie Cobbs 			break;
531901fadf7SArchie Cobbs 		    }
532901fadf7SArchie Cobbs 		case NGM_L2TP_GET_SESS_CONFIG:
533901fadf7SArchie Cobbs 		    {
534901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *conf;
535901fadf7SArchie Cobbs 			u_int16_t session_id;
536901fadf7SArchie Cobbs 			hookpriv_p hpriv;
537901fadf7SArchie Cobbs 
538901fadf7SArchie Cobbs 			/* Get session ID */
539901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(session_id)) {
540901fadf7SArchie Cobbs 				error = EINVAL;
541901fadf7SArchie Cobbs 				break;
542901fadf7SArchie Cobbs 			}
543901fadf7SArchie Cobbs 			memcpy(&session_id, msg->data, 2);
544901fadf7SArchie Cobbs 
545901fadf7SArchie Cobbs 			/* Find matching hook */
5464e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, session_id);
5474e759763SAlexander Motin 			if (hpriv == NULL) {
548901fadf7SArchie Cobbs 				error = ENOENT;
549901fadf7SArchie Cobbs 				break;
550901fadf7SArchie Cobbs 			}
551901fadf7SArchie Cobbs 
552901fadf7SArchie Cobbs 			/* Send response */
553901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
554901fadf7SArchie Cobbs 			if (resp == NULL) {
555901fadf7SArchie Cobbs 				error = ENOMEM;
556901fadf7SArchie Cobbs 				break;
557901fadf7SArchie Cobbs 			}
558901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_sess_config *)resp->data;
559901fadf7SArchie Cobbs 			*conf = hpriv->conf;
560901fadf7SArchie Cobbs 			break;
561901fadf7SArchie Cobbs 		    }
562901fadf7SArchie Cobbs 		case NGM_L2TP_GET_STATS:
563901fadf7SArchie Cobbs 		case NGM_L2TP_CLR_STATS:
564901fadf7SArchie Cobbs 		case NGM_L2TP_GETCLR_STATS:
565901fadf7SArchie Cobbs 		    {
566901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
567901fadf7SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
568901fadf7SArchie Cobbs 				    sizeof(priv->stats), M_NOWAIT);
569901fadf7SArchie Cobbs 				if (resp == NULL) {
570901fadf7SArchie Cobbs 					error = ENOMEM;
571901fadf7SArchie Cobbs 					break;
572901fadf7SArchie Cobbs 				}
573901fadf7SArchie Cobbs 				memcpy(resp->data,
574901fadf7SArchie Cobbs 				    &priv->stats, sizeof(priv->stats));
575901fadf7SArchie Cobbs 			}
576901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_GET_STATS)
577901fadf7SArchie Cobbs 				memset(&priv->stats, 0, sizeof(priv->stats));
578901fadf7SArchie Cobbs 			break;
579901fadf7SArchie Cobbs 		    }
5804807330cSBjoern A. Zeeb 		case NGM_L2TP_GET_SESSION_STATS:
5814807330cSBjoern A. Zeeb 		case NGM_L2TP_CLR_SESSION_STATS:
5824807330cSBjoern A. Zeeb 		case NGM_L2TP_GETCLR_SESSION_STATS:
5834807330cSBjoern A. Zeeb 		    {
5844807330cSBjoern A. Zeeb 			uint16_t session_id;
5854807330cSBjoern A. Zeeb 			hookpriv_p hpriv;
5864807330cSBjoern A. Zeeb 
5874807330cSBjoern A. Zeeb 			/* Get session ID. */
5884807330cSBjoern A. Zeeb 			if (msg->header.arglen != sizeof(session_id)) {
5894807330cSBjoern A. Zeeb 				error = EINVAL;
5904807330cSBjoern A. Zeeb 				break;
5914807330cSBjoern A. Zeeb 			}
5924807330cSBjoern A. Zeeb 			bcopy(msg->data, &session_id, sizeof(uint16_t));
5934807330cSBjoern A. Zeeb 
5944807330cSBjoern A. Zeeb 			/* Find matching hook. */
5954e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, session_id);
5964e759763SAlexander Motin 			if (hpriv == NULL) {
5974807330cSBjoern A. Zeeb 				error = ENOENT;
5984807330cSBjoern A. Zeeb 				break;
5994807330cSBjoern A. Zeeb 			}
6004807330cSBjoern A. Zeeb 
6014807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
6024807330cSBjoern A. Zeeb 				NG_MKRESPONSE(resp, msg,
6034807330cSBjoern A. Zeeb 				    sizeof(hpriv->stats), M_NOWAIT);
6044807330cSBjoern A. Zeeb 				if (resp == NULL) {
6054807330cSBjoern A. Zeeb 					error = ENOMEM;
6064807330cSBjoern A. Zeeb 					break;
6074807330cSBjoern A. Zeeb 				}
6084807330cSBjoern A. Zeeb 				bcopy(&hpriv->stats, resp->data,
6094807330cSBjoern A. Zeeb 					sizeof(hpriv->stats));
6104807330cSBjoern A. Zeeb 			}
6114807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
6124807330cSBjoern A. Zeeb 				bzero(&hpriv->stats, sizeof(hpriv->stats));
6134807330cSBjoern A. Zeeb 			break;
6144807330cSBjoern A. Zeeb 		    }
6151e031324SBjoern A. Zeeb 		case NGM_L2TP_SET_SEQ:
6161e031324SBjoern A. Zeeb 		    {
6171e031324SBjoern A. Zeeb 			struct ng_l2tp_seq_config *const conf =
6181e031324SBjoern A. Zeeb 				(struct ng_l2tp_seq_config *)msg->data;
6191e031324SBjoern A. Zeeb 
6201e031324SBjoern A. Zeeb 			/* Check for invalid or illegal seq config. */
6211e031324SBjoern A. Zeeb 			if (msg->header.arglen != sizeof(*conf)) {
6221e031324SBjoern A. Zeeb 				error = EINVAL;
6231e031324SBjoern A. Zeeb 				break;
6241e031324SBjoern A. Zeeb 			}
6251e031324SBjoern A. Zeeb 			conf->ns = htons(conf->ns);
6261e031324SBjoern A. Zeeb 			conf->nr = htons(conf->nr);
6271e031324SBjoern A. Zeeb 			conf->rack = htons(conf->rack);
6281e031324SBjoern A. Zeeb 			conf->xack = htons(conf->xack);
6291e031324SBjoern A. Zeeb 
6301e031324SBjoern A. Zeeb 			/* Set sequence numbers. */
6311e031324SBjoern A. Zeeb 			error = ng_l2tp_seq_set(priv, conf);
6321e031324SBjoern A. Zeeb 			break;
6331e031324SBjoern A. Zeeb 		    }
634901fadf7SArchie Cobbs 		default:
635901fadf7SArchie Cobbs 			error = EINVAL;
636901fadf7SArchie Cobbs 			break;
637901fadf7SArchie Cobbs 		}
638901fadf7SArchie Cobbs 		break;
639901fadf7SArchie Cobbs 	default:
640901fadf7SArchie Cobbs 		error = EINVAL;
641901fadf7SArchie Cobbs 		break;
642901fadf7SArchie Cobbs 	}
643901fadf7SArchie Cobbs 
644901fadf7SArchie Cobbs 	/* Done */
645901fadf7SArchie Cobbs 	NG_RESPOND_MSG(error, node, item, resp);
646901fadf7SArchie Cobbs 	NG_FREE_MSG(msg);
647901fadf7SArchie Cobbs 	return (error);
648901fadf7SArchie Cobbs }
649901fadf7SArchie Cobbs 
650901fadf7SArchie Cobbs /*
651901fadf7SArchie Cobbs  * Destroy node
652901fadf7SArchie Cobbs  */
653901fadf7SArchie Cobbs static int
654901fadf7SArchie Cobbs ng_l2tp_shutdown(node_p node)
655901fadf7SArchie Cobbs {
656901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
657901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
658901fadf7SArchie Cobbs 
659901fadf7SArchie Cobbs 	/* Reset sequence number state */
6600a76c63dSGleb Smirnoff 	SEQ_LOCK(seq);
661901fadf7SArchie Cobbs 	ng_l2tp_seq_reset(priv);
6620a76c63dSGleb Smirnoff 	SEQ_UNLOCK(seq);
663901fadf7SArchie Cobbs 
664901fadf7SArchie Cobbs 	/* Free private data if neither timer is running */
665*89042ff7SGleb Smirnoff 	ng_uncallout_drain(&seq->rack_timer, node);
666*89042ff7SGleb Smirnoff 	ng_uncallout_drain(&seq->xack_timer, node);
6670ef8db8fSGleb Smirnoff 
668702f9895SAlexander Motin 	mtx_destroy(&seq->mtx);
669702f9895SAlexander Motin 
6701ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_L2TP);
671901fadf7SArchie Cobbs 
672901fadf7SArchie Cobbs 	/* Unref node */
673901fadf7SArchie Cobbs 	NG_NODE_UNREF(node);
674901fadf7SArchie Cobbs 	return (0);
675901fadf7SArchie Cobbs }
676901fadf7SArchie Cobbs 
677901fadf7SArchie Cobbs /*
678901fadf7SArchie Cobbs  * Hook disconnection
679901fadf7SArchie Cobbs  */
680901fadf7SArchie Cobbs static int
681901fadf7SArchie Cobbs ng_l2tp_disconnect(hook_p hook)
682901fadf7SArchie Cobbs {
683901fadf7SArchie Cobbs 	const node_p node = NG_HOOK_NODE(hook);
684901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
685901fadf7SArchie Cobbs 
686901fadf7SArchie Cobbs 	/* Zero out hook pointer */
687901fadf7SArchie Cobbs 	if (hook == priv->ctrl)
688901fadf7SArchie Cobbs 		priv->ctrl = NULL;
689901fadf7SArchie Cobbs 	else if (hook == priv->lower)
690901fadf7SArchie Cobbs 		priv->lower = NULL;
691901fadf7SArchie Cobbs 	else {
6924e759763SAlexander Motin 		const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
6934e759763SAlexander Motin 		LIST_REMOVE(hpriv, sessions);
6941ede983cSDag-Erling Smørgrav 		free(hpriv, M_NETGRAPH_L2TP);
695901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, NULL);
696901fadf7SArchie Cobbs 	}
697901fadf7SArchie Cobbs 
698901fadf7SArchie Cobbs 	/* Go away if no longer connected to anything */
699901fadf7SArchie Cobbs 	if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
700901fadf7SArchie Cobbs 		ng_rmnode_self(node);
701901fadf7SArchie Cobbs 	return (0);
702901fadf7SArchie Cobbs }
703901fadf7SArchie Cobbs 
704901fadf7SArchie Cobbs /*************************************************************************
705901fadf7SArchie Cobbs 			INTERNAL FUNCTIONS
706901fadf7SArchie Cobbs *************************************************************************/
707901fadf7SArchie Cobbs 
708901fadf7SArchie Cobbs /*
709280d6bd7SAlexander Motin  * Find the hook with a given session ID.
710901fadf7SArchie Cobbs  */
7114e759763SAlexander Motin static hookpriv_p
7124e759763SAlexander Motin ng_l2tp_find_session(priv_p privp, u_int16_t sid)
713901fadf7SArchie Cobbs {
7144e759763SAlexander Motin 	uint16_t	hash = SESSHASH(sid);
7154e759763SAlexander Motin 	hookpriv_p	hpriv = NULL;
716901fadf7SArchie Cobbs 
7174e759763SAlexander Motin 	LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
7184e759763SAlexander Motin 		if (hpriv->conf.session_id == sid)
7194e759763SAlexander Motin 			break;
7204e759763SAlexander Motin 	}
7214e759763SAlexander Motin 
7224e759763SAlexander Motin 	return (hpriv);
723901fadf7SArchie Cobbs }
724901fadf7SArchie Cobbs 
725901fadf7SArchie Cobbs /*
726901fadf7SArchie Cobbs  * Reset a hook's session state.
727901fadf7SArchie Cobbs  */
728901fadf7SArchie Cobbs static int
729901fadf7SArchie Cobbs ng_l2tp_reset_session(hook_p hook, void *arg)
730901fadf7SArchie Cobbs {
731901fadf7SArchie Cobbs 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
732901fadf7SArchie Cobbs 
733901fadf7SArchie Cobbs 	if (hpriv != NULL) {
734901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = 0;
735901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = 0;
736b0239937SAlexander Motin 		bzero(&hpriv->stats, sizeof(struct ng_l2tp_session_stats));
737901fadf7SArchie Cobbs 		hpriv->nr = 0;
738901fadf7SArchie Cobbs 		hpriv->ns = 0;
739901fadf7SArchie Cobbs 	}
740901fadf7SArchie Cobbs 	return (-1);
741901fadf7SArchie Cobbs }
742901fadf7SArchie Cobbs 
743901fadf7SArchie Cobbs /*
744901fadf7SArchie Cobbs  * Handle an incoming frame from below.
745901fadf7SArchie Cobbs  */
746901fadf7SArchie Cobbs static int
747bf741e4dSAlexander Motin ng_l2tp_rcvdata_lower(hook_p h, item_p item)
748901fadf7SArchie Cobbs {
749901fadf7SArchie Cobbs 	static const u_int16_t req_bits[2][2] = {
750901fadf7SArchie Cobbs 		{ L2TP_DATA_0BITS, L2TP_DATA_1BITS },
751901fadf7SArchie Cobbs 		{ L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
752901fadf7SArchie Cobbs 	};
753bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(h);
754901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
755901fadf7SArchie Cobbs 	hookpriv_p hpriv = NULL;
756901fadf7SArchie Cobbs 	hook_p hook = NULL;
757901fadf7SArchie Cobbs 	struct mbuf *m;
758280d6bd7SAlexander Motin 	u_int16_t tid, sid;
759901fadf7SArchie Cobbs 	u_int16_t hdr;
760280d6bd7SAlexander Motin 	u_int16_t ns, nr;
761901fadf7SArchie Cobbs 	int is_ctrl;
762901fadf7SArchie Cobbs 	int error;
7634807330cSBjoern A. Zeeb 	int len, plen;
764901fadf7SArchie Cobbs 
765bf741e4dSAlexander Motin 	/* If not configured, reject */
766bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
767bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
768bf741e4dSAlexander Motin 		ERROUT(ENXIO);
769bf741e4dSAlexander Motin 	}
770bf741e4dSAlexander Motin 
771901fadf7SArchie Cobbs 	/* Grab mbuf */
772901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
773901fadf7SArchie Cobbs 
7744807330cSBjoern A. Zeeb 	/* Remember full packet length; needed for per session accounting. */
7754807330cSBjoern A. Zeeb 	plen = m->m_pkthdr.len;
7764807330cSBjoern A. Zeeb 
777901fadf7SArchie Cobbs 	/* Update stats */
778901fadf7SArchie Cobbs 	priv->stats.recvPackets++;
7794807330cSBjoern A. Zeeb 	priv->stats.recvOctets += plen;
780901fadf7SArchie Cobbs 
781901fadf7SArchie Cobbs 	/* Get initial header */
782901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 6) {
783901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
784901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
785901fadf7SArchie Cobbs 		NG_FREE_M(m);
786bf741e4dSAlexander Motin 		ERROUT(EINVAL);
787901fadf7SArchie Cobbs 	}
788901fadf7SArchie Cobbs 	if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
789901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
790901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
791bf741e4dSAlexander Motin 		ERROUT(EINVAL);
792901fadf7SArchie Cobbs 	}
793148ac1daSAlexander Motin 	hdr = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1];
794901fadf7SArchie Cobbs 	m_adj(m, 2);
795901fadf7SArchie Cobbs 
796901fadf7SArchie Cobbs 	/* Check required header bits and minimum length */
797901fadf7SArchie Cobbs 	is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
798901fadf7SArchie Cobbs 	if ((hdr & req_bits[is_ctrl][0]) != 0
799901fadf7SArchie Cobbs 	    || (~hdr & req_bits[is_ctrl][1]) != 0) {
800901fadf7SArchie Cobbs 		priv->stats.recvInvalid++;
801901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
802901fadf7SArchie Cobbs 		NG_FREE_M(m);
803bf741e4dSAlexander Motin 		ERROUT(EINVAL);
804901fadf7SArchie Cobbs 	}
805901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 4				/* tunnel, session id */
806901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_LEN) != 0))		/* length field */
807901fadf7SArchie Cobbs 	    + (4 * ((hdr & L2TP_HDR_SEQ) != 0))		/* seq # fields */
808901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_OFF) != 0))) {	/* offset field */
809901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
810901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
811901fadf7SArchie Cobbs 		NG_FREE_M(m);
812bf741e4dSAlexander Motin 		ERROUT(EINVAL);
813901fadf7SArchie Cobbs 	}
814901fadf7SArchie Cobbs 
815901fadf7SArchie Cobbs 	/* Get and validate length field if present */
816901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_LEN) != 0) {
817901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
818901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
819901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
820bf741e4dSAlexander Motin 			ERROUT(EINVAL);
821901fadf7SArchie Cobbs 		}
822148ac1daSAlexander Motin 		len = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1] - 4;
823901fadf7SArchie Cobbs 		m_adj(m, 2);
824901fadf7SArchie Cobbs 		if (len < 0 || len > m->m_pkthdr.len) {
825901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
826901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
827901fadf7SArchie Cobbs 			NG_FREE_M(m);
828bf741e4dSAlexander Motin 			ERROUT(EINVAL);
829901fadf7SArchie Cobbs 		}
830901fadf7SArchie Cobbs 		if (len < m->m_pkthdr.len)		/* trim extra bytes */
831901fadf7SArchie Cobbs 			m_adj(m, -(m->m_pkthdr.len - len));
832901fadf7SArchie Cobbs 	}
833901fadf7SArchie Cobbs 
834901fadf7SArchie Cobbs 	/* Get tunnel ID and session ID */
835901fadf7SArchie Cobbs 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
836901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
837901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
838bf741e4dSAlexander Motin 		ERROUT(EINVAL);
839901fadf7SArchie Cobbs 	}
840280d6bd7SAlexander Motin 	tid = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
841280d6bd7SAlexander Motin 	sid = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
842901fadf7SArchie Cobbs 	m_adj(m, 4);
843901fadf7SArchie Cobbs 
844901fadf7SArchie Cobbs 	/* Check tunnel ID */
845280d6bd7SAlexander Motin 	if (tid != priv->conf.tunnel_id &&
846280d6bd7SAlexander Motin 	    (priv->conf.match_id || tid != 0)) {
847901fadf7SArchie Cobbs 		priv->stats.recvWrongTunnel++;
848901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
849901fadf7SArchie Cobbs 		NG_FREE_M(m);
850bf741e4dSAlexander Motin 		ERROUT(EADDRNOTAVAIL);
851901fadf7SArchie Cobbs 	}
852901fadf7SArchie Cobbs 
853901fadf7SArchie Cobbs 	/* Check session ID (for data packets only) */
854901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) == 0) {
855280d6bd7SAlexander Motin 		hpriv = ng_l2tp_find_session(priv, sid);
8564e759763SAlexander Motin 		if (hpriv == NULL) {
857901fadf7SArchie Cobbs 			priv->stats.recvUnknownSID++;
858901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
859901fadf7SArchie Cobbs 			NG_FREE_M(m);
860bf741e4dSAlexander Motin 			ERROUT(ENOTCONN);
861901fadf7SArchie Cobbs 		}
8624e759763SAlexander Motin 		hook = hpriv->hook;
863901fadf7SArchie Cobbs 	}
864901fadf7SArchie Cobbs 
865901fadf7SArchie Cobbs 	/* Get Ns, Nr fields if present */
866901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
867901fadf7SArchie Cobbs 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
868901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
869901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
870bf741e4dSAlexander Motin 			ERROUT(EINVAL);
871901fadf7SArchie Cobbs 		}
872280d6bd7SAlexander Motin 		ns = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
873280d6bd7SAlexander Motin 		nr = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
874901fadf7SArchie Cobbs 		m_adj(m, 4);
875280d6bd7SAlexander Motin 	} else
876280d6bd7SAlexander Motin 		ns = nr = 0;
877901fadf7SArchie Cobbs 
878901fadf7SArchie Cobbs 	/* Strip offset padding if present */
879901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_OFF) != 0) {
880901fadf7SArchie Cobbs 		u_int16_t offset;
881901fadf7SArchie Cobbs 
882901fadf7SArchie Cobbs 		/* Get length of offset padding */
883901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
884901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
885901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
886bf741e4dSAlexander Motin 			ERROUT(EINVAL);
887901fadf7SArchie Cobbs 		}
888280d6bd7SAlexander Motin 		offset = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
889901fadf7SArchie Cobbs 
890901fadf7SArchie Cobbs 		/* Trim offset padding */
891ddb72294SBjoern A. Zeeb 		if ((2+offset) > m->m_pkthdr.len) {
892901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
893901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
894901fadf7SArchie Cobbs 			NG_FREE_M(m);
895bf741e4dSAlexander Motin 			ERROUT(EINVAL);
896901fadf7SArchie Cobbs 		}
897ddb72294SBjoern A. Zeeb 		m_adj(m, 2+offset);
898901fadf7SArchie Cobbs 	}
899901fadf7SArchie Cobbs 
900901fadf7SArchie Cobbs 	/* Handle control packets */
901901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) != 0) {
902af63939cSAlexander Motin 		struct l2tp_seq *const seq = &priv->seq;
903901fadf7SArchie Cobbs 
9040a76c63dSGleb Smirnoff 		SEQ_LOCK(seq);
9050a76c63dSGleb Smirnoff 
906901fadf7SArchie Cobbs 		/* Handle receive ack sequence number Nr */
907901fadf7SArchie Cobbs 		ng_l2tp_seq_recv_nr(priv, nr);
908901fadf7SArchie Cobbs 
909901fadf7SArchie Cobbs 		/* Discard ZLB packets */
910901fadf7SArchie Cobbs 		if (m->m_pkthdr.len == 0) {
9110a76c63dSGleb Smirnoff 			SEQ_UNLOCK(seq);
912901fadf7SArchie Cobbs 			priv->stats.recvZLBs++;
913901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
914901fadf7SArchie Cobbs 			NG_FREE_M(m);
915bf741e4dSAlexander Motin 			ERROUT(0);
916901fadf7SArchie Cobbs 		}
917901fadf7SArchie Cobbs 
918901fadf7SArchie Cobbs 		/*
919af63939cSAlexander Motin 		 * If not what we expect or we are busy, drop packet and
920af63939cSAlexander Motin 		 * send an immediate ZLB ack.
921901fadf7SArchie Cobbs 		 */
922af63939cSAlexander Motin 		if (ns != seq->nr || seq->inproc) {
923af63939cSAlexander Motin 			if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
924af63939cSAlexander Motin 				priv->stats.recvDuplicates++;
925af63939cSAlexander Motin 			else
926af63939cSAlexander Motin 				priv->stats.recvOutOfOrder++;
927af63939cSAlexander Motin 			ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
928af63939cSAlexander Motin 			NG_FREE_ITEM(item);
929af63939cSAlexander Motin 			NG_FREE_M(m);
930af63939cSAlexander Motin 			ERROUT(0);
931af63939cSAlexander Motin 		}
932af63939cSAlexander Motin 
933af63939cSAlexander Motin 		/* Prepend session ID to packet. */
934eb1b1807SGleb Smirnoff 		M_PREPEND(m, 2, M_NOWAIT);
935901fadf7SArchie Cobbs 		if (m == NULL) {
9360a76c63dSGleb Smirnoff 			SEQ_UNLOCK(seq);
937901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
938901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
939bf741e4dSAlexander Motin 			ERROUT(ENOBUFS);
940901fadf7SArchie Cobbs 		}
941280d6bd7SAlexander Motin 		mtod(m, u_int8_t *)[0] = sid >> 8;
942280d6bd7SAlexander Motin 		mtod(m, u_int8_t *)[1] = sid & 0xff;
943901fadf7SArchie Cobbs 
9440a76c63dSGleb Smirnoff 		/*
9450a76c63dSGleb Smirnoff 		 * Until we deliver this packet we can't receive next one as
9460a76c63dSGleb Smirnoff 		 * we have no information for sending ack.
9470a76c63dSGleb Smirnoff 		 */
9480a76c63dSGleb Smirnoff 		seq->inproc = 1;
9490a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
9500a76c63dSGleb Smirnoff 
951901fadf7SArchie Cobbs 		/* Deliver packet to upper layers */
952901fadf7SArchie Cobbs 		NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
953af63939cSAlexander Motin 
9540a76c63dSGleb Smirnoff 		SEQ_LOCK(seq);
955af63939cSAlexander Motin 		/* Ready to process next packet. */
956af63939cSAlexander Motin 		seq->inproc = 0;
957af63939cSAlexander Motin 
958af63939cSAlexander Motin 		/* If packet was successfully delivered send ack. */
959af63939cSAlexander Motin 		if (error == 0) {
960af63939cSAlexander Motin 			/* Update recv sequence number */
961af63939cSAlexander Motin 			seq->nr++;
962af63939cSAlexander Motin 			/* Start receive ack timer, if not already running */
963af63939cSAlexander Motin 			if (!callout_active(&seq->xack_timer)) {
964af63939cSAlexander Motin 				ng_callout(&seq->xack_timer, priv->node, NULL,
965af63939cSAlexander Motin 				    L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
966af63939cSAlexander Motin 				    NULL, 0);
967af63939cSAlexander Motin 			}
968af63939cSAlexander Motin 		}
9690a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
970af63939cSAlexander Motin 
971bf741e4dSAlexander Motin 		ERROUT(error);
972901fadf7SArchie Cobbs 	}
973901fadf7SArchie Cobbs 
9744807330cSBjoern A. Zeeb 	/* Per session packet, account it. */
9754807330cSBjoern A. Zeeb 	hpriv->stats.recvPackets++;
9764807330cSBjoern A. Zeeb 	hpriv->stats.recvOctets += plen;
9774807330cSBjoern A. Zeeb 
978901fadf7SArchie Cobbs 	/* Follow peer's lead in data sequencing, if configured to do so */
979901fadf7SArchie Cobbs 	if (!hpriv->conf.control_dseq)
980901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
981901fadf7SArchie Cobbs 
982901fadf7SArchie Cobbs 	/* Handle data sequence numbers if present and enabled */
983901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
984901fadf7SArchie Cobbs 		if (hpriv->conf.enable_dseq
985901fadf7SArchie Cobbs 		    && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
986901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);	/* duplicate or out of order */
987901fadf7SArchie Cobbs 			NG_FREE_M(m);
988901fadf7SArchie Cobbs 			priv->stats.recvDataDrops++;
989bf741e4dSAlexander Motin 			ERROUT(0);
990901fadf7SArchie Cobbs 		}
991901fadf7SArchie Cobbs 		hpriv->nr = ns + 1;
992901fadf7SArchie Cobbs 	}
993901fadf7SArchie Cobbs 
994901fadf7SArchie Cobbs 	/* Drop empty data packets */
995901fadf7SArchie Cobbs 	if (m->m_pkthdr.len == 0) {
996901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
997901fadf7SArchie Cobbs 		NG_FREE_M(m);
998bf741e4dSAlexander Motin 		ERROUT(0);
999901fadf7SArchie Cobbs 	}
1000901fadf7SArchie Cobbs 
1001901fadf7SArchie Cobbs 	/* Deliver data */
1002901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, hook, m);
1003bf741e4dSAlexander Motin done:
1004901fadf7SArchie Cobbs 	return (error);
1005901fadf7SArchie Cobbs }
1006901fadf7SArchie Cobbs 
1007901fadf7SArchie Cobbs /*
1008901fadf7SArchie Cobbs  * Handle an outgoing control frame.
1009901fadf7SArchie Cobbs  */
1010901fadf7SArchie Cobbs static int
1011bf741e4dSAlexander Motin ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1012901fadf7SArchie Cobbs {
1013bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(hook);
1014901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1015901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1016901fadf7SArchie Cobbs 	struct mbuf *m;
1017bf741e4dSAlexander Motin 	int error;
1018901fadf7SArchie Cobbs 	int i;
1019702f9895SAlexander Motin 	u_int16_t	ns;
1020901fadf7SArchie Cobbs 
1021bf741e4dSAlexander Motin 	/* If not configured, reject */
1022bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1023bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1024bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1025bf741e4dSAlexander Motin 	}
1026bf741e4dSAlexander Motin 
1027901fadf7SArchie Cobbs 	/* Grab mbuf and discard other stuff XXX */
1028901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1029901fadf7SArchie Cobbs 	NG_FREE_ITEM(item);
1030901fadf7SArchie Cobbs 
1031901fadf7SArchie Cobbs 	/* Packet should have session ID prepended */
1032901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 2) {
1033901fadf7SArchie Cobbs 		priv->stats.xmitInvalid++;
1034901fadf7SArchie Cobbs 		m_freem(m);
1035bf741e4dSAlexander Motin 		ERROUT(EINVAL);
1036901fadf7SArchie Cobbs 	}
1037901fadf7SArchie Cobbs 
1038901fadf7SArchie Cobbs 	/* Check max length */
1039901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 14) {
1040901fadf7SArchie Cobbs 		priv->stats.xmitTooBig++;
1041901fadf7SArchie Cobbs 		m_freem(m);
1042bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1043901fadf7SArchie Cobbs 	}
1044901fadf7SArchie Cobbs 
10450a76c63dSGleb Smirnoff 	SEQ_LOCK(seq);
1046702f9895SAlexander Motin 
1047901fadf7SArchie Cobbs 	/* Find next empty slot in transmit queue */
1048901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1049901fadf7SArchie Cobbs 	if (i == L2TP_MAX_XWIN) {
10500a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
1051901fadf7SArchie Cobbs 		priv->stats.xmitDrops++;
1052901fadf7SArchie Cobbs 		m_freem(m);
1053bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1054901fadf7SArchie Cobbs 	}
1055901fadf7SArchie Cobbs 	seq->xwin[i] = m;
1056901fadf7SArchie Cobbs 
1057901fadf7SArchie Cobbs 	/* If peer's receive window is already full, nothing else to do */
1058702f9895SAlexander Motin 	if (i >= seq->cwnd) {
10590a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
1060bf741e4dSAlexander Motin 		ERROUT(0);
1061702f9895SAlexander Motin 	}
1062901fadf7SArchie Cobbs 
1063901fadf7SArchie Cobbs 	/* Start retransmit timer if not already running */
1064206fa244SAlexander Motin 	if (!callout_active(&seq->rack_timer))
10650ef8db8fSGleb Smirnoff 		ng_callout(&seq->rack_timer, node, NULL,
10660ef8db8fSGleb Smirnoff 		    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1067901fadf7SArchie Cobbs 
1068702f9895SAlexander Motin 	ns = seq->ns++;
1069702f9895SAlexander Motin 
1070901fadf7SArchie Cobbs 	/* Copy packet */
1071eb1b1807SGleb Smirnoff 	if ((m = L2TP_COPY_MBUF(m, M_NOWAIT)) == NULL) {
10720a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
1073901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1074bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1075901fadf7SArchie Cobbs 	}
1076901fadf7SArchie Cobbs 
1077901fadf7SArchie Cobbs 	/* Send packet and increment xmit sequence number */
1078702f9895SAlexander Motin 	error = ng_l2tp_xmit_ctrl(priv, m, ns);
1079bf741e4dSAlexander Motin done:
1080bf741e4dSAlexander Motin 	return (error);
1081901fadf7SArchie Cobbs }
1082901fadf7SArchie Cobbs 
1083901fadf7SArchie Cobbs /*
1084901fadf7SArchie Cobbs  * Handle an outgoing data frame.
1085901fadf7SArchie Cobbs  */
1086901fadf7SArchie Cobbs static int
1087bf741e4dSAlexander Motin ng_l2tp_rcvdata(hook_p hook, item_p item)
1088901fadf7SArchie Cobbs {
1089bf741e4dSAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1090bf741e4dSAlexander Motin 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1091901fadf7SArchie Cobbs 	struct mbuf *m;
1092148ac1daSAlexander Motin 	uint8_t *p;
1093901fadf7SArchie Cobbs 	u_int16_t hdr;
1094901fadf7SArchie Cobbs 	int error;
1095148ac1daSAlexander Motin 	int i = 2;
1096901fadf7SArchie Cobbs 
1097bf741e4dSAlexander Motin 	/* If not configured, reject */
1098bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1099bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1100bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1101bf741e4dSAlexander Motin 	}
1102bf741e4dSAlexander Motin 
1103901fadf7SArchie Cobbs 	/* Get mbuf */
1104901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1105901fadf7SArchie Cobbs 
1106901fadf7SArchie Cobbs 	/* Check max length */
1107901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 12) {
1108901fadf7SArchie Cobbs 		priv->stats.xmitDataTooBig++;
1109901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1110901fadf7SArchie Cobbs 		NG_FREE_M(m);
1111bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1112901fadf7SArchie Cobbs 	}
1113901fadf7SArchie Cobbs 
1114901fadf7SArchie Cobbs 	/* Prepend L2TP header */
1115901fadf7SArchie Cobbs 	M_PREPEND(m, 6
1116901fadf7SArchie Cobbs 	    + (2 * (hpriv->conf.include_length != 0))
1117901fadf7SArchie Cobbs 	    + (4 * (hpriv->conf.enable_dseq != 0)),
1118eb1b1807SGleb Smirnoff 	    M_NOWAIT);
1119901fadf7SArchie Cobbs 	if (m == NULL) {
1120901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1121901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1122bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1123901fadf7SArchie Cobbs 	}
1124148ac1daSAlexander Motin 	p = mtod(m, uint8_t *);
1125901fadf7SArchie Cobbs 	hdr = L2TP_DATA_HDR;
1126901fadf7SArchie Cobbs 	if (hpriv->conf.include_length) {
1127901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_LEN;
1128148ac1daSAlexander Motin 		p[i++] = m->m_pkthdr.len >> 8;
1129148ac1daSAlexander Motin 		p[i++] = m->m_pkthdr.len & 0xff;
1130901fadf7SArchie Cobbs 	}
1131148ac1daSAlexander Motin 	p[i++] = priv->conf.peer_id >> 8;
1132148ac1daSAlexander Motin 	p[i++] = priv->conf.peer_id & 0xff;
1133148ac1daSAlexander Motin 	p[i++] = hpriv->conf.peer_id >> 8;
1134148ac1daSAlexander Motin 	p[i++] = hpriv->conf.peer_id & 0xff;
1135901fadf7SArchie Cobbs 	if (hpriv->conf.enable_dseq) {
1136901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_SEQ;
1137148ac1daSAlexander Motin 		p[i++] = hpriv->ns >> 8;
1138148ac1daSAlexander Motin 		p[i++] = hpriv->ns & 0xff;
1139148ac1daSAlexander Motin 		p[i++] = hpriv->nr >> 8;
1140148ac1daSAlexander Motin 		p[i++] = hpriv->nr & 0xff;
1141901fadf7SArchie Cobbs 		hpriv->ns++;
1142901fadf7SArchie Cobbs 	}
1143148ac1daSAlexander Motin 	p[0] = hdr >> 8;
1144148ac1daSAlexander Motin 	p[1] = hdr & 0xff;
1145901fadf7SArchie Cobbs 
11464807330cSBjoern A. Zeeb 	/* Update per session stats. */
11474807330cSBjoern A. Zeeb 	hpriv->stats.xmitPackets++;
11484807330cSBjoern A. Zeeb 	hpriv->stats.xmitOctets += m->m_pkthdr.len;
11494807330cSBjoern A. Zeeb 
115034d16c64SAlexander Motin 	/* And the global one. */
115134d16c64SAlexander Motin 	priv->stats.xmitPackets++;
115234d16c64SAlexander Motin 	priv->stats.xmitOctets += m->m_pkthdr.len;
115334d16c64SAlexander Motin 
1154901fadf7SArchie Cobbs 	/* Send packet */
1155901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, priv->lower, m);
1156bf741e4dSAlexander Motin done:
1157901fadf7SArchie Cobbs 	return (error);
1158901fadf7SArchie Cobbs }
1159901fadf7SArchie Cobbs 
1160901fadf7SArchie Cobbs /*
1161901fadf7SArchie Cobbs  * Send a message to our controlling node that we've failed.
1162901fadf7SArchie Cobbs  */
1163901fadf7SArchie Cobbs static void
1164901fadf7SArchie Cobbs ng_l2tp_seq_failure(priv_p priv)
1165901fadf7SArchie Cobbs {
1166901fadf7SArchie Cobbs 	struct ng_mesg *msg;
1167901fadf7SArchie Cobbs 	int error;
1168901fadf7SArchie Cobbs 
1169901fadf7SArchie Cobbs 	NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1170901fadf7SArchie Cobbs 	if (msg == NULL)
1171901fadf7SArchie Cobbs 		return;
1172facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1173901fadf7SArchie Cobbs }
1174901fadf7SArchie Cobbs 
1175901fadf7SArchie Cobbs /************************************************************************
1176901fadf7SArchie Cobbs 			SEQUENCE NUMBER HANDLING
1177901fadf7SArchie Cobbs ************************************************************************/
1178901fadf7SArchie Cobbs 
1179901fadf7SArchie Cobbs /*
1180901fadf7SArchie Cobbs  * Initialize sequence number state.
1181901fadf7SArchie Cobbs  */
1182901fadf7SArchie Cobbs static void
1183901fadf7SArchie Cobbs ng_l2tp_seq_init(priv_p priv)
1184901fadf7SArchie Cobbs {
1185901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1186901fadf7SArchie Cobbs 
1187901fadf7SArchie Cobbs 	KASSERT(priv->conf.peer_win >= 1,
11887e2b43eeSDavid E. O'Brien 	    ("%s: peer_win is zero", __func__));
1189901fadf7SArchie Cobbs 	memset(seq, 0, sizeof(*seq));
1190901fadf7SArchie Cobbs 	seq->cwnd = 1;
1191901fadf7SArchie Cobbs 	seq->wmax = priv->conf.peer_win;
1192901fadf7SArchie Cobbs 	if (seq->wmax > L2TP_MAX_XWIN)
1193901fadf7SArchie Cobbs 		seq->wmax = L2TP_MAX_XWIN;
1194901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
1195702f9895SAlexander Motin 	mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1196*89042ff7SGleb Smirnoff 	callout_init_mtx(&seq->rack_timer, &seq->mtx, CALLOUT_RETURNUNLOCKED);
1197*89042ff7SGleb Smirnoff 	callout_init_mtx(&seq->xack_timer, &seq->mtx, CALLOUT_RETURNUNLOCKED);
1198901fadf7SArchie Cobbs }
1199901fadf7SArchie Cobbs 
1200901fadf7SArchie Cobbs /*
12011e031324SBjoern A. Zeeb  * Set sequence number state as given from user.
12021e031324SBjoern A. Zeeb  */
12031e031324SBjoern A. Zeeb static int
12041e031324SBjoern A. Zeeb ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
12051e031324SBjoern A. Zeeb {
12061e031324SBjoern A. Zeeb 	struct l2tp_seq *const seq = &priv->seq;
12071e031324SBjoern A. Zeeb 
12081e031324SBjoern A. Zeeb 	/* If node is enabled, deny update to sequence numbers. */
12091e031324SBjoern A. Zeeb 	if (priv->conf.enabled)
12101e031324SBjoern A. Zeeb 		return (EBUSY);
12111e031324SBjoern A. Zeeb 
12121e031324SBjoern A. Zeeb 	/* We only can handle the simple cases. */
12131e031324SBjoern A. Zeeb 	if (conf->xack != conf->nr || conf->ns != conf->rack)
12141e031324SBjoern A. Zeeb 		return (EINVAL);
12151e031324SBjoern A. Zeeb 
12161e031324SBjoern A. Zeeb 	/* Set ns,nr,rack,xack parameters. */
12171e031324SBjoern A. Zeeb 	seq->ns = conf->ns;
12181e031324SBjoern A. Zeeb 	seq->nr = conf->nr;
12191e031324SBjoern A. Zeeb 	seq->rack = conf->rack;
12201e031324SBjoern A. Zeeb 	seq->xack = conf->xack;
12211e031324SBjoern A. Zeeb 
12221e031324SBjoern A. Zeeb 	return (0);
12231e031324SBjoern A. Zeeb }
12241e031324SBjoern A. Zeeb 
12251e031324SBjoern A. Zeeb /*
1226901fadf7SArchie Cobbs  * Adjust sequence number state accordingly after reconfiguration.
1227901fadf7SArchie Cobbs  */
1228901fadf7SArchie Cobbs static int
1229901fadf7SArchie Cobbs ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1230901fadf7SArchie Cobbs {
1231901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1232901fadf7SArchie Cobbs 	u_int16_t new_wmax;
12330a76c63dSGleb Smirnoff 	int error = 0;
1234901fadf7SArchie Cobbs 
12350a76c63dSGleb Smirnoff 	SEQ_LOCK(seq);
1236901fadf7SArchie Cobbs 	/* If disabling node, reset state sequence number */
1237901fadf7SArchie Cobbs 	if (!conf->enabled) {
1238901fadf7SArchie Cobbs 		ng_l2tp_seq_reset(priv);
12390a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
1240901fadf7SArchie Cobbs 		return (0);
1241901fadf7SArchie Cobbs 	}
1242901fadf7SArchie Cobbs 
1243901fadf7SArchie Cobbs 	/* Adjust peer's max recv window; it can only increase */
1244901fadf7SArchie Cobbs 	new_wmax = conf->peer_win;
1245901fadf7SArchie Cobbs 	if (new_wmax > L2TP_MAX_XWIN)
1246901fadf7SArchie Cobbs 		new_wmax = L2TP_MAX_XWIN;
1247901fadf7SArchie Cobbs 	if (new_wmax == 0)
12480a76c63dSGleb Smirnoff 		ERROUT(EINVAL);
1249901fadf7SArchie Cobbs 	if (new_wmax < seq->wmax)
12500a76c63dSGleb Smirnoff 		ERROUT(EBUSY);
1251901fadf7SArchie Cobbs 	seq->wmax = new_wmax;
1252901fadf7SArchie Cobbs 
12530a76c63dSGleb Smirnoff done:
12540a76c63dSGleb Smirnoff 	SEQ_UNLOCK(seq);
12550a76c63dSGleb Smirnoff 	return (error);
1256901fadf7SArchie Cobbs }
1257901fadf7SArchie Cobbs 
1258901fadf7SArchie Cobbs /*
1259901fadf7SArchie Cobbs  * Reset sequence number state.
1260901fadf7SArchie Cobbs  */
1261901fadf7SArchie Cobbs static void
1262901fadf7SArchie Cobbs ng_l2tp_seq_reset(priv_p priv)
1263901fadf7SArchie Cobbs {
1264901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1265901fadf7SArchie Cobbs 	hook_p hook;
1266901fadf7SArchie Cobbs 	int i;
1267901fadf7SArchie Cobbs 
12680a76c63dSGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1269901fadf7SArchie Cobbs 
1270901fadf7SArchie Cobbs 	/* Stop timers */
12710ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->rack_timer, priv->node);
12720ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->xack_timer, priv->node);
1273901fadf7SArchie Cobbs 
1274901fadf7SArchie Cobbs 	/* Free retransmit queue */
1275901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN; i++) {
1276901fadf7SArchie Cobbs 		if (seq->xwin[i] == NULL)
1277901fadf7SArchie Cobbs 			break;
1278901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1279901fadf7SArchie Cobbs 	}
1280901fadf7SArchie Cobbs 
1281901fadf7SArchie Cobbs 	/* Reset session hooks' sequence number states */
1282901fadf7SArchie Cobbs 	NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1283901fadf7SArchie Cobbs 
1284901fadf7SArchie Cobbs 	/* Reset node's sequence number state */
1285702f9895SAlexander Motin 	seq->ns = 0;
1286702f9895SAlexander Motin 	seq->nr = 0;
1287702f9895SAlexander Motin 	seq->rack = 0;
1288702f9895SAlexander Motin 	seq->xack = 0;
1289901fadf7SArchie Cobbs 	seq->wmax = L2TP_MAX_XWIN;
1290702f9895SAlexander Motin 	seq->cwnd = 1;
1291901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
1292702f9895SAlexander Motin 	seq->acks = 0;
1293702f9895SAlexander Motin 	seq->rexmits = 0;
1294702f9895SAlexander Motin 	bzero(seq->xwin, sizeof(seq->xwin));
1295901fadf7SArchie Cobbs }
1296901fadf7SArchie Cobbs 
1297901fadf7SArchie Cobbs /*
1298901fadf7SArchie Cobbs  * Handle receipt of an acknowledgement value (Nr) from peer.
1299901fadf7SArchie Cobbs  */
1300901fadf7SArchie Cobbs static void
1301901fadf7SArchie Cobbs ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1302901fadf7SArchie Cobbs {
1303901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1304702f9895SAlexander Motin 	struct mbuf	*xwin[L2TP_MAX_XWIN];	/* partial local copy */
1305901fadf7SArchie Cobbs 	int		nack;
1306702f9895SAlexander Motin 	int		i, j;
1307702f9895SAlexander Motin 	uint16_t	ns;
1308702f9895SAlexander Motin 
13090a76c63dSGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1310901fadf7SArchie Cobbs 
1311901fadf7SArchie Cobbs 	/* Verify peer's ACK is in range */
13120a76c63dSGleb Smirnoff 	if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0)
1313901fadf7SArchie Cobbs 		return;				/* duplicate ack */
1314901fadf7SArchie Cobbs 	if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1315901fadf7SArchie Cobbs 		priv->stats.recvBadAcks++;	/* ack for packet not sent */
1316901fadf7SArchie Cobbs 		return;
1317901fadf7SArchie Cobbs 	}
1318901fadf7SArchie Cobbs 	KASSERT(nack <= L2TP_MAX_XWIN,
13197e2b43eeSDavid E. O'Brien 	    ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1320901fadf7SArchie Cobbs 
1321901fadf7SArchie Cobbs 	/* Update receive ack stats */
1322901fadf7SArchie Cobbs 	seq->rack = nr;
1323901fadf7SArchie Cobbs 	seq->rexmits = 0;
1324901fadf7SArchie Cobbs 
1325901fadf7SArchie Cobbs 	/* Free acknowledged packets and shift up packets in the xmit queue */
1326901fadf7SArchie Cobbs 	for (i = 0; i < nack; i++)
1327901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1328901fadf7SArchie Cobbs 	memmove(seq->xwin, seq->xwin + nack,
1329901fadf7SArchie Cobbs 	    (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1330901fadf7SArchie Cobbs 	memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1331901fadf7SArchie Cobbs 	    nack * sizeof(*seq->xwin));
1332901fadf7SArchie Cobbs 
1333901fadf7SArchie Cobbs 	/*
1334901fadf7SArchie Cobbs 	 * Do slow-start/congestion avoidance windowing algorithm described
1335901fadf7SArchie Cobbs 	 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1336901fadf7SArchie Cobbs 	 * ACK had arrived separately.
1337901fadf7SArchie Cobbs 	 */
1338901fadf7SArchie Cobbs 	if (seq->cwnd < seq->wmax) {
1339901fadf7SArchie Cobbs 		/* Handle slow start phase */
1340901fadf7SArchie Cobbs 		if (seq->cwnd < seq->ssth) {
1341901fadf7SArchie Cobbs 			seq->cwnd += nack;
1342901fadf7SArchie Cobbs 			nack = 0;
1343901fadf7SArchie Cobbs 			if (seq->cwnd > seq->ssth) {	/* into cg.av. phase */
1344901fadf7SArchie Cobbs 				nack = seq->cwnd - seq->ssth;
1345901fadf7SArchie Cobbs 				seq->cwnd = seq->ssth;
1346901fadf7SArchie Cobbs 			}
1347901fadf7SArchie Cobbs 		}
1348901fadf7SArchie Cobbs 
1349901fadf7SArchie Cobbs 		/* Handle congestion avoidance phase */
1350901fadf7SArchie Cobbs 		if (seq->cwnd >= seq->ssth) {
1351901fadf7SArchie Cobbs 			seq->acks += nack;
1352901fadf7SArchie Cobbs 			while (seq->acks >= seq->cwnd) {
1353901fadf7SArchie Cobbs 				seq->acks -= seq->cwnd;
1354901fadf7SArchie Cobbs 				if (seq->cwnd < seq->wmax)
1355901fadf7SArchie Cobbs 					seq->cwnd++;
1356901fadf7SArchie Cobbs 			}
1357901fadf7SArchie Cobbs 		}
1358901fadf7SArchie Cobbs 	}
1359901fadf7SArchie Cobbs 
1360901fadf7SArchie Cobbs 	/* Stop xmit timer */
1361206fa244SAlexander Motin 	if (callout_active(&seq->rack_timer))
13620ef8db8fSGleb Smirnoff 		ng_uncallout(&seq->rack_timer, priv->node);
1363901fadf7SArchie Cobbs 
1364901fadf7SArchie Cobbs 	/* If transmit queue is empty, we're done for now */
13650a76c63dSGleb Smirnoff 	if (seq->xwin[0] == NULL)
1366901fadf7SArchie Cobbs 		return;
1367901fadf7SArchie Cobbs 
1368901fadf7SArchie Cobbs 	/* Start restransmit timer again */
13690ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, priv->node, NULL,
13700ef8db8fSGleb Smirnoff 	    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1371901fadf7SArchie Cobbs 
1372901fadf7SArchie Cobbs 	/*
1373901fadf7SArchie Cobbs 	 * Send more packets, trying to keep peer's receive window full.
1374702f9895SAlexander Motin 	 * Make copy of everything we need before lock release.
1375702f9895SAlexander Motin 	 */
1376702f9895SAlexander Motin 	ns = seq->ns;
1377702f9895SAlexander Motin 	j = 0;
1378702f9895SAlexander Motin 	while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1379702f9895SAlexander Motin 	    && seq->xwin[i] != NULL) {
1380702f9895SAlexander Motin 		xwin[j++] = seq->xwin[i];
1381702f9895SAlexander Motin 		seq->ns++;
1382702f9895SAlexander Motin 	}
1383702f9895SAlexander Motin 
1384702f9895SAlexander Motin 	/*
1385702f9895SAlexander Motin 	 * Send prepared.
1386901fadf7SArchie Cobbs 	 * If there is a memory error, pretend packet was sent, as it
1387901fadf7SArchie Cobbs 	 * will get retransmitted later anyway.
1388901fadf7SArchie Cobbs 	 */
1389702f9895SAlexander Motin 	for (i = 0; i < j; i++) {
1390702f9895SAlexander Motin 		struct mbuf 	*m;
1391eb1b1807SGleb Smirnoff 		if ((m = L2TP_COPY_MBUF(xwin[i], M_NOWAIT)) == NULL)
1392901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
13930a76c63dSGleb Smirnoff 		else {
1394702f9895SAlexander Motin 			ng_l2tp_xmit_ctrl(priv, m, ns);
13950a76c63dSGleb Smirnoff 			SEQ_LOCK(seq);
13960a76c63dSGleb Smirnoff 		}
1397702f9895SAlexander Motin 		ns++;
1398901fadf7SArchie Cobbs 	}
1399901fadf7SArchie Cobbs }
1400901fadf7SArchie Cobbs 
1401901fadf7SArchie Cobbs /*
1402901fadf7SArchie Cobbs  * Handle an ack timeout. We have an outstanding ack that we
1403901fadf7SArchie Cobbs  * were hoping to piggy-back, but haven't, so send a ZLB.
1404901fadf7SArchie Cobbs  */
1405901fadf7SArchie Cobbs static void
14060ef8db8fSGleb Smirnoff ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1407901fadf7SArchie Cobbs {
1408901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1409901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1410901fadf7SArchie Cobbs 
1411*89042ff7SGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1412*89042ff7SGleb Smirnoff 	MPASS(!callout_pending(&seq->xack_timer));
1413*89042ff7SGleb Smirnoff 	MPASS(callout_active(&seq->xack_timer));
1414901fadf7SArchie Cobbs 
1415206fa244SAlexander Motin 	/* Send a ZLB */
1416901fadf7SArchie Cobbs 	ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1417901fadf7SArchie Cobbs 
1418206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1419206fa244SAlexander Motin 	    as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1420901fadf7SArchie Cobbs }
1421901fadf7SArchie Cobbs 
1422901fadf7SArchie Cobbs /*
1423901fadf7SArchie Cobbs  * Handle a transmit timeout. The peer has failed to respond
1424901fadf7SArchie Cobbs  * with an ack for our packet, so retransmit it.
1425901fadf7SArchie Cobbs  */
1426901fadf7SArchie Cobbs static void
14270ef8db8fSGleb Smirnoff ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1428901fadf7SArchie Cobbs {
1429901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1430901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1431901fadf7SArchie Cobbs 	struct mbuf *m;
1432901fadf7SArchie Cobbs 	u_int delay;
1433901fadf7SArchie Cobbs 
1434*89042ff7SGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1435*89042ff7SGleb Smirnoff 	MPASS(seq->xwin[0]);
1436*89042ff7SGleb Smirnoff 	MPASS(!callout_pending(&seq->rack_timer));
1437*89042ff7SGleb Smirnoff 	MPASS(callout_active(&seq->rack_timer));
1438e62e4b85SMark Johnston 
1439901fadf7SArchie Cobbs 	priv->stats.xmitRetransmits++;
1440901fadf7SArchie Cobbs 
1441901fadf7SArchie Cobbs 	/* Have we reached the retransmit limit? If so, notify owner. */
144240097c5dSAlexander Motin 	if (seq->rexmits++ >= priv->conf.rexmit_max)
1443901fadf7SArchie Cobbs 		ng_l2tp_seq_failure(priv);
1444901fadf7SArchie Cobbs 
1445901fadf7SArchie Cobbs 	/* Restart timer, this time with an increased delay */
1446901fadf7SArchie Cobbs 	delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
144740097c5dSAlexander Motin 	if (delay > priv->conf.rexmit_max_to)
144840097c5dSAlexander Motin 		delay = priv->conf.rexmit_max_to;
14490ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, node, NULL,
14500ef8db8fSGleb Smirnoff 	    hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1451901fadf7SArchie Cobbs 
1452901fadf7SArchie Cobbs 	/* Do slow-start/congestion algorithm windowing algorithm */
1453af63939cSAlexander Motin 	seq->ns = seq->rack;
1454901fadf7SArchie Cobbs 	seq->ssth = (seq->cwnd + 1) / 2;
1455901fadf7SArchie Cobbs 	seq->cwnd = 1;
1456901fadf7SArchie Cobbs 	seq->acks = 0;
1457901fadf7SArchie Cobbs 
1458901fadf7SArchie Cobbs 	/* Retransmit oldest unack'd packet */
145930b7addfSGleb Smirnoff 	m = L2TP_COPY_MBUF(seq->xwin[0], M_NOWAIT);
14600a76c63dSGleb Smirnoff 	if (m == NULL) {
14610a76c63dSGleb Smirnoff 		SEQ_UNLOCK(seq);
1462901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
14630a76c63dSGleb Smirnoff 	} else
1464af63939cSAlexander Motin 		ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1465901fadf7SArchie Cobbs 
1466206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1467206fa244SAlexander Motin 	    as ng_callout() is getting called each time */
1468901fadf7SArchie Cobbs }
1469901fadf7SArchie Cobbs 
1470901fadf7SArchie Cobbs /*
1471901fadf7SArchie Cobbs  * Transmit a control stream packet, payload optional.
1472901fadf7SArchie Cobbs  * The transmit sequence number is not incremented.
14730a76c63dSGleb Smirnoff  * Requires seq lock, returns unlocked.
1474901fadf7SArchie Cobbs  */
1475901fadf7SArchie Cobbs static int
1476901fadf7SArchie Cobbs ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1477901fadf7SArchie Cobbs {
1478901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1479148ac1daSAlexander Motin 	uint8_t *p;
14800a76c63dSGleb Smirnoff 	uint16_t nr, session_id = 0;
1481901fadf7SArchie Cobbs 	int error;
1482901fadf7SArchie Cobbs 
14830a76c63dSGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1484702f9895SAlexander Motin 
1485206fa244SAlexander Motin 	/* Stop ack timer: we're sending an ack with this packet.
1486206fa244SAlexander Motin 	   Doing this before to keep state predictable after error. */
1487206fa244SAlexander Motin 	if (callout_active(&seq->xack_timer))
1488206fa244SAlexander Motin 		ng_uncallout(&seq->xack_timer, priv->node);
1489206fa244SAlexander Motin 
14900a76c63dSGleb Smirnoff 	nr = seq->xack = seq->nr;
1491206fa244SAlexander Motin 
14920a76c63dSGleb Smirnoff 	SEQ_UNLOCK(seq);
1493702f9895SAlexander Motin 
1494901fadf7SArchie Cobbs 	/* If no mbuf passed, send an empty packet (ZLB) */
1495901fadf7SArchie Cobbs 	if (m == NULL) {
1496901fadf7SArchie Cobbs 		/* Create a new mbuf for ZLB packet */
1497eb1b1807SGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_DATA);
1498901fadf7SArchie Cobbs 		if (m == NULL) {
1499901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1500901fadf7SArchie Cobbs 			return (ENOBUFS);
1501901fadf7SArchie Cobbs 		}
1502901fadf7SArchie Cobbs 		m->m_len = m->m_pkthdr.len = 12;
1503901fadf7SArchie Cobbs 		m->m_pkthdr.rcvif = NULL;
1504901fadf7SArchie Cobbs 		priv->stats.xmitZLBs++;
1505901fadf7SArchie Cobbs 	} else {
1506901fadf7SArchie Cobbs 		/* Strip off session ID */
1507901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1508901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1509901fadf7SArchie Cobbs 			return (ENOBUFS);
1510901fadf7SArchie Cobbs 		}
1511280d6bd7SAlexander Motin 		session_id = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
1512901fadf7SArchie Cobbs 
1513901fadf7SArchie Cobbs 		/* Make room for L2TP header */
1514eb1b1807SGleb Smirnoff 		M_PREPEND(m, 10, M_NOWAIT);	/* - 2 + 12 = 10 */
1515901fadf7SArchie Cobbs 		if (m == NULL) {
1516901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1517901fadf7SArchie Cobbs 			return (ENOBUFS);
1518901fadf7SArchie Cobbs 		}
1519310dc5a4SBjoern A. Zeeb 
1520310dc5a4SBjoern A. Zeeb 		/*
1521310dc5a4SBjoern A. Zeeb 		 * The below requires 12 contiguous bytes for the L2TP header
1522310dc5a4SBjoern A. Zeeb 		 * to be written into.
1523310dc5a4SBjoern A. Zeeb 		 */
1524310dc5a4SBjoern A. Zeeb 		m = m_pullup(m, 12);
1525310dc5a4SBjoern A. Zeeb 		if (m == NULL) {
1526310dc5a4SBjoern A. Zeeb 			priv->stats.memoryFailures++;
1527310dc5a4SBjoern A. Zeeb 			return (ENOBUFS);
1528310dc5a4SBjoern A. Zeeb 		}
1529901fadf7SArchie Cobbs 	}
1530901fadf7SArchie Cobbs 
1531901fadf7SArchie Cobbs 	/* Fill in L2TP header */
1532148ac1daSAlexander Motin 	p = mtod(m, u_int8_t *);
1533148ac1daSAlexander Motin 	p[0] = L2TP_CTRL_HDR >> 8;
1534148ac1daSAlexander Motin 	p[1] = L2TP_CTRL_HDR & 0xff;
1535148ac1daSAlexander Motin 	p[2] = m->m_pkthdr.len >> 8;
1536148ac1daSAlexander Motin 	p[3] = m->m_pkthdr.len & 0xff;
1537148ac1daSAlexander Motin 	p[4] = priv->conf.peer_id >> 8;
1538148ac1daSAlexander Motin 	p[5] = priv->conf.peer_id & 0xff;
1539148ac1daSAlexander Motin 	p[6] = session_id >> 8;
1540148ac1daSAlexander Motin 	p[7] = session_id & 0xff;
1541148ac1daSAlexander Motin 	p[8] = ns >> 8;
1542148ac1daSAlexander Motin 	p[9] = ns & 0xff;
15430a76c63dSGleb Smirnoff 	p[10] = nr >> 8;
15440a76c63dSGleb Smirnoff 	p[11] = nr & 0xff;
1545901fadf7SArchie Cobbs 
1546901fadf7SArchie Cobbs 	/* Update sequence number info and stats */
1547901fadf7SArchie Cobbs 	priv->stats.xmitPackets++;
1548901fadf7SArchie Cobbs 	priv->stats.xmitOctets += m->m_pkthdr.len;
1549901fadf7SArchie Cobbs 
1550901fadf7SArchie Cobbs 	/* Send packet */
15513ca24c28SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->lower, m);
1552901fadf7SArchie Cobbs 	return (error);
1553901fadf7SArchie Cobbs }
1554901fadf7SArchie Cobbs 
1555901fadf7SArchie Cobbs #ifdef INVARIANTS
1556901fadf7SArchie Cobbs /*
1557901fadf7SArchie Cobbs  * Sanity check sequence number state.
1558901fadf7SArchie Cobbs  */
1559901fadf7SArchie Cobbs static void
1560901fadf7SArchie Cobbs ng_l2tp_seq_check(struct l2tp_seq *seq)
1561901fadf7SArchie Cobbs {
1562702f9895SAlexander Motin 	int self_unack, peer_unack;
1563901fadf7SArchie Cobbs 	int i;
1564901fadf7SArchie Cobbs 
15657e2b43eeSDavid E. O'Brien #define CHECK(p)	KASSERT((p), ("%s: not: %s", __func__, #p))
1566901fadf7SArchie Cobbs 
15670a76c63dSGleb Smirnoff 	SEQ_LOCK_ASSERT(seq);
1568702f9895SAlexander Motin 
1569702f9895SAlexander Motin 	self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1570702f9895SAlexander Motin 	peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1571901fadf7SArchie Cobbs 	CHECK(seq->wmax <= L2TP_MAX_XWIN);
1572901fadf7SArchie Cobbs 	CHECK(seq->cwnd >= 1);
1573901fadf7SArchie Cobbs 	CHECK(seq->cwnd <= seq->wmax);
1574901fadf7SArchie Cobbs 	CHECK(seq->ssth >= 1);
1575901fadf7SArchie Cobbs 	CHECK(seq->ssth <= seq->wmax);
1576901fadf7SArchie Cobbs 	if (seq->cwnd < seq->ssth)
1577901fadf7SArchie Cobbs 		CHECK(seq->acks == 0);
1578901fadf7SArchie Cobbs 	else
1579901fadf7SArchie Cobbs 		CHECK(seq->acks <= seq->cwnd);
1580901fadf7SArchie Cobbs 	CHECK(self_unack >= 0);
1581901fadf7SArchie Cobbs 	CHECK(peer_unack >= 0);
1582901fadf7SArchie Cobbs 	CHECK(peer_unack <= seq->wmax);
1583206fa244SAlexander Motin 	CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1584206fa244SAlexander Motin 	CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1585901fadf7SArchie Cobbs 	for (i = 0; i < peer_unack; i++)
1586901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] != NULL);
1587901fadf7SArchie Cobbs 	for ( ; i < seq->cwnd; i++)	    /* verify peer's recv window full */
1588901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] == NULL);
1589901fadf7SArchie Cobbs 
1590901fadf7SArchie Cobbs #undef CHECK
1591901fadf7SArchie Cobbs }
1592901fadf7SArchie Cobbs #endif	/* INVARIANTS */
1593