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
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
22 * Paul `Rusty' Russell properly handle non-linear skbs
23 * Harald Welte don't use nfcache
27 #define KMSG_COMPONENT "IPVS"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #include <linux/tcp.h>
34 #include <linux/sctp.h>
35 #include <linux/icmp.h>
36 #include <linux/slab.h>
41 #include <net/icmp.h> /* for icmp_send */
42 #include <net/route.h>
43 #include <net/ip6_checksum.h>
44 #include <net/netns/generic.h> /* net_generic() */
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv4.h>
49 #ifdef CONFIG_IP_VS_IPV6
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_route.h>
55 #include <net/ip_vs.h>
58 EXPORT_SYMBOL(register_ip_vs_scheduler
);
59 EXPORT_SYMBOL(unregister_ip_vs_scheduler
);
60 EXPORT_SYMBOL(ip_vs_proto_name
);
61 EXPORT_SYMBOL(ip_vs_conn_new
);
62 EXPORT_SYMBOL(ip_vs_conn_in_get
);
63 EXPORT_SYMBOL(ip_vs_conn_out_get
);
64 #ifdef CONFIG_IP_VS_PROTO_TCP
65 EXPORT_SYMBOL(ip_vs_tcp_conn_listen
);
67 EXPORT_SYMBOL(ip_vs_conn_put
);
68 #ifdef CONFIG_IP_VS_DEBUG
69 EXPORT_SYMBOL(ip_vs_get_debug_level
);
72 static int ip_vs_net_id __read_mostly
;
73 /* netns cnt used for uniqueness */
74 static atomic_t ipvs_netns_cnt
= ATOMIC_INIT(0);
76 /* ID used in ICMP lookups */
77 #define icmp_id(icmph) (((icmph)->un).echo.id)
78 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
80 const char *ip_vs_proto_name(unsigned int proto
)
95 #ifdef CONFIG_IP_VS_IPV6
100 sprintf(buf
, "IP_%u", proto
);
105 void ip_vs_init_hash_table(struct list_head
*table
, int rows
)
108 INIT_LIST_HEAD(&table
[rows
]);
112 ip_vs_in_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
114 struct ip_vs_dest
*dest
= cp
->dest
;
115 struct netns_ipvs
*ipvs
= cp
->ipvs
;
117 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
118 struct ip_vs_cpu_stats
*s
;
119 struct ip_vs_service
*svc
;
121 s
= this_cpu_ptr(dest
->stats
.cpustats
);
122 u64_stats_update_begin(&s
->syncp
);
124 s
->cnt
.inbytes
+= skb
->len
;
125 u64_stats_update_end(&s
->syncp
);
128 svc
= rcu_dereference(dest
->svc
);
129 s
= this_cpu_ptr(svc
->stats
.cpustats
);
130 u64_stats_update_begin(&s
->syncp
);
132 s
->cnt
.inbytes
+= skb
->len
;
133 u64_stats_update_end(&s
->syncp
);
136 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
137 u64_stats_update_begin(&s
->syncp
);
139 s
->cnt
.inbytes
+= skb
->len
;
140 u64_stats_update_end(&s
->syncp
);
146 ip_vs_out_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
148 struct ip_vs_dest
*dest
= cp
->dest
;
149 struct netns_ipvs
*ipvs
= cp
->ipvs
;
151 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
152 struct ip_vs_cpu_stats
*s
;
153 struct ip_vs_service
*svc
;
155 s
= this_cpu_ptr(dest
->stats
.cpustats
);
156 u64_stats_update_begin(&s
->syncp
);
158 s
->cnt
.outbytes
+= skb
->len
;
159 u64_stats_update_end(&s
->syncp
);
162 svc
= rcu_dereference(dest
->svc
);
163 s
= this_cpu_ptr(svc
->stats
.cpustats
);
164 u64_stats_update_begin(&s
->syncp
);
166 s
->cnt
.outbytes
+= skb
->len
;
167 u64_stats_update_end(&s
->syncp
);
170 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
171 u64_stats_update_begin(&s
->syncp
);
173 s
->cnt
.outbytes
+= skb
->len
;
174 u64_stats_update_end(&s
->syncp
);
180 ip_vs_conn_stats(struct ip_vs_conn
*cp
, struct ip_vs_service
*svc
)
182 struct netns_ipvs
*ipvs
= svc
->ipvs
;
183 struct ip_vs_cpu_stats
*s
;
185 s
= this_cpu_ptr(cp
->dest
->stats
.cpustats
);
186 u64_stats_update_begin(&s
->syncp
);
188 u64_stats_update_end(&s
->syncp
);
190 s
= this_cpu_ptr(svc
->stats
.cpustats
);
191 u64_stats_update_begin(&s
->syncp
);
193 u64_stats_update_end(&s
->syncp
);
195 s
= this_cpu_ptr(ipvs
->tot_stats
.cpustats
);
196 u64_stats_update_begin(&s
->syncp
);
198 u64_stats_update_end(&s
->syncp
);
203 ip_vs_set_state(struct ip_vs_conn
*cp
, int direction
,
204 const struct sk_buff
*skb
,
205 struct ip_vs_proto_data
*pd
)
207 if (likely(pd
->pp
->state_transition
))
208 pd
->pp
->state_transition(cp
, direction
, skb
, pd
);
212 ip_vs_conn_fill_param_persist(const struct ip_vs_service
*svc
,
213 struct sk_buff
*skb
, int protocol
,
214 const union nf_inet_addr
*caddr
, __be16 cport
,
215 const union nf_inet_addr
*vaddr
, __be16 vport
,
216 struct ip_vs_conn_param
*p
)
218 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, protocol
, caddr
, cport
, vaddr
,
220 p
->pe
= rcu_dereference(svc
->pe
);
221 if (p
->pe
&& p
->pe
->fill_param
)
222 return p
->pe
->fill_param(p
, skb
);
228 * IPVS persistent scheduling function
229 * It creates a connection entry according to its template if exists,
230 * or selects a server and creates a connection entry plus a template.
231 * Locking: we are svc user (svc->refcnt), so we hold all dests too
232 * Protocols supported: TCP, UDP
234 static struct ip_vs_conn
*
235 ip_vs_sched_persist(struct ip_vs_service
*svc
,
236 struct sk_buff
*skb
, __be16 src_port
, __be16 dst_port
,
237 int *ignored
, struct ip_vs_iphdr
*iph
)
239 struct ip_vs_conn
*cp
= NULL
;
240 struct ip_vs_dest
*dest
;
241 struct ip_vs_conn
*ct
;
242 __be16 dport
= 0; /* destination port to forward */
244 struct ip_vs_conn_param param
;
245 const union nf_inet_addr fwmark
= { .ip
= htonl(svc
->fwmark
) };
246 union nf_inet_addr snet
; /* source network of the client,
248 const union nf_inet_addr
*src_addr
, *dst_addr
;
250 if (likely(!ip_vs_iph_inverse(iph
))) {
251 src_addr
= &iph
->saddr
;
252 dst_addr
= &iph
->daddr
;
254 src_addr
= &iph
->daddr
;
255 dst_addr
= &iph
->saddr
;
259 /* Mask saddr with the netmask to adjust template granularity */
260 #ifdef CONFIG_IP_VS_IPV6
261 if (svc
->af
== AF_INET6
)
262 ipv6_addr_prefix(&snet
.in6
, &src_addr
->in6
,
263 (__force __u32
) svc
->netmask
);
266 snet
.ip
= src_addr
->ip
& svc
->netmask
;
268 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
270 IP_VS_DBG_ADDR(svc
->af
, src_addr
), ntohs(src_port
),
271 IP_VS_DBG_ADDR(svc
->af
, dst_addr
), ntohs(dst_port
),
272 IP_VS_DBG_ADDR(svc
->af
, &snet
));
275 * As far as we know, FTP is a very complicated network protocol, and
276 * it uses control connection and data connections. For active FTP,
277 * FTP server initialize data connection to the client, its source port
278 * is often 20. For passive FTP, FTP server tells the clients the port
279 * that it passively listens to, and the client issues the data
280 * connection. In the tunneling or direct routing mode, the load
281 * balancer is on the client-to-server half of connection, the port
282 * number is unknown to the load balancer. So, a conn template like
283 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
284 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
285 * is created for other persistent services.
288 int protocol
= iph
->protocol
;
289 const union nf_inet_addr
*vaddr
= dst_addr
;
292 if (dst_port
== svc
->port
) {
294 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
296 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
298 if (svc
->port
!= FTPPORT
)
301 /* Note: persistent fwmark-based services and
302 * persistent port zero service are handled here.
304 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
305 * port zero template:
306 * <protocol,caddr,0,vaddr,0,daddr,0>
309 protocol
= IPPROTO_IP
;
313 /* return *ignored = -1 so NF_DROP can be used */
314 if (ip_vs_conn_fill_param_persist(svc
, skb
, protocol
, &snet
, 0,
315 vaddr
, vport
, ¶m
) < 0) {
321 /* Check if a template already exists */
322 ct
= ip_vs_ct_in_get(¶m
);
323 if (!ct
|| !ip_vs_check_template(ct
)) {
324 struct ip_vs_scheduler
*sched
;
327 * No template found or the dest of the connection
328 * template is not available.
329 * return *ignored=0 i.e. ICMP and NF_DROP
331 sched
= rcu_dereference(svc
->scheduler
);
333 /* read svc->sched_data after svc->scheduler */
335 dest
= sched
->schedule(svc
, skb
, iph
);
340 IP_VS_DBG(1, "p-schedule: no dest found.\n");
341 kfree(param
.pe_data
);
346 if (dst_port
== svc
->port
&& svc
->port
!= FTPPORT
)
350 * This adds param.pe_data to the template,
351 * and thus param.pe_data will be destroyed
352 * when the template expires */
353 ct
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
,
354 IP_VS_CONN_F_TEMPLATE
, dest
, skb
->mark
);
356 kfree(param
.pe_data
);
361 ct
->timeout
= svc
->timeout
;
363 /* set destination with the found template */
365 kfree(param
.pe_data
);
369 if (dport
== svc
->port
&& dest
->port
)
372 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
373 && iph
->protocol
== IPPROTO_UDP
) ?
374 IP_VS_CONN_F_ONE_PACKET
: 0;
377 * Create a new connection according to the template
379 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
, src_addr
,
380 src_port
, dst_addr
, dst_port
, ¶m
);
382 cp
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
, flags
, dest
,
393 ip_vs_control_add(cp
, ct
);
396 ip_vs_conn_stats(cp
, svc
);
402 * IPVS main scheduling function
403 * It selects a server according to the virtual service, and
404 * creates a connection entry.
405 * Protocols supported: TCP, UDP
409 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
410 * svc/scheduler decides that this packet should be accepted with
411 * NF_ACCEPT because it must not be scheduled.
413 * 0 : scheduler can not find destination, so try bypass or
414 * return ICMP and then NF_DROP (ip_vs_leave).
416 * -1 : scheduler tried to schedule but fatal error occurred, eg.
417 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
418 * failure such as missing Call-ID, ENOMEM on skb_linearize
419 * or pe_data. In this case we should return NF_DROP without
420 * any attempts to send ICMP with ip_vs_leave.
423 ip_vs_schedule(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
424 struct ip_vs_proto_data
*pd
, int *ignored
,
425 struct ip_vs_iphdr
*iph
)
427 struct ip_vs_protocol
*pp
= pd
->pp
;
428 struct ip_vs_conn
*cp
= NULL
;
429 struct ip_vs_scheduler
*sched
;
430 struct ip_vs_dest
*dest
;
431 __be16 _ports
[2], *pptr
, cport
, vport
;
432 const void *caddr
, *vaddr
;
437 * IPv6 frags, only the first hit here.
439 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
443 if (likely(!ip_vs_iph_inverse(iph
))) {
456 * FTPDATA needs this check when using local real server.
457 * Never schedule Active FTPDATA connections from real server.
458 * For LVS-NAT they must be already created. For other methods
459 * with persistence the connection is created on SYN+ACK.
461 if (cport
== FTPDATA
) {
462 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, iph
->off
,
463 "Not scheduling FTPDATA");
468 * Do not schedule replies from local real server.
470 if ((!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
)) {
471 iph
->hdr_flags
^= IP_VS_HDR_INVERSE
;
472 cp
= pp
->conn_in_get(svc
->ipvs
, svc
->af
, skb
, iph
);
473 iph
->hdr_flags
^= IP_VS_HDR_INVERSE
;
476 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, iph
->off
,
477 "Not scheduling reply for existing"
479 __ip_vs_conn_put(cp
);
487 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
488 return ip_vs_sched_persist(svc
, skb
, cport
, vport
, ignored
,
494 * Non-persistent service
496 if (!svc
->fwmark
&& vport
!= svc
->port
) {
498 pr_err("Schedule: port zero only supported "
499 "in persistent services, "
500 "check your ipvs configuration\n");
504 sched
= rcu_dereference(svc
->scheduler
);
506 /* read svc->sched_data after svc->scheduler */
508 dest
= sched
->schedule(svc
, skb
, iph
);
513 IP_VS_DBG(1, "Schedule: no dest found.\n");
517 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
518 && iph
->protocol
== IPPROTO_UDP
) ?
519 IP_VS_CONN_F_ONE_PACKET
: 0;
522 * Create a connection entry.
525 struct ip_vs_conn_param p
;
527 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
528 caddr
, cport
, vaddr
, vport
, &p
);
529 cp
= ip_vs_conn_new(&p
, dest
->af
, &dest
->addr
,
530 dest
->port
? dest
->port
: vport
,
531 flags
, dest
, skb
->mark
);
538 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
539 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
541 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
542 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
543 IP_VS_DBG_ADDR(cp
->daf
, &cp
->daddr
), ntohs(cp
->dport
),
544 cp
->flags
, atomic_read(&cp
->refcnt
));
546 ip_vs_conn_stats(cp
, svc
);
550 static inline int ip_vs_addr_is_unicast(struct net
*net
, int af
,
551 union nf_inet_addr
*addr
)
553 #ifdef CONFIG_IP_VS_IPV6
555 return ipv6_addr_type(&addr
->in6
) & IPV6_ADDR_UNICAST
;
557 return (inet_addr_type(net
, addr
->ip
) == RTN_UNICAST
);
561 * Pass or drop the packet.
562 * Called by ip_vs_in, when the virtual service is available but
563 * no destination is available for a new connection.
565 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
566 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
568 __be16 _ports
[2], *pptr
, dport
;
569 struct netns_ipvs
*ipvs
= svc
->ipvs
;
570 struct net
*net
= ipvs
->net
;
572 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
575 dport
= likely(!ip_vs_iph_inverse(iph
)) ? pptr
[1] : pptr
[0];
577 /* if it is fwmark-based service, the cache_bypass sysctl is up
578 and the destination is a non-local unicast, then create
579 a cache_bypass connection entry */
580 if (sysctl_cache_bypass(ipvs
) && svc
->fwmark
&&
581 !(iph
->hdr_flags
& (IP_VS_HDR_INVERSE
| IP_VS_HDR_ICMP
)) &&
582 ip_vs_addr_is_unicast(net
, svc
->af
, &iph
->daddr
)) {
584 struct ip_vs_conn
*cp
;
585 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
586 iph
->protocol
== IPPROTO_UDP
) ?
587 IP_VS_CONN_F_ONE_PACKET
: 0;
588 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
590 /* create a new connection entry */
591 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
593 struct ip_vs_conn_param p
;
594 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
595 &iph
->saddr
, pptr
[0],
596 &iph
->daddr
, pptr
[1], &p
);
597 cp
= ip_vs_conn_new(&p
, svc
->af
, &daddr
, 0,
598 IP_VS_CONN_F_BYPASS
| flags
,
605 ip_vs_in_stats(cp
, skb
);
608 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
610 /* transmit the first SYN packet */
611 ret
= cp
->packet_xmit(skb
, cp
, pd
->pp
, iph
);
612 /* do not touch skb anymore */
614 atomic_inc(&cp
->in_pkts
);
620 * When the virtual ftp service is presented, packets destined
621 * for other services on the VIP may get here (except services
622 * listed in the ipvs table), pass the packets, because it is
623 * not ipvs job to decide to drop the packets.
625 if (svc
->port
== FTPPORT
&& dport
!= FTPPORT
)
628 if (unlikely(ip_vs_iph_icmp(iph
)))
632 * Notify the client that the destination is unreachable, and
633 * release the socket buffer.
634 * Since it is in IP layer, the TCP socket is not actually
635 * created, the TCP RST packet cannot be sent, instead that
636 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
638 #ifdef CONFIG_IP_VS_IPV6
639 if (svc
->af
== AF_INET6
) {
641 skb
->dev
= net
->loopback_dev
;
642 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
645 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
652 static int sysctl_snat_reroute(struct netns_ipvs
*ipvs
)
654 return ipvs
->sysctl_snat_reroute
;
657 static int sysctl_nat_icmp_send(struct netns_ipvs
*ipvs
)
659 return ipvs
->sysctl_nat_icmp_send
;
662 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
664 return ipvs
->sysctl_expire_nodest_conn
;
669 static int sysctl_snat_reroute(struct netns_ipvs
*ipvs
) { return 0; }
670 static int sysctl_nat_icmp_send(struct netns_ipvs
*ipvs
) { return 0; }
671 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
675 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
677 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
680 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
682 if (NF_INET_LOCAL_IN
== hooknum
)
683 return IP_DEFRAG_VS_IN
;
684 if (NF_INET_FORWARD
== hooknum
)
685 return IP_DEFRAG_VS_FWD
;
686 return IP_DEFRAG_VS_OUT
;
689 static inline int ip_vs_gather_frags(struct netns_ipvs
*ipvs
,
690 struct sk_buff
*skb
, u_int32_t user
)
695 err
= ip_defrag(ipvs
->net
, skb
, user
);
698 ip_send_check(ip_hdr(skb
));
703 static int ip_vs_route_me_harder(struct netns_ipvs
*ipvs
, int af
,
704 struct sk_buff
*skb
, unsigned int hooknum
)
706 if (!sysctl_snat_reroute(ipvs
))
708 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
709 if (NF_INET_LOCAL_IN
== hooknum
)
711 #ifdef CONFIG_IP_VS_IPV6
712 if (af
== AF_INET6
) {
713 struct dst_entry
*dst
= skb_dst(skb
);
715 if (dst
->dev
&& !(dst
->dev
->flags
& IFF_LOOPBACK
) &&
716 ip6_route_me_harder(ipvs
->net
, skb
) != 0)
720 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
721 ip_route_me_harder(ipvs
->net
, skb
, RTN_LOCAL
) != 0)
728 * Packet has been made sufficiently writable in caller
729 * - inout: 1=in->out, 0=out->in
731 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
732 struct ip_vs_conn
*cp
, int inout
)
734 struct iphdr
*iph
= ip_hdr(skb
);
735 unsigned int icmp_offset
= iph
->ihl
*4;
736 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
738 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
741 iph
->saddr
= cp
->vaddr
.ip
;
743 ciph
->daddr
= cp
->vaddr
.ip
;
746 iph
->daddr
= cp
->daddr
.ip
;
748 ciph
->saddr
= cp
->daddr
.ip
;
752 /* the TCP/UDP/SCTP port */
753 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
754 IPPROTO_SCTP
== ciph
->protocol
) {
755 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
758 ports
[1] = cp
->vport
;
760 ports
[0] = cp
->dport
;
763 /* And finally the ICMP checksum */
765 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
766 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
769 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
770 "Forwarding altered outgoing ICMP");
772 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
773 "Forwarding altered incoming ICMP");
776 #ifdef CONFIG_IP_VS_IPV6
777 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
778 struct ip_vs_conn
*cp
, int inout
)
780 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
781 unsigned int icmp_offset
= 0;
782 unsigned int offs
= 0; /* header offset*/
784 struct icmp6hdr
*icmph
;
785 struct ipv6hdr
*ciph
;
786 unsigned short fragoffs
;
788 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
789 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
790 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
791 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
793 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
796 iph
->saddr
= cp
->vaddr
.in6
;
797 ciph
->daddr
= cp
->vaddr
.in6
;
799 iph
->daddr
= cp
->daddr
.in6
;
800 ciph
->saddr
= cp
->daddr
.in6
;
803 /* the TCP/UDP/SCTP port */
804 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
805 IPPROTO_SCTP
== protocol
)) {
806 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
808 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
809 ntohs(inout
? ports
[1] : ports
[0]),
810 ntohs(inout
? cp
->vport
: cp
->dport
));
812 ports
[1] = cp
->vport
;
814 ports
[0] = cp
->dport
;
817 /* And finally the ICMP checksum */
818 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
819 skb
->len
- icmp_offset
,
821 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
822 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
823 skb
->ip_summed
= CHECKSUM_PARTIAL
;
826 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
827 (void *)ciph
- (void *)iph
,
828 "Forwarding altered outgoing ICMPv6");
830 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
831 (void *)ciph
- (void *)iph
,
832 "Forwarding altered incoming ICMPv6");
836 /* Handle relevant response ICMP messages - forward to the right
839 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
840 union nf_inet_addr
*snet
,
841 __u8 protocol
, struct ip_vs_conn
*cp
,
842 struct ip_vs_protocol
*pp
,
843 unsigned int offset
, unsigned int ihl
,
844 unsigned int hooknum
)
846 unsigned int verdict
= NF_DROP
;
848 if (IP_VS_FWD_METHOD(cp
) != 0) {
849 pr_err("shouldn't reach here, because the box is on the "
850 "half connection in the tun/dr module.\n");
853 /* Ensure the checksum is correct */
854 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
855 /* Failed checksum! */
856 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
857 IP_VS_DBG_ADDR(af
, snet
));
861 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
862 IPPROTO_SCTP
== protocol
)
863 offset
+= 2 * sizeof(__u16
);
864 if (!skb_make_writable(skb
, offset
))
867 #ifdef CONFIG_IP_VS_IPV6
869 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
872 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
874 if (ip_vs_route_me_harder(cp
->ipvs
, af
, skb
, hooknum
))
877 /* do the statistics and put it back */
878 ip_vs_out_stats(cp
, skb
);
880 skb
->ipvs_property
= 1;
881 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
884 ip_vs_update_conntrack(skb
, cp
, 0);
888 __ip_vs_conn_put(cp
);
894 * Handle ICMP messages in the inside-to-outside direction (outgoing).
895 * Find any that might be relevant, check against existing connections.
896 * Currently handles error types - unreachable, quench, ttl exceeded.
898 static int ip_vs_out_icmp(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
899 int *related
, unsigned int hooknum
)
902 struct icmphdr _icmph
, *ic
;
903 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
904 struct ip_vs_iphdr ciph
;
905 struct ip_vs_conn
*cp
;
906 struct ip_vs_protocol
*pp
;
907 unsigned int offset
, ihl
;
908 union nf_inet_addr snet
;
912 /* reassemble IP fragments */
913 if (ip_is_fragment(ip_hdr(skb
))) {
914 if (ip_vs_gather_frags(ipvs
, skb
, ip_vs_defrag_user(hooknum
)))
919 offset
= ihl
= iph
->ihl
* 4;
920 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
924 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
925 ic
->type
, ntohs(icmp_id(ic
)),
926 &iph
->saddr
, &iph
->daddr
);
929 * Work through seeing if this is for us.
930 * These checks are supposed to be in an order that means easy
931 * things are checked first to speed up processing.... however
932 * this means that some packets will manage to get a long way
933 * down this stack and then be rejected, but that's life.
935 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
936 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
937 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
942 /* Now find the contained IP header */
943 offset
+= sizeof(_icmph
);
944 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
946 return NF_ACCEPT
; /* The packet looks wrong, ignore */
948 pp
= ip_vs_proto_get(cih
->protocol
);
952 /* Is the embedded protocol header present? */
953 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
957 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
958 "Checking outgoing ICMP for");
960 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, true, &ciph
);
962 /* The embedded headers contain source and dest in reverse order */
963 cp
= pp
->conn_out_get(ipvs
, AF_INET
, skb
, &ciph
);
967 snet
.ip
= iph
->saddr
;
968 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
969 pp
, ciph
.len
, ihl
, hooknum
);
972 #ifdef CONFIG_IP_VS_IPV6
973 static int ip_vs_out_icmp_v6(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
974 int *related
, unsigned int hooknum
,
975 struct ip_vs_iphdr
*ipvsh
)
977 struct icmp6hdr _icmph
, *ic
;
978 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
979 struct ip_vs_conn
*cp
;
980 struct ip_vs_protocol
*pp
;
981 union nf_inet_addr snet
;
985 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
, ipvsh
);
990 * Work through seeing if this is for us.
991 * These checks are supposed to be in an order that means easy
992 * things are checked first to speed up processing.... however
993 * this means that some packets will manage to get a long way
994 * down this stack and then be rejected, but that's life.
996 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1000 /* Fragment header that is before ICMP header tells us that:
1001 * it's not an error message since they can't be fragmented.
1003 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
1006 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1007 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1008 &ipvsh
->saddr
, &ipvsh
->daddr
);
1010 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, ipvsh
->len
+ sizeof(_icmph
),
1012 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1014 pp
= ip_vs_proto_get(ciph
.protocol
);
1018 /* The embedded headers contain source and dest in reverse order */
1019 cp
= pp
->conn_out_get(ipvs
, AF_INET6
, skb
, &ciph
);
1023 snet
.in6
= ciph
.saddr
.in6
;
1025 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
1026 pp
, offset
, sizeof(struct ipv6hdr
),
1032 * Check if sctp chunc is ABORT chunk
1034 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
1036 sctp_chunkhdr_t
*sch
, schunk
;
1037 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
1038 sizeof(schunk
), &schunk
);
1041 if (sch
->type
== SCTP_CID_ABORT
)
1046 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
1048 struct tcphdr _tcph
, *th
;
1050 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1056 static inline bool is_new_conn(const struct sk_buff
*skb
,
1057 struct ip_vs_iphdr
*iph
)
1059 switch (iph
->protocol
) {
1061 struct tcphdr _tcph
, *th
;
1063 th
= skb_header_pointer(skb
, iph
->len
, sizeof(_tcph
), &_tcph
);
1068 case IPPROTO_SCTP
: {
1069 sctp_chunkhdr_t
*sch
, schunk
;
1071 sch
= skb_header_pointer(skb
, iph
->len
+ sizeof(sctp_sctphdr_t
),
1072 sizeof(schunk
), &schunk
);
1075 return sch
->type
== SCTP_CID_INIT
;
1082 static inline bool is_new_conn_expected(const struct ip_vs_conn
*cp
,
1083 int conn_reuse_mode
)
1085 /* Controlled (FTP DATA or persistence)? */
1089 switch (cp
->protocol
) {
1091 return (cp
->state
== IP_VS_TCP_S_TIME_WAIT
) ||
1092 (cp
->state
== IP_VS_TCP_S_CLOSE
) ||
1093 ((conn_reuse_mode
& 2) &&
1094 (cp
->state
== IP_VS_TCP_S_FIN_WAIT
) &&
1095 (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
));
1097 return cp
->state
== IP_VS_SCTP_S_CLOSED
;
1103 /* Handle response packets: rewrite addresses and send away...
1106 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1107 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
,
1108 unsigned int hooknum
)
1110 struct ip_vs_protocol
*pp
= pd
->pp
;
1112 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
->off
, "Outgoing packet");
1114 if (!skb_make_writable(skb
, iph
->len
))
1117 /* mangle the packet */
1118 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
, iph
))
1121 #ifdef CONFIG_IP_VS_IPV6
1123 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1127 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1128 ip_send_check(ip_hdr(skb
));
1132 * nf_iterate does not expect change in the skb->dst->dev.
1133 * It looks like it is not fatal to enable this code for hooks
1134 * where our handlers are at the end of the chain list and
1135 * when all next handlers use skb->dst->dev and not outdev.
1136 * It will definitely route properly the inout NAT traffic
1137 * when multiple paths are used.
1140 /* For policy routing, packets originating from this
1141 * machine itself may be routed differently to packets
1142 * passing through. We want this packet to be routed as
1143 * if it came from this machine itself. So re-compute
1144 * the routing information.
1146 if (ip_vs_route_me_harder(cp
->ipvs
, af
, skb
, hooknum
))
1149 IP_VS_DBG_PKT(10, af
, pp
, skb
, iph
->off
, "After SNAT");
1151 ip_vs_out_stats(cp
, skb
);
1152 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1153 skb
->ipvs_property
= 1;
1154 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1157 ip_vs_update_conntrack(skb
, cp
, 0);
1171 * Check if outgoing packet belongs to the established ip_vs_conn.
1174 ip_vs_out(struct netns_ipvs
*ipvs
, unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1176 struct ip_vs_iphdr iph
;
1177 struct ip_vs_protocol
*pp
;
1178 struct ip_vs_proto_data
*pd
;
1179 struct ip_vs_conn
*cp
;
1184 /* Already marked as IPVS request or reply? */
1185 if (skb
->ipvs_property
)
1188 sk
= skb_to_full_sk(skb
);
1189 /* Bad... Do not break raw sockets */
1190 if (unlikely(sk
&& hooknum
== NF_INET_LOCAL_OUT
&&
1193 if (sk
->sk_family
== PF_INET
&& inet_sk(sk
)->nodefrag
)
1197 if (unlikely(!skb_dst(skb
)))
1203 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1204 #ifdef CONFIG_IP_VS_IPV6
1205 if (af
== AF_INET6
) {
1206 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1208 int verdict
= ip_vs_out_icmp_v6(ipvs
, skb
, &related
,
1216 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1218 int verdict
= ip_vs_out_icmp(ipvs
, skb
, &related
, hooknum
);
1224 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1229 /* reassemble IP fragments */
1230 #ifdef CONFIG_IP_VS_IPV6
1233 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1234 if (ip_vs_gather_frags(ipvs
, skb
,
1235 ip_vs_defrag_user(hooknum
)))
1238 ip_vs_fill_iph_skb(AF_INET
, skb
, false, &iph
);
1242 * Check if the packet belongs to an existing entry
1244 cp
= pp
->conn_out_get(ipvs
, af
, skb
, &iph
);
1247 return handle_response(af
, skb
, pd
, cp
, &iph
, hooknum
);
1248 if (sysctl_nat_icmp_send(ipvs
) &&
1249 (pp
->protocol
== IPPROTO_TCP
||
1250 pp
->protocol
== IPPROTO_UDP
||
1251 pp
->protocol
== IPPROTO_SCTP
)) {
1252 __be16 _ports
[2], *pptr
;
1254 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1255 sizeof(_ports
), _ports
, &iph
);
1257 return NF_ACCEPT
; /* Not for me */
1258 if (ip_vs_has_real_service(ipvs
, af
, iph
.protocol
, &iph
.saddr
,
1261 * Notify the real server: there is no
1262 * existing entry if it is not RST
1263 * packet or not TCP packet.
1265 if ((iph
.protocol
!= IPPROTO_TCP
&&
1266 iph
.protocol
!= IPPROTO_SCTP
)
1267 || ((iph
.protocol
== IPPROTO_TCP
1268 && !is_tcp_reset(skb
, iph
.len
))
1269 || (iph
.protocol
== IPPROTO_SCTP
1270 && !is_sctp_abort(skb
,
1272 #ifdef CONFIG_IP_VS_IPV6
1273 if (af
== AF_INET6
) {
1275 skb
->dev
= ipvs
->net
->loopback_dev
;
1277 ICMPV6_DEST_UNREACH
,
1278 ICMPV6_PORT_UNREACH
,
1284 ICMP_PORT_UNREACH
, 0);
1289 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
.off
,
1290 "ip_vs_out: packet continues traversal as normal");
1295 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1296 * used only for VS/NAT.
1297 * Check if packet is reply for established ip_vs_conn.
1300 ip_vs_reply4(void *priv
, struct sk_buff
*skb
,
1301 const struct nf_hook_state
*state
)
1303 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1307 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1308 * Check if packet is reply for established ip_vs_conn.
1311 ip_vs_local_reply4(void *priv
, struct sk_buff
*skb
,
1312 const struct nf_hook_state
*state
)
1314 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1317 #ifdef CONFIG_IP_VS_IPV6
1320 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1321 * used only for VS/NAT.
1322 * Check if packet is reply for established ip_vs_conn.
1325 ip_vs_reply6(void *priv
, struct sk_buff
*skb
,
1326 const struct nf_hook_state
*state
)
1328 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1332 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1333 * Check if packet is reply for established ip_vs_conn.
1336 ip_vs_local_reply6(void *priv
, struct sk_buff
*skb
,
1337 const struct nf_hook_state
*state
)
1339 return ip_vs_out(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1345 ip_vs_try_to_schedule(struct netns_ipvs
*ipvs
, int af
, struct sk_buff
*skb
,
1346 struct ip_vs_proto_data
*pd
,
1347 int *verdict
, struct ip_vs_conn
**cpp
,
1348 struct ip_vs_iphdr
*iph
)
1350 struct ip_vs_protocol
*pp
= pd
->pp
;
1352 if (!iph
->fragoffs
) {
1353 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1354 * replayed fragment zero will already have created the cp
1357 /* Schedule and create new connection entry into cpp */
1358 if (!pp
->conn_schedule(ipvs
, af
, skb
, pd
, verdict
, cpp
, iph
))
1362 if (unlikely(!*cpp
)) {
1363 /* sorry, all this trouble for a no-hit :) */
1364 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
->off
,
1365 "ip_vs_in: packet continues traversal as normal");
1366 if (iph
->fragoffs
) {
1367 /* Fragment that couldn't be mapped to a conn entry
1368 * is missing module nf_defrag_ipv6
1370 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1371 IP_VS_DBG_PKT(7, af
, pp
, skb
, iph
->off
,
1372 "unhandled fragment");
1374 *verdict
= NF_ACCEPT
;
1382 * Handle ICMP messages in the outside-to-inside direction (incoming).
1383 * Find any that might be relevant, check against existing connections,
1384 * forward to the right destination host if relevant.
1385 * Currently handles error types - unreachable, quench, ttl exceeded.
1388 ip_vs_in_icmp(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
, int *related
,
1389 unsigned int hooknum
)
1392 struct icmphdr _icmph
, *ic
;
1393 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1394 struct ip_vs_iphdr ciph
;
1395 struct ip_vs_conn
*cp
;
1396 struct ip_vs_protocol
*pp
;
1397 struct ip_vs_proto_data
*pd
;
1398 unsigned int offset
, offset2
, ihl
, verdict
;
1399 bool ipip
, new_cp
= false;
1403 /* reassemble IP fragments */
1404 if (ip_is_fragment(ip_hdr(skb
))) {
1405 if (ip_vs_gather_frags(ipvs
, skb
, ip_vs_defrag_user(hooknum
)))
1410 offset
= ihl
= iph
->ihl
* 4;
1411 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1415 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1416 ic
->type
, ntohs(icmp_id(ic
)),
1417 &iph
->saddr
, &iph
->daddr
);
1420 * Work through seeing if this is for us.
1421 * These checks are supposed to be in an order that means easy
1422 * things are checked first to speed up processing.... however
1423 * this means that some packets will manage to get a long way
1424 * down this stack and then be rejected, but that's life.
1426 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1427 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1428 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1433 /* Now find the contained IP header */
1434 offset
+= sizeof(_icmph
);
1435 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1437 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1439 /* Special case for errors for IPIP packets */
1441 if (cih
->protocol
== IPPROTO_IPIP
) {
1442 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1444 /* Error for our IPIP must arrive at LOCAL_IN */
1445 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1447 offset
+= cih
->ihl
* 4;
1448 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1450 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1454 pd
= ip_vs_proto_data_get(ipvs
, cih
->protocol
);
1459 /* Is the embedded protocol header present? */
1460 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1464 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1465 "Checking incoming ICMP for");
1468 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, !ipip
, &ciph
);
1471 /* The embedded headers contain source and dest in reverse order.
1472 * For IPIP this is error for request, not for reply.
1474 cp
= pp
->conn_in_get(ipvs
, AF_INET
, skb
, &ciph
);
1479 if (!sysctl_schedule_icmp(ipvs
))
1482 if (!ip_vs_try_to_schedule(ipvs
, AF_INET
, skb
, pd
, &v
, &cp
, &ciph
))
1489 /* Ensure the checksum is correct */
1490 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1491 /* Failed checksum! */
1492 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1498 __be32 info
= ic
->un
.gateway
;
1499 __u8 type
= ic
->type
;
1500 __u8 code
= ic
->code
;
1502 /* Update the MTU */
1503 if (ic
->type
== ICMP_DEST_UNREACH
&&
1504 ic
->code
== ICMP_FRAG_NEEDED
) {
1505 struct ip_vs_dest
*dest
= cp
->dest
;
1506 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1507 __be16 frag_off
= cih
->frag_off
;
1509 /* Strip outer IP and ICMP, go to IPIP header */
1510 if (pskb_pull(skb
, ihl
+ sizeof(_icmph
)) == NULL
)
1512 offset2
-= ihl
+ sizeof(_icmph
);
1513 skb_reset_network_header(skb
);
1514 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1515 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1516 ipv4_update_pmtu(skb
, ipvs
->net
,
1518 /* Client uses PMTUD? */
1519 if (!(frag_off
& htons(IP_DF
)))
1521 /* Prefer the resulting PMTU */
1523 struct ip_vs_dest_dst
*dest_dst
;
1526 dest_dst
= rcu_dereference(dest
->dest_dst
);
1528 mtu
= dst_mtu(dest_dst
->dst_cache
);
1531 if (mtu
> 68 + sizeof(struct iphdr
))
1532 mtu
-= sizeof(struct iphdr
);
1535 /* Strip outer IP, ICMP and IPIP, go to IP header of
1538 if (pskb_pull(skb
, offset2
) == NULL
)
1540 skb_reset_network_header(skb
);
1541 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1542 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1543 type
, code
, ntohl(info
));
1544 icmp_send(skb
, type
, code
, info
);
1545 /* ICMP can be shorter but anyways, account it */
1546 ip_vs_out_stats(cp
, skb
);
1550 verdict
= NF_STOLEN
;
1554 /* do the statistics and put it back */
1555 ip_vs_in_stats(cp
, skb
);
1556 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
||
1557 IPPROTO_SCTP
== cih
->protocol
)
1558 offset
+= 2 * sizeof(__u16
);
1559 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1562 if (likely(!new_cp
))
1563 __ip_vs_conn_put(cp
);
1570 #ifdef CONFIG_IP_VS_IPV6
1571 static int ip_vs_in_icmp_v6(struct netns_ipvs
*ipvs
, struct sk_buff
*skb
,
1572 int *related
, unsigned int hooknum
,
1573 struct ip_vs_iphdr
*iph
)
1575 struct icmp6hdr _icmph
, *ic
;
1576 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1577 struct ip_vs_conn
*cp
;
1578 struct ip_vs_protocol
*pp
;
1579 struct ip_vs_proto_data
*pd
;
1580 unsigned int offset
, verdict
;
1581 bool new_cp
= false;
1585 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
, iph
);
1590 * Work through seeing if this is for us.
1591 * These checks are supposed to be in an order that means easy
1592 * things are checked first to speed up processing.... however
1593 * this means that some packets will manage to get a long way
1594 * down this stack and then be rejected, but that's life.
1596 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1600 /* Fragment header that is before ICMP header tells us that:
1601 * it's not an error message since they can't be fragmented.
1603 if (iph
->flags
& IP6_FH_F_FRAG
)
1606 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1607 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1608 &iph
->saddr
, &iph
->daddr
);
1610 offset
= iph
->len
+ sizeof(_icmph
);
1611 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, offset
, true, &ciph
))
1614 pd
= ip_vs_proto_data_get(ipvs
, ciph
.protocol
);
1619 /* Cannot handle fragmented embedded protocol */
1623 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offset
,
1624 "Checking incoming ICMPv6 for");
1626 /* The embedded headers contain source and dest in reverse order
1627 * if not from localhost
1629 cp
= pp
->conn_in_get(ipvs
, AF_INET6
, skb
, &ciph
);
1634 if (!sysctl_schedule_icmp(ipvs
))
1637 if (!ip_vs_try_to_schedule(ipvs
, AF_INET6
, skb
, pd
, &v
, &cp
, &ciph
))
1643 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1644 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1645 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1646 verdict
= NF_ACCEPT
;
1650 /* do the statistics and put it back */
1651 ip_vs_in_stats(cp
, skb
);
1653 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1655 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1656 IPPROTO_SCTP
== ciph
.protocol
)
1657 offset
+= 2 * sizeof(__u16
); /* Also mangle ports */
1659 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1662 if (likely(!new_cp
))
1663 __ip_vs_conn_put(cp
);
1673 * Check if it's for virtual services, look it up,
1674 * and send it on its way...
1677 ip_vs_in(struct netns_ipvs
*ipvs
, unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1679 struct ip_vs_iphdr iph
;
1680 struct ip_vs_protocol
*pp
;
1681 struct ip_vs_proto_data
*pd
;
1682 struct ip_vs_conn
*cp
;
1684 int conn_reuse_mode
;
1687 /* Already marked as IPVS request or reply? */
1688 if (skb
->ipvs_property
)
1693 * - remote client: only PACKET_HOST
1694 * - route: used for struct net when skb->dev is unset
1696 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1697 hooknum
!= NF_INET_LOCAL_OUT
) ||
1699 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1700 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1701 " ignored in hook %u\n",
1702 skb
->pkt_type
, iph
.protocol
,
1703 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1706 /* ipvs enabled in this netns ? */
1707 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1710 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1712 /* Bad... Do not break raw sockets */
1713 sk
= skb_to_full_sk(skb
);
1714 if (unlikely(sk
&& hooknum
== NF_INET_LOCAL_OUT
&&
1717 if (sk
->sk_family
== PF_INET
&& inet_sk(sk
)->nodefrag
)
1721 #ifdef CONFIG_IP_VS_IPV6
1722 if (af
== AF_INET6
) {
1723 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1725 int verdict
= ip_vs_in_icmp_v6(ipvs
, skb
, &related
,
1733 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1735 int verdict
= ip_vs_in_icmp(ipvs
, skb
, &related
,
1742 /* Protocol supported? */
1743 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1744 if (unlikely(!pd
)) {
1745 /* The only way we'll see this packet again is if it's
1746 * encapsulated, so mark it with ipvs_property=1 so we
1747 * skip it if we're ignoring tunneled packets
1749 if (sysctl_ignore_tunneled(ipvs
))
1750 skb
->ipvs_property
= 1;
1756 * Check if the packet belongs to an existing connection entry
1758 cp
= pp
->conn_in_get(ipvs
, af
, skb
, &iph
);
1760 conn_reuse_mode
= sysctl_conn_reuse_mode(ipvs
);
1761 if (conn_reuse_mode
&& !iph
.fragoffs
&& is_new_conn(skb
, &iph
) && cp
) {
1762 bool uses_ct
= false, resched
= false;
1764 if (unlikely(sysctl_expire_nodest_conn(ipvs
)) && cp
->dest
&&
1765 unlikely(!atomic_read(&cp
->dest
->weight
))) {
1767 uses_ct
= ip_vs_conn_uses_conntrack(cp
, skb
);
1768 } else if (is_new_conn_expected(cp
, conn_reuse_mode
)) {
1769 uses_ct
= ip_vs_conn_uses_conntrack(cp
, skb
);
1770 if (!atomic_read(&cp
->n_control
)) {
1773 /* Do not reschedule controlling connection
1774 * that uses conntrack while it is still
1775 * referenced by controlled connection(s).
1782 if (!atomic_read(&cp
->n_control
))
1783 ip_vs_conn_expire_now(cp
);
1784 __ip_vs_conn_put(cp
);
1791 if (unlikely(!cp
)) {
1794 if (!ip_vs_try_to_schedule(ipvs
, af
, skb
, pd
, &v
, &cp
, &iph
))
1798 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
.off
, "Incoming packet");
1800 /* Check the server status */
1801 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1802 /* the destination server is not available */
1804 if (sysctl_expire_nodest_conn(ipvs
)) {
1805 /* try to expire the connection immediately */
1806 ip_vs_conn_expire_now(cp
);
1808 /* don't restart its timer, and silently
1810 __ip_vs_conn_put(cp
);
1814 ip_vs_in_stats(cp
, skb
);
1815 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
1816 if (cp
->packet_xmit
)
1817 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
1818 /* do not touch skb anymore */
1820 IP_VS_DBG_RL("warning: packet_xmit is null");
1824 /* Increase its packet counter and check if it is needed
1825 * to be synchronized
1827 * Sync connection if it is about to close to
1828 * encorage the standby servers to update the connections timeout
1830 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1833 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
1834 pkts
= sysctl_sync_threshold(ipvs
);
1836 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1838 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
1839 ip_vs_sync_conn(ipvs
, cp
, pkts
);
1846 * AF_INET handler in NF_INET_LOCAL_IN chain
1847 * Schedule and forward packets from remote clients
1850 ip_vs_remote_request4(void *priv
, struct sk_buff
*skb
,
1851 const struct nf_hook_state
*state
)
1853 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1857 * AF_INET handler in NF_INET_LOCAL_OUT chain
1858 * Schedule and forward packets from local clients
1861 ip_vs_local_request4(void *priv
, struct sk_buff
*skb
,
1862 const struct nf_hook_state
*state
)
1864 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET
);
1867 #ifdef CONFIG_IP_VS_IPV6
1870 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1871 * Schedule and forward packets from remote clients
1874 ip_vs_remote_request6(void *priv
, struct sk_buff
*skb
,
1875 const struct nf_hook_state
*state
)
1877 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1881 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1882 * Schedule and forward packets from local clients
1885 ip_vs_local_request6(void *priv
, struct sk_buff
*skb
,
1886 const struct nf_hook_state
*state
)
1888 return ip_vs_in(net_ipvs(state
->net
), state
->hook
, skb
, AF_INET6
);
1895 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1896 * related packets destined for 0.0.0.0/0.
1897 * When fwmark-based virtual service is used, such as transparent
1898 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1899 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1900 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1901 * and send them to ip_vs_in_icmp.
1904 ip_vs_forward_icmp(void *priv
, struct sk_buff
*skb
,
1905 const struct nf_hook_state
*state
)
1908 struct netns_ipvs
*ipvs
= net_ipvs(state
->net
);
1910 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1913 /* ipvs enabled in this netns ? */
1914 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1917 return ip_vs_in_icmp(ipvs
, skb
, &r
, state
->hook
);
1920 #ifdef CONFIG_IP_VS_IPV6
1922 ip_vs_forward_icmp_v6(void *priv
, struct sk_buff
*skb
,
1923 const struct nf_hook_state
*state
)
1926 struct netns_ipvs
*ipvs
= net_ipvs(state
->net
);
1927 struct ip_vs_iphdr iphdr
;
1929 ip_vs_fill_iph_skb(AF_INET6
, skb
, false, &iphdr
);
1930 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
1933 /* ipvs enabled in this netns ? */
1934 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1937 return ip_vs_in_icmp_v6(ipvs
, skb
, &r
, state
->hook
, &iphdr
);
1942 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1943 /* After packet filtering, change source only for VS/NAT */
1945 .hook
= ip_vs_reply4
,
1947 .hooknum
= NF_INET_LOCAL_IN
,
1948 .priority
= NF_IP_PRI_NAT_SRC
- 2,
1950 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1951 * or VS/NAT(change destination), so that filtering rules can be
1952 * applied to IPVS. */
1954 .hook
= ip_vs_remote_request4
,
1956 .hooknum
= NF_INET_LOCAL_IN
,
1957 .priority
= NF_IP_PRI_NAT_SRC
- 1,
1959 /* Before ip_vs_in, change source only for VS/NAT */
1961 .hook
= ip_vs_local_reply4
,
1963 .hooknum
= NF_INET_LOCAL_OUT
,
1964 .priority
= NF_IP_PRI_NAT_DST
+ 1,
1966 /* After mangle, schedule and forward local requests */
1968 .hook
= ip_vs_local_request4
,
1970 .hooknum
= NF_INET_LOCAL_OUT
,
1971 .priority
= NF_IP_PRI_NAT_DST
+ 2,
1973 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1974 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1976 .hook
= ip_vs_forward_icmp
,
1978 .hooknum
= NF_INET_FORWARD
,
1981 /* After packet filtering, change source only for VS/NAT */
1983 .hook
= ip_vs_reply4
,
1985 .hooknum
= NF_INET_FORWARD
,
1988 #ifdef CONFIG_IP_VS_IPV6
1989 /* After packet filtering, change source only for VS/NAT */
1991 .hook
= ip_vs_reply6
,
1993 .hooknum
= NF_INET_LOCAL_IN
,
1994 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
1996 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1997 * or VS/NAT(change destination), so that filtering rules can be
1998 * applied to IPVS. */
2000 .hook
= ip_vs_remote_request6
,
2002 .hooknum
= NF_INET_LOCAL_IN
,
2003 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
2005 /* Before ip_vs_in, change source only for VS/NAT */
2007 .hook
= ip_vs_local_reply6
,
2009 .hooknum
= NF_INET_LOCAL_OUT
,
2010 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
2012 /* After mangle, schedule and forward local requests */
2014 .hook
= ip_vs_local_request6
,
2016 .hooknum
= NF_INET_LOCAL_OUT
,
2017 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
2019 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2020 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2022 .hook
= ip_vs_forward_icmp_v6
,
2024 .hooknum
= NF_INET_FORWARD
,
2027 /* After packet filtering, change source only for VS/NAT */
2029 .hook
= ip_vs_reply6
,
2031 .hooknum
= NF_INET_FORWARD
,
2037 * Initialize IP Virtual Server netns mem.
2039 static int __net_init
__ip_vs_init(struct net
*net
)
2041 struct netns_ipvs
*ipvs
;
2043 ipvs
= net_generic(net
, ip_vs_net_id
);
2047 /* Hold the beast until a service is registerd */
2050 /* Counters used for creating unique names */
2051 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
2052 atomic_inc(&ipvs_netns_cnt
);
2055 if (ip_vs_estimator_net_init(ipvs
) < 0)
2056 goto estimator_fail
;
2058 if (ip_vs_control_net_init(ipvs
) < 0)
2061 if (ip_vs_protocol_net_init(ipvs
) < 0)
2064 if (ip_vs_app_net_init(ipvs
) < 0)
2067 if (ip_vs_conn_net_init(ipvs
) < 0)
2070 if (ip_vs_sync_net_init(ipvs
) < 0)
2073 printk(KERN_INFO
"IPVS: Creating netns size=%zu id=%d\n",
2074 sizeof(struct netns_ipvs
), ipvs
->gen
);
2081 ip_vs_conn_net_cleanup(ipvs
);
2083 ip_vs_app_net_cleanup(ipvs
);
2085 ip_vs_protocol_net_cleanup(ipvs
);
2087 ip_vs_control_net_cleanup(ipvs
);
2089 ip_vs_estimator_net_cleanup(ipvs
);
2095 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2097 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2099 ip_vs_service_net_cleanup(ipvs
); /* ip_vs_flush() with locks */
2100 ip_vs_conn_net_cleanup(ipvs
);
2101 ip_vs_app_net_cleanup(ipvs
);
2102 ip_vs_protocol_net_cleanup(ipvs
);
2103 ip_vs_control_net_cleanup(ipvs
);
2104 ip_vs_estimator_net_cleanup(ipvs
);
2105 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs
->gen
);
2109 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2111 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2113 ipvs
->enable
= 0; /* Disable packet reception */
2115 ip_vs_sync_net_cleanup(ipvs
);
2119 static struct pernet_operations ipvs_core_ops
= {
2120 .init
= __ip_vs_init
,
2121 .exit
= __ip_vs_cleanup
,
2122 .id
= &ip_vs_net_id
,
2123 .size
= sizeof(struct netns_ipvs
),
2126 static struct pernet_operations ipvs_core_dev_ops
= {
2127 .exit
= __ip_vs_dev_cleanup
,
2131 * Initialize IP Virtual Server
2133 static int __init
ip_vs_init(void)
2137 ret
= ip_vs_control_init();
2139 pr_err("can't setup control.\n");
2143 ip_vs_protocol_init();
2145 ret
= ip_vs_conn_init();
2147 pr_err("can't setup connection table.\n");
2148 goto cleanup_protocol
;
2151 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2155 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2159 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2161 pr_err("can't register hooks.\n");
2165 ret
= ip_vs_register_nl_ioctl();
2167 pr_err("can't register netlink/ioctl.\n");
2171 pr_info("ipvs loaded.\n");
2176 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2178 unregister_pernet_device(&ipvs_core_dev_ops
);
2180 unregister_pernet_subsys(&ipvs_core_ops
);
2182 ip_vs_conn_cleanup();
2184 ip_vs_protocol_cleanup();
2185 ip_vs_control_cleanup();
2190 static void __exit
ip_vs_cleanup(void)
2192 ip_vs_unregister_nl_ioctl();
2193 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2194 unregister_pernet_device(&ipvs_core_dev_ops
);
2195 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2196 ip_vs_conn_cleanup();
2197 ip_vs_protocol_cleanup();
2198 ip_vs_control_cleanup();
2199 pr_info("ipvs unloaded.\n");
2202 module_init(ip_vs_init
);
2203 module_exit(ip_vs_cleanup
);
2204 MODULE_LICENSE("GPL");