fib_frontend.c 41 KB

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