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