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