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 128 /* 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)))
103 #define SESSHASHSIZE 0x0020
104 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
106 /* Hook private data (data session hooks only) */
107 struct ng_l2tp_hook_private {
108 struct ng_l2tp_sess_config conf; /* hook/session config */
109 struct ng_l2tp_session_stats stats; /* per sessions statistics */
110 hook_p hook; /* hook reference */
111 u_int16_t ns; /* data ns sequence number */
112 u_int16_t nr; /* data nr sequence number */
113 LIST_ENTRY(ng_l2tp_hook_private) sessions;
115 typedef struct ng_l2tp_hook_private *hookpriv_p;
118 * Sequence number state
121 * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
122 * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
123 * - The first (ns - rack) mbuf's in xwin[] array are copies of these
124 * unacknowledged packets; the remainder of xwin[] consists first of
125 * zero or more further untransmitted packets in the transmit queue
126 * - We try to keep the peer's receive window as full as possible.
127 * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
128 * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
129 * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
130 * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
133 u_int16_t ns; /* next xmit seq we send */
134 u_int16_t nr; /* next recv seq we expect */
135 u_int16_t inproc; /* packet is in processing */
136 u_int16_t rack; /* last 'nr' we rec'd */
137 u_int16_t xack; /* last 'nr' we sent */
138 u_int16_t wmax; /* peer's max recv window */
139 u_int16_t cwnd; /* current congestion window */
140 u_int16_t ssth; /* slow start threshold */
141 u_int16_t acks; /* # consecutive acks rec'd */
142 u_int16_t rexmits; /* # retransmits sent */
143 struct callout rack_timer; /* retransmit timer */
144 struct callout xack_timer; /* delayed ack timer */
145 struct mbuf *xwin[L2TP_MAX_XWIN]; /* transmit window */
146 struct mtx mtx; /* seq mutex */
149 /* Node private data */
150 struct ng_l2tp_private {
151 node_p node; /* back pointer to node */
152 hook_p ctrl; /* hook to upper layers */
153 hook_p lower; /* hook to lower layers */
154 struct ng_l2tp_config conf; /* node configuration */
155 struct ng_l2tp_stats stats; /* node statistics */
156 struct l2tp_seq seq; /* ctrl sequence number state */
157 ng_ID_t ftarget; /* failure message target */
158 LIST_HEAD(, ng_l2tp_hook_private) sesshash[SESSHASHSIZE];
160 typedef struct ng_l2tp_private *priv_p;
162 /* Netgraph node methods */
163 static ng_constructor_t ng_l2tp_constructor;
164 static ng_rcvmsg_t ng_l2tp_rcvmsg;
165 static ng_shutdown_t ng_l2tp_shutdown;
166 static ng_newhook_t ng_l2tp_newhook;
167 static ng_rcvdata_t ng_l2tp_rcvdata;
168 static ng_rcvdata_t ng_l2tp_rcvdata_lower;
169 static ng_rcvdata_t ng_l2tp_rcvdata_ctrl;
170 static ng_disconnect_t ng_l2tp_disconnect;
172 /* Internal functions */
173 static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
175 static void ng_l2tp_seq_init(priv_p priv);
176 static int ng_l2tp_seq_set(priv_p priv,
177 const struct ng_l2tp_seq_config *conf);
178 static int ng_l2tp_seq_adjust(priv_p priv,
179 const struct ng_l2tp_config *conf);
180 static void ng_l2tp_seq_reset(priv_p priv);
181 static void ng_l2tp_seq_failure(priv_p priv);
182 static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
183 static void ng_l2tp_seq_xack_timeout(node_p node, hook_p hook,
184 void *arg1, int arg2);
185 static void ng_l2tp_seq_rack_timeout(node_p node, hook_p hook,
186 void *arg1, int arg2);
188 static hookpriv_p ng_l2tp_find_session(priv_p privp, u_int16_t sid);
189 static ng_fn_eachhook ng_l2tp_reset_session;
192 static void ng_l2tp_seq_check(struct l2tp_seq *seq);
195 /* Parse type for struct ng_l2tp_seq_config. */
196 static const struct ng_parse_struct_field
197 ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
198 static const struct ng_parse_type ng_l2tp_seq_config_type = {
199 &ng_parse_struct_type,
200 &ng_l2tp_seq_config_fields
203 /* Parse type for struct ng_l2tp_config */
204 static const struct ng_parse_struct_field
205 ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
206 static const struct ng_parse_type ng_l2tp_config_type = {
207 &ng_parse_struct_type,
208 &ng_l2tp_config_type_fields,
211 /* Parse type for struct ng_l2tp_sess_config */
212 static const struct ng_parse_struct_field
213 ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
214 static const struct ng_parse_type ng_l2tp_sess_config_type = {
215 &ng_parse_struct_type,
216 &ng_l2tp_sess_config_type_fields,
219 /* Parse type for struct ng_l2tp_stats */
220 static const struct ng_parse_struct_field
221 ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
222 static const struct ng_parse_type ng_l2tp_stats_type = {
223 &ng_parse_struct_type,
224 &ng_l2tp_stats_type_fields
227 /* Parse type for struct ng_l2tp_session_stats. */
228 static const struct ng_parse_struct_field
229 ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
230 static const struct ng_parse_type ng_l2tp_session_stats_type = {
231 &ng_parse_struct_type,
232 &ng_l2tp_session_stats_type_fields
235 /* List of commands and how to convert arguments to/from ASCII */
236 static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
241 &ng_l2tp_config_type,
253 NGM_L2TP_SET_SESS_CONFIG,
255 &ng_l2tp_sess_config_type,
260 NGM_L2TP_GET_SESS_CONFIG,
262 &ng_parse_hint16_type,
263 &ng_l2tp_sess_config_type
281 NGM_L2TP_GETCLR_STATS,
288 NGM_L2TP_GET_SESSION_STATS,
290 &ng_parse_int16_type,
291 &ng_l2tp_session_stats_type
295 NGM_L2TP_CLR_SESSION_STATS,
297 &ng_parse_int16_type,
302 NGM_L2TP_GETCLR_SESSION_STATS,
304 &ng_parse_int16_type,
305 &ng_l2tp_session_stats_type
309 NGM_L2TP_ACK_FAILURE,
318 &ng_l2tp_seq_config_type,
324 /* Node type descriptor */
325 static struct ng_type ng_l2tp_typestruct = {
326 .version = NG_ABI_VERSION,
327 .name = NG_L2TP_NODE_TYPE,
328 .constructor = ng_l2tp_constructor,
329 .rcvmsg = ng_l2tp_rcvmsg,
330 .shutdown = ng_l2tp_shutdown,
331 .newhook = ng_l2tp_newhook,
332 .rcvdata = ng_l2tp_rcvdata,
333 .disconnect = ng_l2tp_disconnect,
334 .cmdlist = ng_l2tp_cmdlist,
336 NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
338 /* Sequence number state sanity checking */
340 #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
342 #define L2TP_SEQ_CHECK(x) do { } while (0)
345 /* Whether to use m_copypacket() or m_dup() */
346 #define L2TP_COPY_MBUF m_copypacket
348 #define ERROUT(x) do { error = (x); goto done; } while (0)
350 /************************************************************************
352 ************************************************************************/
355 * Node type constructor
358 ng_l2tp_constructor(node_p node)
363 /* Allocate private structure */
364 priv = malloc(sizeof(*priv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
367 NG_NODE_SET_PRIVATE(node, priv);
370 /* Apply a semi-reasonable default configuration */
371 priv->conf.peer_win = 1;
372 priv->conf.rexmit_max = L2TP_MAX_REXMIT;
373 priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
375 /* Initialize sequence number state */
376 ng_l2tp_seq_init(priv);
378 for (i = 0; i < SESSHASHSIZE; i++)
379 LIST_INIT(&priv->sesshash[i]);
386 * Give our OK for a hook to be added.
389 ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
391 const priv_p priv = NG_NODE_PRIVATE(node);
393 /* Check hook name */
394 if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
395 if (priv->ctrl != NULL)
398 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
399 } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
400 if (priv->lower != NULL)
403 NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
405 static const char hexdig[16] = "0123456789abcdef";
406 u_int16_t session_id;
413 /* Parse hook name to get session ID */
414 if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
415 sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
417 hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
418 for (session_id = i = 0; i < 4; i++) {
419 for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
422 session_id = (session_id << 4) | j;
427 /* Create hook private structure */
428 hpriv = malloc(sizeof(*hpriv),
429 M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
432 hpriv->conf.session_id = session_id;
433 hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
434 hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
436 NG_HOOK_SET_PRIVATE(hook, hpriv);
437 hash = SESSHASH(hpriv->conf.session_id);
438 LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
446 * Receive a control message.
449 ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
451 const priv_p priv = NG_NODE_PRIVATE(node);
452 struct ng_mesg *resp = NULL;
456 NGI_GET_MSG(item, msg);
457 switch (msg->header.typecookie) {
458 case NGM_L2TP_COOKIE:
459 switch (msg->header.cmd) {
460 case NGM_L2TP_SET_CONFIG:
462 struct ng_l2tp_config *const conf =
463 (struct ng_l2tp_config *)msg->data;
465 /* Check for invalid or illegal config */
466 if (msg->header.arglen != sizeof(*conf)) {
470 conf->enabled = !!conf->enabled;
471 conf->match_id = !!conf->match_id;
472 if (priv->conf.enabled
473 && ((priv->conf.tunnel_id != 0
474 && conf->tunnel_id != priv->conf.tunnel_id)
475 || ((priv->conf.peer_id != 0
476 && conf->peer_id != priv->conf.peer_id)))) {
481 /* Save calling node as failure target */
482 priv->ftarget = NGI_RETADDR(item);
484 /* Adjust sequence number state */
485 if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
488 /* Update node's config */
492 case NGM_L2TP_GET_CONFIG:
494 struct ng_l2tp_config *conf;
496 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
501 conf = (struct ng_l2tp_config *)resp->data;
505 case NGM_L2TP_SET_SESS_CONFIG:
507 struct ng_l2tp_sess_config *const conf =
508 (struct ng_l2tp_sess_config *)msg->data;
511 /* Check for invalid or illegal config. */
512 if (msg->header.arglen != sizeof(*conf)) {
517 /* Find matching hook */
518 hpriv = ng_l2tp_find_session(priv, conf->session_id);
524 /* Update hook's config */
528 case NGM_L2TP_GET_SESS_CONFIG:
530 struct ng_l2tp_sess_config *conf;
531 u_int16_t session_id;
535 if (msg->header.arglen != sizeof(session_id)) {
539 memcpy(&session_id, msg->data, 2);
541 /* Find matching hook */
542 hpriv = ng_l2tp_find_session(priv, session_id);
549 NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
554 conf = (struct ng_l2tp_sess_config *)resp->data;
558 case NGM_L2TP_GET_STATS:
559 case NGM_L2TP_CLR_STATS:
560 case NGM_L2TP_GETCLR_STATS:
562 if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
563 NG_MKRESPONSE(resp, msg,
564 sizeof(priv->stats), M_NOWAIT);
570 &priv->stats, sizeof(priv->stats));
572 if (msg->header.cmd != NGM_L2TP_GET_STATS)
573 memset(&priv->stats, 0, sizeof(priv->stats));
576 case NGM_L2TP_GET_SESSION_STATS:
577 case NGM_L2TP_CLR_SESSION_STATS:
578 case NGM_L2TP_GETCLR_SESSION_STATS:
583 /* Get session ID. */
584 if (msg->header.arglen != sizeof(session_id)) {
588 bcopy(msg->data, &session_id, sizeof(uint16_t));
590 /* Find matching hook. */
591 hpriv = ng_l2tp_find_session(priv, session_id);
597 if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
598 NG_MKRESPONSE(resp, msg,
599 sizeof(hpriv->stats), M_NOWAIT);
604 bcopy(&hpriv->stats, resp->data,
605 sizeof(hpriv->stats));
607 if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
608 bzero(&hpriv->stats, sizeof(hpriv->stats));
611 case NGM_L2TP_SET_SEQ:
613 struct ng_l2tp_seq_config *const conf =
614 (struct ng_l2tp_seq_config *)msg->data;
616 /* Check for invalid or illegal seq config. */
617 if (msg->header.arglen != sizeof(*conf)) {
621 conf->ns = htons(conf->ns);
622 conf->nr = htons(conf->nr);
623 conf->rack = htons(conf->rack);
624 conf->xack = htons(conf->xack);
626 /* Set sequence numbers. */
627 error = ng_l2tp_seq_set(priv, conf);
641 NG_RESPOND_MSG(error, node, item, resp);
650 ng_l2tp_shutdown(node_p node)
652 const priv_p priv = NG_NODE_PRIVATE(node);
653 struct l2tp_seq *const seq = &priv->seq;
658 /* Reset sequence number state */
659 ng_l2tp_seq_reset(priv);
661 /* Free private data if neither timer is running */
662 ng_uncallout(&seq->rack_timer, node);
663 ng_uncallout(&seq->xack_timer, node);
665 mtx_destroy(&seq->mtx);
667 free(priv, M_NETGRAPH_L2TP);
678 ng_l2tp_disconnect(hook_p hook)
680 const node_p node = NG_HOOK_NODE(hook);
681 const priv_p priv = NG_NODE_PRIVATE(node);
683 /* Zero out hook pointer */
684 if (hook == priv->ctrl)
686 else if (hook == priv->lower)
689 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
690 LIST_REMOVE(hpriv, sessions);
691 free(hpriv, M_NETGRAPH_L2TP);
692 NG_HOOK_SET_PRIVATE(hook, NULL);
695 /* Go away if no longer connected to anything */
696 if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
697 ng_rmnode_self(node);
701 /*************************************************************************
703 *************************************************************************/
706 * Find the hook with a given session ID.
709 ng_l2tp_find_session(priv_p privp, u_int16_t sid)
711 uint16_t hash = SESSHASH(sid);
712 hookpriv_p hpriv = NULL;
714 LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
715 if (hpriv->conf.session_id == sid)
723 * Reset a hook's session state.
726 ng_l2tp_reset_session(hook_p hook, void *arg)
728 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
731 hpriv->conf.control_dseq = 0;
732 hpriv->conf.enable_dseq = 0;
733 bzero(&hpriv->stats, sizeof(struct ng_l2tp_session_stats));
741 * Handle an incoming frame from below.
744 ng_l2tp_rcvdata_lower(hook_p h, item_p item)
746 static const u_int16_t req_bits[2][2] = {
747 { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
748 { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
750 const node_p node = NG_HOOK_NODE(h);
751 const priv_p priv = NG_NODE_PRIVATE(node);
752 hookpriv_p hpriv = NULL;
763 L2TP_SEQ_CHECK(&priv->seq);
765 /* If not configured, reject */
766 if (!priv->conf.enabled) {
774 /* Remember full packet length; needed for per session accounting. */
775 plen = m->m_pkthdr.len;
778 priv->stats.recvPackets++;
779 priv->stats.recvOctets += plen;
781 /* Get initial header */
782 if (m->m_pkthdr.len < 6) {
783 priv->stats.recvRunts++;
788 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
789 priv->stats.memoryFailures++;
793 hdr = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1];
796 /* Check required header bits and minimum length */
797 is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
798 if ((hdr & req_bits[is_ctrl][0]) != 0
799 || (~hdr & req_bits[is_ctrl][1]) != 0) {
800 priv->stats.recvInvalid++;
805 if (m->m_pkthdr.len < 4 /* tunnel, session id */
806 + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
807 + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
808 + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
809 priv->stats.recvRunts++;
815 /* Get and validate length field if present */
816 if ((hdr & L2TP_HDR_LEN) != 0) {
817 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
818 priv->stats.memoryFailures++;
822 len = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1] - 4;
824 if (len < 0 || len > m->m_pkthdr.len) {
825 priv->stats.recvInvalid++;
830 if (len < m->m_pkthdr.len) /* trim extra bytes */
831 m_adj(m, -(m->m_pkthdr.len - len));
834 /* Get tunnel ID and session ID */
835 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
836 priv->stats.memoryFailures++;
840 tid = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
841 sid = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
844 /* Check tunnel ID */
845 if (tid != priv->conf.tunnel_id &&
846 (priv->conf.match_id || tid != 0)) {
847 priv->stats.recvWrongTunnel++;
850 ERROUT(EADDRNOTAVAIL);
853 /* Check session ID (for data packets only) */
854 if ((hdr & L2TP_HDR_CTRL) == 0) {
855 hpriv = ng_l2tp_find_session(priv, sid);
857 priv->stats.recvUnknownSID++;
865 /* Get Ns, Nr fields if present */
866 if ((hdr & L2TP_HDR_SEQ) != 0) {
867 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
868 priv->stats.memoryFailures++;
872 ns = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
873 nr = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
878 /* Strip offset padding if present */
879 if ((hdr & L2TP_HDR_OFF) != 0) {
882 /* Get length of offset padding */
883 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
884 priv->stats.memoryFailures++;
888 offset = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
890 /* Trim offset padding */
891 if ((2+offset) > m->m_pkthdr.len) {
892 priv->stats.recvInvalid++;
900 /* Handle control packets */
901 if ((hdr & L2TP_HDR_CTRL) != 0) {
902 struct l2tp_seq *const seq = &priv->seq;
904 /* Handle receive ack sequence number Nr */
905 ng_l2tp_seq_recv_nr(priv, nr);
907 /* Discard ZLB packets */
908 if (m->m_pkthdr.len == 0) {
909 priv->stats.recvZLBs++;
917 * If not what we expect or we are busy, drop packet and
918 * send an immediate ZLB ack.
920 if (ns != seq->nr || seq->inproc) {
921 if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
922 priv->stats.recvDuplicates++;
924 priv->stats.recvOutOfOrder++;
925 mtx_unlock(&seq->mtx);
926 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
932 * Until we deliver this packet we can't receive next one as
933 * we have no information for sending ack.
936 mtx_unlock(&seq->mtx);
938 /* Prepend session ID to packet. */
939 M_PREPEND(m, 2, M_DONTWAIT);
942 priv->stats.memoryFailures++;
946 mtod(m, u_int8_t *)[0] = sid >> 8;
947 mtod(m, u_int8_t *)[1] = sid & 0xff;
949 /* Deliver packet to upper layers */
950 NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
953 /* Ready to process next packet. */
956 /* If packet was successfully delivered send ack. */
958 /* Update recv sequence number */
960 /* Start receive ack timer, if not already running */
961 if (!callout_active(&seq->xack_timer)) {
962 ng_callout(&seq->xack_timer, priv->node, NULL,
963 L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
967 mtx_unlock(&seq->mtx);
972 /* Per session packet, account it. */
973 hpriv->stats.recvPackets++;
974 hpriv->stats.recvOctets += plen;
976 /* Follow peer's lead in data sequencing, if configured to do so */
977 if (!hpriv->conf.control_dseq)
978 hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
980 /* Handle data sequence numbers if present and enabled */
981 if ((hdr & L2TP_HDR_SEQ) != 0) {
982 if (hpriv->conf.enable_dseq
983 && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
984 NG_FREE_ITEM(item); /* duplicate or out of order */
986 priv->stats.recvDataDrops++;
992 /* Drop empty data packets */
993 if (m->m_pkthdr.len == 0) {
1000 NG_FWD_NEW_DATA(error, item, hook, m);
1003 L2TP_SEQ_CHECK(&priv->seq);
1008 * Handle an outgoing control frame.
1011 ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
1013 const node_p node = NG_HOOK_NODE(hook);
1014 const priv_p priv = NG_NODE_PRIVATE(node);
1015 struct l2tp_seq *const seq = &priv->seq;
1022 L2TP_SEQ_CHECK(&priv->seq);
1024 /* If not configured, reject */
1025 if (!priv->conf.enabled) {
1030 /* Grab mbuf and discard other stuff XXX */
1034 /* Packet should have session ID prepended */
1035 if (m->m_pkthdr.len < 2) {
1036 priv->stats.xmitInvalid++;
1041 /* Check max length */
1042 if (m->m_pkthdr.len >= 0x10000 - 14) {
1043 priv->stats.xmitTooBig++;
1048 mtx_lock(&seq->mtx);
1050 /* Find next empty slot in transmit queue */
1051 for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
1052 if (i == L2TP_MAX_XWIN) {
1053 mtx_unlock(&seq->mtx);
1054 priv->stats.xmitDrops++;
1060 /* If peer's receive window is already full, nothing else to do */
1061 if (i >= seq->cwnd) {
1062 mtx_unlock(&seq->mtx);
1066 /* Start retransmit timer if not already running */
1067 if (!callout_active(&seq->rack_timer))
1068 ng_callout(&seq->rack_timer, node, NULL,
1069 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1073 mtx_unlock(&seq->mtx);
1076 if ((m = L2TP_COPY_MBUF(m, M_DONTWAIT)) == NULL) {
1077 priv->stats.memoryFailures++;
1081 /* Send packet and increment xmit sequence number */
1082 error = ng_l2tp_xmit_ctrl(priv, m, ns);
1085 L2TP_SEQ_CHECK(&priv->seq);
1090 * Handle an outgoing data frame.
1093 ng_l2tp_rcvdata(hook_p hook, item_p item)
1095 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1096 const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
1104 L2TP_SEQ_CHECK(&priv->seq);
1106 /* If not configured, reject */
1107 if (!priv->conf.enabled) {
1115 /* Check max length */
1116 if (m->m_pkthdr.len >= 0x10000 - 12) {
1117 priv->stats.xmitDataTooBig++;
1123 /* Prepend L2TP header */
1125 + (2 * (hpriv->conf.include_length != 0))
1126 + (4 * (hpriv->conf.enable_dseq != 0)),
1129 priv->stats.memoryFailures++;
1133 p = mtod(m, uint8_t *);
1134 hdr = L2TP_DATA_HDR;
1135 if (hpriv->conf.include_length) {
1136 hdr |= L2TP_HDR_LEN;
1137 p[i++] = m->m_pkthdr.len >> 8;
1138 p[i++] = m->m_pkthdr.len & 0xff;
1140 p[i++] = priv->conf.peer_id >> 8;
1141 p[i++] = priv->conf.peer_id & 0xff;
1142 p[i++] = hpriv->conf.peer_id >> 8;
1143 p[i++] = hpriv->conf.peer_id & 0xff;
1144 if (hpriv->conf.enable_dseq) {
1145 hdr |= L2TP_HDR_SEQ;
1146 p[i++] = hpriv->ns >> 8;
1147 p[i++] = hpriv->ns & 0xff;
1148 p[i++] = hpriv->nr >> 8;
1149 p[i++] = hpriv->nr & 0xff;
1155 /* Update per session stats. */
1156 hpriv->stats.xmitPackets++;
1157 hpriv->stats.xmitOctets += m->m_pkthdr.len;
1159 /* And the global one. */
1160 priv->stats.xmitPackets++;
1161 priv->stats.xmitOctets += m->m_pkthdr.len;
1164 NG_FWD_NEW_DATA(error, item, priv->lower, m);
1167 L2TP_SEQ_CHECK(&priv->seq);
1172 * Send a message to our controlling node that we've failed.
1175 ng_l2tp_seq_failure(priv_p priv)
1177 struct ng_mesg *msg;
1180 NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1183 NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
1186 /************************************************************************
1187 SEQUENCE NUMBER HANDLING
1188 ************************************************************************/
1191 * Initialize sequence number state.
1194 ng_l2tp_seq_init(priv_p priv)
1196 struct l2tp_seq *const seq = &priv->seq;
1198 KASSERT(priv->conf.peer_win >= 1,
1199 ("%s: peer_win is zero", __func__));
1200 memset(seq, 0, sizeof(*seq));
1202 seq->wmax = priv->conf.peer_win;
1203 if (seq->wmax > L2TP_MAX_XWIN)
1204 seq->wmax = L2TP_MAX_XWIN;
1205 seq->ssth = seq->wmax;
1206 ng_callout_init(&seq->rack_timer);
1207 ng_callout_init(&seq->xack_timer);
1208 mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
1209 L2TP_SEQ_CHECK(seq);
1213 * Set sequence number state as given from user.
1216 ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
1218 struct l2tp_seq *const seq = &priv->seq;
1220 /* If node is enabled, deny update to sequence numbers. */
1221 if (priv->conf.enabled)
1224 /* We only can handle the simple cases. */
1225 if (conf->xack != conf->nr || conf->ns != conf->rack)
1228 /* Set ns,nr,rack,xack parameters. */
1231 seq->rack = conf->rack;
1232 seq->xack = conf->xack;
1238 * Adjust sequence number state accordingly after reconfiguration.
1241 ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1243 struct l2tp_seq *const seq = &priv->seq;
1246 /* If disabling node, reset state sequence number */
1247 if (!conf->enabled) {
1248 ng_l2tp_seq_reset(priv);
1252 /* Adjust peer's max recv window; it can only increase */
1253 new_wmax = conf->peer_win;
1254 if (new_wmax > L2TP_MAX_XWIN)
1255 new_wmax = L2TP_MAX_XWIN;
1258 if (new_wmax < seq->wmax)
1260 seq->wmax = new_wmax;
1267 * Reset sequence number state.
1270 ng_l2tp_seq_reset(priv_p priv)
1272 struct l2tp_seq *const seq = &priv->seq;
1277 L2TP_SEQ_CHECK(seq);
1280 ng_uncallout(&seq->rack_timer, priv->node);
1281 ng_uncallout(&seq->xack_timer, priv->node);
1283 /* Free retransmit queue */
1284 for (i = 0; i < L2TP_MAX_XWIN; i++) {
1285 if (seq->xwin[i] == NULL)
1287 m_freem(seq->xwin[i]);
1290 /* Reset session hooks' sequence number states */
1291 NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL, hook);
1293 /* Reset node's sequence number state */
1298 seq->wmax = L2TP_MAX_XWIN;
1300 seq->ssth = seq->wmax;
1303 bzero(seq->xwin, sizeof(seq->xwin));
1306 L2TP_SEQ_CHECK(seq);
1310 * Handle receipt of an acknowledgement value (Nr) from peer.
1313 ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1315 struct l2tp_seq *const seq = &priv->seq;
1316 struct mbuf *xwin[L2TP_MAX_XWIN]; /* partial local copy */
1321 mtx_lock(&seq->mtx);
1323 /* Verify peer's ACK is in range */
1324 if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0) {
1325 mtx_unlock(&seq->mtx);
1326 return; /* duplicate ack */
1328 if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1329 mtx_unlock(&seq->mtx);
1330 priv->stats.recvBadAcks++; /* ack for packet not sent */
1333 KASSERT(nack <= L2TP_MAX_XWIN,
1334 ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1336 /* Update receive ack stats */
1340 /* Free acknowledged packets and shift up packets in the xmit queue */
1341 for (i = 0; i < nack; i++)
1342 m_freem(seq->xwin[i]);
1343 memmove(seq->xwin, seq->xwin + nack,
1344 (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1345 memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1346 nack * sizeof(*seq->xwin));
1349 * Do slow-start/congestion avoidance windowing algorithm described
1350 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1351 * ACK had arrived separately.
1353 if (seq->cwnd < seq->wmax) {
1355 /* Handle slow start phase */
1356 if (seq->cwnd < seq->ssth) {
1359 if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
1360 nack = seq->cwnd - seq->ssth;
1361 seq->cwnd = seq->ssth;
1365 /* Handle congestion avoidance phase */
1366 if (seq->cwnd >= seq->ssth) {
1368 while (seq->acks >= seq->cwnd) {
1369 seq->acks -= seq->cwnd;
1370 if (seq->cwnd < seq->wmax)
1376 /* Stop xmit timer */
1377 if (callout_active(&seq->rack_timer))
1378 ng_uncallout(&seq->rack_timer, priv->node);
1380 /* If transmit queue is empty, we're done for now */
1381 if (seq->xwin[0] == NULL) {
1382 mtx_unlock(&seq->mtx);
1386 /* Start restransmit timer again */
1387 ng_callout(&seq->rack_timer, priv->node, NULL,
1388 hz, ng_l2tp_seq_rack_timeout, NULL, 0);
1391 * Send more packets, trying to keep peer's receive window full.
1392 * Make copy of everything we need before lock release.
1396 while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1397 && seq->xwin[i] != NULL) {
1398 xwin[j++] = seq->xwin[i];
1402 mtx_unlock(&seq->mtx);
1406 * If there is a memory error, pretend packet was sent, as it
1407 * will get retransmitted later anyway.
1409 for (i = 0; i < j; i++) {
1411 if ((m = L2TP_COPY_MBUF(xwin[i], M_DONTWAIT)) == NULL)
1412 priv->stats.memoryFailures++;
1414 ng_l2tp_xmit_ctrl(priv, m, ns);
1420 * Handle an ack timeout. We have an outstanding ack that we
1421 * were hoping to piggy-back, but haven't, so send a ZLB.
1424 ng_l2tp_seq_xack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1426 const priv_p priv = NG_NODE_PRIVATE(node);
1427 struct l2tp_seq *const seq = &priv->seq;
1429 /* Make sure callout is still active before doing anything */
1430 if (callout_pending(&seq->xack_timer) ||
1431 (!callout_active(&seq->xack_timer)))
1435 L2TP_SEQ_CHECK(seq);
1438 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1440 /* callout_deactivate() is not needed here
1441 as ng_uncallout() was called by ng_l2tp_xmit_ctrl() */
1444 L2TP_SEQ_CHECK(seq);
1448 * Handle a transmit timeout. The peer has failed to respond
1449 * with an ack for our packet, so retransmit it.
1452 ng_l2tp_seq_rack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1454 const priv_p priv = NG_NODE_PRIVATE(node);
1455 struct l2tp_seq *const seq = &priv->seq;
1459 /* Make sure callout is still active before doing anything */
1460 if (callout_pending(&seq->rack_timer) ||
1461 (!callout_active(&seq->rack_timer)))
1465 L2TP_SEQ_CHECK(seq);
1467 priv->stats.xmitRetransmits++;
1469 /* Have we reached the retransmit limit? If so, notify owner. */
1470 if (seq->rexmits++ >= priv->conf.rexmit_max)
1471 ng_l2tp_seq_failure(priv);
1473 /* Restart timer, this time with an increased delay */
1474 delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
1475 if (delay > priv->conf.rexmit_max_to)
1476 delay = priv->conf.rexmit_max_to;
1477 ng_callout(&seq->rack_timer, node, NULL,
1478 hz * delay, ng_l2tp_seq_rack_timeout, NULL, 0);
1480 /* Do slow-start/congestion algorithm windowing algorithm */
1481 seq->ns = seq->rack;
1482 seq->ssth = (seq->cwnd + 1) / 2;
1486 /* Retransmit oldest unack'd packet */
1487 if ((m = L2TP_COPY_MBUF(seq->xwin[0], M_DONTWAIT)) == NULL)
1488 priv->stats.memoryFailures++;
1490 ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
1492 /* callout_deactivate() is not needed here
1493 as ng_callout() is getting called each time */
1496 L2TP_SEQ_CHECK(seq);
1500 * Transmit a control stream packet, payload optional.
1501 * The transmit sequence number is not incremented.
1504 ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1506 struct l2tp_seq *const seq = &priv->seq;
1508 u_int16_t session_id = 0;
1511 mtx_lock(&seq->mtx);
1513 /* Stop ack timer: we're sending an ack with this packet.
1514 Doing this before to keep state predictable after error. */
1515 if (callout_active(&seq->xack_timer))
1516 ng_uncallout(&seq->xack_timer, priv->node);
1518 seq->xack = seq->nr;
1520 mtx_unlock(&seq->mtx);
1522 /* If no mbuf passed, send an empty packet (ZLB) */
1525 /* Create a new mbuf for ZLB packet */
1526 MGETHDR(m, M_DONTWAIT, MT_DATA);
1528 priv->stats.memoryFailures++;
1531 m->m_len = m->m_pkthdr.len = 12;
1532 m->m_pkthdr.rcvif = NULL;
1533 priv->stats.xmitZLBs++;
1536 /* Strip off session ID */
1537 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1538 priv->stats.memoryFailures++;
1541 session_id = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
1543 /* Make room for L2TP header */
1544 M_PREPEND(m, 10, M_DONTWAIT); /* - 2 + 12 = 10 */
1546 priv->stats.memoryFailures++;
1551 /* Fill in L2TP header */
1552 p = mtod(m, u_int8_t *);
1553 p[0] = L2TP_CTRL_HDR >> 8;
1554 p[1] = L2TP_CTRL_HDR & 0xff;
1555 p[2] = m->m_pkthdr.len >> 8;
1556 p[3] = m->m_pkthdr.len & 0xff;
1557 p[4] = priv->conf.peer_id >> 8;
1558 p[5] = priv->conf.peer_id & 0xff;
1559 p[6] = session_id >> 8;
1560 p[7] = session_id & 0xff;
1563 p[10] = seq->nr >> 8;
1564 p[11] = seq->nr & 0xff;
1566 /* Update sequence number info and stats */
1567 priv->stats.xmitPackets++;
1568 priv->stats.xmitOctets += m->m_pkthdr.len;
1571 NG_SEND_DATA_ONLY(error, priv->lower, m);
1577 * Sanity check sequence number state.
1580 ng_l2tp_seq_check(struct l2tp_seq *seq)
1582 int self_unack, peer_unack;
1585 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1587 mtx_lock(&seq->mtx);
1589 self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1590 peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1591 CHECK(seq->wmax <= L2TP_MAX_XWIN);
1592 CHECK(seq->cwnd >= 1);
1593 CHECK(seq->cwnd <= seq->wmax);
1594 CHECK(seq->ssth >= 1);
1595 CHECK(seq->ssth <= seq->wmax);
1596 if (seq->cwnd < seq->ssth)
1597 CHECK(seq->acks == 0);
1599 CHECK(seq->acks <= seq->cwnd);
1600 CHECK(self_unack >= 0);
1601 CHECK(peer_unack >= 0);
1602 CHECK(peer_unack <= seq->wmax);
1603 CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
1604 CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
1605 for (i = 0; i < peer_unack; i++)
1606 CHECK(seq->xwin[i] != NULL);
1607 for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
1608 CHECK(seq->xwin[i] == NULL);
1610 mtx_unlock(&seq->mtx);
1614 #endif /* INVARIANTS */