af_netlink.c 74 KB

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