2 net_packet.c -- Handles in- and outgoing VPN packets
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
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.25 2002/11/14 22:09:03 guus Exp $
28 #include <netinet/in.h>
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 /* SunOS really wants sys/socket.h BEFORE net/if.h,
39 and FreeBSD wants these lines below the rest. */
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
53 #include <openssl/rand.h>
54 #include <openssl/evp.h>
55 #include <openssl/pem.h>
56 #include <openssl/hmac.h>
66 #include "connection.h"
84 #define MAX_SEQNO 1073741824
88 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
90 vpn_packet_t pkt1, pkt2;
91 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
93 vpn_packet_t *outpkt = pkt[0];
95 long int complen = MTU + 12;
97 char hmac[EVP_MAX_MD_SIZE];
101 /* Check the message authentication code */
103 if(myself->digest && myself->maclength) {
104 inpkt->len -= myself->maclength;
105 HMAC(myself->digest, myself->key, myself->keylength,
106 (char *) &inpkt->seqno, inpkt->len, hmac, NULL);
108 if(memcmp(hmac, (char *) &inpkt->seqno + inpkt->len, myself->maclength)) {
109 if(debug_lvl >= DEBUG_TRAFFIC)
110 syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"),
111 n->name, n->hostname);
116 /* Decrypt the packet */
119 outpkt = pkt[nextpkt++];
121 EVP_DecryptInit(&ctx, myself->cipher, myself->key,
122 myself->key + myself->cipher->key_len);
123 EVP_DecryptUpdate(&ctx, (char *) &outpkt->seqno, &outlen,
124 (char *) &inpkt->seqno, inpkt->len);
125 EVP_DecryptFinal(&ctx, (char *) &outpkt->seqno + outlen, &outpad);
127 outpkt->len = outlen + outpad;
131 /* Check the sequence number */
133 inpkt->len -= sizeof(inpkt->seqno);
134 inpkt->seqno = ntohl(inpkt->seqno);
136 if(inpkt->seqno <= n->received_seqno) {
137 if(debug_lvl >= DEBUG_TRAFFIC)
139 _("Got late or replayed packet from %s (%s), seqno %d"),
140 n->name, n->hostname, inpkt->seqno);
144 n->received_seqno = inpkt->seqno;
146 if(n->received_seqno > MAX_SEQNO)
149 /* Decompress the packet */
151 if(myself->compression) {
152 outpkt = pkt[nextpkt++];
154 if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK) {
155 syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"),
156 n->name, n->hostname);
160 outpkt->len = complen;
164 receive_packet(n, inpkt);
167 void receive_tcppacket(connection_t *c, char *buffer, int len)
174 memcpy(outpkt.data, buffer, len);
176 receive_packet(c->node, &outpkt);
179 void receive_packet(node_t *n, vpn_packet_t *packet)
183 if(debug_lvl >= DEBUG_TRAFFIC)
184 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"),
185 packet->len, n->name, n->hostname);
187 route_incoming(n, packet);
190 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
192 vpn_packet_t pkt1, pkt2;
193 vpn_packet_t *pkt[] = { &pkt1, &pkt2, &pkt1, &pkt2 };
195 vpn_packet_t *outpkt;
198 long int complen = MTU + 12;
201 static int priority = 0;
207 /* Make sure we have a valid key */
209 if(!n->status.validkey) {
210 if(debug_lvl >= DEBUG_TRAFFIC)
212 _("No valid key known yet for %s (%s), queueing packet"),
213 n->name, n->hostname);
215 /* Since packet is on the stack of handle_tap_input(), we have to make a copy of it first. */
217 copy = xmalloc(sizeof(vpn_packet_t));
218 memcpy(copy, inpkt, sizeof(vpn_packet_t));
220 list_insert_tail(n->queue, copy);
222 if(n->queue->count > MAXQUEUELENGTH)
223 list_delete_head(n->queue);
225 if(!n->status.waitingforkey)
226 send_req_key(n->nexthop->connection, myself, n);
228 n->status.waitingforkey = 1;
233 origlen = inpkt->len;
234 origpriority = inpkt->priority;
236 /* Compress the packet */
239 outpkt = pkt[nextpkt++];
242 (outpkt->data, &complen, inpkt->data, inpkt->len,
243 n->compression) != Z_OK) {
244 syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"),
245 n->name, n->hostname);
249 outpkt->len = complen;
253 /* Add sequence number */
255 inpkt->seqno = htonl(++(n->sent_seqno));
256 inpkt->len += sizeof(inpkt->seqno);
258 /* Encrypt the packet */
261 outpkt = pkt[nextpkt++];
263 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
264 EVP_EncryptUpdate(&ctx, (char *) &outpkt->seqno, &outlen,
265 (char *) &inpkt->seqno, inpkt->len);
266 EVP_EncryptFinal(&ctx, (char *) &outpkt->seqno + outlen, &outpad);
268 outpkt->len = outlen + outpad;
272 /* Add the message authentication code */
274 if(n->digest && n->maclength) {
275 HMAC(n->digest, n->key, n->keylength, (char *) &inpkt->seqno,
276 inpkt->len, (char *) &inpkt->seqno + inpkt->len, &outlen);
277 inpkt->len += n->maclength;
280 /* Determine which socket we have to use */
282 for(sock = 0; sock < listen_sockets; sock++)
283 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
286 if(sock >= listen_sockets)
287 sock = 0; /* If none is available, just use the first and hope for the best. */
289 /* Send the packet */
291 #if defined(SOL_IP) && defined(IP_TOS)
292 if(priorityinheritance && origpriority != priority
293 && listen_socket[sock].sa.sa.sa_family == AF_INET) {
294 priority = origpriority;
295 if(debug_lvl >= DEBUG_TRAFFIC)
296 syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"),
298 if(setsockopt(listen_socket[sock].udp, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
299 syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt",
304 if((sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) {
305 syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name,
306 n->hostname, strerror(errno));
310 inpkt->len = origlen;
314 send a packet to the given vpn ip.
316 void send_packet(node_t *n, vpn_packet_t *packet)
322 if(debug_lvl >= DEBUG_TRAFFIC)
323 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
324 packet->len, n->name, n->hostname);
327 if(debug_lvl >= DEBUG_TRAFFIC)
328 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
333 if(!n->status.reachable) {
334 if(debug_lvl >= DEBUG_TRAFFIC)
335 syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
336 n->name, n->hostname);
341 via = (n->via == myself) ? n->nexthop : n->via;
343 if(via != n && debug_lvl >= DEBUG_TRAFFIC)
344 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
345 n->name, via->name, n->via->hostname);
347 if((myself->options | via->options) & OPTION_TCPONLY) {
348 if(send_tcppacket(via->connection, packet))
349 terminate_connection(via->connection, 1);
351 send_udppacket(via, packet);
354 /* Broadcast a packet using the minimum spanning tree */
356 void broadcast_packet(node_t *from, vpn_packet_t *packet)
363 if(debug_lvl >= DEBUG_TRAFFIC)
364 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
365 packet->len, from->name, from->hostname);
367 for(node = connection_tree->head; node; node = node->next) {
368 c = (connection_t *) node->data;
370 if(c->status.active && c->status.mst && c != from->nexthop->connection)
371 send_packet(c->node, packet);
375 void flush_queue(node_t *n)
377 list_node_t *node, *next;
381 if(debug_lvl >= DEBUG_TRAFFIC)
382 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
384 for(node = n->queue->head; node; node = next) {
386 send_udppacket(n, (vpn_packet_t *) node->data);
387 list_delete_node(n->queue, node);
391 void handle_incoming_vpn_data(int sock)
394 int x, l = sizeof(x);
397 socklen_t fromlen = sizeof(from);
402 if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0) {
403 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
404 __FILE__, __LINE__, sock, strerror(errno));
410 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
414 pkt.len = recvfrom(sock, (char *) &pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
417 syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
421 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
423 n = lookup_node_udp(&from);
426 hostname = sockaddr2hostname(&from);
427 syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"),
434 n->connection->last_ping_time = now;
436 receive_udppacket(n, &pkt);