af_netlink.c 32 KB

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