af_netlink.c 70 KB

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