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 librad_log("Port %d is out of allowed bounds", port);
186 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
191 #ifdef WITH_UDPFROMTO
193 * Initialize udpfromto for all sockets.
195 if (udpfromto_init(sockfd) != 0) {
201 memset(&salocal, 0, sizeof(salocal));
202 if (ipaddr->af == AF_INET) {
203 struct sockaddr_in *sa;
205 sa = (struct sockaddr_in *) &salocal;
206 sa->sin_family = AF_INET;
207 sa->sin_addr = ipaddr->ipaddr.ip4addr;
208 sa->sin_port = htons((uint16_t) port);
211 #ifdef HAVE_STRUCT_SOCKADDR_IN6
212 } else if (ipaddr->af == AF_INET6) {
213 struct sockaddr_in6 *sa;
215 sa = (struct sockaddr_in6 *) &salocal;
216 sa->sin6_family = AF_INET6;
217 sa->sin6_addr = ipaddr->ipaddr.ip6addr;
218 sa->sin6_port = htons((uint16_t) port);
223 * Listening on '::' does NOT get you IPv4 to
224 * IPv6 mapping. You've got to listen on an IPv4
225 * address, too. This makes the rest of the server
226 * design a little simpler.
230 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
233 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
234 (char *)&on, sizeof(on));
236 #endif /* IPV6_V6ONLY */
238 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
240 return sockfd; /* don't bind it */
243 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
253 * We need to keep track of the socket & it's IP/port.
255 typedef struct fr_packet_socket_t {
260 int offset; /* 0..31 */
264 } fr_packet_socket_t;
267 #define FNV_MAGIC_PRIME (0x01000193)
268 #define MAX_SOCKETS (32)
269 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
270 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
272 #define MAX_QUEUES (8)
275 * Structure defining a list of packets (incoming or outgoing)
276 * that should be managed.
278 struct fr_packet_list_t {
281 fr_hash_table_t *dst2id_ht;
288 fr_packet_socket_t sockets[MAX_SOCKETS];
293 * Ugh. Doing this on every sent/received packet is not nice.
295 static fr_packet_socket_t *fr_socket_find(fr_packet_list_t *pl,
300 i = start = SOCK2OFFSET(sockfd);
302 do { /* make this hack slightly more efficient */
303 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
305 i = (i + 1) & SOCKOFFSET_MASK;
306 } while (i != start);
311 int fr_packet_list_socket_remove(fr_packet_list_t *pl, int sockfd)
313 fr_packet_socket_t *ps;
317 ps = fr_socket_find(pl, sockfd);
321 * FIXME: Allow the caller forcibly discard these?
323 if (ps->num_outgoing != 0) return 0;
326 pl->mask &= ~(1 << ps->offset);
332 int fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd)
335 struct sockaddr_storage src;
336 socklen_t sizeof_src = sizeof(src);
337 fr_packet_socket_t *ps;
342 i = start = SOCK2OFFSET(sockfd);
345 if (pl->sockets[i].sockfd == -1) {
346 ps = &pl->sockets[i];
351 i = (i + 1) & SOCKOFFSET_MASK;
352 } while (i != start);
358 memset(ps, 0, sizeof(*ps));
363 * Get address family, etc. first, so we know if we
364 * need to do udpfromto.
366 * FIXME: udpfromto also does this, but it's not
367 * a critical problem.
369 memset(&src, 0, sizeof_src);
370 if (getsockname(sockfd, (struct sockaddr *) &src,
376 * Grab IP addresses & ports from the sockaddr.
378 ps->ipaddr.af = src.ss_family;
379 if (src.ss_family == AF_INET) {
380 struct sockaddr_in *s4;
382 s4 = (struct sockaddr_in *)&src;
383 ps->ipaddr.ipaddr.ip4addr = s4->sin_addr;
384 ps->port = ntohs(s4->sin_port);
386 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
390 #ifdef HAVE_STRUCT_SOCKADDR_IN6
391 } else if (src.ss_family == AF_INET6) {
392 struct sockaddr_in6 *s6;
394 s6 = (struct sockaddr_in6 *)&src;
395 ps->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
396 ps->port = ntohs(s6->sin6_port);
398 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
406 pl->mask |= (1 << ps->offset);
410 static uint32_t packet_entry_hash(const void *data)
412 return fr_request_packet_hash(*(const RADIUS_PACKET * const *) data);
415 static int packet_entry_cmp(const void *one, const void *two)
417 const RADIUS_PACKET * const *a = one;
418 const RADIUS_PACKET * const *b = two;
420 return fr_packet_cmp(*a, *b);
424 * A particular socket can have 256 RADIUS ID's outstanding to
425 * any one destination IP/port. So we have a structure that
426 * manages destination IP & port, and has an array of 256 ID's.
428 * The only magic here is that we map the socket number (0..256)
429 * into an "internal" socket number 0..31, that we use to set
430 * bits in the ID array. If a bit is 1, then that ID is in use
431 * for that socket, and the request MUST be in the packet hash!
433 * Note that as a minor memory leak, we don't have an API to free
434 * this structure, except when we discard the whole packet list.
435 * This means that if destinations are added and removed, they
436 * won't be removed from this tree.
438 typedef struct fr_packet_dst2id_t {
439 fr_ipaddr_t dst_ipaddr;
441 uint32_t id[1]; /* really id[256] */
442 } fr_packet_dst2id_t;
445 static uint32_t packet_dst2id_hash(const void *data)
448 const fr_packet_dst2id_t *pd = data;
450 hash = fr_hash(&pd->dst_port, sizeof(pd->dst_port));
452 switch (pd->dst_ipaddr.af) {
454 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
455 sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
459 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
460 sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
470 static int packet_dst2id_cmp(const void *one, const void *two)
472 const fr_packet_dst2id_t *a = one;
473 const fr_packet_dst2id_t *b = two;
475 if (a->dst_port < b->dst_port) return -1;
476 if (a->dst_port > b->dst_port) return +1;
478 return fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
481 static void packet_dst2id_free(void *data)
487 void fr_packet_list_free(fr_packet_list_t *pl)
491 fr_hash_table_free(pl->ht);
492 fr_hash_table_free(pl->dst2id_ht);
498 * Caller is responsible for managing the packet entries.
500 fr_packet_list_t *fr_packet_list_create(int alloc_id)
503 fr_packet_list_t *pl;
505 pl = malloc(sizeof(*pl));
506 if (!pl) return NULL;
507 memset(pl, 0, sizeof(*pl));
509 pl->ht = fr_hash_table_create(packet_entry_hash,
513 fr_packet_list_free(pl);
517 for (i = 0; i < MAX_SOCKETS; i++) {
518 pl->sockets[i].sockfd = -1;
524 pl->dst2id_ht = fr_hash_table_create(packet_dst2id_hash,
527 if (!pl->dst2id_ht) {
528 fr_packet_list_free(pl);
538 * If pl->alloc_id is set, then fr_packet_list_id_alloc() MUST
539 * be called before inserting the packet into the list!
541 int fr_packet_list_insert(fr_packet_list_t *pl,
542 RADIUS_PACKET **request_p)
544 if (!pl || !request_p || !*request_p) return 0;
546 (*request_p)->hash = fr_request_packet_hash(*request_p);
548 return fr_hash_table_insert(pl->ht, request_p);
551 RADIUS_PACKET **fr_packet_list_find(fr_packet_list_t *pl,
552 RADIUS_PACKET *request)
554 if (!pl || !request) return 0;
556 return fr_hash_table_finddata(pl->ht, &request);
561 * This presumes that the reply has dst_ipaddr && dst_port set up
562 * correctly (i.e. real IP, or "*").
564 RADIUS_PACKET **fr_packet_list_find_byreply(fr_packet_list_t *pl,
565 RADIUS_PACKET *reply)
567 RADIUS_PACKET my_request, *request;
568 fr_packet_socket_t *ps;
570 if (!pl || !reply) return NULL;
572 ps = fr_socket_find(pl, reply->sockfd);
573 if (!ps) return NULL;
576 * Initialize request from reply, AND from the source
577 * IP & port of this socket. The client may have bound
578 * the socket to 0, in which case it's some random port,
579 * that is NOT in the original request->src_port.
581 my_request.sockfd = reply->sockfd;
582 my_request.id = reply->id;
584 if (ps->inaddr_any) {
585 my_request.src_ipaddr = ps->ipaddr;
587 my_request.src_ipaddr = reply->dst_ipaddr;
589 my_request.src_port = ps->port;;
591 my_request.dst_ipaddr = reply->src_ipaddr;
592 my_request.dst_port = reply->src_port;
595 request = &my_request;
597 return fr_hash_table_finddata(pl->ht, &request);
601 RADIUS_PACKET **fr_packet_list_yank(fr_packet_list_t *pl,
602 RADIUS_PACKET *request)
604 if (!pl || !request) return NULL;
606 return fr_hash_table_yank(pl->ht, &request);
609 int fr_packet_list_num_elements(fr_packet_list_t *pl)
613 return fr_hash_table_num_elements(pl->ht);
618 * 1 == ID was allocated & assigned
619 * 0 == error allocating memory
620 * -1 == all ID's are used, caller should open a new socket.
622 * Note that this ALSO assigns a socket to use, and updates
623 * packet->request->src_ipaddr && packet->request->src_port
625 * In multi-threaded systems, the calls to id_alloc && id_free
626 * should be protected by a mutex. This does NOT have to be
627 * the same mutex as the one protecting the insert/find/yank
630 int fr_packet_list_id_alloc(fr_packet_list_t *pl,
631 RADIUS_PACKET *request)
635 fr_packet_dst2id_t my_pd, *pd;
636 fr_packet_socket_t *ps;
638 if (!pl || !pl->alloc_id || !request) return 0;
640 my_pd.dst_ipaddr = request->dst_ipaddr;
641 my_pd.dst_port = request->dst_port;
643 pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
645 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
648 memset(pd, 0, sizeof(*pd) + 255 * sizeof(pd->id[0]));
650 pd->dst_ipaddr = request->dst_ipaddr;
651 pd->dst_port = request->dst_port;
653 if (!fr_hash_table_insert(pl->dst2id_ht, pd)) {
660 * FIXME: Go to an LRU system. This prevents ID re-use
661 * for as long as possible. The main problem with that
662 * approach is that it requires us to populate the
663 * LRU/FIFO when we add a new socket, or a new destination,
664 * which can be expensive.
666 * The LRU can be avoided if the caller takes care to free
667 * Id's only when all responses have been received, OR after
670 id = start = (int) fr_rand() & 0xff;
672 while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
675 if (id == start) return 0;
678 free_mask = ~((~pd->id[id]) & pl->mask);
681 for (i = 0; i < MAX_SOCKETS; i++) {
682 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
684 if ((free_mask & (1 << i)) == 0) {
690 if (start < 0) return 0; /* bad error */
692 pd->id[id] |= (1 << start);
693 ps = &pl->sockets[start];
699 * Set the ID, source IP, and source port.
703 request->sockfd = ps->sockfd;
704 request->src_ipaddr = ps->ipaddr;
705 request->src_port = ps->port;
711 * Should be called AFTER yanking it from the list, so that
712 * any newly inserted entries don't collide with this one.
714 int fr_packet_list_id_free(fr_packet_list_t *pl,
715 RADIUS_PACKET *request)
717 fr_packet_socket_t *ps;
718 fr_packet_dst2id_t my_pd, *pd;
720 if (!pl || !request) return 0;
722 ps = fr_socket_find(pl, request->sockfd);
725 my_pd.dst_ipaddr = request->dst_ipaddr;
726 my_pd.dst_port = request->dst_port;
728 pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
731 pd->id[request->id] &= ~(1 << ps->offset);
732 request->hash = 0; /* invalidate the cached hash */
740 int fr_packet_list_walk(fr_packet_list_t *pl, void *ctx,
741 fr_hash_table_walk_t callback)
743 if (!pl || !callback) return 0;
745 return fr_hash_table_walk(pl->ht, callback, ctx);
748 int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
752 if (!pl || !set) return 0;
756 for (i = 0; i < MAX_SOCKETS; i++) {
757 if (pl->sockets[i].sockfd == -1) continue;
758 FD_SET(pl->sockets[i].sockfd, set);
759 if (pl->sockets[i].sockfd > maxfd) {
760 maxfd = pl->sockets[i].sockfd;
764 if (maxfd < 0) return -1;
770 * Round-robins the receivers, without priority.
772 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
775 RADIUS_PACKET *fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set)
778 RADIUS_PACKET *packet;
780 if (!pl || !set) return NULL;
782 start = pl->last_recv;
785 start &= SOCKOFFSET_MASK;
787 if (pl->sockets[start].sockfd == -1) continue;
789 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
791 packet = rad_recv(pl->sockets[start].sockfd, 0);
792 if (!packet) continue;
795 * Call fr_packet_list_find_byreply(). If it
796 * doesn't find anything, discard the reply.
799 pl->last_recv = start;
801 } while (start != pl->last_recv);
806 int fr_packet_list_num_incoming(fr_packet_list_t *pl)
812 num_elements = fr_hash_table_num_elements(pl->ht);
813 if (num_elements < pl->num_outgoing) return 0; /* panic! */
815 return num_elements - pl->num_outgoing;
818 int fr_packet_list_num_outgoing(fr_packet_list_t *pl)
822 return pl->num_outgoing;