fib_frontend.c 39 KB

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