2 This file is part of GNUnet.
3 Copyright (C) 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file nat/gnunet-service-nat.c
23 * @brief network address translation traversal service
24 * @author Christian Grothoff
26 * The purpose of this service is to enable transports to
27 * traverse NAT routers, by providing traversal options and
28 * knowledge about the local network topology.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_nat_service.h"
42 * How often should we ask the OS about a list of active
45 #define SCAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49 * Internal data structure we track for each of our clients.
57 struct ClientHandle *next;
62 struct ClientHandle *prev;
65 * Underlying handle for this client with the service.
67 struct GNUNET_SERVICE_Client *client;
70 * Message queue for communicating with the client.
72 struct GNUNET_MQ_Handle *mq;
75 * Array of addresses used by the service.
77 struct sockaddr **addrs;
80 * What does this client care about?
82 enum GNUNET_NAT_RegisterFlags flags;
85 * Port we would like as we are configured to use this one for
86 * advertising (in addition to the one we are binding to).
91 * Number of addresses that this service is bound to.
96 * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP.
104 * List of local addresses this system has.
106 struct LocalAddressList
109 * This is a linked list.
111 struct LocalAddressList *next;
116 struct LocalAddressList *prev;
119 * The address itself (i.e. `struct sockaddr_in` or `struct
120 * sockaddr_in6`, in the respective byte order).
122 struct sockaddr_storage addr;
130 * What type of address is this?
132 enum GNUNET_NAT_AddressClass ac;
140 * Handle to our current configuration.
142 static const struct GNUNET_CONFIGURATION_Handle *cfg;
145 * Handle to the statistics service.
147 static struct GNUNET_STATISTICS_Handle *stats;
150 * Task scheduled to periodically scan our network interfaces.
152 static struct GNUNET_SCHEDULER_Task *scan_task;
155 * Head of client DLL.
157 static struct ClientHandle *ch_head;
160 * Tail of client DLL.
162 static struct ClientHandle *ch_tail;
165 * Head of DLL of local addresses.
167 static struct LocalAddressList *lal_head;
170 * Tail of DLL of local addresses.
172 static struct LocalAddressList *lal_tail;
176 * Free the DLL starting at #lal_head.
181 struct LocalAddressList *lal;
183 while (NULL != (lal = lal_head))
185 GNUNET_CONTAINER_DLL_remove (lal_head,
194 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REGISTER message from
197 * @param cls client who sent the message
198 * @param message the message received
199 * @return #GNUNET_OK if message is well-formed
202 check_register (void *cls,
203 const struct GNUNET_NAT_RegisterMessage *message)
205 uint16_t num_addrs = ntohs (message->num_addrs);
206 const char *off = (const char *) &message[1];
207 size_t left = ntohs (message->header.size) - sizeof (*message);
209 for (unsigned int i=0;i<num_addrs;i++)
212 const struct sockaddr *sa = (const struct sockaddr *) off;
214 if (sizeof (sa_family_t) > left)
217 return GNUNET_SYSERR;
219 switch (sa->sa_family)
222 alen = sizeof (struct sockaddr_in);
225 alen = sizeof (struct sockaddr_in6);
229 alen = sizeof (struct sockaddr_un);
234 return GNUNET_SYSERR;
239 return GNUNET_SYSERR;
247 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REGISTER message from client.
248 * We remember the client for updates upon future NAT events.
250 * @param cls client who sent the message
251 * @param message the message received
254 handle_register (void *cls,
255 const struct GNUNET_NAT_RegisterMessage *message)
257 struct ClientHandle *ch = cls;
261 if ( (0 != ch->proto) ||
262 (NULL != ch->addrs) )
264 /* double registration not allowed */
266 GNUNET_SERVICE_client_drop (ch->client);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Received REGISTER message from client\n");
271 ch->flags = message->flags;
272 ch->proto = message->proto;
273 ch->adv_port = ntohs (message->adv_port);
274 ch->num_addrs = ntohs (message->adv_port);
275 ch->addrs = GNUNET_new_array (ch->num_addrs,
277 left = ntohs (message->header.size) - sizeof (*message);
278 off = (const char *) &message[1];
279 for (unsigned int i=0;i<ch->num_addrs;i++)
282 const struct sockaddr *sa = (const struct sockaddr *) off;
284 if (sizeof (sa_family_t) > left)
287 GNUNET_SERVICE_client_drop (ch->client);
290 switch (sa->sa_family)
293 alen = sizeof (struct sockaddr_in);
296 alen = sizeof (struct sockaddr_in6);
300 alen = sizeof (struct sockaddr_un);
305 GNUNET_SERVICE_client_drop (ch->client);
308 GNUNET_assert (alen <= left);
309 ch->addrs[i] = GNUNET_malloc (alen);
310 GNUNET_memcpy (ch->addrs[i],
315 GNUNET_SERVICE_client_continue (ch->client);
320 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN message from
323 * @param cls client who sent the message
324 * @param message the message received
325 * @return #GNUNET_OK if message is well-formed
328 check_stun (void *cls,
329 const struct GNUNET_NAT_HandleStunMessage *message)
331 size_t sa_len = ntohs (message->sender_addr_size);
332 size_t expect = sa_len + ntohs (message->payload_size);
334 if (ntohs (message->header.size) - sizeof (*message) != expect)
337 return GNUNET_SYSERR;
339 if (sa_len < sizeof (sa_family_t))
342 return GNUNET_SYSERR;
349 * Handler for #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN message from
352 * @param cls client who sent the message
353 * @param message the message received
356 handle_stun (void *cls,
357 const struct GNUNET_NAT_HandleStunMessage *message)
359 struct ClientHandle *ch = cls;
360 const char *buf = (const char *) &message[1];
361 const struct sockaddr *sa;
366 sa_len = ntohs (message->sender_addr_size);
367 payload_size = ntohs (message->payload_size);
368 sa = (const struct sockaddr *) &buf[0];
369 payload = (const struct sockaddr *) &buf[sa_len];
370 switch (sa->sa_family)
373 if (sa_len != sizeof (struct sockaddr_in))
376 GNUNET_SERVICE_client_drop (ch->client);
381 if (sa_len != sizeof (struct sockaddr_in6))
384 GNUNET_SERVICE_client_drop (ch->client);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Received HANDLE_STUN message from client\n");
391 // FIXME: actually handle STUN request!
392 GNUNET_SERVICE_client_continue (ch->client);
398 * #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL message from
401 * @param cls client who sent the message
402 * @param message the message received
403 * @return #GNUNET_OK if message is well-formed
406 check_request_connection_reversal (void *cls,
407 const struct GNUNET_NAT_RequestConnectionReversalMessage *message)
411 expect = ntohs (message->local_addr_size)
412 + ntohs (message->remote_addr_size);
413 if (ntohs (message->header.size) - sizeof (*message) != expect)
416 return GNUNET_SYSERR;
423 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL
424 * message from client.
426 * @param cls client who sent the message
427 * @param message the message received
430 handle_request_connection_reversal (void *cls,
431 const struct GNUNET_NAT_RequestConnectionReversalMessage *message)
433 struct ClientHandle *ch = cls;
434 const char *buf = (const char *) &message[1];
435 size_t local_sa_len = ntohs (message->local_addr_size);
436 size_t remote_sa_len = ntohs (message->remote_addr_size);
437 const struct sockaddr *local_sa = (const struct sockaddr *) &buf[0];
438 const struct sockaddr *remote_sa = (const struct sockaddr *) &buf[local_sa_len];
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Received REQUEST CONNECTION REVERSAL message from client\n");
442 switch (local_sa->sa_family)
445 if (local_sa_len != sizeof (struct sockaddr_in))
448 GNUNET_SERVICE_client_drop (ch->client);
453 if (local_sa_len != sizeof (struct sockaddr_in6))
456 GNUNET_SERVICE_client_drop (ch->client);
462 GNUNET_SERVICE_client_drop (ch->client);
465 switch (remote_sa->sa_family)
468 if (remote_sa_len != sizeof (struct sockaddr_in))
471 GNUNET_SERVICE_client_drop (ch->client);
476 if (remote_sa_len != sizeof (struct sockaddr_in6))
479 GNUNET_SERVICE_client_drop (ch->client);
485 GNUNET_SERVICE_client_drop (ch->client);
488 /* FIXME: actually run the logic! */
490 GNUNET_SERVICE_client_continue (ch->client);
495 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_TEST message from
498 * @param cls client who sent the message
499 * @param message the message received
502 handle_test (void *cls,
503 const struct GNUNET_NAT_RequestTestMessage *message)
505 struct ClientHandle *ch = cls;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Received REQUEST_TEST message from client\n");
509 /* FIXME: actually process test request */
510 GNUNET_SERVICE_client_continue (ch->client);
515 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
518 * @param cls client who sent the message
519 * @param message the message received
520 * @return #GNUNET_OK if message is well-formed
523 check_autoconfig_request (void *cls,
524 const struct GNUNET_NAT_AutoconfigRequestMessage *message)
526 return GNUNET_OK; /* checked later */
531 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
534 * @param cls client who sent the message
535 * @param message the message received
538 handle_autoconfig_request (void *cls,
539 const struct GNUNET_NAT_AutoconfigRequestMessage *message)
541 struct ClientHandle *ch = cls;
542 size_t left = ntohs (message->header.size);
543 struct GNUNET_CONFIGURATION_Handle *c;
545 c = GNUNET_CONFIGURATION_create ();
547 GNUNET_CONFIGURATION_deserialize (c,
548 (const char *) &message[1],
553 GNUNET_SERVICE_client_drop (ch->client);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Received REQUEST_AUTO_CONFIG message from client\n");
558 // FIXME: actually handle request...
559 GNUNET_CONFIGURATION_destroy (c);
560 GNUNET_SERVICE_client_continue (ch->client);
565 * Task run during shutdown.
570 shutdown_task (void *cls)
572 if (NULL != scan_task)
574 GNUNET_SCHEDULER_cancel (scan_task);
579 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
587 * Closure for #ifc_proc.
589 struct IfcProcContext
593 * Head of DLL of local addresses.
595 struct LocalAddressList *lal_head;
598 * Tail of DLL of local addresses.
600 struct LocalAddressList *lal_tail;
606 * Check if @a ip is in @a network with @a bits netmask.
608 * @param network to test
609 * @param ip IP address to test
610 * @param bits bitmask for the network
611 * @return #GNUNET_YES if @a ip is in @a network
614 match_ipv4 (const char *network,
615 const struct in_addr *ip,
622 GNUNET_assert (1 == inet_pton (AF_INET,
625 return ! ((ip->s_addr ^ net.s_addr) & htonl (0xFFFFFFFFu << (32 - bits)));
630 * Check if @a ip is in @a network with @a bits netmask.
632 * @param network to test
633 * @param ip IP address to test
634 * @param bits bitmask for the network
635 * @return #GNUNET_YES if @a ip is in @a network
638 match_ipv6 (const char *network,
639 const struct in6_addr *ip,
643 struct in6_addr mask;
648 GNUNET_assert (1 == inet_pton (AF_INET,
651 memset (&mask, 0, sizeof (mask));
655 mask.s6_addr[off++] = 0xFF;
660 mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80;
663 for (unsigned j = 0; j < sizeof (struct in6_addr) / sizeof (uint32_t); j++)
664 if (((((uint32_t *) ip)[j] & ((uint32_t *) &mask)[j])) !=
665 (((uint32_t *) &net)[j] & ((int *) &mask)[j]))
672 * Callback function invoked for each interface found. Adds them
673 * to our new address list.
675 * @param cls a `struct IfcProcContext *`
676 * @param name name of the interface (can be NULL for unknown)
677 * @param isDefault is this presumably the default interface
678 * @param addr address of this interface (can be NULL for unknown or unassigned)
679 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
680 * @param netmask the network mask (can be NULL for unknown or unassigned)
681 * @param addrlen length of the address
682 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
688 const struct sockaddr *addr,
689 const struct sockaddr *broadcast_addr,
690 const struct sockaddr *netmask,
693 struct IfcProcContext *ifc_ctx = cls;
694 struct LocalAddressList *lal;
697 const struct in6_addr *v6;
698 enum GNUNET_NAT_AddressClass ac;
700 switch (addr->sa_family)
703 alen = sizeof (struct sockaddr_in);
704 ip = &((const struct sockaddr_in *) addr)->sin_addr;
705 if (match_ipv4 ("127.0.0.0", ip, 8))
706 ac = GNUNET_NAT_AC_LOOPBACK;
707 else if (match_ipv4 ("10.0.0.0", ip, 8) || /* RFC 1918 */
708 match_ipv4 ("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */
709 match_ipv4 ("192.168.0.0", ip, 12) || /* RFC 1918 */
710 match_ipv4 ("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */
711 match_ipv4 ("172.16.0.0", ip, 16)) /* RFC 1918 */
712 ac = GNUNET_NAT_AC_LAN;
714 ac = GNUNET_NAT_AC_GLOBAL;
717 alen = sizeof (struct sockaddr_in6);
718 ip = &((const struct sockaddr_in6 *) addr)->sin6_addr;
719 if (match_ipv6 ("::1", ip, 128))
720 ac = GNUNET_NAT_AC_LOOPBACK;
721 else if (match_ipv6 ("fc00::", ip, 7) || /* RFC 4193 */
722 match_ipv6 ("fec0::", ip, 10) || /* RFC 3879 */
723 match_ipv6 ("fe80::", ip, 10)) /* RFC 4291, link-local */
724 ac = GNUNET_NAT_AC_LAN;
726 ac = GNUNET_NAT_AC_GLOBAL;
728 if ( (v6->s6_addr[11] == 0xFF) &&
729 (v6->s6_addr[12] == 0xFE) )
731 /* contains a MAC, be extra careful! */
732 ac |= GNUNET_NAT_AC_PRIVATE;
744 lal = GNUNET_malloc (sizeof (*lal));
745 lal->af = addr->sa_family;
747 GNUNET_memcpy (&lal->addr,
750 GNUNET_CONTAINER_DLL_insert (ifc_ctx->lal_head,
758 * Notify all clients about a change in the list
759 * of addresses this peer has.
761 * @param delta the entry in the list that changed
762 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
765 notify_clients (struct LocalAddressList *delta,
768 for (struct ClientHandle *ch = ch_head;
772 struct GNUNET_MQ_Envelope *env;
773 struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
776 if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES))
781 alen = sizeof (struct sockaddr_in);
784 alen = sizeof (struct sockaddr_in6);
790 env = GNUNET_MQ_msg_extra (msg,
792 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
793 msg->add_remove = htonl (add);
794 msg->addr_class = htonl (delta->ac);
795 GNUNET_memcpy (&msg[1],
798 /* FIXME: what about the port number? */
799 GNUNET_MQ_send (ch->mq,
806 * Task we run periodically to scan for network interfaces.
813 struct IfcProcContext ifc_ctx;
816 scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ,
822 GNUNET_OS_network_interfaces_list (&ifc_proc,
824 for (struct LocalAddressList *lal = lal_head;
829 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
833 if ( (pos->af == lal->af) &&
834 (0 == memcmp (&lal->addr,
837 ? sizeof (struct sockaddr_in)
838 : sizeof (struct sockaddr_in6))) )
841 if (GNUNET_NO == found)
846 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
851 for (struct LocalAddressList *lal = lal_head;
855 if ( (pos->af == lal->af) &&
856 (0 == memcmp (&lal->addr,
859 ? sizeof (struct sockaddr_in)
860 : sizeof (struct sockaddr_in6))) )
863 if (GNUNET_NO == found)
869 lal_head = ifc_ctx.lal_head;
870 lal_tail = ifc_ctx.lal_tail;
875 * Handle network size estimate clients.
878 * @param c configuration to use
879 * @param service the initialized service
883 const struct GNUNET_CONFIGURATION_Handle *c,
884 struct GNUNET_SERVICE_Handle *service)
887 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
889 stats = GNUNET_STATISTICS_create ("nat",
891 scan_task = GNUNET_SCHEDULER_add_now (&run_scan,
897 * Callback called when a client connects to the service.
899 * @param cls closure for the service
900 * @param c the new client that connected to the service
901 * @param mq the message queue used to send messages to the client
902 * @return a `struct ClientHandle`
905 client_connect_cb (void *cls,
906 struct GNUNET_SERVICE_Client *c,
907 struct GNUNET_MQ_Handle *mq)
909 struct ClientHandle *ch;
911 ch = GNUNET_new (struct ClientHandle);
914 GNUNET_CONTAINER_DLL_insert (ch_head,
922 * Callback called when a client disconnected from the service
924 * @param cls closure for the service
925 * @param c the client that disconnected
926 * @param internal_cls a `struct ClientHandle *`
929 client_disconnect_cb (void *cls,
930 struct GNUNET_SERVICE_Client *c,
933 struct ClientHandle *ch = internal_cls;
935 GNUNET_CONTAINER_DLL_remove (ch_head,
938 for (unsigned int i=0;i<ch->num_addrs;i++)
939 GNUNET_free_non_null (ch->addrs[i]);
940 GNUNET_free_non_null (ch->addrs);
946 * Define "main" method using service macro.
950 GNUNET_SERVICE_OPTION_NONE,
953 &client_disconnect_cb,
955 GNUNET_MQ_hd_var_size (register,
956 GNUNET_MESSAGE_TYPE_NAT_REGISTER,
957 struct GNUNET_NAT_RegisterMessage,
959 GNUNET_MQ_hd_var_size (stun,
960 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN,
961 struct GNUNET_NAT_HandleStunMessage,
963 GNUNET_MQ_hd_var_size (request_connection_reversal,
964 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL,
965 struct GNUNET_NAT_RequestConnectionReversalMessage,
967 GNUNET_MQ_hd_fixed_size (test,
968 GNUNET_MESSAGE_TYPE_NAT_REQUEST_TEST,
969 struct GNUNET_NAT_RequestTestMessage,
971 GNUNET_MQ_hd_var_size (autoconfig_request,
972 GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG,
973 struct GNUNET_NAT_AutoconfigRequestMessage,
975 GNUNET_MQ_handler_end ());
978 #if defined(LINUX) && defined(__GLIBC__)
982 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
984 void __attribute__ ((constructor))
985 GNUNET_ARM_memory_init ()
987 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
988 mallopt (M_TOP_PAD, 1 * 1024);
993 /* end of gnunet-service-nat.c */