]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * IPVS An implementation of the IP virtual server support for the | |
4 | * LINUX operating system. IPVS is now implemented as a module | |
5 | * over the Netfilter framework. IPVS can be used to build a | |
6 | * high-performance and highly available server based on a | |
7 | * cluster of servers. | |
8 | * | |
1da177e4 LT |
9 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
10 | * Peter Kese <peter.kese@ijs.si> | |
11 | * Julian Anastasov <ja@ssi.bg> | |
12 | * | |
1da177e4 LT |
13 | * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese, |
14 | * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms | |
15 | * and others. | |
16 | * | |
17 | * Changes: | |
18 | * Paul `Rusty' Russell properly handle non-linear skbs | |
6869c4d8 | 19 | * Harald Welte don't use nfcache |
1da177e4 LT |
20 | */ |
21 | ||
9aada7ac HE |
22 | #define KMSG_COMPONENT "IPVS" |
23 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
24 | ||
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/ip.h> | |
28 | #include <linux/tcp.h> | |
2906f66a | 29 | #include <linux/sctp.h> |
1da177e4 | 30 | #include <linux/icmp.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
1da177e4 LT |
32 | |
33 | #include <net/ip.h> | |
34 | #include <net/tcp.h> | |
35 | #include <net/udp.h> | |
36 | #include <net/icmp.h> /* for icmp_send */ | |
508f744c | 37 | #include <net/gue.h> |
6aedd14b | 38 | #include <net/gre.h> |
1da177e4 | 39 | #include <net/route.h> |
2c70b519 | 40 | #include <net/ip6_checksum.h> |
61b1ab45 | 41 | #include <net/netns/generic.h> /* net_generic() */ |
1da177e4 LT |
42 | |
43 | #include <linux/netfilter.h> | |
44 | #include <linux/netfilter_ipv4.h> | |
45 | ||
2a3b791e JV |
46 | #ifdef CONFIG_IP_VS_IPV6 |
47 | #include <net/ipv6.h> | |
48 | #include <linux/netfilter_ipv6.h> | |
489fdeda | 49 | #include <net/ip6_route.h> |
2a3b791e JV |
50 | #endif |
51 | ||
1da177e4 | 52 | #include <net/ip_vs.h> |
6ecd7548 | 53 | #include <linux/indirect_call_wrapper.h> |
1da177e4 LT |
54 | |
55 | ||
56 | EXPORT_SYMBOL(register_ip_vs_scheduler); | |
57 | EXPORT_SYMBOL(unregister_ip_vs_scheduler); | |
1da177e4 LT |
58 | EXPORT_SYMBOL(ip_vs_proto_name); |
59 | EXPORT_SYMBOL(ip_vs_conn_new); | |
60 | EXPORT_SYMBOL(ip_vs_conn_in_get); | |
61 | EXPORT_SYMBOL(ip_vs_conn_out_get); | |
62 | #ifdef CONFIG_IP_VS_PROTO_TCP | |
63 | EXPORT_SYMBOL(ip_vs_tcp_conn_listen); | |
64 | #endif | |
65 | EXPORT_SYMBOL(ip_vs_conn_put); | |
66 | #ifdef CONFIG_IP_VS_DEBUG | |
67 | EXPORT_SYMBOL(ip_vs_get_debug_level); | |
68 | #endif | |
39b97223 | 69 | EXPORT_SYMBOL(ip_vs_new_conn_out); |
1da177e4 | 70 | |
6ecd7548 MC |
71 | #ifdef CONFIG_IP_VS_PROTO_TCP |
72 | INDIRECT_CALLABLE_DECLARE(int | |
73 | tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
74 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)); | |
75 | #endif | |
76 | ||
77 | #ifdef CONFIG_IP_VS_PROTO_UDP | |
78 | INDIRECT_CALLABLE_DECLARE(int | |
79 | udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
80 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)); | |
81 | #endif | |
82 | ||
83 | #if defined(CONFIG_IP_VS_PROTO_TCP) && defined(CONFIG_IP_VS_PROTO_UDP) | |
84 | #define SNAT_CALL(f, ...) \ | |
85 | INDIRECT_CALL_2(f, tcp_snat_handler, udp_snat_handler, __VA_ARGS__) | |
86 | #elif defined(CONFIG_IP_VS_PROTO_TCP) | |
87 | #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, tcp_snat_handler, __VA_ARGS__) | |
88 | #elif defined(CONFIG_IP_VS_PROTO_UDP) | |
89 | #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, udp_snat_handler, __VA_ARGS__) | |
90 | #else | |
91 | #define SNAT_CALL(f, ...) f(__VA_ARGS__) | |
92 | #endif | |
93 | ||
c7d03a00 | 94 | static unsigned int ip_vs_net_id __read_mostly; |
61b1ab45 HS |
95 | /* netns cnt used for uniqueness */ |
96 | static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0); | |
1da177e4 LT |
97 | |
98 | /* ID used in ICMP lookups */ | |
99 | #define icmp_id(icmph) (((icmph)->un).echo.id) | |
2a3b791e | 100 | #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier) |
1da177e4 | 101 | |
95c96174 | 102 | const char *ip_vs_proto_name(unsigned int proto) |
1da177e4 LT |
103 | { |
104 | static char buf[20]; | |
105 | ||
106 | switch (proto) { | |
107 | case IPPROTO_IP: | |
108 | return "IP"; | |
109 | case IPPROTO_UDP: | |
110 | return "UDP"; | |
111 | case IPPROTO_TCP: | |
112 | return "TCP"; | |
2906f66a VMR |
113 | case IPPROTO_SCTP: |
114 | return "SCTP"; | |
1da177e4 LT |
115 | case IPPROTO_ICMP: |
116 | return "ICMP"; | |
2a3b791e JV |
117 | #ifdef CONFIG_IP_VS_IPV6 |
118 | case IPPROTO_ICMPV6: | |
119 | return "ICMPv6"; | |
120 | #endif | |
1da177e4 | 121 | default: |
1404c3ab | 122 | sprintf(buf, "IP_%u", proto); |
1da177e4 LT |
123 | return buf; |
124 | } | |
125 | } | |
126 | ||
127 | void ip_vs_init_hash_table(struct list_head *table, int rows) | |
128 | { | |
129 | while (--rows >= 0) | |
130 | INIT_LIST_HEAD(&table[rows]); | |
131 | } | |
132 | ||
133 | static inline void | |
134 | ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
135 | { | |
136 | struct ip_vs_dest *dest = cp->dest; | |
7c08d78e | 137 | struct netns_ipvs *ipvs = cp->ipvs; |
b17fc996 | 138 | |
1da177e4 | 139 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
b17fc996 | 140 | struct ip_vs_cpu_stats *s; |
bcbde4c0 | 141 | struct ip_vs_service *svc; |
b17fc996 | 142 | |
d5e032fc JA |
143 | local_bh_disable(); |
144 | ||
b17fc996 | 145 | s = this_cpu_ptr(dest->stats.cpustats); |
b17fc996 | 146 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
147 | s->cnt.inpkts++; |
148 | s->cnt.inbytes += skb->len; | |
b17fc996 HS |
149 | u64_stats_update_end(&s->syncp); |
150 | ||
bcbde4c0 JA |
151 | svc = rcu_dereference(dest->svc); |
152 | s = this_cpu_ptr(svc->stats.cpustats); | |
b17fc996 | 153 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
154 | s->cnt.inpkts++; |
155 | s->cnt.inbytes += skb->len; | |
b17fc996 HS |
156 | u64_stats_update_end(&s->syncp); |
157 | ||
2a0751af | 158 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
b17fc996 | 159 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
160 | s->cnt.inpkts++; |
161 | s->cnt.inbytes += skb->len; | |
b17fc996 | 162 | u64_stats_update_end(&s->syncp); |
d5e032fc JA |
163 | |
164 | local_bh_enable(); | |
1da177e4 LT |
165 | } |
166 | } | |
167 | ||
168 | ||
169 | static inline void | |
170 | ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
171 | { | |
172 | struct ip_vs_dest *dest = cp->dest; | |
7c08d78e | 173 | struct netns_ipvs *ipvs = cp->ipvs; |
b17fc996 | 174 | |
1da177e4 | 175 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
b17fc996 | 176 | struct ip_vs_cpu_stats *s; |
bcbde4c0 | 177 | struct ip_vs_service *svc; |
b17fc996 | 178 | |
d5e032fc JA |
179 | local_bh_disable(); |
180 | ||
b17fc996 | 181 | s = this_cpu_ptr(dest->stats.cpustats); |
b17fc996 | 182 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
183 | s->cnt.outpkts++; |
184 | s->cnt.outbytes += skb->len; | |
b17fc996 HS |
185 | u64_stats_update_end(&s->syncp); |
186 | ||
bcbde4c0 JA |
187 | svc = rcu_dereference(dest->svc); |
188 | s = this_cpu_ptr(svc->stats.cpustats); | |
b17fc996 | 189 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
190 | s->cnt.outpkts++; |
191 | s->cnt.outbytes += skb->len; | |
b17fc996 HS |
192 | u64_stats_update_end(&s->syncp); |
193 | ||
2a0751af | 194 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
b17fc996 | 195 | u64_stats_update_begin(&s->syncp); |
cd67cd5e JA |
196 | s->cnt.outpkts++; |
197 | s->cnt.outbytes += skb->len; | |
b17fc996 | 198 | u64_stats_update_end(&s->syncp); |
d5e032fc JA |
199 | |
200 | local_bh_enable(); | |
1da177e4 LT |
201 | } |
202 | } | |
203 | ||
204 | ||
205 | static inline void | |
206 | ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc) | |
207 | { | |
3109d2f2 | 208 | struct netns_ipvs *ipvs = svc->ipvs; |
b17fc996 | 209 | struct ip_vs_cpu_stats *s; |
1da177e4 | 210 | |
d5e032fc JA |
211 | local_bh_disable(); |
212 | ||
b17fc996 | 213 | s = this_cpu_ptr(cp->dest->stats.cpustats); |
cd67cd5e JA |
214 | u64_stats_update_begin(&s->syncp); |
215 | s->cnt.conns++; | |
216 | u64_stats_update_end(&s->syncp); | |
1da177e4 | 217 | |
b17fc996 | 218 | s = this_cpu_ptr(svc->stats.cpustats); |
cd67cd5e JA |
219 | u64_stats_update_begin(&s->syncp); |
220 | s->cnt.conns++; | |
221 | u64_stats_update_end(&s->syncp); | |
b17fc996 | 222 | |
2a0751af | 223 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
cd67cd5e JA |
224 | u64_stats_update_begin(&s->syncp); |
225 | s->cnt.conns++; | |
226 | u64_stats_update_end(&s->syncp); | |
d5e032fc JA |
227 | |
228 | local_bh_enable(); | |
1da177e4 LT |
229 | } |
230 | ||
231 | ||
4a516f11 | 232 | static inline void |
1da177e4 LT |
233 | ip_vs_set_state(struct ip_vs_conn *cp, int direction, |
234 | const struct sk_buff *skb, | |
9330419d | 235 | struct ip_vs_proto_data *pd) |
1da177e4 | 236 | { |
4a516f11 SH |
237 | if (likely(pd->pp->state_transition)) |
238 | pd->pp->state_transition(cp, direction, skb, pd); | |
1da177e4 LT |
239 | } |
240 | ||
a5959d53 | 241 | static inline int |
85999283 SH |
242 | ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc, |
243 | struct sk_buff *skb, int protocol, | |
244 | const union nf_inet_addr *caddr, __be16 cport, | |
245 | const union nf_inet_addr *vaddr, __be16 vport, | |
246 | struct ip_vs_conn_param *p) | |
247 | { | |
3109d2f2 | 248 | ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr, |
6e67e586 | 249 | vport, p); |
ceec4c38 | 250 | p->pe = rcu_dereference(svc->pe); |
85999283 | 251 | if (p->pe && p->pe->fill_param) |
a5959d53 HS |
252 | return p->pe->fill_param(p, skb); |
253 | ||
254 | return 0; | |
85999283 | 255 | } |
1da177e4 | 256 | |
1da177e4 LT |
257 | /* |
258 | * IPVS persistent scheduling function | |
259 | * It creates a connection entry according to its template if exists, | |
260 | * or selects a server and creates a connection entry plus a template. | |
261 | * Locking: we are svc user (svc->refcnt), so we hold all dests too | |
262 | * Protocols supported: TCP, UDP | |
263 | */ | |
264 | static struct ip_vs_conn * | |
265 | ip_vs_sched_persist(struct ip_vs_service *svc, | |
d4383f04 JDB |
266 | struct sk_buff *skb, __be16 src_port, __be16 dst_port, |
267 | int *ignored, struct ip_vs_iphdr *iph) | |
1da177e4 LT |
268 | { |
269 | struct ip_vs_conn *cp = NULL; | |
1da177e4 LT |
270 | struct ip_vs_dest *dest; |
271 | struct ip_vs_conn *ct; | |
5b57a98c | 272 | __be16 dport = 0; /* destination port to forward */ |
3575792e | 273 | unsigned int flags; |
f11017ec | 274 | struct ip_vs_conn_param param; |
e0aac52e | 275 | const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) }; |
28364a59 JV |
276 | union nf_inet_addr snet; /* source network of the client, |
277 | after masking */ | |
ee78378f AG |
278 | const union nf_inet_addr *src_addr, *dst_addr; |
279 | ||
280 | if (likely(!ip_vs_iph_inverse(iph))) { | |
281 | src_addr = &iph->saddr; | |
282 | dst_addr = &iph->daddr; | |
283 | } else { | |
284 | src_addr = &iph->daddr; | |
285 | dst_addr = &iph->saddr; | |
286 | } | |
287 | ||
cd17f9ed | 288 | |
1da177e4 | 289 | /* Mask saddr with the netmask to adjust template granularity */ |
cd17f9ed JV |
290 | #ifdef CONFIG_IP_VS_IPV6 |
291 | if (svc->af == AF_INET6) | |
ee78378f | 292 | ipv6_addr_prefix(&snet.in6, &src_addr->in6, |
0a925864 | 293 | (__force __u32) svc->netmask); |
cd17f9ed JV |
294 | else |
295 | #endif | |
ee78378f | 296 | snet.ip = src_addr->ip & svc->netmask; |
1da177e4 | 297 | |
cd17f9ed JV |
298 | IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u " |
299 | "mnet %s\n", | |
ee78378f AG |
300 | IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port), |
301 | IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port), | |
cd17f9ed | 302 | IP_VS_DBG_ADDR(svc->af, &snet)); |
1da177e4 LT |
303 | |
304 | /* | |
305 | * As far as we know, FTP is a very complicated network protocol, and | |
306 | * it uses control connection and data connections. For active FTP, | |
307 | * FTP server initialize data connection to the client, its source port | |
308 | * is often 20. For passive FTP, FTP server tells the clients the port | |
309 | * that it passively listens to, and the client issues the data | |
310 | * connection. In the tunneling or direct routing mode, the load | |
311 | * balancer is on the client-to-server half of connection, the port | |
312 | * number is unknown to the load balancer. So, a conn template like | |
313 | * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP | |
314 | * service, and a template like <caddr, 0, vaddr, vport, daddr, dport> | |
315 | * is created for other persistent services. | |
316 | */ | |
5b57a98c | 317 | { |
d4383f04 | 318 | int protocol = iph->protocol; |
ee78378f | 319 | const union nf_inet_addr *vaddr = dst_addr; |
f11017ec SH |
320 | __be16 vport = 0; |
321 | ||
ce144f24 | 322 | if (dst_port == svc->port) { |
5b57a98c SH |
323 | /* non-FTP template: |
324 | * <protocol, caddr, 0, vaddr, vport, daddr, dport> | |
325 | * FTP template: | |
326 | * <protocol, caddr, 0, vaddr, 0, daddr, 0> | |
1da177e4 LT |
327 | */ |
328 | if (svc->port != FTPPORT) | |
ce144f24 | 329 | vport = dst_port; |
1da177e4 | 330 | } else { |
5b57a98c SH |
331 | /* Note: persistent fwmark-based services and |
332 | * persistent port zero service are handled here. | |
333 | * fwmark template: | |
334 | * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0> | |
335 | * port zero template: | |
336 | * <protocol,caddr,0,vaddr,0,daddr,0> | |
1da177e4 | 337 | */ |
28364a59 | 338 | if (svc->fwmark) { |
5b57a98c SH |
339 | protocol = IPPROTO_IP; |
340 | vaddr = &fwmark; | |
341 | } | |
1da177e4 | 342 | } |
a5959d53 HS |
343 | /* return *ignored = -1 so NF_DROP can be used */ |
344 | if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0, | |
345 | vaddr, vport, ¶m) < 0) { | |
346 | *ignored = -1; | |
347 | return NULL; | |
348 | } | |
1da177e4 LT |
349 | } |
350 | ||
5b57a98c | 351 | /* Check if a template already exists */ |
f11017ec | 352 | ct = ip_vs_ct_in_get(¶m); |
3ec10d3a | 353 | if (!ct || !ip_vs_check_template(ct, NULL)) { |
ceec4c38 JA |
354 | struct ip_vs_scheduler *sched; |
355 | ||
a5959d53 HS |
356 | /* |
357 | * No template found or the dest of the connection | |
5b57a98c | 358 | * template is not available. |
a5959d53 | 359 | * return *ignored=0 i.e. ICMP and NF_DROP |
5b57a98c | 360 | */ |
ceec4c38 | 361 | sched = rcu_dereference(svc->scheduler); |
05f00505 JA |
362 | if (sched) { |
363 | /* read svc->sched_data after svc->scheduler */ | |
364 | smp_rmb(); | |
365 | dest = sched->schedule(svc, skb, iph); | |
366 | } else { | |
367 | dest = NULL; | |
368 | } | |
5b57a98c SH |
369 | if (!dest) { |
370 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); | |
85999283 | 371 | kfree(param.pe_data); |
a5959d53 | 372 | *ignored = 0; |
5b57a98c SH |
373 | return NULL; |
374 | } | |
375 | ||
ce144f24 | 376 | if (dst_port == svc->port && svc->port != FTPPORT) |
5b57a98c SH |
377 | dport = dest->port; |
378 | ||
85999283 SH |
379 | /* Create a template |
380 | * This adds param.pe_data to the template, | |
381 | * and thus param.pe_data will be destroyed | |
382 | * when the template expires */ | |
ba38528a | 383 | ct = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, |
0e051e68 | 384 | IP_VS_CONN_F_TEMPLATE, dest, skb->mark); |
85999283 SH |
385 | if (ct == NULL) { |
386 | kfree(param.pe_data); | |
a5959d53 | 387 | *ignored = -1; |
5b57a98c | 388 | return NULL; |
85999283 | 389 | } |
5b57a98c SH |
390 | |
391 | ct->timeout = svc->timeout; | |
85999283 | 392 | } else { |
5b57a98c SH |
393 | /* set destination with the found template */ |
394 | dest = ct->dest; | |
85999283 SH |
395 | kfree(param.pe_data); |
396 | } | |
5b57a98c | 397 | |
ce144f24 | 398 | dport = dst_port; |
5b57a98c SH |
399 | if (dport == svc->port && dest->port) |
400 | dport = dest->port; | |
401 | ||
26ec037f | 402 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
d4383f04 | 403 | && iph->protocol == IPPROTO_UDP) ? |
26ec037f NC |
404 | IP_VS_CONN_F_ONE_PACKET : 0; |
405 | ||
1da177e4 LT |
406 | /* |
407 | * Create a new connection according to the template | |
408 | */ | |
3109d2f2 | 409 | ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr, |
ee78378f | 410 | src_port, dst_addr, dst_port, ¶m); |
ce144f24 | 411 | |
ba38528a AG |
412 | cp = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, flags, dest, |
413 | skb->mark); | |
1da177e4 LT |
414 | if (cp == NULL) { |
415 | ip_vs_conn_put(ct); | |
a5959d53 | 416 | *ignored = -1; |
1da177e4 LT |
417 | return NULL; |
418 | } | |
419 | ||
420 | /* | |
421 | * Add its control | |
422 | */ | |
423 | ip_vs_control_add(cp, ct); | |
424 | ip_vs_conn_put(ct); | |
425 | ||
426 | ip_vs_conn_stats(cp, svc); | |
427 | return cp; | |
428 | } | |
429 | ||
430 | ||
431 | /* | |
432 | * IPVS main scheduling function | |
433 | * It selects a server according to the virtual service, and | |
434 | * creates a connection entry. | |
435 | * Protocols supported: TCP, UDP | |
a5959d53 HS |
436 | * |
437 | * Usage of *ignored | |
438 | * | |
439 | * 1 : protocol tried to schedule (eg. on SYN), found svc but the | |
440 | * svc/scheduler decides that this packet should be accepted with | |
441 | * NF_ACCEPT because it must not be scheduled. | |
442 | * | |
443 | * 0 : scheduler can not find destination, so try bypass or | |
444 | * return ICMP and then NF_DROP (ip_vs_leave). | |
445 | * | |
446 | * -1 : scheduler tried to schedule but fatal error occurred, eg. | |
447 | * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param | |
448 | * failure such as missing Call-ID, ENOMEM on skb_linearize | |
449 | * or pe_data. In this case we should return NF_DROP without | |
450 | * any attempts to send ICMP with ip_vs_leave. | |
1da177e4 LT |
451 | */ |
452 | struct ip_vs_conn * | |
190ecd27 | 453 | ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, |
d4383f04 JDB |
454 | struct ip_vs_proto_data *pd, int *ignored, |
455 | struct ip_vs_iphdr *iph) | |
1da177e4 | 456 | { |
9330419d | 457 | struct ip_vs_protocol *pp = pd->pp; |
1da177e4 | 458 | struct ip_vs_conn *cp = NULL; |
ceec4c38 | 459 | struct ip_vs_scheduler *sched; |
1da177e4 | 460 | struct ip_vs_dest *dest; |
ee78378f AG |
461 | __be16 _ports[2], *pptr, cport, vport; |
462 | const void *caddr, *vaddr; | |
3575792e | 463 | unsigned int flags; |
1da177e4 | 464 | |
190ecd27 | 465 | *ignored = 1; |
2f74713d JDB |
466 | /* |
467 | * IPv6 frags, only the first hit here. | |
468 | */ | |
6b3d9330 | 469 | pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports); |
1da177e4 LT |
470 | if (pptr == NULL) |
471 | return NULL; | |
472 | ||
ee78378f AG |
473 | if (likely(!ip_vs_iph_inverse(iph))) { |
474 | cport = pptr[0]; | |
475 | caddr = &iph->saddr; | |
476 | vport = pptr[1]; | |
477 | vaddr = &iph->daddr; | |
478 | } else { | |
479 | cport = pptr[1]; | |
480 | caddr = &iph->daddr; | |
481 | vport = pptr[0]; | |
482 | vaddr = &iph->saddr; | |
483 | } | |
484 | ||
190ecd27 JA |
485 | /* |
486 | * FTPDATA needs this check when using local real server. | |
487 | * Never schedule Active FTPDATA connections from real server. | |
488 | * For LVS-NAT they must be already created. For other methods | |
489 | * with persistence the connection is created on SYN+ACK. | |
490 | */ | |
ee78378f | 491 | if (cport == FTPDATA) { |
b0e010c5 | 492 | IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off, |
0d79641a | 493 | "Not scheduling FTPDATA"); |
190ecd27 JA |
494 | return NULL; |
495 | } | |
496 | ||
497 | /* | |
a5959d53 | 498 | * Do not schedule replies from local real server. |
190ecd27 | 499 | */ |
802c41ad AG |
500 | if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) { |
501 | iph->hdr_flags ^= IP_VS_HDR_INVERSE; | |
6ecd7548 MC |
502 | cp = INDIRECT_CALL_1(pp->conn_in_get, |
503 | ip_vs_conn_in_get_proto, svc->ipvs, | |
504 | svc->af, skb, iph); | |
802c41ad AG |
505 | iph->hdr_flags ^= IP_VS_HDR_INVERSE; |
506 | ||
507 | if (cp) { | |
508 | IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off, | |
509 | "Not scheduling reply for existing" | |
510 | " connection"); | |
511 | __ip_vs_conn_put(cp); | |
512 | return NULL; | |
513 | } | |
190ecd27 JA |
514 | } |
515 | ||
1da177e4 LT |
516 | /* |
517 | * Persistent service | |
518 | */ | |
a5959d53 | 519 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) |
ee78378f | 520 | return ip_vs_sched_persist(svc, skb, cport, vport, ignored, |
d4383f04 | 521 | iph); |
a5959d53 HS |
522 | |
523 | *ignored = 0; | |
1da177e4 LT |
524 | |
525 | /* | |
526 | * Non-persistent service | |
527 | */ | |
ee78378f | 528 | if (!svc->fwmark && vport != svc->port) { |
1da177e4 | 529 | if (!svc->port) |
1e3e238e HE |
530 | pr_err("Schedule: port zero only supported " |
531 | "in persistent services, " | |
532 | "check your ipvs configuration\n"); | |
1da177e4 LT |
533 | return NULL; |
534 | } | |
535 | ||
ceec4c38 | 536 | sched = rcu_dereference(svc->scheduler); |
05f00505 JA |
537 | if (sched) { |
538 | /* read svc->sched_data after svc->scheduler */ | |
539 | smp_rmb(); | |
540 | dest = sched->schedule(svc, skb, iph); | |
541 | } else { | |
542 | dest = NULL; | |
543 | } | |
1da177e4 LT |
544 | if (dest == NULL) { |
545 | IP_VS_DBG(1, "Schedule: no dest found.\n"); | |
546 | return NULL; | |
547 | } | |
548 | ||
26ec037f | 549 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
d4383f04 | 550 | && iph->protocol == IPPROTO_UDP) ? |
26ec037f NC |
551 | IP_VS_CONN_F_ONE_PACKET : 0; |
552 | ||
1da177e4 LT |
553 | /* |
554 | * Create a connection entry. | |
555 | */ | |
f11017ec SH |
556 | { |
557 | struct ip_vs_conn_param p; | |
6e67e586 | 558 | |
3109d2f2 | 559 | ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, |
ee78378f | 560 | caddr, cport, vaddr, vport, &p); |
ba38528a | 561 | cp = ip_vs_conn_new(&p, dest->af, &dest->addr, |
ee78378f | 562 | dest->port ? dest->port : vport, |
0e051e68 | 563 | flags, dest, skb->mark); |
a5959d53 HS |
564 | if (!cp) { |
565 | *ignored = -1; | |
f11017ec | 566 | return NULL; |
a5959d53 | 567 | } |
f11017ec | 568 | } |
1da177e4 | 569 | |
cd17f9ed JV |
570 | IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u " |
571 | "d:%s:%u conn->flags:%X conn->refcnt:%d\n", | |
572 | ip_vs_fwd_tag(cp), | |
f18ae720 JA |
573 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), |
574 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
575 | IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport), | |
b54ab92b | 576 | cp->flags, refcount_read(&cp->refcnt)); |
1da177e4 LT |
577 | |
578 | ip_vs_conn_stats(cp, svc); | |
579 | return cp; | |
580 | } | |
581 | ||
0b729021 AG |
582 | static inline int ip_vs_addr_is_unicast(struct net *net, int af, |
583 | union nf_inet_addr *addr) | |
584 | { | |
585 | #ifdef CONFIG_IP_VS_IPV6 | |
586 | if (af == AF_INET6) | |
587 | return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST; | |
588 | #endif | |
589 | return (inet_addr_type(net, addr->ip) == RTN_UNICAST); | |
590 | } | |
1da177e4 LT |
591 | |
592 | /* | |
593 | * Pass or drop the packet. | |
594 | * Called by ip_vs_in, when the virtual service is available but | |
595 | * no destination is available for a new connection. | |
596 | */ | |
597 | int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, | |
d4383f04 | 598 | struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph) |
1da177e4 | 599 | { |
0b729021 | 600 | __be16 _ports[2], *pptr, dport; |
51efbcbb EB |
601 | struct netns_ipvs *ipvs = svc->ipvs; |
602 | struct net *net = ipvs->net; | |
9330419d | 603 | |
6b3d9330 | 604 | pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports); |
0b729021 | 605 | if (!pptr) |
1da177e4 | 606 | return NF_DROP; |
0b729021 | 607 | dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0]; |
a7a86b86 | 608 | |
1da177e4 | 609 | /* if it is fwmark-based service, the cache_bypass sysctl is up |
2a3b791e | 610 | and the destination is a non-local unicast, then create |
1da177e4 | 611 | a cache_bypass connection entry */ |
57032948 | 612 | if (sysctl_cache_bypass(ipvs) && svc->fwmark && |
0b729021 AG |
613 | !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) && |
614 | ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) { | |
ad542ced | 615 | int ret; |
1da177e4 | 616 | struct ip_vs_conn *cp; |
3575792e | 617 | unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && |
d4383f04 | 618 | iph->protocol == IPPROTO_UDP) ? |
3575792e | 619 | IP_VS_CONN_F_ONE_PACKET : 0; |
7e59b3fe | 620 | union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } }; |
1da177e4 | 621 | |
1da177e4 | 622 | /* create a new connection entry */ |
1e3e238e | 623 | IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__); |
f11017ec SH |
624 | { |
625 | struct ip_vs_conn_param p; | |
3109d2f2 | 626 | ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, |
d4383f04 JDB |
627 | &iph->saddr, pptr[0], |
628 | &iph->daddr, pptr[1], &p); | |
ba38528a | 629 | cp = ip_vs_conn_new(&p, svc->af, &daddr, 0, |
f11017ec | 630 | IP_VS_CONN_F_BYPASS | flags, |
0e051e68 | 631 | NULL, skb->mark); |
f11017ec SH |
632 | if (!cp) |
633 | return NF_DROP; | |
634 | } | |
1da177e4 LT |
635 | |
636 | /* statistics */ | |
637 | ip_vs_in_stats(cp, skb); | |
638 | ||
639 | /* set state */ | |
4a516f11 | 640 | ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 LT |
641 | |
642 | /* transmit the first SYN packet */ | |
d4383f04 | 643 | ret = cp->packet_xmit(skb, cp, pd->pp, iph); |
1da177e4 LT |
644 | /* do not touch skb anymore */ |
645 | ||
698e2a8d MA |
646 | if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control) |
647 | atomic_inc(&cp->control->in_pkts); | |
648 | else | |
649 | atomic_inc(&cp->in_pkts); | |
1da177e4 LT |
650 | ip_vs_conn_put(cp); |
651 | return ret; | |
652 | } | |
653 | ||
654 | /* | |
655 | * When the virtual ftp service is presented, packets destined | |
656 | * for other services on the VIP may get here (except services | |
657 | * listed in the ipvs table), pass the packets, because it is | |
658 | * not ipvs job to decide to drop the packets. | |
659 | */ | |
0b729021 | 660 | if (svc->port == FTPPORT && dport != FTPPORT) |
1da177e4 | 661 | return NF_ACCEPT; |
0b729021 AG |
662 | |
663 | if (unlikely(ip_vs_iph_icmp(iph))) | |
664 | return NF_DROP; | |
1da177e4 LT |
665 | |
666 | /* | |
667 | * Notify the client that the destination is unreachable, and | |
668 | * release the socket buffer. | |
669 | * Since it is in IP layer, the TCP socket is not actually | |
670 | * created, the TCP RST packet cannot be sent, instead that | |
671 | * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ | |
672 | */ | |
2a3b791e | 673 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f | 674 | if (svc->af == AF_INET6) { |
57032948 EB |
675 | if (!skb->dev) |
676 | skb->dev = net->loopback_dev; | |
3ffe533c | 677 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
cb59155f | 678 | } else |
2a3b791e JV |
679 | #endif |
680 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | |
681 | ||
1da177e4 LT |
682 | return NF_DROP; |
683 | } | |
684 | ||
84b3cee3 SH |
685 | #ifdef CONFIG_SYSCTL |
686 | ||
a43d1a6b | 687 | static int sysctl_snat_reroute(struct netns_ipvs *ipvs) |
84b3cee3 | 688 | { |
84b3cee3 SH |
689 | return ipvs->sysctl_snat_reroute; |
690 | } | |
691 | ||
2300f045 | 692 | static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) |
0cfa558e | 693 | { |
0cfa558e SH |
694 | return ipvs->sysctl_nat_icmp_send; |
695 | } | |
696 | ||
71a8ab6c SH |
697 | static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) |
698 | { | |
699 | return ipvs->sysctl_expire_nodest_conn; | |
700 | } | |
701 | ||
84b3cee3 SH |
702 | #else |
703 | ||
a43d1a6b | 704 | static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; } |
2300f045 | 705 | static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; } |
71a8ab6c | 706 | static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; } |
84b3cee3 SH |
707 | |
708 | #endif | |
709 | ||
b1550f22 | 710 | __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset) |
1da177e4 | 711 | { |
d3bc23e7 | 712 | return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0)); |
1da177e4 LT |
713 | } |
714 | ||
1ca5bb54 JA |
715 | static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum) |
716 | { | |
717 | if (NF_INET_LOCAL_IN == hooknum) | |
718 | return IP_DEFRAG_VS_IN; | |
719 | if (NF_INET_FORWARD == hooknum) | |
720 | return IP_DEFRAG_VS_FWD; | |
721 | return IP_DEFRAG_VS_OUT; | |
722 | } | |
723 | ||
57781c1c EB |
724 | static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs, |
725 | struct sk_buff *skb, u_int32_t user) | |
1da177e4 | 726 | { |
ac69269a | 727 | int err; |
776c729e | 728 | |
ac69269a | 729 | local_bh_disable(); |
19bcf9f2 | 730 | err = ip_defrag(ipvs->net, skb, user); |
ac69269a | 731 | local_bh_enable(); |
776c729e | 732 | if (!err) |
eddc9ec5 | 733 | ip_send_check(ip_hdr(skb)); |
776c729e HX |
734 | |
735 | return err; | |
1da177e4 LT |
736 | } |
737 | ||
a43d1a6b EB |
738 | static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af, |
739 | struct sk_buff *skb, unsigned int hooknum) | |
ba4fd7e9 | 740 | { |
a43d1a6b | 741 | if (!sysctl_snat_reroute(ipvs)) |
579eb62a JA |
742 | return 0; |
743 | /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */ | |
744 | if (NF_INET_LOCAL_IN == hooknum) | |
745 | return 0; | |
ba4fd7e9 SH |
746 | #ifdef CONFIG_IP_VS_IPV6 |
747 | if (af == AF_INET6) { | |
579eb62a JA |
748 | struct dst_entry *dst = skb_dst(skb); |
749 | ||
750 | if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) && | |
5f5d74d7 | 751 | ip6_route_me_harder(ipvs->net, skb) != 0) |
ba4fd7e9 SH |
752 | return 1; |
753 | } else | |
754 | #endif | |
579eb62a | 755 | if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) && |
e45f5066 | 756 | ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0) |
ba4fd7e9 SH |
757 | return 1; |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
1da177e4 LT |
762 | /* |
763 | * Packet has been made sufficiently writable in caller | |
764 | * - inout: 1=in->out, 0=out->in | |
765 | */ | |
766 | void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
767 | struct ip_vs_conn *cp, int inout) | |
768 | { | |
eddc9ec5 | 769 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 770 | unsigned int icmp_offset = iph->ihl*4; |
d56f90a7 ACM |
771 | struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) + |
772 | icmp_offset); | |
1da177e4 LT |
773 | struct iphdr *ciph = (struct iphdr *)(icmph + 1); |
774 | ||
775 | if (inout) { | |
e7ade46a | 776 | iph->saddr = cp->vaddr.ip; |
1da177e4 | 777 | ip_send_check(iph); |
e7ade46a | 778 | ciph->daddr = cp->vaddr.ip; |
1da177e4 LT |
779 | ip_send_check(ciph); |
780 | } else { | |
e7ade46a | 781 | iph->daddr = cp->daddr.ip; |
1da177e4 | 782 | ip_send_check(iph); |
e7ade46a | 783 | ciph->saddr = cp->daddr.ip; |
1da177e4 LT |
784 | ip_send_check(ciph); |
785 | } | |
786 | ||
2906f66a VMR |
787 | /* the TCP/UDP/SCTP port */ |
788 | if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol || | |
789 | IPPROTO_SCTP == ciph->protocol) { | |
014d730d | 790 | __be16 *ports = (void *)ciph + ciph->ihl*4; |
1da177e4 LT |
791 | |
792 | if (inout) | |
793 | ports[1] = cp->vport; | |
794 | else | |
795 | ports[0] = cp->dport; | |
796 | } | |
797 | ||
798 | /* And finally the ICMP checksum */ | |
799 | icmph->checksum = 0; | |
800 | icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset); | |
801 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
802 | ||
803 | if (inout) | |
0d79641a | 804 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
805 | "Forwarding altered outgoing ICMP"); |
806 | else | |
0d79641a | 807 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
808 | "Forwarding altered incoming ICMP"); |
809 | } | |
810 | ||
b3cdd2a7 JV |
811 | #ifdef CONFIG_IP_VS_IPV6 |
812 | void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
813 | struct ip_vs_conn *cp, int inout) | |
814 | { | |
815 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
63dca2c0 JDB |
816 | unsigned int icmp_offset = 0; |
817 | unsigned int offs = 0; /* header offset*/ | |
818 | int protocol; | |
819 | struct icmp6hdr *icmph; | |
820 | struct ipv6hdr *ciph; | |
821 | unsigned short fragoffs; | |
822 | ||
823 | ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL); | |
824 | icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset); | |
825 | offs = icmp_offset + sizeof(struct icmp6hdr); | |
826 | ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs); | |
827 | ||
828 | protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL); | |
b3cdd2a7 JV |
829 | |
830 | if (inout) { | |
831 | iph->saddr = cp->vaddr.in6; | |
832 | ciph->daddr = cp->vaddr.in6; | |
833 | } else { | |
834 | iph->daddr = cp->daddr.in6; | |
835 | ciph->saddr = cp->daddr.in6; | |
836 | } | |
837 | ||
2906f66a | 838 | /* the TCP/UDP/SCTP port */ |
63dca2c0 JDB |
839 | if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
840 | IPPROTO_SCTP == protocol)) { | |
841 | __be16 *ports = (void *)(skb_network_header(skb) + offs); | |
b3cdd2a7 | 842 | |
63dca2c0 JDB |
843 | IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__, |
844 | ntohs(inout ? ports[1] : ports[0]), | |
845 | ntohs(inout ? cp->vport : cp->dport)); | |
b3cdd2a7 JV |
846 | if (inout) |
847 | ports[1] = cp->vport; | |
848 | else | |
849 | ports[0] = cp->dport; | |
850 | } | |
851 | ||
852 | /* And finally the ICMP checksum */ | |
8870f842 SH |
853 | icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, |
854 | skb->len - icmp_offset, | |
855 | IPPROTO_ICMPV6, 0); | |
856 | skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset; | |
857 | skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); | |
858 | skb->ip_summed = CHECKSUM_PARTIAL; | |
b3cdd2a7 JV |
859 | |
860 | if (inout) | |
0d79641a JA |
861 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
862 | (void *)ciph - (void *)iph, | |
863 | "Forwarding altered outgoing ICMPv6"); | |
b3cdd2a7 | 864 | else |
0d79641a JA |
865 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
866 | (void *)ciph - (void *)iph, | |
867 | "Forwarding altered incoming ICMPv6"); | |
b3cdd2a7 JV |
868 | } |
869 | #endif | |
870 | ||
4856c84c | 871 | /* Handle relevant response ICMP messages - forward to the right |
6cb90db5 | 872 | * destination host. |
4856c84c | 873 | */ |
f2428ed5 SH |
874 | static int handle_response_icmp(int af, struct sk_buff *skb, |
875 | union nf_inet_addr *snet, | |
876 | __u8 protocol, struct ip_vs_conn *cp, | |
4856c84c | 877 | struct ip_vs_protocol *pp, |
579eb62a JA |
878 | unsigned int offset, unsigned int ihl, |
879 | unsigned int hooknum) | |
4856c84c MT |
880 | { |
881 | unsigned int verdict = NF_DROP; | |
882 | ||
3c5ab3f3 JA |
883 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
884 | goto ignore_cp; | |
4856c84c MT |
885 | |
886 | /* Ensure the checksum is correct */ | |
887 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { | |
888 | /* Failed checksum! */ | |
f2428ed5 SH |
889 | IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n", |
890 | IP_VS_DBG_ADDR(af, snet)); | |
4856c84c MT |
891 | goto out; |
892 | } | |
893 | ||
2906f66a VMR |
894 | if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
895 | IPPROTO_SCTP == protocol) | |
4856c84c | 896 | offset += 2 * sizeof(__u16); |
ec0974df | 897 | if (skb_ensure_writable(skb, offset)) |
4856c84c MT |
898 | goto out; |
899 | ||
f2428ed5 SH |
900 | #ifdef CONFIG_IP_VS_IPV6 |
901 | if (af == AF_INET6) | |
902 | ip_vs_nat_icmp_v6(skb, pp, cp, 1); | |
903 | else | |
904 | #endif | |
905 | ip_vs_nat_icmp(skb, pp, cp, 1); | |
4856c84c | 906 | |
a43d1a6b | 907 | if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum)) |
ba4fd7e9 | 908 | goto out; |
f5a41847 | 909 | |
4856c84c MT |
910 | /* do the statistics and put it back */ |
911 | ip_vs_out_stats(cp, skb); | |
912 | ||
cf356d69 | 913 | skb->ipvs_property = 1; |
f4bc17cd | 914 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 915 | ip_vs_notrack(skb); |
f4bc17cd JA |
916 | else |
917 | ip_vs_update_conntrack(skb, cp, 0); | |
3c5ab3f3 JA |
918 | |
919 | ignore_cp: | |
4856c84c MT |
920 | verdict = NF_ACCEPT; |
921 | ||
922 | out: | |
923 | __ip_vs_conn_put(cp); | |
924 | ||
925 | return verdict; | |
926 | } | |
927 | ||
1da177e4 LT |
928 | /* |
929 | * Handle ICMP messages in the inside-to-outside direction (outgoing). | |
4856c84c | 930 | * Find any that might be relevant, check against existing connections. |
1da177e4 | 931 | * Currently handles error types - unreachable, quench, ttl exceeded. |
1da177e4 | 932 | */ |
7b5f689a EB |
933 | static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, |
934 | int *related, unsigned int hooknum) | |
1da177e4 | 935 | { |
1da177e4 LT |
936 | struct iphdr *iph; |
937 | struct icmphdr _icmph, *ic; | |
938 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 939 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
940 | struct ip_vs_conn *cp; |
941 | struct ip_vs_protocol *pp; | |
4856c84c | 942 | unsigned int offset, ihl; |
f2428ed5 | 943 | union nf_inet_addr snet; |
1da177e4 LT |
944 | |
945 | *related = 1; | |
946 | ||
947 | /* reassemble IP fragments */ | |
56f8a75c | 948 | if (ip_is_fragment(ip_hdr(skb))) { |
57781c1c | 949 | if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 950 | return NF_STOLEN; |
1da177e4 LT |
951 | } |
952 | ||
eddc9ec5 | 953 | iph = ip_hdr(skb); |
1da177e4 LT |
954 | offset = ihl = iph->ihl * 4; |
955 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
956 | if (ic == NULL) | |
957 | return NF_DROP; | |
958 | ||
14d5e834 | 959 | IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 960 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 961 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
962 | |
963 | /* | |
964 | * Work through seeing if this is for us. | |
965 | * These checks are supposed to be in an order that means easy | |
966 | * things are checked first to speed up processing.... however | |
967 | * this means that some packets will manage to get a long way | |
968 | * down this stack and then be rejected, but that's life. | |
969 | */ | |
970 | if ((ic->type != ICMP_DEST_UNREACH) && | |
971 | (ic->type != ICMP_SOURCE_QUENCH) && | |
972 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
973 | *related = 0; | |
974 | return NF_ACCEPT; | |
975 | } | |
976 | ||
977 | /* Now find the contained IP header */ | |
978 | offset += sizeof(_icmph); | |
979 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
980 | if (cih == NULL) | |
981 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
982 | ||
983 | pp = ip_vs_proto_get(cih->protocol); | |
984 | if (!pp) | |
985 | return NF_ACCEPT; | |
986 | ||
987 | /* Is the embedded protocol header present? */ | |
4412ec49 | 988 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
989 | pp->dont_defrag)) |
990 | return NF_ACCEPT; | |
991 | ||
0d79641a JA |
992 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
993 | "Checking outgoing ICMP for"); | |
1da177e4 | 994 | |
4fd9beef | 995 | ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph); |
b0e010c5 | 996 | |
1da177e4 | 997 | /* The embedded headers contain source and dest in reverse order */ |
6ecd7548 MC |
998 | cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto, |
999 | ipvs, AF_INET, skb, &ciph); | |
1da177e4 LT |
1000 | if (!cp) |
1001 | return NF_ACCEPT; | |
1002 | ||
f2428ed5 SH |
1003 | snet.ip = iph->saddr; |
1004 | return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp, | |
579eb62a | 1005 | pp, ciph.len, ihl, hooknum); |
1da177e4 LT |
1006 | } |
1007 | ||
2a3b791e | 1008 | #ifdef CONFIG_IP_VS_IPV6 |
7b5f689a EB |
1009 | static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, |
1010 | int *related, unsigned int hooknum, | |
1011 | struct ip_vs_iphdr *ipvsh) | |
2a3b791e | 1012 | { |
2a3b791e | 1013 | struct icmp6hdr _icmph, *ic; |
63dca2c0 | 1014 | struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */ |
2a3b791e JV |
1015 | struct ip_vs_conn *cp; |
1016 | struct ip_vs_protocol *pp; | |
f2428ed5 | 1017 | union nf_inet_addr snet; |
b0e010c5 | 1018 | unsigned int offset; |
2a3b791e | 1019 | |
63dca2c0 | 1020 | *related = 1; |
6b3d9330 | 1021 | ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph); |
2a3b791e JV |
1022 | if (ic == NULL) |
1023 | return NF_DROP; | |
1024 | ||
2a3b791e JV |
1025 | /* |
1026 | * Work through seeing if this is for us. | |
1027 | * These checks are supposed to be in an order that means easy | |
1028 | * things are checked first to speed up processing.... however | |
1029 | * this means that some packets will manage to get a long way | |
1030 | * down this stack and then be rejected, but that's life. | |
1031 | */ | |
2fab8917 | 1032 | if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) { |
2a3b791e JV |
1033 | *related = 0; |
1034 | return NF_ACCEPT; | |
1035 | } | |
2f74713d JDB |
1036 | /* Fragment header that is before ICMP header tells us that: |
1037 | * it's not an error message since they can't be fragmented. | |
1038 | */ | |
e7165030 | 1039 | if (ipvsh->flags & IP6_FH_F_FRAG) |
2f74713d | 1040 | return NF_DROP; |
2a3b791e | 1041 | |
63dca2c0 JDB |
1042 | IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n", |
1043 | ic->icmp6_type, ntohs(icmpv6_id(ic)), | |
1044 | &ipvsh->saddr, &ipvsh->daddr); | |
1045 | ||
4fd9beef AG |
1046 | if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph), |
1047 | true, &ciph)) | |
2a3b791e | 1048 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
63dca2c0 JDB |
1049 | |
1050 | pp = ip_vs_proto_get(ciph.protocol); | |
2a3b791e JV |
1051 | if (!pp) |
1052 | return NF_ACCEPT; | |
1053 | ||
2a3b791e | 1054 | /* The embedded headers contain source and dest in reverse order */ |
6ecd7548 MC |
1055 | cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto, |
1056 | ipvs, AF_INET6, skb, &ciph); | |
2a3b791e JV |
1057 | if (!cp) |
1058 | return NF_ACCEPT; | |
1059 | ||
63dca2c0 | 1060 | snet.in6 = ciph.saddr.in6; |
b0e010c5 | 1061 | offset = ciph.len; |
63dca2c0 | 1062 | return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp, |
b0e010c5 | 1063 | pp, offset, sizeof(struct ipv6hdr), |
579eb62a | 1064 | hooknum); |
2a3b791e JV |
1065 | } |
1066 | #endif | |
1067 | ||
2906f66a VMR |
1068 | /* |
1069 | * Check if sctp chunc is ABORT chunk | |
1070 | */ | |
1071 | static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len) | |
1072 | { | |
922dbc5b | 1073 | struct sctp_chunkhdr *sch, schunk; |
ae146d9b XL |
1074 | sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr), |
1075 | sizeof(schunk), &schunk); | |
2906f66a VMR |
1076 | if (sch == NULL) |
1077 | return 0; | |
1078 | if (sch->type == SCTP_CID_ABORT) | |
1079 | return 1; | |
1080 | return 0; | |
1081 | } | |
1082 | ||
2a3b791e | 1083 | static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len) |
1da177e4 LT |
1084 | { |
1085 | struct tcphdr _tcph, *th; | |
1086 | ||
2a3b791e | 1087 | th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph); |
1da177e4 LT |
1088 | if (th == NULL) |
1089 | return 0; | |
1090 | return th->rst; | |
1091 | } | |
1092 | ||
dc7b3eb9 GL |
1093 | static inline bool is_new_conn(const struct sk_buff *skb, |
1094 | struct ip_vs_iphdr *iph) | |
1095 | { | |
1096 | switch (iph->protocol) { | |
1097 | case IPPROTO_TCP: { | |
1098 | struct tcphdr _tcph, *th; | |
1099 | ||
1100 | th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph); | |
1101 | if (th == NULL) | |
1102 | return false; | |
1103 | return th->syn; | |
1104 | } | |
1105 | case IPPROTO_SCTP: { | |
922dbc5b | 1106 | struct sctp_chunkhdr *sch, schunk; |
dc7b3eb9 | 1107 | |
ae146d9b | 1108 | sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr), |
dc7b3eb9 GL |
1109 | sizeof(schunk), &schunk); |
1110 | if (sch == NULL) | |
1111 | return false; | |
1112 | return sch->type == SCTP_CID_INIT; | |
1113 | } | |
1114 | default: | |
1115 | return false; | |
1116 | } | |
1117 | } | |
1118 | ||
d752c364 MRL |
1119 | static inline bool is_new_conn_expected(const struct ip_vs_conn *cp, |
1120 | int conn_reuse_mode) | |
1121 | { | |
1122 | /* Controlled (FTP DATA or persistence)? */ | |
1123 | if (cp->control) | |
1124 | return false; | |
1125 | ||
1126 | switch (cp->protocol) { | |
1127 | case IPPROTO_TCP: | |
1128 | return (cp->state == IP_VS_TCP_S_TIME_WAIT) || | |
f911b675 | 1129 | (cp->state == IP_VS_TCP_S_CLOSE) || |
d752c364 MRL |
1130 | ((conn_reuse_mode & 2) && |
1131 | (cp->state == IP_VS_TCP_S_FIN_WAIT) && | |
1132 | (cp->flags & IP_VS_CONN_F_NOOUTPUT)); | |
1133 | case IPPROTO_SCTP: | |
1134 | return cp->state == IP_VS_SCTP_S_CLOSED; | |
1135 | default: | |
1136 | return false; | |
1137 | } | |
1138 | } | |
1139 | ||
39b97223 MA |
1140 | /* Generic function to create new connections for outgoing RS packets |
1141 | * | |
1142 | * Pre-requisites for successful connection creation: | |
1143 | * 1) Virtual Service is NOT fwmark based: | |
1144 | * In fwmark-VS actual vaddr and vport are unknown to IPVS | |
1145 | * 2) Real Server and Virtual Service were NOT configured without port: | |
1146 | * This is to allow match of different VS to the same RS ip-addr | |
1147 | */ | |
1148 | struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc, | |
1149 | struct ip_vs_dest *dest, | |
1150 | struct sk_buff *skb, | |
1151 | const struct ip_vs_iphdr *iph, | |
1152 | __be16 dport, | |
1153 | __be16 cport) | |
1154 | { | |
1155 | struct ip_vs_conn_param param; | |
1156 | struct ip_vs_conn *ct = NULL, *cp = NULL; | |
1157 | const union nf_inet_addr *vaddr, *daddr, *caddr; | |
1158 | union nf_inet_addr snet; | |
1159 | __be16 vport; | |
1160 | unsigned int flags; | |
1161 | ||
1162 | EnterFunction(12); | |
1163 | vaddr = &svc->addr; | |
1164 | vport = svc->port; | |
1165 | daddr = &iph->saddr; | |
1166 | caddr = &iph->daddr; | |
1167 | ||
1168 | /* check pre-requisites are satisfied */ | |
1169 | if (svc->fwmark) | |
1170 | return NULL; | |
1171 | if (!vport || !dport) | |
1172 | return NULL; | |
1173 | ||
1174 | /* for persistent service first create connection template */ | |
1175 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) { | |
1176 | /* apply netmask the same way ingress-side does */ | |
1177 | #ifdef CONFIG_IP_VS_IPV6 | |
1178 | if (svc->af == AF_INET6) | |
1179 | ipv6_addr_prefix(&snet.in6, &caddr->in6, | |
1180 | (__force __u32)svc->netmask); | |
1181 | else | |
1182 | #endif | |
1183 | snet.ip = caddr->ip & svc->netmask; | |
1184 | /* fill params and create template if not existent */ | |
1185 | if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol, | |
1186 | &snet, 0, vaddr, | |
1187 | vport, ¶m) < 0) | |
1188 | return NULL; | |
1189 | ct = ip_vs_ct_in_get(¶m); | |
3ec10d3a MA |
1190 | /* check if template exists and points to the same dest */ |
1191 | if (!ct || !ip_vs_check_template(ct, dest)) { | |
39b97223 MA |
1192 | ct = ip_vs_conn_new(¶m, dest->af, daddr, dport, |
1193 | IP_VS_CONN_F_TEMPLATE, dest, 0); | |
1194 | if (!ct) { | |
1195 | kfree(param.pe_data); | |
1196 | return NULL; | |
1197 | } | |
1198 | ct->timeout = svc->timeout; | |
1199 | } else { | |
1200 | kfree(param.pe_data); | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | /* connection flags */ | |
1205 | flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) && | |
1206 | iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0; | |
1207 | /* create connection */ | |
1208 | ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, | |
1209 | caddr, cport, vaddr, vport, ¶m); | |
1210 | cp = ip_vs_conn_new(¶m, dest->af, daddr, dport, flags, dest, 0); | |
1211 | if (!cp) { | |
1212 | if (ct) | |
1213 | ip_vs_conn_put(ct); | |
1214 | return NULL; | |
1215 | } | |
1216 | if (ct) { | |
1217 | ip_vs_control_add(cp, ct); | |
1218 | ip_vs_conn_put(ct); | |
1219 | } | |
1220 | ip_vs_conn_stats(cp, svc); | |
1221 | ||
1222 | /* return connection (will be used to handle outgoing packet) */ | |
1223 | IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u " | |
1224 | "d:%s:%u conn->flags:%X conn->refcnt:%d\n", | |
1225 | ip_vs_fwd_tag(cp), | |
1226 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), | |
1227 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
1228 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), | |
b54ab92b | 1229 | cp->flags, refcount_read(&cp->refcnt)); |
39b97223 MA |
1230 | LeaveFunction(12); |
1231 | return cp; | |
1232 | } | |
1233 | ||
1234 | /* Handle outgoing packets which are considered requests initiated by | |
1235 | * real servers, so that subsequent responses from external client can be | |
1236 | * routed to the right real server. | |
1237 | * Used also for outgoing responses in OPS mode. | |
1238 | * | |
1239 | * Connection management is handled by persistent-engine specific callback. | |
1240 | */ | |
1241 | static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum, | |
1242 | struct netns_ipvs *ipvs, | |
1243 | int af, struct sk_buff *skb, | |
1244 | const struct ip_vs_iphdr *iph) | |
1245 | { | |
1246 | struct ip_vs_dest *dest; | |
1247 | struct ip_vs_conn *cp = NULL; | |
1248 | __be16 _ports[2], *pptr; | |
1249 | ||
1250 | if (hooknum == NF_INET_LOCAL_IN) | |
1251 | return NULL; | |
1252 | ||
1253 | pptr = frag_safe_skb_hp(skb, iph->len, | |
6b3d9330 | 1254 | sizeof(_ports), _ports); |
39b97223 MA |
1255 | if (!pptr) |
1256 | return NULL; | |
1257 | ||
39b97223 MA |
1258 | dest = ip_vs_find_real_service(ipvs, af, iph->protocol, |
1259 | &iph->saddr, pptr[0]); | |
1260 | if (dest) { | |
1261 | struct ip_vs_service *svc; | |
1262 | struct ip_vs_pe *pe; | |
1263 | ||
1264 | svc = rcu_dereference(dest->svc); | |
1265 | if (svc) { | |
1266 | pe = rcu_dereference(svc->pe); | |
1267 | if (pe && pe->conn_out) | |
1268 | cp = pe->conn_out(svc, dest, skb, iph, | |
1269 | pptr[0], pptr[1]); | |
1270 | } | |
1271 | } | |
39b97223 MA |
1272 | |
1273 | return cp; | |
1274 | } | |
1275 | ||
4856c84c | 1276 | /* Handle response packets: rewrite addresses and send away... |
4856c84c MT |
1277 | */ |
1278 | static unsigned int | |
9330419d | 1279 | handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
579eb62a JA |
1280 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph, |
1281 | unsigned int hooknum) | |
4856c84c | 1282 | { |
9330419d HS |
1283 | struct ip_vs_protocol *pp = pd->pp; |
1284 | ||
b0e010c5 | 1285 | IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet"); |
4856c84c | 1286 | |
ec0974df | 1287 | if (skb_ensure_writable(skb, iph->len)) |
4856c84c MT |
1288 | goto drop; |
1289 | ||
1290 | /* mangle the packet */ | |
6ecd7548 MC |
1291 | if (pp->snat_handler && |
1292 | !SNAT_CALL(pp->snat_handler, skb, pp, cp, iph)) | |
4856c84c MT |
1293 | goto drop; |
1294 | ||
1295 | #ifdef CONFIG_IP_VS_IPV6 | |
1296 | if (af == AF_INET6) | |
1297 | ipv6_hdr(skb)->saddr = cp->vaddr.in6; | |
1298 | else | |
1299 | #endif | |
1300 | { | |
1301 | ip_hdr(skb)->saddr = cp->vaddr.ip; | |
1302 | ip_send_check(ip_hdr(skb)); | |
1303 | } | |
1304 | ||
8a803040 JA |
1305 | /* |
1306 | * nf_iterate does not expect change in the skb->dst->dev. | |
1307 | * It looks like it is not fatal to enable this code for hooks | |
1308 | * where our handlers are at the end of the chain list and | |
1309 | * when all next handlers use skb->dst->dev and not outdev. | |
1310 | * It will definitely route properly the inout NAT traffic | |
1311 | * when multiple paths are used. | |
1312 | */ | |
1313 | ||
4856c84c MT |
1314 | /* For policy routing, packets originating from this |
1315 | * machine itself may be routed differently to packets | |
1316 | * passing through. We want this packet to be routed as | |
1317 | * if it came from this machine itself. So re-compute | |
1318 | * the routing information. | |
1319 | */ | |
a43d1a6b | 1320 | if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum)) |
ba4fd7e9 | 1321 | goto drop; |
4856c84c | 1322 | |
b0e010c5 | 1323 | IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT"); |
4856c84c MT |
1324 | |
1325 | ip_vs_out_stats(cp, skb); | |
9330419d | 1326 | ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd); |
cf356d69 | 1327 | skb->ipvs_property = 1; |
f4bc17cd | 1328 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 1329 | ip_vs_notrack(skb); |
f4bc17cd JA |
1330 | else |
1331 | ip_vs_update_conntrack(skb, cp, 0); | |
4856c84c MT |
1332 | ip_vs_conn_put(cp); |
1333 | ||
4856c84c MT |
1334 | LeaveFunction(11); |
1335 | return NF_ACCEPT; | |
1336 | ||
1337 | drop: | |
1338 | ip_vs_conn_put(cp); | |
1339 | kfree_skb(skb); | |
f4bc17cd | 1340 | LeaveFunction(11); |
4856c84c MT |
1341 | return NF_STOLEN; |
1342 | } | |
1343 | ||
1da177e4 | 1344 | /* |
4856c84c | 1345 | * Check if outgoing packet belongs to the established ip_vs_conn. |
1da177e4 LT |
1346 | */ |
1347 | static unsigned int | |
1b75097d | 1348 | ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1349 | { |
51ef348b | 1350 | struct ip_vs_iphdr iph; |
1da177e4 | 1351 | struct ip_vs_protocol *pp; |
9330419d | 1352 | struct ip_vs_proto_data *pd; |
1da177e4 | 1353 | struct ip_vs_conn *cp; |
340c78e5 | 1354 | struct sock *sk; |
1da177e4 LT |
1355 | |
1356 | EnterFunction(11); | |
1357 | ||
fc604767 | 1358 | /* Already marked as IPVS request or reply? */ |
6869c4d8 | 1359 | if (skb->ipvs_property) |
1da177e4 LT |
1360 | return NF_ACCEPT; |
1361 | ||
340c78e5 | 1362 | sk = skb_to_full_sk(skb); |
fc604767 | 1363 | /* Bad... Do not break raw sockets */ |
340c78e5 | 1364 | if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT && |
fc604767 | 1365 | af == AF_INET)) { |
fc604767 | 1366 | |
340c78e5 | 1367 | if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag) |
fc604767 JA |
1368 | return NF_ACCEPT; |
1369 | } | |
1370 | ||
1371 | if (unlikely(!skb_dst(skb))) | |
1372 | return NF_ACCEPT; | |
1373 | ||
48aed1b0 | 1374 | if (!ipvs->enable) |
7a4f0761 HS |
1375 | return NF_ACCEPT; |
1376 | ||
4fd9beef | 1377 | ip_vs_fill_iph_skb(af, skb, false, &iph); |
2a3b791e JV |
1378 | #ifdef CONFIG_IP_VS_IPV6 |
1379 | if (af == AF_INET6) { | |
1380 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 | 1381 | int related; |
7b5f689a | 1382 | int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related, |
d4383f04 | 1383 | hooknum, &iph); |
1da177e4 | 1384 | |
f5a41847 | 1385 | if (related) |
2a3b791e | 1386 | return verdict; |
2a3b791e JV |
1387 | } |
1388 | } else | |
1389 | #endif | |
1390 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 | 1391 | int related; |
7b5f689a | 1392 | int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum); |
2a3b791e | 1393 | |
f5a41847 | 1394 | if (related) |
2a3b791e | 1395 | return verdict; |
2a3b791e | 1396 | } |
1da177e4 | 1397 | |
18d6ade6 | 1398 | pd = ip_vs_proto_data_get(ipvs, iph.protocol); |
9330419d | 1399 | if (unlikely(!pd)) |
1da177e4 | 1400 | return NF_ACCEPT; |
9330419d | 1401 | pp = pd->pp; |
1da177e4 LT |
1402 | |
1403 | /* reassemble IP fragments */ | |
2a3b791e | 1404 | #ifdef CONFIG_IP_VS_IPV6 |
63dca2c0 | 1405 | if (af == AF_INET) |
2a3b791e | 1406 | #endif |
56f8a75c | 1407 | if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) { |
57781c1c | 1408 | if (ip_vs_gather_frags(ipvs, skb, |
1ca5bb54 | 1409 | ip_vs_defrag_user(hooknum))) |
2a3b791e JV |
1410 | return NF_STOLEN; |
1411 | ||
4fd9beef | 1412 | ip_vs_fill_iph_skb(AF_INET, skb, false, &iph); |
2a3b791e | 1413 | } |
1da177e4 LT |
1414 | |
1415 | /* | |
1416 | * Check if the packet belongs to an existing entry | |
1417 | */ | |
6ecd7548 MC |
1418 | cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto, |
1419 | ipvs, af, skb, &iph); | |
1da177e4 | 1420 | |
3c5ab3f3 JA |
1421 | if (likely(cp)) { |
1422 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) | |
1423 | goto ignore_cp; | |
579eb62a | 1424 | return handle_response(af, skb, pd, cp, &iph, hooknum); |
3c5ab3f3 | 1425 | } |
39b97223 MA |
1426 | |
1427 | /* Check for real-server-started requests */ | |
1428 | if (atomic_read(&ipvs->conn_out_counter)) { | |
1429 | /* Currently only for UDP: | |
1430 | * connection oriented protocols typically use | |
1431 | * ephemeral ports for outgoing connections, so | |
1432 | * related incoming responses would not match any VS | |
1433 | */ | |
1434 | if (pp->protocol == IPPROTO_UDP) { | |
1435 | cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph); | |
1436 | if (likely(cp)) | |
1437 | return handle_response(af, skb, pd, cp, &iph, | |
1438 | hooknum); | |
1439 | } | |
1440 | } | |
1441 | ||
2300f045 | 1442 | if (sysctl_nat_icmp_send(ipvs) && |
cb59155f JA |
1443 | (pp->protocol == IPPROTO_TCP || |
1444 | pp->protocol == IPPROTO_UDP || | |
1445 | pp->protocol == IPPROTO_SCTP)) { | |
1446 | __be16 _ports[2], *pptr; | |
1447 | ||
2f74713d | 1448 | pptr = frag_safe_skb_hp(skb, iph.len, |
6b3d9330 | 1449 | sizeof(_ports), _ports); |
cb59155f JA |
1450 | if (pptr == NULL) |
1451 | return NF_ACCEPT; /* Not for me */ | |
48aed1b0 | 1452 | if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr, |
276472ea | 1453 | pptr[0])) { |
cb59155f JA |
1454 | /* |
1455 | * Notify the real server: there is no | |
1456 | * existing entry if it is not RST | |
1457 | * packet or not TCP packet. | |
1458 | */ | |
1459 | if ((iph.protocol != IPPROTO_TCP && | |
1460 | iph.protocol != IPPROTO_SCTP) | |
1461 | || ((iph.protocol == IPPROTO_TCP | |
1462 | && !is_tcp_reset(skb, iph.len)) | |
1463 | || (iph.protocol == IPPROTO_SCTP | |
1464 | && !is_sctp_abort(skb, | |
1465 | iph.len)))) { | |
2a3b791e | 1466 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f | 1467 | if (af == AF_INET6) { |
cb59155f | 1468 | if (!skb->dev) |
ed1c9f0e | 1469 | skb->dev = ipvs->net->loopback_dev; |
cb59155f JA |
1470 | icmpv6_send(skb, |
1471 | ICMPV6_DEST_UNREACH, | |
1472 | ICMPV6_PORT_UNREACH, | |
1473 | 0); | |
1474 | } else | |
2a3b791e | 1475 | #endif |
cb59155f JA |
1476 | icmp_send(skb, |
1477 | ICMP_DEST_UNREACH, | |
1478 | ICMP_PORT_UNREACH, 0); | |
1479 | return NF_DROP; | |
1da177e4 LT |
1480 | } |
1481 | } | |
1da177e4 | 1482 | } |
3c5ab3f3 JA |
1483 | |
1484 | out: | |
b0e010c5 | 1485 | IP_VS_DBG_PKT(12, af, pp, skb, iph.off, |
cb59155f JA |
1486 | "ip_vs_out: packet continues traversal as normal"); |
1487 | return NF_ACCEPT; | |
3c5ab3f3 JA |
1488 | |
1489 | ignore_cp: | |
1490 | __ip_vs_conn_put(cp); | |
1491 | goto out; | |
1da177e4 LT |
1492 | } |
1493 | ||
fc604767 | 1494 | /* |
cb59155f JA |
1495 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1496 | * used only for VS/NAT. | |
fc604767 JA |
1497 | * Check if packet is reply for established ip_vs_conn. |
1498 | */ | |
1499 | static unsigned int | |
06198b34 | 1500 | ip_vs_reply4(void *priv, struct sk_buff *skb, |
238e54c9 | 1501 | const struct nf_hook_state *state) |
fc604767 | 1502 | { |
1b75097d | 1503 | return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET); |
fc604767 JA |
1504 | } |
1505 | ||
1506 | /* | |
1507 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1508 | * Check if packet is reply for established ip_vs_conn. | |
1509 | */ | |
1510 | static unsigned int | |
06198b34 | 1511 | ip_vs_local_reply4(void *priv, struct sk_buff *skb, |
238e54c9 | 1512 | const struct nf_hook_state *state) |
fc604767 | 1513 | { |
1b75097d | 1514 | return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET); |
fc604767 JA |
1515 | } |
1516 | ||
1517 | #ifdef CONFIG_IP_VS_IPV6 | |
1518 | ||
1519 | /* | |
cb59155f JA |
1520 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1521 | * used only for VS/NAT. | |
fc604767 JA |
1522 | * Check if packet is reply for established ip_vs_conn. |
1523 | */ | |
1524 | static unsigned int | |
06198b34 | 1525 | ip_vs_reply6(void *priv, struct sk_buff *skb, |
238e54c9 | 1526 | const struct nf_hook_state *state) |
fc604767 | 1527 | { |
1b75097d | 1528 | return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6); |
fc604767 JA |
1529 | } |
1530 | ||
1531 | /* | |
1532 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1533 | * Check if packet is reply for established ip_vs_conn. | |
1534 | */ | |
1535 | static unsigned int | |
06198b34 | 1536 | ip_vs_local_reply6(void *priv, struct sk_buff *skb, |
238e54c9 | 1537 | const struct nf_hook_state *state) |
fc604767 | 1538 | { |
1b75097d | 1539 | return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6); |
fc604767 JA |
1540 | } |
1541 | ||
1542 | #endif | |
1da177e4 | 1543 | |
3b5ca617 | 1544 | static unsigned int |
d8f44c33 EB |
1545 | ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, |
1546 | struct ip_vs_proto_data *pd, | |
3b5ca617 AG |
1547 | int *verdict, struct ip_vs_conn **cpp, |
1548 | struct ip_vs_iphdr *iph) | |
1549 | { | |
1550 | struct ip_vs_protocol *pp = pd->pp; | |
1551 | ||
1552 | if (!iph->fragoffs) { | |
1553 | /* No (second) fragments need to enter here, as nf_defrag_ipv6 | |
1554 | * replayed fragment zero will already have created the cp | |
1555 | */ | |
1556 | ||
1557 | /* Schedule and create new connection entry into cpp */ | |
d8f44c33 | 1558 | if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph)) |
3b5ca617 AG |
1559 | return 0; |
1560 | } | |
1561 | ||
1562 | if (unlikely(!*cpp)) { | |
1563 | /* sorry, all this trouble for a no-hit :) */ | |
1564 | IP_VS_DBG_PKT(12, af, pp, skb, iph->off, | |
1565 | "ip_vs_in: packet continues traversal as normal"); | |
098e13f5 AC |
1566 | |
1567 | /* Fragment couldn't be mapped to a conn entry */ | |
1568 | if (iph->fragoffs) | |
3b5ca617 AG |
1569 | IP_VS_DBG_PKT(7, af, pp, skb, iph->off, |
1570 | "unhandled fragment"); | |
098e13f5 | 1571 | |
3b5ca617 AG |
1572 | *verdict = NF_ACCEPT; |
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | return 1; | |
1577 | } | |
1578 | ||
508f744c JA |
1579 | /* Check the UDP tunnel and return its header length */ |
1580 | static int ipvs_udp_decap(struct netns_ipvs *ipvs, struct sk_buff *skb, | |
1581 | unsigned int offset, __u16 af, | |
1582 | const union nf_inet_addr *daddr, __u8 *proto) | |
1583 | { | |
1584 | struct udphdr _udph, *udph; | |
1585 | struct ip_vs_dest *dest; | |
1586 | ||
1587 | udph = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); | |
1588 | if (!udph) | |
1589 | goto unk; | |
1590 | offset += sizeof(struct udphdr); | |
1591 | dest = ip_vs_find_tunnel(ipvs, af, daddr, udph->dest); | |
1592 | if (!dest) | |
1593 | goto unk; | |
1594 | if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) { | |
1595 | struct guehdr _gueh, *gueh; | |
1596 | ||
1597 | gueh = skb_header_pointer(skb, offset, sizeof(_gueh), &_gueh); | |
1598 | if (!gueh) | |
1599 | goto unk; | |
1600 | if (gueh->control != 0 || gueh->version != 0) | |
1601 | goto unk; | |
1602 | /* Later we can support also IPPROTO_IPV6 */ | |
1603 | if (gueh->proto_ctype != IPPROTO_IPIP) | |
1604 | goto unk; | |
1605 | *proto = gueh->proto_ctype; | |
1606 | return sizeof(struct udphdr) + sizeof(struct guehdr) + | |
1607 | (gueh->hlen << 2); | |
1608 | } | |
1609 | ||
1610 | unk: | |
1611 | return 0; | |
1612 | } | |
1613 | ||
6aedd14b JA |
1614 | /* Check the GRE tunnel and return its header length */ |
1615 | static int ipvs_gre_decap(struct netns_ipvs *ipvs, struct sk_buff *skb, | |
1616 | unsigned int offset, __u16 af, | |
1617 | const union nf_inet_addr *daddr, __u8 *proto) | |
1618 | { | |
1619 | struct gre_base_hdr _greh, *greh; | |
1620 | struct ip_vs_dest *dest; | |
1621 | ||
1622 | greh = skb_header_pointer(skb, offset, sizeof(_greh), &_greh); | |
1623 | if (!greh) | |
1624 | goto unk; | |
1625 | dest = ip_vs_find_tunnel(ipvs, af, daddr, 0); | |
1626 | if (!dest) | |
1627 | goto unk; | |
1628 | if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) { | |
1629 | __be16 type; | |
1630 | ||
1631 | /* Only support version 0 and C (csum) */ | |
1632 | if ((greh->flags & ~GRE_CSUM) != 0) | |
1633 | goto unk; | |
1634 | type = greh->protocol; | |
1635 | /* Later we can support also IPPROTO_IPV6 */ | |
1636 | if (type != htons(ETH_P_IP)) | |
1637 | goto unk; | |
1638 | *proto = IPPROTO_IPIP; | |
1639 | return gre_calc_hlen(gre_flags_to_tnl_flags(greh->flags)); | |
1640 | } | |
1641 | ||
1642 | unk: | |
1643 | return 0; | |
1644 | } | |
1645 | ||
1da177e4 LT |
1646 | /* |
1647 | * Handle ICMP messages in the outside-to-inside direction (incoming). | |
1648 | * Find any that might be relevant, check against existing connections, | |
1649 | * forward to the right destination host if relevant. | |
1650 | * Currently handles error types - unreachable, quench, ttl exceeded. | |
1651 | */ | |
e905a9ed | 1652 | static int |
6f2bcea9 EB |
1653 | ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related, |
1654 | unsigned int hooknum) | |
1da177e4 | 1655 | { |
1da177e4 LT |
1656 | struct iphdr *iph; |
1657 | struct icmphdr _icmph, *ic; | |
1658 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 1659 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
1660 | struct ip_vs_conn *cp; |
1661 | struct ip_vs_protocol *pp; | |
9330419d | 1662 | struct ip_vs_proto_data *pd; |
f2edb9f7 | 1663 | unsigned int offset, offset2, ihl, verdict; |
6044eeff | 1664 | bool ipip, new_cp = false; |
2aa3c9f4 | 1665 | union nf_inet_addr *raddr; |
1da177e4 LT |
1666 | |
1667 | *related = 1; | |
1668 | ||
1669 | /* reassemble IP fragments */ | |
56f8a75c | 1670 | if (ip_is_fragment(ip_hdr(skb))) { |
57781c1c | 1671 | if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 1672 | return NF_STOLEN; |
1da177e4 LT |
1673 | } |
1674 | ||
eddc9ec5 | 1675 | iph = ip_hdr(skb); |
1da177e4 LT |
1676 | offset = ihl = iph->ihl * 4; |
1677 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
1678 | if (ic == NULL) | |
1679 | return NF_DROP; | |
1680 | ||
14d5e834 | 1681 | IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 1682 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 1683 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
1684 | |
1685 | /* | |
1686 | * Work through seeing if this is for us. | |
1687 | * These checks are supposed to be in an order that means easy | |
1688 | * things are checked first to speed up processing.... however | |
1689 | * this means that some packets will manage to get a long way | |
1690 | * down this stack and then be rejected, but that's life. | |
1691 | */ | |
1692 | if ((ic->type != ICMP_DEST_UNREACH) && | |
1693 | (ic->type != ICMP_SOURCE_QUENCH) && | |
1694 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
1695 | *related = 0; | |
1696 | return NF_ACCEPT; | |
1697 | } | |
1698 | ||
1699 | /* Now find the contained IP header */ | |
1700 | offset += sizeof(_icmph); | |
1701 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1702 | if (cih == NULL) | |
1703 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
2aa3c9f4 | 1704 | raddr = (union nf_inet_addr *)&cih->daddr; |
1da177e4 | 1705 | |
f2edb9f7 JA |
1706 | /* Special case for errors for IPIP packets */ |
1707 | ipip = false; | |
1708 | if (cih->protocol == IPPROTO_IPIP) { | |
2aa3c9f4 JA |
1709 | struct ip_vs_dest *dest; |
1710 | ||
f2edb9f7 JA |
1711 | if (unlikely(cih->frag_off & htons(IP_OFFSET))) |
1712 | return NF_ACCEPT; | |
1713 | /* Error for our IPIP must arrive at LOCAL_IN */ | |
1714 | if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL)) | |
1715 | return NF_ACCEPT; | |
2aa3c9f4 JA |
1716 | dest = ip_vs_find_tunnel(ipvs, AF_INET, raddr, 0); |
1717 | /* Only for known tunnel */ | |
1718 | if (!dest || dest->tun_type != IP_VS_CONN_F_TUNNEL_TYPE_IPIP) | |
1719 | return NF_ACCEPT; | |
f2edb9f7 JA |
1720 | offset += cih->ihl * 4; |
1721 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1722 | if (cih == NULL) | |
1723 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
1724 | ipip = true; | |
6aedd14b JA |
1725 | } else if ((cih->protocol == IPPROTO_UDP || /* Can be UDP encap */ |
1726 | cih->protocol == IPPROTO_GRE) && /* Can be GRE encap */ | |
508f744c JA |
1727 | /* Error for our tunnel must arrive at LOCAL_IN */ |
1728 | (skb_rtable(skb)->rt_flags & RTCF_LOCAL)) { | |
1729 | __u8 iproto; | |
1730 | int ulen; | |
1731 | ||
1732 | /* Non-first fragment has no UDP header */ | |
1733 | if (unlikely(cih->frag_off & htons(IP_OFFSET))) | |
1734 | return NF_ACCEPT; | |
1735 | offset2 = offset + cih->ihl * 4; | |
6aedd14b JA |
1736 | if (cih->protocol == IPPROTO_UDP) |
1737 | ulen = ipvs_udp_decap(ipvs, skb, offset2, AF_INET, | |
1738 | raddr, &iproto); | |
1739 | else | |
1740 | ulen = ipvs_gre_decap(ipvs, skb, offset2, AF_INET, | |
1741 | raddr, &iproto); | |
508f744c | 1742 | if (ulen > 0) { |
6aedd14b | 1743 | /* Skip IP and UDP/GRE tunnel headers */ |
508f744c JA |
1744 | offset = offset2 + ulen; |
1745 | /* Now we should be at the original IP header */ | |
1746 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), | |
1747 | &_ciph); | |
1748 | if (cih && cih->version == 4 && cih->ihl >= 5 && | |
1749 | iproto == IPPROTO_IPIP) | |
1750 | ipip = true; | |
1751 | else | |
1752 | return NF_ACCEPT; | |
1753 | } | |
f2edb9f7 JA |
1754 | } |
1755 | ||
18d6ade6 | 1756 | pd = ip_vs_proto_data_get(ipvs, cih->protocol); |
9330419d | 1757 | if (!pd) |
1da177e4 | 1758 | return NF_ACCEPT; |
9330419d | 1759 | pp = pd->pp; |
1da177e4 LT |
1760 | |
1761 | /* Is the embedded protocol header present? */ | |
4412ec49 | 1762 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
1763 | pp->dont_defrag)) |
1764 | return NF_ACCEPT; | |
1765 | ||
0d79641a JA |
1766 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
1767 | "Checking incoming ICMP for"); | |
1da177e4 | 1768 | |
f2edb9f7 | 1769 | offset2 = offset; |
4fd9beef | 1770 | ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph); |
63dca2c0 | 1771 | offset = ciph.len; |
b0e010c5 | 1772 | |
f2edb9f7 JA |
1773 | /* The embedded headers contain source and dest in reverse order. |
1774 | * For IPIP this is error for request, not for reply. | |
1775 | */ | |
6ecd7548 MC |
1776 | cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto, |
1777 | ipvs, AF_INET, skb, &ciph); | |
6044eeff AG |
1778 | |
1779 | if (!cp) { | |
1780 | int v; | |
1781 | ||
0261ea1b | 1782 | if (ipip || !sysctl_schedule_icmp(ipvs)) |
6044eeff AG |
1783 | return NF_ACCEPT; |
1784 | ||
d8f44c33 | 1785 | if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph)) |
6044eeff AG |
1786 | return v; |
1787 | new_cp = true; | |
1788 | } | |
1da177e4 LT |
1789 | |
1790 | verdict = NF_DROP; | |
1791 | ||
1792 | /* Ensure the checksum is correct */ | |
60476372 | 1793 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { |
1da177e4 | 1794 | /* Failed checksum! */ |
14d5e834 HH |
1795 | IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n", |
1796 | &iph->saddr); | |
1da177e4 LT |
1797 | goto out; |
1798 | } | |
1799 | ||
f2edb9f7 JA |
1800 | if (ipip) { |
1801 | __be32 info = ic->un.gateway; | |
f44a5f45 PC |
1802 | __u8 type = ic->type; |
1803 | __u8 code = ic->code; | |
f2edb9f7 JA |
1804 | |
1805 | /* Update the MTU */ | |
1806 | if (ic->type == ICMP_DEST_UNREACH && | |
1807 | ic->code == ICMP_FRAG_NEEDED) { | |
1808 | struct ip_vs_dest *dest = cp->dest; | |
1809 | u32 mtu = ntohs(ic->un.frag.mtu); | |
f44a5f45 | 1810 | __be16 frag_off = cih->frag_off; |
f2edb9f7 JA |
1811 | |
1812 | /* Strip outer IP and ICMP, go to IPIP header */ | |
f44a5f45 PC |
1813 | if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL) |
1814 | goto ignore_ipip; | |
f2edb9f7 JA |
1815 | offset2 -= ihl + sizeof(_icmph); |
1816 | skb_reset_network_header(skb); | |
1817 | IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n", | |
1818 | &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu); | |
d888f396 | 1819 | ipv4_update_pmtu(skb, ipvs->net, mtu, 0, 0); |
f2edb9f7 | 1820 | /* Client uses PMTUD? */ |
f44a5f45 | 1821 | if (!(frag_off & htons(IP_DF))) |
f2edb9f7 JA |
1822 | goto ignore_ipip; |
1823 | /* Prefer the resulting PMTU */ | |
1824 | if (dest) { | |
026ace06 JA |
1825 | struct ip_vs_dest_dst *dest_dst; |
1826 | ||
026ace06 JA |
1827 | dest_dst = rcu_dereference(dest->dest_dst); |
1828 | if (dest_dst) | |
1829 | mtu = dst_mtu(dest_dst->dst_cache); | |
f2edb9f7 JA |
1830 | } |
1831 | if (mtu > 68 + sizeof(struct iphdr)) | |
1832 | mtu -= sizeof(struct iphdr); | |
1833 | info = htonl(mtu); | |
1834 | } | |
1835 | /* Strip outer IP, ICMP and IPIP, go to IP header of | |
1836 | * original request. | |
1837 | */ | |
f44a5f45 PC |
1838 | if (pskb_pull(skb, offset2) == NULL) |
1839 | goto ignore_ipip; | |
f2edb9f7 JA |
1840 | skb_reset_network_header(skb); |
1841 | IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n", | |
1842 | &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, | |
f44a5f45 PC |
1843 | type, code, ntohl(info)); |
1844 | icmp_send(skb, type, code, info); | |
f2edb9f7 JA |
1845 | /* ICMP can be shorter but anyways, account it */ |
1846 | ip_vs_out_stats(cp, skb); | |
1847 | ||
1848 | ignore_ipip: | |
1849 | consume_skb(skb); | |
1850 | verdict = NF_STOLEN; | |
1851 | goto out; | |
1852 | } | |
1853 | ||
1da177e4 LT |
1854 | /* do the statistics and put it back */ |
1855 | ip_vs_in_stats(cp, skb); | |
06f3d7f9 JA |
1856 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol || |
1857 | IPPROTO_SCTP == cih->protocol) | |
1da177e4 | 1858 | offset += 2 * sizeof(__u16); |
d4383f04 | 1859 | verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph); |
1da177e4 | 1860 | |
552ad65a | 1861 | out: |
6044eeff AG |
1862 | if (likely(!new_cp)) |
1863 | __ip_vs_conn_put(cp); | |
1864 | else | |
1865 | ip_vs_conn_put(cp); | |
1da177e4 LT |
1866 | |
1867 | return verdict; | |
1868 | } | |
1869 | ||
2a3b791e | 1870 | #ifdef CONFIG_IP_VS_IPV6 |
6f2bcea9 EB |
1871 | static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, |
1872 | int *related, unsigned int hooknum, | |
1873 | struct ip_vs_iphdr *iph) | |
2a3b791e | 1874 | { |
2a3b791e | 1875 | struct icmp6hdr _icmph, *ic; |
63dca2c0 | 1876 | struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */ |
2a3b791e JV |
1877 | struct ip_vs_conn *cp; |
1878 | struct ip_vs_protocol *pp; | |
9330419d | 1879 | struct ip_vs_proto_data *pd; |
b0e010c5 | 1880 | unsigned int offset, verdict; |
6044eeff | 1881 | bool new_cp = false; |
2a3b791e | 1882 | |
63dca2c0 | 1883 | *related = 1; |
2a3b791e | 1884 | |
6b3d9330 | 1885 | ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph); |
2a3b791e JV |
1886 | if (ic == NULL) |
1887 | return NF_DROP; | |
1888 | ||
2a3b791e JV |
1889 | /* |
1890 | * Work through seeing if this is for us. | |
1891 | * These checks are supposed to be in an order that means easy | |
1892 | * things are checked first to speed up processing.... however | |
1893 | * this means that some packets will manage to get a long way | |
1894 | * down this stack and then be rejected, but that's life. | |
1895 | */ | |
2fab8917 | 1896 | if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) { |
2a3b791e JV |
1897 | *related = 0; |
1898 | return NF_ACCEPT; | |
1899 | } | |
2f74713d JDB |
1900 | /* Fragment header that is before ICMP header tells us that: |
1901 | * it's not an error message since they can't be fragmented. | |
1902 | */ | |
e7165030 | 1903 | if (iph->flags & IP6_FH_F_FRAG) |
2f74713d | 1904 | return NF_DROP; |
2a3b791e | 1905 | |
63dca2c0 JDB |
1906 | IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n", |
1907 | ic->icmp6_type, ntohs(icmpv6_id(ic)), | |
1908 | &iph->saddr, &iph->daddr); | |
1909 | ||
b0e010c5 | 1910 | offset = iph->len + sizeof(_icmph); |
4fd9beef | 1911 | if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph)) |
b0e010c5 | 1912 | return NF_ACCEPT; |
2a3b791e | 1913 | |
18d6ade6 | 1914 | pd = ip_vs_proto_data_get(ipvs, ciph.protocol); |
9330419d | 1915 | if (!pd) |
2a3b791e | 1916 | return NF_ACCEPT; |
9330419d | 1917 | pp = pd->pp; |
2a3b791e | 1918 | |
63dca2c0 JDB |
1919 | /* Cannot handle fragmented embedded protocol */ |
1920 | if (ciph.fragoffs) | |
2a3b791e JV |
1921 | return NF_ACCEPT; |
1922 | ||
b0e010c5 | 1923 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset, |
0d79641a | 1924 | "Checking incoming ICMPv6 for"); |
2a3b791e | 1925 | |
2f74713d JDB |
1926 | /* The embedded headers contain source and dest in reverse order |
1927 | * if not from localhost | |
1928 | */ | |
6ecd7548 MC |
1929 | cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto, |
1930 | ipvs, AF_INET6, skb, &ciph); | |
2f74713d | 1931 | |
6044eeff AG |
1932 | if (!cp) { |
1933 | int v; | |
1934 | ||
a47b4300 | 1935 | if (!sysctl_schedule_icmp(ipvs)) |
6044eeff AG |
1936 | return NF_ACCEPT; |
1937 | ||
d8f44c33 | 1938 | if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph)) |
6044eeff AG |
1939 | return v; |
1940 | ||
1941 | new_cp = true; | |
1942 | } | |
1943 | ||
2f74713d JDB |
1944 | /* VS/TUN, VS/DR and LOCALNODE just let it go */ |
1945 | if ((hooknum == NF_INET_LOCAL_OUT) && | |
1946 | (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) { | |
6044eeff AG |
1947 | verdict = NF_ACCEPT; |
1948 | goto out; | |
2f74713d | 1949 | } |
2a3b791e | 1950 | |
2a3b791e JV |
1951 | /* do the statistics and put it back */ |
1952 | ip_vs_in_stats(cp, skb); | |
63dca2c0 JDB |
1953 | |
1954 | /* Need to mangle contained IPv6 header in ICMPv6 packet */ | |
b0e010c5 | 1955 | offset = ciph.len; |
63dca2c0 JDB |
1956 | if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol || |
1957 | IPPROTO_SCTP == ciph.protocol) | |
b0e010c5 | 1958 | offset += 2 * sizeof(__u16); /* Also mangle ports */ |
63dca2c0 | 1959 | |
b0e010c5 | 1960 | verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph); |
2a3b791e | 1961 | |
6044eeff AG |
1962 | out: |
1963 | if (likely(!new_cp)) | |
1964 | __ip_vs_conn_put(cp); | |
1965 | else | |
1966 | ip_vs_conn_put(cp); | |
2a3b791e JV |
1967 | |
1968 | return verdict; | |
1969 | } | |
1970 | #endif | |
1971 | ||
1972 | ||
1da177e4 LT |
1973 | /* |
1974 | * Check if it's for virtual services, look it up, | |
1975 | * and send it on its way... | |
1976 | */ | |
1977 | static unsigned int | |
6e385bb3 | 1978 | ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1979 | { |
51ef348b | 1980 | struct ip_vs_iphdr iph; |
1da177e4 | 1981 | struct ip_vs_protocol *pp; |
9330419d | 1982 | struct ip_vs_proto_data *pd; |
1da177e4 | 1983 | struct ip_vs_conn *cp; |
4a516f11 | 1984 | int ret, pkts; |
d752c364 | 1985 | int conn_reuse_mode; |
340c78e5 | 1986 | struct sock *sk; |
2a3b791e | 1987 | |
fc604767 JA |
1988 | /* Already marked as IPVS request or reply? */ |
1989 | if (skb->ipvs_property) | |
1990 | return NF_ACCEPT; | |
1991 | ||
1da177e4 | 1992 | /* |
cb59155f JA |
1993 | * Big tappo: |
1994 | * - remote client: only PACKET_HOST | |
1995 | * - route: used for struct net when skb->dev is unset | |
1da177e4 | 1996 | */ |
cb59155f JA |
1997 | if (unlikely((skb->pkt_type != PACKET_HOST && |
1998 | hooknum != NF_INET_LOCAL_OUT) || | |
1999 | !skb_dst(skb))) { | |
4fd9beef | 2000 | ip_vs_fill_iph_skb(af, skb, false, &iph); |
cb59155f JA |
2001 | IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s" |
2002 | " ignored in hook %u\n", | |
2003 | skb->pkt_type, iph.protocol, | |
2004 | IP_VS_DBG_ADDR(af, &iph.daddr), hooknum); | |
1da177e4 LT |
2005 | return NF_ACCEPT; |
2006 | } | |
7a4f0761 | 2007 | /* ipvs enabled in this netns ? */ |
0c12582f | 2008 | if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable)) |
7a4f0761 HS |
2009 | return NF_ACCEPT; |
2010 | ||
4fd9beef | 2011 | ip_vs_fill_iph_skb(af, skb, false, &iph); |
cb59155f JA |
2012 | |
2013 | /* Bad... Do not break raw sockets */ | |
340c78e5 ED |
2014 | sk = skb_to_full_sk(skb); |
2015 | if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT && | |
cb59155f | 2016 | af == AF_INET)) { |
cb59155f | 2017 | |
340c78e5 | 2018 | if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag) |
cb59155f JA |
2019 | return NF_ACCEPT; |
2020 | } | |
1da177e4 | 2021 | |
94b26551 JV |
2022 | #ifdef CONFIG_IP_VS_IPV6 |
2023 | if (af == AF_INET6) { | |
2024 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 | 2025 | int related; |
6f2bcea9 EB |
2026 | int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related, |
2027 | hooknum, &iph); | |
1da177e4 | 2028 | |
94b26551 JV |
2029 | if (related) |
2030 | return verdict; | |
94b26551 JV |
2031 | } |
2032 | } else | |
2033 | #endif | |
2034 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 | 2035 | int related; |
6f2bcea9 EB |
2036 | int verdict = ip_vs_in_icmp(ipvs, skb, &related, |
2037 | hooknum); | |
94b26551 JV |
2038 | |
2039 | if (related) | |
2040 | return verdict; | |
94b26551 | 2041 | } |
1da177e4 LT |
2042 | |
2043 | /* Protocol supported? */ | |
18d6ade6 | 2044 | pd = ip_vs_proto_data_get(ipvs, iph.protocol); |
4e478098 AG |
2045 | if (unlikely(!pd)) { |
2046 | /* The only way we'll see this packet again is if it's | |
2047 | * encapsulated, so mark it with ipvs_property=1 so we | |
2048 | * skip it if we're ignoring tunneled packets | |
2049 | */ | |
2050 | if (sysctl_ignore_tunneled(ipvs)) | |
2051 | skb->ipvs_property = 1; | |
2052 | ||
1da177e4 | 2053 | return NF_ACCEPT; |
4e478098 | 2054 | } |
9330419d | 2055 | pp = pd->pp; |
1da177e4 LT |
2056 | /* |
2057 | * Check if the packet belongs to an existing connection entry | |
2058 | */ | |
6ecd7548 MC |
2059 | cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto, |
2060 | ipvs, af, skb, &iph); | |
dc7b3eb9 | 2061 | |
d752c364 | 2062 | conn_reuse_mode = sysctl_conn_reuse_mode(ipvs); |
f719e375 JA |
2063 | if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) { |
2064 | bool uses_ct = false, resched = false; | |
2065 | ||
2066 | if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest && | |
2067 | unlikely(!atomic_read(&cp->dest->weight))) { | |
2068 | resched = true; | |
2069 | uses_ct = ip_vs_conn_uses_conntrack(cp, skb); | |
2070 | } else if (is_new_conn_expected(cp, conn_reuse_mode)) { | |
2071 | uses_ct = ip_vs_conn_uses_conntrack(cp, skb); | |
2072 | if (!atomic_read(&cp->n_control)) { | |
2073 | resched = true; | |
2074 | } else { | |
2075 | /* Do not reschedule controlling connection | |
2076 | * that uses conntrack while it is still | |
2077 | * referenced by controlled connection(s). | |
2078 | */ | |
2079 | resched = !uses_ct; | |
2080 | } | |
2081 | } | |
2082 | ||
2083 | if (resched) { | |
2084 | if (!atomic_read(&cp->n_control)) | |
2085 | ip_vs_conn_expire_now(cp); | |
2086 | __ip_vs_conn_put(cp); | |
2087 | if (uses_ct) | |
2088 | return NF_DROP; | |
2089 | cp = NULL; | |
2090 | } | |
dc7b3eb9 GL |
2091 | } |
2092 | ||
3b5ca617 | 2093 | if (unlikely(!cp)) { |
1da177e4 LT |
2094 | int v; |
2095 | ||
d8f44c33 | 2096 | if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph)) |
1da177e4 LT |
2097 | return v; |
2098 | } | |
2099 | ||
b0e010c5 | 2100 | IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet"); |
3b5ca617 | 2101 | |
1da177e4 LT |
2102 | /* Check the server status */ |
2103 | if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
2104 | /* the destination server is not available */ | |
2105 | ||
a53b42c1 TH |
2106 | __u32 flags = cp->flags; |
2107 | ||
2108 | /* when timer already started, silently drop the packet.*/ | |
2109 | if (timer_pending(&cp->timer)) | |
2110 | __ip_vs_conn_put(cp); | |
2111 | else | |
2112 | ip_vs_conn_put(cp); | |
2113 | ||
2114 | if (sysctl_expire_nodest_conn(ipvs) && | |
2115 | !(flags & IP_VS_CONN_F_ONE_PACKET)) { | |
1da177e4 LT |
2116 | /* try to expire the connection immediately */ |
2117 | ip_vs_conn_expire_now(cp); | |
1da177e4 | 2118 | } |
a53b42c1 | 2119 | |
1da177e4 LT |
2120 | return NF_DROP; |
2121 | } | |
2122 | ||
2123 | ip_vs_in_stats(cp, skb); | |
4a516f11 | 2124 | ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 | 2125 | if (cp->packet_xmit) |
d4383f04 | 2126 | ret = cp->packet_xmit(skb, cp, pp, &iph); |
1da177e4 LT |
2127 | /* do not touch skb anymore */ |
2128 | else { | |
2129 | IP_VS_DBG_RL("warning: packet_xmit is null"); | |
2130 | ret = NF_ACCEPT; | |
2131 | } | |
2132 | ||
efac5276 RB |
2133 | /* Increase its packet counter and check if it is needed |
2134 | * to be synchronized | |
2135 | * | |
2136 | * Sync connection if it is about to close to | |
2137 | * encorage the standby servers to update the connections timeout | |
986a0757 HS |
2138 | * |
2139 | * For ONE_PKT let ip_vs_sync_conn() do the filter work. | |
efac5276 | 2140 | */ |
f131315f | 2141 | |
986a0757 | 2142 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
59e0350e | 2143 | pkts = sysctl_sync_threshold(ipvs); |
986a0757 HS |
2144 | else |
2145 | pkts = atomic_add_return(1, &cp->in_pkts); | |
2146 | ||
749c42b6 | 2147 | if (ipvs->sync_state & IP_VS_STATE_MASTER) |
b61a8c1a | 2148 | ip_vs_sync_conn(ipvs, cp, pkts); |
698e2a8d MA |
2149 | else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control) |
2150 | /* increment is done inside ip_vs_sync_conn too */ | |
2151 | atomic_inc(&cp->control->in_pkts); | |
1da177e4 LT |
2152 | |
2153 | ip_vs_conn_put(cp); | |
2154 | return ret; | |
2155 | } | |
2156 | ||
cb59155f JA |
2157 | /* |
2158 | * AF_INET handler in NF_INET_LOCAL_IN chain | |
2159 | * Schedule and forward packets from remote clients | |
2160 | */ | |
2161 | static unsigned int | |
06198b34 | 2162 | ip_vs_remote_request4(void *priv, struct sk_buff *skb, |
238e54c9 | 2163 | const struct nf_hook_state *state) |
cb59155f | 2164 | { |
6e385bb3 | 2165 | return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET); |
cb59155f JA |
2166 | } |
2167 | ||
2168 | /* | |
2169 | * AF_INET handler in NF_INET_LOCAL_OUT chain | |
2170 | * Schedule and forward packets from local clients | |
2171 | */ | |
2172 | static unsigned int | |
06198b34 | 2173 | ip_vs_local_request4(void *priv, struct sk_buff *skb, |
238e54c9 | 2174 | const struct nf_hook_state *state) |
cb59155f | 2175 | { |
6e385bb3 | 2176 | return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET); |
cb59155f JA |
2177 | } |
2178 | ||
2179 | #ifdef CONFIG_IP_VS_IPV6 | |
2180 | ||
2181 | /* | |
2182 | * AF_INET6 handler in NF_INET_LOCAL_IN chain | |
2183 | * Schedule and forward packets from remote clients | |
2184 | */ | |
2185 | static unsigned int | |
06198b34 | 2186 | ip_vs_remote_request6(void *priv, struct sk_buff *skb, |
238e54c9 | 2187 | const struct nf_hook_state *state) |
cb59155f | 2188 | { |
6e385bb3 | 2189 | return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6); |
cb59155f JA |
2190 | } |
2191 | ||
2192 | /* | |
2193 | * AF_INET6 handler in NF_INET_LOCAL_OUT chain | |
2194 | * Schedule and forward packets from local clients | |
2195 | */ | |
2196 | static unsigned int | |
06198b34 | 2197 | ip_vs_local_request6(void *priv, struct sk_buff *skb, |
238e54c9 | 2198 | const struct nf_hook_state *state) |
cb59155f | 2199 | { |
6e385bb3 | 2200 | return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6); |
cb59155f JA |
2201 | } |
2202 | ||
2203 | #endif | |
2204 | ||
1da177e4 LT |
2205 | |
2206 | /* | |
6e23ae2a | 2207 | * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP |
1da177e4 LT |
2208 | * related packets destined for 0.0.0.0/0. |
2209 | * When fwmark-based virtual service is used, such as transparent | |
2210 | * cache cluster, TCP packets can be marked and routed to ip_vs_in, | |
2211 | * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and | |
6e23ae2a | 2212 | * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain |
1da177e4 LT |
2213 | * and send them to ip_vs_in_icmp. |
2214 | */ | |
2215 | static unsigned int | |
06198b34 | 2216 | ip_vs_forward_icmp(void *priv, struct sk_buff *skb, |
238e54c9 | 2217 | const struct nf_hook_state *state) |
1da177e4 LT |
2218 | { |
2219 | int r; | |
6f2bcea9 | 2220 | struct netns_ipvs *ipvs = net_ipvs(state->net); |
1da177e4 | 2221 | |
3db05fea | 2222 | if (ip_hdr(skb)->protocol != IPPROTO_ICMP) |
1da177e4 LT |
2223 | return NF_ACCEPT; |
2224 | ||
7a4f0761 | 2225 | /* ipvs enabled in this netns ? */ |
0c12582f | 2226 | if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable)) |
7a4f0761 HS |
2227 | return NF_ACCEPT; |
2228 | ||
6f2bcea9 | 2229 | return ip_vs_in_icmp(ipvs, skb, &r, state->hook); |
1da177e4 LT |
2230 | } |
2231 | ||
2a3b791e JV |
2232 | #ifdef CONFIG_IP_VS_IPV6 |
2233 | static unsigned int | |
06198b34 | 2234 | ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb, |
238e54c9 | 2235 | const struct nf_hook_state *state) |
2a3b791e JV |
2236 | { |
2237 | int r; | |
6f2bcea9 | 2238 | struct netns_ipvs *ipvs = net_ipvs(state->net); |
63dca2c0 | 2239 | struct ip_vs_iphdr iphdr; |
2a3b791e | 2240 | |
4fd9beef | 2241 | ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr); |
63dca2c0 | 2242 | if (iphdr.protocol != IPPROTO_ICMPV6) |
2a3b791e JV |
2243 | return NF_ACCEPT; |
2244 | ||
7a4f0761 | 2245 | /* ipvs enabled in this netns ? */ |
0c12582f | 2246 | if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable)) |
7a4f0761 HS |
2247 | return NF_ACCEPT; |
2248 | ||
6f2bcea9 | 2249 | return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr); |
2a3b791e JV |
2250 | } |
2251 | #endif | |
2252 | ||
1da177e4 | 2253 | |
591bb278 | 2254 | static const struct nf_hook_ops ip_vs_ops[] = { |
cb59155f JA |
2255 | /* After packet filtering, change source only for VS/NAT */ |
2256 | { | |
2257 | .hook = ip_vs_reply4, | |
4c809d63 | 2258 | .pf = NFPROTO_IPV4, |
cb59155f | 2259 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 2260 | .priority = NF_IP_PRI_NAT_SRC - 2, |
cb59155f | 2261 | }, |
41c5b317 PM |
2262 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
2263 | * or VS/NAT(change destination), so that filtering rules can be | |
2264 | * applied to IPVS. */ | |
2265 | { | |
cb59155f | 2266 | .hook = ip_vs_remote_request4, |
4c809d63 | 2267 | .pf = NFPROTO_IPV4, |
cb59155f | 2268 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 2269 | .priority = NF_IP_PRI_NAT_SRC - 1, |
41c5b317 | 2270 | }, |
fc604767 | 2271 | /* Before ip_vs_in, change source only for VS/NAT */ |
41c5b317 | 2272 | { |
fc604767 | 2273 | .hook = ip_vs_local_reply4, |
4c809d63 | 2274 | .pf = NFPROTO_IPV4, |
fc604767 | 2275 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 2276 | .priority = NF_IP_PRI_NAT_DST + 1, |
41c5b317 | 2277 | }, |
cb59155f JA |
2278 | /* After mangle, schedule and forward local requests */ |
2279 | { | |
2280 | .hook = ip_vs_local_request4, | |
4c809d63 | 2281 | .pf = NFPROTO_IPV4, |
cb59155f | 2282 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 2283 | .priority = NF_IP_PRI_NAT_DST + 2, |
cb59155f | 2284 | }, |
41c5b317 PM |
2285 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
2286 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
2287 | { | |
2288 | .hook = ip_vs_forward_icmp, | |
4c809d63 | 2289 | .pf = NFPROTO_IPV4, |
cb59155f JA |
2290 | .hooknum = NF_INET_FORWARD, |
2291 | .priority = 99, | |
41c5b317 | 2292 | }, |
fc604767 JA |
2293 | /* After packet filtering, change source only for VS/NAT */ |
2294 | { | |
2295 | .hook = ip_vs_reply4, | |
4c809d63 | 2296 | .pf = NFPROTO_IPV4, |
fc604767 JA |
2297 | .hooknum = NF_INET_FORWARD, |
2298 | .priority = 100, | |
2299 | }, | |
473b23d3 | 2300 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
2301 | /* After packet filtering, change source only for VS/NAT */ |
2302 | { | |
2303 | .hook = ip_vs_reply6, | |
4c809d63 | 2304 | .pf = NFPROTO_IPV6, |
cb59155f | 2305 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 2306 | .priority = NF_IP6_PRI_NAT_SRC - 2, |
cb59155f | 2307 | }, |
473b23d3 JV |
2308 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
2309 | * or VS/NAT(change destination), so that filtering rules can be | |
2310 | * applied to IPVS. */ | |
2311 | { | |
cb59155f | 2312 | .hook = ip_vs_remote_request6, |
4c809d63 | 2313 | .pf = NFPROTO_IPV6, |
cb59155f | 2314 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 2315 | .priority = NF_IP6_PRI_NAT_SRC - 1, |
473b23d3 | 2316 | }, |
fc604767 | 2317 | /* Before ip_vs_in, change source only for VS/NAT */ |
473b23d3 | 2318 | { |
fc604767 | 2319 | .hook = ip_vs_local_reply6, |
eb90b0c7 | 2320 | .pf = NFPROTO_IPV6, |
fc604767 | 2321 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 2322 | .priority = NF_IP6_PRI_NAT_DST + 1, |
473b23d3 | 2323 | }, |
cb59155f JA |
2324 | /* After mangle, schedule and forward local requests */ |
2325 | { | |
2326 | .hook = ip_vs_local_request6, | |
4c809d63 | 2327 | .pf = NFPROTO_IPV6, |
cb59155f | 2328 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 2329 | .priority = NF_IP6_PRI_NAT_DST + 2, |
cb59155f | 2330 | }, |
473b23d3 JV |
2331 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
2332 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
2333 | { | |
2334 | .hook = ip_vs_forward_icmp_v6, | |
4c809d63 | 2335 | .pf = NFPROTO_IPV6, |
cb59155f JA |
2336 | .hooknum = NF_INET_FORWARD, |
2337 | .priority = 99, | |
473b23d3 | 2338 | }, |
fc604767 JA |
2339 | /* After packet filtering, change source only for VS/NAT */ |
2340 | { | |
2341 | .hook = ip_vs_reply6, | |
4c809d63 | 2342 | .pf = NFPROTO_IPV6, |
fc604767 JA |
2343 | .hooknum = NF_INET_FORWARD, |
2344 | .priority = 100, | |
2345 | }, | |
473b23d3 | 2346 | #endif |
1da177e4 | 2347 | }; |
61b1ab45 HS |
2348 | /* |
2349 | * Initialize IP Virtual Server netns mem. | |
2350 | */ | |
2351 | static int __net_init __ip_vs_init(struct net *net) | |
2352 | { | |
2353 | struct netns_ipvs *ipvs; | |
2354 | ||
61b1ab45 | 2355 | ipvs = net_generic(net, ip_vs_net_id); |
0a9ee813 | 2356 | if (ipvs == NULL) |
61b1ab45 | 2357 | return -ENOMEM; |
0a9ee813 | 2358 | |
7a4f0761 HS |
2359 | /* Hold the beast until a service is registerd */ |
2360 | ipvs->enable = 0; | |
f6340ee0 | 2361 | ipvs->net = net; |
61b1ab45 HS |
2362 | /* Counters used for creating unique names */ |
2363 | ipvs->gen = atomic_read(&ipvs_netns_cnt); | |
2364 | atomic_inc(&ipvs_netns_cnt); | |
2365 | net->ipvs = ipvs; | |
7a4f0761 | 2366 | |
a4dd0360 | 2367 | if (ip_vs_estimator_net_init(ipvs) < 0) |
7a4f0761 HS |
2368 | goto estimator_fail; |
2369 | ||
3d993766 | 2370 | if (ip_vs_control_net_init(ipvs) < 0) |
7a4f0761 HS |
2371 | goto control_fail; |
2372 | ||
7d1f88ec | 2373 | if (ip_vs_protocol_net_init(ipvs) < 0) |
7a4f0761 HS |
2374 | goto protocol_fail; |
2375 | ||
b5dd212c | 2376 | if (ip_vs_app_net_init(ipvs) < 0) |
7a4f0761 HS |
2377 | goto app_fail; |
2378 | ||
2f3edc6a | 2379 | if (ip_vs_conn_net_init(ipvs) < 0) |
7a4f0761 HS |
2380 | goto conn_fail; |
2381 | ||
802cb437 | 2382 | if (ip_vs_sync_net_init(ipvs) < 0) |
7a4f0761 HS |
2383 | goto sync_fail; |
2384 | ||
61b1ab45 | 2385 | return 0; |
7a4f0761 HS |
2386 | /* |
2387 | * Error handling | |
2388 | */ | |
2389 | ||
2390 | sync_fail: | |
2f3edc6a | 2391 | ip_vs_conn_net_cleanup(ipvs); |
7a4f0761 | 2392 | conn_fail: |
b5dd212c | 2393 | ip_vs_app_net_cleanup(ipvs); |
7a4f0761 | 2394 | app_fail: |
7d1f88ec | 2395 | ip_vs_protocol_net_cleanup(ipvs); |
7a4f0761 | 2396 | protocol_fail: |
3d993766 | 2397 | ip_vs_control_net_cleanup(ipvs); |
7a4f0761 | 2398 | control_fail: |
a4dd0360 | 2399 | ip_vs_estimator_net_cleanup(ipvs); |
7a4f0761 | 2400 | estimator_fail: |
39f618b4 | 2401 | net->ipvs = NULL; |
7a4f0761 | 2402 | return -ENOMEM; |
61b1ab45 HS |
2403 | } |
2404 | ||
5d5a0815 | 2405 | static void __net_exit __ip_vs_cleanup_batch(struct list_head *net_list) |
61b1ab45 | 2406 | { |
5d5a0815 HY |
2407 | struct netns_ipvs *ipvs; |
2408 | struct net *net; | |
2409 | ||
2410 | ip_vs_service_nets_cleanup(net_list); /* ip_vs_flush() with locks */ | |
2411 | list_for_each_entry(net, net_list, exit_list) { | |
2412 | ipvs = net_ipvs(net); | |
2413 | ip_vs_conn_net_cleanup(ipvs); | |
2414 | ip_vs_app_net_cleanup(ipvs); | |
2415 | ip_vs_protocol_net_cleanup(ipvs); | |
2416 | ip_vs_control_net_cleanup(ipvs); | |
2417 | ip_vs_estimator_net_cleanup(ipvs); | |
2418 | IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen); | |
2419 | net->ipvs = NULL; | |
2420 | } | |
61b1ab45 HS |
2421 | } |
2422 | ||
cf47a0b8 JA |
2423 | static int __net_init __ip_vs_dev_init(struct net *net) |
2424 | { | |
2425 | int ret; | |
2426 | ||
2427 | ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); | |
2428 | if (ret < 0) | |
2429 | goto hook_fail; | |
2430 | return 0; | |
2431 | ||
2432 | hook_fail: | |
2433 | return ret; | |
2434 | } | |
2435 | ||
ac524481 | 2436 | static void __net_exit __ip_vs_dev_cleanup_batch(struct list_head *net_list) |
7a4f0761 | 2437 | { |
ac524481 HY |
2438 | struct netns_ipvs *ipvs; |
2439 | struct net *net; | |
2440 | ||
7a4f0761 | 2441 | EnterFunction(2); |
ac524481 HY |
2442 | list_for_each_entry(net, net_list, exit_list) { |
2443 | ipvs = net_ipvs(net); | |
2444 | nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); | |
2445 | ipvs->enable = 0; /* Disable packet reception */ | |
2446 | smp_wmb(); | |
2447 | ip_vs_sync_net_cleanup(ipvs); | |
2448 | } | |
7a4f0761 HS |
2449 | LeaveFunction(2); |
2450 | } | |
2451 | ||
61b1ab45 HS |
2452 | static struct pernet_operations ipvs_core_ops = { |
2453 | .init = __ip_vs_init, | |
5d5a0815 | 2454 | .exit_batch = __ip_vs_cleanup_batch, |
61b1ab45 HS |
2455 | .id = &ip_vs_net_id, |
2456 | .size = sizeof(struct netns_ipvs), | |
2457 | }; | |
1da177e4 | 2458 | |
7a4f0761 | 2459 | static struct pernet_operations ipvs_core_dev_ops = { |
cf47a0b8 | 2460 | .init = __ip_vs_dev_init, |
ac524481 | 2461 | .exit_batch = __ip_vs_dev_cleanup_batch, |
7a4f0761 HS |
2462 | }; |
2463 | ||
1da177e4 LT |
2464 | /* |
2465 | * Initialize IP Virtual Server | |
2466 | */ | |
2467 | static int __init ip_vs_init(void) | |
2468 | { | |
2469 | int ret; | |
2470 | ||
2471 | ret = ip_vs_control_init(); | |
2472 | if (ret < 0) { | |
1e3e238e | 2473 | pr_err("can't setup control.\n"); |
6c8f7949 | 2474 | goto exit; |
1da177e4 LT |
2475 | } |
2476 | ||
2477 | ip_vs_protocol_init(); | |
2478 | ||
1da177e4 LT |
2479 | ret = ip_vs_conn_init(); |
2480 | if (ret < 0) { | |
1e3e238e | 2481 | pr_err("can't setup connection table.\n"); |
6c8f7949 | 2482 | goto cleanup_protocol; |
61b1ab45 HS |
2483 | } |
2484 | ||
7a4f0761 HS |
2485 | ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */ |
2486 | if (ret < 0) | |
6c8f7949 | 2487 | goto cleanup_conn; |
7a4f0761 HS |
2488 | |
2489 | ret = register_pernet_device(&ipvs_core_dev_ops); | |
2490 | if (ret < 0) | |
2491 | goto cleanup_sub; | |
2492 | ||
8537de8a HS |
2493 | ret = ip_vs_register_nl_ioctl(); |
2494 | if (ret < 0) { | |
2495 | pr_err("can't register netlink/ioctl.\n"); | |
efe41606 | 2496 | goto cleanup_dev; |
8537de8a HS |
2497 | } |
2498 | ||
1e3e238e | 2499 | pr_info("ipvs loaded.\n"); |
7a4f0761 | 2500 | |
1da177e4 LT |
2501 | return ret; |
2502 | ||
7a4f0761 HS |
2503 | cleanup_dev: |
2504 | unregister_pernet_device(&ipvs_core_dev_ops); | |
2505 | cleanup_sub: | |
2506 | unregister_pernet_subsys(&ipvs_core_ops); | |
552ad65a | 2507 | cleanup_conn: |
1da177e4 | 2508 | ip_vs_conn_cleanup(); |
552ad65a | 2509 | cleanup_protocol: |
1da177e4 LT |
2510 | ip_vs_protocol_cleanup(); |
2511 | ip_vs_control_cleanup(); | |
6c8f7949 | 2512 | exit: |
1da177e4 LT |
2513 | return ret; |
2514 | } | |
2515 | ||
2516 | static void __exit ip_vs_cleanup(void) | |
2517 | { | |
8537de8a | 2518 | ip_vs_unregister_nl_ioctl(); |
7a4f0761 HS |
2519 | unregister_pernet_device(&ipvs_core_dev_ops); |
2520 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ | |
1da177e4 | 2521 | ip_vs_conn_cleanup(); |
1da177e4 LT |
2522 | ip_vs_protocol_cleanup(); |
2523 | ip_vs_control_cleanup(); | |
1e3e238e | 2524 | pr_info("ipvs unloaded.\n"); |
1da177e4 LT |
2525 | } |
2526 | ||
2527 | module_init(ip_vs_init); | |
2528 | module_exit(ip_vs_cleanup); | |
2529 | MODULE_LICENSE("GPL"); |