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 neighbour's identity
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, const struct GNUNET_PeerIdentity *peer,
513 const struct GNUNET_HELLO_Address *address,
514 enum GNUNET_TRANSPORT_PeerState state,
515 struct GNUNET_TIME_Absolute state_timeout,
516 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
517 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
519 struct TestConnectionContext *tcc = cls;
520 struct GST_BlacklistCheck *bc;
522 bc = GNUNET_new (struct GST_BlacklistCheck);
523 GNUNET_CONTAINER_DLL_insert(bc_head, bc_tail, bc);
525 bc->cont = &confirm_or_drop_neighbour;
527 bc->bl_pos = tcc->bl;
528 if (GNUNET_YES == tcc->first)
530 /* all would wait for the same client, no need to
531 * create more than just the first task right now */
532 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
533 tcc->first = GNUNET_NO;
539 * Initialize a blacklisting client. We got a blacklist-init
540 * message from this client, add him to the list of clients
541 * to query for blacklisting.
544 * @param client the client
545 * @param message the blacklist-init message that was sent
548 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
549 const struct GNUNET_MessageHeader *message)
551 struct Blacklisters *bl;
552 struct TestConnectionContext tcc;
554 for (bl = bl_head; NULL != bl; bl = bl->next)
555 if (bl->client == client)
558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
562 GNUNET_SERVER_client_mark_monitor (client);
563 bl = GNUNET_new (struct Blacklisters);
565 bl->call_receive_done = GNUNET_YES;
566 GNUNET_SERVER_client_keep (client);
567 GNUNET_CONTAINER_DLL_insert_after (bl_head,
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "New blacklist client %p\n",
575 /* confirm that all existing connections are OK! */
577 tcc.first = GNUNET_YES;
578 GST_neighbours_iterate (&test_connection_ok, &tcc);
583 * A blacklisting client has sent us reply. Process it.
586 * @param client the client
587 * @param message the blacklist-init message that was sent
590 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
591 const struct GNUNET_MessageHeader *message)
593 const struct BlacklistMessage *msg =
594 (const struct BlacklistMessage *) message;
595 struct Blacklisters *bl;
596 struct GST_BlacklistCheck *bc;
599 while ((bl != NULL) && (bl->client != client))
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Blacklist client disconnected\n");
605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Blacklist client %p sent reply for `%s'\n",
612 GNUNET_i2s(&msg->peer));
616 bl->waiting_for_reply = GNUNET_NO;
617 bl->call_receive_done = GNUNET_YES; /* Remember to call receive_done */
620 /* only run this if the blacklist check has not been
621 * cancelled in the meantime... */
622 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Blacklist check failed, peer not allowed\n");
626 bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
627 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
628 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
629 bl->call_receive_done = GNUNET_NO;
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Blacklist check succeeded, continuing with checks\n");
637 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
638 bl->call_receive_done = GNUNET_NO;
639 bc->bl_pos = bc->bl_pos->next;
640 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
643 /* check if any other blacklist checks are waiting for this blacklister */
644 for (bc = bc_head; bc != NULL; bc = bc->next)
645 if ((bc->bl_pos == bl) && (NULL == bc->task))
647 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
654 * Add the given peer to the blacklist (for the given transport).
656 * @param peer peer to blacklist
657 * @param transport_name transport to blacklist for this peer, NULL for all
660 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
661 const char *transport_name)
663 char *transport = NULL;
665 if (NULL != transport_name)
667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
668 "Adding peer `%s' with plugin `%s' to blacklist\n",
669 GNUNET_i2s (peer), transport_name);
670 transport = GNUNET_strdup (transport_name);
673 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
674 "Adding peer `%s' with all plugins to blacklist\n",
676 if (NULL == blacklist)
678 GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
681 GNUNET_CONTAINER_multipeermap_put (blacklist, peer,
683 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
688 * Test if the given blacklist entry matches. If so,
689 * abort the iteration.
691 * @param cls the transport name to match (const char*)
692 * @param key the key (unused)
693 * @param value the 'char *' (name of a blacklisted transport)
694 * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
697 test_blacklisted (void *cls,
698 const struct GNUNET_PeerIdentity *key,
701 const char *transport_name = cls;
704 /* Blacklist entry be:
705 * (NULL == be): peer is blacklisted with all plugins
706 * (NULL != be): peer is blacklisted for a specific plugin
708 * If (NULL != transport_name) we look for a transport specific entry:
709 * if (transport_name == be) forbidden
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
716 (NULL == transport_name) ? "unspecified" : transport_name,
717 (NULL == be) ? "all plugins" : be);
718 /* all plugins for this peer were blacklisted: disallow */
722 /* blacklist check for specific transport */
723 if ((NULL != transport_name) && (NULL != value))
725 if (0 == strcmp (transport_name, be))
726 return GNUNET_NO; /* plugin is blacklisted! */
733 * Test if a peer/transport combination is blacklisted.
735 * @param peer the identity of the peer to test
736 * @param transport_name name of the transport to test, never NULL
737 * @param cont function to call with result
738 * @param cont_cls closure for @a cont
739 * @return handle to the blacklist check, NULL if the decision
740 * was made instantly and @a cont was already called
742 struct GST_BlacklistCheck *
743 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
744 const char *transport_name,
745 GST_BlacklistTestContinuation cont,
748 struct GST_BlacklistCheck *bc;
750 GNUNET_assert (NULL != peer);
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Blacklist check for peer `%s':%s\n",
754 (NULL != transport_name) ? transport_name : "unspecified");
756 /* Check local blacklist by iterating over hashmap
757 * If iteration is aborted, we found a matching blacklist entry */
758 if ((NULL != blacklist) &&
760 GNUNET_CONTAINER_multipeermap_get_multiple (blacklist, peer,
762 (void *) transport_name)))
764 /* Disallowed by config, disapprove instantly */
765 GNUNET_STATISTICS_update (GST_stats,
766 gettext_noop ("# disconnects due to blacklist"),
768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
769 _("Disallowing connection to peer `%s' on transport %s\n"),
771 (NULL != transport_name) ? transport_name : "unspecified");
773 cont (cont_cls, peer, GNUNET_NO);
779 /* no blacklist clients, approve instantly */
781 cont (cont_cls, peer, GNUNET_OK);
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Allowing connection to peer `%s' %s\n",
785 (NULL != transport_name) ? transport_name : "");
789 /* need to query blacklist clients */
790 bc = GNUNET_new (struct GST_BlacklistCheck);
791 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
794 bc->cont_cls = cont_cls;
795 bc->bl_pos = bl_head;
796 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
802 * Cancel a blacklist check.
804 * @param bc check to cancel
807 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
809 GNUNET_CONTAINER_DLL_remove (bc_head,
812 if (NULL != bc->bl_pos)
814 if (bc->bl_pos->bc == bc)
816 /* we're at the head of the queue, remove us! */
817 bc->bl_pos->bc = NULL;
820 if (NULL != bc->task)
822 GNUNET_SCHEDULER_cancel (bc->task);
827 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
834 /* end of file gnunet-service-transport_blacklist.c */