fib_frontend.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * IPv4 Forwarding Information Base: FIB frontend.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/bitops.h>
  14. #include <linux/capability.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/socket.h>
  20. #include <linux/sockios.h>
  21. #include <linux/errno.h>
  22. #include <linux/in.h>
  23. #include <linux/inet.h>
  24. #include <linux/inetdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_addr.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/cache.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/slab.h>
  33. #include <net/inet_dscp.h>
  34. #include <net/ip.h>
  35. #include <net/protocol.h>
  36. #include <net/route.h>
  37. #include <net/tcp.h>
  38. #include <net/sock.h>
  39. #include <net/arp.h>
  40. #include <net/ip_fib.h>
  41. #include <net/nexthop.h>
  42. #include <net/rtnetlink.h>
  43. #include <net/xfrm.h>
  44. #include <net/l3mdev.h>
  45. #include <net/lwtunnel.h>
  46. #include <trace/events/fib.h>
  47. #ifndef CONFIG_IP_MULTIPLE_TABLES
  48. static int __net_init fib4_rules_init(struct net *net)
  49. {
  50. struct fib_table *local_table, *main_table;
  51. main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
  52. if (!main_table)
  53. return -ENOMEM;
  54. local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
  55. if (!local_table)
  56. goto fail;
  57. hlist_add_head_rcu(&local_table->tb_hlist,
  58. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
  59. hlist_add_head_rcu(&main_table->tb_hlist,
  60. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
  61. return 0;
  62. fail:
  63. fib_free_table(main_table);
  64. return -ENOMEM;
  65. }
  66. #else
  67. struct fib_table *fib_new_table(struct net *net, u32 id)
  68. {
  69. struct fib_table *tb, *alias = NULL;
  70. unsigned int h;
  71. if (id == 0)
  72. id = RT_TABLE_MAIN;
  73. tb = fib_get_table(net, id);
  74. if (tb)
  75. return tb;
  76. if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
  77. alias = fib_new_table(net, RT_TABLE_MAIN);
  78. tb = fib_trie_table(id, alias);
  79. if (!tb)
  80. return NULL;
  81. switch (id) {
  82. case RT_TABLE_MAIN:
  83. rcu_assign_pointer(net->ipv4.fib_main, tb);
  84. break;
  85. case RT_TABLE_DEFAULT:
  86. rcu_assign_pointer(net->ipv4.fib_default, tb);
  87. break;
  88. default:
  89. break;
  90. }
  91. h = id & (FIB_TABLE_HASHSZ - 1);
  92. hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
  93. return tb;
  94. }
  95. EXPORT_SYMBOL_GPL(fib_new_table);
  96. /* caller must hold either rtnl or rcu read lock */
  97. struct fib_table *fib_get_table(struct net *net, u32 id)
  98. {
  99. struct fib_table *tb;
  100. struct hlist_head *head;
  101. unsigned int h;
  102. if (id == 0)
  103. id = RT_TABLE_MAIN;
  104. h = id & (FIB_TABLE_HASHSZ - 1);
  105. head = &net->ipv4.fib_table_hash[h];
  106. hlist_for_each_entry_rcu(tb, head, tb_hlist,
  107. lockdep_rtnl_is_held()) {
  108. if (tb->tb_id == id)
  109. return tb;
  110. }
  111. return NULL;
  112. }
  113. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  114. static void fib_replace_table(struct net *net, struct fib_table *old,
  115. struct fib_table *new)
  116. {
  117. #ifdef CONFIG_IP_MULTIPLE_TABLES
  118. switch (new->tb_id) {
  119. case RT_TABLE_MAIN:
  120. rcu_assign_pointer(net->ipv4.fib_main, new);
  121. break;
  122. case RT_TABLE_DEFAULT:
  123. rcu_assign_pointer(net->ipv4.fib_default, new);
  124. break;
  125. default:
  126. break;
  127. }
  128. #endif
  129. /* replace the old table in the hlist */
  130. hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
  131. }
  132. int fib_unmerge(struct net *net)
  133. {
  134. struct fib_table *old, *new, *main_table;
  135. /* attempt to fetch local table if it has been allocated */
  136. old = fib_get_table(net, RT_TABLE_LOCAL);
  137. if (!old)
  138. return 0;
  139. new = fib_trie_unmerge(old);
  140. if (!new)
  141. return -ENOMEM;
  142. /* table is already unmerged */
  143. if (new == old)
  144. return 0;
  145. /* replace merged table with clean table */
  146. fib_replace_table(net, old, new);
  147. fib_free_table(old);
  148. /* attempt to fetch main table if it has been allocated */
  149. main_table = fib_get_table(net, RT_TABLE_MAIN);
  150. if (!main_table)
  151. return 0;
  152. /* flush local entries from main table */
  153. fib_table_flush_external(main_table);
  154. return 0;
  155. }
  156. void fib_flush(struct net *net)
  157. {
  158. int flushed = 0;
  159. unsigned int h;
  160. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  161. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  162. struct hlist_node *tmp;
  163. struct fib_table *tb;
  164. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
  165. flushed += fib_table_flush(net, tb, false);
  166. }
  167. if (flushed)
  168. rt_cache_flush(net);
  169. }
  170. /*
  171. * Find address type as if only "dev" was present in the system. If
  172. * on_dev is NULL then all interfaces are taken into consideration.
  173. */
  174. static inline unsigned int __inet_dev_addr_type(struct net *net,
  175. const struct net_device *dev,
  176. __be32 addr, u32 tb_id)
  177. {
  178. struct flowi4 fl4 = { .daddr = addr };
  179. struct fib_result res;
  180. unsigned int ret = RTN_BROADCAST;
  181. struct fib_table *table;
  182. if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
  183. return RTN_BROADCAST;
  184. if (ipv4_is_multicast(addr))
  185. return RTN_MULTICAST;
  186. rcu_read_lock();
  187. table = fib_get_table(net, tb_id);
  188. if (table) {
  189. ret = RTN_UNICAST;
  190. if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
  191. struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0);
  192. if (!dev || dev == nhc->nhc_dev)
  193. ret = res.type;
  194. }
  195. }
  196. rcu_read_unlock();
  197. return ret;
  198. }
  199. unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
  200. {
  201. return __inet_dev_addr_type(net, NULL, addr, tb_id);
  202. }
  203. EXPORT_SYMBOL(inet_addr_type_table);
  204. unsigned int inet_addr_type(struct net *net, __be32 addr)
  205. {
  206. return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
  207. }
  208. EXPORT_SYMBOL(inet_addr_type);
  209. unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
  210. __be32 addr)
  211. {
  212. u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
  213. return __inet_dev_addr_type(net, dev, addr, rt_table);
  214. }
  215. EXPORT_SYMBOL(inet_dev_addr_type);
  216. /* inet_addr_type with dev == NULL but using the table from a dev
  217. * if one is associated
  218. */
  219. unsigned int inet_addr_type_dev_table(struct net *net,
  220. const struct net_device *dev,
  221. __be32 addr)
  222. {
  223. u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
  224. return __inet_dev_addr_type(net, NULL, addr, rt_table);
  225. }
  226. EXPORT_SYMBOL(inet_addr_type_dev_table);
  227. __be32 fib_compute_spec_dst(struct sk_buff *skb)
  228. {
  229. struct net_device *dev = skb->dev;
  230. struct in_device *in_dev;
  231. struct fib_result res;
  232. struct rtable *rt;
  233. struct net *net;
  234. int scope;
  235. rt = skb_rtable(skb);
  236. if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
  237. RTCF_LOCAL)
  238. return ip_hdr(skb)->daddr;
  239. in_dev = __in_dev_get_rcu(dev);
  240. net = dev_net(dev);
  241. scope = RT_SCOPE_UNIVERSE;
  242. if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
  243. bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev);
  244. struct flowi4 fl4 = {
  245. .flowi4_iif = LOOPBACK_IFINDEX,
  246. .flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev),
  247. .daddr = ip_hdr(skb)->saddr,
  248. .flowi4_tos = ip_hdr(skb)->tos & INET_DSCP_MASK,
  249. .flowi4_scope = scope,
  250. .flowi4_mark = vmark ? skb->mark : 0,
  251. };
  252. if (!fib_lookup(net, &fl4, &res, 0))
  253. return fib_result_prefsrc(net, &res);
  254. } else {
  255. scope = RT_SCOPE_LINK;
  256. }
  257. return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
  258. }
  259. bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev)
  260. {
  261. bool dev_match = false;
  262. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  263. if (unlikely(fi->nh)) {
  264. dev_match = nexthop_uses_dev(fi->nh, dev);
  265. } else {
  266. int ret;
  267. for (ret = 0; ret < fib_info_num_path(fi); ret++) {
  268. const struct fib_nh_common *nhc = fib_info_nhc(fi, ret);
  269. if (nhc_l3mdev_matches_dev(nhc, dev)) {
  270. dev_match = true;
  271. break;
  272. }
  273. }
  274. }
  275. #else
  276. if (fib_info_nhc(fi, 0)->nhc_dev == dev)
  277. dev_match = true;
  278. #endif
  279. return dev_match;
  280. }
  281. EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
  282. /* Given (packet source, input interface) and optional (dst, oif, tos):
  283. * - (main) check, that source is valid i.e. not broadcast or our local
  284. * address.
  285. * - figure out what "logical" interface this packet arrived
  286. * and calculate "specific destination" address.
  287. * - check, that packet arrived from expected physical interface.
  288. * called with rcu_read_lock()
  289. */
  290. static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  291. u8 tos, int oif, struct net_device *dev,
  292. int rpf, struct in_device *idev, u32 *itag)
  293. {
  294. struct net *net = dev_net(dev);
  295. struct flow_keys flkeys;
  296. int ret, no_addr;
  297. struct fib_result res;
  298. struct flowi4 fl4;
  299. bool dev_match;
  300. fl4.flowi4_oif = 0;
  301. fl4.flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev);
  302. fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
  303. fl4.daddr = src;
  304. fl4.saddr = dst;
  305. fl4.flowi4_tos = tos;
  306. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  307. fl4.flowi4_tun_key.tun_id = 0;
  308. fl4.flowi4_flags = 0;
  309. fl4.flowi4_uid = sock_net_uid(net, NULL);
  310. fl4.flowi4_multipath_hash = 0;
  311. no_addr = idev->ifa_list == NULL;
  312. fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
  313. if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
  314. fl4.flowi4_proto = 0;
  315. fl4.fl4_sport = 0;
  316. fl4.fl4_dport = 0;
  317. } else {
  318. swap(fl4.fl4_sport, fl4.fl4_dport);
  319. }
  320. if (fib_lookup(net, &fl4, &res, 0))
  321. goto last_resort;
  322. if (res.type != RTN_UNICAST &&
  323. (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
  324. goto e_inval;
  325. fib_combine_itag(itag, &res);
  326. dev_match = fib_info_nh_uses_dev(res.fi, dev);
  327. /* This is not common, loopback packets retain skb_dst so normally they
  328. * would not even hit this slow path.
  329. */
  330. dev_match = dev_match || (res.type == RTN_LOCAL &&
  331. dev == net->loopback_dev);
  332. if (dev_match) {
  333. ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
  334. return ret;
  335. }
  336. if (no_addr)
  337. goto last_resort;
  338. if (rpf == 1)
  339. goto e_rpf;
  340. fl4.flowi4_oif = dev->ifindex;
  341. ret = 0;
  342. if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
  343. if (res.type == RTN_UNICAST)
  344. ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
  345. }
  346. return ret;
  347. last_resort:
  348. if (rpf)
  349. goto e_rpf;
  350. *itag = 0;
  351. return 0;
  352. e_inval:
  353. return -EINVAL;
  354. e_rpf:
  355. return -EXDEV;
  356. }
  357. /* Ignore rp_filter for packets protected by IPsec. */
  358. int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  359. u8 tos, int oif, struct net_device *dev,
  360. struct in_device *idev, u32 *itag)
  361. {
  362. int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
  363. struct net *net = dev_net(dev);
  364. if (!r && !fib_num_tclassid_users(net) &&
  365. (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
  366. if (IN_DEV_ACCEPT_LOCAL(idev))
  367. goto ok;
  368. /* with custom local routes in place, checking local addresses
  369. * only will be too optimistic, with custom rules, checking
  370. * local addresses only can be too strict, e.g. due to vrf
  371. */
  372. if (net->ipv4.fib_has_custom_local_routes ||
  373. fib4_has_custom_rules(net))
  374. goto full_check;
  375. /* Within the same container, it is regarded as a martian source,
  376. * and the same host but different containers are not.
  377. */
  378. if (inet_lookup_ifaddr_rcu(net, src))
  379. return -EINVAL;
  380. ok:
  381. *itag = 0;
  382. return 0;
  383. }
  384. full_check:
  385. return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
  386. }
  387. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  388. {
  389. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  390. }
  391. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  392. {
  393. struct nlattr *nla;
  394. nla = (struct nlattr *) ((char *) mx + len);
  395. nla->nla_type = type;
  396. nla->nla_len = nla_attr_size(4);
  397. *(u32 *) nla_data(nla) = value;
  398. return len + nla_total_size(4);
  399. }
  400. static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
  401. struct fib_config *cfg)
  402. {
  403. __be32 addr;
  404. int plen;
  405. memset(cfg, 0, sizeof(*cfg));
  406. cfg->fc_nlinfo.nl_net = net;
  407. if (rt->rt_dst.sa_family != AF_INET)
  408. return -EAFNOSUPPORT;
  409. /*
  410. * Check mask for validity:
  411. * a) it must be contiguous.
  412. * b) destination must have all host bits clear.
  413. * c) if application forgot to set correct family (AF_INET),
  414. * reject request unless it is absolutely clear i.e.
  415. * both family and mask are zero.
  416. */
  417. plen = 32;
  418. addr = sk_extract_addr(&rt->rt_dst);
  419. if (!(rt->rt_flags & RTF_HOST)) {
  420. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  421. if (rt->rt_genmask.sa_family != AF_INET) {
  422. if (mask || rt->rt_genmask.sa_family)
  423. return -EAFNOSUPPORT;
  424. }
  425. if (bad_mask(mask, addr))
  426. return -EINVAL;
  427. plen = inet_mask_len(mask);
  428. }
  429. cfg->fc_dst_len = plen;
  430. cfg->fc_dst = addr;
  431. if (cmd != SIOCDELRT) {
  432. cfg->fc_nlflags = NLM_F_CREATE;
  433. cfg->fc_protocol = RTPROT_BOOT;
  434. }
  435. if (rt->rt_metric)
  436. cfg->fc_priority = rt->rt_metric - 1;
  437. if (rt->rt_flags & RTF_REJECT) {
  438. cfg->fc_scope = RT_SCOPE_HOST;
  439. cfg->fc_type = RTN_UNREACHABLE;
  440. return 0;
  441. }
  442. cfg->fc_scope = RT_SCOPE_NOWHERE;
  443. cfg->fc_type = RTN_UNICAST;
  444. if (rt->rt_dev) {
  445. char *colon;
  446. struct net_device *dev;
  447. char devname[IFNAMSIZ];
  448. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  449. return -EFAULT;
  450. devname[IFNAMSIZ-1] = 0;
  451. colon = strchr(devname, ':');
  452. if (colon)
  453. *colon = 0;
  454. dev = __dev_get_by_name(net, devname);
  455. if (!dev)
  456. return -ENODEV;
  457. cfg->fc_oif = dev->ifindex;
  458. cfg->fc_table = l3mdev_fib_table(dev);
  459. if (colon) {
  460. const struct in_ifaddr *ifa;
  461. struct in_device *in_dev;
  462. in_dev = __in_dev_get_rtnl(dev);
  463. if (!in_dev)
  464. return -ENODEV;
  465. *colon = ':';
  466. rcu_read_lock();
  467. in_dev_for_each_ifa_rcu(ifa, in_dev) {
  468. if (strcmp(ifa->ifa_label, devname) == 0)
  469. break;
  470. }
  471. rcu_read_unlock();
  472. if (!ifa)
  473. return -ENODEV;
  474. cfg->fc_prefsrc = ifa->ifa_local;
  475. }
  476. }
  477. addr = sk_extract_addr(&rt->rt_gateway);
  478. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  479. unsigned int addr_type;
  480. cfg->fc_gw4 = addr;
  481. cfg->fc_gw_family = AF_INET;
  482. addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
  483. if (rt->rt_flags & RTF_GATEWAY &&
  484. addr_type == RTN_UNICAST)
  485. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  486. }
  487. if (!cfg->fc_table)
  488. cfg->fc_table = RT_TABLE_MAIN;
  489. if (cmd == SIOCDELRT)
  490. return 0;
  491. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
  492. return -EINVAL;
  493. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  494. cfg->fc_scope = RT_SCOPE_LINK;
  495. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  496. struct nlattr *mx;
  497. int len = 0;
  498. mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
  499. if (!mx)
  500. return -ENOMEM;
  501. if (rt->rt_flags & RTF_MTU)
  502. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  503. if (rt->rt_flags & RTF_WINDOW)
  504. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  505. if (rt->rt_flags & RTF_IRTT)
  506. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  507. cfg->fc_mx = mx;
  508. cfg->fc_mx_len = len;
  509. }
  510. return 0;
  511. }
  512. /*
  513. * Handle IP routing ioctl calls.
  514. * These are used to manipulate the routing tables
  515. */
  516. int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
  517. {
  518. struct fib_config cfg;
  519. int err;
  520. switch (cmd) {
  521. case SIOCADDRT: /* Add a route */
  522. case SIOCDELRT: /* Delete a route */
  523. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  524. return -EPERM;
  525. rtnl_lock();
  526. err = rtentry_to_fib_config(net, cmd, rt, &cfg);
  527. if (err == 0) {
  528. struct fib_table *tb;
  529. if (cmd == SIOCDELRT) {
  530. tb = fib_get_table(net, cfg.fc_table);
  531. if (tb)
  532. err = fib_table_delete(net, tb, &cfg,
  533. NULL);
  534. else
  535. err = -ESRCH;
  536. } else {
  537. tb = fib_new_table(net, cfg.fc_table);
  538. if (tb)
  539. err = fib_table_insert(net, tb,
  540. &cfg, NULL);
  541. else
  542. err = -ENOBUFS;
  543. }
  544. /* allocated by rtentry_to_fib_config() */
  545. kfree(cfg.fc_mx);
  546. }
  547. rtnl_unlock();
  548. return err;
  549. }
  550. return -EINVAL;
  551. }
  552. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  553. [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
  554. [RTA_DST] = { .type = NLA_U32 },
  555. [RTA_SRC] = { .type = NLA_U32 },
  556. [RTA_IIF] = { .type = NLA_U32 },
  557. [RTA_OIF] = { .type = NLA_U32 },
  558. [RTA_GATEWAY] = { .type = NLA_U32 },
  559. [RTA_PRIORITY] = { .type = NLA_U32 },
  560. [RTA_PREFSRC] = { .type = NLA_U32 },
  561. [RTA_METRICS] = { .type = NLA_NESTED },
  562. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  563. [RTA_FLOW] = { .type = NLA_U32 },
  564. [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
  565. [RTA_ENCAP] = { .type = NLA_NESTED },
  566. [RTA_UID] = { .type = NLA_U32 },
  567. [RTA_MARK] = { .type = NLA_U32 },
  568. [RTA_TABLE] = { .type = NLA_U32 },
  569. [RTA_IP_PROTO] = { .type = NLA_U8 },
  570. [RTA_SPORT] = { .type = NLA_U16 },
  571. [RTA_DPORT] = { .type = NLA_U16 },
  572. [RTA_NH_ID] = { .type = NLA_U32 },
  573. };
  574. int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
  575. struct netlink_ext_ack *extack)
  576. {
  577. struct rtvia *via;
  578. int alen;
  579. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  580. NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
  581. return -EINVAL;
  582. }
  583. via = nla_data(nla);
  584. alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
  585. switch (via->rtvia_family) {
  586. case AF_INET:
  587. if (alen != sizeof(__be32)) {
  588. NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
  589. return -EINVAL;
  590. }
  591. cfg->fc_gw_family = AF_INET;
  592. cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
  593. break;
  594. case AF_INET6:
  595. #if IS_ENABLED(CONFIG_IPV6)
  596. if (alen != sizeof(struct in6_addr)) {
  597. NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
  598. return -EINVAL;
  599. }
  600. cfg->fc_gw_family = AF_INET6;
  601. cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
  602. #else
  603. NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
  604. return -EINVAL;
  605. #endif
  606. break;
  607. default:
  608. NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
  609. return -EINVAL;
  610. }
  611. return 0;
  612. }
  613. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  614. struct nlmsghdr *nlh, struct fib_config *cfg,
  615. struct netlink_ext_ack *extack)
  616. {
  617. bool has_gw = false, has_via = false;
  618. struct nlattr *attr;
  619. int err, remaining;
  620. struct rtmsg *rtm;
  621. err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
  622. rtm_ipv4_policy, extack);
  623. if (err < 0)
  624. goto errout;
  625. memset(cfg, 0, sizeof(*cfg));
  626. rtm = nlmsg_data(nlh);
  627. if (!inet_validate_dscp(rtm->rtm_tos)) {
  628. NL_SET_ERR_MSG(extack,
  629. "Invalid dsfield (tos): ECN bits must be 0");
  630. err = -EINVAL;
  631. goto errout;
  632. }
  633. cfg->fc_dscp = inet_dsfield_to_dscp(rtm->rtm_tos);
  634. cfg->fc_dst_len = rtm->rtm_dst_len;
  635. cfg->fc_table = rtm->rtm_table;
  636. cfg->fc_protocol = rtm->rtm_protocol;
  637. cfg->fc_scope = rtm->rtm_scope;
  638. cfg->fc_type = rtm->rtm_type;
  639. cfg->fc_flags = rtm->rtm_flags;
  640. cfg->fc_nlflags = nlh->nlmsg_flags;
  641. cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
  642. cfg->fc_nlinfo.nlh = nlh;
  643. cfg->fc_nlinfo.nl_net = net;
  644. if (cfg->fc_type > RTN_MAX) {
  645. NL_SET_ERR_MSG(extack, "Invalid route type");
  646. err = -EINVAL;
  647. goto errout;
  648. }
  649. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  650. switch (nla_type(attr)) {
  651. case RTA_DST:
  652. cfg->fc_dst = nla_get_be32(attr);
  653. break;
  654. case RTA_OIF:
  655. cfg->fc_oif = nla_get_u32(attr);
  656. break;
  657. case RTA_GATEWAY:
  658. has_gw = true;
  659. cfg->fc_gw4 = nla_get_be32(attr);
  660. if (cfg->fc_gw4)
  661. cfg->fc_gw_family = AF_INET;
  662. break;
  663. case RTA_VIA:
  664. has_via = true;
  665. err = fib_gw_from_via(cfg, attr, extack);
  666. if (err)
  667. goto errout;
  668. break;
  669. case RTA_PRIORITY:
  670. cfg->fc_priority = nla_get_u32(attr);
  671. break;
  672. case RTA_PREFSRC:
  673. cfg->fc_prefsrc = nla_get_be32(attr);
  674. break;
  675. case RTA_METRICS:
  676. cfg->fc_mx = nla_data(attr);
  677. cfg->fc_mx_len = nla_len(attr);
  678. break;
  679. case RTA_MULTIPATH:
  680. err = lwtunnel_valid_encap_type_attr(nla_data(attr),
  681. nla_len(attr),
  682. extack);
  683. if (err < 0)
  684. goto errout;
  685. cfg->fc_mp = nla_data(attr);
  686. cfg->fc_mp_len = nla_len(attr);
  687. break;
  688. case RTA_FLOW:
  689. cfg->fc_flow = nla_get_u32(attr);
  690. break;
  691. case RTA_TABLE:
  692. cfg->fc_table = nla_get_u32(attr);
  693. break;
  694. case RTA_ENCAP:
  695. cfg->fc_encap = attr;
  696. break;
  697. case RTA_ENCAP_TYPE:
  698. cfg->fc_encap_type = nla_get_u16(attr);
  699. err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
  700. extack);
  701. if (err < 0)
  702. goto errout;
  703. break;
  704. case RTA_NH_ID:
  705. cfg->fc_nh_id = nla_get_u32(attr);
  706. break;
  707. }
  708. }
  709. if (cfg->fc_dst_len > 32) {
  710. NL_SET_ERR_MSG(extack, "Invalid prefix length");
  711. err = -EINVAL;
  712. goto errout;
  713. }
  714. if (cfg->fc_dst_len < 32 && (ntohl(cfg->fc_dst) << cfg->fc_dst_len)) {
  715. NL_SET_ERR_MSG(extack, "Invalid prefix for given prefix length");
  716. err = -EINVAL;
  717. goto errout;
  718. }
  719. if (cfg->fc_nh_id) {
  720. if (cfg->fc_oif || cfg->fc_gw_family ||
  721. cfg->fc_encap || cfg->fc_mp) {
  722. NL_SET_ERR_MSG(extack,
  723. "Nexthop specification and nexthop id are mutually exclusive");
  724. err = -EINVAL;
  725. goto errout;
  726. }
  727. }
  728. if (has_gw && has_via) {
  729. NL_SET_ERR_MSG(extack,
  730. "Nexthop configuration can not contain both GATEWAY and VIA");
  731. err = -EINVAL;
  732. goto errout;
  733. }
  734. if (!cfg->fc_table)
  735. cfg->fc_table = RT_TABLE_MAIN;
  736. return 0;
  737. errout:
  738. return err;
  739. }
  740. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  741. struct netlink_ext_ack *extack)
  742. {
  743. struct net *net = sock_net(skb->sk);
  744. struct fib_config cfg;
  745. struct fib_table *tb;
  746. int err;
  747. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  748. if (err < 0)
  749. goto errout;
  750. if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
  751. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  752. err = -EINVAL;
  753. goto errout;
  754. }
  755. tb = fib_get_table(net, cfg.fc_table);
  756. if (!tb) {
  757. NL_SET_ERR_MSG(extack, "FIB table does not exist");
  758. err = -ESRCH;
  759. goto errout;
  760. }
  761. err = fib_table_delete(net, tb, &cfg, extack);
  762. errout:
  763. return err;
  764. }
  765. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  766. struct netlink_ext_ack *extack)
  767. {
  768. struct net *net = sock_net(skb->sk);
  769. struct fib_config cfg;
  770. struct fib_table *tb;
  771. int err;
  772. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  773. if (err < 0)
  774. goto errout;
  775. tb = fib_new_table(net, cfg.fc_table);
  776. if (!tb) {
  777. err = -ENOBUFS;
  778. goto errout;
  779. }
  780. err = fib_table_insert(net, tb, &cfg, extack);
  781. if (!err && cfg.fc_type == RTN_LOCAL)
  782. net->ipv4.fib_has_custom_local_routes = true;
  783. errout:
  784. return err;
  785. }
  786. int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  787. struct fib_dump_filter *filter,
  788. struct netlink_callback *cb)
  789. {
  790. struct netlink_ext_ack *extack = cb->extack;
  791. struct nlattr *tb[RTA_MAX + 1];
  792. struct rtmsg *rtm;
  793. int err, i;
  794. if (filter->rtnl_held)
  795. ASSERT_RTNL();
  796. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  797. NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
  798. return -EINVAL;
  799. }
  800. rtm = nlmsg_data(nlh);
  801. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  802. rtm->rtm_scope) {
  803. NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
  804. return -EINVAL;
  805. }
  806. if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
  807. NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
  808. return -EINVAL;
  809. }
  810. if (rtm->rtm_flags & RTM_F_CLONED)
  811. filter->dump_routes = false;
  812. else
  813. filter->dump_exceptions = false;
  814. filter->flags = rtm->rtm_flags;
  815. filter->protocol = rtm->rtm_protocol;
  816. filter->rt_type = rtm->rtm_type;
  817. filter->table_id = rtm->rtm_table;
  818. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  819. rtm_ipv4_policy, extack);
  820. if (err < 0)
  821. return err;
  822. for (i = 0; i <= RTA_MAX; ++i) {
  823. int ifindex;
  824. if (!tb[i])
  825. continue;
  826. switch (i) {
  827. case RTA_TABLE:
  828. filter->table_id = nla_get_u32(tb[i]);
  829. break;
  830. case RTA_OIF:
  831. ifindex = nla_get_u32(tb[i]);
  832. if (filter->rtnl_held)
  833. filter->dev = __dev_get_by_index(net, ifindex);
  834. else
  835. filter->dev = dev_get_by_index_rcu(net, ifindex);
  836. if (!filter->dev)
  837. return -ENODEV;
  838. break;
  839. default:
  840. NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
  841. return -EINVAL;
  842. }
  843. }
  844. if (filter->flags || filter->protocol || filter->rt_type ||
  845. filter->table_id || filter->dev) {
  846. filter->filter_set = 1;
  847. cb->answer_flags = NLM_F_DUMP_FILTERED;
  848. }
  849. return 0;
  850. }
  851. EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
  852. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  853. {
  854. struct fib_dump_filter filter = {
  855. .dump_routes = true,
  856. .dump_exceptions = true,
  857. .rtnl_held = false,
  858. };
  859. const struct nlmsghdr *nlh = cb->nlh;
  860. struct net *net = sock_net(skb->sk);
  861. unsigned int h, s_h;
  862. unsigned int e = 0, s_e;
  863. struct fib_table *tb;
  864. struct hlist_head *head;
  865. int dumped = 0, err = 0;
  866. rcu_read_lock();
  867. if (cb->strict_check) {
  868. err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
  869. if (err < 0)
  870. goto unlock;
  871. } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
  872. struct rtmsg *rtm = nlmsg_data(nlh);
  873. filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
  874. }
  875. /* ipv4 does not use prefix flag */
  876. if (filter.flags & RTM_F_PREFIX)
  877. goto unlock;
  878. if (filter.table_id) {
  879. tb = fib_get_table(net, filter.table_id);
  880. if (!tb) {
  881. if (rtnl_msg_family(cb->nlh) != PF_INET)
  882. goto unlock;
  883. NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
  884. err = -ENOENT;
  885. goto unlock;
  886. }
  887. err = fib_table_dump(tb, skb, cb, &filter);
  888. goto unlock;
  889. }
  890. s_h = cb->args[0];
  891. s_e = cb->args[1];
  892. err = 0;
  893. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  894. e = 0;
  895. head = &net->ipv4.fib_table_hash[h];
  896. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  897. if (e < s_e)
  898. goto next;
  899. if (dumped)
  900. memset(&cb->args[2], 0, sizeof(cb->args) -
  901. 2 * sizeof(cb->args[0]));
  902. err = fib_table_dump(tb, skb, cb, &filter);
  903. if (err < 0)
  904. goto out;
  905. dumped = 1;
  906. next:
  907. e++;
  908. }
  909. }
  910. out:
  911. cb->args[1] = e;
  912. cb->args[0] = h;
  913. unlock:
  914. rcu_read_unlock();
  915. return err;
  916. }
  917. /* Prepare and feed intra-kernel routing request.
  918. * Really, it should be netlink message, but :-( netlink
  919. * can be not configured, so that we feed it directly
  920. * to fib engine. It is legal, because all events occur
  921. * only when netlink is already locked.
  922. */
  923. static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
  924. struct in_ifaddr *ifa, u32 rt_priority)
  925. {
  926. struct net *net = dev_net(ifa->ifa_dev->dev);
  927. u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
  928. struct fib_table *tb;
  929. struct fib_config cfg = {
  930. .fc_protocol = RTPROT_KERNEL,
  931. .fc_type = type,
  932. .fc_dst = dst,
  933. .fc_dst_len = dst_len,
  934. .fc_priority = rt_priority,
  935. .fc_prefsrc = ifa->ifa_local,
  936. .fc_oif = ifa->ifa_dev->dev->ifindex,
  937. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  938. .fc_nlinfo = {
  939. .nl_net = net,
  940. },
  941. };
  942. if (!tb_id)
  943. tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
  944. tb = fib_new_table(net, tb_id);
  945. if (!tb)
  946. return;
  947. cfg.fc_table = tb->tb_id;
  948. if (type != RTN_LOCAL)
  949. cfg.fc_scope = RT_SCOPE_LINK;
  950. else
  951. cfg.fc_scope = RT_SCOPE_HOST;
  952. if (cmd == RTM_NEWROUTE)
  953. fib_table_insert(net, tb, &cfg, NULL);
  954. else
  955. fib_table_delete(net, tb, &cfg, NULL);
  956. }
  957. void fib_add_ifaddr(struct in_ifaddr *ifa)
  958. {
  959. struct in_device *in_dev = ifa->ifa_dev;
  960. struct net_device *dev = in_dev->dev;
  961. struct in_ifaddr *prim = ifa;
  962. __be32 mask = ifa->ifa_mask;
  963. __be32 addr = ifa->ifa_local;
  964. __be32 prefix = ifa->ifa_address & mask;
  965. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  966. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  967. if (!prim) {
  968. pr_warn("%s: bug: prim == NULL\n", __func__);
  969. return;
  970. }
  971. }
  972. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
  973. if (!(dev->flags & IFF_UP))
  974. return;
  975. /* Add broadcast address, if it is explicitly assigned. */
  976. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) {
  977. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
  978. prim, 0);
  979. arp_invalidate(dev, ifa->ifa_broadcast, false);
  980. }
  981. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  982. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  983. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  984. fib_magic(RTM_NEWROUTE,
  985. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  986. prefix, ifa->ifa_prefixlen, prim,
  987. ifa->ifa_rt_priority);
  988. /* Add the network broadcast address, when it makes sense */
  989. if (ifa->ifa_prefixlen < 31) {
  990. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  991. 32, prim, 0);
  992. arp_invalidate(dev, prefix | ~mask, false);
  993. }
  994. }
  995. }
  996. void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
  997. {
  998. __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
  999. struct in_device *in_dev = ifa->ifa_dev;
  1000. struct net_device *dev = in_dev->dev;
  1001. if (!(dev->flags & IFF_UP) ||
  1002. ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
  1003. ipv4_is_zeronet(prefix) ||
  1004. (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
  1005. return;
  1006. /* add the new */
  1007. fib_magic(RTM_NEWROUTE,
  1008. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  1009. prefix, ifa->ifa_prefixlen, ifa, new_metric);
  1010. /* delete the old */
  1011. fib_magic(RTM_DELROUTE,
  1012. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  1013. prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
  1014. }
  1015. /* Delete primary or secondary address.
  1016. * Optionally, on secondary address promotion consider the addresses
  1017. * from subnet iprim as deleted, even if they are in device list.
  1018. * In this case the secondary ifa can be in device list.
  1019. */
  1020. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  1021. {
  1022. struct in_device *in_dev = ifa->ifa_dev;
  1023. struct net_device *dev = in_dev->dev;
  1024. struct in_ifaddr *ifa1;
  1025. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  1026. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  1027. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  1028. #define LOCAL_OK 1
  1029. #define BRD_OK 2
  1030. #define BRD0_OK 4
  1031. #define BRD1_OK 8
  1032. unsigned int ok = 0;
  1033. int subnet = 0; /* Primary network */
  1034. int gone = 1; /* Address is missing */
  1035. int same_prefsrc = 0; /* Another primary with same IP */
  1036. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  1037. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  1038. if (!prim) {
  1039. /* if the device has been deleted, we don't perform
  1040. * address promotion
  1041. */
  1042. if (!in_dev->dead)
  1043. pr_warn("%s: bug: prim == NULL\n", __func__);
  1044. return;
  1045. }
  1046. if (iprim && iprim != prim) {
  1047. pr_warn("%s: bug: iprim != prim\n", __func__);
  1048. return;
  1049. }
  1050. } else if (!ipv4_is_zeronet(any) &&
  1051. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  1052. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  1053. fib_magic(RTM_DELROUTE,
  1054. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  1055. any, ifa->ifa_prefixlen, prim, 0);
  1056. subnet = 1;
  1057. }
  1058. if (in_dev->dead)
  1059. goto no_promotions;
  1060. /* Deletion is more complicated than add.
  1061. * We should take care of not to delete too much :-)
  1062. *
  1063. * Scan address list to be sure that addresses are really gone.
  1064. */
  1065. rcu_read_lock();
  1066. in_dev_for_each_ifa_rcu(ifa1, in_dev) {
  1067. if (ifa1 == ifa) {
  1068. /* promotion, keep the IP */
  1069. gone = 0;
  1070. continue;
  1071. }
  1072. /* Ignore IFAs from our subnet */
  1073. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  1074. inet_ifa_match(ifa1->ifa_address, iprim))
  1075. continue;
  1076. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  1077. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  1078. /* Another address from our subnet? */
  1079. if (ifa1->ifa_mask == prim->ifa_mask &&
  1080. inet_ifa_match(ifa1->ifa_address, prim))
  1081. prim1 = prim;
  1082. else {
  1083. /* We reached the secondaries, so
  1084. * same_prefsrc should be determined.
  1085. */
  1086. if (!same_prefsrc)
  1087. continue;
  1088. /* Search new prim1 if ifa1 is not
  1089. * using the current prim1
  1090. */
  1091. if (!prim1 ||
  1092. ifa1->ifa_mask != prim1->ifa_mask ||
  1093. !inet_ifa_match(ifa1->ifa_address, prim1))
  1094. prim1 = inet_ifa_byprefix(in_dev,
  1095. ifa1->ifa_address,
  1096. ifa1->ifa_mask);
  1097. if (!prim1)
  1098. continue;
  1099. if (prim1->ifa_local != prim->ifa_local)
  1100. continue;
  1101. }
  1102. } else {
  1103. if (prim->ifa_local != ifa1->ifa_local)
  1104. continue;
  1105. prim1 = ifa1;
  1106. if (prim != prim1)
  1107. same_prefsrc = 1;
  1108. }
  1109. if (ifa->ifa_local == ifa1->ifa_local)
  1110. ok |= LOCAL_OK;
  1111. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  1112. ok |= BRD_OK;
  1113. if (brd == ifa1->ifa_broadcast)
  1114. ok |= BRD1_OK;
  1115. if (any == ifa1->ifa_broadcast)
  1116. ok |= BRD0_OK;
  1117. /* primary has network specific broadcasts */
  1118. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  1119. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  1120. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  1121. if (!ipv4_is_zeronet(any1)) {
  1122. if (ifa->ifa_broadcast == brd1 ||
  1123. ifa->ifa_broadcast == any1)
  1124. ok |= BRD_OK;
  1125. if (brd == brd1 || brd == any1)
  1126. ok |= BRD1_OK;
  1127. if (any == brd1 || any == any1)
  1128. ok |= BRD0_OK;
  1129. }
  1130. }
  1131. }
  1132. rcu_read_unlock();
  1133. no_promotions:
  1134. if (!(ok & BRD_OK))
  1135. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
  1136. prim, 0);
  1137. if (subnet && ifa->ifa_prefixlen < 31) {
  1138. if (!(ok & BRD1_OK))
  1139. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
  1140. prim, 0);
  1141. if (!(ok & BRD0_OK))
  1142. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
  1143. prim, 0);
  1144. }
  1145. if (!(ok & LOCAL_OK)) {
  1146. unsigned int addr_type;
  1147. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
  1148. /* Check, that this local address finally disappeared. */
  1149. addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
  1150. ifa->ifa_local);
  1151. if (gone && addr_type != RTN_LOCAL) {
  1152. /* And the last, but not the least thing.
  1153. * We must flush stray FIB entries.
  1154. *
  1155. * First of all, we scan fib_info list searching
  1156. * for stray nexthop entries, then ignite fib_flush.
  1157. */
  1158. if (fib_sync_down_addr(dev, ifa->ifa_local))
  1159. fib_flush(dev_net(dev));
  1160. }
  1161. }
  1162. #undef LOCAL_OK
  1163. #undef BRD_OK
  1164. #undef BRD0_OK
  1165. #undef BRD1_OK
  1166. }
  1167. static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
  1168. {
  1169. struct fib_result res;
  1170. struct flowi4 fl4 = {
  1171. .flowi4_mark = frn->fl_mark,
  1172. .daddr = frn->fl_addr,
  1173. .flowi4_tos = frn->fl_tos & INET_DSCP_MASK,
  1174. .flowi4_scope = frn->fl_scope,
  1175. };
  1176. struct fib_table *tb;
  1177. rcu_read_lock();
  1178. tb = fib_get_table(net, frn->tb_id_in);
  1179. frn->err = -ENOENT;
  1180. if (tb) {
  1181. local_bh_disable();
  1182. frn->tb_id = tb->tb_id;
  1183. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  1184. if (!frn->err) {
  1185. frn->prefixlen = res.prefixlen;
  1186. frn->nh_sel = res.nh_sel;
  1187. frn->type = res.type;
  1188. frn->scope = res.scope;
  1189. }
  1190. local_bh_enable();
  1191. }
  1192. rcu_read_unlock();
  1193. }
  1194. static void nl_fib_input(struct sk_buff *skb)
  1195. {
  1196. struct net *net;
  1197. struct fib_result_nl *frn;
  1198. struct nlmsghdr *nlh;
  1199. u32 portid;
  1200. net = sock_net(skb->sk);
  1201. nlh = nlmsg_hdr(skb);
  1202. if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
  1203. skb->len < nlh->nlmsg_len ||
  1204. nlmsg_len(nlh) < sizeof(*frn))
  1205. return;
  1206. skb = netlink_skb_clone(skb, GFP_KERNEL);
  1207. if (!skb)
  1208. return;
  1209. nlh = nlmsg_hdr(skb);
  1210. frn = nlmsg_data(nlh);
  1211. nl_fib_lookup(net, frn);
  1212. portid = NETLINK_CB(skb).portid; /* netlink portid */
  1213. NETLINK_CB(skb).portid = 0; /* from kernel */
  1214. NETLINK_CB(skb).dst_group = 0; /* unicast */
  1215. nlmsg_unicast(net->ipv4.fibnl, skb, portid);
  1216. }
  1217. static int __net_init nl_fib_lookup_init(struct net *net)
  1218. {
  1219. struct sock *sk;
  1220. struct netlink_kernel_cfg cfg = {
  1221. .input = nl_fib_input,
  1222. };
  1223. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
  1224. if (!sk)
  1225. return -EAFNOSUPPORT;
  1226. net->ipv4.fibnl = sk;
  1227. return 0;
  1228. }
  1229. static void nl_fib_lookup_exit(struct net *net)
  1230. {
  1231. netlink_kernel_release(net->ipv4.fibnl);
  1232. net->ipv4.fibnl = NULL;
  1233. }
  1234. static void fib_disable_ip(struct net_device *dev, unsigned long event,
  1235. bool force)
  1236. {
  1237. if (fib_sync_down_dev(dev, event, force))
  1238. fib_flush(dev_net(dev));
  1239. else
  1240. rt_cache_flush(dev_net(dev));
  1241. arp_ifdown(dev);
  1242. }
  1243. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  1244. {
  1245. struct in_ifaddr *ifa = ptr;
  1246. struct net_device *dev = ifa->ifa_dev->dev;
  1247. struct net *net = dev_net(dev);
  1248. switch (event) {
  1249. case NETDEV_UP:
  1250. fib_add_ifaddr(ifa);
  1251. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1252. fib_sync_up(dev, RTNH_F_DEAD);
  1253. #endif
  1254. atomic_inc(&net->ipv4.dev_addr_genid);
  1255. rt_cache_flush(dev_net(dev));
  1256. break;
  1257. case NETDEV_DOWN:
  1258. fib_del_ifaddr(ifa, NULL);
  1259. atomic_inc(&net->ipv4.dev_addr_genid);
  1260. if (!ifa->ifa_dev->ifa_list) {
  1261. /* Last address was deleted from this interface.
  1262. * Disable IP.
  1263. */
  1264. fib_disable_ip(dev, event, true);
  1265. } else {
  1266. rt_cache_flush(dev_net(dev));
  1267. }
  1268. break;
  1269. }
  1270. return NOTIFY_DONE;
  1271. }
  1272. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1273. {
  1274. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1275. struct netdev_notifier_changeupper_info *upper_info = ptr;
  1276. struct netdev_notifier_info_ext *info_ext = ptr;
  1277. struct in_device *in_dev;
  1278. struct net *net = dev_net(dev);
  1279. struct in_ifaddr *ifa;
  1280. unsigned int flags;
  1281. if (event == NETDEV_UNREGISTER) {
  1282. fib_disable_ip(dev, event, true);
  1283. rt_flush_dev(dev);
  1284. return NOTIFY_DONE;
  1285. }
  1286. in_dev = __in_dev_get_rtnl(dev);
  1287. if (!in_dev)
  1288. return NOTIFY_DONE;
  1289. switch (event) {
  1290. case NETDEV_UP:
  1291. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1292. fib_add_ifaddr(ifa);
  1293. }
  1294. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1295. fib_sync_up(dev, RTNH_F_DEAD);
  1296. #endif
  1297. atomic_inc(&net->ipv4.dev_addr_genid);
  1298. rt_cache_flush(net);
  1299. break;
  1300. case NETDEV_DOWN:
  1301. fib_disable_ip(dev, event, false);
  1302. break;
  1303. case NETDEV_CHANGE:
  1304. flags = dev_get_flags(dev);
  1305. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1306. fib_sync_up(dev, RTNH_F_LINKDOWN);
  1307. else
  1308. fib_sync_down_dev(dev, event, false);
  1309. rt_cache_flush(net);
  1310. break;
  1311. case NETDEV_CHANGEMTU:
  1312. fib_sync_mtu(dev, info_ext->ext.mtu);
  1313. rt_cache_flush(net);
  1314. break;
  1315. case NETDEV_CHANGEUPPER:
  1316. upper_info = ptr;
  1317. /* flush all routes if dev is linked to or unlinked from
  1318. * an L3 master device (e.g., VRF)
  1319. */
  1320. if (upper_info->upper_dev &&
  1321. netif_is_l3_master(upper_info->upper_dev))
  1322. fib_disable_ip(dev, NETDEV_DOWN, true);
  1323. break;
  1324. }
  1325. return NOTIFY_DONE;
  1326. }
  1327. static struct notifier_block fib_inetaddr_notifier = {
  1328. .notifier_call = fib_inetaddr_event,
  1329. };
  1330. static struct notifier_block fib_netdev_notifier = {
  1331. .notifier_call = fib_netdev_event,
  1332. };
  1333. static int __net_init ip_fib_net_init(struct net *net)
  1334. {
  1335. int err;
  1336. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  1337. err = fib4_notifier_init(net);
  1338. if (err)
  1339. return err;
  1340. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1341. /* Default to 3-tuple */
  1342. net->ipv4.sysctl_fib_multipath_hash_fields =
  1343. FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
  1344. #endif
  1345. /* Avoid false sharing : Use at least a full cache line */
  1346. size = max_t(size_t, size, L1_CACHE_BYTES);
  1347. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1348. if (!net->ipv4.fib_table_hash) {
  1349. err = -ENOMEM;
  1350. goto err_table_hash_alloc;
  1351. }
  1352. err = fib4_rules_init(net);
  1353. if (err < 0)
  1354. goto err_rules_init;
  1355. return 0;
  1356. err_rules_init:
  1357. kfree(net->ipv4.fib_table_hash);
  1358. err_table_hash_alloc:
  1359. fib4_notifier_exit(net);
  1360. return err;
  1361. }
  1362. static void ip_fib_net_exit(struct net *net)
  1363. {
  1364. int i;
  1365. ASSERT_RTNL();
  1366. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1367. RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
  1368. RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
  1369. #endif
  1370. /* Destroy the tables in reverse order to guarantee that the
  1371. * local table, ID 255, is destroyed before the main table, ID
  1372. * 254. This is necessary as the local table may contain
  1373. * references to data contained in the main table.
  1374. */
  1375. for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
  1376. struct hlist_head *head = &net->ipv4.fib_table_hash[i];
  1377. struct hlist_node *tmp;
  1378. struct fib_table *tb;
  1379. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
  1380. hlist_del(&tb->tb_hlist);
  1381. fib_table_flush(net, tb, true);
  1382. fib_free_table(tb);
  1383. }
  1384. }
  1385. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1386. fib4_rules_exit(net);
  1387. #endif
  1388. kfree(net->ipv4.fib_table_hash);
  1389. fib4_notifier_exit(net);
  1390. }
  1391. static int __net_init fib_net_init(struct net *net)
  1392. {
  1393. int error;
  1394. #ifdef CONFIG_IP_ROUTE_CLASSID
  1395. atomic_set(&net->ipv4.fib_num_tclassid_users, 0);
  1396. #endif
  1397. error = ip_fib_net_init(net);
  1398. if (error < 0)
  1399. goto out;
  1400. error = nl_fib_lookup_init(net);
  1401. if (error < 0)
  1402. goto out_nlfl;
  1403. error = fib_proc_init(net);
  1404. if (error < 0)
  1405. goto out_proc;
  1406. out:
  1407. return error;
  1408. out_proc:
  1409. nl_fib_lookup_exit(net);
  1410. out_nlfl:
  1411. rtnl_lock();
  1412. ip_fib_net_exit(net);
  1413. rtnl_unlock();
  1414. goto out;
  1415. }
  1416. static void __net_exit fib_net_exit(struct net *net)
  1417. {
  1418. fib_proc_exit(net);
  1419. nl_fib_lookup_exit(net);
  1420. }
  1421. static void __net_exit fib_net_exit_batch(struct list_head *net_list)
  1422. {
  1423. struct net *net;
  1424. rtnl_lock();
  1425. list_for_each_entry(net, net_list, exit_list)
  1426. ip_fib_net_exit(net);
  1427. rtnl_unlock();
  1428. }
  1429. static struct pernet_operations fib_net_ops = {
  1430. .init = fib_net_init,
  1431. .exit = fib_net_exit,
  1432. .exit_batch = fib_net_exit_batch,
  1433. };
  1434. void __init ip_fib_init(void)
  1435. {
  1436. fib_trie_init();
  1437. register_pernet_subsys(&fib_net_ops);
  1438. register_netdevice_notifier(&fib_netdev_notifier);
  1439. register_inetaddr_notifier(&fib_inetaddr_notifier);
  1440. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
  1441. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
  1442. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib,
  1443. RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE);
  1444. }