af_netlink.c 43 KB

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