af_netlink.c 61 KB

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