af_netlink.c 65 KB

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