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;
124 s = this_cpu_ptr(dest->stats.cpustats);
125 u64_stats_update_begin(&s->syncp);
127 s->cnt.inbytes += skb->len;
128 u64_stats_update_end(&s->syncp);
130 svc = rcu_dereference(dest->svc);
131 s = this_cpu_ptr(svc->stats.cpustats);
132 u64_stats_update_begin(&s->syncp);
134 s->cnt.inbytes += skb->len;
135 u64_stats_update_end(&s->syncp);
137 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
138 u64_stats_update_begin(&s->syncp);
140 s->cnt.inbytes += skb->len;
141 u64_stats_update_end(&s->syncp);
149 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
151 struct ip_vs_dest *dest = cp->dest;
152 struct netns_ipvs *ipvs = cp->ipvs;
154 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
155 struct ip_vs_cpu_stats *s;
156 struct ip_vs_service *svc;
160 s = this_cpu_ptr(dest->stats.cpustats);
161 u64_stats_update_begin(&s->syncp);
163 s->cnt.outbytes += skb->len;
164 u64_stats_update_end(&s->syncp);
166 svc = rcu_dereference(dest->svc);
167 s = this_cpu_ptr(svc->stats.cpustats);
168 u64_stats_update_begin(&s->syncp);
170 s->cnt.outbytes += skb->len;
171 u64_stats_update_end(&s->syncp);
173 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
174 u64_stats_update_begin(&s->syncp);
176 s->cnt.outbytes += skb->len;
177 u64_stats_update_end(&s->syncp);
185 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
187 struct netns_ipvs *ipvs = svc->ipvs;
188 struct ip_vs_cpu_stats *s;
192 s = this_cpu_ptr(cp->dest->stats.cpustats);
193 u64_stats_update_begin(&s->syncp);
195 u64_stats_update_end(&s->syncp);
197 s = this_cpu_ptr(svc->stats.cpustats);
198 u64_stats_update_begin(&s->syncp);
200 u64_stats_update_end(&s->syncp);
202 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
203 u64_stats_update_begin(&s->syncp);
205 u64_stats_update_end(&s->syncp);
212 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
213 const struct sk_buff *skb,
214 struct ip_vs_proto_data *pd)
216 if (likely(pd->pp->state_transition))
217 pd->pp->state_transition(cp, direction, skb, pd);
221 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
222 struct sk_buff *skb, int protocol,
223 const union nf_inet_addr *caddr, __be16 cport,
224 const union nf_inet_addr *vaddr, __be16 vport,
225 struct ip_vs_conn_param *p)
227 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
229 p->pe = rcu_dereference(svc->pe);
230 if (p->pe && p->pe->fill_param)
231 return p->pe->fill_param(p, skb);
237 * IPVS persistent scheduling function
238 * It creates a connection entry according to its template if exists,
239 * or selects a server and creates a connection entry plus a template.
240 * Locking: we are svc user (svc->refcnt), so we hold all dests too
241 * Protocols supported: TCP, UDP
243 static struct ip_vs_conn *
244 ip_vs_sched_persist(struct ip_vs_service *svc,
245 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
246 int *ignored, struct ip_vs_iphdr *iph)
248 struct ip_vs_conn *cp = NULL;
249 struct ip_vs_dest *dest;
250 struct ip_vs_conn *ct;
251 __be16 dport = 0; /* destination port to forward */
253 struct ip_vs_conn_param param;
254 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
255 union nf_inet_addr snet; /* source network of the client,
257 const union nf_inet_addr *src_addr, *dst_addr;
259 if (likely(!ip_vs_iph_inverse(iph))) {
260 src_addr = &iph->saddr;
261 dst_addr = &iph->daddr;
263 src_addr = &iph->daddr;
264 dst_addr = &iph->saddr;
268 /* Mask saddr with the netmask to adjust template granularity */
269 #ifdef CONFIG_IP_VS_IPV6
270 if (svc->af == AF_INET6)
271 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
272 (__force __u32) svc->netmask);
275 snet.ip = src_addr->ip & svc->netmask;
277 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
279 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
280 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
281 IP_VS_DBG_ADDR(svc->af, &snet));
284 * As far as we know, FTP is a very complicated network protocol, and
285 * it uses control connection and data connections. For active FTP,
286 * FTP server initialize data connection to the client, its source port
287 * is often 20. For passive FTP, FTP server tells the clients the port
288 * that it passively listens to, and the client issues the data
289 * connection. In the tunneling or direct routing mode, the load
290 * balancer is on the client-to-server half of connection, the port
291 * number is unknown to the load balancer. So, a conn template like
292 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
293 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
294 * is created for other persistent services.
297 int protocol = iph->protocol;
298 const union nf_inet_addr *vaddr = dst_addr;
301 if (dst_port == svc->port) {
303 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
305 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
307 if (svc->port != FTPPORT)
310 /* Note: persistent fwmark-based services and
311 * persistent port zero service are handled here.
313 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
314 * port zero template:
315 * <protocol,caddr,0,vaddr,0,daddr,0>
318 protocol = IPPROTO_IP;
322 /* return *ignored = -1 so NF_DROP can be used */
323 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
324 vaddr, vport, ¶m) < 0) {
330 /* Check if a template already exists */
331 ct = ip_vs_ct_in_get(¶m);
332 if (!ct || !ip_vs_check_template(ct, NULL)) {
333 struct ip_vs_scheduler *sched;
336 * No template found or the dest of the connection
337 * template is not available.
338 * return *ignored=0 i.e. ICMP and NF_DROP
340 sched = rcu_dereference(svc->scheduler);
342 /* read svc->sched_data after svc->scheduler */
344 dest = sched->schedule(svc, skb, iph);
349 IP_VS_DBG(1, "p-schedule: no dest found.\n");
350 kfree(param.pe_data);
355 if (dst_port == svc->port && svc->port != FTPPORT)
359 * This adds param.pe_data to the template,
360 * and thus param.pe_data will be destroyed
361 * when the template expires */
362 ct = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport,
363 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
365 kfree(param.pe_data);
370 ct->timeout = svc->timeout;
372 /* set destination with the found template */
374 kfree(param.pe_data);
378 if (dport == svc->port && dest->port)
381 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
382 && iph->protocol == IPPROTO_UDP) ?
383 IP_VS_CONN_F_ONE_PACKET : 0;
386 * Create a new connection according to the template
388 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
389 src_port, dst_addr, dst_port, ¶m);
391 cp = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, flags, dest,
402 ip_vs_control_add(cp, ct);
405 ip_vs_conn_stats(cp, svc);
411 * IPVS main scheduling function
412 * It selects a server according to the virtual service, and
413 * creates a connection entry.
414 * Protocols supported: TCP, UDP
418 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
419 * svc/scheduler decides that this packet should be accepted with
420 * NF_ACCEPT because it must not be scheduled.
422 * 0 : scheduler can not find destination, so try bypass or
423 * return ICMP and then NF_DROP (ip_vs_leave).
425 * -1 : scheduler tried to schedule but fatal error occurred, eg.
426 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
427 * failure such as missing Call-ID, ENOMEM on skb_linearize
428 * or pe_data. In this case we should return NF_DROP without
429 * any attempts to send ICMP with ip_vs_leave.
432 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
433 struct ip_vs_proto_data *pd, int *ignored,
434 struct ip_vs_iphdr *iph)
436 struct ip_vs_protocol *pp = pd->pp;
437 struct ip_vs_conn *cp = NULL;
438 struct ip_vs_scheduler *sched;
439 struct ip_vs_dest *dest;
440 __be16 _ports[2], *pptr, cport, vport;
441 const void *caddr, *vaddr;
446 * IPv6 frags, only the first hit here.
448 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
452 if (likely(!ip_vs_iph_inverse(iph))) {
465 * FTPDATA needs this check when using local real server.
466 * Never schedule Active FTPDATA connections from real server.
467 * For LVS-NAT they must be already created. For other methods
468 * with persistence the connection is created on SYN+ACK.
470 if (cport == FTPDATA) {
471 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
472 "Not scheduling FTPDATA");
477 * Do not schedule replies from local real server.
479 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
480 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
481 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
482 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
485 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
486 "Not scheduling reply for existing"
488 __ip_vs_conn_put(cp);
496 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
497 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
503 * Non-persistent service
505 if (!svc->fwmark && vport != svc->port) {
507 pr_err("Schedule: port zero only supported "
508 "in persistent services, "
509 "check your ipvs configuration\n");
513 sched = rcu_dereference(svc->scheduler);
515 /* read svc->sched_data after svc->scheduler */
517 dest = sched->schedule(svc, skb, iph);
522 IP_VS_DBG(1, "Schedule: no dest found.\n");
526 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
527 && iph->protocol == IPPROTO_UDP) ?
528 IP_VS_CONN_F_ONE_PACKET : 0;
531 * Create a connection entry.
534 struct ip_vs_conn_param p;
536 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
537 caddr, cport, vaddr, vport, &p);
538 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
539 dest->port ? dest->port : vport,
540 flags, dest, skb->mark);
547 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
548 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
550 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
551 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
552 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
553 cp->flags, refcount_read(&cp->refcnt));
555 ip_vs_conn_stats(cp, svc);
559 static inline int ip_vs_addr_is_unicast(struct net *net, int af,
560 union nf_inet_addr *addr)
562 #ifdef CONFIG_IP_VS_IPV6
564 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
566 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
570 * Pass or drop the packet.
571 * Called by ip_vs_in, when the virtual service is available but
572 * no destination is available for a new connection.
574 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
575 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
577 __be16 _ports[2], *pptr, dport;
578 struct netns_ipvs *ipvs = svc->ipvs;
579 struct net *net = ipvs->net;
581 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
584 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
586 /* if it is fwmark-based service, the cache_bypass sysctl is up
587 and the destination is a non-local unicast, then create
588 a cache_bypass connection entry */
589 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
590 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
591 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
593 struct ip_vs_conn *cp;
594 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
595 iph->protocol == IPPROTO_UDP) ?
596 IP_VS_CONN_F_ONE_PACKET : 0;
597 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
599 /* create a new connection entry */
600 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
602 struct ip_vs_conn_param p;
603 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
604 &iph->saddr, pptr[0],
605 &iph->daddr, pptr[1], &p);
606 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
607 IP_VS_CONN_F_BYPASS | flags,
614 ip_vs_in_stats(cp, skb);
617 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
619 /* transmit the first SYN packet */
620 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
621 /* do not touch skb anymore */
623 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
624 atomic_inc(&cp->control->in_pkts);
626 atomic_inc(&cp->in_pkts);
632 * When the virtual ftp service is presented, packets destined
633 * for other services on the VIP may get here (except services
634 * listed in the ipvs table), pass the packets, because it is
635 * not ipvs job to decide to drop the packets.
637 if (svc->port == FTPPORT && dport != FTPPORT)
640 if (unlikely(ip_vs_iph_icmp(iph)))
644 * Notify the client that the destination is unreachable, and
645 * release the socket buffer.
646 * Since it is in IP layer, the TCP socket is not actually
647 * created, the TCP RST packet cannot be sent, instead that
648 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
650 #ifdef CONFIG_IP_VS_IPV6
651 if (svc->af == AF_INET6) {
653 skb->dev = net->loopback_dev;
654 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
657 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
664 static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
666 return ipvs->sysctl_snat_reroute;
669 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
671 return ipvs->sysctl_nat_icmp_send;
674 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
676 return ipvs->sysctl_expire_nodest_conn;
681 static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
682 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
683 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
687 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
689 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
692 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
694 if (NF_INET_LOCAL_IN == hooknum)
695 return IP_DEFRAG_VS_IN;
696 if (NF_INET_FORWARD == hooknum)
697 return IP_DEFRAG_VS_FWD;
698 return IP_DEFRAG_VS_OUT;
701 static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
702 struct sk_buff *skb, u_int32_t user)
707 err = ip_defrag(ipvs->net, skb, user);
710 ip_send_check(ip_hdr(skb));
715 static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
716 struct sk_buff *skb, unsigned int hooknum)
718 if (!sysctl_snat_reroute(ipvs))
720 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
721 if (NF_INET_LOCAL_IN == hooknum)
723 #ifdef CONFIG_IP_VS_IPV6
724 if (af == AF_INET6) {
725 struct dst_entry *dst = skb_dst(skb);
727 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
728 ip6_route_me_harder(ipvs->net, skb) != 0)
732 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
733 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
740 * Packet has been made sufficiently writable in caller
741 * - inout: 1=in->out, 0=out->in
743 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
744 struct ip_vs_conn *cp, int inout)
746 struct iphdr *iph = ip_hdr(skb);
747 unsigned int icmp_offset = iph->ihl*4;
748 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
750 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
753 iph->saddr = cp->vaddr.ip;
755 ciph->daddr = cp->vaddr.ip;
758 iph->daddr = cp->daddr.ip;
760 ciph->saddr = cp->daddr.ip;
764 /* the TCP/UDP/SCTP port */
765 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
766 IPPROTO_SCTP == ciph->protocol) {
767 __be16 *ports = (void *)ciph + ciph->ihl*4;
770 ports[1] = cp->vport;
772 ports[0] = cp->dport;
775 /* And finally the ICMP checksum */
777 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
778 skb->ip_summed = CHECKSUM_UNNECESSARY;
781 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
782 "Forwarding altered outgoing ICMP");
784 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
785 "Forwarding altered incoming ICMP");
788 #ifdef CONFIG_IP_VS_IPV6
789 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
790 struct ip_vs_conn *cp, int inout)
792 struct ipv6hdr *iph = ipv6_hdr(skb);
793 unsigned int icmp_offset = 0;
794 unsigned int offs = 0; /* header offset*/
796 struct icmp6hdr *icmph;
797 struct ipv6hdr *ciph;
798 unsigned short fragoffs;
800 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
801 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
802 offs = icmp_offset + sizeof(struct icmp6hdr);
803 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
805 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
808 iph->saddr = cp->vaddr.in6;
809 ciph->daddr = cp->vaddr.in6;
811 iph->daddr = cp->daddr.in6;
812 ciph->saddr = cp->daddr.in6;
815 /* the TCP/UDP/SCTP port */
816 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
817 IPPROTO_SCTP == protocol)) {
818 __be16 *ports = (void *)(skb_network_header(skb) + offs);
820 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
821 ntohs(inout ? ports[1] : ports[0]),
822 ntohs(inout ? cp->vport : cp->dport));
824 ports[1] = cp->vport;
826 ports[0] = cp->dport;
829 /* And finally the ICMP checksum */
830 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
831 skb->len - icmp_offset,
833 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
834 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
835 skb->ip_summed = CHECKSUM_PARTIAL;
838 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
839 (void *)ciph - (void *)iph,
840 "Forwarding altered outgoing ICMPv6");
842 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
843 (void *)ciph - (void *)iph,
844 "Forwarding altered incoming ICMPv6");
848 /* Handle relevant response ICMP messages - forward to the right
851 static int handle_response_icmp(int af, struct sk_buff *skb,
852 union nf_inet_addr *snet,
853 __u8 protocol, struct ip_vs_conn *cp,
854 struct ip_vs_protocol *pp,
855 unsigned int offset, unsigned int ihl,
856 unsigned int hooknum)
858 unsigned int verdict = NF_DROP;
860 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
863 /* Ensure the checksum is correct */
864 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
865 /* Failed checksum! */
866 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
867 IP_VS_DBG_ADDR(af, snet));
871 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
872 IPPROTO_SCTP == protocol)
873 offset += 2 * sizeof(__u16);
874 if (!skb_make_writable(skb, offset))
877 #ifdef CONFIG_IP_VS_IPV6
879 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
882 ip_vs_nat_icmp(skb, pp, cp, 1);
884 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
887 /* do the statistics and put it back */
888 ip_vs_out_stats(cp, skb);
890 skb->ipvs_property = 1;
891 if (!(cp->flags & IP_VS_CONN_F_NFCT))
894 ip_vs_update_conntrack(skb, cp, 0);
900 __ip_vs_conn_put(cp);
906 * Handle ICMP messages in the inside-to-outside direction (outgoing).
907 * Find any that might be relevant, check against existing connections.
908 * Currently handles error types - unreachable, quench, ttl exceeded.
910 static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
911 int *related, unsigned int hooknum)
914 struct icmphdr _icmph, *ic;
915 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
916 struct ip_vs_iphdr ciph;
917 struct ip_vs_conn *cp;
918 struct ip_vs_protocol *pp;
919 unsigned int offset, ihl;
920 union nf_inet_addr snet;
924 /* reassemble IP fragments */
925 if (ip_is_fragment(ip_hdr(skb))) {
926 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
931 offset = ihl = iph->ihl * 4;
932 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
936 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
937 ic->type, ntohs(icmp_id(ic)),
938 &iph->saddr, &iph->daddr);
941 * Work through seeing if this is for us.
942 * These checks are supposed to be in an order that means easy
943 * things are checked first to speed up processing.... however
944 * this means that some packets will manage to get a long way
945 * down this stack and then be rejected, but that's life.
947 if ((ic->type != ICMP_DEST_UNREACH) &&
948 (ic->type != ICMP_SOURCE_QUENCH) &&
949 (ic->type != ICMP_TIME_EXCEEDED)) {
954 /* Now find the contained IP header */
955 offset += sizeof(_icmph);
956 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
958 return NF_ACCEPT; /* The packet looks wrong, ignore */
960 pp = ip_vs_proto_get(cih->protocol);
964 /* Is the embedded protocol header present? */
965 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
969 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
970 "Checking outgoing ICMP for");
972 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
974 /* The embedded headers contain source and dest in reverse order */
975 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
979 snet.ip = iph->saddr;
980 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
981 pp, ciph.len, ihl, hooknum);
984 #ifdef CONFIG_IP_VS_IPV6
985 static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
986 int *related, unsigned int hooknum,
987 struct ip_vs_iphdr *ipvsh)
989 struct icmp6hdr _icmph, *ic;
990 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
991 struct ip_vs_conn *cp;
992 struct ip_vs_protocol *pp;
993 union nf_inet_addr snet;
997 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph);
1002 * Work through seeing if this is for us.
1003 * These checks are supposed to be in an order that means easy
1004 * things are checked first to speed up processing.... however
1005 * this means that some packets will manage to get a long way
1006 * down this stack and then be rejected, but that's life.
1008 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1012 /* Fragment header that is before ICMP header tells us that:
1013 * it's not an error message since they can't be fragmented.
1015 if (ipvsh->flags & IP6_FH_F_FRAG)
1018 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1019 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1020 &ipvsh->saddr, &ipvsh->daddr);
1022 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1024 return NF_ACCEPT; /* The packet looks wrong, ignore */
1026 pp = ip_vs_proto_get(ciph.protocol);
1030 /* The embedded headers contain source and dest in reverse order */
1031 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
1035 snet.in6 = ciph.saddr.in6;
1037 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1038 pp, offset, sizeof(struct ipv6hdr),
1044 * Check if sctp chunc is ABORT chunk
1046 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1048 struct sctp_chunkhdr *sch, schunk;
1049 sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr),
1050 sizeof(schunk), &schunk);
1053 if (sch->type == SCTP_CID_ABORT)
1058 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1060 struct tcphdr _tcph, *th;
1062 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1068 static inline bool is_new_conn(const struct sk_buff *skb,
1069 struct ip_vs_iphdr *iph)
1071 switch (iph->protocol) {
1073 struct tcphdr _tcph, *th;
1075 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1080 case IPPROTO_SCTP: {
1081 struct sctp_chunkhdr *sch, schunk;
1083 sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr),
1084 sizeof(schunk), &schunk);
1087 return sch->type == SCTP_CID_INIT;
1094 static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1095 int conn_reuse_mode)
1097 /* Controlled (FTP DATA or persistence)? */
1101 switch (cp->protocol) {
1103 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1104 (cp->state == IP_VS_TCP_S_CLOSE) ||
1105 ((conn_reuse_mode & 2) &&
1106 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1107 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1109 return cp->state == IP_VS_SCTP_S_CLOSED;
1115 /* Generic function to create new connections for outgoing RS packets
1117 * Pre-requisites for successful connection creation:
1118 * 1) Virtual Service is NOT fwmark based:
1119 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1120 * 2) Real Server and Virtual Service were NOT configured without port:
1121 * This is to allow match of different VS to the same RS ip-addr
1123 struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1124 struct ip_vs_dest *dest,
1125 struct sk_buff *skb,
1126 const struct ip_vs_iphdr *iph,
1130 struct ip_vs_conn_param param;
1131 struct ip_vs_conn *ct = NULL, *cp = NULL;
1132 const union nf_inet_addr *vaddr, *daddr, *caddr;
1133 union nf_inet_addr snet;
1140 daddr = &iph->saddr;
1141 caddr = &iph->daddr;
1143 /* check pre-requisites are satisfied */
1146 if (!vport || !dport)
1149 /* for persistent service first create connection template */
1150 if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
1151 /* apply netmask the same way ingress-side does */
1152 #ifdef CONFIG_IP_VS_IPV6
1153 if (svc->af == AF_INET6)
1154 ipv6_addr_prefix(&snet.in6, &caddr->in6,
1155 (__force __u32)svc->netmask);
1158 snet.ip = caddr->ip & svc->netmask;
1159 /* fill params and create template if not existent */
1160 if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
1164 ct = ip_vs_ct_in_get(¶m);
1165 /* check if template exists and points to the same dest */
1166 if (!ct || !ip_vs_check_template(ct, dest)) {
1167 ct = ip_vs_conn_new(¶m, dest->af, daddr, dport,
1168 IP_VS_CONN_F_TEMPLATE, dest, 0);
1170 kfree(param.pe_data);
1173 ct->timeout = svc->timeout;
1175 kfree(param.pe_data);
1179 /* connection flags */
1180 flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
1181 iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
1182 /* create connection */
1183 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
1184 caddr, cport, vaddr, vport, ¶m);
1185 cp = ip_vs_conn_new(¶m, dest->af, daddr, dport, flags, dest, 0);
1192 ip_vs_control_add(cp, ct);
1195 ip_vs_conn_stats(cp, svc);
1197 /* return connection (will be used to handle outgoing packet) */
1198 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1199 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1201 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
1202 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
1203 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
1204 cp->flags, refcount_read(&cp->refcnt));
1209 /* Handle outgoing packets which are considered requests initiated by
1210 * real servers, so that subsequent responses from external client can be
1211 * routed to the right real server.
1212 * Used also for outgoing responses in OPS mode.
1214 * Connection management is handled by persistent-engine specific callback.
1216 static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
1217 struct netns_ipvs *ipvs,
1218 int af, struct sk_buff *skb,
1219 const struct ip_vs_iphdr *iph)
1221 struct ip_vs_dest *dest;
1222 struct ip_vs_conn *cp = NULL;
1223 __be16 _ports[2], *pptr;
1225 if (hooknum == NF_INET_LOCAL_IN)
1228 pptr = frag_safe_skb_hp(skb, iph->len,
1229 sizeof(_ports), _ports);
1233 dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
1234 &iph->saddr, pptr[0]);
1236 struct ip_vs_service *svc;
1237 struct ip_vs_pe *pe;
1239 svc = rcu_dereference(dest->svc);
1241 pe = rcu_dereference(svc->pe);
1242 if (pe && pe->conn_out)
1243 cp = pe->conn_out(svc, dest, skb, iph,
1251 /* Handle response packets: rewrite addresses and send away...
1254 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1255 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1256 unsigned int hooknum)
1258 struct ip_vs_protocol *pp = pd->pp;
1260 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
1262 if (!skb_make_writable(skb, iph->len))
1265 /* mangle the packet */
1266 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1269 #ifdef CONFIG_IP_VS_IPV6
1271 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1275 ip_hdr(skb)->saddr = cp->vaddr.ip;
1276 ip_send_check(ip_hdr(skb));
1280 * nf_iterate does not expect change in the skb->dst->dev.
1281 * It looks like it is not fatal to enable this code for hooks
1282 * where our handlers are at the end of the chain list and
1283 * when all next handlers use skb->dst->dev and not outdev.
1284 * It will definitely route properly the inout NAT traffic
1285 * when multiple paths are used.
1288 /* For policy routing, packets originating from this
1289 * machine itself may be routed differently to packets
1290 * passing through. We want this packet to be routed as
1291 * if it came from this machine itself. So re-compute
1292 * the routing information.
1294 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
1297 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
1299 ip_vs_out_stats(cp, skb);
1300 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1301 skb->ipvs_property = 1;
1302 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1305 ip_vs_update_conntrack(skb, cp, 0);
1319 * Check if outgoing packet belongs to the established ip_vs_conn.
1322 ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1324 struct ip_vs_iphdr iph;
1325 struct ip_vs_protocol *pp;
1326 struct ip_vs_proto_data *pd;
1327 struct ip_vs_conn *cp;
1332 /* Already marked as IPVS request or reply? */
1333 if (skb->ipvs_property)
1336 sk = skb_to_full_sk(skb);
1337 /* Bad... Do not break raw sockets */
1338 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
1341 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
1345 if (unlikely(!skb_dst(skb)))
1351 ip_vs_fill_iph_skb(af, skb, false, &iph);
1352 #ifdef CONFIG_IP_VS_IPV6
1353 if (af == AF_INET6) {
1354 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1356 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
1364 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1366 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
1372 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1377 /* reassemble IP fragments */
1378 #ifdef CONFIG_IP_VS_IPV6
1381 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1382 if (ip_vs_gather_frags(ipvs, skb,
1383 ip_vs_defrag_user(hooknum)))
1386 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
1390 * Check if the packet belongs to an existing entry
1392 cp = pp->conn_out_get(ipvs, af, skb, &iph);
1395 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
1397 return handle_response(af, skb, pd, cp, &iph, hooknum);
1400 /* Check for real-server-started requests */
1401 if (atomic_read(&ipvs->conn_out_counter)) {
1402 /* Currently only for UDP:
1403 * connection oriented protocols typically use
1404 * ephemeral ports for outgoing connections, so
1405 * related incoming responses would not match any VS
1407 if (pp->protocol == IPPROTO_UDP) {
1408 cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
1410 return handle_response(af, skb, pd, cp, &iph,
1415 if (sysctl_nat_icmp_send(ipvs) &&
1416 (pp->protocol == IPPROTO_TCP ||
1417 pp->protocol == IPPROTO_UDP ||
1418 pp->protocol == IPPROTO_SCTP)) {
1419 __be16 _ports[2], *pptr;
1421 pptr = frag_safe_skb_hp(skb, iph.len,
1422 sizeof(_ports), _ports);
1424 return NF_ACCEPT; /* Not for me */
1425 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
1428 * Notify the real server: there is no
1429 * existing entry if it is not RST
1430 * packet or not TCP packet.
1432 if ((iph.protocol != IPPROTO_TCP &&
1433 iph.protocol != IPPROTO_SCTP)
1434 || ((iph.protocol == IPPROTO_TCP
1435 && !is_tcp_reset(skb, iph.len))
1436 || (iph.protocol == IPPROTO_SCTP
1437 && !is_sctp_abort(skb,
1439 #ifdef CONFIG_IP_VS_IPV6
1440 if (af == AF_INET6) {
1442 skb->dev = ipvs->net->loopback_dev;
1444 ICMPV6_DEST_UNREACH,
1445 ICMPV6_PORT_UNREACH,
1451 ICMP_PORT_UNREACH, 0);
1458 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
1459 "ip_vs_out: packet continues traversal as normal");
1463 __ip_vs_conn_put(cp);
1468 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1469 * used only for VS/NAT.
1470 * Check if packet is reply for established ip_vs_conn.
1473 ip_vs_reply4(void *priv, struct sk_buff *skb,
1474 const struct nf_hook_state *state)
1476 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1480 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1481 * Check if packet is reply for established ip_vs_conn.
1484 ip_vs_local_reply4(void *priv, struct sk_buff *skb,
1485 const struct nf_hook_state *state)
1487 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1490 #ifdef CONFIG_IP_VS_IPV6
1493 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1494 * used only for VS/NAT.
1495 * Check if packet is reply for established ip_vs_conn.
1498 ip_vs_reply6(void *priv, struct sk_buff *skb,
1499 const struct nf_hook_state *state)
1501 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1505 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1506 * Check if packet is reply for established ip_vs_conn.
1509 ip_vs_local_reply6(void *priv, struct sk_buff *skb,
1510 const struct nf_hook_state *state)
1512 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1518 ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1519 struct ip_vs_proto_data *pd,
1520 int *verdict, struct ip_vs_conn **cpp,
1521 struct ip_vs_iphdr *iph)
1523 struct ip_vs_protocol *pp = pd->pp;
1525 if (!iph->fragoffs) {
1526 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1527 * replayed fragment zero will already have created the cp
1530 /* Schedule and create new connection entry into cpp */
1531 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
1535 if (unlikely(!*cpp)) {
1536 /* sorry, all this trouble for a no-hit :) */
1537 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1538 "ip_vs_in: packet continues traversal as normal");
1539 if (iph->fragoffs) {
1540 /* Fragment that couldn't be mapped to a conn entry
1541 * is missing module nf_defrag_ipv6
1543 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1544 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1545 "unhandled fragment");
1547 *verdict = NF_ACCEPT;
1555 * Handle ICMP messages in the outside-to-inside direction (incoming).
1556 * Find any that might be relevant, check against existing connections,
1557 * forward to the right destination host if relevant.
1558 * Currently handles error types - unreachable, quench, ttl exceeded.
1561 ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1562 unsigned int hooknum)
1565 struct icmphdr _icmph, *ic;
1566 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1567 struct ip_vs_iphdr ciph;
1568 struct ip_vs_conn *cp;
1569 struct ip_vs_protocol *pp;
1570 struct ip_vs_proto_data *pd;
1571 unsigned int offset, offset2, ihl, verdict;
1572 bool ipip, new_cp = false;
1576 /* reassemble IP fragments */
1577 if (ip_is_fragment(ip_hdr(skb))) {
1578 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
1583 offset = ihl = iph->ihl * 4;
1584 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1588 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1589 ic->type, ntohs(icmp_id(ic)),
1590 &iph->saddr, &iph->daddr);
1593 * Work through seeing if this is for us.
1594 * These checks are supposed to be in an order that means easy
1595 * things are checked first to speed up processing.... however
1596 * this means that some packets will manage to get a long way
1597 * down this stack and then be rejected, but that's life.
1599 if ((ic->type != ICMP_DEST_UNREACH) &&
1600 (ic->type != ICMP_SOURCE_QUENCH) &&
1601 (ic->type != ICMP_TIME_EXCEEDED)) {
1606 /* Now find the contained IP header */
1607 offset += sizeof(_icmph);
1608 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1610 return NF_ACCEPT; /* The packet looks wrong, ignore */
1612 /* Special case for errors for IPIP packets */
1614 if (cih->protocol == IPPROTO_IPIP) {
1615 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1617 /* Error for our IPIP must arrive at LOCAL_IN */
1618 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1620 offset += cih->ihl * 4;
1621 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1623 return NF_ACCEPT; /* The packet looks wrong, ignore */
1627 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
1632 /* Is the embedded protocol header present? */
1633 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1637 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1638 "Checking incoming ICMP for");
1641 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
1644 /* The embedded headers contain source and dest in reverse order.
1645 * For IPIP this is error for request, not for reply.
1647 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
1652 if (!sysctl_schedule_icmp(ipvs))
1655 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
1662 /* Ensure the checksum is correct */
1663 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1664 /* Failed checksum! */
1665 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1671 __be32 info = ic->un.gateway;
1672 __u8 type = ic->type;
1673 __u8 code = ic->code;
1675 /* Update the MTU */
1676 if (ic->type == ICMP_DEST_UNREACH &&
1677 ic->code == ICMP_FRAG_NEEDED) {
1678 struct ip_vs_dest *dest = cp->dest;
1679 u32 mtu = ntohs(ic->un.frag.mtu);
1680 __be16 frag_off = cih->frag_off;
1682 /* Strip outer IP and ICMP, go to IPIP header */
1683 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1685 offset2 -= ihl + sizeof(_icmph);
1686 skb_reset_network_header(skb);
1687 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1688 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1689 ipv4_update_pmtu(skb, ipvs->net, mtu, 0, 0);
1690 /* Client uses PMTUD? */
1691 if (!(frag_off & htons(IP_DF)))
1693 /* Prefer the resulting PMTU */
1695 struct ip_vs_dest_dst *dest_dst;
1697 dest_dst = rcu_dereference(dest->dest_dst);
1699 mtu = dst_mtu(dest_dst->dst_cache);
1701 if (mtu > 68 + sizeof(struct iphdr))
1702 mtu -= sizeof(struct iphdr);
1705 /* Strip outer IP, ICMP and IPIP, go to IP header of
1708 if (pskb_pull(skb, offset2) == NULL)
1710 skb_reset_network_header(skb);
1711 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1712 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1713 type, code, ntohl(info));
1714 icmp_send(skb, type, code, info);
1715 /* ICMP can be shorter but anyways, account it */
1716 ip_vs_out_stats(cp, skb);
1720 verdict = NF_STOLEN;
1724 /* do the statistics and put it back */
1725 ip_vs_in_stats(cp, skb);
1726 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1727 IPPROTO_SCTP == cih->protocol)
1728 offset += 2 * sizeof(__u16);
1729 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1732 if (likely(!new_cp))
1733 __ip_vs_conn_put(cp);
1740 #ifdef CONFIG_IP_VS_IPV6
1741 static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1742 int *related, unsigned int hooknum,
1743 struct ip_vs_iphdr *iph)
1745 struct icmp6hdr _icmph, *ic;
1746 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1747 struct ip_vs_conn *cp;
1748 struct ip_vs_protocol *pp;
1749 struct ip_vs_proto_data *pd;
1750 unsigned int offset, verdict;
1751 bool new_cp = false;
1755 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph);
1760 * Work through seeing if this is for us.
1761 * These checks are supposed to be in an order that means easy
1762 * things are checked first to speed up processing.... however
1763 * this means that some packets will manage to get a long way
1764 * down this stack and then be rejected, but that's life.
1766 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1770 /* Fragment header that is before ICMP header tells us that:
1771 * it's not an error message since they can't be fragmented.
1773 if (iph->flags & IP6_FH_F_FRAG)
1776 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1777 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1778 &iph->saddr, &iph->daddr);
1780 offset = iph->len + sizeof(_icmph);
1781 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
1784 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
1789 /* Cannot handle fragmented embedded protocol */
1793 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1794 "Checking incoming ICMPv6 for");
1796 /* The embedded headers contain source and dest in reverse order
1797 * if not from localhost
1799 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
1804 if (!sysctl_schedule_icmp(ipvs))
1807 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
1813 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1814 if ((hooknum == NF_INET_LOCAL_OUT) &&
1815 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1816 verdict = NF_ACCEPT;
1820 /* do the statistics and put it back */
1821 ip_vs_in_stats(cp, skb);
1823 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1825 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1826 IPPROTO_SCTP == ciph.protocol)
1827 offset += 2 * sizeof(__u16); /* Also mangle ports */
1829 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
1832 if (likely(!new_cp))
1833 __ip_vs_conn_put(cp);
1843 * Check if it's for virtual services, look it up,
1844 * and send it on its way...
1847 ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1849 struct ip_vs_iphdr iph;
1850 struct ip_vs_protocol *pp;
1851 struct ip_vs_proto_data *pd;
1852 struct ip_vs_conn *cp;
1854 int conn_reuse_mode;
1857 /* Already marked as IPVS request or reply? */
1858 if (skb->ipvs_property)
1863 * - remote client: only PACKET_HOST
1864 * - route: used for struct net when skb->dev is unset
1866 if (unlikely((skb->pkt_type != PACKET_HOST &&
1867 hooknum != NF_INET_LOCAL_OUT) ||
1869 ip_vs_fill_iph_skb(af, skb, false, &iph);
1870 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1871 " ignored in hook %u\n",
1872 skb->pkt_type, iph.protocol,
1873 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1876 /* ipvs enabled in this netns ? */
1877 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1880 ip_vs_fill_iph_skb(af, skb, false, &iph);
1882 /* Bad... Do not break raw sockets */
1883 sk = skb_to_full_sk(skb);
1884 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
1887 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
1891 #ifdef CONFIG_IP_VS_IPV6
1892 if (af == AF_INET6) {
1893 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1895 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1903 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1905 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1912 /* Protocol supported? */
1913 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1914 if (unlikely(!pd)) {
1915 /* The only way we'll see this packet again is if it's
1916 * encapsulated, so mark it with ipvs_property=1 so we
1917 * skip it if we're ignoring tunneled packets
1919 if (sysctl_ignore_tunneled(ipvs))
1920 skb->ipvs_property = 1;
1926 * Check if the packet belongs to an existing connection entry
1928 cp = pp->conn_in_get(ipvs, af, skb, &iph);
1930 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1931 if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
1932 bool uses_ct = false, resched = false;
1934 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1935 unlikely(!atomic_read(&cp->dest->weight))) {
1937 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1938 } else if (is_new_conn_expected(cp, conn_reuse_mode)) {
1939 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1940 if (!atomic_read(&cp->n_control)) {
1943 /* Do not reschedule controlling connection
1944 * that uses conntrack while it is still
1945 * referenced by controlled connection(s).
1952 if (!atomic_read(&cp->n_control))
1953 ip_vs_conn_expire_now(cp);
1954 __ip_vs_conn_put(cp);
1961 if (unlikely(!cp)) {
1964 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
1968 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
1970 /* Check the server status */
1971 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1972 /* the destination server is not available */
1974 __u32 flags = cp->flags;
1976 /* when timer already started, silently drop the packet.*/
1977 if (timer_pending(&cp->timer))
1978 __ip_vs_conn_put(cp);
1982 if (sysctl_expire_nodest_conn(ipvs) &&
1983 !(flags & IP_VS_CONN_F_ONE_PACKET)) {
1984 /* try to expire the connection immediately */
1985 ip_vs_conn_expire_now(cp);
1991 ip_vs_in_stats(cp, skb);
1992 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1993 if (cp->packet_xmit)
1994 ret = cp->packet_xmit(skb, cp, pp, &iph);
1995 /* do not touch skb anymore */
1997 IP_VS_DBG_RL("warning: packet_xmit is null");
2001 /* Increase its packet counter and check if it is needed
2002 * to be synchronized
2004 * Sync connection if it is about to close to
2005 * encorage the standby servers to update the connections timeout
2007 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
2010 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
2011 pkts = sysctl_sync_threshold(ipvs);
2013 pkts = atomic_add_return(1, &cp->in_pkts);
2015 if (ipvs->sync_state & IP_VS_STATE_MASTER)
2016 ip_vs_sync_conn(ipvs, cp, pkts);
2017 else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
2018 /* increment is done inside ip_vs_sync_conn too */
2019 atomic_inc(&cp->control->in_pkts);
2026 * AF_INET handler in NF_INET_LOCAL_IN chain
2027 * Schedule and forward packets from remote clients
2030 ip_vs_remote_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);
2037 * AF_INET handler in NF_INET_LOCAL_OUT chain
2038 * Schedule and forward packets from local clients
2041 ip_vs_local_request4(void *priv, struct sk_buff *skb,
2042 const struct nf_hook_state *state)
2044 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
2047 #ifdef CONFIG_IP_VS_IPV6
2050 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2051 * Schedule and forward packets from remote clients
2054 ip_vs_remote_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);
2061 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2062 * Schedule and forward packets from local clients
2065 ip_vs_local_request6(void *priv, struct sk_buff *skb,
2066 const struct nf_hook_state *state)
2068 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
2075 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
2076 * related packets destined for 0.0.0.0/0.
2077 * When fwmark-based virtual service is used, such as transparent
2078 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2079 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
2080 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
2081 * and send them to ip_vs_in_icmp.
2084 ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
2085 const struct nf_hook_state *state)
2088 struct netns_ipvs *ipvs = net_ipvs(state->net);
2090 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
2093 /* ipvs enabled in this netns ? */
2094 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2097 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
2100 #ifdef CONFIG_IP_VS_IPV6
2102 ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
2103 const struct nf_hook_state *state)
2106 struct netns_ipvs *ipvs = net_ipvs(state->net);
2107 struct ip_vs_iphdr iphdr;
2109 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
2110 if (iphdr.protocol != IPPROTO_ICMPV6)
2113 /* ipvs enabled in this netns ? */
2114 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
2117 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
2122 static const struct nf_hook_ops ip_vs_ops[] = {
2123 /* After packet filtering, change source only for VS/NAT */
2125 .hook = ip_vs_reply4,
2127 .hooknum = NF_INET_LOCAL_IN,
2128 .priority = NF_IP_PRI_NAT_SRC - 2,
2130 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2131 * or VS/NAT(change destination), so that filtering rules can be
2132 * applied to IPVS. */
2134 .hook = ip_vs_remote_request4,
2136 .hooknum = NF_INET_LOCAL_IN,
2137 .priority = NF_IP_PRI_NAT_SRC - 1,
2139 /* Before ip_vs_in, change source only for VS/NAT */
2141 .hook = ip_vs_local_reply4,
2143 .hooknum = NF_INET_LOCAL_OUT,
2144 .priority = NF_IP_PRI_NAT_DST + 1,
2146 /* After mangle, schedule and forward local requests */
2148 .hook = ip_vs_local_request4,
2150 .hooknum = NF_INET_LOCAL_OUT,
2151 .priority = NF_IP_PRI_NAT_DST + 2,
2153 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2154 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2156 .hook = ip_vs_forward_icmp,
2158 .hooknum = NF_INET_FORWARD,
2161 /* After packet filtering, change source only for VS/NAT */
2163 .hook = ip_vs_reply4,
2165 .hooknum = NF_INET_FORWARD,
2168 #ifdef CONFIG_IP_VS_IPV6
2169 /* After packet filtering, change source only for VS/NAT */
2171 .hook = ip_vs_reply6,
2173 .hooknum = NF_INET_LOCAL_IN,
2174 .priority = NF_IP6_PRI_NAT_SRC - 2,
2176 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2177 * or VS/NAT(change destination), so that filtering rules can be
2178 * applied to IPVS. */
2180 .hook = ip_vs_remote_request6,
2182 .hooknum = NF_INET_LOCAL_IN,
2183 .priority = NF_IP6_PRI_NAT_SRC - 1,
2185 /* Before ip_vs_in, change source only for VS/NAT */
2187 .hook = ip_vs_local_reply6,
2189 .hooknum = NF_INET_LOCAL_OUT,
2190 .priority = NF_IP6_PRI_NAT_DST + 1,
2192 /* After mangle, schedule and forward local requests */
2194 .hook = ip_vs_local_request6,
2196 .hooknum = NF_INET_LOCAL_OUT,
2197 .priority = NF_IP6_PRI_NAT_DST + 2,
2199 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2200 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2202 .hook = ip_vs_forward_icmp_v6,
2204 .hooknum = NF_INET_FORWARD,
2207 /* After packet filtering, change source only for VS/NAT */
2209 .hook = ip_vs_reply6,
2211 .hooknum = NF_INET_FORWARD,
2217 * Initialize IP Virtual Server netns mem.
2219 static int __net_init __ip_vs_init(struct net *net)
2221 struct netns_ipvs *ipvs;
2224 ipvs = net_generic(net, ip_vs_net_id);
2228 /* Hold the beast until a service is registerd */
2231 /* Counters used for creating unique names */
2232 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2233 atomic_inc(&ipvs_netns_cnt);
2236 if (ip_vs_estimator_net_init(ipvs) < 0)
2237 goto estimator_fail;
2239 if (ip_vs_control_net_init(ipvs) < 0)
2242 if (ip_vs_protocol_net_init(ipvs) < 0)
2245 if (ip_vs_app_net_init(ipvs) < 0)
2248 if (ip_vs_conn_net_init(ipvs) < 0)
2251 if (ip_vs_sync_net_init(ipvs) < 0)
2254 ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2264 ip_vs_sync_net_cleanup(ipvs);
2266 ip_vs_conn_net_cleanup(ipvs);
2268 ip_vs_app_net_cleanup(ipvs);
2270 ip_vs_protocol_net_cleanup(ipvs);
2272 ip_vs_control_net_cleanup(ipvs);
2274 ip_vs_estimator_net_cleanup(ipvs);
2280 static void __net_exit __ip_vs_cleanup(struct net *net)
2282 struct netns_ipvs *ipvs = net_ipvs(net);
2284 nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2285 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
2286 ip_vs_conn_net_cleanup(ipvs);
2287 ip_vs_app_net_cleanup(ipvs);
2288 ip_vs_protocol_net_cleanup(ipvs);
2289 ip_vs_control_net_cleanup(ipvs);
2290 ip_vs_estimator_net_cleanup(ipvs);
2291 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
2295 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2297 struct netns_ipvs *ipvs = net_ipvs(net);
2299 ipvs->enable = 0; /* Disable packet reception */
2301 ip_vs_sync_net_cleanup(ipvs);
2305 static struct pernet_operations ipvs_core_ops = {
2306 .init = __ip_vs_init,
2307 .exit = __ip_vs_cleanup,
2308 .id = &ip_vs_net_id,
2309 .size = sizeof(struct netns_ipvs),
2312 static struct pernet_operations ipvs_core_dev_ops = {
2313 .exit = __ip_vs_dev_cleanup,
2317 * Initialize IP Virtual Server
2319 static int __init ip_vs_init(void)
2323 ret = ip_vs_control_init();
2325 pr_err("can't setup control.\n");
2329 ip_vs_protocol_init();
2331 ret = ip_vs_conn_init();
2333 pr_err("can't setup connection table.\n");
2334 goto cleanup_protocol;
2337 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2341 ret = register_pernet_device(&ipvs_core_dev_ops);
2345 ret = ip_vs_register_nl_ioctl();
2347 pr_err("can't register netlink/ioctl.\n");
2351 pr_info("ipvs loaded.\n");
2356 unregister_pernet_device(&ipvs_core_dev_ops);
2358 unregister_pernet_subsys(&ipvs_core_ops);
2360 ip_vs_conn_cleanup();
2362 ip_vs_protocol_cleanup();
2363 ip_vs_control_cleanup();
2368 static void __exit ip_vs_cleanup(void)
2370 ip_vs_unregister_nl_ioctl();
2371 unregister_pernet_device(&ipvs_core_dev_ops);
2372 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2373 ip_vs_conn_cleanup();
2374 ip_vs_protocol_cleanup();
2375 ip_vs_control_cleanup();
2376 pr_info("ipvs unloaded.\n");
2379 module_init(ip_vs_init);
2380 module_exit(ip_vs_cleanup);
2381 MODULE_LICENSE("GPL");