af_netlink.c 63 KB

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