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
= net_ipvs(svc
->net
);
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
->net
, 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,
249 /* Mask saddr with the netmask to adjust template granularity */
250 #ifdef CONFIG_IP_VS_IPV6
251 if (svc
->af
== AF_INET6
)
252 ipv6_addr_prefix(&snet
.in6
, &iph
->saddr
.in6
,
253 (__force __u32
) svc
->netmask
);
256 snet
.ip
= iph
->saddr
.ip
& svc
->netmask
;
258 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
260 IP_VS_DBG_ADDR(svc
->af
, &iph
->saddr
), ntohs(src_port
),
261 IP_VS_DBG_ADDR(svc
->af
, &iph
->daddr
), ntohs(dst_port
),
262 IP_VS_DBG_ADDR(svc
->af
, &snet
));
265 * As far as we know, FTP is a very complicated network protocol, and
266 * it uses control connection and data connections. For active FTP,
267 * FTP server initialize data connection to the client, its source port
268 * is often 20. For passive FTP, FTP server tells the clients the port
269 * that it passively listens to, and the client issues the data
270 * connection. In the tunneling or direct routing mode, the load
271 * balancer is on the client-to-server half of connection, the port
272 * number is unknown to the load balancer. So, a conn template like
273 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
274 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
275 * is created for other persistent services.
278 int protocol
= iph
->protocol
;
279 const union nf_inet_addr
*vaddr
= &iph
->daddr
;
282 if (dst_port
== svc
->port
) {
284 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
286 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
288 if (svc
->port
!= FTPPORT
)
291 /* Note: persistent fwmark-based services and
292 * persistent port zero service are handled here.
294 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
295 * port zero template:
296 * <protocol,caddr,0,vaddr,0,daddr,0>
299 protocol
= IPPROTO_IP
;
303 /* return *ignored = -1 so NF_DROP can be used */
304 if (ip_vs_conn_fill_param_persist(svc
, skb
, protocol
, &snet
, 0,
305 vaddr
, vport
, ¶m
) < 0) {
311 /* Check if a template already exists */
312 ct
= ip_vs_ct_in_get(¶m
);
313 if (!ct
|| !ip_vs_check_template(ct
)) {
314 struct ip_vs_scheduler
*sched
;
317 * No template found or the dest of the connection
318 * template is not available.
319 * return *ignored=0 i.e. ICMP and NF_DROP
321 sched
= rcu_dereference(svc
->scheduler
);
323 /* read svc->sched_data after svc->scheduler */
325 dest
= sched
->schedule(svc
, skb
, iph
);
330 IP_VS_DBG(1, "p-schedule: no dest found.\n");
331 kfree(param
.pe_data
);
336 if (dst_port
== svc
->port
&& svc
->port
!= FTPPORT
)
340 * This adds param.pe_data to the template,
341 * and thus param.pe_data will be destroyed
342 * when the template expires */
343 ct
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
,
344 IP_VS_CONN_F_TEMPLATE
, dest
, skb
->mark
);
346 kfree(param
.pe_data
);
351 ct
->timeout
= svc
->timeout
;
353 /* set destination with the found template */
355 kfree(param
.pe_data
);
359 if (dport
== svc
->port
&& dest
->port
)
362 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
363 && iph
->protocol
== IPPROTO_UDP
) ?
364 IP_VS_CONN_F_ONE_PACKET
: 0;
367 * Create a new connection according to the template
369 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
, &iph
->saddr
,
370 src_port
, &iph
->daddr
, dst_port
, ¶m
);
372 cp
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
, flags
, dest
,
383 ip_vs_control_add(cp
, ct
);
386 ip_vs_conn_stats(cp
, svc
);
392 * IPVS main scheduling function
393 * It selects a server according to the virtual service, and
394 * creates a connection entry.
395 * Protocols supported: TCP, UDP
399 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
400 * svc/scheduler decides that this packet should be accepted with
401 * NF_ACCEPT because it must not be scheduled.
403 * 0 : scheduler can not find destination, so try bypass or
404 * return ICMP and then NF_DROP (ip_vs_leave).
406 * -1 : scheduler tried to schedule but fatal error occurred, eg.
407 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
408 * failure such as missing Call-ID, ENOMEM on skb_linearize
409 * or pe_data. In this case we should return NF_DROP without
410 * any attempts to send ICMP with ip_vs_leave.
413 ip_vs_schedule(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
414 struct ip_vs_proto_data
*pd
, int *ignored
,
415 struct ip_vs_iphdr
*iph
)
417 struct ip_vs_protocol
*pp
= pd
->pp
;
418 struct ip_vs_conn
*cp
= NULL
;
419 struct ip_vs_scheduler
*sched
;
420 struct ip_vs_dest
*dest
;
421 __be16 _ports
[2], *pptr
;
426 * IPv6 frags, only the first hit here.
428 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
433 * FTPDATA needs this check when using local real server.
434 * Never schedule Active FTPDATA connections from real server.
435 * For LVS-NAT they must be already created. For other methods
436 * with persistence the connection is created on SYN+ACK.
438 if (pptr
[0] == FTPDATA
) {
439 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
440 "Not scheduling FTPDATA");
445 * Do not schedule replies from local real server.
447 if ((!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
) &&
448 (cp
= pp
->conn_in_get(svc
->af
, skb
, iph
, 1))) {
449 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
450 "Not scheduling reply for existing connection");
451 __ip_vs_conn_put(cp
);
458 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
459 return ip_vs_sched_persist(svc
, skb
, pptr
[0], pptr
[1], ignored
,
465 * Non-persistent service
467 if (!svc
->fwmark
&& pptr
[1] != svc
->port
) {
469 pr_err("Schedule: port zero only supported "
470 "in persistent services, "
471 "check your ipvs configuration\n");
475 sched
= rcu_dereference(svc
->scheduler
);
477 /* read svc->sched_data after svc->scheduler */
479 dest
= sched
->schedule(svc
, skb
, iph
);
484 IP_VS_DBG(1, "Schedule: no dest found.\n");
488 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
489 && iph
->protocol
== IPPROTO_UDP
) ?
490 IP_VS_CONN_F_ONE_PACKET
: 0;
493 * Create a connection entry.
496 struct ip_vs_conn_param p
;
498 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
,
499 &iph
->saddr
, pptr
[0], &iph
->daddr
,
501 cp
= ip_vs_conn_new(&p
, dest
->af
, &dest
->addr
,
502 dest
->port
? dest
->port
: pptr
[1],
503 flags
, dest
, skb
->mark
);
510 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
511 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
513 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
514 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
515 IP_VS_DBG_ADDR(cp
->daf
, &cp
->daddr
), ntohs(cp
->dport
),
516 cp
->flags
, atomic_read(&cp
->refcnt
));
518 ip_vs_conn_stats(cp
, svc
);
524 * Pass or drop the packet.
525 * Called by ip_vs_in, when the virtual service is available but
526 * no destination is available for a new connection.
528 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
529 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
531 __be16 _ports
[2], *pptr
;
534 struct netns_ipvs
*ipvs
;
538 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
546 #ifdef CONFIG_IP_VS_IPV6
547 if (svc
->af
== AF_INET6
)
548 unicast
= ipv6_addr_type(&iph
->daddr
.in6
) & IPV6_ADDR_UNICAST
;
551 unicast
= (inet_addr_type(net
, iph
->daddr
.ip
) == RTN_UNICAST
);
553 /* if it is fwmark-based service, the cache_bypass sysctl is up
554 and the destination is a non-local unicast, then create
555 a cache_bypass connection entry */
556 ipvs
= net_ipvs(net
);
557 if (ipvs
->sysctl_cache_bypass
&& svc
->fwmark
&& unicast
) {
559 struct ip_vs_conn
*cp
;
560 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
561 iph
->protocol
== IPPROTO_UDP
) ?
562 IP_VS_CONN_F_ONE_PACKET
: 0;
563 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
565 /* create a new connection entry */
566 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
568 struct ip_vs_conn_param p
;
569 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
,
570 &iph
->saddr
, pptr
[0],
571 &iph
->daddr
, pptr
[1], &p
);
572 cp
= ip_vs_conn_new(&p
, svc
->af
, &daddr
, 0,
573 IP_VS_CONN_F_BYPASS
| flags
,
580 ip_vs_in_stats(cp
, skb
);
583 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
585 /* transmit the first SYN packet */
586 ret
= cp
->packet_xmit(skb
, cp
, pd
->pp
, iph
);
587 /* do not touch skb anymore */
589 atomic_inc(&cp
->in_pkts
);
596 * When the virtual ftp service is presented, packets destined
597 * for other services on the VIP may get here (except services
598 * listed in the ipvs table), pass the packets, because it is
599 * not ipvs job to decide to drop the packets.
601 if ((svc
->port
== FTPPORT
) && (pptr
[1] != FTPPORT
))
605 * Notify the client that the destination is unreachable, and
606 * release the socket buffer.
607 * Since it is in IP layer, the TCP socket is not actually
608 * created, the TCP RST packet cannot be sent, instead that
609 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
611 #ifdef CONFIG_IP_VS_IPV6
612 if (svc
->af
== AF_INET6
) {
614 struct net
*net_
= dev_net(skb_dst(skb
)->dev
);
616 skb
->dev
= net_
->loopback_dev
;
618 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
621 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
628 static int sysctl_snat_reroute(struct sk_buff
*skb
)
630 struct netns_ipvs
*ipvs
= net_ipvs(skb_net(skb
));
631 return ipvs
->sysctl_snat_reroute
;
634 static int sysctl_nat_icmp_send(struct net
*net
)
636 struct netns_ipvs
*ipvs
= net_ipvs(net
);
637 return ipvs
->sysctl_nat_icmp_send
;
640 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
642 return ipvs
->sysctl_expire_nodest_conn
;
647 static int sysctl_snat_reroute(struct sk_buff
*skb
) { return 0; }
648 static int sysctl_nat_icmp_send(struct net
*net
) { return 0; }
649 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
653 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
655 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
658 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
660 if (NF_INET_LOCAL_IN
== hooknum
)
661 return IP_DEFRAG_VS_IN
;
662 if (NF_INET_FORWARD
== hooknum
)
663 return IP_DEFRAG_VS_FWD
;
664 return IP_DEFRAG_VS_OUT
;
667 static inline int ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
672 err
= ip_defrag(skb
, user
);
675 ip_send_check(ip_hdr(skb
));
680 static int ip_vs_route_me_harder(int af
, struct sk_buff
*skb
,
681 unsigned int hooknum
)
683 if (!sysctl_snat_reroute(skb
))
685 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
686 if (NF_INET_LOCAL_IN
== hooknum
)
688 #ifdef CONFIG_IP_VS_IPV6
689 if (af
== AF_INET6
) {
690 struct dst_entry
*dst
= skb_dst(skb
);
692 if (dst
->dev
&& !(dst
->dev
->flags
& IFF_LOOPBACK
) &&
693 ip6_route_me_harder(skb
) != 0)
697 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
698 ip_route_me_harder(skb
, RTN_LOCAL
) != 0)
705 * Packet has been made sufficiently writable in caller
706 * - inout: 1=in->out, 0=out->in
708 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
709 struct ip_vs_conn
*cp
, int inout
)
711 struct iphdr
*iph
= ip_hdr(skb
);
712 unsigned int icmp_offset
= iph
->ihl
*4;
713 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
715 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
718 iph
->saddr
= cp
->vaddr
.ip
;
720 ciph
->daddr
= cp
->vaddr
.ip
;
723 iph
->daddr
= cp
->daddr
.ip
;
725 ciph
->saddr
= cp
->daddr
.ip
;
729 /* the TCP/UDP/SCTP port */
730 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
731 IPPROTO_SCTP
== ciph
->protocol
) {
732 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
735 ports
[1] = cp
->vport
;
737 ports
[0] = cp
->dport
;
740 /* And finally the ICMP checksum */
742 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
743 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
746 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
747 "Forwarding altered outgoing ICMP");
749 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
750 "Forwarding altered incoming ICMP");
753 #ifdef CONFIG_IP_VS_IPV6
754 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
755 struct ip_vs_conn
*cp
, int inout
)
757 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
758 unsigned int icmp_offset
= 0;
759 unsigned int offs
= 0; /* header offset*/
761 struct icmp6hdr
*icmph
;
762 struct ipv6hdr
*ciph
;
763 unsigned short fragoffs
;
765 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
766 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
767 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
768 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
770 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
773 iph
->saddr
= cp
->vaddr
.in6
;
774 ciph
->daddr
= cp
->vaddr
.in6
;
776 iph
->daddr
= cp
->daddr
.in6
;
777 ciph
->saddr
= cp
->daddr
.in6
;
780 /* the TCP/UDP/SCTP port */
781 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
782 IPPROTO_SCTP
== protocol
)) {
783 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
785 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
786 ntohs(inout
? ports
[1] : ports
[0]),
787 ntohs(inout
? cp
->vport
: cp
->dport
));
789 ports
[1] = cp
->vport
;
791 ports
[0] = cp
->dport
;
794 /* And finally the ICMP checksum */
795 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
796 skb
->len
- icmp_offset
,
798 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
799 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
800 skb
->ip_summed
= CHECKSUM_PARTIAL
;
803 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
804 (void *)ciph
- (void *)iph
,
805 "Forwarding altered outgoing ICMPv6");
807 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
808 (void *)ciph
- (void *)iph
,
809 "Forwarding altered incoming ICMPv6");
813 /* Handle relevant response ICMP messages - forward to the right
816 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
817 union nf_inet_addr
*snet
,
818 __u8 protocol
, struct ip_vs_conn
*cp
,
819 struct ip_vs_protocol
*pp
,
820 unsigned int offset
, unsigned int ihl
,
821 unsigned int hooknum
)
823 unsigned int verdict
= NF_DROP
;
825 if (IP_VS_FWD_METHOD(cp
) != 0) {
826 pr_err("shouldn't reach here, because the box is on the "
827 "half connection in the tun/dr module.\n");
830 /* Ensure the checksum is correct */
831 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
832 /* Failed checksum! */
833 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
834 IP_VS_DBG_ADDR(af
, snet
));
838 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
839 IPPROTO_SCTP
== protocol
)
840 offset
+= 2 * sizeof(__u16
);
841 if (!skb_make_writable(skb
, offset
))
844 #ifdef CONFIG_IP_VS_IPV6
846 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
849 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
851 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
854 /* do the statistics and put it back */
855 ip_vs_out_stats(cp
, skb
);
857 skb
->ipvs_property
= 1;
858 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
861 ip_vs_update_conntrack(skb
, cp
, 0);
865 __ip_vs_conn_put(cp
);
871 * Handle ICMP messages in the inside-to-outside direction (outgoing).
872 * Find any that might be relevant, check against existing connections.
873 * Currently handles error types - unreachable, quench, ttl exceeded.
875 static int ip_vs_out_icmp(struct sk_buff
*skb
, int *related
,
876 unsigned int hooknum
)
879 struct icmphdr _icmph
, *ic
;
880 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
881 struct ip_vs_iphdr ciph
;
882 struct ip_vs_conn
*cp
;
883 struct ip_vs_protocol
*pp
;
884 unsigned int offset
, ihl
;
885 union nf_inet_addr snet
;
889 /* reassemble IP fragments */
890 if (ip_is_fragment(ip_hdr(skb
))) {
891 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
896 offset
= ihl
= iph
->ihl
* 4;
897 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
901 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
902 ic
->type
, ntohs(icmp_id(ic
)),
903 &iph
->saddr
, &iph
->daddr
);
906 * Work through seeing if this is for us.
907 * These checks are supposed to be in an order that means easy
908 * things are checked first to speed up processing.... however
909 * this means that some packets will manage to get a long way
910 * down this stack and then be rejected, but that's life.
912 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
913 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
914 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
919 /* Now find the contained IP header */
920 offset
+= sizeof(_icmph
);
921 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
923 return NF_ACCEPT
; /* The packet looks wrong, ignore */
925 pp
= ip_vs_proto_get(cih
->protocol
);
929 /* Is the embedded protocol header present? */
930 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
934 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
935 "Checking outgoing ICMP for");
937 ip_vs_fill_ip4hdr(cih
, &ciph
);
939 /* The embedded headers contain source and dest in reverse order */
940 cp
= pp
->conn_out_get(AF_INET
, skb
, &ciph
, 1);
944 snet
.ip
= iph
->saddr
;
945 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
946 pp
, ciph
.len
, ihl
, hooknum
);
949 #ifdef CONFIG_IP_VS_IPV6
950 static int ip_vs_out_icmp_v6(struct sk_buff
*skb
, int *related
,
951 unsigned int hooknum
, struct ip_vs_iphdr
*ipvsh
)
953 struct icmp6hdr _icmph
, *ic
;
954 struct ipv6hdr _ip6h
, *ip6h
; /* The ip header contained within ICMP */
955 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
956 struct ip_vs_conn
*cp
;
957 struct ip_vs_protocol
*pp
;
958 union nf_inet_addr snet
;
959 unsigned int writable
;
962 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
, ipvsh
);
967 * Work through seeing if this is for us.
968 * These checks are supposed to be in an order that means easy
969 * things are checked first to speed up processing.... however
970 * this means that some packets will manage to get a long way
971 * down this stack and then be rejected, but that's life.
973 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
977 /* Fragment header that is before ICMP header tells us that:
978 * it's not an error message since they can't be fragmented.
980 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
983 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
984 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
985 &ipvsh
->saddr
, &ipvsh
->daddr
);
987 /* Now find the contained IP header */
988 ciph
.len
= ipvsh
->len
+ sizeof(_icmph
);
989 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
991 return NF_ACCEPT
; /* The packet looks wrong, ignore */
992 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_out_get() handles reverse order */
993 ciph
.daddr
.in6
= ip6h
->daddr
;
994 /* skip possible IPv6 exthdrs of contained IPv6 packet */
995 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
996 if (ciph
.protocol
< 0)
997 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
999 pp
= ip_vs_proto_get(ciph
.protocol
);
1003 /* The embedded headers contain source and dest in reverse order */
1004 cp
= pp
->conn_out_get(AF_INET6
, skb
, &ciph
, 1);
1008 snet
.in6
= ciph
.saddr
.in6
;
1009 writable
= ciph
.len
;
1010 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
1011 pp
, writable
, sizeof(struct ipv6hdr
),
1017 * Check if sctp chunc is ABORT chunk
1019 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
1021 sctp_chunkhdr_t
*sch
, schunk
;
1022 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
1023 sizeof(schunk
), &schunk
);
1026 if (sch
->type
== SCTP_CID_ABORT
)
1031 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
1033 struct tcphdr _tcph
, *th
;
1035 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1041 static inline bool is_new_conn(const struct sk_buff
*skb
,
1042 struct ip_vs_iphdr
*iph
)
1044 switch (iph
->protocol
) {
1046 struct tcphdr _tcph
, *th
;
1048 th
= skb_header_pointer(skb
, iph
->len
, sizeof(_tcph
), &_tcph
);
1053 case IPPROTO_SCTP
: {
1054 sctp_chunkhdr_t
*sch
, schunk
;
1056 sch
= skb_header_pointer(skb
, iph
->len
+ sizeof(sctp_sctphdr_t
),
1057 sizeof(schunk
), &schunk
);
1060 return sch
->type
== SCTP_CID_INIT
;
1067 static inline bool is_new_conn_expected(const struct ip_vs_conn
*cp
,
1068 int conn_reuse_mode
)
1070 /* Controlled (FTP DATA or persistence)? */
1074 switch (cp
->protocol
) {
1076 return (cp
->state
== IP_VS_TCP_S_TIME_WAIT
) ||
1077 ((conn_reuse_mode
& 2) &&
1078 (cp
->state
== IP_VS_TCP_S_FIN_WAIT
) &&
1079 (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
));
1081 return cp
->state
== IP_VS_SCTP_S_CLOSED
;
1087 /* Handle response packets: rewrite addresses and send away...
1090 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1091 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
,
1092 unsigned int hooknum
)
1094 struct ip_vs_protocol
*pp
= pd
->pp
;
1096 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Outgoing packet");
1098 if (!skb_make_writable(skb
, iph
->len
))
1101 /* mangle the packet */
1102 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
, iph
))
1105 #ifdef CONFIG_IP_VS_IPV6
1107 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1111 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1112 ip_send_check(ip_hdr(skb
));
1116 * nf_iterate does not expect change in the skb->dst->dev.
1117 * It looks like it is not fatal to enable this code for hooks
1118 * where our handlers are at the end of the chain list and
1119 * when all next handlers use skb->dst->dev and not outdev.
1120 * It will definitely route properly the inout NAT traffic
1121 * when multiple paths are used.
1124 /* For policy routing, packets originating from this
1125 * machine itself may be routed differently to packets
1126 * passing through. We want this packet to be routed as
1127 * if it came from this machine itself. So re-compute
1128 * the routing information.
1130 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
1133 IP_VS_DBG_PKT(10, af
, pp
, skb
, 0, "After SNAT");
1135 ip_vs_out_stats(cp
, skb
);
1136 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1137 skb
->ipvs_property
= 1;
1138 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1141 ip_vs_update_conntrack(skb
, cp
, 0);
1155 * Check if outgoing packet belongs to the established ip_vs_conn.
1158 ip_vs_out(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1160 struct net
*net
= NULL
;
1161 struct ip_vs_iphdr iph
;
1162 struct ip_vs_protocol
*pp
;
1163 struct ip_vs_proto_data
*pd
;
1164 struct ip_vs_conn
*cp
;
1168 /* Already marked as IPVS request or reply? */
1169 if (skb
->ipvs_property
)
1172 /* Bad... Do not break raw sockets */
1173 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1175 struct sock
*sk
= skb
->sk
;
1176 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1178 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1182 if (unlikely(!skb_dst(skb
)))
1186 if (!net_ipvs(net
)->enable
)
1189 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1190 #ifdef CONFIG_IP_VS_IPV6
1191 if (af
== AF_INET6
) {
1192 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1194 int verdict
= ip_vs_out_icmp_v6(skb
, &related
,
1202 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1204 int verdict
= ip_vs_out_icmp(skb
, &related
, hooknum
);
1210 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1215 /* reassemble IP fragments */
1216 #ifdef CONFIG_IP_VS_IPV6
1219 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1220 if (ip_vs_gather_frags(skb
,
1221 ip_vs_defrag_user(hooknum
)))
1224 ip_vs_fill_ip4hdr(skb_network_header(skb
), &iph
);
1228 * Check if the packet belongs to an existing entry
1230 cp
= pp
->conn_out_get(af
, skb
, &iph
, 0);
1233 return handle_response(af
, skb
, pd
, cp
, &iph
, hooknum
);
1234 if (sysctl_nat_icmp_send(net
) &&
1235 (pp
->protocol
== IPPROTO_TCP
||
1236 pp
->protocol
== IPPROTO_UDP
||
1237 pp
->protocol
== IPPROTO_SCTP
)) {
1238 __be16 _ports
[2], *pptr
;
1240 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1241 sizeof(_ports
), _ports
, &iph
);
1243 return NF_ACCEPT
; /* Not for me */
1244 if (ip_vs_has_real_service(net
, af
, iph
.protocol
, &iph
.saddr
,
1247 * Notify the real server: there is no
1248 * existing entry if it is not RST
1249 * packet or not TCP packet.
1251 if ((iph
.protocol
!= IPPROTO_TCP
&&
1252 iph
.protocol
!= IPPROTO_SCTP
)
1253 || ((iph
.protocol
== IPPROTO_TCP
1254 && !is_tcp_reset(skb
, iph
.len
))
1255 || (iph
.protocol
== IPPROTO_SCTP
1256 && !is_sctp_abort(skb
,
1258 #ifdef CONFIG_IP_VS_IPV6
1259 if (af
== AF_INET6
) {
1261 skb
->dev
= net
->loopback_dev
;
1263 ICMPV6_DEST_UNREACH
,
1264 ICMPV6_PORT_UNREACH
,
1270 ICMP_PORT_UNREACH
, 0);
1275 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1276 "ip_vs_out: packet continues traversal as normal");
1281 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1282 * used only for VS/NAT.
1283 * Check if packet is reply for established ip_vs_conn.
1286 ip_vs_reply4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1287 const struct nf_hook_state
*state
)
1289 return ip_vs_out(ops
->hooknum
, skb
, AF_INET
);
1293 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1294 * Check if packet is reply for established ip_vs_conn.
1297 ip_vs_local_reply4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1298 const struct nf_hook_state
*state
)
1300 return ip_vs_out(ops
->hooknum
, skb
, AF_INET
);
1303 #ifdef CONFIG_IP_VS_IPV6
1306 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1307 * used only for VS/NAT.
1308 * Check if packet is reply for established ip_vs_conn.
1311 ip_vs_reply6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1312 const struct nf_hook_state
*state
)
1314 return ip_vs_out(ops
->hooknum
, skb
, AF_INET6
);
1318 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1319 * Check if packet is reply for established ip_vs_conn.
1322 ip_vs_local_reply6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1323 const struct nf_hook_state
*state
)
1325 return ip_vs_out(ops
->hooknum
, skb
, AF_INET6
);
1331 * Handle ICMP messages in the outside-to-inside direction (incoming).
1332 * Find any that might be relevant, check against existing connections,
1333 * forward to the right destination host if relevant.
1334 * Currently handles error types - unreachable, quench, ttl exceeded.
1337 ip_vs_in_icmp(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1339 struct net
*net
= NULL
;
1341 struct icmphdr _icmph
, *ic
;
1342 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1343 struct ip_vs_iphdr ciph
;
1344 struct ip_vs_conn
*cp
;
1345 struct ip_vs_protocol
*pp
;
1346 struct ip_vs_proto_data
*pd
;
1347 unsigned int offset
, offset2
, ihl
, verdict
;
1352 /* reassemble IP fragments */
1353 if (ip_is_fragment(ip_hdr(skb
))) {
1354 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
1359 offset
= ihl
= iph
->ihl
* 4;
1360 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1364 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1365 ic
->type
, ntohs(icmp_id(ic
)),
1366 &iph
->saddr
, &iph
->daddr
);
1369 * Work through seeing if this is for us.
1370 * These checks are supposed to be in an order that means easy
1371 * things are checked first to speed up processing.... however
1372 * this means that some packets will manage to get a long way
1373 * down this stack and then be rejected, but that's life.
1375 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1376 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1377 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1382 /* Now find the contained IP header */
1383 offset
+= sizeof(_icmph
);
1384 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1386 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1390 /* Special case for errors for IPIP packets */
1392 if (cih
->protocol
== IPPROTO_IPIP
) {
1393 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1395 /* Error for our IPIP must arrive at LOCAL_IN */
1396 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1398 offset
+= cih
->ihl
* 4;
1399 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1401 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1405 pd
= ip_vs_proto_data_get(net
, cih
->protocol
);
1410 /* Is the embedded protocol header present? */
1411 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1415 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1416 "Checking incoming ICMP for");
1419 ip_vs_fill_ip4hdr(cih
, &ciph
);
1422 /* The embedded headers contain source and dest in reverse order.
1423 * For IPIP this is error for request, not for reply.
1425 cp
= pp
->conn_in_get(AF_INET
, skb
, &ciph
, ipip
? 0 : 1);
1431 /* Ensure the checksum is correct */
1432 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1433 /* Failed checksum! */
1434 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1440 __be32 info
= ic
->un
.gateway
;
1441 __u8 type
= ic
->type
;
1442 __u8 code
= ic
->code
;
1444 /* Update the MTU */
1445 if (ic
->type
== ICMP_DEST_UNREACH
&&
1446 ic
->code
== ICMP_FRAG_NEEDED
) {
1447 struct ip_vs_dest
*dest
= cp
->dest
;
1448 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1449 __be16 frag_off
= cih
->frag_off
;
1451 /* Strip outer IP and ICMP, go to IPIP header */
1452 if (pskb_pull(skb
, ihl
+ sizeof(_icmph
)) == NULL
)
1454 offset2
-= ihl
+ sizeof(_icmph
);
1455 skb_reset_network_header(skb
);
1456 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1457 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1458 ipv4_update_pmtu(skb
, dev_net(skb
->dev
),
1460 /* Client uses PMTUD? */
1461 if (!(frag_off
& htons(IP_DF
)))
1463 /* Prefer the resulting PMTU */
1465 struct ip_vs_dest_dst
*dest_dst
;
1468 dest_dst
= rcu_dereference(dest
->dest_dst
);
1470 mtu
= dst_mtu(dest_dst
->dst_cache
);
1473 if (mtu
> 68 + sizeof(struct iphdr
))
1474 mtu
-= sizeof(struct iphdr
);
1477 /* Strip outer IP, ICMP and IPIP, go to IP header of
1480 if (pskb_pull(skb
, offset2
) == NULL
)
1482 skb_reset_network_header(skb
);
1483 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1484 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1485 type
, code
, ntohl(info
));
1486 icmp_send(skb
, type
, code
, info
);
1487 /* ICMP can be shorter but anyways, account it */
1488 ip_vs_out_stats(cp
, skb
);
1492 verdict
= NF_STOLEN
;
1496 /* do the statistics and put it back */
1497 ip_vs_in_stats(cp
, skb
);
1498 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
||
1499 IPPROTO_SCTP
== cih
->protocol
)
1500 offset
+= 2 * sizeof(__u16
);
1501 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1504 __ip_vs_conn_put(cp
);
1509 #ifdef CONFIG_IP_VS_IPV6
1510 static int ip_vs_in_icmp_v6(struct sk_buff
*skb
, int *related
,
1511 unsigned int hooknum
, struct ip_vs_iphdr
*iph
)
1513 struct net
*net
= NULL
;
1514 struct ipv6hdr _ip6h
, *ip6h
;
1515 struct icmp6hdr _icmph
, *ic
;
1516 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1517 struct ip_vs_conn
*cp
;
1518 struct ip_vs_protocol
*pp
;
1519 struct ip_vs_proto_data
*pd
;
1520 unsigned int offs_ciph
, writable
, verdict
;
1524 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
, iph
);
1529 * Work through seeing if this is for us.
1530 * These checks are supposed to be in an order that means easy
1531 * things are checked first to speed up processing.... however
1532 * this means that some packets will manage to get a long way
1533 * down this stack and then be rejected, but that's life.
1535 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1539 /* Fragment header that is before ICMP header tells us that:
1540 * it's not an error message since they can't be fragmented.
1542 if (iph
->flags
& IP6_FH_F_FRAG
)
1545 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1546 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1547 &iph
->saddr
, &iph
->daddr
);
1549 /* Now find the contained IP header */
1550 ciph
.len
= iph
->len
+ sizeof(_icmph
);
1551 offs_ciph
= ciph
.len
; /* Save ip header offset */
1552 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
1554 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1555 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_in_get() handles reverse order */
1556 ciph
.daddr
.in6
= ip6h
->daddr
;
1557 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1558 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
1559 if (ciph
.protocol
< 0)
1560 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
1563 pd
= ip_vs_proto_data_get(net
, ciph
.protocol
);
1568 /* Cannot handle fragmented embedded protocol */
1572 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offs_ciph
,
1573 "Checking incoming ICMPv6 for");
1575 /* The embedded headers contain source and dest in reverse order
1576 * if not from localhost
1578 cp
= pp
->conn_in_get(AF_INET6
, skb
, &ciph
,
1579 (hooknum
== NF_INET_LOCAL_OUT
) ? 0 : 1);
1583 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1584 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1585 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1586 __ip_vs_conn_put(cp
);
1590 /* do the statistics and put it back */
1591 ip_vs_in_stats(cp
, skb
);
1593 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1594 writable
= ciph
.len
;
1595 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1596 IPPROTO_SCTP
== ciph
.protocol
)
1597 writable
+= 2 * sizeof(__u16
); /* Also mangle ports */
1599 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, writable
, hooknum
, &ciph
);
1601 __ip_vs_conn_put(cp
);
1609 * Check if it's for virtual services, look it up,
1610 * and send it on its way...
1613 ip_vs_in(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1616 struct ip_vs_iphdr iph
;
1617 struct ip_vs_protocol
*pp
;
1618 struct ip_vs_proto_data
*pd
;
1619 struct ip_vs_conn
*cp
;
1621 struct netns_ipvs
*ipvs
;
1622 int conn_reuse_mode
;
1624 /* Already marked as IPVS request or reply? */
1625 if (skb
->ipvs_property
)
1630 * - remote client: only PACKET_HOST
1631 * - route: used for struct net when skb->dev is unset
1633 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1634 hooknum
!= NF_INET_LOCAL_OUT
) ||
1636 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1637 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1638 " ignored in hook %u\n",
1639 skb
->pkt_type
, iph
.protocol
,
1640 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1643 /* ipvs enabled in this netns ? */
1645 ipvs
= net_ipvs(net
);
1646 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1649 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1651 /* Bad... Do not break raw sockets */
1652 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1654 struct sock
*sk
= skb
->sk
;
1655 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1657 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1661 #ifdef CONFIG_IP_VS_IPV6
1662 if (af
== AF_INET6
) {
1663 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1665 int verdict
= ip_vs_in_icmp_v6(skb
, &related
, hooknum
,
1673 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1675 int verdict
= ip_vs_in_icmp(skb
, &related
, hooknum
);
1681 /* Protocol supported? */
1682 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1687 * Check if the packet belongs to an existing connection entry
1689 cp
= pp
->conn_in_get(af
, skb
, &iph
, 0);
1691 conn_reuse_mode
= sysctl_conn_reuse_mode(ipvs
);
1692 if (conn_reuse_mode
&& !iph
.fragoffs
&&
1693 is_new_conn(skb
, &iph
) && cp
&&
1694 ((unlikely(sysctl_expire_nodest_conn(ipvs
)) && cp
->dest
&&
1695 unlikely(!atomic_read(&cp
->dest
->weight
))) ||
1696 unlikely(is_new_conn_expected(cp
, conn_reuse_mode
)))) {
1697 if (!atomic_read(&cp
->n_control
))
1698 ip_vs_conn_expire_now(cp
);
1699 __ip_vs_conn_put(cp
);
1703 if (unlikely(!cp
) && !iph
.fragoffs
) {
1704 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1705 * replayed fragment zero will already have created the cp
1709 /* Schedule and create new connection entry into &cp */
1710 if (!pp
->conn_schedule(af
, skb
, pd
, &v
, &cp
, &iph
))
1714 if (unlikely(!cp
)) {
1715 /* sorry, all this trouble for a no-hit :) */
1716 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1717 "ip_vs_in: packet continues traversal as normal");
1719 /* Fragment that couldn't be mapped to a conn entry
1720 * is missing module nf_defrag_ipv6
1722 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1723 IP_VS_DBG_PKT(7, af
, pp
, skb
, 0, "unhandled fragment");
1728 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Incoming packet");
1729 /* Check the server status */
1730 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1731 /* the destination server is not available */
1733 if (sysctl_expire_nodest_conn(ipvs
)) {
1734 /* try to expire the connection immediately */
1735 ip_vs_conn_expire_now(cp
);
1737 /* don't restart its timer, and silently
1739 __ip_vs_conn_put(cp
);
1743 ip_vs_in_stats(cp
, skb
);
1744 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
1745 if (cp
->packet_xmit
)
1746 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
1747 /* do not touch skb anymore */
1749 IP_VS_DBG_RL("warning: packet_xmit is null");
1753 /* Increase its packet counter and check if it is needed
1754 * to be synchronized
1756 * Sync connection if it is about to close to
1757 * encorage the standby servers to update the connections timeout
1759 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1762 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
1763 pkts
= sysctl_sync_threshold(ipvs
);
1765 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1767 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
1768 ip_vs_sync_conn(net
, cp
, pkts
);
1775 * AF_INET handler in NF_INET_LOCAL_IN chain
1776 * Schedule and forward packets from remote clients
1779 ip_vs_remote_request4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1780 const struct nf_hook_state
*state
)
1782 return ip_vs_in(ops
->hooknum
, skb
, AF_INET
);
1786 * AF_INET handler in NF_INET_LOCAL_OUT chain
1787 * Schedule and forward packets from local clients
1790 ip_vs_local_request4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1791 const struct nf_hook_state
*state
)
1793 return ip_vs_in(ops
->hooknum
, skb
, AF_INET
);
1796 #ifdef CONFIG_IP_VS_IPV6
1799 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1800 * Schedule and forward packets from remote clients
1803 ip_vs_remote_request6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1804 const struct nf_hook_state
*state
)
1806 return ip_vs_in(ops
->hooknum
, skb
, AF_INET6
);
1810 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1811 * Schedule and forward packets from local clients
1814 ip_vs_local_request6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1815 const struct nf_hook_state
*state
)
1817 return ip_vs_in(ops
->hooknum
, skb
, AF_INET6
);
1824 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1825 * related packets destined for 0.0.0.0/0.
1826 * When fwmark-based virtual service is used, such as transparent
1827 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1828 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1829 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1830 * and send them to ip_vs_in_icmp.
1833 ip_vs_forward_icmp(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1834 const struct nf_hook_state
*state
)
1838 struct netns_ipvs
*ipvs
;
1840 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1843 /* ipvs enabled in this netns ? */
1845 ipvs
= net_ipvs(net
);
1846 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1849 return ip_vs_in_icmp(skb
, &r
, ops
->hooknum
);
1852 #ifdef CONFIG_IP_VS_IPV6
1854 ip_vs_forward_icmp_v6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1855 const struct nf_hook_state
*state
)
1859 struct netns_ipvs
*ipvs
;
1860 struct ip_vs_iphdr iphdr
;
1862 ip_vs_fill_iph_skb(AF_INET6
, skb
, &iphdr
);
1863 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
1866 /* ipvs enabled in this netns ? */
1868 ipvs
= net_ipvs(net
);
1869 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1872 return ip_vs_in_icmp_v6(skb
, &r
, ops
->hooknum
, &iphdr
);
1877 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1878 /* After packet filtering, change source only for VS/NAT */
1880 .hook
= ip_vs_reply4
,
1881 .owner
= THIS_MODULE
,
1883 .hooknum
= NF_INET_LOCAL_IN
,
1884 .priority
= NF_IP_PRI_NAT_SRC
- 2,
1886 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1887 * or VS/NAT(change destination), so that filtering rules can be
1888 * applied to IPVS. */
1890 .hook
= ip_vs_remote_request4
,
1891 .owner
= THIS_MODULE
,
1893 .hooknum
= NF_INET_LOCAL_IN
,
1894 .priority
= NF_IP_PRI_NAT_SRC
- 1,
1896 /* Before ip_vs_in, change source only for VS/NAT */
1898 .hook
= ip_vs_local_reply4
,
1899 .owner
= THIS_MODULE
,
1901 .hooknum
= NF_INET_LOCAL_OUT
,
1902 .priority
= NF_IP_PRI_NAT_DST
+ 1,
1904 /* After mangle, schedule and forward local requests */
1906 .hook
= ip_vs_local_request4
,
1907 .owner
= THIS_MODULE
,
1909 .hooknum
= NF_INET_LOCAL_OUT
,
1910 .priority
= NF_IP_PRI_NAT_DST
+ 2,
1912 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1913 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1915 .hook
= ip_vs_forward_icmp
,
1916 .owner
= THIS_MODULE
,
1918 .hooknum
= NF_INET_FORWARD
,
1921 /* After packet filtering, change source only for VS/NAT */
1923 .hook
= ip_vs_reply4
,
1924 .owner
= THIS_MODULE
,
1926 .hooknum
= NF_INET_FORWARD
,
1929 #ifdef CONFIG_IP_VS_IPV6
1930 /* After packet filtering, change source only for VS/NAT */
1932 .hook
= ip_vs_reply6
,
1933 .owner
= THIS_MODULE
,
1935 .hooknum
= NF_INET_LOCAL_IN
,
1936 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
1938 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1939 * or VS/NAT(change destination), so that filtering rules can be
1940 * applied to IPVS. */
1942 .hook
= ip_vs_remote_request6
,
1943 .owner
= THIS_MODULE
,
1945 .hooknum
= NF_INET_LOCAL_IN
,
1946 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
1948 /* Before ip_vs_in, change source only for VS/NAT */
1950 .hook
= ip_vs_local_reply6
,
1951 .owner
= THIS_MODULE
,
1953 .hooknum
= NF_INET_LOCAL_OUT
,
1954 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
1956 /* After mangle, schedule and forward local requests */
1958 .hook
= ip_vs_local_request6
,
1959 .owner
= THIS_MODULE
,
1961 .hooknum
= NF_INET_LOCAL_OUT
,
1962 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
1964 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1965 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1967 .hook
= ip_vs_forward_icmp_v6
,
1968 .owner
= THIS_MODULE
,
1970 .hooknum
= NF_INET_FORWARD
,
1973 /* After packet filtering, change source only for VS/NAT */
1975 .hook
= ip_vs_reply6
,
1976 .owner
= THIS_MODULE
,
1978 .hooknum
= NF_INET_FORWARD
,
1984 * Initialize IP Virtual Server netns mem.
1986 static int __net_init
__ip_vs_init(struct net
*net
)
1988 struct netns_ipvs
*ipvs
;
1990 ipvs
= net_generic(net
, ip_vs_net_id
);
1994 /* Hold the beast until a service is registerd */
1997 /* Counters used for creating unique names */
1998 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
1999 atomic_inc(&ipvs_netns_cnt
);
2002 if (ip_vs_estimator_net_init(net
) < 0)
2003 goto estimator_fail
;
2005 if (ip_vs_control_net_init(net
) < 0)
2008 if (ip_vs_protocol_net_init(net
) < 0)
2011 if (ip_vs_app_net_init(net
) < 0)
2014 if (ip_vs_conn_net_init(net
) < 0)
2017 if (ip_vs_sync_net_init(net
) < 0)
2020 printk(KERN_INFO
"IPVS: Creating netns size=%zu id=%d\n",
2021 sizeof(struct netns_ipvs
), ipvs
->gen
);
2028 ip_vs_conn_net_cleanup(net
);
2030 ip_vs_app_net_cleanup(net
);
2032 ip_vs_protocol_net_cleanup(net
);
2034 ip_vs_control_net_cleanup(net
);
2036 ip_vs_estimator_net_cleanup(net
);
2042 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2044 ip_vs_service_net_cleanup(net
); /* ip_vs_flush() with locks */
2045 ip_vs_conn_net_cleanup(net
);
2046 ip_vs_app_net_cleanup(net
);
2047 ip_vs_protocol_net_cleanup(net
);
2048 ip_vs_control_net_cleanup(net
);
2049 ip_vs_estimator_net_cleanup(net
);
2050 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net
)->gen
);
2054 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2057 net_ipvs(net
)->enable
= 0; /* Disable packet reception */
2059 ip_vs_sync_net_cleanup(net
);
2063 static struct pernet_operations ipvs_core_ops
= {
2064 .init
= __ip_vs_init
,
2065 .exit
= __ip_vs_cleanup
,
2066 .id
= &ip_vs_net_id
,
2067 .size
= sizeof(struct netns_ipvs
),
2070 static struct pernet_operations ipvs_core_dev_ops
= {
2071 .exit
= __ip_vs_dev_cleanup
,
2075 * Initialize IP Virtual Server
2077 static int __init
ip_vs_init(void)
2081 ret
= ip_vs_control_init();
2083 pr_err("can't setup control.\n");
2087 ip_vs_protocol_init();
2089 ret
= ip_vs_conn_init();
2091 pr_err("can't setup connection table.\n");
2092 goto cleanup_protocol
;
2095 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2099 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2103 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2105 pr_err("can't register hooks.\n");
2109 ret
= ip_vs_register_nl_ioctl();
2111 pr_err("can't register netlink/ioctl.\n");
2115 pr_info("ipvs loaded.\n");
2120 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2122 unregister_pernet_device(&ipvs_core_dev_ops
);
2124 unregister_pernet_subsys(&ipvs_core_ops
);
2126 ip_vs_conn_cleanup();
2128 ip_vs_protocol_cleanup();
2129 ip_vs_control_cleanup();
2134 static void __exit
ip_vs_cleanup(void)
2136 ip_vs_unregister_nl_ioctl();
2137 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2138 unregister_pernet_device(&ipvs_core_dev_ops
);
2139 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2140 ip_vs_conn_cleanup();
2141 ip_vs_protocol_cleanup();
2142 ip_vs_control_cleanup();
2143 pr_info("ipvs unloaded.\n");
2146 module_init(ip_vs_init
);
2147 module_exit(ip_vs_cleanup
);
2148 MODULE_LICENSE("GPL");