af_netlink.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. * Patrick McHardy <kaber@trash.net>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  14. * added netlink_proto_exit
  15. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  16. * use nlk_sk, as sk->protinfo is on a diet 8)
  17. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  18. * - inc module use count of module that owns
  19. * the kernel socket in case userspace opens
  20. * socket of same protocol
  21. * - remove all module support, since netlink is
  22. * mandatory if CONFIG_NET=y these days
  23. */
  24. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/signal.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/stat.h>
  33. #include <linux/socket.h>
  34. #include <linux/un.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/termios.h>
  37. #include <linux/sockios.h>
  38. #include <linux/net.h>
  39. #include <linux/fs.h>
  40. #include <linux/slab.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/rtnetlink.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/notifier.h>
  48. #include <linux/security.h>
  49. #include <linux/jhash.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/random.h>
  52. #include <linux/bitops.h>
  53. #include <linux/mm.h>
  54. #include <linux/types.h>
  55. #include <linux/audit.h>
  56. #include <linux/mutex.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/if_arp.h>
  59. #include <linux/rhashtable.h>
  60. #include <asm/cacheflush.h>
  61. #include <linux/hash.h>
  62. #include <net/net_namespace.h>
  63. #include <net/sock.h>
  64. #include <net/scm.h>
  65. #include <net/netlink.h>
  66. #include "af_netlink.h"
  67. struct listeners {
  68. struct rcu_head rcu;
  69. unsigned long masks[0];
  70. };
  71. /* state bits */
  72. #define NETLINK_CONGESTED 0x0
  73. /* flags */
  74. #define NETLINK_KERNEL_SOCKET 0x1
  75. #define NETLINK_RECV_PKTINFO 0x2
  76. #define NETLINK_BROADCAST_SEND_ERROR 0x4
  77. #define NETLINK_RECV_NO_ENOBUFS 0x8
  78. static inline int netlink_is_kernel(struct sock *sk)
  79. {
  80. return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
  81. }
  82. struct netlink_table *nl_table;
  83. EXPORT_SYMBOL_GPL(nl_table);
  84. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  85. static int netlink_dump(struct sock *sk);
  86. static void netlink_skb_destructor(struct sk_buff *skb);
  87. /* nl_table locking explained:
  88. * Lookup and traversal are protected with nl_sk_hash_lock or nl_table_lock
  89. * combined with an RCU read-side lock. Insertion and removal are protected
  90. * with nl_sk_hash_lock while using RCU list modification primitives and may
  91. * run in parallel to nl_table_lock protected lookups. Destruction of the
  92. * Netlink socket may only occur *after* nl_table_lock has been acquired
  93. * either during or after the socket has been removed from the list.
  94. */
  95. DEFINE_RWLOCK(nl_table_lock);
  96. EXPORT_SYMBOL_GPL(nl_table_lock);
  97. static atomic_t nl_table_users = ATOMIC_INIT(0);
  98. #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
  99. /* Protects netlink socket hash table mutations */
  100. DEFINE_MUTEX(nl_sk_hash_lock);
  101. EXPORT_SYMBOL_GPL(nl_sk_hash_lock);
  102. static int lockdep_nl_sk_hash_is_held(void)
  103. {
  104. #ifdef CONFIG_LOCKDEP
  105. if (debug_locks)
  106. return lockdep_is_held(&nl_sk_hash_lock) || lockdep_is_held(&nl_table_lock);
  107. #endif
  108. return 1;
  109. }
  110. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  111. static DEFINE_SPINLOCK(netlink_tap_lock);
  112. static struct list_head netlink_tap_all __read_mostly;
  113. static inline u32 netlink_group_mask(u32 group)
  114. {
  115. return group ? 1 << (group - 1) : 0;
  116. }
  117. int netlink_add_tap(struct netlink_tap *nt)
  118. {
  119. if (unlikely(nt->dev->type != ARPHRD_NETLINK))
  120. return -EINVAL;
  121. spin_lock(&netlink_tap_lock);
  122. list_add_rcu(&nt->list, &netlink_tap_all);
  123. spin_unlock(&netlink_tap_lock);
  124. if (nt->module)
  125. __module_get(nt->module);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(netlink_add_tap);
  129. static int __netlink_remove_tap(struct netlink_tap *nt)
  130. {
  131. bool found = false;
  132. struct netlink_tap *tmp;
  133. spin_lock(&netlink_tap_lock);
  134. list_for_each_entry(tmp, &netlink_tap_all, list) {
  135. if (nt == tmp) {
  136. list_del_rcu(&nt->list);
  137. found = true;
  138. goto out;
  139. }
  140. }
  141. pr_warn("__netlink_remove_tap: %p not found\n", nt);
  142. out:
  143. spin_unlock(&netlink_tap_lock);
  144. if (found && nt->module)
  145. module_put(nt->module);
  146. return found ? 0 : -ENODEV;
  147. }
  148. int netlink_remove_tap(struct netlink_tap *nt)
  149. {
  150. int ret;
  151. ret = __netlink_remove_tap(nt);
  152. synchronize_net();
  153. return ret;
  154. }
  155. EXPORT_SYMBOL_GPL(netlink_remove_tap);
  156. static bool netlink_filter_tap(const struct sk_buff *skb)
  157. {
  158. struct sock *sk = skb->sk;
  159. /* We take the more conservative approach and
  160. * whitelist socket protocols that may pass.
  161. */
  162. switch (sk->sk_protocol) {
  163. case NETLINK_ROUTE:
  164. case NETLINK_USERSOCK:
  165. case NETLINK_SOCK_DIAG:
  166. case NETLINK_NFLOG:
  167. case NETLINK_XFRM:
  168. case NETLINK_FIB_LOOKUP:
  169. case NETLINK_NETFILTER:
  170. case NETLINK_GENERIC:
  171. return true;
  172. }
  173. return false;
  174. }
  175. static int __netlink_deliver_tap_skb(struct sk_buff *skb,
  176. struct net_device *dev)
  177. {
  178. struct sk_buff *nskb;
  179. struct sock *sk = skb->sk;
  180. int ret = -ENOMEM;
  181. dev_hold(dev);
  182. nskb = skb_clone(skb, GFP_ATOMIC);
  183. if (nskb) {
  184. nskb->dev = dev;
  185. nskb->protocol = htons((u16) sk->sk_protocol);
  186. nskb->pkt_type = netlink_is_kernel(sk) ?
  187. PACKET_KERNEL : PACKET_USER;
  188. skb_reset_network_header(nskb);
  189. ret = dev_queue_xmit(nskb);
  190. if (unlikely(ret > 0))
  191. ret = net_xmit_errno(ret);
  192. }
  193. dev_put(dev);
  194. return ret;
  195. }
  196. static void __netlink_deliver_tap(struct sk_buff *skb)
  197. {
  198. int ret;
  199. struct netlink_tap *tmp;
  200. if (!netlink_filter_tap(skb))
  201. return;
  202. list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
  203. ret = __netlink_deliver_tap_skb(skb, tmp->dev);
  204. if (unlikely(ret))
  205. break;
  206. }
  207. }
  208. static void netlink_deliver_tap(struct sk_buff *skb)
  209. {
  210. rcu_read_lock();
  211. if (unlikely(!list_empty(&netlink_tap_all)))
  212. __netlink_deliver_tap(skb);
  213. rcu_read_unlock();
  214. }
  215. static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
  216. struct sk_buff *skb)
  217. {
  218. if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
  219. netlink_deliver_tap(skb);
  220. }
  221. static void netlink_overrun(struct sock *sk)
  222. {
  223. struct netlink_sock *nlk = nlk_sk(sk);
  224. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  225. if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
  226. sk->sk_err = ENOBUFS;
  227. sk->sk_error_report(sk);
  228. }
  229. }
  230. atomic_inc(&sk->sk_drops);
  231. }
  232. static void netlink_rcv_wake(struct sock *sk)
  233. {
  234. struct netlink_sock *nlk = nlk_sk(sk);
  235. if (skb_queue_empty(&sk->sk_receive_queue))
  236. clear_bit(NETLINK_CONGESTED, &nlk->state);
  237. if (!test_bit(NETLINK_CONGESTED, &nlk->state))
  238. wake_up_interruptible(&nlk->wait);
  239. }
  240. #ifdef CONFIG_NETLINK_MMAP
  241. static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
  242. {
  243. return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
  244. }
  245. static bool netlink_rx_is_mmaped(struct sock *sk)
  246. {
  247. return nlk_sk(sk)->rx_ring.pg_vec != NULL;
  248. }
  249. static bool netlink_tx_is_mmaped(struct sock *sk)
  250. {
  251. return nlk_sk(sk)->tx_ring.pg_vec != NULL;
  252. }
  253. static __pure struct page *pgvec_to_page(const void *addr)
  254. {
  255. if (is_vmalloc_addr(addr))
  256. return vmalloc_to_page(addr);
  257. else
  258. return virt_to_page(addr);
  259. }
  260. static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
  261. {
  262. unsigned int i;
  263. for (i = 0; i < len; i++) {
  264. if (pg_vec[i] != NULL) {
  265. if (is_vmalloc_addr(pg_vec[i]))
  266. vfree(pg_vec[i]);
  267. else
  268. free_pages((unsigned long)pg_vec[i], order);
  269. }
  270. }
  271. kfree(pg_vec);
  272. }
  273. static void *alloc_one_pg_vec_page(unsigned long order)
  274. {
  275. void *buffer;
  276. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
  277. __GFP_NOWARN | __GFP_NORETRY;
  278. buffer = (void *)__get_free_pages(gfp_flags, order);
  279. if (buffer != NULL)
  280. return buffer;
  281. buffer = vzalloc((1 << order) * PAGE_SIZE);
  282. if (buffer != NULL)
  283. return buffer;
  284. gfp_flags &= ~__GFP_NORETRY;
  285. return (void *)__get_free_pages(gfp_flags, order);
  286. }
  287. static void **alloc_pg_vec(struct netlink_sock *nlk,
  288. struct nl_mmap_req *req, unsigned int order)
  289. {
  290. unsigned int block_nr = req->nm_block_nr;
  291. unsigned int i;
  292. void **pg_vec;
  293. pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
  294. if (pg_vec == NULL)
  295. return NULL;
  296. for (i = 0; i < block_nr; i++) {
  297. pg_vec[i] = alloc_one_pg_vec_page(order);
  298. if (pg_vec[i] == NULL)
  299. goto err1;
  300. }
  301. return pg_vec;
  302. err1:
  303. free_pg_vec(pg_vec, order, block_nr);
  304. return NULL;
  305. }
  306. static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
  307. bool closing, bool tx_ring)
  308. {
  309. struct netlink_sock *nlk = nlk_sk(sk);
  310. struct netlink_ring *ring;
  311. struct sk_buff_head *queue;
  312. void **pg_vec = NULL;
  313. unsigned int order = 0;
  314. int err;
  315. ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
  316. queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  317. if (!closing) {
  318. if (atomic_read(&nlk->mapped))
  319. return -EBUSY;
  320. if (atomic_read(&ring->pending))
  321. return -EBUSY;
  322. }
  323. if (req->nm_block_nr) {
  324. if (ring->pg_vec != NULL)
  325. return -EBUSY;
  326. if ((int)req->nm_block_size <= 0)
  327. return -EINVAL;
  328. if (!PAGE_ALIGNED(req->nm_block_size))
  329. return -EINVAL;
  330. if (req->nm_frame_size < NL_MMAP_HDRLEN)
  331. return -EINVAL;
  332. if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
  333. return -EINVAL;
  334. ring->frames_per_block = req->nm_block_size /
  335. req->nm_frame_size;
  336. if (ring->frames_per_block == 0)
  337. return -EINVAL;
  338. if (ring->frames_per_block * req->nm_block_nr !=
  339. req->nm_frame_nr)
  340. return -EINVAL;
  341. order = get_order(req->nm_block_size);
  342. pg_vec = alloc_pg_vec(nlk, req, order);
  343. if (pg_vec == NULL)
  344. return -ENOMEM;
  345. } else {
  346. if (req->nm_frame_nr)
  347. return -EINVAL;
  348. }
  349. err = -EBUSY;
  350. mutex_lock(&nlk->pg_vec_lock);
  351. if (closing || atomic_read(&nlk->mapped) == 0) {
  352. err = 0;
  353. spin_lock_bh(&queue->lock);
  354. ring->frame_max = req->nm_frame_nr - 1;
  355. ring->head = 0;
  356. ring->frame_size = req->nm_frame_size;
  357. ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
  358. swap(ring->pg_vec_len, req->nm_block_nr);
  359. swap(ring->pg_vec_order, order);
  360. swap(ring->pg_vec, pg_vec);
  361. __skb_queue_purge(queue);
  362. spin_unlock_bh(&queue->lock);
  363. WARN_ON(atomic_read(&nlk->mapped));
  364. }
  365. mutex_unlock(&nlk->pg_vec_lock);
  366. if (pg_vec)
  367. free_pg_vec(pg_vec, order, req->nm_block_nr);
  368. return err;
  369. }
  370. static void netlink_mm_open(struct vm_area_struct *vma)
  371. {
  372. struct file *file = vma->vm_file;
  373. struct socket *sock = file->private_data;
  374. struct sock *sk = sock->sk;
  375. if (sk)
  376. atomic_inc(&nlk_sk(sk)->mapped);
  377. }
  378. static void netlink_mm_close(struct vm_area_struct *vma)
  379. {
  380. struct file *file = vma->vm_file;
  381. struct socket *sock = file->private_data;
  382. struct sock *sk = sock->sk;
  383. if (sk)
  384. atomic_dec(&nlk_sk(sk)->mapped);
  385. }
  386. static const struct vm_operations_struct netlink_mmap_ops = {
  387. .open = netlink_mm_open,
  388. .close = netlink_mm_close,
  389. };
  390. static int netlink_mmap(struct file *file, struct socket *sock,
  391. struct vm_area_struct *vma)
  392. {
  393. struct sock *sk = sock->sk;
  394. struct netlink_sock *nlk = nlk_sk(sk);
  395. struct netlink_ring *ring;
  396. unsigned long start, size, expected;
  397. unsigned int i;
  398. int err = -EINVAL;
  399. if (vma->vm_pgoff)
  400. return -EINVAL;
  401. mutex_lock(&nlk->pg_vec_lock);
  402. expected = 0;
  403. for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
  404. if (ring->pg_vec == NULL)
  405. continue;
  406. expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
  407. }
  408. if (expected == 0)
  409. goto out;
  410. size = vma->vm_end - vma->vm_start;
  411. if (size != expected)
  412. goto out;
  413. start = vma->vm_start;
  414. for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
  415. if (ring->pg_vec == NULL)
  416. continue;
  417. for (i = 0; i < ring->pg_vec_len; i++) {
  418. struct page *page;
  419. void *kaddr = ring->pg_vec[i];
  420. unsigned int pg_num;
  421. for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
  422. page = pgvec_to_page(kaddr);
  423. err = vm_insert_page(vma, start, page);
  424. if (err < 0)
  425. goto out;
  426. start += PAGE_SIZE;
  427. kaddr += PAGE_SIZE;
  428. }
  429. }
  430. }
  431. atomic_inc(&nlk->mapped);
  432. vma->vm_ops = &netlink_mmap_ops;
  433. err = 0;
  434. out:
  435. mutex_unlock(&nlk->pg_vec_lock);
  436. return err;
  437. }
  438. static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
  439. {
  440. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  441. struct page *p_start, *p_end;
  442. /* First page is flushed through netlink_{get,set}_status */
  443. p_start = pgvec_to_page(hdr + PAGE_SIZE);
  444. p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
  445. while (p_start <= p_end) {
  446. flush_dcache_page(p_start);
  447. p_start++;
  448. }
  449. #endif
  450. }
  451. static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
  452. {
  453. smp_rmb();
  454. flush_dcache_page(pgvec_to_page(hdr));
  455. return hdr->nm_status;
  456. }
  457. static void netlink_set_status(struct nl_mmap_hdr *hdr,
  458. enum nl_mmap_status status)
  459. {
  460. hdr->nm_status = status;
  461. flush_dcache_page(pgvec_to_page(hdr));
  462. smp_wmb();
  463. }
  464. static struct nl_mmap_hdr *
  465. __netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
  466. {
  467. unsigned int pg_vec_pos, frame_off;
  468. pg_vec_pos = pos / ring->frames_per_block;
  469. frame_off = pos % ring->frames_per_block;
  470. return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
  471. }
  472. static struct nl_mmap_hdr *
  473. netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
  474. enum nl_mmap_status status)
  475. {
  476. struct nl_mmap_hdr *hdr;
  477. hdr = __netlink_lookup_frame(ring, pos);
  478. if (netlink_get_status(hdr) != status)
  479. return NULL;
  480. return hdr;
  481. }
  482. static struct nl_mmap_hdr *
  483. netlink_current_frame(const struct netlink_ring *ring,
  484. enum nl_mmap_status status)
  485. {
  486. return netlink_lookup_frame(ring, ring->head, status);
  487. }
  488. static struct nl_mmap_hdr *
  489. netlink_previous_frame(const struct netlink_ring *ring,
  490. enum nl_mmap_status status)
  491. {
  492. unsigned int prev;
  493. prev = ring->head ? ring->head - 1 : ring->frame_max;
  494. return netlink_lookup_frame(ring, prev, status);
  495. }
  496. static void netlink_increment_head(struct netlink_ring *ring)
  497. {
  498. ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
  499. }
  500. static void netlink_forward_ring(struct netlink_ring *ring)
  501. {
  502. unsigned int head = ring->head, pos = head;
  503. const struct nl_mmap_hdr *hdr;
  504. do {
  505. hdr = __netlink_lookup_frame(ring, pos);
  506. if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
  507. break;
  508. if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
  509. break;
  510. netlink_increment_head(ring);
  511. } while (ring->head != head);
  512. }
  513. static bool netlink_dump_space(struct netlink_sock *nlk)
  514. {
  515. struct netlink_ring *ring = &nlk->rx_ring;
  516. struct nl_mmap_hdr *hdr;
  517. unsigned int n;
  518. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  519. if (hdr == NULL)
  520. return false;
  521. n = ring->head + ring->frame_max / 2;
  522. if (n > ring->frame_max)
  523. n -= ring->frame_max;
  524. hdr = __netlink_lookup_frame(ring, n);
  525. return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
  526. }
  527. static unsigned int netlink_poll(struct file *file, struct socket *sock,
  528. poll_table *wait)
  529. {
  530. struct sock *sk = sock->sk;
  531. struct netlink_sock *nlk = nlk_sk(sk);
  532. unsigned int mask;
  533. int err;
  534. if (nlk->rx_ring.pg_vec != NULL) {
  535. /* Memory mapped sockets don't call recvmsg(), so flow control
  536. * for dumps is performed here. A dump is allowed to continue
  537. * if at least half the ring is unused.
  538. */
  539. while (nlk->cb_running && netlink_dump_space(nlk)) {
  540. err = netlink_dump(sk);
  541. if (err < 0) {
  542. sk->sk_err = -err;
  543. sk->sk_error_report(sk);
  544. break;
  545. }
  546. }
  547. netlink_rcv_wake(sk);
  548. }
  549. mask = datagram_poll(file, sock, wait);
  550. spin_lock_bh(&sk->sk_receive_queue.lock);
  551. if (nlk->rx_ring.pg_vec) {
  552. netlink_forward_ring(&nlk->rx_ring);
  553. if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
  554. mask |= POLLIN | POLLRDNORM;
  555. }
  556. spin_unlock_bh(&sk->sk_receive_queue.lock);
  557. spin_lock_bh(&sk->sk_write_queue.lock);
  558. if (nlk->tx_ring.pg_vec) {
  559. if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
  560. mask |= POLLOUT | POLLWRNORM;
  561. }
  562. spin_unlock_bh(&sk->sk_write_queue.lock);
  563. return mask;
  564. }
  565. static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
  566. {
  567. return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
  568. }
  569. static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
  570. struct netlink_ring *ring,
  571. struct nl_mmap_hdr *hdr)
  572. {
  573. unsigned int size;
  574. void *data;
  575. size = ring->frame_size - NL_MMAP_HDRLEN;
  576. data = (void *)hdr + NL_MMAP_HDRLEN;
  577. skb->head = data;
  578. skb->data = data;
  579. skb_reset_tail_pointer(skb);
  580. skb->end = skb->tail + size;
  581. skb->len = 0;
  582. skb->destructor = netlink_skb_destructor;
  583. NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
  584. NETLINK_CB(skb).sk = sk;
  585. }
  586. static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
  587. u32 dst_portid, u32 dst_group,
  588. struct sock_iocb *siocb)
  589. {
  590. struct netlink_sock *nlk = nlk_sk(sk);
  591. struct netlink_ring *ring;
  592. struct nl_mmap_hdr *hdr;
  593. struct sk_buff *skb;
  594. unsigned int maxlen;
  595. bool excl = true;
  596. int err = 0, len = 0;
  597. /* Netlink messages are validated by the receiver before processing.
  598. * In order to avoid userspace changing the contents of the message
  599. * after validation, the socket and the ring may only be used by a
  600. * single process, otherwise we fall back to copying.
  601. */
  602. if (atomic_long_read(&sk->sk_socket->file->f_count) > 1 ||
  603. atomic_read(&nlk->mapped) > 1)
  604. excl = false;
  605. mutex_lock(&nlk->pg_vec_lock);
  606. ring = &nlk->tx_ring;
  607. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  608. do {
  609. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
  610. if (hdr == NULL) {
  611. if (!(msg->msg_flags & MSG_DONTWAIT) &&
  612. atomic_read(&nlk->tx_ring.pending))
  613. schedule();
  614. continue;
  615. }
  616. if (hdr->nm_len > maxlen) {
  617. err = -EINVAL;
  618. goto out;
  619. }
  620. netlink_frame_flush_dcache(hdr);
  621. if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
  622. skb = alloc_skb_head(GFP_KERNEL);
  623. if (skb == NULL) {
  624. err = -ENOBUFS;
  625. goto out;
  626. }
  627. sock_hold(sk);
  628. netlink_ring_setup_skb(skb, sk, ring, hdr);
  629. NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
  630. __skb_put(skb, hdr->nm_len);
  631. netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
  632. atomic_inc(&ring->pending);
  633. } else {
  634. skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
  635. if (skb == NULL) {
  636. err = -ENOBUFS;
  637. goto out;
  638. }
  639. __skb_put(skb, hdr->nm_len);
  640. memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
  641. netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
  642. }
  643. netlink_increment_head(ring);
  644. NETLINK_CB(skb).portid = nlk->portid;
  645. NETLINK_CB(skb).dst_group = dst_group;
  646. NETLINK_CB(skb).creds = siocb->scm->creds;
  647. err = security_netlink_send(sk, skb);
  648. if (err) {
  649. kfree_skb(skb);
  650. goto out;
  651. }
  652. if (unlikely(dst_group)) {
  653. atomic_inc(&skb->users);
  654. netlink_broadcast(sk, skb, dst_portid, dst_group,
  655. GFP_KERNEL);
  656. }
  657. err = netlink_unicast(sk, skb, dst_portid,
  658. msg->msg_flags & MSG_DONTWAIT);
  659. if (err < 0)
  660. goto out;
  661. len += err;
  662. } while (hdr != NULL ||
  663. (!(msg->msg_flags & MSG_DONTWAIT) &&
  664. atomic_read(&nlk->tx_ring.pending)));
  665. if (len > 0)
  666. err = len;
  667. out:
  668. mutex_unlock(&nlk->pg_vec_lock);
  669. return err;
  670. }
  671. static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
  672. {
  673. struct nl_mmap_hdr *hdr;
  674. hdr = netlink_mmap_hdr(skb);
  675. hdr->nm_len = skb->len;
  676. hdr->nm_group = NETLINK_CB(skb).dst_group;
  677. hdr->nm_pid = NETLINK_CB(skb).creds.pid;
  678. hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
  679. hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
  680. netlink_frame_flush_dcache(hdr);
  681. netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
  682. NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
  683. kfree_skb(skb);
  684. }
  685. static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
  686. {
  687. struct netlink_sock *nlk = nlk_sk(sk);
  688. struct netlink_ring *ring = &nlk->rx_ring;
  689. struct nl_mmap_hdr *hdr;
  690. spin_lock_bh(&sk->sk_receive_queue.lock);
  691. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  692. if (hdr == NULL) {
  693. spin_unlock_bh(&sk->sk_receive_queue.lock);
  694. kfree_skb(skb);
  695. netlink_overrun(sk);
  696. return;
  697. }
  698. netlink_increment_head(ring);
  699. __skb_queue_tail(&sk->sk_receive_queue, skb);
  700. spin_unlock_bh(&sk->sk_receive_queue.lock);
  701. hdr->nm_len = skb->len;
  702. hdr->nm_group = NETLINK_CB(skb).dst_group;
  703. hdr->nm_pid = NETLINK_CB(skb).creds.pid;
  704. hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
  705. hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
  706. netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
  707. }
  708. #else /* CONFIG_NETLINK_MMAP */
  709. #define netlink_skb_is_mmaped(skb) false
  710. #define netlink_rx_is_mmaped(sk) false
  711. #define netlink_tx_is_mmaped(sk) false
  712. #define netlink_mmap sock_no_mmap
  713. #define netlink_poll datagram_poll
  714. #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
  715. #endif /* CONFIG_NETLINK_MMAP */
  716. static void netlink_skb_destructor(struct sk_buff *skb)
  717. {
  718. #ifdef CONFIG_NETLINK_MMAP
  719. struct nl_mmap_hdr *hdr;
  720. struct netlink_ring *ring;
  721. struct sock *sk;
  722. /* If a packet from the kernel to userspace was freed because of an
  723. * error without being delivered to userspace, the kernel must reset
  724. * the status. In the direction userspace to kernel, the status is
  725. * always reset here after the packet was processed and freed.
  726. */
  727. if (netlink_skb_is_mmaped(skb)) {
  728. hdr = netlink_mmap_hdr(skb);
  729. sk = NETLINK_CB(skb).sk;
  730. if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
  731. netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
  732. ring = &nlk_sk(sk)->tx_ring;
  733. } else {
  734. if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
  735. hdr->nm_len = 0;
  736. netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
  737. }
  738. ring = &nlk_sk(sk)->rx_ring;
  739. }
  740. WARN_ON(atomic_read(&ring->pending) == 0);
  741. atomic_dec(&ring->pending);
  742. sock_put(sk);
  743. skb->head = NULL;
  744. }
  745. #endif
  746. if (is_vmalloc_addr(skb->head)) {
  747. if (!skb->cloned ||
  748. !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
  749. vfree(skb->head);
  750. skb->head = NULL;
  751. }
  752. if (skb->sk != NULL)
  753. sock_rfree(skb);
  754. }
  755. static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  756. {
  757. WARN_ON(skb->sk != NULL);
  758. skb->sk = sk;
  759. skb->destructor = netlink_skb_destructor;
  760. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  761. sk_mem_charge(sk, skb->truesize);
  762. }
  763. static void netlink_sock_destruct(struct sock *sk)
  764. {
  765. struct netlink_sock *nlk = nlk_sk(sk);
  766. if (nlk->cb_running) {
  767. if (nlk->cb.done)
  768. nlk->cb.done(&nlk->cb);
  769. module_put(nlk->cb.module);
  770. kfree_skb(nlk->cb.skb);
  771. }
  772. skb_queue_purge(&sk->sk_receive_queue);
  773. #ifdef CONFIG_NETLINK_MMAP
  774. if (1) {
  775. struct nl_mmap_req req;
  776. memset(&req, 0, sizeof(req));
  777. if (nlk->rx_ring.pg_vec)
  778. netlink_set_ring(sk, &req, true, false);
  779. memset(&req, 0, sizeof(req));
  780. if (nlk->tx_ring.pg_vec)
  781. netlink_set_ring(sk, &req, true, true);
  782. }
  783. #endif /* CONFIG_NETLINK_MMAP */
  784. if (!sock_flag(sk, SOCK_DEAD)) {
  785. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  786. return;
  787. }
  788. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  789. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  790. WARN_ON(nlk_sk(sk)->groups);
  791. }
  792. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  793. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  794. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  795. * this, _but_ remember, it adds useless work on UP machines.
  796. */
  797. void netlink_table_grab(void)
  798. __acquires(nl_table_lock)
  799. {
  800. might_sleep();
  801. write_lock_irq(&nl_table_lock);
  802. if (atomic_read(&nl_table_users)) {
  803. DECLARE_WAITQUEUE(wait, current);
  804. add_wait_queue_exclusive(&nl_table_wait, &wait);
  805. for (;;) {
  806. set_current_state(TASK_UNINTERRUPTIBLE);
  807. if (atomic_read(&nl_table_users) == 0)
  808. break;
  809. write_unlock_irq(&nl_table_lock);
  810. schedule();
  811. write_lock_irq(&nl_table_lock);
  812. }
  813. __set_current_state(TASK_RUNNING);
  814. remove_wait_queue(&nl_table_wait, &wait);
  815. }
  816. }
  817. void netlink_table_ungrab(void)
  818. __releases(nl_table_lock)
  819. {
  820. write_unlock_irq(&nl_table_lock);
  821. wake_up(&nl_table_wait);
  822. }
  823. static inline void
  824. netlink_lock_table(void)
  825. {
  826. /* read_lock() synchronizes us to netlink_table_grab */
  827. read_lock(&nl_table_lock);
  828. atomic_inc(&nl_table_users);
  829. read_unlock(&nl_table_lock);
  830. }
  831. static inline void
  832. netlink_unlock_table(void)
  833. {
  834. if (atomic_dec_and_test(&nl_table_users))
  835. wake_up(&nl_table_wait);
  836. }
  837. struct netlink_compare_arg
  838. {
  839. struct net *net;
  840. u32 portid;
  841. };
  842. static bool netlink_compare(void *ptr, void *arg)
  843. {
  844. struct netlink_compare_arg *x = arg;
  845. struct sock *sk = ptr;
  846. return nlk_sk(sk)->portid == x->portid &&
  847. net_eq(sock_net(sk), x->net);
  848. }
  849. static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
  850. struct net *net)
  851. {
  852. struct netlink_compare_arg arg = {
  853. .net = net,
  854. .portid = portid,
  855. };
  856. u32 hash;
  857. hash = rhashtable_hashfn(&table->hash, &portid, sizeof(portid));
  858. return rhashtable_lookup_compare(&table->hash, hash,
  859. &netlink_compare, &arg);
  860. }
  861. static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
  862. {
  863. struct netlink_table *table = &nl_table[protocol];
  864. struct sock *sk;
  865. read_lock(&nl_table_lock);
  866. rcu_read_lock();
  867. sk = __netlink_lookup(table, portid, net);
  868. if (sk)
  869. sock_hold(sk);
  870. rcu_read_unlock();
  871. read_unlock(&nl_table_lock);
  872. return sk;
  873. }
  874. static const struct proto_ops netlink_ops;
  875. static void
  876. netlink_update_listeners(struct sock *sk)
  877. {
  878. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  879. unsigned long mask;
  880. unsigned int i;
  881. struct listeners *listeners;
  882. listeners = nl_deref_protected(tbl->listeners);
  883. if (!listeners)
  884. return;
  885. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  886. mask = 0;
  887. sk_for_each_bound(sk, &tbl->mc_list) {
  888. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  889. mask |= nlk_sk(sk)->groups[i];
  890. }
  891. listeners->masks[i] = mask;
  892. }
  893. /* this function is only called with the netlink table "grabbed", which
  894. * makes sure updates are visible before bind or setsockopt return. */
  895. }
  896. static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
  897. {
  898. struct netlink_table *table = &nl_table[sk->sk_protocol];
  899. int err = -EADDRINUSE;
  900. mutex_lock(&nl_sk_hash_lock);
  901. if (__netlink_lookup(table, portid, net))
  902. goto err;
  903. err = -EBUSY;
  904. if (nlk_sk(sk)->portid)
  905. goto err;
  906. err = -ENOMEM;
  907. if (BITS_PER_LONG > 32 && unlikely(table->hash.nelems >= UINT_MAX))
  908. goto err;
  909. nlk_sk(sk)->portid = portid;
  910. sock_hold(sk);
  911. rhashtable_insert(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL);
  912. err = 0;
  913. err:
  914. mutex_unlock(&nl_sk_hash_lock);
  915. return err;
  916. }
  917. static void netlink_remove(struct sock *sk)
  918. {
  919. struct netlink_table *table;
  920. mutex_lock(&nl_sk_hash_lock);
  921. table = &nl_table[sk->sk_protocol];
  922. if (rhashtable_remove(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL)) {
  923. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  924. __sock_put(sk);
  925. }
  926. mutex_unlock(&nl_sk_hash_lock);
  927. netlink_table_grab();
  928. if (nlk_sk(sk)->subscriptions)
  929. __sk_del_bind_node(sk);
  930. netlink_table_ungrab();
  931. }
  932. static struct proto netlink_proto = {
  933. .name = "NETLINK",
  934. .owner = THIS_MODULE,
  935. .obj_size = sizeof(struct netlink_sock),
  936. };
  937. static int __netlink_create(struct net *net, struct socket *sock,
  938. struct mutex *cb_mutex, int protocol)
  939. {
  940. struct sock *sk;
  941. struct netlink_sock *nlk;
  942. sock->ops = &netlink_ops;
  943. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  944. if (!sk)
  945. return -ENOMEM;
  946. sock_init_data(sock, sk);
  947. nlk = nlk_sk(sk);
  948. if (cb_mutex) {
  949. nlk->cb_mutex = cb_mutex;
  950. } else {
  951. nlk->cb_mutex = &nlk->cb_def_mutex;
  952. mutex_init(nlk->cb_mutex);
  953. }
  954. init_waitqueue_head(&nlk->wait);
  955. #ifdef CONFIG_NETLINK_MMAP
  956. mutex_init(&nlk->pg_vec_lock);
  957. #endif
  958. sk->sk_destruct = netlink_sock_destruct;
  959. sk->sk_protocol = protocol;
  960. return 0;
  961. }
  962. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  963. int kern)
  964. {
  965. struct module *module = NULL;
  966. struct mutex *cb_mutex;
  967. struct netlink_sock *nlk;
  968. int (*bind)(int group);
  969. void (*unbind)(int group);
  970. int err = 0;
  971. sock->state = SS_UNCONNECTED;
  972. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  973. return -ESOCKTNOSUPPORT;
  974. if (protocol < 0 || protocol >= MAX_LINKS)
  975. return -EPROTONOSUPPORT;
  976. netlink_lock_table();
  977. #ifdef CONFIG_MODULES
  978. if (!nl_table[protocol].registered) {
  979. netlink_unlock_table();
  980. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  981. netlink_lock_table();
  982. }
  983. #endif
  984. if (nl_table[protocol].registered &&
  985. try_module_get(nl_table[protocol].module))
  986. module = nl_table[protocol].module;
  987. else
  988. err = -EPROTONOSUPPORT;
  989. cb_mutex = nl_table[protocol].cb_mutex;
  990. bind = nl_table[protocol].bind;
  991. unbind = nl_table[protocol].unbind;
  992. netlink_unlock_table();
  993. if (err < 0)
  994. goto out;
  995. err = __netlink_create(net, sock, cb_mutex, protocol);
  996. if (err < 0)
  997. goto out_module;
  998. local_bh_disable();
  999. sock_prot_inuse_add(net, &netlink_proto, 1);
  1000. local_bh_enable();
  1001. nlk = nlk_sk(sock->sk);
  1002. nlk->module = module;
  1003. nlk->netlink_bind = bind;
  1004. nlk->netlink_unbind = unbind;
  1005. out:
  1006. return err;
  1007. out_module:
  1008. module_put(module);
  1009. goto out;
  1010. }
  1011. static int netlink_release(struct socket *sock)
  1012. {
  1013. struct sock *sk = sock->sk;
  1014. struct netlink_sock *nlk;
  1015. if (!sk)
  1016. return 0;
  1017. netlink_remove(sk);
  1018. sock_orphan(sk);
  1019. nlk = nlk_sk(sk);
  1020. /*
  1021. * OK. Socket is unlinked, any packets that arrive now
  1022. * will be purged.
  1023. */
  1024. sock->sk = NULL;
  1025. wake_up_interruptible_all(&nlk->wait);
  1026. skb_queue_purge(&sk->sk_write_queue);
  1027. if (nlk->portid) {
  1028. struct netlink_notify n = {
  1029. .net = sock_net(sk),
  1030. .protocol = sk->sk_protocol,
  1031. .portid = nlk->portid,
  1032. };
  1033. atomic_notifier_call_chain(&netlink_chain,
  1034. NETLINK_URELEASE, &n);
  1035. }
  1036. module_put(nlk->module);
  1037. netlink_table_grab();
  1038. if (netlink_is_kernel(sk)) {
  1039. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  1040. if (--nl_table[sk->sk_protocol].registered == 0) {
  1041. struct listeners *old;
  1042. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  1043. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  1044. kfree_rcu(old, rcu);
  1045. nl_table[sk->sk_protocol].module = NULL;
  1046. nl_table[sk->sk_protocol].bind = NULL;
  1047. nl_table[sk->sk_protocol].unbind = NULL;
  1048. nl_table[sk->sk_protocol].flags = 0;
  1049. nl_table[sk->sk_protocol].registered = 0;
  1050. }
  1051. } else if (nlk->subscriptions) {
  1052. netlink_update_listeners(sk);
  1053. }
  1054. netlink_table_ungrab();
  1055. kfree(nlk->groups);
  1056. nlk->groups = NULL;
  1057. local_bh_disable();
  1058. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  1059. local_bh_enable();
  1060. sock_put(sk);
  1061. return 0;
  1062. }
  1063. static int netlink_autobind(struct socket *sock)
  1064. {
  1065. struct sock *sk = sock->sk;
  1066. struct net *net = sock_net(sk);
  1067. struct netlink_table *table = &nl_table[sk->sk_protocol];
  1068. s32 portid = task_tgid_vnr(current);
  1069. int err;
  1070. static s32 rover = -4097;
  1071. retry:
  1072. cond_resched();
  1073. netlink_table_grab();
  1074. rcu_read_lock();
  1075. if (__netlink_lookup(table, portid, net)) {
  1076. /* Bind collision, search negative portid values. */
  1077. portid = rover--;
  1078. if (rover > -4097)
  1079. rover = -4097;
  1080. rcu_read_unlock();
  1081. netlink_table_ungrab();
  1082. goto retry;
  1083. }
  1084. rcu_read_unlock();
  1085. netlink_table_ungrab();
  1086. err = netlink_insert(sk, net, portid);
  1087. if (err == -EADDRINUSE)
  1088. goto retry;
  1089. /* If 2 threads race to autobind, that is fine. */
  1090. if (err == -EBUSY)
  1091. err = 0;
  1092. return err;
  1093. }
  1094. /**
  1095. * __netlink_ns_capable - General netlink message capability test
  1096. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  1097. * @user_ns: The user namespace of the capability to use
  1098. * @cap: The capability to use
  1099. *
  1100. * Test to see if the opener of the socket we received the message
  1101. * from had when the netlink socket was created and the sender of the
  1102. * message has has the capability @cap in the user namespace @user_ns.
  1103. */
  1104. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  1105. struct user_namespace *user_ns, int cap)
  1106. {
  1107. return ((nsp->flags & NETLINK_SKB_DST) ||
  1108. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  1109. ns_capable(user_ns, cap);
  1110. }
  1111. EXPORT_SYMBOL(__netlink_ns_capable);
  1112. /**
  1113. * netlink_ns_capable - General netlink message capability test
  1114. * @skb: socket buffer holding a netlink command from userspace
  1115. * @user_ns: The user namespace of the capability to use
  1116. * @cap: The capability to use
  1117. *
  1118. * Test to see if the opener of the socket we received the message
  1119. * from had when the netlink socket was created and the sender of the
  1120. * message has has the capability @cap in the user namespace @user_ns.
  1121. */
  1122. bool netlink_ns_capable(const struct sk_buff *skb,
  1123. struct user_namespace *user_ns, int cap)
  1124. {
  1125. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  1126. }
  1127. EXPORT_SYMBOL(netlink_ns_capable);
  1128. /**
  1129. * netlink_capable - Netlink global message capability test
  1130. * @skb: socket buffer holding a netlink command from userspace
  1131. * @cap: The capability to use
  1132. *
  1133. * Test to see if the opener of the socket we received the message
  1134. * from had when the netlink socket was created and the sender of the
  1135. * message has has the capability @cap in all user namespaces.
  1136. */
  1137. bool netlink_capable(const struct sk_buff *skb, int cap)
  1138. {
  1139. return netlink_ns_capable(skb, &init_user_ns, cap);
  1140. }
  1141. EXPORT_SYMBOL(netlink_capable);
  1142. /**
  1143. * netlink_net_capable - Netlink network namespace message capability test
  1144. * @skb: socket buffer holding a netlink command from userspace
  1145. * @cap: The capability to use
  1146. *
  1147. * Test to see if the opener of the socket we received the message
  1148. * from had when the netlink socket was created and the sender of the
  1149. * message has has the capability @cap over the network namespace of
  1150. * the socket we received the message from.
  1151. */
  1152. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  1153. {
  1154. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  1155. }
  1156. EXPORT_SYMBOL(netlink_net_capable);
  1157. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  1158. {
  1159. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  1160. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  1161. }
  1162. static void
  1163. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  1164. {
  1165. struct netlink_sock *nlk = nlk_sk(sk);
  1166. if (nlk->subscriptions && !subscriptions)
  1167. __sk_del_bind_node(sk);
  1168. else if (!nlk->subscriptions && subscriptions)
  1169. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  1170. nlk->subscriptions = subscriptions;
  1171. }
  1172. static int netlink_realloc_groups(struct sock *sk)
  1173. {
  1174. struct netlink_sock *nlk = nlk_sk(sk);
  1175. unsigned int groups;
  1176. unsigned long *new_groups;
  1177. int err = 0;
  1178. netlink_table_grab();
  1179. groups = nl_table[sk->sk_protocol].groups;
  1180. if (!nl_table[sk->sk_protocol].registered) {
  1181. err = -ENOENT;
  1182. goto out_unlock;
  1183. }
  1184. if (nlk->ngroups >= groups)
  1185. goto out_unlock;
  1186. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  1187. if (new_groups == NULL) {
  1188. err = -ENOMEM;
  1189. goto out_unlock;
  1190. }
  1191. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  1192. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  1193. nlk->groups = new_groups;
  1194. nlk->ngroups = groups;
  1195. out_unlock:
  1196. netlink_table_ungrab();
  1197. return err;
  1198. }
  1199. static void netlink_unbind(int group, long unsigned int groups,
  1200. struct netlink_sock *nlk)
  1201. {
  1202. int undo;
  1203. if (!nlk->netlink_unbind)
  1204. return;
  1205. for (undo = 0; undo < group; undo++)
  1206. if (test_bit(group, &groups))
  1207. nlk->netlink_unbind(undo);
  1208. }
  1209. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  1210. int addr_len)
  1211. {
  1212. struct sock *sk = sock->sk;
  1213. struct net *net = sock_net(sk);
  1214. struct netlink_sock *nlk = nlk_sk(sk);
  1215. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1216. int err;
  1217. long unsigned int groups = nladdr->nl_groups;
  1218. if (addr_len < sizeof(struct sockaddr_nl))
  1219. return -EINVAL;
  1220. if (nladdr->nl_family != AF_NETLINK)
  1221. return -EINVAL;
  1222. /* Only superuser is allowed to listen multicasts */
  1223. if (groups) {
  1224. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1225. return -EPERM;
  1226. err = netlink_realloc_groups(sk);
  1227. if (err)
  1228. return err;
  1229. }
  1230. if (nlk->portid)
  1231. if (nladdr->nl_pid != nlk->portid)
  1232. return -EINVAL;
  1233. if (nlk->netlink_bind && groups) {
  1234. int group;
  1235. for (group = 0; group < nlk->ngroups; group++) {
  1236. if (!test_bit(group, &groups))
  1237. continue;
  1238. err = nlk->netlink_bind(group);
  1239. if (!err)
  1240. continue;
  1241. netlink_unbind(group, groups, nlk);
  1242. return err;
  1243. }
  1244. }
  1245. if (!nlk->portid) {
  1246. err = nladdr->nl_pid ?
  1247. netlink_insert(sk, net, nladdr->nl_pid) :
  1248. netlink_autobind(sock);
  1249. if (err) {
  1250. netlink_unbind(nlk->ngroups - 1, groups, nlk);
  1251. return err;
  1252. }
  1253. }
  1254. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  1255. return 0;
  1256. netlink_table_grab();
  1257. netlink_update_subscriptions(sk, nlk->subscriptions +
  1258. hweight32(groups) -
  1259. hweight32(nlk->groups[0]));
  1260. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  1261. netlink_update_listeners(sk);
  1262. netlink_table_ungrab();
  1263. return 0;
  1264. }
  1265. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  1266. int alen, int flags)
  1267. {
  1268. int err = 0;
  1269. struct sock *sk = sock->sk;
  1270. struct netlink_sock *nlk = nlk_sk(sk);
  1271. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1272. if (alen < sizeof(addr->sa_family))
  1273. return -EINVAL;
  1274. if (addr->sa_family == AF_UNSPEC) {
  1275. sk->sk_state = NETLINK_UNCONNECTED;
  1276. nlk->dst_portid = 0;
  1277. nlk->dst_group = 0;
  1278. return 0;
  1279. }
  1280. if (addr->sa_family != AF_NETLINK)
  1281. return -EINVAL;
  1282. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  1283. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1284. return -EPERM;
  1285. if (!nlk->portid)
  1286. err = netlink_autobind(sock);
  1287. if (err == 0) {
  1288. sk->sk_state = NETLINK_CONNECTED;
  1289. nlk->dst_portid = nladdr->nl_pid;
  1290. nlk->dst_group = ffs(nladdr->nl_groups);
  1291. }
  1292. return err;
  1293. }
  1294. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  1295. int *addr_len, int peer)
  1296. {
  1297. struct sock *sk = sock->sk;
  1298. struct netlink_sock *nlk = nlk_sk(sk);
  1299. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  1300. nladdr->nl_family = AF_NETLINK;
  1301. nladdr->nl_pad = 0;
  1302. *addr_len = sizeof(*nladdr);
  1303. if (peer) {
  1304. nladdr->nl_pid = nlk->dst_portid;
  1305. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  1306. } else {
  1307. nladdr->nl_pid = nlk->portid;
  1308. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  1309. }
  1310. return 0;
  1311. }
  1312. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  1313. {
  1314. struct sock *sock;
  1315. struct netlink_sock *nlk;
  1316. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  1317. if (!sock)
  1318. return ERR_PTR(-ECONNREFUSED);
  1319. /* Don't bother queuing skb if kernel socket has no input function */
  1320. nlk = nlk_sk(sock);
  1321. if (sock->sk_state == NETLINK_CONNECTED &&
  1322. nlk->dst_portid != nlk_sk(ssk)->portid) {
  1323. sock_put(sock);
  1324. return ERR_PTR(-ECONNREFUSED);
  1325. }
  1326. return sock;
  1327. }
  1328. struct sock *netlink_getsockbyfilp(struct file *filp)
  1329. {
  1330. struct inode *inode = file_inode(filp);
  1331. struct sock *sock;
  1332. if (!S_ISSOCK(inode->i_mode))
  1333. return ERR_PTR(-ENOTSOCK);
  1334. sock = SOCKET_I(inode)->sk;
  1335. if (sock->sk_family != AF_NETLINK)
  1336. return ERR_PTR(-EINVAL);
  1337. sock_hold(sock);
  1338. return sock;
  1339. }
  1340. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  1341. int broadcast)
  1342. {
  1343. struct sk_buff *skb;
  1344. void *data;
  1345. if (size <= NLMSG_GOODSIZE || broadcast)
  1346. return alloc_skb(size, GFP_KERNEL);
  1347. size = SKB_DATA_ALIGN(size) +
  1348. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1349. data = vmalloc(size);
  1350. if (data == NULL)
  1351. return NULL;
  1352. skb = build_skb(data, size);
  1353. if (skb == NULL)
  1354. vfree(data);
  1355. else {
  1356. skb->head_frag = 0;
  1357. skb->destructor = netlink_skb_destructor;
  1358. }
  1359. return skb;
  1360. }
  1361. /*
  1362. * Attach a skb to a netlink socket.
  1363. * The caller must hold a reference to the destination socket. On error, the
  1364. * reference is dropped. The skb is not send to the destination, just all
  1365. * all error checks are performed and memory in the queue is reserved.
  1366. * Return values:
  1367. * < 0: error. skb freed, reference to sock dropped.
  1368. * 0: continue
  1369. * 1: repeat lookup - reference dropped while waiting for socket memory.
  1370. */
  1371. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  1372. long *timeo, struct sock *ssk)
  1373. {
  1374. struct netlink_sock *nlk;
  1375. nlk = nlk_sk(sk);
  1376. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1377. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1378. !netlink_skb_is_mmaped(skb)) {
  1379. DECLARE_WAITQUEUE(wait, current);
  1380. if (!*timeo) {
  1381. if (!ssk || netlink_is_kernel(ssk))
  1382. netlink_overrun(sk);
  1383. sock_put(sk);
  1384. kfree_skb(skb);
  1385. return -EAGAIN;
  1386. }
  1387. __set_current_state(TASK_INTERRUPTIBLE);
  1388. add_wait_queue(&nlk->wait, &wait);
  1389. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1390. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1391. !sock_flag(sk, SOCK_DEAD))
  1392. *timeo = schedule_timeout(*timeo);
  1393. __set_current_state(TASK_RUNNING);
  1394. remove_wait_queue(&nlk->wait, &wait);
  1395. sock_put(sk);
  1396. if (signal_pending(current)) {
  1397. kfree_skb(skb);
  1398. return sock_intr_errno(*timeo);
  1399. }
  1400. return 1;
  1401. }
  1402. netlink_skb_set_owner_r(skb, sk);
  1403. return 0;
  1404. }
  1405. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1406. {
  1407. int len = skb->len;
  1408. netlink_deliver_tap(skb);
  1409. #ifdef CONFIG_NETLINK_MMAP
  1410. if (netlink_skb_is_mmaped(skb))
  1411. netlink_queue_mmaped_skb(sk, skb);
  1412. else if (netlink_rx_is_mmaped(sk))
  1413. netlink_ring_set_copied(sk, skb);
  1414. else
  1415. #endif /* CONFIG_NETLINK_MMAP */
  1416. skb_queue_tail(&sk->sk_receive_queue, skb);
  1417. sk->sk_data_ready(sk);
  1418. return len;
  1419. }
  1420. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1421. {
  1422. int len = __netlink_sendskb(sk, skb);
  1423. sock_put(sk);
  1424. return len;
  1425. }
  1426. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1427. {
  1428. kfree_skb(skb);
  1429. sock_put(sk);
  1430. }
  1431. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1432. {
  1433. int delta;
  1434. WARN_ON(skb->sk != NULL);
  1435. if (netlink_skb_is_mmaped(skb))
  1436. return skb;
  1437. delta = skb->end - skb->tail;
  1438. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1439. return skb;
  1440. if (skb_shared(skb)) {
  1441. struct sk_buff *nskb = skb_clone(skb, allocation);
  1442. if (!nskb)
  1443. return skb;
  1444. consume_skb(skb);
  1445. skb = nskb;
  1446. }
  1447. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1448. skb->truesize -= delta;
  1449. return skb;
  1450. }
  1451. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1452. struct sock *ssk)
  1453. {
  1454. int ret;
  1455. struct netlink_sock *nlk = nlk_sk(sk);
  1456. ret = -ECONNREFUSED;
  1457. if (nlk->netlink_rcv != NULL) {
  1458. ret = skb->len;
  1459. netlink_skb_set_owner_r(skb, sk);
  1460. NETLINK_CB(skb).sk = ssk;
  1461. netlink_deliver_tap_kernel(sk, ssk, skb);
  1462. nlk->netlink_rcv(skb);
  1463. consume_skb(skb);
  1464. } else {
  1465. kfree_skb(skb);
  1466. }
  1467. sock_put(sk);
  1468. return ret;
  1469. }
  1470. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1471. u32 portid, int nonblock)
  1472. {
  1473. struct sock *sk;
  1474. int err;
  1475. long timeo;
  1476. skb = netlink_trim(skb, gfp_any());
  1477. timeo = sock_sndtimeo(ssk, nonblock);
  1478. retry:
  1479. sk = netlink_getsockbyportid(ssk, portid);
  1480. if (IS_ERR(sk)) {
  1481. kfree_skb(skb);
  1482. return PTR_ERR(sk);
  1483. }
  1484. if (netlink_is_kernel(sk))
  1485. return netlink_unicast_kernel(sk, skb, ssk);
  1486. if (sk_filter(sk, skb)) {
  1487. err = skb->len;
  1488. kfree_skb(skb);
  1489. sock_put(sk);
  1490. return err;
  1491. }
  1492. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1493. if (err == 1)
  1494. goto retry;
  1495. if (err)
  1496. return err;
  1497. return netlink_sendskb(sk, skb);
  1498. }
  1499. EXPORT_SYMBOL(netlink_unicast);
  1500. struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
  1501. u32 dst_portid, gfp_t gfp_mask)
  1502. {
  1503. #ifdef CONFIG_NETLINK_MMAP
  1504. struct sock *sk = NULL;
  1505. struct sk_buff *skb;
  1506. struct netlink_ring *ring;
  1507. struct nl_mmap_hdr *hdr;
  1508. unsigned int maxlen;
  1509. sk = netlink_getsockbyportid(ssk, dst_portid);
  1510. if (IS_ERR(sk))
  1511. goto out;
  1512. ring = &nlk_sk(sk)->rx_ring;
  1513. /* fast-path without atomic ops for common case: non-mmaped receiver */
  1514. if (ring->pg_vec == NULL)
  1515. goto out_put;
  1516. if (ring->frame_size - NL_MMAP_HDRLEN < size)
  1517. goto out_put;
  1518. skb = alloc_skb_head(gfp_mask);
  1519. if (skb == NULL)
  1520. goto err1;
  1521. spin_lock_bh(&sk->sk_receive_queue.lock);
  1522. /* check again under lock */
  1523. if (ring->pg_vec == NULL)
  1524. goto out_free;
  1525. /* check again under lock */
  1526. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  1527. if (maxlen < size)
  1528. goto out_free;
  1529. netlink_forward_ring(ring);
  1530. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  1531. if (hdr == NULL)
  1532. goto err2;
  1533. netlink_ring_setup_skb(skb, sk, ring, hdr);
  1534. netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
  1535. atomic_inc(&ring->pending);
  1536. netlink_increment_head(ring);
  1537. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1538. return skb;
  1539. err2:
  1540. kfree_skb(skb);
  1541. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1542. netlink_overrun(sk);
  1543. err1:
  1544. sock_put(sk);
  1545. return NULL;
  1546. out_free:
  1547. kfree_skb(skb);
  1548. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1549. out_put:
  1550. sock_put(sk);
  1551. out:
  1552. #endif
  1553. return alloc_skb(size, gfp_mask);
  1554. }
  1555. EXPORT_SYMBOL_GPL(netlink_alloc_skb);
  1556. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1557. {
  1558. int res = 0;
  1559. struct listeners *listeners;
  1560. BUG_ON(!netlink_is_kernel(sk));
  1561. rcu_read_lock();
  1562. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1563. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1564. res = test_bit(group - 1, listeners->masks);
  1565. rcu_read_unlock();
  1566. return res;
  1567. }
  1568. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1569. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1570. {
  1571. struct netlink_sock *nlk = nlk_sk(sk);
  1572. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1573. !test_bit(NETLINK_CONGESTED, &nlk->state)) {
  1574. netlink_skb_set_owner_r(skb, sk);
  1575. __netlink_sendskb(sk, skb);
  1576. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1577. }
  1578. return -1;
  1579. }
  1580. struct netlink_broadcast_data {
  1581. struct sock *exclude_sk;
  1582. struct net *net;
  1583. u32 portid;
  1584. u32 group;
  1585. int failure;
  1586. int delivery_failure;
  1587. int congested;
  1588. int delivered;
  1589. gfp_t allocation;
  1590. struct sk_buff *skb, *skb2;
  1591. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1592. void *tx_data;
  1593. };
  1594. static void do_one_broadcast(struct sock *sk,
  1595. struct netlink_broadcast_data *p)
  1596. {
  1597. struct netlink_sock *nlk = nlk_sk(sk);
  1598. int val;
  1599. if (p->exclude_sk == sk)
  1600. return;
  1601. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1602. !test_bit(p->group - 1, nlk->groups))
  1603. return;
  1604. if (!net_eq(sock_net(sk), p->net))
  1605. return;
  1606. if (p->failure) {
  1607. netlink_overrun(sk);
  1608. return;
  1609. }
  1610. sock_hold(sk);
  1611. if (p->skb2 == NULL) {
  1612. if (skb_shared(p->skb)) {
  1613. p->skb2 = skb_clone(p->skb, p->allocation);
  1614. } else {
  1615. p->skb2 = skb_get(p->skb);
  1616. /*
  1617. * skb ownership may have been set when
  1618. * delivered to a previous socket.
  1619. */
  1620. skb_orphan(p->skb2);
  1621. }
  1622. }
  1623. if (p->skb2 == NULL) {
  1624. netlink_overrun(sk);
  1625. /* Clone failed. Notify ALL listeners. */
  1626. p->failure = 1;
  1627. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1628. p->delivery_failure = 1;
  1629. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1630. kfree_skb(p->skb2);
  1631. p->skb2 = NULL;
  1632. } else if (sk_filter(sk, p->skb2)) {
  1633. kfree_skb(p->skb2);
  1634. p->skb2 = NULL;
  1635. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  1636. netlink_overrun(sk);
  1637. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1638. p->delivery_failure = 1;
  1639. } else {
  1640. p->congested |= val;
  1641. p->delivered = 1;
  1642. p->skb2 = NULL;
  1643. }
  1644. sock_put(sk);
  1645. }
  1646. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1647. u32 group, gfp_t allocation,
  1648. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1649. void *filter_data)
  1650. {
  1651. struct net *net = sock_net(ssk);
  1652. struct netlink_broadcast_data info;
  1653. struct sock *sk;
  1654. skb = netlink_trim(skb, allocation);
  1655. info.exclude_sk = ssk;
  1656. info.net = net;
  1657. info.portid = portid;
  1658. info.group = group;
  1659. info.failure = 0;
  1660. info.delivery_failure = 0;
  1661. info.congested = 0;
  1662. info.delivered = 0;
  1663. info.allocation = allocation;
  1664. info.skb = skb;
  1665. info.skb2 = NULL;
  1666. info.tx_filter = filter;
  1667. info.tx_data = filter_data;
  1668. /* While we sleep in clone, do not allow to change socket list */
  1669. netlink_lock_table();
  1670. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1671. do_one_broadcast(sk, &info);
  1672. consume_skb(skb);
  1673. netlink_unlock_table();
  1674. if (info.delivery_failure) {
  1675. kfree_skb(info.skb2);
  1676. return -ENOBUFS;
  1677. }
  1678. consume_skb(info.skb2);
  1679. if (info.delivered) {
  1680. if (info.congested && (allocation & __GFP_WAIT))
  1681. yield();
  1682. return 0;
  1683. }
  1684. return -ESRCH;
  1685. }
  1686. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1687. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1688. u32 group, gfp_t allocation)
  1689. {
  1690. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1691. NULL, NULL);
  1692. }
  1693. EXPORT_SYMBOL(netlink_broadcast);
  1694. struct netlink_set_err_data {
  1695. struct sock *exclude_sk;
  1696. u32 portid;
  1697. u32 group;
  1698. int code;
  1699. };
  1700. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1701. {
  1702. struct netlink_sock *nlk = nlk_sk(sk);
  1703. int ret = 0;
  1704. if (sk == p->exclude_sk)
  1705. goto out;
  1706. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1707. goto out;
  1708. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1709. !test_bit(p->group - 1, nlk->groups))
  1710. goto out;
  1711. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  1712. ret = 1;
  1713. goto out;
  1714. }
  1715. sk->sk_err = p->code;
  1716. sk->sk_error_report(sk);
  1717. out:
  1718. return ret;
  1719. }
  1720. /**
  1721. * netlink_set_err - report error to broadcast listeners
  1722. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1723. * @portid: the PORTID of a process that we want to skip (if any)
  1724. * @group: the broadcast group that will notice the error
  1725. * @code: error code, must be negative (as usual in kernelspace)
  1726. *
  1727. * This function returns the number of broadcast listeners that have set the
  1728. * NETLINK_RECV_NO_ENOBUFS socket option.
  1729. */
  1730. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1731. {
  1732. struct netlink_set_err_data info;
  1733. struct sock *sk;
  1734. int ret = 0;
  1735. info.exclude_sk = ssk;
  1736. info.portid = portid;
  1737. info.group = group;
  1738. /* sk->sk_err wants a positive error value */
  1739. info.code = -code;
  1740. read_lock(&nl_table_lock);
  1741. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1742. ret += do_one_set_err(sk, &info);
  1743. read_unlock(&nl_table_lock);
  1744. return ret;
  1745. }
  1746. EXPORT_SYMBOL(netlink_set_err);
  1747. /* must be called with netlink table grabbed */
  1748. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1749. unsigned int group,
  1750. int is_new)
  1751. {
  1752. int old, new = !!is_new, subscriptions;
  1753. old = test_bit(group - 1, nlk->groups);
  1754. subscriptions = nlk->subscriptions - old + new;
  1755. if (new)
  1756. __set_bit(group - 1, nlk->groups);
  1757. else
  1758. __clear_bit(group - 1, nlk->groups);
  1759. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1760. netlink_update_listeners(&nlk->sk);
  1761. }
  1762. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1763. char __user *optval, unsigned int optlen)
  1764. {
  1765. struct sock *sk = sock->sk;
  1766. struct netlink_sock *nlk = nlk_sk(sk);
  1767. unsigned int val = 0;
  1768. int err;
  1769. if (level != SOL_NETLINK)
  1770. return -ENOPROTOOPT;
  1771. if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
  1772. optlen >= sizeof(int) &&
  1773. get_user(val, (unsigned int __user *)optval))
  1774. return -EFAULT;
  1775. switch (optname) {
  1776. case NETLINK_PKTINFO:
  1777. if (val)
  1778. nlk->flags |= NETLINK_RECV_PKTINFO;
  1779. else
  1780. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1781. err = 0;
  1782. break;
  1783. case NETLINK_ADD_MEMBERSHIP:
  1784. case NETLINK_DROP_MEMBERSHIP: {
  1785. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1786. return -EPERM;
  1787. err = netlink_realloc_groups(sk);
  1788. if (err)
  1789. return err;
  1790. if (!val || val - 1 >= nlk->ngroups)
  1791. return -EINVAL;
  1792. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1793. err = nlk->netlink_bind(val);
  1794. if (err)
  1795. return err;
  1796. }
  1797. netlink_table_grab();
  1798. netlink_update_socket_mc(nlk, val,
  1799. optname == NETLINK_ADD_MEMBERSHIP);
  1800. netlink_table_ungrab();
  1801. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1802. nlk->netlink_unbind(val);
  1803. err = 0;
  1804. break;
  1805. }
  1806. case NETLINK_BROADCAST_ERROR:
  1807. if (val)
  1808. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1809. else
  1810. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1811. err = 0;
  1812. break;
  1813. case NETLINK_NO_ENOBUFS:
  1814. if (val) {
  1815. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1816. clear_bit(NETLINK_CONGESTED, &nlk->state);
  1817. wake_up_interruptible(&nlk->wait);
  1818. } else {
  1819. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1820. }
  1821. err = 0;
  1822. break;
  1823. #ifdef CONFIG_NETLINK_MMAP
  1824. case NETLINK_RX_RING:
  1825. case NETLINK_TX_RING: {
  1826. struct nl_mmap_req req;
  1827. /* Rings might consume more memory than queue limits, require
  1828. * CAP_NET_ADMIN.
  1829. */
  1830. if (!capable(CAP_NET_ADMIN))
  1831. return -EPERM;
  1832. if (optlen < sizeof(req))
  1833. return -EINVAL;
  1834. if (copy_from_user(&req, optval, sizeof(req)))
  1835. return -EFAULT;
  1836. err = netlink_set_ring(sk, &req, false,
  1837. optname == NETLINK_TX_RING);
  1838. break;
  1839. }
  1840. #endif /* CONFIG_NETLINK_MMAP */
  1841. default:
  1842. err = -ENOPROTOOPT;
  1843. }
  1844. return err;
  1845. }
  1846. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1847. char __user *optval, int __user *optlen)
  1848. {
  1849. struct sock *sk = sock->sk;
  1850. struct netlink_sock *nlk = nlk_sk(sk);
  1851. int len, val, err;
  1852. if (level != SOL_NETLINK)
  1853. return -ENOPROTOOPT;
  1854. if (get_user(len, optlen))
  1855. return -EFAULT;
  1856. if (len < 0)
  1857. return -EINVAL;
  1858. switch (optname) {
  1859. case NETLINK_PKTINFO:
  1860. if (len < sizeof(int))
  1861. return -EINVAL;
  1862. len = sizeof(int);
  1863. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1864. if (put_user(len, optlen) ||
  1865. put_user(val, optval))
  1866. return -EFAULT;
  1867. err = 0;
  1868. break;
  1869. case NETLINK_BROADCAST_ERROR:
  1870. if (len < sizeof(int))
  1871. return -EINVAL;
  1872. len = sizeof(int);
  1873. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1874. if (put_user(len, optlen) ||
  1875. put_user(val, optval))
  1876. return -EFAULT;
  1877. err = 0;
  1878. break;
  1879. case NETLINK_NO_ENOBUFS:
  1880. if (len < sizeof(int))
  1881. return -EINVAL;
  1882. len = sizeof(int);
  1883. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1884. if (put_user(len, optlen) ||
  1885. put_user(val, optval))
  1886. return -EFAULT;
  1887. err = 0;
  1888. break;
  1889. default:
  1890. err = -ENOPROTOOPT;
  1891. }
  1892. return err;
  1893. }
  1894. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1895. {
  1896. struct nl_pktinfo info;
  1897. info.group = NETLINK_CB(skb).dst_group;
  1898. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1899. }
  1900. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1901. struct msghdr *msg, size_t len)
  1902. {
  1903. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1904. struct sock *sk = sock->sk;
  1905. struct netlink_sock *nlk = nlk_sk(sk);
  1906. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1907. u32 dst_portid;
  1908. u32 dst_group;
  1909. struct sk_buff *skb;
  1910. int err;
  1911. struct scm_cookie scm;
  1912. u32 netlink_skb_flags = 0;
  1913. if (msg->msg_flags&MSG_OOB)
  1914. return -EOPNOTSUPP;
  1915. if (NULL == siocb->scm)
  1916. siocb->scm = &scm;
  1917. err = scm_send(sock, msg, siocb->scm, true);
  1918. if (err < 0)
  1919. return err;
  1920. if (msg->msg_namelen) {
  1921. err = -EINVAL;
  1922. if (addr->nl_family != AF_NETLINK)
  1923. goto out;
  1924. dst_portid = addr->nl_pid;
  1925. dst_group = ffs(addr->nl_groups);
  1926. err = -EPERM;
  1927. if ((dst_group || dst_portid) &&
  1928. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1929. goto out;
  1930. netlink_skb_flags |= NETLINK_SKB_DST;
  1931. } else {
  1932. dst_portid = nlk->dst_portid;
  1933. dst_group = nlk->dst_group;
  1934. }
  1935. if (!nlk->portid) {
  1936. err = netlink_autobind(sock);
  1937. if (err)
  1938. goto out;
  1939. }
  1940. if (netlink_tx_is_mmaped(sk) &&
  1941. msg->msg_iov->iov_base == NULL) {
  1942. err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
  1943. siocb);
  1944. goto out;
  1945. }
  1946. err = -EMSGSIZE;
  1947. if (len > sk->sk_sndbuf - 32)
  1948. goto out;
  1949. err = -ENOBUFS;
  1950. skb = netlink_alloc_large_skb(len, dst_group);
  1951. if (skb == NULL)
  1952. goto out;
  1953. NETLINK_CB(skb).portid = nlk->portid;
  1954. NETLINK_CB(skb).dst_group = dst_group;
  1955. NETLINK_CB(skb).creds = siocb->scm->creds;
  1956. NETLINK_CB(skb).flags = netlink_skb_flags;
  1957. err = -EFAULT;
  1958. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1959. kfree_skb(skb);
  1960. goto out;
  1961. }
  1962. err = security_netlink_send(sk, skb);
  1963. if (err) {
  1964. kfree_skb(skb);
  1965. goto out;
  1966. }
  1967. if (dst_group) {
  1968. atomic_inc(&skb->users);
  1969. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1970. }
  1971. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1972. out:
  1973. scm_destroy(siocb->scm);
  1974. return err;
  1975. }
  1976. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1977. struct msghdr *msg, size_t len,
  1978. int flags)
  1979. {
  1980. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1981. struct scm_cookie scm;
  1982. struct sock *sk = sock->sk;
  1983. struct netlink_sock *nlk = nlk_sk(sk);
  1984. int noblock = flags&MSG_DONTWAIT;
  1985. size_t copied;
  1986. struct sk_buff *skb, *data_skb;
  1987. int err, ret;
  1988. if (flags&MSG_OOB)
  1989. return -EOPNOTSUPP;
  1990. copied = 0;
  1991. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1992. if (skb == NULL)
  1993. goto out;
  1994. data_skb = skb;
  1995. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1996. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1997. /*
  1998. * If this skb has a frag_list, then here that means that we
  1999. * will have to use the frag_list skb's data for compat tasks
  2000. * and the regular skb's data for normal (non-compat) tasks.
  2001. *
  2002. * If we need to send the compat skb, assign it to the
  2003. * 'data_skb' variable so that it will be used below for data
  2004. * copying. We keep 'skb' for everything else, including
  2005. * freeing both later.
  2006. */
  2007. if (flags & MSG_CMSG_COMPAT)
  2008. data_skb = skb_shinfo(skb)->frag_list;
  2009. }
  2010. #endif
  2011. /* Record the max length of recvmsg() calls for future allocations */
  2012. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  2013. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  2014. 16384);
  2015. copied = data_skb->len;
  2016. if (len < copied) {
  2017. msg->msg_flags |= MSG_TRUNC;
  2018. copied = len;
  2019. }
  2020. skb_reset_transport_header(data_skb);
  2021. err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
  2022. if (msg->msg_name) {
  2023. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  2024. addr->nl_family = AF_NETLINK;
  2025. addr->nl_pad = 0;
  2026. addr->nl_pid = NETLINK_CB(skb).portid;
  2027. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  2028. msg->msg_namelen = sizeof(*addr);
  2029. }
  2030. if (nlk->flags & NETLINK_RECV_PKTINFO)
  2031. netlink_cmsg_recv_pktinfo(msg, skb);
  2032. if (NULL == siocb->scm) {
  2033. memset(&scm, 0, sizeof(scm));
  2034. siocb->scm = &scm;
  2035. }
  2036. siocb->scm->creds = *NETLINK_CREDS(skb);
  2037. if (flags & MSG_TRUNC)
  2038. copied = data_skb->len;
  2039. skb_free_datagram(sk, skb);
  2040. if (nlk->cb_running &&
  2041. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  2042. ret = netlink_dump(sk);
  2043. if (ret) {
  2044. sk->sk_err = -ret;
  2045. sk->sk_error_report(sk);
  2046. }
  2047. }
  2048. scm_recv(sock, msg, siocb->scm, flags);
  2049. out:
  2050. netlink_rcv_wake(sk);
  2051. return err ? : copied;
  2052. }
  2053. static void netlink_data_ready(struct sock *sk)
  2054. {
  2055. BUG();
  2056. }
  2057. /*
  2058. * We export these functions to other modules. They provide a
  2059. * complete set of kernel non-blocking support for message
  2060. * queueing.
  2061. */
  2062. struct sock *
  2063. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  2064. struct netlink_kernel_cfg *cfg)
  2065. {
  2066. struct socket *sock;
  2067. struct sock *sk;
  2068. struct netlink_sock *nlk;
  2069. struct listeners *listeners = NULL;
  2070. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  2071. unsigned int groups;
  2072. BUG_ON(!nl_table);
  2073. if (unit < 0 || unit >= MAX_LINKS)
  2074. return NULL;
  2075. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  2076. return NULL;
  2077. /*
  2078. * We have to just have a reference on the net from sk, but don't
  2079. * get_net it. Besides, we cannot get and then put the net here.
  2080. * So we create one inside init_net and the move it to net.
  2081. */
  2082. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  2083. goto out_sock_release_nosk;
  2084. sk = sock->sk;
  2085. sk_change_net(sk, net);
  2086. if (!cfg || cfg->groups < 32)
  2087. groups = 32;
  2088. else
  2089. groups = cfg->groups;
  2090. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2091. if (!listeners)
  2092. goto out_sock_release;
  2093. sk->sk_data_ready = netlink_data_ready;
  2094. if (cfg && cfg->input)
  2095. nlk_sk(sk)->netlink_rcv = cfg->input;
  2096. if (netlink_insert(sk, net, 0))
  2097. goto out_sock_release;
  2098. nlk = nlk_sk(sk);
  2099. nlk->flags |= NETLINK_KERNEL_SOCKET;
  2100. netlink_table_grab();
  2101. if (!nl_table[unit].registered) {
  2102. nl_table[unit].groups = groups;
  2103. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  2104. nl_table[unit].cb_mutex = cb_mutex;
  2105. nl_table[unit].module = module;
  2106. if (cfg) {
  2107. nl_table[unit].bind = cfg->bind;
  2108. nl_table[unit].flags = cfg->flags;
  2109. if (cfg->compare)
  2110. nl_table[unit].compare = cfg->compare;
  2111. }
  2112. nl_table[unit].registered = 1;
  2113. } else {
  2114. kfree(listeners);
  2115. nl_table[unit].registered++;
  2116. }
  2117. netlink_table_ungrab();
  2118. return sk;
  2119. out_sock_release:
  2120. kfree(listeners);
  2121. netlink_kernel_release(sk);
  2122. return NULL;
  2123. out_sock_release_nosk:
  2124. sock_release(sock);
  2125. return NULL;
  2126. }
  2127. EXPORT_SYMBOL(__netlink_kernel_create);
  2128. void
  2129. netlink_kernel_release(struct sock *sk)
  2130. {
  2131. sk_release_kernel(sk);
  2132. }
  2133. EXPORT_SYMBOL(netlink_kernel_release);
  2134. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2135. {
  2136. struct listeners *new, *old;
  2137. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  2138. if (groups < 32)
  2139. groups = 32;
  2140. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  2141. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  2142. if (!new)
  2143. return -ENOMEM;
  2144. old = nl_deref_protected(tbl->listeners);
  2145. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  2146. rcu_assign_pointer(tbl->listeners, new);
  2147. kfree_rcu(old, rcu);
  2148. }
  2149. tbl->groups = groups;
  2150. return 0;
  2151. }
  2152. /**
  2153. * netlink_change_ngroups - change number of multicast groups
  2154. *
  2155. * This changes the number of multicast groups that are available
  2156. * on a certain netlink family. Note that it is not possible to
  2157. * change the number of groups to below 32. Also note that it does
  2158. * not implicitly call netlink_clear_multicast_users() when the
  2159. * number of groups is reduced.
  2160. *
  2161. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  2162. * @groups: The new number of groups.
  2163. */
  2164. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2165. {
  2166. int err;
  2167. netlink_table_grab();
  2168. err = __netlink_change_ngroups(sk, groups);
  2169. netlink_table_ungrab();
  2170. return err;
  2171. }
  2172. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  2173. {
  2174. struct sock *sk;
  2175. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  2176. sk_for_each_bound(sk, &tbl->mc_list)
  2177. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  2178. }
  2179. struct nlmsghdr *
  2180. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  2181. {
  2182. struct nlmsghdr *nlh;
  2183. int size = nlmsg_msg_size(len);
  2184. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  2185. nlh->nlmsg_type = type;
  2186. nlh->nlmsg_len = size;
  2187. nlh->nlmsg_flags = flags;
  2188. nlh->nlmsg_pid = portid;
  2189. nlh->nlmsg_seq = seq;
  2190. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  2191. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  2192. return nlh;
  2193. }
  2194. EXPORT_SYMBOL(__nlmsg_put);
  2195. /*
  2196. * It looks a bit ugly.
  2197. * It would be better to create kernel thread.
  2198. */
  2199. static int netlink_dump(struct sock *sk)
  2200. {
  2201. struct netlink_sock *nlk = nlk_sk(sk);
  2202. struct netlink_callback *cb;
  2203. struct sk_buff *skb = NULL;
  2204. struct nlmsghdr *nlh;
  2205. int len, err = -ENOBUFS;
  2206. int alloc_size;
  2207. mutex_lock(nlk->cb_mutex);
  2208. if (!nlk->cb_running) {
  2209. err = -EINVAL;
  2210. goto errout_skb;
  2211. }
  2212. cb = &nlk->cb;
  2213. alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  2214. if (!netlink_rx_is_mmaped(sk) &&
  2215. atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  2216. goto errout_skb;
  2217. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  2218. * required, but it makes sense to _attempt_ a 16K bytes allocation
  2219. * to reduce number of system calls on dump operations, if user
  2220. * ever provided a big enough buffer.
  2221. */
  2222. if (alloc_size < nlk->max_recvmsg_len) {
  2223. skb = netlink_alloc_skb(sk,
  2224. nlk->max_recvmsg_len,
  2225. nlk->portid,
  2226. GFP_KERNEL |
  2227. __GFP_NOWARN |
  2228. __GFP_NORETRY);
  2229. /* available room should be exact amount to avoid MSG_TRUNC */
  2230. if (skb)
  2231. skb_reserve(skb, skb_tailroom(skb) -
  2232. nlk->max_recvmsg_len);
  2233. }
  2234. if (!skb)
  2235. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  2236. GFP_KERNEL);
  2237. if (!skb)
  2238. goto errout_skb;
  2239. netlink_skb_set_owner_r(skb, sk);
  2240. len = cb->dump(skb, cb);
  2241. if (len > 0) {
  2242. mutex_unlock(nlk->cb_mutex);
  2243. if (sk_filter(sk, skb))
  2244. kfree_skb(skb);
  2245. else
  2246. __netlink_sendskb(sk, skb);
  2247. return 0;
  2248. }
  2249. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  2250. if (!nlh)
  2251. goto errout_skb;
  2252. nl_dump_check_consistent(cb, nlh);
  2253. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  2254. if (sk_filter(sk, skb))
  2255. kfree_skb(skb);
  2256. else
  2257. __netlink_sendskb(sk, skb);
  2258. if (cb->done)
  2259. cb->done(cb);
  2260. nlk->cb_running = false;
  2261. mutex_unlock(nlk->cb_mutex);
  2262. module_put(cb->module);
  2263. consume_skb(cb->skb);
  2264. return 0;
  2265. errout_skb:
  2266. mutex_unlock(nlk->cb_mutex);
  2267. kfree_skb(skb);
  2268. return err;
  2269. }
  2270. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  2271. const struct nlmsghdr *nlh,
  2272. struct netlink_dump_control *control)
  2273. {
  2274. struct netlink_callback *cb;
  2275. struct sock *sk;
  2276. struct netlink_sock *nlk;
  2277. int ret;
  2278. /* Memory mapped dump requests need to be copied to avoid looping
  2279. * on the pending state in netlink_mmap_sendmsg() while the CB hold
  2280. * a reference to the skb.
  2281. */
  2282. if (netlink_skb_is_mmaped(skb)) {
  2283. skb = skb_copy(skb, GFP_KERNEL);
  2284. if (skb == NULL)
  2285. return -ENOBUFS;
  2286. } else
  2287. atomic_inc(&skb->users);
  2288. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  2289. if (sk == NULL) {
  2290. ret = -ECONNREFUSED;
  2291. goto error_free;
  2292. }
  2293. nlk = nlk_sk(sk);
  2294. mutex_lock(nlk->cb_mutex);
  2295. /* A dump is in progress... */
  2296. if (nlk->cb_running) {
  2297. ret = -EBUSY;
  2298. goto error_unlock;
  2299. }
  2300. /* add reference of module which cb->dump belongs to */
  2301. if (!try_module_get(control->module)) {
  2302. ret = -EPROTONOSUPPORT;
  2303. goto error_unlock;
  2304. }
  2305. cb = &nlk->cb;
  2306. memset(cb, 0, sizeof(*cb));
  2307. cb->dump = control->dump;
  2308. cb->done = control->done;
  2309. cb->nlh = nlh;
  2310. cb->data = control->data;
  2311. cb->module = control->module;
  2312. cb->min_dump_alloc = control->min_dump_alloc;
  2313. cb->skb = skb;
  2314. nlk->cb_running = true;
  2315. mutex_unlock(nlk->cb_mutex);
  2316. ret = netlink_dump(sk);
  2317. sock_put(sk);
  2318. if (ret)
  2319. return ret;
  2320. /* We successfully started a dump, by returning -EINTR we
  2321. * signal not to send ACK even if it was requested.
  2322. */
  2323. return -EINTR;
  2324. error_unlock:
  2325. sock_put(sk);
  2326. mutex_unlock(nlk->cb_mutex);
  2327. error_free:
  2328. kfree_skb(skb);
  2329. return ret;
  2330. }
  2331. EXPORT_SYMBOL(__netlink_dump_start);
  2332. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  2333. {
  2334. struct sk_buff *skb;
  2335. struct nlmsghdr *rep;
  2336. struct nlmsgerr *errmsg;
  2337. size_t payload = sizeof(*errmsg);
  2338. /* error messages get the original request appened */
  2339. if (err)
  2340. payload += nlmsg_len(nlh);
  2341. skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
  2342. NETLINK_CB(in_skb).portid, GFP_KERNEL);
  2343. if (!skb) {
  2344. struct sock *sk;
  2345. sk = netlink_lookup(sock_net(in_skb->sk),
  2346. in_skb->sk->sk_protocol,
  2347. NETLINK_CB(in_skb).portid);
  2348. if (sk) {
  2349. sk->sk_err = ENOBUFS;
  2350. sk->sk_error_report(sk);
  2351. sock_put(sk);
  2352. }
  2353. return;
  2354. }
  2355. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  2356. NLMSG_ERROR, payload, 0);
  2357. errmsg = nlmsg_data(rep);
  2358. errmsg->error = err;
  2359. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  2360. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  2361. }
  2362. EXPORT_SYMBOL(netlink_ack);
  2363. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  2364. struct nlmsghdr *))
  2365. {
  2366. struct nlmsghdr *nlh;
  2367. int err;
  2368. while (skb->len >= nlmsg_total_size(0)) {
  2369. int msglen;
  2370. nlh = nlmsg_hdr(skb);
  2371. err = 0;
  2372. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  2373. return 0;
  2374. /* Only requests are handled by the kernel */
  2375. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  2376. goto ack;
  2377. /* Skip control messages */
  2378. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  2379. goto ack;
  2380. err = cb(skb, nlh);
  2381. if (err == -EINTR)
  2382. goto skip;
  2383. ack:
  2384. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  2385. netlink_ack(skb, nlh, err);
  2386. skip:
  2387. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  2388. if (msglen > skb->len)
  2389. msglen = skb->len;
  2390. skb_pull(skb, msglen);
  2391. }
  2392. return 0;
  2393. }
  2394. EXPORT_SYMBOL(netlink_rcv_skb);
  2395. /**
  2396. * nlmsg_notify - send a notification netlink message
  2397. * @sk: netlink socket to use
  2398. * @skb: notification message
  2399. * @portid: destination netlink portid for reports or 0
  2400. * @group: destination multicast group or 0
  2401. * @report: 1 to report back, 0 to disable
  2402. * @flags: allocation flags
  2403. */
  2404. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2405. unsigned int group, int report, gfp_t flags)
  2406. {
  2407. int err = 0;
  2408. if (group) {
  2409. int exclude_portid = 0;
  2410. if (report) {
  2411. atomic_inc(&skb->users);
  2412. exclude_portid = portid;
  2413. }
  2414. /* errors reported via destination sk->sk_err, but propagate
  2415. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2416. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2417. }
  2418. if (report) {
  2419. int err2;
  2420. err2 = nlmsg_unicast(sk, skb, portid);
  2421. if (!err || err == -ESRCH)
  2422. err = err2;
  2423. }
  2424. return err;
  2425. }
  2426. EXPORT_SYMBOL(nlmsg_notify);
  2427. #ifdef CONFIG_PROC_FS
  2428. struct nl_seq_iter {
  2429. struct seq_net_private p;
  2430. int link;
  2431. int hash_idx;
  2432. };
  2433. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  2434. {
  2435. struct nl_seq_iter *iter = seq->private;
  2436. int i, j;
  2437. struct netlink_sock *nlk;
  2438. struct sock *s;
  2439. loff_t off = 0;
  2440. for (i = 0; i < MAX_LINKS; i++) {
  2441. struct rhashtable *ht = &nl_table[i].hash;
  2442. const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
  2443. for (j = 0; j < tbl->size; j++) {
  2444. rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) {
  2445. s = (struct sock *)nlk;
  2446. if (sock_net(s) != seq_file_net(seq))
  2447. continue;
  2448. if (off == pos) {
  2449. iter->link = i;
  2450. iter->hash_idx = j;
  2451. return s;
  2452. }
  2453. ++off;
  2454. }
  2455. }
  2456. }
  2457. return NULL;
  2458. }
  2459. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  2460. __acquires(nl_table_lock) __acquires(RCU)
  2461. {
  2462. read_lock(&nl_table_lock);
  2463. rcu_read_lock();
  2464. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2465. }
  2466. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2467. {
  2468. struct rhashtable *ht;
  2469. struct netlink_sock *nlk;
  2470. struct nl_seq_iter *iter;
  2471. struct net *net;
  2472. int i, j;
  2473. ++*pos;
  2474. if (v == SEQ_START_TOKEN)
  2475. return netlink_seq_socket_idx(seq, 0);
  2476. net = seq_file_net(seq);
  2477. iter = seq->private;
  2478. nlk = v;
  2479. i = iter->link;
  2480. ht = &nl_table[i].hash;
  2481. rht_for_each_entry(nlk, nlk->node.next, ht, node)
  2482. if (net_eq(sock_net((struct sock *)nlk), net))
  2483. return nlk;
  2484. j = iter->hash_idx + 1;
  2485. do {
  2486. const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
  2487. for (; j < tbl->size; j++) {
  2488. rht_for_each_entry(nlk, tbl->buckets[j], ht, node) {
  2489. if (net_eq(sock_net((struct sock *)nlk), net)) {
  2490. iter->link = i;
  2491. iter->hash_idx = j;
  2492. return nlk;
  2493. }
  2494. }
  2495. }
  2496. j = 0;
  2497. } while (++i < MAX_LINKS);
  2498. return NULL;
  2499. }
  2500. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2501. __releases(RCU) __releases(nl_table_lock)
  2502. {
  2503. rcu_read_unlock();
  2504. read_unlock(&nl_table_lock);
  2505. }
  2506. static int netlink_seq_show(struct seq_file *seq, void *v)
  2507. {
  2508. if (v == SEQ_START_TOKEN) {
  2509. seq_puts(seq,
  2510. "sk Eth Pid Groups "
  2511. "Rmem Wmem Dump Locks Drops Inode\n");
  2512. } else {
  2513. struct sock *s = v;
  2514. struct netlink_sock *nlk = nlk_sk(s);
  2515. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2516. s,
  2517. s->sk_protocol,
  2518. nlk->portid,
  2519. nlk->groups ? (u32)nlk->groups[0] : 0,
  2520. sk_rmem_alloc_get(s),
  2521. sk_wmem_alloc_get(s),
  2522. nlk->cb_running,
  2523. atomic_read(&s->sk_refcnt),
  2524. atomic_read(&s->sk_drops),
  2525. sock_i_ino(s)
  2526. );
  2527. }
  2528. return 0;
  2529. }
  2530. static const struct seq_operations netlink_seq_ops = {
  2531. .start = netlink_seq_start,
  2532. .next = netlink_seq_next,
  2533. .stop = netlink_seq_stop,
  2534. .show = netlink_seq_show,
  2535. };
  2536. static int netlink_seq_open(struct inode *inode, struct file *file)
  2537. {
  2538. return seq_open_net(inode, file, &netlink_seq_ops,
  2539. sizeof(struct nl_seq_iter));
  2540. }
  2541. static const struct file_operations netlink_seq_fops = {
  2542. .owner = THIS_MODULE,
  2543. .open = netlink_seq_open,
  2544. .read = seq_read,
  2545. .llseek = seq_lseek,
  2546. .release = seq_release_net,
  2547. };
  2548. #endif
  2549. int netlink_register_notifier(struct notifier_block *nb)
  2550. {
  2551. return atomic_notifier_chain_register(&netlink_chain, nb);
  2552. }
  2553. EXPORT_SYMBOL(netlink_register_notifier);
  2554. int netlink_unregister_notifier(struct notifier_block *nb)
  2555. {
  2556. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2557. }
  2558. EXPORT_SYMBOL(netlink_unregister_notifier);
  2559. static const struct proto_ops netlink_ops = {
  2560. .family = PF_NETLINK,
  2561. .owner = THIS_MODULE,
  2562. .release = netlink_release,
  2563. .bind = netlink_bind,
  2564. .connect = netlink_connect,
  2565. .socketpair = sock_no_socketpair,
  2566. .accept = sock_no_accept,
  2567. .getname = netlink_getname,
  2568. .poll = netlink_poll,
  2569. .ioctl = sock_no_ioctl,
  2570. .listen = sock_no_listen,
  2571. .shutdown = sock_no_shutdown,
  2572. .setsockopt = netlink_setsockopt,
  2573. .getsockopt = netlink_getsockopt,
  2574. .sendmsg = netlink_sendmsg,
  2575. .recvmsg = netlink_recvmsg,
  2576. .mmap = netlink_mmap,
  2577. .sendpage = sock_no_sendpage,
  2578. };
  2579. static const struct net_proto_family netlink_family_ops = {
  2580. .family = PF_NETLINK,
  2581. .create = netlink_create,
  2582. .owner = THIS_MODULE, /* for consistency 8) */
  2583. };
  2584. static int __net_init netlink_net_init(struct net *net)
  2585. {
  2586. #ifdef CONFIG_PROC_FS
  2587. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2588. return -ENOMEM;
  2589. #endif
  2590. return 0;
  2591. }
  2592. static void __net_exit netlink_net_exit(struct net *net)
  2593. {
  2594. #ifdef CONFIG_PROC_FS
  2595. remove_proc_entry("netlink", net->proc_net);
  2596. #endif
  2597. }
  2598. static void __init netlink_add_usersock_entry(void)
  2599. {
  2600. struct listeners *listeners;
  2601. int groups = 32;
  2602. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2603. if (!listeners)
  2604. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2605. netlink_table_grab();
  2606. nl_table[NETLINK_USERSOCK].groups = groups;
  2607. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2608. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2609. nl_table[NETLINK_USERSOCK].registered = 1;
  2610. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2611. netlink_table_ungrab();
  2612. }
  2613. static struct pernet_operations __net_initdata netlink_net_ops = {
  2614. .init = netlink_net_init,
  2615. .exit = netlink_net_exit,
  2616. };
  2617. static int __init netlink_proto_init(void)
  2618. {
  2619. int i;
  2620. int err = proto_register(&netlink_proto, 0);
  2621. struct rhashtable_params ht_params = {
  2622. .head_offset = offsetof(struct netlink_sock, node),
  2623. .key_offset = offsetof(struct netlink_sock, portid),
  2624. .key_len = sizeof(u32), /* portid */
  2625. .hashfn = arch_fast_hash,
  2626. .max_shift = 16, /* 64K */
  2627. .grow_decision = rht_grow_above_75,
  2628. .shrink_decision = rht_shrink_below_30,
  2629. .mutex_is_held = lockdep_nl_sk_hash_is_held,
  2630. };
  2631. if (err != 0)
  2632. goto out;
  2633. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2634. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2635. if (!nl_table)
  2636. goto panic;
  2637. for (i = 0; i < MAX_LINKS; i++) {
  2638. if (rhashtable_init(&nl_table[i].hash, &ht_params) < 0) {
  2639. while (--i > 0)
  2640. rhashtable_destroy(&nl_table[i].hash);
  2641. kfree(nl_table);
  2642. goto panic;
  2643. }
  2644. }
  2645. INIT_LIST_HEAD(&netlink_tap_all);
  2646. netlink_add_usersock_entry();
  2647. sock_register(&netlink_family_ops);
  2648. register_pernet_subsys(&netlink_net_ops);
  2649. /* The netlink device handler may be needed early. */
  2650. rtnetlink_init();
  2651. out:
  2652. return err;
  2653. panic:
  2654. panic("netlink_init: Cannot allocate nl_table\n");
  2655. }
  2656. core_initcall(netlink_proto_init);