af_netlink.c 49 KB

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