af_netlink.c 44 KB

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