Put #ifndef checks for HAVE_RAND_PSEUDO_BYTES in the correct places.
[tinc] / src / net_packet.c
1 /*
2     net_packet.c -- Handles in- and outgoing VPN packets
3     Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: net_packet.c,v 1.1.2.14 2002/04/18 20:09:05 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #ifdef HAVE_LINUX
30  #include <netinet/ip.h>
31  #include <netinet/tcp.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43    and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47
48 #include <openssl/rand.h>
49 #include <openssl/evp.h>
50 #include <openssl/pem.h>
51 #include <openssl/hmac.h>
52
53 #include <zlib.h>
54
55 #include <utils.h>
56 #include <xalloc.h>
57 #include <avl_tree.h>
58 #include <list.h>
59
60 #include "conf.h"
61 #include "connection.h"
62 #include "meta.h"
63 #include "net.h"
64 #include "netutl.h"
65 #include "process.h"
66 #include "protocol.h"
67 #include "subnet.h"
68 #include "graph.h"
69 #include "process.h"
70 #include "route.h"
71 #include "device.h"
72 #include "event.h"
73
74 #include "system.h"
75
76 int keylifetime = 0;
77 int keyexpires = 0;
78
79 #define MAX_SEQNO 1073741824
80
81 /* VPN packet I/O */
82
83 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
84 {
85   vpn_packet_t pkt1, pkt2;
86   vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
87   int nextpkt = 0;
88   vpn_packet_t *outpkt = pkt[0];
89   int outlen, outpad;
90   long int complen = MTU + 12;
91   EVP_CIPHER_CTX ctx;
92   char hmac[EVP_MAX_MD_SIZE];
93 cp
94   /* Check the message authentication code */
95
96   if(myself->digest && myself->maclength)
97     {
98       inpkt->len -= myself->maclength;
99       HMAC(myself->digest, myself->key, myself->keylength, (char *)&inpkt->seqno, inpkt->len, hmac, NULL);
100       if(memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength))
101         {
102           if(debug_lvl >= DEBUG_TRAFFIC)
103             syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
104           return;
105         }
106     }
107
108   /* Decrypt the packet */
109
110   if(myself->cipher)
111   {
112     outpkt = pkt[nextpkt++];
113
114     EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
115     EVP_DecryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
116     EVP_DecryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
117
118     outpkt->len = outlen + outpad;
119     inpkt = outpkt;
120   }
121
122   /* Check the sequence number */
123
124   inpkt->len -= sizeof(inpkt->seqno);
125   inpkt->seqno = ntohl(inpkt->seqno);
126
127   if(inpkt->seqno <= n->received_seqno)
128   {
129     if(debug_lvl >= DEBUG_TRAFFIC)
130       syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
131     return;
132   }
133   
134   n->received_seqno = inpkt->seqno;
135
136   if(n->received_seqno > MAX_SEQNO)
137     keyexpires = 0;
138
139   /* Decompress the packet */
140   
141   if(myself->compression)
142   {
143     outpkt = pkt[nextpkt++];
144
145     if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK)
146     {
147       syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"), n->name, n->hostname);
148       return;
149     }
150     
151     outpkt->len = complen;
152     inpkt = outpkt;
153   }
154
155   receive_packet(n, inpkt);
156 cp
157 }
158
159 void receive_tcppacket(connection_t *c, char *buffer, int len)
160 {
161   vpn_packet_t outpkt;
162 cp
163   outpkt.len = len;
164   memcpy(outpkt.data, buffer, len);
165
166   receive_packet(c->node, &outpkt);
167 cp
168 }
169
170 void receive_packet(node_t *n, vpn_packet_t *packet)
171 {
172 cp
173   if(debug_lvl >= DEBUG_TRAFFIC)
174     syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
175
176   route_incoming(n, packet);
177 cp
178 }
179
180 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
181 {
182   vpn_packet_t pkt1, pkt2;
183   vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
184   int nextpkt = 0;
185   vpn_packet_t *outpkt;
186   int origlen;
187   int outlen, outpad;
188   long int complen = MTU + 12;
189   EVP_CIPHER_CTX ctx;
190   vpn_packet_t *copy;
191   static int priority = 0;
192   int origpriority;
193   int sock;
194 cp
195   /* Make sure we have a valid key */
196
197   if(!n->status.validkey)
198     {
199       if(debug_lvl >= DEBUG_TRAFFIC)
200         syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
201                n->name, n->hostname);
202
203       /* Since packet is on the stack of handle_tap_input(),
204          we have to make a copy of it first. */
205
206       copy = xmalloc(sizeof(vpn_packet_t));
207       memcpy(copy, inpkt, sizeof(vpn_packet_t));
208
209       list_insert_tail(n->queue, copy);
210
211       if(n->queue->count > MAXQUEUELENGTH)
212         list_delete_head(n->queue);
213
214       if(!n->status.waitingforkey)
215         send_req_key(n->nexthop->connection, myself, n);
216
217       n->status.waitingforkey = 1;
218
219       return;
220     }
221
222   origlen = inpkt->len;
223   origpriority = inpkt->priority;
224
225   /* Compress the packet */
226
227   if(n->compression)
228   {
229     outpkt = pkt[nextpkt++];
230
231     if(compress2(outpkt->data, &complen, inpkt->data, inpkt->len, n->compression) != Z_OK)
232     {
233       syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"), n->name, n->hostname);
234       return;
235     }
236     
237     outpkt->len = complen;
238     inpkt = outpkt;
239   }
240
241   /* Add sequence number */
242
243   inpkt->seqno = htonl(++(n->sent_seqno));
244   inpkt->len += sizeof(inpkt->seqno);
245
246   /* Encrypt the packet */
247
248   if(n->cipher)
249   {
250     outpkt = pkt[nextpkt++];
251
252     EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
253     EVP_EncryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
254     EVP_EncryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
255
256     outpkt->len = outlen + outpad;
257     inpkt = outpkt;
258   }
259
260   /* Add the message authentication code */
261
262   if(n->digest && n->maclength)
263     {
264       HMAC(n->digest, n->key, n->keylength, (char *)&inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len, &outlen);
265       inpkt->len += n->maclength;
266     }
267
268   /* Determine which socket we have to use */
269
270   for(sock = 0; sock < listen_sockets; sock++)
271     if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
272       break;
273
274   if(sock >= listen_sockets)
275     sock = 0;  /* If none is available, just use the first and hope for the best. */
276   
277   /* Send the packet */
278
279 #if defined(SOL_IP) && defined(IP_TOS)
280   if(priorityinheritance && origpriority != priority && listen_socket[sock].sa.sa.sa_family == AF_INET)
281     {
282       priority = origpriority;
283       if(debug_lvl >= DEBUG_TRAFFIC)
284         syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
285       if(setsockopt(sock, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
286         syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
287     }
288 #endif
289
290   if((sendto(listen_socket[sock].udp, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
291     {
292       syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
293              n->name, n->hostname, strerror(errno));
294       return;
295     }
296   
297   inpkt->len = origlen;
298 cp
299 }
300
301 /*
302   send a packet to the given vpn ip.
303 */
304 void send_packet(node_t *n, vpn_packet_t *packet)
305 {
306   node_t *via;
307 cp
308   if(debug_lvl >= DEBUG_TRAFFIC)
309     syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
310            packet->len, n->name, n->hostname);
311
312   if(n == myself)
313     {
314       if(debug_lvl >= DEBUG_TRAFFIC)
315         {
316           syslog(LOG_NOTICE, _("Packet is looping back to us!"));
317         }
318
319       return;
320     }
321  
322   if(!n->status.reachable)
323     {
324       if(debug_lvl >= DEBUG_TRAFFIC)
325         syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
326                n->name, n->hostname);
327       return;
328     }
329
330   via = (n->via == myself)?n->nexthop:n->via;
331
332   if(via != n && debug_lvl >= DEBUG_TRAFFIC)
333     syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
334            n->name, via->name, n->via->hostname);
335
336   if((myself->options | via->options) & OPTION_TCPONLY)
337     {
338       if(send_tcppacket(via->connection, packet))
339         terminate_connection(via->connection, 1);
340     }
341   else
342     send_udppacket(via, packet);
343 }
344
345 /* Broadcast a packet using the minimum spanning tree */
346
347 void broadcast_packet(node_t *from, vpn_packet_t *packet)
348 {
349   avl_node_t *node;
350   connection_t *c;
351 cp
352   if(debug_lvl >= DEBUG_TRAFFIC)
353     syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
354            packet->len, from->name, from->hostname);
355
356   for(node = connection_tree->head; node; node = node->next)
357     {
358       c = (connection_t *)node->data;
359       if(c->status.active && c->status.mst && c != from->nexthop->connection)
360         send_packet(c->node, packet);
361     }
362 cp
363 }
364
365 void flush_queue(node_t *n)
366 {
367   list_node_t *node, *next;
368 cp
369   if(debug_lvl >= DEBUG_TRAFFIC)
370     syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
371
372   for(node = n->queue->head; node; node = next)
373     {
374       next = node->next;
375       send_udppacket(n, (vpn_packet_t *)node->data);
376       list_delete_node(n->queue, node);
377     }
378 cp
379 }
380
381 void handle_incoming_vpn_data(int sock)
382 {
383   vpn_packet_t pkt;
384   int x, l = sizeof(x);
385   char *hostname;
386   sockaddr_t from;
387   socklen_t fromlen = sizeof(from);
388   node_t *n;
389 cp
390   if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
391     {
392       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
393              __FILE__, __LINE__, sock, strerror(errno));
394       cp_trace();
395       exit(1);
396     }
397   if(x)
398     {
399       syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
400       return;
401     }
402
403   if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
404     {
405       syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
406       return;
407     }
408
409   sockaddrunmap(&from);  /* Some braindead IPv6 implementations do stupid things. */
410
411   n = lookup_node_udp(&from);
412
413   if(!n)
414     {
415       hostname = sockaddr2hostname(&from);
416       syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
417       free(hostname);
418       return;
419     }
420
421   if(n->connection)
422     n->connection->last_ping_time = now;
423
424   receive_udppacket(n, &pkt);
425 cp
426 }
427