af_netlink.c 43 KB

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