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>
43 * L2TP netgraph node type.
45 * This node type implements the lower layer of the
46 * L2TP protocol as specified in RFC 2661.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/errno.h>
57 #include <sys/libkern.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_l2tp.h>
64 #ifdef NG_SEPARATE_MALLOC
65 MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
67 #define M_NETGRAPH_L2TP M_NETGRAPH
70 /* L2TP header format (first 2 bytes only) */
71 #define L2TP_HDR_CTRL 0x8000 /* control packet */
72 #define L2TP_HDR_LEN 0x4000 /* has length field */
73 #define L2TP_HDR_SEQ 0x0800 /* has ns, nr fields */
74 #define L2TP_HDR_OFF 0x0200 /* has offset field */
75 #define L2TP_HDR_PRIO 0x0100 /* give priority */
76 #define L2TP_HDR_VERS_MASK 0x000f /* version field mask */
77 #define L2TP_HDR_VERSION 0x0002 /* version field */
79 /* Bits that must be zero or one in first two bytes of header */
80 #define L2TP_CTRL_0BITS 0x030d /* ctrl: must be 0 */
81 #define L2TP_CTRL_1BITS 0xc802 /* ctrl: must be 1 */
82 #define L2TP_DATA_0BITS 0x800d /* data: must be 0 */
83 #define L2TP_DATA_1BITS 0x0002 /* data: must be 1 */
85 /* Standard xmit ctrl and data header bits */
86 #define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
87 | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
88 #define L2TP_DATA_HDR (L2TP_HDR_VERSION) /* optional: len, seq */
90 /* Some hard coded values */
91 #define L2TP_MAX_XWIN 16 /* my max xmit window */
92 #define L2TP_MAX_REXMIT 5 /* default max rexmit */
93 #define L2TP_MAX_REXMIT_TO 30 /* default rexmit to */
94 #define L2TP_DELAYED_ACK ((hz + 19) / 20) /* delayed ack: 50 ms */
96 /* Default data sequence number configuration for new sessions */
97 #define L2TP_CONTROL_DSEQ 1 /* we are the lns */
98 #define L2TP_ENABLE_DSEQ 1 /* enable data seq # */
100 /* Compare sequence numbers using circular math */
101 #define L2TP_SEQ_DIFF(x, y) ((int)((int16_t)(x) - (int16_t)(y)))
104 * Sequence number state
107 * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
108 * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
109 * - The first (ns - rack) mbuf's in xwin[] array are copies of these
110 * unacknowledged packets; the remainder of xwin[] consists first of
111 * zero or more further untransmitted packets in the transmit queue
112 * - We try to keep the peer's receive window as full as possible.
113 * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
114 * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
115 * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
116 * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
119 u_int16_t ns; /* next xmit seq we send */
120 u_int16_t nr; /* next recv seq we expect */
121 u_int16_t rack; /* last 'nr' we rec'd */
122 u_int16_t xack; /* last 'nr' we sent */
123 u_int16_t wmax; /* peer's max recv window */
124 u_int16_t cwnd; /* current congestion window */
125 u_int16_t ssth; /* slow start threshold */
126 u_int16_t acks; /* # consecutive acks rec'd */
127 u_int16_t rexmits; /* # retransmits sent */
128 u_int16_t max_rexmits; /* max # retransmits sent */
129 u_int16_t max_rexmit_to; /* max retransmit timeout */
130 struct callout rack_timer; /* retransmit timer */
131 struct callout xack_timer; /* delayed ack timer */
132 struct mbuf *xwin[L2TP_MAX_XWIN]; /* transmit window */
135 /* Node private data */
136 struct ng_l2tp_private {
137 node_p node; /* back pointer to node */
138 hook_p ctrl; /* hook to upper layers */
139 hook_p lower; /* hook to lower layers */
140 struct ng_l2tp_config conf; /* node configuration */
141 struct ng_l2tp_stats stats; /* node statistics */
142 struct l2tp_seq seq; /* ctrl sequence number state */
143 ng_ID_t ftarget; /* failure message target */
145 typedef struct ng_l2tp_private *priv_p;
147 /* Hook private data (data session hooks only) */
148 struct ng_l2tp_hook_private {
149 struct ng_l2tp_sess_config conf; /* hook/session config */
150 struct ng_l2tp_session_stats stats; /* per sessions statistics */
151 u_int16_t ns; /* data ns sequence number */
152 u_int16_t nr; /* data nr sequence number */
154 typedef struct ng_l2tp_hook_private *hookpriv_p;
156 /* Netgraph node methods */
157 static ng_constructor_t ng_l2tp_constructor;
158 static ng_rcvmsg_t ng_l2tp_rcvmsg;
159 static ng_shutdown_t ng_l2tp_shutdown;
160 static ng_newhook_t ng_l2tp_newhook;
161 static ng_rcvdata_t ng_l2tp_rcvdata;
162 static ng_disconnect_t ng_l2tp_disconnect;
164 /* Internal functions */
165 static int ng_l2tp_recv_lower(node_p node, item_p item);
166 static int ng_l2tp_recv_ctrl(node_p node, item_p item);
167 static int ng_l2tp_recv_data(node_p node, item_p item, hookpriv_p hpriv);
169 static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
171 static void ng_l2tp_seq_init(priv_p priv);
172 static int ng_l2tp_seq_set(priv_p priv,
173 const struct ng_l2tp_seq_config *conf);
174 static int ng_l2tp_seq_adjust(priv_p priv,
175 const struct ng_l2tp_config *conf);
176 static void ng_l2tp_seq_reset(priv_p priv);
177 static void ng_l2tp_seq_failure(priv_p priv);
178 static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
179 static int ng_l2tp_seq_recv_ns(priv_p priv, u_int16_t ns);
180 static void ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
181 void *arg1, int arg2);
182 static void ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
183 void *arg1, int arg2);
185 static ng_fn_eachhook ng_l2tp_find_session;
186 static ng_fn_eachhook ng_l2tp_reset_session;
189 static void ng_l2tp_seq_check(struct l2tp_seq *seq);
192 /* Parse type for struct ng_l2tp_seq_config. */
193 static const struct ng_parse_struct_field
194 ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
195 static const struct ng_parse_type ng_l2tp_seq_config_type = {
196 &ng_parse_struct_type,
197 &ng_l2tp_seq_config_fields
200 /* Parse type for struct ng_l2tp_config */
201 static const struct ng_parse_struct_field
202 ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
203 static const struct ng_parse_type ng_l2tp_config_type = {
204 &ng_parse_struct_type,
205 &ng_l2tp_config_type_fields,
208 /* Parse type for struct ng_l2tp_sess_config */
209 static const struct ng_parse_struct_field
210 ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
211 static const struct ng_parse_type ng_l2tp_sess_config_type = {
212 &ng_parse_struct_type,
213 &ng_l2tp_sess_config_type_fields,
216 /* Parse type for struct ng_l2tp_stats */
217 static const struct ng_parse_struct_field
218 ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
219 static const struct ng_parse_type ng_l2tp_stats_type = {
220 &ng_parse_struct_type,
221 &ng_l2tp_stats_type_fields
224 /* Parse type for struct ng_l2tp_session_stats. */
225 static const struct ng_parse_struct_field
226 ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
227 static const struct ng_parse_type ng_l2tp_session_stats_type = {
228 &ng_parse_struct_type,
229 &ng_l2tp_session_stats_type_fields
232 /* List of commands and how to convert arguments to/from ASCII */
233 static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
238 &ng_l2tp_config_type,
250 NGM_L2TP_SET_SESS_CONFIG,
252 &ng_l2tp_sess_config_type,
257 NGM_L2TP_GET_SESS_CONFIG,
259 &ng_parse_hint16_type,
260 &ng_l2tp_sess_config_type
278 NGM_L2TP_GETCLR_STATS,
285 NGM_L2TP_GET_SESSION_STATS,
287 &ng_parse_int16_type,
288 &ng_l2tp_session_stats_type
292 NGM_L2TP_CLR_SESSION_STATS,
294 &ng_parse_int16_type,
299 NGM_L2TP_GETCLR_SESSION_STATS,
301 &ng_parse_int16_type,
302 &ng_l2tp_session_stats_type
306 NGM_L2TP_ACK_FAILURE,
315 &ng_l2tp_seq_config_type,
321 /* Node type descriptor */
322 static struct ng_type ng_l2tp_typestruct = {
323 .version = NG_ABI_VERSION,
324 .name = NG_L2TP_NODE_TYPE,
325 .constructor = ng_l2tp_constructor,
326 .rcvmsg = ng_l2tp_rcvmsg,
327 .shutdown = ng_l2tp_shutdown,
328 .newhook = ng_l2tp_newhook,
329 .rcvdata = ng_l2tp_rcvdata,
330 .disconnect = ng_l2tp_disconnect,
331 .cmdlist = ng_l2tp_cmdlist,
333 NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
335 /* Sequence number state sanity checking */
337 #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
339 #define L2TP_SEQ_CHECK(x) do { } while (0)
343 #define memmove(d, s, l) bcopy(s, d, l)
345 /* Whether to use m_copypacket() or m_dup() */
346 #define L2TP_COPY_MBUF m_copypacket
348 /************************************************************************
350 ************************************************************************/
353 * Node type constructor
356 ng_l2tp_constructor(node_p node)
360 /* Allocate private structure */
361 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
364 NG_NODE_SET_PRIVATE(node, priv);
367 /* Apply a semi-reasonable default configuration */
368 priv->conf.peer_win = 1;
369 priv->conf.rexmit_max = L2TP_MAX_REXMIT;
370 priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
372 /* Initialize sequence number state */
373 ng_l2tp_seq_init(priv);
380 * Give our OK for a hook to be added.
383 ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
385 const priv_p priv = NG_NODE_PRIVATE(node);
387 /* Check hook name */
388 if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
389 if (priv->ctrl != NULL)
392 } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
393 if (priv->lower != NULL)
397 static const char hexdig[16] = "0123456789abcdef";
398 u_int16_t session_id;
404 /* Parse hook name to get session ID */
405 if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
406 sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
408 hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
409 for (session_id = i = 0; i < 4; i++) {
410 for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
413 session_id = (session_id << 4) | j;
418 /* Create hook private structure */
419 MALLOC(hpriv, hookpriv_p,
420 sizeof(*hpriv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
423 hpriv->conf.session_id = htons(session_id);
424 hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
425 hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
426 NG_HOOK_SET_PRIVATE(hook, hpriv);
434 * Receive a control message.
437 ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
439 const priv_p priv = NG_NODE_PRIVATE(node);
440 struct ng_mesg *resp = NULL;
444 NGI_GET_MSG(item, msg);
445 switch (msg->header.typecookie) {
446 case NGM_L2TP_COOKIE:
447 switch (msg->header.cmd) {
448 case NGM_L2TP_SET_CONFIG:
450 struct ng_l2tp_config *const conf =
451 (struct ng_l2tp_config *)msg->data;
453 /* Check for invalid or illegal config */
454 if (msg->header.arglen != sizeof(*conf)) {
458 conf->enabled = !!conf->enabled;
459 conf->match_id = !!conf->match_id;
460 conf->tunnel_id = htons(conf->tunnel_id);
461 conf->peer_id = htons(conf->peer_id);
462 if (priv->conf.enabled
463 && ((priv->conf.tunnel_id != 0
464 && conf->tunnel_id != priv->conf.tunnel_id)
465 || ((priv->conf.peer_id != 0
466 && conf->peer_id != priv->conf.peer_id)))) {
471 /* Save calling node as failure target */
472 priv->ftarget = NGI_RETADDR(item);
474 /* Adjust sequence number state */
475 if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
478 /* Update node's config */
482 case NGM_L2TP_GET_CONFIG:
484 struct ng_l2tp_config *conf;
486 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
491 conf = (struct ng_l2tp_config *)resp->data;
494 /* Put ID's in host order */
495 conf->tunnel_id = ntohs(conf->tunnel_id);
496 conf->peer_id = ntohs(conf->peer_id);
499 case NGM_L2TP_SET_SESS_CONFIG:
501 struct ng_l2tp_sess_config *const conf =
502 (struct ng_l2tp_sess_config *)msg->data;
506 /* Check for invalid or illegal config. */
507 if (msg->header.arglen != sizeof(*conf)) {
512 /* Put ID's in network order */
513 conf->session_id = htons(conf->session_id);
514 conf->peer_id = htons(conf->peer_id);
516 /* Find matching hook */
517 NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
518 (void *)(uintptr_t)conf->session_id, hook);
523 hpriv = NG_HOOK_PRIVATE(hook);
525 /* Update hook's config */
529 case NGM_L2TP_GET_SESS_CONFIG:
531 struct ng_l2tp_sess_config *conf;
532 u_int16_t session_id;
537 if (msg->header.arglen != sizeof(session_id)) {
541 memcpy(&session_id, msg->data, 2);
542 session_id = htons(session_id);
544 /* Find matching hook */
545 NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
546 (void *)(uintptr_t)session_id, hook);
551 hpriv = NG_HOOK_PRIVATE(hook);
554 NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
559 conf = (struct ng_l2tp_sess_config *)resp->data;
562 /* Put ID's in host order */
563 conf->session_id = ntohs(conf->session_id);
564 conf->peer_id = ntohs(conf->peer_id);
567 case NGM_L2TP_GET_STATS:
568 case NGM_L2TP_CLR_STATS:
569 case NGM_L2TP_GETCLR_STATS:
571 if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
572 NG_MKRESPONSE(resp, msg,
573 sizeof(priv->stats), M_NOWAIT);
579 &priv->stats, sizeof(priv->stats));
581 if (msg->header.cmd != NGM_L2TP_GET_STATS)
582 memset(&priv->stats, 0, sizeof(priv->stats));
585 case NGM_L2TP_GET_SESSION_STATS:
586 case NGM_L2TP_CLR_SESSION_STATS:
587 case NGM_L2TP_GETCLR_SESSION_STATS:
593 /* Get session ID. */
594 if (msg->header.arglen != sizeof(session_id)) {
598 bcopy(msg->data, &session_id, sizeof(uint16_t));
599 session_id = htons(session_id);
601 /* Find matching hook. */
602 NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
603 (void *)(uintptr_t)session_id, hook);
608 hpriv = NG_HOOK_PRIVATE(hook);
610 if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
611 NG_MKRESPONSE(resp, msg,
612 sizeof(hpriv->stats), M_NOWAIT);
617 bcopy(&hpriv->stats, resp->data,
618 sizeof(hpriv->stats));
620 if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
621 bzero(&hpriv->stats, sizeof(hpriv->stats));
624 case NGM_L2TP_SET_SEQ:
626 struct ng_l2tp_seq_config *const conf =
627 (struct ng_l2tp_seq_config *)msg->data;
629 /* Check for invalid or illegal seq config. */
630 if (msg->header.arglen != sizeof(*conf)) {
634 conf->ns = htons(conf->ns);
635 conf->nr = htons(conf->nr);
636 conf->rack = htons(conf->rack);
637 conf->xack = htons(conf->xack);
639 /* Set sequence numbers. */
640 error = ng_l2tp_seq_set(priv, conf);
654 NG_RESPOND_MSG(error, node, item, resp);
660 * Receive incoming data on a hook.
663 ng_l2tp_rcvdata(hook_p hook, item_p item)
665 const node_p node = NG_HOOK_NODE(hook);
666 const priv_p priv = NG_NODE_PRIVATE(node);
670 L2TP_SEQ_CHECK(&priv->seq);
672 /* If not configured, reject */
673 if (!priv->conf.enabled) {
678 /* Handle incoming frame from below */
679 if (hook == priv->lower) {
680 error = ng_l2tp_recv_lower(node, item);
684 /* Handle outgoing control frame */
685 if (hook == priv->ctrl) {
686 error = ng_l2tp_recv_ctrl(node, item);
690 /* Handle outgoing data frame */
691 error = ng_l2tp_recv_data(node, item, NG_HOOK_PRIVATE(hook));
695 L2TP_SEQ_CHECK(&priv->seq);
703 ng_l2tp_shutdown(node_p node)
705 const priv_p priv = NG_NODE_PRIVATE(node);
706 struct l2tp_seq *const seq = &priv->seq;
711 /* Reset sequence number state */
712 ng_l2tp_seq_reset(priv);
714 /* Free private data if neither timer is running */
715 ng_uncallout(&seq->rack_timer, node);
716 ng_uncallout(&seq->xack_timer, node);
718 FREE(priv, M_NETGRAPH_L2TP);
729 ng_l2tp_disconnect(hook_p hook)
731 const node_p node = NG_HOOK_NODE(hook);
732 const priv_p priv = NG_NODE_PRIVATE(node);
734 /* Zero out hook pointer */
735 if (hook == priv->ctrl)
737 else if (hook == priv->lower)
740 FREE(NG_HOOK_PRIVATE(hook), M_NETGRAPH_L2TP);
741 NG_HOOK_SET_PRIVATE(hook, NULL);
744 /* Go away if no longer connected to anything */
745 if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
746 ng_rmnode_self(node);
750 /*************************************************************************
752 *************************************************************************/
755 * Find the hook with a given session ID.
758 ng_l2tp_find_session(hook_p hook, void *arg)
760 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
761 const u_int16_t sid = (u_int16_t)(uintptr_t)arg;
763 if (hpriv == NULL || hpriv->conf.session_id != sid)
769 * Reset a hook's session state.
772 ng_l2tp_reset_session(hook_p hook, void *arg)
774 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
777 hpriv->conf.control_dseq = 0;
778 hpriv->conf.enable_dseq = 0;
779 bzero(&hpriv->conf, sizeof(struct ng_l2tp_session_stats));
787 * Handle an incoming frame from below.
790 ng_l2tp_recv_lower(node_p node, item_p item)
792 static const u_int16_t req_bits[2][2] = {
793 { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
794 { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
796 const priv_p priv = NG_NODE_PRIVATE(node);
797 hookpriv_p hpriv = NULL;
811 /* Remember full packet length; needed for per session accounting. */
812 plen = m->m_pkthdr.len;
815 priv->stats.recvPackets++;
816 priv->stats.recvOctets += plen;
818 /* Get initial header */
819 if (m->m_pkthdr.len < 6) {
820 priv->stats.recvRunts++;
825 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
826 priv->stats.memoryFailures++;
830 hdr = ntohs(*mtod(m, u_int16_t *));
833 /* Check required header bits and minimum length */
834 is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
835 if ((hdr & req_bits[is_ctrl][0]) != 0
836 || (~hdr & req_bits[is_ctrl][1]) != 0) {
837 priv->stats.recvInvalid++;
842 if (m->m_pkthdr.len < 4 /* tunnel, session id */
843 + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
844 + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
845 + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
846 priv->stats.recvRunts++;
852 /* Get and validate length field if present */
853 if ((hdr & L2TP_HDR_LEN) != 0) {
854 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
855 priv->stats.memoryFailures++;
859 len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
861 if (len < 0 || len > m->m_pkthdr.len) {
862 priv->stats.recvInvalid++;
867 if (len < m->m_pkthdr.len) /* trim extra bytes */
868 m_adj(m, -(m->m_pkthdr.len - len));
871 /* Get tunnel ID and session ID */
872 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
873 priv->stats.memoryFailures++;
877 memcpy(ids, mtod(m, u_int16_t *), 4);
880 /* Check tunnel ID */
881 if (ids[0] != priv->conf.tunnel_id
882 && (priv->conf.match_id || ids[0] != 0)) {
883 priv->stats.recvWrongTunnel++;
886 return (EADDRNOTAVAIL);
889 /* Check session ID (for data packets only) */
890 if ((hdr & L2TP_HDR_CTRL) == 0) {
891 NG_NODE_FOREACH_HOOK(node, ng_l2tp_find_session,
892 (void *)(uintptr_t)ids[1], hook);
894 priv->stats.recvUnknownSID++;
899 hpriv = NG_HOOK_PRIVATE(hook);
902 /* Get Ns, Nr fields if present */
903 if ((hdr & L2TP_HDR_SEQ) != 0) {
904 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
905 priv->stats.memoryFailures++;
909 memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
911 memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
916 /* Strip offset padding if present */
917 if ((hdr & L2TP_HDR_OFF) != 0) {
920 /* Get length of offset padding */
921 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
922 priv->stats.memoryFailures++;
926 memcpy(&offset, mtod(m, u_int16_t *), 2);
927 offset = ntohs(offset);
929 /* Trim offset padding */
930 if ((2+offset) > m->m_pkthdr.len) {
931 priv->stats.recvInvalid++;
939 /* Handle control packets */
940 if ((hdr & L2TP_HDR_CTRL) != 0) {
942 /* Handle receive ack sequence number Nr */
943 ng_l2tp_seq_recv_nr(priv, nr);
945 /* Discard ZLB packets */
946 if (m->m_pkthdr.len == 0) {
947 priv->stats.recvZLBs++;
954 * Prepend session ID to packet here: we don't want to accept
955 * the send sequence number Ns if we have to drop the packet
956 * later because of a memory error, because then the upper
957 * layer would never get the packet.
959 M_PREPEND(m, 2, M_DONTWAIT);
961 priv->stats.memoryFailures++;
965 memcpy(mtod(m, u_int16_t *), &ids[1], 2);
967 /* Now handle send sequence number */
968 if (ng_l2tp_seq_recv_ns(priv, ns) == -1) {
974 /* Deliver packet to upper layers */
975 NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
979 /* Per session packet, account it. */
980 hpriv->stats.recvPackets++;
981 hpriv->stats.recvOctets += plen;
983 /* Follow peer's lead in data sequencing, if configured to do so */
984 if (!hpriv->conf.control_dseq)
985 hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
987 /* Handle data sequence numbers if present and enabled */
988 if ((hdr & L2TP_HDR_SEQ) != 0) {
989 if (hpriv->conf.enable_dseq
990 && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
991 NG_FREE_ITEM(item); /* duplicate or out of order */
993 priv->stats.recvDataDrops++;
999 /* Drop empty data packets */
1000 if (m->m_pkthdr.len == 0) {
1007 NG_FWD_NEW_DATA(error, item, hook, m);
1012 * Handle an outgoing control frame.
1015 ng_l2tp_recv_ctrl(node_p node, item_p item)
1017 const priv_p priv = NG_NODE_PRIVATE(node);
1018 struct l2tp_seq *const seq = &priv->seq;
1022 /* Grab mbuf and discard other stuff XXX */
1026 /* Packet should have session ID prepended */
1027 if (m->m_pkthdr.len < 2) {
1028 priv->stats.xmitInvalid++;
1033 /* Check max length */
1034 if (m->m_pkthdr.len >= 0x10000 - 14) {
1035 priv->stats.xmitTooBig++;
1040 /* Find next empty slot in transmit queue */
1041 for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1042 if (i == L2TP_MAX_XWIN) {
1043 priv->stats.xmitDrops++;
1049 /* Sanity check receive ack timer state */
1050 KASSERT((i == 0) ^ callout_pending(&seq->rack_timer),
1051 ("%s: xwin %d full but rack timer %s running",
1052 __func__, i, callout_pending(&seq->rack_timer) ? "" : "not "));
1054 /* If peer's receive window is already full, nothing else to do */
1058 /* Start retransmit timer if not already running */
1059 if (!callout_pending(&seq->rack_timer))
1060 ng_callout(&seq->rack_timer, node, NULL,
1061 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1064 if ((m = L2TP_COPY_MBUF(seq->xwin[i], M_DONTWAIT)) == NULL) {
1065 priv->stats.memoryFailures++;
1069 /* Send packet and increment xmit sequence number */
1070 return (ng_l2tp_xmit_ctrl(priv, m, seq->ns++));
1074 * Handle an outgoing data frame.
1077 ng_l2tp_recv_data(node_p node, item_p item, hookpriv_p hpriv)
1079 const priv_p priv = NG_NODE_PRIVATE(node);
1088 /* Check max length */
1089 if (m->m_pkthdr.len >= 0x10000 - 12) {
1090 priv->stats.xmitDataTooBig++;
1096 /* Prepend L2TP header */
1098 + (2 * (hpriv->conf.include_length != 0))
1099 + (4 * (hpriv->conf.enable_dseq != 0)),
1102 priv->stats.memoryFailures++;
1106 hdr = L2TP_DATA_HDR;
1107 if (hpriv->conf.include_length) {
1108 hdr |= L2TP_HDR_LEN;
1109 mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
1111 mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
1112 mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
1113 if (hpriv->conf.enable_dseq) {
1114 hdr |= L2TP_HDR_SEQ;
1115 mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
1116 mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
1119 mtod(m, u_int16_t *)[0] = htons(hdr);
1121 /* Update per session stats. */
1122 hpriv->stats.xmitPackets++;
1123 hpriv->stats.xmitOctets += m->m_pkthdr.len;
1126 NG_FWD_NEW_DATA(error, item, priv->lower, m);
1131 * Send a message to our controlling node that we've failed.
1134 ng_l2tp_seq_failure(priv_p priv)
1136 struct ng_mesg *msg;
1139 NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1142 NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1145 /************************************************************************
1146 SEQUENCE NUMBER HANDLING
1147 ************************************************************************/
1150 * Initialize sequence number state.
1153 ng_l2tp_seq_init(priv_p priv)
1155 struct l2tp_seq *const seq = &priv->seq;
1157 KASSERT(priv->conf.peer_win >= 1,
1158 ("%s: peer_win is zero", __func__));
1159 memset(seq, 0, sizeof(*seq));
1161 seq->wmax = priv->conf.peer_win;
1162 if (seq->wmax > L2TP_MAX_XWIN)
1163 seq->wmax = L2TP_MAX_XWIN;
1164 seq->ssth = seq->wmax;
1165 seq->max_rexmits = priv->conf.rexmit_max;
1166 seq->max_rexmit_to = priv->conf.rexmit_max_to;
1167 ng_callout_init(&seq->rack_timer);
1168 ng_callout_init(&seq->xack_timer);
1169 L2TP_SEQ_CHECK(seq);
1173 * Set sequence number state as given from user.
1176 ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
1178 struct l2tp_seq *const seq = &priv->seq;
1180 /* If node is enabled, deny update to sequence numbers. */
1181 if (priv->conf.enabled)
1184 /* We only can handle the simple cases. */
1185 if (conf->xack != conf->nr || conf->ns != conf->rack)
1188 /* Set ns,nr,rack,xack parameters. */
1191 seq->rack = conf->rack;
1192 seq->xack = conf->xack;
1198 * Adjust sequence number state accordingly after reconfiguration.
1201 ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1203 struct l2tp_seq *const seq = &priv->seq;
1206 /* If disabling node, reset state sequence number */
1207 if (!conf->enabled) {
1208 ng_l2tp_seq_reset(priv);
1212 /* Adjust peer's max recv window; it can only increase */
1213 new_wmax = conf->peer_win;
1214 if (new_wmax > L2TP_MAX_XWIN)
1215 new_wmax = L2TP_MAX_XWIN;
1218 if (new_wmax < seq->wmax)
1220 seq->wmax = new_wmax;
1222 /* Update retransmit parameters */
1223 seq->max_rexmits = conf->rexmit_max;
1224 seq->max_rexmit_to = conf->rexmit_max_to;
1231 * Reset sequence number state.
1234 ng_l2tp_seq_reset(priv_p priv)
1236 struct l2tp_seq *const seq = &priv->seq;
1241 L2TP_SEQ_CHECK(seq);
1244 ng_uncallout(&seq->rack_timer, priv->node);
1245 ng_uncallout(&seq->xack_timer, priv->node);
1247 /* Free retransmit queue */
1248 for (i = 0; i < L2TP_MAX_XWIN; i++) {
1249 if (seq->xwin[i] == NULL)
1251 m_freem(seq->xwin[i]);
1254 /* Reset session hooks' sequence number states */
1255 NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1257 /* Reset node's sequence number state */
1258 memset(seq, 0, sizeof(*seq));
1260 seq->wmax = L2TP_MAX_XWIN;
1261 seq->ssth = seq->wmax;
1264 L2TP_SEQ_CHECK(seq);
1268 * Handle receipt of an acknowledgement value (Nr) from peer.
1271 ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1273 struct l2tp_seq *const seq = &priv->seq;
1278 /* Verify peer's ACK is in range */
1279 if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0)
1280 return; /* duplicate ack */
1281 if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1282 priv->stats.recvBadAcks++; /* ack for packet not sent */
1285 KASSERT(nack <= L2TP_MAX_XWIN,
1286 ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1288 /* Update receive ack stats */
1292 /* Free acknowledged packets and shift up packets in the xmit queue */
1293 for (i = 0; i < nack; i++)
1294 m_freem(seq->xwin[i]);
1295 memmove(seq->xwin, seq->xwin + nack,
1296 (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1297 memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1298 nack * sizeof(*seq->xwin));
1301 * Do slow-start/congestion avoidance windowing algorithm described
1302 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1303 * ACK had arrived separately.
1305 if (seq->cwnd < seq->wmax) {
1307 /* Handle slow start phase */
1308 if (seq->cwnd < seq->ssth) {
1311 if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
1312 nack = seq->cwnd - seq->ssth;
1313 seq->cwnd = seq->ssth;
1317 /* Handle congestion avoidance phase */
1318 if (seq->cwnd >= seq->ssth) {
1320 while (seq->acks >= seq->cwnd) {
1321 seq->acks -= seq->cwnd;
1322 if (seq->cwnd < seq->wmax)
1328 /* Stop xmit timer */
1329 if (callout_pending(&seq->rack_timer))
1330 ng_uncallout(&seq->rack_timer, priv->node);
1332 /* If transmit queue is empty, we're done for now */
1333 if (seq->xwin[0] == NULL)
1336 /* Start restransmit timer again */
1337 ng_callout(&seq->rack_timer, priv->node, NULL,
1338 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1341 * Send more packets, trying to keep peer's receive window full.
1342 * If there is a memory error, pretend packet was sent, as it
1343 * will get retransmitted later anyway.
1345 while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1346 && seq->xwin[i] != NULL) {
1347 if ((m = L2TP_COPY_MBUF(seq->xwin[i], M_DONTWAIT)) == NULL)
1348 priv->stats.memoryFailures++;
1350 ng_l2tp_xmit_ctrl(priv, m, seq->ns);
1356 * Handle receipt of a sequence number value (Ns) from peer.
1357 * We make no attempt to re-order out of order packets.
1359 * This function should only be called for non-ZLB packets.
1366 ng_l2tp_seq_recv_ns(priv_p priv, u_int16_t ns)
1368 struct l2tp_seq *const seq = &priv->seq;
1370 /* If not what we expect, drop packet and send an immediate ZLB ack */
1371 if (ns != seq->nr) {
1372 if (L2TP_SEQ_DIFF(ns, seq->nr) < 0)
1373 priv->stats.recvDuplicates++;
1375 priv->stats.recvOutOfOrder++;
1376 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1380 /* Update recv sequence number */
1383 /* Start receive ack timer, if not already running */
1384 if (!callout_pending(&seq->xack_timer))
1385 ng_callout(&seq->xack_timer, priv->node, NULL,
1386 L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout, NULL, 0);
1393 * Handle an ack timeout. We have an outstanding ack that we
1394 * were hoping to piggy-back, but haven't, so send a ZLB.
1397 ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1399 const priv_p priv = NG_NODE_PRIVATE(node);
1400 struct l2tp_seq *const seq = &priv->seq;
1403 L2TP_SEQ_CHECK(seq);
1405 /* If ack is still outstanding, send a ZLB */
1406 if (seq->xack != seq->nr)
1407 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1410 L2TP_SEQ_CHECK(seq);
1414 * Handle a transmit timeout. The peer has failed to respond
1415 * with an ack for our packet, so retransmit it.
1418 ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1420 const priv_p priv = NG_NODE_PRIVATE(node);
1421 struct l2tp_seq *const seq = &priv->seq;
1426 L2TP_SEQ_CHECK(seq);
1428 /* Make sure peer's ack is still outstanding before doing anything */
1429 if (seq->rack == seq->ns)
1432 priv->stats.xmitRetransmits++;
1434 /* Have we reached the retransmit limit? If so, notify owner. */
1435 if (seq->rexmits++ >= seq->max_rexmits)
1436 ng_l2tp_seq_failure(priv);
1438 /* Restart timer, this time with an increased delay */
1439 delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
1440 if (delay > seq->max_rexmit_to)
1441 delay = seq->max_rexmit_to;
1442 ng_callout(&seq->rack_timer, node, NULL,
1443 hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1445 /* Do slow-start/congestion algorithm windowing algorithm */
1446 seq->ssth = (seq->cwnd + 1) / 2;
1450 /* Retransmit oldest unack'd packet */
1451 if ((m = L2TP_COPY_MBUF(seq->xwin[0], M_DONTWAIT)) == NULL)
1452 priv->stats.memoryFailures++;
1454 ng_l2tp_xmit_ctrl(priv, m, seq->rack);
1458 L2TP_SEQ_CHECK(seq);
1462 * Transmit a control stream packet, payload optional.
1463 * The transmit sequence number is not incremented.
1466 ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1468 struct l2tp_seq *const seq = &priv->seq;
1469 u_int16_t session_id = 0;
1472 /* If no mbuf passed, send an empty packet (ZLB) */
1475 /* Create a new mbuf for ZLB packet */
1476 MGETHDR(m, M_DONTWAIT, MT_DATA);
1478 priv->stats.memoryFailures++;
1481 m->m_len = m->m_pkthdr.len = 12;
1482 m->m_pkthdr.rcvif = NULL;
1483 priv->stats.xmitZLBs++;
1486 /* Strip off session ID */
1487 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1488 priv->stats.memoryFailures++;
1491 memcpy(&session_id, mtod(m, u_int16_t *), 2);
1494 /* Make room for L2TP header */
1495 M_PREPEND(m, 12, M_DONTWAIT);
1497 priv->stats.memoryFailures++;
1502 /* Fill in L2TP header */
1503 mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
1504 mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
1505 mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
1506 mtod(m, u_int16_t *)[3] = session_id;
1507 mtod(m, u_int16_t *)[4] = htons(ns);
1508 mtod(m, u_int16_t *)[5] = htons(seq->nr);
1510 /* Update sequence number info and stats */
1511 priv->stats.xmitPackets++;
1512 priv->stats.xmitOctets += m->m_pkthdr.len;
1514 /* Stop ack timer: we're sending an ack with this packet */
1515 if (callout_pending(&seq->xack_timer))
1516 ng_uncallout(&seq->xack_timer, priv->node);
1518 seq->xack = seq->nr;
1521 NG_SEND_DATA_ONLY(error, priv->lower, m);
1527 * Sanity check sequence number state.
1530 ng_l2tp_seq_check(struct l2tp_seq *seq)
1532 const int self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1533 const int peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1536 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1538 CHECK(seq->wmax <= L2TP_MAX_XWIN);
1539 CHECK(seq->cwnd >= 1);
1540 CHECK(seq->cwnd <= seq->wmax);
1541 CHECK(seq->ssth >= 1);
1542 CHECK(seq->ssth <= seq->wmax);
1543 if (seq->cwnd < seq->ssth)
1544 CHECK(seq->acks == 0);
1546 CHECK(seq->acks <= seq->cwnd);
1547 CHECK(self_unack >= 0);
1548 CHECK(peer_unack >= 0);
1549 CHECK(peer_unack <= seq->wmax);
1550 CHECK((self_unack == 0) ^ callout_pending(&seq->xack_timer));
1551 CHECK((peer_unack == 0) ^ callout_pending(&seq->rack_timer));
1552 for (i = 0; i < peer_unack; i++)
1553 CHECK(seq->xwin[i] != NULL);
1554 for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
1555 CHECK(seq->xwin[i] == NULL);
1559 #endif /* INVARIANTS */