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 priv = kmalloc(sizeof(*priv), M_NETGRAPH_L2TP,
369 M_WAITOK | M_NULLOK | M_ZERO);
372 NG_NODE_SET_PRIVATE(node, priv);
375 /* Apply a semi-reasonable default configuration */
376 priv->conf.peer_win = 1;
377 priv->conf.rexmit_max = L2TP_MAX_REXMIT;
378 priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
380 /* Initialize sequence number state */
381 ng_l2tp_seq_init(priv);
383 for (i = 0; i < SESSHASHSIZE; i++)
384 LIST_INIT(&priv->sesshash[i]);
391 * Give our OK for a hook to be added.
394 ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
396 const priv_p priv = NG_NODE_PRIVATE(node);
398 /* Check hook name */
399 if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
400 if (priv->ctrl != NULL)
403 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
404 } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
405 if (priv->lower != NULL)
408 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
410 static const char hexdig[16] = "0123456789abcdef";
411 u_int16_t session_id;
418 /* Parse hook name to get session ID */
419 if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
420 sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
422 hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
423 for (session_id = i = 0; i < 4; i++) {
424 for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
427 session_id = (session_id << 4) | j;
432 /* Create hook private structure */
433 hpriv = kmalloc(sizeof(*hpriv), M_NETGRAPH_L2TP,
434 M_WAITOK | M_NULLOK | M_ZERO);
437 hpriv->conf.session_id = htons(session_id);
438 hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
439 hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
441 NG_HOOK_SET_PRIVATE(hook, hpriv);
442 hash = SESSHASH(hpriv->conf.session_id);
443 LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
451 * Receive a control message.
454 ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
456 const priv_p priv = NG_NODE_PRIVATE(node);
457 struct ng_mesg *resp = NULL;
461 NGI_GET_MSG(item, msg);
462 switch (msg->header.typecookie) {
463 case NGM_L2TP_COOKIE:
464 switch (msg->header.cmd) {
465 case NGM_L2TP_SET_CONFIG:
467 struct ng_l2tp_config *const conf =
468 (struct ng_l2tp_config *)msg->data;
470 /* Check for invalid or illegal config */
471 if (msg->header.arglen != sizeof(*conf)) {
475 conf->enabled = !!conf->enabled;
476 conf->match_id = !!conf->match_id;
477 conf->tunnel_id = htons(conf->tunnel_id);
478 conf->peer_id = htons(conf->peer_id);
479 if (priv->conf.enabled
480 && ((priv->conf.tunnel_id != 0
481 && conf->tunnel_id != priv->conf.tunnel_id)
482 || ((priv->conf.peer_id != 0
483 && conf->peer_id != priv->conf.peer_id)))) {
488 /* Save calling node as failure target */
489 priv->ftarget = NGI_RETADDR(item);
491 /* Adjust sequence number state */
492 if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
495 /* Update node's config */
499 case NGM_L2TP_GET_CONFIG:
501 struct ng_l2tp_config *conf;
503 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
508 conf = (struct ng_l2tp_config *)resp->data;
511 /* Put ID's in host order */
512 conf->tunnel_id = ntohs(conf->tunnel_id);
513 conf->peer_id = ntohs(conf->peer_id);
516 case NGM_L2TP_SET_SESS_CONFIG:
518 struct ng_l2tp_sess_config *const conf =
519 (struct ng_l2tp_sess_config *)msg->data;
522 /* Check for invalid or illegal config. */
523 if (msg->header.arglen != sizeof(*conf)) {
528 /* Put ID's in network order */
529 conf->session_id = htons(conf->session_id);
530 conf->peer_id = htons(conf->peer_id);
532 /* Find matching hook */
533 hpriv = ng_l2tp_find_session(priv, conf->session_id);
539 /* Update hook's config */
543 case NGM_L2TP_GET_SESS_CONFIG:
545 struct ng_l2tp_sess_config *conf;
546 u_int16_t session_id;
550 if (msg->header.arglen != sizeof(session_id)) {
554 memcpy(&session_id, msg->data, 2);
555 session_id = htons(session_id);
557 /* Find matching hook */
558 hpriv = ng_l2tp_find_session(priv, session_id);
565 NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_WAITOK | M_NULLOK);
570 conf = (struct ng_l2tp_sess_config *)resp->data;
573 /* Put ID's in host order */
574 conf->session_id = ntohs(conf->session_id);
575 conf->peer_id = ntohs(conf->peer_id);
578 case NGM_L2TP_GET_STATS:
579 case NGM_L2TP_CLR_STATS:
580 case NGM_L2TP_GETCLR_STATS:
582 if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
583 NG_MKRESPONSE(resp, msg,
584 sizeof(priv->stats), M_WAITOK | M_NULLOK);
590 &priv->stats, sizeof(priv->stats));
592 if (msg->header.cmd != NGM_L2TP_GET_STATS)
593 memset(&priv->stats, 0, sizeof(priv->stats));
596 case NGM_L2TP_GET_SESSION_STATS:
597 case NGM_L2TP_CLR_SESSION_STATS:
598 case NGM_L2TP_GETCLR_SESSION_STATS:
603 /* Get session ID. */
604 if (msg->header.arglen != sizeof(session_id)) {
608 bcopy(msg->data, &session_id, sizeof(uint16_t));
609 session_id = htons(session_id);
611 /* Find matching hook. */
612 hpriv = ng_l2tp_find_session(priv, session_id);
618 if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
619 NG_MKRESPONSE(resp, msg,
620 sizeof(hpriv->stats), M_WAITOK | M_NULLOK);
625 bcopy(&hpriv->stats, resp->data,
626 sizeof(hpriv->stats));
628 if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
629 bzero(&hpriv->stats, sizeof(hpriv->stats));
632 case NGM_L2TP_SET_SEQ:
634 struct ng_l2tp_seq_config *const conf =
635 (struct ng_l2tp_seq_config *)msg->data;
637 /* Check for invalid or illegal seq config. */
638 if (msg->header.arglen != sizeof(*conf)) {
642 conf->ns = htons(conf->ns);
643 conf->nr = htons(conf->nr);
644 conf->rack = htons(conf->rack);
645 conf->xack = htons(conf->xack);
647 /* Set sequence numbers. */
648 error = ng_l2tp_seq_set(priv, conf);
662 NG_RESPOND_MSG(error, node, item, resp);
671 ng_l2tp_shutdown(node_p node)
673 const priv_p priv = NG_NODE_PRIVATE(node);
674 struct l2tp_seq *const seq = &priv->seq;
679 /* Reset sequence number state */
680 ng_l2tp_seq_reset(priv);
682 /* Free private data if neither timer is running */
683 ng_uncallout(&seq->rack_timer, node);
684 ng_uncallout(&seq->xack_timer, node);
686 mtx_destroy(&seq->mtx);
688 kfree(priv, M_NETGRAPH_L2TP);
699 ng_l2tp_disconnect(hook_p hook)
701 const node_p node = NG_HOOK_NODE(hook);
702 const priv_p priv = NG_NODE_PRIVATE(node);
704 /* Zero out hook pointer */
705 if (hook == priv->ctrl)
707 else if (hook == priv->lower)
710 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
711 LIST_REMOVE(hpriv, sessions);
712 kfree(hpriv, M_NETGRAPH_L2TP);
713 NG_HOOK_SET_PRIVATE(hook, NULL);
716 /* Go away if no longer connected to anything */
717 if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
718 ng_rmnode_self(node);
722 /*************************************************************************
724 *************************************************************************/
727 * Find the hook with a given session ID (in network order).
730 ng_l2tp_find_session(priv_p privp, u_int16_t sid)
732 uint16_t hash = SESSHASH(sid);
733 hookpriv_p hpriv = NULL;
735 LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
736 if (hpriv->conf.session_id == sid)
744 * Reset a hook's session state.
747 ng_l2tp_reset_session(hook_p hook, void *arg)
749 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
752 hpriv->conf.control_dseq = 0;
753 hpriv->conf.enable_dseq = 0;
754 bzero(&hpriv->conf, sizeof(struct ng_l2tp_session_stats));
762 * Handle an incoming frame from below.
765 ng_l2tp_rcvdata_lower(hook_p h, item_p item)
767 static const u_int16_t req_bits[2][2] = {
768 { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
769 { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
771 const node_p node = NG_HOOK_NODE(h);
772 const priv_p priv = NG_NODE_PRIVATE(node);
773 hookpriv_p hpriv = NULL;
785 L2TP_SEQ_CHECK(&priv->seq);
787 /* If not configured, reject */
788 if (!priv->conf.enabled) {
796 /* Remember full packet length; needed for per session accounting. */
797 plen = m->m_pkthdr.len;
800 priv->stats.recvPackets++;
801 priv->stats.recvOctets += plen;
803 /* Get initial header */
804 if (m->m_pkthdr.len < 6) {
805 priv->stats.recvRunts++;
810 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
811 priv->stats.memoryFailures++;
815 hdr = ntohs(*mtod(m, u_int16_t *));
818 /* Check required header bits and minimum length */
819 is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
820 if ((hdr & req_bits[is_ctrl][0]) != 0
821 || (~hdr & req_bits[is_ctrl][1]) != 0) {
822 priv->stats.recvInvalid++;
827 if (m->m_pkthdr.len < 4 /* tunnel, session id */
828 + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
829 + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
830 + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
831 priv->stats.recvRunts++;
837 /* Get and validate length field if present */
838 if ((hdr & L2TP_HDR_LEN) != 0) {
839 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
840 priv->stats.memoryFailures++;
844 len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
846 if (len < 0 || len > m->m_pkthdr.len) {
847 priv->stats.recvInvalid++;
852 if (len < m->m_pkthdr.len) /* trim extra bytes */
853 m_adj(m, -(m->m_pkthdr.len - len));
856 /* Get tunnel ID and session ID */
857 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
858 priv->stats.memoryFailures++;
862 memcpy(ids, mtod(m, u_int16_t *), 4);
865 /* Check tunnel ID */
866 if (ids[0] != priv->conf.tunnel_id
867 && (priv->conf.match_id || ids[0] != 0)) {
868 priv->stats.recvWrongTunnel++;
871 ERROUT(EADDRNOTAVAIL);
874 /* Check session ID (for data packets only) */
875 if ((hdr & L2TP_HDR_CTRL) == 0) {
876 hpriv = ng_l2tp_find_session(priv, ids[1]);
878 priv->stats.recvUnknownSID++;
886 /* Get Ns, Nr fields if present */
887 if ((hdr & L2TP_HDR_SEQ) != 0) {
888 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
889 priv->stats.memoryFailures++;
893 memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
895 memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
900 /* Strip offset padding if present */
901 if ((hdr & L2TP_HDR_OFF) != 0) {
904 /* Get length of offset padding */
905 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
906 priv->stats.memoryFailures++;
910 memcpy(&offset, mtod(m, u_int16_t *), 2);
911 offset = ntohs(offset);
913 /* Trim offset padding */
914 if ((2+offset) > m->m_pkthdr.len) {
915 priv->stats.recvInvalid++;
923 /* Handle control packets */
924 if ((hdr & L2TP_HDR_CTRL) != 0) {
925 struct l2tp_seq *const seq = &priv->seq;
927 /* Handle receive ack sequence number Nr */
928 ng_l2tp_seq_recv_nr(priv, nr);
930 /* Discard ZLB packets */
931 if (m->m_pkthdr.len == 0) {
932 priv->stats.recvZLBs++;
940 * If not what we expect or we are busy, drop packet and
941 * send an immediate ZLB ack.
943 if (ns != seq->nr || seq->inproc) {
944 if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
945 priv->stats.recvDuplicates++;
947 priv->stats.recvOutOfOrder++;
948 mtx_unlock(&seq->mtx);
949 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
955 * Until we deliver this packet we can't receive next one as
956 * we have no information for sending ack.
959 mtx_unlock(&seq->mtx);
961 /* Prepend session ID to packet. */
962 M_PREPEND(m, 2, MB_DONTWAIT);
965 priv->stats.memoryFailures++;
969 memcpy(mtod(m, u_int16_t *), &ids[1], 2);
971 /* Deliver packet to upper layers */
972 NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
975 /* Ready to process next packet. */
978 /* If packet was successfully delivered send ack. */
980 /* Update recv sequence number */
982 /* Start receive ack timer, if not already running */
983 if (!callout_active(&seq->xack_timer)) {
984 ng_callout(&seq->xack_timer, priv->node, NULL,
985 L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
989 mtx_unlock(&seq->mtx);
994 /* Per session packet, account it. */
995 hpriv->stats.recvPackets++;
996 hpriv->stats.recvOctets += plen;
998 /* Follow peer's lead in data sequencing, if configured to do so */
999 if (!hpriv->conf.control_dseq)
1000 hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
1002 /* Handle data sequence numbers if present and enabled */
1003 if ((hdr & L2TP_HDR_SEQ) != 0) {
1004 if (hpriv->conf.enable_dseq
1005 && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
1006 NG_FREE_ITEM(item); /* duplicate or out of order */
1008 priv->stats.recvDataDrops++;
1014 /* Drop empty data packets */
1015 if (m->m_pkthdr.len == 0) {
1022 NG_FWD_NEW_DATA(error, item, hook, m);
1025 L2TP_SEQ_CHECK(&priv->seq);
1030 * Handle an outgoing control frame.
1033 ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1035 const node_p node = NG_HOOK_NODE(hook);
1036 const priv_p priv = NG_NODE_PRIVATE(node);
1037 struct l2tp_seq *const seq = &priv->seq;
1044 L2TP_SEQ_CHECK(&priv->seq);
1046 /* If not configured, reject */
1047 if (!priv->conf.enabled) {
1052 /* Grab mbuf and discard other stuff XXX */
1056 /* Packet should have session ID prepended */
1057 if (m->m_pkthdr.len < 2) {
1058 priv->stats.xmitInvalid++;
1063 /* Check max length */
1064 if (m->m_pkthdr.len >= 0x10000 - 14) {
1065 priv->stats.xmitTooBig++;
1070 mtx_lock(&seq->mtx);
1072 /* Find next empty slot in transmit queue */
1073 for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1074 if (i == L2TP_MAX_XWIN) {
1075 mtx_unlock(&seq->mtx);
1076 priv->stats.xmitDrops++;
1082 /* If peer's receive window is already full, nothing else to do */
1083 if (i >= seq->cwnd) {
1084 mtx_unlock(&seq->mtx);
1088 /* Start retransmit timer if not already running */
1089 if (!callout_active(&seq->rack_timer))
1090 ng_callout(&seq->rack_timer, node, NULL,
1091 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1095 mtx_unlock(&seq->mtx);
1098 if ((m = L2TP_COPY_MBUF(m, MB_DONTWAIT)) == NULL) {
1099 priv->stats.memoryFailures++;
1103 /* Send packet and increment xmit sequence number */
1104 error = ng_l2tp_xmit_ctrl(priv, m, ns);
1107 L2TP_SEQ_CHECK(&priv->seq);
1112 * Handle an outgoing data frame.
1115 ng_l2tp_rcvdata(hook_p hook, item_p item)
1117 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1118 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1125 L2TP_SEQ_CHECK(&priv->seq);
1127 /* If not configured, reject */
1128 if (!priv->conf.enabled) {
1136 /* Check max length */
1137 if (m->m_pkthdr.len >= 0x10000 - 12) {
1138 priv->stats.xmitDataTooBig++;
1144 /* Prepend L2TP header */
1146 + (2 * (hpriv->conf.include_length != 0))
1147 + (4 * (hpriv->conf.enable_dseq != 0)),
1150 priv->stats.memoryFailures++;
1154 hdr = L2TP_DATA_HDR;
1155 if (hpriv->conf.include_length) {
1156 hdr |= L2TP_HDR_LEN;
1157 mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
1159 mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
1160 mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
1161 if (hpriv->conf.enable_dseq) {
1162 hdr |= L2TP_HDR_SEQ;
1163 mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
1164 mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
1167 mtod(m, u_int16_t *)[0] = htons(hdr);
1169 /* Update per session stats. */
1170 hpriv->stats.xmitPackets++;
1171 hpriv->stats.xmitOctets += m->m_pkthdr.len;
1173 /* And the global one. */
1174 priv->stats.xmitPackets++;
1175 priv->stats.xmitOctets += m->m_pkthdr.len;
1178 NG_FWD_NEW_DATA(error, item, priv->lower, m);
1181 L2TP_SEQ_CHECK(&priv->seq);
1186 * Send a message to our controlling node that we've failed.
1189 ng_l2tp_seq_failure(priv_p priv)
1191 struct ng_mesg *msg;
1194 NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_WAITOK | M_NULLOK);
1197 NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1200 /************************************************************************
1201 SEQUENCE NUMBER HANDLING
1202 ************************************************************************/
1205 * Initialize sequence number state.
1208 ng_l2tp_seq_init(priv_p priv)
1210 struct l2tp_seq *const seq = &priv->seq;
1212 KASSERT(priv->conf.peer_win >= 1,
1213 ("%s: peer_win is zero", __func__));
1214 memset(seq, 0, sizeof(*seq));
1216 seq->wmax = priv->conf.peer_win;
1217 if (seq->wmax > L2TP_MAX_XWIN)
1218 seq->wmax = L2TP_MAX_XWIN;
1219 seq->ssth = seq->wmax;
1220 ng_callout_init(&seq->rack_timer);
1221 ng_callout_init(&seq->xack_timer);
1222 mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1223 L2TP_SEQ_CHECK(seq);
1227 * Set sequence number state as given from user.
1230 ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
1232 struct l2tp_seq *const seq = &priv->seq;
1234 /* If node is enabled, deny update to sequence numbers. */
1235 if (priv->conf.enabled)
1238 /* We only can handle the simple cases. */
1239 if (conf->xack != conf->nr || conf->ns != conf->rack)
1242 /* Set ns,nr,rack,xack parameters. */
1245 seq->rack = conf->rack;
1246 seq->xack = conf->xack;
1252 * Adjust sequence number state accordingly after reconfiguration.
1255 ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1257 struct l2tp_seq *const seq = &priv->seq;
1260 /* If disabling node, reset state sequence number */
1261 if (!conf->enabled) {
1262 ng_l2tp_seq_reset(priv);
1266 /* Adjust peer's max recv window; it can only increase */
1267 new_wmax = conf->peer_win;
1268 if (new_wmax > L2TP_MAX_XWIN)
1269 new_wmax = L2TP_MAX_XWIN;
1272 if (new_wmax < seq->wmax)
1274 seq->wmax = new_wmax;
1281 * Reset sequence number state.
1284 ng_l2tp_seq_reset(priv_p priv)
1286 struct l2tp_seq *const seq = &priv->seq;
1291 L2TP_SEQ_CHECK(seq);
1294 ng_uncallout(&seq->rack_timer, priv->node);
1295 ng_uncallout(&seq->xack_timer, priv->node);
1297 /* Free retransmit queue */
1298 for (i = 0; i < L2TP_MAX_XWIN; i++) {
1299 if (seq->xwin[i] == NULL)
1301 m_freem(seq->xwin[i]);
1304 /* Reset session hooks' sequence number states */
1305 NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1307 /* Reset node's sequence number state */
1312 seq->wmax = L2TP_MAX_XWIN;
1314 seq->ssth = seq->wmax;
1317 bzero(seq->xwin, sizeof(seq->xwin));
1320 L2TP_SEQ_CHECK(seq);
1324 * Handle receipt of an acknowledgement value (Nr) from peer.
1327 ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1329 struct l2tp_seq *const seq = &priv->seq;
1330 struct mbuf *xwin[L2TP_MAX_XWIN]; /* partial local copy */
1335 mtx_lock(&seq->mtx);
1337 /* Verify peer's ACK is in range */
1338 if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
1339 mtx_unlock(&seq->mtx);
1340 return; /* duplicate ack */
1342 if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1343 mtx_unlock(&seq->mtx);
1344 priv->stats.recvBadAcks++; /* ack for packet not sent */
1347 KASSERT(nack <= L2TP_MAX_XWIN,
1348 ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1350 /* Update receive ack stats */
1354 /* Free acknowledged packets and shift up packets in the xmit queue */
1355 for (i = 0; i < nack; i++)
1356 m_freem(seq->xwin[i]);
1357 memmove(seq->xwin, seq->xwin + nack,
1358 (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1359 memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1360 nack * sizeof(*seq->xwin));
1363 * Do slow-start/congestion avoidance windowing algorithm described
1364 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1365 * ACK had arrived separately.
1367 if (seq->cwnd < seq->wmax) {
1369 /* Handle slow start phase */
1370 if (seq->cwnd < seq->ssth) {
1373 if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
1374 nack = seq->cwnd - seq->ssth;
1375 seq->cwnd = seq->ssth;
1379 /* Handle congestion avoidance phase */
1380 if (seq->cwnd >= seq->ssth) {
1382 while (seq->acks >= seq->cwnd) {
1383 seq->acks -= seq->cwnd;
1384 if (seq->cwnd < seq->wmax)
1390 /* Stop xmit timer */
1391 if (callout_active(&seq->rack_timer))
1392 ng_uncallout(&seq->rack_timer, priv->node);
1394 /* If transmit queue is empty, we're done for now */
1395 if (seq->xwin[0] == NULL) {
1396 mtx_unlock(&seq->mtx);
1400 /* Start restransmit timer again */
1401 ng_callout(&seq->rack_timer, priv->node, NULL,
1402 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1405 * Send more packets, trying to keep peer's receive window full.
1406 * Make copy of everything we need before lock release.
1410 while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1411 && seq->xwin[i] != NULL) {
1412 xwin[j++] = seq->xwin[i];
1416 mtx_unlock(&seq->mtx);
1420 * If there is a memory error, pretend packet was sent, as it
1421 * will get retransmitted later anyway.
1423 for (i = 0; i < j; i++) {
1425 if ((m = L2TP_COPY_MBUF(xwin[i], MB_DONTWAIT)) == NULL)
1426 priv->stats.memoryFailures++;
1428 ng_l2tp_xmit_ctrl(priv, m, ns);
1434 * Handle an ack timeout. We have an outstanding ack that we
1435 * were hoping to piggy-back, but haven't, so send a ZLB.
1438 ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1440 const priv_p priv = NG_NODE_PRIVATE(node);
1441 struct l2tp_seq *const seq = &priv->seq;
1443 /* Make sure callout is still active before doing anything */
1444 if (callout_pending(&seq->xack_timer) ||
1445 (!callout_active(&seq->xack_timer)))
1449 L2TP_SEQ_CHECK(seq);
1452 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1454 /* callout_deactivate() is not needed here
1455 as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1458 L2TP_SEQ_CHECK(seq);
1462 * Handle a transmit timeout. The peer has failed to respond
1463 * with an ack for our packet, so retransmit it.
1466 ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1468 const priv_p priv = NG_NODE_PRIVATE(node);
1469 struct l2tp_seq *const seq = &priv->seq;
1473 /* Make sure callout is still active before doing anything */
1474 if (callout_pending(&seq->rack_timer) ||
1475 (!callout_active(&seq->rack_timer)))
1479 L2TP_SEQ_CHECK(seq);
1481 priv->stats.xmitRetransmits++;
1483 /* Have we reached the retransmit limit? If so, notify owner. */
1484 if (seq->rexmits++ >= priv->conf.rexmit_max)
1485 ng_l2tp_seq_failure(priv);
1487 /* Restart timer, this time with an increased delay */
1488 delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
1489 if (delay > priv->conf.rexmit_max_to)
1490 delay = priv->conf.rexmit_max_to;
1491 ng_callout(&seq->rack_timer, node, NULL,
1492 hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1494 /* Do slow-start/congestion algorithm windowing algorithm */
1495 seq->ns = seq->rack;
1496 seq->ssth = (seq->cwnd + 1) / 2;
1500 /* Retransmit oldest unack'd packet */
1501 if ((m = L2TP_COPY_MBUF(seq->xwin[0], MB_DONTWAIT)) == NULL)
1502 priv->stats.memoryFailures++;
1504 ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1506 /* callout_deactivate() is not needed here
1507 as ng_callout() is getting called each time */
1510 L2TP_SEQ_CHECK(seq);
1514 * Transmit a control stream packet, payload optional.
1515 * The transmit sequence number is not incremented.
1518 ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1520 struct l2tp_seq *const seq = &priv->seq;
1521 u_int16_t session_id = 0;
1524 mtx_lock(&seq->mtx);
1526 /* Stop ack timer: we're sending an ack with this packet.
1527 Doing this before to keep state predictable after error. */
1528 if (callout_active(&seq->xack_timer))
1529 ng_uncallout(&seq->xack_timer, priv->node);
1531 seq->xack = seq->nr;
1533 mtx_unlock(&seq->mtx);
1535 /* If no mbuf passed, send an empty packet (ZLB) */
1538 /* Create a new mbuf for ZLB packet */
1539 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1541 priv->stats.memoryFailures++;
1544 m->m_len = m->m_pkthdr.len = 12;
1545 m->m_pkthdr.rcvif = NULL;
1546 priv->stats.xmitZLBs++;
1549 /* Strip off session ID */
1550 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1551 priv->stats.memoryFailures++;
1554 memcpy(&session_id, mtod(m, u_int16_t *), 2);
1557 /* Make room for L2TP header */
1558 M_PREPEND(m, 12, MB_DONTWAIT);
1560 priv->stats.memoryFailures++;
1565 /* Fill in L2TP header */
1566 mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
1567 mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
1568 mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
1569 mtod(m, u_int16_t *)[3] = session_id;
1570 mtod(m, u_int16_t *)[4] = htons(ns);
1571 mtod(m, u_int16_t *)[5] = htons(seq->nr);
1573 /* Update sequence number info and stats */
1574 priv->stats.xmitPackets++;
1575 priv->stats.xmitOctets += m->m_pkthdr.len;
1578 NG_SEND_DATA_ONLY(error, priv->lower, m);
1584 * Sanity check sequence number state.
1587 ng_l2tp_seq_check(struct l2tp_seq *seq)
1589 int self_unack, peer_unack;
1592 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1594 mtx_lock(&seq->mtx);
1596 self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1597 peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1598 CHECK(seq->wmax <= L2TP_MAX_XWIN);
1599 CHECK(seq->cwnd >= 1);
1600 CHECK(seq->cwnd <= seq->wmax);
1601 CHECK(seq->ssth >= 1);
1602 CHECK(seq->ssth <= seq->wmax);
1603 if (seq->cwnd < seq->ssth)
1604 CHECK(seq->acks == 0);
1606 CHECK(seq->acks <= seq->cwnd);
1607 CHECK(self_unack >= 0);
1608 CHECK(peer_unack >= 0);
1609 CHECK(peer_unack <= seq->wmax);
1610 CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1611 CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1612 for (i = 0; i < peer_unack; i++)
1613 CHECK(seq->xwin[i] != NULL);
1614 for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
1615 CHECK(seq->xwin[i] == NULL);
1617 mtx_unlock(&seq->mtx);
1621 #endif /* INVARIANTS */