blob: 82bced2fc1e3103c0a429ed6f6620168665c36a1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _NET_IP6_ROUTE_H
3#define _NET_IP6_ROUTE_H
4
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08005struct route_info {
6 __u8 type;
7 __u8 length;
8 __u8 prefix_len;
9#if defined(__BIG_ENDIAN_BITFIELD)
10 __u8 reserved_h:3,
11 route_pref:2,
12 reserved_l:3;
13#elif defined(__LITTLE_ENDIAN_BITFIELD)
14 __u8 reserved_l:3,
15 route_pref:2,
16 reserved_h:3;
17#endif
Al Viroe69a4ad2006-11-14 20:56:00 -080018 __be32 lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080019 __u8 prefix[0]; /* 0,8 or 16 */
20};
21
David Aherna2e2ff52016-06-16 16:24:24 -070022#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/flow.h>
24#include <net/ip6_fib.h>
25#include <net/sock.h>
David Ahernf06b7542017-07-05 14:41:46 -060026#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/ip.h>
28#include <linux/ipv6.h>
YOSHIFUJI Hideaki / 吉藤英明9bb5a142013-01-17 12:53:48 +000029#include <linux/route.h>
David Ahernf88d8ea2019-06-03 20:19:52 -070030#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +090032#define RT6_LOOKUP_F_IFACE 0x00000001
33#define RT6_LOOKUP_F_REACHABLE 0x00000002
34#define RT6_LOOKUP_F_HAS_SADDR 0x00000004
35#define RT6_LOOKUP_F_SRCPREF_TMP 0x00000008
36#define RT6_LOOKUP_F_SRCPREF_PUBLIC 0x00000010
37#define RT6_LOOKUP_F_SRCPREF_COA 0x00000020
David Ahernd5d32e42016-10-24 12:27:23 -070038#define RT6_LOOKUP_F_IGNORE_LINKSTATE 0x00000040
Wei Wang0e09edc2019-06-20 17:36:37 -070039#define RT6_LOOKUP_F_DST_NOREF 0x00000080
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -070040
Eric Dumazet30f78d82014-04-10 21:23:36 -070041/* We do not (yet ?) support IPv6 jumbograms (RFC 2675)
42 * Unlike IPv4, hdr->seg_len doesn't include the IPv6 header
43 */
44#define IP6_MAX_MTU (0xFFFF + sizeof(struct ipv6hdr))
45
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +000046/*
47 * rt6_srcprefs2flags() and rt6_flags2srcprefs() translate
48 * between IPV6_ADDR_PREFERENCES socket option values
49 * IPV6_PREFER_SRC_TMP = 0x1
50 * IPV6_PREFER_SRC_PUBLIC = 0x2
51 * IPV6_PREFER_SRC_COA = 0x4
52 * and above RT6_LOOKUP_F_SRCPREF_xxx flags.
53 */
54static inline int rt6_srcprefs2flags(unsigned int srcprefs)
55{
56 /* No need to bitmask because srcprefs have only 3 bits. */
57 return srcprefs << 3;
58}
59
60static inline unsigned int rt6_flags2srcprefs(int flags)
61{
62 return (flags >> 3) & 7;
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Wang Yufen60ea37f2014-03-29 09:27:30 +080065static inline bool rt6_need_strict(const struct in6_addr *daddr)
66{
67 return ipv6_addr_type(daddr) &
68 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
69}
David S. Millerfbfe95a2012-06-08 23:24:18 -070070
David Ahernf88d8ea2019-06-03 20:19:52 -070071/* fib entries using a nexthop object can not be coalesced into
72 * a multipath route
73 */
David Ahern33bd5ac2018-07-03 14:36:21 -070074static inline bool rt6_qualify_for_ecmp(const struct fib6_info *f6i)
75{
David Ahernbe659b82019-04-21 17:39:18 -070076 /* the RTF_ADDRCONF flag filters out RA's */
David Ahernf88d8ea2019-06-03 20:19:52 -070077 return !(f6i->fib6_flags & RTF_ADDRCONF) && !f6i->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -070078 f6i->fib6_nh->fib_nh_gw_family;
David Ahern33bd5ac2018-07-03 14:36:21 -070079}
80
Joe Perches5c3a0fd2013-09-21 10:22:42 -070081void ip6_route_input(struct sk_buff *skb);
Mahesh Bandeward409b842016-09-16 12:59:08 -070082struct dst_entry *ip6_route_input_lookup(struct net *net,
83 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -080084 struct flowi6 *fl6,
85 const struct sk_buff *skb, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Paolo Abeni6f21c962016-01-29 12:30:19 +010087struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
88 struct flowi6 *fl6, int flags);
89
90static inline struct dst_entry *ip6_route_output(struct net *net,
91 const struct sock *sk,
92 struct flowi6 *fl6)
93{
94 return ip6_route_output_flags(net, sk, fl6, 0);
95}
96
Joe Perches5c3a0fd2013-09-21 10:22:42 -070097struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -080098 const struct sk_buff *skb, int flags);
David Ahern9ff74382016-06-13 13:44:19 -070099struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -0800100 int ifindex, struct flowi6 *fl6,
101 const struct sk_buff *skb, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
WANG Cong2f460932017-05-03 22:07:31 -0700103void ip6_route_init_special_entries(void);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700104int ip6_route_init(void);
105void ip6_route_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700107int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
David Ahernacb54e32018-04-17 17:33:22 -0700109int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
110 struct netlink_ext_ack *extack);
David Ahern93c2fb22018-04-18 15:38:59 -0700111int ip6_ins_rt(struct net *net, struct fib6_info *f6i);
112int ip6_del_rt(struct net *net, struct fib6_info *f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Ahern93c2fb22018-04-18 15:38:59 -0700114void rt6_flush_exceptions(struct fib6_info *f6i);
115void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
Wei Wangc757faa2017-10-06 12:06:01 -0700116 unsigned long now);
Wei Wang35732d02017-10-06 12:05:57 -0700117
David Ahern93c2fb22018-04-18 15:38:59 -0700118static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
David Aherna2e2ff52016-06-16 16:24:24 -0700119 const struct in6_addr *daddr,
120 unsigned int prefs,
121 struct in6_addr *saddr)
122{
David Aherna2e2ff52016-06-16 16:24:24 -0700123 int err = 0;
124
David Ahern6fe74942018-04-18 15:39:03 -0700125 if (f6i && f6i->fib6_prefsrc.plen) {
David Ahern93c2fb22018-04-18 15:38:59 -0700126 *saddr = f6i->fib6_prefsrc.addr;
David Ahern6fe74942018-04-18 15:39:03 -0700127 } else {
128 struct net_device *dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
129
130 err = ipv6_dev_get_saddr(net, dev, daddr, prefs, saddr);
131 }
David Aherna2e2ff52016-06-16 16:24:24 -0700132
133 return err;
134}
Daniel Walterc3968a82011-04-13 21:10:57 +0000135
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700136struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -0800137 const struct in6_addr *saddr, int oif,
138 const struct sk_buff *skb, int flags);
David Ahernb4bac172018-03-02 08:32:18 -0800139u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
140 const struct sk_buff *skb, struct flow_keys *hkeys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700142struct dst_entry *icmp6_dst_alloc(struct net_device *dev, struct flowi6 *fl6);
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800143
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700144void fib6_force_start_gc(struct net *net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David Ahern360a9882018-04-18 15:39:00 -0700146struct fib6_info *addrconf_f6i_alloc(struct net *net, struct inet6_dev *idev,
David Ahern8d1c8022018-04-17 17:33:26 -0700147 const struct in6_addr *addr, bool anycast,
148 gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David Ahern9ab179d2016-04-07 11:10:06 -0700150struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
151 int flags);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * support functions for ND
155 *
156 */
David Ahern8d1c8022018-04-17 17:33:26 -0700157struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -0700158 const struct in6_addr *addr,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700159 struct net_device *dev);
David Ahern8d1c8022018-04-17 17:33:26 -0700160struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -0700161 const struct in6_addr *gwaddr,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700162 struct net_device *dev, unsigned int pref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700164void rt6_purge_dflt_routers(struct net *net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700166int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
167 const struct in6_addr *gwaddr);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800168
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700169void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, int oif,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900170 u32 mark, kuid_t uid);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700171void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900172void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
173 kuid_t uid);
Maciej Żenczykowskid4563362018-09-29 23:44:50 -0700174void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700175void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct netlink_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000179struct rt6_rtnl_dump_arg {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700180 struct sk_buff *skb;
181 struct netlink_callback *cb;
Brian Haley191cd582008-08-14 15:33:21 -0700182 struct net *net;
David Ahern47246762018-10-15 18:56:42 -0700183 struct fib_dump_filter filter;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700184};
185
David Ahern93c2fb22018-04-18 15:38:59 -0700186int rt6_dump_route(struct fib6_info *f6i, void *p_arg);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700187void rt6_mtu_change(struct net_device *dev, unsigned int mtu);
188void rt6_remove_prefsrc(struct inet6_ifaddr *ifp);
Duan Jiongbe7a0102014-05-15 15:56:14 +0800189void rt6_clean_tohost(struct net *net, struct in6_addr *gateway);
David Ahernecc56632019-04-23 08:48:09 -0700190void rt6_sync_up(struct net_device *dev, unsigned char nh_flags);
Ido Schimmel4c981e22018-01-07 12:45:04 +0200191void rt6_disable_ip(struct net_device *dev, unsigned long event);
Ido Schimmel27c6fa72018-01-07 12:45:05 +0200192void rt6_sync_down_dev(struct net_device *dev, unsigned long event);
David Ahern93c2fb22018-04-18 15:38:59 -0700193void rt6_multipath_rebalance(struct fib6_info *f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Xin Long510c3212018-02-14 19:06:02 +0800195void rt6_uncached_list_add(struct rt6_info *rt);
196void rt6_uncached_list_del(struct rt6_info *rt);
197
David Ahern1b70d7922017-08-28 13:53:34 -0700198static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
199{
200 const struct dst_entry *dst = skb_dst(skb);
201 const struct rt6_info *rt6 = NULL;
202
203 if (dst)
204 rt6 = container_of(dst, struct rt6_info, dst);
205
206 return rt6;
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * Store a destination cache entry in a socket
211 */
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800212static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,
213 const struct in6_addr *daddr,
214 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800218 np->dst_cookie = rt6_get_cookie((struct rt6_info *)dst);
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700219 sk_setup_caps(sk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 np->daddr_cache = daddr;
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700221#ifdef CONFIG_IPV6_SUBTREES
222 np->saddr_cache = saddr;
223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Alexey Kodanev7d6850f2018-04-03 15:00:07 +0300226void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
227 const struct flowi6 *fl6);
228
Eric Dumazeta50feda2012-05-18 18:57:34 +0000229static inline bool ipv6_unicast_destination(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Eric Dumazetadf30902009-06-02 05:19:30 +0000231 struct rt6_info *rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return rt->rt6i_flags & RTF_LOCAL;
234}
235
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700236static inline bool ipv6_anycast_destination(const struct dst_entry *dst,
237 const struct in6_addr *daddr)
FX Le Bail509aba32014-01-07 14:57:27 +0100238{
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700239 struct rt6_info *rt = (struct rt6_info *)dst;
FX Le Bail509aba32014-01-07 14:57:27 +0100240
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700241 return rt->rt6i_flags & RTF_ANYCAST ||
Vincent Bernatccdb2d12017-07-15 19:40:20 +0200242 (rt->rt6i_dst.plen < 127 &&
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700243 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr));
FX Le Bail509aba32014-01-07 14:57:27 +0100244}
245
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500246int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
247 int (*output)(struct net *, struct sock *, struct sk_buff *));
David Stevensad0081e2010-12-17 11:42:42 +0000248
249static inline int ip6_skb_dst_mtu(struct sk_buff *skb)
250{
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200251 struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
252 inet6_sk(skb->sk) : NULL;
David Stevensad0081e2010-12-17 11:42:42 +0000253
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100254 return (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) ?
David Stevensad0081e2010-12-17 11:42:42 +0000255 skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
256}
257
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100258static inline bool ip6_sk_accept_pmtu(const struct sock *sk)
259{
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100260 return inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_INTERFACE &&
261 inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_OMIT;
262}
263
WANG Cong60ff7462014-05-04 16:39:18 -0700264static inline bool ip6_sk_ignore_df(const struct sock *sk)
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100265{
266 return inet6_sk(sk)->pmtudisc < IPV6_PMTUDISC_DO ||
267 inet6_sk(sk)->pmtudisc == IPV6_PMTUDISC_OMIT;
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100268}
269
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700270static inline struct in6_addr *rt6_nexthop(struct rt6_info *rt,
271 struct in6_addr *daddr)
YOSHIFUJI Hideaki / 吉藤英明9bb5a142013-01-17 12:53:48 +0000272{
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700273 if (rt->rt6i_flags & RTF_GATEWAY)
274 return &rt->rt6i_gateway;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700275 else if (unlikely(rt->rt6i_flags & RTF_CACHE))
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700276 return &rt->rt6i_dst.addr;
277 else
278 return daddr;
YOSHIFUJI Hideaki / 吉藤英明9bb5a142013-01-17 12:53:48 +0000279}
280
David Ahern8d1c8022018-04-17 17:33:26 -0700281static inline bool rt6_duplicate_nexthop(struct fib6_info *a, struct fib6_info *b)
David Ahernf06b7542017-07-05 14:41:46 -0600282{
David Ahernf88d8ea2019-06-03 20:19:52 -0700283 struct fib6_nh *nha, *nhb;
David Ahernad1601a2019-03-27 20:53:56 -0700284
David Ahernf88d8ea2019-06-03 20:19:52 -0700285 if (a->nh || b->nh)
286 return nexthop_cmp(a->nh, b->nh);
287
288 nha = a->fib6_nh;
289 nhb = b->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -0700290 return nha->fib_nh_dev == nhb->fib_nh_dev &&
291 ipv6_addr_equal(&nha->fib_nh_gw6, &nhb->fib_nh_gw6) &&
292 !lwtunnel_cmp_encap(nha->fib_nh_lws, nhb->fib_nh_lws);
David Ahernf06b7542017-07-05 14:41:46 -0600293}
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -0500294
Felix Fietkau07cb9622018-02-26 10:15:10 +0100295static inline unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
296{
297 struct inet6_dev *idev;
298 unsigned int mtu;
299
300 if (dst_metric_locked(dst, RTAX_MTU)) {
301 mtu = dst_metric_raw(dst, RTAX_MTU);
302 if (mtu)
303 return mtu;
304 }
305
306 mtu = IPV6_MIN_MTU;
307 rcu_read_lock();
308 idev = __in6_dev_get(dst->dev);
309 if (idev)
310 mtu = idev->cnf.mtu6;
311 rcu_read_unlock();
312
313 return mtu;
314}
315
David Ahernb748f262019-04-16 14:36:06 -0700316u32 ip6_mtu_from_fib6(const struct fib6_result *res,
317 const struct in6_addr *daddr,
318 const struct in6_addr *saddr);
David Ahern901731b2018-05-21 09:08:14 -0700319
David Ahernf8a1b432018-04-17 17:33:21 -0700320struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
321 struct net_device *dev, struct sk_buff *skb,
322 const void *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif