fix issue with NAT logic overwriting port specified by user in manual hole punch...
[oweals/gnunet.git] / src / nat / nat_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2007-2017 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @author Christian Grothoff
23  * @author Milan Bouchet-Valat
24  *
25  * @file nat/nat_api.c
26  * Service for handling UPnP and NAT-PMP port forwarding
27  * and external IP address retrieval
28  */
29 #include "platform.h"
30 #include "gnunet_nat_service.h"
31 #include "nat.h"
32 #include "nat_stun.h"
33
34
35 /**
36  * Entry in DLL of addresses of this peer.
37  */
38 struct AddrEntry
39 {
40
41   /**
42    * DLL.
43    */
44   struct AddrEntry *next;
45
46   /**
47    * DLL.
48    */
49   struct AddrEntry *prev;
50
51   /**
52    * Address class of the address.
53    */
54   enum GNUNET_NAT_AddressClass ac;
55
56   /**
57    * Number of bytes that follow.
58    */
59   socklen_t addrlen;
60 };
61
62
63 /**
64  * Handle for active NAT registrations.
65  */
66 struct GNUNET_NAT_Handle
67 {
68
69   /**
70    * Configuration we use.
71    */
72   const struct GNUNET_CONFIGURATION_Handle *cfg;
73
74   /**
75    * Message queue for communicating with the NAT service.
76    */
77   struct GNUNET_MQ_Handle *mq;
78
79   /**
80    * Our registration message.
81    */
82   struct GNUNET_MessageHeader *reg;
83
84   /**
85    * Head of address DLL.
86    */
87   struct AddrEntry *ae_head;
88
89   /**
90    * Tail of address DLL.
91    */
92   struct AddrEntry *ae_tail;
93
94   /**
95    * Function to call when our addresses change.
96    */
97   GNUNET_NAT_AddressCallback address_callback;
98
99   /**
100    * Function to call when another peer requests connection reversal.
101    */
102   GNUNET_NAT_ReversalCallback reversal_callback;
103
104   /**
105    * Closure for the various callbacks.
106    */
107   void *callback_cls;
108
109   /**
110    * Task scheduled to reconnect to the service.
111    */
112   struct GNUNET_SCHEDULER_Task *reconnect_task;
113
114   /**
115    * How long to wait until we reconnect.
116    */
117   struct GNUNET_TIME_Relative reconnect_delay;
118 };
119
120
121 /**
122  * Task to connect to the NAT service.
123  *
124  * @param cls our `struct GNUNET_NAT_Handle *`
125  */
126 static void
127 do_connect (void *cls);
128
129
130 /**
131  * Task to connect to the NAT service.
132  *
133  * @param nh handle to reconnect
134  */
135 static void
136 reconnect (struct GNUNET_NAT_Handle *nh)
137 {
138   struct AddrEntry *ae;
139
140   if (NULL != nh->mq)
141   {
142     GNUNET_MQ_destroy (nh->mq);
143     nh->mq = NULL;
144   }
145   while (NULL != (ae = nh->ae_head))
146   {
147     GNUNET_CONTAINER_DLL_remove (nh->ae_head,
148                                  nh->ae_tail,
149                                  ae);
150     nh->address_callback (nh->callback_cls,
151                           GNUNET_NO,
152                           ae->ac,
153                           (const struct sockaddr *) &ae[1],
154                           ae->addrlen);
155     GNUNET_free (ae);
156   }
157   nh->reconnect_delay
158     = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay);
159   nh->reconnect_task
160     = GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay,
161                                     &do_connect,
162                                     nh);
163 }
164
165
166 /**
167  * Check connection reversal request.
168  *
169  * @param cls our `struct GNUNET_NAT_Handle`
170  * @param crm the message
171  * @return #GNUNET_OK if @a crm is well-formed
172  */
173 static int
174 check_connection_reversal_request (void *cls,
175                                    const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
176 {
177   if (ntohs (crm->header.size) !=
178       sizeof (*crm) +
179       sizeof (struct sockaddr_in) )
180   {
181     GNUNET_break (0);
182     return GNUNET_SYSERR;
183   }
184   return GNUNET_OK;
185 }
186
187
188 /**
189  * Handle connection reversal request.
190  *
191  * @param cls our `struct GNUNET_NAT_Handle`
192  * @param crm the message
193  */
194 static void
195 handle_connection_reversal_request (void *cls,
196                                     const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
197 {
198   struct GNUNET_NAT_Handle *nh = cls;
199
200   nh->reversal_callback (nh->callback_cls,
201                          (const struct sockaddr *) &crm[1],
202                          sizeof (struct sockaddr_in));
203 }
204
205
206 /**
207  * Check address change notification.
208  *
209  * @param cls our `struct GNUNET_NAT_Handle`
210  * @param acn the message
211  * @return #GNUNET_OK if @a crm is well-formed
212  */
213 static int
214 check_address_change_notification (void *cls,
215                                    const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
216 {
217   size_t alen = ntohs (acn->header.size) - sizeof (*acn);
218
219   switch (alen)
220   {
221   case sizeof (struct sockaddr_in):
222     {
223       const struct sockaddr_in *s4
224         = (const struct sockaddr_in *) &acn[1];
225       if (AF_INET != s4->sin_family)
226       {
227         GNUNET_break (0);
228         return GNUNET_SYSERR;
229       }
230     }
231     break;
232   case sizeof (struct sockaddr_in6):
233     {
234       const struct sockaddr_in6 *s6
235         = (const struct sockaddr_in6 *) &acn[1];
236       if (AF_INET6 != s6->sin6_family)
237       {
238         GNUNET_break (0);
239         return GNUNET_SYSERR;
240       }
241     }
242     break;
243   default:
244     GNUNET_break (0);
245     return GNUNET_SYSERR;
246   }
247   return GNUNET_OK;
248 }
249
250
251 /**
252  * Handle connection reversal request.
253  *
254  * @param cls our `struct GNUNET_NAT_Handle`
255  * @param acn the message
256  */
257 static void
258 handle_address_change_notification (void *cls,
259                                     const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
260 {
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;
266
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))
271   {
272     ae = GNUNET_malloc (sizeof (*ae) + alen);
273     ae->ac = ac;
274     ae->addrlen = alen;
275     GNUNET_memcpy (&ae[1],
276                    sa,
277                    alen);
278     GNUNET_CONTAINER_DLL_insert (nh->ae_head,
279                                  nh->ae_tail,
280                                  ae);
281   }
282   else
283   {
284     for (ae = nh->ae_head; NULL != ae; ae = ae->next)
285       if ( (ae->addrlen == alen) &&
286            (0 == memcmp (&ae[1],
287                          sa,
288                          alen)) )
289         break;
290     if (NULL == ae)
291     {
292       GNUNET_break (0);
293       reconnect (nh);
294       return;
295     }
296     GNUNET_CONTAINER_DLL_remove (nh->ae_head,
297                                  nh->ae_tail,
298                                  ae);
299     GNUNET_free (ae);
300   }
301   nh->address_callback (nh->callback_cls,
302                         ntohl (acn->add_remove),
303                         ac,
304                         sa,
305                         alen);
306 }
307
308
309 /**
310  * Handle queue errors by reconnecting to NAT.
311  *
312  * @param cls the `struct GNUNET_NAT_Handle *`
313  * @param error details about the error
314  */
315 static void
316 mq_error_handler (void *cls,
317                   enum GNUNET_MQ_Error error)
318 {
319   struct GNUNET_NAT_Handle *nh = cls;
320
321   reconnect (nh);
322 }
323
324
325 /**
326  * Task to connect to the NAT service.
327  *
328  * @param cls our `struct GNUNET_NAT_Handle *`
329  */
330 static void
331 do_connect (void *cls)
332 {
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,
338                            nh),
339     GNUNET_MQ_hd_var_size (address_change_notification,
340                            GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE,
341                            struct GNUNET_NAT_AddressChangeNotificationMessage,
342                            nh),
343     GNUNET_MQ_handler_end ()
344   };
345   struct GNUNET_MQ_Envelope *env;
346
347   nh->reconnect_task = NULL;
348   nh->mq = GNUNET_CLIENT_connect (nh->cfg,
349                                   "nat",
350                                   handlers,
351                                   &mq_error_handler,
352                                   nh);
353   if (NULL == nh->mq)
354   {
355     reconnect (nh);
356     return;
357   }
358   env = GNUNET_MQ_msg_copy (nh->reg);
359   GNUNET_MQ_send (nh->mq,
360                   env);
361 }
362
363
364 /**
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.
371  *
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
383  */
384 struct GNUNET_NAT_Handle *
385 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
386                      const char *config_section,
387                      uint8_t proto,
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,
393                      void *callback_cls)
394 {
395   struct GNUNET_NAT_Handle *nh;
396   struct GNUNET_NAT_RegisterMessage *rm;
397   size_t len;
398   size_t str_len;
399   char *off;
400
401   len = 0;
402   for (unsigned int i=0;i<num_addrs;i++)
403     len += addrlens[i];
404   str_len = strlen (config_section) + 1;
405   len += str_len;
406   if ( (len > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*rm)) ||
407        (num_addrs > UINT16_MAX) )
408   {
409     GNUNET_break (0);
410     return NULL;
411   }
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;
420   rm->proto = proto;
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++)
425   {
426     switch (addrs[i]->sa_family)
427     {
428     case AF_INET:
429       if (sizeof (struct sockaddr_in) != addrlens[i])
430       {
431         GNUNET_break (0);
432         return NULL;
433       }
434       break;
435     case AF_INET6:
436       if (sizeof (struct sockaddr_in6) != addrlens[i])
437       {
438         GNUNET_break (0);
439         return NULL;
440       }
441       break;
442 #if AF_UNIX
443     case AF_UNIX:
444       if (sizeof (struct sockaddr_un) != addrlens[i])
445       {
446         GNUNET_break (0);
447         return NULL;
448       }
449       break;
450 #endif
451     default:
452       GNUNET_break (0);
453       return NULL;
454     }
455     GNUNET_memcpy (off,
456                    addrs[i],
457                    addrlens[i]);
458     off += addrlens[i];
459   }
460   GNUNET_memcpy (off,
461                  config_section,
462                  str_len);
463
464   nh = GNUNET_new (struct GNUNET_NAT_Handle);
465   nh->reg = &rm->header;
466   nh->cfg = cfg;
467   nh->address_callback = address_callback;
468   nh->reversal_callback = reversal_callback;
469   nh->callback_cls = callback_cls;
470   do_connect (nh);
471   return nh;
472 }
473
474
475 /**
476  * Check if an incoming message is a STUN message.
477  *
478  * @param data the packet
479  * @param len the length of the packet in @a data
480  * @return #GNUNET_YES if @a data is a STUN packet,
481  *         #GNUNET_NO if the packet is invalid (not a stun packet)
482  */
483 static int
484 test_stun_packet (const void *data,
485                   size_t len)
486 {
487   const struct stun_header *hdr;
488   const struct stun_attr *attr;
489   uint32_t advertised_message_size;
490   uint32_t message_magic_cookie;
491
492   /* On entry, 'len' is the length of the UDP payload. After the
493    * initial checks it becomes the size of unprocessed options,
494    * while 'data' is advanced accordingly.
495    */
496   if (len < sizeof(struct stun_header))
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499                 "STUN packet too short (only %d, wanting at least %d)\n",
500                 (int) len,
501                 (int) sizeof (struct stun_header));
502     return GNUNET_NO;
503   }
504   hdr = (const struct stun_header *) data;
505   /* Skip header as it is already in hdr */
506   len -= sizeof (struct stun_header);
507   data += sizeof (struct stun_header);
508
509   /* len as advertised in the message */
510   advertised_message_size = ntohs (hdr->msglen);
511
512   message_magic_cookie = ntohl (hdr->magic);
513   /* Compare if the cookie match */
514   if (STUN_MAGIC_COOKIE != message_magic_cookie)
515   {
516     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517                 "Invalid magic cookie for STUN\n");
518     return GNUNET_NO;
519   }
520
521   if (advertised_message_size > len)
522   {
523     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524                 "Scrambled STUN packet length (got %d, expecting %d)\n",
525                 advertised_message_size,
526                 (int)len);
527     return GNUNET_NO;
528   }
529   len = advertised_message_size;
530   while (len > 0)
531   {
532     if (len < sizeof (struct stun_attr))
533     {
534       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535                   "Attribute too short in STUN packet (got %d, expecting %d)\n",
536                   (int) len,
537                   (int) sizeof(struct stun_attr));
538       return GNUNET_NO;
539     }
540     attr = (const struct stun_attr *) data;
541
542     /* compute total attribute length */
543     advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr);
544
545     /* Check if we still have space in our buffer */
546     if (advertised_message_size > len)
547     {
548       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549                   "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
550                   advertised_message_size,
551                   (int) len);
552       return GNUNET_NO;
553     }
554     data += advertised_message_size;
555     len -= advertised_message_size;
556   }
557   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558               "STUN Packet, msg %04x, length: %d\n",
559               ntohs (hdr->msgtype),
560               advertised_message_size);
561   return GNUNET_OK;
562 }
563
564
565 /**
566  * Handle an incoming STUN message.  This function is useful as
567  * some GNUnet service may be listening on a UDP port and might
568  * thus receive STUN messages while trying to receive other data.
569  * In this case, this function can be used to process replies
570  * to STUN requests.
571  *
572  * The function does some basic sanity checks on packet size and
573  * content, try to extract a bit of information.
574  *
575  * At the moment this only processes BIND requests, and returns the
576  * externally visible address of the request to the rest of the
577  * NAT logic.
578  *
579  * @param nh handle to the NAT service
580  * @param sender_addr address from which we got @a data
581  * @param sender_addr_len number of bytes in @a sender_addr
582  * @param data the packet
583  * @param data_size number of bytes in @a data
584  * @return #GNUNET_OK on success
585  *         #GNUNET_NO if the packet is not a STUN packet
586  *         #GNUNET_SYSERR on internal error handling the packet
587  */
588 int
589 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
590                                const struct sockaddr *sender_addr,
591                                size_t sender_addr_len,
592                                const void *data,
593                                size_t data_size)
594 {
595   struct GNUNET_MQ_Envelope *env;
596   struct GNUNET_NAT_HandleStunMessage *hsn;
597   char *buf;
598
599   if (GNUNET_YES !=
600       test_stun_packet (data,
601                         data_size))
602     return GNUNET_NO;
603   if (NULL == nh->mq)
604     return GNUNET_SYSERR;
605   env = GNUNET_MQ_msg_extra (hsn,
606                              data_size + sender_addr_len,
607                              GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN);
608   hsn->sender_addr_size = htons ((uint16_t) sender_addr_len);
609   hsn->payload_size = htons ((uint16_t) data_size);
610   buf = (char *) &hsn[1];
611   GNUNET_memcpy (buf,
612                  sender_addr,
613                  sender_addr_len);
614   buf += sender_addr_len;
615   GNUNET_memcpy (buf,
616                  data,
617                  data_size);
618   GNUNET_MQ_send (nh->mq,
619                   env);
620   return GNUNET_OK;
621 }
622
623
624 /**
625  * Test if the given address is (currently) a plausible IP address for
626  * this peer.  Mostly a convenience function so that clients do not
627  * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
628  * has returned so far.
629  *
630  * @param nh the handle returned by register
631  * @param addr IP address to test (IPv4 or IPv6)
632  * @param addrlen number of bytes in @a addr
633  * @return #GNUNET_YES if the address is plausible,
634  *         #GNUNET_NO if the address is not plausible,
635  *         #GNUNET_SYSERR if the address is malformed
636  */
637 int
638 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
639                          const void *addr,
640                          socklen_t addrlen)
641 {
642   struct AddrEntry *ae;
643
644   if ( (addrlen != sizeof (struct sockaddr_in)) &&
645        (addrlen != sizeof (struct sockaddr_in6)) )
646   {
647     GNUNET_break (0);
648     return GNUNET_SYSERR;
649   }
650   for (ae = nh->ae_head; NULL != ae; ae = ae->next)
651     if ( (addrlen == ae->addrlen) &&
652          (0 == memcmp (addr,
653                        &ae[1],
654                        addrlen)) )
655       return GNUNET_YES;
656   return GNUNET_NO;
657 }
658
659
660 /**
661  * We learned about a peer (possibly behind NAT) so run the
662  * gnunet-nat-client to send dummy ICMP responses to cause
663  * that peer to connect to us (connection reversal).
664  *
665  * @param nh handle (used for configuration)
666  * @param local_sa our local address of the peer (IPv4-only)
667  * @param remote_sa the remote address of the peer (IPv4-only)
668  * @return #GNUNET_SYSERR on error,
669  *         #GNUNET_NO if connection reversal is unavailable,
670  *         #GNUNET_OK otherwise (presumably in progress)
671  */
672 int
673 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
674                              const struct sockaddr_in *local_sa,
675                              const struct sockaddr_in *remote_sa)
676 {
677   struct GNUNET_MQ_Envelope *env;
678   struct GNUNET_NAT_RequestConnectionReversalMessage *req;
679   char *buf;
680
681   if (NULL == nh->mq)
682     return GNUNET_SYSERR;
683   GNUNET_break (AF_INET == local_sa->sin_family);
684   GNUNET_break (AF_INET == remote_sa->sin_family);
685   env = GNUNET_MQ_msg_extra (req,
686                              2 * sizeof (struct sockaddr_in),
687                              GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL);
688   req->local_addr_size = htons (sizeof (struct sockaddr_in));
689   req->remote_addr_size = htons (sizeof (struct sockaddr_in));
690   buf = (char *) &req[1];
691   GNUNET_memcpy (buf,
692                  local_sa,
693                  sizeof (struct sockaddr_in));
694   buf += sizeof (struct sockaddr_in);
695   GNUNET_memcpy (buf,
696                  remote_sa,
697                  sizeof (struct sockaddr_in));
698   GNUNET_MQ_send (nh->mq,
699                   env);
700   return GNUNET_OK;
701 }
702
703
704 /**
705  * Stop port redirection and public IP address detection for the given
706  * handle.  This frees the handle, after having sent the needed
707  * commands to close open ports.
708  *
709  * @param nh the handle to stop
710  */
711 void
712 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
713 {
714   if (NULL != nh->mq)
715   {
716     GNUNET_MQ_destroy (nh->mq);
717     nh->mq = NULL;
718   }
719   if (NULL != nh->reconnect_task)
720   {
721     GNUNET_SCHEDULER_cancel (nh->reconnect_task);
722     nh->reconnect_task = NULL;
723   }
724   GNUNET_free (nh->reg);
725   GNUNET_free (nh);
726 }
727
728
729 /* end of nat_api.c */