af_netlink.c 40 KB

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