1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
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 module.
8  *
9  * Version:	@(#)ip.h	1.0.2	05/07/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
14  *
15  * Changes:
16  *		Mike McLagan    :       Routing by source
17  */
18 #ifndef _IP_H
19 #define _IP_H
20 
21 #include <linux/types.h>
22 #include <linux/ip.h>
23 #include <linux/in.h>
24 #include <linux/skbuff.h>
25 #include <linux/jhash.h>
26 #include <linux/sockptr.h>
27 #include <linux/static_key.h>
28 #include <linux/android_kabi.h>
29 
30 #include <net/inet_sock.h>
31 #include <net/route.h>
32 #include <net/snmp.h>
33 #include <net/flow.h>
34 #include <net/flow_dissector.h>
35 #include <net/netns/hash.h>
36 #include <net/lwtunnel.h>
37 #include <net/inet_dscp.h>
38 
39 #define IPV4_MAX_PMTU		65535U		/* RFC 2675, Section 5.1 */
40 #define IPV4_MIN_MTU		68			/* RFC 791 */
41 
42 extern unsigned int sysctl_fib_sync_mem;
43 extern unsigned int sysctl_fib_sync_mem_min;
44 extern unsigned int sysctl_fib_sync_mem_max;
45 
46 struct sock;
47 
48 struct inet_skb_parm {
49 	int			iif;
50 	struct ip_options	opt;		/* Compiled IP options		*/
51 	u16			flags;
52 
53 #define IPSKB_FORWARDED		BIT(0)
54 #define IPSKB_XFRM_TUNNEL_SIZE	BIT(1)
55 #define IPSKB_XFRM_TRANSFORMED	BIT(2)
56 #define IPSKB_FRAG_COMPLETE	BIT(3)
57 #define IPSKB_REROUTED		BIT(4)
58 #define IPSKB_DOREDIRECT	BIT(5)
59 #define IPSKB_FRAG_PMTU		BIT(6)
60 #define IPSKB_L3SLAVE		BIT(7)
61 #define IPSKB_NOPOLICY		BIT(8)
62 #define IPSKB_MULTIPATH		BIT(9)
63 
64 	u16			frag_max_size;
65 };
66 
ipv4_l3mdev_skb(u16 flags)67 static inline bool ipv4_l3mdev_skb(u16 flags)
68 {
69 	return !!(flags & IPSKB_L3SLAVE);
70 }
71 
ip_hdrlen(const struct sk_buff * skb)72 static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
73 {
74 	return ip_hdr(skb)->ihl * 4;
75 }
76 
77 struct ipcm_cookie {
78 	struct sockcm_cookie	sockc;
79 	__be32			addr;
80 	int			oif;
81 	struct ip_options_rcu	*opt;
82 	__u8			protocol;
83 	__u8			ttl;
84 	__s16			tos;
85 	char			priority;
86 	__u16			gso_size;
87 	ANDROID_KABI_RESERVE(1);
88 };
89 
ipcm_init(struct ipcm_cookie * ipcm)90 static inline void ipcm_init(struct ipcm_cookie *ipcm)
91 {
92 	*ipcm = (struct ipcm_cookie) { .tos = -1 };
93 }
94 
ipcm_init_sk(struct ipcm_cookie * ipcm,const struct inet_sock * inet)95 static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
96 				const struct inet_sock *inet)
97 {
98 	ipcm_init(ipcm);
99 
100 	ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark);
101 	ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags);
102 	ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if);
103 	ipcm->addr = inet->inet_saddr;
104 	ipcm->protocol = inet->inet_num;
105 }
106 
107 #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
108 #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
109 
110 /* return enslaved device index if relevant */
inet_sdif(const struct sk_buff * skb)111 static inline int inet_sdif(const struct sk_buff *skb)
112 {
113 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
114 	if (skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
115 		return IPCB(skb)->iif;
116 #endif
117 	return 0;
118 }
119 
120 /* Special input handler for packets caught by router alert option.
121    They are selected only by protocol field, and then processed likely
122    local ones; but only if someone wants them! Otherwise, router
123    not running rsvpd will kill RSVP.
124 
125    It is user level problem, what it will make with them.
126    I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
127    but receiver should be enough clever f.e. to forward mtrace requests,
128    sent to multicast group to reach destination designated router.
129  */
130 
131 struct ip_ra_chain {
132 	struct ip_ra_chain __rcu *next;
133 	struct sock		*sk;
134 	union {
135 		void			(*destructor)(struct sock *);
136 		struct sock		*saved_sk;
137 	};
138 	struct rcu_head		rcu;
139 };
140 
141 /* IP flags. */
142 #define IP_CE		0x8000		/* Flag: "Congestion"		*/
143 #define IP_DF		0x4000		/* Flag: "Don't Fragment"	*/
144 #define IP_MF		0x2000		/* Flag: "More Fragments"	*/
145 #define IP_OFFSET	0x1FFF		/* "Fragment Offset" part	*/
146 
147 #define IP_FRAG_TIME	(30 * HZ)		/* fragment lifetime	*/
148 
149 struct msghdr;
150 struct net_device;
151 struct packet_type;
152 struct rtable;
153 struct sockaddr;
154 
155 int igmp_mc_init(void);
156 
157 /*
158  *	Functions provided by ip.c
159  */
160 
161 int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
162 			  __be32 saddr, __be32 daddr,
163 			  struct ip_options_rcu *opt, u8 tos);
164 int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
165 	   struct net_device *orig_dev);
166 void ip_list_rcv(struct list_head *head, struct packet_type *pt,
167 		 struct net_device *orig_dev);
168 int ip_local_deliver(struct sk_buff *skb);
169 void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int proto);
170 int ip_mr_input(struct sk_buff *skb);
171 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
172 int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
173 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
174 		   int (*output)(struct net *, struct sock *, struct sk_buff *));
175 
176 struct ip_fraglist_iter {
177 	struct sk_buff	*frag;
178 	struct iphdr	*iph;
179 	int		offset;
180 	unsigned int	hlen;
181 };
182 
183 void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph,
184 		      unsigned int hlen, struct ip_fraglist_iter *iter);
185 void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter);
186 
ip_fraglist_next(struct ip_fraglist_iter * iter)187 static inline struct sk_buff *ip_fraglist_next(struct ip_fraglist_iter *iter)
188 {
189 	struct sk_buff *skb = iter->frag;
190 
191 	iter->frag = skb->next;
192 	skb_mark_not_on_list(skb);
193 
194 	return skb;
195 }
196 
197 struct ip_frag_state {
198 	bool		DF;
199 	unsigned int	hlen;
200 	unsigned int	ll_rs;
201 	unsigned int	mtu;
202 	unsigned int	left;
203 	int		offset;
204 	int		ptr;
205 	__be16		not_last_frag;
206 };
207 
208 void ip_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int ll_rs,
209 		  unsigned int mtu, bool DF, struct ip_frag_state *state);
210 struct sk_buff *ip_frag_next(struct sk_buff *skb,
211 			     struct ip_frag_state *state);
212 
213 void ip_send_check(struct iphdr *ip);
214 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
215 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
216 
217 int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
218 		    __u8 tos);
219 void ip_init(void);
220 int ip_append_data(struct sock *sk, struct flowi4 *fl4,
221 		   int getfrag(void *from, char *to, int offset, int len,
222 			       int odd, struct sk_buff *skb),
223 		   void *from, int len, int protolen,
224 		   struct ipcm_cookie *ipc,
225 		   struct rtable **rt,
226 		   unsigned int flags);
227 int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd,
228 		       struct sk_buff *skb);
229 struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4,
230 			      struct sk_buff_head *queue,
231 			      struct inet_cork *cork);
232 int ip_send_skb(struct net *net, struct sk_buff *skb);
233 int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4);
234 void ip_flush_pending_frames(struct sock *sk);
235 struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4,
236 			    int getfrag(void *from, char *to, int offset,
237 					int len, int odd, struct sk_buff *skb),
238 			    void *from, int length, int transhdrlen,
239 			    struct ipcm_cookie *ipc, struct rtable **rtp,
240 			    struct inet_cork *cork, unsigned int flags);
241 
242 int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
243 
ip_finish_skb(struct sock * sk,struct flowi4 * fl4)244 static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4)
245 {
246 	return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
247 }
248 
249 /* Get the route scope that should be used when sending a packet. */
ip_sendmsg_scope(const struct inet_sock * inet,const struct ipcm_cookie * ipc,const struct msghdr * msg)250 static inline u8 ip_sendmsg_scope(const struct inet_sock *inet,
251 				  const struct ipcm_cookie *ipc,
252 				  const struct msghdr *msg)
253 {
254 	if (sock_flag(&inet->sk, SOCK_LOCALROUTE) ||
255 	    msg->msg_flags & MSG_DONTROUTE ||
256 	    (ipc->opt && ipc->opt->opt.is_strictroute))
257 		return RT_SCOPE_LINK;
258 
259 	return RT_SCOPE_UNIVERSE;
260 }
261 
get_rttos(struct ipcm_cookie * ipc,struct inet_sock * inet)262 static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
263 {
264 	u8 dsfield = ipc->tos != -1 ? ipc->tos : READ_ONCE(inet->tos);
265 
266 	return dsfield & INET_DSCP_MASK;
267 }
268 
269 /* datagram.c */
270 int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
271 int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
272 
273 void ip4_datagram_release_cb(struct sock *sk);
274 
275 struct ip_reply_arg {
276 	struct kvec iov[1];
277 	int	    flags;
278 	__wsum 	    csum;
279 	int	    csumoffset; /* u16 offset of csum in iov[0].iov_base */
280 				/* -1 if not needed */
281 	int	    bound_dev_if;
282 	u8  	    tos;
283 	kuid_t	    uid;
284 };
285 
286 #define IP_REPLY_ARG_NOSRCCHECK 1
287 
ip_reply_arg_flowi_flags(const struct ip_reply_arg * arg)288 static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
289 {
290 	return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
291 }
292 
293 void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
294 			   const struct ip_options *sopt,
295 			   __be32 daddr, __be32 saddr,
296 			   const struct ip_reply_arg *arg,
297 			   unsigned int len, u64 transmit_time, u32 txhash);
298 
299 #define IP_INC_STATS(net, field)	SNMP_INC_STATS64((net)->mib.ip_statistics, field)
300 #define __IP_INC_STATS(net, field)	__SNMP_INC_STATS64((net)->mib.ip_statistics, field)
301 #define IP_ADD_STATS(net, field, val)	SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
302 #define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
303 #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
304 #define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
305 #define NET_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.net_statistics, field)
306 #define __NET_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.net_statistics, field)
307 #define NET_ADD_STATS(net, field, adnd)	SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
308 #define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
309 
snmp_get_cpu_field(void __percpu * mib,int cpu,int offt)310 static inline u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
311 {
312 	return  *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
313 }
314 
315 unsigned long snmp_fold_field(void __percpu *mib, int offt);
316 #if BITS_PER_LONG==32
317 u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
318 			 size_t syncp_offset);
319 u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off);
320 #else
snmp_get_cpu_field64(void __percpu * mib,int cpu,int offct,size_t syncp_offset)321 static inline u64  snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
322 					size_t syncp_offset)
323 {
324 	return snmp_get_cpu_field(mib, cpu, offct);
325 
326 }
327 
snmp_fold_field64(void __percpu * mib,int offt,size_t syncp_off)328 static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off)
329 {
330 	return snmp_fold_field(mib, offt);
331 }
332 #endif
333 
334 #define snmp_get_cpu_field64_batch(buff64, stats_list, mib_statistic, offset) \
335 { \
336 	int i, c; \
337 	for_each_possible_cpu(c) { \
338 		for (i = 0; stats_list[i].name; i++) \
339 			buff64[i] += snmp_get_cpu_field64( \
340 					mib_statistic, \
341 					c, stats_list[i].entry, \
342 					offset); \
343 	} \
344 }
345 
346 #define snmp_get_cpu_field_batch(buff, stats_list, mib_statistic) \
347 { \
348 	int i, c; \
349 	for_each_possible_cpu(c) { \
350 		for (i = 0; stats_list[i].name; i++) \
351 			buff[i] += snmp_get_cpu_field( \
352 						mib_statistic, \
353 						c, stats_list[i].entry); \
354 	} \
355 }
356 
inet_get_local_port_range(const struct net * net,int * low,int * high)357 static inline void inet_get_local_port_range(const struct net *net, int *low, int *high)
358 {
359 	u32 range = READ_ONCE(net->ipv4.ip_local_ports.range);
360 
361 	*low = range & 0xffff;
362 	*high = range >> 16;
363 }
364 bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
365 
366 #ifdef CONFIG_SYSCTL
inet_is_local_reserved_port(struct net * net,unsigned short port)367 static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
368 {
369 	if (!net->ipv4.sysctl_local_reserved_ports)
370 		return false;
371 	return test_bit(port, net->ipv4.sysctl_local_reserved_ports);
372 }
373 
sysctl_dev_name_is_allowed(const char * name)374 static inline bool sysctl_dev_name_is_allowed(const char *name)
375 {
376 	return strcmp(name, "default") != 0  && strcmp(name, "all") != 0;
377 }
378 
inet_port_requires_bind_service(struct net * net,unsigned short port)379 static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
380 {
381 	return port < READ_ONCE(net->ipv4.sysctl_ip_prot_sock);
382 }
383 
384 #else
inet_is_local_reserved_port(struct net * net,unsigned short port)385 static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
386 {
387 	return false;
388 }
389 
inet_port_requires_bind_service(struct net * net,unsigned short port)390 static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
391 {
392 	return port < PROT_SOCK;
393 }
394 #endif
395 
396 __be32 inet_current_timestamp(void);
397 
398 /* From inetpeer.c */
399 extern int inet_peer_threshold;
400 extern int inet_peer_minttl;
401 extern int inet_peer_maxttl;
402 
403 void ipfrag_init(void);
404 
405 void ip_static_sysctl_init(void);
406 
407 #define IP4_REPLY_MARK(net, mark) \
408 	(READ_ONCE((net)->ipv4.sysctl_fwmark_reflect) ? (mark) : 0)
409 
ip_is_fragment(const struct iphdr * iph)410 static inline bool ip_is_fragment(const struct iphdr *iph)
411 {
412 	return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0;
413 }
414 
415 #ifdef CONFIG_INET
416 #include <net/dst.h>
417 
418 /* The function in 2.2 was invalid, producing wrong result for
419  * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */
420 static inline
ip_decrease_ttl(struct iphdr * iph)421 int ip_decrease_ttl(struct iphdr *iph)
422 {
423 	u32 check = (__force u32)iph->check;
424 	check += (__force u32)htons(0x0100);
425 	iph->check = (__force __sum16)(check + (check>=0xFFFF));
426 	return --iph->ttl;
427 }
428 
ip4h_dscp(const struct iphdr * ip4h)429 static inline dscp_t ip4h_dscp(const struct iphdr *ip4h)
430 {
431 	return inet_dsfield_to_dscp(ip4h->tos);
432 }
433 
ip_mtu_locked(const struct dst_entry * dst)434 static inline int ip_mtu_locked(const struct dst_entry *dst)
435 {
436 	const struct rtable *rt = dst_rtable(dst);
437 
438 	return rt->rt_mtu_locked || dst_metric_locked(dst, RTAX_MTU);
439 }
440 
441 static inline
ip_dont_fragment(const struct sock * sk,const struct dst_entry * dst)442 int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst)
443 {
444 	u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
445 
446 	return  pmtudisc == IP_PMTUDISC_DO ||
447 		(pmtudisc == IP_PMTUDISC_WANT &&
448 		 !ip_mtu_locked(dst));
449 }
450 
ip_sk_accept_pmtu(const struct sock * sk)451 static inline bool ip_sk_accept_pmtu(const struct sock *sk)
452 {
453 	u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
454 
455 	return pmtudisc != IP_PMTUDISC_INTERFACE &&
456 	       pmtudisc != IP_PMTUDISC_OMIT;
457 }
458 
ip_sk_use_pmtu(const struct sock * sk)459 static inline bool ip_sk_use_pmtu(const struct sock *sk)
460 {
461 	return READ_ONCE(inet_sk(sk)->pmtudisc) < IP_PMTUDISC_PROBE;
462 }
463 
ip_sk_ignore_df(const struct sock * sk)464 static inline bool ip_sk_ignore_df(const struct sock *sk)
465 {
466 	u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
467 
468 	return pmtudisc < IP_PMTUDISC_DO || pmtudisc == IP_PMTUDISC_OMIT;
469 }
470 
ip_dst_mtu_maybe_forward(const struct dst_entry * dst,bool forwarding)471 static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst,
472 						    bool forwarding)
473 {
474 	const struct rtable *rt = dst_rtable(dst);
475 	unsigned int mtu, res;
476 	struct net *net;
477 
478 	rcu_read_lock();
479 
480 	net = dev_net_rcu(dst->dev);
481 	if (READ_ONCE(net->ipv4.sysctl_ip_fwd_use_pmtu) ||
482 	    ip_mtu_locked(dst) ||
483 	    !forwarding) {
484 		mtu = rt->rt_pmtu;
485 		if (mtu && time_before(jiffies, rt->dst.expires))
486 			goto out;
487 	}
488 
489 	/* 'forwarding = true' case should always honour route mtu */
490 	mtu = dst_metric_raw(dst, RTAX_MTU);
491 	if (mtu)
492 		goto out;
493 
494 	mtu = READ_ONCE(dst->dev->mtu);
495 
496 	if (unlikely(ip_mtu_locked(dst))) {
497 		if (rt->rt_uses_gateway && mtu > 576)
498 			mtu = 576;
499 	}
500 
501 out:
502 	mtu = min_t(unsigned int, mtu, IP_MAX_MTU);
503 
504 	res = mtu - lwtunnel_headroom(dst->lwtstate, mtu);
505 
506 	rcu_read_unlock();
507 
508 	return res;
509 }
510 
ip_skb_dst_mtu(struct sock * sk,const struct sk_buff * skb)511 static inline unsigned int ip_skb_dst_mtu(struct sock *sk,
512 					  const struct sk_buff *skb)
513 {
514 	unsigned int mtu;
515 
516 	if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) {
517 		bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED;
518 
519 		return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding);
520 	}
521 
522 	mtu = min(READ_ONCE(skb_dst(skb)->dev->mtu), IP_MAX_MTU);
523 	return mtu - lwtunnel_headroom(skb_dst(skb)->lwtstate, mtu);
524 }
525 
526 struct dst_metrics *ip_fib_metrics_init(struct nlattr *fc_mx, int fc_mx_len,
527 					struct netlink_ext_ack *extack);
ip_fib_metrics_put(struct dst_metrics * fib_metrics)528 static inline void ip_fib_metrics_put(struct dst_metrics *fib_metrics)
529 {
530 	if (fib_metrics != &dst_default_metrics &&
531 	    refcount_dec_and_test(&fib_metrics->refcnt))
532 		kfree(fib_metrics);
533 }
534 
535 /* ipv4 and ipv6 both use refcounted metrics if it is not the default */
536 static inline
ip_dst_init_metrics(struct dst_entry * dst,struct dst_metrics * fib_metrics)537 void ip_dst_init_metrics(struct dst_entry *dst, struct dst_metrics *fib_metrics)
538 {
539 	dst_init_metrics(dst, fib_metrics->metrics, true);
540 
541 	if (fib_metrics != &dst_default_metrics) {
542 		dst->_metrics |= DST_METRICS_REFCOUNTED;
543 		refcount_inc(&fib_metrics->refcnt);
544 	}
545 }
546 
547 static inline
ip_dst_metrics_put(struct dst_entry * dst)548 void ip_dst_metrics_put(struct dst_entry *dst)
549 {
550 	struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
551 
552 	if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
553 		kfree(p);
554 }
555 
556 void __ip_select_ident(struct net *net, struct iphdr *iph, int segs);
557 
ip_select_ident_segs(struct net * net,struct sk_buff * skb,struct sock * sk,int segs)558 static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb,
559 					struct sock *sk, int segs)
560 {
561 	struct iphdr *iph = ip_hdr(skb);
562 
563 	/* We had many attacks based on IPID, use the private
564 	 * generator as much as we can.
565 	 */
566 	if (sk && inet_sk(sk)->inet_daddr) {
567 		int val;
568 
569 		/* avoid atomic operations for TCP,
570 		 * as we hold socket lock at this point.
571 		 */
572 		if (sk_is_tcp(sk)) {
573 			sock_owned_by_me(sk);
574 			val = atomic_read(&inet_sk(sk)->inet_id);
575 			atomic_set(&inet_sk(sk)->inet_id, val + segs);
576 		} else {
577 			val = atomic_add_return(segs, &inet_sk(sk)->inet_id);
578 		}
579 		iph->id = htons(val);
580 		return;
581 	}
582 	if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) {
583 		iph->id = 0;
584 	} else {
585 		/* Unfortunately we need the big hammer to get a suitable IPID */
586 		__ip_select_ident(net, iph, segs);
587 	}
588 }
589 
ip_select_ident(struct net * net,struct sk_buff * skb,struct sock * sk)590 static inline void ip_select_ident(struct net *net, struct sk_buff *skb,
591 				   struct sock *sk)
592 {
593 	ip_select_ident_segs(net, skb, sk, 1);
594 }
595 
inet_compute_pseudo(struct sk_buff * skb,int proto)596 static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto)
597 {
598 	return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
599 				  skb->len, proto, 0);
600 }
601 
602 /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store
603  * Equivalent to :	flow->v4addrs.src = iph->saddr;
604  *			flow->v4addrs.dst = iph->daddr;
605  */
iph_to_flow_copy_v4addrs(struct flow_keys * flow,const struct iphdr * iph)606 static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
607 					    const struct iphdr *iph)
608 {
609 	BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) !=
610 		     offsetof(typeof(flow->addrs), v4addrs.src) +
611 			      sizeof(flow->addrs.v4addrs.src));
612 	memcpy(&flow->addrs.v4addrs, &iph->addrs, sizeof(flow->addrs.v4addrs));
613 	flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
614 }
615 
616 /*
617  *	Map a multicast IP onto multicast MAC for type ethernet.
618  */
619 
ip_eth_mc_map(__be32 naddr,char * buf)620 static inline void ip_eth_mc_map(__be32 naddr, char *buf)
621 {
622 	__u32 addr=ntohl(naddr);
623 	buf[0]=0x01;
624 	buf[1]=0x00;
625 	buf[2]=0x5e;
626 	buf[5]=addr&0xFF;
627 	addr>>=8;
628 	buf[4]=addr&0xFF;
629 	addr>>=8;
630 	buf[3]=addr&0x7F;
631 }
632 
633 /*
634  *	Map a multicast IP onto multicast MAC for type IP-over-InfiniBand.
635  *	Leave P_Key as 0 to be filled in by driver.
636  */
637 
ip_ib_mc_map(__be32 naddr,const unsigned char * broadcast,char * buf)638 static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
639 {
640 	__u32 addr;
641 	unsigned char scope = broadcast[5] & 0xF;
642 
643 	buf[0]  = 0;		/* Reserved */
644 	buf[1]  = 0xff;		/* Multicast QPN */
645 	buf[2]  = 0xff;
646 	buf[3]  = 0xff;
647 	addr    = ntohl(naddr);
648 	buf[4]  = 0xff;
649 	buf[5]  = 0x10 | scope;	/* scope from broadcast address */
650 	buf[6]  = 0x40;		/* IPv4 signature */
651 	buf[7]  = 0x1b;
652 	buf[8]  = broadcast[8];		/* P_Key */
653 	buf[9]  = broadcast[9];
654 	buf[10] = 0;
655 	buf[11] = 0;
656 	buf[12] = 0;
657 	buf[13] = 0;
658 	buf[14] = 0;
659 	buf[15] = 0;
660 	buf[19] = addr & 0xff;
661 	addr  >>= 8;
662 	buf[18] = addr & 0xff;
663 	addr  >>= 8;
664 	buf[17] = addr & 0xff;
665 	addr  >>= 8;
666 	buf[16] = addr & 0x0f;
667 }
668 
ip_ipgre_mc_map(__be32 naddr,const unsigned char * broadcast,char * buf)669 static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
670 {
671 	if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0)
672 		memcpy(buf, broadcast, 4);
673 	else
674 		memcpy(buf, &naddr, sizeof(naddr));
675 }
676 
677 #if IS_ENABLED(CONFIG_IPV6)
678 #include <linux/ipv6.h>
679 #endif
680 
inet_reset_saddr(struct sock * sk)681 static __inline__ void inet_reset_saddr(struct sock *sk)
682 {
683 	inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0;
684 #if IS_ENABLED(CONFIG_IPV6)
685 	if (sk->sk_family == PF_INET6) {
686 		struct ipv6_pinfo *np = inet6_sk(sk);
687 
688 		memset(&np->saddr, 0, sizeof(np->saddr));
689 		memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
690 	}
691 #endif
692 }
693 
694 #endif
695 
ipv4_addr_hash(__be32 ip)696 static inline unsigned int ipv4_addr_hash(__be32 ip)
697 {
698 	return (__force unsigned int) ip;
699 }
700 
ipv4_portaddr_hash(const struct net * net,__be32 saddr,unsigned int port)701 static inline u32 ipv4_portaddr_hash(const struct net *net,
702 				     __be32 saddr,
703 				     unsigned int port)
704 {
705 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
706 }
707 
708 bool ip_call_ra_chain(struct sk_buff *skb);
709 
710 /*
711  *	Functions provided by ip_fragment.c
712  */
713 
714 enum ip_defrag_users {
715 	IP_DEFRAG_LOCAL_DELIVER,
716 	IP_DEFRAG_CALL_RA_CHAIN,
717 	IP_DEFRAG_CONNTRACK_IN,
718 	__IP_DEFRAG_CONNTRACK_IN_END	= IP_DEFRAG_CONNTRACK_IN + USHRT_MAX,
719 	IP_DEFRAG_CONNTRACK_OUT,
720 	__IP_DEFRAG_CONNTRACK_OUT_END	= IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
721 	IP_DEFRAG_CONNTRACK_BRIDGE_IN,
722 	__IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
723 	IP_DEFRAG_VS_IN,
724 	IP_DEFRAG_VS_OUT,
725 	IP_DEFRAG_VS_FWD,
726 	IP_DEFRAG_AF_PACKET,
727 	IP_DEFRAG_MACVLAN,
728 };
729 
730 /* Return true if the value of 'user' is between 'lower_bond'
731  * and 'upper_bond' inclusively.
732  */
ip_defrag_user_in_between(u32 user,enum ip_defrag_users lower_bond,enum ip_defrag_users upper_bond)733 static inline bool ip_defrag_user_in_between(u32 user,
734 					     enum ip_defrag_users lower_bond,
735 					     enum ip_defrag_users upper_bond)
736 {
737 	return user >= lower_bond && user <= upper_bond;
738 }
739 
740 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user);
741 #ifdef CONFIG_INET
742 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user);
743 #else
ip_check_defrag(struct net * net,struct sk_buff * skb,u32 user)744 static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
745 {
746 	return skb;
747 }
748 #endif
749 
750 /*
751  *	Functions provided by ip_forward.c
752  */
753 
754 int ip_forward(struct sk_buff *skb);
755 
756 /*
757  *	Functions provided by ip_options.c
758  */
759 
760 void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
761 		      __be32 daddr, struct rtable *rt);
762 
763 int __ip_options_echo(struct net *net, struct ip_options *dopt,
764 		      struct sk_buff *skb, const struct ip_options *sopt);
ip_options_echo(struct net * net,struct ip_options * dopt,struct sk_buff * skb)765 static inline int ip_options_echo(struct net *net, struct ip_options *dopt,
766 				  struct sk_buff *skb)
767 {
768 	return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt);
769 }
770 
771 void ip_options_fragment(struct sk_buff *skb);
772 int __ip_options_compile(struct net *net, struct ip_options *opt,
773 			 struct sk_buff *skb, __be32 *info);
774 int ip_options_compile(struct net *net, struct ip_options *opt,
775 		       struct sk_buff *skb);
776 int ip_options_get(struct net *net, struct ip_options_rcu **optp,
777 		   sockptr_t data, int optlen);
778 void ip_options_undo(struct ip_options *opt);
779 void ip_forward_options(struct sk_buff *skb);
780 int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev);
781 
782 /*
783  *	Functions provided by ip_sockglue.c
784  */
785 
786 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb, bool drop_dst);
787 void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
788 			 struct sk_buff *skb, int tlen, int offset);
789 int ip_cmsg_send(struct sock *sk, struct msghdr *msg,
790 		 struct ipcm_cookie *ipc, bool allow_ipv6);
791 DECLARE_STATIC_KEY_FALSE(ip4_min_ttl);
792 int do_ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
793 		     unsigned int optlen);
794 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
795 		  unsigned int optlen);
796 int do_ip_getsockopt(struct sock *sk, int level, int optname,
797 		     sockptr_t optval, sockptr_t optlen);
798 int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
799 		  int __user *optlen);
800 int ip_ra_control(struct sock *sk, unsigned char on,
801 		  void (*destructor)(struct sock *));
802 
803 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len);
804 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
805 		   u32 info, u8 *payload);
806 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport,
807 		    u32 info);
808 
ip_cmsg_recv(struct msghdr * msg,struct sk_buff * skb)809 static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
810 {
811 	ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0);
812 }
813 
814 bool icmp_global_allow(struct net *net);
815 void icmp_global_consume(struct net *net);
816 
817 #ifdef CONFIG_PROC_FS
818 int ip_misc_proc_init(void);
819 #endif
820 
821 int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family,
822 				struct netlink_ext_ack *extack);
823 
inetdev_valid_mtu(unsigned int mtu)824 static inline bool inetdev_valid_mtu(unsigned int mtu)
825 {
826 	return likely(mtu >= IPV4_MIN_MTU);
827 }
828 
829 void ip_sock_set_freebind(struct sock *sk);
830 int ip_sock_set_mtu_discover(struct sock *sk, int val);
831 void ip_sock_set_pktinfo(struct sock *sk);
832 void ip_sock_set_recverr(struct sock *sk);
833 void ip_sock_set_tos(struct sock *sk, int val);
834 void  __ip_sock_set_tos(struct sock *sk, int val);
835 
836 #endif	/* _IP_H */
837