Adding the possibility to listening multiple IP L2TP tunnels.
[l2tpns.git] / util.c
1 /* Misc util functions */
2
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sched.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <string.h>
10 #include <sys/mman.h>
11
12 #include "l2tpns.h"
13 #ifdef BGP
14 #include "bgp.h"
15 #endif
16
17 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
18 // to use
19 char *fmtaddr(in_addr_t addr, int n)
20 {
21 static char addrs[4][16];
22 struct in_addr in;
23
24 if (n < 0 || n >= 4)
25 return "";
26
27 in.s_addr = addr;
28 return strcpy(addrs[n], inet_ntoa(in));
29 }
30
31 char *fmtMacAddr(uint8_t *pMacAddr)
32 {
33 static char strMAC[2*ETH_ALEN];
34
35 sprintf(strMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
36 pMacAddr[0], pMacAddr[1], pMacAddr[2],
37 pMacAddr[3], pMacAddr[4], pMacAddr[5]);
38
39 return strMAC;
40 }
41
42 void *shared_malloc(unsigned int size)
43 {
44 void * p;
45 p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
46
47 if (p == MAP_FAILED)
48 p = NULL;
49
50 return p;
51 }
52
53 extern int forked;
54 extern int cluster_sockfd, tunfd, controlfd, daefd, snoopfd, ifrfd, ifr6fd, rand_fd;
55 extern int pppoediscfd, pppoesessfd;
56 extern int *radfds;
57 extern int udpfd[MAX_UDPFD + 1];
58
59 pid_t fork_and_close()
60 {
61 pid_t pid = fork();
62 int i;
63
64 if (pid)
65 return pid;
66
67 forked++;
68 if (config->scheduler_fifo)
69 {
70 struct sched_param params = {0};
71 params.sched_priority = 0;
72 if (sched_setscheduler(0, SCHED_OTHER, &params))
73 {
74 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno));
75 LOG(0, 0, 0, "This is probably really really bad.\n");
76 }
77 }
78
79 signal(SIGPIPE, SIG_DFL);
80 signal(SIGCHLD, SIG_DFL);
81 signal(SIGHUP, SIG_DFL);
82 signal(SIGUSR1, SIG_DFL);
83 signal(SIGQUIT, SIG_DFL);
84 signal(SIGKILL, SIG_DFL);
85 signal(SIGTERM, SIG_DFL);
86
87 // Close sockets
88 if (clifd != -1) close(clifd);
89 if (cluster_sockfd != -1) close(cluster_sockfd);
90 if (tunfd != -1) close(tunfd);
91
92 for (i = 0; i < config->nbudpfd; i++)
93 {
94 if (udpfd[i] != -1) close(udpfd[i]);
95 }
96
97 if (pppoediscfd != -1) close(pppoediscfd);
98 if (pppoediscfd != -1) close(pppoediscfd);
99 if (controlfd != -1) close(controlfd);
100 if (daefd != -1) close(daefd);
101 if (snoopfd != -1) close(snoopfd);
102 if (rand_fd != -1) close(rand_fd);
103 if (epollfd != -1) close(epollfd);
104
105 for (i = 0; radfds && i < RADIUS_FDS; i++)
106 close(radfds[i]);
107
108 #ifdef BGP
109 for (i = 0; i < BGP_NUM_PEERS; i++)
110 if (bgp_peers[i].sock != -1)
111 close(bgp_peers[i].sock);
112 #endif /* BGP */
113
114 return pid;
115 }
116
117 ssize_t recvfromto(int s, void *buf, size_t len, int flags,
118 struct sockaddr *from, socklen_t *fromlen, struct in_addr *toaddr)
119 {
120 ssize_t r;
121 struct msghdr msg;
122 struct cmsghdr *cmsg;
123 struct iovec vec;
124 char cbuf[128];
125
126 memset(&msg, 0, sizeof(msg));
127 msg.msg_name = from;
128 msg.msg_namelen = *fromlen;
129
130 vec.iov_base = buf;
131 vec.iov_len = len;
132 msg.msg_iov = &vec;
133 msg.msg_iovlen = 1;
134 msg.msg_flags = 0;
135
136 msg.msg_control = cbuf;
137 msg.msg_controllen = sizeof(cbuf);
138
139 if ((r = recvmsg(s, &msg, flags)) < 0)
140 return r;
141
142 if (fromlen)
143 *fromlen = msg.msg_namelen;
144
145 memset(toaddr, 0, sizeof(*toaddr));
146 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
147 {
148 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO)
149 {
150 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
151 memcpy(toaddr, &i->ipi_addr, sizeof(*toaddr));
152 break;
153 }
154 }
155
156 return r;
157 }
158
159 ssize_t sendtofrom(int s, void const *buf, size_t len, int flags,
160 struct sockaddr const *to, socklen_t tolen, struct in_addr const *from)
161 {
162 struct msghdr msg;
163 struct cmsghdr *cmsg;
164 struct iovec vec;
165 struct in_pktinfo pktinfo;
166 char cbuf[CMSG_SPACE(sizeof(pktinfo))];
167
168 memset(&msg, 0, sizeof(msg));
169 msg.msg_name = (struct sockaddr *) to;
170 msg.msg_namelen = tolen;
171
172 vec.iov_base = (void *) buf;
173 vec.iov_len = len;
174 msg.msg_iov = &vec;
175 msg.msg_iovlen = 1;
176 msg.msg_flags = 0;
177
178 msg.msg_control = cbuf;
179 msg.msg_controllen = sizeof(cbuf);
180
181 cmsg = CMSG_FIRSTHDR(&msg);
182 cmsg->cmsg_level = SOL_IP;
183 cmsg->cmsg_type = IP_PKTINFO;
184 cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
185
186 memset(&pktinfo, 0, sizeof(pktinfo));
187 memcpy(&pktinfo.ipi_spec_dst, from, sizeof(*from));
188 memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
189
190 return sendmsg(s, &msg, flags);
191 }