Lines 22-27
Link Here
|
22 |
#include <unistd.h> |
22 |
#include <unistd.h> |
23 |
#include <stdlib.h> |
23 |
#include <stdlib.h> |
24 |
#include <sys/ioctl.h> |
24 |
#include <sys/ioctl.h> |
|
|
25 |
#include <sys/wait.h> |
25 |
#ifndef LINUX |
26 |
#ifndef LINUX |
26 |
# include <sys/uio.h> |
27 |
# include <sys/uio.h> |
27 |
#endif |
28 |
#endif |
Lines 36-41
int server_socket; /* Server socket */
Link Here
|
36 |
int kernel_support; /* Kernel Support there or not? */ |
37 |
int kernel_support; /* Kernel Support there or not? */ |
37 |
#endif |
38 |
#endif |
38 |
|
39 |
|
|
|
40 |
#ifdef USE_KERNEL |
41 |
void modprobe() { |
42 |
char * modules[] = { "l2tp_ppp", "pppol2tp", NULL }; |
43 |
char ** module; |
44 |
char buf[256], *tok; |
45 |
int pid, exit_status, fd; |
46 |
|
47 |
FILE * fmod = fopen("/proc/modules", "r"); |
48 |
|
49 |
if (fmod == NULL) |
50 |
return; |
51 |
|
52 |
while (fgets(buf, 255, fmod) != NULL) { |
53 |
if ((tok = strtok(buf, " ")) != NULL) { |
54 |
for (module = modules; *module != NULL; ++module) { |
55 |
if (!strcmp(*module, tok)) { |
56 |
fclose(fmod); |
57 |
return; |
58 |
} |
59 |
} |
60 |
} |
61 |
} |
62 |
|
63 |
fclose(fmod); |
64 |
|
65 |
for (module = modules; *module != NULL; ++module) { |
66 |
if ((pid = fork()) >= 0) { |
67 |
if (pid == 0) { |
68 |
setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); |
69 |
if ((fd = open("/dev/null", O_RDWR)) > -1) { |
70 |
dup2(fd, 1); |
71 |
dup2(fd, 2); |
72 |
} |
73 |
execlp("modprobe", "modprobe", "-q", *module, (char *)NULL); |
74 |
exit(1); |
75 |
} else { |
76 |
if ((pid = waitpid(pid, &exit_status, 0)) != -1 && WIFEXITED(exit_status)) { |
77 |
if (WEXITSTATUS(exit_status) == 0) |
78 |
return; |
79 |
} |
80 |
} |
81 |
} |
82 |
} |
83 |
} |
84 |
#endif |
39 |
|
85 |
|
40 |
int init_network (void) |
86 |
int init_network (void) |
41 |
{ |
87 |
{ |
Lines 45-50
int init_network (void)
Link Here
|
45 |
server.sin_family = AF_INET; |
91 |
server.sin_family = AF_INET; |
46 |
server.sin_addr.s_addr = gconfig.listenaddr; |
92 |
server.sin_addr.s_addr = gconfig.listenaddr; |
47 |
server.sin_port = htons (gconfig.port); |
93 |
server.sin_port = htons (gconfig.port); |
|
|
94 |
int flags; |
48 |
if ((server_socket = socket (PF_INET, SOCK_DGRAM, 0)) < 0) |
95 |
if ((server_socket = socket (PF_INET, SOCK_DGRAM, 0)) < 0) |
49 |
{ |
96 |
{ |
50 |
l2tp_log (LOG_CRIT, "%s: Unable to allocate socket. Terminating.\n", |
97 |
l2tp_log (LOG_CRIT, "%s: Unable to allocate socket. Terminating.\n", |
Lines 52-57
int init_network (void)
Link Here
|
52 |
return -EINVAL; |
99 |
return -EINVAL; |
53 |
}; |
100 |
}; |
54 |
|
101 |
|
|
|
102 |
flags = 1; |
103 |
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)); |
104 |
setsockopt(server_socket, SOL_SOCKET, SO_NO_CHECK, &flags, sizeof(flags)); |
105 |
|
55 |
if (bind (server_socket, (struct sockaddr *) &server, sizeof (server))) |
106 |
if (bind (server_socket, (struct sockaddr *) &server, sizeof (server))) |
56 |
{ |
107 |
{ |
57 |
close (server_socket); |
108 |
close (server_socket); |
Lines 91-96
int init_network (void)
Link Here
|
91 |
} |
142 |
} |
92 |
else |
143 |
else |
93 |
{ |
144 |
{ |
|
|
145 |
modprobe(); |
94 |
int kernel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); |
146 |
int kernel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); |
95 |
if (kernel_fd < 0) |
147 |
if (kernel_fd < 0) |
96 |
{ |
148 |
{ |
Lines 321-326
int build_fdset (fd_set *readfds)
Link Here
|
321 |
|
373 |
|
322 |
while (tun) |
374 |
while (tun) |
323 |
{ |
375 |
{ |
|
|
376 |
if (tun->udp_fd > -1) { |
377 |
if (tun->udp_fd > max) |
378 |
max = tun->udp_fd; |
379 |
FD_SET (tun->udp_fd, readfds); |
380 |
} |
324 |
call = tun->call_head; |
381 |
call = tun->call_head; |
325 |
while (call) |
382 |
while (call) |
326 |
{ |
383 |
{ |
Lines 390-395
void network_thread ()
Link Here
|
390 |
struct iovec iov; |
447 |
struct iovec iov; |
391 |
char cbuf[256]; |
448 |
char cbuf[256]; |
392 |
unsigned int refme, refhim; |
449 |
unsigned int refme, refhim; |
|
|
450 |
int * currentfd; |
451 |
int server_socket_processed; |
393 |
|
452 |
|
394 |
/* This one buffer can be recycled for everything except control packets */ |
453 |
/* This one buffer can be recycled for everything except control packets */ |
395 |
buf = new_buf (MAX_RECV_SIZE); |
454 |
buf = new_buf (MAX_RECV_SIZE); |
Lines 428-434
void network_thread ()
Link Here
|
428 |
{ |
487 |
{ |
429 |
do_control (); |
488 |
do_control (); |
430 |
} |
489 |
} |
431 |
if (FD_ISSET (server_socket, &readfds)) |
490 |
server_socket_processed = 0; |
|
|
491 |
currentfd = NULL; |
492 |
st = tunnels.head; |
493 |
while (st || !server_socket_processed) { |
494 |
if (st && (st->udp_fd == -1)) { |
495 |
st=st->next; |
496 |
continue; |
497 |
} |
498 |
if (st) { |
499 |
currentfd = &st->udp_fd; |
500 |
} else { |
501 |
currentfd = &server_socket; |
502 |
server_socket_processed = 1; |
503 |
} |
504 |
if (FD_ISSET (*currentfd, &readfds)) |
432 |
{ |
505 |
{ |
433 |
/* |
506 |
/* |
434 |
* Okay, now we're ready for reading and processing new data. |
507 |
* Okay, now we're ready for reading and processing new data. |
Lines 457-468
void network_thread ()
Link Here
|
457 |
msgh.msg_flags = 0; |
530 |
msgh.msg_flags = 0; |
458 |
|
531 |
|
459 |
/* Receive one packet. */ |
532 |
/* Receive one packet. */ |
460 |
recvsize = recvmsg(server_socket, &msgh, 0); |
533 |
recvsize = recvmsg(*currentfd, &msgh, 0); |
461 |
|
534 |
|
462 |
if (recvsize < MIN_PAYLOAD_HDR_LEN) |
535 |
if (recvsize < MIN_PAYLOAD_HDR_LEN) |
463 |
{ |
536 |
{ |
464 |
if (recvsize < 0) |
537 |
if (recvsize < 0) |
465 |
{ |
538 |
{ |
|
|
539 |
if (errno == ECONNREFUSED) { |
540 |
close(*currentfd); |
541 |
} |
542 |
if ((errno == ECONNREFUSED) || |
543 |
(errno == EBADF)) { |
544 |
*currentfd = -1; |
545 |
} |
466 |
if (errno != EAGAIN) |
546 |
if (errno != EAGAIN) |
467 |
l2tp_log (LOG_WARNING, |
547 |
l2tp_log (LOG_WARNING, |
468 |
"%s: recvfrom returned error %d (%s)\n", |
548 |
"%s: recvfrom returned error %d (%s)\n", |
Lines 567-572
void network_thread ()
Link Here
|
567 |
} |
647 |
} |
568 |
}; |
648 |
}; |
569 |
} |
649 |
} |
|
|
650 |
if (st) st=st->next; |
651 |
} |
570 |
|
652 |
|
571 |
/* |
653 |
/* |
572 |
* finished obvious sources, look for data from PPP connections. |
654 |
* finished obvious sources, look for data from PPP connections. |
Lines 639-641
void network_thread ()
Link Here
|
639 |
} |
721 |
} |
640 |
|
722 |
|
641 |
} |
723 |
} |
|
|
724 |
|
725 |
int connect_pppol2tp(struct tunnel *t) { |
726 |
#ifdef USE_KERNEL |
727 |
if (kernel_support) { |
728 |
int ufd = -1, fd2 = -1; |
729 |
int flags; |
730 |
struct sockaddr_pppol2tp sax; |
731 |
|
732 |
struct sockaddr_in server; |
733 |
server.sin_family = AF_INET; |
734 |
server.sin_addr.s_addr = gconfig.listenaddr; |
735 |
server.sin_port = htons (gconfig.port); |
736 |
if ((ufd = socket (PF_INET, SOCK_DGRAM, 0)) < 0) |
737 |
{ |
738 |
l2tp_log (LOG_CRIT, "%s: Unable to allocate UDP socket. Terminating.\n", |
739 |
__FUNCTION__); |
740 |
return -EINVAL; |
741 |
}; |
742 |
|
743 |
flags=1; |
744 |
setsockopt(ufd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)); |
745 |
setsockopt(ufd, SOL_SOCKET, SO_NO_CHECK, &flags, sizeof(flags)); |
746 |
|
747 |
if (bind (ufd, (struct sockaddr *) &server, sizeof (server))) |
748 |
{ |
749 |
close (ufd); |
750 |
l2tp_log (LOG_CRIT, "%s: Unable to bind UDP socket: %s. Terminating.\n", |
751 |
__FUNCTION__, strerror(errno), errno); |
752 |
return -EINVAL; |
753 |
}; |
754 |
server = t->peer; |
755 |
flags = fcntl(ufd, F_GETFL); |
756 |
if (flags == -1 || fcntl(ufd, F_SETFL, flags | O_NONBLOCK) == -1) { |
757 |
l2tp_log (LOG_WARNING, "%s: Unable to set UDP socket nonblock.\n", |
758 |
__FUNCTION__); |
759 |
return -EINVAL; |
760 |
} |
761 |
if (connect (ufd, (struct sockaddr *) &server, sizeof(server)) < 0) { |
762 |
l2tp_log (LOG_CRIT, "%s: Unable to connect UDP peer. Terminating.\n", |
763 |
__FUNCTION__); |
764 |
return -EINVAL; |
765 |
} |
766 |
|
767 |
t->udp_fd=ufd; |
768 |
|
769 |
fd2 = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); |
770 |
if (fd2 < 0) { |
771 |
l2tp_log (LOG_WARNING, "%s: Unable to allocate PPPoL2TP socket.\n", |
772 |
__FUNCTION__); |
773 |
return -EINVAL; |
774 |
} |
775 |
flags = fcntl(fd2, F_GETFL); |
776 |
if (flags == -1 || fcntl(fd2, F_SETFL, flags | O_NONBLOCK) == -1) { |
777 |
l2tp_log (LOG_WARNING, "%s: Unable to set PPPoL2TP socket nonblock.\n", |
778 |
__FUNCTION__); |
779 |
return -EINVAL; |
780 |
} |
781 |
sax.sa_family = AF_PPPOX; |
782 |
sax.sa_protocol = PX_PROTO_OL2TP; |
783 |
sax.pppol2tp.pid = 0; |
784 |
sax.pppol2tp.fd = t->udp_fd; |
785 |
sax.pppol2tp.addr.sin_addr.s_addr = t->peer.sin_addr.s_addr; |
786 |
sax.pppol2tp.addr.sin_port = t->peer.sin_port; |
787 |
sax.pppol2tp.addr.sin_family = AF_INET; |
788 |
sax.pppol2tp.s_tunnel = t->ourtid; |
789 |
sax.pppol2tp.s_session = 0; |
790 |
sax.pppol2tp.d_tunnel = t->tid; |
791 |
sax.pppol2tp.d_session = 0; |
792 |
if ((connect(fd2, (struct sockaddr *)&sax, sizeof(sax))) < 0) { |
793 |
l2tp_log (LOG_WARNING, "%s: Unable to connect PPPoL2TP socket. %d %s\n", |
794 |
__FUNCTION__, errno, strerror(errno)); |
795 |
close(fd2); |
796 |
return -EINVAL; |
797 |
} |
798 |
t->pppox_fd = fd2; |
799 |
} |
800 |
#endif |
801 |
return 0; |
802 |
} |