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