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 * Take the key fields of a request packet, and convert it to a
32 uint32_t lrad_request_packet_hash(const RADIUS_PACKET *packet)
36 if (packet->hash) return packet->hash;
38 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
39 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
40 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
42 hash = lrad_hash_update(&packet->dst_port,
43 sizeof(packet->dst_port), hash);
44 hash = lrad_hash_update(&packet->src_ipaddr.af,
45 sizeof(packet->src_ipaddr.af), hash);
48 * The caller ensures that src & dst AF are the same.
50 switch (packet->src_ipaddr.af) {
52 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
53 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
55 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
56 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
60 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
61 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
63 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
64 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
71 return lrad_hash_update(&packet->id, sizeof(packet->id), hash);
76 * Take the key fields of a reply packet, and convert it to a
79 * i.e. take a reply packet, and find the hash of the request packet
80 * that asked for the reply. To do this, we hash the reverse fields
81 * of the request. e.g. where the request does (src, dst), we do
84 uint32_t lrad_reply_packet_hash(const RADIUS_PACKET *packet)
88 hash = lrad_hash(&packet->sockfd, sizeof(packet->sockfd));
89 hash = lrad_hash_update(&packet->id, sizeof(packet->id), hash);
90 hash = lrad_hash_update(&packet->src_port, sizeof(packet->src_port),
92 hash = lrad_hash_update(&packet->dst_port,
93 sizeof(packet->dst_port), hash);
94 hash = lrad_hash_update(&packet->src_ipaddr.af,
95 sizeof(packet->src_ipaddr.af), hash);
98 * The caller ensures that src & dst AF are the same.
100 switch (packet->src_ipaddr.af) {
102 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
103 sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
105 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
106 sizeof(packet->src_ipaddr.ipaddr.ip4addr),
110 hash = lrad_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
111 sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
113 hash = lrad_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
114 sizeof(packet->src_ipaddr.ipaddr.ip6addr),
121 return lrad_hash_update(&packet->id, sizeof(packet->id), hash);
125 static int lrad_ipaddr_cmp(const lrad_ipaddr_t *a, const lrad_ipaddr_t *b)
127 if (a->af < b->af) return -1;
128 if (a->af > b->af) return +1;
132 return memcmp(&a->ipaddr.ip4addr,
134 sizeof(a->ipaddr.ip4addr));
137 return memcmp(&a->ipaddr.ip6addr,
139 sizeof(a->ipaddr.ip6addr));
150 * See if two packets are identical.
152 * Note that we do NOT compare the authentication vectors.
153 * That's because if the authentication vector is different,
154 * it means that the NAS has given up on the earlier request.
156 int lrad_packet_cmp(const RADIUS_PACKET *a, const RADIUS_PACKET *b)
160 if (a->sockfd < b->sockfd) return -1;
161 if (a->sockfd > b->sockfd) return +1;
163 if (a->id < b->id) return -1;
164 if (a->id > b->id) return +1;
166 if (a->src_port < b->src_port) return -1;
167 if (a->src_port > b->src_port) return +1;
169 if (a->dst_port < b->dst_port) return -1;
170 if (a->dst_port > b->dst_port) return +1;
172 rcode = lrad_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
173 if (rcode != 0) return rcode;
174 return lrad_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
179 * Create a fake "request" from a reply, for later lookup.
181 void lrad_request_from_reply(RADIUS_PACKET *request,
182 const RADIUS_PACKET *reply)
184 request->sockfd = reply->sockfd;
185 request->id = reply->id;
186 request->src_port = reply->dst_port;
187 request->dst_port = reply->src_port;
188 request->src_ipaddr = reply->dst_ipaddr;
189 request->dst_ipaddr = reply->src_ipaddr;
194 * Open a socket on the given IP and port.
196 int lrad_socket(lrad_ipaddr_t *ipaddr, int port)
199 struct sockaddr_storage salocal;
202 if ((port < 0) || (port > 65535)) {
203 librad_log("Port %d is out of allowed bounds", port);
207 sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
212 #ifdef WITH_UDPFROMTO
214 * Initialize udpfromto for all sockets.
216 if (udpfromto_init(sockfd) != 0) {
222 memset(&salocal, 0, sizeof(salocal));
223 if (ipaddr->af == AF_INET) {
224 struct sockaddr_in *sa;
226 sa = (struct sockaddr_in *) &salocal;
227 sa->sin_family = AF_INET;
228 sa->sin_addr = ipaddr->ipaddr.ip4addr;
229 sa->sin_port = htons((uint16_t) port);
232 #ifdef HAVE_STRUCT_SOCKADDR_IN6
233 } else if (ipaddr->af == AF_INET6) {
234 struct sockaddr_in6 *sa;
236 sa = (struct sockaddr_in6 *) &salocal;
237 sa->sin6_family = AF_INET6;
238 sa->sin6_addr = ipaddr->ipaddr.ip6addr;
239 sa->sin6_port = htons((uint16_t) port);
244 * Listening on '::' does NOT get you IPv4 to
245 * IPv6 mapping. You've got to listen on an IPv4
246 * address, too. This makes the rest of the server
247 * design a little simpler.
251 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
254 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
255 (char *)&on, sizeof(on));
257 #endif /* IPV6_V6ONLY */
259 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
261 return sockfd; /* don't bind it */
264 if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
274 * We need to keep track of the socket & it's IP/port.
276 typedef struct lrad_packet_socket_t {
281 int offset; /* 0..31 */
283 lrad_ipaddr_t ipaddr;
285 } lrad_packet_socket_t;
288 #define FNV_MAGIC_PRIME (0x01000193)
289 #define MAX_SOCKETS (32)
290 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
291 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
293 #define MAX_QUEUES (8)
296 * Structure defining a list of packets (incoming or outgoing)
297 * that should be managed.
299 struct lrad_packet_list_t {
300 lrad_hash_table_t *ht;
302 lrad_hash_table_t *dst2id_ht;
309 lrad_packet_socket_t sockets[MAX_SOCKETS];
314 * Ugh. Doing this on every sent/received packet is not nice.
316 static lrad_packet_socket_t *lrad_socket_find(lrad_packet_list_t *pl,
321 i = start = SOCK2OFFSET(sockfd);
323 do { /* make this hack slightly more efficient */
324 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
326 i = (i + 1) & SOCKOFFSET_MASK;
327 } while (i != start);
332 int lrad_packet_list_socket_remove(lrad_packet_list_t *pl, int sockfd)
334 lrad_packet_socket_t *ps;
338 ps = lrad_socket_find(pl, sockfd);
342 * FIXME: Allow the caller forcibly discard these?
344 if (ps->num_outgoing != 0) return 0;
347 pl->mask &= ~(1 << ps->offset);
353 int lrad_packet_list_socket_add(lrad_packet_list_t *pl, int sockfd)
356 struct sockaddr_storage src;
357 socklen_t sizeof_src = sizeof(src);
358 lrad_packet_socket_t *ps;
363 i = start = SOCK2OFFSET(sockfd);
366 if (pl->sockets[i].sockfd == -1) {
367 ps = &pl->sockets[i];
372 i = (i + 1) & SOCKOFFSET_MASK;
373 } while (i != start);
379 memset(ps, 0, sizeof(*ps));
384 * Get address family, etc. first, so we know if we
385 * need to do udpfromto.
387 * FIXME: udpfromto also does this, but it's not
388 * a critical problem.
390 memset(&src, 0, sizeof_src);
391 if (getsockname(sockfd, (struct sockaddr *) &src,
397 * Grab IP addresses & ports from the sockaddr.
399 ps->ipaddr.af = src.ss_family;
400 if (src.ss_family == AF_INET) {
401 struct sockaddr_in *s4;
403 s4 = (struct sockaddr_in *)&src;
404 ps->ipaddr.ipaddr.ip4addr = s4->sin_addr;
405 ps->port = ntohs(s4->sin_port);
407 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
411 #ifdef HAVE_STRUCT_SOCKADDR_IN6
412 } else if (src.ss_family == AF_INET6) {
413 struct sockaddr_in6 *s6;
415 s6 = (struct sockaddr_in6 *)&src;
416 ps->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
417 ps->port = ntohs(s6->sin6_port);
419 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
427 pl->mask |= (1 << ps->offset);
431 static uint32_t packet_entry_hash(const void *data)
433 return lrad_request_packet_hash(*(const RADIUS_PACKET * const *) data);
436 static int packet_entry_cmp(const void *one, const void *two)
438 const RADIUS_PACKET * const *a = one;
439 const RADIUS_PACKET * const *b = two;
441 return lrad_packet_cmp(*a, *b);
445 * A particular socket can have 256 RADIUS ID's outstanding to
446 * any one destination IP/port. So we have a structure that
447 * manages destination IP & port, and has an array of 256 ID's.
449 * The only magic here is that we map the socket number (0..256)
450 * into an "internal" socket number 0..31, that we use to set
451 * bits in the ID array. If a bit is 1, then that ID is in use
452 * for that socket, and the request MUST be in the packet hash!
454 * Note that as a minor memory leak, we don't have an API to free
455 * this structure, except when we discard the whole packet list.
456 * This means that if destinations are added and removed, they
457 * won't be removed from this tree.
459 typedef struct lrad_packet_dst2id_t {
460 lrad_ipaddr_t dst_ipaddr;
462 uint32_t id[1]; /* really id[256] */
463 } lrad_packet_dst2id_t;
466 static uint32_t packet_dst2id_hash(const void *data)
469 const lrad_packet_dst2id_t *pd = data;
471 hash = lrad_hash(&pd->dst_port, sizeof(pd->dst_port));
473 switch (pd->dst_ipaddr.af) {
475 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
476 sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
480 hash = lrad_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
481 sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
491 static int packet_dst2id_cmp(const void *one, const void *two)
493 const lrad_packet_dst2id_t *a = one;
494 const lrad_packet_dst2id_t *b = two;
496 if (a->dst_port < b->dst_port) return -1;
497 if (a->dst_port > b->dst_port) return +1;
499 return lrad_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
502 static void packet_dst2id_free(void *data)
508 void lrad_packet_list_free(lrad_packet_list_t *pl)
512 lrad_hash_table_free(pl->ht);
513 lrad_hash_table_free(pl->dst2id_ht);
519 * Caller is responsible for managing the packet entries.
521 lrad_packet_list_t *lrad_packet_list_create(int alloc_id)
524 lrad_packet_list_t *pl;
526 pl = malloc(sizeof(*pl));
527 if (!pl) return NULL;
528 memset(pl, 0, sizeof(*pl));
530 pl->ht = lrad_hash_table_create(packet_entry_hash,
534 lrad_packet_list_free(pl);
538 for (i = 0; i < MAX_SOCKETS; i++) {
539 pl->sockets[i].sockfd = -1;
545 pl->dst2id_ht = lrad_hash_table_create(packet_dst2id_hash,
548 if (!pl->dst2id_ht) {
549 lrad_packet_list_free(pl);
559 * If pl->alloc_id is set, then lrad_packet_list_id_alloc() MUST
560 * be called before inserting the packet into the list!
562 int lrad_packet_list_insert(lrad_packet_list_t *pl,
563 RADIUS_PACKET **request_p)
565 if (!pl || !request_p || !*request_p) return 0;
567 (*request_p)->hash = lrad_request_packet_hash(*request_p);
569 return lrad_hash_table_insert(pl->ht, request_p);
572 RADIUS_PACKET **lrad_packet_list_find(lrad_packet_list_t *pl,
573 RADIUS_PACKET *request)
575 if (!pl || !request) return 0;
577 return lrad_hash_table_finddata(pl->ht, &request);
582 * This presumes that the reply has dst_ipaddr && dst_port set up
583 * correctly (i.e. real IP, or "*").
585 RADIUS_PACKET **lrad_packet_list_find_byreply(lrad_packet_list_t *pl,
586 RADIUS_PACKET *reply)
588 RADIUS_PACKET my_request, *request;
589 lrad_packet_socket_t *ps;
591 if (!pl || !reply) return NULL;
593 ps = lrad_socket_find(pl, reply->sockfd);
594 if (!ps) return NULL;
597 * Initialize request from reply, AND from the source
598 * IP & port of this socket. The client may have bound
599 * the socket to 0, in which case it's some random port,
600 * that is NOT in the original request->src_port.
602 my_request.sockfd = reply->sockfd;
603 my_request.id = reply->id;
605 if (ps->inaddr_any) {
606 my_request.src_ipaddr = ps->ipaddr;
608 my_request.src_ipaddr = reply->dst_ipaddr;
610 my_request.src_port = ps->port;;
612 my_request.dst_ipaddr = reply->src_ipaddr;
613 my_request.dst_port = reply->src_port;
616 request = &my_request;
618 return lrad_hash_table_finddata(pl->ht, &request);
622 RADIUS_PACKET **lrad_packet_list_yank(lrad_packet_list_t *pl,
623 RADIUS_PACKET *request)
625 if (!pl || !request) return NULL;
627 return lrad_hash_table_yank(pl->ht, &request);
630 int lrad_packet_list_num_elements(lrad_packet_list_t *pl)
634 return lrad_hash_table_num_elements(pl->ht);
639 * 1 == ID was allocated & assigned
640 * 0 == error allocating memory
641 * -1 == all ID's are used, caller should open a new socket.
643 * Note that this ALSO assigns a socket to use, and updates
644 * packet->request->src_ipaddr && packet->request->src_port
646 * In multi-threaded systems, the calls to id_alloc && id_free
647 * should be protected by a mutex. This does NOT have to be
648 * the same mutex as the one protecting the insert/find/yank
651 int lrad_packet_list_id_alloc(lrad_packet_list_t *pl,
652 RADIUS_PACKET *request)
656 lrad_packet_dst2id_t my_pd, *pd;
657 lrad_packet_socket_t *ps;
659 if (!pl || !pl->alloc_id || !request) return 0;
661 my_pd.dst_ipaddr = request->dst_ipaddr;
662 my_pd.dst_port = request->dst_port;
664 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
666 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
669 memset(pd, 0, sizeof(*pd) + 255 * sizeof(pd->id[0]));
671 pd->dst_ipaddr = request->dst_ipaddr;
672 pd->dst_port = request->dst_port;
674 if (!lrad_hash_table_insert(pl->dst2id_ht, pd)) {
681 * FIXME: Go to an LRU system. This prevents ID re-use
682 * for as long as possible. The main problem with that
683 * approach is that it requires us to populate the
684 * LRU/FIFO when we add a new socket, or a new destination,
685 * which can be expensive.
687 * The LRU can be avoided if the caller takes care to free
688 * Id's only when all responses have been received, OR after
691 id = start = (int) lrad_rand() & 0xff;
693 while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
696 if (id == start) return 0;
699 free_mask = ~((~pd->id[id]) & pl->mask);
702 for (i = 0; i < MAX_SOCKETS; i++) {
703 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
705 if ((free_mask & (1 << i)) == 0) {
711 if (start < 0) return 0; /* bad error */
713 pd->id[id] |= (1 << start);
714 ps = &pl->sockets[start];
720 * Set the ID, source IP, and source port.
724 request->sockfd = ps->sockfd;
725 request->src_ipaddr = ps->ipaddr;
726 request->src_port = ps->port;
732 * Should be called AFTER yanking it from the list, so that
733 * any newly inserted entries don't collide with this one.
735 int lrad_packet_list_id_free(lrad_packet_list_t *pl,
736 RADIUS_PACKET *request)
738 lrad_packet_socket_t *ps;
739 lrad_packet_dst2id_t my_pd, *pd;
741 if (!pl || !request) return 0;
743 ps = lrad_socket_find(pl, request->sockfd);
746 my_pd.dst_ipaddr = request->dst_ipaddr;
747 my_pd.dst_port = request->dst_port;
749 pd = lrad_hash_table_finddata(pl->dst2id_ht, &my_pd);
752 pd->id[request->id] &= ~(1 << ps->offset);
753 request->hash = 0; /* invalidate the cached hash */
761 int lrad_packet_list_walk(lrad_packet_list_t *pl, void *ctx,
762 lrad_hash_table_walk_t callback)
764 if (!pl || !callback) return 0;
766 return lrad_hash_table_walk(pl->ht, callback, ctx);
769 int lrad_packet_list_fd_set(lrad_packet_list_t *pl, fd_set *set)
773 if (!pl || !set) return 0;
777 for (i = 0; i < MAX_SOCKETS; i++) {
778 if (pl->sockets[i].sockfd == -1) continue;
779 FD_SET(pl->sockets[i].sockfd, set);
780 if (pl->sockets[i].sockfd > maxfd) {
781 maxfd = pl->sockets[i].sockfd;
785 if (maxfd < 0) return -1;
791 * Round-robins the receivers, without priority.
793 * FIXME: Add sockfd, if -1, do round-robin, else do sockfd
796 RADIUS_PACKET *lrad_packet_list_recv(lrad_packet_list_t *pl, fd_set *set)
799 RADIUS_PACKET *packet;
801 if (!pl || !set) return NULL;
803 start = pl->last_recv;
806 start &= SOCKOFFSET_MASK;
808 if (pl->sockets[start].sockfd == -1) continue;
810 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
812 packet = rad_recv(pl->sockets[start].sockfd);
813 if (!packet) continue;
816 * Call lrad_packet_list_find_byreply(). If it
817 * doesn't find anything, discard the reply.
820 pl->last_recv = start;
822 } while (start != pl->last_recv);
827 int lrad_packet_list_num_incoming(lrad_packet_list_t *pl)
833 num_elements = lrad_hash_table_num_elements(pl->ht);
834 if (num_elements < pl->num_outgoing) return 0; /* panic! */
836 return num_elements - pl->num_outgoing;
839 int lrad_packet_list_num_outgoing(lrad_packet_list_t *pl)
843 return pl->num_outgoing;