1 /* UDP send/receive module for secnet */
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
8 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
17 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #include "unaligned.h"
26 #include "comm-common.h"
28 static comm_sendmsg_fn udp_sendmsg
;
32 struct udpsocks socks
;
36 * Re comm_addr.ix: This field allows us to note in the comm_addr
37 * which socket an incoming packet was received on. This is required
38 * for conveniently logging the actual source of a packet. But the ix
39 * does not formally form part of the address: it is not used when
40 * sending, nor when comparing two comm_addrs.
42 * The special value -1 means that the comm_addr was constructed by
43 * another module in secnet (eg the resolver), rather than being a
44 * description of the source of an incoming packet.
47 static const char *udp_addr_to_string(void *commst
, const struct comm_addr
*ca
)
49 struct udp
*st
=commst
;
50 struct udpsocks
*socks
=&st
->socks
;
51 static char sbuf
[100];
52 int ix
=ca
->ix
>=0 ? ca
->ix
: 0;
54 assert(ix
>=0 && ix
<socks
->n_socks
);
55 snprintf(sbuf
, sizeof(sbuf
), "udp:%s%s-%s",
56 iaddr_to_string(&socks
->socks
[ix
].addr
),
58 iaddr_to_string(&ca
->ia
));
62 static int udp_socks_beforepoll(void *state
, struct pollfd
*fds
, int *nfds_io
,
65 struct udpsocks
*socks
=state
;
67 BEFOREPOLL_WANT_FDS(socks
->n_socks
);
68 for (i
=0; i
<socks
->n_socks
; i
++) {
69 fds
[i
].fd
=socks
->socks
[i
].fd
;
75 static void udp_socks_afterpoll(void *state
, struct pollfd
*fds
, int nfds
)
77 struct udpsocks
*socks
=state
;
78 struct udpcommon
*uc
=socks
->uc
;
85 struct commcommon
*cc
=&uc
->cc
;
87 for (i
=0; i
<socks
->n_socks
; i
++) {
88 struct udpsock
*us
=&socks
->socks
[i
];
89 if (i
>=nfds
) continue;
90 if (!(fds
[i
].revents
& POLLIN
)) continue;
91 assert(fds
[i
].fd
== us
->fd
);
95 BUF_ASSERT_FREE(cc
->rbuf
);
96 BUF_ALLOC(cc
->rbuf
,"udp_afterpoll");
97 buffer_init(cc
->rbuf
,calculate_max_start_pad());
98 rv
=recvfrom(fd
, cc
->rbuf
->start
,
99 buf_remaining_space(cc
->rbuf
),
100 0, &from
.sa
, &fromlen
);
104 /* Check that the packet came from our poxy server;
105 we shouldn't be contacted directly by anybody else
106 (since they can trivially forge source addresses) */
107 if (!iaddr_equal(&from
,&uc
->proxy
)) {
108 Message(M_INFO
,"udp: received packet that's not "
113 /* proxy protocol supports ipv4 transport only */
114 from
.sa
.sa_family
=AF_INET
;
115 BUF_GET_BYTES(unprepend
,cc
->rbuf
,&from
.sin
.sin_addr
,4);
116 buf_unprepend(cc
->rbuf
,2);
117 BUF_GET_BYTES(unprepend
,cc
->rbuf
,&from
.sin
.sin_port
,2);
123 done
=comm_notify(&cc
->notify
, cc
->rbuf
, &ca
);
126 if (cc
->rbuf
->size
>12 /* prevents traffic amplification */
127 && ((msgtype
=get_uint32(cc
->rbuf
->start
+8))
129 uint32_t source
,dest
;
130 /* Manufacture and send NAK packet */
131 source
=get_uint32(cc
->rbuf
->start
); /* Us */
132 dest
=get_uint32(cc
->rbuf
->start
+4); /* Them */
133 send_nak(&ca
,source
,dest
,msgtype
,cc
->rbuf
,"unwanted");
137 BUF_ASSERT_FREE(cc
->rbuf
);
145 static bool_t
udp_sendmsg(void *commst
, struct buffer_if
*buf
,
146 const struct comm_addr
*dest
)
148 struct udp
*st
=commst
;
149 struct udpcommon
*uc
=&st
->uc
;
150 struct udpsocks
*socks
=&st
->socks
;
154 struct udpsock
*us
=&socks
->socks
[0];
155 sa
=buf_prepend(buf
,8);
156 if (dest
->ia
.sa
.sa_family
!= AF_INET
) {
158 "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
159 iaddr_to_string(&dest
->ia
));
162 memcpy(sa
,&dest
->ia
.sin
.sin_addr
,4);
164 memcpy(sa
+6,&dest
->ia
.sin
.sin_port
,2);
165 sendto(us
->fd
,sa
,buf
->size
+8,0,&uc
->proxy
.sa
,
166 iaddr_socklen(&uc
->proxy
));
167 buf_unprepend(buf
,8);
170 bool_t allunsupported
=True
;
171 int af
=dest
->ia
.sa
.sa_family
;
172 for (i
=0; i
<socks
->n_socks
; i
++) {
173 struct udpsock
*us
=&socks
->socks
[i
];
174 if (us
->addr
.sa
.sa_family
!= af
)
175 /* no point even trying */
177 r
=sendto(us
->fd
, buf
->start
, buf
->size
, 0,
178 &dest
->ia
.sa
, iaddr_socklen(&dest
->ia
));
179 if (r
>=0) return True
;
180 if (!(errno
==EAFNOSUPPORT
|| errno
==ENETUNREACH
))
181 /* who knows what that error means? */
182 allunsupported
=False
;
184 return !allunsupported
; /* see doc for comm_sendmsg_fn in secnet.h */
190 void udp_destroy_socket(struct udpcommon
*uc
, struct udpsock
*us
)
198 bool_t
udp_make_socket(struct udpcommon
*uc
, struct udpsock
*us
,
201 const union iaddr
*addr
=&us
->addr
;
202 struct commcommon
*cc
=&uc
->cc
;
205 #define FAIL_LG 0, cc->cl.description, &cc->loc, failmsgclass
206 #define FAIL(...) do{ \
207 lg_perror(FAIL_LG,errno,__VA_ARGS__); \
211 us
->fd
=socket(addr
->sa
.sa_family
, SOCK_DGRAM
, IPPROTO_UDP
);
212 if (us
->fd
<0) FAIL("socket");
216 if (addr
->sa
.sa_family
==AF_INET6
) {
219 socklen_t optlen
=sizeof(optval
);
220 r
=setsockopt(us
->fd
,IPPROTO_IPV6
,IPV6_V6ONLY
,&optval
,optlen
);
221 if (r
) FAIL("setsockopt(,IPV6_V6ONLY,&1,)");
229 /* XXX this fork() and waitpid() business needs to be hidden
230 in some system-specific library functions. */
233 FAIL("fork() for authbind");
235 char *argv
[5], addrstr
[33], portstr
[5];
239 switch (addr
->sa
.sa_family
) {
241 sprintf(addrstr
,"%08lX",(long)addr
->sin
.sin_addr
.s_addr
);
242 port
=addr
->sin
.sin_port
;
249 sprintf(addrstr
+i
*2,"%02X",addr
->sin6
.sin6_addr
.s6_addr
[i
]);
250 port
=addr
->sin6
.sin6_port
;
254 #endif /*CONFIG_IPV6*/
256 fatal("udp (%s:%d): unsupported address family for authbind",
257 cc
->loc
.file
,cc
->loc
.line
);
259 sprintf(portstr
,"%04X",port
);
260 argv
[0]=uc
->authbind
;
263 argv
[3]=(char*)addrfam
;
266 execvp(uc
->authbind
,argv
);
269 while (waitpid(c
,&status
,0)==-1) {
270 if (errno
==EINTR
) continue;
271 FAIL("waitpid for authbind");
274 if (WIFEXITED(status
) && WEXITSTATUS(status
)<127) {
275 int es
=WEXITSTATUS(status
);
276 lg_perror(FAIL_LG
,es
,
277 "authbind exited with error exit status %d;"
278 " indicates error",es
);
280 lg_exitstatus(FAIL_LG
,status
,"authbind");
285 if (bind(us
->fd
, &addr
->sa
, iaddr_socklen(addr
))!=0)
286 FAIL("bind (%s)",iaddr_to_string(addr
));
291 udp_destroy_socket(uc
,us
);
297 void udp_socks_register(struct udpcommon
*uc
, struct udpsocks
*socks
)
301 register_for_poll(socks
,udp_socks_beforepoll
,udp_socks_afterpoll
,"udp");
304 void udp_socks_deregister(struct udpcommon
*uc
, struct udpsocks
*socks
)
307 deregister_for_poll(socks
->interest
);
310 static void udp_phase_hook(void *sst
, uint32_t new_phase
)
313 struct udpsocks
*socks
=&st
->socks
;
314 struct udpcommon
*uc
=&st
->uc
;
316 for (i
=0; i
<socks
->n_socks
; i
++)
317 udp_make_socket(uc
,&socks
->socks
[i
],M_FATAL
);
319 udp_socks_register(uc
,socks
);
322 static list_t
*udp_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
331 COMM_APPLY(st
,&st
->uc
.cc
,udp_
,"udp",loc
);
332 COMM_APPLY_STANDARD(st
,&st
->uc
.cc
,"udp",args
);
333 UDP_APPLY_STANDARD(st
,&st
->uc
,"udp");
335 struct udpcommon
*uc
=&st
->uc
;
336 struct udpsocks
*socks
=&st
->socks
;
337 struct commcommon
*cc
=&uc
->cc
;
339 union iaddr defaultaddrs
[] = {
341 { .sin6
= { .sin6_family
=AF_INET6
,
342 .sin6_port
=htons(uc
->port
),
343 .sin6_addr
=IN6ADDR_ANY_INIT
} },
345 { .sin
= { .sin_family
=AF_INET
,
346 .sin_port
=htons(uc
->port
),
347 .sin_addr
= { .s_addr
=INADDR_ANY
} } }
350 caddrl
=dict_lookup(d
,"address");
351 socks
->n_socks
=caddrl ?
list_length(caddrl
) : (int)ARRAY_SIZE(defaultaddrs
);
352 if (socks
->n_socks
<=0 || socks
->n_socks
>UDP_MAX_SOCKETS
)
353 cfgfatal(cc
->loc
,"udp","`address' must be 1..%d addresses",
356 for (i
=0; i
<socks
->n_socks
; i
++) {
357 struct udpsock
*us
=&socks
->socks
[i
];
358 if (!list_length(caddrl
)) {
359 us
->addr
=defaultaddrs
[i
];
361 string_item_to_iaddr(list_elem(caddrl
,i
),uc
->port
,&us
->addr
,"udp");
366 l
=dict_lookup(d
,"proxy");
369 uc
->proxy
.sa
.sa_family
=AF_INET
;
371 if (!item
|| item
->type
!=t_string
) {
372 cfgfatal(cc
->loc
,"udp","proxy must supply ""addr"",port\n");
374 a
=string_item_to_ipaddr(item
,"proxy");
375 uc
->proxy
.sin
.sin_addr
.s_addr
=htonl(a
);
377 if (!item
|| item
->type
!=t_number
) {
378 cfgfatal(cc
->loc
,"udp","proxy must supply ""addr"",port\n");
380 uc
->proxy
.sin
.sin_port
=htons(item
->data
.number
);
383 update_max_start_pad(&comm_max_start_pad
, uc
->use_proxy ?
8 : 0);
385 add_hook(PHASE_GETRESOURCES
,udp_phase_hook
,st
);
387 return new_closure(&cc
->cl
);
390 void udp_module(dict_t
*dict
)
392 add_closure(dict
,"udp",udp_apply
);