af_netlink.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. * Patrick McHardy <kaber@trash.net>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  14. * added netlink_proto_exit
  15. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  16. * use nlk_sk, as sk->protinfo is on a diet 8)
  17. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  18. * - inc module use count of module that owns
  19. * the kernel socket in case userspace opens
  20. * socket of same protocol
  21. * - remove all module support, since netlink is
  22. * mandatory if CONFIG_NET=y these days
  23. */
  24. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/signal.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/stat.h>
  33. #include <linux/socket.h>
  34. #include <linux/un.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/termios.h>
  37. #include <linux/sockios.h>
  38. #include <linux/net.h>
  39. #include <linux/fs.h>
  40. #include <linux/slab.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/rtnetlink.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/notifier.h>
  48. #include <linux/security.h>
  49. #include <linux/jhash.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/random.h>
  52. #include <linux/bitops.h>
  53. #include <linux/mm.h>
  54. #include <linux/types.h>
  55. #include <linux/audit.h>
  56. #include <linux/mutex.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/if_arp.h>
  59. #include <linux/rhashtable.h>
  60. #include <asm/cacheflush.h>
  61. #include <linux/hash.h>
  62. #include <linux/genetlink.h>
  63. #include <net/net_namespace.h>
  64. #include <net/sock.h>
  65. #include <net/scm.h>
  66. #include <net/netlink.h>
  67. #include "af_netlink.h"
  68. struct listeners {
  69. struct rcu_head rcu;
  70. unsigned long masks[0];
  71. };
  72. /* state bits */
  73. #define NETLINK_S_CONGESTED 0x0
  74. /* flags */
  75. #define NETLINK_F_KERNEL_SOCKET 0x1
  76. #define NETLINK_F_RECV_PKTINFO 0x2
  77. #define NETLINK_F_BROADCAST_SEND_ERROR 0x4
  78. #define NETLINK_F_RECV_NO_ENOBUFS 0x8
  79. #define NETLINK_F_LISTEN_ALL_NSID 0x10
  80. #define NETLINK_F_CAP_ACK 0x20
  81. static inline int netlink_is_kernel(struct sock *sk)
  82. {
  83. return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
  84. }
  85. struct netlink_table *nl_table __read_mostly;
  86. EXPORT_SYMBOL_GPL(nl_table);
  87. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  88. static 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 ATOMIC_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. atomic_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. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1006. skb->truesize -= delta;
  1007. return skb;
  1008. }
  1009. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1010. struct sock *ssk)
  1011. {
  1012. int ret;
  1013. struct netlink_sock *nlk = nlk_sk(sk);
  1014. ret = -ECONNREFUSED;
  1015. if (nlk->netlink_rcv != NULL) {
  1016. ret = skb->len;
  1017. netlink_skb_set_owner_r(skb, sk);
  1018. NETLINK_CB(skb).sk = ssk;
  1019. netlink_deliver_tap_kernel(sk, ssk, skb);
  1020. nlk->netlink_rcv(skb);
  1021. consume_skb(skb);
  1022. } else {
  1023. kfree_skb(skb);
  1024. }
  1025. sock_put(sk);
  1026. return ret;
  1027. }
  1028. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1029. u32 portid, int nonblock)
  1030. {
  1031. struct sock *sk;
  1032. int err;
  1033. long timeo;
  1034. skb = netlink_trim(skb, gfp_any());
  1035. timeo = sock_sndtimeo(ssk, nonblock);
  1036. retry:
  1037. sk = netlink_getsockbyportid(ssk, portid);
  1038. if (IS_ERR(sk)) {
  1039. kfree_skb(skb);
  1040. return PTR_ERR(sk);
  1041. }
  1042. if (netlink_is_kernel(sk))
  1043. return netlink_unicast_kernel(sk, skb, ssk);
  1044. if (sk_filter(sk, skb)) {
  1045. err = skb->len;
  1046. kfree_skb(skb);
  1047. sock_put(sk);
  1048. return err;
  1049. }
  1050. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1051. if (err == 1)
  1052. goto retry;
  1053. if (err)
  1054. return err;
  1055. return netlink_sendskb(sk, skb);
  1056. }
  1057. EXPORT_SYMBOL(netlink_unicast);
  1058. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1059. {
  1060. int res = 0;
  1061. struct listeners *listeners;
  1062. BUG_ON(!netlink_is_kernel(sk));
  1063. rcu_read_lock();
  1064. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1065. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1066. res = test_bit(group - 1, listeners->masks);
  1067. rcu_read_unlock();
  1068. return res;
  1069. }
  1070. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1071. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1072. {
  1073. struct netlink_sock *nlk = nlk_sk(sk);
  1074. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1075. !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
  1076. netlink_skb_set_owner_r(skb, sk);
  1077. __netlink_sendskb(sk, skb);
  1078. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1079. }
  1080. return -1;
  1081. }
  1082. struct netlink_broadcast_data {
  1083. struct sock *exclude_sk;
  1084. struct net *net;
  1085. u32 portid;
  1086. u32 group;
  1087. int failure;
  1088. int delivery_failure;
  1089. int congested;
  1090. int delivered;
  1091. gfp_t allocation;
  1092. struct sk_buff *skb, *skb2;
  1093. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1094. void *tx_data;
  1095. };
  1096. static void do_one_broadcast(struct sock *sk,
  1097. struct netlink_broadcast_data *p)
  1098. {
  1099. struct netlink_sock *nlk = nlk_sk(sk);
  1100. int val;
  1101. if (p->exclude_sk == sk)
  1102. return;
  1103. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1104. !test_bit(p->group - 1, nlk->groups))
  1105. return;
  1106. if (!net_eq(sock_net(sk), p->net)) {
  1107. if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
  1108. return;
  1109. if (!peernet_has_id(sock_net(sk), p->net))
  1110. return;
  1111. if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
  1112. CAP_NET_BROADCAST))
  1113. return;
  1114. }
  1115. if (p->failure) {
  1116. netlink_overrun(sk);
  1117. return;
  1118. }
  1119. sock_hold(sk);
  1120. if (p->skb2 == NULL) {
  1121. if (skb_shared(p->skb)) {
  1122. p->skb2 = skb_clone(p->skb, p->allocation);
  1123. } else {
  1124. p->skb2 = skb_get(p->skb);
  1125. /*
  1126. * skb ownership may have been set when
  1127. * delivered to a previous socket.
  1128. */
  1129. skb_orphan(p->skb2);
  1130. }
  1131. }
  1132. if (p->skb2 == NULL) {
  1133. netlink_overrun(sk);
  1134. /* Clone failed. Notify ALL listeners. */
  1135. p->failure = 1;
  1136. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1137. p->delivery_failure = 1;
  1138. goto out;
  1139. }
  1140. if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1141. kfree_skb(p->skb2);
  1142. p->skb2 = NULL;
  1143. goto out;
  1144. }
  1145. if (sk_filter(sk, p->skb2)) {
  1146. kfree_skb(p->skb2);
  1147. p->skb2 = NULL;
  1148. goto out;
  1149. }
  1150. NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
  1151. NETLINK_CB(p->skb2).nsid_is_set = true;
  1152. val = netlink_broadcast_deliver(sk, p->skb2);
  1153. if (val < 0) {
  1154. netlink_overrun(sk);
  1155. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1156. p->delivery_failure = 1;
  1157. } else {
  1158. p->congested |= val;
  1159. p->delivered = 1;
  1160. p->skb2 = NULL;
  1161. }
  1162. out:
  1163. sock_put(sk);
  1164. }
  1165. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1166. u32 group, gfp_t allocation,
  1167. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1168. void *filter_data)
  1169. {
  1170. struct net *net = sock_net(ssk);
  1171. struct netlink_broadcast_data info;
  1172. struct sock *sk;
  1173. skb = netlink_trim(skb, allocation);
  1174. info.exclude_sk = ssk;
  1175. info.net = net;
  1176. info.portid = portid;
  1177. info.group = group;
  1178. info.failure = 0;
  1179. info.delivery_failure = 0;
  1180. info.congested = 0;
  1181. info.delivered = 0;
  1182. info.allocation = allocation;
  1183. info.skb = skb;
  1184. info.skb2 = NULL;
  1185. info.tx_filter = filter;
  1186. info.tx_data = filter_data;
  1187. /* While we sleep in clone, do not allow to change socket list */
  1188. netlink_lock_table();
  1189. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1190. do_one_broadcast(sk, &info);
  1191. consume_skb(skb);
  1192. netlink_unlock_table();
  1193. if (info.delivery_failure) {
  1194. kfree_skb(info.skb2);
  1195. return -ENOBUFS;
  1196. }
  1197. consume_skb(info.skb2);
  1198. if (info.delivered) {
  1199. if (info.congested && gfpflags_allow_blocking(allocation))
  1200. yield();
  1201. return 0;
  1202. }
  1203. return -ESRCH;
  1204. }
  1205. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1206. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1207. u32 group, gfp_t allocation)
  1208. {
  1209. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1210. NULL, NULL);
  1211. }
  1212. EXPORT_SYMBOL(netlink_broadcast);
  1213. struct netlink_set_err_data {
  1214. struct sock *exclude_sk;
  1215. u32 portid;
  1216. u32 group;
  1217. int code;
  1218. };
  1219. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1220. {
  1221. struct netlink_sock *nlk = nlk_sk(sk);
  1222. int ret = 0;
  1223. if (sk == p->exclude_sk)
  1224. goto out;
  1225. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1226. goto out;
  1227. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1228. !test_bit(p->group - 1, nlk->groups))
  1229. goto out;
  1230. if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
  1231. ret = 1;
  1232. goto out;
  1233. }
  1234. sk->sk_err = p->code;
  1235. sk->sk_error_report(sk);
  1236. out:
  1237. return ret;
  1238. }
  1239. /**
  1240. * netlink_set_err - report error to broadcast listeners
  1241. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1242. * @portid: the PORTID of a process that we want to skip (if any)
  1243. * @group: the broadcast group that will notice the error
  1244. * @code: error code, must be negative (as usual in kernelspace)
  1245. *
  1246. * This function returns the number of broadcast listeners that have set the
  1247. * NETLINK_NO_ENOBUFS socket option.
  1248. */
  1249. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1250. {
  1251. struct netlink_set_err_data info;
  1252. struct sock *sk;
  1253. int ret = 0;
  1254. info.exclude_sk = ssk;
  1255. info.portid = portid;
  1256. info.group = group;
  1257. /* sk->sk_err wants a positive error value */
  1258. info.code = -code;
  1259. read_lock(&nl_table_lock);
  1260. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1261. ret += do_one_set_err(sk, &info);
  1262. read_unlock(&nl_table_lock);
  1263. return ret;
  1264. }
  1265. EXPORT_SYMBOL(netlink_set_err);
  1266. /* must be called with netlink table grabbed */
  1267. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1268. unsigned int group,
  1269. int is_new)
  1270. {
  1271. int old, new = !!is_new, subscriptions;
  1272. old = test_bit(group - 1, nlk->groups);
  1273. subscriptions = nlk->subscriptions - old + new;
  1274. if (new)
  1275. __set_bit(group - 1, nlk->groups);
  1276. else
  1277. __clear_bit(group - 1, nlk->groups);
  1278. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1279. netlink_update_listeners(&nlk->sk);
  1280. }
  1281. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1282. char __user *optval, unsigned int optlen)
  1283. {
  1284. struct sock *sk = sock->sk;
  1285. struct netlink_sock *nlk = nlk_sk(sk);
  1286. unsigned int val = 0;
  1287. int err;
  1288. if (level != SOL_NETLINK)
  1289. return -ENOPROTOOPT;
  1290. if (optlen >= sizeof(int) &&
  1291. get_user(val, (unsigned int __user *)optval))
  1292. return -EFAULT;
  1293. switch (optname) {
  1294. case NETLINK_PKTINFO:
  1295. if (val)
  1296. nlk->flags |= NETLINK_F_RECV_PKTINFO;
  1297. else
  1298. nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
  1299. err = 0;
  1300. break;
  1301. case NETLINK_ADD_MEMBERSHIP:
  1302. case NETLINK_DROP_MEMBERSHIP: {
  1303. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1304. return -EPERM;
  1305. err = netlink_realloc_groups(sk);
  1306. if (err)
  1307. return err;
  1308. if (!val || val - 1 >= nlk->ngroups)
  1309. return -EINVAL;
  1310. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1311. err = nlk->netlink_bind(sock_net(sk), val);
  1312. if (err)
  1313. return err;
  1314. }
  1315. netlink_table_grab();
  1316. netlink_update_socket_mc(nlk, val,
  1317. optname == NETLINK_ADD_MEMBERSHIP);
  1318. netlink_table_ungrab();
  1319. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1320. nlk->netlink_unbind(sock_net(sk), val);
  1321. err = 0;
  1322. break;
  1323. }
  1324. case NETLINK_BROADCAST_ERROR:
  1325. if (val)
  1326. nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
  1327. else
  1328. nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
  1329. err = 0;
  1330. break;
  1331. case NETLINK_NO_ENOBUFS:
  1332. if (val) {
  1333. nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
  1334. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  1335. wake_up_interruptible(&nlk->wait);
  1336. } else {
  1337. nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
  1338. }
  1339. err = 0;
  1340. break;
  1341. case NETLINK_LISTEN_ALL_NSID:
  1342. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
  1343. return -EPERM;
  1344. if (val)
  1345. nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
  1346. else
  1347. nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
  1348. err = 0;
  1349. break;
  1350. case NETLINK_CAP_ACK:
  1351. if (val)
  1352. nlk->flags |= NETLINK_F_CAP_ACK;
  1353. else
  1354. nlk->flags &= ~NETLINK_F_CAP_ACK;
  1355. err = 0;
  1356. break;
  1357. default:
  1358. err = -ENOPROTOOPT;
  1359. }
  1360. return err;
  1361. }
  1362. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1363. char __user *optval, int __user *optlen)
  1364. {
  1365. struct sock *sk = sock->sk;
  1366. struct netlink_sock *nlk = nlk_sk(sk);
  1367. int len, val, err;
  1368. if (level != SOL_NETLINK)
  1369. return -ENOPROTOOPT;
  1370. if (get_user(len, optlen))
  1371. return -EFAULT;
  1372. if (len < 0)
  1373. return -EINVAL;
  1374. switch (optname) {
  1375. case NETLINK_PKTINFO:
  1376. if (len < sizeof(int))
  1377. return -EINVAL;
  1378. len = sizeof(int);
  1379. val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
  1380. if (put_user(len, optlen) ||
  1381. put_user(val, optval))
  1382. return -EFAULT;
  1383. err = 0;
  1384. break;
  1385. case NETLINK_BROADCAST_ERROR:
  1386. if (len < sizeof(int))
  1387. return -EINVAL;
  1388. len = sizeof(int);
  1389. val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
  1390. if (put_user(len, optlen) ||
  1391. put_user(val, optval))
  1392. return -EFAULT;
  1393. err = 0;
  1394. break;
  1395. case NETLINK_NO_ENOBUFS:
  1396. if (len < sizeof(int))
  1397. return -EINVAL;
  1398. len = sizeof(int);
  1399. val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
  1400. if (put_user(len, optlen) ||
  1401. put_user(val, optval))
  1402. return -EFAULT;
  1403. err = 0;
  1404. break;
  1405. case NETLINK_LIST_MEMBERSHIPS: {
  1406. int pos, idx, shift;
  1407. err = 0;
  1408. netlink_lock_table();
  1409. for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
  1410. if (len - pos < sizeof(u32))
  1411. break;
  1412. idx = pos / sizeof(unsigned long);
  1413. shift = (pos % sizeof(unsigned long)) * 8;
  1414. if (put_user((u32)(nlk->groups[idx] >> shift),
  1415. (u32 __user *)(optval + pos))) {
  1416. err = -EFAULT;
  1417. break;
  1418. }
  1419. }
  1420. if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
  1421. err = -EFAULT;
  1422. netlink_unlock_table();
  1423. break;
  1424. }
  1425. case NETLINK_CAP_ACK:
  1426. if (len < sizeof(int))
  1427. return -EINVAL;
  1428. len = sizeof(int);
  1429. val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
  1430. if (put_user(len, optlen) ||
  1431. put_user(val, optval))
  1432. return -EFAULT;
  1433. err = 0;
  1434. break;
  1435. default:
  1436. err = -ENOPROTOOPT;
  1437. }
  1438. return err;
  1439. }
  1440. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1441. {
  1442. struct nl_pktinfo info;
  1443. info.group = NETLINK_CB(skb).dst_group;
  1444. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1445. }
  1446. static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
  1447. struct sk_buff *skb)
  1448. {
  1449. if (!NETLINK_CB(skb).nsid_is_set)
  1450. return;
  1451. put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
  1452. &NETLINK_CB(skb).nsid);
  1453. }
  1454. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1455. {
  1456. struct sock *sk = sock->sk;
  1457. struct netlink_sock *nlk = nlk_sk(sk);
  1458. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1459. u32 dst_portid;
  1460. u32 dst_group;
  1461. struct sk_buff *skb;
  1462. int err;
  1463. struct scm_cookie scm;
  1464. u32 netlink_skb_flags = 0;
  1465. if (msg->msg_flags&MSG_OOB)
  1466. return -EOPNOTSUPP;
  1467. err = scm_send(sock, msg, &scm, true);
  1468. if (err < 0)
  1469. return err;
  1470. if (msg->msg_namelen) {
  1471. err = -EINVAL;
  1472. if (addr->nl_family != AF_NETLINK)
  1473. goto out;
  1474. dst_portid = addr->nl_pid;
  1475. dst_group = ffs(addr->nl_groups);
  1476. err = -EPERM;
  1477. if ((dst_group || dst_portid) &&
  1478. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1479. goto out;
  1480. netlink_skb_flags |= NETLINK_SKB_DST;
  1481. } else {
  1482. dst_portid = nlk->dst_portid;
  1483. dst_group = nlk->dst_group;
  1484. }
  1485. if (!nlk->bound) {
  1486. err = netlink_autobind(sock);
  1487. if (err)
  1488. goto out;
  1489. } else {
  1490. /* Ensure nlk is hashed and visible. */
  1491. smp_rmb();
  1492. }
  1493. err = -EMSGSIZE;
  1494. if (len > sk->sk_sndbuf - 32)
  1495. goto out;
  1496. err = -ENOBUFS;
  1497. skb = netlink_alloc_large_skb(len, dst_group);
  1498. if (skb == NULL)
  1499. goto out;
  1500. NETLINK_CB(skb).portid = nlk->portid;
  1501. NETLINK_CB(skb).dst_group = dst_group;
  1502. NETLINK_CB(skb).creds = scm.creds;
  1503. NETLINK_CB(skb).flags = netlink_skb_flags;
  1504. err = -EFAULT;
  1505. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1506. kfree_skb(skb);
  1507. goto out;
  1508. }
  1509. err = security_netlink_send(sk, skb);
  1510. if (err) {
  1511. kfree_skb(skb);
  1512. goto out;
  1513. }
  1514. if (dst_group) {
  1515. atomic_inc(&skb->users);
  1516. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1517. }
  1518. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1519. out:
  1520. scm_destroy(&scm);
  1521. return err;
  1522. }
  1523. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1524. int flags)
  1525. {
  1526. struct scm_cookie scm;
  1527. struct sock *sk = sock->sk;
  1528. struct netlink_sock *nlk = nlk_sk(sk);
  1529. int noblock = flags&MSG_DONTWAIT;
  1530. size_t copied;
  1531. struct sk_buff *skb, *data_skb;
  1532. int err, ret;
  1533. if (flags&MSG_OOB)
  1534. return -EOPNOTSUPP;
  1535. copied = 0;
  1536. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1537. if (skb == NULL)
  1538. goto out;
  1539. data_skb = skb;
  1540. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1541. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1542. /*
  1543. * If this skb has a frag_list, then here that means that we
  1544. * will have to use the frag_list skb's data for compat tasks
  1545. * and the regular skb's data for normal (non-compat) tasks.
  1546. *
  1547. * If we need to send the compat skb, assign it to the
  1548. * 'data_skb' variable so that it will be used below for data
  1549. * copying. We keep 'skb' for everything else, including
  1550. * freeing both later.
  1551. */
  1552. if (flags & MSG_CMSG_COMPAT)
  1553. data_skb = skb_shinfo(skb)->frag_list;
  1554. }
  1555. #endif
  1556. /* Record the max length of recvmsg() calls for future allocations */
  1557. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  1558. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  1559. SKB_WITH_OVERHEAD(32768));
  1560. copied = data_skb->len;
  1561. if (len < copied) {
  1562. msg->msg_flags |= MSG_TRUNC;
  1563. copied = len;
  1564. }
  1565. skb_reset_transport_header(data_skb);
  1566. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  1567. if (msg->msg_name) {
  1568. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1569. addr->nl_family = AF_NETLINK;
  1570. addr->nl_pad = 0;
  1571. addr->nl_pid = NETLINK_CB(skb).portid;
  1572. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1573. msg->msg_namelen = sizeof(*addr);
  1574. }
  1575. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  1576. netlink_cmsg_recv_pktinfo(msg, skb);
  1577. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  1578. netlink_cmsg_listen_all_nsid(sk, msg, skb);
  1579. memset(&scm, 0, sizeof(scm));
  1580. scm.creds = *NETLINK_CREDS(skb);
  1581. if (flags & MSG_TRUNC)
  1582. copied = data_skb->len;
  1583. skb_free_datagram(sk, skb);
  1584. if (nlk->cb_running &&
  1585. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1586. ret = netlink_dump(sk);
  1587. if (ret) {
  1588. sk->sk_err = -ret;
  1589. sk->sk_error_report(sk);
  1590. }
  1591. }
  1592. scm_recv(sock, msg, &scm, flags);
  1593. out:
  1594. netlink_rcv_wake(sk);
  1595. return err ? : copied;
  1596. }
  1597. static void netlink_data_ready(struct sock *sk)
  1598. {
  1599. BUG();
  1600. }
  1601. /*
  1602. * We export these functions to other modules. They provide a
  1603. * complete set of kernel non-blocking support for message
  1604. * queueing.
  1605. */
  1606. struct sock *
  1607. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  1608. struct netlink_kernel_cfg *cfg)
  1609. {
  1610. struct socket *sock;
  1611. struct sock *sk;
  1612. struct netlink_sock *nlk;
  1613. struct listeners *listeners = NULL;
  1614. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  1615. unsigned int groups;
  1616. BUG_ON(!nl_table);
  1617. if (unit < 0 || unit >= MAX_LINKS)
  1618. return NULL;
  1619. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1620. return NULL;
  1621. if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
  1622. goto out_sock_release_nosk;
  1623. sk = sock->sk;
  1624. if (!cfg || cfg->groups < 32)
  1625. groups = 32;
  1626. else
  1627. groups = cfg->groups;
  1628. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1629. if (!listeners)
  1630. goto out_sock_release;
  1631. sk->sk_data_ready = netlink_data_ready;
  1632. if (cfg && cfg->input)
  1633. nlk_sk(sk)->netlink_rcv = cfg->input;
  1634. if (netlink_insert(sk, 0))
  1635. goto out_sock_release;
  1636. nlk = nlk_sk(sk);
  1637. nlk->flags |= NETLINK_F_KERNEL_SOCKET;
  1638. netlink_table_grab();
  1639. if (!nl_table[unit].registered) {
  1640. nl_table[unit].groups = groups;
  1641. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1642. nl_table[unit].cb_mutex = cb_mutex;
  1643. nl_table[unit].module = module;
  1644. if (cfg) {
  1645. nl_table[unit].bind = cfg->bind;
  1646. nl_table[unit].unbind = cfg->unbind;
  1647. nl_table[unit].flags = cfg->flags;
  1648. if (cfg->compare)
  1649. nl_table[unit].compare = cfg->compare;
  1650. }
  1651. nl_table[unit].registered = 1;
  1652. } else {
  1653. kfree(listeners);
  1654. nl_table[unit].registered++;
  1655. }
  1656. netlink_table_ungrab();
  1657. return sk;
  1658. out_sock_release:
  1659. kfree(listeners);
  1660. netlink_kernel_release(sk);
  1661. return NULL;
  1662. out_sock_release_nosk:
  1663. sock_release(sock);
  1664. return NULL;
  1665. }
  1666. EXPORT_SYMBOL(__netlink_kernel_create);
  1667. void
  1668. netlink_kernel_release(struct sock *sk)
  1669. {
  1670. if (sk == NULL || sk->sk_socket == NULL)
  1671. return;
  1672. sock_release(sk->sk_socket);
  1673. }
  1674. EXPORT_SYMBOL(netlink_kernel_release);
  1675. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1676. {
  1677. struct listeners *new, *old;
  1678. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1679. if (groups < 32)
  1680. groups = 32;
  1681. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1682. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1683. if (!new)
  1684. return -ENOMEM;
  1685. old = nl_deref_protected(tbl->listeners);
  1686. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1687. rcu_assign_pointer(tbl->listeners, new);
  1688. kfree_rcu(old, rcu);
  1689. }
  1690. tbl->groups = groups;
  1691. return 0;
  1692. }
  1693. /**
  1694. * netlink_change_ngroups - change number of multicast groups
  1695. *
  1696. * This changes the number of multicast groups that are available
  1697. * on a certain netlink family. Note that it is not possible to
  1698. * change the number of groups to below 32. Also note that it does
  1699. * not implicitly call netlink_clear_multicast_users() when the
  1700. * number of groups is reduced.
  1701. *
  1702. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1703. * @groups: The new number of groups.
  1704. */
  1705. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1706. {
  1707. int err;
  1708. netlink_table_grab();
  1709. err = __netlink_change_ngroups(sk, groups);
  1710. netlink_table_ungrab();
  1711. return err;
  1712. }
  1713. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1714. {
  1715. struct sock *sk;
  1716. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1717. sk_for_each_bound(sk, &tbl->mc_list)
  1718. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1719. }
  1720. struct nlmsghdr *
  1721. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  1722. {
  1723. struct nlmsghdr *nlh;
  1724. int size = nlmsg_msg_size(len);
  1725. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  1726. nlh->nlmsg_type = type;
  1727. nlh->nlmsg_len = size;
  1728. nlh->nlmsg_flags = flags;
  1729. nlh->nlmsg_pid = portid;
  1730. nlh->nlmsg_seq = seq;
  1731. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  1732. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  1733. return nlh;
  1734. }
  1735. EXPORT_SYMBOL(__nlmsg_put);
  1736. /*
  1737. * It looks a bit ugly.
  1738. * It would be better to create kernel thread.
  1739. */
  1740. static int netlink_dump(struct sock *sk)
  1741. {
  1742. struct netlink_sock *nlk = nlk_sk(sk);
  1743. struct netlink_callback *cb;
  1744. struct sk_buff *skb = NULL;
  1745. struct nlmsghdr *nlh;
  1746. struct module *module;
  1747. int len, err = -ENOBUFS;
  1748. int alloc_min_size;
  1749. int alloc_size;
  1750. mutex_lock(nlk->cb_mutex);
  1751. if (!nlk->cb_running) {
  1752. err = -EINVAL;
  1753. goto errout_skb;
  1754. }
  1755. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1756. goto errout_skb;
  1757. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  1758. * required, but it makes sense to _attempt_ a 16K bytes allocation
  1759. * to reduce number of system calls on dump operations, if user
  1760. * ever provided a big enough buffer.
  1761. */
  1762. cb = &nlk->cb;
  1763. alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  1764. if (alloc_min_size < nlk->max_recvmsg_len) {
  1765. alloc_size = nlk->max_recvmsg_len;
  1766. skb = alloc_skb(alloc_size,
  1767. (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
  1768. __GFP_NOWARN | __GFP_NORETRY);
  1769. }
  1770. if (!skb) {
  1771. alloc_size = alloc_min_size;
  1772. skb = alloc_skb(alloc_size, GFP_KERNEL);
  1773. }
  1774. if (!skb)
  1775. goto errout_skb;
  1776. /* Trim skb to allocated size. User is expected to provide buffer as
  1777. * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
  1778. * netlink_recvmsg())). dump will pack as many smaller messages as
  1779. * could fit within the allocated skb. skb is typically allocated
  1780. * with larger space than required (could be as much as near 2x the
  1781. * requested size with align to next power of 2 approach). Allowing
  1782. * dump to use the excess space makes it difficult for a user to have a
  1783. * reasonable static buffer based on the expected largest dump of a
  1784. * single netdev. The outcome is MSG_TRUNC error.
  1785. */
  1786. skb_reserve(skb, skb_tailroom(skb) - alloc_size);
  1787. netlink_skb_set_owner_r(skb, sk);
  1788. len = cb->dump(skb, cb);
  1789. if (len > 0) {
  1790. mutex_unlock(nlk->cb_mutex);
  1791. if (sk_filter(sk, skb))
  1792. kfree_skb(skb);
  1793. else
  1794. __netlink_sendskb(sk, skb);
  1795. return 0;
  1796. }
  1797. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1798. if (!nlh)
  1799. goto errout_skb;
  1800. nl_dump_check_consistent(cb, nlh);
  1801. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1802. if (sk_filter(sk, skb))
  1803. kfree_skb(skb);
  1804. else
  1805. __netlink_sendskb(sk, skb);
  1806. if (cb->done)
  1807. cb->done(cb);
  1808. nlk->cb_running = false;
  1809. module = cb->module;
  1810. skb = cb->skb;
  1811. mutex_unlock(nlk->cb_mutex);
  1812. module_put(module);
  1813. consume_skb(skb);
  1814. return 0;
  1815. errout_skb:
  1816. mutex_unlock(nlk->cb_mutex);
  1817. kfree_skb(skb);
  1818. return err;
  1819. }
  1820. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1821. const struct nlmsghdr *nlh,
  1822. struct netlink_dump_control *control)
  1823. {
  1824. struct netlink_callback *cb;
  1825. struct sock *sk;
  1826. struct netlink_sock *nlk;
  1827. int ret;
  1828. atomic_inc(&skb->users);
  1829. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  1830. if (sk == NULL) {
  1831. ret = -ECONNREFUSED;
  1832. goto error_free;
  1833. }
  1834. nlk = nlk_sk(sk);
  1835. mutex_lock(nlk->cb_mutex);
  1836. /* A dump is in progress... */
  1837. if (nlk->cb_running) {
  1838. ret = -EBUSY;
  1839. goto error_unlock;
  1840. }
  1841. /* add reference of module which cb->dump belongs to */
  1842. if (!try_module_get(control->module)) {
  1843. ret = -EPROTONOSUPPORT;
  1844. goto error_unlock;
  1845. }
  1846. cb = &nlk->cb;
  1847. memset(cb, 0, sizeof(*cb));
  1848. cb->start = control->start;
  1849. cb->dump = control->dump;
  1850. cb->done = control->done;
  1851. cb->nlh = nlh;
  1852. cb->data = control->data;
  1853. cb->module = control->module;
  1854. cb->min_dump_alloc = control->min_dump_alloc;
  1855. cb->skb = skb;
  1856. nlk->cb_running = true;
  1857. mutex_unlock(nlk->cb_mutex);
  1858. ret = 0;
  1859. if (cb->start)
  1860. ret = cb->start(cb);
  1861. if (!ret)
  1862. ret = netlink_dump(sk);
  1863. sock_put(sk);
  1864. if (ret)
  1865. return ret;
  1866. /* We successfully started a dump, by returning -EINTR we
  1867. * signal not to send ACK even if it was requested.
  1868. */
  1869. return -EINTR;
  1870. error_unlock:
  1871. sock_put(sk);
  1872. mutex_unlock(nlk->cb_mutex);
  1873. error_free:
  1874. kfree_skb(skb);
  1875. return ret;
  1876. }
  1877. EXPORT_SYMBOL(__netlink_dump_start);
  1878. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1879. {
  1880. struct sk_buff *skb;
  1881. struct nlmsghdr *rep;
  1882. struct nlmsgerr *errmsg;
  1883. size_t payload = sizeof(*errmsg);
  1884. struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
  1885. /* Error messages get the original request appened, unless the user
  1886. * requests to cap the error message.
  1887. */
  1888. if (!(nlk->flags & NETLINK_F_CAP_ACK) && err)
  1889. payload += nlmsg_len(nlh);
  1890. skb = nlmsg_new(payload, GFP_KERNEL);
  1891. if (!skb) {
  1892. struct sock *sk;
  1893. sk = netlink_lookup(sock_net(in_skb->sk),
  1894. in_skb->sk->sk_protocol,
  1895. NETLINK_CB(in_skb).portid);
  1896. if (sk) {
  1897. sk->sk_err = ENOBUFS;
  1898. sk->sk_error_report(sk);
  1899. sock_put(sk);
  1900. }
  1901. return;
  1902. }
  1903. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  1904. NLMSG_ERROR, payload, 0);
  1905. errmsg = nlmsg_data(rep);
  1906. errmsg->error = err;
  1907. memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
  1908. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  1909. }
  1910. EXPORT_SYMBOL(netlink_ack);
  1911. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1912. struct nlmsghdr *))
  1913. {
  1914. struct nlmsghdr *nlh;
  1915. int err;
  1916. while (skb->len >= nlmsg_total_size(0)) {
  1917. int msglen;
  1918. nlh = nlmsg_hdr(skb);
  1919. err = 0;
  1920. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1921. return 0;
  1922. /* Only requests are handled by the kernel */
  1923. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1924. goto ack;
  1925. /* Skip control messages */
  1926. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1927. goto ack;
  1928. err = cb(skb, nlh);
  1929. if (err == -EINTR)
  1930. goto skip;
  1931. ack:
  1932. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1933. netlink_ack(skb, nlh, err);
  1934. skip:
  1935. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1936. if (msglen > skb->len)
  1937. msglen = skb->len;
  1938. skb_pull(skb, msglen);
  1939. }
  1940. return 0;
  1941. }
  1942. EXPORT_SYMBOL(netlink_rcv_skb);
  1943. /**
  1944. * nlmsg_notify - send a notification netlink message
  1945. * @sk: netlink socket to use
  1946. * @skb: notification message
  1947. * @portid: destination netlink portid for reports or 0
  1948. * @group: destination multicast group or 0
  1949. * @report: 1 to report back, 0 to disable
  1950. * @flags: allocation flags
  1951. */
  1952. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  1953. unsigned int group, int report, gfp_t flags)
  1954. {
  1955. int err = 0;
  1956. if (group) {
  1957. int exclude_portid = 0;
  1958. if (report) {
  1959. atomic_inc(&skb->users);
  1960. exclude_portid = portid;
  1961. }
  1962. /* errors reported via destination sk->sk_err, but propagate
  1963. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1964. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  1965. }
  1966. if (report) {
  1967. int err2;
  1968. err2 = nlmsg_unicast(sk, skb, portid);
  1969. if (!err || err == -ESRCH)
  1970. err = err2;
  1971. }
  1972. return err;
  1973. }
  1974. EXPORT_SYMBOL(nlmsg_notify);
  1975. #ifdef CONFIG_PROC_FS
  1976. struct nl_seq_iter {
  1977. struct seq_net_private p;
  1978. struct rhashtable_iter hti;
  1979. int link;
  1980. };
  1981. static int netlink_walk_start(struct nl_seq_iter *iter)
  1982. {
  1983. int err;
  1984. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
  1985. GFP_KERNEL);
  1986. if (err) {
  1987. iter->link = MAX_LINKS;
  1988. return err;
  1989. }
  1990. err = rhashtable_walk_start(&iter->hti);
  1991. return err == -EAGAIN ? 0 : err;
  1992. }
  1993. static void netlink_walk_stop(struct nl_seq_iter *iter)
  1994. {
  1995. rhashtable_walk_stop(&iter->hti);
  1996. rhashtable_walk_exit(&iter->hti);
  1997. }
  1998. static void *__netlink_seq_next(struct seq_file *seq)
  1999. {
  2000. struct nl_seq_iter *iter = seq->private;
  2001. struct netlink_sock *nlk;
  2002. do {
  2003. for (;;) {
  2004. int err;
  2005. nlk = rhashtable_walk_next(&iter->hti);
  2006. if (IS_ERR(nlk)) {
  2007. if (PTR_ERR(nlk) == -EAGAIN)
  2008. continue;
  2009. return nlk;
  2010. }
  2011. if (nlk)
  2012. break;
  2013. netlink_walk_stop(iter);
  2014. if (++iter->link >= MAX_LINKS)
  2015. return NULL;
  2016. err = netlink_walk_start(iter);
  2017. if (err)
  2018. return ERR_PTR(err);
  2019. }
  2020. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2021. return nlk;
  2022. }
  2023. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2024. {
  2025. struct nl_seq_iter *iter = seq->private;
  2026. void *obj = SEQ_START_TOKEN;
  2027. loff_t pos;
  2028. int err;
  2029. iter->link = 0;
  2030. err = netlink_walk_start(iter);
  2031. if (err)
  2032. return ERR_PTR(err);
  2033. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2034. obj = __netlink_seq_next(seq);
  2035. return obj;
  2036. }
  2037. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2038. {
  2039. ++*pos;
  2040. return __netlink_seq_next(seq);
  2041. }
  2042. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2043. {
  2044. struct nl_seq_iter *iter = seq->private;
  2045. if (iter->link >= MAX_LINKS)
  2046. return;
  2047. netlink_walk_stop(iter);
  2048. }
  2049. static int netlink_seq_show(struct seq_file *seq, void *v)
  2050. {
  2051. if (v == SEQ_START_TOKEN) {
  2052. seq_puts(seq,
  2053. "sk Eth Pid Groups "
  2054. "Rmem Wmem Dump Locks Drops Inode\n");
  2055. } else {
  2056. struct sock *s = v;
  2057. struct netlink_sock *nlk = nlk_sk(s);
  2058. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2059. s,
  2060. s->sk_protocol,
  2061. nlk->portid,
  2062. nlk->groups ? (u32)nlk->groups[0] : 0,
  2063. sk_rmem_alloc_get(s),
  2064. sk_wmem_alloc_get(s),
  2065. nlk->cb_running,
  2066. atomic_read(&s->sk_refcnt),
  2067. atomic_read(&s->sk_drops),
  2068. sock_i_ino(s)
  2069. );
  2070. }
  2071. return 0;
  2072. }
  2073. static const struct seq_operations netlink_seq_ops = {
  2074. .start = netlink_seq_start,
  2075. .next = netlink_seq_next,
  2076. .stop = netlink_seq_stop,
  2077. .show = netlink_seq_show,
  2078. };
  2079. static int netlink_seq_open(struct inode *inode, struct file *file)
  2080. {
  2081. return seq_open_net(inode, file, &netlink_seq_ops,
  2082. sizeof(struct nl_seq_iter));
  2083. }
  2084. static const struct file_operations netlink_seq_fops = {
  2085. .owner = THIS_MODULE,
  2086. .open = netlink_seq_open,
  2087. .read = seq_read,
  2088. .llseek = seq_lseek,
  2089. .release = seq_release_net,
  2090. };
  2091. #endif
  2092. int netlink_register_notifier(struct notifier_block *nb)
  2093. {
  2094. return atomic_notifier_chain_register(&netlink_chain, nb);
  2095. }
  2096. EXPORT_SYMBOL(netlink_register_notifier);
  2097. int netlink_unregister_notifier(struct notifier_block *nb)
  2098. {
  2099. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2100. }
  2101. EXPORT_SYMBOL(netlink_unregister_notifier);
  2102. static const struct proto_ops netlink_ops = {
  2103. .family = PF_NETLINK,
  2104. .owner = THIS_MODULE,
  2105. .release = netlink_release,
  2106. .bind = netlink_bind,
  2107. .connect = netlink_connect,
  2108. .socketpair = sock_no_socketpair,
  2109. .accept = sock_no_accept,
  2110. .getname = netlink_getname,
  2111. .poll = datagram_poll,
  2112. .ioctl = netlink_ioctl,
  2113. .listen = sock_no_listen,
  2114. .shutdown = sock_no_shutdown,
  2115. .setsockopt = netlink_setsockopt,
  2116. .getsockopt = netlink_getsockopt,
  2117. .sendmsg = netlink_sendmsg,
  2118. .recvmsg = netlink_recvmsg,
  2119. .mmap = sock_no_mmap,
  2120. .sendpage = sock_no_sendpage,
  2121. };
  2122. static const struct net_proto_family netlink_family_ops = {
  2123. .family = PF_NETLINK,
  2124. .create = netlink_create,
  2125. .owner = THIS_MODULE, /* for consistency 8) */
  2126. };
  2127. static int __net_init netlink_net_init(struct net *net)
  2128. {
  2129. #ifdef CONFIG_PROC_FS
  2130. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2131. return -ENOMEM;
  2132. #endif
  2133. return 0;
  2134. }
  2135. static void __net_exit netlink_net_exit(struct net *net)
  2136. {
  2137. #ifdef CONFIG_PROC_FS
  2138. remove_proc_entry("netlink", net->proc_net);
  2139. #endif
  2140. }
  2141. static void __init netlink_add_usersock_entry(void)
  2142. {
  2143. struct listeners *listeners;
  2144. int groups = 32;
  2145. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2146. if (!listeners)
  2147. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2148. netlink_table_grab();
  2149. nl_table[NETLINK_USERSOCK].groups = groups;
  2150. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2151. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2152. nl_table[NETLINK_USERSOCK].registered = 1;
  2153. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2154. netlink_table_ungrab();
  2155. }
  2156. static struct pernet_operations __net_initdata netlink_net_ops = {
  2157. .init = netlink_net_init,
  2158. .exit = netlink_net_exit,
  2159. };
  2160. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2161. {
  2162. const struct netlink_sock *nlk = data;
  2163. struct netlink_compare_arg arg;
  2164. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2165. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2166. }
  2167. static const struct rhashtable_params netlink_rhashtable_params = {
  2168. .head_offset = offsetof(struct netlink_sock, node),
  2169. .key_len = netlink_compare_arg_len,
  2170. .obj_hashfn = netlink_hash,
  2171. .obj_cmpfn = netlink_compare,
  2172. .automatic_shrinking = true,
  2173. };
  2174. static int __init netlink_proto_init(void)
  2175. {
  2176. int i;
  2177. int err = proto_register(&netlink_proto, 0);
  2178. if (err != 0)
  2179. goto out;
  2180. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2181. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2182. if (!nl_table)
  2183. goto panic;
  2184. for (i = 0; i < MAX_LINKS; i++) {
  2185. if (rhashtable_init(&nl_table[i].hash,
  2186. &netlink_rhashtable_params) < 0) {
  2187. while (--i > 0)
  2188. rhashtable_destroy(&nl_table[i].hash);
  2189. kfree(nl_table);
  2190. goto panic;
  2191. }
  2192. }
  2193. INIT_LIST_HEAD(&netlink_tap_all);
  2194. netlink_add_usersock_entry();
  2195. sock_register(&netlink_family_ops);
  2196. register_pernet_subsys(&netlink_net_ops);
  2197. /* The netlink device handler may be needed early. */
  2198. rtnetlink_init();
  2199. out:
  2200. return err;
  2201. panic:
  2202. panic("netlink_init: Cannot allocate nl_table\n");
  2203. }
  2204. core_initcall(netlink_proto_init);