complete new KX implementation, including KX_AUTH defense against KX-injection based...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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  * @file cadet/gnunet-service-cadet_tunnel.h
23  * @brief cadet service; dealing with tunnels and crypto
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMT (Gnunet Cadet Tunnel)
27  */
28
29 #ifndef GNUNET_SERVICE_CADET_TUNNEL_H
30 #define GNUNET_SERVICE_CADET_TUNNEL_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "platform.h"
41 #include "gnunet_util_lib.h"
42
43 #define CONNECTIONS_PER_TUNNEL 3
44
45 /**
46  * All the connectivity states a tunnel can be in.
47  */
48 enum CadetTunnelCState
49 {
50   /**
51    * Uninitialized status, should never appear in operation.
52    */
53   CADET_TUNNEL_NEW,
54
55   /**
56    * No path to the peer known yet.
57    */
58   CADET_TUNNEL_SEARCHING,
59
60   /**
61    * Request sent, not yet answered.
62    */
63   CADET_TUNNEL_WAITING,
64
65   /**
66    * Peer connected and ready to accept data.
67    */
68   CADET_TUNNEL_READY,
69
70   /**
71    * Tunnel being shut down, don't try to keep it alive.
72    */
73   CADET_TUNNEL_SHUTDOWN
74 };
75
76
77 /**
78  * All the encryption states a tunnel can be in.
79  */
80 enum CadetTunnelEState
81 {
82   /**
83    * Uninitialized status, should never appear in operation.
84    */
85   CADET_TUNNEL_KEY_UNINITIALIZED,
86
87   /**
88    * Ephemeral key sent, waiting for peer's key.
89    */
90   CADET_TUNNEL_KEY_AX_SENT,
91
92   /**
93    * In OTR: New ephemeral key and ping sent, waiting for pong.
94    *
95    * This means that we DO have the peer's ephemeral key, otherwise the
96    * state would be KEY_SENT. We DO NOT have a valid session key (either no
97    * previous key or previous key expired).
98    *
99    *
100    * In Axolotl: Key sent and received but no deciphered traffic yet.
101    *
102    * This means that we can send traffic (otherwise we would never complete
103    * the handshake), but we don't have complete confirmation. Since the first
104    * traffic MUST be a complete channel creation 3-way handshake, no payload
105    * will be sent before confirmation.
106    */
107   CADET_TUNNEL_KEY_AX_AUTH_SENT,
108
109   /**
110    * Handshake completed: session key available.
111    */
112   CADET_TUNNEL_KEY_OK,
113
114   /**
115    * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
116    * we still have a valid session key and therefore we *can* still send
117    * traffic on the tunnel.
118    */
119   CADET_TUNNEL_KEY_REKEY
120 };
121
122 /**
123  * Struct containing all information regarding a given peer
124  */
125 struct CadetTunnel;
126
127
128 #include "gnunet-service-cadet_channel.h"
129 #include "gnunet-service-cadet_connection.h"
130 #include "gnunet-service-cadet_peer.h"
131
132 /**
133  * Handle for messages queued but not yet sent.
134  */
135 struct CadetTunnelQueue;
136
137 /**
138  * Callback called when a queued message is sent.
139  *
140  * @param cls Closure.
141  * @param t Tunnel this message was on.
142  * @param type Type of message sent.
143  * @param size Size of the message.
144  */
145 typedef void
146 (*GCT_sent) (void *cls,
147              struct CadetTunnel *t,
148              struct CadetTunnelQueue *q,
149              uint16_t type, size_t size);
150
151 typedef void
152 (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
153
154
155 typedef void
156 (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
157
158
159 /******************************************************************************/
160 /********************************    API    ***********************************/
161 /******************************************************************************/
162
163 /**
164  * Initialize tunnel subsystem.
165  *
166  * @param c Configuration handle.
167  * @param key ECC private key, to derive all other keys and do crypto.
168  */
169 void
170 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
171           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
172
173
174 /**
175  * Shut down the tunnel subsystem.
176  */
177 void
178 GCT_shutdown (void);
179
180
181 /**
182  * Create a tunnel.
183  *
184  * @param destination Peer this tunnel is towards.
185  */
186 struct CadetTunnel *
187 GCT_new (struct CadetPeer *destination);
188
189
190 /**
191  * Tunnel is empty: destroy it.
192  *
193  * Notifies all connections about the destruction.
194  *
195  * @param t Tunnel to destroy.
196  */
197 void
198 GCT_destroy_empty (struct CadetTunnel *t);
199
200
201 /**
202  * Destroy tunnel if empty (no more channels).
203  *
204  * @param t Tunnel to destroy if empty.
205  */
206 void
207 GCT_destroy_if_empty (struct CadetTunnel *t);
208
209
210 /**
211  * Destroy the tunnel.
212  *
213  * This function does not generate any warning traffic to clients or peers.
214  *
215  * Tasks:
216  * Cancel messages belonging to this tunnel queued to neighbors.
217  * Free any allocated resources linked to the tunnel.
218  *
219  * @param t The tunnel to destroy.
220  */
221 void
222 GCT_destroy (struct CadetTunnel *t);
223
224
225 /**
226  * Change the tunnel's connection state.
227  *
228  * @param t Tunnel whose connection state to change.
229  * @param cstate New connection state.
230  */
231 void
232 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
233
234
235 /**
236  * Change the tunnel encryption state.
237  *
238  * @param t Tunnel whose encryption state to change.
239  * @param state New encryption state.
240  */
241 void
242 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
243
244
245 /**
246  * Add a connection to a tunnel.
247  *
248  * @param t Tunnel.
249  * @param c Connection.
250  */
251 void
252 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
253
254
255 /**
256  * Remove a connection from a tunnel.
257  *
258  * @param t Tunnel.
259  * @param c Connection.
260  */
261 void
262 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
263
264
265 /**
266  * Add a channel to a tunnel.
267  *
268  * @param t Tunnel.
269  * @param ch Channel.
270  */
271 void
272 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
273
274
275 /**
276  * Remove a channel from a tunnel.
277  *
278  * @param t Tunnel.
279  * @param ch Channel.
280  */
281 void
282 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
283
284
285 /**
286  * Search for a channel by global ID.
287  *
288  * @param t Tunnel containing the channel.
289  * @param ctn Public channel number.
290  *
291  * @return channel handler, NULL if doesn't exist
292  */
293 struct CadetChannel *
294 GCT_get_channel (struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber ctn);
295
296
297 /**
298  * Decrypt and process an encrypted message.
299  *
300  * Calls the appropriate handler for a message in a channel of a local tunnel.
301  *
302  * @param t Tunnel this message came on.
303  * @param msg Message header.
304  */
305 void
306 GCT_handle_encrypted (struct CadetTunnel *t,
307                       const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
308
309
310 /**
311  * Handle a Key eXchange message.
312  *
313  * @param t Tunnel on which the message came.
314  * @param msg KX message itself.
315  */
316 void
317 GCT_handle_kx (struct CadetTunnel *t,
318                const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
319
320
321 /**
322  * @brief Use the given path for the tunnel.
323  * Update the next and prev hops (and RCs).
324  * (Re)start the path refresh in case the tunnel is locally owned.
325  *
326  * @param t Tunnel to update.
327  * @param p Path to use.
328  *
329  * @return Connection created.
330  */
331 struct CadetConnection *
332 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
333
334
335 /**
336  * Count all created connections of a tunnel. Not necessarily ready connections!
337  *
338  * @param t Tunnel on which to count.
339  *
340  * @return Number of connections created, either being established or ready.
341  */
342 unsigned int
343 GCT_count_any_connections (struct CadetTunnel *t);
344
345
346 /**
347  * Count established (ready) connections of a tunnel.
348  *
349  * @param t Tunnel on which to count.
350  *
351  * @return Number of connections.
352  */
353 unsigned int
354 GCT_count_connections (struct CadetTunnel *t);
355
356
357 /**
358  * Count channels of a tunnel.
359  *
360  * @param t Tunnel on which to count.
361  *
362  * @return Number of channels.
363  */
364 unsigned int
365 GCT_count_channels (struct CadetTunnel *t);
366
367
368 /**
369  * Get the connectivity state of a tunnel.
370  *
371  * @param t Tunnel.
372  *
373  * @return Tunnel's connectivity state.
374  */
375 enum CadetTunnelCState
376 GCT_get_cstate (struct CadetTunnel *t);
377
378
379 /**
380  * Get the encryption state of a tunnel.
381  *
382  * @param t Tunnel.
383  *
384  * @return Tunnel's encryption state.
385  */
386 enum CadetTunnelEState
387 GCT_get_estate (struct CadetTunnel *t);
388
389
390 /**
391  * Get the maximum buffer space for a tunnel towards a local client.
392  *
393  * @param t Tunnel.
394  *
395  * @return Biggest buffer space offered by any channel in the tunnel.
396  */
397 unsigned int
398 GCT_get_channels_buffer (struct CadetTunnel *t);
399
400
401 /**
402  * Get the total buffer space for a tunnel for P2P traffic.
403  *
404  * @param t Tunnel.
405  *
406  * @return Buffer space offered by all connections in the tunnel.
407  */
408 unsigned int
409 GCT_get_connections_buffer (struct CadetTunnel *t);
410
411
412 /**
413  * Get the tunnel's destination.
414  *
415  * @param t Tunnel.
416  *
417  * @return ID of the destination peer.
418  */
419 const struct GNUNET_PeerIdentity *
420 GCT_get_destination (struct CadetTunnel *t);
421
422
423 /**
424  * Get the tunnel's next free Channel ID.
425  *
426  * @param t Tunnel.
427  *
428  * @return ID of a channel free to use.
429  */
430 struct GNUNET_CADET_ChannelTunnelNumber
431 GCT_get_next_ctn (struct CadetTunnel *t);
432
433
434 /**
435  * Send ACK on one or more channels due to buffer in connections.
436  *
437  * @param t Channel which has some free buffer space.
438  */
439 void
440 GCT_unchoke_channels (struct CadetTunnel *t);
441
442
443 /**
444  * Send ACK on one or more connections due to buffer space to the client.
445  *
446  * Iterates all connections of the tunnel and sends ACKs appropriately.
447  *
448  * @param t Tunnel which has some free buffer space.
449  */
450 void
451 GCT_send_connection_acks (struct CadetTunnel *t);
452
453
454 /**
455  * Cancel a previously sent message while it's in the queue.
456  *
457  * ONLY can be called before the continuation given to the send function
458  * is called. Once the continuation is called, the message is no longer in the
459  * queue.
460  *
461  * @param q Handle to the queue.
462  */
463 void
464 GCT_cancel (struct CadetTunnelQueue *q);
465
466
467 /**
468  * Check if the tunnel has queued traffic.
469  *
470  * @param t Tunnel to check.
471  *
472  * @return #GNUNET_YES if there is queued traffic
473  *         #GNUNET_NO otherwise
474  */
475 int
476 GCT_has_queued_traffic (struct CadetTunnel *t);
477
478 /**
479  * Sends an already built message on a tunnel, encrypting it and
480  * choosing the best connection.
481  *
482  * @param message Message to send. Function modifies it.
483  * @param t Tunnel on which this message is transmitted.
484  * @param c Connection to use (autoselect if NULL).
485  * @param force Force the tunnel to take the message (buffer overfill).
486  * @param cont Continuation to call once message is really sent.
487  * @param cont_cls Closure for @c cont.
488  *
489  * @return Handle to cancel message. NULL if @c cont is NULL.
490  */
491 struct CadetTunnelQueue *
492 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
493                            struct CadetTunnel *t, struct CadetConnection *c,
494                            int force, GCT_sent cont, void *cont_cls);
495
496
497 /**
498  * Send a KX message.
499  *
500  * @param t Tunnel on which to send it.
501  * @param force_reply Force the other peer to reply with a KX message.
502  */
503 void
504 GCT_send_kx (struct CadetTunnel *t, int force_reply);
505
506
507 /**
508  * Is the tunnel directed towards the local peer?
509  *
510  * @param t Tunnel.
511  *
512  * @return #GNUNET_YES if it is loopback.
513  */
514 int
515 GCT_is_loopback (const struct CadetTunnel *t);
516
517
518 /**
519  * Is the tunnel using this path already?
520  *
521  * @param t Tunnel.
522  * @param p Path.
523  *
524  * @return #GNUNET_YES a connection uses this path.
525  */
526 int
527 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
528
529
530 /**
531  * Get a cost of a path for a tunnel considering existing connections.
532  *
533  * @param t Tunnel.
534  * @param path Candidate path.
535  *
536  * @return Cost of the path (path length + number of overlapping nodes)
537  */
538 unsigned int
539 GCT_get_path_cost (const struct CadetTunnel *t,
540                    const struct CadetPeerPath *path);
541
542
543 /**
544  * Get the static string for the peer this tunnel is directed.
545  *
546  * @param t Tunnel.
547  *
548  * @return Static string the destination peer's ID.
549  */
550 const char *
551 GCT_2s (const struct CadetTunnel *t);
552
553
554 /**
555  * Log all possible info about the tunnel state.
556  *
557  * @param t Tunnel to debug.
558  * @param level Debug level to use.
559  */
560 void
561 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
562
563
564 /**
565  * Iterate all tunnels.
566  *
567  * @param iter Iterator.
568  * @param cls Closure for @c iter.
569  */
570 void
571 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
572
573
574 /**
575  * Count all tunnels.
576  *
577  * @return Number of tunnels to remote peers kept by this peer.
578  */
579 unsigned int
580 GCT_count_all (void);
581
582
583 /**
584  * Iterate all connections of a tunnel.
585  *
586  * @param t Tunnel whose connections to iterate.
587  * @param iter Iterator.
588  * @param cls Closure for @c iter.
589  */
590 void
591 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
592
593
594 /**
595  * Iterate all channels of a tunnel.
596  *
597  * @param t Tunnel whose channels to iterate.
598  * @param iter Iterator.
599  * @param cls Closure for @c iter.
600  */
601 void
602 GCT_iterate_channels (struct CadetTunnel *t,
603                       GCT_chan_iter iter,
604                       void *cls);
605
606
607 #if 0                           /* keep Emacsens' auto-indent happy */
608 {
609 #endif
610 #ifdef __cplusplus
611 }
612 #endif
613
614 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
615 #endif
616 /* end of gnunet-cadet-service_tunnel.h */