af_netlink.c 43 KB

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