3 * Copyright (c) 2001-2002 Packet Design, LLC.
6 * Subject to the following obligations and disclaimer of warranty,
7 * use and redistribution of this software, in source or object code
8 * forms, with or without modifications are expressly permitted by
9 * Packet Design; provided, however, that:
11 * (i) Any and all reproductions of the source or object code
12 * must include the copyright notice above and the following
13 * disclaimer of warranties; and
14 * (ii) No rights are granted, in any manner or form, to use
15 * Packet Design trademarks, including the mark "PACKET DESIGN"
16 * on advertising, endorsements, or otherwise except as such
17 * appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
22 * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
23 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
24 * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
25 * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
26 * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
27 * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
28 * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
29 * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
31 * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
32 * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35 * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
36 * THE POSSIBILITY OF SUCH DAMAGE.
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_l2tp.c,v 1.1.2.1 2002/08/20 23:48:15 archie Exp $
41 * $DragonFly: src/sys/netgraph/l2tp/ng_l2tp.c,v 1.8 2006/01/14 11:10:47 swildner Exp $
45 * L2TP netgraph node type.
47 * This node type implements the lower layer of the
48 * L2TP protocol as specified in RFC 2661.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/errno.h>
59 #include <sys/libkern.h>
60 #include <sys/thread2.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_parse.h>
67 #ifdef NG_SEPARATE_MALLOC
68 MALLOC_DEFINE(M_NETGRAPH_L2TP
, "netgraph_l2tp", "netgraph l2tp node");
70 #define M_NETGRAPH_L2TP M_NETGRAPH
73 /* L2TP header format (first 2 bytes only) */
74 #define L2TP_HDR_CTRL 0x8000 /* control packet */
75 #define L2TP_HDR_LEN 0x4000 /* has length field */
76 #define L2TP_HDR_SEQ 0x0800 /* has ns, nr fields */
77 #define L2TP_HDR_OFF 0x0200 /* has offset field */
78 #define L2TP_HDR_PRIO 0x0100 /* give priority */
79 #define L2TP_HDR_VERS_MASK 0x000f /* version field mask */
80 #define L2TP_HDR_VERSION 0x0002 /* version field */
82 /* Bits that must be zero or one in first two bytes of header */
83 #define L2TP_CTRL_0BITS 0x030d /* ctrl: must be 0 */
84 #define L2TP_CTRL_1BITS 0xc802 /* ctrl: must be 1 */
85 #define L2TP_DATA_0BITS 0x800d /* data: must be 0 */
86 #define L2TP_DATA_1BITS 0x0002 /* data: must be 1 */
88 /* Standard xmit ctrl and data header bits */
89 #define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
90 | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
91 #define L2TP_DATA_HDR (L2TP_HDR_VERSION) /* optional: len, seq */
93 /* Some hard coded values */
94 #define L2TP_MAX_XWIN 16 /* my max xmit window */
95 #define L2TP_MAX_REXMIT 5 /* default max rexmit */
96 #define L2TP_MAX_REXMIT_TO 30 /* default rexmit to */
97 #define L2TP_DELAYED_ACK ((hz + 19) / 20) /* delayed ack: 50 ms */
99 /* Default data sequence number configuration for new sessions */
100 #define L2TP_CONTROL_DSEQ 1 /* we are the lns */
101 #define L2TP_ENABLE_DSEQ 1 /* enable data seq # */
103 /* Compare sequence numbers using circular math */
104 #define L2TP_SEQ_DIFF(x, y) ((int)((int16_t)(x) - (int16_t)(y)))
107 * Sequence number state
110 * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
111 * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
112 * - The first (ns - rack) mbuf's in xwin[] array are copies of these
113 * unacknowledged packets; the remainder of xwin[] consists first of
114 * zero or more further untransmitted packets in the transmit queue
115 * - We try to keep the peer's receive window as full as possible.
116 * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
117 * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
118 * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
119 * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
122 u_int16_t ns
; /* next xmit seq we send */
123 u_int16_t nr
; /* next recv seq we expect */
124 u_int16_t rack
; /* last 'nr' we rec'd */
125 u_int16_t xack
; /* last 'nr' we sent */
126 u_int16_t wmax
; /* peer's max recv window */
127 u_int16_t cwnd
; /* current congestion window */
128 u_int16_t ssth
; /* slow start threshold */
129 u_int16_t acks
; /* # consecutive acks rec'd */
130 u_int16_t rexmits
; /* # retransmits sent */
131 u_int16_t max_rexmits
; /* max # retransmits sent */
132 u_int16_t max_rexmit_to
; /* max retransmit timeout */
133 struct callout rack_timer
; /* retransmit timer */
134 struct callout xack_timer
; /* delayed ack timer */
135 u_char rack_timer_running
; /* xmit timer running */
136 u_char xack_timer_running
; /* ack timer running */
137 struct mbuf
*xwin
[L2TP_MAX_XWIN
]; /* transmit window */
140 /* Node private data */
141 struct ng_l2tp_private
{
142 node_p node
; /* back pointer to node */
143 hook_p ctrl
; /* hook to upper layers */
144 hook_p lower
; /* hook to lower layers */
145 struct ng_l2tp_config conf
; /* node configuration */
146 struct ng_l2tp_stats stats
; /* node statistics */
147 struct l2tp_seq seq
; /* ctrl sequence number state */
148 char *ftarget
; /* failure message target */
150 typedef struct ng_l2tp_private
*priv_p
;
152 /* Hook private data (data session hooks only) */
153 struct ng_l2tp_hook_private
{
154 struct ng_l2tp_sess_config conf
; /* hook/session config */
155 u_int16_t ns
; /* data ns sequence number */
156 u_int16_t nr
; /* data nr sequence number */
158 typedef struct ng_l2tp_hook_private
*hookpriv_p
;
160 /* Netgraph node methods */
161 static ng_constructor_t ng_l2tp_constructor
;
162 static ng_rcvmsg_t ng_l2tp_rcvmsg
;
163 static ng_shutdown_t ng_l2tp_shutdown
;
164 static ng_newhook_t ng_l2tp_newhook
;
165 static ng_rcvdata_t ng_l2tp_rcvdata
;
166 static ng_disconnect_t ng_l2tp_disconnect
;
168 /* Internal functions */
169 static int ng_l2tp_recv_lower(node_p node
, struct mbuf
*m
, meta_p meta
);
170 static int ng_l2tp_recv_ctrl(node_p node
, struct mbuf
*m
, meta_p meta
);
171 static int ng_l2tp_recv_data(node_p node
,
172 struct mbuf
*m
, meta_p meta
, hookpriv_p hpriv
);
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_adjust(priv_p priv
,
178 const struct ng_l2tp_config
*conf
);
179 static void ng_l2tp_seq_reset(priv_p priv
);
180 static void ng_l2tp_seq_failure(priv_p priv
);
181 static void ng_l2tp_seq_recv_nr(priv_p priv
, u_int16_t nr
);
182 static int ng_l2tp_seq_recv_ns(priv_p priv
, u_int16_t ns
);
183 static void ng_l2tp_seq_xack_timeout(void *arg
);
184 static void ng_l2tp_seq_rack_timeout(void *arg
);
187 static void ng_l2tp_seq_check(struct l2tp_seq
*seq
);
190 /* Parse type for struct ng_l2tp_config */
191 static const struct ng_parse_struct_field
192 ng_l2tp_config_type_fields
[] = NG_L2TP_CONFIG_TYPE_INFO
;
193 static const struct ng_parse_type ng_l2tp_config_type
= {
194 &ng_parse_struct_type
,
195 &ng_l2tp_config_type_fields
,
198 /* Parse type for struct ng_l2tp_sess_config */
199 static const struct ng_parse_struct_field
200 ng_l2tp_sess_config_type_fields
[] = NG_L2TP_SESS_CONFIG_TYPE_INFO
;
201 static const struct ng_parse_type ng_l2tp_sess_config_type
= {
202 &ng_parse_struct_type
,
203 &ng_l2tp_sess_config_type_fields
,
206 /* Parse type for struct ng_l2tp_stats */
207 static const struct ng_parse_struct_field
208 ng_l2tp_stats_type_fields
[] = NG_L2TP_STATS_TYPE_INFO
;
209 static const struct ng_parse_type ng_pptp_stats_type
= {
210 &ng_parse_struct_type
,
211 &ng_l2tp_stats_type_fields
214 /* List of commands and how to convert arguments to/from ASCII */
215 static const struct ng_cmdlist ng_l2tp_cmdlist
[] = {
220 &ng_l2tp_config_type
,
232 NGM_L2TP_SET_SESS_CONFIG
,
234 &ng_l2tp_sess_config_type
,
239 NGM_L2TP_GET_SESS_CONFIG
,
241 &ng_parse_hint16_type
,
242 &ng_l2tp_sess_config_type
260 NGM_L2TP_GETCLR_STATS
,
267 NGM_L2TP_ACK_FAILURE
,
275 /* Node type descriptor */
276 static struct ng_type ng_l2tp_typestruct
= {
291 NETGRAPH_INIT(l2tp
, &ng_l2tp_typestruct
);
293 /* Sequence number state sanity checking */
295 #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
297 #define L2TP_SEQ_CHECK(x) do { } while (0)
301 #define memmove(d, s, l) ovbcopy(s, d, l)
303 /* Whether to use m_copypacket() or m_dup() */
304 #define L2TP_COPY_MBUF m_copypacket
306 /************************************************************************
308 ************************************************************************/
311 * Node type constructor
314 ng_l2tp_constructor(node_p
*nodep
)
319 /* Allocate private structure */
320 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH_L2TP
, M_NOWAIT
| M_ZERO
);
324 /* Apply a semi-reasonable default configuration */
325 priv
->conf
.peer_win
= 1;
326 priv
->conf
.rexmit_max
= L2TP_MAX_REXMIT
;
327 priv
->conf
.rexmit_max_to
= L2TP_MAX_REXMIT_TO
;
329 /* Initialize sequence number state */
330 ng_l2tp_seq_init(priv
);
332 /* Call generic node constructor */
333 if ((error
= ng_make_node_common(&ng_l2tp_typestruct
, nodep
))) {
334 FREE(priv
, M_NETGRAPH_L2TP
);
337 NG_NODE_SET_PRIVATE(*nodep
, priv
);
345 * Give our OK for a hook to be added.
348 ng_l2tp_newhook(node_p node
, hook_p hook
, const char *name
)
350 const priv_p priv
= NG_NODE_PRIVATE(node
);
352 /* Check hook name */
353 if (strcmp(name
, NG_L2TP_HOOK_CTRL
) == 0) {
354 if (priv
->ctrl
!= NULL
)
357 } else if (strcmp(name
, NG_L2TP_HOOK_LOWER
) == 0) {
358 if (priv
->lower
!= NULL
)
362 static const char hexdig
[16] = "0123456789abcdef";
363 u_int16_t session_id
;
369 /* Parse hook name to get session ID */
370 if (strncmp(name
, NG_L2TP_HOOK_SESSION_P
,
371 sizeof(NG_L2TP_HOOK_SESSION_P
) - 1) != 0)
373 hex
= name
+ sizeof(NG_L2TP_HOOK_SESSION_P
) - 1;
374 for (session_id
= i
= 0; i
< 4; i
++) {
375 for (j
= 0; j
< 16 && hex
[i
] != hexdig
[j
]; j
++);
378 session_id
= (session_id
<< 4) | j
;
383 /* Create hook private structure */
384 MALLOC(hpriv
, hookpriv_p
,
385 sizeof(*hpriv
), M_NETGRAPH_L2TP
, M_NOWAIT
| M_ZERO
);
388 hpriv
->conf
.session_id
= htons(session_id
);
389 hpriv
->conf
.control_dseq
= L2TP_CONTROL_DSEQ
;
390 hpriv
->conf
.enable_dseq
= L2TP_ENABLE_DSEQ
;
391 NG_HOOK_SET_PRIVATE(hook
, hpriv
);
399 * Receive a control message.
402 ng_l2tp_rcvmsg(node_p node
, struct ng_mesg
*msg
,
403 const char *raddr
, struct ng_mesg
**rptr
)
405 const priv_p priv
= NG_NODE_PRIVATE(node
);
406 struct ng_mesg
*resp
= NULL
;
409 switch (msg
->header
.typecookie
) {
410 case NGM_L2TP_COOKIE
:
411 switch (msg
->header
.cmd
) {
412 case NGM_L2TP_SET_CONFIG
:
414 struct ng_l2tp_config
*const conf
=
415 (struct ng_l2tp_config
*)msg
->data
;
417 /* Check for invalid or illegal config */
418 if (msg
->header
.arglen
!= sizeof(*conf
)) {
422 conf
->enabled
= !!conf
->enabled
;
423 conf
->match_id
= !!conf
->match_id
;
424 conf
->tunnel_id
= htons(conf
->tunnel_id
);
425 conf
->peer_id
= htons(conf
->peer_id
);
426 if (priv
->conf
.enabled
427 && ((priv
->conf
.tunnel_id
!= 0
428 && conf
->tunnel_id
!= priv
->conf
.tunnel_id
)
429 || ((priv
->conf
.peer_id
!= 0
430 && conf
->peer_id
!= priv
->conf
.peer_id
)))) {
435 /* Save calling node as failure target */
436 if (priv
->ftarget
!= NULL
)
437 FREE(priv
->ftarget
, M_NETGRAPH_L2TP
);
438 MALLOC(priv
->ftarget
, char *,
439 strlen(raddr
) + 1, M_NETGRAPH_L2TP
, M_NOWAIT
);
440 if (priv
->ftarget
== NULL
) {
444 strcpy(priv
->ftarget
, raddr
);
446 /* Adjust sequence number state */
447 if ((error
= ng_l2tp_seq_adjust(priv
, conf
)) != 0)
450 /* Update node's config */
454 case NGM_L2TP_GET_CONFIG
:
456 struct ng_l2tp_config
*conf
;
458 NG_MKRESPONSE(resp
, msg
, sizeof(*conf
), M_NOWAIT
);
463 conf
= (struct ng_l2tp_config
*)resp
->data
;
466 /* Put ID's in host order */
467 conf
->tunnel_id
= ntohs(conf
->tunnel_id
);
468 conf
->peer_id
= ntohs(conf
->peer_id
);
471 case NGM_L2TP_SET_SESS_CONFIG
:
473 struct ng_l2tp_sess_config
*const conf
=
474 (struct ng_l2tp_sess_config
*)msg
->data
;
475 hookpriv_p hpriv
= NULL
;
478 /* Check for invalid or illegal config */
479 if (msg
->header
.arglen
!= sizeof(*conf
)) {
484 /* Put ID's in network order */
485 conf
->session_id
= htons(conf
->session_id
);
486 conf
->peer_id
= htons(conf
->peer_id
);
488 /* Find matching hook */
489 LIST_FOREACH(hook
, &node
->hooks
, hooks
) {
490 if ((hpriv
= NG_HOOK_PRIVATE(hook
)) == NULL
)
492 if (hpriv
->conf
.session_id
== conf
->session_id
)
500 /* Update hook's config */
504 case NGM_L2TP_GET_SESS_CONFIG
:
506 struct ng_l2tp_sess_config
*conf
;
507 hookpriv_p hpriv
= NULL
;
508 u_int16_t session_id
;
512 if (msg
->header
.arglen
!= sizeof(session_id
)) {
516 memcpy(&session_id
, msg
->data
, 2);
517 session_id
= htons(session_id
);
519 /* Find matching hook */
520 LIST_FOREACH(hook
, &node
->hooks
, hooks
) {
521 if ((hpriv
= NG_HOOK_PRIVATE(hook
)) == NULL
)
523 if (hpriv
->conf
.session_id
== session_id
)
532 NG_MKRESPONSE(resp
, msg
, sizeof(hpriv
->conf
), M_NOWAIT
);
537 conf
= (struct ng_l2tp_sess_config
*)resp
->data
;
540 /* Put ID's in host order */
541 conf
->session_id
= ntohs(conf
->session_id
);
542 conf
->peer_id
= ntohs(conf
->peer_id
);
545 case NGM_L2TP_GET_STATS
:
546 case NGM_L2TP_CLR_STATS
:
547 case NGM_L2TP_GETCLR_STATS
:
549 if (msg
->header
.cmd
!= NGM_L2TP_CLR_STATS
) {
550 NG_MKRESPONSE(resp
, msg
,
551 sizeof(priv
->stats
), M_NOWAIT
);
557 &priv
->stats
, sizeof(priv
->stats
));
559 if (msg
->header
.cmd
!= NGM_L2TP_GET_STATS
)
560 memset(&priv
->stats
, 0, sizeof(priv
->stats
));
575 FREE(resp
, M_NETGRAPH
);
576 FREE(msg
, M_NETGRAPH
);
581 * Receive incoming data on a hook.
584 ng_l2tp_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
586 const node_p node
= NG_HOOK_NODE(hook
);
587 const priv_p priv
= NG_NODE_PRIVATE(node
);
591 L2TP_SEQ_CHECK(&priv
->seq
);
593 /* If not configured, reject */
594 if (!priv
->conf
.enabled
) {
595 NG_FREE_DATA(m
, meta
);
599 /* Handle incoming frame from below */
600 if (hook
== priv
->lower
) {
601 error
= ng_l2tp_recv_lower(node
, m
, meta
);
605 /* Handle outgoing control frame */
606 if (hook
== priv
->ctrl
) {
607 error
= ng_l2tp_recv_ctrl(node
, m
, meta
);
611 /* Handle outgoing data frame */
612 error
= ng_l2tp_recv_data(node
, m
, meta
, NG_HOOK_PRIVATE(hook
));
616 L2TP_SEQ_CHECK(&priv
->seq
);
624 ng_l2tp_shutdown(node_p node
)
626 const priv_p priv
= NG_NODE_PRIVATE(node
);
627 struct l2tp_seq
*const seq
= &priv
->seq
;
632 /* Reset sequence number state */
633 node
->flags
|= NG_INVALID
;
634 ng_l2tp_seq_reset(priv
);
638 /* Free private data if neither timer is running */
639 if (!seq
->rack_timer_running
&& !seq
->xack_timer_running
) {
640 if (priv
->ftarget
!= NULL
)
641 FREE(priv
->ftarget
, M_NETGRAPH_L2TP
);
642 FREE(priv
, M_NETGRAPH_L2TP
);
643 NG_NODE_SET_PRIVATE(node
, NULL
);
655 ng_l2tp_disconnect(hook_p hook
)
657 const node_p node
= NG_HOOK_NODE(hook
);
658 const priv_p priv
= NG_NODE_PRIVATE(node
);
660 /* Zero out hook pointer */
661 if (hook
== priv
->ctrl
)
663 else if (hook
== priv
->lower
)
666 FREE(NG_HOOK_PRIVATE(hook
), M_NETGRAPH_L2TP
);
667 NG_HOOK_SET_PRIVATE(hook
, NULL
);
670 /* Go away if no longer connected to anything */
671 if (node
->numhooks
== 0)
676 /*************************************************************************
678 *************************************************************************/
681 * Handle an incoming frame from below.
684 ng_l2tp_recv_lower(node_p node
, struct mbuf
*m
, meta_p meta
)
686 static const u_int16_t req_bits
[2][2] = {
687 { L2TP_DATA_0BITS
, L2TP_DATA_1BITS
},
688 { L2TP_CTRL_0BITS
, L2TP_CTRL_1BITS
},
690 const priv_p priv
= NG_NODE_PRIVATE(node
);
691 hookpriv_p hpriv
= NULL
;
702 priv
->stats
.recvPackets
++;
703 priv
->stats
.recvOctets
+= m
->m_pkthdr
.len
;
705 /* Get initial header */
706 if (m
->m_pkthdr
.len
< 6) {
707 priv
->stats
.recvRunts
++;
708 NG_FREE_DATA(m
, meta
);
711 if (m
->m_len
< 2 && (m
= m_pullup(m
, 2)) == NULL
) {
712 priv
->stats
.memoryFailures
++;
716 hdr
= ntohs(*mtod(m
, u_int16_t
*));
719 /* Check required header bits and minimum length */
720 is_ctrl
= (hdr
& L2TP_HDR_CTRL
) != 0;
721 if ((hdr
& req_bits
[is_ctrl
][0]) != 0
722 || (~hdr
& req_bits
[is_ctrl
][1]) != 0) {
723 priv
->stats
.recvInvalid
++;
724 NG_FREE_DATA(m
, meta
);
727 if (m
->m_pkthdr
.len
< 4 /* tunnel, session id */
728 + (2 * ((hdr
& L2TP_HDR_LEN
) != 0)) /* length field */
729 + (4 * ((hdr
& L2TP_HDR_SEQ
) != 0)) /* seq # fields */
730 + (2 * ((hdr
& L2TP_HDR_OFF
) != 0))) { /* offset field */
731 priv
->stats
.recvRunts
++;
732 NG_FREE_DATA(m
, meta
);
736 /* Get and validate length field if present */
737 if ((hdr
& L2TP_HDR_LEN
) != 0) {
738 if (m
->m_len
< 2 && (m
= m_pullup(m
, 2)) == NULL
) {
739 priv
->stats
.memoryFailures
++;
743 len
= (u_int16_t
)ntohs(*mtod(m
, u_int16_t
*)) - 4;
745 if (len
< 0 || len
> m
->m_pkthdr
.len
) {
746 priv
->stats
.recvInvalid
++;
747 NG_FREE_DATA(m
, meta
);
750 if (len
< m
->m_pkthdr
.len
) /* trim extra bytes */
751 m_adj(m
, -(m
->m_pkthdr
.len
- len
));
754 /* Get tunnel ID and session ID */
755 if (m
->m_len
< 4 && (m
= m_pullup(m
, 4)) == NULL
) {
756 priv
->stats
.memoryFailures
++;
760 memcpy(ids
, mtod(m
, u_int16_t
*), 4);
763 /* Check tunnel ID */
764 if (ids
[0] != priv
->conf
.tunnel_id
765 && (priv
->conf
.match_id
|| ids
[0] != 0)) {
766 priv
->stats
.recvWrongTunnel
++;
767 NG_FREE_DATA(m
, meta
);
768 return (EADDRNOTAVAIL
);
771 /* Check session ID (for data packets only) */
772 if ((hdr
& L2TP_HDR_CTRL
) == 0) {
773 LIST_FOREACH(hook
, &node
->hooks
, hooks
) {
774 if ((hpriv
= NG_HOOK_PRIVATE(hook
)) == NULL
)
776 if (hpriv
->conf
.session_id
== ids
[1])
780 priv
->stats
.recvUnknownSID
++;
781 NG_FREE_DATA(m
, meta
);
784 hpriv
= NG_HOOK_PRIVATE(hook
);
787 /* Get Ns, Nr fields if present */
788 if ((hdr
& L2TP_HDR_SEQ
) != 0) {
789 if (m
->m_len
< 4 && (m
= m_pullup(m
, 4)) == NULL
) {
790 priv
->stats
.memoryFailures
++;
794 memcpy(&ns
, &mtod(m
, u_int16_t
*)[0], 2);
796 memcpy(&nr
, &mtod(m
, u_int16_t
*)[1], 2);
801 /* Strip offset padding if present */
802 if ((hdr
& L2TP_HDR_OFF
) != 0) {
805 /* Get length of offset padding */
806 if (m
->m_len
< 2 && (m
= m_pullup(m
, 2)) == NULL
) {
807 priv
->stats
.memoryFailures
++;
811 memcpy(&offset
, mtod(m
, u_int16_t
*), 2);
812 offset
= ntohs(offset
);
814 /* Trim offset padding */
815 if (offset
<= 2 || offset
> m
->m_pkthdr
.len
) {
816 priv
->stats
.recvInvalid
++;
817 NG_FREE_DATA(m
, meta
);
823 /* Handle control packets */
824 if ((hdr
& L2TP_HDR_CTRL
) != 0) {
826 /* Handle receive ack sequence number Nr */
827 ng_l2tp_seq_recv_nr(priv
, nr
);
829 /* Discard ZLB packets */
830 if (m
->m_pkthdr
.len
== 0) {
831 priv
->stats
.recvZLBs
++;
832 NG_FREE_DATA(m
, meta
);
837 * Prepend session ID to packet here: we don't want to accept
838 * the send sequence number Ns if we have to drop the packet
839 * later because of a memory error, because then the upper
840 * layer would never get the packet.
842 M_PREPEND(m
, 2, MB_DONTWAIT
);
844 priv
->stats
.memoryFailures
++;
848 memcpy(mtod(m
, u_int16_t
*), &ids
[1], 2);
850 /* Now handle send sequence number */
851 if (ng_l2tp_seq_recv_ns(priv
, ns
) == -1) {
852 NG_FREE_DATA(m
, meta
);
856 /* Deliver packet to upper layers */
857 NG_SEND_DATA(error
, priv
->ctrl
, m
, meta
);
861 /* Follow peer's lead in data sequencing, if configured to do so */
862 if (!hpriv
->conf
.control_dseq
)
863 hpriv
->conf
.enable_dseq
= ((hdr
& L2TP_HDR_SEQ
) != 0);
865 /* Handle data sequence numbers if present and enabled */
866 if ((hdr
& L2TP_HDR_SEQ
) != 0) {
867 if (hpriv
->conf
.enable_dseq
868 && L2TP_SEQ_DIFF(ns
, hpriv
->nr
) < 0) {
869 NG_FREE_DATA(m
, meta
); /* duplicate or out of order */
870 priv
->stats
.recvDataDrops
++;
876 /* Drop empty data packets */
877 if (m
->m_pkthdr
.len
== 0) {
878 NG_FREE_DATA(m
, meta
);
883 NG_SEND_DATA(error
, hook
, m
, meta
);
888 * Handle an outgoing control frame.
891 ng_l2tp_recv_ctrl(node_p node
, struct mbuf
*m
, meta_p meta
)
893 const priv_p priv
= NG_NODE_PRIVATE(node
);
894 struct l2tp_seq
*const seq
= &priv
->seq
;
897 /* Discard meta XXX should queue meta's along with packet */
900 /* Packet should have session ID prepended */
901 if (m
->m_pkthdr
.len
< 2) {
902 priv
->stats
.xmitInvalid
++;
907 /* Check max length */
908 if (m
->m_pkthdr
.len
>= 0x10000 - 14) {
909 priv
->stats
.xmitTooBig
++;
914 /* Find next empty slot in transmit queue */
915 for (i
= 0; i
< L2TP_MAX_XWIN
&& seq
->xwin
[i
] != NULL
; i
++);
916 if (i
== L2TP_MAX_XWIN
) {
917 priv
->stats
.xmitDrops
++;
923 /* Sanity check receive ack timer state */
924 KASSERT((i
== 0) ^ seq
->rack_timer_running
,
925 ("%s: xwin %d full but rack timer %srunning",
926 __func__
, i
, seq
->rack_timer_running
? "" : "not "));
928 /* If peer's receive window is already full, nothing else to do */
932 /* Start retransmit timer if not already running */
933 if (!seq
->rack_timer_running
) {
934 callout_reset(&seq
->rack_timer
,
935 hz
, ng_l2tp_seq_rack_timeout
, node
);
936 seq
->rack_timer_running
= 1;
941 if ((m
= L2TP_COPY_MBUF(seq
->xwin
[i
], MB_DONTWAIT
)) == NULL
) {
942 priv
->stats
.memoryFailures
++;
946 /* Send packet and increment xmit sequence number */
947 return (ng_l2tp_xmit_ctrl(priv
, m
, seq
->ns
++));
951 * Handle an outgoing data frame.
954 ng_l2tp_recv_data(node_p node
, struct mbuf
*m
, meta_p meta
, hookpriv_p hpriv
)
956 const priv_p priv
= NG_NODE_PRIVATE(node
);
961 /* Check max length */
962 if (m
->m_pkthdr
.len
>= 0x10000 - 12) {
963 priv
->stats
.xmitDataTooBig
++;
969 /* Prepend L2TP header */
971 + (2 * (hpriv
->conf
.include_length
!= 0))
972 + (4 * (hpriv
->conf
.enable_dseq
!= 0)),
975 priv
->stats
.memoryFailures
++;
980 if (hpriv
->conf
.include_length
) {
982 mtod(m
, u_int16_t
*)[i
++] = htons(m
->m_pkthdr
.len
);
984 mtod(m
, u_int16_t
*)[i
++] = priv
->conf
.peer_id
;
985 mtod(m
, u_int16_t
*)[i
++] = hpriv
->conf
.peer_id
;
986 if (hpriv
->conf
.enable_dseq
) {
988 mtod(m
, u_int16_t
*)[i
++] = htons(hpriv
->ns
);
989 mtod(m
, u_int16_t
*)[i
++] = htons(hpriv
->nr
);
992 mtod(m
, u_int16_t
*)[0] = htons(hdr
);
995 NG_SEND_DATA(error
, priv
->lower
, m
, meta
);
1000 * Send a message to our controlling node that we've failed.
1003 ng_l2tp_seq_failure(priv_p priv
)
1005 struct ng_mesg
*msg
;
1007 NG_MKMESSAGE(msg
, NGM_L2TP_COOKIE
, NGM_L2TP_ACK_FAILURE
, 0, M_NOWAIT
);
1010 ng_send_msg(priv
->node
, msg
, priv
->ftarget
, NULL
);
1013 /************************************************************************
1014 SEQUENCE NUMBER HANDLING
1015 ************************************************************************/
1018 * Initialize sequence number state.
1021 ng_l2tp_seq_init(priv_p priv
)
1023 struct l2tp_seq
*const seq
= &priv
->seq
;
1025 KASSERT(priv
->conf
.peer_win
>= 1,
1026 ("%s: peer_win is zero", __func__
));
1027 memset(seq
, 0, sizeof(*seq
));
1029 seq
->wmax
= priv
->conf
.peer_win
;
1030 if (seq
->wmax
> L2TP_MAX_XWIN
)
1031 seq
->wmax
= L2TP_MAX_XWIN
;
1032 seq
->ssth
= seq
->wmax
;
1033 seq
->max_rexmits
= priv
->conf
.rexmit_max
;
1034 seq
->max_rexmit_to
= priv
->conf
.rexmit_max_to
;
1035 callout_init(&seq
->rack_timer
);
1036 callout_init(&seq
->xack_timer
);
1037 L2TP_SEQ_CHECK(seq
);
1041 * Adjust sequence number state accordingly after reconfiguration.
1044 ng_l2tp_seq_adjust(priv_p priv
, const struct ng_l2tp_config
*conf
)
1046 struct l2tp_seq
*const seq
= &priv
->seq
;
1049 /* If disabling node, reset state sequence number */
1050 if (!conf
->enabled
) {
1051 ng_l2tp_seq_reset(priv
);
1055 /* Adjust peer's max recv window; it can only increase */
1056 new_wmax
= conf
->peer_win
;
1057 if (new_wmax
> L2TP_MAX_XWIN
)
1058 new_wmax
= L2TP_MAX_XWIN
;
1061 if (new_wmax
< seq
->wmax
)
1063 seq
->wmax
= new_wmax
;
1065 /* Update retransmit parameters */
1066 seq
->max_rexmits
= conf
->rexmit_max
;
1067 seq
->max_rexmit_to
= conf
->rexmit_max_to
;
1074 * Reset sequence number state.
1077 ng_l2tp_seq_reset(priv_p priv
)
1079 struct l2tp_seq
*const seq
= &priv
->seq
;
1080 hookpriv_p hpriv
= NULL
;
1085 L2TP_SEQ_CHECK(seq
);
1088 if (seq
->rack_timer_running
&& callout_stop(&seq
->rack_timer
) == 1) {
1089 seq
->rack_timer_running
= 0;
1090 NG_NODE_UNREF(priv
->node
);
1092 if (seq
->xack_timer_running
&& callout_stop(&seq
->xack_timer
) == 1) {
1093 seq
->xack_timer_running
= 0;
1094 NG_NODE_UNREF(priv
->node
);
1097 /* Free retransmit queue */
1098 for (i
= 0; i
< L2TP_MAX_XWIN
; i
++) {
1099 if (seq
->xwin
[i
] == NULL
)
1101 m_freem(seq
->xwin
[i
]);
1104 /* Reset session hooks' sequence number states */
1105 LIST_FOREACH(hook
, &priv
->node
->hooks
, hooks
) {
1106 if ((hpriv
= NG_HOOK_PRIVATE(hook
)) == NULL
)
1108 hpriv
->conf
.control_dseq
= 0;
1109 hpriv
->conf
.enable_dseq
= 0;
1114 /* Reset node's sequence number state */
1115 memset(seq
, 0, sizeof(*seq
));
1117 seq
->wmax
= L2TP_MAX_XWIN
;
1118 seq
->ssth
= seq
->wmax
;
1121 L2TP_SEQ_CHECK(seq
);
1125 * Handle receipt of an acknowledgement value (Nr) from peer.
1128 ng_l2tp_seq_recv_nr(priv_p priv
, u_int16_t nr
)
1130 struct l2tp_seq
*const seq
= &priv
->seq
;
1135 /* Verify peer's ACK is in range */
1136 if ((nack
= L2TP_SEQ_DIFF(nr
, seq
->rack
)) <= 0)
1137 return; /* duplicate ack */
1138 if (L2TP_SEQ_DIFF(nr
, seq
->ns
) > 0) {
1139 priv
->stats
.recvBadAcks
++; /* ack for packet not sent */
1142 KASSERT(nack
<= L2TP_MAX_XWIN
,
1143 ("%s: nack=%d > %d", __func__
, nack
, L2TP_MAX_XWIN
));
1145 /* Update receive ack stats */
1149 /* Free acknowledged packets and shift up packets in the xmit queue */
1150 for (i
= 0; i
< nack
; i
++)
1151 m_freem(seq
->xwin
[i
]);
1152 memmove(seq
->xwin
, seq
->xwin
+ nack
,
1153 (L2TP_MAX_XWIN
- nack
) * sizeof(*seq
->xwin
));
1154 memset(seq
->xwin
+ (L2TP_MAX_XWIN
- nack
), 0,
1155 nack
* sizeof(*seq
->xwin
));
1158 * Do slow-start/congestion avoidance windowing algorithm described
1159 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1160 * ACK had arrived separately.
1162 if (seq
->cwnd
< seq
->wmax
) {
1164 /* Handle slow start phase */
1165 if (seq
->cwnd
< seq
->ssth
) {
1168 if (seq
->cwnd
> seq
->ssth
) { /* into cg.av. phase */
1169 nack
= seq
->cwnd
- seq
->ssth
;
1170 seq
->cwnd
= seq
->ssth
;
1174 /* Handle congestion avoidance phase */
1175 if (seq
->cwnd
>= seq
->ssth
) {
1177 while (seq
->acks
>= seq
->cwnd
) {
1178 seq
->acks
-= seq
->cwnd
;
1179 if (seq
->cwnd
< seq
->wmax
)
1185 /* Stop xmit timer */
1186 if (seq
->rack_timer_running
&& callout_stop(&seq
->rack_timer
) == 1) {
1187 seq
->rack_timer_running
= 0;
1188 NG_NODE_UNREF(priv
->node
);
1191 /* If transmit queue is empty, we're done for now */
1192 if (seq
->xwin
[0] == NULL
)
1195 /* Start restransmit timer again */
1196 callout_reset(&seq
->rack_timer
,
1197 hz
, ng_l2tp_seq_rack_timeout
, priv
->node
);
1198 seq
->rack_timer_running
= 1;
1199 NG_NODE_REF(priv
->node
);
1202 * Send more packets, trying to keep peer's receive window full.
1203 * If there is a memory error, pretend packet was sent, as it
1204 * will get retransmitted later anyway.
1206 while ((i
= L2TP_SEQ_DIFF(seq
->ns
, seq
->rack
)) < seq
->cwnd
1207 && seq
->xwin
[i
] != NULL
) {
1208 if ((m
= L2TP_COPY_MBUF(seq
->xwin
[i
], MB_DONTWAIT
)) == NULL
)
1209 priv
->stats
.memoryFailures
++;
1211 ng_l2tp_xmit_ctrl(priv
, m
, seq
->ns
);
1217 * Handle receipt of a sequence number value (Ns) from peer.
1218 * We make no attempt to re-order out of order packets.
1220 * This function should only be called for non-ZLB packets.
1227 ng_l2tp_seq_recv_ns(priv_p priv
, u_int16_t ns
)
1229 struct l2tp_seq
*const seq
= &priv
->seq
;
1231 /* If not what we expect, drop packet and send an immediate ZLB ack */
1232 if (ns
!= seq
->nr
) {
1233 if (L2TP_SEQ_DIFF(ns
, seq
->nr
) < 0)
1234 priv
->stats
.recvDuplicates
++;
1236 priv
->stats
.recvOutOfOrder
++;
1237 ng_l2tp_xmit_ctrl(priv
, NULL
, seq
->ns
);
1241 /* Update recv sequence number */
1244 /* Start receive ack timer, if not already running */
1245 if (!seq
->xack_timer_running
) {
1246 callout_reset(&seq
->xack_timer
,
1247 L2TP_DELAYED_ACK
, ng_l2tp_seq_xack_timeout
, priv
->node
);
1248 seq
->xack_timer_running
= 1;
1249 NG_NODE_REF(priv
->node
);
1257 * Handle an ack timeout. We have an outstanding ack that we
1258 * were hoping to piggy-back, but haven't, so send a ZLB.
1261 ng_l2tp_seq_xack_timeout(void *arg
)
1263 const node_p node
= arg
;
1264 const priv_p priv
= NG_NODE_PRIVATE(node
);
1265 struct l2tp_seq
*const seq
= &priv
->seq
;
1267 /* Check if node is going away */
1269 if (NG_NODE_NOT_VALID(node
)) {
1270 seq
->xack_timer_running
= 0;
1271 if (!seq
->rack_timer_running
) {
1272 if (priv
->ftarget
!= NULL
)
1273 FREE(priv
->ftarget
, M_NETGRAPH_L2TP
);
1274 FREE(priv
, M_NETGRAPH_L2TP
);
1275 NG_NODE_SET_PRIVATE(node
, NULL
);
1277 NG_NODE_UNREF(node
);
1283 L2TP_SEQ_CHECK(seq
);
1285 /* If ack is still outstanding, send a ZLB */
1286 if (seq
->xack
!= seq
->nr
)
1287 ng_l2tp_xmit_ctrl(priv
, NULL
, seq
->ns
);
1290 seq
->xack_timer_running
= 0;
1291 NG_NODE_UNREF(node
);
1292 L2TP_SEQ_CHECK(seq
);
1297 * Handle a transmit timeout. The peer has failed to respond
1298 * with an ack for our packet, so retransmit it.
1301 ng_l2tp_seq_rack_timeout(void *arg
)
1303 const node_p node
= arg
;
1304 const priv_p priv
= NG_NODE_PRIVATE(node
);
1305 struct l2tp_seq
*const seq
= &priv
->seq
;
1309 /* Check if node is going away */
1311 if (NG_NODE_NOT_VALID(node
)) {
1312 seq
->rack_timer_running
= 0;
1313 if (!seq
->xack_timer_running
) {
1314 if (priv
->ftarget
!= NULL
)
1315 FREE(priv
->ftarget
, M_NETGRAPH_L2TP
);
1316 FREE(priv
, M_NETGRAPH_L2TP
);
1317 NG_NODE_SET_PRIVATE(node
, NULL
);
1319 NG_NODE_UNREF(node
);
1325 L2TP_SEQ_CHECK(seq
);
1327 /* Make sure peer's ack is still outstanding before doing anything */
1328 if (seq
->rack
== seq
->ns
) {
1329 seq
->rack_timer_running
= 0;
1330 NG_NODE_UNREF(node
);
1333 priv
->stats
.xmitRetransmits
++;
1335 /* Have we reached the retransmit limit? If so, notify owner. */
1336 if (seq
->rexmits
++ >= seq
->max_rexmits
)
1337 ng_l2tp_seq_failure(priv
);
1339 /* Restart timer, this time with an increased delay */
1340 delay
= (seq
->rexmits
> 12) ? (1 << 12) : (1 << seq
->rexmits
);
1341 if (delay
> seq
->max_rexmit_to
)
1342 delay
= seq
->max_rexmit_to
;
1343 callout_reset(&seq
->rack_timer
,
1344 hz
* delay
, ng_l2tp_seq_rack_timeout
, node
);
1346 /* Do slow-start/congestion algorithm windowing algorithm */
1347 seq
->ssth
= (seq
->cwnd
+ 1) / 2;
1351 /* Retransmit oldest unack'd packet */
1352 if ((m
= L2TP_COPY_MBUF(seq
->xwin
[0], MB_DONTWAIT
)) == NULL
)
1353 priv
->stats
.memoryFailures
++;
1355 ng_l2tp_xmit_ctrl(priv
, m
, seq
->rack
);
1359 L2TP_SEQ_CHECK(seq
);
1364 * Transmit a control stream packet, payload optional.
1365 * The transmit sequence number is not incremented.
1368 ng_l2tp_xmit_ctrl(priv_p priv
, struct mbuf
*m
, u_int16_t ns
)
1370 struct l2tp_seq
*const seq
= &priv
->seq
;
1371 u_int16_t session_id
= 0;
1375 /* If no mbuf passed, send an empty packet (ZLB) */
1378 /* Create a new mbuf for ZLB packet */
1379 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
1381 priv
->stats
.memoryFailures
++;
1384 m
->m_len
= m
->m_pkthdr
.len
= 12;
1385 m
->m_pkthdr
.rcvif
= NULL
;
1386 priv
->stats
.xmitZLBs
++;
1389 /* Strip off session ID */
1390 if (m
->m_len
< 2 && (m
= m_pullup(m
, 2)) == NULL
) {
1391 priv
->stats
.memoryFailures
++;
1394 memcpy(&session_id
, mtod(m
, u_int16_t
*), 2);
1397 /* Make room for L2TP header */
1398 M_PREPEND(m
, 12, MB_DONTWAIT
);
1400 priv
->stats
.memoryFailures
++;
1405 /* Fill in L2TP header */
1406 mtod(m
, u_int16_t
*)[0] = htons(L2TP_CTRL_HDR
);
1407 mtod(m
, u_int16_t
*)[1] = htons(m
->m_pkthdr
.len
);
1408 mtod(m
, u_int16_t
*)[2] = priv
->conf
.peer_id
;
1409 mtod(m
, u_int16_t
*)[3] = session_id
;
1410 mtod(m
, u_int16_t
*)[4] = htons(ns
);
1411 mtod(m
, u_int16_t
*)[5] = htons(seq
->nr
);
1413 /* Update sequence number info and stats */
1414 priv
->stats
.xmitPackets
++;
1415 priv
->stats
.xmitOctets
+= m
->m_pkthdr
.len
;
1417 /* Stop ack timer: we're sending an ack with this packet */
1418 if (seq
->xack_timer_running
&& callout_stop(&seq
->xack_timer
) == 1) {
1419 seq
->xack_timer_running
= 0;
1420 NG_NODE_UNREF(priv
->node
);
1422 seq
->xack
= seq
->nr
;
1425 NG_SEND_DATA(error
, priv
->lower
, m
, meta
);
1431 * Sanity check sequence number state.
1434 ng_l2tp_seq_check(struct l2tp_seq
*seq
)
1436 const int self_unack
= L2TP_SEQ_DIFF(seq
->nr
, seq
->xack
);
1437 const int peer_unack
= L2TP_SEQ_DIFF(seq
->ns
, seq
->rack
);
1440 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1442 CHECK(seq
->wmax
<= L2TP_MAX_XWIN
);
1443 CHECK(seq
->cwnd
>= 1);
1444 CHECK(seq
->cwnd
<= seq
->wmax
);
1445 CHECK(seq
->ssth
>= 1);
1446 CHECK(seq
->ssth
<= seq
->wmax
);
1447 if (seq
->cwnd
< seq
->ssth
)
1448 CHECK(seq
->acks
== 0);
1450 CHECK(seq
->acks
<= seq
->cwnd
);
1451 CHECK(self_unack
>= 0);
1452 CHECK(peer_unack
>= 0);
1453 CHECK(peer_unack
<= seq
->wmax
);
1454 CHECK((self_unack
== 0) ^ seq
->xack_timer_running
);
1455 CHECK((peer_unack
== 0) ^ seq
->rack_timer_running
);
1456 CHECK(seq
->rack_timer_running
|| !callout_pending(&seq
->rack_timer
));
1457 CHECK(seq
->xack_timer_running
|| !callout_pending(&seq
->xack_timer
));
1458 for (i
= 0; i
< peer_unack
; i
++)
1459 CHECK(seq
->xwin
[i
] != NULL
);
1460 for ( ; i
< seq
->cwnd
; i
++) /* verify peer's recv window full */
1461 CHECK(seq
->xwin
[i
] == NULL
);
1465 #endif /* INVARIANTS */