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);
71 EXPORT_SYMBOL(ip_vs_new_conn_out);
73 static unsigned int ip_vs_net_id __read_mostly;
74 /* netns cnt used for uniqueness */
75 static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
77 /* ID used in ICMP lookups */
78 #define icmp_id(icmph) (((icmph)->un).echo.id)
79 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
81 const char *ip_vs_proto_name(unsigned int proto)
96 #ifdef CONFIG_IP_VS_IPV6
101 sprintf(buf, "IP_%u", proto);
106 void ip_vs_init_hash_table(struct list_head *table, int rows)
109 INIT_LIST_HEAD(&table[rows]);
113 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
115 struct ip_vs_dest *dest = cp->dest;
116 struct netns_ipvs *ipvs = cp->ipvs;
118 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
119 struct ip_vs_cpu_stats *s;
120 struct ip_vs_service *svc;
122 s = this_cpu_ptr(dest->stats.cpustats);
123 u64_stats_update_begin(&s->syncp);
125 s->cnt.inbytes += skb->len;
126 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);
135 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
136 u64_stats_update_begin(&s->syncp);
138 s->cnt.inbytes += skb->len;
139 u64_stats_update_end(&s->syncp);
145 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147 struct ip_vs_dest *dest = cp->dest;
148 struct netns_ipvs *ipvs = cp->ipvs;
150 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
151 struct ip_vs_cpu_stats *s;
152 struct ip_vs_service *svc;
154 s = this_cpu_ptr(dest->stats.cpustats);
155 u64_stats_update_begin(&s->syncp);
157 s->cnt.outbytes += skb->len;
158 u64_stats_update_end(&s->syncp);
160 svc = rcu_dereference(dest->svc);
161 s = this_cpu_ptr(svc->stats.cpustats);
162 u64_stats_update_begin(&s->syncp);
164 s->cnt.outbytes += skb->len;
165 u64_stats_update_end(&s->syncp);
167 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
168 u64_stats_update_begin(&s->syncp);
170 s->cnt.outbytes += skb->len;
171 u64_stats_update_end(&s->syncp);
177 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
179 struct netns_ipvs *ipvs = svc->ipvs;
180 struct ip_vs_cpu_stats *s;
182 s = this_cpu_ptr(cp->dest->stats.cpustats);
183 u64_stats_update_begin(&s->syncp);
185 u64_stats_update_end(&s->syncp);
187 s = this_cpu_ptr(svc->stats.cpustats);
188 u64_stats_update_begin(&s->syncp);
190 u64_stats_update_end(&s->syncp);
192 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
193 u64_stats_update_begin(&s->syncp);
195 u64_stats_update_end(&s->syncp);
200 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
201 const struct sk_buff *skb,
202 struct ip_vs_proto_data *pd)
204 if (likely(pd->pp->state_transition))
205 pd->pp->state_transition(cp, direction, skb, pd);
209 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
210 struct sk_buff *skb, int protocol,
211 const union nf_inet_addr *caddr, __be16 cport,
212 const union nf_inet_addr *vaddr, __be16 vport,
213 struct ip_vs_conn_param *p)
215 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
217 p->pe = rcu_dereference(svc->pe);
218 if (p->pe && p->pe->fill_param)
219 return p->pe->fill_param(p, skb);
225 * IPVS persistent scheduling function
226 * It creates a connection entry according to its template if exists,
227 * or selects a server and creates a connection entry plus a template.
228 * Locking: we are svc user (svc->refcnt), so we hold all dests too
229 * Protocols supported: TCP, UDP
231 static struct ip_vs_conn *
232 ip_vs_sched_persist(struct ip_vs_service *svc,
233 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
234 int *ignored, struct ip_vs_iphdr *iph)
236 struct ip_vs_conn *cp = NULL;
237 struct ip_vs_dest *dest;
238 struct ip_vs_conn *ct;
239 __be16 dport = 0; /* destination port to forward */
241 struct ip_vs_conn_param param;
242 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
243 union nf_inet_addr snet; /* source network of the client,
245 const union nf_inet_addr *src_addr, *dst_addr;
247 if (likely(!ip_vs_iph_inverse(iph))) {
248 src_addr = &iph->saddr;
249 dst_addr = &iph->daddr;
251 src_addr = &iph->daddr;
252 dst_addr = &iph->saddr;
256 /* Mask saddr with the netmask to adjust template granularity */
257 #ifdef CONFIG_IP_VS_IPV6
258 if (svc->af == AF_INET6)
259 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
260 (__force __u32) svc->netmask);
263 snet.ip = src_addr->ip & svc->netmask;
265 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
267 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
268 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
269 IP_VS_DBG_ADDR(svc->af, &snet));
272 * As far as we know, FTP is a very complicated network protocol, and
273 * it uses control connection and data connections. For active FTP,
274 * FTP server initialize data connection to the client, its source port
275 * is often 20. For passive FTP, FTP server tells the clients the port
276 * that it passively listens to, and the client issues the data
277 * connection. In the tunneling or direct routing mode, the load
278 * balancer is on the client-to-server half of connection, the port
279 * number is unknown to the load balancer. So, a conn template like
280 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
281 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
282 * is created for other persistent services.
285 int protocol = iph->protocol;
286 const union nf_inet_addr *vaddr = dst_addr;
289 if (dst_port == svc->port) {
291 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
293 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
295 if (svc->port != FTPPORT)
298 /* Note: persistent fwmark-based services and
299 * persistent port zero service are handled here.
301 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
302 * port zero template:
303 * <protocol,caddr,0,vaddr,0,daddr,0>
306 protocol = IPPROTO_IP;
310 /* return *ignored = -1 so NF_DROP can be used */
311 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
312 vaddr, vport, ¶m) < 0) {
318 /* Check if a template already exists */
319 ct = ip_vs_ct_in_get(¶m);
320 if (!ct || !ip_vs_check_template(ct, NULL)) {
321 struct ip_vs_scheduler *sched;
324 * No template found or the dest of the connection
325 * template is not available.
326 * return *ignored=0 i.e. ICMP and NF_DROP
328 sched = rcu_dereference(svc->scheduler);
330 /* read svc->sched_data after svc->scheduler */
332 dest = sched->schedule(svc, skb, iph);
337 IP_VS_DBG(1, "p-schedule: no dest found.\n");
338 kfree(param.pe_data);
343 if (dst_port == svc->port && svc->port != FTPPORT)
347 * This adds param.pe_data to the template,
348 * and thus param.pe_data will be destroyed
349 * when the template expires */
350 ct = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport,
351 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
353 kfree(param.pe_data);
358 ct->timeout = svc->timeout;
360 /* set destination with the found template */
362 kfree(param.pe_data);
366 if (dport == svc->port && dest->port)
369 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
370 && iph->protocol == IPPROTO_UDP) ?
371 IP_VS_CONN_F_ONE_PACKET : 0;
374 * Create a new connection according to the template
376 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
377 src_port, dst_addr, dst_port, ¶m);
379 cp = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, flags, dest,
390 ip_vs_control_add(cp, ct);
393 ip_vs_conn_stats(cp, svc);
399 * IPVS main scheduling function
400 * It selects a server according to the virtual service, and
401 * creates a connection entry.
402 * Protocols supported: TCP, UDP
406 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
407 * svc/scheduler decides that this packet should be accepted with
408 * NF_ACCEPT because it must not be scheduled.
410 * 0 : scheduler can not find destination, so try bypass or
411 * return ICMP and then NF_DROP (ip_vs_leave).
413 * -1 : scheduler tried to schedule but fatal error occurred, eg.
414 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
415 * failure such as missing Call-ID, ENOMEM on skb_linearize
416 * or pe_data. In this case we should return NF_DROP without
417 * any attempts to send ICMP with ip_vs_leave.
420 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
421 struct ip_vs_proto_data *pd, int *ignored,
422 struct ip_vs_iphdr *iph)
424 struct ip_vs_protocol *pp = pd->pp;
425 struct ip_vs_conn *cp = NULL;
426 struct ip_vs_scheduler *sched;
427 struct ip_vs_dest *dest;
428 __be16 _ports[2], *pptr, cport, vport;
429 const void *caddr, *vaddr;
434 * IPv6 frags, only the first hit here.
436 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
440 if (likely(!ip_vs_iph_inverse(iph))) {
453 * FTPDATA needs this check when using local real server.
454 * Never schedule Active FTPDATA connections from real server.
455 * For LVS-NAT they must be already created. For other methods
456 * with persistence the connection is created on SYN+ACK.
458 if (cport == FTPDATA) {
459 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
460 "Not scheduling FTPDATA");
465 * Do not schedule replies from local real server.
467 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
468 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
469 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
470 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
473 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
474 "Not scheduling reply for existing"
476 __ip_vs_conn_put(cp);
484 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
485 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
491 * Non-persistent service
493 if (!svc->fwmark && vport != svc->port) {
495 pr_err("Schedule: port zero only supported "
496 "in persistent services, "
497 "check your ipvs configuration\n");
501 sched = rcu_dereference(svc->scheduler);
503 /* read svc->sched_data after svc->scheduler */
505 dest = sched->schedule(svc, skb, iph);
510 IP_VS_DBG(1, "Schedule: no dest found.\n");
514 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
515 && iph->protocol == IPPROTO_UDP) ?
516 IP_VS_CONN_F_ONE_PACKET : 0;
519 * Create a connection entry.
522 struct ip_vs_conn_param p;
524 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
525 caddr, cport, vaddr, vport, &p);
526 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
527 dest->port ? dest->port : vport,
528 flags, dest, skb->mark);
535 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
536 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
538 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
539 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
540 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
541 cp->flags, refcount_read(&cp->refcnt));
543 ip_vs_conn_stats(cp, svc);
547 static inline int ip_vs_addr_is_unicast(struct net *net, int af,
548 union nf_inet_addr *addr)
550 #ifdef CONFIG_IP_VS_IPV6
552 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
554 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
558 * Pass or drop the packet.
559 * Called by ip_vs_in, when the virtual service is available but
560 * no destination is available for a new connection.
562 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
563 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
565 __be16 _ports[2], *pptr, dport;
566 struct netns_ipvs *ipvs = svc->ipvs;
567 struct net *net = ipvs->net;
569 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
572 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
574 /* if it is fwmark-based service, the cache_bypass sysctl is up
575 and the destination is a non-local unicast, then create
576 a cache_bypass connection entry */
577 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
578 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
579 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
581 struct ip_vs_conn *cp;
582 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
583 iph->protocol == IPPROTO_UDP) ?
584 IP_VS_CONN_F_ONE_PACKET : 0;
585 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
587 /* create a new connection entry */
588 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
590 struct ip_vs_conn_param p;
591 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
592 &iph->saddr, pptr[0],
593 &iph->daddr, pptr[1], &p);
594 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
595 IP_VS_CONN_F_BYPASS | flags,
602 ip_vs_in_stats(cp, skb);
605 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
607 /* transmit the first SYN packet */
608 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
609 /* do not touch skb anymore */
611 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
612 atomic_inc(&cp->control->in_pkts);
614 atomic_inc(&cp->in_pkts);
620 * When the virtual ftp service is presented, packets destined
621 * for other services on the VIP may get here (except services
622 * listed in the ipvs table), pass the packets, because it is
623 * not ipvs job to decide to drop the packets.
625 if (svc->port == FTPPORT && dport != FTPPORT)
628 if (unlikely(ip_vs_iph_icmp(iph)))
632 * Notify the client that the destination is unreachable, and
633 * release the socket buffer.
634 * Since it is in IP layer, the TCP socket is not actually
635 * created, the TCP RST packet cannot be sent, instead that
636 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
638 #ifdef CONFIG_IP_VS_IPV6
639 if (svc->af == AF_INET6) {
641 skb->dev = net->loopback_dev;
642 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
645 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
652 static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
654 return ipvs->sysctl_snat_reroute;
657 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
659 return ipvs->sysctl_nat_icmp_send;
662 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
664 return ipvs->sysctl_expire_nodest_conn;
669 static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
670 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
671 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
675 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
677 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
680 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
682 if (NF_INET_LOCAL_IN == hooknum)
683 return IP_DEFRAG_VS_IN;
684 if (NF_INET_FORWARD == hooknum)
685 return IP_DEFRAG_VS_FWD;
686 return IP_DEFRAG_VS_OUT;
689 static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
690 struct sk_buff *skb, u_int32_t user)
695 err = ip_defrag(ipvs->net, skb, user);
698 ip_send_check(ip_hdr(skb));
703 static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
704 struct sk_buff *skb, unsigned int hooknum)
706 if (!sysctl_snat_reroute(ipvs))
708 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
709 if (NF_INET_LOCAL_IN == hooknum)
711 #ifdef CONFIG_IP_VS_IPV6
712 if (af == AF_INET6) {
713 struct dst_entry *dst = skb_dst(skb);
715 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
716 ip6_route_me_harder(ipvs->net, skb) != 0)
720 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
721 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
728 * Packet has been made sufficiently writable in caller
729 * - inout: 1=in->out, 0=out->in
731 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
732 struct ip_vs_conn *cp, int inout)
734 struct iphdr *iph = ip_hdr(skb);
735 unsigned int icmp_offset = iph->ihl*4;
736 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
738 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
741 iph->saddr = cp->vaddr.ip;
743 ciph->daddr = cp->vaddr.ip;
746 iph->daddr = cp->daddr.ip;
748 ciph->saddr = cp->daddr.ip;
752 /* the TCP/UDP/SCTP port */
753 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
754 IPPROTO_SCTP == ciph->protocol) {
755 __be16 *ports = (void *)ciph + ciph->ihl*4;
758 ports[1] = cp->vport;
760 ports[0] = cp->dport;
763 /* And finally the ICMP checksum */
765 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
766 skb->ip_summed = CHECKSUM_UNNECESSARY;
769 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
770 "Forwarding altered outgoing ICMP");
772 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
773 "Forwarding altered incoming ICMP");
776 #ifdef CONFIG_IP_VS_IPV6
777 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
778 struct ip_vs_conn *cp, int inout)
780 struct ipv6hdr *iph = ipv6_hdr(skb);
781 unsigned int icmp_offset = 0;
782 unsigned int offs = 0; /* header offset*/
784 struct icmp6hdr *icmph;
785 struct ipv6hdr *ciph;
786 unsigned short fragoffs;
788 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
789 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
790 offs = icmp_offset + sizeof(struct icmp6hdr);
791 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
793 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
796 iph->saddr = cp->vaddr.in6;
797 ciph->daddr = cp->vaddr.in6;
799 iph->daddr = cp->daddr.in6;
800 ciph->saddr = cp->daddr.in6;
803 /* the TCP/UDP/SCTP port */
804 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
805 IPPROTO_SCTP == protocol)) {
806 __be16 *ports = (void *)(skb_network_header(skb) + offs);
808 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
809 ntohs(inout ? ports[1] : ports[0]),
810 ntohs(inout ? cp->vport : cp->dport));
812 ports[1] = cp->vport;
814 ports[0] = cp->dport;
817 /* And finally the ICMP checksum */
818 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
819 skb->len - icmp_offset,
821 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
822 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
823 skb->ip_summed = CHECKSUM_PARTIAL;
826 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
827 (void *)ciph - (void *)iph,
828 "Forwarding altered outgoing ICMPv6");
830 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
831 (void *)ciph - (void *)iph,
832 "Forwarding altered incoming ICMPv6");
836 /* Handle relevant response ICMP messages - forward to the right
839 static int handle_response_icmp(int af, struct sk_buff *skb,
840 union nf_inet_addr *snet,
841 __u8 protocol, struct ip_vs_conn *cp,
842 struct ip_vs_protocol *pp,
843 unsigned int offset, unsigned int ihl,
844 unsigned int hooknum)
846 unsigned int verdict = NF_DROP;
848 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
851 /* Ensure the checksum is correct */
852 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
853 /* Failed checksum! */
854 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
855 IP_VS_DBG_ADDR(af, snet));
859 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
860 IPPROTO_SCTP == protocol)
861 offset += 2 * sizeof(__u16);
862 if (!skb_make_writable(skb, offset))
865 #ifdef CONFIG_IP_VS_IPV6
867 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
870 ip_vs_nat_icmp(skb, pp, cp, 1);
872 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
875 /* do the statistics and put it back */
876 ip_vs_out_stats(cp, skb);
878 skb->ipvs_property = 1;
879 if (!(cp->flags & IP_VS_CONN_F_NFCT))
882 ip_vs_update_conntrack(skb, cp, 0);
888 __ip_vs_conn_put(cp);
894 * Handle ICMP messages in the inside-to-outside direction (outgoing).
895 * Find any that might be relevant, check against existing connections.
896 * Currently handles error types - unreachable, quench, ttl exceeded.
898 static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
899 int *related, unsigned int hooknum)
902 struct icmphdr _icmph, *ic;
903 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
904 struct ip_vs_iphdr ciph;
905 struct ip_vs_conn *cp;
906 struct ip_vs_protocol *pp;
907 unsigned int offset, ihl;
908 union nf_inet_addr snet;
912 /* reassemble IP fragments */
913 if (ip_is_fragment(ip_hdr(skb))) {
914 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
919 offset = ihl = iph->ihl * 4;
920 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
924 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
925 ic->type, ntohs(icmp_id(ic)),
926 &iph->saddr, &iph->daddr);
929 * Work through seeing if this is for us.
930 * These checks are supposed to be in an order that means easy
931 * things are checked first to speed up processing.... however
932 * this means that some packets will manage to get a long way
933 * down this stack and then be rejected, but that's life.
935 if ((ic->type != ICMP_DEST_UNREACH) &&
936 (ic->type != ICMP_SOURCE_QUENCH) &&
937 (ic->type != ICMP_TIME_EXCEEDED)) {
942 /* Now find the contained IP header */
943 offset += sizeof(_icmph);
944 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
946 return NF_ACCEPT; /* The packet looks wrong, ignore */
948 pp = ip_vs_proto_get(cih->protocol);
952 /* Is the embedded protocol header present? */
953 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
957 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
958 "Checking outgoing ICMP for");
960 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
962 /* The embedded headers contain source and dest in reverse order */
963 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
967 snet.ip = iph->saddr;
968 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
969 pp, ciph.len, ihl, hooknum);
972 #ifdef CONFIG_IP_VS_IPV6
973 static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
974 int *related, unsigned int hooknum,
975 struct ip_vs_iphdr *ipvsh)
977 struct icmp6hdr _icmph, *ic;
978 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
979 struct ip_vs_conn *cp;
980 struct ip_vs_protocol *pp;
981 union nf_inet_addr snet;
985 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
990 * Work through seeing if this is for us.
991 * These checks are supposed to be in an order that means easy
992 * things are checked first to speed up processing.... however
993 * this means that some packets will manage to get a long way
994 * down this stack and then be rejected, but that's life.
996 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1000 /* Fragment header that is before ICMP header tells us that:
1001 * it's not an error message since they can't be fragmented.
1003 if (ipvsh->flags & IP6_FH_F_FRAG)
1006 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1007 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1008 &ipvsh->saddr, &ipvsh->daddr);
1010 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1012 return NF_ACCEPT; /* The packet looks wrong, ignore */
1014 pp = ip_vs_proto_get(ciph.protocol);
1018 /* The embedded headers contain source and dest in reverse order */
1019 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
1023 snet.in6 = ciph.saddr.in6;
1025 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1026 pp, offset, sizeof(struct ipv6hdr),
1032 * Check if sctp chunc is ABORT chunk
1034 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1036 struct sctp_chunkhdr *sch, schunk;
1037 sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr),
1038 sizeof(schunk), &schunk);
1041 if (sch->type == SCTP_CID_ABORT)
1046 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1048 struct tcphdr _tcph, *th;
1050 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1056 static inline bool is_new_conn(const struct sk_buff *skb,
1057 struct ip_vs_iphdr *iph)
1059 switch (iph->protocol) {
1061 struct tcphdr _tcph, *th;
1063 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1068 case IPPROTO_SCTP: {
1069 struct sctp_chunkhdr *sch, schunk;
1071 sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr),
1072 sizeof(schunk), &schunk);
1075 return sch->type == SCTP_CID_INIT;
1082 static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1083 int conn_reuse_mode)
1085 /* Controlled (FTP DATA or persistence)? */
1089 switch (cp->protocol) {
1091 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1092 (cp->state == IP_VS_TCP_S_CLOSE) ||
1093 ((conn_reuse_mode & 2) &&
1094 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1095 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1097 return cp->state == IP_VS_SCTP_S_CLOSED;
1103 /* Generic function to create new connections for outgoing RS packets
1105 * Pre-requisites for successful connection creation:
1106 * 1) Virtual Service is NOT fwmark based:
1107 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1108 * 2) Real Server and Virtual Service were NOT configured without port:
1109 * This is to allow match of different VS to the same RS ip-addr
1111 struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1112 struct ip_vs_dest *dest,
1113 struct sk_buff *skb,
1114 const struct ip_vs_iphdr *iph,
1118 struct ip_vs_conn_param param;
1119 struct ip_vs_conn *ct = NULL, *cp = NULL;
1120 const union nf_inet_addr *vaddr, *daddr, *caddr;
1121 union nf_inet_addr snet;
1128 daddr = &iph->saddr;
1129 caddr = &iph->daddr;
1131 /* check pre-requisites are satisfied */
1134 if (!vport || !dport)
1137 /* for persistent service first create connection template */
1138 if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
1139 /* apply netmask the same way ingress-side does */
1140 #ifdef CONFIG_IP_VS_IPV6
1141 if (svc->af == AF_INET6)
1142 ipv6_addr_prefix(&snet.in6, &caddr->in6,
1143 (__force __u32)svc->netmask);
1146 snet.ip = caddr->ip & svc->netmask;
1147 /* fill params and create template if not existent */
1148 if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
1152 ct = ip_vs_ct_in_get(¶m);
1153 /* check if template exists and points to the same dest */
1154 if (!ct || !ip_vs_check_template(ct, dest)) {
1155 ct = ip_vs_conn_new(¶m, dest->af, daddr, dport,
1156 IP_VS_CONN_F_TEMPLATE, dest, 0);
1158 kfree(param.pe_data);
1161 ct->timeout = svc->timeout;
1163 kfree(param.pe_data);
1167 /* connection flags */
1168 flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
1169 iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
1170 /* create connection */
1171 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
1172 caddr, cport, vaddr, vport, ¶m);
1173 cp = ip_vs_conn_new(¶m, dest->af, daddr, dport, flags, dest, 0);
1180 ip_vs_control_add(cp, ct);
1183 ip_vs_conn_stats(cp, svc);
1185 /* return connection (will be used to handle outgoing packet) */
1186 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1187 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1189 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
1190 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
1191 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
1192 cp->flags, refcount_read(&cp->refcnt));
1197 /* Handle outgoing packets which are considered requests initiated by
1198 * real servers, so that subsequent responses from external client can be
1199 * routed to the right real server.
1200 * Used also for outgoing responses in OPS mode.
1202 * Connection management is handled by persistent-engine specific callback.
1204 static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
1205 struct netns_ipvs *ipvs,
1206 int af, struct sk_buff *skb,
1207 const struct ip_vs_iphdr *iph)
1209 struct ip_vs_dest *dest;
1210 struct ip_vs_conn *cp = NULL;
1211 __be16 _ports[2], *pptr;
1213 if (hooknum == NF_INET_LOCAL_IN)
1216 pptr = frag_safe_skb_hp(skb, iph->len,
1217 sizeof(_ports), _ports, iph);
1221 dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
1222 &iph->saddr, pptr[0]);
1224 struct ip_vs_service *svc;
1225 struct ip_vs_pe *pe;
1227 svc = rcu_dereference(dest->svc);
1229 pe = rcu_dereference(svc->pe);
1230 if (pe && pe->conn_out)
1231 cp = pe->conn_out(svc, dest, skb, iph,
1239 /* Handle response packets: rewrite addresses and send away...
1242 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1243 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1244 unsigned int hooknum)
1246 struct ip_vs_protocol *pp = pd->pp;
1248 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
1250 if (!skb_make_writable(skb, iph->len))
1253 /* mangle the packet */
1254 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1257 #ifdef CONFIG_IP_VS_IPV6
1259 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1263 ip_hdr(skb)->saddr = cp->vaddr.ip;
1264 ip_send_check(ip_hdr(skb));
1268 * nf_iterate does not expect change in the skb->dst->dev.
1269 * It looks like it is not fatal to enable this code for hooks
1270 * where our handlers are at the end of the chain list and
1271 * when all next handlers use skb->dst->dev and not outdev.
1272 * It will definitely route properly the inout NAT traffic
1273 * when multiple paths are used.
1276 /* For policy routing, packets originating from this
1277 * machine itself may be routed differently to packets
1278 * passing through. We want this packet to be routed as
1279 * if it came from this machine itself. So re-compute
1280 * the routing information.
1282 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
1285 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
1287 ip_vs_out_stats(cp, skb);
1288 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1289 skb->ipvs_property = 1;
1290 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1293 ip_vs_update_conntrack(skb, cp, 0);
1307 * Check if outgoing packet belongs to the established ip_vs_conn.
1310 ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1312 struct ip_vs_iphdr iph;
1313 struct ip_vs_protocol *pp;
1314 struct ip_vs_proto_data *pd;
1315 struct ip_vs_conn *cp;
1320 /* Already marked as IPVS request or reply? */
1321 if (skb->ipvs_property)
1324 sk = skb_to_full_sk(skb);
1325 /* Bad... Do not break raw sockets */
1326 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
1329 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
1333 if (unlikely(!skb_dst(skb)))
1339 ip_vs_fill_iph_skb(af, skb, false, &iph);
1340 #ifdef CONFIG_IP_VS_IPV6
1341 if (af == AF_INET6) {
1342 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1344 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
1352 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1354 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
1360 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1365 /* reassemble IP fragments */
1366 #ifdef CONFIG_IP_VS_IPV6
1369 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1370 if (ip_vs_gather_frags(ipvs, skb,
1371 ip_vs_defrag_user(hooknum)))
1374 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
1378 * Check if the packet belongs to an existing entry
1380 cp = pp->conn_out_get(ipvs, af, skb, &iph);
1383 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
1385 return handle_response(af, skb, pd, cp, &iph, hooknum);
1388 /* Check for real-server-started requests */
1389 if (atomic_read(&ipvs->conn_out_counter)) {
1390 /* Currently only for UDP:
1391 * connection oriented protocols typically use
1392 * ephemeral ports for outgoing connections, so
1393 * related incoming responses would not match any VS
1395 if (pp->protocol == IPPROTO_UDP) {
1396 cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
1398 return handle_response(af, skb, pd, cp, &iph,
1403 if (sysctl_nat_icmp_send(ipvs) &&
1404 (pp->protocol == IPPROTO_TCP ||
1405 pp->protocol == IPPROTO_UDP ||
1406 pp->protocol == IPPROTO_SCTP)) {
1407 __be16 _ports[2], *pptr;
1409 pptr = frag_safe_skb_hp(skb, iph.len,
1410 sizeof(_ports), _ports, &iph);
1412 return NF_ACCEPT; /* Not for me */
1413 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
1416 * Notify the real server: there is no
1417 * existing entry if it is not RST
1418 * packet or not TCP packet.
1420 if ((iph.protocol != IPPROTO_TCP &&
1421 iph.protocol != IPPROTO_SCTP)
1422 || ((iph.protocol == IPPROTO_TCP
1423 && !is_tcp_reset(skb, iph.len))
1424 || (iph.protocol == IPPROTO_SCTP
1425 && !is_sctp_abort(skb,
1427 #ifdef CONFIG_IP_VS_IPV6
1428 if (af == AF_INET6) {
1430 skb->dev = ipvs->net->loopback_dev;
1432 ICMPV6_DEST_UNREACH,
1433 ICMPV6_PORT_UNREACH,
1439 ICMP_PORT_UNREACH, 0);
1446 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
1447 "ip_vs_out: packet continues traversal as normal");
1451 __ip_vs_conn_put(cp);
1456 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1457 * used only for VS/NAT.
1458 * Check if packet is reply for established ip_vs_conn.
1461 ip_vs_reply4(void *priv, struct sk_buff *skb,
1462 const struct nf_hook_state *state)
1464 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1468 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1469 * Check if packet is reply for established ip_vs_conn.
1472 ip_vs_local_reply4(void *priv, struct sk_buff *skb,
1473 const struct nf_hook_state *state)
1475 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1478 #ifdef CONFIG_IP_VS_IPV6
1481 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1482 * used only for VS/NAT.
1483 * Check if packet is reply for established ip_vs_conn.
1486 ip_vs_reply6(void *priv, struct sk_buff *skb,
1487 const struct nf_hook_state *state)
1489 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1493 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1494 * Check if packet is reply for established ip_vs_conn.
1497 ip_vs_local_reply6(void *priv, struct sk_buff *skb,
1498 const struct nf_hook_state *state)
1500 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1506 ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1507 struct ip_vs_proto_data *pd,
1508 int *verdict, struct ip_vs_conn **cpp,
1509 struct ip_vs_iphdr *iph)
1511 struct ip_vs_protocol *pp = pd->pp;
1513 if (!iph->fragoffs) {
1514 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1515 * replayed fragment zero will already have created the cp
1518 /* Schedule and create new connection entry into cpp */
1519 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
1523 if (unlikely(!*cpp)) {
1524 /* sorry, all this trouble for a no-hit :) */
1525 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1526 "ip_vs_in: packet continues traversal as normal");
1528 /* Fragment couldn't be mapped to a conn entry */
1530 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1531 "unhandled fragment");
1533 *verdict = NF_ACCEPT;
1541 * Handle ICMP messages in the outside-to-inside direction (incoming).
1542 * Find any that might be relevant, check against existing connections,
1543 * forward to the right destination host if relevant.
1544 * Currently handles error types - unreachable, quench, ttl exceeded.
1547 ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1548 unsigned int hooknum)
1551 struct icmphdr _icmph, *ic;
1552 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1553 struct ip_vs_iphdr ciph;
1554 struct ip_vs_conn *cp;
1555 struct ip_vs_protocol *pp;
1556 struct ip_vs_proto_data *pd;
1557 unsigned int offset, offset2, ihl, verdict;
1558 bool ipip, new_cp = false;
1562 /* reassemble IP fragments */
1563 if (ip_is_fragment(ip_hdr(skb))) {
1564 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
1569 offset = ihl = iph->ihl * 4;
1570 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1574 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1575 ic->type, ntohs(icmp_id(ic)),
1576 &iph->saddr, &iph->daddr);
1579 * Work through seeing if this is for us.
1580 * These checks are supposed to be in an order that means easy
1581 * things are checked first to speed up processing.... however
1582 * this means that some packets will manage to get a long way
1583 * down this stack and then be rejected, but that's life.
1585 if ((ic->type != ICMP_DEST_UNREACH) &&
1586 (ic->type != ICMP_SOURCE_QUENCH) &&
1587 (ic->type != ICMP_TIME_EXCEEDED)) {
1592 /* Now find the contained IP header */
1593 offset += sizeof(_icmph);
1594 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1596 return NF_ACCEPT; /* The packet looks wrong, ignore */
1598 /* Special case for errors for IPIP packets */
1600 if (cih->protocol == IPPROTO_IPIP) {
1601 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1603 /* Error for our IPIP must arrive at LOCAL_IN */
1604 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1606 offset += cih->ihl * 4;
1607 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1609 return NF_ACCEPT; /* The packet looks wrong, ignore */
1613 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
1618 /* Is the embedded protocol header present? */
1619 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1623 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1624 "Checking incoming ICMP for");
1627 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
1630 /* The embedded headers contain source and dest in reverse order.
1631 * For IPIP this is error for request, not for reply.
1633 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
1638 if (ipip || !sysctl_schedule_icmp(ipvs))
1641 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
1648 /* Ensure the checksum is correct */
1649 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1650 /* Failed checksum! */
1651 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1657 __be32 info = ic->un.gateway;
1658 __u8 type = ic->type;
1659 __u8 code = ic->code;
1661 /* Update the MTU */
1662 if (ic->type == ICMP_DEST_UNREACH &&
1663 ic->code == ICMP_FRAG_NEEDED) {
1664 struct ip_vs_dest *dest = cp->dest;
1665 u32 mtu = ntohs(ic->un.frag.mtu);
1666 __be16 frag_off = cih->frag_off;
1668 /* Strip outer IP and ICMP, go to IPIP header */
1669 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1671 offset2 -= ihl + sizeof(_icmph);
1672 skb_reset_network_header(skb);
1673 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1674 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1675 ipv4_update_pmtu(skb, ipvs->net,
1677 /* Client uses PMTUD? */
1678 if (!(frag_off & htons(IP_DF)))
1680 /* Prefer the resulting PMTU */
1682 struct ip_vs_dest_dst *dest_dst;
1684 dest_dst = rcu_dereference(dest->dest_dst);
1686 mtu = dst_mtu(dest_dst->dst_cache);
1688 if (mtu > 68 + sizeof(struct iphdr))
1689 mtu -= sizeof(struct iphdr);
1692 /* Strip outer IP, ICMP and IPIP, go to IP header of
1695 if (pskb_pull(skb, offset2) == NULL)
1697 skb_reset_network_header(skb);
1698 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1699 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1700 type, code, ntohl(info));
1701 icmp_send(skb, type, code, info);
1702 /* ICMP can be shorter but anyways, account it */
1703 ip_vs_out_stats(cp, skb);
1707 verdict = NF_STOLEN;
1711 /* do the statistics and put it back */
1712 ip_vs_in_stats(cp, skb);
1713 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1714 IPPROTO_SCTP == cih->protocol)
1715 offset += 2 * sizeof(__u16);
1716 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1719 if (likely(!new_cp))
1720 __ip_vs_conn_put(cp);
1727 #ifdef CONFIG_IP_VS_IPV6
1728 static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1729 int *related, unsigned int hooknum,
1730 struct ip_vs_iphdr *iph)
1732 struct icmp6hdr _icmph, *ic;
1733 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1734 struct ip_vs_conn *cp;
1735 struct ip_vs_protocol *pp;
1736 struct ip_vs_proto_data *pd;
1737 unsigned int offset, verdict;
1738 bool new_cp = false;
1742 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1747 * Work through seeing if this is for us.
1748 * These checks are supposed to be in an order that means easy
1749 * things are checked first to speed up processing.... however
1750 * this means that some packets will manage to get a long way
1751 * down this stack and then be rejected, but that's life.
1753 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1757 /* Fragment header that is before ICMP header tells us that:
1758 * it's not an error message since they can't be fragmented.
1760 if (iph->flags & IP6_FH_F_FRAG)
1763 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1764 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1765 &iph->saddr, &iph->daddr);
1767 offset = iph->len + sizeof(_icmph);
1768 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
1771 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
1776 /* Cannot handle fragmented embedded protocol */
1780 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1781 "Checking incoming ICMPv6 for");
1783 /* The embedded headers contain source and dest in reverse order
1784 * if not from localhost
1786 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
1791 if (!sysctl_schedule_icmp(ipvs))
1794 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
1800 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1801 if ((hooknum == NF_INET_LOCAL_OUT) &&
1802 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1803 verdict = NF_ACCEPT;
1807 /* do the statistics and put it back */
1808 ip_vs_in_stats(cp, skb);
1810 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1812 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1813 IPPROTO_SCTP == ciph.protocol)
1814 offset += 2 * sizeof(__u16); /* Also mangle ports */
1816 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
1819 if (likely(!new_cp))
1820 __ip_vs_conn_put(cp);
1830 * Check if it's for virtual services, look it up,
1831 * and send it on its way...
1834 ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1836 struct ip_vs_iphdr iph;
1837 struct ip_vs_protocol *pp;
1838 struct ip_vs_proto_data *pd;
1839 struct ip_vs_conn *cp;
1843 /* Already marked as IPVS request or reply? */
1844 if (skb->ipvs_property)
1849 * - remote client: only PACKET_HOST
1850 * - route: used for struct net when skb->dev is unset
1852 if (unlikely((skb->pkt_type != PACKET_HOST &&
1853 hooknum != NF_INET_LOCAL_OUT) ||
1855 ip_vs_fill_iph_skb(af, skb, false, &iph);
1856 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1857 " ignored in hook %u\n",
1858 skb->pkt_type, iph.protocol,
1859 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1862 /* ipvs enabled in this netns ? */
1863 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1866 ip_vs_fill_iph_skb(af, skb, false, &iph);
1868 /* Bad... Do not break raw sockets */
1869 sk = skb_to_full_sk(skb);
1870 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
1873 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
1877 #ifdef CONFIG_IP_VS_IPV6
1878 if (af == AF_INET6) {
1879 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1881 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1889 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1891 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1898 /* Protocol supported? */
1899 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1900 if (unlikely(!pd)) {
1901 /* The only way we'll see this packet again is if it's
1902 * encapsulated, so mark it with ipvs_property=1 so we
1903 * skip it if we're ignoring tunneled packets
1905 if (sysctl_ignore_tunneled(ipvs))
1906 skb->ipvs_property = 1;
1912 * Check if the packet belongs to an existing connection entry
1914 cp = pp->conn_in_get(ipvs, af, skb, &iph);
1916 if (!iph.fragoffs && is_new_conn(skb, &iph) && cp) {
1917 int conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1918 bool old_ct = false, resched = false;
1920 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1921 unlikely(!atomic_read(&cp->dest->weight))) {
1923 old_ct = ip_vs_conn_uses_old_conntrack(cp, skb);
1924 } else if (conn_reuse_mode &&
1925 is_new_conn_expected(cp, conn_reuse_mode)) {
1926 old_ct = ip_vs_conn_uses_old_conntrack(cp, skb);
1927 if (!atomic_read(&cp->n_control)) {
1930 /* Do not reschedule controlling connection
1931 * that uses conntrack while it is still
1932 * referenced by controlled connection(s).
1940 cp->flags &= ~IP_VS_CONN_F_NFCT;
1941 if (!atomic_read(&cp->n_control))
1942 ip_vs_conn_expire_now(cp);
1943 __ip_vs_conn_put(cp);
1950 if (unlikely(!cp)) {
1953 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
1957 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
1959 /* Check the server status */
1960 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1961 /* the destination server is not available */
1963 __u32 flags = cp->flags;
1965 /* when timer already started, silently drop the packet.*/
1966 if (timer_pending(&cp->timer))
1967 __ip_vs_conn_put(cp);
1971 if (sysctl_expire_nodest_conn(ipvs) &&
1972 !(flags & IP_VS_CONN_F_ONE_PACKET)) {
1973 /* try to expire the connection immediately */
1974 ip_vs_conn_expire_now(cp);
1980 ip_vs_in_stats(cp, skb);
1981 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1982 if (cp->packet_xmit)
1983 ret = cp->packet_xmit(skb, cp, pp, &iph);
1984 /* do not touch skb anymore */
1986 IP_VS_DBG_RL("warning: packet_xmit is null");
1990 /* Increase its packet counter and check if it is needed
1991 * to be synchronized
1993 * Sync connection if it is about to close to
1994 * encorage the standby servers to update the connections timeout
1996 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1999 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
2000 pkts = sysctl_sync_threshold(ipvs);
2002 pkts = atomic_add_return(1, &cp->in_pkts);
2004 if (ipvs->sync_state & IP_VS_STATE_MASTER)
2005 ip_vs_sync_conn(ipvs, cp, pkts);
2006 else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
2007 /* increment is done inside ip_vs_sync_conn too */
2008 atomic_inc(&cp->control->in_pkts);
2015 * AF_INET handler in NF_INET_LOCAL_IN chain
2016 * Schedule and forward packets from remote clients
2019 ip_vs_remote_request4(void *priv, struct sk_buff *skb,
2020 const struct nf_hook_state *state)
2022 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
2026 * AF_INET handler in NF_INET_LOCAL_OUT chain
2027 * Schedule and forward packets from local clients
2030 ip_vs_local_request4(void *priv, struct sk_buff *skb,
2031 const struct nf_hook_state *state)
2033 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
2036 #ifdef CONFIG_IP_VS_IPV6
2039 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2040 * Schedule and forward packets from remote clients
2043 ip_vs_remote_request6(void *priv, struct sk_buff *skb,
2044 const struct nf_hook_state *state)
2046 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
2050 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2051 * Schedule and forward packets from local clients
2054 ip_vs_local_request6(void *priv, struct sk_buff *skb,
2055 const struct nf_hook_state *state)
2057 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
2064 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
2065 * related packets destined for 0.0.0.0/0.
2066 * When fwmark-based virtual service is used, such as transparent
2067 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2068 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
2069 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
2070 * and send them to ip_vs_in_icmp.
2073 ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
2074 const struct nf_hook_state *state)
2077 struct netns_ipvs *ipvs = net_ipvs(state->net);
2079 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
2082 /* ipvs enabled in this netns ? */
2083 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2086 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
2089 #ifdef CONFIG_IP_VS_IPV6
2091 ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
2092 const struct nf_hook_state *state)
2095 struct netns_ipvs *ipvs = net_ipvs(state->net);
2096 struct ip_vs_iphdr iphdr;
2098 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
2099 if (iphdr.protocol != IPPROTO_ICMPV6)
2102 /* ipvs enabled in this netns ? */
2103 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2106 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
2111 static const struct nf_hook_ops ip_vs_ops[] = {
2112 /* After packet filtering, change source only for VS/NAT */
2114 .hook = ip_vs_reply4,
2116 .hooknum = NF_INET_LOCAL_IN,
2117 .priority = NF_IP_PRI_NAT_SRC - 2,
2119 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2120 * or VS/NAT(change destination), so that filtering rules can be
2121 * applied to IPVS. */
2123 .hook = ip_vs_remote_request4,
2125 .hooknum = NF_INET_LOCAL_IN,
2126 .priority = NF_IP_PRI_NAT_SRC - 1,
2128 /* Before ip_vs_in, change source only for VS/NAT */
2130 .hook = ip_vs_local_reply4,
2132 .hooknum = NF_INET_LOCAL_OUT,
2133 .priority = NF_IP_PRI_NAT_DST + 1,
2135 /* After mangle, schedule and forward local requests */
2137 .hook = ip_vs_local_request4,
2139 .hooknum = NF_INET_LOCAL_OUT,
2140 .priority = NF_IP_PRI_NAT_DST + 2,
2142 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2143 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2145 .hook = ip_vs_forward_icmp,
2147 .hooknum = NF_INET_FORWARD,
2150 /* After packet filtering, change source only for VS/NAT */
2152 .hook = ip_vs_reply4,
2154 .hooknum = NF_INET_FORWARD,
2157 #ifdef CONFIG_IP_VS_IPV6
2158 /* After packet filtering, change source only for VS/NAT */
2160 .hook = ip_vs_reply6,
2162 .hooknum = NF_INET_LOCAL_IN,
2163 .priority = NF_IP6_PRI_NAT_SRC - 2,
2165 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2166 * or VS/NAT(change destination), so that filtering rules can be
2167 * applied to IPVS. */
2169 .hook = ip_vs_remote_request6,
2171 .hooknum = NF_INET_LOCAL_IN,
2172 .priority = NF_IP6_PRI_NAT_SRC - 1,
2174 /* Before ip_vs_in, change source only for VS/NAT */
2176 .hook = ip_vs_local_reply6,
2178 .hooknum = NF_INET_LOCAL_OUT,
2179 .priority = NF_IP6_PRI_NAT_DST + 1,
2181 /* After mangle, schedule and forward local requests */
2183 .hook = ip_vs_local_request6,
2185 .hooknum = NF_INET_LOCAL_OUT,
2186 .priority = NF_IP6_PRI_NAT_DST + 2,
2188 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2189 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2191 .hook = ip_vs_forward_icmp_v6,
2193 .hooknum = NF_INET_FORWARD,
2196 /* After packet filtering, change source only for VS/NAT */
2198 .hook = ip_vs_reply6,
2200 .hooknum = NF_INET_FORWARD,
2206 * Initialize IP Virtual Server netns mem.
2208 static int __net_init __ip_vs_init(struct net *net)
2210 struct netns_ipvs *ipvs;
2212 ipvs = net_generic(net, ip_vs_net_id);
2216 /* Hold the beast until a service is registerd */
2219 /* Counters used for creating unique names */
2220 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2221 atomic_inc(&ipvs_netns_cnt);
2224 if (ip_vs_estimator_net_init(ipvs) < 0)
2225 goto estimator_fail;
2227 if (ip_vs_control_net_init(ipvs) < 0)
2230 if (ip_vs_protocol_net_init(ipvs) < 0)
2233 if (ip_vs_app_net_init(ipvs) < 0)
2236 if (ip_vs_conn_net_init(ipvs) < 0)
2239 if (ip_vs_sync_net_init(ipvs) < 0)
2248 ip_vs_conn_net_cleanup(ipvs);
2250 ip_vs_app_net_cleanup(ipvs);
2252 ip_vs_protocol_net_cleanup(ipvs);
2254 ip_vs_control_net_cleanup(ipvs);
2256 ip_vs_estimator_net_cleanup(ipvs);
2262 static void __net_exit __ip_vs_cleanup(struct net *net)
2264 struct netns_ipvs *ipvs = net_ipvs(net);
2266 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
2267 ip_vs_conn_net_cleanup(ipvs);
2268 ip_vs_app_net_cleanup(ipvs);
2269 ip_vs_protocol_net_cleanup(ipvs);
2270 ip_vs_control_net_cleanup(ipvs);
2271 ip_vs_estimator_net_cleanup(ipvs);
2272 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
2276 static int __net_init __ip_vs_dev_init(struct net *net)
2280 ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2289 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2291 struct netns_ipvs *ipvs = net_ipvs(net);
2293 nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2294 ipvs->enable = 0; /* Disable packet reception */
2296 ip_vs_sync_net_cleanup(ipvs);
2300 static struct pernet_operations ipvs_core_ops = {
2301 .init = __ip_vs_init,
2302 .exit = __ip_vs_cleanup,
2303 .id = &ip_vs_net_id,
2304 .size = sizeof(struct netns_ipvs),
2307 static struct pernet_operations ipvs_core_dev_ops = {
2308 .init = __ip_vs_dev_init,
2309 .exit = __ip_vs_dev_cleanup,
2313 * Initialize IP Virtual Server
2315 static int __init ip_vs_init(void)
2319 ret = ip_vs_control_init();
2321 pr_err("can't setup control.\n");
2325 ip_vs_protocol_init();
2327 ret = ip_vs_conn_init();
2329 pr_err("can't setup connection table.\n");
2330 goto cleanup_protocol;
2333 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2337 ret = register_pernet_device(&ipvs_core_dev_ops);
2341 ret = ip_vs_register_nl_ioctl();
2343 pr_err("can't register netlink/ioctl.\n");
2347 pr_info("ipvs loaded.\n");
2352 unregister_pernet_device(&ipvs_core_dev_ops);
2354 unregister_pernet_subsys(&ipvs_core_ops);
2356 ip_vs_conn_cleanup();
2358 ip_vs_protocol_cleanup();
2359 ip_vs_control_cleanup();
2364 static void __exit ip_vs_cleanup(void)
2366 ip_vs_unregister_nl_ioctl();
2367 unregister_pernet_device(&ipvs_core_dev_ops);
2368 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2369 ip_vs_conn_cleanup();
2370 ip_vs_protocol_cleanup();
2371 ip_vs_control_cleanup();
2372 pr_info("ipvs unloaded.\n");
2375 module_init(ip_vs_init);
2376 module_exit(ip_vs_cleanup);
2377 MODULE_LICENSE("GPL");