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>
35 * Take the key fields of a request packet, and convert it to a
38 uint32_t fr_request_packet_hash(const RADIUS_PACKET *packet)
42 if (packet->hash) return packet->hash;
44 hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
45 hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
47 hash = fr_hash_update(&packet->dst_port,
48 sizeof(packet->dst_port), hash);
49 hash = fr_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 = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
58 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
60 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
61 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
65 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
66 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
68 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
69 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
76 return fr_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 fr_reply_packet_hash(const RADIUS_PACKET *packet)
93 hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
94 hash = fr_hash_update(&packet->id, sizeof(packet->id), hash);
95 hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
97 hash = fr_hash_update(&packet->dst_port,
98 sizeof(packet->dst_port), hash);
99 hash = fr_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 = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
108 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
110 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
111 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
115 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
116 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
118 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
119 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
126 return fr_hash_update(&packet->id, sizeof(packet->id), hash);
131 * See if two packets are identical.
133 * Note that we do NOT compare the authentication vectors.
134 * That's because if the authentication vector is different,
135 * it means that the NAS has given up on the earlier request.
137 int fr_packet_cmp(const RADIUS_PACKET *a, const RADIUS_PACKET *b)
141 rcode = a->id - b->id;
142 if (rcode != 0) return rcode;
144 rcode = (int) a->src_port - (int) b->src_port;
145 if (rcode != 0) return rcode;
147 rcode = (int) a->dst_port - (int) b->dst_port;
148 if (rcode != 0) return rcode;
150 rcode = a->sockfd - b->sockfd;
151 if (rcode != 0) return rcode;
153 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
154 if (rcode != 0) return rcode;
156 return fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
159 int fr_inaddr_any(fr_ipaddr_t *ipaddr)
162 if (ipaddr->af == AF_INET) {
163 if (ipaddr->ipaddr.ip4addr.s_addr == INADDR_ANY) {
167 #ifdef HAVE_STRUCT_SOCKADDR_IN6
168 } else if (ipaddr->af == AF_INET6) {
169 if (IN6_IS_ADDR_UNSPECIFIED(&(ipaddr->ipaddr.ip6addr))) {
175 fr_strerror_printf("Unknown address family");
184 * Create a fake "request" from a reply, for later lookup.
186 void fr_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;
198 int fr_nonblock(UNUSED int fd)
204 flags = fcntl(fd, F_GETFL, NULL);
207 return fcntl(fd, F_SETFL, flags);
214 * Open a socket on the given IP and port.
216 int fr_socket(fr_ipaddr_t *ipaddr, int port)
219 struct sockaddr_storage salocal;
222 if ((port < 0) || (port > 65535)) {
223 fr_strerror_printf("Port %d is out of allowed bounds", port);
227 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
229 fr_strerror_printf("cannot open socket: %s", strerror(errno));
233 #ifdef WITH_UDPFROMTO
235 * Initialize udpfromto for all sockets.
237 if (udpfromto_init(sockfd) != 0) {
239 fr_strerror_printf("cannot initialize udpfromto: %s", strerror(errno));
244 if (fr_nonblock(sockfd) < 0) {
249 if (!fr_ipaddr2sockaddr(ipaddr, port, &salocal, &salen)) {
253 #ifdef HAVE_STRUCT_SOCKADDR_IN6
254 if (ipaddr->af == AF_INET6) {
256 * Listening on '::' does NOT get you IPv4 to
257 * IPv6 mapping. You've got to listen on an IPv4
258 * address, too. This makes the rest of the server
259 * design a little simpler.
263 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
266 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
267 (char *)&on, sizeof(on));
269 #endif /* IPV6_V6ONLY */
271 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
273 if (ipaddr->af == AF_INET) {
276 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
278 * Disable PMTU discovery. On Linux, this
279 * also makes sure that the "don't fragment"
282 flag = IP_PMTUDISC_DONT;
283 setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER,
284 &flag, sizeof(flag));
287 #if defined(IP_DONTFRAG)
289 * Ensure that the "don't fragment" flag is zero.
292 setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG,
293 &flag, sizeof(flag));
297 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
299 fr_strerror_printf("cannot bind socket: %s", strerror(errno));
308 * We need to keep track of the socket & it's IP/port.
310 typedef struct fr_packet_socket_t {
317 fr_ipaddr_t src_ipaddr;
321 fr_ipaddr_t dst_ipaddr;
331 } fr_packet_socket_t;
334 #define FNV_MAGIC_PRIME (0x01000193)
335 #define MAX_SOCKETS (256)
336 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
337 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
339 #define MAX_QUEUES (8)
342 * Structure defining a list of packets (incoming or outgoing)
343 * that should be managed.
345 struct fr_packet_list_t {
353 fr_packet_socket_t sockets[MAX_SOCKETS];
358 * Ugh. Doing this on every sent/received packet is not nice.
360 static fr_packet_socket_t *fr_socket_find(fr_packet_list_t *pl,
365 i = start = SOCK2OFFSET(sockfd);
367 do { /* make this hack slightly more efficient */
368 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
370 i = (i + 1) & SOCKOFFSET_MASK;
371 } while (i != start);
376 int fr_packet_list_socket_freeze(fr_packet_list_t *pl, int sockfd)
378 fr_packet_socket_t *ps;
381 fr_strerror_printf("Invalid argument");
385 ps = fr_socket_find(pl, sockfd);
387 fr_strerror_printf("No such socket");
395 int fr_packet_list_socket_thaw(fr_packet_list_t *pl, int sockfd)
397 fr_packet_socket_t *ps;
401 ps = fr_socket_find(pl, sockfd);
408 int fr_packet_list_socket_remove(fr_packet_list_t *pl, int sockfd,
411 fr_packet_socket_t *ps;
415 ps = fr_socket_find(pl, sockfd);
419 * FIXME: Allow the caller forcibly discard these?
421 if (ps->num_outgoing != 0) return 0;
425 if (pctx) *pctx = ps->ctx;
430 int fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd, int proto,
431 fr_ipaddr_t *dst_ipaddr, int dst_port,
435 struct sockaddr_storage src;
436 socklen_t sizeof_src;
437 fr_packet_socket_t *ps;
439 if (!pl || !dst_ipaddr || (dst_ipaddr->af == AF_UNSPEC)) {
440 fr_strerror_printf("Invalid argument");
444 if (pl->num_sockets >= MAX_SOCKETS) {
445 fr_strerror_printf("Too many open sockets");
450 if (proto != IPPROTO_UDP) {
451 fr_strerror_printf("only UDP is supported");
457 i = start = SOCK2OFFSET(sockfd);
460 if (pl->sockets[i].sockfd == -1) {
461 ps = &pl->sockets[i];
465 i = (i + 1) & SOCKOFFSET_MASK;
466 } while (i != start);
469 fr_strerror_printf("All socket entries are full");
473 memset(ps, 0, sizeof(*ps));
480 * Get address family, etc. first, so we know if we
481 * need to do udpfromto.
483 * FIXME: udpfromto also does this, but it's not
484 * a critical problem.
486 sizeof_src = sizeof(src);
487 memset(&src, 0, sizeof_src);
488 if (getsockname(sockfd, (struct sockaddr *) &src,
490 fr_strerror_printf("%s", strerror(errno));
494 if (!fr_sockaddr2ipaddr(&src, sizeof_src, &ps->src_ipaddr,
496 fr_strerror_printf("Failed to get IP");
500 ps->dst_ipaddr = *dst_ipaddr;
501 ps->dst_port = dst_port;
503 ps->src_any = fr_inaddr_any(&ps->src_ipaddr);
504 if (ps->src_any < 0) return 0;
506 ps->dst_any = fr_inaddr_any(&ps->dst_ipaddr);
507 if (ps->dst_any < 0) return 0;
510 * As the last step before returning.
518 static int packet_entry_cmp(const void *one, const void *two)
520 const RADIUS_PACKET * const *a = one;
521 const RADIUS_PACKET * const *b = two;
523 return fr_packet_cmp(*a, *b);
526 void fr_packet_list_free(fr_packet_list_t *pl)
530 rbtree_free(pl->tree);
536 * Caller is responsible for managing the packet entries.
538 fr_packet_list_t *fr_packet_list_create(int alloc_id)
541 fr_packet_list_t *pl;
543 pl = malloc(sizeof(*pl));
544 if (!pl) return NULL;
545 memset(pl, 0, sizeof(*pl));
547 pl->tree = rbtree_create(packet_entry_cmp, NULL, 0);
549 fr_packet_list_free(pl);
553 for (i = 0; i < MAX_SOCKETS; i++) {
554 pl->sockets[i].sockfd = -1;
557 pl->alloc_id = alloc_id;
564 * If pl->alloc_id is set, then fr_packet_list_id_alloc() MUST
565 * be called before inserting the packet into the list!
567 int fr_packet_list_insert(fr_packet_list_t *pl,
568 RADIUS_PACKET **request_p)
570 if (!pl || !request_p || !*request_p) return 0;
572 return rbtree_insert(pl->tree, request_p);
575 RADIUS_PACKET **fr_packet_list_find(fr_packet_list_t *pl,
576 RADIUS_PACKET *request)
578 if (!pl || !request) return 0;
580 return rbtree_finddata(pl->tree, &request);
585 * This presumes that the reply has dst_ipaddr && dst_port set up
586 * correctly (i.e. real IP, or "*").
588 RADIUS_PACKET **fr_packet_list_find_byreply(fr_packet_list_t *pl,
589 RADIUS_PACKET *reply)
591 RADIUS_PACKET my_request, *request;
592 fr_packet_socket_t *ps;
594 if (!pl || !reply) return NULL;
596 ps = fr_socket_find(pl, reply->sockfd);
597 if (!ps) return NULL;
600 * Initialize request from reply, AND from the source
601 * IP & port of this socket. The client may have bound
602 * the socket to 0, in which case it's some random port,
603 * that is NOT in the original request->src_port.
605 my_request.sockfd = reply->sockfd;
606 my_request.id = reply->id;
609 my_request.src_ipaddr = ps->src_ipaddr;
611 my_request.src_ipaddr = reply->dst_ipaddr;
613 my_request.src_port = ps->src_port;
615 my_request.dst_ipaddr = reply->src_ipaddr;
616 my_request.dst_port = reply->src_port;
618 request = &my_request;
620 return rbtree_finddata(pl->tree, &request);
624 void fr_packet_list_yank(fr_packet_list_t *pl, RADIUS_PACKET *request)
628 if (!pl || !request) return;
630 node = rbtree_find(pl->tree, &request);
633 rbtree_delete(pl->tree, node);
636 int fr_packet_list_num_elements(fr_packet_list_t *pl)
640 return rbtree_num_elements(pl->tree);
645 * 1 == ID was allocated & assigned
646 * 0 == error allocating memory
647 * -1 == all ID's are used, caller should open a new socket.
649 * Note that this ALSO assigns a socket to use, and updates
650 * packet->request->src_ipaddr && packet->request->src_port
652 * In multi-threaded systems, the calls to id_alloc && id_free
653 * should be protected by a mutex. This does NOT have to be
654 * the same mutex as the one protecting the insert/find/yank
657 * We assume that the packet has dst_ipaddr && dst_port
658 * already initialized. We will use those to find an
659 * outgoing socket. The request MAY also have src_ipaddr set.
661 * We also assume that the sender doesn't care which protocol
664 int fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto,
665 RADIUS_PACKET *request, void **pctx)
667 int i, j, k, fd, id, start_i, start_j, start_k;
669 fr_packet_socket_t *ps;
671 if ((request->dst_ipaddr.af == AF_UNSPEC) ||
672 (request->dst_port == 0)) {
673 fr_strerror_printf("No destination address/port specified");
678 if ((proto != 0) && (proto != IPPROTO_UDP)) {
679 fr_strerror_printf("Invalid destination protocol");
685 * Special case: unspec == "don't care"
687 if (request->src_ipaddr.af == AF_UNSPEC) {
688 memset(&request->src_ipaddr, 0, sizeof(request->src_ipaddr));
689 request->src_ipaddr.af = request->dst_ipaddr.af;
692 src_any = fr_inaddr_any(&request->src_ipaddr);
693 if (src_any < 0) return 0;
696 * MUST specify a destination address.
698 if (fr_inaddr_any(&request->dst_ipaddr) != 0) return 0;
701 * FIXME: Go to an LRU system. This prevents ID re-use
702 * for as long as possible. The main problem with that
703 * approach is that it requires us to populate the
704 * LRU/FIFO when we add a new socket, or a new destination,
705 * which can be expensive.
707 * The LRU can be avoided if the caller takes care to free
708 * Id's only when all responses have been received, OR after
711 * Right now, the random approach is almost OK... it's
712 * brute-force over all of the available ID's, BUT using
713 * random numbers for everything spreads the load a bit.
715 * The old method had a hash lookup on allocation AND
716 * on free. The new method has brute-force on allocation,
717 * and near-zero cost on free.
721 start_i = fr_rand() & SOCKOFFSET_MASK;
723 #define ID_i ((i + start_i) & SOCKOFFSET_MASK)
724 for (i = 0; i < MAX_SOCKETS; i++) {
725 if (pl->sockets[ID_i].sockfd == -1) continue; /* paranoia */
727 ps = &(pl->sockets[ID_i]);
730 * This socket is marked as "don't use for new
731 * packets". But we can still receive packets
732 * that are outstanding.
734 if (ps->dont_use) continue;
737 * All IDs are allocated: ignore it.
739 if (ps->num_outgoing == 256) continue;
742 if (ps->proto != proto) continue;
746 * Address families don't match, skip it.
748 if (ps->src_ipaddr.af != request->dst_ipaddr.af) continue;
751 * MUST match dst port, if we have one.
753 if ((ps->dst_port != 0) &&
754 (ps->dst_port != request->dst_port)) continue;
757 * MUST match requested src port, if one has been given.
759 if ((request->src_port != 0) &&
760 (ps->src_port != request->src_port)) continue;
763 * We're sourcing from *, and they asked for a
764 * specific source address: ignore it.
766 if (ps->src_any && !src_any) continue;
769 * We're sourcing from a specific IP, and they
770 * asked for a source IP that isn't us: ignore
773 if (!ps->src_any && !src_any &&
774 (fr_ipaddr_cmp(&request->src_ipaddr,
775 &ps->src_ipaddr) != 0)) continue;
778 * UDP sockets are allowed to match
779 * destination IPs exactly, OR a socket
780 * with destination * is allowed to match
781 * any requested destination.
783 * TCP sockets must match the destination
784 * exactly. They *always* have dst_any=0,
785 * so the first check always matches.
788 (fr_ipaddr_cmp(&request->dst_ipaddr,
789 &ps->dst_ipaddr) != 0)) continue;
792 * Otherwise, this socket is OK to use.
796 * Look for a free Id, starting from a random number.
798 start_j = fr_rand() & 0x1f;
799 #define ID_j ((j + start_j) & 0x1f)
800 for (j = 0; j < 32; j++) {
801 if (ps->id[ID_j] == 0xff) continue;
804 start_k = fr_rand() & 0x07;
805 #define ID_k ((k + start_k) & 0x07)
806 for (k = 0; k < 8; k++) {
807 if ((ps->id[ID_j] & (1 << ID_k)) != 0) continue;
809 ps->id[ID_j] |= (1 << ID_k);
810 id = (ID_j * 8) + ID_k;
824 * Ask the caller to allocate a new ID.
826 if (fd < 0) return 0;
832 * Set the ID, source IP, and source port.
836 request->sockfd = ps->sockfd;
837 request->src_ipaddr = ps->src_ipaddr;
838 request->src_port = ps->src_port;
840 if (pctx) *pctx = ps->ctx;
846 * Should be called AFTER yanking it from the list, so that
847 * any newly inserted entries don't collide with this one.
849 int fr_packet_list_id_free(fr_packet_list_t *pl,
850 RADIUS_PACKET *request)
852 fr_packet_socket_t *ps;
854 if (!pl || !request) return 0;
856 ps = fr_socket_find(pl, request->sockfd);
860 if (!ps->id[(request->id >> 3) & 0x1f] & (1 << (request->id & 0x07))) {
865 ps->id[(request->id >> 3) & 0x1f] &= ~(1 << (request->id & 0x07));
873 int fr_packet_list_walk(fr_packet_list_t *pl, void *ctx,
874 fr_hash_table_walk_t callback)
876 if (!pl || !callback) return 0;
878 return rbtree_walk(pl->tree, InOrder, callback, ctx);
881 int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
885 if (!pl || !set) return 0;
889 for (i = 0; i < MAX_SOCKETS; i++) {
890 if (pl->sockets[i].sockfd == -1) continue;
891 FD_SET(pl->sockets[i].sockfd, set);
892 if (pl->sockets[i].sockfd > maxfd) {
893 maxfd = pl->sockets[i].sockfd;
897 if (maxfd < 0) return -1;
903 * Round-robins the receivers, without priority.
905 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
908 RADIUS_PACKET *fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set)
911 RADIUS_PACKET *packet;
913 if (!pl || !set) return NULL;
915 start = pl->last_recv;
918 start &= SOCKOFFSET_MASK;
920 if (pl->sockets[start].sockfd == -1) continue;
922 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
925 if (pl->sockets[start].proto == IPPROTO_TCP) {
926 packet = fr_tcp_recv(pl->sockets[start].sockfd, 0);
929 packet = rad_recv(pl->sockets[start].sockfd, 0);
930 if (!packet) continue;
933 * Call fr_packet_list_find_byreply(). If it
934 * doesn't find anything, discard the reply.
937 pl->last_recv = start;
939 } while (start != pl->last_recv);
944 int fr_packet_list_num_incoming(fr_packet_list_t *pl)
950 num_elements = rbtree_num_elements(pl->tree);
951 if (num_elements < pl->num_outgoing) return 0; /* panic! */
953 return num_elements - pl->num_outgoing;
956 int fr_packet_list_num_outgoing(fr_packet_list_t *pl)
960 return pl->num_outgoing;