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