af_netlink.c 75 KB

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