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