ip_vs_ctl.c 55 KB

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