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