]>
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 | * | |
8 | * Version: $Id: ip_vs_core.c,v 1.34 2003/05/10 03:05:23 wensong Exp $ | |
9 | * | |
10 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> | |
11 | * Peter Kese <peter.kese@ijs.si> | |
12 | * Julian Anastasov <ja@ssi.bg> | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License | |
16 | * as published by the Free Software Foundation; either version | |
17 | * 2 of the License, or (at your option) any later version. | |
18 | * | |
19 | * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese, | |
20 | * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms | |
21 | * and others. | |
22 | * | |
23 | * Changes: | |
24 | * Paul `Rusty' Russell properly handle non-linear skbs | |
6869c4d8 | 25 | * Harald Welte don't use nfcache |
1da177e4 LT |
26 | * |
27 | */ | |
28 | ||
29 | #include <linux/module.h> | |
30 | #include <linux/kernel.h> | |
31 | #include <linux/ip.h> | |
32 | #include <linux/tcp.h> | |
33 | #include <linux/icmp.h> | |
34 | ||
35 | #include <net/ip.h> | |
36 | #include <net/tcp.h> | |
37 | #include <net/udp.h> | |
38 | #include <net/icmp.h> /* for icmp_send */ | |
39 | #include <net/route.h> | |
40 | ||
41 | #include <linux/netfilter.h> | |
42 | #include <linux/netfilter_ipv4.h> | |
43 | ||
44 | #include <net/ip_vs.h> | |
45 | ||
46 | ||
47 | EXPORT_SYMBOL(register_ip_vs_scheduler); | |
48 | EXPORT_SYMBOL(unregister_ip_vs_scheduler); | |
49 | EXPORT_SYMBOL(ip_vs_skb_replace); | |
50 | EXPORT_SYMBOL(ip_vs_proto_name); | |
51 | EXPORT_SYMBOL(ip_vs_conn_new); | |
52 | EXPORT_SYMBOL(ip_vs_conn_in_get); | |
53 | EXPORT_SYMBOL(ip_vs_conn_out_get); | |
54 | #ifdef CONFIG_IP_VS_PROTO_TCP | |
55 | EXPORT_SYMBOL(ip_vs_tcp_conn_listen); | |
56 | #endif | |
57 | EXPORT_SYMBOL(ip_vs_conn_put); | |
58 | #ifdef CONFIG_IP_VS_DEBUG | |
59 | EXPORT_SYMBOL(ip_vs_get_debug_level); | |
60 | #endif | |
61 | EXPORT_SYMBOL(ip_vs_make_skb_writable); | |
62 | ||
63 | ||
64 | /* ID used in ICMP lookups */ | |
65 | #define icmp_id(icmph) (((icmph)->un).echo.id) | |
66 | ||
67 | const char *ip_vs_proto_name(unsigned proto) | |
68 | { | |
69 | static char buf[20]; | |
70 | ||
71 | switch (proto) { | |
72 | case IPPROTO_IP: | |
73 | return "IP"; | |
74 | case IPPROTO_UDP: | |
75 | return "UDP"; | |
76 | case IPPROTO_TCP: | |
77 | return "TCP"; | |
78 | case IPPROTO_ICMP: | |
79 | return "ICMP"; | |
80 | default: | |
81 | sprintf(buf, "IP_%d", proto); | |
82 | return buf; | |
83 | } | |
84 | } | |
85 | ||
86 | void ip_vs_init_hash_table(struct list_head *table, int rows) | |
87 | { | |
88 | while (--rows >= 0) | |
89 | INIT_LIST_HEAD(&table[rows]); | |
90 | } | |
91 | ||
92 | static inline void | |
93 | ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
94 | { | |
95 | struct ip_vs_dest *dest = cp->dest; | |
96 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
97 | spin_lock(&dest->stats.lock); | |
98 | dest->stats.inpkts++; | |
99 | dest->stats.inbytes += skb->len; | |
100 | spin_unlock(&dest->stats.lock); | |
101 | ||
102 | spin_lock(&dest->svc->stats.lock); | |
103 | dest->svc->stats.inpkts++; | |
104 | dest->svc->stats.inbytes += skb->len; | |
105 | spin_unlock(&dest->svc->stats.lock); | |
106 | ||
107 | spin_lock(&ip_vs_stats.lock); | |
108 | ip_vs_stats.inpkts++; | |
109 | ip_vs_stats.inbytes += skb->len; | |
110 | spin_unlock(&ip_vs_stats.lock); | |
111 | } | |
112 | } | |
113 | ||
114 | ||
115 | static inline void | |
116 | ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb) | |
117 | { | |
118 | struct ip_vs_dest *dest = cp->dest; | |
119 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
120 | spin_lock(&dest->stats.lock); | |
121 | dest->stats.outpkts++; | |
122 | dest->stats.outbytes += skb->len; | |
123 | spin_unlock(&dest->stats.lock); | |
124 | ||
125 | spin_lock(&dest->svc->stats.lock); | |
126 | dest->svc->stats.outpkts++; | |
127 | dest->svc->stats.outbytes += skb->len; | |
128 | spin_unlock(&dest->svc->stats.lock); | |
129 | ||
130 | spin_lock(&ip_vs_stats.lock); | |
131 | ip_vs_stats.outpkts++; | |
132 | ip_vs_stats.outbytes += skb->len; | |
133 | spin_unlock(&ip_vs_stats.lock); | |
134 | } | |
135 | } | |
136 | ||
137 | ||
138 | static inline void | |
139 | ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc) | |
140 | { | |
141 | spin_lock(&cp->dest->stats.lock); | |
142 | cp->dest->stats.conns++; | |
143 | spin_unlock(&cp->dest->stats.lock); | |
144 | ||
145 | spin_lock(&svc->stats.lock); | |
146 | svc->stats.conns++; | |
147 | spin_unlock(&svc->stats.lock); | |
148 | ||
149 | spin_lock(&ip_vs_stats.lock); | |
150 | ip_vs_stats.conns++; | |
151 | spin_unlock(&ip_vs_stats.lock); | |
152 | } | |
153 | ||
154 | ||
155 | static inline int | |
156 | ip_vs_set_state(struct ip_vs_conn *cp, int direction, | |
157 | const struct sk_buff *skb, | |
158 | struct ip_vs_protocol *pp) | |
159 | { | |
160 | if (unlikely(!pp->state_transition)) | |
161 | return 0; | |
162 | return pp->state_transition(cp, direction, skb, pp); | |
163 | } | |
164 | ||
165 | ||
166 | int ip_vs_make_skb_writable(struct sk_buff **pskb, int writable_len) | |
167 | { | |
168 | struct sk_buff *skb = *pskb; | |
169 | ||
170 | /* skb is already used, better copy skb and its payload */ | |
171 | if (unlikely(skb_shared(skb) || skb->sk)) | |
172 | goto copy_skb; | |
173 | ||
174 | /* skb data is already used, copy it */ | |
175 | if (unlikely(skb_cloned(skb))) | |
176 | goto copy_data; | |
177 | ||
178 | return pskb_may_pull(skb, writable_len); | |
179 | ||
180 | copy_data: | |
181 | if (unlikely(writable_len > skb->len)) | |
182 | return 0; | |
183 | return !pskb_expand_head(skb, 0, 0, GFP_ATOMIC); | |
184 | ||
185 | copy_skb: | |
186 | if (unlikely(writable_len > skb->len)) | |
187 | return 0; | |
188 | skb = skb_copy(skb, GFP_ATOMIC); | |
189 | if (!skb) | |
190 | return 0; | |
191 | BUG_ON(skb_is_nonlinear(skb)); | |
192 | ||
193 | /* Rest of kernel will get very unhappy if we pass it a | |
194 | suddenly-orphaned skbuff */ | |
195 | if ((*pskb)->sk) | |
196 | skb_set_owner_w(skb, (*pskb)->sk); | |
197 | kfree_skb(*pskb); | |
198 | *pskb = skb; | |
199 | return 1; | |
200 | } | |
201 | ||
202 | /* | |
203 | * IPVS persistent scheduling function | |
204 | * It creates a connection entry according to its template if exists, | |
205 | * or selects a server and creates a connection entry plus a template. | |
206 | * Locking: we are svc user (svc->refcnt), so we hold all dests too | |
207 | * Protocols supported: TCP, UDP | |
208 | */ | |
209 | static struct ip_vs_conn * | |
210 | ip_vs_sched_persist(struct ip_vs_service *svc, | |
211 | const struct sk_buff *skb, | |
014d730d | 212 | __be16 ports[2]) |
1da177e4 LT |
213 | { |
214 | struct ip_vs_conn *cp = NULL; | |
eddc9ec5 | 215 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 LT |
216 | struct ip_vs_dest *dest; |
217 | struct ip_vs_conn *ct; | |
014d730d AV |
218 | __be16 dport; /* destination port to forward */ |
219 | __be32 snet; /* source network of the client, after masking */ | |
1da177e4 LT |
220 | |
221 | /* Mask saddr with the netmask to adjust template granularity */ | |
222 | snet = iph->saddr & svc->netmask; | |
223 | ||
224 | IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u " | |
225 | "mnet %u.%u.%u.%u\n", | |
226 | NIPQUAD(iph->saddr), ntohs(ports[0]), | |
227 | NIPQUAD(iph->daddr), ntohs(ports[1]), | |
228 | NIPQUAD(snet)); | |
229 | ||
230 | /* | |
231 | * As far as we know, FTP is a very complicated network protocol, and | |
232 | * it uses control connection and data connections. For active FTP, | |
233 | * FTP server initialize data connection to the client, its source port | |
234 | * is often 20. For passive FTP, FTP server tells the clients the port | |
235 | * that it passively listens to, and the client issues the data | |
236 | * connection. In the tunneling or direct routing mode, the load | |
237 | * balancer is on the client-to-server half of connection, the port | |
238 | * number is unknown to the load balancer. So, a conn template like | |
239 | * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP | |
240 | * service, and a template like <caddr, 0, vaddr, vport, daddr, dport> | |
241 | * is created for other persistent services. | |
242 | */ | |
243 | if (ports[1] == svc->port) { | |
244 | /* Check if a template already exists */ | |
245 | if (svc->port != FTPPORT) | |
87375ab4 | 246 | ct = ip_vs_ct_in_get(iph->protocol, snet, 0, |
1da177e4 LT |
247 | iph->daddr, ports[1]); |
248 | else | |
87375ab4 | 249 | ct = ip_vs_ct_in_get(iph->protocol, snet, 0, |
1da177e4 LT |
250 | iph->daddr, 0); |
251 | ||
252 | if (!ct || !ip_vs_check_template(ct)) { | |
253 | /* | |
254 | * No template found or the dest of the connection | |
255 | * template is not available. | |
256 | */ | |
257 | dest = svc->scheduler->schedule(svc, skb); | |
258 | if (dest == NULL) { | |
259 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); | |
260 | return NULL; | |
261 | } | |
262 | ||
263 | /* | |
264 | * Create a template like <protocol,caddr,0, | |
265 | * vaddr,vport,daddr,dport> for non-ftp service, | |
266 | * and <protocol,caddr,0,vaddr,0,daddr,0> | |
267 | * for ftp service. | |
268 | */ | |
269 | if (svc->port != FTPPORT) | |
270 | ct = ip_vs_conn_new(iph->protocol, | |
271 | snet, 0, | |
272 | iph->daddr, | |
273 | ports[1], | |
274 | dest->addr, dest->port, | |
87375ab4 | 275 | IP_VS_CONN_F_TEMPLATE, |
1da177e4 LT |
276 | dest); |
277 | else | |
278 | ct = ip_vs_conn_new(iph->protocol, | |
279 | snet, 0, | |
280 | iph->daddr, 0, | |
281 | dest->addr, 0, | |
87375ab4 | 282 | IP_VS_CONN_F_TEMPLATE, |
1da177e4 LT |
283 | dest); |
284 | if (ct == NULL) | |
285 | return NULL; | |
286 | ||
287 | ct->timeout = svc->timeout; | |
288 | } else { | |
289 | /* set destination with the found template */ | |
290 | dest = ct->dest; | |
291 | } | |
292 | dport = dest->port; | |
293 | } else { | |
294 | /* | |
295 | * Note: persistent fwmark-based services and persistent | |
296 | * port zero service are handled here. | |
297 | * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0> | |
298 | * port zero template: <protocol,caddr,0,vaddr,0,daddr,0> | |
299 | */ | |
300 | if (svc->fwmark) | |
87375ab4 | 301 | ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0, |
1da177e4 LT |
302 | htonl(svc->fwmark), 0); |
303 | else | |
87375ab4 | 304 | ct = ip_vs_ct_in_get(iph->protocol, snet, 0, |
1da177e4 LT |
305 | iph->daddr, 0); |
306 | ||
307 | if (!ct || !ip_vs_check_template(ct)) { | |
308 | /* | |
309 | * If it is not persistent port zero, return NULL, | |
310 | * otherwise create a connection template. | |
311 | */ | |
312 | if (svc->port) | |
313 | return NULL; | |
314 | ||
315 | dest = svc->scheduler->schedule(svc, skb); | |
316 | if (dest == NULL) { | |
317 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); | |
318 | return NULL; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Create a template according to the service | |
323 | */ | |
324 | if (svc->fwmark) | |
325 | ct = ip_vs_conn_new(IPPROTO_IP, | |
326 | snet, 0, | |
327 | htonl(svc->fwmark), 0, | |
328 | dest->addr, 0, | |
87375ab4 | 329 | IP_VS_CONN_F_TEMPLATE, |
1da177e4 LT |
330 | dest); |
331 | else | |
332 | ct = ip_vs_conn_new(iph->protocol, | |
333 | snet, 0, | |
334 | iph->daddr, 0, | |
335 | dest->addr, 0, | |
87375ab4 | 336 | IP_VS_CONN_F_TEMPLATE, |
1da177e4 LT |
337 | dest); |
338 | if (ct == NULL) | |
339 | return NULL; | |
340 | ||
341 | ct->timeout = svc->timeout; | |
342 | } else { | |
343 | /* set destination with the found template */ | |
344 | dest = ct->dest; | |
345 | } | |
346 | dport = ports[1]; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Create a new connection according to the template | |
351 | */ | |
352 | cp = ip_vs_conn_new(iph->protocol, | |
353 | iph->saddr, ports[0], | |
354 | iph->daddr, ports[1], | |
355 | dest->addr, dport, | |
356 | 0, | |
357 | dest); | |
358 | if (cp == NULL) { | |
359 | ip_vs_conn_put(ct); | |
360 | return NULL; | |
361 | } | |
362 | ||
363 | /* | |
364 | * Add its control | |
365 | */ | |
366 | ip_vs_control_add(cp, ct); | |
367 | ip_vs_conn_put(ct); | |
368 | ||
369 | ip_vs_conn_stats(cp, svc); | |
370 | return cp; | |
371 | } | |
372 | ||
373 | ||
374 | /* | |
375 | * IPVS main scheduling function | |
376 | * It selects a server according to the virtual service, and | |
377 | * creates a connection entry. | |
378 | * Protocols supported: TCP, UDP | |
379 | */ | |
380 | struct ip_vs_conn * | |
381 | ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb) | |
382 | { | |
383 | struct ip_vs_conn *cp = NULL; | |
eddc9ec5 | 384 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 385 | struct ip_vs_dest *dest; |
014d730d | 386 | __be16 _ports[2], *pptr; |
1da177e4 LT |
387 | |
388 | pptr = skb_header_pointer(skb, iph->ihl*4, | |
389 | sizeof(_ports), _ports); | |
390 | if (pptr == NULL) | |
391 | return NULL; | |
392 | ||
393 | /* | |
394 | * Persistent service | |
395 | */ | |
396 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) | |
397 | return ip_vs_sched_persist(svc, skb, pptr); | |
398 | ||
399 | /* | |
400 | * Non-persistent service | |
401 | */ | |
402 | if (!svc->fwmark && pptr[1] != svc->port) { | |
403 | if (!svc->port) | |
404 | IP_VS_ERR("Schedule: port zero only supported " | |
405 | "in persistent services, " | |
406 | "check your ipvs configuration\n"); | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | dest = svc->scheduler->schedule(svc, skb); | |
411 | if (dest == NULL) { | |
412 | IP_VS_DBG(1, "Schedule: no dest found.\n"); | |
413 | return NULL; | |
414 | } | |
415 | ||
416 | /* | |
417 | * Create a connection entry. | |
418 | */ | |
419 | cp = ip_vs_conn_new(iph->protocol, | |
420 | iph->saddr, pptr[0], | |
421 | iph->daddr, pptr[1], | |
422 | dest->addr, dest->port?dest->port:pptr[1], | |
423 | 0, | |
424 | dest); | |
425 | if (cp == NULL) | |
426 | return NULL; | |
427 | ||
428 | IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u " | |
4b5bdf5c | 429 | "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n", |
1da177e4 LT |
430 | ip_vs_fwd_tag(cp), |
431 | NIPQUAD(cp->caddr), ntohs(cp->cport), | |
432 | NIPQUAD(cp->vaddr), ntohs(cp->vport), | |
433 | NIPQUAD(cp->daddr), ntohs(cp->dport), | |
434 | cp->flags, atomic_read(&cp->refcnt)); | |
435 | ||
436 | ip_vs_conn_stats(cp, svc); | |
437 | return cp; | |
438 | } | |
439 | ||
440 | ||
441 | /* | |
442 | * Pass or drop the packet. | |
443 | * Called by ip_vs_in, when the virtual service is available but | |
444 | * no destination is available for a new connection. | |
445 | */ | |
446 | int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, | |
447 | struct ip_vs_protocol *pp) | |
448 | { | |
014d730d | 449 | __be16 _ports[2], *pptr; |
eddc9ec5 | 450 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 LT |
451 | |
452 | pptr = skb_header_pointer(skb, iph->ihl*4, | |
453 | sizeof(_ports), _ports); | |
454 | if (pptr == NULL) { | |
455 | ip_vs_service_put(svc); | |
456 | return NF_DROP; | |
457 | } | |
458 | ||
459 | /* if it is fwmark-based service, the cache_bypass sysctl is up | |
460 | and the destination is RTN_UNICAST (and not local), then create | |
461 | a cache_bypass connection entry */ | |
462 | if (sysctl_ip_vs_cache_bypass && svc->fwmark | |
463 | && (inet_addr_type(iph->daddr) == RTN_UNICAST)) { | |
464 | int ret, cs; | |
465 | struct ip_vs_conn *cp; | |
466 | ||
467 | ip_vs_service_put(svc); | |
468 | ||
469 | /* create a new connection entry */ | |
470 | IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n"); | |
471 | cp = ip_vs_conn_new(iph->protocol, | |
472 | iph->saddr, pptr[0], | |
473 | iph->daddr, pptr[1], | |
474 | 0, 0, | |
475 | IP_VS_CONN_F_BYPASS, | |
476 | NULL); | |
477 | if (cp == NULL) | |
478 | return NF_DROP; | |
479 | ||
480 | /* statistics */ | |
481 | ip_vs_in_stats(cp, skb); | |
482 | ||
483 | /* set state */ | |
484 | cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp); | |
485 | ||
486 | /* transmit the first SYN packet */ | |
487 | ret = cp->packet_xmit(skb, cp, pp); | |
488 | /* do not touch skb anymore */ | |
489 | ||
490 | atomic_inc(&cp->in_pkts); | |
491 | ip_vs_conn_put(cp); | |
492 | return ret; | |
493 | } | |
494 | ||
495 | /* | |
496 | * When the virtual ftp service is presented, packets destined | |
497 | * for other services on the VIP may get here (except services | |
498 | * listed in the ipvs table), pass the packets, because it is | |
499 | * not ipvs job to decide to drop the packets. | |
500 | */ | |
501 | if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) { | |
502 | ip_vs_service_put(svc); | |
503 | return NF_ACCEPT; | |
504 | } | |
505 | ||
506 | ip_vs_service_put(svc); | |
507 | ||
508 | /* | |
509 | * Notify the client that the destination is unreachable, and | |
510 | * release the socket buffer. | |
511 | * Since it is in IP layer, the TCP socket is not actually | |
512 | * created, the TCP RST packet cannot be sent, instead that | |
513 | * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ | |
514 | */ | |
515 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | |
516 | return NF_DROP; | |
517 | } | |
518 | ||
519 | ||
520 | /* | |
521 | * It is hooked before NF_IP_PRI_NAT_SRC at the NF_IP_POST_ROUTING | |
522 | * chain, and is used for VS/NAT. | |
523 | * It detects packets for VS/NAT connections and sends the packets | |
524 | * immediately. This can avoid that iptable_nat mangles the packets | |
525 | * for VS/NAT. | |
526 | */ | |
527 | static unsigned int ip_vs_post_routing(unsigned int hooknum, | |
528 | struct sk_buff **pskb, | |
529 | const struct net_device *in, | |
530 | const struct net_device *out, | |
531 | int (*okfn)(struct sk_buff *)) | |
532 | { | |
6869c4d8 | 533 | if (!((*pskb)->ipvs_property)) |
1da177e4 | 534 | return NF_ACCEPT; |
1da177e4 | 535 | /* The packet was sent from IPVS, exit this chain */ |
abbcc739 | 536 | return NF_STOP; |
1da177e4 LT |
537 | } |
538 | ||
b1550f22 | 539 | __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset) |
1da177e4 | 540 | { |
d3bc23e7 | 541 | return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0)); |
1da177e4 LT |
542 | } |
543 | ||
544 | static inline struct sk_buff * | |
545 | ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user) | |
546 | { | |
547 | skb = ip_defrag(skb, user); | |
548 | if (skb) | |
eddc9ec5 | 549 | ip_send_check(ip_hdr(skb)); |
1da177e4 LT |
550 | return skb; |
551 | } | |
552 | ||
553 | /* | |
554 | * Packet has been made sufficiently writable in caller | |
555 | * - inout: 1=in->out, 0=out->in | |
556 | */ | |
557 | void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, | |
558 | struct ip_vs_conn *cp, int inout) | |
559 | { | |
eddc9ec5 | 560 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 561 | unsigned int icmp_offset = iph->ihl*4; |
d56f90a7 ACM |
562 | struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) + |
563 | icmp_offset); | |
1da177e4 LT |
564 | struct iphdr *ciph = (struct iphdr *)(icmph + 1); |
565 | ||
566 | if (inout) { | |
567 | iph->saddr = cp->vaddr; | |
568 | ip_send_check(iph); | |
569 | ciph->daddr = cp->vaddr; | |
570 | ip_send_check(ciph); | |
571 | } else { | |
572 | iph->daddr = cp->daddr; | |
573 | ip_send_check(iph); | |
574 | ciph->saddr = cp->daddr; | |
575 | ip_send_check(ciph); | |
576 | } | |
577 | ||
578 | /* the TCP/UDP port */ | |
579 | if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) { | |
014d730d | 580 | __be16 *ports = (void *)ciph + ciph->ihl*4; |
1da177e4 LT |
581 | |
582 | if (inout) | |
583 | ports[1] = cp->vport; | |
584 | else | |
585 | ports[0] = cp->dport; | |
586 | } | |
587 | ||
588 | /* And finally the ICMP checksum */ | |
589 | icmph->checksum = 0; | |
590 | icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset); | |
591 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
592 | ||
593 | if (inout) | |
594 | IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph, | |
595 | "Forwarding altered outgoing ICMP"); | |
596 | else | |
597 | IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph, | |
598 | "Forwarding altered incoming ICMP"); | |
599 | } | |
600 | ||
601 | /* | |
602 | * Handle ICMP messages in the inside-to-outside direction (outgoing). | |
603 | * Find any that might be relevant, check against existing connections, | |
604 | * forward to the right destination host if relevant. | |
605 | * Currently handles error types - unreachable, quench, ttl exceeded. | |
606 | * (Only used in VS/NAT) | |
607 | */ | |
608 | static int ip_vs_out_icmp(struct sk_buff **pskb, int *related) | |
609 | { | |
610 | struct sk_buff *skb = *pskb; | |
611 | struct iphdr *iph; | |
612 | struct icmphdr _icmph, *ic; | |
613 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
614 | struct ip_vs_conn *cp; | |
615 | struct ip_vs_protocol *pp; | |
616 | unsigned int offset, ihl, verdict; | |
617 | ||
618 | *related = 1; | |
619 | ||
620 | /* reassemble IP fragments */ | |
eddc9ec5 | 621 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
1da177e4 LT |
622 | skb = ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT); |
623 | if (!skb) | |
624 | return NF_STOLEN; | |
625 | *pskb = skb; | |
626 | } | |
627 | ||
eddc9ec5 | 628 | iph = ip_hdr(skb); |
1da177e4 LT |
629 | offset = ihl = iph->ihl * 4; |
630 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
631 | if (ic == NULL) | |
632 | return NF_DROP; | |
633 | ||
634 | IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n", | |
635 | ic->type, ntohs(icmp_id(ic)), | |
636 | NIPQUAD(iph->saddr), NIPQUAD(iph->daddr)); | |
637 | ||
638 | /* | |
639 | * Work through seeing if this is for us. | |
640 | * These checks are supposed to be in an order that means easy | |
641 | * things are checked first to speed up processing.... however | |
642 | * this means that some packets will manage to get a long way | |
643 | * down this stack and then be rejected, but that's life. | |
644 | */ | |
645 | if ((ic->type != ICMP_DEST_UNREACH) && | |
646 | (ic->type != ICMP_SOURCE_QUENCH) && | |
647 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
648 | *related = 0; | |
649 | return NF_ACCEPT; | |
650 | } | |
651 | ||
652 | /* Now find the contained IP header */ | |
653 | offset += sizeof(_icmph); | |
654 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
655 | if (cih == NULL) | |
656 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
657 | ||
658 | pp = ip_vs_proto_get(cih->protocol); | |
659 | if (!pp) | |
660 | return NF_ACCEPT; | |
661 | ||
662 | /* Is the embedded protocol header present? */ | |
4412ec49 | 663 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
664 | pp->dont_defrag)) |
665 | return NF_ACCEPT; | |
666 | ||
667 | IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for"); | |
668 | ||
669 | offset += cih->ihl * 4; | |
670 | ||
671 | /* The embedded headers contain source and dest in reverse order */ | |
672 | cp = pp->conn_out_get(skb, pp, cih, offset, 1); | |
673 | if (!cp) | |
674 | return NF_ACCEPT; | |
675 | ||
676 | verdict = NF_DROP; | |
677 | ||
678 | if (IP_VS_FWD_METHOD(cp) != 0) { | |
679 | IP_VS_ERR("shouldn't reach here, because the box is on the" | |
680 | "half connection in the tun/dr module.\n"); | |
681 | } | |
682 | ||
683 | /* Ensure the checksum is correct */ | |
684 | if (skb->ip_summed != CHECKSUM_UNNECESSARY && | |
685 | ip_vs_checksum_complete(skb, ihl)) { | |
686 | /* Failed checksum! */ | |
687 | IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n", | |
688 | NIPQUAD(iph->saddr)); | |
689 | goto out; | |
690 | } | |
691 | ||
692 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol) | |
693 | offset += 2 * sizeof(__u16); | |
694 | if (!ip_vs_make_skb_writable(pskb, offset)) | |
695 | goto out; | |
696 | skb = *pskb; | |
697 | ||
698 | ip_vs_nat_icmp(skb, pp, cp, 1); | |
699 | ||
700 | /* do the statistics and put it back */ | |
701 | ip_vs_out_stats(cp, skb); | |
702 | ||
6869c4d8 | 703 | skb->ipvs_property = 1; |
1da177e4 LT |
704 | verdict = NF_ACCEPT; |
705 | ||
706 | out: | |
707 | __ip_vs_conn_put(cp); | |
708 | ||
709 | return verdict; | |
710 | } | |
711 | ||
712 | static inline int is_tcp_reset(const struct sk_buff *skb) | |
713 | { | |
714 | struct tcphdr _tcph, *th; | |
715 | ||
c9bdd4b5 | 716 | th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph); |
1da177e4 LT |
717 | if (th == NULL) |
718 | return 0; | |
719 | return th->rst; | |
720 | } | |
721 | ||
722 | /* | |
723 | * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT. | |
724 | * Check if outgoing packet belongs to the established ip_vs_conn, | |
725 | * rewrite addresses of the packet and send it on its way... | |
726 | */ | |
727 | static unsigned int | |
728 | ip_vs_out(unsigned int hooknum, struct sk_buff **pskb, | |
729 | const struct net_device *in, const struct net_device *out, | |
730 | int (*okfn)(struct sk_buff *)) | |
731 | { | |
732 | struct sk_buff *skb = *pskb; | |
733 | struct iphdr *iph; | |
734 | struct ip_vs_protocol *pp; | |
735 | struct ip_vs_conn *cp; | |
736 | int ihl; | |
737 | ||
738 | EnterFunction(11); | |
739 | ||
6869c4d8 | 740 | if (skb->ipvs_property) |
1da177e4 LT |
741 | return NF_ACCEPT; |
742 | ||
eddc9ec5 | 743 | iph = ip_hdr(skb); |
1da177e4 LT |
744 | if (unlikely(iph->protocol == IPPROTO_ICMP)) { |
745 | int related, verdict = ip_vs_out_icmp(pskb, &related); | |
746 | ||
747 | if (related) | |
748 | return verdict; | |
749 | skb = *pskb; | |
eddc9ec5 | 750 | iph = ip_hdr(skb); |
1da177e4 LT |
751 | } |
752 | ||
753 | pp = ip_vs_proto_get(iph->protocol); | |
754 | if (unlikely(!pp)) | |
755 | return NF_ACCEPT; | |
756 | ||
757 | /* reassemble IP fragments */ | |
4412ec49 | 758 | if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) && |
1da177e4 LT |
759 | !pp->dont_defrag)) { |
760 | skb = ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT); | |
761 | if (!skb) | |
762 | return NF_STOLEN; | |
eddc9ec5 | 763 | iph = ip_hdr(skb); |
1da177e4 LT |
764 | *pskb = skb; |
765 | } | |
766 | ||
767 | ihl = iph->ihl << 2; | |
768 | ||
769 | /* | |
770 | * Check if the packet belongs to an existing entry | |
771 | */ | |
772 | cp = pp->conn_out_get(skb, pp, iph, ihl, 0); | |
773 | ||
774 | if (unlikely(!cp)) { | |
775 | if (sysctl_ip_vs_nat_icmp_send && | |
776 | (pp->protocol == IPPROTO_TCP || | |
777 | pp->protocol == IPPROTO_UDP)) { | |
014d730d | 778 | __be16 _ports[2], *pptr; |
1da177e4 LT |
779 | |
780 | pptr = skb_header_pointer(skb, ihl, | |
781 | sizeof(_ports), _ports); | |
782 | if (pptr == NULL) | |
783 | return NF_ACCEPT; /* Not for me */ | |
784 | if (ip_vs_lookup_real_service(iph->protocol, | |
785 | iph->saddr, pptr[0])) { | |
786 | /* | |
787 | * Notify the real server: there is no | |
788 | * existing entry if it is not RST | |
789 | * packet or not TCP packet. | |
790 | */ | |
791 | if (iph->protocol != IPPROTO_TCP | |
792 | || !is_tcp_reset(skb)) { | |
793 | icmp_send(skb,ICMP_DEST_UNREACH, | |
794 | ICMP_PORT_UNREACH, 0); | |
795 | return NF_DROP; | |
796 | } | |
797 | } | |
798 | } | |
799 | IP_VS_DBG_PKT(12, pp, skb, 0, | |
800 | "packet continues traversal as normal"); | |
801 | return NF_ACCEPT; | |
802 | } | |
803 | ||
804 | IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet"); | |
805 | ||
806 | if (!ip_vs_make_skb_writable(pskb, ihl)) | |
807 | goto drop; | |
808 | ||
809 | /* mangle the packet */ | |
810 | if (pp->snat_handler && !pp->snat_handler(pskb, pp, cp)) | |
811 | goto drop; | |
812 | skb = *pskb; | |
eddc9ec5 ACM |
813 | ip_hdr(skb)->saddr = cp->vaddr; |
814 | ip_send_check(ip_hdr(skb)); | |
1da177e4 | 815 | |
e905a9ed YH |
816 | /* For policy routing, packets originating from this |
817 | * machine itself may be routed differently to packets | |
818 | * passing through. We want this packet to be routed as | |
819 | * if it came from this machine itself. So re-compute | |
820 | * the routing information. | |
821 | */ | |
822 | if (ip_route_me_harder(pskb, RTN_LOCAL) != 0) | |
823 | goto drop; | |
901eaf6c SH |
824 | skb = *pskb; |
825 | ||
1da177e4 LT |
826 | IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT"); |
827 | ||
828 | ip_vs_out_stats(cp, skb); | |
829 | ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp); | |
830 | ip_vs_conn_put(cp); | |
831 | ||
6869c4d8 | 832 | skb->ipvs_property = 1; |
1da177e4 LT |
833 | |
834 | LeaveFunction(11); | |
835 | return NF_ACCEPT; | |
836 | ||
837 | drop: | |
838 | ip_vs_conn_put(cp); | |
839 | kfree_skb(*pskb); | |
840 | return NF_STOLEN; | |
841 | } | |
842 | ||
843 | ||
844 | /* | |
845 | * Handle ICMP messages in the outside-to-inside direction (incoming). | |
846 | * Find any that might be relevant, check against existing connections, | |
847 | * forward to the right destination host if relevant. | |
848 | * Currently handles error types - unreachable, quench, ttl exceeded. | |
849 | */ | |
e905a9ed | 850 | static int |
1da177e4 LT |
851 | ip_vs_in_icmp(struct sk_buff **pskb, int *related, unsigned int hooknum) |
852 | { | |
853 | struct sk_buff *skb = *pskb; | |
854 | struct iphdr *iph; | |
855 | struct icmphdr _icmph, *ic; | |
856 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ | |
857 | struct ip_vs_conn *cp; | |
858 | struct ip_vs_protocol *pp; | |
859 | unsigned int offset, ihl, verdict; | |
860 | ||
861 | *related = 1; | |
862 | ||
863 | /* reassemble IP fragments */ | |
eddc9ec5 | 864 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
1da177e4 | 865 | skb = ip_vs_gather_frags(skb, |
e905a9ed | 866 | hooknum == NF_IP_LOCAL_IN ? |
1da177e4 LT |
867 | IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD); |
868 | if (!skb) | |
869 | return NF_STOLEN; | |
870 | *pskb = skb; | |
871 | } | |
872 | ||
eddc9ec5 | 873 | iph = ip_hdr(skb); |
1da177e4 LT |
874 | offset = ihl = iph->ihl * 4; |
875 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); | |
876 | if (ic == NULL) | |
877 | return NF_DROP; | |
878 | ||
879 | IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n", | |
880 | ic->type, ntohs(icmp_id(ic)), | |
881 | NIPQUAD(iph->saddr), NIPQUAD(iph->daddr)); | |
882 | ||
883 | /* | |
884 | * Work through seeing if this is for us. | |
885 | * These checks are supposed to be in an order that means easy | |
886 | * things are checked first to speed up processing.... however | |
887 | * this means that some packets will manage to get a long way | |
888 | * down this stack and then be rejected, but that's life. | |
889 | */ | |
890 | if ((ic->type != ICMP_DEST_UNREACH) && | |
891 | (ic->type != ICMP_SOURCE_QUENCH) && | |
892 | (ic->type != ICMP_TIME_EXCEEDED)) { | |
893 | *related = 0; | |
894 | return NF_ACCEPT; | |
895 | } | |
896 | ||
897 | /* Now find the contained IP header */ | |
898 | offset += sizeof(_icmph); | |
899 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); | |
900 | if (cih == NULL) | |
901 | return NF_ACCEPT; /* The packet looks wrong, ignore */ | |
902 | ||
903 | pp = ip_vs_proto_get(cih->protocol); | |
904 | if (!pp) | |
905 | return NF_ACCEPT; | |
906 | ||
907 | /* Is the embedded protocol header present? */ | |
4412ec49 | 908 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
1da177e4 LT |
909 | pp->dont_defrag)) |
910 | return NF_ACCEPT; | |
911 | ||
912 | IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for"); | |
913 | ||
914 | offset += cih->ihl * 4; | |
915 | ||
916 | /* The embedded headers contain source and dest in reverse order */ | |
917 | cp = pp->conn_in_get(skb, pp, cih, offset, 1); | |
918 | if (!cp) | |
919 | return NF_ACCEPT; | |
920 | ||
921 | verdict = NF_DROP; | |
922 | ||
923 | /* Ensure the checksum is correct */ | |
924 | if (skb->ip_summed != CHECKSUM_UNNECESSARY && | |
925 | ip_vs_checksum_complete(skb, ihl)) { | |
926 | /* Failed checksum! */ | |
927 | IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n", | |
928 | NIPQUAD(iph->saddr)); | |
929 | goto out; | |
930 | } | |
931 | ||
932 | /* do the statistics and put it back */ | |
933 | ip_vs_in_stats(cp, skb); | |
934 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol) | |
935 | offset += 2 * sizeof(__u16); | |
936 | verdict = ip_vs_icmp_xmit(skb, cp, pp, offset); | |
937 | /* do not touch skb anymore */ | |
938 | ||
939 | out: | |
940 | __ip_vs_conn_put(cp); | |
941 | ||
942 | return verdict; | |
943 | } | |
944 | ||
945 | /* | |
946 | * Check if it's for virtual services, look it up, | |
947 | * and send it on its way... | |
948 | */ | |
949 | static unsigned int | |
950 | ip_vs_in(unsigned int hooknum, struct sk_buff **pskb, | |
951 | const struct net_device *in, const struct net_device *out, | |
952 | int (*okfn)(struct sk_buff *)) | |
953 | { | |
954 | struct sk_buff *skb = *pskb; | |
955 | struct iphdr *iph; | |
956 | struct ip_vs_protocol *pp; | |
957 | struct ip_vs_conn *cp; | |
958 | int ret, restart; | |
959 | int ihl; | |
960 | ||
961 | /* | |
962 | * Big tappo: only PACKET_HOST (neither loopback nor mcasts) | |
963 | * ... don't know why 1st test DOES NOT include 2nd (?) | |
964 | */ | |
965 | if (unlikely(skb->pkt_type != PACKET_HOST | |
966 | || skb->dev == &loopback_dev || skb->sk)) { | |
967 | IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n", | |
968 | skb->pkt_type, | |
eddc9ec5 ACM |
969 | ip_hdr(skb)->protocol, |
970 | NIPQUAD(ip_hdr(skb)->daddr)); | |
1da177e4 LT |
971 | return NF_ACCEPT; |
972 | } | |
973 | ||
eddc9ec5 | 974 | iph = ip_hdr(skb); |
1da177e4 LT |
975 | if (unlikely(iph->protocol == IPPROTO_ICMP)) { |
976 | int related, verdict = ip_vs_in_icmp(pskb, &related, hooknum); | |
977 | ||
978 | if (related) | |
979 | return verdict; | |
980 | skb = *pskb; | |
eddc9ec5 | 981 | iph = ip_hdr(skb); |
1da177e4 LT |
982 | } |
983 | ||
984 | /* Protocol supported? */ | |
985 | pp = ip_vs_proto_get(iph->protocol); | |
986 | if (unlikely(!pp)) | |
987 | return NF_ACCEPT; | |
988 | ||
989 | ihl = iph->ihl << 2; | |
990 | ||
991 | /* | |
992 | * Check if the packet belongs to an existing connection entry | |
993 | */ | |
994 | cp = pp->conn_in_get(skb, pp, iph, ihl, 0); | |
995 | ||
996 | if (unlikely(!cp)) { | |
997 | int v; | |
998 | ||
999 | if (!pp->conn_schedule(skb, pp, &v, &cp)) | |
1000 | return v; | |
1001 | } | |
1002 | ||
1003 | if (unlikely(!cp)) { | |
1004 | /* sorry, all this trouble for a no-hit :) */ | |
1005 | IP_VS_DBG_PKT(12, pp, skb, 0, | |
1006 | "packet continues traversal as normal"); | |
1007 | return NF_ACCEPT; | |
1008 | } | |
1009 | ||
1010 | IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet"); | |
1011 | ||
1012 | /* Check the server status */ | |
1013 | if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
1014 | /* the destination server is not available */ | |
1015 | ||
1016 | if (sysctl_ip_vs_expire_nodest_conn) { | |
1017 | /* try to expire the connection immediately */ | |
1018 | ip_vs_conn_expire_now(cp); | |
1da177e4 | 1019 | } |
dc8103f2 JA |
1020 | /* don't restart its timer, and silently |
1021 | drop the packet. */ | |
1022 | __ip_vs_conn_put(cp); | |
1da177e4 LT |
1023 | return NF_DROP; |
1024 | } | |
1025 | ||
1026 | ip_vs_in_stats(cp, skb); | |
1027 | restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp); | |
1028 | if (cp->packet_xmit) | |
1029 | ret = cp->packet_xmit(skb, cp, pp); | |
1030 | /* do not touch skb anymore */ | |
1031 | else { | |
1032 | IP_VS_DBG_RL("warning: packet_xmit is null"); | |
1033 | ret = NF_ACCEPT; | |
1034 | } | |
1035 | ||
1036 | /* increase its packet counter and check if it is needed | |
1037 | to be synchronized */ | |
1038 | atomic_inc(&cp->in_pkts); | |
1039 | if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && | |
1040 | (cp->protocol != IPPROTO_TCP || | |
1041 | cp->state == IP_VS_TCP_S_ESTABLISHED) && | |
1042 | (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1] | |
1043 | == sysctl_ip_vs_sync_threshold[0])) | |
1044 | ip_vs_sync_conn(cp); | |
1045 | ||
1046 | ip_vs_conn_put(cp); | |
1047 | return ret; | |
1048 | } | |
1049 | ||
1050 | ||
1051 | /* | |
1052 | * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP | |
1053 | * related packets destined for 0.0.0.0/0. | |
1054 | * When fwmark-based virtual service is used, such as transparent | |
1055 | * cache cluster, TCP packets can be marked and routed to ip_vs_in, | |
1056 | * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and | |
1057 | * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain | |
1058 | * and send them to ip_vs_in_icmp. | |
1059 | */ | |
1060 | static unsigned int | |
1061 | ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff **pskb, | |
1062 | const struct net_device *in, const struct net_device *out, | |
1063 | int (*okfn)(struct sk_buff *)) | |
1064 | { | |
1065 | int r; | |
1066 | ||
eddc9ec5 | 1067 | if (ip_hdr(*pskb)->protocol != IPPROTO_ICMP) |
1da177e4 LT |
1068 | return NF_ACCEPT; |
1069 | ||
1070 | return ip_vs_in_icmp(pskb, &r, hooknum); | |
1071 | } | |
1072 | ||
1073 | ||
1074 | /* After packet filtering, forward packet through VS/DR, VS/TUN, | |
1075 | or VS/NAT(change destination), so that filtering rules can be | |
1076 | applied to IPVS. */ | |
1077 | static struct nf_hook_ops ip_vs_in_ops = { | |
1078 | .hook = ip_vs_in, | |
1079 | .owner = THIS_MODULE, | |
1080 | .pf = PF_INET, | |
1081 | .hooknum = NF_IP_LOCAL_IN, | |
1082 | .priority = 100, | |
1083 | }; | |
1084 | ||
1085 | /* After packet filtering, change source only for VS/NAT */ | |
1086 | static struct nf_hook_ops ip_vs_out_ops = { | |
1087 | .hook = ip_vs_out, | |
1088 | .owner = THIS_MODULE, | |
1089 | .pf = PF_INET, | |
1090 | .hooknum = NF_IP_FORWARD, | |
1091 | .priority = 100, | |
1092 | }; | |
1093 | ||
1094 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp | |
1095 | destined for 0.0.0.0/0, which is for incoming IPVS connections */ | |
1096 | static struct nf_hook_ops ip_vs_forward_icmp_ops = { | |
1097 | .hook = ip_vs_forward_icmp, | |
1098 | .owner = THIS_MODULE, | |
1099 | .pf = PF_INET, | |
1100 | .hooknum = NF_IP_FORWARD, | |
1101 | .priority = 99, | |
1102 | }; | |
1103 | ||
1104 | /* Before the netfilter connection tracking, exit from POST_ROUTING */ | |
1105 | static struct nf_hook_ops ip_vs_post_routing_ops = { | |
1106 | .hook = ip_vs_post_routing, | |
1107 | .owner = THIS_MODULE, | |
1108 | .pf = PF_INET, | |
1109 | .hooknum = NF_IP_POST_ROUTING, | |
1110 | .priority = NF_IP_PRI_NAT_SRC-1, | |
1111 | }; | |
1112 | ||
1113 | ||
1114 | /* | |
1115 | * Initialize IP Virtual Server | |
1116 | */ | |
1117 | static int __init ip_vs_init(void) | |
1118 | { | |
1119 | int ret; | |
1120 | ||
1121 | ret = ip_vs_control_init(); | |
1122 | if (ret < 0) { | |
1123 | IP_VS_ERR("can't setup control.\n"); | |
1124 | goto cleanup_nothing; | |
1125 | } | |
1126 | ||
1127 | ip_vs_protocol_init(); | |
1128 | ||
1129 | ret = ip_vs_app_init(); | |
1130 | if (ret < 0) { | |
1131 | IP_VS_ERR("can't setup application helper.\n"); | |
1132 | goto cleanup_protocol; | |
1133 | } | |
1134 | ||
1135 | ret = ip_vs_conn_init(); | |
1136 | if (ret < 0) { | |
1137 | IP_VS_ERR("can't setup connection table.\n"); | |
1138 | goto cleanup_app; | |
1139 | } | |
1140 | ||
1141 | ret = nf_register_hook(&ip_vs_in_ops); | |
1142 | if (ret < 0) { | |
1143 | IP_VS_ERR("can't register in hook.\n"); | |
1144 | goto cleanup_conn; | |
1145 | } | |
1146 | ||
1147 | ret = nf_register_hook(&ip_vs_out_ops); | |
1148 | if (ret < 0) { | |
1149 | IP_VS_ERR("can't register out hook.\n"); | |
1150 | goto cleanup_inops; | |
1151 | } | |
1152 | ret = nf_register_hook(&ip_vs_post_routing_ops); | |
1153 | if (ret < 0) { | |
1154 | IP_VS_ERR("can't register post_routing hook.\n"); | |
1155 | goto cleanup_outops; | |
1156 | } | |
1157 | ret = nf_register_hook(&ip_vs_forward_icmp_ops); | |
1158 | if (ret < 0) { | |
1159 | IP_VS_ERR("can't register forward_icmp hook.\n"); | |
1160 | goto cleanup_postroutingops; | |
1161 | } | |
1162 | ||
1163 | IP_VS_INFO("ipvs loaded.\n"); | |
1164 | return ret; | |
1165 | ||
1166 | cleanup_postroutingops: | |
1167 | nf_unregister_hook(&ip_vs_post_routing_ops); | |
1168 | cleanup_outops: | |
1169 | nf_unregister_hook(&ip_vs_out_ops); | |
1170 | cleanup_inops: | |
1171 | nf_unregister_hook(&ip_vs_in_ops); | |
1172 | cleanup_conn: | |
1173 | ip_vs_conn_cleanup(); | |
1174 | cleanup_app: | |
1175 | ip_vs_app_cleanup(); | |
1176 | cleanup_protocol: | |
1177 | ip_vs_protocol_cleanup(); | |
1178 | ip_vs_control_cleanup(); | |
1179 | cleanup_nothing: | |
1180 | return ret; | |
1181 | } | |
1182 | ||
1183 | static void __exit ip_vs_cleanup(void) | |
1184 | { | |
1185 | nf_unregister_hook(&ip_vs_forward_icmp_ops); | |
1186 | nf_unregister_hook(&ip_vs_post_routing_ops); | |
1187 | nf_unregister_hook(&ip_vs_out_ops); | |
1188 | nf_unregister_hook(&ip_vs_in_ops); | |
1189 | ip_vs_conn_cleanup(); | |
1190 | ip_vs_app_cleanup(); | |
1191 | ip_vs_protocol_cleanup(); | |
1192 | ip_vs_control_cleanup(); | |
1193 | IP_VS_INFO("ipvs unloaded.\n"); | |
1194 | } | |
1195 | ||
1196 | module_init(ip_vs_init); | |
1197 | module_exit(ip_vs_cleanup); | |
1198 | MODULE_LICENSE("GPL"); |