af_netlink.c 70 KB

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