af_netlink.c 45 KB

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