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 LT |
82 | |
83 | const char *ip_vs_proto_name(unsigned proto) | |
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 | ||
191 | static inline int | |
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 | { |
9330419d | 196 | if (unlikely(!pd->pp->state_transition)) |
1da177e4 | 197 | return 0; |
9330419d | 198 | return pd->pp->state_transition(cp, direction, skb, pd); |
1da177e4 LT |
199 | } |
200 | ||
a5959d53 | 201 | static inline int |
85999283 SH |
202 | ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc, |
203 | struct sk_buff *skb, int protocol, | |
204 | const union nf_inet_addr *caddr, __be16 cport, | |
205 | const union nf_inet_addr *vaddr, __be16 vport, | |
206 | struct ip_vs_conn_param *p) | |
207 | { | |
6e67e586 HS |
208 | ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr, |
209 | vport, p); | |
85999283 SH |
210 | p->pe = svc->pe; |
211 | if (p->pe && p->pe->fill_param) | |
a5959d53 HS |
212 | return p->pe->fill_param(p, skb); |
213 | ||
214 | return 0; | |
85999283 | 215 | } |
1da177e4 | 216 | |
1da177e4 LT |
217 | /* |
218 | * IPVS persistent scheduling function | |
219 | * It creates a connection entry according to its template if exists, | |
220 | * or selects a server and creates a connection entry plus a template. | |
221 | * Locking: we are svc user (svc->refcnt), so we hold all dests too | |
222 | * Protocols supported: TCP, UDP | |
223 | */ | |
224 | static struct ip_vs_conn * | |
225 | ip_vs_sched_persist(struct ip_vs_service *svc, | |
85999283 | 226 | struct sk_buff *skb, |
a5959d53 | 227 | __be16 src_port, __be16 dst_port, int *ignored) |
1da177e4 LT |
228 | { |
229 | struct ip_vs_conn *cp = NULL; | |
28364a59 | 230 | struct ip_vs_iphdr iph; |
1da177e4 LT |
231 | struct ip_vs_dest *dest; |
232 | struct ip_vs_conn *ct; | |
5b57a98c | 233 | __be16 dport = 0; /* destination port to forward */ |
3575792e | 234 | unsigned int flags; |
f11017ec | 235 | struct ip_vs_conn_param param; |
28364a59 JV |
236 | union nf_inet_addr snet; /* source network of the client, |
237 | after masking */ | |
cd17f9ed JV |
238 | |
239 | ip_vs_fill_iphdr(svc->af, skb_network_header(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; | |
271 | const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) }; | |
272 | __be16 vport = 0; | |
273 | ||
ce144f24 | 274 | if (dst_port == svc->port) { |
5b57a98c SH |
275 | /* non-FTP template: |
276 | * <protocol, caddr, 0, vaddr, vport, daddr, dport> | |
277 | * FTP template: | |
278 | * <protocol, caddr, 0, vaddr, 0, daddr, 0> | |
1da177e4 LT |
279 | */ |
280 | if (svc->port != FTPPORT) | |
ce144f24 | 281 | vport = dst_port; |
1da177e4 | 282 | } else { |
5b57a98c SH |
283 | /* Note: persistent fwmark-based services and |
284 | * persistent port zero service are handled here. | |
285 | * fwmark template: | |
286 | * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0> | |
287 | * port zero template: | |
288 | * <protocol,caddr,0,vaddr,0,daddr,0> | |
1da177e4 | 289 | */ |
28364a59 | 290 | if (svc->fwmark) { |
5b57a98c SH |
291 | protocol = IPPROTO_IP; |
292 | vaddr = &fwmark; | |
293 | } | |
1da177e4 | 294 | } |
a5959d53 HS |
295 | /* return *ignored = -1 so NF_DROP can be used */ |
296 | if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0, | |
297 | vaddr, vport, ¶m) < 0) { | |
298 | *ignored = -1; | |
299 | return NULL; | |
300 | } | |
1da177e4 LT |
301 | } |
302 | ||
5b57a98c | 303 | /* Check if a template already exists */ |
f11017ec | 304 | ct = ip_vs_ct_in_get(¶m); |
5b57a98c | 305 | if (!ct || !ip_vs_check_template(ct)) { |
a5959d53 HS |
306 | /* |
307 | * No template found or the dest of the connection | |
5b57a98c | 308 | * template is not available. |
a5959d53 | 309 | * return *ignored=0 i.e. ICMP and NF_DROP |
5b57a98c SH |
310 | */ |
311 | dest = svc->scheduler->schedule(svc, skb); | |
312 | if (!dest) { | |
313 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); | |
85999283 | 314 | kfree(param.pe_data); |
a5959d53 | 315 | *ignored = 0; |
5b57a98c SH |
316 | return NULL; |
317 | } | |
318 | ||
ce144f24 | 319 | if (dst_port == svc->port && svc->port != FTPPORT) |
5b57a98c SH |
320 | dport = dest->port; |
321 | ||
85999283 SH |
322 | /* Create a template |
323 | * This adds param.pe_data to the template, | |
324 | * and thus param.pe_data will be destroyed | |
325 | * when the template expires */ | |
f11017ec | 326 | ct = ip_vs_conn_new(¶m, &dest->addr, dport, |
0e051e68 | 327 | IP_VS_CONN_F_TEMPLATE, dest, skb->mark); |
85999283 SH |
328 | if (ct == NULL) { |
329 | kfree(param.pe_data); | |
a5959d53 | 330 | *ignored = -1; |
5b57a98c | 331 | return NULL; |
85999283 | 332 | } |
5b57a98c SH |
333 | |
334 | ct->timeout = svc->timeout; | |
85999283 | 335 | } else { |
5b57a98c SH |
336 | /* set destination with the found template */ |
337 | dest = ct->dest; | |
85999283 SH |
338 | kfree(param.pe_data); |
339 | } | |
5b57a98c | 340 | |
ce144f24 | 341 | dport = dst_port; |
5b57a98c SH |
342 | if (dport == svc->port && dest->port) |
343 | dport = dest->port; | |
344 | ||
26ec037f NC |
345 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
346 | && iph.protocol == IPPROTO_UDP)? | |
347 | IP_VS_CONN_F_ONE_PACKET : 0; | |
348 | ||
1da177e4 LT |
349 | /* |
350 | * Create a new connection according to the template | |
351 | */ | |
6e67e586 HS |
352 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr, |
353 | src_port, &iph.daddr, dst_port, ¶m); | |
ce144f24 | 354 | |
0e051e68 | 355 | cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark); |
1da177e4 LT |
356 | if (cp == NULL) { |
357 | ip_vs_conn_put(ct); | |
a5959d53 | 358 | *ignored = -1; |
1da177e4 LT |
359 | return NULL; |
360 | } | |
361 | ||
362 | /* | |
363 | * Add its control | |
364 | */ | |
365 | ip_vs_control_add(cp, ct); | |
366 | ip_vs_conn_put(ct); | |
367 | ||
368 | ip_vs_conn_stats(cp, svc); | |
369 | return cp; | |
370 | } | |
371 | ||
372 | ||
373 | /* | |
374 | * IPVS main scheduling function | |
375 | * It selects a server according to the virtual service, and | |
376 | * creates a connection entry. | |
377 | * Protocols supported: TCP, UDP | |
a5959d53 HS |
378 | * |
379 | * Usage of *ignored | |
380 | * | |
381 | * 1 : protocol tried to schedule (eg. on SYN), found svc but the | |
382 | * svc/scheduler decides that this packet should be accepted with | |
383 | * NF_ACCEPT because it must not be scheduled. | |
384 | * | |
385 | * 0 : scheduler can not find destination, so try bypass or | |
386 | * return ICMP and then NF_DROP (ip_vs_leave). | |
387 | * | |
388 | * -1 : scheduler tried to schedule but fatal error occurred, eg. | |
389 | * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param | |
390 | * failure such as missing Call-ID, ENOMEM on skb_linearize | |
391 | * or pe_data. In this case we should return NF_DROP without | |
392 | * any attempts to send ICMP with ip_vs_leave. | |
1da177e4 LT |
393 | */ |
394 | struct ip_vs_conn * | |
190ecd27 | 395 | ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, |
9330419d | 396 | struct ip_vs_proto_data *pd, int *ignored) |
1da177e4 | 397 | { |
9330419d | 398 | struct ip_vs_protocol *pp = pd->pp; |
1da177e4 | 399 | struct ip_vs_conn *cp = NULL; |
28364a59 | 400 | struct ip_vs_iphdr iph; |
1da177e4 | 401 | struct ip_vs_dest *dest; |
3575792e JA |
402 | __be16 _ports[2], *pptr; |
403 | unsigned int flags; | |
1da177e4 | 404 | |
190ecd27 | 405 | *ignored = 1; |
28364a59 JV |
406 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
407 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); | |
1da177e4 LT |
408 | if (pptr == NULL) |
409 | return NULL; | |
410 | ||
190ecd27 JA |
411 | /* |
412 | * FTPDATA needs this check when using local real server. | |
413 | * Never schedule Active FTPDATA connections from real server. | |
414 | * For LVS-NAT they must be already created. For other methods | |
415 | * with persistence the connection is created on SYN+ACK. | |
416 | */ | |
417 | if (pptr[0] == FTPDATA) { | |
0d79641a JA |
418 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
419 | "Not scheduling FTPDATA"); | |
190ecd27 JA |
420 | return NULL; |
421 | } | |
422 | ||
423 | /* | |
a5959d53 | 424 | * Do not schedule replies from local real server. |
190ecd27 JA |
425 | */ |
426 | if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) && | |
9330419d | 427 | (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) { |
0d79641a | 428 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
190ecd27 JA |
429 | "Not scheduling reply for existing connection"); |
430 | __ip_vs_conn_put(cp); | |
431 | return NULL; | |
432 | } | |
433 | ||
1da177e4 LT |
434 | /* |
435 | * Persistent service | |
436 | */ | |
a5959d53 HS |
437 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) |
438 | return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored); | |
439 | ||
440 | *ignored = 0; | |
1da177e4 LT |
441 | |
442 | /* | |
443 | * Non-persistent service | |
444 | */ | |
445 | if (!svc->fwmark && pptr[1] != svc->port) { | |
446 | if (!svc->port) | |
1e3e238e HE |
447 | pr_err("Schedule: port zero only supported " |
448 | "in persistent services, " | |
449 | "check your ipvs configuration\n"); | |
1da177e4 LT |
450 | return NULL; |
451 | } | |
452 | ||
453 | dest = svc->scheduler->schedule(svc, skb); | |
454 | if (dest == NULL) { | |
455 | IP_VS_DBG(1, "Schedule: no dest found.\n"); | |
456 | return NULL; | |
457 | } | |
458 | ||
26ec037f NC |
459 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
460 | && iph.protocol == IPPROTO_UDP)? | |
461 | IP_VS_CONN_F_ONE_PACKET : 0; | |
462 | ||
1da177e4 LT |
463 | /* |
464 | * Create a connection entry. | |
465 | */ | |
f11017ec SH |
466 | { |
467 | struct ip_vs_conn_param p; | |
6e67e586 HS |
468 | |
469 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, | |
470 | &iph.saddr, pptr[0], &iph.daddr, pptr[1], | |
471 | &p); | |
f11017ec SH |
472 | cp = ip_vs_conn_new(&p, &dest->addr, |
473 | dest->port ? dest->port : pptr[1], | |
0e051e68 | 474 | flags, dest, skb->mark); |
a5959d53 HS |
475 | if (!cp) { |
476 | *ignored = -1; | |
f11017ec | 477 | return NULL; |
a5959d53 | 478 | } |
f11017ec | 479 | } |
1da177e4 | 480 | |
cd17f9ed JV |
481 | IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u " |
482 | "d:%s:%u conn->flags:%X conn->refcnt:%d\n", | |
483 | ip_vs_fwd_tag(cp), | |
484 | IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport), | |
485 | IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport), | |
486 | IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport), | |
487 | cp->flags, atomic_read(&cp->refcnt)); | |
1da177e4 LT |
488 | |
489 | ip_vs_conn_stats(cp, svc); | |
490 | return cp; | |
491 | } | |
492 | ||
493 | ||
494 | /* | |
495 | * Pass or drop the packet. | |
496 | * Called by ip_vs_in, when the virtual service is available but | |
497 | * no destination is available for a new connection. | |
498 | */ | |
499 | int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, | |
9330419d | 500 | struct ip_vs_proto_data *pd) |
1da177e4 | 501 | { |
4a98480b | 502 | struct net *net; |
a0840e2e | 503 | struct netns_ipvs *ipvs; |
014d730d | 504 | __be16 _ports[2], *pptr; |
28364a59 | 505 | struct ip_vs_iphdr iph; |
2a3b791e | 506 | int unicast; |
9330419d | 507 | |
2a3b791e | 508 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
1da177e4 | 509 | |
28364a59 | 510 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); |
1da177e4 LT |
511 | if (pptr == NULL) { |
512 | ip_vs_service_put(svc); | |
513 | return NF_DROP; | |
514 | } | |
4a98480b | 515 | net = skb_net(skb); |
1da177e4 | 516 | |
2a3b791e JV |
517 | #ifdef CONFIG_IP_VS_IPV6 |
518 | if (svc->af == AF_INET6) | |
519 | unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST; | |
520 | else | |
521 | #endif | |
4a98480b | 522 | unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST); |
2a3b791e | 523 | |
1da177e4 | 524 | /* if it is fwmark-based service, the cache_bypass sysctl is up |
2a3b791e | 525 | and the destination is a non-local unicast, then create |
1da177e4 | 526 | a cache_bypass connection entry */ |
4a98480b | 527 | ipvs = net_ipvs(net); |
a0840e2e | 528 | if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) { |
1da177e4 LT |
529 | int ret, cs; |
530 | struct ip_vs_conn *cp; | |
3575792e JA |
531 | unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && |
532 | iph.protocol == IPPROTO_UDP)? | |
533 | IP_VS_CONN_F_ONE_PACKET : 0; | |
dff630dd | 534 | union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } }; |
1da177e4 LT |
535 | |
536 | ip_vs_service_put(svc); | |
537 | ||
538 | /* create a new connection entry */ | |
1e3e238e | 539 | IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__); |
f11017ec SH |
540 | { |
541 | struct ip_vs_conn_param p; | |
6e67e586 | 542 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, |
f11017ec SH |
543 | &iph.saddr, pptr[0], |
544 | &iph.daddr, pptr[1], &p); | |
545 | cp = ip_vs_conn_new(&p, &daddr, 0, | |
546 | IP_VS_CONN_F_BYPASS | flags, | |
0e051e68 | 547 | NULL, skb->mark); |
f11017ec SH |
548 | if (!cp) |
549 | return NF_DROP; | |
550 | } | |
1da177e4 LT |
551 | |
552 | /* statistics */ | |
553 | ip_vs_in_stats(cp, skb); | |
554 | ||
555 | /* set state */ | |
9330419d | 556 | cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 LT |
557 | |
558 | /* transmit the first SYN packet */ | |
9330419d | 559 | ret = cp->packet_xmit(skb, cp, pd->pp); |
1da177e4 LT |
560 | /* do not touch skb anymore */ |
561 | ||
562 | atomic_inc(&cp->in_pkts); | |
563 | ip_vs_conn_put(cp); | |
564 | return ret; | |
565 | } | |
566 | ||
567 | /* | |
568 | * When the virtual ftp service is presented, packets destined | |
569 | * for other services on the VIP may get here (except services | |
570 | * listed in the ipvs table), pass the packets, because it is | |
571 | * not ipvs job to decide to drop the packets. | |
572 | */ | |
573 | if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) { | |
574 | ip_vs_service_put(svc); | |
575 | return NF_ACCEPT; | |
576 | } | |
577 | ||
578 | ip_vs_service_put(svc); | |
579 | ||
580 | /* | |
581 | * Notify the client that the destination is unreachable, and | |
582 | * release the socket buffer. | |
583 | * Since it is in IP layer, the TCP socket is not actually | |
584 | * created, the TCP RST packet cannot be sent, instead that | |
585 | * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ | |
586 | */ | |
2a3b791e | 587 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
588 | if (svc->af == AF_INET6) { |
589 | if (!skb->dev) { | |
590 | struct net *net = dev_net(skb_dst(skb)->dev); | |
591 | ||
592 | skb->dev = net->loopback_dev; | |
593 | } | |
3ffe533c | 594 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
cb59155f | 595 | } else |
2a3b791e JV |
596 | #endif |
597 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | |
598 | ||
1da177e4 LT |
599 | return NF_DROP; |
600 | } | |
601 | ||
b1550f22 | 602 | __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset) |
1da177e4 | 603 | { |
d3bc23e7 | 604 | return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0)); |
1da177e4 LT |
605 | } |
606 | ||
1ca5bb54 JA |
607 | static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum) |
608 | { | |
609 | if (NF_INET_LOCAL_IN == hooknum) | |
610 | return IP_DEFRAG_VS_IN; | |
611 | if (NF_INET_FORWARD == hooknum) | |
612 | return IP_DEFRAG_VS_FWD; | |
613 | return IP_DEFRAG_VS_OUT; | |
614 | } | |
615 | ||
776c729e | 616 | static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user) |
1da177e4 | 617 | { |
776c729e HX |
618 | int err = ip_defrag(skb, user); |
619 | ||
620 | if (!err) | |
eddc9ec5 | 621 | ip_send_check(ip_hdr(skb)); |
776c729e HX |
622 | |
623 | return err; | |
1da177e4 LT |
624 | } |
625 | ||
2a3b791e JV |
626 | #ifdef CONFIG_IP_VS_IPV6 |
627 | static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user) | |
628 | { | |
629 | /* TODO IPv6: Find out what to do here for IPv6 */ | |
630 | return 0; | |
631 | } | |
632 | #endif | |
633 | ||
ba4fd7e9 SH |
634 | static int ip_vs_route_me_harder(int af, struct sk_buff *skb) |
635 | { | |
636 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); | |
637 | ||
638 | #ifdef CONFIG_IP_VS_IPV6 | |
639 | if (af == AF_INET6) { | |
640 | if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0) | |
641 | return 1; | |
642 | } else | |
643 | #endif | |
644 | if ((ipvs->sysctl_snat_reroute || | |
645 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) && | |
646 | ip_route_me_harder(skb, RTN_LOCAL) != 0) | |
647 | return 1; | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
1da177e4 LT |
652 | /* |
653 | * Packet has been made sufficiently writable in caller | |
654 | * - inout: 1=in->out, 0=out->in | |
655 | */ | |
656 | void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
657 | struct ip_vs_conn *cp, int inout) | |
658 | { | |
eddc9ec5 | 659 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 660 | unsigned int icmp_offset = iph->ihl*4; |
d56f90a7 ACM |
661 | struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) + |
662 | icmp_offset); | |
1da177e4 LT |
663 | struct iphdr *ciph = (struct iphdr *)(icmph + 1); |
664 | ||
665 | if (inout) { | |
e7ade46a | 666 | iph->saddr = cp->vaddr.ip; |
1da177e4 | 667 | ip_send_check(iph); |
e7ade46a | 668 | ciph->daddr = cp->vaddr.ip; |
1da177e4 LT |
669 | ip_send_check(ciph); |
670 | } else { | |
e7ade46a | 671 | iph->daddr = cp->daddr.ip; |
1da177e4 | 672 | ip_send_check(iph); |
e7ade46a | 673 | ciph->saddr = cp->daddr.ip; |
1da177e4 LT |
674 | ip_send_check(ciph); |
675 | } | |
676 | ||
2906f66a VMR |
677 | /* the TCP/UDP/SCTP port */ |
678 | if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol || | |
679 | IPPROTO_SCTP == ciph->protocol) { | |
014d730d | 680 | __be16 *ports = (void *)ciph + ciph->ihl*4; |
1da177e4 LT |
681 | |
682 | if (inout) | |
683 | ports[1] = cp->vport; | |
684 | else | |
685 | ports[0] = cp->dport; | |
686 | } | |
687 | ||
688 | /* And finally the ICMP checksum */ | |
689 | icmph->checksum = 0; | |
690 | icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset); | |
691 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
692 | ||
693 | if (inout) | |
0d79641a | 694 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
695 | "Forwarding altered outgoing ICMP"); |
696 | else | |
0d79641a | 697 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
1da177e4 LT |
698 | "Forwarding altered incoming ICMP"); |
699 | } | |
700 | ||
b3cdd2a7 JV |
701 | #ifdef CONFIG_IP_VS_IPV6 |
702 | void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
703 | struct ip_vs_conn *cp, int inout) | |
704 | { | |
705 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
706 | unsigned int icmp_offset = sizeof(struct ipv6hdr); | |
707 | struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) + | |
708 | icmp_offset); | |
709 | struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1); | |
710 | ||
711 | if (inout) { | |
712 | iph->saddr = cp->vaddr.in6; | |
713 | ciph->daddr = cp->vaddr.in6; | |
714 | } else { | |
715 | iph->daddr = cp->daddr.in6; | |
716 | ciph->saddr = cp->daddr.in6; | |
717 | } | |
718 | ||
2906f66a VMR |
719 | /* the TCP/UDP/SCTP port */ |
720 | if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr || | |
721 | IPPROTO_SCTP == ciph->nexthdr) { | |
b3cdd2a7 JV |
722 | __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr); |
723 | ||
724 | if (inout) | |
725 | ports[1] = cp->vport; | |
726 | else | |
727 | ports[0] = cp->dport; | |
728 | } | |
729 | ||
730 | /* And finally the ICMP checksum */ | |
8870f842 SH |
731 | icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, |
732 | skb->len - icmp_offset, | |
733 | IPPROTO_ICMPV6, 0); | |
734 | skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset; | |
735 | skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); | |
736 | skb->ip_summed = CHECKSUM_PARTIAL; | |
b3cdd2a7 JV |
737 | |
738 | if (inout) | |
0d79641a JA |
739 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
740 | (void *)ciph - (void *)iph, | |
741 | "Forwarding altered outgoing ICMPv6"); | |
b3cdd2a7 | 742 | else |
0d79641a JA |
743 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
744 | (void *)ciph - (void *)iph, | |
745 | "Forwarding altered incoming ICMPv6"); | |
b3cdd2a7 JV |
746 | } |
747 | #endif | |
748 | ||
4856c84c | 749 | /* Handle relevant response ICMP messages - forward to the right |
6cb90db5 | 750 | * destination host. |
4856c84c | 751 | */ |
f2428ed5 SH |
752 | static int handle_response_icmp(int af, struct sk_buff *skb, |
753 | union nf_inet_addr *snet, | |
754 | __u8 protocol, struct ip_vs_conn *cp, | |
4856c84c MT |
755 | struct ip_vs_protocol *pp, |
756 | unsigned int offset, unsigned int ihl) | |
757 | { | |
758 | unsigned int verdict = NF_DROP; | |
759 | ||
760 | if (IP_VS_FWD_METHOD(cp) != 0) { | |
1e3e238e HE |
761 | pr_err("shouldn't reach here, because the box is on the " |
762 | "half connection in the tun/dr module.\n"); | |
4856c84c MT |
763 | } |
764 | ||
765 | /* Ensure the checksum is correct */ | |
766 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { | |
767 | /* Failed checksum! */ | |
f2428ed5 SH |
768 | IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n", |
769 | IP_VS_DBG_ADDR(af, snet)); | |
4856c84c MT |
770 | goto out; |
771 | } | |
772 | ||
2906f66a VMR |
773 | if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
774 | IPPROTO_SCTP == protocol) | |
4856c84c MT |
775 | offset += 2 * sizeof(__u16); |
776 | if (!skb_make_writable(skb, offset)) | |
777 | goto out; | |
778 | ||
f2428ed5 SH |
779 | #ifdef CONFIG_IP_VS_IPV6 |
780 | if (af == AF_INET6) | |
781 | ip_vs_nat_icmp_v6(skb, pp, cp, 1); | |
782 | else | |
783 | #endif | |
784 | ip_vs_nat_icmp(skb, pp, cp, 1); | |
4856c84c | 785 | |
ba4fd7e9 SH |
786 | if (ip_vs_route_me_harder(af, skb)) |
787 | goto out; | |
f5a41847 | 788 | |
4856c84c MT |
789 | /* do the statistics and put it back */ |
790 | ip_vs_out_stats(cp, skb); | |
791 | ||
cf356d69 | 792 | skb->ipvs_property = 1; |
f4bc17cd | 793 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 794 | ip_vs_notrack(skb); |
f4bc17cd JA |
795 | else |
796 | ip_vs_update_conntrack(skb, cp, 0); | |
4856c84c MT |
797 | verdict = NF_ACCEPT; |
798 | ||
799 | out: | |
800 | __ip_vs_conn_put(cp); | |
801 | ||
802 | return verdict; | |
803 | } | |
804 | ||
1da177e4 LT |
805 | /* |
806 | * Handle ICMP messages in the inside-to-outside direction (outgoing). | |
4856c84c | 807 | * Find any that might be relevant, check against existing connections. |
1da177e4 | 808 | * Currently handles error types - unreachable, quench, ttl exceeded. |
1da177e4 | 809 | */ |
1ca5bb54 JA |
810 | static int ip_vs_out_icmp(struct sk_buff *skb, int *related, |
811 | unsigned int hooknum) | |
1da177e4 | 812 | { |
1da177e4 LT |
813 | struct iphdr *iph; |
814 | struct icmphdr _icmph, *ic; | |
815 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 816 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
817 | struct ip_vs_conn *cp; |
818 | struct ip_vs_protocol *pp; | |
4856c84c | 819 | unsigned int offset, ihl; |
f2428ed5 | 820 | union nf_inet_addr snet; |
1da177e4 LT |
821 | |
822 | *related = 1; | |
823 | ||
824 | /* reassemble IP fragments */ | |
eddc9ec5 | 825 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
1ca5bb54 | 826 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 827 | return NF_STOLEN; |
1da177e4 LT |
828 | } |
829 | ||
eddc9ec5 | 830 | iph = ip_hdr(skb); |
1da177e4 LT |
831 | offset = ihl = iph->ihl * 4; |
832 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
833 | if (ic == NULL) | |
834 | return NF_DROP; | |
835 | ||
14d5e834 | 836 | IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 837 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 838 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
839 | |
840 | /* | |
841 | * Work through seeing if this is for us. | |
842 | * These checks are supposed to be in an order that means easy | |
843 | * things are checked first to speed up processing.... however | |
844 | * this means that some packets will manage to get a long way | |
845 | * down this stack and then be rejected, but that's life. | |
846 | */ | |
847 | if ((ic->type != ICMP_DEST_UNREACH) && | |
848 | (ic->type != ICMP_SOURCE_QUENCH) && | |
849 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
850 | *related = 0; | |
851 | return NF_ACCEPT; | |
852 | } | |
853 | ||
854 | /* Now find the contained IP header */ | |
855 | offset += sizeof(_icmph); | |
856 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
857 | if (cih == NULL) | |
858 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
859 | ||
860 | pp = ip_vs_proto_get(cih->protocol); | |
861 | if (!pp) | |
862 | return NF_ACCEPT; | |
863 | ||
864 | /* Is the embedded protocol header present? */ | |
4412ec49 | 865 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
866 | pp->dont_defrag)) |
867 | return NF_ACCEPT; | |
868 | ||
0d79641a JA |
869 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
870 | "Checking outgoing ICMP for"); | |
1da177e4 LT |
871 | |
872 | offset += cih->ihl * 4; | |
873 | ||
51ef348b | 874 | ip_vs_fill_iphdr(AF_INET, cih, &ciph); |
1da177e4 | 875 | /* The embedded headers contain source and dest in reverse order */ |
9330419d | 876 | cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1); |
1da177e4 LT |
877 | if (!cp) |
878 | return NF_ACCEPT; | |
879 | ||
f2428ed5 SH |
880 | snet.ip = iph->saddr; |
881 | return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp, | |
882 | pp, offset, ihl); | |
1da177e4 LT |
883 | } |
884 | ||
2a3b791e | 885 | #ifdef CONFIG_IP_VS_IPV6 |
1ca5bb54 JA |
886 | static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related, |
887 | unsigned int hooknum) | |
2a3b791e JV |
888 | { |
889 | struct ipv6hdr *iph; | |
890 | struct icmp6hdr _icmph, *ic; | |
891 | struct ipv6hdr _ciph, *cih; /* The ip header contained | |
892 | within the ICMP */ | |
893 | struct ip_vs_iphdr ciph; | |
894 | struct ip_vs_conn *cp; | |
895 | struct ip_vs_protocol *pp; | |
f2428ed5 SH |
896 | unsigned int offset; |
897 | union nf_inet_addr snet; | |
2a3b791e JV |
898 | |
899 | *related = 1; | |
900 | ||
901 | /* reassemble IP fragments */ | |
902 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { | |
1ca5bb54 | 903 | if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum))) |
2a3b791e JV |
904 | return NF_STOLEN; |
905 | } | |
906 | ||
907 | iph = ipv6_hdr(skb); | |
908 | offset = sizeof(struct ipv6hdr); | |
909 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
910 | if (ic == NULL) | |
911 | return NF_DROP; | |
912 | ||
5b095d98 | 913 | IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n", |
2a3b791e | 914 | ic->icmp6_type, ntohs(icmpv6_id(ic)), |
38ff4fa4 | 915 | &iph->saddr, &iph->daddr); |
2a3b791e JV |
916 | |
917 | /* | |
918 | * Work through seeing if this is for us. | |
919 | * These checks are supposed to be in an order that means easy | |
920 | * things are checked first to speed up processing.... however | |
921 | * this means that some packets will manage to get a long way | |
922 | * down this stack and then be rejected, but that's life. | |
923 | */ | |
924 | if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) && | |
925 | (ic->icmp6_type != ICMPV6_PKT_TOOBIG) && | |
926 | (ic->icmp6_type != ICMPV6_TIME_EXCEED)) { | |
927 | *related = 0; | |
928 | return NF_ACCEPT; | |
929 | } | |
930 | ||
931 | /* Now find the contained IP header */ | |
932 | offset += sizeof(_icmph); | |
933 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
934 | if (cih == NULL) | |
935 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
936 | ||
937 | pp = ip_vs_proto_get(cih->nexthdr); | |
938 | if (!pp) | |
939 | return NF_ACCEPT; | |
940 | ||
941 | /* Is the embedded protocol header present? */ | |
942 | /* TODO: we don't support fragmentation at the moment anyways */ | |
943 | if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag)) | |
944 | return NF_ACCEPT; | |
945 | ||
0d79641a JA |
946 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset, |
947 | "Checking outgoing ICMPv6 for"); | |
2a3b791e JV |
948 | |
949 | offset += sizeof(struct ipv6hdr); | |
950 | ||
951 | ip_vs_fill_iphdr(AF_INET6, cih, &ciph); | |
952 | /* The embedded headers contain source and dest in reverse order */ | |
9330419d | 953 | cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1); |
2a3b791e JV |
954 | if (!cp) |
955 | return NF_ACCEPT; | |
956 | ||
178f5e49 | 957 | ipv6_addr_copy(&snet.in6, &iph->saddr); |
f2428ed5 SH |
958 | return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp, |
959 | pp, offset, sizeof(struct ipv6hdr)); | |
2a3b791e JV |
960 | } |
961 | #endif | |
962 | ||
2906f66a VMR |
963 | /* |
964 | * Check if sctp chunc is ABORT chunk | |
965 | */ | |
966 | static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len) | |
967 | { | |
968 | sctp_chunkhdr_t *sch, schunk; | |
969 | sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t), | |
970 | sizeof(schunk), &schunk); | |
971 | if (sch == NULL) | |
972 | return 0; | |
973 | if (sch->type == SCTP_CID_ABORT) | |
974 | return 1; | |
975 | return 0; | |
976 | } | |
977 | ||
2a3b791e | 978 | static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len) |
1da177e4 LT |
979 | { |
980 | struct tcphdr _tcph, *th; | |
981 | ||
2a3b791e | 982 | th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph); |
1da177e4 LT |
983 | if (th == NULL) |
984 | return 0; | |
985 | return th->rst; | |
986 | } | |
987 | ||
4856c84c | 988 | /* Handle response packets: rewrite addresses and send away... |
4856c84c MT |
989 | */ |
990 | static unsigned int | |
9330419d | 991 | handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
4856c84c MT |
992 | struct ip_vs_conn *cp, int ihl) |
993 | { | |
9330419d HS |
994 | struct ip_vs_protocol *pp = pd->pp; |
995 | ||
0d79641a | 996 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet"); |
4856c84c MT |
997 | |
998 | if (!skb_make_writable(skb, ihl)) | |
999 | goto drop; | |
1000 | ||
1001 | /* mangle the packet */ | |
1002 | if (pp->snat_handler && !pp->snat_handler(skb, pp, cp)) | |
1003 | goto drop; | |
1004 | ||
1005 | #ifdef CONFIG_IP_VS_IPV6 | |
1006 | if (af == AF_INET6) | |
1007 | ipv6_hdr(skb)->saddr = cp->vaddr.in6; | |
1008 | else | |
1009 | #endif | |
1010 | { | |
1011 | ip_hdr(skb)->saddr = cp->vaddr.ip; | |
1012 | ip_send_check(ip_hdr(skb)); | |
1013 | } | |
1014 | ||
8a803040 JA |
1015 | /* |
1016 | * nf_iterate does not expect change in the skb->dst->dev. | |
1017 | * It looks like it is not fatal to enable this code for hooks | |
1018 | * where our handlers are at the end of the chain list and | |
1019 | * when all next handlers use skb->dst->dev and not outdev. | |
1020 | * It will definitely route properly the inout NAT traffic | |
1021 | * when multiple paths are used. | |
1022 | */ | |
1023 | ||
4856c84c MT |
1024 | /* For policy routing, packets originating from this |
1025 | * machine itself may be routed differently to packets | |
1026 | * passing through. We want this packet to be routed as | |
1027 | * if it came from this machine itself. So re-compute | |
1028 | * the routing information. | |
1029 | */ | |
ba4fd7e9 SH |
1030 | if (ip_vs_route_me_harder(af, skb)) |
1031 | goto drop; | |
4856c84c | 1032 | |
0d79641a | 1033 | IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT"); |
4856c84c MT |
1034 | |
1035 | ip_vs_out_stats(cp, skb); | |
9330419d | 1036 | ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd); |
cf356d69 | 1037 | skb->ipvs_property = 1; |
f4bc17cd | 1038 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
cf356d69 | 1039 | ip_vs_notrack(skb); |
f4bc17cd JA |
1040 | else |
1041 | ip_vs_update_conntrack(skb, cp, 0); | |
4856c84c MT |
1042 | ip_vs_conn_put(cp); |
1043 | ||
4856c84c MT |
1044 | LeaveFunction(11); |
1045 | return NF_ACCEPT; | |
1046 | ||
1047 | drop: | |
1048 | ip_vs_conn_put(cp); | |
1049 | kfree_skb(skb); | |
f4bc17cd | 1050 | LeaveFunction(11); |
4856c84c MT |
1051 | return NF_STOLEN; |
1052 | } | |
1053 | ||
1da177e4 | 1054 | /* |
4856c84c | 1055 | * Check if outgoing packet belongs to the established ip_vs_conn. |
1da177e4 LT |
1056 | */ |
1057 | static unsigned int | |
fc604767 | 1058 | ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1059 | { |
fc723250 | 1060 | struct net *net = NULL; |
51ef348b | 1061 | struct ip_vs_iphdr iph; |
1da177e4 | 1062 | struct ip_vs_protocol *pp; |
9330419d | 1063 | struct ip_vs_proto_data *pd; |
1da177e4 | 1064 | struct ip_vs_conn *cp; |
a0840e2e | 1065 | struct netns_ipvs *ipvs; |
1da177e4 LT |
1066 | |
1067 | EnterFunction(11); | |
1068 | ||
fc604767 | 1069 | /* Already marked as IPVS request or reply? */ |
6869c4d8 | 1070 | if (skb->ipvs_property) |
1da177e4 LT |
1071 | return NF_ACCEPT; |
1072 | ||
fc604767 JA |
1073 | /* Bad... Do not break raw sockets */ |
1074 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && | |
1075 | af == AF_INET)) { | |
1076 | struct sock *sk = skb->sk; | |
1077 | struct inet_sock *inet = inet_sk(skb->sk); | |
1078 | ||
1079 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) | |
1080 | return NF_ACCEPT; | |
1081 | } | |
1082 | ||
1083 | if (unlikely(!skb_dst(skb))) | |
1084 | return NF_ACCEPT; | |
1085 | ||
fc723250 | 1086 | net = skb_net(skb); |
2a3b791e JV |
1087 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
1088 | #ifdef CONFIG_IP_VS_IPV6 | |
1089 | if (af == AF_INET6) { | |
1090 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 JA |
1091 | int related; |
1092 | int verdict = ip_vs_out_icmp_v6(skb, &related, | |
1093 | hooknum); | |
1da177e4 | 1094 | |
f5a41847 | 1095 | if (related) |
2a3b791e JV |
1096 | return verdict; |
1097 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1098 | } | |
1099 | } else | |
1100 | #endif | |
1101 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 JA |
1102 | int related; |
1103 | int verdict = ip_vs_out_icmp(skb, &related, hooknum); | |
2a3b791e | 1104 | |
f5a41847 | 1105 | if (related) |
2a3b791e JV |
1106 | return verdict; |
1107 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1108 | } | |
1da177e4 | 1109 | |
9330419d HS |
1110 | pd = ip_vs_proto_data_get(net, iph.protocol); |
1111 | if (unlikely(!pd)) | |
1da177e4 | 1112 | return NF_ACCEPT; |
9330419d | 1113 | pp = pd->pp; |
1da177e4 LT |
1114 | |
1115 | /* reassemble IP fragments */ | |
2a3b791e JV |
1116 | #ifdef CONFIG_IP_VS_IPV6 |
1117 | if (af == AF_INET6) { | |
1ca5bb54 JA |
1118 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { |
1119 | if (ip_vs_gather_frags_v6(skb, | |
1120 | ip_vs_defrag_user(hooknum))) | |
1121 | return NF_STOLEN; | |
2a3b791e | 1122 | } |
1ca5bb54 JA |
1123 | |
1124 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
2a3b791e JV |
1125 | } else |
1126 | #endif | |
1127 | if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) && | |
1128 | !pp->dont_defrag)) { | |
1ca5bb54 JA |
1129 | if (ip_vs_gather_frags(skb, |
1130 | ip_vs_defrag_user(hooknum))) | |
2a3b791e JV |
1131 | return NF_STOLEN; |
1132 | ||
1133 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1134 | } | |
1da177e4 LT |
1135 | |
1136 | /* | |
1137 | * Check if the packet belongs to an existing entry | |
1138 | */ | |
9330419d | 1139 | cp = pp->conn_out_get(af, skb, &iph, iph.len, 0); |
a0840e2e | 1140 | ipvs = net_ipvs(net); |
1da177e4 | 1141 | |
cb59155f | 1142 | if (likely(cp)) |
9330419d | 1143 | return handle_response(af, skb, pd, cp, iph.len); |
a0840e2e | 1144 | if (ipvs->sysctl_nat_icmp_send && |
cb59155f JA |
1145 | (pp->protocol == IPPROTO_TCP || |
1146 | pp->protocol == IPPROTO_UDP || | |
1147 | pp->protocol == IPPROTO_SCTP)) { | |
1148 | __be16 _ports[2], *pptr; | |
1149 | ||
1150 | pptr = skb_header_pointer(skb, iph.len, | |
1151 | sizeof(_ports), _ports); | |
1152 | if (pptr == NULL) | |
1153 | return NF_ACCEPT; /* Not for me */ | |
fc723250 | 1154 | if (ip_vs_lookup_real_service(net, af, iph.protocol, |
cb59155f JA |
1155 | &iph.saddr, |
1156 | pptr[0])) { | |
1157 | /* | |
1158 | * Notify the real server: there is no | |
1159 | * existing entry if it is not RST | |
1160 | * packet or not TCP packet. | |
1161 | */ | |
1162 | if ((iph.protocol != IPPROTO_TCP && | |
1163 | iph.protocol != IPPROTO_SCTP) | |
1164 | || ((iph.protocol == IPPROTO_TCP | |
1165 | && !is_tcp_reset(skb, iph.len)) | |
1166 | || (iph.protocol == IPPROTO_SCTP | |
1167 | && !is_sctp_abort(skb, | |
1168 | iph.len)))) { | |
2a3b791e | 1169 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
1170 | if (af == AF_INET6) { |
1171 | struct net *net = | |
1172 | dev_net(skb_dst(skb)->dev); | |
1173 | ||
1174 | if (!skb->dev) | |
1175 | skb->dev = net->loopback_dev; | |
1176 | icmpv6_send(skb, | |
1177 | ICMPV6_DEST_UNREACH, | |
1178 | ICMPV6_PORT_UNREACH, | |
1179 | 0); | |
1180 | } else | |
2a3b791e | 1181 | #endif |
cb59155f JA |
1182 | icmp_send(skb, |
1183 | ICMP_DEST_UNREACH, | |
1184 | ICMP_PORT_UNREACH, 0); | |
1185 | return NF_DROP; | |
1da177e4 LT |
1186 | } |
1187 | } | |
1da177e4 | 1188 | } |
0d79641a | 1189 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
cb59155f JA |
1190 | "ip_vs_out: packet continues traversal as normal"); |
1191 | return NF_ACCEPT; | |
1da177e4 LT |
1192 | } |
1193 | ||
fc604767 | 1194 | /* |
cb59155f JA |
1195 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1196 | * used only for VS/NAT. | |
fc604767 JA |
1197 | * Check if packet is reply for established ip_vs_conn. |
1198 | */ | |
1199 | static unsigned int | |
1200 | ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb, | |
1201 | const struct net_device *in, const struct net_device *out, | |
1202 | int (*okfn)(struct sk_buff *)) | |
1203 | { | |
1204 | return ip_vs_out(hooknum, skb, AF_INET); | |
1205 | } | |
1206 | ||
1207 | /* | |
1208 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1209 | * Check if packet is reply for established ip_vs_conn. | |
1210 | */ | |
1211 | static unsigned int | |
1212 | ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb, | |
1213 | const struct net_device *in, const struct net_device *out, | |
1214 | int (*okfn)(struct sk_buff *)) | |
1215 | { | |
1216 | unsigned int verdict; | |
1217 | ||
1218 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1219 | local_bh_disable(); | |
1220 | verdict = ip_vs_out(hooknum, skb, AF_INET); | |
1221 | local_bh_enable(); | |
1222 | return verdict; | |
1223 | } | |
1224 | ||
1225 | #ifdef CONFIG_IP_VS_IPV6 | |
1226 | ||
1227 | /* | |
cb59155f JA |
1228 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
1229 | * used only for VS/NAT. | |
fc604767 JA |
1230 | * Check if packet is reply for established ip_vs_conn. |
1231 | */ | |
1232 | static unsigned int | |
1233 | ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb, | |
1234 | const struct net_device *in, const struct net_device *out, | |
1235 | int (*okfn)(struct sk_buff *)) | |
1236 | { | |
1237 | return ip_vs_out(hooknum, skb, AF_INET6); | |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. | |
1242 | * Check if packet is reply for established ip_vs_conn. | |
1243 | */ | |
1244 | static unsigned int | |
1245 | ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb, | |
1246 | const struct net_device *in, const struct net_device *out, | |
1247 | int (*okfn)(struct sk_buff *)) | |
1248 | { | |
1249 | unsigned int verdict; | |
1250 | ||
1251 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1252 | local_bh_disable(); | |
1253 | verdict = ip_vs_out(hooknum, skb, AF_INET6); | |
1254 | local_bh_enable(); | |
1255 | return verdict; | |
1256 | } | |
1257 | ||
1258 | #endif | |
1da177e4 LT |
1259 | |
1260 | /* | |
1261 | * Handle ICMP messages in the outside-to-inside direction (incoming). | |
1262 | * Find any that might be relevant, check against existing connections, | |
1263 | * forward to the right destination host if relevant. | |
1264 | * Currently handles error types - unreachable, quench, ttl exceeded. | |
1265 | */ | |
e905a9ed | 1266 | static int |
3db05fea | 1267 | ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum) |
1da177e4 | 1268 | { |
9330419d | 1269 | struct net *net = NULL; |
1da177e4 LT |
1270 | struct iphdr *iph; |
1271 | struct icmphdr _icmph, *ic; | |
1272 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
51ef348b | 1273 | struct ip_vs_iphdr ciph; |
1da177e4 LT |
1274 | struct ip_vs_conn *cp; |
1275 | struct ip_vs_protocol *pp; | |
9330419d | 1276 | struct ip_vs_proto_data *pd; |
1da177e4 LT |
1277 | unsigned int offset, ihl, verdict; |
1278 | ||
1279 | *related = 1; | |
1280 | ||
1281 | /* reassemble IP fragments */ | |
eddc9ec5 | 1282 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
1ca5bb54 | 1283 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
1da177e4 | 1284 | return NF_STOLEN; |
1da177e4 LT |
1285 | } |
1286 | ||
eddc9ec5 | 1287 | iph = ip_hdr(skb); |
1da177e4 LT |
1288 | offset = ihl = iph->ihl * 4; |
1289 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
1290 | if (ic == NULL) | |
1291 | return NF_DROP; | |
1292 | ||
14d5e834 | 1293 | IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n", |
1da177e4 | 1294 | ic->type, ntohs(icmp_id(ic)), |
14d5e834 | 1295 | &iph->saddr, &iph->daddr); |
1da177e4 LT |
1296 | |
1297 | /* | |
1298 | * Work through seeing if this is for us. | |
1299 | * These checks are supposed to be in an order that means easy | |
1300 | * things are checked first to speed up processing.... however | |
1301 | * this means that some packets will manage to get a long way | |
1302 | * down this stack and then be rejected, but that's life. | |
1303 | */ | |
1304 | if ((ic->type != ICMP_DEST_UNREACH) && | |
1305 | (ic->type != ICMP_SOURCE_QUENCH) && | |
1306 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
1307 | *related = 0; | |
1308 | return NF_ACCEPT; | |
1309 | } | |
1310 | ||
1311 | /* Now find the contained IP header */ | |
1312 | offset += sizeof(_icmph); | |
1313 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1314 | if (cih == NULL) | |
1315 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
1316 | ||
9330419d HS |
1317 | net = skb_net(skb); |
1318 | pd = ip_vs_proto_data_get(net, cih->protocol); | |
1319 | if (!pd) | |
1da177e4 | 1320 | return NF_ACCEPT; |
9330419d | 1321 | pp = pd->pp; |
1da177e4 LT |
1322 | |
1323 | /* Is the embedded protocol header present? */ | |
4412ec49 | 1324 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
1325 | pp->dont_defrag)) |
1326 | return NF_ACCEPT; | |
1327 | ||
0d79641a JA |
1328 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
1329 | "Checking incoming ICMP for"); | |
1da177e4 LT |
1330 | |
1331 | offset += cih->ihl * 4; | |
1332 | ||
51ef348b | 1333 | ip_vs_fill_iphdr(AF_INET, cih, &ciph); |
1da177e4 | 1334 | /* The embedded headers contain source and dest in reverse order */ |
9330419d | 1335 | cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1); |
6cb90db5 | 1336 | if (!cp) |
1da177e4 LT |
1337 | return NF_ACCEPT; |
1338 | ||
1339 | verdict = NF_DROP; | |
1340 | ||
1341 | /* Ensure the checksum is correct */ | |
60476372 | 1342 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { |
1da177e4 | 1343 | /* Failed checksum! */ |
14d5e834 HH |
1344 | IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n", |
1345 | &iph->saddr); | |
1da177e4 LT |
1346 | goto out; |
1347 | } | |
1348 | ||
1349 | /* do the statistics and put it back */ | |
1350 | ip_vs_in_stats(cp, skb); | |
1351 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol) | |
1352 | offset += 2 * sizeof(__u16); | |
1353 | verdict = ip_vs_icmp_xmit(skb, cp, pp, offset); | |
489fdeda JA |
1354 | /* LOCALNODE from FORWARD hook is not supported */ |
1355 | if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD && | |
1356 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) { | |
1357 | IP_VS_DBG(1, "%s(): " | |
1358 | "local delivery to %pI4 but in FORWARD\n", | |
1359 | __func__, &skb_rtable(skb)->rt_dst); | |
1360 | verdict = NF_DROP; | |
1361 | } | |
1da177e4 LT |
1362 | |
1363 | out: | |
1364 | __ip_vs_conn_put(cp); | |
1365 | ||
1366 | return verdict; | |
1367 | } | |
1368 | ||
2a3b791e JV |
1369 | #ifdef CONFIG_IP_VS_IPV6 |
1370 | static int | |
1371 | ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum) | |
1372 | { | |
9330419d | 1373 | struct net *net = NULL; |
2a3b791e JV |
1374 | struct ipv6hdr *iph; |
1375 | struct icmp6hdr _icmph, *ic; | |
1376 | struct ipv6hdr _ciph, *cih; /* The ip header contained | |
1377 | within the ICMP */ | |
1378 | struct ip_vs_iphdr ciph; | |
1379 | struct ip_vs_conn *cp; | |
1380 | struct ip_vs_protocol *pp; | |
9330419d | 1381 | struct ip_vs_proto_data *pd; |
2a3b791e | 1382 | unsigned int offset, verdict; |
489fdeda | 1383 | struct rt6_info *rt; |
2a3b791e JV |
1384 | |
1385 | *related = 1; | |
1386 | ||
1387 | /* reassemble IP fragments */ | |
1388 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { | |
1ca5bb54 | 1389 | if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum))) |
2a3b791e JV |
1390 | return NF_STOLEN; |
1391 | } | |
1392 | ||
1393 | iph = ipv6_hdr(skb); | |
1394 | offset = sizeof(struct ipv6hdr); | |
1395 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
1396 | if (ic == NULL) | |
1397 | return NF_DROP; | |
1398 | ||
5b095d98 | 1399 | IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n", |
2a3b791e | 1400 | ic->icmp6_type, ntohs(icmpv6_id(ic)), |
38ff4fa4 | 1401 | &iph->saddr, &iph->daddr); |
2a3b791e JV |
1402 | |
1403 | /* | |
1404 | * Work through seeing if this is for us. | |
1405 | * These checks are supposed to be in an order that means easy | |
1406 | * things are checked first to speed up processing.... however | |
1407 | * this means that some packets will manage to get a long way | |
1408 | * down this stack and then be rejected, but that's life. | |
1409 | */ | |
1410 | if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) && | |
1411 | (ic->icmp6_type != ICMPV6_PKT_TOOBIG) && | |
1412 | (ic->icmp6_type != ICMPV6_TIME_EXCEED)) { | |
1413 | *related = 0; | |
1414 | return NF_ACCEPT; | |
1415 | } | |
1416 | ||
1417 | /* Now find the contained IP header */ | |
1418 | offset += sizeof(_icmph); | |
1419 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
1420 | if (cih == NULL) | |
1421 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
1422 | ||
9330419d HS |
1423 | net = skb_net(skb); |
1424 | pd = ip_vs_proto_data_get(net, cih->nexthdr); | |
1425 | if (!pd) | |
2a3b791e | 1426 | return NF_ACCEPT; |
9330419d | 1427 | pp = pd->pp; |
2a3b791e JV |
1428 | |
1429 | /* Is the embedded protocol header present? */ | |
1430 | /* TODO: we don't support fragmentation at the moment anyways */ | |
1431 | if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag)) | |
1432 | return NF_ACCEPT; | |
1433 | ||
0d79641a JA |
1434 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset, |
1435 | "Checking incoming ICMPv6 for"); | |
2a3b791e JV |
1436 | |
1437 | offset += sizeof(struct ipv6hdr); | |
1438 | ||
1439 | ip_vs_fill_iphdr(AF_INET6, cih, &ciph); | |
1440 | /* The embedded headers contain source and dest in reverse order */ | |
9330419d | 1441 | cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1); |
6cb90db5 | 1442 | if (!cp) |
2a3b791e JV |
1443 | return NF_ACCEPT; |
1444 | ||
1445 | verdict = NF_DROP; | |
1446 | ||
1447 | /* do the statistics and put it back */ | |
1448 | ip_vs_in_stats(cp, skb); | |
2906f66a VMR |
1449 | if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr || |
1450 | IPPROTO_SCTP == cih->nexthdr) | |
2a3b791e JV |
1451 | offset += 2 * sizeof(__u16); |
1452 | verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset); | |
489fdeda JA |
1453 | /* LOCALNODE from FORWARD hook is not supported */ |
1454 | if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD && | |
1455 | (rt = (struct rt6_info *) skb_dst(skb)) && | |
1456 | rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) { | |
1457 | IP_VS_DBG(1, "%s(): " | |
1458 | "local delivery to %pI6 but in FORWARD\n", | |
1459 | __func__, &rt->rt6i_dst); | |
1460 | verdict = NF_DROP; | |
1461 | } | |
2a3b791e JV |
1462 | |
1463 | __ip_vs_conn_put(cp); | |
1464 | ||
1465 | return verdict; | |
1466 | } | |
1467 | #endif | |
1468 | ||
1469 | ||
1da177e4 LT |
1470 | /* |
1471 | * Check if it's for virtual services, look it up, | |
1472 | * and send it on its way... | |
1473 | */ | |
1474 | static unsigned int | |
cb59155f | 1475 | ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af) |
1da177e4 | 1476 | { |
f131315f | 1477 | struct net *net; |
51ef348b | 1478 | struct ip_vs_iphdr iph; |
1da177e4 | 1479 | struct ip_vs_protocol *pp; |
9330419d | 1480 | struct ip_vs_proto_data *pd; |
1da177e4 | 1481 | struct ip_vs_conn *cp; |
cb59155f | 1482 | int ret, restart, pkts; |
f131315f | 1483 | struct netns_ipvs *ipvs; |
2a3b791e | 1484 | |
fc604767 JA |
1485 | /* Already marked as IPVS request or reply? */ |
1486 | if (skb->ipvs_property) | |
1487 | return NF_ACCEPT; | |
1488 | ||
1da177e4 | 1489 | /* |
cb59155f JA |
1490 | * Big tappo: |
1491 | * - remote client: only PACKET_HOST | |
1492 | * - route: used for struct net when skb->dev is unset | |
1da177e4 | 1493 | */ |
cb59155f JA |
1494 | if (unlikely((skb->pkt_type != PACKET_HOST && |
1495 | hooknum != NF_INET_LOCAL_OUT) || | |
1496 | !skb_dst(skb))) { | |
1497 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1498 | IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s" | |
1499 | " ignored in hook %u\n", | |
1500 | skb->pkt_type, iph.protocol, | |
1501 | IP_VS_DBG_ADDR(af, &iph.daddr), hooknum); | |
1da177e4 LT |
1502 | return NF_ACCEPT; |
1503 | } | |
cb59155f JA |
1504 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
1505 | ||
1506 | /* Bad... Do not break raw sockets */ | |
1507 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && | |
1508 | af == AF_INET)) { | |
1509 | struct sock *sk = skb->sk; | |
1510 | struct inet_sock *inet = inet_sk(skb->sk); | |
1511 | ||
1512 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) | |
1513 | return NF_ACCEPT; | |
1514 | } | |
1da177e4 | 1515 | |
94b26551 JV |
1516 | #ifdef CONFIG_IP_VS_IPV6 |
1517 | if (af == AF_INET6) { | |
1518 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { | |
1ca5bb54 JA |
1519 | int related; |
1520 | int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum); | |
1da177e4 | 1521 | |
94b26551 JV |
1522 | if (related) |
1523 | return verdict; | |
1524 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1525 | } | |
1526 | } else | |
1527 | #endif | |
1528 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { | |
1ca5bb54 JA |
1529 | int related; |
1530 | int verdict = ip_vs_in_icmp(skb, &related, hooknum); | |
94b26551 JV |
1531 | |
1532 | if (related) | |
1533 | return verdict; | |
1534 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); | |
1535 | } | |
1da177e4 | 1536 | |
9330419d | 1537 | net = skb_net(skb); |
1da177e4 | 1538 | /* Protocol supported? */ |
9330419d HS |
1539 | pd = ip_vs_proto_data_get(net, iph.protocol); |
1540 | if (unlikely(!pd)) | |
1da177e4 | 1541 | return NF_ACCEPT; |
9330419d | 1542 | pp = pd->pp; |
1da177e4 LT |
1543 | /* |
1544 | * Check if the packet belongs to an existing connection entry | |
1545 | */ | |
9330419d | 1546 | cp = pp->conn_in_get(af, skb, &iph, iph.len, 0); |
1da177e4 LT |
1547 | |
1548 | if (unlikely(!cp)) { | |
1549 | int v; | |
1550 | ||
9330419d | 1551 | if (!pp->conn_schedule(af, skb, pd, &v, &cp)) |
1da177e4 LT |
1552 | return v; |
1553 | } | |
1554 | ||
1555 | if (unlikely(!cp)) { | |
1556 | /* sorry, all this trouble for a no-hit :) */ | |
0d79641a | 1557 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
cb59155f | 1558 | "ip_vs_in: packet continues traversal as normal"); |
1da177e4 LT |
1559 | return NF_ACCEPT; |
1560 | } | |
1561 | ||
0d79641a | 1562 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet"); |
f131315f HS |
1563 | net = skb_net(skb); |
1564 | ipvs = net_ipvs(net); | |
1da177e4 LT |
1565 | /* Check the server status */ |
1566 | if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
1567 | /* the destination server is not available */ | |
1568 | ||
a0840e2e | 1569 | if (ipvs->sysctl_expire_nodest_conn) { |
1da177e4 LT |
1570 | /* try to expire the connection immediately */ |
1571 | ip_vs_conn_expire_now(cp); | |
1da177e4 | 1572 | } |
dc8103f2 JA |
1573 | /* don't restart its timer, and silently |
1574 | drop the packet. */ | |
1575 | __ip_vs_conn_put(cp); | |
1da177e4 LT |
1576 | return NF_DROP; |
1577 | } | |
1578 | ||
1579 | ip_vs_in_stats(cp, skb); | |
9330419d | 1580 | restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
1da177e4 LT |
1581 | if (cp->packet_xmit) |
1582 | ret = cp->packet_xmit(skb, cp, pp); | |
1583 | /* do not touch skb anymore */ | |
1584 | else { | |
1585 | IP_VS_DBG_RL("warning: packet_xmit is null"); | |
1586 | ret = NF_ACCEPT; | |
1587 | } | |
1588 | ||
efac5276 RB |
1589 | /* Increase its packet counter and check if it is needed |
1590 | * to be synchronized | |
1591 | * | |
1592 | * Sync connection if it is about to close to | |
1593 | * encorage the standby servers to update the connections timeout | |
986a0757 HS |
1594 | * |
1595 | * For ONE_PKT let ip_vs_sync_conn() do the filter work. | |
efac5276 | 1596 | */ |
f131315f | 1597 | |
986a0757 | 1598 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
a0840e2e | 1599 | pkts = ipvs->sysctl_sync_threshold[0]; |
986a0757 HS |
1600 | else |
1601 | pkts = atomic_add_return(1, &cp->in_pkts); | |
1602 | ||
f131315f | 1603 | if ((ipvs->sync_state & IP_VS_STATE_MASTER) && |
2906f66a VMR |
1604 | cp->protocol == IPPROTO_SCTP) { |
1605 | if ((cp->state == IP_VS_SCTP_S_ESTABLISHED && | |
a0840e2e HS |
1606 | (pkts % ipvs->sysctl_sync_threshold[1] |
1607 | == ipvs->sysctl_sync_threshold[0])) || | |
2906f66a VMR |
1608 | (cp->old_state != cp->state && |
1609 | ((cp->state == IP_VS_SCTP_S_CLOSED) || | |
1610 | (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) || | |
1611 | (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) { | |
f131315f | 1612 | ip_vs_sync_conn(net, cp); |
2906f66a VMR |
1613 | goto out; |
1614 | } | |
1615 | } | |
1616 | ||
8ed2163f | 1617 | /* Keep this block last: TCP and others with pp->num_states <= 1 */ |
f131315f | 1618 | else if ((ipvs->sync_state & IP_VS_STATE_MASTER) && |
efac5276 RB |
1619 | (((cp->protocol != IPPROTO_TCP || |
1620 | cp->state == IP_VS_TCP_S_ESTABLISHED) && | |
a0840e2e HS |
1621 | (pkts % ipvs->sysctl_sync_threshold[1] |
1622 | == ipvs->sysctl_sync_threshold[0])) || | |
efac5276 RB |
1623 | ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) && |
1624 | ((cp->state == IP_VS_TCP_S_FIN_WAIT) || | |
9abfe315 | 1625 | (cp->state == IP_VS_TCP_S_CLOSE) || |
9d3a0de7 RB |
1626 | (cp->state == IP_VS_TCP_S_CLOSE_WAIT) || |
1627 | (cp->state == IP_VS_TCP_S_TIME_WAIT))))) | |
f131315f | 1628 | ip_vs_sync_conn(net, cp); |
2906f66a | 1629 | out: |
efac5276 | 1630 | cp->old_state = cp->state; |
1da177e4 LT |
1631 | |
1632 | ip_vs_conn_put(cp); | |
1633 | return ret; | |
1634 | } | |
1635 | ||
cb59155f JA |
1636 | /* |
1637 | * AF_INET handler in NF_INET_LOCAL_IN chain | |
1638 | * Schedule and forward packets from remote clients | |
1639 | */ | |
1640 | static unsigned int | |
1641 | ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb, | |
1642 | const struct net_device *in, | |
1643 | const struct net_device *out, | |
1644 | int (*okfn)(struct sk_buff *)) | |
1645 | { | |
1646 | return ip_vs_in(hooknum, skb, AF_INET); | |
1647 | } | |
1648 | ||
1649 | /* | |
1650 | * AF_INET handler in NF_INET_LOCAL_OUT chain | |
1651 | * Schedule and forward packets from local clients | |
1652 | */ | |
1653 | static unsigned int | |
1654 | ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb, | |
1655 | const struct net_device *in, const struct net_device *out, | |
1656 | int (*okfn)(struct sk_buff *)) | |
1657 | { | |
1658 | unsigned int verdict; | |
1659 | ||
1660 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1661 | local_bh_disable(); | |
1662 | verdict = ip_vs_in(hooknum, skb, AF_INET); | |
1663 | local_bh_enable(); | |
1664 | return verdict; | |
1665 | } | |
1666 | ||
1667 | #ifdef CONFIG_IP_VS_IPV6 | |
1668 | ||
1669 | /* | |
1670 | * AF_INET6 handler in NF_INET_LOCAL_IN chain | |
1671 | * Schedule and forward packets from remote clients | |
1672 | */ | |
1673 | static unsigned int | |
1674 | ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb, | |
1675 | const struct net_device *in, | |
1676 | const struct net_device *out, | |
1677 | int (*okfn)(struct sk_buff *)) | |
1678 | { | |
1679 | return ip_vs_in(hooknum, skb, AF_INET6); | |
1680 | } | |
1681 | ||
1682 | /* | |
1683 | * AF_INET6 handler in NF_INET_LOCAL_OUT chain | |
1684 | * Schedule and forward packets from local clients | |
1685 | */ | |
1686 | static unsigned int | |
1687 | ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb, | |
1688 | const struct net_device *in, const struct net_device *out, | |
1689 | int (*okfn)(struct sk_buff *)) | |
1690 | { | |
1691 | unsigned int verdict; | |
1692 | ||
1693 | /* Disable BH in LOCAL_OUT until all places are fixed */ | |
1694 | local_bh_disable(); | |
1695 | verdict = ip_vs_in(hooknum, skb, AF_INET6); | |
1696 | local_bh_enable(); | |
1697 | return verdict; | |
1698 | } | |
1699 | ||
1700 | #endif | |
1701 | ||
1da177e4 LT |
1702 | |
1703 | /* | |
6e23ae2a | 1704 | * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP |
1da177e4 LT |
1705 | * related packets destined for 0.0.0.0/0. |
1706 | * When fwmark-based virtual service is used, such as transparent | |
1707 | * cache cluster, TCP packets can be marked and routed to ip_vs_in, | |
1708 | * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and | |
6e23ae2a | 1709 | * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain |
1da177e4 LT |
1710 | * and send them to ip_vs_in_icmp. |
1711 | */ | |
1712 | static unsigned int | |
3db05fea | 1713 | ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb, |
1da177e4 LT |
1714 | const struct net_device *in, const struct net_device *out, |
1715 | int (*okfn)(struct sk_buff *)) | |
1716 | { | |
1717 | int r; | |
1718 | ||
3db05fea | 1719 | if (ip_hdr(skb)->protocol != IPPROTO_ICMP) |
1da177e4 LT |
1720 | return NF_ACCEPT; |
1721 | ||
3db05fea | 1722 | return ip_vs_in_icmp(skb, &r, hooknum); |
1da177e4 LT |
1723 | } |
1724 | ||
2a3b791e JV |
1725 | #ifdef CONFIG_IP_VS_IPV6 |
1726 | static unsigned int | |
1727 | ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb, | |
1728 | const struct net_device *in, const struct net_device *out, | |
1729 | int (*okfn)(struct sk_buff *)) | |
1730 | { | |
1731 | int r; | |
1732 | ||
1733 | if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) | |
1734 | return NF_ACCEPT; | |
1735 | ||
1736 | return ip_vs_in_icmp_v6(skb, &r, hooknum); | |
1737 | } | |
1738 | #endif | |
1739 | ||
1da177e4 | 1740 | |
1999414a | 1741 | static struct nf_hook_ops ip_vs_ops[] __read_mostly = { |
cb59155f JA |
1742 | /* After packet filtering, change source only for VS/NAT */ |
1743 | { | |
1744 | .hook = ip_vs_reply4, | |
1745 | .owner = THIS_MODULE, | |
1746 | .pf = PF_INET, | |
1747 | .hooknum = NF_INET_LOCAL_IN, | |
1748 | .priority = 99, | |
1749 | }, | |
41c5b317 PM |
1750 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
1751 | * or VS/NAT(change destination), so that filtering rules can be | |
1752 | * applied to IPVS. */ | |
1753 | { | |
cb59155f | 1754 | .hook = ip_vs_remote_request4, |
41c5b317 PM |
1755 | .owner = THIS_MODULE, |
1756 | .pf = PF_INET, | |
cb59155f JA |
1757 | .hooknum = NF_INET_LOCAL_IN, |
1758 | .priority = 101, | |
41c5b317 | 1759 | }, |
fc604767 | 1760 | /* Before ip_vs_in, change source only for VS/NAT */ |
41c5b317 | 1761 | { |
fc604767 | 1762 | .hook = ip_vs_local_reply4, |
41c5b317 PM |
1763 | .owner = THIS_MODULE, |
1764 | .pf = PF_INET, | |
fc604767 JA |
1765 | .hooknum = NF_INET_LOCAL_OUT, |
1766 | .priority = -99, | |
41c5b317 | 1767 | }, |
cb59155f JA |
1768 | /* After mangle, schedule and forward local requests */ |
1769 | { | |
1770 | .hook = ip_vs_local_request4, | |
1771 | .owner = THIS_MODULE, | |
1772 | .pf = PF_INET, | |
1773 | .hooknum = NF_INET_LOCAL_OUT, | |
1774 | .priority = -98, | |
1775 | }, | |
41c5b317 PM |
1776 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
1777 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
1778 | { | |
1779 | .hook = ip_vs_forward_icmp, | |
1780 | .owner = THIS_MODULE, | |
1781 | .pf = PF_INET, | |
cb59155f JA |
1782 | .hooknum = NF_INET_FORWARD, |
1783 | .priority = 99, | |
41c5b317 | 1784 | }, |
fc604767 JA |
1785 | /* After packet filtering, change source only for VS/NAT */ |
1786 | { | |
1787 | .hook = ip_vs_reply4, | |
1788 | .owner = THIS_MODULE, | |
1789 | .pf = PF_INET, | |
1790 | .hooknum = NF_INET_FORWARD, | |
1791 | .priority = 100, | |
1792 | }, | |
473b23d3 | 1793 | #ifdef CONFIG_IP_VS_IPV6 |
cb59155f JA |
1794 | /* After packet filtering, change source only for VS/NAT */ |
1795 | { | |
1796 | .hook = ip_vs_reply6, | |
1797 | .owner = THIS_MODULE, | |
1798 | .pf = PF_INET6, | |
1799 | .hooknum = NF_INET_LOCAL_IN, | |
1800 | .priority = 99, | |
1801 | }, | |
473b23d3 JV |
1802 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
1803 | * or VS/NAT(change destination), so that filtering rules can be | |
1804 | * applied to IPVS. */ | |
1805 | { | |
cb59155f | 1806 | .hook = ip_vs_remote_request6, |
473b23d3 JV |
1807 | .owner = THIS_MODULE, |
1808 | .pf = PF_INET6, | |
cb59155f JA |
1809 | .hooknum = NF_INET_LOCAL_IN, |
1810 | .priority = 101, | |
473b23d3 | 1811 | }, |
fc604767 | 1812 | /* Before ip_vs_in, change source only for VS/NAT */ |
473b23d3 | 1813 | { |
fc604767 | 1814 | .hook = ip_vs_local_reply6, |
473b23d3 | 1815 | .owner = THIS_MODULE, |
fc604767 JA |
1816 | .pf = PF_INET, |
1817 | .hooknum = NF_INET_LOCAL_OUT, | |
1818 | .priority = -99, | |
473b23d3 | 1819 | }, |
cb59155f JA |
1820 | /* After mangle, schedule and forward local requests */ |
1821 | { | |
1822 | .hook = ip_vs_local_request6, | |
1823 | .owner = THIS_MODULE, | |
1824 | .pf = PF_INET6, | |
1825 | .hooknum = NF_INET_LOCAL_OUT, | |
1826 | .priority = -98, | |
1827 | }, | |
473b23d3 JV |
1828 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
1829 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
1830 | { | |
1831 | .hook = ip_vs_forward_icmp_v6, | |
1832 | .owner = THIS_MODULE, | |
1833 | .pf = PF_INET6, | |
cb59155f JA |
1834 | .hooknum = NF_INET_FORWARD, |
1835 | .priority = 99, | |
473b23d3 | 1836 | }, |
fc604767 JA |
1837 | /* After packet filtering, change source only for VS/NAT */ |
1838 | { | |
1839 | .hook = ip_vs_reply6, | |
1840 | .owner = THIS_MODULE, | |
1841 | .pf = PF_INET6, | |
1842 | .hooknum = NF_INET_FORWARD, | |
1843 | .priority = 100, | |
1844 | }, | |
473b23d3 | 1845 | #endif |
1da177e4 | 1846 | }; |
61b1ab45 HS |
1847 | /* |
1848 | * Initialize IP Virtual Server netns mem. | |
1849 | */ | |
1850 | static int __net_init __ip_vs_init(struct net *net) | |
1851 | { | |
1852 | struct netns_ipvs *ipvs; | |
1853 | ||
61b1ab45 HS |
1854 | ipvs = net_generic(net, ip_vs_net_id); |
1855 | if (ipvs == NULL) { | |
1856 | pr_err("%s(): no memory.\n", __func__); | |
1857 | return -ENOMEM; | |
1858 | } | |
f6340ee0 | 1859 | ipvs->net = net; |
61b1ab45 HS |
1860 | /* Counters used for creating unique names */ |
1861 | ipvs->gen = atomic_read(&ipvs_netns_cnt); | |
1862 | atomic_inc(&ipvs_netns_cnt); | |
1863 | net->ipvs = ipvs; | |
a870c8c5 | 1864 | printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n", |
61b1ab45 HS |
1865 | sizeof(struct netns_ipvs), ipvs->gen); |
1866 | return 0; | |
1867 | } | |
1868 | ||
1869 | static void __net_exit __ip_vs_cleanup(struct net *net) | |
1870 | { | |
9f4e1ccd | 1871 | IP_VS_DBG(10, "ipvs netns %d released\n", net_ipvs(net)->gen); |
61b1ab45 HS |
1872 | } |
1873 | ||
1874 | static struct pernet_operations ipvs_core_ops = { | |
1875 | .init = __ip_vs_init, | |
1876 | .exit = __ip_vs_cleanup, | |
1877 | .id = &ip_vs_net_id, | |
1878 | .size = sizeof(struct netns_ipvs), | |
1879 | }; | |
1da177e4 LT |
1880 | |
1881 | /* | |
1882 | * Initialize IP Virtual Server | |
1883 | */ | |
1884 | static int __init ip_vs_init(void) | |
1885 | { | |
1886 | int ret; | |
1887 | ||
61b1ab45 HS |
1888 | ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */ |
1889 | if (ret < 0) | |
1890 | return ret; | |
a919cf4b | 1891 | |
61b1ab45 | 1892 | ip_vs_estimator_init(); |
1da177e4 LT |
1893 | ret = ip_vs_control_init(); |
1894 | if (ret < 0) { | |
1e3e238e | 1895 | pr_err("can't setup control.\n"); |
a919cf4b | 1896 | goto cleanup_estimator; |
1da177e4 LT |
1897 | } |
1898 | ||
1899 | ip_vs_protocol_init(); | |
1900 | ||
1901 | ret = ip_vs_app_init(); | |
1902 | if (ret < 0) { | |
1e3e238e | 1903 | pr_err("can't setup application helper.\n"); |
1da177e4 LT |
1904 | goto cleanup_protocol; |
1905 | } | |
1906 | ||
1907 | ret = ip_vs_conn_init(); | |
1908 | if (ret < 0) { | |
1e3e238e | 1909 | pr_err("can't setup connection table.\n"); |
1da177e4 LT |
1910 | goto cleanup_app; |
1911 | } | |
1912 | ||
61b1ab45 HS |
1913 | ret = ip_vs_sync_init(); |
1914 | if (ret < 0) { | |
1915 | pr_err("can't setup sync data.\n"); | |
1916 | goto cleanup_conn; | |
1917 | } | |
1918 | ||
41c5b317 | 1919 | ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
1da177e4 | 1920 | if (ret < 0) { |
1e3e238e | 1921 | pr_err("can't register hooks.\n"); |
61b1ab45 | 1922 | goto cleanup_sync; |
1da177e4 LT |
1923 | } |
1924 | ||
1e3e238e | 1925 | pr_info("ipvs loaded.\n"); |
1da177e4 LT |
1926 | return ret; |
1927 | ||
61b1ab45 HS |
1928 | cleanup_sync: |
1929 | ip_vs_sync_cleanup(); | |
1da177e4 LT |
1930 | cleanup_conn: |
1931 | ip_vs_conn_cleanup(); | |
1932 | cleanup_app: | |
1933 | ip_vs_app_cleanup(); | |
1934 | cleanup_protocol: | |
1935 | ip_vs_protocol_cleanup(); | |
1936 | ip_vs_control_cleanup(); | |
a919cf4b SW |
1937 | cleanup_estimator: |
1938 | ip_vs_estimator_cleanup(); | |
61b1ab45 | 1939 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ |
1da177e4 LT |
1940 | return ret; |
1941 | } | |
1942 | ||
1943 | static void __exit ip_vs_cleanup(void) | |
1944 | { | |
41c5b317 | 1945 | nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
61b1ab45 | 1946 | ip_vs_sync_cleanup(); |
1da177e4 LT |
1947 | ip_vs_conn_cleanup(); |
1948 | ip_vs_app_cleanup(); | |
1949 | ip_vs_protocol_cleanup(); | |
1950 | ip_vs_control_cleanup(); | |
a919cf4b | 1951 | ip_vs_estimator_cleanup(); |
61b1ab45 | 1952 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ |
1e3e238e | 1953 | pr_info("ipvs unloaded.\n"); |
1da177e4 LT |
1954 | } |
1955 | ||
1956 | module_init(ip_vs_init); | |
1957 | module_exit(ip_vs_cleanup); | |
1958 | MODULE_LICENSE("GPL"); |