devinet.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET3 IP device support routines.
  4. *
  5. * Derived from the IP parts of dev.c 1.0.19
  6. * Authors: Ross Biro
  7. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  9. *
  10. * Additional Authors:
  11. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  12. * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  13. *
  14. * Changes:
  15. * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
  16. * lists.
  17. * Cyrus Durgin: updated for kmod
  18. * Matthias Andree: in devinet_ioctl, compare label and
  19. * address (4.4BSD alias style support),
  20. * fall back to comparing just the label
  21. * if no match found.
  22. */
  23. #include <linux/uaccess.h>
  24. #include <linux/bitops.h>
  25. #include <linux/capability.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/string.h>
  31. #include <linux/mm.h>
  32. #include <linux/socket.h>
  33. #include <linux/sockios.h>
  34. #include <linux/in.h>
  35. #include <linux/errno.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/if_addr.h>
  38. #include <linux/if_ether.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/init.h>
  44. #include <linux/notifier.h>
  45. #include <linux/inetdevice.h>
  46. #include <linux/igmp.h>
  47. #include <linux/slab.h>
  48. #include <linux/hash.h>
  49. #ifdef CONFIG_SYSCTL
  50. #include <linux/sysctl.h>
  51. #endif
  52. #include <linux/kmod.h>
  53. #include <linux/netconf.h>
  54. #include <net/arp.h>
  55. #include <net/ip.h>
  56. #include <net/route.h>
  57. #include <net/ip_fib.h>
  58. #include <net/rtnetlink.h>
  59. #include <net/net_namespace.h>
  60. #include <net/addrconf.h>
  61. #define IPV6ONLY_FLAGS \
  62. (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \
  63. IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \
  64. IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY)
  65. static struct ipv4_devconf ipv4_devconf = {
  66. .data = {
  67. [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
  68. [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
  69. [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
  70. [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
  71. [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
  72. [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
  73. },
  74. };
  75. static struct ipv4_devconf ipv4_devconf_dflt = {
  76. .data = {
  77. [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
  78. [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
  79. [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
  80. [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
  81. [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
  82. [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
  83. [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
  84. },
  85. };
  86. #define IPV4_DEVCONF_DFLT(net, attr) \
  87. IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
  88. static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
  89. [IFA_LOCAL] = { .type = NLA_U32 },
  90. [IFA_ADDRESS] = { .type = NLA_U32 },
  91. [IFA_BROADCAST] = { .type = NLA_U32 },
  92. [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
  93. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  94. [IFA_FLAGS] = { .type = NLA_U32 },
  95. [IFA_RT_PRIORITY] = { .type = NLA_U32 },
  96. [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
  97. };
  98. struct inet_fill_args {
  99. u32 portid;
  100. u32 seq;
  101. int event;
  102. unsigned int flags;
  103. int netnsid;
  104. int ifindex;
  105. };
  106. #define IN4_ADDR_HSIZE_SHIFT 8
  107. #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
  108. static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
  109. static u32 inet_addr_hash(const struct net *net, __be32 addr)
  110. {
  111. u32 val = (__force u32) addr ^ net_hash_mix(net);
  112. return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
  113. }
  114. static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
  115. {
  116. u32 hash = inet_addr_hash(net, ifa->ifa_local);
  117. ASSERT_RTNL();
  118. hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
  119. }
  120. static void inet_hash_remove(struct in_ifaddr *ifa)
  121. {
  122. ASSERT_RTNL();
  123. hlist_del_init_rcu(&ifa->hash);
  124. }
  125. /**
  126. * __ip_dev_find - find the first device with a given source address.
  127. * @net: the net namespace
  128. * @addr: the source address
  129. * @devref: if true, take a reference on the found device
  130. *
  131. * If a caller uses devref=false, it should be protected by RCU, or RTNL
  132. */
  133. struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
  134. {
  135. struct net_device *result = NULL;
  136. struct in_ifaddr *ifa;
  137. rcu_read_lock();
  138. ifa = inet_lookup_ifaddr_rcu(net, addr);
  139. if (!ifa) {
  140. struct flowi4 fl4 = { .daddr = addr };
  141. struct fib_result res = { 0 };
  142. struct fib_table *local;
  143. /* Fallback to FIB local table so that communication
  144. * over loopback subnets work.
  145. */
  146. local = fib_get_table(net, RT_TABLE_LOCAL);
  147. if (local &&
  148. !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
  149. res.type == RTN_LOCAL)
  150. result = FIB_RES_DEV(res);
  151. } else {
  152. result = ifa->ifa_dev->dev;
  153. }
  154. if (result && devref)
  155. dev_hold(result);
  156. rcu_read_unlock();
  157. return result;
  158. }
  159. EXPORT_SYMBOL(__ip_dev_find);
  160. /* called under RCU lock */
  161. struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
  162. {
  163. u32 hash = inet_addr_hash(net, addr);
  164. struct in_ifaddr *ifa;
  165. hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
  166. if (ifa->ifa_local == addr &&
  167. net_eq(dev_net(ifa->ifa_dev->dev), net))
  168. return ifa;
  169. return NULL;
  170. }
  171. static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
  172. static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
  173. static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
  174. static void inet_del_ifa(struct in_device *in_dev,
  175. struct in_ifaddr __rcu **ifap,
  176. int destroy);
  177. #ifdef CONFIG_SYSCTL
  178. static int devinet_sysctl_register(struct in_device *idev);
  179. static void devinet_sysctl_unregister(struct in_device *idev);
  180. #else
  181. static int devinet_sysctl_register(struct in_device *idev)
  182. {
  183. return 0;
  184. }
  185. static void devinet_sysctl_unregister(struct in_device *idev)
  186. {
  187. }
  188. #endif
  189. /* Locks all the inet devices. */
  190. static struct in_ifaddr *inet_alloc_ifa(void)
  191. {
  192. return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
  193. }
  194. static void inet_rcu_free_ifa(struct rcu_head *head)
  195. {
  196. struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
  197. if (ifa->ifa_dev)
  198. in_dev_put(ifa->ifa_dev);
  199. kfree(ifa);
  200. }
  201. static void inet_free_ifa(struct in_ifaddr *ifa)
  202. {
  203. call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
  204. }
  205. void in_dev_finish_destroy(struct in_device *idev)
  206. {
  207. struct net_device *dev = idev->dev;
  208. WARN_ON(idev->ifa_list);
  209. WARN_ON(idev->mc_list);
  210. kfree(rcu_dereference_protected(idev->mc_hash, 1));
  211. #ifdef NET_REFCNT_DEBUG
  212. pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
  213. #endif
  214. dev_put(dev);
  215. if (!idev->dead)
  216. pr_err("Freeing alive in_device %p\n", idev);
  217. else
  218. kfree(idev);
  219. }
  220. EXPORT_SYMBOL(in_dev_finish_destroy);
  221. static struct in_device *inetdev_init(struct net_device *dev)
  222. {
  223. struct in_device *in_dev;
  224. int err = -ENOMEM;
  225. ASSERT_RTNL();
  226. in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
  227. if (!in_dev)
  228. goto out;
  229. memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
  230. sizeof(in_dev->cnf));
  231. in_dev->cnf.sysctl = NULL;
  232. in_dev->dev = dev;
  233. in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
  234. if (!in_dev->arp_parms)
  235. goto out_kfree;
  236. if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
  237. dev_disable_lro(dev);
  238. /* Reference in_dev->dev */
  239. dev_hold(dev);
  240. /* Account for reference dev->ip_ptr (below) */
  241. refcount_set(&in_dev->refcnt, 1);
  242. err = devinet_sysctl_register(in_dev);
  243. if (err) {
  244. in_dev->dead = 1;
  245. neigh_parms_release(&arp_tbl, in_dev->arp_parms);
  246. in_dev_put(in_dev);
  247. in_dev = NULL;
  248. goto out;
  249. }
  250. ip_mc_init_dev(in_dev);
  251. if (dev->flags & IFF_UP)
  252. ip_mc_up(in_dev);
  253. /* we can receive as soon as ip_ptr is set -- do this last */
  254. rcu_assign_pointer(dev->ip_ptr, in_dev);
  255. out:
  256. return in_dev ?: ERR_PTR(err);
  257. out_kfree:
  258. kfree(in_dev);
  259. in_dev = NULL;
  260. goto out;
  261. }
  262. static void in_dev_rcu_put(struct rcu_head *head)
  263. {
  264. struct in_device *idev = container_of(head, struct in_device, rcu_head);
  265. in_dev_put(idev);
  266. }
  267. static void inetdev_destroy(struct in_device *in_dev)
  268. {
  269. struct net_device *dev;
  270. struct in_ifaddr *ifa;
  271. ASSERT_RTNL();
  272. dev = in_dev->dev;
  273. in_dev->dead = 1;
  274. ip_mc_destroy_dev(in_dev);
  275. while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) {
  276. inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
  277. inet_free_ifa(ifa);
  278. }
  279. RCU_INIT_POINTER(dev->ip_ptr, NULL);
  280. devinet_sysctl_unregister(in_dev);
  281. neigh_parms_release(&arp_tbl, in_dev->arp_parms);
  282. arp_ifdown(dev);
  283. call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
  284. }
  285. int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
  286. {
  287. const struct in_ifaddr *ifa;
  288. rcu_read_lock();
  289. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  290. if (inet_ifa_match(a, ifa)) {
  291. if (!b || inet_ifa_match(b, ifa)) {
  292. rcu_read_unlock();
  293. return 1;
  294. }
  295. }
  296. }
  297. rcu_read_unlock();
  298. return 0;
  299. }
  300. static void __inet_del_ifa(struct in_device *in_dev,
  301. struct in_ifaddr __rcu **ifap,
  302. int destroy, struct nlmsghdr *nlh, u32 portid)
  303. {
  304. struct in_ifaddr *promote = NULL;
  305. struct in_ifaddr *ifa, *ifa1;
  306. struct in_ifaddr *last_prim;
  307. struct in_ifaddr *prev_prom = NULL;
  308. int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
  309. ASSERT_RTNL();
  310. ifa1 = rtnl_dereference(*ifap);
  311. last_prim = rtnl_dereference(in_dev->ifa_list);
  312. if (in_dev->dead)
  313. goto no_promotions;
  314. /* 1. Deleting primary ifaddr forces deletion all secondaries
  315. * unless alias promotion is set
  316. **/
  317. if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
  318. struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next;
  319. while ((ifa = rtnl_dereference(*ifap1)) != NULL) {
  320. if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
  321. ifa1->ifa_scope <= ifa->ifa_scope)
  322. last_prim = ifa;
  323. if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
  324. ifa1->ifa_mask != ifa->ifa_mask ||
  325. !inet_ifa_match(ifa1->ifa_address, ifa)) {
  326. ifap1 = &ifa->ifa_next;
  327. prev_prom = ifa;
  328. continue;
  329. }
  330. if (!do_promote) {
  331. inet_hash_remove(ifa);
  332. *ifap1 = ifa->ifa_next;
  333. rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
  334. blocking_notifier_call_chain(&inetaddr_chain,
  335. NETDEV_DOWN, ifa);
  336. inet_free_ifa(ifa);
  337. } else {
  338. promote = ifa;
  339. break;
  340. }
  341. }
  342. }
  343. /* On promotion all secondaries from subnet are changing
  344. * the primary IP, we must remove all their routes silently
  345. * and later to add them back with new prefsrc. Do this
  346. * while all addresses are on the device list.
  347. */
  348. for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) {
  349. if (ifa1->ifa_mask == ifa->ifa_mask &&
  350. inet_ifa_match(ifa1->ifa_address, ifa))
  351. fib_del_ifaddr(ifa, ifa1);
  352. }
  353. no_promotions:
  354. /* 2. Unlink it */
  355. *ifap = ifa1->ifa_next;
  356. inet_hash_remove(ifa1);
  357. /* 3. Announce address deletion */
  358. /* Send message first, then call notifier.
  359. At first sight, FIB update triggered by notifier
  360. will refer to already deleted ifaddr, that could confuse
  361. netlink listeners. It is not true: look, gated sees
  362. that route deleted and if it still thinks that ifaddr
  363. is valid, it will try to restore deleted routes... Grr.
  364. So that, this order is correct.
  365. */
  366. rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
  367. blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
  368. if (promote) {
  369. struct in_ifaddr *next_sec;
  370. next_sec = rtnl_dereference(promote->ifa_next);
  371. if (prev_prom) {
  372. struct in_ifaddr *last_sec;
  373. rcu_assign_pointer(prev_prom->ifa_next, next_sec);
  374. last_sec = rtnl_dereference(last_prim->ifa_next);
  375. rcu_assign_pointer(promote->ifa_next, last_sec);
  376. rcu_assign_pointer(last_prim->ifa_next, promote);
  377. }
  378. promote->ifa_flags &= ~IFA_F_SECONDARY;
  379. rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
  380. blocking_notifier_call_chain(&inetaddr_chain,
  381. NETDEV_UP, promote);
  382. for (ifa = next_sec; ifa;
  383. ifa = rtnl_dereference(ifa->ifa_next)) {
  384. if (ifa1->ifa_mask != ifa->ifa_mask ||
  385. !inet_ifa_match(ifa1->ifa_address, ifa))
  386. continue;
  387. fib_add_ifaddr(ifa);
  388. }
  389. }
  390. if (destroy)
  391. inet_free_ifa(ifa1);
  392. }
  393. static void inet_del_ifa(struct in_device *in_dev,
  394. struct in_ifaddr __rcu **ifap,
  395. int destroy)
  396. {
  397. __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
  398. }
  399. static void check_lifetime(struct work_struct *work);
  400. static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
  401. static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
  402. u32 portid, struct netlink_ext_ack *extack)
  403. {
  404. struct in_ifaddr __rcu **last_primary, **ifap;
  405. struct in_device *in_dev = ifa->ifa_dev;
  406. struct in_validator_info ivi;
  407. struct in_ifaddr *ifa1;
  408. int ret;
  409. ASSERT_RTNL();
  410. if (!ifa->ifa_local) {
  411. inet_free_ifa(ifa);
  412. return 0;
  413. }
  414. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  415. last_primary = &in_dev->ifa_list;
  416. /* Don't set IPv6 only flags to IPv4 addresses */
  417. ifa->ifa_flags &= ~IPV6ONLY_FLAGS;
  418. ifap = &in_dev->ifa_list;
  419. ifa1 = rtnl_dereference(*ifap);
  420. while (ifa1) {
  421. if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
  422. ifa->ifa_scope <= ifa1->ifa_scope)
  423. last_primary = &ifa1->ifa_next;
  424. if (ifa1->ifa_mask == ifa->ifa_mask &&
  425. inet_ifa_match(ifa1->ifa_address, ifa)) {
  426. if (ifa1->ifa_local == ifa->ifa_local) {
  427. inet_free_ifa(ifa);
  428. return -EEXIST;
  429. }
  430. if (ifa1->ifa_scope != ifa->ifa_scope) {
  431. inet_free_ifa(ifa);
  432. return -EINVAL;
  433. }
  434. ifa->ifa_flags |= IFA_F_SECONDARY;
  435. }
  436. ifap = &ifa1->ifa_next;
  437. ifa1 = rtnl_dereference(*ifap);
  438. }
  439. /* Allow any devices that wish to register ifaddr validtors to weigh
  440. * in now, before changes are committed. The rntl lock is serializing
  441. * access here, so the state should not change between a validator call
  442. * and a final notify on commit. This isn't invoked on promotion under
  443. * the assumption that validators are checking the address itself, and
  444. * not the flags.
  445. */
  446. ivi.ivi_addr = ifa->ifa_address;
  447. ivi.ivi_dev = ifa->ifa_dev;
  448. ivi.extack = extack;
  449. ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
  450. NETDEV_UP, &ivi);
  451. ret = notifier_to_errno(ret);
  452. if (ret) {
  453. inet_free_ifa(ifa);
  454. return ret;
  455. }
  456. if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
  457. prandom_seed((__force u32) ifa->ifa_local);
  458. ifap = last_primary;
  459. }
  460. rcu_assign_pointer(ifa->ifa_next, *ifap);
  461. rcu_assign_pointer(*ifap, ifa);
  462. inet_hash_insert(dev_net(in_dev->dev), ifa);
  463. cancel_delayed_work(&check_lifetime_work);
  464. queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
  465. /* Send message first, then call notifier.
  466. Notifier will trigger FIB update, so that
  467. listeners of netlink will know about new ifaddr */
  468. rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
  469. blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
  470. return 0;
  471. }
  472. static int inet_insert_ifa(struct in_ifaddr *ifa)
  473. {
  474. return __inet_insert_ifa(ifa, NULL, 0, NULL);
  475. }
  476. static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
  477. {
  478. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  479. ASSERT_RTNL();
  480. if (!in_dev) {
  481. inet_free_ifa(ifa);
  482. return -ENOBUFS;
  483. }
  484. ipv4_devconf_setall(in_dev);
  485. neigh_parms_data_state_setall(in_dev->arp_parms);
  486. if (ifa->ifa_dev != in_dev) {
  487. WARN_ON(ifa->ifa_dev);
  488. in_dev_hold(in_dev);
  489. ifa->ifa_dev = in_dev;
  490. }
  491. if (ipv4_is_loopback(ifa->ifa_local))
  492. ifa->ifa_scope = RT_SCOPE_HOST;
  493. return inet_insert_ifa(ifa);
  494. }
  495. /* Caller must hold RCU or RTNL :
  496. * We dont take a reference on found in_device
  497. */
  498. struct in_device *inetdev_by_index(struct net *net, int ifindex)
  499. {
  500. struct net_device *dev;
  501. struct in_device *in_dev = NULL;
  502. rcu_read_lock();
  503. dev = dev_get_by_index_rcu(net, ifindex);
  504. if (dev)
  505. in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  506. rcu_read_unlock();
  507. return in_dev;
  508. }
  509. EXPORT_SYMBOL(inetdev_by_index);
  510. /* Called only from RTNL semaphored context. No locks. */
  511. struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
  512. __be32 mask)
  513. {
  514. struct in_ifaddr *ifa;
  515. ASSERT_RTNL();
  516. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  517. if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
  518. return ifa;
  519. }
  520. return NULL;
  521. }
  522. static int ip_mc_autojoin_config(struct net *net, bool join,
  523. const struct in_ifaddr *ifa)
  524. {
  525. #if defined(CONFIG_IP_MULTICAST)
  526. struct ip_mreqn mreq = {
  527. .imr_multiaddr.s_addr = ifa->ifa_address,
  528. .imr_ifindex = ifa->ifa_dev->dev->ifindex,
  529. };
  530. struct sock *sk = net->ipv4.mc_autojoin_sk;
  531. int ret;
  532. ASSERT_RTNL();
  533. lock_sock(sk);
  534. if (join)
  535. ret = ip_mc_join_group(sk, &mreq);
  536. else
  537. ret = ip_mc_leave_group(sk, &mreq);
  538. release_sock(sk);
  539. return ret;
  540. #else
  541. return -EOPNOTSUPP;
  542. #endif
  543. }
  544. static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
  545. struct netlink_ext_ack *extack)
  546. {
  547. struct net *net = sock_net(skb->sk);
  548. struct in_ifaddr __rcu **ifap;
  549. struct nlattr *tb[IFA_MAX+1];
  550. struct in_device *in_dev;
  551. struct ifaddrmsg *ifm;
  552. struct in_ifaddr *ifa;
  553. int err = -EINVAL;
  554. ASSERT_RTNL();
  555. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  556. ifa_ipv4_policy, extack);
  557. if (err < 0)
  558. goto errout;
  559. ifm = nlmsg_data(nlh);
  560. in_dev = inetdev_by_index(net, ifm->ifa_index);
  561. if (!in_dev) {
  562. err = -ENODEV;
  563. goto errout;
  564. }
  565. for (ifap = &in_dev->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL;
  566. ifap = &ifa->ifa_next) {
  567. if (tb[IFA_LOCAL] &&
  568. ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
  569. continue;
  570. if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
  571. continue;
  572. if (tb[IFA_ADDRESS] &&
  573. (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
  574. !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
  575. continue;
  576. if (ipv4_is_multicast(ifa->ifa_address))
  577. ip_mc_autojoin_config(net, false, ifa);
  578. __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
  579. return 0;
  580. }
  581. err = -EADDRNOTAVAIL;
  582. errout:
  583. return err;
  584. }
  585. #define INFINITY_LIFE_TIME 0xFFFFFFFF
  586. static void check_lifetime(struct work_struct *work)
  587. {
  588. unsigned long now, next, next_sec, next_sched;
  589. struct in_ifaddr *ifa;
  590. struct hlist_node *n;
  591. int i;
  592. now = jiffies;
  593. next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
  594. for (i = 0; i < IN4_ADDR_HSIZE; i++) {
  595. bool change_needed = false;
  596. rcu_read_lock();
  597. hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
  598. unsigned long age;
  599. if (ifa->ifa_flags & IFA_F_PERMANENT)
  600. continue;
  601. /* We try to batch several events at once. */
  602. age = (now - ifa->ifa_tstamp +
  603. ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  604. if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
  605. age >= ifa->ifa_valid_lft) {
  606. change_needed = true;
  607. } else if (ifa->ifa_preferred_lft ==
  608. INFINITY_LIFE_TIME) {
  609. continue;
  610. } else if (age >= ifa->ifa_preferred_lft) {
  611. if (time_before(ifa->ifa_tstamp +
  612. ifa->ifa_valid_lft * HZ, next))
  613. next = ifa->ifa_tstamp +
  614. ifa->ifa_valid_lft * HZ;
  615. if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
  616. change_needed = true;
  617. } else if (time_before(ifa->ifa_tstamp +
  618. ifa->ifa_preferred_lft * HZ,
  619. next)) {
  620. next = ifa->ifa_tstamp +
  621. ifa->ifa_preferred_lft * HZ;
  622. }
  623. }
  624. rcu_read_unlock();
  625. if (!change_needed)
  626. continue;
  627. rtnl_lock();
  628. hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
  629. unsigned long age;
  630. if (ifa->ifa_flags & IFA_F_PERMANENT)
  631. continue;
  632. /* We try to batch several events at once. */
  633. age = (now - ifa->ifa_tstamp +
  634. ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  635. if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
  636. age >= ifa->ifa_valid_lft) {
  637. struct in_ifaddr __rcu **ifap;
  638. struct in_ifaddr *tmp;
  639. ifap = &ifa->ifa_dev->ifa_list;
  640. tmp = rtnl_dereference(*ifap);
  641. while (tmp) {
  642. if (tmp == ifa) {
  643. inet_del_ifa(ifa->ifa_dev,
  644. ifap, 1);
  645. break;
  646. }
  647. ifap = &tmp->ifa_next;
  648. tmp = rtnl_dereference(*ifap);
  649. }
  650. } else if (ifa->ifa_preferred_lft !=
  651. INFINITY_LIFE_TIME &&
  652. age >= ifa->ifa_preferred_lft &&
  653. !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
  654. ifa->ifa_flags |= IFA_F_DEPRECATED;
  655. rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
  656. }
  657. }
  658. rtnl_unlock();
  659. }
  660. next_sec = round_jiffies_up(next);
  661. next_sched = next;
  662. /* If rounded timeout is accurate enough, accept it. */
  663. if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
  664. next_sched = next_sec;
  665. now = jiffies;
  666. /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
  667. if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
  668. next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
  669. queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
  670. next_sched - now);
  671. }
  672. static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
  673. __u32 prefered_lft)
  674. {
  675. unsigned long timeout;
  676. ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
  677. timeout = addrconf_timeout_fixup(valid_lft, HZ);
  678. if (addrconf_finite_timeout(timeout))
  679. ifa->ifa_valid_lft = timeout;
  680. else
  681. ifa->ifa_flags |= IFA_F_PERMANENT;
  682. timeout = addrconf_timeout_fixup(prefered_lft, HZ);
  683. if (addrconf_finite_timeout(timeout)) {
  684. if (timeout == 0)
  685. ifa->ifa_flags |= IFA_F_DEPRECATED;
  686. ifa->ifa_preferred_lft = timeout;
  687. }
  688. ifa->ifa_tstamp = jiffies;
  689. if (!ifa->ifa_cstamp)
  690. ifa->ifa_cstamp = ifa->ifa_tstamp;
  691. }
  692. static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
  693. __u32 *pvalid_lft, __u32 *pprefered_lft,
  694. struct netlink_ext_ack *extack)
  695. {
  696. struct nlattr *tb[IFA_MAX+1];
  697. struct in_ifaddr *ifa;
  698. struct ifaddrmsg *ifm;
  699. struct net_device *dev;
  700. struct in_device *in_dev;
  701. int err;
  702. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  703. ifa_ipv4_policy, extack);
  704. if (err < 0)
  705. goto errout;
  706. ifm = nlmsg_data(nlh);
  707. err = -EINVAL;
  708. if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
  709. goto errout;
  710. dev = __dev_get_by_index(net, ifm->ifa_index);
  711. err = -ENODEV;
  712. if (!dev)
  713. goto errout;
  714. in_dev = __in_dev_get_rtnl(dev);
  715. err = -ENOBUFS;
  716. if (!in_dev)
  717. goto errout;
  718. ifa = inet_alloc_ifa();
  719. if (!ifa)
  720. /*
  721. * A potential indev allocation can be left alive, it stays
  722. * assigned to its device and is destroy with it.
  723. */
  724. goto errout;
  725. ipv4_devconf_setall(in_dev);
  726. neigh_parms_data_state_setall(in_dev->arp_parms);
  727. in_dev_hold(in_dev);
  728. if (!tb[IFA_ADDRESS])
  729. tb[IFA_ADDRESS] = tb[IFA_LOCAL];
  730. INIT_HLIST_NODE(&ifa->hash);
  731. ifa->ifa_prefixlen = ifm->ifa_prefixlen;
  732. ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
  733. ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
  734. ifm->ifa_flags;
  735. ifa->ifa_scope = ifm->ifa_scope;
  736. ifa->ifa_dev = in_dev;
  737. ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
  738. ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
  739. if (tb[IFA_BROADCAST])
  740. ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
  741. if (tb[IFA_LABEL])
  742. nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
  743. else
  744. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  745. if (tb[IFA_RT_PRIORITY])
  746. ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
  747. if (tb[IFA_CACHEINFO]) {
  748. struct ifa_cacheinfo *ci;
  749. ci = nla_data(tb[IFA_CACHEINFO]);
  750. if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
  751. err = -EINVAL;
  752. goto errout_free;
  753. }
  754. *pvalid_lft = ci->ifa_valid;
  755. *pprefered_lft = ci->ifa_prefered;
  756. }
  757. return ifa;
  758. errout_free:
  759. inet_free_ifa(ifa);
  760. errout:
  761. return ERR_PTR(err);
  762. }
  763. static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
  764. {
  765. struct in_device *in_dev = ifa->ifa_dev;
  766. struct in_ifaddr *ifa1;
  767. if (!ifa->ifa_local)
  768. return NULL;
  769. in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
  770. if (ifa1->ifa_mask == ifa->ifa_mask &&
  771. inet_ifa_match(ifa1->ifa_address, ifa) &&
  772. ifa1->ifa_local == ifa->ifa_local)
  773. return ifa1;
  774. }
  775. return NULL;
  776. }
  777. static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
  778. struct netlink_ext_ack *extack)
  779. {
  780. struct net *net = sock_net(skb->sk);
  781. struct in_ifaddr *ifa;
  782. struct in_ifaddr *ifa_existing;
  783. __u32 valid_lft = INFINITY_LIFE_TIME;
  784. __u32 prefered_lft = INFINITY_LIFE_TIME;
  785. ASSERT_RTNL();
  786. ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
  787. if (IS_ERR(ifa))
  788. return PTR_ERR(ifa);
  789. ifa_existing = find_matching_ifa(ifa);
  790. if (!ifa_existing) {
  791. /* It would be best to check for !NLM_F_CREATE here but
  792. * userspace already relies on not having to provide this.
  793. */
  794. set_ifa_lifetime(ifa, valid_lft, prefered_lft);
  795. if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
  796. int ret = ip_mc_autojoin_config(net, true, ifa);
  797. if (ret < 0) {
  798. inet_free_ifa(ifa);
  799. return ret;
  800. }
  801. }
  802. return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
  803. extack);
  804. } else {
  805. u32 new_metric = ifa->ifa_rt_priority;
  806. inet_free_ifa(ifa);
  807. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  808. !(nlh->nlmsg_flags & NLM_F_REPLACE))
  809. return -EEXIST;
  810. ifa = ifa_existing;
  811. if (ifa->ifa_rt_priority != new_metric) {
  812. fib_modify_prefix_metric(ifa, new_metric);
  813. ifa->ifa_rt_priority = new_metric;
  814. }
  815. set_ifa_lifetime(ifa, valid_lft, prefered_lft);
  816. cancel_delayed_work(&check_lifetime_work);
  817. queue_delayed_work(system_power_efficient_wq,
  818. &check_lifetime_work, 0);
  819. rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
  820. }
  821. return 0;
  822. }
  823. /*
  824. * Determine a default network mask, based on the IP address.
  825. */
  826. static int inet_abc_len(__be32 addr)
  827. {
  828. int rc = -1; /* Something else, probably a multicast. */
  829. if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
  830. rc = 0;
  831. else {
  832. __u32 haddr = ntohl(addr);
  833. if (IN_CLASSA(haddr))
  834. rc = 8;
  835. else if (IN_CLASSB(haddr))
  836. rc = 16;
  837. else if (IN_CLASSC(haddr))
  838. rc = 24;
  839. else if (IN_CLASSE(haddr))
  840. rc = 32;
  841. }
  842. return rc;
  843. }
  844. int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
  845. {
  846. struct sockaddr_in sin_orig;
  847. struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
  848. struct in_ifaddr __rcu **ifap = NULL;
  849. struct in_device *in_dev;
  850. struct in_ifaddr *ifa = NULL;
  851. struct net_device *dev;
  852. char *colon;
  853. int ret = -EFAULT;
  854. int tryaddrmatch = 0;
  855. ifr->ifr_name[IFNAMSIZ - 1] = 0;
  856. /* save original address for comparison */
  857. memcpy(&sin_orig, sin, sizeof(*sin));
  858. colon = strchr(ifr->ifr_name, ':');
  859. if (colon)
  860. *colon = 0;
  861. dev_load(net, ifr->ifr_name);
  862. switch (cmd) {
  863. case SIOCGIFADDR: /* Get interface address */
  864. case SIOCGIFBRDADDR: /* Get the broadcast address */
  865. case SIOCGIFDSTADDR: /* Get the destination address */
  866. case SIOCGIFNETMASK: /* Get the netmask for the interface */
  867. /* Note that these ioctls will not sleep,
  868. so that we do not impose a lock.
  869. One day we will be forced to put shlock here (I mean SMP)
  870. */
  871. tryaddrmatch = (sin_orig.sin_family == AF_INET);
  872. memset(sin, 0, sizeof(*sin));
  873. sin->sin_family = AF_INET;
  874. break;
  875. case SIOCSIFFLAGS:
  876. ret = -EPERM;
  877. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  878. goto out;
  879. break;
  880. case SIOCSIFADDR: /* Set interface address (and family) */
  881. case SIOCSIFBRDADDR: /* Set the broadcast address */
  882. case SIOCSIFDSTADDR: /* Set the destination address */
  883. case SIOCSIFNETMASK: /* Set the netmask for the interface */
  884. ret = -EPERM;
  885. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  886. goto out;
  887. ret = -EINVAL;
  888. if (sin->sin_family != AF_INET)
  889. goto out;
  890. break;
  891. default:
  892. ret = -EINVAL;
  893. goto out;
  894. }
  895. rtnl_lock();
  896. ret = -ENODEV;
  897. dev = __dev_get_by_name(net, ifr->ifr_name);
  898. if (!dev)
  899. goto done;
  900. if (colon)
  901. *colon = ':';
  902. in_dev = __in_dev_get_rtnl(dev);
  903. if (in_dev) {
  904. if (tryaddrmatch) {
  905. /* Matthias Andree */
  906. /* compare label and address (4.4BSD style) */
  907. /* note: we only do this for a limited set of ioctls
  908. and only if the original address family was AF_INET.
  909. This is checked above. */
  910. for (ifap = &in_dev->ifa_list;
  911. (ifa = rtnl_dereference(*ifap)) != NULL;
  912. ifap = &ifa->ifa_next) {
  913. if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
  914. sin_orig.sin_addr.s_addr ==
  915. ifa->ifa_local) {
  916. break; /* found */
  917. }
  918. }
  919. }
  920. /* we didn't get a match, maybe the application is
  921. 4.3BSD-style and passed in junk so we fall back to
  922. comparing just the label */
  923. if (!ifa) {
  924. for (ifap = &in_dev->ifa_list;
  925. (ifa = rtnl_dereference(*ifap)) != NULL;
  926. ifap = &ifa->ifa_next)
  927. if (!strcmp(ifr->ifr_name, ifa->ifa_label))
  928. break;
  929. }
  930. }
  931. ret = -EADDRNOTAVAIL;
  932. if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
  933. goto done;
  934. switch (cmd) {
  935. case SIOCGIFADDR: /* Get interface address */
  936. ret = 0;
  937. sin->sin_addr.s_addr = ifa->ifa_local;
  938. break;
  939. case SIOCGIFBRDADDR: /* Get the broadcast address */
  940. ret = 0;
  941. sin->sin_addr.s_addr = ifa->ifa_broadcast;
  942. break;
  943. case SIOCGIFDSTADDR: /* Get the destination address */
  944. ret = 0;
  945. sin->sin_addr.s_addr = ifa->ifa_address;
  946. break;
  947. case SIOCGIFNETMASK: /* Get the netmask for the interface */
  948. ret = 0;
  949. sin->sin_addr.s_addr = ifa->ifa_mask;
  950. break;
  951. case SIOCSIFFLAGS:
  952. if (colon) {
  953. ret = -EADDRNOTAVAIL;
  954. if (!ifa)
  955. break;
  956. ret = 0;
  957. if (!(ifr->ifr_flags & IFF_UP))
  958. inet_del_ifa(in_dev, ifap, 1);
  959. break;
  960. }
  961. ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
  962. break;
  963. case SIOCSIFADDR: /* Set interface address (and family) */
  964. ret = -EINVAL;
  965. if (inet_abc_len(sin->sin_addr.s_addr) < 0)
  966. break;
  967. if (!ifa) {
  968. ret = -ENOBUFS;
  969. ifa = inet_alloc_ifa();
  970. if (!ifa)
  971. break;
  972. INIT_HLIST_NODE(&ifa->hash);
  973. if (colon)
  974. memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
  975. else
  976. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  977. } else {
  978. ret = 0;
  979. if (ifa->ifa_local == sin->sin_addr.s_addr)
  980. break;
  981. inet_del_ifa(in_dev, ifap, 0);
  982. ifa->ifa_broadcast = 0;
  983. ifa->ifa_scope = 0;
  984. }
  985. ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
  986. if (!(dev->flags & IFF_POINTOPOINT)) {
  987. ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
  988. ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
  989. if ((dev->flags & IFF_BROADCAST) &&
  990. ifa->ifa_prefixlen < 31)
  991. ifa->ifa_broadcast = ifa->ifa_address |
  992. ~ifa->ifa_mask;
  993. } else {
  994. ifa->ifa_prefixlen = 32;
  995. ifa->ifa_mask = inet_make_mask(32);
  996. }
  997. set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
  998. ret = inet_set_ifa(dev, ifa);
  999. break;
  1000. case SIOCSIFBRDADDR: /* Set the broadcast address */
  1001. ret = 0;
  1002. if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
  1003. inet_del_ifa(in_dev, ifap, 0);
  1004. ifa->ifa_broadcast = sin->sin_addr.s_addr;
  1005. inet_insert_ifa(ifa);
  1006. }
  1007. break;
  1008. case SIOCSIFDSTADDR: /* Set the destination address */
  1009. ret = 0;
  1010. if (ifa->ifa_address == sin->sin_addr.s_addr)
  1011. break;
  1012. ret = -EINVAL;
  1013. if (inet_abc_len(sin->sin_addr.s_addr) < 0)
  1014. break;
  1015. ret = 0;
  1016. inet_del_ifa(in_dev, ifap, 0);
  1017. ifa->ifa_address = sin->sin_addr.s_addr;
  1018. inet_insert_ifa(ifa);
  1019. break;
  1020. case SIOCSIFNETMASK: /* Set the netmask for the interface */
  1021. /*
  1022. * The mask we set must be legal.
  1023. */
  1024. ret = -EINVAL;
  1025. if (bad_mask(sin->sin_addr.s_addr, 0))
  1026. break;
  1027. ret = 0;
  1028. if (ifa->ifa_mask != sin->sin_addr.s_addr) {
  1029. __be32 old_mask = ifa->ifa_mask;
  1030. inet_del_ifa(in_dev, ifap, 0);
  1031. ifa->ifa_mask = sin->sin_addr.s_addr;
  1032. ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
  1033. /* See if current broadcast address matches
  1034. * with current netmask, then recalculate
  1035. * the broadcast address. Otherwise it's a
  1036. * funny address, so don't touch it since
  1037. * the user seems to know what (s)he's doing...
  1038. */
  1039. if ((dev->flags & IFF_BROADCAST) &&
  1040. (ifa->ifa_prefixlen < 31) &&
  1041. (ifa->ifa_broadcast ==
  1042. (ifa->ifa_local|~old_mask))) {
  1043. ifa->ifa_broadcast = (ifa->ifa_local |
  1044. ~sin->sin_addr.s_addr);
  1045. }
  1046. inet_insert_ifa(ifa);
  1047. }
  1048. break;
  1049. }
  1050. done:
  1051. rtnl_unlock();
  1052. out:
  1053. return ret;
  1054. }
  1055. static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
  1056. {
  1057. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  1058. const struct in_ifaddr *ifa;
  1059. struct ifreq ifr;
  1060. int done = 0;
  1061. if (WARN_ON(size > sizeof(struct ifreq)))
  1062. goto out;
  1063. if (!in_dev)
  1064. goto out;
  1065. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1066. if (!buf) {
  1067. done += size;
  1068. continue;
  1069. }
  1070. if (len < size)
  1071. break;
  1072. memset(&ifr, 0, sizeof(struct ifreq));
  1073. strcpy(ifr.ifr_name, ifa->ifa_label);
  1074. (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
  1075. (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
  1076. ifa->ifa_local;
  1077. if (copy_to_user(buf + done, &ifr, size)) {
  1078. done = -EFAULT;
  1079. break;
  1080. }
  1081. len -= size;
  1082. done += size;
  1083. }
  1084. out:
  1085. return done;
  1086. }
  1087. static __be32 in_dev_select_addr(const struct in_device *in_dev,
  1088. int scope)
  1089. {
  1090. const struct in_ifaddr *ifa;
  1091. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  1092. if (ifa->ifa_flags & IFA_F_SECONDARY)
  1093. continue;
  1094. if (ifa->ifa_scope != RT_SCOPE_LINK &&
  1095. ifa->ifa_scope <= scope)
  1096. return ifa->ifa_local;
  1097. }
  1098. return 0;
  1099. }
  1100. __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
  1101. {
  1102. const struct in_ifaddr *ifa;
  1103. __be32 addr = 0;
  1104. unsigned char localnet_scope = RT_SCOPE_HOST;
  1105. struct in_device *in_dev;
  1106. struct net *net = dev_net(dev);
  1107. int master_idx;
  1108. rcu_read_lock();
  1109. in_dev = __in_dev_get_rcu(dev);
  1110. if (!in_dev)
  1111. goto no_in_dev;
  1112. if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
  1113. localnet_scope = RT_SCOPE_LINK;
  1114. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  1115. if (ifa->ifa_flags & IFA_F_SECONDARY)
  1116. continue;
  1117. if (min(ifa->ifa_scope, localnet_scope) > scope)
  1118. continue;
  1119. if (!dst || inet_ifa_match(dst, ifa)) {
  1120. addr = ifa->ifa_local;
  1121. break;
  1122. }
  1123. if (!addr)
  1124. addr = ifa->ifa_local;
  1125. }
  1126. if (addr)
  1127. goto out_unlock;
  1128. no_in_dev:
  1129. master_idx = l3mdev_master_ifindex_rcu(dev);
  1130. /* For VRFs, the VRF device takes the place of the loopback device,
  1131. * with addresses on it being preferred. Note in such cases the
  1132. * loopback device will be among the devices that fail the master_idx
  1133. * equality check in the loop below.
  1134. */
  1135. if (master_idx &&
  1136. (dev = dev_get_by_index_rcu(net, master_idx)) &&
  1137. (in_dev = __in_dev_get_rcu(dev))) {
  1138. addr = in_dev_select_addr(in_dev, scope);
  1139. if (addr)
  1140. goto out_unlock;
  1141. }
  1142. /* Not loopback addresses on loopback should be preferred
  1143. in this case. It is important that lo is the first interface
  1144. in dev_base list.
  1145. */
  1146. for_each_netdev_rcu(net, dev) {
  1147. if (l3mdev_master_ifindex_rcu(dev) != master_idx)
  1148. continue;
  1149. in_dev = __in_dev_get_rcu(dev);
  1150. if (!in_dev)
  1151. continue;
  1152. addr = in_dev_select_addr(in_dev, scope);
  1153. if (addr)
  1154. goto out_unlock;
  1155. }
  1156. out_unlock:
  1157. rcu_read_unlock();
  1158. return addr;
  1159. }
  1160. EXPORT_SYMBOL(inet_select_addr);
  1161. static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
  1162. __be32 local, int scope)
  1163. {
  1164. unsigned char localnet_scope = RT_SCOPE_HOST;
  1165. const struct in_ifaddr *ifa;
  1166. __be32 addr = 0;
  1167. int same = 0;
  1168. if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
  1169. localnet_scope = RT_SCOPE_LINK;
  1170. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  1171. unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
  1172. if (!addr &&
  1173. (local == ifa->ifa_local || !local) &&
  1174. min_scope <= scope) {
  1175. addr = ifa->ifa_local;
  1176. if (same)
  1177. break;
  1178. }
  1179. if (!same) {
  1180. same = (!local || inet_ifa_match(local, ifa)) &&
  1181. (!dst || inet_ifa_match(dst, ifa));
  1182. if (same && addr) {
  1183. if (local || !dst)
  1184. break;
  1185. /* Is the selected addr into dst subnet? */
  1186. if (inet_ifa_match(addr, ifa))
  1187. break;
  1188. /* No, then can we use new local src? */
  1189. if (min_scope <= scope) {
  1190. addr = ifa->ifa_local;
  1191. break;
  1192. }
  1193. /* search for large dst subnet for addr */
  1194. same = 0;
  1195. }
  1196. }
  1197. }
  1198. return same ? addr : 0;
  1199. }
  1200. /*
  1201. * Confirm that local IP address exists using wildcards:
  1202. * - net: netns to check, cannot be NULL
  1203. * - in_dev: only on this interface, NULL=any interface
  1204. * - dst: only in the same subnet as dst, 0=any dst
  1205. * - local: address, 0=autoselect the local address
  1206. * - scope: maximum allowed scope value for the local address
  1207. */
  1208. __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
  1209. __be32 dst, __be32 local, int scope)
  1210. {
  1211. __be32 addr = 0;
  1212. struct net_device *dev;
  1213. if (in_dev)
  1214. return confirm_addr_indev(in_dev, dst, local, scope);
  1215. rcu_read_lock();
  1216. for_each_netdev_rcu(net, dev) {
  1217. in_dev = __in_dev_get_rcu(dev);
  1218. if (in_dev) {
  1219. addr = confirm_addr_indev(in_dev, dst, local, scope);
  1220. if (addr)
  1221. break;
  1222. }
  1223. }
  1224. rcu_read_unlock();
  1225. return addr;
  1226. }
  1227. EXPORT_SYMBOL(inet_confirm_addr);
  1228. /*
  1229. * Device notifier
  1230. */
  1231. int register_inetaddr_notifier(struct notifier_block *nb)
  1232. {
  1233. return blocking_notifier_chain_register(&inetaddr_chain, nb);
  1234. }
  1235. EXPORT_SYMBOL(register_inetaddr_notifier);
  1236. int unregister_inetaddr_notifier(struct notifier_block *nb)
  1237. {
  1238. return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
  1239. }
  1240. EXPORT_SYMBOL(unregister_inetaddr_notifier);
  1241. int register_inetaddr_validator_notifier(struct notifier_block *nb)
  1242. {
  1243. return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
  1244. }
  1245. EXPORT_SYMBOL(register_inetaddr_validator_notifier);
  1246. int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
  1247. {
  1248. return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
  1249. nb);
  1250. }
  1251. EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
  1252. /* Rename ifa_labels for a device name change. Make some effort to preserve
  1253. * existing alias numbering and to create unique labels if possible.
  1254. */
  1255. static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
  1256. {
  1257. struct in_ifaddr *ifa;
  1258. int named = 0;
  1259. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1260. char old[IFNAMSIZ], *dot;
  1261. memcpy(old, ifa->ifa_label, IFNAMSIZ);
  1262. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  1263. if (named++ == 0)
  1264. goto skip;
  1265. dot = strchr(old, ':');
  1266. if (!dot) {
  1267. sprintf(old, ":%d", named);
  1268. dot = old;
  1269. }
  1270. if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
  1271. strcat(ifa->ifa_label, dot);
  1272. else
  1273. strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
  1274. skip:
  1275. rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
  1276. }
  1277. }
  1278. static void inetdev_send_gratuitous_arp(struct net_device *dev,
  1279. struct in_device *in_dev)
  1280. {
  1281. const struct in_ifaddr *ifa;
  1282. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1283. arp_send(ARPOP_REQUEST, ETH_P_ARP,
  1284. ifa->ifa_local, dev,
  1285. ifa->ifa_local, NULL,
  1286. dev->dev_addr, NULL);
  1287. }
  1288. }
  1289. /* Called only under RTNL semaphore */
  1290. static int inetdev_event(struct notifier_block *this, unsigned long event,
  1291. void *ptr)
  1292. {
  1293. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1294. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  1295. ASSERT_RTNL();
  1296. if (!in_dev) {
  1297. if (event == NETDEV_REGISTER) {
  1298. in_dev = inetdev_init(dev);
  1299. if (IS_ERR(in_dev))
  1300. return notifier_from_errno(PTR_ERR(in_dev));
  1301. if (dev->flags & IFF_LOOPBACK) {
  1302. IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
  1303. IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
  1304. }
  1305. } else if (event == NETDEV_CHANGEMTU) {
  1306. /* Re-enabling IP */
  1307. if (inetdev_valid_mtu(dev->mtu))
  1308. in_dev = inetdev_init(dev);
  1309. }
  1310. goto out;
  1311. }
  1312. switch (event) {
  1313. case NETDEV_REGISTER:
  1314. pr_debug("%s: bug\n", __func__);
  1315. RCU_INIT_POINTER(dev->ip_ptr, NULL);
  1316. break;
  1317. case NETDEV_UP:
  1318. if (!inetdev_valid_mtu(dev->mtu))
  1319. break;
  1320. if (dev->flags & IFF_LOOPBACK) {
  1321. struct in_ifaddr *ifa = inet_alloc_ifa();
  1322. if (ifa) {
  1323. INIT_HLIST_NODE(&ifa->hash);
  1324. ifa->ifa_local =
  1325. ifa->ifa_address = htonl(INADDR_LOOPBACK);
  1326. ifa->ifa_prefixlen = 8;
  1327. ifa->ifa_mask = inet_make_mask(8);
  1328. in_dev_hold(in_dev);
  1329. ifa->ifa_dev = in_dev;
  1330. ifa->ifa_scope = RT_SCOPE_HOST;
  1331. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  1332. set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
  1333. INFINITY_LIFE_TIME);
  1334. ipv4_devconf_setall(in_dev);
  1335. neigh_parms_data_state_setall(in_dev->arp_parms);
  1336. inet_insert_ifa(ifa);
  1337. }
  1338. }
  1339. ip_mc_up(in_dev);
  1340. fallthrough;
  1341. case NETDEV_CHANGEADDR:
  1342. if (!IN_DEV_ARP_NOTIFY(in_dev))
  1343. break;
  1344. fallthrough;
  1345. case NETDEV_NOTIFY_PEERS:
  1346. /* Send gratuitous ARP to notify of link change */
  1347. inetdev_send_gratuitous_arp(dev, in_dev);
  1348. break;
  1349. case NETDEV_DOWN:
  1350. ip_mc_down(in_dev);
  1351. break;
  1352. case NETDEV_PRE_TYPE_CHANGE:
  1353. ip_mc_unmap(in_dev);
  1354. break;
  1355. case NETDEV_POST_TYPE_CHANGE:
  1356. ip_mc_remap(in_dev);
  1357. break;
  1358. case NETDEV_CHANGEMTU:
  1359. if (inetdev_valid_mtu(dev->mtu))
  1360. break;
  1361. /* disable IP when MTU is not enough */
  1362. fallthrough;
  1363. case NETDEV_UNREGISTER:
  1364. inetdev_destroy(in_dev);
  1365. break;
  1366. case NETDEV_CHANGENAME:
  1367. /* Do not notify about label change, this event is
  1368. * not interesting to applications using netlink.
  1369. */
  1370. inetdev_changename(dev, in_dev);
  1371. devinet_sysctl_unregister(in_dev);
  1372. devinet_sysctl_register(in_dev);
  1373. break;
  1374. }
  1375. out:
  1376. return NOTIFY_DONE;
  1377. }
  1378. static struct notifier_block ip_netdev_notifier = {
  1379. .notifier_call = inetdev_event,
  1380. };
  1381. static size_t inet_nlmsg_size(void)
  1382. {
  1383. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  1384. + nla_total_size(4) /* IFA_ADDRESS */
  1385. + nla_total_size(4) /* IFA_LOCAL */
  1386. + nla_total_size(4) /* IFA_BROADCAST */
  1387. + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
  1388. + nla_total_size(4) /* IFA_FLAGS */
  1389. + nla_total_size(4) /* IFA_RT_PRIORITY */
  1390. + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
  1391. }
  1392. static inline u32 cstamp_delta(unsigned long cstamp)
  1393. {
  1394. return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
  1395. }
  1396. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  1397. unsigned long tstamp, u32 preferred, u32 valid)
  1398. {
  1399. struct ifa_cacheinfo ci;
  1400. ci.cstamp = cstamp_delta(cstamp);
  1401. ci.tstamp = cstamp_delta(tstamp);
  1402. ci.ifa_prefered = preferred;
  1403. ci.ifa_valid = valid;
  1404. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  1405. }
  1406. static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
  1407. struct inet_fill_args *args)
  1408. {
  1409. struct ifaddrmsg *ifm;
  1410. struct nlmsghdr *nlh;
  1411. u32 preferred, valid;
  1412. nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
  1413. args->flags);
  1414. if (!nlh)
  1415. return -EMSGSIZE;
  1416. ifm = nlmsg_data(nlh);
  1417. ifm->ifa_family = AF_INET;
  1418. ifm->ifa_prefixlen = ifa->ifa_prefixlen;
  1419. ifm->ifa_flags = ifa->ifa_flags;
  1420. ifm->ifa_scope = ifa->ifa_scope;
  1421. ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
  1422. if (args->netnsid >= 0 &&
  1423. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
  1424. goto nla_put_failure;
  1425. if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
  1426. preferred = ifa->ifa_preferred_lft;
  1427. valid = ifa->ifa_valid_lft;
  1428. if (preferred != INFINITY_LIFE_TIME) {
  1429. long tval = (jiffies - ifa->ifa_tstamp) / HZ;
  1430. if (preferred > tval)
  1431. preferred -= tval;
  1432. else
  1433. preferred = 0;
  1434. if (valid != INFINITY_LIFE_TIME) {
  1435. if (valid > tval)
  1436. valid -= tval;
  1437. else
  1438. valid = 0;
  1439. }
  1440. }
  1441. } else {
  1442. preferred = INFINITY_LIFE_TIME;
  1443. valid = INFINITY_LIFE_TIME;
  1444. }
  1445. if ((ifa->ifa_address &&
  1446. nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
  1447. (ifa->ifa_local &&
  1448. nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
  1449. (ifa->ifa_broadcast &&
  1450. nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
  1451. (ifa->ifa_label[0] &&
  1452. nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
  1453. nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
  1454. (ifa->ifa_rt_priority &&
  1455. nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
  1456. put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
  1457. preferred, valid))
  1458. goto nla_put_failure;
  1459. nlmsg_end(skb, nlh);
  1460. return 0;
  1461. nla_put_failure:
  1462. nlmsg_cancel(skb, nlh);
  1463. return -EMSGSIZE;
  1464. }
  1465. static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
  1466. struct inet_fill_args *fillargs,
  1467. struct net **tgt_net, struct sock *sk,
  1468. struct netlink_callback *cb)
  1469. {
  1470. struct netlink_ext_ack *extack = cb->extack;
  1471. struct nlattr *tb[IFA_MAX+1];
  1472. struct ifaddrmsg *ifm;
  1473. int err, i;
  1474. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  1475. NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
  1476. return -EINVAL;
  1477. }
  1478. ifm = nlmsg_data(nlh);
  1479. if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
  1480. NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
  1481. return -EINVAL;
  1482. }
  1483. fillargs->ifindex = ifm->ifa_index;
  1484. if (fillargs->ifindex) {
  1485. cb->answer_flags |= NLM_F_DUMP_FILTERED;
  1486. fillargs->flags |= NLM_F_DUMP_FILTERED;
  1487. }
  1488. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
  1489. ifa_ipv4_policy, extack);
  1490. if (err < 0)
  1491. return err;
  1492. for (i = 0; i <= IFA_MAX; ++i) {
  1493. if (!tb[i])
  1494. continue;
  1495. if (i == IFA_TARGET_NETNSID) {
  1496. struct net *net;
  1497. fillargs->netnsid = nla_get_s32(tb[i]);
  1498. net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
  1499. if (IS_ERR(net)) {
  1500. fillargs->netnsid = -1;
  1501. NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
  1502. return PTR_ERR(net);
  1503. }
  1504. *tgt_net = net;
  1505. } else {
  1506. NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
  1507. return -EINVAL;
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
  1513. struct netlink_callback *cb, int s_ip_idx,
  1514. struct inet_fill_args *fillargs)
  1515. {
  1516. struct in_ifaddr *ifa;
  1517. int ip_idx = 0;
  1518. int err;
  1519. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1520. if (ip_idx < s_ip_idx) {
  1521. ip_idx++;
  1522. continue;
  1523. }
  1524. err = inet_fill_ifaddr(skb, ifa, fillargs);
  1525. if (err < 0)
  1526. goto done;
  1527. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1528. ip_idx++;
  1529. }
  1530. err = 0;
  1531. done:
  1532. cb->args[2] = ip_idx;
  1533. return err;
  1534. }
  1535. static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  1536. {
  1537. const struct nlmsghdr *nlh = cb->nlh;
  1538. struct inet_fill_args fillargs = {
  1539. .portid = NETLINK_CB(cb->skb).portid,
  1540. .seq = nlh->nlmsg_seq,
  1541. .event = RTM_NEWADDR,
  1542. .flags = NLM_F_MULTI,
  1543. .netnsid = -1,
  1544. };
  1545. struct net *net = sock_net(skb->sk);
  1546. struct net *tgt_net = net;
  1547. int h, s_h;
  1548. int idx, s_idx;
  1549. int s_ip_idx;
  1550. struct net_device *dev;
  1551. struct in_device *in_dev;
  1552. struct hlist_head *head;
  1553. int err = 0;
  1554. s_h = cb->args[0];
  1555. s_idx = idx = cb->args[1];
  1556. s_ip_idx = cb->args[2];
  1557. if (cb->strict_check) {
  1558. err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
  1559. skb->sk, cb);
  1560. if (err < 0)
  1561. goto put_tgt_net;
  1562. err = 0;
  1563. if (fillargs.ifindex) {
  1564. dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
  1565. if (!dev) {
  1566. err = -ENODEV;
  1567. goto put_tgt_net;
  1568. }
  1569. in_dev = __in_dev_get_rtnl(dev);
  1570. if (in_dev) {
  1571. err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
  1572. &fillargs);
  1573. }
  1574. goto put_tgt_net;
  1575. }
  1576. }
  1577. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1578. idx = 0;
  1579. head = &tgt_net->dev_index_head[h];
  1580. rcu_read_lock();
  1581. cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
  1582. tgt_net->dev_base_seq;
  1583. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  1584. if (idx < s_idx)
  1585. goto cont;
  1586. if (h > s_h || idx > s_idx)
  1587. s_ip_idx = 0;
  1588. in_dev = __in_dev_get_rcu(dev);
  1589. if (!in_dev)
  1590. goto cont;
  1591. err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
  1592. &fillargs);
  1593. if (err < 0) {
  1594. rcu_read_unlock();
  1595. goto done;
  1596. }
  1597. cont:
  1598. idx++;
  1599. }
  1600. rcu_read_unlock();
  1601. }
  1602. done:
  1603. cb->args[0] = h;
  1604. cb->args[1] = idx;
  1605. put_tgt_net:
  1606. if (fillargs.netnsid >= 0)
  1607. put_net(tgt_net);
  1608. return skb->len ? : err;
  1609. }
  1610. static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
  1611. u32 portid)
  1612. {
  1613. struct inet_fill_args fillargs = {
  1614. .portid = portid,
  1615. .seq = nlh ? nlh->nlmsg_seq : 0,
  1616. .event = event,
  1617. .flags = 0,
  1618. .netnsid = -1,
  1619. };
  1620. struct sk_buff *skb;
  1621. int err = -ENOBUFS;
  1622. struct net *net;
  1623. net = dev_net(ifa->ifa_dev->dev);
  1624. skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
  1625. if (!skb)
  1626. goto errout;
  1627. err = inet_fill_ifaddr(skb, ifa, &fillargs);
  1628. if (err < 0) {
  1629. /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
  1630. WARN_ON(err == -EMSGSIZE);
  1631. kfree_skb(skb);
  1632. goto errout;
  1633. }
  1634. rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
  1635. return;
  1636. errout:
  1637. if (err < 0)
  1638. rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
  1639. }
  1640. static size_t inet_get_link_af_size(const struct net_device *dev,
  1641. u32 ext_filter_mask)
  1642. {
  1643. struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  1644. if (!in_dev)
  1645. return 0;
  1646. return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
  1647. }
  1648. static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
  1649. u32 ext_filter_mask)
  1650. {
  1651. struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  1652. struct nlattr *nla;
  1653. int i;
  1654. if (!in_dev)
  1655. return -ENODATA;
  1656. nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
  1657. if (!nla)
  1658. return -EMSGSIZE;
  1659. for (i = 0; i < IPV4_DEVCONF_MAX; i++)
  1660. ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
  1661. return 0;
  1662. }
  1663. static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
  1664. [IFLA_INET_CONF] = { .type = NLA_NESTED },
  1665. };
  1666. static int inet_validate_link_af(const struct net_device *dev,
  1667. const struct nlattr *nla)
  1668. {
  1669. struct nlattr *a, *tb[IFLA_INET_MAX+1];
  1670. int err, rem;
  1671. if (dev && !__in_dev_get_rcu(dev))
  1672. return -EAFNOSUPPORT;
  1673. err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
  1674. inet_af_policy, NULL);
  1675. if (err < 0)
  1676. return err;
  1677. if (tb[IFLA_INET_CONF]) {
  1678. nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
  1679. int cfgid = nla_type(a);
  1680. if (nla_len(a) < 4)
  1681. return -EINVAL;
  1682. if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
  1683. return -EINVAL;
  1684. }
  1685. }
  1686. return 0;
  1687. }
  1688. static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
  1689. {
  1690. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1691. struct nlattr *a, *tb[IFLA_INET_MAX+1];
  1692. int rem;
  1693. if (!in_dev)
  1694. return -EAFNOSUPPORT;
  1695. if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
  1696. return -EINVAL;
  1697. if (tb[IFLA_INET_CONF]) {
  1698. nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
  1699. ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
  1700. }
  1701. return 0;
  1702. }
  1703. static int inet_netconf_msgsize_devconf(int type)
  1704. {
  1705. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  1706. + nla_total_size(4); /* NETCONFA_IFINDEX */
  1707. bool all = false;
  1708. if (type == NETCONFA_ALL)
  1709. all = true;
  1710. if (all || type == NETCONFA_FORWARDING)
  1711. size += nla_total_size(4);
  1712. if (all || type == NETCONFA_RP_FILTER)
  1713. size += nla_total_size(4);
  1714. if (all || type == NETCONFA_MC_FORWARDING)
  1715. size += nla_total_size(4);
  1716. if (all || type == NETCONFA_BC_FORWARDING)
  1717. size += nla_total_size(4);
  1718. if (all || type == NETCONFA_PROXY_NEIGH)
  1719. size += nla_total_size(4);
  1720. if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
  1721. size += nla_total_size(4);
  1722. return size;
  1723. }
  1724. static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
  1725. struct ipv4_devconf *devconf, u32 portid,
  1726. u32 seq, int event, unsigned int flags,
  1727. int type)
  1728. {
  1729. struct nlmsghdr *nlh;
  1730. struct netconfmsg *ncm;
  1731. bool all = false;
  1732. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  1733. flags);
  1734. if (!nlh)
  1735. return -EMSGSIZE;
  1736. if (type == NETCONFA_ALL)
  1737. all = true;
  1738. ncm = nlmsg_data(nlh);
  1739. ncm->ncm_family = AF_INET;
  1740. if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
  1741. goto nla_put_failure;
  1742. if (!devconf)
  1743. goto out;
  1744. if ((all || type == NETCONFA_FORWARDING) &&
  1745. nla_put_s32(skb, NETCONFA_FORWARDING,
  1746. IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
  1747. goto nla_put_failure;
  1748. if ((all || type == NETCONFA_RP_FILTER) &&
  1749. nla_put_s32(skb, NETCONFA_RP_FILTER,
  1750. IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
  1751. goto nla_put_failure;
  1752. if ((all || type == NETCONFA_MC_FORWARDING) &&
  1753. nla_put_s32(skb, NETCONFA_MC_FORWARDING,
  1754. IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
  1755. goto nla_put_failure;
  1756. if ((all || type == NETCONFA_BC_FORWARDING) &&
  1757. nla_put_s32(skb, NETCONFA_BC_FORWARDING,
  1758. IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
  1759. goto nla_put_failure;
  1760. if ((all || type == NETCONFA_PROXY_NEIGH) &&
  1761. nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
  1762. IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
  1763. goto nla_put_failure;
  1764. if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
  1765. nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  1766. IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
  1767. goto nla_put_failure;
  1768. out:
  1769. nlmsg_end(skb, nlh);
  1770. return 0;
  1771. nla_put_failure:
  1772. nlmsg_cancel(skb, nlh);
  1773. return -EMSGSIZE;
  1774. }
  1775. void inet_netconf_notify_devconf(struct net *net, int event, int type,
  1776. int ifindex, struct ipv4_devconf *devconf)
  1777. {
  1778. struct sk_buff *skb;
  1779. int err = -ENOBUFS;
  1780. skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
  1781. if (!skb)
  1782. goto errout;
  1783. err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
  1784. event, 0, type);
  1785. if (err < 0) {
  1786. /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
  1787. WARN_ON(err == -EMSGSIZE);
  1788. kfree_skb(skb);
  1789. goto errout;
  1790. }
  1791. rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
  1792. return;
  1793. errout:
  1794. if (err < 0)
  1795. rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
  1796. }
  1797. static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
  1798. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  1799. [NETCONFA_FORWARDING] = { .len = sizeof(int) },
  1800. [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
  1801. [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
  1802. [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
  1803. };
  1804. static int inet_netconf_valid_get_req(struct sk_buff *skb,
  1805. const struct nlmsghdr *nlh,
  1806. struct nlattr **tb,
  1807. struct netlink_ext_ack *extack)
  1808. {
  1809. int i, err;
  1810. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  1811. NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request");
  1812. return -EINVAL;
  1813. }
  1814. if (!netlink_strict_get_check(skb))
  1815. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  1816. tb, NETCONFA_MAX,
  1817. devconf_ipv4_policy, extack);
  1818. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  1819. tb, NETCONFA_MAX,
  1820. devconf_ipv4_policy, extack);
  1821. if (err)
  1822. return err;
  1823. for (i = 0; i <= NETCONFA_MAX; i++) {
  1824. if (!tb[i])
  1825. continue;
  1826. switch (i) {
  1827. case NETCONFA_IFINDEX:
  1828. break;
  1829. default:
  1830. NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request");
  1831. return -EINVAL;
  1832. }
  1833. }
  1834. return 0;
  1835. }
  1836. static int inet_netconf_get_devconf(struct sk_buff *in_skb,
  1837. struct nlmsghdr *nlh,
  1838. struct netlink_ext_ack *extack)
  1839. {
  1840. struct net *net = sock_net(in_skb->sk);
  1841. struct nlattr *tb[NETCONFA_MAX+1];
  1842. struct sk_buff *skb;
  1843. struct ipv4_devconf *devconf;
  1844. struct in_device *in_dev;
  1845. struct net_device *dev;
  1846. int ifindex;
  1847. int err;
  1848. err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack);
  1849. if (err)
  1850. goto errout;
  1851. err = -EINVAL;
  1852. if (!tb[NETCONFA_IFINDEX])
  1853. goto errout;
  1854. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1855. switch (ifindex) {
  1856. case NETCONFA_IFINDEX_ALL:
  1857. devconf = net->ipv4.devconf_all;
  1858. break;
  1859. case NETCONFA_IFINDEX_DEFAULT:
  1860. devconf = net->ipv4.devconf_dflt;
  1861. break;
  1862. default:
  1863. dev = __dev_get_by_index(net, ifindex);
  1864. if (!dev)
  1865. goto errout;
  1866. in_dev = __in_dev_get_rtnl(dev);
  1867. if (!in_dev)
  1868. goto errout;
  1869. devconf = &in_dev->cnf;
  1870. break;
  1871. }
  1872. err = -ENOBUFS;
  1873. skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1874. if (!skb)
  1875. goto errout;
  1876. err = inet_netconf_fill_devconf(skb, ifindex, devconf,
  1877. NETLINK_CB(in_skb).portid,
  1878. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1879. NETCONFA_ALL);
  1880. if (err < 0) {
  1881. /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
  1882. WARN_ON(err == -EMSGSIZE);
  1883. kfree_skb(skb);
  1884. goto errout;
  1885. }
  1886. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1887. errout:
  1888. return err;
  1889. }
  1890. static int inet_netconf_dump_devconf(struct sk_buff *skb,
  1891. struct netlink_callback *cb)
  1892. {
  1893. const struct nlmsghdr *nlh = cb->nlh;
  1894. struct net *net = sock_net(skb->sk);
  1895. int h, s_h;
  1896. int idx, s_idx;
  1897. struct net_device *dev;
  1898. struct in_device *in_dev;
  1899. struct hlist_head *head;
  1900. if (cb->strict_check) {
  1901. struct netlink_ext_ack *extack = cb->extack;
  1902. struct netconfmsg *ncm;
  1903. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  1904. NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
  1905. return -EINVAL;
  1906. }
  1907. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  1908. NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
  1909. return -EINVAL;
  1910. }
  1911. }
  1912. s_h = cb->args[0];
  1913. s_idx = idx = cb->args[1];
  1914. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1915. idx = 0;
  1916. head = &net->dev_index_head[h];
  1917. rcu_read_lock();
  1918. cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
  1919. net->dev_base_seq;
  1920. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  1921. if (idx < s_idx)
  1922. goto cont;
  1923. in_dev = __in_dev_get_rcu(dev);
  1924. if (!in_dev)
  1925. goto cont;
  1926. if (inet_netconf_fill_devconf(skb, dev->ifindex,
  1927. &in_dev->cnf,
  1928. NETLINK_CB(cb->skb).portid,
  1929. nlh->nlmsg_seq,
  1930. RTM_NEWNETCONF,
  1931. NLM_F_MULTI,
  1932. NETCONFA_ALL) < 0) {
  1933. rcu_read_unlock();
  1934. goto done;
  1935. }
  1936. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1937. cont:
  1938. idx++;
  1939. }
  1940. rcu_read_unlock();
  1941. }
  1942. if (h == NETDEV_HASHENTRIES) {
  1943. if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
  1944. net->ipv4.devconf_all,
  1945. NETLINK_CB(cb->skb).portid,
  1946. nlh->nlmsg_seq,
  1947. RTM_NEWNETCONF, NLM_F_MULTI,
  1948. NETCONFA_ALL) < 0)
  1949. goto done;
  1950. else
  1951. h++;
  1952. }
  1953. if (h == NETDEV_HASHENTRIES + 1) {
  1954. if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
  1955. net->ipv4.devconf_dflt,
  1956. NETLINK_CB(cb->skb).portid,
  1957. nlh->nlmsg_seq,
  1958. RTM_NEWNETCONF, NLM_F_MULTI,
  1959. NETCONFA_ALL) < 0)
  1960. goto done;
  1961. else
  1962. h++;
  1963. }
  1964. done:
  1965. cb->args[0] = h;
  1966. cb->args[1] = idx;
  1967. return skb->len;
  1968. }
  1969. #ifdef CONFIG_SYSCTL
  1970. static void devinet_copy_dflt_conf(struct net *net, int i)
  1971. {
  1972. struct net_device *dev;
  1973. rcu_read_lock();
  1974. for_each_netdev_rcu(net, dev) {
  1975. struct in_device *in_dev;
  1976. in_dev = __in_dev_get_rcu(dev);
  1977. if (in_dev && !test_bit(i, in_dev->cnf.state))
  1978. in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
  1979. }
  1980. rcu_read_unlock();
  1981. }
  1982. /* called with RTNL locked */
  1983. static void inet_forward_change(struct net *net)
  1984. {
  1985. struct net_device *dev;
  1986. int on = IPV4_DEVCONF_ALL(net, FORWARDING);
  1987. IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
  1988. IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
  1989. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1990. NETCONFA_FORWARDING,
  1991. NETCONFA_IFINDEX_ALL,
  1992. net->ipv4.devconf_all);
  1993. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1994. NETCONFA_FORWARDING,
  1995. NETCONFA_IFINDEX_DEFAULT,
  1996. net->ipv4.devconf_dflt);
  1997. for_each_netdev(net, dev) {
  1998. struct in_device *in_dev;
  1999. if (on)
  2000. dev_disable_lro(dev);
  2001. in_dev = __in_dev_get_rtnl(dev);
  2002. if (in_dev) {
  2003. IN_DEV_CONF_SET(in_dev, FORWARDING, on);
  2004. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2005. NETCONFA_FORWARDING,
  2006. dev->ifindex, &in_dev->cnf);
  2007. }
  2008. }
  2009. }
  2010. static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
  2011. {
  2012. if (cnf == net->ipv4.devconf_dflt)
  2013. return NETCONFA_IFINDEX_DEFAULT;
  2014. else if (cnf == net->ipv4.devconf_all)
  2015. return NETCONFA_IFINDEX_ALL;
  2016. else {
  2017. struct in_device *idev
  2018. = container_of(cnf, struct in_device, cnf);
  2019. return idev->dev->ifindex;
  2020. }
  2021. }
  2022. static int devinet_conf_proc(struct ctl_table *ctl, int write,
  2023. void *buffer, size_t *lenp, loff_t *ppos)
  2024. {
  2025. int old_value = *(int *)ctl->data;
  2026. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  2027. int new_value = *(int *)ctl->data;
  2028. if (write) {
  2029. struct ipv4_devconf *cnf = ctl->extra1;
  2030. struct net *net = ctl->extra2;
  2031. int i = (int *)ctl->data - cnf->data;
  2032. int ifindex;
  2033. set_bit(i, cnf->state);
  2034. if (cnf == net->ipv4.devconf_dflt)
  2035. devinet_copy_dflt_conf(net, i);
  2036. if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
  2037. i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
  2038. if ((new_value == 0) && (old_value != 0))
  2039. rt_cache_flush(net);
  2040. if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
  2041. new_value != old_value)
  2042. rt_cache_flush(net);
  2043. if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
  2044. new_value != old_value) {
  2045. ifindex = devinet_conf_ifindex(net, cnf);
  2046. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2047. NETCONFA_RP_FILTER,
  2048. ifindex, cnf);
  2049. }
  2050. if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
  2051. new_value != old_value) {
  2052. ifindex = devinet_conf_ifindex(net, cnf);
  2053. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2054. NETCONFA_PROXY_NEIGH,
  2055. ifindex, cnf);
  2056. }
  2057. if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
  2058. new_value != old_value) {
  2059. ifindex = devinet_conf_ifindex(net, cnf);
  2060. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2061. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  2062. ifindex, cnf);
  2063. }
  2064. }
  2065. return ret;
  2066. }
  2067. static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
  2068. void *buffer, size_t *lenp, loff_t *ppos)
  2069. {
  2070. int *valp = ctl->data;
  2071. int val = *valp;
  2072. loff_t pos = *ppos;
  2073. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  2074. if (write && *valp != val) {
  2075. struct net *net = ctl->extra2;
  2076. if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
  2077. if (!rtnl_trylock()) {
  2078. /* Restore the original values before restarting */
  2079. *valp = val;
  2080. *ppos = pos;
  2081. return restart_syscall();
  2082. }
  2083. if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
  2084. inet_forward_change(net);
  2085. } else {
  2086. struct ipv4_devconf *cnf = ctl->extra1;
  2087. struct in_device *idev =
  2088. container_of(cnf, struct in_device, cnf);
  2089. if (*valp)
  2090. dev_disable_lro(idev->dev);
  2091. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2092. NETCONFA_FORWARDING,
  2093. idev->dev->ifindex,
  2094. cnf);
  2095. }
  2096. rtnl_unlock();
  2097. rt_cache_flush(net);
  2098. } else
  2099. inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
  2100. NETCONFA_FORWARDING,
  2101. NETCONFA_IFINDEX_DEFAULT,
  2102. net->ipv4.devconf_dflt);
  2103. }
  2104. return ret;
  2105. }
  2106. static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
  2107. void *buffer, size_t *lenp, loff_t *ppos)
  2108. {
  2109. int *valp = ctl->data;
  2110. int val = *valp;
  2111. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  2112. struct net *net = ctl->extra2;
  2113. if (write && *valp != val)
  2114. rt_cache_flush(net);
  2115. return ret;
  2116. }
  2117. #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
  2118. { \
  2119. .procname = name, \
  2120. .data = ipv4_devconf.data + \
  2121. IPV4_DEVCONF_ ## attr - 1, \
  2122. .maxlen = sizeof(int), \
  2123. .mode = mval, \
  2124. .proc_handler = proc, \
  2125. .extra1 = &ipv4_devconf, \
  2126. }
  2127. #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
  2128. DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
  2129. #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
  2130. DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
  2131. #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
  2132. DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
  2133. #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
  2134. DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
  2135. static struct devinet_sysctl_table {
  2136. struct ctl_table_header *sysctl_header;
  2137. struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
  2138. } devinet_sysctl = {
  2139. .devinet_vars = {
  2140. DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
  2141. devinet_sysctl_forward),
  2142. DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
  2143. DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
  2144. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
  2145. DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
  2146. DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
  2147. DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
  2148. DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
  2149. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
  2150. "accept_source_route"),
  2151. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
  2152. DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
  2153. DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
  2154. DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
  2155. DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
  2156. DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
  2157. DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
  2158. DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
  2159. DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
  2160. DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
  2161. DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
  2162. DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
  2163. DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
  2164. DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
  2165. "force_igmp_version"),
  2166. DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
  2167. "igmpv2_unsolicited_report_interval"),
  2168. DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
  2169. "igmpv3_unsolicited_report_interval"),
  2170. DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
  2171. "ignore_routes_with_linkdown"),
  2172. DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
  2173. "drop_gratuitous_arp"),
  2174. DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
  2175. DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
  2176. DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
  2177. "promote_secondaries"),
  2178. DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
  2179. "route_localnet"),
  2180. DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
  2181. "drop_unicast_in_l2_multicast"),
  2182. },
  2183. };
  2184. static int __devinet_sysctl_register(struct net *net, char *dev_name,
  2185. int ifindex, struct ipv4_devconf *p)
  2186. {
  2187. int i;
  2188. struct devinet_sysctl_table *t;
  2189. char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
  2190. t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
  2191. if (!t)
  2192. goto out;
  2193. for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
  2194. t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
  2195. t->devinet_vars[i].extra1 = p;
  2196. t->devinet_vars[i].extra2 = net;
  2197. }
  2198. snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
  2199. t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
  2200. if (!t->sysctl_header)
  2201. goto free;
  2202. p->sysctl = t;
  2203. inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
  2204. ifindex, p);
  2205. return 0;
  2206. free:
  2207. kfree(t);
  2208. out:
  2209. return -ENOMEM;
  2210. }
  2211. static void __devinet_sysctl_unregister(struct net *net,
  2212. struct ipv4_devconf *cnf, int ifindex)
  2213. {
  2214. struct devinet_sysctl_table *t = cnf->sysctl;
  2215. if (t) {
  2216. cnf->sysctl = NULL;
  2217. unregister_net_sysctl_table(t->sysctl_header);
  2218. kfree(t);
  2219. }
  2220. inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
  2221. }
  2222. static int devinet_sysctl_register(struct in_device *idev)
  2223. {
  2224. int err;
  2225. if (!sysctl_dev_name_is_allowed(idev->dev->name))
  2226. return -EINVAL;
  2227. err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
  2228. if (err)
  2229. return err;
  2230. err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
  2231. idev->dev->ifindex, &idev->cnf);
  2232. if (err)
  2233. neigh_sysctl_unregister(idev->arp_parms);
  2234. return err;
  2235. }
  2236. static void devinet_sysctl_unregister(struct in_device *idev)
  2237. {
  2238. struct net *net = dev_net(idev->dev);
  2239. __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
  2240. neigh_sysctl_unregister(idev->arp_parms);
  2241. }
  2242. static struct ctl_table ctl_forward_entry[] = {
  2243. {
  2244. .procname = "ip_forward",
  2245. .data = &ipv4_devconf.data[
  2246. IPV4_DEVCONF_FORWARDING - 1],
  2247. .maxlen = sizeof(int),
  2248. .mode = 0644,
  2249. .proc_handler = devinet_sysctl_forward,
  2250. .extra1 = &ipv4_devconf,
  2251. .extra2 = &init_net,
  2252. },
  2253. { },
  2254. };
  2255. #endif
  2256. static __net_init int devinet_init_net(struct net *net)
  2257. {
  2258. int err;
  2259. struct ipv4_devconf *all, *dflt;
  2260. #ifdef CONFIG_SYSCTL
  2261. struct ctl_table *tbl;
  2262. struct ctl_table_header *forw_hdr;
  2263. #endif
  2264. err = -ENOMEM;
  2265. all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
  2266. if (!all)
  2267. goto err_alloc_all;
  2268. dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
  2269. if (!dflt)
  2270. goto err_alloc_dflt;
  2271. #ifdef CONFIG_SYSCTL
  2272. tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
  2273. if (!tbl)
  2274. goto err_alloc_ctl;
  2275. tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
  2276. tbl[0].extra1 = all;
  2277. tbl[0].extra2 = net;
  2278. #endif
  2279. if (!net_eq(net, &init_net)) {
  2280. if (IS_ENABLED(CONFIG_SYSCTL) &&
  2281. sysctl_devconf_inherit_init_net == 3) {
  2282. /* copy from the current netns */
  2283. memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all,
  2284. sizeof(ipv4_devconf));
  2285. memcpy(dflt,
  2286. current->nsproxy->net_ns->ipv4.devconf_dflt,
  2287. sizeof(ipv4_devconf_dflt));
  2288. } else if (!IS_ENABLED(CONFIG_SYSCTL) ||
  2289. sysctl_devconf_inherit_init_net != 2) {
  2290. /* inherit == 0 or 1: copy from init_net */
  2291. memcpy(all, init_net.ipv4.devconf_all,
  2292. sizeof(ipv4_devconf));
  2293. memcpy(dflt, init_net.ipv4.devconf_dflt,
  2294. sizeof(ipv4_devconf_dflt));
  2295. }
  2296. /* else inherit == 2: use compiled values */
  2297. }
  2298. #ifdef CONFIG_SYSCTL
  2299. err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
  2300. if (err < 0)
  2301. goto err_reg_all;
  2302. err = __devinet_sysctl_register(net, "default",
  2303. NETCONFA_IFINDEX_DEFAULT, dflt);
  2304. if (err < 0)
  2305. goto err_reg_dflt;
  2306. err = -ENOMEM;
  2307. forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
  2308. if (!forw_hdr)
  2309. goto err_reg_ctl;
  2310. net->ipv4.forw_hdr = forw_hdr;
  2311. #endif
  2312. net->ipv4.devconf_all = all;
  2313. net->ipv4.devconf_dflt = dflt;
  2314. return 0;
  2315. #ifdef CONFIG_SYSCTL
  2316. err_reg_ctl:
  2317. __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
  2318. err_reg_dflt:
  2319. __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
  2320. err_reg_all:
  2321. kfree(tbl);
  2322. err_alloc_ctl:
  2323. #endif
  2324. kfree(dflt);
  2325. err_alloc_dflt:
  2326. kfree(all);
  2327. err_alloc_all:
  2328. return err;
  2329. }
  2330. static __net_exit void devinet_exit_net(struct net *net)
  2331. {
  2332. #ifdef CONFIG_SYSCTL
  2333. struct ctl_table *tbl;
  2334. tbl = net->ipv4.forw_hdr->ctl_table_arg;
  2335. unregister_net_sysctl_table(net->ipv4.forw_hdr);
  2336. __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
  2337. NETCONFA_IFINDEX_DEFAULT);
  2338. __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
  2339. NETCONFA_IFINDEX_ALL);
  2340. kfree(tbl);
  2341. #endif
  2342. kfree(net->ipv4.devconf_dflt);
  2343. kfree(net->ipv4.devconf_all);
  2344. }
  2345. static __net_initdata struct pernet_operations devinet_ops = {
  2346. .init = devinet_init_net,
  2347. .exit = devinet_exit_net,
  2348. };
  2349. static struct rtnl_af_ops inet_af_ops __read_mostly = {
  2350. .family = AF_INET,
  2351. .fill_link_af = inet_fill_link_af,
  2352. .get_link_af_size = inet_get_link_af_size,
  2353. .validate_link_af = inet_validate_link_af,
  2354. .set_link_af = inet_set_link_af,
  2355. };
  2356. void __init devinet_init(void)
  2357. {
  2358. int i;
  2359. for (i = 0; i < IN4_ADDR_HSIZE; i++)
  2360. INIT_HLIST_HEAD(&inet_addr_lst[i]);
  2361. register_pernet_subsys(&devinet_ops);
  2362. register_gifconf(PF_INET, inet_gifconf);
  2363. register_netdevice_notifier(&ip_netdev_notifier);
  2364. queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
  2365. rtnl_af_register(&inet_af_ops);
  2366. rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
  2367. rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
  2368. rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
  2369. rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
  2370. inet_netconf_dump_devconf, 0);
  2371. }