af_netlink.c 41 KB

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