blob: 80ab0c8e5b4af113e7a67fa0f4032a808d248119 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Julius Volz409a1962008-08-22 14:06:12 +02002 * ip_vs_proto_ah_esp.c: AH/ESP IPSec load balancing support for IPVS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Julian Anastasov <ja@ssi.bg>, February 2002
5 * Wensong Zhang <wensong@linuxvirtualserver.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation;
10 *
11 */
12
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020013#include <linux/in.h>
14#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter_ipv4.h>
19
20#include <net/ip_vs.h>
21
22
23/* TODO:
24
25struct isakmp_hdr {
26 __u8 icookie[8];
27 __u8 rcookie[8];
28 __u8 np;
29 __u8 version;
30 __u8 xchgtype;
31 __u8 flags;
32 __u32 msgid;
33 __u32 length;
34};
35
36*/
37
38#define PORT_ISAKMP 500
39
40
41static struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +020042ah_esp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
43 const struct ip_vs_iphdr *iph, unsigned int proto_off,
Julius Volz409a1962008-08-22 14:06:12 +020044 int inverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct ip_vs_conn *cp;
47
48 if (likely(!inverse)) {
Julius Volz28364a52008-09-02 15:55:43 +020049 cp = ip_vs_conn_in_get(af, IPPROTO_UDP,
50 &iph->saddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090051 htons(PORT_ISAKMP),
Julius Volz28364a52008-09-02 15:55:43 +020052 &iph->daddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090053 htons(PORT_ISAKMP));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 } else {
Julius Volz28364a52008-09-02 15:55:43 +020055 cp = ip_vs_conn_in_get(af, IPPROTO_UDP,
56 &iph->daddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090057 htons(PORT_ISAKMP),
Julius Volz28364a52008-09-02 15:55:43 +020058 &iph->saddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090059 htons(PORT_ISAKMP));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
62 if (!cp) {
63 /*
64 * We are not sure if the packet is from our
65 * service, so our conn_schedule hook should return NF_ACCEPT
66 */
Julius Volz51ef3482008-09-02 15:55:40 +020067 IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for outin packet "
68 "%s%s %s->%s\n",
69 inverse ? "ICMP+" : "",
70 pp->name,
71 IP_VS_DBG_ADDR(af, &iph->saddr),
72 IP_VS_DBG_ADDR(af, &iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
75 return cp;
76}
77
78
79static struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +020080ah_esp_conn_out_get(int af, const struct sk_buff *skb,
81 struct ip_vs_protocol *pp,
82 const struct ip_vs_iphdr *iph,
83 unsigned int proto_off,
84 int inverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct ip_vs_conn *cp;
87
88 if (likely(!inverse)) {
Julius Volz28364a52008-09-02 15:55:43 +020089 cp = ip_vs_conn_out_get(af, IPPROTO_UDP,
90 &iph->saddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090091 htons(PORT_ISAKMP),
Julius Volz28364a52008-09-02 15:55:43 +020092 &iph->daddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090093 htons(PORT_ISAKMP));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 } else {
Julius Volz28364a52008-09-02 15:55:43 +020095 cp = ip_vs_conn_out_get(af, IPPROTO_UDP,
96 &iph->daddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090097 htons(PORT_ISAKMP),
Julius Volz28364a52008-09-02 15:55:43 +020098 &iph->saddr,
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +090099 htons(PORT_ISAKMP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
102 if (!cp) {
Julius Volz51ef3482008-09-02 15:55:40 +0200103 IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for inout packet "
104 "%s%s %s->%s\n",
105 inverse ? "ICMP+" : "",
106 pp->name,
107 IP_VS_DBG_ADDR(af, &iph->saddr),
108 IP_VS_DBG_ADDR(af, &iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
111 return cp;
112}
113
114
115static int
Julius Volz51ef3482008-09-02 15:55:40 +0200116ah_esp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
Julius Volz409a1962008-08-22 14:06:12 +0200117 int *verdict, struct ip_vs_conn **cpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 /*
Julius Volz409a1962008-08-22 14:06:12 +0200120 * AH/ESP is only related traffic. Pass the packet to IP stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122 *verdict = NF_ACCEPT;
123 return 0;
124}
125
126
127static void
Julius Volz3b047d92008-09-02 15:55:41 +0200128ah_esp_debug_packet_v4(struct ip_vs_protocol *pp, const struct sk_buff *skb,
129 int offset, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 char buf[256];
132 struct iphdr _iph, *ih;
133
134 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
135 if (ih == NULL)
136 sprintf(buf, "%s TRUNCATED", pp->name);
137 else
138 sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u",
139 pp->name, NIPQUAD(ih->saddr),
140 NIPQUAD(ih->daddr));
141
142 printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
143}
144
Julius Volz3b047d92008-09-02 15:55:41 +0200145#ifdef CONFIG_IP_VS_IPV6
146static void
147ah_esp_debug_packet_v6(struct ip_vs_protocol *pp, const struct sk_buff *skb,
148 int offset, const char *msg)
149{
150 char buf[256];
151 struct ipv6hdr _iph, *ih;
152
153 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
154 if (ih == NULL)
155 sprintf(buf, "%s TRUNCATED", pp->name);
156 else
157 sprintf(buf, "%s " NIP6_FMT "->" NIP6_FMT,
158 pp->name, NIP6(ih->saddr),
159 NIP6(ih->daddr));
160
161 printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
162}
163#endif
164
165static void
166ah_esp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
167 int offset, const char *msg)
168{
169#ifdef CONFIG_IP_VS_IPV6
Brian Haleyd2866002008-09-16 11:11:11 -0400170 if (skb->protocol == htons(ETH_P_IPV6))
Julius Volz3b047d92008-09-02 15:55:41 +0200171 ah_esp_debug_packet_v6(pp, skb, offset, msg);
172 else
173#endif
174 ah_esp_debug_packet_v4(pp, skb, offset, msg);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Julius Volz409a1962008-08-22 14:06:12 +0200178static void ah_esp_init(struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 /* nothing to do now */
181}
182
183
Julius Volz409a1962008-08-22 14:06:12 +0200184static void ah_esp_exit(struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 /* nothing to do now */
187}
188
189
Julius Volz409a1962008-08-22 14:06:12 +0200190#ifdef CONFIG_IP_VS_PROTO_AH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191struct ip_vs_protocol ip_vs_protocol_ah = {
192 .name = "AH",
193 .protocol = IPPROTO_AH,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700194 .num_states = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .dont_defrag = 1,
Julius Volz409a1962008-08-22 14:06:12 +0200196 .init = ah_esp_init,
197 .exit = ah_esp_exit,
198 .conn_schedule = ah_esp_conn_schedule,
199 .conn_in_get = ah_esp_conn_in_get,
200 .conn_out_get = ah_esp_conn_out_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .snat_handler = NULL,
202 .dnat_handler = NULL,
203 .csum_check = NULL,
204 .state_transition = NULL,
205 .register_app = NULL,
206 .unregister_app = NULL,
207 .app_conn_bind = NULL,
Julius Volz409a1962008-08-22 14:06:12 +0200208 .debug_packet = ah_esp_debug_packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .timeout_change = NULL, /* ISAKMP */
210 .set_state_timeout = NULL,
211};
Julius Volz409a1962008-08-22 14:06:12 +0200212#endif
213
214#ifdef CONFIG_IP_VS_PROTO_ESP
215struct ip_vs_protocol ip_vs_protocol_esp = {
216 .name = "ESP",
217 .protocol = IPPROTO_ESP,
218 .num_states = 1,
219 .dont_defrag = 1,
220 .init = ah_esp_init,
221 .exit = ah_esp_exit,
222 .conn_schedule = ah_esp_conn_schedule,
223 .conn_in_get = ah_esp_conn_in_get,
224 .conn_out_get = ah_esp_conn_out_get,
225 .snat_handler = NULL,
226 .dnat_handler = NULL,
227 .csum_check = NULL,
228 .state_transition = NULL,
229 .register_app = NULL,
230 .unregister_app = NULL,
231 .app_conn_bind = NULL,
232 .debug_packet = ah_esp_debug_packet,
233 .timeout_change = NULL, /* ISAKMP */
234};
235#endif