Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / transport / plugin_transport_udp_broadcasting.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010, 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file transport/plugin_transport_udp_broadcasting.c
23  * @brief Neighbour discovery with UDP
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "plugin_transport_udp.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
40
41 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
42
43 /* *********** Cryogenic ********** */
44 #if LINUX
45 #include <sys/stat.h>
46 #include <fcntl.h>
47
48 #include <sys/ioctl.h>
49 #include <sys/select.h>
50 #include <sys/time.h>
51
52 #define PM_MAGIC 'k'
53 #define PM_SET_DELAY_AND_TIMEOUT _IOW (PM_MAGIC, 1, struct pm_times)
54
55 struct pm_times
56 {
57   unsigned long delay_msecs;
58   unsigned long timeout_msecs;
59 };
60 #endif
61 /************************************/
62
63
64 struct UDP_Beacon_Message
65 {
66   /**
67    * Message header.
68    */
69   struct GNUNET_MessageHeader header;
70
71   /**
72    * What is the identity of the sender
73    */
74   struct GNUNET_PeerIdentity sender;
75 };
76
77
78 struct BroadcastAddress
79 {
80   struct BroadcastAddress *next;
81
82   struct BroadcastAddress *prev;
83
84   /**
85    * ID of select broadcast task
86    */
87   struct GNUNET_SCHEDULER_Task *broadcast_task;
88
89   struct Plugin *plugin;
90
91   struct sockaddr *addr;
92
93   socklen_t addrlen;
94
95 #if LINUX
96   /**
97    * Cryogenic handle.
98    */
99   struct GNUNET_DISK_FileHandle *cryogenic_fd;
100
101   /**
102    * Time out for cryogenic.
103    */
104   struct pm_times cryogenic_times;
105 #endif
106 };
107
108
109 /**
110  * Client-specific context for #broadcast_mst_cb().
111  */
112 struct MstContext
113 {
114   struct Plugin *plugin;
115
116   const union UdpAddress *udp_addr;
117
118   size_t udp_addr_len;
119
120   /**
121    * ATS network type.
122    */
123   enum GNUNET_NetworkType ats_address_network_type;
124 };
125
126
127 /**
128  * Parse broadcast message received.
129  *
130  * @param cls the `struct Plugin`
131  * @param client the `struct MstContext` with sender address
132  * @param message the message we received
133  * @return #GNUNET_OK (always)
134  */
135 static int
136 broadcast_mst_cb (void *cls,
137                   const struct GNUNET_MessageHeader *message)
138 {
139   struct MstContext *mc = cls;
140   struct Plugin *plugin = mc->plugin;
141   struct GNUNET_HELLO_Address *address;
142   const struct GNUNET_MessageHeader *hello;
143   const struct UDP_Beacon_Message *msg;
144
145   msg = (const struct UDP_Beacon_Message *) message;
146
147   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
148       ntohs (msg->header.type))
149     return GNUNET_OK;
150   LOG (GNUNET_ERROR_TYPE_DEBUG,
151        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
152        ntohs (msg->header.size),
153        GNUNET_i2s (&msg->sender),
154        udp_address_to_string (NULL,
155                               mc->udp_addr,
156                               mc->udp_addr_len));
157   hello = (struct GNUNET_MessageHeader *) &msg[1];
158   address = GNUNET_HELLO_address_allocate (&msg->sender,
159                                            PLUGIN_NAME,
160                                            mc->udp_addr,
161                                            mc->udp_addr_len,
162                                            GNUNET_HELLO_ADDRESS_INFO_NONE);
163   plugin->env->receive (plugin->env->cls,
164                         address,
165                         NULL,
166                         hello);
167   GNUNET_HELLO_address_free (address);
168   GNUNET_STATISTICS_update (plugin->env->stats,
169                             _ ("# Multicast HELLO beacons received via UDP"),
170                             1, GNUNET_NO);
171   return GNUNET_OK;
172 }
173
174
175 /**
176  * We received a broadcast message.  Process it and all subsequent
177  * messages in the same packet.
178  *
179  * @param plugin the UDP plugin
180  * @param buf the buffer with the message(s)
181  * @param size number of bytes in @a buf
182  * @param udp_addr address of the sender
183  * @param udp_addr_len number of bytes in @a udp_addr
184  * @param network_type network type of the sender's address
185  */
186 void
187 udp_broadcast_receive (struct Plugin *plugin,
188                        const char *buf,
189                        ssize_t size,
190                        const union UdpAddress *udp_addr,
191                        size_t udp_addr_len,
192                        enum GNUNET_NetworkType network_type)
193 {
194   struct GNUNET_MessageStreamTokenizer *broadcast_mst;
195   struct MstContext mc;
196
197   broadcast_mst = GNUNET_MST_create (&broadcast_mst_cb,
198                                      &mc);
199   mc.plugin = plugin;
200   mc.udp_addr = udp_addr;
201   mc.udp_addr_len = udp_addr_len;
202   mc.ats_address_network_type = network_type;
203   GNUNET_MST_from_buffer (broadcast_mst,
204                           buf, size,
205                           GNUNET_NO,
206                           GNUNET_NO);
207   GNUNET_MST_destroy (broadcast_mst);
208 }
209
210
211 static unsigned int
212 prepare_beacon (struct Plugin *plugin,
213                 struct UDP_Beacon_Message *msg)
214 {
215   uint16_t hello_size;
216   uint16_t msg_size;
217
218   const struct GNUNET_MessageHeader *hello;
219
220   hello = plugin->env->get_our_hello ();
221   if (NULL == hello)
222     return 0;
223   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
224   msg_size = hello_size + sizeof(struct UDP_Beacon_Message);
225
226   if ((hello_size < (sizeof(struct GNUNET_MessageHeader)))||
227       (msg_size > (UDP_MTU)))
228     return 0;
229
230   msg->sender = *(plugin->env->my_identity);
231   msg->header.size = htons (msg_size);
232   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
233   GNUNET_memcpy (&msg[1], hello, hello_size);
234   return msg_size;
235 }
236
237
238 static void
239 udp_ipv4_broadcast_send (void *cls)
240 {
241   struct BroadcastAddress *baddr = cls;
242   struct Plugin *plugin = baddr->plugin;
243   int sent;
244   uint16_t msg_size;
245   char buf[65536] GNUNET_ALIGN;
246
247   baddr->broadcast_task = NULL;
248
249   msg_size = prepare_beacon (plugin, (struct UDP_Beacon_Message *) &buf);
250   if (0 != msg_size)
251   {
252     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
253
254     addr->sin_port = htons (plugin->port);
255     sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
256                                          (const struct sockaddr *) addr,
257                                          baddr->addrlen);
258     if (sent == GNUNET_SYSERR)
259     {
260       if ((ENETUNREACH == errno) || (ENETDOWN == errno))
261       {
262         /* "Network unreachable" or "Network down"
263          *
264          * This indicates that we just do not have network connectivity
265          */
266         GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
267                     "Network connectivity is down, cannot send beacon!\n");
268       }
269       else
270         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
271     }
272     else
273     {
274       LOG (GNUNET_ERROR_TYPE_DEBUG,
275            "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
276            GNUNET_a2s (baddr->addr, baddr->addrlen));
277     }
278   }
279
280 #if LINUX
281   /*
282    * Cryogenic
283    */
284   if (NULL != baddr->cryogenic_fd)
285   {
286     baddr->cryogenic_times.delay_msecs =
287       (plugin->broadcast_interval.rel_value_us / 1000.0) * 0.5;
288     baddr->cryogenic_times.timeout_msecs =
289       (plugin->broadcast_interval.rel_value_us / 1000.0) * 1.5;
290
291     if (ioctl (baddr->cryogenic_fd->fd,
292                PM_SET_DELAY_AND_TIMEOUT,
293                &baddr->cryogenic_times) < 0)
294     {
295       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
296       baddr->broadcast_task =
297         GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
298                                       &udp_ipv4_broadcast_send, baddr);
299     }
300     else
301       GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
302                                        baddr->cryogenic_fd,
303                                        &udp_ipv4_broadcast_send,
304                                        baddr);
305   }
306   else
307 #endif
308   baddr->broadcast_task =
309     GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
310                                   &udp_ipv4_broadcast_send, baddr);
311 }
312
313
314 static void
315 udp_ipv6_broadcast_send (void *cls)
316 {
317   struct BroadcastAddress *baddr = cls;
318   struct Plugin *plugin = baddr->plugin;
319   ssize_t sent;
320   uint16_t msg_size;
321   char buf[65536] GNUNET_ALIGN;
322   const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
323
324   baddr->broadcast_task = NULL;
325
326   msg_size = prepare_beacon (plugin, (struct UDP_Beacon_Message *) &buf);
327   /* Note: unclear if this actually works to limit the multicast to
328      the specified interface as we're not (necessarily) using a
329      link-local multicast group and the kernel suggests that the
330      scope ID is only respected for link-local addresses; however,
331      if the scope ID is ignored, the kernel should just multicast
332      on ALL interfaces, which is merely slightly less efficient;
333      in that case, we might want to revert to only doing this
334      once, and not per interface (hard to test...) */
335   plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
336   sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
337                                        (const struct sockaddr *)
338                                        &plugin->ipv6_multicast_address,
339                                        sizeof(struct sockaddr_in6));
340   plugin->ipv6_multicast_address.sin6_scope_id = 0;
341   if (sent == GNUNET_SYSERR)
342   {
343     if ((ENETUNREACH == errno) || (ENETDOWN == errno))
344     {
345       /* "Network unreachable" or "Network down"
346        *
347        * This indicates that this system is IPv6 enabled, but does not
348        * have a valid global IPv6 address assigned
349        */
350       GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
351                   "Network connectivity is down, cannot send beacon!\n");
352     }
353     else
354       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
355   }
356   else
357   {
358     LOG (GNUNET_ERROR_TYPE_DEBUG,
359          "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
360          (int) sent,
361          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
362                      sizeof(struct sockaddr_in6)));
363   }
364 #if LINUX
365   /*
366    * Cryogenic
367    */
368   if (NULL != baddr->cryogenic_fd)
369   {
370     baddr->cryogenic_times.delay_msecs =
371       (plugin->broadcast_interval.rel_value_us / 1000.0) * 0.5;
372     baddr->cryogenic_times.timeout_msecs =
373       (plugin->broadcast_interval.rel_value_us / 1000.0) * 1.5;
374
375     if (ioctl (baddr->cryogenic_fd->fd,
376                PM_SET_DELAY_AND_TIMEOUT,
377                &baddr->cryogenic_times) < 0)
378     {
379       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
380       baddr->broadcast_task =
381         GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
382                                       &udp_ipv6_broadcast_send, baddr);
383     }
384     else
385       GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
386                                        baddr->cryogenic_fd,
387                                        &udp_ipv6_broadcast_send,
388                                        baddr);
389   }
390   else
391 #endif
392   baddr->broadcast_task =
393     GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
394                                   &udp_ipv6_broadcast_send, baddr);
395 }
396
397
398 /**
399  * Callback function invoked for each interface found.
400  *
401  * @param cls closure with the `struct Plugin`
402  * @param name name of the interface (can be NULL for unknown)
403  * @param isDefault is this presumably the default interface
404  * @param addr address of this interface (can be NULL for unknown or unassigned)
405  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
406  * @param netmask the network mask (can be NULL for unknown or unassigned)
407  * @param addrlen length of the address
408  * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
409  */
410 static int
411 iface_proc (void *cls,
412             const char *name,
413             int isDefault,
414             const struct sockaddr *addr,
415             const struct sockaddr *broadcast_addr,
416             const struct sockaddr *netmask, socklen_t addrlen)
417 {
418   struct Plugin *plugin = cls;
419   struct BroadcastAddress *ba;
420   enum GNUNET_NetworkType network;
421
422   if (NULL == addr)
423     return GNUNET_OK;
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425               "address %s for interface %s %p\n ",
426               GNUNET_a2s (addr, addrlen), name, addr);
427   if (NULL == broadcast_addr)
428     return GNUNET_OK;
429   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430               "broadcast address %s for interface %s %p\n ",
431               GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
433               GNUNET_a2s (netmask, addrlen), name, netmask);
434
435   network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr,
436                                            addrlen);
437   if (GNUNET_NT_LOOPBACK == network)
438   {
439     /* Broadcasting on loopback does not make sense */
440     return GNUNET_YES;
441   }
442
443   ba = GNUNET_new (struct BroadcastAddress);
444   ba->plugin = plugin;
445   ba->addr = GNUNET_malloc (addrlen);
446   GNUNET_memcpy (ba->addr, broadcast_addr, addrlen);
447   ba->addrlen = addrlen;
448
449   if ((GNUNET_YES == plugin->enable_ipv4) &&
450       (NULL != plugin->sockv4) &&
451       (addrlen == sizeof(struct sockaddr_in)))
452   {
453 #if LINUX
454     /*
455      * setup Cryogenic FD for ipv4 broadcasting
456      */
457     char *filename;
458
459     GNUNET_asprintf (&filename,
460                      "/dev/cryogenic/%s",
461                      name);
462     if (0 == access (name, R_OK))
463     {
464       ba->cryogenic_fd =
465         GNUNET_DISK_file_open (filename,
466                                GNUNET_DISK_OPEN_WRITE,
467                                GNUNET_DISK_PERM_NONE);
468     }
469     GNUNET_free (filename);
470 #endif
471     ba->broadcast_task =
472       GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
473   }
474   if ((GNUNET_YES == plugin->enable_ipv6) &&
475       (NULL != plugin->sockv6) &&
476       (addrlen == sizeof(struct sockaddr_in6)))
477   {
478     /* Create IPv6 multicast request */
479     struct ipv6_mreq multicastRequest;
480     const struct sockaddr_in6 *s6 = (const struct
481                                      sockaddr_in6 *) broadcast_addr;
482
483     multicastRequest.ipv6mr_multiaddr =
484       plugin->ipv6_multicast_address.sin6_addr;
485     /* http://tools.ietf.org/html/rfc2553#section-5.2:
486      *
487      * IPV6_JOIN_GROUP
488      *
489      * Join a multicast group on a specified local interface.  If the
490      * interface index is specified as 0, the kernel chooses the local
491      * interface.  For example, some kernels look up the multicast
492      * group in the normal IPv6 routing table and using the resulting
493      * interface; we do this for each interface, so no need to use
494      * zero (anymore...).
495      */
496     multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
497
498     /* Join the multicast group */
499     if (GNUNET_OK !=
500         GNUNET_NETWORK_socket_setsockopt
501           (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
502           &multicastRequest, sizeof(multicastRequest)))
503     {
504       LOG (GNUNET_ERROR_TYPE_WARNING,
505            "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
506     }
507     else
508     {
509 #if LINUX
510       /*
511        * setup Cryogenic FD for ipv6 broadcasting
512        */
513       char *filename;
514
515       GNUNET_asprintf (&filename,
516                        "/dev/cryogenic/%s",
517                        name);
518       if (0 == access (name, R_OK))
519       {
520         ba->cryogenic_fd =
521           GNUNET_DISK_file_open (filename,
522                                  GNUNET_DISK_OPEN_WRITE,
523                                  GNUNET_DISK_PERM_NONE);
524       }
525       GNUNET_free (filename);
526 #endif
527       ba->broadcast_task =
528         GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
529     }
530   }
531   GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
532                                plugin->broadcast_tail, ba);
533   return GNUNET_OK;
534 }
535
536
537 /**
538  * Setup broadcasting subsystem.
539  *
540  * @param plugin
541  * @param server_addrv6
542  * @param server_addrv4
543  */
544 void
545 setup_broadcast (struct Plugin *plugin,
546                  struct sockaddr_in6 *server_addrv6,
547                  struct sockaddr_in *server_addrv4)
548 {
549   if (GNUNET_YES ==
550       GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
551                                             "topology",
552                                             "FRIENDS-ONLY"))
553   {
554     LOG (GNUNET_ERROR_TYPE_WARNING,
555          _ (
556            "Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
557     return;
558   }
559
560   if (GNUNET_YES != plugin->enable_broadcasting)
561     return; /* We do not send, just receive */
562
563   /* create IPv4 broadcast socket */
564   if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
565   {
566     static int yes = 1;
567
568     if (GNUNET_NETWORK_socket_setsockopt
569           (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
570           sizeof(int)) != GNUNET_OK)
571     {
572       LOG (GNUNET_ERROR_TYPE_WARNING,
573            _ (
574              "Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
575            ntohs (server_addrv4->sin_port));
576     }
577   }
578   /* create IPv6 multicast socket */
579   if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
580   {
581     memset (&plugin->ipv6_multicast_address, 0, sizeof(struct sockaddr_in6));
582     GNUNET_assert (1 ==
583                    inet_pton (AF_INET6, "FF05::13B",
584                               &plugin->ipv6_multicast_address.sin6_addr));
585     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
586     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
587   }
588   GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
589 }
590
591
592 /**
593  * Stop broadcasting subsystem.
594  *
595  * @param plugin
596  */
597 void
598 stop_broadcast (struct Plugin *plugin)
599 {
600   if (GNUNET_YES == plugin->enable_broadcasting)
601   {
602     /* Disable broadcasting */
603     while (plugin->broadcast_head != NULL)
604     {
605       struct BroadcastAddress *p = plugin->broadcast_head;
606
607       if (p->broadcast_task != NULL)
608       {
609         GNUNET_SCHEDULER_cancel (p->broadcast_task);
610         p->broadcast_task = NULL;
611       }
612       if ((GNUNET_YES == plugin->enable_ipv6) &&
613           (NULL != plugin->sockv6) &&
614           (p->addrlen == sizeof(struct sockaddr_in6)))
615       {
616         /* Create IPv6 multicast request */
617         struct ipv6_mreq multicastRequest;
618         const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
619
620         multicastRequest.ipv6mr_multiaddr =
621           plugin->ipv6_multicast_address.sin6_addr;
622         multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
623
624         /* Leave the multicast group */
625         if (GNUNET_OK ==
626             GNUNET_NETWORK_socket_setsockopt
627               (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
628               &multicastRequest, sizeof(multicastRequest)))
629         {
630           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
631         }
632         else
633         {
634           LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
635         }
636       }
637
638 #if LINUX
639       GNUNET_DISK_file_close (p->cryogenic_fd);
640 #endif
641       GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
642                                    plugin->broadcast_tail, p);
643       GNUNET_free (p->addr);
644       GNUNET_free (p);
645     }
646   }
647 }
648
649 /* end of plugin_transport_udp_broadcasting.c */