1 /*
2  * fiked - a fake IKE PSK+XAUTH daemon based on vpnc
3  * Copyright (C) 2005, Daniel Roethlisberger <daniel@roe.ch>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see http://www.gnu.org/copyleft/
17  *
18  * $Id: send_dgm.c 97 2005-12-17 17:43:33Z roe $
19  */
20 
21 #include "send_dgm.h"
22 #include "log.h"
23 #include "ike.h"
24 #ifdef WITH_LIBNET
25 #include <libnet.h>
26 
27 /*
28  * Send a UDP datagram on a raw socket.
29  */
raw_send(datagram * dgm,char * shost,uint16_t sport)30 void raw_send(datagram *dgm, char *shost, uint16_t sport)
31 {
32 	static char errbuf[LIBNET_ERRBUF_SIZE];
33 	libnet_t *lnet = libnet_init(LIBNET_RAW4, NULL, errbuf);
34 	libnet_ptag_t udp = libnet_build_udp(
35 		sport, ntohs(dgm->peer_addr.sin_port),
36 		LIBNET_UDP_H + dgm->len, 0, dgm->data, dgm->len, lnet, 0);
37 	if(udp <= 0) {
38 		log_printf(NULL, "ERROR: cannot build UDP header: %s\n",
39 			libnet_geterror(lnet));
40 		return;
41 	}
42 	libnet_ptag_t ip = libnet_build_ipv4(
43 		LIBNET_IPV4_H + LIBNET_UDP_H + dgm->len,
44 		0, 0, 0, 64, IPPROTO_UDP, 0,
45 		inet_addr(shost),
46 		dgm->peer_addr.sin_addr.s_addr,
47 		NULL, 0, lnet, 0);
48 	if(ip <= 0) {
49 		log_printf(NULL, "FATAL: cannot build IP header: %s\n",
50 			libnet_geterror(lnet));
51 		return;
52 	}
53 	int ret = libnet_write(lnet);
54 	if(ret <= 0) {
55 		log_printf(NULL, "ERROR: write error: %s\n",
56 			libnet_geterror(lnet));
57 		return;
58 	}
59 	libnet_destroy(lnet);
60 }
61 
62 #endif /* WITH_LIBNET */
63 
64 /*
65  * Send a datagram, using either raw sockets or UDP socket, depending on opt_raw.
66  */
send_datagram(peer_ctx * ctx,datagram * dgm)67 void send_datagram(peer_ctx *ctx, datagram *dgm)
68 {
69 #ifdef WITH_LIBNET
70 	if(ctx->cfg->opt_raw) {
71 		raw_send(dgm, ctx->cfg->gateway, IKE_PORT);
72 	} else {
73 		udp_socket_send(ctx->cfg->us, dgm);
74 	}
75 #else
76 	udp_socket_send(ctx->cfg->us, dgm);
77 #endif
78 }
79 
80