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