Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 2 | * ip_vs_proto_ah_esp.c: AH/ESP IPSec load balancing support for IPVS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * 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 Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 13 | #include <linux/in.h> |
| 14 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 | |
| 25 | struct 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 | |
| 41 | static struct ip_vs_conn * |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 42 | ah_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 Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 44 | int inverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct ip_vs_conn *cp; |
| 47 | |
| 48 | if (likely(!inverse)) { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 49 | cp = ip_vs_conn_in_get(af, IPPROTO_UDP, |
| 50 | &iph->saddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 51 | htons(PORT_ISAKMP), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 52 | &iph->daddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 53 | htons(PORT_ISAKMP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } else { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 55 | cp = ip_vs_conn_in_get(af, IPPROTO_UDP, |
| 56 | &iph->daddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 57 | htons(PORT_ISAKMP), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 58 | &iph->saddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 59 | htons(PORT_ISAKMP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 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 Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 67 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return cp; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static struct ip_vs_conn * |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 80 | ah_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct ip_vs_conn *cp; |
| 87 | |
| 88 | if (likely(!inverse)) { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 89 | cp = ip_vs_conn_out_get(af, IPPROTO_UDP, |
| 90 | &iph->saddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 91 | htons(PORT_ISAKMP), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 92 | &iph->daddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 93 | htons(PORT_ISAKMP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } else { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 95 | cp = ip_vs_conn_out_get(af, IPPROTO_UDP, |
| 96 | &iph->daddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 97 | htons(PORT_ISAKMP), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 98 | &iph->saddr, |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 99 | htons(PORT_ISAKMP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (!cp) { |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 103 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return cp; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static int |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 116 | ah_esp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp, |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 117 | int *verdict, struct ip_vs_conn **cpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | /* |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 120 | * AH/ESP is only related traffic. Pass the packet to IP stack. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | */ |
| 122 | *verdict = NF_ACCEPT; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void |
Julius Volz | 3b047d9 | 2008-09-02 15:55:41 +0200 | [diff] [blame] | 128 | ah_esp_debug_packet_v4(struct ip_vs_protocol *pp, const struct sk_buff *skb, |
| 129 | int offset, const char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 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 Volz | 3b047d9 | 2008-09-02 15:55:41 +0200 | [diff] [blame] | 145 | #ifdef CONFIG_IP_VS_IPV6 |
| 146 | static void |
| 147 | ah_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 | |
| 165 | static void |
| 166 | ah_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 Haley | d286600 | 2008-09-16 11:11:11 -0400 | [diff] [blame] | 170 | if (skb->protocol == htons(ETH_P_IPV6)) |
Julius Volz | 3b047d9 | 2008-09-02 15:55:41 +0200 | [diff] [blame] | 171 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 178 | static void ah_esp_init(struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
| 180 | /* nothing to do now */ |
| 181 | } |
| 182 | |
| 183 | |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 184 | static void ah_esp_exit(struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | /* nothing to do now */ |
| 187 | } |
| 188 | |
| 189 | |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 190 | #ifdef CONFIG_IP_VS_PROTO_AH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | struct ip_vs_protocol ip_vs_protocol_ah = { |
| 192 | .name = "AH", |
| 193 | .protocol = IPPROTO_AH, |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 194 | .num_states = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | .dont_defrag = 1, |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 196 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .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 Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 208 | .debug_packet = ah_esp_debug_packet, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | .timeout_change = NULL, /* ISAKMP */ |
| 210 | .set_state_timeout = NULL, |
| 211 | }; |
Julius Volz | 409a196 | 2008-08-22 14:06:12 +0200 | [diff] [blame] | 212 | #endif |
| 213 | |
| 214 | #ifdef CONFIG_IP_VS_PROTO_ESP |
| 215 | struct 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 |