af_netlink.c 74 KB

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