af_netlink.c 50 KB

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