fib_frontend.c 34 KB

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