af_netlink.c 67 KB

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