af_netlink.c 61 KB

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