xref: /openbsd-src/sbin/isakmpd/udp.c (revision ea54190e82662a5c895c614ab2ea0aa4c1ba05dc)
1*ea54190eSbluhm /* $OpenBSD: udp.c,v 1.95 2008/07/22 09:45:56 bluhm Exp $	 */
2fc6affffSniklas /* $EOM: udp.c,v 1.57 2001/01/26 10:09:57 niklas Exp $	 */
32040585eSniklas 
42040585eSniklas /*
542af7185Sniklas  * Copyright (c) 1998, 1999, 2001 Niklas Hallqvist.  All rights reserved.
6bdbf6df3Sniklas  * Copyright (c) 2000 Angelos D. Keromytis.  All rights reserved.
7cd6bf844Sho  * Copyright (c) 2003, 2004 H�kan Olsson.  All rights reserved.
82040585eSniklas  *
92040585eSniklas  * Redistribution and use in source and binary forms, with or without
102040585eSniklas  * modification, are permitted provided that the following conditions
112040585eSniklas  * are met:
122040585eSniklas  * 1. Redistributions of source code must retain the above copyright
132040585eSniklas  *    notice, this list of conditions and the following disclaimer.
142040585eSniklas  * 2. Redistributions in binary form must reproduce the above copyright
152040585eSniklas  *    notice, this list of conditions and the following disclaimer in the
162040585eSniklas  *    documentation and/or other materials provided with the distribution.
172040585eSniklas  *
182040585eSniklas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
192040585eSniklas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202040585eSniklas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
212040585eSniklas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
222040585eSniklas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
232040585eSniklas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242040585eSniklas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252040585eSniklas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262040585eSniklas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
272040585eSniklas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282040585eSniklas  */
292040585eSniklas 
302040585eSniklas /*
312040585eSniklas  * This code was written under funding by Ericsson Radio Systems.
322040585eSniklas  */
332040585eSniklas 
342040585eSniklas #include <sys/types.h>
35ed7b55caSniklas #include <sys/ioctl.h>
362040585eSniklas #include <sys/socket.h>
37ed7b55caSniklas #include <sys/sockio.h>
382040585eSniklas #include <net/if.h>
392040585eSniklas #include <netinet/in.h>
402040585eSniklas #include <arpa/inet.h>
412b81057dSniklas #include <ctype.h>
42c27359ddSniklas #include <limits.h>
432040585eSniklas #include <netdb.h>
442040585eSniklas #include <stdlib.h>
452040585eSniklas #include <string.h>
462040585eSniklas #include <unistd.h>
472040585eSniklas 
482b81057dSniklas #include "conf.h"
492040585eSniklas #include "if.h"
502040585eSniklas #include "isakmp.h"
512040585eSniklas #include "log.h"
522040585eSniklas #include "message.h"
53da35d433Sho #include "monitor.h"
542040585eSniklas #include "transport.h"
552040585eSniklas #include "udp.h"
562040585eSniklas #include "util.h"
57cd6bf844Sho #include "virtual.h"
582040585eSniklas 
592040585eSniklas #define UDP_SIZE 65536
602040585eSniklas 
616966a9b4Sniklas /* If a system doesn't have SO_REUSEPORT, SO_REUSEADDR will have to do. */
626966a9b4Sniklas #ifndef SO_REUSEPORT
636966a9b4Sniklas #define SO_REUSEPORT SO_REUSEADDR
646966a9b4Sniklas #endif
656966a9b4Sniklas 
66cd6bf844Sho /* These are reused by udp_encap.c, thus not 'static' here.  */
67cd6bf844Sho struct transport *udp_clone(struct transport *, struct sockaddr *);
68aa584aacSho int		  udp_fd_set(struct transport *, fd_set *, int);
69aa584aacSho int		  udp_fd_isset(struct transport *, fd_set *);
70cd6bf844Sho void		  udp_get_dst(struct transport *, struct sockaddr **);
71cd6bf844Sho void		  udp_get_src(struct transport *, struct sockaddr **);
72cd6bf844Sho char		 *udp_decode_ids(struct transport *);
73dec6ea27Sho void		  udp_remove(struct transport *);
742040585eSniklas 
752040585eSniklas static struct transport *udp_create(char *);
768c08363fSniklas static void     udp_report(struct transport *);
772040585eSniklas static void     udp_handle_message(struct transport *);
78a86dabf6Sho static struct transport *udp_make(struct sockaddr *);
79cd6bf844Sho static int      udp_send_message(struct message *, struct transport *);
802040585eSniklas 
81bc64d176Sniklas static struct transport_vtbl udp_transport_vtbl = {
82cd6bf844Sho 	{0}, "udp_physical",
832040585eSniklas 	udp_create,
84cd6bf844Sho 	0,
858c08363fSniklas 	udp_remove,
868c08363fSniklas 	udp_report,
87aa584aacSho 	udp_fd_set,
88aa584aacSho 	udp_fd_isset,
892040585eSniklas 	udp_handle_message,
902040585eSniklas 	udp_send_message,
912040585eSniklas 	udp_get_dst,
92d580e0efSniklas 	udp_get_src,
93cd6bf844Sho 	udp_decode_ids,
94cd6bf844Sho 	udp_clone,
95cd6bf844Sho 	0
962040585eSniklas };
972040585eSniklas 
98a86dabf6Sho char		*udp_default_port = 0;
99e2cc3c2eSho int		 bind_family = 0;
1002040585eSniklas 
101cd6bf844Sho void
udp_init(void)102cd6bf844Sho udp_init(void)
103bc64d176Sniklas {
104cd6bf844Sho 	transport_method_add(&udp_transport_vtbl);
105bc64d176Sniklas }
106bc64d176Sniklas 
1072040585eSniklas /* Create a UDP transport structure bound to LADDR just for listening.  */
1082040585eSniklas static struct transport *
udp_make(struct sockaddr * laddr)109a86dabf6Sho udp_make(struct sockaddr *laddr)
1102040585eSniklas {
1112040585eSniklas 	struct udp_transport *t = 0;
112eec12a65Sangelos 	int	s, on, wildcardaddress = 0;
113cd6bf844Sho 	char	*tstr;
1142040585eSniklas 
115a86dabf6Sho 	t = calloc(1, sizeof *t);
116fb9475d6Sderaadt 	if (!t) {
117cd6bf844Sho 		log_print("udp_make: calloc (1, %lu) failed",
11812f43dabShshoexer 		    (unsigned long)sizeof *t);
119b7ada372Shshoexer 		free(laddr);
1202040585eSniklas 		return 0;
1212040585eSniklas 	}
122b7ada372Shshoexer 	t->src = laddr;
123b7ada372Shshoexer 
124dc986b1fSderaadt 	s = socket(laddr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
125fb9475d6Sderaadt 	if (s == -1) {
126fb9475d6Sderaadt 		log_error("udp_make: socket (%d, %d, %d)", laddr->sa_family,
127fb9475d6Sderaadt 		    SOCK_DGRAM, IPPROTO_UDP);
1282040585eSniklas 		goto err;
1292040585eSniklas 	}
1302040585eSniklas 	/* Make sure we don't get our traffic encrypted.  */
13164528339Sho 	if (sysdep_cleartext(s, laddr->sa_family) == -1)
132375ca604Sangelos 		goto err;
1332040585eSniklas 
134eec12a65Sangelos 	/* Wildcard address ?  */
135fb9475d6Sderaadt 	switch (laddr->sa_family) {
136eec12a65Sangelos 	case AF_INET:
13712f43dabShshoexer 		if (((struct sockaddr_in *)laddr)->sin_addr.s_addr ==
13812f43dabShshoexer 		    INADDR_ANY)
139eec12a65Sangelos 			wildcardaddress = 1;
140eec12a65Sangelos 		break;
141eec12a65Sangelos 	case AF_INET6:
142eec12a65Sangelos 		if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)laddr)->sin6_addr))
143eec12a65Sangelos 			wildcardaddress = 1;
144eec12a65Sangelos 		break;
145eec12a65Sangelos 	}
146eec12a65Sangelos 
1476c8b4880Sniklas 	/*
1486c8b4880Sniklas 	 * In order to have several bound specific address-port combinations
14912f43dabShshoexer 	 * with the same port SO_REUSEADDR is needed.  If this is a wildcard
15012f43dabShshoexer 	 * socket and we are not listening there, but only sending from it
15112f43dabShshoexer 	 * make sure it is entirely reuseable with SO_REUSEPORT.
1526c8b4880Sniklas 	 */
1532040585eSniklas 	on = 1;
154dc986b1fSderaadt 	if (setsockopt(s, SOL_SOCKET,
155eec12a65Sangelos 	    wildcardaddress ? SO_REUSEPORT : SO_REUSEADDR,
156fb9475d6Sderaadt 	    (void *)&on, sizeof on) == -1) {
15712f43dabShshoexer 		log_error("udp_make: setsockopt (%d, %d, %d, %p, %lu)", s,
15812f43dabShshoexer 		    SOL_SOCKET, wildcardaddress ? SO_REUSEPORT : SO_REUSEADDR,
1597eb3b581Sderaadt 		    &on, (unsigned long)sizeof on);
1602040585eSniklas 		goto err;
1612040585eSniklas 	}
1622040585eSniklas 	t->transport.vtbl = &udp_transport_vtbl;
163c506f982Shshoexer 	if (monitor_bind(s, t->src, SA_LEN(t->src))) {
164a86dabf6Sho 		if (sockaddr2text(t->src, &tstr, 0))
1657eb3b581Sderaadt 			log_error("udp_make: bind (%d, %p, %lu)", s, &t->src,
1667eb3b581Sderaadt 			    (unsigned long)sizeof t->src);
167fb9475d6Sderaadt 		else {
1687eb3b581Sderaadt 			log_error("udp_make: bind (%d, %s, %lu)", s, tstr,
1697eb3b581Sderaadt 			    (unsigned long)sizeof t->src);
170a86dabf6Sho 			free(tstr);
171a86dabf6Sho 		}
1722040585eSniklas 		goto err;
1732040585eSniklas 	}
1742040585eSniklas 	t->s = s;
175cd6bf844Sho 	if (sockaddr2text(t->src, &tstr, 0))
176cd6bf844Sho 		LOG_DBG((LOG_MISC, 20, "udp_make: "
177cd6bf844Sho 		    "transport %p socket %d family %d", t, s,
178cd6bf844Sho 		    t->src->sa_family == AF_INET ? 4 : 6));
179cd6bf844Sho 	else {
180cd6bf844Sho 		LOG_DBG((LOG_MISC, 20, "udp_make: "
181cd6bf844Sho 		    "transport %p socket %d ip %s port %d", t, s,
182cd6bf844Sho 		    tstr, ntohs(sockaddr_port(t->src))));
183cd6bf844Sho 		free (tstr);
184cd6bf844Sho 	}
185cd6bf844Sho 	transport_setup(&t->transport, 0);
1866c8b4880Sniklas 	t->transport.flags |= TRANSPORT_LISTEN;
187fbb98801Sniklas 	return &t->transport;
1882040585eSniklas 
1892040585eSniklas err:
190b2e8d7d4Sangelos 	if (s >= 0)
1912040585eSniklas 		close(s);
192fb9475d6Sderaadt 	if (t) {
193495943a8Sniklas 		/* Already closed.  */
194495943a8Sniklas 		t->s = -1;
195a86dabf6Sho 		udp_remove(&t->transport);
196c545386fSangelos 	}
1972040585eSniklas 	return 0;
1982040585eSniklas }
1992040585eSniklas 
2002040585eSniklas /* Clone a listen transport U, record a destination RADDR for outbound use.  */
201cd6bf844Sho struct transport *
udp_clone(struct transport * ut,struct sockaddr * raddr)202cd6bf844Sho udp_clone(struct transport *ut, struct sockaddr *raddr)
2032040585eSniklas {
204cd6bf844Sho 	struct udp_transport *u = (struct udp_transport *)ut;
2052040585eSniklas 	struct udp_transport *u2;
206fb9475d6Sderaadt 	struct transport *t;
2072040585eSniklas 
2082040585eSniklas 	t = malloc(sizeof *u);
209fb9475d6Sderaadt 	if (!t) {
21012f43dabShshoexer 		log_error("udp_clone: malloc (%lu) failed",
21112f43dabShshoexer 		    (unsigned long)sizeof *u);
2122040585eSniklas 		return 0;
2138c08363fSniklas 	}
2142040585eSniklas 	u2 = (struct udp_transport *)t;
2152040585eSniklas 
2166c8b4880Sniklas 	memcpy(u2, u, sizeof *u);
217a86dabf6Sho 
218c506f982Shshoexer 	u2->src = malloc(SA_LEN(u->src));
219fb9475d6Sderaadt 	if (!u2->src) {
220c506f982Shshoexer 		log_error("udp_clone: malloc (%lu) failed",
221c506f982Shshoexer 		    (unsigned long)SA_LEN(u->src));
222a86dabf6Sho 		free(t);
223a86dabf6Sho 		return 0;
224a86dabf6Sho 	}
225c506f982Shshoexer 	memcpy(u2->src, u->src, SA_LEN(u->src));
226a86dabf6Sho 
227c506f982Shshoexer 	u2->dst = malloc(SA_LEN(raddr));
228fb9475d6Sderaadt 	if (!u2->dst) {
229c506f982Shshoexer 		log_error("udp_clone: malloc (%lu) failed",
230c506f982Shshoexer 		    (unsigned long)SA_LEN(raddr));
231a86dabf6Sho 		free(u2->src);
232a86dabf6Sho 		free(t);
233a86dabf6Sho 		return 0;
234a86dabf6Sho 	}
235c506f982Shshoexer 	memcpy(u2->dst, raddr, SA_LEN(raddr));
236a86dabf6Sho 
2372040585eSniklas 	t->flags &= ~TRANSPORT_LISTEN;
238cd6bf844Sho 	transport_setup(t, 0);
2392040585eSniklas 	return t;
2402040585eSniklas }
2412040585eSniklas 
2422040585eSniklas /*
2432040585eSniklas  * Initialize an object of the UDP transport class.  Fill in the local
2442040585eSniklas  * IP address and port information and create a server socket bound to
2452040585eSniklas  * that specific port.  Add the polymorphic transport structure to the
2462040585eSniklas  * system-wide pools of known ISAKMP transports.
2472040585eSniklas  */
248cd6bf844Sho struct transport *
udp_bind(const struct sockaddr * addr)2493f78cf41Sitojun udp_bind(const struct sockaddr *addr)
2502040585eSniklas {
251b7ada372Shshoexer 	struct sockaddr *src;
2522040585eSniklas 
253c506f982Shshoexer 	src = malloc(SA_LEN(addr));
254a86dabf6Sho 	if (!src)
255a86dabf6Sho 		return 0;
256a86dabf6Sho 
257c506f982Shshoexer 	memcpy(src, addr, SA_LEN(addr));
258a86dabf6Sho 	return udp_make(src);
2592040585eSniklas }
2602040585eSniklas 
2612040585eSniklas /*
2622b81057dSniklas  * NAME is a section name found in the config database.  Setup and return
2636f497690Sniklas  * a transport useable to talk to the peer specified by that name.
2642b81057dSniklas  */
2652b81057dSniklas static struct transport *
udp_create(char * name)2662b81057dSniklas udp_create(char *name)
2672b81057dSniklas {
268cd6bf844Sho 	struct virtual_transport *v;
269bc64d176Sniklas 	struct udp_transport *u;
270*ea54190eSbluhm 	struct transport *rv = 0;
271a86dabf6Sho 	struct sockaddr	*dst, *addr;
2722b81057dSniklas 	char	*addr_str, *port_str;
273cd6bf844Sho 	struct conf_list *addr_list = 0;
274cd6bf844Sho 	struct conf_list_node *addr_node;
2752b81057dSniklas 
2762b81057dSniklas 	port_str = conf_get_str(name, "Port");
277a86dabf6Sho 	if (!port_str)
278a86dabf6Sho 		port_str = udp_default_port;
279a86dabf6Sho 	if (!port_str)
280cd6bf844Sho 		port_str = UDP_DEFAULT_PORT_STR;
2812b81057dSniklas 
2826df03ebfSniklas 	addr_str = conf_get_str(name, "Address");
283fb9475d6Sderaadt 	if (!addr_str) {
28450eea14cSho 		log_print("udp_create: no address configured for \"%s\"",
28550eea14cSho 		    name);
286f69947b5Sniklas 		return 0;
287f69947b5Sniklas 	}
288e3283cbfSmcbride 	if (text2sockaddr(addr_str, port_str, &dst, 0, 0)) {
28912f43dabShshoexer 		log_print("udp_create: address \"%s\" not understood",
29012f43dabShshoexer 		    addr_str);
2912040585eSniklas 		return 0;
2922040585eSniklas 	}
293bc64d176Sniklas 	addr_str = conf_get_str(name, "Local-address");
294bc64d176Sniklas 	if (!addr_str)
295cd6bf844Sho 		addr_list = conf_get_list("General", "Listen-on");
296cd6bf844Sho 	if (!addr_str && !addr_list) {
297cd6bf844Sho 		v = virtual_get_default(dst->sa_family);
298*ea54190eSbluhm 		if (!v) {
299*ea54190eSbluhm 			log_print("udp_create: no virtual default transport "
300*ea54190eSbluhm 			    "for address family %d", dst->sa_family);
301a86dabf6Sho 			goto ret;
302*ea54190eSbluhm 		}
303*ea54190eSbluhm 		u = (struct udp_transport *)v->main;
304*ea54190eSbluhm 		if (!u) {
305*ea54190eSbluhm 			log_print("udp_create: no udp default transport "
306*ea54190eSbluhm 			    "for address family %d", dst->sa_family);
307*ea54190eSbluhm 			goto ret;
308*ea54190eSbluhm 		}
309cd6bf844Sho 		rv = udp_clone((struct transport *)u, dst);
310cd6bf844Sho 		if (rv)
311cd6bf844Sho 			rv->vtbl = &udp_transport_vtbl;
312cd6bf844Sho 		goto ret;
313cd6bf844Sho 	}
314cd6bf844Sho 
315cd6bf844Sho 	if (addr_list) {
316cd6bf844Sho 		for (addr_node = TAILQ_FIRST(&addr_list->fields);
317cd6bf844Sho 		    addr_node; addr_node = TAILQ_NEXT(addr_node, link))
318e3283cbfSmcbride 			if (text2sockaddr(addr_node->field,
3198cd03bd8Sderaadt 			    port_str, &addr, 0, 0) == 0) {
320cd6bf844Sho 				v = virtual_listen_lookup(addr);
321cd6bf844Sho 				free(addr);
322cd6bf844Sho 				if (v) {
323cd6bf844Sho 					addr_str = addr_node->field;
324cd6bf844Sho 					break;
325cd6bf844Sho 				}
326cd6bf844Sho 			}
327cd6bf844Sho 		if (!addr_str) {
328cd6bf844Sho 			log_print("udp_create: no matching listener found");
329a86dabf6Sho 			goto ret;
330a86dabf6Sho 		}
331ed7b55caSniklas 	}
332e3283cbfSmcbride 	if (text2sockaddr(addr_str, port_str, &addr, 0, 0)) {
33312f43dabShshoexer 		log_print("udp_create: address \"%s\" not understood",
33412f43dabShshoexer 		    addr_str);
335a86dabf6Sho 		goto ret;
336bc64d176Sniklas 	}
337cd6bf844Sho 
338cd6bf844Sho 	v = virtual_listen_lookup(addr);
339a86dabf6Sho 	free(addr);
340cd6bf844Sho 	if (!v) {
34112f43dabShshoexer 		log_print("udp_create: %s:%s must exist as a listener too",
34212f43dabShshoexer 		    addr_str, port_str);
343a86dabf6Sho 		goto ret;
344bc64d176Sniklas 	}
345cd6bf844Sho 	rv = udp_clone(v->main, dst);
346dec6ea27Sho 	if (rv)
347cd6bf844Sho 		rv->vtbl = &udp_transport_vtbl;
348a86dabf6Sho 
349a86dabf6Sho ret:
350cd6bf844Sho 	if (addr_list)
351cd6bf844Sho 		conf_free_list(addr_list);
352a86dabf6Sho 	free(dst);
353a86dabf6Sho 	return rv;
3542040585eSniklas }
3552040585eSniklas 
3568c08363fSniklas void
udp_remove(struct transport * t)3578c08363fSniklas udp_remove(struct transport *t)
3588c08363fSniklas {
359a86dabf6Sho 	struct udp_transport *u = (struct udp_transport *)t;
3607fb8cfbaShshoexer 	struct transport *p;
361a86dabf6Sho 
362a86dabf6Sho 	free(u->src);
363a86dabf6Sho 	free(u->dst);
364dec6ea27Sho 	if ((t->flags & TRANSPORT_LISTEN) && u->s >= 0)
365c545386fSangelos 		close(u->s);
3667fb8cfbaShshoexer 
3677fb8cfbaShshoexer 	for (p = LIST_FIRST(&transport_list); p && p != t; p =
3687fb8cfbaShshoexer 	    LIST_NEXT(p, link))
3697fb8cfbaShshoexer 		;
3707fb8cfbaShshoexer 	if (p == t)
371dec6ea27Sho 		LIST_REMOVE(t, link);
372dec6ea27Sho 
373dec6ea27Sho 	LOG_DBG((LOG_TRANSPORT, 90, "udp_remove: removed transport %p", t));
3748c08363fSniklas 	free(t);
3758c08363fSniklas }
3768c08363fSniklas 
3778c08363fSniklas /* Report transport-method specifics of the T transport. */
3788c08363fSniklas void
udp_report(struct transport * t)3798c08363fSniklas udp_report(struct transport *t)
3808c08363fSniklas {
3818c08363fSniklas 	struct udp_transport *u = (struct udp_transport *)t;
382b7ada372Shshoexer 	char		*src = NULL, *dst = NULL;
383cd6bf844Sho 	in_port_t	 sport, dport;
3848c08363fSniklas 
385c27359ddSniklas 	if (sockaddr2text(u->src, &src, 0))
3863fb44e3bShshoexer 		return;
387cd6bf844Sho 	sport = sockaddr_port(u->src);
388a86dabf6Sho 
389c27359ddSniklas 	if (!u->dst || sockaddr2text(u->dst, &dst, 0))
390c27359ddSniklas 		dst = 0;
391cd6bf844Sho 	dport = dst ? sockaddr_port(u->dst) : 0;
392c27359ddSniklas 
393cd6bf844Sho 	LOG_DBG((LOG_REPORT, 0, "udp_report: fd %d src %s:%u dst %s:%u", u->s,
394cd6bf844Sho 	    src, ntohs(sport), dst ? dst : "<none>", ntohs(dport)));
395a86dabf6Sho 
396a86dabf6Sho 	free(dst);
397a86dabf6Sho 	free(src);
3988c08363fSniklas }
3998c08363fSniklas 
4002040585eSniklas /*
4012040585eSniklas  * A message has arrived on transport T's socket.  If T is single-ended,
4022040585eSniklas  * clone it into a double-ended transport which we will use from now on.
4032040585eSniklas  * Package the message as we want it and continue processing in the message
4042040585eSniklas  * module.
4052040585eSniklas  */
4062040585eSniklas static void
udp_handle_message(struct transport * t)4072040585eSniklas udp_handle_message(struct transport *t)
4082040585eSniklas {
409862799fcSangelos 	struct udp_transport *u = (struct udp_transport *)t;
4102040585eSniklas 	u_int8_t        buf[UDP_SIZE];
411a86dabf6Sho 	struct sockaddr_storage from;
412b26670e8Sho 	u_int32_t       len = sizeof from;
4132040585eSniklas 	ssize_t         n;
4142040585eSniklas 	struct message *msg;
4152040585eSniklas 
4162040585eSniklas 	n = recvfrom(u->s, buf, UDP_SIZE, 0, (struct sockaddr *)&from, &len);
417fb9475d6Sderaadt 	if (n == -1) {
41812f43dabShshoexer 		log_error("recvfrom (%d, %p, %d, %d, %p, %p)", u->s, buf,
41912f43dabShshoexer 		    UDP_SIZE, 0, &from, &len);
4202040585eSniklas 		return;
4212040585eSniklas 	}
422440df6e7Smarkus 
423440df6e7Smarkus 	if (t->virtual == (struct transport *)virtual_get_default(AF_INET) ||
424440df6e7Smarkus 	    t->virtual == (struct transport *)virtual_get_default(AF_INET6)) {
425440df6e7Smarkus 		t->virtual->vtbl->reinit();
426440df6e7Smarkus 
427440df6e7Smarkus 		/*
428440df6e7Smarkus 		 * As we don't know the actual destination address of the
429440df6e7Smarkus 		 * packet, we can't really deal with it. So, just ignore it
430440df6e7Smarkus 		 * and hope we catch the retransmission.
431440df6e7Smarkus 		 */
432440df6e7Smarkus 		return;
433440df6e7Smarkus 	}
434440df6e7Smarkus 
4352040585eSniklas 	/*
4362040585eSniklas 	 * Make a specialized UDP transport structure out of the incoming
4372040585eSniklas 	 * transport and the address information we got from recvfrom(2).
4382040585eSniklas 	 */
439cd6bf844Sho 	t = t->virtual->vtbl->clone(t->virtual, (struct sockaddr *)&from);
4402040585eSniklas 	if (!t)
4412040585eSniklas 		return;
4422040585eSniklas 
4432040585eSniklas 	msg = message_alloc(t, buf, n);
444fb9475d6Sderaadt 	if (!msg) {
44512f43dabShshoexer 		log_error("failed to allocate message structure, dropping "
44612f43dabShshoexer 		    "packet received on transport %p", u);
447cd6bf844Sho 		t->vtbl->remove(t);
4482040585eSniklas 		return;
449c545386fSangelos 	}
4502040585eSniklas 	message_recv(msg);
4512040585eSniklas }
4522040585eSniklas 
4532040585eSniklas /* Physically send the message MSG over its associated transport.  */
4542040585eSniklas static int
udp_send_message(struct message * msg,struct transport * t)455cd6bf844Sho udp_send_message(struct message *msg, struct transport *t)
4562040585eSniklas {
457cd6bf844Sho 	struct udp_transport *u = (struct udp_transport *)t;
4582040585eSniklas 	ssize_t         n;
4592040585eSniklas 	struct msghdr   m;
4602040585eSniklas 
4612040585eSniklas 	/*
4622040585eSniklas 	 * Sending on connected sockets requires that no destination address is
4632040585eSniklas 	 * given, or else EISCONN will occur.
4642040585eSniklas 	 */
465a86dabf6Sho 	m.msg_name = (caddr_t) u->dst;
466c506f982Shshoexer 	m.msg_namelen = SA_LEN(u->dst);
4672040585eSniklas 	m.msg_iov = msg->iov;
4682040585eSniklas 	m.msg_iovlen = msg->iovlen;
4692040585eSniklas 	m.msg_control = 0;
4702040585eSniklas 	m.msg_controllen = 0;
4712040585eSniklas 	m.msg_flags = 0;
4722040585eSniklas 	n = sendmsg(u->s, &m, 0);
473fb9475d6Sderaadt 	if (n == -1) {
474c545386fSangelos 		/* XXX We should check whether the address has gone away */
4752040585eSniklas 		log_error("sendmsg (%d, %p, %d)", u->s, &m, 0);
4762040585eSniklas 		return -1;
4772040585eSniklas 	}
4782040585eSniklas 	return 0;
4792040585eSniklas }
4802040585eSniklas 
481aa584aacSho int
udp_fd_set(struct transport * t,fd_set * fds,int bit)482aa584aacSho udp_fd_set(struct transport *t, fd_set *fds, int bit)
483aa584aacSho {
484aa584aacSho 	struct udp_transport		*u = (struct udp_transport *)t;
485aa584aacSho 
486aa584aacSho 	if (bit)
487aa584aacSho 		FD_SET(u->s, fds);
488aa584aacSho 	else
489aa584aacSho 		FD_CLR(u->s, fds);
490aa584aacSho 
491aa584aacSho 	return u->s + 1;
492aa584aacSho }
493aa584aacSho 
494aa584aacSho int
udp_fd_isset(struct transport * t,fd_set * fds)495aa584aacSho udp_fd_isset(struct transport *t, fd_set *fds)
496aa584aacSho {
497aa584aacSho 	struct udp_transport		*u = (struct udp_transport *)t;
498aa584aacSho 
499aa584aacSho 	return FD_ISSET(u->s, fds);
500aa584aacSho }
501aa584aacSho 
5022040585eSniklas /*
5032040585eSniklas  * Get transport T's peer address and stuff it into the sockaddr pointed
5043fd8781bSho  * to by DST.
5052040585eSniklas  */
506cd6bf844Sho void
udp_get_dst(struct transport * t,struct sockaddr ** dst)5073fd8781bSho udp_get_dst(struct transport *t, struct sockaddr **dst)
5082040585eSniklas {
509a86dabf6Sho 	*dst = ((struct udp_transport *)t)->dst;
5102040585eSniklas }
5112040585eSniklas 
5122040585eSniklas /*
5132040585eSniklas  * Get transport T's local address and stuff it into the sockaddr pointed
514a86dabf6Sho  * to by SRC.  Put its length into SRC_LEN.
5152040585eSniklas  */
516cd6bf844Sho void
udp_get_src(struct transport * t,struct sockaddr ** src)5173fd8781bSho udp_get_src(struct transport *t, struct sockaddr **src)
5182040585eSniklas {
519a86dabf6Sho 	*src = ((struct udp_transport *)t)->src;
5202040585eSniklas }
52150fbe797Sniklas 
522cd6bf844Sho char *
udp_decode_ids(struct transport * t)523d580e0efSniklas udp_decode_ids(struct transport *t)
524d580e0efSniklas {
525cd6bf844Sho 	struct sockaddr *src, *dst;
526d580e0efSniklas 	static char     result[1024];
527d580e0efSniklas 	char            idsrc[256], iddst[256];
528d580e0efSniklas 
529cd6bf844Sho 	t->vtbl->get_src(t, &src);
530cd6bf844Sho 	t->vtbl->get_dst(t, &dst);
531cd6bf844Sho 
532c506f982Shshoexer 	if (getnameinfo(src, SA_LEN(src), idsrc, sizeof idsrc, NULL, 0,
533cd6bf844Sho 	    NI_NUMERICHOST) != 0) {
534f09dfa8eSho 		log_print("udp_decode_ids: getnameinfo () failed for 'src'");
535b8380d91Sho 		strlcpy(idsrc, "<error>", 256);
536d580e0efSniklas 	}
537c506f982Shshoexer 	if (getnameinfo(dst, SA_LEN(dst), iddst, sizeof iddst, NULL, 0,
538cd6bf844Sho 	    NI_NUMERICHOST) != 0) {
539f09dfa8eSho 		log_print("udp_decode_ids: getnameinfo () failed for 'dst'");
540b8380d91Sho 		strlcpy(iddst, "<error>", 256);
541d580e0efSniklas 	}
54265091f4cSniklas 
543f799be47Sho 	snprintf(result, sizeof result, "src: %s dst: %s", idsrc, iddst);
544d580e0efSniklas 	return result;
545d580e0efSniklas }
546