af_netlink.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  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. kfree(nlk->groups);
  576. nlk->groups = NULL;
  577. if (!refcount_dec_and_test(&sk->sk_refcnt))
  578. return;
  579. if (nlk->cb_running && nlk->cb.done) {
  580. INIT_WORK(&nlk->work, netlink_sock_destruct_work);
  581. schedule_work(&nlk->work);
  582. return;
  583. }
  584. sk_free(sk);
  585. }
  586. static int netlink_release(struct socket *sock)
  587. {
  588. struct sock *sk = sock->sk;
  589. struct netlink_sock *nlk;
  590. if (!sk)
  591. return 0;
  592. netlink_remove(sk);
  593. sock_orphan(sk);
  594. nlk = nlk_sk(sk);
  595. /*
  596. * OK. Socket is unlinked, any packets that arrive now
  597. * will be purged.
  598. */
  599. /* must not acquire netlink_table_lock in any way again before unbind
  600. * and notifying genetlink is done as otherwise it might deadlock
  601. */
  602. if (nlk->netlink_unbind) {
  603. int i;
  604. for (i = 0; i < nlk->ngroups; i++)
  605. if (test_bit(i, nlk->groups))
  606. nlk->netlink_unbind(sock_net(sk), i + 1);
  607. }
  608. if (sk->sk_protocol == NETLINK_GENERIC &&
  609. atomic_dec_return(&genl_sk_destructing_cnt) == 0)
  610. wake_up(&genl_sk_destructing_waitq);
  611. sock->sk = NULL;
  612. wake_up_interruptible_all(&nlk->wait);
  613. skb_queue_purge(&sk->sk_write_queue);
  614. if (nlk->portid && nlk->bound) {
  615. struct netlink_notify n = {
  616. .net = sock_net(sk),
  617. .protocol = sk->sk_protocol,
  618. .portid = nlk->portid,
  619. };
  620. blocking_notifier_call_chain(&netlink_chain,
  621. NETLINK_URELEASE, &n);
  622. }
  623. module_put(nlk->module);
  624. if (netlink_is_kernel(sk)) {
  625. netlink_table_grab();
  626. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  627. if (--nl_table[sk->sk_protocol].registered == 0) {
  628. struct listeners *old;
  629. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  630. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  631. kfree_rcu(old, rcu);
  632. nl_table[sk->sk_protocol].module = NULL;
  633. nl_table[sk->sk_protocol].bind = NULL;
  634. nl_table[sk->sk_protocol].unbind = NULL;
  635. nl_table[sk->sk_protocol].flags = 0;
  636. nl_table[sk->sk_protocol].registered = 0;
  637. }
  638. netlink_table_ungrab();
  639. }
  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 = 0;
  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. netlink_lock_table();
  824. if (nlk->netlink_bind && groups) {
  825. int group;
  826. for (group = 0; group < nlk->ngroups; group++) {
  827. if (!test_bit(group, &groups))
  828. continue;
  829. err = nlk->netlink_bind(net, group + 1);
  830. if (!err)
  831. continue;
  832. netlink_undo_bind(group, groups, sk);
  833. goto unlock;
  834. }
  835. }
  836. /* No need for barriers here as we return to user-space without
  837. * using any of the bound attributes.
  838. */
  839. if (!bound) {
  840. err = nladdr->nl_pid ?
  841. netlink_insert(sk, nladdr->nl_pid) :
  842. netlink_autobind(sock);
  843. if (err) {
  844. netlink_undo_bind(nlk->ngroups, groups, sk);
  845. goto unlock;
  846. }
  847. }
  848. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  849. goto unlock;
  850. netlink_unlock_table();
  851. netlink_table_grab();
  852. netlink_update_subscriptions(sk, nlk->subscriptions +
  853. hweight32(groups) -
  854. hweight32(nlk->groups[0]));
  855. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  856. netlink_update_listeners(sk);
  857. netlink_table_ungrab();
  858. return 0;
  859. unlock:
  860. netlink_unlock_table();
  861. return err;
  862. }
  863. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  864. int alen, int flags)
  865. {
  866. int err = 0;
  867. struct sock *sk = sock->sk;
  868. struct netlink_sock *nlk = nlk_sk(sk);
  869. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  870. if (alen < sizeof(addr->sa_family))
  871. return -EINVAL;
  872. if (addr->sa_family == AF_UNSPEC) {
  873. sk->sk_state = NETLINK_UNCONNECTED;
  874. nlk->dst_portid = 0;
  875. nlk->dst_group = 0;
  876. return 0;
  877. }
  878. if (addr->sa_family != AF_NETLINK)
  879. return -EINVAL;
  880. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  881. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  882. return -EPERM;
  883. /* No need for barriers here as we return to user-space without
  884. * using any of the bound attributes.
  885. */
  886. if (!nlk->bound)
  887. err = netlink_autobind(sock);
  888. if (err == 0) {
  889. sk->sk_state = NETLINK_CONNECTED;
  890. nlk->dst_portid = nladdr->nl_pid;
  891. nlk->dst_group = ffs(nladdr->nl_groups);
  892. }
  893. return err;
  894. }
  895. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  896. int *addr_len, int peer)
  897. {
  898. struct sock *sk = sock->sk;
  899. struct netlink_sock *nlk = nlk_sk(sk);
  900. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  901. nladdr->nl_family = AF_NETLINK;
  902. nladdr->nl_pad = 0;
  903. *addr_len = sizeof(*nladdr);
  904. if (peer) {
  905. nladdr->nl_pid = nlk->dst_portid;
  906. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  907. } else {
  908. nladdr->nl_pid = nlk->portid;
  909. netlink_lock_table();
  910. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  911. netlink_unlock_table();
  912. }
  913. return 0;
  914. }
  915. static int netlink_ioctl(struct socket *sock, unsigned int cmd,
  916. unsigned long arg)
  917. {
  918. /* try to hand this ioctl down to the NIC drivers.
  919. */
  920. return -ENOIOCTLCMD;
  921. }
  922. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  923. {
  924. struct sock *sock;
  925. struct netlink_sock *nlk;
  926. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  927. if (!sock)
  928. return ERR_PTR(-ECONNREFUSED);
  929. /* Don't bother queuing skb if kernel socket has no input function */
  930. nlk = nlk_sk(sock);
  931. if (sock->sk_state == NETLINK_CONNECTED &&
  932. nlk->dst_portid != nlk_sk(ssk)->portid) {
  933. sock_put(sock);
  934. return ERR_PTR(-ECONNREFUSED);
  935. }
  936. return sock;
  937. }
  938. struct sock *netlink_getsockbyfilp(struct file *filp)
  939. {
  940. struct inode *inode = file_inode(filp);
  941. struct sock *sock;
  942. if (!S_ISSOCK(inode->i_mode))
  943. return ERR_PTR(-ENOTSOCK);
  944. sock = SOCKET_I(inode)->sk;
  945. if (sock->sk_family != AF_NETLINK)
  946. return ERR_PTR(-EINVAL);
  947. sock_hold(sock);
  948. return sock;
  949. }
  950. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  951. int broadcast)
  952. {
  953. struct sk_buff *skb;
  954. void *data;
  955. if (size <= NLMSG_GOODSIZE || broadcast)
  956. return alloc_skb(size, GFP_KERNEL);
  957. size = SKB_DATA_ALIGN(size) +
  958. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  959. data = vmalloc(size);
  960. if (data == NULL)
  961. return NULL;
  962. skb = __build_skb(data, size);
  963. if (skb == NULL)
  964. vfree(data);
  965. else
  966. skb->destructor = netlink_skb_destructor;
  967. return skb;
  968. }
  969. /*
  970. * Attach a skb to a netlink socket.
  971. * The caller must hold a reference to the destination socket. On error, the
  972. * reference is dropped. The skb is not send to the destination, just all
  973. * all error checks are performed and memory in the queue is reserved.
  974. * Return values:
  975. * < 0: error. skb freed, reference to sock dropped.
  976. * 0: continue
  977. * 1: repeat lookup - reference dropped while waiting for socket memory.
  978. */
  979. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  980. long *timeo, struct sock *ssk)
  981. {
  982. struct netlink_sock *nlk;
  983. nlk = nlk_sk(sk);
  984. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  985. test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
  986. DECLARE_WAITQUEUE(wait, current);
  987. if (!*timeo) {
  988. if (!ssk || netlink_is_kernel(ssk))
  989. netlink_overrun(sk);
  990. sock_put(sk);
  991. kfree_skb(skb);
  992. return -EAGAIN;
  993. }
  994. __set_current_state(TASK_INTERRUPTIBLE);
  995. add_wait_queue(&nlk->wait, &wait);
  996. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  997. test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
  998. !sock_flag(sk, SOCK_DEAD))
  999. *timeo = schedule_timeout(*timeo);
  1000. __set_current_state(TASK_RUNNING);
  1001. remove_wait_queue(&nlk->wait, &wait);
  1002. sock_put(sk);
  1003. if (signal_pending(current)) {
  1004. kfree_skb(skb);
  1005. return sock_intr_errno(*timeo);
  1006. }
  1007. return 1;
  1008. }
  1009. netlink_skb_set_owner_r(skb, sk);
  1010. return 0;
  1011. }
  1012. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1013. {
  1014. int len = skb->len;
  1015. netlink_deliver_tap(skb);
  1016. skb_queue_tail(&sk->sk_receive_queue, skb);
  1017. sk->sk_data_ready(sk);
  1018. return len;
  1019. }
  1020. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1021. {
  1022. int len = __netlink_sendskb(sk, skb);
  1023. sock_put(sk);
  1024. return len;
  1025. }
  1026. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1027. {
  1028. kfree_skb(skb);
  1029. sock_put(sk);
  1030. }
  1031. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1032. {
  1033. int delta;
  1034. WARN_ON(skb->sk != NULL);
  1035. delta = skb->end - skb->tail;
  1036. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1037. return skb;
  1038. if (skb_shared(skb)) {
  1039. struct sk_buff *nskb = skb_clone(skb, allocation);
  1040. if (!nskb)
  1041. return skb;
  1042. consume_skb(skb);
  1043. skb = nskb;
  1044. }
  1045. pskb_expand_head(skb, 0, -delta,
  1046. (allocation & ~__GFP_DIRECT_RECLAIM) |
  1047. __GFP_NOWARN | __GFP_NORETRY);
  1048. return skb;
  1049. }
  1050. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1051. struct sock *ssk)
  1052. {
  1053. int ret;
  1054. struct netlink_sock *nlk = nlk_sk(sk);
  1055. ret = -ECONNREFUSED;
  1056. if (nlk->netlink_rcv != NULL) {
  1057. ret = skb->len;
  1058. netlink_skb_set_owner_r(skb, sk);
  1059. NETLINK_CB(skb).sk = ssk;
  1060. netlink_deliver_tap_kernel(sk, ssk, skb);
  1061. nlk->netlink_rcv(skb);
  1062. consume_skb(skb);
  1063. } else {
  1064. kfree_skb(skb);
  1065. }
  1066. sock_put(sk);
  1067. return ret;
  1068. }
  1069. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1070. u32 portid, int nonblock)
  1071. {
  1072. struct sock *sk;
  1073. int err;
  1074. long timeo;
  1075. skb = netlink_trim(skb, gfp_any());
  1076. timeo = sock_sndtimeo(ssk, nonblock);
  1077. retry:
  1078. sk = netlink_getsockbyportid(ssk, portid);
  1079. if (IS_ERR(sk)) {
  1080. kfree_skb(skb);
  1081. return PTR_ERR(sk);
  1082. }
  1083. if (netlink_is_kernel(sk))
  1084. return netlink_unicast_kernel(sk, skb, ssk);
  1085. if (sk_filter(sk, skb)) {
  1086. err = skb->len;
  1087. kfree_skb(skb);
  1088. sock_put(sk);
  1089. return err;
  1090. }
  1091. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1092. if (err == 1)
  1093. goto retry;
  1094. if (err)
  1095. return err;
  1096. return netlink_sendskb(sk, skb);
  1097. }
  1098. EXPORT_SYMBOL(netlink_unicast);
  1099. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1100. {
  1101. int res = 0;
  1102. struct listeners *listeners;
  1103. BUG_ON(!netlink_is_kernel(sk));
  1104. rcu_read_lock();
  1105. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1106. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1107. res = test_bit(group - 1, listeners->masks);
  1108. rcu_read_unlock();
  1109. return res;
  1110. }
  1111. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1112. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1113. {
  1114. struct netlink_sock *nlk = nlk_sk(sk);
  1115. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1116. !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
  1117. netlink_skb_set_owner_r(skb, sk);
  1118. __netlink_sendskb(sk, skb);
  1119. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1120. }
  1121. return -1;
  1122. }
  1123. struct netlink_broadcast_data {
  1124. struct sock *exclude_sk;
  1125. struct net *net;
  1126. u32 portid;
  1127. u32 group;
  1128. int failure;
  1129. int delivery_failure;
  1130. int congested;
  1131. int delivered;
  1132. gfp_t allocation;
  1133. struct sk_buff *skb, *skb2;
  1134. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1135. void *tx_data;
  1136. };
  1137. static void do_one_broadcast(struct sock *sk,
  1138. struct netlink_broadcast_data *p)
  1139. {
  1140. struct netlink_sock *nlk = nlk_sk(sk);
  1141. int val;
  1142. if (p->exclude_sk == sk)
  1143. return;
  1144. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1145. !test_bit(p->group - 1, nlk->groups))
  1146. return;
  1147. if (!net_eq(sock_net(sk), p->net)) {
  1148. if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
  1149. return;
  1150. if (!peernet_has_id(sock_net(sk), p->net))
  1151. return;
  1152. if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
  1153. CAP_NET_BROADCAST))
  1154. return;
  1155. }
  1156. if (p->failure) {
  1157. netlink_overrun(sk);
  1158. return;
  1159. }
  1160. sock_hold(sk);
  1161. if (p->skb2 == NULL) {
  1162. if (skb_shared(p->skb)) {
  1163. p->skb2 = skb_clone(p->skb, p->allocation);
  1164. } else {
  1165. p->skb2 = skb_get(p->skb);
  1166. /*
  1167. * skb ownership may have been set when
  1168. * delivered to a previous socket.
  1169. */
  1170. skb_orphan(p->skb2);
  1171. }
  1172. }
  1173. if (p->skb2 == NULL) {
  1174. netlink_overrun(sk);
  1175. /* Clone failed. Notify ALL listeners. */
  1176. p->failure = 1;
  1177. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1178. p->delivery_failure = 1;
  1179. goto out;
  1180. }
  1181. if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1182. kfree_skb(p->skb2);
  1183. p->skb2 = NULL;
  1184. goto out;
  1185. }
  1186. if (sk_filter(sk, p->skb2)) {
  1187. kfree_skb(p->skb2);
  1188. p->skb2 = NULL;
  1189. goto out;
  1190. }
  1191. NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
  1192. if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
  1193. NETLINK_CB(p->skb2).nsid_is_set = true;
  1194. val = netlink_broadcast_deliver(sk, p->skb2);
  1195. if (val < 0) {
  1196. netlink_overrun(sk);
  1197. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1198. p->delivery_failure = 1;
  1199. } else {
  1200. p->congested |= val;
  1201. p->delivered = 1;
  1202. p->skb2 = NULL;
  1203. }
  1204. out:
  1205. sock_put(sk);
  1206. }
  1207. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1208. u32 group, gfp_t allocation,
  1209. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1210. void *filter_data)
  1211. {
  1212. struct net *net = sock_net(ssk);
  1213. struct netlink_broadcast_data info;
  1214. struct sock *sk;
  1215. skb = netlink_trim(skb, allocation);
  1216. info.exclude_sk = ssk;
  1217. info.net = net;
  1218. info.portid = portid;
  1219. info.group = group;
  1220. info.failure = 0;
  1221. info.delivery_failure = 0;
  1222. info.congested = 0;
  1223. info.delivered = 0;
  1224. info.allocation = allocation;
  1225. info.skb = skb;
  1226. info.skb2 = NULL;
  1227. info.tx_filter = filter;
  1228. info.tx_data = filter_data;
  1229. /* While we sleep in clone, do not allow to change socket list */
  1230. netlink_lock_table();
  1231. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1232. do_one_broadcast(sk, &info);
  1233. consume_skb(skb);
  1234. netlink_unlock_table();
  1235. if (info.delivery_failure) {
  1236. kfree_skb(info.skb2);
  1237. return -ENOBUFS;
  1238. }
  1239. consume_skb(info.skb2);
  1240. if (info.delivered) {
  1241. if (info.congested && gfpflags_allow_blocking(allocation))
  1242. yield();
  1243. return 0;
  1244. }
  1245. return -ESRCH;
  1246. }
  1247. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1248. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1249. u32 group, gfp_t allocation)
  1250. {
  1251. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1252. NULL, NULL);
  1253. }
  1254. EXPORT_SYMBOL(netlink_broadcast);
  1255. struct netlink_set_err_data {
  1256. struct sock *exclude_sk;
  1257. u32 portid;
  1258. u32 group;
  1259. int code;
  1260. };
  1261. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1262. {
  1263. struct netlink_sock *nlk = nlk_sk(sk);
  1264. int ret = 0;
  1265. if (sk == p->exclude_sk)
  1266. goto out;
  1267. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1268. goto out;
  1269. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1270. !test_bit(p->group - 1, nlk->groups))
  1271. goto out;
  1272. if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
  1273. ret = 1;
  1274. goto out;
  1275. }
  1276. sk->sk_err = p->code;
  1277. sk->sk_error_report(sk);
  1278. out:
  1279. return ret;
  1280. }
  1281. /**
  1282. * netlink_set_err - report error to broadcast listeners
  1283. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1284. * @portid: the PORTID of a process that we want to skip (if any)
  1285. * @group: the broadcast group that will notice the error
  1286. * @code: error code, must be negative (as usual in kernelspace)
  1287. *
  1288. * This function returns the number of broadcast listeners that have set the
  1289. * NETLINK_NO_ENOBUFS socket option.
  1290. */
  1291. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1292. {
  1293. struct netlink_set_err_data info;
  1294. struct sock *sk;
  1295. int ret = 0;
  1296. info.exclude_sk = ssk;
  1297. info.portid = portid;
  1298. info.group = group;
  1299. /* sk->sk_err wants a positive error value */
  1300. info.code = -code;
  1301. read_lock(&nl_table_lock);
  1302. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1303. ret += do_one_set_err(sk, &info);
  1304. read_unlock(&nl_table_lock);
  1305. return ret;
  1306. }
  1307. EXPORT_SYMBOL(netlink_set_err);
  1308. /* must be called with netlink table grabbed */
  1309. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1310. unsigned int group,
  1311. int is_new)
  1312. {
  1313. int old, new = !!is_new, subscriptions;
  1314. old = test_bit(group - 1, nlk->groups);
  1315. subscriptions = nlk->subscriptions - old + new;
  1316. if (new)
  1317. __set_bit(group - 1, nlk->groups);
  1318. else
  1319. __clear_bit(group - 1, nlk->groups);
  1320. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1321. netlink_update_listeners(&nlk->sk);
  1322. }
  1323. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1324. char __user *optval, unsigned int optlen)
  1325. {
  1326. struct sock *sk = sock->sk;
  1327. struct netlink_sock *nlk = nlk_sk(sk);
  1328. unsigned int val = 0;
  1329. int err;
  1330. if (level != SOL_NETLINK)
  1331. return -ENOPROTOOPT;
  1332. if (optlen >= sizeof(int) &&
  1333. get_user(val, (unsigned int __user *)optval))
  1334. return -EFAULT;
  1335. switch (optname) {
  1336. case NETLINK_PKTINFO:
  1337. if (val)
  1338. nlk->flags |= NETLINK_F_RECV_PKTINFO;
  1339. else
  1340. nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
  1341. err = 0;
  1342. break;
  1343. case NETLINK_ADD_MEMBERSHIP:
  1344. case NETLINK_DROP_MEMBERSHIP: {
  1345. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1346. return -EPERM;
  1347. err = netlink_realloc_groups(sk);
  1348. if (err)
  1349. return err;
  1350. if (!val || val - 1 >= nlk->ngroups)
  1351. return -EINVAL;
  1352. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1353. err = nlk->netlink_bind(sock_net(sk), val);
  1354. if (err)
  1355. return err;
  1356. }
  1357. netlink_table_grab();
  1358. netlink_update_socket_mc(nlk, val,
  1359. optname == NETLINK_ADD_MEMBERSHIP);
  1360. netlink_table_ungrab();
  1361. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1362. nlk->netlink_unbind(sock_net(sk), val);
  1363. err = 0;
  1364. break;
  1365. }
  1366. case NETLINK_BROADCAST_ERROR:
  1367. if (val)
  1368. nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
  1369. else
  1370. nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
  1371. err = 0;
  1372. break;
  1373. case NETLINK_NO_ENOBUFS:
  1374. if (val) {
  1375. nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
  1376. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  1377. wake_up_interruptible(&nlk->wait);
  1378. } else {
  1379. nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
  1380. }
  1381. err = 0;
  1382. break;
  1383. case NETLINK_LISTEN_ALL_NSID:
  1384. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
  1385. return -EPERM;
  1386. if (val)
  1387. nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
  1388. else
  1389. nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
  1390. err = 0;
  1391. break;
  1392. case NETLINK_CAP_ACK:
  1393. if (val)
  1394. nlk->flags |= NETLINK_F_CAP_ACK;
  1395. else
  1396. nlk->flags &= ~NETLINK_F_CAP_ACK;
  1397. err = 0;
  1398. break;
  1399. case NETLINK_EXT_ACK:
  1400. if (val)
  1401. nlk->flags |= NETLINK_F_EXT_ACK;
  1402. else
  1403. nlk->flags &= ~NETLINK_F_EXT_ACK;
  1404. err = 0;
  1405. break;
  1406. default:
  1407. err = -ENOPROTOOPT;
  1408. }
  1409. return err;
  1410. }
  1411. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1412. char __user *optval, int __user *optlen)
  1413. {
  1414. struct sock *sk = sock->sk;
  1415. struct netlink_sock *nlk = nlk_sk(sk);
  1416. int len, val, err;
  1417. if (level != SOL_NETLINK)
  1418. return -ENOPROTOOPT;
  1419. if (get_user(len, optlen))
  1420. return -EFAULT;
  1421. if (len < 0)
  1422. return -EINVAL;
  1423. switch (optname) {
  1424. case NETLINK_PKTINFO:
  1425. if (len < sizeof(int))
  1426. return -EINVAL;
  1427. len = sizeof(int);
  1428. val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
  1429. if (put_user(len, optlen) ||
  1430. put_user(val, optval))
  1431. return -EFAULT;
  1432. err = 0;
  1433. break;
  1434. case NETLINK_BROADCAST_ERROR:
  1435. if (len < sizeof(int))
  1436. return -EINVAL;
  1437. len = sizeof(int);
  1438. val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
  1439. if (put_user(len, optlen) ||
  1440. put_user(val, optval))
  1441. return -EFAULT;
  1442. err = 0;
  1443. break;
  1444. case NETLINK_NO_ENOBUFS:
  1445. if (len < sizeof(int))
  1446. return -EINVAL;
  1447. len = sizeof(int);
  1448. val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
  1449. if (put_user(len, optlen) ||
  1450. put_user(val, optval))
  1451. return -EFAULT;
  1452. err = 0;
  1453. break;
  1454. case NETLINK_LIST_MEMBERSHIPS: {
  1455. int pos, idx, shift;
  1456. err = 0;
  1457. netlink_lock_table();
  1458. for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
  1459. if (len - pos < sizeof(u32))
  1460. break;
  1461. idx = pos / sizeof(unsigned long);
  1462. shift = (pos % sizeof(unsigned long)) * 8;
  1463. if (put_user((u32)(nlk->groups[idx] >> shift),
  1464. (u32 __user *)(optval + pos))) {
  1465. err = -EFAULT;
  1466. break;
  1467. }
  1468. }
  1469. if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
  1470. err = -EFAULT;
  1471. netlink_unlock_table();
  1472. break;
  1473. }
  1474. case NETLINK_CAP_ACK:
  1475. if (len < sizeof(int))
  1476. return -EINVAL;
  1477. len = sizeof(int);
  1478. val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
  1479. if (put_user(len, optlen) ||
  1480. put_user(val, optval))
  1481. return -EFAULT;
  1482. err = 0;
  1483. break;
  1484. case NETLINK_EXT_ACK:
  1485. if (len < sizeof(int))
  1486. return -EINVAL;
  1487. len = sizeof(int);
  1488. val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
  1489. if (put_user(len, optlen) || put_user(val, optval))
  1490. return -EFAULT;
  1491. err = 0;
  1492. break;
  1493. default:
  1494. err = -ENOPROTOOPT;
  1495. }
  1496. return err;
  1497. }
  1498. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1499. {
  1500. struct nl_pktinfo info;
  1501. info.group = NETLINK_CB(skb).dst_group;
  1502. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1503. }
  1504. static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
  1505. struct sk_buff *skb)
  1506. {
  1507. if (!NETLINK_CB(skb).nsid_is_set)
  1508. return;
  1509. put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
  1510. &NETLINK_CB(skb).nsid);
  1511. }
  1512. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1513. {
  1514. struct sock *sk = sock->sk;
  1515. struct netlink_sock *nlk = nlk_sk(sk);
  1516. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1517. u32 dst_portid;
  1518. u32 dst_group;
  1519. struct sk_buff *skb;
  1520. int err;
  1521. struct scm_cookie scm;
  1522. u32 netlink_skb_flags = 0;
  1523. if (msg->msg_flags&MSG_OOB)
  1524. return -EOPNOTSUPP;
  1525. err = scm_send(sock, msg, &scm, true);
  1526. if (err < 0)
  1527. return err;
  1528. if (msg->msg_namelen) {
  1529. err = -EINVAL;
  1530. if (addr->nl_family != AF_NETLINK)
  1531. goto out;
  1532. dst_portid = addr->nl_pid;
  1533. dst_group = ffs(addr->nl_groups);
  1534. err = -EPERM;
  1535. if ((dst_group || dst_portid) &&
  1536. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1537. goto out;
  1538. netlink_skb_flags |= NETLINK_SKB_DST;
  1539. } else {
  1540. dst_portid = nlk->dst_portid;
  1541. dst_group = nlk->dst_group;
  1542. }
  1543. if (!nlk->bound) {
  1544. err = netlink_autobind(sock);
  1545. if (err)
  1546. goto out;
  1547. } else {
  1548. /* Ensure nlk is hashed and visible. */
  1549. smp_rmb();
  1550. }
  1551. err = -EMSGSIZE;
  1552. if (len > sk->sk_sndbuf - 32)
  1553. goto out;
  1554. err = -ENOBUFS;
  1555. skb = netlink_alloc_large_skb(len, dst_group);
  1556. if (skb == NULL)
  1557. goto out;
  1558. NETLINK_CB(skb).portid = nlk->portid;
  1559. NETLINK_CB(skb).dst_group = dst_group;
  1560. NETLINK_CB(skb).creds = scm.creds;
  1561. NETLINK_CB(skb).flags = netlink_skb_flags;
  1562. err = -EFAULT;
  1563. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1564. kfree_skb(skb);
  1565. goto out;
  1566. }
  1567. err = security_netlink_send(sk, skb);
  1568. if (err) {
  1569. kfree_skb(skb);
  1570. goto out;
  1571. }
  1572. if (dst_group) {
  1573. refcount_inc(&skb->users);
  1574. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1575. }
  1576. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1577. out:
  1578. scm_destroy(&scm);
  1579. return err;
  1580. }
  1581. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1582. int flags)
  1583. {
  1584. struct scm_cookie scm;
  1585. struct sock *sk = sock->sk;
  1586. struct netlink_sock *nlk = nlk_sk(sk);
  1587. int noblock = flags&MSG_DONTWAIT;
  1588. size_t copied;
  1589. struct sk_buff *skb, *data_skb;
  1590. int err, ret;
  1591. if (flags&MSG_OOB)
  1592. return -EOPNOTSUPP;
  1593. copied = 0;
  1594. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1595. if (skb == NULL)
  1596. goto out;
  1597. data_skb = skb;
  1598. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1599. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1600. /*
  1601. * If this skb has a frag_list, then here that means that we
  1602. * will have to use the frag_list skb's data for compat tasks
  1603. * and the regular skb's data for normal (non-compat) tasks.
  1604. *
  1605. * If we need to send the compat skb, assign it to the
  1606. * 'data_skb' variable so that it will be used below for data
  1607. * copying. We keep 'skb' for everything else, including
  1608. * freeing both later.
  1609. */
  1610. if (flags & MSG_CMSG_COMPAT)
  1611. data_skb = skb_shinfo(skb)->frag_list;
  1612. }
  1613. #endif
  1614. /* Record the max length of recvmsg() calls for future allocations */
  1615. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  1616. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  1617. SKB_WITH_OVERHEAD(32768));
  1618. copied = data_skb->len;
  1619. if (len < copied) {
  1620. msg->msg_flags |= MSG_TRUNC;
  1621. copied = len;
  1622. }
  1623. skb_reset_transport_header(data_skb);
  1624. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  1625. if (msg->msg_name) {
  1626. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1627. addr->nl_family = AF_NETLINK;
  1628. addr->nl_pad = 0;
  1629. addr->nl_pid = NETLINK_CB(skb).portid;
  1630. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1631. msg->msg_namelen = sizeof(*addr);
  1632. }
  1633. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  1634. netlink_cmsg_recv_pktinfo(msg, skb);
  1635. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  1636. netlink_cmsg_listen_all_nsid(sk, msg, skb);
  1637. memset(&scm, 0, sizeof(scm));
  1638. scm.creds = *NETLINK_CREDS(skb);
  1639. if (flags & MSG_TRUNC)
  1640. copied = data_skb->len;
  1641. skb_free_datagram(sk, skb);
  1642. if (nlk->cb_running &&
  1643. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1644. ret = netlink_dump(sk);
  1645. if (ret) {
  1646. sk->sk_err = -ret;
  1647. sk->sk_error_report(sk);
  1648. }
  1649. }
  1650. scm_recv(sock, msg, &scm, flags);
  1651. out:
  1652. netlink_rcv_wake(sk);
  1653. return err ? : copied;
  1654. }
  1655. static void netlink_data_ready(struct sock *sk)
  1656. {
  1657. BUG();
  1658. }
  1659. /*
  1660. * We export these functions to other modules. They provide a
  1661. * complete set of kernel non-blocking support for message
  1662. * queueing.
  1663. */
  1664. struct sock *
  1665. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  1666. struct netlink_kernel_cfg *cfg)
  1667. {
  1668. struct socket *sock;
  1669. struct sock *sk;
  1670. struct netlink_sock *nlk;
  1671. struct listeners *listeners = NULL;
  1672. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  1673. unsigned int groups;
  1674. BUG_ON(!nl_table);
  1675. if (unit < 0 || unit >= MAX_LINKS)
  1676. return NULL;
  1677. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1678. return NULL;
  1679. if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
  1680. goto out_sock_release_nosk;
  1681. sk = sock->sk;
  1682. if (!cfg || cfg->groups < 32)
  1683. groups = 32;
  1684. else
  1685. groups = cfg->groups;
  1686. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1687. if (!listeners)
  1688. goto out_sock_release;
  1689. sk->sk_data_ready = netlink_data_ready;
  1690. if (cfg && cfg->input)
  1691. nlk_sk(sk)->netlink_rcv = cfg->input;
  1692. if (netlink_insert(sk, 0))
  1693. goto out_sock_release;
  1694. nlk = nlk_sk(sk);
  1695. nlk->flags |= NETLINK_F_KERNEL_SOCKET;
  1696. netlink_table_grab();
  1697. if (!nl_table[unit].registered) {
  1698. nl_table[unit].groups = groups;
  1699. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1700. nl_table[unit].cb_mutex = cb_mutex;
  1701. nl_table[unit].module = module;
  1702. if (cfg) {
  1703. nl_table[unit].bind = cfg->bind;
  1704. nl_table[unit].unbind = cfg->unbind;
  1705. nl_table[unit].flags = cfg->flags;
  1706. if (cfg->compare)
  1707. nl_table[unit].compare = cfg->compare;
  1708. }
  1709. nl_table[unit].registered = 1;
  1710. } else {
  1711. kfree(listeners);
  1712. nl_table[unit].registered++;
  1713. }
  1714. netlink_table_ungrab();
  1715. return sk;
  1716. out_sock_release:
  1717. kfree(listeners);
  1718. netlink_kernel_release(sk);
  1719. return NULL;
  1720. out_sock_release_nosk:
  1721. sock_release(sock);
  1722. return NULL;
  1723. }
  1724. EXPORT_SYMBOL(__netlink_kernel_create);
  1725. void
  1726. netlink_kernel_release(struct sock *sk)
  1727. {
  1728. if (sk == NULL || sk->sk_socket == NULL)
  1729. return;
  1730. sock_release(sk->sk_socket);
  1731. }
  1732. EXPORT_SYMBOL(netlink_kernel_release);
  1733. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1734. {
  1735. struct listeners *new, *old;
  1736. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1737. if (groups < 32)
  1738. groups = 32;
  1739. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1740. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1741. if (!new)
  1742. return -ENOMEM;
  1743. old = nl_deref_protected(tbl->listeners);
  1744. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1745. rcu_assign_pointer(tbl->listeners, new);
  1746. kfree_rcu(old, rcu);
  1747. }
  1748. tbl->groups = groups;
  1749. return 0;
  1750. }
  1751. /**
  1752. * netlink_change_ngroups - change number of multicast groups
  1753. *
  1754. * This changes the number of multicast groups that are available
  1755. * on a certain netlink family. Note that it is not possible to
  1756. * change the number of groups to below 32. Also note that it does
  1757. * not implicitly call netlink_clear_multicast_users() when the
  1758. * number of groups is reduced.
  1759. *
  1760. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1761. * @groups: The new number of groups.
  1762. */
  1763. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1764. {
  1765. int err;
  1766. netlink_table_grab();
  1767. err = __netlink_change_ngroups(sk, groups);
  1768. netlink_table_ungrab();
  1769. return err;
  1770. }
  1771. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1772. {
  1773. struct sock *sk;
  1774. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1775. sk_for_each_bound(sk, &tbl->mc_list)
  1776. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1777. }
  1778. struct nlmsghdr *
  1779. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  1780. {
  1781. struct nlmsghdr *nlh;
  1782. int size = nlmsg_msg_size(len);
  1783. nlh = skb_put(skb, NLMSG_ALIGN(size));
  1784. nlh->nlmsg_type = type;
  1785. nlh->nlmsg_len = size;
  1786. nlh->nlmsg_flags = flags;
  1787. nlh->nlmsg_pid = portid;
  1788. nlh->nlmsg_seq = seq;
  1789. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  1790. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  1791. return nlh;
  1792. }
  1793. EXPORT_SYMBOL(__nlmsg_put);
  1794. /*
  1795. * It looks a bit ugly.
  1796. * It would be better to create kernel thread.
  1797. */
  1798. static int netlink_dump(struct sock *sk)
  1799. {
  1800. struct netlink_sock *nlk = nlk_sk(sk);
  1801. struct netlink_callback *cb;
  1802. struct sk_buff *skb = NULL;
  1803. struct nlmsghdr *nlh;
  1804. struct module *module;
  1805. int len, err = -ENOBUFS;
  1806. int alloc_min_size;
  1807. int alloc_size;
  1808. mutex_lock(nlk->cb_mutex);
  1809. if (!nlk->cb_running) {
  1810. err = -EINVAL;
  1811. goto errout_skb;
  1812. }
  1813. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1814. goto errout_skb;
  1815. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  1816. * required, but it makes sense to _attempt_ a 16K bytes allocation
  1817. * to reduce number of system calls on dump operations, if user
  1818. * ever provided a big enough buffer.
  1819. */
  1820. cb = &nlk->cb;
  1821. alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  1822. if (alloc_min_size < nlk->max_recvmsg_len) {
  1823. alloc_size = nlk->max_recvmsg_len;
  1824. skb = alloc_skb(alloc_size,
  1825. (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
  1826. __GFP_NOWARN | __GFP_NORETRY);
  1827. }
  1828. if (!skb) {
  1829. alloc_size = alloc_min_size;
  1830. skb = alloc_skb(alloc_size, GFP_KERNEL);
  1831. }
  1832. if (!skb)
  1833. goto errout_skb;
  1834. /* Trim skb to allocated size. User is expected to provide buffer as
  1835. * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
  1836. * netlink_recvmsg())). dump will pack as many smaller messages as
  1837. * could fit within the allocated skb. skb is typically allocated
  1838. * with larger space than required (could be as much as near 2x the
  1839. * requested size with align to next power of 2 approach). Allowing
  1840. * dump to use the excess space makes it difficult for a user to have a
  1841. * reasonable static buffer based on the expected largest dump of a
  1842. * single netdev. The outcome is MSG_TRUNC error.
  1843. */
  1844. skb_reserve(skb, skb_tailroom(skb) - alloc_size);
  1845. netlink_skb_set_owner_r(skb, sk);
  1846. len = cb->dump(skb, cb);
  1847. if (len > 0) {
  1848. mutex_unlock(nlk->cb_mutex);
  1849. if (sk_filter(sk, skb))
  1850. kfree_skb(skb);
  1851. else
  1852. __netlink_sendskb(sk, skb);
  1853. return 0;
  1854. }
  1855. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1856. if (!nlh)
  1857. goto errout_skb;
  1858. nl_dump_check_consistent(cb, nlh);
  1859. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1860. if (sk_filter(sk, skb))
  1861. kfree_skb(skb);
  1862. else
  1863. __netlink_sendskb(sk, skb);
  1864. if (cb->done)
  1865. cb->done(cb);
  1866. nlk->cb_running = false;
  1867. module = cb->module;
  1868. skb = cb->skb;
  1869. mutex_unlock(nlk->cb_mutex);
  1870. module_put(module);
  1871. consume_skb(skb);
  1872. return 0;
  1873. errout_skb:
  1874. mutex_unlock(nlk->cb_mutex);
  1875. kfree_skb(skb);
  1876. return err;
  1877. }
  1878. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1879. const struct nlmsghdr *nlh,
  1880. struct netlink_dump_control *control)
  1881. {
  1882. struct netlink_callback *cb;
  1883. struct sock *sk;
  1884. struct netlink_sock *nlk;
  1885. int ret;
  1886. refcount_inc(&skb->users);
  1887. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  1888. if (sk == NULL) {
  1889. ret = -ECONNREFUSED;
  1890. goto error_free;
  1891. }
  1892. nlk = nlk_sk(sk);
  1893. mutex_lock(nlk->cb_mutex);
  1894. /* A dump is in progress... */
  1895. if (nlk->cb_running) {
  1896. ret = -EBUSY;
  1897. goto error_unlock;
  1898. }
  1899. /* add reference of module which cb->dump belongs to */
  1900. if (!try_module_get(control->module)) {
  1901. ret = -EPROTONOSUPPORT;
  1902. goto error_unlock;
  1903. }
  1904. cb = &nlk->cb;
  1905. memset(cb, 0, sizeof(*cb));
  1906. cb->start = control->start;
  1907. cb->dump = control->dump;
  1908. cb->done = control->done;
  1909. cb->nlh = nlh;
  1910. cb->data = control->data;
  1911. cb->module = control->module;
  1912. cb->min_dump_alloc = control->min_dump_alloc;
  1913. cb->skb = skb;
  1914. if (cb->start) {
  1915. ret = cb->start(cb);
  1916. if (ret)
  1917. goto error_unlock;
  1918. }
  1919. nlk->cb_running = true;
  1920. mutex_unlock(nlk->cb_mutex);
  1921. ret = netlink_dump(sk);
  1922. sock_put(sk);
  1923. if (ret)
  1924. return ret;
  1925. /* We successfully started a dump, by returning -EINTR we
  1926. * signal not to send ACK even if it was requested.
  1927. */
  1928. return -EINTR;
  1929. error_unlock:
  1930. sock_put(sk);
  1931. mutex_unlock(nlk->cb_mutex);
  1932. error_free:
  1933. kfree_skb(skb);
  1934. return ret;
  1935. }
  1936. EXPORT_SYMBOL(__netlink_dump_start);
  1937. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
  1938. const struct netlink_ext_ack *extack)
  1939. {
  1940. struct sk_buff *skb;
  1941. struct nlmsghdr *rep;
  1942. struct nlmsgerr *errmsg;
  1943. size_t payload = sizeof(*errmsg);
  1944. size_t tlvlen = 0;
  1945. struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
  1946. unsigned int flags = 0;
  1947. bool nlk_has_extack = nlk->flags & NETLINK_F_EXT_ACK;
  1948. /* Error messages get the original request appened, unless the user
  1949. * requests to cap the error message, and get extra error data if
  1950. * requested.
  1951. */
  1952. if (err) {
  1953. if (!(nlk->flags & NETLINK_F_CAP_ACK))
  1954. payload += nlmsg_len(nlh);
  1955. else
  1956. flags |= NLM_F_CAPPED;
  1957. if (nlk_has_extack && extack) {
  1958. if (extack->_msg)
  1959. tlvlen += nla_total_size(strlen(extack->_msg) + 1);
  1960. if (extack->bad_attr)
  1961. tlvlen += nla_total_size(sizeof(u32));
  1962. }
  1963. } else {
  1964. flags |= NLM_F_CAPPED;
  1965. if (nlk_has_extack && extack && extack->cookie_len)
  1966. tlvlen += nla_total_size(extack->cookie_len);
  1967. }
  1968. if (tlvlen)
  1969. flags |= NLM_F_ACK_TLVS;
  1970. skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
  1971. if (!skb) {
  1972. struct sock *sk;
  1973. sk = netlink_lookup(sock_net(in_skb->sk),
  1974. in_skb->sk->sk_protocol,
  1975. NETLINK_CB(in_skb).portid);
  1976. if (sk) {
  1977. sk->sk_err = ENOBUFS;
  1978. sk->sk_error_report(sk);
  1979. sock_put(sk);
  1980. }
  1981. return;
  1982. }
  1983. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  1984. NLMSG_ERROR, payload, flags);
  1985. errmsg = nlmsg_data(rep);
  1986. errmsg->error = err;
  1987. memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
  1988. if (nlk_has_extack && extack) {
  1989. if (err) {
  1990. if (extack->_msg)
  1991. WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
  1992. extack->_msg));
  1993. if (extack->bad_attr &&
  1994. !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
  1995. (u8 *)extack->bad_attr >= in_skb->data +
  1996. in_skb->len))
  1997. WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
  1998. (u8 *)extack->bad_attr -
  1999. in_skb->data));
  2000. } else {
  2001. if (extack->cookie_len)
  2002. WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
  2003. extack->cookie_len,
  2004. extack->cookie));
  2005. }
  2006. }
  2007. nlmsg_end(skb, rep);
  2008. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  2009. }
  2010. EXPORT_SYMBOL(netlink_ack);
  2011. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  2012. struct nlmsghdr *,
  2013. struct netlink_ext_ack *))
  2014. {
  2015. struct netlink_ext_ack extack = {};
  2016. struct nlmsghdr *nlh;
  2017. int err;
  2018. while (skb->len >= nlmsg_total_size(0)) {
  2019. int msglen;
  2020. nlh = nlmsg_hdr(skb);
  2021. err = 0;
  2022. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  2023. return 0;
  2024. /* Only requests are handled by the kernel */
  2025. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  2026. goto ack;
  2027. /* Skip control messages */
  2028. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  2029. goto ack;
  2030. err = cb(skb, nlh, &extack);
  2031. if (err == -EINTR)
  2032. goto skip;
  2033. ack:
  2034. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  2035. netlink_ack(skb, nlh, err, &extack);
  2036. skip:
  2037. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  2038. if (msglen > skb->len)
  2039. msglen = skb->len;
  2040. skb_pull(skb, msglen);
  2041. }
  2042. return 0;
  2043. }
  2044. EXPORT_SYMBOL(netlink_rcv_skb);
  2045. /**
  2046. * nlmsg_notify - send a notification netlink message
  2047. * @sk: netlink socket to use
  2048. * @skb: notification message
  2049. * @portid: destination netlink portid for reports or 0
  2050. * @group: destination multicast group or 0
  2051. * @report: 1 to report back, 0 to disable
  2052. * @flags: allocation flags
  2053. */
  2054. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2055. unsigned int group, int report, gfp_t flags)
  2056. {
  2057. int err = 0;
  2058. if (group) {
  2059. int exclude_portid = 0;
  2060. if (report) {
  2061. refcount_inc(&skb->users);
  2062. exclude_portid = portid;
  2063. }
  2064. /* errors reported via destination sk->sk_err, but propagate
  2065. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2066. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2067. }
  2068. if (report) {
  2069. int err2;
  2070. err2 = nlmsg_unicast(sk, skb, portid);
  2071. if (!err || err == -ESRCH)
  2072. err = err2;
  2073. }
  2074. return err;
  2075. }
  2076. EXPORT_SYMBOL(nlmsg_notify);
  2077. #ifdef CONFIG_PROC_FS
  2078. struct nl_seq_iter {
  2079. struct seq_net_private p;
  2080. struct rhashtable_iter hti;
  2081. int link;
  2082. };
  2083. static int netlink_walk_start(struct nl_seq_iter *iter)
  2084. {
  2085. int err;
  2086. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
  2087. GFP_KERNEL);
  2088. if (err) {
  2089. iter->link = MAX_LINKS;
  2090. return err;
  2091. }
  2092. err = rhashtable_walk_start(&iter->hti);
  2093. return err == -EAGAIN ? 0 : err;
  2094. }
  2095. static void netlink_walk_stop(struct nl_seq_iter *iter)
  2096. {
  2097. rhashtable_walk_stop(&iter->hti);
  2098. rhashtable_walk_exit(&iter->hti);
  2099. }
  2100. static void *__netlink_seq_next(struct seq_file *seq)
  2101. {
  2102. struct nl_seq_iter *iter = seq->private;
  2103. struct netlink_sock *nlk;
  2104. do {
  2105. for (;;) {
  2106. int err;
  2107. nlk = rhashtable_walk_next(&iter->hti);
  2108. if (IS_ERR(nlk)) {
  2109. if (PTR_ERR(nlk) == -EAGAIN)
  2110. continue;
  2111. return nlk;
  2112. }
  2113. if (nlk)
  2114. break;
  2115. netlink_walk_stop(iter);
  2116. if (++iter->link >= MAX_LINKS)
  2117. return NULL;
  2118. err = netlink_walk_start(iter);
  2119. if (err)
  2120. return ERR_PTR(err);
  2121. }
  2122. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2123. return nlk;
  2124. }
  2125. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2126. {
  2127. struct nl_seq_iter *iter = seq->private;
  2128. void *obj = SEQ_START_TOKEN;
  2129. loff_t pos;
  2130. int err;
  2131. iter->link = 0;
  2132. err = netlink_walk_start(iter);
  2133. if (err)
  2134. return ERR_PTR(err);
  2135. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2136. obj = __netlink_seq_next(seq);
  2137. return obj;
  2138. }
  2139. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2140. {
  2141. ++*pos;
  2142. return __netlink_seq_next(seq);
  2143. }
  2144. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2145. {
  2146. struct nl_seq_iter *iter = seq->private;
  2147. if (iter->link >= MAX_LINKS)
  2148. return;
  2149. netlink_walk_stop(iter);
  2150. }
  2151. static int netlink_seq_show(struct seq_file *seq, void *v)
  2152. {
  2153. if (v == SEQ_START_TOKEN) {
  2154. seq_puts(seq,
  2155. "sk Eth Pid Groups "
  2156. "Rmem Wmem Dump Locks Drops Inode\n");
  2157. } else {
  2158. struct sock *s = v;
  2159. struct netlink_sock *nlk = nlk_sk(s);
  2160. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2161. s,
  2162. s->sk_protocol,
  2163. nlk->portid,
  2164. nlk->groups ? (u32)nlk->groups[0] : 0,
  2165. sk_rmem_alloc_get(s),
  2166. sk_wmem_alloc_get(s),
  2167. nlk->cb_running,
  2168. refcount_read(&s->sk_refcnt),
  2169. atomic_read(&s->sk_drops),
  2170. sock_i_ino(s)
  2171. );
  2172. }
  2173. return 0;
  2174. }
  2175. static const struct seq_operations netlink_seq_ops = {
  2176. .start = netlink_seq_start,
  2177. .next = netlink_seq_next,
  2178. .stop = netlink_seq_stop,
  2179. .show = netlink_seq_show,
  2180. };
  2181. static int netlink_seq_open(struct inode *inode, struct file *file)
  2182. {
  2183. return seq_open_net(inode, file, &netlink_seq_ops,
  2184. sizeof(struct nl_seq_iter));
  2185. }
  2186. static const struct file_operations netlink_seq_fops = {
  2187. .owner = THIS_MODULE,
  2188. .open = netlink_seq_open,
  2189. .read = seq_read,
  2190. .llseek = seq_lseek,
  2191. .release = seq_release_net,
  2192. };
  2193. #endif
  2194. int netlink_register_notifier(struct notifier_block *nb)
  2195. {
  2196. return blocking_notifier_chain_register(&netlink_chain, nb);
  2197. }
  2198. EXPORT_SYMBOL(netlink_register_notifier);
  2199. int netlink_unregister_notifier(struct notifier_block *nb)
  2200. {
  2201. return blocking_notifier_chain_unregister(&netlink_chain, nb);
  2202. }
  2203. EXPORT_SYMBOL(netlink_unregister_notifier);
  2204. static const struct proto_ops netlink_ops = {
  2205. .family = PF_NETLINK,
  2206. .owner = THIS_MODULE,
  2207. .release = netlink_release,
  2208. .bind = netlink_bind,
  2209. .connect = netlink_connect,
  2210. .socketpair = sock_no_socketpair,
  2211. .accept = sock_no_accept,
  2212. .getname = netlink_getname,
  2213. .poll = datagram_poll,
  2214. .ioctl = netlink_ioctl,
  2215. .listen = sock_no_listen,
  2216. .shutdown = sock_no_shutdown,
  2217. .setsockopt = netlink_setsockopt,
  2218. .getsockopt = netlink_getsockopt,
  2219. .sendmsg = netlink_sendmsg,
  2220. .recvmsg = netlink_recvmsg,
  2221. .mmap = sock_no_mmap,
  2222. .sendpage = sock_no_sendpage,
  2223. };
  2224. static const struct net_proto_family netlink_family_ops = {
  2225. .family = PF_NETLINK,
  2226. .create = netlink_create,
  2227. .owner = THIS_MODULE, /* for consistency 8) */
  2228. };
  2229. static int __net_init netlink_net_init(struct net *net)
  2230. {
  2231. #ifdef CONFIG_PROC_FS
  2232. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2233. return -ENOMEM;
  2234. #endif
  2235. return 0;
  2236. }
  2237. static void __net_exit netlink_net_exit(struct net *net)
  2238. {
  2239. #ifdef CONFIG_PROC_FS
  2240. remove_proc_entry("netlink", net->proc_net);
  2241. #endif
  2242. }
  2243. static void __init netlink_add_usersock_entry(void)
  2244. {
  2245. struct listeners *listeners;
  2246. int groups = 32;
  2247. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2248. if (!listeners)
  2249. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2250. netlink_table_grab();
  2251. nl_table[NETLINK_USERSOCK].groups = groups;
  2252. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2253. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2254. nl_table[NETLINK_USERSOCK].registered = 1;
  2255. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2256. netlink_table_ungrab();
  2257. }
  2258. static struct pernet_operations __net_initdata netlink_net_ops = {
  2259. .init = netlink_net_init,
  2260. .exit = netlink_net_exit,
  2261. };
  2262. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2263. {
  2264. const struct netlink_sock *nlk = data;
  2265. struct netlink_compare_arg arg;
  2266. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2267. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2268. }
  2269. static const struct rhashtable_params netlink_rhashtable_params = {
  2270. .head_offset = offsetof(struct netlink_sock, node),
  2271. .key_len = netlink_compare_arg_len,
  2272. .obj_hashfn = netlink_hash,
  2273. .obj_cmpfn = netlink_compare,
  2274. .automatic_shrinking = true,
  2275. };
  2276. static int __init netlink_proto_init(void)
  2277. {
  2278. int i;
  2279. int err = proto_register(&netlink_proto, 0);
  2280. if (err != 0)
  2281. goto out;
  2282. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2283. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2284. if (!nl_table)
  2285. goto panic;
  2286. for (i = 0; i < MAX_LINKS; i++) {
  2287. if (rhashtable_init(&nl_table[i].hash,
  2288. &netlink_rhashtable_params) < 0) {
  2289. while (--i > 0)
  2290. rhashtable_destroy(&nl_table[i].hash);
  2291. kfree(nl_table);
  2292. goto panic;
  2293. }
  2294. }
  2295. INIT_LIST_HEAD(&netlink_tap_all);
  2296. netlink_add_usersock_entry();
  2297. sock_register(&netlink_family_ops);
  2298. register_pernet_subsys(&netlink_net_ops);
  2299. /* The netlink device handler may be needed early. */
  2300. rtnetlink_init();
  2301. out:
  2302. return err;
  2303. panic:
  2304. panic("netlink_init: Cannot allocate nl_table\n");
  2305. }
  2306. core_initcall(netlink_proto_init);