af_netlink.c 73 KB

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