xref: /openbmc/linux/include/net/ip6_route.h (revision 5e5d6fed374155ba1a7a5ca5f12fbec2285d06a2)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _NET_IP6_ROUTE_H
31da177e4SLinus Torvalds #define _NET_IP6_ROUTE_H
41da177e4SLinus Torvalds 
570ceb4f5SYOSHIFUJI Hideaki struct route_info {
670ceb4f5SYOSHIFUJI Hideaki 	__u8			type;
770ceb4f5SYOSHIFUJI Hideaki 	__u8			length;
870ceb4f5SYOSHIFUJI Hideaki 	__u8			prefix_len;
970ceb4f5SYOSHIFUJI Hideaki #if defined(__BIG_ENDIAN_BITFIELD)
1070ceb4f5SYOSHIFUJI Hideaki 	__u8			reserved_h:3,
1170ceb4f5SYOSHIFUJI Hideaki 				route_pref:2,
1270ceb4f5SYOSHIFUJI Hideaki 				reserved_l:3;
1370ceb4f5SYOSHIFUJI Hideaki #elif defined(__LITTLE_ENDIAN_BITFIELD)
1470ceb4f5SYOSHIFUJI Hideaki 	__u8			reserved_l:3,
1570ceb4f5SYOSHIFUJI Hideaki 				route_pref:2,
1670ceb4f5SYOSHIFUJI Hideaki 				reserved_h:3;
1770ceb4f5SYOSHIFUJI Hideaki #endif
18e69a4adcSAl Viro 	__be32			lifetime;
1970ceb4f5SYOSHIFUJI Hideaki 	__u8			prefix[0];	/* 0,8 or 16 */
2070ceb4f5SYOSHIFUJI Hideaki };
2170ceb4f5SYOSHIFUJI Hideaki 
22a2e2ff56SDavid Ahern #include <net/addrconf.h>
231da177e4SLinus Torvalds #include <net/flow.h>
241da177e4SLinus Torvalds #include <net/ip6_fib.h>
251da177e4SLinus Torvalds #include <net/sock.h>
26f06b7549SDavid Ahern #include <net/lwtunnel.h>
271da177e4SLinus Torvalds #include <linux/ip.h>
281da177e4SLinus Torvalds #include <linux/ipv6.h>
299bb5a148SYOSHIFUJI Hideaki / 吉藤英明 #include <linux/route.h>
301da177e4SLinus Torvalds 
317cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_IFACE		0x00000001
327cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_REACHABLE		0x00000002
337cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_HAS_SADDR		0x00000004
347cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_SRCPREF_TMP	0x00000008
357cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_SRCPREF_PUBLIC	0x00000010
367cbca67cSYOSHIFUJI Hideaki #define RT6_LOOKUP_F_SRCPREF_COA	0x00000020
37d5d32e4bSDavid Ahern #define RT6_LOOKUP_F_IGNORE_LINKSTATE	0x00000040
3877d16f45SYOSHIFUJI Hideaki 
3930f78d8eSEric Dumazet /* We do not (yet ?) support IPv6 jumbograms (RFC 2675)
4030f78d8eSEric Dumazet  * Unlike IPv4, hdr->seg_len doesn't include the IPv6 header
4130f78d8eSEric Dumazet  */
4230f78d8eSEric Dumazet #define IP6_MAX_MTU (0xFFFF + sizeof(struct ipv6hdr))
4330f78d8eSEric Dumazet 
440c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 /*
450c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  * rt6_srcprefs2flags() and rt6_flags2srcprefs() translate
460c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  * between IPV6_ADDR_PREFERENCES socket option values
470c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  *	IPV6_PREFER_SRC_TMP    = 0x1
480c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  *	IPV6_PREFER_SRC_PUBLIC = 0x2
490c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  *	IPV6_PREFER_SRC_COA    = 0x4
500c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  * and above RT6_LOOKUP_F_SRCPREF_xxx flags.
510c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明  */
520c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 static inline int rt6_srcprefs2flags(unsigned int srcprefs)
530c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 {
540c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	/* No need to bitmask because srcprefs have only 3 bits. */
550c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	return srcprefs << 3;
560c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 }
570c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 
580c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 static inline unsigned int rt6_flags2srcprefs(int flags)
590c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 {
600c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	return (flags >> 3) & 7;
610c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 }
621da177e4SLinus Torvalds 
6360ea37f7SWang Yufen static inline bool rt6_need_strict(const struct in6_addr *daddr)
6460ea37f7SWang Yufen {
6560ea37f7SWang Yufen 	return ipv6_addr_type(daddr) &
6660ea37f7SWang Yufen 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
6760ea37f7SWang Yufen }
68fbfe95a4SDavid S. Miller 
69d7dedee1SIdo Schimmel static inline bool rt6_qualify_for_ecmp(const struct rt6_info *rt)
70d7dedee1SIdo Schimmel {
71d7dedee1SIdo Schimmel 	return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
72d7dedee1SIdo Schimmel 	       RTF_GATEWAY;
73d7dedee1SIdo Schimmel }
74d7dedee1SIdo Schimmel 
755c3a0fd7SJoe Perches void ip6_route_input(struct sk_buff *skb);
76d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
77d409b847SMahesh Bandewar 					 struct net_device *dev,
78d409b847SMahesh Bandewar 					 struct flowi6 *fl6, int flags);
791da177e4SLinus Torvalds 
806f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
816f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags);
826f21c96aSPaolo Abeni 
836f21c96aSPaolo Abeni static inline struct dst_entry *ip6_route_output(struct net *net,
846f21c96aSPaolo Abeni 						 const struct sock *sk,
856f21c96aSPaolo Abeni 						 struct flowi6 *fl6)
866f21c96aSPaolo Abeni {
876f21c96aSPaolo Abeni 	return ip6_route_output_flags(net, sk, fl6, 0);
886f21c96aSPaolo Abeni }
896f21c96aSPaolo Abeni 
905c3a0fd7SJoe Perches struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
915c3a0fd7SJoe Perches 				   int flags);
929ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
939ff74384SDavid Ahern 			       int ifindex, struct flowi6 *fl6, int flags);
941da177e4SLinus Torvalds 
952f460933SWANG Cong void ip6_route_init_special_entries(void);
965c3a0fd7SJoe Perches int ip6_route_init(void);
975c3a0fd7SJoe Perches void ip6_route_cleanup(void);
981da177e4SLinus Torvalds 
995c3a0fd7SJoe Perches int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg);
1001da177e4SLinus Torvalds 
101333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, struct netlink_ext_ack *extack);
1025c3a0fd7SJoe Perches int ip6_ins_rt(struct rt6_info *);
1035c3a0fd7SJoe Perches int ip6_del_rt(struct rt6_info *);
1041da177e4SLinus Torvalds 
10535732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt);
10635732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt);
107c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, struct fib6_gc_args *gc_args,
108c757faa8SWei Wang 			unsigned long now);
10935732d01SWei Wang 
110a2e2ff56SDavid Ahern static inline int ip6_route_get_saddr(struct net *net, struct rt6_info *rt,
111a2e2ff56SDavid Ahern 				      const struct in6_addr *daddr,
112a2e2ff56SDavid Ahern 				      unsigned int prefs,
113a2e2ff56SDavid Ahern 				      struct in6_addr *saddr)
114a2e2ff56SDavid Ahern {
115a2e2ff56SDavid Ahern 	struct inet6_dev *idev =
116a2e2ff56SDavid Ahern 			rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
117a2e2ff56SDavid Ahern 	int err = 0;
118a2e2ff56SDavid Ahern 
119a2e2ff56SDavid Ahern 	if (rt && rt->rt6i_prefsrc.plen)
120a2e2ff56SDavid Ahern 		*saddr = rt->rt6i_prefsrc.addr;
121a2e2ff56SDavid Ahern 	else
122a2e2ff56SDavid Ahern 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
123a2e2ff56SDavid Ahern 					 daddr, prefs, saddr);
124a2e2ff56SDavid Ahern 
125a2e2ff56SDavid Ahern 	return err;
126a2e2ff56SDavid Ahern }
127c3968a85SDaniel Walter 
1285c3a0fd7SJoe Perches struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1295c3a0fd7SJoe Perches 			    const struct in6_addr *saddr, int oif, int flags);
130*5e5d6fedSRoopa Prabhu u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb,
131*5e5d6fedSRoopa Prabhu 		       struct flow_keys *hkeys);
1321da177e4SLinus Torvalds 
1335c3a0fd7SJoe Perches struct dst_entry *icmp6_dst_alloc(struct net_device *dev, struct flowi6 *fl6);
1343b00944cSYOSHIFUJI Hideaki 
1355c3a0fd7SJoe Perches void fib6_force_start_gc(struct net *net);
1361da177e4SLinus Torvalds 
1375c3a0fd7SJoe Perches struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1385c3a0fd7SJoe Perches 				    const struct in6_addr *addr, bool anycast);
1391da177e4SLinus Torvalds 
1409ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
1419ab179d8SDavid Ahern 			       int flags);
1429ab179d8SDavid Ahern 
1431da177e4SLinus Torvalds /*
1441da177e4SLinus Torvalds  *	support functions for ND
1451da177e4SLinus Torvalds  *
1461da177e4SLinus Torvalds  */
1475c3a0fd7SJoe Perches struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr,
1481da177e4SLinus Torvalds 				     struct net_device *dev);
1495c3a0fd7SJoe Perches struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1505c3a0fd7SJoe Perches 				     struct net_device *dev, unsigned int pref);
1511da177e4SLinus Torvalds 
1525c3a0fd7SJoe Perches void rt6_purge_dflt_routers(struct net *net);
1531da177e4SLinus Torvalds 
1545c3a0fd7SJoe Perches int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
155b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr);
15670ceb4f5SYOSHIFUJI Hideaki 
1575c3a0fd7SJoe Perches void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, int oif,
158e2d118a1SLorenzo Colitti 		     u32 mark, kuid_t uid);
1595c3a0fd7SJoe Perches void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu);
160e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
161e2d118a1SLorenzo Colitti 		  kuid_t uid);
1625c3a0fd7SJoe Perches void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1635c3a0fd7SJoe Perches 			    u32 mark);
1645c3a0fd7SJoe Perches void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds struct netlink_callback;
1671da177e4SLinus Torvalds 
168fd2c3ef7SEric Dumazet struct rt6_rtnl_dump_arg {
1691b43af54SPatrick McHardy 	struct sk_buff *skb;
1701b43af54SPatrick McHardy 	struct netlink_callback *cb;
171191cd582SBrian Haley 	struct net *net;
1721b43af54SPatrick McHardy };
1731b43af54SPatrick McHardy 
1745c3a0fd7SJoe Perches int rt6_dump_route(struct rt6_info *rt, void *p_arg);
1755c3a0fd7SJoe Perches void rt6_mtu_change(struct net_device *dev, unsigned int mtu);
1765c3a0fd7SJoe Perches void rt6_remove_prefsrc(struct inet6_ifaddr *ifp);
177be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway);
1782127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags);
1794c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event);
18027c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event);
181d7dedee1SIdo Schimmel void rt6_multipath_rebalance(struct rt6_info *rt);
1821da177e4SLinus Torvalds 
1831b70d792SDavid Ahern static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
1841b70d792SDavid Ahern {
1851b70d792SDavid Ahern 	const struct dst_entry *dst = skb_dst(skb);
1861b70d792SDavid Ahern 	const struct rt6_info *rt6 = NULL;
1871b70d792SDavid Ahern 
1881b70d792SDavid Ahern 	if (dst)
1891b70d792SDavid Ahern 		rt6 = container_of(dst, struct rt6_info, dst);
1901b70d792SDavid Ahern 
1911b70d792SDavid Ahern 	return rt6;
1921b70d792SDavid Ahern }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /*
1951da177e4SLinus Torvalds  *	Store a destination cache entry in a socket
1961da177e4SLinus Torvalds  */
1976bd4f355SEric Dumazet static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,
198d3818c92SEric Dumazet 				 const struct in6_addr *daddr,
199d3818c92SEric Dumazet 				 const struct in6_addr *saddr)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2021da177e4SLinus Torvalds 
2036bd4f355SEric Dumazet 	np->dst_cookie = rt6_get_cookie((struct rt6_info *)dst);
204f83ef8c0SHerbert Xu 	sk_setup_caps(sk, dst);
2051da177e4SLinus Torvalds 	np->daddr_cache = daddr;
2068e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
2078e1ef0a9SYOSHIFUJI Hideaki 	np->saddr_cache = saddr;
2088e1ef0a9SYOSHIFUJI Hideaki #endif
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211a50feda5SEric Dumazet static inline bool ipv6_unicast_destination(const struct sk_buff *skb)
2121da177e4SLinus Torvalds {
213adf30907SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *) skb_dst(skb);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	return rt->rt6i_flags & RTF_LOCAL;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2182647a9b0SMartin KaFai Lau static inline bool ipv6_anycast_destination(const struct dst_entry *dst,
2192647a9b0SMartin KaFai Lau 					    const struct in6_addr *daddr)
220509aba3bSFX Le Bail {
2212647a9b0SMartin KaFai Lau 	struct rt6_info *rt = (struct rt6_info *)dst;
222509aba3bSFX Le Bail 
2232647a9b0SMartin KaFai Lau 	return rt->rt6i_flags & RTF_ANYCAST ||
224ccdb2d17SVincent Bernat 		(rt->rt6i_dst.plen < 127 &&
2252647a9b0SMartin KaFai Lau 		 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr));
226509aba3bSFX Le Bail }
227509aba3bSFX Le Bail 
2287d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
2297d8c6e39SEric W. Biederman 		 int (*output)(struct net *, struct sock *, struct sk_buff *));
230ad0081e4SDavid Stevens 
231ad0081e4SDavid Stevens static inline int ip6_skb_dst_mtu(struct sk_buff *skb)
232ad0081e4SDavid Stevens {
233f60e5990Shannes@stressinduktion.org 	struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
234f60e5990Shannes@stressinduktion.org 				inet6_sk(skb->sk) : NULL;
235ad0081e4SDavid Stevens 
23693b36cf3SHannes Frederic Sowa 	return (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) ?
237ad0081e4SDavid Stevens 	       skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
238ad0081e4SDavid Stevens }
239ad0081e4SDavid Stevens 
24093b36cf3SHannes Frederic Sowa static inline bool ip6_sk_accept_pmtu(const struct sock *sk)
24193b36cf3SHannes Frederic Sowa {
2420b95227aSHannes Frederic Sowa 	return inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_INTERFACE &&
2430b95227aSHannes Frederic Sowa 	       inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_OMIT;
2440b95227aSHannes Frederic Sowa }
2450b95227aSHannes Frederic Sowa 
24660ff7467SWANG Cong static inline bool ip6_sk_ignore_df(const struct sock *sk)
2470b95227aSHannes Frederic Sowa {
2480b95227aSHannes Frederic Sowa 	return inet6_sk(sk)->pmtudisc < IPV6_PMTUDISC_DO ||
2490b95227aSHannes Frederic Sowa 	       inet6_sk(sk)->pmtudisc == IPV6_PMTUDISC_OMIT;
25093b36cf3SHannes Frederic Sowa }
25193b36cf3SHannes Frederic Sowa 
2522647a9b0SMartin KaFai Lau static inline struct in6_addr *rt6_nexthop(struct rt6_info *rt,
2532647a9b0SMartin KaFai Lau 					   struct in6_addr *daddr)
2549bb5a148SYOSHIFUJI Hideaki / 吉藤英明 {
2552647a9b0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_GATEWAY)
2569bb5a148SYOSHIFUJI Hideaki / 吉藤英明 		return &rt->rt6i_gateway;
25745e4fd26SMartin KaFai Lau 	else if (unlikely(rt->rt6i_flags & RTF_CACHE))
2582647a9b0SMartin KaFai Lau 		return &rt->rt6i_dst.addr;
2592647a9b0SMartin KaFai Lau 	else
2602647a9b0SMartin KaFai Lau 		return daddr;
2619bb5a148SYOSHIFUJI Hideaki / 吉藤英明 }
2629bb5a148SYOSHIFUJI Hideaki / 吉藤英明 
263f06b7549SDavid Ahern static inline bool rt6_duplicate_nexthop(struct rt6_info *a, struct rt6_info *b)
264f06b7549SDavid Ahern {
265f06b7549SDavid Ahern 	return a->dst.dev == b->dst.dev &&
266f06b7549SDavid Ahern 	       a->rt6i_idev == b->rt6i_idev &&
267f06b7549SDavid Ahern 	       ipv6_addr_equal(&a->rt6i_gateway, &b->rt6i_gateway) &&
268f06b7549SDavid Ahern 	       !lwtunnel_cmp_encap(a->dst.lwtstate, b->dst.lwtstate);
269f06b7549SDavid Ahern }
270*5e5d6fedSRoopa Prabhu 
2711da177e4SLinus Torvalds #endif
272