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