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