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 __read_mostly;
  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. nlk_sk(sk)->portid = 0;
  897. sock_put(sk);
  898. }
  899. err:
  900. release_sock(sk);
  901. return err;
  902. }
  903. static void netlink_remove(struct sock *sk)
  904. {
  905. struct netlink_table *table;
  906. table = &nl_table[sk->sk_protocol];
  907. if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
  908. netlink_rhashtable_params)) {
  909. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  910. __sock_put(sk);
  911. }
  912. netlink_table_grab();
  913. if (nlk_sk(sk)->subscriptions) {
  914. __sk_del_bind_node(sk);
  915. netlink_update_listeners(sk);
  916. }
  917. if (sk->sk_protocol == NETLINK_GENERIC)
  918. atomic_inc(&genl_sk_destructing_cnt);
  919. netlink_table_ungrab();
  920. }
  921. static struct proto netlink_proto = {
  922. .name = "NETLINK",
  923. .owner = THIS_MODULE,
  924. .obj_size = sizeof(struct netlink_sock),
  925. };
  926. static int __netlink_create(struct net *net, struct socket *sock,
  927. struct mutex *cb_mutex, int protocol)
  928. {
  929. struct sock *sk;
  930. struct netlink_sock *nlk;
  931. sock->ops = &netlink_ops;
  932. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  933. if (!sk)
  934. return -ENOMEM;
  935. sock_init_data(sock, sk);
  936. nlk = nlk_sk(sk);
  937. if (cb_mutex) {
  938. nlk->cb_mutex = cb_mutex;
  939. } else {
  940. nlk->cb_mutex = &nlk->cb_def_mutex;
  941. mutex_init(nlk->cb_mutex);
  942. }
  943. init_waitqueue_head(&nlk->wait);
  944. #ifdef CONFIG_NETLINK_MMAP
  945. mutex_init(&nlk->pg_vec_lock);
  946. #endif
  947. sk->sk_destruct = netlink_sock_destruct;
  948. sk->sk_protocol = protocol;
  949. return 0;
  950. }
  951. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  952. int kern)
  953. {
  954. struct module *module = NULL;
  955. struct mutex *cb_mutex;
  956. struct netlink_sock *nlk;
  957. int (*bind)(struct net *net, int group);
  958. void (*unbind)(struct net *net, int group);
  959. int err = 0;
  960. sock->state = SS_UNCONNECTED;
  961. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  962. return -ESOCKTNOSUPPORT;
  963. if (protocol < 0 || protocol >= MAX_LINKS)
  964. return -EPROTONOSUPPORT;
  965. netlink_lock_table();
  966. #ifdef CONFIG_MODULES
  967. if (!nl_table[protocol].registered) {
  968. netlink_unlock_table();
  969. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  970. netlink_lock_table();
  971. }
  972. #endif
  973. if (nl_table[protocol].registered &&
  974. try_module_get(nl_table[protocol].module))
  975. module = nl_table[protocol].module;
  976. else
  977. err = -EPROTONOSUPPORT;
  978. cb_mutex = nl_table[protocol].cb_mutex;
  979. bind = nl_table[protocol].bind;
  980. unbind = nl_table[protocol].unbind;
  981. netlink_unlock_table();
  982. if (err < 0)
  983. goto out;
  984. err = __netlink_create(net, sock, cb_mutex, protocol);
  985. if (err < 0)
  986. goto out_module;
  987. local_bh_disable();
  988. sock_prot_inuse_add(net, &netlink_proto, 1);
  989. local_bh_enable();
  990. nlk = nlk_sk(sock->sk);
  991. nlk->module = module;
  992. nlk->netlink_bind = bind;
  993. nlk->netlink_unbind = unbind;
  994. out:
  995. return err;
  996. out_module:
  997. module_put(module);
  998. goto out;
  999. }
  1000. static void deferred_put_nlk_sk(struct rcu_head *head)
  1001. {
  1002. struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
  1003. sock_put(&nlk->sk);
  1004. }
  1005. static int netlink_release(struct socket *sock)
  1006. {
  1007. struct sock *sk = sock->sk;
  1008. struct netlink_sock *nlk;
  1009. if (!sk)
  1010. return 0;
  1011. netlink_remove(sk);
  1012. sock_orphan(sk);
  1013. nlk = nlk_sk(sk);
  1014. /*
  1015. * OK. Socket is unlinked, any packets that arrive now
  1016. * will be purged.
  1017. */
  1018. /* must not acquire netlink_table_lock in any way again before unbind
  1019. * and notifying genetlink is done as otherwise it might deadlock
  1020. */
  1021. if (nlk->netlink_unbind) {
  1022. int i;
  1023. for (i = 0; i < nlk->ngroups; i++)
  1024. if (test_bit(i, nlk->groups))
  1025. nlk->netlink_unbind(sock_net(sk), i + 1);
  1026. }
  1027. if (sk->sk_protocol == NETLINK_GENERIC &&
  1028. atomic_dec_return(&genl_sk_destructing_cnt) == 0)
  1029. wake_up(&genl_sk_destructing_waitq);
  1030. sock->sk = NULL;
  1031. wake_up_interruptible_all(&nlk->wait);
  1032. skb_queue_purge(&sk->sk_write_queue);
  1033. if (nlk->portid) {
  1034. struct netlink_notify n = {
  1035. .net = sock_net(sk),
  1036. .protocol = sk->sk_protocol,
  1037. .portid = nlk->portid,
  1038. };
  1039. atomic_notifier_call_chain(&netlink_chain,
  1040. NETLINK_URELEASE, &n);
  1041. }
  1042. module_put(nlk->module);
  1043. if (netlink_is_kernel(sk)) {
  1044. netlink_table_grab();
  1045. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  1046. if (--nl_table[sk->sk_protocol].registered == 0) {
  1047. struct listeners *old;
  1048. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  1049. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  1050. kfree_rcu(old, rcu);
  1051. nl_table[sk->sk_protocol].module = NULL;
  1052. nl_table[sk->sk_protocol].bind = NULL;
  1053. nl_table[sk->sk_protocol].unbind = NULL;
  1054. nl_table[sk->sk_protocol].flags = 0;
  1055. nl_table[sk->sk_protocol].registered = 0;
  1056. }
  1057. netlink_table_ungrab();
  1058. }
  1059. kfree(nlk->groups);
  1060. nlk->groups = NULL;
  1061. local_bh_disable();
  1062. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  1063. local_bh_enable();
  1064. call_rcu(&nlk->rcu, deferred_put_nlk_sk);
  1065. return 0;
  1066. }
  1067. static int netlink_autobind(struct socket *sock)
  1068. {
  1069. struct sock *sk = sock->sk;
  1070. struct net *net = sock_net(sk);
  1071. struct netlink_table *table = &nl_table[sk->sk_protocol];
  1072. s32 portid = task_tgid_vnr(current);
  1073. int err;
  1074. static s32 rover = -4097;
  1075. retry:
  1076. cond_resched();
  1077. rcu_read_lock();
  1078. if (__netlink_lookup(table, portid, net)) {
  1079. /* Bind collision, search negative portid values. */
  1080. portid = rover--;
  1081. if (rover > -4097)
  1082. rover = -4097;
  1083. rcu_read_unlock();
  1084. goto retry;
  1085. }
  1086. rcu_read_unlock();
  1087. err = netlink_insert(sk, portid);
  1088. if (err == -EADDRINUSE)
  1089. goto retry;
  1090. /* If 2 threads race to autobind, that is fine. */
  1091. if (err == -EBUSY)
  1092. err = 0;
  1093. return err;
  1094. }
  1095. /**
  1096. * __netlink_ns_capable - General netlink message capability test
  1097. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  1098. * @user_ns: The user namespace of the capability to use
  1099. * @cap: The capability to use
  1100. *
  1101. * Test to see if the opener of the socket we received the message
  1102. * from had when the netlink socket was created and the sender of the
  1103. * message has has the capability @cap in the user namespace @user_ns.
  1104. */
  1105. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  1106. struct user_namespace *user_ns, int cap)
  1107. {
  1108. return ((nsp->flags & NETLINK_SKB_DST) ||
  1109. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  1110. ns_capable(user_ns, cap);
  1111. }
  1112. EXPORT_SYMBOL(__netlink_ns_capable);
  1113. /**
  1114. * netlink_ns_capable - General netlink message capability test
  1115. * @skb: socket buffer holding a netlink command from userspace
  1116. * @user_ns: The user namespace of the capability to use
  1117. * @cap: The capability to use
  1118. *
  1119. * Test to see if the opener of the socket we received the message
  1120. * from had when the netlink socket was created and the sender of the
  1121. * message has has the capability @cap in the user namespace @user_ns.
  1122. */
  1123. bool netlink_ns_capable(const struct sk_buff *skb,
  1124. struct user_namespace *user_ns, int cap)
  1125. {
  1126. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  1127. }
  1128. EXPORT_SYMBOL(netlink_ns_capable);
  1129. /**
  1130. * netlink_capable - Netlink global message capability test
  1131. * @skb: socket buffer holding a netlink command from userspace
  1132. * @cap: The capability to use
  1133. *
  1134. * Test to see if the opener of the socket we received the message
  1135. * from had when the netlink socket was created and the sender of the
  1136. * message has has the capability @cap in all user namespaces.
  1137. */
  1138. bool netlink_capable(const struct sk_buff *skb, int cap)
  1139. {
  1140. return netlink_ns_capable(skb, &init_user_ns, cap);
  1141. }
  1142. EXPORT_SYMBOL(netlink_capable);
  1143. /**
  1144. * netlink_net_capable - Netlink network namespace message capability test
  1145. * @skb: socket buffer holding a netlink command from userspace
  1146. * @cap: The capability to use
  1147. *
  1148. * Test to see if the opener of the socket we received the message
  1149. * from had when the netlink socket was created and the sender of the
  1150. * message has has the capability @cap over the network namespace of
  1151. * the socket we received the message from.
  1152. */
  1153. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  1154. {
  1155. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  1156. }
  1157. EXPORT_SYMBOL(netlink_net_capable);
  1158. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  1159. {
  1160. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  1161. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  1162. }
  1163. static void
  1164. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  1165. {
  1166. struct netlink_sock *nlk = nlk_sk(sk);
  1167. if (nlk->subscriptions && !subscriptions)
  1168. __sk_del_bind_node(sk);
  1169. else if (!nlk->subscriptions && subscriptions)
  1170. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  1171. nlk->subscriptions = subscriptions;
  1172. }
  1173. static int netlink_realloc_groups(struct sock *sk)
  1174. {
  1175. struct netlink_sock *nlk = nlk_sk(sk);
  1176. unsigned int groups;
  1177. unsigned long *new_groups;
  1178. int err = 0;
  1179. netlink_table_grab();
  1180. groups = nl_table[sk->sk_protocol].groups;
  1181. if (!nl_table[sk->sk_protocol].registered) {
  1182. err = -ENOENT;
  1183. goto out_unlock;
  1184. }
  1185. if (nlk->ngroups >= groups)
  1186. goto out_unlock;
  1187. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  1188. if (new_groups == NULL) {
  1189. err = -ENOMEM;
  1190. goto out_unlock;
  1191. }
  1192. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  1193. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  1194. nlk->groups = new_groups;
  1195. nlk->ngroups = groups;
  1196. out_unlock:
  1197. netlink_table_ungrab();
  1198. return err;
  1199. }
  1200. static void netlink_undo_bind(int group, long unsigned int groups,
  1201. struct sock *sk)
  1202. {
  1203. struct netlink_sock *nlk = nlk_sk(sk);
  1204. int undo;
  1205. if (!nlk->netlink_unbind)
  1206. return;
  1207. for (undo = 0; undo < group; undo++)
  1208. if (test_bit(undo, &groups))
  1209. nlk->netlink_unbind(sock_net(sk), undo + 1);
  1210. }
  1211. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  1212. int addr_len)
  1213. {
  1214. struct sock *sk = sock->sk;
  1215. struct net *net = sock_net(sk);
  1216. struct netlink_sock *nlk = nlk_sk(sk);
  1217. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1218. int err;
  1219. long unsigned int groups = nladdr->nl_groups;
  1220. if (addr_len < sizeof(struct sockaddr_nl))
  1221. return -EINVAL;
  1222. if (nladdr->nl_family != AF_NETLINK)
  1223. return -EINVAL;
  1224. /* Only superuser is allowed to listen multicasts */
  1225. if (groups) {
  1226. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1227. return -EPERM;
  1228. err = netlink_realloc_groups(sk);
  1229. if (err)
  1230. return err;
  1231. }
  1232. if (nlk->portid)
  1233. if (nladdr->nl_pid != nlk->portid)
  1234. return -EINVAL;
  1235. if (nlk->netlink_bind && groups) {
  1236. int group;
  1237. for (group = 0; group < nlk->ngroups; group++) {
  1238. if (!test_bit(group, &groups))
  1239. continue;
  1240. err = nlk->netlink_bind(net, group + 1);
  1241. if (!err)
  1242. continue;
  1243. netlink_undo_bind(group, groups, sk);
  1244. return err;
  1245. }
  1246. }
  1247. if (!nlk->portid) {
  1248. err = nladdr->nl_pid ?
  1249. netlink_insert(sk, nladdr->nl_pid) :
  1250. netlink_autobind(sock);
  1251. if (err) {
  1252. netlink_undo_bind(nlk->ngroups, groups, sk);
  1253. return err;
  1254. }
  1255. }
  1256. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  1257. return 0;
  1258. netlink_table_grab();
  1259. netlink_update_subscriptions(sk, nlk->subscriptions +
  1260. hweight32(groups) -
  1261. hweight32(nlk->groups[0]));
  1262. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  1263. netlink_update_listeners(sk);
  1264. netlink_table_ungrab();
  1265. return 0;
  1266. }
  1267. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  1268. int alen, int flags)
  1269. {
  1270. int err = 0;
  1271. struct sock *sk = sock->sk;
  1272. struct netlink_sock *nlk = nlk_sk(sk);
  1273. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1274. if (alen < sizeof(addr->sa_family))
  1275. return -EINVAL;
  1276. if (addr->sa_family == AF_UNSPEC) {
  1277. sk->sk_state = NETLINK_UNCONNECTED;
  1278. nlk->dst_portid = 0;
  1279. nlk->dst_group = 0;
  1280. return 0;
  1281. }
  1282. if (addr->sa_family != AF_NETLINK)
  1283. return -EINVAL;
  1284. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  1285. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1286. return -EPERM;
  1287. if (!nlk->portid)
  1288. err = netlink_autobind(sock);
  1289. if (err == 0) {
  1290. sk->sk_state = NETLINK_CONNECTED;
  1291. nlk->dst_portid = nladdr->nl_pid;
  1292. nlk->dst_group = ffs(nladdr->nl_groups);
  1293. }
  1294. return err;
  1295. }
  1296. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  1297. int *addr_len, int peer)
  1298. {
  1299. struct sock *sk = sock->sk;
  1300. struct netlink_sock *nlk = nlk_sk(sk);
  1301. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  1302. nladdr->nl_family = AF_NETLINK;
  1303. nladdr->nl_pad = 0;
  1304. *addr_len = sizeof(*nladdr);
  1305. if (peer) {
  1306. nladdr->nl_pid = nlk->dst_portid;
  1307. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  1308. } else {
  1309. nladdr->nl_pid = nlk->portid;
  1310. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  1311. }
  1312. return 0;
  1313. }
  1314. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  1315. {
  1316. struct sock *sock;
  1317. struct netlink_sock *nlk;
  1318. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  1319. if (!sock)
  1320. return ERR_PTR(-ECONNREFUSED);
  1321. /* Don't bother queuing skb if kernel socket has no input function */
  1322. nlk = nlk_sk(sock);
  1323. if (sock->sk_state == NETLINK_CONNECTED &&
  1324. nlk->dst_portid != nlk_sk(ssk)->portid) {
  1325. sock_put(sock);
  1326. return ERR_PTR(-ECONNREFUSED);
  1327. }
  1328. return sock;
  1329. }
  1330. struct sock *netlink_getsockbyfilp(struct file *filp)
  1331. {
  1332. struct inode *inode = file_inode(filp);
  1333. struct sock *sock;
  1334. if (!S_ISSOCK(inode->i_mode))
  1335. return ERR_PTR(-ENOTSOCK);
  1336. sock = SOCKET_I(inode)->sk;
  1337. if (sock->sk_family != AF_NETLINK)
  1338. return ERR_PTR(-EINVAL);
  1339. sock_hold(sock);
  1340. return sock;
  1341. }
  1342. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  1343. int broadcast)
  1344. {
  1345. struct sk_buff *skb;
  1346. void *data;
  1347. if (size <= NLMSG_GOODSIZE || broadcast)
  1348. return alloc_skb(size, GFP_KERNEL);
  1349. size = SKB_DATA_ALIGN(size) +
  1350. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1351. data = vmalloc(size);
  1352. if (data == NULL)
  1353. return NULL;
  1354. skb = __build_skb(data, size);
  1355. if (skb == NULL)
  1356. vfree(data);
  1357. else
  1358. skb->destructor = netlink_skb_destructor;
  1359. return skb;
  1360. }
  1361. /*
  1362. * Attach a skb to a netlink socket.
  1363. * The caller must hold a reference to the destination socket. On error, the
  1364. * reference is dropped. The skb is not send to the destination, just all
  1365. * all error checks are performed and memory in the queue is reserved.
  1366. * Return values:
  1367. * < 0: error. skb freed, reference to sock dropped.
  1368. * 0: continue
  1369. * 1: repeat lookup - reference dropped while waiting for socket memory.
  1370. */
  1371. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  1372. long *timeo, struct sock *ssk)
  1373. {
  1374. struct netlink_sock *nlk;
  1375. nlk = nlk_sk(sk);
  1376. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1377. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1378. !netlink_skb_is_mmaped(skb)) {
  1379. DECLARE_WAITQUEUE(wait, current);
  1380. if (!*timeo) {
  1381. if (!ssk || netlink_is_kernel(ssk))
  1382. netlink_overrun(sk);
  1383. sock_put(sk);
  1384. kfree_skb(skb);
  1385. return -EAGAIN;
  1386. }
  1387. __set_current_state(TASK_INTERRUPTIBLE);
  1388. add_wait_queue(&nlk->wait, &wait);
  1389. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1390. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1391. !sock_flag(sk, SOCK_DEAD))
  1392. *timeo = schedule_timeout(*timeo);
  1393. __set_current_state(TASK_RUNNING);
  1394. remove_wait_queue(&nlk->wait, &wait);
  1395. sock_put(sk);
  1396. if (signal_pending(current)) {
  1397. kfree_skb(skb);
  1398. return sock_intr_errno(*timeo);
  1399. }
  1400. return 1;
  1401. }
  1402. netlink_skb_set_owner_r(skb, sk);
  1403. return 0;
  1404. }
  1405. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1406. {
  1407. int len = skb->len;
  1408. netlink_deliver_tap(skb);
  1409. #ifdef CONFIG_NETLINK_MMAP
  1410. if (netlink_skb_is_mmaped(skb))
  1411. netlink_queue_mmaped_skb(sk, skb);
  1412. else if (netlink_rx_is_mmaped(sk))
  1413. netlink_ring_set_copied(sk, skb);
  1414. else
  1415. #endif /* CONFIG_NETLINK_MMAP */
  1416. skb_queue_tail(&sk->sk_receive_queue, skb);
  1417. sk->sk_data_ready(sk);
  1418. return len;
  1419. }
  1420. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1421. {
  1422. int len = __netlink_sendskb(sk, skb);
  1423. sock_put(sk);
  1424. return len;
  1425. }
  1426. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1427. {
  1428. kfree_skb(skb);
  1429. sock_put(sk);
  1430. }
  1431. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1432. {
  1433. int delta;
  1434. WARN_ON(skb->sk != NULL);
  1435. if (netlink_skb_is_mmaped(skb))
  1436. return skb;
  1437. delta = skb->end - skb->tail;
  1438. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1439. return skb;
  1440. if (skb_shared(skb)) {
  1441. struct sk_buff *nskb = skb_clone(skb, allocation);
  1442. if (!nskb)
  1443. return skb;
  1444. consume_skb(skb);
  1445. skb = nskb;
  1446. }
  1447. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1448. skb->truesize -= delta;
  1449. return skb;
  1450. }
  1451. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1452. struct sock *ssk)
  1453. {
  1454. int ret;
  1455. struct netlink_sock *nlk = nlk_sk(sk);
  1456. ret = -ECONNREFUSED;
  1457. if (nlk->netlink_rcv != NULL) {
  1458. ret = skb->len;
  1459. netlink_skb_set_owner_r(skb, sk);
  1460. NETLINK_CB(skb).sk = ssk;
  1461. netlink_deliver_tap_kernel(sk, ssk, skb);
  1462. nlk->netlink_rcv(skb);
  1463. consume_skb(skb);
  1464. } else {
  1465. kfree_skb(skb);
  1466. }
  1467. sock_put(sk);
  1468. return ret;
  1469. }
  1470. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1471. u32 portid, int nonblock)
  1472. {
  1473. struct sock *sk;
  1474. int err;
  1475. long timeo;
  1476. skb = netlink_trim(skb, gfp_any());
  1477. timeo = sock_sndtimeo(ssk, nonblock);
  1478. retry:
  1479. sk = netlink_getsockbyportid(ssk, portid);
  1480. if (IS_ERR(sk)) {
  1481. kfree_skb(skb);
  1482. return PTR_ERR(sk);
  1483. }
  1484. if (netlink_is_kernel(sk))
  1485. return netlink_unicast_kernel(sk, skb, ssk);
  1486. if (sk_filter(sk, skb)) {
  1487. err = skb->len;
  1488. kfree_skb(skb);
  1489. sock_put(sk);
  1490. return err;
  1491. }
  1492. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1493. if (err == 1)
  1494. goto retry;
  1495. if (err)
  1496. return err;
  1497. return netlink_sendskb(sk, skb);
  1498. }
  1499. EXPORT_SYMBOL(netlink_unicast);
  1500. struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
  1501. u32 dst_portid, gfp_t gfp_mask)
  1502. {
  1503. #ifdef CONFIG_NETLINK_MMAP
  1504. struct sock *sk = NULL;
  1505. struct sk_buff *skb;
  1506. struct netlink_ring *ring;
  1507. struct nl_mmap_hdr *hdr;
  1508. unsigned int maxlen;
  1509. sk = netlink_getsockbyportid(ssk, dst_portid);
  1510. if (IS_ERR(sk))
  1511. goto out;
  1512. ring = &nlk_sk(sk)->rx_ring;
  1513. /* fast-path without atomic ops for common case: non-mmaped receiver */
  1514. if (ring->pg_vec == NULL)
  1515. goto out_put;
  1516. if (ring->frame_size - NL_MMAP_HDRLEN < size)
  1517. goto out_put;
  1518. skb = alloc_skb_head(gfp_mask);
  1519. if (skb == NULL)
  1520. goto err1;
  1521. spin_lock_bh(&sk->sk_receive_queue.lock);
  1522. /* check again under lock */
  1523. if (ring->pg_vec == NULL)
  1524. goto out_free;
  1525. /* check again under lock */
  1526. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  1527. if (maxlen < size)
  1528. goto out_free;
  1529. netlink_forward_ring(ring);
  1530. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  1531. if (hdr == NULL)
  1532. goto err2;
  1533. netlink_ring_setup_skb(skb, sk, ring, hdr);
  1534. netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
  1535. atomic_inc(&ring->pending);
  1536. netlink_increment_head(ring);
  1537. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1538. return skb;
  1539. err2:
  1540. kfree_skb(skb);
  1541. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1542. netlink_overrun(sk);
  1543. err1:
  1544. sock_put(sk);
  1545. return NULL;
  1546. out_free:
  1547. kfree_skb(skb);
  1548. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1549. out_put:
  1550. sock_put(sk);
  1551. out:
  1552. #endif
  1553. return alloc_skb(size, gfp_mask);
  1554. }
  1555. EXPORT_SYMBOL_GPL(netlink_alloc_skb);
  1556. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1557. {
  1558. int res = 0;
  1559. struct listeners *listeners;
  1560. BUG_ON(!netlink_is_kernel(sk));
  1561. rcu_read_lock();
  1562. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1563. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1564. res = test_bit(group - 1, listeners->masks);
  1565. rcu_read_unlock();
  1566. return res;
  1567. }
  1568. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1569. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1570. {
  1571. struct netlink_sock *nlk = nlk_sk(sk);
  1572. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1573. !test_bit(NETLINK_CONGESTED, &nlk->state)) {
  1574. netlink_skb_set_owner_r(skb, sk);
  1575. __netlink_sendskb(sk, skb);
  1576. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1577. }
  1578. return -1;
  1579. }
  1580. struct netlink_broadcast_data {
  1581. struct sock *exclude_sk;
  1582. struct net *net;
  1583. u32 portid;
  1584. u32 group;
  1585. int failure;
  1586. int delivery_failure;
  1587. int congested;
  1588. int delivered;
  1589. gfp_t allocation;
  1590. struct sk_buff *skb, *skb2;
  1591. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1592. void *tx_data;
  1593. };
  1594. static void do_one_broadcast(struct sock *sk,
  1595. struct netlink_broadcast_data *p)
  1596. {
  1597. struct netlink_sock *nlk = nlk_sk(sk);
  1598. int val;
  1599. if (p->exclude_sk == sk)
  1600. return;
  1601. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1602. !test_bit(p->group - 1, nlk->groups))
  1603. return;
  1604. if (!net_eq(sock_net(sk), p->net))
  1605. return;
  1606. if (p->failure) {
  1607. netlink_overrun(sk);
  1608. return;
  1609. }
  1610. sock_hold(sk);
  1611. if (p->skb2 == NULL) {
  1612. if (skb_shared(p->skb)) {
  1613. p->skb2 = skb_clone(p->skb, p->allocation);
  1614. } else {
  1615. p->skb2 = skb_get(p->skb);
  1616. /*
  1617. * skb ownership may have been set when
  1618. * delivered to a previous socket.
  1619. */
  1620. skb_orphan(p->skb2);
  1621. }
  1622. }
  1623. if (p->skb2 == NULL) {
  1624. netlink_overrun(sk);
  1625. /* Clone failed. Notify ALL listeners. */
  1626. p->failure = 1;
  1627. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1628. p->delivery_failure = 1;
  1629. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1630. kfree_skb(p->skb2);
  1631. p->skb2 = NULL;
  1632. } else if (sk_filter(sk, p->skb2)) {
  1633. kfree_skb(p->skb2);
  1634. p->skb2 = NULL;
  1635. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  1636. netlink_overrun(sk);
  1637. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1638. p->delivery_failure = 1;
  1639. } else {
  1640. p->congested |= val;
  1641. p->delivered = 1;
  1642. p->skb2 = NULL;
  1643. }
  1644. sock_put(sk);
  1645. }
  1646. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1647. u32 group, gfp_t allocation,
  1648. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1649. void *filter_data)
  1650. {
  1651. struct net *net = sock_net(ssk);
  1652. struct netlink_broadcast_data info;
  1653. struct sock *sk;
  1654. skb = netlink_trim(skb, allocation);
  1655. info.exclude_sk = ssk;
  1656. info.net = net;
  1657. info.portid = portid;
  1658. info.group = group;
  1659. info.failure = 0;
  1660. info.delivery_failure = 0;
  1661. info.congested = 0;
  1662. info.delivered = 0;
  1663. info.allocation = allocation;
  1664. info.skb = skb;
  1665. info.skb2 = NULL;
  1666. info.tx_filter = filter;
  1667. info.tx_data = filter_data;
  1668. /* While we sleep in clone, do not allow to change socket list */
  1669. netlink_lock_table();
  1670. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1671. do_one_broadcast(sk, &info);
  1672. consume_skb(skb);
  1673. netlink_unlock_table();
  1674. if (info.delivery_failure) {
  1675. kfree_skb(info.skb2);
  1676. return -ENOBUFS;
  1677. }
  1678. consume_skb(info.skb2);
  1679. if (info.delivered) {
  1680. if (info.congested && (allocation & __GFP_WAIT))
  1681. yield();
  1682. return 0;
  1683. }
  1684. return -ESRCH;
  1685. }
  1686. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1687. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1688. u32 group, gfp_t allocation)
  1689. {
  1690. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1691. NULL, NULL);
  1692. }
  1693. EXPORT_SYMBOL(netlink_broadcast);
  1694. struct netlink_set_err_data {
  1695. struct sock *exclude_sk;
  1696. u32 portid;
  1697. u32 group;
  1698. int code;
  1699. };
  1700. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1701. {
  1702. struct netlink_sock *nlk = nlk_sk(sk);
  1703. int ret = 0;
  1704. if (sk == p->exclude_sk)
  1705. goto out;
  1706. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1707. goto out;
  1708. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1709. !test_bit(p->group - 1, nlk->groups))
  1710. goto out;
  1711. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  1712. ret = 1;
  1713. goto out;
  1714. }
  1715. sk->sk_err = p->code;
  1716. sk->sk_error_report(sk);
  1717. out:
  1718. return ret;
  1719. }
  1720. /**
  1721. * netlink_set_err - report error to broadcast listeners
  1722. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1723. * @portid: the PORTID of a process that we want to skip (if any)
  1724. * @group: the broadcast group that will notice the error
  1725. * @code: error code, must be negative (as usual in kernelspace)
  1726. *
  1727. * This function returns the number of broadcast listeners that have set the
  1728. * NETLINK_RECV_NO_ENOBUFS socket option.
  1729. */
  1730. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1731. {
  1732. struct netlink_set_err_data info;
  1733. struct sock *sk;
  1734. int ret = 0;
  1735. info.exclude_sk = ssk;
  1736. info.portid = portid;
  1737. info.group = group;
  1738. /* sk->sk_err wants a positive error value */
  1739. info.code = -code;
  1740. read_lock(&nl_table_lock);
  1741. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1742. ret += do_one_set_err(sk, &info);
  1743. read_unlock(&nl_table_lock);
  1744. return ret;
  1745. }
  1746. EXPORT_SYMBOL(netlink_set_err);
  1747. /* must be called with netlink table grabbed */
  1748. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1749. unsigned int group,
  1750. int is_new)
  1751. {
  1752. int old, new = !!is_new, subscriptions;
  1753. old = test_bit(group - 1, nlk->groups);
  1754. subscriptions = nlk->subscriptions - old + new;
  1755. if (new)
  1756. __set_bit(group - 1, nlk->groups);
  1757. else
  1758. __clear_bit(group - 1, nlk->groups);
  1759. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1760. netlink_update_listeners(&nlk->sk);
  1761. }
  1762. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1763. char __user *optval, unsigned int optlen)
  1764. {
  1765. struct sock *sk = sock->sk;
  1766. struct netlink_sock *nlk = nlk_sk(sk);
  1767. unsigned int val = 0;
  1768. int err;
  1769. if (level != SOL_NETLINK)
  1770. return -ENOPROTOOPT;
  1771. if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
  1772. optlen >= sizeof(int) &&
  1773. get_user(val, (unsigned int __user *)optval))
  1774. return -EFAULT;
  1775. switch (optname) {
  1776. case NETLINK_PKTINFO:
  1777. if (val)
  1778. nlk->flags |= NETLINK_RECV_PKTINFO;
  1779. else
  1780. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1781. err = 0;
  1782. break;
  1783. case NETLINK_ADD_MEMBERSHIP:
  1784. case NETLINK_DROP_MEMBERSHIP: {
  1785. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1786. return -EPERM;
  1787. err = netlink_realloc_groups(sk);
  1788. if (err)
  1789. return err;
  1790. if (!val || val - 1 >= nlk->ngroups)
  1791. return -EINVAL;
  1792. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1793. err = nlk->netlink_bind(sock_net(sk), val);
  1794. if (err)
  1795. return err;
  1796. }
  1797. netlink_table_grab();
  1798. netlink_update_socket_mc(nlk, val,
  1799. optname == NETLINK_ADD_MEMBERSHIP);
  1800. netlink_table_ungrab();
  1801. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1802. nlk->netlink_unbind(sock_net(sk), val);
  1803. err = 0;
  1804. break;
  1805. }
  1806. case NETLINK_BROADCAST_ERROR:
  1807. if (val)
  1808. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1809. else
  1810. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1811. err = 0;
  1812. break;
  1813. case NETLINK_NO_ENOBUFS:
  1814. if (val) {
  1815. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1816. clear_bit(NETLINK_CONGESTED, &nlk->state);
  1817. wake_up_interruptible(&nlk->wait);
  1818. } else {
  1819. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1820. }
  1821. err = 0;
  1822. break;
  1823. #ifdef CONFIG_NETLINK_MMAP
  1824. case NETLINK_RX_RING:
  1825. case NETLINK_TX_RING: {
  1826. struct nl_mmap_req req;
  1827. /* Rings might consume more memory than queue limits, require
  1828. * CAP_NET_ADMIN.
  1829. */
  1830. if (!capable(CAP_NET_ADMIN))
  1831. return -EPERM;
  1832. if (optlen < sizeof(req))
  1833. return -EINVAL;
  1834. if (copy_from_user(&req, optval, sizeof(req)))
  1835. return -EFAULT;
  1836. err = netlink_set_ring(sk, &req, false,
  1837. optname == NETLINK_TX_RING);
  1838. break;
  1839. }
  1840. #endif /* CONFIG_NETLINK_MMAP */
  1841. default:
  1842. err = -ENOPROTOOPT;
  1843. }
  1844. return err;
  1845. }
  1846. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1847. char __user *optval, int __user *optlen)
  1848. {
  1849. struct sock *sk = sock->sk;
  1850. struct netlink_sock *nlk = nlk_sk(sk);
  1851. int len, val, err;
  1852. if (level != SOL_NETLINK)
  1853. return -ENOPROTOOPT;
  1854. if (get_user(len, optlen))
  1855. return -EFAULT;
  1856. if (len < 0)
  1857. return -EINVAL;
  1858. switch (optname) {
  1859. case NETLINK_PKTINFO:
  1860. if (len < sizeof(int))
  1861. return -EINVAL;
  1862. len = sizeof(int);
  1863. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1864. if (put_user(len, optlen) ||
  1865. put_user(val, optval))
  1866. return -EFAULT;
  1867. err = 0;
  1868. break;
  1869. case NETLINK_BROADCAST_ERROR:
  1870. if (len < sizeof(int))
  1871. return -EINVAL;
  1872. len = sizeof(int);
  1873. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1874. if (put_user(len, optlen) ||
  1875. put_user(val, optval))
  1876. return -EFAULT;
  1877. err = 0;
  1878. break;
  1879. case NETLINK_NO_ENOBUFS:
  1880. if (len < sizeof(int))
  1881. return -EINVAL;
  1882. len = sizeof(int);
  1883. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1884. if (put_user(len, optlen) ||
  1885. put_user(val, optval))
  1886. return -EFAULT;
  1887. err = 0;
  1888. break;
  1889. default:
  1890. err = -ENOPROTOOPT;
  1891. }
  1892. return err;
  1893. }
  1894. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1895. {
  1896. struct nl_pktinfo info;
  1897. info.group = NETLINK_CB(skb).dst_group;
  1898. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1899. }
  1900. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1901. {
  1902. struct sock *sk = sock->sk;
  1903. struct netlink_sock *nlk = nlk_sk(sk);
  1904. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1905. u32 dst_portid;
  1906. u32 dst_group;
  1907. struct sk_buff *skb;
  1908. int err;
  1909. struct scm_cookie scm;
  1910. u32 netlink_skb_flags = 0;
  1911. if (msg->msg_flags&MSG_OOB)
  1912. return -EOPNOTSUPP;
  1913. err = scm_send(sock, msg, &scm, true);
  1914. if (err < 0)
  1915. return err;
  1916. if (msg->msg_namelen) {
  1917. err = -EINVAL;
  1918. if (addr->nl_family != AF_NETLINK)
  1919. goto out;
  1920. dst_portid = addr->nl_pid;
  1921. dst_group = ffs(addr->nl_groups);
  1922. err = -EPERM;
  1923. if ((dst_group || dst_portid) &&
  1924. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1925. goto out;
  1926. netlink_skb_flags |= NETLINK_SKB_DST;
  1927. } else {
  1928. dst_portid = nlk->dst_portid;
  1929. dst_group = nlk->dst_group;
  1930. }
  1931. if (!nlk->portid) {
  1932. err = netlink_autobind(sock);
  1933. if (err)
  1934. goto out;
  1935. }
  1936. /* It's a really convoluted way for userland to ask for mmaped
  1937. * sendmsg(), but that's what we've got...
  1938. */
  1939. if (netlink_tx_is_mmaped(sk) &&
  1940. msg->msg_iter.type == ITER_IOVEC &&
  1941. msg->msg_iter.nr_segs == 1 &&
  1942. msg->msg_iter.iov->iov_base == NULL) {
  1943. err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
  1944. &scm);
  1945. goto out;
  1946. }
  1947. err = -EMSGSIZE;
  1948. if (len > sk->sk_sndbuf - 32)
  1949. goto out;
  1950. err = -ENOBUFS;
  1951. skb = netlink_alloc_large_skb(len, dst_group);
  1952. if (skb == NULL)
  1953. goto out;
  1954. NETLINK_CB(skb).portid = nlk->portid;
  1955. NETLINK_CB(skb).dst_group = dst_group;
  1956. NETLINK_CB(skb).creds = scm.creds;
  1957. NETLINK_CB(skb).flags = netlink_skb_flags;
  1958. err = -EFAULT;
  1959. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1960. kfree_skb(skb);
  1961. goto out;
  1962. }
  1963. err = security_netlink_send(sk, skb);
  1964. if (err) {
  1965. kfree_skb(skb);
  1966. goto out;
  1967. }
  1968. if (dst_group) {
  1969. atomic_inc(&skb->users);
  1970. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1971. }
  1972. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1973. out:
  1974. scm_destroy(&scm);
  1975. return err;
  1976. }
  1977. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1978. int flags)
  1979. {
  1980. struct scm_cookie scm;
  1981. struct sock *sk = sock->sk;
  1982. struct netlink_sock *nlk = nlk_sk(sk);
  1983. int noblock = flags&MSG_DONTWAIT;
  1984. size_t copied;
  1985. struct sk_buff *skb, *data_skb;
  1986. int err, ret;
  1987. if (flags&MSG_OOB)
  1988. return -EOPNOTSUPP;
  1989. copied = 0;
  1990. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1991. if (skb == NULL)
  1992. goto out;
  1993. data_skb = skb;
  1994. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1995. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1996. /*
  1997. * If this skb has a frag_list, then here that means that we
  1998. * will have to use the frag_list skb's data for compat tasks
  1999. * and the regular skb's data for normal (non-compat) tasks.
  2000. *
  2001. * If we need to send the compat skb, assign it to the
  2002. * 'data_skb' variable so that it will be used below for data
  2003. * copying. We keep 'skb' for everything else, including
  2004. * freeing both later.
  2005. */
  2006. if (flags & MSG_CMSG_COMPAT)
  2007. data_skb = skb_shinfo(skb)->frag_list;
  2008. }
  2009. #endif
  2010. /* Record the max length of recvmsg() calls for future allocations */
  2011. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  2012. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  2013. 16384);
  2014. copied = data_skb->len;
  2015. if (len < copied) {
  2016. msg->msg_flags |= MSG_TRUNC;
  2017. copied = len;
  2018. }
  2019. skb_reset_transport_header(data_skb);
  2020. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  2021. if (msg->msg_name) {
  2022. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  2023. addr->nl_family = AF_NETLINK;
  2024. addr->nl_pad = 0;
  2025. addr->nl_pid = NETLINK_CB(skb).portid;
  2026. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  2027. msg->msg_namelen = sizeof(*addr);
  2028. }
  2029. if (nlk->flags & NETLINK_RECV_PKTINFO)
  2030. netlink_cmsg_recv_pktinfo(msg, skb);
  2031. memset(&scm, 0, sizeof(scm));
  2032. scm.creds = *NETLINK_CREDS(skb);
  2033. if (flags & MSG_TRUNC)
  2034. copied = data_skb->len;
  2035. skb_free_datagram(sk, skb);
  2036. if (nlk->cb_running &&
  2037. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  2038. ret = netlink_dump(sk);
  2039. if (ret) {
  2040. sk->sk_err = -ret;
  2041. sk->sk_error_report(sk);
  2042. }
  2043. }
  2044. scm_recv(sock, msg, &scm, flags);
  2045. out:
  2046. netlink_rcv_wake(sk);
  2047. return err ? : copied;
  2048. }
  2049. static void netlink_data_ready(struct sock *sk)
  2050. {
  2051. BUG();
  2052. }
  2053. /*
  2054. * We export these functions to other modules. They provide a
  2055. * complete set of kernel non-blocking support for message
  2056. * queueing.
  2057. */
  2058. struct sock *
  2059. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  2060. struct netlink_kernel_cfg *cfg)
  2061. {
  2062. struct socket *sock;
  2063. struct sock *sk;
  2064. struct netlink_sock *nlk;
  2065. struct listeners *listeners = NULL;
  2066. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  2067. unsigned int groups;
  2068. BUG_ON(!nl_table);
  2069. if (unit < 0 || unit >= MAX_LINKS)
  2070. return NULL;
  2071. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  2072. return NULL;
  2073. /*
  2074. * We have to just have a reference on the net from sk, but don't
  2075. * get_net it. Besides, we cannot get and then put the net here.
  2076. * So we create one inside init_net and the move it to net.
  2077. */
  2078. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  2079. goto out_sock_release_nosk;
  2080. sk = sock->sk;
  2081. sk_change_net(sk, net);
  2082. if (!cfg || cfg->groups < 32)
  2083. groups = 32;
  2084. else
  2085. groups = cfg->groups;
  2086. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2087. if (!listeners)
  2088. goto out_sock_release;
  2089. sk->sk_data_ready = netlink_data_ready;
  2090. if (cfg && cfg->input)
  2091. nlk_sk(sk)->netlink_rcv = cfg->input;
  2092. if (netlink_insert(sk, 0))
  2093. goto out_sock_release;
  2094. nlk = nlk_sk(sk);
  2095. nlk->flags |= NETLINK_KERNEL_SOCKET;
  2096. netlink_table_grab();
  2097. if (!nl_table[unit].registered) {
  2098. nl_table[unit].groups = groups;
  2099. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  2100. nl_table[unit].cb_mutex = cb_mutex;
  2101. nl_table[unit].module = module;
  2102. if (cfg) {
  2103. nl_table[unit].bind = cfg->bind;
  2104. nl_table[unit].unbind = cfg->unbind;
  2105. nl_table[unit].flags = cfg->flags;
  2106. if (cfg->compare)
  2107. nl_table[unit].compare = cfg->compare;
  2108. }
  2109. nl_table[unit].registered = 1;
  2110. } else {
  2111. kfree(listeners);
  2112. nl_table[unit].registered++;
  2113. }
  2114. netlink_table_ungrab();
  2115. return sk;
  2116. out_sock_release:
  2117. kfree(listeners);
  2118. netlink_kernel_release(sk);
  2119. return NULL;
  2120. out_sock_release_nosk:
  2121. sock_release(sock);
  2122. return NULL;
  2123. }
  2124. EXPORT_SYMBOL(__netlink_kernel_create);
  2125. void
  2126. netlink_kernel_release(struct sock *sk)
  2127. {
  2128. sk_release_kernel(sk);
  2129. }
  2130. EXPORT_SYMBOL(netlink_kernel_release);
  2131. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2132. {
  2133. struct listeners *new, *old;
  2134. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  2135. if (groups < 32)
  2136. groups = 32;
  2137. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  2138. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  2139. if (!new)
  2140. return -ENOMEM;
  2141. old = nl_deref_protected(tbl->listeners);
  2142. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  2143. rcu_assign_pointer(tbl->listeners, new);
  2144. kfree_rcu(old, rcu);
  2145. }
  2146. tbl->groups = groups;
  2147. return 0;
  2148. }
  2149. /**
  2150. * netlink_change_ngroups - change number of multicast groups
  2151. *
  2152. * This changes the number of multicast groups that are available
  2153. * on a certain netlink family. Note that it is not possible to
  2154. * change the number of groups to below 32. Also note that it does
  2155. * not implicitly call netlink_clear_multicast_users() when the
  2156. * number of groups is reduced.
  2157. *
  2158. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  2159. * @groups: The new number of groups.
  2160. */
  2161. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2162. {
  2163. int err;
  2164. netlink_table_grab();
  2165. err = __netlink_change_ngroups(sk, groups);
  2166. netlink_table_ungrab();
  2167. return err;
  2168. }
  2169. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  2170. {
  2171. struct sock *sk;
  2172. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  2173. sk_for_each_bound(sk, &tbl->mc_list)
  2174. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  2175. }
  2176. struct nlmsghdr *
  2177. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  2178. {
  2179. struct nlmsghdr *nlh;
  2180. int size = nlmsg_msg_size(len);
  2181. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  2182. nlh->nlmsg_type = type;
  2183. nlh->nlmsg_len = size;
  2184. nlh->nlmsg_flags = flags;
  2185. nlh->nlmsg_pid = portid;
  2186. nlh->nlmsg_seq = seq;
  2187. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  2188. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  2189. return nlh;
  2190. }
  2191. EXPORT_SYMBOL(__nlmsg_put);
  2192. /*
  2193. * It looks a bit ugly.
  2194. * It would be better to create kernel thread.
  2195. */
  2196. static int netlink_dump(struct sock *sk)
  2197. {
  2198. struct netlink_sock *nlk = nlk_sk(sk);
  2199. struct netlink_callback *cb;
  2200. struct sk_buff *skb = NULL;
  2201. struct nlmsghdr *nlh;
  2202. int len, err = -ENOBUFS;
  2203. int alloc_size;
  2204. mutex_lock(nlk->cb_mutex);
  2205. if (!nlk->cb_running) {
  2206. err = -EINVAL;
  2207. goto errout_skb;
  2208. }
  2209. cb = &nlk->cb;
  2210. alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  2211. if (!netlink_rx_is_mmaped(sk) &&
  2212. atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  2213. goto errout_skb;
  2214. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  2215. * required, but it makes sense to _attempt_ a 16K bytes allocation
  2216. * to reduce number of system calls on dump operations, if user
  2217. * ever provided a big enough buffer.
  2218. */
  2219. if (alloc_size < nlk->max_recvmsg_len) {
  2220. skb = netlink_alloc_skb(sk,
  2221. nlk->max_recvmsg_len,
  2222. nlk->portid,
  2223. GFP_KERNEL |
  2224. __GFP_NOWARN |
  2225. __GFP_NORETRY);
  2226. /* available room should be exact amount to avoid MSG_TRUNC */
  2227. if (skb)
  2228. skb_reserve(skb, skb_tailroom(skb) -
  2229. nlk->max_recvmsg_len);
  2230. }
  2231. if (!skb)
  2232. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  2233. GFP_KERNEL);
  2234. if (!skb)
  2235. goto errout_skb;
  2236. netlink_skb_set_owner_r(skb, sk);
  2237. len = cb->dump(skb, cb);
  2238. if (len > 0) {
  2239. mutex_unlock(nlk->cb_mutex);
  2240. if (sk_filter(sk, skb))
  2241. kfree_skb(skb);
  2242. else
  2243. __netlink_sendskb(sk, skb);
  2244. return 0;
  2245. }
  2246. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  2247. if (!nlh)
  2248. goto errout_skb;
  2249. nl_dump_check_consistent(cb, nlh);
  2250. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  2251. if (sk_filter(sk, skb))
  2252. kfree_skb(skb);
  2253. else
  2254. __netlink_sendskb(sk, skb);
  2255. if (cb->done)
  2256. cb->done(cb);
  2257. nlk->cb_running = false;
  2258. mutex_unlock(nlk->cb_mutex);
  2259. module_put(cb->module);
  2260. consume_skb(cb->skb);
  2261. return 0;
  2262. errout_skb:
  2263. mutex_unlock(nlk->cb_mutex);
  2264. kfree_skb(skb);
  2265. return err;
  2266. }
  2267. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  2268. const struct nlmsghdr *nlh,
  2269. struct netlink_dump_control *control)
  2270. {
  2271. struct netlink_callback *cb;
  2272. struct sock *sk;
  2273. struct netlink_sock *nlk;
  2274. int ret;
  2275. /* Memory mapped dump requests need to be copied to avoid looping
  2276. * on the pending state in netlink_mmap_sendmsg() while the CB hold
  2277. * a reference to the skb.
  2278. */
  2279. if (netlink_skb_is_mmaped(skb)) {
  2280. skb = skb_copy(skb, GFP_KERNEL);
  2281. if (skb == NULL)
  2282. return -ENOBUFS;
  2283. } else
  2284. atomic_inc(&skb->users);
  2285. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  2286. if (sk == NULL) {
  2287. ret = -ECONNREFUSED;
  2288. goto error_free;
  2289. }
  2290. nlk = nlk_sk(sk);
  2291. mutex_lock(nlk->cb_mutex);
  2292. /* A dump is in progress... */
  2293. if (nlk->cb_running) {
  2294. ret = -EBUSY;
  2295. goto error_unlock;
  2296. }
  2297. /* add reference of module which cb->dump belongs to */
  2298. if (!try_module_get(control->module)) {
  2299. ret = -EPROTONOSUPPORT;
  2300. goto error_unlock;
  2301. }
  2302. cb = &nlk->cb;
  2303. memset(cb, 0, sizeof(*cb));
  2304. cb->dump = control->dump;
  2305. cb->done = control->done;
  2306. cb->nlh = nlh;
  2307. cb->data = control->data;
  2308. cb->module = control->module;
  2309. cb->min_dump_alloc = control->min_dump_alloc;
  2310. cb->skb = skb;
  2311. nlk->cb_running = true;
  2312. mutex_unlock(nlk->cb_mutex);
  2313. ret = netlink_dump(sk);
  2314. sock_put(sk);
  2315. if (ret)
  2316. return ret;
  2317. /* We successfully started a dump, by returning -EINTR we
  2318. * signal not to send ACK even if it was requested.
  2319. */
  2320. return -EINTR;
  2321. error_unlock:
  2322. sock_put(sk);
  2323. mutex_unlock(nlk->cb_mutex);
  2324. error_free:
  2325. kfree_skb(skb);
  2326. return ret;
  2327. }
  2328. EXPORT_SYMBOL(__netlink_dump_start);
  2329. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  2330. {
  2331. struct sk_buff *skb;
  2332. struct nlmsghdr *rep;
  2333. struct nlmsgerr *errmsg;
  2334. size_t payload = sizeof(*errmsg);
  2335. /* error messages get the original request appened */
  2336. if (err)
  2337. payload += nlmsg_len(nlh);
  2338. skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
  2339. NETLINK_CB(in_skb).portid, GFP_KERNEL);
  2340. if (!skb) {
  2341. struct sock *sk;
  2342. sk = netlink_lookup(sock_net(in_skb->sk),
  2343. in_skb->sk->sk_protocol,
  2344. NETLINK_CB(in_skb).portid);
  2345. if (sk) {
  2346. sk->sk_err = ENOBUFS;
  2347. sk->sk_error_report(sk);
  2348. sock_put(sk);
  2349. }
  2350. return;
  2351. }
  2352. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  2353. NLMSG_ERROR, payload, 0);
  2354. errmsg = nlmsg_data(rep);
  2355. errmsg->error = err;
  2356. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  2357. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  2358. }
  2359. EXPORT_SYMBOL(netlink_ack);
  2360. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  2361. struct nlmsghdr *))
  2362. {
  2363. struct nlmsghdr *nlh;
  2364. int err;
  2365. while (skb->len >= nlmsg_total_size(0)) {
  2366. int msglen;
  2367. nlh = nlmsg_hdr(skb);
  2368. err = 0;
  2369. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  2370. return 0;
  2371. /* Only requests are handled by the kernel */
  2372. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  2373. goto ack;
  2374. /* Skip control messages */
  2375. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  2376. goto ack;
  2377. err = cb(skb, nlh);
  2378. if (err == -EINTR)
  2379. goto skip;
  2380. ack:
  2381. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  2382. netlink_ack(skb, nlh, err);
  2383. skip:
  2384. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  2385. if (msglen > skb->len)
  2386. msglen = skb->len;
  2387. skb_pull(skb, msglen);
  2388. }
  2389. return 0;
  2390. }
  2391. EXPORT_SYMBOL(netlink_rcv_skb);
  2392. /**
  2393. * nlmsg_notify - send a notification netlink message
  2394. * @sk: netlink socket to use
  2395. * @skb: notification message
  2396. * @portid: destination netlink portid for reports or 0
  2397. * @group: destination multicast group or 0
  2398. * @report: 1 to report back, 0 to disable
  2399. * @flags: allocation flags
  2400. */
  2401. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2402. unsigned int group, int report, gfp_t flags)
  2403. {
  2404. int err = 0;
  2405. if (group) {
  2406. int exclude_portid = 0;
  2407. if (report) {
  2408. atomic_inc(&skb->users);
  2409. exclude_portid = portid;
  2410. }
  2411. /* errors reported via destination sk->sk_err, but propagate
  2412. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2413. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2414. }
  2415. if (report) {
  2416. int err2;
  2417. err2 = nlmsg_unicast(sk, skb, portid);
  2418. if (!err || err == -ESRCH)
  2419. err = err2;
  2420. }
  2421. return err;
  2422. }
  2423. EXPORT_SYMBOL(nlmsg_notify);
  2424. #ifdef CONFIG_PROC_FS
  2425. struct nl_seq_iter {
  2426. struct seq_net_private p;
  2427. struct rhashtable_iter hti;
  2428. int link;
  2429. };
  2430. static int netlink_walk_start(struct nl_seq_iter *iter)
  2431. {
  2432. int err;
  2433. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti);
  2434. if (err) {
  2435. iter->link = MAX_LINKS;
  2436. return err;
  2437. }
  2438. err = rhashtable_walk_start(&iter->hti);
  2439. return err == -EAGAIN ? 0 : err;
  2440. }
  2441. static void netlink_walk_stop(struct nl_seq_iter *iter)
  2442. {
  2443. rhashtable_walk_stop(&iter->hti);
  2444. rhashtable_walk_exit(&iter->hti);
  2445. }
  2446. static void *__netlink_seq_next(struct seq_file *seq)
  2447. {
  2448. struct nl_seq_iter *iter = seq->private;
  2449. struct netlink_sock *nlk;
  2450. do {
  2451. for (;;) {
  2452. int err;
  2453. nlk = rhashtable_walk_next(&iter->hti);
  2454. if (IS_ERR(nlk)) {
  2455. if (PTR_ERR(nlk) == -EAGAIN)
  2456. continue;
  2457. return nlk;
  2458. }
  2459. if (nlk)
  2460. break;
  2461. netlink_walk_stop(iter);
  2462. if (++iter->link >= MAX_LINKS)
  2463. return NULL;
  2464. err = netlink_walk_start(iter);
  2465. if (err)
  2466. return ERR_PTR(err);
  2467. }
  2468. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2469. return nlk;
  2470. }
  2471. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2472. {
  2473. struct nl_seq_iter *iter = seq->private;
  2474. void *obj = SEQ_START_TOKEN;
  2475. loff_t pos;
  2476. int err;
  2477. iter->link = 0;
  2478. err = netlink_walk_start(iter);
  2479. if (err)
  2480. return ERR_PTR(err);
  2481. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2482. obj = __netlink_seq_next(seq);
  2483. return obj;
  2484. }
  2485. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2486. {
  2487. ++*pos;
  2488. return __netlink_seq_next(seq);
  2489. }
  2490. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2491. {
  2492. struct nl_seq_iter *iter = seq->private;
  2493. if (iter->link >= MAX_LINKS)
  2494. return;
  2495. netlink_walk_stop(iter);
  2496. }
  2497. static int netlink_seq_show(struct seq_file *seq, void *v)
  2498. {
  2499. if (v == SEQ_START_TOKEN) {
  2500. seq_puts(seq,
  2501. "sk Eth Pid Groups "
  2502. "Rmem Wmem Dump Locks Drops Inode\n");
  2503. } else {
  2504. struct sock *s = v;
  2505. struct netlink_sock *nlk = nlk_sk(s);
  2506. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2507. s,
  2508. s->sk_protocol,
  2509. nlk->portid,
  2510. nlk->groups ? (u32)nlk->groups[0] : 0,
  2511. sk_rmem_alloc_get(s),
  2512. sk_wmem_alloc_get(s),
  2513. nlk->cb_running,
  2514. atomic_read(&s->sk_refcnt),
  2515. atomic_read(&s->sk_drops),
  2516. sock_i_ino(s)
  2517. );
  2518. }
  2519. return 0;
  2520. }
  2521. static const struct seq_operations netlink_seq_ops = {
  2522. .start = netlink_seq_start,
  2523. .next = netlink_seq_next,
  2524. .stop = netlink_seq_stop,
  2525. .show = netlink_seq_show,
  2526. };
  2527. static int netlink_seq_open(struct inode *inode, struct file *file)
  2528. {
  2529. return seq_open_net(inode, file, &netlink_seq_ops,
  2530. sizeof(struct nl_seq_iter));
  2531. }
  2532. static const struct file_operations netlink_seq_fops = {
  2533. .owner = THIS_MODULE,
  2534. .open = netlink_seq_open,
  2535. .read = seq_read,
  2536. .llseek = seq_lseek,
  2537. .release = seq_release_net,
  2538. };
  2539. #endif
  2540. int netlink_register_notifier(struct notifier_block *nb)
  2541. {
  2542. return atomic_notifier_chain_register(&netlink_chain, nb);
  2543. }
  2544. EXPORT_SYMBOL(netlink_register_notifier);
  2545. int netlink_unregister_notifier(struct notifier_block *nb)
  2546. {
  2547. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2548. }
  2549. EXPORT_SYMBOL(netlink_unregister_notifier);
  2550. static const struct proto_ops netlink_ops = {
  2551. .family = PF_NETLINK,
  2552. .owner = THIS_MODULE,
  2553. .release = netlink_release,
  2554. .bind = netlink_bind,
  2555. .connect = netlink_connect,
  2556. .socketpair = sock_no_socketpair,
  2557. .accept = sock_no_accept,
  2558. .getname = netlink_getname,
  2559. .poll = netlink_poll,
  2560. .ioctl = sock_no_ioctl,
  2561. .listen = sock_no_listen,
  2562. .shutdown = sock_no_shutdown,
  2563. .setsockopt = netlink_setsockopt,
  2564. .getsockopt = netlink_getsockopt,
  2565. .sendmsg = netlink_sendmsg,
  2566. .recvmsg = netlink_recvmsg,
  2567. .mmap = netlink_mmap,
  2568. .sendpage = sock_no_sendpage,
  2569. };
  2570. static const struct net_proto_family netlink_family_ops = {
  2571. .family = PF_NETLINK,
  2572. .create = netlink_create,
  2573. .owner = THIS_MODULE, /* for consistency 8) */
  2574. };
  2575. static int __net_init netlink_net_init(struct net *net)
  2576. {
  2577. #ifdef CONFIG_PROC_FS
  2578. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2579. return -ENOMEM;
  2580. #endif
  2581. return 0;
  2582. }
  2583. static void __net_exit netlink_net_exit(struct net *net)
  2584. {
  2585. #ifdef CONFIG_PROC_FS
  2586. remove_proc_entry("netlink", net->proc_net);
  2587. #endif
  2588. }
  2589. static void __init netlink_add_usersock_entry(void)
  2590. {
  2591. struct listeners *listeners;
  2592. int groups = 32;
  2593. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2594. if (!listeners)
  2595. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2596. netlink_table_grab();
  2597. nl_table[NETLINK_USERSOCK].groups = groups;
  2598. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2599. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2600. nl_table[NETLINK_USERSOCK].registered = 1;
  2601. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2602. netlink_table_ungrab();
  2603. }
  2604. static struct pernet_operations __net_initdata netlink_net_ops = {
  2605. .init = netlink_net_init,
  2606. .exit = netlink_net_exit,
  2607. };
  2608. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2609. {
  2610. const struct netlink_sock *nlk = data;
  2611. struct netlink_compare_arg arg;
  2612. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2613. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2614. }
  2615. static const struct rhashtable_params netlink_rhashtable_params = {
  2616. .head_offset = offsetof(struct netlink_sock, node),
  2617. .key_len = netlink_compare_arg_len,
  2618. .obj_hashfn = netlink_hash,
  2619. .obj_cmpfn = netlink_compare,
  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);