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