af_netlink.c 41 KB

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