af_netlink.c 62 KB

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