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
25 #include <freeradius-devel/libradius.h>
28 #include <freeradius-devel/udpfromto.h>
34 * See if two packets are identical.
36 * Note that we do NOT compare the authentication vectors.
37 * That's because if the authentication vector is different,
38 * it means that the NAS has given up on the earlier request.
40 int fr_packet_cmp(RADIUS_PACKET const *a, RADIUS_PACKET const *b)
47 if (a->id < b->id) return -1;
48 if (a->id > b->id) return +1;
50 if (a->sockfd < b->sockfd) return -1;
51 if (a->sockfd > b->sockfd) return +1;
54 * Source ports are pretty much random.
56 rcode = (int) a->src_port - (int) b->src_port;
57 if (rcode != 0) return rcode;
60 * Usually many client IPs, and few server IPs
62 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
63 if (rcode != 0) return rcode;
66 * One socket can receive packets for multiple
67 * destination IPs, so we check that before checking the
70 rcode = fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
71 if (rcode != 0) return rcode;
74 * At this point, the order of comparing socket FDs
75 * and/or destination ports doesn't matter. One of those
76 * fields will make the socket unique, and the other is
77 * pretty much redundant.
79 rcode = (int) a->dst_port - (int) b->dst_port;
83 int fr_inaddr_any(fr_ipaddr_t *ipaddr)
86 if (ipaddr->af == AF_INET) {
87 if (ipaddr->ipaddr.ip4addr.s_addr == INADDR_ANY) {
91 #ifdef HAVE_STRUCT_SOCKADDR_IN6
92 } else if (ipaddr->af == AF_INET6) {
93 if (IN6_IS_ADDR_UNSPECIFIED(&(ipaddr->ipaddr.ip6addr))) {
99 fr_strerror_printf("Unknown address family");
108 * Create a fake "request" from a reply, for later lookup.
110 void fr_request_from_reply(RADIUS_PACKET *request,
111 RADIUS_PACKET const *reply)
113 request->sockfd = reply->sockfd;
114 request->id = reply->id;
116 request->proto = reply->proto;
118 request->src_port = reply->dst_port;
119 request->dst_port = reply->src_port;
120 request->src_ipaddr = reply->dst_ipaddr;
121 request->dst_ipaddr = reply->src_ipaddr;
125 * Open a socket on the given IP and port.
127 int fr_socket(fr_ipaddr_t *ipaddr, uint16_t port)
130 struct sockaddr_storage salocal;
133 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
135 fr_strerror_printf("cannot open socket: %s", fr_syserror(errno));
139 #ifdef WITH_UDPFROMTO
141 * Initialize udpfromto for all sockets.
143 if (udpfromto_init(sockfd) != 0) {
145 fr_strerror_printf("cannot initialize udpfromto: %s", fr_syserror(errno));
150 if (!fr_ipaddr2sockaddr(ipaddr, port, &salocal, &salen)) {
154 #ifdef HAVE_STRUCT_SOCKADDR_IN6
155 if (ipaddr->af == AF_INET6) {
157 * Listening on '::' does NOT get you IPv4 to
158 * IPv6 mapping. You've got to listen on an IPv4
159 * address, too. This makes the rest of the server
160 * design a little simpler.
164 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
167 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
168 (char *)&on, sizeof(on)) < 0) {
170 fr_strerror_printf("Failed setting sockopt "
171 "IPPROTO_IPV6 - IPV6_V6ONLY"
172 ": %s", fr_syserror(errno));
176 #endif /* IPV6_V6ONLY */
178 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
180 #if (defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)) || defined(IP_DONTFRAG)
181 if (ipaddr->af == AF_INET) {
184 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
187 * Disable PMTU discovery. On Linux, this
188 * also makes sure that the "don't fragment"
191 flag = IP_PMTUDISC_DONT;
192 if (setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER,
193 &flag, sizeof(flag)) < 0) {
195 fr_strerror_printf("Failed setting sockopt "
196 "IPPROTO_IP - IP_MTU_DISCOVER: %s",
202 #if defined(IP_DONTFRAG)
204 * Ensure that the "don't fragment" flag is zero.
207 if (setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG,
208 &flag, sizeof(flag)) < 0) {
210 fr_strerror_printf("Failed setting sockopt "
211 "IPPROTO_IP - IP_DONTFRAG: %s",
219 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
221 fr_strerror_printf("cannot bind socket: %s", fr_syserror(errno));
230 * We need to keep track of the socket & it's IP/port.
232 typedef struct fr_packet_socket_t {
236 uint32_t num_outgoing;
239 fr_ipaddr_t src_ipaddr;
243 fr_ipaddr_t dst_ipaddr;
253 } fr_packet_socket_t;
256 #define FNV_MAGIC_PRIME (0x01000193)
257 #define MAX_SOCKETS (256)
258 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
259 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
262 * Structure defining a list of packets (incoming or outgoing)
263 * that should be managed.
265 struct fr_packet_list_t {
269 uint32_t num_outgoing;
273 fr_packet_socket_t sockets[MAX_SOCKETS];
278 * Ugh. Doing this on every sent/received packet is not nice.
280 static fr_packet_socket_t *fr_socket_find(fr_packet_list_t *pl,
285 i = start = SOCK2OFFSET(sockfd);
287 do { /* make this hack slightly more efficient */
288 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
290 i = (i + 1) & SOCKOFFSET_MASK;
291 } while (i != start);
296 bool fr_packet_list_socket_freeze(fr_packet_list_t *pl, int sockfd)
298 fr_packet_socket_t *ps;
301 fr_strerror_printf("Invalid argument");
305 ps = fr_socket_find(pl, sockfd);
307 fr_strerror_printf("No such socket");
315 bool fr_packet_list_socket_thaw(fr_packet_list_t *pl, int sockfd)
317 fr_packet_socket_t *ps;
319 if (!pl) return false;
321 ps = fr_socket_find(pl, sockfd);
322 if (!ps) return false;
324 ps->dont_use = false;
329 bool fr_packet_list_socket_del(fr_packet_list_t *pl, int sockfd)
331 fr_packet_socket_t *ps;
333 if (!pl) return false;
335 ps = fr_socket_find(pl, sockfd);
336 if (!ps) return false;
338 if (ps->num_outgoing != 0) {
339 fr_strerror_printf("socket is still in use");
350 bool fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd, int proto,
351 fr_ipaddr_t *dst_ipaddr, uint16_t dst_port,
355 struct sockaddr_storage src;
356 socklen_t sizeof_src;
357 fr_packet_socket_t *ps;
359 if (!pl || !dst_ipaddr || (dst_ipaddr->af == AF_UNSPEC)) {
360 fr_strerror_printf("Invalid argument");
364 if (pl->num_sockets >= MAX_SOCKETS) {
365 fr_strerror_printf("Too many open sockets");
370 if (proto != IPPROTO_UDP) {
371 fr_strerror_printf("only UDP is supported");
377 i = start = SOCK2OFFSET(sockfd);
380 if (pl->sockets[i].sockfd == -1) {
381 ps = &pl->sockets[i];
385 i = (i + 1) & SOCKOFFSET_MASK;
386 } while (i != start);
389 fr_strerror_printf("All socket entries are full");
393 memset(ps, 0, sizeof(*ps));
400 * Get address family, etc. first, so we know if we
401 * need to do udpfromto.
403 * FIXME: udpfromto also does this, but it's not
404 * a critical problem.
406 sizeof_src = sizeof(src);
407 memset(&src, 0, sizeof_src);
408 if (getsockname(sockfd, (struct sockaddr *) &src,
410 fr_strerror_printf("%s", fr_syserror(errno));
414 if (!fr_sockaddr2ipaddr(&src, sizeof_src, &ps->src_ipaddr,
416 fr_strerror_printf("Failed to get IP");
420 ps->dst_ipaddr = *dst_ipaddr;
421 ps->dst_port = dst_port;
423 ps->src_any = fr_inaddr_any(&ps->src_ipaddr);
424 if (ps->src_any < 0) return false;
426 ps->dst_any = fr_inaddr_any(&ps->dst_ipaddr);
427 if (ps->dst_any < 0) return false;
430 * As the last step before returning.
438 static int packet_entry_cmp(void const *one, void const *two)
440 RADIUS_PACKET const * const *a = one;
441 RADIUS_PACKET const * const *b = two;
443 return fr_packet_cmp(*a, *b);
446 void fr_packet_list_free(fr_packet_list_t *pl)
450 rbtree_free(pl->tree);
456 * Caller is responsible for managing the packet entries.
458 fr_packet_list_t *fr_packet_list_create(int alloc_id)
461 fr_packet_list_t *pl;
463 pl = malloc(sizeof(*pl));
464 if (!pl) return NULL;
465 memset(pl, 0, sizeof(*pl));
467 pl->tree = rbtree_create(NULL, packet_entry_cmp, NULL, 0);
469 fr_packet_list_free(pl);
473 for (i = 0; i < MAX_SOCKETS; i++) {
474 pl->sockets[i].sockfd = -1;
477 pl->alloc_id = alloc_id;
484 * If pl->alloc_id is set, then fr_packet_list_id_alloc() MUST
485 * be called before inserting the packet into the list!
487 bool fr_packet_list_insert(fr_packet_list_t *pl,
488 RADIUS_PACKET **request_p)
490 if (!pl || !request_p || !*request_p) return 0;
492 VERIFY_PACKET(*request_p);
494 return rbtree_insert(pl->tree, request_p);
497 RADIUS_PACKET **fr_packet_list_find(fr_packet_list_t *pl,
498 RADIUS_PACKET *request)
500 if (!pl || !request) return 0;
502 VERIFY_PACKET(request);
504 return rbtree_finddata(pl->tree, &request);
509 * This presumes that the reply has dst_ipaddr && dst_port set up
510 * correctly (i.e. real IP, or "*").
512 RADIUS_PACKET **fr_packet_list_find_byreply(fr_packet_list_t *pl,
513 RADIUS_PACKET *reply)
515 RADIUS_PACKET my_request, *request;
516 fr_packet_socket_t *ps;
518 if (!pl || !reply) return NULL;
520 VERIFY_PACKET(reply);
522 ps = fr_socket_find(pl, reply->sockfd);
523 if (!ps) return NULL;
526 * Initialize request from reply, AND from the source
527 * IP & port of this socket. The client may have bound
528 * the socket to 0, in which case it's some random port,
529 * that is NOT in the original request->src_port.
531 my_request.sockfd = reply->sockfd;
532 my_request.id = reply->id;
536 * TCP sockets are always bound to the correct src/dst IP/port
538 if (ps->proto == IPPROTO_TCP) {
539 my_request.src_ipaddr = reply->dst_ipaddr;
540 my_request.src_port = reply->dst_port;
545 my_request.src_ipaddr = ps->src_ipaddr;
547 my_request.src_ipaddr = reply->dst_ipaddr;
549 my_request.src_port = ps->src_port;
552 my_request.dst_ipaddr = reply->src_ipaddr;
553 my_request.dst_port = reply->src_port;
556 my_request.proto = reply->proto;
558 request = &my_request;
560 return rbtree_finddata(pl->tree, &request);
564 bool fr_packet_list_yank(fr_packet_list_t *pl, RADIUS_PACKET *request)
568 if (!pl || !request) return false;
570 VERIFY_PACKET(request);
572 node = rbtree_find(pl->tree, &request);
573 if (!node) return false;
575 rbtree_delete(pl->tree, node);
579 uint32_t fr_packet_list_num_elements(fr_packet_list_t *pl)
583 return rbtree_num_elements(pl->tree);
588 * 1 == ID was allocated & assigned
589 * 0 == couldn't allocate ID.
591 * Note that this ALSO assigns a socket to use, and updates
592 * packet->request->src_ipaddr && packet->request->src_port
594 * In multi-threaded systems, the calls to id_alloc && id_free
595 * should be protected by a mutex. This does NOT have to be
596 * the same mutex as the one protecting the insert/find/yank
599 * We assume that the packet has dst_ipaddr && dst_port
600 * already initialized. We will use those to find an
601 * outgoing socket. The request MAY also have src_ipaddr set.
603 * We also assume that the sender doesn't care which protocol
606 bool fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto,
607 RADIUS_PACKET **request_p, void **pctx)
609 int i, j, k, fd, id, start_i, start_j, start_k;
611 fr_packet_socket_t *ps= NULL;
612 RADIUS_PACKET *request = *request_p;
614 VERIFY_PACKET(request);
616 if ((request->dst_ipaddr.af == AF_UNSPEC) ||
617 (request->dst_port == 0)) {
618 fr_strerror_printf("No destination address/port specified");
623 if ((proto != 0) && (proto != IPPROTO_UDP)) {
624 fr_strerror_printf("Invalid destination protocol");
630 * Special case: unspec == "don't care"
632 if (request->src_ipaddr.af == AF_UNSPEC) {
633 memset(&request->src_ipaddr, 0, sizeof(request->src_ipaddr));
634 request->src_ipaddr.af = request->dst_ipaddr.af;
637 src_any = fr_inaddr_any(&request->src_ipaddr);
639 fr_strerror_printf("Can't check src_ipaddr");
644 * MUST specify a destination address.
646 if (fr_inaddr_any(&request->dst_ipaddr) != 0) {
647 fr_strerror_printf("Must specify a dst_ipaddr");
652 * FIXME: Go to an LRU system. This prevents ID re-use
653 * for as long as possible. The main problem with that
654 * approach is that it requires us to populate the
655 * LRU/FIFO when we add a new socket, or a new destination,
656 * which can be expensive.
658 * The LRU can be avoided if the caller takes care to free
659 * Id's only when all responses have been received, OR after
662 * Right now, the random approach is almost OK... it's
663 * brute-force over all of the available ID's, BUT using
664 * random numbers for everything spreads the load a bit.
666 * The old method had a hash lookup on allocation AND
667 * on free. The new method has brute-force on allocation,
668 * and near-zero cost on free.
672 start_i = fr_rand() & SOCKOFFSET_MASK;
674 #define ID_i ((i + start_i) & SOCKOFFSET_MASK)
675 for (i = 0; i < MAX_SOCKETS; i++) {
676 if (pl->sockets[ID_i].sockfd == -1) continue; /* paranoia */
678 ps = &(pl->sockets[ID_i]);
681 * This socket is marked as "don't use for new
682 * packets". But we can still receive packets
683 * that are outstanding.
685 if (ps->dont_use) continue;
688 * All IDs are allocated: ignore it.
690 if (ps->num_outgoing == 256) continue;
693 if (ps->proto != proto) continue;
697 * Address families don't match, skip it.
699 if (ps->src_ipaddr.af != request->dst_ipaddr.af) continue;
702 * MUST match dst port, if we have one.
704 if ((ps->dst_port != 0) &&
705 (ps->dst_port != request->dst_port)) continue;
708 * MUST match requested src port, if one has been given.
710 if ((request->src_port != 0) &&
711 (ps->src_port != request->src_port)) continue;
714 * We don't care about the source IP, but this
715 * socket is link local, and the requested
716 * destination is not link local. Ignore it.
718 if (src_any && (ps->src_ipaddr.af == AF_INET) &&
719 (((ps->src_ipaddr.ipaddr.ip4addr.s_addr >> 24) & 0xff) == 127) &&
720 (((request->dst_ipaddr.ipaddr.ip4addr.s_addr >> 24) & 0xff) != 127)) continue;
723 * We're sourcing from *, and they asked for a
724 * specific source address: ignore it.
726 if (ps->src_any && !src_any) continue;
729 * We're sourcing from a specific IP, and they
730 * asked for a source IP that isn't us: ignore
733 if (!ps->src_any && !src_any &&
734 (fr_ipaddr_cmp(&request->src_ipaddr,
735 &ps->src_ipaddr) != 0)) continue;
738 * UDP sockets are allowed to match
739 * destination IPs exactly, OR a socket
740 * with destination * is allowed to match
741 * any requested destination.
743 * TCP sockets must match the destination
744 * exactly. They *always* have dst_any=0,
745 * so the first check always matches.
748 (fr_ipaddr_cmp(&request->dst_ipaddr,
749 &ps->dst_ipaddr) != 0)) continue;
752 * Otherwise, this socket is OK to use.
756 * Look for a free Id, starting from a random number.
758 start_j = fr_rand() & 0x1f;
759 #define ID_j ((j + start_j) & 0x1f)
760 for (j = 0; j < 32; j++) {
761 if (ps->id[ID_j] == 0xff) continue;
764 start_k = fr_rand() & 0x07;
765 #define ID_k ((k + start_k) & 0x07)
766 for (k = 0; k < 8; k++) {
767 if ((ps->id[ID_j] & (1 << ID_k)) != 0) continue;
769 ps->id[ID_j] |= (1 << ID_k);
770 id = (ID_j * 8) + ID_k;
784 * Ask the caller to allocate a new ID.
787 fr_strerror_printf("Failed finding socket, caller must allocate a new one");
792 * Set the ID, source IP, and source port.
796 request->sockfd = ps->sockfd;
797 request->src_ipaddr = ps->src_ipaddr;
798 request->src_port = ps->src_port;
801 * If we managed to insert it, we're done.
803 if (fr_packet_list_insert(pl, request_p)) {
804 if (pctx) *pctx = ps->ctx;
811 * Mark the ID as free. This is the one line from
812 * id_free() that we care about here.
814 ps->id[(request->id >> 3) & 0x1f] &= ~(1 << (request->id & 0x07));
817 request->sockfd = -1;
818 request->src_ipaddr.af = AF_UNSPEC;
819 request->src_port = 0;
825 * Should be called AFTER yanking it from the list, so that
826 * any newly inserted entries don't collide with this one.
828 bool fr_packet_list_id_free(fr_packet_list_t *pl,
829 RADIUS_PACKET *request, bool yank)
831 fr_packet_socket_t *ps;
833 if (!pl || !request) return false;
835 VERIFY_PACKET(request);
837 if (yank && !fr_packet_list_yank(pl, request)) return false;
839 ps = fr_socket_find(pl, request->sockfd);
840 if (!ps) return false;
843 if (!ps->id[(request->id >> 3) & 0x1f] & (1 << (request->id & 0x07))) {
848 ps->id[(request->id >> 3) & 0x1f] &= ~(1 << (request->id & 0x07));
854 request->src_ipaddr.af = AF_UNSPEC; /* id_alloc checks this */
855 request->src_port = 0;
861 * We always walk RBTREE_DELETE_ORDER, which is like RBTREE_IN_ORDER, except that
862 * <0 means error, stop
863 * 0 means OK, continue
864 * 1 means delete current node and stop
865 * 2 means delete current node and continue
867 int fr_packet_list_walk(fr_packet_list_t *pl, void *ctx, rb_walker_t callback)
869 if (!pl || !callback) return 0;
871 return rbtree_walk(pl->tree, RBTREE_DELETE_ORDER, callback, ctx);
874 int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
878 if (!pl || !set) return 0;
882 for (i = 0; i < MAX_SOCKETS; i++) {
883 if (pl->sockets[i].sockfd == -1) continue;
884 FD_SET(pl->sockets[i].sockfd, set);
885 if (pl->sockets[i].sockfd > maxfd) {
886 maxfd = pl->sockets[i].sockfd;
890 if (maxfd < 0) return -1;
896 * Round-robins the receivers, without priority.
898 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
901 RADIUS_PACKET *fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set)
904 RADIUS_PACKET *packet;
906 if (!pl || !set) return NULL;
908 start = pl->last_recv;
911 start &= SOCKOFFSET_MASK;
913 if (pl->sockets[start].sockfd == -1) continue;
915 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
918 if (pl->sockets[start].proto == IPPROTO_TCP) {
919 packet = fr_tcp_recv(pl->sockets[start].sockfd, 0);
922 packet = rad_recv(NULL, pl->sockets[start].sockfd, 0);
923 if (!packet) continue;
926 * Call fr_packet_list_find_byreply(). If it
927 * doesn't find anything, discard the reply.
930 pl->last_recv = start;
932 packet->proto = pl->sockets[start].proto;
935 } while (start != pl->last_recv);
940 uint32_t fr_packet_list_num_incoming(fr_packet_list_t *pl)
942 uint32_t num_elements;
946 num_elements = rbtree_num_elements(pl->tree);
947 if (num_elements < pl->num_outgoing) return 0; /* panic! */
949 return num_elements - pl->num_outgoing;
952 uint32_t fr_packet_list_num_outgoing(fr_packet_list_t *pl)
956 return pl->num_outgoing;
960 * Debug the packet if requested.
962 void fr_packet_header_print(FILE *fp, RADIUS_PACKET *packet, bool received)
964 char src_ipaddr[128];
965 char dst_ipaddr[128];
971 * Client-specific debugging re-prints the input
972 * packet into the client log.
974 * This really belongs in a utility library
976 if (is_radius_code(packet->code)) {
977 fprintf(fp, "%s %s Id %i from %s%s%s:%i to %s%s%s:%i length %zu\n",
978 received ? "Received" : "Sent",
979 fr_packet_codes[packet->code],
981 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
982 inet_ntop(packet->src_ipaddr.af,
983 &packet->src_ipaddr.ipaddr,
984 src_ipaddr, sizeof(src_ipaddr)),
985 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
987 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
988 inet_ntop(packet->dst_ipaddr.af,
989 &packet->dst_ipaddr.ipaddr,
990 dst_ipaddr, sizeof(dst_ipaddr)),
991 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
995 fprintf(fp, "%s code %u Id %i from %s%s%s:%i to %s%s%s:%i length %zu\n",
996 received ? "Received" : "Sent",
999 packet->src_ipaddr.af == AF_INET6 ? "[" : "",
1000 inet_ntop(packet->src_ipaddr.af,
1001 &packet->src_ipaddr.ipaddr,
1002 src_ipaddr, sizeof(src_ipaddr)),
1003 packet->src_ipaddr.af == AF_INET6 ? "]" : "",
1005 packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
1006 inet_ntop(packet->dst_ipaddr.af,
1007 &packet->dst_ipaddr.ipaddr,
1008 dst_ipaddr, sizeof(dst_ipaddr)),
1009 packet->dst_ipaddr.af == AF_INET6 ? "]" : "",