af_netlink.c 44 KB

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