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"
34 static int client4_sock = -1;
35 static int client6_sock = -1;
36 static struct queue *server_replyq = NULL;
38 static struct addrinfo *srcres = NULL;
40 void udpsetsrcres(char *source) {
42 srcres = resolve_hostport_addrinfo(RAD_UDP, source);
45 void removeudpclientfromreplyq(struct client *c) {
49 /* lock the common queue and remove replies for this client */
50 pthread_mutex_lock(&c->replyq->mutex);
51 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
52 r = (struct request *)n->data;
56 pthread_mutex_unlock(&c->replyq->mutex);
59 /* exactly one of client and server must be non-NULL */
60 /* return who we received from in *client or *server */
61 /* return from in sa if not NULL */
62 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
64 unsigned char buf[4], *rad = NULL;
65 struct sockaddr_storage from;
66 struct sockaddr *fromcopy;
67 socklen_t fromlen = sizeof(from);
69 struct list_node *node;
71 struct client *c = NULL;
81 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
83 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
85 debug(DBG_WARN, "radudpget: recv failed");
89 debug(DBG_WARN, "radudpget: length too small");
95 ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
96 : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
98 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
105 debug(DBG_WARN, "radudpget: length too small");
112 debug(DBG_ERR, "radudpget: malloc failed");
117 cnt = recv(s, rad, len, MSG_TRUNC);
118 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
121 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
125 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
129 pthread_mutex_lock(p->lock);
130 for (node = list_first(p->clients); node;) {
131 c = (struct client *)node->data;
132 node = list_next(node);
135 gettimeofday(&now, NULL);
136 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
137 c->expiry = now.tv_sec + 60;
140 if (c->expiry >= now.tv_sec)
143 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
144 removeudpclientfromreplyq(c);
145 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
146 removelockedclient(c);
150 fromcopy = addr_copy((struct sockaddr *)&from);
152 pthread_mutex_unlock(p->lock);
158 pthread_mutex_unlock(p->lock);
163 gettimeofday(&now, NULL);
164 c->expiry = now.tv_sec + 60;
167 pthread_mutex_unlock(p->lock);
169 *server = p->servers;
173 *port = port_get((struct sockaddr *)&from);
177 int clientradputudp(struct server *server, unsigned char *rad) {
179 struct clsrvconf *conf = server->conf;
182 if (sendto(server->sock, rad, len, 0, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen) >= 0) {
183 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port_get(conf->addrinfo->ai_addr));
187 debug(DBG_WARN, "clientradputudp: send failed");
191 void *udpclientrd(void *arg) {
192 struct server *server;
198 buf = radudpget(*s, NULL, &server, NULL);
203 void *udpserverrd(void *arg) {
205 int *sp = (int *)arg;
210 sleep(5); /* malloc failed */
213 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
220 void *udpserverwr(void *arg) {
221 struct queue *replyq = (struct queue *)arg;
222 struct request *reply;
223 struct sockaddr_storage to;
226 pthread_mutex_lock(&replyq->mutex);
227 while (!(reply = (struct request *)list_shift(replyq->entries))) {
228 debug(DBG_DBG, "udp server writer, waiting for signal");
229 pthread_cond_wait(&replyq->cond, &replyq->mutex);
230 debug(DBG_DBG, "udp server writer, got signal");
232 /* do this with lock, udpserverrd may set from = NULL if from expires */
234 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
235 pthread_mutex_unlock(&replyq->mutex);
237 port_set((struct sockaddr *)&to, reply->udpport);
238 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
239 debug(DBG_WARN, "udpserverwr: send failed");
241 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
246 void addclientudp(struct client *client) {
247 client->replyq = server_replyq;
250 void addserverextraudp(struct clsrvconf *conf) {
251 switch (conf->addrinfo->ai_family) {
253 if (client4_sock < 0) {
254 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
255 if (client4_sock < 0)
256 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
258 conf->servers->sock = client4_sock;
261 if (client6_sock < 0) {
262 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
263 if (client6_sock < 0)
264 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
266 conf->servers->sock = client6_sock;
269 debugx(1, DBG_ERR, "addserver: unsupported address family");
273 void initextraudp() {
274 pthread_t cl4th, cl6th, srvth;
277 freeaddrinfo(srcres);
281 if (client4_sock >= 0)
282 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
283 debugx(1, DBG_ERR, "pthread_create failed");
284 if (client6_sock >= 0)
285 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
286 debugx(1, DBG_ERR, "pthread_create failed");
288 if (find_clconf_type(RAD_UDP, NULL)) {
289 server_replyq = newqueue();
290 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
291 debugx(1, DBG_ERR, "pthread_create failed");