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