af_netlink.c 43 KB

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