af_netlink.c 49 KB

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