Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET  is implemented using the  BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      Definitions for the IP router.
0008  *
0009  * Version: @(#)route.h 1.0.4   05/27/93
0010  *
0011  * Authors: Ross Biro
0012  *      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
0013  * Fixes:
0014  *      Alan Cox    :   Reformatted. Added ip_rt_local()
0015  *      Alan Cox    :   Support for TCP parameters.
0016  *      Alexey Kuznetsov:   Major changes for new routing code.
0017  *      Mike McLagan    :   Routing by source
0018  *      Robert Olsson   :   Added rt_cache statistics
0019  */
0020 #ifndef _ROUTE_H
0021 #define _ROUTE_H
0022 
0023 #include <net/dst.h>
0024 #include <net/inetpeer.h>
0025 #include <net/flow.h>
0026 #include <net/inet_sock.h>
0027 #include <net/ip_fib.h>
0028 #include <net/arp.h>
0029 #include <net/ndisc.h>
0030 #include <linux/in_route.h>
0031 #include <linux/rtnetlink.h>
0032 #include <linux/rcupdate.h>
0033 #include <linux/route.h>
0034 #include <linux/ip.h>
0035 #include <linux/cache.h>
0036 #include <linux/security.h>
0037 
0038 /* IPv4 datagram length is stored into 16bit field (tot_len) */
0039 #define IP_MAX_MTU  0xFFFFU
0040 
0041 #define RTO_ONLINK  0x01
0042 
0043 #define RT_CONN_FLAGS(sk)   (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
0044 #define RT_CONN_FLAGS_TOS(sk,tos)   (RT_TOS(tos) | sock_flag(sk, SOCK_LOCALROUTE))
0045 
0046 static inline __u8 ip_sock_rt_scope(const struct sock *sk)
0047 {
0048     if (sock_flag(sk, SOCK_LOCALROUTE))
0049         return RT_SCOPE_LINK;
0050 
0051     return RT_SCOPE_UNIVERSE;
0052 }
0053 
0054 static inline __u8 ip_sock_rt_tos(const struct sock *sk)
0055 {
0056     return RT_TOS(inet_sk(sk)->tos);
0057 }
0058 
0059 struct ip_tunnel_info;
0060 struct fib_nh;
0061 struct fib_info;
0062 struct uncached_list;
0063 struct rtable {
0064     struct dst_entry    dst;
0065 
0066     int         rt_genid;
0067     unsigned int        rt_flags;
0068     __u16           rt_type;
0069     __u8            rt_is_input;
0070     __u8            rt_uses_gateway;
0071 
0072     int         rt_iif;
0073 
0074     u8          rt_gw_family;
0075     /* Info on neighbour */
0076     union {
0077         __be32      rt_gw4;
0078         struct in6_addr rt_gw6;
0079     };
0080 
0081     /* Miscellaneous cached information */
0082     u32         rt_mtu_locked:1,
0083                 rt_pmtu:31;
0084 
0085     struct list_head    rt_uncached;
0086     struct uncached_list    *rt_uncached_list;
0087 };
0088 
0089 static inline bool rt_is_input_route(const struct rtable *rt)
0090 {
0091     return rt->rt_is_input != 0;
0092 }
0093 
0094 static inline bool rt_is_output_route(const struct rtable *rt)
0095 {
0096     return rt->rt_is_input == 0;
0097 }
0098 
0099 static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr)
0100 {
0101     if (rt->rt_gw_family == AF_INET)
0102         return rt->rt_gw4;
0103     return daddr;
0104 }
0105 
0106 struct ip_rt_acct {
0107     __u32   o_bytes;
0108     __u32   o_packets;
0109     __u32   i_bytes;
0110     __u32   i_packets;
0111 };
0112 
0113 struct rt_cache_stat {
0114         unsigned int in_slow_tot;
0115         unsigned int in_slow_mc;
0116         unsigned int in_no_route;
0117         unsigned int in_brd;
0118         unsigned int in_martian_dst;
0119         unsigned int in_martian_src;
0120         unsigned int out_slow_tot;
0121         unsigned int out_slow_mc;
0122 };
0123 
0124 extern struct ip_rt_acct __percpu *ip_rt_acct;
0125 
0126 struct in_device;
0127 
0128 int ip_rt_init(void);
0129 void rt_cache_flush(struct net *net);
0130 void rt_flush_dev(struct net_device *dev);
0131 struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *flp,
0132                     const struct sk_buff *skb);
0133 struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *flp,
0134                         struct fib_result *res,
0135                         const struct sk_buff *skb);
0136 
0137 static inline struct rtable *__ip_route_output_key(struct net *net,
0138                            struct flowi4 *flp)
0139 {
0140     return ip_route_output_key_hash(net, flp, NULL);
0141 }
0142 
0143 struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,
0144                     const struct sock *sk);
0145 struct rtable *ip_route_output_tunnel(struct sk_buff *skb,
0146                       struct net_device *dev,
0147                       struct net *net, __be32 *saddr,
0148                       const struct ip_tunnel_info *info,
0149                       u8 protocol, bool use_cache);
0150 
0151 struct dst_entry *ipv4_blackhole_route(struct net *net,
0152                        struct dst_entry *dst_orig);
0153 
0154 static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp)
0155 {
0156     return ip_route_output_flow(net, flp, NULL);
0157 }
0158 
0159 static inline struct rtable *ip_route_output(struct net *net, __be32 daddr,
0160                          __be32 saddr, u8 tos, int oif)
0161 {
0162     struct flowi4 fl4 = {
0163         .flowi4_oif = oif,
0164         .flowi4_tos = tos,
0165         .daddr = daddr,
0166         .saddr = saddr,
0167     };
0168     return ip_route_output_key(net, &fl4);
0169 }
0170 
0171 static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4,
0172                            struct sock *sk,
0173                            __be32 daddr, __be32 saddr,
0174                            __be16 dport, __be16 sport,
0175                            __u8 proto, __u8 tos, int oif)
0176 {
0177     flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos,
0178                RT_SCOPE_UNIVERSE, proto,
0179                sk ? inet_sk_flowi_flags(sk) : 0,
0180                daddr, saddr, dport, sport, sock_net_uid(net, sk));
0181     if (sk)
0182         security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
0183     return ip_route_output_flow(net, fl4, sk);
0184 }
0185 
0186 static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4,
0187                          __be32 daddr, __be32 saddr,
0188                          __be32 gre_key, __u8 tos, int oif)
0189 {
0190     memset(fl4, 0, sizeof(*fl4));
0191     fl4->flowi4_oif = oif;
0192     fl4->daddr = daddr;
0193     fl4->saddr = saddr;
0194     fl4->flowi4_tos = tos;
0195     fl4->flowi4_proto = IPPROTO_GRE;
0196     fl4->fl4_gre_key = gre_key;
0197     return ip_route_output_key(net, fl4);
0198 }
0199 int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
0200               u8 tos, struct net_device *dev,
0201               struct in_device *in_dev, u32 *itag);
0202 int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src,
0203              u8 tos, struct net_device *devin);
0204 int ip_route_use_hint(struct sk_buff *skb, __be32 dst, __be32 src,
0205               u8 tos, struct net_device *devin,
0206               const struct sk_buff *hint);
0207 
0208 static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,
0209                  u8 tos, struct net_device *devin)
0210 {
0211     int err;
0212 
0213     rcu_read_lock();
0214     err = ip_route_input_noref(skb, dst, src, tos, devin);
0215     if (!err) {
0216         skb_dst_force(skb);
0217         if (!skb_dst(skb))
0218             err = -EINVAL;
0219     }
0220     rcu_read_unlock();
0221 
0222     return err;
0223 }
0224 
0225 void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, int oif,
0226               u8 protocol);
0227 void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu);
0228 void ipv4_redirect(struct sk_buff *skb, struct net *net, int oif, u8 protocol);
0229 void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk);
0230 void ip_rt_send_redirect(struct sk_buff *skb);
0231 
0232 unsigned int inet_addr_type(struct net *net, __be32 addr);
0233 unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id);
0234 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
0235                 __be32 addr);
0236 unsigned int inet_addr_type_dev_table(struct net *net,
0237                       const struct net_device *dev,
0238                       __be32 addr);
0239 void ip_rt_multicast_event(struct in_device *);
0240 int ip_rt_ioctl(struct net *, unsigned int cmd, struct rtentry *rt);
0241 void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
0242 struct rtable *rt_dst_alloc(struct net_device *dev,
0243                 unsigned int flags, u16 type, bool noxfrm);
0244 struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt);
0245 
0246 struct in_ifaddr;
0247 void fib_add_ifaddr(struct in_ifaddr *);
0248 void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *);
0249 void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric);
0250 
0251 void rt_add_uncached_list(struct rtable *rt);
0252 void rt_del_uncached_list(struct rtable *rt);
0253 
0254 int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
0255                u32 table_id, struct fib_info *fi,
0256                int *fa_index, int fa_start, unsigned int flags);
0257 
0258 static inline void ip_rt_put(struct rtable *rt)
0259 {
0260     /* dst_release() accepts a NULL parameter.
0261      * We rely on dst being first structure in struct rtable
0262      */
0263     BUILD_BUG_ON(offsetof(struct rtable, dst) != 0);
0264     dst_release(&rt->dst);
0265 }
0266 
0267 #define IPTOS_RT_MASK   (IPTOS_TOS_MASK & ~3)
0268 
0269 extern const __u8 ip_tos2prio[16];
0270 
0271 static inline char rt_tos2priority(u8 tos)
0272 {
0273     return ip_tos2prio[IPTOS_TOS(tos)>>1];
0274 }
0275 
0276 /* ip_route_connect() and ip_route_newports() work in tandem whilst
0277  * binding a socket for a new outgoing connection.
0278  *
0279  * In order to use IPSEC properly, we must, in the end, have a
0280  * route that was looked up using all available keys including source
0281  * and destination ports.
0282  *
0283  * However, if a source port needs to be allocated (the user specified
0284  * a wildcard source port) we need to obtain addressing information
0285  * in order to perform that allocation.
0286  *
0287  * So ip_route_connect() looks up a route using wildcarded source and
0288  * destination ports in the key, simply so that we can get a pair of
0289  * addresses to use for port allocation.
0290  *
0291  * Later, once the ports are allocated, ip_route_newports() will make
0292  * another route lookup if needed to make sure we catch any IPSEC
0293  * rules keyed on the port information.
0294  *
0295  * The callers allocate the flow key on their stack, and must pass in
0296  * the same flowi4 object to both the ip_route_connect() and the
0297  * ip_route_newports() calls.
0298  */
0299 
0300 static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst,
0301                      __be32 src, int oif, u8 protocol,
0302                      __be16 sport, __be16 dport,
0303                      const struct sock *sk)
0304 {
0305     __u8 flow_flags = 0;
0306 
0307     if (inet_sk(sk)->transparent)
0308         flow_flags |= FLOWI_FLAG_ANYSRC;
0309 
0310     flowi4_init_output(fl4, oif, sk->sk_mark, ip_sock_rt_tos(sk),
0311                ip_sock_rt_scope(sk), protocol, flow_flags, dst,
0312                src, dport, sport, sk->sk_uid);
0313 }
0314 
0315 static inline struct rtable *ip_route_connect(struct flowi4 *fl4, __be32 dst,
0316                           __be32 src, int oif, u8 protocol,
0317                           __be16 sport, __be16 dport,
0318                           struct sock *sk)
0319 {
0320     struct net *net = sock_net(sk);
0321     struct rtable *rt;
0322 
0323     ip_route_connect_init(fl4, dst, src, oif, protocol, sport, dport, sk);
0324 
0325     if (!dst || !src) {
0326         rt = __ip_route_output_key(net, fl4);
0327         if (IS_ERR(rt))
0328             return rt;
0329         ip_rt_put(rt);
0330         flowi4_update_output(fl4, oif, fl4->flowi4_tos, fl4->daddr,
0331                      fl4->saddr);
0332     }
0333     security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
0334     return ip_route_output_flow(net, fl4, sk);
0335 }
0336 
0337 static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt,
0338                            __be16 orig_sport, __be16 orig_dport,
0339                            __be16 sport, __be16 dport,
0340                            struct sock *sk)
0341 {
0342     if (sport != orig_sport || dport != orig_dport) {
0343         fl4->fl4_dport = dport;
0344         fl4->fl4_sport = sport;
0345         ip_rt_put(rt);
0346         flowi4_update_output(fl4, sk->sk_bound_dev_if,
0347                      RT_CONN_FLAGS(sk), fl4->daddr,
0348                      fl4->saddr);
0349         security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
0350         return ip_route_output_flow(sock_net(sk), fl4, sk);
0351     }
0352     return rt;
0353 }
0354 
0355 static inline int inet_iif(const struct sk_buff *skb)
0356 {
0357     struct rtable *rt = skb_rtable(skb);
0358 
0359     if (rt && rt->rt_iif)
0360         return rt->rt_iif;
0361 
0362     return skb->skb_iif;
0363 }
0364 
0365 static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
0366 {
0367     int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
0368     struct net *net = dev_net(dst->dev);
0369 
0370     if (hoplimit == 0)
0371         hoplimit = READ_ONCE(net->ipv4.sysctl_ip_default_ttl);
0372     return hoplimit;
0373 }
0374 
0375 static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
0376                          __be32 daddr)
0377 {
0378     struct neighbour *neigh;
0379 
0380     neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr);
0381     if (unlikely(!neigh))
0382         neigh = __neigh_create(&arp_tbl, &daddr, dev, false);
0383 
0384     return neigh;
0385 }
0386 
0387 static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
0388                         struct sk_buff *skb,
0389                         bool *is_v6gw)
0390 {
0391     struct net_device *dev = rt->dst.dev;
0392     struct neighbour *neigh;
0393 
0394     if (likely(rt->rt_gw_family == AF_INET)) {
0395         neigh = ip_neigh_gw4(dev, rt->rt_gw4);
0396     } else if (rt->rt_gw_family == AF_INET6) {
0397         neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
0398         *is_v6gw = true;
0399     } else {
0400         neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
0401     }
0402     return neigh;
0403 }
0404 
0405 #endif  /* _ROUTE_H */