af_netlink.c 36 KB

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