moved more code to tlscommon, fixed a couple of minor lint warnings
[radsecproxy.git] / udp.c
1 /*
2  * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
3  *
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.
7  */
8
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <limits.h>
16 #ifdef SYS_SOLARIS9
17 #include <fcntl.h>
18 #endif
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
22 #include <ctype.h>
23 #include <sys/wait.h>
24 #include <arpa/inet.h>
25 #include <regex.h>
26 #include <pthread.h>
27 #include "list.h"
28 #include "radsecproxy.h"
29
30 #ifdef RADPROT_UDP
31 #include "debug.h"
32 #include "util.h"
33
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);
40 void udpsetsrcres();
41 void initextraudp();
42
43 static const struct protodefs protodefs = {
44     "udp",
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 */
56     NULL, /* connecter */
57     NULL, /* clientconnreader */
58     clientradputudp, /* clientradput */
59     addclientudp, /* addclient */
60     addserverextraudp, /* addserverextra */
61     udpsetsrcres, /* setsrcres */
62     initextraudp /* initextra */
63 };
64
65 static int client4_sock = -1;
66 static int client6_sock = -1;
67 static struct queue *server_replyq = NULL;
68
69 static struct addrinfo *srcres = NULL;
70 static uint8_t handle;
71 static struct commonprotoopts *protoopts = NULL;
72
73 const struct protodefs *udpinit(uint8_t h) {
74     handle = h;
75     return &protodefs;
76 }
77
78 static void setprotoopts(struct commonprotoopts *opts) {
79     protoopts = opts;
80 }
81
82 static char **getlistenerargs() {
83     return protoopts ? protoopts->listenargs : NULL;
84 }
85
86 void udpsetsrcres() {
87     if (!srcres)
88         srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
89 }
90
91 void removeudpclientfromreplyq(struct client *c) {
92     struct list_node *n;
93     struct request *r;
94     
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;
99         if (r->from == c)
100             r->from = NULL;
101     }
102     pthread_mutex_unlock(&c->replyq->mutex);
103 }       
104
105 static int addr_equal(struct sockaddr *a, struct sockaddr *b) {
106     switch (a->sa_family) {
107     case AF_INET:
108         return !memcmp(&((struct sockaddr_in*)a)->sin_addr,
109                        &((struct sockaddr_in*)b)->sin_addr,
110                        sizeof(struct in_addr));
111     case AF_INET6:
112         return IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)a)->sin6_addr,
113                                   &((struct sockaddr_in6*)b)->sin6_addr);
114     default:
115         /* Must not reach */
116         return 0;
117     }
118 }
119
120 uint16_t port_get(struct sockaddr *sa) {
121     switch (sa->sa_family) {
122     case AF_INET:
123         return ntohs(((struct sockaddr_in *)sa)->sin_port);
124     case AF_INET6:
125         return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
126     }
127     return 0;
128 }
129
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) {
134     int cnt, len;
135     unsigned char buf[4], *rad = NULL;
136     struct sockaddr_storage from;
137     struct sockaddr *fromcopy;
138     socklen_t fromlen = sizeof(from);
139     struct clsrvconf *p;
140     struct list_node *node;
141     fd_set readfds;
142     struct client *c = NULL;
143     struct timeval now;
144     
145     for (;;) {
146         if (rad) {
147             free(rad);
148             rad = NULL;
149         }
150         FD_ZERO(&readfds);
151         FD_SET(s, &readfds);
152         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
153             continue;
154         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
155         if (cnt == -1) {
156             debug(DBG_WARN, "radudpget: recv failed");
157             continue;
158         }
159         if (cnt < 20) {
160             debug(DBG_WARN, "radudpget: length too small");
161             recv(s, buf, 4, 0);
162             continue;
163         }
164         
165         p = client
166             ? find_clconf(handle, (struct sockaddr *)&from, NULL)
167             : find_srvconf(handle, (struct sockaddr *)&from, NULL);
168         if (!p) {
169             debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
170             recv(s, buf, 4, 0);
171             continue;
172         }
173         
174         len = RADLEN(buf);
175         if (len < 20) {
176             debug(DBG_WARN, "radudpget: length too small");
177             recv(s, buf, 4, 0);
178             continue;
179         }
180             
181         rad = malloc(len);
182         if (!rad) {
183             debug(DBG_ERR, "radudpget: malloc failed");
184             recv(s, buf, 4, 0);
185             continue;
186         }
187         
188         cnt = recv(s, rad, len, MSG_TRUNC);
189         debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
190
191         if (cnt < len) {
192             debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
193             continue;
194         }
195         if (cnt > len)
196             debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
197
198         if (client) {
199             *client = NULL;
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);
204                 if (s != c->sock)
205                     continue;
206                 gettimeofday(&now, NULL);
207                 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
208                     c->expiry = now.tv_sec + 60;
209                     *client = c;
210                 }
211                 if (c->expiry >= now.tv_sec)
212                     continue;
213                 
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);
218                 break;
219             }
220             if (!*client) {
221                 fromcopy = addr_copy((struct sockaddr *)&from);
222                 if (!fromcopy) {
223                     pthread_mutex_unlock(p->lock);
224                     continue;
225                 }
226                 c = addclient(p, 0);
227                 if (!c) {
228                     free(fromcopy);
229                     pthread_mutex_unlock(p->lock);
230                     continue;
231                 }
232                 c->sock = s;
233                 c->addr = fromcopy;
234                 gettimeofday(&now, NULL);
235                 c->expiry = now.tv_sec + 60;
236                 *client = c;
237             }
238             pthread_mutex_unlock(p->lock);
239         } else if (server)
240             *server = p->servers;
241         break;
242     }
243     if (port)
244         *port = port_get((struct sockaddr *)&from);
245     return rad;
246 }
247
248 int clientradputudp(struct server *server, unsigned char *rad) {
249     size_t len;
250     struct clsrvconf *conf = server->conf;
251     
252     len = RADLEN(rad);
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));
255         return 1;
256     }
257
258     debug(DBG_WARN, "clientradputudp: send failed");
259     return 0;
260 }
261
262 void *udpclientrd(void *arg) {
263     struct server *server;
264     unsigned char *buf;
265     int *s = (int *)arg;
266     
267     for (;;) {
268         server = NULL;
269         buf = radudpget(*s, NULL, &server, NULL);
270         replyh(server, buf);
271     }
272 }
273
274 void *udpserverrd(void *arg) {
275     struct request *rq;
276     int *sp = (int *)arg;
277     
278     for (;;) {
279         rq = newrequest();
280         if (!rq) {
281             sleep(5); /* malloc failed */
282             continue;
283         }
284         rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
285         rq->udpsock = *sp;
286         radsrv(rq);
287     }
288     free(sp);
289     return NULL;
290 }
291
292 void *udpserverwr(void *arg) {
293     struct queue *replyq = (struct queue *)arg;
294     struct request *reply;
295     struct sockaddr_storage to;
296     
297     for (;;) {
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");
303         }
304         /* do this with lock, udpserverrd may set from = NULL if from expires */
305         if (reply->from)
306             memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
307         pthread_mutex_unlock(&replyq->mutex);
308         if (reply->from) {
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");
312         }
313         debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
314         freerq(reply);
315     }
316 }
317
318 void addclientudp(struct client *client) {
319     client->replyq = server_replyq;
320 }
321
322 void addserverextraudp(struct clsrvconf *conf) {
323     switch (conf->addrinfo->ai_family) {
324     case AF_INET:
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);
329         }
330         conf->servers->sock = client4_sock;
331         break;
332     case AF_INET6:
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);
337         }
338         conf->servers->sock = client6_sock;
339         break;
340     default:
341         debugx(1, DBG_ERR, "addserver: unsupported address family");
342     }
343 }
344
345 void initextraudp() {
346     pthread_t cl4th, cl6th, srvth;
347
348     if (srcres) {
349         freeaddrinfo(srcres);
350         srcres = NULL;
351     }
352     
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");
359
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");
364     }
365 }
366 #else
367 const struct protodefs *udpinit(uint8_t h) {
368     return NULL;
369 }
370 #endif