af_netlink.c 73 KB

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