af_netlink.c 74 KB

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