devinet.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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 <asm/system.h>
  29. #include <linux/bitops.h>
  30. #include <linux/capability.h>
  31. #include <linux/module.h>
  32. #include <linux/types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/string.h>
  35. #include <linux/mm.h>
  36. #include <linux/socket.h>
  37. #include <linux/sockios.h>
  38. #include <linux/in.h>
  39. #include <linux/errno.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/if_addr.h>
  42. #include <linux/if_ether.h>
  43. #include <linux/inet.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/init.h>
  48. #include <linux/notifier.h>
  49. #include <linux/inetdevice.h>
  50. #include <linux/igmp.h>
  51. #include <linux/slab.h>
  52. #include <linux/hash.h>
  53. #ifdef CONFIG_SYSCTL
  54. #include <linux/sysctl.h>
  55. #endif
  56. #include <linux/kmod.h>
  57. #include <net/arp.h>
  58. #include <net/ip.h>
  59. #include <net/tcp.h>
  60. #include <net/route.h>
  61. #include <net/ip_fib.h>
  62. #include <net/rtnetlink.h>
  63. #include <net/net_namespace.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. },
  72. };
  73. static struct ipv4_devconf ipv4_devconf_dflt = {
  74. .data = {
  75. [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
  76. [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
  77. [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
  78. [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
  79. [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
  80. },
  81. };
  82. #define IPV4_DEVCONF_DFLT(net, attr) \
  83. IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
  84. static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
  85. [IFA_LOCAL] = { .type = NLA_U32 },
  86. [IFA_ADDRESS] = { .type = NLA_U32 },
  87. [IFA_BROADCAST] = { .type = NLA_U32 },
  88. [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
  89. };
  90. /* inet_addr_hash's shifting is dependent upon this IN4_ADDR_HSIZE
  91. * value. So if you change this define, make appropriate changes to
  92. * inet_addr_hash as well.
  93. */
  94. #define IN4_ADDR_HSIZE 256
  95. static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
  96. static DEFINE_SPINLOCK(inet_addr_hash_lock);
  97. static inline unsigned int inet_addr_hash(struct net *net, __be32 addr)
  98. {
  99. u32 val = (__force u32) addr ^ hash_ptr(net, 8);
  100. return ((val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24)) &
  101. (IN4_ADDR_HSIZE - 1));
  102. }
  103. static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
  104. {
  105. unsigned int hash = inet_addr_hash(net, ifa->ifa_local);
  106. spin_lock(&inet_addr_hash_lock);
  107. hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
  108. spin_unlock(&inet_addr_hash_lock);
  109. }
  110. static void inet_hash_remove(struct in_ifaddr *ifa)
  111. {
  112. spin_lock(&inet_addr_hash_lock);
  113. hlist_del_init_rcu(&ifa->hash);
  114. spin_unlock(&inet_addr_hash_lock);
  115. }
  116. /**
  117. * __ip_dev_find - find the first device with a given source address.
  118. * @net: the net namespace
  119. * @addr: the source address
  120. * @devref: if true, take a reference on the found device
  121. *
  122. * If a caller uses devref=false, it should be protected by RCU, or RTNL
  123. */
  124. struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
  125. {
  126. unsigned int hash = inet_addr_hash(net, addr);
  127. struct net_device *result = NULL;
  128. struct in_ifaddr *ifa;
  129. struct hlist_node *node;
  130. rcu_read_lock();
  131. hlist_for_each_entry_rcu(ifa, node, &inet_addr_lst[hash], hash) {
  132. struct net_device *dev = ifa->ifa_dev->dev;
  133. if (!net_eq(dev_net(dev), net))
  134. continue;
  135. if (ifa->ifa_local == addr) {
  136. result = dev;
  137. break;
  138. }
  139. }
  140. if (!result) {
  141. struct flowi4 fl4 = { .daddr = addr };
  142. struct fib_result res = { 0 };
  143. struct fib_table *local;
  144. /* Fallback to FIB local table so that communication
  145. * over loopback subnets work.
  146. */
  147. local = fib_get_table(net, RT_TABLE_LOCAL);
  148. if (local &&
  149. !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
  150. res.type == RTN_LOCAL)
  151. result = FIB_RES_DEV(res);
  152. }
  153. if (result && devref)
  154. dev_hold(result);
  155. rcu_read_unlock();
  156. return result;
  157. }
  158. EXPORT_SYMBOL(__ip_dev_find);
  159. static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
  160. static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
  161. static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
  162. int destroy);
  163. #ifdef CONFIG_SYSCTL
  164. static void devinet_sysctl_register(struct in_device *idev);
  165. static void devinet_sysctl_unregister(struct in_device *idev);
  166. #else
  167. static inline void devinet_sysctl_register(struct in_device *idev)
  168. {
  169. }
  170. static inline void devinet_sysctl_unregister(struct in_device *idev)
  171. {
  172. }
  173. #endif
  174. /* Locks all the inet devices. */
  175. static struct in_ifaddr *inet_alloc_ifa(void)
  176. {
  177. return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
  178. }
  179. static void inet_rcu_free_ifa(struct rcu_head *head)
  180. {
  181. struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
  182. if (ifa->ifa_dev)
  183. in_dev_put(ifa->ifa_dev);
  184. kfree(ifa);
  185. }
  186. static inline void inet_free_ifa(struct in_ifaddr *ifa)
  187. {
  188. call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
  189. }
  190. void in_dev_finish_destroy(struct in_device *idev)
  191. {
  192. struct net_device *dev = idev->dev;
  193. WARN_ON(idev->ifa_list);
  194. WARN_ON(idev->mc_list);
  195. #ifdef NET_REFCNT_DEBUG
  196. printk(KERN_DEBUG "in_dev_finish_destroy: %p=%s\n",
  197. idev, dev ? dev->name : "NIL");
  198. #endif
  199. dev_put(dev);
  200. if (!idev->dead)
  201. pr_err("Freeing alive in_device %p\n", idev);
  202. else
  203. kfree(idev);
  204. }
  205. EXPORT_SYMBOL(in_dev_finish_destroy);
  206. static struct in_device *inetdev_init(struct net_device *dev)
  207. {
  208. struct in_device *in_dev;
  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. devinet_sysctl_register(in_dev);
  227. ip_mc_init_dev(in_dev);
  228. if (dev->flags & IFF_UP)
  229. ip_mc_up(in_dev);
  230. /* we can receive as soon as ip_ptr is set -- do this last */
  231. rcu_assign_pointer(dev->ip_ptr, in_dev);
  232. out:
  233. return in_dev;
  234. out_kfree:
  235. kfree(in_dev);
  236. in_dev = NULL;
  237. goto out;
  238. }
  239. static void in_dev_rcu_put(struct rcu_head *head)
  240. {
  241. struct in_device *idev = container_of(head, struct in_device, rcu_head);
  242. in_dev_put(idev);
  243. }
  244. static void inetdev_destroy(struct in_device *in_dev)
  245. {
  246. struct in_ifaddr *ifa;
  247. struct net_device *dev;
  248. ASSERT_RTNL();
  249. dev = in_dev->dev;
  250. in_dev->dead = 1;
  251. ip_mc_destroy_dev(in_dev);
  252. while ((ifa = in_dev->ifa_list) != NULL) {
  253. inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
  254. inet_free_ifa(ifa);
  255. }
  256. rcu_assign_pointer(dev->ip_ptr, NULL);
  257. devinet_sysctl_unregister(in_dev);
  258. neigh_parms_release(&arp_tbl, in_dev->arp_parms);
  259. arp_ifdown(dev);
  260. call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
  261. }
  262. int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
  263. {
  264. rcu_read_lock();
  265. for_primary_ifa(in_dev) {
  266. if (inet_ifa_match(a, ifa)) {
  267. if (!b || inet_ifa_match(b, ifa)) {
  268. rcu_read_unlock();
  269. return 1;
  270. }
  271. }
  272. } endfor_ifa(in_dev);
  273. rcu_read_unlock();
  274. return 0;
  275. }
  276. static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
  277. int destroy, struct nlmsghdr *nlh, u32 pid)
  278. {
  279. struct in_ifaddr *promote = NULL;
  280. struct in_ifaddr *ifa, *ifa1 = *ifap;
  281. struct in_ifaddr *last_prim = in_dev->ifa_list;
  282. struct in_ifaddr *prev_prom = NULL;
  283. int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
  284. ASSERT_RTNL();
  285. /* 1. Deleting primary ifaddr forces deletion all secondaries
  286. * unless alias promotion is set
  287. **/
  288. if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
  289. struct in_ifaddr **ifap1 = &ifa1->ifa_next;
  290. while ((ifa = *ifap1) != NULL) {
  291. if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
  292. ifa1->ifa_scope <= ifa->ifa_scope)
  293. last_prim = ifa;
  294. if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
  295. ifa1->ifa_mask != ifa->ifa_mask ||
  296. !inet_ifa_match(ifa1->ifa_address, ifa)) {
  297. ifap1 = &ifa->ifa_next;
  298. prev_prom = ifa;
  299. continue;
  300. }
  301. if (!do_promote) {
  302. inet_hash_remove(ifa);
  303. *ifap1 = ifa->ifa_next;
  304. rtmsg_ifa(RTM_DELADDR, ifa, nlh, pid);
  305. blocking_notifier_call_chain(&inetaddr_chain,
  306. NETDEV_DOWN, ifa);
  307. inet_free_ifa(ifa);
  308. } else {
  309. promote = ifa;
  310. break;
  311. }
  312. }
  313. }
  314. /* On promotion all secondaries from subnet are changing
  315. * the primary IP, we must remove all their routes silently
  316. * and later to add them back with new prefsrc. Do this
  317. * while all addresses are on the device list.
  318. */
  319. for (ifa = promote; ifa; ifa = ifa->ifa_next) {
  320. if (ifa1->ifa_mask == ifa->ifa_mask &&
  321. inet_ifa_match(ifa1->ifa_address, ifa))
  322. fib_del_ifaddr(ifa, ifa1);
  323. }
  324. /* 2. Unlink it */
  325. *ifap = ifa1->ifa_next;
  326. inet_hash_remove(ifa1);
  327. /* 3. Announce address deletion */
  328. /* Send message first, then call notifier.
  329. At first sight, FIB update triggered by notifier
  330. will refer to already deleted ifaddr, that could confuse
  331. netlink listeners. It is not true: look, gated sees
  332. that route deleted and if it still thinks that ifaddr
  333. is valid, it will try to restore deleted routes... Grr.
  334. So that, this order is correct.
  335. */
  336. rtmsg_ifa(RTM_DELADDR, ifa1, nlh, pid);
  337. blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
  338. if (promote) {
  339. struct in_ifaddr *next_sec = promote->ifa_next;
  340. if (prev_prom) {
  341. prev_prom->ifa_next = promote->ifa_next;
  342. promote->ifa_next = last_prim->ifa_next;
  343. last_prim->ifa_next = promote;
  344. }
  345. promote->ifa_flags &= ~IFA_F_SECONDARY;
  346. rtmsg_ifa(RTM_NEWADDR, promote, nlh, pid);
  347. blocking_notifier_call_chain(&inetaddr_chain,
  348. NETDEV_UP, promote);
  349. for (ifa = next_sec; ifa; ifa = ifa->ifa_next) {
  350. if (ifa1->ifa_mask != ifa->ifa_mask ||
  351. !inet_ifa_match(ifa1->ifa_address, ifa))
  352. continue;
  353. fib_add_ifaddr(ifa);
  354. }
  355. }
  356. if (destroy)
  357. inet_free_ifa(ifa1);
  358. }
  359. static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap,
  360. int destroy)
  361. {
  362. __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
  363. }
  364. static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
  365. u32 pid)
  366. {
  367. struct in_device *in_dev = ifa->ifa_dev;
  368. struct in_ifaddr *ifa1, **ifap, **last_primary;
  369. ASSERT_RTNL();
  370. if (!ifa->ifa_local) {
  371. inet_free_ifa(ifa);
  372. return 0;
  373. }
  374. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  375. last_primary = &in_dev->ifa_list;
  376. for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL;
  377. ifap = &ifa1->ifa_next) {
  378. if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
  379. ifa->ifa_scope <= ifa1->ifa_scope)
  380. last_primary = &ifa1->ifa_next;
  381. if (ifa1->ifa_mask == ifa->ifa_mask &&
  382. inet_ifa_match(ifa1->ifa_address, ifa)) {
  383. if (ifa1->ifa_local == ifa->ifa_local) {
  384. inet_free_ifa(ifa);
  385. return -EEXIST;
  386. }
  387. if (ifa1->ifa_scope != ifa->ifa_scope) {
  388. inet_free_ifa(ifa);
  389. return -EINVAL;
  390. }
  391. ifa->ifa_flags |= IFA_F_SECONDARY;
  392. }
  393. }
  394. if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
  395. net_srandom(ifa->ifa_local);
  396. ifap = last_primary;
  397. }
  398. ifa->ifa_next = *ifap;
  399. *ifap = ifa;
  400. inet_hash_insert(dev_net(in_dev->dev), ifa);
  401. /* Send message first, then call notifier.
  402. Notifier will trigger FIB update, so that
  403. listeners of netlink will know about new ifaddr */
  404. rtmsg_ifa(RTM_NEWADDR, ifa, nlh, pid);
  405. blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
  406. return 0;
  407. }
  408. static int inet_insert_ifa(struct in_ifaddr *ifa)
  409. {
  410. return __inet_insert_ifa(ifa, NULL, 0);
  411. }
  412. static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
  413. {
  414. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  415. ASSERT_RTNL();
  416. if (!in_dev) {
  417. inet_free_ifa(ifa);
  418. return -ENOBUFS;
  419. }
  420. ipv4_devconf_setall(in_dev);
  421. if (ifa->ifa_dev != in_dev) {
  422. WARN_ON(ifa->ifa_dev);
  423. in_dev_hold(in_dev);
  424. ifa->ifa_dev = in_dev;
  425. }
  426. if (ipv4_is_loopback(ifa->ifa_local))
  427. ifa->ifa_scope = RT_SCOPE_HOST;
  428. return inet_insert_ifa(ifa);
  429. }
  430. /* Caller must hold RCU or RTNL :
  431. * We dont take a reference on found in_device
  432. */
  433. struct in_device *inetdev_by_index(struct net *net, int ifindex)
  434. {
  435. struct net_device *dev;
  436. struct in_device *in_dev = NULL;
  437. rcu_read_lock();
  438. dev = dev_get_by_index_rcu(net, ifindex);
  439. if (dev)
  440. in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  441. rcu_read_unlock();
  442. return in_dev;
  443. }
  444. EXPORT_SYMBOL(inetdev_by_index);
  445. /* Called only from RTNL semaphored context. No locks. */
  446. struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
  447. __be32 mask)
  448. {
  449. ASSERT_RTNL();
  450. for_primary_ifa(in_dev) {
  451. if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
  452. return ifa;
  453. } endfor_ifa(in_dev);
  454. return NULL;
  455. }
  456. static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  457. {
  458. struct net *net = sock_net(skb->sk);
  459. struct nlattr *tb[IFA_MAX+1];
  460. struct in_device *in_dev;
  461. struct ifaddrmsg *ifm;
  462. struct in_ifaddr *ifa, **ifap;
  463. int err = -EINVAL;
  464. ASSERT_RTNL();
  465. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
  466. if (err < 0)
  467. goto errout;
  468. ifm = nlmsg_data(nlh);
  469. in_dev = inetdev_by_index(net, ifm->ifa_index);
  470. if (in_dev == NULL) {
  471. err = -ENODEV;
  472. goto errout;
  473. }
  474. for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
  475. ifap = &ifa->ifa_next) {
  476. if (tb[IFA_LOCAL] &&
  477. ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL]))
  478. continue;
  479. if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
  480. continue;
  481. if (tb[IFA_ADDRESS] &&
  482. (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
  483. !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa)))
  484. continue;
  485. __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).pid);
  486. return 0;
  487. }
  488. err = -EADDRNOTAVAIL;
  489. errout:
  490. return err;
  491. }
  492. static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh)
  493. {
  494. struct nlattr *tb[IFA_MAX+1];
  495. struct in_ifaddr *ifa;
  496. struct ifaddrmsg *ifm;
  497. struct net_device *dev;
  498. struct in_device *in_dev;
  499. int err;
  500. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv4_policy);
  501. if (err < 0)
  502. goto errout;
  503. ifm = nlmsg_data(nlh);
  504. err = -EINVAL;
  505. if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL)
  506. goto errout;
  507. dev = __dev_get_by_index(net, ifm->ifa_index);
  508. err = -ENODEV;
  509. if (dev == NULL)
  510. goto errout;
  511. in_dev = __in_dev_get_rtnl(dev);
  512. err = -ENOBUFS;
  513. if (in_dev == NULL)
  514. goto errout;
  515. ifa = inet_alloc_ifa();
  516. if (ifa == NULL)
  517. /*
  518. * A potential indev allocation can be left alive, it stays
  519. * assigned to its device and is destroy with it.
  520. */
  521. goto errout;
  522. ipv4_devconf_setall(in_dev);
  523. in_dev_hold(in_dev);
  524. if (tb[IFA_ADDRESS] == NULL)
  525. tb[IFA_ADDRESS] = tb[IFA_LOCAL];
  526. INIT_HLIST_NODE(&ifa->hash);
  527. ifa->ifa_prefixlen = ifm->ifa_prefixlen;
  528. ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
  529. ifa->ifa_flags = ifm->ifa_flags;
  530. ifa->ifa_scope = ifm->ifa_scope;
  531. ifa->ifa_dev = in_dev;
  532. ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]);
  533. ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]);
  534. if (tb[IFA_BROADCAST])
  535. ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]);
  536. if (tb[IFA_LABEL])
  537. nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
  538. else
  539. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  540. return ifa;
  541. errout:
  542. return ERR_PTR(err);
  543. }
  544. static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  545. {
  546. struct net *net = sock_net(skb->sk);
  547. struct in_ifaddr *ifa;
  548. ASSERT_RTNL();
  549. ifa = rtm_to_ifaddr(net, nlh);
  550. if (IS_ERR(ifa))
  551. return PTR_ERR(ifa);
  552. return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).pid);
  553. }
  554. /*
  555. * Determine a default network mask, based on the IP address.
  556. */
  557. static inline int inet_abc_len(__be32 addr)
  558. {
  559. int rc = -1; /* Something else, probably a multicast. */
  560. if (ipv4_is_zeronet(addr))
  561. rc = 0;
  562. else {
  563. __u32 haddr = ntohl(addr);
  564. if (IN_CLASSA(haddr))
  565. rc = 8;
  566. else if (IN_CLASSB(haddr))
  567. rc = 16;
  568. else if (IN_CLASSC(haddr))
  569. rc = 24;
  570. }
  571. return rc;
  572. }
  573. int devinet_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  574. {
  575. struct ifreq ifr;
  576. struct sockaddr_in sin_orig;
  577. struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
  578. struct in_device *in_dev;
  579. struct in_ifaddr **ifap = NULL;
  580. struct in_ifaddr *ifa = NULL;
  581. struct net_device *dev;
  582. char *colon;
  583. int ret = -EFAULT;
  584. int tryaddrmatch = 0;
  585. /*
  586. * Fetch the caller's info block into kernel space
  587. */
  588. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  589. goto out;
  590. ifr.ifr_name[IFNAMSIZ - 1] = 0;
  591. /* save original address for comparison */
  592. memcpy(&sin_orig, sin, sizeof(*sin));
  593. colon = strchr(ifr.ifr_name, ':');
  594. if (colon)
  595. *colon = 0;
  596. dev_load(net, ifr.ifr_name);
  597. switch (cmd) {
  598. case SIOCGIFADDR: /* Get interface address */
  599. case SIOCGIFBRDADDR: /* Get the broadcast address */
  600. case SIOCGIFDSTADDR: /* Get the destination address */
  601. case SIOCGIFNETMASK: /* Get the netmask for the interface */
  602. /* Note that these ioctls will not sleep,
  603. so that we do not impose a lock.
  604. One day we will be forced to put shlock here (I mean SMP)
  605. */
  606. tryaddrmatch = (sin_orig.sin_family == AF_INET);
  607. memset(sin, 0, sizeof(*sin));
  608. sin->sin_family = AF_INET;
  609. break;
  610. case SIOCSIFFLAGS:
  611. ret = -EACCES;
  612. if (!capable(CAP_NET_ADMIN))
  613. goto out;
  614. break;
  615. case SIOCSIFADDR: /* Set interface address (and family) */
  616. case SIOCSIFBRDADDR: /* Set the broadcast address */
  617. case SIOCSIFDSTADDR: /* Set the destination address */
  618. case SIOCSIFNETMASK: /* Set the netmask for the interface */
  619. case SIOCKILLADDR: /* Nuke all sockets on this address */
  620. ret = -EACCES;
  621. if (!capable(CAP_NET_ADMIN))
  622. goto out;
  623. ret = -EINVAL;
  624. if (sin->sin_family != AF_INET)
  625. goto out;
  626. break;
  627. default:
  628. ret = -EINVAL;
  629. goto out;
  630. }
  631. rtnl_lock();
  632. ret = -ENODEV;
  633. dev = __dev_get_by_name(net, ifr.ifr_name);
  634. if (!dev)
  635. goto done;
  636. if (colon)
  637. *colon = ':';
  638. in_dev = __in_dev_get_rtnl(dev);
  639. if (in_dev) {
  640. if (tryaddrmatch) {
  641. /* Matthias Andree */
  642. /* compare label and address (4.4BSD style) */
  643. /* note: we only do this for a limited set of ioctls
  644. and only if the original address family was AF_INET.
  645. This is checked above. */
  646. for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
  647. ifap = &ifa->ifa_next) {
  648. if (!strcmp(ifr.ifr_name, ifa->ifa_label) &&
  649. sin_orig.sin_addr.s_addr ==
  650. ifa->ifa_local) {
  651. break; /* found */
  652. }
  653. }
  654. }
  655. /* we didn't get a match, maybe the application is
  656. 4.3BSD-style and passed in junk so we fall back to
  657. comparing just the label */
  658. if (!ifa) {
  659. for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
  660. ifap = &ifa->ifa_next)
  661. if (!strcmp(ifr.ifr_name, ifa->ifa_label))
  662. break;
  663. }
  664. }
  665. ret = -EADDRNOTAVAIL;
  666. if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS
  667. && cmd != SIOCKILLADDR)
  668. goto done;
  669. switch (cmd) {
  670. case SIOCGIFADDR: /* Get interface address */
  671. sin->sin_addr.s_addr = ifa->ifa_local;
  672. goto rarok;
  673. case SIOCGIFBRDADDR: /* Get the broadcast address */
  674. sin->sin_addr.s_addr = ifa->ifa_broadcast;
  675. goto rarok;
  676. case SIOCGIFDSTADDR: /* Get the destination address */
  677. sin->sin_addr.s_addr = ifa->ifa_address;
  678. goto rarok;
  679. case SIOCGIFNETMASK: /* Get the netmask for the interface */
  680. sin->sin_addr.s_addr = ifa->ifa_mask;
  681. goto rarok;
  682. case SIOCSIFFLAGS:
  683. if (colon) {
  684. ret = -EADDRNOTAVAIL;
  685. if (!ifa)
  686. break;
  687. ret = 0;
  688. if (!(ifr.ifr_flags & IFF_UP))
  689. inet_del_ifa(in_dev, ifap, 1);
  690. break;
  691. }
  692. ret = dev_change_flags(dev, ifr.ifr_flags);
  693. break;
  694. case SIOCSIFADDR: /* Set interface address (and family) */
  695. ret = -EINVAL;
  696. if (inet_abc_len(sin->sin_addr.s_addr) < 0)
  697. break;
  698. if (!ifa) {
  699. ret = -ENOBUFS;
  700. ifa = inet_alloc_ifa();
  701. INIT_HLIST_NODE(&ifa->hash);
  702. if (!ifa)
  703. break;
  704. if (colon)
  705. memcpy(ifa->ifa_label, ifr.ifr_name, IFNAMSIZ);
  706. else
  707. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  708. } else {
  709. ret = 0;
  710. if (ifa->ifa_local == sin->sin_addr.s_addr)
  711. break;
  712. inet_del_ifa(in_dev, ifap, 0);
  713. ifa->ifa_broadcast = 0;
  714. ifa->ifa_scope = 0;
  715. }
  716. ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
  717. if (!(dev->flags & IFF_POINTOPOINT)) {
  718. ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
  719. ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
  720. if ((dev->flags & IFF_BROADCAST) &&
  721. ifa->ifa_prefixlen < 31)
  722. ifa->ifa_broadcast = ifa->ifa_address |
  723. ~ifa->ifa_mask;
  724. } else {
  725. ifa->ifa_prefixlen = 32;
  726. ifa->ifa_mask = inet_make_mask(32);
  727. }
  728. ret = inet_set_ifa(dev, ifa);
  729. break;
  730. case SIOCSIFBRDADDR: /* Set the broadcast address */
  731. ret = 0;
  732. if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
  733. inet_del_ifa(in_dev, ifap, 0);
  734. ifa->ifa_broadcast = sin->sin_addr.s_addr;
  735. inet_insert_ifa(ifa);
  736. }
  737. break;
  738. case SIOCSIFDSTADDR: /* Set the destination address */
  739. ret = 0;
  740. if (ifa->ifa_address == sin->sin_addr.s_addr)
  741. break;
  742. ret = -EINVAL;
  743. if (inet_abc_len(sin->sin_addr.s_addr) < 0)
  744. break;
  745. ret = 0;
  746. inet_del_ifa(in_dev, ifap, 0);
  747. ifa->ifa_address = sin->sin_addr.s_addr;
  748. inet_insert_ifa(ifa);
  749. break;
  750. case SIOCSIFNETMASK: /* Set the netmask for the interface */
  751. /*
  752. * The mask we set must be legal.
  753. */
  754. ret = -EINVAL;
  755. if (bad_mask(sin->sin_addr.s_addr, 0))
  756. break;
  757. ret = 0;
  758. if (ifa->ifa_mask != sin->sin_addr.s_addr) {
  759. __be32 old_mask = ifa->ifa_mask;
  760. inet_del_ifa(in_dev, ifap, 0);
  761. ifa->ifa_mask = sin->sin_addr.s_addr;
  762. ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
  763. /* See if current broadcast address matches
  764. * with current netmask, then recalculate
  765. * the broadcast address. Otherwise it's a
  766. * funny address, so don't touch it since
  767. * the user seems to know what (s)he's doing...
  768. */
  769. if ((dev->flags & IFF_BROADCAST) &&
  770. (ifa->ifa_prefixlen < 31) &&
  771. (ifa->ifa_broadcast ==
  772. (ifa->ifa_local|~old_mask))) {
  773. ifa->ifa_broadcast = (ifa->ifa_local |
  774. ~sin->sin_addr.s_addr);
  775. }
  776. inet_insert_ifa(ifa);
  777. }
  778. break;
  779. case SIOCKILLADDR: /* Nuke all connections on this address */
  780. ret = tcp_nuke_addr(net, (struct sockaddr *) sin);
  781. break;
  782. }
  783. done:
  784. rtnl_unlock();
  785. out:
  786. return ret;
  787. rarok:
  788. rtnl_unlock();
  789. ret = copy_to_user(arg, &ifr, sizeof(struct ifreq)) ? -EFAULT : 0;
  790. goto out;
  791. }
  792. static int inet_gifconf(struct net_device *dev, char __user *buf, int len)
  793. {
  794. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  795. struct in_ifaddr *ifa;
  796. struct ifreq ifr;
  797. int done = 0;
  798. if (!in_dev)
  799. goto out;
  800. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  801. if (!buf) {
  802. done += sizeof(ifr);
  803. continue;
  804. }
  805. if (len < (int) sizeof(ifr))
  806. break;
  807. memset(&ifr, 0, sizeof(struct ifreq));
  808. if (ifa->ifa_label)
  809. strcpy(ifr.ifr_name, ifa->ifa_label);
  810. else
  811. strcpy(ifr.ifr_name, dev->name);
  812. (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
  813. (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
  814. ifa->ifa_local;
  815. if (copy_to_user(buf, &ifr, sizeof(struct ifreq))) {
  816. done = -EFAULT;
  817. break;
  818. }
  819. buf += sizeof(struct ifreq);
  820. len -= sizeof(struct ifreq);
  821. done += sizeof(struct ifreq);
  822. }
  823. out:
  824. return done;
  825. }
  826. __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
  827. {
  828. __be32 addr = 0;
  829. struct in_device *in_dev;
  830. struct net *net = dev_net(dev);
  831. rcu_read_lock();
  832. in_dev = __in_dev_get_rcu(dev);
  833. if (!in_dev)
  834. goto no_in_dev;
  835. for_primary_ifa(in_dev) {
  836. if (ifa->ifa_scope > scope)
  837. continue;
  838. if (!dst || inet_ifa_match(dst, ifa)) {
  839. addr = ifa->ifa_local;
  840. break;
  841. }
  842. if (!addr)
  843. addr = ifa->ifa_local;
  844. } endfor_ifa(in_dev);
  845. if (addr)
  846. goto out_unlock;
  847. no_in_dev:
  848. /* Not loopback addresses on loopback should be preferred
  849. in this case. It is importnat that lo is the first interface
  850. in dev_base list.
  851. */
  852. for_each_netdev_rcu(net, dev) {
  853. in_dev = __in_dev_get_rcu(dev);
  854. if (!in_dev)
  855. continue;
  856. for_primary_ifa(in_dev) {
  857. if (ifa->ifa_scope != RT_SCOPE_LINK &&
  858. ifa->ifa_scope <= scope) {
  859. addr = ifa->ifa_local;
  860. goto out_unlock;
  861. }
  862. } endfor_ifa(in_dev);
  863. }
  864. out_unlock:
  865. rcu_read_unlock();
  866. return addr;
  867. }
  868. EXPORT_SYMBOL(inet_select_addr);
  869. static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
  870. __be32 local, int scope)
  871. {
  872. int same = 0;
  873. __be32 addr = 0;
  874. for_ifa(in_dev) {
  875. if (!addr &&
  876. (local == ifa->ifa_local || !local) &&
  877. ifa->ifa_scope <= scope) {
  878. addr = ifa->ifa_local;
  879. if (same)
  880. break;
  881. }
  882. if (!same) {
  883. same = (!local || inet_ifa_match(local, ifa)) &&
  884. (!dst || inet_ifa_match(dst, ifa));
  885. if (same && addr) {
  886. if (local || !dst)
  887. break;
  888. /* Is the selected addr into dst subnet? */
  889. if (inet_ifa_match(addr, ifa))
  890. break;
  891. /* No, then can we use new local src? */
  892. if (ifa->ifa_scope <= scope) {
  893. addr = ifa->ifa_local;
  894. break;
  895. }
  896. /* search for large dst subnet for addr */
  897. same = 0;
  898. }
  899. }
  900. } endfor_ifa(in_dev);
  901. return same ? addr : 0;
  902. }
  903. /*
  904. * Confirm that local IP address exists using wildcards:
  905. * - in_dev: only on this interface, 0=any interface
  906. * - dst: only in the same subnet as dst, 0=any dst
  907. * - local: address, 0=autoselect the local address
  908. * - scope: maximum allowed scope value for the local address
  909. */
  910. __be32 inet_confirm_addr(struct in_device *in_dev,
  911. __be32 dst, __be32 local, int scope)
  912. {
  913. __be32 addr = 0;
  914. struct net_device *dev;
  915. struct net *net;
  916. if (scope != RT_SCOPE_LINK)
  917. return confirm_addr_indev(in_dev, dst, local, scope);
  918. net = dev_net(in_dev->dev);
  919. rcu_read_lock();
  920. for_each_netdev_rcu(net, dev) {
  921. in_dev = __in_dev_get_rcu(dev);
  922. if (in_dev) {
  923. addr = confirm_addr_indev(in_dev, dst, local, scope);
  924. if (addr)
  925. break;
  926. }
  927. }
  928. rcu_read_unlock();
  929. return addr;
  930. }
  931. /*
  932. * Device notifier
  933. */
  934. int register_inetaddr_notifier(struct notifier_block *nb)
  935. {
  936. return blocking_notifier_chain_register(&inetaddr_chain, nb);
  937. }
  938. EXPORT_SYMBOL(register_inetaddr_notifier);
  939. int unregister_inetaddr_notifier(struct notifier_block *nb)
  940. {
  941. return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
  942. }
  943. EXPORT_SYMBOL(unregister_inetaddr_notifier);
  944. /* Rename ifa_labels for a device name change. Make some effort to preserve
  945. * existing alias numbering and to create unique labels if possible.
  946. */
  947. static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
  948. {
  949. struct in_ifaddr *ifa;
  950. int named = 0;
  951. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  952. char old[IFNAMSIZ], *dot;
  953. memcpy(old, ifa->ifa_label, IFNAMSIZ);
  954. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  955. if (named++ == 0)
  956. goto skip;
  957. dot = strchr(old, ':');
  958. if (dot == NULL) {
  959. sprintf(old, ":%d", named);
  960. dot = old;
  961. }
  962. if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
  963. strcat(ifa->ifa_label, dot);
  964. else
  965. strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
  966. skip:
  967. rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
  968. }
  969. }
  970. static inline bool inetdev_valid_mtu(unsigned mtu)
  971. {
  972. return mtu >= 68;
  973. }
  974. static void inetdev_send_gratuitous_arp(struct net_device *dev,
  975. struct in_device *in_dev)
  976. {
  977. struct in_ifaddr *ifa;
  978. for (ifa = in_dev->ifa_list; ifa;
  979. ifa = ifa->ifa_next) {
  980. arp_send(ARPOP_REQUEST, ETH_P_ARP,
  981. ifa->ifa_local, dev,
  982. ifa->ifa_local, NULL,
  983. dev->dev_addr, NULL);
  984. }
  985. }
  986. /* Called only under RTNL semaphore */
  987. static int inetdev_event(struct notifier_block *this, unsigned long event,
  988. void *ptr)
  989. {
  990. struct net_device *dev = ptr;
  991. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  992. ASSERT_RTNL();
  993. if (!in_dev) {
  994. if (event == NETDEV_REGISTER) {
  995. in_dev = inetdev_init(dev);
  996. if (!in_dev)
  997. return notifier_from_errno(-ENOMEM);
  998. if (dev->flags & IFF_LOOPBACK) {
  999. IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
  1000. IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
  1001. }
  1002. } else if (event == NETDEV_CHANGEMTU) {
  1003. /* Re-enabling IP */
  1004. if (inetdev_valid_mtu(dev->mtu))
  1005. in_dev = inetdev_init(dev);
  1006. }
  1007. goto out;
  1008. }
  1009. switch (event) {
  1010. case NETDEV_REGISTER:
  1011. printk(KERN_DEBUG "inetdev_event: bug\n");
  1012. rcu_assign_pointer(dev->ip_ptr, NULL);
  1013. break;
  1014. case NETDEV_UP:
  1015. if (!inetdev_valid_mtu(dev->mtu))
  1016. break;
  1017. if (dev->flags & IFF_LOOPBACK) {
  1018. struct in_ifaddr *ifa = inet_alloc_ifa();
  1019. if (ifa) {
  1020. INIT_HLIST_NODE(&ifa->hash);
  1021. ifa->ifa_local =
  1022. ifa->ifa_address = htonl(INADDR_LOOPBACK);
  1023. ifa->ifa_prefixlen = 8;
  1024. ifa->ifa_mask = inet_make_mask(8);
  1025. in_dev_hold(in_dev);
  1026. ifa->ifa_dev = in_dev;
  1027. ifa->ifa_scope = RT_SCOPE_HOST;
  1028. memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
  1029. inet_insert_ifa(ifa);
  1030. }
  1031. }
  1032. ip_mc_up(in_dev);
  1033. /* fall through */
  1034. case NETDEV_CHANGEADDR:
  1035. if (!IN_DEV_ARP_NOTIFY(in_dev))
  1036. break;
  1037. /* fall through */
  1038. case NETDEV_NOTIFY_PEERS:
  1039. /* Send gratuitous ARP to notify of link change */
  1040. inetdev_send_gratuitous_arp(dev, in_dev);
  1041. break;
  1042. case NETDEV_DOWN:
  1043. ip_mc_down(in_dev);
  1044. break;
  1045. case NETDEV_PRE_TYPE_CHANGE:
  1046. ip_mc_unmap(in_dev);
  1047. break;
  1048. case NETDEV_POST_TYPE_CHANGE:
  1049. ip_mc_remap(in_dev);
  1050. break;
  1051. case NETDEV_CHANGEMTU:
  1052. if (inetdev_valid_mtu(dev->mtu))
  1053. break;
  1054. /* disable IP when MTU is not enough */
  1055. case NETDEV_UNREGISTER:
  1056. inetdev_destroy(in_dev);
  1057. break;
  1058. case NETDEV_CHANGENAME:
  1059. /* Do not notify about label change, this event is
  1060. * not interesting to applications using netlink.
  1061. */
  1062. inetdev_changename(dev, in_dev);
  1063. devinet_sysctl_unregister(in_dev);
  1064. devinet_sysctl_register(in_dev);
  1065. break;
  1066. }
  1067. out:
  1068. return NOTIFY_DONE;
  1069. }
  1070. static struct notifier_block ip_netdev_notifier = {
  1071. .notifier_call = inetdev_event,
  1072. };
  1073. static inline size_t inet_nlmsg_size(void)
  1074. {
  1075. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  1076. + nla_total_size(4) /* IFA_ADDRESS */
  1077. + nla_total_size(4) /* IFA_LOCAL */
  1078. + nla_total_size(4) /* IFA_BROADCAST */
  1079. + nla_total_size(IFNAMSIZ); /* IFA_LABEL */
  1080. }
  1081. static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
  1082. u32 pid, u32 seq, int event, unsigned int flags)
  1083. {
  1084. struct ifaddrmsg *ifm;
  1085. struct nlmsghdr *nlh;
  1086. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
  1087. if (nlh == NULL)
  1088. return -EMSGSIZE;
  1089. ifm = nlmsg_data(nlh);
  1090. ifm->ifa_family = AF_INET;
  1091. ifm->ifa_prefixlen = ifa->ifa_prefixlen;
  1092. ifm->ifa_flags = ifa->ifa_flags|IFA_F_PERMANENT;
  1093. ifm->ifa_scope = ifa->ifa_scope;
  1094. ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
  1095. if (ifa->ifa_address)
  1096. NLA_PUT_BE32(skb, IFA_ADDRESS, ifa->ifa_address);
  1097. if (ifa->ifa_local)
  1098. NLA_PUT_BE32(skb, IFA_LOCAL, ifa->ifa_local);
  1099. if (ifa->ifa_broadcast)
  1100. NLA_PUT_BE32(skb, IFA_BROADCAST, ifa->ifa_broadcast);
  1101. if (ifa->ifa_label[0])
  1102. NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
  1103. return nlmsg_end(skb, nlh);
  1104. nla_put_failure:
  1105. nlmsg_cancel(skb, nlh);
  1106. return -EMSGSIZE;
  1107. }
  1108. static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  1109. {
  1110. struct net *net = sock_net(skb->sk);
  1111. int h, s_h;
  1112. int idx, s_idx;
  1113. int ip_idx, s_ip_idx;
  1114. struct net_device *dev;
  1115. struct in_device *in_dev;
  1116. struct in_ifaddr *ifa;
  1117. struct hlist_head *head;
  1118. struct hlist_node *node;
  1119. s_h = cb->args[0];
  1120. s_idx = idx = cb->args[1];
  1121. s_ip_idx = ip_idx = cb->args[2];
  1122. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1123. idx = 0;
  1124. head = &net->dev_index_head[h];
  1125. rcu_read_lock();
  1126. hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
  1127. if (idx < s_idx)
  1128. goto cont;
  1129. if (h > s_h || idx > s_idx)
  1130. s_ip_idx = 0;
  1131. in_dev = __in_dev_get_rcu(dev);
  1132. if (!in_dev)
  1133. goto cont;
  1134. for (ifa = in_dev->ifa_list, ip_idx = 0; ifa;
  1135. ifa = ifa->ifa_next, ip_idx++) {
  1136. if (ip_idx < s_ip_idx)
  1137. continue;
  1138. if (inet_fill_ifaddr(skb, ifa,
  1139. NETLINK_CB(cb->skb).pid,
  1140. cb->nlh->nlmsg_seq,
  1141. RTM_NEWADDR, NLM_F_MULTI) <= 0) {
  1142. rcu_read_unlock();
  1143. goto done;
  1144. }
  1145. }
  1146. cont:
  1147. idx++;
  1148. }
  1149. rcu_read_unlock();
  1150. }
  1151. done:
  1152. cb->args[0] = h;
  1153. cb->args[1] = idx;
  1154. cb->args[2] = ip_idx;
  1155. return skb->len;
  1156. }
  1157. static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
  1158. u32 pid)
  1159. {
  1160. struct sk_buff *skb;
  1161. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1162. int err = -ENOBUFS;
  1163. struct net *net;
  1164. net = dev_net(ifa->ifa_dev->dev);
  1165. skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
  1166. if (skb == NULL)
  1167. goto errout;
  1168. err = inet_fill_ifaddr(skb, ifa, pid, seq, event, 0);
  1169. if (err < 0) {
  1170. /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
  1171. WARN_ON(err == -EMSGSIZE);
  1172. kfree_skb(skb);
  1173. goto errout;
  1174. }
  1175. rtnl_notify(skb, net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
  1176. return;
  1177. errout:
  1178. if (err < 0)
  1179. rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
  1180. }
  1181. static size_t inet_get_link_af_size(const struct net_device *dev)
  1182. {
  1183. struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  1184. if (!in_dev)
  1185. return 0;
  1186. return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
  1187. }
  1188. static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
  1189. {
  1190. struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
  1191. struct nlattr *nla;
  1192. int i;
  1193. if (!in_dev)
  1194. return -ENODATA;
  1195. nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
  1196. if (nla == NULL)
  1197. return -EMSGSIZE;
  1198. for (i = 0; i < IPV4_DEVCONF_MAX; i++)
  1199. ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
  1200. return 0;
  1201. }
  1202. static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
  1203. [IFLA_INET_CONF] = { .type = NLA_NESTED },
  1204. };
  1205. static int inet_validate_link_af(const struct net_device *dev,
  1206. const struct nlattr *nla)
  1207. {
  1208. struct nlattr *a, *tb[IFLA_INET_MAX+1];
  1209. int err, rem;
  1210. if (dev && !__in_dev_get_rtnl(dev))
  1211. return -EAFNOSUPPORT;
  1212. err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
  1213. if (err < 0)
  1214. return err;
  1215. if (tb[IFLA_INET_CONF]) {
  1216. nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
  1217. int cfgid = nla_type(a);
  1218. if (nla_len(a) < 4)
  1219. return -EINVAL;
  1220. if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
  1221. return -EINVAL;
  1222. }
  1223. }
  1224. return 0;
  1225. }
  1226. static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
  1227. {
  1228. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  1229. struct nlattr *a, *tb[IFLA_INET_MAX+1];
  1230. int rem;
  1231. if (!in_dev)
  1232. return -EAFNOSUPPORT;
  1233. if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
  1234. BUG();
  1235. if (tb[IFLA_INET_CONF]) {
  1236. nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
  1237. ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
  1238. }
  1239. return 0;
  1240. }
  1241. #ifdef CONFIG_SYSCTL
  1242. static void devinet_copy_dflt_conf(struct net *net, int i)
  1243. {
  1244. struct net_device *dev;
  1245. rcu_read_lock();
  1246. for_each_netdev_rcu(net, dev) {
  1247. struct in_device *in_dev;
  1248. in_dev = __in_dev_get_rcu(dev);
  1249. if (in_dev && !test_bit(i, in_dev->cnf.state))
  1250. in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
  1251. }
  1252. rcu_read_unlock();
  1253. }
  1254. /* called with RTNL locked */
  1255. static void inet_forward_change(struct net *net)
  1256. {
  1257. struct net_device *dev;
  1258. int on = IPV4_DEVCONF_ALL(net, FORWARDING);
  1259. IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
  1260. IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
  1261. for_each_netdev(net, dev) {
  1262. struct in_device *in_dev;
  1263. if (on)
  1264. dev_disable_lro(dev);
  1265. rcu_read_lock();
  1266. in_dev = __in_dev_get_rcu(dev);
  1267. if (in_dev)
  1268. IN_DEV_CONF_SET(in_dev, FORWARDING, on);
  1269. rcu_read_unlock();
  1270. }
  1271. }
  1272. static int devinet_conf_proc(ctl_table *ctl, int write,
  1273. void __user *buffer,
  1274. size_t *lenp, loff_t *ppos)
  1275. {
  1276. int old_value = *(int *)ctl->data;
  1277. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1278. int new_value = *(int *)ctl->data;
  1279. if (write) {
  1280. struct ipv4_devconf *cnf = ctl->extra1;
  1281. struct net *net = ctl->extra2;
  1282. int i = (int *)ctl->data - cnf->data;
  1283. set_bit(i, cnf->state);
  1284. if (cnf == net->ipv4.devconf_dflt)
  1285. devinet_copy_dflt_conf(net, i);
  1286. if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1)
  1287. if ((new_value == 0) && (old_value != 0))
  1288. rt_cache_flush(net, 0);
  1289. }
  1290. return ret;
  1291. }
  1292. static int devinet_sysctl_forward(ctl_table *ctl, int write,
  1293. void __user *buffer,
  1294. size_t *lenp, loff_t *ppos)
  1295. {
  1296. int *valp = ctl->data;
  1297. int val = *valp;
  1298. loff_t pos = *ppos;
  1299. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1300. if (write && *valp != val) {
  1301. struct net *net = ctl->extra2;
  1302. if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
  1303. if (!rtnl_trylock()) {
  1304. /* Restore the original values before restarting */
  1305. *valp = val;
  1306. *ppos = pos;
  1307. return restart_syscall();
  1308. }
  1309. if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
  1310. inet_forward_change(net);
  1311. } else if (*valp) {
  1312. struct ipv4_devconf *cnf = ctl->extra1;
  1313. struct in_device *idev =
  1314. container_of(cnf, struct in_device, cnf);
  1315. dev_disable_lro(idev->dev);
  1316. }
  1317. rtnl_unlock();
  1318. rt_cache_flush(net, 0);
  1319. }
  1320. }
  1321. return ret;
  1322. }
  1323. static int ipv4_doint_and_flush(ctl_table *ctl, int write,
  1324. void __user *buffer,
  1325. size_t *lenp, loff_t *ppos)
  1326. {
  1327. int *valp = ctl->data;
  1328. int val = *valp;
  1329. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1330. struct net *net = ctl->extra2;
  1331. if (write && *valp != val)
  1332. rt_cache_flush(net, 0);
  1333. return ret;
  1334. }
  1335. #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
  1336. { \
  1337. .procname = name, \
  1338. .data = ipv4_devconf.data + \
  1339. IPV4_DEVCONF_ ## attr - 1, \
  1340. .maxlen = sizeof(int), \
  1341. .mode = mval, \
  1342. .proc_handler = proc, \
  1343. .extra1 = &ipv4_devconf, \
  1344. }
  1345. #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
  1346. DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
  1347. #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
  1348. DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
  1349. #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
  1350. DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
  1351. #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
  1352. DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
  1353. static struct devinet_sysctl_table {
  1354. struct ctl_table_header *sysctl_header;
  1355. struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
  1356. char *dev_name;
  1357. } devinet_sysctl = {
  1358. .devinet_vars = {
  1359. DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
  1360. devinet_sysctl_forward),
  1361. DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
  1362. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
  1363. DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
  1364. DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
  1365. DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
  1366. DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
  1367. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
  1368. "accept_source_route"),
  1369. DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
  1370. DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
  1371. DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
  1372. DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
  1373. DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
  1374. DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
  1375. DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
  1376. DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
  1377. DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
  1378. DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
  1379. DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
  1380. DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
  1381. DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
  1382. DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
  1383. DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
  1384. DEVINET_SYSCTL_FLUSHING_ENTRY(FORCE_IGMP_VERSION,
  1385. "force_igmp_version"),
  1386. DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
  1387. "promote_secondaries"),
  1388. },
  1389. };
  1390. static int __devinet_sysctl_register(struct net *net, char *dev_name,
  1391. struct ipv4_devconf *p)
  1392. {
  1393. int i;
  1394. struct devinet_sysctl_table *t;
  1395. #define DEVINET_CTL_PATH_DEV 3
  1396. struct ctl_path devinet_ctl_path[] = {
  1397. { .procname = "net", },
  1398. { .procname = "ipv4", },
  1399. { .procname = "conf", },
  1400. { /* to be set */ },
  1401. { },
  1402. };
  1403. t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
  1404. if (!t)
  1405. goto out;
  1406. for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
  1407. t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
  1408. t->devinet_vars[i].extra1 = p;
  1409. t->devinet_vars[i].extra2 = net;
  1410. }
  1411. /*
  1412. * Make a copy of dev_name, because '.procname' is regarded as const
  1413. * by sysctl and we wouldn't want anyone to change it under our feet
  1414. * (see SIOCSIFNAME).
  1415. */
  1416. t->dev_name = kstrdup(dev_name, GFP_KERNEL);
  1417. if (!t->dev_name)
  1418. goto free;
  1419. devinet_ctl_path[DEVINET_CTL_PATH_DEV].procname = t->dev_name;
  1420. t->sysctl_header = register_net_sysctl_table(net, devinet_ctl_path,
  1421. t->devinet_vars);
  1422. if (!t->sysctl_header)
  1423. goto free_procname;
  1424. p->sysctl = t;
  1425. return 0;
  1426. free_procname:
  1427. kfree(t->dev_name);
  1428. free:
  1429. kfree(t);
  1430. out:
  1431. return -ENOBUFS;
  1432. }
  1433. static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
  1434. {
  1435. struct devinet_sysctl_table *t = cnf->sysctl;
  1436. if (t == NULL)
  1437. return;
  1438. cnf->sysctl = NULL;
  1439. unregister_net_sysctl_table(t->sysctl_header);
  1440. kfree(t->dev_name);
  1441. kfree(t);
  1442. }
  1443. static void devinet_sysctl_register(struct in_device *idev)
  1444. {
  1445. neigh_sysctl_register(idev->dev, idev->arp_parms, "ipv4", NULL);
  1446. __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
  1447. &idev->cnf);
  1448. }
  1449. static void devinet_sysctl_unregister(struct in_device *idev)
  1450. {
  1451. __devinet_sysctl_unregister(&idev->cnf);
  1452. neigh_sysctl_unregister(idev->arp_parms);
  1453. }
  1454. static struct ctl_table ctl_forward_entry[] = {
  1455. {
  1456. .procname = "ip_forward",
  1457. .data = &ipv4_devconf.data[
  1458. IPV4_DEVCONF_FORWARDING - 1],
  1459. .maxlen = sizeof(int),
  1460. .mode = 0644,
  1461. .proc_handler = devinet_sysctl_forward,
  1462. .extra1 = &ipv4_devconf,
  1463. .extra2 = &init_net,
  1464. },
  1465. { },
  1466. };
  1467. static __net_initdata struct ctl_path net_ipv4_path[] = {
  1468. { .procname = "net", },
  1469. { .procname = "ipv4", },
  1470. { },
  1471. };
  1472. #endif
  1473. static __net_init int devinet_init_net(struct net *net)
  1474. {
  1475. int err;
  1476. struct ipv4_devconf *all, *dflt;
  1477. #ifdef CONFIG_SYSCTL
  1478. struct ctl_table *tbl = ctl_forward_entry;
  1479. struct ctl_table_header *forw_hdr;
  1480. #endif
  1481. err = -ENOMEM;
  1482. all = &ipv4_devconf;
  1483. dflt = &ipv4_devconf_dflt;
  1484. if (!net_eq(net, &init_net)) {
  1485. all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
  1486. if (all == NULL)
  1487. goto err_alloc_all;
  1488. dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
  1489. if (dflt == NULL)
  1490. goto err_alloc_dflt;
  1491. #ifdef CONFIG_SYSCTL
  1492. tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
  1493. if (tbl == NULL)
  1494. goto err_alloc_ctl;
  1495. tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
  1496. tbl[0].extra1 = all;
  1497. tbl[0].extra2 = net;
  1498. #endif
  1499. }
  1500. #ifdef CONFIG_SYSCTL
  1501. err = __devinet_sysctl_register(net, "all", all);
  1502. if (err < 0)
  1503. goto err_reg_all;
  1504. err = __devinet_sysctl_register(net, "default", dflt);
  1505. if (err < 0)
  1506. goto err_reg_dflt;
  1507. err = -ENOMEM;
  1508. forw_hdr = register_net_sysctl_table(net, net_ipv4_path, tbl);
  1509. if (forw_hdr == NULL)
  1510. goto err_reg_ctl;
  1511. net->ipv4.forw_hdr = forw_hdr;
  1512. #endif
  1513. net->ipv4.devconf_all = all;
  1514. net->ipv4.devconf_dflt = dflt;
  1515. return 0;
  1516. #ifdef CONFIG_SYSCTL
  1517. err_reg_ctl:
  1518. __devinet_sysctl_unregister(dflt);
  1519. err_reg_dflt:
  1520. __devinet_sysctl_unregister(all);
  1521. err_reg_all:
  1522. if (tbl != ctl_forward_entry)
  1523. kfree(tbl);
  1524. err_alloc_ctl:
  1525. #endif
  1526. if (dflt != &ipv4_devconf_dflt)
  1527. kfree(dflt);
  1528. err_alloc_dflt:
  1529. if (all != &ipv4_devconf)
  1530. kfree(all);
  1531. err_alloc_all:
  1532. return err;
  1533. }
  1534. static __net_exit void devinet_exit_net(struct net *net)
  1535. {
  1536. #ifdef CONFIG_SYSCTL
  1537. struct ctl_table *tbl;
  1538. tbl = net->ipv4.forw_hdr->ctl_table_arg;
  1539. unregister_net_sysctl_table(net->ipv4.forw_hdr);
  1540. __devinet_sysctl_unregister(net->ipv4.devconf_dflt);
  1541. __devinet_sysctl_unregister(net->ipv4.devconf_all);
  1542. kfree(tbl);
  1543. #endif
  1544. kfree(net->ipv4.devconf_dflt);
  1545. kfree(net->ipv4.devconf_all);
  1546. }
  1547. static __net_initdata struct pernet_operations devinet_ops = {
  1548. .init = devinet_init_net,
  1549. .exit = devinet_exit_net,
  1550. };
  1551. static struct rtnl_af_ops inet_af_ops = {
  1552. .family = AF_INET,
  1553. .fill_link_af = inet_fill_link_af,
  1554. .get_link_af_size = inet_get_link_af_size,
  1555. .validate_link_af = inet_validate_link_af,
  1556. .set_link_af = inet_set_link_af,
  1557. };
  1558. void __init devinet_init(void)
  1559. {
  1560. int i;
  1561. for (i = 0; i < IN4_ADDR_HSIZE; i++)
  1562. INIT_HLIST_HEAD(&inet_addr_lst[i]);
  1563. register_pernet_subsys(&devinet_ops);
  1564. register_gifconf(PF_INET, inet_gifconf);
  1565. register_netdevice_notifier(&ip_netdev_notifier);
  1566. rtnl_af_register(&inet_af_ops);
  1567. rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
  1568. rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
  1569. rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
  1570. }