af_netlink.c 78 KB

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