2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_blacklist.c
23 * @brief blacklisting implementation
24 * @author Christian Grothoff
25 * @author Matthias Wachs
26 * @details This is the blacklisting component of transport service. With
27 * blacklisting it is possible to deny connections to specific peers of
28 * to use a specific plugin to a specific peer. Peers can be blacklisted using
29 * the configuration or a blacklist client can be asked.
31 * To blacklist peers using the configuration you have to add a section to your
32 * configuration containing the peer id of the peer to blacklist and the plugin
36 * To blacklist connections to P565... on peer AG2P... using tcp add:
37 * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
38 * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G = tcp
40 * To blacklist connections to P565... on peer AG2P... using all plugins add:
41 * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
42 * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G =
44 * You can also add a blacklist client usign the blacklist api. On a blacklist
45 * check, blacklisting first checks internally if the peer is blacklisted and
46 * if not, it asks the blacklisting clients. Clients are asked if it is OK to
47 * connect to a peer ID, the plugin is omitted.
49 * On blacklist check for (peer, plugin)
50 * - Do we have a local blacklist entry for this peer and this plugin?
51 * - YES: disallow connection
52 * - Do we have a local blacklist entry for this peer and all plugins?
53 * - YES: disallow connection
54 * - Does one of the clients disallow?
55 * - YES: disallow connection
59 #include "gnunet-service-transport.h"
60 #include "gnunet-service-transport_blacklist.h"
61 #include "gnunet-service-transport_neighbours.h"
62 #include "transport.h"
65 * Size of the blacklist hash map.
67 #define TRANSPORT_BLACKLIST_HT_SIZE 64
71 * Context we use when performing a blacklist check.
73 struct GST_BlacklistCheck;
77 * Information kept for each client registered to perform
83 * This is a linked list.
85 struct Blacklisters *next;
88 * This is a linked list.
90 struct Blacklisters *prev;
93 * Client responsible for this entry.
95 struct GNUNET_SERVER_Client *client;
98 * Blacklist check that we're currently performing (or NULL
99 * if we're performing one that has been cancelled).
101 struct GST_BlacklistCheck *bc;
104 * Set to #GNUNET_YES if we're currently waiting for a reply.
106 int waiting_for_reply;
109 * #GNUNET_YES if we have to call receive_done for this client
111 int call_receive_done;
118 * Context we use when performing a blacklist check.
120 struct GST_BlacklistCheck
124 * This is a linked list.
126 struct GST_BlacklistCheck *next;
129 * This is a linked list.
131 struct GST_BlacklistCheck *prev;
134 * Peer being checked.
136 struct GNUNET_PeerIdentity peer;
139 * Continuation to call with the result.
141 GST_BlacklistTestContinuation cont;
144 * Closure for @e cont.
149 * Current transmission request handle for this client, or NULL if no
150 * request is pending.
152 struct GNUNET_SERVER_TransmitHandle *th;
155 * Our current position in the blacklisters list.
157 struct Blacklisters *bl_pos;
160 * Current task performing the check.
162 struct GNUNET_SCHEDULER_Task * task;
168 * Head of DLL of active blacklisting queries.
170 static struct GST_BlacklistCheck *bc_head;
173 * Tail of DLL of active blacklisting queries.
175 static struct GST_BlacklistCheck *bc_tail;
178 * Head of DLL of blacklisting clients.
180 static struct Blacklisters *bl_head;
183 * Tail of DLL of blacklisting clients.
185 static struct Blacklisters *bl_tail;
188 * Hashmap of blacklisted peers. Values are of type 'char *' (transport names),
189 * can be NULL if we have no static blacklist.
191 static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
195 * Perform next action in the blacklist check.
197 * @param cls the `struct BlacklistCheck*`
201 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
205 * Called whenever a client is disconnected. Frees our
206 * resources associated with that client.
208 * @param cls closure (unused)
209 * @param client identification of the client
212 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
214 struct Blacklisters *bl;
215 struct GST_BlacklistCheck *bc;
219 for (bl = bl_head; bl != NULL; bl = bl->next)
221 if (bl->client != client)
223 for (bc = bc_head; bc != NULL; bc = bc->next)
225 if (bc->bl_pos != bl)
227 bc->bl_pos = bl->next;
230 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
233 if (bc->task == NULL)
234 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
236 GNUNET_CONTAINER_DLL_remove (bl_head, bl_tail, bl);
237 GNUNET_SERVER_client_drop (bl->client);
245 * Function to iterate over options in the blacklisting section for a peer.
248 * @param section name of the section
249 * @param option name of the option
250 * @param value value of the option
253 blacklist_cfg_iter (void *cls,
258 unsigned int *res = cls;
259 struct GNUNET_PeerIdentity peer;
264 GNUNET_CRYPTO_eddsa_public_key_from_string (option,
269 if ((NULL == value) || (0 == strcmp(value, "")))
271 /* Blacklist whole peer */
272 GST_blacklist_add_peer (&peer, NULL);
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274 _("Adding blacklisting entry for peer `%s'\n"),
279 plugs = GNUNET_strdup (value);
280 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283 _("Adding blacklisting entry for peer `%s':`%s'\n"),
284 GNUNET_i2s (&peer), pos);
285 GST_blacklist_add_peer (&peer, pos);
294 * Read blacklist configuration
296 * @param cfg the configuration handle
297 * @param my_id my peer identity
300 read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
301 const struct GNUNET_PeerIdentity *my_id)
304 unsigned int res = 0;
306 GNUNET_snprintf (cfg_sect,
308 "transport-blacklist-%s",
309 GNUNET_i2s_full (my_id));
310 GNUNET_CONFIGURATION_iterate_section_values (cfg,
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Loaded %u blacklisting entries from configuration\n",
321 * Start blacklist subsystem.
323 * @param server server used to accept clients from
324 * @param cfg configuration handle
325 * @param my_id my peer id
328 GST_blacklist_start (struct GNUNET_SERVER_Handle *server,
329 const struct GNUNET_CONFIGURATION_Handle *cfg,
330 const struct GNUNET_PeerIdentity *my_id)
332 GNUNET_assert (NULL != cfg);
333 GNUNET_assert (NULL != my_id);
334 read_blacklist_configuration (cfg, my_id);
335 GNUNET_SERVER_disconnect_notify (server,
336 &client_disconnect_notification,
342 * Free the given entry in the blacklist.
345 * @param key host identity (unused)
346 * @param value the blacklist entry
347 * @return #GNUNET_OK (continue to iterate)
350 free_blacklist_entry (void *cls,
351 const struct GNUNET_PeerIdentity *key,
356 GNUNET_free_non_null (be);
362 * Stop blacklist subsystem.
365 GST_blacklist_stop ()
367 if (NULL == blacklist)
369 GNUNET_CONTAINER_multipeermap_iterate (blacklist,
370 &free_blacklist_entry,
372 GNUNET_CONTAINER_multipeermap_destroy (blacklist);
378 * Transmit blacklist query to the client.
380 * @param cls the `struct GST_BlacklistCheck`
381 * @param size number of bytes allowed
382 * @param buf where to copy the message
383 * @return number of bytes copied to @a buf
386 transmit_blacklist_message (void *cls, size_t size, void *buf)
388 struct GST_BlacklistCheck *bc = cls;
389 struct Blacklisters *bl;
390 struct BlacklistMessage bm;
395 GNUNET_assert (bc->task == NULL);
396 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
398 "Failed to send blacklist test for peer `%s' to client\n",
399 GNUNET_i2s (&bc->peer));
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Sending blacklist test for peer `%s' to client %p\n",
404 GNUNET_i2s (&bc->peer), bc->bl_pos->client);
406 bm.header.size = htons (sizeof (struct BlacklistMessage));
407 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
408 bm.is_allowed = htonl (0);
410 memcpy (buf, &bm, sizeof (bm));
411 if (GNUNET_YES == bl->call_receive_done)
413 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
414 bl->call_receive_done = GNUNET_NO;
417 bl->waiting_for_reply = GNUNET_YES;
423 * Perform next action in the blacklist check.
425 * @param cls the `struct GST_BlacklistCheck *`
429 do_blacklist_check (void *cls,
430 const struct GNUNET_SCHEDULER_TaskContext *tc)
432 struct GST_BlacklistCheck *bc = cls;
433 struct Blacklisters *bl;
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "No other blacklist clients active, will allow neighbour `%s'\n",
441 GNUNET_i2s (&bc->peer));
443 bc->cont (bc->cont_cls, &bc->peer, GNUNET_OK);
444 GNUNET_CONTAINER_DLL_remove(bc_head, bc_tail, bc);
448 if ((bl->bc != NULL) || (bl->waiting_for_reply != GNUNET_NO))
449 return; /* someone else busy with this client */
452 GNUNET_SERVER_notify_transmit_ready (bl->client,
453 sizeof (struct BlacklistMessage),
454 GNUNET_TIME_UNIT_FOREVER_REL,
455 &transmit_blacklist_message, bc);
460 * Got the result about an existing connection from a new blacklister.
461 * Shutdown the neighbour if necessary.
464 * @param peer the neighbour that was investigated
465 * @param allowed #GNUNET_OK if we can keep it,
466 * #GNUNET_NO if we must shutdown the connection
469 confirm_or_drop_neighbour (void *cls,
470 const struct GNUNET_PeerIdentity *peer,
473 if (GNUNET_OK == allowed)
474 return; /* we're done */
475 GNUNET_STATISTICS_update (GST_stats,
476 gettext_noop ("# disconnects due to blacklist"), 1,
478 GST_neighbours_force_disconnect (peer);
483 * Closure for 'test_connection_ok'.
485 struct TestConnectionContext
488 * Is this the first neighbour we're checking?
493 * Handle to the blacklisting client we need to ask.
495 struct Blacklisters *bl;
500 * Test if an existing connection is still acceptable given a new
501 * blacklisting client.
503 * @param cls the `struct TestConnectionContext *`
504 * @param peer identity of the peer
505 * @param address the address
506 * @param state current state this peer is in
507 * @param state_timeout timeout for the current state of the peer
508 * @param bandwidth_in bandwidth assigned inbound
509 * @param bandwidth_out bandwidth assigned outbound
512 test_connection_ok (void *cls,
513 const struct GNUNET_PeerIdentity *peer,
514 const struct GNUNET_HELLO_Address *address,
515 enum GNUNET_TRANSPORT_PeerState state,
516 struct GNUNET_TIME_Absolute state_timeout,
517 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
518 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
520 struct TestConnectionContext *tcc = cls;
521 struct GST_BlacklistCheck *bc;
523 bc = GNUNET_new (struct GST_BlacklistCheck);
524 GNUNET_CONTAINER_DLL_insert (bc_head,
528 bc->cont = &confirm_or_drop_neighbour;
530 bc->bl_pos = tcc->bl;
531 if (GNUNET_YES == tcc->first)
533 /* all would wait for the same client, no need to
534 * create more than just the first task right now */
535 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
536 tcc->first = GNUNET_NO;
542 * Initialize a blacklisting client. We got a blacklist-init
543 * message from this client, add him to the list of clients
544 * to query for blacklisting.
547 * @param client the client
548 * @param message the blacklist-init message that was sent
551 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
552 const struct GNUNET_MessageHeader *message)
554 struct Blacklisters *bl;
555 struct TestConnectionContext tcc;
557 for (bl = bl_head; NULL != bl; bl = bl->next)
558 if (bl->client == client)
561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
565 GNUNET_SERVER_client_mark_monitor (client);
566 bl = GNUNET_new (struct Blacklisters);
568 bl->call_receive_done = GNUNET_YES;
569 GNUNET_SERVER_client_keep (client);
570 GNUNET_CONTAINER_DLL_insert_after (bl_head,
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "New blacklist client %p\n",
578 /* confirm that all existing connections are OK! */
580 tcc.first = GNUNET_YES;
581 GST_neighbours_iterate (&test_connection_ok, &tcc);
586 * A blacklisting client has sent us reply. Process it.
589 * @param client the client
590 * @param message the blacklist-init message that was sent
593 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message)
596 const struct BlacklistMessage *msg =
597 (const struct BlacklistMessage *) message;
598 struct Blacklisters *bl;
599 struct GST_BlacklistCheck *bc;
602 while ((bl != NULL) && (bl->client != client))
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Blacklist client disconnected\n");
608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 "Blacklist client %p sent reply for `%s'\n",
615 GNUNET_i2s(&msg->peer));
619 bl->waiting_for_reply = GNUNET_NO;
620 bl->call_receive_done = GNUNET_YES; /* Remember to call receive_done */
623 /* only run this if the blacklist check has not been
624 * cancelled in the meantime... */
625 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Blacklist check failed, peer not allowed\n");
629 bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
630 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
631 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
632 bl->call_receive_done = GNUNET_NO;
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Blacklist check succeeded, continuing with checks\n");
640 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
641 bl->call_receive_done = GNUNET_NO;
642 bc->bl_pos = bc->bl_pos->next;
643 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
646 /* check if any other blacklist checks are waiting for this blacklister */
647 for (bc = bc_head; bc != NULL; bc = bc->next)
648 if ((bc->bl_pos == bl) && (NULL == bc->task))
650 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
657 * Add the given peer to the blacklist (for the given transport).
659 * @param peer peer to blacklist
660 * @param transport_name transport to blacklist for this peer, NULL for all
663 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
664 const char *transport_name)
666 char *transport = NULL;
668 if (NULL != transport_name)
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
671 "Adding peer `%s' with plugin `%s' to blacklist\n",
672 GNUNET_i2s (peer), transport_name);
673 transport = GNUNET_strdup (transport_name);
676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
677 "Adding peer `%s' with all plugins to blacklist\n",
679 if (NULL == blacklist)
681 GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
684 GNUNET_CONTAINER_multipeermap_put (blacklist, peer,
686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
691 * Test if the given blacklist entry matches. If so,
692 * abort the iteration.
694 * @param cls the transport name to match (const char*)
695 * @param key the key (unused)
696 * @param value the 'char *' (name of a blacklisted transport)
697 * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
700 test_blacklisted (void *cls,
701 const struct GNUNET_PeerIdentity *key,
704 const char *transport_name = cls;
707 /* Blacklist entry be:
708 * (NULL == be): peer is blacklisted with all plugins
709 * (NULL != be): peer is blacklisted for a specific plugin
711 * If (NULL != transport_name) we look for a transport specific entry:
712 * if (transport_name == be) forbidden
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
719 (NULL == transport_name) ? "unspecified" : transport_name,
720 (NULL == be) ? "all plugins" : be);
721 /* all plugins for this peer were blacklisted: disallow */
725 /* blacklist check for specific transport */
726 if ((NULL != transport_name) && (NULL != value))
728 if (0 == strcmp (transport_name, be))
729 return GNUNET_NO; /* plugin is blacklisted! */
736 * Test if a peer/transport combination is blacklisted.
738 * @param peer the identity of the peer to test
739 * @param transport_name name of the transport to test, never NULL
740 * @param cont function to call with result
741 * @param cont_cls closure for @a cont
742 * @return handle to the blacklist check, NULL if the decision
743 * was made instantly and @a cont was already called
745 struct GST_BlacklistCheck *
746 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
747 const char *transport_name,
748 GST_BlacklistTestContinuation cont,
751 struct GST_BlacklistCheck *bc;
753 GNUNET_assert (NULL != peer);
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "Blacklist check for peer `%s':%s\n",
757 (NULL != transport_name) ? transport_name : "unspecified");
759 /* Check local blacklist by iterating over hashmap
760 * If iteration is aborted, we found a matching blacklist entry */
761 if ((NULL != blacklist) &&
763 GNUNET_CONTAINER_multipeermap_get_multiple (blacklist, peer,
765 (void *) transport_name)))
767 /* Disallowed by config, disapprove instantly */
768 GNUNET_STATISTICS_update (GST_stats,
769 gettext_noop ("# disconnects due to blacklist"),
771 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
772 _("Disallowing connection to peer `%s' on transport %s\n"),
774 (NULL != transport_name) ? transport_name : "unspecified");
776 cont (cont_cls, peer, GNUNET_NO);
782 /* no blacklist clients, approve instantly */
784 cont (cont_cls, peer, GNUNET_OK);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Allowing connection to peer `%s' %s\n",
788 (NULL != transport_name) ? transport_name : "");
792 /* need to query blacklist clients */
793 bc = GNUNET_new (struct GST_BlacklistCheck);
794 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
797 bc->cont_cls = cont_cls;
798 bc->bl_pos = bl_head;
799 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
805 * Cancel a blacklist check.
807 * @param bc check to cancel
810 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
812 GNUNET_CONTAINER_DLL_remove (bc_head,
815 if (NULL != bc->bl_pos)
817 if (bc->bl_pos->bc == bc)
819 /* we're at the head of the queue, remove us! */
820 bc->bl_pos->bc = NULL;
823 if (NULL != bc->task)
825 GNUNET_SCHEDULER_cancel (bc->task);
830 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
837 /* end of file gnunet-service-transport_blacklist.c */