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 static const char rcsid[] = "$Id$";
25 #include <freeradius-devel/autoconf.h>
26 #include <freeradius-devel/libradius.h>
32 * Take the key fields of a request packet, and convert it to a
35 uint32_t lrad_request_packet_hash(const RADIUS_PACKET *packet)
39 if (packet->hash) return packet->hash;
41 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
42 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
43 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
45 hash = lrad_hash_update(&packet->dst_port,
46 sizeof(packet->dst_port), hash);
47 hash = lrad_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 = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
56 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
58 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
59 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
63 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
64 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
66 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
67 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
74 return lrad_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 lrad_reply_packet_hash(const RADIUS_PACKET *packet)
91 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
92 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
93 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
95 hash = lrad_hash_update(&packet->dst_port,
96 sizeof(packet->dst_port), hash);
97 hash = lrad_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 = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
106 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
108 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
109 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
113 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
114 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
116 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
117 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
124 return lrad_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 lrad_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_ipaddr.af < b->dst_ipaddr.af) return -1;
146 if (a->src_ipaddr.af > b->dst_ipaddr.af) return +1;
148 if (a->src_port < b->src_port) return -1;
149 if (a->src_port > b->src_port) return +1;
151 if (a->dst_port < b->dst_port) return -1;
152 if (a->dst_port > b->dst_port) return +1;
154 switch (a->dst_ipaddr.af) {
156 rcode = memcmp(&a->dst_ipaddr.ipaddr.ip4addr,
157 &b->dst_ipaddr.ipaddr.ip4addr,
158 sizeof(a->dst_ipaddr.ipaddr.ip4addr));
159 if (rcode != 0) return rcode;
160 rcode = memcmp(&a->src_ipaddr.ipaddr.ip4addr,
161 &b->src_ipaddr.ipaddr.ip4addr,
162 sizeof(a->src_ipaddr.ipaddr.ip4addr));
163 if (rcode != 0) return rcode;
166 rcode = memcmp(&a->dst_ipaddr.ipaddr.ip6addr,
167 &b->dst_ipaddr.ipaddr.ip6addr,
168 sizeof(a->dst_ipaddr.ipaddr.ip6addr));
169 if (rcode != 0) return rcode;
170 rcode = memcmp(&a->src_ipaddr.ipaddr.ip6addr,
171 &b->src_ipaddr.ipaddr.ip6addr,
172 sizeof(a->src_ipaddr.ipaddr.ip6addr));
173 if (rcode != 0) return rcode;
181 * Everything's equal. Say so.
188 * Create a fake "request" from a reply, for later lookup.
190 void lrad_request_from_reply(RADIUS_PACKET *request,
191 const RADIUS_PACKET *reply)
193 request->sockfd = reply->sockfd;
194 request->id = reply->id;
195 request->src_port = reply->dst_port;
196 request->dst_port = reply->src_port;
197 request->src_ipaddr = reply->dst_ipaddr;
198 request->dst_ipaddr = reply->src_ipaddr;
203 * Open a socket on the given IP and port.
205 int lrad_socket(lrad_ipaddr_t *ipaddr, int port)
208 struct sockaddr salocal;
211 if ((port < 0) || (port > 65535)) {
212 librad_log("Port %d is out of allowed bounds", port);
216 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
218 librad_log("Failed opening socket: %s", strerror(errno));
222 #ifdef WITH_UDPFROMTO
224 * Initialize udpfromto for all sockets.
226 if (udpfromto_init(sockfd) != 0) {
232 if (ipaddr->af == AF_INET) {
233 struct sockaddr_in *sa;
235 sa = (struct sockaddr_in *) &salocal;
236 memset(sa, 0, sizeof(salocal));
237 sa->sin_family = AF_INET;
238 sa->sin_addr = ipaddr->ipaddr.ip4addr;
239 sa->sin_port = htons((uint16_t) port);
242 #ifdef HAVE_STRUCT_SOCKADDR_IN6
243 } else if (ipaddr->af == AF_INET6) {
244 struct sockaddr_in6 *sa;
246 sa = (struct sockaddr_in6 *) &salocal;
247 memset(sa, 0, sizeof(salocal));
248 sa->sin6_family = AF_INET6;
249 sa->sin6_addr = ipaddr->ipaddr.ip6addr;
250 sa->sin6_port = htons((uint16_t) port);
254 * Listening on '::' does NOT get you IPv4 to
255 * IPv6 mapping. You've got to listen on an IPv4
256 * address, too. This makes the rest of the server
257 * design a little simpler.
260 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
263 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
264 (char *)&on, sizeof(on));
266 #endif /* IPV6_V6ONLY */
267 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
269 return sockfd; /* don't bind it */
272 if (bind(sockfd, &salocal, salen) < 0) {
273 librad_log("Bind to address failed: %s", strerror(errno));
283 * We need to keep track of the socket & it's IP/port.
285 typedef struct lrad_packet_socket_t {
290 int offset; /* 0..31 */
292 lrad_ipaddr_t ipaddr;
294 } lrad_packet_socket_t;
297 #define FNV_MAGIC_PRIME (0x01000193)
298 #define MAX_SOCKETS (32)
299 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
300 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
302 #define MAX_QUEUES (8)
305 * Structure defining a list of packets (incoming or outgoing)
306 * that should be managed.
308 struct lrad_packet_list_t {
309 lrad_hash_table_t *ht;
311 lrad_hash_table_t *dst2id_ht;
318 lrad_packet_socket_t sockets[MAX_SOCKETS];
323 * Ugh. Doing this on every sent/received packet is not nice.
325 static lrad_packet_socket_t *lrad_socket_find(lrad_packet_list_t *pl,
330 i = start = SOCK2OFFSET(sockfd);
332 do { /* make this hack slightly more efficient */
333 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
335 i = (i + 1) & SOCKOFFSET_MASK;
336 } while (i != start);
341 int lrad_packet_list_socket_remove(lrad_packet_list_t *pl, int sockfd)
343 lrad_packet_socket_t *ps;
347 ps = lrad_socket_find(pl, sockfd);
351 * FIXME: Allow the caller forcibly discard these?
353 if (ps->num_outgoing != 0) return 0;
356 pl->mask &= ~(1 << ps->offset);
362 int lrad_packet_list_socket_add(lrad_packet_list_t *pl, int sockfd)
365 struct sockaddr_storage src;
366 socklen_t sizeof_src = sizeof(src);
367 lrad_packet_socket_t *ps;
372 i = start = SOCK2OFFSET(sockfd);
375 if (pl->sockets[i].sockfd == -1) {
376 ps = &pl->sockets[i];
381 i = (i + 1) & SOCKOFFSET_MASK;
382 } while (i != start);
388 memset(ps, 0, sizeof(*ps));
393 * Get address family, etc. first, so we know if we
394 * need to do udpfromto.
396 * FIXME: udpfromto also does this, but it's not
397 * a critical problem.
399 memset(&src, 0, sizeof_src);
400 if (getsockname(sockfd, (struct sockaddr *) &src,
406 * Grab IP addresses & ports from the sockaddr.
408 ps->ipaddr.af = src.ss_family;
409 if (src.ss_family == AF_INET) {
410 struct sockaddr_in *s4;
412 s4 = (struct sockaddr_in *)&src;
413 ps->ipaddr.ipaddr.ip4addr = s4->sin_addr;
414 ps->port = ntohs(s4->sin_port);
416 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
420 #ifdef HAVE_STRUCT_SOCKADDR_IN6
421 } else if (src.ss_family == AF_INET6) {
422 struct sockaddr_in6 *s6;
424 s6 = (struct sockaddr_in6 *)&src;
425 ps->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
426 ps->port = ntohs(s6->sin6_port);
428 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
436 pl->mask |= (1 << ps->offset);
441 static uint32_t packet_entry_hash(const void *data)
443 return lrad_request_packet_hash(*(const RADIUS_PACKET * const *) data);
446 static int packet_entry_cmp(const void *one, const void *two)
448 const RADIUS_PACKET * const *a = one;
449 const RADIUS_PACKET * const *b = two;
451 return lrad_packet_cmp(*a, *b);
455 * A particular socket can have 256 RADIUS ID's outstanding to
456 * any one destination IP/port. So we have a structure that
457 * manages destination IP & port, and has an array of 256 ID's.
459 * The only magic here is that we map the socket number (0..256)
460 * into an "internal" socket number 0..31, that we use to set
461 * bits in the ID array. If a bit is 1, then that ID is in use
462 * for that socket, and the request MUST be in the packet hash!
464 * Note that as a minor memory leak, we don't have an API to free
465 * this structure, except when we discard the whole packet list.
466 * This means that if destinations are added and removed, they
467 * won't be removed from this tree.
469 typedef struct lrad_packet_dst2id_t {
470 lrad_ipaddr_t dst_ipaddr;
472 uint32_t id[1]; /* really id[256] */
473 } lrad_packet_dst2id_t;
476 static uint32_t packet_dst2id_hash(const void *data)
479 const lrad_packet_dst2id_t *pd = data;
481 hash = lrad_hash(&pd->dst_port, sizeof(pd->dst_port));
483 switch (pd->dst_ipaddr.af) {
485 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
486 sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
490 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
491 sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
501 static int packet_dst2id_cmp(const void *one, const void *two)
503 const lrad_packet_dst2id_t *a = one;
504 const lrad_packet_dst2id_t *b = two;
506 if (a->dst_port < b->dst_port) return -1;
507 if (a->dst_port > b->dst_port) return +1;
509 switch (a->dst_ipaddr.af) {
511 return memcmp(&a->dst_ipaddr.ipaddr.ip4addr,
512 &b->dst_ipaddr.ipaddr.ip4addr,
513 sizeof(a->dst_ipaddr.ipaddr.ip4addr));
516 return memcmp(&a->dst_ipaddr.ipaddr.ip6addr,
517 &b->dst_ipaddr.ipaddr.ip6addr,
518 sizeof(a->dst_ipaddr.ipaddr.ip6addr));
527 static void packet_ds2id_free(void *data)
529 lrad_packet_dst2id_t *pd = data;
536 void lrad_packet_list_free(lrad_packet_list_t *pl)
540 if (pl->ht) lrad_hash_table_free(pl->ht);
541 if (pl->dst2id_ht) lrad_hash_table_free(pl->dst2id_ht);
546 * Caller is responsible for managing the packet entries.
548 lrad_packet_list_t *lrad_packet_list_create(int alloc_id)
551 lrad_packet_list_t *pl;
553 pl = malloc(sizeof(*pl));
554 if (!pl) return NULL;
555 memset(pl, 0, sizeof(*pl));
557 pl->ht = lrad_hash_table_create(packet_entry_hash,
561 lrad_packet_list_free(pl);
565 for (i = 0; i < MAX_SOCKETS; i++) {
566 pl->sockets[i].sockfd = -1;
572 pl->dst2id_ht = lrad_hash_table_create(packet_dst2id_hash,
575 if (!pl->dst2id_ht) {
576 lrad_packet_list_free(pl);
586 * If pl->alloc_id is set, then lrad_packet_list_id_alloc() MUST
587 * be called before inserting the packet into the list!
589 int lrad_packet_list_insert(lrad_packet_list_t *pl,
590 RADIUS_PACKET **request_p)
592 if (!pl || !request_p || !*request_p) return 0;
594 (*request_p)->hash = lrad_request_packet_hash(*request_p);
596 return lrad_hash_table_insert(pl->ht, request_p);
599 RADIUS_PACKET **lrad_packet_list_find(lrad_packet_list_t *pl,
600 RADIUS_PACKET *request)
602 if (!pl || !request) return 0;
604 return lrad_hash_table_finddata(pl->ht, &request);
609 * This presumes that the reply has dst_ipaddr && dst_port set up
610 * correctly (i.e. real IP, or "*").
612 RADIUS_PACKET **lrad_packet_list_find_byreply(lrad_packet_list_t *pl,
613 RADIUS_PACKET *reply)
615 RADIUS_PACKET my_request, *request;
616 lrad_packet_socket_t *ps;
618 if (!pl || !reply) return NULL;
620 ps = lrad_socket_find(pl, reply->sockfd);
621 if (!ps) return NULL;
624 * Initialize request from reply, AND from the source
625 * IP & port of this socket. The client may have bound
626 * the socket to 0, in which case it's some random port,
627 * that is NOT in the original request->src_port.
629 my_request.sockfd = reply->sockfd;
630 my_request.id = reply->id;
632 if (ps->inaddr_any) {
633 my_request.src_ipaddr = ps->ipaddr;
635 my_request.src_ipaddr = reply->dst_ipaddr;
637 my_request.src_port = ps->port;;
639 my_request.dst_ipaddr = reply->src_ipaddr;
640 my_request.dst_port = reply->src_port;
643 request = &my_request;
645 return lrad_hash_table_finddata(pl->ht, &request);
649 RADIUS_PACKET **lrad_packet_list_yank(lrad_packet_list_t *pl,
650 RADIUS_PACKET *request)
652 if (!pl || !request) return NULL;
654 return lrad_hash_table_yank(pl->ht, &request);
657 int lrad_packet_list_num_elements(lrad_packet_list_t *pl)
661 return lrad_hash_table_num_elements(pl->ht);
666 * 1 == ID was allocated & assigned
667 * 0 == error allocating memory
668 * -1 == all ID's are used, caller should open a new socket.
670 * Note that this ALSO assigns a socket to use, and updates
671 * packet->request->src_ipaddr && packet->request->src_port
673 * In multi-threaded systems, the calls to id_alloc && id_free
674 * should be protected by a mutex. This does NOT have to be
675 * the same mutex as the one protecting the insert/find/yank
678 int lrad_packet_list_id_alloc(lrad_packet_list_t *pl,
679 RADIUS_PACKET *request)
683 lrad_packet_dst2id_t my_pd, *pd;
684 lrad_packet_socket_t *ps;
686 if (!pl || !pl->alloc_id || !request) return 0;
688 my_pd.dst_ipaddr = request->dst_ipaddr;
689 my_pd.dst_port = request->dst_port;
691 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
693 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
696 memcpy(pd, &my_pd, sizeof(*pd) + 255 * sizeof(pd->id[0]));
697 memset(pd->id, 0, 256 * sizeof(pd->id[0]));
699 if (!lrad_hash_table_insert(pl->dst2id_ht, pd)) {
706 * FIXME: Go to an LRU system. This prevents ID re-use
707 * for as long as possible. The main problem with that
708 * approach is that it requires us to populate the
709 * LRU/FIFO when we add a new socket, or a new destination,
710 * which can be expensive.
712 id = start = (int) lrad_rand() & 0xff;
714 while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
717 if (id == start) return -1;
720 free_mask = ~((~pd->id[id]) & pl->mask);
723 for (i = 0; i < MAX_SOCKETS; i++) {
724 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
726 if ((free_mask & (1 << i)) == 0) {
732 if (start < 0) return 0; /* bad error */
734 pd->id[id] |= (1 << start);
735 ps = &pl->sockets[start];
741 * Set the ID, source IP, and source port.
744 request->sockfd = ps->sockfd;
745 request->src_ipaddr = ps->ipaddr;
746 request->src_port = ps->port;
752 * Should be called AFTER yanking it from the list, so that
753 * any newly inserted entries don't collide with this one.
755 int lrad_packet_list_id_free(lrad_packet_list_t *pl,
756 RADIUS_PACKET *request)
758 lrad_packet_socket_t *ps;
759 lrad_packet_dst2id_t my_pd, *pd;
761 if (!pl || !request) return 0;
763 ps = lrad_socket_find(pl, request->sockfd);
766 my_pd.dst_ipaddr = request->dst_ipaddr;
767 my_pd.dst_port = request->dst_port;
769 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
772 pd->id[request->id] &= ~(1 << ps->offset);
773 request->hash = 0; /* invalidate the cached hash */
781 int lrad_packet_list_walk(lrad_packet_list_t *pl, void *ctx,
782 lrad_hash_table_walk_t callback)
784 if (!pl || !callback) return 0;
786 return lrad_hash_table_walk(pl->ht, callback, ctx);
789 int lrad_packet_list_fd_set(lrad_packet_list_t *pl, fd_set *set)
793 if (!pl || !set) return 0;
797 for (i = 0; i < MAX_SOCKETS; i++) {
798 if (pl->sockets[i].sockfd == -1) continue;
799 FD_SET(pl->sockets[i].sockfd, set);
800 if (pl->sockets[i].sockfd > maxfd) {
801 maxfd = pl->sockets[i].sockfd;
805 if (maxfd < 0) return -1;
811 * Round-robins the receivers, without priority.
813 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
816 RADIUS_PACKET *lrad_packet_list_recv(lrad_packet_list_t *pl, fd_set *set)
819 RADIUS_PACKET *packet;
821 if (!pl || !set) return NULL;
823 start = pl->last_recv;
826 start &= SOCKOFFSET_MASK;
828 if (pl->sockets[start].sockfd == -1) continue;
830 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
832 packet = rad_recv(pl->sockets[start].sockfd);
833 if (!packet) continue;
836 * Call lrad_packet_list_find_byreply(). If it
837 * doesn't find anything, discard the reply.
840 pl->last_recv = start;
842 } while (start != pl->last_recv);
847 int lrad_packet_list_num_incoming(lrad_packet_list_t *pl)
853 num_elements = lrad_hash_table_num_elements(pl->ht);
854 if (num_elements < pl->num_outgoing) return 0; /* panic! */
856 return num_elements - pl->num_outgoing;
859 int lrad_packet_list_num_outgoing(lrad_packet_list_t *pl)
863 return pl->num_outgoing;