af_netlink.c 47 KB

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