af_netlink.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  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 = current->tgid;
  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 (addr->sa_family == AF_UNSPEC) {
  574. sk->sk_state = NETLINK_UNCONNECTED;
  575. nlk->dst_pid = 0;
  576. nlk->dst_group = 0;
  577. return 0;
  578. }
  579. if (addr->sa_family != AF_NETLINK)
  580. return -EINVAL;
  581. /* Only superuser is allowed to send multicasts */
  582. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  583. return -EPERM;
  584. if (!nlk->pid)
  585. err = netlink_autobind(sock);
  586. if (err == 0) {
  587. sk->sk_state = NETLINK_CONNECTED;
  588. nlk->dst_pid = nladdr->nl_pid;
  589. nlk->dst_group = ffs(nladdr->nl_groups);
  590. }
  591. return err;
  592. }
  593. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  594. int *addr_len, int peer)
  595. {
  596. struct sock *sk = sock->sk;
  597. struct netlink_sock *nlk = nlk_sk(sk);
  598. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  599. nladdr->nl_family = AF_NETLINK;
  600. nladdr->nl_pad = 0;
  601. *addr_len = sizeof(*nladdr);
  602. if (peer) {
  603. nladdr->nl_pid = nlk->dst_pid;
  604. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  605. } else {
  606. nladdr->nl_pid = nlk->pid;
  607. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  608. }
  609. return 0;
  610. }
  611. static void netlink_overrun(struct sock *sk)
  612. {
  613. struct netlink_sock *nlk = nlk_sk(sk);
  614. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  615. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  616. sk->sk_err = ENOBUFS;
  617. sk->sk_error_report(sk);
  618. }
  619. }
  620. atomic_inc(&sk->sk_drops);
  621. }
  622. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  623. {
  624. struct sock *sock;
  625. struct netlink_sock *nlk;
  626. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
  627. if (!sock)
  628. return ERR_PTR(-ECONNREFUSED);
  629. /* Don't bother queuing skb if kernel socket has no input function */
  630. nlk = nlk_sk(sock);
  631. if (sock->sk_state == NETLINK_CONNECTED &&
  632. nlk->dst_pid != nlk_sk(ssk)->pid) {
  633. sock_put(sock);
  634. return ERR_PTR(-ECONNREFUSED);
  635. }
  636. return sock;
  637. }
  638. struct sock *netlink_getsockbyfilp(struct file *filp)
  639. {
  640. struct inode *inode = filp->f_path.dentry->d_inode;
  641. struct sock *sock;
  642. if (!S_ISSOCK(inode->i_mode))
  643. return ERR_PTR(-ENOTSOCK);
  644. sock = SOCKET_I(inode)->sk;
  645. if (sock->sk_family != AF_NETLINK)
  646. return ERR_PTR(-EINVAL);
  647. sock_hold(sock);
  648. return sock;
  649. }
  650. /*
  651. * Attach a skb to a netlink socket.
  652. * The caller must hold a reference to the destination socket. On error, the
  653. * reference is dropped. The skb is not send to the destination, just all
  654. * all error checks are performed and memory in the queue is reserved.
  655. * Return values:
  656. * < 0: error. skb freed, reference to sock dropped.
  657. * 0: continue
  658. * 1: repeat lookup - reference dropped while waiting for socket memory.
  659. */
  660. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  661. long *timeo, struct sock *ssk)
  662. {
  663. struct netlink_sock *nlk;
  664. nlk = nlk_sk(sk);
  665. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  666. test_bit(0, &nlk->state)) {
  667. DECLARE_WAITQUEUE(wait, current);
  668. if (!*timeo) {
  669. if (!ssk || netlink_is_kernel(ssk))
  670. netlink_overrun(sk);
  671. sock_put(sk);
  672. kfree_skb(skb);
  673. return -EAGAIN;
  674. }
  675. __set_current_state(TASK_INTERRUPTIBLE);
  676. add_wait_queue(&nlk->wait, &wait);
  677. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  678. test_bit(0, &nlk->state)) &&
  679. !sock_flag(sk, SOCK_DEAD))
  680. *timeo = schedule_timeout(*timeo);
  681. __set_current_state(TASK_RUNNING);
  682. remove_wait_queue(&nlk->wait, &wait);
  683. sock_put(sk);
  684. if (signal_pending(current)) {
  685. kfree_skb(skb);
  686. return sock_intr_errno(*timeo);
  687. }
  688. return 1;
  689. }
  690. skb_set_owner_r(skb, sk);
  691. return 0;
  692. }
  693. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  694. {
  695. int len = skb->len;
  696. skb_queue_tail(&sk->sk_receive_queue, skb);
  697. sk->sk_data_ready(sk, len);
  698. sock_put(sk);
  699. return len;
  700. }
  701. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  702. {
  703. kfree_skb(skb);
  704. sock_put(sk);
  705. }
  706. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  707. gfp_t allocation)
  708. {
  709. int delta;
  710. skb_orphan(skb);
  711. delta = skb->end - skb->tail;
  712. if (delta * 2 < skb->truesize)
  713. return skb;
  714. if (skb_shared(skb)) {
  715. struct sk_buff *nskb = skb_clone(skb, allocation);
  716. if (!nskb)
  717. return skb;
  718. kfree_skb(skb);
  719. skb = nskb;
  720. }
  721. if (!pskb_expand_head(skb, 0, -delta, allocation))
  722. skb->truesize -= delta;
  723. return skb;
  724. }
  725. static inline void netlink_rcv_wake(struct sock *sk)
  726. {
  727. struct netlink_sock *nlk = nlk_sk(sk);
  728. if (skb_queue_empty(&sk->sk_receive_queue))
  729. clear_bit(0, &nlk->state);
  730. if (!test_bit(0, &nlk->state))
  731. wake_up_interruptible(&nlk->wait);
  732. }
  733. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  734. {
  735. int ret;
  736. struct netlink_sock *nlk = nlk_sk(sk);
  737. ret = -ECONNREFUSED;
  738. if (nlk->netlink_rcv != NULL) {
  739. ret = skb->len;
  740. skb_set_owner_r(skb, sk);
  741. nlk->netlink_rcv(skb);
  742. }
  743. kfree_skb(skb);
  744. sock_put(sk);
  745. return ret;
  746. }
  747. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  748. u32 pid, int nonblock)
  749. {
  750. struct sock *sk;
  751. int err;
  752. long timeo;
  753. skb = netlink_trim(skb, gfp_any());
  754. timeo = sock_sndtimeo(ssk, nonblock);
  755. retry:
  756. sk = netlink_getsockbypid(ssk, pid);
  757. if (IS_ERR(sk)) {
  758. kfree_skb(skb);
  759. return PTR_ERR(sk);
  760. }
  761. if (netlink_is_kernel(sk))
  762. return netlink_unicast_kernel(sk, skb);
  763. if (sk_filter(sk, skb)) {
  764. err = skb->len;
  765. kfree_skb(skb);
  766. sock_put(sk);
  767. return err;
  768. }
  769. err = netlink_attachskb(sk, skb, &timeo, ssk);
  770. if (err == 1)
  771. goto retry;
  772. if (err)
  773. return err;
  774. return netlink_sendskb(sk, skb);
  775. }
  776. EXPORT_SYMBOL(netlink_unicast);
  777. int netlink_has_listeners(struct sock *sk, unsigned int group)
  778. {
  779. int res = 0;
  780. unsigned long *listeners;
  781. BUG_ON(!netlink_is_kernel(sk));
  782. rcu_read_lock();
  783. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  784. if (group - 1 < nl_table[sk->sk_protocol].groups)
  785. res = test_bit(group - 1, listeners);
  786. rcu_read_unlock();
  787. return res;
  788. }
  789. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  790. static inline int netlink_broadcast_deliver(struct sock *sk,
  791. struct sk_buff *skb)
  792. {
  793. struct netlink_sock *nlk = nlk_sk(sk);
  794. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  795. !test_bit(0, &nlk->state)) {
  796. skb_set_owner_r(skb, sk);
  797. skb_queue_tail(&sk->sk_receive_queue, skb);
  798. sk->sk_data_ready(sk, skb->len);
  799. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  800. }
  801. return -1;
  802. }
  803. struct netlink_broadcast_data {
  804. struct sock *exclude_sk;
  805. struct net *net;
  806. u32 pid;
  807. u32 group;
  808. int failure;
  809. int delivery_failure;
  810. int congested;
  811. int delivered;
  812. gfp_t allocation;
  813. struct sk_buff *skb, *skb2;
  814. };
  815. static inline int do_one_broadcast(struct sock *sk,
  816. struct netlink_broadcast_data *p)
  817. {
  818. struct netlink_sock *nlk = nlk_sk(sk);
  819. int val;
  820. if (p->exclude_sk == sk)
  821. goto out;
  822. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  823. !test_bit(p->group - 1, nlk->groups))
  824. goto out;
  825. if (!net_eq(sock_net(sk), p->net))
  826. goto out;
  827. if (p->failure) {
  828. netlink_overrun(sk);
  829. goto out;
  830. }
  831. sock_hold(sk);
  832. if (p->skb2 == NULL) {
  833. if (skb_shared(p->skb)) {
  834. p->skb2 = skb_clone(p->skb, p->allocation);
  835. } else {
  836. p->skb2 = skb_get(p->skb);
  837. /*
  838. * skb ownership may have been set when
  839. * delivered to a previous socket.
  840. */
  841. skb_orphan(p->skb2);
  842. }
  843. }
  844. if (p->skb2 == NULL) {
  845. netlink_overrun(sk);
  846. /* Clone failed. Notify ALL listeners. */
  847. p->failure = 1;
  848. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  849. p->delivery_failure = 1;
  850. } else if (sk_filter(sk, p->skb2)) {
  851. kfree_skb(p->skb2);
  852. p->skb2 = NULL;
  853. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  854. netlink_overrun(sk);
  855. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  856. p->delivery_failure = 1;
  857. } else {
  858. p->congested |= val;
  859. p->delivered = 1;
  860. p->skb2 = NULL;
  861. }
  862. sock_put(sk);
  863. out:
  864. return 0;
  865. }
  866. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  867. u32 group, gfp_t allocation)
  868. {
  869. struct net *net = sock_net(ssk);
  870. struct netlink_broadcast_data info;
  871. struct hlist_node *node;
  872. struct sock *sk;
  873. skb = netlink_trim(skb, allocation);
  874. info.exclude_sk = ssk;
  875. info.net = net;
  876. info.pid = pid;
  877. info.group = group;
  878. info.failure = 0;
  879. info.delivery_failure = 0;
  880. info.congested = 0;
  881. info.delivered = 0;
  882. info.allocation = allocation;
  883. info.skb = skb;
  884. info.skb2 = NULL;
  885. /* While we sleep in clone, do not allow to change socket list */
  886. netlink_lock_table();
  887. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  888. do_one_broadcast(sk, &info);
  889. kfree_skb(skb);
  890. netlink_unlock_table();
  891. kfree_skb(info.skb2);
  892. if (info.delivery_failure)
  893. return -ENOBUFS;
  894. if (info.delivered) {
  895. if (info.congested && (allocation & __GFP_WAIT))
  896. yield();
  897. return 0;
  898. }
  899. return -ESRCH;
  900. }
  901. EXPORT_SYMBOL(netlink_broadcast);
  902. struct netlink_set_err_data {
  903. struct sock *exclude_sk;
  904. u32 pid;
  905. u32 group;
  906. int code;
  907. };
  908. static inline int do_one_set_err(struct sock *sk,
  909. struct netlink_set_err_data *p)
  910. {
  911. struct netlink_sock *nlk = nlk_sk(sk);
  912. if (sk == p->exclude_sk)
  913. goto out;
  914. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  915. goto out;
  916. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  917. !test_bit(p->group - 1, nlk->groups))
  918. goto out;
  919. sk->sk_err = p->code;
  920. sk->sk_error_report(sk);
  921. out:
  922. return 0;
  923. }
  924. /**
  925. * netlink_set_err - report error to broadcast listeners
  926. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  927. * @pid: the PID of a process that we want to skip (if any)
  928. * @groups: the broadcast group that will notice the error
  929. * @code: error code, must be negative (as usual in kernelspace)
  930. */
  931. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  932. {
  933. struct netlink_set_err_data info;
  934. struct hlist_node *node;
  935. struct sock *sk;
  936. info.exclude_sk = ssk;
  937. info.pid = pid;
  938. info.group = group;
  939. /* sk->sk_err wants a positive error value */
  940. info.code = -code;
  941. read_lock(&nl_table_lock);
  942. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  943. do_one_set_err(sk, &info);
  944. read_unlock(&nl_table_lock);
  945. }
  946. EXPORT_SYMBOL(netlink_set_err);
  947. /* must be called with netlink table grabbed */
  948. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  949. unsigned int group,
  950. int is_new)
  951. {
  952. int old, new = !!is_new, subscriptions;
  953. old = test_bit(group - 1, nlk->groups);
  954. subscriptions = nlk->subscriptions - old + new;
  955. if (new)
  956. __set_bit(group - 1, nlk->groups);
  957. else
  958. __clear_bit(group - 1, nlk->groups);
  959. netlink_update_subscriptions(&nlk->sk, subscriptions);
  960. netlink_update_listeners(&nlk->sk);
  961. }
  962. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  963. char __user *optval, unsigned int optlen)
  964. {
  965. struct sock *sk = sock->sk;
  966. struct netlink_sock *nlk = nlk_sk(sk);
  967. unsigned int val = 0;
  968. int err;
  969. if (level != SOL_NETLINK)
  970. return -ENOPROTOOPT;
  971. if (optlen >= sizeof(int) &&
  972. get_user(val, (unsigned int __user *)optval))
  973. return -EFAULT;
  974. switch (optname) {
  975. case NETLINK_PKTINFO:
  976. if (val)
  977. nlk->flags |= NETLINK_RECV_PKTINFO;
  978. else
  979. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  980. err = 0;
  981. break;
  982. case NETLINK_ADD_MEMBERSHIP:
  983. case NETLINK_DROP_MEMBERSHIP: {
  984. if (!netlink_capable(sock, NL_NONROOT_RECV))
  985. return -EPERM;
  986. err = netlink_realloc_groups(sk);
  987. if (err)
  988. return err;
  989. if (!val || val - 1 >= nlk->ngroups)
  990. return -EINVAL;
  991. netlink_table_grab();
  992. netlink_update_socket_mc(nlk, val,
  993. optname == NETLINK_ADD_MEMBERSHIP);
  994. netlink_table_ungrab();
  995. err = 0;
  996. break;
  997. }
  998. case NETLINK_BROADCAST_ERROR:
  999. if (val)
  1000. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1001. else
  1002. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1003. err = 0;
  1004. break;
  1005. case NETLINK_NO_ENOBUFS:
  1006. if (val) {
  1007. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1008. clear_bit(0, &nlk->state);
  1009. wake_up_interruptible(&nlk->wait);
  1010. } else
  1011. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1012. err = 0;
  1013. break;
  1014. default:
  1015. err = -ENOPROTOOPT;
  1016. }
  1017. return err;
  1018. }
  1019. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1020. char __user *optval, int __user *optlen)
  1021. {
  1022. struct sock *sk = sock->sk;
  1023. struct netlink_sock *nlk = nlk_sk(sk);
  1024. int len, val, err;
  1025. if (level != SOL_NETLINK)
  1026. return -ENOPROTOOPT;
  1027. if (get_user(len, optlen))
  1028. return -EFAULT;
  1029. if (len < 0)
  1030. return -EINVAL;
  1031. switch (optname) {
  1032. case NETLINK_PKTINFO:
  1033. if (len < sizeof(int))
  1034. return -EINVAL;
  1035. len = sizeof(int);
  1036. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1037. if (put_user(len, optlen) ||
  1038. put_user(val, optval))
  1039. return -EFAULT;
  1040. err = 0;
  1041. break;
  1042. case NETLINK_BROADCAST_ERROR:
  1043. if (len < sizeof(int))
  1044. return -EINVAL;
  1045. len = sizeof(int);
  1046. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1047. if (put_user(len, optlen) ||
  1048. put_user(val, optval))
  1049. return -EFAULT;
  1050. err = 0;
  1051. break;
  1052. case NETLINK_NO_ENOBUFS:
  1053. if (len < sizeof(int))
  1054. return -EINVAL;
  1055. len = sizeof(int);
  1056. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1057. if (put_user(len, optlen) ||
  1058. put_user(val, optval))
  1059. return -EFAULT;
  1060. err = 0;
  1061. break;
  1062. default:
  1063. err = -ENOPROTOOPT;
  1064. }
  1065. return err;
  1066. }
  1067. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1068. {
  1069. struct nl_pktinfo info;
  1070. info.group = NETLINK_CB(skb).dst_group;
  1071. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1072. }
  1073. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1074. struct msghdr *msg, size_t len)
  1075. {
  1076. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1077. struct sock *sk = sock->sk;
  1078. struct netlink_sock *nlk = nlk_sk(sk);
  1079. struct sockaddr_nl *addr = msg->msg_name;
  1080. u32 dst_pid;
  1081. u32 dst_group;
  1082. struct sk_buff *skb;
  1083. int err;
  1084. struct scm_cookie scm;
  1085. if (msg->msg_flags&MSG_OOB)
  1086. return -EOPNOTSUPP;
  1087. if (NULL == siocb->scm)
  1088. siocb->scm = &scm;
  1089. err = scm_send(sock, msg, siocb->scm);
  1090. if (err < 0)
  1091. return err;
  1092. if (msg->msg_namelen) {
  1093. if (addr->nl_family != AF_NETLINK)
  1094. return -EINVAL;
  1095. dst_pid = addr->nl_pid;
  1096. dst_group = ffs(addr->nl_groups);
  1097. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1098. return -EPERM;
  1099. } else {
  1100. dst_pid = nlk->dst_pid;
  1101. dst_group = nlk->dst_group;
  1102. }
  1103. if (!nlk->pid) {
  1104. err = netlink_autobind(sock);
  1105. if (err)
  1106. goto out;
  1107. }
  1108. err = -EMSGSIZE;
  1109. if (len > sk->sk_sndbuf - 32)
  1110. goto out;
  1111. err = -ENOBUFS;
  1112. skb = alloc_skb(len, GFP_KERNEL);
  1113. if (skb == NULL)
  1114. goto out;
  1115. NETLINK_CB(skb).pid = nlk->pid;
  1116. NETLINK_CB(skb).dst_group = dst_group;
  1117. NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
  1118. NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
  1119. security_task_getsecid(current, &(NETLINK_CB(skb).sid));
  1120. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1121. /* What can I do? Netlink is asynchronous, so that
  1122. we will have to save current capabilities to
  1123. check them, when this message will be delivered
  1124. to corresponding kernel module. --ANK (980802)
  1125. */
  1126. err = -EFAULT;
  1127. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1128. kfree_skb(skb);
  1129. goto out;
  1130. }
  1131. err = security_netlink_send(sk, skb);
  1132. if (err) {
  1133. kfree_skb(skb);
  1134. goto out;
  1135. }
  1136. if (dst_group) {
  1137. atomic_inc(&skb->users);
  1138. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1139. }
  1140. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1141. out:
  1142. return err;
  1143. }
  1144. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1145. struct msghdr *msg, size_t len,
  1146. int flags)
  1147. {
  1148. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1149. struct scm_cookie scm;
  1150. struct sock *sk = sock->sk;
  1151. struct netlink_sock *nlk = nlk_sk(sk);
  1152. int noblock = flags&MSG_DONTWAIT;
  1153. size_t copied;
  1154. struct sk_buff *skb, *frag __maybe_unused = NULL;
  1155. int err;
  1156. if (flags&MSG_OOB)
  1157. return -EOPNOTSUPP;
  1158. copied = 0;
  1159. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1160. if (skb == NULL)
  1161. goto out;
  1162. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1163. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1164. bool need_compat = !!(flags & MSG_CMSG_COMPAT);
  1165. /*
  1166. * If this skb has a frag_list, then here that means that
  1167. * we will have to use the frag_list skb for compat tasks
  1168. * and the regular skb for non-compat tasks.
  1169. *
  1170. * The skb might (and likely will) be cloned, so we can't
  1171. * just reset frag_list and go on with things -- we need to
  1172. * keep that. For the compat case that's easy -- simply get
  1173. * a reference to the compat skb and free the regular one
  1174. * including the frag. For the non-compat case, we need to
  1175. * avoid sending the frag to the user -- so assign NULL but
  1176. * restore it below before freeing the skb.
  1177. */
  1178. if (need_compat) {
  1179. struct sk_buff *compskb = skb_shinfo(skb)->frag_list;
  1180. skb_get(compskb);
  1181. kfree_skb(skb);
  1182. skb = compskb;
  1183. } else {
  1184. frag = skb_shinfo(skb)->frag_list;
  1185. skb_shinfo(skb)->frag_list = NULL;
  1186. }
  1187. }
  1188. #endif
  1189. msg->msg_namelen = 0;
  1190. copied = skb->len;
  1191. if (len < copied) {
  1192. msg->msg_flags |= MSG_TRUNC;
  1193. copied = len;
  1194. }
  1195. skb_reset_transport_header(skb);
  1196. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1197. if (msg->msg_name) {
  1198. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1199. addr->nl_family = AF_NETLINK;
  1200. addr->nl_pad = 0;
  1201. addr->nl_pid = NETLINK_CB(skb).pid;
  1202. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1203. msg->msg_namelen = sizeof(*addr);
  1204. }
  1205. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1206. netlink_cmsg_recv_pktinfo(msg, skb);
  1207. if (NULL == siocb->scm) {
  1208. memset(&scm, 0, sizeof(scm));
  1209. siocb->scm = &scm;
  1210. }
  1211. siocb->scm->creds = *NETLINK_CREDS(skb);
  1212. if (flags & MSG_TRUNC)
  1213. copied = skb->len;
  1214. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1215. skb_shinfo(skb)->frag_list = frag;
  1216. #endif
  1217. skb_free_datagram(sk, skb);
  1218. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1219. netlink_dump(sk);
  1220. scm_recv(sock, msg, siocb->scm, flags);
  1221. out:
  1222. netlink_rcv_wake(sk);
  1223. return err ? : copied;
  1224. }
  1225. static void netlink_data_ready(struct sock *sk, int len)
  1226. {
  1227. BUG();
  1228. }
  1229. /*
  1230. * We export these functions to other modules. They provide a
  1231. * complete set of kernel non-blocking support for message
  1232. * queueing.
  1233. */
  1234. struct sock *
  1235. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1236. void (*input)(struct sk_buff *skb),
  1237. struct mutex *cb_mutex, struct module *module)
  1238. {
  1239. struct socket *sock;
  1240. struct sock *sk;
  1241. struct netlink_sock *nlk;
  1242. unsigned long *listeners = NULL;
  1243. BUG_ON(!nl_table);
  1244. if (unit < 0 || unit >= MAX_LINKS)
  1245. return NULL;
  1246. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1247. return NULL;
  1248. /*
  1249. * We have to just have a reference on the net from sk, but don't
  1250. * get_net it. Besides, we cannot get and then put the net here.
  1251. * So we create one inside init_net and the move it to net.
  1252. */
  1253. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1254. goto out_sock_release_nosk;
  1255. sk = sock->sk;
  1256. sk_change_net(sk, net);
  1257. if (groups < 32)
  1258. groups = 32;
  1259. listeners = kzalloc(NLGRPSZ(groups) + sizeof(struct listeners_rcu_head),
  1260. GFP_KERNEL);
  1261. if (!listeners)
  1262. goto out_sock_release;
  1263. sk->sk_data_ready = netlink_data_ready;
  1264. if (input)
  1265. nlk_sk(sk)->netlink_rcv = input;
  1266. if (netlink_insert(sk, net, 0))
  1267. goto out_sock_release;
  1268. nlk = nlk_sk(sk);
  1269. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1270. netlink_table_grab();
  1271. if (!nl_table[unit].registered) {
  1272. nl_table[unit].groups = groups;
  1273. nl_table[unit].listeners = listeners;
  1274. nl_table[unit].cb_mutex = cb_mutex;
  1275. nl_table[unit].module = module;
  1276. nl_table[unit].registered = 1;
  1277. } else {
  1278. kfree(listeners);
  1279. nl_table[unit].registered++;
  1280. }
  1281. netlink_table_ungrab();
  1282. return sk;
  1283. out_sock_release:
  1284. kfree(listeners);
  1285. netlink_kernel_release(sk);
  1286. return NULL;
  1287. out_sock_release_nosk:
  1288. sock_release(sock);
  1289. return NULL;
  1290. }
  1291. EXPORT_SYMBOL(netlink_kernel_create);
  1292. void
  1293. netlink_kernel_release(struct sock *sk)
  1294. {
  1295. sk_release_kernel(sk);
  1296. }
  1297. EXPORT_SYMBOL(netlink_kernel_release);
  1298. static void netlink_free_old_listeners(struct rcu_head *rcu_head)
  1299. {
  1300. struct listeners_rcu_head *lrh;
  1301. lrh = container_of(rcu_head, struct listeners_rcu_head, rcu_head);
  1302. kfree(lrh->ptr);
  1303. }
  1304. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1305. {
  1306. unsigned long *listeners, *old = NULL;
  1307. struct listeners_rcu_head *old_rcu_head;
  1308. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1309. if (groups < 32)
  1310. groups = 32;
  1311. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1312. listeners = kzalloc(NLGRPSZ(groups) +
  1313. sizeof(struct listeners_rcu_head),
  1314. GFP_ATOMIC);
  1315. if (!listeners)
  1316. return -ENOMEM;
  1317. old = tbl->listeners;
  1318. memcpy(listeners, old, NLGRPSZ(tbl->groups));
  1319. rcu_assign_pointer(tbl->listeners, listeners);
  1320. /*
  1321. * Free the old memory after an RCU grace period so we
  1322. * don't leak it. We use call_rcu() here in order to be
  1323. * able to call this function from atomic contexts. The
  1324. * allocation of this memory will have reserved enough
  1325. * space for struct listeners_rcu_head at the end.
  1326. */
  1327. old_rcu_head = (void *)(tbl->listeners +
  1328. NLGRPLONGS(tbl->groups));
  1329. old_rcu_head->ptr = old;
  1330. call_rcu(&old_rcu_head->rcu_head, netlink_free_old_listeners);
  1331. }
  1332. tbl->groups = groups;
  1333. return 0;
  1334. }
  1335. /**
  1336. * netlink_change_ngroups - change number of multicast groups
  1337. *
  1338. * This changes the number of multicast groups that are available
  1339. * on a certain netlink family. Note that it is not possible to
  1340. * change the number of groups to below 32. Also note that it does
  1341. * not implicitly call netlink_clear_multicast_users() when the
  1342. * number of groups is reduced.
  1343. *
  1344. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1345. * @groups: The new number of groups.
  1346. */
  1347. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1348. {
  1349. int err;
  1350. netlink_table_grab();
  1351. err = __netlink_change_ngroups(sk, groups);
  1352. netlink_table_ungrab();
  1353. return err;
  1354. }
  1355. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1356. {
  1357. struct sock *sk;
  1358. struct hlist_node *node;
  1359. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1360. sk_for_each_bound(sk, node, &tbl->mc_list)
  1361. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1362. }
  1363. /**
  1364. * netlink_clear_multicast_users - kick off multicast listeners
  1365. *
  1366. * This function removes all listeners from the given group.
  1367. * @ksk: The kernel netlink socket, as returned by
  1368. * netlink_kernel_create().
  1369. * @group: The multicast group to clear.
  1370. */
  1371. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1372. {
  1373. netlink_table_grab();
  1374. __netlink_clear_multicast_users(ksk, group);
  1375. netlink_table_ungrab();
  1376. }
  1377. void netlink_set_nonroot(int protocol, unsigned int flags)
  1378. {
  1379. if ((unsigned int)protocol < MAX_LINKS)
  1380. nl_table[protocol].nl_nonroot = flags;
  1381. }
  1382. EXPORT_SYMBOL(netlink_set_nonroot);
  1383. static void netlink_destroy_callback(struct netlink_callback *cb)
  1384. {
  1385. kfree_skb(cb->skb);
  1386. kfree(cb);
  1387. }
  1388. /*
  1389. * It looks a bit ugly.
  1390. * It would be better to create kernel thread.
  1391. */
  1392. static int netlink_dump(struct sock *sk)
  1393. {
  1394. struct netlink_sock *nlk = nlk_sk(sk);
  1395. struct netlink_callback *cb;
  1396. struct sk_buff *skb;
  1397. struct nlmsghdr *nlh;
  1398. int len, err = -ENOBUFS;
  1399. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1400. if (!skb)
  1401. goto errout;
  1402. mutex_lock(nlk->cb_mutex);
  1403. cb = nlk->cb;
  1404. if (cb == NULL) {
  1405. err = -EINVAL;
  1406. goto errout_skb;
  1407. }
  1408. len = cb->dump(skb, cb);
  1409. if (len > 0) {
  1410. mutex_unlock(nlk->cb_mutex);
  1411. if (sk_filter(sk, skb))
  1412. kfree_skb(skb);
  1413. else {
  1414. skb_queue_tail(&sk->sk_receive_queue, skb);
  1415. sk->sk_data_ready(sk, skb->len);
  1416. }
  1417. return 0;
  1418. }
  1419. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1420. if (!nlh)
  1421. goto errout_skb;
  1422. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1423. if (sk_filter(sk, skb))
  1424. kfree_skb(skb);
  1425. else {
  1426. skb_queue_tail(&sk->sk_receive_queue, skb);
  1427. sk->sk_data_ready(sk, skb->len);
  1428. }
  1429. if (cb->done)
  1430. cb->done(cb);
  1431. nlk->cb = NULL;
  1432. mutex_unlock(nlk->cb_mutex);
  1433. netlink_destroy_callback(cb);
  1434. return 0;
  1435. errout_skb:
  1436. mutex_unlock(nlk->cb_mutex);
  1437. kfree_skb(skb);
  1438. errout:
  1439. return err;
  1440. }
  1441. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1442. const struct nlmsghdr *nlh,
  1443. int (*dump)(struct sk_buff *skb,
  1444. struct netlink_callback *),
  1445. int (*done)(struct netlink_callback *))
  1446. {
  1447. struct netlink_callback *cb;
  1448. struct sock *sk;
  1449. struct netlink_sock *nlk;
  1450. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1451. if (cb == NULL)
  1452. return -ENOBUFS;
  1453. cb->dump = dump;
  1454. cb->done = done;
  1455. cb->nlh = nlh;
  1456. atomic_inc(&skb->users);
  1457. cb->skb = skb;
  1458. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1459. if (sk == NULL) {
  1460. netlink_destroy_callback(cb);
  1461. return -ECONNREFUSED;
  1462. }
  1463. nlk = nlk_sk(sk);
  1464. /* A dump is in progress... */
  1465. mutex_lock(nlk->cb_mutex);
  1466. if (nlk->cb) {
  1467. mutex_unlock(nlk->cb_mutex);
  1468. netlink_destroy_callback(cb);
  1469. sock_put(sk);
  1470. return -EBUSY;
  1471. }
  1472. nlk->cb = cb;
  1473. mutex_unlock(nlk->cb_mutex);
  1474. netlink_dump(sk);
  1475. sock_put(sk);
  1476. /* We successfully started a dump, by returning -EINTR we
  1477. * signal not to send ACK even if it was requested.
  1478. */
  1479. return -EINTR;
  1480. }
  1481. EXPORT_SYMBOL(netlink_dump_start);
  1482. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1483. {
  1484. struct sk_buff *skb;
  1485. struct nlmsghdr *rep;
  1486. struct nlmsgerr *errmsg;
  1487. size_t payload = sizeof(*errmsg);
  1488. /* error messages get the original request appened */
  1489. if (err)
  1490. payload += nlmsg_len(nlh);
  1491. skb = nlmsg_new(payload, GFP_KERNEL);
  1492. if (!skb) {
  1493. struct sock *sk;
  1494. sk = netlink_lookup(sock_net(in_skb->sk),
  1495. in_skb->sk->sk_protocol,
  1496. NETLINK_CB(in_skb).pid);
  1497. if (sk) {
  1498. sk->sk_err = ENOBUFS;
  1499. sk->sk_error_report(sk);
  1500. sock_put(sk);
  1501. }
  1502. return;
  1503. }
  1504. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1505. NLMSG_ERROR, payload, 0);
  1506. errmsg = nlmsg_data(rep);
  1507. errmsg->error = err;
  1508. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1509. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1510. }
  1511. EXPORT_SYMBOL(netlink_ack);
  1512. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1513. struct nlmsghdr *))
  1514. {
  1515. struct nlmsghdr *nlh;
  1516. int err;
  1517. while (skb->len >= nlmsg_total_size(0)) {
  1518. int msglen;
  1519. nlh = nlmsg_hdr(skb);
  1520. err = 0;
  1521. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1522. return 0;
  1523. /* Only requests are handled by the kernel */
  1524. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1525. goto ack;
  1526. /* Skip control messages */
  1527. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1528. goto ack;
  1529. err = cb(skb, nlh);
  1530. if (err == -EINTR)
  1531. goto skip;
  1532. ack:
  1533. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1534. netlink_ack(skb, nlh, err);
  1535. skip:
  1536. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1537. if (msglen > skb->len)
  1538. msglen = skb->len;
  1539. skb_pull(skb, msglen);
  1540. }
  1541. return 0;
  1542. }
  1543. EXPORT_SYMBOL(netlink_rcv_skb);
  1544. /**
  1545. * nlmsg_notify - send a notification netlink message
  1546. * @sk: netlink socket to use
  1547. * @skb: notification message
  1548. * @pid: destination netlink pid for reports or 0
  1549. * @group: destination multicast group or 0
  1550. * @report: 1 to report back, 0 to disable
  1551. * @flags: allocation flags
  1552. */
  1553. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1554. unsigned int group, int report, gfp_t flags)
  1555. {
  1556. int err = 0;
  1557. if (group) {
  1558. int exclude_pid = 0;
  1559. if (report) {
  1560. atomic_inc(&skb->users);
  1561. exclude_pid = pid;
  1562. }
  1563. /* errors reported via destination sk->sk_err, but propagate
  1564. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1565. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1566. }
  1567. if (report) {
  1568. int err2;
  1569. err2 = nlmsg_unicast(sk, skb, pid);
  1570. if (!err || err == -ESRCH)
  1571. err = err2;
  1572. }
  1573. return err;
  1574. }
  1575. EXPORT_SYMBOL(nlmsg_notify);
  1576. #ifdef CONFIG_PROC_FS
  1577. struct nl_seq_iter {
  1578. struct seq_net_private p;
  1579. int link;
  1580. int hash_idx;
  1581. };
  1582. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1583. {
  1584. struct nl_seq_iter *iter = seq->private;
  1585. int i, j;
  1586. struct sock *s;
  1587. struct hlist_node *node;
  1588. loff_t off = 0;
  1589. for (i = 0; i < MAX_LINKS; i++) {
  1590. struct nl_pid_hash *hash = &nl_table[i].hash;
  1591. for (j = 0; j <= hash->mask; j++) {
  1592. sk_for_each(s, node, &hash->table[j]) {
  1593. if (sock_net(s) != seq_file_net(seq))
  1594. continue;
  1595. if (off == pos) {
  1596. iter->link = i;
  1597. iter->hash_idx = j;
  1598. return s;
  1599. }
  1600. ++off;
  1601. }
  1602. }
  1603. }
  1604. return NULL;
  1605. }
  1606. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1607. __acquires(nl_table_lock)
  1608. {
  1609. read_lock(&nl_table_lock);
  1610. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1611. }
  1612. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1613. {
  1614. struct sock *s;
  1615. struct nl_seq_iter *iter;
  1616. int i, j;
  1617. ++*pos;
  1618. if (v == SEQ_START_TOKEN)
  1619. return netlink_seq_socket_idx(seq, 0);
  1620. iter = seq->private;
  1621. s = v;
  1622. do {
  1623. s = sk_next(s);
  1624. } while (s && sock_net(s) != seq_file_net(seq));
  1625. if (s)
  1626. return s;
  1627. i = iter->link;
  1628. j = iter->hash_idx + 1;
  1629. do {
  1630. struct nl_pid_hash *hash = &nl_table[i].hash;
  1631. for (; j <= hash->mask; j++) {
  1632. s = sk_head(&hash->table[j]);
  1633. while (s && sock_net(s) != seq_file_net(seq))
  1634. s = sk_next(s);
  1635. if (s) {
  1636. iter->link = i;
  1637. iter->hash_idx = j;
  1638. return s;
  1639. }
  1640. }
  1641. j = 0;
  1642. } while (++i < MAX_LINKS);
  1643. return NULL;
  1644. }
  1645. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1646. __releases(nl_table_lock)
  1647. {
  1648. read_unlock(&nl_table_lock);
  1649. }
  1650. static int netlink_seq_show(struct seq_file *seq, void *v)
  1651. {
  1652. if (v == SEQ_START_TOKEN)
  1653. seq_puts(seq,
  1654. "sk Eth Pid Groups "
  1655. "Rmem Wmem Dump Locks Drops Inode\n");
  1656. else {
  1657. struct sock *s = v;
  1658. struct netlink_sock *nlk = nlk_sk(s);
  1659. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d %-8lu\n",
  1660. s,
  1661. s->sk_protocol,
  1662. nlk->pid,
  1663. nlk->groups ? (u32)nlk->groups[0] : 0,
  1664. sk_rmem_alloc_get(s),
  1665. sk_wmem_alloc_get(s),
  1666. nlk->cb,
  1667. atomic_read(&s->sk_refcnt),
  1668. atomic_read(&s->sk_drops),
  1669. sock_i_ino(s)
  1670. );
  1671. }
  1672. return 0;
  1673. }
  1674. static const struct seq_operations netlink_seq_ops = {
  1675. .start = netlink_seq_start,
  1676. .next = netlink_seq_next,
  1677. .stop = netlink_seq_stop,
  1678. .show = netlink_seq_show,
  1679. };
  1680. static int netlink_seq_open(struct inode *inode, struct file *file)
  1681. {
  1682. return seq_open_net(inode, file, &netlink_seq_ops,
  1683. sizeof(struct nl_seq_iter));
  1684. }
  1685. static const struct file_operations netlink_seq_fops = {
  1686. .owner = THIS_MODULE,
  1687. .open = netlink_seq_open,
  1688. .read = seq_read,
  1689. .llseek = seq_lseek,
  1690. .release = seq_release_net,
  1691. };
  1692. #endif
  1693. int netlink_register_notifier(struct notifier_block *nb)
  1694. {
  1695. return atomic_notifier_chain_register(&netlink_chain, nb);
  1696. }
  1697. EXPORT_SYMBOL(netlink_register_notifier);
  1698. int netlink_unregister_notifier(struct notifier_block *nb)
  1699. {
  1700. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1701. }
  1702. EXPORT_SYMBOL(netlink_unregister_notifier);
  1703. static const struct proto_ops netlink_ops = {
  1704. .family = PF_NETLINK,
  1705. .owner = THIS_MODULE,
  1706. .release = netlink_release,
  1707. .bind = netlink_bind,
  1708. .connect = netlink_connect,
  1709. .socketpair = sock_no_socketpair,
  1710. .accept = sock_no_accept,
  1711. .getname = netlink_getname,
  1712. .poll = datagram_poll,
  1713. .ioctl = sock_no_ioctl,
  1714. .listen = sock_no_listen,
  1715. .shutdown = sock_no_shutdown,
  1716. .setsockopt = netlink_setsockopt,
  1717. .getsockopt = netlink_getsockopt,
  1718. .sendmsg = netlink_sendmsg,
  1719. .recvmsg = netlink_recvmsg,
  1720. .mmap = sock_no_mmap,
  1721. .sendpage = sock_no_sendpage,
  1722. };
  1723. static const struct net_proto_family netlink_family_ops = {
  1724. .family = PF_NETLINK,
  1725. .create = netlink_create,
  1726. .owner = THIS_MODULE, /* for consistency 8) */
  1727. };
  1728. static int __net_init netlink_net_init(struct net *net)
  1729. {
  1730. #ifdef CONFIG_PROC_FS
  1731. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1732. return -ENOMEM;
  1733. #endif
  1734. return 0;
  1735. }
  1736. static void __net_exit netlink_net_exit(struct net *net)
  1737. {
  1738. #ifdef CONFIG_PROC_FS
  1739. proc_net_remove(net, "netlink");
  1740. #endif
  1741. }
  1742. static struct pernet_operations __net_initdata netlink_net_ops = {
  1743. .init = netlink_net_init,
  1744. .exit = netlink_net_exit,
  1745. };
  1746. static int __init netlink_proto_init(void)
  1747. {
  1748. struct sk_buff *dummy_skb;
  1749. int i;
  1750. unsigned long limit;
  1751. unsigned int order;
  1752. int err = proto_register(&netlink_proto, 0);
  1753. if (err != 0)
  1754. goto out;
  1755. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1756. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1757. if (!nl_table)
  1758. goto panic;
  1759. if (totalram_pages >= (128 * 1024))
  1760. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1761. else
  1762. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1763. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1764. limit = (1UL << order) / sizeof(struct hlist_head);
  1765. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1766. for (i = 0; i < MAX_LINKS; i++) {
  1767. struct nl_pid_hash *hash = &nl_table[i].hash;
  1768. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1769. if (!hash->table) {
  1770. while (i-- > 0)
  1771. nl_pid_hash_free(nl_table[i].hash.table,
  1772. 1 * sizeof(*hash->table));
  1773. kfree(nl_table);
  1774. goto panic;
  1775. }
  1776. hash->max_shift = order;
  1777. hash->shift = 0;
  1778. hash->mask = 0;
  1779. hash->rehash_time = jiffies;
  1780. }
  1781. sock_register(&netlink_family_ops);
  1782. register_pernet_subsys(&netlink_net_ops);
  1783. /* The netlink device handler may be needed early. */
  1784. rtnetlink_init();
  1785. out:
  1786. return err;
  1787. panic:
  1788. panic("netlink_init: Cannot allocate nl_table\n");
  1789. }
  1790. core_initcall(netlink_proto_init);