af_netlink.c 73 KB

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