af_netlink.c 77 KB

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