af_netlink.c 40 KB

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