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, Matthias Wachs
25 * @details This is the blacklisting component of transport service. With
26 * blacklisting it is possible to deny connections to specific peers of
27 * to use a specific plugin to a specific peer. Peers can be blacklisted using
28 * the configuration or a blacklist client can be asked.
30 * To blacklist peers using the configuration you have to add a section to your
31 * configuration containing the peer id of the peer to blacklist and the plugin
35 * To blacklist connections to P565... on peer AG2P... using tcp add:
36 * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
37 * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G = tcp
39 * To blacklist connections to P565... on peer AG2P... using all plugins add:
40 * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
41 * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G =
43 * You can also add a blacklist client usign the blacklist api. On a blacklist
44 * check, blacklisting first checks internally if the peer is blacklisted and
45 * if not, it asks the blacklisting clients. Clients are asked if it is OK to
46 * connect to a peer ID, the plugin is omitted.
48 * On blacklist check for (peer, plugin)
49 * - Do we have a local blacklist entry for this peer and this plugin?
50 * - YES: disallow connection
51 * - Do we have a local blacklist entry for this peer and all plugins?
52 * - YES: disallow connection
53 * - Does one of the clients disallow?
54 * - YES: disallow connection
58 #include "gnunet-service-transport.h"
59 #include "gnunet-service-transport_blacklist.h"
60 #include "gnunet-service-transport_neighbours.h"
61 #include "transport.h"
64 * Size of the blacklist hash map.
66 #define TRANSPORT_BLACKLIST_HT_SIZE 64
70 * Context we use when performing a blacklist check.
72 struct GST_BlacklistCheck;
76 * Information kept for each client registered to perform
82 * This is a linked list.
84 struct Blacklisters *next;
87 * This is a linked list.
89 struct Blacklisters *prev;
92 * Client responsible for this entry.
94 struct GNUNET_SERVER_Client *client;
97 * Blacklist check that we're currently performing (or NULL
98 * if we're performing one that has been cancelled).
100 struct GST_BlacklistCheck *bc;
103 * Set to GNUNET_YES if we're currently waiting for a reply.
105 int waiting_for_reply;
112 * Context we use when performing a blacklist check.
114 struct GST_BlacklistCheck
118 * This is a linked list.
120 struct GST_BlacklistCheck *next;
123 * This is a linked list.
125 struct GST_BlacklistCheck *prev;
128 * Peer being checked.
130 struct GNUNET_PeerIdentity peer;
133 * Continuation to call with the result.
135 GST_BlacklistTestContinuation cont;
143 * Current transmission request handle for this client, or NULL if no
144 * request is pending.
146 struct GNUNET_SERVER_TransmitHandle *th;
149 * Our current position in the blacklisters list.
151 struct Blacklisters *bl_pos;
154 * Current task performing the check.
156 GNUNET_SCHEDULER_TaskIdentifier task;
162 * Head of DLL of active blacklisting queries.
164 static struct GST_BlacklistCheck *bc_head;
167 * Tail of DLL of active blacklisting queries.
169 static struct GST_BlacklistCheck *bc_tail;
172 * Head of DLL of blacklisting clients.
174 static struct Blacklisters *bl_head;
177 * Tail of DLL of blacklisting clients.
179 static struct Blacklisters *bl_tail;
182 * Hashmap of blacklisted peers. Values are of type 'char *' (transport names),
183 * can be NULL if we have no static blacklist.
185 static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
189 * Perform next action in the blacklist check.
191 * @param cls the 'struct BlacklistCheck*'
195 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
199 * Called whenever a client is disconnected. Frees our
200 * resources associated with that client.
202 * @param cls closure (unused)
203 * @param client identification of the client
206 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
208 struct Blacklisters *bl;
209 struct GST_BlacklistCheck *bc;
213 for (bl = bl_head; bl != NULL; bl = bl->next)
215 if (bl->client != client)
217 for (bc = bc_head; bc != NULL; bc = bc->next)
219 if (bc->bl_pos != bl)
221 bc->bl_pos = bl->next;
224 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
227 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
228 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
231 GNUNET_CONTAINER_DLL_remove (bl_head, bl_tail, bl);
232 GNUNET_SERVER_client_drop (bl->client);
240 * Function to iterate over options in the blacklisting section for a peer.
243 * @param section name of the section
244 * @param option name of the option
245 * @param value value of the option
248 blacklist_cfg_iter (void *cls, const char *section,
252 unsigned int *res = cls;
253 struct GNUNET_PeerIdentity peer;
257 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (option,
262 if ((NULL == value) || (0 == strcmp(value, "")))
264 /* Blacklist whole peer */
265 GST_blacklist_add_peer (&peer, NULL);
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
267 _("Adding blacklisting entry for peer `%s'\n"), GNUNET_i2s (&peer));
271 plugs = GNUNET_strdup (value);
272 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
275 _("Adding blacklisting entry for peer `%s':`%s'\n"),
276 GNUNET_i2s (&peer), pos);
277 GST_blacklist_add_peer (&peer, pos);
286 * Read blacklist configuration
288 * @param cfg the configuration handle
289 * @param my_id my peer identity
292 read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
293 const struct GNUNET_PeerIdentity *my_id)
296 unsigned int res = 0;
298 GNUNET_snprintf (cfg_sect,
300 "transport-blacklist-%s",
301 GNUNET_i2s_full (my_id));
302 GNUNET_CONFIGURATION_iterate_section_values (cfg, cfg_sect, &blacklist_cfg_iter, &res);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Loaded %u blacklisting entries from configuration\n", res);
309 * Start blacklist subsystem.
311 * @param server server used to accept clients from
312 * @param cfg configuration handle
313 * @param my_id my peer id
316 GST_blacklist_start (struct GNUNET_SERVER_Handle *server,
317 const struct GNUNET_CONFIGURATION_Handle *cfg,
318 const struct GNUNET_PeerIdentity *my_id)
320 GNUNET_assert (NULL != cfg);
321 GNUNET_assert (NULL != my_id);
322 read_blacklist_configuration (cfg, my_id);
323 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
329 * Free the given entry in the blacklist.
332 * @param key host identity (unused)
333 * @param value the blacklist entry
334 * @return GNUNET_OK (continue to iterate)
337 free_blacklist_entry (void *cls,
338 const struct GNUNET_PeerIdentity *key,
343 GNUNET_free_non_null (be);
349 * Stop blacklist subsystem.
352 GST_blacklist_stop ()
354 if (NULL != blacklist)
356 GNUNET_CONTAINER_multipeermap_iterate (blacklist, &free_blacklist_entry,
358 GNUNET_CONTAINER_multipeermap_destroy (blacklist);
365 * Transmit blacklist query to the client.
367 * @param cls the 'struct GST_BlacklistCheck'
368 * @param size number of bytes allowed
369 * @param buf where to copy the message
370 * @return number of bytes copied to buf
373 transmit_blacklist_message (void *cls, size_t size, void *buf)
375 struct GST_BlacklistCheck *bc = cls;
376 struct Blacklisters *bl;
377 struct BlacklistMessage bm;
382 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
383 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 "Failed to send blacklist test for peer `%s' to client\n",
386 GNUNET_i2s (&bc->peer));
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Sending blacklist test for peer `%s' to client\n",
391 GNUNET_i2s (&bc->peer));
393 bm.header.size = htons (sizeof (struct BlacklistMessage));
394 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
395 bm.is_allowed = htonl (0);
397 memcpy (buf, &bm, sizeof (bm));
398 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
399 bl->waiting_for_reply = GNUNET_YES;
405 * Perform next action in the blacklist check.
407 * @param cls the 'struct GST_BlacklistCheck*'
411 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
413 struct GST_BlacklistCheck *bc = cls;
414 struct Blacklisters *bl;
416 bc->task = GNUNET_SCHEDULER_NO_TASK;
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "No other blacklist clients active, will allow neighbour `%s'\n",
422 GNUNET_i2s (&bc->peer));
423 bc->cont (bc->cont_cls, &bc->peer, GNUNET_OK);
424 GNUNET_CONTAINER_DLL_remove(bc_head, bc_tail, bc);
428 if ((bl->bc != NULL) || (bl->waiting_for_reply != GNUNET_NO))
429 return; /* someone else busy with this client */
432 GNUNET_SERVER_notify_transmit_ready (bl->client,
433 sizeof (struct BlacklistMessage),
434 GNUNET_TIME_UNIT_FOREVER_REL,
435 &transmit_blacklist_message, bc);
440 * Got the result about an existing connection from a new blacklister.
441 * Shutdown the neighbour if necessary.
444 * @param peer the neighbour that was investigated
445 * @param allowed GNUNET_OK if we can keep it,
446 * GNUNET_NO if we must shutdown the connection
449 confirm_or_drop_neighbour (void *cls, const struct GNUNET_PeerIdentity *peer,
452 if (GNUNET_OK == allowed)
453 return; /* we're done */
454 GNUNET_STATISTICS_update (GST_stats,
455 gettext_noop ("# disconnects due to blacklist"), 1,
457 GST_neighbours_force_disconnect (peer);
462 * Closure for 'test_connection_ok'.
464 struct TestConnectionContext
467 * Is this the first neighbour we're checking?
472 * Handle to the blacklisting client we need to ask.
474 struct Blacklisters *bl;
478 * Test if an existing connection is still acceptable given a new
479 * blacklisting client.
481 * @param cls the 'struct TestConnectionContest'
482 * @param peer neighbour's identity
483 * @param address the address
484 * @param state current state this peer is in
485 * @param state_timeout timeout for the current state of the peer
486 * @param bandwidth_in bandwidth assigned inbound
487 * @param bandwidth_out bandwidth assigned outbound
490 test_connection_ok (void *cls, const struct GNUNET_PeerIdentity *peer,
491 const struct GNUNET_HELLO_Address *address,
492 enum GNUNET_TRANSPORT_PeerState state,
493 struct GNUNET_TIME_Absolute state_timeout,
494 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
495 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
497 struct TestConnectionContext *tcc = cls;
498 struct GST_BlacklistCheck *bc;
500 bc = GNUNET_new (struct GST_BlacklistCheck);
501 GNUNET_CONTAINER_DLL_insert(bc_head, bc_tail, bc);
503 bc->cont = &confirm_or_drop_neighbour;
505 bc->bl_pos = tcc->bl;
506 if (GNUNET_YES == tcc->first)
508 /* all would wait for the same client, no need to
509 * create more than just the first task right now */
510 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
511 tcc->first = GNUNET_NO;
517 * Initialize a blacklisting client. We got a blacklist-init
518 * message from this client, add him to the list of clients
519 * to query for blacklisting.
522 * @param client the client
523 * @param message the blacklist-init message that was sent
526 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
527 const struct GNUNET_MessageHeader *message)
529 struct Blacklisters *bl;
530 struct TestConnectionContext tcc;
535 if (bl->client == client)
538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 GNUNET_SERVER_client_mark_monitor (client);
544 bl = GNUNET_new (struct Blacklisters);
546 GNUNET_SERVER_client_keep (client);
547 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
549 /* confirm that all existing connections are OK! */
551 tcc.first = GNUNET_YES;
552 GST_neighbours_iterate (&test_connection_ok, &tcc);
557 * A blacklisting client has sent us reply. Process it.
560 * @param client the client
561 * @param message the blacklist-init message that was sent
564 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
565 const struct GNUNET_MessageHeader *message)
567 const struct BlacklistMessage *msg =
568 (const struct BlacklistMessage *) message;
569 struct Blacklisters *bl;
570 struct GST_BlacklistCheck *bc;
573 while ((bl != NULL) && (bl->client != client))
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist client disconnected\n");
578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
583 bl->waiting_for_reply = GNUNET_NO;
586 /* only run this if the blacklist check has not been
587 * cancelled in the meantime... */
588 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Blacklist check failed, peer not allowed\n");
592 bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
593 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
594 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Blacklist check succeeded, continuing with checks\n");
602 bc->bl_pos = bc->bl_pos->next;
603 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
606 /* check if any other bc's are waiting for this blacklister */
607 for (bc = bc_head; bc != NULL; bc = bc->next)
608 if ((bc->bl_pos == bl) && (GNUNET_SCHEDULER_NO_TASK == bc->task))
610 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
617 * Add the given peer to the blacklist (for the given transport).
619 * @param peer peer to blacklist
620 * @param transport_name transport to blacklist for this peer, NULL for all
623 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
624 const char *transport_name)
626 char * transport = NULL;
628 if (NULL != transport_name)
630 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
631 "Adding peer `%s' with plugin `%s' to blacklist\n",
632 GNUNET_i2s (peer), transport_name);
633 transport = GNUNET_strdup (transport_name);
636 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
637 "Adding peer `%s' with all plugins to blacklist\n",
639 if (blacklist == NULL)
641 GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
644 GNUNET_CONTAINER_multipeermap_put (blacklist, peer,
646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
651 * Test if the given blacklist entry matches. If so,
652 * abort the iteration.
654 * @param cls the transport name to match (const char*)
655 * @param key the key (unused)
656 * @param value the 'char *' (name of a blacklisted transport)
657 * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
660 test_blacklisted (void *cls,
661 const struct GNUNET_PeerIdentity *key,
664 const char *transport_name = cls;
667 /* Blacklist entry be:
668 * (NULL == be): peer is blacklisted with all plugins
669 * (NULL != be): peer is blacklisted for a specific plugin
671 * If (NULL != transport_name) we look for a transport specific entry:
672 * if (transport_name == be) forbidden
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
679 (NULL == transport_name) ? "unspecified" : transport_name,
680 (NULL == be) ? "all plugins" : be);
681 /* all plugins for this peer were blacklisted: disallow */
685 /* blacklist check for specific transport */
686 if ((NULL != transport_name) && (NULL != value))
688 if (0 == strcmp (transport_name, be))
689 return GNUNET_NO; /* plugin is blacklisted! */
696 * Test if a peer/transport combination is blacklisted.
698 * @param peer the identity of the peer to test
699 * @param transport_name name of the transport to test, never NULL
700 * @param cont function to call with result
701 * @param cont_cls closure for 'cont'
702 * @return handle to the blacklist check, NULL if the decision
703 * was made instantly and 'cont' was already called
705 struct GST_BlacklistCheck *
706 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
707 const char *transport_name,
708 GST_BlacklistTestContinuation cont, void *cont_cls)
710 struct GST_BlacklistCheck *bc;
712 GNUNET_assert (peer != NULL);
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist check for peer `%s':%s\n",
714 GNUNET_i2s (peer), (NULL != transport_name) ? transport_name : "unspecified");
716 /* Check local blacklist by iterating over hashmap
717 * If iteration is aborted, we found a matching blacklist entry */
718 if ((blacklist != NULL) &&
720 GNUNET_CONTAINER_multipeermap_get_multiple (blacklist, peer,
722 (void *) transport_name)))
724 /* Disallowed by config, disapprove instantly */
725 GNUNET_STATISTICS_update (GST_stats,
726 gettext_noop ("# disconnects due to blacklist"),
728 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disallowing connection to peer `%s' on transport %s\n",
729 GNUNET_i2s (peer), (NULL != transport_name) ? transport_name : "unspecified");
731 cont (cont_cls, peer, GNUNET_NO);
737 /* no blacklist clients, approve instantly */
739 cont (cont_cls, peer, GNUNET_OK);
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Allowing connection to peer `%s' %s\n",
741 GNUNET_i2s (peer), (NULL != transport_name) ? transport_name : "");
745 /* need to query blacklist clients */
746 bc = GNUNET_new (struct GST_BlacklistCheck);
747 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
750 bc->cont_cls = cont_cls;
751 bc->bl_pos = bl_head;
752 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
758 * Cancel a blacklist check.
760 * @param bc check to cancel
763 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
765 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
766 if (bc->bl_pos != NULL)
768 if (bc->bl_pos->bc == bc)
770 /* we're at the head of the queue, remove us! */
771 bc->bl_pos->bc = NULL;
774 if (GNUNET_SCHEDULER_NO_TASK != bc->task)
776 GNUNET_SCHEDULER_cancel (bc->task);
777 bc->task = GNUNET_SCHEDULER_NO_TASK;
781 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
788 /* end of file gnunet-service-transport_blacklist.c */