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
= net_ipvs(skb_net(skb
));
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
= net_ipvs(skb_net(skb
));
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
->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
);
551 static inline int ip_vs_addr_is_unicast(struct net
*net
, int af
,
552 union nf_inet_addr
*addr
)
554 #ifdef CONFIG_IP_VS_IPV6
556 return ipv6_addr_type(&addr
->in6
) & IPV6_ADDR_UNICAST
;
558 return (inet_addr_type(net
, addr
->ip
) == RTN_UNICAST
);
563 * Pass or drop the packet.
564 * Called by ip_vs_in, when the virtual service is available but
565 * no destination is available for a new connection.
567 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
568 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
570 __be16 _ports
[2], *pptr
, dport
;
573 struct netns_ipvs
*ipvs
;
576 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
579 dport
= likely(!ip_vs_iph_inverse(iph
)) ? pptr
[1] : pptr
[0];
585 /* if it is fwmark-based service, the cache_bypass sysctl is up
586 and the destination is a non-local unicast, then create
587 a cache_bypass connection entry */
588 ipvs
= net_ipvs(net
);
589 if (ipvs
->sysctl_cache_bypass
&& svc
->fwmark
&&
590 !(iph
->hdr_flags
& (IP_VS_HDR_INVERSE
| IP_VS_HDR_ICMP
)) &&
591 ip_vs_addr_is_unicast(net
, svc
->af
, &iph
->daddr
)) {
593 struct ip_vs_conn
*cp
;
594 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
595 iph
->protocol
== IPPROTO_UDP
) ?
596 IP_VS_CONN_F_ONE_PACKET
: 0;
597 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
599 /* create a new connection entry */
600 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
602 struct ip_vs_conn_param p
;
603 ip_vs_conn_fill_param(svc
->ipvs
, svc
->af
, iph
->protocol
,
604 &iph
->saddr
, pptr
[0],
605 &iph
->daddr
, pptr
[1], &p
);
606 cp
= ip_vs_conn_new(&p
, svc
->af
, &daddr
, 0,
607 IP_VS_CONN_F_BYPASS
| flags
,
614 ip_vs_in_stats(cp
, skb
);
617 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
619 /* transmit the first SYN packet */
620 ret
= cp
->packet_xmit(skb
, cp
, pd
->pp
, iph
);
621 /* do not touch skb anymore */
623 atomic_inc(&cp
->in_pkts
);
630 * When the virtual ftp service is presented, packets destined
631 * for other services on the VIP may get here (except services
632 * listed in the ipvs table), pass the packets, because it is
633 * not ipvs job to decide to drop the packets.
635 if (svc
->port
== FTPPORT
&& dport
!= FTPPORT
)
638 if (unlikely(ip_vs_iph_icmp(iph
)))
642 * Notify the client that the destination is unreachable, and
643 * release the socket buffer.
644 * Since it is in IP layer, the TCP socket is not actually
645 * created, the TCP RST packet cannot be sent, instead that
646 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
648 #ifdef CONFIG_IP_VS_IPV6
649 if (svc
->af
== AF_INET6
) {
651 struct net
*net_
= dev_net(skb_dst(skb
)->dev
);
653 skb
->dev
= net_
->loopback_dev
;
655 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
658 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
665 static int sysctl_snat_reroute(struct sk_buff
*skb
)
667 struct netns_ipvs
*ipvs
= net_ipvs(skb_net(skb
));
668 return ipvs
->sysctl_snat_reroute
;
671 static int sysctl_nat_icmp_send(struct net
*net
)
673 struct netns_ipvs
*ipvs
= net_ipvs(net
);
674 return ipvs
->sysctl_nat_icmp_send
;
677 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
679 return ipvs
->sysctl_expire_nodest_conn
;
684 static int sysctl_snat_reroute(struct sk_buff
*skb
) { return 0; }
685 static int sysctl_nat_icmp_send(struct net
*net
) { return 0; }
686 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
690 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
692 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
695 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
697 if (NF_INET_LOCAL_IN
== hooknum
)
698 return IP_DEFRAG_VS_IN
;
699 if (NF_INET_FORWARD
== hooknum
)
700 return IP_DEFRAG_VS_FWD
;
701 return IP_DEFRAG_VS_OUT
;
704 static inline int ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
709 err
= ip_defrag(skb
, user
);
712 ip_send_check(ip_hdr(skb
));
717 static int ip_vs_route_me_harder(int af
, struct sk_buff
*skb
,
718 unsigned int hooknum
)
720 if (!sysctl_snat_reroute(skb
))
722 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
723 if (NF_INET_LOCAL_IN
== hooknum
)
725 #ifdef CONFIG_IP_VS_IPV6
726 if (af
== AF_INET6
) {
727 struct dst_entry
*dst
= skb_dst(skb
);
729 if (dst
->dev
&& !(dst
->dev
->flags
& IFF_LOOPBACK
) &&
730 ip6_route_me_harder(skb
) != 0)
734 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
735 ip_route_me_harder(skb
, RTN_LOCAL
) != 0)
742 * Packet has been made sufficiently writable in caller
743 * - inout: 1=in->out, 0=out->in
745 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
746 struct ip_vs_conn
*cp
, int inout
)
748 struct iphdr
*iph
= ip_hdr(skb
);
749 unsigned int icmp_offset
= iph
->ihl
*4;
750 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
752 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
755 iph
->saddr
= cp
->vaddr
.ip
;
757 ciph
->daddr
= cp
->vaddr
.ip
;
760 iph
->daddr
= cp
->daddr
.ip
;
762 ciph
->saddr
= cp
->daddr
.ip
;
766 /* the TCP/UDP/SCTP port */
767 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
768 IPPROTO_SCTP
== ciph
->protocol
) {
769 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
772 ports
[1] = cp
->vport
;
774 ports
[0] = cp
->dport
;
777 /* And finally the ICMP checksum */
779 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
780 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
783 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
784 "Forwarding altered outgoing ICMP");
786 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
787 "Forwarding altered incoming ICMP");
790 #ifdef CONFIG_IP_VS_IPV6
791 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
792 struct ip_vs_conn
*cp
, int inout
)
794 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
795 unsigned int icmp_offset
= 0;
796 unsigned int offs
= 0; /* header offset*/
798 struct icmp6hdr
*icmph
;
799 struct ipv6hdr
*ciph
;
800 unsigned short fragoffs
;
802 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
803 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
804 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
805 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
807 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
810 iph
->saddr
= cp
->vaddr
.in6
;
811 ciph
->daddr
= cp
->vaddr
.in6
;
813 iph
->daddr
= cp
->daddr
.in6
;
814 ciph
->saddr
= cp
->daddr
.in6
;
817 /* the TCP/UDP/SCTP port */
818 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
819 IPPROTO_SCTP
== protocol
)) {
820 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
822 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
823 ntohs(inout
? ports
[1] : ports
[0]),
824 ntohs(inout
? cp
->vport
: cp
->dport
));
826 ports
[1] = cp
->vport
;
828 ports
[0] = cp
->dport
;
831 /* And finally the ICMP checksum */
832 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
833 skb
->len
- icmp_offset
,
835 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
836 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
837 skb
->ip_summed
= CHECKSUM_PARTIAL
;
840 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
841 (void *)ciph
- (void *)iph
,
842 "Forwarding altered outgoing ICMPv6");
844 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
845 (void *)ciph
- (void *)iph
,
846 "Forwarding altered incoming ICMPv6");
850 /* Handle relevant response ICMP messages - forward to the right
853 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
854 union nf_inet_addr
*snet
,
855 __u8 protocol
, struct ip_vs_conn
*cp
,
856 struct ip_vs_protocol
*pp
,
857 unsigned int offset
, unsigned int ihl
,
858 unsigned int hooknum
)
860 unsigned int verdict
= NF_DROP
;
862 if (IP_VS_FWD_METHOD(cp
) != 0) {
863 pr_err("shouldn't reach here, because the box is on the "
864 "half connection in the tun/dr module.\n");
867 /* Ensure the checksum is correct */
868 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
869 /* Failed checksum! */
870 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
871 IP_VS_DBG_ADDR(af
, snet
));
875 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
876 IPPROTO_SCTP
== protocol
)
877 offset
+= 2 * sizeof(__u16
);
878 if (!skb_make_writable(skb
, offset
))
881 #ifdef CONFIG_IP_VS_IPV6
883 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
886 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
888 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
891 /* do the statistics and put it back */
892 ip_vs_out_stats(cp
, skb
);
894 skb
->ipvs_property
= 1;
895 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
898 ip_vs_update_conntrack(skb
, cp
, 0);
902 __ip_vs_conn_put(cp
);
908 * Handle ICMP messages in the inside-to-outside direction (outgoing).
909 * Find any that might be relevant, check against existing connections.
910 * Currently handles error types - unreachable, quench, ttl exceeded.
912 static int ip_vs_out_icmp(struct sk_buff
*skb
, int *related
,
913 unsigned int hooknum
)
916 struct icmphdr _icmph
, *ic
;
917 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
918 struct ip_vs_iphdr ciph
;
919 struct ip_vs_conn
*cp
;
920 struct ip_vs_protocol
*pp
;
921 unsigned int offset
, ihl
;
922 union nf_inet_addr snet
;
926 /* reassemble IP fragments */
927 if (ip_is_fragment(ip_hdr(skb
))) {
928 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
933 offset
= ihl
= iph
->ihl
* 4;
934 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
938 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
939 ic
->type
, ntohs(icmp_id(ic
)),
940 &iph
->saddr
, &iph
->daddr
);
943 * Work through seeing if this is for us.
944 * These checks are supposed to be in an order that means easy
945 * things are checked first to speed up processing.... however
946 * this means that some packets will manage to get a long way
947 * down this stack and then be rejected, but that's life.
949 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
950 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
951 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
956 /* Now find the contained IP header */
957 offset
+= sizeof(_icmph
);
958 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
960 return NF_ACCEPT
; /* The packet looks wrong, ignore */
962 pp
= ip_vs_proto_get(cih
->protocol
);
966 /* Is the embedded protocol header present? */
967 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
971 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
972 "Checking outgoing ICMP for");
974 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, true, &ciph
);
976 /* The embedded headers contain source and dest in reverse order */
977 cp
= pp
->conn_out_get(AF_INET
, skb
, &ciph
);
981 snet
.ip
= iph
->saddr
;
982 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
983 pp
, ciph
.len
, ihl
, hooknum
);
986 #ifdef CONFIG_IP_VS_IPV6
987 static int ip_vs_out_icmp_v6(struct sk_buff
*skb
, int *related
,
988 unsigned int hooknum
, struct ip_vs_iphdr
*ipvsh
)
990 struct icmp6hdr _icmph
, *ic
;
991 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
992 struct ip_vs_conn
*cp
;
993 struct ip_vs_protocol
*pp
;
994 union nf_inet_addr snet
;
998 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
, ipvsh
);
1003 * Work through seeing if this is for us.
1004 * These checks are supposed to be in an order that means easy
1005 * things are checked first to speed up processing.... however
1006 * this means that some packets will manage to get a long way
1007 * down this stack and then be rejected, but that's life.
1009 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1013 /* Fragment header that is before ICMP header tells us that:
1014 * it's not an error message since they can't be fragmented.
1016 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
1019 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1020 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1021 &ipvsh
->saddr
, &ipvsh
->daddr
);
1023 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, ipvsh
->len
+ sizeof(_icmph
),
1025 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1027 pp
= ip_vs_proto_get(ciph
.protocol
);
1031 /* The embedded headers contain source and dest in reverse order */
1032 cp
= pp
->conn_out_get(AF_INET6
, skb
, &ciph
);
1036 snet
.in6
= ciph
.saddr
.in6
;
1038 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
1039 pp
, offset
, sizeof(struct ipv6hdr
),
1045 * Check if sctp chunc is ABORT chunk
1047 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
1049 sctp_chunkhdr_t
*sch
, schunk
;
1050 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
1051 sizeof(schunk
), &schunk
);
1054 if (sch
->type
== SCTP_CID_ABORT
)
1059 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
1061 struct tcphdr _tcph
, *th
;
1063 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1069 static inline bool is_new_conn(const struct sk_buff
*skb
,
1070 struct ip_vs_iphdr
*iph
)
1072 switch (iph
->protocol
) {
1074 struct tcphdr _tcph
, *th
;
1076 th
= skb_header_pointer(skb
, iph
->len
, sizeof(_tcph
), &_tcph
);
1081 case IPPROTO_SCTP
: {
1082 sctp_chunkhdr_t
*sch
, schunk
;
1084 sch
= skb_header_pointer(skb
, iph
->len
+ sizeof(sctp_sctphdr_t
),
1085 sizeof(schunk
), &schunk
);
1088 return sch
->type
== SCTP_CID_INIT
;
1095 static inline bool is_new_conn_expected(const struct ip_vs_conn
*cp
,
1096 int conn_reuse_mode
)
1098 /* Controlled (FTP DATA or persistence)? */
1102 switch (cp
->protocol
) {
1104 return (cp
->state
== IP_VS_TCP_S_TIME_WAIT
) ||
1105 ((conn_reuse_mode
& 2) &&
1106 (cp
->state
== IP_VS_TCP_S_FIN_WAIT
) &&
1107 (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
));
1109 return cp
->state
== IP_VS_SCTP_S_CLOSED
;
1115 /* Handle response packets: rewrite addresses and send away...
1118 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1119 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
,
1120 unsigned int hooknum
)
1122 struct ip_vs_protocol
*pp
= pd
->pp
;
1124 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
->off
, "Outgoing packet");
1126 if (!skb_make_writable(skb
, iph
->len
))
1129 /* mangle the packet */
1130 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
, iph
))
1133 #ifdef CONFIG_IP_VS_IPV6
1135 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1139 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1140 ip_send_check(ip_hdr(skb
));
1144 * nf_iterate does not expect change in the skb->dst->dev.
1145 * It looks like it is not fatal to enable this code for hooks
1146 * where our handlers are at the end of the chain list and
1147 * when all next handlers use skb->dst->dev and not outdev.
1148 * It will definitely route properly the inout NAT traffic
1149 * when multiple paths are used.
1152 /* For policy routing, packets originating from this
1153 * machine itself may be routed differently to packets
1154 * passing through. We want this packet to be routed as
1155 * if it came from this machine itself. So re-compute
1156 * the routing information.
1158 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
1161 IP_VS_DBG_PKT(10, af
, pp
, skb
, iph
->off
, "After SNAT");
1163 ip_vs_out_stats(cp
, skb
);
1164 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1165 skb
->ipvs_property
= 1;
1166 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1169 ip_vs_update_conntrack(skb
, cp
, 0);
1183 * Check if outgoing packet belongs to the established ip_vs_conn.
1186 ip_vs_out(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1188 struct net
*net
= NULL
;
1189 struct netns_ipvs
*ipvs
;
1190 struct ip_vs_iphdr iph
;
1191 struct ip_vs_protocol
*pp
;
1192 struct ip_vs_proto_data
*pd
;
1193 struct ip_vs_conn
*cp
;
1197 /* Already marked as IPVS request or reply? */
1198 if (skb
->ipvs_property
)
1201 /* Bad... Do not break raw sockets */
1202 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1204 struct sock
*sk
= skb
->sk
;
1205 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1207 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1211 if (unlikely(!skb_dst(skb
)))
1215 ipvs
= net_ipvs(net
);
1219 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1220 #ifdef CONFIG_IP_VS_IPV6
1221 if (af
== AF_INET6
) {
1222 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1224 int verdict
= ip_vs_out_icmp_v6(skb
, &related
,
1232 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1234 int verdict
= ip_vs_out_icmp(skb
, &related
, hooknum
);
1240 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1245 /* reassemble IP fragments */
1246 #ifdef CONFIG_IP_VS_IPV6
1249 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1250 if (ip_vs_gather_frags(skb
,
1251 ip_vs_defrag_user(hooknum
)))
1254 ip_vs_fill_iph_skb(AF_INET
, skb
, false, &iph
);
1258 * Check if the packet belongs to an existing entry
1260 cp
= pp
->conn_out_get(af
, skb
, &iph
);
1263 return handle_response(af
, skb
, pd
, cp
, &iph
, hooknum
);
1264 if (sysctl_nat_icmp_send(net
) &&
1265 (pp
->protocol
== IPPROTO_TCP
||
1266 pp
->protocol
== IPPROTO_UDP
||
1267 pp
->protocol
== IPPROTO_SCTP
)) {
1268 __be16 _ports
[2], *pptr
;
1270 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1271 sizeof(_ports
), _ports
, &iph
);
1273 return NF_ACCEPT
; /* Not for me */
1274 if (ip_vs_has_real_service(ipvs
, af
, iph
.protocol
, &iph
.saddr
,
1277 * Notify the real server: there is no
1278 * existing entry if it is not RST
1279 * packet or not TCP packet.
1281 if ((iph
.protocol
!= IPPROTO_TCP
&&
1282 iph
.protocol
!= IPPROTO_SCTP
)
1283 || ((iph
.protocol
== IPPROTO_TCP
1284 && !is_tcp_reset(skb
, iph
.len
))
1285 || (iph
.protocol
== IPPROTO_SCTP
1286 && !is_sctp_abort(skb
,
1288 #ifdef CONFIG_IP_VS_IPV6
1289 if (af
== AF_INET6
) {
1291 skb
->dev
= net
->loopback_dev
;
1293 ICMPV6_DEST_UNREACH
,
1294 ICMPV6_PORT_UNREACH
,
1300 ICMP_PORT_UNREACH
, 0);
1305 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
.off
,
1306 "ip_vs_out: packet continues traversal as normal");
1311 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1312 * used only for VS/NAT.
1313 * Check if packet is reply for established ip_vs_conn.
1316 ip_vs_reply4(void *priv
, struct sk_buff
*skb
,
1317 const struct nf_hook_state
*state
)
1319 return ip_vs_out(state
->hook
, skb
, AF_INET
);
1323 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1324 * Check if packet is reply for established ip_vs_conn.
1327 ip_vs_local_reply4(void *priv
, struct sk_buff
*skb
,
1328 const struct nf_hook_state
*state
)
1330 return ip_vs_out(state
->hook
, skb
, AF_INET
);
1333 #ifdef CONFIG_IP_VS_IPV6
1336 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1337 * used only for VS/NAT.
1338 * Check if packet is reply for established ip_vs_conn.
1341 ip_vs_reply6(void *priv
, struct sk_buff
*skb
,
1342 const struct nf_hook_state
*state
)
1344 return ip_vs_out(state
->hook
, skb
, AF_INET6
);
1348 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1349 * Check if packet is reply for established ip_vs_conn.
1352 ip_vs_local_reply6(void *priv
, struct sk_buff
*skb
,
1353 const struct nf_hook_state
*state
)
1355 return ip_vs_out(state
->hook
, skb
, AF_INET6
);
1361 ip_vs_try_to_schedule(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1362 int *verdict
, struct ip_vs_conn
**cpp
,
1363 struct ip_vs_iphdr
*iph
)
1365 struct ip_vs_protocol
*pp
= pd
->pp
;
1367 if (!iph
->fragoffs
) {
1368 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1369 * replayed fragment zero will already have created the cp
1372 /* Schedule and create new connection entry into cpp */
1373 if (!pp
->conn_schedule(af
, skb
, pd
, verdict
, cpp
, iph
))
1377 if (unlikely(!*cpp
)) {
1378 /* sorry, all this trouble for a no-hit :) */
1379 IP_VS_DBG_PKT(12, af
, pp
, skb
, iph
->off
,
1380 "ip_vs_in: packet continues traversal as normal");
1381 if (iph
->fragoffs
) {
1382 /* Fragment that couldn't be mapped to a conn entry
1383 * is missing module nf_defrag_ipv6
1385 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1386 IP_VS_DBG_PKT(7, af
, pp
, skb
, iph
->off
,
1387 "unhandled fragment");
1389 *verdict
= NF_ACCEPT
;
1397 * Handle ICMP messages in the outside-to-inside direction (incoming).
1398 * Find any that might be relevant, check against existing connections,
1399 * forward to the right destination host if relevant.
1400 * Currently handles error types - unreachable, quench, ttl exceeded.
1403 ip_vs_in_icmp(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1405 struct net
*net
= NULL
;
1406 struct netns_ipvs
*ipvs
;
1408 struct icmphdr _icmph
, *ic
;
1409 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1410 struct ip_vs_iphdr ciph
;
1411 struct ip_vs_conn
*cp
;
1412 struct ip_vs_protocol
*pp
;
1413 struct ip_vs_proto_data
*pd
;
1414 unsigned int offset
, offset2
, ihl
, verdict
;
1415 bool ipip
, new_cp
= false;
1419 /* reassemble IP fragments */
1420 if (ip_is_fragment(ip_hdr(skb
))) {
1421 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
1426 offset
= ihl
= iph
->ihl
* 4;
1427 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1431 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1432 ic
->type
, ntohs(icmp_id(ic
)),
1433 &iph
->saddr
, &iph
->daddr
);
1436 * Work through seeing if this is for us.
1437 * These checks are supposed to be in an order that means easy
1438 * things are checked first to speed up processing.... however
1439 * this means that some packets will manage to get a long way
1440 * down this stack and then be rejected, but that's life.
1442 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1443 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1444 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1449 /* Now find the contained IP header */
1450 offset
+= sizeof(_icmph
);
1451 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1453 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1456 ipvs
= net_ipvs(net
);
1458 /* Special case for errors for IPIP packets */
1460 if (cih
->protocol
== IPPROTO_IPIP
) {
1461 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1463 /* Error for our IPIP must arrive at LOCAL_IN */
1464 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1466 offset
+= cih
->ihl
* 4;
1467 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1469 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1473 pd
= ip_vs_proto_data_get(ipvs
, cih
->protocol
);
1478 /* Is the embedded protocol header present? */
1479 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1483 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1484 "Checking incoming ICMP for");
1487 ip_vs_fill_iph_skb_icmp(AF_INET
, skb
, offset
, !ipip
, &ciph
);
1490 /* The embedded headers contain source and dest in reverse order.
1491 * For IPIP this is error for request, not for reply.
1493 cp
= pp
->conn_in_get(AF_INET
, skb
, &ciph
);
1498 if (!sysctl_schedule_icmp(ipvs
))
1501 if (!ip_vs_try_to_schedule(AF_INET
, skb
, pd
, &v
, &cp
, &ciph
))
1508 /* Ensure the checksum is correct */
1509 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1510 /* Failed checksum! */
1511 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1517 __be32 info
= ic
->un
.gateway
;
1518 __u8 type
= ic
->type
;
1519 __u8 code
= ic
->code
;
1521 /* Update the MTU */
1522 if (ic
->type
== ICMP_DEST_UNREACH
&&
1523 ic
->code
== ICMP_FRAG_NEEDED
) {
1524 struct ip_vs_dest
*dest
= cp
->dest
;
1525 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1526 __be16 frag_off
= cih
->frag_off
;
1528 /* Strip outer IP and ICMP, go to IPIP header */
1529 if (pskb_pull(skb
, ihl
+ sizeof(_icmph
)) == NULL
)
1531 offset2
-= ihl
+ sizeof(_icmph
);
1532 skb_reset_network_header(skb
);
1533 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1534 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1535 ipv4_update_pmtu(skb
, dev_net(skb
->dev
),
1537 /* Client uses PMTUD? */
1538 if (!(frag_off
& htons(IP_DF
)))
1540 /* Prefer the resulting PMTU */
1542 struct ip_vs_dest_dst
*dest_dst
;
1545 dest_dst
= rcu_dereference(dest
->dest_dst
);
1547 mtu
= dst_mtu(dest_dst
->dst_cache
);
1550 if (mtu
> 68 + sizeof(struct iphdr
))
1551 mtu
-= sizeof(struct iphdr
);
1554 /* Strip outer IP, ICMP and IPIP, go to IP header of
1557 if (pskb_pull(skb
, offset2
) == NULL
)
1559 skb_reset_network_header(skb
);
1560 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1561 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1562 type
, code
, ntohl(info
));
1563 icmp_send(skb
, type
, code
, info
);
1564 /* ICMP can be shorter but anyways, account it */
1565 ip_vs_out_stats(cp
, skb
);
1569 verdict
= NF_STOLEN
;
1573 /* do the statistics and put it back */
1574 ip_vs_in_stats(cp
, skb
);
1575 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
||
1576 IPPROTO_SCTP
== cih
->protocol
)
1577 offset
+= 2 * sizeof(__u16
);
1578 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1581 if (likely(!new_cp
))
1582 __ip_vs_conn_put(cp
);
1589 #ifdef CONFIG_IP_VS_IPV6
1590 static int ip_vs_in_icmp_v6(struct sk_buff
*skb
, int *related
,
1591 unsigned int hooknum
, struct ip_vs_iphdr
*iph
)
1593 struct net
*net
= NULL
;
1594 struct netns_ipvs
*ipvs
;
1595 struct icmp6hdr _icmph
, *ic
;
1596 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1597 struct ip_vs_conn
*cp
;
1598 struct ip_vs_protocol
*pp
;
1599 struct ip_vs_proto_data
*pd
;
1600 unsigned int offset
, verdict
;
1601 bool new_cp
= false;
1605 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
, iph
);
1610 * Work through seeing if this is for us.
1611 * These checks are supposed to be in an order that means easy
1612 * things are checked first to speed up processing.... however
1613 * this means that some packets will manage to get a long way
1614 * down this stack and then be rejected, but that's life.
1616 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1620 /* Fragment header that is before ICMP header tells us that:
1621 * it's not an error message since they can't be fragmented.
1623 if (iph
->flags
& IP6_FH_F_FRAG
)
1626 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1627 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1628 &iph
->saddr
, &iph
->daddr
);
1630 offset
= iph
->len
+ sizeof(_icmph
);
1631 if (!ip_vs_fill_iph_skb_icmp(AF_INET6
, skb
, offset
, true, &ciph
))
1635 ipvs
= net_ipvs(net
);
1636 pd
= ip_vs_proto_data_get(ipvs
, ciph
.protocol
);
1641 /* Cannot handle fragmented embedded protocol */
1645 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offset
,
1646 "Checking incoming ICMPv6 for");
1648 /* The embedded headers contain source and dest in reverse order
1649 * if not from localhost
1651 cp
= pp
->conn_in_get(AF_INET6
, skb
, &ciph
);
1656 if (!sysctl_schedule_icmp(ipvs
))
1659 if (!ip_vs_try_to_schedule(AF_INET6
, skb
, pd
, &v
, &cp
, &ciph
))
1665 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1666 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1667 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1668 verdict
= NF_ACCEPT
;
1672 /* do the statistics and put it back */
1673 ip_vs_in_stats(cp
, skb
);
1675 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1677 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1678 IPPROTO_SCTP
== ciph
.protocol
)
1679 offset
+= 2 * sizeof(__u16
); /* Also mangle ports */
1681 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1684 if (likely(!new_cp
))
1685 __ip_vs_conn_put(cp
);
1695 * Check if it's for virtual services, look it up,
1696 * and send it on its way...
1699 ip_vs_in(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1702 struct ip_vs_iphdr iph
;
1703 struct ip_vs_protocol
*pp
;
1704 struct ip_vs_proto_data
*pd
;
1705 struct ip_vs_conn
*cp
;
1707 struct netns_ipvs
*ipvs
;
1708 int conn_reuse_mode
;
1710 /* Already marked as IPVS request or reply? */
1711 if (skb
->ipvs_property
)
1716 * - remote client: only PACKET_HOST
1717 * - route: used for struct net when skb->dev is unset
1719 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1720 hooknum
!= NF_INET_LOCAL_OUT
) ||
1722 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1723 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1724 " ignored in hook %u\n",
1725 skb
->pkt_type
, iph
.protocol
,
1726 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1729 /* ipvs enabled in this netns ? */
1731 ipvs
= net_ipvs(net
);
1732 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1735 ip_vs_fill_iph_skb(af
, skb
, false, &iph
);
1737 /* Bad... Do not break raw sockets */
1738 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1740 struct sock
*sk
= skb
->sk
;
1741 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1743 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1747 #ifdef CONFIG_IP_VS_IPV6
1748 if (af
== AF_INET6
) {
1749 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1751 int verdict
= ip_vs_in_icmp_v6(skb
, &related
, hooknum
,
1759 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1761 int verdict
= ip_vs_in_icmp(skb
, &related
, hooknum
);
1767 /* Protocol supported? */
1768 pd
= ip_vs_proto_data_get(ipvs
, iph
.protocol
);
1769 if (unlikely(!pd
)) {
1770 /* The only way we'll see this packet again is if it's
1771 * encapsulated, so mark it with ipvs_property=1 so we
1772 * skip it if we're ignoring tunneled packets
1774 if (sysctl_ignore_tunneled(ipvs
))
1775 skb
->ipvs_property
= 1;
1781 * Check if the packet belongs to an existing connection entry
1783 cp
= pp
->conn_in_get(af
, skb
, &iph
);
1785 conn_reuse_mode
= sysctl_conn_reuse_mode(ipvs
);
1786 if (conn_reuse_mode
&& !iph
.fragoffs
&&
1787 is_new_conn(skb
, &iph
) && cp
&&
1788 ((unlikely(sysctl_expire_nodest_conn(ipvs
)) && cp
->dest
&&
1789 unlikely(!atomic_read(&cp
->dest
->weight
))) ||
1790 unlikely(is_new_conn_expected(cp
, conn_reuse_mode
)))) {
1791 if (!atomic_read(&cp
->n_control
))
1792 ip_vs_conn_expire_now(cp
);
1793 __ip_vs_conn_put(cp
);
1797 if (unlikely(!cp
)) {
1800 if (!ip_vs_try_to_schedule(af
, skb
, pd
, &v
, &cp
, &iph
))
1804 IP_VS_DBG_PKT(11, af
, pp
, skb
, iph
.off
, "Incoming packet");
1806 /* Check the server status */
1807 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1808 /* the destination server is not available */
1810 if (sysctl_expire_nodest_conn(ipvs
)) {
1811 /* try to expire the connection immediately */
1812 ip_vs_conn_expire_now(cp
);
1814 /* don't restart its timer, and silently
1816 __ip_vs_conn_put(cp
);
1820 ip_vs_in_stats(cp
, skb
);
1821 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
1822 if (cp
->packet_xmit
)
1823 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
1824 /* do not touch skb anymore */
1826 IP_VS_DBG_RL("warning: packet_xmit is null");
1830 /* Increase its packet counter and check if it is needed
1831 * to be synchronized
1833 * Sync connection if it is about to close to
1834 * encorage the standby servers to update the connections timeout
1836 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1839 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
1840 pkts
= sysctl_sync_threshold(ipvs
);
1842 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1844 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
1845 ip_vs_sync_conn(ipvs
, cp
, pkts
);
1852 * AF_INET handler in NF_INET_LOCAL_IN chain
1853 * Schedule and forward packets from remote clients
1856 ip_vs_remote_request4(void *priv
, struct sk_buff
*skb
,
1857 const struct nf_hook_state
*state
)
1859 return ip_vs_in(state
->hook
, skb
, AF_INET
);
1863 * AF_INET handler in NF_INET_LOCAL_OUT chain
1864 * Schedule and forward packets from local clients
1867 ip_vs_local_request4(void *priv
, struct sk_buff
*skb
,
1868 const struct nf_hook_state
*state
)
1870 return ip_vs_in(state
->hook
, skb
, AF_INET
);
1873 #ifdef CONFIG_IP_VS_IPV6
1876 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1877 * Schedule and forward packets from remote clients
1880 ip_vs_remote_request6(void *priv
, struct sk_buff
*skb
,
1881 const struct nf_hook_state
*state
)
1883 return ip_vs_in(state
->hook
, skb
, AF_INET6
);
1887 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1888 * Schedule and forward packets from local clients
1891 ip_vs_local_request6(void *priv
, struct sk_buff
*skb
,
1892 const struct nf_hook_state
*state
)
1894 return ip_vs_in(state
->hook
, skb
, AF_INET6
);
1901 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1902 * related packets destined for 0.0.0.0/0.
1903 * When fwmark-based virtual service is used, such as transparent
1904 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1905 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1906 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1907 * and send them to ip_vs_in_icmp.
1910 ip_vs_forward_icmp(void *priv
, struct sk_buff
*skb
,
1911 const struct nf_hook_state
*state
)
1914 struct netns_ipvs
*ipvs
;
1916 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1919 /* ipvs enabled in this netns ? */
1920 ipvs
= net_ipvs(state
->net
);
1921 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1924 return ip_vs_in_icmp(skb
, &r
, state
->hook
);
1927 #ifdef CONFIG_IP_VS_IPV6
1929 ip_vs_forward_icmp_v6(void *priv
, struct sk_buff
*skb
,
1930 const struct nf_hook_state
*state
)
1933 struct netns_ipvs
*ipvs
;
1934 struct ip_vs_iphdr iphdr
;
1936 ip_vs_fill_iph_skb(AF_INET6
, skb
, false, &iphdr
);
1937 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
1940 /* ipvs enabled in this netns ? */
1941 ipvs
= net_ipvs(state
->net
);
1942 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1945 return ip_vs_in_icmp_v6(skb
, &r
, state
->hook
, &iphdr
);
1950 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1951 /* After packet filtering, change source only for VS/NAT */
1953 .hook
= ip_vs_reply4
,
1954 .owner
= THIS_MODULE
,
1956 .hooknum
= NF_INET_LOCAL_IN
,
1957 .priority
= NF_IP_PRI_NAT_SRC
- 2,
1959 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1960 * or VS/NAT(change destination), so that filtering rules can be
1961 * applied to IPVS. */
1963 .hook
= ip_vs_remote_request4
,
1964 .owner
= THIS_MODULE
,
1966 .hooknum
= NF_INET_LOCAL_IN
,
1967 .priority
= NF_IP_PRI_NAT_SRC
- 1,
1969 /* Before ip_vs_in, change source only for VS/NAT */
1971 .hook
= ip_vs_local_reply4
,
1972 .owner
= THIS_MODULE
,
1974 .hooknum
= NF_INET_LOCAL_OUT
,
1975 .priority
= NF_IP_PRI_NAT_DST
+ 1,
1977 /* After mangle, schedule and forward local requests */
1979 .hook
= ip_vs_local_request4
,
1980 .owner
= THIS_MODULE
,
1982 .hooknum
= NF_INET_LOCAL_OUT
,
1983 .priority
= NF_IP_PRI_NAT_DST
+ 2,
1985 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1986 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1988 .hook
= ip_vs_forward_icmp
,
1989 .owner
= THIS_MODULE
,
1991 .hooknum
= NF_INET_FORWARD
,
1994 /* After packet filtering, change source only for VS/NAT */
1996 .hook
= ip_vs_reply4
,
1997 .owner
= THIS_MODULE
,
1999 .hooknum
= NF_INET_FORWARD
,
2002 #ifdef CONFIG_IP_VS_IPV6
2003 /* After packet filtering, change source only for VS/NAT */
2005 .hook
= ip_vs_reply6
,
2006 .owner
= THIS_MODULE
,
2008 .hooknum
= NF_INET_LOCAL_IN
,
2009 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
2011 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2012 * or VS/NAT(change destination), so that filtering rules can be
2013 * applied to IPVS. */
2015 .hook
= ip_vs_remote_request6
,
2016 .owner
= THIS_MODULE
,
2018 .hooknum
= NF_INET_LOCAL_IN
,
2019 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
2021 /* Before ip_vs_in, change source only for VS/NAT */
2023 .hook
= ip_vs_local_reply6
,
2024 .owner
= THIS_MODULE
,
2026 .hooknum
= NF_INET_LOCAL_OUT
,
2027 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
2029 /* After mangle, schedule and forward local requests */
2031 .hook
= ip_vs_local_request6
,
2032 .owner
= THIS_MODULE
,
2034 .hooknum
= NF_INET_LOCAL_OUT
,
2035 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
2037 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2038 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2040 .hook
= ip_vs_forward_icmp_v6
,
2041 .owner
= THIS_MODULE
,
2043 .hooknum
= NF_INET_FORWARD
,
2046 /* After packet filtering, change source only for VS/NAT */
2048 .hook
= ip_vs_reply6
,
2049 .owner
= THIS_MODULE
,
2051 .hooknum
= NF_INET_FORWARD
,
2057 * Initialize IP Virtual Server netns mem.
2059 static int __net_init
__ip_vs_init(struct net
*net
)
2061 struct netns_ipvs
*ipvs
;
2063 ipvs
= net_generic(net
, ip_vs_net_id
);
2067 /* Hold the beast until a service is registerd */
2070 /* Counters used for creating unique names */
2071 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
2072 atomic_inc(&ipvs_netns_cnt
);
2075 if (ip_vs_estimator_net_init(ipvs
) < 0)
2076 goto estimator_fail
;
2078 if (ip_vs_control_net_init(ipvs
) < 0)
2081 if (ip_vs_protocol_net_init(net
) < 0)
2084 if (ip_vs_app_net_init(net
) < 0)
2087 if (ip_vs_conn_net_init(net
) < 0)
2090 if (ip_vs_sync_net_init(ipvs
) < 0)
2093 printk(KERN_INFO
"IPVS: Creating netns size=%zu id=%d\n",
2094 sizeof(struct netns_ipvs
), ipvs
->gen
);
2101 ip_vs_conn_net_cleanup(net
);
2103 ip_vs_app_net_cleanup(net
);
2105 ip_vs_protocol_net_cleanup(net
);
2107 ip_vs_control_net_cleanup(ipvs
);
2109 ip_vs_estimator_net_cleanup(ipvs
);
2115 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2117 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2119 ip_vs_service_net_cleanup(ipvs
); /* ip_vs_flush() with locks */
2120 ip_vs_conn_net_cleanup(net
);
2121 ip_vs_app_net_cleanup(net
);
2122 ip_vs_protocol_net_cleanup(net
);
2123 ip_vs_control_net_cleanup(ipvs
);
2124 ip_vs_estimator_net_cleanup(ipvs
);
2125 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs
->gen
);
2129 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2131 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2133 ipvs
->enable
= 0; /* Disable packet reception */
2135 ip_vs_sync_net_cleanup(ipvs
);
2139 static struct pernet_operations ipvs_core_ops
= {
2140 .init
= __ip_vs_init
,
2141 .exit
= __ip_vs_cleanup
,
2142 .id
= &ip_vs_net_id
,
2143 .size
= sizeof(struct netns_ipvs
),
2146 static struct pernet_operations ipvs_core_dev_ops
= {
2147 .exit
= __ip_vs_dev_cleanup
,
2151 * Initialize IP Virtual Server
2153 static int __init
ip_vs_init(void)
2157 ret
= ip_vs_control_init();
2159 pr_err("can't setup control.\n");
2163 ip_vs_protocol_init();
2165 ret
= ip_vs_conn_init();
2167 pr_err("can't setup connection table.\n");
2168 goto cleanup_protocol
;
2171 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2175 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2179 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2181 pr_err("can't register hooks.\n");
2185 ret
= ip_vs_register_nl_ioctl();
2187 pr_err("can't register netlink/ioctl.\n");
2191 pr_info("ipvs loaded.\n");
2196 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2198 unregister_pernet_device(&ipvs_core_dev_ops
);
2200 unregister_pernet_subsys(&ipvs_core_ops
);
2202 ip_vs_conn_cleanup();
2204 ip_vs_protocol_cleanup();
2205 ip_vs_control_cleanup();
2210 static void __exit
ip_vs_cleanup(void)
2212 ip_vs_unregister_nl_ioctl();
2213 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2214 unregister_pernet_device(&ipvs_core_dev_ops
);
2215 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2216 ip_vs_conn_cleanup();
2217 ip_vs_protocol_cleanup();
2218 ip_vs_control_cleanup();
2219 pr_info("ipvs unloaded.\n");
2222 module_init(ip_vs_init
);
2223 module_exit(ip_vs_cleanup
);
2224 MODULE_LICENSE("GPL");