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>
28 #include "radsecproxy.h"
34 static void setprotoopts(struct commonprotoopts *opts);
35 static char **getlistenerargs();
36 void *udpserverrd(void *arg);
37 int clientradputudp(struct server *server, unsigned char *rad);
38 void addclientudp(struct client *client);
39 void addserverextraudp(struct clsrvconf *conf);
43 static const struct protodefs protodefs = {
45 NULL, /* secretdefault */
46 SOCK_DGRAM, /* socktype */
47 "1812", /* portdefault */
48 REQUEST_RETRY_COUNT, /* retrycountdefault */
49 10, /* retrycountmax */
50 REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
51 60, /* retryintervalmax */
52 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
53 setprotoopts, /* setprotoopts */
54 getlistenerargs, /* getlistenerargs */
55 udpserverrd, /* listener */
57 NULL, /* clientconnreader */
58 clientradputudp, /* clientradput */
59 addclientudp, /* addclient */
60 addserverextraudp, /* addserverextra */
61 udpsetsrcres, /* setsrcres */
62 initextraudp /* initextra */
65 static int client4_sock = -1;
66 static int client6_sock = -1;
67 static struct queue *server_replyq = NULL;
69 static struct addrinfo *srcres = NULL;
70 static uint8_t handle;
71 static struct commonprotoopts *protoopts = NULL;
73 const struct protodefs *udpinit(uint8_t h) {
78 static void setprotoopts(struct commonprotoopts *opts) {
82 static char **getlistenerargs() {
83 return protoopts ? protoopts->listenargs : NULL;
88 srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
91 void removeudpclientfromreplyq(struct client *c) {
95 /* lock the common queue and remove replies for this client */
96 pthread_mutex_lock(&c->replyq->mutex);
97 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
98 r = (struct request *)n->data;
102 pthread_mutex_unlock(&c->replyq->mutex);
105 static int addr_equal(struct sockaddr *a, struct sockaddr *b) {
106 switch (a->sa_family) {
108 return !memcmp(&((struct sockaddr_in*)a)->sin_addr,
109 &((struct sockaddr_in*)b)->sin_addr,
110 sizeof(struct in_addr));
112 return IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)a)->sin6_addr,
113 &((struct sockaddr_in6*)b)->sin6_addr);
120 uint16_t port_get(struct sockaddr *sa) {
121 switch (sa->sa_family) {
123 return ntohs(((struct sockaddr_in *)sa)->sin_port);
125 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
130 /* exactly one of client and server must be non-NULL */
131 /* return who we received from in *client or *server */
132 /* return from in sa if not NULL */
133 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
135 unsigned char buf[4], *rad = NULL;
136 struct sockaddr_storage from;
137 struct sockaddr *fromcopy;
138 socklen_t fromlen = sizeof(from);
140 struct list_node *node;
142 struct client *c = NULL;
152 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
154 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
156 debug(DBG_WARN, "radudpget: recv failed");
160 debug(DBG_WARN, "radudpget: length too small");
166 ? find_clconf(handle, (struct sockaddr *)&from, NULL)
167 : find_srvconf(handle, (struct sockaddr *)&from, NULL);
169 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
176 debug(DBG_WARN, "radudpget: length too small");
183 debug(DBG_ERR, "radudpget: malloc failed");
188 cnt = recv(s, rad, len, MSG_TRUNC);
189 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
192 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
196 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
200 pthread_mutex_lock(p->lock);
201 for (node = list_first(p->clients); node;) {
202 c = (struct client *)node->data;
203 node = list_next(node);
206 gettimeofday(&now, NULL);
207 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
208 c->expiry = now.tv_sec + 60;
211 if (c->expiry >= now.tv_sec)
214 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
215 removeudpclientfromreplyq(c);
216 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
217 removelockedclient(c);
221 fromcopy = addr_copy((struct sockaddr *)&from);
223 pthread_mutex_unlock(p->lock);
229 pthread_mutex_unlock(p->lock);
234 gettimeofday(&now, NULL);
235 c->expiry = now.tv_sec + 60;
238 pthread_mutex_unlock(p->lock);
240 *server = p->servers;
244 *port = port_get((struct sockaddr *)&from);
248 int clientradputudp(struct server *server, unsigned char *rad) {
250 struct clsrvconf *conf = server->conf;
253 if (sendto(server->sock, rad, len, 0, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen) >= 0) {
254 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port_get(conf->addrinfo->ai_addr));
258 debug(DBG_WARN, "clientradputudp: send failed");
262 void *udpclientrd(void *arg) {
263 struct server *server;
269 buf = radudpget(*s, NULL, &server, NULL);
274 void *udpserverrd(void *arg) {
276 int *sp = (int *)arg;
281 sleep(5); /* malloc failed */
284 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
292 void *udpserverwr(void *arg) {
293 struct queue *replyq = (struct queue *)arg;
294 struct request *reply;
295 struct sockaddr_storage to;
298 pthread_mutex_lock(&replyq->mutex);
299 while (!(reply = (struct request *)list_shift(replyq->entries))) {
300 debug(DBG_DBG, "udp server writer, waiting for signal");
301 pthread_cond_wait(&replyq->cond, &replyq->mutex);
302 debug(DBG_DBG, "udp server writer, got signal");
304 /* do this with lock, udpserverrd may set from = NULL if from expires */
306 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
307 pthread_mutex_unlock(&replyq->mutex);
309 port_set((struct sockaddr *)&to, reply->udpport);
310 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
311 debug(DBG_WARN, "udpserverwr: send failed");
313 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
318 void addclientudp(struct client *client) {
319 client->replyq = server_replyq;
322 void addserverextraudp(struct clsrvconf *conf) {
323 switch (conf->addrinfo->ai_family) {
325 if (client4_sock < 0) {
326 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
327 if (client4_sock < 0)
328 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
330 conf->servers->sock = client4_sock;
333 if (client6_sock < 0) {
334 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
335 if (client6_sock < 0)
336 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
338 conf->servers->sock = client6_sock;
341 debugx(1, DBG_ERR, "addserver: unsupported address family");
345 void initextraudp() {
346 pthread_t cl4th, cl6th, srvth;
349 freeaddrinfo(srcres);
353 if (client4_sock >= 0)
354 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
355 debugx(1, DBG_ERR, "pthread_create failed");
356 if (client6_sock >= 0)
357 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
358 debugx(1, DBG_ERR, "pthread_create failed");
360 if (find_clconf_type(handle, NULL)) {
361 server_replyq = newqueue();
362 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
363 debugx(1, DBG_ERR, "pthread_create failed");
367 const struct protodefs *udpinit(uint8_t h) {