af_netlink.c 67 KB

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