af_netlink.c 44 KB

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