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