xref: /freebsd/sys/netgraph/ng_l2tp.c (revision af63939c678365d3eb7ef9e179cf60d22f6a4d4a)
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
65901fadf7SArchie Cobbs 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 */
101901fadf7SArchie Cobbs #define L2TP_SEQ_DIFF(x, y)	((int)((int16_t)(x) - (int16_t)(y)))
102901fadf7SArchie Cobbs 
103901fadf7SArchie Cobbs /*
104901fadf7SArchie Cobbs  * Sequence number state
105901fadf7SArchie Cobbs  *
106901fadf7SArchie Cobbs  * Invariants:
107901fadf7SArchie Cobbs  *    - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
108901fadf7SArchie Cobbs  *    - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
109901fadf7SArchie Cobbs  *    - The first (ns - rack) mbuf's in xwin[] array are copies of these
110901fadf7SArchie Cobbs  *	unacknowledged packets; the remainder of xwin[] consists first of
111901fadf7SArchie Cobbs  *	zero or more further untransmitted packets in the transmit queue
112901fadf7SArchie Cobbs  *    - We try to keep the peer's receive window as full as possible.
113901fadf7SArchie Cobbs  *	Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
114901fadf7SArchie Cobbs  *    - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
115901fadf7SArchie Cobbs  *    - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
116901fadf7SArchie Cobbs  *    - xack_timer is running iff xack != nr (unack'd rec'd pkts)
117901fadf7SArchie Cobbs  */
118901fadf7SArchie Cobbs struct l2tp_seq {
119901fadf7SArchie Cobbs 	u_int16_t		ns;		/* next xmit seq we send */
120901fadf7SArchie Cobbs 	u_int16_t		nr;		/* next recv seq we expect */
121af63939cSAlexander Motin 	u_int16_t		inproc;		/* packet is in processing */
122901fadf7SArchie Cobbs 	u_int16_t		rack;		/* last 'nr' we rec'd */
123901fadf7SArchie Cobbs 	u_int16_t		xack;		/* last 'nr' we sent */
124901fadf7SArchie Cobbs 	u_int16_t		wmax;		/* peer's max recv window */
125901fadf7SArchie Cobbs 	u_int16_t		cwnd;		/* current congestion window */
126901fadf7SArchie Cobbs 	u_int16_t		ssth;		/* slow start threshold */
127901fadf7SArchie Cobbs 	u_int16_t		acks;		/* # consecutive acks rec'd */
128901fadf7SArchie Cobbs 	u_int16_t		rexmits;	/* # retransmits sent */
129901fadf7SArchie Cobbs 	struct callout		rack_timer;	/* retransmit timer */
130901fadf7SArchie Cobbs 	struct callout		xack_timer;	/* delayed ack timer */
131901fadf7SArchie Cobbs 	struct mbuf		*xwin[L2TP_MAX_XWIN];	/* transmit window */
132702f9895SAlexander Motin 	struct mtx		mtx;			/* seq mutex */
133901fadf7SArchie Cobbs };
134901fadf7SArchie Cobbs 
135901fadf7SArchie Cobbs /* Node private data */
136901fadf7SArchie Cobbs struct ng_l2tp_private {
137901fadf7SArchie Cobbs 	node_p			node;		/* back pointer to node */
138901fadf7SArchie Cobbs 	hook_p			ctrl;		/* hook to upper layers */
139901fadf7SArchie Cobbs 	hook_p			lower;		/* hook to lower layers */
140901fadf7SArchie Cobbs 	struct ng_l2tp_config	conf;		/* node configuration */
141901fadf7SArchie Cobbs 	struct ng_l2tp_stats	stats;		/* node statistics */
142901fadf7SArchie Cobbs 	struct l2tp_seq		seq;		/* ctrl sequence number state */
143901fadf7SArchie Cobbs 	ng_ID_t			ftarget;	/* failure message target */
144901fadf7SArchie Cobbs };
145901fadf7SArchie Cobbs typedef struct ng_l2tp_private *priv_p;
146901fadf7SArchie Cobbs 
147901fadf7SArchie Cobbs /* Hook private data (data session hooks only) */
148901fadf7SArchie Cobbs struct ng_l2tp_hook_private {
149901fadf7SArchie Cobbs 	struct ng_l2tp_sess_config	conf;	/* hook/session config */
1504807330cSBjoern A. Zeeb 	struct ng_l2tp_session_stats	stats;	/* per sessions statistics */
151901fadf7SArchie Cobbs 	u_int16_t			ns;	/* data ns sequence number */
152901fadf7SArchie Cobbs 	u_int16_t			nr;	/* data nr sequence number */
153901fadf7SArchie Cobbs };
154901fadf7SArchie Cobbs typedef struct ng_l2tp_hook_private *hookpriv_p;
155901fadf7SArchie Cobbs 
156901fadf7SArchie Cobbs /* Netgraph node methods */
157901fadf7SArchie Cobbs static ng_constructor_t	ng_l2tp_constructor;
158901fadf7SArchie Cobbs static ng_rcvmsg_t	ng_l2tp_rcvmsg;
159901fadf7SArchie Cobbs static ng_shutdown_t	ng_l2tp_shutdown;
160901fadf7SArchie Cobbs static ng_newhook_t	ng_l2tp_newhook;
161901fadf7SArchie Cobbs static ng_rcvdata_t	ng_l2tp_rcvdata;
162bf741e4dSAlexander Motin static ng_rcvdata_t	ng_l2tp_rcvdata_lower;
163bf741e4dSAlexander Motin static ng_rcvdata_t	ng_l2tp_rcvdata_ctrl;
164901fadf7SArchie Cobbs static ng_disconnect_t	ng_l2tp_disconnect;
165901fadf7SArchie Cobbs 
166901fadf7SArchie Cobbs /* Internal functions */
167901fadf7SArchie Cobbs static int	ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
168901fadf7SArchie Cobbs 
169901fadf7SArchie Cobbs static void	ng_l2tp_seq_init(priv_p priv);
1701e031324SBjoern A. Zeeb static int	ng_l2tp_seq_set(priv_p priv,
1711e031324SBjoern A. Zeeb 			const struct ng_l2tp_seq_config *conf);
172901fadf7SArchie Cobbs static int	ng_l2tp_seq_adjust(priv_p priv,
173901fadf7SArchie Cobbs 			const struct ng_l2tp_config *conf);
174901fadf7SArchie Cobbs static void	ng_l2tp_seq_reset(priv_p priv);
175901fadf7SArchie Cobbs static void	ng_l2tp_seq_failure(priv_p priv);
176901fadf7SArchie Cobbs static void	ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
1770ef8db8fSGleb Smirnoff static void	ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
1780ef8db8fSGleb Smirnoff 		    void *arg1, int arg2);
1790ef8db8fSGleb Smirnoff static void	ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
1800ef8db8fSGleb Smirnoff 		    void *arg1, int arg2);
181901fadf7SArchie Cobbs 
182901fadf7SArchie Cobbs static ng_fn_eachhook	ng_l2tp_find_session;
183901fadf7SArchie Cobbs static ng_fn_eachhook	ng_l2tp_reset_session;
184901fadf7SArchie Cobbs 
185901fadf7SArchie Cobbs #ifdef INVARIANTS
186901fadf7SArchie Cobbs static void	ng_l2tp_seq_check(struct l2tp_seq *seq);
187901fadf7SArchie Cobbs #endif
188901fadf7SArchie Cobbs 
1891e031324SBjoern A. Zeeb /* Parse type for struct ng_l2tp_seq_config. */
1901e031324SBjoern A. Zeeb static const struct ng_parse_struct_field
1911e031324SBjoern A. Zeeb 	ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
1921e031324SBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_seq_config_type = {
1931e031324SBjoern A. Zeeb 	&ng_parse_struct_type,
1941e031324SBjoern A. Zeeb 	&ng_l2tp_seq_config_fields
1951e031324SBjoern A. Zeeb };
1961e031324SBjoern A. Zeeb 
197901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_config */
198901fadf7SArchie Cobbs static const struct ng_parse_struct_field
199901fadf7SArchie Cobbs 	ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
200901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_config_type = {
201901fadf7SArchie Cobbs 	&ng_parse_struct_type,
202901fadf7SArchie Cobbs 	&ng_l2tp_config_type_fields,
203901fadf7SArchie Cobbs };
204901fadf7SArchie Cobbs 
205901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_sess_config */
206901fadf7SArchie Cobbs static const struct ng_parse_struct_field
207901fadf7SArchie Cobbs 	ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
208901fadf7SArchie Cobbs static const struct ng_parse_type ng_l2tp_sess_config_type = {
209901fadf7SArchie Cobbs 	&ng_parse_struct_type,
210901fadf7SArchie Cobbs 	&ng_l2tp_sess_config_type_fields,
211901fadf7SArchie Cobbs };
212901fadf7SArchie Cobbs 
213901fadf7SArchie Cobbs /* Parse type for struct ng_l2tp_stats */
214901fadf7SArchie Cobbs static const struct ng_parse_struct_field
215901fadf7SArchie Cobbs 	ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
2162c9027fcSArchie Cobbs static const struct ng_parse_type ng_l2tp_stats_type = {
217901fadf7SArchie Cobbs 	&ng_parse_struct_type,
218901fadf7SArchie Cobbs 	&ng_l2tp_stats_type_fields
219901fadf7SArchie Cobbs };
220901fadf7SArchie Cobbs 
2214807330cSBjoern A. Zeeb /* Parse type for struct ng_l2tp_session_stats. */
2224807330cSBjoern A. Zeeb static const struct ng_parse_struct_field
2234807330cSBjoern A. Zeeb 	ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
2244807330cSBjoern A. Zeeb static const struct ng_parse_type ng_l2tp_session_stats_type = {
2254807330cSBjoern A. Zeeb 	&ng_parse_struct_type,
2264807330cSBjoern A. Zeeb 	&ng_l2tp_session_stats_type_fields
2274807330cSBjoern A. Zeeb };
2284807330cSBjoern A. Zeeb 
229901fadf7SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
230901fadf7SArchie Cobbs static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
231901fadf7SArchie Cobbs 	{
232901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
233901fadf7SArchie Cobbs 	  NGM_L2TP_SET_CONFIG,
234901fadf7SArchie Cobbs 	  "setconfig",
235901fadf7SArchie Cobbs 	  &ng_l2tp_config_type,
236901fadf7SArchie Cobbs 	  NULL
237901fadf7SArchie Cobbs 	},
238901fadf7SArchie Cobbs 	{
239901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
240901fadf7SArchie Cobbs 	  NGM_L2TP_GET_CONFIG,
241901fadf7SArchie Cobbs 	  "getconfig",
242901fadf7SArchie Cobbs 	  NULL,
243901fadf7SArchie Cobbs 	  &ng_l2tp_config_type
244901fadf7SArchie Cobbs 	},
245901fadf7SArchie Cobbs 	{
246901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
247901fadf7SArchie Cobbs 	  NGM_L2TP_SET_SESS_CONFIG,
248901fadf7SArchie Cobbs 	  "setsessconfig",
249901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type,
250901fadf7SArchie Cobbs 	  NULL
251901fadf7SArchie Cobbs 	},
252901fadf7SArchie Cobbs 	{
253901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
254901fadf7SArchie Cobbs 	  NGM_L2TP_GET_SESS_CONFIG,
255901fadf7SArchie Cobbs 	  "getsessconfig",
256901fadf7SArchie Cobbs 	  &ng_parse_hint16_type,
257901fadf7SArchie Cobbs 	  &ng_l2tp_sess_config_type
258901fadf7SArchie Cobbs 	},
259901fadf7SArchie Cobbs 	{
260901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
261901fadf7SArchie Cobbs 	  NGM_L2TP_GET_STATS,
262901fadf7SArchie Cobbs 	  "getstats",
263901fadf7SArchie Cobbs 	  NULL,
2642c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
265901fadf7SArchie Cobbs 	},
266901fadf7SArchie Cobbs 	{
267901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
268901fadf7SArchie Cobbs 	  NGM_L2TP_CLR_STATS,
269901fadf7SArchie Cobbs 	  "clrstats",
270901fadf7SArchie Cobbs 	  NULL,
271901fadf7SArchie Cobbs 	  NULL
272901fadf7SArchie Cobbs 	},
273901fadf7SArchie Cobbs 	{
274901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
275901fadf7SArchie Cobbs 	  NGM_L2TP_GETCLR_STATS,
276901fadf7SArchie Cobbs 	  "getclrstats",
277901fadf7SArchie Cobbs 	  NULL,
2782c9027fcSArchie Cobbs 	  &ng_l2tp_stats_type
279901fadf7SArchie Cobbs 	},
280901fadf7SArchie Cobbs 	{
281901fadf7SArchie Cobbs 	  NGM_L2TP_COOKIE,
2824807330cSBjoern A. Zeeb 	  NGM_L2TP_GET_SESSION_STATS,
2834807330cSBjoern A. Zeeb 	  "getsessstats",
2844807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2854807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
2864807330cSBjoern A. Zeeb 	},
2874807330cSBjoern A. Zeeb 	{
2884807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
2894807330cSBjoern A. Zeeb 	  NGM_L2TP_CLR_SESSION_STATS,
2904807330cSBjoern A. Zeeb 	  "clrsessstats",
2914807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2924807330cSBjoern A. Zeeb 	  NULL
2934807330cSBjoern A. Zeeb 	},
2944807330cSBjoern A. Zeeb 	{
2954807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
2964807330cSBjoern A. Zeeb 	  NGM_L2TP_GETCLR_SESSION_STATS,
2974807330cSBjoern A. Zeeb 	  "getclrsessstats",
2984807330cSBjoern A. Zeeb 	  &ng_parse_int16_type,
2994807330cSBjoern A. Zeeb 	  &ng_l2tp_session_stats_type
3004807330cSBjoern A. Zeeb 	},
3014807330cSBjoern A. Zeeb 	{
3024807330cSBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
303901fadf7SArchie Cobbs 	  NGM_L2TP_ACK_FAILURE,
304901fadf7SArchie Cobbs 	  "ackfailure",
305901fadf7SArchie Cobbs 	  NULL,
306901fadf7SArchie Cobbs 	  NULL
307901fadf7SArchie Cobbs 	},
3081e031324SBjoern A. Zeeb 	{
3091e031324SBjoern A. Zeeb 	  NGM_L2TP_COOKIE,
3101e031324SBjoern A. Zeeb 	  NGM_L2TP_SET_SEQ,
3111e031324SBjoern A. Zeeb 	  "setsequence",
3121e031324SBjoern A. Zeeb 	  &ng_l2tp_seq_config_type,
3131e031324SBjoern A. Zeeb 	  NULL
3141e031324SBjoern A. Zeeb 	},
315901fadf7SArchie Cobbs 	{ 0 }
316901fadf7SArchie Cobbs };
317901fadf7SArchie Cobbs 
318901fadf7SArchie Cobbs /* Node type descriptor */
319901fadf7SArchie Cobbs static struct ng_type ng_l2tp_typestruct = {
320f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
321f8aae777SJulian Elischer 	.name =		NG_L2TP_NODE_TYPE,
322f8aae777SJulian Elischer 	.constructor =	ng_l2tp_constructor,
323f8aae777SJulian Elischer 	.rcvmsg =	ng_l2tp_rcvmsg,
324f8aae777SJulian Elischer 	.shutdown =	ng_l2tp_shutdown,
325f8aae777SJulian Elischer 	.newhook =	ng_l2tp_newhook,
326f8aae777SJulian Elischer 	.rcvdata =	ng_l2tp_rcvdata,
327f8aae777SJulian Elischer 	.disconnect =	ng_l2tp_disconnect,
328f8aae777SJulian Elischer 	.cmdlist =	ng_l2tp_cmdlist,
329901fadf7SArchie Cobbs };
330901fadf7SArchie Cobbs NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
331901fadf7SArchie Cobbs 
332901fadf7SArchie Cobbs /* Sequence number state sanity checking */
333901fadf7SArchie Cobbs #ifdef INVARIANTS
334901fadf7SArchie Cobbs #define L2TP_SEQ_CHECK(seq)	ng_l2tp_seq_check(seq)
335901fadf7SArchie Cobbs #else
336901fadf7SArchie Cobbs #define L2TP_SEQ_CHECK(x)	do { } while (0)
337901fadf7SArchie Cobbs #endif
338901fadf7SArchie Cobbs 
33986fea6beSBosko Milekic /* memmove macro */
340f7854568SDag-Erling Smørgrav #define memmove(d, s, l)	bcopy(s, d, l)
341901fadf7SArchie Cobbs 
342901fadf7SArchie Cobbs /* Whether to use m_copypacket() or m_dup() */
343901fadf7SArchie Cobbs #define L2TP_COPY_MBUF		m_copypacket
344901fadf7SArchie Cobbs 
345bf741e4dSAlexander Motin #define ERROUT(x)	do { error = (x); goto done; } while (0)
346bf741e4dSAlexander Motin 
347901fadf7SArchie Cobbs /************************************************************************
348901fadf7SArchie Cobbs 			NETGRAPH NODE STUFF
349901fadf7SArchie Cobbs ************************************************************************/
350901fadf7SArchie Cobbs 
351901fadf7SArchie Cobbs /*
352901fadf7SArchie Cobbs  * Node type constructor
353901fadf7SArchie Cobbs  */
354901fadf7SArchie Cobbs static int
355901fadf7SArchie Cobbs ng_l2tp_constructor(node_p node)
356901fadf7SArchie Cobbs {
357901fadf7SArchie Cobbs 	priv_p priv;
358901fadf7SArchie Cobbs 
359901fadf7SArchie Cobbs 	/* Allocate private structure */
360901fadf7SArchie Cobbs 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
361901fadf7SArchie Cobbs 	if (priv == NULL)
362901fadf7SArchie Cobbs 		return (ENOMEM);
363901fadf7SArchie Cobbs 	NG_NODE_SET_PRIVATE(node, priv);
364901fadf7SArchie Cobbs 	priv->node = node;
365901fadf7SArchie Cobbs 
366901fadf7SArchie Cobbs 	/* Apply a semi-reasonable default configuration */
367901fadf7SArchie Cobbs 	priv->conf.peer_win = 1;
368901fadf7SArchie Cobbs 	priv->conf.rexmit_max = L2TP_MAX_REXMIT;
369901fadf7SArchie Cobbs 	priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
370901fadf7SArchie Cobbs 
371901fadf7SArchie Cobbs 	/* Initialize sequence number state */
372901fadf7SArchie Cobbs 	ng_l2tp_seq_init(priv);
373901fadf7SArchie Cobbs 
374901fadf7SArchie Cobbs 	/* Done */
375901fadf7SArchie Cobbs 	return (0);
376901fadf7SArchie Cobbs }
377901fadf7SArchie Cobbs 
378901fadf7SArchie Cobbs /*
379901fadf7SArchie Cobbs  * Give our OK for a hook to be added.
380901fadf7SArchie Cobbs  */
381901fadf7SArchie Cobbs static int
382901fadf7SArchie Cobbs ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
383901fadf7SArchie Cobbs {
384901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
385901fadf7SArchie Cobbs 
386901fadf7SArchie Cobbs 	/* Check hook name */
387901fadf7SArchie Cobbs 	if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
388901fadf7SArchie Cobbs 		if (priv->ctrl != NULL)
389901fadf7SArchie Cobbs 			return (EISCONN);
390901fadf7SArchie Cobbs 		priv->ctrl = hook;
391bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
392901fadf7SArchie Cobbs 	} else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
393901fadf7SArchie Cobbs 		if (priv->lower != NULL)
394901fadf7SArchie Cobbs 			return (EISCONN);
395901fadf7SArchie Cobbs 		priv->lower = hook;
396bf741e4dSAlexander Motin 		NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
397901fadf7SArchie Cobbs 	} else {
398901fadf7SArchie Cobbs 		static const char hexdig[16] = "0123456789abcdef";
399901fadf7SArchie Cobbs 		u_int16_t session_id;
400901fadf7SArchie Cobbs 		hookpriv_p hpriv;
401901fadf7SArchie Cobbs 		const char *hex;
402901fadf7SArchie Cobbs 		int i;
403901fadf7SArchie Cobbs 		int j;
404901fadf7SArchie Cobbs 
405901fadf7SArchie Cobbs 		/* Parse hook name to get session ID */
406901fadf7SArchie Cobbs 		if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
407901fadf7SArchie Cobbs 		    sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
408901fadf7SArchie Cobbs 			return (EINVAL);
409901fadf7SArchie Cobbs 		hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
410901fadf7SArchie Cobbs 		for (session_id = i = 0; i < 4; i++) {
411901fadf7SArchie Cobbs 			for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
412901fadf7SArchie Cobbs 			if (j == 16)
413901fadf7SArchie Cobbs 				return (EINVAL);
414901fadf7SArchie Cobbs 			session_id = (session_id << 4) | j;
415901fadf7SArchie Cobbs 		}
416901fadf7SArchie Cobbs 		if (hex[i] != '\0')
417901fadf7SArchie Cobbs 			return (EINVAL);
418901fadf7SArchie Cobbs 
419901fadf7SArchie Cobbs 		/* Create hook private structure */
420901fadf7SArchie Cobbs 		MALLOC(hpriv, hookpriv_p,
421901fadf7SArchie Cobbs 		    sizeof(*hpriv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
422901fadf7SArchie Cobbs 		if (hpriv == NULL)
423901fadf7SArchie Cobbs 			return (ENOMEM);
424901fadf7SArchie Cobbs 		hpriv->conf.session_id = htons(session_id);
425901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
426901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
427901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, hpriv);
428901fadf7SArchie Cobbs 	}
429901fadf7SArchie Cobbs 
430901fadf7SArchie Cobbs 	/* Done */
431901fadf7SArchie Cobbs 	return (0);
432901fadf7SArchie Cobbs }
433901fadf7SArchie Cobbs 
434901fadf7SArchie Cobbs /*
435901fadf7SArchie Cobbs  * Receive a control message.
436901fadf7SArchie Cobbs  */
437901fadf7SArchie Cobbs static int
438901fadf7SArchie Cobbs ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
439901fadf7SArchie Cobbs {
440901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
441901fadf7SArchie Cobbs 	struct ng_mesg *resp = NULL;
442901fadf7SArchie Cobbs 	struct ng_mesg *msg;
443901fadf7SArchie Cobbs 	int error = 0;
444901fadf7SArchie Cobbs 
445901fadf7SArchie Cobbs 	NGI_GET_MSG(item, msg);
446901fadf7SArchie Cobbs 	switch (msg->header.typecookie) {
447901fadf7SArchie Cobbs 	case NGM_L2TP_COOKIE:
448901fadf7SArchie Cobbs 		switch (msg->header.cmd) {
449901fadf7SArchie Cobbs 		case NGM_L2TP_SET_CONFIG:
450901fadf7SArchie Cobbs 		    {
451901fadf7SArchie Cobbs 			struct ng_l2tp_config *const conf =
452901fadf7SArchie Cobbs 				(struct ng_l2tp_config *)msg->data;
453901fadf7SArchie Cobbs 
454901fadf7SArchie Cobbs 			/* Check for invalid or illegal config */
455901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
456901fadf7SArchie Cobbs 				error = EINVAL;
457901fadf7SArchie Cobbs 				break;
458901fadf7SArchie Cobbs 			}
459901fadf7SArchie Cobbs 			conf->enabled = !!conf->enabled;
460901fadf7SArchie Cobbs 			conf->match_id = !!conf->match_id;
461901fadf7SArchie Cobbs 			conf->tunnel_id = htons(conf->tunnel_id);
462901fadf7SArchie Cobbs 			conf->peer_id = htons(conf->peer_id);
463901fadf7SArchie Cobbs 			if (priv->conf.enabled
464901fadf7SArchie Cobbs 			    && ((priv->conf.tunnel_id != 0
465901fadf7SArchie Cobbs 			       && conf->tunnel_id != priv->conf.tunnel_id)
466901fadf7SArchie Cobbs 			      || ((priv->conf.peer_id != 0
467901fadf7SArchie Cobbs 			       && conf->peer_id != priv->conf.peer_id)))) {
468901fadf7SArchie Cobbs 				error = EBUSY;
469901fadf7SArchie Cobbs 				break;
470901fadf7SArchie Cobbs 			}
471901fadf7SArchie Cobbs 
472901fadf7SArchie Cobbs 			/* Save calling node as failure target */
473901fadf7SArchie Cobbs 			priv->ftarget = NGI_RETADDR(item);
474901fadf7SArchie Cobbs 
475901fadf7SArchie Cobbs 			/* Adjust sequence number state */
476901fadf7SArchie Cobbs 			if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
477901fadf7SArchie Cobbs 				break;
478901fadf7SArchie Cobbs 
479901fadf7SArchie Cobbs 			/* Update node's config */
480901fadf7SArchie Cobbs 			priv->conf = *conf;
481901fadf7SArchie Cobbs 			break;
482901fadf7SArchie Cobbs 		    }
483901fadf7SArchie Cobbs 		case NGM_L2TP_GET_CONFIG:
484901fadf7SArchie Cobbs 		    {
485901fadf7SArchie Cobbs 			struct ng_l2tp_config *conf;
486901fadf7SArchie Cobbs 
487901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
488901fadf7SArchie Cobbs 			if (resp == NULL) {
489901fadf7SArchie Cobbs 				error = ENOMEM;
490901fadf7SArchie Cobbs 				break;
491901fadf7SArchie Cobbs 			}
492901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_config *)resp->data;
493901fadf7SArchie Cobbs 			*conf = priv->conf;
494901fadf7SArchie Cobbs 
495901fadf7SArchie Cobbs 			/* Put ID's in host order */
496901fadf7SArchie Cobbs 			conf->tunnel_id = ntohs(conf->tunnel_id);
497901fadf7SArchie Cobbs 			conf->peer_id = ntohs(conf->peer_id);
498901fadf7SArchie Cobbs 			break;
499901fadf7SArchie Cobbs 		    }
500901fadf7SArchie Cobbs 		case NGM_L2TP_SET_SESS_CONFIG:
501901fadf7SArchie Cobbs 		    {
502901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *const conf =
503901fadf7SArchie Cobbs 			    (struct ng_l2tp_sess_config *)msg->data;
504901fadf7SArchie Cobbs 			hookpriv_p hpriv;
505901fadf7SArchie Cobbs 			hook_p hook;
506901fadf7SArchie Cobbs 
5071e031324SBjoern A. Zeeb 			/* Check for invalid or illegal config. */
508901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf)) {
509901fadf7SArchie Cobbs 				error = EINVAL;
510901fadf7SArchie Cobbs 				break;
511901fadf7SArchie Cobbs 			}
512901fadf7SArchie Cobbs 
513901fadf7SArchie Cobbs 			/* Put ID's in network order */
514901fadf7SArchie Cobbs 			conf->session_id = htons(conf->session_id);
515901fadf7SArchie Cobbs 			conf->peer_id = htons(conf->peer_id);
516901fadf7SArchie Cobbs 
517901fadf7SArchie Cobbs 			/* Find matching hook */
518901fadf7SArchie Cobbs 			NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
519901fadf7SArchie Cobbs 			    (void *)(uintptr_t)conf->session_id, hook);
520901fadf7SArchie Cobbs 			if (hook == NULL) {
521901fadf7SArchie Cobbs 				error = ENOENT;
522901fadf7SArchie Cobbs 				break;
523901fadf7SArchie Cobbs 			}
524901fadf7SArchie Cobbs 			hpriv = NG_HOOK_PRIVATE(hook);
525901fadf7SArchie Cobbs 
526901fadf7SArchie Cobbs 			/* Update hook's config */
527901fadf7SArchie Cobbs 			hpriv->conf = *conf;
528901fadf7SArchie Cobbs 			break;
529901fadf7SArchie Cobbs 		    }
530901fadf7SArchie Cobbs 		case NGM_L2TP_GET_SESS_CONFIG:
531901fadf7SArchie Cobbs 		    {
532901fadf7SArchie Cobbs 			struct ng_l2tp_sess_config *conf;
533901fadf7SArchie Cobbs 			u_int16_t session_id;
534901fadf7SArchie Cobbs 			hookpriv_p hpriv;
535901fadf7SArchie Cobbs 			hook_p hook;
536901fadf7SArchie Cobbs 
537901fadf7SArchie Cobbs 			/* Get session ID */
538901fadf7SArchie Cobbs 			if (msg->header.arglen != sizeof(session_id)) {
539901fadf7SArchie Cobbs 				error = EINVAL;
540901fadf7SArchie Cobbs 				break;
541901fadf7SArchie Cobbs 			}
542901fadf7SArchie Cobbs 			memcpy(&session_id, msg->data, 2);
543901fadf7SArchie Cobbs 			session_id = htons(session_id);
544901fadf7SArchie Cobbs 
545901fadf7SArchie Cobbs 			/* Find matching hook */
546901fadf7SArchie Cobbs 			NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
547901fadf7SArchie Cobbs 			    (void *)(uintptr_t)session_id, hook);
548901fadf7SArchie Cobbs 			if (hook == NULL) {
549901fadf7SArchie Cobbs 				error = ENOENT;
550901fadf7SArchie Cobbs 				break;
551901fadf7SArchie Cobbs 			}
552901fadf7SArchie Cobbs 			hpriv = NG_HOOK_PRIVATE(hook);
553901fadf7SArchie Cobbs 
554901fadf7SArchie Cobbs 			/* Send response */
555901fadf7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
556901fadf7SArchie Cobbs 			if (resp == NULL) {
557901fadf7SArchie Cobbs 				error = ENOMEM;
558901fadf7SArchie Cobbs 				break;
559901fadf7SArchie Cobbs 			}
560901fadf7SArchie Cobbs 			conf = (struct ng_l2tp_sess_config *)resp->data;
561901fadf7SArchie Cobbs 			*conf = hpriv->conf;
562901fadf7SArchie Cobbs 
563901fadf7SArchie Cobbs 			/* Put ID's in host order */
564901fadf7SArchie Cobbs 			conf->session_id = ntohs(conf->session_id);
565901fadf7SArchie Cobbs 			conf->peer_id = ntohs(conf->peer_id);
566901fadf7SArchie Cobbs 			break;
567901fadf7SArchie Cobbs 		    }
568901fadf7SArchie Cobbs 		case NGM_L2TP_GET_STATS:
569901fadf7SArchie Cobbs 		case NGM_L2TP_CLR_STATS:
570901fadf7SArchie Cobbs 		case NGM_L2TP_GETCLR_STATS:
571901fadf7SArchie Cobbs 		    {
572901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
573901fadf7SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
574901fadf7SArchie Cobbs 				    sizeof(priv->stats), M_NOWAIT);
575901fadf7SArchie Cobbs 				if (resp == NULL) {
576901fadf7SArchie Cobbs 					error = ENOMEM;
577901fadf7SArchie Cobbs 					break;
578901fadf7SArchie Cobbs 				}
579901fadf7SArchie Cobbs 				memcpy(resp->data,
580901fadf7SArchie Cobbs 				    &priv->stats, sizeof(priv->stats));
581901fadf7SArchie Cobbs 			}
582901fadf7SArchie Cobbs 			if (msg->header.cmd != NGM_L2TP_GET_STATS)
583901fadf7SArchie Cobbs 				memset(&priv->stats, 0, sizeof(priv->stats));
584901fadf7SArchie Cobbs 			break;
585901fadf7SArchie Cobbs 		    }
5864807330cSBjoern A. Zeeb 		case NGM_L2TP_GET_SESSION_STATS:
5874807330cSBjoern A. Zeeb 		case NGM_L2TP_CLR_SESSION_STATS:
5884807330cSBjoern A. Zeeb 		case NGM_L2TP_GETCLR_SESSION_STATS:
5894807330cSBjoern A. Zeeb 		    {
5904807330cSBjoern A. Zeeb 			uint16_t session_id;
5914807330cSBjoern A. Zeeb 			hookpriv_p hpriv;
5924807330cSBjoern A. Zeeb 			hook_p hook;
5934807330cSBjoern A. Zeeb 
5944807330cSBjoern A. Zeeb 			/* Get session ID. */
5954807330cSBjoern A. Zeeb 			if (msg->header.arglen != sizeof(session_id)) {
5964807330cSBjoern A. Zeeb 				error = EINVAL;
5974807330cSBjoern A. Zeeb 				break;
5984807330cSBjoern A. Zeeb 			}
5994807330cSBjoern A. Zeeb 			bcopy(msg->data, &session_id, sizeof(uint16_t));
6004807330cSBjoern A. Zeeb 			session_id = htons(session_id);
6014807330cSBjoern A. Zeeb 
6024807330cSBjoern A. Zeeb 			/* Find matching hook. */
6034807330cSBjoern A. Zeeb 			NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
6044807330cSBjoern A. Zeeb 			    (void *)(uintptr_t)session_id, hook);
6054807330cSBjoern A. Zeeb 			if (hook == NULL) {
6064807330cSBjoern A. Zeeb 				error = ENOENT;
6074807330cSBjoern A. Zeeb 				break;
6084807330cSBjoern A. Zeeb 			}
6094807330cSBjoern A. Zeeb 			hpriv = NG_HOOK_PRIVATE(hook);
6104807330cSBjoern A. Zeeb 
6114807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
6124807330cSBjoern A. Zeeb 				NG_MKRESPONSE(resp, msg,
6134807330cSBjoern A. Zeeb 				    sizeof(hpriv->stats), M_NOWAIT);
6144807330cSBjoern A. Zeeb 				if (resp == NULL) {
6154807330cSBjoern A. Zeeb 					error = ENOMEM;
6164807330cSBjoern A. Zeeb 					break;
6174807330cSBjoern A. Zeeb 				}
6184807330cSBjoern A. Zeeb 				bcopy(&hpriv->stats, resp->data,
6194807330cSBjoern A. Zeeb 					sizeof(hpriv->stats));
6204807330cSBjoern A. Zeeb 			}
6214807330cSBjoern A. Zeeb 			if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
6224807330cSBjoern A. Zeeb 				bzero(&hpriv->stats, sizeof(hpriv->stats));
6234807330cSBjoern A. Zeeb 			break;
6244807330cSBjoern A. Zeeb 		    }
6251e031324SBjoern A. Zeeb 		case NGM_L2TP_SET_SEQ:
6261e031324SBjoern A. Zeeb 		    {
6271e031324SBjoern A. Zeeb 			struct ng_l2tp_seq_config *const conf =
6281e031324SBjoern A. Zeeb 				(struct ng_l2tp_seq_config *)msg->data;
6291e031324SBjoern A. Zeeb 
6301e031324SBjoern A. Zeeb 			/* Check for invalid or illegal seq config. */
6311e031324SBjoern A. Zeeb 			if (msg->header.arglen != sizeof(*conf)) {
6321e031324SBjoern A. Zeeb 				error = EINVAL;
6331e031324SBjoern A. Zeeb 				break;
6341e031324SBjoern A. Zeeb 			}
6351e031324SBjoern A. Zeeb 			conf->ns = htons(conf->ns);
6361e031324SBjoern A. Zeeb 			conf->nr = htons(conf->nr);
6371e031324SBjoern A. Zeeb 			conf->rack = htons(conf->rack);
6381e031324SBjoern A. Zeeb 			conf->xack = htons(conf->xack);
6391e031324SBjoern A. Zeeb 
6401e031324SBjoern A. Zeeb 			/* Set sequence numbers. */
6411e031324SBjoern A. Zeeb 			error = ng_l2tp_seq_set(priv, conf);
6421e031324SBjoern A. Zeeb 			break;
6431e031324SBjoern A. Zeeb 		    }
644901fadf7SArchie Cobbs 		default:
645901fadf7SArchie Cobbs 			error = EINVAL;
646901fadf7SArchie Cobbs 			break;
647901fadf7SArchie Cobbs 		}
648901fadf7SArchie Cobbs 		break;
649901fadf7SArchie Cobbs 	default:
650901fadf7SArchie Cobbs 		error = EINVAL;
651901fadf7SArchie Cobbs 		break;
652901fadf7SArchie Cobbs 	}
653901fadf7SArchie Cobbs 
654901fadf7SArchie Cobbs 	/* Done */
655901fadf7SArchie Cobbs 	NG_RESPOND_MSG(error, node, item, resp);
656901fadf7SArchie Cobbs 	NG_FREE_MSG(msg);
657901fadf7SArchie Cobbs 	return (error);
658901fadf7SArchie Cobbs }
659901fadf7SArchie Cobbs 
660901fadf7SArchie Cobbs /*
661901fadf7SArchie Cobbs  * Destroy node
662901fadf7SArchie Cobbs  */
663901fadf7SArchie Cobbs static int
664901fadf7SArchie Cobbs ng_l2tp_shutdown(node_p node)
665901fadf7SArchie Cobbs {
666901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
667901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
668901fadf7SArchie Cobbs 
669901fadf7SArchie Cobbs 	/* Sanity check */
670901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
671901fadf7SArchie Cobbs 
672901fadf7SArchie Cobbs 	/* Reset sequence number state */
673901fadf7SArchie Cobbs 	ng_l2tp_seq_reset(priv);
674901fadf7SArchie Cobbs 
675901fadf7SArchie Cobbs 	/* Free private data if neither timer is running */
6760ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->rack_timer, node);
6770ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->xack_timer, node);
6780ef8db8fSGleb Smirnoff 
679702f9895SAlexander Motin 	mtx_destroy(&seq->mtx);
680702f9895SAlexander Motin 
681901fadf7SArchie Cobbs 	FREE(priv, M_NETGRAPH_L2TP);
682901fadf7SArchie Cobbs 
683901fadf7SArchie Cobbs 	/* Unref node */
684901fadf7SArchie Cobbs 	NG_NODE_UNREF(node);
685901fadf7SArchie Cobbs 	return (0);
686901fadf7SArchie Cobbs }
687901fadf7SArchie Cobbs 
688901fadf7SArchie Cobbs /*
689901fadf7SArchie Cobbs  * Hook disconnection
690901fadf7SArchie Cobbs  */
691901fadf7SArchie Cobbs static int
692901fadf7SArchie Cobbs ng_l2tp_disconnect(hook_p hook)
693901fadf7SArchie Cobbs {
694901fadf7SArchie Cobbs 	const node_p node = NG_HOOK_NODE(hook);
695901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
696901fadf7SArchie Cobbs 
697901fadf7SArchie Cobbs 	/* Zero out hook pointer */
698901fadf7SArchie Cobbs 	if (hook == priv->ctrl)
699901fadf7SArchie Cobbs 		priv->ctrl = NULL;
700901fadf7SArchie Cobbs 	else if (hook == priv->lower)
701901fadf7SArchie Cobbs 		priv->lower = NULL;
702901fadf7SArchie Cobbs 	else {
703901fadf7SArchie Cobbs 		FREE(NG_HOOK_PRIVATE(hook), M_NETGRAPH_L2TP);
704901fadf7SArchie Cobbs 		NG_HOOK_SET_PRIVATE(hook, NULL);
705901fadf7SArchie Cobbs 	}
706901fadf7SArchie Cobbs 
707901fadf7SArchie Cobbs 	/* Go away if no longer connected to anything */
708901fadf7SArchie Cobbs 	if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
709901fadf7SArchie Cobbs 		ng_rmnode_self(node);
710901fadf7SArchie Cobbs 	return (0);
711901fadf7SArchie Cobbs }
712901fadf7SArchie Cobbs 
713901fadf7SArchie Cobbs /*************************************************************************
714901fadf7SArchie Cobbs 			INTERNAL FUNCTIONS
715901fadf7SArchie Cobbs *************************************************************************/
716901fadf7SArchie Cobbs 
717901fadf7SArchie Cobbs /*
718901fadf7SArchie Cobbs  * Find the hook with a given session ID.
719901fadf7SArchie Cobbs  */
720901fadf7SArchie Cobbs static int
721901fadf7SArchie Cobbs ng_l2tp_find_session(hook_p hook, void *arg)
722901fadf7SArchie Cobbs {
723901fadf7SArchie Cobbs 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
724901fadf7SArchie Cobbs 	const u_int16_t sid = (u_int16_t)(uintptr_t)arg;
725901fadf7SArchie Cobbs 
726901fadf7SArchie Cobbs 	if (hpriv == NULL || hpriv->conf.session_id != sid)
727901fadf7SArchie Cobbs 		return (-1);
728901fadf7SArchie Cobbs 	return (0);
729901fadf7SArchie Cobbs }
730901fadf7SArchie Cobbs 
731901fadf7SArchie Cobbs /*
732901fadf7SArchie Cobbs  * Reset a hook's session state.
733901fadf7SArchie Cobbs  */
734901fadf7SArchie Cobbs static int
735901fadf7SArchie Cobbs ng_l2tp_reset_session(hook_p hook, void *arg)
736901fadf7SArchie Cobbs {
737901fadf7SArchie Cobbs 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
738901fadf7SArchie Cobbs 
739901fadf7SArchie Cobbs 	if (hpriv != NULL) {
740901fadf7SArchie Cobbs 		hpriv->conf.control_dseq = 0;
741901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = 0;
7424807330cSBjoern A. Zeeb 		bzero(&hpriv->conf, sizeof(struct ng_l2tp_session_stats));
743901fadf7SArchie Cobbs 		hpriv->nr = 0;
744901fadf7SArchie Cobbs 		hpriv->ns = 0;
745901fadf7SArchie Cobbs 	}
746901fadf7SArchie Cobbs 	return (-1);
747901fadf7SArchie Cobbs }
748901fadf7SArchie Cobbs 
749901fadf7SArchie Cobbs /*
750901fadf7SArchie Cobbs  * Handle an incoming frame from below.
751901fadf7SArchie Cobbs  */
752901fadf7SArchie Cobbs static int
753bf741e4dSAlexander Motin ng_l2tp_rcvdata_lower(hook_p h, item_p item)
754901fadf7SArchie Cobbs {
755901fadf7SArchie Cobbs 	static const u_int16_t req_bits[2][2] = {
756901fadf7SArchie Cobbs 		{ L2TP_DATA_0BITS, L2TP_DATA_1BITS },
757901fadf7SArchie Cobbs 		{ L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
758901fadf7SArchie Cobbs 	};
759bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(h);
760901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
761901fadf7SArchie Cobbs 	hookpriv_p hpriv = NULL;
762901fadf7SArchie Cobbs 	hook_p hook = NULL;
763901fadf7SArchie Cobbs 	u_int16_t ids[2];
764901fadf7SArchie Cobbs 	struct mbuf *m;
765901fadf7SArchie Cobbs 	u_int16_t hdr;
766901fadf7SArchie Cobbs 	u_int16_t ns;
767901fadf7SArchie Cobbs 	u_int16_t nr;
768901fadf7SArchie Cobbs 	int is_ctrl;
769901fadf7SArchie Cobbs 	int error;
7704807330cSBjoern A. Zeeb 	int len, plen;
771901fadf7SArchie Cobbs 
772bf741e4dSAlexander Motin 	/* Sanity check */
773bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
774bf741e4dSAlexander Motin 
775bf741e4dSAlexander Motin 	/* If not configured, reject */
776bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
777bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
778bf741e4dSAlexander Motin 		ERROUT(ENXIO);
779bf741e4dSAlexander Motin 	}
780bf741e4dSAlexander Motin 
781901fadf7SArchie Cobbs 	/* Grab mbuf */
782901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
783901fadf7SArchie Cobbs 
7844807330cSBjoern A. Zeeb 	/* Remember full packet length; needed for per session accounting. */
7854807330cSBjoern A. Zeeb 	plen = m->m_pkthdr.len;
7864807330cSBjoern A. Zeeb 
787901fadf7SArchie Cobbs 	/* Update stats */
788901fadf7SArchie Cobbs 	priv->stats.recvPackets++;
7894807330cSBjoern A. Zeeb 	priv->stats.recvOctets += plen;
790901fadf7SArchie Cobbs 
791901fadf7SArchie Cobbs 	/* Get initial header */
792901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 6) {
793901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
794901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
795901fadf7SArchie Cobbs 		NG_FREE_M(m);
796bf741e4dSAlexander Motin 		ERROUT(EINVAL);
797901fadf7SArchie Cobbs 	}
798901fadf7SArchie Cobbs 	if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
799901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
800901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
801bf741e4dSAlexander Motin 		ERROUT(EINVAL);
802901fadf7SArchie Cobbs 	}
803901fadf7SArchie Cobbs 	hdr = ntohs(*mtod(m, u_int16_t *));
804901fadf7SArchie Cobbs 	m_adj(m, 2);
805901fadf7SArchie Cobbs 
806901fadf7SArchie Cobbs 	/* Check required header bits and minimum length */
807901fadf7SArchie Cobbs 	is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
808901fadf7SArchie Cobbs 	if ((hdr & req_bits[is_ctrl][0]) != 0
809901fadf7SArchie Cobbs 	    || (~hdr & req_bits[is_ctrl][1]) != 0) {
810901fadf7SArchie Cobbs 		priv->stats.recvInvalid++;
811901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
812901fadf7SArchie Cobbs 		NG_FREE_M(m);
813bf741e4dSAlexander Motin 		ERROUT(EINVAL);
814901fadf7SArchie Cobbs 	}
815901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 4				/* tunnel, session id */
816901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_LEN) != 0))		/* length field */
817901fadf7SArchie Cobbs 	    + (4 * ((hdr & L2TP_HDR_SEQ) != 0))		/* seq # fields */
818901fadf7SArchie Cobbs 	    + (2 * ((hdr & L2TP_HDR_OFF) != 0))) {	/* offset field */
819901fadf7SArchie Cobbs 		priv->stats.recvRunts++;
820901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
821901fadf7SArchie Cobbs 		NG_FREE_M(m);
822bf741e4dSAlexander Motin 		ERROUT(EINVAL);
823901fadf7SArchie Cobbs 	}
824901fadf7SArchie Cobbs 
825901fadf7SArchie Cobbs 	/* Get and validate length field if present */
826901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_LEN) != 0) {
827901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
828901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
829901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
830bf741e4dSAlexander Motin 			ERROUT(EINVAL);
831901fadf7SArchie Cobbs 		}
832901fadf7SArchie Cobbs 		len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
833901fadf7SArchie Cobbs 		m_adj(m, 2);
834901fadf7SArchie Cobbs 		if (len < 0 || len > m->m_pkthdr.len) {
835901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
836901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
837901fadf7SArchie Cobbs 			NG_FREE_M(m);
838bf741e4dSAlexander Motin 			ERROUT(EINVAL);
839901fadf7SArchie Cobbs 		}
840901fadf7SArchie Cobbs 		if (len < m->m_pkthdr.len)		/* trim extra bytes */
841901fadf7SArchie Cobbs 			m_adj(m, -(m->m_pkthdr.len - len));
842901fadf7SArchie Cobbs 	}
843901fadf7SArchie Cobbs 
844901fadf7SArchie Cobbs 	/* Get tunnel ID and session ID */
845901fadf7SArchie Cobbs 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
846901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
847901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
848bf741e4dSAlexander Motin 		ERROUT(EINVAL);
849901fadf7SArchie Cobbs 	}
850901fadf7SArchie Cobbs 	memcpy(ids, mtod(m, u_int16_t *), 4);
851901fadf7SArchie Cobbs 	m_adj(m, 4);
852901fadf7SArchie Cobbs 
853901fadf7SArchie Cobbs 	/* Check tunnel ID */
854901fadf7SArchie Cobbs 	if (ids[0] != priv->conf.tunnel_id
855901fadf7SArchie Cobbs 	    && (priv->conf.match_id || ids[0] != 0)) {
856901fadf7SArchie Cobbs 		priv->stats.recvWrongTunnel++;
857901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
858901fadf7SArchie Cobbs 		NG_FREE_M(m);
859bf741e4dSAlexander Motin 		ERROUT(EADDRNOTAVAIL);
860901fadf7SArchie Cobbs 	}
861901fadf7SArchie Cobbs 
862901fadf7SArchie Cobbs 	/* Check session ID (for data packets only) */
863901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) == 0) {
864901fadf7SArchie Cobbs 		NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
865901fadf7SArchie Cobbs 		    (void *)(uintptr_t)ids[1], hook);
866901fadf7SArchie Cobbs 		if (hook == NULL) {
867901fadf7SArchie Cobbs 			priv->stats.recvUnknownSID++;
868901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
869901fadf7SArchie Cobbs 			NG_FREE_M(m);
870bf741e4dSAlexander Motin 			ERROUT(ENOTCONN);
871901fadf7SArchie Cobbs 		}
872901fadf7SArchie Cobbs 		hpriv = NG_HOOK_PRIVATE(hook);
873901fadf7SArchie Cobbs 	}
874901fadf7SArchie Cobbs 
875901fadf7SArchie Cobbs 	/* Get Ns, Nr fields if present */
876901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
877901fadf7SArchie Cobbs 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
878901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
879901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
880bf741e4dSAlexander Motin 			ERROUT(EINVAL);
881901fadf7SArchie Cobbs 		}
882901fadf7SArchie Cobbs 		memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
883901fadf7SArchie Cobbs 		ns = ntohs(ns);
884901fadf7SArchie Cobbs 		memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
885901fadf7SArchie Cobbs 		nr = ntohs(nr);
886901fadf7SArchie Cobbs 		m_adj(m, 4);
887901fadf7SArchie Cobbs 	}
888901fadf7SArchie Cobbs 
889901fadf7SArchie Cobbs 	/* Strip offset padding if present */
890901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_OFF) != 0) {
891901fadf7SArchie Cobbs 		u_int16_t offset;
892901fadf7SArchie Cobbs 
893901fadf7SArchie Cobbs 		/* Get length of offset padding */
894901fadf7SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
895901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
896901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
897bf741e4dSAlexander Motin 			ERROUT(EINVAL);
898901fadf7SArchie Cobbs 		}
899901fadf7SArchie Cobbs 		memcpy(&offset, mtod(m, u_int16_t *), 2);
900901fadf7SArchie Cobbs 		offset = ntohs(offset);
901901fadf7SArchie Cobbs 
902901fadf7SArchie Cobbs 		/* Trim offset padding */
903ddb72294SBjoern A. Zeeb 		if ((2+offset) > m->m_pkthdr.len) {
904901fadf7SArchie Cobbs 			priv->stats.recvInvalid++;
905901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
906901fadf7SArchie Cobbs 			NG_FREE_M(m);
907bf741e4dSAlexander Motin 			ERROUT(EINVAL);
908901fadf7SArchie Cobbs 		}
909ddb72294SBjoern A. Zeeb 		m_adj(m, 2+offset);
910901fadf7SArchie Cobbs 	}
911901fadf7SArchie Cobbs 
912901fadf7SArchie Cobbs 	/* Handle control packets */
913901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_CTRL) != 0) {
914af63939cSAlexander Motin 		struct l2tp_seq *const seq = &priv->seq;
915901fadf7SArchie Cobbs 
916901fadf7SArchie Cobbs 		/* Handle receive ack sequence number Nr */
917901fadf7SArchie Cobbs 		ng_l2tp_seq_recv_nr(priv, nr);
918901fadf7SArchie Cobbs 
919901fadf7SArchie Cobbs 		/* Discard ZLB packets */
920901fadf7SArchie Cobbs 		if (m->m_pkthdr.len == 0) {
921901fadf7SArchie Cobbs 			priv->stats.recvZLBs++;
922901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
923901fadf7SArchie Cobbs 			NG_FREE_M(m);
924bf741e4dSAlexander Motin 			ERROUT(0);
925901fadf7SArchie Cobbs 		}
926901fadf7SArchie Cobbs 
927af63939cSAlexander Motin 		mtx_lock(&seq->mtx);
928901fadf7SArchie Cobbs 		/*
929af63939cSAlexander Motin 		 * If not what we expect or we are busy, drop packet and
930af63939cSAlexander Motin 		 * send an immediate ZLB ack.
931901fadf7SArchie Cobbs 		 */
932af63939cSAlexander Motin 		if (ns != seq->nr || seq->inproc) {
933af63939cSAlexander Motin 			if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
934af63939cSAlexander Motin 				priv->stats.recvDuplicates++;
935af63939cSAlexander Motin 			else
936af63939cSAlexander Motin 				priv->stats.recvOutOfOrder++;
937af63939cSAlexander Motin 			mtx_unlock(&seq->mtx);
938af63939cSAlexander Motin 			ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
939af63939cSAlexander Motin 			NG_FREE_ITEM(item);
940af63939cSAlexander Motin 			NG_FREE_M(m);
941af63939cSAlexander Motin 			ERROUT(0);
942af63939cSAlexander Motin 		}
943af63939cSAlexander Motin 		/*
944af63939cSAlexander Motin 		 * Until we deliver this packet we can't receive next one as
945af63939cSAlexander Motin 		 * we have no information for sending ack.
946af63939cSAlexander Motin 		 */
947af63939cSAlexander Motin 		seq->inproc = 1;
948af63939cSAlexander Motin 		mtx_unlock(&seq->mtx);
949af63939cSAlexander Motin 
950af63939cSAlexander Motin 		/* Prepend session ID to packet. */
951a163d034SWarner Losh 		M_PREPEND(m, 2, M_DONTWAIT);
952901fadf7SArchie Cobbs 		if (m == NULL) {
953901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
954901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);
955bf741e4dSAlexander Motin 			ERROUT(ENOBUFS);
956901fadf7SArchie Cobbs 		}
957901fadf7SArchie Cobbs 		memcpy(mtod(m, u_int16_t *), &ids[1], 2);
958901fadf7SArchie Cobbs 
959901fadf7SArchie Cobbs 		/* Deliver packet to upper layers */
960901fadf7SArchie Cobbs 		NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
961af63939cSAlexander Motin 
962af63939cSAlexander Motin 		mtx_lock(&seq->mtx);
963af63939cSAlexander Motin 		/* Ready to process next packet. */
964af63939cSAlexander Motin 		seq->inproc = 0;
965af63939cSAlexander Motin 
966af63939cSAlexander Motin 		/* If packet was successfully delivered send ack. */
967af63939cSAlexander Motin 		if (error == 0) {
968af63939cSAlexander Motin 			/* Update recv sequence number */
969af63939cSAlexander Motin 			seq->nr++;
970af63939cSAlexander Motin 			/* Start receive ack timer, if not already running */
971af63939cSAlexander Motin 			if (!callout_active(&seq->xack_timer)) {
972af63939cSAlexander Motin 				ng_callout(&seq->xack_timer, priv->node, NULL,
973af63939cSAlexander Motin 				    L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
974af63939cSAlexander Motin 				    NULL, 0);
975af63939cSAlexander Motin 			}
976af63939cSAlexander Motin 		}
977af63939cSAlexander Motin 		mtx_unlock(&seq->mtx);
978af63939cSAlexander Motin 
979bf741e4dSAlexander Motin 		ERROUT(error);
980901fadf7SArchie Cobbs 	}
981901fadf7SArchie Cobbs 
9824807330cSBjoern A. Zeeb 	/* Per session packet, account it. */
9834807330cSBjoern A. Zeeb 	hpriv->stats.recvPackets++;
9844807330cSBjoern A. Zeeb 	hpriv->stats.recvOctets += plen;
9854807330cSBjoern A. Zeeb 
986901fadf7SArchie Cobbs 	/* Follow peer's lead in data sequencing, if configured to do so */
987901fadf7SArchie Cobbs 	if (!hpriv->conf.control_dseq)
988901fadf7SArchie Cobbs 		hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
989901fadf7SArchie Cobbs 
990901fadf7SArchie Cobbs 	/* Handle data sequence numbers if present and enabled */
991901fadf7SArchie Cobbs 	if ((hdr & L2TP_HDR_SEQ) != 0) {
992901fadf7SArchie Cobbs 		if (hpriv->conf.enable_dseq
993901fadf7SArchie Cobbs 		    && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
994901fadf7SArchie Cobbs 			NG_FREE_ITEM(item);	/* duplicate or out of order */
995901fadf7SArchie Cobbs 			NG_FREE_M(m);
996901fadf7SArchie Cobbs 			priv->stats.recvDataDrops++;
997bf741e4dSAlexander Motin 			ERROUT(0);
998901fadf7SArchie Cobbs 		}
999901fadf7SArchie Cobbs 		hpriv->nr = ns + 1;
1000901fadf7SArchie Cobbs 	}
1001901fadf7SArchie Cobbs 
1002901fadf7SArchie Cobbs 	/* Drop empty data packets */
1003901fadf7SArchie Cobbs 	if (m->m_pkthdr.len == 0) {
1004901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1005901fadf7SArchie Cobbs 		NG_FREE_M(m);
1006bf741e4dSAlexander Motin 		ERROUT(0);
1007901fadf7SArchie Cobbs 	}
1008901fadf7SArchie Cobbs 
1009901fadf7SArchie Cobbs 	/* Deliver data */
1010901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, hook, m);
1011bf741e4dSAlexander Motin done:
1012bf741e4dSAlexander Motin 	/* Done */
1013bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1014901fadf7SArchie Cobbs 	return (error);
1015901fadf7SArchie Cobbs }
1016901fadf7SArchie Cobbs 
1017901fadf7SArchie Cobbs /*
1018901fadf7SArchie Cobbs  * Handle an outgoing control frame.
1019901fadf7SArchie Cobbs  */
1020901fadf7SArchie Cobbs static int
1021bf741e4dSAlexander Motin ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1022901fadf7SArchie Cobbs {
1023bf741e4dSAlexander Motin 	const node_p node = NG_HOOK_NODE(hook);
1024901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1025901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1026901fadf7SArchie Cobbs 	struct mbuf *m;
1027bf741e4dSAlexander Motin 	int error;
1028901fadf7SArchie Cobbs 	int i;
1029702f9895SAlexander Motin 	u_int16_t	ns;
1030901fadf7SArchie Cobbs 
1031bf741e4dSAlexander Motin 	/* Sanity check */
1032bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1033bf741e4dSAlexander Motin 
1034bf741e4dSAlexander Motin 	/* If not configured, reject */
1035bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1036bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1037bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1038bf741e4dSAlexander Motin 	}
1039bf741e4dSAlexander Motin 
1040901fadf7SArchie Cobbs 	/* Grab mbuf and discard other stuff XXX */
1041901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1042901fadf7SArchie Cobbs 	NG_FREE_ITEM(item);
1043901fadf7SArchie Cobbs 
1044901fadf7SArchie Cobbs 	/* Packet should have session ID prepended */
1045901fadf7SArchie Cobbs 	if (m->m_pkthdr.len < 2) {
1046901fadf7SArchie Cobbs 		priv->stats.xmitInvalid++;
1047901fadf7SArchie Cobbs 		m_freem(m);
1048bf741e4dSAlexander Motin 		ERROUT(EINVAL);
1049901fadf7SArchie Cobbs 	}
1050901fadf7SArchie Cobbs 
1051901fadf7SArchie Cobbs 	/* Check max length */
1052901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 14) {
1053901fadf7SArchie Cobbs 		priv->stats.xmitTooBig++;
1054901fadf7SArchie Cobbs 		m_freem(m);
1055bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1056901fadf7SArchie Cobbs 	}
1057901fadf7SArchie Cobbs 
1058702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1059702f9895SAlexander Motin 
1060901fadf7SArchie Cobbs 	/* Find next empty slot in transmit queue */
1061901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1062901fadf7SArchie Cobbs 	if (i == L2TP_MAX_XWIN) {
1063702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1064901fadf7SArchie Cobbs 		priv->stats.xmitDrops++;
1065901fadf7SArchie Cobbs 		m_freem(m);
1066bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1067901fadf7SArchie Cobbs 	}
1068901fadf7SArchie Cobbs 	seq->xwin[i] = m;
1069901fadf7SArchie Cobbs 
1070901fadf7SArchie Cobbs 	/* If peer's receive window is already full, nothing else to do */
1071702f9895SAlexander Motin 	if (i >= seq->cwnd) {
1072702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1073bf741e4dSAlexander Motin 		ERROUT(0);
1074702f9895SAlexander Motin 	}
1075901fadf7SArchie Cobbs 
1076901fadf7SArchie Cobbs 	/* Start retransmit timer if not already running */
1077206fa244SAlexander Motin 	if (!callout_active(&seq->rack_timer))
10780ef8db8fSGleb Smirnoff 		ng_callout(&seq->rack_timer, node, NULL,
10790ef8db8fSGleb Smirnoff 		    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1080901fadf7SArchie Cobbs 
1081702f9895SAlexander Motin 	ns = seq->ns++;
1082702f9895SAlexander Motin 
1083702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1084702f9895SAlexander Motin 
1085901fadf7SArchie Cobbs 	/* Copy packet */
1086702f9895SAlexander Motin 	if ((m = L2TP_COPY_MBUF(m, M_DONTWAIT)) == NULL) {
1087901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1088bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1089901fadf7SArchie Cobbs 	}
1090901fadf7SArchie Cobbs 
1091901fadf7SArchie Cobbs 	/* Send packet and increment xmit sequence number */
1092702f9895SAlexander Motin 	error = ng_l2tp_xmit_ctrl(priv, m, ns);
1093bf741e4dSAlexander Motin done:
1094bf741e4dSAlexander Motin 	/* Done */
1095bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1096bf741e4dSAlexander Motin 	return (error);
1097901fadf7SArchie Cobbs }
1098901fadf7SArchie Cobbs 
1099901fadf7SArchie Cobbs /*
1100901fadf7SArchie Cobbs  * Handle an outgoing data frame.
1101901fadf7SArchie Cobbs  */
1102901fadf7SArchie Cobbs static int
1103bf741e4dSAlexander Motin ng_l2tp_rcvdata(hook_p hook, item_p item)
1104901fadf7SArchie Cobbs {
1105bf741e4dSAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1106bf741e4dSAlexander Motin 	const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1107901fadf7SArchie Cobbs 	struct mbuf *m;
1108901fadf7SArchie Cobbs 	u_int16_t hdr;
1109901fadf7SArchie Cobbs 	int error;
1110901fadf7SArchie Cobbs 	int i = 1;
1111901fadf7SArchie Cobbs 
1112bf741e4dSAlexander Motin 	/* Sanity check */
1113bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1114bf741e4dSAlexander Motin 
1115bf741e4dSAlexander Motin 	/* If not configured, reject */
1116bf741e4dSAlexander Motin 	if (!priv->conf.enabled) {
1117bf741e4dSAlexander Motin 		NG_FREE_ITEM(item);
1118bf741e4dSAlexander Motin 		ERROUT(ENXIO);
1119bf741e4dSAlexander Motin 	}
1120bf741e4dSAlexander Motin 
1121901fadf7SArchie Cobbs 	/* Get mbuf */
1122901fadf7SArchie Cobbs 	NGI_GET_M(item, m);
1123901fadf7SArchie Cobbs 
1124901fadf7SArchie Cobbs 	/* Check max length */
1125901fadf7SArchie Cobbs 	if (m->m_pkthdr.len >= 0x10000 - 12) {
1126901fadf7SArchie Cobbs 		priv->stats.xmitDataTooBig++;
1127901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1128901fadf7SArchie Cobbs 		NG_FREE_M(m);
1129bf741e4dSAlexander Motin 		ERROUT(EOVERFLOW);
1130901fadf7SArchie Cobbs 	}
1131901fadf7SArchie Cobbs 
1132901fadf7SArchie Cobbs 	/* Prepend L2TP header */
1133901fadf7SArchie Cobbs 	M_PREPEND(m, 6
1134901fadf7SArchie Cobbs 	    + (2 * (hpriv->conf.include_length != 0))
1135901fadf7SArchie Cobbs 	    + (4 * (hpriv->conf.enable_dseq != 0)),
1136a163d034SWarner Losh 	    M_DONTWAIT);
1137901fadf7SArchie Cobbs 	if (m == NULL) {
1138901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1139901fadf7SArchie Cobbs 		NG_FREE_ITEM(item);
1140bf741e4dSAlexander Motin 		ERROUT(ENOBUFS);
1141901fadf7SArchie Cobbs 	}
1142901fadf7SArchie Cobbs 	hdr = L2TP_DATA_HDR;
1143901fadf7SArchie Cobbs 	if (hpriv->conf.include_length) {
1144901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_LEN;
1145901fadf7SArchie Cobbs 		mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
1146901fadf7SArchie Cobbs 	}
1147901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
1148901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
1149901fadf7SArchie Cobbs 	if (hpriv->conf.enable_dseq) {
1150901fadf7SArchie Cobbs 		hdr |= L2TP_HDR_SEQ;
1151901fadf7SArchie Cobbs 		mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
1152901fadf7SArchie Cobbs 		mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
1153901fadf7SArchie Cobbs 		hpriv->ns++;
1154901fadf7SArchie Cobbs 	}
1155901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[0] = htons(hdr);
1156901fadf7SArchie Cobbs 
11574807330cSBjoern A. Zeeb 	/* Update per session stats. */
11584807330cSBjoern A. Zeeb 	hpriv->stats.xmitPackets++;
11594807330cSBjoern A. Zeeb 	hpriv->stats.xmitOctets += m->m_pkthdr.len;
11604807330cSBjoern A. Zeeb 
116134d16c64SAlexander Motin 	/* And the global one. */
116234d16c64SAlexander Motin 	priv->stats.xmitPackets++;
116334d16c64SAlexander Motin 	priv->stats.xmitOctets += m->m_pkthdr.len;
116434d16c64SAlexander Motin 
1165901fadf7SArchie Cobbs 	/* Send packet */
1166901fadf7SArchie Cobbs 	NG_FWD_NEW_DATA(error, item, priv->lower, m);
1167bf741e4dSAlexander Motin done:
1168bf741e4dSAlexander Motin 	/* Done */
1169bf741e4dSAlexander Motin 	L2TP_SEQ_CHECK(&priv->seq);
1170901fadf7SArchie Cobbs 	return (error);
1171901fadf7SArchie Cobbs }
1172901fadf7SArchie Cobbs 
1173901fadf7SArchie Cobbs /*
1174901fadf7SArchie Cobbs  * Send a message to our controlling node that we've failed.
1175901fadf7SArchie Cobbs  */
1176901fadf7SArchie Cobbs static void
1177901fadf7SArchie Cobbs ng_l2tp_seq_failure(priv_p priv)
1178901fadf7SArchie Cobbs {
1179901fadf7SArchie Cobbs 	struct ng_mesg *msg;
1180901fadf7SArchie Cobbs 	int error;
1181901fadf7SArchie Cobbs 
1182901fadf7SArchie Cobbs 	NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1183901fadf7SArchie Cobbs 	if (msg == NULL)
1184901fadf7SArchie Cobbs 		return;
1185facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1186901fadf7SArchie Cobbs }
1187901fadf7SArchie Cobbs 
1188901fadf7SArchie Cobbs /************************************************************************
1189901fadf7SArchie Cobbs 			SEQUENCE NUMBER HANDLING
1190901fadf7SArchie Cobbs ************************************************************************/
1191901fadf7SArchie Cobbs 
1192901fadf7SArchie Cobbs /*
1193901fadf7SArchie Cobbs  * Initialize sequence number state.
1194901fadf7SArchie Cobbs  */
1195901fadf7SArchie Cobbs static void
1196901fadf7SArchie Cobbs ng_l2tp_seq_init(priv_p priv)
1197901fadf7SArchie Cobbs {
1198901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1199901fadf7SArchie Cobbs 
1200901fadf7SArchie Cobbs 	KASSERT(priv->conf.peer_win >= 1,
12017e2b43eeSDavid E. O'Brien 	    ("%s: peer_win is zero", __func__));
1202901fadf7SArchie Cobbs 	memset(seq, 0, sizeof(*seq));
1203901fadf7SArchie Cobbs 	seq->cwnd = 1;
1204901fadf7SArchie Cobbs 	seq->wmax = priv->conf.peer_win;
1205901fadf7SArchie Cobbs 	if (seq->wmax > L2TP_MAX_XWIN)
1206901fadf7SArchie Cobbs 		seq->wmax = L2TP_MAX_XWIN;
1207901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
12080ef8db8fSGleb Smirnoff 	ng_callout_init(&seq->rack_timer);
12090ef8db8fSGleb Smirnoff 	ng_callout_init(&seq->xack_timer);
1210702f9895SAlexander Motin 	mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1211901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1212901fadf7SArchie Cobbs }
1213901fadf7SArchie Cobbs 
1214901fadf7SArchie Cobbs /*
12151e031324SBjoern A. Zeeb  * Set sequence number state as given from user.
12161e031324SBjoern A. Zeeb  */
12171e031324SBjoern A. Zeeb static int
12181e031324SBjoern A. Zeeb ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
12191e031324SBjoern A. Zeeb {
12201e031324SBjoern A. Zeeb 	struct l2tp_seq *const seq = &priv->seq;
12211e031324SBjoern A. Zeeb 
12221e031324SBjoern A. Zeeb 	/* If node is enabled, deny update to sequence numbers. */
12231e031324SBjoern A. Zeeb 	if (priv->conf.enabled)
12241e031324SBjoern A. Zeeb 		return (EBUSY);
12251e031324SBjoern A. Zeeb 
12261e031324SBjoern A. Zeeb 	/* We only can handle the simple cases. */
12271e031324SBjoern A. Zeeb 	if (conf->xack != conf->nr || conf->ns != conf->rack)
12281e031324SBjoern A. Zeeb 		return (EINVAL);
12291e031324SBjoern A. Zeeb 
12301e031324SBjoern A. Zeeb 	/* Set ns,nr,rack,xack parameters. */
12311e031324SBjoern A. Zeeb 	seq->ns = conf->ns;
12321e031324SBjoern A. Zeeb 	seq->nr = conf->nr;
12331e031324SBjoern A. Zeeb 	seq->rack = conf->rack;
12341e031324SBjoern A. Zeeb 	seq->xack = conf->xack;
12351e031324SBjoern A. Zeeb 
12361e031324SBjoern A. Zeeb 	return (0);
12371e031324SBjoern A. Zeeb }
12381e031324SBjoern A. Zeeb 
12391e031324SBjoern A. Zeeb /*
1240901fadf7SArchie Cobbs  * Adjust sequence number state accordingly after reconfiguration.
1241901fadf7SArchie Cobbs  */
1242901fadf7SArchie Cobbs static int
1243901fadf7SArchie Cobbs ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1244901fadf7SArchie Cobbs {
1245901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1246901fadf7SArchie Cobbs 	u_int16_t new_wmax;
1247901fadf7SArchie Cobbs 
1248901fadf7SArchie Cobbs 	/* If disabling node, reset state sequence number */
1249901fadf7SArchie Cobbs 	if (!conf->enabled) {
1250901fadf7SArchie Cobbs 		ng_l2tp_seq_reset(priv);
1251901fadf7SArchie Cobbs 		return (0);
1252901fadf7SArchie Cobbs 	}
1253901fadf7SArchie Cobbs 
1254901fadf7SArchie Cobbs 	/* Adjust peer's max recv window; it can only increase */
1255901fadf7SArchie Cobbs 	new_wmax = conf->peer_win;
1256901fadf7SArchie Cobbs 	if (new_wmax > L2TP_MAX_XWIN)
1257901fadf7SArchie Cobbs 		new_wmax = L2TP_MAX_XWIN;
1258901fadf7SArchie Cobbs 	if (new_wmax == 0)
1259901fadf7SArchie Cobbs 		return (EINVAL);
1260901fadf7SArchie Cobbs 	if (new_wmax < seq->wmax)
1261901fadf7SArchie Cobbs 		return (EBUSY);
1262901fadf7SArchie Cobbs 	seq->wmax = new_wmax;
1263901fadf7SArchie Cobbs 
1264901fadf7SArchie Cobbs 	/* Done */
1265901fadf7SArchie Cobbs 	return (0);
1266901fadf7SArchie Cobbs }
1267901fadf7SArchie Cobbs 
1268901fadf7SArchie Cobbs /*
1269901fadf7SArchie Cobbs  * Reset sequence number state.
1270901fadf7SArchie Cobbs  */
1271901fadf7SArchie Cobbs static void
1272901fadf7SArchie Cobbs ng_l2tp_seq_reset(priv_p priv)
1273901fadf7SArchie Cobbs {
1274901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1275901fadf7SArchie Cobbs 	hook_p hook;
1276901fadf7SArchie Cobbs 	int i;
1277901fadf7SArchie Cobbs 
1278901fadf7SArchie Cobbs 	/* Sanity check */
1279901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1280901fadf7SArchie Cobbs 
1281901fadf7SArchie Cobbs 	/* Stop timers */
12820ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->rack_timer, priv->node);
12830ef8db8fSGleb Smirnoff 	ng_uncallout(&seq->xack_timer, priv->node);
1284901fadf7SArchie Cobbs 
1285901fadf7SArchie Cobbs 	/* Free retransmit queue */
1286901fadf7SArchie Cobbs 	for (i = 0; i < L2TP_MAX_XWIN; i++) {
1287901fadf7SArchie Cobbs 		if (seq->xwin[i] == NULL)
1288901fadf7SArchie Cobbs 			break;
1289901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1290901fadf7SArchie Cobbs 	}
1291901fadf7SArchie Cobbs 
1292901fadf7SArchie Cobbs 	/* Reset session hooks' sequence number states */
1293901fadf7SArchie Cobbs 	NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1294901fadf7SArchie Cobbs 
1295901fadf7SArchie Cobbs 	/* Reset node's sequence number state */
1296702f9895SAlexander Motin 	seq->ns = 0;
1297702f9895SAlexander Motin 	seq->nr = 0;
1298702f9895SAlexander Motin 	seq->rack = 0;
1299702f9895SAlexander Motin 	seq->xack = 0;
1300901fadf7SArchie Cobbs 	seq->wmax = L2TP_MAX_XWIN;
1301702f9895SAlexander Motin 	seq->cwnd = 1;
1302901fadf7SArchie Cobbs 	seq->ssth = seq->wmax;
1303702f9895SAlexander Motin 	seq->acks = 0;
1304702f9895SAlexander Motin 	seq->rexmits = 0;
1305702f9895SAlexander Motin 	bzero(seq->xwin, sizeof(seq->xwin));
1306901fadf7SArchie Cobbs 
1307901fadf7SArchie Cobbs 	/* Done */
1308901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1309901fadf7SArchie Cobbs }
1310901fadf7SArchie Cobbs 
1311901fadf7SArchie Cobbs /*
1312901fadf7SArchie Cobbs  * Handle receipt of an acknowledgement value (Nr) from peer.
1313901fadf7SArchie Cobbs  */
1314901fadf7SArchie Cobbs static void
1315901fadf7SArchie Cobbs ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1316901fadf7SArchie Cobbs {
1317901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1318702f9895SAlexander Motin 	struct mbuf	*xwin[L2TP_MAX_XWIN];	/* partial local copy */
1319901fadf7SArchie Cobbs 	int		nack;
1320702f9895SAlexander Motin 	int		i, j;
1321702f9895SAlexander Motin 	uint16_t	ns;
1322702f9895SAlexander Motin 
1323702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1324901fadf7SArchie Cobbs 
1325901fadf7SArchie Cobbs 	/* Verify peer's ACK is in range */
1326702f9895SAlexander Motin 	if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
1327702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1328901fadf7SArchie Cobbs 		return;				/* duplicate ack */
1329702f9895SAlexander Motin 	}
1330901fadf7SArchie Cobbs 	if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1331702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1332901fadf7SArchie Cobbs 		priv->stats.recvBadAcks++;	/* ack for packet not sent */
1333901fadf7SArchie Cobbs 		return;
1334901fadf7SArchie Cobbs 	}
1335901fadf7SArchie Cobbs 	KASSERT(nack <= L2TP_MAX_XWIN,
13367e2b43eeSDavid E. O'Brien 	    ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1337901fadf7SArchie Cobbs 
1338901fadf7SArchie Cobbs 	/* Update receive ack stats */
1339901fadf7SArchie Cobbs 	seq->rack = nr;
1340901fadf7SArchie Cobbs 	seq->rexmits = 0;
1341901fadf7SArchie Cobbs 
1342901fadf7SArchie Cobbs 	/* Free acknowledged packets and shift up packets in the xmit queue */
1343901fadf7SArchie Cobbs 	for (i = 0; i < nack; i++)
1344901fadf7SArchie Cobbs 		m_freem(seq->xwin[i]);
1345901fadf7SArchie Cobbs 	memmove(seq->xwin, seq->xwin + nack,
1346901fadf7SArchie Cobbs 	    (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1347901fadf7SArchie Cobbs 	memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1348901fadf7SArchie Cobbs 	    nack * sizeof(*seq->xwin));
1349901fadf7SArchie Cobbs 
1350901fadf7SArchie Cobbs 	/*
1351901fadf7SArchie Cobbs 	 * Do slow-start/congestion avoidance windowing algorithm described
1352901fadf7SArchie Cobbs 	 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1353901fadf7SArchie Cobbs 	 * ACK had arrived separately.
1354901fadf7SArchie Cobbs 	 */
1355901fadf7SArchie Cobbs 	if (seq->cwnd < seq->wmax) {
1356901fadf7SArchie Cobbs 
1357901fadf7SArchie Cobbs 		/* Handle slow start phase */
1358901fadf7SArchie Cobbs 		if (seq->cwnd < seq->ssth) {
1359901fadf7SArchie Cobbs 			seq->cwnd += nack;
1360901fadf7SArchie Cobbs 			nack = 0;
1361901fadf7SArchie Cobbs 			if (seq->cwnd > seq->ssth) {	/* into cg.av. phase */
1362901fadf7SArchie Cobbs 				nack = seq->cwnd - seq->ssth;
1363901fadf7SArchie Cobbs 				seq->cwnd = seq->ssth;
1364901fadf7SArchie Cobbs 			}
1365901fadf7SArchie Cobbs 		}
1366901fadf7SArchie Cobbs 
1367901fadf7SArchie Cobbs 		/* Handle congestion avoidance phase */
1368901fadf7SArchie Cobbs 		if (seq->cwnd >= seq->ssth) {
1369901fadf7SArchie Cobbs 			seq->acks += nack;
1370901fadf7SArchie Cobbs 			while (seq->acks >= seq->cwnd) {
1371901fadf7SArchie Cobbs 				seq->acks -= seq->cwnd;
1372901fadf7SArchie Cobbs 				if (seq->cwnd < seq->wmax)
1373901fadf7SArchie Cobbs 					seq->cwnd++;
1374901fadf7SArchie Cobbs 			}
1375901fadf7SArchie Cobbs 		}
1376901fadf7SArchie Cobbs 	}
1377901fadf7SArchie Cobbs 
1378901fadf7SArchie Cobbs 	/* Stop xmit timer */
1379206fa244SAlexander Motin 	if (callout_active(&seq->rack_timer))
13800ef8db8fSGleb Smirnoff 		ng_uncallout(&seq->rack_timer, priv->node);
1381901fadf7SArchie Cobbs 
1382901fadf7SArchie Cobbs 	/* If transmit queue is empty, we're done for now */
1383702f9895SAlexander Motin 	if (seq->xwin[0] == NULL) {
1384702f9895SAlexander Motin 		mtx_unlock(&seq->mtx);
1385901fadf7SArchie Cobbs 		return;
1386702f9895SAlexander Motin 	}
1387901fadf7SArchie Cobbs 
1388901fadf7SArchie Cobbs 	/* Start restransmit timer again */
13890ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, priv->node, NULL,
13900ef8db8fSGleb Smirnoff 	    hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1391901fadf7SArchie Cobbs 
1392901fadf7SArchie Cobbs 	/*
1393901fadf7SArchie Cobbs 	 * Send more packets, trying to keep peer's receive window full.
1394702f9895SAlexander Motin 	 * Make copy of everything we need before lock release.
1395702f9895SAlexander Motin 	 */
1396702f9895SAlexander Motin 	ns = seq->ns;
1397702f9895SAlexander Motin 	j = 0;
1398702f9895SAlexander Motin 	while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1399702f9895SAlexander Motin 	    && seq->xwin[i] != NULL) {
1400702f9895SAlexander Motin 		xwin[j++] = seq->xwin[i];
1401702f9895SAlexander Motin 		seq->ns++;
1402702f9895SAlexander Motin 	}
1403702f9895SAlexander Motin 
1404702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1405702f9895SAlexander Motin 
1406702f9895SAlexander Motin 	/*
1407702f9895SAlexander Motin 	 * Send prepared.
1408901fadf7SArchie Cobbs 	 * If there is a memory error, pretend packet was sent, as it
1409901fadf7SArchie Cobbs 	 * will get retransmitted later anyway.
1410901fadf7SArchie Cobbs 	 */
1411702f9895SAlexander Motin 	for (i = 0; i < j; i++) {
1412702f9895SAlexander Motin 		struct mbuf 	*m;
1413702f9895SAlexander Motin 		if ((m = L2TP_COPY_MBUF(xwin[i], M_DONTWAIT)) == NULL)
1414901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1415901fadf7SArchie Cobbs 		else
1416702f9895SAlexander Motin 			ng_l2tp_xmit_ctrl(priv, m, ns);
1417702f9895SAlexander Motin 		ns++;
1418901fadf7SArchie Cobbs 	}
1419901fadf7SArchie Cobbs }
1420901fadf7SArchie Cobbs 
1421901fadf7SArchie Cobbs /*
1422901fadf7SArchie Cobbs  * Handle an ack timeout. We have an outstanding ack that we
1423901fadf7SArchie Cobbs  * were hoping to piggy-back, but haven't, so send a ZLB.
1424901fadf7SArchie Cobbs  */
1425901fadf7SArchie Cobbs static void
14260ef8db8fSGleb Smirnoff ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1427901fadf7SArchie Cobbs {
1428901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1429901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1430901fadf7SArchie Cobbs 
1431206fa244SAlexander Motin 	/* Make sure callout is still active before doing anything */
1432206fa244SAlexander Motin 	if (callout_pending(&seq->xack_timer) ||
1433206fa244SAlexander Motin 	    (!callout_active(&seq->xack_timer)))
1434206fa244SAlexander Motin 		return;
1435206fa244SAlexander Motin 
1436901fadf7SArchie Cobbs 	/* Sanity check */
1437901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1438901fadf7SArchie Cobbs 
1439206fa244SAlexander Motin 	/* Send a ZLB */
1440901fadf7SArchie Cobbs 	ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1441901fadf7SArchie Cobbs 
1442206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1443206fa244SAlexander Motin 	    as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1444206fa244SAlexander Motin 
1445206fa244SAlexander Motin 	/* Sanity check */
1446901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1447901fadf7SArchie Cobbs }
1448901fadf7SArchie Cobbs 
1449901fadf7SArchie Cobbs /*
1450901fadf7SArchie Cobbs  * Handle a transmit timeout. The peer has failed to respond
1451901fadf7SArchie Cobbs  * with an ack for our packet, so retransmit it.
1452901fadf7SArchie Cobbs  */
1453901fadf7SArchie Cobbs static void
14540ef8db8fSGleb Smirnoff ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1455901fadf7SArchie Cobbs {
1456901fadf7SArchie Cobbs 	const priv_p priv = NG_NODE_PRIVATE(node);
1457901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
1458901fadf7SArchie Cobbs 	struct mbuf *m;
1459901fadf7SArchie Cobbs 	u_int delay;
1460901fadf7SArchie Cobbs 
1461206fa244SAlexander Motin 	/* Make sure callout is still active before doing anything */
1462206fa244SAlexander Motin 	if (callout_pending(&seq->rack_timer) ||
1463206fa244SAlexander Motin 	    (!callout_active(&seq->rack_timer)))
1464206fa244SAlexander Motin 		return;
1465206fa244SAlexander Motin 
1466901fadf7SArchie Cobbs 	/* Sanity check */
1467901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1468901fadf7SArchie Cobbs 
1469901fadf7SArchie Cobbs 	priv->stats.xmitRetransmits++;
1470901fadf7SArchie Cobbs 
1471901fadf7SArchie Cobbs 	/* Have we reached the retransmit limit? If so, notify owner. */
147240097c5dSAlexander Motin 	if (seq->rexmits++ >= priv->conf.rexmit_max)
1473901fadf7SArchie Cobbs 		ng_l2tp_seq_failure(priv);
1474901fadf7SArchie Cobbs 
1475901fadf7SArchie Cobbs 	/* Restart timer, this time with an increased delay */
1476901fadf7SArchie Cobbs 	delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
147740097c5dSAlexander Motin 	if (delay > priv->conf.rexmit_max_to)
147840097c5dSAlexander Motin 		delay = priv->conf.rexmit_max_to;
14790ef8db8fSGleb Smirnoff 	ng_callout(&seq->rack_timer, node, NULL,
14800ef8db8fSGleb Smirnoff 	    hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1481901fadf7SArchie Cobbs 
1482901fadf7SArchie Cobbs 	/* Do slow-start/congestion algorithm windowing algorithm */
1483af63939cSAlexander Motin 	seq->ns = seq->rack;
1484901fadf7SArchie Cobbs 	seq->ssth = (seq->cwnd + 1) / 2;
1485901fadf7SArchie Cobbs 	seq->cwnd = 1;
1486901fadf7SArchie Cobbs 	seq->acks = 0;
1487901fadf7SArchie Cobbs 
1488901fadf7SArchie Cobbs 	/* Retransmit oldest unack'd packet */
1489a163d034SWarner Losh 	if ((m = L2TP_COPY_MBUF(seq->xwin[0], M_DONTWAIT)) == NULL)
1490901fadf7SArchie Cobbs 		priv->stats.memoryFailures++;
1491901fadf7SArchie Cobbs 	else
1492af63939cSAlexander Motin 		ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1493901fadf7SArchie Cobbs 
1494206fa244SAlexander Motin 	/* callout_deactivate() is not needed here
1495206fa244SAlexander Motin 	    as ng_callout() is getting called each time */
1496206fa244SAlexander Motin 
1497206fa244SAlexander Motin 	/* Sanity check */
1498901fadf7SArchie Cobbs 	L2TP_SEQ_CHECK(seq);
1499901fadf7SArchie Cobbs }
1500901fadf7SArchie Cobbs 
1501901fadf7SArchie Cobbs /*
1502901fadf7SArchie Cobbs  * Transmit a control stream packet, payload optional.
1503901fadf7SArchie Cobbs  * The transmit sequence number is not incremented.
1504901fadf7SArchie Cobbs  */
1505901fadf7SArchie Cobbs static int
1506901fadf7SArchie Cobbs ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1507901fadf7SArchie Cobbs {
1508901fadf7SArchie Cobbs 	struct l2tp_seq *const seq = &priv->seq;
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 */
1527a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, 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 		}
1542901fadf7SArchie Cobbs 		memcpy(&session_id, mtod(m, u_int16_t *), 2);
1543901fadf7SArchie Cobbs 		m_adj(m, 2);
1544901fadf7SArchie Cobbs 
1545901fadf7SArchie Cobbs 		/* Make room for L2TP header */
1546a163d034SWarner Losh 		M_PREPEND(m, 12, M_DONTWAIT);
1547901fadf7SArchie Cobbs 		if (m == NULL) {
1548901fadf7SArchie Cobbs 			priv->stats.memoryFailures++;
1549901fadf7SArchie Cobbs 			return (ENOBUFS);
1550901fadf7SArchie Cobbs 		}
1551901fadf7SArchie Cobbs 	}
1552901fadf7SArchie Cobbs 
1553901fadf7SArchie Cobbs 	/* Fill in L2TP header */
1554901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
1555901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
1556901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
1557901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[3] = session_id;
1558901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[4] = htons(ns);
1559901fadf7SArchie Cobbs 	mtod(m, u_int16_t *)[5] = htons(seq->nr);
1560901fadf7SArchie Cobbs 
1561901fadf7SArchie Cobbs 	/* Update sequence number info and stats */
1562901fadf7SArchie Cobbs 	priv->stats.xmitPackets++;
1563901fadf7SArchie Cobbs 	priv->stats.xmitOctets += m->m_pkthdr.len;
1564901fadf7SArchie Cobbs 
1565901fadf7SArchie Cobbs 	/* Send packet */
15663ca24c28SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->lower, m);
1567901fadf7SArchie Cobbs 	return (error);
1568901fadf7SArchie Cobbs }
1569901fadf7SArchie Cobbs 
1570901fadf7SArchie Cobbs #ifdef INVARIANTS
1571901fadf7SArchie Cobbs /*
1572901fadf7SArchie Cobbs  * Sanity check sequence number state.
1573901fadf7SArchie Cobbs  */
1574901fadf7SArchie Cobbs static void
1575901fadf7SArchie Cobbs ng_l2tp_seq_check(struct l2tp_seq *seq)
1576901fadf7SArchie Cobbs {
1577702f9895SAlexander Motin 	int self_unack, peer_unack;
1578901fadf7SArchie Cobbs 	int i;
1579901fadf7SArchie Cobbs 
15807e2b43eeSDavid E. O'Brien #define CHECK(p)	KASSERT((p), ("%s: not: %s", __func__, #p))
1581901fadf7SArchie Cobbs 
1582702f9895SAlexander Motin 	mtx_lock(&seq->mtx);
1583702f9895SAlexander Motin 
1584702f9895SAlexander Motin 	self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1585702f9895SAlexander Motin 	peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1586901fadf7SArchie Cobbs 	CHECK(seq->wmax <= L2TP_MAX_XWIN);
1587901fadf7SArchie Cobbs 	CHECK(seq->cwnd >= 1);
1588901fadf7SArchie Cobbs 	CHECK(seq->cwnd <= seq->wmax);
1589901fadf7SArchie Cobbs 	CHECK(seq->ssth >= 1);
1590901fadf7SArchie Cobbs 	CHECK(seq->ssth <= seq->wmax);
1591901fadf7SArchie Cobbs 	if (seq->cwnd < seq->ssth)
1592901fadf7SArchie Cobbs 		CHECK(seq->acks == 0);
1593901fadf7SArchie Cobbs 	else
1594901fadf7SArchie Cobbs 		CHECK(seq->acks <= seq->cwnd);
1595901fadf7SArchie Cobbs 	CHECK(self_unack >= 0);
1596901fadf7SArchie Cobbs 	CHECK(peer_unack >= 0);
1597901fadf7SArchie Cobbs 	CHECK(peer_unack <= seq->wmax);
1598206fa244SAlexander Motin 	CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1599206fa244SAlexander Motin 	CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1600901fadf7SArchie Cobbs 	for (i = 0; i < peer_unack; i++)
1601901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] != NULL);
1602901fadf7SArchie Cobbs 	for ( ; i < seq->cwnd; i++)	    /* verify peer's recv window full */
1603901fadf7SArchie Cobbs 		CHECK(seq->xwin[i] == NULL);
1604901fadf7SArchie Cobbs 
1605702f9895SAlexander Motin 	mtx_unlock(&seq->mtx);
1606702f9895SAlexander Motin 
1607901fadf7SArchie Cobbs #undef CHECK
1608901fadf7SArchie Cobbs }
1609901fadf7SArchie Cobbs #endif	/* INVARIANTS */
1610