af_netlink.c 74 KB

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