af_netlink.c 50 KB

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