2 This file is part of GNUnet.
3 Copyright (C) 2007-2017 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 * @author Christian Grothoff
23 * @author Milan Bouchet-Valat
26 * Service for handling UPnP and NAT-PMP port forwarding
27 * and external IP address retrieval
30 #include "gnunet_nat_service.h"
36 * Entry in DLL of addresses of this peer.
44 struct AddrEntry *next;
49 struct AddrEntry *prev;
52 * Address class of the address.
54 enum GNUNET_NAT_AddressClass ac;
57 * Number of bytes that follow.
64 * Handle for active NAT registrations.
66 struct GNUNET_NAT_Handle
70 * Configuration we use.
72 const struct GNUNET_CONFIGURATION_Handle *cfg;
75 * Message queue for communicating with the NAT service.
77 struct GNUNET_MQ_Handle *mq;
80 * Our registration message.
82 struct GNUNET_MessageHeader *reg;
85 * Head of address DLL.
87 struct AddrEntry *ae_head;
90 * Tail of address DLL.
92 struct AddrEntry *ae_tail;
95 * Function to call when our addresses change.
97 GNUNET_NAT_AddressCallback address_callback;
100 * Function to call when another peer requests connection reversal.
102 GNUNET_NAT_ReversalCallback reversal_callback;
105 * Closure for the various callbacks.
110 * Task scheduled to reconnect to the service.
112 struct GNUNET_SCHEDULER_Task *reconnect_task;
115 * How long to wait until we reconnect.
117 struct GNUNET_TIME_Relative reconnect_delay;
122 * Task to connect to the NAT service.
124 * @param cls our `struct GNUNET_NAT_Handle *`
127 do_connect (void *cls);
131 * Task to connect to the NAT service.
133 * @param nh handle to reconnect
136 reconnect (struct GNUNET_NAT_Handle *nh)
138 struct AddrEntry *ae;
142 GNUNET_MQ_destroy (nh->mq);
145 while (NULL != (ae = nh->ae_head))
147 GNUNET_CONTAINER_DLL_remove (nh->ae_head,
150 nh->address_callback (nh->callback_cls,
153 (const struct sockaddr *) &ae[1],
158 = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay);
160 = GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay,
167 * Check connection reversal request.
169 * @param cls our `struct GNUNET_NAT_Handle`
170 * @param crm the message
171 * @return #GNUNET_OK if @a crm is well-formed
174 check_connection_reversal_request (void *cls,
175 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
177 if (ntohs (crm->header.size) !=
179 sizeof (struct sockaddr_in) )
182 return GNUNET_SYSERR;
189 * Handle connection reversal request.
191 * @param cls our `struct GNUNET_NAT_Handle`
192 * @param crm the message
195 handle_connection_reversal_request (void *cls,
196 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
198 struct GNUNET_NAT_Handle *nh = cls;
200 nh->reversal_callback (nh->callback_cls,
201 (const struct sockaddr *) &crm[1],
202 sizeof (struct sockaddr_in));
207 * Check address change notification.
209 * @param cls our `struct GNUNET_NAT_Handle`
210 * @param acn the message
211 * @return #GNUNET_OK if @a crm is well-formed
214 check_address_change_notification (void *cls,
215 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
217 size_t alen = ntohs (acn->header.size) - sizeof (*acn);
221 case sizeof (struct sockaddr_in):
223 const struct sockaddr_in *s4
224 = (const struct sockaddr_in *) &acn[1];
225 if (AF_INET != s4->sin_family)
228 return GNUNET_SYSERR;
232 case sizeof (struct sockaddr_in6):
234 const struct sockaddr_in6 *s6
235 = (const struct sockaddr_in6 *) &acn[1];
236 if (AF_INET6 != s6->sin6_family)
239 return GNUNET_SYSERR;
245 return GNUNET_SYSERR;
252 * Handle connection reversal request.
254 * @param cls our `struct GNUNET_NAT_Handle`
255 * @param acn the message
258 handle_address_change_notification (void *cls,
259 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
261 struct GNUNET_NAT_Handle *nh = cls;
262 size_t alen = ntohs (acn->header.size) - sizeof (*acn);
263 const struct sockaddr *sa = (const struct sockaddr *) &acn[1];
264 enum GNUNET_NAT_AddressClass ac;
265 struct AddrEntry *ae;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Received address change notification\n");
269 ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class);
270 if (GNUNET_YES == ntohl (acn->add_remove))
272 ae = GNUNET_malloc (sizeof (*ae) + alen);
275 GNUNET_memcpy (&ae[1],
278 GNUNET_CONTAINER_DLL_insert (nh->ae_head,
284 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
285 if ( (ae->addrlen == alen) &&
286 (0 == memcmp (&ae[1],
296 GNUNET_CONTAINER_DLL_remove (nh->ae_head,
301 nh->address_callback (nh->callback_cls,
302 ntohl (acn->add_remove),
310 * Handle queue errors by reconnecting to NAT.
312 * @param cls the `struct GNUNET_NAT_Handle *`
313 * @param error details about the error
316 mq_error_handler (void *cls,
317 enum GNUNET_MQ_Error error)
319 struct GNUNET_NAT_Handle *nh = cls;
326 * Task to connect to the NAT service.
328 * @param cls our `struct GNUNET_NAT_Handle *`
331 do_connect (void *cls)
333 struct GNUNET_NAT_Handle *nh = cls;
334 struct GNUNET_MQ_MessageHandler handlers[] = {
335 GNUNET_MQ_hd_var_size (connection_reversal_request,
336 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED,
337 struct GNUNET_NAT_ConnectionReversalRequestedMessage,
339 GNUNET_MQ_hd_var_size (address_change_notification,
340 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE,
341 struct GNUNET_NAT_AddressChangeNotificationMessage,
343 GNUNET_MQ_handler_end ()
345 struct GNUNET_MQ_Envelope *env;
347 nh->reconnect_task = NULL;
348 nh->mq = GNUNET_CLIENT_connect (nh->cfg,
358 env = GNUNET_MQ_msg_copy (nh->reg);
359 GNUNET_MQ_send (nh->mq,
365 * Attempt to enable port redirection and detect public IP address
366 * contacting UPnP or NAT-PMP routers on the local network. Use @a
367 * addr to specify to which of the local host's addresses should the
368 * external port be mapped. The port is taken from the corresponding
369 * sockaddr_in[6] field. The NAT module should call the given @a
370 * address_callback for any 'plausible' external address.
372 * @param cfg configuration to use
373 * @param config_section name of the configuration section for optionsx
374 * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
375 * @param num_addrs number of addresses in @a addrs
376 * @param addrs list of local addresses packets should be redirected to
377 * @param addrlens actual lengths of the addresses in @a addrs
378 * @param address_callback function to call everytime the public IP address changes
379 * @param reversal_callback function to call if someone wants connection reversal from us,
380 * NULL if connection reversal is not supported
381 * @param callback_cls closure for callbacks
382 * @return NULL on error, otherwise handle that can be used to unregister
384 struct GNUNET_NAT_Handle *
385 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
386 const char *config_section,
388 unsigned int num_addrs,
389 const struct sockaddr **addrs,
390 const socklen_t *addrlens,
391 GNUNET_NAT_AddressCallback address_callback,
392 GNUNET_NAT_ReversalCallback reversal_callback,
395 struct GNUNET_NAT_Handle *nh;
396 struct GNUNET_NAT_RegisterMessage *rm;
402 for (unsigned int i=0;i<num_addrs;i++)
404 str_len = strlen (config_section) + 1;
406 if ( (len > GNUNET_MAX_MESSAGE_SIZE - sizeof (*rm)) ||
407 (num_addrs > UINT16_MAX) )
412 rm = GNUNET_malloc (sizeof (*rm) + len);
413 rm->header.size = htons (sizeof (*rm) + len);
414 rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER);
415 rm->flags = GNUNET_NAT_RF_NONE;
416 if (NULL != address_callback)
417 rm->flags |= GNUNET_NAT_RF_ADDRESSES;
418 if (NULL != reversal_callback)
419 rm->flags |= GNUNET_NAT_RF_REVERSAL;
421 rm->str_len = htons (str_len);
422 rm->num_addrs = htons ((uint16_t) num_addrs);
423 off = (char *) &rm[1];
424 for (unsigned int i=0;i<num_addrs;i++)
426 switch (addrs[i]->sa_family)
429 if (sizeof (struct sockaddr_in) != addrlens[i])
437 if (sizeof (struct sockaddr_in6) != addrlens[i])
446 if (sizeof (struct sockaddr_un) != addrlens[i])
468 nh = GNUNET_new (struct GNUNET_NAT_Handle);
469 nh->reg = &rm->header;
471 nh->address_callback = address_callback;
472 nh->reversal_callback = reversal_callback;
473 nh->callback_cls = callback_cls;
480 * Check if an incoming message is a STUN message.
482 * @param data the packet
483 * @param len the length of the packet in @a data
484 * @return #GNUNET_YES if @a data is a STUN packet,
485 * #GNUNET_NO if the packet is invalid (not a stun packet)
488 test_stun_packet (const void *data,
491 const struct stun_header *hdr;
492 const struct stun_attr *attr;
493 uint32_t advertised_message_size;
494 uint32_t message_magic_cookie;
496 /* On entry, 'len' is the length of the UDP payload. After the
497 * initial checks it becomes the size of unprocessed options,
498 * while 'data' is advanced accordingly.
500 if (len < sizeof(struct stun_header))
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "STUN packet too short (only %d, wanting at least %d)\n",
505 (int) sizeof (struct stun_header));
508 hdr = (const struct stun_header *) data;
509 /* Skip header as it is already in hdr */
510 len -= sizeof (struct stun_header);
511 data += sizeof (struct stun_header);
513 /* len as advertised in the message */
514 advertised_message_size = ntohs (hdr->msglen);
516 message_magic_cookie = ntohl (hdr->magic);
517 /* Compare if the cookie match */
518 if (STUN_MAGIC_COOKIE != message_magic_cookie)
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Invalid magic cookie for STUN\n");
525 if (advertised_message_size > len)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Scrambled STUN packet length (got %d, expecting %d)\n",
529 advertised_message_size,
533 len = advertised_message_size;
536 if (len < sizeof (struct stun_attr))
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Attribute too short in STUN packet (got %d, expecting %d)\n",
541 (int) sizeof(struct stun_attr));
544 attr = (const struct stun_attr *) data;
546 /* compute total attribute length */
547 advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
549 /* Check if we still have space in our buffer */
550 if (advertised_message_size > len)
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
554 advertised_message_size,
558 data += advertised_message_size;
559 len -= advertised_message_size;
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "STUN Packet, msg %04x, length: %d\n",
563 ntohs (hdr->msgtype),
564 advertised_message_size);
570 * Handle an incoming STUN message. This function is useful as
571 * some GNUnet service may be listening on a UDP port and might
572 * thus receive STUN messages while trying to receive other data.
573 * In this case, this function can be used to process replies
576 * The function does some basic sanity checks on packet size and
577 * content, try to extract a bit of information.
579 * At the moment this only processes BIND requests, and returns the
580 * externally visible address of the request to the rest of the
583 * @param nh handle to the NAT service
584 * @param sender_addr address from which we got @a data
585 * @param sender_addr_len number of bytes in @a sender_addr
586 * @param data the packet
587 * @param data_size number of bytes in @a data
588 * @return #GNUNET_OK on success
589 * #GNUNET_NO if the packet is not a STUN packet
590 * #GNUNET_SYSERR on internal error handling the packet
593 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
594 const struct sockaddr *sender_addr,
595 size_t sender_addr_len,
599 struct GNUNET_MQ_Envelope *env;
600 struct GNUNET_NAT_HandleStunMessage *hsn;
604 test_stun_packet (data,
608 return GNUNET_SYSERR;
609 env = GNUNET_MQ_msg_extra (hsn,
610 data_size + sender_addr_len,
611 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN);
612 hsn->sender_addr_size = htons ((uint16_t) sender_addr_len);
613 hsn->payload_size = htons ((uint16_t) data_size);
614 buf = (char *) &hsn[1];
618 buf += sender_addr_len;
622 GNUNET_MQ_send (nh->mq,
629 * Test if the given address is (currently) a plausible IP address for
630 * this peer. Mostly a convenience function so that clients do not
631 * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
632 * has returned so far.
634 * @param nh the handle returned by register
635 * @param addr IP address to test (IPv4 or IPv6)
636 * @param addrlen number of bytes in @a addr
637 * @return #GNUNET_YES if the address is plausible,
638 * #GNUNET_NO if the address is not plausible,
639 * #GNUNET_SYSERR if the address is malformed
642 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
646 struct AddrEntry *ae;
648 if ( (addrlen != sizeof (struct sockaddr_in)) &&
649 (addrlen != sizeof (struct sockaddr_in6)) )
652 return GNUNET_SYSERR;
654 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
655 if ( (addrlen == ae->addrlen) &&
665 * We learned about a peer (possibly behind NAT) so run the
666 * gnunet-nat-client to send dummy ICMP responses to cause
667 * that peer to connect to us (connection reversal).
669 * @param nh handle (used for configuration)
670 * @param local_sa our local address of the peer (IPv4-only)
671 * @param remote_sa the remote address of the peer (IPv4-only)
672 * @return #GNUNET_SYSERR on error,
673 * #GNUNET_NO if connection reversal is unavailable,
674 * #GNUNET_OK otherwise (presumably in progress)
677 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
678 const struct sockaddr_in *local_sa,
679 const struct sockaddr_in *remote_sa)
681 struct GNUNET_MQ_Envelope *env;
682 struct GNUNET_NAT_RequestConnectionReversalMessage *req;
686 return GNUNET_SYSERR;
687 GNUNET_break (AF_INET == local_sa->sin_family);
688 GNUNET_break (AF_INET == remote_sa->sin_family);
689 env = GNUNET_MQ_msg_extra (req,
690 2 * sizeof (struct sockaddr_in),
691 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL);
692 req->local_addr_size = htons (sizeof (struct sockaddr_in));
693 req->remote_addr_size = htons (sizeof (struct sockaddr_in));
694 buf = (char *) &req[1];
697 sizeof (struct sockaddr_in));
698 buf += sizeof (struct sockaddr_in);
701 sizeof (struct sockaddr_in));
702 GNUNET_MQ_send (nh->mq,
709 * Stop port redirection and public IP address detection for the given
710 * handle. This frees the handle, after having sent the needed
711 * commands to close open ports.
713 * @param nh the handle to stop
716 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
720 GNUNET_MQ_destroy (nh->mq);
723 if (NULL != nh->reconnect_task)
725 GNUNET_SCHEDULER_cancel (nh->reconnect_task);
726 nh->reconnect_task = NULL;
728 GNUNET_free (nh->reg);
733 /* end of nat_api.c */