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 void removeudpclientfromreplyq(struct client *c) {
42 /* lock the common queue and remove replies for this client */
43 pthread_mutex_lock(&c->replyq->mutex);
44 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
45 r = (struct request *)n->data;
49 pthread_mutex_unlock(&c->replyq->mutex);
52 /* exactly one of client and server must be non-NULL */
53 /* return who we received from in *client or *server */
54 /* return from in sa if not NULL */
55 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
57 unsigned char buf[4], *rad = NULL;
58 struct sockaddr_storage from;
59 struct sockaddr *fromcopy;
60 socklen_t fromlen = sizeof(from);
62 struct list_node *node;
64 struct client *c = NULL;
74 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
76 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
78 debug(DBG_WARN, "radudpget: recv failed");
82 debug(DBG_WARN, "radudpget: length too small");
88 ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
89 : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
91 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
98 debug(DBG_WARN, "radudpget: length too small");
105 debug(DBG_ERR, "radudpget: malloc failed");
110 cnt = recv(s, rad, len, MSG_TRUNC);
111 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
114 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
118 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
122 pthread_mutex_lock(p->lock);
123 for (node = list_first(p->clients); node;) {
124 c = (struct client *)node->data;
125 node = list_next(node);
128 gettimeofday(&now, NULL);
129 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
130 c->expiry = now.tv_sec + 60;
133 if (c->expiry >= now.tv_sec)
136 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
137 removeudpclientfromreplyq(c);
138 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
139 removelockedclient(c);
143 fromcopy = addr_copy((struct sockaddr *)&from);
145 pthread_mutex_unlock(p->lock);
151 pthread_mutex_unlock(p->lock);
156 gettimeofday(&now, NULL);
157 c->expiry = now.tv_sec + 60;
160 pthread_mutex_unlock(p->lock);
162 *server = p->servers;
166 *port = port_get((struct sockaddr *)&from);
170 int clientradputudp(struct server *server, unsigned char *rad) {
172 struct sockaddr_storage sa;
173 struct sockaddr *sap;
174 struct clsrvconf *conf = server->conf;
178 port = port_get(conf->addrinfo->ai_addr);
180 if (*rad == RAD_Accounting_Request) {
181 sap = (struct sockaddr *)&sa;
182 memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
183 port_set(sap, ++port);
185 sap = conf->addrinfo->ai_addr;
187 if (sendto(server->sock, rad, len, 0, sap, conf->addrinfo->ai_addrlen) >= 0) {
188 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port);
192 debug(DBG_WARN, "clientradputudp: send failed");
196 void *udpclientrd(void *arg) {
197 struct server *server;
203 buf = radudpget(*s, NULL, &server, NULL);
208 void *udpserverrd(void *arg) {
210 int *sp = (int *)arg;
215 sleep(5); /* malloc failed */
218 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
225 void *udpserverwr(void *arg) {
226 struct queue *replyq = (struct queue *)arg;
227 struct request *reply;
228 struct sockaddr_storage to;
231 pthread_mutex_lock(&replyq->mutex);
232 while (!(reply = (struct request *)list_shift(replyq->entries))) {
233 debug(DBG_DBG, "udp server writer, waiting for signal");
234 pthread_cond_wait(&replyq->cond, &replyq->mutex);
235 debug(DBG_DBG, "udp server writer, got signal");
237 /* do this with lock, udpserverrd may set from = NULL if from expires */
239 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
240 pthread_mutex_unlock(&replyq->mutex);
242 port_set((struct sockaddr *)&to, reply->udpport);
243 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
244 debug(DBG_WARN, "udpserverwr: send failed");
246 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
251 void addclientudp(struct client *client) {
252 client->replyq = server_replyq;
255 void addserverextraudp(struct clsrvconf *conf) {
256 switch (conf->addrinfo->ai_family) {
258 if (client4_sock < 0) {
259 client4_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET, 0, 1);
260 if (client4_sock < 0)
261 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
263 conf->servers->sock = client4_sock;
266 if (client6_sock < 0) {
267 client6_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET6, 0, 1);
268 if (client6_sock < 0)
269 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
271 conf->servers->sock = client6_sock;
274 debugx(1, DBG_ERR, "addserver: unsupported address family");
278 void initextraudp() {
279 pthread_t cl4th, cl6th, srvth;
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");