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