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>
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.
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.
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.
20 $Id: net_packet.c,v 1.1.2.10 2002/03/17 15:59:29 guus Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.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>
48 #include <openssl/rand.h>
49 #include <openssl/evp.h>
50 #include <openssl/pem.h>
51 #include <openssl/hmac.h>
53 #ifndef HAVE_RAND_PSEUDO_BYTES
54 #define RAND_pseudo_bytes RAND_bytes
65 #include "connection.h"
83 #define MAX_SEQNO 1073741824
87 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
89 vpn_packet_t pkt1, pkt2;
90 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
92 vpn_packet_t *outpkt = pkt[0];
94 long int complen = MTU + 12;
96 char hmac[EVP_MAX_MD_SIZE];
98 /* Check the message authentication code */
100 if(myself->digest && myself->maclength)
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))
106 if(debug_lvl >= DEBUG_TRAFFIC)
107 syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
112 /* Decrypt the packet */
116 outpkt = pkt[nextpkt++];
118 EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
119 EVP_DecryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
120 EVP_DecryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
122 outpkt->len = outlen + outpad;
126 /* Check the sequence number */
128 inpkt->len -= sizeof(inpkt->seqno);
129 inpkt->seqno = ntohl(inpkt->seqno);
131 if(inpkt->seqno <= n->received_seqno)
133 if(debug_lvl >= DEBUG_TRAFFIC)
134 syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
138 n->received_seqno = inpkt->seqno;
140 if(n->received_seqno > MAX_SEQNO)
143 /* Decompress the packet */
145 if(myself->compression)
147 outpkt = pkt[nextpkt++];
149 if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK)
151 syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"), n->name, n->hostname);
155 outpkt->len = complen;
159 receive_packet(n, inpkt);
163 void receive_tcppacket(connection_t *c, char *buffer, int len)
168 memcpy(outpkt.data, buffer, len);
170 receive_packet(c->node, &outpkt);
174 void receive_packet(node_t *n, vpn_packet_t *packet)
177 if(debug_lvl >= DEBUG_TRAFFIC)
178 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
180 route_incoming(n, packet);
184 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
186 vpn_packet_t pkt1, pkt2;
187 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
189 vpn_packet_t *outpkt;
192 long int complen = MTU + 12;
195 static int priority = 0;
198 if(!n->status.validkey)
200 if(debug_lvl >= DEBUG_TRAFFIC)
201 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
202 n->name, n->hostname);
204 /* Since packet is on the stack of handle_tap_input(),
205 we have to make a copy of it first. */
207 copy = xmalloc(sizeof(vpn_packet_t));
208 memcpy(copy, inpkt, sizeof(vpn_packet_t));
210 list_insert_tail(n->queue, copy);
212 if(!n->status.waitingforkey)
213 send_req_key(n->nexthop->connection, myself, n);
218 origlen = inpkt->len;
219 origpriority = inpkt->priority;
221 /* Compress the packet */
225 outpkt = pkt[nextpkt++];
227 if(compress2(outpkt->data, &complen, inpkt->data, inpkt->len, n->compression) != Z_OK)
229 syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"), n->name, n->hostname);
233 outpkt->len = complen;
237 /* Add sequence number */
239 inpkt->seqno = htonl(++(n->sent_seqno));
240 inpkt->len += sizeof(inpkt->seqno);
242 /* Encrypt the packet */
246 outpkt = pkt[nextpkt++];
248 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
249 EVP_EncryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
250 EVP_EncryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
252 outpkt->len = outlen + outpad;
256 /* Add the message authentication code */
258 if(n->digest && n->maclength)
260 HMAC(n->digest, n->key, n->keylength, (char *)&inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len, &outlen);
261 inpkt->len += n->maclength;
264 /* Send the packet */
266 #if defined(SOL_IP) && defined(IP_TOS)
267 if(priorityinheritance && origpriority != priority)
269 priority = origpriority;
270 if(debug_lvl >= DEBUG_TRAFFIC)
271 syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
272 if(setsockopt(udp_socket[0], SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
273 syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
277 if((sendto(udp_socket[0], (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
279 syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
280 n->name, n->hostname, strerror(errno));
284 inpkt->len = origlen;
289 send a packet to the given vpn ip.
291 void send_packet(node_t *n, vpn_packet_t *packet)
295 if(debug_lvl >= DEBUG_TRAFFIC)
296 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
297 packet->len, n->name, n->hostname);
301 if(debug_lvl >= DEBUG_TRAFFIC)
303 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
309 if(!n->status.reachable)
311 if(debug_lvl >= DEBUG_TRAFFIC)
312 syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
313 n->name, n->hostname);
317 via = (n->via == myself)?n->nexthop:n->via;
319 if(via != n && debug_lvl >= DEBUG_TRAFFIC)
320 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
321 n->name, via->name, n->via->hostname);
323 if((myself->options | via->options) & OPTION_TCPONLY)
325 if(send_tcppacket(via->connection, packet))
326 terminate_connection(via->connection, 1);
329 send_udppacket(via, packet);
332 /* Broadcast a packet using the minimum spanning tree */
334 void broadcast_packet(node_t *from, vpn_packet_t *packet)
339 if(debug_lvl >= DEBUG_TRAFFIC)
340 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
341 packet->len, from->name, from->hostname);
343 for(node = connection_tree->head; node; node = node->next)
345 c = (connection_t *)node->data;
346 if(c->status.active && c->status.mst && c != from->nexthop->connection)
347 send_packet(c->node, packet);
352 void flush_queue(node_t *n)
354 list_node_t *node, *next;
356 if(debug_lvl >= DEBUG_TRAFFIC)
357 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
359 for(node = n->queue->head; node; node = next)
362 send_udppacket(n, (vpn_packet_t *)node->data);
363 list_delete_node(n->queue, node);
368 void handle_incoming_vpn_data(int sock)
371 int x, l = sizeof(x);
374 socklen_t fromlen = sizeof(from);
377 if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
379 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
380 __FILE__, __LINE__, sock, strerror(errno));
386 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
390 if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
392 syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
396 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
398 n = lookup_node_udp(&from);
402 hostname = sockaddr2hostname(&from);
403 syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
409 n->connection->last_ping_time = now;
411 receive_udppacket(n, &pkt);