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.14 2002/04/18 20:09:05 zarq 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>
61 #include "connection.h"
79 #define MAX_SEQNO 1073741824
83 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
85 vpn_packet_t pkt1, pkt2;
86 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
88 vpn_packet_t *outpkt = pkt[0];
90 long int complen = MTU + 12;
92 char hmac[EVP_MAX_MD_SIZE];
94 /* Check the message authentication code */
96 if(myself->digest && myself->maclength)
98 inpkt->len -= myself->maclength;
99 HMAC(myself->digest, myself->key, myself->keylength, (char *)&inpkt->seqno, inpkt->len, hmac, NULL);
100 if(memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength))
102 if(debug_lvl >= DEBUG_TRAFFIC)
103 syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
108 /* Decrypt the packet */
112 outpkt = pkt[nextpkt++];
114 EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
115 EVP_DecryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
116 EVP_DecryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
118 outpkt->len = outlen + outpad;
122 /* Check the sequence number */
124 inpkt->len -= sizeof(inpkt->seqno);
125 inpkt->seqno = ntohl(inpkt->seqno);
127 if(inpkt->seqno <= n->received_seqno)
129 if(debug_lvl >= DEBUG_TRAFFIC)
130 syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
134 n->received_seqno = inpkt->seqno;
136 if(n->received_seqno > MAX_SEQNO)
139 /* Decompress the packet */
141 if(myself->compression)
143 outpkt = pkt[nextpkt++];
145 if(uncompress(outpkt->data, &complen, inpkt->data, inpkt->len) != Z_OK)
147 syslog(LOG_ERR, _("Error while uncompressing packet from %s (%s)"), n->name, n->hostname);
151 outpkt->len = complen;
155 receive_packet(n, inpkt);
159 void receive_tcppacket(connection_t *c, char *buffer, int len)
164 memcpy(outpkt.data, buffer, len);
166 receive_packet(c->node, &outpkt);
170 void receive_packet(node_t *n, vpn_packet_t *packet)
173 if(debug_lvl >= DEBUG_TRAFFIC)
174 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
176 route_incoming(n, packet);
180 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
182 vpn_packet_t pkt1, pkt2;
183 vpn_packet_t *pkt[] = {&pkt1, &pkt2, &pkt1, &pkt2};
185 vpn_packet_t *outpkt;
188 long int complen = MTU + 12;
191 static int priority = 0;
195 /* Make sure we have a valid key */
197 if(!n->status.validkey)
199 if(debug_lvl >= DEBUG_TRAFFIC)
200 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
201 n->name, n->hostname);
203 /* Since packet is on the stack of handle_tap_input(),
204 we have to make a copy of it first. */
206 copy = xmalloc(sizeof(vpn_packet_t));
207 memcpy(copy, inpkt, sizeof(vpn_packet_t));
209 list_insert_tail(n->queue, copy);
211 if(n->queue->count > MAXQUEUELENGTH)
212 list_delete_head(n->queue);
214 if(!n->status.waitingforkey)
215 send_req_key(n->nexthop->connection, myself, n);
217 n->status.waitingforkey = 1;
222 origlen = inpkt->len;
223 origpriority = inpkt->priority;
225 /* Compress the packet */
229 outpkt = pkt[nextpkt++];
231 if(compress2(outpkt->data, &complen, inpkt->data, inpkt->len, n->compression) != Z_OK)
233 syslog(LOG_ERR, _("Error while compressing packet to %s (%s)"), n->name, n->hostname);
237 outpkt->len = complen;
241 /* Add sequence number */
243 inpkt->seqno = htonl(++(n->sent_seqno));
244 inpkt->len += sizeof(inpkt->seqno);
246 /* Encrypt the packet */
250 outpkt = pkt[nextpkt++];
252 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
253 EVP_EncryptUpdate(&ctx, (char *)&outpkt->seqno, &outlen, (char *)&inpkt->seqno, inpkt->len);
254 EVP_EncryptFinal(&ctx, (char *)&outpkt->seqno + outlen, &outpad);
256 outpkt->len = outlen + outpad;
260 /* Add the message authentication code */
262 if(n->digest && n->maclength)
264 HMAC(n->digest, n->key, n->keylength, (char *)&inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len, &outlen);
265 inpkt->len += n->maclength;
268 /* Determine which socket we have to use */
270 for(sock = 0; sock < listen_sockets; sock++)
271 if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
274 if(sock >= listen_sockets)
275 sock = 0; /* If none is available, just use the first and hope for the best. */
277 /* Send the packet */
279 #if defined(SOL_IP) && defined(IP_TOS)
280 if(priorityinheritance && origpriority != priority && listen_socket[sock].sa.sa.sa_family == AF_INET)
282 priority = origpriority;
283 if(debug_lvl >= DEBUG_TRAFFIC)
284 syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
285 if(setsockopt(sock, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
286 syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
290 if((sendto(listen_socket[sock].udp, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
292 syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
293 n->name, n->hostname, strerror(errno));
297 inpkt->len = origlen;
302 send a packet to the given vpn ip.
304 void send_packet(node_t *n, vpn_packet_t *packet)
308 if(debug_lvl >= DEBUG_TRAFFIC)
309 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
310 packet->len, n->name, n->hostname);
314 if(debug_lvl >= DEBUG_TRAFFIC)
316 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
322 if(!n->status.reachable)
324 if(debug_lvl >= DEBUG_TRAFFIC)
325 syslog(LOG_INFO, _("Node %s (%s) is not reachable"),
326 n->name, n->hostname);
330 via = (n->via == myself)?n->nexthop:n->via;
332 if(via != n && debug_lvl >= DEBUG_TRAFFIC)
333 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
334 n->name, via->name, n->via->hostname);
336 if((myself->options | via->options) & OPTION_TCPONLY)
338 if(send_tcppacket(via->connection, packet))
339 terminate_connection(via->connection, 1);
342 send_udppacket(via, packet);
345 /* Broadcast a packet using the minimum spanning tree */
347 void broadcast_packet(node_t *from, vpn_packet_t *packet)
352 if(debug_lvl >= DEBUG_TRAFFIC)
353 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
354 packet->len, from->name, from->hostname);
356 for(node = connection_tree->head; node; node = node->next)
358 c = (connection_t *)node->data;
359 if(c->status.active && c->status.mst && c != from->nexthop->connection)
360 send_packet(c->node, packet);
365 void flush_queue(node_t *n)
367 list_node_t *node, *next;
369 if(debug_lvl >= DEBUG_TRAFFIC)
370 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
372 for(node = n->queue->head; node; node = next)
375 send_udppacket(n, (vpn_packet_t *)node->data);
376 list_delete_node(n->queue, node);
381 void handle_incoming_vpn_data(int sock)
384 int x, l = sizeof(x);
387 socklen_t fromlen = sizeof(from);
390 if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
392 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
393 __FILE__, __LINE__, sock, strerror(errno));
399 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
403 if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
405 syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
409 sockaddrunmap(&from); /* Some braindead IPv6 implementations do stupid things. */
411 n = lookup_node_udp(&from);
415 hostname = sockaddr2hostname(&from);
416 syslog(LOG_WARNING, _("Received UDP packet from unknown source %s"), hostname);
422 n->connection->last_ping_time = now;
424 receive_udppacket(n, &pkt);