af_netlink.c 39 KB

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