0001
0002 #ifndef _NET_IP6_ROUTE_H
0003 #define _NET_IP6_ROUTE_H
0004
0005 #include <net/addrconf.h>
0006 #include <net/flow.h>
0007 #include <net/ip6_fib.h>
0008 #include <net/sock.h>
0009 #include <net/lwtunnel.h>
0010 #include <linux/ip.h>
0011 #include <linux/ipv6.h>
0012 #include <linux/route.h>
0013 #include <net/nexthop.h>
0014
0015 struct route_info {
0016 __u8 type;
0017 __u8 length;
0018 __u8 prefix_len;
0019 #if defined(__BIG_ENDIAN_BITFIELD)
0020 __u8 reserved_h:3,
0021 route_pref:2,
0022 reserved_l:3;
0023 #elif defined(__LITTLE_ENDIAN_BITFIELD)
0024 __u8 reserved_l:3,
0025 route_pref:2,
0026 reserved_h:3;
0027 #endif
0028 __be32 lifetime;
0029 __u8 prefix[];
0030 };
0031
0032 #define RT6_LOOKUP_F_IFACE 0x00000001
0033 #define RT6_LOOKUP_F_REACHABLE 0x00000002
0034 #define RT6_LOOKUP_F_HAS_SADDR 0x00000004
0035 #define RT6_LOOKUP_F_SRCPREF_TMP 0x00000008
0036 #define RT6_LOOKUP_F_SRCPREF_PUBLIC 0x00000010
0037 #define RT6_LOOKUP_F_SRCPREF_COA 0x00000020
0038 #define RT6_LOOKUP_F_IGNORE_LINKSTATE 0x00000040
0039 #define RT6_LOOKUP_F_DST_NOREF 0x00000080
0040
0041
0042
0043
0044 #define IP6_MAX_MTU (0xFFFF + sizeof(struct ipv6hdr))
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 static inline int rt6_srcprefs2flags(unsigned int srcprefs)
0055 {
0056
0057 return srcprefs << 3;
0058 }
0059
0060 static inline unsigned int rt6_flags2srcprefs(int flags)
0061 {
0062 return (flags >> 3) & 7;
0063 }
0064
0065 static inline bool rt6_need_strict(const struct in6_addr *daddr)
0066 {
0067 return ipv6_addr_type(daddr) &
0068 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
0069 }
0070
0071
0072
0073
0074 static inline bool rt6_qualify_for_ecmp(const struct fib6_info *f6i)
0075 {
0076
0077 return !(f6i->fib6_flags & RTF_ADDRCONF) && !f6i->nh &&
0078 f6i->fib6_nh->fib_nh_gw_family;
0079 }
0080
0081 void ip6_route_input(struct sk_buff *skb);
0082 struct dst_entry *ip6_route_input_lookup(struct net *net,
0083 struct net_device *dev,
0084 struct flowi6 *fl6,
0085 const struct sk_buff *skb, int flags);
0086
0087 struct dst_entry *ip6_route_output_flags_noref(struct net *net,
0088 const struct sock *sk,
0089 struct flowi6 *fl6, int flags);
0090
0091 struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
0092 struct flowi6 *fl6, int flags);
0093
0094 static inline struct dst_entry *ip6_route_output(struct net *net,
0095 const struct sock *sk,
0096 struct flowi6 *fl6)
0097 {
0098 return ip6_route_output_flags(net, sk, fl6, 0);
0099 }
0100
0101
0102
0103
0104 static inline void ip6_rt_put_flags(struct rt6_info *rt, int flags)
0105 {
0106 if (!(flags & RT6_LOOKUP_F_DST_NOREF) ||
0107 !list_empty(&rt->rt6i_uncached))
0108 ip6_rt_put(rt);
0109 }
0110
0111 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
0112 const struct sk_buff *skb, int flags);
0113 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
0114 int ifindex, struct flowi6 *fl6,
0115 const struct sk_buff *skb, int flags);
0116
0117 void ip6_route_init_special_entries(void);
0118 int ip6_route_init(void);
0119 void ip6_route_cleanup(void);
0120
0121 int ipv6_route_ioctl(struct net *net, unsigned int cmd,
0122 struct in6_rtmsg *rtmsg);
0123
0124 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
0125 struct netlink_ext_ack *extack);
0126 int ip6_ins_rt(struct net *net, struct fib6_info *f6i);
0127 int ip6_del_rt(struct net *net, struct fib6_info *f6i, bool skip_notify);
0128
0129 void rt6_flush_exceptions(struct fib6_info *f6i);
0130 void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
0131 unsigned long now);
0132
0133 static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
0134 const struct in6_addr *daddr,
0135 unsigned int prefs,
0136 struct in6_addr *saddr)
0137 {
0138 int err = 0;
0139
0140 if (f6i && f6i->fib6_prefsrc.plen) {
0141 *saddr = f6i->fib6_prefsrc.addr;
0142 } else {
0143 struct net_device *dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
0144
0145 err = ipv6_dev_get_saddr(net, dev, daddr, prefs, saddr);
0146 }
0147
0148 return err;
0149 }
0150
0151 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
0152 const struct in6_addr *saddr, int oif,
0153 const struct sk_buff *skb, int flags);
0154 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
0155 const struct sk_buff *skb, struct flow_keys *hkeys);
0156
0157 struct dst_entry *icmp6_dst_alloc(struct net_device *dev, struct flowi6 *fl6);
0158
0159 void fib6_force_start_gc(struct net *net);
0160
0161 struct fib6_info *addrconf_f6i_alloc(struct net *net, struct inet6_dev *idev,
0162 const struct in6_addr *addr, bool anycast,
0163 gfp_t gfp_flags);
0164
0165 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
0166 int flags);
0167
0168
0169
0170
0171
0172 struct fib6_info *rt6_get_dflt_router(struct net *net,
0173 const struct in6_addr *addr,
0174 struct net_device *dev);
0175 struct fib6_info *rt6_add_dflt_router(struct net *net,
0176 const struct in6_addr *gwaddr,
0177 struct net_device *dev, unsigned int pref,
0178 u32 defrtr_usr_metric);
0179
0180 void rt6_purge_dflt_routers(struct net *net);
0181
0182 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
0183 const struct in6_addr *gwaddr);
0184
0185 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, int oif,
0186 u32 mark, kuid_t uid);
0187 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu);
0188 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
0189 kuid_t uid);
0190 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif);
0191 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk);
0192
0193 struct netlink_callback;
0194
0195 struct rt6_rtnl_dump_arg {
0196 struct sk_buff *skb;
0197 struct netlink_callback *cb;
0198 struct net *net;
0199 struct fib_dump_filter filter;
0200 };
0201
0202 int rt6_dump_route(struct fib6_info *f6i, void *p_arg, unsigned int skip);
0203 void rt6_mtu_change(struct net_device *dev, unsigned int mtu);
0204 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp);
0205 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway);
0206 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags);
0207 void rt6_disable_ip(struct net_device *dev, unsigned long event);
0208 void rt6_sync_down_dev(struct net_device *dev, unsigned long event);
0209 void rt6_multipath_rebalance(struct fib6_info *f6i);
0210
0211 void rt6_uncached_list_add(struct rt6_info *rt);
0212 void rt6_uncached_list_del(struct rt6_info *rt);
0213
0214 static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
0215 {
0216 const struct dst_entry *dst = skb_dst(skb);
0217 const struct rt6_info *rt6 = NULL;
0218
0219 if (dst)
0220 rt6 = container_of(dst, struct rt6_info, dst);
0221
0222 return rt6;
0223 }
0224
0225
0226
0227
0228 static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst,
0229 const struct in6_addr *daddr,
0230 const struct in6_addr *saddr)
0231 {
0232 struct ipv6_pinfo *np = inet6_sk(sk);
0233
0234 np->dst_cookie = rt6_get_cookie((struct rt6_info *)dst);
0235 sk_setup_caps(sk, dst);
0236 np->daddr_cache = daddr;
0237 #ifdef CONFIG_IPV6_SUBTREES
0238 np->saddr_cache = saddr;
0239 #endif
0240 }
0241
0242 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
0243 const struct flowi6 *fl6);
0244
0245 static inline bool ipv6_unicast_destination(const struct sk_buff *skb)
0246 {
0247 struct rt6_info *rt = (struct rt6_info *) skb_dst(skb);
0248
0249 return rt->rt6i_flags & RTF_LOCAL;
0250 }
0251
0252 static inline bool ipv6_anycast_destination(const struct dst_entry *dst,
0253 const struct in6_addr *daddr)
0254 {
0255 struct rt6_info *rt = (struct rt6_info *)dst;
0256
0257 return rt->rt6i_flags & RTF_ANYCAST ||
0258 (rt->rt6i_dst.plen < 127 &&
0259 !(rt->rt6i_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) &&
0260 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr));
0261 }
0262
0263 int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
0264 int (*output)(struct net *, struct sock *, struct sk_buff *));
0265
0266 static inline unsigned int ip6_skb_dst_mtu(const struct sk_buff *skb)
0267 {
0268 const struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
0269 inet6_sk(skb->sk) : NULL;
0270 const struct dst_entry *dst = skb_dst(skb);
0271 unsigned int mtu;
0272
0273 if (np && np->pmtudisc >= IPV6_PMTUDISC_PROBE) {
0274 mtu = READ_ONCE(dst->dev->mtu);
0275 mtu -= lwtunnel_headroom(dst->lwtstate, mtu);
0276 } else {
0277 mtu = dst_mtu(dst);
0278 }
0279 return mtu;
0280 }
0281
0282 static inline bool ip6_sk_accept_pmtu(const struct sock *sk)
0283 {
0284 return inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_INTERFACE &&
0285 inet6_sk(sk)->pmtudisc != IPV6_PMTUDISC_OMIT;
0286 }
0287
0288 static inline bool ip6_sk_ignore_df(const struct sock *sk)
0289 {
0290 return inet6_sk(sk)->pmtudisc < IPV6_PMTUDISC_DO ||
0291 inet6_sk(sk)->pmtudisc == IPV6_PMTUDISC_OMIT;
0292 }
0293
0294 static inline const struct in6_addr *rt6_nexthop(const struct rt6_info *rt,
0295 const struct in6_addr *daddr)
0296 {
0297 if (rt->rt6i_flags & RTF_GATEWAY)
0298 return &rt->rt6i_gateway;
0299 else if (unlikely(rt->rt6i_flags & RTF_CACHE))
0300 return &rt->rt6i_dst.addr;
0301 else
0302 return daddr;
0303 }
0304
0305 static inline bool rt6_duplicate_nexthop(struct fib6_info *a, struct fib6_info *b)
0306 {
0307 struct fib6_nh *nha, *nhb;
0308
0309 if (a->nh || b->nh)
0310 return nexthop_cmp(a->nh, b->nh);
0311
0312 nha = a->fib6_nh;
0313 nhb = b->fib6_nh;
0314 return nha->fib_nh_dev == nhb->fib_nh_dev &&
0315 ipv6_addr_equal(&nha->fib_nh_gw6, &nhb->fib_nh_gw6) &&
0316 !lwtunnel_cmp_encap(nha->fib_nh_lws, nhb->fib_nh_lws);
0317 }
0318
0319 static inline unsigned int ip6_dst_mtu_maybe_forward(const struct dst_entry *dst,
0320 bool forwarding)
0321 {
0322 struct inet6_dev *idev;
0323 unsigned int mtu;
0324
0325 if (!forwarding || dst_metric_locked(dst, RTAX_MTU)) {
0326 mtu = dst_metric_raw(dst, RTAX_MTU);
0327 if (mtu)
0328 goto out;
0329 }
0330
0331 mtu = IPV6_MIN_MTU;
0332 rcu_read_lock();
0333 idev = __in6_dev_get(dst->dev);
0334 if (idev)
0335 mtu = idev->cnf.mtu6;
0336 rcu_read_unlock();
0337
0338 out:
0339 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
0340 }
0341
0342 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
0343 const struct in6_addr *daddr,
0344 const struct in6_addr *saddr);
0345
0346 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
0347 struct net_device *dev, struct sk_buff *skb,
0348 const void *daddr);
0349 #endif