af_netlink.c 73 KB

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