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 int ip_vs_net_id __read_mostly;
73 #ifdef IP_VS_GENERIC_NETNS
74 EXPORT_SYMBOL(ip_vs_net_id);
76 /* netns cnt used for uniqueness */
77 static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
79 /* ID used in ICMP lookups */
80 #define icmp_id(icmph) (((icmph)->un).echo.id)
81 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
83 const char *ip_vs_proto_name(unsigned proto)
98 #ifdef CONFIG_IP_VS_IPV6
103 sprintf(buf, "IP_%d", proto);
108 void ip_vs_init_hash_table(struct list_head *table, int rows)
111 INIT_LIST_HEAD(&table[rows]);
115 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
117 struct ip_vs_dest *dest = cp->dest;
118 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
120 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
121 struct ip_vs_cpu_stats *s;
123 s = this_cpu_ptr(dest->stats.cpustats);
125 u64_stats_update_begin(&s->syncp);
126 s->ustats.inbytes += skb->len;
127 u64_stats_update_end(&s->syncp);
129 s = this_cpu_ptr(dest->svc->stats.cpustats);
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
135 s = this_cpu_ptr(ipvs->cpustats);
137 u64_stats_update_begin(&s->syncp);
138 s->ustats.inbytes += skb->len;
139 u64_stats_update_end(&s->syncp);
145 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147 struct ip_vs_dest *dest = cp->dest;
148 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
150 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
151 struct ip_vs_cpu_stats *s;
153 s = this_cpu_ptr(dest->stats.cpustats);
155 u64_stats_update_begin(&s->syncp);
156 s->ustats.outbytes += skb->len;
157 u64_stats_update_end(&s->syncp);
159 s = this_cpu_ptr(dest->svc->stats.cpustats);
161 u64_stats_update_begin(&s->syncp);
162 s->ustats.outbytes += skb->len;
163 u64_stats_update_end(&s->syncp);
165 s = this_cpu_ptr(ipvs->cpustats);
167 u64_stats_update_begin(&s->syncp);
168 s->ustats.outbytes += skb->len;
169 u64_stats_update_end(&s->syncp);
175 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
177 struct netns_ipvs *ipvs = net_ipvs(svc->net);
178 struct ip_vs_cpu_stats *s;
180 s = this_cpu_ptr(cp->dest->stats.cpustats);
183 s = this_cpu_ptr(svc->stats.cpustats);
186 s = this_cpu_ptr(ipvs->cpustats);
192 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
193 const struct sk_buff *skb,
194 struct ip_vs_proto_data *pd)
196 if (unlikely(!pd->pp->state_transition))
198 return pd->pp->state_transition(cp, direction, skb, pd);
202 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
203 struct sk_buff *skb, int protocol,
204 const union nf_inet_addr *caddr, __be16 cport,
205 const union nf_inet_addr *vaddr, __be16 vport,
206 struct ip_vs_conn_param *p)
208 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
211 if (p->pe && p->pe->fill_param)
212 return p->pe->fill_param(p, skb);
218 * IPVS persistent scheduling function
219 * It creates a connection entry according to its template if exists,
220 * or selects a server and creates a connection entry plus a template.
221 * Locking: we are svc user (svc->refcnt), so we hold all dests too
222 * Protocols supported: TCP, UDP
224 static struct ip_vs_conn *
225 ip_vs_sched_persist(struct ip_vs_service *svc,
227 __be16 src_port, __be16 dst_port, int *ignored)
229 struct ip_vs_conn *cp = NULL;
230 struct ip_vs_iphdr iph;
231 struct ip_vs_dest *dest;
232 struct ip_vs_conn *ct;
233 __be16 dport = 0; /* destination port to forward */
235 struct ip_vs_conn_param param;
236 union nf_inet_addr snet; /* source network of the client,
239 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
241 /* Mask saddr with the netmask to adjust template granularity */
242 #ifdef CONFIG_IP_VS_IPV6
243 if (svc->af == AF_INET6)
244 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
247 snet.ip = iph.saddr.ip & svc->netmask;
249 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
251 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port),
252 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port),
253 IP_VS_DBG_ADDR(svc->af, &snet));
256 * As far as we know, FTP is a very complicated network protocol, and
257 * it uses control connection and data connections. For active FTP,
258 * FTP server initialize data connection to the client, its source port
259 * is often 20. For passive FTP, FTP server tells the clients the port
260 * that it passively listens to, and the client issues the data
261 * connection. In the tunneling or direct routing mode, the load
262 * balancer is on the client-to-server half of connection, the port
263 * number is unknown to the load balancer. So, a conn template like
264 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
265 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
266 * is created for other persistent services.
269 int protocol = iph.protocol;
270 const union nf_inet_addr *vaddr = &iph.daddr;
271 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
274 if (dst_port == svc->port) {
276 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
278 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
280 if (svc->port != FTPPORT)
283 /* Note: persistent fwmark-based services and
284 * persistent port zero service are handled here.
286 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
287 * port zero template:
288 * <protocol,caddr,0,vaddr,0,daddr,0>
291 protocol = IPPROTO_IP;
295 /* return *ignored = -1 so NF_DROP can be used */
296 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
297 vaddr, vport, ¶m) < 0) {
303 /* Check if a template already exists */
304 ct = ip_vs_ct_in_get(¶m);
305 if (!ct || !ip_vs_check_template(ct)) {
307 * No template found or the dest of the connection
308 * template is not available.
309 * return *ignored=0 i.e. ICMP and NF_DROP
311 dest = svc->scheduler->schedule(svc, skb);
313 IP_VS_DBG(1, "p-schedule: no dest found.\n");
314 kfree(param.pe_data);
319 if (dst_port == svc->port && svc->port != FTPPORT)
323 * This adds param.pe_data to the template,
324 * and thus param.pe_data will be destroyed
325 * when the template expires */
326 ct = ip_vs_conn_new(¶m, &dest->addr, dport,
327 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
329 kfree(param.pe_data);
334 ct->timeout = svc->timeout;
336 /* set destination with the found template */
338 kfree(param.pe_data);
342 if (dport == svc->port && dest->port)
345 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
346 && iph.protocol == IPPROTO_UDP)?
347 IP_VS_CONN_F_ONE_PACKET : 0;
350 * Create a new connection according to the template
352 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr,
353 src_port, &iph.daddr, dst_port, ¶m);
355 cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark);
365 ip_vs_control_add(cp, ct);
368 ip_vs_conn_stats(cp, svc);
374 * IPVS main scheduling function
375 * It selects a server according to the virtual service, and
376 * creates a connection entry.
377 * Protocols supported: TCP, UDP
381 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
382 * svc/scheduler decides that this packet should be accepted with
383 * NF_ACCEPT because it must not be scheduled.
385 * 0 : scheduler can not find destination, so try bypass or
386 * return ICMP and then NF_DROP (ip_vs_leave).
388 * -1 : scheduler tried to schedule but fatal error occurred, eg.
389 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
390 * failure such as missing Call-ID, ENOMEM on skb_linearize
391 * or pe_data. In this case we should return NF_DROP without
392 * any attempts to send ICMP with ip_vs_leave.
395 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
396 struct ip_vs_proto_data *pd, int *ignored)
398 struct ip_vs_protocol *pp = pd->pp;
399 struct ip_vs_conn *cp = NULL;
400 struct ip_vs_iphdr iph;
401 struct ip_vs_dest *dest;
402 __be16 _ports[2], *pptr;
406 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
407 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
412 * FTPDATA needs this check when using local real server.
413 * Never schedule Active FTPDATA connections from real server.
414 * For LVS-NAT they must be already created. For other methods
415 * with persistence the connection is created on SYN+ACK.
417 if (pptr[0] == FTPDATA) {
418 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
419 "Not scheduling FTPDATA");
424 * Do not schedule replies from local real server.
426 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
427 (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) {
428 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
429 "Not scheduling reply for existing connection");
430 __ip_vs_conn_put(cp);
437 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
438 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored);
443 * Non-persistent service
445 if (!svc->fwmark && pptr[1] != svc->port) {
447 pr_err("Schedule: port zero only supported "
448 "in persistent services, "
449 "check your ipvs configuration\n");
453 dest = svc->scheduler->schedule(svc, skb);
455 IP_VS_DBG(1, "Schedule: no dest found.\n");
459 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
460 && iph.protocol == IPPROTO_UDP)?
461 IP_VS_CONN_F_ONE_PACKET : 0;
464 * Create a connection entry.
467 struct ip_vs_conn_param p;
469 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
470 &iph.saddr, pptr[0], &iph.daddr, pptr[1],
472 cp = ip_vs_conn_new(&p, &dest->addr,
473 dest->port ? dest->port : pptr[1],
474 flags, dest, skb->mark);
481 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
482 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
484 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
485 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
486 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
487 cp->flags, atomic_read(&cp->refcnt));
489 ip_vs_conn_stats(cp, svc);
495 * Pass or drop the packet.
496 * Called by ip_vs_in, when the virtual service is available but
497 * no destination is available for a new connection.
499 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
500 struct ip_vs_proto_data *pd)
502 struct netns_ipvs *ipvs;
503 __be16 _ports[2], *pptr;
504 struct ip_vs_iphdr iph;
507 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
509 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
511 ip_vs_service_put(svc);
515 #ifdef CONFIG_IP_VS_IPV6
516 if (svc->af == AF_INET6)
517 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
520 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
522 /* if it is fwmark-based service, the cache_bypass sysctl is up
523 and the destination is a non-local unicast, then create
524 a cache_bypass connection entry */
525 ipvs = net_ipvs(skb_net(skb));
526 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
528 struct ip_vs_conn *cp;
529 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
530 iph.protocol == IPPROTO_UDP)?
531 IP_VS_CONN_F_ONE_PACKET : 0;
532 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
534 ip_vs_service_put(svc);
536 /* create a new connection entry */
537 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
539 struct ip_vs_conn_param p;
540 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
542 &iph.daddr, pptr[1], &p);
543 cp = ip_vs_conn_new(&p, &daddr, 0,
544 IP_VS_CONN_F_BYPASS | flags,
551 ip_vs_in_stats(cp, skb);
554 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
556 /* transmit the first SYN packet */
557 ret = cp->packet_xmit(skb, cp, pd->pp);
558 /* do not touch skb anymore */
560 atomic_inc(&cp->in_pkts);
566 * When the virtual ftp service is presented, packets destined
567 * for other services on the VIP may get here (except services
568 * listed in the ipvs table), pass the packets, because it is
569 * not ipvs job to decide to drop the packets.
571 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
572 ip_vs_service_put(svc);
576 ip_vs_service_put(svc);
579 * Notify the client that the destination is unreachable, and
580 * release the socket buffer.
581 * Since it is in IP layer, the TCP socket is not actually
582 * created, the TCP RST packet cannot be sent, instead that
583 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
585 #ifdef CONFIG_IP_VS_IPV6
586 if (svc->af == AF_INET6) {
588 struct net *net = dev_net(skb_dst(skb)->dev);
590 skb->dev = net->loopback_dev;
592 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
595 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
600 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
602 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
605 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
607 if (NF_INET_LOCAL_IN == hooknum)
608 return IP_DEFRAG_VS_IN;
609 if (NF_INET_FORWARD == hooknum)
610 return IP_DEFRAG_VS_FWD;
611 return IP_DEFRAG_VS_OUT;
614 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
616 int err = ip_defrag(skb, user);
619 ip_send_check(ip_hdr(skb));
624 #ifdef CONFIG_IP_VS_IPV6
625 static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
627 /* TODO IPv6: Find out what to do here for IPv6 */
633 * Packet has been made sufficiently writable in caller
634 * - inout: 1=in->out, 0=out->in
636 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
637 struct ip_vs_conn *cp, int inout)
639 struct iphdr *iph = ip_hdr(skb);
640 unsigned int icmp_offset = iph->ihl*4;
641 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
643 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
646 iph->saddr = cp->vaddr.ip;
648 ciph->daddr = cp->vaddr.ip;
651 iph->daddr = cp->daddr.ip;
653 ciph->saddr = cp->daddr.ip;
657 /* the TCP/UDP/SCTP port */
658 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
659 IPPROTO_SCTP == ciph->protocol) {
660 __be16 *ports = (void *)ciph + ciph->ihl*4;
663 ports[1] = cp->vport;
665 ports[0] = cp->dport;
668 /* And finally the ICMP checksum */
670 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
671 skb->ip_summed = CHECKSUM_UNNECESSARY;
674 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
675 "Forwarding altered outgoing ICMP");
677 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
678 "Forwarding altered incoming ICMP");
681 #ifdef CONFIG_IP_VS_IPV6
682 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
683 struct ip_vs_conn *cp, int inout)
685 struct ipv6hdr *iph = ipv6_hdr(skb);
686 unsigned int icmp_offset = sizeof(struct ipv6hdr);
687 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
689 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
692 iph->saddr = cp->vaddr.in6;
693 ciph->daddr = cp->vaddr.in6;
695 iph->daddr = cp->daddr.in6;
696 ciph->saddr = cp->daddr.in6;
699 /* the TCP/UDP/SCTP port */
700 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
701 IPPROTO_SCTP == ciph->nexthdr) {
702 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
705 ports[1] = cp->vport;
707 ports[0] = cp->dport;
710 /* And finally the ICMP checksum */
711 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
712 skb->len - icmp_offset,
714 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
715 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
716 skb->ip_summed = CHECKSUM_PARTIAL;
719 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
720 (void *)ciph - (void *)iph,
721 "Forwarding altered outgoing ICMPv6");
723 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
724 (void *)ciph - (void *)iph,
725 "Forwarding altered incoming ICMPv6");
729 /* Handle relevant response ICMP messages - forward to the right
730 * destination host. Used for NAT and local client.
732 static int handle_response_icmp(int af, struct sk_buff *skb,
733 union nf_inet_addr *snet,
734 __u8 protocol, struct ip_vs_conn *cp,
735 struct ip_vs_protocol *pp,
736 unsigned int offset, unsigned int ihl)
738 struct netns_ipvs *ipvs;
739 unsigned int verdict = NF_DROP;
741 if (IP_VS_FWD_METHOD(cp) != 0) {
742 pr_err("shouldn't reach here, because the box is on the "
743 "half connection in the tun/dr module.\n");
746 /* Ensure the checksum is correct */
747 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
748 /* Failed checksum! */
749 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
750 IP_VS_DBG_ADDR(af, snet));
754 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
755 IPPROTO_SCTP == protocol)
756 offset += 2 * sizeof(__u16);
757 if (!skb_make_writable(skb, offset))
760 ipvs = net_ipvs(skb_net(skb));
762 #ifdef CONFIG_IP_VS_IPV6
764 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
767 ip_vs_nat_icmp(skb, pp, cp, 1);
769 #ifdef CONFIG_IP_VS_IPV6
770 if (af == AF_INET6) {
771 if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0)
775 if ((ipvs->sysctl_snat_reroute ||
776 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
777 ip_route_me_harder(skb, RTN_LOCAL) != 0)
780 /* do the statistics and put it back */
781 ip_vs_out_stats(cp, skb);
783 skb->ipvs_property = 1;
784 if (!(cp->flags & IP_VS_CONN_F_NFCT))
787 ip_vs_update_conntrack(skb, cp, 0);
791 __ip_vs_conn_put(cp);
797 * Handle ICMP messages in the inside-to-outside direction (outgoing).
798 * Find any that might be relevant, check against existing connections.
799 * Currently handles error types - unreachable, quench, ttl exceeded.
801 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
802 unsigned int hooknum)
805 struct icmphdr _icmph, *ic;
806 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
807 struct ip_vs_iphdr ciph;
808 struct ip_vs_conn *cp;
809 struct ip_vs_protocol *pp;
810 unsigned int offset, ihl;
811 union nf_inet_addr snet;
815 /* reassemble IP fragments */
816 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
817 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
822 offset = ihl = iph->ihl * 4;
823 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
827 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
828 ic->type, ntohs(icmp_id(ic)),
829 &iph->saddr, &iph->daddr);
832 * Work through seeing if this is for us.
833 * These checks are supposed to be in an order that means easy
834 * things are checked first to speed up processing.... however
835 * this means that some packets will manage to get a long way
836 * down this stack and then be rejected, but that's life.
838 if ((ic->type != ICMP_DEST_UNREACH) &&
839 (ic->type != ICMP_SOURCE_QUENCH) &&
840 (ic->type != ICMP_TIME_EXCEEDED)) {
845 /* Now find the contained IP header */
846 offset += sizeof(_icmph);
847 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
849 return NF_ACCEPT; /* The packet looks wrong, ignore */
851 pp = ip_vs_proto_get(cih->protocol);
855 /* Is the embedded protocol header present? */
856 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
860 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
861 "Checking outgoing ICMP for");
863 offset += cih->ihl * 4;
865 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
866 /* The embedded headers contain source and dest in reverse order */
867 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
871 snet.ip = iph->saddr;
872 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
876 #ifdef CONFIG_IP_VS_IPV6
877 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
878 unsigned int hooknum)
881 struct icmp6hdr _icmph, *ic;
882 struct ipv6hdr _ciph, *cih; /* The ip header contained
884 struct ip_vs_iphdr ciph;
885 struct ip_vs_conn *cp;
886 struct ip_vs_protocol *pp;
888 union nf_inet_addr snet;
892 /* reassemble IP fragments */
893 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
894 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
899 offset = sizeof(struct ipv6hdr);
900 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
904 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
905 ic->icmp6_type, ntohs(icmpv6_id(ic)),
906 &iph->saddr, &iph->daddr);
909 * Work through seeing if this is for us.
910 * These checks are supposed to be in an order that means easy
911 * things are checked first to speed up processing.... however
912 * this means that some packets will manage to get a long way
913 * down this stack and then be rejected, but that's life.
915 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
916 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
917 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
922 /* Now find the contained IP header */
923 offset += sizeof(_icmph);
924 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
926 return NF_ACCEPT; /* The packet looks wrong, ignore */
928 pp = ip_vs_proto_get(cih->nexthdr);
932 /* Is the embedded protocol header present? */
933 /* TODO: we don't support fragmentation at the moment anyways */
934 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
937 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
938 "Checking outgoing ICMPv6 for");
940 offset += sizeof(struct ipv6hdr);
942 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
943 /* The embedded headers contain source and dest in reverse order */
944 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
948 ipv6_addr_copy(&snet.in6, &iph->saddr);
949 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
950 pp, offset, sizeof(struct ipv6hdr));
955 * Check if sctp chunc is ABORT chunk
957 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
959 sctp_chunkhdr_t *sch, schunk;
960 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
961 sizeof(schunk), &schunk);
964 if (sch->type == SCTP_CID_ABORT)
969 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
971 struct tcphdr _tcph, *th;
973 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
979 /* Handle response packets: rewrite addresses and send away...
980 * Used for NAT and local client.
983 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
984 struct ip_vs_conn *cp, int ihl)
986 struct ip_vs_protocol *pp = pd->pp;
987 struct netns_ipvs *ipvs;
989 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
991 if (!skb_make_writable(skb, ihl))
994 /* mangle the packet */
995 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
998 #ifdef CONFIG_IP_VS_IPV6
1000 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1004 ip_hdr(skb)->saddr = cp->vaddr.ip;
1005 ip_send_check(ip_hdr(skb));
1009 * nf_iterate does not expect change in the skb->dst->dev.
1010 * It looks like it is not fatal to enable this code for hooks
1011 * where our handlers are at the end of the chain list and
1012 * when all next handlers use skb->dst->dev and not outdev.
1013 * It will definitely route properly the inout NAT traffic
1014 * when multiple paths are used.
1017 /* For policy routing, packets originating from this
1018 * machine itself may be routed differently to packets
1019 * passing through. We want this packet to be routed as
1020 * if it came from this machine itself. So re-compute
1021 * the routing information.
1023 ipvs = net_ipvs(skb_net(skb));
1025 #ifdef CONFIG_IP_VS_IPV6
1026 if (af == AF_INET6) {
1027 if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0)
1031 if ((ipvs->sysctl_snat_reroute ||
1032 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
1033 ip_route_me_harder(skb, RTN_LOCAL) != 0)
1036 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1038 ip_vs_out_stats(cp, skb);
1039 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1040 skb->ipvs_property = 1;
1041 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1044 ip_vs_update_conntrack(skb, cp, 0);
1058 * Check if outgoing packet belongs to the established ip_vs_conn.
1061 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1063 struct net *net = NULL;
1064 struct ip_vs_iphdr iph;
1065 struct ip_vs_protocol *pp;
1066 struct ip_vs_proto_data *pd;
1067 struct ip_vs_conn *cp;
1068 struct netns_ipvs *ipvs;
1072 /* Already marked as IPVS request or reply? */
1073 if (skb->ipvs_property)
1076 /* Bad... Do not break raw sockets */
1077 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1079 struct sock *sk = skb->sk;
1080 struct inet_sock *inet = inet_sk(skb->sk);
1082 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1086 if (unlikely(!skb_dst(skb)))
1090 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1091 #ifdef CONFIG_IP_VS_IPV6
1092 if (af == AF_INET6) {
1093 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1095 int verdict = ip_vs_out_icmp_v6(skb, &related,
1100 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1104 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1106 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1110 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1113 pd = ip_vs_proto_data_get(net, iph.protocol);
1118 /* reassemble IP fragments */
1119 #ifdef CONFIG_IP_VS_IPV6
1120 if (af == AF_INET6) {
1121 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1122 if (ip_vs_gather_frags_v6(skb,
1123 ip_vs_defrag_user(hooknum)))
1127 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1130 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1131 !pp->dont_defrag)) {
1132 if (ip_vs_gather_frags(skb,
1133 ip_vs_defrag_user(hooknum)))
1136 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1140 * Check if the packet belongs to an existing entry
1142 cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
1143 ipvs = net_ipvs(net);
1146 return handle_response(af, skb, pd, cp, iph.len);
1147 if (ipvs->sysctl_nat_icmp_send &&
1148 (pp->protocol == IPPROTO_TCP ||
1149 pp->protocol == IPPROTO_UDP ||
1150 pp->protocol == IPPROTO_SCTP)) {
1151 __be16 _ports[2], *pptr;
1153 pptr = skb_header_pointer(skb, iph.len,
1154 sizeof(_ports), _ports);
1156 return NF_ACCEPT; /* Not for me */
1157 if (ip_vs_lookup_real_service(net, af, iph.protocol,
1161 * Notify the real server: there is no
1162 * existing entry if it is not RST
1163 * packet or not TCP packet.
1165 if ((iph.protocol != IPPROTO_TCP &&
1166 iph.protocol != IPPROTO_SCTP)
1167 || ((iph.protocol == IPPROTO_TCP
1168 && !is_tcp_reset(skb, iph.len))
1169 || (iph.protocol == IPPROTO_SCTP
1170 && !is_sctp_abort(skb,
1172 #ifdef CONFIG_IP_VS_IPV6
1173 if (af == AF_INET6) {
1175 dev_net(skb_dst(skb)->dev);
1178 skb->dev = net->loopback_dev;
1180 ICMPV6_DEST_UNREACH,
1181 ICMPV6_PORT_UNREACH,
1187 ICMP_PORT_UNREACH, 0);
1192 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1193 "ip_vs_out: packet continues traversal as normal");
1198 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1199 * used only for VS/NAT.
1200 * Check if packet is reply for established ip_vs_conn.
1203 ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1204 const struct net_device *in, const struct net_device *out,
1205 int (*okfn)(struct sk_buff *))
1207 return ip_vs_out(hooknum, skb, AF_INET);
1211 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1212 * Check if packet is reply for established ip_vs_conn.
1215 ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1216 const struct net_device *in, const struct net_device *out,
1217 int (*okfn)(struct sk_buff *))
1219 unsigned int verdict;
1221 /* Disable BH in LOCAL_OUT until all places are fixed */
1223 verdict = ip_vs_out(hooknum, skb, AF_INET);
1228 #ifdef CONFIG_IP_VS_IPV6
1231 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1232 * used only for VS/NAT.
1233 * Check if packet is reply for established ip_vs_conn.
1236 ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1237 const struct net_device *in, const struct net_device *out,
1238 int (*okfn)(struct sk_buff *))
1240 return ip_vs_out(hooknum, skb, AF_INET6);
1244 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1245 * Check if packet is reply for established ip_vs_conn.
1248 ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1249 const struct net_device *in, const struct net_device *out,
1250 int (*okfn)(struct sk_buff *))
1252 unsigned int verdict;
1254 /* Disable BH in LOCAL_OUT until all places are fixed */
1256 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1264 * Handle ICMP messages in the outside-to-inside direction (incoming).
1265 * Find any that might be relevant, check against existing connections,
1266 * forward to the right destination host if relevant.
1267 * Currently handles error types - unreachable, quench, ttl exceeded.
1270 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1272 struct net *net = NULL;
1274 struct icmphdr _icmph, *ic;
1275 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1276 struct ip_vs_iphdr ciph;
1277 struct ip_vs_conn *cp;
1278 struct ip_vs_protocol *pp;
1279 struct ip_vs_proto_data *pd;
1280 unsigned int offset, ihl, verdict;
1281 union nf_inet_addr snet;
1285 /* reassemble IP fragments */
1286 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1287 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1292 offset = ihl = iph->ihl * 4;
1293 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1297 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1298 ic->type, ntohs(icmp_id(ic)),
1299 &iph->saddr, &iph->daddr);
1302 * Work through seeing if this is for us.
1303 * These checks are supposed to be in an order that means easy
1304 * things are checked first to speed up processing.... however
1305 * this means that some packets will manage to get a long way
1306 * down this stack and then be rejected, but that's life.
1308 if ((ic->type != ICMP_DEST_UNREACH) &&
1309 (ic->type != ICMP_SOURCE_QUENCH) &&
1310 (ic->type != ICMP_TIME_EXCEEDED)) {
1315 /* Now find the contained IP header */
1316 offset += sizeof(_icmph);
1317 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1319 return NF_ACCEPT; /* The packet looks wrong, ignore */
1322 pd = ip_vs_proto_data_get(net, cih->protocol);
1327 /* Is the embedded protocol header present? */
1328 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1332 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1333 "Checking incoming ICMP for");
1335 offset += cih->ihl * 4;
1337 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1338 /* The embedded headers contain source and dest in reverse order */
1339 cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
1341 /* The packet could also belong to a local client */
1342 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
1344 snet.ip = iph->saddr;
1345 return handle_response_icmp(AF_INET, skb, &snet,
1346 cih->protocol, cp, pp,
1354 /* Ensure the checksum is correct */
1355 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1356 /* Failed checksum! */
1357 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1362 /* do the statistics and put it back */
1363 ip_vs_in_stats(cp, skb);
1364 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1365 offset += 2 * sizeof(__u16);
1366 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1367 /* LOCALNODE from FORWARD hook is not supported */
1368 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1369 skb_rtable(skb)->rt_flags & RTCF_LOCAL) {
1370 IP_VS_DBG(1, "%s(): "
1371 "local delivery to %pI4 but in FORWARD\n",
1372 __func__, &skb_rtable(skb)->rt_dst);
1377 __ip_vs_conn_put(cp);
1382 #ifdef CONFIG_IP_VS_IPV6
1384 ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1386 struct net *net = NULL;
1387 struct ipv6hdr *iph;
1388 struct icmp6hdr _icmph, *ic;
1389 struct ipv6hdr _ciph, *cih; /* The ip header contained
1391 struct ip_vs_iphdr ciph;
1392 struct ip_vs_conn *cp;
1393 struct ip_vs_protocol *pp;
1394 struct ip_vs_proto_data *pd;
1395 unsigned int offset, verdict;
1396 union nf_inet_addr snet;
1397 struct rt6_info *rt;
1401 /* reassemble IP fragments */
1402 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1403 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
1407 iph = ipv6_hdr(skb);
1408 offset = sizeof(struct ipv6hdr);
1409 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1413 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
1414 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1415 &iph->saddr, &iph->daddr);
1418 * Work through seeing if this is for us.
1419 * These checks are supposed to be in an order that means easy
1420 * things are checked first to speed up processing.... however
1421 * this means that some packets will manage to get a long way
1422 * down this stack and then be rejected, but that's life.
1424 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1425 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1426 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1431 /* Now find the contained IP header */
1432 offset += sizeof(_icmph);
1433 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1435 return NF_ACCEPT; /* The packet looks wrong, ignore */
1438 pd = ip_vs_proto_data_get(net, cih->nexthdr);
1443 /* Is the embedded protocol header present? */
1444 /* TODO: we don't support fragmentation at the moment anyways */
1445 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1448 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1449 "Checking incoming ICMPv6 for");
1451 offset += sizeof(struct ipv6hdr);
1453 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1454 /* The embedded headers contain source and dest in reverse order */
1455 cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
1457 /* The packet could also belong to a local client */
1458 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
1460 ipv6_addr_copy(&snet.in6, &iph->saddr);
1461 return handle_response_icmp(AF_INET6, skb, &snet,
1464 sizeof(struct ipv6hdr));
1471 /* do the statistics and put it back */
1472 ip_vs_in_stats(cp, skb);
1473 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1474 IPPROTO_SCTP == cih->nexthdr)
1475 offset += 2 * sizeof(__u16);
1476 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1477 /* LOCALNODE from FORWARD hook is not supported */
1478 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1479 (rt = (struct rt6_info *) skb_dst(skb)) &&
1480 rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) {
1481 IP_VS_DBG(1, "%s(): "
1482 "local delivery to %pI6 but in FORWARD\n",
1483 __func__, &rt->rt6i_dst);
1487 __ip_vs_conn_put(cp);
1495 * Check if it's for virtual services, look it up,
1496 * and send it on its way...
1499 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1502 struct ip_vs_iphdr iph;
1503 struct ip_vs_protocol *pp;
1504 struct ip_vs_proto_data *pd;
1505 struct ip_vs_conn *cp;
1506 int ret, restart, pkts;
1507 struct netns_ipvs *ipvs;
1509 /* Already marked as IPVS request or reply? */
1510 if (skb->ipvs_property)
1515 * - remote client: only PACKET_HOST
1516 * - route: used for struct net when skb->dev is unset
1518 if (unlikely((skb->pkt_type != PACKET_HOST &&
1519 hooknum != NF_INET_LOCAL_OUT) ||
1521 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1522 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1523 " ignored in hook %u\n",
1524 skb->pkt_type, iph.protocol,
1525 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1528 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1530 /* Bad... Do not break raw sockets */
1531 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1533 struct sock *sk = skb->sk;
1534 struct inet_sock *inet = inet_sk(skb->sk);
1536 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1540 #ifdef CONFIG_IP_VS_IPV6
1541 if (af == AF_INET6) {
1542 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1544 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
1548 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1552 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1554 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1558 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1562 /* Protocol supported? */
1563 pd = ip_vs_proto_data_get(net, iph.protocol);
1568 * Check if the packet belongs to an existing connection entry
1570 cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
1572 if (unlikely(!cp)) {
1575 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
1579 if (unlikely(!cp)) {
1580 /* sorry, all this trouble for a no-hit :) */
1581 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1582 "ip_vs_in: packet continues traversal as normal");
1586 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1588 ipvs = net_ipvs(net);
1589 /* Check the server status */
1590 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1591 /* the destination server is not available */
1593 if (ipvs->sysctl_expire_nodest_conn) {
1594 /* try to expire the connection immediately */
1595 ip_vs_conn_expire_now(cp);
1597 /* don't restart its timer, and silently
1599 __ip_vs_conn_put(cp);
1603 ip_vs_in_stats(cp, skb);
1604 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1605 if (cp->packet_xmit)
1606 ret = cp->packet_xmit(skb, cp, pp);
1607 /* do not touch skb anymore */
1609 IP_VS_DBG_RL("warning: packet_xmit is null");
1613 /* Increase its packet counter and check if it is needed
1614 * to be synchronized
1616 * Sync connection if it is about to close to
1617 * encorage the standby servers to update the connections timeout
1619 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1622 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1623 pkts = ipvs->sysctl_sync_threshold[0];
1625 pkts = atomic_add_return(1, &cp->in_pkts);
1627 if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
1628 cp->protocol == IPPROTO_SCTP) {
1629 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
1630 (pkts % ipvs->sysctl_sync_threshold[1]
1631 == ipvs->sysctl_sync_threshold[0])) ||
1632 (cp->old_state != cp->state &&
1633 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1634 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1635 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
1636 ip_vs_sync_conn(net, cp);
1641 /* Keep this block last: TCP and others with pp->num_states <= 1 */
1642 else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
1643 (((cp->protocol != IPPROTO_TCP ||
1644 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1645 (pkts % ipvs->sysctl_sync_threshold[1]
1646 == ipvs->sysctl_sync_threshold[0])) ||
1647 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1648 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
1649 (cp->state == IP_VS_TCP_S_CLOSE) ||
1650 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1651 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1652 ip_vs_sync_conn(net, cp);
1654 cp->old_state = cp->state;
1661 * AF_INET handler in NF_INET_LOCAL_IN chain
1662 * Schedule and forward packets from remote clients
1665 ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1666 const struct net_device *in,
1667 const struct net_device *out,
1668 int (*okfn)(struct sk_buff *))
1670 return ip_vs_in(hooknum, skb, AF_INET);
1674 * AF_INET handler in NF_INET_LOCAL_OUT chain
1675 * Schedule and forward packets from local clients
1678 ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1679 const struct net_device *in, const struct net_device *out,
1680 int (*okfn)(struct sk_buff *))
1682 unsigned int verdict;
1684 /* Disable BH in LOCAL_OUT until all places are fixed */
1686 verdict = ip_vs_in(hooknum, skb, AF_INET);
1691 #ifdef CONFIG_IP_VS_IPV6
1694 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1695 * Schedule and forward packets from remote clients
1698 ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1699 const struct net_device *in,
1700 const struct net_device *out,
1701 int (*okfn)(struct sk_buff *))
1703 return ip_vs_in(hooknum, skb, AF_INET6);
1707 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1708 * Schedule and forward packets from local clients
1711 ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1712 const struct net_device *in, const struct net_device *out,
1713 int (*okfn)(struct sk_buff *))
1715 unsigned int verdict;
1717 /* Disable BH in LOCAL_OUT until all places are fixed */
1719 verdict = ip_vs_in(hooknum, skb, AF_INET6);
1728 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1729 * related packets destined for 0.0.0.0/0.
1730 * When fwmark-based virtual service is used, such as transparent
1731 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1732 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1733 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1734 * and send them to ip_vs_in_icmp.
1737 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1738 const struct net_device *in, const struct net_device *out,
1739 int (*okfn)(struct sk_buff *))
1743 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1746 return ip_vs_in_icmp(skb, &r, hooknum);
1749 #ifdef CONFIG_IP_VS_IPV6
1751 ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1752 const struct net_device *in, const struct net_device *out,
1753 int (*okfn)(struct sk_buff *))
1757 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1760 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1765 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1766 /* After packet filtering, change source only for VS/NAT */
1768 .hook = ip_vs_reply4,
1769 .owner = THIS_MODULE,
1771 .hooknum = NF_INET_LOCAL_IN,
1774 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1775 * or VS/NAT(change destination), so that filtering rules can be
1776 * applied to IPVS. */
1778 .hook = ip_vs_remote_request4,
1779 .owner = THIS_MODULE,
1781 .hooknum = NF_INET_LOCAL_IN,
1784 /* Before ip_vs_in, change source only for VS/NAT */
1786 .hook = ip_vs_local_reply4,
1787 .owner = THIS_MODULE,
1789 .hooknum = NF_INET_LOCAL_OUT,
1792 /* After mangle, schedule and forward local requests */
1794 .hook = ip_vs_local_request4,
1795 .owner = THIS_MODULE,
1797 .hooknum = NF_INET_LOCAL_OUT,
1800 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1801 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1803 .hook = ip_vs_forward_icmp,
1804 .owner = THIS_MODULE,
1806 .hooknum = NF_INET_FORWARD,
1809 /* After packet filtering, change source only for VS/NAT */
1811 .hook = ip_vs_reply4,
1812 .owner = THIS_MODULE,
1814 .hooknum = NF_INET_FORWARD,
1817 #ifdef CONFIG_IP_VS_IPV6
1818 /* After packet filtering, change source only for VS/NAT */
1820 .hook = ip_vs_reply6,
1821 .owner = THIS_MODULE,
1823 .hooknum = NF_INET_LOCAL_IN,
1826 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1827 * or VS/NAT(change destination), so that filtering rules can be
1828 * applied to IPVS. */
1830 .hook = ip_vs_remote_request6,
1831 .owner = THIS_MODULE,
1833 .hooknum = NF_INET_LOCAL_IN,
1836 /* Before ip_vs_in, change source only for VS/NAT */
1838 .hook = ip_vs_local_reply6,
1839 .owner = THIS_MODULE,
1841 .hooknum = NF_INET_LOCAL_OUT,
1844 /* After mangle, schedule and forward local requests */
1846 .hook = ip_vs_local_request6,
1847 .owner = THIS_MODULE,
1849 .hooknum = NF_INET_LOCAL_OUT,
1852 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1853 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1855 .hook = ip_vs_forward_icmp_v6,
1856 .owner = THIS_MODULE,
1858 .hooknum = NF_INET_FORWARD,
1861 /* After packet filtering, change source only for VS/NAT */
1863 .hook = ip_vs_reply6,
1864 .owner = THIS_MODULE,
1866 .hooknum = NF_INET_FORWARD,
1872 * Initialize IP Virtual Server netns mem.
1874 static int __net_init __ip_vs_init(struct net *net)
1876 struct netns_ipvs *ipvs;
1878 if (!net_eq(net, &init_net)) {
1879 pr_err("The final patch for enabling netns is missing\n");
1882 ipvs = net_generic(net, ip_vs_net_id);
1884 pr_err("%s(): no memory.\n", __func__);
1888 /* Counters used for creating unique names */
1889 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1890 atomic_inc(&ipvs_netns_cnt);
1892 printk(KERN_INFO "IPVS: Creating netns size=%lu id=%d\n",
1893 sizeof(struct netns_ipvs), ipvs->gen);
1897 static void __net_exit __ip_vs_cleanup(struct net *net)
1899 struct netns_ipvs *ipvs = net_ipvs(net);
1901 IP_VS_DBG(10, "ipvs netns %d released\n", ipvs->gen);
1904 static struct pernet_operations ipvs_core_ops = {
1905 .init = __ip_vs_init,
1906 .exit = __ip_vs_cleanup,
1907 .id = &ip_vs_net_id,
1908 .size = sizeof(struct netns_ipvs),
1912 * Initialize IP Virtual Server
1914 static int __init ip_vs_init(void)
1918 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
1922 ip_vs_estimator_init();
1923 ret = ip_vs_control_init();
1925 pr_err("can't setup control.\n");
1926 goto cleanup_estimator;
1929 ip_vs_protocol_init();
1931 ret = ip_vs_app_init();
1933 pr_err("can't setup application helper.\n");
1934 goto cleanup_protocol;
1937 ret = ip_vs_conn_init();
1939 pr_err("can't setup connection table.\n");
1943 ret = ip_vs_sync_init();
1945 pr_err("can't setup sync data.\n");
1949 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1951 pr_err("can't register hooks.\n");
1955 pr_info("ipvs loaded.\n");
1959 ip_vs_sync_cleanup();
1961 ip_vs_conn_cleanup();
1963 ip_vs_app_cleanup();
1965 ip_vs_protocol_cleanup();
1966 ip_vs_control_cleanup();
1968 ip_vs_estimator_cleanup();
1969 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1973 static void __exit ip_vs_cleanup(void)
1975 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1976 ip_vs_sync_cleanup();
1977 ip_vs_conn_cleanup();
1978 ip_vs_app_cleanup();
1979 ip_vs_protocol_cleanup();
1980 ip_vs_control_cleanup();
1981 ip_vs_estimator_cleanup();
1982 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1983 pr_info("ipvs unloaded.\n");
1986 module_init(ip_vs_init);
1987 module_exit(ip_vs_cleanup);
1988 MODULE_LICENSE("GPL");