2 * Copyright (C) 2006-2009 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"
35 static void setprotoopts(struct commonprotoopts *opts);
36 static char **getlistenerargs();
37 void *udpserverrd(void *arg);
38 int clientradputudp(struct server *server, unsigned char *rad);
39 void addclientudp(struct client *client);
40 void addserverextraudp(struct clsrvconf *conf);
44 static const struct protodefs protodefs = {
46 NULL, /* secretdefault */
47 SOCK_DGRAM, /* socktype */
48 "1812", /* portdefault */
49 REQUEST_RETRY_COUNT, /* retrycountdefault */
50 10, /* retrycountmax */
51 REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
52 60, /* retryintervalmax */
53 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
54 setprotoopts, /* setprotoopts */
55 getlistenerargs, /* getlistenerargs */
56 udpserverrd, /* listener */
58 NULL, /* clientconnreader */
59 clientradputudp, /* clientradput */
60 addclientudp, /* addclient */
61 addserverextraudp, /* addserverextra */
62 udpsetsrcres, /* setsrcres */
63 initextraudp /* initextra */
66 static int client4_sock = -1;
67 static int client6_sock = -1;
68 static struct gqueue *server_replyq = NULL;
70 static struct addrinfo *srcres = NULL;
71 static uint8_t handle;
72 static struct commonprotoopts *protoopts = NULL;
74 const struct protodefs *udpinit(uint8_t h) {
79 static void setprotoopts(struct commonprotoopts *opts) {
83 static char **getlistenerargs() {
84 return protoopts ? protoopts->listenargs : NULL;
90 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
91 AF_UNSPEC, NULL, protodefs.socktype);
94 void removeudpclientfromreplyq(struct client *c) {
98 /* lock the common queue and remove replies for this client */
99 pthread_mutex_lock(&c->replyq->mutex);
100 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
101 r = (struct request *)n->data;
105 pthread_mutex_unlock(&c->replyq->mutex);
108 static int addr_equal(struct sockaddr *a, struct sockaddr *b) {
109 switch (a->sa_family) {
111 return !memcmp(&((struct sockaddr_in*)a)->sin_addr,
112 &((struct sockaddr_in*)b)->sin_addr,
113 sizeof(struct in_addr));
115 return IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)a)->sin6_addr,
116 &((struct sockaddr_in6*)b)->sin6_addr);
123 uint16_t port_get(struct sockaddr *sa) {
124 switch (sa->sa_family) {
126 return ntohs(((struct sockaddr_in *)sa)->sin_port);
128 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
133 /* exactly one of client and server must be non-NULL */
134 /* return who we received from in *client or *server */
135 /* return from in sa if not NULL */
136 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
138 unsigned char buf[4], *rad = NULL;
139 struct sockaddr_storage from;
140 struct sockaddr *fromcopy;
141 socklen_t fromlen = sizeof(from);
143 struct list_node *node;
145 struct client *c = NULL;
155 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
157 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
159 debug(DBG_WARN, "radudpget: recv failed");
164 ? find_clconf(handle, (struct sockaddr *)&from, NULL)
165 : find_srvconf(handle, (struct sockaddr *)&from, NULL);
167 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
174 debug(DBG_WARN, "radudpget: length too small");
181 debug(DBG_ERR, "radudpget: malloc failed");
186 cnt = recv(s, rad, len, MSG_TRUNC);
187 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
190 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
194 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
198 pthread_mutex_lock(p->lock);
199 for (node = list_first(p->clients); node;) {
200 c = (struct client *)node->data;
201 node = list_next(node);
204 gettimeofday(&now, NULL);
205 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
206 c->expiry = now.tv_sec + 60;
209 if (c->expiry >= now.tv_sec)
212 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
213 removeudpclientfromreplyq(c);
214 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
215 removelockedclient(c);
219 fromcopy = addr_copy((struct sockaddr *)&from);
221 pthread_mutex_unlock(p->lock);
227 pthread_mutex_unlock(p->lock);
232 gettimeofday(&now, NULL);
233 c->expiry = now.tv_sec + 60;
236 pthread_mutex_unlock(p->lock);
238 *server = p->servers;
242 *port = port_get((struct sockaddr *)&from);
246 int clientradputudp(struct server *server, unsigned char *rad) {
248 struct clsrvconf *conf = server->conf;
252 ai = ((struct hostportres *)list_first(conf->hostports)->data)->addrinfo;
253 if (sendto(server->sock, rad, len, 0, ai->ai_addr, ai->ai_addrlen) >= 0) {
254 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, addr2string(ai->ai_addr), port_get(ai->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 gqueue *replyq = (struct gqueue *)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 assert(list_first(conf->hostports) != NULL);
324 switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
326 if (client4_sock < 0) {
327 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
328 if (client4_sock < 0)
329 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
331 conf->servers->sock = client4_sock;
334 if (client6_sock < 0) {
335 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
336 if (client6_sock < 0)
337 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
339 conf->servers->sock = client6_sock;
342 debugx(1, DBG_ERR, "addserver: unsupported address family");
346 void initextraudp() {
347 pthread_t cl4th, cl6th, srvth;
350 freeaddrinfo(srcres);
354 if (client4_sock >= 0)
355 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
356 debugx(1, DBG_ERR, "pthread_create failed");
357 if (client6_sock >= 0)
358 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
359 debugx(1, DBG_ERR, "pthread_create failed");
361 if (find_clconf_type(handle, NULL)) {
362 server_replyq = newqueue();
363 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
364 debugx(1, DBG_ERR, "pthread_create failed");
368 const struct protodefs *udpinit(uint8_t h) {
373 /* Local Variables: */
374 /* c-file-style: "stroustrup" */