af_netlink.c 68 KB

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