ip_vs_proto_ah_esp.c 4.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * ip_vs_proto_ah_esp.c:	AH/ESP IPSec load balancing support for IPVS
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12
 *
 * Authors:	Julian Anastasov <ja@ssi.bg>, February 2002
 *		Wensong Zhang <wensong@linuxvirtualserver.org>
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		version 2 as published by the Free Software Foundation;
 *
 */

13 14
#include <linux/in.h>
#include <linux/ip.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <net/ip_vs.h>


/* TODO:

struct isakmp_hdr {
	__u8		icookie[8];
	__u8		rcookie[8];
	__u8		np;
	__u8		version;
	__u8		xchgtype;
	__u8		flags;
	__u32		msgid;
	__u32		length;
};

*/

#define PORT_ISAKMP	500


static struct ip_vs_conn *
42 43 44 45 46
ah_esp_conn_in_get(const struct sk_buff *skb,
		   struct ip_vs_protocol *pp,
		   const struct iphdr *iph,
		   unsigned int proto_off,
		   int inverse)
L
Linus Torvalds 已提交
47 48 49 50 51 52
{
	struct ip_vs_conn *cp;

	if (likely(!inverse)) {
		cp = ip_vs_conn_in_get(IPPROTO_UDP,
				       iph->saddr,
53
				       htons(PORT_ISAKMP),
L
Linus Torvalds 已提交
54
				       iph->daddr,
55
				       htons(PORT_ISAKMP));
L
Linus Torvalds 已提交
56 57 58
	} else {
		cp = ip_vs_conn_in_get(IPPROTO_UDP,
				       iph->daddr,
59
				       htons(PORT_ISAKMP),
L
Linus Torvalds 已提交
60
				       iph->saddr,
61
				       htons(PORT_ISAKMP));
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	}

	if (!cp) {
		/*
		 * We are not sure if the packet is from our
		 * service, so our conn_schedule hook should return NF_ACCEPT
		 */
		IP_VS_DBG(12, "Unknown ISAKMP entry for outin packet "
			  "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
			  inverse ? "ICMP+" : "",
			  pp->name,
			  NIPQUAD(iph->saddr),
			  NIPQUAD(iph->daddr));
	}

	return cp;
}


static struct ip_vs_conn *
82 83
ah_esp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
		    const struct iphdr *iph, unsigned int proto_off, int inverse)
L
Linus Torvalds 已提交
84 85 86 87 88 89
{
	struct ip_vs_conn *cp;

	if (likely(!inverse)) {
		cp = ip_vs_conn_out_get(IPPROTO_UDP,
					iph->saddr,
90
					htons(PORT_ISAKMP),
L
Linus Torvalds 已提交
91
					iph->daddr,
92
					htons(PORT_ISAKMP));
L
Linus Torvalds 已提交
93 94 95
	} else {
		cp = ip_vs_conn_out_get(IPPROTO_UDP,
					iph->daddr,
96
					htons(PORT_ISAKMP),
L
Linus Torvalds 已提交
97
					iph->saddr,
98
					htons(PORT_ISAKMP));
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	}

	if (!cp) {
		IP_VS_DBG(12, "Unknown ISAKMP entry for inout packet "
			  "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
			  inverse ? "ICMP+" : "",
			  pp->name,
			  NIPQUAD(iph->saddr),
			  NIPQUAD(iph->daddr));
	}

	return cp;
}


static int
115 116 117
ah_esp_conn_schedule(struct sk_buff *skb,
		     struct ip_vs_protocol *pp,
		     int *verdict, struct ip_vs_conn **cpp)
L
Linus Torvalds 已提交
118 119
{
	/*
120
	 * AH/ESP is only related traffic. Pass the packet to IP stack.
L
Linus Torvalds 已提交
121 122 123 124 125 126 127
	 */
	*verdict = NF_ACCEPT;
	return 0;
}


static void
128 129
ah_esp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
		    int offset, const char *msg)
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
	char buf[256];
	struct iphdr _iph, *ih;

	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
	if (ih == NULL)
		sprintf(buf, "%s TRUNCATED", pp->name);
	else
		sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u",
			pp->name, NIPQUAD(ih->saddr),
			NIPQUAD(ih->daddr));

	printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
}


146
static void ah_esp_init(struct ip_vs_protocol *pp)
L
Linus Torvalds 已提交
147 148 149 150 151
{
	/* nothing to do now */
}


152
static void ah_esp_exit(struct ip_vs_protocol *pp)
L
Linus Torvalds 已提交
153 154 155 156 157
{
	/* nothing to do now */
}


158
#ifdef CONFIG_IP_VS_PROTO_AH
L
Linus Torvalds 已提交
159 160 161
struct ip_vs_protocol ip_vs_protocol_ah = {
	.name =			"AH",
	.protocol =		IPPROTO_AH,
162
	.num_states =		1,
L
Linus Torvalds 已提交
163
	.dont_defrag =		1,
164 165 166 167 168
	.init =			ah_esp_init,
	.exit =			ah_esp_exit,
	.conn_schedule =	ah_esp_conn_schedule,
	.conn_in_get =		ah_esp_conn_in_get,
	.conn_out_get =		ah_esp_conn_out_get,
L
Linus Torvalds 已提交
169 170 171 172 173 174 175
	.snat_handler =		NULL,
	.dnat_handler =		NULL,
	.csum_check =		NULL,
	.state_transition =	NULL,
	.register_app =		NULL,
	.unregister_app =	NULL,
	.app_conn_bind =	NULL,
176
	.debug_packet =		ah_esp_debug_packet,
L
Linus Torvalds 已提交
177 178 179
	.timeout_change =	NULL,		/* ISAKMP */
	.set_state_timeout =	NULL,
};
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#endif

#ifdef CONFIG_IP_VS_PROTO_ESP
struct ip_vs_protocol ip_vs_protocol_esp = {
	.name =			"ESP",
	.protocol =		IPPROTO_ESP,
	.num_states =		1,
	.dont_defrag =		1,
	.init =			ah_esp_init,
	.exit =			ah_esp_exit,
	.conn_schedule =	ah_esp_conn_schedule,
	.conn_in_get =		ah_esp_conn_in_get,
	.conn_out_get =		ah_esp_conn_out_get,
	.snat_handler =		NULL,
	.dnat_handler =		NULL,
	.csum_check =		NULL,
	.state_transition =	NULL,
	.register_app =		NULL,
	.unregister_app =	NULL,
	.app_conn_bind =	NULL,
	.debug_packet =		ah_esp_debug_packet,
	.timeout_change =	NULL,		/* ISAKMP */
};
#endif