/kern_oII/net/netfilter/ipvs/ip_vs_ctl.c

http://omnia2droid.googlecode.com/ · C · 3417 lines · 2491 code · 521 blank · 405 comment · 427 complexity · 88f2529b39bc91aa5103e965ee62f588 MD5 · raw file

Large files are truncated click here to view the full file

  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. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/capability.h>
  24. #include <linux/fs.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/swap.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/netfilter.h>
  31. #include <linux/netfilter_ipv4.h>
  32. #include <linux/mutex.h>
  33. #include <net/net_namespace.h>
  34. #include <net/ip.h>
  35. #ifdef CONFIG_IP_VS_IPV6
  36. #include <net/ipv6.h>
  37. #include <net/ip6_route.h>
  38. #endif
  39. #include <net/route.h>
  40. #include <net/sock.h>
  41. #include <net/genetlink.h>
  42. #include <asm/uaccess.h>
  43. #include <net/ip_vs.h>
  44. /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
  45. static DEFINE_MUTEX(__ip_vs_mutex);
  46. /* lock for service table */
  47. static DEFINE_RWLOCK(__ip_vs_svc_lock);
  48. /* lock for table with the real services */
  49. static DEFINE_RWLOCK(__ip_vs_rs_lock);
  50. /* lock for state and timeout tables */
  51. static DEFINE_RWLOCK(__ip_vs_securetcp_lock);
  52. /* lock for drop entry handling */
  53. static DEFINE_SPINLOCK(__ip_vs_dropentry_lock);
  54. /* lock for drop packet handling */
  55. static DEFINE_SPINLOCK(__ip_vs_droppacket_lock);
  56. /* 1/rate drop and drop-entry variables */
  57. int ip_vs_drop_rate = 0;
  58. int ip_vs_drop_counter = 0;
  59. static atomic_t ip_vs_dropentry = ATOMIC_INIT(0);
  60. /* number of virtual services */
  61. static int ip_vs_num_services = 0;
  62. /* sysctl variables */
  63. static int sysctl_ip_vs_drop_entry = 0;
  64. static int sysctl_ip_vs_drop_packet = 0;
  65. static int sysctl_ip_vs_secure_tcp = 0;
  66. static int sysctl_ip_vs_amemthresh = 1024;
  67. static int sysctl_ip_vs_am_droprate = 10;
  68. int sysctl_ip_vs_cache_bypass = 0;
  69. int sysctl_ip_vs_expire_nodest_conn = 0;
  70. int sysctl_ip_vs_expire_quiescent_template = 0;
  71. int sysctl_ip_vs_sync_threshold[2] = { 3, 50 };
  72. int sysctl_ip_vs_nat_icmp_send = 0;
  73. #ifdef CONFIG_IP_VS_DEBUG
  74. static int sysctl_ip_vs_debug_level = 0;
  75. int ip_vs_get_debug_level(void)
  76. {
  77. return sysctl_ip_vs_debug_level;
  78. }
  79. #endif
  80. #ifdef CONFIG_IP_VS_IPV6
  81. /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
  82. static int __ip_vs_addr_is_local_v6(const struct in6_addr *addr)
  83. {
  84. struct rt6_info *rt;
  85. struct flowi fl = {
  86. .oif = 0,
  87. .nl_u = {
  88. .ip6_u = {
  89. .daddr = *addr,
  90. .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
  91. };
  92. rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
  93. if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
  94. return 1;
  95. return 0;
  96. }
  97. #endif
  98. /*
  99. * update_defense_level is called from keventd and from sysctl,
  100. * so it needs to protect itself from softirqs
  101. */
  102. static void update_defense_level(void)
  103. {
  104. struct sysinfo i;
  105. static int old_secure_tcp = 0;
  106. int availmem;
  107. int nomem;
  108. int to_change = -1;
  109. /* we only count free and buffered memory (in pages) */
  110. si_meminfo(&i);
  111. availmem = i.freeram + i.bufferram;
  112. /* however in linux 2.5 the i.bufferram is total page cache size,
  113. we need adjust it */
  114. /* si_swapinfo(&i); */
  115. /* availmem = availmem - (i.totalswap - i.freeswap); */
  116. nomem = (availmem < sysctl_ip_vs_amemthresh);
  117. local_bh_disable();
  118. /* drop_entry */
  119. spin_lock(&__ip_vs_dropentry_lock);
  120. switch (sysctl_ip_vs_drop_entry) {
  121. case 0:
  122. atomic_set(&ip_vs_dropentry, 0);
  123. break;
  124. case 1:
  125. if (nomem) {
  126. atomic_set(&ip_vs_dropentry, 1);
  127. sysctl_ip_vs_drop_entry = 2;
  128. } else {
  129. atomic_set(&ip_vs_dropentry, 0);
  130. }
  131. break;
  132. case 2:
  133. if (nomem) {
  134. atomic_set(&ip_vs_dropentry, 1);
  135. } else {
  136. atomic_set(&ip_vs_dropentry, 0);
  137. sysctl_ip_vs_drop_entry = 1;
  138. };
  139. break;
  140. case 3:
  141. atomic_set(&ip_vs_dropentry, 1);
  142. break;
  143. }
  144. spin_unlock(&__ip_vs_dropentry_lock);
  145. /* drop_packet */
  146. spin_lock(&__ip_vs_droppacket_lock);
  147. switch (sysctl_ip_vs_drop_packet) {
  148. case 0:
  149. ip_vs_drop_rate = 0;
  150. break;
  151. case 1:
  152. if (nomem) {
  153. ip_vs_drop_rate = ip_vs_drop_counter
  154. = sysctl_ip_vs_amemthresh /
  155. (sysctl_ip_vs_amemthresh-availmem);
  156. sysctl_ip_vs_drop_packet = 2;
  157. } else {
  158. ip_vs_drop_rate = 0;
  159. }
  160. break;
  161. case 2:
  162. if (nomem) {
  163. ip_vs_drop_rate = ip_vs_drop_counter
  164. = sysctl_ip_vs_amemthresh /
  165. (sysctl_ip_vs_amemthresh-availmem);
  166. } else {
  167. ip_vs_drop_rate = 0;
  168. sysctl_ip_vs_drop_packet = 1;
  169. }
  170. break;
  171. case 3:
  172. ip_vs_drop_rate = sysctl_ip_vs_am_droprate;
  173. break;
  174. }
  175. spin_unlock(&__ip_vs_droppacket_lock);
  176. /* secure_tcp */
  177. write_lock(&__ip_vs_securetcp_lock);
  178. switch (sysctl_ip_vs_secure_tcp) {
  179. case 0:
  180. if (old_secure_tcp >= 2)
  181. to_change = 0;
  182. break;
  183. case 1:
  184. if (nomem) {
  185. if (old_secure_tcp < 2)
  186. to_change = 1;
  187. sysctl_ip_vs_secure_tcp = 2;
  188. } else {
  189. if (old_secure_tcp >= 2)
  190. to_change = 0;
  191. }
  192. break;
  193. case 2:
  194. if (nomem) {
  195. if (old_secure_tcp < 2)
  196. to_change = 1;
  197. } else {
  198. if (old_secure_tcp >= 2)
  199. to_change = 0;
  200. sysctl_ip_vs_secure_tcp = 1;
  201. }
  202. break;
  203. case 3:
  204. if (old_secure_tcp < 2)
  205. to_change = 1;
  206. break;
  207. }
  208. old_secure_tcp = sysctl_ip_vs_secure_tcp;
  209. if (to_change >= 0)
  210. ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp>1);
  211. write_unlock(&__ip_vs_securetcp_lock);
  212. local_bh_enable();
  213. }
  214. /*
  215. * Timer for checking the defense
  216. */
  217. #define DEFENSE_TIMER_PERIOD 1*HZ
  218. static void defense_work_handler(struct work_struct *work);
  219. static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
  220. static void defense_work_handler(struct work_struct *work)
  221. {
  222. update_defense_level();
  223. if (atomic_read(&ip_vs_dropentry))
  224. ip_vs_random_dropentry();
  225. schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
  226. }
  227. int
  228. ip_vs_use_count_inc(void)
  229. {
  230. return try_module_get(THIS_MODULE);
  231. }
  232. void
  233. ip_vs_use_count_dec(void)
  234. {
  235. module_put(THIS_MODULE);
  236. }
  237. /*
  238. * Hash table: for virtual service lookups
  239. */
  240. #define IP_VS_SVC_TAB_BITS 8
  241. #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
  242. #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
  243. /* the service table hashed by <protocol, addr, port> */
  244. static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
  245. /* the service table hashed by fwmark */
  246. static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
  247. /*
  248. * Hash table: for real service lookups
  249. */
  250. #define IP_VS_RTAB_BITS 4
  251. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  252. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  253. static struct list_head ip_vs_rtable[IP_VS_RTAB_SIZE];
  254. /*
  255. * Trash for destinations
  256. */
  257. static LIST_HEAD(ip_vs_dest_trash);
  258. /*
  259. * FTP & NULL virtual service counters
  260. */
  261. static atomic_t ip_vs_ftpsvc_counter = ATOMIC_INIT(0);
  262. static atomic_t ip_vs_nullsvc_counter = ATOMIC_INIT(0);
  263. /*
  264. * Returns hash value for virtual service
  265. */
  266. static __inline__ unsigned
  267. ip_vs_svc_hashkey(int af, unsigned proto, const union nf_inet_addr *addr,
  268. __be16 port)
  269. {
  270. register unsigned porth = ntohs(port);
  271. __be32 addr_fold = addr->ip;
  272. #ifdef CONFIG_IP_VS_IPV6
  273. if (af == AF_INET6)
  274. addr_fold = addr->ip6[0]^addr->ip6[1]^
  275. addr->ip6[2]^addr->ip6[3];
  276. #endif
  277. return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
  278. & IP_VS_SVC_TAB_MASK;
  279. }
  280. /*
  281. * Returns hash value of fwmark for virtual service lookup
  282. */
  283. static __inline__ unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark)
  284. {
  285. return fwmark & IP_VS_SVC_TAB_MASK;
  286. }
  287. /*
  288. * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
  289. * or in the ip_vs_svc_fwm_table by fwmark.
  290. * Should be called with locked tables.
  291. */
  292. static int ip_vs_svc_hash(struct ip_vs_service *svc)
  293. {
  294. unsigned hash;
  295. if (svc->flags & IP_VS_SVC_F_HASHED) {
  296. IP_VS_ERR("ip_vs_svc_hash(): request for already hashed, "
  297. "called from %p\n", __builtin_return_address(0));
  298. return 0;
  299. }
  300. if (svc->fwmark == 0) {
  301. /*
  302. * Hash it by <protocol,addr,port> in ip_vs_svc_table
  303. */
  304. hash = ip_vs_svc_hashkey(svc->af, svc->protocol, &svc->addr,
  305. svc->port);
  306. list_add(&svc->s_list, &ip_vs_svc_table[hash]);
  307. } else {
  308. /*
  309. * Hash it by fwmark in ip_vs_svc_fwm_table
  310. */
  311. hash = ip_vs_svc_fwm_hashkey(svc->fwmark);
  312. list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
  313. }
  314. svc->flags |= IP_VS_SVC_F_HASHED;
  315. /* increase its refcnt because it is referenced by the svc table */
  316. atomic_inc(&svc->refcnt);
  317. return 1;
  318. }
  319. /*
  320. * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
  321. * Should be called with locked tables.
  322. */
  323. static int ip_vs_svc_unhash(struct ip_vs_service *svc)
  324. {
  325. if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
  326. IP_VS_ERR("ip_vs_svc_unhash(): request for unhash flagged, "
  327. "called from %p\n", __builtin_return_address(0));
  328. return 0;
  329. }
  330. if (svc->fwmark == 0) {
  331. /* Remove it from the ip_vs_svc_table table */
  332. list_del(&svc->s_list);
  333. } else {
  334. /* Remove it from the ip_vs_svc_fwm_table table */
  335. list_del(&svc->f_list);
  336. }
  337. svc->flags &= ~IP_VS_SVC_F_HASHED;
  338. atomic_dec(&svc->refcnt);
  339. return 1;
  340. }
  341. /*
  342. * Get service by {proto,addr,port} in the service table.
  343. */
  344. static inline struct ip_vs_service *
  345. __ip_vs_service_get(int af, __u16 protocol, const union nf_inet_addr *vaddr,
  346. __be16 vport)
  347. {
  348. unsigned hash;
  349. struct ip_vs_service *svc;
  350. /* Check for "full" addressed entries */
  351. hash = ip_vs_svc_hashkey(af, protocol, vaddr, vport);
  352. list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
  353. if ((svc->af == af)
  354. && ip_vs_addr_equal(af, &svc->addr, vaddr)
  355. && (svc->port == vport)
  356. && (svc->protocol == protocol)) {
  357. /* HIT */
  358. atomic_inc(&svc->usecnt);
  359. return svc;
  360. }
  361. }
  362. return NULL;
  363. }
  364. /*
  365. * Get service by {fwmark} in the service table.
  366. */
  367. static inline struct ip_vs_service *
  368. __ip_vs_svc_fwm_get(int af, __u32 fwmark)
  369. {
  370. unsigned hash;
  371. struct ip_vs_service *svc;
  372. /* Check for fwmark addressed entries */
  373. hash = ip_vs_svc_fwm_hashkey(fwmark);
  374. list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) {
  375. if (svc->fwmark == fwmark && svc->af == af) {
  376. /* HIT */
  377. atomic_inc(&svc->usecnt);
  378. return svc;
  379. }
  380. }
  381. return NULL;
  382. }
  383. struct ip_vs_service *
  384. ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
  385. const union nf_inet_addr *vaddr, __be16 vport)
  386. {
  387. struct ip_vs_service *svc;
  388. read_lock(&__ip_vs_svc_lock);
  389. /*
  390. * Check the table hashed by fwmark first
  391. */
  392. if (fwmark && (svc = __ip_vs_svc_fwm_get(af, fwmark)))
  393. goto out;
  394. /*
  395. * Check the table hashed by <protocol,addr,port>
  396. * for "full" addressed entries
  397. */
  398. svc = __ip_vs_service_get(af, protocol, vaddr, vport);
  399. if (svc == NULL
  400. && protocol == IPPROTO_TCP
  401. && atomic_read(&ip_vs_ftpsvc_counter)
  402. && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
  403. /*
  404. * Check if ftp service entry exists, the packet
  405. * might belong to FTP data connections.
  406. */
  407. svc = __ip_vs_service_get(af, protocol, vaddr, FTPPORT);
  408. }
  409. if (svc == NULL
  410. && atomic_read(&ip_vs_nullsvc_counter)) {
  411. /*
  412. * Check if the catch-all port (port zero) exists
  413. */
  414. svc = __ip_vs_service_get(af, protocol, vaddr, 0);
  415. }
  416. out:
  417. read_unlock(&__ip_vs_svc_lock);
  418. IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
  419. fwmark, ip_vs_proto_name(protocol),
  420. IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
  421. svc ? "hit" : "not hit");
  422. return svc;
  423. }
  424. static inline void
  425. __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  426. {
  427. atomic_inc(&svc->refcnt);
  428. dest->svc = svc;
  429. }
  430. static inline void
  431. __ip_vs_unbind_svc(struct ip_vs_dest *dest)
  432. {
  433. struct ip_vs_service *svc = dest->svc;
  434. dest->svc = NULL;
  435. if (atomic_dec_and_test(&svc->refcnt))
  436. kfree(svc);
  437. }
  438. /*
  439. * Returns hash value for real service
  440. */
  441. static inline unsigned ip_vs_rs_hashkey(int af,
  442. const union nf_inet_addr *addr,
  443. __be16 port)
  444. {
  445. register unsigned porth = ntohs(port);
  446. __be32 addr_fold = addr->ip;
  447. #ifdef CONFIG_IP_VS_IPV6
  448. if (af == AF_INET6)
  449. addr_fold = addr->ip6[0]^addr->ip6[1]^
  450. addr->ip6[2]^addr->ip6[3];
  451. #endif
  452. return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
  453. & IP_VS_RTAB_MASK;
  454. }
  455. /*
  456. * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
  457. * should be called with locked tables.
  458. */
  459. static int ip_vs_rs_hash(struct ip_vs_dest *dest)
  460. {
  461. unsigned hash;
  462. if (!list_empty(&dest->d_list)) {
  463. return 0;
  464. }
  465. /*
  466. * Hash by proto,addr,port,
  467. * which are the parameters of the real service.
  468. */
  469. hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
  470. list_add(&dest->d_list, &ip_vs_rtable[hash]);
  471. return 1;
  472. }
  473. /*
  474. * UNhashes ip_vs_dest from ip_vs_rtable.
  475. * should be called with locked tables.
  476. */
  477. static int ip_vs_rs_unhash(struct ip_vs_dest *dest)
  478. {
  479. /*
  480. * Remove it from the ip_vs_rtable table.
  481. */
  482. if (!list_empty(&dest->d_list)) {
  483. list_del(&dest->d_list);
  484. INIT_LIST_HEAD(&dest->d_list);
  485. }
  486. return 1;
  487. }
  488. /*
  489. * Lookup real service by <proto,addr,port> in the real service table.
  490. */
  491. struct ip_vs_dest *
  492. ip_vs_lookup_real_service(int af, __u16 protocol,
  493. const union nf_inet_addr *daddr,
  494. __be16 dport)
  495. {
  496. unsigned hash;
  497. struct ip_vs_dest *dest;
  498. /*
  499. * Check for "full" addressed entries
  500. * Return the first found entry
  501. */
  502. hash = ip_vs_rs_hashkey(af, daddr, dport);
  503. read_lock(&__ip_vs_rs_lock);
  504. list_for_each_entry(dest, &ip_vs_rtable[hash], d_list) {
  505. if ((dest->af == af)
  506. && ip_vs_addr_equal(af, &dest->addr, daddr)
  507. && (dest->port == dport)
  508. && ((dest->protocol == protocol) ||
  509. dest->vfwmark)) {
  510. /* HIT */
  511. read_unlock(&__ip_vs_rs_lock);
  512. return dest;
  513. }
  514. }
  515. read_unlock(&__ip_vs_rs_lock);
  516. return NULL;
  517. }
  518. /*
  519. * Lookup destination by {addr,port} in the given service
  520. */
  521. static struct ip_vs_dest *
  522. ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
  523. __be16 dport)
  524. {
  525. struct ip_vs_dest *dest;
  526. /*
  527. * Find the destination for the given service
  528. */
  529. list_for_each_entry(dest, &svc->destinations, n_list) {
  530. if ((dest->af == svc->af)
  531. && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
  532. && (dest->port == dport)) {
  533. /* HIT */
  534. return dest;
  535. }
  536. }
  537. return NULL;
  538. }
  539. /*
  540. * Find destination by {daddr,dport,vaddr,protocol}
  541. * Cretaed to be used in ip_vs_process_message() in
  542. * the backup synchronization daemon. It finds the
  543. * destination to be bound to the received connection
  544. * on the backup.
  545. *
  546. * ip_vs_lookup_real_service() looked promissing, but
  547. * seems not working as expected.
  548. */
  549. struct ip_vs_dest *ip_vs_find_dest(int af, const union nf_inet_addr *daddr,
  550. __be16 dport,
  551. const union nf_inet_addr *vaddr,
  552. __be16 vport, __u16 protocol)
  553. {
  554. struct ip_vs_dest *dest;
  555. struct ip_vs_service *svc;
  556. svc = ip_vs_service_get(af, 0, protocol, vaddr, vport);
  557. if (!svc)
  558. return NULL;
  559. dest = ip_vs_lookup_dest(svc, daddr, dport);
  560. if (dest)
  561. atomic_inc(&dest->refcnt);
  562. ip_vs_service_put(svc);
  563. return dest;
  564. }
  565. /*
  566. * Lookup dest by {svc,addr,port} in the destination trash.
  567. * The destination trash is used to hold the destinations that are removed
  568. * from the service table but are still referenced by some conn entries.
  569. * The reason to add the destination trash is when the dest is temporary
  570. * down (either by administrator or by monitor program), the dest can be
  571. * picked back from the trash, the remaining connections to the dest can
  572. * continue, and the counting information of the dest is also useful for
  573. * scheduling.
  574. */
  575. static struct ip_vs_dest *
  576. ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
  577. __be16 dport)
  578. {
  579. struct ip_vs_dest *dest, *nxt;
  580. /*
  581. * Find the destination in trash
  582. */
  583. list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
  584. IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
  585. "dest->refcnt=%d\n",
  586. dest->vfwmark,
  587. IP_VS_DBG_ADDR(svc->af, &dest->addr),
  588. ntohs(dest->port),
  589. atomic_read(&dest->refcnt));
  590. if (dest->af == svc->af &&
  591. ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
  592. dest->port == dport &&
  593. dest->vfwmark == svc->fwmark &&
  594. dest->protocol == svc->protocol &&
  595. (svc->fwmark ||
  596. (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
  597. dest->vport == svc->port))) {
  598. /* HIT */
  599. return dest;
  600. }
  601. /*
  602. * Try to purge the destination from trash if not referenced
  603. */
  604. if (atomic_read(&dest->refcnt) == 1) {
  605. IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
  606. "from trash\n",
  607. dest->vfwmark,
  608. IP_VS_DBG_ADDR(svc->af, &dest->addr),
  609. ntohs(dest->port));
  610. list_del(&dest->n_list);
  611. ip_vs_dst_reset(dest);
  612. __ip_vs_unbind_svc(dest);
  613. kfree(dest);
  614. }
  615. }
  616. return NULL;
  617. }
  618. /*
  619. * Clean up all the destinations in the trash
  620. * Called by the ip_vs_control_cleanup()
  621. *
  622. * When the ip_vs_control_clearup is activated by ipvs module exit,
  623. * the service tables must have been flushed and all the connections
  624. * are expired, and the refcnt of each destination in the trash must
  625. * be 1, so we simply release them here.
  626. */
  627. static void ip_vs_trash_cleanup(void)
  628. {
  629. struct ip_vs_dest *dest, *nxt;
  630. list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
  631. list_del(&dest->n_list);
  632. ip_vs_dst_reset(dest);
  633. __ip_vs_unbind_svc(dest);
  634. kfree(dest);
  635. }
  636. }
  637. static void
  638. ip_vs_zero_stats(struct ip_vs_stats *stats)
  639. {
  640. spin_lock_bh(&stats->lock);
  641. memset(&stats->ustats, 0, sizeof(stats->ustats));
  642. ip_vs_zero_estimator(stats);
  643. spin_unlock_bh(&stats->lock);
  644. }
  645. /*
  646. * Update a destination in the given service
  647. */
  648. static void
  649. __ip_vs_update_dest(struct ip_vs_service *svc,
  650. struct ip_vs_dest *dest, struct ip_vs_dest_user_kern *udest)
  651. {
  652. int conn_flags;
  653. /* set the weight and the flags */
  654. atomic_set(&dest->weight, udest->weight);
  655. conn_flags = udest->conn_flags | IP_VS_CONN_F_INACTIVE;
  656. /* check if local node and update the flags */
  657. #ifdef CONFIG_IP_VS_IPV6
  658. if (svc->af == AF_INET6) {
  659. if (__ip_vs_addr_is_local_v6(&udest->addr.in6)) {
  660. conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
  661. | IP_VS_CONN_F_LOCALNODE;
  662. }
  663. } else
  664. #endif
  665. if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
  666. conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
  667. | IP_VS_CONN_F_LOCALNODE;
  668. }
  669. /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
  670. if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
  671. conn_flags |= IP_VS_CONN_F_NOOUTPUT;
  672. } else {
  673. /*
  674. * Put the real service in ip_vs_rtable if not present.
  675. * For now only for NAT!
  676. */
  677. write_lock_bh(&__ip_vs_rs_lock);
  678. ip_vs_rs_hash(dest);
  679. write_unlock_bh(&__ip_vs_rs_lock);
  680. }
  681. atomic_set(&dest->conn_flags, conn_flags);
  682. /* bind the service */
  683. if (!dest->svc) {
  684. __ip_vs_bind_svc(dest, svc);
  685. } else {
  686. if (dest->svc != svc) {
  687. __ip_vs_unbind_svc(dest);
  688. ip_vs_zero_stats(&dest->stats);
  689. __ip_vs_bind_svc(dest, svc);
  690. }
  691. }
  692. /* set the dest status flags */
  693. dest->flags |= IP_VS_DEST_F_AVAILABLE;
  694. if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
  695. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  696. dest->u_threshold = udest->u_threshold;
  697. dest->l_threshold = udest->l_threshold;
  698. }
  699. /*
  700. * Create a destination for the given service
  701. */
  702. static int
  703. ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
  704. struct ip_vs_dest **dest_p)
  705. {
  706. struct ip_vs_dest *dest;
  707. unsigned atype;
  708. EnterFunction(2);
  709. #ifdef CONFIG_IP_VS_IPV6
  710. if (svc->af == AF_INET6) {
  711. atype = ipv6_addr_type(&udest->addr.in6);
  712. if ((!(atype & IPV6_ADDR_UNICAST) ||
  713. atype & IPV6_ADDR_LINKLOCAL) &&
  714. !__ip_vs_addr_is_local_v6(&udest->addr.in6))
  715. return -EINVAL;
  716. } else
  717. #endif
  718. {
  719. atype = inet_addr_type(&init_net, udest->addr.ip);
  720. if (atype != RTN_LOCAL && atype != RTN_UNICAST)
  721. return -EINVAL;
  722. }
  723. dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
  724. if (dest == NULL) {
  725. IP_VS_ERR("ip_vs_new_dest: kmalloc failed.\n");
  726. return -ENOMEM;
  727. }
  728. dest->af = svc->af;
  729. dest->protocol = svc->protocol;
  730. dest->vaddr = svc->addr;
  731. dest->vport = svc->port;
  732. dest->vfwmark = svc->fwmark;
  733. ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
  734. dest->port = udest->port;
  735. atomic_set(&dest->activeconns, 0);
  736. atomic_set(&dest->inactconns, 0);
  737. atomic_set(&dest->persistconns, 0);
  738. atomic_set(&dest->refcnt, 0);
  739. INIT_LIST_HEAD(&dest->d_list);
  740. spin_lock_init(&dest->dst_lock);
  741. spin_lock_init(&dest->stats.lock);
  742. __ip_vs_update_dest(svc, dest, udest);
  743. ip_vs_new_estimator(&dest->stats);
  744. *dest_p = dest;
  745. LeaveFunction(2);
  746. return 0;
  747. }
  748. /*
  749. * Add a destination into an existing service
  750. */
  751. static int
  752. ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  753. {
  754. struct ip_vs_dest *dest;
  755. union nf_inet_addr daddr;
  756. __be16 dport = udest->port;
  757. int ret;
  758. EnterFunction(2);
  759. if (udest->weight < 0) {
  760. IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
  761. return -ERANGE;
  762. }
  763. if (udest->l_threshold > udest->u_threshold) {
  764. IP_VS_ERR("ip_vs_add_dest(): lower threshold is higher than "
  765. "upper threshold\n");
  766. return -ERANGE;
  767. }
  768. ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
  769. /*
  770. * Check if the dest already exists in the list
  771. */
  772. dest = ip_vs_lookup_dest(svc, &daddr, dport);
  773. if (dest != NULL) {
  774. IP_VS_DBG(1, "ip_vs_add_dest(): dest already exists\n");
  775. return -EEXIST;
  776. }
  777. /*
  778. * Check if the dest already exists in the trash and
  779. * is from the same service
  780. */
  781. dest = ip_vs_trash_get_dest(svc, &daddr, dport);
  782. if (dest != NULL) {
  783. IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
  784. "dest->refcnt=%d, service %u/%s:%u\n",
  785. IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
  786. atomic_read(&dest->refcnt),
  787. dest->vfwmark,
  788. IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
  789. ntohs(dest->vport));
  790. __ip_vs_update_dest(svc, dest, udest);
  791. /*
  792. * Get the destination from the trash
  793. */
  794. list_del(&dest->n_list);
  795. ip_vs_new_estimator(&dest->stats);
  796. write_lock_bh(&__ip_vs_svc_lock);
  797. /*
  798. * Wait until all other svc users go away.
  799. */
  800. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  801. list_add(&dest->n_list, &svc->destinations);
  802. svc->num_dests++;
  803. /* call the update_service function of its scheduler */
  804. if (svc->scheduler->update_service)
  805. svc->scheduler->update_service(svc);
  806. write_unlock_bh(&__ip_vs_svc_lock);
  807. return 0;
  808. }
  809. /*
  810. * Allocate and initialize the dest structure
  811. */
  812. ret = ip_vs_new_dest(svc, udest, &dest);
  813. if (ret) {
  814. return ret;
  815. }
  816. /*
  817. * Add the dest entry into the list
  818. */
  819. atomic_inc(&dest->refcnt);
  820. write_lock_bh(&__ip_vs_svc_lock);
  821. /*
  822. * Wait until all other svc users go away.
  823. */
  824. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  825. list_add(&dest->n_list, &svc->destinations);
  826. svc->num_dests++;
  827. /* call the update_service function of its scheduler */
  828. if (svc->scheduler->update_service)
  829. svc->scheduler->update_service(svc);
  830. write_unlock_bh(&__ip_vs_svc_lock);
  831. LeaveFunction(2);
  832. return 0;
  833. }
  834. /*
  835. * Edit a destination in the given service
  836. */
  837. static int
  838. ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  839. {
  840. struct ip_vs_dest *dest;
  841. union nf_inet_addr daddr;
  842. __be16 dport = udest->port;
  843. EnterFunction(2);
  844. if (udest->weight < 0) {
  845. IP_VS_ERR("ip_vs_edit_dest(): server weight less than zero\n");
  846. return -ERANGE;
  847. }
  848. if (udest->l_threshold > udest->u_threshold) {
  849. IP_VS_ERR("ip_vs_edit_dest(): lower threshold is higher than "
  850. "upper threshold\n");
  851. return -ERANGE;
  852. }
  853. ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
  854. /*
  855. * Lookup the destination list
  856. */
  857. dest = ip_vs_lookup_dest(svc, &daddr, dport);
  858. if (dest == NULL) {
  859. IP_VS_DBG(1, "ip_vs_edit_dest(): dest doesn't exist\n");
  860. return -ENOENT;
  861. }
  862. __ip_vs_update_dest(svc, dest, udest);
  863. write_lock_bh(&__ip_vs_svc_lock);
  864. /* Wait until all other svc users go away */
  865. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  866. /* call the update_service, because server weight may be changed */
  867. if (svc->scheduler->update_service)
  868. svc->scheduler->update_service(svc);
  869. write_unlock_bh(&__ip_vs_svc_lock);
  870. LeaveFunction(2);
  871. return 0;
  872. }
  873. /*
  874. * Delete a destination (must be already unlinked from the service)
  875. */
  876. static void __ip_vs_del_dest(struct ip_vs_dest *dest)
  877. {
  878. ip_vs_kill_estimator(&dest->stats);
  879. /*
  880. * Remove it from the d-linked list with the real services.
  881. */
  882. write_lock_bh(&__ip_vs_rs_lock);
  883. ip_vs_rs_unhash(dest);
  884. write_unlock_bh(&__ip_vs_rs_lock);
  885. /*
  886. * Decrease the refcnt of the dest, and free the dest
  887. * if nobody refers to it (refcnt=0). Otherwise, throw
  888. * the destination into the trash.
  889. */
  890. if (atomic_dec_and_test(&dest->refcnt)) {
  891. ip_vs_dst_reset(dest);
  892. /* simply decrease svc->refcnt here, let the caller check
  893. and release the service if nobody refers to it.
  894. Only user context can release destination and service,
  895. and only one user context can update virtual service at a
  896. time, so the operation here is OK */
  897. atomic_dec(&dest->svc->refcnt);
  898. kfree(dest);
  899. } else {
  900. IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
  901. "dest->refcnt=%d\n",
  902. IP_VS_DBG_ADDR(dest->af, &dest->addr),
  903. ntohs(dest->port),
  904. atomic_read(&dest->refcnt));
  905. list_add(&dest->n_list, &ip_vs_dest_trash);
  906. atomic_inc(&dest->refcnt);
  907. }
  908. }
  909. /*
  910. * Unlink a destination from the given service
  911. */
  912. static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
  913. struct ip_vs_dest *dest,
  914. int svcupd)
  915. {
  916. dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
  917. /*
  918. * Remove it from the d-linked destination list.
  919. */
  920. list_del(&dest->n_list);
  921. svc->num_dests--;
  922. /*
  923. * Call the update_service function of its scheduler
  924. */
  925. if (svcupd && svc->scheduler->update_service)
  926. svc->scheduler->update_service(svc);
  927. }
  928. /*
  929. * Delete a destination server in the given service
  930. */
  931. static int
  932. ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  933. {
  934. struct ip_vs_dest *dest;
  935. __be16 dport = udest->port;
  936. EnterFunction(2);
  937. dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
  938. if (dest == NULL) {
  939. IP_VS_DBG(1, "ip_vs_del_dest(): destination not found!\n");
  940. return -ENOENT;
  941. }
  942. write_lock_bh(&__ip_vs_svc_lock);
  943. /*
  944. * Wait until all other svc users go away.
  945. */
  946. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  947. /*
  948. * Unlink dest from the service
  949. */
  950. __ip_vs_unlink_dest(svc, dest, 1);
  951. write_unlock_bh(&__ip_vs_svc_lock);
  952. /*
  953. * Delete the destination
  954. */
  955. __ip_vs_del_dest(dest);
  956. LeaveFunction(2);
  957. return 0;
  958. }
  959. /*
  960. * Add a service into the service hash table
  961. */
  962. static int
  963. ip_vs_add_service(struct ip_vs_service_user_kern *u,
  964. struct ip_vs_service **svc_p)
  965. {
  966. int ret = 0;
  967. struct ip_vs_scheduler *sched = NULL;
  968. struct ip_vs_service *svc = NULL;
  969. /* increase the module use count */
  970. ip_vs_use_count_inc();
  971. /* Lookup the scheduler by 'u->sched_name' */
  972. sched = ip_vs_scheduler_get(u->sched_name);
  973. if (sched == NULL) {
  974. IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
  975. u->sched_name);
  976. ret = -ENOENT;
  977. goto out_mod_dec;
  978. }
  979. #ifdef CONFIG_IP_VS_IPV6
  980. if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
  981. ret = -EINVAL;
  982. goto out_err;
  983. }
  984. #endif
  985. svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
  986. if (svc == NULL) {
  987. IP_VS_DBG(1, "ip_vs_add_service: kmalloc failed.\n");
  988. ret = -ENOMEM;
  989. goto out_err;
  990. }
  991. /* I'm the first user of the service */
  992. atomic_set(&svc->usecnt, 1);
  993. atomic_set(&svc->refcnt, 0);
  994. svc->af = u->af;
  995. svc->protocol = u->protocol;
  996. ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
  997. svc->port = u->port;
  998. svc->fwmark = u->fwmark;
  999. svc->flags = u->flags;
  1000. svc->timeout = u->timeout * HZ;
  1001. svc->netmask = u->netmask;
  1002. INIT_LIST_HEAD(&svc->destinations);
  1003. rwlock_init(&svc->sched_lock);
  1004. spin_lock_init(&svc->stats.lock);
  1005. /* Bind the scheduler */
  1006. ret = ip_vs_bind_scheduler(svc, sched);
  1007. if (ret)
  1008. goto out_err;
  1009. sched = NULL;
  1010. /* Update the virtual service counters */
  1011. if (svc->port == FTPPORT)
  1012. atomic_inc(&ip_vs_ftpsvc_counter);
  1013. else if (svc->port == 0)
  1014. atomic_inc(&ip_vs_nullsvc_counter);
  1015. ip_vs_new_estimator(&svc->stats);
  1016. /* Count only IPv4 services for old get/setsockopt interface */
  1017. if (svc->af == AF_INET)
  1018. ip_vs_num_services++;
  1019. /* Hash the service into the service table */
  1020. write_lock_bh(&__ip_vs_svc_lock);
  1021. ip_vs_svc_hash(svc);
  1022. write_unlock_bh(&__ip_vs_svc_lock);
  1023. *svc_p = svc;
  1024. return 0;
  1025. out_err:
  1026. if (svc != NULL) {
  1027. if (svc->scheduler)
  1028. ip_vs_unbind_scheduler(svc);
  1029. if (svc->inc) {
  1030. local_bh_disable();
  1031. ip_vs_app_inc_put(svc->inc);
  1032. local_bh_enable();
  1033. }
  1034. kfree(svc);
  1035. }
  1036. ip_vs_scheduler_put(sched);
  1037. out_mod_dec:
  1038. /* decrease the module use count */
  1039. ip_vs_use_count_dec();
  1040. return ret;
  1041. }
  1042. /*
  1043. * Edit a service and bind it with a new scheduler
  1044. */
  1045. static int
  1046. ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
  1047. {
  1048. struct ip_vs_scheduler *sched, *old_sched;
  1049. int ret = 0;
  1050. /*
  1051. * Lookup the scheduler, by 'u->sched_name'
  1052. */
  1053. sched = ip_vs_scheduler_get(u->sched_name);
  1054. if (sched == NULL) {
  1055. IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
  1056. u->sched_name);
  1057. return -ENOENT;
  1058. }
  1059. old_sched = sched;
  1060. #ifdef CONFIG_IP_VS_IPV6
  1061. if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
  1062. ret = -EINVAL;
  1063. goto out;
  1064. }
  1065. #endif
  1066. write_lock_bh(&__ip_vs_svc_lock);
  1067. /*
  1068. * Wait until all other svc users go away.
  1069. */
  1070. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  1071. /*
  1072. * Set the flags and timeout value
  1073. */
  1074. svc->flags = u->flags | IP_VS_SVC_F_HASHED;
  1075. svc->timeout = u->timeout * HZ;
  1076. svc->netmask = u->netmask;
  1077. old_sched = svc->scheduler;
  1078. if (sched != old_sched) {
  1079. /*
  1080. * Unbind the old scheduler
  1081. */
  1082. if ((ret = ip_vs_unbind_scheduler(svc))) {
  1083. old_sched = sched;
  1084. goto out_unlock;
  1085. }
  1086. /*
  1087. * Bind the new scheduler
  1088. */
  1089. if ((ret = ip_vs_bind_scheduler(svc, sched))) {
  1090. /*
  1091. * If ip_vs_bind_scheduler fails, restore the old
  1092. * scheduler.
  1093. * The main reason of failure is out of memory.
  1094. *
  1095. * The question is if the old scheduler can be
  1096. * restored all the time. TODO: if it cannot be
  1097. * restored some time, we must delete the service,
  1098. * otherwise the system may crash.
  1099. */
  1100. ip_vs_bind_scheduler(svc, old_sched);
  1101. old_sched = sched;
  1102. goto out_unlock;
  1103. }
  1104. }
  1105. out_unlock:
  1106. write_unlock_bh(&__ip_vs_svc_lock);
  1107. #ifdef CONFIG_IP_VS_IPV6
  1108. out:
  1109. #endif
  1110. if (old_sched)
  1111. ip_vs_scheduler_put(old_sched);
  1112. return ret;
  1113. }
  1114. /*
  1115. * Delete a service from the service list
  1116. * - The service must be unlinked, unlocked and not referenced!
  1117. * - We are called under _bh lock
  1118. */
  1119. static void __ip_vs_del_service(struct ip_vs_service *svc)
  1120. {
  1121. struct ip_vs_dest *dest, *nxt;
  1122. struct ip_vs_scheduler *old_sched;
  1123. /* Count only IPv4 services for old get/setsockopt interface */
  1124. if (svc->af == AF_INET)
  1125. ip_vs_num_services--;
  1126. ip_vs_kill_estimator(&svc->stats);
  1127. /* Unbind scheduler */
  1128. old_sched = svc->scheduler;
  1129. ip_vs_unbind_scheduler(svc);
  1130. if (old_sched)
  1131. ip_vs_scheduler_put(old_sched);
  1132. /* Unbind app inc */
  1133. if (svc->inc) {
  1134. ip_vs_app_inc_put(svc->inc);
  1135. svc->inc = NULL;
  1136. }
  1137. /*
  1138. * Unlink the whole destination list
  1139. */
  1140. list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
  1141. __ip_vs_unlink_dest(svc, dest, 0);
  1142. __ip_vs_del_dest(dest);
  1143. }
  1144. /*
  1145. * Update the virtual service counters
  1146. */
  1147. if (svc->port == FTPPORT)
  1148. atomic_dec(&ip_vs_ftpsvc_counter);
  1149. else if (svc->port == 0)
  1150. atomic_dec(&ip_vs_nullsvc_counter);
  1151. /*
  1152. * Free the service if nobody refers to it
  1153. */
  1154. if (atomic_read(&svc->refcnt) == 0)
  1155. kfree(svc);
  1156. /* decrease the module use count */
  1157. ip_vs_use_count_dec();
  1158. }
  1159. /*
  1160. * Delete a service from the service list
  1161. */
  1162. static int ip_vs_del_service(struct ip_vs_service *svc)
  1163. {
  1164. if (svc == NULL)
  1165. return -EEXIST;
  1166. /*
  1167. * Unhash it from the service table
  1168. */
  1169. write_lock_bh(&__ip_vs_svc_lock);
  1170. ip_vs_svc_unhash(svc);
  1171. /*
  1172. * Wait until all the svc users go away.
  1173. */
  1174. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  1175. __ip_vs_del_service(svc);
  1176. write_unlock_bh(&__ip_vs_svc_lock);
  1177. return 0;
  1178. }
  1179. /*
  1180. * Flush all the virtual services
  1181. */
  1182. static int ip_vs_flush(void)
  1183. {
  1184. int idx;
  1185. struct ip_vs_service *svc, *nxt;
  1186. /*
  1187. * Flush the service table hashed by <protocol,addr,port>
  1188. */
  1189. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1190. list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
  1191. write_lock_bh(&__ip_vs_svc_lock);
  1192. ip_vs_svc_unhash(svc);
  1193. /*
  1194. * Wait until all the svc users go away.
  1195. */
  1196. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
  1197. __ip_vs_del_service(svc);
  1198. write_unlock_bh(&__ip_vs_svc_lock);
  1199. }
  1200. }
  1201. /*
  1202. * Flush the service table hashed by fwmark
  1203. */
  1204. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1205. list_for_each_entry_safe(svc, nxt,
  1206. &ip_vs_svc_fwm_table[idx], f_list) {
  1207. write_lock_bh(&__ip_vs_svc_lock);
  1208. ip_vs_svc_unhash(svc);
  1209. /*
  1210. * Wait until all the svc users go away.
  1211. */
  1212. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
  1213. __ip_vs_del_service(svc);
  1214. write_unlock_bh(&__ip_vs_svc_lock);
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. /*
  1220. * Zero counters in a service or all services
  1221. */
  1222. static int ip_vs_zero_service(struct ip_vs_service *svc)
  1223. {
  1224. struct ip_vs_dest *dest;
  1225. write_lock_bh(&__ip_vs_svc_lock);
  1226. list_for_each_entry(dest, &svc->destinations, n_list) {
  1227. ip_vs_zero_stats(&dest->stats);
  1228. }
  1229. ip_vs_zero_stats(&svc->stats);
  1230. write_unlock_bh(&__ip_vs_svc_lock);
  1231. return 0;
  1232. }
  1233. static int ip_vs_zero_all(void)
  1234. {
  1235. int idx;
  1236. struct ip_vs_service *svc;
  1237. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1238. list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
  1239. ip_vs_zero_service(svc);
  1240. }
  1241. }
  1242. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1243. list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
  1244. ip_vs_zero_service(svc);
  1245. }
  1246. }
  1247. ip_vs_zero_stats(&ip_vs_stats);
  1248. return 0;
  1249. }
  1250. static int
  1251. proc_do_defense_mode(ctl_table *table, int write, struct file * filp,
  1252. void __user *buffer, size_t *lenp, loff_t *ppos)
  1253. {
  1254. int *valp = table->data;
  1255. int val = *valp;
  1256. int rc;
  1257. rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  1258. if (write && (*valp != val)) {
  1259. if ((*valp < 0) || (*valp > 3)) {
  1260. /* Restore the correct value */
  1261. *valp = val;
  1262. } else {
  1263. update_defense_level();
  1264. }
  1265. }
  1266. return rc;
  1267. }
  1268. static int
  1269. proc_do_sync_threshold(ctl_table *table, int write, struct file *filp,
  1270. void __user *buffer, size_t *lenp, loff_t *ppos)
  1271. {
  1272. int *valp = table->data;
  1273. int val[2];
  1274. int rc;
  1275. /* backup the value first */
  1276. memcpy(val, valp, sizeof(val));
  1277. rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  1278. if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
  1279. /* Restore the correct value */
  1280. memcpy(valp, val, sizeof(val));
  1281. }
  1282. return rc;
  1283. }
  1284. /*
  1285. * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
  1286. */
  1287. static struct ctl_table vs_vars[] = {
  1288. {
  1289. .procname = "amemthresh",
  1290. .data = &sysctl_ip_vs_amemthresh,
  1291. .maxlen = sizeof(int),
  1292. .mode = 0644,
  1293. .proc_handler = proc_dointvec,
  1294. },
  1295. #ifdef CONFIG_IP_VS_DEBUG
  1296. {
  1297. .procname = "debug_level",
  1298. .data = &sysctl_ip_vs_debug_level,
  1299. .maxlen = sizeof(int),
  1300. .mode = 0644,
  1301. .proc_handler = proc_dointvec,
  1302. },
  1303. #endif
  1304. {
  1305. .procname = "am_droprate",
  1306. .data = &sysctl_ip_vs_am_droprate,
  1307. .maxlen = sizeof(int),
  1308. .mode = 0644,
  1309. .proc_handler = proc_dointvec,
  1310. },
  1311. {
  1312. .procname = "drop_entry",
  1313. .data = &sysctl_ip_vs_drop_entry,
  1314. .maxlen = sizeof(int),
  1315. .mode = 0644,
  1316. .proc_handler = proc_do_defense_mode,
  1317. },
  1318. {
  1319. .procname = "drop_packet",
  1320. .data = &sysctl_ip_vs_drop_packet,
  1321. .maxlen = sizeof(int),
  1322. .mode = 0644,
  1323. .proc_handler = proc_do_defense_mode,
  1324. },
  1325. {
  1326. .procname = "secure_tcp",
  1327. .data = &sysctl_ip_vs_secure_tcp,
  1328. .maxlen = sizeof(int),
  1329. .mode = 0644,
  1330. .proc_handler = proc_do_defense_mode,
  1331. },
  1332. #if 0
  1333. {
  1334. .procname = "timeout_established",
  1335. .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
  1336. .maxlen = sizeof(int),
  1337. .mode = 0644,
  1338. .proc_handler = proc_dointvec_jiffies,
  1339. },
  1340. {
  1341. .procname = "timeout_synsent",
  1342. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
  1343. .maxlen = sizeof(int),
  1344. .mode = 0644,
  1345. .proc_handler = proc_dointvec_jiffies,
  1346. },
  1347. {
  1348. .procname = "timeout_synrecv",
  1349. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
  1350. .maxlen = sizeof(int),
  1351. .mode = 0644,
  1352. .proc_handler = proc_dointvec_jiffies,
  1353. },
  1354. {
  1355. .procname = "timeout_finwait",
  1356. .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
  1357. .maxlen = sizeof(int),
  1358. .mode = 0644,
  1359. .proc_handler = proc_dointvec_jiffies,
  1360. },
  1361. {
  1362. .procname = "timeout_timewait",
  1363. .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
  1364. .maxlen = sizeof(int),
  1365. .mode = 0644,
  1366. .proc_handler = proc_dointvec_jiffies,
  1367. },
  1368. {
  1369. .procname = "timeout_close",
  1370. .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
  1371. .maxlen = sizeof(int),
  1372. .mode = 0644,
  1373. .proc_handler = proc_dointvec_jiffies,
  1374. },
  1375. {
  1376. .procname = "timeout_closewait",
  1377. .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
  1378. .maxlen = sizeof(int),
  1379. .mode = 0644,
  1380. .proc_handler = proc_dointvec_jiffies,
  1381. },
  1382. {
  1383. .procname = "timeout_lastack",
  1384. .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
  1385. .maxlen = sizeof(int),
  1386. .mode = 0644,
  1387. .proc_handler = proc_dointvec_jiffies,
  1388. },
  1389. {
  1390. .procname = "timeout_listen",
  1391. .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
  1392. .maxlen = sizeof(int),
  1393. .mode = 0644,
  1394. .proc_handler = proc_dointvec_jiffies,
  1395. },
  1396. {
  1397. .procname = "timeout_synack",
  1398. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
  1399. .maxlen = sizeof(int),
  1400. .mode = 0644,
  1401. .proc_handler = proc_dointvec_jiffies,
  1402. },
  1403. {
  1404. .procname = "timeout_udp",
  1405. .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
  1406. .maxlen = sizeof(int),
  1407. .mode = 0644,
  1408. .proc_handler = proc_dointvec_jiffies,
  1409. },
  1410. {
  1411. .procname = "timeout_icmp",
  1412. .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
  1413. .maxlen = sizeof(int),
  1414. .mode = 0644,
  1415. .proc_handler = proc_dointvec_jiffies,
  1416. },
  1417. #endif
  1418. {
  1419. .procname = "cache_bypass",
  1420. .data = &sysctl_ip_vs_cache_bypass,
  1421. .maxlen = sizeof(int),
  1422. .mode = 0644,
  1423. .proc_handler = proc_dointvec,
  1424. },
  1425. {
  1426. .procname = "expire_nodest_conn",
  1427. .data = &sysctl_ip_vs_expire_nodest_conn,
  1428. .maxlen = sizeof(int),
  1429. .mode = 0644,
  1430. .proc_handler = proc_dointvec,
  1431. },
  1432. {
  1433. .procname = "expire_quiescent_template",
  1434. .data = &sysctl_ip_vs_expire_quiescent_template,
  1435. .maxlen = sizeof(int),
  1436. .mode = 0644,
  1437. .proc_handler = proc_dointvec,
  1438. },
  1439. {
  1440. .procname = "sync_threshold",
  1441. .data = &sysctl_ip_vs_sync_threshold,
  1442. .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
  1443. .mode = 0644,
  1444. .proc_handler = proc_do_sync_threshold,
  1445. },
  1446. {
  1447. .procname = "nat_icmp_send",
  1448. .data = &sysctl_ip_vs_nat_icmp_send,
  1449. .maxlen = sizeof(int),
  1450. .mode = 0644,
  1451. .proc_handler = proc_dointvec,
  1452. },
  1453. { .ctl_name = 0 }
  1454. };
  1455. const struct ctl_path net_vs_ctl_path[] = {
  1456. { .procname = "net", .ctl_name = CTL_NET, },
  1457. { .procname = "ipv4", .ctl_name = NET_IPV4, },
  1458. { .procname = "vs", },
  1459. { }
  1460. };
  1461. EXPORT_SYMBOL_GPL(net_vs_ctl_path);
  1462. static struct ctl_table_header * sysctl_header;
  1463. #ifdef CONFIG_PROC_FS
  1464. struct ip_vs_iter {
  1465. struct list_head *table;
  1466. int bucket;
  1467. };
  1468. /*
  1469. * Write the contents of the VS rule table to a PROCfs file.
  1470. * (It is kept just for backward compatibility)
  1471. */
  1472. static inline const char *ip_vs_fwd_name(unsigned flags)
  1473. {
  1474. switch (flags & IP_VS_CONN_F_FWD_MASK) {
  1475. case IP_VS_CONN_F_LOCALNODE:
  1476. return "Local";
  1477. case IP_VS_CONN_F_TUNNEL:
  1478. return "Tunnel";
  1479. case IP_VS_CONN_F_DROUTE:
  1480. return "Route";
  1481. default:
  1482. return "Masq";
  1483. }
  1484. }
  1485. /* Get the Nth entry in the two lists */
  1486. static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
  1487. {
  1488. struct ip_vs_iter *iter = seq->private;
  1489. int idx;
  1490. struct ip_vs_service *svc;
  1491. /* look in hash by protocol */
  1492. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1493. list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
  1494. if (pos-- == 0){
  1495. iter->table = ip_vs_svc_table;
  1496. iter->bucket = idx;
  1497. return svc;
  1498. }
  1499. }
  1500. }
  1501. /* keep looking in fwmark */
  1502. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1503. list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
  1504. if (pos-- == 0) {
  1505. iter->table = ip_vs_svc_fwm_table;
  1506. iter->bucket = idx;
  1507. return svc;
  1508. }
  1509. }
  1510. }
  1511. return NULL;
  1512. }
  1513. static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
  1514. __acquires(__ip_vs_svc_lock)
  1515. {
  1516. read_lock_bh(&__ip_vs_svc_lock);
  1517. return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
  1518. }
  1519. static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1520. {
  1521. struct list_head *e;
  1522. struct ip_vs_iter *iter;
  1523. struct ip_vs_service *svc;
  1524. ++*pos;
  1525. if (v == SEQ_START_TOKEN)
  1526. return ip_vs_info_array(seq,0);
  1527. svc = v;
  1528. iter = seq->private;
  1529. if (iter->table == ip_vs_svc_table) {
  1530. /* next service in table hashed by protocol */
  1531. if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
  1532. return list_entry(e, struct ip_vs_service, s_list);
  1533. while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
  1534. list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
  1535. s_list) {
  1536. return svc;
  1537. }
  1538. }
  1539. iter->table = ip_vs_svc_fwm_table;
  1540. iter->bucket = -1;
  1541. goto scan_fwmark;
  1542. }
  1543. /* next service in hashed by fwmark */
  1544. if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
  1545. return list_entry(e, struct ip_vs_service, f_list);
  1546. scan_fwmark:
  1547. while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
  1548. list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
  1549. f_list)
  1550. return svc;
  1551. }
  1552. return NULL;
  1553. }
  1554. static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
  1555. __releases(__ip_vs_svc_lock)
  1556. {
  1557. read_unlock_bh(&__ip_vs_svc_lock);
  1558. }
  1559. static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
  1560. {
  1561. if (v == SEQ_START_TOKEN) {
  1562. seq_printf(seq,
  1563. "IP Virtual Server version %d.%d.%d (size=%d)\n",
  1564. NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
  1565. seq_puts(seq,
  1566. "Prot LocalAddress:Port Scheduler Flags\n");
  1567. seq_puts(seq,
  1568. " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
  1569. } else {
  1570. const struct ip_vs_service *svc = v;
  1571. const struct ip_vs_iter *iter = seq->private;
  1572. const struct ip_vs_dest *dest;
  1573. if (iter->table == ip_vs_svc_table) {
  1574. #ifdef CONFIG_IP_VS_IPV6
  1575. if (svc->af == AF_INET6)
  1576. seq_printf(seq, "%s [%pI6]:%04X %s ",
  1577. ip_vs_proto_name(svc->protocol),
  1578. &svc->addr.in6,
  1579. ntohs(svc->port),
  1580. svc->scheduler->name);
  1581. else
  1582. #endif
  1583. seq_printf(seq, "%s %08X:%04X %s ",
  1584. ip_vs_proto_name(svc->protocol),
  1585. ntohl(svc->addr.ip),
  1586. ntohs(svc->port),
  1587. svc->scheduler->name);
  1588. } else {
  1589. seq_printf(seq, "FWM %08X %s ",
  1590. svc->fwmark, svc->scheduler->name);
  1591. }
  1592. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  1593. seq_printf(seq, "persistent %d %08X\n",
  1594. svc->timeout,
  1595. ntohl(svc->netmask));
  1596. else
  1597. seq_putc(seq, '\n');
  1598. list_for_each_entry(dest, &svc->destinations, n_list) {
  1599. #ifdef CONFIG_IP_VS_IPV6
  1600. if (dest->af == AF_INET6)
  1601. seq_printf(seq,
  1602. " -> [%pI6]:%04X"
  1603. " %-7s %-6d %-10d %-10d\n",
  1604. &dest->addr.in6,
  1605. ntohs(dest->port),
  1606. ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
  1607. atomic_read(&dest->weight),
  1608. atomic_read(&dest->activeconns),
  1609. atomic_read(&dest->inactconns));
  1610. else
  1611. #endif
  1612. seq_printf(seq,
  1613. " -> %08X:%04X "
  1614. "%-7s %-6d %-10d %-10d\n",
  1615. ntohl(dest->addr.ip),
  1616. ntohs(dest->port),
  1617. ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
  1618. atomic_read(&dest->weight),
  1619. atomic_read(&dest->activeconns),
  1620. atomic_read(&dest->inactconns));
  1621. }
  1622. }
  1623. return 0;
  1624. }
  1625. static const struct seq_operations ip_vs_info_seq_ops = {
  1626. .start = ip_vs_info_seq_start,
  1627. .next = ip_vs_info_seq_next,
  1628. .stop = ip_vs_info_seq_stop,
  1629. .show = ip_vs_info_seq_show,
  1630. };
  1631. static int ip_vs_info_open(struct inode *inode, struct file *file)
  1632. {
  1633. return seq_open_private(file, &ip_vs_info_seq_ops,
  1634. sizeof(struct ip_vs_iter));
  1635. }
  1636. static const struct file_operations ip_vs_info_fops = {
  1637. .owner = THIS_MODULE,
  1638. .open = ip_vs_info_open,
  1639. .read = seq_read,
  1640. .llseek = seq_lseek,
  1641. .release = seq_release_private,
  1642. };
  1643. #endif
  1644. struct ip_vs_stats ip_vs_stats = {
  1645. .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
  1646. };
  1647. #ifdef CONFIG_PROC_FS
  1648. static int ip_vs_stats_show(struct seq_file *seq, void *v)
  1649. {
  1650. /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
  1651. seq_puts(seq,
  1652. " Total Incoming Outgoing Incoming Outgoing\n");
  1653. seq_printf(seq,
  1654. " Conns Packets Packets Bytes Bytes\n");
  1655. spin_lock_bh(&ip_vs_stats.lock);
  1656. seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
  1657. ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
  1658. (unsigned long long) ip_vs_stats.ustats.inbytes,
  1659. (unsigned long long) ip_vs_stats.ustats.outbytes);
  1660. /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
  1661. seq_puts(seq,
  1662. " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
  1663. seq_printf(seq,"%8X %8X %8X %16X %16X\n",
  1664. ip_vs_stats.ustats.cps,
  1665. ip_vs_stats.ustats.inpps,
  1666. ip_vs_stats.ustats.outpps,
  1667. ip_vs_stats.ustats.inbps,
  1668. ip_vs_stats.ustats.outbps);
  1669. spin_unlock_bh(&ip_vs_stats.lock);
  1670. return 0;
  1671. }
  1672. static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
  1673. {
  1674. return single_open(file, ip_vs_stats_show, NULL);
  1675. }
  1676. static const struct file_operations ip_vs_stats_fops = {
  1677. .owner = THIS_MODULE,
  1678. .open = ip_vs_stats_seq_open,
  1679. .read = seq_read,
  1680. .llseek = seq_lseek,
  1681. .release = single_release,
  1682. };
  1683. #endif
  1684. /*
  1685. * Set timeout values for tcp tcpfin udp in the timeout_table.
  1686. */
  1687. static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
  1688. {
  1689. IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
  1690. u->tcp_timeout,
  1691. u->tcp_fin_timeout,
  1692. u->udp_timeout);
  1693. #ifdef CONFIG_IP_VS_PROTO_TCP
  1694. if (u->tcp_timeout) {
  1695. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
  1696. = u->tcp_timeout * HZ;
  1697. }
  1698. if (u->tcp_fin_timeout) {
  1699. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
  1700. = u->tcp_fin_timeout * HZ;
  1701. }
  1702. #endif
  1703. #ifdef CONFIG_IP_VS_PROTO_UDP
  1704. if (u->udp_timeout) {
  1705. ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
  1706. = u->udp_timeout * HZ;
  1707. }
  1708. #endif
  1709. return 0;
  1710. }
  1711. #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
  1712. #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
  1713. #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
  1714. sizeof(struct ip_vs_dest_user))
  1715. #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
  1716. #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
  1717. #define MAX_ARG_LEN SVCDEST_ARG_LEN
  1718. static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
  1719. [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
  1720. [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
  1721. [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
  1722. [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
  1723. [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
  1724. [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
  1725. [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
  1726. [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
  1727. [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
  1728. [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
  1729. [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
  1730. };
  1731. static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
  1732. struct ip_vs_service_user *usvc_compat)
  1733. {
  1734. usvc->af = AF_INET;
  1735. usvc->protocol = usvc_compat->protocol;
  1736. usvc->addr.ip = usvc_compat->addr;
  1737. usvc->port = usvc_compat->port;
  1738. usvc->fwmark = usvc_compat->fwmark;
  1739. /* Deep copy of sched_name is not needed here */
  1740. usvc->sched_name = usvc_compat->sched_name;
  1741. usvc->flags = usvc_compat->flags;
  1742. usvc->timeout = usvc_compat->timeout;
  1743. usvc->netmask = usvc_compat->netmask;
  1744. }
  1745. static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
  1746. struct ip_vs_dest_user *udest_compat)
  1747. {
  1748. udest->addr.ip = udest_compat->addr;
  1749. udest->port = udest_compat->port;
  1750. udest->conn_flags = udest_compat->conn_flags;
  1751. udest->weight = udest_compat->weight;
  1752. udest->u_threshold = udest_compat->u_threshold;
  1753. udest->l_threshold = udest_compat->l_threshold;
  1754. }
  1755. static int
  1756. do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
  1757. {
  1758. int ret;
  1759. unsigned char arg[MAX_ARG_LEN];
  1760. struct ip_vs_service_user *usvc_compat;
  1761. struct ip_vs_service_user_kern usvc;
  1762. struct ip_vs_service *svc;
  1763. struct ip_vs_dest_user *udest_compat;
  1764. struct ip_vs_dest_user_kern udest;
  1765. if (!capable(CAP_NET_ADMIN))
  1766. return -EPERM;
  1767. if (len != set_arglen[SET_CMDID(cmd)]) {
  1768. IP_VS_ERR("set_ctl: len %u != %u\n",
  1769. len, set_arglen[SET_CMDID(cmd)]);
  1770. return -EINVAL;
  1771. }
  1772. if (copy_from_user(arg, user, len) != 0)
  1773. return -EFAULT;
  1774. /* increase the module use count */
  1775. ip_vs_use_count_inc();
  1776. if (mutex_lock_interruptible(&__ip_vs_mutex)) {
  1777. ret = -ERESTARTSYS;
  1778. goto out_dec;
  1779. }
  1780. if (cmd == IP_VS_SO_SET_FLUSH) {
  1781. /* Flush the virtual service */
  1782. ret = ip_vs_flush();
  1783. goto out_unlock;
  1784. } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
  1785. /* Set timeout values for (tcp tcpfin udp) */
  1786. ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
  1787. goto out_unlock;
  1788. } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
  1789. struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
  1790. ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
  1791. goto out_unlock;
  1792. } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
  1793. struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
  1794. ret = stop_sync_thread(dm->state);
  1795. goto out_unlock;
  1796. }
  1797. usvc_compat = (struct ip_vs_service_user *)arg;
  1798. udest_compat = (struct ip_vs_de