ip_vs_ctl.c 54 KB

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