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/notifier.h>
  47. #include <linux/security.h>
  48. #include <linux/jhash.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/random.h>
  51. #include <linux/bitops.h>
  52. #include <linux/mm.h>
  53. #include <linux/types.h>
  54. #include <linux/audit.h>
  55. #include <linux/selinux.h>
  56. #include <linux/mutex.h>
  57. #include <net/sock.h>
  58. #include <net/scm.h>
  59. #include <net/netlink.h>
  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. struct mutex *cb_mutex;
  75. struct mutex cb_def_mutex;
  76. void (*data_ready)(struct sock *sk, int bytes);
  77. struct module *module;
  78. };
  79. #define NETLINK_KERNEL_SOCKET 0x1
  80. #define NETLINK_RECV_PKTINFO 0x2
  81. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  82. {
  83. return (struct netlink_sock *)sk;
  84. }
  85. struct nl_pid_hash {
  86. struct hlist_head *table;
  87. unsigned long rehash_time;
  88. unsigned int mask;
  89. unsigned int shift;
  90. unsigned int entries;
  91. unsigned int max_shift;
  92. u32 rnd;
  93. };
  94. struct netlink_table {
  95. struct nl_pid_hash hash;
  96. struct hlist_head mc_list;
  97. unsigned long *listeners;
  98. unsigned int nl_nonroot;
  99. unsigned int groups;
  100. struct mutex *cb_mutex;
  101. struct module *module;
  102. int registered;
  103. };
  104. static struct netlink_table *nl_table;
  105. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  106. static int netlink_dump(struct sock *sk);
  107. static void netlink_destroy_callback(struct netlink_callback *cb);
  108. static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb);
  109. static DEFINE_RWLOCK(nl_table_lock);
  110. static atomic_t nl_table_users = ATOMIC_INIT(0);
  111. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  112. static u32 netlink_group_mask(u32 group)
  113. {
  114. return group ? 1 << (group - 1) : 0;
  115. }
  116. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  117. {
  118. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  119. }
  120. static void netlink_sock_destruct(struct sock *sk)
  121. {
  122. struct netlink_sock *nlk = nlk_sk(sk);
  123. if (nlk->cb) {
  124. if (nlk->cb->done)
  125. nlk->cb->done(nlk->cb);
  126. netlink_destroy_callback(nlk->cb);
  127. }
  128. skb_queue_purge(&sk->sk_receive_queue);
  129. if (!sock_flag(sk, SOCK_DEAD)) {
  130. printk("Freeing alive netlink socket %p\n", sk);
  131. return;
  132. }
  133. BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
  134. BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
  135. BUG_TRAP(!nlk_sk(sk)->groups);
  136. }
  137. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
  138. * Look, when several writers sleep and reader wakes them up, all but one
  139. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  140. * this, _but_ remember, it adds useless work on UP machines.
  141. */
  142. static void netlink_table_grab(void)
  143. {
  144. write_lock_irq(&nl_table_lock);
  145. if (atomic_read(&nl_table_users)) {
  146. DECLARE_WAITQUEUE(wait, current);
  147. add_wait_queue_exclusive(&nl_table_wait, &wait);
  148. for(;;) {
  149. set_current_state(TASK_UNINTERRUPTIBLE);
  150. if (atomic_read(&nl_table_users) == 0)
  151. break;
  152. write_unlock_irq(&nl_table_lock);
  153. schedule();
  154. write_lock_irq(&nl_table_lock);
  155. }
  156. __set_current_state(TASK_RUNNING);
  157. remove_wait_queue(&nl_table_wait, &wait);
  158. }
  159. }
  160. static __inline__ void netlink_table_ungrab(void)
  161. {
  162. write_unlock_irq(&nl_table_lock);
  163. wake_up(&nl_table_wait);
  164. }
  165. static __inline__ void
  166. netlink_lock_table(void)
  167. {
  168. /* read_lock() synchronizes us to netlink_table_grab */
  169. read_lock(&nl_table_lock);
  170. atomic_inc(&nl_table_users);
  171. read_unlock(&nl_table_lock);
  172. }
  173. static __inline__ void
  174. netlink_unlock_table(void)
  175. {
  176. if (atomic_dec_and_test(&nl_table_users))
  177. wake_up(&nl_table_wait);
  178. }
  179. static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
  180. {
  181. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  182. struct hlist_head *head;
  183. struct sock *sk;
  184. struct hlist_node *node;
  185. read_lock(&nl_table_lock);
  186. head = nl_pid_hashfn(hash, pid);
  187. sk_for_each(sk, node, head) {
  188. if (nlk_sk(sk)->pid == pid) {
  189. sock_hold(sk);
  190. goto found;
  191. }
  192. }
  193. sk = NULL;
  194. found:
  195. read_unlock(&nl_table_lock);
  196. return sk;
  197. }
  198. static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
  199. {
  200. if (size <= PAGE_SIZE)
  201. return kmalloc(size, GFP_ATOMIC);
  202. else
  203. return (struct hlist_head *)
  204. __get_free_pages(GFP_ATOMIC, get_order(size));
  205. }
  206. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  207. {
  208. if (size <= PAGE_SIZE)
  209. kfree(table);
  210. else
  211. free_pages((unsigned long)table, get_order(size));
  212. }
  213. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  214. {
  215. unsigned int omask, mask, shift;
  216. size_t osize, size;
  217. struct hlist_head *otable, *table;
  218. int i;
  219. omask = mask = hash->mask;
  220. osize = size = (mask + 1) * sizeof(*table);
  221. shift = hash->shift;
  222. if (grow) {
  223. if (++shift > hash->max_shift)
  224. return 0;
  225. mask = mask * 2 + 1;
  226. size *= 2;
  227. }
  228. table = nl_pid_hash_alloc(size);
  229. if (!table)
  230. return 0;
  231. memset(table, 0, size);
  232. otable = hash->table;
  233. hash->table = table;
  234. hash->mask = mask;
  235. hash->shift = shift;
  236. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  237. for (i = 0; i <= omask; i++) {
  238. struct sock *sk;
  239. struct hlist_node *node, *tmp;
  240. sk_for_each_safe(sk, node, tmp, &otable[i])
  241. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  242. }
  243. nl_pid_hash_free(otable, osize);
  244. hash->rehash_time = jiffies + 10 * 60 * HZ;
  245. return 1;
  246. }
  247. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  248. {
  249. int avg = hash->entries >> hash->shift;
  250. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  251. return 1;
  252. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  253. nl_pid_hash_rehash(hash, 0);
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. static const struct proto_ops netlink_ops;
  259. static void
  260. netlink_update_listeners(struct sock *sk)
  261. {
  262. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  263. struct hlist_node *node;
  264. unsigned long mask;
  265. unsigned int i;
  266. for (i = 0; i < NLGRPSZ(tbl->groups)/sizeof(unsigned long); i++) {
  267. mask = 0;
  268. sk_for_each_bound(sk, node, &tbl->mc_list)
  269. mask |= nlk_sk(sk)->groups[i];
  270. tbl->listeners[i] = mask;
  271. }
  272. /* this function is only called with the netlink table "grabbed", which
  273. * makes sure updates are visible before bind or setsockopt return. */
  274. }
  275. static int netlink_insert(struct sock *sk, u32 pid)
  276. {
  277. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  278. struct hlist_head *head;
  279. int err = -EADDRINUSE;
  280. struct sock *osk;
  281. struct hlist_node *node;
  282. int len;
  283. netlink_table_grab();
  284. head = nl_pid_hashfn(hash, pid);
  285. len = 0;
  286. sk_for_each(osk, node, head) {
  287. if (nlk_sk(osk)->pid == pid)
  288. break;
  289. len++;
  290. }
  291. if (node)
  292. goto err;
  293. err = -EBUSY;
  294. if (nlk_sk(sk)->pid)
  295. goto err;
  296. err = -ENOMEM;
  297. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  298. goto err;
  299. if (len && nl_pid_hash_dilute(hash, len))
  300. head = nl_pid_hashfn(hash, pid);
  301. hash->entries++;
  302. nlk_sk(sk)->pid = pid;
  303. sk_add_node(sk, head);
  304. err = 0;
  305. err:
  306. netlink_table_ungrab();
  307. return err;
  308. }
  309. static void netlink_remove(struct sock *sk)
  310. {
  311. netlink_table_grab();
  312. if (sk_del_node_init(sk))
  313. nl_table[sk->sk_protocol].hash.entries--;
  314. if (nlk_sk(sk)->subscriptions)
  315. __sk_del_bind_node(sk);
  316. netlink_table_ungrab();
  317. }
  318. static struct proto netlink_proto = {
  319. .name = "NETLINK",
  320. .owner = THIS_MODULE,
  321. .obj_size = sizeof(struct netlink_sock),
  322. };
  323. static int __netlink_create(struct socket *sock, struct mutex *cb_mutex,
  324. int protocol)
  325. {
  326. struct sock *sk;
  327. struct netlink_sock *nlk;
  328. sock->ops = &netlink_ops;
  329. sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
  330. if (!sk)
  331. return -ENOMEM;
  332. sock_init_data(sock, sk);
  333. nlk = nlk_sk(sk);
  334. if (cb_mutex)
  335. nlk->cb_mutex = cb_mutex;
  336. else {
  337. nlk->cb_mutex = &nlk->cb_def_mutex;
  338. mutex_init(nlk->cb_mutex);
  339. }
  340. init_waitqueue_head(&nlk->wait);
  341. sk->sk_destruct = netlink_sock_destruct;
  342. sk->sk_protocol = protocol;
  343. return 0;
  344. }
  345. static int netlink_create(struct socket *sock, int protocol)
  346. {
  347. struct module *module = NULL;
  348. struct mutex *cb_mutex;
  349. struct netlink_sock *nlk;
  350. int err = 0;
  351. sock->state = SS_UNCONNECTED;
  352. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  353. return -ESOCKTNOSUPPORT;
  354. if (protocol<0 || protocol >= MAX_LINKS)
  355. return -EPROTONOSUPPORT;
  356. netlink_lock_table();
  357. #ifdef CONFIG_KMOD
  358. if (!nl_table[protocol].registered) {
  359. netlink_unlock_table();
  360. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  361. netlink_lock_table();
  362. }
  363. #endif
  364. if (nl_table[protocol].registered &&
  365. try_module_get(nl_table[protocol].module))
  366. module = nl_table[protocol].module;
  367. cb_mutex = nl_table[protocol].cb_mutex;
  368. netlink_unlock_table();
  369. if ((err = __netlink_create(sock, cb_mutex, protocol)) < 0)
  370. goto out_module;
  371. nlk = nlk_sk(sock->sk);
  372. nlk->module = module;
  373. out:
  374. return err;
  375. out_module:
  376. module_put(module);
  377. goto out;
  378. }
  379. static int netlink_release(struct socket *sock)
  380. {
  381. struct sock *sk = sock->sk;
  382. struct netlink_sock *nlk;
  383. if (!sk)
  384. return 0;
  385. netlink_remove(sk);
  386. sock_orphan(sk);
  387. nlk = nlk_sk(sk);
  388. /*
  389. * OK. Socket is unlinked, any packets that arrive now
  390. * will be purged.
  391. */
  392. sock->sk = NULL;
  393. wake_up_interruptible_all(&nlk->wait);
  394. skb_queue_purge(&sk->sk_write_queue);
  395. if (nlk->pid && !nlk->subscriptions) {
  396. struct netlink_notify n = {
  397. .protocol = sk->sk_protocol,
  398. .pid = nlk->pid,
  399. };
  400. atomic_notifier_call_chain(&netlink_chain,
  401. NETLINK_URELEASE, &n);
  402. }
  403. module_put(nlk->module);
  404. netlink_table_grab();
  405. if (nlk->flags & NETLINK_KERNEL_SOCKET) {
  406. kfree(nl_table[sk->sk_protocol].listeners);
  407. nl_table[sk->sk_protocol].module = NULL;
  408. nl_table[sk->sk_protocol].registered = 0;
  409. } else if (nlk->subscriptions)
  410. netlink_update_listeners(sk);
  411. netlink_table_ungrab();
  412. kfree(nlk->groups);
  413. nlk->groups = NULL;
  414. sock_put(sk);
  415. return 0;
  416. }
  417. static int netlink_autobind(struct socket *sock)
  418. {
  419. struct sock *sk = sock->sk;
  420. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  421. struct hlist_head *head;
  422. struct sock *osk;
  423. struct hlist_node *node;
  424. s32 pid = current->tgid;
  425. int err;
  426. static s32 rover = -4097;
  427. retry:
  428. cond_resched();
  429. netlink_table_grab();
  430. head = nl_pid_hashfn(hash, pid);
  431. sk_for_each(osk, node, head) {
  432. if (nlk_sk(osk)->pid == pid) {
  433. /* Bind collision, search negative pid values. */
  434. pid = rover--;
  435. if (rover > -4097)
  436. rover = -4097;
  437. netlink_table_ungrab();
  438. goto retry;
  439. }
  440. }
  441. netlink_table_ungrab();
  442. err = netlink_insert(sk, pid);
  443. if (err == -EADDRINUSE)
  444. goto retry;
  445. /* If 2 threads race to autobind, that is fine. */
  446. if (err == -EBUSY)
  447. err = 0;
  448. return err;
  449. }
  450. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  451. {
  452. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  453. capable(CAP_NET_ADMIN);
  454. }
  455. static void
  456. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  457. {
  458. struct netlink_sock *nlk = nlk_sk(sk);
  459. if (nlk->subscriptions && !subscriptions)
  460. __sk_del_bind_node(sk);
  461. else if (!nlk->subscriptions && subscriptions)
  462. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  463. nlk->subscriptions = subscriptions;
  464. }
  465. static int netlink_alloc_groups(struct sock *sk)
  466. {
  467. struct netlink_sock *nlk = nlk_sk(sk);
  468. unsigned int groups;
  469. int err = 0;
  470. netlink_lock_table();
  471. groups = nl_table[sk->sk_protocol].groups;
  472. if (!nl_table[sk->sk_protocol].registered)
  473. err = -ENOENT;
  474. netlink_unlock_table();
  475. if (err)
  476. return err;
  477. nlk->groups = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
  478. if (nlk->groups == NULL)
  479. return -ENOMEM;
  480. nlk->ngroups = groups;
  481. return 0;
  482. }
  483. static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  484. {
  485. struct sock *sk = sock->sk;
  486. struct netlink_sock *nlk = nlk_sk(sk);
  487. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  488. int err;
  489. if (nladdr->nl_family != AF_NETLINK)
  490. return -EINVAL;
  491. /* Only superuser is allowed to listen multicasts */
  492. if (nladdr->nl_groups) {
  493. if (!netlink_capable(sock, NL_NONROOT_RECV))
  494. return -EPERM;
  495. if (nlk->groups == NULL) {
  496. err = netlink_alloc_groups(sk);
  497. if (err)
  498. return err;
  499. }
  500. }
  501. if (nlk->pid) {
  502. if (nladdr->nl_pid != nlk->pid)
  503. return -EINVAL;
  504. } else {
  505. err = nladdr->nl_pid ?
  506. netlink_insert(sk, nladdr->nl_pid) :
  507. netlink_autobind(sock);
  508. if (err)
  509. return err;
  510. }
  511. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  512. return 0;
  513. netlink_table_grab();
  514. netlink_update_subscriptions(sk, nlk->subscriptions +
  515. hweight32(nladdr->nl_groups) -
  516. hweight32(nlk->groups[0]));
  517. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  518. netlink_update_listeners(sk);
  519. netlink_table_ungrab();
  520. return 0;
  521. }
  522. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  523. int alen, int flags)
  524. {
  525. int err = 0;
  526. struct sock *sk = sock->sk;
  527. struct netlink_sock *nlk = nlk_sk(sk);
  528. struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
  529. if (addr->sa_family == AF_UNSPEC) {
  530. sk->sk_state = NETLINK_UNCONNECTED;
  531. nlk->dst_pid = 0;
  532. nlk->dst_group = 0;
  533. return 0;
  534. }
  535. if (addr->sa_family != AF_NETLINK)
  536. return -EINVAL;
  537. /* Only superuser is allowed to send multicasts */
  538. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  539. return -EPERM;
  540. if (!nlk->pid)
  541. err = netlink_autobind(sock);
  542. if (err == 0) {
  543. sk->sk_state = NETLINK_CONNECTED;
  544. nlk->dst_pid = nladdr->nl_pid;
  545. nlk->dst_group = ffs(nladdr->nl_groups);
  546. }
  547. return err;
  548. }
  549. static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
  550. {
  551. struct sock *sk = sock->sk;
  552. struct netlink_sock *nlk = nlk_sk(sk);
  553. struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
  554. nladdr->nl_family = AF_NETLINK;
  555. nladdr->nl_pad = 0;
  556. *addr_len = sizeof(*nladdr);
  557. if (peer) {
  558. nladdr->nl_pid = nlk->dst_pid;
  559. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  560. } else {
  561. nladdr->nl_pid = nlk->pid;
  562. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  563. }
  564. return 0;
  565. }
  566. static void netlink_overrun(struct sock *sk)
  567. {
  568. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  569. sk->sk_err = ENOBUFS;
  570. sk->sk_error_report(sk);
  571. }
  572. }
  573. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  574. {
  575. int protocol = ssk->sk_protocol;
  576. struct sock *sock;
  577. struct netlink_sock *nlk;
  578. sock = netlink_lookup(protocol, pid);
  579. if (!sock)
  580. return ERR_PTR(-ECONNREFUSED);
  581. /* Don't bother queuing skb if kernel socket has no input function */
  582. nlk = nlk_sk(sock);
  583. if ((nlk->pid == 0 && !nlk->data_ready) ||
  584. (sock->sk_state == NETLINK_CONNECTED &&
  585. nlk->dst_pid != nlk_sk(ssk)->pid)) {
  586. sock_put(sock);
  587. return ERR_PTR(-ECONNREFUSED);
  588. }
  589. return sock;
  590. }
  591. struct sock *netlink_getsockbyfilp(struct file *filp)
  592. {
  593. struct inode *inode = filp->f_path.dentry->d_inode;
  594. struct sock *sock;
  595. if (!S_ISSOCK(inode->i_mode))
  596. return ERR_PTR(-ENOTSOCK);
  597. sock = SOCKET_I(inode)->sk;
  598. if (sock->sk_family != AF_NETLINK)
  599. return ERR_PTR(-EINVAL);
  600. sock_hold(sock);
  601. return sock;
  602. }
  603. /*
  604. * Attach a skb to a netlink socket.
  605. * The caller must hold a reference to the destination socket. On error, the
  606. * reference is dropped. The skb is not send to the destination, just all
  607. * all error checks are performed and memory in the queue is reserved.
  608. * Return values:
  609. * < 0: error. skb freed, reference to sock dropped.
  610. * 0: continue
  611. * 1: repeat lookup - reference dropped while waiting for socket memory.
  612. */
  613. int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock,
  614. long timeo, struct sock *ssk)
  615. {
  616. struct netlink_sock *nlk;
  617. nlk = nlk_sk(sk);
  618. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  619. test_bit(0, &nlk->state)) {
  620. DECLARE_WAITQUEUE(wait, current);
  621. if (!timeo) {
  622. if (!ssk || nlk_sk(ssk)->pid == 0)
  623. netlink_overrun(sk);
  624. sock_put(sk);
  625. kfree_skb(skb);
  626. return -EAGAIN;
  627. }
  628. __set_current_state(TASK_INTERRUPTIBLE);
  629. add_wait_queue(&nlk->wait, &wait);
  630. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  631. test_bit(0, &nlk->state)) &&
  632. !sock_flag(sk, SOCK_DEAD))
  633. timeo = schedule_timeout(timeo);
  634. __set_current_state(TASK_RUNNING);
  635. remove_wait_queue(&nlk->wait, &wait);
  636. sock_put(sk);
  637. if (signal_pending(current)) {
  638. kfree_skb(skb);
  639. return sock_intr_errno(timeo);
  640. }
  641. return 1;
  642. }
  643. skb_set_owner_r(skb, sk);
  644. return 0;
  645. }
  646. int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
  647. {
  648. int len = skb->len;
  649. skb_queue_tail(&sk->sk_receive_queue, skb);
  650. sk->sk_data_ready(sk, len);
  651. sock_put(sk);
  652. return len;
  653. }
  654. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  655. {
  656. kfree_skb(skb);
  657. sock_put(sk);
  658. }
  659. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  660. gfp_t allocation)
  661. {
  662. int delta;
  663. skb_orphan(skb);
  664. delta = skb->end - skb->tail;
  665. if (delta * 2 < skb->truesize)
  666. return skb;
  667. if (skb_shared(skb)) {
  668. struct sk_buff *nskb = skb_clone(skb, allocation);
  669. if (!nskb)
  670. return skb;
  671. kfree_skb(skb);
  672. skb = nskb;
  673. }
  674. if (!pskb_expand_head(skb, 0, -delta, allocation))
  675. skb->truesize -= delta;
  676. return skb;
  677. }
  678. int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
  679. {
  680. struct sock *sk;
  681. int err;
  682. long timeo;
  683. skb = netlink_trim(skb, gfp_any());
  684. timeo = sock_sndtimeo(ssk, nonblock);
  685. retry:
  686. sk = netlink_getsockbypid(ssk, pid);
  687. if (IS_ERR(sk)) {
  688. kfree_skb(skb);
  689. return PTR_ERR(sk);
  690. }
  691. err = netlink_attachskb(sk, skb, nonblock, timeo, ssk);
  692. if (err == 1)
  693. goto retry;
  694. if (err)
  695. return err;
  696. return netlink_sendskb(sk, skb, ssk->sk_protocol);
  697. }
  698. int netlink_has_listeners(struct sock *sk, unsigned int group)
  699. {
  700. int res = 0;
  701. BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET));
  702. if (group - 1 < nl_table[sk->sk_protocol].groups)
  703. res = test_bit(group - 1, nl_table[sk->sk_protocol].listeners);
  704. return res;
  705. }
  706. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  707. static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  708. {
  709. struct netlink_sock *nlk = nlk_sk(sk);
  710. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  711. !test_bit(0, &nlk->state)) {
  712. skb_set_owner_r(skb, sk);
  713. skb_queue_tail(&sk->sk_receive_queue, skb);
  714. sk->sk_data_ready(sk, skb->len);
  715. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  716. }
  717. return -1;
  718. }
  719. struct netlink_broadcast_data {
  720. struct sock *exclude_sk;
  721. u32 pid;
  722. u32 group;
  723. int failure;
  724. int congested;
  725. int delivered;
  726. gfp_t allocation;
  727. struct sk_buff *skb, *skb2;
  728. };
  729. static inline int do_one_broadcast(struct sock *sk,
  730. struct netlink_broadcast_data *p)
  731. {
  732. struct netlink_sock *nlk = nlk_sk(sk);
  733. int val;
  734. if (p->exclude_sk == sk)
  735. goto out;
  736. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  737. !test_bit(p->group - 1, nlk->groups))
  738. goto out;
  739. if (p->failure) {
  740. netlink_overrun(sk);
  741. goto out;
  742. }
  743. sock_hold(sk);
  744. if (p->skb2 == NULL) {
  745. if (skb_shared(p->skb)) {
  746. p->skb2 = skb_clone(p->skb, p->allocation);
  747. } else {
  748. p->skb2 = skb_get(p->skb);
  749. /*
  750. * skb ownership may have been set when
  751. * delivered to a previous socket.
  752. */
  753. skb_orphan(p->skb2);
  754. }
  755. }
  756. if (p->skb2 == NULL) {
  757. netlink_overrun(sk);
  758. /* Clone failed. Notify ALL listeners. */
  759. p->failure = 1;
  760. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  761. netlink_overrun(sk);
  762. } else {
  763. p->congested |= val;
  764. p->delivered = 1;
  765. p->skb2 = NULL;
  766. }
  767. sock_put(sk);
  768. out:
  769. return 0;
  770. }
  771. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  772. u32 group, gfp_t allocation)
  773. {
  774. struct netlink_broadcast_data info;
  775. struct hlist_node *node;
  776. struct sock *sk;
  777. skb = netlink_trim(skb, allocation);
  778. info.exclude_sk = ssk;
  779. info.pid = pid;
  780. info.group = group;
  781. info.failure = 0;
  782. info.congested = 0;
  783. info.delivered = 0;
  784. info.allocation = allocation;
  785. info.skb = skb;
  786. info.skb2 = NULL;
  787. /* While we sleep in clone, do not allow to change socket list */
  788. netlink_lock_table();
  789. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  790. do_one_broadcast(sk, &info);
  791. kfree_skb(skb);
  792. netlink_unlock_table();
  793. if (info.skb2)
  794. kfree_skb(info.skb2);
  795. if (info.delivered) {
  796. if (info.congested && (allocation & __GFP_WAIT))
  797. yield();
  798. return 0;
  799. }
  800. if (info.failure)
  801. return -ENOBUFS;
  802. return -ESRCH;
  803. }
  804. struct netlink_set_err_data {
  805. struct sock *exclude_sk;
  806. u32 pid;
  807. u32 group;
  808. int code;
  809. };
  810. static inline int do_one_set_err(struct sock *sk,
  811. struct netlink_set_err_data *p)
  812. {
  813. struct netlink_sock *nlk = nlk_sk(sk);
  814. if (sk == p->exclude_sk)
  815. goto out;
  816. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  817. !test_bit(p->group - 1, nlk->groups))
  818. goto out;
  819. sk->sk_err = p->code;
  820. sk->sk_error_report(sk);
  821. out:
  822. return 0;
  823. }
  824. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  825. {
  826. struct netlink_set_err_data info;
  827. struct hlist_node *node;
  828. struct sock *sk;
  829. info.exclude_sk = ssk;
  830. info.pid = pid;
  831. info.group = group;
  832. info.code = code;
  833. read_lock(&nl_table_lock);
  834. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  835. do_one_set_err(sk, &info);
  836. read_unlock(&nl_table_lock);
  837. }
  838. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  839. char __user *optval, int optlen)
  840. {
  841. struct sock *sk = sock->sk;
  842. struct netlink_sock *nlk = nlk_sk(sk);
  843. unsigned int val = 0;
  844. int err;
  845. if (level != SOL_NETLINK)
  846. return -ENOPROTOOPT;
  847. if (optlen >= sizeof(int) &&
  848. get_user(val, (unsigned 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 const 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);