devinet.c 60 KB

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