xref: /freebsd/sys/netgraph/ng_l2tp.c (revision 30b7addf5acdb9f73a38ea014ff1533c4cce91ff)
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 
191901fadf7SArchie Cobbs #ifdef INVARIANTS
192901fadf7SArchie Cobbs static void	ng_l2tp_seq_check(struct l2tp_seq *seq);
193901fadf7SArchie Cobbs #endif
194901fadf7SArchie Cobbs 
1951e031324SBjoern A. Zeeb /* Parse type for struct ng_l2tp_seq_config. */
1961e031324SBjoern A. Zeeb static const struct ng_parse_struct_field
1971e031324SBjoern A. Zeeb 	ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
1981e031324SBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_seq_config_type = {
1991e031324SBjoern A. Zeeb 	&ng_parse_struct_type,
2001e031324SBjoern A. Zeeb 	&ng_l2tp_seq_config_fields
2011e031324SBjoern A. Zeeb };
2021e031324SBjoern A. Zeeb 
203901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_config */
204901fadf7SArchie Cobbs static const struct ng_parse_struct_field
205901fadf7SArchie Cobbs 	ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
206901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_config_type = {
207901fadf7SArchie Cobbs 	&ng_parse_struct_type,
208901fadf7SArchie Cobbs 	&ng_l2tp_config_type_fields,
209901fadf7SArchie Cobbs };
210901fadf7SArchie Cobbs 
211901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_sess_config */
212901fadf7SArchie Cobbs static const struct ng_parse_struct_field
213901fadf7SArchie Cobbs 	ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
214901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_sess_config_type = {
215901fadf7SArchie Cobbs 	&ng_parse_struct_type,
216901fadf7SArchie Cobbs 	&ng_l2tp_sess_config_type_fields,
217901fadf7SArchie Cobbs };
218901fadf7SArchie Cobbs 
219901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_stats */
220901fadf7SArchie Cobbs static const struct ng_parse_struct_field
221901fadf7SArchie Cobbs 	ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
2222c9027fcSArchie Cobbs static const struct ng_parse_type ng_l2tp_stats_type = {
223901fadf7SArchie Cobbs 	&ng_parse_struct_type,
224901fadf7SArchie Cobbs 	&ng_l2tp_stats_type_fields
225901fadf7SArchie Cobbs };
226901fadf7SArchie Cobbs 
2274807330cSBjoern A. Zeeb /* Parse type for struct ng_l2tp_session_stats. */
2284807330cSBjoern A. Zeeb static const struct ng_parse_struct_field
2294807330cSBjoern A. Zeeb 	ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
2304807330cSBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_session_stats_type = {
2314807330cSBjoern A. Zeeb 	&ng_parse_struct_type,
2324807330cSBjoern A. Zeeb 	&ng_l2tp_session_stats_type_fields
2334807330cSBjoern A. Zeeb };
2344807330cSBjoern A. Zeeb 
235901fadf7SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
236901fadf7SArchie Cobbs static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
237901fadf7SArchie Cobbs 	{
238901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
239901fadf7SArchie Cobbs 	  NGM_L2TP_SET_CONFIG,
240901fadf7SArchie Cobbs 	  "setconfig",
241901fadf7SArchie Cobbs 	  &ng_l2tp_config_type,
242901fadf7SArchie Cobbs 	  NULL
243901fadf7SArchie Cobbs 	},
244901fadf7SArchie Cobbs 	{
245901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
246901fadf7SArchie Cobbs 	  NGM_L2TP_GET_CONFIG,
247901fadf7SArchie Cobbs 	  "getconfig",
248901fadf7SArchie Cobbs 	  NULL,
249901fadf7SArchie Cobbs 	  &ng_l2tp_config_type
250901fadf7SArchie Cobbs 	},
251901fadf7SArchie Cobbs 	{
252901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
253901fadf7SArchie Cobbs 	  NGM_L2TP_SET_SESS_CONFIG,
254901fadf7SArchie Cobbs 	  "setsessconfig",
255901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type,
256901fadf7SArchie Cobbs 	  NULL
257901fadf7SArchie Cobbs 	},
258901fadf7SArchie Cobbs 	{
259901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
260901fadf7SArchie Cobbs 	  NGM_L2TP_GET_SESS_CONFIG,
261901fadf7SArchie Cobbs 	  "getsessconfig",
262901fadf7SArchie Cobbs 	  &ng_parse_hint16_type,
263901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type
264901fadf7SArchie Cobbs 	},
265901fadf7SArchie Cobbs 	{
266901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
267901fadf7SArchie Cobbs 	  NGM_L2TP_GET_STATS,
268901fadf7SArchie Cobbs 	  "getstats",
269901fadf7SArchie Cobbs 	  NULL,
2702c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
271901fadf7SArchie Cobbs 	},
272901fadf7SArchie Cobbs 	{
273901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
274901fadf7SArchie Cobbs 	  NGM_L2TP_CLR_STATS,
275901fadf7SArchie Cobbs 	  "clrstats",
276901fadf7SArchie Cobbs 	  NULL,
277901fadf7SArchie Cobbs 	  NULL
278901fadf7SArchie Cobbs 	},
279901fadf7SArchie Cobbs 	{
280901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
281901fadf7SArchie Cobbs 	  NGM_L2TP_GETCLR_STATS,
282901fadf7SArchie Cobbs 	  "getclrstats",
283901fadf7SArchie Cobbs 	  NULL,
2842c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
285901fadf7SArchie Cobbs 	},
286901fadf7SArchie Cobbs 	{
287901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
2884807330cSBjoern A. Zeeb 	  NGM_L2TP_GET_SESSION_STATS,
2894807330cSBjoern A. Zeeb 	  "getsessstats",
2904807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2914807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
2924807330cSBjoern A. Zeeb 	},
2934807330cSBjoern A. Zeeb 	{
2944807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
2954807330cSBjoern A. Zeeb 	  NGM_L2TP_CLR_SESSION_STATS,
2964807330cSBjoern A. Zeeb 	  "clrsessstats",
2974807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2984807330cSBjoern A. Zeeb 	  NULL
2994807330cSBjoern A. Zeeb 	},
3004807330cSBjoern A. Zeeb 	{
3014807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
3024807330cSBjoern A. Zeeb 	  NGM_L2TP_GETCLR_SESSION_STATS,
3034807330cSBjoern A. Zeeb 	  "getclrsessstats",
3044807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
3054807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
3064807330cSBjoern A. Zeeb 	},
3074807330cSBjoern A. Zeeb 	{
3084807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
309901fadf7SArchie Cobbs 	  NGM_L2TP_ACK_FAILURE,
310901fadf7SArchie Cobbs 	  "ackfailure",
311901fadf7SArchie Cobbs 	  NULL,
312901fadf7SArchie Cobbs 	  NULL
313901fadf7SArchie Cobbs 	},
3141e031324SBjoern A. Zeeb 	{
3151e031324SBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
3161e031324SBjoern A. Zeeb 	  NGM_L2TP_SET_SEQ,
3171e031324SBjoern A. Zeeb 	  "setsequence",
3181e031324SBjoern A. Zeeb 	  &ng_l2tp_seq_config_type,
3191e031324SBjoern A. Zeeb 	  NULL
3201e031324SBjoern A. Zeeb 	},
321901fadf7SArchie Cobbs 	{ 0 }
322901fadf7SArchie Cobbs };
323901fadf7SArchie Cobbs 
324901fadf7SArchie Cobbs /* Node type descriptor */
325901fadf7SArchie Cobbs static struct ng_type ng_l2tp_typestruct = {
326f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
327f8aae777SJulian Elischer 	.name =		NG_L2TP_NODE_TYPE,
328f8aae777SJulian Elischer 	.constructor =	ng_l2tp_constructor,
329f8aae777SJulian Elischer 	.rcvmsg =	ng_l2tp_rcvmsg,
330f8aae777SJulian Elischer 	.shutdown =	ng_l2tp_shutdown,
331f8aae777SJulian Elischer 	.newhook =	ng_l2tp_newhook,
332f8aae777SJulian Elischer 	.rcvdata =	ng_l2tp_rcvdata,
333f8aae777SJulian Elischer 	.disconnect =	ng_l2tp_disconnect,
334f8aae777SJulian Elischer 	.cmdlist =	ng_l2tp_cmdlist,
335901fadf7SArchie Cobbs };
336901fadf7SArchie Cobbs NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
337901fadf7SArchie Cobbs 
338901fadf7SArchie Cobbs /* Sequence number state sanity checking */
339901fadf7SArchie Cobbs #ifdef INVARIANTS
340901fadf7SArchie Cobbs #define L2TP_SEQ_CHECK(seq)	ng_l2tp_seq_check(seq)
341901fadf7SArchie Cobbs #else
342901fadf7SArchie Cobbs #define L2TP_SEQ_CHECK(x)	do { } while (0)
343901fadf7SArchie Cobbs #endif
344901fadf7SArchie Cobbs 
345901fadf7SArchie Cobbs /* Whether to use m_copypacket() or m_dup() */
346901fadf7SArchie Cobbs #define L2TP_COPY_MBUF		m_copypacket
347901fadf7SArchie Cobbs 
348bf741e4dSAlexander Motin #define ERROUT(x)	do { error = (x); goto done; } while (0)
349bf741e4dSAlexander Motin 
350901fadf7SArchie Cobbs /************************************************************************
351901fadf7SArchie Cobbs 			NETGRAPH NODE STUFF
352901fadf7SArchie Cobbs ************************************************************************/
353901fadf7SArchie Cobbs 
354901fadf7SArchie Cobbs /*
355901fadf7SArchie Cobbs  * Node type constructor
356901fadf7SArchie Cobbs  */
357901fadf7SArchie Cobbs static int
358901fadf7SArchie Cobbs ng_l2tp_constructor(node_p node)
359901fadf7SArchie Cobbs {
360901fadf7SArchie Cobbs 	priv_p priv;
3614e759763SAlexander Motin 	int	i;
362901fadf7SArchie Cobbs 
363901fadf7SArchie Cobbs 	/* Allocate private structure */
364674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_L2TP, M_WAITOK | M_ZERO);
365901fadf7SArchie Cobbs 	NG_NODE_SET_PRIVATE(node, priv);
366901fadf7SArchie Cobbs 	priv->node = node;
367901fadf7SArchie Cobbs 
368901fadf7SArchie Cobbs 	/* Apply a semi-reasonable default configuration */
369901fadf7SArchie Cobbs 	priv->conf.peer_win = 1;
370901fadf7SArchie Cobbs 	priv->conf.rexmit_max = L2TP_MAX_REXMIT;
371901fadf7SArchie Cobbs 	priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
372901fadf7SArchie Cobbs 
373901fadf7SArchie Cobbs 	/* Initialize sequence number state */
374901fadf7SArchie Cobbs 	ng_l2tp_seq_init(priv);
375901fadf7SArchie Cobbs 
3764e759763SAlexander Motin 	for (i = 0; i < SESSHASHSIZE; i++)
3774e759763SAlexander Motin 	    LIST_INIT(&priv->sesshash[i]);
3784e759763SAlexander Motin 
379901fadf7SArchie Cobbs 	/* Done */
380901fadf7SArchie Cobbs 	return (0);
381901fadf7SArchie Cobbs }
382901fadf7SArchie Cobbs 
383901fadf7SArchie Cobbs /*
384901fadf7SArchie Cobbs  * Give our OK for a hook to be added.
385901fadf7SArchie Cobbs  */
386901fadf7SArchie Cobbs static int
387901fadf7SArchie Cobbs ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
388901fadf7SArchie Cobbs {
389901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
390901fadf7SArchie Cobbs 
391901fadf7SArchie Cobbs 	/* Check hook name */
392901fadf7SArchie Cobbs 	if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
393901fadf7SArchie Cobbs 		if (priv->ctrl != NULL)
394901fadf7SArchie Cobbs 			return (EISCONN);
395901fadf7SArchie Cobbs 		priv->ctrl = hook;
396bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
397901fadf7SArchie Cobbs 	} else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
398901fadf7SArchie Cobbs 		if (priv->lower != NULL)
399901fadf7SArchie Cobbs 			return (EISCONN);
400901fadf7SArchie Cobbs 		priv->lower = hook;
401bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
402901fadf7SArchie Cobbs 	} else {
403901fadf7SArchie Cobbs 		static const char hexdig[16] = "0123456789abcdef";
404901fadf7SArchie Cobbs 		u_int16_t session_id;
405901fadf7SArchie Cobbs 		hookpriv_p hpriv;
4064e759763SAlexander Motin 		uint16_t hash;
407901fadf7SArchie Cobbs 		const char *hex;
408901fadf7SArchie Cobbs 		int i;
409901fadf7SArchie Cobbs 		int j;
410901fadf7SArchie Cobbs 
411901fadf7SArchie Cobbs 		/* Parse hook name to get session ID */
412901fadf7SArchie Cobbs 		if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
413901fadf7SArchie Cobbs 		    sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
414901fadf7SArchie Cobbs 			return (EINVAL);
415901fadf7SArchie Cobbs 		hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
416901fadf7SArchie Cobbs 		for (session_id = i = 0; i < 4; i++) {
417901fadf7SArchie Cobbs 			for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
418901fadf7SArchie Cobbs 			if (j == 16)
419901fadf7SArchie Cobbs 				return (EINVAL);
420901fadf7SArchie Cobbs 			session_id = (session_id << 4) | j;
421901fadf7SArchie Cobbs 		}
422901fadf7SArchie Cobbs 		if (hex[i] != '\0')
423901fadf7SArchie Cobbs 			return (EINVAL);
424901fadf7SArchie Cobbs 
425901fadf7SArchie Cobbs 		/* Create hook private structure */
426e11e3f18SDag-Erling Smørgrav 		hpriv = malloc(sizeof(*hpriv),
427e11e3f18SDag-Erling Smørgrav 		    M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
428901fadf7SArchie Cobbs 		if (hpriv == NULL)
429901fadf7SArchie Cobbs 			return (ENOMEM);
430280d6bd7SAlexander Motin 		hpriv->conf.session_id = session_id;
431901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
432901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
4334e759763SAlexander Motin 		hpriv->hook = hook;
434901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, hpriv);
4354e759763SAlexander Motin 		hash = SESSHASH(hpriv->conf.session_id);
4364e759763SAlexander Motin 		LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
437901fadf7SArchie Cobbs 	}
438901fadf7SArchie Cobbs 
439901fadf7SArchie Cobbs 	/* Done */
440901fadf7SArchie Cobbs 	return (0);
441901fadf7SArchie Cobbs }
442901fadf7SArchie Cobbs 
443901fadf7SArchie Cobbs /*
444901fadf7SArchie Cobbs  * Receive a control message.
445901fadf7SArchie Cobbs  */
446901fadf7SArchie Cobbs static int
447901fadf7SArchie Cobbs ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
448901fadf7SArchie Cobbs {
449901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
450901fadf7SArchie Cobbs 	struct ng_mesg *resp = NULL;
451901fadf7SArchie Cobbs 	struct ng_mesg *msg;
452901fadf7SArchie Cobbs 	int error = 0;
453901fadf7SArchie Cobbs 
454901fadf7SArchie Cobbs 	NGI_GET_MSG(item, msg);
455901fadf7SArchie Cobbs 	switch (msg->header.typecookie) {
456901fadf7SArchie Cobbs 	case NGM_L2TP_COOKIE:
457901fadf7SArchie Cobbs 		switch (msg->header.cmd) {
458901fadf7SArchie Cobbs 		case NGM_L2TP_SET_CONFIG:
459901fadf7SArchie Cobbs 		    {
460901fadf7SArchie Cobbs 			struct ng_l2tp_config *const conf =
461901fadf7SArchie Cobbs 				(struct ng_l2tp_config *)msg->data;
462901fadf7SArchie Cobbs 
463901fadf7SArchie Cobbs 			/* Check for invalid or illegal config */
464901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
465901fadf7SArchie Cobbs 				error = EINVAL;
466901fadf7SArchie Cobbs 				break;
467901fadf7SArchie Cobbs 			}
468901fadf7SArchie Cobbs 			conf->enabled = !!conf->enabled;
469901fadf7SArchie Cobbs 			conf->match_id = !!conf->match_id;
470901fadf7SArchie Cobbs 			if (priv->conf.enabled
471901fadf7SArchie Cobbs 			    && ((priv->conf.tunnel_id != 0
472901fadf7SArchie Cobbs 			       && conf->tunnel_id != priv->conf.tunnel_id)
473901fadf7SArchie Cobbs 			      || ((priv->conf.peer_id != 0
474901fadf7SArchie Cobbs 			       && conf->peer_id != priv->conf.peer_id)))) {
475901fadf7SArchie Cobbs 				error = EBUSY;
476901fadf7SArchie Cobbs 				break;
477901fadf7SArchie Cobbs 			}
478901fadf7SArchie Cobbs 
479901fadf7SArchie Cobbs 			/* Save calling node as failure target */
480901fadf7SArchie Cobbs 			priv->ftarget = NGI_RETADDR(item);
481901fadf7SArchie Cobbs 
482901fadf7SArchie Cobbs 			/* Adjust sequence number state */
483901fadf7SArchie Cobbs 			if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
484901fadf7SArchie Cobbs 				break;
485901fadf7SArchie Cobbs 
486901fadf7SArchie Cobbs 			/* Update node's config */
487901fadf7SArchie Cobbs 			priv->conf = *conf;
488901fadf7SArchie Cobbs 			break;
489901fadf7SArchie Cobbs 		    }
490901fadf7SArchie Cobbs 		case NGM_L2TP_GET_CONFIG:
491901fadf7SArchie Cobbs 		    {
492901fadf7SArchie Cobbs 			struct ng_l2tp_config *conf;
493901fadf7SArchie Cobbs 
494901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
495901fadf7SArchie Cobbs 			if (resp == NULL) {
496901fadf7SArchie Cobbs 				error = ENOMEM;
497901fadf7SArchie Cobbs 				break;
498901fadf7SArchie Cobbs 			}
499901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_config *)resp->data;
500901fadf7SArchie Cobbs 			*conf = priv->conf;
501901fadf7SArchie Cobbs 			break;
502901fadf7SArchie Cobbs 		    }
503901fadf7SArchie Cobbs 		case NGM_L2TP_SET_SESS_CONFIG:
504901fadf7SArchie Cobbs 		    {
505901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *const conf =
506901fadf7SArchie Cobbs 			    (struct ng_l2tp_sess_config *)msg->data;
507901fadf7SArchie Cobbs 			hookpriv_p hpriv;
508901fadf7SArchie Cobbs 
5091e031324SBjoern A. Zeeb 			/* Check for invalid or illegal config. */
510901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
511901fadf7SArchie Cobbs 				error = EINVAL;
512901fadf7SArchie Cobbs 				break;
513901fadf7SArchie Cobbs 			}
514901fadf7SArchie Cobbs 
515901fadf7SArchie Cobbs 			/* Find matching hook */
5164e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, conf->session_id);
5174e759763SAlexander Motin 			if (hpriv == NULL) {
518901fadf7SArchie Cobbs 				error = ENOENT;
519901fadf7SArchie Cobbs 				break;
520901fadf7SArchie Cobbs 			}
521901fadf7SArchie Cobbs 
522901fadf7SArchie Cobbs 			/* Update hook's config */
523901fadf7SArchie Cobbs 			hpriv->conf = *conf;
524901fadf7SArchie Cobbs 			break;
525901fadf7SArchie Cobbs 		    }
526901fadf7SArchie Cobbs 		case NGM_L2TP_GET_SESS_CONFIG:
527901fadf7SArchie Cobbs 		    {
528901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *conf;
529901fadf7SArchie Cobbs 			u_int16_t session_id;
530901fadf7SArchie Cobbs 			hookpriv_p hpriv;
531901fadf7SArchie Cobbs 
532901fadf7SArchie Cobbs 			/* Get session ID */
533901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(session_id)) {
534901fadf7SArchie Cobbs 				error = EINVAL;
535901fadf7SArchie Cobbs 				break;
536901fadf7SArchie Cobbs 			}
537901fadf7SArchie Cobbs 			memcpy(&session_id, msg->data, 2);
538901fadf7SArchie Cobbs 
539901fadf7SArchie Cobbs 			/* Find matching hook */
5404e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, session_id);
5414e759763SAlexander Motin 			if (hpriv == NULL) {
542901fadf7SArchie Cobbs 				error = ENOENT;
543901fadf7SArchie Cobbs 				break;
544901fadf7SArchie Cobbs 			}
545901fadf7SArchie Cobbs 
546901fadf7SArchie Cobbs 			/* Send response */
547901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
548901fadf7SArchie Cobbs 			if (resp == NULL) {
549901fadf7SArchie Cobbs 				error = ENOMEM;
550901fadf7SArchie Cobbs 				break;
551901fadf7SArchie Cobbs 			}
552901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_sess_config *)resp->data;
553901fadf7SArchie Cobbs 			*conf = hpriv->conf;
554901fadf7SArchie Cobbs 			break;
555901fadf7SArchie Cobbs 		    }
556901fadf7SArchie Cobbs 		case NGM_L2TP_GET_STATS:
557901fadf7SArchie Cobbs 		case NGM_L2TP_CLR_STATS:
558901fadf7SArchie Cobbs 		case NGM_L2TP_GETCLR_STATS:
559901fadf7SArchie Cobbs 		    {
560901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
561901fadf7SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
562901fadf7SArchie Cobbs 				    sizeof(priv->stats), M_NOWAIT);
563901fadf7SArchie Cobbs 				if (resp == NULL) {
564901fadf7SArchie Cobbs 					error = ENOMEM;
565901fadf7SArchie Cobbs 					break;
566901fadf7SArchie Cobbs 				}
567901fadf7SArchie Cobbs 				memcpy(resp->data,
568901fadf7SArchie Cobbs 				    &priv->stats, sizeof(priv->stats));
569901fadf7SArchie Cobbs 			}
570901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_GET_STATS)
571901fadf7SArchie Cobbs 				memset(&priv->stats, 0, sizeof(priv->stats));
572901fadf7SArchie Cobbs 			break;
573901fadf7SArchie Cobbs 		    }
5744807330cSBjoern A. Zeeb 		case NGM_L2TP_GET_SESSION_STATS:
5754807330cSBjoern A. Zeeb 		case NGM_L2TP_CLR_SESSION_STATS:
5764807330cSBjoern A. Zeeb 		case NGM_L2TP_GETCLR_SESSION_STATS:
5774807330cSBjoern A. Zeeb 		    {
5784807330cSBjoern A. Zeeb 			uint16_t session_id;
5794807330cSBjoern A. Zeeb 			hookpriv_p hpriv;
5804807330cSBjoern A. Zeeb 
5814807330cSBjoern A. Zeeb 			/* Get session ID. */
5824807330cSBjoern A. Zeeb 			if (msg->header.arglen != sizeof(session_id)) {
5834807330cSBjoern A. Zeeb 				error = EINVAL;
5844807330cSBjoern A. Zeeb 				break;
5854807330cSBjoern A. Zeeb 			}
5864807330cSBjoern A. Zeeb 			bcopy(msg->data, &session_id, sizeof(uint16_t));
5874807330cSBjoern A. Zeeb 
5884807330cSBjoern A. Zeeb 			/* Find matching hook. */
5894e759763SAlexander Motin 			hpriv = ng_l2tp_find_session(priv, session_id);
5904e759763SAlexander Motin 			if (hpriv == NULL) {
5914807330cSBjoern A. Zeeb 				error = ENOENT;
5924807330cSBjoern A. Zeeb 				break;
5934807330cSBjoern A. Zeeb 			}
5944807330cSBjoern A. Zeeb 
5954807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
5964807330cSBjoern A. Zeeb 				NG_MKRESPONSE(resp, msg,
5974807330cSBjoern A. Zeeb 				    sizeof(hpriv->stats), M_NOWAIT);
5984807330cSBjoern A. Zeeb 				if (resp == NULL) {
5994807330cSBjoern A. Zeeb 					error = ENOMEM;
6004807330cSBjoern A. Zeeb 					break;
6014807330cSBjoern A. Zeeb 				}
6024807330cSBjoern A. Zeeb 				bcopy(&hpriv->stats, resp->data,
6034807330cSBjoern A. Zeeb 					sizeof(hpriv->stats));
6044807330cSBjoern A. Zeeb 			}
6054807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
6064807330cSBjoern A. Zeeb 				bzero(&hpriv->stats, sizeof(hpriv->stats));
6074807330cSBjoern A. Zeeb 			break;
6084807330cSBjoern A. Zeeb 		    }
6091e031324SBjoern A. Zeeb 		case NGM_L2TP_SET_SEQ:
6101e031324SBjoern A. Zeeb 		    {
6111e031324SBjoern A. Zeeb 			struct ng_l2tp_seq_config *const conf =
6121e031324SBjoern A. Zeeb 				(struct ng_l2tp_seq_config *)msg->data;
6131e031324SBjoern A. Zeeb 
6141e031324SBjoern A. Zeeb 			/* Check for invalid or illegal seq config. */
6151e031324SBjoern A. Zeeb 			if (msg->header.arglen != sizeof(*conf)) {
6161e031324SBjoern A. Zeeb 				error = EINVAL;
6171e031324SBjoern A. Zeeb 				break;
6181e031324SBjoern A. Zeeb 			}
6191e031324SBjoern A. Zeeb 			conf->ns = htons(conf->ns);
6201e031324SBjoern A. Zeeb 			conf->nr = htons(conf->nr);
6211e031324SBjoern A. Zeeb 			conf->rack = htons(conf->rack);
6221e031324SBjoern A. Zeeb 			conf->xack = htons(conf->xack);
6231e031324SBjoern A. Zeeb 
6241e031324SBjoern A. Zeeb 			/* Set sequence numbers. */
6251e031324SBjoern A. Zeeb 			error = ng_l2tp_seq_set(priv, conf);
6261e031324SBjoern A. Zeeb 			break;
6271e031324SBjoern A. Zeeb 		    }
628901fadf7SArchie Cobbs 		default:
629901fadf7SArchie Cobbs 			error = EINVAL;
630901fadf7SArchie Cobbs 			break;
631901fadf7SArchie Cobbs 		}
632901fadf7SArchie Cobbs 		break;
633901fadf7SArchie Cobbs 	default:
634901fadf7SArchie Cobbs 		error = EINVAL;
635901fadf7SArchie Cobbs 		break;
636901fadf7SArchie Cobbs 	}
637901fadf7SArchie Cobbs 
638901fadf7SArchie Cobbs 	/* Done */
639901fadf7SArchie Cobbs 	NG_RESPOND_MSG(error, node, item, resp);
640901fadf7SArchie Cobbs 	NG_FREE_MSG(msg);
641901fadf7SArchie Cobbs 	return (error);
642901fadf7SArchie Cobbs }
643901fadf7SArchie Cobbs 
644901fadf7SArchie Cobbs /*
645901fadf7SArchie Cobbs  * Destroy node
646901fadf7SArchie Cobbs  */
647901fadf7SArchie Cobbs static int
648901fadf7SArchie Cobbs ng_l2tp_shutdown(node_p node)
649901fadf7SArchie Cobbs {
650901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
651901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
652901fadf7SArchie Cobbs 
653901fadf7SArchie Cobbs 	/* Sanity check */
654901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
655901fadf7SArchie Cobbs 
656901fadf7SArchie Cobbs 	/* Reset sequence number state */
657901fadf7SArchie Cobbs 	ng_l2tp_seq_reset(priv);
658901fadf7SArchie Cobbs 
659901fadf7SArchie Cobbs 	/* Free private data if neither timer is running */
6600ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->rack_timer, node);
6610ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->xack_timer, node);
6620ef8db8fSGleb Smirnoff 
663702f9895SAlexander Motin 	mtx_destroy(&seq->mtx);
664702f9895SAlexander Motin 
6651ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_L2TP);
666901fadf7SArchie Cobbs 
667901fadf7SArchie Cobbs 	/* Unref node */
668901fadf7SArchie Cobbs 	NG_NODE_UNREF(node);
669901fadf7SArchie Cobbs 	return (0);
670901fadf7SArchie Cobbs }
671901fadf7SArchie Cobbs 
672901fadf7SArchie Cobbs /*
673901fadf7SArchie Cobbs  * Hook disconnection
674901fadf7SArchie Cobbs  */
675901fadf7SArchie Cobbs static int
676901fadf7SArchie Cobbs ng_l2tp_disconnect(hook_p hook)
677901fadf7SArchie Cobbs {
678901fadf7SArchie Cobbs 	const node_p node = NG_HOOK_NODE(hook);
679901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
680901fadf7SArchie Cobbs 
681901fadf7SArchie Cobbs 	/* Zero out hook pointer */
682901fadf7SArchie Cobbs 	if (hook == priv->ctrl)
683901fadf7SArchie Cobbs 		priv->ctrl = NULL;
684901fadf7SArchie Cobbs 	else if (hook == priv->lower)
685901fadf7SArchie Cobbs 		priv->lower = NULL;
686901fadf7SArchie Cobbs 	else {
6874e759763SAlexander Motin 		const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
6884e759763SAlexander Motin 		LIST_REMOVE(hpriv, sessions);
6891ede983cSDag-Erling Smørgrav 		free(hpriv, M_NETGRAPH_L2TP);
690901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, NULL);
691901fadf7SArchie Cobbs 	}
692901fadf7SArchie Cobbs 
693901fadf7SArchie Cobbs 	/* Go away if no longer connected to anything */
694901fadf7SArchie Cobbs 	if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
695901fadf7SArchie Cobbs 		ng_rmnode_self(node);
696901fadf7SArchie Cobbs 	return (0);
697901fadf7SArchie Cobbs }
698901fadf7SArchie Cobbs 
699901fadf7SArchie Cobbs /*************************************************************************
700901fadf7SArchie Cobbs 			INTERNAL FUNCTIONS
701901fadf7SArchie Cobbs *************************************************************************/
702901fadf7SArchie Cobbs 
703901fadf7SArchie Cobbs /*
704280d6bd7SAlexander Motin  * Find the hook with a given session ID.
705901fadf7SArchie Cobbs  */
7064e759763SAlexander Motin static hookpriv_p
7074e759763SAlexander Motin ng_l2tp_find_session(priv_p privp, u_int16_t sid)
708901fadf7SArchie Cobbs {
7094e759763SAlexander Motin 	uint16_t	hash = SESSHASH(sid);
7104e759763SAlexander Motin 	hookpriv_p	hpriv = NULL;
711901fadf7SArchie Cobbs 
7124e759763SAlexander Motin 	LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
7134e759763SAlexander Motin 		if (hpriv->conf.session_id == sid)
7144e759763SAlexander Motin 			break;
7154e759763SAlexander Motin 	}
7164e759763SAlexander Motin 
7174e759763SAlexander Motin 	return (hpriv);
718901fadf7SArchie Cobbs }
719901fadf7SArchie Cobbs 
720901fadf7SArchie Cobbs /*
721901fadf7SArchie Cobbs  * Reset a hook's session state.
722901fadf7SArchie Cobbs  */
723901fadf7SArchie Cobbs static int
724901fadf7SArchie Cobbs ng_l2tp_reset_session(hook_p hook, void *arg)
725901fadf7SArchie Cobbs {
726901fadf7SArchie Cobbs 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
727901fadf7SArchie Cobbs 
728901fadf7SArchie Cobbs 	if (hpriv != NULL) {
729901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = 0;
730901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = 0;
731b0239937SAlexander Motin 		bzero(&hpriv->stats, sizeof(struct ng_l2tp_session_stats));
732901fadf7SArchie Cobbs 		hpriv->nr = 0;
733901fadf7SArchie Cobbs 		hpriv->ns = 0;
734901fadf7SArchie Cobbs 	}
735901fadf7SArchie Cobbs 	return (-1);
736901fadf7SArchie Cobbs }
737901fadf7SArchie Cobbs 
738901fadf7SArchie Cobbs /*
739901fadf7SArchie Cobbs  * Handle an incoming frame from below.
740901fadf7SArchie Cobbs  */
741901fadf7SArchie Cobbs static int
742bf741e4dSAlexander Motin ng_l2tp_rcvdata_lower(hook_p h, item_p item)
743901fadf7SArchie Cobbs {
744901fadf7SArchie Cobbs 	static const u_int16_t req_bits[2][2] = {
745901fadf7SArchie Cobbs 		{ L2TP_DATA_0BITS, L2TP_DATA_1BITS },
746901fadf7SArchie Cobbs 		{ L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
747901fadf7SArchie Cobbs 	};
748bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(h);
749901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
750901fadf7SArchie Cobbs 	hookpriv_p hpriv = NULL;
751901fadf7SArchie Cobbs 	hook_p hook = NULL;
752901fadf7SArchie Cobbs 	struct mbuf *m;
753280d6bd7SAlexander Motin 	u_int16_t tid, sid;
754901fadf7SArchie Cobbs 	u_int16_t hdr;
755280d6bd7SAlexander Motin 	u_int16_t ns, nr;
756901fadf7SArchie Cobbs 	int is_ctrl;
757901fadf7SArchie Cobbs 	int error;
7584807330cSBjoern A. Zeeb 	int len, plen;
759901fadf7SArchie Cobbs 
760bf741e4dSAlexander Motin 	/* Sanity check */
761bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
762bf741e4dSAlexander Motin 
763bf741e4dSAlexander Motin 	/* If not configured, reject */
764bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
765bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
766bf741e4dSAlexander Motin 		ERROUT(ENXIO);
767bf741e4dSAlexander Motin 	}
768bf741e4dSAlexander Motin 
769901fadf7SArchie Cobbs 	/* Grab mbuf */
770901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
771901fadf7SArchie Cobbs 
7724807330cSBjoern A. Zeeb 	/* Remember full packet length; needed for per session accounting. */
7734807330cSBjoern A. Zeeb 	plen = m->m_pkthdr.len;
7744807330cSBjoern A. Zeeb 
775901fadf7SArchie Cobbs 	/* Update stats */
776901fadf7SArchie Cobbs 	priv->stats.recvPackets++;
7774807330cSBjoern A. Zeeb 	priv->stats.recvOctets += plen;
778901fadf7SArchie Cobbs 
779901fadf7SArchie Cobbs 	/* Get initial header */
780901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 6) {
781901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
782901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
783901fadf7SArchie Cobbs 		NG_FREE_M(m);
784bf741e4dSAlexander Motin 		ERROUT(EINVAL);
785901fadf7SArchie Cobbs 	}
786901fadf7SArchie Cobbs 	if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
787901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
788901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
789bf741e4dSAlexander Motin 		ERROUT(EINVAL);
790901fadf7SArchie Cobbs 	}
791148ac1daSAlexander Motin 	hdr = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1];
792901fadf7SArchie Cobbs 	m_adj(m, 2);
793901fadf7SArchie Cobbs 
794901fadf7SArchie Cobbs 	/* Check required header bits and minimum length */
795901fadf7SArchie Cobbs 	is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
796901fadf7SArchie Cobbs 	if ((hdr & req_bits[is_ctrl][0]) != 0
797901fadf7SArchie Cobbs 	    || (~hdr & req_bits[is_ctrl][1]) != 0) {
798901fadf7SArchie Cobbs 		priv->stats.recvInvalid++;
799901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
800901fadf7SArchie Cobbs 		NG_FREE_M(m);
801bf741e4dSAlexander Motin 		ERROUT(EINVAL);
802901fadf7SArchie Cobbs 	}
803901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 4				/* tunnel, session id */
804901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_LEN) != 0))		/* length field */
805901fadf7SArchie Cobbs 	    + (4 * ((hdr & L2TP_HDR_SEQ) != 0))		/* seq # fields */
806901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_OFF) != 0))) {	/* offset field */
807901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
808901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
809901fadf7SArchie Cobbs 		NG_FREE_M(m);
810bf741e4dSAlexander Motin 		ERROUT(EINVAL);
811901fadf7SArchie Cobbs 	}
812901fadf7SArchie Cobbs 
813901fadf7SArchie Cobbs 	/* Get and validate length field if present */
814901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_LEN) != 0) {
815901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
816901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
817901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
818bf741e4dSAlexander Motin 			ERROUT(EINVAL);
819901fadf7SArchie Cobbs 		}
820148ac1daSAlexander Motin 		len = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1] - 4;
821901fadf7SArchie Cobbs 		m_adj(m, 2);
822901fadf7SArchie Cobbs 		if (len < 0 || len > m->m_pkthdr.len) {
823901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
824901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
825901fadf7SArchie Cobbs 			NG_FREE_M(m);
826bf741e4dSAlexander Motin 			ERROUT(EINVAL);
827901fadf7SArchie Cobbs 		}
828901fadf7SArchie Cobbs 		if (len < m->m_pkthdr.len)		/* trim extra bytes */
829901fadf7SArchie Cobbs 			m_adj(m, -(m->m_pkthdr.len - len));
830901fadf7SArchie Cobbs 	}
831901fadf7SArchie Cobbs 
832901fadf7SArchie Cobbs 	/* Get tunnel ID and session ID */
833901fadf7SArchie Cobbs 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
834901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
835901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
836bf741e4dSAlexander Motin 		ERROUT(EINVAL);
837901fadf7SArchie Cobbs 	}
838280d6bd7SAlexander Motin 	tid = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
839280d6bd7SAlexander Motin 	sid = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
840901fadf7SArchie Cobbs 	m_adj(m, 4);
841901fadf7SArchie Cobbs 
842901fadf7SArchie Cobbs 	/* Check tunnel ID */
843280d6bd7SAlexander Motin 	if (tid != priv->conf.tunnel_id &&
844280d6bd7SAlexander Motin 	    (priv->conf.match_id || tid != 0)) {
845901fadf7SArchie Cobbs 		priv->stats.recvWrongTunnel++;
846901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
847901fadf7SArchie Cobbs 		NG_FREE_M(m);
848bf741e4dSAlexander Motin 		ERROUT(EADDRNOTAVAIL);
849901fadf7SArchie Cobbs 	}
850901fadf7SArchie Cobbs 
851901fadf7SArchie Cobbs 	/* Check session ID (for data packets only) */
852901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) == 0) {
853280d6bd7SAlexander Motin 		hpriv = ng_l2tp_find_session(priv, sid);
8544e759763SAlexander Motin 		if (hpriv == NULL) {
855901fadf7SArchie Cobbs 			priv->stats.recvUnknownSID++;
856901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
857901fadf7SArchie Cobbs 			NG_FREE_M(m);
858bf741e4dSAlexander Motin 			ERROUT(ENOTCONN);
859901fadf7SArchie Cobbs 		}
8604e759763SAlexander Motin 		hook = hpriv->hook;
861901fadf7SArchie Cobbs 	}
862901fadf7SArchie Cobbs 
863901fadf7SArchie Cobbs 	/* Get Ns, Nr fields if present */
864901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
865901fadf7SArchie Cobbs 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
866901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
867901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
868bf741e4dSAlexander Motin 			ERROUT(EINVAL);
869901fadf7SArchie Cobbs 		}
870280d6bd7SAlexander Motin 		ns = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
871280d6bd7SAlexander Motin 		nr = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
872901fadf7SArchie Cobbs 		m_adj(m, 4);
873280d6bd7SAlexander Motin 	} else
874280d6bd7SAlexander Motin 		ns = nr = 0;
875901fadf7SArchie Cobbs 
876901fadf7SArchie Cobbs 	/* Strip offset padding if present */
877901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_OFF) != 0) {
878901fadf7SArchie Cobbs 		u_int16_t offset;
879901fadf7SArchie Cobbs 
880901fadf7SArchie Cobbs 		/* Get length of offset padding */
881901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
882901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
883901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
884bf741e4dSAlexander Motin 			ERROUT(EINVAL);
885901fadf7SArchie Cobbs 		}
886280d6bd7SAlexander Motin 		offset = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
887901fadf7SArchie Cobbs 
888901fadf7SArchie Cobbs 		/* Trim offset padding */
889ddb72294SBjoern A. Zeeb 		if ((2+offset) > m->m_pkthdr.len) {
890901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
891901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
892901fadf7SArchie Cobbs 			NG_FREE_M(m);
893bf741e4dSAlexander Motin 			ERROUT(EINVAL);
894901fadf7SArchie Cobbs 		}
895ddb72294SBjoern A. Zeeb 		m_adj(m, 2+offset);
896901fadf7SArchie Cobbs 	}
897901fadf7SArchie Cobbs 
898901fadf7SArchie Cobbs 	/* Handle control packets */
899901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) != 0) {
900af63939cSAlexander Motin 		struct l2tp_seq *const seq = &priv->seq;
901901fadf7SArchie Cobbs 
902901fadf7SArchie Cobbs 		/* Handle receive ack sequence number Nr */
903901fadf7SArchie Cobbs 		ng_l2tp_seq_recv_nr(priv, nr);
904901fadf7SArchie Cobbs 
905901fadf7SArchie Cobbs 		/* Discard ZLB packets */
906901fadf7SArchie Cobbs 		if (m->m_pkthdr.len == 0) {
907901fadf7SArchie Cobbs 			priv->stats.recvZLBs++;
908901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
909901fadf7SArchie Cobbs 			NG_FREE_M(m);
910bf741e4dSAlexander Motin 			ERROUT(0);
911901fadf7SArchie Cobbs 		}
912901fadf7SArchie Cobbs 
913af63939cSAlexander Motin 		mtx_lock(&seq->mtx);
914901fadf7SArchie Cobbs 		/*
915af63939cSAlexander Motin 		 * If not what we expect or we are busy, drop packet and
916af63939cSAlexander Motin 		 * send an immediate ZLB ack.
917901fadf7SArchie Cobbs 		 */
918af63939cSAlexander Motin 		if (ns != seq->nr || seq->inproc) {
919af63939cSAlexander Motin 			if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
920af63939cSAlexander Motin 				priv->stats.recvDuplicates++;
921af63939cSAlexander Motin 			else
922af63939cSAlexander Motin 				priv->stats.recvOutOfOrder++;
923af63939cSAlexander Motin 			mtx_unlock(&seq->mtx);
924af63939cSAlexander Motin 			ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
925af63939cSAlexander Motin 			NG_FREE_ITEM(item);
926af63939cSAlexander Motin 			NG_FREE_M(m);
927af63939cSAlexander Motin 			ERROUT(0);
928af63939cSAlexander Motin 		}
929af63939cSAlexander Motin 		/*
930af63939cSAlexander Motin 		 * Until we deliver this packet we can't receive next one as
931af63939cSAlexander Motin 		 * we have no information for sending ack.
932af63939cSAlexander Motin 		 */
933af63939cSAlexander Motin 		seq->inproc = 1;
934af63939cSAlexander Motin 		mtx_unlock(&seq->mtx);
935af63939cSAlexander Motin 
936af63939cSAlexander Motin 		/* Prepend session ID to packet. */
937eb1b1807SGleb Smirnoff 		M_PREPEND(m, 2, M_NOWAIT);
938901fadf7SArchie Cobbs 		if (m == NULL) {
939395adfbeSAlexander Motin 			seq->inproc = 0;
940901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
941901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
942bf741e4dSAlexander Motin 			ERROUT(ENOBUFS);
943901fadf7SArchie Cobbs 		}
944280d6bd7SAlexander Motin 		mtod(m, u_int8_t *)[0] = sid >> 8;
945280d6bd7SAlexander Motin 		mtod(m, u_int8_t *)[1] = sid & 0xff;
946901fadf7SArchie Cobbs 
947901fadf7SArchie Cobbs 		/* Deliver packet to upper layers */
948901fadf7SArchie Cobbs 		NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
949af63939cSAlexander Motin 
950af63939cSAlexander Motin 		mtx_lock(&seq->mtx);
951af63939cSAlexander Motin 		/* Ready to process next packet. */
952af63939cSAlexander Motin 		seq->inproc = 0;
953af63939cSAlexander Motin 
954af63939cSAlexander Motin 		/* If packet was successfully delivered send ack. */
955af63939cSAlexander Motin 		if (error == 0) {
956af63939cSAlexander Motin 			/* Update recv sequence number */
957af63939cSAlexander Motin 			seq->nr++;
958af63939cSAlexander Motin 			/* Start receive ack timer, if not already running */
959af63939cSAlexander Motin 			if (!callout_active(&seq->xack_timer)) {
960af63939cSAlexander Motin 				ng_callout(&seq->xack_timer, priv->node, NULL,
961af63939cSAlexander Motin 				    L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
962af63939cSAlexander Motin 				    NULL, 0);
963af63939cSAlexander Motin 			}
964af63939cSAlexander Motin 		}
965af63939cSAlexander Motin 		mtx_unlock(&seq->mtx);
966af63939cSAlexander Motin 
967bf741e4dSAlexander Motin 		ERROUT(error);
968901fadf7SArchie Cobbs 	}
969901fadf7SArchie Cobbs 
9704807330cSBjoern A. Zeeb 	/* Per session packet, account it. */
9714807330cSBjoern A. Zeeb 	hpriv->stats.recvPackets++;
9724807330cSBjoern A. Zeeb 	hpriv->stats.recvOctets += plen;
9734807330cSBjoern A. Zeeb 
974901fadf7SArchie Cobbs 	/* Follow peer's lead in data sequencing, if configured to do so */
975901fadf7SArchie Cobbs 	if (!hpriv->conf.control_dseq)
976901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
977901fadf7SArchie Cobbs 
978901fadf7SArchie Cobbs 	/* Handle data sequence numbers if present and enabled */
979901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
980901fadf7SArchie Cobbs 		if (hpriv->conf.enable_dseq
981901fadf7SArchie Cobbs 		    && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
982901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);	/* duplicate or out of order */
983901fadf7SArchie Cobbs 			NG_FREE_M(m);
984901fadf7SArchie Cobbs 			priv->stats.recvDataDrops++;
985bf741e4dSAlexander Motin 			ERROUT(0);
986901fadf7SArchie Cobbs 		}
987901fadf7SArchie Cobbs 		hpriv->nr = ns + 1;
988901fadf7SArchie Cobbs 	}
989901fadf7SArchie Cobbs 
990901fadf7SArchie Cobbs 	/* Drop empty data packets */
991901fadf7SArchie Cobbs 	if (m->m_pkthdr.len == 0) {
992901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
993901fadf7SArchie Cobbs 		NG_FREE_M(m);
994bf741e4dSAlexander Motin 		ERROUT(0);
995901fadf7SArchie Cobbs 	}
996901fadf7SArchie Cobbs 
997901fadf7SArchie Cobbs 	/* Deliver data */
998901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, hook, m);
999bf741e4dSAlexander Motin done:
1000bf741e4dSAlexander Motin 	/* Done */
1001bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1002901fadf7SArchie Cobbs 	return (error);
1003901fadf7SArchie Cobbs }
1004901fadf7SArchie Cobbs 
1005901fadf7SArchie Cobbs /*
1006901fadf7SArchie Cobbs  * Handle an outgoing control frame.
1007901fadf7SArchie Cobbs  */
1008901fadf7SArchie Cobbs static int
1009bf741e4dSAlexander Motin ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1010901fadf7SArchie Cobbs {
1011bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(hook);
1012901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1013901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1014901fadf7SArchie Cobbs 	struct mbuf *m;
1015bf741e4dSAlexander Motin 	int error;
1016901fadf7SArchie Cobbs 	int i;
1017702f9895SAlexander Motin 	u_int16_t	ns;
1018901fadf7SArchie Cobbs 
1019bf741e4dSAlexander Motin 	/* Sanity check */
1020bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1021bf741e4dSAlexander Motin 
1022bf741e4dSAlexander Motin 	/* If not configured, reject */
1023bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1024bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1025bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1026bf741e4dSAlexander Motin 	}
1027bf741e4dSAlexander Motin 
1028901fadf7SArchie Cobbs 	/* Grab mbuf and discard other stuff XXX */
1029901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1030901fadf7SArchie Cobbs 	NG_FREE_ITEM(item);
1031901fadf7SArchie Cobbs 
1032901fadf7SArchie Cobbs 	/* Packet should have session ID prepended */
1033901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 2) {
1034901fadf7SArchie Cobbs 		priv->stats.xmitInvalid++;
1035901fadf7SArchie Cobbs 		m_freem(m);
1036bf741e4dSAlexander Motin 		ERROUT(EINVAL);
1037901fadf7SArchie Cobbs 	}
1038901fadf7SArchie Cobbs 
1039901fadf7SArchie Cobbs 	/* Check max length */
1040901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 14) {
1041901fadf7SArchie Cobbs 		priv->stats.xmitTooBig++;
1042901fadf7SArchie Cobbs 		m_freem(m);
1043bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1044901fadf7SArchie Cobbs 	}
1045901fadf7SArchie Cobbs 
1046702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1047702f9895SAlexander Motin 
1048901fadf7SArchie Cobbs 	/* Find next empty slot in transmit queue */
1049901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1050901fadf7SArchie Cobbs 	if (i == L2TP_MAX_XWIN) {
1051702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1052901fadf7SArchie Cobbs 		priv->stats.xmitDrops++;
1053901fadf7SArchie Cobbs 		m_freem(m);
1054bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1055901fadf7SArchie Cobbs 	}
1056901fadf7SArchie Cobbs 	seq->xwin[i] = m;
1057901fadf7SArchie Cobbs 
1058901fadf7SArchie Cobbs 	/* If peer's receive window is already full, nothing else to do */
1059702f9895SAlexander Motin 	if (i >= seq->cwnd) {
1060702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1061bf741e4dSAlexander Motin 		ERROUT(0);
1062702f9895SAlexander Motin 	}
1063901fadf7SArchie Cobbs 
1064901fadf7SArchie Cobbs 	/* Start retransmit timer if not already running */
1065206fa244SAlexander Motin 	if (!callout_active(&seq->rack_timer))
10660ef8db8fSGleb Smirnoff 		ng_callout(&seq->rack_timer, node, NULL,
10670ef8db8fSGleb Smirnoff 		    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1068901fadf7SArchie Cobbs 
1069702f9895SAlexander Motin 	ns = seq->ns++;
1070702f9895SAlexander Motin 
1071702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1072702f9895SAlexander Motin 
1073901fadf7SArchie Cobbs 	/* Copy packet */
1074eb1b1807SGleb Smirnoff 	if ((m = L2TP_COPY_MBUF(m, M_NOWAIT)) == NULL) {
1075901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1076bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1077901fadf7SArchie Cobbs 	}
1078901fadf7SArchie Cobbs 
1079901fadf7SArchie Cobbs 	/* Send packet and increment xmit sequence number */
1080702f9895SAlexander Motin 	error = ng_l2tp_xmit_ctrl(priv, m, ns);
1081bf741e4dSAlexander Motin done:
1082bf741e4dSAlexander Motin 	/* Done */
1083bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1084bf741e4dSAlexander Motin 	return (error);
1085901fadf7SArchie Cobbs }
1086901fadf7SArchie Cobbs 
1087901fadf7SArchie Cobbs /*
1088901fadf7SArchie Cobbs  * Handle an outgoing data frame.
1089901fadf7SArchie Cobbs  */
1090901fadf7SArchie Cobbs static int
1091bf741e4dSAlexander Motin ng_l2tp_rcvdata(hook_p hook, item_p item)
1092901fadf7SArchie Cobbs {
1093bf741e4dSAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1094bf741e4dSAlexander Motin 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1095901fadf7SArchie Cobbs 	struct mbuf *m;
1096148ac1daSAlexander Motin 	uint8_t *p;
1097901fadf7SArchie Cobbs 	u_int16_t hdr;
1098901fadf7SArchie Cobbs 	int error;
1099148ac1daSAlexander Motin 	int i = 2;
1100901fadf7SArchie Cobbs 
1101bf741e4dSAlexander Motin 	/* Sanity check */
1102bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1103bf741e4dSAlexander Motin 
1104bf741e4dSAlexander Motin 	/* If not configured, reject */
1105bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1106bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1107bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1108bf741e4dSAlexander Motin 	}
1109bf741e4dSAlexander Motin 
1110901fadf7SArchie Cobbs 	/* Get mbuf */
1111901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1112901fadf7SArchie Cobbs 
1113901fadf7SArchie Cobbs 	/* Check max length */
1114901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 12) {
1115901fadf7SArchie Cobbs 		priv->stats.xmitDataTooBig++;
1116901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1117901fadf7SArchie Cobbs 		NG_FREE_M(m);
1118bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1119901fadf7SArchie Cobbs 	}
1120901fadf7SArchie Cobbs 
1121901fadf7SArchie Cobbs 	/* Prepend L2TP header */
1122901fadf7SArchie Cobbs 	M_PREPEND(m, 6
1123901fadf7SArchie Cobbs 	    + (2 * (hpriv->conf.include_length != 0))
1124901fadf7SArchie Cobbs 	    + (4 * (hpriv->conf.enable_dseq != 0)),
1125eb1b1807SGleb Smirnoff 	    M_NOWAIT);
1126901fadf7SArchie Cobbs 	if (m == NULL) {
1127901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1128901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1129bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1130901fadf7SArchie Cobbs 	}
1131148ac1daSAlexander Motin 	p = mtod(m, uint8_t *);
1132901fadf7SArchie Cobbs 	hdr = L2TP_DATA_HDR;
1133901fadf7SArchie Cobbs 	if (hpriv->conf.include_length) {
1134901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_LEN;
1135148ac1daSAlexander Motin 		p[i++] = m->m_pkthdr.len >> 8;
1136148ac1daSAlexander Motin 		p[i++] = m->m_pkthdr.len & 0xff;
1137901fadf7SArchie Cobbs 	}
1138148ac1daSAlexander Motin 	p[i++] = priv->conf.peer_id >> 8;
1139148ac1daSAlexander Motin 	p[i++] = priv->conf.peer_id & 0xff;
1140148ac1daSAlexander Motin 	p[i++] = hpriv->conf.peer_id >> 8;
1141148ac1daSAlexander Motin 	p[i++] = hpriv->conf.peer_id & 0xff;
1142901fadf7SArchie Cobbs 	if (hpriv->conf.enable_dseq) {
1143901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_SEQ;
1144148ac1daSAlexander Motin 		p[i++] = hpriv->ns >> 8;
1145148ac1daSAlexander Motin 		p[i++] = hpriv->ns & 0xff;
1146148ac1daSAlexander Motin 		p[i++] = hpriv->nr >> 8;
1147148ac1daSAlexander Motin 		p[i++] = hpriv->nr & 0xff;
1148901fadf7SArchie Cobbs 		hpriv->ns++;
1149901fadf7SArchie Cobbs 	}
1150148ac1daSAlexander Motin 	p[0] = hdr >> 8;
1151148ac1daSAlexander Motin 	p[1] = hdr & 0xff;
1152901fadf7SArchie Cobbs 
11534807330cSBjoern A. Zeeb 	/* Update per session stats. */
11544807330cSBjoern A. Zeeb 	hpriv->stats.xmitPackets++;
11554807330cSBjoern A. Zeeb 	hpriv->stats.xmitOctets += m->m_pkthdr.len;
11564807330cSBjoern A. Zeeb 
115734d16c64SAlexander Motin 	/* And the global one. */
115834d16c64SAlexander Motin 	priv->stats.xmitPackets++;
115934d16c64SAlexander Motin 	priv->stats.xmitOctets += m->m_pkthdr.len;
116034d16c64SAlexander Motin 
1161901fadf7SArchie Cobbs 	/* Send packet */
1162901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, priv->lower, m);
1163bf741e4dSAlexander Motin done:
1164bf741e4dSAlexander Motin 	/* Done */
1165bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1166901fadf7SArchie Cobbs 	return (error);
1167901fadf7SArchie Cobbs }
1168901fadf7SArchie Cobbs 
1169901fadf7SArchie Cobbs /*
1170901fadf7SArchie Cobbs  * Send a message to our controlling node that we've failed.
1171901fadf7SArchie Cobbs  */
1172901fadf7SArchie Cobbs static void
1173901fadf7SArchie Cobbs ng_l2tp_seq_failure(priv_p priv)
1174901fadf7SArchie Cobbs {
1175901fadf7SArchie Cobbs 	struct ng_mesg *msg;
1176901fadf7SArchie Cobbs 	int error;
1177901fadf7SArchie Cobbs 
1178901fadf7SArchie Cobbs 	NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1179901fadf7SArchie Cobbs 	if (msg == NULL)
1180901fadf7SArchie Cobbs 		return;
1181facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1182901fadf7SArchie Cobbs }
1183901fadf7SArchie Cobbs 
1184901fadf7SArchie Cobbs /************************************************************************
1185901fadf7SArchie Cobbs 			SEQUENCE NUMBER HANDLING
1186901fadf7SArchie Cobbs ************************************************************************/
1187901fadf7SArchie Cobbs 
1188901fadf7SArchie Cobbs /*
1189901fadf7SArchie Cobbs  * Initialize sequence number state.
1190901fadf7SArchie Cobbs  */
1191901fadf7SArchie Cobbs static void
1192901fadf7SArchie Cobbs ng_l2tp_seq_init(priv_p priv)
1193901fadf7SArchie Cobbs {
1194901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1195901fadf7SArchie Cobbs 
1196901fadf7SArchie Cobbs 	KASSERT(priv->conf.peer_win >= 1,
11977e2b43eeSDavid E. O'Brien 	    ("%s: peer_win is zero", __func__));
1198901fadf7SArchie Cobbs 	memset(seq, 0, sizeof(*seq));
1199901fadf7SArchie Cobbs 	seq->cwnd = 1;
1200901fadf7SArchie Cobbs 	seq->wmax = priv->conf.peer_win;
1201901fadf7SArchie Cobbs 	if (seq->wmax > L2TP_MAX_XWIN)
1202901fadf7SArchie Cobbs 		seq->wmax = L2TP_MAX_XWIN;
1203901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
12040ef8db8fSGleb Smirnoff 	ng_callout_init(&seq->rack_timer);
12050ef8db8fSGleb Smirnoff 	ng_callout_init(&seq->xack_timer);
1206702f9895SAlexander Motin 	mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1207901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1208901fadf7SArchie Cobbs }
1209901fadf7SArchie Cobbs 
1210901fadf7SArchie Cobbs /*
12111e031324SBjoern A. Zeeb  * Set sequence number state as given from user.
12121e031324SBjoern A. Zeeb  */
12131e031324SBjoern A. Zeeb static int
12141e031324SBjoern A. Zeeb ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
12151e031324SBjoern A. Zeeb {
12161e031324SBjoern A. Zeeb 	struct l2tp_seq *const seq = &priv->seq;
12171e031324SBjoern A. Zeeb 
12181e031324SBjoern A. Zeeb 	/* If node is enabled, deny update to sequence numbers. */
12191e031324SBjoern A. Zeeb 	if (priv->conf.enabled)
12201e031324SBjoern A. Zeeb 		return (EBUSY);
12211e031324SBjoern A. Zeeb 
12221e031324SBjoern A. Zeeb 	/* We only can handle the simple cases. */
12231e031324SBjoern A. Zeeb 	if (conf->xack != conf->nr || conf->ns != conf->rack)
12241e031324SBjoern A. Zeeb 		return (EINVAL);
12251e031324SBjoern A. Zeeb 
12261e031324SBjoern A. Zeeb 	/* Set ns,nr,rack,xack parameters. */
12271e031324SBjoern A. Zeeb 	seq->ns = conf->ns;
12281e031324SBjoern A. Zeeb 	seq->nr = conf->nr;
12291e031324SBjoern A. Zeeb 	seq->rack = conf->rack;
12301e031324SBjoern A. Zeeb 	seq->xack = conf->xack;
12311e031324SBjoern A. Zeeb 
12321e031324SBjoern A. Zeeb 	return (0);
12331e031324SBjoern A. Zeeb }
12341e031324SBjoern A. Zeeb 
12351e031324SBjoern A. Zeeb /*
1236901fadf7SArchie Cobbs  * Adjust sequence number state accordingly after reconfiguration.
1237901fadf7SArchie Cobbs  */
1238901fadf7SArchie Cobbs static int
1239901fadf7SArchie Cobbs ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1240901fadf7SArchie Cobbs {
1241901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1242901fadf7SArchie Cobbs 	u_int16_t new_wmax;
1243901fadf7SArchie Cobbs 
1244901fadf7SArchie Cobbs 	/* If disabling node, reset state sequence number */
1245901fadf7SArchie Cobbs 	if (!conf->enabled) {
1246901fadf7SArchie Cobbs 		ng_l2tp_seq_reset(priv);
1247901fadf7SArchie Cobbs 		return (0);
1248901fadf7SArchie Cobbs 	}
1249901fadf7SArchie Cobbs 
1250901fadf7SArchie Cobbs 	/* Adjust peer's max recv window; it can only increase */
1251901fadf7SArchie Cobbs 	new_wmax = conf->peer_win;
1252901fadf7SArchie Cobbs 	if (new_wmax > L2TP_MAX_XWIN)
1253901fadf7SArchie Cobbs 		new_wmax = L2TP_MAX_XWIN;
1254901fadf7SArchie Cobbs 	if (new_wmax == 0)
1255901fadf7SArchie Cobbs 		return (EINVAL);
1256901fadf7SArchie Cobbs 	if (new_wmax < seq->wmax)
1257901fadf7SArchie Cobbs 		return (EBUSY);
1258901fadf7SArchie Cobbs 	seq->wmax = new_wmax;
1259901fadf7SArchie Cobbs 
1260901fadf7SArchie Cobbs 	/* Done */
1261901fadf7SArchie Cobbs 	return (0);
1262901fadf7SArchie Cobbs }
1263901fadf7SArchie Cobbs 
1264901fadf7SArchie Cobbs /*
1265901fadf7SArchie Cobbs  * Reset sequence number state.
1266901fadf7SArchie Cobbs  */
1267901fadf7SArchie Cobbs static void
1268901fadf7SArchie Cobbs ng_l2tp_seq_reset(priv_p priv)
1269901fadf7SArchie Cobbs {
1270901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1271901fadf7SArchie Cobbs 	hook_p hook;
1272901fadf7SArchie Cobbs 	int i;
1273901fadf7SArchie Cobbs 
1274901fadf7SArchie Cobbs 	/* Sanity check */
1275901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1276901fadf7SArchie Cobbs 
1277901fadf7SArchie Cobbs 	/* Stop timers */
12780ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->rack_timer, priv->node);
12790ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->xack_timer, priv->node);
1280901fadf7SArchie Cobbs 
1281901fadf7SArchie Cobbs 	/* Free retransmit queue */
1282901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN; i++) {
1283901fadf7SArchie Cobbs 		if (seq->xwin[i] == NULL)
1284901fadf7SArchie Cobbs 			break;
1285901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1286901fadf7SArchie Cobbs 	}
1287901fadf7SArchie Cobbs 
1288901fadf7SArchie Cobbs 	/* Reset session hooks' sequence number states */
1289901fadf7SArchie Cobbs 	NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1290901fadf7SArchie Cobbs 
1291901fadf7SArchie Cobbs 	/* Reset node's sequence number state */
1292702f9895SAlexander Motin 	seq->ns = 0;
1293702f9895SAlexander Motin 	seq->nr = 0;
1294702f9895SAlexander Motin 	seq->rack = 0;
1295702f9895SAlexander Motin 	seq->xack = 0;
1296901fadf7SArchie Cobbs 	seq->wmax = L2TP_MAX_XWIN;
1297702f9895SAlexander Motin 	seq->cwnd = 1;
1298901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
1299702f9895SAlexander Motin 	seq->acks = 0;
1300702f9895SAlexander Motin 	seq->rexmits = 0;
1301702f9895SAlexander Motin 	bzero(seq->xwin, sizeof(seq->xwin));
1302901fadf7SArchie Cobbs 
1303901fadf7SArchie Cobbs 	/* Done */
1304901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1305901fadf7SArchie Cobbs }
1306901fadf7SArchie Cobbs 
1307901fadf7SArchie Cobbs /*
1308901fadf7SArchie Cobbs  * Handle receipt of an acknowledgement value (Nr) from peer.
1309901fadf7SArchie Cobbs  */
1310901fadf7SArchie Cobbs static void
1311901fadf7SArchie Cobbs ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1312901fadf7SArchie Cobbs {
1313901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1314702f9895SAlexander Motin 	struct mbuf	*xwin[L2TP_MAX_XWIN];	/* partial local copy */
1315901fadf7SArchie Cobbs 	int		nack;
1316702f9895SAlexander Motin 	int		i, j;
1317702f9895SAlexander Motin 	uint16_t	ns;
1318702f9895SAlexander Motin 
1319702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1320901fadf7SArchie Cobbs 
1321901fadf7SArchie Cobbs 	/* Verify peer's ACK is in range */
1322702f9895SAlexander Motin 	if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
1323702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1324901fadf7SArchie Cobbs 		return;				/* duplicate ack */
1325702f9895SAlexander Motin 	}
1326901fadf7SArchie Cobbs 	if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1327702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1328901fadf7SArchie Cobbs 		priv->stats.recvBadAcks++;	/* ack for packet not sent */
1329901fadf7SArchie Cobbs 		return;
1330901fadf7SArchie Cobbs 	}
1331901fadf7SArchie Cobbs 	KASSERT(nack <= L2TP_MAX_XWIN,
13327e2b43eeSDavid E. O'Brien 	    ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1333901fadf7SArchie Cobbs 
1334901fadf7SArchie Cobbs 	/* Update receive ack stats */
1335901fadf7SArchie Cobbs 	seq->rack = nr;
1336901fadf7SArchie Cobbs 	seq->rexmits = 0;
1337901fadf7SArchie Cobbs 
1338901fadf7SArchie Cobbs 	/* Free acknowledged packets and shift up packets in the xmit queue */
1339901fadf7SArchie Cobbs 	for (i = 0; i < nack; i++)
1340901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1341901fadf7SArchie Cobbs 	memmove(seq->xwin, seq->xwin + nack,
1342901fadf7SArchie Cobbs 	    (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1343901fadf7SArchie Cobbs 	memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1344901fadf7SArchie Cobbs 	    nack * sizeof(*seq->xwin));
1345901fadf7SArchie Cobbs 
1346901fadf7SArchie Cobbs 	/*
1347901fadf7SArchie Cobbs 	 * Do slow-start/congestion avoidance windowing algorithm described
1348901fadf7SArchie Cobbs 	 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1349901fadf7SArchie Cobbs 	 * ACK had arrived separately.
1350901fadf7SArchie Cobbs 	 */
1351901fadf7SArchie Cobbs 	if (seq->cwnd < seq->wmax) {
1352901fadf7SArchie Cobbs 
1353901fadf7SArchie Cobbs 		/* Handle slow start phase */
1354901fadf7SArchie Cobbs 		if (seq->cwnd < seq->ssth) {
1355901fadf7SArchie Cobbs 			seq->cwnd += nack;
1356901fadf7SArchie Cobbs 			nack = 0;
1357901fadf7SArchie Cobbs 			if (seq->cwnd > seq->ssth) {	/* into cg.av. phase */
1358901fadf7SArchie Cobbs 				nack = seq->cwnd - seq->ssth;
1359901fadf7SArchie Cobbs 				seq->cwnd = seq->ssth;
1360901fadf7SArchie Cobbs 			}
1361901fadf7SArchie Cobbs 		}
1362901fadf7SArchie Cobbs 
1363901fadf7SArchie Cobbs 		/* Handle congestion avoidance phase */
1364901fadf7SArchie Cobbs 		if (seq->cwnd >= seq->ssth) {
1365901fadf7SArchie Cobbs 			seq->acks += nack;
1366901fadf7SArchie Cobbs 			while (seq->acks >= seq->cwnd) {
1367901fadf7SArchie Cobbs 				seq->acks -= seq->cwnd;
1368901fadf7SArchie Cobbs 				if (seq->cwnd < seq->wmax)
1369901fadf7SArchie Cobbs 					seq->cwnd++;
1370901fadf7SArchie Cobbs 			}
1371901fadf7SArchie Cobbs 		}
1372901fadf7SArchie Cobbs 	}
1373901fadf7SArchie Cobbs 
1374901fadf7SArchie Cobbs 	/* Stop xmit timer */
1375206fa244SAlexander Motin 	if (callout_active(&seq->rack_timer))
13760ef8db8fSGleb Smirnoff 		ng_uncallout(&seq->rack_timer, priv->node);
1377901fadf7SArchie Cobbs 
1378901fadf7SArchie Cobbs 	/* If transmit queue is empty, we're done for now */
1379702f9895SAlexander Motin 	if (seq->xwin[0] == NULL) {
1380702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1381901fadf7SArchie Cobbs 		return;
1382702f9895SAlexander Motin 	}
1383901fadf7SArchie Cobbs 
1384901fadf7SArchie Cobbs 	/* Start restransmit timer again */
13850ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, priv->node, NULL,
13860ef8db8fSGleb Smirnoff 	    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1387901fadf7SArchie Cobbs 
1388901fadf7SArchie Cobbs 	/*
1389901fadf7SArchie Cobbs 	 * Send more packets, trying to keep peer's receive window full.
1390702f9895SAlexander Motin 	 * Make copy of everything we need before lock release.
1391702f9895SAlexander Motin 	 */
1392702f9895SAlexander Motin 	ns = seq->ns;
1393702f9895SAlexander Motin 	j = 0;
1394702f9895SAlexander Motin 	while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1395702f9895SAlexander Motin 	    && seq->xwin[i] != NULL) {
1396702f9895SAlexander Motin 		xwin[j++] = seq->xwin[i];
1397702f9895SAlexander Motin 		seq->ns++;
1398702f9895SAlexander Motin 	}
1399702f9895SAlexander Motin 
1400702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1401702f9895SAlexander Motin 
1402702f9895SAlexander Motin 	/*
1403702f9895SAlexander Motin 	 * Send prepared.
1404901fadf7SArchie Cobbs 	 * If there is a memory error, pretend packet was sent, as it
1405901fadf7SArchie Cobbs 	 * will get retransmitted later anyway.
1406901fadf7SArchie Cobbs 	 */
1407702f9895SAlexander Motin 	for (i = 0; i < j; i++) {
1408702f9895SAlexander Motin 		struct mbuf 	*m;
1409eb1b1807SGleb Smirnoff 		if ((m = L2TP_COPY_MBUF(xwin[i], M_NOWAIT)) == NULL)
1410901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1411901fadf7SArchie Cobbs 		else
1412702f9895SAlexander Motin 			ng_l2tp_xmit_ctrl(priv, m, ns);
1413702f9895SAlexander Motin 		ns++;
1414901fadf7SArchie Cobbs 	}
1415901fadf7SArchie Cobbs }
1416901fadf7SArchie Cobbs 
1417901fadf7SArchie Cobbs /*
1418901fadf7SArchie Cobbs  * Handle an ack timeout. We have an outstanding ack that we
1419901fadf7SArchie Cobbs  * were hoping to piggy-back, but haven't, so send a ZLB.
1420901fadf7SArchie Cobbs  */
1421901fadf7SArchie Cobbs static void
14220ef8db8fSGleb Smirnoff ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1423901fadf7SArchie Cobbs {
1424901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1425901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1426901fadf7SArchie Cobbs 
1427206fa244SAlexander Motin 	/* Make sure callout is still active before doing anything */
1428206fa244SAlexander Motin 	if (callout_pending(&seq->xack_timer) ||
1429206fa244SAlexander Motin 	    (!callout_active(&seq->xack_timer)))
1430206fa244SAlexander Motin 		return;
1431206fa244SAlexander Motin 
1432901fadf7SArchie Cobbs 	/* Sanity check */
1433901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1434901fadf7SArchie Cobbs 
1435206fa244SAlexander Motin 	/* Send a ZLB */
1436901fadf7SArchie Cobbs 	ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1437901fadf7SArchie Cobbs 
1438206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1439206fa244SAlexander Motin 	    as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1440206fa244SAlexander Motin 
1441206fa244SAlexander Motin 	/* Sanity check */
1442901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1443901fadf7SArchie Cobbs }
1444901fadf7SArchie Cobbs 
1445901fadf7SArchie Cobbs /*
1446901fadf7SArchie Cobbs  * Handle a transmit timeout. The peer has failed to respond
1447901fadf7SArchie Cobbs  * with an ack for our packet, so retransmit it.
1448901fadf7SArchie Cobbs  */
1449901fadf7SArchie Cobbs static void
14500ef8db8fSGleb Smirnoff ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1451901fadf7SArchie Cobbs {
1452901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1453901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1454901fadf7SArchie Cobbs 	struct mbuf *m;
1455901fadf7SArchie Cobbs 	u_int delay;
1456901fadf7SArchie Cobbs 
1457206fa244SAlexander Motin 	/* Make sure callout is still active before doing anything */
1458206fa244SAlexander Motin 	if (callout_pending(&seq->rack_timer) ||
1459206fa244SAlexander Motin 	    (!callout_active(&seq->rack_timer)))
1460206fa244SAlexander Motin 		return;
1461206fa244SAlexander Motin 
1462901fadf7SArchie Cobbs 	/* Sanity check */
1463901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1464901fadf7SArchie Cobbs 
1465*30b7addfSGleb Smirnoff 	mtx_lock(&seq->mtx);
1466901fadf7SArchie Cobbs 	priv->stats.xmitRetransmits++;
1467901fadf7SArchie Cobbs 
1468901fadf7SArchie Cobbs 	/* Have we reached the retransmit limit? If so, notify owner. */
146940097c5dSAlexander Motin 	if (seq->rexmits++ >= priv->conf.rexmit_max)
1470901fadf7SArchie Cobbs 		ng_l2tp_seq_failure(priv);
1471901fadf7SArchie Cobbs 
1472901fadf7SArchie Cobbs 	/* Restart timer, this time with an increased delay */
1473901fadf7SArchie Cobbs 	delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
147440097c5dSAlexander Motin 	if (delay > priv->conf.rexmit_max_to)
147540097c5dSAlexander Motin 		delay = priv->conf.rexmit_max_to;
14760ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, node, NULL,
14770ef8db8fSGleb Smirnoff 	    hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1478901fadf7SArchie Cobbs 
1479901fadf7SArchie Cobbs 	/* Do slow-start/congestion algorithm windowing algorithm */
1480af63939cSAlexander Motin 	seq->ns = seq->rack;
1481901fadf7SArchie Cobbs 	seq->ssth = (seq->cwnd + 1) / 2;
1482901fadf7SArchie Cobbs 	seq->cwnd = 1;
1483901fadf7SArchie Cobbs 	seq->acks = 0;
1484901fadf7SArchie Cobbs 
1485901fadf7SArchie Cobbs 	/* Retransmit oldest unack'd packet */
1486*30b7addfSGleb Smirnoff 	m = L2TP_COPY_MBUF(seq->xwin[0], M_NOWAIT);
1487*30b7addfSGleb Smirnoff 	mtx_unlock(&seq->mtx);
1488*30b7addfSGleb Smirnoff 	if (m == NULL)
1489901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1490901fadf7SArchie Cobbs 	else
1491af63939cSAlexander Motin 		ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1492901fadf7SArchie Cobbs 
1493206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1494206fa244SAlexander Motin 	    as ng_callout() is getting called each time */
1495206fa244SAlexander Motin 
1496206fa244SAlexander Motin 	/* Sanity check */
1497901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1498901fadf7SArchie Cobbs }
1499901fadf7SArchie Cobbs 
1500901fadf7SArchie Cobbs /*
1501901fadf7SArchie Cobbs  * Transmit a control stream packet, payload optional.
1502901fadf7SArchie Cobbs  * The transmit sequence number is not incremented.
1503901fadf7SArchie Cobbs  */
1504901fadf7SArchie Cobbs static int
1505901fadf7SArchie Cobbs ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1506901fadf7SArchie Cobbs {
1507901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1508148ac1daSAlexander Motin 	uint8_t *p;
1509901fadf7SArchie Cobbs 	u_int16_t session_id = 0;
1510901fadf7SArchie Cobbs 	int error;
1511901fadf7SArchie Cobbs 
1512702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1513702f9895SAlexander Motin 
1514206fa244SAlexander Motin 	/* Stop ack timer: we're sending an ack with this packet.
1515206fa244SAlexander Motin 	   Doing this before to keep state predictable after error. */
1516206fa244SAlexander Motin 	if (callout_active(&seq->xack_timer))
1517206fa244SAlexander Motin 		ng_uncallout(&seq->xack_timer, priv->node);
1518206fa244SAlexander Motin 
1519206fa244SAlexander Motin 	seq->xack = seq->nr;
1520206fa244SAlexander Motin 
1521702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1522702f9895SAlexander Motin 
1523901fadf7SArchie Cobbs 	/* If no mbuf passed, send an empty packet (ZLB) */
1524901fadf7SArchie Cobbs 	if (m == NULL) {
1525901fadf7SArchie Cobbs 
1526901fadf7SArchie Cobbs 		/* Create a new mbuf for ZLB packet */
1527eb1b1807SGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_DATA);
1528901fadf7SArchie Cobbs 		if (m == NULL) {
1529901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1530901fadf7SArchie Cobbs 			return (ENOBUFS);
1531901fadf7SArchie Cobbs 		}
1532901fadf7SArchie Cobbs 		m->m_len = m->m_pkthdr.len = 12;
1533901fadf7SArchie Cobbs 		m->m_pkthdr.rcvif = NULL;
1534901fadf7SArchie Cobbs 		priv->stats.xmitZLBs++;
1535901fadf7SArchie Cobbs 	} else {
1536901fadf7SArchie Cobbs 
1537901fadf7SArchie Cobbs 		/* Strip off session ID */
1538901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1539901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1540901fadf7SArchie Cobbs 			return (ENOBUFS);
1541901fadf7SArchie Cobbs 		}
1542280d6bd7SAlexander Motin 		session_id = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
1543901fadf7SArchie Cobbs 
1544901fadf7SArchie Cobbs 		/* Make room for L2TP header */
1545eb1b1807SGleb Smirnoff 		M_PREPEND(m, 10, M_NOWAIT);	/* - 2 + 12 = 10 */
1546901fadf7SArchie Cobbs 		if (m == NULL) {
1547901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1548901fadf7SArchie Cobbs 			return (ENOBUFS);
1549901fadf7SArchie Cobbs 		}
1550310dc5a4SBjoern A. Zeeb 
1551310dc5a4SBjoern A. Zeeb 		/*
1552310dc5a4SBjoern A. Zeeb 		 * The below requires 12 contiguous bytes for the L2TP header
1553310dc5a4SBjoern A. Zeeb 		 * to be written into.
1554310dc5a4SBjoern A. Zeeb 		 */
1555310dc5a4SBjoern A. Zeeb 		m = m_pullup(m, 12);
1556310dc5a4SBjoern A. Zeeb 		if (m == NULL) {
1557310dc5a4SBjoern A. Zeeb 			priv->stats.memoryFailures++;
1558310dc5a4SBjoern A. Zeeb 			return (ENOBUFS);
1559310dc5a4SBjoern A. Zeeb 		}
1560901fadf7SArchie Cobbs 	}
1561901fadf7SArchie Cobbs 
1562901fadf7SArchie Cobbs 	/* Fill in L2TP header */
1563148ac1daSAlexander Motin 	p = mtod(m, u_int8_t *);
1564148ac1daSAlexander Motin 	p[0] = L2TP_CTRL_HDR >> 8;
1565148ac1daSAlexander Motin 	p[1] = L2TP_CTRL_HDR & 0xff;
1566148ac1daSAlexander Motin 	p[2] = m->m_pkthdr.len >> 8;
1567148ac1daSAlexander Motin 	p[3] = m->m_pkthdr.len & 0xff;
1568148ac1daSAlexander Motin 	p[4] = priv->conf.peer_id >> 8;
1569148ac1daSAlexander Motin 	p[5] = priv->conf.peer_id & 0xff;
1570148ac1daSAlexander Motin 	p[6] = session_id >> 8;
1571148ac1daSAlexander Motin 	p[7] = session_id & 0xff;
1572148ac1daSAlexander Motin 	p[8] = ns >> 8;
1573148ac1daSAlexander Motin 	p[9] = ns & 0xff;
1574148ac1daSAlexander Motin 	p[10] = seq->nr >> 8;
1575148ac1daSAlexander Motin 	p[11] = seq->nr & 0xff;
1576901fadf7SArchie Cobbs 
1577901fadf7SArchie Cobbs 	/* Update sequence number info and stats */
1578901fadf7SArchie Cobbs 	priv->stats.xmitPackets++;
1579901fadf7SArchie Cobbs 	priv->stats.xmitOctets += m->m_pkthdr.len;
1580901fadf7SArchie Cobbs 
1581901fadf7SArchie Cobbs 	/* Send packet */
15823ca24c28SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->lower, m);
1583901fadf7SArchie Cobbs 	return (error);
1584901fadf7SArchie Cobbs }
1585901fadf7SArchie Cobbs 
1586901fadf7SArchie Cobbs #ifdef INVARIANTS
1587901fadf7SArchie Cobbs /*
1588901fadf7SArchie Cobbs  * Sanity check sequence number state.
1589901fadf7SArchie Cobbs  */
1590901fadf7SArchie Cobbs static void
1591901fadf7SArchie Cobbs ng_l2tp_seq_check(struct l2tp_seq *seq)
1592901fadf7SArchie Cobbs {
1593702f9895SAlexander Motin 	int self_unack, peer_unack;
1594901fadf7SArchie Cobbs 	int i;
1595901fadf7SArchie Cobbs 
15967e2b43eeSDavid E. O'Brien #define CHECK(p)	KASSERT((p), ("%s: not: %s", __func__, #p))
1597901fadf7SArchie Cobbs 
1598702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1599702f9895SAlexander Motin 
1600702f9895SAlexander Motin 	self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1601702f9895SAlexander Motin 	peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1602901fadf7SArchie Cobbs 	CHECK(seq->wmax <= L2TP_MAX_XWIN);
1603901fadf7SArchie Cobbs 	CHECK(seq->cwnd >= 1);
1604901fadf7SArchie Cobbs 	CHECK(seq->cwnd <= seq->wmax);
1605901fadf7SArchie Cobbs 	CHECK(seq->ssth >= 1);
1606901fadf7SArchie Cobbs 	CHECK(seq->ssth <= seq->wmax);
1607901fadf7SArchie Cobbs 	if (seq->cwnd < seq->ssth)
1608901fadf7SArchie Cobbs 		CHECK(seq->acks == 0);
1609901fadf7SArchie Cobbs 	else
1610901fadf7SArchie Cobbs 		CHECK(seq->acks <= seq->cwnd);
1611901fadf7SArchie Cobbs 	CHECK(self_unack >= 0);
1612901fadf7SArchie Cobbs 	CHECK(peer_unack >= 0);
1613901fadf7SArchie Cobbs 	CHECK(peer_unack <= seq->wmax);
1614206fa244SAlexander Motin 	CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1615206fa244SAlexander Motin 	CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1616901fadf7SArchie Cobbs 	for (i = 0; i < peer_unack; i++)
1617901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] != NULL);
1618901fadf7SArchie Cobbs 	for ( ; i < seq->cwnd; i++)	    /* verify peer's recv window full */
1619901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] == NULL);
1620901fadf7SArchie Cobbs 
1621702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1622702f9895SAlexander Motin 
1623901fadf7SArchie Cobbs #undef CHECK
1624901fadf7SArchie Cobbs }
1625901fadf7SArchie Cobbs #endif	/* INVARIANTS */
1626