1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
/* $OpenBSD: l2tpd.c,v 1.14 2014/03/22 04:32:39 yasuoka Exp $ */
/*-
* Copyright (c) 2009 Internet Initiative Japan Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**@file L2TP(Layer Two Tunneling Protocol "L2TP") / RFC2661 */
/* $Id: l2tpd.c,v 1.14 2014/03/22 04:32:39 yasuoka Exp $ */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <event.h>
#ifdef USE_LIBSOCKUTIL
#include <seil/sockfromto.h>
#else
#include "recvfromto.h"
#endif
#include "bytebuf.h"
#include "hash.h"
#include "slist.h"
#include "debugutil.h"
#include "l2tp.h"
#include "l2tp_subr.h"
#include "l2tp_local.h"
#include "addr_range.h"
#include "net_utils.h"
#ifdef L2TPD_DEBUG
#define L2TPD_ASSERT(x) ASSERT(x)
#define L2TPD_DBG(x) l2tpd_log x
#else
#define L2TPD_ASSERT(x)
#endif
#define L2TPD_IPSEC_POLICY_IN "in ipsec esp/transport//require"
#define L2TPD_IPSEC_POLICY_OUT "out ipsec esp/transport//require"
static void l2tpd_io_event (int, short, void *);
static inline int short_cmp (const void *, const void *);
static inline uint32_t short_hash (const void *, int);
/* sequence # of l2tpd ID */
static u_int l2tpd_id_seq = 0;
/* L2TP daemon instance */
/**
* initialize L2TP daemon instance
* <p>
* {@link _l2tpd#bind_sin} will return with .sin_family = AF_INET,
* .sin_port = 1701 and .sin_len = "appropriate value"
* </p>
*/
int
l2tpd_init(l2tpd *_this)
{
int i, off;
u_int id;
L2TPD_ASSERT(_this != NULL);
memset(_this, 0, sizeof(l2tpd));
slist_init(&_this->listener);
slist_init(&_this->free_session_id_list);
_this->id = l2tpd_id_seq++;
if ((_this->ctrl_map = hash_create(short_cmp, short_hash,
L2TPD_TUNNEL_HASH_SIZ)) == NULL) {
log_printf(LOG_ERR, "hash_create() failed in %s(): %m",
__func__);
return 1;
}
if (slist_add(&_this->free_session_id_list,
(void *)L2TP_SESSION_ID_SHUFFLE_MARK) == NULL) {
l2tpd_log(_this, LOG_ERR, "slist_add() failed on %s(): %m",
__func__);
return 1;
}
off = arc4random() % L2TP_SESSION_ID_MASK;
for (i = 0; i < L2TP_NCALL; i++) {
id = (i + off) % L2TP_SESSION_ID_MASK;
if (id == 0)
id = (off - 1) % L2TP_SESSION_ID_MASK;
if (slist_add(&_this->free_session_id_list,
(void *)(uintptr_t)id) == NULL) {
l2tpd_log(_this, LOG_ERR,
"slist_add() failed on %s(): %m", __func__);
return 1;
}
}
_this->purge_ipsec_sa = 1;
_this->state = L2TPD_STATE_INIT;
return 0;
}
/*
* Add a {@link :l2tpd_listener} to the {@link ::l2tpd L2TP daemon}
* @param _this {@link ::l2tpd L2TP daemon}
* @param idx index of the lisnter
* @param tun_name tunnel name (ex. "L2TP")
* @param bindaddr bind address
*/
int
l2tpd_add_listener(l2tpd *_this, int idx, struct l2tp_conf *conf,
struct sockaddr *addr)
{
l2tpd_listener *plistener, *plsnr;
plistener = NULL;
if (idx == 0 && slist_length(&_this->listener) > 0) {
slist_itr_first(&_this->listener);
while (slist_itr_has_next(&_this->listener)) {
slist_itr_next(&_this->listener);
plsnr = slist_itr_remove(&_this->listener);
L2TPD_ASSERT(plsnr != NULL);
L2TPD_ASSERT(plsnr->sock == -1);
free(plsnr);
}
}
L2TPD_ASSERT(slist_length(&_this->listener) == idx);
if (slist_length(&_this->listener) != idx) {
l2tpd_log(_this, LOG_ERR,
"Invalid argument error on %s(): idx must be %d but %d",
__func__, slist_length(&_this->listener), idx);
goto fail;
}
if ((plistener = malloc(sizeof(l2tpd_listener))) == NULL) {
l2tpd_log(_this, LOG_ERR, "malloc() failed in %s: %m",
__func__);
goto fail;
}
memset(plistener, 0, sizeof(l2tpd_listener));
L2TPD_ASSERT(sizeof(plistener->bind) >= addr->sa_len);
memcpy(&plistener->bind, addr, addr->sa_len);
if (plistener->bind.sin6.sin6_port == 0)
plistener->bind.sin6.sin6_port = htons(L2TPD_DEFAULT_UDP_PORT);
plistener->sock = -1;
plistener->self = _this;
plistener->index = idx;
plistener->conf = conf;
strlcpy(plistener->tun_name, conf->name, sizeof(plistener->tun_name));
if (slist_add(&_this->listener, plistener) == NULL) {
l2tpd_log(_this, LOG_ERR, "slist_add() failed in %s: %m",
__func__);
goto fail;
}
return 0;
fail:
if (plistener != NULL)
free(plistener);
return 1;
}
/* finalize L2TP daemon instance */
void
l2tpd_uninit(l2tpd *_this)
{
l2tpd_listener *plsnr;
L2TPD_ASSERT(_this != NULL);
slist_fini(&_this->free_session_id_list);
if (_this->ctrl_map != NULL) {
hash_free(_this->ctrl_map);
_this->ctrl_map = NULL;
}
slist_itr_first(&_this->listener);
while (slist_itr_has_next(&_this->listener)) {
plsnr = slist_itr_next(&_this->listener);
L2TPD_ASSERT(plsnr != NULL);
L2TPD_ASSERT(plsnr->sock == -1);
free(plsnr);
}
slist_fini(&_this->listener);
event_del(&_this->ev_timeout); /* just in case */
_this->state = L2TPD_STATE_STOPPED;
}
/** assign the call to the l2tpd */
int
l2tpd_assign_call(l2tpd *_this, l2tp_call *call)
{
int shuffle_cnt;
u_int session_id;
shuffle_cnt = 0;
do {
session_id = (uintptr_t)slist_remove_first(
&_this->free_session_id_list);
if (session_id != L2TP_SESSION_ID_SHUFFLE_MARK)
break;
L2TPD_ASSERT(shuffle_cnt == 0);
if (shuffle_cnt++ > 0) {
l2tpd_log(_this, LOG_ERR,
"unexpected errror in %s(): free_session_id_list "
"full", __func__);
slist_add(&_this->free_session_id_list,
(void *)L2TP_SESSION_ID_SHUFFLE_MARK);
return 1;
}
slist_shuffle(&_this->free_session_id_list);
slist_add(&_this->free_session_id_list,
(void *)L2TP_SESSION_ID_SHUFFLE_MARK);
} while (1);
call->id = session_id;
return 0;
}
/* this function will be called when the call is released */
void
l2tpd_release_call(l2tpd *_this, l2tp_call *call)
{
slist_add(&_this->free_session_id_list, (void *)(uintptr_t)call->id);
}
/* start l2tpd listner */
static int
l2tpd_listener_start(l2tpd_listener *_this)
{
l2tpd *_l2tpd;
int af, lvl, opt, sock, ival;
char hbuf[NI_MAXHOST + NI_MAXSERV + 16];
_l2tpd = _this->self;
sock = -1;
af = _this->bind.sin6.sin6_family;
lvl = (af == AF_INET)? IPPROTO_IP : IPPROTO_IPV6;
if (_this->tun_name[0] == '\0')
strlcpy(_this->tun_name, L2TPD_DEFAULT_LAYER2_LABEL,
sizeof(_this->tun_name));
if ((sock = socket(_this->bind.sin6.sin6_family,
SOCK_DGRAM, IPPROTO_UDP)) < 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"socket() failed in %s(): %m", __func__);
goto fail;
}
#if defined(IP_STRICT_RCVIF) && defined(USE_STRICT_RCVIF)
ival = 1;
if (setsockopt(sock, IPPROTO_IP, IP_STRICT_RCVIF, &ival, sizeof(ival))
!= 0)
l2tpd_log(_l2tpd, LOG_WARNING,
"%s(): setsockopt(IP_STRICT_RCVIF) failed: %m", __func__);
#endif
if ((ival = fcntl(sock, F_GETFL, 0)) < 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"fcntl(,F_GETFL) failed in %s(): %m", __func__);
goto fail;
} else if (fcntl(sock, F_SETFL, ival | O_NONBLOCK) < 0) {
l2tpd_log(_l2tpd, LOG_ERR, "fcntl(,F_SETFL,O_NONBLOCK) failed "
"in %s(): %m", __func__);
goto fail;
}
ival = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &ival, sizeof(ival))
!= 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"setsockopt(,,SO_REUSEPORT) failed in %s(): %m", __func__);
goto fail;
}
if (bind(sock, (struct sockaddr *)&_this->bind,
_this->bind.sin6.sin6_len) != 0) {
l2tpd_log(_l2tpd, LOG_ERR, "Binding %s/udp: %m",
addrport_tostring((struct sockaddr *)&_this->bind,
_this->bind.sin6.sin6_len, hbuf, sizeof(hbuf)));
goto fail;
}
#ifdef USE_LIBSOCKUTIL
if (setsockoptfromto(sock) != 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"setsockoptfromto() failed in %s(): %m", __func__);
goto fail;
}
#else
opt = (af == AF_INET)? IP_RECVDSTADDR : IPV6_RECVPKTINFO;
ival = 1;
if (setsockopt(sock, lvl, opt, &ival, sizeof(ival)) != 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"setsockopt(,,IP{,V6}_RECVDSTADDR) failed in %s(): %m",
__func__);
goto fail;
}
#endif
#ifdef USE_SA_COOKIE
if (af == AF_INET) {
ival = 1;
if (setsockopt(sock, IPPROTO_IP, IP_IPSECFLOWINFO, &ival,
sizeof(ival)) != 0) {
l2tpd_log(_l2tpd, LOG_ERR,
"setsockopt(,,IP_IPSECFLOWINFO) failed in %s(): %m",
__func__);
goto fail;
}
}
#endif
#ifdef IP_PIPEX
opt = (af == AF_INET)? IP_PIPEX : IPV6_PIPEX;
ival = 1;
if (setsockopt(sock, lvl, opt, &ival, sizeof(ival)) != 0)
l2tpd_log(_l2tpd, LOG_WARNING,
"%s(): setsockopt(IP{,V6}_PIPEX) failed: %m", __func__);
#endif
if (_this->conf->require_ipsec) {
#ifdef IP_IPSEC_POLICY
caddr_t ipsec_policy_in, ipsec_policy_out;
opt = (af == AF_INET)? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
/*
* Note: ipsec_set_policy() will assign the buffer for
* yacc parser stack, however it never free.
* it cause memory leak (-2000byte).
*/
if ((ipsec_policy_in = ipsec_set_policy(L2TPD_IPSEC_POLICY_IN,
strlen(L2TPD_IPSEC_POLICY_IN))) == NULL) {
l2tpd_log(_l2tpd, LOG_ERR,
"ipsec_set_policy(L2TPD_IPSEC_POLICY_IN) failed "
"at %s(): %s: %m", __func__, ipsec_strerror());
} else if (setsockopt(sock, lvl, opt, ipsec_policy_in,
ipsec_get_policylen(ipsec_policy_in)) < 0) {
l2tpd_log(_l2tpd, LOG_WARNING,
"setsockopt(,,IP_IPSEC_POLICY(in)) failed "
"in %s(): %m", __func__);
}
if ((ipsec_policy_out = ipsec_set_policy(L2TPD_IPSEC_POLICY_OUT,
strlen(L2TPD_IPSEC_POLICY_OUT))) == NULL) {
l2tpd_log(_l2tpd, LOG_ERR,
"ipsec_set_policy(L2TPD_IPSEC_POLICY_OUT) failed "
"at %s(): %s: %m", __func__, ipsec_strerror());
}
if (ipsec_policy_out != NULL &&
setsockopt(sock, lvl, opt, ipsec_policy_out,
ipsec_get_policylen(ipsec_policy_out)) < 0) {
l2tpd_log(_l2tpd, LOG_WARNING,
"setsockopt(,,IP_IPSEC_POLICY(out)) failed "
"in %s(): %m", __func__);
}
if (ipsec_policy_in != NULL)
free(ipsec_policy_in);
if (ipsec_policy_out != NULL)
free(ipsec_policy_out);
#elif defined(IP_ESP_TRANS_LEVEL)
opt = (af == AF_INET)
? IP_ESP_TRANS_LEVEL : IPV6_ESP_TRANS_LEVEL;
ival = IPSEC_LEVEL_REQUIRE;
if (setsockopt(sock, lvl, opt, &ival, sizeof(ival)) != 0) {
l2tpd_log(_l2tpd, LOG_WARNING,
"setsockopt(,,IP{,V6}_ESP_TRANS_LEVEL(out)) failed "
"in %s(): %m", __func__);
}
#else
#error IP_IPSEC_POLICY or IP_ESP_TRANS_LEVEL must be usable.
#endif
}
_this->sock = sock;
event_set(&_this->ev_sock, _this->sock, EV_READ | EV_PERSIST,
l2tpd_io_event, _this);
event_add(&_this->ev_sock, NULL);
l2tpd_log(_l2tpd, LOG_INFO, "Listening %s/udp (L2TP LNS) [%s]",
addrport_tostring((struct sockaddr *)&_this->bind,
_this->bind.sin6.sin6_len, hbuf, sizeof(hbuf)), _this->tun_name);
return 0;
fail:
if (sock >= 0)
close(sock);
return 1;
}
/* start L2TP daemon */
int
l2tpd_start(l2tpd *_this)
{
int rval;
l2tpd_listener *plsnr;
rval = 0;
L2TPD_ASSERT(_this->state == L2TPD_STATE_INIT);
if (_this->state != L2TPD_STATE_INIT) {
l2tpd_log(_this, LOG_ERR, "Failed to start l2tpd: illegal "
"state.");
return -1;
}
slist_itr_first(&_this->listener);
while (slist_itr_has_next(&_this->listener)) {
plsnr = slist_itr_next(&_this->listener);
rval |= l2tpd_listener_start(plsnr);
}
if (rval == 0)
_this->state = L2TPD_STATE_RUNNING;
return rval;
}
/* stop l2tp lisnter */
static void
l2tpd_listener_stop(l2tpd_listener *_this)
{
char hbuf[NI_MAXHOST + NI_MAXSERV + 16];
if (_this->sock >= 0) {
event_del(&_this->ev_sock);
close(_this->sock);
l2tpd_log(_this->self, LOG_INFO,
"Shutdown %s/udp (L2TP LNS)",
addrport_tostring((struct sockaddr *)&_this->bind,
_this->bind.sin6.sin6_len, hbuf, sizeof(hbuf)));
_this->sock = -1;
}
}
/* stop immediattly without disconnect operation */
void
l2tpd_stop_immediatly(l2tpd *_this)
{
l2tpd_listener *plsnr;
slist_itr_first(&_this->listener);
while (slist_itr_has_next(&_this->listener)) {
plsnr = slist_itr_next(&_this->listener);
l2tpd_listener_stop(plsnr);
}
event_del(&_this->ev_timeout); /* XXX */
_this->state = L2TPD_STATE_STOPPED;
}
/*
* this function will be called when {@link ::_l2tp_ctrl control}
* is terminated.
*/
void
l2tpd_ctrl_finished_notify(l2tpd *_this)
{
if (_this->state != L2TPD_STATE_SHUTTING_DOWN)
return;
if (hash_first(_this->ctrl_map) != NULL)
return;
l2tpd_stop_immediatly(_this);
}
static void
l2tpd_stop_timeout(int fd, short evtype, void *ctx)
{
hash_link *hl;
l2tp_ctrl *ctrl;
l2tpd *_this;
_this = ctx;
l2tpd_log(_this, LOG_INFO, "Shutdown timeout");
for (hl = hash_first(_this->ctrl_map); hl != NULL;
hl = hash_next(_this->ctrl_map)) {
ctrl = hl->item;
l2tp_ctrl_stop(ctrl, 0);
}
l2tpd_stop_immediatly(_this);
}
/* stop L2TP daemon */
void
l2tpd_stop(l2tpd *_this)
{
int nctrls = 0;
hash_link *hl;
l2tp_ctrl *ctrl;
nctrls = 0;
event_del(&_this->ev_timeout);
if (l2tpd_is_stopped(_this))
return;
if (l2tpd_is_shutting_down(_this)) {
/* terminate immediately, when 2nd call */
l2tpd_stop_immediatly(_this);
return;
}
for (hl = hash_first(_this->ctrl_map); hl != NULL;
hl = hash_next(_this->ctrl_map)) {
ctrl = hl->item;
l2tp_ctrl_stop(ctrl, L2TP_STOP_CCN_RCODE_SHUTTING_DOWN);
nctrls++;
}
_this->state = L2TPD_STATE_SHUTTING_DOWN;
if (nctrls > 0) {
struct timeval tv0;
tv0.tv_usec = 0;
tv0.tv_sec = L2TPD_SHUTDOWN_TIMEOUT;
evtimer_set(&_this->ev_timeout, l2tpd_stop_timeout, _this);
evtimer_add(&_this->ev_timeout, &tv0);
return;
}
l2tpd_stop_immediatly(_this);
}
/*
* Configuration
*/
int
l2tpd_reload(l2tpd *_this, struct l2tp_confs *l2tp_conf)
{
int i;
struct l2tp_conf *conf;
l2tpd_listener *listener;
struct l2tp_listen_addr *addr;
if (slist_length(&_this->listener) > 0) {
/*
* TODO: add / remove / restart listener.
*/
slist_itr_first(&_this->listener);
while (slist_itr_has_next(&_this->listener)) {
listener = slist_itr_next(&_this->listener);
TAILQ_FOREACH(conf, l2tp_conf, entry) {
if (strcmp(listener->tun_name,
conf->name) == 0) {
listener->conf = conf;
break;
}
}
}
return 0;
}
i = 0;
TAILQ_FOREACH(conf, l2tp_conf, entry) {
TAILQ_FOREACH(addr, &conf->listen, entry)
l2tpd_add_listener(_this, i++, conf,
(struct sockaddr *)&addr->addr);
}
if (l2tpd_start(_this) != 0)
return -1;
return 0;
}
/*
* I/O functions
*/
/* logging when deny an access */
void
l2tpd_log_access_deny(l2tpd *_this, const char *reason, struct sockaddr *peer)
{
char buf[BUFSIZ];
l2tpd_log(_this, LOG_ALERT, "Received packet from %s/udp: "
"%s", addrport_tostring(peer, peer->sa_len, buf, sizeof(buf)),
reason);
}
/* I/O event handler */
static void
l2tpd_io_event(int fd, short evtype, void *ctx)
{
int sz;
l2tpd *_l2tpd;
l2tpd_listener *_this;
socklen_t peerlen, socklen;
struct sockaddr_storage peer, sock;
u_char buf[8192];
void *nat_t;
_this = ctx;
_l2tpd = _this->self;
if ((evtype & EV_READ) != 0) {
peerlen = sizeof(peer);
socklen = sizeof(sock);
while (!l2tpd_is_stopped(_l2tpd)) {
#if defined(USE_LIBSOCKUTIL) || defined(USE_SA_COOKIE)
int sa_cookie_len;
struct in_ipsec_sa_cookie sa_cookie;
sa_cookie_len = sizeof(sa_cookie);
if ((sz = recvfromto_nat_t(_this->sock, buf,
sizeof(buf), 0,
(struct sockaddr *)&peer, &peerlen,
(struct sockaddr *)&sock, &socklen,
&sa_cookie, &sa_cookie_len)) <= 0) {
#else
if ((sz = recvfromto(_this->sock, buf,
sizeof(buf), 0,
(struct sockaddr *)&peer, &peerlen,
(struct sockaddr *)&sock, &socklen)) <= 0) {
#endif
if (errno == EAGAIN || errno == EINTR)
break;
l2tpd_log(_l2tpd, LOG_ERR,
"recvfrom() failed in %s(): %m",
__func__);
l2tpd_stop(_l2tpd);
return;
}
/* source address check (allows.in) */
switch (peer.ss_family) {
case AF_INET:
#if defined(USE_LIBSOCKUTIL) || defined(USE_SA_COOKIE)
if (sa_cookie_len > 0)
nat_t = &sa_cookie;
else
nat_t = NULL;
#else
nat_t = NULL;
#endif
l2tp_ctrl_input(_l2tpd, _this->index,
(struct sockaddr *)&peer,
(struct sockaddr *)&sock, nat_t,
buf, sz);
break;
case AF_INET6:
l2tp_ctrl_input(_l2tpd, _this->index,
(struct sockaddr *)&peer,
(struct sockaddr *)&sock, NULL,
buf, sz);
break;
default:
l2tpd_log(_l2tpd, LOG_ERR,
"received from unknown address family = %d",
peer.ss_family);
break;
}
}
}
}
/*
* L2TP control
*/
l2tp_ctrl *
l2tpd_get_ctrl(l2tpd *_this, unsigned tunid)
{
hash_link *hl;
hl = hash_lookup(_this->ctrl_map, (void *)(uintptr_t)tunid);
if (hl == NULL)
return NULL;
return hl->item;
}
void
l2tpd_add_ctrl(l2tpd *_this, l2tp_ctrl *ctrl)
{
hash_insert(_this->ctrl_map, (void *)(uintptr_t)ctrl->tunnel_id, ctrl);
}
void
l2tpd_remove_ctrl(l2tpd *_this, unsigned tunid)
{
hash_delete(_this->ctrl_map, (void *)(uintptr_t)tunid, 0);
}
/*
* misc
*/
void
l2tpd_log(l2tpd *_this, int prio, const char *fmt, ...)
{
char logbuf[BUFSIZ];
va_list ap;
va_start(ap, fmt);
#ifdef L2TPD_MULTIPLE
snprintf(logbuf, sizeof(logbuf), "l2tpd id=%u %s", _this->id, fmt);
#else
snprintf(logbuf, sizeof(logbuf), "l2tpd %s", fmt);
#endif
vlog_printf(prio, logbuf, ap);
va_end(ap);
}
|