af_netlink.c 50 KB

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