memcmp: Introduce and use consttime_memeq
[secnet] / udp.c
CommitLineData
2fe58dfd
SE
1/* UDP send/receive module for secnet */
2
3/* This module enables sites to communicate by sending UDP
4 * packets. When an instance of the module is created we can
5 * optionally bind to a particular local IP address (not implemented
6 * yet).
7 *
2fe58dfd
SE
8 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
10
8689b3a9 11#include "secnet.h"
2fe58dfd
SE
12#include <stdio.h>
13#include <unistd.h>
14#include <fcntl.h>
15#include <string.h>
16#include <errno.h>
2fe58dfd 17#include <sys/socket.h>
b2a56f7c 18#include <sys/wait.h>
5edf478f
IJ
19#include <netinet/in.h>
20#include <arpa/inet.h>
2fe58dfd 21#include "util.h"
794f2398 22#include "unaligned.h"
ff05a229 23#include "ipaddr.h"
2fe58dfd
SE
24
25static beforepoll_fn udp_beforepoll;
26static afterpoll_fn udp_afterpoll;
27static comm_request_notify_fn request_notify;
28static comm_release_notify_fn release_notify;
29static comm_sendmsg_fn udp_sendmsg;
30
2fe58dfd
SE
31struct notify_list {
32 comm_notify_fn *fn;
33 void *state;
34 struct notify_list *next;
35};
36
37struct udp {
38 closure_t cl;
39 struct comm_if ops;
40 struct cloc loc;
3b83c932 41 uint32_t addr;
baa06aeb 42 uint16_t port;
2fe58dfd 43 int fd;
b2a56f7c 44 string_t authbind;
2fe58dfd
SE
45 struct buffer_if *rbuf;
46 struct notify_list *notify;
ff05a229
SE
47 bool_t use_proxy;
48 struct sockaddr_in proxy;
2fe58dfd
SE
49};
50
5edf478f
IJ
51static const char *saddr_to_string(const struct sockaddr_in *sin) {
52 static char bufs[2][100];
53 static int b;
54
55 b ^= 1;
56 snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d",
57 inet_ntoa(sin->sin_addr),
58 ntohs(sin->sin_port));
59 return bufs[b];
60}
61
62static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
63 struct udp *st=commst;
64 static char sbuf[100];
65
66 struct sockaddr_in la;
67 la.sin_addr.s_addr=htonl(st->addr);
68 la.sin_port=htons(st->port);
69
70 snprintf(sbuf, sizeof(sbuf), "udp:%s-%s",
71 saddr_to_string(&la), saddr_to_string(&ca->sin));
72 return sbuf;
73}
74
2fe58dfd 75static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
90a39563 76 int *timeout_io)
2fe58dfd
SE
77{
78 struct udp *st=state;
79 if (*nfds_io<1) {
80 *nfds_io=1;
81 return ERANGE;
82 }
83 *nfds_io=1;
84 fds->fd=st->fd;
85 fds->events=POLLIN;
86 return 0;
87}
88
90a39563 89static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
2fe58dfd
SE
90{
91 struct udp *st=state;
92 struct sockaddr_in from;
b1a0f651 93 socklen_t fromlen;
2fe58dfd
SE
94 struct notify_list *n;
95 bool_t done;
96 int rv;
97
98 if (nfds && (fds->revents & POLLIN)) {
99 do {
a15faeb2 100 FILLZERO(from);
2fe58dfd
SE
101 fromlen=sizeof(from);
102 BUF_ASSERT_FREE(st->rbuf);
103 BUF_ALLOC(st->rbuf,"udp_afterpoll");
104 rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
105 (struct sockaddr *)&from, &fromlen);
106 if (rv>0) {
107 st->rbuf->size=rv;
ff05a229
SE
108 if (st->use_proxy) {
109 /* Check that the packet came from our poxy server;
110 we shouldn't be contacted directly by anybody else
111 (since they can trivially forge source addresses) */
112 if (memcmp(&from.sin_addr,&st->proxy.sin_addr,4)!=0 ||
113 memcmp(&from.sin_port,&st->proxy.sin_port,2)!=0) {
114 Message(M_INFO,"udp: received packet that's not "
115 "from the proxy\n");
116 BUF_FREE(st->rbuf);
117 continue;
118 }
119 memcpy(&from.sin_addr,buf_unprepend(st->rbuf,4),4);
120 buf_unprepend(st->rbuf,2);
121 memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2);
122 }
2fe58dfd
SE
123 done=False;
124 for (n=st->notify; n; n=n->next) {
a15faeb2
IJ
125 struct comm_addr ca;
126 FILLZERO(ca);
127 ca.comm=&st->ops;
128 ca.sin=from;
129 if (n->fn(n->state, st->rbuf, &ca)) {
2fe58dfd
SE
130 done=True;
131 break;
132 }
133 }
134 if (!done) {
794f2398 135 uint32_t source,dest;
ff05a229 136 /* Manufacture and send NAK packet */
794f2398
SE
137 source=get_uint32(st->rbuf->start); /* Us */
138 dest=get_uint32(st->rbuf->start+4); /* Them */
139 Message(M_INFO,"udp (port %d): sending NAK\n",st->port);
140 buffer_init(st->rbuf,0);
141 buf_append_uint32(st->rbuf,dest);
142 buf_append_uint32(st->rbuf,source);
143 buf_append_uint32(st->rbuf,0); /* NAK is msg type 0 */
144 sendto(st->fd, st->rbuf->start, st->rbuf->size, 0,
145 (struct sockaddr *)&from, sizeof(from));
2fe58dfd
SE
146 BUF_FREE(st->rbuf);
147 }
148 BUF_ASSERT_FREE(st->rbuf);
149 } else {
150 BUF_FREE(st->rbuf);
151 }
152 } while (rv>=0);
153 }
154}
155
156static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
157{
158 struct udp *st=commst;
159 struct notify_list *n;
160
161 n=safe_malloc(sizeof(*n),"request_notify");
162 n->fn=fn;
163 n->state=nst;
164 n->next=st->notify;
165 st->notify=n;
166}
167
168static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
169{
170 struct udp *st=commst;
171 struct notify_list *n, **p, *t;
172
173 /* XXX untested */
174 p=&st->notify;
175 for (n=st->notify; n; )
176 {
177 if (n->state==nst && n->fn==fn) {
178 t=n;
179 *p=n->next;
180 n=n->next;
181 free(t);
182 } else {
183 p=&n->next;
184 n=n->next;
185 }
186 }
187}
188
189static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
a15faeb2 190 const struct comm_addr *dest)
2fe58dfd
SE
191{
192 struct udp *st=commst;
ff05a229 193 uint8_t *sa;
2fe58dfd 194
ff05a229
SE
195 if (st->use_proxy) {
196 sa=buf->start-8;
a15faeb2 197 memcpy(sa,&dest->sin.sin_addr,4);
ff05a229 198 memset(sa+4,0,4);
a15faeb2 199 memcpy(sa+6,&dest->sin.sin_port,2);
ff05a229
SE
200 sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
201 sizeof(st->proxy));
202 } else {
203 sendto(st->fd, buf->start, buf->size, 0,
a15faeb2 204 (struct sockaddr *)&dest->sin, sizeof(dest->sin));
ff05a229 205 }
2fe58dfd
SE
206
207 return True;
208}
209
baa06aeb
SE
210static void udp_phase_hook(void *sst, uint32_t new_phase)
211{
212 struct udp *st=sst;
213 struct sockaddr_in addr;
214
3b83c932 215 st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
baa06aeb
SE
216 if (st->fd<0) {
217 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
218 }
219 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
220 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
221 st->loc.file,st->loc.line);
222 }
223 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
224 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
225 st->loc.file,st->loc.line);
226 }
227
076bb54e 228 FILLZERO(addr);
baa06aeb 229 addr.sin_family=AF_INET;
3b83c932 230 addr.sin_addr.s_addr=htonl(st->addr);
baa06aeb 231 addr.sin_port=htons(st->port);
b2a56f7c
SE
232 if (st->authbind) {
233 pid_t c;
234 int status;
235
236 /* XXX this fork() and waitpid() business needs to be hidden
237 in some system-specific library functions. */
238 c=fork();
239 if (c==-1) {
240 fatal_perror("udp_phase_hook: fork() for authbind");
241 }
242 if (c==0) {
3b83c932 243 char *argv[4], addrstr[9], portstr[5];
51b25953
IJ
244 sprintf(addrstr,"%08lX",(long)addr.sin_addr.s_addr);
245 sprintf(portstr,"%04X",addr.sin_port);
b2a56f7c 246 argv[0]=st->authbind;
3b83c932
SE
247 argv[1]=addrstr;
248 argv[2]=portstr;
b2a56f7c
SE
249 argv[3]=NULL;
250 dup2(st->fd,0);
251 execvp(st->authbind,argv);
3b83c932 252 _exit(255);
b2a56f7c 253 }
3b83c932
SE
254 while (waitpid(c,&status,0)==-1) {
255 if (errno==EINTR) continue;
b2a56f7c
SE
256 fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
257 }
3b83c932
SE
258 if (WIFSIGNALED(status)) {
259 fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
260 st->loc.line, WTERMSIG(status));
261 }
262 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
263 fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
264 st->loc.line, WEXITSTATUS(status));
265 }
b2a56f7c
SE
266 } else {
267 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
268 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
269 }
baa06aeb
SE
270 }
271
272 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
273}
274
2fe58dfd
SE
275static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
276 list_t *args)
277{
278 struct udp *st;
3b83c932 279 item_t *i,*j;
2fe58dfd 280 dict_t *d;
ff05a229
SE
281 list_t *l;
282 uint32_t a;
2fe58dfd
SE
283
284 st=safe_malloc(sizeof(*st),"udp_apply(st)");
285 st->loc=loc;
286 st->cl.description="udp";
287 st->cl.type=CL_COMM;
288 st->cl.apply=NULL;
289 st->cl.interface=&st->ops;
290 st->ops.st=st;
ff05a229
SE
291 st->ops.min_start_pad=0;
292 st->ops.min_end_pad=0;
2fe58dfd
SE
293 st->ops.request_notify=request_notify;
294 st->ops.release_notify=release_notify;
295 st->ops.sendmsg=udp_sendmsg;
5edf478f 296 st->ops.addr_to_string=addr_to_string;
baa06aeb 297 st->port=0;
ff05a229 298 st->use_proxy=False;
2fe58dfd
SE
299
300 i=list_elem(args,0);
301 if (!i || i->type!=t_dict) {
302 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
303 }
304 d=i->data.dict;
305
3b83c932 306 j=dict_find_item(d,"address",False,"udp",st->loc);
44128667 307 st->addr=j?string_item_to_ipaddr(j, "udp"):INADDR_ANY;
baa06aeb 308 st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
2fe58dfd 309 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
b2a56f7c 310 st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
ff05a229
SE
311 l=dict_lookup(d,"proxy");
312 if (l) {
313 st->use_proxy=True;
076bb54e 314 FILLZERO(st->proxy);
ff05a229
SE
315 st->proxy.sin_family=AF_INET;
316 i=list_elem(l,0);
317 if (!i || i->type!=t_string) {
318 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
319 }
320 a=string_item_to_ipaddr(i,"proxy");
321 st->proxy.sin_addr.s_addr=htonl(a);
322 i=list_elem(l,1);
323 if (!i || i->type!=t_number) {
324 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
325 }
326 st->proxy.sin_port=htons(i->data.number);
327 st->ops.min_start_pad=8;
328 }
2fe58dfd 329
baa06aeb 330 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
2fe58dfd
SE
331
332 return new_closure(&st->cl);
333}
334
2fe58dfd
SE
335void udp_module(dict_t *dict)
336{
337 add_closure(dict,"udp",udp_apply);
338}