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
);
322 dest
= sched
->schedule(svc
, skb
, iph
);
324 IP_VS_DBG(1, "p-schedule: no dest found.\n");
325 kfree(param
.pe_data
);
330 if (dst_port
== svc
->port
&& svc
->port
!= FTPPORT
)
334 * This adds param.pe_data to the template,
335 * and thus param.pe_data will be destroyed
336 * when the template expires */
337 ct
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
,
338 IP_VS_CONN_F_TEMPLATE
, dest
, skb
->mark
);
340 kfree(param
.pe_data
);
345 ct
->timeout
= svc
->timeout
;
347 /* set destination with the found template */
349 kfree(param
.pe_data
);
353 if (dport
== svc
->port
&& dest
->port
)
356 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
357 && iph
->protocol
== IPPROTO_UDP
) ?
358 IP_VS_CONN_F_ONE_PACKET
: 0;
361 * Create a new connection according to the template
363 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
, &iph
->saddr
,
364 src_port
, &iph
->daddr
, dst_port
, ¶m
);
366 cp
= ip_vs_conn_new(¶m
, dest
->af
, &dest
->addr
, dport
, flags
, dest
,
377 ip_vs_control_add(cp
, ct
);
380 ip_vs_conn_stats(cp
, svc
);
386 * IPVS main scheduling function
387 * It selects a server according to the virtual service, and
388 * creates a connection entry.
389 * Protocols supported: TCP, UDP
393 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
394 * svc/scheduler decides that this packet should be accepted with
395 * NF_ACCEPT because it must not be scheduled.
397 * 0 : scheduler can not find destination, so try bypass or
398 * return ICMP and then NF_DROP (ip_vs_leave).
400 * -1 : scheduler tried to schedule but fatal error occurred, eg.
401 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
402 * failure such as missing Call-ID, ENOMEM on skb_linearize
403 * or pe_data. In this case we should return NF_DROP without
404 * any attempts to send ICMP with ip_vs_leave.
407 ip_vs_schedule(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
408 struct ip_vs_proto_data
*pd
, int *ignored
,
409 struct ip_vs_iphdr
*iph
)
411 struct ip_vs_protocol
*pp
= pd
->pp
;
412 struct ip_vs_conn
*cp
= NULL
;
413 struct ip_vs_scheduler
*sched
;
414 struct ip_vs_dest
*dest
;
415 __be16 _ports
[2], *pptr
;
420 * IPv6 frags, only the first hit here.
422 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
427 * FTPDATA needs this check when using local real server.
428 * Never schedule Active FTPDATA connections from real server.
429 * For LVS-NAT they must be already created. For other methods
430 * with persistence the connection is created on SYN+ACK.
432 if (pptr
[0] == FTPDATA
) {
433 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
434 "Not scheduling FTPDATA");
439 * Do not schedule replies from local real server.
441 if ((!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
) &&
442 (cp
= pp
->conn_in_get(svc
->af
, skb
, iph
, 1))) {
443 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
444 "Not scheduling reply for existing connection");
445 __ip_vs_conn_put(cp
);
452 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
453 return ip_vs_sched_persist(svc
, skb
, pptr
[0], pptr
[1], ignored
,
459 * Non-persistent service
461 if (!svc
->fwmark
&& pptr
[1] != svc
->port
) {
463 pr_err("Schedule: port zero only supported "
464 "in persistent services, "
465 "check your ipvs configuration\n");
469 sched
= rcu_dereference(svc
->scheduler
);
470 dest
= sched
->schedule(svc
, skb
, iph
);
472 IP_VS_DBG(1, "Schedule: no dest found.\n");
476 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
477 && iph
->protocol
== IPPROTO_UDP
) ?
478 IP_VS_CONN_F_ONE_PACKET
: 0;
481 * Create a connection entry.
484 struct ip_vs_conn_param p
;
486 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
,
487 &iph
->saddr
, pptr
[0], &iph
->daddr
,
489 cp
= ip_vs_conn_new(&p
, dest
->af
, &dest
->addr
,
490 dest
->port
? dest
->port
: pptr
[1],
491 flags
, dest
, skb
->mark
);
498 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
499 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
501 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
502 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
503 IP_VS_DBG_ADDR(cp
->daf
, &cp
->daddr
), ntohs(cp
->dport
),
504 cp
->flags
, atomic_read(&cp
->refcnt
));
506 ip_vs_conn_stats(cp
, svc
);
512 * Pass or drop the packet.
513 * Called by ip_vs_in, when the virtual service is available but
514 * no destination is available for a new connection.
516 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
517 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
519 __be16 _ports
[2], *pptr
;
522 struct netns_ipvs
*ipvs
;
526 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
534 #ifdef CONFIG_IP_VS_IPV6
535 if (svc
->af
== AF_INET6
)
536 unicast
= ipv6_addr_type(&iph
->daddr
.in6
) & IPV6_ADDR_UNICAST
;
539 unicast
= (inet_addr_type(net
, iph
->daddr
.ip
) == RTN_UNICAST
);
541 /* if it is fwmark-based service, the cache_bypass sysctl is up
542 and the destination is a non-local unicast, then create
543 a cache_bypass connection entry */
544 ipvs
= net_ipvs(net
);
545 if (ipvs
->sysctl_cache_bypass
&& svc
->fwmark
&& unicast
) {
547 struct ip_vs_conn
*cp
;
548 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
549 iph
->protocol
== IPPROTO_UDP
) ?
550 IP_VS_CONN_F_ONE_PACKET
: 0;
551 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
553 /* create a new connection entry */
554 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
556 struct ip_vs_conn_param p
;
557 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
,
558 &iph
->saddr
, pptr
[0],
559 &iph
->daddr
, pptr
[1], &p
);
560 cp
= ip_vs_conn_new(&p
, svc
->af
, &daddr
, 0,
561 IP_VS_CONN_F_BYPASS
| flags
,
568 ip_vs_in_stats(cp
, skb
);
571 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
573 /* transmit the first SYN packet */
574 ret
= cp
->packet_xmit(skb
, cp
, pd
->pp
, iph
);
575 /* do not touch skb anymore */
577 atomic_inc(&cp
->in_pkts
);
584 * When the virtual ftp service is presented, packets destined
585 * for other services on the VIP may get here (except services
586 * listed in the ipvs table), pass the packets, because it is
587 * not ipvs job to decide to drop the packets.
589 if ((svc
->port
== FTPPORT
) && (pptr
[1] != FTPPORT
))
593 * Notify the client that the destination is unreachable, and
594 * release the socket buffer.
595 * Since it is in IP layer, the TCP socket is not actually
596 * created, the TCP RST packet cannot be sent, instead that
597 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
599 #ifdef CONFIG_IP_VS_IPV6
600 if (svc
->af
== AF_INET6
) {
602 struct net
*net_
= dev_net(skb_dst(skb
)->dev
);
604 skb
->dev
= net_
->loopback_dev
;
606 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
609 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
616 static int sysctl_snat_reroute(struct sk_buff
*skb
)
618 struct netns_ipvs
*ipvs
= net_ipvs(skb_net(skb
));
619 return ipvs
->sysctl_snat_reroute
;
622 static int sysctl_nat_icmp_send(struct net
*net
)
624 struct netns_ipvs
*ipvs
= net_ipvs(net
);
625 return ipvs
->sysctl_nat_icmp_send
;
628 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
630 return ipvs
->sysctl_expire_nodest_conn
;
635 static int sysctl_snat_reroute(struct sk_buff
*skb
) { return 0; }
636 static int sysctl_nat_icmp_send(struct net
*net
) { return 0; }
637 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
641 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
643 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
646 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
648 if (NF_INET_LOCAL_IN
== hooknum
)
649 return IP_DEFRAG_VS_IN
;
650 if (NF_INET_FORWARD
== hooknum
)
651 return IP_DEFRAG_VS_FWD
;
652 return IP_DEFRAG_VS_OUT
;
655 static inline int ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
660 err
= ip_defrag(skb
, user
);
663 ip_send_check(ip_hdr(skb
));
668 static int ip_vs_route_me_harder(int af
, struct sk_buff
*skb
,
669 unsigned int hooknum
)
671 if (!sysctl_snat_reroute(skb
))
673 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
674 if (NF_INET_LOCAL_IN
== hooknum
)
676 #ifdef CONFIG_IP_VS_IPV6
677 if (af
== AF_INET6
) {
678 struct dst_entry
*dst
= skb_dst(skb
);
680 if (dst
->dev
&& !(dst
->dev
->flags
& IFF_LOOPBACK
) &&
681 ip6_route_me_harder(skb
) != 0)
685 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
686 ip_route_me_harder(skb
, RTN_LOCAL
) != 0)
693 * Packet has been made sufficiently writable in caller
694 * - inout: 1=in->out, 0=out->in
696 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
697 struct ip_vs_conn
*cp
, int inout
)
699 struct iphdr
*iph
= ip_hdr(skb
);
700 unsigned int icmp_offset
= iph
->ihl
*4;
701 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
703 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
706 iph
->saddr
= cp
->vaddr
.ip
;
708 ciph
->daddr
= cp
->vaddr
.ip
;
711 iph
->daddr
= cp
->daddr
.ip
;
713 ciph
->saddr
= cp
->daddr
.ip
;
717 /* the TCP/UDP/SCTP port */
718 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
719 IPPROTO_SCTP
== ciph
->protocol
) {
720 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
723 ports
[1] = cp
->vport
;
725 ports
[0] = cp
->dport
;
728 /* And finally the ICMP checksum */
730 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
731 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
734 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
735 "Forwarding altered outgoing ICMP");
737 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
738 "Forwarding altered incoming ICMP");
741 #ifdef CONFIG_IP_VS_IPV6
742 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
743 struct ip_vs_conn
*cp
, int inout
)
745 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
746 unsigned int icmp_offset
= 0;
747 unsigned int offs
= 0; /* header offset*/
749 struct icmp6hdr
*icmph
;
750 struct ipv6hdr
*ciph
;
751 unsigned short fragoffs
;
753 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
754 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
755 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
756 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
758 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
761 iph
->saddr
= cp
->vaddr
.in6
;
762 ciph
->daddr
= cp
->vaddr
.in6
;
764 iph
->daddr
= cp
->daddr
.in6
;
765 ciph
->saddr
= cp
->daddr
.in6
;
768 /* the TCP/UDP/SCTP port */
769 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
770 IPPROTO_SCTP
== protocol
)) {
771 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
773 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
774 ntohs(inout
? ports
[1] : ports
[0]),
775 ntohs(inout
? cp
->vport
: cp
->dport
));
777 ports
[1] = cp
->vport
;
779 ports
[0] = cp
->dport
;
782 /* And finally the ICMP checksum */
783 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
784 skb
->len
- icmp_offset
,
786 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
787 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
788 skb
->ip_summed
= CHECKSUM_PARTIAL
;
791 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
792 (void *)ciph
- (void *)iph
,
793 "Forwarding altered outgoing ICMPv6");
795 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
796 (void *)ciph
- (void *)iph
,
797 "Forwarding altered incoming ICMPv6");
801 /* Handle relevant response ICMP messages - forward to the right
804 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
805 union nf_inet_addr
*snet
,
806 __u8 protocol
, struct ip_vs_conn
*cp
,
807 struct ip_vs_protocol
*pp
,
808 unsigned int offset
, unsigned int ihl
,
809 unsigned int hooknum
)
811 unsigned int verdict
= NF_DROP
;
813 if (IP_VS_FWD_METHOD(cp
) != 0) {
814 pr_err("shouldn't reach here, because the box is on the "
815 "half connection in the tun/dr module.\n");
818 /* Ensure the checksum is correct */
819 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
820 /* Failed checksum! */
821 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
822 IP_VS_DBG_ADDR(af
, snet
));
826 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
827 IPPROTO_SCTP
== protocol
)
828 offset
+= 2 * sizeof(__u16
);
829 if (!skb_make_writable(skb
, offset
))
832 #ifdef CONFIG_IP_VS_IPV6
834 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
837 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
839 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
842 /* do the statistics and put it back */
843 ip_vs_out_stats(cp
, skb
);
845 skb
->ipvs_property
= 1;
846 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
849 ip_vs_update_conntrack(skb
, cp
, 0);
853 __ip_vs_conn_put(cp
);
859 * Handle ICMP messages in the inside-to-outside direction (outgoing).
860 * Find any that might be relevant, check against existing connections.
861 * Currently handles error types - unreachable, quench, ttl exceeded.
863 static int ip_vs_out_icmp(struct sk_buff
*skb
, int *related
,
864 unsigned int hooknum
)
867 struct icmphdr _icmph
, *ic
;
868 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
869 struct ip_vs_iphdr ciph
;
870 struct ip_vs_conn
*cp
;
871 struct ip_vs_protocol
*pp
;
872 unsigned int offset
, ihl
;
873 union nf_inet_addr snet
;
877 /* reassemble IP fragments */
878 if (ip_is_fragment(ip_hdr(skb
))) {
879 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
884 offset
= ihl
= iph
->ihl
* 4;
885 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
889 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
890 ic
->type
, ntohs(icmp_id(ic
)),
891 &iph
->saddr
, &iph
->daddr
);
894 * Work through seeing if this is for us.
895 * These checks are supposed to be in an order that means easy
896 * things are checked first to speed up processing.... however
897 * this means that some packets will manage to get a long way
898 * down this stack and then be rejected, but that's life.
900 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
901 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
902 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
907 /* Now find the contained IP header */
908 offset
+= sizeof(_icmph
);
909 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
911 return NF_ACCEPT
; /* The packet looks wrong, ignore */
913 pp
= ip_vs_proto_get(cih
->protocol
);
917 /* Is the embedded protocol header present? */
918 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
922 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
923 "Checking outgoing ICMP for");
925 ip_vs_fill_ip4hdr(cih
, &ciph
);
927 /* The embedded headers contain source and dest in reverse order */
928 cp
= pp
->conn_out_get(AF_INET
, skb
, &ciph
, 1);
932 snet
.ip
= iph
->saddr
;
933 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
934 pp
, ciph
.len
, ihl
, hooknum
);
937 #ifdef CONFIG_IP_VS_IPV6
938 static int ip_vs_out_icmp_v6(struct sk_buff
*skb
, int *related
,
939 unsigned int hooknum
, struct ip_vs_iphdr
*ipvsh
)
941 struct icmp6hdr _icmph
, *ic
;
942 struct ipv6hdr _ip6h
, *ip6h
; /* The ip header contained within ICMP */
943 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
944 struct ip_vs_conn
*cp
;
945 struct ip_vs_protocol
*pp
;
946 union nf_inet_addr snet
;
947 unsigned int writable
;
950 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
, ipvsh
);
955 * Work through seeing if this is for us.
956 * These checks are supposed to be in an order that means easy
957 * things are checked first to speed up processing.... however
958 * this means that some packets will manage to get a long way
959 * down this stack and then be rejected, but that's life.
961 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
965 /* Fragment header that is before ICMP header tells us that:
966 * it's not an error message since they can't be fragmented.
968 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
971 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
972 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
973 &ipvsh
->saddr
, &ipvsh
->daddr
);
975 /* Now find the contained IP header */
976 ciph
.len
= ipvsh
->len
+ sizeof(_icmph
);
977 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
979 return NF_ACCEPT
; /* The packet looks wrong, ignore */
980 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_out_get() handles reverse order */
981 ciph
.daddr
.in6
= ip6h
->daddr
;
982 /* skip possible IPv6 exthdrs of contained IPv6 packet */
983 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
984 if (ciph
.protocol
< 0)
985 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
987 pp
= ip_vs_proto_get(ciph
.protocol
);
991 /* The embedded headers contain source and dest in reverse order */
992 cp
= pp
->conn_out_get(AF_INET6
, skb
, &ciph
, 1);
996 snet
.in6
= ciph
.saddr
.in6
;
998 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
999 pp
, writable
, sizeof(struct ipv6hdr
),
1005 * Check if sctp chunc is ABORT chunk
1007 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
1009 sctp_chunkhdr_t
*sch
, schunk
;
1010 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
1011 sizeof(schunk
), &schunk
);
1014 if (sch
->type
== SCTP_CID_ABORT
)
1019 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
1021 struct tcphdr _tcph
, *th
;
1023 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1029 static inline bool is_new_conn(const struct sk_buff
*skb
,
1030 struct ip_vs_iphdr
*iph
)
1032 switch (iph
->protocol
) {
1034 struct tcphdr _tcph
, *th
;
1036 th
= skb_header_pointer(skb
, iph
->len
, sizeof(_tcph
), &_tcph
);
1041 case IPPROTO_SCTP
: {
1042 sctp_chunkhdr_t
*sch
, schunk
;
1044 sch
= skb_header_pointer(skb
, iph
->len
+ sizeof(sctp_sctphdr_t
),
1045 sizeof(schunk
), &schunk
);
1048 return sch
->type
== SCTP_CID_INIT
;
1055 static inline bool is_new_conn_expected(const struct ip_vs_conn
*cp
,
1056 int conn_reuse_mode
)
1058 /* Controlled (FTP DATA or persistence)? */
1062 switch (cp
->protocol
) {
1064 return (cp
->state
== IP_VS_TCP_S_TIME_WAIT
) ||
1065 ((conn_reuse_mode
& 2) &&
1066 (cp
->state
== IP_VS_TCP_S_FIN_WAIT
) &&
1067 (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
));
1069 return cp
->state
== IP_VS_SCTP_S_CLOSED
;
1075 /* Handle response packets: rewrite addresses and send away...
1078 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1079 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
,
1080 unsigned int hooknum
)
1082 struct ip_vs_protocol
*pp
= pd
->pp
;
1084 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Outgoing packet");
1086 if (!skb_make_writable(skb
, iph
->len
))
1089 /* mangle the packet */
1090 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
, iph
))
1093 #ifdef CONFIG_IP_VS_IPV6
1095 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1099 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1100 ip_send_check(ip_hdr(skb
));
1104 * nf_iterate does not expect change in the skb->dst->dev.
1105 * It looks like it is not fatal to enable this code for hooks
1106 * where our handlers are at the end of the chain list and
1107 * when all next handlers use skb->dst->dev and not outdev.
1108 * It will definitely route properly the inout NAT traffic
1109 * when multiple paths are used.
1112 /* For policy routing, packets originating from this
1113 * machine itself may be routed differently to packets
1114 * passing through. We want this packet to be routed as
1115 * if it came from this machine itself. So re-compute
1116 * the routing information.
1118 if (ip_vs_route_me_harder(af
, skb
, hooknum
))
1121 IP_VS_DBG_PKT(10, af
, pp
, skb
, 0, "After SNAT");
1123 ip_vs_out_stats(cp
, skb
);
1124 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1125 skb
->ipvs_property
= 1;
1126 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1129 ip_vs_update_conntrack(skb
, cp
, 0);
1143 * Check if outgoing packet belongs to the established ip_vs_conn.
1146 ip_vs_out(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1148 struct net
*net
= NULL
;
1149 struct ip_vs_iphdr iph
;
1150 struct ip_vs_protocol
*pp
;
1151 struct ip_vs_proto_data
*pd
;
1152 struct ip_vs_conn
*cp
;
1156 /* Already marked as IPVS request or reply? */
1157 if (skb
->ipvs_property
)
1160 /* Bad... Do not break raw sockets */
1161 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1163 struct sock
*sk
= skb
->sk
;
1164 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1166 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1170 if (unlikely(!skb_dst(skb
)))
1174 if (!net_ipvs(net
)->enable
)
1177 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1178 #ifdef CONFIG_IP_VS_IPV6
1179 if (af
== AF_INET6
) {
1180 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1182 int verdict
= ip_vs_out_icmp_v6(skb
, &related
,
1190 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1192 int verdict
= ip_vs_out_icmp(skb
, &related
, hooknum
);
1198 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1203 /* reassemble IP fragments */
1204 #ifdef CONFIG_IP_VS_IPV6
1207 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1208 if (ip_vs_gather_frags(skb
,
1209 ip_vs_defrag_user(hooknum
)))
1212 ip_vs_fill_ip4hdr(skb_network_header(skb
), &iph
);
1216 * Check if the packet belongs to an existing entry
1218 cp
= pp
->conn_out_get(af
, skb
, &iph
, 0);
1221 return handle_response(af
, skb
, pd
, cp
, &iph
, hooknum
);
1222 if (sysctl_nat_icmp_send(net
) &&
1223 (pp
->protocol
== IPPROTO_TCP
||
1224 pp
->protocol
== IPPROTO_UDP
||
1225 pp
->protocol
== IPPROTO_SCTP
)) {
1226 __be16 _ports
[2], *pptr
;
1228 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1229 sizeof(_ports
), _ports
, &iph
);
1231 return NF_ACCEPT
; /* Not for me */
1232 if (ip_vs_has_real_service(net
, af
, iph
.protocol
, &iph
.saddr
,
1235 * Notify the real server: there is no
1236 * existing entry if it is not RST
1237 * packet or not TCP packet.
1239 if ((iph
.protocol
!= IPPROTO_TCP
&&
1240 iph
.protocol
!= IPPROTO_SCTP
)
1241 || ((iph
.protocol
== IPPROTO_TCP
1242 && !is_tcp_reset(skb
, iph
.len
))
1243 || (iph
.protocol
== IPPROTO_SCTP
1244 && !is_sctp_abort(skb
,
1246 #ifdef CONFIG_IP_VS_IPV6
1247 if (af
== AF_INET6
) {
1249 skb
->dev
= net
->loopback_dev
;
1251 ICMPV6_DEST_UNREACH
,
1252 ICMPV6_PORT_UNREACH
,
1258 ICMP_PORT_UNREACH
, 0);
1263 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1264 "ip_vs_out: packet continues traversal as normal");
1269 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1270 * used only for VS/NAT.
1271 * Check if packet is reply for established ip_vs_conn.
1274 ip_vs_reply4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1275 const struct nf_hook_state
*state
)
1277 return ip_vs_out(ops
->hooknum
, skb
, AF_INET
);
1281 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1282 * Check if packet is reply for established ip_vs_conn.
1285 ip_vs_local_reply4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1286 const struct nf_hook_state
*state
)
1288 return ip_vs_out(ops
->hooknum
, skb
, AF_INET
);
1291 #ifdef CONFIG_IP_VS_IPV6
1294 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1295 * used only for VS/NAT.
1296 * Check if packet is reply for established ip_vs_conn.
1299 ip_vs_reply6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1300 const struct nf_hook_state
*state
)
1302 return ip_vs_out(ops
->hooknum
, skb
, AF_INET6
);
1306 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1307 * Check if packet is reply for established ip_vs_conn.
1310 ip_vs_local_reply6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1311 const struct nf_hook_state
*state
)
1313 return ip_vs_out(ops
->hooknum
, skb
, AF_INET6
);
1319 * Handle ICMP messages in the outside-to-inside direction (incoming).
1320 * Find any that might be relevant, check against existing connections,
1321 * forward to the right destination host if relevant.
1322 * Currently handles error types - unreachable, quench, ttl exceeded.
1325 ip_vs_in_icmp(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1327 struct net
*net
= NULL
;
1329 struct icmphdr _icmph
, *ic
;
1330 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1331 struct ip_vs_iphdr ciph
;
1332 struct ip_vs_conn
*cp
;
1333 struct ip_vs_protocol
*pp
;
1334 struct ip_vs_proto_data
*pd
;
1335 unsigned int offset
, offset2
, ihl
, verdict
;
1340 /* reassemble IP fragments */
1341 if (ip_is_fragment(ip_hdr(skb
))) {
1342 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
1347 offset
= ihl
= iph
->ihl
* 4;
1348 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1352 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1353 ic
->type
, ntohs(icmp_id(ic
)),
1354 &iph
->saddr
, &iph
->daddr
);
1357 * Work through seeing if this is for us.
1358 * These checks are supposed to be in an order that means easy
1359 * things are checked first to speed up processing.... however
1360 * this means that some packets will manage to get a long way
1361 * down this stack and then be rejected, but that's life.
1363 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1364 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1365 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1370 /* Now find the contained IP header */
1371 offset
+= sizeof(_icmph
);
1372 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1374 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1378 /* Special case for errors for IPIP packets */
1380 if (cih
->protocol
== IPPROTO_IPIP
) {
1381 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1383 /* Error for our IPIP must arrive at LOCAL_IN */
1384 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1386 offset
+= cih
->ihl
* 4;
1387 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1389 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1393 pd
= ip_vs_proto_data_get(net
, cih
->protocol
);
1398 /* Is the embedded protocol header present? */
1399 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1403 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1404 "Checking incoming ICMP for");
1407 ip_vs_fill_ip4hdr(cih
, &ciph
);
1410 /* The embedded headers contain source and dest in reverse order.
1411 * For IPIP this is error for request, not for reply.
1413 cp
= pp
->conn_in_get(AF_INET
, skb
, &ciph
, ipip
? 0 : 1);
1419 /* Ensure the checksum is correct */
1420 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1421 /* Failed checksum! */
1422 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1428 __be32 info
= ic
->un
.gateway
;
1429 __u8 type
= ic
->type
;
1430 __u8 code
= ic
->code
;
1432 /* Update the MTU */
1433 if (ic
->type
== ICMP_DEST_UNREACH
&&
1434 ic
->code
== ICMP_FRAG_NEEDED
) {
1435 struct ip_vs_dest
*dest
= cp
->dest
;
1436 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1437 __be16 frag_off
= cih
->frag_off
;
1439 /* Strip outer IP and ICMP, go to IPIP header */
1440 if (pskb_pull(skb
, ihl
+ sizeof(_icmph
)) == NULL
)
1442 offset2
-= ihl
+ sizeof(_icmph
);
1443 skb_reset_network_header(skb
);
1444 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1445 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1446 ipv4_update_pmtu(skb
, dev_net(skb
->dev
),
1448 /* Client uses PMTUD? */
1449 if (!(frag_off
& htons(IP_DF
)))
1451 /* Prefer the resulting PMTU */
1453 struct ip_vs_dest_dst
*dest_dst
;
1456 dest_dst
= rcu_dereference(dest
->dest_dst
);
1458 mtu
= dst_mtu(dest_dst
->dst_cache
);
1461 if (mtu
> 68 + sizeof(struct iphdr
))
1462 mtu
-= sizeof(struct iphdr
);
1465 /* Strip outer IP, ICMP and IPIP, go to IP header of
1468 if (pskb_pull(skb
, offset2
) == NULL
)
1470 skb_reset_network_header(skb
);
1471 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1472 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1473 type
, code
, ntohl(info
));
1474 icmp_send(skb
, type
, code
, info
);
1475 /* ICMP can be shorter but anyways, account it */
1476 ip_vs_out_stats(cp
, skb
);
1480 verdict
= NF_STOLEN
;
1484 /* do the statistics and put it back */
1485 ip_vs_in_stats(cp
, skb
);
1486 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
||
1487 IPPROTO_SCTP
== cih
->protocol
)
1488 offset
+= 2 * sizeof(__u16
);
1489 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1492 __ip_vs_conn_put(cp
);
1497 #ifdef CONFIG_IP_VS_IPV6
1498 static int ip_vs_in_icmp_v6(struct sk_buff
*skb
, int *related
,
1499 unsigned int hooknum
, struct ip_vs_iphdr
*iph
)
1501 struct net
*net
= NULL
;
1502 struct ipv6hdr _ip6h
, *ip6h
;
1503 struct icmp6hdr _icmph
, *ic
;
1504 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1505 struct ip_vs_conn
*cp
;
1506 struct ip_vs_protocol
*pp
;
1507 struct ip_vs_proto_data
*pd
;
1508 unsigned int offs_ciph
, writable
, verdict
;
1512 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
, iph
);
1517 * Work through seeing if this is for us.
1518 * These checks are supposed to be in an order that means easy
1519 * things are checked first to speed up processing.... however
1520 * this means that some packets will manage to get a long way
1521 * down this stack and then be rejected, but that's life.
1523 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1527 /* Fragment header that is before ICMP header tells us that:
1528 * it's not an error message since they can't be fragmented.
1530 if (iph
->flags
& IP6_FH_F_FRAG
)
1533 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1534 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1535 &iph
->saddr
, &iph
->daddr
);
1537 /* Now find the contained IP header */
1538 ciph
.len
= iph
->len
+ sizeof(_icmph
);
1539 offs_ciph
= ciph
.len
; /* Save ip header offset */
1540 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
1542 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1543 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_in_get() handles reverse order */
1544 ciph
.daddr
.in6
= ip6h
->daddr
;
1545 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1546 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
1547 if (ciph
.protocol
< 0)
1548 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
1551 pd
= ip_vs_proto_data_get(net
, ciph
.protocol
);
1556 /* Cannot handle fragmented embedded protocol */
1560 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offs_ciph
,
1561 "Checking incoming ICMPv6 for");
1563 /* The embedded headers contain source and dest in reverse order
1564 * if not from localhost
1566 cp
= pp
->conn_in_get(AF_INET6
, skb
, &ciph
,
1567 (hooknum
== NF_INET_LOCAL_OUT
) ? 0 : 1);
1571 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1572 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1573 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1574 __ip_vs_conn_put(cp
);
1578 /* do the statistics and put it back */
1579 ip_vs_in_stats(cp
, skb
);
1581 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1582 writable
= ciph
.len
;
1583 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1584 IPPROTO_SCTP
== ciph
.protocol
)
1585 writable
+= 2 * sizeof(__u16
); /* Also mangle ports */
1587 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, writable
, hooknum
, &ciph
);
1589 __ip_vs_conn_put(cp
);
1597 * Check if it's for virtual services, look it up,
1598 * and send it on its way...
1601 ip_vs_in(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1604 struct ip_vs_iphdr iph
;
1605 struct ip_vs_protocol
*pp
;
1606 struct ip_vs_proto_data
*pd
;
1607 struct ip_vs_conn
*cp
;
1609 struct netns_ipvs
*ipvs
;
1610 int conn_reuse_mode
;
1612 /* Already marked as IPVS request or reply? */
1613 if (skb
->ipvs_property
)
1618 * - remote client: only PACKET_HOST
1619 * - route: used for struct net when skb->dev is unset
1621 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1622 hooknum
!= NF_INET_LOCAL_OUT
) ||
1624 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1625 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1626 " ignored in hook %u\n",
1627 skb
->pkt_type
, iph
.protocol
,
1628 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1631 /* ipvs enabled in this netns ? */
1633 ipvs
= net_ipvs(net
);
1634 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1637 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1639 /* Bad... Do not break raw sockets */
1640 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1642 struct sock
*sk
= skb
->sk
;
1643 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1645 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1649 #ifdef CONFIG_IP_VS_IPV6
1650 if (af
== AF_INET6
) {
1651 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1653 int verdict
= ip_vs_in_icmp_v6(skb
, &related
, hooknum
,
1661 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1663 int verdict
= ip_vs_in_icmp(skb
, &related
, hooknum
);
1669 /* Protocol supported? */
1670 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1675 * Check if the packet belongs to an existing connection entry
1677 cp
= pp
->conn_in_get(af
, skb
, &iph
, 0);
1679 conn_reuse_mode
= sysctl_conn_reuse_mode(ipvs
);
1680 if (conn_reuse_mode
&& !iph
.fragoffs
&&
1681 is_new_conn(skb
, &iph
) && cp
&&
1682 ((unlikely(sysctl_expire_nodest_conn(ipvs
)) && cp
->dest
&&
1683 unlikely(!atomic_read(&cp
->dest
->weight
))) ||
1684 unlikely(is_new_conn_expected(cp
, conn_reuse_mode
)))) {
1685 if (!atomic_read(&cp
->n_control
))
1686 ip_vs_conn_expire_now(cp
);
1687 __ip_vs_conn_put(cp
);
1691 if (unlikely(!cp
) && !iph
.fragoffs
) {
1692 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1693 * replayed fragment zero will already have created the cp
1697 /* Schedule and create new connection entry into &cp */
1698 if (!pp
->conn_schedule(af
, skb
, pd
, &v
, &cp
, &iph
))
1702 if (unlikely(!cp
)) {
1703 /* sorry, all this trouble for a no-hit :) */
1704 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1705 "ip_vs_in: packet continues traversal as normal");
1707 /* Fragment that couldn't be mapped to a conn entry
1708 * is missing module nf_defrag_ipv6
1710 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1711 IP_VS_DBG_PKT(7, af
, pp
, skb
, 0, "unhandled fragment");
1716 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Incoming packet");
1717 /* Check the server status */
1718 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1719 /* the destination server is not available */
1721 if (sysctl_expire_nodest_conn(ipvs
)) {
1722 /* try to expire the connection immediately */
1723 ip_vs_conn_expire_now(cp
);
1725 /* don't restart its timer, and silently
1727 __ip_vs_conn_put(cp
);
1731 ip_vs_in_stats(cp
, skb
);
1732 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
1733 if (cp
->packet_xmit
)
1734 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
1735 /* do not touch skb anymore */
1737 IP_VS_DBG_RL("warning: packet_xmit is null");
1741 /* Increase its packet counter and check if it is needed
1742 * to be synchronized
1744 * Sync connection if it is about to close to
1745 * encorage the standby servers to update the connections timeout
1747 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1750 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
1751 pkts
= sysctl_sync_threshold(ipvs
);
1753 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1755 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
1756 ip_vs_sync_conn(net
, cp
, pkts
);
1763 * AF_INET handler in NF_INET_LOCAL_IN chain
1764 * Schedule and forward packets from remote clients
1767 ip_vs_remote_request4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1768 const struct nf_hook_state
*state
)
1770 return ip_vs_in(ops
->hooknum
, skb
, AF_INET
);
1774 * AF_INET handler in NF_INET_LOCAL_OUT chain
1775 * Schedule and forward packets from local clients
1778 ip_vs_local_request4(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1779 const struct nf_hook_state
*state
)
1781 return ip_vs_in(ops
->hooknum
, skb
, AF_INET
);
1784 #ifdef CONFIG_IP_VS_IPV6
1787 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1788 * Schedule and forward packets from remote clients
1791 ip_vs_remote_request6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1792 const struct nf_hook_state
*state
)
1794 return ip_vs_in(ops
->hooknum
, skb
, AF_INET6
);
1798 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1799 * Schedule and forward packets from local clients
1802 ip_vs_local_request6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1803 const struct nf_hook_state
*state
)
1805 return ip_vs_in(ops
->hooknum
, skb
, AF_INET6
);
1812 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1813 * related packets destined for 0.0.0.0/0.
1814 * When fwmark-based virtual service is used, such as transparent
1815 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1816 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1817 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1818 * and send them to ip_vs_in_icmp.
1821 ip_vs_forward_icmp(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1822 const struct nf_hook_state
*state
)
1826 struct netns_ipvs
*ipvs
;
1828 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1831 /* ipvs enabled in this netns ? */
1833 ipvs
= net_ipvs(net
);
1834 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1837 return ip_vs_in_icmp(skb
, &r
, ops
->hooknum
);
1840 #ifdef CONFIG_IP_VS_IPV6
1842 ip_vs_forward_icmp_v6(const struct nf_hook_ops
*ops
, struct sk_buff
*skb
,
1843 const struct nf_hook_state
*state
)
1847 struct netns_ipvs
*ipvs
;
1848 struct ip_vs_iphdr iphdr
;
1850 ip_vs_fill_iph_skb(AF_INET6
, skb
, &iphdr
);
1851 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
1854 /* ipvs enabled in this netns ? */
1856 ipvs
= net_ipvs(net
);
1857 if (unlikely(sysctl_backup_only(ipvs
) || !ipvs
->enable
))
1860 return ip_vs_in_icmp_v6(skb
, &r
, ops
->hooknum
, &iphdr
);
1865 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1866 /* After packet filtering, change source only for VS/NAT */
1868 .hook
= ip_vs_reply4
,
1869 .owner
= THIS_MODULE
,
1871 .hooknum
= NF_INET_LOCAL_IN
,
1872 .priority
= NF_IP_PRI_NAT_SRC
- 2,
1874 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1875 * or VS/NAT(change destination), so that filtering rules can be
1876 * applied to IPVS. */
1878 .hook
= ip_vs_remote_request4
,
1879 .owner
= THIS_MODULE
,
1881 .hooknum
= NF_INET_LOCAL_IN
,
1882 .priority
= NF_IP_PRI_NAT_SRC
- 1,
1884 /* Before ip_vs_in, change source only for VS/NAT */
1886 .hook
= ip_vs_local_reply4
,
1887 .owner
= THIS_MODULE
,
1889 .hooknum
= NF_INET_LOCAL_OUT
,
1890 .priority
= NF_IP_PRI_NAT_DST
+ 1,
1892 /* After mangle, schedule and forward local requests */
1894 .hook
= ip_vs_local_request4
,
1895 .owner
= THIS_MODULE
,
1897 .hooknum
= NF_INET_LOCAL_OUT
,
1898 .priority
= NF_IP_PRI_NAT_DST
+ 2,
1900 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1901 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1903 .hook
= ip_vs_forward_icmp
,
1904 .owner
= THIS_MODULE
,
1906 .hooknum
= NF_INET_FORWARD
,
1909 /* After packet filtering, change source only for VS/NAT */
1911 .hook
= ip_vs_reply4
,
1912 .owner
= THIS_MODULE
,
1914 .hooknum
= NF_INET_FORWARD
,
1917 #ifdef CONFIG_IP_VS_IPV6
1918 /* After packet filtering, change source only for VS/NAT */
1920 .hook
= ip_vs_reply6
,
1921 .owner
= THIS_MODULE
,
1923 .hooknum
= NF_INET_LOCAL_IN
,
1924 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
1926 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1927 * or VS/NAT(change destination), so that filtering rules can be
1928 * applied to IPVS. */
1930 .hook
= ip_vs_remote_request6
,
1931 .owner
= THIS_MODULE
,
1933 .hooknum
= NF_INET_LOCAL_IN
,
1934 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
1936 /* Before ip_vs_in, change source only for VS/NAT */
1938 .hook
= ip_vs_local_reply6
,
1939 .owner
= THIS_MODULE
,
1941 .hooknum
= NF_INET_LOCAL_OUT
,
1942 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
1944 /* After mangle, schedule and forward local requests */
1946 .hook
= ip_vs_local_request6
,
1947 .owner
= THIS_MODULE
,
1949 .hooknum
= NF_INET_LOCAL_OUT
,
1950 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
1952 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1953 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1955 .hook
= ip_vs_forward_icmp_v6
,
1956 .owner
= THIS_MODULE
,
1958 .hooknum
= NF_INET_FORWARD
,
1961 /* After packet filtering, change source only for VS/NAT */
1963 .hook
= ip_vs_reply6
,
1964 .owner
= THIS_MODULE
,
1966 .hooknum
= NF_INET_FORWARD
,
1972 * Initialize IP Virtual Server netns mem.
1974 static int __net_init
__ip_vs_init(struct net
*net
)
1976 struct netns_ipvs
*ipvs
;
1978 ipvs
= net_generic(net
, ip_vs_net_id
);
1982 /* Hold the beast until a service is registerd */
1985 /* Counters used for creating unique names */
1986 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
1987 atomic_inc(&ipvs_netns_cnt
);
1990 if (ip_vs_estimator_net_init(net
) < 0)
1991 goto estimator_fail
;
1993 if (ip_vs_control_net_init(net
) < 0)
1996 if (ip_vs_protocol_net_init(net
) < 0)
1999 if (ip_vs_app_net_init(net
) < 0)
2002 if (ip_vs_conn_net_init(net
) < 0)
2005 if (ip_vs_sync_net_init(net
) < 0)
2008 printk(KERN_INFO
"IPVS: Creating netns size=%zu id=%d\n",
2009 sizeof(struct netns_ipvs
), ipvs
->gen
);
2016 ip_vs_conn_net_cleanup(net
);
2018 ip_vs_app_net_cleanup(net
);
2020 ip_vs_protocol_net_cleanup(net
);
2022 ip_vs_control_net_cleanup(net
);
2024 ip_vs_estimator_net_cleanup(net
);
2030 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2032 ip_vs_service_net_cleanup(net
); /* ip_vs_flush() with locks */
2033 ip_vs_conn_net_cleanup(net
);
2034 ip_vs_app_net_cleanup(net
);
2035 ip_vs_protocol_net_cleanup(net
);
2036 ip_vs_control_net_cleanup(net
);
2037 ip_vs_estimator_net_cleanup(net
);
2038 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net
)->gen
);
2042 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2045 net_ipvs(net
)->enable
= 0; /* Disable packet reception */
2047 ip_vs_sync_net_cleanup(net
);
2051 static struct pernet_operations ipvs_core_ops
= {
2052 .init
= __ip_vs_init
,
2053 .exit
= __ip_vs_cleanup
,
2054 .id
= &ip_vs_net_id
,
2055 .size
= sizeof(struct netns_ipvs
),
2058 static struct pernet_operations ipvs_core_dev_ops
= {
2059 .exit
= __ip_vs_dev_cleanup
,
2063 * Initialize IP Virtual Server
2065 static int __init
ip_vs_init(void)
2069 ret
= ip_vs_control_init();
2071 pr_err("can't setup control.\n");
2075 ip_vs_protocol_init();
2077 ret
= ip_vs_conn_init();
2079 pr_err("can't setup connection table.\n");
2080 goto cleanup_protocol
;
2083 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2087 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2091 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2093 pr_err("can't register hooks.\n");
2097 ret
= ip_vs_register_nl_ioctl();
2099 pr_err("can't register netlink/ioctl.\n");
2103 pr_info("ipvs loaded.\n");
2108 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2110 unregister_pernet_device(&ipvs_core_dev_ops
);
2112 unregister_pernet_subsys(&ipvs_core_ops
);
2114 ip_vs_conn_cleanup();
2116 ip_vs_protocol_cleanup();
2117 ip_vs_control_cleanup();
2122 static void __exit
ip_vs_cleanup(void)
2124 ip_vs_unregister_nl_ioctl();
2125 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2126 unregister_pernet_device(&ipvs_core_dev_ops
);
2127 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2128 ip_vs_conn_cleanup();
2129 ip_vs_protocol_cleanup();
2130 ip_vs_control_cleanup();
2131 pr_info("ipvs unloaded.\n");
2134 module_init(ip_vs_init
);
2135 module_exit(ip_vs_cleanup
);
2136 MODULE_LICENSE("GPL");