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