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_%d", 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;
120 s = this_cpu_ptr(dest->stats.cpustats);
122 u64_stats_update_begin(&s->syncp);
123 s->ustats.inbytes += skb->len;
124 u64_stats_update_end(&s->syncp);
126 s = this_cpu_ptr(dest->svc->stats.cpustats);
128 u64_stats_update_begin(&s->syncp);
129 s->ustats.inbytes += skb->len;
130 u64_stats_update_end(&s->syncp);
132 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
134 u64_stats_update_begin(&s->syncp);
135 s->ustats.inbytes += skb->len;
136 u64_stats_update_end(&s->syncp);
142 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
144 struct ip_vs_dest *dest = cp->dest;
145 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
147 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
148 struct ip_vs_cpu_stats *s;
150 s = this_cpu_ptr(dest->stats.cpustats);
152 u64_stats_update_begin(&s->syncp);
153 s->ustats.outbytes += skb->len;
154 u64_stats_update_end(&s->syncp);
156 s = this_cpu_ptr(dest->svc->stats.cpustats);
158 u64_stats_update_begin(&s->syncp);
159 s->ustats.outbytes += skb->len;
160 u64_stats_update_end(&s->syncp);
162 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
164 u64_stats_update_begin(&s->syncp);
165 s->ustats.outbytes += skb->len;
166 u64_stats_update_end(&s->syncp);
172 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
174 struct netns_ipvs *ipvs = net_ipvs(svc->net);
175 struct ip_vs_cpu_stats *s;
177 s = this_cpu_ptr(cp->dest->stats.cpustats);
180 s = this_cpu_ptr(svc->stats.cpustats);
183 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
189 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
190 const struct sk_buff *skb,
191 struct ip_vs_proto_data *pd)
193 if (likely(pd->pp->state_transition))
194 pd->pp->state_transition(cp, direction, skb, pd);
198 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
199 struct sk_buff *skb, int protocol,
200 const union nf_inet_addr *caddr, __be16 cport,
201 const union nf_inet_addr *vaddr, __be16 vport,
202 struct ip_vs_conn_param *p)
204 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
206 p->pe = rcu_dereference(svc->pe);
207 if (p->pe && p->pe->fill_param)
208 return p->pe->fill_param(p, skb);
214 * IPVS persistent scheduling function
215 * It creates a connection entry according to its template if exists,
216 * or selects a server and creates a connection entry plus a template.
217 * Locking: we are svc user (svc->refcnt), so we hold all dests too
218 * Protocols supported: TCP, UDP
220 static struct ip_vs_conn *
221 ip_vs_sched_persist(struct ip_vs_service *svc,
222 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
223 int *ignored, struct ip_vs_iphdr *iph)
225 struct ip_vs_conn *cp = NULL;
226 struct ip_vs_dest *dest;
227 struct ip_vs_conn *ct;
228 __be16 dport = 0; /* destination port to forward */
230 struct ip_vs_conn_param param;
231 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
232 union nf_inet_addr snet; /* source network of the client,
235 /* Mask saddr with the netmask to adjust template granularity */
236 #ifdef CONFIG_IP_VS_IPV6
237 if (svc->af == AF_INET6)
238 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
239 (__force __u32) svc->netmask);
242 snet.ip = iph->saddr.ip & svc->netmask;
244 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
246 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
247 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
248 IP_VS_DBG_ADDR(svc->af, &snet));
251 * As far as we know, FTP is a very complicated network protocol, and
252 * it uses control connection and data connections. For active FTP,
253 * FTP server initialize data connection to the client, its source port
254 * is often 20. For passive FTP, FTP server tells the clients the port
255 * that it passively listens to, and the client issues the data
256 * connection. In the tunneling or direct routing mode, the load
257 * balancer is on the client-to-server half of connection, the port
258 * number is unknown to the load balancer. So, a conn template like
259 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
260 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
261 * is created for other persistent services.
264 int protocol = iph->protocol;
265 const union nf_inet_addr *vaddr = &iph->daddr;
268 if (dst_port == svc->port) {
270 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
272 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
274 if (svc->port != FTPPORT)
277 /* Note: persistent fwmark-based services and
278 * persistent port zero service are handled here.
280 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
281 * port zero template:
282 * <protocol,caddr,0,vaddr,0,daddr,0>
285 protocol = IPPROTO_IP;
289 /* return *ignored = -1 so NF_DROP can be used */
290 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
291 vaddr, vport, ¶m) < 0) {
297 /* Check if a template already exists */
298 ct = ip_vs_ct_in_get(¶m);
299 if (!ct || !ip_vs_check_template(ct)) {
300 struct ip_vs_scheduler *sched;
303 * No template found or the dest of the connection
304 * template is not available.
305 * return *ignored=0 i.e. ICMP and NF_DROP
307 sched = rcu_dereference(svc->scheduler);
308 dest = sched->schedule(svc, skb, iph);
310 IP_VS_DBG(1, "p-schedule: no dest found.\n");
311 kfree(param.pe_data);
316 if (dst_port == svc->port && svc->port != FTPPORT)
320 * This adds param.pe_data to the template,
321 * and thus param.pe_data will be destroyed
322 * when the template expires */
323 ct = ip_vs_conn_new(¶m, &dest->addr, dport,
324 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
326 kfree(param.pe_data);
331 ct->timeout = svc->timeout;
333 /* set destination with the found template */
335 kfree(param.pe_data);
339 if (dport == svc->port && dest->port)
342 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
343 && iph->protocol == IPPROTO_UDP) ?
344 IP_VS_CONN_F_ONE_PACKET : 0;
347 * Create a new connection according to the template
349 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
350 src_port, &iph->daddr, dst_port, ¶m);
352 cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark);
362 ip_vs_control_add(cp, ct);
365 ip_vs_conn_stats(cp, svc);
371 * IPVS main scheduling function
372 * It selects a server according to the virtual service, and
373 * creates a connection entry.
374 * Protocols supported: TCP, UDP
378 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
379 * svc/scheduler decides that this packet should be accepted with
380 * NF_ACCEPT because it must not be scheduled.
382 * 0 : scheduler can not find destination, so try bypass or
383 * return ICMP and then NF_DROP (ip_vs_leave).
385 * -1 : scheduler tried to schedule but fatal error occurred, eg.
386 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
387 * failure such as missing Call-ID, ENOMEM on skb_linearize
388 * or pe_data. In this case we should return NF_DROP without
389 * any attempts to send ICMP with ip_vs_leave.
392 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
393 struct ip_vs_proto_data *pd, int *ignored,
394 struct ip_vs_iphdr *iph)
396 struct ip_vs_protocol *pp = pd->pp;
397 struct ip_vs_conn *cp = NULL;
398 struct ip_vs_scheduler *sched;
399 struct ip_vs_dest *dest;
400 __be16 _ports[2], *pptr;
405 * IPv6 frags, only the first hit here.
407 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
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, 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,
444 * Non-persistent service
446 if (!svc->fwmark && pptr[1] != svc->port) {
448 pr_err("Schedule: port zero only supported "
449 "in persistent services, "
450 "check your ipvs configuration\n");
454 sched = rcu_dereference(svc->scheduler);
455 dest = sched->schedule(svc, skb, iph);
457 IP_VS_DBG(1, "Schedule: no dest found.\n");
461 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
462 && iph->protocol == IPPROTO_UDP) ?
463 IP_VS_CONN_F_ONE_PACKET : 0;
466 * Create a connection entry.
469 struct ip_vs_conn_param p;
471 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
472 &iph->saddr, pptr[0], &iph->daddr,
474 cp = ip_vs_conn_new(&p, &dest->addr,
475 dest->port ? dest->port : pptr[1],
476 flags, dest, skb->mark);
483 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
484 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
486 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
487 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
488 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
489 cp->flags, atomic_read(&cp->refcnt));
491 ip_vs_conn_stats(cp, svc);
497 * Pass or drop the packet.
498 * Called by ip_vs_in, when the virtual service is available but
499 * no destination is available for a new connection.
501 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
502 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
504 __be16 _ports[2], *pptr;
507 struct netns_ipvs *ipvs;
511 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
519 #ifdef CONFIG_IP_VS_IPV6
520 if (svc->af == AF_INET6)
521 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
524 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
526 /* if it is fwmark-based service, the cache_bypass sysctl is up
527 and the destination is a non-local unicast, then create
528 a cache_bypass connection entry */
529 ipvs = net_ipvs(net);
530 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
532 struct ip_vs_conn *cp;
533 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
534 iph->protocol == IPPROTO_UDP) ?
535 IP_VS_CONN_F_ONE_PACKET : 0;
536 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
538 /* create a new connection entry */
539 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
541 struct ip_vs_conn_param p;
542 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
543 &iph->saddr, pptr[0],
544 &iph->daddr, pptr[1], &p);
545 cp = ip_vs_conn_new(&p, &daddr, 0,
546 IP_VS_CONN_F_BYPASS | flags,
553 ip_vs_in_stats(cp, skb);
556 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
558 /* transmit the first SYN packet */
559 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
560 /* do not touch skb anymore */
562 atomic_inc(&cp->in_pkts);
569 * When the virtual ftp service is presented, packets destined
570 * for other services on the VIP may get here (except services
571 * listed in the ipvs table), pass the packets, because it is
572 * not ipvs job to decide to drop the packets.
574 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
578 * Notify the client that the destination is unreachable, and
579 * release the socket buffer.
580 * Since it is in IP layer, the TCP socket is not actually
581 * created, the TCP RST packet cannot be sent, instead that
582 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
584 #ifdef CONFIG_IP_VS_IPV6
585 if (svc->af == AF_INET6) {
587 struct net *net_ = dev_net(skb_dst(skb)->dev);
589 skb->dev = net_->loopback_dev;
591 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
594 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
601 static int sysctl_snat_reroute(struct sk_buff *skb)
603 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
604 return ipvs->sysctl_snat_reroute;
607 static int sysctl_nat_icmp_send(struct net *net)
609 struct netns_ipvs *ipvs = net_ipvs(net);
610 return ipvs->sysctl_nat_icmp_send;
613 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
615 return ipvs->sysctl_expire_nodest_conn;
620 static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
621 static int sysctl_nat_icmp_send(struct net *net) { return 0; }
622 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
626 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
628 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
631 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
633 if (NF_INET_LOCAL_IN == hooknum)
634 return IP_DEFRAG_VS_IN;
635 if (NF_INET_FORWARD == hooknum)
636 return IP_DEFRAG_VS_FWD;
637 return IP_DEFRAG_VS_OUT;
640 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
645 err = ip_defrag(skb, user);
648 ip_send_check(ip_hdr(skb));
653 static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
655 #ifdef CONFIG_IP_VS_IPV6
656 if (af == AF_INET6) {
657 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
661 if ((sysctl_snat_reroute(skb) ||
662 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
663 ip_route_me_harder(skb, RTN_LOCAL) != 0)
670 * Packet has been made sufficiently writable in caller
671 * - inout: 1=in->out, 0=out->in
673 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
674 struct ip_vs_conn *cp, int inout)
676 struct iphdr *iph = ip_hdr(skb);
677 unsigned int icmp_offset = iph->ihl*4;
678 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
680 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
683 iph->saddr = cp->vaddr.ip;
685 ciph->daddr = cp->vaddr.ip;
688 iph->daddr = cp->daddr.ip;
690 ciph->saddr = cp->daddr.ip;
694 /* the TCP/UDP/SCTP port */
695 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
696 IPPROTO_SCTP == ciph->protocol) {
697 __be16 *ports = (void *)ciph + ciph->ihl*4;
700 ports[1] = cp->vport;
702 ports[0] = cp->dport;
705 /* And finally the ICMP checksum */
707 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
708 skb->ip_summed = CHECKSUM_UNNECESSARY;
711 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
712 "Forwarding altered outgoing ICMP");
714 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
715 "Forwarding altered incoming ICMP");
718 #ifdef CONFIG_IP_VS_IPV6
719 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
720 struct ip_vs_conn *cp, int inout)
722 struct ipv6hdr *iph = ipv6_hdr(skb);
723 unsigned int icmp_offset = 0;
724 unsigned int offs = 0; /* header offset*/
726 struct icmp6hdr *icmph;
727 struct ipv6hdr *ciph;
728 unsigned short fragoffs;
730 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
731 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
732 offs = icmp_offset + sizeof(struct icmp6hdr);
733 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
735 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
738 iph->saddr = cp->vaddr.in6;
739 ciph->daddr = cp->vaddr.in6;
741 iph->daddr = cp->daddr.in6;
742 ciph->saddr = cp->daddr.in6;
745 /* the TCP/UDP/SCTP port */
746 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
747 IPPROTO_SCTP == protocol)) {
748 __be16 *ports = (void *)(skb_network_header(skb) + offs);
750 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
751 ntohs(inout ? ports[1] : ports[0]),
752 ntohs(inout ? cp->vport : cp->dport));
754 ports[1] = cp->vport;
756 ports[0] = cp->dport;
759 /* And finally the ICMP checksum */
760 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
761 skb->len - icmp_offset,
763 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
764 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
765 skb->ip_summed = CHECKSUM_PARTIAL;
768 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
769 (void *)ciph - (void *)iph,
770 "Forwarding altered outgoing ICMPv6");
772 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
773 (void *)ciph - (void *)iph,
774 "Forwarding altered incoming ICMPv6");
778 /* Handle relevant response ICMP messages - forward to the right
781 static int handle_response_icmp(int af, struct sk_buff *skb,
782 union nf_inet_addr *snet,
783 __u8 protocol, struct ip_vs_conn *cp,
784 struct ip_vs_protocol *pp,
785 unsigned int offset, unsigned int ihl)
787 unsigned int verdict = NF_DROP;
789 if (IP_VS_FWD_METHOD(cp) != 0) {
790 pr_err("shouldn't reach here, because the box is on the "
791 "half connection in the tun/dr module.\n");
794 /* Ensure the checksum is correct */
795 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
796 /* Failed checksum! */
797 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
798 IP_VS_DBG_ADDR(af, snet));
802 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
803 IPPROTO_SCTP == protocol)
804 offset += 2 * sizeof(__u16);
805 if (!skb_make_writable(skb, offset))
808 #ifdef CONFIG_IP_VS_IPV6
810 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
813 ip_vs_nat_icmp(skb, pp, cp, 1);
815 if (ip_vs_route_me_harder(af, skb))
818 /* do the statistics and put it back */
819 ip_vs_out_stats(cp, skb);
821 skb->ipvs_property = 1;
822 if (!(cp->flags & IP_VS_CONN_F_NFCT))
825 ip_vs_update_conntrack(skb, cp, 0);
829 __ip_vs_conn_put(cp);
835 * Handle ICMP messages in the inside-to-outside direction (outgoing).
836 * Find any that might be relevant, check against existing connections.
837 * Currently handles error types - unreachable, quench, ttl exceeded.
839 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
840 unsigned int hooknum)
843 struct icmphdr _icmph, *ic;
844 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
845 struct ip_vs_iphdr ciph;
846 struct ip_vs_conn *cp;
847 struct ip_vs_protocol *pp;
848 unsigned int offset, ihl;
849 union nf_inet_addr snet;
853 /* reassemble IP fragments */
854 if (ip_is_fragment(ip_hdr(skb))) {
855 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
860 offset = ihl = iph->ihl * 4;
861 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
865 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
866 ic->type, ntohs(icmp_id(ic)),
867 &iph->saddr, &iph->daddr);
870 * Work through seeing if this is for us.
871 * These checks are supposed to be in an order that means easy
872 * things are checked first to speed up processing.... however
873 * this means that some packets will manage to get a long way
874 * down this stack and then be rejected, but that's life.
876 if ((ic->type != ICMP_DEST_UNREACH) &&
877 (ic->type != ICMP_SOURCE_QUENCH) &&
878 (ic->type != ICMP_TIME_EXCEEDED)) {
883 /* Now find the contained IP header */
884 offset += sizeof(_icmph);
885 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
887 return NF_ACCEPT; /* The packet looks wrong, ignore */
889 pp = ip_vs_proto_get(cih->protocol);
893 /* Is the embedded protocol header present? */
894 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
898 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
899 "Checking outgoing ICMP for");
901 ip_vs_fill_ip4hdr(cih, &ciph);
903 /* The embedded headers contain source and dest in reverse order */
904 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
908 snet.ip = iph->saddr;
909 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
913 #ifdef CONFIG_IP_VS_IPV6
914 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
915 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
917 struct icmp6hdr _icmph, *ic;
918 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
919 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
920 struct ip_vs_conn *cp;
921 struct ip_vs_protocol *pp;
922 union nf_inet_addr snet;
923 unsigned int writable;
926 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
931 * Work through seeing if this is for us.
932 * These checks are supposed to be in an order that means easy
933 * things are checked first to speed up processing.... however
934 * this means that some packets will manage to get a long way
935 * down this stack and then be rejected, but that's life.
937 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
941 /* Fragment header that is before ICMP header tells us that:
942 * it's not an error message since they can't be fragmented.
944 if (ipvsh->flags & IP6_FH_F_FRAG)
947 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
948 ic->icmp6_type, ntohs(icmpv6_id(ic)),
949 &ipvsh->saddr, &ipvsh->daddr);
951 /* Now find the contained IP header */
952 ciph.len = ipvsh->len + sizeof(_icmph);
953 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
955 return NF_ACCEPT; /* The packet looks wrong, ignore */
956 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
957 ciph.daddr.in6 = ip6h->daddr;
958 /* skip possible IPv6 exthdrs of contained IPv6 packet */
959 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
960 if (ciph.protocol < 0)
961 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
963 pp = ip_vs_proto_get(ciph.protocol);
967 /* The embedded headers contain source and dest in reverse order */
968 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
972 snet.in6 = ciph.saddr.in6;
974 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
975 pp, writable, sizeof(struct ipv6hdr));
980 * Check if sctp chunc is ABORT chunk
982 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
984 sctp_chunkhdr_t *sch, schunk;
985 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
986 sizeof(schunk), &schunk);
989 if (sch->type == SCTP_CID_ABORT)
994 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
996 struct tcphdr _tcph, *th;
998 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1004 static inline bool is_new_conn(const struct sk_buff *skb,
1005 struct ip_vs_iphdr *iph)
1007 switch (iph->protocol) {
1009 struct tcphdr _tcph, *th;
1011 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1016 case IPPROTO_SCTP: {
1017 sctp_chunkhdr_t *sch, schunk;
1019 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1020 sizeof(schunk), &schunk);
1023 return sch->type == SCTP_CID_INIT;
1030 /* Handle response packets: rewrite addresses and send away...
1033 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1034 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
1036 struct ip_vs_protocol *pp = pd->pp;
1038 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1040 if (!skb_make_writable(skb, iph->len))
1043 /* mangle the packet */
1044 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1047 #ifdef CONFIG_IP_VS_IPV6
1049 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1053 ip_hdr(skb)->saddr = cp->vaddr.ip;
1054 ip_send_check(ip_hdr(skb));
1058 * nf_iterate does not expect change in the skb->dst->dev.
1059 * It looks like it is not fatal to enable this code for hooks
1060 * where our handlers are at the end of the chain list and
1061 * when all next handlers use skb->dst->dev and not outdev.
1062 * It will definitely route properly the inout NAT traffic
1063 * when multiple paths are used.
1066 /* For policy routing, packets originating from this
1067 * machine itself may be routed differently to packets
1068 * passing through. We want this packet to be routed as
1069 * if it came from this machine itself. So re-compute
1070 * the routing information.
1072 if (ip_vs_route_me_harder(af, skb))
1075 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1077 ip_vs_out_stats(cp, skb);
1078 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1079 skb->ipvs_property = 1;
1080 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1083 ip_vs_update_conntrack(skb, cp, 0);
1097 * Check if outgoing packet belongs to the established ip_vs_conn.
1100 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1102 struct net *net = NULL;
1103 struct ip_vs_iphdr iph;
1104 struct ip_vs_protocol *pp;
1105 struct ip_vs_proto_data *pd;
1106 struct ip_vs_conn *cp;
1110 /* Already marked as IPVS request or reply? */
1111 if (skb->ipvs_property)
1114 /* Bad... Do not break raw sockets */
1115 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1117 struct sock *sk = skb->sk;
1118 struct inet_sock *inet = inet_sk(skb->sk);
1120 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1124 if (unlikely(!skb_dst(skb)))
1128 if (!net_ipvs(net)->enable)
1131 ip_vs_fill_iph_skb(af, skb, &iph);
1132 #ifdef CONFIG_IP_VS_IPV6
1133 if (af == AF_INET6) {
1134 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1135 struct sk_buff *reasm = skb_nfct_reasm(skb);
1136 /* Save fw mark for coming frags */
1137 reasm->ipvs_property = 1;
1138 reasm->mark = skb->mark;
1140 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1142 int verdict = ip_vs_out_icmp_v6(skb, &related,
1150 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1152 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1158 pd = ip_vs_proto_data_get(net, iph.protocol);
1163 /* reassemble IP fragments */
1164 #ifdef CONFIG_IP_VS_IPV6
1167 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1168 if (ip_vs_gather_frags(skb,
1169 ip_vs_defrag_user(hooknum)))
1172 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
1176 * Check if the packet belongs to an existing entry
1178 cp = pp->conn_out_get(af, skb, &iph, 0);
1181 return handle_response(af, skb, pd, cp, &iph);
1182 if (sysctl_nat_icmp_send(net) &&
1183 (pp->protocol == IPPROTO_TCP ||
1184 pp->protocol == IPPROTO_UDP ||
1185 pp->protocol == IPPROTO_SCTP)) {
1186 __be16 _ports[2], *pptr;
1188 pptr = frag_safe_skb_hp(skb, iph.len,
1189 sizeof(_ports), _ports, &iph);
1191 return NF_ACCEPT; /* Not for me */
1192 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1195 * Notify the real server: there is no
1196 * existing entry if it is not RST
1197 * packet or not TCP packet.
1199 if ((iph.protocol != IPPROTO_TCP &&
1200 iph.protocol != IPPROTO_SCTP)
1201 || ((iph.protocol == IPPROTO_TCP
1202 && !is_tcp_reset(skb, iph.len))
1203 || (iph.protocol == IPPROTO_SCTP
1204 && !is_sctp_abort(skb,
1206 #ifdef CONFIG_IP_VS_IPV6
1207 if (af == AF_INET6) {
1209 skb->dev = net->loopback_dev;
1211 ICMPV6_DEST_UNREACH,
1212 ICMPV6_PORT_UNREACH,
1218 ICMP_PORT_UNREACH, 0);
1223 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1224 "ip_vs_out: packet continues traversal as normal");
1229 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1230 * used only for VS/NAT.
1231 * Check if packet is reply for established ip_vs_conn.
1234 ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1235 const struct net_device *in, const struct net_device *out,
1236 int (*okfn)(struct sk_buff *))
1238 return ip_vs_out(hooknum, skb, AF_INET);
1242 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1243 * Check if packet is reply for established ip_vs_conn.
1246 ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1247 const struct net_device *in, const struct net_device *out,
1248 int (*okfn)(struct sk_buff *))
1250 return ip_vs_out(hooknum, skb, AF_INET);
1253 #ifdef CONFIG_IP_VS_IPV6
1256 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1257 * used only for VS/NAT.
1258 * Check if packet is reply for established ip_vs_conn.
1261 ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1262 const struct net_device *in, const struct net_device *out,
1263 int (*okfn)(struct sk_buff *))
1265 return ip_vs_out(hooknum, skb, AF_INET6);
1269 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1270 * Check if packet is reply for established ip_vs_conn.
1273 ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1274 const struct net_device *in, const struct net_device *out,
1275 int (*okfn)(struct sk_buff *))
1277 return ip_vs_out(hooknum, skb, AF_INET6);
1283 * Handle ICMP messages in the outside-to-inside direction (incoming).
1284 * Find any that might be relevant, check against existing connections,
1285 * forward to the right destination host if relevant.
1286 * Currently handles error types - unreachable, quench, ttl exceeded.
1289 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1291 struct net *net = NULL;
1293 struct icmphdr _icmph, *ic;
1294 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1295 struct ip_vs_iphdr ciph;
1296 struct ip_vs_conn *cp;
1297 struct ip_vs_protocol *pp;
1298 struct ip_vs_proto_data *pd;
1299 unsigned int offset, offset2, ihl, verdict;
1304 /* reassemble IP fragments */
1305 if (ip_is_fragment(ip_hdr(skb))) {
1306 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1311 offset = ihl = iph->ihl * 4;
1312 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1316 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1317 ic->type, ntohs(icmp_id(ic)),
1318 &iph->saddr, &iph->daddr);
1321 * Work through seeing if this is for us.
1322 * These checks are supposed to be in an order that means easy
1323 * things are checked first to speed up processing.... however
1324 * this means that some packets will manage to get a long way
1325 * down this stack and then be rejected, but that's life.
1327 if ((ic->type != ICMP_DEST_UNREACH) &&
1328 (ic->type != ICMP_SOURCE_QUENCH) &&
1329 (ic->type != ICMP_TIME_EXCEEDED)) {
1334 /* Now find the contained IP header */
1335 offset += sizeof(_icmph);
1336 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1338 return NF_ACCEPT; /* The packet looks wrong, ignore */
1342 /* Special case for errors for IPIP packets */
1344 if (cih->protocol == IPPROTO_IPIP) {
1345 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1347 /* Error for our IPIP must arrive at LOCAL_IN */
1348 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1350 offset += cih->ihl * 4;
1351 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1353 return NF_ACCEPT; /* The packet looks wrong, ignore */
1357 pd = ip_vs_proto_data_get(net, cih->protocol);
1362 /* Is the embedded protocol header present? */
1363 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1367 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1368 "Checking incoming ICMP for");
1371 ip_vs_fill_ip4hdr(cih, &ciph);
1374 /* The embedded headers contain source and dest in reverse order.
1375 * For IPIP this is error for request, not for reply.
1377 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
1383 /* Ensure the checksum is correct */
1384 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1385 /* Failed checksum! */
1386 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1392 __be32 info = ic->un.gateway;
1394 /* Update the MTU */
1395 if (ic->type == ICMP_DEST_UNREACH &&
1396 ic->code == ICMP_FRAG_NEEDED) {
1397 struct ip_vs_dest *dest = cp->dest;
1398 u32 mtu = ntohs(ic->un.frag.mtu);
1400 /* Strip outer IP and ICMP, go to IPIP header */
1401 __skb_pull(skb, ihl + sizeof(_icmph));
1402 offset2 -= ihl + sizeof(_icmph);
1403 skb_reset_network_header(skb);
1404 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1405 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1406 ipv4_update_pmtu(skb, dev_net(skb->dev),
1408 /* Client uses PMTUD? */
1409 if (!(cih->frag_off & htons(IP_DF)))
1411 /* Prefer the resulting PMTU */
1413 struct ip_vs_dest_dst *dest_dst;
1416 dest_dst = rcu_dereference(dest->dest_dst);
1418 mtu = dst_mtu(dest_dst->dst_cache);
1421 if (mtu > 68 + sizeof(struct iphdr))
1422 mtu -= sizeof(struct iphdr);
1425 /* Strip outer IP, ICMP and IPIP, go to IP header of
1428 __skb_pull(skb, offset2);
1429 skb_reset_network_header(skb);
1430 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1431 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1432 ic->type, ic->code, ntohl(info));
1433 icmp_send(skb, ic->type, ic->code, info);
1434 /* ICMP can be shorter but anyways, account it */
1435 ip_vs_out_stats(cp, skb);
1439 verdict = NF_STOLEN;
1443 /* do the statistics and put it back */
1444 ip_vs_in_stats(cp, skb);
1445 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1446 IPPROTO_SCTP == cih->protocol)
1447 offset += 2 * sizeof(__u16);
1448 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1451 __ip_vs_conn_put(cp);
1456 #ifdef CONFIG_IP_VS_IPV6
1457 static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1458 unsigned int hooknum, struct ip_vs_iphdr *iph)
1460 struct net *net = NULL;
1461 struct ipv6hdr _ip6h, *ip6h;
1462 struct icmp6hdr _icmph, *ic;
1463 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1464 struct ip_vs_conn *cp;
1465 struct ip_vs_protocol *pp;
1466 struct ip_vs_proto_data *pd;
1467 unsigned int offs_ciph, writable, verdict;
1471 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1476 * Work through seeing if this is for us.
1477 * These checks are supposed to be in an order that means easy
1478 * things are checked first to speed up processing.... however
1479 * this means that some packets will manage to get a long way
1480 * down this stack and then be rejected, but that's life.
1482 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1486 /* Fragment header that is before ICMP header tells us that:
1487 * it's not an error message since they can't be fragmented.
1489 if (iph->flags & IP6_FH_F_FRAG)
1492 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1493 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1494 &iph->saddr, &iph->daddr);
1496 /* Now find the contained IP header */
1497 ciph.len = iph->len + sizeof(_icmph);
1498 offs_ciph = ciph.len; /* Save ip header offset */
1499 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1501 return NF_ACCEPT; /* The packet looks wrong, ignore */
1502 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1503 ciph.daddr.in6 = ip6h->daddr;
1504 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1505 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1506 if (ciph.protocol < 0)
1507 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
1510 pd = ip_vs_proto_data_get(net, ciph.protocol);
1515 /* Cannot handle fragmented embedded protocol */
1519 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
1520 "Checking incoming ICMPv6 for");
1522 /* The embedded headers contain source and dest in reverse order
1523 * if not from localhost
1525 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
1526 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1530 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1531 if ((hooknum == NF_INET_LOCAL_OUT) &&
1532 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1533 __ip_vs_conn_put(cp);
1537 /* do the statistics and put it back */
1538 ip_vs_in_stats(cp, skb);
1540 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1541 writable = ciph.len;
1542 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1543 IPPROTO_SCTP == ciph.protocol)
1544 writable += 2 * sizeof(__u16); /* Also mangle ports */
1546 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
1548 __ip_vs_conn_put(cp);
1556 * Check if it's for virtual services, look it up,
1557 * and send it on its way...
1560 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1563 struct ip_vs_iphdr iph;
1564 struct ip_vs_protocol *pp;
1565 struct ip_vs_proto_data *pd;
1566 struct ip_vs_conn *cp;
1568 struct netns_ipvs *ipvs;
1570 /* Already marked as IPVS request or reply? */
1571 if (skb->ipvs_property)
1576 * - remote client: only PACKET_HOST
1577 * - route: used for struct net when skb->dev is unset
1579 if (unlikely((skb->pkt_type != PACKET_HOST &&
1580 hooknum != NF_INET_LOCAL_OUT) ||
1582 ip_vs_fill_iph_skb(af, skb, &iph);
1583 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1584 " ignored in hook %u\n",
1585 skb->pkt_type, iph.protocol,
1586 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1589 /* ipvs enabled in this netns ? */
1591 ipvs = net_ipvs(net);
1592 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1595 ip_vs_fill_iph_skb(af, skb, &iph);
1597 /* Bad... Do not break raw sockets */
1598 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1600 struct sock *sk = skb->sk;
1601 struct inet_sock *inet = inet_sk(skb->sk);
1603 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1607 #ifdef CONFIG_IP_VS_IPV6
1608 if (af == AF_INET6) {
1609 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1610 struct sk_buff *reasm = skb_nfct_reasm(skb);
1611 /* Save fw mark for coming frags. */
1612 reasm->ipvs_property = 1;
1613 reasm->mark = skb->mark;
1615 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1617 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1625 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1627 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1633 /* Protocol supported? */
1634 pd = ip_vs_proto_data_get(net, iph.protocol);
1639 * Check if the packet belongs to an existing connection entry
1641 cp = pp->conn_in_get(af, skb, &iph, 0);
1643 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1644 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1645 is_new_conn(skb, &iph)) {
1646 ip_vs_conn_expire_now(cp);
1647 __ip_vs_conn_put(cp);
1651 if (unlikely(!cp) && !iph.fragoffs) {
1652 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1653 * replayed fragment zero will already have created the cp
1657 /* Schedule and create new connection entry into &cp */
1658 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
1662 if (unlikely(!cp)) {
1663 /* sorry, all this trouble for a no-hit :) */
1664 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1665 "ip_vs_in: packet continues traversal as normal");
1666 if (iph.fragoffs && !skb_nfct_reasm(skb)) {
1667 /* Fragment that couldn't be mapped to a conn entry
1668 * and don't have any pointer to a reasm skb
1669 * is missing module nf_defrag_ipv6
1671 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1672 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1677 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1678 /* Check the server status */
1679 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1680 /* the destination server is not available */
1682 if (sysctl_expire_nodest_conn(ipvs)) {
1683 /* try to expire the connection immediately */
1684 ip_vs_conn_expire_now(cp);
1686 /* don't restart its timer, and silently
1688 __ip_vs_conn_put(cp);
1692 ip_vs_in_stats(cp, skb);
1693 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1694 if (cp->packet_xmit)
1695 ret = cp->packet_xmit(skb, cp, pp, &iph);
1696 /* do not touch skb anymore */
1698 IP_VS_DBG_RL("warning: packet_xmit is null");
1702 /* Increase its packet counter and check if it is needed
1703 * to be synchronized
1705 * Sync connection if it is about to close to
1706 * encorage the standby servers to update the connections timeout
1708 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1711 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1712 pkts = sysctl_sync_threshold(ipvs);
1714 pkts = atomic_add_return(1, &cp->in_pkts);
1716 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1717 ip_vs_sync_conn(net, cp, pkts);
1724 * AF_INET handler in NF_INET_LOCAL_IN chain
1725 * Schedule and forward packets from remote clients
1728 ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1729 const struct net_device *in,
1730 const struct net_device *out,
1731 int (*okfn)(struct sk_buff *))
1733 return ip_vs_in(hooknum, skb, AF_INET);
1737 * AF_INET handler in NF_INET_LOCAL_OUT chain
1738 * Schedule and forward packets from local clients
1741 ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1742 const struct net_device *in, const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1745 return ip_vs_in(hooknum, skb, AF_INET);
1748 #ifdef CONFIG_IP_VS_IPV6
1751 * AF_INET6 fragment handling
1752 * Copy info from first fragment, to the rest of them.
1755 ip_vs_preroute_frag6(unsigned int hooknum, struct sk_buff *skb,
1756 const struct net_device *in,
1757 const struct net_device *out,
1758 int (*okfn)(struct sk_buff *))
1760 struct sk_buff *reasm = skb_nfct_reasm(skb);
1763 /* Skip if not a "replay" from nf_ct_frag6_output or first fragment.
1764 * ipvs_property is set when checking first fragment
1765 * in ip_vs_in() and ip_vs_out().
1768 IP_VS_DBG(2, "Fragment recv prop:%d\n", reasm->ipvs_property);
1769 if (!reasm || !reasm->ipvs_property)
1773 if (!net_ipvs(net)->enable)
1776 /* Copy stored fw mark, saved in ip_vs_{in,out} */
1777 skb->mark = reasm->mark;
1783 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1784 * Schedule and forward packets from remote clients
1787 ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1788 const struct net_device *in,
1789 const struct net_device *out,
1790 int (*okfn)(struct sk_buff *))
1792 return ip_vs_in(hooknum, skb, AF_INET6);
1796 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1797 * Schedule and forward packets from local clients
1800 ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1801 const struct net_device *in, const struct net_device *out,
1802 int (*okfn)(struct sk_buff *))
1804 return ip_vs_in(hooknum, skb, AF_INET6);
1811 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1812 * related packets destined for 0.0.0.0/0.
1813 * When fwmark-based virtual service is used, such as transparent
1814 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1815 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1816 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1817 * and send them to ip_vs_in_icmp.
1820 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1821 const struct net_device *in, const struct net_device *out,
1822 int (*okfn)(struct sk_buff *))
1826 struct netns_ipvs *ipvs;
1828 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1831 /* ipvs enabled in this netns ? */
1833 ipvs = net_ipvs(net);
1834 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1837 return ip_vs_in_icmp(skb, &r, hooknum);
1840 #ifdef CONFIG_IP_VS_IPV6
1842 ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1843 const struct net_device *in, const struct net_device *out,
1844 int (*okfn)(struct sk_buff *))
1848 struct netns_ipvs *ipvs;
1849 struct ip_vs_iphdr iphdr;
1851 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1852 if (iphdr.protocol != IPPROTO_ICMPV6)
1855 /* ipvs enabled in this netns ? */
1857 ipvs = net_ipvs(net);
1858 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1861 return ip_vs_in_icmp_v6(skb, &r, hooknum, &iphdr);
1866 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1867 /* After packet filtering, change source only for VS/NAT */
1869 .hook = ip_vs_reply4,
1870 .owner = THIS_MODULE,
1872 .hooknum = NF_INET_LOCAL_IN,
1873 .priority = NF_IP_PRI_NAT_SRC - 2,
1875 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1876 * or VS/NAT(change destination), so that filtering rules can be
1877 * applied to IPVS. */
1879 .hook = ip_vs_remote_request4,
1880 .owner = THIS_MODULE,
1882 .hooknum = NF_INET_LOCAL_IN,
1883 .priority = NF_IP_PRI_NAT_SRC - 1,
1885 /* Before ip_vs_in, change source only for VS/NAT */
1887 .hook = ip_vs_local_reply4,
1888 .owner = THIS_MODULE,
1890 .hooknum = NF_INET_LOCAL_OUT,
1891 .priority = NF_IP_PRI_NAT_DST + 1,
1893 /* After mangle, schedule and forward local requests */
1895 .hook = ip_vs_local_request4,
1896 .owner = THIS_MODULE,
1898 .hooknum = NF_INET_LOCAL_OUT,
1899 .priority = NF_IP_PRI_NAT_DST + 2,
1901 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1902 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1904 .hook = ip_vs_forward_icmp,
1905 .owner = THIS_MODULE,
1907 .hooknum = NF_INET_FORWARD,
1910 /* After packet filtering, change source only for VS/NAT */
1912 .hook = ip_vs_reply4,
1913 .owner = THIS_MODULE,
1915 .hooknum = NF_INET_FORWARD,
1918 #ifdef CONFIG_IP_VS_IPV6
1919 /* After mangle & nat fetch 2:nd fragment and following */
1921 .hook = ip_vs_preroute_frag6,
1922 .owner = THIS_MODULE,
1924 .hooknum = NF_INET_PRE_ROUTING,
1925 .priority = NF_IP6_PRI_NAT_DST + 1,
1927 /* After packet filtering, change source only for VS/NAT */
1929 .hook = ip_vs_reply6,
1930 .owner = THIS_MODULE,
1932 .hooknum = NF_INET_LOCAL_IN,
1933 .priority = NF_IP6_PRI_NAT_SRC - 2,
1935 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1936 * or VS/NAT(change destination), so that filtering rules can be
1937 * applied to IPVS. */
1939 .hook = ip_vs_remote_request6,
1940 .owner = THIS_MODULE,
1942 .hooknum = NF_INET_LOCAL_IN,
1943 .priority = NF_IP6_PRI_NAT_SRC - 1,
1945 /* Before ip_vs_in, change source only for VS/NAT */
1947 .hook = ip_vs_local_reply6,
1948 .owner = THIS_MODULE,
1950 .hooknum = NF_INET_LOCAL_OUT,
1951 .priority = NF_IP6_PRI_NAT_DST + 1,
1953 /* After mangle, schedule and forward local requests */
1955 .hook = ip_vs_local_request6,
1956 .owner = THIS_MODULE,
1958 .hooknum = NF_INET_LOCAL_OUT,
1959 .priority = NF_IP6_PRI_NAT_DST + 2,
1961 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1962 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1964 .hook = ip_vs_forward_icmp_v6,
1965 .owner = THIS_MODULE,
1967 .hooknum = NF_INET_FORWARD,
1970 /* After packet filtering, change source only for VS/NAT */
1972 .hook = ip_vs_reply6,
1973 .owner = THIS_MODULE,
1975 .hooknum = NF_INET_FORWARD,
1981 * Initialize IP Virtual Server netns mem.
1983 static int __net_init __ip_vs_init(struct net *net)
1985 struct netns_ipvs *ipvs;
1987 ipvs = net_generic(net, ip_vs_net_id);
1991 /* Hold the beast until a service is registerd */
1994 /* Counters used for creating unique names */
1995 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1996 atomic_inc(&ipvs_netns_cnt);
1999 if (ip_vs_estimator_net_init(net) < 0)
2000 goto estimator_fail;
2002 if (ip_vs_control_net_init(net) < 0)
2005 if (ip_vs_protocol_net_init(net) < 0)
2008 if (ip_vs_app_net_init(net) < 0)
2011 if (ip_vs_conn_net_init(net) < 0)
2014 if (ip_vs_sync_net_init(net) < 0)
2017 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
2018 sizeof(struct netns_ipvs), ipvs->gen);
2025 ip_vs_conn_net_cleanup(net);
2027 ip_vs_app_net_cleanup(net);
2029 ip_vs_protocol_net_cleanup(net);
2031 ip_vs_control_net_cleanup(net);
2033 ip_vs_estimator_net_cleanup(net);
2039 static void __net_exit __ip_vs_cleanup(struct net *net)
2041 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2042 ip_vs_conn_net_cleanup(net);
2043 ip_vs_app_net_cleanup(net);
2044 ip_vs_protocol_net_cleanup(net);
2045 ip_vs_control_net_cleanup(net);
2046 ip_vs_estimator_net_cleanup(net);
2047 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
2051 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2054 net_ipvs(net)->enable = 0; /* Disable packet reception */
2056 ip_vs_sync_net_cleanup(net);
2060 static struct pernet_operations ipvs_core_ops = {
2061 .init = __ip_vs_init,
2062 .exit = __ip_vs_cleanup,
2063 .id = &ip_vs_net_id,
2064 .size = sizeof(struct netns_ipvs),
2067 static struct pernet_operations ipvs_core_dev_ops = {
2068 .exit = __ip_vs_dev_cleanup,
2072 * Initialize IP Virtual Server
2074 static int __init ip_vs_init(void)
2078 ret = ip_vs_control_init();
2080 pr_err("can't setup control.\n");
2084 ip_vs_protocol_init();
2086 ret = ip_vs_conn_init();
2088 pr_err("can't setup connection table.\n");
2089 goto cleanup_protocol;
2092 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2096 ret = register_pernet_device(&ipvs_core_dev_ops);
2100 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2102 pr_err("can't register hooks.\n");
2106 ret = ip_vs_register_nl_ioctl();
2108 pr_err("can't register netlink/ioctl.\n");
2112 pr_info("ipvs loaded.\n");
2117 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2119 unregister_pernet_device(&ipvs_core_dev_ops);
2121 unregister_pernet_subsys(&ipvs_core_ops);
2123 ip_vs_conn_cleanup();
2125 ip_vs_protocol_cleanup();
2126 ip_vs_control_cleanup();
2131 static void __exit ip_vs_cleanup(void)
2133 ip_vs_unregister_nl_ioctl();
2134 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2135 unregister_pernet_device(&ipvs_core_dev_ops);
2136 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2137 ip_vs_conn_cleanup();
2138 ip_vs_protocol_cleanup();
2139 ip_vs_control_cleanup();
2140 pr_info("ipvs unloaded.\n");
2143 module_init(ip_vs_init);
2144 module_exit(ip_vs_cleanup);
2145 MODULE_LICENSE("GPL");