af_netlink.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  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, unsigned int nm_len)
  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 + 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. smp_mb();
  460. hdr->nm_status = status;
  461. flush_dcache_page(pgvec_to_page(hdr));
  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. int err = 0, len = 0;
  595. mutex_lock(&nlk->pg_vec_lock);
  596. ring = &nlk->tx_ring;
  597. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  598. do {
  599. unsigned int nm_len;
  600. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
  601. if (hdr == NULL) {
  602. if (!(msg->msg_flags & MSG_DONTWAIT) &&
  603. atomic_read(&nlk->tx_ring.pending))
  604. schedule();
  605. continue;
  606. }
  607. nm_len = ACCESS_ONCE(hdr->nm_len);
  608. if (nm_len > maxlen) {
  609. err = -EINVAL;
  610. goto out;
  611. }
  612. netlink_frame_flush_dcache(hdr, nm_len);
  613. skb = alloc_skb(nm_len, GFP_KERNEL);
  614. if (skb == NULL) {
  615. err = -ENOBUFS;
  616. goto out;
  617. }
  618. __skb_put(skb, nm_len);
  619. memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, nm_len);
  620. netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
  621. netlink_increment_head(ring);
  622. NETLINK_CB(skb).portid = nlk->portid;
  623. NETLINK_CB(skb).dst_group = dst_group;
  624. NETLINK_CB(skb).creds = siocb->scm->creds;
  625. err = security_netlink_send(sk, skb);
  626. if (err) {
  627. kfree_skb(skb);
  628. goto out;
  629. }
  630. if (unlikely(dst_group)) {
  631. atomic_inc(&skb->users);
  632. netlink_broadcast(sk, skb, dst_portid, dst_group,
  633. GFP_KERNEL);
  634. }
  635. err = netlink_unicast(sk, skb, dst_portid,
  636. msg->msg_flags & MSG_DONTWAIT);
  637. if (err < 0)
  638. goto out;
  639. len += err;
  640. } while (hdr != NULL ||
  641. (!(msg->msg_flags & MSG_DONTWAIT) &&
  642. atomic_read(&nlk->tx_ring.pending)));
  643. if (len > 0)
  644. err = len;
  645. out:
  646. mutex_unlock(&nlk->pg_vec_lock);
  647. return err;
  648. }
  649. static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
  650. {
  651. struct nl_mmap_hdr *hdr;
  652. hdr = netlink_mmap_hdr(skb);
  653. hdr->nm_len = skb->len;
  654. hdr->nm_group = NETLINK_CB(skb).dst_group;
  655. hdr->nm_pid = NETLINK_CB(skb).creds.pid;
  656. hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
  657. hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
  658. netlink_frame_flush_dcache(hdr, hdr->nm_len);
  659. netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
  660. NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
  661. kfree_skb(skb);
  662. }
  663. static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
  664. {
  665. struct netlink_sock *nlk = nlk_sk(sk);
  666. struct netlink_ring *ring = &nlk->rx_ring;
  667. struct nl_mmap_hdr *hdr;
  668. spin_lock_bh(&sk->sk_receive_queue.lock);
  669. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  670. if (hdr == NULL) {
  671. spin_unlock_bh(&sk->sk_receive_queue.lock);
  672. kfree_skb(skb);
  673. netlink_overrun(sk);
  674. return;
  675. }
  676. netlink_increment_head(ring);
  677. __skb_queue_tail(&sk->sk_receive_queue, skb);
  678. spin_unlock_bh(&sk->sk_receive_queue.lock);
  679. hdr->nm_len = skb->len;
  680. hdr->nm_group = NETLINK_CB(skb).dst_group;
  681. hdr->nm_pid = NETLINK_CB(skb).creds.pid;
  682. hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
  683. hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
  684. netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
  685. }
  686. #else /* CONFIG_NETLINK_MMAP */
  687. #define netlink_skb_is_mmaped(skb) false
  688. #define netlink_rx_is_mmaped(sk) false
  689. #define netlink_tx_is_mmaped(sk) false
  690. #define netlink_mmap sock_no_mmap
  691. #define netlink_poll datagram_poll
  692. #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
  693. #endif /* CONFIG_NETLINK_MMAP */
  694. static void netlink_skb_destructor(struct sk_buff *skb)
  695. {
  696. #ifdef CONFIG_NETLINK_MMAP
  697. struct nl_mmap_hdr *hdr;
  698. struct netlink_ring *ring;
  699. struct sock *sk;
  700. /* If a packet from the kernel to userspace was freed because of an
  701. * error without being delivered to userspace, the kernel must reset
  702. * the status. In the direction userspace to kernel, the status is
  703. * always reset here after the packet was processed and freed.
  704. */
  705. if (netlink_skb_is_mmaped(skb)) {
  706. hdr = netlink_mmap_hdr(skb);
  707. sk = NETLINK_CB(skb).sk;
  708. if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
  709. netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
  710. ring = &nlk_sk(sk)->tx_ring;
  711. } else {
  712. if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
  713. hdr->nm_len = 0;
  714. netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
  715. }
  716. ring = &nlk_sk(sk)->rx_ring;
  717. }
  718. WARN_ON(atomic_read(&ring->pending) == 0);
  719. atomic_dec(&ring->pending);
  720. sock_put(sk);
  721. skb->head = NULL;
  722. }
  723. #endif
  724. if (is_vmalloc_addr(skb->head)) {
  725. if (!skb->cloned ||
  726. !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
  727. vfree(skb->head);
  728. skb->head = NULL;
  729. }
  730. if (skb->sk != NULL)
  731. sock_rfree(skb);
  732. }
  733. static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  734. {
  735. WARN_ON(skb->sk != NULL);
  736. skb->sk = sk;
  737. skb->destructor = netlink_skb_destructor;
  738. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  739. sk_mem_charge(sk, skb->truesize);
  740. }
  741. static void netlink_sock_destruct(struct sock *sk)
  742. {
  743. struct netlink_sock *nlk = nlk_sk(sk);
  744. if (nlk->cb_running) {
  745. if (nlk->cb.done)
  746. nlk->cb.done(&nlk->cb);
  747. module_put(nlk->cb.module);
  748. kfree_skb(nlk->cb.skb);
  749. }
  750. skb_queue_purge(&sk->sk_receive_queue);
  751. #ifdef CONFIG_NETLINK_MMAP
  752. if (1) {
  753. struct nl_mmap_req req;
  754. memset(&req, 0, sizeof(req));
  755. if (nlk->rx_ring.pg_vec)
  756. netlink_set_ring(sk, &req, true, false);
  757. memset(&req, 0, sizeof(req));
  758. if (nlk->tx_ring.pg_vec)
  759. netlink_set_ring(sk, &req, true, true);
  760. }
  761. #endif /* CONFIG_NETLINK_MMAP */
  762. if (!sock_flag(sk, SOCK_DEAD)) {
  763. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  764. return;
  765. }
  766. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  767. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  768. WARN_ON(nlk_sk(sk)->groups);
  769. }
  770. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  771. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  772. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  773. * this, _but_ remember, it adds useless work on UP machines.
  774. */
  775. void netlink_table_grab(void)
  776. __acquires(nl_table_lock)
  777. {
  778. might_sleep();
  779. write_lock_irq(&nl_table_lock);
  780. if (atomic_read(&nl_table_users)) {
  781. DECLARE_WAITQUEUE(wait, current);
  782. add_wait_queue_exclusive(&nl_table_wait, &wait);
  783. for (;;) {
  784. set_current_state(TASK_UNINTERRUPTIBLE);
  785. if (atomic_read(&nl_table_users) == 0)
  786. break;
  787. write_unlock_irq(&nl_table_lock);
  788. schedule();
  789. write_lock_irq(&nl_table_lock);
  790. }
  791. __set_current_state(TASK_RUNNING);
  792. remove_wait_queue(&nl_table_wait, &wait);
  793. }
  794. }
  795. void netlink_table_ungrab(void)
  796. __releases(nl_table_lock)
  797. {
  798. write_unlock_irq(&nl_table_lock);
  799. wake_up(&nl_table_wait);
  800. }
  801. static inline void
  802. netlink_lock_table(void)
  803. {
  804. /* read_lock() synchronizes us to netlink_table_grab */
  805. read_lock(&nl_table_lock);
  806. atomic_inc(&nl_table_users);
  807. read_unlock(&nl_table_lock);
  808. }
  809. static inline void
  810. netlink_unlock_table(void)
  811. {
  812. if (atomic_dec_and_test(&nl_table_users))
  813. wake_up(&nl_table_wait);
  814. }
  815. struct netlink_compare_arg
  816. {
  817. struct net *net;
  818. u32 portid;
  819. };
  820. static bool netlink_compare(void *ptr, void *arg)
  821. {
  822. struct netlink_compare_arg *x = arg;
  823. struct sock *sk = ptr;
  824. return nlk_sk(sk)->portid == x->portid &&
  825. net_eq(sock_net(sk), x->net);
  826. }
  827. static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
  828. struct net *net)
  829. {
  830. struct netlink_compare_arg arg = {
  831. .net = net,
  832. .portid = portid,
  833. };
  834. u32 hash;
  835. hash = rhashtable_hashfn(&table->hash, &portid, sizeof(portid));
  836. return rhashtable_lookup_compare(&table->hash, hash,
  837. &netlink_compare, &arg);
  838. }
  839. static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
  840. {
  841. struct netlink_table *table = &nl_table[protocol];
  842. struct sock *sk;
  843. read_lock(&nl_table_lock);
  844. rcu_read_lock();
  845. sk = __netlink_lookup(table, portid, net);
  846. if (sk)
  847. sock_hold(sk);
  848. rcu_read_unlock();
  849. read_unlock(&nl_table_lock);
  850. return sk;
  851. }
  852. static const struct proto_ops netlink_ops;
  853. static void
  854. netlink_update_listeners(struct sock *sk)
  855. {
  856. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  857. unsigned long mask;
  858. unsigned int i;
  859. struct listeners *listeners;
  860. listeners = nl_deref_protected(tbl->listeners);
  861. if (!listeners)
  862. return;
  863. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  864. mask = 0;
  865. sk_for_each_bound(sk, &tbl->mc_list) {
  866. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  867. mask |= nlk_sk(sk)->groups[i];
  868. }
  869. listeners->masks[i] = mask;
  870. }
  871. /* this function is only called with the netlink table "grabbed", which
  872. * makes sure updates are visible before bind or setsockopt return. */
  873. }
  874. static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
  875. {
  876. struct netlink_table *table = &nl_table[sk->sk_protocol];
  877. int err = -EADDRINUSE;
  878. mutex_lock(&nl_sk_hash_lock);
  879. if (__netlink_lookup(table, portid, net))
  880. goto err;
  881. err = -EBUSY;
  882. if (nlk_sk(sk)->portid)
  883. goto err;
  884. err = -ENOMEM;
  885. if (BITS_PER_LONG > 32 && unlikely(table->hash.nelems >= UINT_MAX))
  886. goto err;
  887. nlk_sk(sk)->portid = portid;
  888. sock_hold(sk);
  889. rhashtable_insert(&table->hash, &nlk_sk(sk)->node);
  890. err = 0;
  891. err:
  892. mutex_unlock(&nl_sk_hash_lock);
  893. return err;
  894. }
  895. static void netlink_remove(struct sock *sk)
  896. {
  897. struct netlink_table *table;
  898. mutex_lock(&nl_sk_hash_lock);
  899. table = &nl_table[sk->sk_protocol];
  900. if (rhashtable_remove(&table->hash, &nlk_sk(sk)->node)) {
  901. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  902. __sock_put(sk);
  903. }
  904. mutex_unlock(&nl_sk_hash_lock);
  905. netlink_table_grab();
  906. if (nlk_sk(sk)->subscriptions) {
  907. __sk_del_bind_node(sk);
  908. netlink_update_listeners(sk);
  909. }
  910. netlink_table_ungrab();
  911. }
  912. static struct proto netlink_proto = {
  913. .name = "NETLINK",
  914. .owner = THIS_MODULE,
  915. .obj_size = sizeof(struct netlink_sock),
  916. };
  917. static int __netlink_create(struct net *net, struct socket *sock,
  918. struct mutex *cb_mutex, int protocol)
  919. {
  920. struct sock *sk;
  921. struct netlink_sock *nlk;
  922. sock->ops = &netlink_ops;
  923. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  924. if (!sk)
  925. return -ENOMEM;
  926. sock_init_data(sock, sk);
  927. nlk = nlk_sk(sk);
  928. if (cb_mutex) {
  929. nlk->cb_mutex = cb_mutex;
  930. } else {
  931. nlk->cb_mutex = &nlk->cb_def_mutex;
  932. mutex_init(nlk->cb_mutex);
  933. }
  934. init_waitqueue_head(&nlk->wait);
  935. #ifdef CONFIG_NETLINK_MMAP
  936. mutex_init(&nlk->pg_vec_lock);
  937. #endif
  938. sk->sk_destruct = netlink_sock_destruct;
  939. sk->sk_protocol = protocol;
  940. return 0;
  941. }
  942. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  943. int kern)
  944. {
  945. struct module *module = NULL;
  946. struct mutex *cb_mutex;
  947. struct netlink_sock *nlk;
  948. int (*bind)(struct net *net, int group);
  949. void (*unbind)(struct net *net, int group);
  950. int err = 0;
  951. sock->state = SS_UNCONNECTED;
  952. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  953. return -ESOCKTNOSUPPORT;
  954. if (protocol < 0 || protocol >= MAX_LINKS)
  955. return -EPROTONOSUPPORT;
  956. netlink_lock_table();
  957. #ifdef CONFIG_MODULES
  958. if (!nl_table[protocol].registered) {
  959. netlink_unlock_table();
  960. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  961. netlink_lock_table();
  962. }
  963. #endif
  964. if (nl_table[protocol].registered &&
  965. try_module_get(nl_table[protocol].module))
  966. module = nl_table[protocol].module;
  967. else
  968. err = -EPROTONOSUPPORT;
  969. cb_mutex = nl_table[protocol].cb_mutex;
  970. bind = nl_table[protocol].bind;
  971. unbind = nl_table[protocol].unbind;
  972. netlink_unlock_table();
  973. if (err < 0)
  974. goto out;
  975. err = __netlink_create(net, sock, cb_mutex, protocol);
  976. if (err < 0)
  977. goto out_module;
  978. local_bh_disable();
  979. sock_prot_inuse_add(net, &netlink_proto, 1);
  980. local_bh_enable();
  981. nlk = nlk_sk(sock->sk);
  982. nlk->module = module;
  983. nlk->netlink_bind = bind;
  984. nlk->netlink_unbind = unbind;
  985. out:
  986. return err;
  987. out_module:
  988. module_put(module);
  989. goto out;
  990. }
  991. static int netlink_release(struct socket *sock)
  992. {
  993. struct sock *sk = sock->sk;
  994. struct netlink_sock *nlk;
  995. if (!sk)
  996. return 0;
  997. netlink_remove(sk);
  998. sock_orphan(sk);
  999. nlk = nlk_sk(sk);
  1000. /*
  1001. * OK. Socket is unlinked, any packets that arrive now
  1002. * will be purged.
  1003. */
  1004. sock->sk = NULL;
  1005. wake_up_interruptible_all(&nlk->wait);
  1006. skb_queue_purge(&sk->sk_write_queue);
  1007. if (nlk->portid) {
  1008. struct netlink_notify n = {
  1009. .net = sock_net(sk),
  1010. .protocol = sk->sk_protocol,
  1011. .portid = nlk->portid,
  1012. };
  1013. atomic_notifier_call_chain(&netlink_chain,
  1014. NETLINK_URELEASE, &n);
  1015. }
  1016. module_put(nlk->module);
  1017. if (netlink_is_kernel(sk)) {
  1018. netlink_table_grab();
  1019. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  1020. if (--nl_table[sk->sk_protocol].registered == 0) {
  1021. struct listeners *old;
  1022. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  1023. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  1024. kfree_rcu(old, rcu);
  1025. nl_table[sk->sk_protocol].module = NULL;
  1026. nl_table[sk->sk_protocol].bind = NULL;
  1027. nl_table[sk->sk_protocol].unbind = NULL;
  1028. nl_table[sk->sk_protocol].flags = 0;
  1029. nl_table[sk->sk_protocol].registered = 0;
  1030. }
  1031. netlink_table_ungrab();
  1032. }
  1033. if (nlk->netlink_unbind) {
  1034. int i;
  1035. for (i = 0; i < nlk->ngroups; i++)
  1036. if (test_bit(i, nlk->groups))
  1037. nlk->netlink_unbind(sock_net(sk), i + 1);
  1038. }
  1039. kfree(nlk->groups);
  1040. nlk->groups = NULL;
  1041. local_bh_disable();
  1042. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  1043. local_bh_enable();
  1044. sock_put(sk);
  1045. return 0;
  1046. }
  1047. static int netlink_autobind(struct socket *sock)
  1048. {
  1049. struct sock *sk = sock->sk;
  1050. struct net *net = sock_net(sk);
  1051. struct netlink_table *table = &nl_table[sk->sk_protocol];
  1052. s32 portid = task_tgid_vnr(current);
  1053. int err;
  1054. static s32 rover = -4097;
  1055. retry:
  1056. cond_resched();
  1057. netlink_table_grab();
  1058. rcu_read_lock();
  1059. if (__netlink_lookup(table, portid, net)) {
  1060. /* Bind collision, search negative portid values. */
  1061. portid = rover--;
  1062. if (rover > -4097)
  1063. rover = -4097;
  1064. rcu_read_unlock();
  1065. netlink_table_ungrab();
  1066. goto retry;
  1067. }
  1068. rcu_read_unlock();
  1069. netlink_table_ungrab();
  1070. err = netlink_insert(sk, net, portid);
  1071. if (err == -EADDRINUSE)
  1072. goto retry;
  1073. /* If 2 threads race to autobind, that is fine. */
  1074. if (err == -EBUSY)
  1075. err = 0;
  1076. return err;
  1077. }
  1078. /**
  1079. * __netlink_ns_capable - General netlink message capability test
  1080. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  1081. * @user_ns: The user namespace of the capability to use
  1082. * @cap: The capability to use
  1083. *
  1084. * Test to see if the opener of the socket we received the message
  1085. * from had when the netlink socket was created and the sender of the
  1086. * message has has the capability @cap in the user namespace @user_ns.
  1087. */
  1088. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  1089. struct user_namespace *user_ns, int cap)
  1090. {
  1091. return ((nsp->flags & NETLINK_SKB_DST) ||
  1092. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  1093. ns_capable(user_ns, cap);
  1094. }
  1095. EXPORT_SYMBOL(__netlink_ns_capable);
  1096. /**
  1097. * netlink_ns_capable - General netlink message capability test
  1098. * @skb: socket buffer holding a netlink command from userspace
  1099. * @user_ns: The user namespace of the capability to use
  1100. * @cap: The capability to use
  1101. *
  1102. * Test to see if the opener of the socket we received the message
  1103. * from had when the netlink socket was created and the sender of the
  1104. * message has has the capability @cap in the user namespace @user_ns.
  1105. */
  1106. bool netlink_ns_capable(const struct sk_buff *skb,
  1107. struct user_namespace *user_ns, int cap)
  1108. {
  1109. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  1110. }
  1111. EXPORT_SYMBOL(netlink_ns_capable);
  1112. /**
  1113. * netlink_capable - Netlink global message capability test
  1114. * @skb: socket buffer holding a netlink command from userspace
  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 all user namespaces.
  1120. */
  1121. bool netlink_capable(const struct sk_buff *skb, int cap)
  1122. {
  1123. return netlink_ns_capable(skb, &init_user_ns, cap);
  1124. }
  1125. EXPORT_SYMBOL(netlink_capable);
  1126. /**
  1127. * netlink_net_capable - Netlink network namespace message capability test
  1128. * @skb: socket buffer holding a netlink command from userspace
  1129. * @cap: The capability to use
  1130. *
  1131. * Test to see if the opener of the socket we received the message
  1132. * from had when the netlink socket was created and the sender of the
  1133. * message has has the capability @cap over the network namespace of
  1134. * the socket we received the message from.
  1135. */
  1136. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  1137. {
  1138. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  1139. }
  1140. EXPORT_SYMBOL(netlink_net_capable);
  1141. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  1142. {
  1143. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  1144. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  1145. }
  1146. static void
  1147. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  1148. {
  1149. struct netlink_sock *nlk = nlk_sk(sk);
  1150. if (nlk->subscriptions && !subscriptions)
  1151. __sk_del_bind_node(sk);
  1152. else if (!nlk->subscriptions && subscriptions)
  1153. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  1154. nlk->subscriptions = subscriptions;
  1155. }
  1156. static int netlink_realloc_groups(struct sock *sk)
  1157. {
  1158. struct netlink_sock *nlk = nlk_sk(sk);
  1159. unsigned int groups;
  1160. unsigned long *new_groups;
  1161. int err = 0;
  1162. netlink_table_grab();
  1163. groups = nl_table[sk->sk_protocol].groups;
  1164. if (!nl_table[sk->sk_protocol].registered) {
  1165. err = -ENOENT;
  1166. goto out_unlock;
  1167. }
  1168. if (nlk->ngroups >= groups)
  1169. goto out_unlock;
  1170. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  1171. if (new_groups == NULL) {
  1172. err = -ENOMEM;
  1173. goto out_unlock;
  1174. }
  1175. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  1176. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  1177. nlk->groups = new_groups;
  1178. nlk->ngroups = groups;
  1179. out_unlock:
  1180. netlink_table_ungrab();
  1181. return err;
  1182. }
  1183. static void netlink_undo_bind(int group, long unsigned int groups,
  1184. struct sock *sk)
  1185. {
  1186. struct netlink_sock *nlk = nlk_sk(sk);
  1187. int undo;
  1188. if (!nlk->netlink_unbind)
  1189. return;
  1190. for (undo = 0; undo < group; undo++)
  1191. if (test_bit(undo, &groups))
  1192. nlk->netlink_unbind(sock_net(sk), undo);
  1193. }
  1194. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  1195. int addr_len)
  1196. {
  1197. struct sock *sk = sock->sk;
  1198. struct net *net = sock_net(sk);
  1199. struct netlink_sock *nlk = nlk_sk(sk);
  1200. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1201. int err;
  1202. long unsigned int groups = nladdr->nl_groups;
  1203. if (addr_len < sizeof(struct sockaddr_nl))
  1204. return -EINVAL;
  1205. if (nladdr->nl_family != AF_NETLINK)
  1206. return -EINVAL;
  1207. /* Only superuser is allowed to listen multicasts */
  1208. if (groups) {
  1209. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1210. return -EPERM;
  1211. err = netlink_realloc_groups(sk);
  1212. if (err)
  1213. return err;
  1214. }
  1215. if (nlk->portid)
  1216. if (nladdr->nl_pid != nlk->portid)
  1217. return -EINVAL;
  1218. if (nlk->netlink_bind && groups) {
  1219. int group;
  1220. for (group = 0; group < nlk->ngroups; group++) {
  1221. if (!test_bit(group, &groups))
  1222. continue;
  1223. err = nlk->netlink_bind(net, group);
  1224. if (!err)
  1225. continue;
  1226. netlink_undo_bind(group, groups, sk);
  1227. return err;
  1228. }
  1229. }
  1230. if (!nlk->portid) {
  1231. err = nladdr->nl_pid ?
  1232. netlink_insert(sk, net, nladdr->nl_pid) :
  1233. netlink_autobind(sock);
  1234. if (err) {
  1235. netlink_undo_bind(nlk->ngroups, groups, sk);
  1236. return err;
  1237. }
  1238. }
  1239. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  1240. return 0;
  1241. netlink_table_grab();
  1242. netlink_update_subscriptions(sk, nlk->subscriptions +
  1243. hweight32(groups) -
  1244. hweight32(nlk->groups[0]));
  1245. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  1246. netlink_update_listeners(sk);
  1247. netlink_table_ungrab();
  1248. return 0;
  1249. }
  1250. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  1251. int alen, int flags)
  1252. {
  1253. int err = 0;
  1254. struct sock *sk = sock->sk;
  1255. struct netlink_sock *nlk = nlk_sk(sk);
  1256. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  1257. if (alen < sizeof(addr->sa_family))
  1258. return -EINVAL;
  1259. if (addr->sa_family == AF_UNSPEC) {
  1260. sk->sk_state = NETLINK_UNCONNECTED;
  1261. nlk->dst_portid = 0;
  1262. nlk->dst_group = 0;
  1263. return 0;
  1264. }
  1265. if (addr->sa_family != AF_NETLINK)
  1266. return -EINVAL;
  1267. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  1268. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1269. return -EPERM;
  1270. if (!nlk->portid)
  1271. err = netlink_autobind(sock);
  1272. if (err == 0) {
  1273. sk->sk_state = NETLINK_CONNECTED;
  1274. nlk->dst_portid = nladdr->nl_pid;
  1275. nlk->dst_group = ffs(nladdr->nl_groups);
  1276. }
  1277. return err;
  1278. }
  1279. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  1280. int *addr_len, int peer)
  1281. {
  1282. struct sock *sk = sock->sk;
  1283. struct netlink_sock *nlk = nlk_sk(sk);
  1284. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  1285. nladdr->nl_family = AF_NETLINK;
  1286. nladdr->nl_pad = 0;
  1287. *addr_len = sizeof(*nladdr);
  1288. if (peer) {
  1289. nladdr->nl_pid = nlk->dst_portid;
  1290. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  1291. } else {
  1292. nladdr->nl_pid = nlk->portid;
  1293. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  1294. }
  1295. return 0;
  1296. }
  1297. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  1298. {
  1299. struct sock *sock;
  1300. struct netlink_sock *nlk;
  1301. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  1302. if (!sock)
  1303. return ERR_PTR(-ECONNREFUSED);
  1304. /* Don't bother queuing skb if kernel socket has no input function */
  1305. nlk = nlk_sk(sock);
  1306. if (sock->sk_state == NETLINK_CONNECTED &&
  1307. nlk->dst_portid != nlk_sk(ssk)->portid) {
  1308. sock_put(sock);
  1309. return ERR_PTR(-ECONNREFUSED);
  1310. }
  1311. return sock;
  1312. }
  1313. struct sock *netlink_getsockbyfilp(struct file *filp)
  1314. {
  1315. struct inode *inode = file_inode(filp);
  1316. struct sock *sock;
  1317. if (!S_ISSOCK(inode->i_mode))
  1318. return ERR_PTR(-ENOTSOCK);
  1319. sock = SOCKET_I(inode)->sk;
  1320. if (sock->sk_family != AF_NETLINK)
  1321. return ERR_PTR(-EINVAL);
  1322. sock_hold(sock);
  1323. return sock;
  1324. }
  1325. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  1326. int broadcast)
  1327. {
  1328. struct sk_buff *skb;
  1329. void *data;
  1330. if (size <= NLMSG_GOODSIZE || broadcast)
  1331. return alloc_skb(size, GFP_KERNEL);
  1332. size = SKB_DATA_ALIGN(size) +
  1333. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1334. data = vmalloc(size);
  1335. if (data == NULL)
  1336. return NULL;
  1337. skb = build_skb(data, size);
  1338. if (skb == NULL)
  1339. vfree(data);
  1340. else {
  1341. skb->head_frag = 0;
  1342. skb->destructor = netlink_skb_destructor;
  1343. }
  1344. return skb;
  1345. }
  1346. /*
  1347. * Attach a skb to a netlink socket.
  1348. * The caller must hold a reference to the destination socket. On error, the
  1349. * reference is dropped. The skb is not send to the destination, just all
  1350. * all error checks are performed and memory in the queue is reserved.
  1351. * Return values:
  1352. * < 0: error. skb freed, reference to sock dropped.
  1353. * 0: continue
  1354. * 1: repeat lookup - reference dropped while waiting for socket memory.
  1355. */
  1356. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  1357. long *timeo, struct sock *ssk)
  1358. {
  1359. struct netlink_sock *nlk;
  1360. nlk = nlk_sk(sk);
  1361. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1362. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1363. !netlink_skb_is_mmaped(skb)) {
  1364. DECLARE_WAITQUEUE(wait, current);
  1365. if (!*timeo) {
  1366. if (!ssk || netlink_is_kernel(ssk))
  1367. netlink_overrun(sk);
  1368. sock_put(sk);
  1369. kfree_skb(skb);
  1370. return -EAGAIN;
  1371. }
  1372. __set_current_state(TASK_INTERRUPTIBLE);
  1373. add_wait_queue(&nlk->wait, &wait);
  1374. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  1375. test_bit(NETLINK_CONGESTED, &nlk->state)) &&
  1376. !sock_flag(sk, SOCK_DEAD))
  1377. *timeo = schedule_timeout(*timeo);
  1378. __set_current_state(TASK_RUNNING);
  1379. remove_wait_queue(&nlk->wait, &wait);
  1380. sock_put(sk);
  1381. if (signal_pending(current)) {
  1382. kfree_skb(skb);
  1383. return sock_intr_errno(*timeo);
  1384. }
  1385. return 1;
  1386. }
  1387. netlink_skb_set_owner_r(skb, sk);
  1388. return 0;
  1389. }
  1390. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1391. {
  1392. int len = skb->len;
  1393. netlink_deliver_tap(skb);
  1394. #ifdef CONFIG_NETLINK_MMAP
  1395. if (netlink_skb_is_mmaped(skb))
  1396. netlink_queue_mmaped_skb(sk, skb);
  1397. else if (netlink_rx_is_mmaped(sk))
  1398. netlink_ring_set_copied(sk, skb);
  1399. else
  1400. #endif /* CONFIG_NETLINK_MMAP */
  1401. skb_queue_tail(&sk->sk_receive_queue, skb);
  1402. sk->sk_data_ready(sk);
  1403. return len;
  1404. }
  1405. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1406. {
  1407. int len = __netlink_sendskb(sk, skb);
  1408. sock_put(sk);
  1409. return len;
  1410. }
  1411. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1412. {
  1413. kfree_skb(skb);
  1414. sock_put(sk);
  1415. }
  1416. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1417. {
  1418. int delta;
  1419. WARN_ON(skb->sk != NULL);
  1420. if (netlink_skb_is_mmaped(skb))
  1421. return skb;
  1422. delta = skb->end - skb->tail;
  1423. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1424. return skb;
  1425. if (skb_shared(skb)) {
  1426. struct sk_buff *nskb = skb_clone(skb, allocation);
  1427. if (!nskb)
  1428. return skb;
  1429. consume_skb(skb);
  1430. skb = nskb;
  1431. }
  1432. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1433. skb->truesize -= delta;
  1434. return skb;
  1435. }
  1436. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1437. struct sock *ssk)
  1438. {
  1439. int ret;
  1440. struct netlink_sock *nlk = nlk_sk(sk);
  1441. ret = -ECONNREFUSED;
  1442. if (nlk->netlink_rcv != NULL) {
  1443. ret = skb->len;
  1444. netlink_skb_set_owner_r(skb, sk);
  1445. NETLINK_CB(skb).sk = ssk;
  1446. netlink_deliver_tap_kernel(sk, ssk, skb);
  1447. nlk->netlink_rcv(skb);
  1448. consume_skb(skb);
  1449. } else {
  1450. kfree_skb(skb);
  1451. }
  1452. sock_put(sk);
  1453. return ret;
  1454. }
  1455. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1456. u32 portid, int nonblock)
  1457. {
  1458. struct sock *sk;
  1459. int err;
  1460. long timeo;
  1461. skb = netlink_trim(skb, gfp_any());
  1462. timeo = sock_sndtimeo(ssk, nonblock);
  1463. retry:
  1464. sk = netlink_getsockbyportid(ssk, portid);
  1465. if (IS_ERR(sk)) {
  1466. kfree_skb(skb);
  1467. return PTR_ERR(sk);
  1468. }
  1469. if (netlink_is_kernel(sk))
  1470. return netlink_unicast_kernel(sk, skb, ssk);
  1471. if (sk_filter(sk, skb)) {
  1472. err = skb->len;
  1473. kfree_skb(skb);
  1474. sock_put(sk);
  1475. return err;
  1476. }
  1477. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1478. if (err == 1)
  1479. goto retry;
  1480. if (err)
  1481. return err;
  1482. return netlink_sendskb(sk, skb);
  1483. }
  1484. EXPORT_SYMBOL(netlink_unicast);
  1485. struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
  1486. u32 dst_portid, gfp_t gfp_mask)
  1487. {
  1488. #ifdef CONFIG_NETLINK_MMAP
  1489. struct sock *sk = NULL;
  1490. struct sk_buff *skb;
  1491. struct netlink_ring *ring;
  1492. struct nl_mmap_hdr *hdr;
  1493. unsigned int maxlen;
  1494. sk = netlink_getsockbyportid(ssk, dst_portid);
  1495. if (IS_ERR(sk))
  1496. goto out;
  1497. ring = &nlk_sk(sk)->rx_ring;
  1498. /* fast-path without atomic ops for common case: non-mmaped receiver */
  1499. if (ring->pg_vec == NULL)
  1500. goto out_put;
  1501. if (ring->frame_size - NL_MMAP_HDRLEN < size)
  1502. goto out_put;
  1503. skb = alloc_skb_head(gfp_mask);
  1504. if (skb == NULL)
  1505. goto err1;
  1506. spin_lock_bh(&sk->sk_receive_queue.lock);
  1507. /* check again under lock */
  1508. if (ring->pg_vec == NULL)
  1509. goto out_free;
  1510. /* check again under lock */
  1511. maxlen = ring->frame_size - NL_MMAP_HDRLEN;
  1512. if (maxlen < size)
  1513. goto out_free;
  1514. netlink_forward_ring(ring);
  1515. hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
  1516. if (hdr == NULL)
  1517. goto err2;
  1518. netlink_ring_setup_skb(skb, sk, ring, hdr);
  1519. netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
  1520. atomic_inc(&ring->pending);
  1521. netlink_increment_head(ring);
  1522. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1523. return skb;
  1524. err2:
  1525. kfree_skb(skb);
  1526. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1527. netlink_overrun(sk);
  1528. err1:
  1529. sock_put(sk);
  1530. return NULL;
  1531. out_free:
  1532. kfree_skb(skb);
  1533. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1534. out_put:
  1535. sock_put(sk);
  1536. out:
  1537. #endif
  1538. return alloc_skb(size, gfp_mask);
  1539. }
  1540. EXPORT_SYMBOL_GPL(netlink_alloc_skb);
  1541. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1542. {
  1543. int res = 0;
  1544. struct listeners *listeners;
  1545. BUG_ON(!netlink_is_kernel(sk));
  1546. rcu_read_lock();
  1547. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1548. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1549. res = test_bit(group - 1, listeners->masks);
  1550. rcu_read_unlock();
  1551. return res;
  1552. }
  1553. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1554. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1555. {
  1556. struct netlink_sock *nlk = nlk_sk(sk);
  1557. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1558. !test_bit(NETLINK_CONGESTED, &nlk->state)) {
  1559. netlink_skb_set_owner_r(skb, sk);
  1560. __netlink_sendskb(sk, skb);
  1561. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1562. }
  1563. return -1;
  1564. }
  1565. struct netlink_broadcast_data {
  1566. struct sock *exclude_sk;
  1567. struct net *net;
  1568. u32 portid;
  1569. u32 group;
  1570. int failure;
  1571. int delivery_failure;
  1572. int congested;
  1573. int delivered;
  1574. gfp_t allocation;
  1575. struct sk_buff *skb, *skb2;
  1576. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1577. void *tx_data;
  1578. };
  1579. static void do_one_broadcast(struct sock *sk,
  1580. struct netlink_broadcast_data *p)
  1581. {
  1582. struct netlink_sock *nlk = nlk_sk(sk);
  1583. int val;
  1584. if (p->exclude_sk == sk)
  1585. return;
  1586. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1587. !test_bit(p->group - 1, nlk->groups))
  1588. return;
  1589. if (!net_eq(sock_net(sk), p->net))
  1590. return;
  1591. if (p->failure) {
  1592. netlink_overrun(sk);
  1593. return;
  1594. }
  1595. sock_hold(sk);
  1596. if (p->skb2 == NULL) {
  1597. if (skb_shared(p->skb)) {
  1598. p->skb2 = skb_clone(p->skb, p->allocation);
  1599. } else {
  1600. p->skb2 = skb_get(p->skb);
  1601. /*
  1602. * skb ownership may have been set when
  1603. * delivered to a previous socket.
  1604. */
  1605. skb_orphan(p->skb2);
  1606. }
  1607. }
  1608. if (p->skb2 == NULL) {
  1609. netlink_overrun(sk);
  1610. /* Clone failed. Notify ALL listeners. */
  1611. p->failure = 1;
  1612. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1613. p->delivery_failure = 1;
  1614. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1615. kfree_skb(p->skb2);
  1616. p->skb2 = NULL;
  1617. } else if (sk_filter(sk, p->skb2)) {
  1618. kfree_skb(p->skb2);
  1619. p->skb2 = NULL;
  1620. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  1621. netlink_overrun(sk);
  1622. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  1623. p->delivery_failure = 1;
  1624. } else {
  1625. p->congested |= val;
  1626. p->delivered = 1;
  1627. p->skb2 = NULL;
  1628. }
  1629. sock_put(sk);
  1630. }
  1631. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1632. u32 group, gfp_t allocation,
  1633. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1634. void *filter_data)
  1635. {
  1636. struct net *net = sock_net(ssk);
  1637. struct netlink_broadcast_data info;
  1638. struct sock *sk;
  1639. skb = netlink_trim(skb, allocation);
  1640. info.exclude_sk = ssk;
  1641. info.net = net;
  1642. info.portid = portid;
  1643. info.group = group;
  1644. info.failure = 0;
  1645. info.delivery_failure = 0;
  1646. info.congested = 0;
  1647. info.delivered = 0;
  1648. info.allocation = allocation;
  1649. info.skb = skb;
  1650. info.skb2 = NULL;
  1651. info.tx_filter = filter;
  1652. info.tx_data = filter_data;
  1653. /* While we sleep in clone, do not allow to change socket list */
  1654. netlink_lock_table();
  1655. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1656. do_one_broadcast(sk, &info);
  1657. consume_skb(skb);
  1658. netlink_unlock_table();
  1659. if (info.delivery_failure) {
  1660. kfree_skb(info.skb2);
  1661. return -ENOBUFS;
  1662. }
  1663. consume_skb(info.skb2);
  1664. if (info.delivered) {
  1665. if (info.congested && (allocation & __GFP_WAIT))
  1666. yield();
  1667. return 0;
  1668. }
  1669. return -ESRCH;
  1670. }
  1671. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1672. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1673. u32 group, gfp_t allocation)
  1674. {
  1675. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1676. NULL, NULL);
  1677. }
  1678. EXPORT_SYMBOL(netlink_broadcast);
  1679. struct netlink_set_err_data {
  1680. struct sock *exclude_sk;
  1681. u32 portid;
  1682. u32 group;
  1683. int code;
  1684. };
  1685. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1686. {
  1687. struct netlink_sock *nlk = nlk_sk(sk);
  1688. int ret = 0;
  1689. if (sk == p->exclude_sk)
  1690. goto out;
  1691. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1692. goto out;
  1693. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1694. !test_bit(p->group - 1, nlk->groups))
  1695. goto out;
  1696. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  1697. ret = 1;
  1698. goto out;
  1699. }
  1700. sk->sk_err = p->code;
  1701. sk->sk_error_report(sk);
  1702. out:
  1703. return ret;
  1704. }
  1705. /**
  1706. * netlink_set_err - report error to broadcast listeners
  1707. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1708. * @portid: the PORTID of a process that we want to skip (if any)
  1709. * @group: the broadcast group that will notice the error
  1710. * @code: error code, must be negative (as usual in kernelspace)
  1711. *
  1712. * This function returns the number of broadcast listeners that have set the
  1713. * NETLINK_RECV_NO_ENOBUFS socket option.
  1714. */
  1715. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1716. {
  1717. struct netlink_set_err_data info;
  1718. struct sock *sk;
  1719. int ret = 0;
  1720. info.exclude_sk = ssk;
  1721. info.portid = portid;
  1722. info.group = group;
  1723. /* sk->sk_err wants a positive error value */
  1724. info.code = -code;
  1725. read_lock(&nl_table_lock);
  1726. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1727. ret += do_one_set_err(sk, &info);
  1728. read_unlock(&nl_table_lock);
  1729. return ret;
  1730. }
  1731. EXPORT_SYMBOL(netlink_set_err);
  1732. /* must be called with netlink table grabbed */
  1733. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1734. unsigned int group,
  1735. int is_new)
  1736. {
  1737. int old, new = !!is_new, subscriptions;
  1738. old = test_bit(group - 1, nlk->groups);
  1739. subscriptions = nlk->subscriptions - old + new;
  1740. if (new)
  1741. __set_bit(group - 1, nlk->groups);
  1742. else
  1743. __clear_bit(group - 1, nlk->groups);
  1744. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1745. netlink_update_listeners(&nlk->sk);
  1746. }
  1747. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1748. char __user *optval, unsigned int optlen)
  1749. {
  1750. struct sock *sk = sock->sk;
  1751. struct netlink_sock *nlk = nlk_sk(sk);
  1752. unsigned int val = 0;
  1753. int err;
  1754. if (level != SOL_NETLINK)
  1755. return -ENOPROTOOPT;
  1756. if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
  1757. optlen >= sizeof(int) &&
  1758. get_user(val, (unsigned int __user *)optval))
  1759. return -EFAULT;
  1760. switch (optname) {
  1761. case NETLINK_PKTINFO:
  1762. if (val)
  1763. nlk->flags |= NETLINK_RECV_PKTINFO;
  1764. else
  1765. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1766. err = 0;
  1767. break;
  1768. case NETLINK_ADD_MEMBERSHIP:
  1769. case NETLINK_DROP_MEMBERSHIP: {
  1770. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1771. return -EPERM;
  1772. err = netlink_realloc_groups(sk);
  1773. if (err)
  1774. return err;
  1775. if (!val || val - 1 >= nlk->ngroups)
  1776. return -EINVAL;
  1777. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1778. err = nlk->netlink_bind(sock_net(sk), val);
  1779. if (err)
  1780. return err;
  1781. }
  1782. netlink_table_grab();
  1783. netlink_update_socket_mc(nlk, val,
  1784. optname == NETLINK_ADD_MEMBERSHIP);
  1785. netlink_table_ungrab();
  1786. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1787. nlk->netlink_unbind(sock_net(sk), val);
  1788. err = 0;
  1789. break;
  1790. }
  1791. case NETLINK_BROADCAST_ERROR:
  1792. if (val)
  1793. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1794. else
  1795. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1796. err = 0;
  1797. break;
  1798. case NETLINK_NO_ENOBUFS:
  1799. if (val) {
  1800. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1801. clear_bit(NETLINK_CONGESTED, &nlk->state);
  1802. wake_up_interruptible(&nlk->wait);
  1803. } else {
  1804. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1805. }
  1806. err = 0;
  1807. break;
  1808. #ifdef CONFIG_NETLINK_MMAP
  1809. case NETLINK_RX_RING:
  1810. case NETLINK_TX_RING: {
  1811. struct nl_mmap_req req;
  1812. /* Rings might consume more memory than queue limits, require
  1813. * CAP_NET_ADMIN.
  1814. */
  1815. if (!capable(CAP_NET_ADMIN))
  1816. return -EPERM;
  1817. if (optlen < sizeof(req))
  1818. return -EINVAL;
  1819. if (copy_from_user(&req, optval, sizeof(req)))
  1820. return -EFAULT;
  1821. err = netlink_set_ring(sk, &req, false,
  1822. optname == NETLINK_TX_RING);
  1823. break;
  1824. }
  1825. #endif /* CONFIG_NETLINK_MMAP */
  1826. default:
  1827. err = -ENOPROTOOPT;
  1828. }
  1829. return err;
  1830. }
  1831. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1832. char __user *optval, int __user *optlen)
  1833. {
  1834. struct sock *sk = sock->sk;
  1835. struct netlink_sock *nlk = nlk_sk(sk);
  1836. int len, val, err;
  1837. if (level != SOL_NETLINK)
  1838. return -ENOPROTOOPT;
  1839. if (get_user(len, optlen))
  1840. return -EFAULT;
  1841. if (len < 0)
  1842. return -EINVAL;
  1843. switch (optname) {
  1844. case NETLINK_PKTINFO:
  1845. if (len < sizeof(int))
  1846. return -EINVAL;
  1847. len = sizeof(int);
  1848. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1849. if (put_user(len, optlen) ||
  1850. put_user(val, optval))
  1851. return -EFAULT;
  1852. err = 0;
  1853. break;
  1854. case NETLINK_BROADCAST_ERROR:
  1855. if (len < sizeof(int))
  1856. return -EINVAL;
  1857. len = sizeof(int);
  1858. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1859. if (put_user(len, optlen) ||
  1860. put_user(val, optval))
  1861. return -EFAULT;
  1862. err = 0;
  1863. break;
  1864. case NETLINK_NO_ENOBUFS:
  1865. if (len < sizeof(int))
  1866. return -EINVAL;
  1867. len = sizeof(int);
  1868. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1869. if (put_user(len, optlen) ||
  1870. put_user(val, optval))
  1871. return -EFAULT;
  1872. err = 0;
  1873. break;
  1874. default:
  1875. err = -ENOPROTOOPT;
  1876. }
  1877. return err;
  1878. }
  1879. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1880. {
  1881. struct nl_pktinfo info;
  1882. info.group = NETLINK_CB(skb).dst_group;
  1883. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1884. }
  1885. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1886. struct msghdr *msg, size_t len)
  1887. {
  1888. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1889. struct sock *sk = sock->sk;
  1890. struct netlink_sock *nlk = nlk_sk(sk);
  1891. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1892. u32 dst_portid;
  1893. u32 dst_group;
  1894. struct sk_buff *skb;
  1895. int err;
  1896. struct scm_cookie scm;
  1897. u32 netlink_skb_flags = 0;
  1898. if (msg->msg_flags&MSG_OOB)
  1899. return -EOPNOTSUPP;
  1900. if (NULL == siocb->scm)
  1901. siocb->scm = &scm;
  1902. err = scm_send(sock, msg, siocb->scm, true);
  1903. if (err < 0)
  1904. return err;
  1905. if (msg->msg_namelen) {
  1906. err = -EINVAL;
  1907. if (addr->nl_family != AF_NETLINK)
  1908. goto out;
  1909. dst_portid = addr->nl_pid;
  1910. dst_group = ffs(addr->nl_groups);
  1911. err = -EPERM;
  1912. if ((dst_group || dst_portid) &&
  1913. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1914. goto out;
  1915. netlink_skb_flags |= NETLINK_SKB_DST;
  1916. } else {
  1917. dst_portid = nlk->dst_portid;
  1918. dst_group = nlk->dst_group;
  1919. }
  1920. if (!nlk->portid) {
  1921. err = netlink_autobind(sock);
  1922. if (err)
  1923. goto out;
  1924. }
  1925. if (netlink_tx_is_mmaped(sk) &&
  1926. msg->msg_iter.iov->iov_base == NULL) {
  1927. err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
  1928. siocb);
  1929. goto out;
  1930. }
  1931. err = -EMSGSIZE;
  1932. if (len > sk->sk_sndbuf - 32)
  1933. goto out;
  1934. err = -ENOBUFS;
  1935. skb = netlink_alloc_large_skb(len, dst_group);
  1936. if (skb == NULL)
  1937. goto out;
  1938. NETLINK_CB(skb).portid = nlk->portid;
  1939. NETLINK_CB(skb).dst_group = dst_group;
  1940. NETLINK_CB(skb).creds = siocb->scm->creds;
  1941. NETLINK_CB(skb).flags = netlink_skb_flags;
  1942. err = -EFAULT;
  1943. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1944. kfree_skb(skb);
  1945. goto out;
  1946. }
  1947. err = security_netlink_send(sk, skb);
  1948. if (err) {
  1949. kfree_skb(skb);
  1950. goto out;
  1951. }
  1952. if (dst_group) {
  1953. atomic_inc(&skb->users);
  1954. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1955. }
  1956. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1957. out:
  1958. scm_destroy(siocb->scm);
  1959. return err;
  1960. }
  1961. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1962. struct msghdr *msg, size_t len,
  1963. int flags)
  1964. {
  1965. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1966. struct scm_cookie scm;
  1967. struct sock *sk = sock->sk;
  1968. struct netlink_sock *nlk = nlk_sk(sk);
  1969. int noblock = flags&MSG_DONTWAIT;
  1970. size_t copied;
  1971. struct sk_buff *skb, *data_skb;
  1972. int err, ret;
  1973. if (flags&MSG_OOB)
  1974. return -EOPNOTSUPP;
  1975. copied = 0;
  1976. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1977. if (skb == NULL)
  1978. goto out;
  1979. data_skb = skb;
  1980. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1981. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1982. /*
  1983. * If this skb has a frag_list, then here that means that we
  1984. * will have to use the frag_list skb's data for compat tasks
  1985. * and the regular skb's data for normal (non-compat) tasks.
  1986. *
  1987. * If we need to send the compat skb, assign it to the
  1988. * 'data_skb' variable so that it will be used below for data
  1989. * copying. We keep 'skb' for everything else, including
  1990. * freeing both later.
  1991. */
  1992. if (flags & MSG_CMSG_COMPAT)
  1993. data_skb = skb_shinfo(skb)->frag_list;
  1994. }
  1995. #endif
  1996. /* Record the max length of recvmsg() calls for future allocations */
  1997. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  1998. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  1999. 16384);
  2000. copied = data_skb->len;
  2001. if (len < copied) {
  2002. msg->msg_flags |= MSG_TRUNC;
  2003. copied = len;
  2004. }
  2005. skb_reset_transport_header(data_skb);
  2006. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  2007. if (msg->msg_name) {
  2008. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  2009. addr->nl_family = AF_NETLINK;
  2010. addr->nl_pad = 0;
  2011. addr->nl_pid = NETLINK_CB(skb).portid;
  2012. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  2013. msg->msg_namelen = sizeof(*addr);
  2014. }
  2015. if (nlk->flags & NETLINK_RECV_PKTINFO)
  2016. netlink_cmsg_recv_pktinfo(msg, skb);
  2017. if (NULL == siocb->scm) {
  2018. memset(&scm, 0, sizeof(scm));
  2019. siocb->scm = &scm;
  2020. }
  2021. siocb->scm->creds = *NETLINK_CREDS(skb);
  2022. if (flags & MSG_TRUNC)
  2023. copied = data_skb->len;
  2024. skb_free_datagram(sk, skb);
  2025. if (nlk->cb_running &&
  2026. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  2027. ret = netlink_dump(sk);
  2028. if (ret) {
  2029. sk->sk_err = -ret;
  2030. sk->sk_error_report(sk);
  2031. }
  2032. }
  2033. scm_recv(sock, msg, siocb->scm, flags);
  2034. out:
  2035. netlink_rcv_wake(sk);
  2036. return err ? : copied;
  2037. }
  2038. static void netlink_data_ready(struct sock *sk)
  2039. {
  2040. BUG();
  2041. }
  2042. /*
  2043. * We export these functions to other modules. They provide a
  2044. * complete set of kernel non-blocking support for message
  2045. * queueing.
  2046. */
  2047. struct sock *
  2048. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  2049. struct netlink_kernel_cfg *cfg)
  2050. {
  2051. struct socket *sock;
  2052. struct sock *sk;
  2053. struct netlink_sock *nlk;
  2054. struct listeners *listeners = NULL;
  2055. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  2056. unsigned int groups;
  2057. BUG_ON(!nl_table);
  2058. if (unit < 0 || unit >= MAX_LINKS)
  2059. return NULL;
  2060. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  2061. return NULL;
  2062. /*
  2063. * We have to just have a reference on the net from sk, but don't
  2064. * get_net it. Besides, we cannot get and then put the net here.
  2065. * So we create one inside init_net and the move it to net.
  2066. */
  2067. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  2068. goto out_sock_release_nosk;
  2069. sk = sock->sk;
  2070. sk_change_net(sk, net);
  2071. if (!cfg || cfg->groups < 32)
  2072. groups = 32;
  2073. else
  2074. groups = cfg->groups;
  2075. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2076. if (!listeners)
  2077. goto out_sock_release;
  2078. sk->sk_data_ready = netlink_data_ready;
  2079. if (cfg && cfg->input)
  2080. nlk_sk(sk)->netlink_rcv = cfg->input;
  2081. if (netlink_insert(sk, net, 0))
  2082. goto out_sock_release;
  2083. nlk = nlk_sk(sk);
  2084. nlk->flags |= NETLINK_KERNEL_SOCKET;
  2085. netlink_table_grab();
  2086. if (!nl_table[unit].registered) {
  2087. nl_table[unit].groups = groups;
  2088. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  2089. nl_table[unit].cb_mutex = cb_mutex;
  2090. nl_table[unit].module = module;
  2091. if (cfg) {
  2092. nl_table[unit].bind = cfg->bind;
  2093. nl_table[unit].unbind = cfg->unbind;
  2094. nl_table[unit].flags = cfg->flags;
  2095. if (cfg->compare)
  2096. nl_table[unit].compare = cfg->compare;
  2097. }
  2098. nl_table[unit].registered = 1;
  2099. } else {
  2100. kfree(listeners);
  2101. nl_table[unit].registered++;
  2102. }
  2103. netlink_table_ungrab();
  2104. return sk;
  2105. out_sock_release:
  2106. kfree(listeners);
  2107. netlink_kernel_release(sk);
  2108. return NULL;
  2109. out_sock_release_nosk:
  2110. sock_release(sock);
  2111. return NULL;
  2112. }
  2113. EXPORT_SYMBOL(__netlink_kernel_create);
  2114. void
  2115. netlink_kernel_release(struct sock *sk)
  2116. {
  2117. sk_release_kernel(sk);
  2118. }
  2119. EXPORT_SYMBOL(netlink_kernel_release);
  2120. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2121. {
  2122. struct listeners *new, *old;
  2123. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  2124. if (groups < 32)
  2125. groups = 32;
  2126. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  2127. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  2128. if (!new)
  2129. return -ENOMEM;
  2130. old = nl_deref_protected(tbl->listeners);
  2131. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  2132. rcu_assign_pointer(tbl->listeners, new);
  2133. kfree_rcu(old, rcu);
  2134. }
  2135. tbl->groups = groups;
  2136. return 0;
  2137. }
  2138. /**
  2139. * netlink_change_ngroups - change number of multicast groups
  2140. *
  2141. * This changes the number of multicast groups that are available
  2142. * on a certain netlink family. Note that it is not possible to
  2143. * change the number of groups to below 32. Also note that it does
  2144. * not implicitly call netlink_clear_multicast_users() when the
  2145. * number of groups is reduced.
  2146. *
  2147. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  2148. * @groups: The new number of groups.
  2149. */
  2150. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  2151. {
  2152. int err;
  2153. netlink_table_grab();
  2154. err = __netlink_change_ngroups(sk, groups);
  2155. netlink_table_ungrab();
  2156. return err;
  2157. }
  2158. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  2159. {
  2160. struct sock *sk;
  2161. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  2162. sk_for_each_bound(sk, &tbl->mc_list)
  2163. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  2164. }
  2165. struct nlmsghdr *
  2166. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  2167. {
  2168. struct nlmsghdr *nlh;
  2169. int size = nlmsg_msg_size(len);
  2170. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  2171. nlh->nlmsg_type = type;
  2172. nlh->nlmsg_len = size;
  2173. nlh->nlmsg_flags = flags;
  2174. nlh->nlmsg_pid = portid;
  2175. nlh->nlmsg_seq = seq;
  2176. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  2177. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  2178. return nlh;
  2179. }
  2180. EXPORT_SYMBOL(__nlmsg_put);
  2181. /*
  2182. * It looks a bit ugly.
  2183. * It would be better to create kernel thread.
  2184. */
  2185. static int netlink_dump(struct sock *sk)
  2186. {
  2187. struct netlink_sock *nlk = nlk_sk(sk);
  2188. struct netlink_callback *cb;
  2189. struct sk_buff *skb = NULL;
  2190. struct nlmsghdr *nlh;
  2191. int len, err = -ENOBUFS;
  2192. int alloc_size;
  2193. mutex_lock(nlk->cb_mutex);
  2194. if (!nlk->cb_running) {
  2195. err = -EINVAL;
  2196. goto errout_skb;
  2197. }
  2198. cb = &nlk->cb;
  2199. alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  2200. if (!netlink_rx_is_mmaped(sk) &&
  2201. atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  2202. goto errout_skb;
  2203. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  2204. * required, but it makes sense to _attempt_ a 16K bytes allocation
  2205. * to reduce number of system calls on dump operations, if user
  2206. * ever provided a big enough buffer.
  2207. */
  2208. if (alloc_size < nlk->max_recvmsg_len) {
  2209. skb = netlink_alloc_skb(sk,
  2210. nlk->max_recvmsg_len,
  2211. nlk->portid,
  2212. GFP_KERNEL |
  2213. __GFP_NOWARN |
  2214. __GFP_NORETRY);
  2215. /* available room should be exact amount to avoid MSG_TRUNC */
  2216. if (skb)
  2217. skb_reserve(skb, skb_tailroom(skb) -
  2218. nlk->max_recvmsg_len);
  2219. }
  2220. if (!skb)
  2221. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  2222. GFP_KERNEL);
  2223. if (!skb)
  2224. goto errout_skb;
  2225. netlink_skb_set_owner_r(skb, sk);
  2226. len = cb->dump(skb, cb);
  2227. if (len > 0) {
  2228. mutex_unlock(nlk->cb_mutex);
  2229. if (sk_filter(sk, skb))
  2230. kfree_skb(skb);
  2231. else
  2232. __netlink_sendskb(sk, skb);
  2233. return 0;
  2234. }
  2235. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  2236. if (!nlh)
  2237. goto errout_skb;
  2238. nl_dump_check_consistent(cb, nlh);
  2239. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  2240. if (sk_filter(sk, skb))
  2241. kfree_skb(skb);
  2242. else
  2243. __netlink_sendskb(sk, skb);
  2244. if (cb->done)
  2245. cb->done(cb);
  2246. nlk->cb_running = false;
  2247. mutex_unlock(nlk->cb_mutex);
  2248. module_put(cb->module);
  2249. consume_skb(cb->skb);
  2250. return 0;
  2251. errout_skb:
  2252. mutex_unlock(nlk->cb_mutex);
  2253. kfree_skb(skb);
  2254. return err;
  2255. }
  2256. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  2257. const struct nlmsghdr *nlh,
  2258. struct netlink_dump_control *control)
  2259. {
  2260. struct netlink_callback *cb;
  2261. struct sock *sk;
  2262. struct netlink_sock *nlk;
  2263. int ret;
  2264. /* Memory mapped dump requests need to be copied to avoid looping
  2265. * on the pending state in netlink_mmap_sendmsg() while the CB hold
  2266. * a reference to the skb.
  2267. */
  2268. if (netlink_skb_is_mmaped(skb)) {
  2269. skb = skb_copy(skb, GFP_KERNEL);
  2270. if (skb == NULL)
  2271. return -ENOBUFS;
  2272. } else
  2273. atomic_inc(&skb->users);
  2274. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  2275. if (sk == NULL) {
  2276. ret = -ECONNREFUSED;
  2277. goto error_free;
  2278. }
  2279. nlk = nlk_sk(sk);
  2280. mutex_lock(nlk->cb_mutex);
  2281. /* A dump is in progress... */
  2282. if (nlk->cb_running) {
  2283. ret = -EBUSY;
  2284. goto error_unlock;
  2285. }
  2286. /* add reference of module which cb->dump belongs to */
  2287. if (!try_module_get(control->module)) {
  2288. ret = -EPROTONOSUPPORT;
  2289. goto error_unlock;
  2290. }
  2291. cb = &nlk->cb;
  2292. memset(cb, 0, sizeof(*cb));
  2293. cb->dump = control->dump;
  2294. cb->done = control->done;
  2295. cb->nlh = nlh;
  2296. cb->data = control->data;
  2297. cb->module = control->module;
  2298. cb->min_dump_alloc = control->min_dump_alloc;
  2299. cb->skb = skb;
  2300. nlk->cb_running = true;
  2301. mutex_unlock(nlk->cb_mutex);
  2302. ret = netlink_dump(sk);
  2303. sock_put(sk);
  2304. if (ret)
  2305. return ret;
  2306. /* We successfully started a dump, by returning -EINTR we
  2307. * signal not to send ACK even if it was requested.
  2308. */
  2309. return -EINTR;
  2310. error_unlock:
  2311. sock_put(sk);
  2312. mutex_unlock(nlk->cb_mutex);
  2313. error_free:
  2314. kfree_skb(skb);
  2315. return ret;
  2316. }
  2317. EXPORT_SYMBOL(__netlink_dump_start);
  2318. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  2319. {
  2320. struct sk_buff *skb;
  2321. struct nlmsghdr *rep;
  2322. struct nlmsgerr *errmsg;
  2323. size_t payload = sizeof(*errmsg);
  2324. /* error messages get the original request appened */
  2325. if (err)
  2326. payload += nlmsg_len(nlh);
  2327. skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
  2328. NETLINK_CB(in_skb).portid, GFP_KERNEL);
  2329. if (!skb) {
  2330. struct sock *sk;
  2331. sk = netlink_lookup(sock_net(in_skb->sk),
  2332. in_skb->sk->sk_protocol,
  2333. NETLINK_CB(in_skb).portid);
  2334. if (sk) {
  2335. sk->sk_err = ENOBUFS;
  2336. sk->sk_error_report(sk);
  2337. sock_put(sk);
  2338. }
  2339. return;
  2340. }
  2341. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  2342. NLMSG_ERROR, payload, 0);
  2343. errmsg = nlmsg_data(rep);
  2344. errmsg->error = err;
  2345. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  2346. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  2347. }
  2348. EXPORT_SYMBOL(netlink_ack);
  2349. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  2350. struct nlmsghdr *))
  2351. {
  2352. struct nlmsghdr *nlh;
  2353. int err;
  2354. while (skb->len >= nlmsg_total_size(0)) {
  2355. int msglen;
  2356. nlh = nlmsg_hdr(skb);
  2357. err = 0;
  2358. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  2359. return 0;
  2360. /* Only requests are handled by the kernel */
  2361. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  2362. goto ack;
  2363. /* Skip control messages */
  2364. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  2365. goto ack;
  2366. err = cb(skb, nlh);
  2367. if (err == -EINTR)
  2368. goto skip;
  2369. ack:
  2370. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  2371. netlink_ack(skb, nlh, err);
  2372. skip:
  2373. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  2374. if (msglen > skb->len)
  2375. msglen = skb->len;
  2376. skb_pull(skb, msglen);
  2377. }
  2378. return 0;
  2379. }
  2380. EXPORT_SYMBOL(netlink_rcv_skb);
  2381. /**
  2382. * nlmsg_notify - send a notification netlink message
  2383. * @sk: netlink socket to use
  2384. * @skb: notification message
  2385. * @portid: destination netlink portid for reports or 0
  2386. * @group: destination multicast group or 0
  2387. * @report: 1 to report back, 0 to disable
  2388. * @flags: allocation flags
  2389. */
  2390. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2391. unsigned int group, int report, gfp_t flags)
  2392. {
  2393. int err = 0;
  2394. if (group) {
  2395. int exclude_portid = 0;
  2396. if (report) {
  2397. atomic_inc(&skb->users);
  2398. exclude_portid = portid;
  2399. }
  2400. /* errors reported via destination sk->sk_err, but propagate
  2401. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2402. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2403. }
  2404. if (report) {
  2405. int err2;
  2406. err2 = nlmsg_unicast(sk, skb, portid);
  2407. if (!err || err == -ESRCH)
  2408. err = err2;
  2409. }
  2410. return err;
  2411. }
  2412. EXPORT_SYMBOL(nlmsg_notify);
  2413. #ifdef CONFIG_PROC_FS
  2414. struct nl_seq_iter {
  2415. struct seq_net_private p;
  2416. int link;
  2417. int hash_idx;
  2418. };
  2419. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  2420. {
  2421. struct nl_seq_iter *iter = seq->private;
  2422. int i, j;
  2423. struct netlink_sock *nlk;
  2424. struct sock *s;
  2425. loff_t off = 0;
  2426. for (i = 0; i < MAX_LINKS; i++) {
  2427. struct rhashtable *ht = &nl_table[i].hash;
  2428. const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
  2429. for (j = 0; j < tbl->size; j++) {
  2430. rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) {
  2431. s = (struct sock *)nlk;
  2432. if (sock_net(s) != seq_file_net(seq))
  2433. continue;
  2434. if (off == pos) {
  2435. iter->link = i;
  2436. iter->hash_idx = j;
  2437. return s;
  2438. }
  2439. ++off;
  2440. }
  2441. }
  2442. }
  2443. return NULL;
  2444. }
  2445. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  2446. __acquires(nl_table_lock) __acquires(RCU)
  2447. {
  2448. read_lock(&nl_table_lock);
  2449. rcu_read_lock();
  2450. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2451. }
  2452. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2453. {
  2454. struct rhashtable *ht;
  2455. struct netlink_sock *nlk;
  2456. struct nl_seq_iter *iter;
  2457. struct net *net;
  2458. int i, j;
  2459. ++*pos;
  2460. if (v == SEQ_START_TOKEN)
  2461. return netlink_seq_socket_idx(seq, 0);
  2462. net = seq_file_net(seq);
  2463. iter = seq->private;
  2464. nlk = v;
  2465. i = iter->link;
  2466. ht = &nl_table[i].hash;
  2467. rht_for_each_entry(nlk, nlk->node.next, ht, node)
  2468. if (net_eq(sock_net((struct sock *)nlk), net))
  2469. return nlk;
  2470. j = iter->hash_idx + 1;
  2471. do {
  2472. const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
  2473. for (; j < tbl->size; j++) {
  2474. rht_for_each_entry(nlk, tbl->buckets[j], ht, node) {
  2475. if (net_eq(sock_net((struct sock *)nlk), net)) {
  2476. iter->link = i;
  2477. iter->hash_idx = j;
  2478. return nlk;
  2479. }
  2480. }
  2481. }
  2482. j = 0;
  2483. } while (++i < MAX_LINKS);
  2484. return NULL;
  2485. }
  2486. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2487. __releases(RCU) __releases(nl_table_lock)
  2488. {
  2489. rcu_read_unlock();
  2490. read_unlock(&nl_table_lock);
  2491. }
  2492. static int netlink_seq_show(struct seq_file *seq, void *v)
  2493. {
  2494. if (v == SEQ_START_TOKEN) {
  2495. seq_puts(seq,
  2496. "sk Eth Pid Groups "
  2497. "Rmem Wmem Dump Locks Drops Inode\n");
  2498. } else {
  2499. struct sock *s = v;
  2500. struct netlink_sock *nlk = nlk_sk(s);
  2501. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2502. s,
  2503. s->sk_protocol,
  2504. nlk->portid,
  2505. nlk->groups ? (u32)nlk->groups[0] : 0,
  2506. sk_rmem_alloc_get(s),
  2507. sk_wmem_alloc_get(s),
  2508. nlk->cb_running,
  2509. atomic_read(&s->sk_refcnt),
  2510. atomic_read(&s->sk_drops),
  2511. sock_i_ino(s)
  2512. );
  2513. }
  2514. return 0;
  2515. }
  2516. static const struct seq_operations netlink_seq_ops = {
  2517. .start = netlink_seq_start,
  2518. .next = netlink_seq_next,
  2519. .stop = netlink_seq_stop,
  2520. .show = netlink_seq_show,
  2521. };
  2522. static int netlink_seq_open(struct inode *inode, struct file *file)
  2523. {
  2524. return seq_open_net(inode, file, &netlink_seq_ops,
  2525. sizeof(struct nl_seq_iter));
  2526. }
  2527. static const struct file_operations netlink_seq_fops = {
  2528. .owner = THIS_MODULE,
  2529. .open = netlink_seq_open,
  2530. .read = seq_read,
  2531. .llseek = seq_lseek,
  2532. .release = seq_release_net,
  2533. };
  2534. #endif
  2535. int netlink_register_notifier(struct notifier_block *nb)
  2536. {
  2537. return atomic_notifier_chain_register(&netlink_chain, nb);
  2538. }
  2539. EXPORT_SYMBOL(netlink_register_notifier);
  2540. int netlink_unregister_notifier(struct notifier_block *nb)
  2541. {
  2542. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2543. }
  2544. EXPORT_SYMBOL(netlink_unregister_notifier);
  2545. static const struct proto_ops netlink_ops = {
  2546. .family = PF_NETLINK,
  2547. .owner = THIS_MODULE,
  2548. .release = netlink_release,
  2549. .bind = netlink_bind,
  2550. .connect = netlink_connect,
  2551. .socketpair = sock_no_socketpair,
  2552. .accept = sock_no_accept,
  2553. .getname = netlink_getname,
  2554. .poll = netlink_poll,
  2555. .ioctl = sock_no_ioctl,
  2556. .listen = sock_no_listen,
  2557. .shutdown = sock_no_shutdown,
  2558. .setsockopt = netlink_setsockopt,
  2559. .getsockopt = netlink_getsockopt,
  2560. .sendmsg = netlink_sendmsg,
  2561. .recvmsg = netlink_recvmsg,
  2562. .mmap = netlink_mmap,
  2563. .sendpage = sock_no_sendpage,
  2564. };
  2565. static const struct net_proto_family netlink_family_ops = {
  2566. .family = PF_NETLINK,
  2567. .create = netlink_create,
  2568. .owner = THIS_MODULE, /* for consistency 8) */
  2569. };
  2570. static int __net_init netlink_net_init(struct net *net)
  2571. {
  2572. #ifdef CONFIG_PROC_FS
  2573. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2574. return -ENOMEM;
  2575. #endif
  2576. return 0;
  2577. }
  2578. static void __net_exit netlink_net_exit(struct net *net)
  2579. {
  2580. #ifdef CONFIG_PROC_FS
  2581. remove_proc_entry("netlink", net->proc_net);
  2582. #endif
  2583. }
  2584. static void __init netlink_add_usersock_entry(void)
  2585. {
  2586. struct listeners *listeners;
  2587. int groups = 32;
  2588. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2589. if (!listeners)
  2590. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2591. netlink_table_grab();
  2592. nl_table[NETLINK_USERSOCK].groups = groups;
  2593. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2594. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2595. nl_table[NETLINK_USERSOCK].registered = 1;
  2596. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2597. netlink_table_ungrab();
  2598. }
  2599. static struct pernet_operations __net_initdata netlink_net_ops = {
  2600. .init = netlink_net_init,
  2601. .exit = netlink_net_exit,
  2602. };
  2603. static int __init netlink_proto_init(void)
  2604. {
  2605. int i;
  2606. int err = proto_register(&netlink_proto, 0);
  2607. struct rhashtable_params ht_params = {
  2608. .head_offset = offsetof(struct netlink_sock, node),
  2609. .key_offset = offsetof(struct netlink_sock, portid),
  2610. .key_len = sizeof(u32), /* portid */
  2611. .hashfn = jhash,
  2612. .max_shift = 16, /* 64K */
  2613. .grow_decision = rht_grow_above_75,
  2614. .shrink_decision = rht_shrink_below_30,
  2615. #ifdef CONFIG_PROVE_LOCKING
  2616. .mutex_is_held = lockdep_nl_sk_hash_is_held,
  2617. #endif
  2618. };
  2619. if (err != 0)
  2620. goto out;
  2621. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2622. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2623. if (!nl_table)
  2624. goto panic;
  2625. for (i = 0; i < MAX_LINKS; i++) {
  2626. if (rhashtable_init(&nl_table[i].hash, &ht_params) < 0) {
  2627. while (--i > 0)
  2628. rhashtable_destroy(&nl_table[i].hash);
  2629. kfree(nl_table);
  2630. goto panic;
  2631. }
  2632. }
  2633. INIT_LIST_HEAD(&netlink_tap_all);
  2634. netlink_add_usersock_entry();
  2635. sock_register(&netlink_family_ops);
  2636. register_pernet_subsys(&netlink_net_ops);
  2637. /* The netlink device handler may be needed early. */
  2638. rtnetlink_init();
  2639. out:
  2640. return err;
  2641. panic:
  2642. panic("netlink_init: Cannot allocate nl_table\n");
  2643. }
  2644. core_initcall(netlink_proto_init);