158b02238SNuno Antunes /*-
258b02238SNuno Antunes * Copyright (c) 2001-2002 Packet Design, LLC.
358b02238SNuno Antunes * All rights reserved.
458b02238SNuno Antunes *
558b02238SNuno Antunes * Subject to the following obligations and disclaimer of warranty,
658b02238SNuno Antunes * use and redistribution of this software, in source or object code
758b02238SNuno Antunes * forms, with or without modifications are expressly permitted by
858b02238SNuno Antunes * Packet Design; provided, however, that:
958b02238SNuno Antunes *
1058b02238SNuno Antunes * (i) Any and all reproductions of the source or object code
1158b02238SNuno Antunes * must include the copyright notice above and the following
1258b02238SNuno Antunes * disclaimer of warranties; and
1358b02238SNuno Antunes * (ii) No rights are granted, in any manner or form, to use
1458b02238SNuno Antunes * Packet Design trademarks, including the mark "PACKET DESIGN"
1558b02238SNuno Antunes * on advertising, endorsements, or otherwise except as such
1658b02238SNuno Antunes * appears in the above copyright notice or in the software.
1758b02238SNuno Antunes *
1858b02238SNuno Antunes * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
1958b02238SNuno Antunes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
2058b02238SNuno Antunes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
2158b02238SNuno Antunes * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
2258b02238SNuno Antunes * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
2358b02238SNuno Antunes * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
2458b02238SNuno Antunes * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
2558b02238SNuno Antunes * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
2658b02238SNuno Antunes * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
2758b02238SNuno Antunes * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
2858b02238SNuno Antunes * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
2958b02238SNuno Antunes * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
3058b02238SNuno Antunes * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
3158b02238SNuno Antunes * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
3258b02238SNuno Antunes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3358b02238SNuno Antunes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3458b02238SNuno Antunes * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
3558b02238SNuno Antunes * THE POSSIBILITY OF SUCH DAMAGE.
3658b02238SNuno Antunes *
3758b02238SNuno Antunes * Author: Archie Cobbs <archie@freebsd.org>
3858b02238SNuno Antunes *
3958b02238SNuno Antunes * $FreeBSD: src/sys/netgraph/ng_l2tp.c,v 1.25 2008/03/16 21:33:12 mav Exp $
4058b02238SNuno Antunes */
4158b02238SNuno Antunes
4258b02238SNuno Antunes /*
4358b02238SNuno Antunes * L2TP netgraph node type.
4458b02238SNuno Antunes *
4558b02238SNuno Antunes * This node type implements the lower layer of the
4658b02238SNuno Antunes * L2TP protocol as specified in RFC 2661.
4758b02238SNuno Antunes */
4858b02238SNuno Antunes
4958b02238SNuno Antunes #include <sys/param.h>
5058b02238SNuno Antunes #include <sys/systm.h>
5158b02238SNuno Antunes #include <sys/kernel.h>
5258b02238SNuno Antunes #include <sys/time.h>
5358b02238SNuno Antunes #include <sys/conf.h>
5458b02238SNuno Antunes #include <sys/mbuf.h>
5558b02238SNuno Antunes #include <sys/malloc.h>
5658b02238SNuno Antunes #include <sys/errno.h>
5758b02238SNuno Antunes #include <sys/libkern.h>
5858b02238SNuno Antunes
5958b02238SNuno Antunes #include <netgraph7/ng_message.h>
6058b02238SNuno Antunes #include <netgraph7/netgraph.h>
6158b02238SNuno Antunes #include <netgraph7/ng_parse.h>
6258b02238SNuno Antunes #include "ng_l2tp.h"
6358b02238SNuno Antunes
6458b02238SNuno Antunes #ifdef NG_SEPARATE_MALLOC
6558b02238SNuno Antunes MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
6658b02238SNuno Antunes #else
6758b02238SNuno Antunes #define M_NETGRAPH_L2TP M_NETGRAPH
6858b02238SNuno Antunes #endif
6958b02238SNuno Antunes
7058b02238SNuno Antunes /* L2TP header format (first 2 bytes only) */
7158b02238SNuno Antunes #define L2TP_HDR_CTRL 0x8000 /* control packet */
7258b02238SNuno Antunes #define L2TP_HDR_LEN 0x4000 /* has length field */
7358b02238SNuno Antunes #define L2TP_HDR_SEQ 0x0800 /* has ns, nr fields */
7458b02238SNuno Antunes #define L2TP_HDR_OFF 0x0200 /* has offset field */
7558b02238SNuno Antunes #define L2TP_HDR_PRIO 0x0100 /* give priority */
7658b02238SNuno Antunes #define L2TP_HDR_VERS_MASK 0x000f /* version field mask */
7758b02238SNuno Antunes #define L2TP_HDR_VERSION 0x0002 /* version field */
7858b02238SNuno Antunes
7958b02238SNuno Antunes /* Bits that must be zero or one in first two bytes of header */
8058b02238SNuno Antunes #define L2TP_CTRL_0BITS 0x030d /* ctrl: must be 0 */
8158b02238SNuno Antunes #define L2TP_CTRL_1BITS 0xc802 /* ctrl: must be 1 */
8258b02238SNuno Antunes #define L2TP_DATA_0BITS 0x800d /* data: must be 0 */
8358b02238SNuno Antunes #define L2TP_DATA_1BITS 0x0002 /* data: must be 1 */
8458b02238SNuno Antunes
8558b02238SNuno Antunes /* Standard xmit ctrl and data header bits */
8658b02238SNuno Antunes #define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
8758b02238SNuno Antunes | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
8858b02238SNuno Antunes #define L2TP_DATA_HDR (L2TP_HDR_VERSION) /* optional: len, seq */
8958b02238SNuno Antunes
9058b02238SNuno Antunes /* Some hard coded values */
9158b02238SNuno Antunes #define L2TP_MAX_XWIN 128 /* my max xmit window */
9258b02238SNuno Antunes #define L2TP_MAX_REXMIT 5 /* default max rexmit */
9358b02238SNuno Antunes #define L2TP_MAX_REXMIT_TO 30 /* default rexmit to */
9458b02238SNuno Antunes #define L2TP_DELAYED_ACK ((hz + 19) / 20) /* delayed ack: 50 ms */
9558b02238SNuno Antunes
9658b02238SNuno Antunes /* Default data sequence number configuration for new sessions */
9758b02238SNuno Antunes #define L2TP_CONTROL_DSEQ 1 /* we are the lns */
9858b02238SNuno Antunes #define L2TP_ENABLE_DSEQ 1 /* enable data seq # */
9958b02238SNuno Antunes
10058b02238SNuno Antunes /* Compare sequence numbers using circular math */
10158b02238SNuno Antunes #define L2TP_SEQ_DIFF(x, y) ((int)((int16_t)(x) - (int16_t)(y)))
10258b02238SNuno Antunes
10358b02238SNuno Antunes #define SESSHASHSIZE 0x0020
10458b02238SNuno Antunes #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
10558b02238SNuno Antunes
10658b02238SNuno Antunes /* Hook private data (data session hooks only) */
10758b02238SNuno Antunes struct ng_l2tp_hook_private {
10858b02238SNuno Antunes struct ng_l2tp_sess_config conf; /* hook/session config */
10958b02238SNuno Antunes struct ng_l2tp_session_stats stats; /* per sessions statistics */
11058b02238SNuno Antunes hook_p hook; /* hook reference */
11158b02238SNuno Antunes u_int16_t ns; /* data ns sequence number */
11258b02238SNuno Antunes u_int16_t nr; /* data nr sequence number */
11358b02238SNuno Antunes LIST_ENTRY(ng_l2tp_hook_private) sessions;
11458b02238SNuno Antunes };
11558b02238SNuno Antunes typedef struct ng_l2tp_hook_private *hookpriv_p;
11658b02238SNuno Antunes
11758b02238SNuno Antunes /*
11858b02238SNuno Antunes * Sequence number state
11958b02238SNuno Antunes *
12058b02238SNuno Antunes * Invariants:
12158b02238SNuno Antunes * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
12258b02238SNuno Antunes * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
12358b02238SNuno Antunes * - The first (ns - rack) mbuf's in xwin[] array are copies of these
12458b02238SNuno Antunes * unacknowledged packets; the remainder of xwin[] consists first of
12558b02238SNuno Antunes * zero or more further untransmitted packets in the transmit queue
12658b02238SNuno Antunes * - We try to keep the peer's receive window as full as possible.
12758b02238SNuno Antunes * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
12858b02238SNuno Antunes * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
12958b02238SNuno Antunes * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
13058b02238SNuno Antunes * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
13158b02238SNuno Antunes */
13258b02238SNuno Antunes struct l2tp_seq {
13358b02238SNuno Antunes u_int16_t ns; /* next xmit seq we send */
13458b02238SNuno Antunes u_int16_t nr; /* next recv seq we expect */
13558b02238SNuno Antunes u_int16_t inproc; /* packet is in processing */
13658b02238SNuno Antunes u_int16_t rack; /* last 'nr' we rec'd */
13758b02238SNuno Antunes u_int16_t xack; /* last 'nr' we sent */
13858b02238SNuno Antunes u_int16_t wmax; /* peer's max recv window */
13958b02238SNuno Antunes u_int16_t cwnd; /* current congestion window */
14058b02238SNuno Antunes u_int16_t ssth; /* slow start threshold */
14158b02238SNuno Antunes u_int16_t acks; /* # consecutive acks rec'd */
14258b02238SNuno Antunes u_int16_t rexmits; /* # retransmits sent */
14358b02238SNuno Antunes struct callout rack_timer; /* retransmit timer */
14458b02238SNuno Antunes struct callout xack_timer; /* delayed ack timer */
14558b02238SNuno Antunes struct mbuf *xwin[L2TP_MAX_XWIN]; /* transmit window */
14658b02238SNuno Antunes struct mtx mtx; /* seq mutex */
14758b02238SNuno Antunes };
14858b02238SNuno Antunes
14958b02238SNuno Antunes /* Node private data */
15058b02238SNuno Antunes struct ng_l2tp_private {
15158b02238SNuno Antunes node_p node; /* back pointer to node */
15258b02238SNuno Antunes hook_p ctrl; /* hook to upper layers */
15358b02238SNuno Antunes hook_p lower; /* hook to lower layers */
15458b02238SNuno Antunes struct ng_l2tp_config conf; /* node configuration */
15558b02238SNuno Antunes struct ng_l2tp_stats stats; /* node statistics */
15658b02238SNuno Antunes struct l2tp_seq seq; /* ctrl sequence number state */
15758b02238SNuno Antunes ng_ID_t ftarget; /* failure message target */
15858b02238SNuno Antunes LIST_HEAD(, ng_l2tp_hook_private) sesshash[SESSHASHSIZE];
15958b02238SNuno Antunes };
16058b02238SNuno Antunes typedef struct ng_l2tp_private *priv_p;
16158b02238SNuno Antunes
16258b02238SNuno Antunes /* Netgraph node methods */
16358b02238SNuno Antunes static ng_constructor_t ng_l2tp_constructor;
16458b02238SNuno Antunes static ng_rcvmsg_t ng_l2tp_rcvmsg;
16558b02238SNuno Antunes static ng_shutdown_t ng_l2tp_shutdown;
16658b02238SNuno Antunes static ng_newhook_t ng_l2tp_newhook;
16758b02238SNuno Antunes static ng_rcvdata_t ng_l2tp_rcvdata;
16858b02238SNuno Antunes static ng_rcvdata_t ng_l2tp_rcvdata_lower;
16958b02238SNuno Antunes static ng_rcvdata_t ng_l2tp_rcvdata_ctrl;
17058b02238SNuno Antunes static ng_disconnect_t ng_l2tp_disconnect;
17158b02238SNuno Antunes
17258b02238SNuno Antunes /* Internal functions */
17358b02238SNuno Antunes static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
17458b02238SNuno Antunes
17558b02238SNuno Antunes static void ng_l2tp_seq_init(priv_p priv);
17658b02238SNuno Antunes static int ng_l2tp_seq_set(priv_p priv,
17758b02238SNuno Antunes const struct ng_l2tp_seq_config *conf);
17858b02238SNuno Antunes static int ng_l2tp_seq_adjust(priv_p priv,
17958b02238SNuno Antunes const struct ng_l2tp_config *conf);
18058b02238SNuno Antunes static void ng_l2tp_seq_reset(priv_p priv);
18158b02238SNuno Antunes static void ng_l2tp_seq_failure(priv_p priv);
18258b02238SNuno Antunes static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
18358b02238SNuno Antunes static void ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
18458b02238SNuno Antunes void *arg1, int arg2);
18558b02238SNuno Antunes static void ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
18658b02238SNuno Antunes void *arg1, int arg2);
18758b02238SNuno Antunes
18858b02238SNuno Antunes static hookpriv_p ng_l2tp_find_session(priv_p privp, u_int16_t sid);
18958b02238SNuno Antunes static ng_fn_eachhook ng_l2tp_reset_session;
19058b02238SNuno Antunes
19158b02238SNuno Antunes #ifdef INVARIANTS
19258b02238SNuno Antunes static void ng_l2tp_seq_check(struct l2tp_seq *seq);
19358b02238SNuno Antunes #endif
19458b02238SNuno Antunes
19558b02238SNuno Antunes /* Parse type for struct ng_l2tp_seq_config. */
19658b02238SNuno Antunes static const struct ng_parse_struct_field
19758b02238SNuno Antunes ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
19858b02238SNuno Antunes static const struct ng_parse_type ng_l2tp_seq_config_type = {
19958b02238SNuno Antunes &ng_parse_struct_type,
20058b02238SNuno Antunes &ng_l2tp_seq_config_fields
20158b02238SNuno Antunes };
20258b02238SNuno Antunes
20358b02238SNuno Antunes /* Parse type for struct ng_l2tp_config */
20458b02238SNuno Antunes static const struct ng_parse_struct_field
20558b02238SNuno Antunes ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
20658b02238SNuno Antunes static const struct ng_parse_type ng_l2tp_config_type = {
20758b02238SNuno Antunes &ng_parse_struct_type,
20858b02238SNuno Antunes &ng_l2tp_config_type_fields,
20958b02238SNuno Antunes };
21058b02238SNuno Antunes
21158b02238SNuno Antunes /* Parse type for struct ng_l2tp_sess_config */
21258b02238SNuno Antunes static const struct ng_parse_struct_field
21358b02238SNuno Antunes ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
21458b02238SNuno Antunes static const struct ng_parse_type ng_l2tp_sess_config_type = {
21558b02238SNuno Antunes &ng_parse_struct_type,
21658b02238SNuno Antunes &ng_l2tp_sess_config_type_fields,
21758b02238SNuno Antunes };
21858b02238SNuno Antunes
21958b02238SNuno Antunes /* Parse type for struct ng_l2tp_stats */
22058b02238SNuno Antunes static const struct ng_parse_struct_field
22158b02238SNuno Antunes ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
22258b02238SNuno Antunes static const struct ng_parse_type ng_l2tp_stats_type = {
22358b02238SNuno Antunes &ng_parse_struct_type,
22458b02238SNuno Antunes &ng_l2tp_stats_type_fields
22558b02238SNuno Antunes };
22658b02238SNuno Antunes
22758b02238SNuno Antunes /* Parse type for struct ng_l2tp_session_stats. */
22858b02238SNuno Antunes static const struct ng_parse_struct_field
22958b02238SNuno Antunes ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
23058b02238SNuno Antunes static const struct ng_parse_type ng_l2tp_session_stats_type = {
23158b02238SNuno Antunes &ng_parse_struct_type,
23258b02238SNuno Antunes &ng_l2tp_session_stats_type_fields
23358b02238SNuno Antunes };
23458b02238SNuno Antunes
23558b02238SNuno Antunes /* List of commands and how to convert arguments to/from ASCII */
23658b02238SNuno Antunes static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
23758b02238SNuno Antunes {
23858b02238SNuno Antunes NGM_L2TP_COOKIE,
23958b02238SNuno Antunes NGM_L2TP_SET_CONFIG,
24058b02238SNuno Antunes "setconfig",
24158b02238SNuno Antunes &ng_l2tp_config_type,
24258b02238SNuno Antunes NULL
24358b02238SNuno Antunes },
24458b02238SNuno Antunes {
24558b02238SNuno Antunes NGM_L2TP_COOKIE,
24658b02238SNuno Antunes NGM_L2TP_GET_CONFIG,
24758b02238SNuno Antunes "getconfig",
24858b02238SNuno Antunes NULL,
24958b02238SNuno Antunes &ng_l2tp_config_type
25058b02238SNuno Antunes },
25158b02238SNuno Antunes {
25258b02238SNuno Antunes NGM_L2TP_COOKIE,
25358b02238SNuno Antunes NGM_L2TP_SET_SESS_CONFIG,
25458b02238SNuno Antunes "setsessconfig",
25558b02238SNuno Antunes &ng_l2tp_sess_config_type,
25658b02238SNuno Antunes NULL
25758b02238SNuno Antunes },
25858b02238SNuno Antunes {
25958b02238SNuno Antunes NGM_L2TP_COOKIE,
26058b02238SNuno Antunes NGM_L2TP_GET_SESS_CONFIG,
26158b02238SNuno Antunes "getsessconfig",
26258b02238SNuno Antunes &ng_parse_hint16_type,
26358b02238SNuno Antunes &ng_l2tp_sess_config_type
26458b02238SNuno Antunes },
26558b02238SNuno Antunes {
26658b02238SNuno Antunes NGM_L2TP_COOKIE,
26758b02238SNuno Antunes NGM_L2TP_GET_STATS,
26858b02238SNuno Antunes "getstats",
26958b02238SNuno Antunes NULL,
27058b02238SNuno Antunes &ng_l2tp_stats_type
27158b02238SNuno Antunes },
27258b02238SNuno Antunes {
27358b02238SNuno Antunes NGM_L2TP_COOKIE,
27458b02238SNuno Antunes NGM_L2TP_CLR_STATS,
27558b02238SNuno Antunes "clrstats",
27658b02238SNuno Antunes NULL,
27758b02238SNuno Antunes NULL
27858b02238SNuno Antunes },
27958b02238SNuno Antunes {
28058b02238SNuno Antunes NGM_L2TP_COOKIE,
28158b02238SNuno Antunes NGM_L2TP_GETCLR_STATS,
28258b02238SNuno Antunes "getclrstats",
28358b02238SNuno Antunes NULL,
28458b02238SNuno Antunes &ng_l2tp_stats_type
28558b02238SNuno Antunes },
28658b02238SNuno Antunes {
28758b02238SNuno Antunes NGM_L2TP_COOKIE,
28858b02238SNuno Antunes NGM_L2TP_GET_SESSION_STATS,
28958b02238SNuno Antunes "getsessstats",
29058b02238SNuno Antunes &ng_parse_int16_type,
29158b02238SNuno Antunes &ng_l2tp_session_stats_type
29258b02238SNuno Antunes },
29358b02238SNuno Antunes {
29458b02238SNuno Antunes NGM_L2TP_COOKIE,
29558b02238SNuno Antunes NGM_L2TP_CLR_SESSION_STATS,
29658b02238SNuno Antunes "clrsessstats",
29758b02238SNuno Antunes &ng_parse_int16_type,
29858b02238SNuno Antunes NULL
29958b02238SNuno Antunes },
30058b02238SNuno Antunes {
30158b02238SNuno Antunes NGM_L2TP_COOKIE,
30258b02238SNuno Antunes NGM_L2TP_GETCLR_SESSION_STATS,
30358b02238SNuno Antunes "getclrsessstats",
30458b02238SNuno Antunes &ng_parse_int16_type,
30558b02238SNuno Antunes &ng_l2tp_session_stats_type
30658b02238SNuno Antunes },
30758b02238SNuno Antunes {
30858b02238SNuno Antunes NGM_L2TP_COOKIE,
30958b02238SNuno Antunes NGM_L2TP_ACK_FAILURE,
31058b02238SNuno Antunes "ackfailure",
31158b02238SNuno Antunes NULL,
31258b02238SNuno Antunes NULL
31358b02238SNuno Antunes },
31458b02238SNuno Antunes {
31558b02238SNuno Antunes NGM_L2TP_COOKIE,
31658b02238SNuno Antunes NGM_L2TP_SET_SEQ,
31758b02238SNuno Antunes "setsequence",
31858b02238SNuno Antunes &ng_l2tp_seq_config_type,
31958b02238SNuno Antunes NULL
32058b02238SNuno Antunes },
32158b02238SNuno Antunes { 0 }
32258b02238SNuno Antunes };
32358b02238SNuno Antunes
32458b02238SNuno Antunes /* Node type descriptor */
32558b02238SNuno Antunes static struct ng_type ng_l2tp_typestruct = {
32658b02238SNuno Antunes .version = NG_ABI_VERSION,
32758b02238SNuno Antunes .name = NG_L2TP_NODE_TYPE,
32858b02238SNuno Antunes .constructor = ng_l2tp_constructor,
32958b02238SNuno Antunes .rcvmsg = ng_l2tp_rcvmsg,
33058b02238SNuno Antunes .shutdown = ng_l2tp_shutdown,
33158b02238SNuno Antunes .newhook = ng_l2tp_newhook,
33258b02238SNuno Antunes .rcvdata = ng_l2tp_rcvdata,
33358b02238SNuno Antunes .disconnect = ng_l2tp_disconnect,
33458b02238SNuno Antunes .cmdlist = ng_l2tp_cmdlist,
33558b02238SNuno Antunes };
33658b02238SNuno Antunes NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
33758b02238SNuno Antunes
33858b02238SNuno Antunes /* Sequence number state sanity checking */
33958b02238SNuno Antunes #ifdef INVARIANTS
34058b02238SNuno Antunes #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
34158b02238SNuno Antunes #else
34258b02238SNuno Antunes #define L2TP_SEQ_CHECK(x) do { } while (0)
34358b02238SNuno Antunes #endif
34458b02238SNuno Antunes
34558b02238SNuno Antunes /* Whether to use m_copypacket() or m_dup() */
34658b02238SNuno Antunes #define L2TP_COPY_MBUF m_copypacket
34758b02238SNuno Antunes
34858b02238SNuno Antunes #define ERROUT(x) do { error = (x); goto done; } while (0)
34958b02238SNuno Antunes
35058b02238SNuno Antunes /************************************************************************
35158b02238SNuno Antunes NETGRAPH NODE STUFF
35258b02238SNuno Antunes ************************************************************************/
35358b02238SNuno Antunes
35458b02238SNuno Antunes /*
35558b02238SNuno Antunes * Node type constructor
35658b02238SNuno Antunes */
35758b02238SNuno Antunes static int
ng_l2tp_constructor(node_p node)35858b02238SNuno Antunes ng_l2tp_constructor(node_p node)
35958b02238SNuno Antunes {
36058b02238SNuno Antunes priv_p priv;
36158b02238SNuno Antunes int i;
36258b02238SNuno Antunes
36358b02238SNuno Antunes /* Allocate private structure */
36458b02238SNuno Antunes priv = kmalloc(sizeof(*priv), M_NETGRAPH_L2TP,
36558b02238SNuno Antunes M_WAITOK | M_NULLOK | M_ZERO);
36658b02238SNuno Antunes if (priv == NULL)
36758b02238SNuno Antunes return (ENOMEM);
36858b02238SNuno Antunes NG_NODE_SET_PRIVATE(node, priv);
36958b02238SNuno Antunes priv->node = node;
37058b02238SNuno Antunes
37158b02238SNuno Antunes /* Apply a semi-reasonable default configuration */
37258b02238SNuno Antunes priv->conf.peer_win = 1;
37358b02238SNuno Antunes priv->conf.rexmit_max = L2TP_MAX_REXMIT;
37458b02238SNuno Antunes priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
37558b02238SNuno Antunes
37658b02238SNuno Antunes /* Initialize sequence number state */
37758b02238SNuno Antunes ng_l2tp_seq_init(priv);
37858b02238SNuno Antunes
37958b02238SNuno Antunes for (i = 0; i < SESSHASHSIZE; i++)
38058b02238SNuno Antunes LIST_INIT(&priv->sesshash[i]);
38158b02238SNuno Antunes
38258b02238SNuno Antunes /* Done */
38358b02238SNuno Antunes return (0);
38458b02238SNuno Antunes }
38558b02238SNuno Antunes
38658b02238SNuno Antunes /*
38758b02238SNuno Antunes * Give our OK for a hook to be added.
38858b02238SNuno Antunes */
38958b02238SNuno Antunes static int
ng_l2tp_newhook(node_p node,hook_p hook,const char * name)39058b02238SNuno Antunes ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
39158b02238SNuno Antunes {
39258b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
39358b02238SNuno Antunes
39458b02238SNuno Antunes /* Check hook name */
39558b02238SNuno Antunes if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
39658b02238SNuno Antunes if (priv->ctrl != NULL)
39758b02238SNuno Antunes return (EISCONN);
39858b02238SNuno Antunes priv->ctrl = hook;
39958b02238SNuno Antunes NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
40058b02238SNuno Antunes } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
40158b02238SNuno Antunes if (priv->lower != NULL)
40258b02238SNuno Antunes return (EISCONN);
40358b02238SNuno Antunes priv->lower = hook;
40458b02238SNuno Antunes NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
40558b02238SNuno Antunes } else {
40658b02238SNuno Antunes static const char hexdig[16] = "0123456789abcdef";
40758b02238SNuno Antunes u_int16_t session_id;
40858b02238SNuno Antunes hookpriv_p hpriv;
40958b02238SNuno Antunes uint16_t hash;
41058b02238SNuno Antunes const char *hex;
41158b02238SNuno Antunes int i;
41258b02238SNuno Antunes int j;
41358b02238SNuno Antunes
41458b02238SNuno Antunes /* Parse hook name to get session ID */
41558b02238SNuno Antunes if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
41658b02238SNuno Antunes sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
41758b02238SNuno Antunes return (EINVAL);
41858b02238SNuno Antunes hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
41958b02238SNuno Antunes for (session_id = i = 0; i < 4; i++) {
42058b02238SNuno Antunes for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
42158b02238SNuno Antunes if (j == 16)
42258b02238SNuno Antunes return (EINVAL);
42358b02238SNuno Antunes session_id = (session_id << 4) | j;
42458b02238SNuno Antunes }
42558b02238SNuno Antunes if (hex[i] != '\0')
42658b02238SNuno Antunes return (EINVAL);
42758b02238SNuno Antunes
42858b02238SNuno Antunes /* Create hook private structure */
42958b02238SNuno Antunes hpriv = kmalloc(sizeof(*hpriv), M_NETGRAPH_L2TP,
43058b02238SNuno Antunes M_WAITOK | M_NULLOK | M_ZERO);
43158b02238SNuno Antunes if (hpriv == NULL)
43258b02238SNuno Antunes return (ENOMEM);
43358b02238SNuno Antunes hpriv->conf.session_id = htons(session_id);
43458b02238SNuno Antunes hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
43558b02238SNuno Antunes hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
43658b02238SNuno Antunes hpriv->hook = hook;
43758b02238SNuno Antunes NG_HOOK_SET_PRIVATE(hook, hpriv);
43858b02238SNuno Antunes hash = SESSHASH(hpriv->conf.session_id);
43958b02238SNuno Antunes LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
44058b02238SNuno Antunes }
44158b02238SNuno Antunes
44258b02238SNuno Antunes /* Done */
44358b02238SNuno Antunes return (0);
44458b02238SNuno Antunes }
44558b02238SNuno Antunes
44658b02238SNuno Antunes /*
44758b02238SNuno Antunes * Receive a control message.
44858b02238SNuno Antunes */
44958b02238SNuno Antunes static int
ng_l2tp_rcvmsg(node_p node,item_p item,hook_p lasthook)45058b02238SNuno Antunes ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
45158b02238SNuno Antunes {
45258b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
45358b02238SNuno Antunes struct ng_mesg *resp = NULL;
45458b02238SNuno Antunes struct ng_mesg *msg;
45558b02238SNuno Antunes int error = 0;
45658b02238SNuno Antunes
45758b02238SNuno Antunes NGI_GET_MSG(item, msg);
45858b02238SNuno Antunes switch (msg->header.typecookie) {
45958b02238SNuno Antunes case NGM_L2TP_COOKIE:
46058b02238SNuno Antunes switch (msg->header.cmd) {
46158b02238SNuno Antunes case NGM_L2TP_SET_CONFIG:
46258b02238SNuno Antunes {
46358b02238SNuno Antunes struct ng_l2tp_config *const conf =
46458b02238SNuno Antunes (struct ng_l2tp_config *)msg->data;
46558b02238SNuno Antunes
46658b02238SNuno Antunes /* Check for invalid or illegal config */
46758b02238SNuno Antunes if (msg->header.arglen != sizeof(*conf)) {
46858b02238SNuno Antunes error = EINVAL;
46958b02238SNuno Antunes break;
47058b02238SNuno Antunes }
47158b02238SNuno Antunes conf->enabled = !!conf->enabled;
47258b02238SNuno Antunes conf->match_id = !!conf->match_id;
47358b02238SNuno Antunes conf->tunnel_id = htons(conf->tunnel_id);
47458b02238SNuno Antunes conf->peer_id = htons(conf->peer_id);
47558b02238SNuno Antunes if (priv->conf.enabled
47658b02238SNuno Antunes && ((priv->conf.tunnel_id != 0
47758b02238SNuno Antunes && conf->tunnel_id != priv->conf.tunnel_id)
47858b02238SNuno Antunes || ((priv->conf.peer_id != 0
47958b02238SNuno Antunes && conf->peer_id != priv->conf.peer_id)))) {
48058b02238SNuno Antunes error = EBUSY;
48158b02238SNuno Antunes break;
48258b02238SNuno Antunes }
48358b02238SNuno Antunes
48458b02238SNuno Antunes /* Save calling node as failure target */
48558b02238SNuno Antunes priv->ftarget = NGI_RETADDR(item);
48658b02238SNuno Antunes
48758b02238SNuno Antunes /* Adjust sequence number state */
48858b02238SNuno Antunes if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
48958b02238SNuno Antunes break;
49058b02238SNuno Antunes
49158b02238SNuno Antunes /* Update node's config */
49258b02238SNuno Antunes priv->conf = *conf;
49358b02238SNuno Antunes break;
49458b02238SNuno Antunes }
49558b02238SNuno Antunes case NGM_L2TP_GET_CONFIG:
49658b02238SNuno Antunes {
49758b02238SNuno Antunes struct ng_l2tp_config *conf;
49858b02238SNuno Antunes
49958b02238SNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
50058b02238SNuno Antunes if (resp == NULL) {
50158b02238SNuno Antunes error = ENOMEM;
50258b02238SNuno Antunes break;
50358b02238SNuno Antunes }
50458b02238SNuno Antunes conf = (struct ng_l2tp_config *)resp->data;
50558b02238SNuno Antunes *conf = priv->conf;
50658b02238SNuno Antunes
50758b02238SNuno Antunes /* Put ID's in host order */
50858b02238SNuno Antunes conf->tunnel_id = ntohs(conf->tunnel_id);
50958b02238SNuno Antunes conf->peer_id = ntohs(conf->peer_id);
51058b02238SNuno Antunes break;
51158b02238SNuno Antunes }
51258b02238SNuno Antunes case NGM_L2TP_SET_SESS_CONFIG:
51358b02238SNuno Antunes {
51458b02238SNuno Antunes struct ng_l2tp_sess_config *const conf =
51558b02238SNuno Antunes (struct ng_l2tp_sess_config *)msg->data;
51658b02238SNuno Antunes hookpriv_p hpriv;
51758b02238SNuno Antunes
51858b02238SNuno Antunes /* Check for invalid or illegal config. */
51958b02238SNuno Antunes if (msg->header.arglen != sizeof(*conf)) {
52058b02238SNuno Antunes error = EINVAL;
52158b02238SNuno Antunes break;
52258b02238SNuno Antunes }
52358b02238SNuno Antunes
52458b02238SNuno Antunes /* Put ID's in network order */
52558b02238SNuno Antunes conf->session_id = htons(conf->session_id);
52658b02238SNuno Antunes conf->peer_id = htons(conf->peer_id);
52758b02238SNuno Antunes
52858b02238SNuno Antunes /* Find matching hook */
52958b02238SNuno Antunes hpriv = ng_l2tp_find_session(priv, conf->session_id);
53058b02238SNuno Antunes if (hpriv == NULL) {
53158b02238SNuno Antunes error = ENOENT;
53258b02238SNuno Antunes break;
53358b02238SNuno Antunes }
53458b02238SNuno Antunes
53558b02238SNuno Antunes /* Update hook's config */
53658b02238SNuno Antunes hpriv->conf = *conf;
53758b02238SNuno Antunes break;
53858b02238SNuno Antunes }
53958b02238SNuno Antunes case NGM_L2TP_GET_SESS_CONFIG:
54058b02238SNuno Antunes {
54158b02238SNuno Antunes struct ng_l2tp_sess_config *conf;
54258b02238SNuno Antunes u_int16_t session_id;
54358b02238SNuno Antunes hookpriv_p hpriv;
54458b02238SNuno Antunes
54558b02238SNuno Antunes /* Get session ID */
54658b02238SNuno Antunes if (msg->header.arglen != sizeof(session_id)) {
54758b02238SNuno Antunes error = EINVAL;
54858b02238SNuno Antunes break;
54958b02238SNuno Antunes }
55058b02238SNuno Antunes memcpy(&session_id, msg->data, 2);
55158b02238SNuno Antunes session_id = htons(session_id);
55258b02238SNuno Antunes
55358b02238SNuno Antunes /* Find matching hook */
55458b02238SNuno Antunes hpriv = ng_l2tp_find_session(priv, session_id);
55558b02238SNuno Antunes if (hpriv == NULL) {
55658b02238SNuno Antunes error = ENOENT;
55758b02238SNuno Antunes break;
55858b02238SNuno Antunes }
55958b02238SNuno Antunes
56058b02238SNuno Antunes /* Send response */
56158b02238SNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_WAITOK | M_NULLOK);
56258b02238SNuno Antunes if (resp == NULL) {
56358b02238SNuno Antunes error = ENOMEM;
56458b02238SNuno Antunes break;
56558b02238SNuno Antunes }
56658b02238SNuno Antunes conf = (struct ng_l2tp_sess_config *)resp->data;
56758b02238SNuno Antunes *conf = hpriv->conf;
56858b02238SNuno Antunes
56958b02238SNuno Antunes /* Put ID's in host order */
57058b02238SNuno Antunes conf->session_id = ntohs(conf->session_id);
57158b02238SNuno Antunes conf->peer_id = ntohs(conf->peer_id);
57258b02238SNuno Antunes break;
57358b02238SNuno Antunes }
57458b02238SNuno Antunes case NGM_L2TP_GET_STATS:
57558b02238SNuno Antunes case NGM_L2TP_CLR_STATS:
57658b02238SNuno Antunes case NGM_L2TP_GETCLR_STATS:
57758b02238SNuno Antunes {
57858b02238SNuno Antunes if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
57958b02238SNuno Antunes NG_MKRESPONSE(resp, msg,
58058b02238SNuno Antunes sizeof(priv->stats), M_WAITOK | M_NULLOK);
58158b02238SNuno Antunes if (resp == NULL) {
58258b02238SNuno Antunes error = ENOMEM;
58358b02238SNuno Antunes break;
58458b02238SNuno Antunes }
58558b02238SNuno Antunes memcpy(resp->data,
58658b02238SNuno Antunes &priv->stats, sizeof(priv->stats));
58758b02238SNuno Antunes }
58858b02238SNuno Antunes if (msg->header.cmd != NGM_L2TP_GET_STATS)
58958b02238SNuno Antunes memset(&priv->stats, 0, sizeof(priv->stats));
59058b02238SNuno Antunes break;
59158b02238SNuno Antunes }
59258b02238SNuno Antunes case NGM_L2TP_GET_SESSION_STATS:
59358b02238SNuno Antunes case NGM_L2TP_CLR_SESSION_STATS:
59458b02238SNuno Antunes case NGM_L2TP_GETCLR_SESSION_STATS:
59558b02238SNuno Antunes {
59658b02238SNuno Antunes uint16_t session_id;
59758b02238SNuno Antunes hookpriv_p hpriv;
59858b02238SNuno Antunes
59958b02238SNuno Antunes /* Get session ID. */
60058b02238SNuno Antunes if (msg->header.arglen != sizeof(session_id)) {
60158b02238SNuno Antunes error = EINVAL;
60258b02238SNuno Antunes break;
60358b02238SNuno Antunes }
60458b02238SNuno Antunes bcopy(msg->data, &session_id, sizeof(uint16_t));
60558b02238SNuno Antunes session_id = htons(session_id);
60658b02238SNuno Antunes
60758b02238SNuno Antunes /* Find matching hook. */
60858b02238SNuno Antunes hpriv = ng_l2tp_find_session(priv, session_id);
60958b02238SNuno Antunes if (hpriv == NULL) {
61058b02238SNuno Antunes error = ENOENT;
61158b02238SNuno Antunes break;
61258b02238SNuno Antunes }
61358b02238SNuno Antunes
61458b02238SNuno Antunes if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
61558b02238SNuno Antunes NG_MKRESPONSE(resp, msg,
61658b02238SNuno Antunes sizeof(hpriv->stats), M_WAITOK | M_NULLOK);
61758b02238SNuno Antunes if (resp == NULL) {
61858b02238SNuno Antunes error = ENOMEM;
61958b02238SNuno Antunes break;
62058b02238SNuno Antunes }
62158b02238SNuno Antunes bcopy(&hpriv->stats, resp->data,
62258b02238SNuno Antunes sizeof(hpriv->stats));
62358b02238SNuno Antunes }
62458b02238SNuno Antunes if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
62558b02238SNuno Antunes bzero(&hpriv->stats, sizeof(hpriv->stats));
62658b02238SNuno Antunes break;
62758b02238SNuno Antunes }
62858b02238SNuno Antunes case NGM_L2TP_SET_SEQ:
62958b02238SNuno Antunes {
63058b02238SNuno Antunes struct ng_l2tp_seq_config *const conf =
63158b02238SNuno Antunes (struct ng_l2tp_seq_config *)msg->data;
63258b02238SNuno Antunes
63358b02238SNuno Antunes /* Check for invalid or illegal seq config. */
63458b02238SNuno Antunes if (msg->header.arglen != sizeof(*conf)) {
63558b02238SNuno Antunes error = EINVAL;
63658b02238SNuno Antunes break;
63758b02238SNuno Antunes }
63858b02238SNuno Antunes conf->ns = htons(conf->ns);
63958b02238SNuno Antunes conf->nr = htons(conf->nr);
64058b02238SNuno Antunes conf->rack = htons(conf->rack);
64158b02238SNuno Antunes conf->xack = htons(conf->xack);
64258b02238SNuno Antunes
64358b02238SNuno Antunes /* Set sequence numbers. */
64458b02238SNuno Antunes error = ng_l2tp_seq_set(priv, conf);
64558b02238SNuno Antunes break;
64658b02238SNuno Antunes }
64758b02238SNuno Antunes default:
64858b02238SNuno Antunes error = EINVAL;
64958b02238SNuno Antunes break;
65058b02238SNuno Antunes }
65158b02238SNuno Antunes break;
65258b02238SNuno Antunes default:
65358b02238SNuno Antunes error = EINVAL;
65458b02238SNuno Antunes break;
65558b02238SNuno Antunes }
65658b02238SNuno Antunes
65758b02238SNuno Antunes /* Done */
65858b02238SNuno Antunes NG_RESPOND_MSG(error, node, item, resp);
65958b02238SNuno Antunes NG_FREE_MSG(msg);
66058b02238SNuno Antunes return (error);
66158b02238SNuno Antunes }
66258b02238SNuno Antunes
66358b02238SNuno Antunes /*
66458b02238SNuno Antunes * Destroy node
66558b02238SNuno Antunes */
66658b02238SNuno Antunes static int
ng_l2tp_shutdown(node_p node)66758b02238SNuno Antunes ng_l2tp_shutdown(node_p node)
66858b02238SNuno Antunes {
66958b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
67058b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
67158b02238SNuno Antunes
67258b02238SNuno Antunes /* Sanity check */
67358b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
67458b02238SNuno Antunes
67558b02238SNuno Antunes /* Reset sequence number state */
67658b02238SNuno Antunes ng_l2tp_seq_reset(priv);
67758b02238SNuno Antunes
67858b02238SNuno Antunes /* Free private data if neither timer is running */
67958b02238SNuno Antunes ng_uncallout(&seq->rack_timer, node);
68058b02238SNuno Antunes ng_uncallout(&seq->xack_timer, node);
68158b02238SNuno Antunes
682a6c72860SNuno Antunes mtx_uninit(&seq->mtx);
68358b02238SNuno Antunes
68458b02238SNuno Antunes kfree(priv, M_NETGRAPH_L2TP);
68558b02238SNuno Antunes
68658b02238SNuno Antunes /* Unref node */
68758b02238SNuno Antunes NG_NODE_UNREF(node);
68858b02238SNuno Antunes return (0);
68958b02238SNuno Antunes }
69058b02238SNuno Antunes
69158b02238SNuno Antunes /*
69258b02238SNuno Antunes * Hook disconnection
69358b02238SNuno Antunes */
69458b02238SNuno Antunes static int
ng_l2tp_disconnect(hook_p hook)69558b02238SNuno Antunes ng_l2tp_disconnect(hook_p hook)
69658b02238SNuno Antunes {
69758b02238SNuno Antunes const node_p node = NG_HOOK_NODE(hook);
69858b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
69958b02238SNuno Antunes
70058b02238SNuno Antunes /* Zero out hook pointer */
70158b02238SNuno Antunes if (hook == priv->ctrl)
70258b02238SNuno Antunes priv->ctrl = NULL;
70358b02238SNuno Antunes else if (hook == priv->lower)
70458b02238SNuno Antunes priv->lower = NULL;
70558b02238SNuno Antunes else {
70658b02238SNuno Antunes const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
70758b02238SNuno Antunes LIST_REMOVE(hpriv, sessions);
70858b02238SNuno Antunes kfree(hpriv, M_NETGRAPH_L2TP);
70958b02238SNuno Antunes NG_HOOK_SET_PRIVATE(hook, NULL);
71058b02238SNuno Antunes }
71158b02238SNuno Antunes
71258b02238SNuno Antunes /* Go away if no longer connected to anything */
71358b02238SNuno Antunes if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
71458b02238SNuno Antunes ng_rmnode_self(node);
71558b02238SNuno Antunes return (0);
71658b02238SNuno Antunes }
71758b02238SNuno Antunes
71858b02238SNuno Antunes /*************************************************************************
71958b02238SNuno Antunes INTERNAL FUNCTIONS
72058b02238SNuno Antunes *************************************************************************/
72158b02238SNuno Antunes
72258b02238SNuno Antunes /*
72358b02238SNuno Antunes * Find the hook with a given session ID (in network order).
72458b02238SNuno Antunes */
72558b02238SNuno Antunes static hookpriv_p
ng_l2tp_find_session(priv_p privp,u_int16_t sid)72658b02238SNuno Antunes ng_l2tp_find_session(priv_p privp, u_int16_t sid)
72758b02238SNuno Antunes {
72858b02238SNuno Antunes uint16_t hash = SESSHASH(sid);
72958b02238SNuno Antunes hookpriv_p hpriv = NULL;
73058b02238SNuno Antunes
73158b02238SNuno Antunes LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
73258b02238SNuno Antunes if (hpriv->conf.session_id == sid)
73358b02238SNuno Antunes break;
73458b02238SNuno Antunes }
73558b02238SNuno Antunes
73658b02238SNuno Antunes return (hpriv);
73758b02238SNuno Antunes }
73858b02238SNuno Antunes
73958b02238SNuno Antunes /*
74058b02238SNuno Antunes * Reset a hook's session state.
74158b02238SNuno Antunes */
74258b02238SNuno Antunes static int
ng_l2tp_reset_session(hook_p hook,void * arg)74358b02238SNuno Antunes ng_l2tp_reset_session(hook_p hook, void *arg)
74458b02238SNuno Antunes {
74558b02238SNuno Antunes const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
74658b02238SNuno Antunes
74758b02238SNuno Antunes if (hpriv != NULL) {
74858b02238SNuno Antunes hpriv->conf.control_dseq = 0;
74958b02238SNuno Antunes hpriv->conf.enable_dseq = 0;
75058b02238SNuno Antunes bzero(&hpriv->conf, sizeof(struct ng_l2tp_session_stats));
75158b02238SNuno Antunes hpriv->nr = 0;
75258b02238SNuno Antunes hpriv->ns = 0;
75358b02238SNuno Antunes }
75458b02238SNuno Antunes return (-1);
75558b02238SNuno Antunes }
75658b02238SNuno Antunes
75758b02238SNuno Antunes /*
75858b02238SNuno Antunes * Handle an incoming frame from below.
75958b02238SNuno Antunes */
76058b02238SNuno Antunes static int
ng_l2tp_rcvdata_lower(hook_p h,item_p item)76158b02238SNuno Antunes ng_l2tp_rcvdata_lower(hook_p h, item_p item)
76258b02238SNuno Antunes {
76358b02238SNuno Antunes static const u_int16_t req_bits[2][2] = {
76458b02238SNuno Antunes { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
76558b02238SNuno Antunes { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
76658b02238SNuno Antunes };
76758b02238SNuno Antunes const node_p node = NG_HOOK_NODE(h);
76858b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
76958b02238SNuno Antunes hookpriv_p hpriv = NULL;
77058b02238SNuno Antunes hook_p hook = NULL;
77158b02238SNuno Antunes u_int16_t ids[2];
77258b02238SNuno Antunes struct mbuf *m;
77358b02238SNuno Antunes u_int16_t hdr;
77458b02238SNuno Antunes u_int16_t ns;
77558b02238SNuno Antunes u_int16_t nr;
77658b02238SNuno Antunes int is_ctrl;
77758b02238SNuno Antunes int error;
77858b02238SNuno Antunes int len, plen;
77958b02238SNuno Antunes
78058b02238SNuno Antunes /* Sanity check */
78158b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
78258b02238SNuno Antunes
78358b02238SNuno Antunes /* If not configured, reject */
78458b02238SNuno Antunes if (!priv->conf.enabled) {
78558b02238SNuno Antunes NG_FREE_ITEM(item);
78658b02238SNuno Antunes ERROUT(ENXIO);
78758b02238SNuno Antunes }
78858b02238SNuno Antunes
78958b02238SNuno Antunes /* Grab mbuf */
79058b02238SNuno Antunes NGI_GET_M(item, m);
79158b02238SNuno Antunes
79258b02238SNuno Antunes /* Remember full packet length; needed for per session accounting. */
79358b02238SNuno Antunes plen = m->m_pkthdr.len;
79458b02238SNuno Antunes
79558b02238SNuno Antunes /* Update stats */
79658b02238SNuno Antunes priv->stats.recvPackets++;
79758b02238SNuno Antunes priv->stats.recvOctets += plen;
79858b02238SNuno Antunes
79958b02238SNuno Antunes /* Get initial header */
80058b02238SNuno Antunes if (m->m_pkthdr.len < 6) {
80158b02238SNuno Antunes priv->stats.recvRunts++;
80258b02238SNuno Antunes NG_FREE_ITEM(item);
80358b02238SNuno Antunes NG_FREE_M(m);
80458b02238SNuno Antunes ERROUT(EINVAL);
80558b02238SNuno Antunes }
80658b02238SNuno Antunes if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
80758b02238SNuno Antunes priv->stats.memoryFailures++;
80858b02238SNuno Antunes NG_FREE_ITEM(item);
80958b02238SNuno Antunes ERROUT(EINVAL);
81058b02238SNuno Antunes }
81158b02238SNuno Antunes hdr = ntohs(*mtod(m, u_int16_t *));
81258b02238SNuno Antunes m_adj(m, 2);
81358b02238SNuno Antunes
81458b02238SNuno Antunes /* Check required header bits and minimum length */
81558b02238SNuno Antunes is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
81658b02238SNuno Antunes if ((hdr & req_bits[is_ctrl][0]) != 0
81758b02238SNuno Antunes || (~hdr & req_bits[is_ctrl][1]) != 0) {
81858b02238SNuno Antunes priv->stats.recvInvalid++;
81958b02238SNuno Antunes NG_FREE_ITEM(item);
82058b02238SNuno Antunes NG_FREE_M(m);
82158b02238SNuno Antunes ERROUT(EINVAL);
82258b02238SNuno Antunes }
82358b02238SNuno Antunes if (m->m_pkthdr.len < 4 /* tunnel, session id */
82458b02238SNuno Antunes + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
82558b02238SNuno Antunes + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
82658b02238SNuno Antunes + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
82758b02238SNuno Antunes priv->stats.recvRunts++;
82858b02238SNuno Antunes NG_FREE_ITEM(item);
82958b02238SNuno Antunes NG_FREE_M(m);
83058b02238SNuno Antunes ERROUT(EINVAL);
83158b02238SNuno Antunes }
83258b02238SNuno Antunes
83358b02238SNuno Antunes /* Get and validate length field if present */
83458b02238SNuno Antunes if ((hdr & L2TP_HDR_LEN) != 0) {
83558b02238SNuno Antunes if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
83658b02238SNuno Antunes priv->stats.memoryFailures++;
83758b02238SNuno Antunes NG_FREE_ITEM(item);
83858b02238SNuno Antunes ERROUT(EINVAL);
83958b02238SNuno Antunes }
84058b02238SNuno Antunes len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
84158b02238SNuno Antunes m_adj(m, 2);
84258b02238SNuno Antunes if (len < 0 || len > m->m_pkthdr.len) {
84358b02238SNuno Antunes priv->stats.recvInvalid++;
84458b02238SNuno Antunes NG_FREE_ITEM(item);
84558b02238SNuno Antunes NG_FREE_M(m);
84658b02238SNuno Antunes ERROUT(EINVAL);
84758b02238SNuno Antunes }
84858b02238SNuno Antunes if (len < m->m_pkthdr.len) /* trim extra bytes */
84958b02238SNuno Antunes m_adj(m, -(m->m_pkthdr.len - len));
85058b02238SNuno Antunes }
85158b02238SNuno Antunes
85258b02238SNuno Antunes /* Get tunnel ID and session ID */
85358b02238SNuno Antunes if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
85458b02238SNuno Antunes priv->stats.memoryFailures++;
85558b02238SNuno Antunes NG_FREE_ITEM(item);
85658b02238SNuno Antunes ERROUT(EINVAL);
85758b02238SNuno Antunes }
85858b02238SNuno Antunes memcpy(ids, mtod(m, u_int16_t *), 4);
85958b02238SNuno Antunes m_adj(m, 4);
86058b02238SNuno Antunes
86158b02238SNuno Antunes /* Check tunnel ID */
86258b02238SNuno Antunes if (ids[0] != priv->conf.tunnel_id
86358b02238SNuno Antunes && (priv->conf.match_id || ids[0] != 0)) {
86458b02238SNuno Antunes priv->stats.recvWrongTunnel++;
86558b02238SNuno Antunes NG_FREE_ITEM(item);
86658b02238SNuno Antunes NG_FREE_M(m);
86758b02238SNuno Antunes ERROUT(EADDRNOTAVAIL);
86858b02238SNuno Antunes }
86958b02238SNuno Antunes
87058b02238SNuno Antunes /* Check session ID (for data packets only) */
87158b02238SNuno Antunes if ((hdr & L2TP_HDR_CTRL) == 0) {
87258b02238SNuno Antunes hpriv = ng_l2tp_find_session(priv, ids[1]);
87358b02238SNuno Antunes if (hpriv == NULL) {
87458b02238SNuno Antunes priv->stats.recvUnknownSID++;
87558b02238SNuno Antunes NG_FREE_ITEM(item);
87658b02238SNuno Antunes NG_FREE_M(m);
87758b02238SNuno Antunes ERROUT(ENOTCONN);
87858b02238SNuno Antunes }
87958b02238SNuno Antunes hook = hpriv->hook;
88058b02238SNuno Antunes }
88158b02238SNuno Antunes
88258b02238SNuno Antunes /* Get Ns, Nr fields if present */
88358b02238SNuno Antunes if ((hdr & L2TP_HDR_SEQ) != 0) {
88458b02238SNuno Antunes if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
88558b02238SNuno Antunes priv->stats.memoryFailures++;
88658b02238SNuno Antunes NG_FREE_ITEM(item);
88758b02238SNuno Antunes ERROUT(EINVAL);
88858b02238SNuno Antunes }
88958b02238SNuno Antunes memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
89058b02238SNuno Antunes ns = ntohs(ns);
89158b02238SNuno Antunes memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
89258b02238SNuno Antunes nr = ntohs(nr);
89358b02238SNuno Antunes m_adj(m, 4);
894*32ac4919SSascha Wildner } else {
895*32ac4919SSascha Wildner nr = 0; /* avoid gcc complaint */
896*32ac4919SSascha Wildner ns = 0; /* avoid gcc complaint */
89758b02238SNuno Antunes }
89858b02238SNuno Antunes
89958b02238SNuno Antunes /* Strip offset padding if present */
90058b02238SNuno Antunes if ((hdr & L2TP_HDR_OFF) != 0) {
90158b02238SNuno Antunes u_int16_t offset;
90258b02238SNuno Antunes
90358b02238SNuno Antunes /* Get length of offset padding */
90458b02238SNuno Antunes if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
90558b02238SNuno Antunes priv->stats.memoryFailures++;
90658b02238SNuno Antunes NG_FREE_ITEM(item);
90758b02238SNuno Antunes ERROUT(EINVAL);
90858b02238SNuno Antunes }
90958b02238SNuno Antunes memcpy(&offset, mtod(m, u_int16_t *), 2);
91058b02238SNuno Antunes offset = ntohs(offset);
91158b02238SNuno Antunes
91258b02238SNuno Antunes /* Trim offset padding */
91358b02238SNuno Antunes if ((2+offset) > m->m_pkthdr.len) {
91458b02238SNuno Antunes priv->stats.recvInvalid++;
91558b02238SNuno Antunes NG_FREE_ITEM(item);
91658b02238SNuno Antunes NG_FREE_M(m);
91758b02238SNuno Antunes ERROUT(EINVAL);
91858b02238SNuno Antunes }
91958b02238SNuno Antunes m_adj(m, 2+offset);
92058b02238SNuno Antunes }
92158b02238SNuno Antunes
92258b02238SNuno Antunes /* Handle control packets */
92358b02238SNuno Antunes if ((hdr & L2TP_HDR_CTRL) != 0) {
92458b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
92558b02238SNuno Antunes
92658b02238SNuno Antunes /* Handle receive ack sequence number Nr */
92758b02238SNuno Antunes ng_l2tp_seq_recv_nr(priv, nr);
92858b02238SNuno Antunes
92958b02238SNuno Antunes /* Discard ZLB packets */
93058b02238SNuno Antunes if (m->m_pkthdr.len == 0) {
93158b02238SNuno Antunes priv->stats.recvZLBs++;
93258b02238SNuno Antunes NG_FREE_ITEM(item);
93358b02238SNuno Antunes NG_FREE_M(m);
93458b02238SNuno Antunes ERROUT(0);
93558b02238SNuno Antunes }
93658b02238SNuno Antunes
93758b02238SNuno Antunes mtx_lock(&seq->mtx);
93858b02238SNuno Antunes /*
93958b02238SNuno Antunes * If not what we expect or we are busy, drop packet and
94058b02238SNuno Antunes * send an immediate ZLB ack.
94158b02238SNuno Antunes */
94258b02238SNuno Antunes if (ns != seq->nr || seq->inproc) {
94358b02238SNuno Antunes if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
94458b02238SNuno Antunes priv->stats.recvDuplicates++;
94558b02238SNuno Antunes else
94658b02238SNuno Antunes priv->stats.recvOutOfOrder++;
94758b02238SNuno Antunes mtx_unlock(&seq->mtx);
94858b02238SNuno Antunes ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
94958b02238SNuno Antunes NG_FREE_ITEM(item);
95058b02238SNuno Antunes NG_FREE_M(m);
95158b02238SNuno Antunes ERROUT(0);
95258b02238SNuno Antunes }
95358b02238SNuno Antunes /*
95458b02238SNuno Antunes * Until we deliver this packet we can't receive next one as
95558b02238SNuno Antunes * we have no information for sending ack.
95658b02238SNuno Antunes */
95758b02238SNuno Antunes seq->inproc = 1;
95858b02238SNuno Antunes mtx_unlock(&seq->mtx);
95958b02238SNuno Antunes
96058b02238SNuno Antunes /* Prepend session ID to packet. */
961b5523eacSSascha Wildner M_PREPEND(m, 2, M_NOWAIT);
96258b02238SNuno Antunes if (m == NULL) {
96358b02238SNuno Antunes seq->inproc = 0;
96458b02238SNuno Antunes priv->stats.memoryFailures++;
96558b02238SNuno Antunes NG_FREE_ITEM(item);
96658b02238SNuno Antunes ERROUT(ENOBUFS);
96758b02238SNuno Antunes }
96858b02238SNuno Antunes memcpy(mtod(m, u_int16_t *), &ids[1], 2);
96958b02238SNuno Antunes
97058b02238SNuno Antunes /* Deliver packet to upper layers */
97158b02238SNuno Antunes NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
97258b02238SNuno Antunes
97358b02238SNuno Antunes mtx_lock(&seq->mtx);
97458b02238SNuno Antunes /* Ready to process next packet. */
97558b02238SNuno Antunes seq->inproc = 0;
97658b02238SNuno Antunes
97758b02238SNuno Antunes /* If packet was successfully delivered send ack. */
97858b02238SNuno Antunes if (error == 0) {
97958b02238SNuno Antunes /* Update recv sequence number */
98058b02238SNuno Antunes seq->nr++;
98158b02238SNuno Antunes /* Start receive ack timer, if not already running */
98258b02238SNuno Antunes if (!callout_active(&seq->xack_timer)) {
98358b02238SNuno Antunes ng_callout(&seq->xack_timer, priv->node, NULL,
98458b02238SNuno Antunes L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
98558b02238SNuno Antunes NULL, 0);
98658b02238SNuno Antunes }
98758b02238SNuno Antunes }
98858b02238SNuno Antunes mtx_unlock(&seq->mtx);
98958b02238SNuno Antunes
99058b02238SNuno Antunes ERROUT(error);
99158b02238SNuno Antunes }
99258b02238SNuno Antunes
99358b02238SNuno Antunes /* Per session packet, account it. */
99458b02238SNuno Antunes hpriv->stats.recvPackets++;
99558b02238SNuno Antunes hpriv->stats.recvOctets += plen;
99658b02238SNuno Antunes
99758b02238SNuno Antunes /* Follow peer's lead in data sequencing, if configured to do so */
99858b02238SNuno Antunes if (!hpriv->conf.control_dseq)
99958b02238SNuno Antunes hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
100058b02238SNuno Antunes
100158b02238SNuno Antunes /* Handle data sequence numbers if present and enabled */
100258b02238SNuno Antunes if ((hdr & L2TP_HDR_SEQ) != 0) {
100358b02238SNuno Antunes if (hpriv->conf.enable_dseq
100458b02238SNuno Antunes && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
100558b02238SNuno Antunes NG_FREE_ITEM(item); /* duplicate or out of order */
100658b02238SNuno Antunes NG_FREE_M(m);
100758b02238SNuno Antunes priv->stats.recvDataDrops++;
100858b02238SNuno Antunes ERROUT(0);
100958b02238SNuno Antunes }
101058b02238SNuno Antunes hpriv->nr = ns + 1;
101158b02238SNuno Antunes }
101258b02238SNuno Antunes
101358b02238SNuno Antunes /* Drop empty data packets */
101458b02238SNuno Antunes if (m->m_pkthdr.len == 0) {
101558b02238SNuno Antunes NG_FREE_ITEM(item);
101658b02238SNuno Antunes NG_FREE_M(m);
101758b02238SNuno Antunes ERROUT(0);
101858b02238SNuno Antunes }
101958b02238SNuno Antunes
102058b02238SNuno Antunes /* Deliver data */
102158b02238SNuno Antunes NG_FWD_NEW_DATA(error, item, hook, m);
102258b02238SNuno Antunes done:
102358b02238SNuno Antunes /* Done */
102458b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
102558b02238SNuno Antunes return (error);
102658b02238SNuno Antunes }
102758b02238SNuno Antunes
102858b02238SNuno Antunes /*
102958b02238SNuno Antunes * Handle an outgoing control frame.
103058b02238SNuno Antunes */
103158b02238SNuno Antunes static int
ng_l2tp_rcvdata_ctrl(hook_p hook,item_p item)103258b02238SNuno Antunes ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
103358b02238SNuno Antunes {
103458b02238SNuno Antunes const node_p node = NG_HOOK_NODE(hook);
103558b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
103658b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
103758b02238SNuno Antunes struct mbuf *m;
103858b02238SNuno Antunes int error;
103958b02238SNuno Antunes int i;
104058b02238SNuno Antunes u_int16_t ns;
104158b02238SNuno Antunes
104258b02238SNuno Antunes /* Sanity check */
104358b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
104458b02238SNuno Antunes
104558b02238SNuno Antunes /* If not configured, reject */
104658b02238SNuno Antunes if (!priv->conf.enabled) {
104758b02238SNuno Antunes NG_FREE_ITEM(item);
104858b02238SNuno Antunes ERROUT(ENXIO);
104958b02238SNuno Antunes }
105058b02238SNuno Antunes
105158b02238SNuno Antunes /* Grab mbuf and discard other stuff XXX */
105258b02238SNuno Antunes NGI_GET_M(item, m);
105358b02238SNuno Antunes NG_FREE_ITEM(item);
105458b02238SNuno Antunes
105558b02238SNuno Antunes /* Packet should have session ID prepended */
105658b02238SNuno Antunes if (m->m_pkthdr.len < 2) {
105758b02238SNuno Antunes priv->stats.xmitInvalid++;
105858b02238SNuno Antunes m_freem(m);
105958b02238SNuno Antunes ERROUT(EINVAL);
106058b02238SNuno Antunes }
106158b02238SNuno Antunes
106258b02238SNuno Antunes /* Check max length */
106358b02238SNuno Antunes if (m->m_pkthdr.len >= 0x10000 - 14) {
106458b02238SNuno Antunes priv->stats.xmitTooBig++;
106558b02238SNuno Antunes m_freem(m);
106658b02238SNuno Antunes ERROUT(EOVERFLOW);
106758b02238SNuno Antunes }
106858b02238SNuno Antunes
106958b02238SNuno Antunes mtx_lock(&seq->mtx);
107058b02238SNuno Antunes
107158b02238SNuno Antunes /* Find next empty slot in transmit queue */
107258b02238SNuno Antunes for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
107358b02238SNuno Antunes if (i == L2TP_MAX_XWIN) {
107458b02238SNuno Antunes mtx_unlock(&seq->mtx);
107558b02238SNuno Antunes priv->stats.xmitDrops++;
107658b02238SNuno Antunes m_freem(m);
107758b02238SNuno Antunes ERROUT(ENOBUFS);
107858b02238SNuno Antunes }
107958b02238SNuno Antunes seq->xwin[i] = m;
108058b02238SNuno Antunes
108158b02238SNuno Antunes /* If peer's receive window is already full, nothing else to do */
108258b02238SNuno Antunes if (i >= seq->cwnd) {
108358b02238SNuno Antunes mtx_unlock(&seq->mtx);
108458b02238SNuno Antunes ERROUT(0);
108558b02238SNuno Antunes }
108658b02238SNuno Antunes
108758b02238SNuno Antunes /* Start retransmit timer if not already running */
108858b02238SNuno Antunes if (!callout_active(&seq->rack_timer))
108958b02238SNuno Antunes ng_callout(&seq->rack_timer, node, NULL,
109058b02238SNuno Antunes hz, ng_l2tp_seq_rack_timeout, NULL, 0);
109158b02238SNuno Antunes
109258b02238SNuno Antunes ns = seq->ns++;
109358b02238SNuno Antunes
109458b02238SNuno Antunes mtx_unlock(&seq->mtx);
109558b02238SNuno Antunes
109658b02238SNuno Antunes /* Copy packet */
1097b5523eacSSascha Wildner if ((m = L2TP_COPY_MBUF(m, M_NOWAIT)) == NULL) {
109858b02238SNuno Antunes priv->stats.memoryFailures++;
109958b02238SNuno Antunes ERROUT(ENOBUFS);
110058b02238SNuno Antunes }
110158b02238SNuno Antunes
110258b02238SNuno Antunes /* Send packet and increment xmit sequence number */
110358b02238SNuno Antunes error = ng_l2tp_xmit_ctrl(priv, m, ns);
110458b02238SNuno Antunes done:
110558b02238SNuno Antunes /* Done */
110658b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
110758b02238SNuno Antunes return (error);
110858b02238SNuno Antunes }
110958b02238SNuno Antunes
111058b02238SNuno Antunes /*
111158b02238SNuno Antunes * Handle an outgoing data frame.
111258b02238SNuno Antunes */
111358b02238SNuno Antunes static int
ng_l2tp_rcvdata(hook_p hook,item_p item)111458b02238SNuno Antunes ng_l2tp_rcvdata(hook_p hook, item_p item)
111558b02238SNuno Antunes {
111658b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
111758b02238SNuno Antunes const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
111858b02238SNuno Antunes struct mbuf *m;
111958b02238SNuno Antunes u_int16_t hdr;
112058b02238SNuno Antunes int error;
112158b02238SNuno Antunes int i = 1;
112258b02238SNuno Antunes
112358b02238SNuno Antunes /* Sanity check */
112458b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
112558b02238SNuno Antunes
112658b02238SNuno Antunes /* If not configured, reject */
112758b02238SNuno Antunes if (!priv->conf.enabled) {
112858b02238SNuno Antunes NG_FREE_ITEM(item);
112958b02238SNuno Antunes ERROUT(ENXIO);
113058b02238SNuno Antunes }
113158b02238SNuno Antunes
113258b02238SNuno Antunes /* Get mbuf */
113358b02238SNuno Antunes NGI_GET_M(item, m);
113458b02238SNuno Antunes
113558b02238SNuno Antunes /* Check max length */
113658b02238SNuno Antunes if (m->m_pkthdr.len >= 0x10000 - 12) {
113758b02238SNuno Antunes priv->stats.xmitDataTooBig++;
113858b02238SNuno Antunes NG_FREE_ITEM(item);
113958b02238SNuno Antunes NG_FREE_M(m);
114058b02238SNuno Antunes ERROUT(EOVERFLOW);
114158b02238SNuno Antunes }
114258b02238SNuno Antunes
114358b02238SNuno Antunes /* Prepend L2TP header */
114458b02238SNuno Antunes M_PREPEND(m, 6
114558b02238SNuno Antunes + (2 * (hpriv->conf.include_length != 0))
114658b02238SNuno Antunes + (4 * (hpriv->conf.enable_dseq != 0)),
1147b5523eacSSascha Wildner M_NOWAIT);
114858b02238SNuno Antunes if (m == NULL) {
114958b02238SNuno Antunes priv->stats.memoryFailures++;
115058b02238SNuno Antunes NG_FREE_ITEM(item);
115158b02238SNuno Antunes ERROUT(ENOBUFS);
115258b02238SNuno Antunes }
115358b02238SNuno Antunes hdr = L2TP_DATA_HDR;
115458b02238SNuno Antunes if (hpriv->conf.include_length) {
115558b02238SNuno Antunes hdr |= L2TP_HDR_LEN;
115658b02238SNuno Antunes mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
115758b02238SNuno Antunes }
115858b02238SNuno Antunes mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
115958b02238SNuno Antunes mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
116058b02238SNuno Antunes if (hpriv->conf.enable_dseq) {
116158b02238SNuno Antunes hdr |= L2TP_HDR_SEQ;
116258b02238SNuno Antunes mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
116358b02238SNuno Antunes mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
116458b02238SNuno Antunes hpriv->ns++;
116558b02238SNuno Antunes }
116658b02238SNuno Antunes mtod(m, u_int16_t *)[0] = htons(hdr);
116758b02238SNuno Antunes
116858b02238SNuno Antunes /* Update per session stats. */
116958b02238SNuno Antunes hpriv->stats.xmitPackets++;
117058b02238SNuno Antunes hpriv->stats.xmitOctets += m->m_pkthdr.len;
117158b02238SNuno Antunes
117258b02238SNuno Antunes /* And the global one. */
117358b02238SNuno Antunes priv->stats.xmitPackets++;
117458b02238SNuno Antunes priv->stats.xmitOctets += m->m_pkthdr.len;
117558b02238SNuno Antunes
117658b02238SNuno Antunes /* Send packet */
117758b02238SNuno Antunes NG_FWD_NEW_DATA(error, item, priv->lower, m);
117858b02238SNuno Antunes done:
117958b02238SNuno Antunes /* Done */
118058b02238SNuno Antunes L2TP_SEQ_CHECK(&priv->seq);
118158b02238SNuno Antunes return (error);
118258b02238SNuno Antunes }
118358b02238SNuno Antunes
118458b02238SNuno Antunes /*
118558b02238SNuno Antunes * Send a message to our controlling node that we've failed.
118658b02238SNuno Antunes */
118758b02238SNuno Antunes static void
ng_l2tp_seq_failure(priv_p priv)118858b02238SNuno Antunes ng_l2tp_seq_failure(priv_p priv)
118958b02238SNuno Antunes {
119058b02238SNuno Antunes struct ng_mesg *msg;
119158b02238SNuno Antunes int error;
119258b02238SNuno Antunes
119358b02238SNuno Antunes NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_WAITOK | M_NULLOK);
119458b02238SNuno Antunes if (msg == NULL)
119558b02238SNuno Antunes return;
119658b02238SNuno Antunes NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
119758b02238SNuno Antunes }
119858b02238SNuno Antunes
119958b02238SNuno Antunes /************************************************************************
120058b02238SNuno Antunes SEQUENCE NUMBER HANDLING
120158b02238SNuno Antunes ************************************************************************/
120258b02238SNuno Antunes
120358b02238SNuno Antunes /*
120458b02238SNuno Antunes * Initialize sequence number state.
120558b02238SNuno Antunes */
120658b02238SNuno Antunes static void
ng_l2tp_seq_init(priv_p priv)120758b02238SNuno Antunes ng_l2tp_seq_init(priv_p priv)
120858b02238SNuno Antunes {
120958b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
121058b02238SNuno Antunes
121158b02238SNuno Antunes KASSERT(priv->conf.peer_win >= 1,
121258b02238SNuno Antunes ("%s: peer_win is zero", __func__));
121358b02238SNuno Antunes memset(seq, 0, sizeof(*seq));
121458b02238SNuno Antunes seq->cwnd = 1;
121558b02238SNuno Antunes seq->wmax = priv->conf.peer_win;
121658b02238SNuno Antunes if (seq->wmax > L2TP_MAX_XWIN)
121758b02238SNuno Antunes seq->wmax = L2TP_MAX_XWIN;
121858b02238SNuno Antunes seq->ssth = seq->wmax;
121958b02238SNuno Antunes ng_callout_init(&seq->rack_timer);
122058b02238SNuno Antunes ng_callout_init(&seq->xack_timer);
1221c0095626SSascha Wildner mtx_init(&seq->mtx, "ng_l2tp");
122258b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
122358b02238SNuno Antunes }
122458b02238SNuno Antunes
122558b02238SNuno Antunes /*
122658b02238SNuno Antunes * Set sequence number state as given from user.
122758b02238SNuno Antunes */
122858b02238SNuno Antunes static int
ng_l2tp_seq_set(priv_p priv,const struct ng_l2tp_seq_config * conf)122958b02238SNuno Antunes ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
123058b02238SNuno Antunes {
123158b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
123258b02238SNuno Antunes
123358b02238SNuno Antunes /* If node is enabled, deny update to sequence numbers. */
123458b02238SNuno Antunes if (priv->conf.enabled)
123558b02238SNuno Antunes return (EBUSY);
123658b02238SNuno Antunes
123758b02238SNuno Antunes /* We only can handle the simple cases. */
123858b02238SNuno Antunes if (conf->xack != conf->nr || conf->ns != conf->rack)
123958b02238SNuno Antunes return (EINVAL);
124058b02238SNuno Antunes
124158b02238SNuno Antunes /* Set ns,nr,rack,xack parameters. */
124258b02238SNuno Antunes seq->ns = conf->ns;
124358b02238SNuno Antunes seq->nr = conf->nr;
124458b02238SNuno Antunes seq->rack = conf->rack;
124558b02238SNuno Antunes seq->xack = conf->xack;
124658b02238SNuno Antunes
124758b02238SNuno Antunes return (0);
124858b02238SNuno Antunes }
124958b02238SNuno Antunes
125058b02238SNuno Antunes /*
125158b02238SNuno Antunes * Adjust sequence number state accordingly after reconfiguration.
125258b02238SNuno Antunes */
125358b02238SNuno Antunes static int
ng_l2tp_seq_adjust(priv_p priv,const struct ng_l2tp_config * conf)125458b02238SNuno Antunes ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
125558b02238SNuno Antunes {
125658b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
125758b02238SNuno Antunes u_int16_t new_wmax;
125858b02238SNuno Antunes
125958b02238SNuno Antunes /* If disabling node, reset state sequence number */
126058b02238SNuno Antunes if (!conf->enabled) {
126158b02238SNuno Antunes ng_l2tp_seq_reset(priv);
126258b02238SNuno Antunes return (0);
126358b02238SNuno Antunes }
126458b02238SNuno Antunes
126558b02238SNuno Antunes /* Adjust peer's max recv window; it can only increase */
126658b02238SNuno Antunes new_wmax = conf->peer_win;
126758b02238SNuno Antunes if (new_wmax > L2TP_MAX_XWIN)
126858b02238SNuno Antunes new_wmax = L2TP_MAX_XWIN;
126958b02238SNuno Antunes if (new_wmax == 0)
127058b02238SNuno Antunes return (EINVAL);
127158b02238SNuno Antunes if (new_wmax < seq->wmax)
127258b02238SNuno Antunes return (EBUSY);
127358b02238SNuno Antunes seq->wmax = new_wmax;
127458b02238SNuno Antunes
127558b02238SNuno Antunes /* Done */
127658b02238SNuno Antunes return (0);
127758b02238SNuno Antunes }
127858b02238SNuno Antunes
127958b02238SNuno Antunes /*
128058b02238SNuno Antunes * Reset sequence number state.
128158b02238SNuno Antunes */
128258b02238SNuno Antunes static void
ng_l2tp_seq_reset(priv_p priv)128358b02238SNuno Antunes ng_l2tp_seq_reset(priv_p priv)
128458b02238SNuno Antunes {
128558b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
128658b02238SNuno Antunes hook_p hook;
128758b02238SNuno Antunes int i;
128858b02238SNuno Antunes
128958b02238SNuno Antunes /* Sanity check */
129058b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
129158b02238SNuno Antunes
129258b02238SNuno Antunes /* Stop timers */
129358b02238SNuno Antunes ng_uncallout(&seq->rack_timer, priv->node);
129458b02238SNuno Antunes ng_uncallout(&seq->xack_timer, priv->node);
129558b02238SNuno Antunes
129658b02238SNuno Antunes /* Free retransmit queue */
129758b02238SNuno Antunes for (i = 0; i < L2TP_MAX_XWIN; i++) {
129858b02238SNuno Antunes if (seq->xwin[i] == NULL)
129958b02238SNuno Antunes break;
130058b02238SNuno Antunes m_freem(seq->xwin[i]);
130158b02238SNuno Antunes }
130258b02238SNuno Antunes
130358b02238SNuno Antunes /* Reset session hooks' sequence number states */
130458b02238SNuno Antunes NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
130558b02238SNuno Antunes
130658b02238SNuno Antunes /* Reset node's sequence number state */
130758b02238SNuno Antunes seq->ns = 0;
130858b02238SNuno Antunes seq->nr = 0;
130958b02238SNuno Antunes seq->rack = 0;
131058b02238SNuno Antunes seq->xack = 0;
131158b02238SNuno Antunes seq->wmax = L2TP_MAX_XWIN;
131258b02238SNuno Antunes seq->cwnd = 1;
131358b02238SNuno Antunes seq->ssth = seq->wmax;
131458b02238SNuno Antunes seq->acks = 0;
131558b02238SNuno Antunes seq->rexmits = 0;
131658b02238SNuno Antunes bzero(seq->xwin, sizeof(seq->xwin));
131758b02238SNuno Antunes
131858b02238SNuno Antunes /* Done */
131958b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
132058b02238SNuno Antunes }
132158b02238SNuno Antunes
132258b02238SNuno Antunes /*
132358b02238SNuno Antunes * Handle receipt of an acknowledgement value (Nr) from peer.
132458b02238SNuno Antunes */
132558b02238SNuno Antunes static void
ng_l2tp_seq_recv_nr(priv_p priv,u_int16_t nr)132658b02238SNuno Antunes ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
132758b02238SNuno Antunes {
132858b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
132958b02238SNuno Antunes struct mbuf *xwin[L2TP_MAX_XWIN]; /* partial local copy */
133058b02238SNuno Antunes int nack;
133158b02238SNuno Antunes int i, j;
133258b02238SNuno Antunes uint16_t ns;
133358b02238SNuno Antunes
133458b02238SNuno Antunes mtx_lock(&seq->mtx);
133558b02238SNuno Antunes
133658b02238SNuno Antunes /* Verify peer's ACK is in range */
133758b02238SNuno Antunes if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
133858b02238SNuno Antunes mtx_unlock(&seq->mtx);
133958b02238SNuno Antunes return; /* duplicate ack */
134058b02238SNuno Antunes }
134158b02238SNuno Antunes if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
134258b02238SNuno Antunes mtx_unlock(&seq->mtx);
134358b02238SNuno Antunes priv->stats.recvBadAcks++; /* ack for packet not sent */
134458b02238SNuno Antunes return;
134558b02238SNuno Antunes }
134658b02238SNuno Antunes KASSERT(nack <= L2TP_MAX_XWIN,
134758b02238SNuno Antunes ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
134858b02238SNuno Antunes
134958b02238SNuno Antunes /* Update receive ack stats */
135058b02238SNuno Antunes seq->rack = nr;
135158b02238SNuno Antunes seq->rexmits = 0;
135258b02238SNuno Antunes
135358b02238SNuno Antunes /* Free acknowledged packets and shift up packets in the xmit queue */
135458b02238SNuno Antunes for (i = 0; i < nack; i++)
135558b02238SNuno Antunes m_freem(seq->xwin[i]);
135658b02238SNuno Antunes memmove(seq->xwin, seq->xwin + nack,
135758b02238SNuno Antunes (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
135858b02238SNuno Antunes memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
135958b02238SNuno Antunes nack * sizeof(*seq->xwin));
136058b02238SNuno Antunes
136158b02238SNuno Antunes /*
136258b02238SNuno Antunes * Do slow-start/congestion avoidance windowing algorithm described
136358b02238SNuno Antunes * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
136458b02238SNuno Antunes * ACK had arrived separately.
136558b02238SNuno Antunes */
136658b02238SNuno Antunes if (seq->cwnd < seq->wmax) {
136758b02238SNuno Antunes
136858b02238SNuno Antunes /* Handle slow start phase */
136958b02238SNuno Antunes if (seq->cwnd < seq->ssth) {
137058b02238SNuno Antunes seq->cwnd += nack;
137158b02238SNuno Antunes nack = 0;
137258b02238SNuno Antunes if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
137358b02238SNuno Antunes nack = seq->cwnd - seq->ssth;
137458b02238SNuno Antunes seq->cwnd = seq->ssth;
137558b02238SNuno Antunes }
137658b02238SNuno Antunes }
137758b02238SNuno Antunes
137858b02238SNuno Antunes /* Handle congestion avoidance phase */
137958b02238SNuno Antunes if (seq->cwnd >= seq->ssth) {
138058b02238SNuno Antunes seq->acks += nack;
138158b02238SNuno Antunes while (seq->acks >= seq->cwnd) {
138258b02238SNuno Antunes seq->acks -= seq->cwnd;
138358b02238SNuno Antunes if (seq->cwnd < seq->wmax)
138458b02238SNuno Antunes seq->cwnd++;
138558b02238SNuno Antunes }
138658b02238SNuno Antunes }
138758b02238SNuno Antunes }
138858b02238SNuno Antunes
138958b02238SNuno Antunes /* Stop xmit timer */
139058b02238SNuno Antunes if (callout_active(&seq->rack_timer))
139158b02238SNuno Antunes ng_uncallout(&seq->rack_timer, priv->node);
139258b02238SNuno Antunes
139358b02238SNuno Antunes /* If transmit queue is empty, we're done for now */
139458b02238SNuno Antunes if (seq->xwin[0] == NULL) {
139558b02238SNuno Antunes mtx_unlock(&seq->mtx);
139658b02238SNuno Antunes return;
139758b02238SNuno Antunes }
139858b02238SNuno Antunes
139958b02238SNuno Antunes /* Start restransmit timer again */
140058b02238SNuno Antunes ng_callout(&seq->rack_timer, priv->node, NULL,
140158b02238SNuno Antunes hz, ng_l2tp_seq_rack_timeout, NULL, 0);
140258b02238SNuno Antunes
140358b02238SNuno Antunes /*
140458b02238SNuno Antunes * Send more packets, trying to keep peer's receive window full.
140558b02238SNuno Antunes * Make copy of everything we need before lock release.
140658b02238SNuno Antunes */
140758b02238SNuno Antunes ns = seq->ns;
140858b02238SNuno Antunes j = 0;
140958b02238SNuno Antunes while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
141058b02238SNuno Antunes && seq->xwin[i] != NULL) {
141158b02238SNuno Antunes xwin[j++] = seq->xwin[i];
141258b02238SNuno Antunes seq->ns++;
141358b02238SNuno Antunes }
141458b02238SNuno Antunes
141558b02238SNuno Antunes mtx_unlock(&seq->mtx);
141658b02238SNuno Antunes
141758b02238SNuno Antunes /*
141858b02238SNuno Antunes * Send prepared.
141958b02238SNuno Antunes * If there is a memory error, pretend packet was sent, as it
142058b02238SNuno Antunes * will get retransmitted later anyway.
142158b02238SNuno Antunes */
142258b02238SNuno Antunes for (i = 0; i < j; i++) {
142358b02238SNuno Antunes struct mbuf *m;
1424b5523eacSSascha Wildner if ((m = L2TP_COPY_MBUF(xwin[i], M_NOWAIT)) == NULL)
142558b02238SNuno Antunes priv->stats.memoryFailures++;
142658b02238SNuno Antunes else
142758b02238SNuno Antunes ng_l2tp_xmit_ctrl(priv, m, ns);
142858b02238SNuno Antunes ns++;
142958b02238SNuno Antunes }
143058b02238SNuno Antunes }
143158b02238SNuno Antunes
143258b02238SNuno Antunes /*
143358b02238SNuno Antunes * Handle an ack timeout. We have an outstanding ack that we
143458b02238SNuno Antunes * were hoping to piggy-back, but haven't, so send a ZLB.
143558b02238SNuno Antunes */
143658b02238SNuno Antunes static void
ng_l2tp_seq_xack_timeout(node_p node,hook_p hook,void * arg1,int arg2)143758b02238SNuno Antunes ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
143858b02238SNuno Antunes {
143958b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
144058b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
144158b02238SNuno Antunes
144258b02238SNuno Antunes /* Make sure callout is still active before doing anything */
144358b02238SNuno Antunes if (callout_pending(&seq->xack_timer) ||
144458b02238SNuno Antunes (!callout_active(&seq->xack_timer)))
144558b02238SNuno Antunes return;
144658b02238SNuno Antunes
144758b02238SNuno Antunes /* Sanity check */
144858b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
144958b02238SNuno Antunes
145058b02238SNuno Antunes /* Send a ZLB */
145158b02238SNuno Antunes ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
145258b02238SNuno Antunes
145358b02238SNuno Antunes /* callout_deactivate() is not needed here
145458b02238SNuno Antunes as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
145558b02238SNuno Antunes
145658b02238SNuno Antunes /* Sanity check */
145758b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
145858b02238SNuno Antunes }
145958b02238SNuno Antunes
146058b02238SNuno Antunes /*
146158b02238SNuno Antunes * Handle a transmit timeout. The peer has failed to respond
146258b02238SNuno Antunes * with an ack for our packet, so retransmit it.
146358b02238SNuno Antunes */
146458b02238SNuno Antunes static void
ng_l2tp_seq_rack_timeout(node_p node,hook_p hook,void * arg1,int arg2)146558b02238SNuno Antunes ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
146658b02238SNuno Antunes {
146758b02238SNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
146858b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
146958b02238SNuno Antunes struct mbuf *m;
147058b02238SNuno Antunes u_int delay;
147158b02238SNuno Antunes
147258b02238SNuno Antunes /* Make sure callout is still active before doing anything */
147358b02238SNuno Antunes if (callout_pending(&seq->rack_timer) ||
147458b02238SNuno Antunes (!callout_active(&seq->rack_timer)))
147558b02238SNuno Antunes return;
147658b02238SNuno Antunes
147758b02238SNuno Antunes /* Sanity check */
147858b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
147958b02238SNuno Antunes
148058b02238SNuno Antunes priv->stats.xmitRetransmits++;
148158b02238SNuno Antunes
148258b02238SNuno Antunes /* Have we reached the retransmit limit? If so, notify owner. */
148358b02238SNuno Antunes if (seq->rexmits++ >= priv->conf.rexmit_max)
148458b02238SNuno Antunes ng_l2tp_seq_failure(priv);
148558b02238SNuno Antunes
148658b02238SNuno Antunes /* Restart timer, this time with an increased delay */
148758b02238SNuno Antunes delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
148858b02238SNuno Antunes if (delay > priv->conf.rexmit_max_to)
148958b02238SNuno Antunes delay = priv->conf.rexmit_max_to;
149058b02238SNuno Antunes ng_callout(&seq->rack_timer, node, NULL,
149158b02238SNuno Antunes hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
149258b02238SNuno Antunes
149358b02238SNuno Antunes /* Do slow-start/congestion algorithm windowing algorithm */
149458b02238SNuno Antunes seq->ns = seq->rack;
149558b02238SNuno Antunes seq->ssth = (seq->cwnd + 1) / 2;
149658b02238SNuno Antunes seq->cwnd = 1;
149758b02238SNuno Antunes seq->acks = 0;
149858b02238SNuno Antunes
149958b02238SNuno Antunes /* Retransmit oldest unack'd packet */
1500b5523eacSSascha Wildner if ((m = L2TP_COPY_MBUF(seq->xwin[0], M_NOWAIT)) == NULL)
150158b02238SNuno Antunes priv->stats.memoryFailures++;
150258b02238SNuno Antunes else
150358b02238SNuno Antunes ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
150458b02238SNuno Antunes
150558b02238SNuno Antunes /* callout_deactivate() is not needed here
150658b02238SNuno Antunes as ng_callout() is getting called each time */
150758b02238SNuno Antunes
150858b02238SNuno Antunes /* Sanity check */
150958b02238SNuno Antunes L2TP_SEQ_CHECK(seq);
151058b02238SNuno Antunes }
151158b02238SNuno Antunes
151258b02238SNuno Antunes /*
151358b02238SNuno Antunes * Transmit a control stream packet, payload optional.
151458b02238SNuno Antunes * The transmit sequence number is not incremented.
151558b02238SNuno Antunes */
151658b02238SNuno Antunes static int
ng_l2tp_xmit_ctrl(priv_p priv,struct mbuf * m,u_int16_t ns)151758b02238SNuno Antunes ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
151858b02238SNuno Antunes {
151958b02238SNuno Antunes struct l2tp_seq *const seq = &priv->seq;
152058b02238SNuno Antunes u_int16_t session_id = 0;
152158b02238SNuno Antunes int error;
152258b02238SNuno Antunes
152358b02238SNuno Antunes mtx_lock(&seq->mtx);
152458b02238SNuno Antunes
152558b02238SNuno Antunes /* Stop ack timer: we're sending an ack with this packet.
152658b02238SNuno Antunes Doing this before to keep state predictable after error. */
152758b02238SNuno Antunes if (callout_active(&seq->xack_timer))
152858b02238SNuno Antunes ng_uncallout(&seq->xack_timer, priv->node);
152958b02238SNuno Antunes
153058b02238SNuno Antunes seq->xack = seq->nr;
153158b02238SNuno Antunes
153258b02238SNuno Antunes mtx_unlock(&seq->mtx);
153358b02238SNuno Antunes
153458b02238SNuno Antunes /* If no mbuf passed, send an empty packet (ZLB) */
153558b02238SNuno Antunes if (m == NULL) {
153658b02238SNuno Antunes
153758b02238SNuno Antunes /* Create a new mbuf for ZLB packet */
1538b5523eacSSascha Wildner MGETHDR(m, M_NOWAIT, MT_DATA);
153958b02238SNuno Antunes if (m == NULL) {
154058b02238SNuno Antunes priv->stats.memoryFailures++;
154158b02238SNuno Antunes return (ENOBUFS);
154258b02238SNuno Antunes }
154358b02238SNuno Antunes m->m_len = m->m_pkthdr.len = 12;
154458b02238SNuno Antunes m->m_pkthdr.rcvif = NULL;
154558b02238SNuno Antunes priv->stats.xmitZLBs++;
154658b02238SNuno Antunes } else {
154758b02238SNuno Antunes
154858b02238SNuno Antunes /* Strip off session ID */
154958b02238SNuno Antunes if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
155058b02238SNuno Antunes priv->stats.memoryFailures++;
155158b02238SNuno Antunes return (ENOBUFS);
155258b02238SNuno Antunes }
155358b02238SNuno Antunes memcpy(&session_id, mtod(m, u_int16_t *), 2);
155458b02238SNuno Antunes m_adj(m, 2);
155558b02238SNuno Antunes
155658b02238SNuno Antunes /* Make room for L2TP header */
1557b5523eacSSascha Wildner M_PREPEND(m, 12, M_NOWAIT);
155858b02238SNuno Antunes if (m == NULL) {
155958b02238SNuno Antunes priv->stats.memoryFailures++;
156058b02238SNuno Antunes return (ENOBUFS);
156158b02238SNuno Antunes }
156258b02238SNuno Antunes }
156358b02238SNuno Antunes
156458b02238SNuno Antunes /* Fill in L2TP header */
156558b02238SNuno Antunes mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
156658b02238SNuno Antunes mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
156758b02238SNuno Antunes mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
156858b02238SNuno Antunes mtod(m, u_int16_t *)[3] = session_id;
156958b02238SNuno Antunes mtod(m, u_int16_t *)[4] = htons(ns);
157058b02238SNuno Antunes mtod(m, u_int16_t *)[5] = htons(seq->nr);
157158b02238SNuno Antunes
157258b02238SNuno Antunes /* Update sequence number info and stats */
157358b02238SNuno Antunes priv->stats.xmitPackets++;
157458b02238SNuno Antunes priv->stats.xmitOctets += m->m_pkthdr.len;
157558b02238SNuno Antunes
157658b02238SNuno Antunes /* Send packet */
157758b02238SNuno Antunes NG_SEND_DATA_ONLY(error, priv->lower, m);
157858b02238SNuno Antunes return (error);
157958b02238SNuno Antunes }
158058b02238SNuno Antunes
158158b02238SNuno Antunes #ifdef INVARIANTS
158258b02238SNuno Antunes /*
158358b02238SNuno Antunes * Sanity check sequence number state.
158458b02238SNuno Antunes */
158558b02238SNuno Antunes static void
ng_l2tp_seq_check(struct l2tp_seq * seq)158658b02238SNuno Antunes ng_l2tp_seq_check(struct l2tp_seq *seq)
158758b02238SNuno Antunes {
158858b02238SNuno Antunes int self_unack, peer_unack;
158958b02238SNuno Antunes int i;
159058b02238SNuno Antunes
159158b02238SNuno Antunes #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
159258b02238SNuno Antunes
159358b02238SNuno Antunes mtx_lock(&seq->mtx);
159458b02238SNuno Antunes
159558b02238SNuno Antunes self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
159658b02238SNuno Antunes peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
159758b02238SNuno Antunes CHECK(seq->wmax <= L2TP_MAX_XWIN);
159858b02238SNuno Antunes CHECK(seq->cwnd >= 1);
159958b02238SNuno Antunes CHECK(seq->cwnd <= seq->wmax);
160058b02238SNuno Antunes CHECK(seq->ssth >= 1);
160158b02238SNuno Antunes CHECK(seq->ssth <= seq->wmax);
160258b02238SNuno Antunes if (seq->cwnd < seq->ssth)
160358b02238SNuno Antunes CHECK(seq->acks == 0);
160458b02238SNuno Antunes else
160558b02238SNuno Antunes CHECK(seq->acks <= seq->cwnd);
160658b02238SNuno Antunes CHECK(self_unack >= 0);
160758b02238SNuno Antunes CHECK(peer_unack >= 0);
160858b02238SNuno Antunes CHECK(peer_unack <= seq->wmax);
160958b02238SNuno Antunes CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
161058b02238SNuno Antunes CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
161158b02238SNuno Antunes for (i = 0; i < peer_unack; i++)
161258b02238SNuno Antunes CHECK(seq->xwin[i] != NULL);
161358b02238SNuno Antunes for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
161458b02238SNuno Antunes CHECK(seq->xwin[i] == NULL);
161558b02238SNuno Antunes
161658b02238SNuno Antunes mtx_unlock(&seq->mtx);
161758b02238SNuno Antunes
161858b02238SNuno Antunes #undef CHECK
161958b02238SNuno Antunes }
162058b02238SNuno Antunes #endif /* INVARIANTS */
1621