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/libradius.h>
29 #include <freeradius-devel/udpfromto.h>
33 * Take the key fields of a request packet, and convert it to a
36 uint32_t fr_request_packet_hash(const RADIUS_PACKET *packet)
40 if (packet->hash) return packet->hash;
42 hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
43 hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
45 hash = fr_hash_update(&packet->dst_port,
46 sizeof(packet->dst_port), hash);
47 hash = fr_hash_update(&packet->src_ipaddr.af,
48 sizeof(packet->src_ipaddr.af), hash);
51 * The caller ensures that src & dst AF are the same.
53 switch (packet->src_ipaddr.af) {
55 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
56 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
58 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
59 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
63 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
64 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
66 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
67 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
74 return fr_hash_update(&packet->id, sizeof(packet->id), hash);
79 * Take the key fields of a reply packet, and convert it to a
82 * i.e. take a reply packet, and find the hash of the request packet
83 * that asked for the reply. To do this, we hash the reverse fields
84 * of the request. e.g. where the request does (src, dst), we do
87 uint32_t fr_reply_packet_hash(const RADIUS_PACKET *packet)
91 hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
92 hash = fr_hash_update(&packet->id, sizeof(packet->id), hash);
93 hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
95 hash = fr_hash_update(&packet->dst_port,
96 sizeof(packet->dst_port), hash);
97 hash = fr_hash_update(&packet->src_ipaddr.af,
98 sizeof(packet->src_ipaddr.af), hash);
101 * The caller ensures that src & dst AF are the same.
103 switch (packet->src_ipaddr.af) {
105 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
106 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
108 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
109 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
113 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
114 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
116 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
117 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
124 return fr_hash_update(&packet->id, sizeof(packet->id), hash);
129 * See if two packets are identical.
131 * Note that we do NOT compare the authentication vectors.
132 * That's because if the authentication vector is different,
133 * it means that the NAS has given up on the earlier request.
135 int fr_packet_cmp(const RADIUS_PACKET *a, const RADIUS_PACKET *b)
139 if (a->sockfd < b->sockfd) return -1;
140 if (a->sockfd > b->sockfd) return +1;
142 if (a->id < b->id) return -1;
143 if (a->id > b->id) return +1;
145 if (a->src_port < b->src_port) return -1;
146 if (a->src_port > b->src_port) return +1;
148 if (a->dst_port < b->dst_port) return -1;
149 if (a->dst_port > b->dst_port) return +1;
151 rcode = fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
152 if (rcode != 0) return rcode;
153 return fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
158 * Create a fake "request" from a reply, for later lookup.
160 void fr_request_from_reply(RADIUS_PACKET *request,
161 const RADIUS_PACKET *reply)
163 request->sockfd = reply->sockfd;
164 request->id = reply->id;
165 request->src_port = reply->dst_port;
166 request->dst_port = reply->src_port;
167 request->src_ipaddr = reply->dst_ipaddr;
168 request->dst_ipaddr = reply->src_ipaddr;
173 * Open a socket on the given IP and port.
175 int fr_socket(fr_ipaddr_t *ipaddr, int port)
178 struct sockaddr_storage salocal;
181 if ((port < 0) || (port > 65535)) {
182 fr_strerror_printf("Port %d is out of allowed bounds", port);
186 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
188 fr_strerror_printf("cannot open socket: %s", strerror(errno));
192 #ifdef WITH_UDPFROMTO
194 * Initialize udpfromto for all sockets.
196 if (udpfromto_init(sockfd) != 0) {
198 fr_strerror_printf("cannot initialize udpfromto: %s", strerror(errno));
204 if (!fr_ipaddr2sockaddr(ipaddr, port, &salocal, &salen)) {
208 #ifdef HAVE_STRUCT_SOCKADDR_IN6
209 if (ipaddr->af == AF_INET6) {
211 * Listening on '::' does NOT get you IPv4 to
212 * IPv6 mapping. You've got to listen on an IPv4
213 * address, too. This makes the rest of the server
214 * design a little simpler.
218 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
221 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
222 (char *)&on, sizeof(on));
224 #endif /* IPV6_V6ONLY */
226 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
228 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
230 fr_strerror_printf("cannot bind socket: %s", strerror(errno));
239 * We need to keep track of the socket & it's IP/port.
241 typedef struct fr_packet_socket_t {
246 int offset; /* 0..31 */
250 } fr_packet_socket_t;
253 #define FNV_MAGIC_PRIME (0x01000193)
254 #define MAX_SOCKETS (32)
255 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
256 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
258 #define MAX_QUEUES (8)
261 * Structure defining a list of packets (incoming or outgoing)
262 * that should be managed.
264 struct fr_packet_list_t {
267 fr_hash_table_t *dst2id_ht;
274 fr_packet_socket_t sockets[MAX_SOCKETS];
279 * Ugh. Doing this on every sent/received packet is not nice.
281 static fr_packet_socket_t *fr_socket_find(fr_packet_list_t *pl,
286 i = start = SOCK2OFFSET(sockfd);
288 do { /* make this hack slightly more efficient */
289 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
291 i = (i + 1) & SOCKOFFSET_MASK;
292 } while (i != start);
297 int fr_packet_list_socket_remove(fr_packet_list_t *pl, int sockfd)
299 fr_packet_socket_t *ps;
303 ps = fr_socket_find(pl, sockfd);
307 * FIXME: Allow the caller forcibly discard these?
309 if (ps->num_outgoing != 0) return 0;
312 pl->mask &= ~(1 << ps->offset);
318 int fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd)
321 struct sockaddr_storage src;
322 socklen_t sizeof_src = sizeof(src);
323 fr_packet_socket_t *ps;
328 i = start = SOCK2OFFSET(sockfd);
331 if (pl->sockets[i].sockfd == -1) {
332 ps = &pl->sockets[i];
337 i = (i + 1) & SOCKOFFSET_MASK;
338 } while (i != start);
344 memset(ps, 0, sizeof(*ps));
349 * Get address family, etc. first, so we know if we
350 * need to do udpfromto.
352 * FIXME: udpfromto also does this, but it's not
353 * a critical problem.
355 memset(&src, 0, sizeof_src);
356 if (getsockname(sockfd, (struct sockaddr *) &src,
361 if (!fr_sockaddr2ipaddr(&src, sizeof_src, &ps->ipaddr, &ps->port)) {
366 * Grab IP addresses & ports from the sockaddr.
368 if (src.ss_family == AF_INET) {
369 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
373 #ifdef HAVE_STRUCT_SOCKADDR_IN6
374 } else if (src.ss_family == AF_INET6) {
375 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
383 pl->mask |= (1 << ps->offset);
387 static uint32_t packet_entry_hash(const void *data)
389 return fr_request_packet_hash(*(const RADIUS_PACKET * const *) data);
392 static int packet_entry_cmp(const void *one, const void *two)
394 const RADIUS_PACKET * const *a = one;
395 const RADIUS_PACKET * const *b = two;
397 return fr_packet_cmp(*a, *b);
401 * A particular socket can have 256 RADIUS ID's outstanding to
402 * any one destination IP/port. So we have a structure that
403 * manages destination IP & port, and has an array of 256 ID's.
405 * The only magic here is that we map the socket number (0..256)
406 * into an "internal" socket number 0..31, that we use to set
407 * bits in the ID array. If a bit is 1, then that ID is in use
408 * for that socket, and the request MUST be in the packet hash!
410 * Note that as a minor memory leak, we don't have an API to free
411 * this structure, except when we discard the whole packet list.
412 * This means that if destinations are added and removed, they
413 * won't be removed from this tree.
415 typedef struct fr_packet_dst2id_t {
416 fr_ipaddr_t dst_ipaddr;
418 uint32_t id[1]; /* really id[256] */
419 } fr_packet_dst2id_t;
422 static uint32_t packet_dst2id_hash(const void *data)
425 const fr_packet_dst2id_t *pd = data;
427 hash = fr_hash(&pd->dst_port, sizeof(pd->dst_port));
429 switch (pd->dst_ipaddr.af) {
431 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
432 sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
436 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
437 sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
447 static int packet_dst2id_cmp(const void *one, const void *two)
449 const fr_packet_dst2id_t *a = one;
450 const fr_packet_dst2id_t *b = two;
452 if (a->dst_port < b->dst_port) return -1;
453 if (a->dst_port > b->dst_port) return +1;
455 return fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
458 static void packet_dst2id_free(void *data)
464 void fr_packet_list_free(fr_packet_list_t *pl)
468 fr_hash_table_free(pl->ht);
469 fr_hash_table_free(pl->dst2id_ht);
475 * Caller is responsible for managing the packet entries.
477 fr_packet_list_t *fr_packet_list_create(int alloc_id)
480 fr_packet_list_t *pl;
482 pl = malloc(sizeof(*pl));
483 if (!pl) return NULL;
484 memset(pl, 0, sizeof(*pl));
486 pl->ht = fr_hash_table_create(packet_entry_hash,
490 fr_packet_list_free(pl);
494 for (i = 0; i < MAX_SOCKETS; i++) {
495 pl->sockets[i].sockfd = -1;
501 pl->dst2id_ht = fr_hash_table_create(packet_dst2id_hash,
504 if (!pl->dst2id_ht) {
505 fr_packet_list_free(pl);
515 * If pl->alloc_id is set, then fr_packet_list_id_alloc() MUST
516 * be called before inserting the packet into the list!
518 int fr_packet_list_insert(fr_packet_list_t *pl,
519 RADIUS_PACKET **request_p)
521 if (!pl || !request_p || !*request_p) return 0;
523 (*request_p)->hash = fr_request_packet_hash(*request_p);
525 return fr_hash_table_insert(pl->ht, request_p);
528 RADIUS_PACKET **fr_packet_list_find(fr_packet_list_t *pl,
529 RADIUS_PACKET *request)
531 if (!pl || !request) return 0;
533 return fr_hash_table_finddata(pl->ht, &request);
538 * This presumes that the reply has dst_ipaddr && dst_port set up
539 * correctly (i.e. real IP, or "*").
541 RADIUS_PACKET **fr_packet_list_find_byreply(fr_packet_list_t *pl,
542 RADIUS_PACKET *reply)
544 RADIUS_PACKET my_request, *request;
545 fr_packet_socket_t *ps;
547 if (!pl || !reply) return NULL;
549 ps = fr_socket_find(pl, reply->sockfd);
550 if (!ps) return NULL;
553 * Initialize request from reply, AND from the source
554 * IP & port of this socket. The client may have bound
555 * the socket to 0, in which case it's some random port,
556 * that is NOT in the original request->src_port.
558 my_request.sockfd = reply->sockfd;
559 my_request.id = reply->id;
561 if (ps->inaddr_any) {
562 my_request.src_ipaddr = ps->ipaddr;
564 my_request.src_ipaddr = reply->dst_ipaddr;
566 my_request.src_port = ps->port;;
568 my_request.dst_ipaddr = reply->src_ipaddr;
569 my_request.dst_port = reply->src_port;
572 request = &my_request;
574 return fr_hash_table_finddata(pl->ht, &request);
578 RADIUS_PACKET **fr_packet_list_yank(fr_packet_list_t *pl,
579 RADIUS_PACKET *request)
581 if (!pl || !request) return NULL;
583 return fr_hash_table_yank(pl->ht, &request);
586 int fr_packet_list_num_elements(fr_packet_list_t *pl)
590 return fr_hash_table_num_elements(pl->ht);
595 * 1 == ID was allocated & assigned
596 * 0 == error allocating memory
597 * -1 == all ID's are used, caller should open a new socket.
599 * Note that this ALSO assigns a socket to use, and updates
600 * packet->request->src_ipaddr && packet->request->src_port
602 * In multi-threaded systems, the calls to id_alloc && id_free
603 * should be protected by a mutex. This does NOT have to be
604 * the same mutex as the one protecting the insert/find/yank
607 int fr_packet_list_id_alloc(fr_packet_list_t *pl,
608 RADIUS_PACKET *request)
610 int i, id, start, fd;
612 fr_packet_dst2id_t my_pd, *pd;
613 fr_packet_socket_t *ps;
615 if (!pl || !pl->alloc_id || !request) return 0;
617 my_pd.dst_ipaddr = request->dst_ipaddr;
618 my_pd.dst_port = request->dst_port;
620 pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
622 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
625 memset(pd, 0, sizeof(*pd) + 255 * sizeof(pd->id[0]));
627 pd->dst_ipaddr = request->dst_ipaddr;
628 pd->dst_port = request->dst_port;
630 if (!fr_hash_table_insert(pl->dst2id_ht, pd)) {
637 * FIXME: Go to an LRU system. This prevents ID re-use
638 * for as long as possible. The main problem with that
639 * approach is that it requires us to populate the
640 * LRU/FIFO when we add a new socket, or a new destination,
641 * which can be expensive.
643 * The LRU can be avoided if the caller takes care to free
644 * Id's only when all responses have been received, OR after
647 id = start = (int) fr_rand() & 0xff;
649 while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
653 if (id == start) return 0;
656 free_mask = ~((~pd->id[id]) & pl->mask);
659 * This ID has at least one socket free. Check the sockets
660 * to see if they are satisfactory for the caller.
663 for (i = 0; i < MAX_SOCKETS; i++) {
664 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
667 * This ID is allocated.
669 if ((free_mask & (1 << i)) != 0) continue;
672 * If the caller cares about the source address,
673 * try to re-use that. This means that the
674 * requested source address is set, AND this
675 * socket wasn't bound to "*", AND the requested
676 * source address is the same as this socket
679 if ((request->src_ipaddr.af != AF_UNSPEC) &&
680 !pl->sockets[i].inaddr_any &&
681 (fr_ipaddr_cmp(&request->src_ipaddr, &pl->sockets[i].ipaddr) != 0)) continue;
684 * They asked for a specific address, and this socket
685 * is bound to a wildcard address. Ignore this one, too.
687 if ((request->src_ipaddr.af != AF_UNSPEC) &&
688 pl->sockets[i].inaddr_any) continue;
695 goto redo; /* keep searching IDs */
698 pd->id[id] |= (1 << fd);
699 ps = &pl->sockets[fd];
705 * Set the ID, source IP, and source port.
709 request->sockfd = ps->sockfd;
710 request->src_ipaddr = ps->ipaddr;
711 request->src_port = ps->port;
717 * Should be called AFTER yanking it from the list, so that
718 * any newly inserted entries don't collide with this one.
720 int fr_packet_list_id_free(fr_packet_list_t *pl,
721 RADIUS_PACKET *request)
723 fr_packet_socket_t *ps;
724 fr_packet_dst2id_t my_pd, *pd;
726 if (!pl || !request) return 0;
728 ps = fr_socket_find(pl, request->sockfd);
731 my_pd.dst_ipaddr = request->dst_ipaddr;
732 my_pd.dst_port = request->dst_port;
734 pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
737 pd->id[request->id] &= ~(1 << ps->offset);
738 request->hash = 0; /* invalidate the cached hash */
746 int fr_packet_list_walk(fr_packet_list_t *pl, void *ctx,
747 fr_hash_table_walk_t callback)
749 if (!pl || !callback) return 0;
751 return fr_hash_table_walk(pl->ht, callback, ctx);
754 int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
758 if (!pl || !set) return 0;
762 for (i = 0; i < MAX_SOCKETS; i++) {
763 if (pl->sockets[i].sockfd == -1) continue;
764 FD_SET(pl->sockets[i].sockfd, set);
765 if (pl->sockets[i].sockfd > maxfd) {
766 maxfd = pl->sockets[i].sockfd;
770 if (maxfd < 0) return -1;
776 * Round-robins the receivers, without priority.
778 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
781 RADIUS_PACKET *fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set)
784 RADIUS_PACKET *packet;
786 if (!pl || !set) return NULL;
788 start = pl->last_recv;
791 start &= SOCKOFFSET_MASK;
793 if (pl->sockets[start].sockfd == -1) continue;
795 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
797 packet = rad_recv(pl->sockets[start].sockfd, 0);
798 if (!packet) continue;
801 * Call fr_packet_list_find_byreply(). If it
802 * doesn't find anything, discard the reply.
805 pl->last_recv = start;
807 } while (start != pl->last_recv);
812 int fr_packet_list_num_incoming(fr_packet_list_t *pl)
818 num_elements = fr_hash_table_num_elements(pl->ht);
819 if (num_elements < pl->num_outgoing) return 0; /* panic! */
821 return num_elements - pl->num_outgoing;
824 int fr_packet_list_num_outgoing(fr_packet_list_t *pl)
828 return pl->num_outgoing;