ab85ae0f531ff29e0411b6f565e05e7d4aca8dcb
[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 netns_ipvs *ipvs = svc->ipvs;
572 struct net *net = ipvs->net;
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 /* if it is fwmark-based service, the cache_bypass sysctl is up
580 and the destination is a non-local unicast, then create
581 a cache_bypass connection entry */
582 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
583 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
584 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
585 int ret;
586 struct ip_vs_conn *cp;
587 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
588 iph->protocol == IPPROTO_UDP) ?
589 IP_VS_CONN_F_ONE_PACKET : 0;
590 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
591
592 /* create a new connection entry */
593 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
594 {
595 struct ip_vs_conn_param p;
596 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
597 &iph->saddr, pptr[0],
598 &iph->daddr, pptr[1], &p);
599 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
600 IP_VS_CONN_F_BYPASS | flags,
601 NULL, skb->mark);
602 if (!cp)
603 return NF_DROP;
604 }
605
606 /* statistics */
607 ip_vs_in_stats(cp, skb);
608
609 /* set state */
610 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
611
612 /* transmit the first SYN packet */
613 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
614 /* do not touch skb anymore */
615
616 atomic_inc(&cp->in_pkts);
617 ip_vs_conn_put(cp);
618 return ret;
619 }
620
621 /*
622 * When the virtual ftp service is presented, packets destined
623 * for other services on the VIP may get here (except services
624 * listed in the ipvs table), pass the packets, because it is
625 * not ipvs job to decide to drop the packets.
626 */
627 if (svc->port == FTPPORT && dport != FTPPORT)
628 return NF_ACCEPT;
629
630 if (unlikely(ip_vs_iph_icmp(iph)))
631 return NF_DROP;
632
633 /*
634 * Notify the client that the destination is unreachable, and
635 * release the socket buffer.
636 * Since it is in IP layer, the TCP socket is not actually
637 * created, the TCP RST packet cannot be sent, instead that
638 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
639 */
640 #ifdef CONFIG_IP_VS_IPV6
641 if (svc->af == AF_INET6) {
642 if (!skb->dev)
643 skb->dev = net->loopback_dev;
644 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
645 } else
646 #endif
647 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
648
649 return NF_DROP;
650 }
651
652 #ifdef CONFIG_SYSCTL
653
654 static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
655 {
656 return ipvs->sysctl_snat_reroute;
657 }
658
659 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
660 {
661 return ipvs->sysctl_nat_icmp_send;
662 }
663
664 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
665 {
666 return ipvs->sysctl_expire_nodest_conn;
667 }
668
669 #else
670
671 static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
672 static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
673 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
674
675 #endif
676
677 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
678 {
679 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
680 }
681
682 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
683 {
684 if (NF_INET_LOCAL_IN == hooknum)
685 return IP_DEFRAG_VS_IN;
686 if (NF_INET_FORWARD == hooknum)
687 return IP_DEFRAG_VS_FWD;
688 return IP_DEFRAG_VS_OUT;
689 }
690
691 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
692 {
693 int err;
694
695 local_bh_disable();
696 err = ip_defrag(skb, user);
697 local_bh_enable();
698 if (!err)
699 ip_send_check(ip_hdr(skb));
700
701 return err;
702 }
703
704 static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
705 struct sk_buff *skb, unsigned int hooknum)
706 {
707 if (!sysctl_snat_reroute(ipvs))
708 return 0;
709 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
710 if (NF_INET_LOCAL_IN == hooknum)
711 return 0;
712 #ifdef CONFIG_IP_VS_IPV6
713 if (af == AF_INET6) {
714 struct dst_entry *dst = skb_dst(skb);
715
716 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
717 ip6_route_me_harder(skb) != 0)
718 return 1;
719 } else
720 #endif
721 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
722 ip_route_me_harder(skb, RTN_LOCAL) != 0)
723 return 1;
724
725 return 0;
726 }
727
728 /*
729 * Packet has been made sufficiently writable in caller
730 * - inout: 1=in->out, 0=out->in
731 */
732 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
733 struct ip_vs_conn *cp, int inout)
734 {
735 struct iphdr *iph = ip_hdr(skb);
736 unsigned int icmp_offset = iph->ihl*4;
737 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
738 icmp_offset);
739 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
740
741 if (inout) {
742 iph->saddr = cp->vaddr.ip;
743 ip_send_check(iph);
744 ciph->daddr = cp->vaddr.ip;
745 ip_send_check(ciph);
746 } else {
747 iph->daddr = cp->daddr.ip;
748 ip_send_check(iph);
749 ciph->saddr = cp->daddr.ip;
750 ip_send_check(ciph);
751 }
752
753 /* the TCP/UDP/SCTP port */
754 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
755 IPPROTO_SCTP == ciph->protocol) {
756 __be16 *ports = (void *)ciph + ciph->ihl*4;
757
758 if (inout)
759 ports[1] = cp->vport;
760 else
761 ports[0] = cp->dport;
762 }
763
764 /* And finally the ICMP checksum */
765 icmph->checksum = 0;
766 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
767 skb->ip_summed = CHECKSUM_UNNECESSARY;
768
769 if (inout)
770 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
771 "Forwarding altered outgoing ICMP");
772 else
773 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
774 "Forwarding altered incoming ICMP");
775 }
776
777 #ifdef CONFIG_IP_VS_IPV6
778 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
779 struct ip_vs_conn *cp, int inout)
780 {
781 struct ipv6hdr *iph = ipv6_hdr(skb);
782 unsigned int icmp_offset = 0;
783 unsigned int offs = 0; /* header offset*/
784 int protocol;
785 struct icmp6hdr *icmph;
786 struct ipv6hdr *ciph;
787 unsigned short fragoffs;
788
789 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
790 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
791 offs = icmp_offset + sizeof(struct icmp6hdr);
792 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
793
794 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
795
796 if (inout) {
797 iph->saddr = cp->vaddr.in6;
798 ciph->daddr = cp->vaddr.in6;
799 } else {
800 iph->daddr = cp->daddr.in6;
801 ciph->saddr = cp->daddr.in6;
802 }
803
804 /* the TCP/UDP/SCTP port */
805 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
806 IPPROTO_SCTP == protocol)) {
807 __be16 *ports = (void *)(skb_network_header(skb) + offs);
808
809 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
810 ntohs(inout ? ports[1] : ports[0]),
811 ntohs(inout ? cp->vport : cp->dport));
812 if (inout)
813 ports[1] = cp->vport;
814 else
815 ports[0] = cp->dport;
816 }
817
818 /* And finally the ICMP checksum */
819 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
820 skb->len - icmp_offset,
821 IPPROTO_ICMPV6, 0);
822 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
823 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
824 skb->ip_summed = CHECKSUM_PARTIAL;
825
826 if (inout)
827 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
828 (void *)ciph - (void *)iph,
829 "Forwarding altered outgoing ICMPv6");
830 else
831 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
832 (void *)ciph - (void *)iph,
833 "Forwarding altered incoming ICMPv6");
834 }
835 #endif
836
837 /* Handle relevant response ICMP messages - forward to the right
838 * destination host.
839 */
840 static int handle_response_icmp(int af, struct sk_buff *skb,
841 union nf_inet_addr *snet,
842 __u8 protocol, struct ip_vs_conn *cp,
843 struct ip_vs_protocol *pp,
844 unsigned int offset, unsigned int ihl,
845 unsigned int hooknum)
846 {
847 unsigned int verdict = NF_DROP;
848
849 if (IP_VS_FWD_METHOD(cp) != 0) {
850 pr_err("shouldn't reach here, because the box is on the "
851 "half connection in the tun/dr module.\n");
852 }
853
854 /* Ensure the checksum is correct */
855 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
856 /* Failed checksum! */
857 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
858 IP_VS_DBG_ADDR(af, snet));
859 goto out;
860 }
861
862 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
863 IPPROTO_SCTP == protocol)
864 offset += 2 * sizeof(__u16);
865 if (!skb_make_writable(skb, offset))
866 goto out;
867
868 #ifdef CONFIG_IP_VS_IPV6
869 if (af == AF_INET6)
870 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
871 else
872 #endif
873 ip_vs_nat_icmp(skb, pp, cp, 1);
874
875 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
876 goto out;
877
878 /* do the statistics and put it back */
879 ip_vs_out_stats(cp, skb);
880
881 skb->ipvs_property = 1;
882 if (!(cp->flags & IP_VS_CONN_F_NFCT))
883 ip_vs_notrack(skb);
884 else
885 ip_vs_update_conntrack(skb, cp, 0);
886 verdict = NF_ACCEPT;
887
888 out:
889 __ip_vs_conn_put(cp);
890
891 return verdict;
892 }
893
894 /*
895 * Handle ICMP messages in the inside-to-outside direction (outgoing).
896 * Find any that might be relevant, check against existing connections.
897 * Currently handles error types - unreachable, quench, ttl exceeded.
898 */
899 static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
900 int *related, unsigned int hooknum)
901 {
902 struct iphdr *iph;
903 struct icmphdr _icmph, *ic;
904 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
905 struct ip_vs_iphdr ciph;
906 struct ip_vs_conn *cp;
907 struct ip_vs_protocol *pp;
908 unsigned int offset, ihl;
909 union nf_inet_addr snet;
910
911 *related = 1;
912
913 /* reassemble IP fragments */
914 if (ip_is_fragment(ip_hdr(skb))) {
915 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
916 return NF_STOLEN;
917 }
918
919 iph = ip_hdr(skb);
920 offset = ihl = iph->ihl * 4;
921 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
922 if (ic == NULL)
923 return NF_DROP;
924
925 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
926 ic->type, ntohs(icmp_id(ic)),
927 &iph->saddr, &iph->daddr);
928
929 /*
930 * Work through seeing if this is for us.
931 * These checks are supposed to be in an order that means easy
932 * things are checked first to speed up processing.... however
933 * this means that some packets will manage to get a long way
934 * down this stack and then be rejected, but that's life.
935 */
936 if ((ic->type != ICMP_DEST_UNREACH) &&
937 (ic->type != ICMP_SOURCE_QUENCH) &&
938 (ic->type != ICMP_TIME_EXCEEDED)) {
939 *related = 0;
940 return NF_ACCEPT;
941 }
942
943 /* Now find the contained IP header */
944 offset += sizeof(_icmph);
945 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
946 if (cih == NULL)
947 return NF_ACCEPT; /* The packet looks wrong, ignore */
948
949 pp = ip_vs_proto_get(cih->protocol);
950 if (!pp)
951 return NF_ACCEPT;
952
953 /* Is the embedded protocol header present? */
954 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
955 pp->dont_defrag))
956 return NF_ACCEPT;
957
958 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
959 "Checking outgoing ICMP for");
960
961 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
962
963 /* The embedded headers contain source and dest in reverse order */
964 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
965 if (!cp)
966 return NF_ACCEPT;
967
968 snet.ip = iph->saddr;
969 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
970 pp, ciph.len, ihl, hooknum);
971 }
972
973 #ifdef CONFIG_IP_VS_IPV6
974 static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
975 int *related, unsigned int hooknum,
976 struct ip_vs_iphdr *ipvsh)
977 {
978 struct icmp6hdr _icmph, *ic;
979 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
980 struct ip_vs_conn *cp;
981 struct ip_vs_protocol *pp;
982 union nf_inet_addr snet;
983 unsigned int offset;
984
985 *related = 1;
986 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
987 if (ic == NULL)
988 return NF_DROP;
989
990 /*
991 * Work through seeing if this is for us.
992 * These checks are supposed to be in an order that means easy
993 * things are checked first to speed up processing.... however
994 * this means that some packets will manage to get a long way
995 * down this stack and then be rejected, but that's life.
996 */
997 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
998 *related = 0;
999 return NF_ACCEPT;
1000 }
1001 /* Fragment header that is before ICMP header tells us that:
1002 * it's not an error message since they can't be fragmented.
1003 */
1004 if (ipvsh->flags & IP6_FH_F_FRAG)
1005 return NF_DROP;
1006
1007 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1008 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1009 &ipvsh->saddr, &ipvsh->daddr);
1010
1011 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1012 true, &ciph))
1013 return NF_ACCEPT; /* The packet looks wrong, ignore */
1014
1015 pp = ip_vs_proto_get(ciph.protocol);
1016 if (!pp)
1017 return NF_ACCEPT;
1018
1019 /* The embedded headers contain source and dest in reverse order */
1020 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
1021 if (!cp)
1022 return NF_ACCEPT;
1023
1024 snet.in6 = ciph.saddr.in6;
1025 offset = ciph.len;
1026 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
1027 pp, offset, sizeof(struct ipv6hdr),
1028 hooknum);
1029 }
1030 #endif
1031
1032 /*
1033 * Check if sctp chunc is ABORT chunk
1034 */
1035 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1036 {
1037 sctp_chunkhdr_t *sch, schunk;
1038 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1039 sizeof(schunk), &schunk);
1040 if (sch == NULL)
1041 return 0;
1042 if (sch->type == SCTP_CID_ABORT)
1043 return 1;
1044 return 0;
1045 }
1046
1047 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1048 {
1049 struct tcphdr _tcph, *th;
1050
1051 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1052 if (th == NULL)
1053 return 0;
1054 return th->rst;
1055 }
1056
1057 static inline bool is_new_conn(const struct sk_buff *skb,
1058 struct ip_vs_iphdr *iph)
1059 {
1060 switch (iph->protocol) {
1061 case IPPROTO_TCP: {
1062 struct tcphdr _tcph, *th;
1063
1064 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1065 if (th == NULL)
1066 return false;
1067 return th->syn;
1068 }
1069 case IPPROTO_SCTP: {
1070 sctp_chunkhdr_t *sch, schunk;
1071
1072 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1073 sizeof(schunk), &schunk);
1074 if (sch == NULL)
1075 return false;
1076 return sch->type == SCTP_CID_INIT;
1077 }
1078 default:
1079 return false;
1080 }
1081 }
1082
1083 static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1084 int conn_reuse_mode)
1085 {
1086 /* Controlled (FTP DATA or persistence)? */
1087 if (cp->control)
1088 return false;
1089
1090 switch (cp->protocol) {
1091 case IPPROTO_TCP:
1092 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1093 ((conn_reuse_mode & 2) &&
1094 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1095 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1096 case IPPROTO_SCTP:
1097 return cp->state == IP_VS_SCTP_S_CLOSED;
1098 default:
1099 return false;
1100 }
1101 }
1102
1103 /* Handle response packets: rewrite addresses and send away...
1104 */
1105 static unsigned int
1106 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1107 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1108 unsigned int hooknum)
1109 {
1110 struct ip_vs_protocol *pp = pd->pp;
1111
1112 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
1113
1114 if (!skb_make_writable(skb, iph->len))
1115 goto drop;
1116
1117 /* mangle the packet */
1118 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1119 goto drop;
1120
1121 #ifdef CONFIG_IP_VS_IPV6
1122 if (af == AF_INET6)
1123 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1124 else
1125 #endif
1126 {
1127 ip_hdr(skb)->saddr = cp->vaddr.ip;
1128 ip_send_check(ip_hdr(skb));
1129 }
1130
1131 /*
1132 * nf_iterate does not expect change in the skb->dst->dev.
1133 * It looks like it is not fatal to enable this code for hooks
1134 * where our handlers are at the end of the chain list and
1135 * when all next handlers use skb->dst->dev and not outdev.
1136 * It will definitely route properly the inout NAT traffic
1137 * when multiple paths are used.
1138 */
1139
1140 /* For policy routing, packets originating from this
1141 * machine itself may be routed differently to packets
1142 * passing through. We want this packet to be routed as
1143 * if it came from this machine itself. So re-compute
1144 * the routing information.
1145 */
1146 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
1147 goto drop;
1148
1149 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
1150
1151 ip_vs_out_stats(cp, skb);
1152 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1153 skb->ipvs_property = 1;
1154 if (!(cp->flags & IP_VS_CONN_F_NFCT))
1155 ip_vs_notrack(skb);
1156 else
1157 ip_vs_update_conntrack(skb, cp, 0);
1158 ip_vs_conn_put(cp);
1159
1160 LeaveFunction(11);
1161 return NF_ACCEPT;
1162
1163 drop:
1164 ip_vs_conn_put(cp);
1165 kfree_skb(skb);
1166 LeaveFunction(11);
1167 return NF_STOLEN;
1168 }
1169
1170 /*
1171 * Check if outgoing packet belongs to the established ip_vs_conn.
1172 */
1173 static unsigned int
1174 ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1175 {
1176 struct net *net = ipvs->net;
1177 struct ip_vs_iphdr iph;
1178 struct ip_vs_protocol *pp;
1179 struct ip_vs_proto_data *pd;
1180 struct ip_vs_conn *cp;
1181
1182 EnterFunction(11);
1183
1184 /* Already marked as IPVS request or reply? */
1185 if (skb->ipvs_property)
1186 return NF_ACCEPT;
1187
1188 /* Bad... Do not break raw sockets */
1189 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1190 af == AF_INET)) {
1191 struct sock *sk = skb->sk;
1192 struct inet_sock *inet = inet_sk(skb->sk);
1193
1194 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1195 return NF_ACCEPT;
1196 }
1197
1198 if (unlikely(!skb_dst(skb)))
1199 return NF_ACCEPT;
1200
1201 if (!ipvs->enable)
1202 return NF_ACCEPT;
1203
1204 ip_vs_fill_iph_skb(af, skb, false, &iph);
1205 #ifdef CONFIG_IP_VS_IPV6
1206 if (af == AF_INET6) {
1207 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1208 int related;
1209 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
1210 hooknum, &iph);
1211
1212 if (related)
1213 return verdict;
1214 }
1215 } else
1216 #endif
1217 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1218 int related;
1219 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
1220
1221 if (related)
1222 return verdict;
1223 }
1224
1225 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1226 if (unlikely(!pd))
1227 return NF_ACCEPT;
1228 pp = pd->pp;
1229
1230 /* reassemble IP fragments */
1231 #ifdef CONFIG_IP_VS_IPV6
1232 if (af == AF_INET)
1233 #endif
1234 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1235 if (ip_vs_gather_frags(skb,
1236 ip_vs_defrag_user(hooknum)))
1237 return NF_STOLEN;
1238
1239 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
1240 }
1241
1242 /*
1243 * Check if the packet belongs to an existing entry
1244 */
1245 cp = pp->conn_out_get(ipvs, af, skb, &iph);
1246
1247 if (likely(cp))
1248 return handle_response(af, skb, pd, cp, &iph, hooknum);
1249 if (sysctl_nat_icmp_send(ipvs) &&
1250 (pp->protocol == IPPROTO_TCP ||
1251 pp->protocol == IPPROTO_UDP ||
1252 pp->protocol == IPPROTO_SCTP)) {
1253 __be16 _ports[2], *pptr;
1254
1255 pptr = frag_safe_skb_hp(skb, iph.len,
1256 sizeof(_ports), _ports, &iph);
1257 if (pptr == NULL)
1258 return NF_ACCEPT; /* Not for me */
1259 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
1260 pptr[0])) {
1261 /*
1262 * Notify the real server: there is no
1263 * existing entry if it is not RST
1264 * packet or not TCP packet.
1265 */
1266 if ((iph.protocol != IPPROTO_TCP &&
1267 iph.protocol != IPPROTO_SCTP)
1268 || ((iph.protocol == IPPROTO_TCP
1269 && !is_tcp_reset(skb, iph.len))
1270 || (iph.protocol == IPPROTO_SCTP
1271 && !is_sctp_abort(skb,
1272 iph.len)))) {
1273 #ifdef CONFIG_IP_VS_IPV6
1274 if (af == AF_INET6) {
1275 if (!skb->dev)
1276 skb->dev = net->loopback_dev;
1277 icmpv6_send(skb,
1278 ICMPV6_DEST_UNREACH,
1279 ICMPV6_PORT_UNREACH,
1280 0);
1281 } else
1282 #endif
1283 icmp_send(skb,
1284 ICMP_DEST_UNREACH,
1285 ICMP_PORT_UNREACH, 0);
1286 return NF_DROP;
1287 }
1288 }
1289 }
1290 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
1291 "ip_vs_out: packet continues traversal as normal");
1292 return NF_ACCEPT;
1293 }
1294
1295 /*
1296 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1297 * used only for VS/NAT.
1298 * Check if packet is reply for established ip_vs_conn.
1299 */
1300 static unsigned int
1301 ip_vs_reply4(void *priv, struct sk_buff *skb,
1302 const struct nf_hook_state *state)
1303 {
1304 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1305 }
1306
1307 /*
1308 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1309 * Check if packet is reply for established ip_vs_conn.
1310 */
1311 static unsigned int
1312 ip_vs_local_reply4(void *priv, struct sk_buff *skb,
1313 const struct nf_hook_state *state)
1314 {
1315 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
1316 }
1317
1318 #ifdef CONFIG_IP_VS_IPV6
1319
1320 /*
1321 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1322 * used only for VS/NAT.
1323 * Check if packet is reply for established ip_vs_conn.
1324 */
1325 static unsigned int
1326 ip_vs_reply6(void *priv, struct sk_buff *skb,
1327 const struct nf_hook_state *state)
1328 {
1329 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1330 }
1331
1332 /*
1333 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1334 * Check if packet is reply for established ip_vs_conn.
1335 */
1336 static unsigned int
1337 ip_vs_local_reply6(void *priv, struct sk_buff *skb,
1338 const struct nf_hook_state *state)
1339 {
1340 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
1341 }
1342
1343 #endif
1344
1345 static unsigned int
1346 ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1347 struct ip_vs_proto_data *pd,
1348 int *verdict, struct ip_vs_conn **cpp,
1349 struct ip_vs_iphdr *iph)
1350 {
1351 struct ip_vs_protocol *pp = pd->pp;
1352
1353 if (!iph->fragoffs) {
1354 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1355 * replayed fragment zero will already have created the cp
1356 */
1357
1358 /* Schedule and create new connection entry into cpp */
1359 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
1360 return 0;
1361 }
1362
1363 if (unlikely(!*cpp)) {
1364 /* sorry, all this trouble for a no-hit :) */
1365 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1366 "ip_vs_in: packet continues traversal as normal");
1367 if (iph->fragoffs) {
1368 /* Fragment that couldn't be mapped to a conn entry
1369 * is missing module nf_defrag_ipv6
1370 */
1371 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1372 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1373 "unhandled fragment");
1374 }
1375 *verdict = NF_ACCEPT;
1376 return 0;
1377 }
1378
1379 return 1;
1380 }
1381
1382 /*
1383 * Handle ICMP messages in the outside-to-inside direction (incoming).
1384 * Find any that might be relevant, check against existing connections,
1385 * forward to the right destination host if relevant.
1386 * Currently handles error types - unreachable, quench, ttl exceeded.
1387 */
1388 static int
1389 ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1390 unsigned int hooknum)
1391 {
1392 struct iphdr *iph;
1393 struct icmphdr _icmph, *ic;
1394 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1395 struct ip_vs_iphdr ciph;
1396 struct ip_vs_conn *cp;
1397 struct ip_vs_protocol *pp;
1398 struct ip_vs_proto_data *pd;
1399 unsigned int offset, offset2, ihl, verdict;
1400 bool ipip, new_cp = false;
1401
1402 *related = 1;
1403
1404 /* reassemble IP fragments */
1405 if (ip_is_fragment(ip_hdr(skb))) {
1406 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1407 return NF_STOLEN;
1408 }
1409
1410 iph = ip_hdr(skb);
1411 offset = ihl = iph->ihl * 4;
1412 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1413 if (ic == NULL)
1414 return NF_DROP;
1415
1416 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1417 ic->type, ntohs(icmp_id(ic)),
1418 &iph->saddr, &iph->daddr);
1419
1420 /*
1421 * Work through seeing if this is for us.
1422 * These checks are supposed to be in an order that means easy
1423 * things are checked first to speed up processing.... however
1424 * this means that some packets will manage to get a long way
1425 * down this stack and then be rejected, but that's life.
1426 */
1427 if ((ic->type != ICMP_DEST_UNREACH) &&
1428 (ic->type != ICMP_SOURCE_QUENCH) &&
1429 (ic->type != ICMP_TIME_EXCEEDED)) {
1430 *related = 0;
1431 return NF_ACCEPT;
1432 }
1433
1434 /* Now find the contained IP header */
1435 offset += sizeof(_icmph);
1436 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1437 if (cih == NULL)
1438 return NF_ACCEPT; /* The packet looks wrong, ignore */
1439
1440 /* Special case for errors for IPIP packets */
1441 ipip = false;
1442 if (cih->protocol == IPPROTO_IPIP) {
1443 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1444 return NF_ACCEPT;
1445 /* Error for our IPIP must arrive at LOCAL_IN */
1446 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1447 return NF_ACCEPT;
1448 offset += cih->ihl * 4;
1449 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1450 if (cih == NULL)
1451 return NF_ACCEPT; /* The packet looks wrong, ignore */
1452 ipip = true;
1453 }
1454
1455 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
1456 if (!pd)
1457 return NF_ACCEPT;
1458 pp = pd->pp;
1459
1460 /* Is the embedded protocol header present? */
1461 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1462 pp->dont_defrag))
1463 return NF_ACCEPT;
1464
1465 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1466 "Checking incoming ICMP for");
1467
1468 offset2 = offset;
1469 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
1470 offset = ciph.len;
1471
1472 /* The embedded headers contain source and dest in reverse order.
1473 * For IPIP this is error for request, not for reply.
1474 */
1475 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
1476
1477 if (!cp) {
1478 int v;
1479
1480 if (!sysctl_schedule_icmp(ipvs))
1481 return NF_ACCEPT;
1482
1483 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
1484 return v;
1485 new_cp = true;
1486 }
1487
1488 verdict = NF_DROP;
1489
1490 /* Ensure the checksum is correct */
1491 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1492 /* Failed checksum! */
1493 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1494 &iph->saddr);
1495 goto out;
1496 }
1497
1498 if (ipip) {
1499 __be32 info = ic->un.gateway;
1500 __u8 type = ic->type;
1501 __u8 code = ic->code;
1502
1503 /* Update the MTU */
1504 if (ic->type == ICMP_DEST_UNREACH &&
1505 ic->code == ICMP_FRAG_NEEDED) {
1506 struct ip_vs_dest *dest = cp->dest;
1507 u32 mtu = ntohs(ic->un.frag.mtu);
1508 __be16 frag_off = cih->frag_off;
1509
1510 /* Strip outer IP and ICMP, go to IPIP header */
1511 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1512 goto ignore_ipip;
1513 offset2 -= ihl + sizeof(_icmph);
1514 skb_reset_network_header(skb);
1515 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1516 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1517 ipv4_update_pmtu(skb, ipvs->net,
1518 mtu, 0, 0, 0, 0);
1519 /* Client uses PMTUD? */
1520 if (!(frag_off & htons(IP_DF)))
1521 goto ignore_ipip;
1522 /* Prefer the resulting PMTU */
1523 if (dest) {
1524 struct ip_vs_dest_dst *dest_dst;
1525
1526 rcu_read_lock();
1527 dest_dst = rcu_dereference(dest->dest_dst);
1528 if (dest_dst)
1529 mtu = dst_mtu(dest_dst->dst_cache);
1530 rcu_read_unlock();
1531 }
1532 if (mtu > 68 + sizeof(struct iphdr))
1533 mtu -= sizeof(struct iphdr);
1534 info = htonl(mtu);
1535 }
1536 /* Strip outer IP, ICMP and IPIP, go to IP header of
1537 * original request.
1538 */
1539 if (pskb_pull(skb, offset2) == NULL)
1540 goto ignore_ipip;
1541 skb_reset_network_header(skb);
1542 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1543 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1544 type, code, ntohl(info));
1545 icmp_send(skb, type, code, info);
1546 /* ICMP can be shorter but anyways, account it */
1547 ip_vs_out_stats(cp, skb);
1548
1549 ignore_ipip:
1550 consume_skb(skb);
1551 verdict = NF_STOLEN;
1552 goto out;
1553 }
1554
1555 /* do the statistics and put it back */
1556 ip_vs_in_stats(cp, skb);
1557 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1558 IPPROTO_SCTP == cih->protocol)
1559 offset += 2 * sizeof(__u16);
1560 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1561
1562 out:
1563 if (likely(!new_cp))
1564 __ip_vs_conn_put(cp);
1565 else
1566 ip_vs_conn_put(cp);
1567
1568 return verdict;
1569 }
1570
1571 #ifdef CONFIG_IP_VS_IPV6
1572 static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1573 int *related, unsigned int hooknum,
1574 struct ip_vs_iphdr *iph)
1575 {
1576 struct icmp6hdr _icmph, *ic;
1577 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1578 struct ip_vs_conn *cp;
1579 struct ip_vs_protocol *pp;
1580 struct ip_vs_proto_data *pd;
1581 unsigned int offset, verdict;
1582 bool new_cp = false;
1583
1584 *related = 1;
1585
1586 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1587 if (ic == NULL)
1588 return NF_DROP;
1589
1590 /*
1591 * Work through seeing if this is for us.
1592 * These checks are supposed to be in an order that means easy
1593 * things are checked first to speed up processing.... however
1594 * this means that some packets will manage to get a long way
1595 * down this stack and then be rejected, but that's life.
1596 */
1597 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1598 *related = 0;
1599 return NF_ACCEPT;
1600 }
1601 /* Fragment header that is before ICMP header tells us that:
1602 * it's not an error message since they can't be fragmented.
1603 */
1604 if (iph->flags & IP6_FH_F_FRAG)
1605 return NF_DROP;
1606
1607 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1608 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1609 &iph->saddr, &iph->daddr);
1610
1611 offset = iph->len + sizeof(_icmph);
1612 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
1613 return NF_ACCEPT;
1614
1615 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
1616 if (!pd)
1617 return NF_ACCEPT;
1618 pp = pd->pp;
1619
1620 /* Cannot handle fragmented embedded protocol */
1621 if (ciph.fragoffs)
1622 return NF_ACCEPT;
1623
1624 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1625 "Checking incoming ICMPv6 for");
1626
1627 /* The embedded headers contain source and dest in reverse order
1628 * if not from localhost
1629 */
1630 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
1631
1632 if (!cp) {
1633 int v;
1634
1635 if (!sysctl_schedule_icmp(ipvs))
1636 return NF_ACCEPT;
1637
1638 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
1639 return v;
1640
1641 new_cp = true;
1642 }
1643
1644 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1645 if ((hooknum == NF_INET_LOCAL_OUT) &&
1646 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1647 verdict = NF_ACCEPT;
1648 goto out;
1649 }
1650
1651 /* do the statistics and put it back */
1652 ip_vs_in_stats(cp, skb);
1653
1654 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1655 offset = ciph.len;
1656 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1657 IPPROTO_SCTP == ciph.protocol)
1658 offset += 2 * sizeof(__u16); /* Also mangle ports */
1659
1660 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
1661
1662 out:
1663 if (likely(!new_cp))
1664 __ip_vs_conn_put(cp);
1665 else
1666 ip_vs_conn_put(cp);
1667
1668 return verdict;
1669 }
1670 #endif
1671
1672
1673 /*
1674 * Check if it's for virtual services, look it up,
1675 * and send it on its way...
1676 */
1677 static unsigned int
1678 ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1679 {
1680 struct ip_vs_iphdr iph;
1681 struct ip_vs_protocol *pp;
1682 struct ip_vs_proto_data *pd;
1683 struct ip_vs_conn *cp;
1684 int ret, pkts;
1685 int conn_reuse_mode;
1686
1687 /* Already marked as IPVS request or reply? */
1688 if (skb->ipvs_property)
1689 return NF_ACCEPT;
1690
1691 /*
1692 * Big tappo:
1693 * - remote client: only PACKET_HOST
1694 * - route: used for struct net when skb->dev is unset
1695 */
1696 if (unlikely((skb->pkt_type != PACKET_HOST &&
1697 hooknum != NF_INET_LOCAL_OUT) ||
1698 !skb_dst(skb))) {
1699 ip_vs_fill_iph_skb(af, skb, false, &iph);
1700 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1701 " ignored in hook %u\n",
1702 skb->pkt_type, iph.protocol,
1703 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1704 return NF_ACCEPT;
1705 }
1706 /* ipvs enabled in this netns ? */
1707 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1708 return NF_ACCEPT;
1709
1710 ip_vs_fill_iph_skb(af, skb, false, &iph);
1711
1712 /* Bad... Do not break raw sockets */
1713 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1714 af == AF_INET)) {
1715 struct sock *sk = skb->sk;
1716 struct inet_sock *inet = inet_sk(skb->sk);
1717
1718 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1719 return NF_ACCEPT;
1720 }
1721
1722 #ifdef CONFIG_IP_VS_IPV6
1723 if (af == AF_INET6) {
1724 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1725 int related;
1726 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1727 hooknum, &iph);
1728
1729 if (related)
1730 return verdict;
1731 }
1732 } else
1733 #endif
1734 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1735 int related;
1736 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1737 hooknum);
1738
1739 if (related)
1740 return verdict;
1741 }
1742
1743 /* Protocol supported? */
1744 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
1745 if (unlikely(!pd)) {
1746 /* The only way we'll see this packet again is if it's
1747 * encapsulated, so mark it with ipvs_property=1 so we
1748 * skip it if we're ignoring tunneled packets
1749 */
1750 if (sysctl_ignore_tunneled(ipvs))
1751 skb->ipvs_property = 1;
1752
1753 return NF_ACCEPT;
1754 }
1755 pp = pd->pp;
1756 /*
1757 * Check if the packet belongs to an existing connection entry
1758 */
1759 cp = pp->conn_in_get(ipvs, af, skb, &iph);
1760
1761 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1762 if (conn_reuse_mode && !iph.fragoffs &&
1763 is_new_conn(skb, &iph) && cp &&
1764 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1765 unlikely(!atomic_read(&cp->dest->weight))) ||
1766 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1767 if (!atomic_read(&cp->n_control))
1768 ip_vs_conn_expire_now(cp);
1769 __ip_vs_conn_put(cp);
1770 cp = NULL;
1771 }
1772
1773 if (unlikely(!cp)) {
1774 int v;
1775
1776 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
1777 return v;
1778 }
1779
1780 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
1781
1782 /* Check the server status */
1783 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1784 /* the destination server is not available */
1785
1786 if (sysctl_expire_nodest_conn(ipvs)) {
1787 /* try to expire the connection immediately */
1788 ip_vs_conn_expire_now(cp);
1789 }
1790 /* don't restart its timer, and silently
1791 drop the packet. */
1792 __ip_vs_conn_put(cp);
1793 return NF_DROP;
1794 }
1795
1796 ip_vs_in_stats(cp, skb);
1797 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1798 if (cp->packet_xmit)
1799 ret = cp->packet_xmit(skb, cp, pp, &iph);
1800 /* do not touch skb anymore */
1801 else {
1802 IP_VS_DBG_RL("warning: packet_xmit is null");
1803 ret = NF_ACCEPT;
1804 }
1805
1806 /* Increase its packet counter and check if it is needed
1807 * to be synchronized
1808 *
1809 * Sync connection if it is about to close to
1810 * encorage the standby servers to update the connections timeout
1811 *
1812 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1813 */
1814
1815 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1816 pkts = sysctl_sync_threshold(ipvs);
1817 else
1818 pkts = atomic_add_return(1, &cp->in_pkts);
1819
1820 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1821 ip_vs_sync_conn(ipvs, cp, pkts);
1822
1823 ip_vs_conn_put(cp);
1824 return ret;
1825 }
1826
1827 /*
1828 * AF_INET handler in NF_INET_LOCAL_IN chain
1829 * Schedule and forward packets from remote clients
1830 */
1831 static unsigned int
1832 ip_vs_remote_request4(void *priv, struct sk_buff *skb,
1833 const struct nf_hook_state *state)
1834 {
1835 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
1836 }
1837
1838 /*
1839 * AF_INET handler in NF_INET_LOCAL_OUT chain
1840 * Schedule and forward packets from local clients
1841 */
1842 static unsigned int
1843 ip_vs_local_request4(void *priv, struct sk_buff *skb,
1844 const struct nf_hook_state *state)
1845 {
1846 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
1847 }
1848
1849 #ifdef CONFIG_IP_VS_IPV6
1850
1851 /*
1852 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1853 * Schedule and forward packets from remote clients
1854 */
1855 static unsigned int
1856 ip_vs_remote_request6(void *priv, struct sk_buff *skb,
1857 const struct nf_hook_state *state)
1858 {
1859 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
1860 }
1861
1862 /*
1863 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1864 * Schedule and forward packets from local clients
1865 */
1866 static unsigned int
1867 ip_vs_local_request6(void *priv, struct sk_buff *skb,
1868 const struct nf_hook_state *state)
1869 {
1870 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
1871 }
1872
1873 #endif
1874
1875
1876 /*
1877 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1878 * related packets destined for 0.0.0.0/0.
1879 * When fwmark-based virtual service is used, such as transparent
1880 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1881 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1882 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1883 * and send them to ip_vs_in_icmp.
1884 */
1885 static unsigned int
1886 ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
1887 const struct nf_hook_state *state)
1888 {
1889 int r;
1890 struct netns_ipvs *ipvs = net_ipvs(state->net);
1891
1892 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1893 return NF_ACCEPT;
1894
1895 /* ipvs enabled in this netns ? */
1896 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1897 return NF_ACCEPT;
1898
1899 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
1900 }
1901
1902 #ifdef CONFIG_IP_VS_IPV6
1903 static unsigned int
1904 ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
1905 const struct nf_hook_state *state)
1906 {
1907 int r;
1908 struct netns_ipvs *ipvs = net_ipvs(state->net);
1909 struct ip_vs_iphdr iphdr;
1910
1911 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
1912 if (iphdr.protocol != IPPROTO_ICMPV6)
1913 return NF_ACCEPT;
1914
1915 /* ipvs enabled in this netns ? */
1916 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1917 return NF_ACCEPT;
1918
1919 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
1920 }
1921 #endif
1922
1923
1924 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1925 /* After packet filtering, change source only for VS/NAT */
1926 {
1927 .hook = ip_vs_reply4,
1928 .owner = THIS_MODULE,
1929 .pf = NFPROTO_IPV4,
1930 .hooknum = NF_INET_LOCAL_IN,
1931 .priority = NF_IP_PRI_NAT_SRC - 2,
1932 },
1933 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1934 * or VS/NAT(change destination), so that filtering rules can be
1935 * applied to IPVS. */
1936 {
1937 .hook = ip_vs_remote_request4,
1938 .owner = THIS_MODULE,
1939 .pf = NFPROTO_IPV4,
1940 .hooknum = NF_INET_LOCAL_IN,
1941 .priority = NF_IP_PRI_NAT_SRC - 1,
1942 },
1943 /* Before ip_vs_in, change source only for VS/NAT */
1944 {
1945 .hook = ip_vs_local_reply4,
1946 .owner = THIS_MODULE,
1947 .pf = NFPROTO_IPV4,
1948 .hooknum = NF_INET_LOCAL_OUT,
1949 .priority = NF_IP_PRI_NAT_DST + 1,
1950 },
1951 /* After mangle, schedule and forward local requests */
1952 {
1953 .hook = ip_vs_local_request4,
1954 .owner = THIS_MODULE,
1955 .pf = NFPROTO_IPV4,
1956 .hooknum = NF_INET_LOCAL_OUT,
1957 .priority = NF_IP_PRI_NAT_DST + 2,
1958 },
1959 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1960 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1961 {
1962 .hook = ip_vs_forward_icmp,
1963 .owner = THIS_MODULE,
1964 .pf = NFPROTO_IPV4,
1965 .hooknum = NF_INET_FORWARD,
1966 .priority = 99,
1967 },
1968 /* After packet filtering, change source only for VS/NAT */
1969 {
1970 .hook = ip_vs_reply4,
1971 .owner = THIS_MODULE,
1972 .pf = NFPROTO_IPV4,
1973 .hooknum = NF_INET_FORWARD,
1974 .priority = 100,
1975 },
1976 #ifdef CONFIG_IP_VS_IPV6
1977 /* After packet filtering, change source only for VS/NAT */
1978 {
1979 .hook = ip_vs_reply6,
1980 .owner = THIS_MODULE,
1981 .pf = NFPROTO_IPV6,
1982 .hooknum = NF_INET_LOCAL_IN,
1983 .priority = NF_IP6_PRI_NAT_SRC - 2,
1984 },
1985 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1986 * or VS/NAT(change destination), so that filtering rules can be
1987 * applied to IPVS. */
1988 {
1989 .hook = ip_vs_remote_request6,
1990 .owner = THIS_MODULE,
1991 .pf = NFPROTO_IPV6,
1992 .hooknum = NF_INET_LOCAL_IN,
1993 .priority = NF_IP6_PRI_NAT_SRC - 1,
1994 },
1995 /* Before ip_vs_in, change source only for VS/NAT */
1996 {
1997 .hook = ip_vs_local_reply6,
1998 .owner = THIS_MODULE,
1999 .pf = NFPROTO_IPV6,
2000 .hooknum = NF_INET_LOCAL_OUT,
2001 .priority = NF_IP6_PRI_NAT_DST + 1,
2002 },
2003 /* After mangle, schedule and forward local requests */
2004 {
2005 .hook = ip_vs_local_request6,
2006 .owner = THIS_MODULE,
2007 .pf = NFPROTO_IPV6,
2008 .hooknum = NF_INET_LOCAL_OUT,
2009 .priority = NF_IP6_PRI_NAT_DST + 2,
2010 },
2011 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2012 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2013 {
2014 .hook = ip_vs_forward_icmp_v6,
2015 .owner = THIS_MODULE,
2016 .pf = NFPROTO_IPV6,
2017 .hooknum = NF_INET_FORWARD,
2018 .priority = 99,
2019 },
2020 /* After packet filtering, change source only for VS/NAT */
2021 {
2022 .hook = ip_vs_reply6,
2023 .owner = THIS_MODULE,
2024 .pf = NFPROTO_IPV6,
2025 .hooknum = NF_INET_FORWARD,
2026 .priority = 100,
2027 },
2028 #endif
2029 };
2030 /*
2031 * Initialize IP Virtual Server netns mem.
2032 */
2033 static int __net_init __ip_vs_init(struct net *net)
2034 {
2035 struct netns_ipvs *ipvs;
2036
2037 ipvs = net_generic(net, ip_vs_net_id);
2038 if (ipvs == NULL)
2039 return -ENOMEM;
2040
2041 /* Hold the beast until a service is registerd */
2042 ipvs->enable = 0;
2043 ipvs->net = net;
2044 /* Counters used for creating unique names */
2045 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2046 atomic_inc(&ipvs_netns_cnt);
2047 net->ipvs = ipvs;
2048
2049 if (ip_vs_estimator_net_init(ipvs) < 0)
2050 goto estimator_fail;
2051
2052 if (ip_vs_control_net_init(ipvs) < 0)
2053 goto control_fail;
2054
2055 if (ip_vs_protocol_net_init(ipvs) < 0)
2056 goto protocol_fail;
2057
2058 if (ip_vs_app_net_init(ipvs) < 0)
2059 goto app_fail;
2060
2061 if (ip_vs_conn_net_init(ipvs) < 0)
2062 goto conn_fail;
2063
2064 if (ip_vs_sync_net_init(ipvs) < 0)
2065 goto sync_fail;
2066
2067 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
2068 sizeof(struct netns_ipvs), ipvs->gen);
2069 return 0;
2070 /*
2071 * Error handling
2072 */
2073
2074 sync_fail:
2075 ip_vs_conn_net_cleanup(ipvs);
2076 conn_fail:
2077 ip_vs_app_net_cleanup(ipvs);
2078 app_fail:
2079 ip_vs_protocol_net_cleanup(ipvs);
2080 protocol_fail:
2081 ip_vs_control_net_cleanup(ipvs);
2082 control_fail:
2083 ip_vs_estimator_net_cleanup(ipvs);
2084 estimator_fail:
2085 net->ipvs = NULL;
2086 return -ENOMEM;
2087 }
2088
2089 static void __net_exit __ip_vs_cleanup(struct net *net)
2090 {
2091 struct netns_ipvs *ipvs = net_ipvs(net);
2092
2093 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
2094 ip_vs_conn_net_cleanup(ipvs);
2095 ip_vs_app_net_cleanup(ipvs);
2096 ip_vs_protocol_net_cleanup(ipvs);
2097 ip_vs_control_net_cleanup(ipvs);
2098 ip_vs_estimator_net_cleanup(ipvs);
2099 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
2100 net->ipvs = NULL;
2101 }
2102
2103 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2104 {
2105 struct netns_ipvs *ipvs = net_ipvs(net);
2106 EnterFunction(2);
2107 ipvs->enable = 0; /* Disable packet reception */
2108 smp_wmb();
2109 ip_vs_sync_net_cleanup(ipvs);
2110 LeaveFunction(2);
2111 }
2112
2113 static struct pernet_operations ipvs_core_ops = {
2114 .init = __ip_vs_init,
2115 .exit = __ip_vs_cleanup,
2116 .id = &ip_vs_net_id,
2117 .size = sizeof(struct netns_ipvs),
2118 };
2119
2120 static struct pernet_operations ipvs_core_dev_ops = {
2121 .exit = __ip_vs_dev_cleanup,
2122 };
2123
2124 /*
2125 * Initialize IP Virtual Server
2126 */
2127 static int __init ip_vs_init(void)
2128 {
2129 int ret;
2130
2131 ret = ip_vs_control_init();
2132 if (ret < 0) {
2133 pr_err("can't setup control.\n");
2134 goto exit;
2135 }
2136
2137 ip_vs_protocol_init();
2138
2139 ret = ip_vs_conn_init();
2140 if (ret < 0) {
2141 pr_err("can't setup connection table.\n");
2142 goto cleanup_protocol;
2143 }
2144
2145 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2146 if (ret < 0)
2147 goto cleanup_conn;
2148
2149 ret = register_pernet_device(&ipvs_core_dev_ops);
2150 if (ret < 0)
2151 goto cleanup_sub;
2152
2153 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2154 if (ret < 0) {
2155 pr_err("can't register hooks.\n");
2156 goto cleanup_dev;
2157 }
2158
2159 ret = ip_vs_register_nl_ioctl();
2160 if (ret < 0) {
2161 pr_err("can't register netlink/ioctl.\n");
2162 goto cleanup_hooks;
2163 }
2164
2165 pr_info("ipvs loaded.\n");
2166
2167 return ret;
2168
2169 cleanup_hooks:
2170 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2171 cleanup_dev:
2172 unregister_pernet_device(&ipvs_core_dev_ops);
2173 cleanup_sub:
2174 unregister_pernet_subsys(&ipvs_core_ops);
2175 cleanup_conn:
2176 ip_vs_conn_cleanup();
2177 cleanup_protocol:
2178 ip_vs_protocol_cleanup();
2179 ip_vs_control_cleanup();
2180 exit:
2181 return ret;
2182 }
2183
2184 static void __exit ip_vs_cleanup(void)
2185 {
2186 ip_vs_unregister_nl_ioctl();
2187 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2188 unregister_pernet_device(&ipvs_core_dev_ops);
2189 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
2190 ip_vs_conn_cleanup();
2191 ip_vs_protocol_cleanup();
2192 ip_vs_control_cleanup();
2193 pr_info("ipvs unloaded.\n");
2194 }
2195
2196 module_init(ip_vs_init);
2197 module_exit(ip_vs_cleanup);
2198 MODULE_LICENSE("GPL");
This page took 0.073384 seconds and 4 git commands to generate.