af_netlink.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  13. * added netlink_proto_exit
  14. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  15. * use nlk_sk, as sk->protinfo is on a diet 8)
  16. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  17. * - inc module use count of module that owns
  18. * the kernel socket in case userspace opens
  19. * socket of same protocol
  20. * - remove all module support, since netlink is
  21. * mandatory if CONFIG_NET=y these days
  22. */
  23. #include <linux/module.h>
  24. #include <linux/capability.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/signal.h>
  28. #include <linux/sched.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/stat.h>
  32. #include <linux/socket.h>
  33. #include <linux/un.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/termios.h>
  36. #include <linux/sockios.h>
  37. #include <linux/net.h>
  38. #include <linux/fs.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/notifier.h>
  47. #include <linux/security.h>
  48. #include <linux/jhash.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/random.h>
  51. #include <linux/bitops.h>
  52. #include <linux/mm.h>
  53. #include <linux/types.h>
  54. #include <linux/audit.h>
  55. #include <linux/mutex.h>
  56. #include <net/net_namespace.h>
  57. #include <net/sock.h>
  58. #include <net/scm.h>
  59. #include <net/netlink.h>
  60. #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
  61. #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
  62. struct netlink_sock {
  63. /* struct sock has to be the first member of netlink_sock */
  64. struct sock sk;
  65. u32 pid;
  66. u32 dst_pid;
  67. u32 dst_group;
  68. u32 flags;
  69. u32 subscriptions;
  70. u32 ngroups;
  71. unsigned long *groups;
  72. unsigned long state;
  73. wait_queue_head_t wait;
  74. struct netlink_callback *cb;
  75. struct mutex *cb_mutex;
  76. struct mutex cb_def_mutex;
  77. void (*netlink_rcv)(struct sk_buff *skb);
  78. struct module *module;
  79. };
  80. struct listeners_rcu_head {
  81. struct rcu_head rcu_head;
  82. void *ptr;
  83. };
  84. #define NETLINK_KERNEL_SOCKET 0x1
  85. #define NETLINK_RECV_PKTINFO 0x2
  86. #define NETLINK_BROADCAST_SEND_ERROR 0x4
  87. #define NETLINK_RECV_NO_ENOBUFS 0x8
  88. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  89. {
  90. return container_of(sk, struct netlink_sock, sk);
  91. }
  92. static inline int netlink_is_kernel(struct sock *sk)
  93. {
  94. return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
  95. }
  96. struct nl_pid_hash {
  97. struct hlist_head *table;
  98. unsigned long rehash_time;
  99. unsigned int mask;
  100. unsigned int shift;
  101. unsigned int entries;
  102. unsigned int max_shift;
  103. u32 rnd;
  104. };
  105. struct netlink_table {
  106. struct nl_pid_hash hash;
  107. struct hlist_head mc_list;
  108. unsigned long *listeners;
  109. unsigned int nl_nonroot;
  110. unsigned int groups;
  111. struct mutex *cb_mutex;
  112. struct module *module;
  113. int registered;
  114. };
  115. static struct netlink_table *nl_table;
  116. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  117. static int netlink_dump(struct sock *sk);
  118. static void netlink_destroy_callback(struct netlink_callback *cb);
  119. static DEFINE_RWLOCK(nl_table_lock);
  120. static atomic_t nl_table_users = ATOMIC_INIT(0);
  121. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  122. static u32 netlink_group_mask(u32 group)
  123. {
  124. return group ? 1 << (group - 1) : 0;
  125. }
  126. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  127. {
  128. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  129. }
  130. static void netlink_sock_destruct(struct sock *sk)
  131. {
  132. struct netlink_sock *nlk = nlk_sk(sk);
  133. if (nlk->cb) {
  134. if (nlk->cb->done)
  135. nlk->cb->done(nlk->cb);
  136. netlink_destroy_callback(nlk->cb);
  137. }
  138. skb_queue_purge(&sk->sk_receive_queue);
  139. if (!sock_flag(sk, SOCK_DEAD)) {
  140. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  141. return;
  142. }
  143. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  144. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  145. WARN_ON(nlk_sk(sk)->groups);
  146. }
  147. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  148. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  149. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  150. * this, _but_ remember, it adds useless work on UP machines.
  151. */
  152. void netlink_table_grab(void)
  153. __acquires(nl_table_lock)
  154. {
  155. might_sleep();
  156. write_lock_irq(&nl_table_lock);
  157. if (atomic_read(&nl_table_users)) {
  158. DECLARE_WAITQUEUE(wait, current);
  159. add_wait_queue_exclusive(&nl_table_wait, &wait);
  160. for (;;) {
  161. set_current_state(TASK_UNINTERRUPTIBLE);
  162. if (atomic_read(&nl_table_users) == 0)
  163. break;
  164. write_unlock_irq(&nl_table_lock);
  165. schedule();
  166. write_lock_irq(&nl_table_lock);
  167. }
  168. __set_current_state(TASK_RUNNING);
  169. remove_wait_queue(&nl_table_wait, &wait);
  170. }
  171. }
  172. void netlink_table_ungrab(void)
  173. __releases(nl_table_lock)
  174. {
  175. write_unlock_irq(&nl_table_lock);
  176. wake_up(&nl_table_wait);
  177. }
  178. static inline void
  179. netlink_lock_table(void)
  180. {
  181. /* read_lock() synchronizes us to netlink_table_grab */
  182. read_lock(&nl_table_lock);
  183. atomic_inc(&nl_table_users);
  184. read_unlock(&nl_table_lock);
  185. }
  186. static inline void
  187. netlink_unlock_table(void)
  188. {
  189. if (atomic_dec_and_test(&nl_table_users))
  190. wake_up(&nl_table_wait);
  191. }
  192. static inline struct sock *netlink_lookup(struct net *net, int protocol,
  193. u32 pid)
  194. {
  195. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  196. struct hlist_head *head;
  197. struct sock *sk;
  198. struct hlist_node *node;
  199. read_lock(&nl_table_lock);
  200. head = nl_pid_hashfn(hash, pid);
  201. sk_for_each(sk, node, head) {
  202. if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
  203. sock_hold(sk);
  204. goto found;
  205. }
  206. }
  207. sk = NULL;
  208. found:
  209. read_unlock(&nl_table_lock);
  210. return sk;
  211. }
  212. static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
  213. {
  214. if (size <= PAGE_SIZE)
  215. return kzalloc(size, GFP_ATOMIC);
  216. else
  217. return (struct hlist_head *)
  218. __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
  219. get_order(size));
  220. }
  221. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  222. {
  223. if (size <= PAGE_SIZE)
  224. kfree(table);
  225. else
  226. free_pages((unsigned long)table, get_order(size));
  227. }
  228. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  229. {
  230. unsigned int omask, mask, shift;
  231. size_t osize, size;
  232. struct hlist_head *otable, *table;
  233. int i;
  234. omask = mask = hash->mask;
  235. osize = size = (mask + 1) * sizeof(*table);
  236. shift = hash->shift;
  237. if (grow) {
  238. if (++shift > hash->max_shift)
  239. return 0;
  240. mask = mask * 2 + 1;
  241. size *= 2;
  242. }
  243. table = nl_pid_hash_zalloc(size);
  244. if (!table)
  245. return 0;
  246. otable = hash->table;
  247. hash->table = table;
  248. hash->mask = mask;
  249. hash->shift = shift;
  250. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  251. for (i = 0; i <= omask; i++) {
  252. struct sock *sk;
  253. struct hlist_node *node, *tmp;
  254. sk_for_each_safe(sk, node, tmp, &otable[i])
  255. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  256. }
  257. nl_pid_hash_free(otable, osize);
  258. hash->rehash_time = jiffies + 10 * 60 * HZ;
  259. return 1;
  260. }
  261. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  262. {
  263. int avg = hash->entries >> hash->shift;
  264. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  265. return 1;
  266. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  267. nl_pid_hash_rehash(hash, 0);
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. static const struct proto_ops netlink_ops;
  273. static void
  274. netlink_update_listeners(struct sock *sk)
  275. {
  276. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  277. struct hlist_node *node;
  278. unsigned long mask;
  279. unsigned int i;
  280. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  281. mask = 0;
  282. sk_for_each_bound(sk, node, &tbl->mc_list) {
  283. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  284. mask |= nlk_sk(sk)->groups[i];
  285. }
  286. tbl->listeners[i] = mask;
  287. }
  288. /* this function is only called with the netlink table "grabbed", which
  289. * makes sure updates are visible before bind or setsockopt return. */
  290. }
  291. static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
  292. {
  293. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  294. struct hlist_head *head;
  295. int err = -EADDRINUSE;
  296. struct sock *osk;
  297. struct hlist_node *node;
  298. int len;
  299. netlink_table_grab();
  300. head = nl_pid_hashfn(hash, pid);
  301. len = 0;
  302. sk_for_each(osk, node, head) {
  303. if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
  304. break;
  305. len++;
  306. }
  307. if (node)
  308. goto err;
  309. err = -EBUSY;
  310. if (nlk_sk(sk)->pid)
  311. goto err;
  312. err = -ENOMEM;
  313. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  314. goto err;
  315. if (len && nl_pid_hash_dilute(hash, len))
  316. head = nl_pid_hashfn(hash, pid);
  317. hash->entries++;
  318. nlk_sk(sk)->pid = pid;
  319. sk_add_node(sk, head);
  320. err = 0;
  321. err:
  322. netlink_table_ungrab();
  323. return err;
  324. }
  325. static void netlink_remove(struct sock *sk)
  326. {
  327. netlink_table_grab();
  328. if (sk_del_node_init(sk))
  329. nl_table[sk->sk_protocol].hash.entries--;
  330. if (nlk_sk(sk)->subscriptions)
  331. __sk_del_bind_node(sk);
  332. netlink_table_ungrab();
  333. }
  334. static struct proto netlink_proto = {
  335. .name = "NETLINK",
  336. .owner = THIS_MODULE,
  337. .obj_size = sizeof(struct netlink_sock),
  338. };
  339. static int __netlink_create(struct net *net, struct socket *sock,
  340. struct mutex *cb_mutex, int protocol)
  341. {
  342. struct sock *sk;
  343. struct netlink_sock *nlk;
  344. sock->ops = &netlink_ops;
  345. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  346. if (!sk)
  347. return -ENOMEM;
  348. sock_init_data(sock, sk);
  349. nlk = nlk_sk(sk);
  350. if (cb_mutex)
  351. nlk->cb_mutex = cb_mutex;
  352. else {
  353. nlk->cb_mutex = &nlk->cb_def_mutex;
  354. mutex_init(nlk->cb_mutex);
  355. }
  356. init_waitqueue_head(&nlk->wait);
  357. sk->sk_destruct = netlink_sock_destruct;
  358. sk->sk_protocol = protocol;
  359. return 0;
  360. }
  361. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  362. int kern)
  363. {
  364. struct module *module = NULL;
  365. struct mutex *cb_mutex;
  366. struct netlink_sock *nlk;
  367. int err = 0;
  368. sock->state = SS_UNCONNECTED;
  369. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  370. return -ESOCKTNOSUPPORT;
  371. if (protocol < 0 || protocol >= MAX_LINKS)
  372. return -EPROTONOSUPPORT;
  373. netlink_lock_table();
  374. #ifdef CONFIG_MODULES
  375. if (!nl_table[protocol].registered) {
  376. netlink_unlock_table();
  377. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  378. netlink_lock_table();
  379. }
  380. #endif
  381. if (nl_table[protocol].registered &&
  382. try_module_get(nl_table[protocol].module))
  383. module = nl_table[protocol].module;
  384. else
  385. err = -EPROTONOSUPPORT;
  386. cb_mutex = nl_table[protocol].cb_mutex;
  387. netlink_unlock_table();
  388. if (err < 0)
  389. goto out;
  390. err = __netlink_create(net, sock, cb_mutex, protocol);
  391. if (err < 0)
  392. goto out_module;
  393. local_bh_disable();
  394. sock_prot_inuse_add(net, &netlink_proto, 1);
  395. local_bh_enable();
  396. nlk = nlk_sk(sock->sk);
  397. nlk->module = module;
  398. out:
  399. return err;
  400. out_module:
  401. module_put(module);
  402. goto out;
  403. }
  404. static int netlink_release(struct socket *sock)
  405. {
  406. struct sock *sk = sock->sk;
  407. struct netlink_sock *nlk;
  408. if (!sk)
  409. return 0;
  410. netlink_remove(sk);
  411. sock_orphan(sk);
  412. nlk = nlk_sk(sk);
  413. /*
  414. * OK. Socket is unlinked, any packets that arrive now
  415. * will be purged.
  416. */
  417. sock->sk = NULL;
  418. wake_up_interruptible_all(&nlk->wait);
  419. skb_queue_purge(&sk->sk_write_queue);
  420. if (nlk->pid) {
  421. struct netlink_notify n = {
  422. .net = sock_net(sk),
  423. .protocol = sk->sk_protocol,
  424. .pid = nlk->pid,
  425. };
  426. atomic_notifier_call_chain(&netlink_chain,
  427. NETLINK_URELEASE, &n);
  428. }
  429. module_put(nlk->module);
  430. netlink_table_grab();
  431. if (netlink_is_kernel(sk)) {
  432. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  433. if (--nl_table[sk->sk_protocol].registered == 0) {
  434. kfree(nl_table[sk->sk_protocol].listeners);
  435. nl_table[sk->sk_protocol].module = NULL;
  436. nl_table[sk->sk_protocol].registered = 0;
  437. }
  438. } else if (nlk->subscriptions)
  439. netlink_update_listeners(sk);
  440. netlink_table_ungrab();
  441. kfree(nlk->groups);
  442. nlk->groups = NULL;
  443. local_bh_disable();
  444. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  445. local_bh_enable();
  446. sock_put(sk);
  447. return 0;
  448. }
  449. static int netlink_autobind(struct socket *sock)
  450. {
  451. struct sock *sk = sock->sk;
  452. struct net *net = sock_net(sk);
  453. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  454. struct hlist_head *head;
  455. struct sock *osk;
  456. struct hlist_node *node;
  457. s32 pid = task_tgid_vnr(current);
  458. int err;
  459. static s32 rover = -4097;
  460. retry:
  461. cond_resched();
  462. netlink_table_grab();
  463. head = nl_pid_hashfn(hash, pid);
  464. sk_for_each(osk, node, head) {
  465. if (!net_eq(sock_net(osk), net))
  466. continue;
  467. if (nlk_sk(osk)->pid == pid) {
  468. /* Bind collision, search negative pid values. */
  469. pid = rover--;
  470. if (rover > -4097)
  471. rover = -4097;
  472. netlink_table_ungrab();
  473. goto retry;
  474. }
  475. }
  476. netlink_table_ungrab();
  477. err = netlink_insert(sk, net, pid);
  478. if (err == -EADDRINUSE)
  479. goto retry;
  480. /* If 2 threads race to autobind, that is fine. */
  481. if (err == -EBUSY)
  482. err = 0;
  483. return err;
  484. }
  485. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  486. {
  487. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  488. capable(CAP_NET_ADMIN);
  489. }
  490. static void
  491. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  492. {
  493. struct netlink_sock *nlk = nlk_sk(sk);
  494. if (nlk->subscriptions && !subscriptions)
  495. __sk_del_bind_node(sk);
  496. else if (!nlk->subscriptions && subscriptions)
  497. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  498. nlk->subscriptions = subscriptions;
  499. }
  500. static int netlink_realloc_groups(struct sock *sk)
  501. {
  502. struct netlink_sock *nlk = nlk_sk(sk);
  503. unsigned int groups;
  504. unsigned long *new_groups;
  505. int err = 0;
  506. netlink_table_grab();
  507. groups = nl_table[sk->sk_protocol].groups;
  508. if (!nl_table[sk->sk_protocol].registered) {
  509. err = -ENOENT;
  510. goto out_unlock;
  511. }
  512. if (nlk->ngroups >= groups)
  513. goto out_unlock;
  514. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  515. if (new_groups == NULL) {
  516. err = -ENOMEM;
  517. goto out_unlock;
  518. }
  519. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  520. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  521. nlk->groups = new_groups;
  522. nlk->ngroups = groups;
  523. out_unlock:
  524. netlink_table_ungrab();
  525. return err;
  526. }
  527. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  528. int addr_len)
  529. {
  530. struct sock *sk = sock->sk;
  531. struct net *net = sock_net(sk);
  532. struct netlink_sock *nlk = nlk_sk(sk);
  533. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  534. int err;
  535. if (nladdr->nl_family != AF_NETLINK)
  536. return -EINVAL;
  537. /* Only superuser is allowed to listen multicasts */
  538. if (nladdr->nl_groups) {
  539. if (!netlink_capable(sock, NL_NONROOT_RECV))
  540. return -EPERM;
  541. err = netlink_realloc_groups(sk);
  542. if (err)
  543. return err;
  544. }
  545. if (nlk->pid) {
  546. if (nladdr->nl_pid != nlk->pid)
  547. return -EINVAL;
  548. } else {
  549. err = nladdr->nl_pid ?
  550. netlink_insert(sk, net, nladdr->nl_pid) :
  551. netlink_autobind(sock);
  552. if (err)
  553. return err;
  554. }
  555. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  556. return 0;
  557. netlink_table_grab();
  558. netlink_update_subscriptions(sk, nlk->subscriptions +
  559. hweight32(nladdr->nl_groups) -
  560. hweight32(nlk->groups[0]));
  561. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  562. netlink_update_listeners(sk);
  563. netlink_table_ungrab();
  564. return 0;
  565. }
  566. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  567. int alen, int flags)
  568. {
  569. int err = 0;
  570. struct sock *sk = sock->sk;
  571. struct netlink_sock *nlk = nlk_sk(sk);
  572. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  573. if (alen < sizeof(addr->sa_family))
  574. return -EINVAL;
  575. if (addr->sa_family == AF_UNSPEC) {
  576. sk->sk_state = NETLINK_UNCONNECTED;
  577. nlk->dst_pid = 0;
  578. nlk->dst_group = 0;
  579. return 0;
  580. }
  581. if (addr->sa_family != AF_NETLINK)
  582. return -EINVAL;
  583. /* Only superuser is allowed to send multicasts */
  584. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  585. return -EPERM;
  586. if (!nlk->pid)
  587. err = netlink_autobind(sock);
  588. if (err == 0) {
  589. sk->sk_state = NETLINK_CONNECTED;
  590. nlk->dst_pid = nladdr->nl_pid;
  591. nlk->dst_group = ffs(nladdr->nl_groups);
  592. }
  593. return err;
  594. }
  595. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  596. int *addr_len, int peer)
  597. {
  598. struct sock *sk = sock->sk;
  599. struct netlink_sock *nlk = nlk_sk(sk);
  600. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  601. nladdr->nl_family = AF_NETLINK;
  602. nladdr->nl_pad = 0;
  603. *addr_len = sizeof(*nladdr);
  604. if (peer) {
  605. nladdr->nl_pid = nlk->dst_pid;
  606. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  607. } else {
  608. nladdr->nl_pid = nlk->pid;
  609. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  610. }
  611. return 0;
  612. }
  613. static void netlink_overrun(struct sock *sk)
  614. {
  615. struct netlink_sock *nlk = nlk_sk(sk);
  616. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  617. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  618. sk->sk_err = ENOBUFS;
  619. sk->sk_error_report(sk);
  620. }
  621. }
  622. atomic_inc(&sk->sk_drops);
  623. }
  624. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  625. {
  626. struct sock *sock;
  627. struct netlink_sock *nlk;
  628. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
  629. if (!sock)
  630. return ERR_PTR(-ECONNREFUSED);
  631. /* Don't bother queuing skb if kernel socket has no input function */
  632. nlk = nlk_sk(sock);
  633. if (sock->sk_state == NETLINK_CONNECTED &&
  634. nlk->dst_pid != nlk_sk(ssk)->pid) {
  635. sock_put(sock);
  636. return ERR_PTR(-ECONNREFUSED);
  637. }
  638. return sock;
  639. }
  640. struct sock *netlink_getsockbyfilp(struct file *filp)
  641. {
  642. struct inode *inode = filp->f_path.dentry->d_inode;
  643. struct sock *sock;
  644. if (!S_ISSOCK(inode->i_mode))
  645. return ERR_PTR(-ENOTSOCK);
  646. sock = SOCKET_I(inode)->sk;
  647. if (sock->sk_family != AF_NETLINK)
  648. return ERR_PTR(-EINVAL);
  649. sock_hold(sock);
  650. return sock;
  651. }
  652. /*
  653. * Attach a skb to a netlink socket.
  654. * The caller must hold a reference to the destination socket. On error, the
  655. * reference is dropped. The skb is not send to the destination, just all
  656. * all error checks are performed and memory in the queue is reserved.
  657. * Return values:
  658. * < 0: error. skb freed, reference to sock dropped.
  659. * 0: continue
  660. * 1: repeat lookup - reference dropped while waiting for socket memory.
  661. */
  662. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  663. long *timeo, struct sock *ssk)
  664. {
  665. struct netlink_sock *nlk;
  666. nlk = nlk_sk(sk);
  667. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  668. test_bit(0, &nlk->state)) {
  669. DECLARE_WAITQUEUE(wait, current);
  670. if (!*timeo) {
  671. if (!ssk || netlink_is_kernel(ssk))
  672. netlink_overrun(sk);
  673. sock_put(sk);
  674. kfree_skb(skb);
  675. return -EAGAIN;
  676. }
  677. __set_current_state(TASK_INTERRUPTIBLE);
  678. add_wait_queue(&nlk->wait, &wait);
  679. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  680. test_bit(0, &nlk->state)) &&
  681. !sock_flag(sk, SOCK_DEAD))
  682. *timeo = schedule_timeout(*timeo);
  683. __set_current_state(TASK_RUNNING);
  684. remove_wait_queue(&nlk->wait, &wait);
  685. sock_put(sk);
  686. if (signal_pending(current)) {
  687. kfree_skb(skb);
  688. return sock_intr_errno(*timeo);
  689. }
  690. return 1;
  691. }
  692. skb_set_owner_r(skb, sk);
  693. return 0;
  694. }
  695. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  696. {
  697. int len = skb->len;
  698. skb_queue_tail(&sk->sk_receive_queue, skb);
  699. sk->sk_data_ready(sk, len);
  700. sock_put(sk);
  701. return len;
  702. }
  703. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  704. {
  705. kfree_skb(skb);
  706. sock_put(sk);
  707. }
  708. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  709. gfp_t allocation)
  710. {
  711. int delta;
  712. skb_orphan(skb);
  713. delta = skb->end - skb->tail;
  714. if (delta * 2 < skb->truesize)
  715. return skb;
  716. if (skb_shared(skb)) {
  717. struct sk_buff *nskb = skb_clone(skb, allocation);
  718. if (!nskb)
  719. return skb;
  720. kfree_skb(skb);
  721. skb = nskb;
  722. }
  723. if (!pskb_expand_head(skb, 0, -delta, allocation))
  724. skb->truesize -= delta;
  725. return skb;
  726. }
  727. static inline void netlink_rcv_wake(struct sock *sk)
  728. {
  729. struct netlink_sock *nlk = nlk_sk(sk);
  730. if (skb_queue_empty(&sk->sk_receive_queue))
  731. clear_bit(0, &nlk->state);
  732. if (!test_bit(0, &nlk->state))
  733. wake_up_interruptible(&nlk->wait);
  734. }
  735. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  736. {
  737. int ret;
  738. struct netlink_sock *nlk = nlk_sk(sk);
  739. ret = -ECONNREFUSED;
  740. if (nlk->netlink_rcv != NULL) {
  741. ret = skb->len;
  742. skb_set_owner_r(skb, sk);
  743. nlk->netlink_rcv(skb);
  744. }
  745. kfree_skb(skb);
  746. sock_put(sk);
  747. return ret;
  748. }
  749. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  750. u32 pid, int nonblock)
  751. {
  752. struct sock *sk;
  753. int err;
  754. long timeo;
  755. skb = netlink_trim(skb, gfp_any());
  756. timeo = sock_sndtimeo(ssk, nonblock);
  757. retry:
  758. sk = netlink_getsockbypid(ssk, pid);
  759. if (IS_ERR(sk)) {
  760. kfree_skb(skb);
  761. return PTR_ERR(sk);
  762. }
  763. if (netlink_is_kernel(sk))
  764. return netlink_unicast_kernel(sk, skb);
  765. if (sk_filter(sk, skb)) {
  766. err = skb->len;
  767. kfree_skb(skb);
  768. sock_put(sk);
  769. return err;
  770. }
  771. err = netlink_attachskb(sk, skb, &timeo, ssk);
  772. if (err == 1)
  773. goto retry;
  774. if (err)
  775. return err;
  776. return netlink_sendskb(sk, skb);
  777. }
  778. EXPORT_SYMBOL(netlink_unicast);
  779. int netlink_has_listeners(struct sock *sk, unsigned int group)
  780. {
  781. int res = 0;
  782. unsigned long *listeners;
  783. BUG_ON(!netlink_is_kernel(sk));
  784. rcu_read_lock();
  785. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  786. if (group - 1 < nl_table[sk->sk_protocol].groups)
  787. res = test_bit(group - 1, listeners);
  788. rcu_read_unlock();
  789. return res;
  790. }
  791. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  792. static inline int netlink_broadcast_deliver(struct sock *sk,
  793. struct sk_buff *skb)
  794. {
  795. struct netlink_sock *nlk = nlk_sk(sk);
  796. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  797. !test_bit(0, &nlk->state)) {
  798. skb_set_owner_r(skb, sk);
  799. skb_queue_tail(&sk->sk_receive_queue, skb);
  800. sk->sk_data_ready(sk, skb->len);
  801. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  802. }
  803. return -1;
  804. }
  805. struct netlink_broadcast_data {
  806. struct sock *exclude_sk;
  807. struct net *net;
  808. u32 pid;
  809. u32 group;
  810. int failure;
  811. int delivery_failure;
  812. int congested;
  813. int delivered;
  814. gfp_t allocation;
  815. struct sk_buff *skb, *skb2;
  816. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  817. void *tx_data;
  818. };
  819. static inline int do_one_broadcast(struct sock *sk,
  820. struct netlink_broadcast_data *p)
  821. {
  822. struct netlink_sock *nlk = nlk_sk(sk);
  823. int val;
  824. if (p->exclude_sk == sk)
  825. goto out;
  826. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  827. !test_bit(p->group - 1, nlk->groups))
  828. goto out;
  829. if (!net_eq(sock_net(sk), p->net))
  830. goto out;
  831. if (p->failure) {
  832. netlink_overrun(sk);
  833. goto out;
  834. }
  835. sock_hold(sk);
  836. if (p->skb2 == NULL) {
  837. if (skb_shared(p->skb)) {
  838. p->skb2 = skb_clone(p->skb, p->allocation);
  839. } else {
  840. p->skb2 = skb_get(p->skb);
  841. /*
  842. * skb ownership may have been set when
  843. * delivered to a previous socket.
  844. */
  845. skb_orphan(p->skb2);
  846. }
  847. }
  848. if (p->skb2 == NULL) {
  849. netlink_overrun(sk);
  850. /* Clone failed. Notify ALL listeners. */
  851. p->failure = 1;
  852. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  853. p->delivery_failure = 1;
  854. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  855. kfree_skb(p->skb2);
  856. p->skb2 = NULL;
  857. } else if (sk_filter(sk, p->skb2)) {
  858. kfree_skb(p->skb2);
  859. p->skb2 = NULL;
  860. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  861. netlink_overrun(sk);
  862. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  863. p->delivery_failure = 1;
  864. } else {
  865. p->congested |= val;
  866. p->delivered = 1;
  867. p->skb2 = NULL;
  868. }
  869. sock_put(sk);
  870. out:
  871. return 0;
  872. }
  873. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid,
  874. u32 group, gfp_t allocation,
  875. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  876. void *filter_data)
  877. {
  878. struct net *net = sock_net(ssk);
  879. struct netlink_broadcast_data info;
  880. struct hlist_node *node;
  881. struct sock *sk;
  882. skb = netlink_trim(skb, allocation);
  883. info.exclude_sk = ssk;
  884. info.net = net;
  885. info.pid = pid;
  886. info.group = group;
  887. info.failure = 0;
  888. info.delivery_failure = 0;
  889. info.congested = 0;
  890. info.delivered = 0;
  891. info.allocation = allocation;
  892. info.skb = skb;
  893. info.skb2 = NULL;
  894. info.tx_filter = filter;
  895. info.tx_data = filter_data;
  896. /* While we sleep in clone, do not allow to change socket list */
  897. netlink_lock_table();
  898. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  899. do_one_broadcast(sk, &info);
  900. kfree_skb(skb);
  901. netlink_unlock_table();
  902. kfree_skb(info.skb2);
  903. if (info.delivery_failure)
  904. return -ENOBUFS;
  905. if (info.delivered) {
  906. if (info.congested && (allocation & __GFP_WAIT))
  907. yield();
  908. return 0;
  909. }
  910. return -ESRCH;
  911. }
  912. EXPORT_SYMBOL(netlink_broadcast_filtered);
  913. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  914. u32 group, gfp_t allocation)
  915. {
  916. return netlink_broadcast_filtered(ssk, skb, pid, group, allocation,
  917. NULL, NULL);
  918. }
  919. EXPORT_SYMBOL(netlink_broadcast);
  920. struct netlink_set_err_data {
  921. struct sock *exclude_sk;
  922. u32 pid;
  923. u32 group;
  924. int code;
  925. };
  926. static inline int do_one_set_err(struct sock *sk,
  927. struct netlink_set_err_data *p)
  928. {
  929. struct netlink_sock *nlk = nlk_sk(sk);
  930. int ret = 0;
  931. if (sk == p->exclude_sk)
  932. goto out;
  933. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  934. goto out;
  935. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  936. !test_bit(p->group - 1, nlk->groups))
  937. goto out;
  938. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  939. ret = 1;
  940. goto out;
  941. }
  942. sk->sk_err = p->code;
  943. sk->sk_error_report(sk);
  944. out:
  945. return ret;
  946. }
  947. /**
  948. * netlink_set_err - report error to broadcast listeners
  949. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  950. * @pid: the PID of a process that we want to skip (if any)
  951. * @groups: the broadcast group that will notice the error
  952. * @code: error code, must be negative (as usual in kernelspace)
  953. *
  954. * This function returns the number of broadcast listeners that have set the
  955. * NETLINK_RECV_NO_ENOBUFS socket option.
  956. */
  957. int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  958. {
  959. struct netlink_set_err_data info;
  960. struct hlist_node *node;
  961. struct sock *sk;
  962. int ret = 0;
  963. info.exclude_sk = ssk;
  964. info.pid = pid;
  965. info.group = group;
  966. /* sk->sk_err wants a positive error value */
  967. info.code = -code;
  968. read_lock(&nl_table_lock);
  969. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  970. ret += do_one_set_err(sk, &info);
  971. read_unlock(&nl_table_lock);
  972. return ret;
  973. }
  974. EXPORT_SYMBOL(netlink_set_err);
  975. /* must be called with netlink table grabbed */
  976. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  977. unsigned int group,
  978. int is_new)
  979. {
  980. int old, new = !!is_new, subscriptions;
  981. old = test_bit(group - 1, nlk->groups);
  982. subscriptions = nlk->subscriptions - old + new;
  983. if (new)
  984. __set_bit(group - 1, nlk->groups);
  985. else
  986. __clear_bit(group - 1, nlk->groups);
  987. netlink_update_subscriptions(&nlk->sk, subscriptions);
  988. netlink_update_listeners(&nlk->sk);
  989. }
  990. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  991. char __user *optval, unsigned int optlen)
  992. {
  993. struct sock *sk = sock->sk;
  994. struct netlink_sock *nlk = nlk_sk(sk);
  995. unsigned int val = 0;
  996. int err;
  997. if (level != SOL_NETLINK)
  998. return -ENOPROTOOPT;
  999. if (optlen >= sizeof(int) &&
  1000. get_user(val, (unsigned int __user *)optval))
  1001. return -EFAULT;
  1002. switch (optname) {
  1003. case NETLINK_PKTINFO:
  1004. if (val)
  1005. nlk->flags |= NETLINK_RECV_PKTINFO;
  1006. else
  1007. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1008. err = 0;
  1009. break;
  1010. case NETLINK_ADD_MEMBERSHIP:
  1011. case NETLINK_DROP_MEMBERSHIP: {
  1012. if (!netlink_capable(sock, NL_NONROOT_RECV))
  1013. return -EPERM;
  1014. err = netlink_realloc_groups(sk);
  1015. if (err)
  1016. return err;
  1017. if (!val || val - 1 >= nlk->ngroups)
  1018. return -EINVAL;
  1019. netlink_table_grab();
  1020. netlink_update_socket_mc(nlk, val,
  1021. optname == NETLINK_ADD_MEMBERSHIP);
  1022. netlink_table_ungrab();
  1023. err = 0;
  1024. break;
  1025. }
  1026. case NETLINK_BROADCAST_ERROR:
  1027. if (val)
  1028. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1029. else
  1030. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1031. err = 0;
  1032. break;
  1033. case NETLINK_NO_ENOBUFS:
  1034. if (val) {
  1035. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1036. clear_bit(0, &nlk->state);
  1037. wake_up_interruptible(&nlk->wait);
  1038. } else
  1039. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1040. err = 0;
  1041. break;
  1042. default:
  1043. err = -ENOPROTOOPT;
  1044. }
  1045. return err;
  1046. }
  1047. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1048. char __user *optval, int __user *optlen)
  1049. {
  1050. struct sock *sk = sock->sk;
  1051. struct netlink_sock *nlk = nlk_sk(sk);
  1052. int len, val, err;
  1053. if (level != SOL_NETLINK)
  1054. return -ENOPROTOOPT;
  1055. if (get_user(len, optlen))
  1056. return -EFAULT;
  1057. if (len < 0)
  1058. return -EINVAL;
  1059. switch (optname) {
  1060. case NETLINK_PKTINFO:
  1061. if (len < sizeof(int))
  1062. return -EINVAL;
  1063. len = sizeof(int);
  1064. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1065. if (put_user(len, optlen) ||
  1066. put_user(val, optval))
  1067. return -EFAULT;
  1068. err = 0;
  1069. break;
  1070. case NETLINK_BROADCAST_ERROR:
  1071. if (len < sizeof(int))
  1072. return -EINVAL;
  1073. len = sizeof(int);
  1074. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1075. if (put_user(len, optlen) ||
  1076. put_user(val, optval))
  1077. return -EFAULT;
  1078. err = 0;
  1079. break;
  1080. case NETLINK_NO_ENOBUFS:
  1081. if (len < sizeof(int))
  1082. return -EINVAL;
  1083. len = sizeof(int);
  1084. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1085. if (put_user(len, optlen) ||
  1086. put_user(val, optval))
  1087. return -EFAULT;
  1088. err = 0;
  1089. break;
  1090. default:
  1091. err = -ENOPROTOOPT;
  1092. }
  1093. return err;
  1094. }
  1095. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1096. {
  1097. struct nl_pktinfo info;
  1098. info.group = NETLINK_CB(skb).dst_group;
  1099. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1100. }
  1101. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1102. struct msghdr *msg, size_t len)
  1103. {
  1104. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1105. struct sock *sk = sock->sk;
  1106. struct netlink_sock *nlk = nlk_sk(sk);
  1107. struct sockaddr_nl *addr = msg->msg_name;
  1108. u32 dst_pid;
  1109. u32 dst_group;
  1110. struct sk_buff *skb;
  1111. int err;
  1112. struct scm_cookie scm;
  1113. if (msg->msg_flags&MSG_OOB)
  1114. return -EOPNOTSUPP;
  1115. if (NULL == siocb->scm) {
  1116. siocb->scm = &scm;
  1117. memset(&scm, 0, sizeof(scm));
  1118. }
  1119. err = scm_send(sock, msg, siocb->scm);
  1120. if (err < 0)
  1121. return err;
  1122. if (msg->msg_namelen) {
  1123. err = -EINVAL;
  1124. if (addr->nl_family != AF_NETLINK)
  1125. goto out;
  1126. dst_pid = addr->nl_pid;
  1127. dst_group = ffs(addr->nl_groups);
  1128. err = -EPERM;
  1129. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1130. goto out;
  1131. } else {
  1132. dst_pid = nlk->dst_pid;
  1133. dst_group = nlk->dst_group;
  1134. }
  1135. if (!nlk->pid) {
  1136. err = netlink_autobind(sock);
  1137. if (err)
  1138. goto out;
  1139. }
  1140. err = -EMSGSIZE;
  1141. if (len > sk->sk_sndbuf - 32)
  1142. goto out;
  1143. err = -ENOBUFS;
  1144. skb = alloc_skb(len, GFP_KERNEL);
  1145. if (skb == NULL)
  1146. goto out;
  1147. NETLINK_CB(skb).pid = nlk->pid;
  1148. NETLINK_CB(skb).dst_group = dst_group;
  1149. NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
  1150. NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
  1151. security_task_getsecid(current, &(NETLINK_CB(skb).sid));
  1152. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1153. /* What can I do? Netlink is asynchronous, so that
  1154. we will have to save current capabilities to
  1155. check them, when this message will be delivered
  1156. to corresponding kernel module. --ANK (980802)
  1157. */
  1158. err = -EFAULT;
  1159. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1160. kfree_skb(skb);
  1161. goto out;
  1162. }
  1163. err = security_netlink_send(sk, skb);
  1164. if (err) {
  1165. kfree_skb(skb);
  1166. goto out;
  1167. }
  1168. if (dst_group) {
  1169. atomic_inc(&skb->users);
  1170. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1171. }
  1172. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1173. out:
  1174. scm_destroy(siocb->scm);
  1175. return err;
  1176. }
  1177. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1178. struct msghdr *msg, size_t len,
  1179. int flags)
  1180. {
  1181. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1182. struct scm_cookie scm;
  1183. struct sock *sk = sock->sk;
  1184. struct netlink_sock *nlk = nlk_sk(sk);
  1185. int noblock = flags&MSG_DONTWAIT;
  1186. size_t copied;
  1187. struct sk_buff *skb, *frag __maybe_unused = NULL;
  1188. int err;
  1189. if (flags&MSG_OOB)
  1190. return -EOPNOTSUPP;
  1191. copied = 0;
  1192. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1193. if (skb == NULL)
  1194. goto out;
  1195. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1196. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1197. bool need_compat = !!(flags & MSG_CMSG_COMPAT);
  1198. /*
  1199. * If this skb has a frag_list, then here that means that
  1200. * we will have to use the frag_list skb for compat tasks
  1201. * and the regular skb for non-compat tasks.
  1202. *
  1203. * The skb might (and likely will) be cloned, so we can't
  1204. * just reset frag_list and go on with things -- we need to
  1205. * keep that. For the compat case that's easy -- simply get
  1206. * a reference to the compat skb and free the regular one
  1207. * including the frag. For the non-compat case, we need to
  1208. * avoid sending the frag to the user -- so assign NULL but
  1209. * restore it below before freeing the skb.
  1210. */
  1211. if (need_compat) {
  1212. struct sk_buff *compskb = skb_shinfo(skb)->frag_list;
  1213. skb_get(compskb);
  1214. kfree_skb(skb);
  1215. skb = compskb;
  1216. } else {
  1217. frag = skb_shinfo(skb)->frag_list;
  1218. skb_shinfo(skb)->frag_list = NULL;
  1219. }
  1220. }
  1221. #endif
  1222. msg->msg_namelen = 0;
  1223. copied = skb->len;
  1224. if (len < copied) {
  1225. msg->msg_flags |= MSG_TRUNC;
  1226. copied = len;
  1227. }
  1228. skb_reset_transport_header(skb);
  1229. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1230. if (msg->msg_name) {
  1231. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1232. addr->nl_family = AF_NETLINK;
  1233. addr->nl_pad = 0;
  1234. addr->nl_pid = NETLINK_CB(skb).pid;
  1235. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1236. msg->msg_namelen = sizeof(*addr);
  1237. }
  1238. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1239. netlink_cmsg_recv_pktinfo(msg, skb);
  1240. if (NULL == siocb->scm) {
  1241. memset(&scm, 0, sizeof(scm));
  1242. siocb->scm = &scm;
  1243. }
  1244. siocb->scm->creds = *NETLINK_CREDS(skb);
  1245. if (flags & MSG_TRUNC)
  1246. copied = skb->len;
  1247. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1248. skb_shinfo(skb)->frag_list = frag;
  1249. #endif
  1250. skb_free_datagram(sk, skb);
  1251. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1252. netlink_dump(sk);
  1253. scm_recv(sock, msg, siocb->scm, flags);
  1254. out:
  1255. netlink_rcv_wake(sk);
  1256. return err ? : copied;
  1257. }
  1258. static void netlink_data_ready(struct sock *sk, int len)
  1259. {
  1260. BUG();
  1261. }
  1262. /*
  1263. * We export these functions to other modules. They provide a
  1264. * complete set of kernel non-blocking support for message
  1265. * queueing.
  1266. */
  1267. struct sock *
  1268. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1269. void (*input)(struct sk_buff *skb),
  1270. struct mutex *cb_mutex, struct module *module)
  1271. {
  1272. struct socket *sock;
  1273. struct sock *sk;
  1274. struct netlink_sock *nlk;
  1275. unsigned long *listeners = NULL;
  1276. BUG_ON(!nl_table);
  1277. if (unit < 0 || unit >= MAX_LINKS)
  1278. return NULL;
  1279. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1280. return NULL;
  1281. /*
  1282. * We have to just have a reference on the net from sk, but don't
  1283. * get_net it. Besides, we cannot get and then put the net here.
  1284. * So we create one inside init_net and the move it to net.
  1285. */
  1286. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1287. goto out_sock_release_nosk;
  1288. sk = sock->sk;
  1289. sk_change_net(sk, net);
  1290. if (groups < 32)
  1291. groups = 32;
  1292. listeners = kzalloc(NLGRPSZ(groups) + sizeof(struct listeners_rcu_head),
  1293. GFP_KERNEL);
  1294. if (!listeners)
  1295. goto out_sock_release;
  1296. sk->sk_data_ready = netlink_data_ready;
  1297. if (input)
  1298. nlk_sk(sk)->netlink_rcv = input;
  1299. if (netlink_insert(sk, net, 0))
  1300. goto out_sock_release;
  1301. nlk = nlk_sk(sk);
  1302. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1303. netlink_table_grab();
  1304. if (!nl_table[unit].registered) {
  1305. nl_table[unit].groups = groups;
  1306. nl_table[unit].listeners = listeners;
  1307. nl_table[unit].cb_mutex = cb_mutex;
  1308. nl_table[unit].module = module;
  1309. nl_table[unit].registered = 1;
  1310. } else {
  1311. kfree(listeners);
  1312. nl_table[unit].registered++;
  1313. }
  1314. netlink_table_ungrab();
  1315. return sk;
  1316. out_sock_release:
  1317. kfree(listeners);
  1318. netlink_kernel_release(sk);
  1319. return NULL;
  1320. out_sock_release_nosk:
  1321. sock_release(sock);
  1322. return NULL;
  1323. }
  1324. EXPORT_SYMBOL(netlink_kernel_create);
  1325. void
  1326. netlink_kernel_release(struct sock *sk)
  1327. {
  1328. sk_release_kernel(sk);
  1329. }
  1330. EXPORT_SYMBOL(netlink_kernel_release);
  1331. static void netlink_free_old_listeners(struct rcu_head *rcu_head)
  1332. {
  1333. struct listeners_rcu_head *lrh;
  1334. lrh = container_of(rcu_head, struct listeners_rcu_head, rcu_head);
  1335. kfree(lrh->ptr);
  1336. }
  1337. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1338. {
  1339. unsigned long *listeners, *old = NULL;
  1340. struct listeners_rcu_head *old_rcu_head;
  1341. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1342. if (groups < 32)
  1343. groups = 32;
  1344. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1345. listeners = kzalloc(NLGRPSZ(groups) +
  1346. sizeof(struct listeners_rcu_head),
  1347. GFP_ATOMIC);
  1348. if (!listeners)
  1349. return -ENOMEM;
  1350. old = tbl->listeners;
  1351. memcpy(listeners, old, NLGRPSZ(tbl->groups));
  1352. rcu_assign_pointer(tbl->listeners, listeners);
  1353. /*
  1354. * Free the old memory after an RCU grace period so we
  1355. * don't leak it. We use call_rcu() here in order to be
  1356. * able to call this function from atomic contexts. The
  1357. * allocation of this memory will have reserved enough
  1358. * space for struct listeners_rcu_head at the end.
  1359. */
  1360. old_rcu_head = (void *)(tbl->listeners +
  1361. NLGRPLONGS(tbl->groups));
  1362. old_rcu_head->ptr = old;
  1363. call_rcu(&old_rcu_head->rcu_head, netlink_free_old_listeners);
  1364. }
  1365. tbl->groups = groups;
  1366. return 0;
  1367. }
  1368. /**
  1369. * netlink_change_ngroups - change number of multicast groups
  1370. *
  1371. * This changes the number of multicast groups that are available
  1372. * on a certain netlink family. Note that it is not possible to
  1373. * change the number of groups to below 32. Also note that it does
  1374. * not implicitly call netlink_clear_multicast_users() when the
  1375. * number of groups is reduced.
  1376. *
  1377. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1378. * @groups: The new number of groups.
  1379. */
  1380. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1381. {
  1382. int err;
  1383. netlink_table_grab();
  1384. err = __netlink_change_ngroups(sk, groups);
  1385. netlink_table_ungrab();
  1386. return err;
  1387. }
  1388. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1389. {
  1390. struct sock *sk;
  1391. struct hlist_node *node;
  1392. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1393. sk_for_each_bound(sk, node, &tbl->mc_list)
  1394. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1395. }
  1396. /**
  1397. * netlink_clear_multicast_users - kick off multicast listeners
  1398. *
  1399. * This function removes all listeners from the given group.
  1400. * @ksk: The kernel netlink socket, as returned by
  1401. * netlink_kernel_create().
  1402. * @group: The multicast group to clear.
  1403. */
  1404. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1405. {
  1406. netlink_table_grab();
  1407. __netlink_clear_multicast_users(ksk, group);
  1408. netlink_table_ungrab();
  1409. }
  1410. void netlink_set_nonroot(int protocol, unsigned int flags)
  1411. {
  1412. if ((unsigned int)protocol < MAX_LINKS)
  1413. nl_table[protocol].nl_nonroot = flags;
  1414. }
  1415. EXPORT_SYMBOL(netlink_set_nonroot);
  1416. static void netlink_destroy_callback(struct netlink_callback *cb)
  1417. {
  1418. kfree_skb(cb->skb);
  1419. kfree(cb);
  1420. }
  1421. /*
  1422. * It looks a bit ugly.
  1423. * It would be better to create kernel thread.
  1424. */
  1425. static int netlink_dump(struct sock *sk)
  1426. {
  1427. struct netlink_sock *nlk = nlk_sk(sk);
  1428. struct netlink_callback *cb;
  1429. struct sk_buff *skb;
  1430. struct nlmsghdr *nlh;
  1431. int len, err = -ENOBUFS;
  1432. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1433. if (!skb)
  1434. goto errout;
  1435. mutex_lock(nlk->cb_mutex);
  1436. cb = nlk->cb;
  1437. if (cb == NULL) {
  1438. err = -EINVAL;
  1439. goto errout_skb;
  1440. }
  1441. len = cb->dump(skb, cb);
  1442. if (len > 0) {
  1443. mutex_unlock(nlk->cb_mutex);
  1444. if (sk_filter(sk, skb))
  1445. kfree_skb(skb);
  1446. else {
  1447. skb_queue_tail(&sk->sk_receive_queue, skb);
  1448. sk->sk_data_ready(sk, skb->len);
  1449. }
  1450. return 0;
  1451. }
  1452. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1453. if (!nlh)
  1454. goto errout_skb;
  1455. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1456. if (sk_filter(sk, skb))
  1457. kfree_skb(skb);
  1458. else {
  1459. skb_queue_tail(&sk->sk_receive_queue, skb);
  1460. sk->sk_data_ready(sk, skb->len);
  1461. }
  1462. if (cb->done)
  1463. cb->done(cb);
  1464. nlk->cb = NULL;
  1465. mutex_unlock(nlk->cb_mutex);
  1466. netlink_destroy_callback(cb);
  1467. return 0;
  1468. errout_skb:
  1469. mutex_unlock(nlk->cb_mutex);
  1470. kfree_skb(skb);
  1471. errout:
  1472. return err;
  1473. }
  1474. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1475. const struct nlmsghdr *nlh,
  1476. int (*dump)(struct sk_buff *skb,
  1477. struct netlink_callback *),
  1478. int (*done)(struct netlink_callback *))
  1479. {
  1480. struct netlink_callback *cb;
  1481. struct sock *sk;
  1482. struct netlink_sock *nlk;
  1483. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1484. if (cb == NULL)
  1485. return -ENOBUFS;
  1486. cb->dump = dump;
  1487. cb->done = done;
  1488. cb->nlh = nlh;
  1489. atomic_inc(&skb->users);
  1490. cb->skb = skb;
  1491. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1492. if (sk == NULL) {
  1493. netlink_destroy_callback(cb);
  1494. return -ECONNREFUSED;
  1495. }
  1496. nlk = nlk_sk(sk);
  1497. /* A dump is in progress... */
  1498. mutex_lock(nlk->cb_mutex);
  1499. if (nlk->cb) {
  1500. mutex_unlock(nlk->cb_mutex);
  1501. netlink_destroy_callback(cb);
  1502. sock_put(sk);
  1503. return -EBUSY;
  1504. }
  1505. nlk->cb = cb;
  1506. mutex_unlock(nlk->cb_mutex);
  1507. netlink_dump(sk);
  1508. sock_put(sk);
  1509. /* We successfully started a dump, by returning -EINTR we
  1510. * signal not to send ACK even if it was requested.
  1511. */
  1512. return -EINTR;
  1513. }
  1514. EXPORT_SYMBOL(netlink_dump_start);
  1515. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1516. {
  1517. struct sk_buff *skb;
  1518. struct nlmsghdr *rep;
  1519. struct nlmsgerr *errmsg;
  1520. size_t payload = sizeof(*errmsg);
  1521. /* error messages get the original request appened */
  1522. if (err)
  1523. payload += nlmsg_len(nlh);
  1524. skb = nlmsg_new(payload, GFP_KERNEL);
  1525. if (!skb) {
  1526. struct sock *sk;
  1527. sk = netlink_lookup(sock_net(in_skb->sk),
  1528. in_skb->sk->sk_protocol,
  1529. NETLINK_CB(in_skb).pid);
  1530. if (sk) {
  1531. sk->sk_err = ENOBUFS;
  1532. sk->sk_error_report(sk);
  1533. sock_put(sk);
  1534. }
  1535. return;
  1536. }
  1537. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1538. NLMSG_ERROR, payload, 0);
  1539. errmsg = nlmsg_data(rep);
  1540. errmsg->error = err;
  1541. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1542. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1543. }
  1544. EXPORT_SYMBOL(netlink_ack);
  1545. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1546. struct nlmsghdr *))
  1547. {
  1548. struct nlmsghdr *nlh;
  1549. int err;
  1550. while (skb->len >= nlmsg_total_size(0)) {
  1551. int msglen;
  1552. nlh = nlmsg_hdr(skb);
  1553. err = 0;
  1554. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1555. return 0;
  1556. /* Only requests are handled by the kernel */
  1557. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1558. goto ack;
  1559. /* Skip control messages */
  1560. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1561. goto ack;
  1562. err = cb(skb, nlh);
  1563. if (err == -EINTR)
  1564. goto skip;
  1565. ack:
  1566. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1567. netlink_ack(skb, nlh, err);
  1568. skip:
  1569. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1570. if (msglen > skb->len)
  1571. msglen = skb->len;
  1572. skb_pull(skb, msglen);
  1573. }
  1574. return 0;
  1575. }
  1576. EXPORT_SYMBOL(netlink_rcv_skb);
  1577. /**
  1578. * nlmsg_notify - send a notification netlink message
  1579. * @sk: netlink socket to use
  1580. * @skb: notification message
  1581. * @pid: destination netlink pid for reports or 0
  1582. * @group: destination multicast group or 0
  1583. * @report: 1 to report back, 0 to disable
  1584. * @flags: allocation flags
  1585. */
  1586. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1587. unsigned int group, int report, gfp_t flags)
  1588. {
  1589. int err = 0;
  1590. if (group) {
  1591. int exclude_pid = 0;
  1592. if (report) {
  1593. atomic_inc(&skb->users);
  1594. exclude_pid = pid;
  1595. }
  1596. /* errors reported via destination sk->sk_err, but propagate
  1597. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1598. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1599. }
  1600. if (report) {
  1601. int err2;
  1602. err2 = nlmsg_unicast(sk, skb, pid);
  1603. if (!err || err == -ESRCH)
  1604. err = err2;
  1605. }
  1606. return err;
  1607. }
  1608. EXPORT_SYMBOL(nlmsg_notify);
  1609. #ifdef CONFIG_PROC_FS
  1610. struct nl_seq_iter {
  1611. struct seq_net_private p;
  1612. int link;
  1613. int hash_idx;
  1614. };
  1615. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1616. {
  1617. struct nl_seq_iter *iter = seq->private;
  1618. int i, j;
  1619. struct sock *s;
  1620. struct hlist_node *node;
  1621. loff_t off = 0;
  1622. for (i = 0; i < MAX_LINKS; i++) {
  1623. struct nl_pid_hash *hash = &nl_table[i].hash;
  1624. for (j = 0; j <= hash->mask; j++) {
  1625. sk_for_each(s, node, &hash->table[j]) {
  1626. if (sock_net(s) != seq_file_net(seq))
  1627. continue;
  1628. if (off == pos) {
  1629. iter->link = i;
  1630. iter->hash_idx = j;
  1631. return s;
  1632. }
  1633. ++off;
  1634. }
  1635. }
  1636. }
  1637. return NULL;
  1638. }
  1639. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1640. __acquires(nl_table_lock)
  1641. {
  1642. read_lock(&nl_table_lock);
  1643. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1644. }
  1645. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1646. {
  1647. struct sock *s;
  1648. struct nl_seq_iter *iter;
  1649. int i, j;
  1650. ++*pos;
  1651. if (v == SEQ_START_TOKEN)
  1652. return netlink_seq_socket_idx(seq, 0);
  1653. iter = seq->private;
  1654. s = v;
  1655. do {
  1656. s = sk_next(s);
  1657. } while (s && sock_net(s) != seq_file_net(seq));
  1658. if (s)
  1659. return s;
  1660. i = iter->link;
  1661. j = iter->hash_idx + 1;
  1662. do {
  1663. struct nl_pid_hash *hash = &nl_table[i].hash;
  1664. for (; j <= hash->mask; j++) {
  1665. s = sk_head(&hash->table[j]);
  1666. while (s && sock_net(s) != seq_file_net(seq))
  1667. s = sk_next(s);
  1668. if (s) {
  1669. iter->link = i;
  1670. iter->hash_idx = j;
  1671. return s;
  1672. }
  1673. }
  1674. j = 0;
  1675. } while (++i < MAX_LINKS);
  1676. return NULL;
  1677. }
  1678. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1679. __releases(nl_table_lock)
  1680. {
  1681. read_unlock(&nl_table_lock);
  1682. }
  1683. static int netlink_seq_show(struct seq_file *seq, void *v)
  1684. {
  1685. if (v == SEQ_START_TOKEN)
  1686. seq_puts(seq,
  1687. "sk Eth Pid Groups "
  1688. "Rmem Wmem Dump Locks Drops Inode\n");
  1689. else {
  1690. struct sock *s = v;
  1691. struct netlink_sock *nlk = nlk_sk(s);
  1692. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d %-8lu\n",
  1693. s,
  1694. s->sk_protocol,
  1695. nlk->pid,
  1696. nlk->groups ? (u32)nlk->groups[0] : 0,
  1697. sk_rmem_alloc_get(s),
  1698. sk_wmem_alloc_get(s),
  1699. nlk->cb,
  1700. atomic_read(&s->sk_refcnt),
  1701. atomic_read(&s->sk_drops),
  1702. sock_i_ino(s)
  1703. );
  1704. }
  1705. return 0;
  1706. }
  1707. static const struct seq_operations netlink_seq_ops = {
  1708. .start = netlink_seq_start,
  1709. .next = netlink_seq_next,
  1710. .stop = netlink_seq_stop,
  1711. .show = netlink_seq_show,
  1712. };
  1713. static int netlink_seq_open(struct inode *inode, struct file *file)
  1714. {
  1715. return seq_open_net(inode, file, &netlink_seq_ops,
  1716. sizeof(struct nl_seq_iter));
  1717. }
  1718. static const struct file_operations netlink_seq_fops = {
  1719. .owner = THIS_MODULE,
  1720. .open = netlink_seq_open,
  1721. .read = seq_read,
  1722. .llseek = seq_lseek,
  1723. .release = seq_release_net,
  1724. };
  1725. #endif
  1726. int netlink_register_notifier(struct notifier_block *nb)
  1727. {
  1728. return atomic_notifier_chain_register(&netlink_chain, nb);
  1729. }
  1730. EXPORT_SYMBOL(netlink_register_notifier);
  1731. int netlink_unregister_notifier(struct notifier_block *nb)
  1732. {
  1733. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1734. }
  1735. EXPORT_SYMBOL(netlink_unregister_notifier);
  1736. static const struct proto_ops netlink_ops = {
  1737. .family = PF_NETLINK,
  1738. .owner = THIS_MODULE,
  1739. .release = netlink_release,
  1740. .bind = netlink_bind,
  1741. .connect = netlink_connect,
  1742. .socketpair = sock_no_socketpair,
  1743. .accept = sock_no_accept,
  1744. .getname = netlink_getname,
  1745. .poll = datagram_poll,
  1746. .ioctl = sock_no_ioctl,
  1747. .listen = sock_no_listen,
  1748. .shutdown = sock_no_shutdown,
  1749. .setsockopt = netlink_setsockopt,
  1750. .getsockopt = netlink_getsockopt,
  1751. .sendmsg = netlink_sendmsg,
  1752. .recvmsg = netlink_recvmsg,
  1753. .mmap = sock_no_mmap,
  1754. .sendpage = sock_no_sendpage,
  1755. };
  1756. static const struct net_proto_family netlink_family_ops = {
  1757. .family = PF_NETLINK,
  1758. .create = netlink_create,
  1759. .owner = THIS_MODULE, /* for consistency 8) */
  1760. };
  1761. static int __net_init netlink_net_init(struct net *net)
  1762. {
  1763. #ifdef CONFIG_PROC_FS
  1764. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1765. return -ENOMEM;
  1766. #endif
  1767. return 0;
  1768. }
  1769. static void __net_exit netlink_net_exit(struct net *net)
  1770. {
  1771. #ifdef CONFIG_PROC_FS
  1772. proc_net_remove(net, "netlink");
  1773. #endif
  1774. }
  1775. static struct pernet_operations __net_initdata netlink_net_ops = {
  1776. .init = netlink_net_init,
  1777. .exit = netlink_net_exit,
  1778. };
  1779. static int __init netlink_proto_init(void)
  1780. {
  1781. struct sk_buff *dummy_skb;
  1782. int i;
  1783. unsigned long limit;
  1784. unsigned int order;
  1785. int err = proto_register(&netlink_proto, 0);
  1786. if (err != 0)
  1787. goto out;
  1788. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1789. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1790. if (!nl_table)
  1791. goto panic;
  1792. if (totalram_pages >= (128 * 1024))
  1793. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1794. else
  1795. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1796. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1797. limit = (1UL << order) / sizeof(struct hlist_head);
  1798. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1799. for (i = 0; i < MAX_LINKS; i++) {
  1800. struct nl_pid_hash *hash = &nl_table[i].hash;
  1801. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1802. if (!hash->table) {
  1803. while (i-- > 0)
  1804. nl_pid_hash_free(nl_table[i].hash.table,
  1805. 1 * sizeof(*hash->table));
  1806. kfree(nl_table);
  1807. goto panic;
  1808. }
  1809. hash->max_shift = order;
  1810. hash->shift = 0;
  1811. hash->mask = 0;
  1812. hash->rehash_time = jiffies;
  1813. }
  1814. sock_register(&netlink_family_ops);
  1815. register_pernet_subsys(&netlink_net_ops);
  1816. /* The netlink device handler may be needed early. */
  1817. rtnetlink_init();
  1818. out:
  1819. return err;
  1820. panic:
  1821. panic("netlink_init: Cannot allocate nl_table\n");
  1822. }
  1823. core_initcall(netlink_proto_init);