ipvs: Wrap sysctl_cache_bypass and remove ifdefs in ip_vs_leave
[deliverable/linux.git] / net / netfilter / ipvs / ip_vs_core.c
1 /*
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
6 * cluster of servers.
7 *
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
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.
16 *
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
19 * and others.
20 *
21 * Changes:
22 * Paul `Rusty' Russell properly handle non-linear skbs
23 * Harald Welte don't use nfcache
24 *
25 */
26
27 #define KMSG_COMPONENT "IPVS"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/ip.h>
33 #include <linux/tcp.h>
34 #include <linux/sctp.h>
35 #include <linux/icmp.h>
36 #include <linux/slab.h>
37
38 #include <net/ip.h>
39 #include <net/tcp.h>
40 #include <net/udp.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() */
45
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv4.h>
48
49 #ifdef CONFIG_IP_VS_IPV6
50 #include <net/ipv6.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_route.h>
53 #endif
54
55 #include <net/ip_vs.h>
56
57
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);
66 #endif
67 EXPORT_SYMBOL(ip_vs_conn_put);
68 #ifdef CONFIG_IP_VS_DEBUG
69 EXPORT_SYMBOL(ip_vs_get_debug_level);
70 #endif
71
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);
75
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)
79
80 const char *ip_vs_proto_name(unsigned int proto)
81 {
82 static char buf[20];
83
84 switch (proto) {
85 case IPPROTO_IP:
86 return "IP";
87 case IPPROTO_UDP:
88 return "UDP";
89 case IPPROTO_TCP:
90 return "TCP";
91 case IPPROTO_SCTP:
92 return "SCTP";
93 case IPPROTO_ICMP:
94 return "ICMP";
95 #ifdef CONFIG_IP_VS_IPV6
96 case IPPROTO_ICMPV6:
97 return "ICMPv6";
98 #endif
99 default:
100 sprintf(buf, "IP_%u", proto);
101 return buf;
102 }
103 }
104
105 void ip_vs_init_hash_table(struct list_head *table, int rows)
106 {
107 while (--rows >= 0)
108 INIT_LIST_HEAD(&table[rows]);
109 }
110
111 static inline void
112 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
113 {
114 struct ip_vs_dest *dest = cp->dest;
115 struct netns_ipvs *ipvs = cp->ipvs;
116
117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
118 struct ip_vs_cpu_stats *s;
119 struct ip_vs_service *svc;
120
121 s = this_cpu_ptr(dest->stats.cpustats);
122 u64_stats_update_begin(&s->syncp);
123 s->cnt.inpkts++;
124 s->cnt.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp);
126
127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
130 u64_stats_update_begin(&s->syncp);
131 s->cnt.inpkts++;
132 s->cnt.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
134 rcu_read_unlock();
135
136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
137 u64_stats_update_begin(&s->syncp);
138 s->cnt.inpkts++;
139 s->cnt.inbytes += skb->len;
140 u64_stats_update_end(&s->syncp);
141 }
142 }
143
144
145 static inline void
146 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147 {
148 struct ip_vs_dest *dest = cp->dest;
149 struct netns_ipvs *ipvs = cp->ipvs;
150
151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
152 struct ip_vs_cpu_stats *s;
153 struct ip_vs_service *svc;
154
155 s = this_cpu_ptr(dest->stats.cpustats);
156 u64_stats_update_begin(&s->syncp);
157 s->cnt.outpkts++;
158 s->cnt.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp);
160
161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
164 u64_stats_update_begin(&s->syncp);
165 s->cnt.outpkts++;
166 s->cnt.outbytes += skb->len;
167 u64_stats_update_end(&s->syncp);
168 rcu_read_unlock();
169
170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
171 u64_stats_update_begin(&s->syncp);
172 s->cnt.outpkts++;
173 s->cnt.outbytes += skb->len;
174 u64_stats_update_end(&s->syncp);
175 }
176 }
177
178
179 static inline void
180 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
181 {
182 struct netns_ipvs *ipvs = svc->ipvs;
183 struct ip_vs_cpu_stats *s;
184
185 s = this_cpu_ptr(cp->dest->stats.cpustats);
186 u64_stats_update_begin(&s->syncp);
187 s->cnt.conns++;
188 u64_stats_update_end(&s->syncp);
189
190 s = this_cpu_ptr(svc->stats.cpustats);
191 u64_stats_update_begin(&s->syncp);
192 s->cnt.conns++;
193 u64_stats_update_end(&s->syncp);
194
195 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
196 u64_stats_update_begin(&s->syncp);
197 s->cnt.conns++;
198 u64_stats_update_end(&s->syncp);
199 }
200
201
202 static inline void
203 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
204 const struct sk_buff *skb,
205 struct ip_vs_proto_data *pd)
206 {
207 if (likely(pd->pp->state_transition))
208 pd->pp->state_transition(cp, direction, skb, pd);
209 }
210
211 static inline int
212 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
213 struct sk_buff *skb, int protocol,
214 const union nf_inet_addr *caddr, __be16 cport,
215 const union nf_inet_addr *vaddr, __be16 vport,
216 struct ip_vs_conn_param *p)
217 {
218 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
219 vport, p);
220 p->pe = rcu_dereference(svc->pe);
221 if (p->pe && p->pe->fill_param)
222 return p->pe->fill_param(p, skb);
223
224 return 0;
225 }
226
227 /*
228 * IPVS persistent scheduling function
229 * It creates a connection entry according to its template if exists,
230 * or selects a server and creates a connection entry plus a template.
231 * Locking: we are svc user (svc->refcnt), so we hold all dests too
232 * Protocols supported: TCP, UDP
233 */
234 static struct ip_vs_conn *
235 ip_vs_sched_persist(struct ip_vs_service *svc,
236 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
237 int *ignored, struct ip_vs_iphdr *iph)
238 {
239 struct ip_vs_conn *cp = NULL;
240 struct ip_vs_dest *dest;
241 struct ip_vs_conn *ct;
242 __be16 dport = 0; /* destination port to forward */
243 unsigned int flags;
244 struct ip_vs_conn_param param;
245 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
246 union nf_inet_addr snet; /* source network of the client,
247 after masking */
248 const union nf_inet_addr *src_addr, *dst_addr;
249
250 if (likely(!ip_vs_iph_inverse(iph))) {
251 src_addr = &iph->saddr;
252 dst_addr = &iph->daddr;
253 } else {
254 src_addr = &iph->daddr;
255 dst_addr = &iph->saddr;
256 }
257
258
259 /* Mask saddr with the netmask to adjust template granularity */
260 #ifdef CONFIG_IP_VS_IPV6
261 if (svc->af == AF_INET6)
262 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
263 (__force __u32) svc->netmask);
264 else
265 #endif
266 snet.ip = src_addr->ip & svc->netmask;
267
268 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
269 "mnet %s\n",
270 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
271 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
272 IP_VS_DBG_ADDR(svc->af, &snet));
273
274 /*
275 * As far as we know, FTP is a very complicated network protocol, and
276 * it uses control connection and data connections. For active FTP,
277 * FTP server initialize data connection to the client, its source port
278 * is often 20. For passive FTP, FTP server tells the clients the port
279 * that it passively listens to, and the client issues the data
280 * connection. In the tunneling or direct routing mode, the load
281 * balancer is on the client-to-server half of connection, the port
282 * number is unknown to the load balancer. So, a conn template like
283 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
284 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
285 * is created for other persistent services.
286 */
287 {
288 int protocol = iph->protocol;
289 const union nf_inet_addr *vaddr = dst_addr;
290 __be16 vport = 0;
291
292 if (dst_port == svc->port) {
293 /* non-FTP template:
294 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
295 * FTP template:
296 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
297 */
298 if (svc->port != FTPPORT)
299 vport = dst_port;
300 } else {
301 /* Note: persistent fwmark-based services and
302 * persistent port zero service are handled here.
303 * fwmark template:
304 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
305 * port zero template:
306 * <protocol,caddr,0,vaddr,0,daddr,0>
307 */
308 if (svc->fwmark) {
309 protocol = IPPROTO_IP;
310 vaddr = &fwmark;
311 }
312 }
313 /* return *ignored = -1 so NF_DROP can be used */
314 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
315 vaddr, vport, &param) < 0) {
316 *ignored = -1;
317 return NULL;
318 }
319 }
320
321 /* Check if a template already exists */
322 ct = ip_vs_ct_in_get(&param);
323 if (!ct || !ip_vs_check_template(ct)) {
324 struct ip_vs_scheduler *sched;
325
326 /*
327 * No template found or the dest of the connection
328 * template is not available.
329 * return *ignored=0 i.e. ICMP and NF_DROP
330 */
331 sched = rcu_dereference(svc->scheduler);
332 if (sched) {
333 /* read svc->sched_data after svc->scheduler */
334 smp_rmb();
335 dest = sched->schedule(svc, skb, iph);
336 } else {
337 dest = NULL;
338 }
339 if (!dest) {
340 IP_VS_DBG(1, "p-schedule: no dest found.\n");
341 kfree(param.pe_data);
342 *ignored = 0;
343 return NULL;
344 }
345
346 if (dst_port == svc->port && svc->port != FTPPORT)
347 dport = dest->port;
348
349 /* Create a template
350 * This adds param.pe_data to the template,
351 * and thus param.pe_data will be destroyed
352 * when the template expires */
353 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
354 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
355 if (ct == NULL) {
356 kfree(param.pe_data);
357 *ignored = -1;
358 return NULL;
359 }
360
361 ct->timeout = svc->timeout;
362 } else {
363 /* set destination with the found template */
364 dest = ct->dest;
365 kfree(param.pe_data);
366 }
367
368 dport = dst_port;
369 if (dport == svc->port && dest->port)
370 dport = dest->port;
371
372 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
373 && iph->protocol == IPPROTO_UDP) ?
374 IP_VS_CONN_F_ONE_PACKET : 0;
375
376 /*
377 * Create a new connection according to the template
378 */
379 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
380 src_port, dst_addr, dst_port, &param);
381
382 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
383 skb->mark);
384 if (cp == NULL) {
385 ip_vs_conn_put(ct);
386 *ignored = -1;
387 return NULL;
388 }
389
390 /*
391 * Add its control
392 */
393 ip_vs_control_add(cp, ct);
394 ip_vs_conn_put(ct);
395
396 ip_vs_conn_stats(cp, svc);
397 return cp;
398 }
399
400
401 /*
402 * IPVS main scheduling function
403 * It selects a server according to the virtual service, and
404 * creates a connection entry.
405 * Protocols supported: TCP, UDP
406 *
407 * Usage of *ignored
408 *
409 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
410 * svc/scheduler decides that this packet should be accepted with
411 * NF_ACCEPT because it must not be scheduled.
412 *
413 * 0 : scheduler can not find destination, so try bypass or
414 * return ICMP and then NF_DROP (ip_vs_leave).
415 *
416 * -1 : scheduler tried to schedule but fatal error occurred, eg.
417 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
418 * failure such as missing Call-ID, ENOMEM on skb_linearize
419 * or pe_data. In this case we should return NF_DROP without
420 * any attempts to send ICMP with ip_vs_leave.
421 */
422 struct ip_vs_conn *
423 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
424 struct ip_vs_proto_data *pd, int *ignored,
425 struct ip_vs_iphdr *iph)
426 {
427 struct ip_vs_protocol *pp = pd->pp;
428 struct ip_vs_conn *cp = NULL;
429 struct ip_vs_scheduler *sched;
430 struct ip_vs_dest *dest;
431 __be16 _ports[2], *pptr, cport, vport;
432 const void *caddr, *vaddr;
433 unsigned int flags;
434
435 *ignored = 1;
436 /*
437 * IPv6 frags, only the first hit here.
438 */
439 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
440 if (pptr == NULL)
441 return NULL;
442
443 if (likely(!ip_vs_iph_inverse(iph))) {
444 cport = pptr[0];
445 caddr = &iph->saddr;
446 vport = pptr[1];
447 vaddr = &iph->daddr;
448 } else {
449 cport = pptr[1];
450 caddr = &iph->daddr;
451 vport = pptr[0];
452 vaddr = &iph->saddr;
453 }
454
455 /*
456 * FTPDATA needs this check when using local real server.
457 * Never schedule Active FTPDATA connections from real server.
458 * For LVS-NAT they must be already created. For other methods
459 * with persistence the connection is created on SYN+ACK.
460 */
461 if (cport == FTPDATA) {
462 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
463 "Not scheduling FTPDATA");
464 return NULL;
465 }
466
467 /*
468 * Do not schedule replies from local real server.
469 */
470 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
471 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
472 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
473 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
474
475 if (cp) {
476 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
477 "Not scheduling reply for existing"
478 " connection");
479 __ip_vs_conn_put(cp);
480 return NULL;
481 }
482 }
483
484 /*
485 * Persistent service
486 */
487 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
488 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
489 iph);
490
491 *ignored = 0;
492
493 /*
494 * Non-persistent service
495 */
496 if (!svc->fwmark && vport != svc->port) {
497 if (!svc->port)
498 pr_err("Schedule: port zero only supported "
499 "in persistent services, "
500 "check your ipvs configuration\n");
501 return NULL;
502 }
503
504 sched = rcu_dereference(svc->scheduler);
505 if (sched) {
506 /* read svc->sched_data after svc->scheduler */
507 smp_rmb();
508 dest = sched->schedule(svc, skb, iph);
509 } else {
510 dest = NULL;
511 }
512 if (dest == NULL) {
513 IP_VS_DBG(1, "Schedule: no dest found.\n");
514 return NULL;
515 }
516
517 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
518 && iph->protocol == IPPROTO_UDP) ?
519 IP_VS_CONN_F_ONE_PACKET : 0;
520
521 /*
522 * Create a connection entry.
523 */
524 {
525 struct ip_vs_conn_param p;
526
527 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
528 caddr, cport, vaddr, vport, &p);
529 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
530 dest->port ? dest->port : vport,
531 flags, dest, skb->mark);
532 if (!cp) {
533 *ignored = -1;
534 return NULL;
535 }
536 }
537
538 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
539 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
540 ip_vs_fwd_tag(cp),
541 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
542 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
543 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
544 cp->flags, atomic_read(&cp->refcnt));
545
546 ip_vs_conn_stats(cp, svc);
547 return cp;
548 }
549
550 #ifdef CONFIG_SYSCTL
551 static inline int ip_vs_addr_is_unicast(struct net *net, int af,
552 union nf_inet_addr *addr)
553 {
554 #ifdef CONFIG_IP_VS_IPV6
555 if (af == AF_INET6)
556 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
557 #endif
558 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
559 }
560 #endif
561
562 /*
563 * Pass or drop the packet.
564 * Called by ip_vs_in, when the virtual service is available but
565 * no destination is available for a new connection.
566 */
567 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
568 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
569 {
570 __be16 _ports[2], *pptr, dport;
571 struct net *net;
572 struct netns_ipvs *ipvs;
573
574 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
575 if (!pptr)
576 return NF_DROP;
577 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
578
579 net = skb_net(skb);
580
581
582 /* if it is fwmark-based service, the cache_bypass sysctl is up
583 and the destination is a non-local unicast, then create
584 a cache_bypass connection entry */
585 ipvs = net_ipvs(net);
586 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
587 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
588 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
589 int ret;
590 struct ip_vs_conn *cp;
591 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
592 iph->protocol == IPPROTO_UDP) ?
593 IP_VS_CONN_F_ONE_PACKET : 0;
594 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
595
596 /* create a new connection entry */
597 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
598 {
599 struct ip_vs_conn_param p;
600 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
601 &iph->saddr, pptr[0],
602 &iph->daddr, pptr[1], &p);
603 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
604 IP_VS_CONN_F_BYPASS | flags,
605 NULL, skb->mark);
606 if (!cp)
607 return NF_DROP;
608 }
609
610 /* statistics */
611 ip_vs_in_stats(cp, skb);
612
613 /* set state */
614 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
615
616 /* transmit the first SYN packet */
617 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
618 /* do not touch skb anymore */
619
620 atomic_inc(&cp->in_pkts);
621 ip_vs_conn_put(cp);
622 return ret;
623 }
624
625 /*
626 * When the virtual ftp service is presented, packets destined
627 * for other services on the VIP may get here (except services
628 * listed in the ipvs table), pass the packets, because it is
629 * not ipvs job to decide to drop the packets.
630 */
631 if (svc->port == FTPPORT && dport != FTPPORT)
632 return NF_ACCEPT;
633
634 if (unlikely(ip_vs_iph_icmp(iph)))
635 return NF_DROP;
636
637 /*
638 * Notify the client that the destination is unreachable, and
639 * release the socket buffer.
640 * Since it is in IP layer, the TCP socket is not actually
641 * created, the TCP RST packet cannot be sent, instead that
642 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
643 */
644 #ifdef CONFIG_IP_VS_IPV6
645 if (svc->af == AF_INET6) {
646 if (!skb->dev)
647 skb->dev = net->loopback_dev;
648 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
649 } else
650 #endif
651 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
652
653 return NF_DROP;
654 }
655
656 #ifdef CONFIG_SYSCTL
657
658 static int sysctl_snat_reroute(struct sk_buff *skb)
659 {
660 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
661 return ipvs->sysctl_snat_reroute;
662 }
663
664 static int sysctl_nat_icmp_send(struct net *net)
665 {
666 struct netns_ipvs *ipvs = net_ipvs(net);
667 return ipvs->sysctl_nat_icmp_send;
668 }
669
670 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
671 {
672 return ipvs->sysctl_expire_nodest_conn;
673 }
674
675 #else
676
677 static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
678 static int sysctl_nat_icmp_send(struct net *net) { return 0; }
679 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
680
681 #endif
682
683 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
684 {
685 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
686 }
687
688 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
689 {
690 if (NF_INET_LOCAL_IN == hooknum)
691 return IP_DEFRAG_VS_IN;
692 if (NF_INET_FORWARD == hooknum)
693 return IP_DEFRAG_VS_FWD;
694 return IP_DEFRAG_VS_OUT;
695 }
696
697 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
698 {
699 int err;
700
701 local_bh_disable();
702 err = ip_defrag(skb, user);
703 local_bh_enable();
704 if (!err)
705 ip_send_check(ip_hdr(skb));
706
707 return err;
708 }
709
710 static int ip_vs_route_me_harder(int af, struct sk_buff *skb,
711 unsigned int hooknum)
712 {
713 if (!sysctl_snat_reroute(skb))
714 return 0;
715 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
716 if (NF_INET_LOCAL_IN == hooknum)
717 return 0;
718 #ifdef CONFIG_IP_VS_IPV6
719 if (af == AF_INET6) {
720 struct dst_entry *dst = skb_dst(skb);
721
722 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
723 ip6_route_me_harder(skb) != 0)
724 return 1;
725 } else
726 #endif
727 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
728 ip_route_me_harder(skb, RTN_LOCAL) != 0)
729 return 1;
730
731 return 0;
732 }
733
734 /*
735 * Packet has been made sufficiently writable in caller
736 * - inout: 1=in->out, 0=out->in
737 */
738 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
739 struct ip_vs_conn *cp, int inout)
740 {
741 struct iphdr *iph = ip_hdr(skb);
742 unsigned int icmp_offset = iph->ihl*4;
743 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
744 icmp_offset);
745 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
746
747 if (inout) {
748 iph->saddr = cp->vaddr.ip;
749 ip_send_check(iph);
750 ciph->daddr = cp->vaddr.ip;
751 ip_send_check(ciph);
752 } else {
753 iph->daddr = cp->daddr.ip;
754 ip_send_check(iph);
755 ciph->saddr = cp->daddr.ip;
756 ip_send_check(ciph);
757 }
758
759 /* the TCP/UDP/SCTP port */
760 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
761 IPPROTO_SCTP == ciph->protocol) {
762 __be16 *ports = (void *)ciph + ciph->ihl*4;
763
764 if (inout)
765 ports[1] = cp->vport;
766 else
767 ports[0] = cp->dport;
768 }
769
770 /* And finally the ICMP checksum */
771 icmph->checksum = 0;
772 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
773 skb->ip_summed = CHECKSUM_UNNECESSARY;
774
775 if (inout)
776 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
777 "Forwarding altered outgoing ICMP");
778 else
779 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
780 "Forwarding altered incoming ICMP");
781 }
782
783 #ifdef CONFIG_IP_VS_IPV6
784 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
785 struct ip_vs_conn *cp, int inout)
786 {
787 struct ipv6hdr *iph = ipv6_hdr(skb);
788 unsigned int icmp_offset = 0;
789 unsigned int offs = 0; /* header offset*/
790 int protocol;
791 struct icmp6hdr *icmph;
792 struct ipv6hdr *ciph;
793 unsigned short fragoffs;
794
795 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
796 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
797 offs = icmp_offset + sizeof(struct icmp6hdr);
798 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
799
800 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
801
802 if (inout) {
803 iph->saddr = cp->vaddr.in6;
804 ciph->daddr = cp->vaddr.in6;
805 } else {
806 iph->daddr = cp->daddr.in6;
807 ciph->saddr = cp->daddr.in6;
808 }
809
810 /* the TCP/UDP/SCTP port */
811 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
812 IPPROTO_SCTP == protocol)) {
813 __be16 *ports = (void *)(skb_network_header(skb) + offs);
814
815 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
816 ntohs(inout ? ports[1] : ports[0]),
817 ntohs(inout ? cp->vport : cp->dport));
818 if (inout)
819 ports[1] = cp->vport;
820 else
821 ports[0] = cp->dport;
822 }
823
824 /* And finally the ICMP checksum */
825 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
826 skb->len - icmp_offset,
827 IPPROTO_ICMPV6, 0);
828 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
829 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
830 skb->ip_summed = CHECKSUM_PARTIAL;
831
832 if (inout)
833 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
834 (void *)ciph - (void *)iph,
835 "Forwarding altered outgoing ICMPv6");
836 else
837 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
838 (void *)ciph - (void *)iph,
839 "Forwarding altered incoming ICMPv6");
840 }
841 #endif
842
843 /* Handle relevant response ICMP messages - forward to the right
844 * destination host.
845 */
846 static int handle_response_icmp(int af, struct sk_buff *skb,
847 union nf_inet_addr *snet,
848 __u8 protocol, struct ip_vs_conn *cp,
849 struct ip_vs_protocol *pp,
850 unsigned int offset, unsigned int ihl,
851 unsigned int hooknum)
852 {
853 unsigned int verdict = NF_DROP;
854
855 if (IP_VS_FWD_METHOD(cp) != 0) {
856 pr_err("shouldn't reach here, because the box is on the "
857 "half connection in the tun/dr module.\n");
858 }
859
860 /* Ensure the checksum is correct */
861 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
862 /* Failed checksum! */
863 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
864 IP_VS_DBG_ADDR(af, snet));
865 goto out;
866 }
867
868 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
869 IPPROTO_SCTP == protocol)
870 offset += 2 * sizeof(__u16);
871 if (!skb_make_writable(skb, offset))
872 goto out;
873
874 #ifdef CONFIG_IP_VS_IPV6
875 if (af == AF_INET6)
876 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
877 else
878 #endif
879 ip_vs_nat_icmp(skb, pp, cp, 1);
880
881 if (ip_vs_route_me_harder(af, skb, hooknum))
882 goto out;
883
884 /* do the statistics and put it back */
885 ip_vs_out_stats(cp, skb);
886
887 skb->ipvs_property = 1;
888 if (!(cp->flags & IP_VS_CONN_F_NFCT))
889 ip_vs_notrack(skb);
890 else
891 ip_vs_update_conntrack(skb, cp, 0);
892 verdict = NF_ACCEPT;
893
894 out:
895 __ip_vs_conn_put(cp);
896
897 return verdict;
898 }
899
900 /*
901 * Handle ICMP messages in the inside-to-outside direction (outgoing).
902 * Find any that might be relevant, check against existing connections.
903 * Currently handles error types - unreachable, quench, ttl exceeded.
904 */
905 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
906 unsigned int hooknum)
907 {
908 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
909 struct iphdr *iph;
910 struct icmphdr _icmph, *ic;
911 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
912 struct ip_vs_iphdr ciph;
913 struct ip_vs_conn *cp;
914 struct ip_vs_protocol *pp;
915 unsigned int offset, ihl;
916 union nf_inet_addr snet;
917
918 *related = 1;
919
920 /* reassemble IP fragments */
921 if (ip_is_fragment(ip_hdr(skb))) {
922 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
923 return NF_STOLEN;
924 }
925
926 iph = ip_hdr(skb);
927 offset = ihl = iph->ihl * 4;
928 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
929 if (ic == NULL)
930 return NF_DROP;
931
932 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
933 ic->type, ntohs(icmp_id(ic)),
934 &iph->saddr, &iph->daddr);
935
936 /*
937 * Work through seeing if this is for us.
938 * These checks are supposed to be in an order that means easy
939 * things are checked first to speed up processing.... however
940 * this means that some packets will manage to get a long way
941 * down this stack and then be rejected, but that's life.
942 */
943 if ((ic->type != ICMP_DEST_UNREACH) &&
944 (ic->type != ICMP_SOURCE_QUENCH) &&
945 (ic->type != ICMP_TIME_EXCEEDED)) {
946 *related = 0;
947 return NF_ACCEPT;
948 }
949
950 /* Now find the contained IP header */
951 offset += sizeof(_icmph);
952 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
953 if (cih == NULL)
954 return NF_ACCEPT; /* The packet looks wrong, ignore */
955
956 pp = ip_vs_proto_get(cih->protocol);
957 if (!pp)
958 return NF_ACCEPT;
959
960 /* Is the embedded protocol header present? */
961 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
962 pp->dont_defrag))
963 return NF_ACCEPT;
964
965 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
966 "Checking outgoing ICMP for");
967
968 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
969
970 /* The embedded headers contain source and dest in reverse order */
971 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
972 if (!cp)
973 return NF_ACCEPT;
974
975 snet.ip = iph->saddr;
976 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
977 pp, ciph.len, ihl, hooknum);
978 }
979
980 #ifdef CONFIG_IP_VS_IPV6
981 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
982 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
983 {
984 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
985 struct icmp6hdr _icmph, *ic;
986 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
987 struct ip_vs_conn *cp;
988 struct ip_vs_protocol *pp;
989 union nf_inet_addr snet;
990 unsigned int offset;
991
992 *related = 1;
993 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
994 if (ic == NULL)
995 return NF_DROP;
996
997 /*
998 * Work through seeing if this is for us.
999 * These checks are supposed to be in an order that means easy
1000 * things are checked first to speed up processing.... however
1001 * this means that some packets will manage to get a long way
1002 * down this stack and then be rejected, but that's life.
1003 */
1004 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1005 *related = 0;
1006 return NF_ACCEPT;
1007 }
1008 /* Fragment header that is before ICMP header tells us that:
1009 * it's not an error message since they can't be fragmented.
1010 */
1011 if (ipvsh->flags & IP6_FH_F_FRAG)
1012 return NF_DROP;
1013
1014 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1015 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1016 &ipvsh->saddr, &ipvsh->daddr);
1017
1018 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1019 true, &ciph))
1020 return NF_ACCEPT; /* The packet looks wrong, ignore */
1021
1022 pp = ip_vs_proto_get(ciph.protocol);
1023 if (!pp)
1024 return NF_ACCEPT;
1025
1026 /* The embedded headers contain source and dest in reverse order */
1027 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
1028 if (!cp)
1029 return NF_ACCEPT;
1030
1031 snet.in6 = ciph.saddr.in6;
1032 offset = ciph.len;
1033 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1034 pp, offset, sizeof(struct ipv6hdr),
1035 hooknum);
1036 }
1037 #endif
1038
1039 /*
1040 * Check if sctp chunc is ABORT chunk
1041 */
1042 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1043 {
1044 sctp_chunkhdr_t *sch, schunk;
1045 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1046 sizeof(schunk), &schunk);
1047 if (sch == NULL)
1048 return 0;
1049 if (sch->type == SCTP_CID_ABORT)
1050 return 1;
1051 return 0;
1052 }
1053
1054 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1055 {
1056 struct tcphdr _tcph, *th;
1057
1058 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1059 if (th == NULL)
1060 return 0;
1061 return th->rst;
1062 }
1063
1064 static inline bool is_new_conn(const struct sk_buff *skb,
1065 struct ip_vs_iphdr *iph)
1066 {
1067 switch (iph->protocol) {
1068 case IPPROTO_TCP: {
1069 struct tcphdr _tcph, *th;
1070
1071 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1072 if (th == NULL)
1073 return false;
1074 return th->syn;
1075 }
1076 case IPPROTO_SCTP: {
1077 sctp_chunkhdr_t *sch, schunk;
1078
1079 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1080 sizeof(schunk), &schunk);
1081 if (sch == NULL)
1082 return false;
1083 return sch->type == SCTP_CID_INIT;
1084 }
1085 default:
1086 return false;
1087 }
1088 }
1089
1090 static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1091 int conn_reuse_mode)
1092 {
1093 /* Controlled (FTP DATA or persistence)? */
1094 if (cp->control)
1095 return false;
1096
1097 switch (cp->protocol) {
1098 case IPPROTO_TCP:
1099 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1100 ((conn_reuse_mode & 2) &&
1101 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1102 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1103 case IPPROTO_SCTP:
1104 return cp->state == IP_VS_SCTP_S_CLOSED;
1105 default:
1106 return false;
1107 }
1108 }
1109
1110 /* Handle response packets: rewrite addresses and send away...
1111 */
1112 static unsigned int
1113 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1114 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1115 unsigned int hooknum)
1116 {
1117 struct ip_vs_protocol *pp = pd->pp;
1118
1119 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
1120
1121 if (!skb_make_writable(skb, iph->len))
1122 goto drop;
1123
1124 /* mangle the packet */
1125 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1126 goto drop;
1127
1128 #ifdef CONFIG_IP_VS_IPV6
1129 if (af == AF_INET6)
1130 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1131 else
1132 #endif
1133 {
1134 ip_hdr(skb)->saddr = cp->vaddr.ip;
1135 ip_send_check(ip_hdr(skb));
1136 }
1137
1138 /*
1139 * nf_iterate does not expect change in the skb->dst->dev.
1140 * It looks like it is not fatal to enable this code for hooks
1141 * where our handlers are at the end of the chain list and
1142 * when all next handlers use skb->dst->dev and not outdev.
1143 * It will definitely route properly the inout NAT traffic
1144 * when multiple paths are used.
1145 */
1146
1147 /* For policy routing, packets originating from this
1148 * machine itself may be routed differently to packets
1149 * passing through. We want this packet to be routed as
1150 * if it came from this machine itself. So re-compute
1151 * the routing information.
1152 */
1153 if (ip_vs_route_me_harder(af, skb, hooknum))
1154 goto drop;
1155
1156 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
1157
1158 ip_vs_out_stats(cp, skb);
1159 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1160 skb->ipvs_property = 1;
1161 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1162 ip_vs_notrack(skb);
1163 else
1164 ip_vs_update_conntrack(skb, cp, 0);
1165 ip_vs_conn_put(cp);
1166
1167 LeaveFunction(11);
1168 return NF_ACCEPT;
1169
1170 drop:
1171 ip_vs_conn_put(cp);
1172 kfree_skb(skb);
1173 LeaveFunction(11);
1174 return NF_STOLEN;
1175 }
1176
1177 /*
1178 * Check if outgoing packet belongs to the established ip_vs_conn.
1179 */
1180 static unsigned int
1181 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1182 {
1183 struct net *net = NULL;
1184 struct netns_ipvs *ipvs;
1185 struct ip_vs_iphdr iph;
1186 struct ip_vs_protocol *pp;
1187 struct ip_vs_proto_data *pd;
1188 struct ip_vs_conn *cp;
1189
1190 EnterFunction(11);
1191
1192 /* Already marked as IPVS request or reply? */
1193 if (skb->ipvs_property)
1194 return NF_ACCEPT;
1195
1196 /* Bad... Do not break raw sockets */
1197 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1198 af == AF_INET)) {
1199 struct sock *sk = skb->sk;
1200 struct inet_sock *inet = inet_sk(skb->sk);
1201
1202 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1203 return NF_ACCEPT;
1204 }
1205
1206 if (unlikely(!skb_dst(skb)))
1207 return NF_ACCEPT;
1208
1209 net = skb_net(skb);
1210 ipvs = net_ipvs(net);
1211 if (!ipvs->enable)
1212 return NF_ACCEPT;
1213
1214 ip_vs_fill_iph_skb(af, skb, false, &iph);
1215 #ifdef CONFIG_IP_VS_IPV6
1216 if (af == AF_INET6) {
1217 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1218 int related;
1219 int verdict = ip_vs_out_icmp_v6(skb, &related,
1220 hooknum, &iph);
1221
1222 if (related)
1223 return verdict;
1224 }
1225 } else
1226 #endif
1227 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1228 int related;
1229 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1230
1231 if (related)
1232 return verdict;
1233 }
1234
1235 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1236 if (unlikely(!pd))
1237 return NF_ACCEPT;
1238 pp = pd->pp;
1239
1240 /* reassemble IP fragments */
1241 #ifdef CONFIG_IP_VS_IPV6
1242 if (af == AF_INET)
1243 #endif
1244 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1245 if (ip_vs_gather_frags(skb,
1246 ip_vs_defrag_user(hooknum)))
1247 return NF_STOLEN;
1248
1249 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
1250 }
1251
1252 /*
1253 * Check if the packet belongs to an existing entry
1254 */
1255 cp = pp->conn_out_get(ipvs, af, skb, &iph);
1256
1257 if (likely(cp))
1258 return handle_response(af, skb, pd, cp, &iph, hooknum);
1259 if (sysctl_nat_icmp_send(net) &&
1260 (pp->protocol == IPPROTO_TCP ||
1261 pp->protocol == IPPROTO_UDP ||
1262 pp->protocol == IPPROTO_SCTP)) {
1263 __be16 _ports[2], *pptr;
1264
1265 pptr = frag_safe_skb_hp(skb, iph.len,
1266 sizeof(_ports), _ports, &iph);
1267 if (pptr == NULL)
1268 return NF_ACCEPT; /* Not for me */
1269 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
1270 pptr[0])) {
1271 /*
1272 * Notify the real server: there is no
1273 * existing entry if it is not RST
1274 * packet or not TCP packet.
1275 */
1276 if ((iph.protocol != IPPROTO_TCP &&
1277 iph.protocol != IPPROTO_SCTP)
1278 || ((iph.protocol == IPPROTO_TCP
1279 && !is_tcp_reset(skb, iph.len))
1280 || (iph.protocol == IPPROTO_SCTP
1281 && !is_sctp_abort(skb,
1282 iph.len)))) {
1283 #ifdef CONFIG_IP_VS_IPV6
1284 if (af == AF_INET6) {
1285 if (!skb->dev)
1286 skb->dev = net->loopback_dev;
1287 icmpv6_send(skb,
1288 ICMPV6_DEST_UNREACH,
1289 ICMPV6_PORT_UNREACH,
1290 0);
1291 } else
1292 #endif
1293 icmp_send(skb,
1294 ICMP_DEST_UNREACH,
1295 ICMP_PORT_UNREACH, 0);
1296 return NF_DROP;
1297 }
1298 }
1299 }
1300 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
1301 "ip_vs_out: packet continues traversal as normal");
1302 return NF_ACCEPT;
1303 }
1304
1305 /*
1306 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1307 * used only for VS/NAT.
1308 * Check if packet is reply for established ip_vs_conn.
1309 */
1310 static unsigned int
1311 ip_vs_reply4(void *priv, struct sk_buff *skb,
1312 const struct nf_hook_state *state)
1313 {
1314 return ip_vs_out(state->hook, skb, AF_INET);
1315 }
1316
1317 /*
1318 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1319 * Check if packet is reply for established ip_vs_conn.
1320 */
1321 static unsigned int
1322 ip_vs_local_reply4(void *priv, struct sk_buff *skb,
1323 const struct nf_hook_state *state)
1324 {
1325 return ip_vs_out(state->hook, skb, AF_INET);
1326 }
1327
1328 #ifdef CONFIG_IP_VS_IPV6
1329
1330 /*
1331 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1332 * used only for VS/NAT.
1333 * Check if packet is reply for established ip_vs_conn.
1334 */
1335 static unsigned int
1336 ip_vs_reply6(void *priv, struct sk_buff *skb,
1337 const struct nf_hook_state *state)
1338 {
1339 return ip_vs_out(state->hook, skb, AF_INET6);
1340 }
1341
1342 /*
1343 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1344 * Check if packet is reply for established ip_vs_conn.
1345 */
1346 static unsigned int
1347 ip_vs_local_reply6(void *priv, struct sk_buff *skb,
1348 const struct nf_hook_state *state)
1349 {
1350 return ip_vs_out(state->hook, skb, AF_INET6);
1351 }
1352
1353 #endif
1354
1355 static unsigned int
1356 ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1357 struct ip_vs_proto_data *pd,
1358 int *verdict, struct ip_vs_conn **cpp,
1359 struct ip_vs_iphdr *iph)
1360 {
1361 struct ip_vs_protocol *pp = pd->pp;
1362
1363 if (!iph->fragoffs) {
1364 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1365 * replayed fragment zero will already have created the cp
1366 */
1367
1368 /* Schedule and create new connection entry into cpp */
1369 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
1370 return 0;
1371 }
1372
1373 if (unlikely(!*cpp)) {
1374 /* sorry, all this trouble for a no-hit :) */
1375 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1376 "ip_vs_in: packet continues traversal as normal");
1377 if (iph->fragoffs) {
1378 /* Fragment that couldn't be mapped to a conn entry
1379 * is missing module nf_defrag_ipv6
1380 */
1381 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1382 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1383 "unhandled fragment");
1384 }
1385 *verdict = NF_ACCEPT;
1386 return 0;
1387 }
1388
1389 return 1;
1390 }
1391
1392 /*
1393 * Handle ICMP messages in the outside-to-inside direction (incoming).
1394 * Find any that might be relevant, check against existing connections,
1395 * forward to the right destination host if relevant.
1396 * Currently handles error types - unreachable, quench, ttl exceeded.
1397 */
1398 static int
1399 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1400 {
1401 struct net *net = NULL;
1402 struct netns_ipvs *ipvs;
1403 struct iphdr *iph;
1404 struct icmphdr _icmph, *ic;
1405 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1406 struct ip_vs_iphdr ciph;
1407 struct ip_vs_conn *cp;
1408 struct ip_vs_protocol *pp;
1409 struct ip_vs_proto_data *pd;
1410 unsigned int offset, offset2, ihl, verdict;
1411 bool ipip, new_cp = false;
1412
1413 *related = 1;
1414
1415 /* reassemble IP fragments */
1416 if (ip_is_fragment(ip_hdr(skb))) {
1417 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1418 return NF_STOLEN;
1419 }
1420
1421 iph = ip_hdr(skb);
1422 offset = ihl = iph->ihl * 4;
1423 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1424 if (ic == NULL)
1425 return NF_DROP;
1426
1427 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1428 ic->type, ntohs(icmp_id(ic)),
1429 &iph->saddr, &iph->daddr);
1430
1431 /*
1432 * Work through seeing if this is for us.
1433 * These checks are supposed to be in an order that means easy
1434 * things are checked first to speed up processing.... however
1435 * this means that some packets will manage to get a long way
1436 * down this stack and then be rejected, but that's life.
1437 */
1438 if ((ic->type != ICMP_DEST_UNREACH) &&
1439 (ic->type != ICMP_SOURCE_QUENCH) &&
1440 (ic->type != ICMP_TIME_EXCEEDED)) {
1441 *related = 0;
1442 return NF_ACCEPT;
1443 }
1444
1445 /* Now find the contained IP header */
1446 offset += sizeof(_icmph);
1447 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1448 if (cih == NULL)
1449 return NF_ACCEPT; /* The packet looks wrong, ignore */
1450
1451 net = skb_net(skb);
1452 ipvs = net_ipvs(net);
1453
1454 /* Special case for errors for IPIP packets */
1455 ipip = false;
1456 if (cih->protocol == IPPROTO_IPIP) {
1457 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1458 return NF_ACCEPT;
1459 /* Error for our IPIP must arrive at LOCAL_IN */
1460 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1461 return NF_ACCEPT;
1462 offset += cih->ihl * 4;
1463 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1464 if (cih == NULL)
1465 return NF_ACCEPT; /* The packet looks wrong, ignore */
1466 ipip = true;
1467 }
1468
1469 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
1470 if (!pd)
1471 return NF_ACCEPT;
1472 pp = pd->pp;
1473
1474 /* Is the embedded protocol header present? */
1475 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1476 pp->dont_defrag))
1477 return NF_ACCEPT;
1478
1479 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1480 "Checking incoming ICMP for");
1481
1482 offset2 = offset;
1483 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
1484 offset = ciph.len;
1485
1486 /* The embedded headers contain source and dest in reverse order.
1487 * For IPIP this is error for request, not for reply.
1488 */
1489 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
1490
1491 if (!cp) {
1492 int v;
1493
1494 if (!sysctl_schedule_icmp(ipvs))
1495 return NF_ACCEPT;
1496
1497 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
1498 return v;
1499 new_cp = true;
1500 }
1501
1502 verdict = NF_DROP;
1503
1504 /* Ensure the checksum is correct */
1505 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1506 /* Failed checksum! */
1507 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1508 &iph->saddr);
1509 goto out;
1510 }
1511
1512 if (ipip) {
1513 __be32 info = ic->un.gateway;
1514 __u8 type = ic->type;
1515 __u8 code = ic->code;
1516
1517 /* Update the MTU */
1518 if (ic->type == ICMP_DEST_UNREACH &&
1519 ic->code == ICMP_FRAG_NEEDED) {
1520 struct ip_vs_dest *dest = cp->dest;
1521 u32 mtu = ntohs(ic->un.frag.mtu);
1522 __be16 frag_off = cih->frag_off;
1523
1524 /* Strip outer IP and ICMP, go to IPIP header */
1525 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1526 goto ignore_ipip;
1527 offset2 -= ihl + sizeof(_icmph);
1528 skb_reset_network_header(skb);
1529 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1530 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1531 ipv4_update_pmtu(skb, dev_net(skb->dev),
1532 mtu, 0, 0, 0, 0);
1533 /* Client uses PMTUD? */
1534 if (!(frag_off & htons(IP_DF)))
1535 goto ignore_ipip;
1536 /* Prefer the resulting PMTU */
1537 if (dest) {
1538 struct ip_vs_dest_dst *dest_dst;
1539
1540 rcu_read_lock();
1541 dest_dst = rcu_dereference(dest->dest_dst);
1542 if (dest_dst)
1543 mtu = dst_mtu(dest_dst->dst_cache);
1544 rcu_read_unlock();
1545 }
1546 if (mtu > 68 + sizeof(struct iphdr))
1547 mtu -= sizeof(struct iphdr);
1548 info = htonl(mtu);
1549 }
1550 /* Strip outer IP, ICMP and IPIP, go to IP header of
1551 * original request.
1552 */
1553 if (pskb_pull(skb, offset2) == NULL)
1554 goto ignore_ipip;
1555 skb_reset_network_header(skb);
1556 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1557 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1558 type, code, ntohl(info));
1559 icmp_send(skb, type, code, info);
1560 /* ICMP can be shorter but anyways, account it */
1561 ip_vs_out_stats(cp, skb);
1562
1563 ignore_ipip:
1564 consume_skb(skb);
1565 verdict = NF_STOLEN;
1566 goto out;
1567 }
1568
1569 /* do the statistics and put it back */
1570 ip_vs_in_stats(cp, skb);
1571 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1572 IPPROTO_SCTP == cih->protocol)
1573 offset += 2 * sizeof(__u16);
1574 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1575
1576 out:
1577 if (likely(!new_cp))
1578 __ip_vs_conn_put(cp);
1579 else
1580 ip_vs_conn_put(cp);
1581
1582 return verdict;
1583 }
1584
1585 #ifdef CONFIG_IP_VS_IPV6
1586 static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1587 unsigned int hooknum, struct ip_vs_iphdr *iph)
1588 {
1589 struct net *net = NULL;
1590 struct netns_ipvs *ipvs;
1591 struct icmp6hdr _icmph, *ic;
1592 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1593 struct ip_vs_conn *cp;
1594 struct ip_vs_protocol *pp;
1595 struct ip_vs_proto_data *pd;
1596 unsigned int offset, verdict;
1597 bool new_cp = false;
1598
1599 *related = 1;
1600
1601 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1602 if (ic == NULL)
1603 return NF_DROP;
1604
1605 /*
1606 * Work through seeing if this is for us.
1607 * These checks are supposed to be in an order that means easy
1608 * things are checked first to speed up processing.... however
1609 * this means that some packets will manage to get a long way
1610 * down this stack and then be rejected, but that's life.
1611 */
1612 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1613 *related = 0;
1614 return NF_ACCEPT;
1615 }
1616 /* Fragment header that is before ICMP header tells us that:
1617 * it's not an error message since they can't be fragmented.
1618 */
1619 if (iph->flags & IP6_FH_F_FRAG)
1620 return NF_DROP;
1621
1622 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1623 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1624 &iph->saddr, &iph->daddr);
1625
1626 offset = iph->len + sizeof(_icmph);
1627 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
1628 return NF_ACCEPT;
1629
1630 net = skb_net(skb);
1631 ipvs = net_ipvs(net);
1632 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
1633 if (!pd)
1634 return NF_ACCEPT;
1635 pp = pd->pp;
1636
1637 /* Cannot handle fragmented embedded protocol */
1638 if (ciph.fragoffs)
1639 return NF_ACCEPT;
1640
1641 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1642 "Checking incoming ICMPv6 for");
1643
1644 /* The embedded headers contain source and dest in reverse order
1645 * if not from localhost
1646 */
1647 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
1648
1649 if (!cp) {
1650 int v;
1651
1652 if (!sysctl_schedule_icmp(ipvs))
1653 return NF_ACCEPT;
1654
1655 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
1656 return v;
1657
1658 new_cp = true;
1659 }
1660
1661 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1662 if ((hooknum == NF_INET_LOCAL_OUT) &&
1663 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1664 verdict = NF_ACCEPT;
1665 goto out;
1666 }
1667
1668 /* do the statistics and put it back */
1669 ip_vs_in_stats(cp, skb);
1670
1671 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1672 offset = ciph.len;
1673 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1674 IPPROTO_SCTP == ciph.protocol)
1675 offset += 2 * sizeof(__u16); /* Also mangle ports */
1676
1677 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
1678
1679 out:
1680 if (likely(!new_cp))
1681 __ip_vs_conn_put(cp);
1682 else
1683 ip_vs_conn_put(cp);
1684
1685 return verdict;
1686 }
1687 #endif
1688
1689
1690 /*
1691 * Check if it's for virtual services, look it up,
1692 * and send it on its way...
1693 */
1694 static unsigned int
1695 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1696 {
1697 struct net *net;
1698 struct ip_vs_iphdr iph;
1699 struct ip_vs_protocol *pp;
1700 struct ip_vs_proto_data *pd;
1701 struct ip_vs_conn *cp;
1702 int ret, pkts;
1703 struct netns_ipvs *ipvs;
1704 int conn_reuse_mode;
1705
1706 /* Already marked as IPVS request or reply? */
1707 if (skb->ipvs_property)
1708 return NF_ACCEPT;
1709
1710 /*
1711 * Big tappo:
1712 * - remote client: only PACKET_HOST
1713 * - route: used for struct net when skb->dev is unset
1714 */
1715 if (unlikely((skb->pkt_type != PACKET_HOST &&
1716 hooknum != NF_INET_LOCAL_OUT) ||
1717 !skb_dst(skb))) {
1718 ip_vs_fill_iph_skb(af, skb, false, &iph);
1719 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1720 " ignored in hook %u\n",
1721 skb->pkt_type, iph.protocol,
1722 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1723 return NF_ACCEPT;
1724 }
1725 /* ipvs enabled in this netns ? */
1726 net = skb_net(skb);
1727 ipvs = net_ipvs(net);
1728 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1729 return NF_ACCEPT;
1730
1731 ip_vs_fill_iph_skb(af, skb, false, &iph);
1732
1733 /* Bad... Do not break raw sockets */
1734 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1735 af == AF_INET)) {
1736 struct sock *sk = skb->sk;
1737 struct inet_sock *inet = inet_sk(skb->sk);
1738
1739 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1740 return NF_ACCEPT;
1741 }
1742
1743 #ifdef CONFIG_IP_VS_IPV6
1744 if (af == AF_INET6) {
1745 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1746 int related;
1747 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1748 &iph);
1749
1750 if (related)
1751 return verdict;
1752 }
1753 } else
1754 #endif
1755 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1756 int related;
1757 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1758
1759 if (related)
1760 return verdict;
1761 }
1762
1763 /* Protocol supported? */
1764 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1765 if (unlikely(!pd)) {
1766 /* The only way we'll see this packet again is if it's
1767 * encapsulated, so mark it with ipvs_property=1 so we
1768 * skip it if we're ignoring tunneled packets
1769 */
1770 if (sysctl_ignore_tunneled(ipvs))
1771 skb->ipvs_property = 1;
1772
1773 return NF_ACCEPT;
1774 }
1775 pp = pd->pp;
1776 /*
1777 * Check if the packet belongs to an existing connection entry
1778 */
1779 cp = pp->conn_in_get(ipvs, af, skb, &iph);
1780
1781 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1782 if (conn_reuse_mode && !iph.fragoffs &&
1783 is_new_conn(skb, &iph) && cp &&
1784 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1785 unlikely(!atomic_read(&cp->dest->weight))) ||
1786 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1787 if (!atomic_read(&cp->n_control))
1788 ip_vs_conn_expire_now(cp);
1789 __ip_vs_conn_put(cp);
1790 cp = NULL;
1791 }
1792
1793 if (unlikely(!cp)) {
1794 int v;
1795
1796 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
1797 return v;
1798 }
1799
1800 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
1801
1802 /* Check the server status */
1803 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1804 /* the destination server is not available */
1805
1806 if (sysctl_expire_nodest_conn(ipvs)) {
1807 /* try to expire the connection immediately */
1808 ip_vs_conn_expire_now(cp);
1809 }
1810 /* don't restart its timer, and silently
1811 drop the packet. */
1812 __ip_vs_conn_put(cp);
1813 return NF_DROP;
1814 }
1815
1816 ip_vs_in_stats(cp, skb);
1817 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1818 if (cp->packet_xmit)
1819 ret = cp->packet_xmit(skb, cp, pp, &iph);
1820 /* do not touch skb anymore */
1821 else {
1822 IP_VS_DBG_RL("warning: packet_xmit is null");
1823 ret = NF_ACCEPT;
1824 }
1825
1826 /* Increase its packet counter and check if it is needed
1827 * to be synchronized
1828 *
1829 * Sync connection if it is about to close to
1830 * encorage the standby servers to update the connections timeout
1831 *
1832 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1833 */
1834
1835 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1836 pkts = sysctl_sync_threshold(ipvs);
1837 else
1838 pkts = atomic_add_return(1, &cp->in_pkts);
1839
1840 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1841 ip_vs_sync_conn(ipvs, cp, pkts);
1842
1843 ip_vs_conn_put(cp);
1844 return ret;
1845 }
1846
1847 /*
1848 * AF_INET handler in NF_INET_LOCAL_IN chain
1849 * Schedule and forward packets from remote clients
1850 */
1851 static unsigned int
1852 ip_vs_remote_request4(void *priv, struct sk_buff *skb,
1853 const struct nf_hook_state *state)
1854 {
1855 return ip_vs_in(state->hook, skb, AF_INET);
1856 }
1857
1858 /*
1859 * AF_INET handler in NF_INET_LOCAL_OUT chain
1860 * Schedule and forward packets from local clients
1861 */
1862 static unsigned int
1863 ip_vs_local_request4(void *priv, struct sk_buff *skb,
1864 const struct nf_hook_state *state)
1865 {
1866 return ip_vs_in(state->hook, skb, AF_INET);
1867 }
1868
1869 #ifdef CONFIG_IP_VS_IPV6
1870
1871 /*
1872 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1873 * Schedule and forward packets from remote clients
1874 */
1875 static unsigned int
1876 ip_vs_remote_request6(void *priv, struct sk_buff *skb,
1877 const struct nf_hook_state *state)
1878 {
1879 return ip_vs_in(state->hook, skb, AF_INET6);
1880 }
1881
1882 /*
1883 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1884 * Schedule and forward packets from local clients
1885 */
1886 static unsigned int
1887 ip_vs_local_request6(void *priv, struct sk_buff *skb,
1888 const struct nf_hook_state *state)
1889 {
1890 return ip_vs_in(state->hook, skb, AF_INET6);
1891 }
1892
1893 #endif
1894
1895
1896 /*
1897 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1898 * related packets destined for 0.0.0.0/0.
1899 * When fwmark-based virtual service is used, such as transparent
1900 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1901 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1902 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1903 * and send them to ip_vs_in_icmp.
1904 */
1905 static unsigned int
1906 ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
1907 const struct nf_hook_state *state)
1908 {
1909 int r;
1910 struct netns_ipvs *ipvs;
1911
1912 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1913 return NF_ACCEPT;
1914
1915 /* ipvs enabled in this netns ? */
1916 ipvs = net_ipvs(state->net);
1917 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1918 return NF_ACCEPT;
1919
1920 return ip_vs_in_icmp(skb, &r, state->hook);
1921 }
1922
1923 #ifdef CONFIG_IP_VS_IPV6
1924 static unsigned int
1925 ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
1926 const struct nf_hook_state *state)
1927 {
1928 int r;
1929 struct netns_ipvs *ipvs;
1930 struct ip_vs_iphdr iphdr;
1931
1932 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
1933 if (iphdr.protocol != IPPROTO_ICMPV6)
1934 return NF_ACCEPT;
1935
1936 /* ipvs enabled in this netns ? */
1937 ipvs = net_ipvs(state->net);
1938 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1939 return NF_ACCEPT;
1940
1941 return ip_vs_in_icmp_v6(skb, &r, state->hook, &iphdr);
1942 }
1943 #endif
1944
1945
1946 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1947 /* After packet filtering, change source only for VS/NAT */
1948 {
1949 .hook = ip_vs_reply4,
1950 .owner = THIS_MODULE,
1951 .pf = NFPROTO_IPV4,
1952 .hooknum = NF_INET_LOCAL_IN,
1953 .priority = NF_IP_PRI_NAT_SRC - 2,
1954 },
1955 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1956 * or VS/NAT(change destination), so that filtering rules can be
1957 * applied to IPVS. */
1958 {
1959 .hook = ip_vs_remote_request4,
1960 .owner = THIS_MODULE,
1961 .pf = NFPROTO_IPV4,
1962 .hooknum = NF_INET_LOCAL_IN,
1963 .priority = NF_IP_PRI_NAT_SRC - 1,
1964 },
1965 /* Before ip_vs_in, change source only for VS/NAT */
1966 {
1967 .hook = ip_vs_local_reply4,
1968 .owner = THIS_MODULE,
1969 .pf = NFPROTO_IPV4,
1970 .hooknum = NF_INET_LOCAL_OUT,
1971 .priority = NF_IP_PRI_NAT_DST + 1,
1972 },
1973 /* After mangle, schedule and forward local requests */
1974 {
1975 .hook = ip_vs_local_request4,
1976 .owner = THIS_MODULE,
1977 .pf = NFPROTO_IPV4,
1978 .hooknum = NF_INET_LOCAL_OUT,
1979 .priority = NF_IP_PRI_NAT_DST + 2,
1980 },
1981 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1982 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1983 {
1984 .hook = ip_vs_forward_icmp,
1985 .owner = THIS_MODULE,
1986 .pf = NFPROTO_IPV4,
1987 .hooknum = NF_INET_FORWARD,
1988 .priority = 99,
1989 },
1990 /* After packet filtering, change source only for VS/NAT */
1991 {
1992 .hook = ip_vs_reply4,
1993 .owner = THIS_MODULE,
1994 .pf = NFPROTO_IPV4,
1995 .hooknum = NF_INET_FORWARD,
1996 .priority = 100,
1997 },
1998 #ifdef CONFIG_IP_VS_IPV6
1999 /* After packet filtering, change source only for VS/NAT */
2000 {
2001 .hook = ip_vs_reply6,
2002 .owner = THIS_MODULE,
2003 .pf = NFPROTO_IPV6,
2004 .hooknum = NF_INET_LOCAL_IN,
2005 .priority = NF_IP6_PRI_NAT_SRC - 2,
2006 },
2007 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2008 * or VS/NAT(change destination), so that filtering rules can be
2009 * applied to IPVS. */
2010 {
2011 .hook = ip_vs_remote_request6,
2012 .owner = THIS_MODULE,
2013 .pf = NFPROTO_IPV6,
2014 .hooknum = NF_INET_LOCAL_IN,
2015 .priority = NF_IP6_PRI_NAT_SRC - 1,
2016 },
2017 /* Before ip_vs_in, change source only for VS/NAT */
2018 {
2019 .hook = ip_vs_local_reply6,
2020 .owner = THIS_MODULE,
2021 .pf = NFPROTO_IPV6,
2022 .hooknum = NF_INET_LOCAL_OUT,
2023 .priority = NF_IP6_PRI_NAT_DST + 1,
2024 },
2025 /* After mangle, schedule and forward local requests */
2026 {
2027 .hook = ip_vs_local_request6,
2028 .owner = THIS_MODULE,
2029 .pf = NFPROTO_IPV6,
2030 .hooknum = NF_INET_LOCAL_OUT,
2031 .priority = NF_IP6_PRI_NAT_DST + 2,
2032 },
2033 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2034 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2035 {
2036 .hook = ip_vs_forward_icmp_v6,
2037 .owner = THIS_MODULE,
2038 .pf = NFPROTO_IPV6,
2039 .hooknum = NF_INET_FORWARD,
2040 .priority = 99,
2041 },
2042 /* After packet filtering, change source only for VS/NAT */
2043 {
2044 .hook = ip_vs_reply6,
2045 .owner = THIS_MODULE,
2046 .pf = NFPROTO_IPV6,
2047 .hooknum = NF_INET_FORWARD,
2048 .priority = 100,
2049 },
2050 #endif
2051 };
2052 /*
2053 * Initialize IP Virtual Server netns mem.
2054 */
2055 static int __net_init __ip_vs_init(struct net *net)
2056 {
2057 struct netns_ipvs *ipvs;
2058
2059 ipvs = net_generic(net, ip_vs_net_id);
2060 if (ipvs == NULL)
2061 return -ENOMEM;
2062
2063 /* Hold the beast until a service is registerd */
2064 ipvs->enable = 0;
2065 ipvs->net = net;
2066 /* Counters used for creating unique names */
2067 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2068 atomic_inc(&ipvs_netns_cnt);
2069 net->ipvs = ipvs;
2070
2071 if (ip_vs_estimator_net_init(ipvs) < 0)
2072 goto estimator_fail;
2073
2074 if (ip_vs_control_net_init(ipvs) < 0)
2075 goto control_fail;
2076
2077 if (ip_vs_protocol_net_init(net) < 0)
2078 goto protocol_fail;
2079
2080 if (ip_vs_app_net_init(ipvs) < 0)
2081 goto app_fail;
2082
2083 if (ip_vs_conn_net_init(ipvs) < 0)
2084 goto conn_fail;
2085
2086 if (ip_vs_sync_net_init(ipvs) < 0)
2087 goto sync_fail;
2088
2089 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
2090 sizeof(struct netns_ipvs), ipvs->gen);
2091 return 0;
2092 /*
2093 * Error handling
2094 */
2095
2096 sync_fail:
2097 ip_vs_conn_net_cleanup(ipvs);
2098 conn_fail:
2099 ip_vs_app_net_cleanup(ipvs);
2100 app_fail:
2101 ip_vs_protocol_net_cleanup(net);
2102 protocol_fail:
2103 ip_vs_control_net_cleanup(ipvs);
2104 control_fail:
2105 ip_vs_estimator_net_cleanup(ipvs);
2106 estimator_fail:
2107 net->ipvs = NULL;
2108 return -ENOMEM;
2109 }
2110
2111 static void __net_exit __ip_vs_cleanup(struct net *net)
2112 {
2113 struct netns_ipvs *ipvs = net_ipvs(net);
2114
2115 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
2116 ip_vs_conn_net_cleanup(ipvs);
2117 ip_vs_app_net_cleanup(ipvs);
2118 ip_vs_protocol_net_cleanup(net);
2119 ip_vs_control_net_cleanup(ipvs);
2120 ip_vs_estimator_net_cleanup(ipvs);
2121 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
2122 net->ipvs = NULL;
2123 }
2124
2125 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2126 {
2127 struct netns_ipvs *ipvs = net_ipvs(net);
2128 EnterFunction(2);
2129 ipvs->enable = 0; /* Disable packet reception */
2130 smp_wmb();
2131 ip_vs_sync_net_cleanup(ipvs);
2132 LeaveFunction(2);
2133 }
2134
2135 static struct pernet_operations ipvs_core_ops = {
2136 .init = __ip_vs_init,
2137 .exit = __ip_vs_cleanup,
2138 .id = &ip_vs_net_id,
2139 .size = sizeof(struct netns_ipvs),
2140 };
2141
2142 static struct pernet_operations ipvs_core_dev_ops = {
2143 .exit = __ip_vs_dev_cleanup,
2144 };
2145
2146 /*
2147 * Initialize IP Virtual Server
2148 */
2149 static int __init ip_vs_init(void)
2150 {
2151 int ret;
2152
2153 ret = ip_vs_control_init();
2154 if (ret < 0) {
2155 pr_err("can't setup control.\n");
2156 goto exit;
2157 }
2158
2159 ip_vs_protocol_init();
2160
2161 ret = ip_vs_conn_init();
2162 if (ret < 0) {
2163 pr_err("can't setup connection table.\n");
2164 goto cleanup_protocol;
2165 }
2166
2167 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2168 if (ret < 0)
2169 goto cleanup_conn;
2170
2171 ret = register_pernet_device(&ipvs_core_dev_ops);
2172 if (ret < 0)
2173 goto cleanup_sub;
2174
2175 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2176 if (ret < 0) {
2177 pr_err("can't register hooks.\n");
2178 goto cleanup_dev;
2179 }
2180
2181 ret = ip_vs_register_nl_ioctl();
2182 if (ret < 0) {
2183 pr_err("can't register netlink/ioctl.\n");
2184 goto cleanup_hooks;
2185 }
2186
2187 pr_info("ipvs loaded.\n");
2188
2189 return ret;
2190
2191 cleanup_hooks:
2192 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2193 cleanup_dev:
2194 unregister_pernet_device(&ipvs_core_dev_ops);
2195 cleanup_sub:
2196 unregister_pernet_subsys(&ipvs_core_ops);
2197 cleanup_conn:
2198 ip_vs_conn_cleanup();
2199 cleanup_protocol:
2200 ip_vs_protocol_cleanup();
2201 ip_vs_control_cleanup();
2202 exit:
2203 return ret;
2204 }
2205
2206 static void __exit ip_vs_cleanup(void)
2207 {
2208 ip_vs_unregister_nl_ioctl();
2209 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2210 unregister_pernet_device(&ipvs_core_dev_ops);
2211 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2212 ip_vs_conn_cleanup();
2213 ip_vs_protocol_cleanup();
2214 ip_vs_control_cleanup();
2215 pr_info("ipvs unloaded.\n");
2216 }
2217
2218 module_init(ip_vs_init);
2219 module_exit(ip_vs_cleanup);
2220 MODULE_LICENSE("GPL");
This page took 0.100742 seconds and 5 git commands to generate.