af_netlink.c 40 KB

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