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