2 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
10 #include <sys/socket.h>
11 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
24 #include <arpa/inet.h>
27 #include <openssl/ssl.h>
31 #include "radsecproxy.h"
33 static void setprotoopts(struct commonprotoopts *opts);
34 static char **getlistenerargs();
35 void *udpserverrd(void *arg);
36 int clientradputudp(struct server *server, unsigned char *rad);
37 void addclientudp(struct client *client);
38 void addserverextraudp(struct clsrvconf *conf);
42 static const struct protodefs protodefs = {
44 NULL, /* secretdefault */
45 SOCK_DGRAM, /* socktype */
46 "1812", /* portdefault */
47 REQUEST_RETRY_COUNT, /* retrycountdefault */
48 10, /* retrycountmax */
49 REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
50 60, /* retryintervalmax */
51 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
52 setprotoopts, /* setprotoopts */
53 getlistenerargs, /* getlistenerargs */
54 udpserverrd, /* listener */
56 NULL, /* clientconnreader */
57 clientradputudp, /* clientradput */
58 addclientudp, /* addclient */
59 addserverextraudp, /* addserverextra */
60 udpsetsrcres, /* setsrcres */
61 initextraudp /* initextra */
64 static int client4_sock = -1;
65 static int client6_sock = -1;
66 static struct queue *server_replyq = NULL;
68 static struct addrinfo *srcres = NULL;
69 static uint8_t handle;
70 static struct commonprotoopts *protoopts = NULL;
72 const struct protodefs *udpinit(uint8_t h) {
77 static void setprotoopts(struct commonprotoopts *opts) {
81 static char **getlistenerargs() {
82 return protoopts ? protoopts->listenargs : NULL;
87 srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
90 void removeudpclientfromreplyq(struct client *c) {
94 /* lock the common queue and remove replies for this client */
95 pthread_mutex_lock(&c->replyq->mutex);
96 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
97 r = (struct request *)n->data;
101 pthread_mutex_unlock(&c->replyq->mutex);
104 /* exactly one of client and server must be non-NULL */
105 /* return who we received from in *client or *server */
106 /* return from in sa if not NULL */
107 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
109 unsigned char buf[4], *rad = NULL;
110 struct sockaddr_storage from;
111 struct sockaddr *fromcopy;
112 socklen_t fromlen = sizeof(from);
114 struct list_node *node;
116 struct client *c = NULL;
126 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
128 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
130 debug(DBG_WARN, "radudpget: recv failed");
134 debug(DBG_WARN, "radudpget: length too small");
140 ? find_clconf(handle, (struct sockaddr *)&from, NULL)
141 : find_srvconf(handle, (struct sockaddr *)&from, NULL);
143 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
150 debug(DBG_WARN, "radudpget: length too small");
157 debug(DBG_ERR, "radudpget: malloc failed");
162 cnt = recv(s, rad, len, MSG_TRUNC);
163 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
166 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
170 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
174 pthread_mutex_lock(p->lock);
175 for (node = list_first(p->clients); node;) {
176 c = (struct client *)node->data;
177 node = list_next(node);
180 gettimeofday(&now, NULL);
181 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
182 c->expiry = now.tv_sec + 60;
185 if (c->expiry >= now.tv_sec)
188 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
189 removeudpclientfromreplyq(c);
190 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
191 removelockedclient(c);
195 fromcopy = addr_copy((struct sockaddr *)&from);
197 pthread_mutex_unlock(p->lock);
203 pthread_mutex_unlock(p->lock);
208 gettimeofday(&now, NULL);
209 c->expiry = now.tv_sec + 60;
212 pthread_mutex_unlock(p->lock);
214 *server = p->servers;
218 *port = port_get((struct sockaddr *)&from);
222 int clientradputudp(struct server *server, unsigned char *rad) {
224 struct clsrvconf *conf = server->conf;
227 if (sendto(server->sock, rad, len, 0, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen) >= 0) {
228 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port_get(conf->addrinfo->ai_addr));
232 debug(DBG_WARN, "clientradputudp: send failed");
236 void *udpclientrd(void *arg) {
237 struct server *server;
243 buf = radudpget(*s, NULL, &server, NULL);
248 void *udpserverrd(void *arg) {
250 int *sp = (int *)arg;
255 sleep(5); /* malloc failed */
258 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
265 void *udpserverwr(void *arg) {
266 struct queue *replyq = (struct queue *)arg;
267 struct request *reply;
268 struct sockaddr_storage to;
271 pthread_mutex_lock(&replyq->mutex);
272 while (!(reply = (struct request *)list_shift(replyq->entries))) {
273 debug(DBG_DBG, "udp server writer, waiting for signal");
274 pthread_cond_wait(&replyq->cond, &replyq->mutex);
275 debug(DBG_DBG, "udp server writer, got signal");
277 /* do this with lock, udpserverrd may set from = NULL if from expires */
279 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
280 pthread_mutex_unlock(&replyq->mutex);
282 port_set((struct sockaddr *)&to, reply->udpport);
283 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
284 debug(DBG_WARN, "udpserverwr: send failed");
286 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
291 void addclientudp(struct client *client) {
292 client->replyq = server_replyq;
295 void addserverextraudp(struct clsrvconf *conf) {
296 switch (conf->addrinfo->ai_family) {
298 if (client4_sock < 0) {
299 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
300 if (client4_sock < 0)
301 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
303 conf->servers->sock = client4_sock;
306 if (client6_sock < 0) {
307 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
308 if (client6_sock < 0)
309 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
311 conf->servers->sock = client6_sock;
314 debugx(1, DBG_ERR, "addserver: unsupported address family");
318 void initextraudp() {
319 pthread_t cl4th, cl6th, srvth;
322 freeaddrinfo(srcres);
326 if (client4_sock >= 0)
327 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
328 debugx(1, DBG_ERR, "pthread_create failed");
329 if (client6_sock >= 0)
330 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
331 debugx(1, DBG_ERR, "pthread_create failed");
333 if (find_clconf_type(handle, NULL)) {
334 server_replyq = newqueue();
335 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
336 debugx(1, DBG_ERR, "pthread_create failed");