af_netlink.c 74 KB

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