blob: a378eff827c7484937084c4e4cbbe3f96e2a67d5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 * Definitions for the Forwarding Information Base.
8 *
9 * Authors: A.N.Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#ifndef _NET_IP_FIB_H
13#define _NET_IP_FIB_H
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/flow.h>
16#include <linux/seq_file.h>
David S. Miller4895c772012-07-17 04:19:00 -070017#include <linux/rcupdate.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020018#include <net/fib_notifier.h>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070019#include <net/fib_rules.h>
Guillaume Naultf55fbb62022-02-04 14:58:16 +010020#include <net/inet_dscp.h>
David S. Miller8e773272012-06-11 00:01:52 -070021#include <net/inetpeer.h>
Eric Dumazetd26b3a72012-07-31 05:45:30 +000022#include <linux/percpu.h>
Jiri Pirkob90eb752016-09-26 12:52:29 +020023#include <linux/notifier.h>
Reshetova, Elena0029c0d2017-07-04 09:35:02 +030024#include <linux/refcount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Thomas Graf4e902c52006-08-17 18:14:52 -070026struct fib_config {
Thomas Graf4e902c52006-08-17 18:14:52 -070027 u8 fc_dst_len;
Guillaume Naultf55fbb62022-02-04 14:58:16 +010028 dscp_t fc_dscp;
Thomas Graf4e902c52006-08-17 18:14:52 -070029 u8 fc_protocol;
30 u8 fc_scope;
31 u8 fc_type;
David Ahernf35b7942019-04-05 16:30:28 -070032 u8 fc_gw_family;
33 /* 2 bytes unused */
Thomas Graf4e902c52006-08-17 18:14:52 -070034 u32 fc_table;
Al Viro6d85c102006-09-26 22:15:46 -070035 __be32 fc_dst;
David Aherna4ea5d42019-04-05 16:30:30 -070036 union {
37 __be32 fc_gw4;
38 struct in6_addr fc_gw6;
39 };
Thomas Graf4e902c52006-08-17 18:14:52 -070040 int fc_oif;
41 u32 fc_flags;
42 u32 fc_priority;
Al Viro6d85c102006-09-26 22:15:46 -070043 __be32 fc_prefsrc;
David Ahern493ced12019-06-08 14:53:32 -070044 u32 fc_nh_id;
Thomas Graf4e902c52006-08-17 18:14:52 -070045 struct nlattr *fc_mx;
46 struct rtnexthop *fc_mp;
47 int fc_mx_len;
48 int fc_mp_len;
49 u32 fc_flow;
Thomas Graf4e902c52006-08-17 18:14:52 -070050 u32 fc_nlflags;
51 struct nl_info fc_nlinfo;
Roopa Prabhu571e7222015-07-21 10:43:47 +020052 struct nlattr *fc_encap;
53 u16 fc_encap_type;
54};
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56struct fib_info;
David S. Millerf2bb4be2012-07-17 12:20:47 -070057struct rtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David S. Miller4895c772012-07-17 04:19:00 -070059struct fib_nh_exception {
60 struct fib_nh_exception __rcu *fnhe_next;
Timo Teräs5aad1de2013-05-27 20:46:33 +000061 int fnhe_genid;
David S. Miller4895c772012-07-17 04:19:00 -070062 __be32 fnhe_daddr;
63 u32 fnhe_pmtu;
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +010064 bool fnhe_mtu_locked;
Julian Anastasovaee06da2012-07-18 10:15:35 +000065 __be32 fnhe_gw;
David S. Miller4895c772012-07-17 04:19:00 -070066 unsigned long fnhe_expires;
Timo Teräs2ffae992013-06-27 10:27:05 +030067 struct rtable __rcu *fnhe_rth_input;
68 struct rtable __rcu *fnhe_rth_output;
David S. Miller4895c772012-07-17 04:19:00 -070069 unsigned long fnhe_stamp;
Xin Longdeed49d2016-02-18 21:21:19 +080070 struct rcu_head rcu;
David S. Miller4895c772012-07-17 04:19:00 -070071};
72
73struct fnhe_hash_bucket {
74 struct fib_nh_exception __rcu *chain;
75};
76
Eric Dumazetd546c622014-09-04 08:21:31 -070077#define FNHE_HASH_SHIFT 11
78#define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
David S. Miller4895c772012-07-17 04:19:00 -070079#define FNHE_RECLAIM_DEPTH 5
80
David Ahernf1741732019-03-27 20:53:57 -070081struct fib_nh_common {
82 struct net_device *nhc_dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -080083 netdevice_tracker nhc_dev_tracker;
David Ahernf1741732019-03-27 20:53:57 -070084 int nhc_oif;
David Ahernf1741732019-03-27 20:53:57 -070085 unsigned char nhc_scope;
86 u8 nhc_family;
David Ahernbdf00462019-04-05 16:30:26 -070087 u8 nhc_gw_family;
David Ahernecc56632019-04-23 08:48:09 -070088 unsigned char nhc_flags;
89 struct lwtunnel_state *nhc_lwtstate;
David Ahernbdf00462019-04-05 16:30:26 -070090
David Ahernf1741732019-03-27 20:53:57 -070091 union {
92 __be32 ipv4;
93 struct in6_addr ipv6;
94 } nhc_gw;
95
96 int nhc_weight;
97 atomic_t nhc_upper_bound;
David Ahern0f457a32019-04-30 07:45:48 -070098
99 /* v4 specific, but allows fib6_nh with v4 routes */
100 struct rtable __rcu * __percpu *nhc_pcpu_rth_output;
101 struct rtable __rcu *nhc_rth_input;
David Aherna5995e72019-04-30 07:45:50 -0700102 struct fnhe_hash_bucket __rcu *nhc_exceptions;
David Ahernf1741732019-03-27 20:53:57 -0700103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct fib_nh {
David Ahernf1741732019-03-27 20:53:57 -0700106 struct fib_nh_common nh_common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct hlist_node nh_hash;
108 struct fib_info *nh_parent;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100109#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 __u32 nh_tclassid;
111#endif
David S. Miller1fc050a2011-03-07 20:54:48 -0800112 __be32 nh_saddr;
David S. Miller436c3b62011-03-24 17:42:21 -0700113 int nh_saddr_genid;
David Ahernf1741732019-03-27 20:53:57 -0700114#define fib_nh_family nh_common.nhc_family
115#define fib_nh_dev nh_common.nhc_dev
Eric Dumazete44b14e2021-12-06 17:30:32 -0800116#define fib_nh_dev_tracker nh_common.nhc_dev_tracker
David Ahernf1741732019-03-27 20:53:57 -0700117#define fib_nh_oif nh_common.nhc_oif
118#define fib_nh_flags nh_common.nhc_flags
119#define fib_nh_lws nh_common.nhc_lwtstate
120#define fib_nh_scope nh_common.nhc_scope
David Ahernbdf00462019-04-05 16:30:26 -0700121#define fib_nh_gw_family nh_common.nhc_gw_family
David Ahernf1741732019-03-27 20:53:57 -0700122#define fib_nh_gw4 nh_common.nhc_gw.ipv4
123#define fib_nh_gw6 nh_common.nhc_gw.ipv6
124#define fib_nh_weight nh_common.nhc_weight
125#define fib_nh_upper_bound nh_common.nhc_upper_bound
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128/*
129 * This structure contains data shared by many of routes.
130 */
131
David Ahern4c7e8082019-06-03 20:19:51 -0700132struct nexthop;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134struct fib_info {
135 struct hlist_node fib_hash;
136 struct hlist_node fib_lhash;
David Ahern4c7e8082019-06-03 20:19:51 -0700137 struct list_head nh_list;
Denis V. Lunev7462bd742008-01-31 18:49:32 -0800138 struct net *fib_net;
Yajun Deng79976892021-07-29 15:13:50 +0800139 refcount_t fib_treeref;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +0300140 refcount_t fib_clntref;
Eric Dumazet95c96172012-04-15 05:58:06 +0000141 unsigned int fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -0700142 unsigned char fib_dead;
143 unsigned char fib_protocol;
144 unsigned char fib_scope;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000145 unsigned char fib_type;
Al Virob83738a2006-09-26 22:14:15 -0700146 __be32 fib_prefsrc;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +1200147 u32 fib_tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 u32 fib_priority;
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700149 struct dst_metrics *fib_metrics;
150#define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
151#define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
152#define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
153#define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int fib_nhs;
David Ahern19a9d132019-04-05 16:30:39 -0700155 bool fib_nh_is_v6;
David Ahern1bff1a02019-05-22 12:04:42 -0700156 bool nh_updated;
David Ahern4c7e8082019-06-03 20:19:51 -0700157 struct nexthop *nh;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000158 struct rcu_head rcu;
Gustavo A. R. Silvaa5311062020-03-02 06:03:52 -0600159 struct fib_nh fib_nh[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162
163#ifdef CONFIG_IP_MULTIPLE_TABLES
164struct fib_rule;
165#endif
166
David S. Miller5b470442011-01-31 16:10:03 -0800167struct fib_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168struct fib_result {
David Aherneba618a2019-04-02 14:11:55 -0700169 __be32 prefix;
170 unsigned char prefixlen;
171 unsigned char nh_sel;
172 unsigned char type;
173 unsigned char scope;
174 u32 tclassid;
175 struct fib_nh_common *nhc;
176 struct fib_info *fi;
177 struct fib_table *table;
178 struct hlist_head *fa_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
Robert Olsson246955f2005-06-20 13:36:39 -0700181struct fib_result_nl {
Al Viro80e856e2006-09-26 22:19:36 -0700182 __be32 fl_addr; /* To be looked up*/
Thomas Graf5f3008932006-11-09 15:21:41 -0800183 u32 fl_mark;
Robert Olsson246955f2005-06-20 13:36:39 -0700184 unsigned char fl_tos;
185 unsigned char fl_scope;
186 unsigned char tb_id_in;
187
188 unsigned char tb_id; /* Results */
189 unsigned char prefixlen;
190 unsigned char nh_sel;
191 unsigned char type;
192 unsigned char scope;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800193 int err;
Robert Olsson246955f2005-06-20 13:36:39 -0700194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Denis V. Lunev5b9e12d2013-03-13 00:24:15 +0000196#ifdef CONFIG_IP_MULTIPLE_TABLES
197#define FIB_TABLE_HASHSZ 256
198#else
199#define FIB_TABLE_HASHSZ 2
200#endif
201
David Aherndcb1ecb2019-06-03 20:19:50 -0700202__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
203 unsigned char scope);
David Aherneba618a2019-04-02 14:11:55 -0700204__be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
David S. Miller436c3b62011-03-24 17:42:21 -0700205
David Aherneba618a2019-04-02 14:11:55 -0700206#define FIB_RES_NHC(res) ((res).nhc)
207#define FIB_RES_DEV(res) (FIB_RES_NHC(res)->nhc_dev)
208#define FIB_RES_OIF(res) (FIB_RES_NHC(res)->nhc_oif)
David S. Miller1fc050a2011-03-07 20:54:48 -0800209
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200210struct fib_rt_info {
211 struct fib_info *fi;
212 u32 tb_id;
213 __be32 dst;
214 int dst_len;
Guillaume Nault888ade82022-04-08 22:08:37 +0200215 dscp_t dscp;
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200216 u8 type;
Ido Schimmel90b93f12020-01-14 13:23:11 +0200217 u8 offload:1,
218 trap:1,
Amit Cohen36c51002021-02-07 10:22:50 +0200219 offload_failed:1,
220 unused:5;
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200221};
222
Jiri Pirkob90eb752016-09-26 12:52:29 +0200223struct fib_entry_notifier_info {
224 struct fib_notifier_info info; /* must be first */
225 u32 dst;
226 int dst_len;
227 struct fib_info *fi;
Guillaume Nault568a3f32022-04-08 22:08:40 +0200228 dscp_t dscp;
Jiri Pirkob90eb752016-09-26 12:52:29 +0200229 u8 type;
230 u32 tb_id;
Jiri Pirkob90eb752016-09-26 12:52:29 +0200231};
232
Ido Schimmel982acb92017-02-08 11:16:39 +0100233struct fib_nh_notifier_info {
234 struct fib_notifier_info info; /* must be first */
235 struct fib_nh *fib_nh;
236};
237
Jiri Pirko7c550da2019-10-03 11:49:27 +0200238int call_fib4_notifier(struct notifier_block *nb,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +0200239 enum fib_event_type event_type,
Jiri Pirkob90eb752016-09-26 12:52:29 +0200240 struct fib_notifier_info *info);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +0200241int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
242 struct fib_notifier_info *info);
243
244int __net_init fib4_notifier_init(struct net *net);
245void __net_exit fib4_notifier_exit(struct net *net);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200246
David Ahern1bff1a02019-05-22 12:04:42 -0700247void fib_info_notify_update(struct net *net, struct nl_info *info);
Jiri Pirkob7a59552019-10-03 11:49:30 +0200248int fib_notify(struct net *net, struct notifier_block *nb,
249 struct netlink_ext_ack *extack);
Ido Schimmelc0243892017-03-10 08:56:18 +0100250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251struct fib_table {
David S. Miller8e773272012-06-11 00:01:52 -0700252 struct hlist_node tb_hlist;
253 u32 tb_id;
David S. Miller8e773272012-06-11 00:01:52 -0700254 int tb_num_default;
Alexander Duycka7e53532015-03-04 15:02:44 -0800255 struct rcu_head rcu;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800256 unsigned long *tb_data;
Gustavo A. R. Silvaa5311062020-03-02 06:03:52 -0600257 unsigned long __data[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
David Ahern47246762018-10-15 18:56:42 -0700260struct fib_dump_filter {
261 u32 table_id;
262 /* filter_set is an optimization that an entry is set */
263 bool filter_set;
Stefano Brivio564c91f2019-06-21 17:45:20 +0200264 bool dump_routes;
265 bool dump_exceptions;
David Ahern47246762018-10-15 18:56:42 -0700266 unsigned char protocol;
267 unsigned char rt_type;
268 unsigned int flags;
269 struct net_device *dev;
270};
271
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700272int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
273 struct fib_result *res, int fib_flags);
David Ahern6d8422a12017-05-21 10:12:02 -0600274int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
275 struct netlink_ext_ack *extack);
David Ahern78055992017-05-27 16:19:26 -0600276int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
277 struct netlink_ext_ack *extack);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700278int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
David Ahern18a80212018-10-15 18:56:43 -0700279 struct netlink_callback *cb, struct fib_dump_filter *filter);
Ido Schimmelf97f4dd2019-01-09 09:57:39 +0000280int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800281struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
Alexander Duyck3b709332016-11-15 05:46:06 -0500282void fib_table_flush_external(struct fib_table *table);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700283void fib_free_table(struct fib_table *tb);
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +0000284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#ifndef CONFIG_IP_MULTIPLE_TABLES
286
Alexander Duycka5a519b2014-12-02 10:58:21 -0800287#define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
288#define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800290static inline struct fib_table *fib_get_table(struct net *net, u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Alexander Duycka7e53532015-03-04 15:02:44 -0800292 struct hlist_node *tb_hlist;
Denis V. Lunev93456b62008-01-10 03:23:38 -0800293 struct hlist_head *ptr;
294
295 ptr = id == RT_TABLE_LOCAL ?
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800296 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
297 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
Alexander Duycka7e53532015-03-04 15:02:44 -0800298
299 tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
300
301 return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800304static inline struct fib_table *fib_new_table(struct net *net, u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800306 return fib_get_table(net, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
David S. Miller22bd5b92011-03-11 19:54:08 -0500309static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400310 struct fib_result *res, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Alexander Duycka7e53532015-03-04 15:02:44 -0800312 struct fib_table *tb;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800313 int err = -ENETUNREACH;
Denis V. Lunev93456b62008-01-10 03:23:38 -0800314
Alexander Duyck345e9b52014-12-31 10:56:24 -0800315 rcu_read_lock();
Denis V. Lunev93456b62008-01-10 03:23:38 -0800316
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800317 tb = fib_get_table(net, RT_TABLE_MAIN);
Nikola Forró0315e382015-09-17 16:01:32 +0200318 if (tb)
319 err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
320
321 if (err == -EAGAIN)
322 err = -ENETUNREACH;
Alexander Duyck345e9b52014-12-31 10:56:24 -0800323
324 rcu_read_unlock();
325
326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Paolo Abenic43c3d72019-11-20 13:47:36 +0100329static inline bool fib4_has_custom_rules(const struct net *net)
330{
331 return false;
332}
333
Ido Schimmel3c710062017-03-16 09:08:12 +0100334static inline bool fib4_rule_default(const struct fib_rule *rule)
335{
336 return true;
337}
338
Jiri Pirkob7a59552019-10-03 11:49:30 +0200339static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
340 struct netlink_ext_ack *extack)
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200341{
342 return 0;
343}
344
345static inline unsigned int fib4_rules_seq_read(struct net *net)
346{
347 return 0;
348}
349
Roopa Prabhue37b1e92018-02-28 22:42:41 -0500350static inline bool fib4_rules_early_flow_dissect(struct net *net,
351 struct sk_buff *skb,
352 struct flowi4 *fl4,
353 struct flow_keys *flkeys)
354{
355 return false;
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#else /* CONFIG_IP_MULTIPLE_TABLES */
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700358int __net_init fib4_rules_init(struct net *net);
359void __net_exit fib4_rules_exit(struct net *net);
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -0800360
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700361struct fib_table *fib_new_table(struct net *net, u32 id);
362struct fib_table *fib_get_table(struct net *net, u32 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400364int __fib_lookup(struct net *net, struct flowi4 *flp,
365 struct fib_result *res, unsigned int flags);
David S. Millerf4530fa2012-07-05 22:13:13 -0700366
367static inline int fib_lookup(struct net *net, struct flowi4 *flp,
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400368 struct fib_result *res, unsigned int flags)
David S. Millerf4530fa2012-07-05 22:13:13 -0700369{
Alexander Duycka7e53532015-03-04 15:02:44 -0800370 struct fib_table *tb;
Nikola Forró0315e382015-09-17 16:01:32 +0200371 int err = -ENETUNREACH;
Alexander Duyck345e9b52014-12-31 10:56:24 -0800372
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400373 flags |= FIB_LOOKUP_NOREF;
Alexander Duycka7e53532015-03-04 15:02:44 -0800374 if (net->ipv4.fib_has_custom_rules)
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400375 return __fib_lookup(net, flp, res, flags);
Alexander Duyck345e9b52014-12-31 10:56:24 -0800376
Alexander Duycka7e53532015-03-04 15:02:44 -0800377 rcu_read_lock();
Alexander Duyck345e9b52014-12-31 10:56:24 -0800378
Alexander Duycka7e53532015-03-04 15:02:44 -0800379 res->tclassid = 0;
Alexander Duyck345e9b52014-12-31 10:56:24 -0800380
Nikola Forró0315e382015-09-17 16:01:32 +0200381 tb = rcu_dereference_rtnl(net->ipv4.fib_main);
382 if (tb)
383 err = fib_table_lookup(tb, flp, res, flags);
Alexander Duycka7e53532015-03-04 15:02:44 -0800384
Nikola Forró0315e382015-09-17 16:01:32 +0200385 if (!err)
386 goto out;
387
388 tb = rcu_dereference_rtnl(net->ipv4.fib_default);
389 if (tb)
390 err = fib_table_lookup(tb, flp, res, flags);
391
392out:
393 if (err == -EAGAIN)
394 err = -ENETUNREACH;
Alexander Duycka7e53532015-03-04 15:02:44 -0800395
396 rcu_read_unlock();
397
398 return err;
David S. Millerf4530fa2012-07-05 22:13:13 -0700399}
400
Paolo Abenic43c3d72019-11-20 13:47:36 +0100401static inline bool fib4_has_custom_rules(const struct net *net)
402{
403 return net->ipv4.fib_has_custom_rules;
404}
405
Ido Schimmel3c710062017-03-16 09:08:12 +0100406bool fib4_rule_default(const struct fib_rule *rule);
Jiri Pirkob7a59552019-10-03 11:49:30 +0200407int fib4_rules_dump(struct net *net, struct notifier_block *nb,
408 struct netlink_ext_ack *extack);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200409unsigned int fib4_rules_seq_read(struct net *net);
Ido Schimmel3c710062017-03-16 09:08:12 +0100410
Roopa Prabhue37b1e92018-02-28 22:42:41 -0500411static inline bool fib4_rules_early_flow_dissect(struct net *net,
412 struct sk_buff *skb,
413 struct flowi4 *fl4,
414 struct flow_keys *flkeys)
415{
416 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
417
418 if (!net->ipv4.fib_rules_require_fldissect)
419 return false;
420
421 skb_flow_dissect_flow_keys(skb, flkeys, flag);
422 fl4->fl4_sport = flkeys->ports.src;
423 fl4->fl4_dport = flkeys->ports.dst;
424 fl4->flowi4_proto = flkeys->basic.ip_proto;
425
426 return true;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#endif /* CONFIG_IP_MULTIPLE_TABLES */
430
431/* Exported by fib_frontend.c */
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700432extern const struct nla_policy rtm_ipv4_policy[];
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700433void ip_fib_init(void);
David Ahernd1566262019-04-05 16:30:40 -0700434int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
435 struct netlink_ext_ack *extack);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700436__be32 fib_compute_spec_dst(struct sk_buff *skb);
David Ahern78f27562018-09-20 13:50:47 -0700437bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700438int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
439 u8 tos, int oif, struct net_device *dev,
440 struct in_device *idev, u32 *itag);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700441#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf4530fa2012-07-05 22:13:13 -0700442static inline int fib_num_tclassid_users(struct net *net)
443{
Eric Dumazet213f5f82021-12-01 18:26:35 -0800444 return atomic_read(&net->ipv4.fib_num_tclassid_users);
David S. Millerf4530fa2012-07-05 22:13:13 -0700445}
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700446#else
David S. Millerf4530fa2012-07-05 22:13:13 -0700447static inline int fib_num_tclassid_users(struct net *net)
448{
449 return 0;
450}
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700451#endif
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800452int fib_unmerge(struct net *net);
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200453
David Ahern1fd1c762020-05-26 12:56:18 -0600454static inline bool nhc_l3mdev_matches_dev(const struct fib_nh_common *nhc,
455const struct net_device *dev)
456{
457 if (nhc->nhc_dev == dev ||
458 l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex)
459 return true;
460
461 return false;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* Exported by fib_semantics.c */
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700465int ip_fib_check_default(__be32 gw, struct net_device *dev);
Julian Anastasov4f823de2015-10-30 10:23:33 +0200466int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +1200467int fib_sync_down_addr(struct net_device *dev, __be32 local);
David Ahernecc56632019-04-23 08:48:09 -0700468int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +0200469void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
David Ahern06c77c32019-05-22 12:04:46 -0700470void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200471
Ido Schimmelce5c9c22021-05-17 21:15:18 +0300472/* Fields used for sysctl_fib_multipath_hash_fields.
473 * Common to IPv4 and IPv6.
474 *
475 * Add new fields at the end. This is user API.
476 */
477#define FIB_MULTIPATH_HASH_FIELD_SRC_IP BIT(0)
478#define FIB_MULTIPATH_HASH_FIELD_DST_IP BIT(1)
479#define FIB_MULTIPATH_HASH_FIELD_IP_PROTO BIT(2)
480#define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL BIT(3)
481#define FIB_MULTIPATH_HASH_FIELD_SRC_PORT BIT(4)
482#define FIB_MULTIPATH_HASH_FIELD_DST_PORT BIT(5)
483#define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP BIT(6)
484#define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP BIT(7)
485#define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO BIT(8)
486#define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL BIT(9)
487#define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT BIT(10)
488#define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT BIT(11)
489
490#define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK \
491 (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
492 FIB_MULTIPATH_HASH_FIELD_DST_IP | \
493 FIB_MULTIPATH_HASH_FIELD_IP_PROTO | \
494 FIB_MULTIPATH_HASH_FIELD_FLOWLABEL | \
495 FIB_MULTIPATH_HASH_FIELD_SRC_PORT | \
496 FIB_MULTIPATH_HASH_FIELD_DST_PORT)
497
498#define FIB_MULTIPATH_HASH_FIELD_INNER_MASK \
499 (FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP | \
500 FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP | \
501 FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO | \
502 FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL | \
503 FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT | \
504 FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
505
506#define FIB_MULTIPATH_HASH_FIELD_ALL_MASK \
507 (FIB_MULTIPATH_HASH_FIELD_OUTER_MASK | \
508 FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
509
510#define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK \
511 (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
512 FIB_MULTIPATH_HASH_FIELD_DST_IP | \
513 FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
514
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +0200515#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern7efc0b62018-03-02 08:32:12 -0800516int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
Roopa Prabhue37b1e92018-02-28 22:42:41 -0500517 const struct sk_buff *skb, struct flow_keys *flkeys);
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +0200518#endif
David Ahernac1fab22019-05-22 12:04:43 -0700519int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
520 struct netlink_ext_ack *extack);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200521void fib_select_multipath(struct fib_result *res, int hash);
David Ahern3ce58d82015-10-05 08:51:25 -0700522void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +0200523 struct flowi4 *fl4, const struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Aherne4516ef2019-03-27 20:53:48 -0700525int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
526 struct fib_config *cfg, int nh_weight,
527 struct netlink_ext_ack *extack);
David Ahernfaa041a2019-03-27 20:53:49 -0700528void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
Alexander Aringfaee6762020-03-27 18:00:21 -0400529int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
530 struct nlattr *fc_encap, u16 fc_encap_type,
531 void *cfg, gfp_t gfp_flags,
David Ahern979e2762019-03-27 20:53:58 -0700532 struct netlink_ext_ack *extack);
533void fib_nh_common_release(struct fib_nh_common *nhc);
David Aherne4516ef2019-03-27 20:53:48 -0700534
David S. Miller5348ba82011-02-01 15:30:56 -0800535/* Exported by fib_trie.c */
Ido Schimmel90b93f12020-01-14 13:23:11 +0200536void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700537void fib_trie_init(void);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800538struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
David Ahernaf7888a2020-05-26 12:56:17 -0600539bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
540 const struct flowi4 *flp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David S. Millerb6bf3ca2011-02-16 22:04:57 -0800542static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Patrick McHardyc7066f72011-01-14 13:36:42 +0100544#ifdef CONFIG_IP_ROUTE_CLASSID
David Aherneba618a2019-04-02 14:11:55 -0700545 struct fib_nh_common *nhc = res->nhc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#ifdef CONFIG_IP_MULTIPLE_TABLES
547 u32 rtag;
548#endif
David Aherndcb1ecb2019-06-03 20:19:50 -0700549 if (nhc->nhc_family == AF_INET) {
550 struct fib_nh *nh;
551
552 nh = container_of(nhc, struct fib_nh, nh_common);
553 *itag = nh->nh_tclassid << 16;
554 } else {
555 *itag = 0;
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#ifdef CONFIG_IP_MULTIPLE_TABLES
David S. Miller85b91b02012-07-13 08:21:29 -0700559 rtag = res->tclassid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (*itag == 0)
561 *itag = (rtag<<16);
562 *itag |= (rtag>>16);
563#endif
564#endif
565}
566
David Ahern9bd83662019-05-22 12:04:44 -0700567void fib_flush(struct net *net);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700568void free_fib_info(struct fib_info *fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Ido Schimmel1c677b32016-12-03 16:44:59 +0100570static inline void fib_info_hold(struct fib_info *fi)
571{
Reshetova, Elena0029c0d2017-07-04 09:35:02 +0300572 refcount_inc(&fi->fib_clntref);
Ido Schimmel1c677b32016-12-03 16:44:59 +0100573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static inline void fib_info_put(struct fib_info *fi)
576{
Reshetova, Elena0029c0d2017-07-04 09:35:02 +0300577 if (refcount_dec_and_test(&fi->fib_clntref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 free_fib_info(fi);
579}
580
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300581#ifdef CONFIG_PROC_FS
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700582int __net_init fib_proc_init(struct net *net);
583void __net_exit fib_proc_exit(struct net *net);
Li Zefancc8274f2008-02-05 02:54:16 -0800584#else
585static inline int fib_proc_init(struct net *net)
586{
587 return 0;
588}
589static inline void fib_proc_exit(struct net *net)
590{
591}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300592#endif
593
David Ahern50d889b2018-05-21 09:08:13 -0700594u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
595
David Ahern47246762018-10-15 18:56:42 -0700596int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
597 struct fib_dump_filter *filter,
David Aherneffe6792018-10-15 18:56:48 -0700598 struct netlink_callback *cb);
David Ahernc0a72072019-04-02 14:11:58 -0700599
600int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400601 u8 rt_family, unsigned char *flags, bool skip_oif);
David Ahernc0a72072019-04-02 14:11:58 -0700602int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
Xiao Liang597aa162021-09-23 23:03:19 +0800603 int nh_weight, u8 rt_family, u32 nh_tclassid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#endif /* _NET_FIB_H */