af_netlink.c 73 KB

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