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>
29 #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;
89 srcres = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
92 void removeudpclientfromreplyq(struct client *c) {
96 /* lock the common queue and remove replies for this client */
97 pthread_mutex_lock(&c->replyq->mutex);
98 for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
99 r = (struct request *)n->data;
103 pthread_mutex_unlock(&c->replyq->mutex);
106 static int addr_equal(struct sockaddr *a, struct sockaddr *b) {
107 switch (a->sa_family) {
109 return !memcmp(&((struct sockaddr_in*)a)->sin_addr,
110 &((struct sockaddr_in*)b)->sin_addr,
111 sizeof(struct in_addr));
113 return IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)a)->sin6_addr,
114 &((struct sockaddr_in6*)b)->sin6_addr);
121 uint16_t port_get(struct sockaddr *sa) {
122 switch (sa->sa_family) {
124 return ntohs(((struct sockaddr_in *)sa)->sin_port);
126 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
131 /* exactly one of client and server must be non-NULL */
132 /* return who we received from in *client or *server */
133 /* return from in sa if not NULL */
134 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
136 unsigned char buf[4], *rad = NULL;
137 struct sockaddr_storage from;
138 struct sockaddr *fromcopy;
139 socklen_t fromlen = sizeof(from);
141 struct list_node *node;
143 struct client *c = NULL;
153 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
155 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
157 debug(DBG_WARN, "radudpget: recv failed");
162 ? find_clconf(handle, (struct sockaddr *)&from, NULL)
163 : find_srvconf(handle, (struct sockaddr *)&from, NULL);
165 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
172 debug(DBG_WARN, "radudpget: length too small");
179 debug(DBG_ERR, "radudpget: malloc failed");
184 cnt = recv(s, rad, len, MSG_TRUNC);
185 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
188 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
192 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
196 pthread_mutex_lock(p->lock);
197 for (node = list_first(p->clients); node;) {
198 c = (struct client *)node->data;
199 node = list_next(node);
202 gettimeofday(&now, NULL);
203 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
204 c->expiry = now.tv_sec + 60;
207 if (c->expiry >= now.tv_sec)
210 debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
211 removeudpclientfromreplyq(c);
212 c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
213 removelockedclient(c);
217 fromcopy = addr_copy((struct sockaddr *)&from);
219 pthread_mutex_unlock(p->lock);
225 pthread_mutex_unlock(p->lock);
230 gettimeofday(&now, NULL);
231 c->expiry = now.tv_sec + 60;
234 pthread_mutex_unlock(p->lock);
236 *server = p->servers;
240 *port = port_get((struct sockaddr *)&from);
244 int clientradputudp(struct server *server, unsigned char *rad) {
246 struct clsrvconf *conf = server->conf;
250 ai = ((struct hostportres *)list_first(conf->hostports)->data)->addrinfo;
251 if (sendto(server->sock, rad, len, 0, ai->ai_addr, ai->ai_addrlen) >= 0) {
252 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, addr2string(ai->ai_addr), port_get(ai->ai_addr));
256 debug(DBG_WARN, "clientradputudp: send failed");
260 void *udpclientrd(void *arg) {
261 struct server *server;
267 buf = radudpget(*s, NULL, &server, NULL);
272 void *udpserverrd(void *arg) {
274 int *sp = (int *)arg;
279 sleep(5); /* malloc failed */
282 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
290 void *udpserverwr(void *arg) {
291 struct gqueue *replyq = (struct gqueue *)arg;
292 struct request *reply;
293 struct sockaddr_storage to;
296 pthread_mutex_lock(&replyq->mutex);
297 while (!(reply = (struct request *)list_shift(replyq->entries))) {
298 debug(DBG_DBG, "udp server writer, waiting for signal");
299 pthread_cond_wait(&replyq->cond, &replyq->mutex);
300 debug(DBG_DBG, "udp server writer, got signal");
302 /* do this with lock, udpserverrd may set from = NULL if from expires */
304 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
305 pthread_mutex_unlock(&replyq->mutex);
307 port_set((struct sockaddr *)&to, reply->udpport);
308 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
309 debug(DBG_WARN, "udpserverwr: send failed");
311 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
316 void addclientudp(struct client *client) {
317 client->replyq = server_replyq;
320 void addserverextraudp(struct clsrvconf *conf) {
321 switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
323 if (client4_sock < 0) {
324 client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
325 if (client4_sock < 0)
326 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
328 conf->servers->sock = client4_sock;
331 if (client6_sock < 0) {
332 client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
333 if (client6_sock < 0)
334 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
336 conf->servers->sock = client6_sock;
339 debugx(1, DBG_ERR, "addserver: unsupported address family");
343 void initextraudp() {
344 pthread_t cl4th, cl6th, srvth;
347 freeaddrinfo(srcres);
351 if (client4_sock >= 0)
352 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
353 debugx(1, DBG_ERR, "pthread_create failed");
354 if (client6_sock >= 0)
355 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
356 debugx(1, DBG_ERR, "pthread_create failed");
358 if (find_clconf_type(handle, NULL)) {
359 server_replyq = newqueue();
360 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
361 debugx(1, DBG_ERR, "pthread_create failed");
365 const struct protodefs *udpinit(uint8_t h) {
370 /* Local Variables: */
371 /* c-file-style: "stroustrup" */