af_netlink.c 64 KB

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