ip_vs_ctl.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294
  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the NetFilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  9. * Peter Kese <peter.kese@ijs.si>
  10. * Julian Anastasov <ja@ssi.bg>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * Changes:
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/capability.h>
  24. #include <linux/fs.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/swap.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/netfilter.h>
  31. #include <linux/netfilter_ipv4.h>
  32. #include <linux/mutex.h>
  33. #include <net/net_namespace.h>
  34. #include <net/ip.h>
  35. #include <net/route.h>
  36. #include <net/sock.h>
  37. #include <net/genetlink.h>
  38. #include <asm/uaccess.h>
  39. #include <net/ip_vs.h>
  40. /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
  41. static DEFINE_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(struct work_struct *work);
  197. static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
  198. static void defense_work_handler(struct work_struct *work)
  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, __be32 addr, __be16 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.ip,
  276. 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, __be32 vaddr, __be16 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.ip == 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, __be32 vaddr, __be16 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(__be32 addr, __be16 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.ip, 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, __be32 daddr, __be16 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.ip == 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, __be32 daddr, __be16 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.ip == daddr) && (dest->port == dport)) {
  486. /* HIT */
  487. return dest;
  488. }
  489. }
  490. return NULL;
  491. }
  492. /*
  493. * Find destination by {daddr,dport,vaddr,protocol}
  494. * Cretaed to be used in ip_vs_process_message() in
  495. * the backup synchronization daemon. It finds the
  496. * destination to be bound to the received connection
  497. * on the backup.
  498. *
  499. * ip_vs_lookup_real_service() looked promissing, but
  500. * seems not working as expected.
  501. */
  502. struct ip_vs_dest *ip_vs_find_dest(__be32 daddr, __be16 dport,
  503. __be32 vaddr, __be16 vport, __u16 protocol)
  504. {
  505. struct ip_vs_dest *dest;
  506. struct ip_vs_service *svc;
  507. svc = ip_vs_service_get(0, protocol, vaddr, vport);
  508. if (!svc)
  509. return NULL;
  510. dest = ip_vs_lookup_dest(svc, daddr, dport);
  511. if (dest)
  512. atomic_inc(&dest->refcnt);
  513. ip_vs_service_put(svc);
  514. return dest;
  515. }
  516. /*
  517. * Lookup dest by {svc,addr,port} in the destination trash.
  518. * The destination trash is used to hold the destinations that are removed
  519. * from the service table but are still referenced by some conn entries.
  520. * The reason to add the destination trash is when the dest is temporary
  521. * down (either by administrator or by monitor program), the dest can be
  522. * picked back from the trash, the remaining connections to the dest can
  523. * continue, and the counting information of the dest is also useful for
  524. * scheduling.
  525. */
  526. static struct ip_vs_dest *
  527. ip_vs_trash_get_dest(struct ip_vs_service *svc, __be32 daddr, __be16 dport)
  528. {
  529. struct ip_vs_dest *dest, *nxt;
  530. /*
  531. * Find the destination in trash
  532. */
  533. list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
  534. IP_VS_DBG(3, "Destination %u/%u.%u.%u.%u:%u still in trash, "
  535. "dest->refcnt=%d\n",
  536. dest->vfwmark,
  537. NIPQUAD(dest->addr.ip), ntohs(dest->port),
  538. atomic_read(&dest->refcnt));
  539. if (dest->addr.ip == daddr &&
  540. dest->port == dport &&
  541. dest->vfwmark == svc->fwmark &&
  542. dest->protocol == svc->protocol &&
  543. (svc->fwmark ||
  544. (dest->vaddr.ip == svc->addr.ip &&
  545. dest->vport == svc->port))) {
  546. /* HIT */
  547. return dest;
  548. }
  549. /*
  550. * Try to purge the destination from trash if not referenced
  551. */
  552. if (atomic_read(&dest->refcnt) == 1) {
  553. IP_VS_DBG(3, "Removing destination %u/%u.%u.%u.%u:%u "
  554. "from trash\n",
  555. dest->vfwmark,
  556. NIPQUAD(dest->addr.ip), ntohs(dest->port));
  557. list_del(&dest->n_list);
  558. ip_vs_dst_reset(dest);
  559. __ip_vs_unbind_svc(dest);
  560. kfree(dest);
  561. }
  562. }
  563. return NULL;
  564. }
  565. /*
  566. * Clean up all the destinations in the trash
  567. * Called by the ip_vs_control_cleanup()
  568. *
  569. * When the ip_vs_control_clearup is activated by ipvs module exit,
  570. * the service tables must have been flushed and all the connections
  571. * are expired, and the refcnt of each destination in the trash must
  572. * be 1, so we simply release them here.
  573. */
  574. static void ip_vs_trash_cleanup(void)
  575. {
  576. struct ip_vs_dest *dest, *nxt;
  577. list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
  578. list_del(&dest->n_list);
  579. ip_vs_dst_reset(dest);
  580. __ip_vs_unbind_svc(dest);
  581. kfree(dest);
  582. }
  583. }
  584. static void
  585. ip_vs_zero_stats(struct ip_vs_stats *stats)
  586. {
  587. spin_lock_bh(&stats->lock);
  588. stats->conns = 0;
  589. stats->inpkts = 0;
  590. stats->outpkts = 0;
  591. stats->inbytes = 0;
  592. stats->outbytes = 0;
  593. stats->cps = 0;
  594. stats->inpps = 0;
  595. stats->outpps = 0;
  596. stats->inbps = 0;
  597. stats->outbps = 0;
  598. ip_vs_zero_estimator(stats);
  599. spin_unlock_bh(&stats->lock);
  600. }
  601. /*
  602. * Update a destination in the given service
  603. */
  604. static void
  605. __ip_vs_update_dest(struct ip_vs_service *svc,
  606. struct ip_vs_dest *dest, struct ip_vs_dest_user_kern *udest)
  607. {
  608. int conn_flags;
  609. /* set the weight and the flags */
  610. atomic_set(&dest->weight, udest->weight);
  611. conn_flags = udest->conn_flags | IP_VS_CONN_F_INACTIVE;
  612. /* check if local node and update the flags */
  613. if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
  614. conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
  615. | IP_VS_CONN_F_LOCALNODE;
  616. }
  617. /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
  618. if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
  619. conn_flags |= IP_VS_CONN_F_NOOUTPUT;
  620. } else {
  621. /*
  622. * Put the real service in ip_vs_rtable if not present.
  623. * For now only for NAT!
  624. */
  625. write_lock_bh(&__ip_vs_rs_lock);
  626. ip_vs_rs_hash(dest);
  627. write_unlock_bh(&__ip_vs_rs_lock);
  628. }
  629. atomic_set(&dest->conn_flags, conn_flags);
  630. /* bind the service */
  631. if (!dest->svc) {
  632. __ip_vs_bind_svc(dest, svc);
  633. } else {
  634. if (dest->svc != svc) {
  635. __ip_vs_unbind_svc(dest);
  636. ip_vs_zero_stats(&dest->stats);
  637. __ip_vs_bind_svc(dest, svc);
  638. }
  639. }
  640. /* set the dest status flags */
  641. dest->flags |= IP_VS_DEST_F_AVAILABLE;
  642. if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
  643. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  644. dest->u_threshold = udest->u_threshold;
  645. dest->l_threshold = udest->l_threshold;
  646. }
  647. /*
  648. * Create a destination for the given service
  649. */
  650. static int
  651. ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
  652. struct ip_vs_dest **dest_p)
  653. {
  654. struct ip_vs_dest *dest;
  655. unsigned atype;
  656. EnterFunction(2);
  657. atype = inet_addr_type(&init_net, udest->addr.ip);
  658. if (atype != RTN_LOCAL && atype != RTN_UNICAST)
  659. return -EINVAL;
  660. dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
  661. if (dest == NULL) {
  662. IP_VS_ERR("ip_vs_new_dest: kmalloc failed.\n");
  663. return -ENOMEM;
  664. }
  665. dest->af = svc->af;
  666. dest->protocol = svc->protocol;
  667. dest->vaddr = svc->addr;
  668. dest->vport = svc->port;
  669. dest->vfwmark = svc->fwmark;
  670. ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
  671. dest->port = udest->port;
  672. atomic_set(&dest->activeconns, 0);
  673. atomic_set(&dest->inactconns, 0);
  674. atomic_set(&dest->persistconns, 0);
  675. atomic_set(&dest->refcnt, 0);
  676. INIT_LIST_HEAD(&dest->d_list);
  677. spin_lock_init(&dest->dst_lock);
  678. spin_lock_init(&dest->stats.lock);
  679. __ip_vs_update_dest(svc, dest, udest);
  680. ip_vs_new_estimator(&dest->stats);
  681. *dest_p = dest;
  682. LeaveFunction(2);
  683. return 0;
  684. }
  685. /*
  686. * Add a destination into an existing service
  687. */
  688. static int
  689. ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  690. {
  691. struct ip_vs_dest *dest;
  692. union nf_inet_addr daddr;
  693. __be16 dport = udest->port;
  694. int ret;
  695. EnterFunction(2);
  696. if (udest->weight < 0) {
  697. IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
  698. return -ERANGE;
  699. }
  700. if (udest->l_threshold > udest->u_threshold) {
  701. IP_VS_ERR("ip_vs_add_dest(): lower threshold is higher than "
  702. "upper threshold\n");
  703. return -ERANGE;
  704. }
  705. ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
  706. /*
  707. * Check if the dest already exists in the list
  708. */
  709. dest = ip_vs_lookup_dest(svc, daddr.ip, dport);
  710. if (dest != NULL) {
  711. IP_VS_DBG(1, "ip_vs_add_dest(): dest already exists\n");
  712. return -EEXIST;
  713. }
  714. /*
  715. * Check if the dest already exists in the trash and
  716. * is from the same service
  717. */
  718. dest = ip_vs_trash_get_dest(svc, daddr.ip, dport);
  719. if (dest != NULL) {
  720. IP_VS_DBG(3, "Get destination %u.%u.%u.%u:%u from trash, "
  721. "dest->refcnt=%d, service %u/%u.%u.%u.%u:%u\n",
  722. NIPQUAD(daddr), ntohs(dport),
  723. atomic_read(&dest->refcnt),
  724. dest->vfwmark,
  725. NIPQUAD(dest->vaddr.ip),
  726. ntohs(dest->vport));
  727. __ip_vs_update_dest(svc, dest, udest);
  728. /*
  729. * Get the destination from the trash
  730. */
  731. list_del(&dest->n_list);
  732. ip_vs_new_estimator(&dest->stats);
  733. write_lock_bh(&__ip_vs_svc_lock);
  734. /*
  735. * Wait until all other svc users go away.
  736. */
  737. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  738. list_add(&dest->n_list, &svc->destinations);
  739. svc->num_dests++;
  740. /* call the update_service function of its scheduler */
  741. if (svc->scheduler->update_service)
  742. svc->scheduler->update_service(svc);
  743. write_unlock_bh(&__ip_vs_svc_lock);
  744. return 0;
  745. }
  746. /*
  747. * Allocate and initialize the dest structure
  748. */
  749. ret = ip_vs_new_dest(svc, udest, &dest);
  750. if (ret) {
  751. return ret;
  752. }
  753. /*
  754. * Add the dest entry into the list
  755. */
  756. atomic_inc(&dest->refcnt);
  757. write_lock_bh(&__ip_vs_svc_lock);
  758. /*
  759. * Wait until all other svc users go away.
  760. */
  761. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  762. list_add(&dest->n_list, &svc->destinations);
  763. svc->num_dests++;
  764. /* call the update_service function of its scheduler */
  765. if (svc->scheduler->update_service)
  766. svc->scheduler->update_service(svc);
  767. write_unlock_bh(&__ip_vs_svc_lock);
  768. LeaveFunction(2);
  769. return 0;
  770. }
  771. /*
  772. * Edit a destination in the given service
  773. */
  774. static int
  775. ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  776. {
  777. struct ip_vs_dest *dest;
  778. union nf_inet_addr daddr;
  779. __be16 dport = udest->port;
  780. EnterFunction(2);
  781. if (udest->weight < 0) {
  782. IP_VS_ERR("ip_vs_edit_dest(): server weight less than zero\n");
  783. return -ERANGE;
  784. }
  785. if (udest->l_threshold > udest->u_threshold) {
  786. IP_VS_ERR("ip_vs_edit_dest(): lower threshold is higher than "
  787. "upper threshold\n");
  788. return -ERANGE;
  789. }
  790. ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
  791. /*
  792. * Lookup the destination list
  793. */
  794. dest = ip_vs_lookup_dest(svc, daddr.ip, dport);
  795. if (dest == NULL) {
  796. IP_VS_DBG(1, "ip_vs_edit_dest(): dest doesn't exist\n");
  797. return -ENOENT;
  798. }
  799. __ip_vs_update_dest(svc, dest, udest);
  800. write_lock_bh(&__ip_vs_svc_lock);
  801. /* Wait until all other svc users go away */
  802. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  803. /* call the update_service, because server weight may be changed */
  804. if (svc->scheduler->update_service)
  805. svc->scheduler->update_service(svc);
  806. write_unlock_bh(&__ip_vs_svc_lock);
  807. LeaveFunction(2);
  808. return 0;
  809. }
  810. /*
  811. * Delete a destination (must be already unlinked from the service)
  812. */
  813. static void __ip_vs_del_dest(struct ip_vs_dest *dest)
  814. {
  815. ip_vs_kill_estimator(&dest->stats);
  816. /*
  817. * Remove it from the d-linked list with the real services.
  818. */
  819. write_lock_bh(&__ip_vs_rs_lock);
  820. ip_vs_rs_unhash(dest);
  821. write_unlock_bh(&__ip_vs_rs_lock);
  822. /*
  823. * Decrease the refcnt of the dest, and free the dest
  824. * if nobody refers to it (refcnt=0). Otherwise, throw
  825. * the destination into the trash.
  826. */
  827. if (atomic_dec_and_test(&dest->refcnt)) {
  828. ip_vs_dst_reset(dest);
  829. /* simply decrease svc->refcnt here, let the caller check
  830. and release the service if nobody refers to it.
  831. Only user context can release destination and service,
  832. and only one user context can update virtual service at a
  833. time, so the operation here is OK */
  834. atomic_dec(&dest->svc->refcnt);
  835. kfree(dest);
  836. } else {
  837. IP_VS_DBG(3, "Moving dest %u.%u.%u.%u:%u into trash, "
  838. "dest->refcnt=%d\n",
  839. NIPQUAD(dest->addr.ip), ntohs(dest->port),
  840. atomic_read(&dest->refcnt));
  841. list_add(&dest->n_list, &ip_vs_dest_trash);
  842. atomic_inc(&dest->refcnt);
  843. }
  844. }
  845. /*
  846. * Unlink a destination from the given service
  847. */
  848. static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
  849. struct ip_vs_dest *dest,
  850. int svcupd)
  851. {
  852. dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
  853. /*
  854. * Remove it from the d-linked destination list.
  855. */
  856. list_del(&dest->n_list);
  857. svc->num_dests--;
  858. /*
  859. * Call the update_service function of its scheduler
  860. */
  861. if (svcupd && svc->scheduler->update_service)
  862. svc->scheduler->update_service(svc);
  863. }
  864. /*
  865. * Delete a destination server in the given service
  866. */
  867. static int
  868. ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
  869. {
  870. struct ip_vs_dest *dest;
  871. __be16 dport = udest->port;
  872. EnterFunction(2);
  873. dest = ip_vs_lookup_dest(svc, udest->addr.ip, dport);
  874. if (dest == NULL) {
  875. IP_VS_DBG(1, "ip_vs_del_dest(): destination not found!\n");
  876. return -ENOENT;
  877. }
  878. write_lock_bh(&__ip_vs_svc_lock);
  879. /*
  880. * Wait until all other svc users go away.
  881. */
  882. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  883. /*
  884. * Unlink dest from the service
  885. */
  886. __ip_vs_unlink_dest(svc, dest, 1);
  887. write_unlock_bh(&__ip_vs_svc_lock);
  888. /*
  889. * Delete the destination
  890. */
  891. __ip_vs_del_dest(dest);
  892. LeaveFunction(2);
  893. return 0;
  894. }
  895. /*
  896. * Add a service into the service hash table
  897. */
  898. static int
  899. ip_vs_add_service(struct ip_vs_service_user_kern *u,
  900. struct ip_vs_service **svc_p)
  901. {
  902. int ret = 0;
  903. struct ip_vs_scheduler *sched = NULL;
  904. struct ip_vs_service *svc = NULL;
  905. /* increase the module use count */
  906. ip_vs_use_count_inc();
  907. /* Lookup the scheduler by 'u->sched_name' */
  908. sched = ip_vs_scheduler_get(u->sched_name);
  909. if (sched == NULL) {
  910. IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
  911. u->sched_name);
  912. ret = -ENOENT;
  913. goto out_mod_dec;
  914. }
  915. svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
  916. if (svc == NULL) {
  917. IP_VS_DBG(1, "ip_vs_add_service: kmalloc failed.\n");
  918. ret = -ENOMEM;
  919. goto out_err;
  920. }
  921. /* I'm the first user of the service */
  922. atomic_set(&svc->usecnt, 1);
  923. atomic_set(&svc->refcnt, 0);
  924. svc->af = u->af;
  925. svc->protocol = u->protocol;
  926. ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
  927. svc->port = u->port;
  928. svc->fwmark = u->fwmark;
  929. svc->flags = u->flags;
  930. svc->timeout = u->timeout * HZ;
  931. svc->netmask = u->netmask;
  932. INIT_LIST_HEAD(&svc->destinations);
  933. rwlock_init(&svc->sched_lock);
  934. spin_lock_init(&svc->stats.lock);
  935. /* Bind the scheduler */
  936. ret = ip_vs_bind_scheduler(svc, sched);
  937. if (ret)
  938. goto out_err;
  939. sched = NULL;
  940. /* Update the virtual service counters */
  941. if (svc->port == FTPPORT)
  942. atomic_inc(&ip_vs_ftpsvc_counter);
  943. else if (svc->port == 0)
  944. atomic_inc(&ip_vs_nullsvc_counter);
  945. ip_vs_new_estimator(&svc->stats);
  946. ip_vs_num_services++;
  947. /* Hash the service into the service table */
  948. write_lock_bh(&__ip_vs_svc_lock);
  949. ip_vs_svc_hash(svc);
  950. write_unlock_bh(&__ip_vs_svc_lock);
  951. *svc_p = svc;
  952. return 0;
  953. out_err:
  954. if (svc != NULL) {
  955. if (svc->scheduler)
  956. ip_vs_unbind_scheduler(svc);
  957. if (svc->inc) {
  958. local_bh_disable();
  959. ip_vs_app_inc_put(svc->inc);
  960. local_bh_enable();
  961. }
  962. kfree(svc);
  963. }
  964. ip_vs_scheduler_put(sched);
  965. out_mod_dec:
  966. /* decrease the module use count */
  967. ip_vs_use_count_dec();
  968. return ret;
  969. }
  970. /*
  971. * Edit a service and bind it with a new scheduler
  972. */
  973. static int
  974. ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
  975. {
  976. struct ip_vs_scheduler *sched, *old_sched;
  977. int ret = 0;
  978. /*
  979. * Lookup the scheduler, by 'u->sched_name'
  980. */
  981. sched = ip_vs_scheduler_get(u->sched_name);
  982. if (sched == NULL) {
  983. IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
  984. u->sched_name);
  985. return -ENOENT;
  986. }
  987. old_sched = sched;
  988. write_lock_bh(&__ip_vs_svc_lock);
  989. /*
  990. * Wait until all other svc users go away.
  991. */
  992. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  993. /*
  994. * Set the flags and timeout value
  995. */
  996. svc->flags = u->flags | IP_VS_SVC_F_HASHED;
  997. svc->timeout = u->timeout * HZ;
  998. svc->netmask = u->netmask;
  999. old_sched = svc->scheduler;
  1000. if (sched != old_sched) {
  1001. /*
  1002. * Unbind the old scheduler
  1003. */
  1004. if ((ret = ip_vs_unbind_scheduler(svc))) {
  1005. old_sched = sched;
  1006. goto out;
  1007. }
  1008. /*
  1009. * Bind the new scheduler
  1010. */
  1011. if ((ret = ip_vs_bind_scheduler(svc, sched))) {
  1012. /*
  1013. * If ip_vs_bind_scheduler fails, restore the old
  1014. * scheduler.
  1015. * The main reason of failure is out of memory.
  1016. *
  1017. * The question is if the old scheduler can be
  1018. * restored all the time. TODO: if it cannot be
  1019. * restored some time, we must delete the service,
  1020. * otherwise the system may crash.
  1021. */
  1022. ip_vs_bind_scheduler(svc, old_sched);
  1023. old_sched = sched;
  1024. goto out;
  1025. }
  1026. }
  1027. out:
  1028. write_unlock_bh(&__ip_vs_svc_lock);
  1029. if (old_sched)
  1030. ip_vs_scheduler_put(old_sched);
  1031. return ret;
  1032. }
  1033. /*
  1034. * Delete a service from the service list
  1035. * - The service must be unlinked, unlocked and not referenced!
  1036. * - We are called under _bh lock
  1037. */
  1038. static void __ip_vs_del_service(struct ip_vs_service *svc)
  1039. {
  1040. struct ip_vs_dest *dest, *nxt;
  1041. struct ip_vs_scheduler *old_sched;
  1042. ip_vs_num_services--;
  1043. ip_vs_kill_estimator(&svc->stats);
  1044. /* Unbind scheduler */
  1045. old_sched = svc->scheduler;
  1046. ip_vs_unbind_scheduler(svc);
  1047. if (old_sched)
  1048. ip_vs_scheduler_put(old_sched);
  1049. /* Unbind app inc */
  1050. if (svc->inc) {
  1051. ip_vs_app_inc_put(svc->inc);
  1052. svc->inc = NULL;
  1053. }
  1054. /*
  1055. * Unlink the whole destination list
  1056. */
  1057. list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
  1058. __ip_vs_unlink_dest(svc, dest, 0);
  1059. __ip_vs_del_dest(dest);
  1060. }
  1061. /*
  1062. * Update the virtual service counters
  1063. */
  1064. if (svc->port == FTPPORT)
  1065. atomic_dec(&ip_vs_ftpsvc_counter);
  1066. else if (svc->port == 0)
  1067. atomic_dec(&ip_vs_nullsvc_counter);
  1068. /*
  1069. * Free the service if nobody refers to it
  1070. */
  1071. if (atomic_read(&svc->refcnt) == 0)
  1072. kfree(svc);
  1073. /* decrease the module use count */
  1074. ip_vs_use_count_dec();
  1075. }
  1076. /*
  1077. * Delete a service from the service list
  1078. */
  1079. static int ip_vs_del_service(struct ip_vs_service *svc)
  1080. {
  1081. if (svc == NULL)
  1082. return -EEXIST;
  1083. /*
  1084. * Unhash it from the service table
  1085. */
  1086. write_lock_bh(&__ip_vs_svc_lock);
  1087. ip_vs_svc_unhash(svc);
  1088. /*
  1089. * Wait until all the svc users go away.
  1090. */
  1091. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
  1092. __ip_vs_del_service(svc);
  1093. write_unlock_bh(&__ip_vs_svc_lock);
  1094. return 0;
  1095. }
  1096. /*
  1097. * Flush all the virtual services
  1098. */
  1099. static int ip_vs_flush(void)
  1100. {
  1101. int idx;
  1102. struct ip_vs_service *svc, *nxt;
  1103. /*
  1104. * Flush the service table hashed by <protocol,addr,port>
  1105. */
  1106. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1107. list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
  1108. write_lock_bh(&__ip_vs_svc_lock);
  1109. ip_vs_svc_unhash(svc);
  1110. /*
  1111. * Wait until all the svc users go away.
  1112. */
  1113. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
  1114. __ip_vs_del_service(svc);
  1115. write_unlock_bh(&__ip_vs_svc_lock);
  1116. }
  1117. }
  1118. /*
  1119. * Flush the service table hashed by fwmark
  1120. */
  1121. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1122. list_for_each_entry_safe(svc, nxt,
  1123. &ip_vs_svc_fwm_table[idx], f_list) {
  1124. write_lock_bh(&__ip_vs_svc_lock);
  1125. ip_vs_svc_unhash(svc);
  1126. /*
  1127. * Wait until all the svc users go away.
  1128. */
  1129. IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
  1130. __ip_vs_del_service(svc);
  1131. write_unlock_bh(&__ip_vs_svc_lock);
  1132. }
  1133. }
  1134. return 0;
  1135. }
  1136. /*
  1137. * Zero counters in a service or all services
  1138. */
  1139. static int ip_vs_zero_service(struct ip_vs_service *svc)
  1140. {
  1141. struct ip_vs_dest *dest;
  1142. write_lock_bh(&__ip_vs_svc_lock);
  1143. list_for_each_entry(dest, &svc->destinations, n_list) {
  1144. ip_vs_zero_stats(&dest->stats);
  1145. }
  1146. ip_vs_zero_stats(&svc->stats);
  1147. write_unlock_bh(&__ip_vs_svc_lock);
  1148. return 0;
  1149. }
  1150. static int ip_vs_zero_all(void)
  1151. {
  1152. int idx;
  1153. struct ip_vs_service *svc;
  1154. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1155. list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
  1156. ip_vs_zero_service(svc);
  1157. }
  1158. }
  1159. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1160. list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
  1161. ip_vs_zero_service(svc);
  1162. }
  1163. }
  1164. ip_vs_zero_stats(&ip_vs_stats);
  1165. return 0;
  1166. }
  1167. static int
  1168. proc_do_defense_mode(ctl_table *table, int write, struct file * filp,
  1169. void __user *buffer, size_t *lenp, loff_t *ppos)
  1170. {
  1171. int *valp = table->data;
  1172. int val = *valp;
  1173. int rc;
  1174. rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  1175. if (write && (*valp != val)) {
  1176. if ((*valp < 0) || (*valp > 3)) {
  1177. /* Restore the correct value */
  1178. *valp = val;
  1179. } else {
  1180. update_defense_level();
  1181. }
  1182. }
  1183. return rc;
  1184. }
  1185. static int
  1186. proc_do_sync_threshold(ctl_table *table, int write, struct file *filp,
  1187. void __user *buffer, size_t *lenp, loff_t *ppos)
  1188. {
  1189. int *valp = table->data;
  1190. int val[2];
  1191. int rc;
  1192. /* backup the value first */
  1193. memcpy(val, valp, sizeof(val));
  1194. rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  1195. if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
  1196. /* Restore the correct value */
  1197. memcpy(valp, val, sizeof(val));
  1198. }
  1199. return rc;
  1200. }
  1201. /*
  1202. * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
  1203. */
  1204. static struct ctl_table vs_vars[] = {
  1205. {
  1206. .procname = "amemthresh",
  1207. .data = &sysctl_ip_vs_amemthresh,
  1208. .maxlen = sizeof(int),
  1209. .mode = 0644,
  1210. .proc_handler = &proc_dointvec,
  1211. },
  1212. #ifdef CONFIG_IP_VS_DEBUG
  1213. {
  1214. .procname = "debug_level",
  1215. .data = &sysctl_ip_vs_debug_level,
  1216. .maxlen = sizeof(int),
  1217. .mode = 0644,
  1218. .proc_handler = &proc_dointvec,
  1219. },
  1220. #endif
  1221. {
  1222. .procname = "am_droprate",
  1223. .data = &sysctl_ip_vs_am_droprate,
  1224. .maxlen = sizeof(int),
  1225. .mode = 0644,
  1226. .proc_handler = &proc_dointvec,
  1227. },
  1228. {
  1229. .procname = "drop_entry",
  1230. .data = &sysctl_ip_vs_drop_entry,
  1231. .maxlen = sizeof(int),
  1232. .mode = 0644,
  1233. .proc_handler = &proc_do_defense_mode,
  1234. },
  1235. {
  1236. .procname = "drop_packet",
  1237. .data = &sysctl_ip_vs_drop_packet,
  1238. .maxlen = sizeof(int),
  1239. .mode = 0644,
  1240. .proc_handler = &proc_do_defense_mode,
  1241. },
  1242. {
  1243. .procname = "secure_tcp",
  1244. .data = &sysctl_ip_vs_secure_tcp,
  1245. .maxlen = sizeof(int),
  1246. .mode = 0644,
  1247. .proc_handler = &proc_do_defense_mode,
  1248. },
  1249. #if 0
  1250. {
  1251. .procname = "timeout_established",
  1252. .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
  1253. .maxlen = sizeof(int),
  1254. .mode = 0644,
  1255. .proc_handler = &proc_dointvec_jiffies,
  1256. },
  1257. {
  1258. .procname = "timeout_synsent",
  1259. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
  1260. .maxlen = sizeof(int),
  1261. .mode = 0644,
  1262. .proc_handler = &proc_dointvec_jiffies,
  1263. },
  1264. {
  1265. .procname = "timeout_synrecv",
  1266. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
  1267. .maxlen = sizeof(int),
  1268. .mode = 0644,
  1269. .proc_handler = &proc_dointvec_jiffies,
  1270. },
  1271. {
  1272. .procname = "timeout_finwait",
  1273. .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
  1274. .maxlen = sizeof(int),
  1275. .mode = 0644,
  1276. .proc_handler = &proc_dointvec_jiffies,
  1277. },
  1278. {
  1279. .procname = "timeout_timewait",
  1280. .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
  1281. .maxlen = sizeof(int),
  1282. .mode = 0644,
  1283. .proc_handler = &proc_dointvec_jiffies,
  1284. },
  1285. {
  1286. .procname = "timeout_close",
  1287. .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
  1288. .maxlen = sizeof(int),
  1289. .mode = 0644,
  1290. .proc_handler = &proc_dointvec_jiffies,
  1291. },
  1292. {
  1293. .procname = "timeout_closewait",
  1294. .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
  1295. .maxlen = sizeof(int),
  1296. .mode = 0644,
  1297. .proc_handler = &proc_dointvec_jiffies,
  1298. },
  1299. {
  1300. .procname = "timeout_lastack",
  1301. .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
  1302. .maxlen = sizeof(int),
  1303. .mode = 0644,
  1304. .proc_handler = &proc_dointvec_jiffies,
  1305. },
  1306. {
  1307. .procname = "timeout_listen",
  1308. .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
  1309. .maxlen = sizeof(int),
  1310. .mode = 0644,
  1311. .proc_handler = &proc_dointvec_jiffies,
  1312. },
  1313. {
  1314. .procname = "timeout_synack",
  1315. .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
  1316. .maxlen = sizeof(int),
  1317. .mode = 0644,
  1318. .proc_handler = &proc_dointvec_jiffies,
  1319. },
  1320. {
  1321. .procname = "timeout_udp",
  1322. .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
  1323. .maxlen = sizeof(int),
  1324. .mode = 0644,
  1325. .proc_handler = &proc_dointvec_jiffies,
  1326. },
  1327. {
  1328. .procname = "timeout_icmp",
  1329. .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
  1330. .maxlen = sizeof(int),
  1331. .mode = 0644,
  1332. .proc_handler = &proc_dointvec_jiffies,
  1333. },
  1334. #endif
  1335. {
  1336. .procname = "cache_bypass",
  1337. .data = &sysctl_ip_vs_cache_bypass,
  1338. .maxlen = sizeof(int),
  1339. .mode = 0644,
  1340. .proc_handler = &proc_dointvec,
  1341. },
  1342. {
  1343. .procname = "expire_nodest_conn",
  1344. .data = &sysctl_ip_vs_expire_nodest_conn,
  1345. .maxlen = sizeof(int),
  1346. .mode = 0644,
  1347. .proc_handler = &proc_dointvec,
  1348. },
  1349. {
  1350. .procname = "expire_quiescent_template",
  1351. .data = &sysctl_ip_vs_expire_quiescent_template,
  1352. .maxlen = sizeof(int),
  1353. .mode = 0644,
  1354. .proc_handler = &proc_dointvec,
  1355. },
  1356. {
  1357. .procname = "sync_threshold",
  1358. .data = &sysctl_ip_vs_sync_threshold,
  1359. .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
  1360. .mode = 0644,
  1361. .proc_handler = &proc_do_sync_threshold,
  1362. },
  1363. {
  1364. .procname = "nat_icmp_send",
  1365. .data = &sysctl_ip_vs_nat_icmp_send,
  1366. .maxlen = sizeof(int),
  1367. .mode = 0644,
  1368. .proc_handler = &proc_dointvec,
  1369. },
  1370. { .ctl_name = 0 }
  1371. };
  1372. const struct ctl_path net_vs_ctl_path[] = {
  1373. { .procname = "net", .ctl_name = CTL_NET, },
  1374. { .procname = "ipv4", .ctl_name = NET_IPV4, },
  1375. { .procname = "vs", },
  1376. { }
  1377. };
  1378. EXPORT_SYMBOL_GPL(net_vs_ctl_path);
  1379. static struct ctl_table_header * sysctl_header;
  1380. #ifdef CONFIG_PROC_FS
  1381. struct ip_vs_iter {
  1382. struct list_head *table;
  1383. int bucket;
  1384. };
  1385. /*
  1386. * Write the contents of the VS rule table to a PROCfs file.
  1387. * (It is kept just for backward compatibility)
  1388. */
  1389. static inline const char *ip_vs_fwd_name(unsigned flags)
  1390. {
  1391. switch (flags & IP_VS_CONN_F_FWD_MASK) {
  1392. case IP_VS_CONN_F_LOCALNODE:
  1393. return "Local";
  1394. case IP_VS_CONN_F_TUNNEL:
  1395. return "Tunnel";
  1396. case IP_VS_CONN_F_DROUTE:
  1397. return "Route";
  1398. default:
  1399. return "Masq";
  1400. }
  1401. }
  1402. /* Get the Nth entry in the two lists */
  1403. static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
  1404. {
  1405. struct ip_vs_iter *iter = seq->private;
  1406. int idx;
  1407. struct ip_vs_service *svc;
  1408. /* look in hash by protocol */
  1409. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1410. list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
  1411. if (pos-- == 0){
  1412. iter->table = ip_vs_svc_table;
  1413. iter->bucket = idx;
  1414. return svc;
  1415. }
  1416. }
  1417. }
  1418. /* keep looking in fwmark */
  1419. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1420. list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
  1421. if (pos-- == 0) {
  1422. iter->table = ip_vs_svc_fwm_table;
  1423. iter->bucket = idx;
  1424. return svc;
  1425. }
  1426. }
  1427. }
  1428. return NULL;
  1429. }
  1430. static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
  1431. {
  1432. read_lock_bh(&__ip_vs_svc_lock);
  1433. return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
  1434. }
  1435. static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1436. {
  1437. struct list_head *e;
  1438. struct ip_vs_iter *iter;
  1439. struct ip_vs_service *svc;
  1440. ++*pos;
  1441. if (v == SEQ_START_TOKEN)
  1442. return ip_vs_info_array(seq,0);
  1443. svc = v;
  1444. iter = seq->private;
  1445. if (iter->table == ip_vs_svc_table) {
  1446. /* next service in table hashed by protocol */
  1447. if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
  1448. return list_entry(e, struct ip_vs_service, s_list);
  1449. while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
  1450. list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
  1451. s_list) {
  1452. return svc;
  1453. }
  1454. }
  1455. iter->table = ip_vs_svc_fwm_table;
  1456. iter->bucket = -1;
  1457. goto scan_fwmark;
  1458. }
  1459. /* next service in hashed by fwmark */
  1460. if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
  1461. return list_entry(e, struct ip_vs_service, f_list);
  1462. scan_fwmark:
  1463. while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
  1464. list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
  1465. f_list)
  1466. return svc;
  1467. }
  1468. return NULL;
  1469. }
  1470. static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
  1471. {
  1472. read_unlock_bh(&__ip_vs_svc_lock);
  1473. }
  1474. static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
  1475. {
  1476. if (v == SEQ_START_TOKEN) {
  1477. seq_printf(seq,
  1478. "IP Virtual Server version %d.%d.%d (size=%d)\n",
  1479. NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
  1480. seq_puts(seq,
  1481. "Prot LocalAddress:Port Scheduler Flags\n");
  1482. seq_puts(seq,
  1483. " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
  1484. } else {
  1485. const struct ip_vs_service *svc = v;
  1486. const struct ip_vs_iter *iter = seq->private;
  1487. const struct ip_vs_dest *dest;
  1488. if (iter->table == ip_vs_svc_table)
  1489. seq_printf(seq, "%s %08X:%04X %s ",
  1490. ip_vs_proto_name(svc->protocol),
  1491. ntohl(svc->addr.ip),
  1492. ntohs(svc->port),
  1493. svc->scheduler->name);
  1494. else
  1495. seq_printf(seq, "FWM %08X %s ",
  1496. svc->fwmark, svc->scheduler->name);
  1497. if (svc->flags & IP_VS_SVC_F_PERSISTENT)
  1498. seq_printf(seq, "persistent %d %08X\n",
  1499. svc->timeout,
  1500. ntohl(svc->netmask));
  1501. else
  1502. seq_putc(seq, '\n');
  1503. list_for_each_entry(dest, &svc->destinations, n_list) {
  1504. seq_printf(seq,
  1505. " -> %08X:%04X %-7s %-6d %-10d %-10d\n",
  1506. ntohl(dest->addr.ip), ntohs(dest->port),
  1507. ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
  1508. atomic_read(&dest->weight),
  1509. atomic_read(&dest->activeconns),
  1510. atomic_read(&dest->inactconns));
  1511. }
  1512. }
  1513. return 0;
  1514. }
  1515. static const struct seq_operations ip_vs_info_seq_ops = {
  1516. .start = ip_vs_info_seq_start,
  1517. .next = ip_vs_info_seq_next,
  1518. .stop = ip_vs_info_seq_stop,
  1519. .show = ip_vs_info_seq_show,
  1520. };
  1521. static int ip_vs_info_open(struct inode *inode, struct file *file)
  1522. {
  1523. return seq_open_private(file, &ip_vs_info_seq_ops,
  1524. sizeof(struct ip_vs_iter));
  1525. }
  1526. static const struct file_operations ip_vs_info_fops = {
  1527. .owner = THIS_MODULE,
  1528. .open = ip_vs_info_open,
  1529. .read = seq_read,
  1530. .llseek = seq_lseek,
  1531. .release = seq_release_private,
  1532. };
  1533. #endif
  1534. struct ip_vs_stats ip_vs_stats = {
  1535. .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
  1536. };
  1537. #ifdef CONFIG_PROC_FS
  1538. static int ip_vs_stats_show(struct seq_file *seq, void *v)
  1539. {
  1540. /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
  1541. seq_puts(seq,
  1542. " Total Incoming Outgoing Incoming Outgoing\n");
  1543. seq_printf(seq,
  1544. " Conns Packets Packets Bytes Bytes\n");
  1545. spin_lock_bh(&ip_vs_stats.lock);
  1546. seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.conns,
  1547. ip_vs_stats.inpkts, ip_vs_stats.outpkts,
  1548. (unsigned long long) ip_vs_stats.inbytes,
  1549. (unsigned long long) ip_vs_stats.outbytes);
  1550. /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
  1551. seq_puts(seq,
  1552. " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
  1553. seq_printf(seq,"%8X %8X %8X %16X %16X\n",
  1554. ip_vs_stats.cps,
  1555. ip_vs_stats.inpps,
  1556. ip_vs_stats.outpps,
  1557. ip_vs_stats.inbps,
  1558. ip_vs_stats.outbps);
  1559. spin_unlock_bh(&ip_vs_stats.lock);
  1560. return 0;
  1561. }
  1562. static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
  1563. {
  1564. return single_open(file, ip_vs_stats_show, NULL);
  1565. }
  1566. static const struct file_operations ip_vs_stats_fops = {
  1567. .owner = THIS_MODULE,
  1568. .open = ip_vs_stats_seq_open,
  1569. .read = seq_read,
  1570. .llseek = seq_lseek,
  1571. .release = single_release,
  1572. };
  1573. #endif
  1574. /*
  1575. * Set timeout values for tcp tcpfin udp in the timeout_table.
  1576. */
  1577. static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
  1578. {
  1579. IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
  1580. u->tcp_timeout,
  1581. u->tcp_fin_timeout,
  1582. u->udp_timeout);
  1583. #ifdef CONFIG_IP_VS_PROTO_TCP
  1584. if (u->tcp_timeout) {
  1585. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
  1586. = u->tcp_timeout * HZ;
  1587. }
  1588. if (u->tcp_fin_timeout) {
  1589. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
  1590. = u->tcp_fin_timeout * HZ;
  1591. }
  1592. #endif
  1593. #ifdef CONFIG_IP_VS_PROTO_UDP
  1594. if (u->udp_timeout) {
  1595. ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
  1596. = u->udp_timeout * HZ;
  1597. }
  1598. #endif
  1599. return 0;
  1600. }
  1601. #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
  1602. #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
  1603. #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
  1604. sizeof(struct ip_vs_dest_user))
  1605. #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
  1606. #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
  1607. #define MAX_ARG_LEN SVCDEST_ARG_LEN
  1608. static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
  1609. [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
  1610. [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
  1611. [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
  1612. [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
  1613. [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
  1614. [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
  1615. [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
  1616. [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
  1617. [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
  1618. [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
  1619. [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
  1620. };
  1621. static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
  1622. struct ip_vs_service_user *usvc_compat)
  1623. {
  1624. usvc->af = AF_INET;
  1625. usvc->protocol = usvc_compat->protocol;
  1626. usvc->addr.ip = usvc_compat->addr;
  1627. usvc->port = usvc_compat->port;
  1628. usvc->fwmark = usvc_compat->fwmark;
  1629. /* Deep copy of sched_name is not needed here */
  1630. usvc->sched_name = usvc_compat->sched_name;
  1631. usvc->flags = usvc_compat->flags;
  1632. usvc->timeout = usvc_compat->timeout;
  1633. usvc->netmask = usvc_compat->netmask;
  1634. }
  1635. static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
  1636. struct ip_vs_dest_user *udest_compat)
  1637. {
  1638. udest->addr.ip = udest_compat->addr;
  1639. udest->port = udest_compat->port;
  1640. udest->conn_flags = udest_compat->conn_flags;
  1641. udest->weight = udest_compat->weight;
  1642. udest->u_threshold = udest_compat->u_threshold;
  1643. udest->l_threshold = udest_compat->l_threshold;
  1644. }
  1645. static int
  1646. do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
  1647. {
  1648. int ret;
  1649. unsigned char arg[MAX_ARG_LEN];
  1650. struct ip_vs_service_user *usvc_compat;
  1651. struct ip_vs_service_user_kern usvc;
  1652. struct ip_vs_service *svc;
  1653. struct ip_vs_dest_user *udest_compat;
  1654. struct ip_vs_dest_user_kern udest;
  1655. if (!capable(CAP_NET_ADMIN))
  1656. return -EPERM;
  1657. if (len != set_arglen[SET_CMDID(cmd)]) {
  1658. IP_VS_ERR("set_ctl: len %u != %u\n",
  1659. len, set_arglen[SET_CMDID(cmd)]);
  1660. return -EINVAL;
  1661. }
  1662. if (copy_from_user(arg, user, len) != 0)
  1663. return -EFAULT;
  1664. /* increase the module use count */
  1665. ip_vs_use_count_inc();
  1666. if (mutex_lock_interruptible(&__ip_vs_mutex)) {
  1667. ret = -ERESTARTSYS;
  1668. goto out_dec;
  1669. }
  1670. if (cmd == IP_VS_SO_SET_FLUSH) {
  1671. /* Flush the virtual service */
  1672. ret = ip_vs_flush();
  1673. goto out_unlock;
  1674. } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
  1675. /* Set timeout values for (tcp tcpfin udp) */
  1676. ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
  1677. goto out_unlock;
  1678. } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
  1679. struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
  1680. ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
  1681. goto out_unlock;
  1682. } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
  1683. struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
  1684. ret = stop_sync_thread(dm->state);
  1685. goto out_unlock;
  1686. }
  1687. usvc_compat = (struct ip_vs_service_user *)arg;
  1688. udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
  1689. /* We only use the new structs internally, so copy userspace compat
  1690. * structs to extended internal versions */
  1691. ip_vs_copy_usvc_compat(&usvc, usvc_compat);
  1692. ip_vs_copy_udest_compat(&udest, udest_compat);
  1693. if (cmd == IP_VS_SO_SET_ZERO) {
  1694. /* if no service address is set, zero counters in all */
  1695. if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
  1696. ret = ip_vs_zero_all();
  1697. goto out_unlock;
  1698. }
  1699. }
  1700. /* Check for valid protocol: TCP or UDP, even for fwmark!=0 */
  1701. if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP) {
  1702. IP_VS_ERR("set_ctl: invalid protocol: %d %d.%d.%d.%d:%d %s\n",
  1703. usvc.protocol, NIPQUAD(usvc.addr.ip),
  1704. ntohs(usvc.port), usvc.sched_name);
  1705. ret = -EFAULT;
  1706. goto out_unlock;
  1707. }
  1708. /* Lookup the exact service by <protocol, addr, port> or fwmark */
  1709. if (usvc.fwmark == 0)
  1710. svc = __ip_vs_service_get(usvc.protocol,
  1711. usvc.addr.ip, usvc.port);
  1712. else
  1713. svc = __ip_vs_svc_fwm_get(usvc.fwmark);
  1714. if (cmd != IP_VS_SO_SET_ADD
  1715. && (svc == NULL || svc->protocol != usvc.protocol)) {
  1716. ret = -ESRCH;
  1717. goto out_unlock;
  1718. }
  1719. switch (cmd) {
  1720. case IP_VS_SO_SET_ADD:
  1721. if (svc != NULL)
  1722. ret = -EEXIST;
  1723. else
  1724. ret = ip_vs_add_service(&usvc, &svc);
  1725. break;
  1726. case IP_VS_SO_SET_EDIT:
  1727. ret = ip_vs_edit_service(svc, &usvc);
  1728. break;
  1729. case IP_VS_SO_SET_DEL:
  1730. ret = ip_vs_del_service(svc);
  1731. if (!ret)
  1732. goto out_unlock;
  1733. break;
  1734. case IP_VS_SO_SET_ZERO:
  1735. ret = ip_vs_zero_service(svc);
  1736. break;
  1737. case IP_VS_SO_SET_ADDDEST:
  1738. ret = ip_vs_add_dest(svc, &udest);
  1739. break;
  1740. case IP_VS_SO_SET_EDITDEST:
  1741. ret = ip_vs_edit_dest(svc, &udest);
  1742. break;
  1743. case IP_VS_SO_SET_DELDEST:
  1744. ret = ip_vs_del_dest(svc, &udest);
  1745. break;
  1746. default:
  1747. ret = -EINVAL;
  1748. }
  1749. if (svc)
  1750. ip_vs_service_put(svc);
  1751. out_unlock:
  1752. mutex_unlock(&__ip_vs_mutex);
  1753. out_dec:
  1754. /* decrease the module use count */
  1755. ip_vs_use_count_dec();
  1756. return ret;
  1757. }
  1758. static void
  1759. ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
  1760. {
  1761. spin_lock_bh(&src->lock);
  1762. memcpy(dst, src, (char*)&src->lock - (char*)src);
  1763. spin_unlock_bh(&src->lock);
  1764. }
  1765. static void
  1766. ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
  1767. {
  1768. dst->protocol = src->protocol;
  1769. dst->addr = src->addr.ip;
  1770. dst->port = src->port;
  1771. dst->fwmark = src->fwmark;
  1772. strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
  1773. dst->flags = src->flags;
  1774. dst->timeout = src->timeout / HZ;
  1775. dst->netmask = src->netmask;
  1776. dst->num_dests = src->num_dests;
  1777. ip_vs_copy_stats(&dst->stats, &src->stats);
  1778. }
  1779. static inline int
  1780. __ip_vs_get_service_entries(const struct ip_vs_get_services *get,
  1781. struct ip_vs_get_services __user *uptr)
  1782. {
  1783. int idx, count=0;
  1784. struct ip_vs_service *svc;
  1785. struct ip_vs_service_entry entry;
  1786. int ret = 0;
  1787. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1788. list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
  1789. if (count >= get->num_services)
  1790. goto out;
  1791. memset(&entry, 0, sizeof(entry));
  1792. ip_vs_copy_service(&entry, svc);
  1793. if (copy_to_user(&uptr->entrytable[count],
  1794. &entry, sizeof(entry))) {
  1795. ret = -EFAULT;
  1796. goto out;
  1797. }
  1798. count++;
  1799. }
  1800. }
  1801. for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  1802. list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
  1803. if (count >= get->num_services)
  1804. goto out;
  1805. memset(&entry, 0, sizeof(entry));
  1806. ip_vs_copy_service(&entry, svc);
  1807. if (copy_to_user(&uptr->entrytable[count],
  1808. &entry, sizeof(entry))) {
  1809. ret = -EFAULT;
  1810. goto out;
  1811. }
  1812. count++;
  1813. }
  1814. }
  1815. out:
  1816. return ret;
  1817. }
  1818. static inline int
  1819. __ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
  1820. struct ip_vs_get_dests __user *uptr)
  1821. {
  1822. struct ip_vs_service *svc;
  1823. int ret = 0;
  1824. if (get->fwmark)
  1825. svc = __ip_vs_svc_fwm_get(get->fwmark);
  1826. else
  1827. svc = __ip_vs_service_get(get->protocol,
  1828. get->addr, get->port);
  1829. if (svc) {
  1830. int count = 0;
  1831. struct ip_vs_dest *dest;
  1832. struct ip_vs_dest_entry entry;
  1833. list_for_each_entry(dest, &svc->destinations, n_list) {
  1834. if (count >= get->num_dests)
  1835. break;
  1836. entry.addr = dest->addr.ip;
  1837. entry.port = dest->port;
  1838. entry.conn_flags = atomic_read(&dest->conn_flags);
  1839. entry.weight = atomic_read(&dest->weight);
  1840. entry.u_threshold = dest->u_threshold;
  1841. entry.l_threshold = dest->l_threshold;
  1842. entry.activeconns = atomic_read(&dest->activeconns);
  1843. entry.inactconns = atomic_read(&dest->inactconns);
  1844. entry.persistconns = atomic_read(&dest->persistconns);
  1845. ip_vs_copy_stats(&entry.stats, &dest->stats);
  1846. if (copy_to_user(&uptr->entrytable[count],
  1847. &entry, sizeof(entry))) {
  1848. ret = -EFAULT;
  1849. break;
  1850. }
  1851. count++;
  1852. }
  1853. ip_vs_service_put(svc);
  1854. } else
  1855. ret = -ESRCH;
  1856. return ret;
  1857. }
  1858. static inline void
  1859. __ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
  1860. {
  1861. #ifdef CONFIG_IP_VS_PROTO_TCP
  1862. u->tcp_timeout =
  1863. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
  1864. u->tcp_fin_timeout =
  1865. ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
  1866. #endif
  1867. #ifdef CONFIG_IP_VS_PROTO_UDP
  1868. u->udp_timeout =
  1869. ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
  1870. #endif
  1871. }
  1872. #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
  1873. #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
  1874. #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
  1875. #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
  1876. #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
  1877. #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
  1878. #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
  1879. static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
  1880. [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
  1881. [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
  1882. [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
  1883. [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
  1884. [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
  1885. [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
  1886. [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
  1887. };
  1888. static int
  1889. do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1890. {
  1891. unsigned char arg[128];
  1892. int ret = 0;
  1893. if (!capable(CAP_NET_ADMIN))
  1894. return -EPERM;
  1895. if (*len < get_arglen[GET_CMDID(cmd)]) {
  1896. IP_VS_ERR("get_ctl: len %u < %u\n",
  1897. *len, get_arglen[GET_CMDID(cmd)]);
  1898. return -EINVAL;
  1899. }
  1900. if (copy_from_user(arg, user, get_arglen[GET_CMDID(cmd)]) != 0)
  1901. return -EFAULT;
  1902. if (mutex_lock_interruptible(&__ip_vs_mutex))
  1903. return -ERESTARTSYS;
  1904. switch (cmd) {
  1905. case IP_VS_SO_GET_VERSION:
  1906. {
  1907. char buf[64];
  1908. sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
  1909. NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
  1910. if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
  1911. ret = -EFAULT;
  1912. goto out;
  1913. }
  1914. *len = strlen(buf)+1;
  1915. }
  1916. break;
  1917. case IP_VS_SO_GET_INFO:
  1918. {
  1919. struct ip_vs_getinfo info;
  1920. info.version = IP_VS_VERSION_CODE;
  1921. info.size = IP_VS_CONN_TAB_SIZE;
  1922. info.num_services = ip_vs_num_services;
  1923. if (copy_to_user(user, &info, sizeof(info)) != 0)
  1924. ret = -EFAULT;
  1925. }
  1926. break;
  1927. case IP_VS_SO_GET_SERVICES:
  1928. {
  1929. struct ip_vs_get_services *get;
  1930. int size;
  1931. get = (struct ip_vs_get_services *)arg;
  1932. size = sizeof(*get) +
  1933. sizeof(struct ip_vs_service_entry) * get->num_services;
  1934. if (*len != size) {
  1935. IP_VS_ERR("length: %u != %u\n", *len, size);
  1936. ret = -EINVAL;
  1937. goto out;
  1938. }
  1939. ret = __ip_vs_get_service_entries(get, user);
  1940. }
  1941. break;
  1942. case IP_VS_SO_GET_SERVICE:
  1943. {
  1944. struct ip_vs_service_entry *entry;
  1945. struct ip_vs_service *svc;
  1946. entry = (struct ip_vs_service_entry *)arg;
  1947. if (entry->fwmark)
  1948. svc = __ip_vs_svc_fwm_get(entry->fwmark);
  1949. else
  1950. svc = __ip_vs_service_get(entry->protocol,
  1951. entry->addr, entry->port);
  1952. if (svc) {
  1953. ip_vs_copy_service(entry, svc);
  1954. if (copy_to_user(user, entry, sizeof(*entry)) != 0)
  1955. ret = -EFAULT;
  1956. ip_vs_service_put(svc);
  1957. } else
  1958. ret = -ESRCH;
  1959. }
  1960. break;
  1961. case IP_VS_SO_GET_DESTS:
  1962. {
  1963. struct ip_vs_get_dests *get;
  1964. int size;
  1965. get = (struct ip_vs_get_dests *)arg;
  1966. size = sizeof(*get) +
  1967. sizeof(struct ip_vs_dest_entry) * get->num_dests;
  1968. if (*len != size) {
  1969. IP_VS_ERR("length: %u != %u\n", *len, size);
  1970. ret = -EINVAL;
  1971. goto out;
  1972. }
  1973. ret = __ip_vs_get_dest_entries(get, user);
  1974. }
  1975. break;
  1976. case IP_VS_SO_GET_TIMEOUT:
  1977. {
  1978. struct ip_vs_timeout_user t;
  1979. __ip_vs_get_timeouts(&t);
  1980. if (copy_to_user(user, &t, sizeof(t)) != 0)
  1981. ret = -EFAULT;
  1982. }
  1983. break;
  1984. case IP_VS_SO_GET_DAEMON:
  1985. {
  1986. struct ip_vs_daemon_user d[2];
  1987. memset(&d, 0, sizeof(d));
  1988. if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
  1989. d[0].state = IP_VS_STATE_MASTER;
  1990. strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
  1991. d[0].syncid = ip_vs_master_syncid;
  1992. }
  1993. if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
  1994. d[1].state = IP_VS_STATE_BACKUP;
  1995. strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
  1996. d[1].syncid = ip_vs_backup_syncid;
  1997. }
  1998. if (copy_to_user(user, &d, sizeof(d)) != 0)
  1999. ret = -EFAULT;
  2000. }
  2001. break;
  2002. default:
  2003. ret = -EINVAL;
  2004. }
  2005. out:
  2006. mutex_unlock(&__ip_vs_mutex);
  2007. return ret;
  2008. }
  2009. static struct nf_sockopt_ops ip_vs_sockopts = {
  2010. .pf = PF_INET,
  2011. .set_optmin = IP_VS_BASE_CTL,
  2012. .set_optmax = IP_VS_SO_SET_MAX+1,
  2013. .set = do_ip_vs_set_ctl,
  2014. .get_optmin = IP_VS_BASE_CTL,
  2015. .get_optmax = IP_VS_SO_GET_MAX+1,
  2016. .get = do_ip_vs_get_ctl,
  2017. .owner = THIS_MODULE,
  2018. };
  2019. /*
  2020. * Generic Netlink interface
  2021. */
  2022. /* IPVS genetlink family */
  2023. static struct genl_family ip_vs_genl_family = {
  2024. .id = GENL_ID_GENERATE,
  2025. .hdrsize = 0,
  2026. .name = IPVS_GENL_NAME,
  2027. .version = IPVS_GENL_VERSION,
  2028. .maxattr = IPVS_CMD_MAX,
  2029. };
  2030. /* Policy used for first-level command attributes */
  2031. static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
  2032. [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
  2033. [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
  2034. [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
  2035. [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
  2036. [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
  2037. [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
  2038. };
  2039. /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
  2040. static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
  2041. [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
  2042. [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
  2043. .len = IP_VS_IFNAME_MAXLEN },
  2044. [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
  2045. };
  2046. /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
  2047. static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
  2048. [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
  2049. [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
  2050. [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
  2051. .len = sizeof(union nf_inet_addr) },
  2052. [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
  2053. [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
  2054. [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
  2055. .len = IP_VS_SCHEDNAME_MAXLEN },
  2056. [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
  2057. .len = sizeof(struct ip_vs_flags) },
  2058. [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
  2059. [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
  2060. [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
  2061. };
  2062. /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
  2063. static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
  2064. [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
  2065. .len = sizeof(union nf_inet_addr) },
  2066. [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
  2067. [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
  2068. [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
  2069. [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
  2070. [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
  2071. [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
  2072. [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
  2073. [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
  2074. [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
  2075. };
  2076. static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
  2077. struct ip_vs_stats *stats)
  2078. {
  2079. struct nlattr *nl_stats = nla_nest_start(skb, container_type);
  2080. if (!nl_stats)
  2081. return -EMSGSIZE;
  2082. spin_lock_bh(&stats->lock);
  2083. NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->conns);
  2084. NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->inpkts);
  2085. NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->outpkts);
  2086. NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->inbytes);
  2087. NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->outbytes);
  2088. NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->cps);
  2089. NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->inpps);
  2090. NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->outpps);
  2091. NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->inbps);
  2092. NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->outbps);
  2093. spin_unlock_bh(&stats->lock);
  2094. nla_nest_end(skb, nl_stats);
  2095. return 0;
  2096. nla_put_failure:
  2097. spin_unlock_bh(&stats->lock);
  2098. nla_nest_cancel(skb, nl_stats);
  2099. return -EMSGSIZE;
  2100. }
  2101. static int ip_vs_genl_fill_service(struct sk_buff *skb,
  2102. struct ip_vs_service *svc)
  2103. {
  2104. struct nlattr *nl_service;
  2105. struct ip_vs_flags flags = { .flags = svc->flags,
  2106. .mask = ~0 };
  2107. nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
  2108. if (!nl_service)
  2109. return -EMSGSIZE;
  2110. NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, AF_INET);
  2111. if (svc->fwmark) {
  2112. NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
  2113. } else {
  2114. NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
  2115. NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
  2116. NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
  2117. }
  2118. NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
  2119. NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
  2120. NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
  2121. NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
  2122. if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
  2123. goto nla_put_failure;
  2124. nla_nest_end(skb, nl_service);
  2125. return 0;
  2126. nla_put_failure:
  2127. nla_nest_cancel(skb, nl_service);
  2128. return -EMSGSIZE;
  2129. }
  2130. static int ip_vs_genl_dump_service(struct sk_buff *skb,
  2131. struct ip_vs_service *svc,
  2132. struct netlink_callback *cb)
  2133. {
  2134. void *hdr;
  2135. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  2136. &ip_vs_genl_family, NLM_F_MULTI,
  2137. IPVS_CMD_NEW_SERVICE);
  2138. if (!hdr)
  2139. return -EMSGSIZE;
  2140. if (ip_vs_genl_fill_service(skb, svc) < 0)
  2141. goto nla_put_failure;
  2142. return genlmsg_end(skb, hdr);
  2143. nla_put_failure:
  2144. genlmsg_cancel(skb, hdr);
  2145. return -EMSGSIZE;
  2146. }
  2147. static int ip_vs_genl_dump_services(struct sk_buff *skb,
  2148. struct netlink_callback *cb)
  2149. {
  2150. int idx = 0, i;
  2151. int start = cb->args[0];
  2152. struct ip_vs_service *svc;
  2153. mutex_lock(&__ip_vs_mutex);
  2154. for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
  2155. list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
  2156. if (++idx <= start)
  2157. continue;
  2158. if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
  2159. idx--;
  2160. goto nla_put_failure;
  2161. }
  2162. }
  2163. }
  2164. for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
  2165. list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
  2166. if (++idx <= start)
  2167. continue;
  2168. if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
  2169. idx--;
  2170. goto nla_put_failure;
  2171. }
  2172. }
  2173. }
  2174. nla_put_failure:
  2175. mutex_unlock(&__ip_vs_mutex);
  2176. cb->args[0] = idx;
  2177. return skb->len;
  2178. }
  2179. static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
  2180. struct nlattr *nla, int full_entry)
  2181. {
  2182. struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
  2183. struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
  2184. /* Parse mandatory identifying service fields first */
  2185. if (nla == NULL ||
  2186. nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
  2187. return -EINVAL;
  2188. nla_af = attrs[IPVS_SVC_ATTR_AF];
  2189. nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
  2190. nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
  2191. nla_port = attrs[IPVS_SVC_ATTR_PORT];
  2192. nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
  2193. if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
  2194. return -EINVAL;
  2195. usvc->af = nla_get_u16(nla_af);
  2196. /* For now, only support IPv4 */
  2197. if (nla_get_u16(nla_af) != AF_INET)
  2198. return -EAFNOSUPPORT;
  2199. if (nla_fwmark) {
  2200. usvc->protocol = IPPROTO_TCP;
  2201. usvc->fwmark = nla_get_u32(nla_fwmark);
  2202. } else {
  2203. usvc->protocol = nla_get_u16(nla_protocol);
  2204. nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
  2205. usvc->port = nla_get_u16(nla_port);
  2206. usvc->fwmark = 0;
  2207. }
  2208. /* If a full entry was requested, check for the additional fields */
  2209. if (full_entry) {
  2210. struct nlattr *nla_sched, *nla_flags, *nla_timeout,
  2211. *nla_netmask;
  2212. struct ip_vs_flags flags;
  2213. struct ip_vs_service *svc;
  2214. nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
  2215. nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
  2216. nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
  2217. nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
  2218. if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
  2219. return -EINVAL;
  2220. nla_memcpy(&flags, nla_flags, sizeof(flags));
  2221. /* prefill flags from service if it already exists */
  2222. if (usvc->fwmark)
  2223. svc = __ip_vs_svc_fwm_get(usvc->fwmark);
  2224. else
  2225. svc = __ip_vs_service_get(usvc->protocol, usvc->addr.ip,
  2226. usvc->port);
  2227. if (svc) {
  2228. usvc->flags = svc->flags;
  2229. ip_vs_service_put(svc);
  2230. } else
  2231. usvc->flags = 0;
  2232. /* set new flags from userland */
  2233. usvc->flags = (usvc->flags & ~flags.mask) |
  2234. (flags.flags & flags.mask);
  2235. usvc->sched_name = nla_data(nla_sched);
  2236. usvc->timeout = nla_get_u32(nla_timeout);
  2237. usvc->netmask = nla_get_u32(nla_netmask);
  2238. }
  2239. return 0;
  2240. }
  2241. static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
  2242. {
  2243. struct ip_vs_service_user_kern usvc;
  2244. int ret;
  2245. ret = ip_vs_genl_parse_service(&usvc, nla, 0);
  2246. if (ret)
  2247. return ERR_PTR(ret);
  2248. if (usvc.fwmark)
  2249. return __ip_vs_svc_fwm_get(usvc.fwmark);
  2250. else
  2251. return __ip_vs_service_get(usvc.protocol, usvc.addr.ip,
  2252. usvc.port);
  2253. }
  2254. static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
  2255. {
  2256. struct nlattr *nl_dest;
  2257. nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
  2258. if (!nl_dest)
  2259. return -EMSGSIZE;
  2260. NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
  2261. NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
  2262. NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
  2263. atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
  2264. NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
  2265. NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
  2266. NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
  2267. NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
  2268. atomic_read(&dest->activeconns));
  2269. NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
  2270. atomic_read(&dest->inactconns));
  2271. NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
  2272. atomic_read(&dest->persistconns));
  2273. if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
  2274. goto nla_put_failure;
  2275. nla_nest_end(skb, nl_dest);
  2276. return 0;
  2277. nla_put_failure:
  2278. nla_nest_cancel(skb, nl_dest);
  2279. return -EMSGSIZE;
  2280. }
  2281. static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
  2282. struct netlink_callback *cb)
  2283. {
  2284. void *hdr;
  2285. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  2286. &ip_vs_genl_family, NLM_F_MULTI,
  2287. IPVS_CMD_NEW_DEST);
  2288. if (!hdr)
  2289. return -EMSGSIZE;
  2290. if (ip_vs_genl_fill_dest(skb, dest) < 0)
  2291. goto nla_put_failure;
  2292. return genlmsg_end(skb, hdr);
  2293. nla_put_failure:
  2294. genlmsg_cancel(skb, hdr);
  2295. return -EMSGSIZE;
  2296. }
  2297. static int ip_vs_genl_dump_dests(struct sk_buff *skb,
  2298. struct netlink_callback *cb)
  2299. {
  2300. int idx = 0;
  2301. int start = cb->args[0];
  2302. struct ip_vs_service *svc;
  2303. struct ip_vs_dest *dest;
  2304. struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
  2305. mutex_lock(&__ip_vs_mutex);
  2306. /* Try to find the service for which to dump destinations */
  2307. if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
  2308. IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
  2309. goto out_err;
  2310. svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
  2311. if (IS_ERR(svc) || svc == NULL)
  2312. goto out_err;
  2313. /* Dump the destinations */
  2314. list_for_each_entry(dest, &svc->destinations, n_list) {
  2315. if (++idx <= start)
  2316. continue;
  2317. if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
  2318. idx--;
  2319. goto nla_put_failure;
  2320. }
  2321. }
  2322. nla_put_failure:
  2323. cb->args[0] = idx;
  2324. ip_vs_service_put(svc);
  2325. out_err:
  2326. mutex_unlock(&__ip_vs_mutex);
  2327. return skb->len;
  2328. }
  2329. static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
  2330. struct nlattr *nla, int full_entry)
  2331. {
  2332. struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
  2333. struct nlattr *nla_addr, *nla_port;
  2334. /* Parse mandatory identifying destination fields first */
  2335. if (nla == NULL ||
  2336. nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
  2337. return -EINVAL;
  2338. nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
  2339. nla_port = attrs[IPVS_DEST_ATTR_PORT];
  2340. if (!(nla_addr && nla_port))
  2341. return -EINVAL;
  2342. nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
  2343. udest->port = nla_get_u16(nla_port);
  2344. /* If a full entry was requested, check for the additional fields */
  2345. if (full_entry) {
  2346. struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
  2347. *nla_l_thresh;
  2348. nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
  2349. nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
  2350. nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
  2351. nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
  2352. if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
  2353. return -EINVAL;
  2354. udest->conn_flags = nla_get_u32(nla_fwd)
  2355. & IP_VS_CONN_F_FWD_MASK;
  2356. udest->weight = nla_get_u32(nla_weight);
  2357. udest->u_threshold = nla_get_u32(nla_u_thresh);
  2358. udest->l_threshold = nla_get_u32(nla_l_thresh);
  2359. }
  2360. return 0;
  2361. }
  2362. static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
  2363. const char *mcast_ifn, __be32 syncid)
  2364. {
  2365. struct nlattr *nl_daemon;
  2366. nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
  2367. if (!nl_daemon)
  2368. return -EMSGSIZE;
  2369. NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
  2370. NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
  2371. NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
  2372. nla_nest_end(skb, nl_daemon);
  2373. return 0;
  2374. nla_put_failure:
  2375. nla_nest_cancel(skb, nl_daemon);
  2376. return -EMSGSIZE;
  2377. }
  2378. static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
  2379. const char *mcast_ifn, __be32 syncid,
  2380. struct netlink_callback *cb)
  2381. {
  2382. void *hdr;
  2383. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  2384. &ip_vs_genl_family, NLM_F_MULTI,
  2385. IPVS_CMD_NEW_DAEMON);
  2386. if (!hdr)
  2387. return -EMSGSIZE;
  2388. if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
  2389. goto nla_put_failure;
  2390. return genlmsg_end(skb, hdr);
  2391. nla_put_failure:
  2392. genlmsg_cancel(skb, hdr);
  2393. return -EMSGSIZE;
  2394. }
  2395. static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
  2396. struct netlink_callback *cb)
  2397. {
  2398. mutex_lock(&__ip_vs_mutex);
  2399. if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
  2400. if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
  2401. ip_vs_master_mcast_ifn,
  2402. ip_vs_master_syncid, cb) < 0)
  2403. goto nla_put_failure;
  2404. cb->args[0] = 1;
  2405. }
  2406. if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
  2407. if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
  2408. ip_vs_backup_mcast_ifn,
  2409. ip_vs_backup_syncid, cb) < 0)
  2410. goto nla_put_failure;
  2411. cb->args[1] = 1;
  2412. }
  2413. nla_put_failure:
  2414. mutex_unlock(&__ip_vs_mutex);
  2415. return skb->len;
  2416. }
  2417. static int ip_vs_genl_new_daemon(struct nlattr **attrs)
  2418. {
  2419. if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
  2420. attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
  2421. attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
  2422. return -EINVAL;
  2423. return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
  2424. nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
  2425. nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
  2426. }
  2427. static int ip_vs_genl_del_daemon(struct nlattr **attrs)
  2428. {
  2429. if (!attrs[IPVS_DAEMON_ATTR_STATE])
  2430. return -EINVAL;
  2431. return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
  2432. }
  2433. static int ip_vs_genl_set_config(struct nlattr **attrs)
  2434. {
  2435. struct ip_vs_timeout_user t;
  2436. __ip_vs_get_timeouts(&t);
  2437. if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
  2438. t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
  2439. if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
  2440. t.tcp_fin_timeout =
  2441. nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
  2442. if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
  2443. t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
  2444. return ip_vs_set_timeout(&t);
  2445. }
  2446. static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
  2447. {
  2448. struct ip_vs_service *svc = NULL;
  2449. struct ip_vs_service_user_kern usvc;
  2450. struct ip_vs_dest_user_kern udest;
  2451. int ret = 0, cmd;
  2452. int need_full_svc = 0, need_full_dest = 0;
  2453. cmd = info->genlhdr->cmd;
  2454. mutex_lock(&__ip_vs_mutex);
  2455. if (cmd == IPVS_CMD_FLUSH) {
  2456. ret = ip_vs_flush();
  2457. goto out;
  2458. } else if (cmd == IPVS_CMD_SET_CONFIG) {
  2459. ret = ip_vs_genl_set_config(info->attrs);
  2460. goto out;
  2461. } else if (cmd == IPVS_CMD_NEW_DAEMON ||
  2462. cmd == IPVS_CMD_DEL_DAEMON) {
  2463. struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
  2464. if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
  2465. nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
  2466. info->attrs[IPVS_CMD_ATTR_DAEMON],
  2467. ip_vs_daemon_policy)) {
  2468. ret = -EINVAL;
  2469. goto out;
  2470. }
  2471. if (cmd == IPVS_CMD_NEW_DAEMON)
  2472. ret = ip_vs_genl_new_daemon(daemon_attrs);
  2473. else
  2474. ret = ip_vs_genl_del_daemon(daemon_attrs);
  2475. goto out;
  2476. } else if (cmd == IPVS_CMD_ZERO &&
  2477. !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
  2478. ret = ip_vs_zero_all();
  2479. goto out;
  2480. }
  2481. /* All following commands require a service argument, so check if we
  2482. * received a valid one. We need a full service specification when
  2483. * adding / editing a service. Only identifying members otherwise. */
  2484. if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
  2485. need_full_svc = 1;
  2486. ret = ip_vs_genl_parse_service(&usvc,
  2487. info->attrs[IPVS_CMD_ATTR_SERVICE],
  2488. need_full_svc);
  2489. if (ret)
  2490. goto out;
  2491. /* Lookup the exact service by <protocol, addr, port> or fwmark */
  2492. if (usvc.fwmark == 0)
  2493. svc = __ip_vs_service_get(usvc.protocol, usvc.addr.ip,
  2494. usvc.port);
  2495. else
  2496. svc = __ip_vs_svc_fwm_get(usvc.fwmark);
  2497. /* Unless we're adding a new service, the service must already exist */
  2498. if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
  2499. ret = -ESRCH;
  2500. goto out;
  2501. }
  2502. /* Destination commands require a valid destination argument. For
  2503. * adding / editing a destination, we need a full destination
  2504. * specification. */
  2505. if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
  2506. cmd == IPVS_CMD_DEL_DEST) {
  2507. if (cmd != IPVS_CMD_DEL_DEST)
  2508. need_full_dest = 1;
  2509. ret = ip_vs_genl_parse_dest(&udest,
  2510. info->attrs[IPVS_CMD_ATTR_DEST],
  2511. need_full_dest);
  2512. if (ret)
  2513. goto out;
  2514. }
  2515. switch (cmd) {
  2516. case IPVS_CMD_NEW_SERVICE:
  2517. if (svc == NULL)
  2518. ret = ip_vs_add_service(&usvc, &svc);
  2519. else
  2520. ret = -EEXIST;
  2521. break;
  2522. case IPVS_CMD_SET_SERVICE:
  2523. ret = ip_vs_edit_service(svc, &usvc);
  2524. break;
  2525. case IPVS_CMD_DEL_SERVICE:
  2526. ret = ip_vs_del_service(svc);
  2527. break;
  2528. case IPVS_CMD_NEW_DEST:
  2529. ret = ip_vs_add_dest(svc, &udest);
  2530. break;
  2531. case IPVS_CMD_SET_DEST:
  2532. ret = ip_vs_edit_dest(svc, &udest);
  2533. break;
  2534. case IPVS_CMD_DEL_DEST:
  2535. ret = ip_vs_del_dest(svc, &udest);
  2536. break;
  2537. case IPVS_CMD_ZERO:
  2538. ret = ip_vs_zero_service(svc);
  2539. break;
  2540. default:
  2541. ret = -EINVAL;
  2542. }
  2543. out:
  2544. if (svc)
  2545. ip_vs_service_put(svc);
  2546. mutex_unlock(&__ip_vs_mutex);
  2547. return ret;
  2548. }
  2549. static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
  2550. {
  2551. struct sk_buff *msg;
  2552. void *reply;
  2553. int ret, cmd, reply_cmd;
  2554. cmd = info->genlhdr->cmd;
  2555. if (cmd == IPVS_CMD_GET_SERVICE)
  2556. reply_cmd = IPVS_CMD_NEW_SERVICE;
  2557. else if (cmd == IPVS_CMD_GET_INFO)
  2558. reply_cmd = IPVS_CMD_SET_INFO;
  2559. else if (cmd == IPVS_CMD_GET_CONFIG)
  2560. reply_cmd = IPVS_CMD_SET_CONFIG;
  2561. else {
  2562. IP_VS_ERR("unknown Generic Netlink command\n");
  2563. return -EINVAL;
  2564. }
  2565. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2566. if (!msg)
  2567. return -ENOMEM;
  2568. mutex_lock(&__ip_vs_mutex);
  2569. reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
  2570. if (reply == NULL)
  2571. goto nla_put_failure;
  2572. switch (cmd) {
  2573. case IPVS_CMD_GET_SERVICE:
  2574. {
  2575. struct ip_vs_service *svc;
  2576. svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
  2577. if (IS_ERR(svc)) {
  2578. ret = PTR_ERR(svc);
  2579. goto out_err;
  2580. } else if (svc) {
  2581. ret = ip_vs_genl_fill_service(msg, svc);
  2582. ip_vs_service_put(svc);
  2583. if (ret)
  2584. goto nla_put_failure;
  2585. } else {
  2586. ret = -ESRCH;
  2587. goto out_err;
  2588. }
  2589. break;
  2590. }
  2591. case IPVS_CMD_GET_CONFIG:
  2592. {
  2593. struct ip_vs_timeout_user t;
  2594. __ip_vs_get_timeouts(&t);
  2595. #ifdef CONFIG_IP_VS_PROTO_TCP
  2596. NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
  2597. NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
  2598. t.tcp_fin_timeout);
  2599. #endif
  2600. #ifdef CONFIG_IP_VS_PROTO_UDP
  2601. NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
  2602. #endif
  2603. break;
  2604. }
  2605. case IPVS_CMD_GET_INFO:
  2606. NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
  2607. NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
  2608. IP_VS_CONN_TAB_SIZE);
  2609. break;
  2610. }
  2611. genlmsg_end(msg, reply);
  2612. ret = genlmsg_unicast(msg, info->snd_pid);
  2613. goto out;
  2614. nla_put_failure:
  2615. IP_VS_ERR("not enough space in Netlink message\n");
  2616. ret = -EMSGSIZE;
  2617. out_err:
  2618. nlmsg_free(msg);
  2619. out:
  2620. mutex_unlock(&__ip_vs_mutex);
  2621. return ret;
  2622. }
  2623. static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
  2624. {
  2625. .cmd = IPVS_CMD_NEW_SERVICE,
  2626. .flags = GENL_ADMIN_PERM,
  2627. .policy = ip_vs_cmd_policy,
  2628. .doit = ip_vs_genl_set_cmd,
  2629. },
  2630. {
  2631. .cmd = IPVS_CMD_SET_SERVICE,
  2632. .flags = GENL_ADMIN_PERM,
  2633. .policy = ip_vs_cmd_policy,
  2634. .doit = ip_vs_genl_set_cmd,
  2635. },
  2636. {
  2637. .cmd = IPVS_CMD_DEL_SERVICE,
  2638. .flags = GENL_ADMIN_PERM,
  2639. .policy = ip_vs_cmd_policy,
  2640. .doit = ip_vs_genl_set_cmd,
  2641. },
  2642. {
  2643. .cmd = IPVS_CMD_GET_SERVICE,
  2644. .flags = GENL_ADMIN_PERM,
  2645. .doit = ip_vs_genl_get_cmd,
  2646. .dumpit = ip_vs_genl_dump_services,
  2647. .policy = ip_vs_cmd_policy,
  2648. },
  2649. {
  2650. .cmd = IPVS_CMD_NEW_DEST,
  2651. .flags = GENL_ADMIN_PERM,
  2652. .policy = ip_vs_cmd_policy,
  2653. .doit = ip_vs_genl_set_cmd,
  2654. },
  2655. {
  2656. .cmd = IPVS_CMD_SET_DEST,
  2657. .flags = GENL_ADMIN_PERM,
  2658. .policy = ip_vs_cmd_policy,
  2659. .doit = ip_vs_genl_set_cmd,
  2660. },
  2661. {
  2662. .cmd = IPVS_CMD_DEL_DEST,
  2663. .flags = GENL_ADMIN_PERM,
  2664. .policy = ip_vs_cmd_policy,
  2665. .doit = ip_vs_genl_set_cmd,
  2666. },
  2667. {
  2668. .cmd = IPVS_CMD_GET_DEST,
  2669. .flags = GENL_ADMIN_PERM,
  2670. .policy = ip_vs_cmd_policy,
  2671. .dumpit = ip_vs_genl_dump_dests,
  2672. },
  2673. {
  2674. .cmd = IPVS_CMD_NEW_DAEMON,
  2675. .flags = GENL_ADMIN_PERM,
  2676. .policy = ip_vs_cmd_policy,
  2677. .doit = ip_vs_genl_set_cmd,
  2678. },
  2679. {
  2680. .cmd = IPVS_CMD_DEL_DAEMON,
  2681. .flags = GENL_ADMIN_PERM,
  2682. .policy = ip_vs_cmd_policy,
  2683. .doit = ip_vs_genl_set_cmd,
  2684. },
  2685. {
  2686. .cmd = IPVS_CMD_GET_DAEMON,
  2687. .flags = GENL_ADMIN_PERM,
  2688. .dumpit = ip_vs_genl_dump_daemons,
  2689. },
  2690. {
  2691. .cmd = IPVS_CMD_SET_CONFIG,
  2692. .flags = GENL_ADMIN_PERM,
  2693. .policy = ip_vs_cmd_policy,
  2694. .doit = ip_vs_genl_set_cmd,
  2695. },
  2696. {
  2697. .cmd = IPVS_CMD_GET_CONFIG,
  2698. .flags = GENL_ADMIN_PERM,
  2699. .doit = ip_vs_genl_get_cmd,
  2700. },
  2701. {
  2702. .cmd = IPVS_CMD_GET_INFO,
  2703. .flags = GENL_ADMIN_PERM,
  2704. .doit = ip_vs_genl_get_cmd,
  2705. },
  2706. {
  2707. .cmd = IPVS_CMD_ZERO,
  2708. .flags = GENL_ADMIN_PERM,
  2709. .policy = ip_vs_cmd_policy,
  2710. .doit = ip_vs_genl_set_cmd,
  2711. },
  2712. {
  2713. .cmd = IPVS_CMD_FLUSH,
  2714. .flags = GENL_ADMIN_PERM,
  2715. .doit = ip_vs_genl_set_cmd,
  2716. },
  2717. };
  2718. static int __init ip_vs_genl_register(void)
  2719. {
  2720. int ret, i;
  2721. ret = genl_register_family(&ip_vs_genl_family);
  2722. if (ret)
  2723. return ret;
  2724. for (i = 0; i < ARRAY_SIZE(ip_vs_genl_ops); i++) {
  2725. ret = genl_register_ops(&ip_vs_genl_family, &ip_vs_genl_ops[i]);
  2726. if (ret)
  2727. goto err_out;
  2728. }
  2729. return 0;
  2730. err_out:
  2731. genl_unregister_family(&ip_vs_genl_family);
  2732. return ret;
  2733. }
  2734. static void ip_vs_genl_unregister(void)
  2735. {
  2736. genl_unregister_family(&ip_vs_genl_family);
  2737. }
  2738. /* End of Generic Netlink interface definitions */
  2739. int __init ip_vs_control_init(void)
  2740. {
  2741. int ret;
  2742. int idx;
  2743. EnterFunction(2);
  2744. ret = nf_register_sockopt(&ip_vs_sockopts);
  2745. if (ret) {
  2746. IP_VS_ERR("cannot register sockopt.\n");
  2747. return ret;
  2748. }
  2749. ret = ip_vs_genl_register();
  2750. if (ret) {
  2751. IP_VS_ERR("cannot register Generic Netlink interface.\n");
  2752. nf_unregister_sockopt(&ip_vs_sockopts);
  2753. return ret;
  2754. }
  2755. proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
  2756. proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
  2757. sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
  2758. /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
  2759. for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
  2760. INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
  2761. INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
  2762. }
  2763. for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
  2764. INIT_LIST_HEAD(&ip_vs_rtable[idx]);
  2765. }
  2766. ip_vs_new_estimator(&ip_vs_stats);
  2767. /* Hook the defense timer */
  2768. schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
  2769. LeaveFunction(2);
  2770. return 0;
  2771. }
  2772. void ip_vs_control_cleanup(void)
  2773. {
  2774. EnterFunction(2);
  2775. ip_vs_trash_cleanup();
  2776. cancel_rearming_delayed_work(&defense_work);
  2777. cancel_work_sync(&defense_work.work);
  2778. ip_vs_kill_estimator(&ip_vs_stats);
  2779. unregister_sysctl_table(sysctl_header);
  2780. proc_net_remove(&init_net, "ip_vs_stats");
  2781. proc_net_remove(&init_net, "ip_vs");
  2782. ip_vs_genl_unregister();
  2783. nf_unregister_sockopt(&ip_vs_sockopts);
  2784. LeaveFunction(2);
  2785. }