fib_frontend.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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, <[email protected]>
  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 & IPTOS_RT_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_nh_id) {
  710. if (cfg->fc_oif || cfg->fc_gw_family ||
  711. cfg->fc_encap || cfg->fc_mp) {
  712. NL_SET_ERR_MSG(extack,
  713. "Nexthop specification and nexthop id are mutually exclusive");
  714. return -EINVAL;
  715. }
  716. }
  717. if (has_gw && has_via) {
  718. NL_SET_ERR_MSG(extack,
  719. "Nexthop configuration can not contain both GATEWAY and VIA");
  720. return -EINVAL;
  721. }
  722. if (!cfg->fc_table)
  723. cfg->fc_table = RT_TABLE_MAIN;
  724. return 0;
  725. errout:
  726. return err;
  727. }
  728. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  729. struct netlink_ext_ack *extack)
  730. {
  731. struct net *net = sock_net(skb->sk);
  732. struct fib_config cfg;
  733. struct fib_table *tb;
  734. int err;
  735. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  736. if (err < 0)
  737. goto errout;
  738. if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
  739. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  740. err = -EINVAL;
  741. goto errout;
  742. }
  743. tb = fib_get_table(net, cfg.fc_table);
  744. if (!tb) {
  745. NL_SET_ERR_MSG(extack, "FIB table does not exist");
  746. err = -ESRCH;
  747. goto errout;
  748. }
  749. err = fib_table_delete(net, tb, &cfg, extack);
  750. errout:
  751. return err;
  752. }
  753. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  754. struct netlink_ext_ack *extack)
  755. {
  756. struct net *net = sock_net(skb->sk);
  757. struct fib_config cfg;
  758. struct fib_table *tb;
  759. int err;
  760. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  761. if (err < 0)
  762. goto errout;
  763. tb = fib_new_table(net, cfg.fc_table);
  764. if (!tb) {
  765. err = -ENOBUFS;
  766. goto errout;
  767. }
  768. err = fib_table_insert(net, tb, &cfg, extack);
  769. if (!err && cfg.fc_type == RTN_LOCAL)
  770. net->ipv4.fib_has_custom_local_routes = true;
  771. errout:
  772. return err;
  773. }
  774. int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  775. struct fib_dump_filter *filter,
  776. struct netlink_callback *cb)
  777. {
  778. struct netlink_ext_ack *extack = cb->extack;
  779. struct nlattr *tb[RTA_MAX + 1];
  780. struct rtmsg *rtm;
  781. int err, i;
  782. ASSERT_RTNL();
  783. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  784. NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
  785. return -EINVAL;
  786. }
  787. rtm = nlmsg_data(nlh);
  788. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  789. rtm->rtm_scope) {
  790. NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
  791. return -EINVAL;
  792. }
  793. if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
  794. NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
  795. return -EINVAL;
  796. }
  797. if (rtm->rtm_flags & RTM_F_CLONED)
  798. filter->dump_routes = false;
  799. else
  800. filter->dump_exceptions = false;
  801. filter->flags = rtm->rtm_flags;
  802. filter->protocol = rtm->rtm_protocol;
  803. filter->rt_type = rtm->rtm_type;
  804. filter->table_id = rtm->rtm_table;
  805. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  806. rtm_ipv4_policy, extack);
  807. if (err < 0)
  808. return err;
  809. for (i = 0; i <= RTA_MAX; ++i) {
  810. int ifindex;
  811. if (!tb[i])
  812. continue;
  813. switch (i) {
  814. case RTA_TABLE:
  815. filter->table_id = nla_get_u32(tb[i]);
  816. break;
  817. case RTA_OIF:
  818. ifindex = nla_get_u32(tb[i]);
  819. filter->dev = __dev_get_by_index(net, ifindex);
  820. if (!filter->dev)
  821. return -ENODEV;
  822. break;
  823. default:
  824. NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
  825. return -EINVAL;
  826. }
  827. }
  828. if (filter->flags || filter->protocol || filter->rt_type ||
  829. filter->table_id || filter->dev) {
  830. filter->filter_set = 1;
  831. cb->answer_flags = NLM_F_DUMP_FILTERED;
  832. }
  833. return 0;
  834. }
  835. EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
  836. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  837. {
  838. struct fib_dump_filter filter = { .dump_routes = true,
  839. .dump_exceptions = true };
  840. const struct nlmsghdr *nlh = cb->nlh;
  841. struct net *net = sock_net(skb->sk);
  842. unsigned int h, s_h;
  843. unsigned int e = 0, s_e;
  844. struct fib_table *tb;
  845. struct hlist_head *head;
  846. int dumped = 0, err;
  847. if (cb->strict_check) {
  848. err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
  849. if (err < 0)
  850. return err;
  851. } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
  852. struct rtmsg *rtm = nlmsg_data(nlh);
  853. filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
  854. }
  855. /* ipv4 does not use prefix flag */
  856. if (filter.flags & RTM_F_PREFIX)
  857. return skb->len;
  858. if (filter.table_id) {
  859. tb = fib_get_table(net, filter.table_id);
  860. if (!tb) {
  861. if (rtnl_msg_family(cb->nlh) != PF_INET)
  862. return skb->len;
  863. NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
  864. return -ENOENT;
  865. }
  866. rcu_read_lock();
  867. err = fib_table_dump(tb, skb, cb, &filter);
  868. rcu_read_unlock();
  869. return skb->len ? : err;
  870. }
  871. s_h = cb->args[0];
  872. s_e = cb->args[1];
  873. rcu_read_lock();
  874. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  875. e = 0;
  876. head = &net->ipv4.fib_table_hash[h];
  877. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  878. if (e < s_e)
  879. goto next;
  880. if (dumped)
  881. memset(&cb->args[2], 0, sizeof(cb->args) -
  882. 2 * sizeof(cb->args[0]));
  883. err = fib_table_dump(tb, skb, cb, &filter);
  884. if (err < 0) {
  885. if (likely(skb->len))
  886. goto out;
  887. goto out_err;
  888. }
  889. dumped = 1;
  890. next:
  891. e++;
  892. }
  893. }
  894. out:
  895. err = skb->len;
  896. out_err:
  897. rcu_read_unlock();
  898. cb->args[1] = e;
  899. cb->args[0] = h;
  900. return err;
  901. }
  902. /* Prepare and feed intra-kernel routing request.
  903. * Really, it should be netlink message, but :-( netlink
  904. * can be not configured, so that we feed it directly
  905. * to fib engine. It is legal, because all events occur
  906. * only when netlink is already locked.
  907. */
  908. static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
  909. struct in_ifaddr *ifa, u32 rt_priority)
  910. {
  911. struct net *net = dev_net(ifa->ifa_dev->dev);
  912. u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
  913. struct fib_table *tb;
  914. struct fib_config cfg = {
  915. .fc_protocol = RTPROT_KERNEL,
  916. .fc_type = type,
  917. .fc_dst = dst,
  918. .fc_dst_len = dst_len,
  919. .fc_priority = rt_priority,
  920. .fc_prefsrc = ifa->ifa_local,
  921. .fc_oif = ifa->ifa_dev->dev->ifindex,
  922. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  923. .fc_nlinfo = {
  924. .nl_net = net,
  925. },
  926. };
  927. if (!tb_id)
  928. tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
  929. tb = fib_new_table(net, tb_id);
  930. if (!tb)
  931. return;
  932. cfg.fc_table = tb->tb_id;
  933. if (type != RTN_LOCAL)
  934. cfg.fc_scope = RT_SCOPE_LINK;
  935. else
  936. cfg.fc_scope = RT_SCOPE_HOST;
  937. if (cmd == RTM_NEWROUTE)
  938. fib_table_insert(net, tb, &cfg, NULL);
  939. else
  940. fib_table_delete(net, tb, &cfg, NULL);
  941. }
  942. void fib_add_ifaddr(struct in_ifaddr *ifa)
  943. {
  944. struct in_device *in_dev = ifa->ifa_dev;
  945. struct net_device *dev = in_dev->dev;
  946. struct in_ifaddr *prim = ifa;
  947. __be32 mask = ifa->ifa_mask;
  948. __be32 addr = ifa->ifa_local;
  949. __be32 prefix = ifa->ifa_address & mask;
  950. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  951. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  952. if (!prim) {
  953. pr_warn("%s: bug: prim == NULL\n", __func__);
  954. return;
  955. }
  956. }
  957. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
  958. if (!(dev->flags & IFF_UP))
  959. return;
  960. /* Add broadcast address, if it is explicitly assigned. */
  961. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) {
  962. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
  963. prim, 0);
  964. arp_invalidate(dev, ifa->ifa_broadcast, false);
  965. }
  966. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  967. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  968. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  969. fib_magic(RTM_NEWROUTE,
  970. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  971. prefix, ifa->ifa_prefixlen, prim,
  972. ifa->ifa_rt_priority);
  973. /* Add the network broadcast address, when it makes sense */
  974. if (ifa->ifa_prefixlen < 31) {
  975. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  976. 32, prim, 0);
  977. arp_invalidate(dev, prefix | ~mask, false);
  978. }
  979. }
  980. }
  981. void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
  982. {
  983. __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
  984. struct in_device *in_dev = ifa->ifa_dev;
  985. struct net_device *dev = in_dev->dev;
  986. if (!(dev->flags & IFF_UP) ||
  987. ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
  988. ipv4_is_zeronet(prefix) ||
  989. (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
  990. return;
  991. /* add the new */
  992. fib_magic(RTM_NEWROUTE,
  993. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  994. prefix, ifa->ifa_prefixlen, ifa, new_metric);
  995. /* delete the old */
  996. fib_magic(RTM_DELROUTE,
  997. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  998. prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
  999. }
  1000. /* Delete primary or secondary address.
  1001. * Optionally, on secondary address promotion consider the addresses
  1002. * from subnet iprim as deleted, even if they are in device list.
  1003. * In this case the secondary ifa can be in device list.
  1004. */
  1005. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  1006. {
  1007. struct in_device *in_dev = ifa->ifa_dev;
  1008. struct net_device *dev = in_dev->dev;
  1009. struct in_ifaddr *ifa1;
  1010. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  1011. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  1012. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  1013. #define LOCAL_OK 1
  1014. #define BRD_OK 2
  1015. #define BRD0_OK 4
  1016. #define BRD1_OK 8
  1017. unsigned int ok = 0;
  1018. int subnet = 0; /* Primary network */
  1019. int gone = 1; /* Address is missing */
  1020. int same_prefsrc = 0; /* Another primary with same IP */
  1021. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  1022. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  1023. if (!prim) {
  1024. /* if the device has been deleted, we don't perform
  1025. * address promotion
  1026. */
  1027. if (!in_dev->dead)
  1028. pr_warn("%s: bug: prim == NULL\n", __func__);
  1029. return;
  1030. }
  1031. if (iprim && iprim != prim) {
  1032. pr_warn("%s: bug: iprim != prim\n", __func__);
  1033. return;
  1034. }
  1035. } else if (!ipv4_is_zeronet(any) &&
  1036. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  1037. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  1038. fib_magic(RTM_DELROUTE,
  1039. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  1040. any, ifa->ifa_prefixlen, prim, 0);
  1041. subnet = 1;
  1042. }
  1043. if (in_dev->dead)
  1044. goto no_promotions;
  1045. /* Deletion is more complicated than add.
  1046. * We should take care of not to delete too much :-)
  1047. *
  1048. * Scan address list to be sure that addresses are really gone.
  1049. */
  1050. rcu_read_lock();
  1051. in_dev_for_each_ifa_rcu(ifa1, in_dev) {
  1052. if (ifa1 == ifa) {
  1053. /* promotion, keep the IP */
  1054. gone = 0;
  1055. continue;
  1056. }
  1057. /* Ignore IFAs from our subnet */
  1058. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  1059. inet_ifa_match(ifa1->ifa_address, iprim))
  1060. continue;
  1061. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  1062. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  1063. /* Another address from our subnet? */
  1064. if (ifa1->ifa_mask == prim->ifa_mask &&
  1065. inet_ifa_match(ifa1->ifa_address, prim))
  1066. prim1 = prim;
  1067. else {
  1068. /* We reached the secondaries, so
  1069. * same_prefsrc should be determined.
  1070. */
  1071. if (!same_prefsrc)
  1072. continue;
  1073. /* Search new prim1 if ifa1 is not
  1074. * using the current prim1
  1075. */
  1076. if (!prim1 ||
  1077. ifa1->ifa_mask != prim1->ifa_mask ||
  1078. !inet_ifa_match(ifa1->ifa_address, prim1))
  1079. prim1 = inet_ifa_byprefix(in_dev,
  1080. ifa1->ifa_address,
  1081. ifa1->ifa_mask);
  1082. if (!prim1)
  1083. continue;
  1084. if (prim1->ifa_local != prim->ifa_local)
  1085. continue;
  1086. }
  1087. } else {
  1088. if (prim->ifa_local != ifa1->ifa_local)
  1089. continue;
  1090. prim1 = ifa1;
  1091. if (prim != prim1)
  1092. same_prefsrc = 1;
  1093. }
  1094. if (ifa->ifa_local == ifa1->ifa_local)
  1095. ok |= LOCAL_OK;
  1096. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  1097. ok |= BRD_OK;
  1098. if (brd == ifa1->ifa_broadcast)
  1099. ok |= BRD1_OK;
  1100. if (any == ifa1->ifa_broadcast)
  1101. ok |= BRD0_OK;
  1102. /* primary has network specific broadcasts */
  1103. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  1104. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  1105. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  1106. if (!ipv4_is_zeronet(any1)) {
  1107. if (ifa->ifa_broadcast == brd1 ||
  1108. ifa->ifa_broadcast == any1)
  1109. ok |= BRD_OK;
  1110. if (brd == brd1 || brd == any1)
  1111. ok |= BRD1_OK;
  1112. if (any == brd1 || any == any1)
  1113. ok |= BRD0_OK;
  1114. }
  1115. }
  1116. }
  1117. rcu_read_unlock();
  1118. no_promotions:
  1119. if (!(ok & BRD_OK))
  1120. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
  1121. prim, 0);
  1122. if (subnet && ifa->ifa_prefixlen < 31) {
  1123. if (!(ok & BRD1_OK))
  1124. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
  1125. prim, 0);
  1126. if (!(ok & BRD0_OK))
  1127. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
  1128. prim, 0);
  1129. }
  1130. if (!(ok & LOCAL_OK)) {
  1131. unsigned int addr_type;
  1132. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
  1133. /* Check, that this local address finally disappeared. */
  1134. addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
  1135. ifa->ifa_local);
  1136. if (gone && addr_type != RTN_LOCAL) {
  1137. /* And the last, but not the least thing.
  1138. * We must flush stray FIB entries.
  1139. *
  1140. * First of all, we scan fib_info list searching
  1141. * for stray nexthop entries, then ignite fib_flush.
  1142. */
  1143. if (fib_sync_down_addr(dev, ifa->ifa_local))
  1144. fib_flush(dev_net(dev));
  1145. }
  1146. }
  1147. #undef LOCAL_OK
  1148. #undef BRD_OK
  1149. #undef BRD0_OK
  1150. #undef BRD1_OK
  1151. }
  1152. static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
  1153. {
  1154. struct fib_result res;
  1155. struct flowi4 fl4 = {
  1156. .flowi4_mark = frn->fl_mark,
  1157. .daddr = frn->fl_addr,
  1158. .flowi4_tos = frn->fl_tos,
  1159. .flowi4_scope = frn->fl_scope,
  1160. };
  1161. struct fib_table *tb;
  1162. rcu_read_lock();
  1163. tb = fib_get_table(net, frn->tb_id_in);
  1164. frn->err = -ENOENT;
  1165. if (tb) {
  1166. local_bh_disable();
  1167. frn->tb_id = tb->tb_id;
  1168. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  1169. if (!frn->err) {
  1170. frn->prefixlen = res.prefixlen;
  1171. frn->nh_sel = res.nh_sel;
  1172. frn->type = res.type;
  1173. frn->scope = res.scope;
  1174. }
  1175. local_bh_enable();
  1176. }
  1177. rcu_read_unlock();
  1178. }
  1179. static void nl_fib_input(struct sk_buff *skb)
  1180. {
  1181. struct net *net;
  1182. struct fib_result_nl *frn;
  1183. struct nlmsghdr *nlh;
  1184. u32 portid;
  1185. net = sock_net(skb->sk);
  1186. nlh = nlmsg_hdr(skb);
  1187. if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
  1188. skb->len < nlh->nlmsg_len ||
  1189. nlmsg_len(nlh) < sizeof(*frn))
  1190. return;
  1191. skb = netlink_skb_clone(skb, GFP_KERNEL);
  1192. if (!skb)
  1193. return;
  1194. nlh = nlmsg_hdr(skb);
  1195. frn = nlmsg_data(nlh);
  1196. nl_fib_lookup(net, frn);
  1197. portid = NETLINK_CB(skb).portid; /* netlink portid */
  1198. NETLINK_CB(skb).portid = 0; /* from kernel */
  1199. NETLINK_CB(skb).dst_group = 0; /* unicast */
  1200. nlmsg_unicast(net->ipv4.fibnl, skb, portid);
  1201. }
  1202. static int __net_init nl_fib_lookup_init(struct net *net)
  1203. {
  1204. struct sock *sk;
  1205. struct netlink_kernel_cfg cfg = {
  1206. .input = nl_fib_input,
  1207. };
  1208. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
  1209. if (!sk)
  1210. return -EAFNOSUPPORT;
  1211. net->ipv4.fibnl = sk;
  1212. return 0;
  1213. }
  1214. static void nl_fib_lookup_exit(struct net *net)
  1215. {
  1216. netlink_kernel_release(net->ipv4.fibnl);
  1217. net->ipv4.fibnl = NULL;
  1218. }
  1219. static void fib_disable_ip(struct net_device *dev, unsigned long event,
  1220. bool force)
  1221. {
  1222. if (fib_sync_down_dev(dev, event, force))
  1223. fib_flush(dev_net(dev));
  1224. else
  1225. rt_cache_flush(dev_net(dev));
  1226. arp_ifdown(dev);
  1227. }
  1228. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  1229. {
  1230. struct in_ifaddr *ifa = ptr;
  1231. struct net_device *dev = ifa->ifa_dev->dev;
  1232. struct net *net = dev_net(dev);
  1233. switch (event) {
  1234. case NETDEV_UP:
  1235. fib_add_ifaddr(ifa);
  1236. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1237. fib_sync_up(dev, RTNH_F_DEAD);
  1238. #endif
  1239. atomic_inc(&net->ipv4.dev_addr_genid);
  1240. rt_cache_flush(dev_net(dev));
  1241. break;
  1242. case NETDEV_DOWN:
  1243. fib_del_ifaddr(ifa, NULL);
  1244. atomic_inc(&net->ipv4.dev_addr_genid);
  1245. if (!ifa->ifa_dev->ifa_list) {
  1246. /* Last address was deleted from this interface.
  1247. * Disable IP.
  1248. */
  1249. fib_disable_ip(dev, event, true);
  1250. } else {
  1251. rt_cache_flush(dev_net(dev));
  1252. }
  1253. break;
  1254. }
  1255. return NOTIFY_DONE;
  1256. }
  1257. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1258. {
  1259. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1260. struct netdev_notifier_changeupper_info *upper_info = ptr;
  1261. struct netdev_notifier_info_ext *info_ext = ptr;
  1262. struct in_device *in_dev;
  1263. struct net *net = dev_net(dev);
  1264. struct in_ifaddr *ifa;
  1265. unsigned int flags;
  1266. if (event == NETDEV_UNREGISTER) {
  1267. fib_disable_ip(dev, event, true);
  1268. rt_flush_dev(dev);
  1269. return NOTIFY_DONE;
  1270. }
  1271. in_dev = __in_dev_get_rtnl(dev);
  1272. if (!in_dev)
  1273. return NOTIFY_DONE;
  1274. switch (event) {
  1275. case NETDEV_UP:
  1276. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  1277. fib_add_ifaddr(ifa);
  1278. }
  1279. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1280. fib_sync_up(dev, RTNH_F_DEAD);
  1281. #endif
  1282. atomic_inc(&net->ipv4.dev_addr_genid);
  1283. rt_cache_flush(net);
  1284. break;
  1285. case NETDEV_DOWN:
  1286. fib_disable_ip(dev, event, false);
  1287. break;
  1288. case NETDEV_CHANGE:
  1289. flags = dev_get_flags(dev);
  1290. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1291. fib_sync_up(dev, RTNH_F_LINKDOWN);
  1292. else
  1293. fib_sync_down_dev(dev, event, false);
  1294. rt_cache_flush(net);
  1295. break;
  1296. case NETDEV_CHANGEMTU:
  1297. fib_sync_mtu(dev, info_ext->ext.mtu);
  1298. rt_cache_flush(net);
  1299. break;
  1300. case NETDEV_CHANGEUPPER:
  1301. upper_info = ptr;
  1302. /* flush all routes if dev is linked to or unlinked from
  1303. * an L3 master device (e.g., VRF)
  1304. */
  1305. if (upper_info->upper_dev &&
  1306. netif_is_l3_master(upper_info->upper_dev))
  1307. fib_disable_ip(dev, NETDEV_DOWN, true);
  1308. break;
  1309. }
  1310. return NOTIFY_DONE;
  1311. }
  1312. static struct notifier_block fib_inetaddr_notifier = {
  1313. .notifier_call = fib_inetaddr_event,
  1314. };
  1315. static struct notifier_block fib_netdev_notifier = {
  1316. .notifier_call = fib_netdev_event,
  1317. };
  1318. static int __net_init ip_fib_net_init(struct net *net)
  1319. {
  1320. int err;
  1321. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  1322. err = fib4_notifier_init(net);
  1323. if (err)
  1324. return err;
  1325. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1326. /* Default to 3-tuple */
  1327. net->ipv4.sysctl_fib_multipath_hash_fields =
  1328. FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
  1329. #endif
  1330. /* Avoid false sharing : Use at least a full cache line */
  1331. size = max_t(size_t, size, L1_CACHE_BYTES);
  1332. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1333. if (!net->ipv4.fib_table_hash) {
  1334. err = -ENOMEM;
  1335. goto err_table_hash_alloc;
  1336. }
  1337. err = fib4_rules_init(net);
  1338. if (err < 0)
  1339. goto err_rules_init;
  1340. return 0;
  1341. err_rules_init:
  1342. kfree(net->ipv4.fib_table_hash);
  1343. err_table_hash_alloc:
  1344. fib4_notifier_exit(net);
  1345. return err;
  1346. }
  1347. static void ip_fib_net_exit(struct net *net)
  1348. {
  1349. int i;
  1350. ASSERT_RTNL();
  1351. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1352. RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
  1353. RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
  1354. #endif
  1355. /* Destroy the tables in reverse order to guarantee that the
  1356. * local table, ID 255, is destroyed before the main table, ID
  1357. * 254. This is necessary as the local table may contain
  1358. * references to data contained in the main table.
  1359. */
  1360. for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
  1361. struct hlist_head *head = &net->ipv4.fib_table_hash[i];
  1362. struct hlist_node *tmp;
  1363. struct fib_table *tb;
  1364. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
  1365. hlist_del(&tb->tb_hlist);
  1366. fib_table_flush(net, tb, true);
  1367. fib_free_table(tb);
  1368. }
  1369. }
  1370. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1371. fib4_rules_exit(net);
  1372. #endif
  1373. kfree(net->ipv4.fib_table_hash);
  1374. fib4_notifier_exit(net);
  1375. }
  1376. static int __net_init fib_net_init(struct net *net)
  1377. {
  1378. int error;
  1379. #ifdef CONFIG_IP_ROUTE_CLASSID
  1380. atomic_set(&net->ipv4.fib_num_tclassid_users, 0);
  1381. #endif
  1382. error = ip_fib_net_init(net);
  1383. if (error < 0)
  1384. goto out;
  1385. error = nl_fib_lookup_init(net);
  1386. if (error < 0)
  1387. goto out_nlfl;
  1388. error = fib_proc_init(net);
  1389. if (error < 0)
  1390. goto out_proc;
  1391. out:
  1392. return error;
  1393. out_proc:
  1394. nl_fib_lookup_exit(net);
  1395. out_nlfl:
  1396. rtnl_lock();
  1397. ip_fib_net_exit(net);
  1398. rtnl_unlock();
  1399. goto out;
  1400. }
  1401. static void __net_exit fib_net_exit(struct net *net)
  1402. {
  1403. fib_proc_exit(net);
  1404. nl_fib_lookup_exit(net);
  1405. }
  1406. static void __net_exit fib_net_exit_batch(struct list_head *net_list)
  1407. {
  1408. struct net *net;
  1409. rtnl_lock();
  1410. list_for_each_entry(net, net_list, exit_list)
  1411. ip_fib_net_exit(net);
  1412. rtnl_unlock();
  1413. }
  1414. static struct pernet_operations fib_net_ops = {
  1415. .init = fib_net_init,
  1416. .exit = fib_net_exit,
  1417. .exit_batch = fib_net_exit_batch,
  1418. };
  1419. void __init ip_fib_init(void)
  1420. {
  1421. fib_trie_init();
  1422. register_pernet_subsys(&fib_net_ops);
  1423. register_netdevice_notifier(&fib_netdev_notifier);
  1424. register_inetaddr_notifier(&fib_inetaddr_notifier);
  1425. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
  1426. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
  1427. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
  1428. }