- Change SA_LEN to SALEN, former one is already defined on some platforms.
[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.4 2002/02/20 22:37:38 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_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 #ifndef HAVE_RAND_PSEUDO_BYTES
54 #define RAND_pseudo_bytes RAND_bytes
55 #endif
56
57 #include <zlib.h>
58
59 #include <utils.h>
60 #include <xalloc.h>
61 #include <avl_tree.h>
62 #include <list.h>
63
64 #include "conf.h"
65 #include "connection.h"
66 #include "meta.h"
67 #include "net.h"
68 #include "netutl.h"
69 #include "process.h"
70 #include "protocol.h"
71 #include "subnet.h"
72 #include "graph.h"
73 #include "process.h"
74 #include "route.h"
75 #include "device.h"
76 #include "event.h"
77
78 #include "system.h"
79
80 int keylifetime = 0;
81 int keyexpires = 0;
82
83 #define MAX_SEQNO 1073741824
84
85 /* VPN packet I/O */
86
87 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
88 {
89   vpn_packet_t pkt1, pkt2;
90   vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
91   int nextpkt = 0;
92   vpn_packet_t *outpkt = pkt[0];
93   int outlen, outpad;
94   long int complen = MTU + 12;
95   EVP_CIPHER_CTX ctx;
96   char hmac[EVP_MAX_MD_SIZE];
97 cp
98   /* Check the message authentication code */
99
100   if(myself->digest && myself->maclength)
101     {
102       inpkt->len -= myself->maclength;
103       HMAC(myself->digest, myself->key, myself->keylength, (char *)&inpkt->seqno, inpkt->len, hmac, NULL);
104       if(memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength))
105         {
106           syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
107           return;
108         }
109     }
110
111   /* Decrypt the packet */
112
113   if(myself->cipher)
114   {
115     outpkt = pkt[nextpkt++];
116
117     EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
118     EVP_DecryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
119     EVP_DecryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
120
121     outpkt->len = outlen + outpad;
122     inpkt = outpkt;
123   }
124
125   /* Check the sequence number */
126
127   inpkt->len -= sizeof(inpkt->seqno);
128   inpkt->seqno = ntohl(inpkt->seqno);
129
130   if(inpkt->seqno <= n->received_seqno)
131   {
132     syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
133     return;
134   }
135   
136   n->received_seqno = inpkt->seqno;
137
138   if(n->received_seqno > MAX_SEQNO)
139     keyexpires = 0;
140
141   /* Decompress the packet */
142   
143   if(myself->compression)
144   {
145     outpkt = pkt[nextpkt++];
146
147     if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK)
148     {
149       syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"), n->name, n->hostname);
150       return;
151     }
152     
153     outpkt->len = complen;
154     inpkt = outpkt;
155   }
156
157   receive_packet(n, inpkt);
158 cp
159 }
160
161 void receive_tcppacket(connection_t *c, char *buffer, int len)
162 {
163   vpn_packet_t outpkt;
164 cp
165   outpkt.len = len;
166   memcpy(outpkt.data, buffer, len);
167
168   receive_packet(c->node, &outpkt);
169 cp
170 }
171
172 void receive_packet(node_t *n, vpn_packet_t *packet)
173 {
174 cp
175   if(debug_lvl >= DEBUG_TRAFFIC)
176     syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
177
178   route_incoming(n, packet);
179 cp
180 }
181
182 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
183 {
184   vpn_packet_t pkt1, pkt2;
185   vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
186   int nextpkt = 0;
187   vpn_packet_t *outpkt;
188   int origlen;
189   int outlen, outpad;
190   long int complen = MTU + 12;
191   EVP_CIPHER_CTX ctx;
192   vpn_packet_t *copy;
193 cp
194   if(!n->status.validkey)
195     {
196       if(debug_lvl >= DEBUG_TRAFFIC)
197         syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
198                n->name, n->hostname);
199
200       /* Since packet is on the stack of handle_tap_input(),
201          we have to make a copy of it first. */
202
203       copy = xmalloc(sizeof(vpn_packet_t));
204       memcpy(copy, inpkt, sizeof(vpn_packet_t));
205
206       list_insert_tail(n->queue, copy);
207
208       if(!n->status.waitingforkey)
209         send_req_key(n->nexthop->connection, myself, n);
210
211       return;
212     }
213
214   origlen = inpkt->len;
215
216   /* Compress the packet */
217
218   if(n->compression)
219   {
220     outpkt = pkt[nextpkt++];
221
222     if(compress2(outpkt->data, &complen, inpkt->data, inpkt->len, n->compression) != Z_OK)
223     {
224       syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"), n->name, n->hostname);
225       return;
226     }
227     
228     outpkt->len = complen;
229     inpkt = outpkt;
230   }
231
232   /* Add sequence number */
233
234   inpkt->seqno = htonl(++(n->sent_seqno));
235   inpkt->len += sizeof(inpkt->seqno);
236
237   /* Encrypt the packet */
238
239   if(n->cipher)
240   {
241     outpkt = pkt[nextpkt++];
242
243     EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
244     EVP_EncryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
245     EVP_EncryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
246
247     outpkt->len = outlen + outpad;
248     inpkt = outpkt;
249   }
250
251   /* Add the message authentication code */
252
253   if(n->digest && n->maclength)
254     {
255       HMAC(n->digest, n->key, n->keylength, (char *)&inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len, &outlen);
256       inpkt->len += n->maclength;
257     }
258
259   /* Send the packet */
260
261   if((sendto(udp_socket, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
262     {
263       syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
264              n->name, n->hostname, strerror(errno));
265       return;
266     }
267   
268   inpkt->len = origlen;
269 cp
270 }
271
272 /*
273   send a packet to the given vpn ip.
274 */
275 void send_packet(node_t *n, vpn_packet_t *packet)
276 {
277   node_t *via;
278 cp
279   if(debug_lvl >= DEBUG_TRAFFIC)
280     syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
281            packet->len, n->name, n->hostname);
282
283   if(n == myself)
284     {
285       if(debug_lvl >= DEBUG_TRAFFIC)
286         {
287           syslog(LOG_NOTICE, _("Packet is looping back to us!"));
288         }
289
290       return;
291     }
292  
293   if(!n->status.reachable)
294     {
295       if(debug_lvl >= DEBUG_TRAFFIC)
296         syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
297                n->name, n->hostname);
298       return;
299     }
300
301   via = (n->via == myself)?n->nexthop:n->via;
302
303   if(via != n && debug_lvl >= DEBUG_TRAFFIC)
304     syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
305            n->name, via->name, n->via->hostname);
306
307   if((myself->options | via->options) & OPTION_TCPONLY)
308     {
309       if(send_tcppacket(via->connection, packet))
310         terminate_connection(via->connection, 1);
311     }
312   else
313     send_udppacket(via, packet);
314 }
315
316 /* Broadcast a packet using the minimum spanning tree */
317
318 void broadcast_packet(node_t *from, vpn_packet_t *packet)
319 {
320   avl_node_t *node;
321   connection_t *c;
322 cp
323   if(debug_lvl >= DEBUG_TRAFFIC)
324     syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
325            packet->len, from->name, from->hostname);
326
327   for(node = connection_tree->head; node; node = node->next)
328     {
329       c = (connection_t *)node->data;
330       if(c->status.active && c->status.mst && c != from->nexthop->connection)
331         send_packet(c->node, packet);
332     }
333 cp
334 }
335
336 void flush_queue(node_t *n)
337 {
338   list_node_t *node, *next;
339 cp
340   if(debug_lvl >= DEBUG_TRAFFIC)
341     syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
342
343   for(node = n->queue->head; node; node = next)
344     {
345       next = node->next;
346       send_udppacket(n, (vpn_packet_t *)node->data);
347       list_delete_node(n->queue, node);
348     }
349 cp
350 }
351
352 void handle_incoming_vpn_data(void)
353 {
354   vpn_packet_t pkt;
355   int x, l = sizeof(x);
356   char *hostname;
357   sockaddr_t from;
358   socklen_t fromlen = sizeof(from);
359   node_t *n;
360 cp
361   if(getsockopt(udp_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
362     {
363       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
364              __FILE__, __LINE__, udp_socket, strerror(errno));
365       return;
366     }
367   if(x)
368     {
369       syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
370       return;
371     }
372
373   if((pkt.len = recvfrom(udp_socket, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
374     {
375       syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
376       return;
377     }
378
379   n = lookup_node_udp(&from);
380
381   if(!n)
382     {
383       hostname = sockaddr2hostname(&from);
384       syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
385       free(hostname);
386       return;
387     }
388
389 /*
390   if(n->connection)
391     n->connection->last_ping_time = time(NULL);
392 */
393   receive_udppacket(n, &pkt);
394 cp
395 }
396