af_netlink.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  13. * added netlink_proto_exit
  14. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  15. * use nlk_sk, as sk->protinfo is on a diet 8)
  16. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  17. * - inc module use count of module that owns
  18. * the kernel socket in case userspace opens
  19. * socket of same protocol
  20. * - remove all module support, since netlink is
  21. * mandatory if CONFIG_NET=y these days
  22. */
  23. #include <linux/module.h>
  24. #include <linux/capability.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/signal.h>
  28. #include <linux/sched.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/stat.h>
  32. #include <linux/socket.h>
  33. #include <linux/un.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/termios.h>
  36. #include <linux/sockios.h>
  37. #include <linux/net.h>
  38. #include <linux/fs.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/notifier.h>
  47. #include <linux/security.h>
  48. #include <linux/jhash.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/random.h>
  51. #include <linux/bitops.h>
  52. #include <linux/mm.h>
  53. #include <linux/types.h>
  54. #include <linux/audit.h>
  55. #include <linux/mutex.h>
  56. #include <net/net_namespace.h>
  57. #include <net/sock.h>
  58. #include <net/scm.h>
  59. #include <net/netlink.h>
  60. #include "af_netlink.h"
  61. struct listeners {
  62. struct rcu_head rcu;
  63. unsigned long masks[0];
  64. };
  65. /* state bits */
  66. #define NETLINK_CONGESTED 0x0
  67. /* flags */
  68. #define NETLINK_KERNEL_SOCKET 0x1
  69. #define NETLINK_RECV_PKTINFO 0x2
  70. #define NETLINK_BROADCAST_SEND_ERROR 0x4
  71. #define NETLINK_RECV_NO_ENOBUFS 0x8
  72. static inline int netlink_is_kernel(struct sock *sk)
  73. {
  74. return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
  75. }
  76. struct netlink_table *nl_table;
  77. EXPORT_SYMBOL_GPL(nl_table);
  78. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  79. static int netlink_dump(struct sock *sk);
  80. DEFINE_RWLOCK(nl_table_lock);
  81. EXPORT_SYMBOL_GPL(nl_table_lock);
  82. static atomic_t nl_table_users = ATOMIC_INIT(0);
  83. #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
  84. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  85. static inline u32 netlink_group_mask(u32 group)
  86. {
  87. return group ? 1 << (group - 1) : 0;
  88. }
  89. static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
  90. {
  91. return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
  92. }
  93. static void netlink_destroy_callback(struct netlink_callback *cb)
  94. {
  95. kfree_skb(cb->skb);
  96. kfree(cb);
  97. }
  98. static void netlink_consume_callback(struct netlink_callback *cb)
  99. {
  100. consume_skb(cb->skb);
  101. kfree(cb);
  102. }
  103. static void netlink_sock_destruct(struct sock *sk)
  104. {
  105. struct netlink_sock *nlk = nlk_sk(sk);
  106. if (nlk->cb) {
  107. if (nlk->cb->done)
  108. nlk->cb->done(nlk->cb);
  109. module_put(nlk->cb->module);
  110. netlink_destroy_callback(nlk->cb);
  111. }
  112. skb_queue_purge(&sk->sk_receive_queue);
  113. if (!sock_flag(sk, SOCK_DEAD)) {
  114. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  115. return;
  116. }
  117. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  118. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  119. WARN_ON(nlk_sk(sk)->groups);
  120. }
  121. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  122. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  123. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  124. * this, _but_ remember, it adds useless work on UP machines.
  125. */
  126. void netlink_table_grab(void)
  127. __acquires(nl_table_lock)
  128. {
  129. might_sleep();
  130. write_lock_irq(&nl_table_lock);
  131. if (atomic_read(&nl_table_users)) {
  132. DECLARE_WAITQUEUE(wait, current);
  133. add_wait_queue_exclusive(&nl_table_wait, &wait);
  134. for (;;) {
  135. set_current_state(TASK_UNINTERRUPTIBLE);
  136. if (atomic_read(&nl_table_users) == 0)
  137. break;
  138. write_unlock_irq(&nl_table_lock);
  139. schedule();
  140. write_lock_irq(&nl_table_lock);
  141. }
  142. __set_current_state(TASK_RUNNING);
  143. remove_wait_queue(&nl_table_wait, &wait);
  144. }
  145. }
  146. void netlink_table_ungrab(void)
  147. __releases(nl_table_lock)
  148. {
  149. write_unlock_irq(&nl_table_lock);
  150. wake_up(&nl_table_wait);
  151. }
  152. static inline void
  153. netlink_lock_table(void)
  154. {
  155. /* read_lock() synchronizes us to netlink_table_grab */
  156. read_lock(&nl_table_lock);
  157. atomic_inc(&nl_table_users);
  158. read_unlock(&nl_table_lock);
  159. }
  160. static inline void
  161. netlink_unlock_table(void)
  162. {
  163. if (atomic_dec_and_test(&nl_table_users))
  164. wake_up(&nl_table_wait);
  165. }
  166. static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
  167. {
  168. struct nl_portid_hash *hash = &nl_table[protocol].hash;
  169. struct hlist_head *head;
  170. struct sock *sk;
  171. read_lock(&nl_table_lock);
  172. head = nl_portid_hashfn(hash, portid);
  173. sk_for_each(sk, head) {
  174. if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
  175. sock_hold(sk);
  176. goto found;
  177. }
  178. }
  179. sk = NULL;
  180. found:
  181. read_unlock(&nl_table_lock);
  182. return sk;
  183. }
  184. static struct hlist_head *nl_portid_hash_zalloc(size_t size)
  185. {
  186. if (size <= PAGE_SIZE)
  187. return kzalloc(size, GFP_ATOMIC);
  188. else
  189. return (struct hlist_head *)
  190. __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
  191. get_order(size));
  192. }
  193. static void nl_portid_hash_free(struct hlist_head *table, size_t size)
  194. {
  195. if (size <= PAGE_SIZE)
  196. kfree(table);
  197. else
  198. free_pages((unsigned long)table, get_order(size));
  199. }
  200. static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
  201. {
  202. unsigned int omask, mask, shift;
  203. size_t osize, size;
  204. struct hlist_head *otable, *table;
  205. int i;
  206. omask = mask = hash->mask;
  207. osize = size = (mask + 1) * sizeof(*table);
  208. shift = hash->shift;
  209. if (grow) {
  210. if (++shift > hash->max_shift)
  211. return 0;
  212. mask = mask * 2 + 1;
  213. size *= 2;
  214. }
  215. table = nl_portid_hash_zalloc(size);
  216. if (!table)
  217. return 0;
  218. otable = hash->table;
  219. hash->table = table;
  220. hash->mask = mask;
  221. hash->shift = shift;
  222. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  223. for (i = 0; i <= omask; i++) {
  224. struct sock *sk;
  225. struct hlist_node *tmp;
  226. sk_for_each_safe(sk, tmp, &otable[i])
  227. __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
  228. }
  229. nl_portid_hash_free(otable, osize);
  230. hash->rehash_time = jiffies + 10 * 60 * HZ;
  231. return 1;
  232. }
  233. static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
  234. {
  235. int avg = hash->entries >> hash->shift;
  236. if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
  237. return 1;
  238. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  239. nl_portid_hash_rehash(hash, 0);
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. static const struct proto_ops netlink_ops;
  245. static void
  246. netlink_update_listeners(struct sock *sk)
  247. {
  248. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  249. unsigned long mask;
  250. unsigned int i;
  251. struct listeners *listeners;
  252. listeners = nl_deref_protected(tbl->listeners);
  253. if (!listeners)
  254. return;
  255. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  256. mask = 0;
  257. sk_for_each_bound(sk, &tbl->mc_list) {
  258. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  259. mask |= nlk_sk(sk)->groups[i];
  260. }
  261. listeners->masks[i] = mask;
  262. }
  263. /* this function is only called with the netlink table "grabbed", which
  264. * makes sure updates are visible before bind or setsockopt return. */
  265. }
  266. static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
  267. {
  268. struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
  269. struct hlist_head *head;
  270. int err = -EADDRINUSE;
  271. struct sock *osk;
  272. int len;
  273. netlink_table_grab();
  274. head = nl_portid_hashfn(hash, portid);
  275. len = 0;
  276. sk_for_each(osk, head) {
  277. if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
  278. break;
  279. len++;
  280. }
  281. if (osk)
  282. goto err;
  283. err = -EBUSY;
  284. if (nlk_sk(sk)->portid)
  285. goto err;
  286. err = -ENOMEM;
  287. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  288. goto err;
  289. if (len && nl_portid_hash_dilute(hash, len))
  290. head = nl_portid_hashfn(hash, portid);
  291. hash->entries++;
  292. nlk_sk(sk)->portid = portid;
  293. sk_add_node(sk, head);
  294. err = 0;
  295. err:
  296. netlink_table_ungrab();
  297. return err;
  298. }
  299. static void netlink_remove(struct sock *sk)
  300. {
  301. netlink_table_grab();
  302. if (sk_del_node_init(sk))
  303. nl_table[sk->sk_protocol].hash.entries--;
  304. if (nlk_sk(sk)->subscriptions)
  305. __sk_del_bind_node(sk);
  306. netlink_table_ungrab();
  307. }
  308. static struct proto netlink_proto = {
  309. .name = "NETLINK",
  310. .owner = THIS_MODULE,
  311. .obj_size = sizeof(struct netlink_sock),
  312. };
  313. static int __netlink_create(struct net *net, struct socket *sock,
  314. struct mutex *cb_mutex, int protocol)
  315. {
  316. struct sock *sk;
  317. struct netlink_sock *nlk;
  318. sock->ops = &netlink_ops;
  319. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  320. if (!sk)
  321. return -ENOMEM;
  322. sock_init_data(sock, sk);
  323. nlk = nlk_sk(sk);
  324. if (cb_mutex) {
  325. nlk->cb_mutex = cb_mutex;
  326. } else {
  327. nlk->cb_mutex = &nlk->cb_def_mutex;
  328. mutex_init(nlk->cb_mutex);
  329. }
  330. init_waitqueue_head(&nlk->wait);
  331. sk->sk_destruct = netlink_sock_destruct;
  332. sk->sk_protocol = protocol;
  333. return 0;
  334. }
  335. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  336. int kern)
  337. {
  338. struct module *module = NULL;
  339. struct mutex *cb_mutex;
  340. struct netlink_sock *nlk;
  341. void (*bind)(int group);
  342. int err = 0;
  343. sock->state = SS_UNCONNECTED;
  344. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  345. return -ESOCKTNOSUPPORT;
  346. if (protocol < 0 || protocol >= MAX_LINKS)
  347. return -EPROTONOSUPPORT;
  348. netlink_lock_table();
  349. #ifdef CONFIG_MODULES
  350. if (!nl_table[protocol].registered) {
  351. netlink_unlock_table();
  352. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  353. netlink_lock_table();
  354. }
  355. #endif
  356. if (nl_table[protocol].registered &&
  357. try_module_get(nl_table[protocol].module))
  358. module = nl_table[protocol].module;
  359. else
  360. err = -EPROTONOSUPPORT;
  361. cb_mutex = nl_table[protocol].cb_mutex;
  362. bind = nl_table[protocol].bind;
  363. netlink_unlock_table();
  364. if (err < 0)
  365. goto out;
  366. err = __netlink_create(net, sock, cb_mutex, protocol);
  367. if (err < 0)
  368. goto out_module;
  369. local_bh_disable();
  370. sock_prot_inuse_add(net, &netlink_proto, 1);
  371. local_bh_enable();
  372. nlk = nlk_sk(sock->sk);
  373. nlk->module = module;
  374. nlk->netlink_bind = bind;
  375. out:
  376. return err;
  377. out_module:
  378. module_put(module);
  379. goto out;
  380. }
  381. static int netlink_release(struct socket *sock)
  382. {
  383. struct sock *sk = sock->sk;
  384. struct netlink_sock *nlk;
  385. if (!sk)
  386. return 0;
  387. netlink_remove(sk);
  388. sock_orphan(sk);
  389. nlk = nlk_sk(sk);
  390. /*
  391. * OK. Socket is unlinked, any packets that arrive now
  392. * will be purged.
  393. */
  394. sock->sk = NULL;
  395. wake_up_interruptible_all(&nlk->wait);
  396. skb_queue_purge(&sk->sk_write_queue);
  397. if (nlk->portid) {
  398. struct netlink_notify n = {
  399. .net = sock_net(sk),
  400. .protocol = sk->sk_protocol,
  401. .portid = nlk->portid,
  402. };
  403. atomic_notifier_call_chain(&netlink_chain,
  404. NETLINK_URELEASE, &n);
  405. }
  406. module_put(nlk->module);
  407. netlink_table_grab();
  408. if (netlink_is_kernel(sk)) {
  409. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  410. if (--nl_table[sk->sk_protocol].registered == 0) {
  411. struct listeners *old;
  412. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  413. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  414. kfree_rcu(old, rcu);
  415. nl_table[sk->sk_protocol].module = NULL;
  416. nl_table[sk->sk_protocol].bind = NULL;
  417. nl_table[sk->sk_protocol].flags = 0;
  418. nl_table[sk->sk_protocol].registered = 0;
  419. }
  420. } else if (nlk->subscriptions) {
  421. netlink_update_listeners(sk);
  422. }
  423. netlink_table_ungrab();
  424. kfree(nlk->groups);
  425. nlk->groups = NULL;
  426. local_bh_disable();
  427. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  428. local_bh_enable();
  429. sock_put(sk);
  430. return 0;
  431. }
  432. static int netlink_autobind(struct socket *sock)
  433. {
  434. struct sock *sk = sock->sk;
  435. struct net *net = sock_net(sk);
  436. struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
  437. struct hlist_head *head;
  438. struct sock *osk;
  439. s32 portid = task_tgid_vnr(current);
  440. int err;
  441. static s32 rover = -4097;
  442. retry:
  443. cond_resched();
  444. netlink_table_grab();
  445. head = nl_portid_hashfn(hash, portid);
  446. sk_for_each(osk, head) {
  447. if (!net_eq(sock_net(osk), net))
  448. continue;
  449. if (nlk_sk(osk)->portid == portid) {
  450. /* Bind collision, search negative portid values. */
  451. portid = rover--;
  452. if (rover > -4097)
  453. rover = -4097;
  454. netlink_table_ungrab();
  455. goto retry;
  456. }
  457. }
  458. netlink_table_ungrab();
  459. err = netlink_insert(sk, net, portid);
  460. if (err == -EADDRINUSE)
  461. goto retry;
  462. /* If 2 threads race to autobind, that is fine. */
  463. if (err == -EBUSY)
  464. err = 0;
  465. return err;
  466. }
  467. static inline int netlink_capable(const struct socket *sock, unsigned int flag)
  468. {
  469. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  470. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  471. }
  472. static void
  473. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  474. {
  475. struct netlink_sock *nlk = nlk_sk(sk);
  476. if (nlk->subscriptions && !subscriptions)
  477. __sk_del_bind_node(sk);
  478. else if (!nlk->subscriptions && subscriptions)
  479. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  480. nlk->subscriptions = subscriptions;
  481. }
  482. static int netlink_realloc_groups(struct sock *sk)
  483. {
  484. struct netlink_sock *nlk = nlk_sk(sk);
  485. unsigned int groups;
  486. unsigned long *new_groups;
  487. int err = 0;
  488. netlink_table_grab();
  489. groups = nl_table[sk->sk_protocol].groups;
  490. if (!nl_table[sk->sk_protocol].registered) {
  491. err = -ENOENT;
  492. goto out_unlock;
  493. }
  494. if (nlk->ngroups >= groups)
  495. goto out_unlock;
  496. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  497. if (new_groups == NULL) {
  498. err = -ENOMEM;
  499. goto out_unlock;
  500. }
  501. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  502. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  503. nlk->groups = new_groups;
  504. nlk->ngroups = groups;
  505. out_unlock:
  506. netlink_table_ungrab();
  507. return err;
  508. }
  509. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  510. int addr_len)
  511. {
  512. struct sock *sk = sock->sk;
  513. struct net *net = sock_net(sk);
  514. struct netlink_sock *nlk = nlk_sk(sk);
  515. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  516. int err;
  517. if (addr_len < sizeof(struct sockaddr_nl))
  518. return -EINVAL;
  519. if (nladdr->nl_family != AF_NETLINK)
  520. return -EINVAL;
  521. /* Only superuser is allowed to listen multicasts */
  522. if (nladdr->nl_groups) {
  523. if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
  524. return -EPERM;
  525. err = netlink_realloc_groups(sk);
  526. if (err)
  527. return err;
  528. }
  529. if (nlk->portid) {
  530. if (nladdr->nl_pid != nlk->portid)
  531. return -EINVAL;
  532. } else {
  533. err = nladdr->nl_pid ?
  534. netlink_insert(sk, net, nladdr->nl_pid) :
  535. netlink_autobind(sock);
  536. if (err)
  537. return err;
  538. }
  539. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  540. return 0;
  541. netlink_table_grab();
  542. netlink_update_subscriptions(sk, nlk->subscriptions +
  543. hweight32(nladdr->nl_groups) -
  544. hweight32(nlk->groups[0]));
  545. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  546. netlink_update_listeners(sk);
  547. netlink_table_ungrab();
  548. if (nlk->netlink_bind && nlk->groups[0]) {
  549. int i;
  550. for (i=0; i<nlk->ngroups; i++) {
  551. if (test_bit(i, nlk->groups))
  552. nlk->netlink_bind(i);
  553. }
  554. }
  555. return 0;
  556. }
  557. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  558. int alen, int flags)
  559. {
  560. int err = 0;
  561. struct sock *sk = sock->sk;
  562. struct netlink_sock *nlk = nlk_sk(sk);
  563. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  564. if (alen < sizeof(addr->sa_family))
  565. return -EINVAL;
  566. if (addr->sa_family == AF_UNSPEC) {
  567. sk->sk_state = NETLINK_UNCONNECTED;
  568. nlk->dst_portid = 0;
  569. nlk->dst_group = 0;
  570. return 0;
  571. }
  572. if (addr->sa_family != AF_NETLINK)
  573. return -EINVAL;
  574. /* Only superuser is allowed to send multicasts */
  575. if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
  576. return -EPERM;
  577. if (!nlk->portid)
  578. err = netlink_autobind(sock);
  579. if (err == 0) {
  580. sk->sk_state = NETLINK_CONNECTED;
  581. nlk->dst_portid = nladdr->nl_pid;
  582. nlk->dst_group = ffs(nladdr->nl_groups);
  583. }
  584. return err;
  585. }
  586. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  587. int *addr_len, int peer)
  588. {
  589. struct sock *sk = sock->sk;
  590. struct netlink_sock *nlk = nlk_sk(sk);
  591. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  592. nladdr->nl_family = AF_NETLINK;
  593. nladdr->nl_pad = 0;
  594. *addr_len = sizeof(*nladdr);
  595. if (peer) {
  596. nladdr->nl_pid = nlk->dst_portid;
  597. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  598. } else {
  599. nladdr->nl_pid = nlk->portid;
  600. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  601. }
  602. return 0;
  603. }
  604. static void netlink_overrun(struct sock *sk)
  605. {
  606. struct netlink_sock *nlk = nlk_sk(sk);
  607. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  608. if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
  609. sk->sk_err = ENOBUFS;
  610. sk->sk_error_report(sk);
  611. }
  612. }
  613. atomic_inc(&sk->sk_drops);
  614. }
  615. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  616. {
  617. struct sock *sock;
  618. struct netlink_sock *nlk;
  619. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  620. if (!sock)
  621. return ERR_PTR(-ECONNREFUSED);
  622. /* Don't bother queuing skb if kernel socket has no input function */
  623. nlk = nlk_sk(sock);
  624. if (sock->sk_state == NETLINK_CONNECTED &&
  625. nlk->dst_portid != nlk_sk(ssk)->portid) {
  626. sock_put(sock);
  627. return ERR_PTR(-ECONNREFUSED);
  628. }
  629. return sock;
  630. }
  631. struct sock *netlink_getsockbyfilp(struct file *filp)
  632. {
  633. struct inode *inode = file_inode(filp);
  634. struct sock *sock;
  635. if (!S_ISSOCK(inode->i_mode))
  636. return ERR_PTR(-ENOTSOCK);
  637. sock = SOCKET_I(inode)->sk;
  638. if (sock->sk_family != AF_NETLINK)
  639. return ERR_PTR(-EINVAL);
  640. sock_hold(sock);
  641. return sock;
  642. }
  643. /*
  644. * Attach a skb to a netlink socket.
  645. * The caller must hold a reference to the destination socket. On error, the
  646. * reference is dropped. The skb is not send to the destination, just all
  647. * all error checks are performed and memory in the queue is reserved.
  648. * Return values:
  649. * < 0: error. skb freed, reference to sock dropped.
  650. * 0: continue
  651. * 1: repeat lookup - reference dropped while waiting for socket memory.
  652. */
  653. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  654. long *timeo, struct sock *ssk)
  655. {
  656. struct netlink_sock *nlk;
  657. nlk = nlk_sk(sk);
  658. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  659. test_bit(NETLINK_CONGESTED, &nlk->state)) {
  660. DECLARE_WAITQUEUE(wait, current);
  661. if (!*timeo) {
  662. if (!ssk || netlink_is_kernel(ssk))
  663. netlink_overrun(sk);
  664. sock_put(sk);
  665. kfree_skb(skb);
  666. return -EAGAIN;
  667. }
  668. __set_current_state(TASK_INTERRUPTIBLE);
  669. add_wait_queue(&nlk->wait, &wait);
  670. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  671. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  672. !sock_flag(sk, SOCK_DEAD))
  673. *timeo = schedule_timeout(*timeo);
  674. __set_current_state(TASK_RUNNING);
  675. remove_wait_queue(&nlk->wait, &wait);
  676. sock_put(sk);
  677. if (signal_pending(current)) {
  678. kfree_skb(skb);
  679. return sock_intr_errno(*timeo);
  680. }
  681. return 1;
  682. }
  683. skb_set_owner_r(skb, sk);
  684. return 0;
  685. }
  686. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  687. {
  688. int len = skb->len;
  689. skb_queue_tail(&sk->sk_receive_queue, skb);
  690. sk->sk_data_ready(sk, len);
  691. return len;
  692. }
  693. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  694. {
  695. int len = __netlink_sendskb(sk, skb);
  696. sock_put(sk);
  697. return len;
  698. }
  699. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  700. {
  701. kfree_skb(skb);
  702. sock_put(sk);
  703. }
  704. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  705. {
  706. int delta;
  707. skb_orphan(skb);
  708. delta = skb->end - skb->tail;
  709. if (delta * 2 < skb->truesize)
  710. return skb;
  711. if (skb_shared(skb)) {
  712. struct sk_buff *nskb = skb_clone(skb, allocation);
  713. if (!nskb)
  714. return skb;
  715. consume_skb(skb);
  716. skb = nskb;
  717. }
  718. if (!pskb_expand_head(skb, 0, -delta, allocation))
  719. skb->truesize -= delta;
  720. return skb;
  721. }
  722. static void netlink_rcv_wake(struct sock *sk)
  723. {
  724. struct netlink_sock *nlk = nlk_sk(sk);
  725. if (skb_queue_empty(&sk->sk_receive_queue))
  726. clear_bit(NETLINK_CONGESTED, &nlk->state);
  727. if (!test_bit(NETLINK_CONGESTED, &nlk->state))
  728. wake_up_interruptible(&nlk->wait);
  729. }
  730. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  731. struct sock *ssk)
  732. {
  733. int ret;
  734. struct netlink_sock *nlk = nlk_sk(sk);
  735. ret = -ECONNREFUSED;
  736. if (nlk->netlink_rcv != NULL) {
  737. ret = skb->len;
  738. skb_set_owner_r(skb, sk);
  739. NETLINK_CB(skb).ssk = ssk;
  740. nlk->netlink_rcv(skb);
  741. consume_skb(skb);
  742. } else {
  743. kfree_skb(skb);
  744. }
  745. sock_put(sk);
  746. return ret;
  747. }
  748. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  749. u32 portid, int nonblock)
  750. {
  751. struct sock *sk;
  752. int err;
  753. long timeo;
  754. skb = netlink_trim(skb, gfp_any());
  755. timeo = sock_sndtimeo(ssk, nonblock);
  756. retry:
  757. sk = netlink_getsockbyportid(ssk, portid);
  758. if (IS_ERR(sk)) {
  759. kfree_skb(skb);
  760. return PTR_ERR(sk);
  761. }
  762. if (netlink_is_kernel(sk))
  763. return netlink_unicast_kernel(sk, skb, ssk);
  764. if (sk_filter(sk, skb)) {
  765. err = skb->len;
  766. kfree_skb(skb);
  767. sock_put(sk);
  768. return err;
  769. }
  770. err = netlink_attachskb(sk, skb, &timeo, ssk);
  771. if (err == 1)
  772. goto retry;
  773. if (err)
  774. return err;
  775. return netlink_sendskb(sk, skb);
  776. }
  777. EXPORT_SYMBOL(netlink_unicast);
  778. int netlink_has_listeners(struct sock *sk, unsigned int group)
  779. {
  780. int res = 0;
  781. struct listeners *listeners;
  782. BUG_ON(!netlink_is_kernel(sk));
  783. rcu_read_lock();
  784. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  785. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  786. res = test_bit(group - 1, listeners->masks);
  787. rcu_read_unlock();
  788. return res;
  789. }
  790. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  791. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  792. {
  793. struct netlink_sock *nlk = nlk_sk(sk);
  794. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  795. !test_bit(NETLINK_CONGESTED, &nlk->state)) {
  796. skb_set_owner_r(skb, sk);
  797. __netlink_sendskb(sk, skb);
  798. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  799. }
  800. return -1;
  801. }
  802. struct netlink_broadcast_data {
  803. struct sock *exclude_sk;
  804. struct net *net;
  805. u32 portid;
  806. u32 group;
  807. int failure;
  808. int delivery_failure;
  809. int congested;
  810. int delivered;
  811. gfp_t allocation;
  812. struct sk_buff *skb, *skb2;
  813. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  814. void *tx_data;
  815. };
  816. static int do_one_broadcast(struct sock *sk,
  817. struct netlink_broadcast_data *p)
  818. {
  819. struct netlink_sock *nlk = nlk_sk(sk);
  820. int val;
  821. if (p->exclude_sk == sk)
  822. goto out;
  823. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  824. !test_bit(p->group - 1, nlk->groups))
  825. goto out;
  826. if (!net_eq(sock_net(sk), p->net))
  827. goto out;
  828. if (p->failure) {
  829. netlink_overrun(sk);
  830. goto out;
  831. }
  832. sock_hold(sk);
  833. if (p->skb2 == NULL) {
  834. if (skb_shared(p->skb)) {
  835. p->skb2 = skb_clone(p->skb, p->allocation);
  836. } else {
  837. p->skb2 = skb_get(p->skb);
  838. /*
  839. * skb ownership may have been set when
  840. * delivered to a previous socket.
  841. */
  842. skb_orphan(p->skb2);
  843. }
  844. }
  845. if (p->skb2 == NULL) {
  846. netlink_overrun(sk);
  847. /* Clone failed. Notify ALL listeners. */
  848. p->failure = 1;
  849. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  850. p->delivery_failure = 1;
  851. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  852. kfree_skb(p->skb2);
  853. p->skb2 = NULL;
  854. } else if (sk_filter(sk, p->skb2)) {
  855. kfree_skb(p->skb2);
  856. p->skb2 = NULL;
  857. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  858. netlink_overrun(sk);
  859. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  860. p->delivery_failure = 1;
  861. } else {
  862. p->congested |= val;
  863. p->delivered = 1;
  864. p->skb2 = NULL;
  865. }
  866. sock_put(sk);
  867. out:
  868. return 0;
  869. }
  870. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  871. u32 group, gfp_t allocation,
  872. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  873. void *filter_data)
  874. {
  875. struct net *net = sock_net(ssk);
  876. struct netlink_broadcast_data info;
  877. struct sock *sk;
  878. skb = netlink_trim(skb, allocation);
  879. info.exclude_sk = ssk;
  880. info.net = net;
  881. info.portid = portid;
  882. info.group = group;
  883. info.failure = 0;
  884. info.delivery_failure = 0;
  885. info.congested = 0;
  886. info.delivered = 0;
  887. info.allocation = allocation;
  888. info.skb = skb;
  889. info.skb2 = NULL;
  890. info.tx_filter = filter;
  891. info.tx_data = filter_data;
  892. /* While we sleep in clone, do not allow to change socket list */
  893. netlink_lock_table();
  894. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  895. do_one_broadcast(sk, &info);
  896. consume_skb(skb);
  897. netlink_unlock_table();
  898. if (info.delivery_failure) {
  899. kfree_skb(info.skb2);
  900. return -ENOBUFS;
  901. }
  902. consume_skb(info.skb2);
  903. if (info.delivered) {
  904. if (info.congested && (allocation & __GFP_WAIT))
  905. yield();
  906. return 0;
  907. }
  908. return -ESRCH;
  909. }
  910. EXPORT_SYMBOL(netlink_broadcast_filtered);
  911. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  912. u32 group, gfp_t allocation)
  913. {
  914. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  915. NULL, NULL);
  916. }
  917. EXPORT_SYMBOL(netlink_broadcast);
  918. struct netlink_set_err_data {
  919. struct sock *exclude_sk;
  920. u32 portid;
  921. u32 group;
  922. int code;
  923. };
  924. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  925. {
  926. struct netlink_sock *nlk = nlk_sk(sk);
  927. int ret = 0;
  928. if (sk == p->exclude_sk)
  929. goto out;
  930. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  931. goto out;
  932. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  933. !test_bit(p->group - 1, nlk->groups))
  934. goto out;
  935. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  936. ret = 1;
  937. goto out;
  938. }
  939. sk->sk_err = p->code;
  940. sk->sk_error_report(sk);
  941. out:
  942. return ret;
  943. }
  944. /**
  945. * netlink_set_err - report error to broadcast listeners
  946. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  947. * @portid: the PORTID of a process that we want to skip (if any)
  948. * @groups: the broadcast group that will notice the error
  949. * @code: error code, must be negative (as usual in kernelspace)
  950. *
  951. * This function returns the number of broadcast listeners that have set the
  952. * NETLINK_RECV_NO_ENOBUFS socket option.
  953. */
  954. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  955. {
  956. struct netlink_set_err_data info;
  957. struct sock *sk;
  958. int ret = 0;
  959. info.exclude_sk = ssk;
  960. info.portid = portid;
  961. info.group = group;
  962. /* sk->sk_err wants a positive error value */
  963. info.code = -code;
  964. read_lock(&nl_table_lock);
  965. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  966. ret += do_one_set_err(sk, &info);
  967. read_unlock(&nl_table_lock);
  968. return ret;
  969. }
  970. EXPORT_SYMBOL(netlink_set_err);
  971. /* must be called with netlink table grabbed */
  972. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  973. unsigned int group,
  974. int is_new)
  975. {
  976. int old, new = !!is_new, subscriptions;
  977. old = test_bit(group - 1, nlk->groups);
  978. subscriptions = nlk->subscriptions - old + new;
  979. if (new)
  980. __set_bit(group - 1, nlk->groups);
  981. else
  982. __clear_bit(group - 1, nlk->groups);
  983. netlink_update_subscriptions(&nlk->sk, subscriptions);
  984. netlink_update_listeners(&nlk->sk);
  985. }
  986. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  987. char __user *optval, unsigned int optlen)
  988. {
  989. struct sock *sk = sock->sk;
  990. struct netlink_sock *nlk = nlk_sk(sk);
  991. unsigned int val = 0;
  992. int err;
  993. if (level != SOL_NETLINK)
  994. return -ENOPROTOOPT;
  995. if (optlen >= sizeof(int) &&
  996. get_user(val, (unsigned int __user *)optval))
  997. return -EFAULT;
  998. switch (optname) {
  999. case NETLINK_PKTINFO:
  1000. if (val)
  1001. nlk->flags |= NETLINK_RECV_PKTINFO;
  1002. else
  1003. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1004. err = 0;
  1005. break;
  1006. case NETLINK_ADD_MEMBERSHIP:
  1007. case NETLINK_DROP_MEMBERSHIP: {
  1008. if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
  1009. return -EPERM;
  1010. err = netlink_realloc_groups(sk);
  1011. if (err)
  1012. return err;
  1013. if (!val || val - 1 >= nlk->ngroups)
  1014. return -EINVAL;
  1015. netlink_table_grab();
  1016. netlink_update_socket_mc(nlk, val,
  1017. optname == NETLINK_ADD_MEMBERSHIP);
  1018. netlink_table_ungrab();
  1019. if (nlk->netlink_bind)
  1020. nlk->netlink_bind(val);
  1021. err = 0;
  1022. break;
  1023. }
  1024. case NETLINK_BROADCAST_ERROR:
  1025. if (val)
  1026. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1027. else
  1028. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1029. err = 0;
  1030. break;
  1031. case NETLINK_NO_ENOBUFS:
  1032. if (val) {
  1033. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1034. clear_bit(NETLINK_CONGESTED, &nlk->state);
  1035. wake_up_interruptible(&nlk->wait);
  1036. } else {
  1037. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1038. }
  1039. err = 0;
  1040. break;
  1041. default:
  1042. err = -ENOPROTOOPT;
  1043. }
  1044. return err;
  1045. }
  1046. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1047. char __user *optval, int __user *optlen)
  1048. {
  1049. struct sock *sk = sock->sk;
  1050. struct netlink_sock *nlk = nlk_sk(sk);
  1051. int len, val, err;
  1052. if (level != SOL_NETLINK)
  1053. return -ENOPROTOOPT;
  1054. if (get_user(len, optlen))
  1055. return -EFAULT;
  1056. if (len < 0)
  1057. return -EINVAL;
  1058. switch (optname) {
  1059. case NETLINK_PKTINFO:
  1060. if (len < sizeof(int))
  1061. return -EINVAL;
  1062. len = sizeof(int);
  1063. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1064. if (put_user(len, optlen) ||
  1065. put_user(val, optval))
  1066. return -EFAULT;
  1067. err = 0;
  1068. break;
  1069. case NETLINK_BROADCAST_ERROR:
  1070. if (len < sizeof(int))
  1071. return -EINVAL;
  1072. len = sizeof(int);
  1073. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1074. if (put_user(len, optlen) ||
  1075. put_user(val, optval))
  1076. return -EFAULT;
  1077. err = 0;
  1078. break;
  1079. case NETLINK_NO_ENOBUFS:
  1080. if (len < sizeof(int))
  1081. return -EINVAL;
  1082. len = sizeof(int);
  1083. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1084. if (put_user(len, optlen) ||
  1085. put_user(val, optval))
  1086. return -EFAULT;
  1087. err = 0;
  1088. break;
  1089. default:
  1090. err = -ENOPROTOOPT;
  1091. }
  1092. return err;
  1093. }
  1094. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1095. {
  1096. struct nl_pktinfo info;
  1097. info.group = NETLINK_CB(skb).dst_group;
  1098. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1099. }
  1100. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1101. struct msghdr *msg, size_t len)
  1102. {
  1103. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1104. struct sock *sk = sock->sk;
  1105. struct netlink_sock *nlk = nlk_sk(sk);
  1106. struct sockaddr_nl *addr = msg->msg_name;
  1107. u32 dst_portid;
  1108. u32 dst_group;
  1109. struct sk_buff *skb;
  1110. int err;
  1111. struct scm_cookie scm;
  1112. if (msg->msg_flags&MSG_OOB)
  1113. return -EOPNOTSUPP;
  1114. if (NULL == siocb->scm)
  1115. siocb->scm = &scm;
  1116. err = scm_send(sock, msg, siocb->scm, true);
  1117. if (err < 0)
  1118. return err;
  1119. if (msg->msg_namelen) {
  1120. err = -EINVAL;
  1121. if (addr->nl_family != AF_NETLINK)
  1122. goto out;
  1123. dst_portid = addr->nl_pid;
  1124. dst_group = ffs(addr->nl_groups);
  1125. err = -EPERM;
  1126. if ((dst_group || dst_portid) &&
  1127. !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
  1128. goto out;
  1129. } else {
  1130. dst_portid = nlk->dst_portid;
  1131. dst_group = nlk->dst_group;
  1132. }
  1133. if (!nlk->portid) {
  1134. err = netlink_autobind(sock);
  1135. if (err)
  1136. goto out;
  1137. }
  1138. err = -EMSGSIZE;
  1139. if (len > sk->sk_sndbuf - 32)
  1140. goto out;
  1141. err = -ENOBUFS;
  1142. skb = alloc_skb(len, GFP_KERNEL);
  1143. if (skb == NULL)
  1144. goto out;
  1145. NETLINK_CB(skb).portid = nlk->portid;
  1146. NETLINK_CB(skb).dst_group = dst_group;
  1147. NETLINK_CB(skb).creds = siocb->scm->creds;
  1148. err = -EFAULT;
  1149. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1150. kfree_skb(skb);
  1151. goto out;
  1152. }
  1153. err = security_netlink_send(sk, skb);
  1154. if (err) {
  1155. kfree_skb(skb);
  1156. goto out;
  1157. }
  1158. if (dst_group) {
  1159. atomic_inc(&skb->users);
  1160. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1161. }
  1162. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1163. out:
  1164. scm_destroy(siocb->scm);
  1165. return err;
  1166. }
  1167. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1168. struct msghdr *msg, size_t len,
  1169. int flags)
  1170. {
  1171. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1172. struct scm_cookie scm;
  1173. struct sock *sk = sock->sk;
  1174. struct netlink_sock *nlk = nlk_sk(sk);
  1175. int noblock = flags&MSG_DONTWAIT;
  1176. size_t copied;
  1177. struct sk_buff *skb, *data_skb;
  1178. int err, ret;
  1179. if (flags&MSG_OOB)
  1180. return -EOPNOTSUPP;
  1181. copied = 0;
  1182. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1183. if (skb == NULL)
  1184. goto out;
  1185. data_skb = skb;
  1186. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1187. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1188. /*
  1189. * If this skb has a frag_list, then here that means that we
  1190. * will have to use the frag_list skb's data for compat tasks
  1191. * and the regular skb's data for normal (non-compat) tasks.
  1192. *
  1193. * If we need to send the compat skb, assign it to the
  1194. * 'data_skb' variable so that it will be used below for data
  1195. * copying. We keep 'skb' for everything else, including
  1196. * freeing both later.
  1197. */
  1198. if (flags & MSG_CMSG_COMPAT)
  1199. data_skb = skb_shinfo(skb)->frag_list;
  1200. }
  1201. #endif
  1202. msg->msg_namelen = 0;
  1203. copied = data_skb->len;
  1204. if (len < copied) {
  1205. msg->msg_flags |= MSG_TRUNC;
  1206. copied = len;
  1207. }
  1208. skb_reset_transport_header(data_skb);
  1209. err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
  1210. if (msg->msg_name) {
  1211. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1212. addr->nl_family = AF_NETLINK;
  1213. addr->nl_pad = 0;
  1214. addr->nl_pid = NETLINK_CB(skb).portid;
  1215. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1216. msg->msg_namelen = sizeof(*addr);
  1217. }
  1218. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1219. netlink_cmsg_recv_pktinfo(msg, skb);
  1220. if (NULL == siocb->scm) {
  1221. memset(&scm, 0, sizeof(scm));
  1222. siocb->scm = &scm;
  1223. }
  1224. siocb->scm->creds = *NETLINK_CREDS(skb);
  1225. if (flags & MSG_TRUNC)
  1226. copied = data_skb->len;
  1227. skb_free_datagram(sk, skb);
  1228. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1229. ret = netlink_dump(sk);
  1230. if (ret) {
  1231. sk->sk_err = ret;
  1232. sk->sk_error_report(sk);
  1233. }
  1234. }
  1235. scm_recv(sock, msg, siocb->scm, flags);
  1236. out:
  1237. netlink_rcv_wake(sk);
  1238. return err ? : copied;
  1239. }
  1240. static void netlink_data_ready(struct sock *sk, int len)
  1241. {
  1242. BUG();
  1243. }
  1244. /*
  1245. * We export these functions to other modules. They provide a
  1246. * complete set of kernel non-blocking support for message
  1247. * queueing.
  1248. */
  1249. struct sock *
  1250. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  1251. struct netlink_kernel_cfg *cfg)
  1252. {
  1253. struct socket *sock;
  1254. struct sock *sk;
  1255. struct netlink_sock *nlk;
  1256. struct listeners *listeners = NULL;
  1257. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  1258. unsigned int groups;
  1259. BUG_ON(!nl_table);
  1260. if (unit < 0 || unit >= MAX_LINKS)
  1261. return NULL;
  1262. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1263. return NULL;
  1264. /*
  1265. * We have to just have a reference on the net from sk, but don't
  1266. * get_net it. Besides, we cannot get and then put the net here.
  1267. * So we create one inside init_net and the move it to net.
  1268. */
  1269. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1270. goto out_sock_release_nosk;
  1271. sk = sock->sk;
  1272. sk_change_net(sk, net);
  1273. if (!cfg || cfg->groups < 32)
  1274. groups = 32;
  1275. else
  1276. groups = cfg->groups;
  1277. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1278. if (!listeners)
  1279. goto out_sock_release;
  1280. sk->sk_data_ready = netlink_data_ready;
  1281. if (cfg && cfg->input)
  1282. nlk_sk(sk)->netlink_rcv = cfg->input;
  1283. if (netlink_insert(sk, net, 0))
  1284. goto out_sock_release;
  1285. nlk = nlk_sk(sk);
  1286. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1287. netlink_table_grab();
  1288. if (!nl_table[unit].registered) {
  1289. nl_table[unit].groups = groups;
  1290. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1291. nl_table[unit].cb_mutex = cb_mutex;
  1292. nl_table[unit].module = module;
  1293. if (cfg) {
  1294. nl_table[unit].bind = cfg->bind;
  1295. nl_table[unit].flags = cfg->flags;
  1296. }
  1297. nl_table[unit].registered = 1;
  1298. } else {
  1299. kfree(listeners);
  1300. nl_table[unit].registered++;
  1301. }
  1302. netlink_table_ungrab();
  1303. return sk;
  1304. out_sock_release:
  1305. kfree(listeners);
  1306. netlink_kernel_release(sk);
  1307. return NULL;
  1308. out_sock_release_nosk:
  1309. sock_release(sock);
  1310. return NULL;
  1311. }
  1312. EXPORT_SYMBOL(__netlink_kernel_create);
  1313. void
  1314. netlink_kernel_release(struct sock *sk)
  1315. {
  1316. sk_release_kernel(sk);
  1317. }
  1318. EXPORT_SYMBOL(netlink_kernel_release);
  1319. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1320. {
  1321. struct listeners *new, *old;
  1322. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1323. if (groups < 32)
  1324. groups = 32;
  1325. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1326. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1327. if (!new)
  1328. return -ENOMEM;
  1329. old = nl_deref_protected(tbl->listeners);
  1330. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1331. rcu_assign_pointer(tbl->listeners, new);
  1332. kfree_rcu(old, rcu);
  1333. }
  1334. tbl->groups = groups;
  1335. return 0;
  1336. }
  1337. /**
  1338. * netlink_change_ngroups - change number of multicast groups
  1339. *
  1340. * This changes the number of multicast groups that are available
  1341. * on a certain netlink family. Note that it is not possible to
  1342. * change the number of groups to below 32. Also note that it does
  1343. * not implicitly call netlink_clear_multicast_users() when the
  1344. * number of groups is reduced.
  1345. *
  1346. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1347. * @groups: The new number of groups.
  1348. */
  1349. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1350. {
  1351. int err;
  1352. netlink_table_grab();
  1353. err = __netlink_change_ngroups(sk, groups);
  1354. netlink_table_ungrab();
  1355. return err;
  1356. }
  1357. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1358. {
  1359. struct sock *sk;
  1360. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1361. sk_for_each_bound(sk, &tbl->mc_list)
  1362. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1363. }
  1364. /**
  1365. * netlink_clear_multicast_users - kick off multicast listeners
  1366. *
  1367. * This function removes all listeners from the given group.
  1368. * @ksk: The kernel netlink socket, as returned by
  1369. * netlink_kernel_create().
  1370. * @group: The multicast group to clear.
  1371. */
  1372. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1373. {
  1374. netlink_table_grab();
  1375. __netlink_clear_multicast_users(ksk, group);
  1376. netlink_table_ungrab();
  1377. }
  1378. struct nlmsghdr *
  1379. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  1380. {
  1381. struct nlmsghdr *nlh;
  1382. int size = nlmsg_msg_size(len);
  1383. nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
  1384. nlh->nlmsg_type = type;
  1385. nlh->nlmsg_len = size;
  1386. nlh->nlmsg_flags = flags;
  1387. nlh->nlmsg_pid = portid;
  1388. nlh->nlmsg_seq = seq;
  1389. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  1390. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  1391. return nlh;
  1392. }
  1393. EXPORT_SYMBOL(__nlmsg_put);
  1394. /*
  1395. * It looks a bit ugly.
  1396. * It would be better to create kernel thread.
  1397. */
  1398. static int netlink_dump(struct sock *sk)
  1399. {
  1400. struct netlink_sock *nlk = nlk_sk(sk);
  1401. struct netlink_callback *cb;
  1402. struct sk_buff *skb = NULL;
  1403. struct nlmsghdr *nlh;
  1404. int len, err = -ENOBUFS;
  1405. int alloc_size;
  1406. mutex_lock(nlk->cb_mutex);
  1407. cb = nlk->cb;
  1408. if (cb == NULL) {
  1409. err = -EINVAL;
  1410. goto errout_skb;
  1411. }
  1412. alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  1413. skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
  1414. if (!skb)
  1415. goto errout_skb;
  1416. len = cb->dump(skb, cb);
  1417. if (len > 0) {
  1418. mutex_unlock(nlk->cb_mutex);
  1419. if (sk_filter(sk, skb))
  1420. kfree_skb(skb);
  1421. else
  1422. __netlink_sendskb(sk, skb);
  1423. return 0;
  1424. }
  1425. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1426. if (!nlh)
  1427. goto errout_skb;
  1428. nl_dump_check_consistent(cb, nlh);
  1429. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1430. if (sk_filter(sk, skb))
  1431. kfree_skb(skb);
  1432. else
  1433. __netlink_sendskb(sk, skb);
  1434. if (cb->done)
  1435. cb->done(cb);
  1436. nlk->cb = NULL;
  1437. mutex_unlock(nlk->cb_mutex);
  1438. module_put(cb->module);
  1439. netlink_consume_callback(cb);
  1440. return 0;
  1441. errout_skb:
  1442. mutex_unlock(nlk->cb_mutex);
  1443. kfree_skb(skb);
  1444. return err;
  1445. }
  1446. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1447. const struct nlmsghdr *nlh,
  1448. struct netlink_dump_control *control)
  1449. {
  1450. struct netlink_callback *cb;
  1451. struct sock *sk;
  1452. struct netlink_sock *nlk;
  1453. int ret;
  1454. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1455. if (cb == NULL)
  1456. return -ENOBUFS;
  1457. cb->dump = control->dump;
  1458. cb->done = control->done;
  1459. cb->nlh = nlh;
  1460. cb->data = control->data;
  1461. cb->module = control->module;
  1462. cb->min_dump_alloc = control->min_dump_alloc;
  1463. atomic_inc(&skb->users);
  1464. cb->skb = skb;
  1465. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  1466. if (sk == NULL) {
  1467. netlink_destroy_callback(cb);
  1468. return -ECONNREFUSED;
  1469. }
  1470. nlk = nlk_sk(sk);
  1471. mutex_lock(nlk->cb_mutex);
  1472. /* A dump is in progress... */
  1473. if (nlk->cb) {
  1474. mutex_unlock(nlk->cb_mutex);
  1475. netlink_destroy_callback(cb);
  1476. ret = -EBUSY;
  1477. goto out;
  1478. }
  1479. /* add reference of module which cb->dump belongs to */
  1480. if (!try_module_get(cb->module)) {
  1481. mutex_unlock(nlk->cb_mutex);
  1482. netlink_destroy_callback(cb);
  1483. ret = -EPROTONOSUPPORT;
  1484. goto out;
  1485. }
  1486. nlk->cb = cb;
  1487. mutex_unlock(nlk->cb_mutex);
  1488. ret = netlink_dump(sk);
  1489. out:
  1490. sock_put(sk);
  1491. if (ret)
  1492. return ret;
  1493. /* We successfully started a dump, by returning -EINTR we
  1494. * signal not to send ACK even if it was requested.
  1495. */
  1496. return -EINTR;
  1497. }
  1498. EXPORT_SYMBOL(__netlink_dump_start);
  1499. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1500. {
  1501. struct sk_buff *skb;
  1502. struct nlmsghdr *rep;
  1503. struct nlmsgerr *errmsg;
  1504. size_t payload = sizeof(*errmsg);
  1505. /* error messages get the original request appened */
  1506. if (err)
  1507. payload += nlmsg_len(nlh);
  1508. skb = nlmsg_new(payload, GFP_KERNEL);
  1509. if (!skb) {
  1510. struct sock *sk;
  1511. sk = netlink_lookup(sock_net(in_skb->sk),
  1512. in_skb->sk->sk_protocol,
  1513. NETLINK_CB(in_skb).portid);
  1514. if (sk) {
  1515. sk->sk_err = ENOBUFS;
  1516. sk->sk_error_report(sk);
  1517. sock_put(sk);
  1518. }
  1519. return;
  1520. }
  1521. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  1522. NLMSG_ERROR, payload, 0);
  1523. errmsg = nlmsg_data(rep);
  1524. errmsg->error = err;
  1525. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1526. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  1527. }
  1528. EXPORT_SYMBOL(netlink_ack);
  1529. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1530. struct nlmsghdr *))
  1531. {
  1532. struct nlmsghdr *nlh;
  1533. int err;
  1534. while (skb->len >= nlmsg_total_size(0)) {
  1535. int msglen;
  1536. nlh = nlmsg_hdr(skb);
  1537. err = 0;
  1538. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1539. return 0;
  1540. /* Only requests are handled by the kernel */
  1541. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1542. goto ack;
  1543. /* Skip control messages */
  1544. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1545. goto ack;
  1546. err = cb(skb, nlh);
  1547. if (err == -EINTR)
  1548. goto skip;
  1549. ack:
  1550. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1551. netlink_ack(skb, nlh, err);
  1552. skip:
  1553. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1554. if (msglen > skb->len)
  1555. msglen = skb->len;
  1556. skb_pull(skb, msglen);
  1557. }
  1558. return 0;
  1559. }
  1560. EXPORT_SYMBOL(netlink_rcv_skb);
  1561. /**
  1562. * nlmsg_notify - send a notification netlink message
  1563. * @sk: netlink socket to use
  1564. * @skb: notification message
  1565. * @portid: destination netlink portid for reports or 0
  1566. * @group: destination multicast group or 0
  1567. * @report: 1 to report back, 0 to disable
  1568. * @flags: allocation flags
  1569. */
  1570. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  1571. unsigned int group, int report, gfp_t flags)
  1572. {
  1573. int err = 0;
  1574. if (group) {
  1575. int exclude_portid = 0;
  1576. if (report) {
  1577. atomic_inc(&skb->users);
  1578. exclude_portid = portid;
  1579. }
  1580. /* errors reported via destination sk->sk_err, but propagate
  1581. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1582. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  1583. }
  1584. if (report) {
  1585. int err2;
  1586. err2 = nlmsg_unicast(sk, skb, portid);
  1587. if (!err || err == -ESRCH)
  1588. err = err2;
  1589. }
  1590. return err;
  1591. }
  1592. EXPORT_SYMBOL(nlmsg_notify);
  1593. #ifdef CONFIG_PROC_FS
  1594. struct nl_seq_iter {
  1595. struct seq_net_private p;
  1596. int link;
  1597. int hash_idx;
  1598. };
  1599. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1600. {
  1601. struct nl_seq_iter *iter = seq->private;
  1602. int i, j;
  1603. struct sock *s;
  1604. loff_t off = 0;
  1605. for (i = 0; i < MAX_LINKS; i++) {
  1606. struct nl_portid_hash *hash = &nl_table[i].hash;
  1607. for (j = 0; j <= hash->mask; j++) {
  1608. sk_for_each(s, &hash->table[j]) {
  1609. if (sock_net(s) != seq_file_net(seq))
  1610. continue;
  1611. if (off == pos) {
  1612. iter->link = i;
  1613. iter->hash_idx = j;
  1614. return s;
  1615. }
  1616. ++off;
  1617. }
  1618. }
  1619. }
  1620. return NULL;
  1621. }
  1622. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1623. __acquires(nl_table_lock)
  1624. {
  1625. read_lock(&nl_table_lock);
  1626. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1627. }
  1628. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1629. {
  1630. struct sock *s;
  1631. struct nl_seq_iter *iter;
  1632. int i, j;
  1633. ++*pos;
  1634. if (v == SEQ_START_TOKEN)
  1635. return netlink_seq_socket_idx(seq, 0);
  1636. iter = seq->private;
  1637. s = v;
  1638. do {
  1639. s = sk_next(s);
  1640. } while (s && sock_net(s) != seq_file_net(seq));
  1641. if (s)
  1642. return s;
  1643. i = iter->link;
  1644. j = iter->hash_idx + 1;
  1645. do {
  1646. struct nl_portid_hash *hash = &nl_table[i].hash;
  1647. for (; j <= hash->mask; j++) {
  1648. s = sk_head(&hash->table[j]);
  1649. while (s && sock_net(s) != seq_file_net(seq))
  1650. s = sk_next(s);
  1651. if (s) {
  1652. iter->link = i;
  1653. iter->hash_idx = j;
  1654. return s;
  1655. }
  1656. }
  1657. j = 0;
  1658. } while (++i < MAX_LINKS);
  1659. return NULL;
  1660. }
  1661. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1662. __releases(nl_table_lock)
  1663. {
  1664. read_unlock(&nl_table_lock);
  1665. }
  1666. static int netlink_seq_show(struct seq_file *seq, void *v)
  1667. {
  1668. if (v == SEQ_START_TOKEN) {
  1669. seq_puts(seq,
  1670. "sk Eth Pid Groups "
  1671. "Rmem Wmem Dump Locks Drops Inode\n");
  1672. } else {
  1673. struct sock *s = v;
  1674. struct netlink_sock *nlk = nlk_sk(s);
  1675. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
  1676. s,
  1677. s->sk_protocol,
  1678. nlk->portid,
  1679. nlk->groups ? (u32)nlk->groups[0] : 0,
  1680. sk_rmem_alloc_get(s),
  1681. sk_wmem_alloc_get(s),
  1682. nlk->cb,
  1683. atomic_read(&s->sk_refcnt),
  1684. atomic_read(&s->sk_drops),
  1685. sock_i_ino(s)
  1686. );
  1687. }
  1688. return 0;
  1689. }
  1690. static const struct seq_operations netlink_seq_ops = {
  1691. .start = netlink_seq_start,
  1692. .next = netlink_seq_next,
  1693. .stop = netlink_seq_stop,
  1694. .show = netlink_seq_show,
  1695. };
  1696. static int netlink_seq_open(struct inode *inode, struct file *file)
  1697. {
  1698. return seq_open_net(inode, file, &netlink_seq_ops,
  1699. sizeof(struct nl_seq_iter));
  1700. }
  1701. static const struct file_operations netlink_seq_fops = {
  1702. .owner = THIS_MODULE,
  1703. .open = netlink_seq_open,
  1704. .read = seq_read,
  1705. .llseek = seq_lseek,
  1706. .release = seq_release_net,
  1707. };
  1708. #endif
  1709. int netlink_register_notifier(struct notifier_block *nb)
  1710. {
  1711. return atomic_notifier_chain_register(&netlink_chain, nb);
  1712. }
  1713. EXPORT_SYMBOL(netlink_register_notifier);
  1714. int netlink_unregister_notifier(struct notifier_block *nb)
  1715. {
  1716. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1717. }
  1718. EXPORT_SYMBOL(netlink_unregister_notifier);
  1719. static const struct proto_ops netlink_ops = {
  1720. .family = PF_NETLINK,
  1721. .owner = THIS_MODULE,
  1722. .release = netlink_release,
  1723. .bind = netlink_bind,
  1724. .connect = netlink_connect,
  1725. .socketpair = sock_no_socketpair,
  1726. .accept = sock_no_accept,
  1727. .getname = netlink_getname,
  1728. .poll = datagram_poll,
  1729. .ioctl = sock_no_ioctl,
  1730. .listen = sock_no_listen,
  1731. .shutdown = sock_no_shutdown,
  1732. .setsockopt = netlink_setsockopt,
  1733. .getsockopt = netlink_getsockopt,
  1734. .sendmsg = netlink_sendmsg,
  1735. .recvmsg = netlink_recvmsg,
  1736. .mmap = sock_no_mmap,
  1737. .sendpage = sock_no_sendpage,
  1738. };
  1739. static const struct net_proto_family netlink_family_ops = {
  1740. .family = PF_NETLINK,
  1741. .create = netlink_create,
  1742. .owner = THIS_MODULE, /* for consistency 8) */
  1743. };
  1744. static int __net_init netlink_net_init(struct net *net)
  1745. {
  1746. #ifdef CONFIG_PROC_FS
  1747. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  1748. return -ENOMEM;
  1749. #endif
  1750. return 0;
  1751. }
  1752. static void __net_exit netlink_net_exit(struct net *net)
  1753. {
  1754. #ifdef CONFIG_PROC_FS
  1755. remove_proc_entry("netlink", net->proc_net);
  1756. #endif
  1757. }
  1758. static void __init netlink_add_usersock_entry(void)
  1759. {
  1760. struct listeners *listeners;
  1761. int groups = 32;
  1762. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1763. if (!listeners)
  1764. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  1765. netlink_table_grab();
  1766. nl_table[NETLINK_USERSOCK].groups = groups;
  1767. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  1768. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  1769. nl_table[NETLINK_USERSOCK].registered = 1;
  1770. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  1771. netlink_table_ungrab();
  1772. }
  1773. static struct pernet_operations __net_initdata netlink_net_ops = {
  1774. .init = netlink_net_init,
  1775. .exit = netlink_net_exit,
  1776. };
  1777. static int __init netlink_proto_init(void)
  1778. {
  1779. int i;
  1780. unsigned long limit;
  1781. unsigned int order;
  1782. int err = proto_register(&netlink_proto, 0);
  1783. if (err != 0)
  1784. goto out;
  1785. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  1786. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1787. if (!nl_table)
  1788. goto panic;
  1789. if (totalram_pages >= (128 * 1024))
  1790. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1791. else
  1792. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1793. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1794. limit = (1UL << order) / sizeof(struct hlist_head);
  1795. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1796. for (i = 0; i < MAX_LINKS; i++) {
  1797. struct nl_portid_hash *hash = &nl_table[i].hash;
  1798. hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
  1799. if (!hash->table) {
  1800. while (i-- > 0)
  1801. nl_portid_hash_free(nl_table[i].hash.table,
  1802. 1 * sizeof(*hash->table));
  1803. kfree(nl_table);
  1804. goto panic;
  1805. }
  1806. hash->max_shift = order;
  1807. hash->shift = 0;
  1808. hash->mask = 0;
  1809. hash->rehash_time = jiffies;
  1810. }
  1811. netlink_add_usersock_entry();
  1812. sock_register(&netlink_family_ops);
  1813. register_pernet_subsys(&netlink_net_ops);
  1814. /* The netlink device handler may be needed early. */
  1815. rtnetlink_init();
  1816. out:
  1817. return err;
  1818. panic:
  1819. panic("netlink_init: Cannot allocate nl_table\n");
  1820. }
  1821. core_initcall(netlink_proto_init);