1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS An implementation of the IP virtual server support for the
4 * LINUX operating system. IPVS is now implemented as a module
5 * over the Netfilter framework. IPVS can be used to build a
6 * high-performance and highly available server based on a
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
13 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
14 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
18 * Paul `Rusty' Russell properly handle non-linear skbs
19 * Harald Welte don't use nfcache
22 #define KMSG_COMPONENT "IPVS"
23 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/tcp.h>
29 #include <linux/sctp.h>
30 #include <linux/icmp.h>
31 #include <linux/slab.h>
36 #include <net/icmp.h> /* for icmp_send */
37 #include <net/route.h>
38 #include <net/ip6_checksum.h>
39 #include <net/netns/generic.h> /* net_generic() */
41 #include <linux/netfilter.h>
42 #include <linux/netfilter_ipv4.h>
44 #ifdef CONFIG_IP_VS_IPV6
46 #include <linux/netfilter_ipv6.h>
47 #include <net/ip6_route.h>
50 #include <net/ip_vs.h>
51 #include <linux/indirect_call_wrapper.h>
54 EXPORT_SYMBOL(register_ip_vs_scheduler
);
55 EXPORT_SYMBOL(unregister_ip_vs_scheduler
);
56 EXPORT_SYMBOL(ip_vs_proto_name
);
57 EXPORT_SYMBOL(ip_vs_conn_new
);
58 EXPORT_SYMBOL(ip_vs_conn_in_get
);
59 EXPORT_SYMBOL(ip_vs_conn_out_get
);
60 #ifdef CONFIG_IP_VS_PROTO_TCP
61 EXPORT_SYMBOL(ip_vs_tcp_conn_listen
);
63 EXPORT_SYMBOL(ip_vs_conn_put
);
64 #ifdef CONFIG_IP_VS_DEBUG
65 EXPORT_SYMBOL(ip_vs_get_debug_level
);
67 EXPORT_SYMBOL(ip_vs_new_conn_out
);
69 #ifdef CONFIG_IP_VS_PROTO_TCP
70 INDIRECT_CALLABLE_DECLARE(int
71 tcp_snat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
72 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
));
75 #ifdef CONFIG_IP_VS_PROTO_UDP
76 INDIRECT_CALLABLE_DECLARE(int
77 udp_snat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
78 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
));
81 #if defined(CONFIG_IP_VS_PROTO_TCP) && defined(CONFIG_IP_VS_PROTO_UDP)
82 #define SNAT_CALL(f, ...) \
83 INDIRECT_CALL_2(f, tcp_snat_handler, udp_snat_handler, __VA_ARGS__)
84 #elif defined(CONFIG_IP_VS_PROTO_TCP)
85 #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, tcp_snat_handler, __VA_ARGS__)
86 #elif defined(CONFIG_IP_VS_PROTO_UDP)
87 #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, udp_snat_handler, __VA_ARGS__)
89 #define SNAT_CALL(f, ...) f(__VA_ARGS__)
92 static unsigned int ip_vs_net_id __read_mostly
;
93 /* netns cnt used for uniqueness */
94 static atomic_t ipvs_netns_cnt
= ATOMIC_INIT(0);
96 /* ID used in ICMP lookups */
97 #define icmp_id(icmph) (((icmph)->un).echo.id)
98 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
100 const char *ip_vs_proto_name(unsigned int proto
)
115 #ifdef CONFIG_IP_VS_IPV6
120 sprintf(buf
, "IP_%u", proto
);
125 void ip_vs_init_hash_table(struct list_head
*table
, int rows
)
128 INIT_LIST_HEAD(&table
[rows
]);
132 ip_vs_in_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
134 struct ip_vs_dest
*dest
= cp
->dest
;
135 struct netns_ipvs
*ipvs
= cp
->ipvs
;
137 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
138 struct ip_vs_cpu_stats
*s
;
139 struct ip_vs_service
*svc
;
143 s
= this_cpu_ptr(dest
->stats
.cpustats
);
144 u64_stats_update_begin(&s
->syncp
);
146 s
->cnt
.inbytes
+= skb
->len
;
147 u64_stats_update_end(&s
->syncp
);
149 svc
= rcu_dereference(dest
->svc
);
150 s
= this_cpu_ptr(svc
->stats
.cpustats
);
151 u64_stats_update_begin(&s
->syncp
);
153 s
->cnt
.inbytes
+= skb
->len
;
154 u64_stats_update_end(&s
->syncp
);
156 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
157 u64_stats_update_begin(&s
->syncp
);
159 s
->cnt
.inbytes
+= skb
->len
;
160 u64_stats_update_end(&s
->syncp
);
168 ip_vs_out_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
170 struct ip_vs_dest
*dest
= cp
->dest
;
171 struct netns_ipvs
*ipvs
= cp
->ipvs
;
173 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
174 struct ip_vs_cpu_stats
*s
;
175 struct ip_vs_service
*svc
;
179 s
= this_cpu_ptr(dest
->stats
.cpustats
);
180 u64_stats_update_begin(&s
->syncp
);
182 s
->cnt
.outbytes
+= skb
->len
;
183 u64_stats_update_end(&s
->syncp
);
185 svc
= rcu_dereference(dest
->svc
);
186 s
= this_cpu_ptr(svc
->stats
.cpustats
);
187 u64_stats_update_begin(&s
->syncp
);
189 s
->cnt
.outbytes
+= skb
->len
;
190 u64_stats_update_end(&s
->syncp
);
192 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
193 u64_stats_update_begin(&s
->syncp
);
195 s
->cnt
.outbytes
+= skb
->len
;
196 u64_stats_update_end(&s
->syncp
);
204 ip_vs_conn_stats(struct ip_vs_conn
*cp
, struct ip_vs_service
*svc
)
206 struct netns_ipvs
*ipvs
= svc
->ipvs
;
207 struct ip_vs_cpu_stats
*s
;
211 s
= this_cpu_ptr(cp
->dest
->stats
.cpustats
);
212 u64_stats_update_begin(&s
->syncp
);
214 u64_stats_update_end(&s
->syncp
);
216 s
= this_cpu_ptr(svc
->stats
.cpustats
);
217 u64_stats_update_begin(&s
->syncp
);
219 u64_stats_update_end(&s
->syncp
);
221 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
222 u64_stats_update_begin(&s
->syncp
);
224 u64_stats_update_end(&s
->syncp
);
231 ip_vs_set_state(struct ip_vs_conn
*cp
, int direction
,
232 const struct sk_buff
*skb
,
233 struct ip_vs_proto_data
*pd
)
235 if (likely(pd
->pp
->state_transition
))
236 pd
->pp
->state_transition(cp
, direction
, skb
, pd
);
240 ip_vs_conn_fill_param_persist(const struct ip_vs_service
*svc
,
241 struct sk_buff
*skb
, int protocol
,
242 const union nf_inet_addr
*caddr
, __be16 cport
,
243 const union nf_inet_addr
*vaddr
, __be16 vport
,
244 struct ip_vs_conn_param
*p
)
246 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, protocol
, caddr
, cport
, vaddr
,
248 p
->pe
= rcu_dereference(svc
->pe
);
249 if (p
->pe
&& p
->pe
->fill_param
)
250 return p
->pe
->fill_param(p
, skb
);
256 * IPVS persistent scheduling function
257 * It creates a connection entry according to its template if exists,
258 * or selects a server and creates a connection entry plus a template.
259 * Locking: we are svc user (svc->refcnt), so we hold all dests too
260 * Protocols supported: TCP, UDP
262 static struct ip_vs_conn
*
263 ip_vs_sched_persist(struct ip_vs_service
*svc
,
264 struct sk_buff
*skb
, __be16 src_port
, __be16 dst_port
,
265 int *ignored
, struct ip_vs_iphdr
*iph
)
267 struct ip_vs_conn
*cp
= NULL
;
268 struct ip_vs_dest
*dest
;
269 struct ip_vs_conn
*ct
;
270 __be16 dport
= 0; /* destination port to forward */
272 struct ip_vs_conn_param param
;
273 const union nf_inet_addr fwmark
= { .ip
= htonl(svc
->fwmark
) };
274 union nf_inet_addr snet
; /* source network of the client,
276 const union nf_inet_addr
*src_addr
, *dst_addr
;
278 if (likely(!ip_vs_iph_inverse(iph
))) {
279 src_addr
= &iph
->saddr
;
280 dst_addr
= &iph
->daddr
;
282 src_addr
= &iph
->daddr
;
283 dst_addr
= &iph
->saddr
;
287 /* Mask saddr with the netmask to adjust template granularity */
288 #ifdef CONFIG_IP_VS_IPV6
289 if (svc
->af
== AF_INET6
)
290 ipv6_addr_prefix(&snet
.in6
, &src_addr
->in6
,
291 (__force __u32
) svc
->netmask
);
294 snet
.ip
= src_addr
->ip
& svc
->netmask
;
296 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
298 IP_VS_DBG_ADDR(svc
->af
, src_addr
), ntohs(src_port
),
299 IP_VS_DBG_ADDR(svc
->af
, dst_addr
), ntohs(dst_port
),
300 IP_VS_DBG_ADDR(svc
->af
, &snet
));
303 * As far as we know, FTP is a very complicated network protocol, and
304 * it uses control connection and data connections. For active FTP,
305 * FTP server initialize data connection to the client, its source port
306 * is often 20. For passive FTP, FTP server tells the clients the port
307 * that it passively listens to, and the client issues the data
308 * connection. In the tunneling or direct routing mode, the load
309 * balancer is on the client-to-server half of connection, the port
310 * number is unknown to the load balancer. So, a conn template like
311 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
312 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
313 * is created for other persistent services.
316 int protocol
= iph
->protocol
;
317 const union nf_inet_addr
*vaddr
= dst_addr
;
320 if (dst_port
== svc
->port
) {
322 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
324 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
326 if (svc
->port
!= FTPPORT
)
329 /* Note: persistent fwmark-based services and
330 * persistent port zero service are handled here.
332 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
333 * port zero template:
334 * <protocol,caddr,0,vaddr,0,daddr,0>
337 protocol
= IPPROTO_IP
;
341 /* return *ignored = -1 so NF_DROP can be used */
342 if (ip_vs_conn_fill_param_persist(svc
, skb
, protocol
, &snet
, 0,
343 vaddr
, vport
, ¶m
) < 0) {
349 /* Check if a template already exists */
350 ct
= ip_vs_ct_in_get(¶m
);
351 if (!ct
|| !ip_vs_check_template(ct
, NULL
)) {
352 struct ip_vs_scheduler
*sched
;
355 * No template found or the dest of the connection
356 * template is not available.
357 * return *ignored=0 i.e. ICMP and NF_DROP
359 sched
= rcu_dereference(svc
->scheduler
);
361 /* read svc->sched_data after svc->scheduler */
363 dest
= sched
->schedule(svc
, skb
, iph
);
368 IP_VS_DBG(1, "p-schedule: no dest found.\n");
369 kfree(param
.pe_data
);
374 if (dst_port
== svc
->port
&& svc
->port
!= FTPPORT
)
378 * This adds param.pe_data to the template,
379 * and thus param.pe_data will be destroyed
380 * when the template expires */
381 ct
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
,
382 IP_VS_CONN_F_TEMPLATE
, dest
, skb
->mark
);
384 kfree(param
.pe_data
);
389 ct
->timeout
= svc
->timeout
;
391 /* set destination with the found template */
393 kfree(param
.pe_data
);
397 if (dport
== svc
->port
&& dest
->port
)
400 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
401 && iph
->protocol
== IPPROTO_UDP
) ?
402 IP_VS_CONN_F_ONE_PACKET
: 0;
405 * Create a new connection according to the template
407 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
, src_addr
,
408 src_port
, dst_addr
, dst_port
, ¶m
);
410 cp
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
, flags
, dest
,
421 ip_vs_control_add(cp
, ct
);
424 ip_vs_conn_stats(cp
, svc
);
430 * IPVS main scheduling function
431 * It selects a server according to the virtual service, and
432 * creates a connection entry.
433 * Protocols supported: TCP, UDP
437 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
438 * svc/scheduler decides that this packet should be accepted with
439 * NF_ACCEPT because it must not be scheduled.
441 * 0 : scheduler can not find destination, so try bypass or
442 * return ICMP and then NF_DROP (ip_vs_leave).
444 * -1 : scheduler tried to schedule but fatal error occurred, eg.
445 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
446 * failure such as missing Call-ID, ENOMEM on skb_linearize
447 * or pe_data. In this case we should return NF_DROP without
448 * any attempts to send ICMP with ip_vs_leave.
451 ip_vs_schedule(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
452 struct ip_vs_proto_data
*pd
, int *ignored
,
453 struct ip_vs_iphdr
*iph
)
455 struct ip_vs_protocol
*pp
= pd
->pp
;
456 struct ip_vs_conn
*cp
= NULL
;
457 struct ip_vs_scheduler
*sched
;
458 struct ip_vs_dest
*dest
;
459 __be16 _ports
[2], *pptr
, cport
, vport
;
460 const void *caddr
, *vaddr
;
465 * IPv6 frags, only the first hit here.
467 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
);
471 if (likely(!ip_vs_iph_inverse(iph
))) {
484 * FTPDATA needs this check when using local real server.
485 * Never schedule Active FTPDATA connections from real server.
486 * For LVS-NAT they must be already created. For other methods
487 * with persistence the connection is created on SYN+ACK.
489 if (cport
== FTPDATA
) {
490 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, iph
->off
,
491 "Not scheduling FTPDATA");
496 * Do not schedule replies from local real server.
498 if ((!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
)) {
499 iph
->hdr_flags
^= IP_VS_HDR_INVERSE
;
500 cp
= INDIRECT_CALL_1(pp
->conn_in_get
,
501 ip_vs_conn_in_get_proto
, svc
->ipvs
,
503 iph
->hdr_flags
^= IP_VS_HDR_INVERSE
;
506 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, iph
->off
,
507 "Not scheduling reply for existing"
509 __ip_vs_conn_put(cp
);
517 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
518 return ip_vs_sched_persist(svc
, skb
, cport
, vport
, ignored
,
524 * Non-persistent service
526 if (!svc
->fwmark
&& vport
!= svc
->port
) {
528 pr_err("Schedule: port zero only supported "
529 "in persistent services, "
530 "check your ipvs configuration\n");
534 sched
= rcu_dereference(svc
->scheduler
);
536 /* read svc->sched_data after svc->scheduler */
538 dest
= sched
->schedule(svc
, skb
, iph
);
543 IP_VS_DBG(1, "Schedule: no dest found.\n");
547 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
548 && iph
->protocol
== IPPROTO_UDP
) ?
549 IP_VS_CONN_F_ONE_PACKET
: 0;
552 * Create a connection entry.
555 struct ip_vs_conn_param p
;
557 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
558 caddr
, cport
, vaddr
, vport
, &p
);
559 cp
= ip_vs_conn_new(&p
, dest
->af
, &dest
->addr
,
560 dest
->port
? dest
->port
: vport
,
561 flags
, dest
, skb
->mark
);
568 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
569 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
571 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
572 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
573 IP_VS_DBG_ADDR(cp
->daf
, &cp
->daddr
), ntohs(cp
->dport
),
574 cp
->flags
, refcount_read(&cp
->refcnt
));
576 ip_vs_conn_stats(cp
, svc
);
580 static inline int ip_vs_addr_is_unicast(struct net
*net
, int af
,
581 union nf_inet_addr
*addr
)
583 #ifdef CONFIG_IP_VS_IPV6
585 return ipv6_addr_type(&addr
->in6
) & IPV6_ADDR_UNICAST
;
587 return (inet_addr_type(net
, addr
->ip
) == RTN_UNICAST
);
591 * Pass or drop the packet.
592 * Called by ip_vs_in, when the virtual service is available but
593 * no destination is available for a new connection.
595 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
596 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
598 __be16 _ports
[2], *pptr
, dport
;
599 struct netns_ipvs
*ipvs
= svc
->ipvs
;
600 struct net
*net
= ipvs
->net
;
602 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
);
605 dport
= likely(!ip_vs_iph_inverse(iph
)) ? pptr
[1] : pptr
[0];
607 /* if it is fwmark-based service, the cache_bypass sysctl is up
608 and the destination is a non-local unicast, then create
609 a cache_bypass connection entry */
610 if (sysctl_cache_bypass(ipvs
) && svc
->fwmark
&&
611 !(iph
->hdr_flags
& (IP_VS_HDR_INVERSE
| IP_VS_HDR_ICMP
)) &&
612 ip_vs_addr_is_unicast(net
, svc
->af
, &iph
->daddr
)) {
614 struct ip_vs_conn
*cp
;
615 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
616 iph
->protocol
== IPPROTO_UDP
) ?
617 IP_VS_CONN_F_ONE_PACKET
: 0;
618 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
620 /* create a new connection entry */
621 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
623 struct ip_vs_conn_param p
;
624 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
625 &iph
->saddr
, pptr
[0],
626 &iph
->daddr
, pptr
[1], &p
);
627 cp
= ip_vs_conn_new(&p
, svc
->af
, &daddr
, 0,
628 IP_VS_CONN_F_BYPASS
| flags
,
635 ip_vs_in_stats(cp
, skb
);
638 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
640 /* transmit the first SYN packet */
641 ret
= cp
->packet_xmit(skb
, cp
, pd
->pp
, iph
);
642 /* do not touch skb anymore */
644 if ((cp
->flags
& IP_VS_CONN_F_ONE_PACKET
) && cp
->control
)
645 atomic_inc(&cp
->control
->in_pkts
);
647 atomic_inc(&cp
->in_pkts
);
653 * When the virtual ftp service is presented, packets destined
654 * for other services on the VIP may get here (except services
655 * listed in the ipvs table), pass the packets, because it is
656 * not ipvs job to decide to drop the packets.
658 if (svc
->port
== FTPPORT
&& dport
!= FTPPORT
)
661 if (unlikely(ip_vs_iph_icmp(iph
)))
665 * Notify the client that the destination is unreachable, and
666 * release the socket buffer.
667 * Since it is in IP layer, the TCP socket is not actually
668 * created, the TCP RST packet cannot be sent, instead that
669 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
671 #ifdef CONFIG_IP_VS_IPV6
672 if (svc
->af
== AF_INET6
) {
674 skb
->dev
= net
->loopback_dev
;
675 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
678 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
685 static int sysctl_snat_reroute(struct netns_ipvs
*ipvs
)
687 return ipvs
->sysctl_snat_reroute
;
690 static int sysctl_nat_icmp_send(struct netns_ipvs
*ipvs
)
692 return ipvs
->sysctl_nat_icmp_send
;
695 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
697 return ipvs
->sysctl_expire_nodest_conn
;
702 static int sysctl_snat_reroute(struct netns_ipvs
*ipvs
) { return 0; }
703 static int sysctl_nat_icmp_send(struct netns_ipvs
*ipvs
) { return 0; }
704 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
708 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
710 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
713 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
715 if (NF_INET_LOCAL_IN
== hooknum
)
716 return IP_DEFRAG_VS_IN
;
717 if (NF_INET_FORWARD
== hooknum
)
718 return IP_DEFRAG_VS_FWD
;
719 return IP_DEFRAG_VS_OUT
;
722 static inline int ip_vs_gather_frags(struct netns_ipvs
*ipvs
,
723 struct sk_buff
*skb
, u_int32_t user
)
728 err
= ip_defrag(ipvs
->net
, skb
, user
);
731 ip_send_check(ip_hdr(skb
));
736 static int ip_vs_route_me_harder(struct netns_ipvs
*ipvs
, int af
,
737 struct sk_buff
*skb
, unsigned int hooknum
)
739 if (!sysctl_snat_reroute(ipvs
))
741 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
742 if (NF_INET_LOCAL_IN
== hooknum
)
744 #ifdef CONFIG_IP_VS_IPV6
745 if (af
== AF_INET6
) {
746 struct dst_entry
*dst
= skb_dst(skb
);
748 if (dst
->dev
&& !(dst
->dev
->flags
& IFF_LOOPBACK
) &&
749 ip6_route_me_harder(ipvs
->net
, skb
) != 0)
753 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
754 ip_route_me_harder(ipvs
->net
, skb
, RTN_LOCAL
) != 0)
761 * Packet has been made sufficiently writable in caller
762 * - inout: 1=in->out, 0=out->in
764 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
765 struct ip_vs_conn
*cp
, int inout
)
767 struct iphdr
*iph
= ip_hdr(skb
);
768 unsigned int icmp_offset
= iph
->ihl
*4;
769 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
771 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
774 iph
->saddr
= cp
->vaddr
.ip
;
776 ciph
->daddr
= cp
->vaddr
.ip
;
779 iph
->daddr
= cp
->daddr
.ip
;
781 ciph
->saddr
= cp
->daddr
.ip
;
785 /* the TCP/UDP/SCTP port */
786 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
787 IPPROTO_SCTP
== ciph
->protocol
) {
788 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
791 ports
[1] = cp
->vport
;
793 ports
[0] = cp
->dport
;
796 /* And finally the ICMP checksum */
798 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
799 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
802 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
803 "Forwarding altered outgoing ICMP");
805 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
806 "Forwarding altered incoming ICMP");
809 #ifdef CONFIG_IP_VS_IPV6
810 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
811 struct ip_vs_conn
*cp
, int inout
)
813 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
814 unsigned int icmp_offset
= 0;
815 unsigned int offs
= 0; /* header offset*/
817 struct icmp6hdr
*icmph
;
818 struct ipv6hdr
*ciph
;
819 unsigned short fragoffs
;
821 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
822 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
823 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
824 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
826 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
829 iph
->saddr
= cp
->vaddr
.in6
;
830 ciph
->daddr
= cp
->vaddr
.in6
;
832 iph
->daddr
= cp
->daddr
.in6
;
833 ciph
->saddr
= cp
->daddr
.in6
;
836 /* the TCP/UDP/SCTP port */
837 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
838 IPPROTO_SCTP
== protocol
)) {
839 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
841 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
842 ntohs(inout
? ports
[1] : ports
[0]),
843 ntohs(inout
? cp
->vport
: cp
->dport
));
845 ports
[1] = cp
->vport
;
847 ports
[0] = cp
->dport
;
850 /* And finally the ICMP checksum */
851 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
852 skb
->len
- icmp_offset
,
854 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
855 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
856 skb
->ip_summed
= CHECKSUM_PARTIAL
;
859 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
860 (void *)ciph
- (void *)iph
,
861 "Forwarding altered outgoing ICMPv6");
863 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
864 (void *)ciph
- (void *)iph
,
865 "Forwarding altered incoming ICMPv6");
869 /* Handle relevant response ICMP messages - forward to the right
872 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
873 union nf_inet_addr
*snet
,
874 __u8 protocol
, struct ip_vs_conn
*cp
,
875 struct ip_vs_protocol
*pp
,
876 unsigned int offset
, unsigned int ihl
,
877 unsigned int hooknum
)
879 unsigned int verdict
= NF_DROP
;
881 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
884 /* Ensure the checksum is correct */
885 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
886 /* Failed checksum! */
887 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
888 IP_VS_DBG_ADDR(af
, snet
));
892 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
893 IPPROTO_SCTP
== protocol
)
894 offset
+= 2 * sizeof(__u16
);
895 if (!skb_make_writable(skb
, offset
))
898 #ifdef CONFIG_IP_VS_IPV6
900 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
903 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
905 if (ip_vs_route_me_harder(cp
->ipvs
, af
, skb
, hooknum
))
908 /* do the statistics and put it back */
909 ip_vs_out_stats(cp
, skb
);
911 skb
->ipvs_property
= 1;
912 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
915 ip_vs_update_conntrack(skb
, cp
, 0);
921 __ip_vs_conn_put(cp
);
927 * Handle ICMP messages in the inside-to-outside direction (outgoing).
928 * Find any that might be relevant, check against existing connections.
929 * Currently handles error types - unreachable, quench, ttl exceeded.
931 static int ip_vs_out_icmp(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
932 int *related
, unsigned int hooknum
)
935 struct icmphdr _icmph
, *ic
;
936 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
937 struct ip_vs_iphdr ciph
;
938 struct ip_vs_conn
*cp
;
939 struct ip_vs_protocol
*pp
;
940 unsigned int offset
, ihl
;
941 union nf_inet_addr snet
;
945 /* reassemble IP fragments */
946 if (ip_is_fragment(ip_hdr(skb
))) {
947 if (ip_vs_gather_frags(ipvs
, skb
, ip_vs_defrag_user(hooknum
)))
952 offset
= ihl
= iph
->ihl
* 4;
953 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
957 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
958 ic
->type
, ntohs(icmp_id(ic
)),
959 &iph
->saddr
, &iph
->daddr
);
962 * Work through seeing if this is for us.
963 * These checks are supposed to be in an order that means easy
964 * things are checked first to speed up processing.... however
965 * this means that some packets will manage to get a long way
966 * down this stack and then be rejected, but that's life.
968 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
969 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
970 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
975 /* Now find the contained IP header */
976 offset
+= sizeof(_icmph
);
977 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
979 return NF_ACCEPT
; /* The packet looks wrong, ignore */
981 pp
= ip_vs_proto_get(cih
->protocol
);
985 /* Is the embedded protocol header present? */
986 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
990 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
991 "Checking outgoing ICMP for");
993 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, true, &ciph
);
995 /* The embedded headers contain source and dest in reverse order */
996 cp
= INDIRECT_CALL_1(pp
->conn_out_get
, ip_vs_conn_out_get_proto
,
997 ipvs
, AF_INET
, skb
, &ciph
);
1001 snet
.ip
= iph
->saddr
;
1002 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
1003 pp
, ciph
.len
, ihl
, hooknum
);
1006 #ifdef CONFIG_IP_VS_IPV6
1007 static int ip_vs_out_icmp_v6(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
1008 int *related
, unsigned int hooknum
,
1009 struct ip_vs_iphdr
*ipvsh
)
1011 struct icmp6hdr _icmph
, *ic
;
1012 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1013 struct ip_vs_conn
*cp
;
1014 struct ip_vs_protocol
*pp
;
1015 union nf_inet_addr snet
;
1016 unsigned int offset
;
1019 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
);
1024 * Work through seeing if this is for us.
1025 * These checks are supposed to be in an order that means easy
1026 * things are checked first to speed up processing.... however
1027 * this means that some packets will manage to get a long way
1028 * down this stack and then be rejected, but that's life.
1030 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1034 /* Fragment header that is before ICMP header tells us that:
1035 * it's not an error message since they can't be fragmented.
1037 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
1040 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1041 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1042 &ipvsh
->saddr
, &ipvsh
->daddr
);
1044 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, ipvsh
->len
+ sizeof(_icmph
),
1046 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1048 pp
= ip_vs_proto_get(ciph
.protocol
);
1052 /* The embedded headers contain source and dest in reverse order */
1053 cp
= INDIRECT_CALL_1(pp
->conn_out_get
, ip_vs_conn_out_get_proto
,
1054 ipvs
, AF_INET6
, skb
, &ciph
);
1058 snet
.in6
= ciph
.saddr
.in6
;
1060 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
1061 pp
, offset
, sizeof(struct ipv6hdr
),
1067 * Check if sctp chunc is ABORT chunk
1069 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
1071 struct sctp_chunkhdr
*sch
, schunk
;
1072 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(struct sctphdr
),
1073 sizeof(schunk
), &schunk
);
1076 if (sch
->type
== SCTP_CID_ABORT
)
1081 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
1083 struct tcphdr _tcph
, *th
;
1085 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1091 static inline bool is_new_conn(const struct sk_buff
*skb
,
1092 struct ip_vs_iphdr
*iph
)
1094 switch (iph
->protocol
) {
1096 struct tcphdr _tcph
, *th
;
1098 th
= skb_header_pointer(skb
, iph
->len
, sizeof(_tcph
), &_tcph
);
1103 case IPPROTO_SCTP
: {
1104 struct sctp_chunkhdr
*sch
, schunk
;
1106 sch
= skb_header_pointer(skb
, iph
->len
+ sizeof(struct sctphdr
),
1107 sizeof(schunk
), &schunk
);
1110 return sch
->type
== SCTP_CID_INIT
;
1117 static inline bool is_new_conn_expected(const struct ip_vs_conn
*cp
,
1118 int conn_reuse_mode
)
1120 /* Controlled (FTP DATA or persistence)? */
1124 switch (cp
->protocol
) {
1126 return (cp
->state
== IP_VS_TCP_S_TIME_WAIT
) ||
1127 (cp
->state
== IP_VS_TCP_S_CLOSE
) ||
1128 ((conn_reuse_mode
& 2) &&
1129 (cp
->state
== IP_VS_TCP_S_FIN_WAIT
) &&
1130 (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
));
1132 return cp
->state
== IP_VS_SCTP_S_CLOSED
;
1138 /* Generic function to create new connections for outgoing RS packets
1140 * Pre-requisites for successful connection creation:
1141 * 1) Virtual Service is NOT fwmark based:
1142 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1143 * 2) Real Server and Virtual Service were NOT configured without port:
1144 * This is to allow match of different VS to the same RS ip-addr
1146 struct ip_vs_conn
*ip_vs_new_conn_out(struct ip_vs_service
*svc
,
1147 struct ip_vs_dest
*dest
,
1148 struct sk_buff
*skb
,
1149 const struct ip_vs_iphdr
*iph
,
1153 struct ip_vs_conn_param param
;
1154 struct ip_vs_conn
*ct
= NULL
, *cp
= NULL
;
1155 const union nf_inet_addr
*vaddr
, *daddr
, *caddr
;
1156 union nf_inet_addr snet
;
1163 daddr
= &iph
->saddr
;
1164 caddr
= &iph
->daddr
;
1166 /* check pre-requisites are satisfied */
1169 if (!vport
|| !dport
)
1172 /* for persistent service first create connection template */
1173 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
) {
1174 /* apply netmask the same way ingress-side does */
1175 #ifdef CONFIG_IP_VS_IPV6
1176 if (svc
->af
== AF_INET6
)
1177 ipv6_addr_prefix(&snet
.in6
, &caddr
->in6
,
1178 (__force __u32
)svc
->netmask
);
1181 snet
.ip
= caddr
->ip
& svc
->netmask
;
1182 /* fill params and create template if not existent */
1183 if (ip_vs_conn_fill_param_persist(svc
, skb
, iph
->protocol
,
1187 ct
= ip_vs_ct_in_get(¶m
);
1188 /* check if template exists and points to the same dest */
1189 if (!ct
|| !ip_vs_check_template(ct
, dest
)) {
1190 ct
= ip_vs_conn_new(¶m
, dest
->af
, daddr
, dport
,
1191 IP_VS_CONN_F_TEMPLATE
, dest
, 0);
1193 kfree(param
.pe_data
);
1196 ct
->timeout
= svc
->timeout
;
1198 kfree(param
.pe_data
);
1202 /* connection flags */
1203 flags
= ((svc
->flags
& IP_VS_SVC_F_ONEPACKET
) &&
1204 iph
->protocol
== IPPROTO_UDP
) ? IP_VS_CONN_F_ONE_PACKET
: 0;
1205 /* create connection */
1206 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
1207 caddr
, cport
, vaddr
, vport
, ¶m
);
1208 cp
= ip_vs_conn_new(¶m
, dest
->af
, daddr
, dport
, flags
, dest
, 0);
1215 ip_vs_control_add(cp
, ct
);
1218 ip_vs_conn_stats(cp
, svc
);
1220 /* return connection (will be used to handle outgoing packet) */
1221 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1222 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1224 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
1225 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
1226 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
1227 cp
->flags
, refcount_read(&cp
->refcnt
));
1232 /* Handle outgoing packets which are considered requests initiated by
1233 * real servers, so that subsequent responses from external client can be
1234 * routed to the right real server.
1235 * Used also for outgoing responses in OPS mode.
1237 * Connection management is handled by persistent-engine specific callback.
1239 static struct ip_vs_conn
*__ip_vs_rs_conn_out(unsigned int hooknum
,
1240 struct netns_ipvs
*ipvs
,
1241 int af
, struct sk_buff
*skb
,
1242 const struct ip_vs_iphdr
*iph
)
1244 struct ip_vs_dest
*dest
;
1245 struct ip_vs_conn
*cp
= NULL
;
1246 __be16 _ports
[2], *pptr
;
1248 if (hooknum
== NF_INET_LOCAL_IN
)
1251 pptr
= frag_safe_skb_hp(skb
, iph
->len
,
1252 sizeof(_ports
), _ports
);
1256 dest
= ip_vs_find_real_service(ipvs
, af
, iph
->protocol
,
1257 &iph
->saddr
, pptr
[0]);
1259 struct ip_vs_service
*svc
;
1260 struct ip_vs_pe
*pe
;
1262 svc
= rcu_dereference(dest
->svc
);
1264 pe
= rcu_dereference(svc
->pe
);
1265 if (pe
&& pe
->conn_out
)
1266 cp
= pe
->conn_out(svc
, dest
, skb
, iph
,
1274 /* Handle response packets: rewrite addresses and send away...
1277 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1278 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
,
1279 unsigned int hooknum
)
1281 struct ip_vs_protocol
*pp
= pd
->pp
;
1283 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
->off
, "Outgoing packet");
1285 if (!skb_make_writable(skb
, iph
->len
))
1288 /* mangle the packet */
1289 if (pp
->snat_handler
&&
1290 !SNAT_CALL(pp
->snat_handler
, skb
, pp
, cp
, iph
))
1293 #ifdef CONFIG_IP_VS_IPV6
1295 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1299 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1300 ip_send_check(ip_hdr(skb
));
1304 * nf_iterate does not expect change in the skb->dst->dev.
1305 * It looks like it is not fatal to enable this code for hooks
1306 * where our handlers are at the end of the chain list and
1307 * when all next handlers use skb->dst->dev and not outdev.
1308 * It will definitely route properly the inout NAT traffic
1309 * when multiple paths are used.
1312 /* For policy routing, packets originating from this
1313 * machine itself may be routed differently to packets
1314 * passing through. We want this packet to be routed as
1315 * if it came from this machine itself. So re-compute
1316 * the routing information.
1318 if (ip_vs_route_me_harder(cp
->ipvs
, af
, skb
, hooknum
))
1321 IP_VS_DBG_PKT(10, af
, pp
, skb
, iph
->off
, "After SNAT");
1323 ip_vs_out_stats(cp
, skb
);
1324 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1325 skb
->ipvs_property
= 1;
1326 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1329 ip_vs_update_conntrack(skb
, cp
, 0);
1343 * Check if outgoing packet belongs to the established ip_vs_conn.
1346 ip_vs_out(struct netns_ipvs
*ipvs
, unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1348 struct ip_vs_iphdr iph
;
1349 struct ip_vs_protocol
*pp
;
1350 struct ip_vs_proto_data
*pd
;
1351 struct ip_vs_conn
*cp
;
1356 /* Already marked as IPVS request or reply? */
1357 if (skb
->ipvs_property
)
1360 sk
= skb_to_full_sk(skb
);
1361 /* Bad... Do not break raw sockets */
1362 if (unlikely(sk
&& hooknum
== NF_INET_LOCAL_OUT
&&
1365 if (sk
->sk_family
== PF_INET
&& inet_sk(sk
)->nodefrag
)
1369 if (unlikely(!skb_dst(skb
)))
1375 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1376 #ifdef CONFIG_IP_VS_IPV6
1377 if (af
== AF_INET6
) {
1378 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1380 int verdict
= ip_vs_out_icmp_v6(ipvs
, skb
, &related
,
1388 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1390 int verdict
= ip_vs_out_icmp(ipvs
, skb
, &related
, hooknum
);
1396 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1401 /* reassemble IP fragments */
1402 #ifdef CONFIG_IP_VS_IPV6
1405 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1406 if (ip_vs_gather_frags(ipvs
, skb
,
1407 ip_vs_defrag_user(hooknum
)))
1410 ip_vs_fill_iph_skb(AF_INET
, skb
, false, &iph
);
1414 * Check if the packet belongs to an existing entry
1416 cp
= INDIRECT_CALL_1(pp
->conn_out_get
, ip_vs_conn_out_get_proto
,
1417 ipvs
, af
, skb
, &iph
);
1420 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
1422 return handle_response(af
, skb
, pd
, cp
, &iph
, hooknum
);
1425 /* Check for real-server-started requests */
1426 if (atomic_read(&ipvs
->conn_out_counter
)) {
1427 /* Currently only for UDP:
1428 * connection oriented protocols typically use
1429 * ephemeral ports for outgoing connections, so
1430 * related incoming responses would not match any VS
1432 if (pp
->protocol
== IPPROTO_UDP
) {
1433 cp
= __ip_vs_rs_conn_out(hooknum
, ipvs
, af
, skb
, &iph
);
1435 return handle_response(af
, skb
, pd
, cp
, &iph
,
1440 if (sysctl_nat_icmp_send(ipvs
) &&
1441 (pp
->protocol
== IPPROTO_TCP
||
1442 pp
->protocol
== IPPROTO_UDP
||
1443 pp
->protocol
== IPPROTO_SCTP
)) {
1444 __be16 _ports
[2], *pptr
;
1446 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1447 sizeof(_ports
), _ports
);
1449 return NF_ACCEPT
; /* Not for me */
1450 if (ip_vs_has_real_service(ipvs
, af
, iph
.protocol
, &iph
.saddr
,
1453 * Notify the real server: there is no
1454 * existing entry if it is not RST
1455 * packet or not TCP packet.
1457 if ((iph
.protocol
!= IPPROTO_TCP
&&
1458 iph
.protocol
!= IPPROTO_SCTP
)
1459 || ((iph
.protocol
== IPPROTO_TCP
1460 && !is_tcp_reset(skb
, iph
.len
))
1461 || (iph
.protocol
== IPPROTO_SCTP
1462 && !is_sctp_abort(skb
,
1464 #ifdef CONFIG_IP_VS_IPV6
1465 if (af
== AF_INET6
) {
1467 skb
->dev
= ipvs
->net
->loopback_dev
;
1469 ICMPV6_DEST_UNREACH
,
1470 ICMPV6_PORT_UNREACH
,
1476 ICMP_PORT_UNREACH
, 0);
1483 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
.off
,
1484 "ip_vs_out: packet continues traversal as normal");
1488 __ip_vs_conn_put(cp
);
1493 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1494 * used only for VS/NAT.
1495 * Check if packet is reply for established ip_vs_conn.
1498 ip_vs_reply4(void *priv
, struct sk_buff
*skb
,
1499 const struct nf_hook_state
*state
)
1501 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1505 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1506 * Check if packet is reply for established ip_vs_conn.
1509 ip_vs_local_reply4(void *priv
, struct sk_buff
*skb
,
1510 const struct nf_hook_state
*state
)
1512 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1515 #ifdef CONFIG_IP_VS_IPV6
1518 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1519 * used only for VS/NAT.
1520 * Check if packet is reply for established ip_vs_conn.
1523 ip_vs_reply6(void *priv
, struct sk_buff
*skb
,
1524 const struct nf_hook_state
*state
)
1526 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1530 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1531 * Check if packet is reply for established ip_vs_conn.
1534 ip_vs_local_reply6(void *priv
, struct sk_buff
*skb
,
1535 const struct nf_hook_state
*state
)
1537 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1543 ip_vs_try_to_schedule(struct netns_ipvs
*ipvs
, int af
, struct sk_buff
*skb
,
1544 struct ip_vs_proto_data
*pd
,
1545 int *verdict
, struct ip_vs_conn
**cpp
,
1546 struct ip_vs_iphdr
*iph
)
1548 struct ip_vs_protocol
*pp
= pd
->pp
;
1550 if (!iph
->fragoffs
) {
1551 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1552 * replayed fragment zero will already have created the cp
1555 /* Schedule and create new connection entry into cpp */
1556 if (!pp
->conn_schedule(ipvs
, af
, skb
, pd
, verdict
, cpp
, iph
))
1560 if (unlikely(!*cpp
)) {
1561 /* sorry, all this trouble for a no-hit :) */
1562 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
->off
,
1563 "ip_vs_in: packet continues traversal as normal");
1565 /* Fragment couldn't be mapped to a conn entry */
1567 IP_VS_DBG_PKT(7, af
, pp
, skb
, iph
->off
,
1568 "unhandled fragment");
1570 *verdict
= NF_ACCEPT
;
1578 * Handle ICMP messages in the outside-to-inside direction (incoming).
1579 * Find any that might be relevant, check against existing connections,
1580 * forward to the right destination host if relevant.
1581 * Currently handles error types - unreachable, quench, ttl exceeded.
1584 ip_vs_in_icmp(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
, int *related
,
1585 unsigned int hooknum
)
1588 struct icmphdr _icmph
, *ic
;
1589 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1590 struct ip_vs_iphdr ciph
;
1591 struct ip_vs_conn
*cp
;
1592 struct ip_vs_protocol
*pp
;
1593 struct ip_vs_proto_data
*pd
;
1594 unsigned int offset
, offset2
, ihl
, verdict
;
1595 bool ipip
, new_cp
= false;
1599 /* reassemble IP fragments */
1600 if (ip_is_fragment(ip_hdr(skb
))) {
1601 if (ip_vs_gather_frags(ipvs
, skb
, ip_vs_defrag_user(hooknum
)))
1606 offset
= ihl
= iph
->ihl
* 4;
1607 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1611 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1612 ic
->type
, ntohs(icmp_id(ic
)),
1613 &iph
->saddr
, &iph
->daddr
);
1616 * Work through seeing if this is for us.
1617 * These checks are supposed to be in an order that means easy
1618 * things are checked first to speed up processing.... however
1619 * this means that some packets will manage to get a long way
1620 * down this stack and then be rejected, but that's life.
1622 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1623 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1624 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1629 /* Now find the contained IP header */
1630 offset
+= sizeof(_icmph
);
1631 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1633 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1635 /* Special case for errors for IPIP packets */
1637 if (cih
->protocol
== IPPROTO_IPIP
) {
1638 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1640 /* Error for our IPIP must arrive at LOCAL_IN */
1641 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1643 offset
+= cih
->ihl
* 4;
1644 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1646 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1650 pd
= ip_vs_proto_data_get(ipvs
, cih
->protocol
);
1655 /* Is the embedded protocol header present? */
1656 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1660 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1661 "Checking incoming ICMP for");
1664 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, !ipip
, &ciph
);
1667 /* The embedded headers contain source and dest in reverse order.
1668 * For IPIP this is error for request, not for reply.
1670 cp
= INDIRECT_CALL_1(pp
->conn_in_get
, ip_vs_conn_in_get_proto
,
1671 ipvs
, AF_INET
, skb
, &ciph
);
1676 if (ipip
|| !sysctl_schedule_icmp(ipvs
))
1679 if (!ip_vs_try_to_schedule(ipvs
, AF_INET
, skb
, pd
, &v
, &cp
, &ciph
))
1686 /* Ensure the checksum is correct */
1687 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1688 /* Failed checksum! */
1689 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1695 __be32 info
= ic
->un
.gateway
;
1696 __u8 type
= ic
->type
;
1697 __u8 code
= ic
->code
;
1699 /* Update the MTU */
1700 if (ic
->type
== ICMP_DEST_UNREACH
&&
1701 ic
->code
== ICMP_FRAG_NEEDED
) {
1702 struct ip_vs_dest
*dest
= cp
->dest
;
1703 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1704 __be16 frag_off
= cih
->frag_off
;
1706 /* Strip outer IP and ICMP, go to IPIP header */
1707 if (pskb_pull(skb
, ihl
+ sizeof(_icmph
)) == NULL
)
1709 offset2
-= ihl
+ sizeof(_icmph
);
1710 skb_reset_network_header(skb
);
1711 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1712 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1713 ipv4_update_pmtu(skb
, ipvs
->net
, mtu
, 0, 0);
1714 /* Client uses PMTUD? */
1715 if (!(frag_off
& htons(IP_DF
)))
1717 /* Prefer the resulting PMTU */
1719 struct ip_vs_dest_dst
*dest_dst
;
1721 dest_dst
= rcu_dereference(dest
->dest_dst
);
1723 mtu
= dst_mtu(dest_dst
->dst_cache
);
1725 if (mtu
> 68 + sizeof(struct iphdr
))
1726 mtu
-= sizeof(struct iphdr
);
1729 /* Strip outer IP, ICMP and IPIP, go to IP header of
1732 if (pskb_pull(skb
, offset2
) == NULL
)
1734 skb_reset_network_header(skb
);
1735 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1736 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1737 type
, code
, ntohl(info
));
1738 icmp_send(skb
, type
, code
, info
);
1739 /* ICMP can be shorter but anyways, account it */
1740 ip_vs_out_stats(cp
, skb
);
1744 verdict
= NF_STOLEN
;
1748 /* do the statistics and put it back */
1749 ip_vs_in_stats(cp
, skb
);
1750 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
||
1751 IPPROTO_SCTP
== cih
->protocol
)
1752 offset
+= 2 * sizeof(__u16
);
1753 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1756 if (likely(!new_cp
))
1757 __ip_vs_conn_put(cp
);
1764 #ifdef CONFIG_IP_VS_IPV6
1765 static int ip_vs_in_icmp_v6(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
1766 int *related
, unsigned int hooknum
,
1767 struct ip_vs_iphdr
*iph
)
1769 struct icmp6hdr _icmph
, *ic
;
1770 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1771 struct ip_vs_conn
*cp
;
1772 struct ip_vs_protocol
*pp
;
1773 struct ip_vs_proto_data
*pd
;
1774 unsigned int offset
, verdict
;
1775 bool new_cp
= false;
1779 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
);
1784 * Work through seeing if this is for us.
1785 * These checks are supposed to be in an order that means easy
1786 * things are checked first to speed up processing.... however
1787 * this means that some packets will manage to get a long way
1788 * down this stack and then be rejected, but that's life.
1790 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1794 /* Fragment header that is before ICMP header tells us that:
1795 * it's not an error message since they can't be fragmented.
1797 if (iph
->flags
& IP6_FH_F_FRAG
)
1800 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1801 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1802 &iph
->saddr
, &iph
->daddr
);
1804 offset
= iph
->len
+ sizeof(_icmph
);
1805 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, offset
, true, &ciph
))
1808 pd
= ip_vs_proto_data_get(ipvs
, ciph
.protocol
);
1813 /* Cannot handle fragmented embedded protocol */
1817 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offset
,
1818 "Checking incoming ICMPv6 for");
1820 /* The embedded headers contain source and dest in reverse order
1821 * if not from localhost
1823 cp
= INDIRECT_CALL_1(pp
->conn_in_get
, ip_vs_conn_in_get_proto
,
1824 ipvs
, AF_INET6
, skb
, &ciph
);
1829 if (!sysctl_schedule_icmp(ipvs
))
1832 if (!ip_vs_try_to_schedule(ipvs
, AF_INET6
, skb
, pd
, &v
, &cp
, &ciph
))
1838 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1839 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1840 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1841 verdict
= NF_ACCEPT
;
1845 /* do the statistics and put it back */
1846 ip_vs_in_stats(cp
, skb
);
1848 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1850 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1851 IPPROTO_SCTP
== ciph
.protocol
)
1852 offset
+= 2 * sizeof(__u16
); /* Also mangle ports */
1854 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1857 if (likely(!new_cp
))
1858 __ip_vs_conn_put(cp
);
1868 * Check if it's for virtual services, look it up,
1869 * and send it on its way...
1872 ip_vs_in(struct netns_ipvs
*ipvs
, unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1874 struct ip_vs_iphdr iph
;
1875 struct ip_vs_protocol
*pp
;
1876 struct ip_vs_proto_data
*pd
;
1877 struct ip_vs_conn
*cp
;
1879 int conn_reuse_mode
;
1882 /* Already marked as IPVS request or reply? */
1883 if (skb
->ipvs_property
)
1888 * - remote client: only PACKET_HOST
1889 * - route: used for struct net when skb->dev is unset
1891 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1892 hooknum
!= NF_INET_LOCAL_OUT
) ||
1894 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1895 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1896 " ignored in hook %u\n",
1897 skb
->pkt_type
, iph
.protocol
,
1898 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1901 /* ipvs enabled in this netns ? */
1902 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1905 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1907 /* Bad... Do not break raw sockets */
1908 sk
= skb_to_full_sk(skb
);
1909 if (unlikely(sk
&& hooknum
== NF_INET_LOCAL_OUT
&&
1912 if (sk
->sk_family
== PF_INET
&& inet_sk(sk
)->nodefrag
)
1916 #ifdef CONFIG_IP_VS_IPV6
1917 if (af
== AF_INET6
) {
1918 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1920 int verdict
= ip_vs_in_icmp_v6(ipvs
, skb
, &related
,
1928 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1930 int verdict
= ip_vs_in_icmp(ipvs
, skb
, &related
,
1937 /* Protocol supported? */
1938 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1939 if (unlikely(!pd
)) {
1940 /* The only way we'll see this packet again is if it's
1941 * encapsulated, so mark it with ipvs_property=1 so we
1942 * skip it if we're ignoring tunneled packets
1944 if (sysctl_ignore_tunneled(ipvs
))
1945 skb
->ipvs_property
= 1;
1951 * Check if the packet belongs to an existing connection entry
1953 cp
= INDIRECT_CALL_1(pp
->conn_in_get
, ip_vs_conn_in_get_proto
,
1954 ipvs
, af
, skb
, &iph
);
1956 conn_reuse_mode
= sysctl_conn_reuse_mode(ipvs
);
1957 if (conn_reuse_mode
&& !iph
.fragoffs
&& is_new_conn(skb
, &iph
) && cp
) {
1958 bool uses_ct
= false, resched
= false;
1960 if (unlikely(sysctl_expire_nodest_conn(ipvs
)) && cp
->dest
&&
1961 unlikely(!atomic_read(&cp
->dest
->weight
))) {
1963 uses_ct
= ip_vs_conn_uses_conntrack(cp
, skb
);
1964 } else if (is_new_conn_expected(cp
, conn_reuse_mode
)) {
1965 uses_ct
= ip_vs_conn_uses_conntrack(cp
, skb
);
1966 if (!atomic_read(&cp
->n_control
)) {
1969 /* Do not reschedule controlling connection
1970 * that uses conntrack while it is still
1971 * referenced by controlled connection(s).
1978 if (!atomic_read(&cp
->n_control
))
1979 ip_vs_conn_expire_now(cp
);
1980 __ip_vs_conn_put(cp
);
1987 if (unlikely(!cp
)) {
1990 if (!ip_vs_try_to_schedule(ipvs
, af
, skb
, pd
, &v
, &cp
, &iph
))
1994 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
.off
, "Incoming packet");
1996 /* Check the server status */
1997 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1998 /* the destination server is not available */
2000 __u32 flags
= cp
->flags
;
2002 /* when timer already started, silently drop the packet.*/
2003 if (timer_pending(&cp
->timer
))
2004 __ip_vs_conn_put(cp
);
2008 if (sysctl_expire_nodest_conn(ipvs
) &&
2009 !(flags
& IP_VS_CONN_F_ONE_PACKET
)) {
2010 /* try to expire the connection immediately */
2011 ip_vs_conn_expire_now(cp
);
2017 ip_vs_in_stats(cp
, skb
);
2018 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
2019 if (cp
->packet_xmit
)
2020 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
2021 /* do not touch skb anymore */
2023 IP_VS_DBG_RL("warning: packet_xmit is null");
2027 /* Increase its packet counter and check if it is needed
2028 * to be synchronized
2030 * Sync connection if it is about to close to
2031 * encorage the standby servers to update the connections timeout
2033 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
2036 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
2037 pkts
= sysctl_sync_threshold(ipvs
);
2039 pkts
= atomic_add_return(1, &cp
->in_pkts
);
2041 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
2042 ip_vs_sync_conn(ipvs
, cp
, pkts
);
2043 else if ((cp
->flags
& IP_VS_CONN_F_ONE_PACKET
) && cp
->control
)
2044 /* increment is done inside ip_vs_sync_conn too */
2045 atomic_inc(&cp
->control
->in_pkts
);
2052 * AF_INET handler in NF_INET_LOCAL_IN chain
2053 * Schedule and forward packets from remote clients
2056 ip_vs_remote_request4(void *priv
, struct sk_buff
*skb
,
2057 const struct nf_hook_state
*state
)
2059 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
2063 * AF_INET handler in NF_INET_LOCAL_OUT chain
2064 * Schedule and forward packets from local clients
2067 ip_vs_local_request4(void *priv
, struct sk_buff
*skb
,
2068 const struct nf_hook_state
*state
)
2070 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
2073 #ifdef CONFIG_IP_VS_IPV6
2076 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2077 * Schedule and forward packets from remote clients
2080 ip_vs_remote_request6(void *priv
, struct sk_buff
*skb
,
2081 const struct nf_hook_state
*state
)
2083 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
2087 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2088 * Schedule and forward packets from local clients
2091 ip_vs_local_request6(void *priv
, struct sk_buff
*skb
,
2092 const struct nf_hook_state
*state
)
2094 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
2101 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
2102 * related packets destined for 0.0.0.0/0.
2103 * When fwmark-based virtual service is used, such as transparent
2104 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2105 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
2106 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
2107 * and send them to ip_vs_in_icmp.
2110 ip_vs_forward_icmp(void *priv
, struct sk_buff
*skb
,
2111 const struct nf_hook_state
*state
)
2114 struct netns_ipvs
*ipvs
= net_ipvs(state
->net
);
2116 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
2119 /* ipvs enabled in this netns ? */
2120 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
2123 return ip_vs_in_icmp(ipvs
, skb
, &r
, state
->hook
);
2126 #ifdef CONFIG_IP_VS_IPV6
2128 ip_vs_forward_icmp_v6(void *priv
, struct sk_buff
*skb
,
2129 const struct nf_hook_state
*state
)
2132 struct netns_ipvs
*ipvs
= net_ipvs(state
->net
);
2133 struct ip_vs_iphdr iphdr
;
2135 ip_vs_fill_iph_skb(AF_INET6
, skb
, false, &iphdr
);
2136 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
2139 /* ipvs enabled in this netns ? */
2140 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
2143 return ip_vs_in_icmp_v6(ipvs
, skb
, &r
, state
->hook
, &iphdr
);
2148 static const struct nf_hook_ops ip_vs_ops
[] = {
2149 /* After packet filtering, change source only for VS/NAT */
2151 .hook
= ip_vs_reply4
,
2153 .hooknum
= NF_INET_LOCAL_IN
,
2154 .priority
= NF_IP_PRI_NAT_SRC
- 2,
2156 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2157 * or VS/NAT(change destination), so that filtering rules can be
2158 * applied to IPVS. */
2160 .hook
= ip_vs_remote_request4
,
2162 .hooknum
= NF_INET_LOCAL_IN
,
2163 .priority
= NF_IP_PRI_NAT_SRC
- 1,
2165 /* Before ip_vs_in, change source only for VS/NAT */
2167 .hook
= ip_vs_local_reply4
,
2169 .hooknum
= NF_INET_LOCAL_OUT
,
2170 .priority
= NF_IP_PRI_NAT_DST
+ 1,
2172 /* After mangle, schedule and forward local requests */
2174 .hook
= ip_vs_local_request4
,
2176 .hooknum
= NF_INET_LOCAL_OUT
,
2177 .priority
= NF_IP_PRI_NAT_DST
+ 2,
2179 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2180 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2182 .hook
= ip_vs_forward_icmp
,
2184 .hooknum
= NF_INET_FORWARD
,
2187 /* After packet filtering, change source only for VS/NAT */
2189 .hook
= ip_vs_reply4
,
2191 .hooknum
= NF_INET_FORWARD
,
2194 #ifdef CONFIG_IP_VS_IPV6
2195 /* After packet filtering, change source only for VS/NAT */
2197 .hook
= ip_vs_reply6
,
2199 .hooknum
= NF_INET_LOCAL_IN
,
2200 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
2202 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2203 * or VS/NAT(change destination), so that filtering rules can be
2204 * applied to IPVS. */
2206 .hook
= ip_vs_remote_request6
,
2208 .hooknum
= NF_INET_LOCAL_IN
,
2209 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
2211 /* Before ip_vs_in, change source only for VS/NAT */
2213 .hook
= ip_vs_local_reply6
,
2215 .hooknum
= NF_INET_LOCAL_OUT
,
2216 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
2218 /* After mangle, schedule and forward local requests */
2220 .hook
= ip_vs_local_request6
,
2222 .hooknum
= NF_INET_LOCAL_OUT
,
2223 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
2225 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2226 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2228 .hook
= ip_vs_forward_icmp_v6
,
2230 .hooknum
= NF_INET_FORWARD
,
2233 /* After packet filtering, change source only for VS/NAT */
2235 .hook
= ip_vs_reply6
,
2237 .hooknum
= NF_INET_FORWARD
,
2243 * Initialize IP Virtual Server netns mem.
2245 static int __net_init
__ip_vs_init(struct net
*net
)
2247 struct netns_ipvs
*ipvs
;
2250 ipvs
= net_generic(net
, ip_vs_net_id
);
2254 /* Hold the beast until a service is registerd */
2257 /* Counters used for creating unique names */
2258 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
2259 atomic_inc(&ipvs_netns_cnt
);
2262 if (ip_vs_estimator_net_init(ipvs
) < 0)
2263 goto estimator_fail
;
2265 if (ip_vs_control_net_init(ipvs
) < 0)
2268 if (ip_vs_protocol_net_init(ipvs
) < 0)
2271 if (ip_vs_app_net_init(ipvs
) < 0)
2274 if (ip_vs_conn_net_init(ipvs
) < 0)
2277 if (ip_vs_sync_net_init(ipvs
) < 0)
2280 ret
= nf_register_net_hooks(net
, ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2290 ip_vs_sync_net_cleanup(ipvs
);
2292 ip_vs_conn_net_cleanup(ipvs
);
2294 ip_vs_app_net_cleanup(ipvs
);
2296 ip_vs_protocol_net_cleanup(ipvs
);
2298 ip_vs_control_net_cleanup(ipvs
);
2300 ip_vs_estimator_net_cleanup(ipvs
);
2306 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2308 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2310 ip_vs_service_net_cleanup(ipvs
); /* ip_vs_flush() with locks */
2311 ip_vs_conn_net_cleanup(ipvs
);
2312 ip_vs_app_net_cleanup(ipvs
);
2313 ip_vs_protocol_net_cleanup(ipvs
);
2314 ip_vs_control_net_cleanup(ipvs
);
2315 ip_vs_estimator_net_cleanup(ipvs
);
2316 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs
->gen
);
2320 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2322 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2324 nf_unregister_net_hooks(net
, ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2325 ipvs
->enable
= 0; /* Disable packet reception */
2327 ip_vs_sync_net_cleanup(ipvs
);
2331 static struct pernet_operations ipvs_core_ops
= {
2332 .init
= __ip_vs_init
,
2333 .exit
= __ip_vs_cleanup
,
2334 .id
= &ip_vs_net_id
,
2335 .size
= sizeof(struct netns_ipvs
),
2338 static struct pernet_operations ipvs_core_dev_ops
= {
2339 .exit
= __ip_vs_dev_cleanup
,
2343 * Initialize IP Virtual Server
2345 static int __init
ip_vs_init(void)
2349 ret
= ip_vs_control_init();
2351 pr_err("can't setup control.\n");
2355 ip_vs_protocol_init();
2357 ret
= ip_vs_conn_init();
2359 pr_err("can't setup connection table.\n");
2360 goto cleanup_protocol
;
2363 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2367 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2371 ret
= ip_vs_register_nl_ioctl();
2373 pr_err("can't register netlink/ioctl.\n");
2377 pr_info("ipvs loaded.\n");
2382 unregister_pernet_device(&ipvs_core_dev_ops
);
2384 unregister_pernet_subsys(&ipvs_core_ops
);
2386 ip_vs_conn_cleanup();
2388 ip_vs_protocol_cleanup();
2389 ip_vs_control_cleanup();
2394 static void __exit
ip_vs_cleanup(void)
2396 ip_vs_unregister_nl_ioctl();
2397 unregister_pernet_device(&ipvs_core_dev_ops
);
2398 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2399 ip_vs_conn_cleanup();
2400 ip_vs_protocol_cleanup();
2401 ip_vs_control_cleanup();
2402 pr_info("ipvs unloaded.\n");
2405 module_init(ip_vs_init
);
2406 module_exit(ip_vs_cleanup
);
2407 MODULE_LICENSE("GPL");