af_netlink.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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. #include <net/netlink.h>
  59. #define Nprintk(a...)
  60. #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
  61. struct netlink_sock {
  62. /* struct sock has to be the first member of netlink_sock */
  63. struct sock sk;
  64. u32 pid;
  65. u32 dst_pid;
  66. u32 dst_group;
  67. u32 flags;
  68. u32 subscriptions;
  69. u32 ngroups;
  70. unsigned long *groups;
  71. unsigned long state;
  72. wait_queue_head_t wait;
  73. struct netlink_callback *cb;
  74. spinlock_t cb_lock;
  75. void (*data_ready)(struct sock *sk, int bytes);
  76. struct module *module;
  77. };
  78. #define NETLINK_KERNEL_SOCKET 0x1
  79. #define NETLINK_RECV_PKTINFO 0x2
  80. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  81. {
  82. return (struct netlink_sock *)sk;
  83. }
  84. struct nl_pid_hash {
  85. struct hlist_head *table;
  86. unsigned long rehash_time;
  87. unsigned int mask;
  88. unsigned int shift;
  89. unsigned int entries;
  90. unsigned int max_shift;
  91. u32 rnd;
  92. };
  93. struct netlink_table {
  94. struct nl_pid_hash hash;
  95. struct hlist_head mc_list;
  96. unsigned int nl_nonroot;
  97. unsigned int groups;
  98. struct module *module;
  99. int registered;
  100. };
  101. static struct netlink_table *nl_table;
  102. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  103. static int netlink_dump(struct sock *sk);
  104. static void netlink_destroy_callback(struct netlink_callback *cb);
  105. static DEFINE_RWLOCK(nl_table_lock);
  106. static atomic_t nl_table_users = ATOMIC_INIT(0);
  107. static struct notifier_block *netlink_chain;
  108. static u32 netlink_group_mask(u32 group)
  109. {
  110. return group ? 1 << (group - 1) : 0;
  111. }
  112. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  113. {
  114. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  115. }
  116. static void netlink_sock_destruct(struct sock *sk)
  117. {
  118. skb_queue_purge(&sk->sk_receive_queue);
  119. if (!sock_flag(sk, SOCK_DEAD)) {
  120. printk("Freeing alive netlink socket %p\n", sk);
  121. return;
  122. }
  123. BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
  124. BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
  125. BUG_TRAP(!nlk_sk(sk)->cb);
  126. BUG_TRAP(!nlk_sk(sk)->groups);
  127. }
  128. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
  129. * Look, when several writers sleep and reader wakes them up, all but one
  130. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  131. * this, _but_ remember, it adds useless work on UP machines.
  132. */
  133. static void netlink_table_grab(void)
  134. {
  135. write_lock_bh(&nl_table_lock);
  136. if (atomic_read(&nl_table_users)) {
  137. DECLARE_WAITQUEUE(wait, current);
  138. add_wait_queue_exclusive(&nl_table_wait, &wait);
  139. for(;;) {
  140. set_current_state(TASK_UNINTERRUPTIBLE);
  141. if (atomic_read(&nl_table_users) == 0)
  142. break;
  143. write_unlock_bh(&nl_table_lock);
  144. schedule();
  145. write_lock_bh(&nl_table_lock);
  146. }
  147. __set_current_state(TASK_RUNNING);
  148. remove_wait_queue(&nl_table_wait, &wait);
  149. }
  150. }
  151. static __inline__ void netlink_table_ungrab(void)
  152. {
  153. write_unlock_bh(&nl_table_lock);
  154. wake_up(&nl_table_wait);
  155. }
  156. static __inline__ void
  157. netlink_lock_table(void)
  158. {
  159. /* read_lock() synchronizes us to netlink_table_grab */
  160. read_lock(&nl_table_lock);
  161. atomic_inc(&nl_table_users);
  162. read_unlock(&nl_table_lock);
  163. }
  164. static __inline__ void
  165. netlink_unlock_table(void)
  166. {
  167. if (atomic_dec_and_test(&nl_table_users))
  168. wake_up(&nl_table_wait);
  169. }
  170. static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
  171. {
  172. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  173. struct hlist_head *head;
  174. struct sock *sk;
  175. struct hlist_node *node;
  176. read_lock(&nl_table_lock);
  177. head = nl_pid_hashfn(hash, pid);
  178. sk_for_each(sk, node, head) {
  179. if (nlk_sk(sk)->pid == pid) {
  180. sock_hold(sk);
  181. goto found;
  182. }
  183. }
  184. sk = NULL;
  185. found:
  186. read_unlock(&nl_table_lock);
  187. return sk;
  188. }
  189. static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
  190. {
  191. if (size <= PAGE_SIZE)
  192. return kmalloc(size, GFP_ATOMIC);
  193. else
  194. return (struct hlist_head *)
  195. __get_free_pages(GFP_ATOMIC, get_order(size));
  196. }
  197. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  198. {
  199. if (size <= PAGE_SIZE)
  200. kfree(table);
  201. else
  202. free_pages((unsigned long)table, get_order(size));
  203. }
  204. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  205. {
  206. unsigned int omask, mask, shift;
  207. size_t osize, size;
  208. struct hlist_head *otable, *table;
  209. int i;
  210. omask = mask = hash->mask;
  211. osize = size = (mask + 1) * sizeof(*table);
  212. shift = hash->shift;
  213. if (grow) {
  214. if (++shift > hash->max_shift)
  215. return 0;
  216. mask = mask * 2 + 1;
  217. size *= 2;
  218. }
  219. table = nl_pid_hash_alloc(size);
  220. if (!table)
  221. return 0;
  222. memset(table, 0, size);
  223. otable = hash->table;
  224. hash->table = table;
  225. hash->mask = mask;
  226. hash->shift = shift;
  227. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  228. for (i = 0; i <= omask; i++) {
  229. struct sock *sk;
  230. struct hlist_node *node, *tmp;
  231. sk_for_each_safe(sk, node, tmp, &otable[i])
  232. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  233. }
  234. nl_pid_hash_free(otable, osize);
  235. hash->rehash_time = jiffies + 10 * 60 * HZ;
  236. return 1;
  237. }
  238. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  239. {
  240. int avg = hash->entries >> hash->shift;
  241. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  242. return 1;
  243. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  244. nl_pid_hash_rehash(hash, 0);
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. static struct proto_ops netlink_ops;
  250. static int netlink_insert(struct sock *sk, u32 pid)
  251. {
  252. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  253. struct hlist_head *head;
  254. int err = -EADDRINUSE;
  255. struct sock *osk;
  256. struct hlist_node *node;
  257. int len;
  258. netlink_table_grab();
  259. head = nl_pid_hashfn(hash, pid);
  260. len = 0;
  261. sk_for_each(osk, node, head) {
  262. if (nlk_sk(osk)->pid == pid)
  263. break;
  264. len++;
  265. }
  266. if (node)
  267. goto err;
  268. err = -EBUSY;
  269. if (nlk_sk(sk)->pid)
  270. goto err;
  271. err = -ENOMEM;
  272. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  273. goto err;
  274. if (len && nl_pid_hash_dilute(hash, len))
  275. head = nl_pid_hashfn(hash, pid);
  276. hash->entries++;
  277. nlk_sk(sk)->pid = pid;
  278. sk_add_node(sk, head);
  279. err = 0;
  280. err:
  281. netlink_table_ungrab();
  282. return err;
  283. }
  284. static void netlink_remove(struct sock *sk)
  285. {
  286. netlink_table_grab();
  287. if (sk_del_node_init(sk))
  288. nl_table[sk->sk_protocol].hash.entries--;
  289. if (nlk_sk(sk)->subscriptions)
  290. __sk_del_bind_node(sk);
  291. netlink_table_ungrab();
  292. }
  293. static struct proto netlink_proto = {
  294. .name = "NETLINK",
  295. .owner = THIS_MODULE,
  296. .obj_size = sizeof(struct netlink_sock),
  297. };
  298. static int __netlink_create(struct socket *sock, int protocol)
  299. {
  300. struct sock *sk;
  301. struct netlink_sock *nlk;
  302. sock->ops = &netlink_ops;
  303. sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
  304. if (!sk)
  305. return -ENOMEM;
  306. sock_init_data(sock, sk);
  307. nlk = nlk_sk(sk);
  308. spin_lock_init(&nlk->cb_lock);
  309. init_waitqueue_head(&nlk->wait);
  310. sk->sk_destruct = netlink_sock_destruct;
  311. sk->sk_protocol = protocol;
  312. return 0;
  313. }
  314. static int netlink_create(struct socket *sock, int protocol)
  315. {
  316. struct module *module = NULL;
  317. struct netlink_sock *nlk;
  318. unsigned int groups;
  319. int err = 0;
  320. sock->state = SS_UNCONNECTED;
  321. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  322. return -ESOCKTNOSUPPORT;
  323. if (protocol<0 || protocol >= MAX_LINKS)
  324. return -EPROTONOSUPPORT;
  325. netlink_lock_table();
  326. #ifdef CONFIG_KMOD
  327. if (!nl_table[protocol].registered) {
  328. netlink_unlock_table();
  329. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  330. netlink_lock_table();
  331. }
  332. #endif
  333. if (nl_table[protocol].registered &&
  334. try_module_get(nl_table[protocol].module))
  335. module = nl_table[protocol].module;
  336. groups = nl_table[protocol].groups;
  337. netlink_unlock_table();
  338. if ((err = __netlink_create(sock, protocol) < 0))
  339. goto out_module;
  340. nlk = nlk_sk(sock->sk);
  341. nlk->module = module;
  342. out:
  343. return err;
  344. out_module:
  345. module_put(module);
  346. goto out;
  347. }
  348. static int netlink_release(struct socket *sock)
  349. {
  350. struct sock *sk = sock->sk;
  351. struct netlink_sock *nlk;
  352. if (!sk)
  353. return 0;
  354. netlink_remove(sk);
  355. nlk = nlk_sk(sk);
  356. spin_lock(&nlk->cb_lock);
  357. if (nlk->cb) {
  358. if (nlk->cb->done)
  359. nlk->cb->done(nlk->cb);
  360. netlink_destroy_callback(nlk->cb);
  361. nlk->cb = NULL;
  362. }
  363. spin_unlock(&nlk->cb_lock);
  364. /* OK. Socket is unlinked, and, therefore,
  365. no new packets will arrive */
  366. sock_orphan(sk);
  367. sock->sk = NULL;
  368. wake_up_interruptible_all(&nlk->wait);
  369. skb_queue_purge(&sk->sk_write_queue);
  370. if (nlk->pid && !nlk->subscriptions) {
  371. struct netlink_notify n = {
  372. .protocol = sk->sk_protocol,
  373. .pid = nlk->pid,
  374. };
  375. notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
  376. }
  377. if (nlk->module)
  378. module_put(nlk->module);
  379. if (nlk->flags & NETLINK_KERNEL_SOCKET) {
  380. netlink_table_grab();
  381. nl_table[sk->sk_protocol].module = NULL;
  382. nl_table[sk->sk_protocol].registered = 0;
  383. netlink_table_ungrab();
  384. }
  385. kfree(nlk->groups);
  386. nlk->groups = NULL;
  387. sock_put(sk);
  388. return 0;
  389. }
  390. static int netlink_autobind(struct socket *sock)
  391. {
  392. struct sock *sk = sock->sk;
  393. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  394. struct hlist_head *head;
  395. struct sock *osk;
  396. struct hlist_node *node;
  397. s32 pid = current->tgid;
  398. int err;
  399. static s32 rover = -4097;
  400. retry:
  401. cond_resched();
  402. netlink_table_grab();
  403. head = nl_pid_hashfn(hash, pid);
  404. sk_for_each(osk, node, head) {
  405. if (nlk_sk(osk)->pid == pid) {
  406. /* Bind collision, search negative pid values. */
  407. pid = rover--;
  408. if (rover > -4097)
  409. rover = -4097;
  410. netlink_table_ungrab();
  411. goto retry;
  412. }
  413. }
  414. netlink_table_ungrab();
  415. err = netlink_insert(sk, pid);
  416. if (err == -EADDRINUSE)
  417. goto retry;
  418. /* If 2 threads race to autobind, that is fine. */
  419. if (err == -EBUSY)
  420. err = 0;
  421. return err;
  422. }
  423. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  424. {
  425. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  426. capable(CAP_NET_ADMIN);
  427. }
  428. static void
  429. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  430. {
  431. struct netlink_sock *nlk = nlk_sk(sk);
  432. if (nlk->subscriptions && !subscriptions)
  433. __sk_del_bind_node(sk);
  434. else if (!nlk->subscriptions && subscriptions)
  435. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  436. nlk->subscriptions = subscriptions;
  437. }
  438. static int netlink_alloc_groups(struct sock *sk)
  439. {
  440. struct netlink_sock *nlk = nlk_sk(sk);
  441. unsigned int groups;
  442. int err = 0;
  443. netlink_lock_table();
  444. groups = nl_table[sk->sk_protocol].groups;
  445. if (!nl_table[sk->sk_protocol].registered)
  446. err = -ENOENT;
  447. netlink_unlock_table();
  448. if (err)
  449. return err;
  450. nlk->groups = kmalloc(NLGRPSZ(groups), GFP_KERNEL);
  451. if (nlk->groups == NULL)
  452. return -ENOMEM;
  453. memset(nlk->groups, 0, NLGRPSZ(groups));
  454. nlk->ngroups = groups;
  455. return 0;
  456. }
  457. static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  458. {
  459. struct sock *sk = sock->sk;
  460. struct netlink_sock *nlk = nlk_sk(sk);
  461. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  462. int err;
  463. if (nladdr->nl_family != AF_NETLINK)
  464. return -EINVAL;
  465. /* Only superuser is allowed to listen multicasts */
  466. if (nladdr->nl_groups) {
  467. if (!netlink_capable(sock, NL_NONROOT_RECV))
  468. return -EPERM;
  469. if (nlk->groups == NULL) {
  470. err = netlink_alloc_groups(sk);
  471. if (err)
  472. return err;
  473. }
  474. }
  475. if (nlk->pid) {
  476. if (nladdr->nl_pid != nlk->pid)
  477. return -EINVAL;
  478. } else {
  479. err = nladdr->nl_pid ?
  480. netlink_insert(sk, nladdr->nl_pid) :
  481. netlink_autobind(sock);
  482. if (err)
  483. return err;
  484. }
  485. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  486. return 0;
  487. netlink_table_grab();
  488. netlink_update_subscriptions(sk, nlk->subscriptions +
  489. hweight32(nladdr->nl_groups) -
  490. hweight32(nlk->groups[0]));
  491. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  492. netlink_table_ungrab();
  493. return 0;
  494. }
  495. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  496. int alen, int flags)
  497. {
  498. int err = 0;
  499. struct sock *sk = sock->sk;
  500. struct netlink_sock *nlk = nlk_sk(sk);
  501. struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
  502. if (addr->sa_family == AF_UNSPEC) {
  503. sk->sk_state = NETLINK_UNCONNECTED;
  504. nlk->dst_pid = 0;
  505. nlk->dst_group = 0;
  506. return 0;
  507. }
  508. if (addr->sa_family != AF_NETLINK)
  509. return -EINVAL;
  510. /* Only superuser is allowed to send multicasts */
  511. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  512. return -EPERM;
  513. if (!nlk->pid)
  514. err = netlink_autobind(sock);
  515. if (err == 0) {
  516. sk->sk_state = NETLINK_CONNECTED;
  517. nlk->dst_pid = nladdr->nl_pid;
  518. nlk->dst_group = ffs(nladdr->nl_groups);
  519. }
  520. return err;
  521. }
  522. static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
  523. {
  524. struct sock *sk = sock->sk;
  525. struct netlink_sock *nlk = nlk_sk(sk);
  526. struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
  527. nladdr->nl_family = AF_NETLINK;
  528. nladdr->nl_pad = 0;
  529. *addr_len = sizeof(*nladdr);
  530. if (peer) {
  531. nladdr->nl_pid = nlk->dst_pid;
  532. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  533. } else {
  534. nladdr->nl_pid = nlk->pid;
  535. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  536. }
  537. return 0;
  538. }
  539. static void netlink_overrun(struct sock *sk)
  540. {
  541. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  542. sk->sk_err = ENOBUFS;
  543. sk->sk_error_report(sk);
  544. }
  545. }
  546. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  547. {
  548. int protocol = ssk->sk_protocol;
  549. struct sock *sock;
  550. struct netlink_sock *nlk;
  551. sock = netlink_lookup(protocol, pid);
  552. if (!sock)
  553. return ERR_PTR(-ECONNREFUSED);
  554. /* Don't bother queuing skb if kernel socket has no input function */
  555. nlk = nlk_sk(sock);
  556. if ((nlk->pid == 0 && !nlk->data_ready) ||
  557. (sock->sk_state == NETLINK_CONNECTED &&
  558. nlk->dst_pid != nlk_sk(ssk)->pid)) {
  559. sock_put(sock);
  560. return ERR_PTR(-ECONNREFUSED);
  561. }
  562. return sock;
  563. }
  564. struct sock *netlink_getsockbyfilp(struct file *filp)
  565. {
  566. struct inode *inode = filp->f_dentry->d_inode;
  567. struct sock *sock;
  568. if (!S_ISSOCK(inode->i_mode))
  569. return ERR_PTR(-ENOTSOCK);
  570. sock = SOCKET_I(inode)->sk;
  571. if (sock->sk_family != AF_NETLINK)
  572. return ERR_PTR(-EINVAL);
  573. sock_hold(sock);
  574. return sock;
  575. }
  576. /*
  577. * Attach a skb to a netlink socket.
  578. * The caller must hold a reference to the destination socket. On error, the
  579. * reference is dropped. The skb is not send to the destination, just all
  580. * all error checks are performed and memory in the queue is reserved.
  581. * Return values:
  582. * < 0: error. skb freed, reference to sock dropped.
  583. * 0: continue
  584. * 1: repeat lookup - reference dropped while waiting for socket memory.
  585. */
  586. int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
  587. {
  588. struct netlink_sock *nlk;
  589. nlk = nlk_sk(sk);
  590. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  591. test_bit(0, &nlk->state)) {
  592. DECLARE_WAITQUEUE(wait, current);
  593. if (!timeo) {
  594. if (!nlk->pid)
  595. netlink_overrun(sk);
  596. sock_put(sk);
  597. kfree_skb(skb);
  598. return -EAGAIN;
  599. }
  600. __set_current_state(TASK_INTERRUPTIBLE);
  601. add_wait_queue(&nlk->wait, &wait);
  602. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  603. test_bit(0, &nlk->state)) &&
  604. !sock_flag(sk, SOCK_DEAD))
  605. timeo = schedule_timeout(timeo);
  606. __set_current_state(TASK_RUNNING);
  607. remove_wait_queue(&nlk->wait, &wait);
  608. sock_put(sk);
  609. if (signal_pending(current)) {
  610. kfree_skb(skb);
  611. return sock_intr_errno(timeo);
  612. }
  613. return 1;
  614. }
  615. skb_set_owner_r(skb, sk);
  616. return 0;
  617. }
  618. int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
  619. {
  620. int len = skb->len;
  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. gfp_t 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. gfp_t 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, gfp_t 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. if (cb->done)
  1100. cb->done(cb);
  1101. nlk->cb = NULL;
  1102. spin_unlock(&nlk->cb_lock);
  1103. netlink_destroy_callback(cb);
  1104. return 0;
  1105. nlmsg_failure:
  1106. return -ENOBUFS;
  1107. }
  1108. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1109. struct nlmsghdr *nlh,
  1110. int (*dump)(struct sk_buff *skb, struct netlink_callback*),
  1111. int (*done)(struct netlink_callback*))
  1112. {
  1113. struct netlink_callback *cb;
  1114. struct sock *sk;
  1115. struct netlink_sock *nlk;
  1116. cb = kmalloc(sizeof(*cb), GFP_KERNEL);
  1117. if (cb == NULL)
  1118. return -ENOBUFS;
  1119. memset(cb, 0, sizeof(*cb));
  1120. cb->dump = dump;
  1121. cb->done = done;
  1122. cb->nlh = nlh;
  1123. atomic_inc(&skb->users);
  1124. cb->skb = skb;
  1125. sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
  1126. if (sk == NULL) {
  1127. netlink_destroy_callback(cb);
  1128. return -ECONNREFUSED;
  1129. }
  1130. nlk = nlk_sk(sk);
  1131. /* A dump is in progress... */
  1132. spin_lock(&nlk->cb_lock);
  1133. if (nlk->cb) {
  1134. spin_unlock(&nlk->cb_lock);
  1135. netlink_destroy_callback(cb);
  1136. sock_put(sk);
  1137. return -EBUSY;
  1138. }
  1139. nlk->cb = cb;
  1140. spin_unlock(&nlk->cb_lock);
  1141. netlink_dump(sk);
  1142. sock_put(sk);
  1143. return 0;
  1144. }
  1145. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1146. {
  1147. struct sk_buff *skb;
  1148. struct nlmsghdr *rep;
  1149. struct nlmsgerr *errmsg;
  1150. int size;
  1151. if (err == 0)
  1152. size = NLMSG_SPACE(sizeof(struct nlmsgerr));
  1153. else
  1154. size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
  1155. skb = alloc_skb(size, GFP_KERNEL);
  1156. if (!skb) {
  1157. struct sock *sk;
  1158. sk = netlink_lookup(in_skb->sk->sk_protocol,
  1159. NETLINK_CB(in_skb).pid);
  1160. if (sk) {
  1161. sk->sk_err = ENOBUFS;
  1162. sk->sk_error_report(sk);
  1163. sock_put(sk);
  1164. }
  1165. return;
  1166. }
  1167. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1168. NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
  1169. errmsg = NLMSG_DATA(rep);
  1170. errmsg->error = err;
  1171. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
  1172. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1173. }
  1174. static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1175. struct nlmsghdr *, int *))
  1176. {
  1177. unsigned int total_len;
  1178. struct nlmsghdr *nlh;
  1179. int err;
  1180. while (skb->len >= nlmsg_total_size(0)) {
  1181. nlh = (struct nlmsghdr *) skb->data;
  1182. if (skb->len < nlh->nlmsg_len)
  1183. return 0;
  1184. total_len = min(NLMSG_ALIGN(nlh->nlmsg_len), skb->len);
  1185. if (cb(skb, nlh, &err) < 0) {
  1186. /* Not an error, but we have to interrupt processing
  1187. * here. Note: that in this case we do not pull
  1188. * message from skb, it will be processed later.
  1189. */
  1190. if (err == 0)
  1191. return -1;
  1192. netlink_ack(skb, nlh, err);
  1193. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  1194. netlink_ack(skb, nlh, 0);
  1195. skb_pull(skb, total_len);
  1196. }
  1197. return 0;
  1198. }
  1199. /**
  1200. * nelink_run_queue - Process netlink receive queue.
  1201. * @sk: Netlink socket containing the queue
  1202. * @qlen: Place to store queue length upon entry
  1203. * @cb: Callback function invoked for each netlink message found
  1204. *
  1205. * Processes as much as there was in the queue upon entry and invokes
  1206. * a callback function for each netlink message found. The callback
  1207. * function may refuse a message by returning a negative error code
  1208. * but setting the error pointer to 0 in which case this function
  1209. * returns with a qlen != 0.
  1210. *
  1211. * qlen must be initialized to 0 before the initial entry, afterwards
  1212. * the function may be called repeatedly until qlen reaches 0.
  1213. */
  1214. void netlink_run_queue(struct sock *sk, unsigned int *qlen,
  1215. int (*cb)(struct sk_buff *, struct nlmsghdr *, int *))
  1216. {
  1217. struct sk_buff *skb;
  1218. if (!*qlen || *qlen > skb_queue_len(&sk->sk_receive_queue))
  1219. *qlen = skb_queue_len(&sk->sk_receive_queue);
  1220. for (; *qlen; (*qlen)--) {
  1221. skb = skb_dequeue(&sk->sk_receive_queue);
  1222. if (netlink_rcv_skb(skb, cb)) {
  1223. if (skb->len)
  1224. skb_queue_head(&sk->sk_receive_queue, skb);
  1225. else {
  1226. kfree_skb(skb);
  1227. (*qlen)--;
  1228. }
  1229. break;
  1230. }
  1231. kfree_skb(skb);
  1232. }
  1233. }
  1234. /**
  1235. * netlink_queue_skip - Skip netlink message while processing queue.
  1236. * @nlh: Netlink message to be skipped
  1237. * @skb: Socket buffer containing the netlink messages.
  1238. *
  1239. * Pulls the given netlink message off the socket buffer so the next
  1240. * call to netlink_queue_run() will not reconsider the message.
  1241. */
  1242. void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb)
  1243. {
  1244. int msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1245. if (msglen > skb->len)
  1246. msglen = skb->len;
  1247. skb_pull(skb, msglen);
  1248. }
  1249. #ifdef CONFIG_PROC_FS
  1250. struct nl_seq_iter {
  1251. int link;
  1252. int hash_idx;
  1253. };
  1254. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1255. {
  1256. struct nl_seq_iter *iter = seq->private;
  1257. int i, j;
  1258. struct sock *s;
  1259. struct hlist_node *node;
  1260. loff_t off = 0;
  1261. for (i=0; i<MAX_LINKS; i++) {
  1262. struct nl_pid_hash *hash = &nl_table[i].hash;
  1263. for (j = 0; j <= hash->mask; j++) {
  1264. sk_for_each(s, node, &hash->table[j]) {
  1265. if (off == pos) {
  1266. iter->link = i;
  1267. iter->hash_idx = j;
  1268. return s;
  1269. }
  1270. ++off;
  1271. }
  1272. }
  1273. }
  1274. return NULL;
  1275. }
  1276. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1277. {
  1278. read_lock(&nl_table_lock);
  1279. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1280. }
  1281. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1282. {
  1283. struct sock *s;
  1284. struct nl_seq_iter *iter;
  1285. int i, j;
  1286. ++*pos;
  1287. if (v == SEQ_START_TOKEN)
  1288. return netlink_seq_socket_idx(seq, 0);
  1289. s = sk_next(v);
  1290. if (s)
  1291. return s;
  1292. iter = seq->private;
  1293. i = iter->link;
  1294. j = iter->hash_idx + 1;
  1295. do {
  1296. struct nl_pid_hash *hash = &nl_table[i].hash;
  1297. for (; j <= hash->mask; j++) {
  1298. s = sk_head(&hash->table[j]);
  1299. if (s) {
  1300. iter->link = i;
  1301. iter->hash_idx = j;
  1302. return s;
  1303. }
  1304. }
  1305. j = 0;
  1306. } while (++i < MAX_LINKS);
  1307. return NULL;
  1308. }
  1309. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1310. {
  1311. read_unlock(&nl_table_lock);
  1312. }
  1313. static int netlink_seq_show(struct seq_file *seq, void *v)
  1314. {
  1315. if (v == SEQ_START_TOKEN)
  1316. seq_puts(seq,
  1317. "sk Eth Pid Groups "
  1318. "Rmem Wmem Dump Locks\n");
  1319. else {
  1320. struct sock *s = v;
  1321. struct netlink_sock *nlk = nlk_sk(s);
  1322. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
  1323. s,
  1324. s->sk_protocol,
  1325. nlk->pid,
  1326. nlk->groups ? (u32)nlk->groups[0] : 0,
  1327. atomic_read(&s->sk_rmem_alloc),
  1328. atomic_read(&s->sk_wmem_alloc),
  1329. nlk->cb,
  1330. atomic_read(&s->sk_refcnt)
  1331. );
  1332. }
  1333. return 0;
  1334. }
  1335. static struct seq_operations netlink_seq_ops = {
  1336. .start = netlink_seq_start,
  1337. .next = netlink_seq_next,
  1338. .stop = netlink_seq_stop,
  1339. .show = netlink_seq_show,
  1340. };
  1341. static int netlink_seq_open(struct inode *inode, struct file *file)
  1342. {
  1343. struct seq_file *seq;
  1344. struct nl_seq_iter *iter;
  1345. int err;
  1346. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  1347. if (!iter)
  1348. return -ENOMEM;
  1349. err = seq_open(file, &netlink_seq_ops);
  1350. if (err) {
  1351. kfree(iter);
  1352. return err;
  1353. }
  1354. memset(iter, 0, sizeof(*iter));
  1355. seq = file->private_data;
  1356. seq->private = iter;
  1357. return 0;
  1358. }
  1359. static struct file_operations netlink_seq_fops = {
  1360. .owner = THIS_MODULE,
  1361. .open = netlink_seq_open,
  1362. .read = seq_read,
  1363. .llseek = seq_lseek,
  1364. .release = seq_release_private,
  1365. };
  1366. #endif
  1367. int netlink_register_notifier(struct notifier_block *nb)
  1368. {
  1369. return notifier_chain_register(&netlink_chain, nb);
  1370. }
  1371. int netlink_unregister_notifier(struct notifier_block *nb)
  1372. {
  1373. return notifier_chain_unregister(&netlink_chain, nb);
  1374. }
  1375. static struct proto_ops netlink_ops = {
  1376. .family = PF_NETLINK,
  1377. .owner = THIS_MODULE,
  1378. .release = netlink_release,
  1379. .bind = netlink_bind,
  1380. .connect = netlink_connect,
  1381. .socketpair = sock_no_socketpair,
  1382. .accept = sock_no_accept,
  1383. .getname = netlink_getname,
  1384. .poll = datagram_poll,
  1385. .ioctl = sock_no_ioctl,
  1386. .listen = sock_no_listen,
  1387. .shutdown = sock_no_shutdown,
  1388. .setsockopt = netlink_setsockopt,
  1389. .getsockopt = netlink_getsockopt,
  1390. .sendmsg = netlink_sendmsg,
  1391. .recvmsg = netlink_recvmsg,
  1392. .mmap = sock_no_mmap,
  1393. .sendpage = sock_no_sendpage,
  1394. };
  1395. static struct net_proto_family netlink_family_ops = {
  1396. .family = PF_NETLINK,
  1397. .create = netlink_create,
  1398. .owner = THIS_MODULE, /* for consistency 8) */
  1399. };
  1400. extern void netlink_skb_parms_too_large(void);
  1401. static int __init netlink_proto_init(void)
  1402. {
  1403. struct sk_buff *dummy_skb;
  1404. int i;
  1405. unsigned long max;
  1406. unsigned int order;
  1407. int err = proto_register(&netlink_proto, 0);
  1408. if (err != 0)
  1409. goto out;
  1410. if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
  1411. netlink_skb_parms_too_large();
  1412. nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
  1413. if (!nl_table) {
  1414. enomem:
  1415. printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
  1416. return -ENOMEM;
  1417. }
  1418. memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
  1419. if (num_physpages >= (128 * 1024))
  1420. max = num_physpages >> (21 - PAGE_SHIFT);
  1421. else
  1422. max = num_physpages >> (23 - PAGE_SHIFT);
  1423. order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
  1424. max = (1UL << order) / sizeof(struct hlist_head);
  1425. order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
  1426. for (i = 0; i < MAX_LINKS; i++) {
  1427. struct nl_pid_hash *hash = &nl_table[i].hash;
  1428. hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
  1429. if (!hash->table) {
  1430. while (i-- > 0)
  1431. nl_pid_hash_free(nl_table[i].hash.table,
  1432. 1 * sizeof(*hash->table));
  1433. kfree(nl_table);
  1434. goto enomem;
  1435. }
  1436. memset(hash->table, 0, 1 * sizeof(*hash->table));
  1437. hash->max_shift = order;
  1438. hash->shift = 0;
  1439. hash->mask = 0;
  1440. hash->rehash_time = jiffies;
  1441. }
  1442. sock_register(&netlink_family_ops);
  1443. #ifdef CONFIG_PROC_FS
  1444. proc_net_fops_create("netlink", 0, &netlink_seq_fops);
  1445. #endif
  1446. /* The netlink device handler may be needed early. */
  1447. rtnetlink_init();
  1448. out:
  1449. return err;
  1450. }
  1451. core_initcall(netlink_proto_init);
  1452. EXPORT_SYMBOL(netlink_ack);
  1453. EXPORT_SYMBOL(netlink_run_queue);
  1454. EXPORT_SYMBOL(netlink_queue_skip);
  1455. EXPORT_SYMBOL(netlink_broadcast);
  1456. EXPORT_SYMBOL(netlink_dump_start);
  1457. EXPORT_SYMBOL(netlink_kernel_create);
  1458. EXPORT_SYMBOL(netlink_register_notifier);
  1459. EXPORT_SYMBOL(netlink_set_err);
  1460. EXPORT_SYMBOL(netlink_set_nonroot);
  1461. EXPORT_SYMBOL(netlink_unicast);
  1462. EXPORT_SYMBOL(netlink_unregister_notifier);