blob: bbcf2aba149f9529c8fdabd754161beeef86b525 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Definitions for the IP router.
8 *
9 * Version: @(#)route.h 1.0.4 05/27/93
10 *
Jesper Juhl02c30a82005-05-05 16:16:16 -070011 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13 * Fixes:
14 * Alan Cox : Reformatted. Added ip_rt_local()
15 * Alan Cox : Support for TCP parameters.
16 * Alexey Kuznetsov: Major changes for new routing code.
17 * Mike McLagan : Routing by source
18 * Robert Olsson : Added rt_cache statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20#ifndef _ROUTE_H
21#define _ROUTE_H
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/dst.h>
24#include <net/inetpeer.h>
25#include <net/flow.h>
KOVACS Krisztian79876872008-10-01 07:35:39 -070026#include <net/inet_sock.h>
Peter Nørlund79a13152015-09-30 10:12:22 +020027#include <net/ip_fib.h>
David Ahern5c9f7c12019-04-05 16:30:34 -070028#include <net/arp.h>
29#include <net/ndisc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/in_route.h>
31#include <linux/rtnetlink.h>
David S. Millerc6cffba2012-07-26 11:14:38 +000032#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/route.h>
34#include <linux/ip.h>
35#include <linux/cache.h>
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -070036#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +010038/* IPv4 datagram length is stored into 16bit field (tot_len) */
39#define IP_MAX_MTU 0xFFFFU
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define RTO_ONLINK 0x01
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define RT_CONN_FLAGS(sk) (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
Francesco Fuscoaa661582013-09-24 15:43:09 +020044#define RT_CONN_FLAGS_TOS(sk,tos) (RT_TOS(tos) | sock_flag(sk, SOCK_LOCALROUTE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Guillaume Nault67e1e2f2022-04-21 01:21:33 +020046static inline __u8 ip_sock_rt_scope(const struct sock *sk)
47{
48 if (sock_flag(sk, SOCK_LOCALROUTE))
49 return RT_SCOPE_LINK;
50
51 return RT_SCOPE_UNIVERSE;
52}
53
54static inline __u8 ip_sock_rt_tos(const struct sock *sk)
55{
56 return RT_TOS(inet_sk(sk)->tos);
57}
58
Jakub Kicinskib6459412021-12-28 16:49:13 -080059struct ip_tunnel_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct fib_nh;
David S. Miller62fa8a82011-01-26 20:51:05 -080061struct fib_info;
Eric Dumazet5055c372015-01-14 15:17:06 -080062struct uncached_list;
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000063struct rtable {
Changli Gaod8d1f302010-06-10 23:31:35 -070064 struct dst_entry dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric Dumazet29e75252008-01-31 17:05:09 -080066 int rt_genid;
Eric Dumazet95c96172012-04-15 05:58:06 +000067 unsigned int rt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 __u16 rt_type;
Julian Anastasov155e8332012-10-08 11:41:18 +000069 __u8 rt_is_input;
David Ahern77d5bc72019-09-17 10:39:49 -070070 __u8 rt_uses_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int rt_iif;
73
David Ahern77d5bc72019-09-17 10:39:49 -070074 u8 rt_gw_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* Info on neighbour */
David Ahern0f5f7d72019-04-05 16:30:29 -070076 union {
77 __be32 rt_gw4;
78 struct in6_addr rt_gw6;
79 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Miscellaneous cached information */
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +010082 u32 rt_mtu_locked:1,
83 rt_pmtu:31;
David S. Millercaacf052012-07-31 15:06:50 -070084
85 struct list_head rt_uncached;
Eric Dumazet5055c372015-01-14 15:17:06 -080086 struct uncached_list *rt_uncached_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Steffen Klassertb8400f32011-11-23 02:14:15 +000089static inline bool rt_is_input_route(const struct rtable *rt)
David S. Millerc7537962010-11-11 17:07:48 -080090{
David S. Miller9917e1e82012-07-17 14:44:26 -070091 return rt->rt_is_input != 0;
David S. Millerc7537962010-11-11 17:07:48 -080092}
93
Steffen Klassertb8400f32011-11-23 02:14:15 +000094static inline bool rt_is_output_route(const struct rtable *rt)
David S. Millerc7537962010-11-11 17:07:48 -080095{
David S. Miller9917e1e82012-07-17 14:44:26 -070096 return rt->rt_is_input == 0;
David S. Millerc7537962010-11-11 17:07:48 -080097}
98
David S. Millerf8126f12012-07-13 05:03:45 -070099static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr)
100{
David Ahern1550c172019-04-05 16:30:27 -0700101 if (rt->rt_gw_family == AF_INET)
102 return rt->rt_gw4;
David S. Millerf8126f12012-07-13 05:03:45 -0700103 return daddr;
104}
105
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000106struct ip_rt_acct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 __u32 o_bytes;
108 __u32 o_packets;
109 __u32 i_bytes;
110 __u32 i_packets;
111};
112
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000113struct rt_cache_stat {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int in_slow_tot;
115 unsigned int in_slow_mc;
116 unsigned int in_no_route;
117 unsigned int in_brd;
118 unsigned int in_martian_dst;
119 unsigned int in_martian_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int out_slow_tot;
121 unsigned int out_slow_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Tejun Heo7d720c32010-02-16 15:20:26 +0000124extern struct ip_rt_acct __percpu *ip_rt_acct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126struct in_device;
Joe Perches2702c4bb2013-09-22 10:32:22 -0700127
128int ip_rt_init(void);
129void rt_cache_flush(struct net *net);
130void rt_flush_dev(struct net_device *dev);
David Ahern3abd1ade2017-05-25 10:42:33 -0700131struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *flp,
132 const struct sk_buff *skb);
133struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *flp,
134 struct fib_result *res,
135 const struct sk_buff *skb);
Peter Nørlund79a13152015-09-30 10:12:22 +0200136
137static inline struct rtable *__ip_route_output_key(struct net *net,
138 struct flowi4 *flp)
139{
David Ahern3abd1ade2017-05-25 10:42:33 -0700140 return ip_route_output_key_hash(net, flp, NULL);
Peter Nørlund79a13152015-09-30 10:12:22 +0200141}
142
Joe Perches2702c4bb2013-09-22 10:32:22 -0700143struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,
Eric Dumazet6f9c9612015-09-25 07:39:10 -0700144 const struct sock *sk);
Martin Varghese571912c2020-02-24 10:57:50 +0530145struct rtable *ip_route_output_tunnel(struct sk_buff *skb,
146 struct net_device *dev,
147 struct net *net, __be32 *saddr,
148 const struct ip_tunnel_info *info,
149 u8 protocol, bool use_cache);
150
Joe Perches2702c4bb2013-09-22 10:32:22 -0700151struct dst_entry *ipv4_blackhole_route(struct net *net,
152 struct dst_entry *dst_orig);
Eric Dumazet407eadd2010-05-10 11:32:55 +0000153
David S. Miller9d6ec932011-03-12 01:12:47 -0500154static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp)
David S. Miller5bfa7872011-03-02 14:56:30 -0800155{
156 return ip_route_output_flow(net, flp, NULL);
157}
158
David S. Miller78fbfd82011-03-12 00:00:52 -0500159static inline struct rtable *ip_route_output(struct net *net, __be32 daddr,
160 __be32 saddr, u8 tos, int oif)
161{
David S. Miller9d6ec932011-03-12 01:12:47 -0500162 struct flowi4 fl4 = {
163 .flowi4_oif = oif,
Roland Dreierc5d21c42012-06-10 20:05:24 +0000164 .flowi4_tos = tos,
David S. Miller9d6ec932011-03-12 01:12:47 -0500165 .daddr = daddr,
166 .saddr = saddr,
David S. Miller78fbfd82011-03-12 00:00:52 -0500167 };
David S. Miller9d6ec932011-03-12 01:12:47 -0500168 return ip_route_output_key(net, &fl4);
David S. Miller78fbfd82011-03-12 00:00:52 -0500169}
170
David S. Miller31e4543d2011-05-03 20:25:42 -0700171static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4,
172 struct sock *sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500173 __be32 daddr, __be32 saddr,
174 __be16 dport, __be16 sport,
175 __u8 proto, __u8 tos, int oif)
176{
David S. Miller31e4543d2011-05-03 20:25:42 -0700177 flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos,
David S. Miller94b92b882011-03-31 04:52:59 -0700178 RT_SCOPE_UNIVERSE, proto,
179 sk ? inet_sk_flowi_flags(sk) : 0,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900180 daddr, saddr, dport, sport, sock_net_uid(net, sk));
David S. Miller78fbfd82011-03-12 00:00:52 -0500181 if (sk)
Paul Moore3df98d72020-09-27 22:38:26 -0400182 security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
David S. Miller31e4543d2011-05-03 20:25:42 -0700183 return ip_route_output_flow(net, fl4, sk);
David S. Miller78fbfd82011-03-12 00:00:52 -0500184}
185
David S. Millercbb1e852011-05-04 12:33:34 -0700186static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4,
David S. Miller78fbfd82011-03-12 00:00:52 -0500187 __be32 daddr, __be32 saddr,
188 __be32 gre_key, __u8 tos, int oif)
189{
David S. Millercbb1e852011-05-04 12:33:34 -0700190 memset(fl4, 0, sizeof(*fl4));
191 fl4->flowi4_oif = oif;
192 fl4->daddr = daddr;
193 fl4->saddr = saddr;
194 fl4->flowi4_tos = tos;
195 fl4->flowi4_proto = IPPROTO_GRE;
196 fl4->fl4_gre_key = gre_key;
197 return ip_route_output_key(net, fl4);
David S. Miller78fbfd82011-03-12 00:00:52 -0500198}
Paolo Abenibc044e82017-09-28 15:51:37 +0200199int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
200 u8 tos, struct net_device *dev,
201 struct in_device *in_dev, u32 *itag);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700202int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src,
203 u8 tos, struct net_device *devin);
David Ahern5510cdf2017-05-25 10:42:34 -0700204int ip_route_input_rcu(struct sk_buff *skb, __be32 dst, __be32 src,
205 u8 tos, struct net_device *devin,
206 struct fib_result *res);
David S. Millerc6cffba2012-07-26 11:14:38 +0000207
Paolo Abeni02b24942019-11-20 13:47:37 +0100208int ip_route_use_hint(struct sk_buff *skb, __be32 dst, __be32 src,
209 u8 tos, struct net_device *devin,
210 const struct sk_buff *hint);
211
David S. Millerc6cffba2012-07-26 11:14:38 +0000212static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,
213 u8 tos, struct net_device *devin)
214{
215 int err;
216
217 rcu_read_lock();
218 err = ip_route_input_noref(skb, dst, src, tos, devin);
Stefano Brivio64327fc2017-08-31 18:11:41 +0200219 if (!err) {
Eric Dumazet222d7dbd2017-09-21 09:15:46 -0700220 skb_dst_force(skb);
Stefano Brivio64327fc2017-08-31 18:11:41 +0200221 if (!skb_dst(skb))
222 err = -EINVAL;
223 }
David S. Millerc6cffba2012-07-26 11:14:38 +0000224 rcu_read_unlock();
225
226 return err;
227}
Eric Dumazet407eadd2010-05-10 11:32:55 +0000228
Joe Perches2702c4bb2013-09-22 10:32:22 -0700229void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, int oif,
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700230 u8 protocol);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700231void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu);
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700232void ipv4_redirect(struct sk_buff *skb, struct net *net, int oif, u8 protocol);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700233void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk);
234void ip_rt_send_redirect(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Joe Perches2702c4bb2013-09-22 10:32:22 -0700236unsigned int inet_addr_type(struct net *net, __be32 addr);
David Ahern9b8ff512015-09-01 14:26:35 -0600237unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700238unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
239 __be32 addr);
David Ahern30bbaa12015-08-13 14:59:05 -0600240unsigned int inet_addr_type_dev_table(struct net *net,
241 const struct net_device *dev,
242 __be32 addr);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700243void ip_rt_multicast_event(struct in_device *);
Al Viroca25c302017-07-01 08:03:10 -0400244int ip_rt_ioctl(struct net *, unsigned int cmd, struct rtentry *rt);
Joe Perches2702c4bb2013-09-22 10:32:22 -0700245void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
David Ahern9ab179d2016-04-07 11:10:06 -0700246struct rtable *rt_dst_alloc(struct net_device *dev,
247 unsigned int flags, u16 type,
David Laightaf13b3c2020-03-23 14:31:19 +0000248 bool nopolicy, bool noxfrm);
Stephen Suryaputra5b18f122019-06-26 02:21:16 -0400249struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800251struct in_ifaddr;
Joe Perches2702c4bb2013-09-22 10:32:22 -0700252void fib_add_ifaddr(struct in_ifaddr *);
253void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *);
David Ahernaf4d7682018-05-27 08:09:57 -0700254void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800255
Xin Long510c3212018-02-14 19:06:02 +0800256void rt_add_uncached_list(struct rtable *rt);
257void rt_del_uncached_list(struct rtable *rt);
258
Stefano Brivioee289062019-06-21 17:45:23 +0200259int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
260 u32 table_id, struct fib_info *fi,
John Fastabende93fb3e2019-08-23 17:11:38 -0700261 int *fa_index, int fa_start, unsigned int flags);
Stefano Brivioee289062019-06-21 17:45:23 +0200262
Eric Dumazet6da025f2012-10-28 22:33:23 +0000263static inline void ip_rt_put(struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Eric Dumazet6da025f2012-10-28 22:33:23 +0000265 /* dst_release() accepts a NULL parameter.
266 * We rely on dst being first structure in struct rtable
267 */
268 BUILD_BUG_ON(offsetof(struct rtable, dst) != 0);
269 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272#define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3)
273
Philippe De Muyter4839c522007-07-09 15:32:57 -0700274extern const __u8 ip_tos2prio[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276static inline char rt_tos2priority(u8 tos)
277{
278 return ip_tos2prio[IPTOS_TOS(tos)>>1];
279}
280
David S. Miller2d7192d2011-04-26 13:28:44 -0700281/* ip_route_connect() and ip_route_newports() work in tandem whilst
282 * binding a socket for a new outgoing connection.
283 *
284 * In order to use IPSEC properly, we must, in the end, have a
285 * route that was looked up using all available keys including source
286 * and destination ports.
287 *
288 * However, if a source port needs to be allocated (the user specified
289 * a wildcard source port) we need to obtain addressing information
290 * in order to perform that allocation.
291 *
292 * So ip_route_connect() looks up a route using wildcarded source and
293 * destination ports in the key, simply so that we can get a pair of
294 * addresses to use for port allocation.
295 *
296 * Later, once the ports are allocated, ip_route_newports() will make
297 * another route lookup if needed to make sure we catch any IPSEC
298 * rules keyed on the port information.
299 *
300 * The callers allocate the flow key on their stack, and must pass in
301 * the same flowi4 object to both the ip_route_connect() and the
302 * ip_route_newports() calls.
303 */
KOVACS Krisztian79876872008-10-01 07:35:39 -0700304
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200305static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst,
306 __be32 src, int oif, u8 protocol,
David S. Miller2d7192d2011-04-26 13:28:44 -0700307 __be16 sport, __be16 dport,
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200308 const struct sock *sk)
David S. Miller2d7192d2011-04-26 13:28:44 -0700309{
310 __u8 flow_flags = 0;
311
KOVACS Krisztian79876872008-10-01 07:35:39 -0700312 if (inet_sk(sk)->transparent)
David S. Miller94b92b882011-03-31 04:52:59 -0700313 flow_flags |= FLOWI_FLAG_ANYSRC;
David S. Miller94b92b882011-03-31 04:52:59 -0700314
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200315 flowi4_init_output(fl4, oif, sk->sk_mark, ip_sock_rt_tos(sk),
316 ip_sock_rt_scope(sk), protocol, flow_flags, dst,
317 src, dport, sport, sk->sk_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200320static inline struct rtable *ip_route_connect(struct flowi4 *fl4, __be32 dst,
321 __be32 src, int oif, u8 protocol,
David S. Miller2d7192d2011-04-26 13:28:44 -0700322 __be16 sport, __be16 dport,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +0200323 struct sock *sk)
David S. Miller2d7192d2011-04-26 13:28:44 -0700324{
325 struct net *net = sock_net(sk);
326 struct rtable *rt;
327
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200328 ip_route_connect_init(fl4, dst, src, oif, protocol, sport, dport, sk);
David S. Miller2d7192d2011-04-26 13:28:44 -0700329
330 if (!dst || !src) {
331 rt = __ip_route_output_key(net, fl4);
332 if (IS_ERR(rt))
333 return rt;
David S. Miller2d7192d2011-04-26 13:28:44 -0700334 ip_rt_put(rt);
Guillaume Nault67e1e2f2022-04-21 01:21:33 +0200335 flowi4_update_output(fl4, oif, fl4->flowi4_tos, fl4->daddr,
336 fl4->saddr);
David S. Miller2d7192d2011-04-26 13:28:44 -0700337 }
Paul Moore3df98d72020-09-27 22:38:26 -0400338 security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
David S. Miller2d7192d2011-04-26 13:28:44 -0700339 return ip_route_output_flow(net, fl4, sk);
340}
341
342static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt,
343 __be16 orig_sport, __be16 orig_dport,
344 __be16 sport, __be16 dport,
345 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David S. Millerdca8b082011-02-24 13:38:12 -0800347 if (sport != orig_sport || dport != orig_dport) {
David S. Miller2d7192d2011-04-26 13:28:44 -0700348 fl4->fl4_dport = dport;
349 fl4->fl4_sport = sport;
David S. Millerb23dd4f2011-03-02 14:31:35 -0800350 ip_rt_put(rt);
Julian Anastasove6b45242012-02-04 13:04:46 +0000351 flowi4_update_output(fl4, sk->sk_bound_dev_if,
352 RT_CONN_FLAGS(sk), fl4->daddr,
353 fl4->saddr);
Paul Moore3df98d72020-09-27 22:38:26 -0400354 security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
David S. Miller2d7192d2011-04-26 13:28:44 -0700355 return ip_route_output_flow(sock_net(sk), fl4, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800357 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
KOVACS Krisztian1668e012008-10-01 07:33:10 -0700360static inline int inet_iif(const struct sk_buff *skb)
361{
Tom Herbert0340d0b2016-04-05 08:22:49 -0700362 struct rtable *rt = skb_rtable(skb);
David S. Miller13378ca2012-07-23 13:57:45 -0700363
Tom Herbert0340d0b2016-04-05 08:22:49 -0700364 if (rt && rt->rt_iif)
365 return rt->rt_iif;
366
David S. Miller13378ca2012-07-23 13:57:45 -0700367 return skb->skb_iif;
KOVACS Krisztian1668e012008-10-01 07:33:10 -0700368}
369
David S. Miller323e1262010-12-12 21:55:08 -0800370static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
371{
372 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
Nikolay Borisovfa50d972016-02-15 12:11:27 +0200373 struct net *net = dev_net(dst->dev);
David S. Miller323e1262010-12-12 21:55:08 -0800374
375 if (hoplimit == 0)
Kuniyuki Iwashima8281b7e2022-07-13 13:51:51 -0700376 hoplimit = READ_ONCE(net->ipv4.sysctl_ip_default_ttl);
David S. Miller323e1262010-12-12 21:55:08 -0800377 return hoplimit;
378}
379
David Ahern5c9f7c12019-04-05 16:30:34 -0700380static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
381 __be32 daddr)
382{
383 struct neighbour *neigh;
384
Eric Dumazet3c42b202022-01-26 17:34:04 -0800385 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr);
David Ahern5c9f7c12019-04-05 16:30:34 -0700386 if (unlikely(!neigh))
387 neigh = __neigh_create(&arp_tbl, &daddr, dev, false);
388
389 return neigh;
390}
391
392static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
393 struct sk_buff *skb,
394 bool *is_v6gw)
395{
396 struct net_device *dev = rt->dst.dev;
397 struct neighbour *neigh;
398
399 if (likely(rt->rt_gw_family == AF_INET)) {
400 neigh = ip_neigh_gw4(dev, rt->rt_gw4);
401 } else if (rt->rt_gw_family == AF_INET6) {
402 neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
403 *is_v6gw = true;
404 } else {
405 neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
406 }
407 return neigh;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#endif /* _ROUTE_H */