2 * packet.c Generic packet manipulation functions.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000-2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/autoconf.h>
27 #include <freeradius-devel/missing.h>
28 #include <freeradius-devel/libradius.h>
34 * Take the key fields of a request packet, and convert it to a
37 uint32_t lrad_request_packet_hash(const RADIUS_PACKET *packet)
41 if (packet->hash) return packet->hash;
43 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
44 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
45 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
47 hash = lrad_hash_update(&packet->dst_port,
48 sizeof(packet->dst_port), hash);
49 hash = lrad_hash_update(&packet->src_ipaddr.af,
50 sizeof(packet->src_ipaddr.af), hash);
53 * The caller ensures that src & dst AF are the same.
55 switch (packet->src_ipaddr.af) {
57 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
58 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
60 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
61 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
65 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
66 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
68 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
69 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
76 return lrad_hash_update(&packet->id, sizeof(packet->id), hash);
81 * Take the key fields of a reply packet, and convert it to a
84 * i.e. take a reply packet, and find the hash of the request packet
85 * that asked for the reply. To do this, we hash the reverse fields
86 * of the request. e.g. where the request does (src, dst), we do
89 uint32_t lrad_reply_packet_hash(const RADIUS_PACKET *packet)
93 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
94 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
95 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
97 hash = lrad_hash_update(&packet->dst_port,
98 sizeof(packet->dst_port), hash);
99 hash = lrad_hash_update(&packet->src_ipaddr.af,
100 sizeof(packet->src_ipaddr.af), hash);
103 * The caller ensures that src & dst AF are the same.
105 switch (packet->src_ipaddr.af) {
107 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
108 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
110 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
111 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
115 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
116 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
118 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
119 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
126 return lrad_hash_update(&packet->id, sizeof(packet->id), hash);
130 static int lrad_ipaddr_cmp(const lrad_ipaddr_t *a, const lrad_ipaddr_t *b)
132 if (a->af < b->af) return -1;
133 if (a->af > b->af) return +1;
137 return memcmp(&a->ipaddr.ip4addr,
139 sizeof(a->ipaddr.ip4addr));
142 return memcmp(&a->ipaddr.ip6addr,
144 sizeof(a->ipaddr.ip6addr));
155 * See if two packets are identical.
157 * Note that we do NOT compare the authentication vectors.
158 * That's because if the authentication vector is different,
159 * it means that the NAS has given up on the earlier request.
161 int lrad_packet_cmp(const RADIUS_PACKET *a, const RADIUS_PACKET *b)
165 if (a->sockfd < b->sockfd) return -1;
166 if (a->sockfd > b->sockfd) return +1;
168 if (a->id < b->id) return -1;
169 if (a->id > b->id) return +1;
171 if (a->src_port < b->src_port) return -1;
172 if (a->src_port > b->src_port) return +1;
174 if (a->dst_port < b->dst_port) return -1;
175 if (a->dst_port > b->dst_port) return +1;
177 rcode = lrad_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
178 if (rcode != 0) return rcode;
179 return lrad_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
184 * Create a fake "request" from a reply, for later lookup.
186 void lrad_request_from_reply(RADIUS_PACKET *request,
187 const RADIUS_PACKET *reply)
189 request->sockfd = reply->sockfd;
190 request->id = reply->id;
191 request->src_port = reply->dst_port;
192 request->dst_port = reply->src_port;
193 request->src_ipaddr = reply->dst_ipaddr;
194 request->dst_ipaddr = reply->src_ipaddr;
199 * Open a socket on the given IP and port.
201 int lrad_socket(lrad_ipaddr_t *ipaddr, int port)
204 struct sockaddr_storage salocal;
207 if ((port < 0) || (port > 65535)) {
208 librad_log("Port %d is out of allowed bounds", port);
212 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
217 #ifdef WITH_UDPFROMTO
219 * Initialize udpfromto for all sockets.
221 if (udpfromto_init(sockfd) != 0) {
227 memset(&salocal, 0, sizeof(salocal));
228 if (ipaddr->af == AF_INET) {
229 struct sockaddr_in *sa;
231 sa = (struct sockaddr_in *) &salocal;
232 sa->sin_family = AF_INET;
233 sa->sin_addr = ipaddr->ipaddr.ip4addr;
234 sa->sin_port = htons((uint16_t) port);
237 #ifdef HAVE_STRUCT_SOCKADDR_IN6
238 } else if (ipaddr->af == AF_INET6) {
239 struct sockaddr_in6 *sa;
241 sa = (struct sockaddr_in6 *) &salocal;
242 sa->sin6_family = AF_INET6;
243 sa->sin6_addr = ipaddr->ipaddr.ip6addr;
244 sa->sin6_port = htons((uint16_t) port);
249 * Listening on '::' does NOT get you IPv4 to
250 * IPv6 mapping. You've got to listen on an IPv4
251 * address, too. This makes the rest of the server
252 * design a little simpler.
256 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
259 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
260 (char *)&on, sizeof(on));
262 #endif /* IPV6_V6ONLY */
264 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
266 return sockfd; /* don't bind it */
269 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
279 * We need to keep track of the socket & it's IP/port.
281 typedef struct lrad_packet_socket_t {
286 int offset; /* 0..31 */
288 lrad_ipaddr_t ipaddr;
290 } lrad_packet_socket_t;
293 #define FNV_MAGIC_PRIME (0x01000193)
294 #define MAX_SOCKETS (32)
295 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
296 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
298 #define MAX_QUEUES (8)
301 * Structure defining a list of packets (incoming or outgoing)
302 * that should be managed.
304 struct lrad_packet_list_t {
305 lrad_hash_table_t *ht;
307 lrad_hash_table_t *dst2id_ht;
314 lrad_packet_socket_t sockets[MAX_SOCKETS];
319 * Ugh. Doing this on every sent/received packet is not nice.
321 static lrad_packet_socket_t *lrad_socket_find(lrad_packet_list_t *pl,
326 i = start = SOCK2OFFSET(sockfd);
328 do { /* make this hack slightly more efficient */
329 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
331 i = (i + 1) & SOCKOFFSET_MASK;
332 } while (i != start);
337 int lrad_packet_list_socket_remove(lrad_packet_list_t *pl, int sockfd)
339 lrad_packet_socket_t *ps;
343 ps = lrad_socket_find(pl, sockfd);
347 * FIXME: Allow the caller forcibly discard these?
349 if (ps->num_outgoing != 0) return 0;
352 pl->mask &= ~(1 << ps->offset);
358 int lrad_packet_list_socket_add(lrad_packet_list_t *pl, int sockfd)
361 struct sockaddr_storage src;
362 socklen_t sizeof_src = sizeof(src);
363 lrad_packet_socket_t *ps;
368 i = start = SOCK2OFFSET(sockfd);
371 if (pl->sockets[i].sockfd == -1) {
372 ps = &pl->sockets[i];
377 i = (i + 1) & SOCKOFFSET_MASK;
378 } while (i != start);
384 memset(ps, 0, sizeof(*ps));
389 * Get address family, etc. first, so we know if we
390 * need to do udpfromto.
392 * FIXME: udpfromto also does this, but it's not
393 * a critical problem.
395 memset(&src, 0, sizeof_src);
396 if (getsockname(sockfd, (struct sockaddr *) &src,
402 * Grab IP addresses & ports from the sockaddr.
404 ps->ipaddr.af = src.ss_family;
405 if (src.ss_family == AF_INET) {
406 struct sockaddr_in *s4;
408 s4 = (struct sockaddr_in *)&src;
409 ps->ipaddr.ipaddr.ip4addr = s4->sin_addr;
410 ps->port = ntohs(s4->sin_port);
412 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
416 #ifdef HAVE_STRUCT_SOCKADDR_IN6
417 } else if (src.ss_family == AF_INET6) {
418 struct sockaddr_in6 *s6;
420 s6 = (struct sockaddr_in6 *)&src;
421 ps->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
422 ps->port = ntohs(s6->sin6_port);
424 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
432 pl->mask |= (1 << ps->offset);
436 static uint32_t packet_entry_hash(const void *data)
438 return lrad_request_packet_hash(*(const RADIUS_PACKET * const *) data);
441 static int packet_entry_cmp(const void *one, const void *two)
443 const RADIUS_PACKET * const *a = one;
444 const RADIUS_PACKET * const *b = two;
446 return lrad_packet_cmp(*a, *b);
450 * A particular socket can have 256 RADIUS ID's outstanding to
451 * any one destination IP/port. So we have a structure that
452 * manages destination IP & port, and has an array of 256 ID's.
454 * The only magic here is that we map the socket number (0..256)
455 * into an "internal" socket number 0..31, that we use to set
456 * bits in the ID array. If a bit is 1, then that ID is in use
457 * for that socket, and the request MUST be in the packet hash!
459 * Note that as a minor memory leak, we don't have an API to free
460 * this structure, except when we discard the whole packet list.
461 * This means that if destinations are added and removed, they
462 * won't be removed from this tree.
464 typedef struct lrad_packet_dst2id_t {
465 lrad_ipaddr_t dst_ipaddr;
467 uint32_t id[1]; /* really id[256] */
468 } lrad_packet_dst2id_t;
471 static uint32_t packet_dst2id_hash(const void *data)
474 const lrad_packet_dst2id_t *pd = data;
476 hash = lrad_hash(&pd->dst_port, sizeof(pd->dst_port));
478 switch (pd->dst_ipaddr.af) {
480 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
481 sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
485 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
486 sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
496 static int packet_dst2id_cmp(const void *one, const void *two)
498 const lrad_packet_dst2id_t *a = one;
499 const lrad_packet_dst2id_t *b = two;
501 if (a->dst_port < b->dst_port) return -1;
502 if (a->dst_port > b->dst_port) return +1;
504 return lrad_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
507 static void packet_dst2id_free(void *data)
513 void lrad_packet_list_free(lrad_packet_list_t *pl)
517 if (pl->ht) lrad_hash_table_free(pl->ht);
518 if (pl->dst2id_ht) lrad_hash_table_free(pl->dst2id_ht);
524 * Caller is responsible for managing the packet entries.
526 lrad_packet_list_t *lrad_packet_list_create(int alloc_id)
529 lrad_packet_list_t *pl;
531 pl = malloc(sizeof(*pl));
532 if (!pl) return NULL;
533 memset(pl, 0, sizeof(*pl));
535 pl->ht = lrad_hash_table_create(packet_entry_hash,
539 lrad_packet_list_free(pl);
543 for (i = 0; i < MAX_SOCKETS; i++) {
544 pl->sockets[i].sockfd = -1;
550 pl->dst2id_ht = lrad_hash_table_create(packet_dst2id_hash,
553 if (!pl->dst2id_ht) {
554 lrad_packet_list_free(pl);
564 * If pl->alloc_id is set, then lrad_packet_list_id_alloc() MUST
565 * be called before inserting the packet into the list!
567 int lrad_packet_list_insert(lrad_packet_list_t *pl,
568 RADIUS_PACKET **request_p)
570 if (!pl || !request_p || !*request_p) return 0;
572 (*request_p)->hash = lrad_request_packet_hash(*request_p);
574 return lrad_hash_table_insert(pl->ht, request_p);
577 RADIUS_PACKET **lrad_packet_list_find(lrad_packet_list_t *pl,
578 RADIUS_PACKET *request)
580 if (!pl || !request) return 0;
582 return lrad_hash_table_finddata(pl->ht, &request);
587 * This presumes that the reply has dst_ipaddr && dst_port set up
588 * correctly (i.e. real IP, or "*").
590 RADIUS_PACKET **lrad_packet_list_find_byreply(lrad_packet_list_t *pl,
591 RADIUS_PACKET *reply)
593 RADIUS_PACKET my_request, *request;
594 lrad_packet_socket_t *ps;
596 if (!pl || !reply) return NULL;
598 ps = lrad_socket_find(pl, reply->sockfd);
599 if (!ps) return NULL;
602 * Initialize request from reply, AND from the source
603 * IP & port of this socket. The client may have bound
604 * the socket to 0, in which case it's some random port,
605 * that is NOT in the original request->src_port.
607 my_request.sockfd = reply->sockfd;
608 my_request.id = reply->id;
610 if (ps->inaddr_any) {
611 my_request.src_ipaddr = ps->ipaddr;
613 my_request.src_ipaddr = reply->dst_ipaddr;
615 my_request.src_port = ps->port;;
617 my_request.dst_ipaddr = reply->src_ipaddr;
618 my_request.dst_port = reply->src_port;
621 request = &my_request;
623 return lrad_hash_table_finddata(pl->ht, &request);
627 RADIUS_PACKET **lrad_packet_list_yank(lrad_packet_list_t *pl,
628 RADIUS_PACKET *request)
630 if (!pl || !request) return NULL;
632 return lrad_hash_table_yank(pl->ht, &request);
635 int lrad_packet_list_num_elements(lrad_packet_list_t *pl)
639 return lrad_hash_table_num_elements(pl->ht);
644 * 1 == ID was allocated & assigned
645 * 0 == error allocating memory
646 * -1 == all ID's are used, caller should open a new socket.
648 * Note that this ALSO assigns a socket to use, and updates
649 * packet->request->src_ipaddr && packet->request->src_port
651 * In multi-threaded systems, the calls to id_alloc && id_free
652 * should be protected by a mutex. This does NOT have to be
653 * the same mutex as the one protecting the insert/find/yank
656 int lrad_packet_list_id_alloc(lrad_packet_list_t *pl,
657 RADIUS_PACKET *request)
661 lrad_packet_dst2id_t my_pd, *pd;
662 lrad_packet_socket_t *ps;
664 if (!pl || !pl->alloc_id || !request) return 0;
666 my_pd.dst_ipaddr = request->dst_ipaddr;
667 my_pd.dst_port = request->dst_port;
669 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
671 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
674 memset(pd, 0, sizeof(*pd) + 255 * sizeof(pd->id[0]));
676 if (!lrad_hash_table_insert(pl->dst2id_ht, pd)) {
683 * FIXME: Go to an LRU system. This prevents ID re-use
684 * for as long as possible. The main problem with that
685 * approach is that it requires us to populate the
686 * LRU/FIFO when we add a new socket, or a new destination,
687 * which can be expensive.
689 id = start = (int) lrad_rand() & 0xff;
691 while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
694 if (id == start) return -1;
697 free_mask = ~((~pd->id[id]) & pl->mask);
700 for (i = 0; i < MAX_SOCKETS; i++) {
701 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
703 if ((free_mask & (1 << i)) == 0) {
709 if (start < 0) return 0; /* bad error */
711 pd->id[id] |= (1 << start);
712 ps = &pl->sockets[start];
718 * Set the ID, source IP, and source port.
721 request->sockfd = ps->sockfd;
722 request->src_ipaddr = ps->ipaddr;
723 request->src_port = ps->port;
729 * Should be called AFTER yanking it from the list, so that
730 * any newly inserted entries don't collide with this one.
732 int lrad_packet_list_id_free(lrad_packet_list_t *pl,
733 RADIUS_PACKET *request)
735 lrad_packet_socket_t *ps;
736 lrad_packet_dst2id_t my_pd, *pd;
738 if (!pl || !request) return 0;
740 ps = lrad_socket_find(pl, request->sockfd);
743 my_pd.dst_ipaddr = request->dst_ipaddr;
744 my_pd.dst_port = request->dst_port;
746 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
749 pd->id[request->id] &= ~(1 << ps->offset);
750 request->hash = 0; /* invalidate the cached hash */
758 int lrad_packet_list_walk(lrad_packet_list_t *pl, void *ctx,
759 lrad_hash_table_walk_t callback)
761 if (!pl || !callback) return 0;
763 return lrad_hash_table_walk(pl->ht, callback, ctx);
766 int lrad_packet_list_fd_set(lrad_packet_list_t *pl, fd_set *set)
770 if (!pl || !set) return 0;
774 for (i = 0; i < MAX_SOCKETS; i++) {
775 if (pl->sockets[i].sockfd == -1) continue;
776 FD_SET(pl->sockets[i].sockfd, set);
777 if (pl->sockets[i].sockfd > maxfd) {
778 maxfd = pl->sockets[i].sockfd;
782 if (maxfd < 0) return -1;
788 * Round-robins the receivers, without priority.
790 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
793 RADIUS_PACKET *lrad_packet_list_recv(lrad_packet_list_t *pl, fd_set *set)
796 RADIUS_PACKET *packet;
798 if (!pl || !set) return NULL;
800 start = pl->last_recv;
803 start &= SOCKOFFSET_MASK;
805 if (pl->sockets[start].sockfd == -1) continue;
807 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
809 packet = rad_recv(pl->sockets[start].sockfd);
810 if (!packet) continue;
813 * Call lrad_packet_list_find_byreply(). If it
814 * doesn't find anything, discard the reply.
817 pl->last_recv = start;
819 } while (start != pl->last_recv);
824 int lrad_packet_list_num_incoming(lrad_packet_list_t *pl)
830 num_elements = lrad_hash_table_num_elements(pl->ht);
831 if (num_elements < pl->num_outgoing) return 0; /* panic! */
833 return num_elements - pl->num_outgoing;
836 int lrad_packet_list_num_outgoing(lrad_packet_list_t *pl)
840 return pl->num_outgoing;