radsecproxy-1.6.5.
[radsecproxy.git] / udp.c
1 /*
2  * Copyright (C) 2006-2009 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 <assert.h>
28 #include "radsecproxy.h"
29 #include "hostport.h"
30
31 #ifdef RADPROT_UDP
32 #include "debug.h"
33 #include "util.h"
34
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);
41 void udpsetsrcres();
42 void initextraudp();
43
44 static const struct protodefs protodefs = {
45     "udp",
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 */
57     NULL, /* connecter */
58     NULL, /* clientconnreader */
59     clientradputudp, /* clientradput */
60     addclientudp, /* addclient */
61     addserverextraudp, /* addserverextra */
62     udpsetsrcres, /* setsrcres */
63     initextraudp /* initextra */
64 };
65
66 static int client4_sock = -1;
67 static int client6_sock = -1;
68 static struct gqueue *server_replyq = NULL;
69
70 static struct addrinfo *srcres = NULL;
71 static uint8_t handle;
72 static struct commonprotoopts *protoopts = NULL;
73
74 const struct protodefs *udpinit(uint8_t h) {
75     handle = h;
76     return &protodefs;
77 }
78
79 static void setprotoopts(struct commonprotoopts *opts) {
80     protoopts = opts;
81 }
82
83 static char **getlistenerargs() {
84     return protoopts ? protoopts->listenargs : NULL;
85 }
86
87 void udpsetsrcres() {
88     if (!srcres)
89         srcres =
90             resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
91                                    AF_UNSPEC, NULL, protodefs.socktype);
92 }
93
94 void removeudpclientfromreplyq(struct client *c) {
95     struct list_node *n;
96     struct request *r;
97
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;
102         if (r->from == c)
103             r->from = NULL;
104     }
105     pthread_mutex_unlock(&c->replyq->mutex);
106 }
107
108 static int addr_equal(struct sockaddr *a, struct sockaddr *b) {
109     switch (a->sa_family) {
110     case AF_INET:
111         return !memcmp(&((struct sockaddr_in*)a)->sin_addr,
112                        &((struct sockaddr_in*)b)->sin_addr,
113                        sizeof(struct in_addr));
114     case AF_INET6:
115         return IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6*)a)->sin6_addr,
116                                   &((struct sockaddr_in6*)b)->sin6_addr);
117     default:
118         /* Must not reach */
119         return 0;
120     }
121 }
122
123 uint16_t port_get(struct sockaddr *sa) {
124     switch (sa->sa_family) {
125     case AF_INET:
126         return ntohs(((struct sockaddr_in *)sa)->sin_port);
127     case AF_INET6:
128         return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
129     }
130     return 0;
131 }
132
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) {
137     int cnt, len;
138     unsigned char buf[4], *rad = NULL;
139     struct sockaddr_storage from;
140     struct sockaddr *fromcopy;
141     socklen_t fromlen = sizeof(from);
142     struct clsrvconf *p;
143     struct list_node *node;
144     fd_set readfds;
145     struct client *c = NULL;
146     struct timeval now;
147
148     for (;;) {
149         if (rad) {
150             free(rad);
151             rad = NULL;
152         }
153         FD_ZERO(&readfds);
154         FD_SET(s, &readfds);
155         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
156             continue;
157         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
158         if (cnt == -1) {
159             debug(DBG_WARN, "radudpget: recv failed");
160             continue;
161         }
162
163         p = client
164             ? find_clconf(handle, (struct sockaddr *)&from, NULL)
165             : find_srvconf(handle, (struct sockaddr *)&from, NULL);
166         if (!p) {
167             debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
168             recv(s, buf, 4, 0);
169             continue;
170         }
171
172         len = RADLEN(buf);
173         if (len < 20) {
174             debug(DBG_WARN, "radudpget: length too small");
175             recv(s, buf, 4, 0);
176             continue;
177         }
178
179         rad = malloc(len);
180         if (!rad) {
181             debug(DBG_ERR, "radudpget: malloc failed");
182             recv(s, buf, 4, 0);
183             continue;
184         }
185
186         cnt = recv(s, rad, len, MSG_TRUNC);
187         debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
188
189         if (cnt < len) {
190             debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
191             continue;
192         }
193         if (cnt > len)
194             debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
195
196         if (client) {
197             *client = NULL;
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);
202                 if (s != c->sock)
203                     continue;
204                 gettimeofday(&now, NULL);
205                 if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
206                     c->expiry = now.tv_sec + 60;
207                     *client = c;
208                 }
209                 if (c->expiry >= now.tv_sec)
210                     continue;
211
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);
216                 break;
217             }
218             if (!*client) {
219                 fromcopy = addr_copy((struct sockaddr *)&from);
220                 if (!fromcopy) {
221                     pthread_mutex_unlock(p->lock);
222                     continue;
223                 }
224                 c = addclient(p, 0);
225                 if (!c) {
226                     free(fromcopy);
227                     pthread_mutex_unlock(p->lock);
228                     continue;
229                 }
230                 c->sock = s;
231                 c->addr = fromcopy;
232                 gettimeofday(&now, NULL);
233                 c->expiry = now.tv_sec + 60;
234                 *client = c;
235             }
236             pthread_mutex_unlock(p->lock);
237         } else if (server)
238             *server = p->servers;
239         break;
240     }
241     if (port)
242         *port = port_get((struct sockaddr *)&from);
243     return rad;
244 }
245
246 int clientradputudp(struct server *server, unsigned char *rad) {
247     size_t len;
248     struct clsrvconf *conf = server->conf;
249     struct addrinfo *ai;
250
251     len = RADLEN(rad);
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));
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 gqueue *replyq = (struct gqueue *)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     assert(list_first(conf->hostports) != NULL);
324     switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
325     case AF_INET:
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);
330         }
331         conf->servers->sock = client4_sock;
332         break;
333     case AF_INET6:
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);
338         }
339         conf->servers->sock = client6_sock;
340         break;
341     default:
342         debugx(1, DBG_ERR, "addserver: unsupported address family");
343     }
344 }
345
346 void initextraudp() {
347     pthread_t cl4th, cl6th, srvth;
348
349     if (srcres) {
350         freeaddrinfo(srcres);
351         srcres = NULL;
352     }
353
354     if (client4_sock >= 0)
355         if (pthread_create(&cl4th, &pthread_attr, udpclientrd, (void *)&client4_sock))
356             debugx(1, DBG_ERR, "pthread_create failed");
357     if (client6_sock >= 0)
358         if (pthread_create(&cl6th, &pthread_attr, udpclientrd, (void *)&client6_sock))
359             debugx(1, DBG_ERR, "pthread_create failed");
360
361     if (find_clconf_type(handle, NULL)) {
362         server_replyq = newqueue();
363         if (pthread_create(&srvth, &pthread_attr, udpserverwr, (void *)server_replyq))
364             debugx(1, DBG_ERR, "pthread_create failed");
365     }
366 }
367 #else
368 const struct protodefs *udpinit(uint8_t h) {
369     return NULL;
370 }
371 #endif
372
373 /* Local Variables: */
374 /* c-file-style: "stroustrup" */
375 /* End: */