af_netlink.c 60 KB

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