blob: a6ce6ea856b948a30d029ed609e52ab45ae7ec10 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +090031#define RT6_LOOKUP_F_IFACE 0x00000001
32#define RT6_LOOKUP_F_REACHABLE 0x00000002
33#define RT6_LOOKUP_F_HAS_SADDR 0x00000004
34#define RT6_LOOKUP_F_SRCPREF_TMP 0x00000008
35#define RT6_LOOKUP_F_SRCPREF_PUBLIC 0x00000010
36#define RT6_LOOKUP_F_SRCPREF_COA 0x00000020
David Ahernd5d32e42016-10-24 12:27:23 -070037#define RT6_LOOKUP_F_IGNORE_LINKSTATE 0x00000040
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -070038
Eric Dumazet30f78d82014-04-10 21:23:36 -070039/* We do not (yet ?) support IPv6 jumbograms (RFC 2675)
40 * Unlike IPv4, hdr->seg_len doesn't include the IPv6 header
41 */
42#define IP6_MAX_MTU (0xFFFF + sizeof(struct ipv6hdr))
43
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +000044/*
45 * rt6_srcprefs2flags() and rt6_flags2srcprefs() translate
46 * between IPV6_ADDR_PREFERENCES socket option values
47 * IPV6_PREFER_SRC_TMP = 0x1
48 * IPV6_PREFER_SRC_PUBLIC = 0x2
49 * IPV6_PREFER_SRC_COA = 0x4
50 * and above RT6_LOOKUP_F_SRCPREF_xxx flags.
51 */
52static inline int rt6_srcprefs2flags(unsigned int srcprefs)
53{
54 /* No need to bitmask because srcprefs have only 3 bits. */
55 return srcprefs << 3;
56}
57
58static inline unsigned int rt6_flags2srcprefs(int flags)
59{
60 return (flags >> 3) & 7;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Wang Yufen60ea37f2014-03-29 09:27:30 +080063static inline bool rt6_need_strict(const struct in6_addr *daddr)
64{
65 return ipv6_addr_type(daddr) &
66 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
67}
David S. Millerfbfe95a2012-06-08 23:24:18 -070068
David Ahern33bd5ac2018-07-03 14:36:21 -070069static inline bool rt6_qualify_for_ecmp(const struct fib6_info *f6i)
70{
David Ahernbe659b82019-04-21 17:39:18 -070071 /* the RTF_ADDRCONF flag filters out RA's */
72 return !(f6i->fib6_flags & RTF_ADDRCONF) &&
David Ahern1cf844c2019-05-22 20:27:59 -070073 f6i->fib6_nh->fib_nh_gw_family;
David Ahern33bd5ac2018-07-03 14:36:21 -070074}
75
Joe Perches5c3a0fd2013-09-21 10:22:42 -070076void ip6_route_input(struct sk_buff *skb);
Mahesh Bandeward409b842016-09-16 12:59:08 -070077struct dst_entry *ip6_route_input_lookup(struct net *net,
78 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -080079 struct flowi6 *fl6,
80 const struct sk_buff *skb, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Paolo Abeni6f21c962016-01-29 12:30:19 +010082struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
83 struct flowi6 *fl6, int flags);
84
85static inline struct dst_entry *ip6_route_output(struct net *net,
86 const struct sock *sk,
87 struct flowi6 *fl6)
88{
89 return ip6_route_output_flags(net, sk, fl6, 0);
90}
91
Joe Perches5c3a0fd2013-09-21 10:22:42 -070092struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -080093 const struct sk_buff *skb, int flags);
David Ahern9ff74382016-06-13 13:44:19 -070094struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -080095 int ifindex, struct flowi6 *fl6,
96 const struct sk_buff *skb, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
WANG Cong2f460932017-05-03 22:07:31 -070098void ip6_route_init_special_entries(void);
Joe Perches5c3a0fd2013-09-21 10:22:42 -070099int ip6_route_init(void);
100void ip6_route_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700102int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David Ahernacb54e32018-04-17 17:33:22 -0700104int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
105 struct netlink_ext_ack *extack);
David Ahern93c2fb22018-04-18 15:38:59 -0700106int ip6_ins_rt(struct net *net, struct fib6_info *f6i);
107int ip6_del_rt(struct net *net, struct fib6_info *f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
David Ahern93c2fb22018-04-18 15:38:59 -0700109void rt6_flush_exceptions(struct fib6_info *f6i);
110void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
Wei Wangc757faa2017-10-06 12:06:01 -0700111 unsigned long now);
Wei Wang35732d02017-10-06 12:05:57 -0700112
David Ahern93c2fb22018-04-18 15:38:59 -0700113static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
David Aherna2e2ff52016-06-16 16:24:24 -0700114 const struct in6_addr *daddr,
115 unsigned int prefs,
116 struct in6_addr *saddr)
117{
David Aherna2e2ff52016-06-16 16:24:24 -0700118 int err = 0;
119
David Ahern6fe74942018-04-18 15:39:03 -0700120 if (f6i && f6i->fib6_prefsrc.plen) {
David Ahern93c2fb22018-04-18 15:38:59 -0700121 *saddr = f6i->fib6_prefsrc.addr;
David Ahern6fe74942018-04-18 15:39:03 -0700122 } else {
123 struct net_device *dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
124
125 err = ipv6_dev_get_saddr(net, dev, daddr, prefs, saddr);
126 }
David Aherna2e2ff52016-06-16 16:24:24 -0700127
128 return err;
129}
Daniel Walterc3968a82011-04-13 21:10:57 +0000130
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700131struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -0800132 const struct in6_addr *saddr, int oif,
133 const struct sk_buff *skb, int flags);
David Ahernb4bac172018-03-02 08:32:18 -0800134u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
135 const struct sk_buff *skb, struct flow_keys *hkeys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700137struct dst_entry *icmp6_dst_alloc(struct net_device *dev, struct flowi6 *fl6);
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -0800138
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700139void fib6_force_start_gc(struct net *net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
David Ahern360a9882018-04-18 15:39:00 -0700141struct fib6_info *addrconf_f6i_alloc(struct net *net, struct inet6_dev *idev,
David Ahern8d1c8022018-04-17 17:33:26 -0700142 const struct in6_addr *addr, bool anycast,
143 gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Ahern9ab179d2016-04-07 11:10:06 -0700145struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
146 int flags);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * support functions for ND
150 *
151 */
David Ahern8d1c8022018-04-17 17:33:26 -0700152struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -0700153 const struct in6_addr *addr,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700154 struct net_device *dev);
David Ahern8d1c8022018-04-17 17:33:26 -0700155struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -0700156 const struct in6_addr *gwaddr,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700157 struct net_device *dev, unsigned int pref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700159void rt6_purge_dflt_routers(struct net *net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700161int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
162 const struct in6_addr *gwaddr);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800163
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700164void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, int oif,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900165 u32 mark, kuid_t uid);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700166void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900167void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
168 kuid_t uid);
Maciej Żenczykowskid4563362018-09-29 23:44:50 -0700169void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700170void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172struct netlink_callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000174struct rt6_rtnl_dump_arg {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700175 struct sk_buff *skb;
176 struct netlink_callback *cb;
Brian Haley191cd582008-08-14 15:33:21 -0700177 struct net *net;
David Ahern47246762018-10-15 18:56:42 -0700178 struct fib_dump_filter filter;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700179};
180
David Ahern93c2fb22018-04-18 15:38:59 -0700181int rt6_dump_route(struct fib6_info *f6i, void *p_arg);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700182void rt6_mtu_change(struct net_device *dev, unsigned int mtu);
183void rt6_remove_prefsrc(struct inet6_ifaddr *ifp);
Duan Jiongbe7a0102014-05-15 15:56:14 +0800184void rt6_clean_tohost(struct net *net, struct in6_addr *gateway);
David Ahernecc56632019-04-23 08:48:09 -0700185void rt6_sync_up(struct net_device *dev, unsigned char nh_flags);
Ido Schimmel4c981e22018-01-07 12:45:04 +0200186void rt6_disable_ip(struct net_device *dev, unsigned long event);
Ido Schimmel27c6fa72018-01-07 12:45:05 +0200187void rt6_sync_down_dev(struct net_device *dev, unsigned long event);
David Ahern93c2fb22018-04-18 15:38:59 -0700188void rt6_multipath_rebalance(struct fib6_info *f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Xin Long510c3212018-02-14 19:06:02 +0800190void rt6_uncached_list_add(struct rt6_info *rt);
191void rt6_uncached_list_del(struct rt6_info *rt);
192
David Ahern1b70d7922017-08-28 13:53:34 -0700193static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
194{
195 const struct dst_entry *dst = skb_dst(skb);
196 const struct rt6_info *rt6 = NULL;
197
198 if (dst)
199 rt6 = container_of(dst, struct rt6_info, dst);
200
201 return rt6;
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/*
205 * Store a destination cache entry in a socket
206 */
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800207static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,
208 const struct in6_addr *daddr,
209 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800213 np->dst_cookie = rt6_get_cookie((struct rt6_info *)dst);
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700214 sk_setup_caps(sk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 np->daddr_cache = daddr;
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700216#ifdef CONFIG_IPV6_SUBTREES
217 np->saddr_cache = saddr;
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Alexey Kodanev7d6850f2018-04-03 15:00:07 +0300221void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
222 const struct flowi6 *fl6);
223
Eric Dumazeta50feda2012-05-18 18:57:34 +0000224static inline bool ipv6_unicast_destination(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Eric Dumazetadf30902009-06-02 05:19:30 +0000226 struct rt6_info *rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return rt->rt6i_flags & RTF_LOCAL;
229}
230
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700231static inline bool ipv6_anycast_destination(const struct dst_entry *dst,
232 const struct in6_addr *daddr)
FX Le Bail509aba32014-01-07 14:57:27 +0100233{
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700234 struct rt6_info *rt = (struct rt6_info *)dst;
FX Le Bail509aba32014-01-07 14:57:27 +0100235
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700236 return rt->rt6i_flags & RTF_ANYCAST ||
Vincent Bernatccdb2d12017-07-15 19:40:20 +0200237 (rt->rt6i_dst.plen < 127 &&
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700238 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr));
FX Le Bail509aba32014-01-07 14:57:27 +0100239}
240
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500241int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
242 int (*output)(struct net *, struct sock *, struct sk_buff *));
David Stevensad0081e2010-12-17 11:42:42 +0000243
244static inline int ip6_skb_dst_mtu(struct sk_buff *skb)
245{
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200246 struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
247 inet6_sk(skb->sk) : NULL;
David Stevensad0081e2010-12-17 11:42:42 +0000248
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100249 return (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) ?
David Stevensad0081e2010-12-17 11:42:42 +0000250 skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
251}
252
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100253static inline bool ip6_sk_accept_pmtu(const struct sock *sk)
254{
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100255 return inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_INTERFACE &&
256 inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_OMIT;
257}
258
WANG Cong60ff7462014-05-04 16:39:18 -0700259static inline bool ip6_sk_ignore_df(const struct sock *sk)
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100260{
261 return inet6_sk(sk)->pmtudisc < IPV6_PMTUDISC_DO ||
262 inet6_sk(sk)->pmtudisc == IPV6_PMTUDISC_OMIT;
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100263}
264
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700265static inline struct in6_addr *rt6_nexthop(struct rt6_info *rt,
266 struct in6_addr *daddr)
YOSHIFUJI Hideaki / 吉藤英明9bb5a142013-01-17 12:53:48 +0000267{
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700268 if (rt->rt6i_flags & RTF_GATEWAY)
269 return &rt->rt6i_gateway;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700270 else if (unlikely(rt->rt6i_flags & RTF_CACHE))
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700271 return &rt->rt6i_dst.addr;
272 else
273 return daddr;
YOSHIFUJI Hideaki / 吉藤英明9bb5a142013-01-17 12:53:48 +0000274}
275
David Ahern8d1c8022018-04-17 17:33:26 -0700276static inline bool rt6_duplicate_nexthop(struct fib6_info *a, struct fib6_info *b)
David Ahernf06b7542017-07-05 14:41:46 -0600277{
David Ahern1cf844c2019-05-22 20:27:59 -0700278 struct fib6_nh *nha = a->fib6_nh, *nhb = b->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -0700279
280 return nha->fib_nh_dev == nhb->fib_nh_dev &&
281 ipv6_addr_equal(&nha->fib_nh_gw6, &nhb->fib_nh_gw6) &&
282 !lwtunnel_cmp_encap(nha->fib_nh_lws, nhb->fib_nh_lws);
David Ahernf06b7542017-07-05 14:41:46 -0600283}
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -0500284
Felix Fietkau07cb9622018-02-26 10:15:10 +0100285static inline unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
286{
287 struct inet6_dev *idev;
288 unsigned int mtu;
289
290 if (dst_metric_locked(dst, RTAX_MTU)) {
291 mtu = dst_metric_raw(dst, RTAX_MTU);
292 if (mtu)
293 return mtu;
294 }
295
296 mtu = IPV6_MIN_MTU;
297 rcu_read_lock();
298 idev = __in6_dev_get(dst->dev);
299 if (idev)
300 mtu = idev->cnf.mtu6;
301 rcu_read_unlock();
302
303 return mtu;
304}
305
David Ahernb748f262019-04-16 14:36:06 -0700306u32 ip6_mtu_from_fib6(const struct fib6_result *res,
307 const struct in6_addr *daddr,
308 const struct in6_addr *saddr);
David Ahern901731b2018-05-21 09:08:14 -0700309
David Ahernf8a1b432018-04-17 17:33:21 -0700310struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
311 struct net_device *dev, struct sk_buff *skb,
312 const void *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif