af_netlink.c 36 KB

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