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