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