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 int 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
->tot_stats
.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
->tot_stats
.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
->tot_stats
.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 (likely(pd
->pp
->state_transition
))
197 pd
->pp
->state_transition(cp
, direction
, skb
, pd
);
201 ip_vs_conn_fill_param_persist(const struct ip_vs_service
*svc
,
202 struct sk_buff
*skb
, int protocol
,
203 const union nf_inet_addr
*caddr
, __be16 cport
,
204 const union nf_inet_addr
*vaddr
, __be16 vport
,
205 struct ip_vs_conn_param
*p
)
207 ip_vs_conn_fill_param(svc
->net
, svc
->af
, protocol
, caddr
, cport
, vaddr
,
210 if (p
->pe
&& p
->pe
->fill_param
)
211 return p
->pe
->fill_param(p
, skb
);
217 * IPVS persistent scheduling function
218 * It creates a connection entry according to its template if exists,
219 * or selects a server and creates a connection entry plus a template.
220 * Locking: we are svc user (svc->refcnt), so we hold all dests too
221 * Protocols supported: TCP, UDP
223 static struct ip_vs_conn
*
224 ip_vs_sched_persist(struct ip_vs_service
*svc
,
225 struct sk_buff
*skb
, __be16 src_port
, __be16 dst_port
,
226 int *ignored
, struct ip_vs_iphdr
*iph
)
228 struct ip_vs_conn
*cp
= NULL
;
229 struct ip_vs_dest
*dest
;
230 struct ip_vs_conn
*ct
;
231 __be16 dport
= 0; /* destination port to forward */
233 struct ip_vs_conn_param param
;
234 const union nf_inet_addr fwmark
= { .ip
= htonl(svc
->fwmark
) };
235 union nf_inet_addr snet
; /* source network of the client,
238 /* Mask saddr with the netmask to adjust template granularity */
239 #ifdef CONFIG_IP_VS_IPV6
240 if (svc
->af
== AF_INET6
)
241 ipv6_addr_prefix(&snet
.in6
, &iph
->saddr
.in6
, svc
->netmask
);
244 snet
.ip
= iph
->saddr
.ip
& svc
->netmask
;
246 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
248 IP_VS_DBG_ADDR(svc
->af
, &iph
->saddr
), ntohs(src_port
),
249 IP_VS_DBG_ADDR(svc
->af
, &iph
->daddr
), ntohs(dst_port
),
250 IP_VS_DBG_ADDR(svc
->af
, &snet
));
253 * As far as we know, FTP is a very complicated network protocol, and
254 * it uses control connection and data connections. For active FTP,
255 * FTP server initialize data connection to the client, its source port
256 * is often 20. For passive FTP, FTP server tells the clients the port
257 * that it passively listens to, and the client issues the data
258 * connection. In the tunneling or direct routing mode, the load
259 * balancer is on the client-to-server half of connection, the port
260 * number is unknown to the load balancer. So, a conn template like
261 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
262 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
263 * is created for other persistent services.
266 int protocol
= iph
->protocol
;
267 const union nf_inet_addr
*vaddr
= &iph
->daddr
;
270 if (dst_port
== svc
->port
) {
272 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
274 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
276 if (svc
->port
!= FTPPORT
)
279 /* Note: persistent fwmark-based services and
280 * persistent port zero service are handled here.
282 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
283 * port zero template:
284 * <protocol,caddr,0,vaddr,0,daddr,0>
287 protocol
= IPPROTO_IP
;
291 /* return *ignored = -1 so NF_DROP can be used */
292 if (ip_vs_conn_fill_param_persist(svc
, skb
, protocol
, &snet
, 0,
293 vaddr
, vport
, ¶m
) < 0) {
299 /* Check if a template already exists */
300 ct
= ip_vs_ct_in_get(¶m
);
301 if (!ct
|| !ip_vs_check_template(ct
)) {
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 dest
= svc
->scheduler
->schedule(svc
, skb
);
309 IP_VS_DBG(1, "p-schedule: no dest found.\n");
310 kfree(param
.pe_data
);
315 if (dst_port
== svc
->port
&& svc
->port
!= FTPPORT
)
319 * This adds param.pe_data to the template,
320 * and thus param.pe_data will be destroyed
321 * when the template expires */
322 ct
= ip_vs_conn_new(¶m
, &dest
->addr
, dport
,
323 IP_VS_CONN_F_TEMPLATE
, dest
, skb
->mark
);
325 kfree(param
.pe_data
);
330 ct
->timeout
= svc
->timeout
;
332 /* set destination with the found template */
334 kfree(param
.pe_data
);
338 if (dport
== svc
->port
&& dest
->port
)
341 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
342 && iph
->protocol
== IPPROTO_UDP
) ?
343 IP_VS_CONN_F_ONE_PACKET
: 0;
346 * Create a new connection according to the template
348 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
, &iph
->saddr
,
349 src_port
, &iph
->daddr
, dst_port
, ¶m
);
351 cp
= ip_vs_conn_new(¶m
, &dest
->addr
, dport
, flags
, dest
, skb
->mark
);
361 ip_vs_control_add(cp
, ct
);
364 ip_vs_conn_stats(cp
, svc
);
370 * IPVS main scheduling function
371 * It selects a server according to the virtual service, and
372 * creates a connection entry.
373 * Protocols supported: TCP, UDP
377 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
378 * svc/scheduler decides that this packet should be accepted with
379 * NF_ACCEPT because it must not be scheduled.
381 * 0 : scheduler can not find destination, so try bypass or
382 * return ICMP and then NF_DROP (ip_vs_leave).
384 * -1 : scheduler tried to schedule but fatal error occurred, eg.
385 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
386 * failure such as missing Call-ID, ENOMEM on skb_linearize
387 * or pe_data. In this case we should return NF_DROP without
388 * any attempts to send ICMP with ip_vs_leave.
391 ip_vs_schedule(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
392 struct ip_vs_proto_data
*pd
, int *ignored
,
393 struct ip_vs_iphdr
*iph
)
395 struct ip_vs_protocol
*pp
= pd
->pp
;
396 struct ip_vs_conn
*cp
= NULL
;
397 struct ip_vs_dest
*dest
;
398 __be16 _ports
[2], *pptr
;
403 * IPv6 frags, only the first hit here.
405 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
410 * FTPDATA needs this check when using local real server.
411 * Never schedule Active FTPDATA connections from real server.
412 * For LVS-NAT they must be already created. For other methods
413 * with persistence the connection is created on SYN+ACK.
415 if (pptr
[0] == FTPDATA
) {
416 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
417 "Not scheduling FTPDATA");
422 * Do not schedule replies from local real server.
424 if ((!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
) &&
425 (cp
= pp
->conn_in_get(svc
->af
, skb
, iph
, 1))) {
426 IP_VS_DBG_PKT(12, svc
->af
, pp
, skb
, 0,
427 "Not scheduling reply for existing connection");
428 __ip_vs_conn_put(cp
);
435 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
436 return ip_vs_sched_persist(svc
, skb
, pptr
[0], pptr
[1], ignored
,
442 * Non-persistent service
444 if (!svc
->fwmark
&& pptr
[1] != svc
->port
) {
446 pr_err("Schedule: port zero only supported "
447 "in persistent services, "
448 "check your ipvs configuration\n");
452 dest
= svc
->scheduler
->schedule(svc
, skb
);
454 IP_VS_DBG(1, "Schedule: no dest found.\n");
458 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
459 && iph
->protocol
== IPPROTO_UDP
) ?
460 IP_VS_CONN_F_ONE_PACKET
: 0;
463 * Create a connection entry.
466 struct ip_vs_conn_param p
;
468 ip_vs_conn_fill_param(svc
->net
, svc
->af
, iph
->protocol
,
469 &iph
->saddr
, pptr
[0], &iph
->daddr
,
471 cp
= ip_vs_conn_new(&p
, &dest
->addr
,
472 dest
->port
? dest
->port
: pptr
[1],
473 flags
, dest
, skb
->mark
);
480 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
481 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
483 IP_VS_DBG_ADDR(svc
->af
, &cp
->caddr
), ntohs(cp
->cport
),
484 IP_VS_DBG_ADDR(svc
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
485 IP_VS_DBG_ADDR(svc
->af
, &cp
->daddr
), ntohs(cp
->dport
),
486 cp
->flags
, atomic_read(&cp
->refcnt
));
488 ip_vs_conn_stats(cp
, svc
);
494 * Pass or drop the packet.
495 * Called by ip_vs_in, when the virtual service is available but
496 * no destination is available for a new connection.
498 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
499 struct ip_vs_proto_data
*pd
, struct ip_vs_iphdr
*iph
)
501 __be16 _ports
[2], *pptr
;
504 struct netns_ipvs
*ipvs
;
508 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
510 ip_vs_service_put(svc
);
517 #ifdef CONFIG_IP_VS_IPV6
518 if (svc
->af
== AF_INET6
)
519 unicast
= ipv6_addr_type(&iph
->daddr
.in6
) & IPV6_ADDR_UNICAST
;
522 unicast
= (inet_addr_type(net
, iph
->daddr
.ip
) == RTN_UNICAST
);
524 /* if it is fwmark-based service, the cache_bypass sysctl is up
525 and the destination is a non-local unicast, then create
526 a cache_bypass connection entry */
527 ipvs
= net_ipvs(net
);
528 if (ipvs
->sysctl_cache_bypass
&& svc
->fwmark
&& unicast
) {
530 struct ip_vs_conn
*cp
;
531 unsigned int flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
532 iph
->protocol
== IPPROTO_UDP
) ?
533 IP_VS_CONN_F_ONE_PACKET
: 0;
534 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
536 ip_vs_service_put(svc
);
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
)) {
575 ip_vs_service_put(svc
);
579 ip_vs_service_put(svc
);
582 * Notify the client that the destination is unreachable, and
583 * release the socket buffer.
584 * Since it is in IP layer, the TCP socket is not actually
585 * created, the TCP RST packet cannot be sent, instead that
586 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
588 #ifdef CONFIG_IP_VS_IPV6
589 if (svc
->af
== AF_INET6
) {
591 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
593 skb
->dev
= net
->loopback_dev
;
595 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
598 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
605 static int sysctl_snat_reroute(struct sk_buff
*skb
)
607 struct netns_ipvs
*ipvs
= net_ipvs(skb_net(skb
));
608 return ipvs
->sysctl_snat_reroute
;
611 static int sysctl_nat_icmp_send(struct net
*net
)
613 struct netns_ipvs
*ipvs
= net_ipvs(net
);
614 return ipvs
->sysctl_nat_icmp_send
;
617 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
)
619 return ipvs
->sysctl_expire_nodest_conn
;
624 static int sysctl_snat_reroute(struct sk_buff
*skb
) { return 0; }
625 static int sysctl_nat_icmp_send(struct net
*net
) { return 0; }
626 static int sysctl_expire_nodest_conn(struct netns_ipvs
*ipvs
) { return 0; }
630 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
632 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
635 static inline enum ip_defrag_users
ip_vs_defrag_user(unsigned int hooknum
)
637 if (NF_INET_LOCAL_IN
== hooknum
)
638 return IP_DEFRAG_VS_IN
;
639 if (NF_INET_FORWARD
== hooknum
)
640 return IP_DEFRAG_VS_FWD
;
641 return IP_DEFRAG_VS_OUT
;
644 static inline int ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
646 int err
= ip_defrag(skb
, user
);
649 ip_send_check(ip_hdr(skb
));
654 static int ip_vs_route_me_harder(int af
, struct sk_buff
*skb
)
656 #ifdef CONFIG_IP_VS_IPV6
657 if (af
== AF_INET6
) {
658 if (sysctl_snat_reroute(skb
) && ip6_route_me_harder(skb
) != 0)
662 if ((sysctl_snat_reroute(skb
) ||
663 skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
) &&
664 ip_route_me_harder(skb
, RTN_LOCAL
) != 0)
671 * Packet has been made sufficiently writable in caller
672 * - inout: 1=in->out, 0=out->in
674 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
675 struct ip_vs_conn
*cp
, int inout
)
677 struct iphdr
*iph
= ip_hdr(skb
);
678 unsigned int icmp_offset
= iph
->ihl
*4;
679 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
681 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
684 iph
->saddr
= cp
->vaddr
.ip
;
686 ciph
->daddr
= cp
->vaddr
.ip
;
689 iph
->daddr
= cp
->daddr
.ip
;
691 ciph
->saddr
= cp
->daddr
.ip
;
695 /* the TCP/UDP/SCTP port */
696 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
697 IPPROTO_SCTP
== ciph
->protocol
) {
698 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
701 ports
[1] = cp
->vport
;
703 ports
[0] = cp
->dport
;
706 /* And finally the ICMP checksum */
708 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
709 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
712 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
713 "Forwarding altered outgoing ICMP");
715 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, (void *)ciph
- (void *)iph
,
716 "Forwarding altered incoming ICMP");
719 #ifdef CONFIG_IP_VS_IPV6
720 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
721 struct ip_vs_conn
*cp
, int inout
)
723 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
724 unsigned int icmp_offset
= 0;
725 unsigned int offs
= 0; /* header offset*/
727 struct icmp6hdr
*icmph
;
728 struct ipv6hdr
*ciph
;
729 unsigned short fragoffs
;
731 ipv6_find_hdr(skb
, &icmp_offset
, IPPROTO_ICMPV6
, &fragoffs
, NULL
);
732 icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) + icmp_offset
);
733 offs
= icmp_offset
+ sizeof(struct icmp6hdr
);
734 ciph
= (struct ipv6hdr
*)(skb_network_header(skb
) + offs
);
736 protocol
= ipv6_find_hdr(skb
, &offs
, -1, &fragoffs
, NULL
);
739 iph
->saddr
= cp
->vaddr
.in6
;
740 ciph
->daddr
= cp
->vaddr
.in6
;
742 iph
->daddr
= cp
->daddr
.in6
;
743 ciph
->saddr
= cp
->daddr
.in6
;
746 /* the TCP/UDP/SCTP port */
747 if (!fragoffs
&& (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
748 IPPROTO_SCTP
== protocol
)) {
749 __be16
*ports
= (void *)(skb_network_header(skb
) + offs
);
751 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__
,
752 ntohs(inout
? ports
[1] : ports
[0]),
753 ntohs(inout
? cp
->vport
: cp
->dport
));
755 ports
[1] = cp
->vport
;
757 ports
[0] = cp
->dport
;
760 /* And finally the ICMP checksum */
761 icmph
->icmp6_cksum
= ~csum_ipv6_magic(&iph
->saddr
, &iph
->daddr
,
762 skb
->len
- icmp_offset
,
764 skb
->csum_start
= skb_network_header(skb
) - skb
->head
+ icmp_offset
;
765 skb
->csum_offset
= offsetof(struct icmp6hdr
, icmp6_cksum
);
766 skb
->ip_summed
= CHECKSUM_PARTIAL
;
769 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
770 (void *)ciph
- (void *)iph
,
771 "Forwarding altered outgoing ICMPv6");
773 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
,
774 (void *)ciph
- (void *)iph
,
775 "Forwarding altered incoming ICMPv6");
779 /* Handle relevant response ICMP messages - forward to the right
782 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
783 union nf_inet_addr
*snet
,
784 __u8 protocol
, struct ip_vs_conn
*cp
,
785 struct ip_vs_protocol
*pp
,
786 unsigned int offset
, unsigned int ihl
)
788 unsigned int verdict
= NF_DROP
;
790 if (IP_VS_FWD_METHOD(cp
) != 0) {
791 pr_err("shouldn't reach here, because the box is on the "
792 "half connection in the tun/dr module.\n");
795 /* Ensure the checksum is correct */
796 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
797 /* Failed checksum! */
798 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
799 IP_VS_DBG_ADDR(af
, snet
));
803 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
804 IPPROTO_SCTP
== protocol
)
805 offset
+= 2 * sizeof(__u16
);
806 if (!skb_make_writable(skb
, offset
))
809 #ifdef CONFIG_IP_VS_IPV6
811 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
814 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
816 if (ip_vs_route_me_harder(af
, skb
))
819 /* do the statistics and put it back */
820 ip_vs_out_stats(cp
, skb
);
822 skb
->ipvs_property
= 1;
823 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
826 ip_vs_update_conntrack(skb
, cp
, 0);
830 __ip_vs_conn_put(cp
);
836 * Handle ICMP messages in the inside-to-outside direction (outgoing).
837 * Find any that might be relevant, check against existing connections.
838 * Currently handles error types - unreachable, quench, ttl exceeded.
840 static int ip_vs_out_icmp(struct sk_buff
*skb
, int *related
,
841 unsigned int hooknum
)
844 struct icmphdr _icmph
, *ic
;
845 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
846 struct ip_vs_iphdr ciph
;
847 struct ip_vs_conn
*cp
;
848 struct ip_vs_protocol
*pp
;
849 unsigned int offset
, ihl
;
850 union nf_inet_addr snet
;
854 /* reassemble IP fragments */
855 if (ip_is_fragment(ip_hdr(skb
))) {
856 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
861 offset
= ihl
= iph
->ihl
* 4;
862 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
866 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
867 ic
->type
, ntohs(icmp_id(ic
)),
868 &iph
->saddr
, &iph
->daddr
);
871 * Work through seeing if this is for us.
872 * These checks are supposed to be in an order that means easy
873 * things are checked first to speed up processing.... however
874 * this means that some packets will manage to get a long way
875 * down this stack and then be rejected, but that's life.
877 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
878 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
879 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
884 /* Now find the contained IP header */
885 offset
+= sizeof(_icmph
);
886 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
888 return NF_ACCEPT
; /* The packet looks wrong, ignore */
890 pp
= ip_vs_proto_get(cih
->protocol
);
894 /* Is the embedded protocol header present? */
895 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
899 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
900 "Checking outgoing ICMP for");
902 ip_vs_fill_ip4hdr(cih
, &ciph
);
904 /* The embedded headers contain source and dest in reverse order */
905 cp
= pp
->conn_out_get(AF_INET
, skb
, &ciph
, 1);
909 snet
.ip
= iph
->saddr
;
910 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
914 #ifdef CONFIG_IP_VS_IPV6
915 static int ip_vs_out_icmp_v6(struct sk_buff
*skb
, int *related
,
916 unsigned int hooknum
, struct ip_vs_iphdr
*ipvsh
)
918 struct icmp6hdr _icmph
, *ic
;
919 struct ipv6hdr _ip6h
, *ip6h
; /* The ip header contained within ICMP */
920 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
921 struct ip_vs_conn
*cp
;
922 struct ip_vs_protocol
*pp
;
923 union nf_inet_addr snet
;
924 unsigned int writable
;
927 ic
= frag_safe_skb_hp(skb
, ipvsh
->len
, sizeof(_icmph
), &_icmph
, ipvsh
);
932 * Work through seeing if this is for us.
933 * These checks are supposed to be in an order that means easy
934 * things are checked first to speed up processing.... however
935 * this means that some packets will manage to get a long way
936 * down this stack and then be rejected, but that's life.
938 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
942 /* Fragment header that is before ICMP header tells us that:
943 * it's not an error message since they can't be fragmented.
945 if (ipvsh
->flags
& IP6_FH_F_FRAG
)
948 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
949 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
950 &ipvsh
->saddr
, &ipvsh
->daddr
);
952 /* Now find the contained IP header */
953 ciph
.len
= ipvsh
->len
+ sizeof(_icmph
);
954 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
956 return NF_ACCEPT
; /* The packet looks wrong, ignore */
957 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_out_get() handles reverse order */
958 ciph
.daddr
.in6
= ip6h
->daddr
;
959 /* skip possible IPv6 exthdrs of contained IPv6 packet */
960 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
961 if (ciph
.protocol
< 0)
962 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
964 pp
= ip_vs_proto_get(ciph
.protocol
);
968 /* The embedded headers contain source and dest in reverse order */
969 cp
= pp
->conn_out_get(AF_INET6
, skb
, &ciph
, 1);
973 snet
.in6
= ciph
.saddr
.in6
;
975 return handle_response_icmp(AF_INET6
, skb
, &snet
, ciph
.protocol
, cp
,
976 pp
, writable
, sizeof(struct ipv6hdr
));
981 * Check if sctp chunc is ABORT chunk
983 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
985 sctp_chunkhdr_t
*sch
, schunk
;
986 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
987 sizeof(schunk
), &schunk
);
990 if (sch
->type
== SCTP_CID_ABORT
)
995 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
997 struct tcphdr _tcph
, *th
;
999 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
1005 /* Handle response packets: rewrite addresses and send away...
1008 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
1009 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
1011 struct ip_vs_protocol
*pp
= pd
->pp
;
1013 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Outgoing packet");
1015 if (!skb_make_writable(skb
, iph
->len
))
1018 /* mangle the packet */
1019 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
, iph
))
1022 #ifdef CONFIG_IP_VS_IPV6
1024 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
1028 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
1029 ip_send_check(ip_hdr(skb
));
1033 * nf_iterate does not expect change in the skb->dst->dev.
1034 * It looks like it is not fatal to enable this code for hooks
1035 * where our handlers are at the end of the chain list and
1036 * when all next handlers use skb->dst->dev and not outdev.
1037 * It will definitely route properly the inout NAT traffic
1038 * when multiple paths are used.
1041 /* For policy routing, packets originating from this
1042 * machine itself may be routed differently to packets
1043 * passing through. We want this packet to be routed as
1044 * if it came from this machine itself. So re-compute
1045 * the routing information.
1047 if (ip_vs_route_me_harder(af
, skb
))
1050 IP_VS_DBG_PKT(10, af
, pp
, skb
, 0, "After SNAT");
1052 ip_vs_out_stats(cp
, skb
);
1053 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pd
);
1054 skb
->ipvs_property
= 1;
1055 if (!(cp
->flags
& IP_VS_CONN_F_NFCT
))
1058 ip_vs_update_conntrack(skb
, cp
, 0);
1072 * Check if outgoing packet belongs to the established ip_vs_conn.
1075 ip_vs_out(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1077 struct net
*net
= NULL
;
1078 struct ip_vs_iphdr iph
;
1079 struct ip_vs_protocol
*pp
;
1080 struct ip_vs_proto_data
*pd
;
1081 struct ip_vs_conn
*cp
;
1085 /* Already marked as IPVS request or reply? */
1086 if (skb
->ipvs_property
)
1089 /* Bad... Do not break raw sockets */
1090 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1092 struct sock
*sk
= skb
->sk
;
1093 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1095 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1099 if (unlikely(!skb_dst(skb
)))
1103 if (!net_ipvs(net
)->enable
)
1106 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1107 #ifdef CONFIG_IP_VS_IPV6
1108 if (af
== AF_INET6
) {
1109 if (!iph
.fragoffs
&& skb_nfct_reasm(skb
)) {
1110 struct sk_buff
*reasm
= skb_nfct_reasm(skb
);
1111 /* Save fw mark for coming frags */
1112 reasm
->ipvs_property
= 1;
1113 reasm
->mark
= skb
->mark
;
1115 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1117 int verdict
= ip_vs_out_icmp_v6(skb
, &related
,
1125 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1127 int verdict
= ip_vs_out_icmp(skb
, &related
, hooknum
);
1133 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1138 /* reassemble IP fragments */
1139 #ifdef CONFIG_IP_VS_IPV6
1142 if (unlikely(ip_is_fragment(ip_hdr(skb
)) && !pp
->dont_defrag
)) {
1143 if (ip_vs_gather_frags(skb
,
1144 ip_vs_defrag_user(hooknum
)))
1147 ip_vs_fill_ip4hdr(skb_network_header(skb
), &iph
);
1151 * Check if the packet belongs to an existing entry
1153 cp
= pp
->conn_out_get(af
, skb
, &iph
, 0);
1156 return handle_response(af
, skb
, pd
, cp
, &iph
);
1157 if (sysctl_nat_icmp_send(net
) &&
1158 (pp
->protocol
== IPPROTO_TCP
||
1159 pp
->protocol
== IPPROTO_UDP
||
1160 pp
->protocol
== IPPROTO_SCTP
)) {
1161 __be16 _ports
[2], *pptr
;
1163 pptr
= frag_safe_skb_hp(skb
, iph
.len
,
1164 sizeof(_ports
), _ports
, &iph
);
1166 return NF_ACCEPT
; /* Not for me */
1167 if (ip_vs_lookup_real_service(net
, af
, iph
.protocol
,
1171 * Notify the real server: there is no
1172 * existing entry if it is not RST
1173 * packet or not TCP packet.
1175 if ((iph
.protocol
!= IPPROTO_TCP
&&
1176 iph
.protocol
!= IPPROTO_SCTP
)
1177 || ((iph
.protocol
== IPPROTO_TCP
1178 && !is_tcp_reset(skb
, iph
.len
))
1179 || (iph
.protocol
== IPPROTO_SCTP
1180 && !is_sctp_abort(skb
,
1182 #ifdef CONFIG_IP_VS_IPV6
1183 if (af
== AF_INET6
) {
1185 dev_net(skb_dst(skb
)->dev
);
1188 skb
->dev
= net
->loopback_dev
;
1190 ICMPV6_DEST_UNREACH
,
1191 ICMPV6_PORT_UNREACH
,
1197 ICMP_PORT_UNREACH
, 0);
1202 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1203 "ip_vs_out: packet continues traversal as normal");
1208 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1209 * used only for VS/NAT.
1210 * Check if packet is reply for established ip_vs_conn.
1213 ip_vs_reply4(unsigned int hooknum
, struct sk_buff
*skb
,
1214 const struct net_device
*in
, const struct net_device
*out
,
1215 int (*okfn
)(struct sk_buff
*))
1217 return ip_vs_out(hooknum
, skb
, AF_INET
);
1221 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1222 * Check if packet is reply for established ip_vs_conn.
1225 ip_vs_local_reply4(unsigned int hooknum
, struct sk_buff
*skb
,
1226 const struct net_device
*in
, const struct net_device
*out
,
1227 int (*okfn
)(struct sk_buff
*))
1229 unsigned int verdict
;
1231 /* Disable BH in LOCAL_OUT until all places are fixed */
1233 verdict
= ip_vs_out(hooknum
, skb
, AF_INET
);
1238 #ifdef CONFIG_IP_VS_IPV6
1241 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1242 * used only for VS/NAT.
1243 * Check if packet is reply for established ip_vs_conn.
1246 ip_vs_reply6(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_INET6
);
1254 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1255 * Check if packet is reply for established ip_vs_conn.
1258 ip_vs_local_reply6(unsigned int hooknum
, struct sk_buff
*skb
,
1259 const struct net_device
*in
, const struct net_device
*out
,
1260 int (*okfn
)(struct sk_buff
*))
1262 unsigned int verdict
;
1264 /* Disable BH in LOCAL_OUT until all places are fixed */
1266 verdict
= ip_vs_out(hooknum
, skb
, AF_INET6
);
1274 * Handle ICMP messages in the outside-to-inside direction (incoming).
1275 * Find any that might be relevant, check against existing connections,
1276 * forward to the right destination host if relevant.
1277 * Currently handles error types - unreachable, quench, ttl exceeded.
1280 ip_vs_in_icmp(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1282 struct net
*net
= NULL
;
1284 struct icmphdr _icmph
, *ic
;
1285 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1286 struct ip_vs_iphdr ciph
;
1287 struct ip_vs_conn
*cp
;
1288 struct ip_vs_protocol
*pp
;
1289 struct ip_vs_proto_data
*pd
;
1290 unsigned int offset
, offset2
, ihl
, verdict
;
1295 /* reassemble IP fragments */
1296 if (ip_is_fragment(ip_hdr(skb
))) {
1297 if (ip_vs_gather_frags(skb
, ip_vs_defrag_user(hooknum
)))
1302 offset
= ihl
= iph
->ihl
* 4;
1303 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1307 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1308 ic
->type
, ntohs(icmp_id(ic
)),
1309 &iph
->saddr
, &iph
->daddr
);
1312 * Work through seeing if this is for us.
1313 * These checks are supposed to be in an order that means easy
1314 * things are checked first to speed up processing.... however
1315 * this means that some packets will manage to get a long way
1316 * down this stack and then be rejected, but that's life.
1318 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1319 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1320 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1325 /* Now find the contained IP header */
1326 offset
+= sizeof(_icmph
);
1327 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1329 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1333 /* Special case for errors for IPIP packets */
1335 if (cih
->protocol
== IPPROTO_IPIP
) {
1336 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
)))
1338 /* Error for our IPIP must arrive at LOCAL_IN */
1339 if (!(skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
))
1341 offset
+= cih
->ihl
* 4;
1342 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1344 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1348 pd
= ip_vs_proto_data_get(net
, cih
->protocol
);
1353 /* Is the embedded protocol header present? */
1354 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1358 IP_VS_DBG_PKT(11, AF_INET
, pp
, skb
, offset
,
1359 "Checking incoming ICMP for");
1362 ip_vs_fill_ip4hdr(cih
, &ciph
);
1365 /* The embedded headers contain source and dest in reverse order.
1366 * For IPIP this is error for request, not for reply.
1368 cp
= pp
->conn_in_get(AF_INET
, skb
, &ciph
, ipip
? 0 : 1);
1374 /* Ensure the checksum is correct */
1375 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1376 /* Failed checksum! */
1377 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1383 __be32 info
= ic
->un
.gateway
;
1385 /* Update the MTU */
1386 if (ic
->type
== ICMP_DEST_UNREACH
&&
1387 ic
->code
== ICMP_FRAG_NEEDED
) {
1388 struct ip_vs_dest
*dest
= cp
->dest
;
1389 u32 mtu
= ntohs(ic
->un
.frag
.mtu
);
1391 /* Strip outer IP and ICMP, go to IPIP header */
1392 __skb_pull(skb
, ihl
+ sizeof(_icmph
));
1393 offset2
-= ihl
+ sizeof(_icmph
);
1394 skb_reset_network_header(skb
);
1395 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1396 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
, mtu
);
1398 ipv4_update_pmtu(skb
, dev_net(skb
->dev
),
1401 /* Client uses PMTUD? */
1402 if (!(cih
->frag_off
& htons(IP_DF
)))
1404 /* Prefer the resulting PMTU */
1406 spin_lock(&dest
->dst_lock
);
1407 if (dest
->dst_cache
)
1408 mtu
= dst_mtu(dest
->dst_cache
);
1409 spin_unlock(&dest
->dst_lock
);
1411 if (mtu
> 68 + sizeof(struct iphdr
))
1412 mtu
-= sizeof(struct iphdr
);
1415 /* Strip outer IP, ICMP and IPIP, go to IP header of
1418 __skb_pull(skb
, offset2
);
1419 skb_reset_network_header(skb
);
1420 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1421 &ip_hdr(skb
)->saddr
, &ip_hdr(skb
)->daddr
,
1422 ic
->type
, ic
->code
, ntohl(info
));
1423 icmp_send(skb
, ic
->type
, ic
->code
, info
);
1424 /* ICMP can be shorter but anyways, account it */
1425 ip_vs_out_stats(cp
, skb
);
1429 verdict
= NF_STOLEN
;
1433 /* do the statistics and put it back */
1434 ip_vs_in_stats(cp
, skb
);
1435 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
)
1436 offset
+= 2 * sizeof(__u16
);
1437 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
, hooknum
, &ciph
);
1440 __ip_vs_conn_put(cp
);
1445 #ifdef CONFIG_IP_VS_IPV6
1446 static int ip_vs_in_icmp_v6(struct sk_buff
*skb
, int *related
,
1447 unsigned int hooknum
, struct ip_vs_iphdr
*iph
)
1449 struct net
*net
= NULL
;
1450 struct ipv6hdr _ip6h
, *ip6h
;
1451 struct icmp6hdr _icmph
, *ic
;
1452 struct ip_vs_iphdr ciph
= {.flags
= 0, .fragoffs
= 0};/*Contained IP */
1453 struct ip_vs_conn
*cp
;
1454 struct ip_vs_protocol
*pp
;
1455 struct ip_vs_proto_data
*pd
;
1456 unsigned int offs_ciph
, writable
, verdict
;
1460 ic
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_icmph
), &_icmph
, iph
);
1465 * Work through seeing if this is for us.
1466 * These checks are supposed to be in an order that means easy
1467 * things are checked first to speed up processing.... however
1468 * this means that some packets will manage to get a long way
1469 * down this stack and then be rejected, but that's life.
1471 if (ic
->icmp6_type
& ICMPV6_INFOMSG_MASK
) {
1475 /* Fragment header that is before ICMP header tells us that:
1476 * it's not an error message since they can't be fragmented.
1478 if (iph
->flags
& IP6_FH_F_FRAG
)
1481 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1482 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1483 &iph
->saddr
, &iph
->daddr
);
1485 /* Now find the contained IP header */
1486 ciph
.len
= iph
->len
+ sizeof(_icmph
);
1487 offs_ciph
= ciph
.len
; /* Save ip header offset */
1488 ip6h
= skb_header_pointer(skb
, ciph
.len
, sizeof(_ip6h
), &_ip6h
);
1490 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1491 ciph
.saddr
.in6
= ip6h
->saddr
; /* conn_in_get() handles reverse order */
1492 ciph
.daddr
.in6
= ip6h
->daddr
;
1493 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1494 ciph
.protocol
= ipv6_find_hdr(skb
, &ciph
.len
, -1, &ciph
.fragoffs
, NULL
);
1495 if (ciph
.protocol
< 0)
1496 return NF_ACCEPT
; /* Contained IPv6 hdr looks wrong, ignore */
1499 pd
= ip_vs_proto_data_get(net
, ciph
.protocol
);
1504 /* Cannot handle fragmented embedded protocol */
1508 IP_VS_DBG_PKT(11, AF_INET6
, pp
, skb
, offs_ciph
,
1509 "Checking incoming ICMPv6 for");
1511 /* The embedded headers contain source and dest in reverse order
1512 * if not from localhost
1514 cp
= pp
->conn_in_get(AF_INET6
, skb
, &ciph
,
1515 (hooknum
== NF_INET_LOCAL_OUT
) ? 0 : 1);
1519 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1520 if ((hooknum
== NF_INET_LOCAL_OUT
) &&
1521 (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)) {
1522 __ip_vs_conn_put(cp
);
1526 /* do the statistics and put it back */
1527 ip_vs_in_stats(cp
, skb
);
1529 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1530 writable
= ciph
.len
;
1531 if (IPPROTO_TCP
== ciph
.protocol
|| IPPROTO_UDP
== ciph
.protocol
||
1532 IPPROTO_SCTP
== ciph
.protocol
)
1533 writable
+= 2 * sizeof(__u16
); /* Also mangle ports */
1535 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, writable
, hooknum
, &ciph
);
1537 __ip_vs_conn_put(cp
);
1545 * Check if it's for virtual services, look it up,
1546 * and send it on its way...
1549 ip_vs_in(unsigned int hooknum
, struct sk_buff
*skb
, int af
)
1552 struct ip_vs_iphdr iph
;
1553 struct ip_vs_protocol
*pp
;
1554 struct ip_vs_proto_data
*pd
;
1555 struct ip_vs_conn
*cp
;
1557 struct netns_ipvs
*ipvs
;
1559 /* Already marked as IPVS request or reply? */
1560 if (skb
->ipvs_property
)
1565 * - remote client: only PACKET_HOST
1566 * - route: used for struct net when skb->dev is unset
1568 if (unlikely((skb
->pkt_type
!= PACKET_HOST
&&
1569 hooknum
!= NF_INET_LOCAL_OUT
) ||
1571 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1572 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1573 " ignored in hook %u\n",
1574 skb
->pkt_type
, iph
.protocol
,
1575 IP_VS_DBG_ADDR(af
, &iph
.daddr
), hooknum
);
1578 /* ipvs enabled in this netns ? */
1580 if (!net_ipvs(net
)->enable
)
1583 ip_vs_fill_iph_skb(af
, skb
, &iph
);
1585 /* Bad... Do not break raw sockets */
1586 if (unlikely(skb
->sk
!= NULL
&& hooknum
== NF_INET_LOCAL_OUT
&&
1588 struct sock
*sk
= skb
->sk
;
1589 struct inet_sock
*inet
= inet_sk(skb
->sk
);
1591 if (inet
&& sk
->sk_family
== PF_INET
&& inet
->nodefrag
)
1595 #ifdef CONFIG_IP_VS_IPV6
1596 if (af
== AF_INET6
) {
1597 if (!iph
.fragoffs
&& skb_nfct_reasm(skb
)) {
1598 struct sk_buff
*reasm
= skb_nfct_reasm(skb
);
1599 /* Save fw mark for coming frags. */
1600 reasm
->ipvs_property
= 1;
1601 reasm
->mark
= skb
->mark
;
1603 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1605 int verdict
= ip_vs_in_icmp_v6(skb
, &related
, hooknum
,
1613 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1615 int verdict
= ip_vs_in_icmp(skb
, &related
, hooknum
);
1621 /* Protocol supported? */
1622 pd
= ip_vs_proto_data_get(net
, iph
.protocol
);
1627 * Check if the packet belongs to an existing connection entry
1629 cp
= pp
->conn_in_get(af
, skb
, &iph
, 0);
1630 if (unlikely(!cp
) && !iph
.fragoffs
) {
1631 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1632 * replayed fragment zero will already have created the cp
1636 /* Schedule and create new connection entry into &cp */
1637 if (!pp
->conn_schedule(af
, skb
, pd
, &v
, &cp
, &iph
))
1641 if (unlikely(!cp
)) {
1642 /* sorry, all this trouble for a no-hit :) */
1643 IP_VS_DBG_PKT(12, af
, pp
, skb
, 0,
1644 "ip_vs_in: packet continues traversal as normal");
1645 if (iph
.fragoffs
&& !skb_nfct_reasm(skb
)) {
1646 /* Fragment that couldn't be mapped to a conn entry
1647 * and don't have any pointer to a reasm skb
1648 * is missing module nf_defrag_ipv6
1650 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1651 IP_VS_DBG_PKT(7, af
, pp
, skb
, 0, "unhandled fragment");
1656 IP_VS_DBG_PKT(11, af
, pp
, skb
, 0, "Incoming packet");
1657 ipvs
= net_ipvs(net
);
1658 /* Check the server status */
1659 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1660 /* the destination server is not available */
1662 if (sysctl_expire_nodest_conn(ipvs
)) {
1663 /* try to expire the connection immediately */
1664 ip_vs_conn_expire_now(cp
);
1666 /* don't restart its timer, and silently
1668 __ip_vs_conn_put(cp
);
1672 ip_vs_in_stats(cp
, skb
);
1673 ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pd
);
1674 if (cp
->packet_xmit
)
1675 ret
= cp
->packet_xmit(skb
, cp
, pp
, &iph
);
1676 /* do not touch skb anymore */
1678 IP_VS_DBG_RL("warning: packet_xmit is null");
1682 /* Increase its packet counter and check if it is needed
1683 * to be synchronized
1685 * Sync connection if it is about to close to
1686 * encorage the standby servers to update the connections timeout
1688 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1691 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
1692 pkts
= sysctl_sync_threshold(ipvs
);
1694 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1696 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
1697 ip_vs_sync_conn(net
, cp
, pkts
);
1704 * AF_INET handler in NF_INET_LOCAL_IN chain
1705 * Schedule and forward packets from remote clients
1708 ip_vs_remote_request4(unsigned int hooknum
, struct sk_buff
*skb
,
1709 const struct net_device
*in
,
1710 const struct net_device
*out
,
1711 int (*okfn
)(struct sk_buff
*))
1713 return ip_vs_in(hooknum
, skb
, AF_INET
);
1717 * AF_INET handler in NF_INET_LOCAL_OUT chain
1718 * Schedule and forward packets from local clients
1721 ip_vs_local_request4(unsigned int hooknum
, struct sk_buff
*skb
,
1722 const struct net_device
*in
, const struct net_device
*out
,
1723 int (*okfn
)(struct sk_buff
*))
1725 unsigned int verdict
;
1727 /* Disable BH in LOCAL_OUT until all places are fixed */
1729 verdict
= ip_vs_in(hooknum
, skb
, AF_INET
);
1734 #ifdef CONFIG_IP_VS_IPV6
1737 * AF_INET6 fragment handling
1738 * Copy info from first fragment, to the rest of them.
1741 ip_vs_preroute_frag6(unsigned int hooknum
, struct sk_buff
*skb
,
1742 const struct net_device
*in
,
1743 const struct net_device
*out
,
1744 int (*okfn
)(struct sk_buff
*))
1746 struct sk_buff
*reasm
= skb_nfct_reasm(skb
);
1749 /* Skip if not a "replay" from nf_ct_frag6_output or first fragment.
1750 * ipvs_property is set when checking first fragment
1751 * in ip_vs_in() and ip_vs_out().
1754 IP_VS_DBG(2, "Fragment recv prop:%d\n", reasm
->ipvs_property
);
1755 if (!reasm
|| !reasm
->ipvs_property
)
1759 if (!net_ipvs(net
)->enable
)
1762 /* Copy stored fw mark, saved in ip_vs_{in,out} */
1763 skb
->mark
= reasm
->mark
;
1769 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1770 * Schedule and forward packets from remote clients
1773 ip_vs_remote_request6(unsigned int hooknum
, struct sk_buff
*skb
,
1774 const struct net_device
*in
,
1775 const struct net_device
*out
,
1776 int (*okfn
)(struct sk_buff
*))
1778 return ip_vs_in(hooknum
, skb
, AF_INET6
);
1782 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1783 * Schedule and forward packets from local clients
1786 ip_vs_local_request6(unsigned int hooknum
, struct sk_buff
*skb
,
1787 const struct net_device
*in
, const struct net_device
*out
,
1788 int (*okfn
)(struct sk_buff
*))
1790 unsigned int verdict
;
1792 /* Disable BH in LOCAL_OUT until all places are fixed */
1794 verdict
= ip_vs_in(hooknum
, skb
, AF_INET6
);
1803 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1804 * related packets destined for 0.0.0.0/0.
1805 * When fwmark-based virtual service is used, such as transparent
1806 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1807 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1808 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1809 * and send them to ip_vs_in_icmp.
1812 ip_vs_forward_icmp(unsigned int hooknum
, struct sk_buff
*skb
,
1813 const struct net_device
*in
, const struct net_device
*out
,
1814 int (*okfn
)(struct sk_buff
*))
1819 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1822 /* ipvs enabled in this netns ? */
1824 if (!net_ipvs(net
)->enable
)
1827 return ip_vs_in_icmp(skb
, &r
, hooknum
);
1830 #ifdef CONFIG_IP_VS_IPV6
1832 ip_vs_forward_icmp_v6(unsigned int hooknum
, struct sk_buff
*skb
,
1833 const struct net_device
*in
, const struct net_device
*out
,
1834 int (*okfn
)(struct sk_buff
*))
1838 struct ip_vs_iphdr iphdr
;
1840 ip_vs_fill_iph_skb(AF_INET6
, skb
, &iphdr
);
1841 if (iphdr
.protocol
!= IPPROTO_ICMPV6
)
1844 /* ipvs enabled in this netns ? */
1846 if (!net_ipvs(net
)->enable
)
1849 return ip_vs_in_icmp_v6(skb
, &r
, hooknum
, &iphdr
);
1854 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1855 /* After packet filtering, change source only for VS/NAT */
1857 .hook
= ip_vs_reply4
,
1858 .owner
= THIS_MODULE
,
1860 .hooknum
= NF_INET_LOCAL_IN
,
1861 .priority
= NF_IP_PRI_NAT_SRC
- 2,
1863 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1864 * or VS/NAT(change destination), so that filtering rules can be
1865 * applied to IPVS. */
1867 .hook
= ip_vs_remote_request4
,
1868 .owner
= THIS_MODULE
,
1870 .hooknum
= NF_INET_LOCAL_IN
,
1871 .priority
= NF_IP_PRI_NAT_SRC
- 1,
1873 /* Before ip_vs_in, change source only for VS/NAT */
1875 .hook
= ip_vs_local_reply4
,
1876 .owner
= THIS_MODULE
,
1878 .hooknum
= NF_INET_LOCAL_OUT
,
1879 .priority
= NF_IP_PRI_NAT_DST
+ 1,
1881 /* After mangle, schedule and forward local requests */
1883 .hook
= ip_vs_local_request4
,
1884 .owner
= THIS_MODULE
,
1886 .hooknum
= NF_INET_LOCAL_OUT
,
1887 .priority
= NF_IP_PRI_NAT_DST
+ 2,
1889 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1890 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1892 .hook
= ip_vs_forward_icmp
,
1893 .owner
= THIS_MODULE
,
1895 .hooknum
= NF_INET_FORWARD
,
1898 /* After packet filtering, change source only for VS/NAT */
1900 .hook
= ip_vs_reply4
,
1901 .owner
= THIS_MODULE
,
1903 .hooknum
= NF_INET_FORWARD
,
1906 #ifdef CONFIG_IP_VS_IPV6
1907 /* After mangle & nat fetch 2:nd fragment and following */
1909 .hook
= ip_vs_preroute_frag6
,
1910 .owner
= THIS_MODULE
,
1912 .hooknum
= NF_INET_PRE_ROUTING
,
1913 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
1915 /* After packet filtering, change source only for VS/NAT */
1917 .hook
= ip_vs_reply6
,
1918 .owner
= THIS_MODULE
,
1920 .hooknum
= NF_INET_LOCAL_IN
,
1921 .priority
= NF_IP6_PRI_NAT_SRC
- 2,
1923 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1924 * or VS/NAT(change destination), so that filtering rules can be
1925 * applied to IPVS. */
1927 .hook
= ip_vs_remote_request6
,
1928 .owner
= THIS_MODULE
,
1930 .hooknum
= NF_INET_LOCAL_IN
,
1931 .priority
= NF_IP6_PRI_NAT_SRC
- 1,
1933 /* Before ip_vs_in, change source only for VS/NAT */
1935 .hook
= ip_vs_local_reply6
,
1936 .owner
= THIS_MODULE
,
1938 .hooknum
= NF_INET_LOCAL_OUT
,
1939 .priority
= NF_IP6_PRI_NAT_DST
+ 1,
1941 /* After mangle, schedule and forward local requests */
1943 .hook
= ip_vs_local_request6
,
1944 .owner
= THIS_MODULE
,
1946 .hooknum
= NF_INET_LOCAL_OUT
,
1947 .priority
= NF_IP6_PRI_NAT_DST
+ 2,
1949 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1950 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1952 .hook
= ip_vs_forward_icmp_v6
,
1953 .owner
= THIS_MODULE
,
1955 .hooknum
= NF_INET_FORWARD
,
1958 /* After packet filtering, change source only for VS/NAT */
1960 .hook
= ip_vs_reply6
,
1961 .owner
= THIS_MODULE
,
1963 .hooknum
= NF_INET_FORWARD
,
1969 * Initialize IP Virtual Server netns mem.
1971 static int __net_init
__ip_vs_init(struct net
*net
)
1973 struct netns_ipvs
*ipvs
;
1975 ipvs
= net_generic(net
, ip_vs_net_id
);
1979 /* Hold the beast until a service is registerd */
1982 /* Counters used for creating unique names */
1983 ipvs
->gen
= atomic_read(&ipvs_netns_cnt
);
1984 atomic_inc(&ipvs_netns_cnt
);
1987 if (ip_vs_estimator_net_init(net
) < 0)
1988 goto estimator_fail
;
1990 if (ip_vs_control_net_init(net
) < 0)
1993 if (ip_vs_protocol_net_init(net
) < 0)
1996 if (ip_vs_app_net_init(net
) < 0)
1999 if (ip_vs_conn_net_init(net
) < 0)
2002 if (ip_vs_sync_net_init(net
) < 0)
2005 printk(KERN_INFO
"IPVS: Creating netns size=%zu id=%d\n",
2006 sizeof(struct netns_ipvs
), ipvs
->gen
);
2013 ip_vs_conn_net_cleanup(net
);
2015 ip_vs_app_net_cleanup(net
);
2017 ip_vs_protocol_net_cleanup(net
);
2019 ip_vs_control_net_cleanup(net
);
2021 ip_vs_estimator_net_cleanup(net
);
2027 static void __net_exit
__ip_vs_cleanup(struct net
*net
)
2029 ip_vs_service_net_cleanup(net
); /* ip_vs_flush() with locks */
2030 ip_vs_conn_net_cleanup(net
);
2031 ip_vs_app_net_cleanup(net
);
2032 ip_vs_protocol_net_cleanup(net
);
2033 ip_vs_control_net_cleanup(net
);
2034 ip_vs_estimator_net_cleanup(net
);
2035 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net
)->gen
);
2039 static void __net_exit
__ip_vs_dev_cleanup(struct net
*net
)
2042 net_ipvs(net
)->enable
= 0; /* Disable packet reception */
2044 ip_vs_sync_net_cleanup(net
);
2048 static struct pernet_operations ipvs_core_ops
= {
2049 .init
= __ip_vs_init
,
2050 .exit
= __ip_vs_cleanup
,
2051 .id
= &ip_vs_net_id
,
2052 .size
= sizeof(struct netns_ipvs
),
2055 static struct pernet_operations ipvs_core_dev_ops
= {
2056 .exit
= __ip_vs_dev_cleanup
,
2060 * Initialize IP Virtual Server
2062 static int __init
ip_vs_init(void)
2066 ret
= ip_vs_control_init();
2068 pr_err("can't setup control.\n");
2072 ip_vs_protocol_init();
2074 ret
= ip_vs_conn_init();
2076 pr_err("can't setup connection table.\n");
2077 goto cleanup_protocol
;
2080 ret
= register_pernet_subsys(&ipvs_core_ops
); /* Alloc ip_vs struct */
2084 ret
= register_pernet_device(&ipvs_core_dev_ops
);
2088 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2090 pr_err("can't register hooks.\n");
2094 ret
= ip_vs_register_nl_ioctl();
2096 pr_err("can't register netlink/ioctl.\n");
2100 pr_info("ipvs loaded.\n");
2105 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2107 unregister_pernet_device(&ipvs_core_dev_ops
);
2109 unregister_pernet_subsys(&ipvs_core_ops
);
2111 ip_vs_conn_cleanup();
2113 ip_vs_protocol_cleanup();
2114 ip_vs_control_cleanup();
2119 static void __exit
ip_vs_cleanup(void)
2121 ip_vs_unregister_nl_ioctl();
2122 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
2123 unregister_pernet_device(&ipvs_core_dev_ops
);
2124 unregister_pernet_subsys(&ipvs_core_ops
); /* free ip_vs struct */
2125 ip_vs_conn_cleanup();
2126 ip_vs_protocol_cleanup();
2127 ip_vs_control_cleanup();
2128 pr_info("ipvs unloaded.\n");
2131 module_init(ip_vs_init
);
2132 module_exit(ip_vs_cleanup
);
2133 MODULE_LICENSE("GPL");