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

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

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ 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  * Changes:
 18  *
 19  */
 20 
 21 #define KMSG_COMPONENT "IPVS"
 22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 23 
 24 #include <linux/module.h>
 25 #include <linux/init.h>
 26 #include <linux/types.h>
 27 #include <linux/capability.h>
 28 #include <linux/fs.h>
 29 #include <linux/sysctl.h>
 30 #include <linux/proc_fs.h>
 31 #include <linux/workqueue.h>
 32 #include <linux/swap.h>
 33 #include <linux/seq_file.h>
 34 #include <linux/slab.h>
 35 
 36 #include <linux/netfilter.h>
 37 #include <linux/netfilter_ipv4.h>
 38 #include <linux/mutex.h>
 39 
 40 #include <net/net_namespace.h>
 41 #include <linux/nsproxy.h>
 42 #include <net/ip.h>
 43 #ifdef CONFIG_IP_VS_IPV6
 44 #include <net/ipv6.h>
 45 #include <net/ip6_route.h>
 46 #endif
 47 #include <net/route.h>
 48 #include <net/sock.h>
 49 #include <net/genetlink.h>
 50 
 51 #include <linux/uaccess.h>
 52 
 53 #include <net/ip_vs.h>
 54 
 55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
 56 static DEFINE_MUTEX(__ip_vs_mutex);
 57 
 58 /* sysctl variables */
 59 
 60 #ifdef CONFIG_IP_VS_DEBUG
 61 static int sysctl_ip_vs_debug_level = 0;
 62 
 63 int ip_vs_get_debug_level(void)
 64 {
 65         return sysctl_ip_vs_debug_level;
 66 }
 67 #endif
 68 
 69 
 70 /*  Protos */
 71 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
 72 
 73 
 74 #ifdef CONFIG_IP_VS_IPV6
 75 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
 76 static bool __ip_vs_addr_is_local_v6(struct net *net,
 77                                      const struct in6_addr *addr)
 78 {
 79         struct flowi6 fl6 = {
 80                 .daddr = *addr,
 81         };
 82         struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
 83         bool is_local;
 84 
 85         is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
 86 
 87         dst_release(dst);
 88         return is_local;
 89 }
 90 #endif
 91 
 92 #ifdef CONFIG_SYSCTL
 93 /*
 94  *      update_defense_level is called from keventd and from sysctl,
 95  *      so it needs to protect itself from softirqs
 96  */
 97 static void update_defense_level(struct netns_ipvs *ipvs)
 98 {
 99         struct sysinfo i;
100         static int old_secure_tcp = 0;
101         int availmem;
102         int nomem;
103         int to_change = -1;
104 
105         /* we only count free and buffered memory (in pages) */
106         si_meminfo(&i);
107         availmem = i.freeram + i.bufferram;
108         /* however in linux 2.5 the i.bufferram is total page cache size,
109            we need adjust it */
110         /* si_swapinfo(&i); */
111         /* availmem = availmem - (i.totalswap - i.freeswap); */
112 
113         nomem = (availmem < ipvs->sysctl_amemthresh);
114 
115         local_bh_disable();
116 
117         /* drop_entry */
118         spin_lock(&ipvs->dropentry_lock);
119         switch (ipvs->sysctl_drop_entry) {
120         case 0:
121                 atomic_set(&ipvs->dropentry, 0);
122                 break;
123         case 1:
124                 if (nomem) {
125                         atomic_set(&ipvs->dropentry, 1);
126                         ipvs->sysctl_drop_entry = 2;
127                 } else {
128                         atomic_set(&ipvs->dropentry, 0);
129                 }
130                 break;
131         case 2:
132                 if (nomem) {
133                         atomic_set(&ipvs->dropentry, 1);
134                 } else {
135                         atomic_set(&ipvs->dropentry, 0);
136                         ipvs->sysctl_drop_entry = 1;
137                 };
138                 break;
139         case 3:
140                 atomic_set(&ipvs->dropentry, 1);
141                 break;
142         }
143         spin_unlock(&ipvs->dropentry_lock);
144 
145         /* drop_packet */
146         spin_lock(&ipvs->droppacket_lock);
147         switch (ipvs->sysctl_drop_packet) {
148         case 0:
149                 ipvs->drop_rate = 0;
150                 break;
151         case 1:
152                 if (nomem) {
153                         ipvs->drop_rate = ipvs->drop_counter
154                                 = ipvs->sysctl_amemthresh /
155                                 (ipvs->sysctl_amemthresh-availmem);
156                         ipvs->sysctl_drop_packet = 2;
157                 } else {
158                         ipvs->drop_rate = 0;
159                 }
160                 break;
161         case 2:
162                 if (nomem) {
163                         ipvs->drop_rate = ipvs->drop_counter
164                                 = ipvs->sysctl_amemthresh /
165                                 (ipvs->sysctl_amemthresh-availmem);
166                 } else {
167                         ipvs->drop_rate = 0;
168                         ipvs->sysctl_drop_packet = 1;
169                 }
170                 break;
171         case 3:
172                 ipvs->drop_rate = ipvs->sysctl_am_droprate;
173                 break;
174         }
175         spin_unlock(&ipvs->droppacket_lock);
176 
177         /* secure_tcp */
178         spin_lock(&ipvs->securetcp_lock);
179         switch (ipvs->sysctl_secure_tcp) {
180         case 0:
181                 if (old_secure_tcp >= 2)
182                         to_change = 0;
183                 break;
184         case 1:
185                 if (nomem) {
186                         if (old_secure_tcp < 2)
187                                 to_change = 1;
188                         ipvs->sysctl_secure_tcp = 2;
189                 } else {
190                         if (old_secure_tcp >= 2)
191                                 to_change = 0;
192                 }
193                 break;
194         case 2:
195                 if (nomem) {
196                         if (old_secure_tcp < 2)
197                                 to_change = 1;
198                 } else {
199                         if (old_secure_tcp >= 2)
200                                 to_change = 0;
201                         ipvs->sysctl_secure_tcp = 1;
202                 }
203                 break;
204         case 3:
205                 if (old_secure_tcp < 2)
206                         to_change = 1;
207                 break;
208         }
209         old_secure_tcp = ipvs->sysctl_secure_tcp;
210         if (to_change >= 0)
211                 ip_vs_protocol_timeout_change(ipvs,
212                                               ipvs->sysctl_secure_tcp > 1);
213         spin_unlock(&ipvs->securetcp_lock);
214 
215         local_bh_enable();
216 }
217 
218 
219 /*
220  *      Timer for checking the defense
221  */
222 #define DEFENSE_TIMER_PERIOD    1*HZ
223 
224 static void defense_work_handler(struct work_struct *work)
225 {
226         struct netns_ipvs *ipvs =
227                 container_of(work, struct netns_ipvs, defense_work.work);
228 
229         update_defense_level(ipvs);
230         if (atomic_read(&ipvs->dropentry))
231                 ip_vs_random_dropentry(ipvs);
232         schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
233 }
234 #endif
235 
236 int
237 ip_vs_use_count_inc(void)
238 {
239         return try_module_get(THIS_MODULE);
240 }
241 
242 void
243 ip_vs_use_count_dec(void)
244 {
245         module_put(THIS_MODULE);
246 }
247 
248 
249 /*
250  *      Hash table: for virtual service lookups
251  */
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
255 
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
260 
261 
262 /*
263  *      Returns hash value for virtual service
264  */
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
267                   const union nf_inet_addr *addr, __be16 port)
268 {
269         register unsigned int porth = ntohs(port);
270         __be32 addr_fold = addr->ip;
271         __u32 ahash;
272 
273 #ifdef CONFIG_IP_VS_IPV6
274         if (af == AF_INET6)
275                 addr_fold = addr->ip6[0]^addr->ip6[1]^
276                             addr->ip6[2]^addr->ip6[3];
277 #endif
278         ahash = ntohl(addr_fold);
279         ahash ^= ((size_t) ipvs >> 8);
280 
281         return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
282                IP_VS_SVC_TAB_MASK;
283 }
284 
285 /*
286  *      Returns hash value of fwmark for virtual service lookup
287  */
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
289 {
290         return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
291 }
292 
293 /*
294  *      Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
295  *      or in the ip_vs_svc_fwm_table by fwmark.
296  *      Should be called with locked tables.
297  */
298 static int ip_vs_svc_hash(struct ip_vs_service *svc)
299 {
300         unsigned int hash;
301 
302         if (svc->flags & IP_VS_SVC_F_HASHED) {
303                 pr_err("%s(): request for already hashed, called from %pS\n",
304                        __func__, __builtin_return_address(0));
305                 return 0;
306         }
307 
308         if (svc->fwmark == 0) {
309                 /*
310                  *  Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
311                  */
312                 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
313                                          &svc->addr, svc->port);
314                 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
315         } else {
316                 /*
317                  *  Hash it by fwmark in svc_fwm_table
318                  */
319                 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
320                 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
321         }
322 
323         svc->flags |= IP_VS_SVC_F_HASHED;
324         /* increase its refcnt because it is referenced by the svc table */
325         atomic_inc(&svc->refcnt);
326         return 1;
327 }
328 
329 
330 /*
331  *      Unhashes a service from svc_table / svc_fwm_table.
332  *      Should be called with locked tables.
333  */
334 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
335 {
336         if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
337                 pr_err("%s(): request for unhash flagged, called from %pS\n",
338                        __func__, __builtin_return_address(0));
339                 return 0;
340         }
341 
342         if (svc->fwmark == 0) {
343                 /* Remove it from the svc_table table */
344                 hlist_del_rcu(&svc->s_list);
345         } else {
346                 /* Remove it from the svc_fwm_table table */
347                 hlist_del_rcu(&svc->f_list);
348         }
349 
350         svc->flags &= ~IP_VS_SVC_F_HASHED;
351         atomic_dec(&svc->refcnt);
352         return 1;
353 }
354 
355 
356 /*
357  *      Get service by {netns, proto,addr,port} in the service table.
358  */
359 static inline struct ip_vs_service *
360 __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
361                      const union nf_inet_addr *vaddr, __be16 vport)
362 {
363         unsigned int hash;
364         struct ip_vs_service *svc;
365 
366         /* Check for "full" addressed entries */
367         hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
368 
369         hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
370                 if ((svc->af == af)
371                     && ip_vs_addr_equal(af, &svc->addr, vaddr)
372                     && (svc->port == vport)
373                     && (svc->protocol == protocol)
374                     && (svc->ipvs == ipvs)) {
375                         /* HIT */
376                         return svc;
377                 }
378         }
379 
380         return NULL;
381 }
382 
383 
384 /*
385  *      Get service by {fwmark} in the service table.
386  */
387 static inline struct ip_vs_service *
388 __ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
389 {
390         unsigned int hash;
391         struct ip_vs_service *svc;
392 
393         /* Check for fwmark addressed entries */
394         hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
395 
396         hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
397                 if (svc->fwmark == fwmark && svc->af == af
398                     && (svc->ipvs == ipvs)) {
399                         /* HIT */
400                         return svc;
401                 }
402         }
403 
404         return NULL;
405 }
406 
407 /* Find service, called under RCU lock */
408 struct ip_vs_service *
409 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
410                    const union nf_inet_addr *vaddr, __be16 vport)
411 {
412         struct ip_vs_service *svc;
413 
414         /*
415          *      Check the table hashed by fwmark first
416          */
417         if (fwmark) {
418                 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
419                 if (svc)
420                         goto out;
421         }
422 
423         /*
424          *      Check the table hashed by <protocol,addr,port>
425          *      for "full" addressed entries
426          */
427         svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
428 
429         if (!svc && protocol == IPPROTO_TCP &&
430             atomic_read(&ipvs->ftpsvc_counter) &&
431             (vport == FTPDATA || ntohs(vport) >= inet_prot_sock(ipvs->net))) {
432                 /*
433                  * Check if ftp service entry exists, the packet
434                  * might belong to FTP data connections.
435                  */
436                 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
437         }
438 
439         if (svc == NULL
440             && atomic_read(&ipvs->nullsvc_counter)) {
441                 /*
442                  * Check if the catch-all port (port zero) exists
443                  */
444                 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
445         }
446 
447   out:
448         IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
449                       fwmark, ip_vs_proto_name(protocol),
450                       IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
451                       svc ? "hit" : "not hit");
452 
453         return svc;
454 }
455 
456 
457 static inline void
458 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
459 {
460         atomic_inc(&svc->refcnt);
461         rcu_assign_pointer(dest->svc, svc);
462 }
463 
464 static void ip_vs_service_free(struct ip_vs_service *svc)
465 {
466         free_percpu(svc->stats.cpustats);
467         kfree(svc);
468 }
469 
470 static void ip_vs_service_rcu_free(struct rcu_head *head)
471 {
472         struct ip_vs_service *svc;
473 
474         svc = container_of(head, struct ip_vs_service, rcu_head);
475         ip_vs_service_free(svc);
476 }
477 
478 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
479 {
480         if (atomic_dec_and_test(&svc->refcnt)) {
481                 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
482                               svc->fwmark,
483                               IP_VS_DBG_ADDR(svc->af, &svc->addr),
484                               ntohs(svc->port));
485                 if (do_delay)
486                         call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
487                 else
488                         ip_vs_service_free(svc);
489         }
490 }
491 
492 
493 /*
494  *      Returns hash value for real service
495  */
496 static inline unsigned int ip_vs_rs_hashkey(int af,
497                                             const union nf_inet_addr *addr,
498                                             __be16 port)
499 {
500         register unsigned int porth = ntohs(port);
501         __be32 addr_fold = addr->ip;
502 
503 #ifdef CONFIG_IP_VS_IPV6
504         if (af == AF_INET6)
505                 addr_fold = addr->ip6[0]^addr->ip6[1]^
506                             addr->ip6[2]^addr->ip6[3];
507 #endif
508 
509         return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
510                 & IP_VS_RTAB_MASK;
511 }
512 
513 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
514 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
515 {
516         unsigned int hash;
517 
518         if (dest->in_rs_table)
519                 return;
520 
521         /*
522          *      Hash by proto,addr,port,
523          *      which are the parameters of the real service.
524          */
525         hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
526 
527         hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
528         dest->in_rs_table = 1;
529 }
530 
531 /* Unhash ip_vs_dest from rs_table. */
532 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
533 {
534         /*
535          * Remove it from the rs_table table.
536          */
537         if (dest->in_rs_table) {
538                 hlist_del_rcu(&dest->d_list);
539                 dest->in_rs_table = 0;
540         }
541 }
542 
543 /* Check if real service by <proto,addr,port> is present */
544 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
545                             const union nf_inet_addr *daddr, __be16 dport)
546 {
547         unsigned int hash;
548         struct ip_vs_dest *dest;
549 
550         /* Check for "full" addressed entries */
551         hash = ip_vs_rs_hashkey(af, daddr, dport);
552 
553         hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
554                 if (dest->port == dport &&
555                     dest->af == af &&
556                     ip_vs_addr_equal(af, &dest->addr, daddr) &&
557                     (dest->protocol == protocol || dest->vfwmark)) {
558                         /* HIT */
559                         return true;
560                 }
561         }
562 
563         return false;
564 }
565 
566 /* Find real service record by <proto,addr,port>.
567  * In case of multiple records with the same <proto,addr,port>, only
568  * the first found record is returned.
569  *
570  * To be called under RCU lock.
571  */
572 struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
573                                            __u16 protocol,
574                                            const union nf_inet_addr *daddr,
575                                            __be16 dport)
576 {
577         unsigned int hash;
578         struct ip_vs_dest *dest;
579 
580         /* Check for "full" addressed entries */
581         hash = ip_vs_rs_hashkey(af, daddr, dport);
582 
583         hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
584                 if (dest->port == dport &&
585                     dest->af == af &&
586                     ip_vs_addr_equal(af, &dest->addr, daddr) &&
587                         (dest->protocol == protocol || dest->vfwmark)) {
588                         /* HIT */
589                         return dest;
590                 }
591         }
592 
593         return NULL;
594 }
595 
596 /* Lookup destination by {addr,port} in the given service
597  * Called under RCU lock.
598  */
599 static struct ip_vs_dest *
600 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
601                   const union nf_inet_addr *daddr, __be16 dport)
602 {
603         struct ip_vs_dest *dest;
604 
605         /*
606          * Find the destination for the given service
607          */
608         list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
609                 if ((dest->af == dest_af) &&
610                     ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
611                     (dest->port == dport)) {
612                         /* HIT */
613                         return dest;
614                 }
615         }
616 
617         return NULL;
618 }
619 
620 /*
621  * Find destination by {daddr,dport,vaddr,protocol}
622  * Created to be used in ip_vs_process_message() in
623  * the backup synchronization daemon. It finds the
624  * destination to be bound to the received connection
625  * on the backup.
626  * Called under RCU lock, no refcnt is returned.
627  */
628 struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
629                                    const union nf_inet_addr *daddr,
630                                    __be16 dport,
631                                    const union nf_inet_addr *vaddr,
632                                    __be16 vport, __u16 protocol, __u32 fwmark,
633                                    __u32 flags)
634 {
635         struct ip_vs_dest *dest;
636         struct ip_vs_service *svc;
637         __be16 port = dport;
638 
639         svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
640         if (!svc)
641                 return NULL;
642         if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
643                 port = 0;
644         dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
645         if (!dest)
646                 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
647         return dest;
648 }
649 
650 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
651 {
652         struct ip_vs_dest_dst *dest_dst = container_of(head,
653                                                        struct ip_vs_dest_dst,
654                                                        rcu_head);
655 
656         dst_release(dest_dst->dst_cache);
657         kfree(dest_dst);
658 }
659 
660 /* Release dest_dst and dst_cache for dest in user context */
661 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
662 {
663         struct ip_vs_dest_dst *old;
664 
665         old = rcu_dereference_protected(dest->dest_dst, 1);
666         if (old) {
667                 RCU_INIT_POINTER(dest->dest_dst, NULL);
668                 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
669         }
670 }
671 
672 /*
673  *  Lookup dest by {svc,addr,port} in the destination trash.
674  *  The destination trash is used to hold the destinations that are removed
675  *  from the service table but are still referenced by some conn entries.
676  *  The reason to add the destination trash is when the dest is temporary
677  *  down (either by administrator or by monitor program), the dest can be
678  *  picked back from the trash, the remaining connections to the dest can
679  *  continue, and the counting information of the dest is also useful for
680  *  scheduling.
681  */
682 static struct ip_vs_dest *
683 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
684                      const union nf_inet_addr *daddr, __be16 dport)
685 {
686         struct ip_vs_dest *dest;
687         struct netns_ipvs *ipvs = svc->ipvs;
688 
689         /*
690          * Find the destination in trash
691          */
692         spin_lock_bh(&ipvs->dest_trash_lock);
693         list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
694                 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
695                               "dest->refcnt=%d\n",
696                               dest->vfwmark,
697                               IP_VS_DBG_ADDR(dest->af, &dest->addr),
698                               ntohs(dest->port),
699                               refcount_read(&dest->refcnt));
700                 if (dest->af == dest_af &&
701                     ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
702                     dest->port == dport &&
703                     dest->vfwmark == svc->fwmark &&
704                     dest->protocol == svc->protocol &&
705                     (svc->fwmark ||
706                      (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
707                       dest->vport == svc->port))) {
708                         /* HIT */
709                         list_del(&dest->t_list);
710                         goto out;
711                 }
712         }
713 
714         dest = NULL;
715 
716 out:
717         spin_unlock_bh(&ipvs->dest_trash_lock);
718 
719         return dest;
720 }
721 
722 static void ip_vs_dest_free(struct ip_vs_dest *dest)
723 {
724         struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
725 
726         __ip_vs_dst_cache_reset(dest);
727         __ip_vs_svc_put(svc, false);
728         free_percpu(dest->stats.cpustats);
729         ip_vs_dest_put_and_free(dest);
730 }
731 
732 /*
733  *  Clean up all the destinations in the trash
734  *  Called by the ip_vs_control_cleanup()
735  *
736  *  When the ip_vs_control_clearup is activated by ipvs module exit,
737  *  the service tables must have been flushed and all the connections
738  *  are expired, and the refcnt of each destination in the trash must
739  *  be 1, so we simply release them here.
740  */
741 static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
742 {
743         struct ip_vs_dest *dest, *nxt;
744 
745         del_timer_sync(&ipvs->dest_trash_timer);
746         /* No need to use dest_trash_lock */
747         list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
748                 list_del(&dest->t_list);
749                 ip_vs_dest_free(dest);
750         }
751 }
752 
753 static void
754 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
755 {
756 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
757 
758         spin_lock_bh(&src->lock);
759 
760         IP_VS_SHOW_STATS_COUNTER(conns);
761         IP_VS_SHOW_STATS_COUNTER(inpkts);
762         IP_VS_SHOW_STATS_COUNTER(outpkts);
763         IP_VS_SHOW_STATS_COUNTER(inbytes);
764         IP_VS_SHOW_STATS_COUNTER(outbytes);
765 
766         ip_vs_read_estimator(dst, src);
767 
768         spin_unlock_bh(&src->lock);
769 }
770 
771 static void
772 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
773 {
774         dst->conns = (u32)src->conns;
775         dst->inpkts = (u32)src->inpkts;
776         dst->outpkts = (u32)src->outpkts;
777         dst->inbytes = src->inbytes;
778         dst->outbytes = src->outbytes;
779         dst->cps = (u32)src->cps;
780         dst->inpps = (u32)src->inpps;
781         dst->outpps = (u32)src->outpps;
782         dst->inbps = (u32)src->inbps;
783         dst->outbps = (u32)src->outbps;
784 }
785 
786 static void
787 ip_vs_zero_stats(struct ip_vs_stats *stats)
788 {
789         spin_lock_bh(&stats->lock);
790 
791         /* get current counters as zero point, rates are zeroed */
792 
793 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
794 
795         IP_VS_ZERO_STATS_COUNTER(conns);
796         IP_VS_ZERO_STATS_COUNTER(inpkts);
797         IP_VS_ZERO_STATS_COUNTER(outpkts);
798         IP_VS_ZERO_STATS_COUNTER(inbytes);
799         IP_VS_ZERO_STATS_COUNTER(outbytes);
800 
801         ip_vs_zero_estimator(stats);
802 
803         spin_unlock_bh(&stats->lock);
804 }
805 
806 /*
807  *      Update a destination in the given service
808  */
809 static void
810 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
811                     struct ip_vs_dest_user_kern *udest, int add)
812 {
813         struct netns_ipvs *ipvs = svc->ipvs;
814         struct ip_vs_service *old_svc;
815         struct ip_vs_scheduler *sched;
816         int conn_flags;
817 
818         /* We cannot modify an address and change the address family */
819         BUG_ON(!add && udest->af != dest->af);
820 
821         if (add && udest->af != svc->af)
822                 ipvs->mixed_address_family_dests++;
823 
824         /* set the weight and the flags */
825         atomic_set(&dest->weight, udest->weight);
826         conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
827         conn_flags |= IP_VS_CONN_F_INACTIVE;
828 
829         /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
830         if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
831                 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
832         } else {
833                 /*
834                  *    Put the real service in rs_table if not present.
835                  *    For now only for NAT!
836                  */
837                 ip_vs_rs_hash(ipvs, dest);
838         }
839         atomic_set(&dest->conn_flags, conn_flags);
840 
841         /* bind the service */
842         old_svc = rcu_dereference_protected(dest->svc, 1);
843         if (!old_svc) {
844                 __ip_vs_bind_svc(dest, svc);
845         } else {
846                 if (old_svc != svc) {
847                         ip_vs_zero_stats(&dest->stats);
848                         __ip_vs_bind_svc(dest, svc);
849                         __ip_vs_svc_put(old_svc, true);
850                 }
851         }
852 
853         /* set the dest status flags */
854         dest->flags |= IP_VS_DEST_F_AVAILABLE;
855 
856         if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
857                 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
858         dest->u_threshold = udest->u_threshold;
859         dest->l_threshold = udest->l_threshold;
860 
861         dest->af = udest->af;
862 
863         spin_lock_bh(&dest->dst_lock);
864         __ip_vs_dst_cache_reset(dest);
865         spin_unlock_bh(&dest->dst_lock);
866 
867         if (add) {
868                 ip_vs_start_estimator(svc->ipvs, &dest->stats);
869                 list_add_rcu(&dest->n_list, &svc->destinations);
870                 svc->num_dests++;
871                 sched = rcu_dereference_protected(svc->scheduler, 1);
872                 if (sched && sched->add_dest)
873                         sched->add_dest(svc, dest);
874         } else {
875                 sched = rcu_dereference_protected(svc->scheduler, 1);
876                 if (sched && sched->upd_dest)
877                         sched->upd_dest(svc, dest);
878         }
879 }
880 
881 
882 /*
883  *      Create a destination for the given service
884  */
885 static int
886 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
887                struct ip_vs_dest **dest_p)
888 {
889         struct ip_vs_dest *dest;
890         unsigned int atype, i;
891 
892         EnterFunction(2);
893 
894 #ifdef CONFIG_IP_VS_IPV6
895         if (udest->af == AF_INET6) {
896                 atype = ipv6_addr_type(&udest->addr.in6);
897                 if ((!(atype & IPV6_ADDR_UNICAST) ||
898                         atype & IPV6_ADDR_LINKLOCAL) &&
899                         !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
900                         return -EINVAL;
901         } else
902 #endif
903         {
904                 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
905                 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
906                         return -EINVAL;
907         }
908 
909         dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
910         if (dest == NULL)
911                 return -ENOMEM;
912 
913         dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
914         if (!dest->stats.cpustats)
915                 goto err_alloc;
916 
917         for_each_possible_cpu(i) {
918                 struct ip_vs_cpu_stats *ip_vs_dest_stats;
919                 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
920                 u64_stats_init(&ip_vs_dest_stats->syncp);
921         }
922 
923         dest->af = udest->af;
924         dest->protocol = svc->protocol;
925         dest->vaddr = svc->addr;
926         dest->vport = svc->port;
927         dest->vfwmark = svc->fwmark;
928         ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
929         dest->port = udest->port;
930 
931         atomic_set(&dest->activeconns, 0);
932         atomic_set(&dest->inactconns, 0);
933         atomic_set(&dest->persistconns, 0);
934         refcount_set(&dest->refcnt, 1);
935 
936         INIT_HLIST_NODE(&dest->d_list);
937         spin_lock_init(&dest->dst_lock);
938         spin_lock_init(&dest->stats.lock);
939         __ip_vs_update_dest(svc, dest, udest, 1);
940 
941         *dest_p = dest;
942 
943         LeaveFunction(2);
944         return 0;
945 
946 err_alloc:
947         kfree(dest);
948         return -ENOMEM;
949 }
950 
951 
952 /*
953  *      Add a destination into an existing service
954  */
955 static int
956 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
957 {
958         struct ip_vs_dest *dest;
959         union nf_inet_addr daddr;
960         __be16 dport = udest->port;
961         int ret;
962 
963         EnterFunction(2);
964 
965         if (udest->weight < 0) {
966                 pr_err("%s(): server weight less than zero\n", __func__);
967                 return -ERANGE;
968         }
969 
970         if (udest->l_threshold > udest->u_threshold) {
971                 pr_err("%s(): lower threshold is higher than upper threshold\n",
972                         __func__);
973                 return -ERANGE;
974         }
975 
976         ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
977 
978         /* We use function that requires RCU lock */
979         rcu_read_lock();
980         dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
981         rcu_read_unlock();
982 
983         if (dest != NULL) {
984                 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
985                 return -EEXIST;
986         }
987 
988         /*
989          * Check if the dest already exists in the trash and
990          * is from the same service
991          */
992         dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
993 
994         if (dest != NULL) {
995                 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
996                               "dest->refcnt=%d, service %u/%s:%u\n",
997                               IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
998                               refcount_read(&dest->refcnt),
999                               dest->vfwmark,
1000                               IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1001                               ntohs(dest->vport));
1002 
1003                 __ip_vs_update_dest(svc, dest, udest, 1);
1004                 ret = 0;
1005         } else {
1006                 /*
1007                  * Allocate and initialize the dest structure
1008                  */
1009                 ret = ip_vs_new_dest(svc, udest, &dest);
1010         }
1011         LeaveFunction(2);
1012 
1013         return ret;
1014 }
1015 
1016 
1017 /*
1018  *      Edit a destination in the given service
1019  */
1020 static int
1021 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1022 {
1023         struct ip_vs_dest *dest;
1024         union nf_inet_addr daddr;
1025         __be16 dport = udest->port;
1026 
1027         EnterFunction(2);
1028 
1029         if (udest->weight < 0) {
1030                 pr_err("%s(): server weight less than zero\n", __func__);
1031                 return -ERANGE;
1032         }
1033 
1034         if (udest->l_threshold > udest->u_threshold) {
1035                 pr_err("%s(): lower threshold is higher than upper threshold\n",
1036                         __func__);
1037                 return -ERANGE;
1038         }
1039 
1040         ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1041 
1042         /* We use function that requires RCU lock */
1043         rcu_read_lock();
1044         dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1045         rcu_read_unlock();
1046 
1047         if (dest == NULL) {
1048                 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1049                 return -ENOENT;
1050         }
1051 
1052         __ip_vs_update_dest(svc, dest, udest, 0);
1053         LeaveFunction(2);
1054 
1055         return 0;
1056 }
1057 
1058 /*
1059  *      Delete a destination (must be already unlinked from the service)
1060  */
1061 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1062                              bool cleanup)
1063 {
1064         ip_vs_stop_estimator(ipvs, &dest->stats);
1065 
1066         /*
1067          *  Remove it from the d-linked list with the real services.
1068          */
1069         ip_vs_rs_unhash(dest);
1070 
1071         spin_lock_bh(&ipvs->dest_trash_lock);
1072         IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1073                       IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1074                       refcount_read(&dest->refcnt));
1075         if (list_empty(&ipvs->dest_trash) && !cleanup)
1076                 mod_timer(&ipvs->dest_trash_timer,
1077                           jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1078         /* dest lives in trash with reference */
1079         list_add(&dest->t_list, &ipvs->dest_trash);
1080         dest->idle_start = 0;
1081         spin_unlock_bh(&ipvs->dest_trash_lock);
1082 }
1083 
1084 
1085 /*
1086  *      Unlink a destination from the given service
1087  */
1088 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1089                                 struct ip_vs_dest *dest,
1090                                 int svcupd)
1091 {
1092         dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1093 
1094         /*
1095          *  Remove it from the d-linked destination list.
1096          */
1097         list_del_rcu(&dest->n_list);
1098         svc->num_dests--;
1099 
1100         if (dest->af != svc->af)
1101                 svc->ipvs->mixed_address_family_dests--;
1102 
1103         if (svcupd) {
1104                 struct ip_vs_scheduler *sched;
1105 
1106                 sched = rcu_dereference_protected(svc->scheduler, 1);
1107                 if (sched && sched->del_dest)
1108                         sched->del_dest(svc, dest);
1109         }
1110 }
1111 
1112 
1113 /*
1114  *      Delete a destination server in the given service
1115  */
1116 static int
1117 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1118 {
1119         struct ip_vs_dest *dest;
1120         __be16 dport = udest->port;
1121 
1122         EnterFunction(2);
1123 
1124         /* We use function that requires RCU lock */
1125         rcu_read_lock();
1126         dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1127         rcu_read_unlock();
1128 
1129         if (dest == NULL) {
1130                 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1131                 return -ENOENT;
1132         }
1133 
1134         /*
1135          *      Unlink dest from the service
1136          */
1137         __ip_vs_unlink_dest(svc, dest, 1);
1138 
1139         /*
1140          *      Delete the destination
1141          */
1142         __ip_vs_del_dest(svc->ipvs, dest, false);
1143 
1144         LeaveFunction(2);
1145 
1146         return 0;
1147 }
1148 
1149 static void ip_vs_dest_trash_expire(struct timer_list *t)
1150 {
1151         struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1152         struct ip_vs_dest *dest, *next;
1153         unsigned long now = jiffies;
1154 
1155         spin_lock(&ipvs->dest_trash_lock);
1156         list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1157                 if (refcount_read(&dest->refcnt) > 1)
1158                         continue;
1159                 if (dest->idle_start) {
1160                         if (time_before(now, dest->idle_start +
1161                                              IP_VS_DEST_TRASH_PERIOD))
1162                                 continue;
1163                 } else {
1164                         dest->idle_start = max(1UL, now);
1165                         continue;
1166                 }
1167                 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1168                               dest->vfwmark,
1169                               IP_VS_DBG_ADDR(dest->af, &dest->addr),
1170                               ntohs(dest->port));
1171                 list_del(&dest->t_list);
1172                 ip_vs_dest_free(dest);
1173         }
1174         if (!list_empty(&ipvs->dest_trash))
1175                 mod_timer(&ipvs->dest_trash_timer,
1176                           jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1177         spin_unlock(&ipvs->dest_trash_lock);
1178 }
1179 
1180 /*
1181  *      Add a service into the service hash table
1182  */
1183 static int
1184 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1185                   struct ip_vs_service **svc_p)
1186 {
1187         int ret = 0, i;
1188         struct ip_vs_scheduler *sched = NULL;
1189         struct ip_vs_pe *pe = NULL;
1190         struct ip_vs_service *svc = NULL;
1191 
1192         /* increase the module use count */
1193         ip_vs_use_count_inc();
1194 
1195         /* Lookup the scheduler by 'u->sched_name' */
1196         if (strcmp(u->sched_name, "none")) {
1197                 sched = ip_vs_scheduler_get(u->sched_name);
1198                 if (!sched) {
1199                         pr_info("Scheduler module ip_vs_%s not found\n",
1200                                 u->sched_name);
1201                         ret = -ENOENT;
1202                         goto out_err;
1203                 }
1204         }
1205 
1206         if (u->pe_name && *u->pe_name) {
1207                 pe = ip_vs_pe_getbyname(u->pe_name);
1208                 if (pe == NULL) {
1209                         pr_info("persistence engine module ip_vs_pe_%s "
1210                                 "not found\n", u->pe_name);
1211                         ret = -ENOENT;
1212                         goto out_err;
1213                 }
1214         }
1215 
1216 #ifdef CONFIG_IP_VS_IPV6
1217         if (u->af == AF_INET6) {
1218                 __u32 plen = (__force __u32) u->netmask;
1219 
1220                 if (plen < 1 || plen > 128) {
1221                         ret = -EINVAL;
1222                         goto out_err;
1223                 }
1224         }
1225 #endif
1226 
1227         svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1228         if (svc == NULL) {
1229                 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1230                 ret = -ENOMEM;
1231                 goto out_err;
1232         }
1233         svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1234         if (!svc->stats.cpustats) {
1235                 ret = -ENOMEM;
1236                 goto out_err;
1237         }
1238 
1239         for_each_possible_cpu(i) {
1240                 struct ip_vs_cpu_stats *ip_vs_stats;
1241                 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1242                 u64_stats_init(&ip_vs_stats->syncp);
1243         }
1244 
1245 
1246         /* I'm the first user of the service */
1247         atomic_set(&svc->refcnt, 0);
1248 
1249         svc->af = u->af;
1250         svc->protocol = u->protocol;
1251         ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1252         svc->port = u->port;
1253         svc->fwmark = u->fwmark;
1254         svc->flags = u->flags;
1255         svc->timeout = u->timeout * HZ;
1256         svc->netmask = u->netmask;
1257         svc->ipvs = ipvs;
1258 
1259         INIT_LIST_HEAD(&svc->destinations);
1260         spin_lock_init(&svc->sched_lock);
1261         spin_lock_init(&svc->stats.lock);
1262 
1263         /* Bind the scheduler */
1264         if (sched) {
1265                 ret = ip_vs_bind_scheduler(svc, sched);
1266                 if (ret)
1267                         goto out_err;
1268                 sched = NULL;
1269         }
1270 
1271         /* Bind the ct retriever */
1272         RCU_INIT_POINTER(svc->pe, pe);
1273         pe = NULL;
1274 
1275         /* Update the virtual service counters */
1276         if (svc->port == FTPPORT)
1277                 atomic_inc(&ipvs->ftpsvc_counter);
1278         else if (svc->port == 0)
1279                 atomic_inc(&ipvs->nullsvc_counter);
1280         if (svc->pe && svc->pe->conn_out)
1281                 atomic_inc(&ipvs->conn_out_counter);
1282 
1283         ip_vs_start_estimator(ipvs, &svc->stats);
1284 
1285         /* Count only IPv4 services for old get/setsockopt interface */
1286         if (svc->af == AF_INET)
1287                 ipvs->num_services++;
1288 
1289         /* Hash the service into the service table */
1290         ip_vs_svc_hash(svc);
1291 
1292         *svc_p = svc;
1293         /* Now there is a service - full throttle */
1294         ipvs->enable = 1;
1295         return 0;
1296 
1297 
1298  out_err:
1299         if (svc != NULL) {
1300                 ip_vs_unbind_scheduler(svc, sched);
1301                 ip_vs_service_free(svc);
1302         }
1303         ip_vs_scheduler_put(sched);
1304         ip_vs_pe_put(pe);
1305 
1306         /* decrease the module use count */
1307         ip_vs_use_count_dec();
1308 
1309         return ret;
1310 }
1311 
1312 
1313 /*
1314  *      Edit a service and bind it with a new scheduler
1315  */
1316 static int
1317 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1318 {
1319         struct ip_vs_scheduler *sched = NULL, *old_sched;
1320         struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1321         int ret = 0;
1322         bool new_pe_conn_out, old_pe_conn_out;
1323 
1324         /*
1325          * Lookup the scheduler, by 'u->sched_name'
1326          */
1327         if (strcmp(u->sched_name, "none")) {
1328                 sched = ip_vs_scheduler_get(u->sched_name);
1329                 if (!sched) {
1330                         pr_info("Scheduler module ip_vs_%s not found\n",
1331                                 u->sched_name);
1332                         return -ENOENT;
1333                 }
1334         }
1335         old_sched = sched;
1336 
1337         if (u->pe_name && *u->pe_name) {
1338                 pe = ip_vs_pe_getbyname(u->pe_name);
1339                 if (pe == NULL) {
1340                         pr_info("persistence engine module ip_vs_pe_%s "
1341                                 "not found\n", u->pe_name);
1342                         ret = -ENOENT;
1343                         goto out;
1344                 }
1345                 old_pe = pe;
1346         }
1347 
1348 #ifdef CONFIG_IP_VS_IPV6
1349         if (u->af == AF_INET6) {
1350                 __u32 plen = (__force __u32) u->netmask;
1351 
1352                 if (plen < 1 || plen > 128) {
1353                         ret = -EINVAL;
1354                         goto out;
1355                 }
1356         }
1357 #endif
1358 
1359         old_sched = rcu_dereference_protected(svc->scheduler, 1);
1360         if (sched != old_sched) {
1361                 if (old_sched) {
1362                         ip_vs_unbind_scheduler(svc, old_sched);
1363                         RCU_INIT_POINTER(svc->scheduler, NULL);
1364                         /* Wait all svc->sched_data users */
1365                         synchronize_rcu();
1366                 }
1367                 /* Bind the new scheduler */
1368                 if (sched) {
1369                         ret = ip_vs_bind_scheduler(svc, sched);
1370                         if (ret) {
1371                                 ip_vs_scheduler_put(sched);
1372                                 goto out;
1373                         }
1374                 }
1375         }
1376 
1377         /*
1378          * Set the flags and timeout value
1379          */
1380         svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1381         svc->timeout = u->timeout * HZ;
1382         svc->netmask = u->netmask;
1383 
1384         old_pe = rcu_dereference_protected(svc->pe, 1);
1385         if (pe != old_pe) {
1386                 rcu_assign_pointer(svc->pe, pe);
1387                 /* check for optional methods in new pe */
1388                 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1389                 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1390                 if (new_pe_conn_out && !old_pe_conn_out)
1391                         atomic_inc(&svc->ipvs->conn_out_counter);
1392                 if (old_pe_conn_out && !new_pe_conn_out)
1393                         atomic_dec(&svc->ipvs->conn_out_counter);
1394         }
1395 
1396 out:
1397         ip_vs_scheduler_put(old_sched);
1398         ip_vs_pe_put(old_pe);
1399         return ret;
1400 }
1401 
1402 /*
1403  *      Delete a service from the service list
1404  *      - The service must be unlinked, unlocked and not referenced!
1405  *      - We are called under _bh lock
1406  */
1407 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1408 {
1409         struct ip_vs_dest *dest, *nxt;
1410         struct ip_vs_scheduler *old_sched;
1411         struct ip_vs_pe *old_pe;
1412         struct netns_ipvs *ipvs = svc->ipvs;
1413 
1414         /* Count only IPv4 services for old get/setsockopt interface */
1415         if (svc->af == AF_INET)
1416                 ipvs->num_services--;
1417 
1418         ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1419 
1420         /* Unbind scheduler */
1421         old_sched = rcu_dereference_protected(svc->scheduler, 1);
1422         ip_vs_unbind_scheduler(svc, old_sched);
1423         ip_vs_scheduler_put(old_sched);
1424 
1425         /* Unbind persistence engine, keep svc->pe */
1426         old_pe = rcu_dereference_protected(svc->pe, 1);
1427         if (old_pe && old_pe->conn_out)
1428                 atomic_dec(&ipvs->conn_out_counter);
1429         ip_vs_pe_put(old_pe);
1430 
1431         /*
1432          *    Unlink the whole destination list
1433          */
1434         list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1435                 __ip_vs_unlink_dest(svc, dest, 0);
1436                 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1437         }
1438 
1439         /*
1440          *    Update the virtual service counters
1441          */
1442         if (svc->port == FTPPORT)
1443                 atomic_dec(&ipvs->ftpsvc_counter);
1444         else if (svc->port == 0)
1445                 atomic_dec(&ipvs->nullsvc_counter);
1446 
1447         /*
1448          *    Free the service if nobody refers to it
1449          */
1450         __ip_vs_svc_put(svc, true);
1451 
1452         /* decrease the module use count */
1453         ip_vs_use_count_dec();
1454 }
1455 
1456 /*
1457  * Unlink a service from list and try to delete it if its refcnt reached 0
1458  */
1459 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1460 {
1461         /* Hold svc to avoid double release from dest_trash */
1462         atomic_inc(&svc->refcnt);
1463         /*
1464          * Unhash it from the service table
1465          */
1466         ip_vs_svc_unhash(svc);
1467 
1468         __ip_vs_del_service(svc, cleanup);
1469 }
1470 
1471 /*
1472  *      Delete a service from the service list
1473  */
1474 static int ip_vs_del_service(struct ip_vs_service *svc)
1475 {
1476         if (svc == NULL)
1477                 return -EEXIST;
1478         ip_vs_unlink_service(svc, false);
1479 
1480         return 0;
1481 }
1482 
1483 
1484 /*
1485  *      Flush all the virtual services
1486  */
1487 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1488 {
1489         int idx;
1490         struct ip_vs_service *svc;
1491         struct hlist_node *n;
1492 
1493         /*
1494          * Flush the service table hashed by <netns,protocol,addr,port>
1495          */
1496         for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1497                 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1498                                           s_list) {
1499                         if (svc->ipvs == ipvs)
1500                                 ip_vs_unlink_service(svc, cleanup);
1501                 }
1502         }
1503 
1504         /*
1505          * Flush the service table hashed by fwmark
1506          */
1507         for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1508                 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1509                                           f_list) {
1510                         if (svc->ipvs == ipvs)
1511                                 ip_vs_unlink_service(svc, cleanup);
1512                 }
1513         }
1514 
1515         return 0;
1516 }
1517 
1518 /*
1519  *      Delete service by {netns} in the service table.
1520  *      Called by __ip_vs_cleanup()
1521  */
1522 void ip_vs_service_net_cleanup(struct netns_ipvs *ipvs)
1523 {
1524         EnterFunction(2);
1525         /* Check for "full" addressed entries */
1526         mutex_lock(&__ip_vs_mutex);
1527         ip_vs_flush(ipvs, true);
1528         mutex_unlock(&__ip_vs_mutex);
1529         LeaveFunction(2);
1530 }
1531 
1532 /* Put all references for device (dst_cache) */
1533 static inline void
1534 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1535 {
1536         struct ip_vs_dest_dst *dest_dst;
1537 
1538         spin_lock_bh(&dest->dst_lock);
1539         dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1540         if (dest_dst && dest_dst->dst_cache->dev == dev) {
1541                 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1542                               dev->name,
1543                               IP_VS_DBG_ADDR(dest->af, &dest->addr),
1544                               ntohs(dest->port),
1545                               refcount_read(&dest->refcnt));
1546                 __ip_vs_dst_cache_reset(dest);
1547         }
1548         spin_unlock_bh(&dest->dst_lock);
1549 
1550 }
1551 /* Netdev event receiver
1552  * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1553  */
1554 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1555                            void *ptr)
1556 {
1557         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1558         struct net *net = dev_net(dev);
1559         struct netns_ipvs *ipvs = net_ipvs(net);
1560         struct ip_vs_service *svc;
1561         struct ip_vs_dest *dest;
1562         unsigned int idx;
1563 
1564         if (event != NETDEV_DOWN || !ipvs)
1565                 return NOTIFY_DONE;
1566         IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1567         EnterFunction(2);
1568         mutex_lock(&__ip_vs_mutex);
1569         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1570                 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1571                         if (svc->ipvs == ipvs) {
1572                                 list_for_each_entry(dest, &svc->destinations,
1573                                                     n_list) {
1574                                         ip_vs_forget_dev(dest, dev);
1575                                 }
1576                         }
1577                 }
1578 
1579                 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1580                         if (svc->ipvs == ipvs) {
1581                                 list_for_each_entry(dest, &svc->destinations,
1582                                                     n_list) {
1583                                         ip_vs_forget_dev(dest, dev);
1584                                 }
1585                         }
1586 
1587                 }
1588         }
1589 
1590         spin_lock_bh(&ipvs->dest_trash_lock);
1591         list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1592                 ip_vs_forget_dev(dest, dev);
1593         }
1594         spin_unlock_bh(&ipvs->dest_trash_lock);
1595         mutex_unlock(&__ip_vs_mutex);
1596         LeaveFunction(2);
1597         return NOTIFY_DONE;
1598 }
1599 
1600 /*
1601  *      Zero counters in a service or all services
1602  */
1603 static int ip_vs_zero_service(struct ip_vs_service *svc)
1604 {
1605         struct ip_vs_dest *dest;
1606 
1607         list_for_each_entry(dest, &svc->destinations, n_list) {
1608                 ip_vs_zero_stats(&dest->stats);
1609         }
1610         ip_vs_zero_stats(&svc->stats);
1611         return 0;
1612 }
1613 
1614 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1615 {
1616         int idx;
1617         struct ip_vs_service *svc;
1618 
1619         for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1620                 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1621                         if (svc->ipvs == ipvs)
1622                                 ip_vs_zero_service(svc);
1623                 }
1624         }
1625 
1626         for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1627                 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1628                         if (svc->ipvs == ipvs)
1629                                 ip_vs_zero_service(svc);
1630                 }
1631         }
1632 
1633         ip_vs_zero_stats(&ipvs->tot_stats);
1634         return 0;
1635 }
1636 
1637 #ifdef CONFIG_SYSCTL
1638 
1639 static int zero;
1640 static int three = 3;
1641 
1642 static int
1643 proc_do_defense_mode(struct ctl_table *table, int write,
1644                      void __user *buffer, size_t *lenp, loff_t *ppos)
1645 {
1646         struct netns_ipvs *ipvs = table->extra2;
1647         int *valp = table->data;
1648         int val = *valp;
1649         int rc;
1650 
1651         rc = proc_dointvec(table, write, buffer, lenp, ppos);
1652         if (write && (*valp != val)) {
1653                 if ((*valp < 0) || (*valp > 3)) {
1654                         /* Restore the correct value */
1655                         *valp = val;
1656                 } else {
1657                         update_defense_level(ipvs);
1658                 }
1659         }
1660         return rc;
1661 }
1662 
1663 static int
1664 proc_do_sync_threshold(struct ctl_table *table, int write,
1665                        void __user *buffer, size_t *lenp, loff_t *ppos)
1666 {
1667         int *valp = table->data;
1668         int val[2];
1669         int rc;
1670 
1671         /* backup the value first */
1672         memcpy(val, valp, sizeof(val));
1673 
1674         rc = proc_dointvec(table, write, buffer, lenp, ppos);
1675         if (write && (valp[0] < 0 || valp[1] < 0 ||
1676             (valp[0] >= valp[1] && valp[1]))) {
1677                 /* Restore the correct value */
1678                 memcpy(valp, val, sizeof(val));
1679         }
1680         return rc;
1681 }
1682 
1683 static int
1684 proc_do_sync_mode(struct ctl_table *table, int write,
1685                      void __user *buffer, size_t *lenp, loff_t *ppos)
1686 {
1687         int *valp = table->data;
1688         int val = *valp;
1689         int rc;
1690 
1691         rc = proc_dointvec(table, write, buffer, lenp, ppos);
1692         if (write && (*valp != val)) {
1693                 if ((*valp < 0) || (*valp > 1)) {
1694                         /* Restore the correct value */
1695                         *valp = val;
1696                 }
1697         }
1698         return rc;
1699 }
1700 
1701 static int
1702 proc_do_sync_ports(struct ctl_table *table, int write,
1703                    void __user *buffer, size_t *lenp, loff_t *ppos)
1704 {
1705         int *valp = table->data;
1706         int val = *valp;
1707         int rc;
1708 
1709         rc = proc_dointvec(table, write, buffer, lenp, ppos);
1710         if (write && (*valp != val)) {
1711                 if (*valp < 1 || !is_power_of_2(*valp)) {
1712                         /* Restore the correct value */
1713                         *valp = val;
1714                 }
1715         }
1716         return rc;
1717 }
1718 
1719 /*
1720  *      IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1721  *      Do not change order or insert new entries without
1722  *      align with netns init in ip_vs_control_net_init()
1723  */
1724 
1725 static struct ctl_table vs_vars[] = {
1726         {
1727                 .procname       = "amemthresh",
1728                 .maxlen         = sizeof(int),
1729                 .mode           = 0644,
1730                 .proc_handler   = proc_dointvec,
1731         },
1732         {
1733                 .procname       = "am_droprate",
1734                 .maxlen         = sizeof(int),
1735                 .mode           = 0644,
1736                 .proc_handler   = proc_dointvec,
1737         },
1738         {
1739                 .procname       = "drop_entry",
1740                 .maxlen         = sizeof(int),
1741                 .mode           = 0644,
1742                 .proc_handler   = proc_do_defense_mode,
1743         },
1744         {
1745                 .procname       = "drop_packet",
1746                 .maxlen         = sizeof(int),
1747                 .mode           = 0644,
1748                 .proc_handler   = proc_do_defense_mode,
1749         },
1750 #ifdef CONFIG_IP_VS_NFCT
1751         {
1752                 .procname       = "conntrack",
1753                 .maxlen         = sizeof(int),
1754                 .mode           = 0644,
1755                 .proc_handler   = &proc_dointvec,
1756         },
1757 #endif
1758         {
1759                 .procname       = "secure_tcp",
1760                 .maxlen         = sizeof(int),
1761                 .mode           = 0644,
1762                 .proc_handler   = proc_do_defense_mode,
1763         },
1764         {
1765                 .procname       = "snat_reroute",
1766                 .maxlen         = sizeof(int),
1767                 .mode           = 0644,
1768                 .proc_handler   = &proc_dointvec,
1769         },
1770         {
1771                 .procname       = "sync_version",
1772                 .maxlen         = sizeof(int),
1773                 .mode           = 0644,
1774                 .proc_handler   = proc_do_sync_mode,
1775         },
1776         {
1777                 .procname       = "sync_ports",
1778                 .maxlen         = sizeof(int),
1779                 .mode           = 0644,
1780                 .proc_handler   = proc_do_sync_ports,
1781         },
1782         {
1783                 .procname       = "sync_persist_mode",
1784                 .maxlen         = sizeof(int),
1785                 .mode           = 0644,
1786                 .proc_handler   = proc_dointvec,
1787         },
1788         {
1789                 .procname       = "sync_qlen_max",
1790                 .maxlen         = sizeof(unsigned long),
1791                 .mode           = 0644,
1792                 .proc_handler   = proc_doulongvec_minmax,
1793         },
1794         {
1795                 .procname       = "sync_sock_size",
1796                 .maxlen         = sizeof(int),
1797                 .mode           = 0644,
1798                 .proc_handler   = proc_dointvec,
1799         },
1800         {
1801                 .procname       = "cache_bypass",
1802                 .maxlen         = sizeof(int),
1803                 .mode           = 0644,
1804                 .proc_handler   = proc_dointvec,
1805         },
1806         {
1807                 .procname       = "expire_nodest_conn",
1808                 .maxlen         = sizeof(int),
1809                 .mode           = 0644,
1810                 .proc_handler   = proc_dointvec,
1811         },
1812         {
1813                 .procname       = "sloppy_tcp",
1814                 .maxlen         = sizeof(int),
1815                 .mode           = 0644,
1816                 .proc_handler   = proc_dointvec,
1817         },
1818         {
1819                 .procname       = "sloppy_sctp",
1820                 .maxlen         = sizeof(int),
1821                 .mode           = 0644,
1822                 .proc_handler   = proc_dointvec,
1823         },
1824         {
1825                 .procname       = "expire_quiescent_template",
1826                 .maxlen         = sizeof(int),
1827                 .mode           = 0644,
1828                 .proc_handler   = proc_dointvec,
1829         },
1830         {
1831                 .procname       = "sync_threshold",
1832                 .maxlen         =
1833                         sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1834                 .mode           = 0644,
1835                 .proc_handler   = proc_do_sync_threshold,
1836         },
1837         {
1838                 .procname       = "sync_refresh_period",
1839                 .maxlen         = sizeof(int),
1840                 .mode           = 0644,
1841                 .proc_handler   = proc_dointvec_jiffies,
1842         },
1843         {
1844                 .procname       = "sync_retries",
1845                 .maxlen         = sizeof(int),
1846                 .mode           = 0644,
1847                 .proc_handler   = proc_dointvec_minmax,
1848                 .extra1         = &zero,
1849                 .extra2         = &three,
1850         },
1851         {
1852                 .procname       = "nat_icmp_send",
1853                 .maxlen         = sizeof(int),
1854                 .mode           = 0644,
1855                 .proc_handler   = proc_dointvec,
1856         },
1857         {
1858                 .procname       = "pmtu_disc",
1859                 .maxlen         = sizeof(int),
1860                 .mode           = 0644,
1861                 .proc_handler   = proc_dointvec,
1862         },
1863         {
1864                 .procname       = "backup_only",
1865                 .maxlen         = sizeof(int),
1866                 .mode           = 0644,
1867                 .proc_handler   = proc_dointvec,
1868         },
1869         {
1870                 .procname       = "conn_reuse_mode",
1871                 .maxlen         = sizeof(int),
1872                 .mode           = 0644,
1873                 .proc_handler   = proc_dointvec,
1874         },
1875         {
1876                 .procname       = "schedule_icmp",
1877                 .maxlen         = sizeof(int),
1878                 .mode           = 0644,
1879                 .proc_handler   = proc_dointvec,
1880         },
1881         {
1882                 .procname       = "ignore_tunneled",
1883                 .maxlen         = sizeof(int),
1884                 .mode           = 0644,
1885                 .proc_handler   = proc_dointvec,
1886         },
1887 #ifdef CONFIG_IP_VS_DEBUG
1888         {
1889                 .procname       = "debug_level",
1890                 .data           = &sysctl_ip_vs_debug_level,
1891                 .maxlen         = sizeof(int),
1892                 .mode           = 0644,
1893                 .proc_handler   = proc_dointvec,
1894         },
1895 #endif
1896         { }
1897 };
1898 
1899 #endif
1900 
1901 #ifdef CONFIG_PROC_FS
1902 
1903 struct ip_vs_iter {
1904         struct seq_net_private p;  /* Do not move this, netns depends upon it*/
1905         struct hlist_head *table;
1906         int bucket;
1907 };
1908 
1909 /*
1910  *      Write the contents of the VS rule table to a PROCfs file.
1911  *      (It is kept just for backward compatibility)
1912  */
1913 static inline const char *ip_vs_fwd_name(unsigned int flags)
1914 {
1915         switch (flags & IP_VS_CONN_F_FWD_MASK) {
1916         case IP_VS_CONN_F_LOCALNODE:
1917                 return "Local";
1918         case IP_VS_CONN_F_TUNNEL:
1919                 return "Tunnel";
1920         case IP_VS_CONN_F_DROUTE:
1921                 return "Route";
1922         default:
1923                 return "Masq";
1924         }
1925 }
1926 
1927 
1928 /* Get the Nth entry in the two lists */
1929 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1930 {
1931         struct net *net = seq_file_net(seq);
1932         struct netns_ipvs *ipvs = net_ipvs(net);
1933         struct ip_vs_iter *iter = seq->private;
1934         int idx;
1935         struct ip_vs_service *svc;
1936 
1937         /* look in hash by protocol */
1938         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1939                 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1940                         if ((svc->ipvs == ipvs) && pos-- == 0) {
1941                                 iter->table = ip_vs_svc_table;
1942                                 iter->bucket = idx;
1943                                 return svc;
1944                         }
1945                 }
1946         }
1947 
1948         /* keep looking in fwmark */
1949         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1950                 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1951                                          f_list) {
1952                         if ((svc->ipvs == ipvs) && pos-- == 0) {
1953                                 iter->table = ip_vs_svc_fwm_table;
1954                                 iter->bucket = idx;
1955                                 return svc;
1956                         }
1957                 }
1958         }
1959 
1960         return NULL;
1961 }
1962 
1963 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1964         __acquires(RCU)
1965 {
1966         rcu_read_lock();
1967         return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1968 }
1969 
1970 
1971 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1972 {
1973         struct hlist_node *e;
1974         struct ip_vs_iter *iter;
1975         struct ip_vs_service *svc;
1976 
1977         ++*pos;
1978         if (v == SEQ_START_TOKEN)
1979                 return ip_vs_info_array(seq,0);
1980 
1981         svc = v;
1982         iter = seq->private;
1983 
1984         if (iter->table == ip_vs_svc_table) {
1985                 /* next service in table hashed by protocol */
1986                 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1987                 if (e)
1988                         return hlist_entry(e, struct ip_vs_service, s_list);
1989 
1990                 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1991                         hlist_for_each_entry_rcu(svc,
1992                                                  &ip_vs_svc_table[iter->bucket],
1993                                                  s_list) {
1994                                 return svc;
1995                         }
1996                 }
1997 
1998                 iter->table = ip_vs_svc_fwm_table;
1999                 iter->bucket = -1;
2000                 goto scan_fwmark;
2001         }
2002 
2003         /* next service in hashed by fwmark */
2004         e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2005         if (e)
2006                 return hlist_entry(e, struct ip_vs_service, f_list);
2007 
2008  scan_fwmark:
2009         while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2010                 hlist_for_each_entry_rcu(svc,
2011                                          &ip_vs_svc_fwm_table[iter->bucket],
2012                                          f_list)
2013                         return svc;
2014         }
2015 
2016         return NULL;
2017 }
2018 
2019 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2020         __releases(RCU)
2021 {
2022         rcu_read_unlock();
2023 }
2024 
2025 
2026 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2027 {
2028         if (v == SEQ_START_TOKEN) {
2029                 seq_printf(seq,
2030                         "IP Virtual Server version %d.%d.%d (size=%d)\n",
2031                         NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2032                 seq_puts(seq,
2033                          "Prot LocalAddress:Port Scheduler Flags\n");
2034                 seq_puts(seq,
2035                          "  -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2036         } else {
2037                 struct net *net = seq_file_net(seq);
2038                 struct netns_ipvs *ipvs = net_ipvs(net);
2039                 const struct ip_vs_service *svc = v;
2040                 const struct ip_vs_iter *iter = seq->private;
2041                 const struct ip_vs_dest *dest;
2042                 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2043                 char *sched_name = sched ? sched->name : "none";
2044 
2045                 if (svc->ipvs != ipvs)
2046                         return 0;
2047                 if (iter->table == ip_vs_svc_table) {
2048 #ifdef CONFIG_IP_VS_IPV6
2049                         if (svc->af == AF_INET6)
2050                                 seq_printf(seq, "%s  [%pI6]:%04X %s ",
2051                                            ip_vs_proto_name(svc->protocol),
2052                                            &svc->addr.in6,
2053                                            ntohs(svc->port),
2054                                            sched_name);
2055                         else
2056 #endif
2057                                 seq_printf(seq, "%s  %08X:%04X %s %s ",
2058                                            ip_vs_proto_name(svc->protocol),
2059                                            ntohl(svc->addr.ip),
2060                                            ntohs(svc->port),
2061                                            sched_name,
2062                                            (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2063                 } else {
2064                         seq_printf(seq, "FWM  %08X %s %s",
2065                                    svc->fwmark, sched_name,
2066                                    (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2067                 }
2068 
2069                 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2070                         seq_printf(seq, "persistent %d %08X\n",
2071                                 svc->timeout,
2072                                 ntohl(svc->netmask));
2073                 else
2074                         seq_putc(seq, '\n');
2075 
2076                 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2077 #ifdef CONFIG_IP_VS_IPV6
2078                         if (dest->af == AF_INET6)
2079                                 seq_printf(seq,
2080                                            "  -> [%pI6]:%04X"
2081                                            "      %-7s %-6d %-10d %-10d\n",
2082                                            &dest->addr.in6,
2083                                            ntohs(dest->port),
2084                                            ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2085                                            atomic_read(&dest->weight),
2086                                            atomic_read(&dest->activeconns),
2087                                            atomic_read(&dest->inactconns));
2088                         else
2089 #endif
2090                                 seq_printf(seq,
2091                                            "  -> %08X:%04X      "
2092                                            "%-7s %-6d %-10d %-10d\n",
2093                                            ntohl(dest->addr.ip),
2094                                            ntohs(dest->port),
2095                                            ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2096                                            atomic_read(&dest->weight),
2097                                            atomic_read(&dest->activeconns),
2098                                            atomic_read(&dest->inactconns));
2099 
2100                 }
2101         }
2102         return 0;
2103 }
2104 
2105 static const struct seq_operations ip_vs_info_seq_ops = {
2106         .start = ip_vs_info_seq_start,
2107         .next  = ip_vs_info_seq_next,
2108         .stop  = ip_vs_info_seq_stop,
2109         .show  = ip_vs_info_seq_show,
2110 };
2111 
2112 static int ip_vs_info_open(struct inode *inode, struct file *file)
2113 {
2114         return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2115                         sizeof(struct ip_vs_iter));
2116 }
2117 
2118 static const struct file_operations ip_vs_info_fops = {
2119         .owner   = THIS_MODULE,
2120         .open    = ip_vs_info_open,
2121         .read    = seq_read,
2122         .llseek  = seq_lseek,
2123         .release = seq_release_net,
2124 };
2125 
2126 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2127 {
2128         struct net *net = seq_file_single_net(seq);
2129         struct ip_vs_kstats show;
2130 
2131 /*               01234567 01234567 01234567 0123456701234567 0123456701234567 */
2132         seq_puts(seq,
2133                  "   Total Incoming Outgoing         Incoming         Outgoing\n");
2134         seq_puts(seq,
2135                  "   Conns  Packets  Packets            Bytes            Bytes\n");
2136 
2137         ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2138         seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2139                    (unsigned long long)show.conns,
2140                    (unsigned long long)show.inpkts,
2141                    (unsigned long long)show.outpkts,
2142                    (unsigned long long)show.inbytes,
2143                    (unsigned long long)show.outbytes);
2144 
2145 /*                01234567 01234567 01234567 0123456701234567 0123456701234567*/
2146         seq_puts(seq,
2147                  " Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s\n");
2148         seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2149                    (unsigned long long)show.cps,
2150                    (unsigned long long)show.inpps,
2151                    (unsigned long long)show.outpps,
2152                    (unsigned long long)show.inbps,
2153                    (unsigned long long)show.outbps);
2154 
2155         return 0;
2156 }
2157 
2158 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2159 {
2160         return single_open_net(inode, file, ip_vs_stats_show);
2161 }
2162 
2163 static const struct file_operations ip_vs_stats_fops = {
2164         .owner = THIS_MODULE,
2165         .open = ip_vs_stats_seq_open,
2166         .read = seq_read,
2167         .llseek = seq_lseek,
2168         .release = single_release_net,
2169 };
2170 
2171 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2172 {
2173         struct net *net = seq_file_single_net(seq);
2174         struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2175         struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2176         struct ip_vs_kstats kstats;
2177         int i;
2178 
2179 /*               01234567 01234567 01234567 0123456701234567 0123456701234567 */
2180         seq_puts(seq,
2181                  "       Total Incoming Outgoing         Incoming         Outgoing\n");
2182         seq_puts(seq,
2183                  "CPU    Conns  Packets  Packets            Bytes            Bytes\n");
2184 
2185         for_each_possible_cpu(i) {
2186                 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2187                 unsigned int start;
2188                 u64 conns, inpkts, outpkts, inbytes, outbytes;
2189 
2190                 do {
2191                         start = u64_stats_fetch_begin_irq(&u->syncp);
2192                         conns = u->cnt.conns;
2193                         inpkts = u->cnt.inpkts;
2194                         outpkts = u->cnt.outpkts;
2195                         inbytes = u->cnt.inbytes;
2196                         outbytes = u->cnt.outbytes;
2197                 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2198 
2199                 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2200                            i, (u64)conns, (u64)inpkts,
2201                            (u64)outpkts, (u64)inbytes,
2202                            (u64)outbytes);
2203         }
2204 
2205         ip_vs_copy_stats(&kstats, tot_stats);
2206 
2207         seq_printf(seq, "  ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2208                    (unsigned long long)kstats.conns,
2209                    (unsigned long long)kstats.inpkts,
2210                    (unsigned long long)kstats.outpkts,
2211                    (unsigned long long)kstats.inbytes,
2212                    (unsigned long long)kstats.outbytes);
2213 
2214 /*                ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2215         seq_puts(seq,
2216                  "     Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s\n");
2217         seq_printf(seq, "    %8LX %8LX %8LX %16LX %16LX\n",
2218                    kstats.cps,
2219                    kstats.inpps,
2220                    kstats.outpps,
2221                    kstats.inbps,
2222                    kstats.outbps);
2223 
2224         return 0;
2225 }
2226 
2227 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2228 {
2229         return single_open_net(inode, file, ip_vs_stats_percpu_show);
2230 }
2231 
2232 static const struct file_operations ip_vs_stats_percpu_fops = {
2233         .owner = THIS_MODULE,
2234         .open = ip_vs_stats_percpu_seq_open,
2235         .read = seq_read,
2236         .llseek = seq_lseek,
2237         .release = single_release_net,
2238 };
2239 #endif
2240 
2241 /*
2242  *      Set timeout values for tcp tcpfin udp in the timeout_table.
2243  */
2244 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2245 {
2246 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2247         struct ip_vs_proto_data *pd;
2248 #endif
2249 
2250         IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2251                   u->tcp_timeout,
2252                   u->tcp_fin_timeout,
2253                   u->udp_timeout);
2254 
2255 #ifdef CONFIG_IP_VS_PROTO_TCP
2256         if (u->tcp_timeout) {
2257                 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2258                 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2259                         = u->tcp_timeout * HZ;
2260         }
2261 
2262         if (u->tcp_fin_timeout) {
2263                 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2264                 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2265                         = u->tcp_fin_timeout * HZ;
2266         }
2267 #endif
2268 
2269 #ifdef CONFIG_IP_VS_PROTO_UDP
2270         if (u->udp_timeout) {
2271                 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2272                 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2273                         = u->udp_timeout * HZ;
2274         }
2275 #endif
2276         return 0;
2277 }
2278 
2279 #define CMDID(cmd)              (cmd - IP_VS_BASE_CTL)
2280 
2281 struct ip_vs_svcdest_user {
2282         struct ip_vs_service_user       s;
2283         struct ip_vs_dest_user          d;
2284 };
2285 
2286 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2287         [CMDID(IP_VS_SO_SET_ADD)]         = sizeof(struct ip_vs_service_user),
2288         [CMDID(IP_VS_SO_SET_EDIT)]        = sizeof(struct ip_vs_service_user),
2289         [CMDID(IP_VS_SO_SET_DEL)]         = sizeof(struct ip_vs_service_user),
2290         [CMDID(IP_VS_SO_SET_ADDDEST)]     = sizeof(struct ip_vs_svcdest_user),
2291         [CMDID(IP_VS_SO_SET_DELDEST)]     = sizeof(struct ip_vs_svcdest_user),
2292         [CMDID(IP_VS_SO_SET_EDITDEST)]    = sizeof(struct ip_vs_svcdest_user),
2293         [CMDID(IP_VS_SO_SET_TIMEOUT)]     = sizeof(struct ip_vs_timeout_user),
2294         [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2295         [CMDID(IP_VS_SO_SET_STOPDAEMON)]  = sizeof(struct ip_vs_daemon_user),
2296         [CMDID(IP_VS_SO_SET_ZERO)]        = sizeof(struct ip_vs_service_user),
2297 };
2298 
2299 union ip_vs_set_arglen {
2300         struct ip_vs_service_user       field_IP_VS_SO_SET_ADD;
2301         struct ip_vs_service_user       field_IP_VS_SO_SET_EDIT;
2302         struct ip_vs_service_user       field_IP_VS_SO_SET_DEL;
2303         struct ip_vs_svcdest_user       field_IP_VS_SO_SET_ADDDEST;
2304         struct ip_vs_svcdest_user       field_IP_VS_SO_SET_DELDEST;
2305         struct ip_vs_svcdest_user       field_IP_VS_SO_SET_EDITDEST;
2306         struct ip_vs_timeout_user       field_IP_VS_SO_SET_TIMEOUT;
2307         struct ip_vs_daemon_user        field_IP_VS_SO_SET_STARTDAEMON;
2308         struct ip_vs_daemon_user        field_IP_VS_SO_SET_STOPDAEMON;
2309         struct ip_vs_service_user       field_IP_VS_SO_SET_ZERO;
2310 };
2311 
2312 #define MAX_SET_ARGLEN  sizeof(union ip_vs_set_arglen)
2313 
2314 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2315                                   struct ip_vs_service_user *usvc_compat)
2316 {
2317         memset(usvc, 0, sizeof(*usvc));
2318 
2319         usvc->af                = AF_INET;
2320         usvc->protocol          = usvc_compat->protocol;
2321         usvc->addr.ip           = usvc_compat->addr;
2322         usvc->port              = usvc_compat->port;
2323         usvc->fwmark            = usvc_compat->fwmark;
2324 
2325         /* Deep copy of sched_name is not needed here */
2326         usvc->sched_name        = usvc_compat->sched_name;
2327 
2328         usvc->flags             = usvc_compat->flags;
2329         usvc->timeout           = usvc_compat->timeout;
2330         usvc->netmask           = usvc_compat->netmask;
2331 }
2332 
2333 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2334                                    struct ip_vs_dest_user *udest_compat)
2335 {
2336         memset(udest, 0, sizeof(*udest));
2337 
2338         udest->addr.ip          = udest_compat->addr;
2339         udest->port             = udest_compat->port;
2340         udest->conn_flags       = udest_compat->conn_flags;
2341         udest->weight           = udest_compat->weight;
2342         udest->u_threshold      = udest_compat->u_threshold;
2343         udest->l_threshold      = udest_compat->l_threshold;
2344         udest->af               = AF_INET;
2345 }
2346 
2347 static int
2348 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2349 {
2350         struct net *net = sock_net(sk);
2351         int ret;
2352         unsigned char arg[MAX_SET_ARGLEN];
2353         struct ip_vs_service_user *usvc_compat;
2354         struct ip_vs_service_user_kern usvc;
2355         struct ip_vs_service *svc;
2356         struct ip_vs_dest_user *udest_compat;
2357         struct ip_vs_dest_user_kern udest;
2358         struct netns_ipvs *ipvs = net_ipvs(net);
2359 
2360         BUILD_BUG_ON(sizeof(arg) > 255);
2361         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2362                 return -EPERM;
2363 
2364         if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2365                 return -EINVAL;
2366         if (len != set_arglen[CMDID(cmd)]) {
2367                 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2368                           len, set_arglen[CMDID(cmd)]);
2369                 return -EINVAL;
2370         }
2371 
2372         if (copy_from_user(arg, user, len) != 0)
2373                 return -EFAULT;
2374 
2375         /* increase the module use count */
2376         ip_vs_use_count_inc();
2377 
2378         /* Handle daemons since they have another lock */
2379         if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2380             cmd == IP_VS_SO_SET_STOPDAEMON) {
2381                 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2382 
2383                 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2384                         struct ipvs_sync_daemon_cfg cfg;
2385 
2386                         memset(&cfg, 0, sizeof(cfg));
2387                         strlcpy(cfg.mcast_ifn, dm->mcast_ifn,
2388                                 sizeof(cfg.mcast_ifn));
2389                         cfg.syncid = dm->syncid;
2390                         rtnl_lock();
2391                         mutex_lock(&ipvs->sync_mutex);
2392                         ret = start_sync_thread(ipvs, &cfg, dm->state);
2393                         mutex_unlock(&ipvs->sync_mutex);
2394                         rtnl_unlock();
2395                 } else {
2396                         mutex_lock(&ipvs->sync_mutex);
2397                         ret = stop_sync_thread(ipvs, dm->state);
2398                         mutex_unlock(&ipvs->sync_mutex);
2399                 }
2400                 goto out_dec;
2401         }
2402 
2403         mutex_lock(&__ip_vs_mutex);
2404         if (cmd == IP_VS_SO_SET_FLUSH) {
2405                 /* Flush the virtual service */
2406                 ret = ip_vs_flush(ipvs, false);
2407                 goto out_unlock;
2408         } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2409                 /* Set timeout values for (tcp tcpfin udp) */
2410                 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2411                 goto out_unlock;
2412         }
2413 
2414         usvc_compat = (struct ip_vs_service_user *)arg;
2415         udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2416 
2417         /* We only use the new structs internally, so copy userspace compat
2418          * structs to extended internal versions */
2419         ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2420         ip_vs_copy_udest_compat(&udest, udest_compat);
2421 
2422         if (cmd == IP_VS_SO_SET_ZERO) {
2423                 /* if no service address is set, zero counters in all */
2424                 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2425                         ret = ip_vs_zero_all(ipvs);
2426                         goto out_unlock;
2427                 }
2428         }
2429 
2430         /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2431         if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2432             usvc.protocol != IPPROTO_SCTP) {
2433                 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2434                        usvc.protocol, &usvc.addr.ip,
2435                        ntohs(usvc.port), usvc.sched_name);
2436                 ret = -EFAULT;
2437                 goto out_unlock;
2438         }
2439 
2440         /* Lookup the exact service by <protocol, addr, port> or fwmark */
2441         rcu_read_lock();
2442         if (usvc.fwmark == 0)
2443                 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2444                                            &usvc.addr, usvc.port);
2445         else
2446                 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2447         rcu_read_unlock();
2448 
2449         if (cmd != IP_VS_SO_SET_ADD
2450             && (svc == NULL || svc->protocol != usvc.protocol)) {
2451                 ret = -ESRCH;
2452                 goto out_unlock;
2453         }
2454 
2455         switch (cmd) {
2456         case IP_VS_SO_SET_ADD:
2457                 if (svc != NULL)
2458                         ret = -EEXIST;
2459                 else
2460                         ret = ip_vs_add_service(ipvs, &usvc, &svc);
2461                 break;
2462         case IP_VS_SO_SET_EDIT:
2463                 ret = ip_vs_edit_service(svc, &usvc);
2464                 break;
2465         case IP_VS_SO_SET_DEL:
2466                 ret = ip_vs_del_service(svc);
2467                 if (!ret)
2468                         goto out_unlock;
2469                 break;
2470         case IP_VS_SO_SET_ZERO:
2471                 ret = ip_vs_zero_service(svc);
2472                 break;
2473         case IP_VS_SO_SET_ADDDEST:
2474                 ret = ip_vs_add_dest(svc, &udest);
2475                 break;
2476         case IP_VS_SO_SET_EDITDEST:
2477                 ret = ip_vs_edit_dest(svc, &udest);
2478                 break;
2479         case IP_VS_SO_SET_DELDEST:
2480                 ret = ip_vs_del_dest(svc, &udest);
2481                 break;
2482         default:
2483                 ret = -EINVAL;
2484         }
2485 
2486   out_unlock:
2487         mutex_unlock(&__ip_vs_mutex);
2488   out_dec:
2489         /* decrease the module use count */
2490         ip_vs_use_count_dec();
2491 
2492         return ret;
2493 }
2494 
2495 
2496 static void
2497 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2498 {
2499         struct ip_vs_scheduler *sched;
2500         struct ip_vs_kstats kstats;
2501         char *sched_name;
2502 
2503         sched = rcu_dereference_protected(src->scheduler, 1);
2504         sched_name = sched ? sched->name : "none";
2505         dst->protocol = src->protocol;
2506         dst->addr = src->addr.ip;
2507         dst->port = src->port;
2508         dst->fwmark = src->fwmark;
2509         strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2510         dst->flags = src->flags;
2511         dst->timeout = src->timeout / HZ;
2512         dst->netmask = src->netmask;
2513         dst->num_dests = src->num_dests;
2514         ip_vs_copy_stats(&kstats, &src->stats);
2515         ip_vs_export_stats_user(&dst->stats, &kstats);
2516 }
2517 
2518 static inline int
2519 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2520                             const struct ip_vs_get_services *get,
2521                             struct ip_vs_get_services __user *uptr)
2522 {
2523         int idx, count=0;
2524         struct ip_vs_service *svc;
2525         struct ip_vs_service_entry entry;
2526         int ret = 0;
2527 
2528         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2529                 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2530                         /* Only expose IPv4 entries to old interface */
2531                         if (svc->af != AF_INET || (svc->ipvs != ipvs))
2532                                 continue;
2533 
2534                         if (count >= get->num_services)
2535                                 goto out;
2536                         memset(&entry, 0, sizeof(entry));
2537                         ip_vs_copy_service(&entry, svc);
2538                         if (copy_to_user(&uptr->entrytable[count],
2539                                          &entry, sizeof(entry))) {
2540                                 ret = -EFAULT;
2541                                 goto out;
2542                         }
2543                         count++;
2544                 }
2545         }
2546 
2547         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2548                 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2549                         /* Only expose IPv4 entries to old interface */
2550                         if (svc->af != AF_INET || (svc->ipvs != ipvs))
2551                                 continue;
2552 
2553                         if (count >= get->num_services)
2554                                 goto out;
2555                         memset(&entry, 0, sizeof(entry));
2556                         ip_vs_copy_service(&entry, svc);
2557                         if (copy_to_user(&uptr->entrytable[count],
2558                                          &entry, sizeof(entry))) {
2559                                 ret = -EFAULT;
2560                                 goto out;
2561                         }
2562                         count++;
2563                 }
2564         }
2565 out:
2566         return ret;
2567 }
2568 
2569 static inline int
2570 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2571                          struct ip_vs_get_dests __user *uptr)
2572 {
2573         struct ip_vs_service *svc;
2574         union nf_inet_addr addr = { .ip = get->addr };
2575         int ret = 0;
2576 
2577         rcu_read_lock();
2578         if (get->fwmark)
2579                 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2580         else
2581                 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2582                                            get->port);
2583         rcu_read_unlock();
2584 
2585         if (svc) {
2586                 int count = 0;
2587                 struct ip_vs_dest *dest;
2588                 struct ip_vs_dest_entry entry;
2589                 struct ip_vs_kstats kstats;
2590 
2591                 memset(&entry, 0, sizeof(entry));
2592                 list_for_each_entry(dest, &svc->destinations, n_list) {
2593                         if (count >= get->num_dests)
2594                                 break;
2595 
2596                         /* Cannot expose heterogeneous members via sockopt
2597                          * interface
2598                          */
2599                         if (dest->af != svc->af)
2600                                 continue;
2601 
2602                         entry.addr = dest->addr.ip;
2603                         entry.port = dest->port;
2604                         entry.conn_flags = atomic_read(&dest->conn_flags);
2605                         entry.weight = atomic_read(&dest->weight);
2606                         entry.u_threshold = dest->u_threshold;
2607                         entry.l_threshold = dest->l_threshold;
2608                         entry.activeconns = atomic_read(&dest->activeconns);
2609                         entry.inactconns = atomic_read(&dest->inactconns);
2610                         entry.persistconns = atomic_read(&dest->persistconns);
2611                         ip_vs_copy_stats(&kstats, &dest->stats);
2612                         ip_vs_export_stats_user(&entry.stats, &kstats);
2613                         if (copy_to_user(&uptr->entrytable[count],
2614                                          &entry, sizeof(entry))) {
2615                                 ret = -EFAULT;
2616                                 break;
2617                         }
2618                         count++;
2619                 }
2620         } else
2621                 ret = -ESRCH;
2622         return ret;
2623 }
2624 
2625 static inline void
2626 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2627 {
2628 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2629         struct ip_vs_proto_data *pd;
2630 #endif
2631 
2632         memset(u, 0, sizeof (*u));
2633 
2634 #ifdef CONFIG_IP_VS_PROTO_TCP
2635         pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2636         u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2637         u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2638 #endif
2639 #ifdef CONFIG_IP_VS_PROTO_UDP
2640         pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2641         u->udp_timeout =
2642                         pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2643 #endif
2644 }
2645 
2646 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2647         [CMDID(IP_VS_SO_GET_VERSION)]  = 64,
2648         [CMDID(IP_VS_SO_GET_INFO)]     = sizeof(struct ip_vs_getinfo),
2649         [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2650         [CMDID(IP_VS_SO_GET_SERVICE)]  = sizeof(struct ip_vs_service_entry),
2651         [CMDID(IP_VS_SO_GET_DESTS)]    = sizeof(struct ip_vs_get_dests),
2652         [CMDID(IP_VS_SO_GET_TIMEOUT)]  = sizeof(struct ip_vs_timeout_user),
2653         [CMDID(IP_VS_SO_GET_DAEMON)]   = 2 * sizeof(struct ip_vs_daemon_user),
2654 };
2655 
2656 union ip_vs_get_arglen {
2657         char                            field_IP_VS_SO_GET_VERSION[64];
2658         struct ip_vs_getinfo            field_IP_VS_SO_GET_INFO;
2659         struct ip_vs_get_services       field_IP_VS_SO_GET_SERVICES;
2660         struct ip_vs_service_entry      field_IP_VS_SO_GET_SERVICE;
2661         struct ip_vs_get_dests          field_IP_VS_SO_GET_DESTS;
2662         struct ip_vs_timeout_user       field_IP_VS_SO_GET_TIMEOUT;
2663         struct ip_vs_daemon_user        field_IP_VS_SO_GET_DAEMON[2];
2664 };
2665 
2666 #define MAX_GET_ARGLEN  sizeof(union ip_vs_get_arglen)
2667 
2668 static int
2669 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2670 {
2671         unsigned char arg[MAX_GET_ARGLEN];
2672         int ret = 0;
2673         unsigned int copylen;
2674         struct net *net = sock_net(sk);
2675         struct netns_ipvs *ipvs = net_ipvs(net);
2676 
2677         BUG_ON(!net);
2678         BUILD_BUG_ON(sizeof(arg) > 255);
2679         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2680                 return -EPERM;
2681 
2682         if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2683                 return -EINVAL;
2684 
2685         copylen = get_arglen[CMDID(cmd)];
2686         if (*len < (int) copylen) {
2687                 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2688                 return -EINVAL;
2689         }
2690 
2691         if (copy_from_user(arg, user, copylen) != 0)
2692                 return -EFAULT;
2693         /*
2694          * Handle daemons first since it has its own locking
2695          */
2696         if (cmd == IP_VS_SO_GET_DAEMON) {
2697                 struct ip_vs_daemon_user d[2];
2698 
2699                 memset(&d, 0, sizeof(d));
2700                 mutex_lock(&ipvs->sync_mutex);
2701                 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2702                         d[0].state = IP_VS_STATE_MASTER;
2703                         strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2704                                 sizeof(d[0].mcast_ifn));
2705                         d[0].syncid = ipvs->mcfg.syncid;
2706                 }
2707                 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2708                         d[1].state = IP_VS_STATE_BACKUP;
2709                         strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2710                                 sizeof(d[1].mcast_ifn));
2711                         d[1].syncid = ipvs->bcfg.syncid;
2712                 }
2713                 if (copy_to_user(user, &d, sizeof(d)) != 0)
2714                         ret = -EFAULT;
2715                 mutex_unlock(&ipvs->sync_mutex);
2716                 return ret;
2717         }
2718 
2719         mutex_lock(&__ip_vs_mutex);
2720         switch (cmd) {
2721         case IP_VS_SO_GET_VERSION:
2722         {
2723                 char buf[64];
2724 
2725                 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2726                         NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2727                 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2728                         ret = -EFAULT;
2729                         goto out;
2730                 }
2731                 *len = strlen(buf)+1;
2732         }
2733         break;
2734 
2735         case IP_VS_SO_GET_INFO:
2736         {
2737                 struct ip_vs_getinfo info;
2738                 info.version = IP_VS_VERSION_CODE;
2739                 info.size = ip_vs_conn_tab_size;
2740                 info.num_services = ipvs->num_services;
2741                 if (copy_to_user(user, &info, sizeof(info)) != 0)
2742                         ret = -EFAULT;
2743         }
2744         break;
2745 
2746         case IP_VS_SO_GET_SERVICES:
2747         {
2748                 struct ip_vs_get_services *get;
2749                 int size;
2750 
2751                 get = (struct ip_vs_get_services *)arg;
2752                 size = sizeof(*get) +
2753                         sizeof(struct ip_vs_service_entry) * get->num_services;
2754                 if (*len != size) {
2755                         pr_err("length: %u != %u\n", *len, size);
2756                         ret = -EINVAL;
2757                         goto out;
2758                 }
2759                 ret = __ip_vs_get_service_entries(ipvs, get, user);
2760         }
2761         break;
2762 
2763         case IP_VS_SO_GET_SERVICE:
2764         {
2765                 struct ip_vs_service_entry *entry;
2766                 struct ip_vs_service *svc;
2767                 union nf_inet_addr addr;
2768 
2769                 entry = (struct ip_vs_service_entry *)arg;
2770                 addr.ip = entry->addr;
2771                 rcu_read_lock();
2772                 if (entry->fwmark)
2773                         svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2774                 else
2775                         svc = __ip_vs_service_find(ipvs, AF_INET,
2776                                                    entry->protocol, &addr,
2777                                                    entry->port);
2778                 rcu_read_unlock();
2779                 if (svc) {
2780                         ip_vs_copy_service(entry, svc);
2781                         if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2782                                 ret = -EFAULT;
2783                 } else
2784                         ret = -ESRCH;
2785         }
2786         break;
2787 
2788         case IP_VS_SO_GET_DESTS:
2789         {
2790                 struct ip_vs_get_dests *get;
2791                 int size;
2792 
2793                 get = (struct ip_vs_get_dests *)arg;
2794                 size = sizeof(*get) +
2795                         sizeof(struct ip_vs_dest_entry) * get->num_dests;
2796                 if (*len != size) {
2797                         pr_err("length: %u != %u\n", *len, size);
2798                         ret = -EINVAL;
2799                         goto out;
2800                 }
2801                 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2802         }
2803         break;
2804 
2805         case IP_VS_SO_GET_TIMEOUT:
2806         {
2807                 struct ip_vs_timeout_user t;
2808 
2809                 __ip_vs_get_timeouts(ipvs, &t);
2810                 if (copy_to_user(user, &t, sizeof(t)) != 0)
2811                         ret = -EFAULT;
2812         }
2813         break;
2814 
2815         default:
2816                 ret = -EINVAL;
2817         }
2818 
2819 out:
2820         mutex_unlock(&__ip_vs_mutex);
2821         return ret;
2822 }
2823 
2824 
2825 static struct nf_sockopt_ops ip_vs_sockopts = {
2826         .pf             = PF_INET,
2827         .set_optmin     = IP_VS_BASE_CTL,
2828         .set_optmax     = IP_VS_SO_SET_MAX+1,
2829         .set            = do_ip_vs_set_ctl,
2830         .get_optmin     = IP_VS_BASE_CTL,
2831         .get_optmax     = IP_VS_SO_GET_MAX+1,
2832         .get            = do_ip_vs_get_ctl,
2833         .owner          = THIS_MODULE,
2834 };
2835 
2836 /*
2837  * Generic Netlink interface
2838  */
2839 
2840 /* IPVS genetlink family */
2841 static struct genl_family ip_vs_genl_family;
2842 
2843 /* Policy used for first-level command attributes */
2844 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2845         [IPVS_CMD_ATTR_SERVICE]         = { .type = NLA_NESTED },
2846         [IPVS_CMD_ATTR_DEST]            = { .type = NLA_NESTED },
2847         [IPVS_CMD_ATTR_DAEMON]          = { .type = NLA_NESTED },
2848         [IPVS_CMD_ATTR_TIMEOUT_TCP]     = { .type = NLA_U32 },
2849         [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2850         [IPVS_CMD_ATTR_TIMEOUT_UDP]     = { .type = NLA_U32 },
2851 };
2852 
2853 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2854 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2855         [IPVS_DAEMON_ATTR_STATE]        = { .type = NLA_U32 },
2856         [IPVS_DAEMON_ATTR_MCAST_IFN]    = { .type = NLA_NUL_STRING,
2857                                             .len = IP_VS_IFNAME_MAXLEN },
2858         [IPVS_DAEMON_ATTR_SYNC_ID]      = { .type = NLA_U32 },
2859         [IPVS_DAEMON_ATTR_SYNC_MAXLEN]  = { .type = NLA_U16 },
2860         [IPVS_DAEMON_ATTR_MCAST_GROUP]  = { .type = NLA_U32 },
2861         [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2862         [IPVS_DAEMON_ATTR_MCAST_PORT]   = { .type = NLA_U16 },
2863         [IPVS_DAEMON_ATTR_MCAST_TTL]    = { .type = NLA_U8 },
2864 };
2865 
2866 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2867 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2868         [IPVS_SVC_ATTR_AF]              = { .type = NLA_U16 },
2869         [IPVS_SVC_ATTR_PROTOCOL]        = { .type = NLA_U16 },
2870         [IPVS_SVC_ATTR_ADDR]            = { .type = NLA_BINARY,
2871                                             .len = sizeof(union nf_inet_addr) },
2872         [IPVS_SVC_ATTR_PORT]            = { .type = NLA_U16 },
2873         [IPVS_SVC_ATTR_FWMARK]          = { .type = NLA_U32 },
2874         [IPVS_SVC_ATTR_SCHED_NAME]      = { .type = NLA_NUL_STRING,
2875                                             .len = IP_VS_SCHEDNAME_MAXLEN },
2876         [IPVS_SVC_ATTR_PE_NAME]         = { .type = NLA_NUL_STRING,
2877                                             .len = IP_VS_PENAME_MAXLEN },
2878         [IPVS_SVC_ATTR_FLAGS]           = { .type = NLA_BINARY,
2879                                             .len = sizeof(struct ip_vs_flags) },
2880         [IPVS_SVC_ATTR_TIMEOUT]         = { .type = NLA_U32 },
2881         [IPVS_SVC_ATTR_NETMASK]         = { .type = NLA_U32 },
2882         [IPVS_SVC_ATTR_STATS]           = { .type = NLA_NESTED },
2883 };
2884 
2885 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2886 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2887         [IPVS_DEST_ATTR_ADDR]           = { .type = NLA_BINARY,
2888                                             .len = sizeof(union nf_inet_addr) },
2889         [IPVS_DEST_ATTR_PORT]           = { .type = NLA_U16 },
2890         [IPVS_DEST_ATTR_FWD_METHOD]     = { .type = NLA_U32 },
2891         [IPVS_DEST_ATTR_WEIGHT]         = { .type = NLA_U32 },
2892         [IPVS_DEST_ATTR_U_THRESH]       = { .type = NLA_U32 },
2893         [IPVS_DEST_ATTR_L_THRESH]       = { .type = NLA_U32 },
2894         [IPVS_DEST_ATTR_ACTIVE_CONNS]   = { .type = NLA_U32 },
2895         [IPVS_DEST_ATTR_INACT_CONNS]    = { .type = NLA_U32 },
2896         [IPVS_DEST_ATTR_PERSIST_CONNS]  = { .type = NLA_U32 },
2897         [IPVS_DEST_ATTR_STATS]          = { .type = NLA_NESTED },
2898         [IPVS_DEST_ATTR_ADDR_FAMILY]    = { .type = NLA_U16 },
2899 };
2900 
2901 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2902                                  struct ip_vs_kstats *kstats)
2903 {
2904         struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2905 
2906         if (!nl_stats)
2907                 return -EMSGSIZE;
2908 
2909         if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2910             nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2911             nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2912             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2913                               IPVS_STATS_ATTR_PAD) ||
2914             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2915                               IPVS_STATS_ATTR_PAD) ||
2916             nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2917             nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2918             nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2919             nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2920             nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2921                 goto nla_put_failure;
2922         nla_nest_end(skb, nl_stats);
2923 
2924         return 0;
2925 
2926 nla_put_failure:
2927         nla_nest_cancel(skb, nl_stats);
2928         return -EMSGSIZE;
2929 }
2930 
2931 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2932                                    struct ip_vs_kstats *kstats)
2933 {
2934         struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2935 
2936         if (!nl_stats)
2937                 return -EMSGSIZE;
2938 
2939         if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
2940                               IPVS_STATS_ATTR_PAD) ||
2941             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
2942                               IPVS_STATS_ATTR_PAD) ||
2943             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
2944                               IPVS_STATS_ATTR_PAD) ||
2945             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2946                               IPVS_STATS_ATTR_PAD) ||
2947             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2948                               IPVS_STATS_ATTR_PAD) ||
2949             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
2950                               IPVS_STATS_ATTR_PAD) ||
2951             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
2952                               IPVS_STATS_ATTR_PAD) ||
2953             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
2954                               IPVS_STATS_ATTR_PAD) ||
2955             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
2956                               IPVS_STATS_ATTR_PAD) ||
2957             nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
2958                               IPVS_STATS_ATTR_PAD))
2959                 goto nla_put_failure;
2960         nla_nest_end(skb, nl_stats);
2961 
2962         return 0;
2963 
2964 nla_put_failure:
2965         nla_nest_cancel(skb, nl_stats);
2966         return -EMSGSIZE;
2967 }
2968 
2969 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2970                                    struct ip_vs_service *svc)
2971 {
2972         struct ip_vs_scheduler *sched;
2973         struct ip_vs_pe *pe;
2974         struct nlattr *nl_service;
2975         struct ip_vs_flags flags = { .flags = svc->flags,
2976                                      .mask = ~0 };
2977         struct ip_vs_kstats kstats;
2978         char *sched_name;
2979 
2980         nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2981         if (!nl_service)
2982                 return -EMSGSIZE;
2983 
2984         if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2985                 goto nla_put_failure;
2986         if (svc->fwmark) {
2987                 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2988                         goto nla_put_failure;
2989         } else {
2990                 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2991                     nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2992                     nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2993                         goto nla_put_failure;
2994         }
2995 
2996         sched = rcu_dereference_protected(svc->scheduler, 1);
2997         sched_name = sched ? sched->name : "none";
2998         pe = rcu_dereference_protected(svc->pe, 1);
2999         if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
3000             (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
3001             nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3002             nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
3003             nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
3004                 goto nla_put_failure;
3005         ip_vs_copy_stats(&kstats, &svc->stats);
3006         if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3007                 goto nla_put_failure;
3008         if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
3009                 goto nla_put_failure;
3010 
3011         nla_nest_end(skb, nl_service);
3012 
3013         return 0;
3014 
3015 nla_put_failure:
3016         nla_nest_cancel(skb, nl_service);
3017         return -EMSGSIZE;
3018 }
3019 
3020 static int ip_vs_genl_dump_service(struct sk_buff *skb,
3021                                    struct ip_vs_service *svc,
3022                                    struct netlink_callback *cb)
3023 {
3024         void *hdr;
3025 
3026         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3027                           &ip_vs_genl_family, NLM_F_MULTI,
3028                           IPVS_CMD_NEW_SERVICE);
3029         if (!hdr)
3030                 return -EMSGSIZE;
3031 
3032         if (ip_vs_genl_fill_service(skb, svc) < 0)
3033                 goto nla_put_failure;
3034 
3035         genlmsg_end(skb, hdr);
3036         return 0;
3037 
3038 nla_put_failure:
3039         genlmsg_cancel(skb, hdr);
3040         return -EMSGSIZE;
3041 }
3042 
3043 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3044                                     struct netlink_callback *cb)
3045 {
3046         int idx = 0, i;
3047         int start = cb->args[0];
3048         struct ip_vs_service *svc;
3049         struct net *net = sock_net(skb->sk);
3050         struct netns_ipvs *ipvs = net_ipvs(net);
3051 
3052         mutex_lock(&__ip_vs_mutex);
3053         for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3054                 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3055                         if (++idx <= start || (svc->ipvs != ipvs))
3056                                 continue;
3057                         if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3058                                 idx--;
3059                                 goto nla_put_failure;
3060                         }
3061                 }
3062         }
3063 
3064         for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3065                 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3066                         if (++idx <= start || (svc->ipvs != ipvs))
3067                                 continue;
3068                         if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3069                                 idx--;
3070                                 goto nla_put_failure;
3071                         }
3072                 }
3073         }
3074 
3075 nla_put_failure:
3076         mutex_unlock(&__ip_vs_mutex);
3077         cb->args[0] = idx;
3078 
3079         return skb->len;
3080 }
3081 
3082 static bool ip_vs_is_af_valid(int af)
3083 {
3084         if (af == AF_INET)
3085                 return true;
3086 #ifdef CONFIG_IP_VS_IPV6
3087         if (af == AF_INET6 && ipv6_mod_enabled())
3088                 return true;
3089 #endif
3090         return false;
3091 }
3092 
3093 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3094                                     struct ip_vs_service_user_kern *usvc,
3095                                     struct nlattr *nla, int full_entry,
3096                                     struct ip_vs_service **ret_svc)
3097 {
3098         struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3099         struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3100         struct ip_vs_service *svc;
3101 
3102         /* Parse mandatory identifying service fields first */
3103         if (nla == NULL ||
3104             nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla,
3105                              ip_vs_svc_policy, NULL))
3106                 return -EINVAL;
3107 
3108         nla_af          = attrs[IPVS_SVC_ATTR_AF];
3109         nla_protocol    = attrs[IPVS_SVC_ATTR_PROTOCOL];
3110         nla_addr        = attrs[IPVS_SVC_ATTR_ADDR];
3111         nla_port        = attrs[IPVS_SVC_ATTR_PORT];
3112         nla_fwmark      = attrs[IPVS_SVC_ATTR_FWMARK];
3113 
3114         if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3115                 return -EINVAL;
3116 
3117         memset(usvc, 0, sizeof(*usvc));
3118 
3119         usvc->af = nla_get_u16(nla_af);
3120         if (!ip_vs_is_af_valid(usvc->af))
3121                 return -EAFNOSUPPORT;
3122 
3123         if (nla_fwmark) {
3124                 usvc->protocol = IPPROTO_TCP;
3125                 usvc->fwmark = nla_get_u32(nla_fwmark);
3126         } else {
3127                 usvc->protocol = nla_get_u16(nla_protocol);
3128                 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3129                 usvc->port = nla_get_be16(nla_port);
3130                 usvc->fwmark = 0;
3131         }
3132 
3133         rcu_read_lock();
3134         if (usvc->fwmark)
3135                 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3136         else
3137                 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3138                                            &usvc->addr, usvc->port);
3139         rcu_read_unlock();
3140         *ret_svc = svc;
3141 
3142         /* If a full entry was requested, check for the additional fields */
3143         if (full_entry) {
3144                 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3145                               *nla_netmask;
3146                 struct ip_vs_flags flags;
3147 
3148                 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3149                 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3150                 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3151                 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3152                 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3153 
3154                 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3155                         return -EINVAL;
3156 
3157                 nla_memcpy(&flags, nla_flags, sizeof(flags));
3158 
3159                 /* prefill flags from service if it already exists */
3160                 if (svc)
3161                         usvc->flags = svc->flags;
3162 
3163                 /* set new flags from userland */
3164                 usvc->flags = (usvc->flags & ~flags.mask) |
3165                               (flags.flags & flags.mask);
3166                 usvc->sched_name = nla_data(nla_sched);
3167                 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3168                 usvc->timeout = nla_get_u32(nla_timeout);
3169                 usvc->netmask = nla_get_be32(nla_netmask);
3170         }
3171 
3172         return 0;
3173 }
3174 
3175 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3176                                                      struct nlattr *nla)
3177 {
3178         struct ip_vs_service_user_kern usvc;
3179         struct ip_vs_service *svc;
3180         int ret;
3181 
3182         ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, 0, &svc);
3183         return ret ? ERR_PTR(ret) : svc;
3184 }
3185 
3186 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3187 {
3188         struct nlattr *nl_dest;
3189         struct ip_vs_kstats kstats;
3190 
3191         nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3192         if (!nl_dest)
3193                 return -EMSGSIZE;
3194 
3195         if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3196             nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3197             nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3198                         (atomic_read(&dest->conn_flags) &
3199                          IP_VS_CONN_F_FWD_MASK)) ||
3200             nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3201                         atomic_read(&dest->weight)) ||
3202             nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3203             nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3204             nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3205                         atomic_read(&dest->activeconns)) ||
3206             nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3207                         atomic_read(&dest->inactconns)) ||
3208             nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3209                         atomic_read(&dest->persistconns)) ||
3210             nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3211                 goto nla_put_failure;
3212         ip_vs_copy_stats(&kstats, &dest->stats);
3213         if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3214                 goto nla_put_failure;
3215         if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3216                 goto nla_put_failure;
3217 
3218         nla_nest_end(skb, nl_dest);
3219 
3220         return 0;
3221 
3222 nla_put_failure:
3223         nla_nest_cancel(skb, nl_dest);
3224         return -EMSGSIZE;
3225 }
3226 
3227 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3228                                 struct netlink_callback *cb)
3229 {
3230         void *hdr;
3231 
3232         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3233                           &ip_vs_genl_family, NLM_F_MULTI,
3234                           IPVS_CMD_NEW_DEST);
3235         if (!hdr)
3236                 return -EMSGSIZE;
3237 
3238         if (ip_vs_genl_fill_dest(skb, dest) < 0)
3239                 goto nla_put_failure;
3240 
3241         genlmsg_end(skb, hdr);
3242         return 0;
3243 
3244 nla_put_failure:
3245         genlmsg_cancel(skb, hdr);
3246         return -EMSGSIZE;
3247 }
3248 
3249 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3250                                  struct netlink_callback *cb)
3251 {
3252         int idx = 0;
3253         int start = cb->args[0];
3254         struct ip_vs_service *svc;
3255         struct ip_vs_dest *dest;
3256         struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3257         struct net *net = sock_net(skb->sk);
3258         struct netns_ipvs *ipvs = net_ipvs(net);
3259 
3260         mutex_lock(&__ip_vs_mutex);
3261 
3262         /* Try to find the service for which to dump destinations */
3263         if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX,
3264                         ip_vs_cmd_policy, NULL))
3265                 goto out_err;
3266 
3267 
3268         svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3269         if (IS_ERR_OR_NULL(svc))
3270                 goto out_err;
3271 
3272         /* Dump the destinations */
3273         list_for_each_entry(dest, &svc->destinations, n_list) {
3274                 if (++idx <= start)
3275                         continue;
3276                 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3277                         idx--;
3278                         goto nla_put_failure;
3279                 }
3280         }
3281 
3282 nla_put_failure:
3283         cb->args[0] = idx;
3284 
3285 out_err:
3286         mutex_unlock(&__ip_vs_mutex);
3287 
3288         return skb->len;
3289 }
3290 
3291 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3292                                  struct nlattr *nla, int full_entry)
3293 {
3294         struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3295         struct nlattr *nla_addr, *nla_port;
3296         struct nlattr *nla_addr_family;
3297 
3298         /* Parse mandatory identifying destination fields first */
3299         if (nla == NULL ||
3300             nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla,
3301                              ip_vs_dest_policy, NULL))
3302                 return -EINVAL;
3303 
3304         nla_addr        = attrs[IPVS_DEST_ATTR_ADDR];
3305         nla_port        = attrs[IPVS_DEST_ATTR_PORT];
3306         nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3307 
3308         if (!(nla_addr && nla_port))
3309                 return -EINVAL;
3310 
3311         memset(udest, 0, sizeof(*udest));
3312 
3313         nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3314         udest->port = nla_get_be16(nla_port);
3315 
3316         if (nla_addr_family)
3317                 udest->af = nla_get_u16(nla_addr_family);
3318         else
3319                 udest->af = 0;
3320 
3321         /* If a full entry was requested, check for the additional fields */
3322         if (full_entry) {
3323                 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3324                               *nla_l_thresh;
3325 
3326                 nla_fwd         = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3327                 nla_weight      = attrs[IPVS_DEST_ATTR_WEIGHT];
3328                 nla_u_thresh    = attrs[IPVS_DEST_ATTR_U_THRESH];
3329                 nla_l_thresh    = attrs[IPVS_DEST_ATTR_L_THRESH];
3330 
3331                 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3332                         return -EINVAL;
3333 
3334                 udest->conn_flags = nla_get_u32(nla_fwd)
3335                                     & IP_VS_CONN_F_FWD_MASK;
3336                 udest->weight = nla_get_u32(nla_weight);
3337                 udest->u_threshold = nla_get_u32(nla_u_thresh);
3338                 udest->l_threshold = nla_get_u32(nla_l_thresh);
3339         }
3340 
3341         return 0;
3342 }
3343 
3344 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3345                                   struct ipvs_sync_daemon_cfg *c)
3346 {
3347         struct nlattr *nl_daemon;
3348 
3349         nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3350         if (!nl_daemon)
3351                 return -EMSGSIZE;
3352 
3353         if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3354             nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3355             nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3356             nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3357             nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3358             nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3359                 goto nla_put_failure;
3360 #ifdef CONFIG_IP_VS_IPV6
3361         if (c->mcast_af == AF_INET6) {
3362                 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3363                                      &c->mcast_group.in6))
3364                         goto nla_put_failure;
3365         } else
3366 #endif
3367                 if (c->mcast_af == AF_INET &&
3368                     nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3369                                     c->mcast_group.ip))
3370                         goto nla_put_failure;
3371         nla_nest_end(skb, nl_daemon);
3372 
3373         return 0;
3374 
3375 nla_put_failure:
3376         nla_nest_cancel(skb, nl_daemon);
3377         return -EMSGSIZE;
3378 }
3379 
3380 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3381                                   struct ipvs_sync_daemon_cfg *c,
3382                                   struct netlink_callback *cb)
3383 {
3384         void *hdr;
3385         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3386                           &ip_vs_genl_family, NLM_F_MULTI,
3387                           IPVS_CMD_NEW_DAEMON);
3388         if (!hdr)
3389                 return -EMSGSIZE;
3390 
3391         if (ip_vs_genl_fill_daemon(skb, state, c))
3392                 goto nla_put_failure;
3393 
3394         genlmsg_end(skb, hdr);
3395         return 0;
3396 
3397 nla_put_failure:
3398         genlmsg_cancel(skb, hdr);
3399         return -EMSGSIZE;
3400 }
3401 
3402 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3403                                    struct netlink_callback *cb)
3404 {
3405         struct net *net = sock_net(skb->sk);
3406         struct netns_ipvs *ipvs = net_ipvs(net);
3407 
3408         mutex_lock(&ipvs->sync_mutex);
3409         if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3410                 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3411                                            &ipvs->mcfg, cb) < 0)
3412                         goto nla_put_failure;
3413 
3414                 cb->args[0] = 1;
3415         }
3416 
3417         if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3418                 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3419                                            &ipvs->bcfg, cb) < 0)
3420                         goto nla_put_failure;
3421 
3422                 cb->args[1] = 1;
3423         }
3424 
3425 nla_put_failure:
3426         mutex_unlock(&ipvs->sync_mutex);
3427 
3428         return skb->len;
3429 }
3430 
3431 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3432 {
3433         struct ipvs_sync_daemon_cfg c;
3434         struct nlattr *a;
3435         int ret;
3436 
3437         memset(&c, 0, sizeof(c));
3438         if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3439               attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3440               attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3441                 return -EINVAL;
3442         strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3443                 sizeof(c.mcast_ifn));
3444         c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3445 
3446         a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3447         if (a)
3448                 c.sync_maxlen = nla_get_u16(a);
3449 
3450         a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3451         if (a) {
3452                 c.mcast_af = AF_INET;
3453                 c.mcast_group.ip = nla_get_in_addr(a);
3454                 if (!ipv4_is_multicast(c.mcast_group.ip))
3455                         return -EINVAL;
3456         } else {
3457                 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3458                 if (a) {
3459 #ifdef CONFIG_IP_VS_IPV6
3460                         int addr_type;
3461 
3462                         c.mcast_af = AF_INET6;
3463                         c.mcast_group.in6 = nla_get_in6_addr(a);
3464                         addr_type = ipv6_addr_type(&c.mcast_group.in6);
3465                         if (!(addr_type & IPV6_ADDR_MULTICAST))
3466                                 return -EINVAL;
3467 #else
3468                         return -EAFNOSUPPORT;
3469 #endif
3470                 }
3471         }
3472 
3473         a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3474         if (a)
3475                 c.mcast_port = nla_get_u16(a);
3476 
3477         a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3478         if (a)
3479                 c.mcast_ttl = nla_get_u8(a);
3480 
3481         /* The synchronization protocol is incompatible with mixed family
3482          * services
3483          */
3484         if (ipvs->mixed_address_family_dests > 0)
3485                 return -EINVAL;
3486 
3487         rtnl_lock();
3488         mutex_lock(&ipvs->sync_mutex);
3489         ret = start_sync_thread(ipvs, &c,
3490                                 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3491         mutex_unlock(&ipvs->sync_mutex);
3492         rtnl_unlock();
3493         return ret;
3494 }
3495 
3496 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3497 {
3498         int ret;
3499 
3500         if (!attrs[IPVS_DAEMON_ATTR_STATE])
3501                 return -EINVAL;
3502 
3503         mutex_lock(&ipvs->sync_mutex);
3504         ret = stop_sync_thread(ipvs,
3505                                nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3506         mutex_unlock(&ipvs->sync_mutex);
3507         return ret;
3508 }
3509 
3510 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3511 {
3512         struct ip_vs_timeout_user t;
3513 
3514         __ip_vs_get_timeouts(ipvs, &t);
3515 
3516         if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3517                 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3518 
3519         if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3520                 t.tcp_fin_timeout =
3521                         nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3522 
3523         if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3524                 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3525 
3526         return ip_vs_set_timeout(ipvs, &t);
3527 }
3528 
3529 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3530 {
3531         int ret = -EINVAL, cmd;
3532         struct net *net = sock_net(skb->sk);
3533         struct netns_ipvs *ipvs = net_ipvs(net);
3534 
3535         cmd = info->genlhdr->cmd;
3536 
3537         if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3538                 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3539 
3540                 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3541                     nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3542                                      info->attrs[IPVS_CMD_ATTR_DAEMON],
3543                                      ip_vs_daemon_policy, info->extack))
3544                         goto out;
3545 
3546                 if (cmd == IPVS_CMD_NEW_DAEMON)
3547                         ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3548                 else
3549                         ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3550         }
3551 
3552 out:
3553         return ret;
3554 }
3555 
3556 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3557 {
3558         struct ip_vs_service *svc = NULL;
3559         struct ip_vs_service_user_kern usvc;
3560         struct ip_vs_dest_user_kern udest;
3561         int ret = 0, cmd;
3562         int need_full_svc = 0, need_full_dest = 0;
3563         struct net *net = sock_net(skb->sk);
3564         struct netns_ipvs *ipvs = net_ipvs(net);
3565 
3566         cmd = info->genlhdr->cmd;
3567 
3568         mutex_lock(&__ip_vs_mutex);
3569 
3570         if (cmd == IPVS_CMD_FLUSH) {
3571                 ret = ip_vs_flush(ipvs, false);
3572                 goto out;
3573         } else if (cmd == IPVS_CMD_SET_CONFIG) {
3574                 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3575                 goto out;
3576         } else if (cmd == IPVS_CMD_ZERO &&
3577                    !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3578                 ret = ip_vs_zero_all(ipvs);
3579                 goto out;
3580         }
3581 
3582         /* All following commands require a service argument, so check if we
3583          * received a valid one. We need a full service specification when
3584          * adding / editing a service. Only identifying members otherwise. */
3585         if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3586                 need_full_svc = 1;
3587 
3588         ret = ip_vs_genl_parse_service(ipvs, &usvc,
3589                                        info->attrs[IPVS_CMD_ATTR_SERVICE],
3590                                        need_full_svc, &svc);
3591         if (ret)
3592                 goto out;
3593 
3594         /* Unless we're adding a new service, the service must already exist */
3595         if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3596                 ret = -ESRCH;
3597                 goto out;
3598         }
3599 
3600         /* Destination commands require a valid destination argument. For
3601          * adding / editing a destination, we need a full destination
3602          * specification. */
3603         if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3604             cmd == IPVS_CMD_DEL_DEST) {
3605                 if (cmd != IPVS_CMD_DEL_DEST)
3606                         need_full_dest = 1;
3607 
3608                 ret = ip_vs_genl_parse_dest(&udest,
3609                                             info->attrs[IPVS_CMD_ATTR_DEST],
3610                                             need_full_dest);
3611                 if (ret)
3612                         goto out;
3613 
3614                 /* Old protocols did not allow the user to specify address
3615                  * family, so we set it to zero instead.  We also didn't
3616                  * allow heterogeneous pools in the old code, so it's safe
3617                  * to assume that this will have the same address family as
3618                  * the service.
3619                  */
3620                 if (udest.af == 0)
3621                         udest.af = svc->af;
3622 
3623                 if (!ip_vs_is_af_valid(udest.af)) {
3624                         ret = -EAFNOSUPPORT;
3625                         goto out;
3626                 }
3627 
3628                 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3629                         /* The synchronization protocol is incompatible
3630                          * with mixed family services
3631                          */
3632                         if (ipvs->sync_state) {
3633                                 ret = -EINVAL;
3634                                 goto out;
3635                         }
3636 
3637                         /* Which connection types do we support? */
3638                         switch (udest.conn_flags) {
3639                         case IP_VS_CONN_F_TUNNEL:
3640                                 /* We are able to forward this */
3641                                 break;
3642                         default:
3643                                 ret = -EINVAL;
3644                                 goto out;
3645                         }
3646                 }
3647         }
3648 
3649         switch (cmd) {
3650         case IPVS_CMD_NEW_SERVICE:
3651                 if (svc == NULL)
3652                         ret = ip_vs_add_service(ipvs, &usvc, &svc);
3653                 else
3654                         ret = -EEXIST;
3655                 break;
3656         case IPVS_CMD_SET_SERVICE:
3657                 ret = ip_vs_edit_service(svc, &usvc);
3658                 break;
3659         case IPVS_CMD_DEL_SERVICE:
3660                 ret = ip_vs_del_service(svc);
3661                 /* do not use svc, it can be freed */
3662                 break;
3663         case IPVS_CMD_NEW_DEST:
3664                 ret = ip_vs_add_dest(svc, &udest);
3665                 break;
3666         case IPVS_CMD_SET_DEST:
3667                 ret = ip_vs_edit_dest(svc, &udest);
3668                 break;
3669         case IPVS_CMD_DEL_DEST:
3670                 ret = ip_vs_del_dest(svc, &udest);
3671                 break;
3672         case IPVS_CMD_ZERO:
3673                 ret = ip_vs_zero_service(svc);
3674                 break;
3675         default:
3676                 ret = -EINVAL;
3677         }
3678 
3679 out:
3680         mutex_unlock(&__ip_vs_mutex);
3681 
3682         return ret;
3683 }
3684 
3685 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3686 {
3687         struct sk_buff *msg;
3688         void *reply;
3689         int ret, cmd, reply_cmd;
3690         struct net *net = sock_net(skb->sk);
3691         struct netns_ipvs *ipvs = net_ipvs(net);
3692 
3693         cmd = info->genlhdr->cmd;
3694 
3695         if (cmd == IPVS_CMD_GET_SERVICE)
3696                 reply_cmd = IPVS_CMD_NEW_SERVICE;
3697         else if (cmd == IPVS_CMD_GET_INFO)
3698                 reply_cmd = IPVS_CMD_SET_INFO;
3699         else if (cmd == IPVS_CMD_GET_CONFIG)
3700                 reply_cmd = IPVS_CMD_SET_CONFIG;
3701         else {
3702                 pr_err("unknown Generic Netlink command\n");
3703                 return -EINVAL;
3704         }
3705 
3706         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3707         if (!msg)
3708                 return -ENOMEM;
3709 
3710         mutex_lock(&__ip_vs_mutex);
3711 
3712         reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3713         if (reply == NULL)
3714                 goto nla_put_failure;
3715 
3716         switch (cmd) {
3717         case IPVS_CMD_GET_SERVICE:
3718         {
3719                 struct ip_vs_service *svc;
3720 
3721                 svc = ip_vs_genl_find_service(ipvs,
3722                                               info->attrs[IPVS_CMD_ATTR_SERVICE]);
3723                 if (IS_ERR(svc)) {
3724                         ret = PTR_ERR(svc);
3725                         goto out_err;
3726                 } else if (svc) {
3727                         ret = ip_vs_genl_fill_service(msg, svc);
3728                         if (ret)
3729                                 goto nla_put_failure;
3730                 } else {
3731                         ret = -ESRCH;
3732                         goto out_err;
3733                 }
3734 
3735                 break;
3736         }
3737 
3738         case IPVS_CMD_GET_CONFIG:
3739         {
3740                 struct ip_vs_timeout_user t;
3741 
3742                 __ip_vs_get_timeouts(ipvs, &t);
3743 #ifdef CONFIG_IP_VS_PROTO_TCP
3744                 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3745                                 t.tcp_timeout) ||
3746                     nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3747                                 t.tcp_fin_timeout))
3748                         goto nla_put_failure;
3749 #endif
3750 #ifdef CONFIG_IP_VS_PROTO_UDP
3751                 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3752                         goto nla_put_failure;
3753 #endif
3754 
3755                 break;
3756         }
3757 
3758         case IPVS_CMD_GET_INFO:
3759                 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3760                                 IP_VS_VERSION_CODE) ||
3761                     nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3762                                 ip_vs_conn_tab_size))
3763                         goto nla_put_failure;
3764                 break;
3765         }
3766 
3767         genlmsg_end(msg, reply);
3768         ret = genlmsg_reply(msg, info);
3769         goto out;
3770 
3771 nla_put_failure:
3772         pr_err("not enough space in Netlink message\n");
3773         ret = -EMSGSIZE;
3774 
3775 out_err:
3776         nlmsg_free(msg);
3777 out:
3778         mutex_unlock(&__ip_vs_mutex);
3779 
3780         return ret;
3781 }
3782 
3783 
3784 static const struct genl_ops ip_vs_genl_ops[] = {
3785         {
3786                 .cmd    = IPVS_CMD_NEW_SERVICE,
3787                 .flags  = GENL_ADMIN_PERM,
3788                 .policy = ip_vs_cmd_policy,
3789                 .doit   = ip_vs_genl_set_cmd,
3790         },
3791         {
3792                 .cmd    = IPVS_CMD_SET_SERVICE,
3793                 .flags  = GENL_ADMIN_PERM,
3794                 .policy = ip_vs_cmd_policy,
3795                 .doit   = ip_vs_genl_set_cmd,
3796         },
3797         {
3798                 .cmd    = IPVS_CMD_DEL_SERVICE,
3799                 .flags  = GENL_ADMIN_PERM,
3800                 .policy = ip_vs_cmd_policy,
3801                 .doit   = ip_vs_genl_set_cmd,
3802         },
3803         {
3804                 .cmd    = IPVS_CMD_GET_SERVICE,
3805                 .flags  = GENL_ADMIN_PERM,
3806                 .doit   = ip_vs_genl_get_cmd,
3807                 .dumpit = ip_vs_genl_dump_services,
3808                 .policy = ip_vs_cmd_policy,
3809         },
3810         {
3811                 .cmd    = IPVS_CMD_NEW_DEST,
3812                 .flags  = GENL_ADMIN_PERM,
3813                 .policy = ip_vs_cmd_policy,
3814                 .doit   = ip_vs_genl_set_cmd,
3815         },
3816         {
3817                 .cmd    = IPVS_CMD_SET_DEST,
3818                 .flags  = GENL_ADMIN_PERM,
3819                 .policy = ip_vs_cmd_policy,
3820                 .doit   = ip_vs_genl_set_cmd,
3821         },
3822         {
3823                 .cmd    = IPVS_CMD_DEL_DEST,
3824                 .flags  = GENL_ADMIN_PERM,
3825                 .policy = ip_vs_cmd_policy,
3826                 .doit   = ip_vs_genl_set_cmd,
3827         },
3828         {
3829                 .cmd    = IPVS_CMD_GET_DEST,
3830                 .flags  = GENL_ADMIN_PERM,
3831                 .policy = ip_vs_cmd_policy,
3832                 .dumpit = ip_vs_genl_dump_dests,
3833         },
3834         {
3835                 .cmd    = IPVS_CMD_NEW_DAEMON,
3836                 .flags  = GENL_ADMIN_PERM,
3837                 .policy = ip_vs_cmd_policy,
3838                 .doit   = ip_vs_genl_set_daemon,
3839         },
3840         {
3841                 .cmd    = IPVS_CMD_DEL_DAEMON,
3842                 .flags  = GENL_ADMIN_PERM,
3843                 .policy = ip_vs_cmd_policy,
3844                 .doit   = ip_vs_genl_set_daemon,
3845         },
3846         {
3847                 .cmd    = IPVS_CMD_GET_DAEMON,
3848                 .flags  = GENL_ADMIN_PERM,
3849                 .dumpit = ip_vs_genl_dump_daemons,
3850         },
3851         {
3852                 .cmd    = IPVS_CMD_SET_CONFIG,
3853                 .flags  = GENL_ADMIN_PERM,
3854                 .policy = ip_vs_cmd_policy,
3855                 .doit   = ip_vs_genl_set_cmd,
3856         },
3857         {
3858                 .cmd    = IPVS_CMD_GET_CONFIG,
3859                 .flags  = GENL_ADMIN_PERM,
3860                 .doit   = ip_vs_genl_get_cmd,
3861         },
3862         {
3863                 .cmd    = IPVS_CMD_GET_INFO,
3864                 .flags  = GENL_ADMIN_PERM,
3865                 .doit   = ip_vs_genl_get_cmd,
3866         },
3867         {
3868                 .cmd    = IPVS_CMD_ZERO,
3869                 .flags  = GENL_ADMIN_PERM,
3870                 .policy = ip_vs_cmd_policy,
3871                 .doit   = ip_vs_genl_set_cmd,
3872         },
3873         {
3874                 .cmd    = IPVS_CMD_FLUSH,
3875                 .flags  = GENL_ADMIN_PERM,
3876                 .doit   = ip_vs_genl_set_cmd,
3877         },
3878 };
3879 
3880 static struct genl_family ip_vs_genl_family __ro_after_init = {
3881         .hdrsize        = 0,
3882         .name           = IPVS_GENL_NAME,
3883         .version        = IPVS_GENL_VERSION,
3884         .maxattr        = IPVS_CMD_ATTR_MAX,
3885         .netnsok        = true,         /* Make ipvsadm to work on netns */
3886         .module         = THIS_MODULE,
3887         .ops            = ip_vs_genl_ops,
3888         .n_ops          = ARRAY_SIZE(ip_vs_genl_ops),
3889 };
3890 
3891 static int __init ip_vs_genl_register(void)
3892 {
3893         return genl_register_family(&ip_vs_genl_family);
3894 }
3895 
3896 static void ip_vs_genl_unregister(void)
3897 {
3898         genl_unregister_family(&ip_vs_genl_family);
3899 }
3900 
3901 /* End of Generic Netlink interface definitions */
3902 
3903 /*
3904  * per netns intit/exit func.
3905  */
3906 #ifdef CONFIG_SYSCTL
3907 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
3908 {
3909         struct net *net = ipvs->net;
3910         int idx;
3911         struct ctl_table *tbl;
3912 
3913         atomic_set(&ipvs->dropentry, 0);
3914         spin_lock_init(&ipvs->dropentry_lock);
3915         spin_lock_init(&ipvs->droppacket_lock);
3916         spin_lock_init(&ipvs->securetcp_lock);
3917 
3918         if (!net_eq(net, &init_net)) {
3919                 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3920                 if (tbl == NULL)
3921                         return -ENOMEM;
3922 
3923                 /* Don't export sysctls to unprivileged users */
3924                 if (net->user_ns != &init_user_ns)
3925                         tbl[0].procname = NULL;
3926         } else
3927                 tbl = vs_vars;
3928         /* Initialize sysctl defaults */
3929         for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
3930                 if (tbl[idx].proc_handler == proc_do_defense_mode)
3931                         tbl[idx].extra2 = ipvs;
3932         }
3933         idx = 0;
3934         ipvs->sysctl_amemthresh = 1024;
3935         tbl[idx++].data = &ipvs->sysctl_amemthresh;
3936         ipvs->sysctl_am_droprate = 10;
3937         tbl[idx++].data = &ipvs->sysctl_am_droprate;
3938         tbl[idx++].data = &ipvs->sysctl_drop_entry;
3939         tbl[idx++].data = &ipvs->sysctl_drop_packet;
3940 #ifdef CONFIG_IP_VS_NFCT
3941         tbl[idx++].data = &ipvs->sysctl_conntrack;
3942 #endif
3943         tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3944         ipvs->sysctl_snat_reroute = 1;
3945         tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3946         ipvs->sysctl_sync_ver = 1;
3947         tbl[idx++].data = &ipvs->sysctl_sync_ver;
3948         ipvs->sysctl_sync_ports = 1;
3949         tbl[idx++].data = &ipvs->sysctl_sync_ports;
3950         tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3951         ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3952         tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3953         ipvs->sysctl_sync_sock_size = 0;
3954         tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3955         tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3956         tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3957         tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3958         tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3959         tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3960         ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3961         ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3962         tbl[idx].data = &ipvs->sysctl_sync_threshold;
3963         tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3964         ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3965         tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3966         ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3967         tbl[idx++].data = &ipvs->sysctl_sync_retries;
3968         tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3969         ipvs->sysctl_pmtu_disc = 1;
3970         tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3971         tbl[idx++].data = &ipvs->sysctl_backup_only;
3972         ipvs->sysctl_conn_reuse_mode = 1;
3973         tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
3974         tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
3975         tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
3976 
3977         ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3978         if (ipvs->sysctl_hdr == NULL) {
3979                 if (!net_eq(net, &init_net))
3980                         kfree(tbl);
3981                 return -ENOMEM;
3982         }
3983         ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
3984         ipvs->sysctl_tbl = tbl;
3985         /* Schedule defense work */
3986         INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3987         schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3988 
3989         return 0;
3990 }
3991 
3992 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
3993 {
3994         struct net *net = ipvs->net;
3995 
3996         cancel_delayed_work_sync(&ipvs->defense_work);
3997         cancel_work_sync(&ipvs->defense_work.work);
3998         unregister_net_sysctl_table(ipvs->sysctl_hdr);
3999         ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
4000 
4001         if (!net_eq(net, &init_net))
4002                 kfree(ipvs->sysctl_tbl);
4003 }
4004 
4005 #else
4006 
4007 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
4008 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
4009 
4010 #endif
4011 
4012 static struct notifier_block ip_vs_dst_notifier = {
4013         .notifier_call = ip_vs_dst_event,
4014 };
4015 
4016 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
4017 {
4018         int i, idx;
4019 
4020         /* Initialize rs_table */
4021         for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
4022                 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
4023 
4024         INIT_LIST_HEAD(&ipvs->dest_trash);
4025         spin_lock_init(&ipvs->dest_trash_lock);
4026         timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
4027         atomic_set(&ipvs->ftpsvc_counter, 0);
4028         atomic_set(&ipvs->nullsvc_counter, 0);
4029         atomic_set(&ipvs->conn_out_counter, 0);
4030 
4031         /* procfs stats */
4032         ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
4033         if (!ipvs->tot_stats.cpustats)
4034                 return -ENOMEM;
4035 
4036         for_each_possible_cpu(i) {
4037                 struct ip_vs_cpu_stats *ipvs_tot_stats;
4038                 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4039                 u64_stats_init(&ipvs_tot_stats->syncp);
4040         }
4041 
4042         spin_lock_init(&ipvs->tot_stats.lock);
4043 
4044         proc_create("ip_vs", 0, ipvs->net->proc_net, &ip_vs_info_fops);
4045         proc_create("ip_vs_stats", 0, ipvs->net->proc_net, &ip_vs_stats_fops);
4046         proc_create("ip_vs_stats_percpu", 0, ipvs->net->proc_net,
4047                     &ip_vs_stats_percpu_fops);
4048 
4049         if (ip_vs_control_net_init_sysctl(ipvs))
4050                 goto err;
4051 
4052         return 0;
4053 
4054 err:
4055         free_percpu(ipvs->tot_stats.cpustats);
4056         return -ENOMEM;
4057 }
4058 
4059 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4060 {
4061         ip_vs_trash_cleanup(ipvs);
4062         ip_vs_control_net_cleanup_sysctl(ipvs);
4063         remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4064         remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4065         remove_proc_entry("ip_vs", ipvs->net->proc_net);
4066         free_percpu(ipvs->tot_stats.cpustats);
4067 }
4068 
4069 int __init ip_vs_register_nl_ioctl(void)
4070 {
4071         int ret;
4072 
4073         ret = nf_register_sockopt(&ip_vs_sockopts);
4074         if (ret) {
4075                 pr_err("cannot register sockopt.\n");
4076                 goto err_sock;
4077         }
4078 
4079         ret = ip_vs_genl_register();
4080         if (ret) {
4081                 pr_err("cannot register Generic Netlink interface.\n");
4082                 goto err_genl;
4083         }
4084         return 0;
4085 
4086 err_genl:
4087         nf_unregister_sockopt(&ip_vs_sockopts);
4088 err_sock:
4089         return ret;
4090 }
4091 
4092 void ip_vs_unregister_nl_ioctl(void)
4093 {
4094         ip_vs_genl_unregister();
4095         nf_unregister_sockopt(&ip_vs_sockopts);
4096 }
4097 
4098 int __init ip_vs_control_init(void)
4099 {
4100         int idx;
4101         int ret;
4102 
4103         EnterFunction(2);
4104 
4105         /* Initialize svc_table, ip_vs_svc_fwm_table */
4106         for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4107                 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4108                 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4109         }
4110 
4111         smp_wmb();      /* Do we really need it now ? */
4112 
4113         ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4114         if (ret < 0)
4115                 return ret;
4116 
4117         LeaveFunction(2);
4118         return 0;
4119 }
4120 
4121 
4122 void ip_vs_control_cleanup(void)
4123 {
4124         EnterFunction(2);
4125         unregister_netdevice_notifier(&ip_vs_dst_notifier);
4126         LeaveFunction(2);
4127 }
4128 

~ [ 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