xref: /openbmc/linux/include/net/ip.h (revision 13415e46)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the IP module.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)ip.h	1.0.2	05/07/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Changes:
151da177e4SLinus Torvalds  *		Mike McLagan    :       Routing by source
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
181da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
191da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
201da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds #ifndef _IP_H
231da177e4SLinus Torvalds #define _IP_H
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <linux/types.h>
261da177e4SLinus Torvalds #include <linux/ip.h>
271da177e4SLinus Torvalds #include <linux/in.h>
28c9bdd4b5SArnaldo Carvalho de Melo #include <linux/skbuff.h>
2914c85021SArnaldo Carvalho de Melo 
3014c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h>
31aa661581SFrancesco Fusco #include <net/route.h>
321da177e4SLinus Torvalds #include <net/snmp.h>
3386b08d86SKOVACS Krisztian #include <net/flow.h>
341bd758ebSJiri Pirko #include <net/flow_dissector.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds struct sock;
371da177e4SLinus Torvalds 
38fd2c3ef7SEric Dumazet struct inet_skb_parm {
391da177e4SLinus Torvalds 	struct ip_options	opt;		/* Compiled IP options		*/
401da177e4SLinus Torvalds 	unsigned char		flags;
411da177e4SLinus Torvalds 
42df4d9254SHannes Frederic Sowa #define IPSKB_FORWARDED		BIT(0)
43df4d9254SHannes Frederic Sowa #define IPSKB_XFRM_TUNNEL_SIZE	BIT(1)
44df4d9254SHannes Frederic Sowa #define IPSKB_XFRM_TRANSFORMED	BIT(2)
45df4d9254SHannes Frederic Sowa #define IPSKB_FRAG_COMPLETE	BIT(3)
46df4d9254SHannes Frederic Sowa #define IPSKB_REROUTED		BIT(4)
47df4d9254SHannes Frederic Sowa #define IPSKB_DOREDIRECT	BIT(5)
48d6b915e2SFlorian Westphal #define IPSKB_FRAG_PMTU		BIT(6)
495f2d04f1SPatrick McHardy 
505f2d04f1SPatrick McHardy 	u16			frag_max_size;
511da177e4SLinus Torvalds };
521da177e4SLinus Torvalds 
53c9bdd4b5SArnaldo Carvalho de Melo static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
54c9bdd4b5SArnaldo Carvalho de Melo {
55eddc9ec5SArnaldo Carvalho de Melo 	return ip_hdr(skb)->ihl * 4;
56c9bdd4b5SArnaldo Carvalho de Melo }
57c9bdd4b5SArnaldo Carvalho de Melo 
58fd2c3ef7SEric Dumazet struct ipcm_cookie {
5924025c46SSoheil Hassas Yeganeh 	struct sockcm_cookie	sockc;
60c1d18f9fSAl Viro 	__be32			addr;
611da177e4SLinus Torvalds 	int			oif;
62f6d8bd05SEric Dumazet 	struct ip_options_rcu	*opt;
632244d07bSOliver Hartkopp 	__u8			tx_flags;
64f02db315SFrancesco Fusco 	__u8			ttl;
65f02db315SFrancesco Fusco 	__s16			tos;
66f02db315SFrancesco Fusco 	char			priority;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
704b261c75SHannes Frederic Sowa #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
711da177e4SLinus Torvalds 
72fd2c3ef7SEric Dumazet struct ip_ra_chain {
7343a951e9SEric Dumazet 	struct ip_ra_chain __rcu *next;
741da177e4SLinus Torvalds 	struct sock		*sk;
75592fcb9dSEric Dumazet 	union {
761da177e4SLinus Torvalds 		void			(*destructor)(struct sock *);
77592fcb9dSEric Dumazet 		struct sock		*saved_sk;
78592fcb9dSEric Dumazet 	};
7966018506SEric Dumazet 	struct rcu_head		rcu;
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
8243a951e9SEric Dumazet extern struct ip_ra_chain __rcu *ip_ra_chain;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* IP flags. */
851da177e4SLinus Torvalds #define IP_CE		0x8000		/* Flag: "Congestion"		*/
861da177e4SLinus Torvalds #define IP_DF		0x4000		/* Flag: "Don't Fragment"	*/
871da177e4SLinus Torvalds #define IP_MF		0x2000		/* Flag: "More Fragments"	*/
881da177e4SLinus Torvalds #define IP_OFFSET	0x1FFF		/* "Fragment Offset" part	*/
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define IP_FRAG_TIME	(30 * HZ)		/* fragment lifetime	*/
911da177e4SLinus Torvalds 
9214c85021SArnaldo Carvalho de Melo struct msghdr;
9314c85021SArnaldo Carvalho de Melo struct net_device;
9414c85021SArnaldo Carvalho de Melo struct packet_type;
9514c85021SArnaldo Carvalho de Melo struct rtable;
9614c85021SArnaldo Carvalho de Melo struct sockaddr;
9714c85021SArnaldo Carvalho de Melo 
9872c1d3bdSWANG Cong int igmp_mc_init(void);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /*
1011da177e4SLinus Torvalds  *	Functions provided by ip.c
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds 
104cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
10513d8eaa0SAl Viro 			  __be32 saddr, __be32 daddr,
106f6d8bd05SEric Dumazet 			  struct ip_options_rcu *opt);
1075c3a0fd7SJoe Perches int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
1085c3a0fd7SJoe Perches 	   struct net_device *orig_dev);
1095c3a0fd7SJoe Perches int ip_local_deliver(struct sk_buff *skb);
1105c3a0fd7SJoe Perches int ip_mr_input(struct sk_buff *skb);
111ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
112ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
113694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
114694869b3SEric W. Biederman 		   int (*output)(struct net *, struct sock *, struct sk_buff *));
1155c3a0fd7SJoe Perches void ip_send_check(struct iphdr *ip);
116cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
11733224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
118aad88724SEric Dumazet 
119b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
1205c3a0fd7SJoe Perches void ip_init(void);
1215c3a0fd7SJoe Perches int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1221da177e4SLinus Torvalds 		   int getfrag(void *from, char *to, int offset, int len,
1231da177e4SLinus Torvalds 			       int odd, struct sk_buff *skb),
1241da177e4SLinus Torvalds 		   void *from, int len, int protolen,
1251da177e4SLinus Torvalds 		   struct ipcm_cookie *ipc,
1262e77d89bSEric Dumazet 		   struct rtable **rt,
1271da177e4SLinus Torvalds 		   unsigned int flags);
1285c3a0fd7SJoe Perches int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd,
1295c3a0fd7SJoe Perches 		       struct sk_buff *skb);
1305c3a0fd7SJoe Perches ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1311da177e4SLinus Torvalds 		       int offset, size_t size, int flags);
1325c3a0fd7SJoe Perches struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4,
1331c32c5adSHerbert Xu 			      struct sk_buff_head *queue,
1341c32c5adSHerbert Xu 			      struct inet_cork *cork);
1355c3a0fd7SJoe Perches int ip_send_skb(struct net *net, struct sk_buff *skb);
1365c3a0fd7SJoe Perches int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4);
1375c3a0fd7SJoe Perches void ip_flush_pending_frames(struct sock *sk);
1385c3a0fd7SJoe Perches struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4,
1395c3a0fd7SJoe Perches 			    int getfrag(void *from, char *to, int offset,
1405c3a0fd7SJoe Perches 					int len, int odd, struct sk_buff *skb),
1411c32c5adSHerbert Xu 			    void *from, int length, int transhdrlen,
1425c3a0fd7SJoe Perches 			    struct ipcm_cookie *ipc, struct rtable **rtp,
1431c32c5adSHerbert Xu 			    unsigned int flags);
1441c32c5adSHerbert Xu 
14577968b78SDavid S. Miller static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4)
1461c32c5adSHerbert Xu {
14777968b78SDavid S. Miller 	return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1481c32c5adSHerbert Xu }
1491da177e4SLinus Torvalds 
150aa661581SFrancesco Fusco static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
151aa661581SFrancesco Fusco {
152aa661581SFrancesco Fusco 	return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos);
153aa661581SFrancesco Fusco }
154aa661581SFrancesco Fusco 
155aa661581SFrancesco Fusco static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk)
156aa661581SFrancesco Fusco {
157aa661581SFrancesco Fusco 	return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk);
158aa661581SFrancesco Fusco }
159aa661581SFrancesco Fusco 
1601da177e4SLinus Torvalds /* datagram.c */
16103645a11SEric Dumazet int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
1625c3a0fd7SJoe Perches int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
1631da177e4SLinus Torvalds 
1645c3a0fd7SJoe Perches void ip4_datagram_release_cb(struct sock *sk);
1658141ed9fSSteffen Klassert 
1661da177e4SLinus Torvalds struct ip_reply_arg {
1671da177e4SLinus Torvalds 	struct kvec iov[1];
16888ef4a5aSKOVACS Krisztian 	int	    flags;
169d6f5493cSAl Viro 	__wsum 	    csum;
1701da177e4SLinus Torvalds 	int	    csumoffset; /* u16 offset of csum in iov[0].iov_base */
1711da177e4SLinus Torvalds 				/* -1 if not needed */
172f0e48dbfSPatrick McHardy 	int	    bound_dev_if;
17366b13d99SEric Dumazet 	u8  	    tos;
1741da177e4SLinus Torvalds };
1751da177e4SLinus Torvalds 
17688ef4a5aSKOVACS Krisztian #define IP_REPLY_ARG_NOSRCCHECK 1
17788ef4a5aSKOVACS Krisztian 
17886b08d86SKOVACS Krisztian static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
17986b08d86SKOVACS Krisztian {
18086b08d86SKOVACS Krisztian 	return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
18186b08d86SKOVACS Krisztian }
18286b08d86SKOVACS Krisztian 
183bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
18424a2d43dSEric Dumazet 			   const struct ip_options *sopt,
18524a2d43dSEric Dumazet 			   __be32 daddr, __be32 saddr,
18624a2d43dSEric Dumazet 			   const struct ip_reply_arg *arg,
18770e73416SDavid S. Miller 			   unsigned int len);
1881da177e4SLinus Torvalds 
1894ce3c183SEric Dumazet #define IP_INC_STATS(net, field)	SNMP_INC_STATS64((net)->mib.ip_statistics, field)
19013415e46SEric Dumazet #define __IP_INC_STATS(net, field)	__SNMP_INC_STATS64((net)->mib.ip_statistics, field)
1914ce3c183SEric Dumazet #define IP_ADD_STATS(net, field, val)	SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
19213415e46SEric Dumazet #define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
1934ce3c183SEric Dumazet #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
19413415e46SEric Dumazet #define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
19561a7e260SPavel Emelyanov #define NET_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.net_statistics, field)
19613415e46SEric Dumazet #define __NET_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.net_statistics, field)
197f7324acdSDavid S. Miller #define NET_ADD_STATS(net, field, adnd)	SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
19813415e46SEric Dumazet #define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
1991da177e4SLinus Torvalds 
200c4c6bc31SRaghavendra K T u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offct);
201698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt);
2024ce3c183SEric Dumazet #if BITS_PER_LONG==32
203c4c6bc31SRaghavendra K T u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
204c4c6bc31SRaghavendra K T 			 size_t syncp_offset);
205698365faSWANG Cong u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off);
2064ce3c183SEric Dumazet #else
207c4c6bc31SRaghavendra K T static inline u64  snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
208c4c6bc31SRaghavendra K T 					size_t syncp_offset)
209c4c6bc31SRaghavendra K T {
210c4c6bc31SRaghavendra K T 	return snmp_get_cpu_field(mib, cpu, offct);
211c4c6bc31SRaghavendra K T 
212c4c6bc31SRaghavendra K T }
213c4c6bc31SRaghavendra K T 
214698365faSWANG Cong static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off)
2154ce3c183SEric Dumazet {
2164ce3c183SEric Dumazet 	return snmp_fold_field(mib, offt);
2174ce3c183SEric Dumazet }
2184ce3c183SEric Dumazet #endif
21933490170SYOSHIFUJI Hideaki 
2200bbf87d8SEric W. Biederman void inet_get_local_port_range(struct net *net, int *low, int *high);
221227b60f5SStephen Hemminger 
222fcd77db0SDavid S. Miller #ifdef CONFIG_SYSCTL
223122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port)
224e3826f1eSAmerigo Wang {
225122ff243SWANG Cong 	if (!net->ipv4.sysctl_local_reserved_ports)
226122ff243SWANG Cong 		return 0;
227122ff243SWANG Cong 	return test_bit(port, net->ipv4.sysctl_local_reserved_ports);
228e3826f1eSAmerigo Wang }
22920e61da7SWANG Cong 
23020e61da7SWANG Cong static inline bool sysctl_dev_name_is_allowed(const char *name)
23120e61da7SWANG Cong {
23220e61da7SWANG Cong 	return strcmp(name, "default") != 0  && strcmp(name, "all") != 0;
23320e61da7SWANG Cong }
23420e61da7SWANG Cong 
235122ff243SWANG Cong #else
236122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port)
237122ff243SWANG Cong {
238122ff243SWANG Cong 	return 0;
239122ff243SWANG Cong }
240122ff243SWANG Cong #endif
241e3826f1eSAmerigo Wang 
242822c8685SDeepa Dinamani __be32 inet_current_timestamp(void);
243822c8685SDeepa Dinamani 
24420380731SArnaldo Carvalho de Melo /* From inetpeer.c */
24520380731SArnaldo Carvalho de Melo extern int inet_peer_threshold;
24620380731SArnaldo Carvalho de Melo extern int inet_peer_minttl;
24720380731SArnaldo Carvalho de Melo extern int inet_peer_maxttl;
24820380731SArnaldo Carvalho de Melo 
2495c3a0fd7SJoe Perches void ipfrag_init(void);
25020380731SArnaldo Carvalho de Melo 
2515c3a0fd7SJoe Perches void ip_static_sysctl_init(void);
252bd7b1533SAl Viro 
253e110861fSLorenzo Colitti #define IP4_REPLY_MARK(net, mark) \
254e110861fSLorenzo Colitti 	((net)->ipv4.sysctl_fwmark_reflect ? (mark) : 0)
255e110861fSLorenzo Colitti 
256d18cd551SDavid S. Miller static inline bool ip_is_fragment(const struct iphdr *iph)
257d18cd551SDavid S. Miller {
258d18cd551SDavid S. Miller 	return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0;
259d18cd551SDavid S. Miller }
260d18cd551SDavid S. Miller 
2611da177e4SLinus Torvalds #ifdef CONFIG_INET
26214c85021SArnaldo Carvalho de Melo #include <net/dst.h>
26314c85021SArnaldo Carvalho de Melo 
2641da177e4SLinus Torvalds /* The function in 2.2 was invalid, producing wrong result for
2651da177e4SLinus Torvalds  * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */
2661da177e4SLinus Torvalds static inline
2671da177e4SLinus Torvalds int ip_decrease_ttl(struct iphdr *iph)
2681da177e4SLinus Torvalds {
2695c78f275SAl Viro 	u32 check = (__force u32)iph->check;
2705c78f275SAl Viro 	check += (__force u32)htons(0x0100);
2715c78f275SAl Viro 	iph->check = (__force __sum16)(check + (check>=0xFFFF));
2721da177e4SLinus Torvalds 	return --iph->ttl;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static inline
2764e3f5d72SEric Dumazet int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst)
2771da177e4SLinus Torvalds {
2784e3f5d72SEric Dumazet 	u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
2794e3f5d72SEric Dumazet 
2804e3f5d72SEric Dumazet 	return  pmtudisc == IP_PMTUDISC_DO ||
2814e3f5d72SEric Dumazet 		(pmtudisc == IP_PMTUDISC_WANT &&
282a02cec21SEric Dumazet 		 !(dst_metric_locked(dst, RTAX_MTU)));
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
285f87c10a8SHannes Frederic Sowa static inline bool ip_sk_accept_pmtu(const struct sock *sk)
286f87c10a8SHannes Frederic Sowa {
2871b346576SHannes Frederic Sowa 	return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE &&
2881b346576SHannes Frederic Sowa 	       inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT;
289f87c10a8SHannes Frederic Sowa }
290f87c10a8SHannes Frederic Sowa 
291f87c10a8SHannes Frederic Sowa static inline bool ip_sk_use_pmtu(const struct sock *sk)
292f87c10a8SHannes Frederic Sowa {
293f87c10a8SHannes Frederic Sowa 	return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE;
294f87c10a8SHannes Frederic Sowa }
295f87c10a8SHannes Frederic Sowa 
29660ff7467SWANG Cong static inline bool ip_sk_ignore_df(const struct sock *sk)
2971b346576SHannes Frederic Sowa {
2981b346576SHannes Frederic Sowa 	return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO ||
2991b346576SHannes Frederic Sowa 	       inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT;
3001b346576SHannes Frederic Sowa }
3011b346576SHannes Frederic Sowa 
302f87c10a8SHannes Frederic Sowa static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst,
303f87c10a8SHannes Frederic Sowa 						    bool forwarding)
304f87c10a8SHannes Frederic Sowa {
305f87c10a8SHannes Frederic Sowa 	struct net *net = dev_net(dst->dev);
306f87c10a8SHannes Frederic Sowa 
307f87c10a8SHannes Frederic Sowa 	if (net->ipv4.sysctl_ip_fwd_use_pmtu ||
308f87c10a8SHannes Frederic Sowa 	    dst_metric_locked(dst, RTAX_MTU) ||
309f87c10a8SHannes Frederic Sowa 	    !forwarding)
310f87c10a8SHannes Frederic Sowa 		return dst_mtu(dst);
311f87c10a8SHannes Frederic Sowa 
312f87c10a8SHannes Frederic Sowa 	return min(dst->dev->mtu, IP_MAX_MTU);
313f87c10a8SHannes Frederic Sowa }
314f87c10a8SHannes Frederic Sowa 
315f87c10a8SHannes Frederic Sowa static inline unsigned int ip_skb_dst_mtu(const struct sk_buff *skb)
316f87c10a8SHannes Frederic Sowa {
317caf3f267SEric Dumazet 	struct sock *sk = skb->sk;
318caf3f267SEric Dumazet 
319caf3f267SEric Dumazet 	if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) {
320f87c10a8SHannes Frederic Sowa 		bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED;
321caf3f267SEric Dumazet 
322f87c10a8SHannes Frederic Sowa 		return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding);
323f87c10a8SHannes Frederic Sowa 	}
324caf3f267SEric Dumazet 
325caf3f267SEric Dumazet 	return min(skb_dst(skb)->dev->mtu, IP_MAX_MTU);
326f87c10a8SHannes Frederic Sowa }
327f87c10a8SHannes Frederic Sowa 
32804ca6973SEric Dumazet u32 ip_idents_reserve(u32 hash, int segs);
329b6a7719aSHannes Frederic Sowa void __ip_select_ident(struct net *net, struct iphdr *iph, int segs);
33073f156a6SEric Dumazet 
331b6a7719aSHannes Frederic Sowa static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb,
332b6a7719aSHannes Frederic Sowa 					struct sock *sk, int segs)
3331da177e4SLinus Torvalds {
334703133deSAnsis Atteka 	struct iphdr *iph = ip_hdr(skb);
335703133deSAnsis Atteka 
33660ff7467SWANG Cong 	if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) {
3371da177e4SLinus Torvalds 		/* This is only to work around buggy Windows95/2000
3381da177e4SLinus Torvalds 		 * VJ compression implementations.  If the ID field
3391da177e4SLinus Torvalds 		 * does not change, they drop every other packet in
3401da177e4SLinus Torvalds 		 * a TCP stream using header compression.
3411da177e4SLinus Torvalds 		 */
342c720c7e8SEric Dumazet 		if (sk && inet_sk(sk)->inet_daddr) {
343c720c7e8SEric Dumazet 			iph->id = htons(inet_sk(sk)->inet_id);
34473f156a6SEric Dumazet 			inet_sk(sk)->inet_id += segs;
34573f156a6SEric Dumazet 		} else {
3461da177e4SLinus Torvalds 			iph->id = 0;
34773f156a6SEric Dumazet 		}
34873f156a6SEric Dumazet 	} else {
349b6a7719aSHannes Frederic Sowa 		__ip_select_ident(net, iph, segs);
35073f156a6SEric Dumazet 	}
35173f156a6SEric Dumazet }
35273f156a6SEric Dumazet 
353b6a7719aSHannes Frederic Sowa static inline void ip_select_ident(struct net *net, struct sk_buff *skb,
354b6a7719aSHannes Frederic Sowa 				   struct sock *sk)
35573f156a6SEric Dumazet {
356b6a7719aSHannes Frederic Sowa 	ip_select_ident_segs(net, skb, sk, 1);
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
359ed70fcfcSTom Herbert static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto)
360ed70fcfcSTom Herbert {
361ed70fcfcSTom Herbert 	return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
362ed70fcfcSTom Herbert 				  skb->len, proto, 0);
363ed70fcfcSTom Herbert }
364ed70fcfcSTom Herbert 
365c3f83241STom Herbert /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store
366c3f83241STom Herbert  * Equivalent to :	flow->v4addrs.src = iph->saddr;
367c3f83241STom Herbert  *			flow->v4addrs.dst = iph->daddr;
368c3f83241STom Herbert  */
369c3f83241STom Herbert static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
370c3f83241STom Herbert 					    const struct iphdr *iph)
371c3f83241STom Herbert {
372c3f83241STom Herbert 	BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) !=
373c3f83241STom Herbert 		     offsetof(typeof(flow->addrs), v4addrs.src) +
374c3f83241STom Herbert 			      sizeof(flow->addrs.v4addrs.src));
375c3f83241STom Herbert 	memcpy(&flow->addrs.v4addrs, &iph->saddr, sizeof(flow->addrs.v4addrs));
376c3f83241STom Herbert 	flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
377c3f83241STom Herbert }
378c3f83241STom Herbert 
3791933a785STom Herbert static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto)
3801933a785STom Herbert {
3811933a785STom Herbert 	const struct iphdr *iph = skb_gro_network_header(skb);
3821933a785STom Herbert 
3831933a785STom Herbert 	return csum_tcpudp_nofold(iph->saddr, iph->daddr,
3841933a785STom Herbert 				  skb_gro_len(skb), proto, 0);
3851933a785STom Herbert }
3861933a785STom Herbert 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  *	Map a multicast IP onto multicast MAC for type ethernet.
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds 
391714e85beSAl Viro static inline void ip_eth_mc_map(__be32 naddr, char *buf)
3921da177e4SLinus Torvalds {
393714e85beSAl Viro 	__u32 addr=ntohl(naddr);
3941da177e4SLinus Torvalds 	buf[0]=0x01;
3951da177e4SLinus Torvalds 	buf[1]=0x00;
3961da177e4SLinus Torvalds 	buf[2]=0x5e;
3971da177e4SLinus Torvalds 	buf[5]=addr&0xFF;
3981da177e4SLinus Torvalds 	addr>>=8;
3991da177e4SLinus Torvalds 	buf[4]=addr&0xFF;
4001da177e4SLinus Torvalds 	addr>>=8;
4011da177e4SLinus Torvalds 	buf[3]=addr&0x7F;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /*
4051da177e4SLinus Torvalds  *	Map a multicast IP onto multicast MAC for type IP-over-InfiniBand.
4061da177e4SLinus Torvalds  *	Leave P_Key as 0 to be filled in by driver.
4071da177e4SLinus Torvalds  */
4081da177e4SLinus Torvalds 
409a9e527e3SRolf Manderscheid static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
4101da177e4SLinus Torvalds {
411714e85beSAl Viro 	__u32 addr;
412a9e527e3SRolf Manderscheid 	unsigned char scope = broadcast[5] & 0xF;
413a9e527e3SRolf Manderscheid 
4141da177e4SLinus Torvalds 	buf[0]  = 0;		/* Reserved */
4151da177e4SLinus Torvalds 	buf[1]  = 0xff;		/* Multicast QPN */
4161da177e4SLinus Torvalds 	buf[2]  = 0xff;
4171da177e4SLinus Torvalds 	buf[3]  = 0xff;
418714e85beSAl Viro 	addr    = ntohl(naddr);
4191da177e4SLinus Torvalds 	buf[4]  = 0xff;
420a9e527e3SRolf Manderscheid 	buf[5]  = 0x10 | scope;	/* scope from broadcast address */
4211da177e4SLinus Torvalds 	buf[6]  = 0x40;		/* IPv4 signature */
4221da177e4SLinus Torvalds 	buf[7]  = 0x1b;
423a9e527e3SRolf Manderscheid 	buf[8]  = broadcast[8];		/* P_Key */
424a9e527e3SRolf Manderscheid 	buf[9]  = broadcast[9];
4251da177e4SLinus Torvalds 	buf[10] = 0;
4261da177e4SLinus Torvalds 	buf[11] = 0;
4271da177e4SLinus Torvalds 	buf[12] = 0;
4281da177e4SLinus Torvalds 	buf[13] = 0;
4291da177e4SLinus Torvalds 	buf[14] = 0;
4301da177e4SLinus Torvalds 	buf[15] = 0;
4311da177e4SLinus Torvalds 	buf[19] = addr & 0xff;
4321da177e4SLinus Torvalds 	addr  >>= 8;
4331da177e4SLinus Torvalds 	buf[18] = addr & 0xff;
4341da177e4SLinus Torvalds 	addr  >>= 8;
4351da177e4SLinus Torvalds 	buf[17] = addr & 0xff;
4361da177e4SLinus Torvalds 	addr  >>= 8;
4371da177e4SLinus Torvalds 	buf[16] = addr & 0x0f;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
44093ca3bb5STimo Teräs static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
44193ca3bb5STimo Teräs {
44293ca3bb5STimo Teräs 	if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0)
44393ca3bb5STimo Teräs 		memcpy(buf, broadcast, 4);
44493ca3bb5STimo Teräs 	else
44593ca3bb5STimo Teräs 		memcpy(buf, &naddr, sizeof(naddr));
44693ca3bb5STimo Teräs }
44793ca3bb5STimo Teräs 
448dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4491da177e4SLinus Torvalds #include <linux/ipv6.h>
4501da177e4SLinus Torvalds #endif
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static __inline__ void inet_reset_saddr(struct sock *sk)
4531da177e4SLinus Torvalds {
454c720c7e8SEric Dumazet 	inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0;
455dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4561da177e4SLinus Torvalds 	if (sk->sk_family == PF_INET6) {
4571da177e4SLinus Torvalds 		struct ipv6_pinfo *np = inet6_sk(sk);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 		memset(&np->saddr, 0, sizeof(np->saddr));
460efe4208fSEric Dumazet 		memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds #endif
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds #endif
4661da177e4SLinus Torvalds 
46772afa352SDavid Ahern static inline unsigned int ipv4_addr_hash(__be32 ip)
46872afa352SDavid Ahern {
46972afa352SDavid Ahern 	return (__force unsigned int) ip;
47072afa352SDavid Ahern }
47172afa352SDavid Ahern 
4725c3a0fd7SJoe Perches bool ip_call_ra_chain(struct sk_buff *skb);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /*
475b798232fSRami Rosen  *	Functions provided by ip_fragment.c
4761da177e4SLinus Torvalds  */
4771da177e4SLinus Torvalds 
478fd2c3ef7SEric Dumazet enum ip_defrag_users {
4791da177e4SLinus Torvalds 	IP_DEFRAG_LOCAL_DELIVER,
4801da177e4SLinus Torvalds 	IP_DEFRAG_CALL_RA_CHAIN,
4811da177e4SLinus Torvalds 	IP_DEFRAG_CONNTRACK_IN,
4824be929beSAlexey Dobriyan 	__IP_DEFRAG_CONNTRACK_IN_END	= IP_DEFRAG_CONNTRACK_IN + USHRT_MAX,
4831da177e4SLinus Torvalds 	IP_DEFRAG_CONNTRACK_OUT,
4844be929beSAlexey Dobriyan 	__IP_DEFRAG_CONNTRACK_OUT_END	= IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
4858fa9ff68SPatrick McHardy 	IP_DEFRAG_CONNTRACK_BRIDGE_IN,
4864be929beSAlexey Dobriyan 	__IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
4871da177e4SLinus Torvalds 	IP_DEFRAG_VS_IN,
4881da177e4SLinus Torvalds 	IP_DEFRAG_VS_OUT,
489595fc71bSDavid S. Miller 	IP_DEFRAG_VS_FWD,
490595fc71bSDavid S. Miller 	IP_DEFRAG_AF_PACKET,
491bc416d97SEric Dumazet 	IP_DEFRAG_MACVLAN,
4921da177e4SLinus Torvalds };
4931da177e4SLinus Torvalds 
4945cf42280SAndy Zhou /* Return true if the value of 'user' is between 'lower_bond'
4955cf42280SAndy Zhou  * and 'upper_bond' inclusively.
4965cf42280SAndy Zhou  */
4975cf42280SAndy Zhou static inline bool ip_defrag_user_in_between(u32 user,
4985cf42280SAndy Zhou 					     enum ip_defrag_users lower_bond,
4995cf42280SAndy Zhou 					     enum ip_defrag_users upper_bond)
5005cf42280SAndy Zhou {
5015cf42280SAndy Zhou 	return user >= lower_bond && user <= upper_bond;
5025cf42280SAndy Zhou }
5035cf42280SAndy Zhou 
50419bcf9f2SEric W. Biederman int ip_defrag(struct net *net, struct sk_buff *skb, u32 user);
505bc416d97SEric Dumazet #ifdef CONFIG_INET
50619bcf9f2SEric W. Biederman struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user);
507bc416d97SEric Dumazet #else
50819bcf9f2SEric W. Biederman static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
509bc416d97SEric Dumazet {
510bc416d97SEric Dumazet 	return skb;
511bc416d97SEric Dumazet }
512bc416d97SEric Dumazet #endif
5136ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /*
5161da177e4SLinus Torvalds  *	Functions provided by ip_forward.c
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds 
5195c3a0fd7SJoe Perches int ip_forward(struct sk_buff *skb);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds  *	Functions provided by ip_options.c
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds 
5255c3a0fd7SJoe Perches void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
526f6d8bd05SEric Dumazet 		      __be32 daddr, struct rtable *rt, int is_frag);
52724a2d43dSEric Dumazet 
52824a2d43dSEric Dumazet int __ip_options_echo(struct ip_options *dopt, struct sk_buff *skb,
52924a2d43dSEric Dumazet 		      const struct ip_options *sopt);
53024a2d43dSEric Dumazet static inline int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb)
53124a2d43dSEric Dumazet {
53224a2d43dSEric Dumazet 	return __ip_options_echo(dopt, skb, &IPCB(skb)->opt);
53324a2d43dSEric Dumazet }
53424a2d43dSEric Dumazet 
5355c3a0fd7SJoe Perches void ip_options_fragment(struct sk_buff *skb);
5365c3a0fd7SJoe Perches int ip_options_compile(struct net *net, struct ip_options *opt,
5375c3a0fd7SJoe Perches 		       struct sk_buff *skb);
5385c3a0fd7SJoe Perches int ip_options_get(struct net *net, struct ip_options_rcu **optp,
5394c6ea29dSArnaldo Carvalho de Melo 		   unsigned char *data, int optlen);
5405c3a0fd7SJoe Perches int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp,
5414c6ea29dSArnaldo Carvalho de Melo 			     unsigned char __user *data, int optlen);
5425c3a0fd7SJoe Perches void ip_options_undo(struct ip_options *opt);
5435c3a0fd7SJoe Perches void ip_forward_options(struct sk_buff *skb);
5445c3a0fd7SJoe Perches int ip_options_rcv_srr(struct sk_buff *skb);
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /*
5471da177e4SLinus Torvalds  *	Functions provided by ip_sockglue.c
5481da177e4SLinus Torvalds  */
5491da177e4SLinus Torvalds 
550fbf8866dSShawn Bohrer void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb);
5515961de9fSTom Herbert void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb, int offset);
55224025c46SSoheil Hassas Yeganeh int ip_cmsg_send(struct sock *sk, struct msghdr *msg,
553c8e6ad08SHannes Frederic Sowa 		 struct ipcm_cookie *ipc, bool allow_ipv6);
5545c3a0fd7SJoe Perches int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
5555c3a0fd7SJoe Perches 		  unsigned int optlen);
5565c3a0fd7SJoe Perches int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
5575c3a0fd7SJoe Perches 		  int __user *optlen);
5585c3a0fd7SJoe Perches int compat_ip_setsockopt(struct sock *sk, int level, int optname,
5595c3a0fd7SJoe Perches 			 char __user *optval, unsigned int optlen);
5605c3a0fd7SJoe Perches int compat_ip_getsockopt(struct sock *sk, int level, int optname,
5615c3a0fd7SJoe Perches 			 char __user *optval, int __user *optlen);
5625c3a0fd7SJoe Perches int ip_ra_control(struct sock *sk, unsigned char on,
5635c3a0fd7SJoe Perches 		  void (*destructor)(struct sock *));
5641da177e4SLinus Torvalds 
56585fbaa75SHannes Frederic Sowa int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len);
5665c3a0fd7SJoe Perches void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
5675c3a0fd7SJoe Perches 		   u32 info, u8 *payload);
5685c3a0fd7SJoe Perches void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport,
5691da177e4SLinus Torvalds 		    u32 info);
5701da177e4SLinus Torvalds 
5715961de9fSTom Herbert static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
5725961de9fSTom Herbert {
5735961de9fSTom Herbert 	ip_cmsg_recv_offset(msg, skb, 0);
5745961de9fSTom Herbert }
5755961de9fSTom Herbert 
5764cdf507dSEric Dumazet bool icmp_global_allow(void);
5774cdf507dSEric Dumazet extern int sysctl_icmp_msgs_per_sec;
5784cdf507dSEric Dumazet extern int sysctl_icmp_msgs_burst;
5794cdf507dSEric Dumazet 
58020380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
5815c3a0fd7SJoe Perches int ip_misc_proc_init(void);
58220380731SArnaldo Carvalho de Melo #endif
58320380731SArnaldo Carvalho de Melo 
5841da177e4SLinus Torvalds #endif	/* _IP_H */
585