12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Definitions for the Forwarding Information Base.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Authors: A.N.Kuznetsov, <[email protected]>
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #ifndef _NET_IP_FIB_H
131da177e4SLinus Torvalds #define _NET_IP_FIB_H
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds #include <net/flow.h>
161da177e4SLinus Torvalds #include <linux/seq_file.h>
174895c771SDavid S. Miller #include <linux/rcupdate.h>
1804b1d4e5SIdo Schimmel #include <net/fib_notifier.h>
19e1ef4bf2SThomas Graf #include <net/fib_rules.h>
20f55fbb6aSGuillaume Nault #include <net/inet_dscp.h>
218e773277SDavid S. Miller #include <net/inetpeer.h>
22d26b3a7cSEric Dumazet #include <linux/percpu.h>
23b90eb754SJiri Pirko #include <linux/notifier.h>
240029c0deSReshetova, Elena #include <linux/refcount.h>
251083d733SIdo Schimmel #include <linux/ip.h>
261fa3314cSIdo Schimmel #include <linux/in_route.h>
271da177e4SLinus Torvalds
284e902c57SThomas Graf struct fib_config {
294e902c57SThomas Graf u8 fc_dst_len;
30f55fbb6aSGuillaume Nault dscp_t fc_dscp;
314e902c57SThomas Graf u8 fc_protocol;
324e902c57SThomas Graf u8 fc_scope;
334e902c57SThomas Graf u8 fc_type;
34f35b794bSDavid Ahern u8 fc_gw_family;
35f35b794bSDavid Ahern /* 2 bytes unused */
364e902c57SThomas Graf u32 fc_table;
376d85c10aSAl Viro __be32 fc_dst;
38a4ea5d43SDavid Ahern union {
39f35b794bSDavid Ahern __be32 fc_gw4;
40a4ea5d43SDavid Ahern struct in6_addr fc_gw6;
41a4ea5d43SDavid Ahern };
424e902c57SThomas Graf int fc_oif;
434e902c57SThomas Graf u32 fc_flags;
444e902c57SThomas Graf u32 fc_priority;
456d85c10aSAl Viro __be32 fc_prefsrc;
46493ced1aSDavid Ahern u32 fc_nh_id;
474e902c57SThomas Graf struct nlattr *fc_mx;
484e902c57SThomas Graf struct rtnexthop *fc_mp;
494e902c57SThomas Graf int fc_mx_len;
504e902c57SThomas Graf int fc_mp_len;
514e902c57SThomas Graf u32 fc_flow;
524e902c57SThomas Graf u32 fc_nlflags;
534e902c57SThomas Graf struct nl_info fc_nlinfo;
54571e7226SRoopa Prabhu struct nlattr *fc_encap;
55571e7226SRoopa Prabhu u16 fc_encap_type;
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds struct fib_info;
59f2bb4bedSDavid S. Miller struct rtable;
601da177e4SLinus Torvalds
614895c771SDavid S. Miller struct fib_nh_exception {
624895c771SDavid S. Miller struct fib_nh_exception __rcu *fnhe_next;
635aad1de5STimo Teräs int fnhe_genid;
644895c771SDavid S. Miller __be32 fnhe_daddr;
654895c771SDavid S. Miller u32 fnhe_pmtu;
66d52e5a7eSSabrina Dubroca bool fnhe_mtu_locked;
67aee06da6SJulian Anastasov __be32 fnhe_gw;
684895c771SDavid S. Miller unsigned long fnhe_expires;
692ffae99dSTimo Teräs struct rtable __rcu *fnhe_rth_input;
702ffae99dSTimo Teräs struct rtable __rcu *fnhe_rth_output;
714895c771SDavid S. Miller unsigned long fnhe_stamp;
72deed49dfSXin Long struct rcu_head rcu;
734895c771SDavid S. Miller };
744895c771SDavid S. Miller
754895c771SDavid S. Miller struct fnhe_hash_bucket {
764895c771SDavid S. Miller struct fib_nh_exception __rcu *chain;
774895c771SDavid S. Miller };
784895c771SDavid S. Miller
79d546c621SEric Dumazet #define FNHE_HASH_SHIFT 11
80d546c621SEric Dumazet #define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT)
814895c771SDavid S. Miller #define FNHE_RECLAIM_DEPTH 5
824895c771SDavid S. Miller
83f1741730SDavid Ahern struct fib_nh_common {
84f1741730SDavid Ahern struct net_device *nhc_dev;
85e44b14ebSEric Dumazet netdevice_tracker nhc_dev_tracker;
86f1741730SDavid Ahern int nhc_oif;
87f1741730SDavid Ahern unsigned char nhc_scope;
88f1741730SDavid Ahern u8 nhc_family;
89bdf00467SDavid Ahern u8 nhc_gw_family;
90ecc5663cSDavid Ahern unsigned char nhc_flags;
91ecc5663cSDavid Ahern struct lwtunnel_state *nhc_lwtstate;
92bdf00467SDavid Ahern
93f1741730SDavid Ahern union {
94f1741730SDavid Ahern __be32 ipv4;
95f1741730SDavid Ahern struct in6_addr ipv6;
96f1741730SDavid Ahern } nhc_gw;
97f1741730SDavid Ahern
98f1741730SDavid Ahern int nhc_weight;
99f1741730SDavid Ahern atomic_t nhc_upper_bound;
1000f457a36SDavid Ahern
1010f457a36SDavid Ahern /* v4 specific, but allows fib6_nh with v4 routes */
1020f457a36SDavid Ahern struct rtable __rcu * __percpu *nhc_pcpu_rth_output;
1030f457a36SDavid Ahern struct rtable __rcu *nhc_rth_input;
104a5995e71SDavid Ahern struct fnhe_hash_bucket __rcu *nhc_exceptions;
105f1741730SDavid Ahern };
106f1741730SDavid Ahern
1071da177e4SLinus Torvalds struct fib_nh {
108f1741730SDavid Ahern struct fib_nh_common nh_common;
1091da177e4SLinus Torvalds struct hlist_node nh_hash;
1101da177e4SLinus Torvalds struct fib_info *nh_parent;
111c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1121da177e4SLinus Torvalds __u32 nh_tclassid;
1131da177e4SLinus Torvalds #endif
1141fc050a1SDavid S. Miller __be32 nh_saddr;
115436c3b66SDavid S. Miller int nh_saddr_genid;
116f1741730SDavid Ahern #define fib_nh_family nh_common.nhc_family
117f1741730SDavid Ahern #define fib_nh_dev nh_common.nhc_dev
118e44b14ebSEric Dumazet #define fib_nh_dev_tracker nh_common.nhc_dev_tracker
119f1741730SDavid Ahern #define fib_nh_oif nh_common.nhc_oif
120f1741730SDavid Ahern #define fib_nh_flags nh_common.nhc_flags
121f1741730SDavid Ahern #define fib_nh_lws nh_common.nhc_lwtstate
122f1741730SDavid Ahern #define fib_nh_scope nh_common.nhc_scope
123bdf00467SDavid Ahern #define fib_nh_gw_family nh_common.nhc_gw_family
124f1741730SDavid Ahern #define fib_nh_gw4 nh_common.nhc_gw.ipv4
125f1741730SDavid Ahern #define fib_nh_gw6 nh_common.nhc_gw.ipv6
126f1741730SDavid Ahern #define fib_nh_weight nh_common.nhc_weight
127f1741730SDavid Ahern #define fib_nh_upper_bound nh_common.nhc_upper_bound
1281da177e4SLinus Torvalds };
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds /*
1311da177e4SLinus Torvalds * This structure contains data shared by many of routes.
1321da177e4SLinus Torvalds */
1331da177e4SLinus Torvalds
1344c7e8084SDavid Ahern struct nexthop;
1354c7e8084SDavid Ahern
1361da177e4SLinus Torvalds struct fib_info {
1371da177e4SLinus Torvalds struct hlist_node fib_hash;
1381da177e4SLinus Torvalds struct hlist_node fib_lhash;
1394c7e8084SDavid Ahern struct list_head nh_list;
1407462bd74SDenis V. Lunev struct net *fib_net;
14179976892SYajun Deng refcount_t fib_treeref;
1420029c0deSReshetova, Elena refcount_t fib_clntref;
14395c96174SEric Dumazet unsigned int fib_flags;
14437e826c5SDavid S. Miller unsigned char fib_dead;
14537e826c5SDavid S. Miller unsigned char fib_protocol;
14637e826c5SDavid S. Miller unsigned char fib_scope;
147f4ef85bbSEric Dumazet unsigned char fib_type;
148b83738aeSAl Viro __be32 fib_prefsrc;
1495a56a0b3SMark Tomlinson u32 fib_tb_id;
1501da177e4SLinus Torvalds u32 fib_priority;
1513fb07dafSEric Dumazet struct dst_metrics *fib_metrics;
1523fb07dafSEric Dumazet #define fib_mtu fib_metrics->metrics[RTAX_MTU-1]
1533fb07dafSEric Dumazet #define fib_window fib_metrics->metrics[RTAX_WINDOW-1]
1543fb07dafSEric Dumazet #define fib_rtt fib_metrics->metrics[RTAX_RTT-1]
1553fb07dafSEric Dumazet #define fib_advmss fib_metrics->metrics[RTAX_ADVMSS-1]
1561da177e4SLinus Torvalds int fib_nhs;
15719a9d136SDavid Ahern bool fib_nh_is_v6;
1581bff1a0cSDavid Ahern bool nh_updated;
1594b2b6060SHangbin Liu bool pfsrc_removed;
1604c7e8084SDavid Ahern struct nexthop *nh;
161ebc0ffaeSEric Dumazet struct rcu_head rcu;
1625b98fd5dSKees Cook struct fib_nh fib_nh[] __counted_by(fib_nhs);
1631da177e4SLinus Torvalds };
1641da177e4SLinus Torvalds
165*cfc47029SKuniyuki Iwashima int __net_init fib4_semantics_init(struct net *net);
166*cfc47029SKuniyuki Iwashima void __net_exit fib4_semantics_exit(struct net *net);
1671da177e4SLinus Torvalds
1681da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
1691da177e4SLinus Torvalds struct fib_rule;
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds
1725b470441SDavid S. Miller struct fib_table;
1731da177e4SLinus Torvalds struct fib_result {
1746ffd9034SDavid Ahern __be32 prefix;
1751da177e4SLinus Torvalds unsigned char prefixlen;
1761da177e4SLinus Torvalds unsigned char nh_sel;
1771da177e4SLinus Torvalds unsigned char type;
1781da177e4SLinus Torvalds unsigned char scope;
17985b91b03SDavid S. Miller u32 tclassid;
180338bb57eSIdo Schimmel dscp_t dscp;
181eba618abSDavid Ahern struct fib_nh_common *nhc;
1821da177e4SLinus Torvalds struct fib_info *fi;
1835b470441SDavid S. Miller struct fib_table *table;
18456315f9eSAlexander Duyck struct hlist_head *fa_head;
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds
187246955feSRobert Olsson struct fib_result_nl {
18880e856e1SAl Viro __be32 fl_addr; /* To be looked up*/
1895f300893SThomas Graf u32 fl_mark;
190246955feSRobert Olsson unsigned char fl_tos;
191246955feSRobert Olsson unsigned char fl_scope;
192246955feSRobert Olsson unsigned char tb_id_in;
193246955feSRobert Olsson
194246955feSRobert Olsson unsigned char tb_id; /* Results */
195246955feSRobert Olsson unsigned char prefixlen;
196246955feSRobert Olsson unsigned char nh_sel;
197246955feSRobert Olsson unsigned char type;
198246955feSRobert Olsson unsigned char scope;
199246955feSRobert Olsson int err;
200246955feSRobert Olsson };
2011da177e4SLinus Torvalds
2025b9e12dbSDenis V. Lunev #ifdef CONFIG_IP_MULTIPLE_TABLES
2035b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 256
2045b9e12dbSDenis V. Lunev #else
2055b9e12dbSDenis V. Lunev #define FIB_TABLE_HASHSZ 2
2065b9e12dbSDenis V. Lunev #endif
2075b9e12dbSDenis V. Lunev
208dcb1ecb5SDavid Ahern __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
209c3669486SDavid Ahern unsigned char scope);
210eba618abSDavid Ahern __be32 fib_result_prefsrc(struct net *net, struct fib_result *res);
211436c3b66SDavid S. Miller
212eba618abSDavid Ahern #define FIB_RES_NHC(res) ((res).nhc)
213eba618abSDavid Ahern #define FIB_RES_DEV(res) (FIB_RES_NHC(res)->nhc_dev)
214eba618abSDavid Ahern #define FIB_RES_OIF(res) (FIB_RES_NHC(res)->nhc_oif)
2151fc050a1SDavid S. Miller
2161e301fd0SIdo Schimmel struct fib_rt_info {
2171e301fd0SIdo Schimmel struct fib_info *fi;
2181e301fd0SIdo Schimmel u32 tb_id;
2191e301fd0SIdo Schimmel __be32 dst;
2201e301fd0SIdo Schimmel int dst_len;
221888ade8fSGuillaume Nault dscp_t dscp;
2221e301fd0SIdo Schimmel u8 type;
22390b93f1bSIdo Schimmel u8 offload:1,
22490b93f1bSIdo Schimmel trap:1,
22536c5100eSAmit Cohen offload_failed:1,
22636c5100eSAmit Cohen unused:5;
2271e301fd0SIdo Schimmel };
2281e301fd0SIdo Schimmel
229b90eb754SJiri Pirko struct fib_entry_notifier_info {
230b90eb754SJiri Pirko struct fib_notifier_info info; /* must be first */
231b90eb754SJiri Pirko u32 dst;
232b90eb754SJiri Pirko int dst_len;
233b90eb754SJiri Pirko struct fib_info *fi;
234568a3f33SGuillaume Nault dscp_t dscp;
235b90eb754SJiri Pirko u8 type;
236b90eb754SJiri Pirko u32 tb_id;
237b90eb754SJiri Pirko };
238b90eb754SJiri Pirko
239982acb97SIdo Schimmel struct fib_nh_notifier_info {
240982acb97SIdo Schimmel struct fib_notifier_info info; /* must be first */
241982acb97SIdo Schimmel struct fib_nh *fib_nh;
242982acb97SIdo Schimmel };
243982acb97SIdo Schimmel
2447c550dafSJiri Pirko int call_fib4_notifier(struct notifier_block *nb,
245c0243892SIdo Schimmel enum fib_event_type event_type,
246c0243892SIdo Schimmel struct fib_notifier_info *info);
24704b1d4e5SIdo Schimmel int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
248b90eb754SJiri Pirko struct fib_notifier_info *info);
249b90eb754SJiri Pirko
25004b1d4e5SIdo Schimmel int __net_init fib4_notifier_init(struct net *net);
25104b1d4e5SIdo Schimmel void __net_exit fib4_notifier_exit(struct net *net);
25204b1d4e5SIdo Schimmel
2531bff1a0cSDavid Ahern void fib_info_notify_update(struct net *net, struct nl_info *info);
254b7a59557SJiri Pirko int fib_notify(struct net *net, struct notifier_block *nb,
255b7a59557SJiri Pirko struct netlink_ext_ack *extack);
256c0243892SIdo Schimmel
2571da177e4SLinus Torvalds struct fib_table {
2581af5a8c4SPatrick McHardy struct hlist_node tb_hlist;
2592dfe55b4SPatrick McHardy u32 tb_id;
26021d8c49eSDavid S. Miller int tb_num_default;
261a7e53531SAlexander Duyck struct rcu_head rcu;
2620ddcf43dSAlexander Duyck unsigned long *tb_data;
263a5311060SGustavo A. R. Silva unsigned long __data[];
2641da177e4SLinus Torvalds };
2651da177e4SLinus Torvalds
2664724676dSDavid Ahern struct fib_dump_filter {
2674724676dSDavid Ahern u32 table_id;
2684724676dSDavid Ahern /* filter_set is an optimization that an entry is set */
2694724676dSDavid Ahern bool filter_set;
270564c91f7SStefano Brivio bool dump_routes;
271564c91f7SStefano Brivio bool dump_exceptions;
27222e36ea9SEric Dumazet bool rtnl_held;
2734724676dSDavid Ahern unsigned char protocol;
2744724676dSDavid Ahern unsigned char rt_type;
2754724676dSDavid Ahern unsigned int flags;
2764724676dSDavid Ahern struct net_device *dev;
2774724676dSDavid Ahern };
2784724676dSDavid Ahern
2795c3a0fd7SJoe Perches int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
280ebc0ffaeSEric Dumazet struct fib_result *res, int fib_flags);
2816d8422a1SDavid Ahern int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
2826d8422a1SDavid Ahern struct netlink_ext_ack *extack);
28378055998SDavid Ahern int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
28478055998SDavid Ahern struct netlink_ext_ack *extack);
2855c3a0fd7SJoe Perches int fib_table_dump(struct fib_table *table, struct sk_buff *skb,
28618a8021aSDavid Ahern struct netlink_callback *cb, struct fib_dump_filter *filter);
287f97f4dd8SIdo Schimmel int fib_table_flush(struct net *net, struct fib_table *table, bool flush_all);
2880ddcf43dSAlexander Duyck struct fib_table *fib_trie_unmerge(struct fib_table *main_tb);
2893b709334SAlexander Duyck void fib_table_flush_external(struct fib_table *table);
2905c3a0fd7SJoe Perches void fib_free_table(struct fib_table *tb);
2914aa2c466SPavel Emelyanov
2921da177e4SLinus Torvalds #ifndef CONFIG_IP_MULTIPLE_TABLES
2931da177e4SLinus Torvalds
294a5a519b2SAlexander Duyck #define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1))
295a5a519b2SAlexander Duyck #define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1))
2961da177e4SLinus Torvalds
fib_get_table(struct net * net,u32 id)2978ad4942cSDenis V. Lunev static inline struct fib_table *fib_get_table(struct net *net, u32 id)
2981da177e4SLinus Torvalds {
299a7e53531SAlexander Duyck struct hlist_node *tb_hlist;
30093456b6dSDenis V. Lunev struct hlist_head *ptr;
30193456b6dSDenis V. Lunev
30293456b6dSDenis V. Lunev ptr = id == RT_TABLE_LOCAL ?
303e4aef8aeSDenis V. Lunev &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] :
304e4aef8aeSDenis V. Lunev &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX];
305a7e53531SAlexander Duyck
306a7e53531SAlexander Duyck tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr));
307a7e53531SAlexander Duyck
308a7e53531SAlexander Duyck return hlist_entry(tb_hlist, struct fib_table, tb_hlist);
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds
fib_new_table(struct net * net,u32 id)3118ad4942cSDenis V. Lunev static inline struct fib_table *fib_new_table(struct net *net, u32 id)
3121da177e4SLinus Torvalds {
3138ad4942cSDenis V. Lunev return fib_get_table(net, id);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds
fib_lookup(struct net * net,const struct flowi4 * flp,struct fib_result * res,unsigned int flags)31622bd5b9bSDavid S. Miller static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
3170eeb075fSAndy Gospodarek struct fib_result *res, unsigned int flags)
3181da177e4SLinus Torvalds {
319a7e53531SAlexander Duyck struct fib_table *tb;
3200ddcf43dSAlexander Duyck int err = -ENETUNREACH;
32193456b6dSDenis V. Lunev
322345e9b54SAlexander Duyck rcu_read_lock();
32393456b6dSDenis V. Lunev
324a7e53531SAlexander Duyck tb = fib_get_table(net, RT_TABLE_MAIN);
3250315e382SNikola Forró if (tb)
3260315e382SNikola Forró err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
3270315e382SNikola Forró
3280315e382SNikola Forró if (err == -EAGAIN)
3290315e382SNikola Forró err = -ENETUNREACH;
330345e9b54SAlexander Duyck
331345e9b54SAlexander Duyck rcu_read_unlock();
332345e9b54SAlexander Duyck
333345e9b54SAlexander Duyck return err;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds
fib4_has_custom_rules(const struct net * net)336c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
337c43c3d76SPaolo Abeni {
338c43c3d76SPaolo Abeni return false;
339c43c3d76SPaolo Abeni }
340c43c3d76SPaolo Abeni
fib4_rule_default(const struct fib_rule * rule)3413c71006dSIdo Schimmel static inline bool fib4_rule_default(const struct fib_rule *rule)
3423c71006dSIdo Schimmel {
3433c71006dSIdo Schimmel return true;
3443c71006dSIdo Schimmel }
3453c71006dSIdo Schimmel
fib4_rules_dump(struct net * net,struct notifier_block * nb,struct netlink_ext_ack * extack)346b7a59557SJiri Pirko static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
347b7a59557SJiri Pirko struct netlink_ext_ack *extack)
3481b2a4440SIdo Schimmel {
3491b2a4440SIdo Schimmel return 0;
3501b2a4440SIdo Schimmel }
3511b2a4440SIdo Schimmel
fib4_rules_seq_read(const struct net * net)35216207384SEric Dumazet static inline unsigned int fib4_rules_seq_read(const struct net *net)
3531b2a4440SIdo Schimmel {
3541b2a4440SIdo Schimmel return 0;
3551b2a4440SIdo Schimmel }
3561b2a4440SIdo Schimmel
fib4_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi4 * fl4,struct flow_keys * flkeys)357e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
358e37b1e97SRoopa Prabhu struct sk_buff *skb,
359e37b1e97SRoopa Prabhu struct flowi4 *fl4,
360e37b1e97SRoopa Prabhu struct flow_keys *flkeys)
361e37b1e97SRoopa Prabhu {
362e37b1e97SRoopa Prabhu return false;
363e37b1e97SRoopa Prabhu }
3641da177e4SLinus Torvalds #else /* CONFIG_IP_MULTIPLE_TABLES */
3655c3a0fd7SJoe Perches int __net_init fib4_rules_init(struct net *net);
3665c3a0fd7SJoe Perches void __net_exit fib4_rules_exit(struct net *net);
367c3e9a353SPavel Emelyanov
3685c3a0fd7SJoe Perches struct fib_table *fib_new_table(struct net *net, u32 id);
3695c3a0fd7SJoe Perches struct fib_table *fib_get_table(struct net *net, u32 id);
3701da177e4SLinus Torvalds
3710eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
3720eeb075fSAndy Gospodarek struct fib_result *res, unsigned int flags);
373f4530fa5SDavid S. Miller
fib_lookup(struct net * net,struct flowi4 * flp,struct fib_result * res,unsigned int flags)374f4530fa5SDavid S. Miller static inline int fib_lookup(struct net *net, struct flowi4 *flp,
3750eeb075fSAndy Gospodarek struct fib_result *res, unsigned int flags)
376f4530fa5SDavid S. Miller {
377a7e53531SAlexander Duyck struct fib_table *tb;
3780315e382SNikola Forró int err = -ENETUNREACH;
379a7e53531SAlexander Duyck
3800eeb075fSAndy Gospodarek flags |= FIB_LOOKUP_NOREF;
381a7e53531SAlexander Duyck if (net->ipv4.fib_has_custom_rules)
3820eeb075fSAndy Gospodarek return __fib_lookup(net, flp, res, flags);
383345e9b54SAlexander Duyck
384345e9b54SAlexander Duyck rcu_read_lock();
385345e9b54SAlexander Duyck
38685b91b03SDavid S. Miller res->tclassid = 0;
387a7e53531SAlexander Duyck
388a7e53531SAlexander Duyck tb = rcu_dereference_rtnl(net->ipv4.fib_main);
3890315e382SNikola Forró if (tb)
3900315e382SNikola Forró err = fib_table_lookup(tb, flp, res, flags);
3910315e382SNikola Forró
3920315e382SNikola Forró if (!err)
3930315e382SNikola Forró goto out;
394a7e53531SAlexander Duyck
395a7e53531SAlexander Duyck tb = rcu_dereference_rtnl(net->ipv4.fib_default);
3960315e382SNikola Forró if (tb)
3970315e382SNikola Forró err = fib_table_lookup(tb, flp, res, flags);
3980315e382SNikola Forró
3990315e382SNikola Forró out:
4000315e382SNikola Forró if (err == -EAGAIN)
4010315e382SNikola Forró err = -ENETUNREACH;
402345e9b54SAlexander Duyck
403345e9b54SAlexander Duyck rcu_read_unlock();
404345e9b54SAlexander Duyck
405345e9b54SAlexander Duyck return err;
406f4530fa5SDavid S. Miller }
407f4530fa5SDavid S. Miller
fib4_has_custom_rules(const struct net * net)408c43c3d76SPaolo Abeni static inline bool fib4_has_custom_rules(const struct net *net)
409c43c3d76SPaolo Abeni {
410c43c3d76SPaolo Abeni return net->ipv4.fib_has_custom_rules;
411c43c3d76SPaolo Abeni }
412c43c3d76SPaolo Abeni
4133c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule);
414b7a59557SJiri Pirko int fib4_rules_dump(struct net *net, struct notifier_block *nb,
415b7a59557SJiri Pirko struct netlink_ext_ack *extack);
41616207384SEric Dumazet unsigned int fib4_rules_seq_read(const struct net *net);
4173c71006dSIdo Schimmel
fib4_rules_early_flow_dissect(struct net * net,struct sk_buff * skb,struct flowi4 * fl4,struct flow_keys * flkeys)418e37b1e97SRoopa Prabhu static inline bool fib4_rules_early_flow_dissect(struct net *net,
419e37b1e97SRoopa Prabhu struct sk_buff *skb,
420e37b1e97SRoopa Prabhu struct flowi4 *fl4,
421e37b1e97SRoopa Prabhu struct flow_keys *flkeys)
422e37b1e97SRoopa Prabhu {
423e37b1e97SRoopa Prabhu unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
424e37b1e97SRoopa Prabhu
425e37b1e97SRoopa Prabhu if (!net->ipv4.fib_rules_require_fldissect)
426e37b1e97SRoopa Prabhu return false;
427e37b1e97SRoopa Prabhu
4288aae7625SFlorian Westphal memset(flkeys, 0, sizeof(*flkeys));
4298aae7625SFlorian Westphal __skb_flow_dissect(net, skb, &flow_keys_dissector,
4308aae7625SFlorian Westphal flkeys, NULL, 0, 0, 0, flag);
4318aae7625SFlorian Westphal
432e37b1e97SRoopa Prabhu fl4->fl4_sport = flkeys->ports.src;
433e37b1e97SRoopa Prabhu fl4->fl4_dport = flkeys->ports.dst;
434e37b1e97SRoopa Prabhu fl4->flowi4_proto = flkeys->basic.ip_proto;
435e37b1e97SRoopa Prabhu
436e37b1e97SRoopa Prabhu return true;
437e37b1e97SRoopa Prabhu }
438e37b1e97SRoopa Prabhu
4391da177e4SLinus Torvalds #endif /* CONFIG_IP_MULTIPLE_TABLES */
4401da177e4SLinus Torvalds
fib_dscp_masked_match(dscp_t dscp,const struct flowi4 * fl4)4411fa3314cSIdo Schimmel static inline bool fib_dscp_masked_match(dscp_t dscp, const struct flowi4 *fl4)
4421fa3314cSIdo Schimmel {
4431fa3314cSIdo Schimmel return dscp == inet_dsfield_to_dscp(RT_TOS(fl4->flowi4_tos));
4441fa3314cSIdo Schimmel }
4451fa3314cSIdo Schimmel
4461da177e4SLinus Torvalds /* Exported by fib_frontend.c */
447ef7c79edSPatrick McHardy extern const struct nla_policy rtm_ipv4_policy[];
4485c3a0fd7SJoe Perches void ip_fib_init(void);
449d1566268SDavid Ahern int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
450d1566268SDavid Ahern struct netlink_ext_ack *extack);
4515c3a0fd7SJoe Perches __be32 fib_compute_spec_dst(struct sk_buff *skb);
45278f2756cSDavid Ahern bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
4535c3a0fd7SJoe Perches int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
454d36236abSGuillaume Nault dscp_t dscp, int oif, struct net_device *dev,
4559e56e380SDavid S. Miller struct in_device *idev, u32 *itag);
456d36236abSGuillaume Nault
45737653a0bSMenglong Dong static inline enum skb_drop_reason
fib_validate_source_reason(struct sk_buff * skb,__be32 src,__be32 dst,dscp_t dscp,int oif,struct net_device * dev,struct in_device * idev,u32 * itag)45837653a0bSMenglong Dong fib_validate_source_reason(struct sk_buff *skb, __be32 src, __be32 dst,
45937653a0bSMenglong Dong dscp_t dscp, int oif, struct net_device *dev,
46037653a0bSMenglong Dong struct in_device *idev, u32 *itag)
46137653a0bSMenglong Dong {
46237653a0bSMenglong Dong int err = fib_validate_source(skb, src, dst, dscp, oif, dev, idev,
46337653a0bSMenglong Dong itag);
46437653a0bSMenglong Dong if (err < 0)
46537653a0bSMenglong Dong return -err;
46637653a0bSMenglong Dong return SKB_NOT_DROPPED_YET;
46737653a0bSMenglong Dong }
46837653a0bSMenglong Dong
4697a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
fib_num_tclassid_users(struct net * net)470f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
471f4530fa5SDavid S. Miller {
472213f5f8fSEric Dumazet return atomic_read(&net->ipv4.fib_num_tclassid_users);
473f4530fa5SDavid S. Miller }
4747a9bc9b8SDavid S. Miller #else
fib_num_tclassid_users(struct net * net)475f4530fa5SDavid S. Miller static inline int fib_num_tclassid_users(struct net *net)
476f4530fa5SDavid S. Miller {
477f4530fa5SDavid S. Miller return 0;
478f4530fa5SDavid S. Miller }
4797a9bc9b8SDavid S. Miller #endif
4800ddcf43dSAlexander Duyck int fib_unmerge(struct net *net);
48114c85021SArnaldo Carvalho de Melo
nhc_l3mdev_matches_dev(const struct fib_nh_common * nhc,const struct net_device * dev)4821fd1c768SDavid Ahern static inline bool nhc_l3mdev_matches_dev(const struct fib_nh_common *nhc,
4831fd1c768SDavid Ahern const struct net_device *dev)
4841fd1c768SDavid Ahern {
4851fd1c768SDavid Ahern if (nhc->nhc_dev == dev ||
4861fd1c768SDavid Ahern l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex)
4871fd1c768SDavid Ahern return true;
4881fd1c768SDavid Ahern
4891fd1c768SDavid Ahern return false;
4901fd1c768SDavid Ahern }
4911fd1c768SDavid Ahern
4921da177e4SLinus Torvalds /* Exported by fib_semantics.c */
4935c3a0fd7SJoe Perches int ip_fib_check_default(__be32 gw, struct net_device *dev);
4944f823defSJulian Anastasov int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
4955a56a0b3SMark Tomlinson int fib_sync_down_addr(struct net_device *dev, __be32 local);
496ecc5663cSDavid Ahern int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
497af7d6cceSSabrina Dubroca void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
49806c77c3eSDavid Ahern void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
4990e884c78SPeter Nørlund
500ce5c9c20SIdo Schimmel /* Fields used for sysctl_fib_multipath_hash_fields.
501ce5c9c20SIdo Schimmel * Common to IPv4 and IPv6.
502ce5c9c20SIdo Schimmel *
503ce5c9c20SIdo Schimmel * Add new fields at the end. This is user API.
504ce5c9c20SIdo Schimmel */
505ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_IP BIT(0)
506ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_IP BIT(1)
507ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_IP_PROTO BIT(2)
508ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL BIT(3)
509ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_SRC_PORT BIT(4)
510ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DST_PORT BIT(5)
511ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP BIT(6)
512ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP BIT(7)
513ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO BIT(8)
514ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL BIT(9)
515ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT BIT(10)
516ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT BIT(11)
517ce5c9c20SIdo Schimmel
518ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK \
519ce5c9c20SIdo Schimmel (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
520ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_DST_IP | \
521ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_IP_PROTO | \
522ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_FLOWLABEL | \
523ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_SRC_PORT | \
524ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_DST_PORT)
525ce5c9c20SIdo Schimmel
526ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_INNER_MASK \
527ce5c9c20SIdo Schimmel (FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP | \
528ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP | \
529ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO | \
530ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL | \
531ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT | \
532ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
533ce5c9c20SIdo Schimmel
534ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_ALL_MASK \
535ce5c9c20SIdo Schimmel (FIB_MULTIPATH_HASH_FIELD_OUTER_MASK | \
536ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
537ce5c9c20SIdo Schimmel
538ce5c9c20SIdo Schimmel #define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK \
539ce5c9c20SIdo Schimmel (FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
540ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_DST_IP | \
541ce5c9c20SIdo Schimmel FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
542ce5c9c20SIdo Schimmel
543bf4e0a3dSNikolay Aleksandrov #ifdef CONFIG_IP_ROUTE_MULTIPATH
5447efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
545e37b1e97SRoopa Prabhu const struct sk_buff *skb, struct flow_keys *flkeys);
5463e453ca1SPetr Machata
5474ee2a8caSPetr Machata static void
fib_multipath_hash_construct_key(siphash_key_t * key,u32 mp_seed)5484ee2a8caSPetr Machata fib_multipath_hash_construct_key(siphash_key_t *key, u32 mp_seed)
5494ee2a8caSPetr Machata {
5504ee2a8caSPetr Machata u64 mp_seed_64 = mp_seed;
5514ee2a8caSPetr Machata
5524ee2a8caSPetr Machata key->key[0] = (mp_seed_64 << 32) | mp_seed_64;
5534ee2a8caSPetr Machata key->key[1] = key->key[0];
5544ee2a8caSPetr Machata }
5554ee2a8caSPetr Machata
fib_multipath_hash_from_keys(const struct net * net,struct flow_keys * keys)5564ee2a8caSPetr Machata static inline u32 fib_multipath_hash_from_keys(const struct net *net,
5574ee2a8caSPetr Machata struct flow_keys *keys)
5584ee2a8caSPetr Machata {
5594ee2a8caSPetr Machata siphash_aligned_key_t hash_key;
5604ee2a8caSPetr Machata u32 mp_seed;
5614ee2a8caSPetr Machata
5624ee2a8caSPetr Machata mp_seed = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_seed).mp_seed;
5634ee2a8caSPetr Machata fib_multipath_hash_construct_key(&hash_key, mp_seed);
5644ee2a8caSPetr Machata
5654ee2a8caSPetr Machata return flow_hash_from_keys_seed(keys, &hash_key);
5664ee2a8caSPetr Machata }
5674ee2a8caSPetr Machata #else
fib_multipath_hash_from_keys(const struct net * net,struct flow_keys * keys)5683e453ca1SPetr Machata static inline u32 fib_multipath_hash_from_keys(const struct net *net,
5693e453ca1SPetr Machata struct flow_keys *keys)
5703e453ca1SPetr Machata {
5713e453ca1SPetr Machata return flow_hash_from_keys(keys);
5723e453ca1SPetr Machata }
5734ee2a8caSPetr Machata #endif
5743e453ca1SPetr Machata
575ac1fab2dSDavid Ahern int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
576ac1fab2dSDavid Ahern struct netlink_ext_ack *extack);
5770e884c78SPeter Nørlund void fib_select_multipath(struct fib_result *res, int hash);
5783ce58d84SDavid Ahern void fib_select_path(struct net *net, struct fib_result *res,
579bf4e0a3dSNikolay Aleksandrov struct flowi4 *fl4, const struct sk_buff *skb);
5801da177e4SLinus Torvalds
581e4516ef6SDavid Ahern int fib_nh_init(struct net *net, struct fib_nh *fib_nh,
582e4516ef6SDavid Ahern struct fib_config *cfg, int nh_weight,
583e4516ef6SDavid Ahern struct netlink_ext_ack *extack);
584faa041a4SDavid Ahern void fib_nh_release(struct net *net, struct fib_nh *fib_nh);
585faee6769SAlexander Aring int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
586faee6769SAlexander Aring struct nlattr *fc_encap, u16 fc_encap_type,
587faee6769SAlexander Aring void *cfg, gfp_t gfp_flags,
588979e276eSDavid Ahern struct netlink_ext_ack *extack);
589979e276eSDavid Ahern void fib_nh_common_release(struct fib_nh_common *nhc);
590e4516ef6SDavid Ahern
5915348ba85SDavid S. Miller /* Exported by fib_trie.c */
59290b93f1bSIdo Schimmel void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri);
5935c3a0fd7SJoe Perches void fib_trie_init(void);
5940ddcf43dSAlexander Duyck struct fib_table *fib_trie_table(u32 id, struct fib_table *alias);
595af7888adSDavid Ahern bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
596af7888adSDavid Ahern const struct flowi4 *flp);
5971da177e4SLinus Torvalds
fib_combine_itag(u32 * itag,const struct fib_result * res)598b6bf3ca0SDavid S. Miller static inline void fib_combine_itag(u32 *itag, const struct fib_result *res)
5991da177e4SLinus Torvalds {
600c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
601eba618abSDavid Ahern struct fib_nh_common *nhc = res->nhc;
6021da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
6031da177e4SLinus Torvalds u32 rtag;
6041da177e4SLinus Torvalds #endif
605dcb1ecb5SDavid Ahern if (nhc->nhc_family == AF_INET) {
606dcb1ecb5SDavid Ahern struct fib_nh *nh;
607dcb1ecb5SDavid Ahern
608dcb1ecb5SDavid Ahern nh = container_of(nhc, struct fib_nh, nh_common);
609eba618abSDavid Ahern *itag = nh->nh_tclassid << 16;
610dcb1ecb5SDavid Ahern } else {
611dcb1ecb5SDavid Ahern *itag = 0;
612dcb1ecb5SDavid Ahern }
613dcb1ecb5SDavid Ahern
6141da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
61585b91b03SDavid S. Miller rtag = res->tclassid;
6161da177e4SLinus Torvalds if (*itag == 0)
6171da177e4SLinus Torvalds *itag = (rtag<<16);
6181da177e4SLinus Torvalds *itag |= (rtag>>16);
6191da177e4SLinus Torvalds #endif
6201da177e4SLinus Torvalds #endif
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds
6239bd83667SDavid Ahern void fib_flush(struct net *net);
6245c3a0fd7SJoe Perches void free_fib_info(struct fib_info *fi);
6251da177e4SLinus Torvalds
fib_info_hold(struct fib_info * fi)6261c677b3dSIdo Schimmel static inline void fib_info_hold(struct fib_info *fi)
6271c677b3dSIdo Schimmel {
6280029c0deSReshetova, Elena refcount_inc(&fi->fib_clntref);
6291c677b3dSIdo Schimmel }
6301c677b3dSIdo Schimmel
fib_info_put(struct fib_info * fi)6311da177e4SLinus Torvalds static inline void fib_info_put(struct fib_info *fi)
6321da177e4SLinus Torvalds {
6330029c0deSReshetova, Elena if (refcount_dec_and_test(&fi->fib_clntref))
6341da177e4SLinus Torvalds free_fib_info(fi);
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds
63720380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
6385c3a0fd7SJoe Perches int __net_init fib_proc_init(struct net *net);
6395c3a0fd7SJoe Perches void __net_exit fib_proc_exit(struct net *net);
640cc8274f5SLi Zefan #else
fib_proc_init(struct net * net)641cc8274f5SLi Zefan static inline int fib_proc_init(struct net *net)
642cc8274f5SLi Zefan {
643cc8274f5SLi Zefan return 0;
644cc8274f5SLi Zefan }
fib_proc_exit(struct net * net)645cc8274f5SLi Zefan static inline void fib_proc_exit(struct net *net)
646cc8274f5SLi Zefan {
647cc8274f5SLi Zefan }
64820380731SArnaldo Carvalho de Melo #endif
64920380731SArnaldo Carvalho de Melo
65050d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr);
65150d889b1SDavid Ahern
6524724676dSDavid Ahern int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
6534724676dSDavid Ahern struct fib_dump_filter *filter,
654effe6792SDavid Ahern struct netlink_callback *cb);
655c0a72077SDavid Ahern
656c0a72077SDavid Ahern int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nh,
6577bdf4de1SDonald Sharp u8 rt_family, unsigned char *flags, bool skip_oif);
658c0a72077SDavid Ahern int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nh,
659597aa16cSXiao Liang int nh_weight, u8 rt_family, u32 nh_tclassid);
6601da177e4SLinus Torvalds #endif /* _NET_FIB_H */
661