af_netlink.c 39 KB

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