ip_vs_ctl.c 55 KB

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