af_netlink.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. struct netlink_sock *nlk;
  619. int len = skb->len;
  620. nlk = nlk_sk(sk);
  621. skb_queue_tail(&sk->sk_receive_queue, skb);
  622. sk->sk_data_ready(sk, len);
  623. sock_put(sk);
  624. return len;
  625. }
  626. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  627. {
  628. kfree_skb(skb);
  629. sock_put(sk);
  630. }
  631. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  632. unsigned int __nocast allocation)
  633. {
  634. int delta;
  635. skb_orphan(skb);
  636. delta = skb->end - skb->tail;
  637. if (delta * 2 < skb->truesize)
  638. return skb;
  639. if (skb_shared(skb)) {
  640. struct sk_buff *nskb = skb_clone(skb, allocation);
  641. if (!nskb)
  642. return skb;
  643. kfree_skb(skb);
  644. skb = nskb;
  645. }
  646. if (!pskb_expand_head(skb, 0, -delta, allocation))
  647. skb->truesize -= delta;
  648. return skb;
  649. }
  650. int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
  651. {
  652. struct sock *sk;
  653. int err;
  654. long timeo;
  655. skb = netlink_trim(skb, gfp_any());
  656. timeo = sock_sndtimeo(ssk, nonblock);
  657. retry:
  658. sk = netlink_getsockbypid(ssk, pid);
  659. if (IS_ERR(sk)) {
  660. kfree_skb(skb);
  661. return PTR_ERR(sk);
  662. }
  663. err = netlink_attachskb(sk, skb, nonblock, timeo);
  664. if (err == 1)
  665. goto retry;
  666. if (err)
  667. return err;
  668. return netlink_sendskb(sk, skb, ssk->sk_protocol);
  669. }
  670. static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  671. {
  672. struct netlink_sock *nlk = nlk_sk(sk);
  673. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  674. !test_bit(0, &nlk->state)) {
  675. skb_set_owner_r(skb, sk);
  676. skb_queue_tail(&sk->sk_receive_queue, skb);
  677. sk->sk_data_ready(sk, skb->len);
  678. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  679. }
  680. return -1;
  681. }
  682. struct netlink_broadcast_data {
  683. struct sock *exclude_sk;
  684. u32 pid;
  685. u32 group;
  686. int failure;
  687. int congested;
  688. int delivered;
  689. unsigned int allocation;
  690. struct sk_buff *skb, *skb2;
  691. };
  692. static inline int do_one_broadcast(struct sock *sk,
  693. struct netlink_broadcast_data *p)
  694. {
  695. struct netlink_sock *nlk = nlk_sk(sk);
  696. int val;
  697. if (p->exclude_sk == sk)
  698. goto out;
  699. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  700. !test_bit(p->group - 1, nlk->groups))
  701. goto out;
  702. if (p->failure) {
  703. netlink_overrun(sk);
  704. goto out;
  705. }
  706. sock_hold(sk);
  707. if (p->skb2 == NULL) {
  708. if (skb_shared(p->skb)) {
  709. p->skb2 = skb_clone(p->skb, p->allocation);
  710. } else {
  711. p->skb2 = skb_get(p->skb);
  712. /*
  713. * skb ownership may have been set when
  714. * delivered to a previous socket.
  715. */
  716. skb_orphan(p->skb2);
  717. }
  718. }
  719. if (p->skb2 == NULL) {
  720. netlink_overrun(sk);
  721. /* Clone failed. Notify ALL listeners. */
  722. p->failure = 1;
  723. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  724. netlink_overrun(sk);
  725. } else {
  726. p->congested |= val;
  727. p->delivered = 1;
  728. p->skb2 = NULL;
  729. }
  730. sock_put(sk);
  731. out:
  732. return 0;
  733. }
  734. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  735. u32 group, unsigned int __nocast allocation)
  736. {
  737. struct netlink_broadcast_data info;
  738. struct hlist_node *node;
  739. struct sock *sk;
  740. skb = netlink_trim(skb, allocation);
  741. info.exclude_sk = ssk;
  742. info.pid = pid;
  743. info.group = group;
  744. info.failure = 0;
  745. info.congested = 0;
  746. info.delivered = 0;
  747. info.allocation = allocation;
  748. info.skb = skb;
  749. info.skb2 = NULL;
  750. /* While we sleep in clone, do not allow to change socket list */
  751. netlink_lock_table();
  752. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  753. do_one_broadcast(sk, &info);
  754. kfree_skb(skb);
  755. netlink_unlock_table();
  756. if (info.skb2)
  757. kfree_skb(info.skb2);
  758. if (info.delivered) {
  759. if (info.congested && (allocation & __GFP_WAIT))
  760. yield();
  761. return 0;
  762. }
  763. if (info.failure)
  764. return -ENOBUFS;
  765. return -ESRCH;
  766. }
  767. struct netlink_set_err_data {
  768. struct sock *exclude_sk;
  769. u32 pid;
  770. u32 group;
  771. int code;
  772. };
  773. static inline int do_one_set_err(struct sock *sk,
  774. struct netlink_set_err_data *p)
  775. {
  776. struct netlink_sock *nlk = nlk_sk(sk);
  777. if (sk == p->exclude_sk)
  778. goto out;
  779. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  780. !test_bit(p->group - 1, nlk->groups))
  781. goto out;
  782. sk->sk_err = p->code;
  783. sk->sk_error_report(sk);
  784. out:
  785. return 0;
  786. }
  787. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  788. {
  789. struct netlink_set_err_data info;
  790. struct hlist_node *node;
  791. struct sock *sk;
  792. info.exclude_sk = ssk;
  793. info.pid = pid;
  794. info.group = group;
  795. info.code = code;
  796. read_lock(&nl_table_lock);
  797. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  798. do_one_set_err(sk, &info);
  799. read_unlock(&nl_table_lock);
  800. }
  801. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  802. char __user *optval, int optlen)
  803. {
  804. struct sock *sk = sock->sk;
  805. struct netlink_sock *nlk = nlk_sk(sk);
  806. int val = 0, err;
  807. if (level != SOL_NETLINK)
  808. return -ENOPROTOOPT;
  809. if (optlen >= sizeof(int) &&
  810. get_user(val, (int __user *)optval))
  811. return -EFAULT;
  812. switch (optname) {
  813. case NETLINK_PKTINFO:
  814. if (val)
  815. nlk->flags |= NETLINK_RECV_PKTINFO;
  816. else
  817. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  818. err = 0;
  819. break;
  820. case NETLINK_ADD_MEMBERSHIP:
  821. case NETLINK_DROP_MEMBERSHIP: {
  822. unsigned int subscriptions;
  823. int old, new = optname == NETLINK_ADD_MEMBERSHIP ? 1 : 0;
  824. if (!netlink_capable(sock, NL_NONROOT_RECV))
  825. return -EPERM;
  826. if (nlk->groups == NULL) {
  827. err = netlink_alloc_groups(sk);
  828. if (err)
  829. return err;
  830. }
  831. if (!val || val - 1 >= nlk->ngroups)
  832. return -EINVAL;
  833. netlink_table_grab();
  834. old = test_bit(val - 1, nlk->groups);
  835. subscriptions = nlk->subscriptions - old + new;
  836. if (new)
  837. __set_bit(val - 1, nlk->groups);
  838. else
  839. __clear_bit(val - 1, nlk->groups);
  840. netlink_update_subscriptions(sk, subscriptions);
  841. netlink_table_ungrab();
  842. err = 0;
  843. break;
  844. }
  845. default:
  846. err = -ENOPROTOOPT;
  847. }
  848. return err;
  849. }
  850. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  851. char __user *optval, int __user *optlen)
  852. {
  853. struct sock *sk = sock->sk;
  854. struct netlink_sock *nlk = nlk_sk(sk);
  855. int len, val, err;
  856. if (level != SOL_NETLINK)
  857. return -ENOPROTOOPT;
  858. if (get_user(len, optlen))
  859. return -EFAULT;
  860. if (len < 0)
  861. return -EINVAL;
  862. switch (optname) {
  863. case NETLINK_PKTINFO:
  864. if (len < sizeof(int))
  865. return -EINVAL;
  866. len = sizeof(int);
  867. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  868. put_user(len, optlen);
  869. put_user(val, optval);
  870. err = 0;
  871. break;
  872. default:
  873. err = -ENOPROTOOPT;
  874. }
  875. return err;
  876. }
  877. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  878. {
  879. struct nl_pktinfo info;
  880. info.group = NETLINK_CB(skb).dst_group;
  881. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  882. }
  883. static inline void netlink_rcv_wake(struct sock *sk)
  884. {
  885. struct netlink_sock *nlk = nlk_sk(sk);
  886. if (skb_queue_empty(&sk->sk_receive_queue))
  887. clear_bit(0, &nlk->state);
  888. if (!test_bit(0, &nlk->state))
  889. wake_up_interruptible(&nlk->wait);
  890. }
  891. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  892. struct msghdr *msg, size_t len)
  893. {
  894. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  895. struct sock *sk = sock->sk;
  896. struct netlink_sock *nlk = nlk_sk(sk);
  897. struct sockaddr_nl *addr=msg->msg_name;
  898. u32 dst_pid;
  899. u32 dst_group;
  900. struct sk_buff *skb;
  901. int err;
  902. struct scm_cookie scm;
  903. if (msg->msg_flags&MSG_OOB)
  904. return -EOPNOTSUPP;
  905. if (NULL == siocb->scm)
  906. siocb->scm = &scm;
  907. err = scm_send(sock, msg, siocb->scm);
  908. if (err < 0)
  909. return err;
  910. if (msg->msg_namelen) {
  911. if (addr->nl_family != AF_NETLINK)
  912. return -EINVAL;
  913. dst_pid = addr->nl_pid;
  914. dst_group = ffs(addr->nl_groups);
  915. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  916. return -EPERM;
  917. } else {
  918. dst_pid = nlk->dst_pid;
  919. dst_group = nlk->dst_group;
  920. }
  921. if (!nlk->pid) {
  922. err = netlink_autobind(sock);
  923. if (err)
  924. goto out;
  925. }
  926. err = -EMSGSIZE;
  927. if (len > sk->sk_sndbuf - 32)
  928. goto out;
  929. err = -ENOBUFS;
  930. skb = alloc_skb(len, GFP_KERNEL);
  931. if (skb==NULL)
  932. goto out;
  933. NETLINK_CB(skb).pid = nlk->pid;
  934. NETLINK_CB(skb).dst_pid = dst_pid;
  935. NETLINK_CB(skb).dst_group = dst_group;
  936. NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
  937. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  938. /* What can I do? Netlink is asynchronous, so that
  939. we will have to save current capabilities to
  940. check them, when this message will be delivered
  941. to corresponding kernel module. --ANK (980802)
  942. */
  943. err = -EFAULT;
  944. if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
  945. kfree_skb(skb);
  946. goto out;
  947. }
  948. err = security_netlink_send(sk, skb);
  949. if (err) {
  950. kfree_skb(skb);
  951. goto out;
  952. }
  953. if (dst_group) {
  954. atomic_inc(&skb->users);
  955. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  956. }
  957. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  958. out:
  959. return err;
  960. }
  961. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  962. struct msghdr *msg, size_t len,
  963. int flags)
  964. {
  965. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  966. struct scm_cookie scm;
  967. struct sock *sk = sock->sk;
  968. struct netlink_sock *nlk = nlk_sk(sk);
  969. int noblock = flags&MSG_DONTWAIT;
  970. size_t copied;
  971. struct sk_buff *skb;
  972. int err;
  973. if (flags&MSG_OOB)
  974. return -EOPNOTSUPP;
  975. copied = 0;
  976. skb = skb_recv_datagram(sk,flags,noblock,&err);
  977. if (skb==NULL)
  978. goto out;
  979. msg->msg_namelen = 0;
  980. copied = skb->len;
  981. if (len < copied) {
  982. msg->msg_flags |= MSG_TRUNC;
  983. copied = len;
  984. }
  985. skb->h.raw = skb->data;
  986. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  987. if (msg->msg_name) {
  988. struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
  989. addr->nl_family = AF_NETLINK;
  990. addr->nl_pad = 0;
  991. addr->nl_pid = NETLINK_CB(skb).pid;
  992. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  993. msg->msg_namelen = sizeof(*addr);
  994. }
  995. if (NULL == siocb->scm) {
  996. memset(&scm, 0, sizeof(scm));
  997. siocb->scm = &scm;
  998. }
  999. siocb->scm->creds = *NETLINK_CREDS(skb);
  1000. skb_free_datagram(sk, skb);
  1001. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1002. netlink_dump(sk);
  1003. scm_recv(sock, msg, siocb->scm, flags);
  1004. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1005. netlink_cmsg_recv_pktinfo(msg, skb);
  1006. out:
  1007. netlink_rcv_wake(sk);
  1008. return err ? : copied;
  1009. }
  1010. static void netlink_data_ready(struct sock *sk, int len)
  1011. {
  1012. struct netlink_sock *nlk = nlk_sk(sk);
  1013. if (nlk->data_ready)
  1014. nlk->data_ready(sk, len);
  1015. netlink_rcv_wake(sk);
  1016. }
  1017. /*
  1018. * We export these functions to other modules. They provide a
  1019. * complete set of kernel non-blocking support for message
  1020. * queueing.
  1021. */
  1022. struct sock *
  1023. netlink_kernel_create(int unit, unsigned int groups,
  1024. void (*input)(struct sock *sk, int len),
  1025. struct module *module)
  1026. {
  1027. struct socket *sock;
  1028. struct sock *sk;
  1029. struct netlink_sock *nlk;
  1030. if (!nl_table)
  1031. return NULL;
  1032. if (unit<0 || unit>=MAX_LINKS)
  1033. return NULL;
  1034. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1035. return NULL;
  1036. if (__netlink_create(sock, unit) < 0)
  1037. goto out_sock_release;
  1038. sk = sock->sk;
  1039. sk->sk_data_ready = netlink_data_ready;
  1040. if (input)
  1041. nlk_sk(sk)->data_ready = input;
  1042. if (netlink_insert(sk, 0))
  1043. goto out_sock_release;
  1044. nlk = nlk_sk(sk);
  1045. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1046. netlink_table_grab();
  1047. nl_table[unit].groups = groups < 32 ? 32 : groups;
  1048. nl_table[unit].module = module;
  1049. nl_table[unit].registered = 1;
  1050. netlink_table_ungrab();
  1051. return sk;
  1052. out_sock_release:
  1053. sock_release(sock);
  1054. return NULL;
  1055. }
  1056. void netlink_set_nonroot(int protocol, unsigned int flags)
  1057. {
  1058. if ((unsigned int)protocol < MAX_LINKS)
  1059. nl_table[protocol].nl_nonroot = flags;
  1060. }
  1061. static void netlink_destroy_callback(struct netlink_callback *cb)
  1062. {
  1063. if (cb->skb)
  1064. kfree_skb(cb->skb);
  1065. kfree(cb);
  1066. }
  1067. /*
  1068. * It looks a bit ugly.
  1069. * It would be better to create kernel thread.
  1070. */
  1071. static int netlink_dump(struct sock *sk)
  1072. {
  1073. struct netlink_sock *nlk = nlk_sk(sk);
  1074. struct netlink_callback *cb;
  1075. struct sk_buff *skb;
  1076. struct nlmsghdr *nlh;
  1077. int len;
  1078. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1079. if (!skb)
  1080. return -ENOBUFS;
  1081. spin_lock(&nlk->cb_lock);
  1082. cb = nlk->cb;
  1083. if (cb == NULL) {
  1084. spin_unlock(&nlk->cb_lock);
  1085. kfree_skb(skb);
  1086. return -EINVAL;
  1087. }
  1088. len = cb->dump(skb, cb);
  1089. if (len > 0) {
  1090. spin_unlock(&nlk->cb_lock);
  1091. skb_queue_tail(&sk->sk_receive_queue, skb);
  1092. sk->sk_data_ready(sk, len);
  1093. return 0;
  1094. }
  1095. nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1096. memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
  1097. skb_queue_tail(&sk->sk_receive_queue, skb);
  1098. sk->sk_data_ready(sk, skb->len);
  1099. cb->done(cb);
  1100. nlk->cb = NULL;
  1101. spin_unlock(&nlk->cb_lock);
  1102. netlink_destroy_callback(cb);
  1103. return 0;
  1104. nlmsg_failure:
  1105. return -ENOBUFS;
  1106. }
  1107. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1108. struct nlmsghdr *nlh,
  1109. int (*dump)(struct sk_buff *skb, struct netlink_callback*),
  1110. int (*done)(struct netlink_callback*))
  1111. {
  1112. struct netlink_callback *cb;
  1113. struct sock *sk;
  1114. struct netlink_sock *nlk;
  1115. cb = kmalloc(sizeof(*cb), GFP_KERNEL);
  1116. if (cb == NULL)
  1117. return -ENOBUFS;
  1118. memset(cb, 0, sizeof(*cb));
  1119. cb->dump = dump;
  1120. cb->done = done;
  1121. cb->nlh = nlh;
  1122. atomic_inc(&skb->users);
  1123. cb->skb = skb;
  1124. sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
  1125. if (sk == NULL) {
  1126. netlink_destroy_callback(cb);
  1127. return -ECONNREFUSED;
  1128. }
  1129. nlk = nlk_sk(sk);
  1130. /* A dump is in progress... */
  1131. spin_lock(&nlk->cb_lock);
  1132. if (nlk->cb) {
  1133. spin_unlock(&nlk->cb_lock);
  1134. netlink_destroy_callback(cb);
  1135. sock_put(sk);
  1136. return -EBUSY;
  1137. }
  1138. nlk->cb = cb;
  1139. spin_unlock(&nlk->cb_lock);
  1140. netlink_dump(sk);
  1141. sock_put(sk);
  1142. return 0;
  1143. }
  1144. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1145. {
  1146. struct sk_buff *skb;
  1147. struct nlmsghdr *rep;
  1148. struct nlmsgerr *errmsg;
  1149. int size;
  1150. if (err == 0)
  1151. size = NLMSG_SPACE(sizeof(struct nlmsgerr));
  1152. else
  1153. size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
  1154. skb = alloc_skb(size, GFP_KERNEL);
  1155. if (!skb) {
  1156. struct sock *sk;
  1157. sk = netlink_lookup(in_skb->sk->sk_protocol,
  1158. NETLINK_CB(in_skb).pid);
  1159. if (sk) {
  1160. sk->sk_err = ENOBUFS;
  1161. sk->sk_error_report(sk);
  1162. sock_put(sk);
  1163. }
  1164. return;
  1165. }
  1166. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1167. NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
  1168. errmsg = NLMSG_DATA(rep);
  1169. errmsg->error = err;
  1170. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
  1171. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1172. }
  1173. #ifdef CONFIG_PROC_FS
  1174. struct nl_seq_iter {
  1175. int link;
  1176. int hash_idx;
  1177. };
  1178. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1179. {
  1180. struct nl_seq_iter *iter = seq->private;
  1181. int i, j;
  1182. struct sock *s;
  1183. struct hlist_node *node;
  1184. loff_t off = 0;
  1185. for (i=0; i<MAX_LINKS; i++) {
  1186. struct nl_pid_hash *hash = &nl_table[i].hash;
  1187. for (j = 0; j <= hash->mask; j++) {
  1188. sk_for_each(s, node, &hash->table[j]) {
  1189. if (off == pos) {
  1190. iter->link = i;
  1191. iter->hash_idx = j;
  1192. return s;
  1193. }
  1194. ++off;
  1195. }
  1196. }
  1197. }
  1198. return NULL;
  1199. }
  1200. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1201. {
  1202. read_lock(&nl_table_lock);
  1203. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1204. }
  1205. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1206. {
  1207. struct sock *s;
  1208. struct nl_seq_iter *iter;
  1209. int i, j;
  1210. ++*pos;
  1211. if (v == SEQ_START_TOKEN)
  1212. return netlink_seq_socket_idx(seq, 0);
  1213. s = sk_next(v);
  1214. if (s)
  1215. return s;
  1216. iter = seq->private;
  1217. i = iter->link;
  1218. j = iter->hash_idx + 1;
  1219. do {
  1220. struct nl_pid_hash *hash = &nl_table[i].hash;
  1221. for (; j <= hash->mask; j++) {
  1222. s = sk_head(&hash->table[j]);
  1223. if (s) {
  1224. iter->link = i;
  1225. iter->hash_idx = j;
  1226. return s;
  1227. }
  1228. }
  1229. j = 0;
  1230. } while (++i < MAX_LINKS);
  1231. return NULL;
  1232. }
  1233. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1234. {
  1235. read_unlock(&nl_table_lock);
  1236. }
  1237. static int netlink_seq_show(struct seq_file *seq, void *v)
  1238. {
  1239. if (v == SEQ_START_TOKEN)
  1240. seq_puts(seq,
  1241. "sk Eth Pid Groups "
  1242. "Rmem Wmem Dump Locks\n");
  1243. else {
  1244. struct sock *s = v;
  1245. struct netlink_sock *nlk = nlk_sk(s);
  1246. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
  1247. s,
  1248. s->sk_protocol,
  1249. nlk->pid,
  1250. nlk->groups ? (u32)nlk->groups[0] : 0,
  1251. atomic_read(&s->sk_rmem_alloc),
  1252. atomic_read(&s->sk_wmem_alloc),
  1253. nlk->cb,
  1254. atomic_read(&s->sk_refcnt)
  1255. );
  1256. }
  1257. return 0;
  1258. }
  1259. static struct seq_operations netlink_seq_ops = {
  1260. .start = netlink_seq_start,
  1261. .next = netlink_seq_next,
  1262. .stop = netlink_seq_stop,
  1263. .show = netlink_seq_show,
  1264. };
  1265. static int netlink_seq_open(struct inode *inode, struct file *file)
  1266. {
  1267. struct seq_file *seq;
  1268. struct nl_seq_iter *iter;
  1269. int err;
  1270. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  1271. if (!iter)
  1272. return -ENOMEM;
  1273. err = seq_open(file, &netlink_seq_ops);
  1274. if (err) {
  1275. kfree(iter);
  1276. return err;
  1277. }
  1278. memset(iter, 0, sizeof(*iter));
  1279. seq = file->private_data;
  1280. seq->private = iter;
  1281. return 0;
  1282. }
  1283. static struct file_operations netlink_seq_fops = {
  1284. .owner = THIS_MODULE,
  1285. .open = netlink_seq_open,
  1286. .read = seq_read,
  1287. .llseek = seq_lseek,
  1288. .release = seq_release_private,
  1289. };
  1290. #endif
  1291. int netlink_register_notifier(struct notifier_block *nb)
  1292. {
  1293. return notifier_chain_register(&netlink_chain, nb);
  1294. }
  1295. int netlink_unregister_notifier(struct notifier_block *nb)
  1296. {
  1297. return notifier_chain_unregister(&netlink_chain, nb);
  1298. }
  1299. static struct proto_ops netlink_ops = {
  1300. .family = PF_NETLINK,
  1301. .owner = THIS_MODULE,
  1302. .release = netlink_release,
  1303. .bind = netlink_bind,
  1304. .connect = netlink_connect,
  1305. .socketpair = sock_no_socketpair,
  1306. .accept = sock_no_accept,
  1307. .getname = netlink_getname,
  1308. .poll = datagram_poll,
  1309. .ioctl = sock_no_ioctl,
  1310. .listen = sock_no_listen,
  1311. .shutdown = sock_no_shutdown,
  1312. .setsockopt = netlink_setsockopt,
  1313. .getsockopt = netlink_getsockopt,
  1314. .sendmsg = netlink_sendmsg,
  1315. .recvmsg = netlink_recvmsg,
  1316. .mmap = sock_no_mmap,
  1317. .sendpage = sock_no_sendpage,
  1318. };
  1319. static struct net_proto_family netlink_family_ops = {
  1320. .family = PF_NETLINK,
  1321. .create = netlink_create,
  1322. .owner = THIS_MODULE, /* for consistency 8) */
  1323. };
  1324. extern void netlink_skb_parms_too_large(void);
  1325. static int __init netlink_proto_init(void)
  1326. {
  1327. struct sk_buff *dummy_skb;
  1328. int i;
  1329. unsigned long max;
  1330. unsigned int order;
  1331. int err = proto_register(&netlink_proto, 0);
  1332. if (err != 0)
  1333. goto out;
  1334. if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
  1335. netlink_skb_parms_too_large();
  1336. nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
  1337. if (!nl_table) {
  1338. enomem:
  1339. printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
  1340. return -ENOMEM;
  1341. }
  1342. memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
  1343. if (num_physpages >= (128 * 1024))
  1344. max = num_physpages >> (21 - PAGE_SHIFT);
  1345. else
  1346. max = num_physpages >> (23 - PAGE_SHIFT);
  1347. order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
  1348. max = (1UL << order) / sizeof(struct hlist_head);
  1349. order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
  1350. for (i = 0; i < MAX_LINKS; i++) {
  1351. struct nl_pid_hash *hash = &nl_table[i].hash;
  1352. hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
  1353. if (!hash->table) {
  1354. while (i-- > 0)
  1355. nl_pid_hash_free(nl_table[i].hash.table,
  1356. 1 * sizeof(*hash->table));
  1357. kfree(nl_table);
  1358. goto enomem;
  1359. }
  1360. memset(hash->table, 0, 1 * sizeof(*hash->table));
  1361. hash->max_shift = order;
  1362. hash->shift = 0;
  1363. hash->mask = 0;
  1364. hash->rehash_time = jiffies;
  1365. }
  1366. sock_register(&netlink_family_ops);
  1367. #ifdef CONFIG_PROC_FS
  1368. proc_net_fops_create("netlink", 0, &netlink_seq_fops);
  1369. #endif
  1370. /* The netlink device handler may be needed early. */
  1371. rtnetlink_init();
  1372. out:
  1373. return err;
  1374. }
  1375. core_initcall(netlink_proto_init);
  1376. EXPORT_SYMBOL(netlink_ack);
  1377. EXPORT_SYMBOL(netlink_broadcast);
  1378. EXPORT_SYMBOL(netlink_dump_start);
  1379. EXPORT_SYMBOL(netlink_kernel_create);
  1380. EXPORT_SYMBOL(netlink_register_notifier);
  1381. EXPORT_SYMBOL(netlink_set_err);
  1382. EXPORT_SYMBOL(netlink_set_nonroot);
  1383. EXPORT_SYMBOL(netlink_unicast);
  1384. EXPORT_SYMBOL(netlink_unregister_notifier);