af_netlink.c 77 KB

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