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; | |
baa06aeb | 39 | uint16_t port; |
2fe58dfd | 40 | int fd; |
b2a56f7c | 41 | string_t authbind; |
2fe58dfd SE |
42 | struct buffer_if *rbuf; |
43 | struct notify_list *notify; | |
ff05a229 SE |
44 | bool_t use_proxy; |
45 | struct sockaddr_in proxy; | |
2fe58dfd SE |
46 | }; |
47 | ||
48 | static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io, | |
49 | int *timeout_io, const struct timeval *tv, | |
50 | uint64_t *now) | |
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 | ||
63 | static void udp_afterpoll(void *state, struct pollfd *fds, int nfds, | |
64 | const struct timeval *tv, uint64_t *now) | |
65 | { | |
66 | struct udp *st=state; | |
67 | struct sockaddr_in from; | |
68 | int fromlen; | |
69 | struct notify_list *n; | |
70 | bool_t done; | |
71 | int rv; | |
72 | ||
73 | if (nfds && (fds->revents & POLLIN)) { | |
74 | do { | |
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) { | |
99 | if (n->fn(n->state, st->rbuf, &from)) { | |
100 | done=True; | |
101 | break; | |
102 | } | |
103 | } | |
104 | if (!done) { | |
794f2398 | 105 | uint32_t source,dest; |
ff05a229 | 106 | /* Manufacture and send NAK packet */ |
794f2398 SE |
107 | source=get_uint32(st->rbuf->start); /* Us */ |
108 | dest=get_uint32(st->rbuf->start+4); /* Them */ | |
109 | Message(M_INFO,"udp (port %d): sending NAK\n",st->port); | |
110 | buffer_init(st->rbuf,0); | |
111 | buf_append_uint32(st->rbuf,dest); | |
112 | buf_append_uint32(st->rbuf,source); | |
113 | buf_append_uint32(st->rbuf,0); /* NAK is msg type 0 */ | |
114 | sendto(st->fd, st->rbuf->start, st->rbuf->size, 0, | |
115 | (struct sockaddr *)&from, sizeof(from)); | |
2fe58dfd SE |
116 | BUF_FREE(st->rbuf); |
117 | } | |
118 | BUF_ASSERT_FREE(st->rbuf); | |
119 | } else { | |
120 | BUF_FREE(st->rbuf); | |
121 | } | |
122 | } while (rv>=0); | |
123 | } | |
124 | } | |
125 | ||
126 | static void request_notify(void *commst, void *nst, comm_notify_fn *fn) | |
127 | { | |
128 | struct udp *st=commst; | |
129 | struct notify_list *n; | |
130 | ||
131 | n=safe_malloc(sizeof(*n),"request_notify"); | |
132 | n->fn=fn; | |
133 | n->state=nst; | |
134 | n->next=st->notify; | |
135 | st->notify=n; | |
136 | } | |
137 | ||
138 | static void release_notify(void *commst, void *nst, comm_notify_fn *fn) | |
139 | { | |
140 | struct udp *st=commst; | |
141 | struct notify_list *n, **p, *t; | |
142 | ||
143 | /* XXX untested */ | |
144 | p=&st->notify; | |
145 | for (n=st->notify; n; ) | |
146 | { | |
147 | if (n->state==nst && n->fn==fn) { | |
148 | t=n; | |
149 | *p=n->next; | |
150 | n=n->next; | |
151 | free(t); | |
152 | } else { | |
153 | p=&n->next; | |
154 | n=n->next; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | static bool_t udp_sendmsg(void *commst, struct buffer_if *buf, | |
160 | struct sockaddr_in *dest) | |
161 | { | |
162 | struct udp *st=commst; | |
ff05a229 | 163 | uint8_t *sa; |
2fe58dfd | 164 | |
ff05a229 SE |
165 | if (st->use_proxy) { |
166 | sa=buf->start-8; | |
167 | memcpy(sa,&dest->sin_addr,4); | |
168 | memset(sa+4,0,4); | |
169 | memcpy(sa+6,&dest->sin_port,2); | |
170 | sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy, | |
171 | sizeof(st->proxy)); | |
172 | } else { | |
173 | sendto(st->fd, buf->start, buf->size, 0, | |
174 | (struct sockaddr *)dest, sizeof(*dest)); | |
175 | } | |
2fe58dfd SE |
176 | |
177 | return True; | |
178 | } | |
179 | ||
baa06aeb SE |
180 | static void udp_phase_hook(void *sst, uint32_t new_phase) |
181 | { | |
182 | struct udp *st=sst; | |
183 | struct sockaddr_in addr; | |
184 | ||
185 | st->fd=socket(AF_INET, SOCK_DGRAM, 0); | |
186 | if (st->fd<0) { | |
187 | fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line); | |
188 | } | |
189 | if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) { | |
190 | fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)", | |
191 | st->loc.file,st->loc.line); | |
192 | } | |
193 | if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) { | |
194 | fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)", | |
195 | st->loc.file,st->loc.line); | |
196 | } | |
197 | ||
198 | memset(&addr, 0, sizeof(addr)); | |
199 | addr.sin_family=AF_INET; | |
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) { | |
212 | char *argv[4]; | |
213 | argv[0]=st->authbind; | |
fe5e9cc4 SE |
214 | argv[1]=strdup("00000000"); |
215 | if (!argv[1]) exit(ENOMEM); | |
b2a56f7c SE |
216 | argv[2]=alloca(8); |
217 | if (!argv[2]) exit(ENOMEM); | |
218 | sprintf(argv[2],"%04X",htons(st->port)); | |
219 | argv[3]=NULL; | |
220 | dup2(st->fd,0); | |
221 | execvp(st->authbind,argv); | |
222 | exit(ENOEXEC); | |
223 | } | |
224 | waitpid(c,&status,0); | |
225 | if (WEXITSTATUS(status)!=0) { | |
226 | errno=WEXITSTATUS(status); | |
227 | fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line); | |
228 | } | |
229 | } else { | |
230 | if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) { | |
231 | fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line); | |
232 | } | |
baa06aeb SE |
233 | } |
234 | ||
235 | register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp"); | |
236 | } | |
237 | ||
2fe58dfd SE |
238 | static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context, |
239 | list_t *args) | |
240 | { | |
241 | struct udp *st; | |
242 | item_t *i; | |
243 | dict_t *d; | |
ff05a229 SE |
244 | list_t *l; |
245 | uint32_t a; | |
2fe58dfd SE |
246 | |
247 | st=safe_malloc(sizeof(*st),"udp_apply(st)"); | |
248 | st->loc=loc; | |
249 | st->cl.description="udp"; | |
250 | st->cl.type=CL_COMM; | |
251 | st->cl.apply=NULL; | |
252 | st->cl.interface=&st->ops; | |
253 | st->ops.st=st; | |
ff05a229 SE |
254 | st->ops.min_start_pad=0; |
255 | st->ops.min_end_pad=0; | |
2fe58dfd SE |
256 | st->ops.request_notify=request_notify; |
257 | st->ops.release_notify=release_notify; | |
258 | st->ops.sendmsg=udp_sendmsg; | |
baa06aeb | 259 | st->port=0; |
ff05a229 | 260 | st->use_proxy=False; |
2fe58dfd SE |
261 | |
262 | i=list_elem(args,0); | |
263 | if (!i || i->type!=t_dict) { | |
264 | cfgfatal(st->loc,"udp","first argument must be a dictionary\n"); | |
265 | } | |
266 | d=i->data.dict; | |
267 | ||
baa06aeb | 268 | st->port=dict_read_number(d,"port",True,"udp",st->loc,0); |
2fe58dfd | 269 | st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc); |
b2a56f7c | 270 | st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc); |
ff05a229 SE |
271 | l=dict_lookup(d,"proxy"); |
272 | if (l) { | |
273 | st->use_proxy=True; | |
274 | memset(&st->proxy,0,sizeof(st->proxy)); | |
275 | st->proxy.sin_family=AF_INET; | |
276 | i=list_elem(l,0); | |
277 | if (!i || i->type!=t_string) { | |
278 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); | |
279 | } | |
280 | a=string_item_to_ipaddr(i,"proxy"); | |
281 | st->proxy.sin_addr.s_addr=htonl(a); | |
282 | i=list_elem(l,1); | |
283 | if (!i || i->type!=t_number) { | |
284 | cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n"); | |
285 | } | |
286 | st->proxy.sin_port=htons(i->data.number); | |
287 | st->ops.min_start_pad=8; | |
288 | } | |
2fe58dfd | 289 | |
baa06aeb | 290 | add_hook(PHASE_GETRESOURCES,udp_phase_hook,st); |
2fe58dfd SE |
291 | |
292 | return new_closure(&st->cl); | |
293 | } | |
294 | ||
295 | init_module udp_module; | |
296 | void udp_module(dict_t *dict) | |
297 | { | |
298 | add_closure(dict,"udp",udp_apply); | |
299 | } |