2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the Netfilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
22 * Paul `Rusty' Russell properly handle non-linear skbs
23 * Harald Welte don't use nfcache
27 #define KMSG_COMPONENT "IPVS"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #include <linux/tcp.h>
34 #include <linux/sctp.h>
35 #include <linux/icmp.h>
36 #include <linux/slab.h>
41 #include <net/icmp.h> /* for icmp_send */
42 #include <net/route.h>
43 #include <net/ip6_checksum.h>
44 #include <net/netns/generic.h> /* net_generic() */
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv4.h>
49 #ifdef CONFIG_IP_VS_IPV6
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_route.h>
55 #include <net/ip_vs.h>
58 EXPORT_SYMBOL(register_ip_vs_scheduler);
59 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
60 EXPORT_SYMBOL(ip_vs_proto_name);
61 EXPORT_SYMBOL(ip_vs_conn_new);
62 EXPORT_SYMBOL(ip_vs_conn_in_get);
63 EXPORT_SYMBOL(ip_vs_conn_out_get);
64 #ifdef CONFIG_IP_VS_PROTO_TCP
65 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
67 EXPORT_SYMBOL(ip_vs_conn_put);
68 #ifdef CONFIG_IP_VS_DEBUG
69 EXPORT_SYMBOL(ip_vs_get_debug_level);
72 static int ip_vs_net_id __read_mostly;
73 /* netns cnt used for uniqueness */
74 static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
76 /* ID used in ICMP lookups */
77 #define icmp_id(icmph) (((icmph)->un).echo.id)
78 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
80 const char *ip_vs_proto_name(unsigned int proto)
95 #ifdef CONFIG_IP_VS_IPV6
100 sprintf(buf, "IP_%u", proto);
105 void ip_vs_init_hash_table(struct list_head *table, int rows)
108 INIT_LIST_HEAD(&table[rows]);
112 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
114 struct ip_vs_dest *dest = cp->dest;
115 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
118 struct ip_vs_cpu_stats *s;
119 struct ip_vs_service *svc;
121 s = this_cpu_ptr(dest->stats.cpustats);
123 u64_stats_update_begin(&s->syncp);
124 s->ustats.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp);
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
138 u64_stats_update_begin(&s->syncp);
139 s->ustats.inbytes += skb->len;
140 u64_stats_update_end(&s->syncp);
146 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
148 struct ip_vs_dest *dest = cp->dest;
149 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
152 struct ip_vs_cpu_stats *s;
153 struct ip_vs_service *svc;
155 s = this_cpu_ptr(dest->stats.cpustats);
157 u64_stats_update_begin(&s->syncp);
158 s->ustats.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp);
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
165 u64_stats_update_begin(&s->syncp);
166 s->ustats.outbytes += skb->len;
167 u64_stats_update_end(&s->syncp);
170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
172 u64_stats_update_begin(&s->syncp);
173 s->ustats.outbytes += skb->len;
174 u64_stats_update_end(&s->syncp);
180 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
182 struct netns_ipvs *ipvs = net_ipvs(svc->net);
183 struct ip_vs_cpu_stats *s;
185 s = this_cpu_ptr(cp->dest->stats.cpustats);
188 s = this_cpu_ptr(svc->stats.cpustats);
191 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
197 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
198 const struct sk_buff *skb,
199 struct ip_vs_proto_data *pd)
201 if (likely(pd->pp->state_transition))
202 pd->pp->state_transition(cp, direction, skb, pd);
206 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
207 struct sk_buff *skb, int protocol,
208 const union nf_inet_addr *caddr, __be16 cport,
209 const union nf_inet_addr *vaddr, __be16 vport,
210 struct ip_vs_conn_param *p)
212 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
214 p->pe = rcu_dereference(svc->pe);
215 if (p->pe && p->pe->fill_param)
216 return p->pe->fill_param(p, skb);
222 * IPVS persistent scheduling function
223 * It creates a connection entry according to its template if exists,
224 * or selects a server and creates a connection entry plus a template.
225 * Locking: we are svc user (svc->refcnt), so we hold all dests too
226 * Protocols supported: TCP, UDP
228 static struct ip_vs_conn *
229 ip_vs_sched_persist(struct ip_vs_service *svc,
230 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
231 int *ignored, struct ip_vs_iphdr *iph)
233 struct ip_vs_conn *cp = NULL;
234 struct ip_vs_dest *dest;
235 struct ip_vs_conn *ct;
236 __be16 dport = 0; /* destination port to forward */
238 struct ip_vs_conn_param param;
239 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
240 union nf_inet_addr snet; /* source network of the client,
243 /* Mask saddr with the netmask to adjust template granularity */
244 #ifdef CONFIG_IP_VS_IPV6
245 if (svc->af == AF_INET6)
246 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
247 (__force __u32) svc->netmask);
250 snet.ip = iph->saddr.ip & svc->netmask;
252 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
254 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
255 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
256 IP_VS_DBG_ADDR(svc->af, &snet));
259 * As far as we know, FTP is a very complicated network protocol, and
260 * it uses control connection and data connections. For active FTP,
261 * FTP server initialize data connection to the client, its source port
262 * is often 20. For passive FTP, FTP server tells the clients the port
263 * that it passively listens to, and the client issues the data
264 * connection. In the tunneling or direct routing mode, the load
265 * balancer is on the client-to-server half of connection, the port
266 * number is unknown to the load balancer. So, a conn template like
267 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
268 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
269 * is created for other persistent services.
272 int protocol = iph->protocol;
273 const union nf_inet_addr *vaddr = &iph->daddr;
276 if (dst_port == svc->port) {
278 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
280 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
282 if (svc->port != FTPPORT)
285 /* Note: persistent fwmark-based services and
286 * persistent port zero service are handled here.
288 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
289 * port zero template:
290 * <protocol,caddr,0,vaddr,0,daddr,0>
293 protocol = IPPROTO_IP;
297 /* return *ignored = -1 so NF_DROP can be used */
298 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
299 vaddr, vport, ¶m) < 0) {
305 /* Check if a template already exists */
306 ct = ip_vs_ct_in_get(¶m);
307 if (!ct || !ip_vs_check_template(ct)) {
308 struct ip_vs_scheduler *sched;
311 * No template found or the dest of the connection
312 * template is not available.
313 * return *ignored=0 i.e. ICMP and NF_DROP
315 sched = rcu_dereference(svc->scheduler);
316 dest = sched->schedule(svc, skb, iph);
318 IP_VS_DBG(1, "p-schedule: no dest found.\n");
319 kfree(param.pe_data);
324 if (dst_port == svc->port && svc->port != FTPPORT)
328 * This adds param.pe_data to the template,
329 * and thus param.pe_data will be destroyed
330 * when the template expires */
331 ct = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport,
332 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
334 kfree(param.pe_data);
339 ct->timeout = svc->timeout;
341 /* set destination with the found template */
343 kfree(param.pe_data);
347 if (dport == svc->port && dest->port)
350 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
351 && iph->protocol == IPPROTO_UDP) ?
352 IP_VS_CONN_F_ONE_PACKET : 0;
355 * Create a new connection according to the template
357 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
358 src_port, &iph->daddr, dst_port, ¶m);
360 cp = ip_vs_conn_new(¶m, dest->af, &dest->addr, dport, flags, dest,
371 ip_vs_control_add(cp, ct);
374 ip_vs_conn_stats(cp, svc);
380 * IPVS main scheduling function
381 * It selects a server according to the virtual service, and
382 * creates a connection entry.
383 * Protocols supported: TCP, UDP
387 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
388 * svc/scheduler decides that this packet should be accepted with
389 * NF_ACCEPT because it must not be scheduled.
391 * 0 : scheduler can not find destination, so try bypass or
392 * return ICMP and then NF_DROP (ip_vs_leave).
394 * -1 : scheduler tried to schedule but fatal error occurred, eg.
395 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
396 * failure such as missing Call-ID, ENOMEM on skb_linearize
397 * or pe_data. In this case we should return NF_DROP without
398 * any attempts to send ICMP with ip_vs_leave.
401 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
402 struct ip_vs_proto_data *pd, int *ignored,
403 struct ip_vs_iphdr *iph)
405 struct ip_vs_protocol *pp = pd->pp;
406 struct ip_vs_conn *cp = NULL;
407 struct ip_vs_scheduler *sched;
408 struct ip_vs_dest *dest;
409 __be16 _ports[2], *pptr;
414 * IPv6 frags, only the first hit here.
416 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
421 * FTPDATA needs this check when using local real server.
422 * Never schedule Active FTPDATA connections from real server.
423 * For LVS-NAT they must be already created. For other methods
424 * with persistence the connection is created on SYN+ACK.
426 if (pptr[0] == FTPDATA) {
427 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
428 "Not scheduling FTPDATA");
433 * Do not schedule replies from local real server.
435 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
436 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
437 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
438 "Not scheduling reply for existing connection");
439 __ip_vs_conn_put(cp);
446 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
447 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
453 * Non-persistent service
455 if (!svc->fwmark && pptr[1] != svc->port) {
457 pr_err("Schedule: port zero only supported "
458 "in persistent services, "
459 "check your ipvs configuration\n");
463 sched = rcu_dereference(svc->scheduler);
464 dest = sched->schedule(svc, skb, iph);
466 IP_VS_DBG(1, "Schedule: no dest found.\n");
470 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
471 && iph->protocol == IPPROTO_UDP) ?
472 IP_VS_CONN_F_ONE_PACKET : 0;
475 * Create a connection entry.
478 struct ip_vs_conn_param p;
480 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
481 &iph->saddr, pptr[0], &iph->daddr,
483 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
484 dest->port ? dest->port : pptr[1],
485 flags, dest, skb->mark);
492 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
493 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
495 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
496 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
497 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
498 cp->flags, atomic_read(&cp->refcnt));
500 ip_vs_conn_stats(cp, svc);
506 * Pass or drop the packet.
507 * Called by ip_vs_in, when the virtual service is available but
508 * no destination is available for a new connection.
510 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
511 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
513 __be16 _ports[2], *pptr;
516 struct netns_ipvs *ipvs;
520 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
528 #ifdef CONFIG_IP_VS_IPV6
529 if (svc->af == AF_INET6)
530 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
533 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
535 /* if it is fwmark-based service, the cache_bypass sysctl is up
536 and the destination is a non-local unicast, then create
537 a cache_bypass connection entry */
538 ipvs = net_ipvs(net);
539 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
541 struct ip_vs_conn *cp;
542 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
543 iph->protocol == IPPROTO_UDP) ?
544 IP_VS_CONN_F_ONE_PACKET : 0;
545 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
547 /* create a new connection entry */
548 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
550 struct ip_vs_conn_param p;
551 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
552 &iph->saddr, pptr[0],
553 &iph->daddr, pptr[1], &p);
554 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
555 IP_VS_CONN_F_BYPASS | flags,
562 ip_vs_in_stats(cp, skb);
565 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
567 /* transmit the first SYN packet */
568 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
569 /* do not touch skb anymore */
571 atomic_inc(&cp->in_pkts);
578 * When the virtual ftp service is presented, packets destined
579 * for other services on the VIP may get here (except services
580 * listed in the ipvs table), pass the packets, because it is
581 * not ipvs job to decide to drop the packets.
583 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
587 * Notify the client that the destination is unreachable, and
588 * release the socket buffer.
589 * Since it is in IP layer, the TCP socket is not actually
590 * created, the TCP RST packet cannot be sent, instead that
591 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
593 #ifdef CONFIG_IP_VS_IPV6
594 if (svc->af == AF_INET6) {
596 struct net *net_ = dev_net(skb_dst(skb)->dev);
598 skb->dev = net_->loopback_dev;
600 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
603 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
610 static int sysctl_snat_reroute(struct sk_buff *skb)
612 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
613 return ipvs->sysctl_snat_reroute;
616 static int sysctl_nat_icmp_send(struct net *net)
618 struct netns_ipvs *ipvs = net_ipvs(net);
619 return ipvs->sysctl_nat_icmp_send;
622 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
624 return ipvs->sysctl_expire_nodest_conn;
629 static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
630 static int sysctl_nat_icmp_send(struct net *net) { return 0; }
631 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
635 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
637 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
640 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
642 if (NF_INET_LOCAL_IN == hooknum)
643 return IP_DEFRAG_VS_IN;
644 if (NF_INET_FORWARD == hooknum)
645 return IP_DEFRAG_VS_FWD;
646 return IP_DEFRAG_VS_OUT;
649 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
654 err = ip_defrag(skb, user);
657 ip_send_check(ip_hdr(skb));
662 static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
664 #ifdef CONFIG_IP_VS_IPV6
665 if (af == AF_INET6) {
666 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
670 if ((sysctl_snat_reroute(skb) ||
671 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
672 ip_route_me_harder(skb, RTN_LOCAL) != 0)
679 * Packet has been made sufficiently writable in caller
680 * - inout: 1=in->out, 0=out->in
682 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
683 struct ip_vs_conn *cp, int inout)
685 struct iphdr *iph = ip_hdr(skb);
686 unsigned int icmp_offset = iph->ihl*4;
687 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
689 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
692 iph->saddr = cp->vaddr.ip;
694 ciph->daddr = cp->vaddr.ip;
697 iph->daddr = cp->daddr.ip;
699 ciph->saddr = cp->daddr.ip;
703 /* the TCP/UDP/SCTP port */
704 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
705 IPPROTO_SCTP == ciph->protocol) {
706 __be16 *ports = (void *)ciph + ciph->ihl*4;
709 ports[1] = cp->vport;
711 ports[0] = cp->dport;
714 /* And finally the ICMP checksum */
716 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
717 skb->ip_summed = CHECKSUM_UNNECESSARY;
720 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
721 "Forwarding altered outgoing ICMP");
723 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
724 "Forwarding altered incoming ICMP");
727 #ifdef CONFIG_IP_VS_IPV6
728 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
729 struct ip_vs_conn *cp, int inout)
731 struct ipv6hdr *iph = ipv6_hdr(skb);
732 unsigned int icmp_offset = 0;
733 unsigned int offs = 0; /* header offset*/
735 struct icmp6hdr *icmph;
736 struct ipv6hdr *ciph;
737 unsigned short fragoffs;
739 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
740 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
741 offs = icmp_offset + sizeof(struct icmp6hdr);
742 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
744 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
747 iph->saddr = cp->vaddr.in6;
748 ciph->daddr = cp->vaddr.in6;
750 iph->daddr = cp->daddr.in6;
751 ciph->saddr = cp->daddr.in6;
754 /* the TCP/UDP/SCTP port */
755 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
756 IPPROTO_SCTP == protocol)) {
757 __be16 *ports = (void *)(skb_network_header(skb) + offs);
759 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
760 ntohs(inout ? ports[1] : ports[0]),
761 ntohs(inout ? cp->vport : cp->dport));
763 ports[1] = cp->vport;
765 ports[0] = cp->dport;
768 /* And finally the ICMP checksum */
769 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
770 skb->len - icmp_offset,
772 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
773 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
774 skb->ip_summed = CHECKSUM_PARTIAL;
777 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
778 (void *)ciph - (void *)iph,
779 "Forwarding altered outgoing ICMPv6");
781 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
782 (void *)ciph - (void *)iph,
783 "Forwarding altered incoming ICMPv6");
787 /* Handle relevant response ICMP messages - forward to the right
790 static int handle_response_icmp(int af, struct sk_buff *skb,
791 union nf_inet_addr *snet,
792 __u8 protocol, struct ip_vs_conn *cp,
793 struct ip_vs_protocol *pp,
794 unsigned int offset, unsigned int ihl)
796 unsigned int verdict = NF_DROP;
798 if (IP_VS_FWD_METHOD(cp) != 0) {
799 pr_err("shouldn't reach here, because the box is on the "
800 "half connection in the tun/dr module.\n");
803 /* Ensure the checksum is correct */
804 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
805 /* Failed checksum! */
806 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
807 IP_VS_DBG_ADDR(af, snet));
811 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
812 IPPROTO_SCTP == protocol)
813 offset += 2 * sizeof(__u16);
814 if (!skb_make_writable(skb, offset))
817 #ifdef CONFIG_IP_VS_IPV6
819 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
822 ip_vs_nat_icmp(skb, pp, cp, 1);
824 if (ip_vs_route_me_harder(af, skb))
827 /* do the statistics and put it back */
828 ip_vs_out_stats(cp, skb);
830 skb->ipvs_property = 1;
831 if (!(cp->flags & IP_VS_CONN_F_NFCT))
834 ip_vs_update_conntrack(skb, cp, 0);
838 __ip_vs_conn_put(cp);
844 * Handle ICMP messages in the inside-to-outside direction (outgoing).
845 * Find any that might be relevant, check against existing connections.
846 * Currently handles error types - unreachable, quench, ttl exceeded.
848 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
849 unsigned int hooknum)
852 struct icmphdr _icmph, *ic;
853 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
854 struct ip_vs_iphdr ciph;
855 struct ip_vs_conn *cp;
856 struct ip_vs_protocol *pp;
857 unsigned int offset, ihl;
858 union nf_inet_addr snet;
862 /* reassemble IP fragments */
863 if (ip_is_fragment(ip_hdr(skb))) {
864 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
869 offset = ihl = iph->ihl * 4;
870 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
874 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
875 ic->type, ntohs(icmp_id(ic)),
876 &iph->saddr, &iph->daddr);
879 * Work through seeing if this is for us.
880 * These checks are supposed to be in an order that means easy
881 * things are checked first to speed up processing.... however
882 * this means that some packets will manage to get a long way
883 * down this stack and then be rejected, but that's life.
885 if ((ic->type != ICMP_DEST_UNREACH) &&
886 (ic->type != ICMP_SOURCE_QUENCH) &&
887 (ic->type != ICMP_TIME_EXCEEDED)) {
892 /* Now find the contained IP header */
893 offset += sizeof(_icmph);
894 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
896 return NF_ACCEPT; /* The packet looks wrong, ignore */
898 pp = ip_vs_proto_get(cih->protocol);
902 /* Is the embedded protocol header present? */
903 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
907 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
908 "Checking outgoing ICMP for");
910 ip_vs_fill_ip4hdr(cih, &ciph);
912 /* The embedded headers contain source and dest in reverse order */
913 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
917 snet.ip = iph->saddr;
918 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
922 #ifdef CONFIG_IP_VS_IPV6
923 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
924 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
926 struct icmp6hdr _icmph, *ic;
927 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
928 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
929 struct ip_vs_conn *cp;
930 struct ip_vs_protocol *pp;
931 union nf_inet_addr snet;
932 unsigned int writable;
935 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
940 * Work through seeing if this is for us.
941 * These checks are supposed to be in an order that means easy
942 * things are checked first to speed up processing.... however
943 * this means that some packets will manage to get a long way
944 * down this stack and then be rejected, but that's life.
946 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
950 /* Fragment header that is before ICMP header tells us that:
951 * it's not an error message since they can't be fragmented.
953 if (ipvsh->flags & IP6_FH_F_FRAG)
956 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
957 ic->icmp6_type, ntohs(icmpv6_id(ic)),
958 &ipvsh->saddr, &ipvsh->daddr);
960 /* Now find the contained IP header */
961 ciph.len = ipvsh->len + sizeof(_icmph);
962 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
964 return NF_ACCEPT; /* The packet looks wrong, ignore */
965 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
966 ciph.daddr.in6 = ip6h->daddr;
967 /* skip possible IPv6 exthdrs of contained IPv6 packet */
968 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
969 if (ciph.protocol < 0)
970 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
972 pp = ip_vs_proto_get(ciph.protocol);
976 /* The embedded headers contain source and dest in reverse order */
977 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
981 snet.in6 = ciph.saddr.in6;
983 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
984 pp, writable, sizeof(struct ipv6hdr));
989 * Check if sctp chunc is ABORT chunk
991 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
993 sctp_chunkhdr_t *sch, schunk;
994 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
995 sizeof(schunk), &schunk);
998 if (sch->type == SCTP_CID_ABORT)
1003 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1005 struct tcphdr _tcph, *th;
1007 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1013 static inline bool is_new_conn(const struct sk_buff *skb,
1014 struct ip_vs_iphdr *iph)
1016 switch (iph->protocol) {
1018 struct tcphdr _tcph, *th;
1020 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1025 case IPPROTO_SCTP: {
1026 sctp_chunkhdr_t *sch, schunk;
1028 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1029 sizeof(schunk), &schunk);
1032 return sch->type == SCTP_CID_INIT;
1039 /* Handle response packets: rewrite addresses and send away...
1042 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1043 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
1045 struct ip_vs_protocol *pp = pd->pp;
1047 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1049 if (!skb_make_writable(skb, iph->len))
1052 /* mangle the packet */
1053 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1056 #ifdef CONFIG_IP_VS_IPV6
1058 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1062 ip_hdr(skb)->saddr = cp->vaddr.ip;
1063 ip_send_check(ip_hdr(skb));
1067 * nf_iterate does not expect change in the skb->dst->dev.
1068 * It looks like it is not fatal to enable this code for hooks
1069 * where our handlers are at the end of the chain list and
1070 * when all next handlers use skb->dst->dev and not outdev.
1071 * It will definitely route properly the inout NAT traffic
1072 * when multiple paths are used.
1075 /* For policy routing, packets originating from this
1076 * machine itself may be routed differently to packets
1077 * passing through. We want this packet to be routed as
1078 * if it came from this machine itself. So re-compute
1079 * the routing information.
1081 if (ip_vs_route_me_harder(af, skb))
1084 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1086 ip_vs_out_stats(cp, skb);
1087 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1088 skb->ipvs_property = 1;
1089 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1092 ip_vs_update_conntrack(skb, cp, 0);
1106 * Check if outgoing packet belongs to the established ip_vs_conn.
1109 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1111 struct net *net = NULL;
1112 struct ip_vs_iphdr iph;
1113 struct ip_vs_protocol *pp;
1114 struct ip_vs_proto_data *pd;
1115 struct ip_vs_conn *cp;
1119 /* Already marked as IPVS request or reply? */
1120 if (skb->ipvs_property)
1123 /* Bad... Do not break raw sockets */
1124 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1126 struct sock *sk = skb->sk;
1127 struct inet_sock *inet = inet_sk(skb->sk);
1129 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1133 if (unlikely(!skb_dst(skb)))
1137 if (!net_ipvs(net)->enable)
1140 ip_vs_fill_iph_skb(af, skb, &iph);
1141 #ifdef CONFIG_IP_VS_IPV6
1142 if (af == AF_INET6) {
1143 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1145 int verdict = ip_vs_out_icmp_v6(skb, &related,
1153 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1155 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1161 pd = ip_vs_proto_data_get(net, iph.protocol);
1166 /* reassemble IP fragments */
1167 #ifdef CONFIG_IP_VS_IPV6
1170 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1171 if (ip_vs_gather_frags(skb,
1172 ip_vs_defrag_user(hooknum)))
1175 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
1179 * Check if the packet belongs to an existing entry
1181 cp = pp->conn_out_get(af, skb, &iph, 0);
1184 return handle_response(af, skb, pd, cp, &iph);
1185 if (sysctl_nat_icmp_send(net) &&
1186 (pp->protocol == IPPROTO_TCP ||
1187 pp->protocol == IPPROTO_UDP ||
1188 pp->protocol == IPPROTO_SCTP)) {
1189 __be16 _ports[2], *pptr;
1191 pptr = frag_safe_skb_hp(skb, iph.len,
1192 sizeof(_ports), _ports, &iph);
1194 return NF_ACCEPT; /* Not for me */
1195 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1198 * Notify the real server: there is no
1199 * existing entry if it is not RST
1200 * packet or not TCP packet.
1202 if ((iph.protocol != IPPROTO_TCP &&
1203 iph.protocol != IPPROTO_SCTP)
1204 || ((iph.protocol == IPPROTO_TCP
1205 && !is_tcp_reset(skb, iph.len))
1206 || (iph.protocol == IPPROTO_SCTP
1207 && !is_sctp_abort(skb,
1209 #ifdef CONFIG_IP_VS_IPV6
1210 if (af == AF_INET6) {
1212 skb->dev = net->loopback_dev;
1214 ICMPV6_DEST_UNREACH,
1215 ICMPV6_PORT_UNREACH,
1221 ICMP_PORT_UNREACH, 0);
1226 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1227 "ip_vs_out: packet continues traversal as normal");
1232 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1233 * used only for VS/NAT.
1234 * Check if packet is reply for established ip_vs_conn.
1237 ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1238 const struct net_device *in, const struct net_device *out,
1239 int (*okfn)(struct sk_buff *))
1241 return ip_vs_out(ops->hooknum, skb, AF_INET);
1245 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1246 * Check if packet is reply for established ip_vs_conn.
1249 ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1250 const struct net_device *in, const struct net_device *out,
1251 int (*okfn)(struct sk_buff *))
1253 return ip_vs_out(ops->hooknum, skb, AF_INET);
1256 #ifdef CONFIG_IP_VS_IPV6
1259 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1260 * used only for VS/NAT.
1261 * Check if packet is reply for established ip_vs_conn.
1264 ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1265 const struct net_device *in, const struct net_device *out,
1266 int (*okfn)(struct sk_buff *))
1268 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1272 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1273 * Check if packet is reply for established ip_vs_conn.
1276 ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1277 const struct net_device *in, const struct net_device *out,
1278 int (*okfn)(struct sk_buff *))
1280 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1286 * Handle ICMP messages in the outside-to-inside direction (incoming).
1287 * Find any that might be relevant, check against existing connections,
1288 * forward to the right destination host if relevant.
1289 * Currently handles error types - unreachable, quench, ttl exceeded.
1292 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1294 struct net *net = NULL;
1296 struct icmphdr _icmph, *ic;
1297 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1298 struct ip_vs_iphdr ciph;
1299 struct ip_vs_conn *cp;
1300 struct ip_vs_protocol *pp;
1301 struct ip_vs_proto_data *pd;
1302 unsigned int offset, offset2, ihl, verdict;
1307 /* reassemble IP fragments */
1308 if (ip_is_fragment(ip_hdr(skb))) {
1309 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1314 offset = ihl = iph->ihl * 4;
1315 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1319 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1320 ic->type, ntohs(icmp_id(ic)),
1321 &iph->saddr, &iph->daddr);
1324 * Work through seeing if this is for us.
1325 * These checks are supposed to be in an order that means easy
1326 * things are checked first to speed up processing.... however
1327 * this means that some packets will manage to get a long way
1328 * down this stack and then be rejected, but that's life.
1330 if ((ic->type != ICMP_DEST_UNREACH) &&
1331 (ic->type != ICMP_SOURCE_QUENCH) &&
1332 (ic->type != ICMP_TIME_EXCEEDED)) {
1337 /* Now find the contained IP header */
1338 offset += sizeof(_icmph);
1339 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1341 return NF_ACCEPT; /* The packet looks wrong, ignore */
1345 /* Special case for errors for IPIP packets */
1347 if (cih->protocol == IPPROTO_IPIP) {
1348 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1350 /* Error for our IPIP must arrive at LOCAL_IN */
1351 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1353 offset += cih->ihl * 4;
1354 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1356 return NF_ACCEPT; /* The packet looks wrong, ignore */
1360 pd = ip_vs_proto_data_get(net, cih->protocol);
1365 /* Is the embedded protocol header present? */
1366 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1370 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1371 "Checking incoming ICMP for");
1374 ip_vs_fill_ip4hdr(cih, &ciph);
1377 /* The embedded headers contain source and dest in reverse order.
1378 * For IPIP this is error for request, not for reply.
1380 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
1386 /* Ensure the checksum is correct */
1387 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1388 /* Failed checksum! */
1389 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1395 __be32 info = ic->un.gateway;
1396 __u8 type = ic->type;
1397 __u8 code = ic->code;
1399 /* Update the MTU */
1400 if (ic->type == ICMP_DEST_UNREACH &&
1401 ic->code == ICMP_FRAG_NEEDED) {
1402 struct ip_vs_dest *dest = cp->dest;
1403 u32 mtu = ntohs(ic->un.frag.mtu);
1404 __be16 frag_off = cih->frag_off;
1406 /* Strip outer IP and ICMP, go to IPIP header */
1407 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1409 offset2 -= ihl + sizeof(_icmph);
1410 skb_reset_network_header(skb);
1411 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1412 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1413 ipv4_update_pmtu(skb, dev_net(skb->dev),
1415 /* Client uses PMTUD? */
1416 if (!(frag_off & htons(IP_DF)))
1418 /* Prefer the resulting PMTU */
1420 struct ip_vs_dest_dst *dest_dst;
1423 dest_dst = rcu_dereference(dest->dest_dst);
1425 mtu = dst_mtu(dest_dst->dst_cache);
1428 if (mtu > 68 + sizeof(struct iphdr))
1429 mtu -= sizeof(struct iphdr);
1432 /* Strip outer IP, ICMP and IPIP, go to IP header of
1435 if (pskb_pull(skb, offset2) == NULL)
1437 skb_reset_network_header(skb);
1438 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1439 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1440 type, code, ntohl(info));
1441 icmp_send(skb, type, code, info);
1442 /* ICMP can be shorter but anyways, account it */
1443 ip_vs_out_stats(cp, skb);
1447 verdict = NF_STOLEN;
1451 /* do the statistics and put it back */
1452 ip_vs_in_stats(cp, skb);
1453 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1454 IPPROTO_SCTP == cih->protocol)
1455 offset += 2 * sizeof(__u16);
1456 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1459 __ip_vs_conn_put(cp);
1464 #ifdef CONFIG_IP_VS_IPV6
1465 static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1466 unsigned int hooknum, struct ip_vs_iphdr *iph)
1468 struct net *net = NULL;
1469 struct ipv6hdr _ip6h, *ip6h;
1470 struct icmp6hdr _icmph, *ic;
1471 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1472 struct ip_vs_conn *cp;
1473 struct ip_vs_protocol *pp;
1474 struct ip_vs_proto_data *pd;
1475 unsigned int offs_ciph, writable, verdict;
1479 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1484 * Work through seeing if this is for us.
1485 * These checks are supposed to be in an order that means easy
1486 * things are checked first to speed up processing.... however
1487 * this means that some packets will manage to get a long way
1488 * down this stack and then be rejected, but that's life.
1490 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1494 /* Fragment header that is before ICMP header tells us that:
1495 * it's not an error message since they can't be fragmented.
1497 if (iph->flags & IP6_FH_F_FRAG)
1500 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1501 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1502 &iph->saddr, &iph->daddr);
1504 /* Now find the contained IP header */
1505 ciph.len = iph->len + sizeof(_icmph);
1506 offs_ciph = ciph.len; /* Save ip header offset */
1507 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1509 return NF_ACCEPT; /* The packet looks wrong, ignore */
1510 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1511 ciph.daddr.in6 = ip6h->daddr;
1512 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1513 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1514 if (ciph.protocol < 0)
1515 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
1518 pd = ip_vs_proto_data_get(net, ciph.protocol);
1523 /* Cannot handle fragmented embedded protocol */
1527 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
1528 "Checking incoming ICMPv6 for");
1530 /* The embedded headers contain source and dest in reverse order
1531 * if not from localhost
1533 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
1534 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1538 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1539 if ((hooknum == NF_INET_LOCAL_OUT) &&
1540 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1541 __ip_vs_conn_put(cp);
1545 /* do the statistics and put it back */
1546 ip_vs_in_stats(cp, skb);
1548 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1549 writable = ciph.len;
1550 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1551 IPPROTO_SCTP == ciph.protocol)
1552 writable += 2 * sizeof(__u16); /* Also mangle ports */
1554 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
1556 __ip_vs_conn_put(cp);
1564 * Check if it's for virtual services, look it up,
1565 * and send it on its way...
1568 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1571 struct ip_vs_iphdr iph;
1572 struct ip_vs_protocol *pp;
1573 struct ip_vs_proto_data *pd;
1574 struct ip_vs_conn *cp;
1576 struct netns_ipvs *ipvs;
1578 /* Already marked as IPVS request or reply? */
1579 if (skb->ipvs_property)
1584 * - remote client: only PACKET_HOST
1585 * - route: used for struct net when skb->dev is unset
1587 if (unlikely((skb->pkt_type != PACKET_HOST &&
1588 hooknum != NF_INET_LOCAL_OUT) ||
1590 ip_vs_fill_iph_skb(af, skb, &iph);
1591 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1592 " ignored in hook %u\n",
1593 skb->pkt_type, iph.protocol,
1594 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1597 /* ipvs enabled in this netns ? */
1599 ipvs = net_ipvs(net);
1600 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1603 ip_vs_fill_iph_skb(af, skb, &iph);
1605 /* Bad... Do not break raw sockets */
1606 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1608 struct sock *sk = skb->sk;
1609 struct inet_sock *inet = inet_sk(skb->sk);
1611 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1615 #ifdef CONFIG_IP_VS_IPV6
1616 if (af == AF_INET6) {
1617 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1619 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1627 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1629 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1635 /* Protocol supported? */
1636 pd = ip_vs_proto_data_get(net, iph.protocol);
1641 * Check if the packet belongs to an existing connection entry
1643 cp = pp->conn_in_get(af, skb, &iph, 0);
1645 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1646 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1647 is_new_conn(skb, &iph)) {
1648 ip_vs_conn_expire_now(cp);
1649 __ip_vs_conn_put(cp);
1653 if (unlikely(!cp) && !iph.fragoffs) {
1654 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1655 * replayed fragment zero will already have created the cp
1659 /* Schedule and create new connection entry into &cp */
1660 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
1664 if (unlikely(!cp)) {
1665 /* sorry, all this trouble for a no-hit :) */
1666 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1667 "ip_vs_in: packet continues traversal as normal");
1669 /* Fragment that couldn't be mapped to a conn entry
1670 * is missing module nf_defrag_ipv6
1672 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1673 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1678 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1679 /* Check the server status */
1680 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1681 /* the destination server is not available */
1683 if (sysctl_expire_nodest_conn(ipvs)) {
1684 /* try to expire the connection immediately */
1685 ip_vs_conn_expire_now(cp);
1687 /* don't restart its timer, and silently
1689 __ip_vs_conn_put(cp);
1693 ip_vs_in_stats(cp, skb);
1694 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1695 if (cp->packet_xmit)
1696 ret = cp->packet_xmit(skb, cp, pp, &iph);
1697 /* do not touch skb anymore */
1699 IP_VS_DBG_RL("warning: packet_xmit is null");
1703 /* Increase its packet counter and check if it is needed
1704 * to be synchronized
1706 * Sync connection if it is about to close to
1707 * encorage the standby servers to update the connections timeout
1709 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1712 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1713 pkts = sysctl_sync_threshold(ipvs);
1715 pkts = atomic_add_return(1, &cp->in_pkts);
1717 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1718 ip_vs_sync_conn(net, cp, pkts);
1725 * AF_INET handler in NF_INET_LOCAL_IN chain
1726 * Schedule and forward packets from remote clients
1729 ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1730 const struct net_device *in,
1731 const struct net_device *out,
1732 int (*okfn)(struct sk_buff *))
1734 return ip_vs_in(ops->hooknum, skb, AF_INET);
1738 * AF_INET handler in NF_INET_LOCAL_OUT chain
1739 * Schedule and forward packets from local clients
1742 ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1743 const struct net_device *in, const struct net_device *out,
1744 int (*okfn)(struct sk_buff *))
1746 return ip_vs_in(ops->hooknum, skb, AF_INET);
1749 #ifdef CONFIG_IP_VS_IPV6
1752 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1753 * Schedule and forward packets from remote clients
1756 ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1757 const struct net_device *in,
1758 const struct net_device *out,
1759 int (*okfn)(struct sk_buff *))
1761 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1765 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1766 * Schedule and forward packets from local clients
1769 ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1770 const struct net_device *in, const struct net_device *out,
1771 int (*okfn)(struct sk_buff *))
1773 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1780 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1781 * related packets destined for 0.0.0.0/0.
1782 * When fwmark-based virtual service is used, such as transparent
1783 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1784 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1785 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1786 * and send them to ip_vs_in_icmp.
1789 ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1790 const struct net_device *in, const struct net_device *out,
1791 int (*okfn)(struct sk_buff *))
1795 struct netns_ipvs *ipvs;
1797 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1800 /* ipvs enabled in this netns ? */
1802 ipvs = net_ipvs(net);
1803 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1806 return ip_vs_in_icmp(skb, &r, ops->hooknum);
1809 #ifdef CONFIG_IP_VS_IPV6
1811 ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1812 const struct net_device *in, const struct net_device *out,
1813 int (*okfn)(struct sk_buff *))
1817 struct netns_ipvs *ipvs;
1818 struct ip_vs_iphdr iphdr;
1820 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1821 if (iphdr.protocol != IPPROTO_ICMPV6)
1824 /* ipvs enabled in this netns ? */
1826 ipvs = net_ipvs(net);
1827 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1830 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
1835 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1836 /* After packet filtering, change source only for VS/NAT */
1838 .hook = ip_vs_reply4,
1839 .owner = THIS_MODULE,
1841 .hooknum = NF_INET_LOCAL_IN,
1842 .priority = NF_IP_PRI_NAT_SRC - 2,
1844 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1845 * or VS/NAT(change destination), so that filtering rules can be
1846 * applied to IPVS. */
1848 .hook = ip_vs_remote_request4,
1849 .owner = THIS_MODULE,
1851 .hooknum = NF_INET_LOCAL_IN,
1852 .priority = NF_IP_PRI_NAT_SRC - 1,
1854 /* Before ip_vs_in, change source only for VS/NAT */
1856 .hook = ip_vs_local_reply4,
1857 .owner = THIS_MODULE,
1859 .hooknum = NF_INET_LOCAL_OUT,
1860 .priority = NF_IP_PRI_NAT_DST + 1,
1862 /* After mangle, schedule and forward local requests */
1864 .hook = ip_vs_local_request4,
1865 .owner = THIS_MODULE,
1867 .hooknum = NF_INET_LOCAL_OUT,
1868 .priority = NF_IP_PRI_NAT_DST + 2,
1870 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1871 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1873 .hook = ip_vs_forward_icmp,
1874 .owner = THIS_MODULE,
1876 .hooknum = NF_INET_FORWARD,
1879 /* After packet filtering, change source only for VS/NAT */
1881 .hook = ip_vs_reply4,
1882 .owner = THIS_MODULE,
1884 .hooknum = NF_INET_FORWARD,
1887 #ifdef CONFIG_IP_VS_IPV6
1888 /* After packet filtering, change source only for VS/NAT */
1890 .hook = ip_vs_reply6,
1891 .owner = THIS_MODULE,
1893 .hooknum = NF_INET_LOCAL_IN,
1894 .priority = NF_IP6_PRI_NAT_SRC - 2,
1896 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1897 * or VS/NAT(change destination), so that filtering rules can be
1898 * applied to IPVS. */
1900 .hook = ip_vs_remote_request6,
1901 .owner = THIS_MODULE,
1903 .hooknum = NF_INET_LOCAL_IN,
1904 .priority = NF_IP6_PRI_NAT_SRC - 1,
1906 /* Before ip_vs_in, change source only for VS/NAT */
1908 .hook = ip_vs_local_reply6,
1909 .owner = THIS_MODULE,
1911 .hooknum = NF_INET_LOCAL_OUT,
1912 .priority = NF_IP6_PRI_NAT_DST + 1,
1914 /* After mangle, schedule and forward local requests */
1916 .hook = ip_vs_local_request6,
1917 .owner = THIS_MODULE,
1919 .hooknum = NF_INET_LOCAL_OUT,
1920 .priority = NF_IP6_PRI_NAT_DST + 2,
1922 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1923 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1925 .hook = ip_vs_forward_icmp_v6,
1926 .owner = THIS_MODULE,
1928 .hooknum = NF_INET_FORWARD,
1931 /* After packet filtering, change source only for VS/NAT */
1933 .hook = ip_vs_reply6,
1934 .owner = THIS_MODULE,
1936 .hooknum = NF_INET_FORWARD,
1942 * Initialize IP Virtual Server netns mem.
1944 static int __net_init __ip_vs_init(struct net *net)
1946 struct netns_ipvs *ipvs;
1948 ipvs = net_generic(net, ip_vs_net_id);
1952 /* Hold the beast until a service is registerd */
1955 /* Counters used for creating unique names */
1956 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1957 atomic_inc(&ipvs_netns_cnt);
1960 if (ip_vs_estimator_net_init(net) < 0)
1961 goto estimator_fail;
1963 if (ip_vs_control_net_init(net) < 0)
1966 if (ip_vs_protocol_net_init(net) < 0)
1969 if (ip_vs_app_net_init(net) < 0)
1972 if (ip_vs_conn_net_init(net) < 0)
1975 if (ip_vs_sync_net_init(net) < 0)
1978 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
1979 sizeof(struct netns_ipvs), ipvs->gen);
1986 ip_vs_conn_net_cleanup(net);
1988 ip_vs_app_net_cleanup(net);
1990 ip_vs_protocol_net_cleanup(net);
1992 ip_vs_control_net_cleanup(net);
1994 ip_vs_estimator_net_cleanup(net);
2000 static void __net_exit __ip_vs_cleanup(struct net *net)
2002 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2003 ip_vs_conn_net_cleanup(net);
2004 ip_vs_app_net_cleanup(net);
2005 ip_vs_protocol_net_cleanup(net);
2006 ip_vs_control_net_cleanup(net);
2007 ip_vs_estimator_net_cleanup(net);
2008 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
2012 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2015 net_ipvs(net)->enable = 0; /* Disable packet reception */
2017 ip_vs_sync_net_cleanup(net);
2021 static struct pernet_operations ipvs_core_ops = {
2022 .init = __ip_vs_init,
2023 .exit = __ip_vs_cleanup,
2024 .id = &ip_vs_net_id,
2025 .size = sizeof(struct netns_ipvs),
2028 static struct pernet_operations ipvs_core_dev_ops = {
2029 .exit = __ip_vs_dev_cleanup,
2033 * Initialize IP Virtual Server
2035 static int __init ip_vs_init(void)
2039 ret = ip_vs_control_init();
2041 pr_err("can't setup control.\n");
2045 ip_vs_protocol_init();
2047 ret = ip_vs_conn_init();
2049 pr_err("can't setup connection table.\n");
2050 goto cleanup_protocol;
2053 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2057 ret = register_pernet_device(&ipvs_core_dev_ops);
2061 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2063 pr_err("can't register hooks.\n");
2067 ret = ip_vs_register_nl_ioctl();
2069 pr_err("can't register netlink/ioctl.\n");
2073 pr_info("ipvs loaded.\n");
2078 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2080 unregister_pernet_device(&ipvs_core_dev_ops);
2082 unregister_pernet_subsys(&ipvs_core_ops);
2084 ip_vs_conn_cleanup();
2086 ip_vs_protocol_cleanup();
2087 ip_vs_control_cleanup();
2092 static void __exit ip_vs_cleanup(void)
2094 ip_vs_unregister_nl_ioctl();
2095 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2096 unregister_pernet_device(&ipvs_core_dev_ops);
2097 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2098 ip_vs_conn_cleanup();
2099 ip_vs_protocol_cleanup();
2100 ip_vs_control_cleanup();
2101 pr_info("ipvs unloaded.\n");
2104 module_init(ip_vs_init);
2105 module_exit(ip_vs_cleanup);
2106 MODULE_LICENSE("GPL");