~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/ipvs/ip_vs_core.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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                                  unsigned int hooknum)
663 {
664         if (!sysctl_snat_reroute(skb))
665                 return 0;
666         /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
667         if (NF_INET_LOCAL_IN == hooknum)
668                 return 0;
669 #ifdef CONFIG_IP_VS_IPV6
670         if (af == AF_INET6) {
671                 struct dst_entry *dst = skb_dst(skb);
672 
673                 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
674                     ip6_route_me_harder(skb) != 0)
675                         return 1;
676         } else
677 #endif
678                 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
679                     ip_route_me_harder(skb, RTN_LOCAL) != 0)
680                         return 1;
681 
682         return 0;
683 }
684 
685 /*
686  * Packet has been made sufficiently writable in caller
687  * - inout: 1=in->out, 0=out->in
688  */
689 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
690                     struct ip_vs_conn *cp, int inout)
691 {
692         struct iphdr *iph        = ip_hdr(skb);
693         unsigned int icmp_offset = iph->ihl*4;
694         struct icmphdr *icmph    = (struct icmphdr *)(skb_network_header(skb) +
695                                                       icmp_offset);
696         struct iphdr *ciph       = (struct iphdr *)(icmph + 1);
697 
698         if (inout) {
699                 iph->saddr = cp->vaddr.ip;
700                 ip_send_check(iph);
701                 ciph->daddr = cp->vaddr.ip;
702                 ip_send_check(ciph);
703         } else {
704                 iph->daddr = cp->daddr.ip;
705                 ip_send_check(iph);
706                 ciph->saddr = cp->daddr.ip;
707                 ip_send_check(ciph);
708         }
709 
710         /* the TCP/UDP/SCTP port */
711         if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
712             IPPROTO_SCTP == ciph->protocol) {
713                 __be16 *ports = (void *)ciph + ciph->ihl*4;
714 
715                 if (inout)
716                         ports[1] = cp->vport;
717                 else
718                         ports[0] = cp->dport;
719         }
720 
721         /* And finally the ICMP checksum */
722         icmph->checksum = 0;
723         icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
724         skb->ip_summed = CHECKSUM_UNNECESSARY;
725 
726         if (inout)
727                 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
728                         "Forwarding altered outgoing ICMP");
729         else
730                 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
731                         "Forwarding altered incoming ICMP");
732 }
733 
734 #ifdef CONFIG_IP_VS_IPV6
735 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
736                     struct ip_vs_conn *cp, int inout)
737 {
738         struct ipv6hdr *iph      = ipv6_hdr(skb);
739         unsigned int icmp_offset = 0;
740         unsigned int offs        = 0; /* header offset*/
741         int protocol;
742         struct icmp6hdr *icmph;
743         struct ipv6hdr *ciph;
744         unsigned short fragoffs;
745 
746         ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
747         icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
748         offs = icmp_offset + sizeof(struct icmp6hdr);
749         ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
750 
751         protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
752 
753         if (inout) {
754                 iph->saddr = cp->vaddr.in6;
755                 ciph->daddr = cp->vaddr.in6;
756         } else {
757                 iph->daddr = cp->daddr.in6;
758                 ciph->saddr = cp->daddr.in6;
759         }
760 
761         /* the TCP/UDP/SCTP port */
762         if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
763                           IPPROTO_SCTP == protocol)) {
764                 __be16 *ports = (void *)(skb_network_header(skb) + offs);
765 
766                 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
767                               ntohs(inout ? ports[1] : ports[0]),
768                               ntohs(inout ? cp->vport : cp->dport));
769                 if (inout)
770                         ports[1] = cp->vport;
771                 else
772                         ports[0] = cp->dport;
773         }
774 
775         /* And finally the ICMP checksum */
776         icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
777                                               skb->len - icmp_offset,
778                                               IPPROTO_ICMPV6, 0);
779         skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
780         skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
781         skb->ip_summed = CHECKSUM_PARTIAL;
782 
783         if (inout)
784                 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
785                               (void *)ciph - (void *)iph,
786                               "Forwarding altered outgoing ICMPv6");
787         else
788                 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
789                               (void *)ciph - (void *)iph,
790                               "Forwarding altered incoming ICMPv6");
791 }
792 #endif
793 
794 /* Handle relevant response ICMP messages - forward to the right
795  * destination host.
796  */
797 static int handle_response_icmp(int af, struct sk_buff *skb,
798                                 union nf_inet_addr *snet,
799                                 __u8 protocol, struct ip_vs_conn *cp,
800                                 struct ip_vs_protocol *pp,
801                                 unsigned int offset, unsigned int ihl,
802                                 unsigned int hooknum)
803 {
804         unsigned int verdict = NF_DROP;
805 
806         if (IP_VS_FWD_METHOD(cp) != 0) {
807                 pr_err("shouldn't reach here, because the box is on the "
808                        "half connection in the tun/dr module.\n");
809         }
810 
811         /* Ensure the checksum is correct */
812         if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
813                 /* Failed checksum! */
814                 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
815                               IP_VS_DBG_ADDR(af, snet));
816                 goto out;
817         }
818 
819         if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
820             IPPROTO_SCTP == protocol)
821                 offset += 2 * sizeof(__u16);
822         if (!skb_make_writable(skb, offset))
823                 goto out;
824 
825 #ifdef CONFIG_IP_VS_IPV6
826         if (af == AF_INET6)
827                 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
828         else
829 #endif
830                 ip_vs_nat_icmp(skb, pp, cp, 1);
831 
832         if (ip_vs_route_me_harder(af, skb, hooknum))
833                 goto out;
834 
835         /* do the statistics and put it back */
836         ip_vs_out_stats(cp, skb);
837 
838         skb->ipvs_property = 1;
839         if (!(cp->flags & IP_VS_CONN_F_NFCT))
840                 ip_vs_notrack(skb);
841         else
842                 ip_vs_update_conntrack(skb, cp, 0);
843         verdict = NF_ACCEPT;
844 
845 out:
846         __ip_vs_conn_put(cp);
847 
848         return verdict;
849 }
850 
851 /*
852  *      Handle ICMP messages in the inside-to-outside direction (outgoing).
853  *      Find any that might be relevant, check against existing connections.
854  *      Currently handles error types - unreachable, quench, ttl exceeded.
855  */
856 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
857                           unsigned int hooknum)
858 {
859         struct iphdr *iph;
860         struct icmphdr  _icmph, *ic;
861         struct iphdr    _ciph, *cih;    /* The ip header contained within the ICMP */
862         struct ip_vs_iphdr ciph;
863         struct ip_vs_conn *cp;
864         struct ip_vs_protocol *pp;
865         unsigned int offset, ihl;
866         union nf_inet_addr snet;
867 
868         *related = 1;
869 
870         /* reassemble IP fragments */
871         if (ip_is_fragment(ip_hdr(skb))) {
872                 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
873                         return NF_STOLEN;
874         }
875 
876         iph = ip_hdr(skb);
877         offset = ihl = iph->ihl * 4;
878         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
879         if (ic == NULL)
880                 return NF_DROP;
881 
882         IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
883                   ic->type, ntohs(icmp_id(ic)),
884                   &iph->saddr, &iph->daddr);
885 
886         /*
887          * Work through seeing if this is for us.
888          * These checks are supposed to be in an order that means easy
889          * things are checked first to speed up processing.... however
890          * this means that some packets will manage to get a long way
891          * down this stack and then be rejected, but that's life.
892          */
893         if ((ic->type != ICMP_DEST_UNREACH) &&
894             (ic->type != ICMP_SOURCE_QUENCH) &&
895             (ic->type != ICMP_TIME_EXCEEDED)) {
896                 *related = 0;
897                 return NF_ACCEPT;
898         }
899 
900         /* Now find the contained IP header */
901         offset += sizeof(_icmph);
902         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
903         if (cih == NULL)
904                 return NF_ACCEPT; /* The packet looks wrong, ignore */
905 
906         pp = ip_vs_proto_get(cih->protocol);
907         if (!pp)
908                 return NF_ACCEPT;
909 
910         /* Is the embedded protocol header present? */
911         if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
912                      pp->dont_defrag))
913                 return NF_ACCEPT;
914 
915         IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
916                       "Checking outgoing ICMP for");
917 
918         ip_vs_fill_ip4hdr(cih, &ciph);
919         ciph.len += offset;
920         /* The embedded headers contain source and dest in reverse order */
921         cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
922         if (!cp)
923                 return NF_ACCEPT;
924 
925         snet.ip = iph->saddr;
926         return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
927                                     pp, ciph.len, ihl, hooknum);
928 }
929 
930 #ifdef CONFIG_IP_VS_IPV6
931 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
932                              unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
933 {
934         struct icmp6hdr _icmph, *ic;
935         struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
936         struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
937         struct ip_vs_conn *cp;
938         struct ip_vs_protocol *pp;
939         union nf_inet_addr snet;
940         unsigned int writable;
941 
942         *related = 1;
943         ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
944         if (ic == NULL)
945                 return NF_DROP;
946 
947         /*
948          * Work through seeing if this is for us.
949          * These checks are supposed to be in an order that means easy
950          * things are checked first to speed up processing.... however
951          * this means that some packets will manage to get a long way
952          * down this stack and then be rejected, but that's life.
953          */
954         if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
955                 *related = 0;
956                 return NF_ACCEPT;
957         }
958         /* Fragment header that is before ICMP header tells us that:
959          * it's not an error message since they can't be fragmented.
960          */
961         if (ipvsh->flags & IP6_FH_F_FRAG)
962                 return NF_DROP;
963 
964         IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
965                   ic->icmp6_type, ntohs(icmpv6_id(ic)),
966                   &ipvsh->saddr, &ipvsh->daddr);
967 
968         /* Now find the contained IP header */
969         ciph.len = ipvsh->len + sizeof(_icmph);
970         ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
971         if (ip6h == NULL)
972                 return NF_ACCEPT; /* The packet looks wrong, ignore */
973         ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
974         ciph.daddr.in6 = ip6h->daddr;
975         /* skip possible IPv6 exthdrs of contained IPv6 packet */
976         ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
977         if (ciph.protocol < 0)
978                 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
979 
980         pp = ip_vs_proto_get(ciph.protocol);
981         if (!pp)
982                 return NF_ACCEPT;
983 
984         /* The embedded headers contain source and dest in reverse order */
985         cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
986         if (!cp)
987                 return NF_ACCEPT;
988 
989         snet.in6 = ciph.saddr.in6;
990         writable = ciph.len;
991         return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
992                                     pp, writable, sizeof(struct ipv6hdr),
993                                     hooknum);
994 }
995 #endif
996 
997 /*
998  * Check if sctp chunc is ABORT chunk
999  */
1000 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1001 {
1002         sctp_chunkhdr_t *sch, schunk;
1003         sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1004                         sizeof(schunk), &schunk);
1005         if (sch == NULL)
1006                 return 0;
1007         if (sch->type == SCTP_CID_ABORT)
1008                 return 1;
1009         return 0;
1010 }
1011 
1012 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1013 {
1014         struct tcphdr _tcph, *th;
1015 
1016         th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1017         if (th == NULL)
1018                 return 0;
1019         return th->rst;
1020 }
1021 
1022 static inline bool is_new_conn(const struct sk_buff *skb,
1023                                struct ip_vs_iphdr *iph)
1024 {
1025         switch (iph->protocol) {
1026         case IPPROTO_TCP: {
1027                 struct tcphdr _tcph, *th;
1028 
1029                 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1030                 if (th == NULL)
1031                         return false;
1032                 return th->syn;
1033         }
1034         case IPPROTO_SCTP: {
1035                 sctp_chunkhdr_t *sch, schunk;
1036 
1037                 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1038                                          sizeof(schunk), &schunk);
1039                 if (sch == NULL)
1040                         return false;
1041                 return sch->type == SCTP_CID_INIT;
1042         }
1043         default:
1044                 return false;
1045         }
1046 }
1047 
1048 /* Handle response packets: rewrite addresses and send away...
1049  */
1050 static unsigned int
1051 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1052                 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1053                 unsigned int hooknum)
1054 {
1055         struct ip_vs_protocol *pp = pd->pp;
1056 
1057         IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1058 
1059         if (!skb_make_writable(skb, iph->len))
1060                 goto drop;
1061 
1062         /* mangle the packet */
1063         if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
1064                 goto drop;
1065 
1066 #ifdef CONFIG_IP_VS_IPV6
1067         if (af == AF_INET6)
1068                 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1069         else
1070 #endif
1071         {
1072                 ip_hdr(skb)->saddr = cp->vaddr.ip;
1073                 ip_send_check(ip_hdr(skb));
1074         }
1075 
1076         /*
1077          * nf_iterate does not expect change in the skb->dst->dev.
1078          * It looks like it is not fatal to enable this code for hooks
1079          * where our handlers are at the end of the chain list and
1080          * when all next handlers use skb->dst->dev and not outdev.
1081          * It will definitely route properly the inout NAT traffic
1082          * when multiple paths are used.
1083          */
1084 
1085         /* For policy routing, packets originating from this
1086          * machine itself may be routed differently to packets
1087          * passing through.  We want this packet to be routed as
1088          * if it came from this machine itself.  So re-compute
1089          * the routing information.
1090          */
1091         if (ip_vs_route_me_harder(af, skb, hooknum))
1092                 goto drop;
1093 
1094         IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1095 
1096         ip_vs_out_stats(cp, skb);
1097         ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1098         skb->ipvs_property = 1;
1099         if (!(cp->flags & IP_VS_CONN_F_NFCT))
1100                 ip_vs_notrack(skb);
1101         else
1102                 ip_vs_update_conntrack(skb, cp, 0);
1103         ip_vs_conn_put(cp);
1104 
1105         LeaveFunction(11);
1106         return NF_ACCEPT;
1107 
1108 drop:
1109         ip_vs_conn_put(cp);
1110         kfree_skb(skb);
1111         LeaveFunction(11);
1112         return NF_STOLEN;
1113 }
1114 
1115 /*
1116  *      Check if outgoing packet belongs to the established ip_vs_conn.
1117  */
1118 static unsigned int
1119 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1120 {
1121         struct net *net = NULL;
1122         struct ip_vs_iphdr iph;
1123         struct ip_vs_protocol *pp;
1124         struct ip_vs_proto_data *pd;
1125         struct ip_vs_conn *cp;
1126 
1127         EnterFunction(11);
1128 
1129         /* Already marked as IPVS request or reply? */
1130         if (skb->ipvs_property)
1131                 return NF_ACCEPT;
1132 
1133         /* Bad... Do not break raw sockets */
1134         if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1135                      af == AF_INET)) {
1136                 struct sock *sk = skb->sk;
1137                 struct inet_sock *inet = inet_sk(skb->sk);
1138 
1139                 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1140                         return NF_ACCEPT;
1141         }
1142 
1143         if (unlikely(!skb_dst(skb)))
1144                 return NF_ACCEPT;
1145 
1146         net = skb_net(skb);
1147         if (!net_ipvs(net)->enable)
1148                 return NF_ACCEPT;
1149 
1150         ip_vs_fill_iph_skb(af, skb, &iph);
1151 #ifdef CONFIG_IP_VS_IPV6
1152         if (af == AF_INET6) {
1153                 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1154                         int related;
1155                         int verdict = ip_vs_out_icmp_v6(skb, &related,
1156                                                         hooknum, &iph);
1157 
1158                         if (related)
1159                                 return verdict;
1160                 }
1161         } else
1162 #endif
1163                 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1164                         int related;
1165                         int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1166 
1167                         if (related)
1168                                 return verdict;
1169                 }
1170 
1171         pd = ip_vs_proto_data_get(net, iph.protocol);
1172         if (unlikely(!pd))
1173                 return NF_ACCEPT;
1174         pp = pd->pp;
1175 
1176         /* reassemble IP fragments */
1177 #ifdef CONFIG_IP_VS_IPV6
1178         if (af == AF_INET)
1179 #endif
1180                 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1181                         if (ip_vs_gather_frags(skb,
1182                                                ip_vs_defrag_user(hooknum)))
1183                                 return NF_STOLEN;
1184 
1185                         ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
1186                 }
1187 
1188         /*
1189          * Check if the packet belongs to an existing entry
1190          */
1191         cp = pp->conn_out_get(af, skb, &iph, 0);
1192 
1193         if (likely(cp))
1194                 return handle_response(af, skb, pd, cp, &iph, hooknum);
1195         if (sysctl_nat_icmp_send(net) &&
1196             (pp->protocol == IPPROTO_TCP ||
1197              pp->protocol == IPPROTO_UDP ||
1198              pp->protocol == IPPROTO_SCTP)) {
1199                 __be16 _ports[2], *pptr;
1200 
1201                 pptr = frag_safe_skb_hp(skb, iph.len,
1202                                          sizeof(_ports), _ports, &iph);
1203                 if (pptr == NULL)
1204                         return NF_ACCEPT;       /* Not for me */
1205                 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1206                                            pptr[0])) {
1207                         /*
1208                          * Notify the real server: there is no
1209                          * existing entry if it is not RST
1210                          * packet or not TCP packet.
1211                          */
1212                         if ((iph.protocol != IPPROTO_TCP &&
1213                              iph.protocol != IPPROTO_SCTP)
1214                              || ((iph.protocol == IPPROTO_TCP
1215                                   && !is_tcp_reset(skb, iph.len))
1216                                  || (iph.protocol == IPPROTO_SCTP
1217                                         && !is_sctp_abort(skb,
1218                                                 iph.len)))) {
1219 #ifdef CONFIG_IP_VS_IPV6
1220                                 if (af == AF_INET6) {
1221                                         if (!skb->dev)
1222                                                 skb->dev = net->loopback_dev;
1223                                         icmpv6_send(skb,
1224                                                     ICMPV6_DEST_UNREACH,
1225                                                     ICMPV6_PORT_UNREACH,
1226                                                     0);
1227                                 } else
1228 #endif
1229                                         icmp_send(skb,
1230                                                   ICMP_DEST_UNREACH,
1231                                                   ICMP_PORT_UNREACH, 0);
1232                                 return NF_DROP;
1233                         }
1234                 }
1235         }
1236         IP_VS_DBG_PKT(12, af, pp, skb, 0,
1237                       "ip_vs_out: packet continues traversal as normal");
1238         return NF_ACCEPT;
1239 }
1240 
1241 /*
1242  *      It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1243  *      used only for VS/NAT.
1244  *      Check if packet is reply for established ip_vs_conn.
1245  */
1246 static unsigned int
1247 ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1248              const struct net_device *in, const struct net_device *out,
1249              int (*okfn)(struct sk_buff *))
1250 {
1251         return ip_vs_out(ops->hooknum, skb, AF_INET);
1252 }
1253 
1254 /*
1255  *      It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1256  *      Check if packet is reply for established ip_vs_conn.
1257  */
1258 static unsigned int
1259 ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1260                    const struct net_device *in, const struct net_device *out,
1261                    int (*okfn)(struct sk_buff *))
1262 {
1263         return ip_vs_out(ops->hooknum, skb, AF_INET);
1264 }
1265 
1266 #ifdef CONFIG_IP_VS_IPV6
1267 
1268 /*
1269  *      It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1270  *      used only for VS/NAT.
1271  *      Check if packet is reply for established ip_vs_conn.
1272  */
1273 static unsigned int
1274 ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1275              const struct net_device *in, const struct net_device *out,
1276              int (*okfn)(struct sk_buff *))
1277 {
1278         return ip_vs_out(ops->hooknum, skb, AF_INET6);
1279 }
1280 
1281 /*
1282  *      It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1283  *      Check if packet is reply for established ip_vs_conn.
1284  */
1285 static unsigned int
1286 ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1287                    const struct net_device *in, const struct net_device *out,
1288                    int (*okfn)(struct sk_buff *))
1289 {
1290         return ip_vs_out(ops->hooknum, skb, AF_INET6);
1291 }
1292 
1293 #endif
1294 
1295 /*
1296  *      Handle ICMP messages in the outside-to-inside direction (incoming).
1297  *      Find any that might be relevant, check against existing connections,
1298  *      forward to the right destination host if relevant.
1299  *      Currently handles error types - unreachable, quench, ttl exceeded.
1300  */
1301 static int
1302 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1303 {
1304         struct net *net = NULL;
1305         struct iphdr *iph;
1306         struct icmphdr  _icmph, *ic;
1307         struct iphdr    _ciph, *cih;    /* The ip header contained within the ICMP */
1308         struct ip_vs_iphdr ciph;
1309         struct ip_vs_conn *cp;
1310         struct ip_vs_protocol *pp;
1311         struct ip_vs_proto_data *pd;
1312         unsigned int offset, offset2, ihl, verdict;
1313         bool ipip;
1314 
1315         *related = 1;
1316 
1317         /* reassemble IP fragments */
1318         if (ip_is_fragment(ip_hdr(skb))) {
1319                 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1320                         return NF_STOLEN;
1321         }
1322 
1323         iph = ip_hdr(skb);
1324         offset = ihl = iph->ihl * 4;
1325         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1326         if (ic == NULL)
1327                 return NF_DROP;
1328 
1329         IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1330                   ic->type, ntohs(icmp_id(ic)),
1331                   &iph->saddr, &iph->daddr);
1332 
1333         /*
1334          * Work through seeing if this is for us.
1335          * These checks are supposed to be in an order that means easy
1336          * things are checked first to speed up processing.... however
1337          * this means that some packets will manage to get a long way
1338          * down this stack and then be rejected, but that's life.
1339          */
1340         if ((ic->type != ICMP_DEST_UNREACH) &&
1341             (ic->type != ICMP_SOURCE_QUENCH) &&
1342             (ic->type != ICMP_TIME_EXCEEDED)) {
1343                 *related = 0;
1344                 return NF_ACCEPT;
1345         }
1346 
1347         /* Now find the contained IP header */
1348         offset += sizeof(_icmph);
1349         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1350         if (cih == NULL)
1351                 return NF_ACCEPT; /* The packet looks wrong, ignore */
1352 
1353         net = skb_net(skb);
1354 
1355         /* Special case for errors for IPIP packets */
1356         ipip = false;
1357         if (cih->protocol == IPPROTO_IPIP) {
1358                 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1359                         return NF_ACCEPT;
1360                 /* Error for our IPIP must arrive at LOCAL_IN */
1361                 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1362                         return NF_ACCEPT;
1363                 offset += cih->ihl * 4;
1364                 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1365                 if (cih == NULL)
1366                         return NF_ACCEPT; /* The packet looks wrong, ignore */
1367                 ipip = true;
1368         }
1369 
1370         pd = ip_vs_proto_data_get(net, cih->protocol);
1371         if (!pd)
1372                 return NF_ACCEPT;
1373         pp = pd->pp;
1374 
1375         /* Is the embedded protocol header present? */
1376         if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1377                      pp->dont_defrag))
1378                 return NF_ACCEPT;
1379 
1380         IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1381                       "Checking incoming ICMP for");
1382 
1383         offset2 = offset;
1384         ip_vs_fill_ip4hdr(cih, &ciph);
1385         ciph.len += offset;
1386         offset = ciph.len;
1387         /* The embedded headers contain source and dest in reverse order.
1388          * For IPIP this is error for request, not for reply.
1389          */
1390         cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
1391         if (!cp)
1392                 return NF_ACCEPT;
1393 
1394         verdict = NF_DROP;
1395 
1396         /* Ensure the checksum is correct */
1397         if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1398                 /* Failed checksum! */
1399                 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1400                           &iph->saddr);
1401                 goto out;
1402         }
1403 
1404         if (ipip) {
1405                 __be32 info = ic->un.gateway;
1406                 __u8 type = ic->type;
1407                 __u8 code = ic->code;
1408 
1409                 /* Update the MTU */
1410                 if (ic->type == ICMP_DEST_UNREACH &&
1411                     ic->code == ICMP_FRAG_NEEDED) {
1412                         struct ip_vs_dest *dest = cp->dest;
1413                         u32 mtu = ntohs(ic->un.frag.mtu);
1414                         __be16 frag_off = cih->frag_off;
1415 
1416                         /* Strip outer IP and ICMP, go to IPIP header */
1417                         if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1418                                 goto ignore_ipip;
1419                         offset2 -= ihl + sizeof(_icmph);
1420                         skb_reset_network_header(skb);
1421                         IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1422                                 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1423                         ipv4_update_pmtu(skb, dev_net(skb->dev),
1424                                          mtu, 0, 0, 0, 0);
1425                         /* Client uses PMTUD? */
1426                         if (!(frag_off & htons(IP_DF)))
1427                                 goto ignore_ipip;
1428                         /* Prefer the resulting PMTU */
1429                         if (dest) {
1430                                 struct ip_vs_dest_dst *dest_dst;
1431 
1432                                 rcu_read_lock();
1433                                 dest_dst = rcu_dereference(dest->dest_dst);
1434                                 if (dest_dst)
1435                                         mtu = dst_mtu(dest_dst->dst_cache);
1436                                 rcu_read_unlock();
1437                         }
1438                         if (mtu > 68 + sizeof(struct iphdr))
1439                                 mtu -= sizeof(struct iphdr);
1440                         info = htonl(mtu);
1441                 }
1442                 /* Strip outer IP, ICMP and IPIP, go to IP header of
1443                  * original request.
1444                  */
1445                 if (pskb_pull(skb, offset2) == NULL)
1446                         goto ignore_ipip;
1447                 skb_reset_network_header(skb);
1448                 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1449                         &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1450                         type, code, ntohl(info));
1451                 icmp_send(skb, type, code, info);
1452                 /* ICMP can be shorter but anyways, account it */
1453                 ip_vs_out_stats(cp, skb);
1454 
1455 ignore_ipip:
1456                 consume_skb(skb);
1457                 verdict = NF_STOLEN;
1458                 goto out;
1459         }
1460 
1461         /* do the statistics and put it back */
1462         ip_vs_in_stats(cp, skb);
1463         if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1464             IPPROTO_SCTP == cih->protocol)
1465                 offset += 2 * sizeof(__u16);
1466         verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1467 
1468 out:
1469         __ip_vs_conn_put(cp);
1470 
1471         return verdict;
1472 }
1473 
1474 #ifdef CONFIG_IP_VS_IPV6
1475 static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1476                             unsigned int hooknum, struct ip_vs_iphdr *iph)
1477 {
1478         struct net *net = NULL;
1479         struct ipv6hdr _ip6h, *ip6h;
1480         struct icmp6hdr _icmph, *ic;
1481         struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
1482         struct ip_vs_conn *cp;
1483         struct ip_vs_protocol *pp;
1484         struct ip_vs_proto_data *pd;
1485         unsigned int offs_ciph, writable, verdict;
1486 
1487         *related = 1;
1488 
1489         ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
1490         if (ic == NULL)
1491                 return NF_DROP;
1492 
1493         /*
1494          * Work through seeing if this is for us.
1495          * These checks are supposed to be in an order that means easy
1496          * things are checked first to speed up processing.... however
1497          * this means that some packets will manage to get a long way
1498          * down this stack and then be rejected, but that's life.
1499          */
1500         if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
1501                 *related = 0;
1502                 return NF_ACCEPT;
1503         }
1504         /* Fragment header that is before ICMP header tells us that:
1505          * it's not an error message since they can't be fragmented.
1506          */
1507         if (iph->flags & IP6_FH_F_FRAG)
1508                 return NF_DROP;
1509 
1510         IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1511                   ic->icmp6_type, ntohs(icmpv6_id(ic)),
1512                   &iph->saddr, &iph->daddr);
1513 
1514         /* Now find the contained IP header */
1515         ciph.len = iph->len + sizeof(_icmph);
1516         offs_ciph = ciph.len; /* Save ip header offset */
1517         ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1518         if (ip6h == NULL)
1519                 return NF_ACCEPT; /* The packet looks wrong, ignore */
1520         ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1521         ciph.daddr.in6 = ip6h->daddr;
1522         /* skip possible IPv6 exthdrs of contained IPv6 packet */
1523         ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1524         if (ciph.protocol < 0)
1525                 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
1526 
1527         net = skb_net(skb);
1528         pd = ip_vs_proto_data_get(net, ciph.protocol);
1529         if (!pd)
1530                 return NF_ACCEPT;
1531         pp = pd->pp;
1532 
1533         /* Cannot handle fragmented embedded protocol */
1534         if (ciph.fragoffs)
1535                 return NF_ACCEPT;
1536 
1537         IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
1538                       "Checking incoming ICMPv6 for");
1539 
1540         /* The embedded headers contain source and dest in reverse order
1541          * if not from localhost
1542          */
1543         cp = pp->conn_in_get(AF_INET6, skb, &ciph,
1544                              (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1545 
1546         if (!cp)
1547                 return NF_ACCEPT;
1548         /* VS/TUN, VS/DR and LOCALNODE just let it go */
1549         if ((hooknum == NF_INET_LOCAL_OUT) &&
1550             (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1551                 __ip_vs_conn_put(cp);
1552                 return NF_ACCEPT;
1553         }
1554 
1555         /* do the statistics and put it back */
1556         ip_vs_in_stats(cp, skb);
1557 
1558         /* Need to mangle contained IPv6 header in ICMPv6 packet */
1559         writable = ciph.len;
1560         if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1561             IPPROTO_SCTP == ciph.protocol)
1562                 writable += 2 * sizeof(__u16); /* Also mangle ports */
1563 
1564         verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
1565 
1566         __ip_vs_conn_put(cp);
1567 
1568         return verdict;
1569 }
1570 #endif
1571 
1572 
1573 /*
1574  *      Check if it's for virtual services, look it up,
1575  *      and send it on its way...
1576  */
1577 static unsigned int
1578 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1579 {
1580         struct net *net;
1581         struct ip_vs_iphdr iph;
1582         struct ip_vs_protocol *pp;
1583         struct ip_vs_proto_data *pd;
1584         struct ip_vs_conn *cp;
1585         int ret, pkts;
1586         struct netns_ipvs *ipvs;
1587 
1588         /* Already marked as IPVS request or reply? */
1589         if (skb->ipvs_property)
1590                 return NF_ACCEPT;
1591 
1592         /*
1593          *      Big tappo:
1594          *      - remote client: only PACKET_HOST
1595          *      - route: used for struct net when skb->dev is unset
1596          */
1597         if (unlikely((skb->pkt_type != PACKET_HOST &&
1598                       hooknum != NF_INET_LOCAL_OUT) ||
1599                      !skb_dst(skb))) {
1600                 ip_vs_fill_iph_skb(af, skb, &iph);
1601                 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1602                               " ignored in hook %u\n",
1603                               skb->pkt_type, iph.protocol,
1604                               IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1605                 return NF_ACCEPT;
1606         }
1607         /* ipvs enabled in this netns ? */
1608         net = skb_net(skb);
1609         ipvs = net_ipvs(net);
1610         if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1611                 return NF_ACCEPT;
1612 
1613         ip_vs_fill_iph_skb(af, skb, &iph);
1614 
1615         /* Bad... Do not break raw sockets */
1616         if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1617                      af == AF_INET)) {
1618                 struct sock *sk = skb->sk;
1619                 struct inet_sock *inet = inet_sk(skb->sk);
1620 
1621                 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1622                         return NF_ACCEPT;
1623         }
1624 
1625 #ifdef CONFIG_IP_VS_IPV6
1626         if (af == AF_INET6) {
1627                 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1628                         int related;
1629                         int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1630                                                        &iph);
1631 
1632                         if (related)
1633                                 return verdict;
1634                 }
1635         } else
1636 #endif
1637                 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1638                         int related;
1639                         int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1640 
1641                         if (related)
1642                                 return verdict;
1643                 }
1644 
1645         /* Protocol supported? */
1646         pd = ip_vs_proto_data_get(net, iph.protocol);
1647         if (unlikely(!pd))
1648                 return NF_ACCEPT;
1649         pp = pd->pp;
1650         /*
1651          * Check if the packet belongs to an existing connection entry
1652          */
1653         cp = pp->conn_in_get(af, skb, &iph, 0);
1654 
1655         if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1656             unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1657             is_new_conn(skb, &iph)) {
1658                 ip_vs_conn_expire_now(cp);
1659                 __ip_vs_conn_put(cp);
1660                 cp = NULL;
1661         }
1662 
1663         if (unlikely(!cp) && !iph.fragoffs) {
1664                 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1665                  * replayed fragment zero will already have created the cp
1666                  */
1667                 int v;
1668 
1669                 /* Schedule and create new connection entry into &cp */
1670                 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
1671                         return v;
1672         }
1673 
1674         if (unlikely(!cp)) {
1675                 /* sorry, all this trouble for a no-hit :) */
1676                 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1677                               "ip_vs_in: packet continues traversal as normal");
1678                 if (iph.fragoffs) {
1679                         /* Fragment that couldn't be mapped to a conn entry
1680                          * is missing module nf_defrag_ipv6
1681                          */
1682                         IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1683                         IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1684                 }
1685                 return NF_ACCEPT;
1686         }
1687 
1688         IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1689         /* Check the server status */
1690         if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1691                 /* the destination server is not available */
1692 
1693                 if (sysctl_expire_nodest_conn(ipvs)) {
1694                         /* try to expire the connection immediately */
1695                         ip_vs_conn_expire_now(cp);
1696                 }
1697                 /* don't restart its timer, and silently
1698                    drop the packet. */
1699                 __ip_vs_conn_put(cp);
1700                 return NF_DROP;
1701         }
1702 
1703         ip_vs_in_stats(cp, skb);
1704         ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1705         if (cp->packet_xmit)
1706                 ret = cp->packet_xmit(skb, cp, pp, &iph);
1707                 /* do not touch skb anymore */
1708         else {
1709                 IP_VS_DBG_RL("warning: packet_xmit is null");
1710                 ret = NF_ACCEPT;
1711         }
1712 
1713         /* Increase its packet counter and check if it is needed
1714          * to be synchronized
1715          *
1716          * Sync connection if it is about to close to
1717          * encorage the standby servers to update the connections timeout
1718          *
1719          * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1720          */
1721 
1722         if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1723                 pkts = sysctl_sync_threshold(ipvs);
1724         else
1725                 pkts = atomic_add_return(1, &cp->in_pkts);
1726 
1727         if (ipvs->sync_state & IP_VS_STATE_MASTER)
1728                 ip_vs_sync_conn(net, cp, pkts);
1729 
1730         ip_vs_conn_put(cp);
1731         return ret;
1732 }
1733 
1734 /*
1735  *      AF_INET handler in NF_INET_LOCAL_IN chain
1736  *      Schedule and forward packets from remote clients
1737  */
1738 static unsigned int
1739 ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1740                       const struct net_device *in,
1741                       const struct net_device *out,
1742                       int (*okfn)(struct sk_buff *))
1743 {
1744         return ip_vs_in(ops->hooknum, skb, AF_INET);
1745 }
1746 
1747 /*
1748  *      AF_INET handler in NF_INET_LOCAL_OUT chain
1749  *      Schedule and forward packets from local clients
1750  */
1751 static unsigned int
1752 ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1753                      const struct net_device *in, const struct net_device *out,
1754                      int (*okfn)(struct sk_buff *))
1755 {
1756         return ip_vs_in(ops->hooknum, skb, AF_INET);
1757 }
1758 
1759 #ifdef CONFIG_IP_VS_IPV6
1760 
1761 /*
1762  *      AF_INET6 handler in NF_INET_LOCAL_IN chain
1763  *      Schedule and forward packets from remote clients
1764  */
1765 static unsigned int
1766 ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1767                       const struct net_device *in,
1768                       const struct net_device *out,
1769                       int (*okfn)(struct sk_buff *))
1770 {
1771         return ip_vs_in(ops->hooknum, skb, AF_INET6);
1772 }
1773 
1774 /*
1775  *      AF_INET6 handler in NF_INET_LOCAL_OUT chain
1776  *      Schedule and forward packets from local clients
1777  */
1778 static unsigned int
1779 ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1780                      const struct net_device *in, const struct net_device *out,
1781                      int (*okfn)(struct sk_buff *))
1782 {
1783         return ip_vs_in(ops->hooknum, skb, AF_INET6);
1784 }
1785 
1786 #endif
1787 
1788 
1789 /*
1790  *      It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1791  *      related packets destined for 0.0.0.0/0.
1792  *      When fwmark-based virtual service is used, such as transparent
1793  *      cache cluster, TCP packets can be marked and routed to ip_vs_in,
1794  *      but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1795  *      sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1796  *      and send them to ip_vs_in_icmp.
1797  */
1798 static unsigned int
1799 ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1800                    const struct net_device *in, const struct net_device *out,
1801                    int (*okfn)(struct sk_buff *))
1802 {
1803         int r;
1804         struct net *net;
1805         struct netns_ipvs *ipvs;
1806 
1807         if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1808                 return NF_ACCEPT;
1809 
1810         /* ipvs enabled in this netns ? */
1811         net = skb_net(skb);
1812         ipvs = net_ipvs(net);
1813         if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1814                 return NF_ACCEPT;
1815 
1816         return ip_vs_in_icmp(skb, &r, ops->hooknum);
1817 }
1818 
1819 #ifdef CONFIG_IP_VS_IPV6
1820 static unsigned int
1821 ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1822                       const struct net_device *in, const struct net_device *out,
1823                       int (*okfn)(struct sk_buff *))
1824 {
1825         int r;
1826         struct net *net;
1827         struct netns_ipvs *ipvs;
1828         struct ip_vs_iphdr iphdr;
1829 
1830         ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1831         if (iphdr.protocol != IPPROTO_ICMPV6)
1832                 return NF_ACCEPT;
1833 
1834         /* ipvs enabled in this netns ? */
1835         net = skb_net(skb);
1836         ipvs = net_ipvs(net);
1837         if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1838                 return NF_ACCEPT;
1839 
1840         return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
1841 }
1842 #endif
1843 
1844 
1845 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1846         /* After packet filtering, change source only for VS/NAT */
1847         {
1848                 .hook           = ip_vs_reply4,
1849                 .owner          = THIS_MODULE,
1850                 .pf             = NFPROTO_IPV4,
1851                 .hooknum        = NF_INET_LOCAL_IN,
1852                 .priority       = NF_IP_PRI_NAT_SRC - 2,
1853         },
1854         /* After packet filtering, forward packet through VS/DR, VS/TUN,
1855          * or VS/NAT(change destination), so that filtering rules can be
1856          * applied to IPVS. */
1857         {
1858                 .hook           = ip_vs_remote_request4,
1859                 .owner          = THIS_MODULE,
1860                 .pf             = NFPROTO_IPV4,
1861                 .hooknum        = NF_INET_LOCAL_IN,
1862                 .priority       = NF_IP_PRI_NAT_SRC - 1,
1863         },
1864         /* Before ip_vs_in, change source only for VS/NAT */
1865         {
1866                 .hook           = ip_vs_local_reply4,
1867                 .owner          = THIS_MODULE,
1868                 .pf             = NFPROTO_IPV4,
1869                 .hooknum        = NF_INET_LOCAL_OUT,
1870                 .priority       = NF_IP_PRI_NAT_DST + 1,
1871         },
1872         /* After mangle, schedule and forward local requests */
1873         {
1874                 .hook           = ip_vs_local_request4,
1875                 .owner          = THIS_MODULE,
1876                 .pf             = NFPROTO_IPV4,
1877                 .hooknum        = NF_INET_LOCAL_OUT,
1878                 .priority       = NF_IP_PRI_NAT_DST + 2,
1879         },
1880         /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1881          * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1882         {
1883                 .hook           = ip_vs_forward_icmp,
1884                 .owner          = THIS_MODULE,
1885                 .pf             = NFPROTO_IPV4,
1886                 .hooknum        = NF_INET_FORWARD,
1887                 .priority       = 99,
1888         },
1889         /* After packet filtering, change source only for VS/NAT */
1890         {
1891                 .hook           = ip_vs_reply4,
1892                 .owner          = THIS_MODULE,
1893                 .pf             = NFPROTO_IPV4,
1894                 .hooknum        = NF_INET_FORWARD,
1895                 .priority       = 100,
1896         },
1897 #ifdef CONFIG_IP_VS_IPV6
1898         /* After packet filtering, change source only for VS/NAT */
1899         {
1900                 .hook           = ip_vs_reply6,
1901                 .owner          = THIS_MODULE,
1902                 .pf             = NFPROTO_IPV6,
1903                 .hooknum        = NF_INET_LOCAL_IN,
1904                 .priority       = NF_IP6_PRI_NAT_SRC - 2,
1905         },
1906         /* After packet filtering, forward packet through VS/DR, VS/TUN,
1907          * or VS/NAT(change destination), so that filtering rules can be
1908          * applied to IPVS. */
1909         {
1910                 .hook           = ip_vs_remote_request6,
1911                 .owner          = THIS_MODULE,
1912                 .pf             = NFPROTO_IPV6,
1913                 .hooknum        = NF_INET_LOCAL_IN,
1914                 .priority       = NF_IP6_PRI_NAT_SRC - 1,
1915         },
1916         /* Before ip_vs_in, change source only for VS/NAT */
1917         {
1918                 .hook           = ip_vs_local_reply6,
1919                 .owner          = THIS_MODULE,
1920                 .pf             = NFPROTO_IPV6,
1921                 .hooknum        = NF_INET_LOCAL_OUT,
1922                 .priority       = NF_IP6_PRI_NAT_DST + 1,
1923         },
1924         /* After mangle, schedule and forward local requests */
1925         {
1926                 .hook           = ip_vs_local_request6,
1927                 .owner          = THIS_MODULE,
1928                 .pf             = NFPROTO_IPV6,
1929                 .hooknum        = NF_INET_LOCAL_OUT,
1930                 .priority       = NF_IP6_PRI_NAT_DST + 2,
1931         },
1932         /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1933          * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1934         {
1935                 .hook           = ip_vs_forward_icmp_v6,
1936                 .owner          = THIS_MODULE,
1937                 .pf             = NFPROTO_IPV6,
1938                 .hooknum        = NF_INET_FORWARD,
1939                 .priority       = 99,
1940         },
1941         /* After packet filtering, change source only for VS/NAT */
1942         {
1943                 .hook           = ip_vs_reply6,
1944                 .owner          = THIS_MODULE,
1945                 .pf             = NFPROTO_IPV6,
1946                 .hooknum        = NF_INET_FORWARD,
1947                 .priority       = 100,
1948         },
1949 #endif
1950 };
1951 /*
1952  *      Initialize IP Virtual Server netns mem.
1953  */
1954 static int __net_init __ip_vs_init(struct net *net)
1955 {
1956         struct netns_ipvs *ipvs;
1957 
1958         ipvs = net_generic(net, ip_vs_net_id);
1959         if (ipvs == NULL)
1960                 return -ENOMEM;
1961 
1962         /* Hold the beast until a service is registerd */
1963         ipvs->enable = 0;
1964         ipvs->net = net;
1965         /* Counters used for creating unique names */
1966         ipvs->gen = atomic_read(&ipvs_netns_cnt);
1967         atomic_inc(&ipvs_netns_cnt);
1968         net->ipvs = ipvs;
1969 
1970         if (ip_vs_estimator_net_init(net) < 0)
1971                 goto estimator_fail;
1972 
1973         if (ip_vs_control_net_init(net) < 0)
1974                 goto control_fail;
1975 
1976         if (ip_vs_protocol_net_init(net) < 0)
1977                 goto protocol_fail;
1978 
1979         if (ip_vs_app_net_init(net) < 0)
1980                 goto app_fail;
1981 
1982         if (ip_vs_conn_net_init(net) < 0)
1983                 goto conn_fail;
1984 
1985         if (ip_vs_sync_net_init(net) < 0)
1986                 goto sync_fail;
1987 
1988         printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
1989                          sizeof(struct netns_ipvs), ipvs->gen);
1990         return 0;
1991 /*
1992  * Error handling
1993  */
1994 
1995 sync_fail:
1996         ip_vs_conn_net_cleanup(net);
1997 conn_fail:
1998         ip_vs_app_net_cleanup(net);
1999 app_fail:
2000         ip_vs_protocol_net_cleanup(net);
2001 protocol_fail:
2002         ip_vs_control_net_cleanup(net);
2003 control_fail:
2004         ip_vs_estimator_net_cleanup(net);
2005 estimator_fail:
2006         net->ipvs = NULL;
2007         return -ENOMEM;
2008 }
2009 
2010 static void __net_exit __ip_vs_cleanup(struct net *net)
2011 {
2012         ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2013         ip_vs_conn_net_cleanup(net);
2014         ip_vs_app_net_cleanup(net);
2015         ip_vs_protocol_net_cleanup(net);
2016         ip_vs_control_net_cleanup(net);
2017         ip_vs_estimator_net_cleanup(net);
2018         IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
2019         net->ipvs = NULL;
2020 }
2021 
2022 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2023 {
2024         EnterFunction(2);
2025         net_ipvs(net)->enable = 0;      /* Disable packet reception */
2026         smp_wmb();
2027         ip_vs_sync_net_cleanup(net);
2028         LeaveFunction(2);
2029 }
2030 
2031 static struct pernet_operations ipvs_core_ops = {
2032         .init = __ip_vs_init,
2033         .exit = __ip_vs_cleanup,
2034         .id   = &ip_vs_net_id,
2035         .size = sizeof(struct netns_ipvs),
2036 };
2037 
2038 static struct pernet_operations ipvs_core_dev_ops = {
2039         .exit = __ip_vs_dev_cleanup,
2040 };
2041 
2042 /*
2043  *      Initialize IP Virtual Server
2044  */
2045 static int __init ip_vs_init(void)
2046 {
2047         int ret;
2048 
2049         ret = ip_vs_control_init();
2050         if (ret < 0) {
2051                 pr_err("can't setup control.\n");
2052                 goto exit;
2053         }
2054 
2055         ip_vs_protocol_init();
2056 
2057         ret = ip_vs_conn_init();
2058         if (ret < 0) {
2059                 pr_err("can't setup connection table.\n");
2060                 goto cleanup_protocol;
2061         }
2062 
2063         ret = register_pernet_subsys(&ipvs_core_ops);   /* Alloc ip_vs struct */
2064         if (ret < 0)
2065                 goto cleanup_conn;
2066 
2067         ret = register_pernet_device(&ipvs_core_dev_ops);
2068         if (ret < 0)
2069                 goto cleanup_sub;
2070 
2071         ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2072         if (ret < 0) {
2073                 pr_err("can't register hooks.\n");
2074                 goto cleanup_dev;
2075         }
2076 
2077         ret = ip_vs_register_nl_ioctl();
2078         if (ret < 0) {
2079                 pr_err("can't register netlink/ioctl.\n");
2080                 goto cleanup_hooks;
2081         }
2082 
2083         pr_info("ipvs loaded.\n");
2084 
2085         return ret;
2086 
2087 cleanup_hooks:
2088         nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2089 cleanup_dev:
2090         unregister_pernet_device(&ipvs_core_dev_ops);
2091 cleanup_sub:
2092         unregister_pernet_subsys(&ipvs_core_ops);
2093 cleanup_conn:
2094         ip_vs_conn_cleanup();
2095 cleanup_protocol:
2096         ip_vs_protocol_cleanup();
2097         ip_vs_control_cleanup();
2098 exit:
2099         return ret;
2100 }
2101 
2102 static void __exit ip_vs_cleanup(void)
2103 {
2104         ip_vs_unregister_nl_ioctl();
2105         nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2106         unregister_pernet_device(&ipvs_core_dev_ops);
2107         unregister_pernet_subsys(&ipvs_core_ops);       /* free ip_vs struct */
2108         ip_vs_conn_cleanup();
2109         ip_vs_protocol_cleanup();
2110         ip_vs_control_cleanup();
2111         pr_info("ipvs unloaded.\n");
2112 }
2113 
2114 module_init(ip_vs_init);
2115 module_exit(ip_vs_cleanup);
2116 MODULE_LICENSE("GPL");
2117 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp