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