1
2 /*
3 * Copyright (c) 2001-2002 Packet Design, LLC.
4 * All rights reserved.
5 *
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:
10 *
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.
18 *
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.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $FreeBSD: src/sys/netgraph/ng_l2tp.c,v 1.1.2.1 2002/08/20 23:48:15 archie Exp $
41 */
42
43 /*
44 * L2TP netgraph node type.
45 *
46 * This node type implements the lower layer of the
47 * L2TP protocol as specified in RFC 2661.
48 */
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/time.h>
54 #include <sys/conf.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/errno.h>
58 #include <sys/libkern.h>
59 #include <sys/thread2.h>
60
61 #include <netgraph/ng_message.h>
62 #include <netgraph/netgraph.h>
63 #include <netgraph/ng_parse.h>
64 #include "ng_l2tp.h"
65
66 #ifdef NG_SEPARATE_MALLOC
67 MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
68 #else
69 #define M_NETGRAPH_L2TP M_NETGRAPH
70 #endif
71
72 /* L2TP header format (first 2 bytes only) */
73 #define L2TP_HDR_CTRL 0x8000 /* control packet */
74 #define L2TP_HDR_LEN 0x4000 /* has length field */
75 #define L2TP_HDR_SEQ 0x0800 /* has ns, nr fields */
76 #define L2TP_HDR_OFF 0x0200 /* has offset field */
77 #define L2TP_HDR_PRIO 0x0100 /* give priority */
78 #define L2TP_HDR_VERS_MASK 0x000f /* version field mask */
79 #define L2TP_HDR_VERSION 0x0002 /* version field */
80
81 /* Bits that must be zero or one in first two bytes of header */
82 #define L2TP_CTRL_0BITS 0x030d /* ctrl: must be 0 */
83 #define L2TP_CTRL_1BITS 0xc802 /* ctrl: must be 1 */
84 #define L2TP_DATA_0BITS 0x800d /* data: must be 0 */
85 #define L2TP_DATA_1BITS 0x0002 /* data: must be 1 */
86
87 /* Standard xmit ctrl and data header bits */
88 #define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
89 | L2TP_HDR_SEQ | L2TP_HDR_VERSION)
90 #define L2TP_DATA_HDR (L2TP_HDR_VERSION) /* optional: len, seq */
91
92 /* Some hard coded values */
93 #define L2TP_MAX_XWIN 16 /* my max xmit window */
94 #define L2TP_MAX_REXMIT 5 /* default max rexmit */
95 #define L2TP_MAX_REXMIT_TO 30 /* default rexmit to */
96 #define L2TP_DELAYED_ACK ((hz + 19) / 20) /* delayed ack: 50 ms */
97
98 /* Default data sequence number configuration for new sessions */
99 #define L2TP_CONTROL_DSEQ 1 /* we are the lns */
100 #define L2TP_ENABLE_DSEQ 1 /* enable data seq # */
101
102 /* Compare sequence numbers using circular math */
103 #define L2TP_SEQ_DIFF(x, y) ((int)((int16_t)(x) - (int16_t)(y)))
104
105 /*
106 * Sequence number state
107 *
108 * Invariants:
109 * - If cwnd < ssth, we're doing slow start, otherwise congestion avoidance
110 * - The number of unacknowledged xmit packets is (ns - rack) <= seq->wmax
111 * - The first (ns - rack) mbuf's in xwin[] array are copies of these
112 * unacknowledged packets; the remainder of xwin[] consists first of
113 * zero or more further untransmitted packets in the transmit queue
114 * - We try to keep the peer's receive window as full as possible.
115 * Therefore, (i < cwnd && xwin[i] != NULL) implies (ns - rack) > i.
116 * - rack_timer is running iff (ns - rack) > 0 (unack'd xmit'd pkts)
117 * - If xack != nr, there are unacknowledged recv packet(s) (delayed ack)
118 * - xack_timer is running iff xack != nr (unack'd rec'd pkts)
119 */
120 struct l2tp_seq {
121 u_int16_t ns; /* next xmit seq we send */
122 u_int16_t nr; /* next recv seq we expect */
123 u_int16_t rack; /* last 'nr' we rec'd */
124 u_int16_t xack; /* last 'nr' we sent */
125 u_int16_t wmax; /* peer's max recv window */
126 u_int16_t cwnd; /* current congestion window */
127 u_int16_t ssth; /* slow start threshold */
128 u_int16_t acks; /* # consecutive acks rec'd */
129 u_int16_t rexmits; /* # retransmits sent */
130 u_int16_t max_rexmits; /* max # retransmits sent */
131 u_int16_t max_rexmit_to; /* max retransmit timeout */
132 struct callout rack_timer; /* retransmit timer */
133 struct callout xack_timer; /* delayed ack timer */
134 u_char rack_timer_running; /* xmit timer running */
135 u_char xack_timer_running; /* ack timer running */
136 struct mbuf *xwin[L2TP_MAX_XWIN]; /* transmit window */
137 };
138
139 /* Node private data */
140 struct ng_l2tp_private {
141 node_p node; /* back pointer to node */
142 hook_p ctrl; /* hook to upper layers */
143 hook_p lower; /* hook to lower layers */
144 struct ng_l2tp_config conf; /* node configuration */
145 struct ng_l2tp_stats stats; /* node statistics */
146 struct l2tp_seq seq; /* ctrl sequence number state */
147 char *ftarget; /* failure message target */
148 };
149 typedef struct ng_l2tp_private *priv_p;
150
151 /* Hook private data (data session hooks only) */
152 struct ng_l2tp_hook_private {
153 struct ng_l2tp_sess_config conf; /* hook/session config */
154 u_int16_t ns; /* data ns sequence number */
155 u_int16_t nr; /* data nr sequence number */
156 };
157 typedef struct ng_l2tp_hook_private *hookpriv_p;
158
159 /* Netgraph node methods */
160 static ng_constructor_t ng_l2tp_constructor;
161 static ng_rcvmsg_t ng_l2tp_rcvmsg;
162 static ng_shutdown_t ng_l2tp_shutdown;
163 static ng_newhook_t ng_l2tp_newhook;
164 static ng_rcvdata_t ng_l2tp_rcvdata;
165 static ng_disconnect_t ng_l2tp_disconnect;
166
167 /* Internal functions */
168 static int ng_l2tp_recv_lower(node_p node, struct mbuf *m, meta_p meta);
169 static int ng_l2tp_recv_ctrl(node_p node, struct mbuf *m, meta_p meta);
170 static int ng_l2tp_recv_data(node_p node,
171 struct mbuf *m, meta_p meta, hookpriv_p hpriv);
172
173 static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
174
175 static void ng_l2tp_seq_init(priv_p priv);
176 static int ng_l2tp_seq_adjust(priv_p priv,
177 const struct ng_l2tp_config *conf);
178 static void ng_l2tp_seq_reset(priv_p priv);
179 static void ng_l2tp_seq_failure(priv_p priv);
180 static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
181 static int ng_l2tp_seq_recv_ns(priv_p priv, u_int16_t ns);
182 static void ng_l2tp_seq_xack_timeout(void *arg);
183 static void ng_l2tp_seq_rack_timeout(void *arg);
184
185 #ifdef INVARIANTS
186 static void ng_l2tp_seq_check(struct l2tp_seq *seq);
187 #endif
188
189 /* Parse type for struct ng_l2tp_config */
190 static const struct ng_parse_struct_field
191 ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
192 static const struct ng_parse_type ng_l2tp_config_type = {
193 &ng_parse_struct_type,
194 &ng_l2tp_config_type_fields,
195 };
196
197 /* Parse type for struct ng_l2tp_sess_config */
198 static const struct ng_parse_struct_field
199 ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
200 static const struct ng_parse_type ng_l2tp_sess_config_type = {
201 &ng_parse_struct_type,
202 &ng_l2tp_sess_config_type_fields,
203 };
204
205 /* Parse type for struct ng_l2tp_stats */
206 static const struct ng_parse_struct_field
207 ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
208 static const struct ng_parse_type ng_pptp_stats_type = {
209 &ng_parse_struct_type,
210 &ng_l2tp_stats_type_fields
211 };
212
213 /* List of commands and how to convert arguments to/from ASCII */
214 static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
215 {
216 NGM_L2TP_COOKIE,
217 NGM_L2TP_SET_CONFIG,
218 "setconfig",
219 &ng_l2tp_config_type,
220 NULL
221 },
222 {
223 NGM_L2TP_COOKIE,
224 NGM_L2TP_GET_CONFIG,
225 "getconfig",
226 NULL,
227 &ng_l2tp_config_type
228 },
229 {
230 NGM_L2TP_COOKIE,
231 NGM_L2TP_SET_SESS_CONFIG,
232 "setsessconfig",
233 &ng_l2tp_sess_config_type,
234 NULL
235 },
236 {
237 NGM_L2TP_COOKIE,
238 NGM_L2TP_GET_SESS_CONFIG,
239 "getsessconfig",
240 &ng_parse_hint16_type,
241 &ng_l2tp_sess_config_type
242 },
243 {
244 NGM_L2TP_COOKIE,
245 NGM_L2TP_GET_STATS,
246 "getstats",
247 NULL,
248 &ng_pptp_stats_type
249 },
250 {
251 NGM_L2TP_COOKIE,
252 NGM_L2TP_CLR_STATS,
253 "clrstats",
254 NULL,
255 NULL
256 },
257 {
258 NGM_L2TP_COOKIE,
259 NGM_L2TP_GETCLR_STATS,
260 "getclrstats",
261 NULL,
262 &ng_pptp_stats_type
263 },
264 {
265 NGM_L2TP_COOKIE,
266 NGM_L2TP_ACK_FAILURE,
267 "ackfailure",
268 NULL,
269 NULL
270 },
271 { 0 }
272 };
273
274 /* Node type descriptor */
275 static struct ng_type ng_l2tp_typestruct = {
276 NG_VERSION,
277 NG_L2TP_NODE_TYPE,
278 NULL,
279 ng_l2tp_constructor,
280 ng_l2tp_rcvmsg,
281 ng_l2tp_shutdown,
282 ng_l2tp_newhook,
283 NULL,
284 NULL,
285 ng_l2tp_rcvdata,
286 ng_l2tp_rcvdata,
287 ng_l2tp_disconnect,
288 ng_l2tp_cmdlist
289 };
290 NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
291
292 /* Sequence number state sanity checking */
293 #ifdef INVARIANTS
294 #define L2TP_SEQ_CHECK(seq) ng_l2tp_seq_check(seq)
295 #else
296 #define L2TP_SEQ_CHECK(x) do { } while (0)
297 #endif
298
299 /* Whether to use m_copypacket() or m_dup() */
300 #define L2TP_COPY_MBUF m_copypacket
301
302 /************************************************************************
303 NETGRAPH NODE STUFF
304 ************************************************************************/
305
306 /*
307 * Node type constructor
308 */
309 static int
ng_l2tp_constructor(node_p * nodep)310 ng_l2tp_constructor(node_p *nodep)
311 {
312 priv_p priv;
313 int error;
314
315 /* Allocate private structure */
316 priv = kmalloc(sizeof(*priv), M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
317 if (priv == NULL)
318 return (ENOMEM);
319
320 /* Apply a semi-reasonable default configuration */
321 priv->conf.peer_win = 1;
322 priv->conf.rexmit_max = L2TP_MAX_REXMIT;
323 priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
324
325 /* Initialize sequence number state */
326 ng_l2tp_seq_init(priv);
327
328 /* Call generic node constructor */
329 if ((error = ng_make_node_common(&ng_l2tp_typestruct, nodep))) {
330 kfree(priv, M_NETGRAPH_L2TP);
331 return (error);
332 }
333 NG_NODE_SET_PRIVATE(*nodep, priv);
334 priv->node = *nodep;
335
336 /* Done */
337 return (0);
338 }
339
340 /*
341 * Give our OK for a hook to be added.
342 */
343 static int
ng_l2tp_newhook(node_p node,hook_p hook,const char * name)344 ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
345 {
346 const priv_p priv = NG_NODE_PRIVATE(node);
347
348 /* Check hook name */
349 if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
350 if (priv->ctrl != NULL)
351 return (EISCONN);
352 priv->ctrl = hook;
353 } else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
354 if (priv->lower != NULL)
355 return (EISCONN);
356 priv->lower = hook;
357 } else {
358 static const char hexdig[16] = "0123456789abcdef";
359 u_int16_t session_id;
360 hookpriv_p hpriv;
361 const char *hex;
362 int i;
363 int j;
364
365 /* Parse hook name to get session ID */
366 if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
367 sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
368 return (EINVAL);
369 hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
370 for (session_id = i = 0; i < 4; i++) {
371 for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
372 if (j == 16)
373 return (EINVAL);
374 session_id = (session_id << 4) | j;
375 }
376 if (hex[i] != '\0')
377 return (EINVAL);
378
379 /* Create hook private structure */
380 hpriv = kmalloc(sizeof(*hpriv), M_NETGRAPH_L2TP,
381 M_NOWAIT | M_ZERO);
382 if (hpriv == NULL)
383 return (ENOMEM);
384 hpriv->conf.session_id = htons(session_id);
385 hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
386 hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
387 NG_HOOK_SET_PRIVATE(hook, hpriv);
388 }
389
390 /* Done */
391 return (0);
392 }
393
394 /*
395 * Receive a control message.
396 */
397 static int
ng_l2tp_rcvmsg(node_p node,struct ng_mesg * msg,const char * raddr,struct ng_mesg ** rptr)398 ng_l2tp_rcvmsg(node_p node, struct ng_mesg *msg,
399 const char *raddr, struct ng_mesg **rptr)
400 {
401 const priv_p priv = NG_NODE_PRIVATE(node);
402 struct ng_mesg *resp = NULL;
403 int error = 0;
404
405 switch (msg->header.typecookie) {
406 case NGM_L2TP_COOKIE:
407 switch (msg->header.cmd) {
408 case NGM_L2TP_SET_CONFIG:
409 {
410 struct ng_l2tp_config *const conf =
411 (struct ng_l2tp_config *)msg->data;
412
413 /* Check for invalid or illegal config */
414 if (msg->header.arglen != sizeof(*conf)) {
415 error = EINVAL;
416 break;
417 }
418 conf->enabled = !!conf->enabled;
419 conf->match_id = !!conf->match_id;
420 conf->tunnel_id = htons(conf->tunnel_id);
421 conf->peer_id = htons(conf->peer_id);
422 if (priv->conf.enabled
423 && ((priv->conf.tunnel_id != 0
424 && conf->tunnel_id != priv->conf.tunnel_id)
425 || ((priv->conf.peer_id != 0
426 && conf->peer_id != priv->conf.peer_id)))) {
427 error = EBUSY;
428 break;
429 }
430
431 /* Save calling node as failure target */
432 if (priv->ftarget != NULL)
433 kfree(priv->ftarget, M_NETGRAPH_L2TP);
434 priv->ftarget = kmalloc(strlen(raddr) + 1,
435 M_NETGRAPH_L2TP, M_NOWAIT);
436 if (priv->ftarget == NULL) {
437 error = ENOMEM;
438 break;
439 }
440 strcpy(priv->ftarget, raddr);
441
442 /* Adjust sequence number state */
443 if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
444 break;
445
446 /* Update node's config */
447 priv->conf = *conf;
448 break;
449 }
450 case NGM_L2TP_GET_CONFIG:
451 {
452 struct ng_l2tp_config *conf;
453
454 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
455 if (resp == NULL) {
456 error = ENOMEM;
457 break;
458 }
459 conf = (struct ng_l2tp_config *)resp->data;
460 *conf = priv->conf;
461
462 /* Put ID's in host order */
463 conf->tunnel_id = ntohs(conf->tunnel_id);
464 conf->peer_id = ntohs(conf->peer_id);
465 break;
466 }
467 case NGM_L2TP_SET_SESS_CONFIG:
468 {
469 struct ng_l2tp_sess_config *const conf =
470 (struct ng_l2tp_sess_config *)msg->data;
471 hookpriv_p hpriv = NULL;
472 hook_p hook;
473
474 /* Check for invalid or illegal config */
475 if (msg->header.arglen != sizeof(*conf)) {
476 error = EINVAL;
477 break;
478 }
479
480 /* Put ID's in network order */
481 conf->session_id = htons(conf->session_id);
482 conf->peer_id = htons(conf->peer_id);
483
484 /* Find matching hook */
485 LIST_FOREACH(hook, &node->hooks, hooks) {
486 if ((hpriv = NG_HOOK_PRIVATE(hook)) == NULL)
487 continue;
488 if (hpriv->conf.session_id == conf->session_id)
489 break;
490 }
491 if (hook == NULL) {
492 error = ENOENT;
493 break;
494 }
495
496 /* Update hook's config */
497 hpriv->conf = *conf;
498 break;
499 }
500 case NGM_L2TP_GET_SESS_CONFIG:
501 {
502 struct ng_l2tp_sess_config *conf;
503 hookpriv_p hpriv = NULL;
504 u_int16_t session_id;
505 hook_p hook;
506
507 /* Get session ID */
508 if (msg->header.arglen != sizeof(session_id)) {
509 error = EINVAL;
510 break;
511 }
512 memcpy(&session_id, msg->data, 2);
513 session_id = htons(session_id);
514
515 /* Find matching hook */
516 LIST_FOREACH(hook, &node->hooks, hooks) {
517 if ((hpriv = NG_HOOK_PRIVATE(hook)) == NULL)
518 continue;
519 if (hpriv->conf.session_id == session_id)
520 break;
521 }
522 if (hook == NULL) {
523 error = ENOENT;
524 break;
525 }
526
527 /* Send response */
528 NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
529 if (resp == NULL) {
530 error = ENOMEM;
531 break;
532 }
533 conf = (struct ng_l2tp_sess_config *)resp->data;
534 *conf = hpriv->conf;
535
536 /* Put ID's in host order */
537 conf->session_id = ntohs(conf->session_id);
538 conf->peer_id = ntohs(conf->peer_id);
539 break;
540 }
541 case NGM_L2TP_GET_STATS:
542 case NGM_L2TP_CLR_STATS:
543 case NGM_L2TP_GETCLR_STATS:
544 {
545 if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
546 NG_MKRESPONSE(resp, msg,
547 sizeof(priv->stats), M_NOWAIT);
548 if (resp == NULL) {
549 error = ENOMEM;
550 break;
551 }
552 memcpy(resp->data,
553 &priv->stats, sizeof(priv->stats));
554 }
555 if (msg->header.cmd != NGM_L2TP_GET_STATS)
556 memset(&priv->stats, 0, sizeof(priv->stats));
557 break;
558 }
559 default:
560 error = EINVAL;
561 break;
562 }
563 break;
564 default:
565 error = EINVAL;
566 break;
567 }
568 if (rptr)
569 *rptr = resp;
570 else if (resp)
571 kfree(resp, M_NETGRAPH);
572 kfree(msg, M_NETGRAPH);
573 return (error);
574 }
575
576 /*
577 * Receive incoming data on a hook.
578 */
579 static int
ng_l2tp_rcvdata(hook_p hook,struct mbuf * m,meta_p meta)580 ng_l2tp_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
581 {
582 const node_p node = NG_HOOK_NODE(hook);
583 const priv_p priv = NG_NODE_PRIVATE(node);
584 int error;
585
586 /* Sanity check */
587 L2TP_SEQ_CHECK(&priv->seq);
588
589 /* If not configured, reject */
590 if (!priv->conf.enabled) {
591 NG_FREE_DATA(m, meta);
592 return (ENXIO);
593 }
594
595 /* Handle incoming frame from below */
596 if (hook == priv->lower) {
597 error = ng_l2tp_recv_lower(node, m, meta);
598 goto done;
599 }
600
601 /* Handle outgoing control frame */
602 if (hook == priv->ctrl) {
603 error = ng_l2tp_recv_ctrl(node, m, meta);
604 goto done;
605 }
606
607 /* Handle outgoing data frame */
608 error = ng_l2tp_recv_data(node, m, meta, NG_HOOK_PRIVATE(hook));
609
610 done:
611 /* Done */
612 L2TP_SEQ_CHECK(&priv->seq);
613 return (error);
614 }
615
616 /*
617 * Destroy node
618 */
619 static int
ng_l2tp_shutdown(node_p node)620 ng_l2tp_shutdown(node_p node)
621 {
622 const priv_p priv = NG_NODE_PRIVATE(node);
623 struct l2tp_seq *const seq = &priv->seq;
624
625 /* Sanity check */
626 L2TP_SEQ_CHECK(seq);
627
628 /* Reset sequence number state */
629 node->flags |= NG_INVALID;
630 ng_l2tp_seq_reset(priv);
631 ng_cutlinks(node);
632 ng_unname(node);
633
634 /* Free private data if neither timer is running */
635 if (!seq->rack_timer_running && !seq->xack_timer_running) {
636 if (priv->ftarget != NULL)
637 kfree(priv->ftarget, M_NETGRAPH_L2TP);
638 kfree(priv, M_NETGRAPH_L2TP);
639 NG_NODE_SET_PRIVATE(node, NULL);
640 }
641
642 /* Unref node */
643 NG_NODE_UNREF(node);
644 return (0);
645 }
646
647 /*
648 * Hook disconnection
649 */
650 static int
ng_l2tp_disconnect(hook_p hook)651 ng_l2tp_disconnect(hook_p hook)
652 {
653 const node_p node = NG_HOOK_NODE(hook);
654 const priv_p priv = NG_NODE_PRIVATE(node);
655
656 /* Zero out hook pointer */
657 if (hook == priv->ctrl)
658 priv->ctrl = NULL;
659 else if (hook == priv->lower)
660 priv->lower = NULL;
661 else {
662 kfree(NG_HOOK_PRIVATE(hook), M_NETGRAPH_L2TP);
663 NG_HOOK_SET_PRIVATE(hook, NULL);
664 }
665
666 /* Go away if no longer connected to anything */
667 if (node->numhooks == 0)
668 ng_rmnode(node);
669 return (0);
670 }
671
672 /*************************************************************************
673 INTERNAL FUNCTIONS
674 *************************************************************************/
675
676 /*
677 * Handle an incoming frame from below.
678 */
679 static int
ng_l2tp_recv_lower(node_p node,struct mbuf * m,meta_p meta)680 ng_l2tp_recv_lower(node_p node, struct mbuf *m, meta_p meta)
681 {
682 static const u_int16_t req_bits[2][2] = {
683 { L2TP_DATA_0BITS, L2TP_DATA_1BITS },
684 { L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
685 };
686 const priv_p priv = NG_NODE_PRIVATE(node);
687 hookpriv_p hpriv = NULL;
688 hook_p hook = NULL;
689 u_int16_t ids[2];
690 u_int16_t hdr;
691 u_int16_t ns;
692 u_int16_t nr;
693 int is_ctrl;
694 int error;
695 int len;
696
697 /* Update stats */
698 priv->stats.recvPackets++;
699 priv->stats.recvOctets += m->m_pkthdr.len;
700
701 /* Get initial header */
702 if (m->m_pkthdr.len < 6) {
703 priv->stats.recvRunts++;
704 NG_FREE_DATA(m, meta);
705 return (EINVAL);
706 }
707 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
708 priv->stats.memoryFailures++;
709 NG_FREE_META(meta);
710 return (EINVAL);
711 }
712 hdr = ntohs(*mtod(m, u_int16_t *));
713 m_adj(m, 2);
714
715 /* Check required header bits and minimum length */
716 is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
717 if ((hdr & req_bits[is_ctrl][0]) != 0
718 || (~hdr & req_bits[is_ctrl][1]) != 0) {
719 priv->stats.recvInvalid++;
720 NG_FREE_DATA(m, meta);
721 return (EINVAL);
722 }
723 if (m->m_pkthdr.len < 4 /* tunnel, session id */
724 + (2 * ((hdr & L2TP_HDR_LEN) != 0)) /* length field */
725 + (4 * ((hdr & L2TP_HDR_SEQ) != 0)) /* seq # fields */
726 + (2 * ((hdr & L2TP_HDR_OFF) != 0))) { /* offset field */
727 priv->stats.recvRunts++;
728 NG_FREE_DATA(m, meta);
729 return (EINVAL);
730 }
731
732 /* Get and validate length field if present */
733 if ((hdr & L2TP_HDR_LEN) != 0) {
734 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
735 priv->stats.memoryFailures++;
736 NG_FREE_META(meta);
737 return (EINVAL);
738 }
739 len = (u_int16_t)ntohs(*mtod(m, u_int16_t *)) - 4;
740 m_adj(m, 2);
741 if (len < 0 || len > m->m_pkthdr.len) {
742 priv->stats.recvInvalid++;
743 NG_FREE_DATA(m, meta);
744 return (EINVAL);
745 }
746 if (len < m->m_pkthdr.len) /* trim extra bytes */
747 m_adj(m, -(m->m_pkthdr.len - len));
748 }
749
750 /* Get tunnel ID and session ID */
751 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
752 priv->stats.memoryFailures++;
753 NG_FREE_META(meta);
754 return (EINVAL);
755 }
756 memcpy(ids, mtod(m, u_int16_t *), 4);
757 m_adj(m, 4);
758
759 /* Check tunnel ID */
760 if (ids[0] != priv->conf.tunnel_id
761 && (priv->conf.match_id || ids[0] != 0)) {
762 priv->stats.recvWrongTunnel++;
763 NG_FREE_DATA(m, meta);
764 return (EADDRNOTAVAIL);
765 }
766
767 /* Check session ID (for data packets only) */
768 if ((hdr & L2TP_HDR_CTRL) == 0) {
769 LIST_FOREACH(hook, &node->hooks, hooks) {
770 if ((hpriv = NG_HOOK_PRIVATE(hook)) == NULL)
771 continue;
772 if (hpriv->conf.session_id == ids[1])
773 break;
774 }
775 if (hook == NULL) {
776 priv->stats.recvUnknownSID++;
777 NG_FREE_DATA(m, meta);
778 return (ENOTCONN);
779 }
780 hpriv = NG_HOOK_PRIVATE(hook);
781 }
782
783 /* Get Ns, Nr fields if present */
784 if ((hdr & L2TP_HDR_SEQ) != 0) {
785 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
786 priv->stats.memoryFailures++;
787 NG_FREE_META(meta);
788 return (EINVAL);
789 }
790 memcpy(&ns, &mtod(m, u_int16_t *)[0], 2);
791 ns = ntohs(ns);
792 memcpy(&nr, &mtod(m, u_int16_t *)[1], 2);
793 nr = ntohs(nr);
794 m_adj(m, 4);
795 } else {
796 nr = 0; /* avoid gcc complaint */
797 ns = 0; /* avoid gcc complaint */
798 }
799
800 /* Strip offset padding if present */
801 if ((hdr & L2TP_HDR_OFF) != 0) {
802 u_int16_t offset;
803
804 /* Get length of offset padding */
805 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
806 priv->stats.memoryFailures++;
807 NG_FREE_META(meta);
808 return (EINVAL);
809 }
810 memcpy(&offset, mtod(m, u_int16_t *), 2);
811 offset = ntohs(offset);
812
813 /* Trim offset padding */
814 if (offset <= 2 || offset > m->m_pkthdr.len) {
815 priv->stats.recvInvalid++;
816 NG_FREE_DATA(m, meta);
817 return (EINVAL);
818 }
819 m_adj(m, offset);
820 }
821
822 /* Handle control packets */
823 if ((hdr & L2TP_HDR_CTRL) != 0) {
824
825 /* Handle receive ack sequence number Nr */
826 ng_l2tp_seq_recv_nr(priv, nr);
827
828 /* Discard ZLB packets */
829 if (m->m_pkthdr.len == 0) {
830 priv->stats.recvZLBs++;
831 NG_FREE_DATA(m, meta);
832 return (0);
833 }
834
835 /*
836 * Prepend session ID to packet here: we don't want to accept
837 * the send sequence number Ns if we have to drop the packet
838 * later because of a memory error, because then the upper
839 * layer would never get the packet.
840 */
841 M_PREPEND(m, 2, M_NOWAIT);
842 if (m == NULL) {
843 priv->stats.memoryFailures++;
844 NG_FREE_META(meta);
845 return (ENOBUFS);
846 }
847 memcpy(mtod(m, u_int16_t *), &ids[1], 2);
848
849 /* Now handle send sequence number */
850 if (ng_l2tp_seq_recv_ns(priv, ns) == -1) {
851 NG_FREE_DATA(m, meta);
852 return (0);
853 }
854
855 /* Deliver packet to upper layers */
856 NG_SEND_DATA(error, priv->ctrl, m, meta);
857 return (error);
858 }
859
860 /* Follow peer's lead in data sequencing, if configured to do so */
861 if (!hpriv->conf.control_dseq)
862 hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
863
864 /* Handle data sequence numbers if present and enabled */
865 if ((hdr & L2TP_HDR_SEQ) != 0) {
866 if (hpriv->conf.enable_dseq
867 && L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
868 NG_FREE_DATA(m, meta); /* duplicate or out of order */
869 priv->stats.recvDataDrops++;
870 return (0);
871 }
872 hpriv->nr = ns + 1;
873 }
874
875 /* Drop empty data packets */
876 if (m->m_pkthdr.len == 0) {
877 NG_FREE_DATA(m, meta);
878 return (0);
879 }
880
881 /* Deliver data */
882 NG_SEND_DATA(error, hook, m, meta);
883 return (error);
884 }
885
886 /*
887 * Handle an outgoing control frame.
888 */
889 static int
ng_l2tp_recv_ctrl(node_p node,struct mbuf * m,meta_p meta)890 ng_l2tp_recv_ctrl(node_p node, struct mbuf *m, meta_p meta)
891 {
892 const priv_p priv = NG_NODE_PRIVATE(node);
893 struct l2tp_seq *const seq = &priv->seq;
894 int i;
895
896 /* Discard meta XXX should queue meta's along with packet */
897 NG_FREE_META(meta);
898
899 /* Packet should have session ID prepended */
900 if (m->m_pkthdr.len < 2) {
901 priv->stats.xmitInvalid++;
902 m_freem(m);
903 return (EINVAL);
904 }
905
906 /* Check max length */
907 if (m->m_pkthdr.len >= 0x10000 - 14) {
908 priv->stats.xmitTooBig++;
909 m_freem(m);
910 return (EOVERFLOW);
911 }
912
913 /* Find next empty slot in transmit queue */
914 for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
915 if (i == L2TP_MAX_XWIN) {
916 priv->stats.xmitDrops++;
917 m_freem(m);
918 return (ENOBUFS);
919 }
920 seq->xwin[i] = m;
921
922 /* Sanity check receive ack timer state */
923 KASSERT((i == 0) ^ seq->rack_timer_running,
924 ("%s: xwin %d full but rack timer %srunning",
925 __func__, i, seq->rack_timer_running ? "" : "not "));
926
927 /* If peer's receive window is already full, nothing else to do */
928 if (i >= seq->cwnd)
929 return (0);
930
931 /* Start retransmit timer if not already running */
932 if (!seq->rack_timer_running) {
933 callout_reset(&seq->rack_timer,
934 hz, ng_l2tp_seq_rack_timeout, node);
935 seq->rack_timer_running = 1;
936 NG_NODE_REF(node);
937 }
938
939 /* Copy packet */
940 if ((m = L2TP_COPY_MBUF(seq->xwin[i], M_NOWAIT)) == NULL) {
941 priv->stats.memoryFailures++;
942 return (ENOBUFS);
943 }
944
945 /* Send packet and increment xmit sequence number */
946 return (ng_l2tp_xmit_ctrl(priv, m, seq->ns++));
947 }
948
949 /*
950 * Handle an outgoing data frame.
951 */
952 static int
ng_l2tp_recv_data(node_p node,struct mbuf * m,meta_p meta,hookpriv_p hpriv)953 ng_l2tp_recv_data(node_p node, struct mbuf *m, meta_p meta, hookpriv_p hpriv)
954 {
955 const priv_p priv = NG_NODE_PRIVATE(node);
956 u_int16_t hdr;
957 int error;
958 int i = 1;
959
960 /* Check max length */
961 if (m->m_pkthdr.len >= 0x10000 - 12) {
962 priv->stats.xmitDataTooBig++;
963 NG_FREE_META(meta);
964 m_freem(m);
965 return (EOVERFLOW);
966 }
967
968 /* Prepend L2TP header */
969 M_PREPEND(m, 6
970 + (2 * (hpriv->conf.include_length != 0))
971 + (4 * (hpriv->conf.enable_dseq != 0)),
972 M_NOWAIT);
973 if (m == NULL) {
974 priv->stats.memoryFailures++;
975 NG_FREE_META(meta);
976 return (ENOBUFS);
977 }
978 hdr = L2TP_DATA_HDR;
979 if (hpriv->conf.include_length) {
980 hdr |= L2TP_HDR_LEN;
981 mtod(m, u_int16_t *)[i++] = htons(m->m_pkthdr.len);
982 }
983 mtod(m, u_int16_t *)[i++] = priv->conf.peer_id;
984 mtod(m, u_int16_t *)[i++] = hpriv->conf.peer_id;
985 if (hpriv->conf.enable_dseq) {
986 hdr |= L2TP_HDR_SEQ;
987 mtod(m, u_int16_t *)[i++] = htons(hpriv->ns);
988 mtod(m, u_int16_t *)[i++] = htons(hpriv->nr);
989 hpriv->ns++;
990 }
991 mtod(m, u_int16_t *)[0] = htons(hdr);
992
993 /* Send packet */
994 NG_SEND_DATA(error, priv->lower, m, meta);
995 return (error);
996 }
997
998 /*
999 * Send a message to our controlling node that we've failed.
1000 */
1001 static void
ng_l2tp_seq_failure(priv_p priv)1002 ng_l2tp_seq_failure(priv_p priv)
1003 {
1004 struct ng_mesg *msg;
1005
1006 NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
1007 if (msg == NULL)
1008 return;
1009 ng_send_msg(priv->node, msg, priv->ftarget, NULL);
1010 }
1011
1012 /************************************************************************
1013 SEQUENCE NUMBER HANDLING
1014 ************************************************************************/
1015
1016 /*
1017 * Initialize sequence number state.
1018 */
1019 static void
ng_l2tp_seq_init(priv_p priv)1020 ng_l2tp_seq_init(priv_p priv)
1021 {
1022 struct l2tp_seq *const seq = &priv->seq;
1023
1024 KASSERT(priv->conf.peer_win >= 1,
1025 ("%s: peer_win is zero", __func__));
1026 memset(seq, 0, sizeof(*seq));
1027 seq->cwnd = 1;
1028 seq->wmax = priv->conf.peer_win;
1029 if (seq->wmax > L2TP_MAX_XWIN)
1030 seq->wmax = L2TP_MAX_XWIN;
1031 seq->ssth = seq->wmax;
1032 seq->max_rexmits = priv->conf.rexmit_max;
1033 seq->max_rexmit_to = priv->conf.rexmit_max_to;
1034 callout_init(&seq->rack_timer);
1035 callout_init(&seq->xack_timer);
1036 L2TP_SEQ_CHECK(seq);
1037 }
1038
1039 /*
1040 * Adjust sequence number state accordingly after reconfiguration.
1041 */
1042 static int
ng_l2tp_seq_adjust(priv_p priv,const struct ng_l2tp_config * conf)1043 ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
1044 {
1045 struct l2tp_seq *const seq = &priv->seq;
1046 u_int16_t new_wmax;
1047
1048 /* If disabling node, reset state sequence number */
1049 if (!conf->enabled) {
1050 ng_l2tp_seq_reset(priv);
1051 return (0);
1052 }
1053
1054 /* Adjust peer's max recv window; it can only increase */
1055 new_wmax = conf->peer_win;
1056 if (new_wmax > L2TP_MAX_XWIN)
1057 new_wmax = L2TP_MAX_XWIN;
1058 if (new_wmax == 0)
1059 return (EINVAL);
1060 if (new_wmax < seq->wmax)
1061 return (EBUSY);
1062 seq->wmax = new_wmax;
1063
1064 /* Update retransmit parameters */
1065 seq->max_rexmits = conf->rexmit_max;
1066 seq->max_rexmit_to = conf->rexmit_max_to;
1067
1068 /* Done */
1069 return (0);
1070 }
1071
1072 /*
1073 * Reset sequence number state.
1074 */
1075 static void
ng_l2tp_seq_reset(priv_p priv)1076 ng_l2tp_seq_reset(priv_p priv)
1077 {
1078 struct l2tp_seq *const seq = &priv->seq;
1079 hookpriv_p hpriv = NULL;
1080 hook_p hook;
1081 int i;
1082
1083 /* Sanity check */
1084 L2TP_SEQ_CHECK(seq);
1085
1086 /* Stop timers */
1087 if (seq->rack_timer_running && callout_stop(&seq->rack_timer) == 1) {
1088 seq->rack_timer_running = 0;
1089 NG_NODE_UNREF(priv->node);
1090 }
1091 if (seq->xack_timer_running && callout_stop(&seq->xack_timer) == 1) {
1092 seq->xack_timer_running = 0;
1093 NG_NODE_UNREF(priv->node);
1094 }
1095
1096 /* Free retransmit queue */
1097 for (i = 0; i < L2TP_MAX_XWIN; i++) {
1098 if (seq->xwin[i] == NULL)
1099 break;
1100 m_freem(seq->xwin[i]);
1101 }
1102
1103 /* Reset session hooks' sequence number states */
1104 LIST_FOREACH(hook, &priv->node->hooks, hooks) {
1105 if ((hpriv = NG_HOOK_PRIVATE(hook)) == NULL)
1106 continue;
1107 hpriv->conf.control_dseq = 0;
1108 hpriv->conf.enable_dseq = 0;
1109 hpriv->nr = 0;
1110 hpriv->ns = 0;
1111 }
1112
1113 /* Reset node's sequence number state */
1114 memset(seq, 0, sizeof(*seq));
1115 seq->cwnd = 1;
1116 seq->wmax = L2TP_MAX_XWIN;
1117 seq->ssth = seq->wmax;
1118
1119 /* Done */
1120 L2TP_SEQ_CHECK(seq);
1121 }
1122
1123 /*
1124 * Handle receipt of an acknowledgement value (Nr) from peer.
1125 */
1126 static void
ng_l2tp_seq_recv_nr(priv_p priv,u_int16_t nr)1127 ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
1128 {
1129 struct l2tp_seq *const seq = &priv->seq;
1130 struct mbuf *m;
1131 int nack;
1132 int i;
1133
1134 /* Verify peer's ACK is in range */
1135 if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0)
1136 return; /* duplicate ack */
1137 if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
1138 priv->stats.recvBadAcks++; /* ack for packet not sent */
1139 return;
1140 }
1141 KASSERT(nack <= L2TP_MAX_XWIN,
1142 ("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
1143
1144 /* Update receive ack stats */
1145 seq->rack = nr;
1146 seq->rexmits = 0;
1147
1148 /* Free acknowledged packets and shift up packets in the xmit queue */
1149 for (i = 0; i < nack; i++)
1150 m_freem(seq->xwin[i]);
1151 memmove(seq->xwin, seq->xwin + nack,
1152 (L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
1153 memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
1154 nack * sizeof(*seq->xwin));
1155
1156 /*
1157 * Do slow-start/congestion avoidance windowing algorithm described
1158 * in RFC 2661, Appendix A. Here we handle a multiple ACK as if each
1159 * ACK had arrived separately.
1160 */
1161 if (seq->cwnd < seq->wmax) {
1162
1163 /* Handle slow start phase */
1164 if (seq->cwnd < seq->ssth) {
1165 seq->cwnd += nack;
1166 nack = 0;
1167 if (seq->cwnd > seq->ssth) { /* into cg.av. phase */
1168 nack = seq->cwnd - seq->ssth;
1169 seq->cwnd = seq->ssth;
1170 }
1171 }
1172
1173 /* Handle congestion avoidance phase */
1174 if (seq->cwnd >= seq->ssth) {
1175 seq->acks += nack;
1176 while (seq->acks >= seq->cwnd) {
1177 seq->acks -= seq->cwnd;
1178 if (seq->cwnd < seq->wmax)
1179 seq->cwnd++;
1180 }
1181 }
1182 }
1183
1184 /* Stop xmit timer */
1185 if (seq->rack_timer_running && callout_stop(&seq->rack_timer) == 1) {
1186 seq->rack_timer_running = 0;
1187 NG_NODE_UNREF(priv->node);
1188 }
1189
1190 /* If transmit queue is empty, we're done for now */
1191 if (seq->xwin[0] == NULL)
1192 return;
1193
1194 /* Start restransmit timer again */
1195 callout_reset(&seq->rack_timer,
1196 hz, ng_l2tp_seq_rack_timeout, priv->node);
1197 seq->rack_timer_running = 1;
1198 NG_NODE_REF(priv->node);
1199
1200 /*
1201 * Send more packets, trying to keep peer's receive window full.
1202 * If there is a memory error, pretend packet was sent, as it
1203 * will get retransmitted later anyway.
1204 */
1205 while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
1206 && seq->xwin[i] != NULL) {
1207 if ((m = L2TP_COPY_MBUF(seq->xwin[i], M_NOWAIT)) == NULL)
1208 priv->stats.memoryFailures++;
1209 else
1210 ng_l2tp_xmit_ctrl(priv, m, seq->ns);
1211 seq->ns++;
1212 }
1213 }
1214
1215 /*
1216 * Handle receipt of a sequence number value (Ns) from peer.
1217 * We make no attempt to re-order out of order packets.
1218 *
1219 * This function should only be called for non-ZLB packets.
1220 *
1221 * Returns:
1222 * 0 Accept packet
1223 * -1 Drop packet
1224 */
1225 static int
ng_l2tp_seq_recv_ns(priv_p priv,u_int16_t ns)1226 ng_l2tp_seq_recv_ns(priv_p priv, u_int16_t ns)
1227 {
1228 struct l2tp_seq *const seq = &priv->seq;
1229
1230 /* If not what we expect, drop packet and send an immediate ZLB ack */
1231 if (ns != seq->nr) {
1232 if (L2TP_SEQ_DIFF(ns, seq->nr) < 0)
1233 priv->stats.recvDuplicates++;
1234 else
1235 priv->stats.recvOutOfOrder++;
1236 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1237 return (-1);
1238 }
1239
1240 /* Update recv sequence number */
1241 seq->nr++;
1242
1243 /* Start receive ack timer, if not already running */
1244 if (!seq->xack_timer_running) {
1245 callout_reset(&seq->xack_timer,
1246 L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout, priv->node);
1247 seq->xack_timer_running = 1;
1248 NG_NODE_REF(priv->node);
1249 }
1250
1251 /* Accept packet */
1252 return (0);
1253 }
1254
1255 /*
1256 * Handle an ack timeout. We have an outstanding ack that we
1257 * were hoping to piggy-back, but haven't, so send a ZLB.
1258 */
1259 static void
ng_l2tp_seq_xack_timeout(void * arg)1260 ng_l2tp_seq_xack_timeout(void *arg)
1261 {
1262 const node_p node = arg;
1263 const priv_p priv = NG_NODE_PRIVATE(node);
1264 struct l2tp_seq *const seq = &priv->seq;
1265
1266 /* Check if node is going away */
1267 crit_enter();
1268 if (NG_NODE_NOT_VALID(node)) {
1269 seq->xack_timer_running = 0;
1270 if (!seq->rack_timer_running) {
1271 if (priv->ftarget != NULL)
1272 kfree(priv->ftarget, M_NETGRAPH_L2TP);
1273 kfree(priv, M_NETGRAPH_L2TP);
1274 NG_NODE_SET_PRIVATE(node, NULL);
1275 }
1276 NG_NODE_UNREF(node);
1277 crit_exit();
1278 return;
1279 }
1280
1281 /* Sanity check */
1282 L2TP_SEQ_CHECK(seq);
1283
1284 /* If ack is still outstanding, send a ZLB */
1285 if (seq->xack != seq->nr)
1286 ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
1287
1288 /* Done */
1289 seq->xack_timer_running = 0;
1290 NG_NODE_UNREF(node);
1291 L2TP_SEQ_CHECK(seq);
1292 crit_exit();
1293 }
1294
1295 /*
1296 * Handle a transmit timeout. The peer has failed to respond
1297 * with an ack for our packet, so retransmit it.
1298 */
1299 static void
ng_l2tp_seq_rack_timeout(void * arg)1300 ng_l2tp_seq_rack_timeout(void *arg)
1301 {
1302 const node_p node = arg;
1303 const priv_p priv = NG_NODE_PRIVATE(node);
1304 struct l2tp_seq *const seq = &priv->seq;
1305 struct mbuf *m;
1306 u_int delay;
1307
1308 /* Check if node is going away */
1309 crit_enter();
1310 if (NG_NODE_NOT_VALID(node)) {
1311 seq->rack_timer_running = 0;
1312 if (!seq->xack_timer_running) {
1313 if (priv->ftarget != NULL)
1314 kfree(priv->ftarget, M_NETGRAPH_L2TP);
1315 kfree(priv, M_NETGRAPH_L2TP);
1316 NG_NODE_SET_PRIVATE(node, NULL);
1317 }
1318 NG_NODE_UNREF(node);
1319 crit_exit();
1320 return;
1321 }
1322
1323 /* Sanity check */
1324 L2TP_SEQ_CHECK(seq);
1325
1326 /* Make sure peer's ack is still outstanding before doing anything */
1327 if (seq->rack == seq->ns) {
1328 seq->rack_timer_running = 0;
1329 NG_NODE_UNREF(node);
1330 goto done;
1331 }
1332 priv->stats.xmitRetransmits++;
1333
1334 /* Have we reached the retransmit limit? If so, notify owner. */
1335 if (seq->rexmits++ >= seq->max_rexmits)
1336 ng_l2tp_seq_failure(priv);
1337
1338 /* Restart timer, this time with an increased delay */
1339 delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
1340 if (delay > seq->max_rexmit_to)
1341 delay = seq->max_rexmit_to;
1342 callout_reset(&seq->rack_timer,
1343 hz * delay, ng_l2tp_seq_rack_timeout, node);
1344
1345 /* Do slow-start/congestion algorithm windowing algorithm */
1346 seq->ssth = (seq->cwnd + 1) / 2;
1347 seq->cwnd = 1;
1348 seq->acks = 0;
1349
1350 /* Retransmit oldest unack'd packet */
1351 if ((m = L2TP_COPY_MBUF(seq->xwin[0], M_NOWAIT)) == NULL)
1352 priv->stats.memoryFailures++;
1353 else
1354 ng_l2tp_xmit_ctrl(priv, m, seq->rack);
1355
1356 done:
1357 /* Done */
1358 L2TP_SEQ_CHECK(seq);
1359 crit_exit();
1360 }
1361
1362 /*
1363 * Transmit a control stream packet, payload optional.
1364 * The transmit sequence number is not incremented.
1365 */
1366 static int
ng_l2tp_xmit_ctrl(priv_p priv,struct mbuf * m,u_int16_t ns)1367 ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
1368 {
1369 struct l2tp_seq *const seq = &priv->seq;
1370 u_int16_t session_id = 0;
1371 meta_p meta = NULL;
1372 int error;
1373
1374 /* If no mbuf passed, send an empty packet (ZLB) */
1375 if (m == NULL) {
1376
1377 /* Create a new mbuf for ZLB packet */
1378 MGETHDR(m, M_NOWAIT, MT_DATA);
1379 if (m == NULL) {
1380 priv->stats.memoryFailures++;
1381 return (ENOBUFS);
1382 }
1383 m->m_len = m->m_pkthdr.len = 12;
1384 m->m_pkthdr.rcvif = NULL;
1385 priv->stats.xmitZLBs++;
1386 } else {
1387
1388 /* Strip off session ID */
1389 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1390 priv->stats.memoryFailures++;
1391 return (ENOBUFS);
1392 }
1393 memcpy(&session_id, mtod(m, u_int16_t *), 2);
1394 m_adj(m, 2);
1395
1396 /* Make room for L2TP header */
1397 M_PREPEND(m, 12, M_NOWAIT);
1398 if (m == NULL) {
1399 priv->stats.memoryFailures++;
1400 return (ENOBUFS);
1401 }
1402 }
1403
1404 /* Fill in L2TP header */
1405 mtod(m, u_int16_t *)[0] = htons(L2TP_CTRL_HDR);
1406 mtod(m, u_int16_t *)[1] = htons(m->m_pkthdr.len);
1407 mtod(m, u_int16_t *)[2] = priv->conf.peer_id;
1408 mtod(m, u_int16_t *)[3] = session_id;
1409 mtod(m, u_int16_t *)[4] = htons(ns);
1410 mtod(m, u_int16_t *)[5] = htons(seq->nr);
1411
1412 /* Update sequence number info and stats */
1413 priv->stats.xmitPackets++;
1414 priv->stats.xmitOctets += m->m_pkthdr.len;
1415
1416 /* Stop ack timer: we're sending an ack with this packet */
1417 if (seq->xack_timer_running && callout_stop(&seq->xack_timer) == 1) {
1418 seq->xack_timer_running = 0;
1419 NG_NODE_UNREF(priv->node);
1420 }
1421 seq->xack = seq->nr;
1422
1423 /* Send packet */
1424 NG_SEND_DATA(error, priv->lower, m, meta);
1425 return (error);
1426 }
1427
1428 #ifdef INVARIANTS
1429 /*
1430 * Sanity check sequence number state.
1431 */
1432 static void
ng_l2tp_seq_check(struct l2tp_seq * seq)1433 ng_l2tp_seq_check(struct l2tp_seq *seq)
1434 {
1435 const int self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
1436 const int peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
1437 int i;
1438
1439 #define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
1440
1441 CHECK(seq->wmax <= L2TP_MAX_XWIN);
1442 CHECK(seq->cwnd >= 1);
1443 CHECK(seq->cwnd <= seq->wmax);
1444 CHECK(seq->ssth >= 1);
1445 CHECK(seq->ssth <= seq->wmax);
1446 if (seq->cwnd < seq->ssth)
1447 CHECK(seq->acks == 0);
1448 else
1449 CHECK(seq->acks <= seq->cwnd);
1450 CHECK(self_unack >= 0);
1451 CHECK(peer_unack >= 0);
1452 CHECK(peer_unack <= seq->wmax);
1453 CHECK((self_unack == 0) ^ seq->xack_timer_running);
1454 CHECK((peer_unack == 0) ^ seq->rack_timer_running);
1455 CHECK(seq->rack_timer_running || !callout_pending(&seq->rack_timer));
1456 CHECK(seq->xack_timer_running || !callout_pending(&seq->xack_timer));
1457 for (i = 0; i < peer_unack; i++)
1458 CHECK(seq->xwin[i] != NULL);
1459 for ( ; i < seq->cwnd; i++) /* verify peer's recv window full */
1460 CHECK(seq->xwin[i] == NULL);
1461
1462 #undef CHECK
1463 }
1464 #endif /* INVARIANTS */
1465
1466
1467