2 * Copyright (c) 2001-2002 Packet Design, LLC.
5 * Subject to the following obligations and disclaimer of warranty,
6 * use and redistribution of this software, in source or object code
7 * forms, with or without modifications are expressly permitted by
8 * Packet Design; provided, however, that:
10 * (i) Any and all reproductions of the source or object code
11 * must include the copyright notice above and the following
12 * disclaimer of warranties; and
13 * (ii) No rights are granted, in any manner or form, to use
14 * Packet Design trademarks, including the mark "PACKET DESIGN"
15 * on advertising, endorsements, or otherwise except as such
16 * appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
21 * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
22 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
23 * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
24 * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
25 * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
26 * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
27 * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
28 * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
30 * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
31 * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34 * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_l2tp.c,v 1.25 2008/03/16 21:33:12 mav Exp $
40 * $DragonFly: src/sys/netgraph7/ng_l2tp.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
44 * L2TP netgraph node type.
46 * This node type implements the lower layer of the
47 * L2TP protocol as specified in RFC 2661.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/errno.h>
58 #include <sys/libkern.h>
60 #include "ng_message.h"
65 #ifdef NG_SEPARATE_MALLOC
66 MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
68 #define M_NETGRAPH_L2TP M_NETGRAPH
71 /* L2TP header format (first 2 bytes only) */
72 #define L2TP_HDR_CTRL 0x8000 /* control packet */
73 #define L2TP_HDR_LEN 0x4000 /* has length field */
74 #define L2TP_HDR_SEQ 0x0800 /* has ns, nr fields */
75 #define L2TP_HDR_OFF 0x0200 /* has offset field */
76 #define L2TP_HDR_PRIO 0x0100 /* give priority */
77 #define L2TP_HDR_VERS_MASK 0x000f /* version field mask */
78 #define L2TP_HDR_VERSION 0x0002 /* version field */
80 /* Bits that must be zero or one in first two bytes of header */
81 #define L2TP_CTRL_0BITS 0x030d /* ctrl: must be 0 */
82 #define L2TP_CTRL_1BITS 0xc802 /* ctrl: must be 1 */
83 #define L2TP_DATA_0BITS 0x800d /* data: must be 0 */
84 #define L2TP_DATA_1BITS 0x0002 /* data: must be 1 */
86 /* Standard xmit ctrl and data header bits */
87 #define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
88 | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
89 #define L2TP_DATA_HDR (L2TP_HDR_VERSION) /* optional: len, seq */
91 /* Some hard coded values */
92 #define L2TP_MAX_XWIN 128 /* my max xmit window */
93 #define L2TP_MAX_REXMIT 5 /* default max rexmit */
94 #define L2TP_MAX_REXMIT_TO 30 /* default rexmit to */
95 #define L2TP_DELAYED_ACK ((hz + 19) / 20) /* delayed ack: 50 ms */
97 /* Default data sequence number configuration for new sessions */
98 #define L2TP_CONTROL_DSEQ 1 /* we are the lns */
99 #define L2TP_ENABLE_DSEQ 1 /* enable data seq # */
101 /* Compare sequence numbers using circular math */
102 #define L2TP_SEQ_DIFF(x, y) ((int)((int16_t)(x) - (int16_t)(y)))
104 #define SESSHASHSIZE 0x0020
105 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
107 /* Hook private data (data session hooks only) */
108 struct ng_l2tp_hook_private {
109 struct ng_l2tp_sess_config conf; /* hook/session config */
110 struct ng_l2tp_session_stats stats; /* per sessions statistics */
111 hook_p hook; /* hook reference */
112 u_int16_t ns; /* data ns sequence number */
113 u_int16_t nr; /* data nr sequence number */
114 LIST_ENTRY(ng_l2tp_hook_private) sessions;
116 typedef struct ng_l2tp_hook_private *hookpriv_p;
119 * Sequence number state
122 * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
123 * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
124 * - The first (ns - rack) mbuf's in xwin[] array are copies of these
125 * unacknowledged packets; the remainder of xwin[] consists first of
126 * zero or more further untransmitted packets in the transmit queue
127 * - We try to keep the peer's receive window as full as possible.
128 * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
129 * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
130 * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
131 * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
134 u_int16_t ns; /* next xmit seq we send */
135 u_int16_t nr; /* next recv seq we expect */
136 u_int16_t inproc; /* packet is in processing */
137 u_int16_t rack; /* last 'nr' we rec'd */
138 u_int16_t xack; /* last 'nr' we sent */
139 u_int16_t wmax; /* peer's max recv window */
140 u_int16_t cwnd; /* current congestion window */
141 u_int16_t ssth; /* slow start threshold */
142 u_int16_t acks; /* # consecutive acks rec'd */
143 u_int16_t rexmits; /* # retransmits sent */
144 struct callout rack_timer; /* retransmit timer */
145 struct callout xack_timer; /* delayed ack timer */
146 struct mbuf *xwin[L2TP_MAX_XWIN]; /* transmit window */
147 struct mtx mtx; /* seq mutex */
150 /* Node private data */
151 struct ng_l2tp_private {
152 node_p node; /* back pointer to node */
153 hook_p ctrl; /* hook to upper layers */
154 hook_p lower; /* hook to lower layers */
155 struct ng_l2tp_config conf; /* node configuration */
156 struct ng_l2tp_stats stats; /* node statistics */
157 struct l2tp_seq seq; /* ctrl sequence number state */
158 ng_ID_t ftarget; /* failure message target */
159 LIST_HEAD(, ng_l2tp_hook_private) sesshash[SESSHASHSIZE];
161 typedef struct ng_l2tp_private *priv_p;
163 /* Netgraph node methods */
164 static ng_constructor_t ng_l2tp_constructor;
165 static ng_rcvmsg_t ng_l2tp_rcvmsg;
166 static ng_shutdown_t ng_l2tp_shutdown;
167 static ng_newhook_t ng_l2tp_newhook;
168 static ng_rcvdata_t ng_l2tp_rcvdata;
169 static ng_rcvdata_t ng_l2tp_rcvdata_lower;
170 static ng_rcvdata_t ng_l2tp_rcvdata_ctrl;
171 static ng_disconnect_t ng_l2tp_disconnect;
173 /* Internal functions */
174 static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
176 static void ng_l2tp_seq_init(priv_p priv);
177 static int ng_l2tp_seq_set(priv_p priv,
178 const struct ng_l2tp_seq_config *conf);
179 static int ng_l2tp_seq_adjust(priv_p priv,
180 const struct ng_l2tp_config *conf);
181 static void ng_l2tp_seq_reset(priv_p priv);
182 static void ng_l2tp_seq_failure(priv_p priv);
183 static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
184 static void ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
185 void *arg1, int arg2);
186 static void ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
187 void *arg1, int arg2);
189 static hookpriv_p ng_l2tp_find_session(priv_p privp, u_int16_t sid);
190 static ng_fn_eachhook ng_l2tp_reset_session;
193 static void ng_l2tp_seq_check(struct l2tp_seq *seq);
196 /* Parse type for struct ng_l2tp_seq_config. */
197 static const struct ng_parse_struct_field
198 ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
199 static const struct ng_parse_type ng_l2tp_seq_config_type = {
200 &ng_parse_struct_type,
201 &ng_l2tp_seq_config_fields
204 /* Parse type for struct ng_l2tp_config */
205 static const struct ng_parse_struct_field
206 ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
207 static const struct ng_parse_type ng_l2tp_config_type = {
208 &ng_parse_struct_type,
209 &ng_l2tp_config_type_fields,
212 /* Parse type for struct ng_l2tp_sess_config */
213 static const struct ng_parse_struct_field
214 ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
215 static const struct ng_parse_type ng_l2tp_sess_config_type = {
216 &ng_parse_struct_type,
217 &ng_l2tp_sess_config_type_fields,
220 /* Parse type for struct ng_l2tp_stats */
221 static const struct ng_parse_struct_field
222 ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
223 static const struct ng_parse_type ng_l2tp_stats_type = {
224 &ng_parse_struct_type,
225 &ng_l2tp_stats_type_fields
228 /* Parse type for struct ng_l2tp_session_stats. */
229 static const struct ng_parse_struct_field
230 ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
231 static const struct ng_parse_type ng_l2tp_session_stats_type = {
232 &ng_parse_struct_type,
233 &ng_l2tp_session_stats_type_fields
236 /* List of commands and how to convert arguments to/from ASCII */
237 static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
242 &ng_l2tp_config_type,
254 NGM_L2TP_SET_SESS_CONFIG,
256 &ng_l2tp_sess_config_type,
261 NGM_L2TP_GET_SESS_CONFIG,
263 &ng_parse_hint16_type,
264 &ng_l2tp_sess_config_type
282 NGM_L2TP_GETCLR_STATS,
289 NGM_L2TP_GET_SESSION_STATS,
291 &ng_parse_int16_type,
292 &ng_l2tp_session_stats_type
296 NGM_L2TP_CLR_SESSION_STATS,
298 &ng_parse_int16_type,
303 NGM_L2TP_GETCLR_SESSION_STATS,
305 &ng_parse_int16_type,
306 &ng_l2tp_session_stats_type
310 NGM_L2TP_ACK_FAILURE,
319 &ng_l2tp_seq_config_type,
325 /* Node type descriptor */
326 static struct ng_type ng_l2tp_typestruct = {
327 .version = NG_ABI_VERSION,
328 .name = NG_L2TP_NODE_TYPE,
329 .constructor = ng_l2tp_constructor,
330 .rcvmsg = ng_l2tp_rcvmsg,
331 .shutdown = ng_l2tp_shutdown,
332 .newhook = ng_l2tp_newhook,
333 .rcvdata = ng_l2tp_rcvdata,
334 .disconnect = ng_l2tp_disconnect,
335 .cmdlist = ng_l2tp_cmdlist,
337 NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
339 /* Sequence number state sanity checking */
341 #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
343 #define L2TP_SEQ_CHECK(x) do { } while (0)
347 #define memmove(d, s, l) bcopy(s, d, l)
349 /* Whether to use m_copypacket() or m_dup() */
350 #define L2TP_COPY_MBUF m_copypacket
352 #define ERROUT(x) do { error = (x); goto done; } while (0)
354 /************************************************************************
356 ************************************************************************/
359 * Node type constructor
362 ng_l2tp_constructor(node_p node)
367 /* Allocate private structure */
368 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_L2TP, M_WAITOK | M_NULLOK | M_ZERO);
371 NG_NODE_SET_PRIVATE(node, priv);
374 /* Apply a semi-reasonable default configuration */
375 priv->conf.peer_win = 1;
376 priv->conf.rexmit_max = L2TP_MAX_REXMIT;
377 priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
379 /* Initialize sequence number state */
380 ng_l2tp_seq_init(priv);
382 for (i = 0; i < SESSHASHSIZE; i++)
383 LIST_INIT(&priv->sesshash[i]);
390 * Give our OK for a hook to be added.
393 ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
395 const priv_p priv = NG_NODE_PRIVATE(node);
397 /* Check hook name */
398 if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
399 if (priv->ctrl != NULL)
402 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
403 } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
404 if (priv->lower != NULL)
407 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
409 static const char hexdig[16] = "0123456789abcdef";
410 u_int16_t session_id;
417 /* Parse hook name to get session ID */
418 if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
419 sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
421 hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
422 for (session_id = i = 0; i < 4; i++) {
423 for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
426 session_id = (session_id << 4) | j;
431 /* Create hook private structure */
432 MALLOC(hpriv, hookpriv_p,
433 sizeof(*hpriv), M_NETGRAPH_L2TP, M_WAITOK | M_NULLOK | M_ZERO);
436 hpriv->conf.session_id = htons(session_id);
437 hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
438 hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
440 NG_HOOK_SET_PRIVATE(hook, hpriv);
441 hash = SESSHASH(hpriv->conf.session_id);
442 LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
450 * Receive a control message.
453 ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
455 const priv_p priv = NG_NODE_PRIVATE(node);
456 struct ng_mesg *resp = NULL;
460 NGI_GET_MSG(item, msg);
461 switch (msg->header.typecookie) {
462 case NGM_L2TP_COOKIE:
463 switch (msg->header.cmd) {
464 case NGM_L2TP_SET_CONFIG:
466 struct ng_l2tp_config *const conf =
467 (struct ng_l2tp_config *)msg->data;
469 /* Check for invalid or illegal config */
470 if (msg->header.arglen != sizeof(*conf)) {
474 conf->enabled = !!conf->enabled;
475 conf->match_id = !!conf->match_id;
476 conf->tunnel_id = htons(conf->tunnel_id);
477 conf->peer_id = htons(conf->peer_id);
478 if (priv->conf.enabled
479 && ((priv->conf.tunnel_id != 0
480 && conf->tunnel_id != priv->conf.tunnel_id)
481 || ((priv->conf.peer_id != 0
482 && conf->peer_id != priv->conf.peer_id)))) {
487 /* Save calling node as failure target */
488 priv->ftarget = NGI_RETADDR(item);
490 /* Adjust sequence number state */
491 if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
494 /* Update node's config */
498 case NGM_L2TP_GET_CONFIG:
500 struct ng_l2tp_config *conf;
502 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
507 conf = (struct ng_l2tp_config *)resp->data;
510 /* Put ID's in host order */
511 conf->tunnel_id = ntohs(conf->tunnel_id);
512 conf->peer_id = ntohs(conf->peer_id);
515 case NGM_L2TP_SET_SESS_CONFIG:
517 struct ng_l2tp_sess_config *const conf =
518 (struct ng_l2tp_sess_config *)msg->data;
521 /* Check for invalid or illegal config. */
522 if (msg->header.arglen != sizeof(*conf)) {
527 /* Put ID's in network order */
528 conf->session_id = htons(conf->session_id);
529 conf->peer_id = htons(conf->peer_id);
531 /* Find matching hook */
532 hpriv = ng_l2tp_find_session(priv, conf->session_id);
538 /* Update hook's config */
542 case NGM_L2TP_GET_SESS_CONFIG:
544 struct ng_l2tp_sess_config *conf;
545 u_int16_t session_id;
549 if (msg->header.arglen != sizeof(session_id)) {
553 memcpy(&session_id, msg->data, 2);
554 session_id = htons(session_id);
556 /* Find matching hook */
557 hpriv = ng_l2tp_find_session(priv, session_id);
564 NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_WAITOK | M_NULLOK);
569 conf = (struct ng_l2tp_sess_config *)resp->data;
572 /* Put ID's in host order */
573 conf->session_id = ntohs(conf->session_id);
574 conf->peer_id = ntohs(conf->peer_id);
577 case NGM_L2TP_GET_STATS:
578 case NGM_L2TP_CLR_STATS:
579 case NGM_L2TP_GETCLR_STATS:
581 if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
582 NG_MKRESPONSE(resp, msg,
583 sizeof(priv->stats), M_WAITOK | M_NULLOK);
589 &priv->stats, sizeof(priv->stats));
591 if (msg->header.cmd != NGM_L2TP_GET_STATS)
592 memset(&priv->stats, 0, sizeof(priv->stats));
595 case NGM_L2TP_GET_SESSION_STATS:
596 case NGM_L2TP_CLR_SESSION_STATS:
597 case NGM_L2TP_GETCLR_SESSION_STATS:
602 /* Get session ID. */
603 if (msg->header.arglen != sizeof(session_id)) {
607 bcopy(msg->data, &session_id, sizeof(uint16_t));
608 session_id = htons(session_id);
610 /* Find matching hook. */
611 hpriv = ng_l2tp_find_session(priv, session_id);
617 if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
618 NG_MKRESPONSE(resp, msg,
619 sizeof(hpriv->stats), M_WAITOK | M_NULLOK);
624 bcopy(&hpriv->stats, resp->data,
625 sizeof(hpriv->stats));
627 if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
628 bzero(&hpriv->stats, sizeof(hpriv->stats));
631 case NGM_L2TP_SET_SEQ:
633 struct ng_l2tp_seq_config *const conf =
634 (struct ng_l2tp_seq_config *)msg->data;
636 /* Check for invalid or illegal seq config. */
637 if (msg->header.arglen != sizeof(*conf)) {
641 conf->ns = htons(conf->ns);
642 conf->nr = htons(conf->nr);
643 conf->rack = htons(conf->rack);
644 conf->xack = htons(conf->xack);
646 /* Set sequence numbers. */
647 error = ng_l2tp_seq_set(priv, conf);
661 NG_RESPOND_MSG(error, node, item, resp);
670 ng_l2tp_shutdown(node_p node)
672 const priv_p priv = NG_NODE_PRIVATE(node);
673 struct l2tp_seq *const seq = &priv->seq;
678 /* Reset sequence number state */
679 ng_l2tp_seq_reset(priv);
681 /* Free private data if neither timer is running */
682 ng_uncallout(&seq->rack_timer, node);
683 ng_uncallout(&seq->xack_timer, node);
685 mtx_destroy(&seq->mtx);
687 FREE(priv, M_NETGRAPH_L2TP);
698 ng_l2tp_disconnect(hook_p hook)
700 const node_p node = NG_HOOK_NODE(hook);
701 const priv_p priv = NG_NODE_PRIVATE(node);
703 /* Zero out hook pointer */
704 if (hook == priv->ctrl)
706 else if (hook == priv->lower)
709 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
710 LIST_REMOVE(hpriv, sessions);
711 FREE(hpriv, M_NETGRAPH_L2TP);
712 NG_HOOK_SET_PRIVATE(hook, NULL);
715 /* Go away if no longer connected to anything */
716 if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
717 ng_rmnode_self(node);
721 /*************************************************************************
723 *************************************************************************/
726 * Find the hook with a given session ID (in network order).
729 ng_l2tp_find_session(priv_p privp, u_int16_t sid)
731 uint16_t hash = SESSHASH(sid);
732 hookpriv_p hpriv = NULL;
734 LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
735 if (hpriv->conf.session_id == sid)
743 * Reset a hook's session state.
746 ng_l2tp_reset_session(hook_p hook, void *arg)
748 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
751 hpriv->conf.control_dseq = 0;
752 hpriv->conf.enable_dseq = 0;
753 bzero(&hpriv->conf, sizeof(struct ng_l2tp_session_stats));
761 * Handle an incoming frame from below.
764 ng_l2tp_rcvdata_lower(hook_p h, item_p item)
766 static const u_int16_t req_bits[2][2] = {
767 { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
768 { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
770 const node_p node = NG_HOOK_NODE(h);
771 const priv_p priv = NG_NODE_PRIVATE(node);
772 hookpriv_p hpriv = NULL;
784 L2TP_SEQ_CHECK(&priv->seq);
786 /* If not configured, reject */
787 if (!priv->conf.enabled) {
795 /* Remember full packet length; needed for per session accounting. */
796 plen = m->m_pkthdr.len;
799 priv->stats.recvPackets++;
800 priv->stats.recvOctets += plen;
802 /* Get initial header */
803 if (m->m_pkthdr.len < 6) {
804 priv->stats.recvRunts++;
809 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
810 priv->stats.memoryFailures++;
814 hdr = ntohs(*mtod(m, u_int16_t *));
817 /* Check required header bits and minimum length */
818 is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
819 if ((hdr & req_bits[is_ctrl][0]) != 0
820 || (~hdr & req_bits[is_ctrl][1]) != 0) {
821 priv->stats.recvInvalid++;
826 if (m->m_pkthdr.len < 4 /* tunnel, session id */
827 + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
828 + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
829 + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
830 priv->stats.recvRunts++;
836 /* Get and validate length field if present */
837 if ((hdr & L2TP_HDR_LEN) != 0) {
838 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
839 priv->stats.memoryFailures++;
843 len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
845 if (len < 0 || len > m->m_pkthdr.len) {
846 priv->stats.recvInvalid++;
851 if (len < m->m_pkthdr.len) /* trim extra bytes */
852 m_adj(m, -(m->m_pkthdr.len - len));
855 /* Get tunnel ID and session ID */
856 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
857 priv->stats.memoryFailures++;
861 memcpy(ids, mtod(m, u_int16_t *), 4);
864 /* Check tunnel ID */
865 if (ids[0] != priv->conf.tunnel_id
866 && (priv->conf.match_id || ids[0] != 0)) {
867 priv->stats.recvWrongTunnel++;
870 ERROUT(EADDRNOTAVAIL);
873 /* Check session ID (for data packets only) */
874 if ((hdr & L2TP_HDR_CTRL) == 0) {
875 hpriv = ng_l2tp_find_session(priv, ids[1]);
877 priv->stats.recvUnknownSID++;
885 /* Get Ns, Nr fields if present */
886 if ((hdr & L2TP_HDR_SEQ) != 0) {
887 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
888 priv->stats.memoryFailures++;
892 memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
894 memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
899 /* Strip offset padding if present */
900 if ((hdr & L2TP_HDR_OFF) != 0) {
903 /* Get length of offset padding */
904 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
905 priv->stats.memoryFailures++;
909 memcpy(&offset, mtod(m, u_int16_t *), 2);
910 offset = ntohs(offset);
912 /* Trim offset padding */
913 if ((2+offset) > m->m_pkthdr.len) {
914 priv->stats.recvInvalid++;
922 /* Handle control packets */
923 if ((hdr & L2TP_HDR_CTRL) != 0) {
924 struct l2tp_seq *const seq = &priv->seq;
926 /* Handle receive ack sequence number Nr */
927 ng_l2tp_seq_recv_nr(priv, nr);
929 /* Discard ZLB packets */
930 if (m->m_pkthdr.len == 0) {
931 priv->stats.recvZLBs++;
939 * If not what we expect or we are busy, drop packet and
940 * send an immediate ZLB ack.
942 if (ns != seq->nr || seq->inproc) {
943 if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
944 priv->stats.recvDuplicates++;
946 priv->stats.recvOutOfOrder++;
947 mtx_unlock(&seq->mtx);
948 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
954 * Until we deliver this packet we can't receive next one as
955 * we have no information for sending ack.
958 mtx_unlock(&seq->mtx);
960 /* Prepend session ID to packet. */
961 M_PREPEND(m, 2, MB_DONTWAIT);
964 priv->stats.memoryFailures++;
968 memcpy(mtod(m, u_int16_t *), &ids[1], 2);
970 /* Deliver packet to upper layers */
971 NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
974 /* Ready to process next packet. */
977 /* If packet was successfully delivered send ack. */
979 /* Update recv sequence number */
981 /* Start receive ack timer, if not already running */
982 if (!callout_active(&seq->xack_timer)) {
983 ng_callout(&seq->xack_timer, priv->node, NULL,
984 L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
988 mtx_unlock(&seq->mtx);
993 /* Per session packet, account it. */
994 hpriv->stats.recvPackets++;
995 hpriv->stats.recvOctets += plen;
997 /* Follow peer's lead in data sequencing, if configured to do so */
998 if (!hpriv->conf.control_dseq)
999 hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
1001 /* Handle data sequence numbers if present and enabled */
1002 if ((hdr & L2TP_HDR_SEQ) != 0) {
1003 if (hpriv->conf.enable_dseq
1004 && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
1005 NG_FREE_ITEM(item); /* duplicate or out of order */
1007 priv->stats.recvDataDrops++;
1013 /* Drop empty data packets */
1014 if (m->m_pkthdr.len == 0) {
1021 NG_FWD_NEW_DATA(error, item, hook, m);
1024 L2TP_SEQ_CHECK(&priv->seq);
1029 * Handle an outgoing control frame.
1032 ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1034 const node_p node = NG_HOOK_NODE(hook);
1035 const priv_p priv = NG_NODE_PRIVATE(node);
1036 struct l2tp_seq *const seq = &priv->seq;
1043 L2TP_SEQ_CHECK(&priv->seq);
1045 /* If not configured, reject */
1046 if (!priv->conf.enabled) {
1051 /* Grab mbuf and discard other stuff XXX */
1055 /* Packet should have session ID prepended */
1056 if (m->m_pkthdr.len < 2) {
1057 priv->stats.xmitInvalid++;
1062 /* Check max length */
1063 if (m->m_pkthdr.len >= 0x10000 - 14) {
1064 priv->stats.xmitTooBig++;
1069 mtx_lock(&seq->mtx);
1071 /* Find next empty slot in transmit queue */
1072 for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1073 if (i == L2TP_MAX_XWIN) {
1074 mtx_unlock(&seq->mtx);
1075 priv->stats.xmitDrops++;
1081 /* If peer's receive window is already full, nothing else to do */
1082 if (i >= seq->cwnd) {
1083 mtx_unlock(&seq->mtx);
1087 /* Start retransmit timer if not already running */
1088 if (!callout_active(&seq->rack_timer))
1089 ng_callout(&seq->rack_timer, node, NULL,
1090 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1094 mtx_unlock(&seq->mtx);
1097 if ((m = L2TP_COPY_MBUF(m, MB_DONTWAIT)) == NULL) {
1098 priv->stats.memoryFailures++;
1102 /* Send packet and increment xmit sequence number */
1103 error = ng_l2tp_xmit_ctrl(priv, m, ns);
1106 L2TP_SEQ_CHECK(&priv->seq);
1111 * Handle an outgoing data frame.
1114 ng_l2tp_rcvdata(hook_p hook, item_p item)
1116 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1117 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1124 L2TP_SEQ_CHECK(&priv->seq);
1126 /* If not configured, reject */
1127 if (!priv->conf.enabled) {
1135 /* Check max length */
1136 if (m->m_pkthdr.len >= 0x10000 - 12) {
1137 priv->stats.xmitDataTooBig++;
1143 /* Prepend L2TP header */
1145 + (2 * (hpriv->conf.include_length != 0))
1146 + (4 * (hpriv->conf.enable_dseq != 0)),
1149 priv->stats.memoryFailures++;
1153 hdr = L2TP_DATA_HDR;
1154 if (hpriv->conf.include_length) {
1155 hdr |= L2TP_HDR_LEN;
1156 mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
1158 mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
1159 mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
1160 if (hpriv->conf.enable_dseq) {
1161 hdr |= L2TP_HDR_SEQ;
1162 mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
1163 mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
1166 mtod(m, u_int16_t *)[0] = htons(hdr);
1168 /* Update per session stats. */
1169 hpriv->stats.xmitPackets++;
1170 hpriv->stats.xmitOctets += m->m_pkthdr.len;
1172 /* And the global one. */
1173 priv->stats.xmitPackets++;
1174 priv->stats.xmitOctets += m->m_pkthdr.len;
1177 NG_FWD_NEW_DATA(error, item, priv->lower, m);
1180 L2TP_SEQ_CHECK(&priv->seq);
1185 * Send a message to our controlling node that we've failed.
1188 ng_l2tp_seq_failure(priv_p priv)
1190 struct ng_mesg *msg;
1193 NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_WAITOK | M_NULLOK);
1196 NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1199 /************************************************************************
1200 SEQUENCE NUMBER HANDLING
1201 ************************************************************************/
1204 * Initialize sequence number state.
1207 ng_l2tp_seq_init(priv_p priv)
1209 struct l2tp_seq *const seq = &priv->seq;
1211 KASSERT(priv->conf.peer_win >= 1,
1212 ("%s: peer_win is zero", __func__));
1213 memset(seq, 0, sizeof(*seq));
1215 seq->wmax = priv->conf.peer_win;
1216 if (seq->wmax > L2TP_MAX_XWIN)
1217 seq->wmax = L2TP_MAX_XWIN;
1218 seq->ssth = seq->wmax;
1219 ng_callout_init(&seq->rack_timer);
1220 ng_callout_init(&seq->xack_timer);
1221 mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1222 L2TP_SEQ_CHECK(seq);
1226 * Set sequence number state as given from user.
1229 ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
1231 struct l2tp_seq *const seq = &priv->seq;
1233 /* If node is enabled, deny update to sequence numbers. */
1234 if (priv->conf.enabled)
1237 /* We only can handle the simple cases. */
1238 if (conf->xack != conf->nr || conf->ns != conf->rack)
1241 /* Set ns,nr,rack,xack parameters. */
1244 seq->rack = conf->rack;
1245 seq->xack = conf->xack;
1251 * Adjust sequence number state accordingly after reconfiguration.
1254 ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1256 struct l2tp_seq *const seq = &priv->seq;
1259 /* If disabling node, reset state sequence number */
1260 if (!conf->enabled) {
1261 ng_l2tp_seq_reset(priv);
1265 /* Adjust peer's max recv window; it can only increase */
1266 new_wmax = conf->peer_win;
1267 if (new_wmax > L2TP_MAX_XWIN)
1268 new_wmax = L2TP_MAX_XWIN;
1271 if (new_wmax < seq->wmax)
1273 seq->wmax = new_wmax;
1280 * Reset sequence number state.
1283 ng_l2tp_seq_reset(priv_p priv)
1285 struct l2tp_seq *const seq = &priv->seq;
1290 L2TP_SEQ_CHECK(seq);
1293 ng_uncallout(&seq->rack_timer, priv->node);
1294 ng_uncallout(&seq->xack_timer, priv->node);
1296 /* Free retransmit queue */
1297 for (i = 0; i < L2TP_MAX_XWIN; i++) {
1298 if (seq->xwin[i] == NULL)
1300 m_freem(seq->xwin[i]);
1303 /* Reset session hooks' sequence number states */
1304 NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1306 /* Reset node's sequence number state */
1311 seq->wmax = L2TP_MAX_XWIN;
1313 seq->ssth = seq->wmax;
1316 bzero(seq->xwin, sizeof(seq->xwin));
1319 L2TP_SEQ_CHECK(seq);
1323 * Handle receipt of an acknowledgement value (Nr) from peer.
1326 ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1328 struct l2tp_seq *const seq = &priv->seq;
1329 struct mbuf *xwin[L2TP_MAX_XWIN]; /* partial local copy */
1334 mtx_lock(&seq->mtx);
1336 /* Verify peer's ACK is in range */
1337 if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
1338 mtx_unlock(&seq->mtx);
1339 return; /* duplicate ack */
1341 if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1342 mtx_unlock(&seq->mtx);
1343 priv->stats.recvBadAcks++; /* ack for packet not sent */
1346 KASSERT(nack <= L2TP_MAX_XWIN,
1347 ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1349 /* Update receive ack stats */
1353 /* Free acknowledged packets and shift up packets in the xmit queue */
1354 for (i = 0; i < nack; i++)
1355 m_freem(seq->xwin[i]);
1356 memmove(seq->xwin, seq->xwin + nack,
1357 (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1358 memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1359 nack * sizeof(*seq->xwin));
1362 * Do slow-start/congestion avoidance windowing algorithm described
1363 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1364 * ACK had arrived separately.
1366 if (seq->cwnd < seq->wmax) {
1368 /* Handle slow start phase */
1369 if (seq->cwnd < seq->ssth) {
1372 if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
1373 nack = seq->cwnd - seq->ssth;
1374 seq->cwnd = seq->ssth;
1378 /* Handle congestion avoidance phase */
1379 if (seq->cwnd >= seq->ssth) {
1381 while (seq->acks >= seq->cwnd) {
1382 seq->acks -= seq->cwnd;
1383 if (seq->cwnd < seq->wmax)
1389 /* Stop xmit timer */
1390 if (callout_active(&seq->rack_timer))
1391 ng_uncallout(&seq->rack_timer, priv->node);
1393 /* If transmit queue is empty, we're done for now */
1394 if (seq->xwin[0] == NULL) {
1395 mtx_unlock(&seq->mtx);
1399 /* Start restransmit timer again */
1400 ng_callout(&seq->rack_timer, priv->node, NULL,
1401 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1404 * Send more packets, trying to keep peer's receive window full.
1405 * Make copy of everything we need before lock release.
1409 while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1410 && seq->xwin[i] != NULL) {
1411 xwin[j++] = seq->xwin[i];
1415 mtx_unlock(&seq->mtx);
1419 * If there is a memory error, pretend packet was sent, as it
1420 * will get retransmitted later anyway.
1422 for (i = 0; i < j; i++) {
1424 if ((m = L2TP_COPY_MBUF(xwin[i], MB_DONTWAIT)) == NULL)
1425 priv->stats.memoryFailures++;
1427 ng_l2tp_xmit_ctrl(priv, m, ns);
1433 * Handle an ack timeout. We have an outstanding ack that we
1434 * were hoping to piggy-back, but haven't, so send a ZLB.
1437 ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1439 const priv_p priv = NG_NODE_PRIVATE(node);
1440 struct l2tp_seq *const seq = &priv->seq;
1442 /* Make sure callout is still active before doing anything */
1443 if (callout_pending(&seq->xack_timer) ||
1444 (!callout_active(&seq->xack_timer)))
1448 L2TP_SEQ_CHECK(seq);
1451 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1453 /* callout_deactivate() is not needed here
1454 as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1457 L2TP_SEQ_CHECK(seq);
1461 * Handle a transmit timeout. The peer has failed to respond
1462 * with an ack for our packet, so retransmit it.
1465 ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1467 const priv_p priv = NG_NODE_PRIVATE(node);
1468 struct l2tp_seq *const seq = &priv->seq;
1472 /* Make sure callout is still active before doing anything */
1473 if (callout_pending(&seq->rack_timer) ||
1474 (!callout_active(&seq->rack_timer)))
1478 L2TP_SEQ_CHECK(seq);
1480 priv->stats.xmitRetransmits++;
1482 /* Have we reached the retransmit limit? If so, notify owner. */
1483 if (seq->rexmits++ >= priv->conf.rexmit_max)
1484 ng_l2tp_seq_failure(priv);
1486 /* Restart timer, this time with an increased delay */
1487 delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
1488 if (delay > priv->conf.rexmit_max_to)
1489 delay = priv->conf.rexmit_max_to;
1490 ng_callout(&seq->rack_timer, node, NULL,
1491 hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1493 /* Do slow-start/congestion algorithm windowing algorithm */
1494 seq->ns = seq->rack;
1495 seq->ssth = (seq->cwnd + 1) / 2;
1499 /* Retransmit oldest unack'd packet */
1500 if ((m = L2TP_COPY_MBUF(seq->xwin[0], MB_DONTWAIT)) == NULL)
1501 priv->stats.memoryFailures++;
1503 ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1505 /* callout_deactivate() is not needed here
1506 as ng_callout() is getting called each time */
1509 L2TP_SEQ_CHECK(seq);
1513 * Transmit a control stream packet, payload optional.
1514 * The transmit sequence number is not incremented.
1517 ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1519 struct l2tp_seq *const seq = &priv->seq;
1520 u_int16_t session_id = 0;
1523 mtx_lock(&seq->mtx);
1525 /* Stop ack timer: we're sending an ack with this packet.
1526 Doing this before to keep state predictable after error. */
1527 if (callout_active(&seq->xack_timer))
1528 ng_uncallout(&seq->xack_timer, priv->node);
1530 seq->xack = seq->nr;
1532 mtx_unlock(&seq->mtx);
1534 /* If no mbuf passed, send an empty packet (ZLB) */
1537 /* Create a new mbuf for ZLB packet */
1538 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1540 priv->stats.memoryFailures++;
1543 m->m_len = m->m_pkthdr.len = 12;
1544 m->m_pkthdr.rcvif = NULL;
1545 priv->stats.xmitZLBs++;
1548 /* Strip off session ID */
1549 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1550 priv->stats.memoryFailures++;
1553 memcpy(&session_id, mtod(m, u_int16_t *), 2);
1556 /* Make room for L2TP header */
1557 M_PREPEND(m, 12, MB_DONTWAIT);
1559 priv->stats.memoryFailures++;
1564 /* Fill in L2TP header */
1565 mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
1566 mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
1567 mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
1568 mtod(m, u_int16_t *)[3] = session_id;
1569 mtod(m, u_int16_t *)[4] = htons(ns);
1570 mtod(m, u_int16_t *)[5] = htons(seq->nr);
1572 /* Update sequence number info and stats */
1573 priv->stats.xmitPackets++;
1574 priv->stats.xmitOctets += m->m_pkthdr.len;
1577 NG_SEND_DATA_ONLY(error, priv->lower, m);
1583 * Sanity check sequence number state.
1586 ng_l2tp_seq_check(struct l2tp_seq *seq)
1588 int self_unack, peer_unack;
1591 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1593 mtx_lock(&seq->mtx);
1595 self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1596 peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1597 CHECK(seq->wmax <= L2TP_MAX_XWIN);
1598 CHECK(seq->cwnd >= 1);
1599 CHECK(seq->cwnd <= seq->wmax);
1600 CHECK(seq->ssth >= 1);
1601 CHECK(seq->ssth <= seq->wmax);
1602 if (seq->cwnd < seq->ssth)
1603 CHECK(seq->acks == 0);
1605 CHECK(seq->acks <= seq->cwnd);
1606 CHECK(self_unack >= 0);
1607 CHECK(peer_unack >= 0);
1608 CHECK(peer_unack <= seq->wmax);
1609 CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1610 CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1611 for (i = 0; i < peer_unack; i++)
1612 CHECK(seq->xwin[i] != NULL);
1613 for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
1614 CHECK(seq->xwin[i] == NULL);
1616 mtx_unlock(&seq->mtx);
1620 #endif /* INVARIANTS */