af_netlink.c 69 KB

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