xref: /openbmc/linux/include/net/ip6_route.h (revision 2127d95aef6c795c3bd8b805722c5c46e8fe45dd)
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 
695c3a0fd7SJoe Perches void ip6_route_input(struct sk_buff *skb);
70d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
71d409b847SMahesh Bandewar 					 struct net_device *dev,
72d409b847SMahesh Bandewar 					 struct flowi6 *fl6, int flags);
731da177e4SLinus Torvalds 
746f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
756f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags);
766f21c96aSPaolo Abeni 
776f21c96aSPaolo Abeni static inline struct dst_entry *ip6_route_output(struct net *net,
786f21c96aSPaolo Abeni 						 const struct sock *sk,
796f21c96aSPaolo Abeni 						 struct flowi6 *fl6)
806f21c96aSPaolo Abeni {
816f21c96aSPaolo Abeni 	return ip6_route_output_flags(net, sk, fl6, 0);
826f21c96aSPaolo Abeni }
836f21c96aSPaolo Abeni 
845c3a0fd7SJoe Perches struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
855c3a0fd7SJoe Perches 				   int flags);
869ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
879ff74384SDavid Ahern 			       int ifindex, struct flowi6 *fl6, int flags);
881da177e4SLinus Torvalds 
892f460933SWANG Cong void ip6_route_init_special_entries(void);
905c3a0fd7SJoe Perches int ip6_route_init(void);
915c3a0fd7SJoe Perches void ip6_route_cleanup(void);
921da177e4SLinus Torvalds 
935c3a0fd7SJoe Perches int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg);
941da177e4SLinus Torvalds 
95333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, struct netlink_ext_ack *extack);
965c3a0fd7SJoe Perches int ip6_ins_rt(struct rt6_info *);
975c3a0fd7SJoe Perches int ip6_del_rt(struct rt6_info *);
981da177e4SLinus Torvalds 
9935732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt);
10035732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt);
101c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, struct fib6_gc_args *gc_args,
102c757faa8SWei Wang 			unsigned long now);
10335732d01SWei Wang 
104a2e2ff56SDavid Ahern static inline int ip6_route_get_saddr(struct net *net, struct rt6_info *rt,
105a2e2ff56SDavid Ahern 				      const struct in6_addr *daddr,
106a2e2ff56SDavid Ahern 				      unsigned int prefs,
107a2e2ff56SDavid Ahern 				      struct in6_addr *saddr)
108a2e2ff56SDavid Ahern {
109a2e2ff56SDavid Ahern 	struct inet6_dev *idev =
110a2e2ff56SDavid Ahern 			rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
111a2e2ff56SDavid Ahern 	int err = 0;
112a2e2ff56SDavid Ahern 
113a2e2ff56SDavid Ahern 	if (rt && rt->rt6i_prefsrc.plen)
114a2e2ff56SDavid Ahern 		*saddr = rt->rt6i_prefsrc.addr;
115a2e2ff56SDavid Ahern 	else
116a2e2ff56SDavid Ahern 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
117a2e2ff56SDavid Ahern 					 daddr, prefs, saddr);
118a2e2ff56SDavid Ahern 
119a2e2ff56SDavid Ahern 	return err;
120a2e2ff56SDavid Ahern }
121c3968a85SDaniel Walter 
1225c3a0fd7SJoe Perches struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1235c3a0fd7SJoe Perches 			    const struct in6_addr *saddr, int oif, int flags);
12423aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb);
1251da177e4SLinus Torvalds 
1265c3a0fd7SJoe Perches struct dst_entry *icmp6_dst_alloc(struct net_device *dev, struct flowi6 *fl6);
1273b00944cSYOSHIFUJI Hideaki 
1285c3a0fd7SJoe Perches void fib6_force_start_gc(struct net *net);
1291da177e4SLinus Torvalds 
1305c3a0fd7SJoe Perches struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1315c3a0fd7SJoe Perches 				    const struct in6_addr *addr, bool anycast);
1321da177e4SLinus Torvalds 
1339ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
1349ab179d8SDavid Ahern 			       int flags);
1359ab179d8SDavid Ahern 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  *	support functions for ND
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  */
1405c3a0fd7SJoe Perches struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr,
1411da177e4SLinus Torvalds 				     struct net_device *dev);
1425c3a0fd7SJoe Perches struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1435c3a0fd7SJoe Perches 				     struct net_device *dev, unsigned int pref);
1441da177e4SLinus Torvalds 
1455c3a0fd7SJoe Perches void rt6_purge_dflt_routers(struct net *net);
1461da177e4SLinus Torvalds 
1475c3a0fd7SJoe Perches int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
148b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr);
14970ceb4f5SYOSHIFUJI Hideaki 
1505c3a0fd7SJoe Perches void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, int oif,
151e2d118a1SLorenzo Colitti 		     u32 mark, kuid_t uid);
1525c3a0fd7SJoe Perches void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu);
153e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
154e2d118a1SLorenzo Colitti 		  kuid_t uid);
1555c3a0fd7SJoe Perches void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1565c3a0fd7SJoe Perches 			    u32 mark);
1575c3a0fd7SJoe Perches void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds struct netlink_callback;
1601da177e4SLinus Torvalds 
161fd2c3ef7SEric Dumazet struct rt6_rtnl_dump_arg {
1621b43af54SPatrick McHardy 	struct sk_buff *skb;
1631b43af54SPatrick McHardy 	struct netlink_callback *cb;
164191cd582SBrian Haley 	struct net *net;
1651b43af54SPatrick McHardy };
1661b43af54SPatrick McHardy 
1675c3a0fd7SJoe Perches int rt6_dump_route(struct rt6_info *rt, void *p_arg);
1685c3a0fd7SJoe Perches void rt6_ifdown(struct net *net, struct net_device *dev);
1695c3a0fd7SJoe Perches void rt6_mtu_change(struct net_device *dev, unsigned int mtu);
1705c3a0fd7SJoe Perches void rt6_remove_prefsrc(struct inet6_ifaddr *ifp);
171be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway);
172*2127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags);
1731da177e4SLinus Torvalds 
1741b70d792SDavid Ahern static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
1751b70d792SDavid Ahern {
1761b70d792SDavid Ahern 	const struct dst_entry *dst = skb_dst(skb);
1771b70d792SDavid Ahern 	const struct rt6_info *rt6 = NULL;
1781b70d792SDavid Ahern 
1791b70d792SDavid Ahern 	if (dst)
1801b70d792SDavid Ahern 		rt6 = container_of(dst, struct rt6_info, dst);
1811b70d792SDavid Ahern 
1821b70d792SDavid Ahern 	return rt6;
1831b70d792SDavid Ahern }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /*
1861da177e4SLinus Torvalds  *	Store a destination cache entry in a socket
1871da177e4SLinus Torvalds  */
1886bd4f355SEric Dumazet static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,
189d3818c92SEric Dumazet 				 const struct in6_addr *daddr,
190d3818c92SEric Dumazet 				 const struct in6_addr *saddr)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
1931da177e4SLinus Torvalds 
1946bd4f355SEric Dumazet 	np->dst_cookie = rt6_get_cookie((struct rt6_info *)dst);
195f83ef8c0SHerbert Xu 	sk_setup_caps(sk, dst);
1961da177e4SLinus Torvalds 	np->daddr_cache = daddr;
1978e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
1988e1ef0a9SYOSHIFUJI Hideaki 	np->saddr_cache = saddr;
1998e1ef0a9SYOSHIFUJI Hideaki #endif
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202a50feda5SEric Dumazet static inline bool ipv6_unicast_destination(const struct sk_buff *skb)
2031da177e4SLinus Torvalds {
204adf30907SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *) skb_dst(skb);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	return rt->rt6i_flags & RTF_LOCAL;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2092647a9b0SMartin KaFai Lau static inline bool ipv6_anycast_destination(const struct dst_entry *dst,
2102647a9b0SMartin KaFai Lau 					    const struct in6_addr *daddr)
211509aba3bSFX Le Bail {
2122647a9b0SMartin KaFai Lau 	struct rt6_info *rt = (struct rt6_info *)dst;
213509aba3bSFX Le Bail 
2142647a9b0SMartin KaFai Lau 	return rt->rt6i_flags & RTF_ANYCAST ||
215ccdb2d17SVincent Bernat 		(rt->rt6i_dst.plen < 127 &&
2162647a9b0SMartin KaFai Lau 		 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr));
217509aba3bSFX Le Bail }
218509aba3bSFX Le Bail 
2197d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
2207d8c6e39SEric W. Biederman 		 int (*output)(struct net *, struct sock *, struct sk_buff *));
221ad0081e4SDavid Stevens 
222ad0081e4SDavid Stevens static inline int ip6_skb_dst_mtu(struct sk_buff *skb)
223ad0081e4SDavid Stevens {
224f60e5990Shannes@stressinduktion.org 	struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
225f60e5990Shannes@stressinduktion.org 				inet6_sk(skb->sk) : NULL;
226ad0081e4SDavid Stevens 
22793b36cf3SHannes Frederic Sowa 	return (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) ?
228ad0081e4SDavid Stevens 	       skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
229ad0081e4SDavid Stevens }
230ad0081e4SDavid Stevens 
23193b36cf3SHannes Frederic Sowa static inline bool ip6_sk_accept_pmtu(const struct sock *sk)
23293b36cf3SHannes Frederic Sowa {
2330b95227aSHannes Frederic Sowa 	return inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_INTERFACE &&
2340b95227aSHannes Frederic Sowa 	       inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_OMIT;
2350b95227aSHannes Frederic Sowa }
2360b95227aSHannes Frederic Sowa 
23760ff7467SWANG Cong static inline bool ip6_sk_ignore_df(const struct sock *sk)
2380b95227aSHannes Frederic Sowa {
2390b95227aSHannes Frederic Sowa 	return inet6_sk(sk)->pmtudisc < IPV6_PMTUDISC_DO ||
2400b95227aSHannes Frederic Sowa 	       inet6_sk(sk)->pmtudisc == IPV6_PMTUDISC_OMIT;
24193b36cf3SHannes Frederic Sowa }
24293b36cf3SHannes Frederic Sowa 
2432647a9b0SMartin KaFai Lau static inline struct in6_addr *rt6_nexthop(struct rt6_info *rt,
2442647a9b0SMartin KaFai Lau 					   struct in6_addr *daddr)
2459bb5a148SYOSHIFUJI Hideaki / 吉藤英明 {
2462647a9b0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_GATEWAY)
2479bb5a148SYOSHIFUJI Hideaki / 吉藤英明 		return &rt->rt6i_gateway;
24845e4fd26SMartin KaFai Lau 	else if (unlikely(rt->rt6i_flags & RTF_CACHE))
2492647a9b0SMartin KaFai Lau 		return &rt->rt6i_dst.addr;
2502647a9b0SMartin KaFai Lau 	else
2512647a9b0SMartin KaFai Lau 		return daddr;
2529bb5a148SYOSHIFUJI Hideaki / 吉藤英明 }
2539bb5a148SYOSHIFUJI Hideaki / 吉藤英明 
254f06b7549SDavid Ahern static inline bool rt6_duplicate_nexthop(struct rt6_info *a, struct rt6_info *b)
255f06b7549SDavid Ahern {
256f06b7549SDavid Ahern 	return a->dst.dev == b->dst.dev &&
257f06b7549SDavid Ahern 	       a->rt6i_idev == b->rt6i_idev &&
258f06b7549SDavid Ahern 	       ipv6_addr_equal(&a->rt6i_gateway, &b->rt6i_gateway) &&
259f06b7549SDavid Ahern 	       !lwtunnel_cmp_encap(a->dst.lwtstate, b->dst.lwtstate);
260f06b7549SDavid Ahern }
2611da177e4SLinus Torvalds #endif
262