Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPVS An implementation of the IP virtual server support for the | |
3 | * LINUX operating system. IPVS is now implemented as a module | |
4 | * over the Netfilter framework. IPVS can be used to build a | |
5 | * high-performance and highly available server based on a | |
6 | * cluster of servers. | |
7 | * | |
1da177e4 LT |
8 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
9 | * Peter Kese <peter.kese@ijs.si> | |
10 | * Julian Anastasov <ja@ssi.bg> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; either version | |
15 | * 2 of the License, or (at your option) any later version. | |
16 | * | |
17 | * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese, | |
18 | * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms | |
19 | * and others. | |
20 | * | |
21 | * Changes: | |
22 | * Paul `Rusty' Russell properly handle non-linear skbs | |
6869c4d8 | 23 | * Harald Welte don't use nfcache |
1da177e4 LT |
24 | * |
25 | */ | |
26 | ||
9aada7ac HE |
27 | #define KMSG_COMPONENT "IPVS" |
28 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
29 | ||
1da177e4 LT |
30 | #include <linux/module.h> |
31 | #include <linux/kernel.h> | |
32 | #include <linux/ip.h> | |
33 | #include <linux/tcp.h> | |
2906f66a | 34 | #include <linux/sctp.h> |
1da177e4 | 35 | #include <linux/icmp.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
1da177e4 LT |
37 | |
38 | #include <net/ip.h> | |
39 | #include <net/tcp.h> | |
40 | #include <net/udp.h> | |
41 | #include <net/icmp.h> /* for icmp_send */ | |
42 | #include <net/route.h> | |
2c70b519 | 43 | #include <net/ip6_checksum.h> |
61b1ab45 | 44 | #include <net/netns/generic.h> /* net_generic() */ |
1da177e4 LT |
45 | |
46 | #include <linux/netfilter.h> | |
47 | #include <linux/netfilter_ipv4.h> | |
48 | ||
2a3b791e JV |
49 | #ifdef CONFIG_IP_VS_IPV6 |
50 | #include <net/ipv6.h> | |
51 | #include <linux/netfilter_ipv6.h> | |
489fdeda | 52 | #include <net/ip6_route.h> |
2a3b791e JV |
53 | #endif |
54 | ||
1da177e4 LT |
55 | #include <net/ip_vs.h> |
56 | ||
57 | ||
58 | EXPORT_SYMBOL(register_ip_vs_scheduler); | |
59 | EXPORT_SYMBOL(unregister_ip_vs_scheduler); | |
1da177e4 LT |
60 | EXPORT_SYMBOL(ip_vs_proto_name); |
61 | EXPORT_SYMBOL(ip_vs_conn_new); | |
62 | EXPORT_SYMBOL(ip_vs_conn_in_get); | |
63 | EXPORT_SYMBOL(ip_vs_conn_out_get); | |
64 | #ifdef CONFIG_IP_VS_PROTO_TCP | |
65 | EXPORT_SYMBOL(ip_vs_tcp_conn_listen); | |
66 | #endif | |
67 | EXPORT_SYMBOL(ip_vs_conn_put); | |
68 | #ifdef CONFIG_IP_VS_DEBUG | |
69 | EXPORT_SYMBOL(ip_vs_get_debug_level); | |
70 | #endif | |
1da177e4 | 71 | |
61b1ab45 HS |
72 | int ip_vs_net_id __read_mostly; |
73 | #ifdef IP_VS_GENERIC_NETNS | |
74 | EXPORT_SYMBOL(ip_vs_net_id); | |
75 | #endif | |
76 | /* netns cnt used for uniqueness */ | |
77 | static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0); | |
1da177e4 LT |
78 | |
79 | /* ID used in ICMP lookups */ | |
80 | #define icmp_id(icmph) (((icmph)->un).echo.id) | |
2a3b791e | 81 | #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier) |
1da177e4 | 82 | |
95c96174 | 83 | const char *ip_vs_proto_name(unsigned int proto) |
1da177e4 LT |
84 | { |
85 | static char buf[20]; | |
86 | ||
87 | switch (proto) { | |
88 | case IPPROTO_IP: | |
89 | return "IP"; | |
90 | case IPPROTO_UDP: | |
91 | return "UDP"; | |
92 | case IPPROTO_TCP: | |
93 | return "TCP"; | |
2906f66a VMR |
94 | case IPPROTO_SCTP: |
95 | return "SCTP"; | |
1da177e4 LT |
96 | case IPPROTO_ICMP: |
97 | return "ICMP"; | |
2a3b791e JV |
98 | #ifdef CONFIG_IP_VS_IPV6 |
99 | case IPPROTO_ICMPV6: | |
100 | return "ICMPv6"; | |
101 | #endif | |
1da177e4 LT |
102 | default: |
103 | sprintf(buf, "IP_%d", proto); | |
104 | return buf; | |
105 | } | |
106 | } | |
107 | ||
108 | void ip_vs_init_hash_table(struct list_head *table, int rows) | |
109 | { | |
110 | while (--rows >= 0) | |
111 | INIT_LIST_HEAD(&table[rows]); | |
112 | } | |
113 | ||
114 | static inline void | |
115 | ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
116 | { | |
117 | struct ip_vs_dest *dest = cp->dest; | |
b17fc996 HS |
118 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); |
119 | ||
1da177e4 | 120 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
b17fc996 HS |
121 | struct ip_vs_cpu_stats *s; |
122 | ||
123 | s = this_cpu_ptr(dest->stats.cpustats); | |
124 | s->ustats.inpkts++; | |
125 | u64_stats_update_begin(&s->syncp); | |
126 | s->ustats.inbytes += skb->len; | |
127 | u64_stats_update_end(&s->syncp); | |
128 | ||
129 | s = this_cpu_ptr(dest->svc->stats.cpustats); | |
130 | s->ustats.inpkts++; | |
131 | u64_stats_update_begin(&s->syncp); | |
132 | s->ustats.inbytes += skb->len; | |
133 | u64_stats_update_end(&s->syncp); | |
134 | ||
2a0751af | 135 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
b17fc996 HS |
136 | s->ustats.inpkts++; |
137 | u64_stats_update_begin(&s->syncp); | |
138 | s->ustats.inbytes += skb->len; | |
139 | u64_stats_update_end(&s->syncp); | |
1da177e4 LT |
140 | } |
141 | } | |
142 | ||
143 | ||
144 | static inline void | |
145 | ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
146 | { | |
147 | struct ip_vs_dest *dest = cp->dest; | |
b17fc996 HS |
148 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); |
149 | ||
1da177e4 | 150 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
b17fc996 HS |
151 | struct ip_vs_cpu_stats *s; |
152 | ||
153 | s = this_cpu_ptr(dest->stats.cpustats); | |
154 | s->ustats.outpkts++; | |
155 | u64_stats_update_begin(&s->syncp); | |
156 | s->ustats.outbytes += skb->len; | |
157 | u64_stats_update_end(&s->syncp); | |
158 | ||
159 | s = this_cpu_ptr(dest->svc->stats.cpustats); | |
160 | s->ustats.outpkts++; | |
161 | u64_stats_update_begin(&s->syncp); | |
162 | s->ustats.outbytes += skb->len; | |
163 | u64_stats_update_end(&s->syncp); | |
164 | ||
2a0751af | 165 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
b17fc996 HS |
166 | s->ustats.outpkts++; |
167 | u64_stats_update_begin(&s->syncp); | |
168 | s->ustats.outbytes += skb->len; | |
169 | u64_stats_update_end(&s->syncp); | |
1da177e4 LT |
170 | } |
171 | } | |
172 | ||
173 | ||
174 | static inline void | |
175 | ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc) | |
176 | { | |
b17fc996 HS |
177 | struct netns_ipvs *ipvs = net_ipvs(svc->net); |
178 | struct ip_vs_cpu_stats *s; | |
1da177e4 | 179 | |
b17fc996 HS |
180 | s = this_cpu_ptr(cp->dest->stats.cpustats); |
181 | s->ustats.conns++; | |
1da177e4 | 182 | |
b17fc996 HS |
183 | s = this_cpu_ptr(svc->stats.cpustats); |
184 | s->ustats.conns++; | |
185 | ||
2a0751af | 186 | s = this_cpu_ptr(ipvs->tot_stats.cpustats); |
b17fc996 | 187 | s->ustats.conns++; |
1da177e4 LT |
188 | } |
189 | ||
190 | ||
4a516f11 | 191 | static inline void |
1da177e4 LT |
192 | ip_vs_set_state(struct ip_vs_conn *cp, int direction, |
193 | const struct sk_buff *skb, | |
9330419d | 194 | struct ip_vs_proto_data *pd) |
1da177e4 | 195 | { |
4a516f11 SH |
196 | if (likely(pd->pp->state_transition)) |
197 | pd->pp->state_transition(cp, direction, skb, pd); | |
1da177e4 LT |
198 | } |
199 | ||
a5959d53 | 200 | static inline int |
85999283 SH |
201 | ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc, |
202 | struct sk_buff *skb, int protocol, | |
203 | const union nf_inet_addr *caddr, __be16 cport, | |
204 | const union nf_inet_addr *vaddr, __be16 vport, | |
205 | struct ip_vs_conn_param *p) | |
206 | { | |
6e67e586 HS |
207 | ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr, |
208 | vport, p); | |
85999283 SH |
209 | p->pe = svc->pe; |
210 | if (p->pe && p->pe->fill_param) | |
a5959d53 HS |
211 | return p->pe->fill_param(p, skb); |
212 | ||
213 | return 0; | |
85999283 | 214 | } |
1da177e4 | 215 | |
1da177e4 LT |
216 | /* |
217 | * IPVS persistent scheduling function | |
218 | * It creates a connection entry according to its template if exists, | |
219 | * or selects a server and creates a connection entry plus a template. | |
220 | * Locking: we are svc user (svc->refcnt), so we hold all dests too | |
221 | * Protocols supported: TCP, UDP | |
222 | */ | |
223 | static struct ip_vs_conn * | |
224 | ip_vs_sched_persist(struct ip_vs_service *svc, | |
85999283 | 225 | struct sk_buff *skb, |
a5959d53 | 226 | __be16 src_port, __be16 dst_port, int *ignored) |
1da177e4 LT |
227 | { |
228 | struct ip_vs_conn *cp = NULL; | |
28364a59 | 229 | struct ip_vs_iphdr iph; |
1da177e4 LT |
230 | struct ip_vs_dest *dest; |
231 | struct ip_vs_conn *ct; | |
5b57a98c | 232 | __be16 dport = 0; /* destination port to forward */ |
3575792e | 233 | unsigned int flags; |
f11017ec | 234 | struct ip_vs_conn_param param; |
e0aac52e | 235 | const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) }; |
28364a59 JV |
236 | union nf_inet_addr snet; /* source network of the client, |
237 | after masking */ | |
cd17f9ed | 238 | |
63dca2c0 | 239 | ip_vs_fill_iph_skb(svc->af, skb, &iph); |
1da177e4 LT |
240 | |
241 | /* Mask saddr with the netmask to adjust template granularity */ | |
cd17f9ed JV |
242 | #ifdef CONFIG_IP_VS_IPV6 |
243 | if (svc->af == AF_INET6) | |
244 | ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask); | |
245 | else | |
246 | #endif | |
247 | snet.ip = iph.saddr.ip & svc->netmask; | |
1da177e4 | 248 | |
cd17f9ed JV |
249 | IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u " |
250 | "mnet %s\n", | |
ce144f24 HS |
251 | IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port), |
252 | IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port), | |
cd17f9ed | 253 | IP_VS_DBG_ADDR(svc->af, &snet)); |
1da177e4 LT |
254 | |
255 | /* | |
256 | * As far as we know, FTP is a very complicated network protocol, and | |
257 | * it uses control connection and data connections. For active FTP, | |
258 | * FTP server initialize data connection to the client, its source port | |
259 | * is often 20. For passive FTP, FTP server tells the clients the port | |
260 | * that it passively listens to, and the client issues the data | |
261 | * connection. In the tunneling or direct routing mode, the load | |
262 | * balancer is on the client-to-server half of connection, the port | |
263 | * number is unknown to the load balancer. So, a conn template like | |
264 | * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP | |
265 | * service, and a template like <caddr, 0, vaddr, vport, daddr, dport> | |
266 | * is created for other persistent services. | |
267 | */ | |
5b57a98c | 268 | { |
f11017ec SH |
269 | int protocol = iph.protocol; |
270 | const union nf_inet_addr *vaddr = &iph.daddr; | |
f11017ec SH |
271 | __be16 vport = 0; |
272 | ||
ce144f24 | 273 | if (dst_port == svc->port) { |
5b57a98c SH |
274 | /* non-FTP template: |
275 | * <protocol, caddr, 0, vaddr, vport, daddr, dport> | |
276 | * FTP template: | |
277 | * <protocol, caddr, 0, vaddr, 0, daddr, 0> | |
1da177e4 LT |
278 | */ |
279 | if (svc->port != FTPPORT) | |
ce144f24 | 280 | vport = dst_port; |
1da177e4 | 281 | } else { |
5b57a98c SH |
282 | /* Note: persistent fwmark-based services and |
283 | * persistent port zero service are handled here. | |
284 | * fwmark template: | |
285 | * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0> | |
286 | * port zero template: | |
287 | * <protocol,caddr,0,vaddr,0,daddr,0> | |
1da177e4 | 288 | */ |
28364a59 | 289 | if (svc->fwmark) { |
5b57a98c SH |
290 | protocol = IPPROTO_IP; |
291 | vaddr = &fwmark; | |
292 | } | |
1da177e4 | 293 | } |
a5959d53 HS |
294 | /* return *ignored = -1 so NF_DROP can be used */ |
295 | if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0, | |
296 | vaddr, vport, ¶m) < 0) { | |
297 | *ignored = -1; | |
298 | return NULL; | |
299 | } | |
1da177e4 LT |
300 | } |
301 | ||
5b57a98c | 302 | /* Check if a template already exists */ |
f11017ec | 303 | ct = ip_vs_ct_in_get(¶m); |
5b57a98c | 304 | if (!ct || !ip_vs_check_template(ct)) { |
a5959d53 HS |
305 | /* |
306 | * No template found or the dest of the connection | |
5b57a98c | 307 | * template is not available. |
a5959d53 | 308 | * return *ignored=0 i.e. ICMP and NF_DROP |
5b57a98c SH |
309 | */ |
310 | dest = svc->scheduler->schedule(svc, skb); | |
311 | if (!dest) { | |
312 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); | |
85999283 | 313 | kfree(param.pe_data); |
a5959d53 | 314 | *ignored = 0; |
5b57a98c SH |
315 | return NULL; |
316 | } | |
317 | ||
ce144f24 | 318 | if (dst_port == svc->port && svc->port != FTPPORT) |
5b57a98c SH |
319 | dport = dest->port; |
320 | ||
85999283 SH |
321 | /* Create a template |
322 | * This adds param.pe_data to the template, | |
323 | * and thus param.pe_data will be destroyed | |
324 | * when the template expires */ | |
f11017ec | 325 | ct = ip_vs_conn_new(¶m, &dest->addr, dport, |
0e051e68 | 326 | IP_VS_CONN_F_TEMPLATE, dest, skb->mark); |
85999283 SH |
327 | if (ct == NULL) { |
328 | kfree(param.pe_data); | |
a5959d53 | 329 | *ignored = -1; |
5b57a98c | 330 | return NULL; |
85999283 | 331 | } |
5b57a98c SH |
332 | |
333 | ct->timeout = svc->timeout; | |
85999283 | 334 | } else { |
5b57a98c SH |
335 | /* set destination with the found template */ |
336 | dest = ct->dest; | |
85999283 SH |
337 | kfree(param.pe_data); |
338 | } | |
5b57a98c | 339 | |
ce144f24 | 340 | dport = dst_port; |
5b57a98c SH |
341 | if (dport == svc->port && dest->port) |
342 | dport = dest->port; | |
343 | ||
26ec037f NC |
344 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
345 | && iph.protocol == IPPROTO_UDP)? | |
346 | IP_VS_CONN_F_ONE_PACKET : 0; | |
347 | ||
1da177e4 LT |
348 | /* |
349 | * Create a new connection according to the template | |
350 | */ | |
6e67e586 HS |
351 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr, |
352 | src_port, &iph.daddr, dst_port, ¶m); | |
ce144f24 | 353 | |
0e051e68 | 354 | cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark); |
1da177e4 LT |
355 | if (cp == NULL) { |
356 | ip_vs_conn_put(ct); | |
a5959d53 | 357 | *ignored = -1; |
1da177e4 LT |
358 | return NULL; |
359 | } | |
360 | ||
361 | /* | |
362 | * Add its control | |
363 | */ | |
364 | ip_vs_control_add(cp, ct); | |
365 | ip_vs_conn_put(ct); | |
366 | ||
367 | ip_vs_conn_stats(cp, svc); | |
368 | return cp; | |
369 | } | |
370 | ||
371 | ||
372 | /* | |
373 | * IPVS main scheduling function | |
374 | * It selects a server according to the virtual service, and | |
375 | * creates a connection entry. | |
376 | * Protocols supported: TCP, UDP | |
a5959d53 HS |
377 | * |
378 | * Usage of *ignored | |
379 | * | |
380 | * 1 : protocol tried to schedule (eg. on SYN), found svc but the | |
381 | * svc/scheduler decides that this packet should be accepted with | |
382 | * NF_ACCEPT because it must not be scheduled. | |
383 | * | |
384 | * 0 : scheduler can not find destination, so try bypass or | |
385 | * return ICMP and then NF_DROP (ip_vs_leave). | |
386 | * | |
387 | * -1 : scheduler tried to schedule but fatal error occurred, eg. | |
388 | * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param | |
389 | * failure such as missing Call-ID, ENOMEM on skb_linearize | |
390 | * or pe_data. In this case we should return NF_DROP without | |
391 | * any attempts to send ICMP with ip_vs_leave. | |
1da177e4 LT |
392 | */ |
393 | struct ip_vs_conn * | |
190ecd27 | 394 | ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, |
9330419d | 395 | struct ip_vs_proto_data *pd, int *ignored) |
1da177e4 | 396 | { |
9330419d | 397 | struct ip_vs_protocol *pp = pd->pp; |
1da177e4 | 398 | struct ip_vs_conn *cp = NULL; |
28364a59 | 399 | struct ip_vs_iphdr iph; |
1da177e4 | 400 | struct ip_vs_dest *dest; |
3575792e JA |
401 | __be16 _ports[2], *pptr; |
402 | unsigned int flags; | |
1da177e4 | 403 | |
190ecd27 | 404 | *ignored = 1; |
63dca2c0 | 405 | ip_vs_fill_iph_skb(svc->af, skb, &iph); |
28364a59 | 406 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); |
1da177e4 LT |
407 | if (pptr == NULL) |
408 | return NULL; | |
409 | ||
190ecd27 JA |
410 | /* |
411 | * FTPDATA needs this check when using local real server. | |
412 | * Never schedule Active FTPDATA connections from real server. | |
413 | * For LVS-NAT they must be already created. For other methods | |
414 | * with persistence the connection is created on SYN+ACK. | |
415 | */ | |
416 | if (pptr[0] == FTPDATA) { | |
0d79641a JA |
417 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
418 | "Not scheduling FTPDATA"); | |
190ecd27 JA |
419 | return NULL; |
420 | } | |
421 | ||
422 | /* | |
a5959d53 | 423 | * Do not schedule replies from local real server. |
190ecd27 JA |
424 | */ |
425 | if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) && | |
9330419d | 426 | (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) { |
0d79641a | 427 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
190ecd27 JA |
428 | "Not scheduling reply for existing connection"); |
429 | __ip_vs_conn_put(cp); | |
430 | return NULL; | |
431 | } | |
432 | ||
1da177e4 LT |
433 | /* |
434 | * Persistent service | |
435 | */ | |
a5959d53 HS |
436 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) |
437 | return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored); | |
438 | ||
439 | *ignored = 0; | |
1da177e4 LT |
440 | |
441 | /* | |
442 | * Non-persistent service | |
443 | */ | |
444 | if (!svc->fwmark && pptr[1] != svc->port) { | |
445 | if (!svc->port) | |
1e3e238e HE |
446 | pr_err("Schedule: port zero only supported " |
447 | "in persistent services, " | |
448 | "check your ipvs configuration\n"); | |
1da177e4 LT |
449 | return NULL; |
450 | } | |
451 | ||
452 | dest = svc->scheduler->schedule(svc, skb); | |
453 | if (dest == NULL) { | |
454 | IP_VS_DBG(1, "Schedule: no dest found.\n"); | |
455 | return NULL; | |
456 | } | |
457 | ||
26ec037f NC |
458 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
459 | && iph.protocol == IPPROTO_UDP)? | |
460 | IP_VS_CONN_F_ONE_PACKET : 0; | |
461 | ||
1da177e4 LT |
462 | /* |
463 | * Create a connection entry. | |
464 | */ | |
f11017ec SH |
465 | { |
466 | struct ip_vs_conn_param p; | |
6e67e586 HS |
467 | |
468 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, | |
469 | &iph.saddr, pptr[0], &iph.daddr, pptr[1], | |
470 | &p); | |
f11017ec SH |
471 | cp = ip_vs_conn_new(&p, &dest->addr, |
472 | dest->port ? dest->port : pptr[1], | |
0e051e68 | 473 | flags, dest, skb->mark); |
a5959d53 HS |
474 | if (!cp) { |
475 | *ignored = -1; | |
f11017ec | 476 | return NULL; |
a5959d53 | 477 | } |
f11017ec | 478 | } |
1da177e4 | 479 | |
cd17f9ed JV |
480 | IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u " |
481 | "d:%s:%u conn->flags:%X conn->refcnt:%d\n", | |
482 | ip_vs_fwd_tag(cp), | |
483 | IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport), | |
484 | IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport), | |
485 | IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport), | |
486 | cp->flags, atomic_read(&cp->refcnt)); | |
1da177e4 LT |
487 | |
488 | ip_vs_conn_stats(cp, svc); | |
489 | return cp; | |
490 | } | |
491 | ||
492 | ||
493 | /* | |
494 | * Pass or drop the packet. | |
495 | * Called by ip_vs_in, when the virtual service is available but | |
496 | * no destination is available for a new connection. | |
497 | */ | |
498 | int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, | |
9330419d | 499 | struct ip_vs_proto_data *pd) |
1da177e4 | 500 | { |
014d730d | 501 | __be16 _ports[2], *pptr; |
28364a59 | 502 | struct ip_vs_iphdr iph; |
a7a86b86 SH |
503 | #ifdef CONFIG_SYSCTL |
504 | struct net *net; | |
505 | struct netns_ipvs *ipvs; | |
2a3b791e | 506 | int unicast; |
a7a86b86 | 507 | #endif |
9330419d | 508 | |
63dca2c0 | 509 | ip_vs_fill_iph_skb(svc->af, skb, &iph); |
1da177e4 | 510 | |
28364a59 | 511 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); |
1da177e4 LT |
512 | if (pptr == NULL) { |
513 | ip_vs_service_put(svc); | |
514 | return NF_DROP; | |
515 | } | |
a7a86b86 SH |
516 | |
517 | #ifdef CONFIG_SYSCTL | |
4a98480b | 518 | net = skb_net(skb); |
1da177e4 | 519 | |
2a3b791e JV |
520 | #ifdef CONFIG_IP_VS_IPV6 |
521 | if (svc->af == AF_INET6) | |
522 | unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST; | |
523 | else | |
524 | #endif | |
4a98480b | 525 | unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST); |
2a3b791e | 526 | |
1da177e4 | 527 | /* if it is fwmark-based service, the cache_bypass sysctl is up |
2a3b791e | 528 | and the destination is a non-local unicast, then create |
1da177e4 | 529 | a cache_bypass connection entry */ |
4a98480b | 530 | ipvs = net_ipvs(net); |
a0840e2e | 531 | if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) { |
ad542ced | 532 | int ret; |
1da177e4 | 533 | struct ip_vs_conn *cp; |
3575792e JA |
534 | unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && |
535 | iph.protocol == IPPROTO_UDP)? | |
536 | IP_VS_CONN_F_ONE_PACKET : 0; | |
dff630dd | 537 | union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } }; |
1da177e4 LT |
538 | |
539 | ip_vs_service_put(svc); | |
540 | ||
541 | /* create a new connection entry */ | |
1e3e238e | 542 | IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__); |
f11017ec SH |
543 | { |
544 | struct ip_vs_conn_param p; | |
6e67e586 | 545 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, |
f11017ec SH |
546 | &iph.saddr, pptr[0], |
547 | &iph.daddr, pptr[1], &p); | |
548 | cp = ip_vs_conn_new(&p, &daddr, 0, | |
549 | IP_VS_CONN_F_BYPASS | flags, | |
0e051e68 | 550 | NULL, skb->mark); |
f11017ec SH |
551 | if (!cp) |
552 | return NF_DROP; | |
553 | } | |
1da177e4 LT |
554 | |
555 | /* statistics */ | |
556 | ip_vs_in_stats(cp, skb); | |
557 | ||
558 | /* set state */ | |
4a516f11 | 559 | ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 LT |
560 | |
561 | /* transmit the first SYN packet */ | |
9330419d | 562 | ret = cp->packet_xmit(skb, cp, pd->pp); |
1da177e4 LT |
563 | /* do not touch skb anymore */ |
564 | ||
565 | atomic_inc(&cp->in_pkts); | |
566 | ip_vs_conn_put(cp); | |
567 | return ret; | |
568 | } | |
a7a86b86 | 569 | #endif |
1da177e4 LT |
570 | |
571 | /* | |
572 | * When the virtual ftp service is presented, packets destined | |
573 | * for other services on the VIP may get here (except services | |
574 | * listed in the ipvs table), pass the packets, because it is | |
575 | * not ipvs job to decide to drop the packets. | |
576 | */ | |
577 | if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) { | |
578 | ip_vs_service_put(svc); | |
579 | return NF_ACCEPT; | |
580 | } | |
581 | ||
582 | ip_vs_service_put(svc); | |
583 | ||
584 | /* | |
585 | * Notify the client that the destination is unreachable, and | |
586 | * release the socket buffer. | |
587 | * Since it is in IP layer, the TCP socket is not actually | |
588 | * created, the TCP RST packet cannot be sent, instead that | |
589 | * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ | |
590 | */ | |
2a3b791e | 591 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
592 | if (svc->af == AF_INET6) { |
593 | if (!skb->dev) { | |
594 | struct net *net = dev_net(skb_dst(skb)->dev); | |
595 | ||
596 | skb->dev = net->loopback_dev; | |
597 | } | |
3ffe533c | 598 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
cb59155f | 599 | } else |
2a3b791e JV |
600 | #endif |
601 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | |
602 | ||
1da177e4 LT |
603 | return NF_DROP; |
604 | } | |
605 | ||
84b3cee3 SH |
606 | #ifdef CONFIG_SYSCTL |
607 | ||
608 | static int sysctl_snat_reroute(struct sk_buff *skb) | |
609 | { | |
610 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); | |
611 | return ipvs->sysctl_snat_reroute; | |
612 | } | |
613 | ||
0cfa558e SH |
614 | static int sysctl_nat_icmp_send(struct net *net) |
615 | { | |
616 | struct netns_ipvs *ipvs = net_ipvs(net); | |
617 | return ipvs->sysctl_nat_icmp_send; | |
618 | } | |
619 | ||
71a8ab6c SH |
620 | static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) |
621 | { | |
622 | return ipvs->sysctl_expire_nodest_conn; | |
623 | } | |
624 | ||
84b3cee3 SH |
625 | #else |
626 | ||
627 | static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; } | |
0cfa558e | 628 | static int sysctl_nat_icmp_send(struct net *net) { return 0; } |
71a8ab6c | 629 | static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; } |
84b3cee3 SH |
630 | |
631 | #endif | |
632 | ||
b1550f22 | 633 | __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset) |
1da177e4 | 634 | { |
d3bc23e7 | 635 | return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0)); |
1da177e4 LT |
636 | } |
637 | ||
1ca5bb54 JA |
638 | static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum) |
639 | { | |
640 | if (NF_INET_LOCAL_IN == hooknum) | |
641 | return IP_DEFRAG_VS_IN; | |
642 | if (NF_INET_FORWARD == hooknum) | |
643 | return IP_DEFRAG_VS_FWD; | |
644 | return IP_DEFRAG_VS_OUT; | |
645 | } | |
646 | ||
776c729e | 647 | static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user) |
1da177e4 | 648 | { |
776c729e HX |
649 | int err = ip_defrag(skb, user); |
650 | ||
651 | if (!err) | |
eddc9ec5 | 652 | ip_send_check(ip_hdr(skb)); |
776c729e HX |
653 | |
654 | return err; | |
1da177e4 LT |
655 | } |
656 | ||
2a3b791e JV |
657 | #ifdef CONFIG_IP_VS_IPV6 |
658 | static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user) | |
659 | { | |
660 | /* TODO IPv6: Find out what to do here for IPv6 */ | |
661 | return 0; | |
662 | } | |
663 | #endif | |
664 | ||
ba4fd7e9 SH |
665 | static int ip_vs_route_me_harder(int af, struct sk_buff *skb) |
666 | { | |
ba4fd7e9 SH |
667 | #ifdef CONFIG_IP_VS_IPV6 |
668 | if (af == AF_INET6) { | |
84b3cee3 | 669 | if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0) |
ba4fd7e9 SH |
670 | return 1; |
671 | } else | |
672 | #endif | |
84b3cee3 | 673 | if ((sysctl_snat_reroute(skb) || |
ba4fd7e9 SH |
674 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) && |
675 | ip_route_me_harder(skb, RTN_LOCAL) != 0) | |
676 | return 1; | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
1da177e4 LT |
681 | /* |
682 | * Packet has been made sufficiently writable in caller | |
683 | * - inout: 1=in->out, 0=out->in | |
684 | */ | |
685 | void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
686 | struct ip_vs_conn *cp, int inout) | |
687 | { | |
eddc9ec5 | 688 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 689 | unsigned int icmp_offset = iph->ihl*4; |
d56f90a7 ACM |
690 | struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) + |
691 | icmp_offset); | |
1da177e4 LT |
692 | struct iphdr *ciph = (struct iphdr *)(icmph + 1); |
693 | ||
694 | if (inout) { | |
e7ade46a | 695 | iph->saddr = cp->vaddr.ip; |
1da177e4 | 696 | ip_send_check(iph); |
e7ade46a | 697 | ciph->daddr = cp->vaddr.ip; |
1da177e4 LT |
698 | ip_send_check(ciph); |
699 | } else { | |
e7ade46a | 700 | iph->daddr = cp->daddr.ip; |
1da177e4 | 701 | ip_send_check(iph); |
e7ade46a | 702 | ciph->saddr = cp->daddr.ip; |
1da177e4 LT |
703 | ip_send_check(ciph); |
704 | } | |
705 | ||
2906f66a VMR |
706 | /* the TCP/UDP/SCTP port */ |
707 | if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol || | |
708 | IPPROTO_SCTP == ciph->protocol) { | |
014d730d | 709 | __be16 *ports = (void *)ciph + ciph->ihl*4; |
1da177e4 LT |
710 | |
711 | if (inout) | |
712 | ports[1] = cp->vport; | |
713 | else | |
714 | ports[0] = cp->dport; | |
715 | } | |
716 | ||
717 | /* And finally the ICMP checksum */ | |
718 | icmph->checksum = 0; | |
719 | icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset); | |
720 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
721 | ||
722 | if (inout) | |
0d79641a | 723 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
724 | "Forwarding altered outgoing ICMP"); |
725 | else | |
0d79641a | 726 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
727 | "Forwarding altered incoming ICMP"); |
728 | } | |
729 | ||
b3cdd2a7 JV |
730 | #ifdef CONFIG_IP_VS_IPV6 |
731 | void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
732 | struct ip_vs_conn *cp, int inout) | |
733 | { | |
734 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
63dca2c0 JDB |
735 | unsigned int icmp_offset = 0; |
736 | unsigned int offs = 0; /* header offset*/ | |
737 | int protocol; | |
738 | struct icmp6hdr *icmph; | |
739 | struct ipv6hdr *ciph; | |
740 | unsigned short fragoffs; | |
741 | ||
742 | ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL); | |
743 | icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset); | |
744 | offs = icmp_offset + sizeof(struct icmp6hdr); | |
745 | ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs); | |
746 | ||
747 | protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL); | |
b3cdd2a7 JV |
748 | |
749 | if (inout) { | |
750 | iph->saddr = cp->vaddr.in6; | |
751 | ciph->daddr = cp->vaddr.in6; | |
752 | } else { | |
753 | iph->daddr = cp->daddr.in6; | |
754 | ciph->saddr = cp->daddr.in6; | |
755 | } | |
756 | ||
2906f66a | 757 | /* the TCP/UDP/SCTP port */ |
63dca2c0 JDB |
758 | if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
759 | IPPROTO_SCTP == protocol)) { | |
760 | __be16 *ports = (void *)(skb_network_header(skb) + offs); | |
b3cdd2a7 | 761 | |
63dca2c0 JDB |
762 | IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__, |
763 | ntohs(inout ? ports[1] : ports[0]), | |
764 | ntohs(inout ? cp->vport : cp->dport)); | |
b3cdd2a7 JV |
765 | if (inout) |
766 | ports[1] = cp->vport; | |
767 | else | |
768 | ports[0] = cp->dport; | |
769 | } | |
770 | ||
771 | /* And finally the ICMP checksum */ | |
8870f842 SH |
772 | icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, |
773 | skb->len - icmp_offset, | |
774 | IPPROTO_ICMPV6, 0); | |
775 | skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset; | |
776 | skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); | |
777 | skb->ip_summed = CHECKSUM_PARTIAL; | |
b3cdd2a7 JV |
778 | |
779 | if (inout) | |
0d79641a JA |
780 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
781 | (void *)ciph - (void *)iph, | |
782 | "Forwarding altered outgoing ICMPv6"); | |
b3cdd2a7 | 783 | else |
0d79641a JA |
784 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
785 | (void *)ciph - (void *)iph, | |
786 | "Forwarding altered incoming ICMPv6"); | |
b3cdd2a7 JV |
787 | } |
788 | #endif | |
789 | ||
4856c84c | 790 | /* Handle relevant response ICMP messages - forward to the right |
6cb90db5 | 791 | * destination host. |
4856c84c | 792 | */ |
f2428ed5 SH |
793 | static int handle_response_icmp(int af, struct sk_buff *skb, |
794 | union nf_inet_addr *snet, | |
795 | __u8 protocol, struct ip_vs_conn *cp, | |
4856c84c MT |
796 | struct ip_vs_protocol *pp, |
797 | unsigned int offset, unsigned int ihl) | |
798 | { | |
799 | unsigned int verdict = NF_DROP; | |
800 | ||
801 | if (IP_VS_FWD_METHOD(cp) != 0) { | |
1e3e238e HE |
802 | pr_err("shouldn't reach here, because the box is on the " |
803 | "half connection in the tun/dr module.\n"); | |
4856c84c MT |
804 | } |
805 | ||
806 | /* Ensure the checksum is correct */ | |
807 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { | |
808 | /* Failed checksum! */ | |
f2428ed5 SH |
809 | IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n", |
810 | IP_VS_DBG_ADDR(af, snet)); | |
4856c84c MT |
811 | goto out; |
812 | } | |
813 | ||
2906f66a VMR |
814 | if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
815 | IPPROTO_SCTP == protocol) | |
4856c84c MT |
816 | offset += 2 * sizeof(__u16); |
817 | if (!skb_make_writable(skb, offset)) | |
818 | goto out; | |
819 | ||
f2428ed5 SH |
820 | #ifdef CONFIG_IP_VS_IPV6 |
821 | if (af == AF_INET6) | |
822 | ip_vs_nat_icmp_v6(skb, pp, cp, 1); | |
823 | else | |
824 | #endif | |
825 | ip_vs_nat_icmp(skb, pp, cp, 1); | |
4856c84c | 826 | |
ba4fd7e9 SH |
827 | if (ip_vs_route_me_harder(af, skb)) |
828 | goto out; | |
f5a41847 | 829 | |
4856c84c MT |
830 | /* do the statistics and put it back */ |
831 | ip_vs_out_stats(cp, skb); | |
832 | ||
cf356d69 | 833 | skb->ipvs_property = 1; |
f4bc17cd | 834 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 835 | ip_vs_notrack(skb); |
f4bc17cd JA |
836 | else |
837 | ip_vs_update_conntrack(skb, cp, 0); | |
4856c84c MT |
838 | verdict = NF_ACCEPT; |
839 | ||
840 | out: | |
841 | __ip_vs_conn_put(cp); | |
842 | ||
843 | return verdict; | |
844 | } | |
845 | ||
1da177e4 LT |
846 | /* |
847 | * Handle ICMP messages in the inside-to-outside direction (outgoing). | |
4856c84c | 848 | * Find any that might be relevant, check against existing connections. |
1da177e4 | 849 | * Currently handles error types - unreachable, quench, ttl exceeded. |
1da177e4 | 850 | */ |
1ca5bb54 JA |
851 | static int ip_vs_out_icmp(struct sk_buff *skb, int *related, |
852 | unsigned int hooknum) | |
1da177e4 | 853 | { |
1da177e4 LT |
854 | struct iphdr *iph; |
855 | struct icmphdr _icmph, *ic; | |
856 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 857 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
858 | struct ip_vs_conn *cp; |
859 | struct ip_vs_protocol *pp; | |
4856c84c | 860 | unsigned int offset, ihl; |
f2428ed5 | 861 | union nf_inet_addr snet; |
1da177e4 LT |
862 | |
863 | *related = 1; | |
864 | ||
865 | /* reassemble IP fragments */ | |
56f8a75c | 866 | if (ip_is_fragment(ip_hdr(skb))) { |
1ca5bb54 | 867 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 868 | return NF_STOLEN; |
1da177e4 LT |
869 | } |
870 | ||
eddc9ec5 | 871 | iph = ip_hdr(skb); |
1da177e4 LT |
872 | offset = ihl = iph->ihl * 4; |
873 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
874 | if (ic == NULL) | |
875 | return NF_DROP; | |
876 | ||
14d5e834 | 877 | IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 878 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 879 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
880 | |
881 | /* | |
882 | * Work through seeing if this is for us. | |
883 | * These checks are supposed to be in an order that means easy | |
884 | * things are checked first to speed up processing.... however | |
885 | * this means that some packets will manage to get a long way | |
886 | * down this stack and then be rejected, but that's life. | |
887 | */ | |
888 | if ((ic->type != ICMP_DEST_UNREACH) && | |
889 | (ic->type != ICMP_SOURCE_QUENCH) && | |
890 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
891 | *related = 0; | |
892 | return NF_ACCEPT; | |
893 | } | |
894 | ||
895 | /* Now find the contained IP header */ | |
896 | offset += sizeof(_icmph); | |
897 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
898 | if (cih == NULL) | |
899 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
900 | ||
901 | pp = ip_vs_proto_get(cih->protocol); | |
902 | if (!pp) | |
903 | return NF_ACCEPT; | |
904 | ||
905 | /* Is the embedded protocol header present? */ | |
4412ec49 | 906 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
907 | pp->dont_defrag)) |
908 | return NF_ACCEPT; | |
909 | ||
0d79641a JA |
910 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
911 | "Checking outgoing ICMP for"); | |
1da177e4 | 912 | |
63dca2c0 JDB |
913 | ip_vs_fill_ip4hdr(cih, &ciph); |
914 | ciph.len += offset; | |
1da177e4 | 915 | /* The embedded headers contain source and dest in reverse order */ |
9330419d | 916 | cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1); |
1da177e4 LT |
917 | if (!cp) |
918 | return NF_ACCEPT; | |
919 | ||
f2428ed5 SH |
920 | snet.ip = iph->saddr; |
921 | return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp, | |
63dca2c0 | 922 | pp, ciph.len, ihl); |
1da177e4 LT |
923 | } |
924 | ||
2a3b791e | 925 | #ifdef CONFIG_IP_VS_IPV6 |
1ca5bb54 JA |
926 | static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related, |
927 | unsigned int hooknum) | |
2a3b791e | 928 | { |
2a3b791e | 929 | struct icmp6hdr _icmph, *ic; |
63dca2c0 JDB |
930 | struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */ |
931 | struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */ | |
2a3b791e JV |
932 | struct ip_vs_conn *cp; |
933 | struct ip_vs_protocol *pp; | |
f2428ed5 | 934 | union nf_inet_addr snet; |
63dca2c0 | 935 | unsigned int writable; |
2a3b791e | 936 | |
63dca2c0 JDB |
937 | struct ip_vs_iphdr ipvsh_stack; |
938 | struct ip_vs_iphdr *ipvsh = &ipvsh_stack; | |
939 | ip_vs_fill_iph_skb(AF_INET6, skb, ipvsh); | |
2a3b791e | 940 | |
63dca2c0 | 941 | *related = 1; |
2a3b791e | 942 | |
63dca2c0 | 943 | ic = skb_header_pointer(skb, ipvsh->len, sizeof(_icmph), &_icmph); |
2a3b791e JV |
944 | if (ic == NULL) |
945 | return NF_DROP; | |
946 | ||
2a3b791e JV |
947 | /* |
948 | * Work through seeing if this is for us. | |
949 | * These checks are supposed to be in an order that means easy | |
950 | * things are checked first to speed up processing.... however | |
951 | * this means that some packets will manage to get a long way | |
952 | * down this stack and then be rejected, but that's life. | |
953 | */ | |
2fab8917 | 954 | if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) { |
2a3b791e JV |
955 | *related = 0; |
956 | return NF_ACCEPT; | |
957 | } | |
958 | ||
63dca2c0 JDB |
959 | IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n", |
960 | ic->icmp6_type, ntohs(icmpv6_id(ic)), | |
961 | &ipvsh->saddr, &ipvsh->daddr); | |
962 | ||
2a3b791e | 963 | /* Now find the contained IP header */ |
63dca2c0 JDB |
964 | ciph.len = ipvsh->len + sizeof(_icmph); |
965 | ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h); | |
966 | if (ip6h == NULL) | |
2a3b791e | 967 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
63dca2c0 JDB |
968 | ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */ |
969 | ciph.daddr.in6 = ip6h->daddr; | |
970 | /* skip possible IPv6 exthdrs of contained IPv6 packet */ | |
971 | ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL); | |
972 | if (ciph.protocol < 0) | |
973 | return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */ | |
974 | ||
975 | pp = ip_vs_proto_get(ciph.protocol); | |
2a3b791e JV |
976 | if (!pp) |
977 | return NF_ACCEPT; | |
978 | ||
2a3b791e | 979 | /* The embedded headers contain source and dest in reverse order */ |
63dca2c0 | 980 | cp = pp->conn_out_get(AF_INET6, skb, &ciph, ciph.len, 1); |
2a3b791e JV |
981 | if (!cp) |
982 | return NF_ACCEPT; | |
983 | ||
63dca2c0 JDB |
984 | snet.in6 = ciph.saddr.in6; |
985 | writable = ciph.len; | |
986 | return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp, | |
987 | pp, writable, sizeof(struct ipv6hdr)); | |
2a3b791e JV |
988 | } |
989 | #endif | |
990 | ||
2906f66a VMR |
991 | /* |
992 | * Check if sctp chunc is ABORT chunk | |
993 | */ | |
994 | static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len) | |
995 | { | |
996 | sctp_chunkhdr_t *sch, schunk; | |
997 | sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t), | |
998 | sizeof(schunk), &schunk); | |
999 | if (sch == NULL) | |
1000 | return 0; | |
1001 | if (sch->type == SCTP_CID_ABORT) | |
1002 | return 1; | |
1003 | return 0; | |
1004 | } | |
1005 | ||
2a3b791e | 1006 | static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len) |
1da177e4 LT |
1007 | { |
1008 | struct tcphdr _tcph, *th; | |
1009 | ||
2a3b791e | 1010 | th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph); |
1da177e4 LT |
1011 | if (th == NULL) |
1012 | return 0; | |
1013 | return th->rst; | |
1014 | } | |
1015 | ||
4856c84c | 1016 | /* Handle response packets: rewrite addresses and send away... |
4856c84c MT |
1017 | */ |
1018 | static unsigned int | |
9330419d | 1019 | handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
4856c84c MT |
1020 | struct ip_vs_conn *cp, int ihl) |
1021 | { | |
9330419d HS |
1022 | struct ip_vs_protocol *pp = pd->pp; |
1023 | ||
0d79641a | 1024 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet"); |
4856c84c MT |
1025 | |
1026 | if (!skb_make_writable(skb, ihl)) | |
1027 | goto drop; | |
1028 | ||
1029 | /* mangle the packet */ | |
1030 | if (pp->snat_handler && !pp->snat_handler(skb, pp, cp)) | |
1031 | goto drop; | |
1032 | ||
1033 | #ifdef CONFIG_IP_VS_IPV6 | |
1034 | if (af == AF_INET6) | |
1035 | ipv6_hdr(skb)->saddr = cp->vaddr.in6; | |
1036 | else | |
1037 | #endif | |
1038 | { | |
1039 | ip_hdr(skb)->saddr = cp->vaddr.ip; | |
1040 | ip_send_check(ip_hdr(skb)); | |
1041 | } | |
1042 | ||
8a803040 JA |
1043 | /* |
1044 | * nf_iterate does not expect change in the skb->dst->dev. | |
1045 | * It looks like it is not fatal to enable this code for hooks | |
1046 | * where our handlers are at the end of the chain list and | |
1047 | * when all next handlers use skb->dst->dev and not outdev. | |
1048 | * It will definitely route properly the inout NAT traffic | |
1049 | * when multiple paths are used. | |
1050 | */ | |
1051 | ||
4856c84c MT |
1052 | /* For policy routing, packets originating from this |
1053 | * machine itself may be routed differently to packets | |
1054 | * passing through. We want this packet to be routed as | |
1055 | * if it came from this machine itself. So re-compute | |
1056 | * the routing information. | |
1057 | */ | |
ba4fd7e9 SH |
1058 | if (ip_vs_route_me_harder(af, skb)) |
1059 | goto drop; | |
4856c84c | 1060 | |
0d79641a | 1061 | IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT"); |
4856c84c MT |
1062 | |
1063 | ip_vs_out_stats(cp, skb); | |
9330419d | 1064 | ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd); |
cf356d69 | 1065 | skb->ipvs_property = 1; |
f4bc17cd | 1066 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 1067 | ip_vs_notrack(skb); |
f4bc17cd JA |
1068 | else |
1069 | ip_vs_update_conntrack(skb, cp, 0); | |
4856c84c MT |
1070 | ip_vs_conn_put(cp); |
1071 | ||
4856c84c MT |
1072 | LeaveFunction(11); |
1073 | return NF_ACCEPT; | |
1074 | ||
1075 | drop: | |
1076 | ip_vs_conn_put(cp); | |
1077 | kfree_skb(skb); | |
f4bc17cd | 1078 | LeaveFunction(11); |
4856c84c MT |
1079 | return NF_STOLEN; |
1080 | } | |
1081 | ||
1da177e4 | 1082 | /* |
4856c84c | 1083 | * Check if outgoing packet belongs to the established ip_vs_conn. |
1da177e4 LT |
1084 | */ |
1085 | static unsigned int | |
fc604767 | 1086 | ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1087 | { |
fc723250 | 1088 | struct net *net = NULL; |
51ef348b | 1089 | struct ip_vs_iphdr iph; |
1da177e4 | 1090 | struct ip_vs_protocol *pp; |
9330419d | 1091 | struct ip_vs_proto_data *pd; |
1da177e4 | 1092 | struct ip_vs_conn *cp; |
1da177e4 LT |
1093 | |
1094 | EnterFunction(11); | |
1095 | ||
fc604767 | 1096 | /* Already marked as IPVS request or reply? */ |
6869c4d8 | 1097 | if (skb->ipvs_property) |
1da177e4 LT |
1098 | return NF_ACCEPT; |
1099 | ||
fc604767 JA |
1100 | /* Bad... Do not break raw sockets */ |
1101 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && | |
1102 | af == AF_INET)) { | |
1103 | struct sock *sk = skb->sk; | |
1104 | struct inet_sock *inet = inet_sk(skb->sk); | |
1105 | ||
1106 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) | |
1107 | return NF_ACCEPT; | |
1108 | } | |
1109 | ||
1110 | if (unlikely(!skb_dst(skb))) | |
1111 | return NF_ACCEPT; | |
1112 | ||
fc723250 | 1113 | net = skb_net(skb); |
7a4f0761 HS |
1114 | if (!net_ipvs(net)->enable) |
1115 | return NF_ACCEPT; | |
1116 | ||
63dca2c0 | 1117 | ip_vs_fill_iph_skb(af, skb, &iph); |
2a3b791e JV |
1118 | #ifdef CONFIG_IP_VS_IPV6 |
1119 | if (af == AF_INET6) { | |
1120 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 JA |
1121 | int related; |
1122 | int verdict = ip_vs_out_icmp_v6(skb, &related, | |
1123 | hooknum); | |
1da177e4 | 1124 | |
f5a41847 | 1125 | if (related) |
2a3b791e | 1126 | return verdict; |
63dca2c0 | 1127 | ip_vs_fill_iph_skb(af, skb, &iph); |
2a3b791e JV |
1128 | } |
1129 | } else | |
1130 | #endif | |
1131 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 JA |
1132 | int related; |
1133 | int verdict = ip_vs_out_icmp(skb, &related, hooknum); | |
2a3b791e | 1134 | |
f5a41847 | 1135 | if (related) |
2a3b791e | 1136 | return verdict; |
63dca2c0 | 1137 | ip_vs_fill_ip4hdr(skb_network_header(skb), &iph); |
2a3b791e | 1138 | } |
1da177e4 | 1139 | |
9330419d HS |
1140 | pd = ip_vs_proto_data_get(net, iph.protocol); |
1141 | if (unlikely(!pd)) | |
1da177e4 | 1142 | return NF_ACCEPT; |
9330419d | 1143 | pp = pd->pp; |
1da177e4 LT |
1144 | |
1145 | /* reassemble IP fragments */ | |
2a3b791e | 1146 | #ifdef CONFIG_IP_VS_IPV6 |
63dca2c0 | 1147 | if (af == AF_INET) |
2a3b791e | 1148 | #endif |
56f8a75c | 1149 | if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) { |
1ca5bb54 JA |
1150 | if (ip_vs_gather_frags(skb, |
1151 | ip_vs_defrag_user(hooknum))) | |
2a3b791e JV |
1152 | return NF_STOLEN; |
1153 | ||
63dca2c0 | 1154 | ip_vs_fill_ip4hdr(skb_network_header(skb), &iph); |
2a3b791e | 1155 | } |
1da177e4 LT |
1156 | |
1157 | /* | |
1158 | * Check if the packet belongs to an existing entry | |
1159 | */ | |
9330419d | 1160 | cp = pp->conn_out_get(af, skb, &iph, iph.len, 0); |
1da177e4 | 1161 | |
cb59155f | 1162 | if (likely(cp)) |
9330419d | 1163 | return handle_response(af, skb, pd, cp, iph.len); |
0cfa558e | 1164 | if (sysctl_nat_icmp_send(net) && |
cb59155f JA |
1165 | (pp->protocol == IPPROTO_TCP || |
1166 | pp->protocol == IPPROTO_UDP || | |
1167 | pp->protocol == IPPROTO_SCTP)) { | |
1168 | __be16 _ports[2], *pptr; | |
1169 | ||
1170 | pptr = skb_header_pointer(skb, iph.len, | |
1171 | sizeof(_ports), _ports); | |
1172 | if (pptr == NULL) | |
1173 | return NF_ACCEPT; /* Not for me */ | |
fc723250 | 1174 | if (ip_vs_lookup_real_service(net, af, iph.protocol, |
cb59155f JA |
1175 | &iph.saddr, |
1176 | pptr[0])) { | |
1177 | /* | |
1178 | * Notify the real server: there is no | |
1179 | * existing entry if it is not RST | |
1180 | * packet or not TCP packet. | |
1181 | */ | |
1182 | if ((iph.protocol != IPPROTO_TCP && | |
1183 | iph.protocol != IPPROTO_SCTP) | |
1184 | || ((iph.protocol == IPPROTO_TCP | |
1185 | && !is_tcp_reset(skb, iph.len)) | |
1186 | || (iph.protocol == IPPROTO_SCTP | |
1187 | && !is_sctp_abort(skb, | |
1188 | iph.len)))) { | |
2a3b791e | 1189 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
1190 | if (af == AF_INET6) { |
1191 | struct net *net = | |
1192 | dev_net(skb_dst(skb)->dev); | |
1193 | ||
1194 | if (!skb->dev) | |
1195 | skb->dev = net->loopback_dev; | |
1196 | icmpv6_send(skb, | |
1197 | ICMPV6_DEST_UNREACH, | |
1198 | ICMPV6_PORT_UNREACH, | |
1199 | 0); | |
1200 | } else | |
2a3b791e | 1201 | #endif |
cb59155f JA |
1202 | icmp_send(skb, |
1203 | ICMP_DEST_UNREACH, | |
1204 | ICMP_PORT_UNREACH, 0); | |
1205 | return NF_DROP; | |
1da177e4 LT |
1206 | } |
1207 | } | |
1da177e4 | 1208 | } |
0d79641a | 1209 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
cb59155f JA |
1210 | "ip_vs_out: packet continues traversal as normal"); |
1211 | return NF_ACCEPT; | |
1da177e4 LT |
1212 | } |
1213 | ||
fc604767 | 1214 | /* |
cb59155f JA |
1215 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1216 | * used only for VS/NAT. | |
fc604767 JA |
1217 | * Check if packet is reply for established ip_vs_conn. |
1218 | */ | |
1219 | static unsigned int | |
1220 | ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb, | |
1221 | const struct net_device *in, const struct net_device *out, | |
1222 | int (*okfn)(struct sk_buff *)) | |
1223 | { | |
1224 | return ip_vs_out(hooknum, skb, AF_INET); | |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1229 | * Check if packet is reply for established ip_vs_conn. | |
1230 | */ | |
1231 | static unsigned int | |
1232 | ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb, | |
1233 | const struct net_device *in, const struct net_device *out, | |
1234 | int (*okfn)(struct sk_buff *)) | |
1235 | { | |
1236 | unsigned int verdict; | |
1237 | ||
1238 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1239 | local_bh_disable(); | |
1240 | verdict = ip_vs_out(hooknum, skb, AF_INET); | |
1241 | local_bh_enable(); | |
1242 | return verdict; | |
1243 | } | |
1244 | ||
1245 | #ifdef CONFIG_IP_VS_IPV6 | |
1246 | ||
1247 | /* | |
cb59155f JA |
1248 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1249 | * used only for VS/NAT. | |
fc604767 JA |
1250 | * Check if packet is reply for established ip_vs_conn. |
1251 | */ | |
1252 | static unsigned int | |
1253 | ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb, | |
1254 | const struct net_device *in, const struct net_device *out, | |
1255 | int (*okfn)(struct sk_buff *)) | |
1256 | { | |
1257 | return ip_vs_out(hooknum, skb, AF_INET6); | |
1258 | } | |
1259 | ||
1260 | /* | |
1261 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1262 | * Check if packet is reply for established ip_vs_conn. | |
1263 | */ | |
1264 | static unsigned int | |
1265 | ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb, | |
1266 | const struct net_device *in, const struct net_device *out, | |
1267 | int (*okfn)(struct sk_buff *)) | |
1268 | { | |
1269 | unsigned int verdict; | |
1270 | ||
1271 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1272 | local_bh_disable(); | |
1273 | verdict = ip_vs_out(hooknum, skb, AF_INET6); | |
1274 | local_bh_enable(); | |
1275 | return verdict; | |
1276 | } | |
1277 | ||
1278 | #endif | |
1da177e4 LT |
1279 | |
1280 | /* | |
1281 | * Handle ICMP messages in the outside-to-inside direction (incoming). | |
1282 | * Find any that might be relevant, check against existing connections, | |
1283 | * forward to the right destination host if relevant. | |
1284 | * Currently handles error types - unreachable, quench, ttl exceeded. | |
1285 | */ | |
e905a9ed | 1286 | static int |
3db05fea | 1287 | ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum) |
1da177e4 | 1288 | { |
9330419d | 1289 | struct net *net = NULL; |
1da177e4 LT |
1290 | struct iphdr *iph; |
1291 | struct icmphdr _icmph, *ic; | |
1292 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 1293 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
1294 | struct ip_vs_conn *cp; |
1295 | struct ip_vs_protocol *pp; | |
9330419d | 1296 | struct ip_vs_proto_data *pd; |
f2edb9f7 JA |
1297 | unsigned int offset, offset2, ihl, verdict; |
1298 | bool ipip; | |
1da177e4 LT |
1299 | |
1300 | *related = 1; | |
1301 | ||
1302 | /* reassemble IP fragments */ | |
56f8a75c | 1303 | if (ip_is_fragment(ip_hdr(skb))) { |
1ca5bb54 | 1304 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 1305 | return NF_STOLEN; |
1da177e4 LT |
1306 | } |
1307 | ||
eddc9ec5 | 1308 | iph = ip_hdr(skb); |
1da177e4 LT |
1309 | offset = ihl = iph->ihl * 4; |
1310 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
1311 | if (ic == NULL) | |
1312 | return NF_DROP; | |
1313 | ||
14d5e834 | 1314 | IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 1315 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 1316 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
1317 | |
1318 | /* | |
1319 | * Work through seeing if this is for us. | |
1320 | * These checks are supposed to be in an order that means easy | |
1321 | * things are checked first to speed up processing.... however | |
1322 | * this means that some packets will manage to get a long way | |
1323 | * down this stack and then be rejected, but that's life. | |
1324 | */ | |
1325 | if ((ic->type != ICMP_DEST_UNREACH) && | |
1326 | (ic->type != ICMP_SOURCE_QUENCH) && | |
1327 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
1328 | *related = 0; | |
1329 | return NF_ACCEPT; | |
1330 | } | |
1331 | ||
1332 | /* Now find the contained IP header */ | |
1333 | offset += sizeof(_icmph); | |
1334 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1335 | if (cih == NULL) | |
1336 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
1337 | ||
9330419d | 1338 | net = skb_net(skb); |
7a4f0761 | 1339 | |
f2edb9f7 JA |
1340 | /* Special case for errors for IPIP packets */ |
1341 | ipip = false; | |
1342 | if (cih->protocol == IPPROTO_IPIP) { | |
1343 | if (unlikely(cih->frag_off & htons(IP_OFFSET))) | |
1344 | return NF_ACCEPT; | |
1345 | /* Error for our IPIP must arrive at LOCAL_IN */ | |
1346 | if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL)) | |
1347 | return NF_ACCEPT; | |
1348 | offset += cih->ihl * 4; | |
1349 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1350 | if (cih == NULL) | |
1351 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
1352 | ipip = true; | |
1353 | } | |
1354 | ||
9330419d HS |
1355 | pd = ip_vs_proto_data_get(net, cih->protocol); |
1356 | if (!pd) | |
1da177e4 | 1357 | return NF_ACCEPT; |
9330419d | 1358 | pp = pd->pp; |
1da177e4 LT |
1359 | |
1360 | /* Is the embedded protocol header present? */ | |
4412ec49 | 1361 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
1362 | pp->dont_defrag)) |
1363 | return NF_ACCEPT; | |
1364 | ||
0d79641a JA |
1365 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
1366 | "Checking incoming ICMP for"); | |
1da177e4 | 1367 | |
f2edb9f7 | 1368 | offset2 = offset; |
63dca2c0 JDB |
1369 | ip_vs_fill_ip4hdr(cih, &ciph); |
1370 | ciph.len += offset; | |
1371 | offset = ciph.len; | |
f2edb9f7 JA |
1372 | /* The embedded headers contain source and dest in reverse order. |
1373 | * For IPIP this is error for request, not for reply. | |
1374 | */ | |
1375 | cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, ipip ? 0 : 1); | |
6cb90db5 | 1376 | if (!cp) |
1da177e4 LT |
1377 | return NF_ACCEPT; |
1378 | ||
1379 | verdict = NF_DROP; | |
1380 | ||
1381 | /* Ensure the checksum is correct */ | |
60476372 | 1382 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { |
1da177e4 | 1383 | /* Failed checksum! */ |
14d5e834 HH |
1384 | IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n", |
1385 | &iph->saddr); | |
1da177e4 LT |
1386 | goto out; |
1387 | } | |
1388 | ||
f2edb9f7 JA |
1389 | if (ipip) { |
1390 | __be32 info = ic->un.gateway; | |
1391 | ||
1392 | /* Update the MTU */ | |
1393 | if (ic->type == ICMP_DEST_UNREACH && | |
1394 | ic->code == ICMP_FRAG_NEEDED) { | |
1395 | struct ip_vs_dest *dest = cp->dest; | |
1396 | u32 mtu = ntohs(ic->un.frag.mtu); | |
1397 | ||
1398 | /* Strip outer IP and ICMP, go to IPIP header */ | |
1399 | __skb_pull(skb, ihl + sizeof(_icmph)); | |
1400 | offset2 -= ihl + sizeof(_icmph); | |
1401 | skb_reset_network_header(skb); | |
1402 | IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n", | |
1403 | &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu); | |
1404 | rcu_read_lock(); | |
1405 | ipv4_update_pmtu(skb, dev_net(skb->dev), | |
1406 | mtu, 0, 0, 0, 0); | |
1407 | rcu_read_unlock(); | |
1408 | /* Client uses PMTUD? */ | |
1409 | if (!(cih->frag_off & htons(IP_DF))) | |
1410 | goto ignore_ipip; | |
1411 | /* Prefer the resulting PMTU */ | |
1412 | if (dest) { | |
1413 | spin_lock(&dest->dst_lock); | |
1414 | if (dest->dst_cache) | |
1415 | mtu = dst_mtu(dest->dst_cache); | |
1416 | spin_unlock(&dest->dst_lock); | |
1417 | } | |
1418 | if (mtu > 68 + sizeof(struct iphdr)) | |
1419 | mtu -= sizeof(struct iphdr); | |
1420 | info = htonl(mtu); | |
1421 | } | |
1422 | /* Strip outer IP, ICMP and IPIP, go to IP header of | |
1423 | * original request. | |
1424 | */ | |
1425 | __skb_pull(skb, offset2); | |
1426 | skb_reset_network_header(skb); | |
1427 | IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n", | |
1428 | &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, | |
1429 | ic->type, ic->code, ntohl(info)); | |
1430 | icmp_send(skb, ic->type, ic->code, info); | |
1431 | /* ICMP can be shorter but anyways, account it */ | |
1432 | ip_vs_out_stats(cp, skb); | |
1433 | ||
1434 | ignore_ipip: | |
1435 | consume_skb(skb); | |
1436 | verdict = NF_STOLEN; | |
1437 | goto out; | |
1438 | } | |
1439 | ||
1da177e4 LT |
1440 | /* do the statistics and put it back */ |
1441 | ip_vs_in_stats(cp, skb); | |
1442 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol) | |
1443 | offset += 2 * sizeof(__u16); | |
c92f5ca2 | 1444 | verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum); |
1da177e4 | 1445 | |
552ad65a | 1446 | out: |
1da177e4 LT |
1447 | __ip_vs_conn_put(cp); |
1448 | ||
1449 | return verdict; | |
1450 | } | |
1451 | ||
2a3b791e JV |
1452 | #ifdef CONFIG_IP_VS_IPV6 |
1453 | static int | |
1454 | ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum) | |
1455 | { | |
9330419d | 1456 | struct net *net = NULL; |
63dca2c0 | 1457 | struct ipv6hdr _ip6h, *ip6h; |
2a3b791e | 1458 | struct icmp6hdr _icmph, *ic; |
63dca2c0 | 1459 | struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */ |
2a3b791e JV |
1460 | struct ip_vs_conn *cp; |
1461 | struct ip_vs_protocol *pp; | |
9330419d | 1462 | struct ip_vs_proto_data *pd; |
63dca2c0 | 1463 | unsigned int offs_ciph, writable, verdict; |
2a3b791e | 1464 | |
63dca2c0 JDB |
1465 | struct ip_vs_iphdr iph_stack; |
1466 | struct ip_vs_iphdr *iph = &iph_stack; | |
1467 | ip_vs_fill_iph_skb(AF_INET6, skb, iph); | |
2a3b791e | 1468 | |
63dca2c0 | 1469 | *related = 1; |
2a3b791e | 1470 | |
63dca2c0 | 1471 | ic = skb_header_pointer(skb, iph->len, sizeof(_icmph), &_icmph); |
2a3b791e JV |
1472 | if (ic == NULL) |
1473 | return NF_DROP; | |
1474 | ||
2a3b791e JV |
1475 | /* |
1476 | * Work through seeing if this is for us. | |
1477 | * These checks are supposed to be in an order that means easy | |
1478 | * things are checked first to speed up processing.... however | |
1479 | * this means that some packets will manage to get a long way | |
1480 | * down this stack and then be rejected, but that's life. | |
1481 | */ | |
2fab8917 | 1482 | if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) { |
2a3b791e JV |
1483 | *related = 0; |
1484 | return NF_ACCEPT; | |
1485 | } | |
1486 | ||
63dca2c0 JDB |
1487 | IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n", |
1488 | ic->icmp6_type, ntohs(icmpv6_id(ic)), | |
1489 | &iph->saddr, &iph->daddr); | |
1490 | ||
2a3b791e | 1491 | /* Now find the contained IP header */ |
63dca2c0 JDB |
1492 | ciph.len = iph->len + sizeof(_icmph); |
1493 | offs_ciph = ciph.len; /* Save ip header offset */ | |
1494 | ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h); | |
1495 | if (ip6h == NULL) | |
2a3b791e | 1496 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
63dca2c0 JDB |
1497 | ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */ |
1498 | ciph.daddr.in6 = ip6h->daddr; | |
1499 | /* skip possible IPv6 exthdrs of contained IPv6 packet */ | |
1500 | ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL); | |
1501 | if (ciph.protocol < 0) | |
1502 | return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */ | |
2a3b791e | 1503 | |
9330419d | 1504 | net = skb_net(skb); |
63dca2c0 | 1505 | pd = ip_vs_proto_data_get(net, ciph.protocol); |
9330419d | 1506 | if (!pd) |
2a3b791e | 1507 | return NF_ACCEPT; |
9330419d | 1508 | pp = pd->pp; |
2a3b791e | 1509 | |
63dca2c0 JDB |
1510 | /* Cannot handle fragmented embedded protocol */ |
1511 | if (ciph.fragoffs) | |
2a3b791e JV |
1512 | return NF_ACCEPT; |
1513 | ||
63dca2c0 | 1514 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph, |
0d79641a | 1515 | "Checking incoming ICMPv6 for"); |
2a3b791e | 1516 | |
2a3b791e | 1517 | /* The embedded headers contain source and dest in reverse order */ |
63dca2c0 | 1518 | cp = pp->conn_in_get(AF_INET6, skb, &ciph, ciph.len, 1); |
6cb90db5 | 1519 | if (!cp) |
2a3b791e JV |
1520 | return NF_ACCEPT; |
1521 | ||
2a3b791e JV |
1522 | /* do the statistics and put it back */ |
1523 | ip_vs_in_stats(cp, skb); | |
63dca2c0 JDB |
1524 | |
1525 | /* Need to mangle contained IPv6 header in ICMPv6 packet */ | |
1526 | writable = ciph.len; | |
1527 | if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol || | |
1528 | IPPROTO_SCTP == ciph.protocol) | |
1529 | writable += 2 * sizeof(__u16); /* Also mangle ports */ | |
1530 | ||
1531 | verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum); | |
2a3b791e JV |
1532 | |
1533 | __ip_vs_conn_put(cp); | |
1534 | ||
1535 | return verdict; | |
1536 | } | |
1537 | #endif | |
1538 | ||
1539 | ||
1da177e4 LT |
1540 | /* |
1541 | * Check if it's for virtual services, look it up, | |
1542 | * and send it on its way... | |
1543 | */ | |
1544 | static unsigned int | |
cb59155f | 1545 | ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1546 | { |
f131315f | 1547 | struct net *net; |
51ef348b | 1548 | struct ip_vs_iphdr iph; |
1da177e4 | 1549 | struct ip_vs_protocol *pp; |
9330419d | 1550 | struct ip_vs_proto_data *pd; |
1da177e4 | 1551 | struct ip_vs_conn *cp; |
4a516f11 | 1552 | int ret, pkts; |
f131315f | 1553 | struct netns_ipvs *ipvs; |
2a3b791e | 1554 | |
fc604767 JA |
1555 | /* Already marked as IPVS request or reply? */ |
1556 | if (skb->ipvs_property) | |
1557 | return NF_ACCEPT; | |
1558 | ||
1da177e4 | 1559 | /* |
cb59155f JA |
1560 | * Big tappo: |
1561 | * - remote client: only PACKET_HOST | |
1562 | * - route: used for struct net when skb->dev is unset | |
1da177e4 | 1563 | */ |
cb59155f JA |
1564 | if (unlikely((skb->pkt_type != PACKET_HOST && |
1565 | hooknum != NF_INET_LOCAL_OUT) || | |
1566 | !skb_dst(skb))) { | |
63dca2c0 | 1567 | ip_vs_fill_iph_skb(af, skb, &iph); |
cb59155f JA |
1568 | IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s" |
1569 | " ignored in hook %u\n", | |
1570 | skb->pkt_type, iph.protocol, | |
1571 | IP_VS_DBG_ADDR(af, &iph.daddr), hooknum); | |
1da177e4 LT |
1572 | return NF_ACCEPT; |
1573 | } | |
7a4f0761 HS |
1574 | /* ipvs enabled in this netns ? */ |
1575 | net = skb_net(skb); | |
1576 | if (!net_ipvs(net)->enable) | |
1577 | return NF_ACCEPT; | |
1578 | ||
63dca2c0 | 1579 | ip_vs_fill_iph_skb(af, skb, &iph); |
cb59155f JA |
1580 | |
1581 | /* Bad... Do not break raw sockets */ | |
1582 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && | |
1583 | af == AF_INET)) { | |
1584 | struct sock *sk = skb->sk; | |
1585 | struct inet_sock *inet = inet_sk(skb->sk); | |
1586 | ||
1587 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) | |
1588 | return NF_ACCEPT; | |
1589 | } | |
1da177e4 | 1590 | |
94b26551 JV |
1591 | #ifdef CONFIG_IP_VS_IPV6 |
1592 | if (af == AF_INET6) { | |
1593 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 JA |
1594 | int related; |
1595 | int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum); | |
1da177e4 | 1596 | |
94b26551 JV |
1597 | if (related) |
1598 | return verdict; | |
94b26551 JV |
1599 | } |
1600 | } else | |
1601 | #endif | |
1602 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 JA |
1603 | int related; |
1604 | int verdict = ip_vs_in_icmp(skb, &related, hooknum); | |
94b26551 JV |
1605 | |
1606 | if (related) | |
1607 | return verdict; | |
94b26551 | 1608 | } |
1da177e4 LT |
1609 | |
1610 | /* Protocol supported? */ | |
9330419d HS |
1611 | pd = ip_vs_proto_data_get(net, iph.protocol); |
1612 | if (unlikely(!pd)) | |
1da177e4 | 1613 | return NF_ACCEPT; |
9330419d | 1614 | pp = pd->pp; |
1da177e4 LT |
1615 | /* |
1616 | * Check if the packet belongs to an existing connection entry | |
63dca2c0 | 1617 | * Only sched first IPv6 fragment. |
1da177e4 | 1618 | */ |
9330419d | 1619 | cp = pp->conn_in_get(af, skb, &iph, iph.len, 0); |
1da177e4 | 1620 | |
63dca2c0 | 1621 | if (unlikely(!cp) && !iph.fragoffs) { |
1da177e4 LT |
1622 | int v; |
1623 | ||
9330419d | 1624 | if (!pp->conn_schedule(af, skb, pd, &v, &cp)) |
1da177e4 LT |
1625 | return v; |
1626 | } | |
1627 | ||
1628 | if (unlikely(!cp)) { | |
1629 | /* sorry, all this trouble for a no-hit :) */ | |
0d79641a | 1630 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
cb59155f | 1631 | "ip_vs_in: packet continues traversal as normal"); |
1da177e4 LT |
1632 | return NF_ACCEPT; |
1633 | } | |
1634 | ||
0d79641a | 1635 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet"); |
f131315f | 1636 | ipvs = net_ipvs(net); |
1da177e4 LT |
1637 | /* Check the server status */ |
1638 | if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
1639 | /* the destination server is not available */ | |
1640 | ||
71a8ab6c | 1641 | if (sysctl_expire_nodest_conn(ipvs)) { |
1da177e4 LT |
1642 | /* try to expire the connection immediately */ |
1643 | ip_vs_conn_expire_now(cp); | |
1da177e4 | 1644 | } |
dc8103f2 JA |
1645 | /* don't restart its timer, and silently |
1646 | drop the packet. */ | |
1647 | __ip_vs_conn_put(cp); | |
1da177e4 LT |
1648 | return NF_DROP; |
1649 | } | |
1650 | ||
1651 | ip_vs_in_stats(cp, skb); | |
4a516f11 | 1652 | ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 LT |
1653 | if (cp->packet_xmit) |
1654 | ret = cp->packet_xmit(skb, cp, pp); | |
1655 | /* do not touch skb anymore */ | |
1656 | else { | |
1657 | IP_VS_DBG_RL("warning: packet_xmit is null"); | |
1658 | ret = NF_ACCEPT; | |
1659 | } | |
1660 | ||
efac5276 RB |
1661 | /* Increase its packet counter and check if it is needed |
1662 | * to be synchronized | |
1663 | * | |
1664 | * Sync connection if it is about to close to | |
1665 | * encorage the standby servers to update the connections timeout | |
986a0757 HS |
1666 | * |
1667 | * For ONE_PKT let ip_vs_sync_conn() do the filter work. | |
efac5276 | 1668 | */ |
f131315f | 1669 | |
986a0757 | 1670 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
59e0350e | 1671 | pkts = sysctl_sync_threshold(ipvs); |
986a0757 HS |
1672 | else |
1673 | pkts = atomic_add_return(1, &cp->in_pkts); | |
1674 | ||
749c42b6 JA |
1675 | if (ipvs->sync_state & IP_VS_STATE_MASTER) |
1676 | ip_vs_sync_conn(net, cp, pkts); | |
1da177e4 LT |
1677 | |
1678 | ip_vs_conn_put(cp); | |
1679 | return ret; | |
1680 | } | |
1681 | ||
cb59155f JA |
1682 | /* |
1683 | * AF_INET handler in NF_INET_LOCAL_IN chain | |
1684 | * Schedule and forward packets from remote clients | |
1685 | */ | |
1686 | static unsigned int | |
1687 | ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb, | |
1688 | const struct net_device *in, | |
1689 | const struct net_device *out, | |
1690 | int (*okfn)(struct sk_buff *)) | |
1691 | { | |
1692 | return ip_vs_in(hooknum, skb, AF_INET); | |
1693 | } | |
1694 | ||
1695 | /* | |
1696 | * AF_INET handler in NF_INET_LOCAL_OUT chain | |
1697 | * Schedule and forward packets from local clients | |
1698 | */ | |
1699 | static unsigned int | |
1700 | ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb, | |
1701 | const struct net_device *in, const struct net_device *out, | |
1702 | int (*okfn)(struct sk_buff *)) | |
1703 | { | |
1704 | unsigned int verdict; | |
1705 | ||
1706 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1707 | local_bh_disable(); | |
1708 | verdict = ip_vs_in(hooknum, skb, AF_INET); | |
1709 | local_bh_enable(); | |
1710 | return verdict; | |
1711 | } | |
1712 | ||
1713 | #ifdef CONFIG_IP_VS_IPV6 | |
1714 | ||
1715 | /* | |
1716 | * AF_INET6 handler in NF_INET_LOCAL_IN chain | |
1717 | * Schedule and forward packets from remote clients | |
1718 | */ | |
1719 | static unsigned int | |
1720 | ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb, | |
1721 | const struct net_device *in, | |
1722 | const struct net_device *out, | |
1723 | int (*okfn)(struct sk_buff *)) | |
1724 | { | |
1725 | return ip_vs_in(hooknum, skb, AF_INET6); | |
1726 | } | |
1727 | ||
1728 | /* | |
1729 | * AF_INET6 handler in NF_INET_LOCAL_OUT chain | |
1730 | * Schedule and forward packets from local clients | |
1731 | */ | |
1732 | static unsigned int | |
1733 | ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb, | |
1734 | const struct net_device *in, const struct net_device *out, | |
1735 | int (*okfn)(struct sk_buff *)) | |
1736 | { | |
1737 | unsigned int verdict; | |
1738 | ||
1739 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1740 | local_bh_disable(); | |
1741 | verdict = ip_vs_in(hooknum, skb, AF_INET6); | |
1742 | local_bh_enable(); | |
1743 | return verdict; | |
1744 | } | |
1745 | ||
1746 | #endif | |
1747 | ||
1da177e4 LT |
1748 | |
1749 | /* | |
6e23ae2a | 1750 | * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP |
1da177e4 LT |
1751 | * related packets destined for 0.0.0.0/0. |
1752 | * When fwmark-based virtual service is used, such as transparent | |
1753 | * cache cluster, TCP packets can be marked and routed to ip_vs_in, | |
1754 | * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and | |
6e23ae2a | 1755 | * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain |
1da177e4 LT |
1756 | * and send them to ip_vs_in_icmp. |
1757 | */ | |
1758 | static unsigned int | |
3db05fea | 1759 | ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb, |
1da177e4 LT |
1760 | const struct net_device *in, const struct net_device *out, |
1761 | int (*okfn)(struct sk_buff *)) | |
1762 | { | |
1763 | int r; | |
7a4f0761 | 1764 | struct net *net; |
1da177e4 | 1765 | |
3db05fea | 1766 | if (ip_hdr(skb)->protocol != IPPROTO_ICMP) |
1da177e4 LT |
1767 | return NF_ACCEPT; |
1768 | ||
7a4f0761 HS |
1769 | /* ipvs enabled in this netns ? */ |
1770 | net = skb_net(skb); | |
1771 | if (!net_ipvs(net)->enable) | |
1772 | return NF_ACCEPT; | |
1773 | ||
3db05fea | 1774 | return ip_vs_in_icmp(skb, &r, hooknum); |
1da177e4 LT |
1775 | } |
1776 | ||
2a3b791e JV |
1777 | #ifdef CONFIG_IP_VS_IPV6 |
1778 | static unsigned int | |
1779 | ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb, | |
1780 | const struct net_device *in, const struct net_device *out, | |
1781 | int (*okfn)(struct sk_buff *)) | |
1782 | { | |
1783 | int r; | |
7a4f0761 | 1784 | struct net *net; |
63dca2c0 | 1785 | struct ip_vs_iphdr iphdr; |
2a3b791e | 1786 | |
63dca2c0 JDB |
1787 | ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr); |
1788 | if (iphdr.protocol != IPPROTO_ICMPV6) | |
2a3b791e JV |
1789 | return NF_ACCEPT; |
1790 | ||
7a4f0761 HS |
1791 | /* ipvs enabled in this netns ? */ |
1792 | net = skb_net(skb); | |
1793 | if (!net_ipvs(net)->enable) | |
1794 | return NF_ACCEPT; | |
1795 | ||
2a3b791e JV |
1796 | return ip_vs_in_icmp_v6(skb, &r, hooknum); |
1797 | } | |
1798 | #endif | |
1799 | ||
1da177e4 | 1800 | |
1999414a | 1801 | static struct nf_hook_ops ip_vs_ops[] __read_mostly = { |
cb59155f JA |
1802 | /* After packet filtering, change source only for VS/NAT */ |
1803 | { | |
1804 | .hook = ip_vs_reply4, | |
1805 | .owner = THIS_MODULE, | |
4c809d63 | 1806 | .pf = NFPROTO_IPV4, |
cb59155f | 1807 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 1808 | .priority = NF_IP_PRI_NAT_SRC - 2, |
cb59155f | 1809 | }, |
41c5b317 PM |
1810 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
1811 | * or VS/NAT(change destination), so that filtering rules can be | |
1812 | * applied to IPVS. */ | |
1813 | { | |
cb59155f | 1814 | .hook = ip_vs_remote_request4, |
41c5b317 | 1815 | .owner = THIS_MODULE, |
4c809d63 | 1816 | .pf = NFPROTO_IPV4, |
cb59155f | 1817 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 1818 | .priority = NF_IP_PRI_NAT_SRC - 1, |
41c5b317 | 1819 | }, |
fc604767 | 1820 | /* Before ip_vs_in, change source only for VS/NAT */ |
41c5b317 | 1821 | { |
fc604767 | 1822 | .hook = ip_vs_local_reply4, |
41c5b317 | 1823 | .owner = THIS_MODULE, |
4c809d63 | 1824 | .pf = NFPROTO_IPV4, |
fc604767 | 1825 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 1826 | .priority = NF_IP_PRI_NAT_DST + 1, |
41c5b317 | 1827 | }, |
cb59155f JA |
1828 | /* After mangle, schedule and forward local requests */ |
1829 | { | |
1830 | .hook = ip_vs_local_request4, | |
1831 | .owner = THIS_MODULE, | |
4c809d63 | 1832 | .pf = NFPROTO_IPV4, |
cb59155f | 1833 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 1834 | .priority = NF_IP_PRI_NAT_DST + 2, |
cb59155f | 1835 | }, |
41c5b317 PM |
1836 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
1837 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
1838 | { | |
1839 | .hook = ip_vs_forward_icmp, | |
1840 | .owner = THIS_MODULE, | |
4c809d63 | 1841 | .pf = NFPROTO_IPV4, |
cb59155f JA |
1842 | .hooknum = NF_INET_FORWARD, |
1843 | .priority = 99, | |
41c5b317 | 1844 | }, |
fc604767 JA |
1845 | /* After packet filtering, change source only for VS/NAT */ |
1846 | { | |
1847 | .hook = ip_vs_reply4, | |
1848 | .owner = THIS_MODULE, | |
4c809d63 | 1849 | .pf = NFPROTO_IPV4, |
fc604767 JA |
1850 | .hooknum = NF_INET_FORWARD, |
1851 | .priority = 100, | |
1852 | }, | |
473b23d3 | 1853 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
1854 | /* After packet filtering, change source only for VS/NAT */ |
1855 | { | |
1856 | .hook = ip_vs_reply6, | |
1857 | .owner = THIS_MODULE, | |
4c809d63 | 1858 | .pf = NFPROTO_IPV6, |
cb59155f | 1859 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 1860 | .priority = NF_IP6_PRI_NAT_SRC - 2, |
cb59155f | 1861 | }, |
473b23d3 JV |
1862 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
1863 | * or VS/NAT(change destination), so that filtering rules can be | |
1864 | * applied to IPVS. */ | |
1865 | { | |
cb59155f | 1866 | .hook = ip_vs_remote_request6, |
473b23d3 | 1867 | .owner = THIS_MODULE, |
4c809d63 | 1868 | .pf = NFPROTO_IPV6, |
cb59155f | 1869 | .hooknum = NF_INET_LOCAL_IN, |
afb523c5 | 1870 | .priority = NF_IP6_PRI_NAT_SRC - 1, |
473b23d3 | 1871 | }, |
fc604767 | 1872 | /* Before ip_vs_in, change source only for VS/NAT */ |
473b23d3 | 1873 | { |
fc604767 | 1874 | .hook = ip_vs_local_reply6, |
473b23d3 | 1875 | .owner = THIS_MODULE, |
4c809d63 | 1876 | .pf = NFPROTO_IPV4, |
fc604767 | 1877 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 1878 | .priority = NF_IP6_PRI_NAT_DST + 1, |
473b23d3 | 1879 | }, |
cb59155f JA |
1880 | /* After mangle, schedule and forward local requests */ |
1881 | { | |
1882 | .hook = ip_vs_local_request6, | |
1883 | .owner = THIS_MODULE, | |
4c809d63 | 1884 | .pf = NFPROTO_IPV6, |
cb59155f | 1885 | .hooknum = NF_INET_LOCAL_OUT, |
afb523c5 | 1886 | .priority = NF_IP6_PRI_NAT_DST + 2, |
cb59155f | 1887 | }, |
473b23d3 JV |
1888 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
1889 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
1890 | { | |
1891 | .hook = ip_vs_forward_icmp_v6, | |
1892 | .owner = THIS_MODULE, | |
4c809d63 | 1893 | .pf = NFPROTO_IPV6, |
cb59155f JA |
1894 | .hooknum = NF_INET_FORWARD, |
1895 | .priority = 99, | |
473b23d3 | 1896 | }, |
fc604767 JA |
1897 | /* After packet filtering, change source only for VS/NAT */ |
1898 | { | |
1899 | .hook = ip_vs_reply6, | |
1900 | .owner = THIS_MODULE, | |
4c809d63 | 1901 | .pf = NFPROTO_IPV6, |
fc604767 JA |
1902 | .hooknum = NF_INET_FORWARD, |
1903 | .priority = 100, | |
1904 | }, | |
473b23d3 | 1905 | #endif |
1da177e4 | 1906 | }; |
61b1ab45 HS |
1907 | /* |
1908 | * Initialize IP Virtual Server netns mem. | |
1909 | */ | |
1910 | static int __net_init __ip_vs_init(struct net *net) | |
1911 | { | |
1912 | struct netns_ipvs *ipvs; | |
1913 | ||
61b1ab45 | 1914 | ipvs = net_generic(net, ip_vs_net_id); |
0a9ee813 | 1915 | if (ipvs == NULL) |
61b1ab45 | 1916 | return -ENOMEM; |
0a9ee813 | 1917 | |
7a4f0761 HS |
1918 | /* Hold the beast until a service is registerd */ |
1919 | ipvs->enable = 0; | |
f6340ee0 | 1920 | ipvs->net = net; |
61b1ab45 HS |
1921 | /* Counters used for creating unique names */ |
1922 | ipvs->gen = atomic_read(&ipvs_netns_cnt); | |
1923 | atomic_inc(&ipvs_netns_cnt); | |
1924 | net->ipvs = ipvs; | |
7a4f0761 | 1925 | |
503cf15a | 1926 | if (ip_vs_estimator_net_init(net) < 0) |
7a4f0761 HS |
1927 | goto estimator_fail; |
1928 | ||
503cf15a | 1929 | if (ip_vs_control_net_init(net) < 0) |
7a4f0761 HS |
1930 | goto control_fail; |
1931 | ||
503cf15a | 1932 | if (ip_vs_protocol_net_init(net) < 0) |
7a4f0761 HS |
1933 | goto protocol_fail; |
1934 | ||
503cf15a | 1935 | if (ip_vs_app_net_init(net) < 0) |
7a4f0761 HS |
1936 | goto app_fail; |
1937 | ||
503cf15a | 1938 | if (ip_vs_conn_net_init(net) < 0) |
7a4f0761 HS |
1939 | goto conn_fail; |
1940 | ||
503cf15a | 1941 | if (ip_vs_sync_net_init(net) < 0) |
7a4f0761 HS |
1942 | goto sync_fail; |
1943 | ||
a870c8c5 | 1944 | printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n", |
61b1ab45 HS |
1945 | sizeof(struct netns_ipvs), ipvs->gen); |
1946 | return 0; | |
7a4f0761 HS |
1947 | /* |
1948 | * Error handling | |
1949 | */ | |
1950 | ||
1951 | sync_fail: | |
503cf15a | 1952 | ip_vs_conn_net_cleanup(net); |
7a4f0761 | 1953 | conn_fail: |
503cf15a | 1954 | ip_vs_app_net_cleanup(net); |
7a4f0761 | 1955 | app_fail: |
503cf15a | 1956 | ip_vs_protocol_net_cleanup(net); |
7a4f0761 | 1957 | protocol_fail: |
503cf15a | 1958 | ip_vs_control_net_cleanup(net); |
7a4f0761 | 1959 | control_fail: |
503cf15a | 1960 | ip_vs_estimator_net_cleanup(net); |
7a4f0761 | 1961 | estimator_fail: |
39f618b4 | 1962 | net->ipvs = NULL; |
7a4f0761 | 1963 | return -ENOMEM; |
61b1ab45 HS |
1964 | } |
1965 | ||
1966 | static void __net_exit __ip_vs_cleanup(struct net *net) | |
1967 | { | |
503cf15a HS |
1968 | ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */ |
1969 | ip_vs_conn_net_cleanup(net); | |
1970 | ip_vs_app_net_cleanup(net); | |
1971 | ip_vs_protocol_net_cleanup(net); | |
1972 | ip_vs_control_net_cleanup(net); | |
1973 | ip_vs_estimator_net_cleanup(net); | |
1ae132b0 | 1974 | IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen); |
39f618b4 | 1975 | net->ipvs = NULL; |
61b1ab45 HS |
1976 | } |
1977 | ||
7a4f0761 HS |
1978 | static void __net_exit __ip_vs_dev_cleanup(struct net *net) |
1979 | { | |
1980 | EnterFunction(2); | |
1981 | net_ipvs(net)->enable = 0; /* Disable packet reception */ | |
8f4e0a18 | 1982 | smp_wmb(); |
503cf15a | 1983 | ip_vs_sync_net_cleanup(net); |
7a4f0761 HS |
1984 | LeaveFunction(2); |
1985 | } | |
1986 | ||
61b1ab45 HS |
1987 | static struct pernet_operations ipvs_core_ops = { |
1988 | .init = __ip_vs_init, | |
1989 | .exit = __ip_vs_cleanup, | |
1990 | .id = &ip_vs_net_id, | |
1991 | .size = sizeof(struct netns_ipvs), | |
1992 | }; | |
1da177e4 | 1993 | |
7a4f0761 HS |
1994 | static struct pernet_operations ipvs_core_dev_ops = { |
1995 | .exit = __ip_vs_dev_cleanup, | |
1996 | }; | |
1997 | ||
1da177e4 LT |
1998 | /* |
1999 | * Initialize IP Virtual Server | |
2000 | */ | |
2001 | static int __init ip_vs_init(void) | |
2002 | { | |
2003 | int ret; | |
2004 | ||
2005 | ret = ip_vs_control_init(); | |
2006 | if (ret < 0) { | |
1e3e238e | 2007 | pr_err("can't setup control.\n"); |
6c8f7949 | 2008 | goto exit; |
1da177e4 LT |
2009 | } |
2010 | ||
2011 | ip_vs_protocol_init(); | |
2012 | ||
1da177e4 LT |
2013 | ret = ip_vs_conn_init(); |
2014 | if (ret < 0) { | |
1e3e238e | 2015 | pr_err("can't setup connection table.\n"); |
6c8f7949 | 2016 | goto cleanup_protocol; |
61b1ab45 HS |
2017 | } |
2018 | ||
7a4f0761 HS |
2019 | ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */ |
2020 | if (ret < 0) | |
6c8f7949 | 2021 | goto cleanup_conn; |
7a4f0761 HS |
2022 | |
2023 | ret = register_pernet_device(&ipvs_core_dev_ops); | |
2024 | if (ret < 0) | |
2025 | goto cleanup_sub; | |
2026 | ||
41c5b317 | 2027 | ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
1da177e4 | 2028 | if (ret < 0) { |
1e3e238e | 2029 | pr_err("can't register hooks.\n"); |
7a4f0761 | 2030 | goto cleanup_dev; |
1da177e4 LT |
2031 | } |
2032 | ||
8537de8a HS |
2033 | ret = ip_vs_register_nl_ioctl(); |
2034 | if (ret < 0) { | |
2035 | pr_err("can't register netlink/ioctl.\n"); | |
2036 | goto cleanup_hooks; | |
2037 | } | |
2038 | ||
1e3e238e | 2039 | pr_info("ipvs loaded.\n"); |
7a4f0761 | 2040 | |
1da177e4 LT |
2041 | return ret; |
2042 | ||
8537de8a HS |
2043 | cleanup_hooks: |
2044 | nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); | |
7a4f0761 HS |
2045 | cleanup_dev: |
2046 | unregister_pernet_device(&ipvs_core_dev_ops); | |
2047 | cleanup_sub: | |
2048 | unregister_pernet_subsys(&ipvs_core_ops); | |
552ad65a | 2049 | cleanup_conn: |
1da177e4 | 2050 | ip_vs_conn_cleanup(); |
552ad65a | 2051 | cleanup_protocol: |
1da177e4 LT |
2052 | ip_vs_protocol_cleanup(); |
2053 | ip_vs_control_cleanup(); | |
6c8f7949 | 2054 | exit: |
1da177e4 LT |
2055 | return ret; |
2056 | } | |
2057 | ||
2058 | static void __exit ip_vs_cleanup(void) | |
2059 | { | |
8537de8a | 2060 | ip_vs_unregister_nl_ioctl(); |
41c5b317 | 2061 | nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
7a4f0761 HS |
2062 | unregister_pernet_device(&ipvs_core_dev_ops); |
2063 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ | |
1da177e4 | 2064 | ip_vs_conn_cleanup(); |
1da177e4 LT |
2065 | ip_vs_protocol_cleanup(); |
2066 | ip_vs_control_cleanup(); | |
1e3e238e | 2067 | pr_info("ipvs unloaded.\n"); |
1da177e4 LT |
2068 | } |
2069 | ||
2070 | module_init(ip_vs_init); | |
2071 | module_exit(ip_vs_cleanup); | |
2072 | MODULE_LICENSE("GPL"); |