af_netlink.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  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. * Patrick McHardy <kaber@trash.net>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  14. * added netlink_proto_exit
  15. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  16. * use nlk_sk, as sk->protinfo is on a diet 8)
  17. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  18. * - inc module use count of module that owns
  19. * the kernel socket in case userspace opens
  20. * socket of same protocol
  21. * - remove all module support, since netlink is
  22. * mandatory if CONFIG_NET=y these days
  23. */
  24. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/signal.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/stat.h>
  33. #include <linux/socket.h>
  34. #include <linux/un.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/termios.h>
  37. #include <linux/sockios.h>
  38. #include <linux/net.h>
  39. #include <linux/fs.h>
  40. #include <linux/slab.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/rtnetlink.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/notifier.h>
  48. #include <linux/security.h>
  49. #include <linux/jhash.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/random.h>
  52. #include <linux/bitops.h>
  53. #include <linux/mm.h>
  54. #include <linux/types.h>
  55. #include <linux/audit.h>
  56. #include <linux/mutex.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/if_arp.h>
  59. #include <linux/rhashtable.h>
  60. #include <asm/cacheflush.h>
  61. #include <linux/hash.h>
  62. #include <linux/genetlink.h>
  63. #include <net/net_namespace.h>
  64. #include <net/sock.h>
  65. #include <net/scm.h>
  66. #include <net/netlink.h>
  67. #include "af_netlink.h"
  68. struct listeners {
  69. struct rcu_head rcu;
  70. unsigned long masks[0];
  71. };
  72. /* state bits */
  73. #define NETLINK_S_CONGESTED 0x0
  74. /* flags */
  75. #define NETLINK_F_KERNEL_SOCKET 0x1
  76. #define NETLINK_F_RECV_PKTINFO 0x2
  77. #define NETLINK_F_BROADCAST_SEND_ERROR 0x4
  78. #define NETLINK_F_RECV_NO_ENOBUFS 0x8
  79. #define NETLINK_F_LISTEN_ALL_NSID 0x10
  80. #define NETLINK_F_CAP_ACK 0x20
  81. static inline int netlink_is_kernel(struct sock *sk)
  82. {
  83. return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
  84. }
  85. struct netlink_table *nl_table __read_mostly;
  86. EXPORT_SYMBOL_GPL(nl_table);
  87. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  88. static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
  89. static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
  90. "nlk_cb_mutex-ROUTE",
  91. "nlk_cb_mutex-1",
  92. "nlk_cb_mutex-USERSOCK",
  93. "nlk_cb_mutex-FIREWALL",
  94. "nlk_cb_mutex-SOCK_DIAG",
  95. "nlk_cb_mutex-NFLOG",
  96. "nlk_cb_mutex-XFRM",
  97. "nlk_cb_mutex-SELINUX",
  98. "nlk_cb_mutex-ISCSI",
  99. "nlk_cb_mutex-AUDIT",
  100. "nlk_cb_mutex-FIB_LOOKUP",
  101. "nlk_cb_mutex-CONNECTOR",
  102. "nlk_cb_mutex-NETFILTER",
  103. "nlk_cb_mutex-IP6_FW",
  104. "nlk_cb_mutex-DNRTMSG",
  105. "nlk_cb_mutex-KOBJECT_UEVENT",
  106. "nlk_cb_mutex-GENERIC",
  107. "nlk_cb_mutex-17",
  108. "nlk_cb_mutex-SCSITRANSPORT",
  109. "nlk_cb_mutex-ECRYPTFS",
  110. "nlk_cb_mutex-RDMA",
  111. "nlk_cb_mutex-CRYPTO",
  112. "nlk_cb_mutex-SMC",
  113. "nlk_cb_mutex-23",
  114. "nlk_cb_mutex-24",
  115. "nlk_cb_mutex-25",
  116. "nlk_cb_mutex-26",
  117. "nlk_cb_mutex-27",
  118. "nlk_cb_mutex-28",
  119. "nlk_cb_mutex-29",
  120. "nlk_cb_mutex-30",
  121. "nlk_cb_mutex-31",
  122. "nlk_cb_mutex-MAX_LINKS"
  123. };
  124. static int netlink_dump(struct sock *sk);
  125. static void netlink_skb_destructor(struct sk_buff *skb);
  126. /* nl_table locking explained:
  127. * Lookup and traversal are protected with an RCU read-side lock. Insertion
  128. * and removal are protected with per bucket lock while using RCU list
  129. * modification primitives and may run in parallel to RCU protected lookups.
  130. * Destruction of the Netlink socket may only occur *after* nl_table_lock has
  131. * been acquired * either during or after the socket has been removed from
  132. * the list and after an RCU grace period.
  133. */
  134. DEFINE_RWLOCK(nl_table_lock);
  135. EXPORT_SYMBOL_GPL(nl_table_lock);
  136. static atomic_t nl_table_users = ATOMIC_INIT(0);
  137. #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
  138. static BLOCKING_NOTIFIER_HEAD(netlink_chain);
  139. static DEFINE_SPINLOCK(netlink_tap_lock);
  140. static struct list_head netlink_tap_all __read_mostly;
  141. static const struct rhashtable_params netlink_rhashtable_params;
  142. static inline u32 netlink_group_mask(u32 group)
  143. {
  144. return group ? 1 << (group - 1) : 0;
  145. }
  146. static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
  147. gfp_t gfp_mask)
  148. {
  149. unsigned int len = skb_end_offset(skb);
  150. struct sk_buff *new;
  151. new = alloc_skb(len, gfp_mask);
  152. if (new == NULL)
  153. return NULL;
  154. NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
  155. NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
  156. NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
  157. memcpy(skb_put(new, len), skb->data, len);
  158. return new;
  159. }
  160. int netlink_add_tap(struct netlink_tap *nt)
  161. {
  162. if (unlikely(nt->dev->type != ARPHRD_NETLINK))
  163. return -EINVAL;
  164. spin_lock(&netlink_tap_lock);
  165. list_add_rcu(&nt->list, &netlink_tap_all);
  166. spin_unlock(&netlink_tap_lock);
  167. __module_get(nt->module);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(netlink_add_tap);
  171. static int __netlink_remove_tap(struct netlink_tap *nt)
  172. {
  173. bool found = false;
  174. struct netlink_tap *tmp;
  175. spin_lock(&netlink_tap_lock);
  176. list_for_each_entry(tmp, &netlink_tap_all, list) {
  177. if (nt == tmp) {
  178. list_del_rcu(&nt->list);
  179. found = true;
  180. goto out;
  181. }
  182. }
  183. pr_warn("__netlink_remove_tap: %p not found\n", nt);
  184. out:
  185. spin_unlock(&netlink_tap_lock);
  186. if (found)
  187. module_put(nt->module);
  188. return found ? 0 : -ENODEV;
  189. }
  190. int netlink_remove_tap(struct netlink_tap *nt)
  191. {
  192. int ret;
  193. ret = __netlink_remove_tap(nt);
  194. synchronize_net();
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(netlink_remove_tap);
  198. static bool netlink_filter_tap(const struct sk_buff *skb)
  199. {
  200. struct sock *sk = skb->sk;
  201. /* We take the more conservative approach and
  202. * whitelist socket protocols that may pass.
  203. */
  204. switch (sk->sk_protocol) {
  205. case NETLINK_ROUTE:
  206. case NETLINK_USERSOCK:
  207. case NETLINK_SOCK_DIAG:
  208. case NETLINK_NFLOG:
  209. case NETLINK_XFRM:
  210. case NETLINK_FIB_LOOKUP:
  211. case NETLINK_NETFILTER:
  212. case NETLINK_GENERIC:
  213. return true;
  214. }
  215. return false;
  216. }
  217. static int __netlink_deliver_tap_skb(struct sk_buff *skb,
  218. struct net_device *dev)
  219. {
  220. struct sk_buff *nskb;
  221. struct sock *sk = skb->sk;
  222. int ret = -ENOMEM;
  223. dev_hold(dev);
  224. if (is_vmalloc_addr(skb->head))
  225. nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
  226. else
  227. nskb = skb_clone(skb, GFP_ATOMIC);
  228. if (nskb) {
  229. nskb->dev = dev;
  230. nskb->protocol = htons((u16) sk->sk_protocol);
  231. nskb->pkt_type = netlink_is_kernel(sk) ?
  232. PACKET_KERNEL : PACKET_USER;
  233. skb_reset_network_header(nskb);
  234. ret = dev_queue_xmit(nskb);
  235. if (unlikely(ret > 0))
  236. ret = net_xmit_errno(ret);
  237. }
  238. dev_put(dev);
  239. return ret;
  240. }
  241. static void __netlink_deliver_tap(struct sk_buff *skb)
  242. {
  243. int ret;
  244. struct netlink_tap *tmp;
  245. if (!netlink_filter_tap(skb))
  246. return;
  247. list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
  248. ret = __netlink_deliver_tap_skb(skb, tmp->dev);
  249. if (unlikely(ret))
  250. break;
  251. }
  252. }
  253. static void netlink_deliver_tap(struct sk_buff *skb)
  254. {
  255. rcu_read_lock();
  256. if (unlikely(!list_empty(&netlink_tap_all)))
  257. __netlink_deliver_tap(skb);
  258. rcu_read_unlock();
  259. }
  260. static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
  261. struct sk_buff *skb)
  262. {
  263. if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
  264. netlink_deliver_tap(skb);
  265. }
  266. static void netlink_overrun(struct sock *sk)
  267. {
  268. struct netlink_sock *nlk = nlk_sk(sk);
  269. if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
  270. if (!test_and_set_bit(NETLINK_S_CONGESTED,
  271. &nlk_sk(sk)->state)) {
  272. sk->sk_err = ENOBUFS;
  273. sk->sk_error_report(sk);
  274. }
  275. }
  276. atomic_inc(&sk->sk_drops);
  277. }
  278. static void netlink_rcv_wake(struct sock *sk)
  279. {
  280. struct netlink_sock *nlk = nlk_sk(sk);
  281. if (skb_queue_empty(&sk->sk_receive_queue))
  282. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  283. if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
  284. wake_up_interruptible(&nlk->wait);
  285. }
  286. static void netlink_skb_destructor(struct sk_buff *skb)
  287. {
  288. if (is_vmalloc_addr(skb->head)) {
  289. if (!skb->cloned ||
  290. !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
  291. vfree(skb->head);
  292. skb->head = NULL;
  293. }
  294. if (skb->sk != NULL)
  295. sock_rfree(skb);
  296. }
  297. static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  298. {
  299. WARN_ON(skb->sk != NULL);
  300. skb->sk = sk;
  301. skb->destructor = netlink_skb_destructor;
  302. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  303. sk_mem_charge(sk, skb->truesize);
  304. }
  305. static void netlink_sock_destruct(struct sock *sk)
  306. {
  307. struct netlink_sock *nlk = nlk_sk(sk);
  308. if (nlk->cb_running) {
  309. if (nlk->cb.done)
  310. nlk->cb.done(&nlk->cb);
  311. module_put(nlk->cb.module);
  312. kfree_skb(nlk->cb.skb);
  313. }
  314. skb_queue_purge(&sk->sk_receive_queue);
  315. if (!sock_flag(sk, SOCK_DEAD)) {
  316. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  317. return;
  318. }
  319. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  320. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  321. WARN_ON(nlk_sk(sk)->groups);
  322. }
  323. static void netlink_sock_destruct_work(struct work_struct *work)
  324. {
  325. struct netlink_sock *nlk = container_of(work, struct netlink_sock,
  326. work);
  327. sk_free(&nlk->sk);
  328. }
  329. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  330. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  331. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  332. * this, _but_ remember, it adds useless work on UP machines.
  333. */
  334. void netlink_table_grab(void)
  335. __acquires(nl_table_lock)
  336. {
  337. might_sleep();
  338. write_lock_irq(&nl_table_lock);
  339. if (atomic_read(&nl_table_users)) {
  340. DECLARE_WAITQUEUE(wait, current);
  341. add_wait_queue_exclusive(&nl_table_wait, &wait);
  342. for (;;) {
  343. set_current_state(TASK_UNINTERRUPTIBLE);
  344. if (atomic_read(&nl_table_users) == 0)
  345. break;
  346. write_unlock_irq(&nl_table_lock);
  347. schedule();
  348. write_lock_irq(&nl_table_lock);
  349. }
  350. __set_current_state(TASK_RUNNING);
  351. remove_wait_queue(&nl_table_wait, &wait);
  352. }
  353. }
  354. void netlink_table_ungrab(void)
  355. __releases(nl_table_lock)
  356. {
  357. write_unlock_irq(&nl_table_lock);
  358. wake_up(&nl_table_wait);
  359. }
  360. static inline void
  361. netlink_lock_table(void)
  362. {
  363. /* read_lock() synchronizes us to netlink_table_grab */
  364. read_lock(&nl_table_lock);
  365. atomic_inc(&nl_table_users);
  366. read_unlock(&nl_table_lock);
  367. }
  368. static inline void
  369. netlink_unlock_table(void)
  370. {
  371. if (atomic_dec_and_test(&nl_table_users))
  372. wake_up(&nl_table_wait);
  373. }
  374. struct netlink_compare_arg
  375. {
  376. possible_net_t pnet;
  377. u32 portid;
  378. };
  379. /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
  380. #define netlink_compare_arg_len \
  381. (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
  382. static inline int netlink_compare(struct rhashtable_compare_arg *arg,
  383. const void *ptr)
  384. {
  385. const struct netlink_compare_arg *x = arg->key;
  386. const struct netlink_sock *nlk = ptr;
  387. return nlk->portid != x->portid ||
  388. !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
  389. }
  390. static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
  391. struct net *net, u32 portid)
  392. {
  393. memset(arg, 0, sizeof(*arg));
  394. write_pnet(&arg->pnet, net);
  395. arg->portid = portid;
  396. }
  397. static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
  398. struct net *net)
  399. {
  400. struct netlink_compare_arg arg;
  401. netlink_compare_arg_init(&arg, net, portid);
  402. return rhashtable_lookup_fast(&table->hash, &arg,
  403. netlink_rhashtable_params);
  404. }
  405. static int __netlink_insert(struct netlink_table *table, struct sock *sk)
  406. {
  407. struct netlink_compare_arg arg;
  408. netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
  409. return rhashtable_lookup_insert_key(&table->hash, &arg,
  410. &nlk_sk(sk)->node,
  411. netlink_rhashtable_params);
  412. }
  413. static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
  414. {
  415. struct netlink_table *table = &nl_table[protocol];
  416. struct sock *sk;
  417. rcu_read_lock();
  418. sk = __netlink_lookup(table, portid, net);
  419. if (sk)
  420. sock_hold(sk);
  421. rcu_read_unlock();
  422. return sk;
  423. }
  424. static const struct proto_ops netlink_ops;
  425. static void
  426. netlink_update_listeners(struct sock *sk)
  427. {
  428. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  429. unsigned long mask;
  430. unsigned int i;
  431. struct listeners *listeners;
  432. listeners = nl_deref_protected(tbl->listeners);
  433. if (!listeners)
  434. return;
  435. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  436. mask = 0;
  437. sk_for_each_bound(sk, &tbl->mc_list) {
  438. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  439. mask |= nlk_sk(sk)->groups[i];
  440. }
  441. listeners->masks[i] = mask;
  442. }
  443. /* this function is only called with the netlink table "grabbed", which
  444. * makes sure updates are visible before bind or setsockopt return. */
  445. }
  446. static int netlink_insert(struct sock *sk, u32 portid)
  447. {
  448. struct netlink_table *table = &nl_table[sk->sk_protocol];
  449. int err;
  450. lock_sock(sk);
  451. err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
  452. if (nlk_sk(sk)->bound)
  453. goto err;
  454. err = -ENOMEM;
  455. if (BITS_PER_LONG > 32 &&
  456. unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
  457. goto err;
  458. nlk_sk(sk)->portid = portid;
  459. sock_hold(sk);
  460. err = __netlink_insert(table, sk);
  461. if (err) {
  462. /* In case the hashtable backend returns with -EBUSY
  463. * from here, it must not escape to the caller.
  464. */
  465. if (unlikely(err == -EBUSY))
  466. err = -EOVERFLOW;
  467. if (err == -EEXIST)
  468. err = -EADDRINUSE;
  469. sock_put(sk);
  470. goto err;
  471. }
  472. /* We need to ensure that the socket is hashed and visible. */
  473. smp_wmb();
  474. nlk_sk(sk)->bound = portid;
  475. err:
  476. release_sock(sk);
  477. return err;
  478. }
  479. static void netlink_remove(struct sock *sk)
  480. {
  481. struct netlink_table *table;
  482. table = &nl_table[sk->sk_protocol];
  483. if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
  484. netlink_rhashtable_params)) {
  485. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  486. __sock_put(sk);
  487. }
  488. netlink_table_grab();
  489. if (nlk_sk(sk)->subscriptions) {
  490. __sk_del_bind_node(sk);
  491. netlink_update_listeners(sk);
  492. }
  493. if (sk->sk_protocol == NETLINK_GENERIC)
  494. atomic_inc(&genl_sk_destructing_cnt);
  495. netlink_table_ungrab();
  496. }
  497. static struct proto netlink_proto = {
  498. .name = "NETLINK",
  499. .owner = THIS_MODULE,
  500. .obj_size = sizeof(struct netlink_sock),
  501. };
  502. static int __netlink_create(struct net *net, struct socket *sock,
  503. struct mutex *cb_mutex, int protocol,
  504. int kern)
  505. {
  506. struct sock *sk;
  507. struct netlink_sock *nlk;
  508. sock->ops = &netlink_ops;
  509. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
  510. if (!sk)
  511. return -ENOMEM;
  512. sock_init_data(sock, sk);
  513. nlk = nlk_sk(sk);
  514. if (cb_mutex) {
  515. nlk->cb_mutex = cb_mutex;
  516. } else {
  517. nlk->cb_mutex = &nlk->cb_def_mutex;
  518. mutex_init(nlk->cb_mutex);
  519. lockdep_set_class_and_name(nlk->cb_mutex,
  520. nlk_cb_mutex_keys + protocol,
  521. nlk_cb_mutex_key_strings[protocol]);
  522. }
  523. init_waitqueue_head(&nlk->wait);
  524. sk->sk_destruct = netlink_sock_destruct;
  525. sk->sk_protocol = protocol;
  526. return 0;
  527. }
  528. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  529. int kern)
  530. {
  531. struct module *module = NULL;
  532. struct mutex *cb_mutex;
  533. struct netlink_sock *nlk;
  534. int (*bind)(struct net *net, int group);
  535. void (*unbind)(struct net *net, int group);
  536. int err = 0;
  537. sock->state = SS_UNCONNECTED;
  538. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  539. return -ESOCKTNOSUPPORT;
  540. if (protocol < 0 || protocol >= MAX_LINKS)
  541. return -EPROTONOSUPPORT;
  542. netlink_lock_table();
  543. #ifdef CONFIG_MODULES
  544. if (!nl_table[protocol].registered) {
  545. netlink_unlock_table();
  546. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  547. netlink_lock_table();
  548. }
  549. #endif
  550. if (nl_table[protocol].registered &&
  551. try_module_get(nl_table[protocol].module))
  552. module = nl_table[protocol].module;
  553. else
  554. err = -EPROTONOSUPPORT;
  555. cb_mutex = nl_table[protocol].cb_mutex;
  556. bind = nl_table[protocol].bind;
  557. unbind = nl_table[protocol].unbind;
  558. netlink_unlock_table();
  559. if (err < 0)
  560. goto out;
  561. err = __netlink_create(net, sock, cb_mutex, protocol, kern);
  562. if (err < 0)
  563. goto out_module;
  564. local_bh_disable();
  565. sock_prot_inuse_add(net, &netlink_proto, 1);
  566. local_bh_enable();
  567. nlk = nlk_sk(sock->sk);
  568. nlk->module = module;
  569. nlk->netlink_bind = bind;
  570. nlk->netlink_unbind = unbind;
  571. out:
  572. return err;
  573. out_module:
  574. module_put(module);
  575. goto out;
  576. }
  577. static void deferred_put_nlk_sk(struct rcu_head *head)
  578. {
  579. struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
  580. struct sock *sk = &nlk->sk;
  581. if (!atomic_dec_and_test(&sk->sk_refcnt))
  582. return;
  583. if (nlk->cb_running && nlk->cb.done) {
  584. INIT_WORK(&nlk->work, netlink_sock_destruct_work);
  585. schedule_work(&nlk->work);
  586. return;
  587. }
  588. sk_free(sk);
  589. }
  590. static int netlink_release(struct socket *sock)
  591. {
  592. struct sock *sk = sock->sk;
  593. struct netlink_sock *nlk;
  594. if (!sk)
  595. return 0;
  596. netlink_remove(sk);
  597. sock_orphan(sk);
  598. nlk = nlk_sk(sk);
  599. /*
  600. * OK. Socket is unlinked, any packets that arrive now
  601. * will be purged.
  602. */
  603. /* must not acquire netlink_table_lock in any way again before unbind
  604. * and notifying genetlink is done as otherwise it might deadlock
  605. */
  606. if (nlk->netlink_unbind) {
  607. int i;
  608. for (i = 0; i < nlk->ngroups; i++)
  609. if (test_bit(i, nlk->groups))
  610. nlk->netlink_unbind(sock_net(sk), i + 1);
  611. }
  612. if (sk->sk_protocol == NETLINK_GENERIC &&
  613. atomic_dec_return(&genl_sk_destructing_cnt) == 0)
  614. wake_up(&genl_sk_destructing_waitq);
  615. sock->sk = NULL;
  616. wake_up_interruptible_all(&nlk->wait);
  617. skb_queue_purge(&sk->sk_write_queue);
  618. if (nlk->portid && nlk->bound) {
  619. struct netlink_notify n = {
  620. .net = sock_net(sk),
  621. .protocol = sk->sk_protocol,
  622. .portid = nlk->portid,
  623. };
  624. blocking_notifier_call_chain(&netlink_chain,
  625. NETLINK_URELEASE, &n);
  626. }
  627. module_put(nlk->module);
  628. if (netlink_is_kernel(sk)) {
  629. netlink_table_grab();
  630. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  631. if (--nl_table[sk->sk_protocol].registered == 0) {
  632. struct listeners *old;
  633. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  634. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  635. kfree_rcu(old, rcu);
  636. nl_table[sk->sk_protocol].module = NULL;
  637. nl_table[sk->sk_protocol].bind = NULL;
  638. nl_table[sk->sk_protocol].unbind = NULL;
  639. nl_table[sk->sk_protocol].flags = 0;
  640. nl_table[sk->sk_protocol].registered = 0;
  641. }
  642. netlink_table_ungrab();
  643. }
  644. kfree(nlk->groups);
  645. nlk->groups = NULL;
  646. local_bh_disable();
  647. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  648. local_bh_enable();
  649. call_rcu(&nlk->rcu, deferred_put_nlk_sk);
  650. return 0;
  651. }
  652. static int netlink_autobind(struct socket *sock)
  653. {
  654. struct sock *sk = sock->sk;
  655. struct net *net = sock_net(sk);
  656. struct netlink_table *table = &nl_table[sk->sk_protocol];
  657. s32 portid = task_tgid_vnr(current);
  658. int err;
  659. s32 rover = -4096;
  660. bool ok;
  661. retry:
  662. cond_resched();
  663. rcu_read_lock();
  664. ok = !__netlink_lookup(table, portid, net);
  665. rcu_read_unlock();
  666. if (!ok) {
  667. /* Bind collision, search negative portid values. */
  668. if (rover == -4096)
  669. /* rover will be in range [S32_MIN, -4097] */
  670. rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
  671. else if (rover >= -4096)
  672. rover = -4097;
  673. portid = rover--;
  674. goto retry;
  675. }
  676. err = netlink_insert(sk, portid);
  677. if (err == -EADDRINUSE)
  678. goto retry;
  679. /* If 2 threads race to autobind, that is fine. */
  680. if (err == -EBUSY)
  681. err = 0;
  682. return err;
  683. }
  684. /**
  685. * __netlink_ns_capable - General netlink message capability test
  686. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  687. * @user_ns: The user namespace of the capability to use
  688. * @cap: The capability to use
  689. *
  690. * Test to see if the opener of the socket we received the message
  691. * from had when the netlink socket was created and the sender of the
  692. * message has has the capability @cap in the user namespace @user_ns.
  693. */
  694. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  695. struct user_namespace *user_ns, int cap)
  696. {
  697. return ((nsp->flags & NETLINK_SKB_DST) ||
  698. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  699. ns_capable(user_ns, cap);
  700. }
  701. EXPORT_SYMBOL(__netlink_ns_capable);
  702. /**
  703. * netlink_ns_capable - General netlink message capability test
  704. * @skb: socket buffer holding a netlink command from userspace
  705. * @user_ns: The user namespace of the capability to use
  706. * @cap: The capability to use
  707. *
  708. * Test to see if the opener of the socket we received the message
  709. * from had when the netlink socket was created and the sender of the
  710. * message has has the capability @cap in the user namespace @user_ns.
  711. */
  712. bool netlink_ns_capable(const struct sk_buff *skb,
  713. struct user_namespace *user_ns, int cap)
  714. {
  715. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  716. }
  717. EXPORT_SYMBOL(netlink_ns_capable);
  718. /**
  719. * netlink_capable - Netlink global message capability test
  720. * @skb: socket buffer holding a netlink command from userspace
  721. * @cap: The capability to use
  722. *
  723. * Test to see if the opener of the socket we received the message
  724. * from had when the netlink socket was created and the sender of the
  725. * message has has the capability @cap in all user namespaces.
  726. */
  727. bool netlink_capable(const struct sk_buff *skb, int cap)
  728. {
  729. return netlink_ns_capable(skb, &init_user_ns, cap);
  730. }
  731. EXPORT_SYMBOL(netlink_capable);
  732. /**
  733. * netlink_net_capable - Netlink network namespace message capability test
  734. * @skb: socket buffer holding a netlink command from userspace
  735. * @cap: The capability to use
  736. *
  737. * Test to see if the opener of the socket we received the message
  738. * from had when the netlink socket was created and the sender of the
  739. * message has has the capability @cap over the network namespace of
  740. * the socket we received the message from.
  741. */
  742. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  743. {
  744. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  745. }
  746. EXPORT_SYMBOL(netlink_net_capable);
  747. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  748. {
  749. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  750. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  751. }
  752. static void
  753. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  754. {
  755. struct netlink_sock *nlk = nlk_sk(sk);
  756. if (nlk->subscriptions && !subscriptions)
  757. __sk_del_bind_node(sk);
  758. else if (!nlk->subscriptions && subscriptions)
  759. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  760. nlk->subscriptions = subscriptions;
  761. }
  762. static int netlink_realloc_groups(struct sock *sk)
  763. {
  764. struct netlink_sock *nlk = nlk_sk(sk);
  765. unsigned int groups;
  766. unsigned long *new_groups;
  767. int err = 0;
  768. netlink_table_grab();
  769. groups = nl_table[sk->sk_protocol].groups;
  770. if (!nl_table[sk->sk_protocol].registered) {
  771. err = -ENOENT;
  772. goto out_unlock;
  773. }
  774. if (nlk->ngroups >= groups)
  775. goto out_unlock;
  776. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  777. if (new_groups == NULL) {
  778. err = -ENOMEM;
  779. goto out_unlock;
  780. }
  781. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  782. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  783. nlk->groups = new_groups;
  784. nlk->ngroups = groups;
  785. out_unlock:
  786. netlink_table_ungrab();
  787. return err;
  788. }
  789. static void netlink_undo_bind(int group, long unsigned int groups,
  790. struct sock *sk)
  791. {
  792. struct netlink_sock *nlk = nlk_sk(sk);
  793. int undo;
  794. if (!nlk->netlink_unbind)
  795. return;
  796. for (undo = 0; undo < group; undo++)
  797. if (test_bit(undo, &groups))
  798. nlk->netlink_unbind(sock_net(sk), undo + 1);
  799. }
  800. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  801. int addr_len)
  802. {
  803. struct sock *sk = sock->sk;
  804. struct net *net = sock_net(sk);
  805. struct netlink_sock *nlk = nlk_sk(sk);
  806. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  807. int err;
  808. long unsigned int groups = nladdr->nl_groups;
  809. bool bound;
  810. if (addr_len < sizeof(struct sockaddr_nl))
  811. return -EINVAL;
  812. if (nladdr->nl_family != AF_NETLINK)
  813. return -EINVAL;
  814. /* Only superuser is allowed to listen multicasts */
  815. if (groups) {
  816. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  817. return -EPERM;
  818. err = netlink_realloc_groups(sk);
  819. if (err)
  820. return err;
  821. }
  822. bound = nlk->bound;
  823. if (bound) {
  824. /* Ensure nlk->portid is up-to-date. */
  825. smp_rmb();
  826. if (nladdr->nl_pid != nlk->portid)
  827. return -EINVAL;
  828. }
  829. if (nlk->netlink_bind && groups) {
  830. int group;
  831. for (group = 0; group < nlk->ngroups; group++) {
  832. if (!test_bit(group, &groups))
  833. continue;
  834. err = nlk->netlink_bind(net, group + 1);
  835. if (!err)
  836. continue;
  837. netlink_undo_bind(group, groups, sk);
  838. return err;
  839. }
  840. }
  841. /* No need for barriers here as we return to user-space without
  842. * using any of the bound attributes.
  843. */
  844. if (!bound) {
  845. err = nladdr->nl_pid ?
  846. netlink_insert(sk, nladdr->nl_pid) :
  847. netlink_autobind(sock);
  848. if (err) {
  849. netlink_undo_bind(nlk->ngroups, groups, sk);
  850. return err;
  851. }
  852. }
  853. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  854. return 0;
  855. netlink_table_grab();
  856. netlink_update_subscriptions(sk, nlk->subscriptions +
  857. hweight32(groups) -
  858. hweight32(nlk->groups[0]));
  859. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  860. netlink_update_listeners(sk);
  861. netlink_table_ungrab();
  862. return 0;
  863. }
  864. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  865. int alen, int flags)
  866. {
  867. int err = 0;
  868. struct sock *sk = sock->sk;
  869. struct netlink_sock *nlk = nlk_sk(sk);
  870. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  871. if (alen < sizeof(addr->sa_family))
  872. return -EINVAL;
  873. if (addr->sa_family == AF_UNSPEC) {
  874. sk->sk_state = NETLINK_UNCONNECTED;
  875. nlk->dst_portid = 0;
  876. nlk->dst_group = 0;
  877. return 0;
  878. }
  879. if (addr->sa_family != AF_NETLINK)
  880. return -EINVAL;
  881. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  882. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  883. return -EPERM;
  884. /* No need for barriers here as we return to user-space without
  885. * using any of the bound attributes.
  886. */
  887. if (!nlk->bound)
  888. err = netlink_autobind(sock);
  889. if (err == 0) {
  890. sk->sk_state = NETLINK_CONNECTED;
  891. nlk->dst_portid = nladdr->nl_pid;
  892. nlk->dst_group = ffs(nladdr->nl_groups);
  893. }
  894. return err;
  895. }
  896. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  897. int *addr_len, int peer)
  898. {
  899. struct sock *sk = sock->sk;
  900. struct netlink_sock *nlk = nlk_sk(sk);
  901. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  902. nladdr->nl_family = AF_NETLINK;
  903. nladdr->nl_pad = 0;
  904. *addr_len = sizeof(*nladdr);
  905. if (peer) {
  906. nladdr->nl_pid = nlk->dst_portid;
  907. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  908. } else {
  909. nladdr->nl_pid = nlk->portid;
  910. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  911. }
  912. return 0;
  913. }
  914. static int netlink_ioctl(struct socket *sock, unsigned int cmd,
  915. unsigned long arg)
  916. {
  917. /* try to hand this ioctl down to the NIC drivers.
  918. */
  919. return -ENOIOCTLCMD;
  920. }
  921. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  922. {
  923. struct sock *sock;
  924. struct netlink_sock *nlk;
  925. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  926. if (!sock)
  927. return ERR_PTR(-ECONNREFUSED);
  928. /* Don't bother queuing skb if kernel socket has no input function */
  929. nlk = nlk_sk(sock);
  930. if (sock->sk_state == NETLINK_CONNECTED &&
  931. nlk->dst_portid != nlk_sk(ssk)->portid) {
  932. sock_put(sock);
  933. return ERR_PTR(-ECONNREFUSED);
  934. }
  935. return sock;
  936. }
  937. struct sock *netlink_getsockbyfilp(struct file *filp)
  938. {
  939. struct inode *inode = file_inode(filp);
  940. struct sock *sock;
  941. if (!S_ISSOCK(inode->i_mode))
  942. return ERR_PTR(-ENOTSOCK);
  943. sock = SOCKET_I(inode)->sk;
  944. if (sock->sk_family != AF_NETLINK)
  945. return ERR_PTR(-EINVAL);
  946. sock_hold(sock);
  947. return sock;
  948. }
  949. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  950. int broadcast)
  951. {
  952. struct sk_buff *skb;
  953. void *data;
  954. if (size <= NLMSG_GOODSIZE || broadcast)
  955. return alloc_skb(size, GFP_KERNEL);
  956. size = SKB_DATA_ALIGN(size) +
  957. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  958. data = vmalloc(size);
  959. if (data == NULL)
  960. return NULL;
  961. skb = __build_skb(data, size);
  962. if (skb == NULL)
  963. vfree(data);
  964. else
  965. skb->destructor = netlink_skb_destructor;
  966. return skb;
  967. }
  968. /*
  969. * Attach a skb to a netlink socket.
  970. * The caller must hold a reference to the destination socket. On error, the
  971. * reference is dropped. The skb is not send to the destination, just all
  972. * all error checks are performed and memory in the queue is reserved.
  973. * Return values:
  974. * < 0: error. skb freed, reference to sock dropped.
  975. * 0: continue
  976. * 1: repeat lookup - reference dropped while waiting for socket memory.
  977. */
  978. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  979. long *timeo, struct sock *ssk)
  980. {
  981. struct netlink_sock *nlk;
  982. nlk = nlk_sk(sk);
  983. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  984. test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
  985. DECLARE_WAITQUEUE(wait, current);
  986. if (!*timeo) {
  987. if (!ssk || netlink_is_kernel(ssk))
  988. netlink_overrun(sk);
  989. sock_put(sk);
  990. kfree_skb(skb);
  991. return -EAGAIN;
  992. }
  993. __set_current_state(TASK_INTERRUPTIBLE);
  994. add_wait_queue(&nlk->wait, &wait);
  995. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  996. test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
  997. !sock_flag(sk, SOCK_DEAD))
  998. *timeo = schedule_timeout(*timeo);
  999. __set_current_state(TASK_RUNNING);
  1000. remove_wait_queue(&nlk->wait, &wait);
  1001. sock_put(sk);
  1002. if (signal_pending(current)) {
  1003. kfree_skb(skb);
  1004. return sock_intr_errno(*timeo);
  1005. }
  1006. return 1;
  1007. }
  1008. netlink_skb_set_owner_r(skb, sk);
  1009. return 0;
  1010. }
  1011. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1012. {
  1013. int len = skb->len;
  1014. netlink_deliver_tap(skb);
  1015. skb_queue_tail(&sk->sk_receive_queue, skb);
  1016. sk->sk_data_ready(sk);
  1017. return len;
  1018. }
  1019. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1020. {
  1021. int len = __netlink_sendskb(sk, skb);
  1022. sock_put(sk);
  1023. return len;
  1024. }
  1025. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1026. {
  1027. kfree_skb(skb);
  1028. sock_put(sk);
  1029. }
  1030. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1031. {
  1032. int delta;
  1033. WARN_ON(skb->sk != NULL);
  1034. delta = skb->end - skb->tail;
  1035. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1036. return skb;
  1037. if (skb_shared(skb)) {
  1038. struct sk_buff *nskb = skb_clone(skb, allocation);
  1039. if (!nskb)
  1040. return skb;
  1041. consume_skb(skb);
  1042. skb = nskb;
  1043. }
  1044. pskb_expand_head(skb, 0, -delta,
  1045. (allocation & ~__GFP_DIRECT_RECLAIM) |
  1046. __GFP_NOWARN | __GFP_NORETRY);
  1047. return skb;
  1048. }
  1049. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1050. struct sock *ssk)
  1051. {
  1052. int ret;
  1053. struct netlink_sock *nlk = nlk_sk(sk);
  1054. ret = -ECONNREFUSED;
  1055. if (nlk->netlink_rcv != NULL) {
  1056. ret = skb->len;
  1057. netlink_skb_set_owner_r(skb, sk);
  1058. NETLINK_CB(skb).sk = ssk;
  1059. netlink_deliver_tap_kernel(sk, ssk, skb);
  1060. nlk->netlink_rcv(skb);
  1061. consume_skb(skb);
  1062. } else {
  1063. kfree_skb(skb);
  1064. }
  1065. sock_put(sk);
  1066. return ret;
  1067. }
  1068. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1069. u32 portid, int nonblock)
  1070. {
  1071. struct sock *sk;
  1072. int err;
  1073. long timeo;
  1074. skb = netlink_trim(skb, gfp_any());
  1075. timeo = sock_sndtimeo(ssk, nonblock);
  1076. retry:
  1077. sk = netlink_getsockbyportid(ssk, portid);
  1078. if (IS_ERR(sk)) {
  1079. kfree_skb(skb);
  1080. return PTR_ERR(sk);
  1081. }
  1082. if (netlink_is_kernel(sk))
  1083. return netlink_unicast_kernel(sk, skb, ssk);
  1084. if (sk_filter(sk, skb)) {
  1085. err = skb->len;
  1086. kfree_skb(skb);
  1087. sock_put(sk);
  1088. return err;
  1089. }
  1090. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1091. if (err == 1)
  1092. goto retry;
  1093. if (err)
  1094. return err;
  1095. return netlink_sendskb(sk, skb);
  1096. }
  1097. EXPORT_SYMBOL(netlink_unicast);
  1098. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1099. {
  1100. int res = 0;
  1101. struct listeners *listeners;
  1102. BUG_ON(!netlink_is_kernel(sk));
  1103. rcu_read_lock();
  1104. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1105. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1106. res = test_bit(group - 1, listeners->masks);
  1107. rcu_read_unlock();
  1108. return res;
  1109. }
  1110. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1111. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1112. {
  1113. struct netlink_sock *nlk = nlk_sk(sk);
  1114. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1115. !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
  1116. netlink_skb_set_owner_r(skb, sk);
  1117. __netlink_sendskb(sk, skb);
  1118. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1119. }
  1120. return -1;
  1121. }
  1122. struct netlink_broadcast_data {
  1123. struct sock *exclude_sk;
  1124. struct net *net;
  1125. u32 portid;
  1126. u32 group;
  1127. int failure;
  1128. int delivery_failure;
  1129. int congested;
  1130. int delivered;
  1131. gfp_t allocation;
  1132. struct sk_buff *skb, *skb2;
  1133. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1134. void *tx_data;
  1135. };
  1136. static void do_one_broadcast(struct sock *sk,
  1137. struct netlink_broadcast_data *p)
  1138. {
  1139. struct netlink_sock *nlk = nlk_sk(sk);
  1140. int val;
  1141. if (p->exclude_sk == sk)
  1142. return;
  1143. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1144. !test_bit(p->group - 1, nlk->groups))
  1145. return;
  1146. if (!net_eq(sock_net(sk), p->net)) {
  1147. if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
  1148. return;
  1149. if (!peernet_has_id(sock_net(sk), p->net))
  1150. return;
  1151. if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
  1152. CAP_NET_BROADCAST))
  1153. return;
  1154. }
  1155. if (p->failure) {
  1156. netlink_overrun(sk);
  1157. return;
  1158. }
  1159. sock_hold(sk);
  1160. if (p->skb2 == NULL) {
  1161. if (skb_shared(p->skb)) {
  1162. p->skb2 = skb_clone(p->skb, p->allocation);
  1163. } else {
  1164. p->skb2 = skb_get(p->skb);
  1165. /*
  1166. * skb ownership may have been set when
  1167. * delivered to a previous socket.
  1168. */
  1169. skb_orphan(p->skb2);
  1170. }
  1171. }
  1172. if (p->skb2 == NULL) {
  1173. netlink_overrun(sk);
  1174. /* Clone failed. Notify ALL listeners. */
  1175. p->failure = 1;
  1176. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1177. p->delivery_failure = 1;
  1178. goto out;
  1179. }
  1180. if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1181. kfree_skb(p->skb2);
  1182. p->skb2 = NULL;
  1183. goto out;
  1184. }
  1185. if (sk_filter(sk, p->skb2)) {
  1186. kfree_skb(p->skb2);
  1187. p->skb2 = NULL;
  1188. goto out;
  1189. }
  1190. NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
  1191. NETLINK_CB(p->skb2).nsid_is_set = true;
  1192. val = netlink_broadcast_deliver(sk, p->skb2);
  1193. if (val < 0) {
  1194. netlink_overrun(sk);
  1195. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1196. p->delivery_failure = 1;
  1197. } else {
  1198. p->congested |= val;
  1199. p->delivered = 1;
  1200. p->skb2 = NULL;
  1201. }
  1202. out:
  1203. sock_put(sk);
  1204. }
  1205. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1206. u32 group, gfp_t allocation,
  1207. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1208. void *filter_data)
  1209. {
  1210. struct net *net = sock_net(ssk);
  1211. struct netlink_broadcast_data info;
  1212. struct sock *sk;
  1213. skb = netlink_trim(skb, allocation);
  1214. info.exclude_sk = ssk;
  1215. info.net = net;
  1216. info.portid = portid;
  1217. info.group = group;
  1218. info.failure = 0;
  1219. info.delivery_failure = 0;
  1220. info.congested = 0;
  1221. info.delivered = 0;
  1222. info.allocation = allocation;
  1223. info.skb = skb;
  1224. info.skb2 = NULL;
  1225. info.tx_filter = filter;
  1226. info.tx_data = filter_data;
  1227. /* While we sleep in clone, do not allow to change socket list */
  1228. netlink_lock_table();
  1229. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1230. do_one_broadcast(sk, &info);
  1231. consume_skb(skb);
  1232. netlink_unlock_table();
  1233. if (info.delivery_failure) {
  1234. kfree_skb(info.skb2);
  1235. return -ENOBUFS;
  1236. }
  1237. consume_skb(info.skb2);
  1238. if (info.delivered) {
  1239. if (info.congested && gfpflags_allow_blocking(allocation))
  1240. yield();
  1241. return 0;
  1242. }
  1243. return -ESRCH;
  1244. }
  1245. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1246. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1247. u32 group, gfp_t allocation)
  1248. {
  1249. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1250. NULL, NULL);
  1251. }
  1252. EXPORT_SYMBOL(netlink_broadcast);
  1253. struct netlink_set_err_data {
  1254. struct sock *exclude_sk;
  1255. u32 portid;
  1256. u32 group;
  1257. int code;
  1258. };
  1259. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1260. {
  1261. struct netlink_sock *nlk = nlk_sk(sk);
  1262. int ret = 0;
  1263. if (sk == p->exclude_sk)
  1264. goto out;
  1265. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1266. goto out;
  1267. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1268. !test_bit(p->group - 1, nlk->groups))
  1269. goto out;
  1270. if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
  1271. ret = 1;
  1272. goto out;
  1273. }
  1274. sk->sk_err = p->code;
  1275. sk->sk_error_report(sk);
  1276. out:
  1277. return ret;
  1278. }
  1279. /**
  1280. * netlink_set_err - report error to broadcast listeners
  1281. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1282. * @portid: the PORTID of a process that we want to skip (if any)
  1283. * @group: the broadcast group that will notice the error
  1284. * @code: error code, must be negative (as usual in kernelspace)
  1285. *
  1286. * This function returns the number of broadcast listeners that have set the
  1287. * NETLINK_NO_ENOBUFS socket option.
  1288. */
  1289. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1290. {
  1291. struct netlink_set_err_data info;
  1292. struct sock *sk;
  1293. int ret = 0;
  1294. info.exclude_sk = ssk;
  1295. info.portid = portid;
  1296. info.group = group;
  1297. /* sk->sk_err wants a positive error value */
  1298. info.code = -code;
  1299. read_lock(&nl_table_lock);
  1300. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1301. ret += do_one_set_err(sk, &info);
  1302. read_unlock(&nl_table_lock);
  1303. return ret;
  1304. }
  1305. EXPORT_SYMBOL(netlink_set_err);
  1306. /* must be called with netlink table grabbed */
  1307. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1308. unsigned int group,
  1309. int is_new)
  1310. {
  1311. int old, new = !!is_new, subscriptions;
  1312. old = test_bit(group - 1, nlk->groups);
  1313. subscriptions = nlk->subscriptions - old + new;
  1314. if (new)
  1315. __set_bit(group - 1, nlk->groups);
  1316. else
  1317. __clear_bit(group - 1, nlk->groups);
  1318. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1319. netlink_update_listeners(&nlk->sk);
  1320. }
  1321. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1322. char __user *optval, unsigned int optlen)
  1323. {
  1324. struct sock *sk = sock->sk;
  1325. struct netlink_sock *nlk = nlk_sk(sk);
  1326. unsigned int val = 0;
  1327. int err;
  1328. if (level != SOL_NETLINK)
  1329. return -ENOPROTOOPT;
  1330. if (optlen >= sizeof(int) &&
  1331. get_user(val, (unsigned int __user *)optval))
  1332. return -EFAULT;
  1333. switch (optname) {
  1334. case NETLINK_PKTINFO:
  1335. if (val)
  1336. nlk->flags |= NETLINK_F_RECV_PKTINFO;
  1337. else
  1338. nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
  1339. err = 0;
  1340. break;
  1341. case NETLINK_ADD_MEMBERSHIP:
  1342. case NETLINK_DROP_MEMBERSHIP: {
  1343. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1344. return -EPERM;
  1345. err = netlink_realloc_groups(sk);
  1346. if (err)
  1347. return err;
  1348. if (!val || val - 1 >= nlk->ngroups)
  1349. return -EINVAL;
  1350. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1351. err = nlk->netlink_bind(sock_net(sk), val);
  1352. if (err)
  1353. return err;
  1354. }
  1355. netlink_table_grab();
  1356. netlink_update_socket_mc(nlk, val,
  1357. optname == NETLINK_ADD_MEMBERSHIP);
  1358. netlink_table_ungrab();
  1359. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1360. nlk->netlink_unbind(sock_net(sk), val);
  1361. err = 0;
  1362. break;
  1363. }
  1364. case NETLINK_BROADCAST_ERROR:
  1365. if (val)
  1366. nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
  1367. else
  1368. nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
  1369. err = 0;
  1370. break;
  1371. case NETLINK_NO_ENOBUFS:
  1372. if (val) {
  1373. nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
  1374. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  1375. wake_up_interruptible(&nlk->wait);
  1376. } else {
  1377. nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
  1378. }
  1379. err = 0;
  1380. break;
  1381. case NETLINK_LISTEN_ALL_NSID:
  1382. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
  1383. return -EPERM;
  1384. if (val)
  1385. nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
  1386. else
  1387. nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
  1388. err = 0;
  1389. break;
  1390. case NETLINK_CAP_ACK:
  1391. if (val)
  1392. nlk->flags |= NETLINK_F_CAP_ACK;
  1393. else
  1394. nlk->flags &= ~NETLINK_F_CAP_ACK;
  1395. err = 0;
  1396. break;
  1397. default:
  1398. err = -ENOPROTOOPT;
  1399. }
  1400. return err;
  1401. }
  1402. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1403. char __user *optval, int __user *optlen)
  1404. {
  1405. struct sock *sk = sock->sk;
  1406. struct netlink_sock *nlk = nlk_sk(sk);
  1407. int len, val, err;
  1408. if (level != SOL_NETLINK)
  1409. return -ENOPROTOOPT;
  1410. if (get_user(len, optlen))
  1411. return -EFAULT;
  1412. if (len < 0)
  1413. return -EINVAL;
  1414. switch (optname) {
  1415. case NETLINK_PKTINFO:
  1416. if (len < sizeof(int))
  1417. return -EINVAL;
  1418. len = sizeof(int);
  1419. val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
  1420. if (put_user(len, optlen) ||
  1421. put_user(val, optval))
  1422. return -EFAULT;
  1423. err = 0;
  1424. break;
  1425. case NETLINK_BROADCAST_ERROR:
  1426. if (len < sizeof(int))
  1427. return -EINVAL;
  1428. len = sizeof(int);
  1429. val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
  1430. if (put_user(len, optlen) ||
  1431. put_user(val, optval))
  1432. return -EFAULT;
  1433. err = 0;
  1434. break;
  1435. case NETLINK_NO_ENOBUFS:
  1436. if (len < sizeof(int))
  1437. return -EINVAL;
  1438. len = sizeof(int);
  1439. val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
  1440. if (put_user(len, optlen) ||
  1441. put_user(val, optval))
  1442. return -EFAULT;
  1443. err = 0;
  1444. break;
  1445. case NETLINK_LIST_MEMBERSHIPS: {
  1446. int pos, idx, shift;
  1447. err = 0;
  1448. netlink_lock_table();
  1449. for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
  1450. if (len - pos < sizeof(u32))
  1451. break;
  1452. idx = pos / sizeof(unsigned long);
  1453. shift = (pos % sizeof(unsigned long)) * 8;
  1454. if (put_user((u32)(nlk->groups[idx] >> shift),
  1455. (u32 __user *)(optval + pos))) {
  1456. err = -EFAULT;
  1457. break;
  1458. }
  1459. }
  1460. if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
  1461. err = -EFAULT;
  1462. netlink_unlock_table();
  1463. break;
  1464. }
  1465. case NETLINK_CAP_ACK:
  1466. if (len < sizeof(int))
  1467. return -EINVAL;
  1468. len = sizeof(int);
  1469. val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
  1470. if (put_user(len, optlen) ||
  1471. put_user(val, optval))
  1472. return -EFAULT;
  1473. err = 0;
  1474. break;
  1475. default:
  1476. err = -ENOPROTOOPT;
  1477. }
  1478. return err;
  1479. }
  1480. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1481. {
  1482. struct nl_pktinfo info;
  1483. info.group = NETLINK_CB(skb).dst_group;
  1484. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1485. }
  1486. static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
  1487. struct sk_buff *skb)
  1488. {
  1489. if (!NETLINK_CB(skb).nsid_is_set)
  1490. return;
  1491. put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
  1492. &NETLINK_CB(skb).nsid);
  1493. }
  1494. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1495. {
  1496. struct sock *sk = sock->sk;
  1497. struct netlink_sock *nlk = nlk_sk(sk);
  1498. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1499. u32 dst_portid;
  1500. u32 dst_group;
  1501. struct sk_buff *skb;
  1502. int err;
  1503. struct scm_cookie scm;
  1504. u32 netlink_skb_flags = 0;
  1505. if (msg->msg_flags&MSG_OOB)
  1506. return -EOPNOTSUPP;
  1507. err = scm_send(sock, msg, &scm, true);
  1508. if (err < 0)
  1509. return err;
  1510. if (msg->msg_namelen) {
  1511. err = -EINVAL;
  1512. if (addr->nl_family != AF_NETLINK)
  1513. goto out;
  1514. dst_portid = addr->nl_pid;
  1515. dst_group = ffs(addr->nl_groups);
  1516. err = -EPERM;
  1517. if ((dst_group || dst_portid) &&
  1518. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1519. goto out;
  1520. netlink_skb_flags |= NETLINK_SKB_DST;
  1521. } else {
  1522. dst_portid = nlk->dst_portid;
  1523. dst_group = nlk->dst_group;
  1524. }
  1525. if (!nlk->bound) {
  1526. err = netlink_autobind(sock);
  1527. if (err)
  1528. goto out;
  1529. } else {
  1530. /* Ensure nlk is hashed and visible. */
  1531. smp_rmb();
  1532. }
  1533. err = -EMSGSIZE;
  1534. if (len > sk->sk_sndbuf - 32)
  1535. goto out;
  1536. err = -ENOBUFS;
  1537. skb = netlink_alloc_large_skb(len, dst_group);
  1538. if (skb == NULL)
  1539. goto out;
  1540. NETLINK_CB(skb).portid = nlk->portid;
  1541. NETLINK_CB(skb).dst_group = dst_group;
  1542. NETLINK_CB(skb).creds = scm.creds;
  1543. NETLINK_CB(skb).flags = netlink_skb_flags;
  1544. err = -EFAULT;
  1545. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1546. kfree_skb(skb);
  1547. goto out;
  1548. }
  1549. err = security_netlink_send(sk, skb);
  1550. if (err) {
  1551. kfree_skb(skb);
  1552. goto out;
  1553. }
  1554. if (dst_group) {
  1555. atomic_inc(&skb->users);
  1556. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1557. }
  1558. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1559. out:
  1560. scm_destroy(&scm);
  1561. return err;
  1562. }
  1563. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1564. int flags)
  1565. {
  1566. struct scm_cookie scm;
  1567. struct sock *sk = sock->sk;
  1568. struct netlink_sock *nlk = nlk_sk(sk);
  1569. int noblock = flags&MSG_DONTWAIT;
  1570. size_t copied;
  1571. struct sk_buff *skb, *data_skb;
  1572. int err, ret;
  1573. if (flags&MSG_OOB)
  1574. return -EOPNOTSUPP;
  1575. copied = 0;
  1576. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1577. if (skb == NULL)
  1578. goto out;
  1579. data_skb = skb;
  1580. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1581. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1582. /*
  1583. * If this skb has a frag_list, then here that means that we
  1584. * will have to use the frag_list skb's data for compat tasks
  1585. * and the regular skb's data for normal (non-compat) tasks.
  1586. *
  1587. * If we need to send the compat skb, assign it to the
  1588. * 'data_skb' variable so that it will be used below for data
  1589. * copying. We keep 'skb' for everything else, including
  1590. * freeing both later.
  1591. */
  1592. if (flags & MSG_CMSG_COMPAT)
  1593. data_skb = skb_shinfo(skb)->frag_list;
  1594. }
  1595. #endif
  1596. /* Record the max length of recvmsg() calls for future allocations */
  1597. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  1598. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  1599. SKB_WITH_OVERHEAD(32768));
  1600. copied = data_skb->len;
  1601. if (len < copied) {
  1602. msg->msg_flags |= MSG_TRUNC;
  1603. copied = len;
  1604. }
  1605. skb_reset_transport_header(data_skb);
  1606. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  1607. if (msg->msg_name) {
  1608. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1609. addr->nl_family = AF_NETLINK;
  1610. addr->nl_pad = 0;
  1611. addr->nl_pid = NETLINK_CB(skb).portid;
  1612. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1613. msg->msg_namelen = sizeof(*addr);
  1614. }
  1615. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  1616. netlink_cmsg_recv_pktinfo(msg, skb);
  1617. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  1618. netlink_cmsg_listen_all_nsid(sk, msg, skb);
  1619. memset(&scm, 0, sizeof(scm));
  1620. scm.creds = *NETLINK_CREDS(skb);
  1621. if (flags & MSG_TRUNC)
  1622. copied = data_skb->len;
  1623. skb_free_datagram(sk, skb);
  1624. if (nlk->cb_running &&
  1625. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1626. ret = netlink_dump(sk);
  1627. if (ret) {
  1628. sk->sk_err = -ret;
  1629. sk->sk_error_report(sk);
  1630. }
  1631. }
  1632. scm_recv(sock, msg, &scm, flags);
  1633. out:
  1634. netlink_rcv_wake(sk);
  1635. return err ? : copied;
  1636. }
  1637. static void netlink_data_ready(struct sock *sk)
  1638. {
  1639. BUG();
  1640. }
  1641. /*
  1642. * We export these functions to other modules. They provide a
  1643. * complete set of kernel non-blocking support for message
  1644. * queueing.
  1645. */
  1646. struct sock *
  1647. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  1648. struct netlink_kernel_cfg *cfg)
  1649. {
  1650. struct socket *sock;
  1651. struct sock *sk;
  1652. struct netlink_sock *nlk;
  1653. struct listeners *listeners = NULL;
  1654. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  1655. unsigned int groups;
  1656. BUG_ON(!nl_table);
  1657. if (unit < 0 || unit >= MAX_LINKS)
  1658. return NULL;
  1659. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1660. return NULL;
  1661. if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
  1662. goto out_sock_release_nosk;
  1663. sk = sock->sk;
  1664. if (!cfg || cfg->groups < 32)
  1665. groups = 32;
  1666. else
  1667. groups = cfg->groups;
  1668. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1669. if (!listeners)
  1670. goto out_sock_release;
  1671. sk->sk_data_ready = netlink_data_ready;
  1672. if (cfg && cfg->input)
  1673. nlk_sk(sk)->netlink_rcv = cfg->input;
  1674. if (netlink_insert(sk, 0))
  1675. goto out_sock_release;
  1676. nlk = nlk_sk(sk);
  1677. nlk->flags |= NETLINK_F_KERNEL_SOCKET;
  1678. netlink_table_grab();
  1679. if (!nl_table[unit].registered) {
  1680. nl_table[unit].groups = groups;
  1681. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1682. nl_table[unit].cb_mutex = cb_mutex;
  1683. nl_table[unit].module = module;
  1684. if (cfg) {
  1685. nl_table[unit].bind = cfg->bind;
  1686. nl_table[unit].unbind = cfg->unbind;
  1687. nl_table[unit].flags = cfg->flags;
  1688. if (cfg->compare)
  1689. nl_table[unit].compare = cfg->compare;
  1690. }
  1691. nl_table[unit].registered = 1;
  1692. } else {
  1693. kfree(listeners);
  1694. nl_table[unit].registered++;
  1695. }
  1696. netlink_table_ungrab();
  1697. return sk;
  1698. out_sock_release:
  1699. kfree(listeners);
  1700. netlink_kernel_release(sk);
  1701. return NULL;
  1702. out_sock_release_nosk:
  1703. sock_release(sock);
  1704. return NULL;
  1705. }
  1706. EXPORT_SYMBOL(__netlink_kernel_create);
  1707. void
  1708. netlink_kernel_release(struct sock *sk)
  1709. {
  1710. if (sk == NULL || sk->sk_socket == NULL)
  1711. return;
  1712. sock_release(sk->sk_socket);
  1713. }
  1714. EXPORT_SYMBOL(netlink_kernel_release);
  1715. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1716. {
  1717. struct listeners *new, *old;
  1718. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1719. if (groups < 32)
  1720. groups = 32;
  1721. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1722. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1723. if (!new)
  1724. return -ENOMEM;
  1725. old = nl_deref_protected(tbl->listeners);
  1726. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1727. rcu_assign_pointer(tbl->listeners, new);
  1728. kfree_rcu(old, rcu);
  1729. }
  1730. tbl->groups = groups;
  1731. return 0;
  1732. }
  1733. /**
  1734. * netlink_change_ngroups - change number of multicast groups
  1735. *
  1736. * This changes the number of multicast groups that are available
  1737. * on a certain netlink family. Note that it is not possible to
  1738. * change the number of groups to below 32. Also note that it does
  1739. * not implicitly call netlink_clear_multicast_users() when the
  1740. * number of groups is reduced.
  1741. *
  1742. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1743. * @groups: The new number of groups.
  1744. */
  1745. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1746. {
  1747. int err;
  1748. netlink_table_grab();
  1749. err = __netlink_change_ngroups(sk, groups);
  1750. netlink_table_ungrab();
  1751. return err;
  1752. }
  1753. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1754. {
  1755. struct sock *sk;
  1756. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1757. sk_for_each_bound(sk, &tbl->mc_list)
  1758. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1759. }
  1760. struct nlmsghdr *
  1761. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  1762. {
  1763. struct nlmsghdr *nlh;
  1764. int size = nlmsg_msg_size(len);
  1765. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  1766. nlh->nlmsg_type = type;
  1767. nlh->nlmsg_len = size;
  1768. nlh->nlmsg_flags = flags;
  1769. nlh->nlmsg_pid = portid;
  1770. nlh->nlmsg_seq = seq;
  1771. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  1772. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  1773. return nlh;
  1774. }
  1775. EXPORT_SYMBOL(__nlmsg_put);
  1776. /*
  1777. * It looks a bit ugly.
  1778. * It would be better to create kernel thread.
  1779. */
  1780. static int netlink_dump(struct sock *sk)
  1781. {
  1782. struct netlink_sock *nlk = nlk_sk(sk);
  1783. struct netlink_callback *cb;
  1784. struct sk_buff *skb = NULL;
  1785. struct nlmsghdr *nlh;
  1786. struct module *module;
  1787. int len, err = -ENOBUFS;
  1788. int alloc_min_size;
  1789. int alloc_size;
  1790. mutex_lock(nlk->cb_mutex);
  1791. if (!nlk->cb_running) {
  1792. err = -EINVAL;
  1793. goto errout_skb;
  1794. }
  1795. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1796. goto errout_skb;
  1797. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  1798. * required, but it makes sense to _attempt_ a 16K bytes allocation
  1799. * to reduce number of system calls on dump operations, if user
  1800. * ever provided a big enough buffer.
  1801. */
  1802. cb = &nlk->cb;
  1803. alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  1804. if (alloc_min_size < nlk->max_recvmsg_len) {
  1805. alloc_size = nlk->max_recvmsg_len;
  1806. skb = alloc_skb(alloc_size,
  1807. (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
  1808. __GFP_NOWARN | __GFP_NORETRY);
  1809. }
  1810. if (!skb) {
  1811. alloc_size = alloc_min_size;
  1812. skb = alloc_skb(alloc_size, GFP_KERNEL);
  1813. }
  1814. if (!skb)
  1815. goto errout_skb;
  1816. /* Trim skb to allocated size. User is expected to provide buffer as
  1817. * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
  1818. * netlink_recvmsg())). dump will pack as many smaller messages as
  1819. * could fit within the allocated skb. skb is typically allocated
  1820. * with larger space than required (could be as much as near 2x the
  1821. * requested size with align to next power of 2 approach). Allowing
  1822. * dump to use the excess space makes it difficult for a user to have a
  1823. * reasonable static buffer based on the expected largest dump of a
  1824. * single netdev. The outcome is MSG_TRUNC error.
  1825. */
  1826. skb_reserve(skb, skb_tailroom(skb) - alloc_size);
  1827. netlink_skb_set_owner_r(skb, sk);
  1828. len = cb->dump(skb, cb);
  1829. if (len > 0) {
  1830. mutex_unlock(nlk->cb_mutex);
  1831. if (sk_filter(sk, skb))
  1832. kfree_skb(skb);
  1833. else
  1834. __netlink_sendskb(sk, skb);
  1835. return 0;
  1836. }
  1837. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1838. if (!nlh)
  1839. goto errout_skb;
  1840. nl_dump_check_consistent(cb, nlh);
  1841. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1842. if (sk_filter(sk, skb))
  1843. kfree_skb(skb);
  1844. else
  1845. __netlink_sendskb(sk, skb);
  1846. if (cb->done)
  1847. cb->done(cb);
  1848. nlk->cb_running = false;
  1849. module = cb->module;
  1850. skb = cb->skb;
  1851. mutex_unlock(nlk->cb_mutex);
  1852. module_put(module);
  1853. consume_skb(skb);
  1854. return 0;
  1855. errout_skb:
  1856. mutex_unlock(nlk->cb_mutex);
  1857. kfree_skb(skb);
  1858. return err;
  1859. }
  1860. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1861. const struct nlmsghdr *nlh,
  1862. struct netlink_dump_control *control)
  1863. {
  1864. struct netlink_callback *cb;
  1865. struct sock *sk;
  1866. struct netlink_sock *nlk;
  1867. int ret;
  1868. atomic_inc(&skb->users);
  1869. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  1870. if (sk == NULL) {
  1871. ret = -ECONNREFUSED;
  1872. goto error_free;
  1873. }
  1874. nlk = nlk_sk(sk);
  1875. mutex_lock(nlk->cb_mutex);
  1876. /* A dump is in progress... */
  1877. if (nlk->cb_running) {
  1878. ret = -EBUSY;
  1879. goto error_unlock;
  1880. }
  1881. /* add reference of module which cb->dump belongs to */
  1882. if (!try_module_get(control->module)) {
  1883. ret = -EPROTONOSUPPORT;
  1884. goto error_unlock;
  1885. }
  1886. cb = &nlk->cb;
  1887. memset(cb, 0, sizeof(*cb));
  1888. cb->start = control->start;
  1889. cb->dump = control->dump;
  1890. cb->done = control->done;
  1891. cb->nlh = nlh;
  1892. cb->data = control->data;
  1893. cb->module = control->module;
  1894. cb->min_dump_alloc = control->min_dump_alloc;
  1895. cb->skb = skb;
  1896. nlk->cb_running = true;
  1897. mutex_unlock(nlk->cb_mutex);
  1898. if (cb->start)
  1899. cb->start(cb);
  1900. ret = netlink_dump(sk);
  1901. sock_put(sk);
  1902. if (ret)
  1903. return ret;
  1904. /* We successfully started a dump, by returning -EINTR we
  1905. * signal not to send ACK even if it was requested.
  1906. */
  1907. return -EINTR;
  1908. error_unlock:
  1909. sock_put(sk);
  1910. mutex_unlock(nlk->cb_mutex);
  1911. error_free:
  1912. kfree_skb(skb);
  1913. return ret;
  1914. }
  1915. EXPORT_SYMBOL(__netlink_dump_start);
  1916. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1917. {
  1918. struct sk_buff *skb;
  1919. struct nlmsghdr *rep;
  1920. struct nlmsgerr *errmsg;
  1921. size_t payload = sizeof(*errmsg);
  1922. struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
  1923. /* Error messages get the original request appened, unless the user
  1924. * requests to cap the error message.
  1925. */
  1926. if (!(nlk->flags & NETLINK_F_CAP_ACK) && err)
  1927. payload += nlmsg_len(nlh);
  1928. skb = nlmsg_new(payload, GFP_KERNEL);
  1929. if (!skb) {
  1930. struct sock *sk;
  1931. sk = netlink_lookup(sock_net(in_skb->sk),
  1932. in_skb->sk->sk_protocol,
  1933. NETLINK_CB(in_skb).portid);
  1934. if (sk) {
  1935. sk->sk_err = ENOBUFS;
  1936. sk->sk_error_report(sk);
  1937. sock_put(sk);
  1938. }
  1939. return;
  1940. }
  1941. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  1942. NLMSG_ERROR, payload, 0);
  1943. errmsg = nlmsg_data(rep);
  1944. errmsg->error = err;
  1945. memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
  1946. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  1947. }
  1948. EXPORT_SYMBOL(netlink_ack);
  1949. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1950. struct nlmsghdr *))
  1951. {
  1952. struct nlmsghdr *nlh;
  1953. int err;
  1954. while (skb->len >= nlmsg_total_size(0)) {
  1955. int msglen;
  1956. nlh = nlmsg_hdr(skb);
  1957. err = 0;
  1958. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1959. return 0;
  1960. /* Only requests are handled by the kernel */
  1961. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1962. goto ack;
  1963. /* Skip control messages */
  1964. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1965. goto ack;
  1966. err = cb(skb, nlh);
  1967. if (err == -EINTR)
  1968. goto skip;
  1969. ack:
  1970. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1971. netlink_ack(skb, nlh, err);
  1972. skip:
  1973. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1974. if (msglen > skb->len)
  1975. msglen = skb->len;
  1976. skb_pull(skb, msglen);
  1977. }
  1978. return 0;
  1979. }
  1980. EXPORT_SYMBOL(netlink_rcv_skb);
  1981. /**
  1982. * nlmsg_notify - send a notification netlink message
  1983. * @sk: netlink socket to use
  1984. * @skb: notification message
  1985. * @portid: destination netlink portid for reports or 0
  1986. * @group: destination multicast group or 0
  1987. * @report: 1 to report back, 0 to disable
  1988. * @flags: allocation flags
  1989. */
  1990. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  1991. unsigned int group, int report, gfp_t flags)
  1992. {
  1993. int err = 0;
  1994. if (group) {
  1995. int exclude_portid = 0;
  1996. if (report) {
  1997. atomic_inc(&skb->users);
  1998. exclude_portid = portid;
  1999. }
  2000. /* errors reported via destination sk->sk_err, but propagate
  2001. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2002. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2003. }
  2004. if (report) {
  2005. int err2;
  2006. err2 = nlmsg_unicast(sk, skb, portid);
  2007. if (!err || err == -ESRCH)
  2008. err = err2;
  2009. }
  2010. return err;
  2011. }
  2012. EXPORT_SYMBOL(nlmsg_notify);
  2013. #ifdef CONFIG_PROC_FS
  2014. struct nl_seq_iter {
  2015. struct seq_net_private p;
  2016. struct rhashtable_iter hti;
  2017. int link;
  2018. };
  2019. static int netlink_walk_start(struct nl_seq_iter *iter)
  2020. {
  2021. int err;
  2022. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
  2023. GFP_KERNEL);
  2024. if (err) {
  2025. iter->link = MAX_LINKS;
  2026. return err;
  2027. }
  2028. err = rhashtable_walk_start(&iter->hti);
  2029. return err == -EAGAIN ? 0 : err;
  2030. }
  2031. static void netlink_walk_stop(struct nl_seq_iter *iter)
  2032. {
  2033. rhashtable_walk_stop(&iter->hti);
  2034. rhashtable_walk_exit(&iter->hti);
  2035. }
  2036. static void *__netlink_seq_next(struct seq_file *seq)
  2037. {
  2038. struct nl_seq_iter *iter = seq->private;
  2039. struct netlink_sock *nlk;
  2040. do {
  2041. for (;;) {
  2042. int err;
  2043. nlk = rhashtable_walk_next(&iter->hti);
  2044. if (IS_ERR(nlk)) {
  2045. if (PTR_ERR(nlk) == -EAGAIN)
  2046. continue;
  2047. return nlk;
  2048. }
  2049. if (nlk)
  2050. break;
  2051. netlink_walk_stop(iter);
  2052. if (++iter->link >= MAX_LINKS)
  2053. return NULL;
  2054. err = netlink_walk_start(iter);
  2055. if (err)
  2056. return ERR_PTR(err);
  2057. }
  2058. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2059. return nlk;
  2060. }
  2061. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2062. {
  2063. struct nl_seq_iter *iter = seq->private;
  2064. void *obj = SEQ_START_TOKEN;
  2065. loff_t pos;
  2066. int err;
  2067. iter->link = 0;
  2068. err = netlink_walk_start(iter);
  2069. if (err)
  2070. return ERR_PTR(err);
  2071. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2072. obj = __netlink_seq_next(seq);
  2073. return obj;
  2074. }
  2075. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2076. {
  2077. ++*pos;
  2078. return __netlink_seq_next(seq);
  2079. }
  2080. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2081. {
  2082. struct nl_seq_iter *iter = seq->private;
  2083. if (iter->link >= MAX_LINKS)
  2084. return;
  2085. netlink_walk_stop(iter);
  2086. }
  2087. static int netlink_seq_show(struct seq_file *seq, void *v)
  2088. {
  2089. if (v == SEQ_START_TOKEN) {
  2090. seq_puts(seq,
  2091. "sk Eth Pid Groups "
  2092. "Rmem Wmem Dump Locks Drops Inode\n");
  2093. } else {
  2094. struct sock *s = v;
  2095. struct netlink_sock *nlk = nlk_sk(s);
  2096. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2097. s,
  2098. s->sk_protocol,
  2099. nlk->portid,
  2100. nlk->groups ? (u32)nlk->groups[0] : 0,
  2101. sk_rmem_alloc_get(s),
  2102. sk_wmem_alloc_get(s),
  2103. nlk->cb_running,
  2104. atomic_read(&s->sk_refcnt),
  2105. atomic_read(&s->sk_drops),
  2106. sock_i_ino(s)
  2107. );
  2108. }
  2109. return 0;
  2110. }
  2111. static const struct seq_operations netlink_seq_ops = {
  2112. .start = netlink_seq_start,
  2113. .next = netlink_seq_next,
  2114. .stop = netlink_seq_stop,
  2115. .show = netlink_seq_show,
  2116. };
  2117. static int netlink_seq_open(struct inode *inode, struct file *file)
  2118. {
  2119. return seq_open_net(inode, file, &netlink_seq_ops,
  2120. sizeof(struct nl_seq_iter));
  2121. }
  2122. static const struct file_operations netlink_seq_fops = {
  2123. .owner = THIS_MODULE,
  2124. .open = netlink_seq_open,
  2125. .read = seq_read,
  2126. .llseek = seq_lseek,
  2127. .release = seq_release_net,
  2128. };
  2129. #endif
  2130. int netlink_register_notifier(struct notifier_block *nb)
  2131. {
  2132. return blocking_notifier_chain_register(&netlink_chain, nb);
  2133. }
  2134. EXPORT_SYMBOL(netlink_register_notifier);
  2135. int netlink_unregister_notifier(struct notifier_block *nb)
  2136. {
  2137. return blocking_notifier_chain_unregister(&netlink_chain, nb);
  2138. }
  2139. EXPORT_SYMBOL(netlink_unregister_notifier);
  2140. static const struct proto_ops netlink_ops = {
  2141. .family = PF_NETLINK,
  2142. .owner = THIS_MODULE,
  2143. .release = netlink_release,
  2144. .bind = netlink_bind,
  2145. .connect = netlink_connect,
  2146. .socketpair = sock_no_socketpair,
  2147. .accept = sock_no_accept,
  2148. .getname = netlink_getname,
  2149. .poll = datagram_poll,
  2150. .ioctl = netlink_ioctl,
  2151. .listen = sock_no_listen,
  2152. .shutdown = sock_no_shutdown,
  2153. .setsockopt = netlink_setsockopt,
  2154. .getsockopt = netlink_getsockopt,
  2155. .sendmsg = netlink_sendmsg,
  2156. .recvmsg = netlink_recvmsg,
  2157. .mmap = sock_no_mmap,
  2158. .sendpage = sock_no_sendpage,
  2159. };
  2160. static const struct net_proto_family netlink_family_ops = {
  2161. .family = PF_NETLINK,
  2162. .create = netlink_create,
  2163. .owner = THIS_MODULE, /* for consistency 8) */
  2164. };
  2165. static int __net_init netlink_net_init(struct net *net)
  2166. {
  2167. #ifdef CONFIG_PROC_FS
  2168. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2169. return -ENOMEM;
  2170. #endif
  2171. return 0;
  2172. }
  2173. static void __net_exit netlink_net_exit(struct net *net)
  2174. {
  2175. #ifdef CONFIG_PROC_FS
  2176. remove_proc_entry("netlink", net->proc_net);
  2177. #endif
  2178. }
  2179. static void __init netlink_add_usersock_entry(void)
  2180. {
  2181. struct listeners *listeners;
  2182. int groups = 32;
  2183. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2184. if (!listeners)
  2185. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2186. netlink_table_grab();
  2187. nl_table[NETLINK_USERSOCK].groups = groups;
  2188. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2189. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2190. nl_table[NETLINK_USERSOCK].registered = 1;
  2191. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2192. netlink_table_ungrab();
  2193. }
  2194. static struct pernet_operations __net_initdata netlink_net_ops = {
  2195. .init = netlink_net_init,
  2196. .exit = netlink_net_exit,
  2197. };
  2198. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2199. {
  2200. const struct netlink_sock *nlk = data;
  2201. struct netlink_compare_arg arg;
  2202. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2203. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2204. }
  2205. static const struct rhashtable_params netlink_rhashtable_params = {
  2206. .head_offset = offsetof(struct netlink_sock, node),
  2207. .key_len = netlink_compare_arg_len,
  2208. .obj_hashfn = netlink_hash,
  2209. .obj_cmpfn = netlink_compare,
  2210. .automatic_shrinking = true,
  2211. };
  2212. static int __init netlink_proto_init(void)
  2213. {
  2214. int i;
  2215. int err = proto_register(&netlink_proto, 0);
  2216. if (err != 0)
  2217. goto out;
  2218. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2219. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2220. if (!nl_table)
  2221. goto panic;
  2222. for (i = 0; i < MAX_LINKS; i++) {
  2223. if (rhashtable_init(&nl_table[i].hash,
  2224. &netlink_rhashtable_params) < 0) {
  2225. while (--i > 0)
  2226. rhashtable_destroy(&nl_table[i].hash);
  2227. kfree(nl_table);
  2228. goto panic;
  2229. }
  2230. }
  2231. INIT_LIST_HEAD(&netlink_tap_all);
  2232. netlink_add_usersock_entry();
  2233. sock_register(&netlink_family_ops);
  2234. register_pernet_subsys(&netlink_net_ops);
  2235. /* The netlink device handler may be needed early. */
  2236. rtnetlink_init();
  2237. out:
  2238. return err;
  2239. panic:
  2240. panic("netlink_init: Cannot allocate nl_table\n");
  2241. }
  2242. core_initcall(netlink_proto_init);