af_netlink.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251
  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 __read_mostly;
  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. nlk_sk(sk)->portid = 0;
  899. sock_put(sk);
  900. }
  901. err:
  902. release_sock(sk);
  903. return err;
  904. }
  905. static void netlink_remove(struct sock *sk)
  906. {
  907. struct netlink_table *table;
  908. table = &nl_table[sk->sk_protocol];
  909. if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
  910. netlink_rhashtable_params)) {
  911. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  912. __sock_put(sk);
  913. }
  914. netlink_table_grab();
  915. if (nlk_sk(sk)->subscriptions) {
  916. __sk_del_bind_node(sk);
  917. netlink_update_listeners(sk);
  918. }
  919. if (sk->sk_protocol == NETLINK_GENERIC)
  920. atomic_inc(&genl_sk_destructing_cnt);
  921. netlink_table_ungrab();
  922. }
  923. static struct proto netlink_proto = {
  924. .name = "NETLINK",
  925. .owner = THIS_MODULE,
  926. .obj_size = sizeof(struct netlink_sock),
  927. };
  928. static int __netlink_create(struct net *net, struct socket *sock,
  929. struct mutex *cb_mutex, int protocol,
  930. int kern)
  931. {
  932. struct sock *sk;
  933. struct netlink_sock *nlk;
  934. sock->ops = &netlink_ops;
  935. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
  936. if (!sk)
  937. return -ENOMEM;
  938. sock_init_data(sock, sk);
  939. nlk = nlk_sk(sk);
  940. if (cb_mutex) {
  941. nlk->cb_mutex = cb_mutex;
  942. } else {
  943. nlk->cb_mutex = &nlk->cb_def_mutex;
  944. mutex_init(nlk->cb_mutex);
  945. }
  946. init_waitqueue_head(&nlk->wait);
  947. #ifdef CONFIG_NETLINK_MMAP
  948. mutex_init(&nlk->pg_vec_lock);
  949. #endif
  950. sk->sk_destruct = netlink_sock_destruct;
  951. sk->sk_protocol = protocol;
  952. return 0;
  953. }
  954. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  955. int kern)
  956. {
  957. struct module *module = NULL;
  958. struct mutex *cb_mutex;
  959. struct netlink_sock *nlk;
  960. int (*bind)(struct net *net, int group);
  961. void (*unbind)(struct net *net, int group);
  962. int err = 0;
  963. sock->state = SS_UNCONNECTED;
  964. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  965. return -ESOCKTNOSUPPORT;
  966. if (protocol < 0 || protocol >= MAX_LINKS)
  967. return -EPROTONOSUPPORT;
  968. netlink_lock_table();
  969. #ifdef CONFIG_MODULES
  970. if (!nl_table[protocol].registered) {
  971. netlink_unlock_table();
  972. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  973. netlink_lock_table();
  974. }
  975. #endif
  976. if (nl_table[protocol].registered &&
  977. try_module_get(nl_table[protocol].module))
  978. module = nl_table[protocol].module;
  979. else
  980. err = -EPROTONOSUPPORT;
  981. cb_mutex = nl_table[protocol].cb_mutex;
  982. bind = nl_table[protocol].bind;
  983. unbind = nl_table[protocol].unbind;
  984. netlink_unlock_table();
  985. if (err < 0)
  986. goto out;
  987. err = __netlink_create(net, sock, cb_mutex, protocol, kern);
  988. if (err < 0)
  989. goto out_module;
  990. local_bh_disable();
  991. sock_prot_inuse_add(net, &netlink_proto, 1);
  992. local_bh_enable();
  993. nlk = nlk_sk(sock->sk);
  994. nlk->module = module;
  995. nlk->netlink_bind = bind;
  996. nlk->netlink_unbind = unbind;
  997. out:
  998. return err;
  999. out_module:
  1000. module_put(module);
  1001. goto out;
  1002. }
  1003. static void deferred_put_nlk_sk(struct rcu_head *head)
  1004. {
  1005. struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
  1006. sock_put(&nlk->sk);
  1007. }
  1008. static int netlink_release(struct socket *sock)
  1009. {
  1010. struct sock *sk = sock->sk;
  1011. struct netlink_sock *nlk;
  1012. if (!sk)
  1013. return 0;
  1014. netlink_remove(sk);
  1015. sock_orphan(sk);
  1016. nlk = nlk_sk(sk);
  1017. /*
  1018. * OK. Socket is unlinked, any packets that arrive now
  1019. * will be purged.
  1020. */
  1021. /* must not acquire netlink_table_lock in any way again before unbind
  1022. * and notifying genetlink is done as otherwise it might deadlock
  1023. */
  1024. if (nlk->netlink_unbind) {
  1025. int i;
  1026. for (i = 0; i < nlk->ngroups; i++)
  1027. if (test_bit(i, nlk->groups))
  1028. nlk->netlink_unbind(sock_net(sk), i + 1);
  1029. }
  1030. if (sk->sk_protocol == NETLINK_GENERIC &&
  1031. atomic_dec_return(&genl_sk_destructing_cnt) == 0)
  1032. wake_up(&genl_sk_destructing_waitq);
  1033. sock->sk = NULL;
  1034. wake_up_interruptible_all(&nlk->wait);
  1035. skb_queue_purge(&sk->sk_write_queue);
  1036. if (nlk->portid) {
  1037. struct netlink_notify n = {
  1038. .net = sock_net(sk),
  1039. .protocol = sk->sk_protocol,
  1040. .portid = nlk->portid,
  1041. };
  1042. atomic_notifier_call_chain(&netlink_chain,
  1043. NETLINK_URELEASE, &n);
  1044. }
  1045. module_put(nlk->module);
  1046. if (netlink_is_kernel(sk)) {
  1047. netlink_table_grab();
  1048. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  1049. if (--nl_table[sk->sk_protocol].registered == 0) {
  1050. struct listeners *old;
  1051. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  1052. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  1053. kfree_rcu(old, rcu);
  1054. nl_table[sk->sk_protocol].module = NULL;
  1055. nl_table[sk->sk_protocol].bind = NULL;
  1056. nl_table[sk->sk_protocol].unbind = NULL;
  1057. nl_table[sk->sk_protocol].flags = 0;
  1058. nl_table[sk->sk_protocol].registered = 0;
  1059. }
  1060. netlink_table_ungrab();
  1061. }
  1062. kfree(nlk->groups);
  1063. nlk->groups = NULL;
  1064. local_bh_disable();
  1065. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  1066. local_bh_enable();
  1067. call_rcu(&nlk->rcu, deferred_put_nlk_sk);
  1068. return 0;
  1069. }
  1070. static int netlink_autobind(struct socket *sock)
  1071. {
  1072. struct sock *sk = sock->sk;
  1073. struct net *net = sock_net(sk);
  1074. struct netlink_table *table = &nl_table[sk->sk_protocol];
  1075. s32 portid = task_tgid_vnr(current);
  1076. int err;
  1077. s32 rover = -4096;
  1078. bool ok;
  1079. retry:
  1080. cond_resched();
  1081. rcu_read_lock();
  1082. ok = !__netlink_lookup(table, portid, net);
  1083. rcu_read_unlock();
  1084. if (!ok) {
  1085. /* Bind collision, search negative portid values. */
  1086. if (rover == -4096)
  1087. /* rover will be in range [S32_MIN, -4097] */
  1088. rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
  1089. else if (rover >= -4096)
  1090. rover = -4097;
  1091. portid = rover--;
  1092. goto retry;
  1093. }
  1094. err = netlink_insert(sk, portid);
  1095. if (err == -EADDRINUSE)
  1096. goto retry;
  1097. /* If 2 threads race to autobind, that is fine. */
  1098. if (err == -EBUSY)
  1099. err = 0;
  1100. return err;
  1101. }
  1102. /**
  1103. * __netlink_ns_capable - General netlink message capability test
  1104. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  1105. * @user_ns: The user namespace of the capability to use
  1106. * @cap: The capability to use
  1107. *
  1108. * Test to see if the opener of the socket we received the message
  1109. * from had when the netlink socket was created and the sender of the
  1110. * message has has the capability @cap in the user namespace @user_ns.
  1111. */
  1112. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  1113. struct user_namespace *user_ns, int cap)
  1114. {
  1115. return ((nsp->flags & NETLINK_SKB_DST) ||
  1116. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  1117. ns_capable(user_ns, cap);
  1118. }
  1119. EXPORT_SYMBOL(__netlink_ns_capable);
  1120. /**
  1121. * netlink_ns_capable - General netlink message capability test
  1122. * @skb: socket buffer holding a netlink command from userspace
  1123. * @user_ns: The user namespace of the capability to use
  1124. * @cap: The capability to use
  1125. *
  1126. * Test to see if the opener of the socket we received the message
  1127. * from had when the netlink socket was created and the sender of the
  1128. * message has has the capability @cap in the user namespace @user_ns.
  1129. */
  1130. bool netlink_ns_capable(const struct sk_buff *skb,
  1131. struct user_namespace *user_ns, int cap)
  1132. {
  1133. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  1134. }
  1135. EXPORT_SYMBOL(netlink_ns_capable);
  1136. /**
  1137. * netlink_capable - Netlink global message capability test
  1138. * @skb: socket buffer holding a netlink command from userspace
  1139. * @cap: The capability to use
  1140. *
  1141. * Test to see if the opener of the socket we received the message
  1142. * from had when the netlink socket was created and the sender of the
  1143. * message has has the capability @cap in all user namespaces.
  1144. */
  1145. bool netlink_capable(const struct sk_buff *skb, int cap)
  1146. {
  1147. return netlink_ns_capable(skb, &init_user_ns, cap);
  1148. }
  1149. EXPORT_SYMBOL(netlink_capable);
  1150. /**
  1151. * netlink_net_capable - Netlink network namespace message capability test
  1152. * @skb: socket buffer holding a netlink command from userspace
  1153. * @cap: The capability to use
  1154. *
  1155. * Test to see if the opener of the socket we received the message
  1156. * from had when the netlink socket was created and the sender of the
  1157. * message has has the capability @cap over the network namespace of
  1158. * the socket we received the message from.
  1159. */
  1160. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  1161. {
  1162. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  1163. }
  1164. EXPORT_SYMBOL(netlink_net_capable);
  1165. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  1166. {
  1167. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  1168. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  1169. }
  1170. static void
  1171. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  1172. {
  1173. struct netlink_sock *nlk = nlk_sk(sk);
  1174. if (nlk->subscriptions && !subscriptions)
  1175. __sk_del_bind_node(sk);
  1176. else if (!nlk->subscriptions && subscriptions)
  1177. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  1178. nlk->subscriptions = subscriptions;
  1179. }
  1180. static int netlink_realloc_groups(struct sock *sk)
  1181. {
  1182. struct netlink_sock *nlk = nlk_sk(sk);
  1183. unsigned int groups;
  1184. unsigned long *new_groups;
  1185. int err = 0;
  1186. netlink_table_grab();
  1187. groups = nl_table[sk->sk_protocol].groups;
  1188. if (!nl_table[sk->sk_protocol].registered) {
  1189. err = -ENOENT;
  1190. goto out_unlock;
  1191. }
  1192. if (nlk->ngroups >= groups)
  1193. goto out_unlock;
  1194. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  1195. if (new_groups == NULL) {
  1196. err = -ENOMEM;
  1197. goto out_unlock;
  1198. }
  1199. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  1200. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  1201. nlk->groups = new_groups;
  1202. nlk->ngroups = groups;
  1203. out_unlock:
  1204. netlink_table_ungrab();
  1205. return err;
  1206. }
  1207. static void netlink_undo_bind(int group, long unsigned int groups,
  1208. struct sock *sk)
  1209. {
  1210. struct netlink_sock *nlk = nlk_sk(sk);
  1211. int undo;
  1212. if (!nlk->netlink_unbind)
  1213. return;
  1214. for (undo = 0; undo < group; undo++)
  1215. if (test_bit(undo, &groups))
  1216. nlk->netlink_unbind(sock_net(sk), undo + 1);
  1217. }
  1218. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  1219. int addr_len)
  1220. {
  1221. struct sock *sk = sock->sk;
  1222. struct net *net = sock_net(sk);
  1223. struct netlink_sock *nlk = nlk_sk(sk);
  1224. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1225. int err;
  1226. long unsigned int groups = nladdr->nl_groups;
  1227. if (addr_len < sizeof(struct sockaddr_nl))
  1228. return -EINVAL;
  1229. if (nladdr->nl_family != AF_NETLINK)
  1230. return -EINVAL;
  1231. /* Only superuser is allowed to listen multicasts */
  1232. if (groups) {
  1233. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1234. return -EPERM;
  1235. err = netlink_realloc_groups(sk);
  1236. if (err)
  1237. return err;
  1238. }
  1239. if (nlk->portid)
  1240. if (nladdr->nl_pid != nlk->portid)
  1241. return -EINVAL;
  1242. if (nlk->netlink_bind && groups) {
  1243. int group;
  1244. for (group = 0; group < nlk->ngroups; group++) {
  1245. if (!test_bit(group, &groups))
  1246. continue;
  1247. err = nlk->netlink_bind(net, group + 1);
  1248. if (!err)
  1249. continue;
  1250. netlink_undo_bind(group, groups, sk);
  1251. return err;
  1252. }
  1253. }
  1254. if (!nlk->portid) {
  1255. err = nladdr->nl_pid ?
  1256. netlink_insert(sk, nladdr->nl_pid) :
  1257. netlink_autobind(sock);
  1258. if (err) {
  1259. netlink_undo_bind(nlk->ngroups, groups, sk);
  1260. return err;
  1261. }
  1262. }
  1263. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  1264. return 0;
  1265. netlink_table_grab();
  1266. netlink_update_subscriptions(sk, nlk->subscriptions +
  1267. hweight32(groups) -
  1268. hweight32(nlk->groups[0]));
  1269. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  1270. netlink_update_listeners(sk);
  1271. netlink_table_ungrab();
  1272. return 0;
  1273. }
  1274. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  1275. int alen, int flags)
  1276. {
  1277. int err = 0;
  1278. struct sock *sk = sock->sk;
  1279. struct netlink_sock *nlk = nlk_sk(sk);
  1280. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1281. if (alen < sizeof(addr->sa_family))
  1282. return -EINVAL;
  1283. if (addr->sa_family == AF_UNSPEC) {
  1284. sk->sk_state = NETLINK_UNCONNECTED;
  1285. nlk->dst_portid = 0;
  1286. nlk->dst_group = 0;
  1287. return 0;
  1288. }
  1289. if (addr->sa_family != AF_NETLINK)
  1290. return -EINVAL;
  1291. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  1292. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1293. return -EPERM;
  1294. if (!nlk->portid)
  1295. err = netlink_autobind(sock);
  1296. if (err == 0) {
  1297. sk->sk_state = NETLINK_CONNECTED;
  1298. nlk->dst_portid = nladdr->nl_pid;
  1299. nlk->dst_group = ffs(nladdr->nl_groups);
  1300. }
  1301. return err;
  1302. }
  1303. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  1304. int *addr_len, int peer)
  1305. {
  1306. struct sock *sk = sock->sk;
  1307. struct netlink_sock *nlk = nlk_sk(sk);
  1308. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  1309. nladdr->nl_family = AF_NETLINK;
  1310. nladdr->nl_pad = 0;
  1311. *addr_len = sizeof(*nladdr);
  1312. if (peer) {
  1313. nladdr->nl_pid = nlk->dst_portid;
  1314. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  1315. } else {
  1316. nladdr->nl_pid = nlk->portid;
  1317. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  1318. }
  1319. return 0;
  1320. }
  1321. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  1322. {
  1323. struct sock *sock;
  1324. struct netlink_sock *nlk;
  1325. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  1326. if (!sock)
  1327. return ERR_PTR(-ECONNREFUSED);
  1328. /* Don't bother queuing skb if kernel socket has no input function */
  1329. nlk = nlk_sk(sock);
  1330. if (sock->sk_state == NETLINK_CONNECTED &&
  1331. nlk->dst_portid != nlk_sk(ssk)->portid) {
  1332. sock_put(sock);
  1333. return ERR_PTR(-ECONNREFUSED);
  1334. }
  1335. return sock;
  1336. }
  1337. struct sock *netlink_getsockbyfilp(struct file *filp)
  1338. {
  1339. struct inode *inode = file_inode(filp);
  1340. struct sock *sock;
  1341. if (!S_ISSOCK(inode->i_mode))
  1342. return ERR_PTR(-ENOTSOCK);
  1343. sock = SOCKET_I(inode)->sk;
  1344. if (sock->sk_family != AF_NETLINK)
  1345. return ERR_PTR(-EINVAL);
  1346. sock_hold(sock);
  1347. return sock;
  1348. }
  1349. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  1350. int broadcast)
  1351. {
  1352. struct sk_buff *skb;
  1353. void *data;
  1354. if (size <= NLMSG_GOODSIZE || broadcast)
  1355. return alloc_skb(size, GFP_KERNEL);
  1356. size = SKB_DATA_ALIGN(size) +
  1357. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1358. data = vmalloc(size);
  1359. if (data == NULL)
  1360. return NULL;
  1361. skb = __build_skb(data, size);
  1362. if (skb == NULL)
  1363. vfree(data);
  1364. else
  1365. skb->destructor = netlink_skb_destructor;
  1366. return skb;
  1367. }
  1368. /*
  1369. * Attach a skb to a netlink socket.
  1370. * The caller must hold a reference to the destination socket. On error, the
  1371. * reference is dropped. The skb is not send to the destination, just all
  1372. * all error checks are performed and memory in the queue is reserved.
  1373. * Return values:
  1374. * < 0: error. skb freed, reference to sock dropped.
  1375. * 0: continue
  1376. * 1: repeat lookup - reference dropped while waiting for socket memory.
  1377. */
  1378. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  1379. long *timeo, struct sock *ssk)
  1380. {
  1381. struct netlink_sock *nlk;
  1382. nlk = nlk_sk(sk);
  1383. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1384. test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
  1385. !netlink_skb_is_mmaped(skb)) {
  1386. DECLARE_WAITQUEUE(wait, current);
  1387. if (!*timeo) {
  1388. if (!ssk || netlink_is_kernel(ssk))
  1389. netlink_overrun(sk);
  1390. sock_put(sk);
  1391. kfree_skb(skb);
  1392. return -EAGAIN;
  1393. }
  1394. __set_current_state(TASK_INTERRUPTIBLE);
  1395. add_wait_queue(&nlk->wait, &wait);
  1396. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1397. test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
  1398. !sock_flag(sk, SOCK_DEAD))
  1399. *timeo = schedule_timeout(*timeo);
  1400. __set_current_state(TASK_RUNNING);
  1401. remove_wait_queue(&nlk->wait, &wait);
  1402. sock_put(sk);
  1403. if (signal_pending(current)) {
  1404. kfree_skb(skb);
  1405. return sock_intr_errno(*timeo);
  1406. }
  1407. return 1;
  1408. }
  1409. netlink_skb_set_owner_r(skb, sk);
  1410. return 0;
  1411. }
  1412. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1413. {
  1414. int len = skb->len;
  1415. netlink_deliver_tap(skb);
  1416. #ifdef CONFIG_NETLINK_MMAP
  1417. if (netlink_skb_is_mmaped(skb))
  1418. netlink_queue_mmaped_skb(sk, skb);
  1419. else if (netlink_rx_is_mmaped(sk))
  1420. netlink_ring_set_copied(sk, skb);
  1421. else
  1422. #endif /* CONFIG_NETLINK_MMAP */
  1423. skb_queue_tail(&sk->sk_receive_queue, skb);
  1424. sk->sk_data_ready(sk);
  1425. return len;
  1426. }
  1427. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1428. {
  1429. int len = __netlink_sendskb(sk, skb);
  1430. sock_put(sk);
  1431. return len;
  1432. }
  1433. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1434. {
  1435. kfree_skb(skb);
  1436. sock_put(sk);
  1437. }
  1438. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1439. {
  1440. int delta;
  1441. WARN_ON(skb->sk != NULL);
  1442. if (netlink_skb_is_mmaped(skb))
  1443. return skb;
  1444. delta = skb->end - skb->tail;
  1445. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1446. return skb;
  1447. if (skb_shared(skb)) {
  1448. struct sk_buff *nskb = skb_clone(skb, allocation);
  1449. if (!nskb)
  1450. return skb;
  1451. consume_skb(skb);
  1452. skb = nskb;
  1453. }
  1454. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1455. skb->truesize -= delta;
  1456. return skb;
  1457. }
  1458. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1459. struct sock *ssk)
  1460. {
  1461. int ret;
  1462. struct netlink_sock *nlk = nlk_sk(sk);
  1463. ret = -ECONNREFUSED;
  1464. if (nlk->netlink_rcv != NULL) {
  1465. ret = skb->len;
  1466. netlink_skb_set_owner_r(skb, sk);
  1467. NETLINK_CB(skb).sk = ssk;
  1468. netlink_deliver_tap_kernel(sk, ssk, skb);
  1469. nlk->netlink_rcv(skb);
  1470. consume_skb(skb);
  1471. } else {
  1472. kfree_skb(skb);
  1473. }
  1474. sock_put(sk);
  1475. return ret;
  1476. }
  1477. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1478. u32 portid, int nonblock)
  1479. {
  1480. struct sock *sk;
  1481. int err;
  1482. long timeo;
  1483. skb = netlink_trim(skb, gfp_any());
  1484. timeo = sock_sndtimeo(ssk, nonblock);
  1485. retry:
  1486. sk = netlink_getsockbyportid(ssk, portid);
  1487. if (IS_ERR(sk)) {
  1488. kfree_skb(skb);
  1489. return PTR_ERR(sk);
  1490. }
  1491. if (netlink_is_kernel(sk))
  1492. return netlink_unicast_kernel(sk, skb, ssk);
  1493. if (sk_filter(sk, skb)) {
  1494. err = skb->len;
  1495. kfree_skb(skb);
  1496. sock_put(sk);
  1497. return err;
  1498. }
  1499. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1500. if (err == 1)
  1501. goto retry;
  1502. if (err)
  1503. return err;
  1504. return netlink_sendskb(sk, skb);
  1505. }
  1506. EXPORT_SYMBOL(netlink_unicast);
  1507. struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
  1508. u32 dst_portid, gfp_t gfp_mask)
  1509. {
  1510. #ifdef CONFIG_NETLINK_MMAP
  1511. struct sock *sk = NULL;
  1512. struct sk_buff *skb;
  1513. struct netlink_ring *ring;
  1514. struct nl_mmap_hdr *hdr;
  1515. unsigned int maxlen;
  1516. sk = netlink_getsockbyportid(ssk, dst_portid);
  1517. if (IS_ERR(sk))
  1518. goto out;
  1519. ring = &nlk_sk(sk)->rx_ring;
  1520. /* fast-path without atomic ops for common case: non-mmaped receiver */
  1521. if (ring->pg_vec == NULL)
  1522. goto out_put;
  1523. if (ring->frame_size - NL_MMAP_HDRLEN < size)
  1524. goto out_put;
  1525. skb = alloc_skb_head(gfp_mask);
  1526. if (skb == NULL)
  1527. goto err1;
  1528. spin_lock_bh(&sk->sk_receive_queue.lock);
  1529. /* check again under lock */
  1530. if (ring->pg_vec == NULL)
  1531. goto out_free;
  1532. /* check again under lock */
  1533. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  1534. if (maxlen < size)
  1535. goto out_free;
  1536. netlink_forward_ring(ring);
  1537. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  1538. if (hdr == NULL)
  1539. goto err2;
  1540. netlink_ring_setup_skb(skb, sk, ring, hdr);
  1541. netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
  1542. atomic_inc(&ring->pending);
  1543. netlink_increment_head(ring);
  1544. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1545. return skb;
  1546. err2:
  1547. kfree_skb(skb);
  1548. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1549. netlink_overrun(sk);
  1550. err1:
  1551. sock_put(sk);
  1552. return NULL;
  1553. out_free:
  1554. kfree_skb(skb);
  1555. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1556. out_put:
  1557. sock_put(sk);
  1558. out:
  1559. #endif
  1560. return alloc_skb(size, gfp_mask);
  1561. }
  1562. EXPORT_SYMBOL_GPL(netlink_alloc_skb);
  1563. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1564. {
  1565. int res = 0;
  1566. struct listeners *listeners;
  1567. BUG_ON(!netlink_is_kernel(sk));
  1568. rcu_read_lock();
  1569. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1570. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1571. res = test_bit(group - 1, listeners->masks);
  1572. rcu_read_unlock();
  1573. return res;
  1574. }
  1575. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1576. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1577. {
  1578. struct netlink_sock *nlk = nlk_sk(sk);
  1579. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1580. !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
  1581. netlink_skb_set_owner_r(skb, sk);
  1582. __netlink_sendskb(sk, skb);
  1583. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1584. }
  1585. return -1;
  1586. }
  1587. struct netlink_broadcast_data {
  1588. struct sock *exclude_sk;
  1589. struct net *net;
  1590. u32 portid;
  1591. u32 group;
  1592. int failure;
  1593. int delivery_failure;
  1594. int congested;
  1595. int delivered;
  1596. gfp_t allocation;
  1597. struct sk_buff *skb, *skb2;
  1598. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1599. void *tx_data;
  1600. };
  1601. static void do_one_broadcast(struct sock *sk,
  1602. struct netlink_broadcast_data *p)
  1603. {
  1604. struct netlink_sock *nlk = nlk_sk(sk);
  1605. int val;
  1606. if (p->exclude_sk == sk)
  1607. return;
  1608. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1609. !test_bit(p->group - 1, nlk->groups))
  1610. return;
  1611. if (!net_eq(sock_net(sk), p->net)) {
  1612. if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
  1613. return;
  1614. if (!peernet_has_id(sock_net(sk), p->net))
  1615. return;
  1616. if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
  1617. CAP_NET_BROADCAST))
  1618. return;
  1619. }
  1620. if (p->failure) {
  1621. netlink_overrun(sk);
  1622. return;
  1623. }
  1624. sock_hold(sk);
  1625. if (p->skb2 == NULL) {
  1626. if (skb_shared(p->skb)) {
  1627. p->skb2 = skb_clone(p->skb, p->allocation);
  1628. } else {
  1629. p->skb2 = skb_get(p->skb);
  1630. /*
  1631. * skb ownership may have been set when
  1632. * delivered to a previous socket.
  1633. */
  1634. skb_orphan(p->skb2);
  1635. }
  1636. }
  1637. if (p->skb2 == NULL) {
  1638. netlink_overrun(sk);
  1639. /* Clone failed. Notify ALL listeners. */
  1640. p->failure = 1;
  1641. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1642. p->delivery_failure = 1;
  1643. goto out;
  1644. }
  1645. if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1646. kfree_skb(p->skb2);
  1647. p->skb2 = NULL;
  1648. goto out;
  1649. }
  1650. if (sk_filter(sk, p->skb2)) {
  1651. kfree_skb(p->skb2);
  1652. p->skb2 = NULL;
  1653. goto out;
  1654. }
  1655. NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
  1656. NETLINK_CB(p->skb2).nsid_is_set = true;
  1657. val = netlink_broadcast_deliver(sk, p->skb2);
  1658. if (val < 0) {
  1659. netlink_overrun(sk);
  1660. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1661. p->delivery_failure = 1;
  1662. } else {
  1663. p->congested |= val;
  1664. p->delivered = 1;
  1665. p->skb2 = NULL;
  1666. }
  1667. out:
  1668. sock_put(sk);
  1669. }
  1670. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1671. u32 group, gfp_t allocation,
  1672. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1673. void *filter_data)
  1674. {
  1675. struct net *net = sock_net(ssk);
  1676. struct netlink_broadcast_data info;
  1677. struct sock *sk;
  1678. skb = netlink_trim(skb, allocation);
  1679. info.exclude_sk = ssk;
  1680. info.net = net;
  1681. info.portid = portid;
  1682. info.group = group;
  1683. info.failure = 0;
  1684. info.delivery_failure = 0;
  1685. info.congested = 0;
  1686. info.delivered = 0;
  1687. info.allocation = allocation;
  1688. info.skb = skb;
  1689. info.skb2 = NULL;
  1690. info.tx_filter = filter;
  1691. info.tx_data = filter_data;
  1692. /* While we sleep in clone, do not allow to change socket list */
  1693. netlink_lock_table();
  1694. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1695. do_one_broadcast(sk, &info);
  1696. consume_skb(skb);
  1697. netlink_unlock_table();
  1698. if (info.delivery_failure) {
  1699. kfree_skb(info.skb2);
  1700. return -ENOBUFS;
  1701. }
  1702. consume_skb(info.skb2);
  1703. if (info.delivered) {
  1704. if (info.congested && (allocation & __GFP_WAIT))
  1705. yield();
  1706. return 0;
  1707. }
  1708. return -ESRCH;
  1709. }
  1710. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1711. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1712. u32 group, gfp_t allocation)
  1713. {
  1714. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1715. NULL, NULL);
  1716. }
  1717. EXPORT_SYMBOL(netlink_broadcast);
  1718. struct netlink_set_err_data {
  1719. struct sock *exclude_sk;
  1720. u32 portid;
  1721. u32 group;
  1722. int code;
  1723. };
  1724. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1725. {
  1726. struct netlink_sock *nlk = nlk_sk(sk);
  1727. int ret = 0;
  1728. if (sk == p->exclude_sk)
  1729. goto out;
  1730. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1731. goto out;
  1732. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1733. !test_bit(p->group - 1, nlk->groups))
  1734. goto out;
  1735. if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
  1736. ret = 1;
  1737. goto out;
  1738. }
  1739. sk->sk_err = p->code;
  1740. sk->sk_error_report(sk);
  1741. out:
  1742. return ret;
  1743. }
  1744. /**
  1745. * netlink_set_err - report error to broadcast listeners
  1746. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1747. * @portid: the PORTID of a process that we want to skip (if any)
  1748. * @group: the broadcast group that will notice the error
  1749. * @code: error code, must be negative (as usual in kernelspace)
  1750. *
  1751. * This function returns the number of broadcast listeners that have set the
  1752. * NETLINK_NO_ENOBUFS socket option.
  1753. */
  1754. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1755. {
  1756. struct netlink_set_err_data info;
  1757. struct sock *sk;
  1758. int ret = 0;
  1759. info.exclude_sk = ssk;
  1760. info.portid = portid;
  1761. info.group = group;
  1762. /* sk->sk_err wants a positive error value */
  1763. info.code = -code;
  1764. read_lock(&nl_table_lock);
  1765. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1766. ret += do_one_set_err(sk, &info);
  1767. read_unlock(&nl_table_lock);
  1768. return ret;
  1769. }
  1770. EXPORT_SYMBOL(netlink_set_err);
  1771. /* must be called with netlink table grabbed */
  1772. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1773. unsigned int group,
  1774. int is_new)
  1775. {
  1776. int old, new = !!is_new, subscriptions;
  1777. old = test_bit(group - 1, nlk->groups);
  1778. subscriptions = nlk->subscriptions - old + new;
  1779. if (new)
  1780. __set_bit(group - 1, nlk->groups);
  1781. else
  1782. __clear_bit(group - 1, nlk->groups);
  1783. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1784. netlink_update_listeners(&nlk->sk);
  1785. }
  1786. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1787. char __user *optval, unsigned int optlen)
  1788. {
  1789. struct sock *sk = sock->sk;
  1790. struct netlink_sock *nlk = nlk_sk(sk);
  1791. unsigned int val = 0;
  1792. int err;
  1793. if (level != SOL_NETLINK)
  1794. return -ENOPROTOOPT;
  1795. if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
  1796. optlen >= sizeof(int) &&
  1797. get_user(val, (unsigned int __user *)optval))
  1798. return -EFAULT;
  1799. switch (optname) {
  1800. case NETLINK_PKTINFO:
  1801. if (val)
  1802. nlk->flags |= NETLINK_F_RECV_PKTINFO;
  1803. else
  1804. nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
  1805. err = 0;
  1806. break;
  1807. case NETLINK_ADD_MEMBERSHIP:
  1808. case NETLINK_DROP_MEMBERSHIP: {
  1809. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1810. return -EPERM;
  1811. err = netlink_realloc_groups(sk);
  1812. if (err)
  1813. return err;
  1814. if (!val || val - 1 >= nlk->ngroups)
  1815. return -EINVAL;
  1816. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1817. err = nlk->netlink_bind(sock_net(sk), val);
  1818. if (err)
  1819. return err;
  1820. }
  1821. netlink_table_grab();
  1822. netlink_update_socket_mc(nlk, val,
  1823. optname == NETLINK_ADD_MEMBERSHIP);
  1824. netlink_table_ungrab();
  1825. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1826. nlk->netlink_unbind(sock_net(sk), val);
  1827. err = 0;
  1828. break;
  1829. }
  1830. case NETLINK_BROADCAST_ERROR:
  1831. if (val)
  1832. nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
  1833. else
  1834. nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
  1835. err = 0;
  1836. break;
  1837. case NETLINK_NO_ENOBUFS:
  1838. if (val) {
  1839. nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
  1840. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  1841. wake_up_interruptible(&nlk->wait);
  1842. } else {
  1843. nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
  1844. }
  1845. err = 0;
  1846. break;
  1847. #ifdef CONFIG_NETLINK_MMAP
  1848. case NETLINK_RX_RING:
  1849. case NETLINK_TX_RING: {
  1850. struct nl_mmap_req req;
  1851. /* Rings might consume more memory than queue limits, require
  1852. * CAP_NET_ADMIN.
  1853. */
  1854. if (!capable(CAP_NET_ADMIN))
  1855. return -EPERM;
  1856. if (optlen < sizeof(req))
  1857. return -EINVAL;
  1858. if (copy_from_user(&req, optval, sizeof(req)))
  1859. return -EFAULT;
  1860. err = netlink_set_ring(sk, &req, false,
  1861. optname == NETLINK_TX_RING);
  1862. break;
  1863. }
  1864. #endif /* CONFIG_NETLINK_MMAP */
  1865. case NETLINK_LISTEN_ALL_NSID:
  1866. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
  1867. return -EPERM;
  1868. if (val)
  1869. nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
  1870. else
  1871. nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
  1872. err = 0;
  1873. break;
  1874. default:
  1875. err = -ENOPROTOOPT;
  1876. }
  1877. return err;
  1878. }
  1879. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1880. char __user *optval, int __user *optlen)
  1881. {
  1882. struct sock *sk = sock->sk;
  1883. struct netlink_sock *nlk = nlk_sk(sk);
  1884. int len, val, err;
  1885. if (level != SOL_NETLINK)
  1886. return -ENOPROTOOPT;
  1887. if (get_user(len, optlen))
  1888. return -EFAULT;
  1889. if (len < 0)
  1890. return -EINVAL;
  1891. switch (optname) {
  1892. case NETLINK_PKTINFO:
  1893. if (len < sizeof(int))
  1894. return -EINVAL;
  1895. len = sizeof(int);
  1896. val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
  1897. if (put_user(len, optlen) ||
  1898. put_user(val, optval))
  1899. return -EFAULT;
  1900. err = 0;
  1901. break;
  1902. case NETLINK_BROADCAST_ERROR:
  1903. if (len < sizeof(int))
  1904. return -EINVAL;
  1905. len = sizeof(int);
  1906. val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
  1907. if (put_user(len, optlen) ||
  1908. put_user(val, optval))
  1909. return -EFAULT;
  1910. err = 0;
  1911. break;
  1912. case NETLINK_NO_ENOBUFS:
  1913. if (len < sizeof(int))
  1914. return -EINVAL;
  1915. len = sizeof(int);
  1916. val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
  1917. if (put_user(len, optlen) ||
  1918. put_user(val, optval))
  1919. return -EFAULT;
  1920. err = 0;
  1921. break;
  1922. case NETLINK_LIST_MEMBERSHIPS: {
  1923. int pos, idx, shift;
  1924. err = 0;
  1925. netlink_table_grab();
  1926. for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
  1927. if (len - pos < sizeof(u32))
  1928. break;
  1929. idx = pos / sizeof(unsigned long);
  1930. shift = (pos % sizeof(unsigned long)) * 8;
  1931. if (put_user((u32)(nlk->groups[idx] >> shift),
  1932. (u32 __user *)(optval + pos))) {
  1933. err = -EFAULT;
  1934. break;
  1935. }
  1936. }
  1937. if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
  1938. err = -EFAULT;
  1939. netlink_table_ungrab();
  1940. break;
  1941. }
  1942. default:
  1943. err = -ENOPROTOOPT;
  1944. }
  1945. return err;
  1946. }
  1947. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1948. {
  1949. struct nl_pktinfo info;
  1950. info.group = NETLINK_CB(skb).dst_group;
  1951. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1952. }
  1953. static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
  1954. struct sk_buff *skb)
  1955. {
  1956. if (!NETLINK_CB(skb).nsid_is_set)
  1957. return;
  1958. put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
  1959. &NETLINK_CB(skb).nsid);
  1960. }
  1961. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1962. {
  1963. struct sock *sk = sock->sk;
  1964. struct netlink_sock *nlk = nlk_sk(sk);
  1965. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1966. u32 dst_portid;
  1967. u32 dst_group;
  1968. struct sk_buff *skb;
  1969. int err;
  1970. struct scm_cookie scm;
  1971. u32 netlink_skb_flags = 0;
  1972. if (msg->msg_flags&MSG_OOB)
  1973. return -EOPNOTSUPP;
  1974. err = scm_send(sock, msg, &scm, true);
  1975. if (err < 0)
  1976. return err;
  1977. if (msg->msg_namelen) {
  1978. err = -EINVAL;
  1979. if (addr->nl_family != AF_NETLINK)
  1980. goto out;
  1981. dst_portid = addr->nl_pid;
  1982. dst_group = ffs(addr->nl_groups);
  1983. err = -EPERM;
  1984. if ((dst_group || dst_portid) &&
  1985. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1986. goto out;
  1987. netlink_skb_flags |= NETLINK_SKB_DST;
  1988. } else {
  1989. dst_portid = nlk->dst_portid;
  1990. dst_group = nlk->dst_group;
  1991. }
  1992. if (!nlk->portid) {
  1993. err = netlink_autobind(sock);
  1994. if (err)
  1995. goto out;
  1996. }
  1997. /* It's a really convoluted way for userland to ask for mmaped
  1998. * sendmsg(), but that's what we've got...
  1999. */
  2000. if (netlink_tx_is_mmaped(sk) &&
  2001. msg->msg_iter.type == ITER_IOVEC &&
  2002. msg->msg_iter.nr_segs == 1 &&
  2003. msg->msg_iter.iov->iov_base == NULL) {
  2004. err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
  2005. &scm);
  2006. goto out;
  2007. }
  2008. err = -EMSGSIZE;
  2009. if (len > sk->sk_sndbuf - 32)
  2010. goto out;
  2011. err = -ENOBUFS;
  2012. skb = netlink_alloc_large_skb(len, dst_group);
  2013. if (skb == NULL)
  2014. goto out;
  2015. NETLINK_CB(skb).portid = nlk->portid;
  2016. NETLINK_CB(skb).dst_group = dst_group;
  2017. NETLINK_CB(skb).creds = scm.creds;
  2018. NETLINK_CB(skb).flags = netlink_skb_flags;
  2019. err = -EFAULT;
  2020. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  2021. kfree_skb(skb);
  2022. goto out;
  2023. }
  2024. err = security_netlink_send(sk, skb);
  2025. if (err) {
  2026. kfree_skb(skb);
  2027. goto out;
  2028. }
  2029. if (dst_group) {
  2030. atomic_inc(&skb->users);
  2031. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  2032. }
  2033. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  2034. out:
  2035. scm_destroy(&scm);
  2036. return err;
  2037. }
  2038. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  2039. int flags)
  2040. {
  2041. struct scm_cookie scm;
  2042. struct sock *sk = sock->sk;
  2043. struct netlink_sock *nlk = nlk_sk(sk);
  2044. int noblock = flags&MSG_DONTWAIT;
  2045. size_t copied;
  2046. struct sk_buff *skb, *data_skb;
  2047. int err, ret;
  2048. if (flags&MSG_OOB)
  2049. return -EOPNOTSUPP;
  2050. copied = 0;
  2051. skb = skb_recv_datagram(sk, flags, noblock, &err);
  2052. if (skb == NULL)
  2053. goto out;
  2054. data_skb = skb;
  2055. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  2056. if (unlikely(skb_shinfo(skb)->frag_list)) {
  2057. /*
  2058. * If this skb has a frag_list, then here that means that we
  2059. * will have to use the frag_list skb's data for compat tasks
  2060. * and the regular skb's data for normal (non-compat) tasks.
  2061. *
  2062. * If we need to send the compat skb, assign it to the
  2063. * 'data_skb' variable so that it will be used below for data
  2064. * copying. We keep 'skb' for everything else, including
  2065. * freeing both later.
  2066. */
  2067. if (flags & MSG_CMSG_COMPAT)
  2068. data_skb = skb_shinfo(skb)->frag_list;
  2069. }
  2070. #endif
  2071. /* Record the max length of recvmsg() calls for future allocations */
  2072. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  2073. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  2074. 16384);
  2075. copied = data_skb->len;
  2076. if (len < copied) {
  2077. msg->msg_flags |= MSG_TRUNC;
  2078. copied = len;
  2079. }
  2080. skb_reset_transport_header(data_skb);
  2081. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  2082. if (msg->msg_name) {
  2083. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  2084. addr->nl_family = AF_NETLINK;
  2085. addr->nl_pad = 0;
  2086. addr->nl_pid = NETLINK_CB(skb).portid;
  2087. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  2088. msg->msg_namelen = sizeof(*addr);
  2089. }
  2090. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  2091. netlink_cmsg_recv_pktinfo(msg, skb);
  2092. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  2093. netlink_cmsg_listen_all_nsid(sk, msg, skb);
  2094. memset(&scm, 0, sizeof(scm));
  2095. scm.creds = *NETLINK_CREDS(skb);
  2096. if (flags & MSG_TRUNC)
  2097. copied = data_skb->len;
  2098. skb_free_datagram(sk, skb);
  2099. if (nlk->cb_running &&
  2100. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  2101. ret = netlink_dump(sk);
  2102. if (ret) {
  2103. sk->sk_err = -ret;
  2104. sk->sk_error_report(sk);
  2105. }
  2106. }
  2107. scm_recv(sock, msg, &scm, flags);
  2108. out:
  2109. netlink_rcv_wake(sk);
  2110. return err ? : copied;
  2111. }
  2112. static void netlink_data_ready(struct sock *sk)
  2113. {
  2114. BUG();
  2115. }
  2116. /*
  2117. * We export these functions to other modules. They provide a
  2118. * complete set of kernel non-blocking support for message
  2119. * queueing.
  2120. */
  2121. struct sock *
  2122. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  2123. struct netlink_kernel_cfg *cfg)
  2124. {
  2125. struct socket *sock;
  2126. struct sock *sk;
  2127. struct netlink_sock *nlk;
  2128. struct listeners *listeners = NULL;
  2129. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  2130. unsigned int groups;
  2131. BUG_ON(!nl_table);
  2132. if (unit < 0 || unit >= MAX_LINKS)
  2133. return NULL;
  2134. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  2135. return NULL;
  2136. if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
  2137. goto out_sock_release_nosk;
  2138. sk = sock->sk;
  2139. if (!cfg || cfg->groups < 32)
  2140. groups = 32;
  2141. else
  2142. groups = cfg->groups;
  2143. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2144. if (!listeners)
  2145. goto out_sock_release;
  2146. sk->sk_data_ready = netlink_data_ready;
  2147. if (cfg && cfg->input)
  2148. nlk_sk(sk)->netlink_rcv = cfg->input;
  2149. if (netlink_insert(sk, 0))
  2150. goto out_sock_release;
  2151. nlk = nlk_sk(sk);
  2152. nlk->flags |= NETLINK_F_KERNEL_SOCKET;
  2153. netlink_table_grab();
  2154. if (!nl_table[unit].registered) {
  2155. nl_table[unit].groups = groups;
  2156. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  2157. nl_table[unit].cb_mutex = cb_mutex;
  2158. nl_table[unit].module = module;
  2159. if (cfg) {
  2160. nl_table[unit].bind = cfg->bind;
  2161. nl_table[unit].unbind = cfg->unbind;
  2162. nl_table[unit].flags = cfg->flags;
  2163. if (cfg->compare)
  2164. nl_table[unit].compare = cfg->compare;
  2165. }
  2166. nl_table[unit].registered = 1;
  2167. } else {
  2168. kfree(listeners);
  2169. nl_table[unit].registered++;
  2170. }
  2171. netlink_table_ungrab();
  2172. return sk;
  2173. out_sock_release:
  2174. kfree(listeners);
  2175. netlink_kernel_release(sk);
  2176. return NULL;
  2177. out_sock_release_nosk:
  2178. sock_release(sock);
  2179. return NULL;
  2180. }
  2181. EXPORT_SYMBOL(__netlink_kernel_create);
  2182. void
  2183. netlink_kernel_release(struct sock *sk)
  2184. {
  2185. if (sk == NULL || sk->sk_socket == NULL)
  2186. return;
  2187. sock_release(sk->sk_socket);
  2188. }
  2189. EXPORT_SYMBOL(netlink_kernel_release);
  2190. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2191. {
  2192. struct listeners *new, *old;
  2193. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  2194. if (groups < 32)
  2195. groups = 32;
  2196. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  2197. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  2198. if (!new)
  2199. return -ENOMEM;
  2200. old = nl_deref_protected(tbl->listeners);
  2201. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  2202. rcu_assign_pointer(tbl->listeners, new);
  2203. kfree_rcu(old, rcu);
  2204. }
  2205. tbl->groups = groups;
  2206. return 0;
  2207. }
  2208. /**
  2209. * netlink_change_ngroups - change number of multicast groups
  2210. *
  2211. * This changes the number of multicast groups that are available
  2212. * on a certain netlink family. Note that it is not possible to
  2213. * change the number of groups to below 32. Also note that it does
  2214. * not implicitly call netlink_clear_multicast_users() when the
  2215. * number of groups is reduced.
  2216. *
  2217. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  2218. * @groups: The new number of groups.
  2219. */
  2220. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2221. {
  2222. int err;
  2223. netlink_table_grab();
  2224. err = __netlink_change_ngroups(sk, groups);
  2225. netlink_table_ungrab();
  2226. return err;
  2227. }
  2228. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  2229. {
  2230. struct sock *sk;
  2231. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  2232. sk_for_each_bound(sk, &tbl->mc_list)
  2233. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  2234. }
  2235. struct nlmsghdr *
  2236. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  2237. {
  2238. struct nlmsghdr *nlh;
  2239. int size = nlmsg_msg_size(len);
  2240. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  2241. nlh->nlmsg_type = type;
  2242. nlh->nlmsg_len = size;
  2243. nlh->nlmsg_flags = flags;
  2244. nlh->nlmsg_pid = portid;
  2245. nlh->nlmsg_seq = seq;
  2246. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  2247. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  2248. return nlh;
  2249. }
  2250. EXPORT_SYMBOL(__nlmsg_put);
  2251. /*
  2252. * It looks a bit ugly.
  2253. * It would be better to create kernel thread.
  2254. */
  2255. static int netlink_dump(struct sock *sk)
  2256. {
  2257. struct netlink_sock *nlk = nlk_sk(sk);
  2258. struct netlink_callback *cb;
  2259. struct sk_buff *skb = NULL;
  2260. struct nlmsghdr *nlh;
  2261. int len, err = -ENOBUFS;
  2262. int alloc_size;
  2263. mutex_lock(nlk->cb_mutex);
  2264. if (!nlk->cb_running) {
  2265. err = -EINVAL;
  2266. goto errout_skb;
  2267. }
  2268. cb = &nlk->cb;
  2269. alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  2270. if (!netlink_rx_is_mmaped(sk) &&
  2271. atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  2272. goto errout_skb;
  2273. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  2274. * required, but it makes sense to _attempt_ a 16K bytes allocation
  2275. * to reduce number of system calls on dump operations, if user
  2276. * ever provided a big enough buffer.
  2277. */
  2278. if (alloc_size < nlk->max_recvmsg_len) {
  2279. skb = netlink_alloc_skb(sk,
  2280. nlk->max_recvmsg_len,
  2281. nlk->portid,
  2282. GFP_KERNEL |
  2283. __GFP_NOWARN |
  2284. __GFP_NORETRY);
  2285. /* available room should be exact amount to avoid MSG_TRUNC */
  2286. if (skb)
  2287. skb_reserve(skb, skb_tailroom(skb) -
  2288. nlk->max_recvmsg_len);
  2289. }
  2290. if (!skb)
  2291. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  2292. GFP_KERNEL);
  2293. if (!skb)
  2294. goto errout_skb;
  2295. netlink_skb_set_owner_r(skb, sk);
  2296. len = cb->dump(skb, cb);
  2297. if (len > 0) {
  2298. mutex_unlock(nlk->cb_mutex);
  2299. if (sk_filter(sk, skb))
  2300. kfree_skb(skb);
  2301. else
  2302. __netlink_sendskb(sk, skb);
  2303. return 0;
  2304. }
  2305. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  2306. if (!nlh)
  2307. goto errout_skb;
  2308. nl_dump_check_consistent(cb, nlh);
  2309. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  2310. if (sk_filter(sk, skb))
  2311. kfree_skb(skb);
  2312. else
  2313. __netlink_sendskb(sk, skb);
  2314. if (cb->done)
  2315. cb->done(cb);
  2316. nlk->cb_running = false;
  2317. mutex_unlock(nlk->cb_mutex);
  2318. module_put(cb->module);
  2319. consume_skb(cb->skb);
  2320. return 0;
  2321. errout_skb:
  2322. mutex_unlock(nlk->cb_mutex);
  2323. kfree_skb(skb);
  2324. return err;
  2325. }
  2326. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  2327. const struct nlmsghdr *nlh,
  2328. struct netlink_dump_control *control)
  2329. {
  2330. struct netlink_callback *cb;
  2331. struct sock *sk;
  2332. struct netlink_sock *nlk;
  2333. int ret;
  2334. /* Memory mapped dump requests need to be copied to avoid looping
  2335. * on the pending state in netlink_mmap_sendmsg() while the CB hold
  2336. * a reference to the skb.
  2337. */
  2338. if (netlink_skb_is_mmaped(skb)) {
  2339. skb = skb_copy(skb, GFP_KERNEL);
  2340. if (skb == NULL)
  2341. return -ENOBUFS;
  2342. } else
  2343. atomic_inc(&skb->users);
  2344. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  2345. if (sk == NULL) {
  2346. ret = -ECONNREFUSED;
  2347. goto error_free;
  2348. }
  2349. nlk = nlk_sk(sk);
  2350. mutex_lock(nlk->cb_mutex);
  2351. /* A dump is in progress... */
  2352. if (nlk->cb_running) {
  2353. ret = -EBUSY;
  2354. goto error_unlock;
  2355. }
  2356. /* add reference of module which cb->dump belongs to */
  2357. if (!try_module_get(control->module)) {
  2358. ret = -EPROTONOSUPPORT;
  2359. goto error_unlock;
  2360. }
  2361. cb = &nlk->cb;
  2362. memset(cb, 0, sizeof(*cb));
  2363. cb->dump = control->dump;
  2364. cb->done = control->done;
  2365. cb->nlh = nlh;
  2366. cb->data = control->data;
  2367. cb->module = control->module;
  2368. cb->min_dump_alloc = control->min_dump_alloc;
  2369. cb->skb = skb;
  2370. nlk->cb_running = true;
  2371. mutex_unlock(nlk->cb_mutex);
  2372. ret = netlink_dump(sk);
  2373. sock_put(sk);
  2374. if (ret)
  2375. return ret;
  2376. /* We successfully started a dump, by returning -EINTR we
  2377. * signal not to send ACK even if it was requested.
  2378. */
  2379. return -EINTR;
  2380. error_unlock:
  2381. sock_put(sk);
  2382. mutex_unlock(nlk->cb_mutex);
  2383. error_free:
  2384. kfree_skb(skb);
  2385. return ret;
  2386. }
  2387. EXPORT_SYMBOL(__netlink_dump_start);
  2388. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  2389. {
  2390. struct sk_buff *skb;
  2391. struct nlmsghdr *rep;
  2392. struct nlmsgerr *errmsg;
  2393. size_t payload = sizeof(*errmsg);
  2394. /* error messages get the original request appened */
  2395. if (err)
  2396. payload += nlmsg_len(nlh);
  2397. skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
  2398. NETLINK_CB(in_skb).portid, GFP_KERNEL);
  2399. if (!skb) {
  2400. struct sock *sk;
  2401. sk = netlink_lookup(sock_net(in_skb->sk),
  2402. in_skb->sk->sk_protocol,
  2403. NETLINK_CB(in_skb).portid);
  2404. if (sk) {
  2405. sk->sk_err = ENOBUFS;
  2406. sk->sk_error_report(sk);
  2407. sock_put(sk);
  2408. }
  2409. return;
  2410. }
  2411. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  2412. NLMSG_ERROR, payload, 0);
  2413. errmsg = nlmsg_data(rep);
  2414. errmsg->error = err;
  2415. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  2416. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  2417. }
  2418. EXPORT_SYMBOL(netlink_ack);
  2419. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  2420. struct nlmsghdr *))
  2421. {
  2422. struct nlmsghdr *nlh;
  2423. int err;
  2424. while (skb->len >= nlmsg_total_size(0)) {
  2425. int msglen;
  2426. nlh = nlmsg_hdr(skb);
  2427. err = 0;
  2428. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  2429. return 0;
  2430. /* Only requests are handled by the kernel */
  2431. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  2432. goto ack;
  2433. /* Skip control messages */
  2434. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  2435. goto ack;
  2436. err = cb(skb, nlh);
  2437. if (err == -EINTR)
  2438. goto skip;
  2439. ack:
  2440. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  2441. netlink_ack(skb, nlh, err);
  2442. skip:
  2443. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  2444. if (msglen > skb->len)
  2445. msglen = skb->len;
  2446. skb_pull(skb, msglen);
  2447. }
  2448. return 0;
  2449. }
  2450. EXPORT_SYMBOL(netlink_rcv_skb);
  2451. /**
  2452. * nlmsg_notify - send a notification netlink message
  2453. * @sk: netlink socket to use
  2454. * @skb: notification message
  2455. * @portid: destination netlink portid for reports or 0
  2456. * @group: destination multicast group or 0
  2457. * @report: 1 to report back, 0 to disable
  2458. * @flags: allocation flags
  2459. */
  2460. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2461. unsigned int group, int report, gfp_t flags)
  2462. {
  2463. int err = 0;
  2464. if (group) {
  2465. int exclude_portid = 0;
  2466. if (report) {
  2467. atomic_inc(&skb->users);
  2468. exclude_portid = portid;
  2469. }
  2470. /* errors reported via destination sk->sk_err, but propagate
  2471. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2472. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2473. }
  2474. if (report) {
  2475. int err2;
  2476. err2 = nlmsg_unicast(sk, skb, portid);
  2477. if (!err || err == -ESRCH)
  2478. err = err2;
  2479. }
  2480. return err;
  2481. }
  2482. EXPORT_SYMBOL(nlmsg_notify);
  2483. #ifdef CONFIG_PROC_FS
  2484. struct nl_seq_iter {
  2485. struct seq_net_private p;
  2486. struct rhashtable_iter hti;
  2487. int link;
  2488. };
  2489. static int netlink_walk_start(struct nl_seq_iter *iter)
  2490. {
  2491. int err;
  2492. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti);
  2493. if (err) {
  2494. iter->link = MAX_LINKS;
  2495. return err;
  2496. }
  2497. err = rhashtable_walk_start(&iter->hti);
  2498. return err == -EAGAIN ? 0 : err;
  2499. }
  2500. static void netlink_walk_stop(struct nl_seq_iter *iter)
  2501. {
  2502. rhashtable_walk_stop(&iter->hti);
  2503. rhashtable_walk_exit(&iter->hti);
  2504. }
  2505. static void *__netlink_seq_next(struct seq_file *seq)
  2506. {
  2507. struct nl_seq_iter *iter = seq->private;
  2508. struct netlink_sock *nlk;
  2509. do {
  2510. for (;;) {
  2511. int err;
  2512. nlk = rhashtable_walk_next(&iter->hti);
  2513. if (IS_ERR(nlk)) {
  2514. if (PTR_ERR(nlk) == -EAGAIN)
  2515. continue;
  2516. return nlk;
  2517. }
  2518. if (nlk)
  2519. break;
  2520. netlink_walk_stop(iter);
  2521. if (++iter->link >= MAX_LINKS)
  2522. return NULL;
  2523. err = netlink_walk_start(iter);
  2524. if (err)
  2525. return ERR_PTR(err);
  2526. }
  2527. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2528. return nlk;
  2529. }
  2530. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2531. {
  2532. struct nl_seq_iter *iter = seq->private;
  2533. void *obj = SEQ_START_TOKEN;
  2534. loff_t pos;
  2535. int err;
  2536. iter->link = 0;
  2537. err = netlink_walk_start(iter);
  2538. if (err)
  2539. return ERR_PTR(err);
  2540. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2541. obj = __netlink_seq_next(seq);
  2542. return obj;
  2543. }
  2544. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2545. {
  2546. ++*pos;
  2547. return __netlink_seq_next(seq);
  2548. }
  2549. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2550. {
  2551. struct nl_seq_iter *iter = seq->private;
  2552. if (iter->link >= MAX_LINKS)
  2553. return;
  2554. netlink_walk_stop(iter);
  2555. }
  2556. static int netlink_seq_show(struct seq_file *seq, void *v)
  2557. {
  2558. if (v == SEQ_START_TOKEN) {
  2559. seq_puts(seq,
  2560. "sk Eth Pid Groups "
  2561. "Rmem Wmem Dump Locks Drops Inode\n");
  2562. } else {
  2563. struct sock *s = v;
  2564. struct netlink_sock *nlk = nlk_sk(s);
  2565. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2566. s,
  2567. s->sk_protocol,
  2568. nlk->portid,
  2569. nlk->groups ? (u32)nlk->groups[0] : 0,
  2570. sk_rmem_alloc_get(s),
  2571. sk_wmem_alloc_get(s),
  2572. nlk->cb_running,
  2573. atomic_read(&s->sk_refcnt),
  2574. atomic_read(&s->sk_drops),
  2575. sock_i_ino(s)
  2576. );
  2577. }
  2578. return 0;
  2579. }
  2580. static const struct seq_operations netlink_seq_ops = {
  2581. .start = netlink_seq_start,
  2582. .next = netlink_seq_next,
  2583. .stop = netlink_seq_stop,
  2584. .show = netlink_seq_show,
  2585. };
  2586. static int netlink_seq_open(struct inode *inode, struct file *file)
  2587. {
  2588. return seq_open_net(inode, file, &netlink_seq_ops,
  2589. sizeof(struct nl_seq_iter));
  2590. }
  2591. static const struct file_operations netlink_seq_fops = {
  2592. .owner = THIS_MODULE,
  2593. .open = netlink_seq_open,
  2594. .read = seq_read,
  2595. .llseek = seq_lseek,
  2596. .release = seq_release_net,
  2597. };
  2598. #endif
  2599. int netlink_register_notifier(struct notifier_block *nb)
  2600. {
  2601. return atomic_notifier_chain_register(&netlink_chain, nb);
  2602. }
  2603. EXPORT_SYMBOL(netlink_register_notifier);
  2604. int netlink_unregister_notifier(struct notifier_block *nb)
  2605. {
  2606. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2607. }
  2608. EXPORT_SYMBOL(netlink_unregister_notifier);
  2609. static const struct proto_ops netlink_ops = {
  2610. .family = PF_NETLINK,
  2611. .owner = THIS_MODULE,
  2612. .release = netlink_release,
  2613. .bind = netlink_bind,
  2614. .connect = netlink_connect,
  2615. .socketpair = sock_no_socketpair,
  2616. .accept = sock_no_accept,
  2617. .getname = netlink_getname,
  2618. .poll = netlink_poll,
  2619. .ioctl = sock_no_ioctl,
  2620. .listen = sock_no_listen,
  2621. .shutdown = sock_no_shutdown,
  2622. .setsockopt = netlink_setsockopt,
  2623. .getsockopt = netlink_getsockopt,
  2624. .sendmsg = netlink_sendmsg,
  2625. .recvmsg = netlink_recvmsg,
  2626. .mmap = netlink_mmap,
  2627. .sendpage = sock_no_sendpage,
  2628. };
  2629. static const struct net_proto_family netlink_family_ops = {
  2630. .family = PF_NETLINK,
  2631. .create = netlink_create,
  2632. .owner = THIS_MODULE, /* for consistency 8) */
  2633. };
  2634. static int __net_init netlink_net_init(struct net *net)
  2635. {
  2636. #ifdef CONFIG_PROC_FS
  2637. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2638. return -ENOMEM;
  2639. #endif
  2640. return 0;
  2641. }
  2642. static void __net_exit netlink_net_exit(struct net *net)
  2643. {
  2644. #ifdef CONFIG_PROC_FS
  2645. remove_proc_entry("netlink", net->proc_net);
  2646. #endif
  2647. }
  2648. static void __init netlink_add_usersock_entry(void)
  2649. {
  2650. struct listeners *listeners;
  2651. int groups = 32;
  2652. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2653. if (!listeners)
  2654. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2655. netlink_table_grab();
  2656. nl_table[NETLINK_USERSOCK].groups = groups;
  2657. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2658. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2659. nl_table[NETLINK_USERSOCK].registered = 1;
  2660. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2661. netlink_table_ungrab();
  2662. }
  2663. static struct pernet_operations __net_initdata netlink_net_ops = {
  2664. .init = netlink_net_init,
  2665. .exit = netlink_net_exit,
  2666. };
  2667. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2668. {
  2669. const struct netlink_sock *nlk = data;
  2670. struct netlink_compare_arg arg;
  2671. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2672. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2673. }
  2674. static const struct rhashtable_params netlink_rhashtable_params = {
  2675. .head_offset = offsetof(struct netlink_sock, node),
  2676. .key_len = netlink_compare_arg_len,
  2677. .obj_hashfn = netlink_hash,
  2678. .obj_cmpfn = netlink_compare,
  2679. .automatic_shrinking = true,
  2680. };
  2681. static int __init netlink_proto_init(void)
  2682. {
  2683. int i;
  2684. int err = proto_register(&netlink_proto, 0);
  2685. if (err != 0)
  2686. goto out;
  2687. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2688. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2689. if (!nl_table)
  2690. goto panic;
  2691. for (i = 0; i < MAX_LINKS; i++) {
  2692. if (rhashtable_init(&nl_table[i].hash,
  2693. &netlink_rhashtable_params) < 0) {
  2694. while (--i > 0)
  2695. rhashtable_destroy(&nl_table[i].hash);
  2696. kfree(nl_table);
  2697. goto panic;
  2698. }
  2699. }
  2700. INIT_LIST_HEAD(&netlink_tap_all);
  2701. netlink_add_usersock_entry();
  2702. sock_register(&netlink_family_ops);
  2703. register_pernet_subsys(&netlink_net_ops);
  2704. /* The netlink device handler may be needed early. */
  2705. rtnetlink_init();
  2706. out:
  2707. return err;
  2708. panic:
  2709. panic("netlink_init: Cannot allocate nl_table\n");
  2710. }
  2711. core_initcall(netlink_proto_init);