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