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.15 2002/06/08 12:57:09 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IP_H
30 #include <netinet/ip.h>
32 #ifdef HAVE_NETINET_TCP_H
33 #include <netinet/tcp.h>
40 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 /* SunOS really wants sys/socket.h BEFORE net/if.h,
45 and FreeBSD wants these lines below the rest. */
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
50 #include <openssl/rand.h>
51 #include <openssl/evp.h>
52 #include <openssl/pem.h>
53 #include <openssl/hmac.h>
63 #include "connection.h"
81 #define MAX_SEQNO 1073741824
85 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
87 vpn_packet_t pkt1, pkt2;
88 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
90 vpn_packet_t *outpkt = pkt[0];
92 long int complen = MTU + 12;
94 char hmac[EVP_MAX_MD_SIZE];
96 /* Check the message authentication code */
98 if(myself->digest && myself->maclength)
100 inpkt->len -= myself->maclength;
101 HMAC(myself->digest, myself->key, myself->keylength, (char *)&inpkt->seqno, inpkt->len, hmac, NULL);
102 if(memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength))
104 if(debug_lvl >= DEBUG_TRAFFIC)
105 syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
110 /* Decrypt the packet */
114 outpkt = pkt[nextpkt++];
116 EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
117 EVP_DecryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
118 EVP_DecryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
120 outpkt->len = outlen + outpad;
124 /* Check the sequence number */
126 inpkt->len -= sizeof(inpkt->seqno);
127 inpkt->seqno = ntohl(inpkt->seqno);
129 if(inpkt->seqno <= n->received_seqno)
131 if(debug_lvl >= DEBUG_TRAFFIC)
132 syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
136 n->received_seqno = inpkt->seqno;
138 if(n->received_seqno > MAX_SEQNO)
141 /* Decompress the packet */
143 if(myself->compression)
145 outpkt = pkt[nextpkt++];
147 if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK)
149 syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"), n->name, n->hostname);
153 outpkt->len = complen;
157 receive_packet(n, inpkt);
161 void receive_tcppacket(connection_t *c, char *buffer, int len)
166 memcpy(outpkt.data, buffer, len);
168 receive_packet(c->node, &outpkt);
172 void receive_packet(node_t *n, vpn_packet_t *packet)
175 if(debug_lvl >= DEBUG_TRAFFIC)
176 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
178 route_incoming(n, packet);
182 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
184 vpn_packet_t pkt1, pkt2;
185 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
187 vpn_packet_t *outpkt;
190 long int complen = MTU + 12;
193 static int priority = 0;
197 /* Make sure we have a valid key */
199 if(!n->status.validkey)
201 if(debug_lvl >= DEBUG_TRAFFIC)
202 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
203 n->name, n->hostname);
205 /* Since packet is on the stack of handle_tap_input(),
206 we have to make a copy of it first. */
208 copy = xmalloc(sizeof(vpn_packet_t));
209 memcpy(copy, inpkt, sizeof(vpn_packet_t));
211 list_insert_tail(n->queue, copy);
213 if(n->queue->count > MAXQUEUELENGTH)
214 list_delete_head(n->queue);
216 if(!n->status.waitingforkey)
217 send_req_key(n->nexthop->connection, myself, n);
219 n->status.waitingforkey = 1;
224 origlen = inpkt->len;
225 origpriority = inpkt->priority;
227 /* Compress the packet */
231 outpkt = pkt[nextpkt++];
233 if(compress2(outpkt->data, &complen, inpkt->data, inpkt->len, n->compression) != Z_OK)
235 syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"), n->name, n->hostname);
239 outpkt->len = complen;
243 /* Add sequence number */
245 inpkt->seqno = htonl(++(n->sent_seqno));
246 inpkt->len += sizeof(inpkt->seqno);
248 /* Encrypt the packet */
252 outpkt = pkt[nextpkt++];
254 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
255 EVP_EncryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
256 EVP_EncryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
258 outpkt->len = outlen + outpad;
262 /* Add the message authentication code */
264 if(n->digest && n->maclength)
266 HMAC(n->digest, n->key, n->keylength, (char *)&inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len, &outlen);
267 inpkt->len += n->maclength;
270 /* Determine which socket we have to use */
272 for(sock = 0; sock < listen_sockets; sock++)
273 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
276 if(sock >= listen_sockets)
277 sock = 0; /* If none is available, just use the first and hope for the best. */
279 /* Send the packet */
281 #if defined(SOL_IP) && defined(IP_TOS)
282 if(priorityinheritance && origpriority != priority && listen_socket[sock].sa.sa.sa_family == AF_INET)
284 priority = origpriority;
285 if(debug_lvl >= DEBUG_TRAFFIC)
286 syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
287 if(setsockopt(sock, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
288 syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
292 if((sendto(listen_socket[sock].udp, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
294 syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
295 n->name, n->hostname, strerror(errno));
299 inpkt->len = origlen;
304 send a packet to the given vpn ip.
306 void send_packet(node_t *n, vpn_packet_t *packet)
310 if(debug_lvl >= DEBUG_TRAFFIC)
311 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
312 packet->len, n->name, n->hostname);
316 if(debug_lvl >= DEBUG_TRAFFIC)
318 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
324 if(!n->status.reachable)
326 if(debug_lvl >= DEBUG_TRAFFIC)
327 syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
328 n->name, n->hostname);
332 via = (n->via == myself)?n->nexthop:n->via;
334 if(via != n && debug_lvl >= DEBUG_TRAFFIC)
335 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
336 n->name, via->name, n->via->hostname);
338 if((myself->options | via->options) & OPTION_TCPONLY)
340 if(send_tcppacket(via->connection, packet))
341 terminate_connection(via->connection, 1);
344 send_udppacket(via, packet);
347 /* Broadcast a packet using the minimum spanning tree */
349 void broadcast_packet(node_t *from, vpn_packet_t *packet)
354 if(debug_lvl >= DEBUG_TRAFFIC)
355 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
356 packet->len, from->name, from->hostname);
358 for(node = connection_tree->head; node; node = node->next)
360 c = (connection_t *)node->data;
361 if(c->status.active && c->status.mst && c != from->nexthop->connection)
362 send_packet(c->node, packet);
367 void flush_queue(node_t *n)
369 list_node_t *node, *next;
371 if(debug_lvl >= DEBUG_TRAFFIC)
372 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
374 for(node = n->queue->head; node; node = next)
377 send_udppacket(n, (vpn_packet_t *)node->data);
378 list_delete_node(n->queue, node);
383 void handle_incoming_vpn_data(int sock)
386 int x, l = sizeof(x);
389 socklen_t fromlen = sizeof(from);
392 if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
394 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
395 __FILE__, __LINE__, sock, strerror(errno));
401 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
405 if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
407 syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
411 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
413 n = lookup_node_udp(&from);
417 hostname = sockaddr2hostname(&from);
418 syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
424 n->connection->last_ping_time = now;
426 receive_udppacket(n, &pkt);