1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS An implementation of the IP virtual server support for the
4 * LINUX operating system. IPVS is now implemented as a module
5 * over the Netfilter framework. IPVS can be used to build a
6 * high-performance and highly available server based on a
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
13 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
14 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
18 * Paul `Rusty' Russell properly handle non-linear skbs
19 * Harald Welte don't use nfcache
22 #define KMSG_COMPONENT "IPVS"
23 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/tcp.h>
29 #include <linux/sctp.h>
30 #include <linux/icmp.h>
31 #include <linux/slab.h>
36 #include <net/icmp.h> /* for icmp_send */
39 #include <net/route.h>
40 #include <net/ip6_checksum.h>
41 #include <net/netns/generic.h> /* net_generic() */
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_ipv4.h>
46 #ifdef CONFIG_IP_VS_IPV6
48 #include <linux/netfilter_ipv6.h>
49 #include <net/ip6_route.h>
52 #include <net/ip_vs.h>
53 #include <linux/indirect_call_wrapper.h>
56 EXPORT_SYMBOL(register_ip_vs_scheduler);
57 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
58 EXPORT_SYMBOL(ip_vs_proto_name);
59 EXPORT_SYMBOL(ip_vs_conn_new);
60 EXPORT_SYMBOL(ip_vs_conn_in_get);
61 EXPORT_SYMBOL(ip_vs_conn_out_get);
62 #ifdef CONFIG_IP_VS_PROTO_TCP
63 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
65 EXPORT_SYMBOL(ip_vs_conn_put);
66 #ifdef CONFIG_IP_VS_DEBUG
67 EXPORT_SYMBOL(ip_vs_get_debug_level);
69 EXPORT_SYMBOL(ip_vs_new_conn_out);
71 #ifdef CONFIG_IP_VS_PROTO_TCP
72 INDIRECT_CALLABLE_DECLARE(int
73 tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
74 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
77 #ifdef CONFIG_IP_VS_PROTO_UDP
78 INDIRECT_CALLABLE_DECLARE(int
79 udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
80 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
83 #if defined(CONFIG_IP_VS_PROTO_TCP) && defined(CONFIG_IP_VS_PROTO_UDP)
84 #define SNAT_CALL(f, ...) \
85 INDIRECT_CALL_2(f, tcp_snat_handler, udp_snat_handler, __VA_ARGS__)
86 #elif defined(CONFIG_IP_VS_PROTO_TCP)
87 #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, tcp_snat_handler, __VA_ARGS__)
88 #elif defined(CONFIG_IP_VS_PROTO_UDP)
89 #define SNAT_CALL(f, ...) INDIRECT_CALL_1(f, udp_snat_handler, __VA_ARGS__)
91 #define SNAT_CALL(f, ...) f(__VA_ARGS__)
94 static unsigned int ip_vs_net_id __read_mostly;
95 /* netns cnt used for uniqueness */
96 static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
98 /* ID used in ICMP lookups */
99 #define icmp_id(icmph) (((icmph)->un).echo.id)
100 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
102 const char *ip_vs_proto_name(unsigned int proto)
117 #ifdef CONFIG_IP_VS_IPV6
122 sprintf(buf, "IP_%u", proto);
127 void ip_vs_init_hash_table(struct list_head *table, int rows)
130 INIT_LIST_HEAD(&table[rows]);
134 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
136 struct ip_vs_dest *dest = cp->dest;
137 struct netns_ipvs *ipvs = cp->ipvs;
139 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
140 struct ip_vs_cpu_stats *s;
141 struct ip_vs_service *svc;
145 s = this_cpu_ptr(dest->stats.cpustats);
146 u64_stats_update_begin(&s->syncp);
148 s->cnt.inbytes += skb->len;
149 u64_stats_update_end(&s->syncp);
151 svc = rcu_dereference(dest->svc);
152 s = this_cpu_ptr(svc->stats.cpustats);
153 u64_stats_update_begin(&s->syncp);
155 s->cnt.inbytes += skb->len;
156 u64_stats_update_end(&s->syncp);
158 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
159 u64_stats_update_begin(&s->syncp);
161 s->cnt.inbytes += skb->len;
162 u64_stats_update_end(&s->syncp);
170 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
172 struct ip_vs_dest *dest = cp->dest;
173 struct netns_ipvs *ipvs = cp->ipvs;
175 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
176 struct ip_vs_cpu_stats *s;
177 struct ip_vs_service *svc;
181 s = this_cpu_ptr(dest->stats.cpustats);
182 u64_stats_update_begin(&s->syncp);
184 s->cnt.outbytes += skb->len;
185 u64_stats_update_end(&s->syncp);
187 svc = rcu_dereference(dest->svc);
188 s = this_cpu_ptr(svc->stats.cpustats);
189 u64_stats_update_begin(&s->syncp);
191 s->cnt.outbytes += skb->len;
192 u64_stats_update_end(&s->syncp);
194 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
195 u64_stats_update_begin(&s->syncp);
197 s->cnt.outbytes += skb->len;
198 u64_stats_update_end(&s->syncp);
206 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
208 struct netns_ipvs *ipvs = svc->ipvs;
209 struct ip_vs_cpu_stats *s;
213 s = this_cpu_ptr(cp->dest->stats.cpustats);
214 u64_stats_update_begin(&s->syncp);
216 u64_stats_update_end(&s->syncp);
218 s = this_cpu_ptr(svc->stats.cpustats);
219 u64_stats_update_begin(&s->syncp);
221 u64_stats_update_end(&s->syncp);
223 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
224 u64_stats_update_begin(&s->syncp);
226 u64_stats_update_end(&s->syncp);
233 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
234 const struct sk_buff *skb,
235 struct ip_vs_proto_data *pd)
237 if (likely(pd->pp->state_transition))
238 pd->pp->state_transition(cp, direction, skb, pd);
242 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
243 struct sk_buff *skb, int protocol,
244 const union nf_inet_addr *caddr, __be16 cport,
245 const union nf_inet_addr *vaddr, __be16 vport,
246 struct ip_vs_conn_param *p)
248 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
250 p->pe = rcu_dereference(svc->pe);
251 if (p->pe && p->pe->fill_param)
252 return p->pe->fill_param(p, skb);
258 * IPVS persistent scheduling function
259 * It creates a connection entry according to its template if exists,
260 * or selects a server and creates a connection entry plus a template.
261 * Locking: we are svc user (svc->refcnt), so we hold all dests too
262 * Protocols supported: TCP, UDP
264 static struct ip_vs_conn *
265 ip_vs_sched_persist(struct ip_vs_service *svc,
266 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
267 int *ignored, struct ip_vs_iphdr *iph)
269 struct ip_vs_conn *cp = NULL;
270 struct ip_vs_dest *dest;
271 struct ip_vs_conn *ct;
272 __be16 dport = 0; /* destination port to forward */
274 struct ip_vs_conn_param param;
275 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
276 union nf_inet_addr snet; /* source network of the client,
278 const union nf_inet_addr *src_addr, *dst_addr;
280 if (likely(!ip_vs_iph_inverse(iph))) {
281 src_addr = &iph->saddr;
282 dst_addr = &iph->daddr;
284 src_addr = &iph->daddr;
285 dst_addr = &iph->saddr;
289 /* Mask saddr with the netmask to adjust template granularity */
290 #ifdef CONFIG_IP_VS_IPV6
291 if (svc->af == AF_INET6)
292 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
293 (__force __u32) svc->netmask);
296 snet.ip = src_addr->ip & svc->netmask;
298 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
300 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
301 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
302 IP_VS_DBG_ADDR(svc->af, &snet));
305 * As far as we know, FTP is a very complicated network protocol, and
306 * it uses control connection and data connections. For active FTP,
307 * FTP server initialize data connection to the client, its source port
308 * is often 20. For passive FTP, FTP server tells the clients the port
309 * that it passively listens to, and the client issues the data
310 * connection. In the tunneling or direct routing mode, the load
311 * balancer is on the client-to-server half of connection, the port
312 * number is unknown to the load balancer. So, a conn template like
313 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
314 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
315 * is created for other persistent services.
318 int protocol = iph->protocol;
319 const union nf_inet_addr *vaddr = dst_addr;
322 if (dst_port == svc->port) {
324 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
326 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
328 if (svc->port != FTPPORT)
331 /* Note: persistent fwmark-based services and
332 * persistent port zero service are handled here.
334 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
335 * port zero template:
336 * <protocol,caddr,0,vaddr,0,daddr,0>
339 protocol = IPPROTO_IP;
343 /* return *ignored = -1 so NF_DROP can be used */
344 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
345 vaddr, vport, ¶m) < 0) {
351 /* Check if a template already exists */
352 ct = ip_vs_ct_in_get(¶m);
353 if (!ct || !ip_vs_check_template(ct, NULL)) {
354 struct ip_vs_scheduler *sched;
357 * No template found or the dest of the connection
358 * template is not available.
359 * return *ignored=0 i.e. ICMP and NF_DROP
361 sched = rcu_dereference(svc->scheduler);
363 /* read svc->sched_data after svc->scheduler */
365 dest = sched->schedule(svc, skb, iph);
370 IP_VS_DBG(1, "p-schedule: no dest found.\n");
371 kfree(param.pe_data);
376 if (dst_port == svc->port && svc->port != FTPPORT)
380 * This adds param.pe_data to the template,
381 * and thus param.pe_data will be destroyed
382 * when the template expires */
383 ct = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport,
384 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
386 kfree(param.pe_data);
391 ct->timeout = svc->timeout;
393 /* set destination with the found template */
395 kfree(param.pe_data);
399 if (dport == svc->port && dest->port)
402 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
403 && iph->protocol == IPPROTO_UDP) ?
404 IP_VS_CONN_F_ONE_PACKET : 0;
407 * Create a new connection according to the template
409 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
410 src_port, dst_addr, dst_port, ¶m);
412 cp = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, flags, dest,
423 ip_vs_control_add(cp, ct);
426 ip_vs_conn_stats(cp, svc);
432 * IPVS main scheduling function
433 * It selects a server according to the virtual service, and
434 * creates a connection entry.
435 * Protocols supported: TCP, UDP
439 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
440 * svc/scheduler decides that this packet should be accepted with
441 * NF_ACCEPT because it must not be scheduled.
443 * 0 : scheduler can not find destination, so try bypass or
444 * return ICMP and then NF_DROP (ip_vs_leave).
446 * -1 : scheduler tried to schedule but fatal error occurred, eg.
447 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
448 * failure such as missing Call-ID, ENOMEM on skb_linearize
449 * or pe_data. In this case we should return NF_DROP without
450 * any attempts to send ICMP with ip_vs_leave.
453 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
454 struct ip_vs_proto_data *pd, int *ignored,
455 struct ip_vs_iphdr *iph)
457 struct ip_vs_protocol *pp = pd->pp;
458 struct ip_vs_conn *cp = NULL;
459 struct ip_vs_scheduler *sched;
460 struct ip_vs_dest *dest;
461 __be16 _ports[2], *pptr, cport, vport;
462 const void *caddr, *vaddr;
467 * IPv6 frags, only the first hit here.
469 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
473 if (likely(!ip_vs_iph_inverse(iph))) {
486 * FTPDATA needs this check when using local real server.
487 * Never schedule Active FTPDATA connections from real server.
488 * For LVS-NAT they must be already created. For other methods
489 * with persistence the connection is created on SYN+ACK.
491 if (cport == FTPDATA) {
492 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
493 "Not scheduling FTPDATA");
498 * Do not schedule replies from local real server.
500 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
501 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
502 cp = INDIRECT_CALL_1(pp->conn_in_get,
503 ip_vs_conn_in_get_proto, svc->ipvs,
505 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
508 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
509 "Not scheduling reply for existing"
511 __ip_vs_conn_put(cp);
519 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
520 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
526 * Non-persistent service
528 if (!svc->fwmark && vport != svc->port) {
530 pr_err("Schedule: port zero only supported "
531 "in persistent services, "
532 "check your ipvs configuration\n");
536 sched = rcu_dereference(svc->scheduler);
538 /* read svc->sched_data after svc->scheduler */
540 dest = sched->schedule(svc, skb, iph);
545 IP_VS_DBG(1, "Schedule: no dest found.\n");
549 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
550 && iph->protocol == IPPROTO_UDP) ?
551 IP_VS_CONN_F_ONE_PACKET : 0;
554 * Create a connection entry.
557 struct ip_vs_conn_param p;
559 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
560 caddr, cport, vaddr, vport, &p);
561 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
562 dest->port ? dest->port : vport,
563 flags, dest, skb->mark);
570 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
571 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
573 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
574 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
575 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
576 cp->flags, refcount_read(&cp->refcnt));
578 ip_vs_conn_stats(cp, svc);
582 static inline int ip_vs_addr_is_unicast(struct net *net, int af,
583 union nf_inet_addr *addr)
585 #ifdef CONFIG_IP_VS_IPV6
587 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
589 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
593 * Pass or drop the packet.
594 * Called by ip_vs_in, when the virtual service is available but
595 * no destination is available for a new connection.
597 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
598 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
600 __be16 _ports[2], *pptr, dport;
601 struct netns_ipvs *ipvs = svc->ipvs;
602 struct net *net = ipvs->net;
604 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
607 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
609 /* if it is fwmark-based service, the cache_bypass sysctl is up
610 and the destination is a non-local unicast, then create
611 a cache_bypass connection entry */
612 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
613 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
614 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
616 struct ip_vs_conn *cp;
617 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
618 iph->protocol == IPPROTO_UDP) ?
619 IP_VS_CONN_F_ONE_PACKET : 0;
620 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
622 /* create a new connection entry */
623 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
625 struct ip_vs_conn_param p;
626 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
627 &iph->saddr, pptr[0],
628 &iph->daddr, pptr[1], &p);
629 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
630 IP_VS_CONN_F_BYPASS | flags,
637 ip_vs_in_stats(cp, skb);
640 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
642 /* transmit the first SYN packet */
643 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
644 /* do not touch skb anymore */
646 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
647 atomic_inc(&cp->control->in_pkts);
649 atomic_inc(&cp->in_pkts);
655 * When the virtual ftp service is presented, packets destined
656 * for other services on the VIP may get here (except services
657 * listed in the ipvs table), pass the packets, because it is
658 * not ipvs job to decide to drop the packets.
660 if (svc->port == FTPPORT && dport != FTPPORT)
663 if (unlikely(ip_vs_iph_icmp(iph)))
667 * Notify the client that the destination is unreachable, and
668 * release the socket buffer.
669 * Since it is in IP layer, the TCP socket is not actually
670 * created, the TCP RST packet cannot be sent, instead that
671 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
673 #ifdef CONFIG_IP_VS_IPV6
674 if (svc->af == AF_INET6) {
676 skb->dev = net->loopback_dev;
677 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
680 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
687 static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
689 return ipvs->sysctl_snat_reroute;
692 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
694 return ipvs->sysctl_nat_icmp_send;
697 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
699 return ipvs->sysctl_expire_nodest_conn;
704 static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
705 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
706 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
710 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
712 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
715 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
717 if (NF_INET_LOCAL_IN == hooknum)
718 return IP_DEFRAG_VS_IN;
719 if (NF_INET_FORWARD == hooknum)
720 return IP_DEFRAG_VS_FWD;
721 return IP_DEFRAG_VS_OUT;
724 static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
725 struct sk_buff *skb, u_int32_t user)
730 err = ip_defrag(ipvs->net, skb, user);
733 ip_send_check(ip_hdr(skb));
738 static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
739 struct sk_buff *skb, unsigned int hooknum)
741 if (!sysctl_snat_reroute(ipvs))
743 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
744 if (NF_INET_LOCAL_IN == hooknum)
746 #ifdef CONFIG_IP_VS_IPV6
747 if (af == AF_INET6) {
748 struct dst_entry *dst = skb_dst(skb);
750 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
751 ip6_route_me_harder(ipvs->net, skb) != 0)
755 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
756 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
763 * Packet has been made sufficiently writable in caller
764 * - inout: 1=in->out, 0=out->in
766 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
767 struct ip_vs_conn *cp, int inout)
769 struct iphdr *iph = ip_hdr(skb);
770 unsigned int icmp_offset = iph->ihl*4;
771 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
773 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
776 iph->saddr = cp->vaddr.ip;
778 ciph->daddr = cp->vaddr.ip;
781 iph->daddr = cp->daddr.ip;
783 ciph->saddr = cp->daddr.ip;
787 /* the TCP/UDP/SCTP port */
788 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
789 IPPROTO_SCTP == ciph->protocol) {
790 __be16 *ports = (void *)ciph + ciph->ihl*4;
793 ports[1] = cp->vport;
795 ports[0] = cp->dport;
798 /* And finally the ICMP checksum */
800 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
801 skb->ip_summed = CHECKSUM_UNNECESSARY;
804 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
805 "Forwarding altered outgoing ICMP");
807 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
808 "Forwarding altered incoming ICMP");
811 #ifdef CONFIG_IP_VS_IPV6
812 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
813 struct ip_vs_conn *cp, int inout)
815 struct ipv6hdr *iph = ipv6_hdr(skb);
816 unsigned int icmp_offset = 0;
817 unsigned int offs = 0; /* header offset*/
819 struct icmp6hdr *icmph;
820 struct ipv6hdr *ciph;
821 unsigned short fragoffs;
823 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
824 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
825 offs = icmp_offset + sizeof(struct icmp6hdr);
826 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
828 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
831 iph->saddr = cp->vaddr.in6;
832 ciph->daddr = cp->vaddr.in6;
834 iph->daddr = cp->daddr.in6;
835 ciph->saddr = cp->daddr.in6;
838 /* the TCP/UDP/SCTP port */
839 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
840 IPPROTO_SCTP == protocol)) {
841 __be16 *ports = (void *)(skb_network_header(skb) + offs);
843 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
844 ntohs(inout ? ports[1] : ports[0]),
845 ntohs(inout ? cp->vport : cp->dport));
847 ports[1] = cp->vport;
849 ports[0] = cp->dport;
852 /* And finally the ICMP checksum */
853 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
854 skb->len - icmp_offset,
856 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
857 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
858 skb->ip_summed = CHECKSUM_PARTIAL;
861 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
862 (void *)ciph - (void *)iph,
863 "Forwarding altered outgoing ICMPv6");
865 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
866 (void *)ciph - (void *)iph,
867 "Forwarding altered incoming ICMPv6");
871 /* Handle relevant response ICMP messages - forward to the right
874 static int handle_response_icmp(int af, struct sk_buff *skb,
875 union nf_inet_addr *snet,
876 __u8 protocol, struct ip_vs_conn *cp,
877 struct ip_vs_protocol *pp,
878 unsigned int offset, unsigned int ihl,
879 unsigned int hooknum)
881 unsigned int verdict = NF_DROP;
883 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
886 /* Ensure the checksum is correct */
887 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
888 /* Failed checksum! */
889 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
890 IP_VS_DBG_ADDR(af, snet));
894 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
895 IPPROTO_SCTP == protocol)
896 offset += 2 * sizeof(__u16);
897 if (skb_ensure_writable(skb, offset))
900 #ifdef CONFIG_IP_VS_IPV6
902 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
905 ip_vs_nat_icmp(skb, pp, cp, 1);
907 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
910 /* do the statistics and put it back */
911 ip_vs_out_stats(cp, skb);
913 skb->ipvs_property = 1;
914 if (!(cp->flags & IP_VS_CONN_F_NFCT))
917 ip_vs_update_conntrack(skb, cp, 0);
923 __ip_vs_conn_put(cp);
929 * Handle ICMP messages in the inside-to-outside direction (outgoing).
930 * Find any that might be relevant, check against existing connections.
931 * Currently handles error types - unreachable, quench, ttl exceeded.
933 static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
934 int *related, unsigned int hooknum)
937 struct icmphdr _icmph, *ic;
938 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
939 struct ip_vs_iphdr ciph;
940 struct ip_vs_conn *cp;
941 struct ip_vs_protocol *pp;
942 unsigned int offset, ihl;
943 union nf_inet_addr snet;
947 /* reassemble IP fragments */
948 if (ip_is_fragment(ip_hdr(skb))) {
949 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
954 offset = ihl = iph->ihl * 4;
955 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
959 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
960 ic->type, ntohs(icmp_id(ic)),
961 &iph->saddr, &iph->daddr);
964 * Work through seeing if this is for us.
965 * These checks are supposed to be in an order that means easy
966 * things are checked first to speed up processing.... however
967 * this means that some packets will manage to get a long way
968 * down this stack and then be rejected, but that's life.
970 if ((ic->type != ICMP_DEST_UNREACH) &&
971 (ic->type != ICMP_SOURCE_QUENCH) &&
972 (ic->type != ICMP_TIME_EXCEEDED)) {
977 /* Now find the contained IP header */
978 offset += sizeof(_icmph);
979 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
981 return NF_ACCEPT; /* The packet looks wrong, ignore */
983 pp = ip_vs_proto_get(cih->protocol);
987 /* Is the embedded protocol header present? */
988 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
992 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
993 "Checking outgoing ICMP for");
995 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
997 /* The embedded headers contain source and dest in reverse order */
998 cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
999 ipvs, AF_INET, skb, &ciph);
1003 snet.ip = iph->saddr;
1004 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
1005 pp, ciph.len, ihl, hooknum);
1008 #ifdef CONFIG_IP_VS_IPV6
1009 static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1010 int *related, unsigned int hooknum,
1011 struct ip_vs_iphdr *ipvsh)
1013 struct icmp6hdr _icmph, *ic;
1014 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1015 struct ip_vs_conn *cp;
1016 struct ip_vs_protocol *pp;
1017 union nf_inet_addr snet;
1018 unsigned int offset;
1021 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph);
1026 * Work through seeing if this is for us.
1027 * These checks are supposed to be in an order that means easy
1028 * things are checked first to speed up processing.... however
1029 * this means that some packets will manage to get a long way
1030 * down this stack and then be rejected, but that's life.
1032 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1036 /* Fragment header that is before ICMP header tells us that:
1037 * it's not an error message since they can't be fragmented.
1039 if (ipvsh->flags & IP6_FH_F_FRAG)
1042 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1043 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1044 &ipvsh->saddr, &ipvsh->daddr);
1046 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1048 return NF_ACCEPT; /* The packet looks wrong, ignore */
1050 pp = ip_vs_proto_get(ciph.protocol);
1054 /* The embedded headers contain source and dest in reverse order */
1055 cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
1056 ipvs, AF_INET6, skb, &ciph);
1060 snet.in6 = ciph.saddr.in6;
1062 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1063 pp, offset, sizeof(struct ipv6hdr),
1069 * Check if sctp chunc is ABORT chunk
1071 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1073 struct sctp_chunkhdr *sch, schunk;
1074 sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr),
1075 sizeof(schunk), &schunk);
1078 if (sch->type == SCTP_CID_ABORT)
1083 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1085 struct tcphdr _tcph, *th;
1087 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1093 static inline bool is_new_conn(const struct sk_buff *skb,
1094 struct ip_vs_iphdr *iph)
1096 switch (iph->protocol) {
1098 struct tcphdr _tcph, *th;
1100 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1105 case IPPROTO_SCTP: {
1106 struct sctp_chunkhdr *sch, schunk;
1108 sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr),
1109 sizeof(schunk), &schunk);
1112 return sch->type == SCTP_CID_INIT;
1119 static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1120 int conn_reuse_mode)
1122 /* Controlled (FTP DATA or persistence)? */
1126 switch (cp->protocol) {
1128 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1129 (cp->state == IP_VS_TCP_S_CLOSE) ||
1130 ((conn_reuse_mode & 2) &&
1131 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1132 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1134 return cp->state == IP_VS_SCTP_S_CLOSED;
1140 /* Generic function to create new connections for outgoing RS packets
1142 * Pre-requisites for successful connection creation:
1143 * 1) Virtual Service is NOT fwmark based:
1144 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1145 * 2) Real Server and Virtual Service were NOT configured without port:
1146 * This is to allow match of different VS to the same RS ip-addr
1148 struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1149 struct ip_vs_dest *dest,
1150 struct sk_buff *skb,
1151 const struct ip_vs_iphdr *iph,
1155 struct ip_vs_conn_param param;
1156 struct ip_vs_conn *ct = NULL, *cp = NULL;
1157 const union nf_inet_addr *vaddr, *daddr, *caddr;
1158 union nf_inet_addr snet;
1165 daddr = &iph->saddr;
1166 caddr = &iph->daddr;
1168 /* check pre-requisites are satisfied */
1171 if (!vport || !dport)
1174 /* for persistent service first create connection template */
1175 if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
1176 /* apply netmask the same way ingress-side does */
1177 #ifdef CONFIG_IP_VS_IPV6
1178 if (svc->af == AF_INET6)
1179 ipv6_addr_prefix(&snet.in6, &caddr->in6,
1180 (__force __u32)svc->netmask);
1183 snet.ip = caddr->ip & svc->netmask;
1184 /* fill params and create template if not existent */
1185 if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
1189 ct = ip_vs_ct_in_get(¶m);
1190 /* check if template exists and points to the same dest */
1191 if (!ct || !ip_vs_check_template(ct, dest)) {
1192 ct = ip_vs_conn_new(¶m, dest->af, daddr, dport,
1193 IP_VS_CONN_F_TEMPLATE, dest, 0);
1195 kfree(param.pe_data);
1198 ct->timeout = svc->timeout;
1200 kfree(param.pe_data);
1204 /* connection flags */
1205 flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
1206 iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
1207 /* create connection */
1208 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
1209 caddr, cport, vaddr, vport, ¶m);
1210 cp = ip_vs_conn_new(¶m, dest->af, daddr, dport, flags, dest, 0);
1217 ip_vs_control_add(cp, ct);
1220 ip_vs_conn_stats(cp, svc);
1222 /* return connection (will be used to handle outgoing packet) */
1223 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1224 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1226 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
1227 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
1228 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
1229 cp->flags, refcount_read(&cp->refcnt));
1234 /* Handle outgoing packets which are considered requests initiated by
1235 * real servers, so that subsequent responses from external client can be
1236 * routed to the right real server.
1237 * Used also for outgoing responses in OPS mode.
1239 * Connection management is handled by persistent-engine specific callback.
1241 static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
1242 struct netns_ipvs *ipvs,
1243 int af, struct sk_buff *skb,
1244 const struct ip_vs_iphdr *iph)
1246 struct ip_vs_dest *dest;
1247 struct ip_vs_conn *cp = NULL;
1248 __be16 _ports[2], *pptr;
1250 if (hooknum == NF_INET_LOCAL_IN)
1253 pptr = frag_safe_skb_hp(skb, iph->len,
1254 sizeof(_ports), _ports);
1258 dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
1259 &iph->saddr, pptr[0]);
1261 struct ip_vs_service *svc;
1262 struct ip_vs_pe *pe;
1264 svc = rcu_dereference(dest->svc);
1266 pe = rcu_dereference(svc->pe);
1267 if (pe && pe->conn_out)
1268 cp = pe->conn_out(svc, dest, skb, iph,
1276 /* Handle response packets: rewrite addresses and send away...
1279 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1280 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1281 unsigned int hooknum)
1283 struct ip_vs_protocol *pp = pd->pp;
1285 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
1287 if (skb_ensure_writable(skb, iph->len))
1290 /* mangle the packet */
1291 if (pp->snat_handler &&
1292 !SNAT_CALL(pp->snat_handler, skb, pp, cp, iph))
1295 #ifdef CONFIG_IP_VS_IPV6
1297 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1301 ip_hdr(skb)->saddr = cp->vaddr.ip;
1302 ip_send_check(ip_hdr(skb));
1306 * nf_iterate does not expect change in the skb->dst->dev.
1307 * It looks like it is not fatal to enable this code for hooks
1308 * where our handlers are at the end of the chain list and
1309 * when all next handlers use skb->dst->dev and not outdev.
1310 * It will definitely route properly the inout NAT traffic
1311 * when multiple paths are used.
1314 /* For policy routing, packets originating from this
1315 * machine itself may be routed differently to packets
1316 * passing through. We want this packet to be routed as
1317 * if it came from this machine itself. So re-compute
1318 * the routing information.
1320 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
1323 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
1325 ip_vs_out_stats(cp, skb);
1326 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1327 skb->ipvs_property = 1;
1328 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1331 ip_vs_update_conntrack(skb, cp, 0);
1345 * Check if outgoing packet belongs to the established ip_vs_conn.
1348 ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1350 struct ip_vs_iphdr iph;
1351 struct ip_vs_protocol *pp;
1352 struct ip_vs_proto_data *pd;
1353 struct ip_vs_conn *cp;
1358 /* Already marked as IPVS request or reply? */
1359 if (skb->ipvs_property)
1362 sk = skb_to_full_sk(skb);
1363 /* Bad... Do not break raw sockets */
1364 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
1367 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
1371 if (unlikely(!skb_dst(skb)))
1377 ip_vs_fill_iph_skb(af, skb, false, &iph);
1378 #ifdef CONFIG_IP_VS_IPV6
1379 if (af == AF_INET6) {
1380 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1382 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
1390 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1392 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
1398 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1403 /* reassemble IP fragments */
1404 #ifdef CONFIG_IP_VS_IPV6
1407 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1408 if (ip_vs_gather_frags(ipvs, skb,
1409 ip_vs_defrag_user(hooknum)))
1412 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
1416 * Check if the packet belongs to an existing entry
1418 cp = INDIRECT_CALL_1(pp->conn_out_get, ip_vs_conn_out_get_proto,
1419 ipvs, af, skb, &iph);
1422 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
1424 return handle_response(af, skb, pd, cp, &iph, hooknum);
1427 /* Check for real-server-started requests */
1428 if (atomic_read(&ipvs->conn_out_counter)) {
1429 /* Currently only for UDP:
1430 * connection oriented protocols typically use
1431 * ephemeral ports for outgoing connections, so
1432 * related incoming responses would not match any VS
1434 if (pp->protocol == IPPROTO_UDP) {
1435 cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
1437 return handle_response(af, skb, pd, cp, &iph,
1442 if (sysctl_nat_icmp_send(ipvs) &&
1443 (pp->protocol == IPPROTO_TCP ||
1444 pp->protocol == IPPROTO_UDP ||
1445 pp->protocol == IPPROTO_SCTP)) {
1446 __be16 _ports[2], *pptr;
1448 pptr = frag_safe_skb_hp(skb, iph.len,
1449 sizeof(_ports), _ports);
1451 return NF_ACCEPT; /* Not for me */
1452 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
1455 * Notify the real server: there is no
1456 * existing entry if it is not RST
1457 * packet or not TCP packet.
1459 if ((iph.protocol != IPPROTO_TCP &&
1460 iph.protocol != IPPROTO_SCTP)
1461 || ((iph.protocol == IPPROTO_TCP
1462 && !is_tcp_reset(skb, iph.len))
1463 || (iph.protocol == IPPROTO_SCTP
1464 && !is_sctp_abort(skb,
1466 #ifdef CONFIG_IP_VS_IPV6
1467 if (af == AF_INET6) {
1469 skb->dev = ipvs->net->loopback_dev;
1471 ICMPV6_DEST_UNREACH,
1472 ICMPV6_PORT_UNREACH,
1478 ICMP_PORT_UNREACH, 0);
1485 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
1486 "ip_vs_out: packet continues traversal as normal");
1490 __ip_vs_conn_put(cp);
1495 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1496 * used only for VS/NAT.
1497 * Check if packet is reply for established ip_vs_conn.
1500 ip_vs_reply4(void *priv, struct sk_buff *skb,
1501 const struct nf_hook_state *state)
1503 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1507 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1508 * Check if packet is reply for established ip_vs_conn.
1511 ip_vs_local_reply4(void *priv, struct sk_buff *skb,
1512 const struct nf_hook_state *state)
1514 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1517 #ifdef CONFIG_IP_VS_IPV6
1520 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1521 * used only for VS/NAT.
1522 * Check if packet is reply for established ip_vs_conn.
1525 ip_vs_reply6(void *priv, struct sk_buff *skb,
1526 const struct nf_hook_state *state)
1528 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1532 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1533 * Check if packet is reply for established ip_vs_conn.
1536 ip_vs_local_reply6(void *priv, struct sk_buff *skb,
1537 const struct nf_hook_state *state)
1539 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1545 ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1546 struct ip_vs_proto_data *pd,
1547 int *verdict, struct ip_vs_conn **cpp,
1548 struct ip_vs_iphdr *iph)
1550 struct ip_vs_protocol *pp = pd->pp;
1552 if (!iph->fragoffs) {
1553 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1554 * replayed fragment zero will already have created the cp
1557 /* Schedule and create new connection entry into cpp */
1558 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
1562 if (unlikely(!*cpp)) {
1563 /* sorry, all this trouble for a no-hit :) */
1564 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1565 "ip_vs_in: packet continues traversal as normal");
1567 /* Fragment couldn't be mapped to a conn entry */
1569 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1570 "unhandled fragment");
1572 *verdict = NF_ACCEPT;
1579 /* Check the UDP tunnel and return its header length */
1580 static int ipvs_udp_decap(struct netns_ipvs *ipvs, struct sk_buff *skb,
1581 unsigned int offset, __u16 af,
1582 const union nf_inet_addr *daddr, __u8 *proto)
1584 struct udphdr _udph, *udph;
1585 struct ip_vs_dest *dest;
1587 udph = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
1590 offset += sizeof(struct udphdr);
1591 dest = ip_vs_find_tunnel(ipvs, af, daddr, udph->dest);
1594 if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1595 struct guehdr _gueh, *gueh;
1597 gueh = skb_header_pointer(skb, offset, sizeof(_gueh), &_gueh);
1600 if (gueh->control != 0 || gueh->version != 0)
1602 /* Later we can support also IPPROTO_IPV6 */
1603 if (gueh->proto_ctype != IPPROTO_IPIP)
1605 *proto = gueh->proto_ctype;
1606 return sizeof(struct udphdr) + sizeof(struct guehdr) +
1614 /* Check the GRE tunnel and return its header length */
1615 static int ipvs_gre_decap(struct netns_ipvs *ipvs, struct sk_buff *skb,
1616 unsigned int offset, __u16 af,
1617 const union nf_inet_addr *daddr, __u8 *proto)
1619 struct gre_base_hdr _greh, *greh;
1620 struct ip_vs_dest *dest;
1622 greh = skb_header_pointer(skb, offset, sizeof(_greh), &_greh);
1625 dest = ip_vs_find_tunnel(ipvs, af, daddr, 0);
1628 if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
1631 /* Only support version 0 and C (csum) */
1632 if ((greh->flags & ~GRE_CSUM) != 0)
1634 type = greh->protocol;
1635 /* Later we can support also IPPROTO_IPV6 */
1636 if (type != htons(ETH_P_IP))
1638 *proto = IPPROTO_IPIP;
1639 return gre_calc_hlen(gre_flags_to_tnl_flags(greh->flags));
1647 * Handle ICMP messages in the outside-to-inside direction (incoming).
1648 * Find any that might be relevant, check against existing connections,
1649 * forward to the right destination host if relevant.
1650 * Currently handles error types - unreachable, quench, ttl exceeded.
1653 ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1654 unsigned int hooknum)
1657 struct icmphdr _icmph, *ic;
1658 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1659 struct ip_vs_iphdr ciph;
1660 struct ip_vs_conn *cp;
1661 struct ip_vs_protocol *pp;
1662 struct ip_vs_proto_data *pd;
1663 unsigned int offset, offset2, ihl, verdict;
1664 bool ipip, new_cp = false;
1665 union nf_inet_addr *raddr;
1669 /* reassemble IP fragments */
1670 if (ip_is_fragment(ip_hdr(skb))) {
1671 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
1676 offset = ihl = iph->ihl * 4;
1677 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1681 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1682 ic->type, ntohs(icmp_id(ic)),
1683 &iph->saddr, &iph->daddr);
1686 * Work through seeing if this is for us.
1687 * These checks are supposed to be in an order that means easy
1688 * things are checked first to speed up processing.... however
1689 * this means that some packets will manage to get a long way
1690 * down this stack and then be rejected, but that's life.
1692 if ((ic->type != ICMP_DEST_UNREACH) &&
1693 (ic->type != ICMP_SOURCE_QUENCH) &&
1694 (ic->type != ICMP_TIME_EXCEEDED)) {
1699 /* Now find the contained IP header */
1700 offset += sizeof(_icmph);
1701 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1703 return NF_ACCEPT; /* The packet looks wrong, ignore */
1704 raddr = (union nf_inet_addr *)&cih->daddr;
1706 /* Special case for errors for IPIP packets */
1708 if (cih->protocol == IPPROTO_IPIP) {
1709 struct ip_vs_dest *dest;
1711 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1713 /* Error for our IPIP must arrive at LOCAL_IN */
1714 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1716 dest = ip_vs_find_tunnel(ipvs, AF_INET, raddr, 0);
1717 /* Only for known tunnel */
1718 if (!dest || dest->tun_type != IP_VS_CONN_F_TUNNEL_TYPE_IPIP)
1720 offset += cih->ihl * 4;
1721 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1723 return NF_ACCEPT; /* The packet looks wrong, ignore */
1725 } else if ((cih->protocol == IPPROTO_UDP || /* Can be UDP encap */
1726 cih->protocol == IPPROTO_GRE) && /* Can be GRE encap */
1727 /* Error for our tunnel must arrive at LOCAL_IN */
1728 (skb_rtable(skb)->rt_flags & RTCF_LOCAL)) {
1732 /* Non-first fragment has no UDP header */
1733 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1735 offset2 = offset + cih->ihl * 4;
1736 if (cih->protocol == IPPROTO_UDP)
1737 ulen = ipvs_udp_decap(ipvs, skb, offset2, AF_INET,
1740 ulen = ipvs_gre_decap(ipvs, skb, offset2, AF_INET,
1743 /* Skip IP and UDP/GRE tunnel headers */
1744 offset = offset2 + ulen;
1745 /* Now we should be at the original IP header */
1746 cih = skb_header_pointer(skb, offset, sizeof(_ciph),
1748 if (cih && cih->version == 4 && cih->ihl >= 5 &&
1749 iproto == IPPROTO_IPIP)
1756 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
1761 /* Is the embedded protocol header present? */
1762 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1766 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1767 "Checking incoming ICMP for");
1770 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
1773 /* The embedded headers contain source and dest in reverse order.
1774 * For IPIP this is error for request, not for reply.
1776 cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
1777 ipvs, AF_INET, skb, &ciph);
1782 if (ipip || !sysctl_schedule_icmp(ipvs))
1785 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
1792 /* Ensure the checksum is correct */
1793 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1794 /* Failed checksum! */
1795 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1801 __be32 info = ic->un.gateway;
1802 __u8 type = ic->type;
1803 __u8 code = ic->code;
1805 /* Update the MTU */
1806 if (ic->type == ICMP_DEST_UNREACH &&
1807 ic->code == ICMP_FRAG_NEEDED) {
1808 struct ip_vs_dest *dest = cp->dest;
1809 u32 mtu = ntohs(ic->un.frag.mtu);
1810 __be16 frag_off = cih->frag_off;
1812 /* Strip outer IP and ICMP, go to IPIP header */
1813 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1815 offset2 -= ihl + sizeof(_icmph);
1816 skb_reset_network_header(skb);
1817 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1818 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1819 ipv4_update_pmtu(skb, ipvs->net, mtu, 0, 0);
1820 /* Client uses PMTUD? */
1821 if (!(frag_off & htons(IP_DF)))
1823 /* Prefer the resulting PMTU */
1825 struct ip_vs_dest_dst *dest_dst;
1827 dest_dst = rcu_dereference(dest->dest_dst);
1829 mtu = dst_mtu(dest_dst->dst_cache);
1831 if (mtu > 68 + sizeof(struct iphdr))
1832 mtu -= sizeof(struct iphdr);
1835 /* Strip outer IP, ICMP and IPIP, go to IP header of
1838 if (pskb_pull(skb, offset2) == NULL)
1840 skb_reset_network_header(skb);
1841 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1842 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1843 type, code, ntohl(info));
1844 icmp_send(skb, type, code, info);
1845 /* ICMP can be shorter but anyways, account it */
1846 ip_vs_out_stats(cp, skb);
1850 verdict = NF_STOLEN;
1854 /* do the statistics and put it back */
1855 ip_vs_in_stats(cp, skb);
1856 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1857 IPPROTO_SCTP == cih->protocol)
1858 offset += 2 * sizeof(__u16);
1859 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1862 if (likely(!new_cp))
1863 __ip_vs_conn_put(cp);
1870 #ifdef CONFIG_IP_VS_IPV6
1871 static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1872 int *related, unsigned int hooknum,
1873 struct ip_vs_iphdr *iph)
1875 struct icmp6hdr _icmph, *ic;
1876 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1877 struct ip_vs_conn *cp;
1878 struct ip_vs_protocol *pp;
1879 struct ip_vs_proto_data *pd;
1880 unsigned int offset, verdict;
1881 bool new_cp = false;
1885 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph);
1890 * Work through seeing if this is for us.
1891 * These checks are supposed to be in an order that means easy
1892 * things are checked first to speed up processing.... however
1893 * this means that some packets will manage to get a long way
1894 * down this stack and then be rejected, but that's life.
1896 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1900 /* Fragment header that is before ICMP header tells us that:
1901 * it's not an error message since they can't be fragmented.
1903 if (iph->flags & IP6_FH_F_FRAG)
1906 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1907 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1908 &iph->saddr, &iph->daddr);
1910 offset = iph->len + sizeof(_icmph);
1911 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
1914 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
1919 /* Cannot handle fragmented embedded protocol */
1923 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1924 "Checking incoming ICMPv6 for");
1926 /* The embedded headers contain source and dest in reverse order
1927 * if not from localhost
1929 cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
1930 ipvs, AF_INET6, skb, &ciph);
1935 if (!sysctl_schedule_icmp(ipvs))
1938 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
1944 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1945 if ((hooknum == NF_INET_LOCAL_OUT) &&
1946 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1947 verdict = NF_ACCEPT;
1951 /* do the statistics and put it back */
1952 ip_vs_in_stats(cp, skb);
1954 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1956 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1957 IPPROTO_SCTP == ciph.protocol)
1958 offset += 2 * sizeof(__u16); /* Also mangle ports */
1960 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
1963 if (likely(!new_cp))
1964 __ip_vs_conn_put(cp);
1974 * Check if it's for virtual services, look it up,
1975 * and send it on its way...
1978 ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1980 struct ip_vs_iphdr iph;
1981 struct ip_vs_protocol *pp;
1982 struct ip_vs_proto_data *pd;
1983 struct ip_vs_conn *cp;
1985 int conn_reuse_mode;
1988 /* Already marked as IPVS request or reply? */
1989 if (skb->ipvs_property)
1994 * - remote client: only PACKET_HOST
1995 * - route: used for struct net when skb->dev is unset
1997 if (unlikely((skb->pkt_type != PACKET_HOST &&
1998 hooknum != NF_INET_LOCAL_OUT) ||
2000 ip_vs_fill_iph_skb(af, skb, false, &iph);
2001 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
2002 " ignored in hook %u\n",
2003 skb->pkt_type, iph.protocol,
2004 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
2007 /* ipvs enabled in this netns ? */
2008 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2011 ip_vs_fill_iph_skb(af, skb, false, &iph);
2013 /* Bad... Do not break raw sockets */
2014 sk = skb_to_full_sk(skb);
2015 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
2018 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
2022 #ifdef CONFIG_IP_VS_IPV6
2023 if (af == AF_INET6) {
2024 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
2026 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
2034 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
2036 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
2043 /* Protocol supported? */
2044 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
2045 if (unlikely(!pd)) {
2046 /* The only way we'll see this packet again is if it's
2047 * encapsulated, so mark it with ipvs_property=1 so we
2048 * skip it if we're ignoring tunneled packets
2050 if (sysctl_ignore_tunneled(ipvs))
2051 skb->ipvs_property = 1;
2057 * Check if the packet belongs to an existing connection entry
2059 cp = INDIRECT_CALL_1(pp->conn_in_get, ip_vs_conn_in_get_proto,
2060 ipvs, af, skb, &iph);
2062 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
2063 if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
2064 bool uses_ct = false, resched = false;
2066 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
2067 unlikely(!atomic_read(&cp->dest->weight))) {
2069 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
2070 } else if (is_new_conn_expected(cp, conn_reuse_mode)) {
2071 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
2072 if (!atomic_read(&cp->n_control)) {
2075 /* Do not reschedule controlling connection
2076 * that uses conntrack while it is still
2077 * referenced by controlled connection(s).
2084 if (!atomic_read(&cp->n_control))
2085 ip_vs_conn_expire_now(cp);
2086 __ip_vs_conn_put(cp);
2093 if (unlikely(!cp)) {
2096 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
2100 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
2102 /* Check the server status */
2103 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
2104 /* the destination server is not available */
2106 __u32 flags = cp->flags;
2108 /* when timer already started, silently drop the packet.*/
2109 if (timer_pending(&cp->timer))
2110 __ip_vs_conn_put(cp);
2114 if (sysctl_expire_nodest_conn(ipvs) &&
2115 !(flags & IP_VS_CONN_F_ONE_PACKET)) {
2116 /* try to expire the connection immediately */
2117 ip_vs_conn_expire_now(cp);
2123 ip_vs_in_stats(cp, skb);
2124 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
2125 if (cp->packet_xmit)
2126 ret = cp->packet_xmit(skb, cp, pp, &iph);
2127 /* do not touch skb anymore */
2129 IP_VS_DBG_RL("warning: packet_xmit is null");
2133 /* Increase its packet counter and check if it is needed
2134 * to be synchronized
2136 * Sync connection if it is about to close to
2137 * encorage the standby servers to update the connections timeout
2139 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
2142 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
2143 pkts = sysctl_sync_threshold(ipvs);
2145 pkts = atomic_add_return(1, &cp->in_pkts);
2147 if (ipvs->sync_state & IP_VS_STATE_MASTER)
2148 ip_vs_sync_conn(ipvs, cp, pkts);
2149 else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
2150 /* increment is done inside ip_vs_sync_conn too */
2151 atomic_inc(&cp->control->in_pkts);
2158 * AF_INET handler in NF_INET_LOCAL_IN chain
2159 * Schedule and forward packets from remote clients
2162 ip_vs_remote_request4(void *priv, struct sk_buff *skb,
2163 const struct nf_hook_state *state)
2165 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
2169 * AF_INET handler in NF_INET_LOCAL_OUT chain
2170 * Schedule and forward packets from local clients
2173 ip_vs_local_request4(void *priv, struct sk_buff *skb,
2174 const struct nf_hook_state *state)
2176 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
2179 #ifdef CONFIG_IP_VS_IPV6
2182 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2183 * Schedule and forward packets from remote clients
2186 ip_vs_remote_request6(void *priv, struct sk_buff *skb,
2187 const struct nf_hook_state *state)
2189 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
2193 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2194 * Schedule and forward packets from local clients
2197 ip_vs_local_request6(void *priv, struct sk_buff *skb,
2198 const struct nf_hook_state *state)
2200 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
2207 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
2208 * related packets destined for 0.0.0.0/0.
2209 * When fwmark-based virtual service is used, such as transparent
2210 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2211 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
2212 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
2213 * and send them to ip_vs_in_icmp.
2216 ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
2217 const struct nf_hook_state *state)
2220 struct netns_ipvs *ipvs = net_ipvs(state->net);
2222 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
2225 /* ipvs enabled in this netns ? */
2226 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2229 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
2232 #ifdef CONFIG_IP_VS_IPV6
2234 ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
2235 const struct nf_hook_state *state)
2238 struct netns_ipvs *ipvs = net_ipvs(state->net);
2239 struct ip_vs_iphdr iphdr;
2241 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
2242 if (iphdr.protocol != IPPROTO_ICMPV6)
2245 /* ipvs enabled in this netns ? */
2246 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2249 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
2254 static const struct nf_hook_ops ip_vs_ops[] = {
2255 /* After packet filtering, change source only for VS/NAT */
2257 .hook = ip_vs_reply4,
2259 .hooknum = NF_INET_LOCAL_IN,
2260 .priority = NF_IP_PRI_NAT_SRC - 2,
2262 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2263 * or VS/NAT(change destination), so that filtering rules can be
2264 * applied to IPVS. */
2266 .hook = ip_vs_remote_request4,
2268 .hooknum = NF_INET_LOCAL_IN,
2269 .priority = NF_IP_PRI_NAT_SRC - 1,
2271 /* Before ip_vs_in, change source only for VS/NAT */
2273 .hook = ip_vs_local_reply4,
2275 .hooknum = NF_INET_LOCAL_OUT,
2276 .priority = NF_IP_PRI_NAT_DST + 1,
2278 /* After mangle, schedule and forward local requests */
2280 .hook = ip_vs_local_request4,
2282 .hooknum = NF_INET_LOCAL_OUT,
2283 .priority = NF_IP_PRI_NAT_DST + 2,
2285 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2286 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2288 .hook = ip_vs_forward_icmp,
2290 .hooknum = NF_INET_FORWARD,
2293 /* After packet filtering, change source only for VS/NAT */
2295 .hook = ip_vs_reply4,
2297 .hooknum = NF_INET_FORWARD,
2300 #ifdef CONFIG_IP_VS_IPV6
2301 /* After packet filtering, change source only for VS/NAT */
2303 .hook = ip_vs_reply6,
2305 .hooknum = NF_INET_LOCAL_IN,
2306 .priority = NF_IP6_PRI_NAT_SRC - 2,
2308 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2309 * or VS/NAT(change destination), so that filtering rules can be
2310 * applied to IPVS. */
2312 .hook = ip_vs_remote_request6,
2314 .hooknum = NF_INET_LOCAL_IN,
2315 .priority = NF_IP6_PRI_NAT_SRC - 1,
2317 /* Before ip_vs_in, change source only for VS/NAT */
2319 .hook = ip_vs_local_reply6,
2321 .hooknum = NF_INET_LOCAL_OUT,
2322 .priority = NF_IP6_PRI_NAT_DST + 1,
2324 /* After mangle, schedule and forward local requests */
2326 .hook = ip_vs_local_request6,
2328 .hooknum = NF_INET_LOCAL_OUT,
2329 .priority = NF_IP6_PRI_NAT_DST + 2,
2331 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2332 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2334 .hook = ip_vs_forward_icmp_v6,
2336 .hooknum = NF_INET_FORWARD,
2339 /* After packet filtering, change source only for VS/NAT */
2341 .hook = ip_vs_reply6,
2343 .hooknum = NF_INET_FORWARD,
2349 * Initialize IP Virtual Server netns mem.
2351 static int __net_init __ip_vs_init(struct net *net)
2353 struct netns_ipvs *ipvs;
2355 ipvs = net_generic(net, ip_vs_net_id);
2359 /* Hold the beast until a service is registerd */
2362 /* Counters used for creating unique names */
2363 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2364 atomic_inc(&ipvs_netns_cnt);
2367 if (ip_vs_estimator_net_init(ipvs) < 0)
2368 goto estimator_fail;
2370 if (ip_vs_control_net_init(ipvs) < 0)
2373 if (ip_vs_protocol_net_init(ipvs) < 0)
2376 if (ip_vs_app_net_init(ipvs) < 0)
2379 if (ip_vs_conn_net_init(ipvs) < 0)
2382 if (ip_vs_sync_net_init(ipvs) < 0)
2391 ip_vs_conn_net_cleanup(ipvs);
2393 ip_vs_app_net_cleanup(ipvs);
2395 ip_vs_protocol_net_cleanup(ipvs);
2397 ip_vs_control_net_cleanup(ipvs);
2399 ip_vs_estimator_net_cleanup(ipvs);
2405 static void __net_exit __ip_vs_cleanup_batch(struct list_head *net_list)
2407 struct netns_ipvs *ipvs;
2410 ip_vs_service_nets_cleanup(net_list); /* ip_vs_flush() with locks */
2411 list_for_each_entry(net, net_list, exit_list) {
2412 ipvs = net_ipvs(net);
2413 ip_vs_conn_net_cleanup(ipvs);
2414 ip_vs_app_net_cleanup(ipvs);
2415 ip_vs_protocol_net_cleanup(ipvs);
2416 ip_vs_control_net_cleanup(ipvs);
2417 ip_vs_estimator_net_cleanup(ipvs);
2418 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
2423 static int __net_init __ip_vs_dev_init(struct net *net)
2427 ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2436 static void __net_exit __ip_vs_dev_cleanup_batch(struct list_head *net_list)
2438 struct netns_ipvs *ipvs;
2442 list_for_each_entry(net, net_list, exit_list) {
2443 ipvs = net_ipvs(net);
2444 nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2445 ipvs->enable = 0; /* Disable packet reception */
2447 ip_vs_sync_net_cleanup(ipvs);
2452 static struct pernet_operations ipvs_core_ops = {
2453 .init = __ip_vs_init,
2454 .exit_batch = __ip_vs_cleanup_batch,
2455 .id = &ip_vs_net_id,
2456 .size = sizeof(struct netns_ipvs),
2459 static struct pernet_operations ipvs_core_dev_ops = {
2460 .init = __ip_vs_dev_init,
2461 .exit_batch = __ip_vs_dev_cleanup_batch,
2465 * Initialize IP Virtual Server
2467 static int __init ip_vs_init(void)
2471 ret = ip_vs_control_init();
2473 pr_err("can't setup control.\n");
2477 ip_vs_protocol_init();
2479 ret = ip_vs_conn_init();
2481 pr_err("can't setup connection table.\n");
2482 goto cleanup_protocol;
2485 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2489 ret = register_pernet_device(&ipvs_core_dev_ops);
2493 ret = ip_vs_register_nl_ioctl();
2495 pr_err("can't register netlink/ioctl.\n");
2499 pr_info("ipvs loaded.\n");
2504 unregister_pernet_device(&ipvs_core_dev_ops);
2506 unregister_pernet_subsys(&ipvs_core_ops);
2508 ip_vs_conn_cleanup();
2510 ip_vs_protocol_cleanup();
2511 ip_vs_control_cleanup();
2516 static void __exit ip_vs_cleanup(void)
2518 ip_vs_unregister_nl_ioctl();
2519 unregister_pernet_device(&ipvs_core_dev_ops);
2520 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2521 ip_vs_conn_cleanup();
2522 ip_vs_protocol_cleanup();
2523 ip_vs_control_cleanup();
2524 pr_info("ipvs unloaded.\n");
2527 module_init(ip_vs_init);
2528 module_exit(ip_vs_cleanup);
2529 MODULE_LICENSE("GPL");