blob: 4f11f7df7dd6788e9715b80a38540e862d1ff9e7 [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 module.
8 *
9 * Version: @(#)ip.h 1.0.2 05/07/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, <[email protected]>
13 * Alan Cox, <[email protected]>
14 *
15 * Changes:
16 * Mike McLagan : Routing by source
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef _IP_H
19#define _IP_H
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/ip.h>
23#include <linux/in.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030024#include <linux/skbuff.h>
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -080025#include <linux/jhash.h>
Christoph Hellwigde40a3e2020-07-23 08:08:57 +020026#include <linux/sockptr.h>
Eric Dumazet020e71a2021-10-25 09:48:24 -070027#include <linux/static_key.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028
29#include <net/inet_sock.h>
Francesco Fuscoaa661582013-09-24 15:43:09 +020030#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/snmp.h>
KOVACS Krisztian86b08d82008-10-01 07:44:42 -070032#include <net/flow.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020033#include <net/flow_dissector.h>
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -080034#include <net/netns/hash.h>
Vadim Fedorenkofade5642021-06-25 19:21:39 +030035#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Gao Feng7ed14d92017-04-12 12:34:03 +080037#define IPV4_MAX_PMTU 65535U /* RFC 2675, Section 5.1 */
Eric Dumazetb5476022017-12-11 07:17:39 -080038#define IPV4_MIN_MTU 68 /* RFC 791 */
Gao Feng7ed14d92017-04-12 12:34:03 +080039
David Ahern9ab948a2019-03-20 09:18:59 -070040extern unsigned int sysctl_fib_sync_mem;
41extern unsigned int sysctl_fib_sync_mem_min;
42extern unsigned int sysctl_fib_sync_mem_max;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct sock;
45
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000046struct inet_skb_parm {
David Ahern0b922b72016-05-10 11:19:51 -070047 int iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct ip_options opt; /* Compiled IP options */
David Aherna04a4802016-10-16 20:02:52 -070049 u16 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Hannes Frederic Sowadf4d9252015-01-23 12:01:26 +010051#define IPSKB_FORWARDED BIT(0)
52#define IPSKB_XFRM_TUNNEL_SIZE BIT(1)
53#define IPSKB_XFRM_TRANSFORMED BIT(2)
54#define IPSKB_FRAG_COMPLETE BIT(3)
55#define IPSKB_REROUTED BIT(4)
56#define IPSKB_DOREDIRECT BIT(5)
Florian Westphald6b915e2015-05-22 16:32:51 +020057#define IPSKB_FRAG_PMTU BIT(6)
Lance Richardson9ee6c5d2016-11-02 16:36:17 -040058#define IPSKB_L3SLAVE BIT(7)
Eyal Birgere6175a2e2022-05-13 23:34:02 +030059#define IPSKB_NOPOLICY BIT(8)
Sriram Yagnaraman0b2ee662023-08-31 10:03:30 +020060#define IPSKB_MULTIPATH BIT(9)
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020061
62 u16 frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
David Aherna04a4802016-10-16 20:02:52 -070065static inline bool ipv4_l3mdev_skb(u16 flags)
66{
67 return !!(flags & IPSKB_L3SLAVE);
68}
69
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030070static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
71{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070072 return ip_hdr(skb)->ihl * 4;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030073}
74
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000075struct ipcm_cookie {
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -040076 struct sockcm_cookie sockc;
Al Viroc1d18f92006-09-27 18:28:28 -070077 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int oif;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000079 struct ip_options_rcu *opt;
Nicolas Dichtel3f5413c2023-05-22 14:08:20 +020080 __u8 protocol;
Francesco Fuscof02db312013-09-24 15:43:08 +020081 __u8 ttl;
82 __s16 tos;
83 char priority;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -040084 __u16 gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Willem de Bruijn35178202018-07-06 10:12:54 -040087static inline void ipcm_init(struct ipcm_cookie *ipcm)
88{
89 *ipcm = (struct ipcm_cookie) { .tos = -1 };
90}
91
92static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
93 const struct inet_sock *inet)
94{
95 ipcm_init(ipcm);
96
Eric Dumazetb5346802023-07-28 15:03:15 +000097 ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark);
Eric Dumazetc48fcb42023-08-31 13:52:11 +000098 ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags);
Eric Dumazet4c971d22022-05-13 11:55:41 -070099 ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if);
Willem de Bruijn35178202018-07-06 10:12:54 -0400100 ipcm->addr = inet->inet_saddr;
Nicolas Dichtel3f5413c2023-05-22 14:08:20 +0200101 ipcm->protocol = inet->inet_num;
Willem de Bruijn35178202018-07-06 10:12:54 -0400102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100105#define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
David Ahernfb74c272017-08-07 08:44:16 -0700107/* return enslaved device index if relevant */
Eric Dumazetd6bb2d12020-11-09 15:13:48 -0800108static inline int inet_sdif(const struct sk_buff *skb)
David Ahernfb74c272017-08-07 08:44:16 -0700109{
110#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
111 if (skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
112 return IPCB(skb)->iif;
113#endif
114 return 0;
115}
116
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300117/* Special input handler for packets caught by router alert option.
118 They are selected only by protocol field, and then processed likely
119 local ones; but only if someone wants them! Otherwise, router
120 not running rsvpd will kill RSVP.
121
122 It is user level problem, what it will make with them.
123 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
124 but receiver should be enough clever f.e. to forward mtrace requests,
125 sent to multicast group to reach destination designated router.
126 */
127
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000128struct ip_ra_chain {
Eric Dumazet43a951e2010-10-25 03:32:44 +0000129 struct ip_ra_chain __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct sock *sk;
Eric Dumazet592fcb92010-06-09 16:21:07 +0000131 union {
132 void (*destructor)(struct sock *);
133 struct sock *saved_sk;
134 };
Eric Dumazet66018502010-06-07 03:12:08 +0000135 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* IP flags. */
139#define IP_CE 0x8000 /* Flag: "Congestion" */
140#define IP_DF 0x4000 /* Flag: "Don't Fragment" */
141#define IP_MF 0x2000 /* Flag: "More Fragments" */
142#define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
143
144#define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */
145
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200146struct msghdr;
147struct net_device;
148struct packet_type;
149struct rtable;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200150struct sockaddr;
151
WANG Cong72c1d3b2014-01-10 16:09:45 -0800152int igmp_mc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*
155 * Functions provided by ip.c
156 */
157
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700158int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700159 __be32 saddr, __be32 daddr,
Wei Wangde033b7d2020-09-09 17:50:47 -0700160 struct ip_options_rcu *opt, u8 tos);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700161int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
162 struct net_device *orig_dev);
Edward Cree17266ee2018-07-02 16:14:12 +0100163void ip_list_rcv(struct list_head *head, struct packet_type *pt,
164 struct net_device *orig_dev);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700165int ip_local_deliver(struct sk_buff *skb);
Paolo Abeni68cb7d52018-11-07 12:38:31 +0100166void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int proto);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700167int ip_mr_input(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -0500168int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
169int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Eric W. Biederman694869b2015-06-12 21:55:31 -0500170int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
171 int (*output)(struct net *, struct sock *, struct sk_buff *));
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200172
173struct ip_fraglist_iter {
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200174 struct sk_buff *frag;
175 struct iphdr *iph;
176 int offset;
177 unsigned int hlen;
178};
179
180void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph,
181 unsigned int hlen, struct ip_fraglist_iter *iter);
182void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter);
183
184static inline struct sk_buff *ip_fraglist_next(struct ip_fraglist_iter *iter)
185{
186 struct sk_buff *skb = iter->frag;
187
188 iter->frag = skb->next;
189 skb_mark_not_on_list(skb);
190
191 return skb;
192}
193
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200194struct ip_frag_state {
Eric Dumazete7a409c2019-10-19 09:26:37 -0700195 bool DF;
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200196 unsigned int hlen;
197 unsigned int ll_rs;
198 unsigned int mtu;
199 unsigned int left;
200 int offset;
201 int ptr;
202 __be16 not_last_frag;
203};
204
205void ip_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int ll_rs,
Eric Dumazete7a409c2019-10-19 09:26:37 -0700206 unsigned int mtu, bool DF, struct ip_frag_state *state);
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200207struct sk_buff *ip_frag_next(struct sk_buff *skb,
208 struct ip_frag_state *state);
209
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700210void ip_send_check(struct iphdr *ip);
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500211int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Eric W. Biederman33224b12015-10-07 16:48:46 -0500212int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Eric Dumazetaad88722014-04-15 13:47:15 -0400213
Xin Long69b9e1e2018-07-02 18:21:11 +0800214int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
215 __u8 tos);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700216void ip_init(void);
217int ip_append_data(struct sock *sk, struct flowi4 *fl4,
218 int getfrag(void *from, char *to, int offset, int len,
219 int odd, struct sk_buff *skb),
220 void *from, int len, int protolen,
221 struct ipcm_cookie *ipc,
222 struct rtable **rt,
223 unsigned int flags);
224int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd,
225 struct sk_buff *skb);
226ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
227 int offset, size_t size, int flags);
228struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4,
229 struct sk_buff_head *queue,
230 struct inet_cork *cork);
231int ip_send_skb(struct net *net, struct sk_buff *skb);
232int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4);
233void ip_flush_pending_frames(struct sock *sk);
234struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4,
235 int getfrag(void *from, char *to, int offset,
236 int len, int odd, struct sk_buff *skb),
237 void *from, int length, int transhdrlen,
238 struct ipcm_cookie *ipc, struct rtable **rtp,
Willem de Bruijn1cd78842018-04-26 13:42:15 -0400239 struct inet_cork *cork, unsigned int flags);
Herbert Xu1c32c5a2011-03-01 02:36:47 +0000240
Eric Dumazet05e22e82020-06-19 12:12:34 -0700241int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
Xin Long69b9e1e2018-07-02 18:21:11 +0800242
David S. Miller77968b72011-05-08 17:12:19 -0700243static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4)
Herbert Xu1c32c5a2011-03-01 02:36:47 +0000244{
David S. Miller77968b72011-05-08 17:12:19 -0700245 return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
Herbert Xu1c32c5a2011-03-01 02:36:47 +0000246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Francesco Fuscoaa661582013-09-24 15:43:09 +0200248static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
249{
250 return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos);
251}
252
253static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk)
254{
255 return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk);
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* datagram.c */
Eric Dumazet03645a12015-07-14 08:10:22 +0200259int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700260int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700262void ip4_datagram_release_cb(struct sock *sk);
Steffen Klassert8141ed92013-01-21 02:00:03 +0000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264struct ip_reply_arg {
Stephen Hemminger82695b32018-02-27 15:48:21 -0800265 struct kvec iov[1];
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700266 int flags;
Al Virod6f5493c2006-11-14 21:26:08 -0800267 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int csumoffset; /* u16 offset of csum in iov[0].iov_base */
Stephen Hemminger82695b32018-02-27 15:48:21 -0800269 /* -1 if not needed */
Patrick McHardyf0e48db2007-06-04 21:32:46 -0700270 int bound_dev_if;
Eric Dumazet66b13d92011-10-24 03:06:21 -0400271 u8 tos;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900272 kuid_t uid;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800273};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700275#define IP_REPLY_ARG_NOSRCCHECK 1
276
KOVACS Krisztian86b08d82008-10-01 07:44:42 -0700277static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
278{
279 return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
280}
281
Eric Dumazetbdbbb852015-01-29 21:35:05 -0800282void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
Eric Dumazet24a2d432014-09-27 09:50:55 -0700283 const struct ip_options *sopt,
284 __be32 daddr, __be32 saddr,
285 const struct ip_reply_arg *arg,
Antoine Tenartf62a00b2023-05-23 18:14:52 +0200286 unsigned int len, u64 transmit_time, u32 txhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700288#define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field)
Eric Dumazet13415e42016-04-27 16:44:43 -0700289#define __IP_INC_STATS(net, field) __SNMP_INC_STATS64((net)->mib.ip_statistics, field)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700290#define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
Eric Dumazet13415e42016-04-27 16:44:43 -0700291#define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700292#define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
Eric Dumazet13415e42016-04-27 16:44:43 -0700293#define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
Pavel Emelyanov61a7e262008-07-18 04:03:08 -0700294#define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field)
Eric Dumazet13415e42016-04-27 16:44:43 -0700295#define __NET_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.net_statistics, field)
David S. Millerf7324ac2014-03-06 15:03:17 -0500296#define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
Eric Dumazet13415e42016-04-27 16:44:43 -0700297#define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Eric Dumazet59f09ae2021-09-29 18:03:32 -0700299static inline u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
300{
301 return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
302}
303
WANG Cong698365f2014-05-05 15:55:55 -0700304unsigned long snmp_fold_field(void __percpu *mib, int offt);
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700305#if BITS_PER_LONG==32
Raghavendra K Tc4c6bc32015-08-30 11:29:41 +0530306u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
307 size_t syncp_offset);
WANG Cong698365f2014-05-05 15:55:55 -0700308u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off);
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700309#else
Raghavendra K Tc4c6bc32015-08-30 11:29:41 +0530310static inline u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
311 size_t syncp_offset)
312{
313 return snmp_get_cpu_field(mib, cpu, offct);
314
315}
316
WANG Cong698365f2014-05-05 15:55:55 -0700317static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700318{
319 return snmp_fold_field(mib, offt);
320}
321#endif
YOSHIFUJI Hideaki33490172007-04-20 15:57:15 -0700322
Jia He6348ef22016-09-30 11:28:58 +0800323#define snmp_get_cpu_field64_batch(buff64, stats_list, mib_statistic, offset) \
324{ \
325 int i, c; \
326 for_each_possible_cpu(c) { \
327 for (i = 0; stats_list[i].name; i++) \
328 buff64[i] += snmp_get_cpu_field64( \
329 mib_statistic, \
330 c, stats_list[i].entry, \
331 offset); \
332 } \
333}
334
335#define snmp_get_cpu_field_batch(buff, stats_list, mib_statistic) \
336{ \
337 int i, c; \
338 for_each_possible_cpu(c) { \
339 for (i = 0; stats_list[i].name; i++) \
340 buff[i] += snmp_get_cpu_field( \
341 mib_statistic, \
342 c, stats_list[i].entry); \
343 } \
344}
345
Jakub Sitnicki67284862023-01-24 14:36:43 +0100346void inet_get_local_port_range(const struct net *net, int *low, int *high);
347void inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700348
David S. Millerfcd77db2014-05-15 13:43:14 -0400349#ifdef CONFIG_SYSCTL
Maciej Żenczykowski66e2f5f2019-11-26 14:44:16 -0800350static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
Amerigo Wange3826f12010-05-05 00:27:06 +0000351{
WANG Cong122ff242014-05-12 16:04:53 -0700352 if (!net->ipv4.sysctl_local_reserved_ports)
Maciej Żenczykowski30429fb2019-11-22 13:50:52 -0800353 return false;
WANG Cong122ff242014-05-12 16:04:53 -0700354 return test_bit(port, net->ipv4.sysctl_local_reserved_ports);
Amerigo Wange3826f12010-05-05 00:27:06 +0000355}
WANG Cong20e61da2014-07-25 15:25:08 -0700356
357static inline bool sysctl_dev_name_is_allowed(const char *name)
358{
359 return strcmp(name, "default") != 0 && strcmp(name, "all") != 0;
360}
361
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -0800362static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
Krister Johansen4548b682017-01-20 17:49:11 -0800363{
Kuniyuki Iwashima9b55c202022-07-18 10:26:42 -0700364 return port < READ_ONCE(net->ipv4.sysctl_ip_prot_sock);
Krister Johansen4548b682017-01-20 17:49:11 -0800365}
366
WANG Cong122ff242014-05-12 16:04:53 -0700367#else
Maciej Żenczykowski66e2f5f2019-11-26 14:44:16 -0800368static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
WANG Cong122ff242014-05-12 16:04:53 -0700369{
Maciej Żenczykowski30429fb2019-11-22 13:50:52 -0800370 return false;
WANG Cong122ff242014-05-12 16:04:53 -0700371}
Krister Johansen4548b682017-01-20 17:49:11 -0800372
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -0800373static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
Krister Johansen4548b682017-01-20 17:49:11 -0800374{
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -0800375 return port < PROT_SOCK;
Krister Johansen4548b682017-01-20 17:49:11 -0800376}
WANG Cong122ff242014-05-12 16:04:53 -0700377#endif
Amerigo Wange3826f12010-05-05 00:27:06 +0000378
Deepa Dinamani822c8682016-02-27 00:32:15 -0800379__be32 inet_current_timestamp(void);
380
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300381/* From inetpeer.c */
382extern int inet_peer_threshold;
383extern int inet_peer_minttl;
384extern int inet_peer_maxttl;
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300385
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700386void ipfrag_init(void);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300387
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700388void ip_static_sysctl_init(void);
Al Virobd7b1532008-07-15 16:00:59 -0400389
Lorenzo Colittie1108612014-05-13 10:17:33 -0700390#define IP4_REPLY_MARK(net, mark) \
Kuniyuki Iwashima85d0b4d2022-07-13 13:51:57 -0700391 (READ_ONCE((net)->ipv4.sysctl_fwmark_reflect) ? (mark) : 0)
Lorenzo Colittie1108612014-05-13 10:17:33 -0700392
David S. Millerd18cd552011-06-23 21:28:52 -0700393static inline bool ip_is_fragment(const struct iphdr *iph)
394{
395 return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#ifdef CONFIG_INET
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200399#include <net/dst.h>
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* The function in 2.2 was invalid, producing wrong result for
402 * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */
403static inline
404int ip_decrease_ttl(struct iphdr *iph)
405{
Al Viro5c78f272006-11-14 21:42:26 -0800406 u32 check = (__force u32)iph->check;
407 check += (__force u32)htons(0x0100);
408 iph->check = (__force __sum16)(check + (check>=0xFFFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return --iph->ttl;
410}
411
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +0100412static inline int ip_mtu_locked(const struct dst_entry *dst)
413{
414 const struct rtable *rt = (const struct rtable *)dst;
415
416 return rt->rt_mtu_locked || dst_metric_locked(dst, RTAX_MTU);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static inline
Eric Dumazet4e3f5d72015-09-25 07:39:14 -0700420int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Eric Dumazet4e3f5d72015-09-25 07:39:14 -0700422 u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
423
424 return pmtudisc == IP_PMTUDISC_DO ||
425 (pmtudisc == IP_PMTUDISC_WANT &&
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +0100426 !ip_mtu_locked(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100429static inline bool ip_sk_accept_pmtu(const struct sock *sk)
430{
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100431 return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE &&
432 inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT;
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100433}
434
435static inline bool ip_sk_use_pmtu(const struct sock *sk)
436{
437 return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE;
438}
439
WANG Cong60ff7462014-05-04 16:39:18 -0700440static inline bool ip_sk_ignore_df(const struct sock *sk)
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100441{
442 return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO ||
443 inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT;
444}
445
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100446static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst,
447 bool forwarding)
448{
Vadim Fedorenkoac6627a2021-07-20 23:06:28 +0300449 const struct rtable *rt = container_of(dst, struct rtable, dst);
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100450 struct net *net = dev_net(dst->dev);
Maciej Żenczykowski02a1b172020-09-23 13:18:15 -0700451 unsigned int mtu;
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100452
Kuniyuki Iwashima60c158d2022-07-13 13:51:53 -0700453 if (READ_ONCE(net->ipv4.sysctl_ip_fwd_use_pmtu) ||
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +0100454 ip_mtu_locked(dst) ||
Vadim Fedorenkoac6627a2021-07-20 23:06:28 +0300455 !forwarding) {
456 mtu = rt->rt_pmtu;
457 if (mtu && time_before(jiffies, rt->dst.expires))
458 goto out;
459 }
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100460
Maciej Żenczykowski02a1b172020-09-23 13:18:15 -0700461 /* 'forwarding = true' case should always honour route mtu */
462 mtu = dst_metric_raw(dst, RTAX_MTU);
Vadim Fedorenkoac6627a2021-07-20 23:06:28 +0300463 if (mtu)
464 goto out;
465
466 mtu = READ_ONCE(dst->dev->mtu);
467
468 if (unlikely(ip_mtu_locked(dst))) {
469 if (rt->rt_uses_gateway && mtu > 576)
470 mtu = 576;
471 }
472
473out:
474 mtu = min_t(unsigned int, mtu, IP_MAX_MTU);
Maciej Żenczykowski02a1b172020-09-23 13:18:15 -0700475
Vadim Fedorenkofade5642021-06-25 19:21:39 +0300476 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100477}
478
Shmulik Ladkanifedbb6b42016-06-29 21:47:03 +0300479static inline unsigned int ip_skb_dst_mtu(struct sock *sk,
480 const struct sk_buff *skb)
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100481{
Vadim Fedorenkofade5642021-06-25 19:21:39 +0300482 unsigned int mtu;
483
Eric Dumazetcaf3f262015-10-04 21:08:08 -0700484 if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) {
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100485 bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED;
Eric Dumazetcaf3f262015-10-04 21:08:08 -0700486
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100487 return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding);
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100488 }
Eric Dumazetcaf3f262015-10-04 21:08:08 -0700489
Vadim Fedorenkofade5642021-06-25 19:21:39 +0300490 mtu = min(READ_ONCE(skb_dst(skb)->dev->mtu), IP_MAX_MTU);
491 return mtu - lwtunnel_headroom(skb_dst(skb)->lwtstate, mtu);
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100492}
493
David Ahern767a2212018-10-04 20:07:51 -0700494struct dst_metrics *ip_fib_metrics_init(struct net *net, struct nlattr *fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -0800495 int fc_mx_len,
496 struct netlink_ext_ack *extack);
David Aherncc5f0eb2018-10-04 20:07:52 -0700497static inline void ip_fib_metrics_put(struct dst_metrics *fib_metrics)
498{
499 if (fib_metrics != &dst_default_metrics &&
500 refcount_dec_and_test(&fib_metrics->refcnt))
501 kfree(fib_metrics);
502}
David Aherna9195252018-04-17 17:33:07 -0700503
David Aherne1255ed2018-10-04 20:07:53 -0700504/* ipv4 and ipv6 both use refcounted metrics if it is not the default */
505static inline
506void ip_dst_init_metrics(struct dst_entry *dst, struct dst_metrics *fib_metrics)
507{
508 dst_init_metrics(dst, fib_metrics->metrics, true);
509
510 if (fib_metrics != &dst_default_metrics) {
511 dst->_metrics |= DST_METRICS_REFCOUNTED;
512 refcount_inc(&fib_metrics->refcnt);
513 }
514}
515
David Ahern1620a332018-10-04 20:07:54 -0700516static inline
517void ip_dst_metrics_put(struct dst_entry *dst)
518{
519 struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
520
521 if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
522 kfree(p);
523}
524
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100525void __ip_select_ident(struct net *net, struct iphdr *iph, int segs);
Eric Dumazet73f156a2014-06-02 05:26:03 -0700526
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100527static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb,
528 struct sock *sk, int segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Ansis Atteka703133de2013-09-18 15:29:53 -0700530 struct iphdr *iph = ip_hdr(skb);
531
Eric Dumazet23f574062022-01-26 17:10:22 -0800532 /* We had many attacks based on IPID, use the private
533 * generator as much as we can.
534 */
535 if (sk && inet_sk(sk)->inet_daddr) {
Eric Dumazet417e7ec2023-08-19 03:17:07 +0000536 int val;
537
538 /* avoid atomic operations for TCP,
539 * as we hold socket lock at this point.
540 */
541 if (sk_is_tcp(sk)) {
542 sock_owned_by_me(sk);
543 val = atomic_read(&inet_sk(sk)->inet_id);
544 atomic_set(&inet_sk(sk)->inet_id, val + segs);
545 } else {
546 val = atomic_add_return(segs, &inet_sk(sk)->inet_id);
547 }
548 iph->id = htons(val);
Eric Dumazet23f574062022-01-26 17:10:22 -0800549 return;
550 }
WANG Cong60ff7462014-05-04 16:39:18 -0700551 if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) {
Eric Dumazet23f574062022-01-26 17:10:22 -0800552 iph->id = 0;
Eric Dumazet73f156a2014-06-02 05:26:03 -0700553 } else {
Eric Dumazet23f574062022-01-26 17:10:22 -0800554 /* Unfortunately we need the big hammer to get a suitable IPID */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100555 __ip_select_ident(net, iph, segs);
Eric Dumazet73f156a2014-06-02 05:26:03 -0700556 }
557}
558
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100559static inline void ip_select_ident(struct net *net, struct sk_buff *skb,
560 struct sock *sk)
Eric Dumazet73f156a2014-06-02 05:26:03 -0700561{
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100562 ip_select_ident_segs(net, skb, sk, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Tom Herberted70fcf2014-05-02 16:29:38 -0700565static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto)
566{
567 return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
568 skb->len, proto, 0);
569}
570
Tom Herbertc3f83242015-06-04 09:16:40 -0700571/* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store
572 * Equivalent to : flow->v4addrs.src = iph->saddr;
573 * flow->v4addrs.dst = iph->daddr;
574 */
575static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
576 const struct iphdr *iph)
577{
578 BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) !=
579 offsetof(typeof(flow->addrs), v4addrs.src) +
580 sizeof(flow->addrs.v4addrs.src));
Hangbin Liu58e0be1e2022-11-15 22:24:00 +0800581 memcpy(&flow->addrs.v4addrs, &iph->addrs, sizeof(flow->addrs.v4addrs));
Tom Herbertc3f83242015-06-04 09:16:40 -0700582 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * Map a multicast IP onto multicast MAC for type ethernet.
587 */
588
Al Viro714e85be2006-11-14 20:51:49 -0800589static inline void ip_eth_mc_map(__be32 naddr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Al Viro714e85be2006-11-14 20:51:49 -0800591 __u32 addr=ntohl(naddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 buf[0]=0x01;
593 buf[1]=0x00;
594 buf[2]=0x5e;
595 buf[5]=addr&0xFF;
596 addr>>=8;
597 buf[4]=addr&0xFF;
598 addr>>=8;
599 buf[3]=addr&0x7F;
600}
601
602/*
603 * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand.
604 * Leave P_Key as 0 to be filled in by driver.
605 */
606
Rolf Manderscheida9e527e32007-12-10 13:38:41 -0700607static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Al Viro714e85be2006-11-14 20:51:49 -0800609 __u32 addr;
Rolf Manderscheida9e527e32007-12-10 13:38:41 -0700610 unsigned char scope = broadcast[5] & 0xF;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 buf[0] = 0; /* Reserved */
613 buf[1] = 0xff; /* Multicast QPN */
614 buf[2] = 0xff;
615 buf[3] = 0xff;
Al Viro714e85be2006-11-14 20:51:49 -0800616 addr = ntohl(naddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 buf[4] = 0xff;
Rolf Manderscheida9e527e32007-12-10 13:38:41 -0700618 buf[5] = 0x10 | scope; /* scope from broadcast address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 buf[6] = 0x40; /* IPv4 signature */
620 buf[7] = 0x1b;
Rolf Manderscheida9e527e32007-12-10 13:38:41 -0700621 buf[8] = broadcast[8]; /* P_Key */
622 buf[9] = broadcast[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 buf[10] = 0;
624 buf[11] = 0;
625 buf[12] = 0;
626 buf[13] = 0;
627 buf[14] = 0;
628 buf[15] = 0;
629 buf[19] = addr & 0xff;
630 addr >>= 8;
631 buf[18] = addr & 0xff;
632 addr >>= 8;
633 buf[17] = addr & 0xff;
634 addr >>= 8;
635 buf[16] = addr & 0x0f;
636}
637
Timo Teräs93ca3bb2011-03-28 22:40:53 +0000638static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
639{
640 if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0)
641 memcpy(buf, broadcast, 4);
642 else
643 memcpy(buf, &naddr, sizeof(naddr));
644}
645
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000646#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#include <linux/ipv6.h>
648#endif
649
650static __inline__ void inet_reset_saddr(struct sock *sk)
651{
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000652 inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000653#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (sk->sk_family == PF_INET6) {
655 struct ipv6_pinfo *np = inet6_sk(sk);
656
657 memset(&np->saddr, 0, sizeof(np->saddr));
Eric Dumazetefe42082013-10-03 15:42:29 -0700658 memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660#endif
661}
662
663#endif
664
David Ahern72afa352015-08-27 16:06:59 -0700665static inline unsigned int ipv4_addr_hash(__be32 ip)
666{
667 return (__force unsigned int) ip;
668}
669
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800670static inline u32 ipv4_portaddr_hash(const struct net *net,
671 __be32 saddr,
672 unsigned int port)
673{
674 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
675}
676
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700677bool ip_call_ra_chain(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/*
Rami Rosenb7982322008-01-01 21:13:09 -0800680 * Functions provided by ip_fragment.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
682
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000683enum ip_defrag_users {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 IP_DEFRAG_LOCAL_DELIVER,
685 IP_DEFRAG_CALL_RA_CHAIN,
686 IP_DEFRAG_CONNTRACK_IN,
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700687 __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 IP_DEFRAG_CONNTRACK_OUT,
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700689 __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
Patrick McHardy8fa9ff62009-12-15 16:59:59 +0100690 IP_DEFRAG_CONNTRACK_BRIDGE_IN,
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700691 __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 IP_DEFRAG_VS_IN,
693 IP_DEFRAG_VS_OUT,
David S. Miller595fc712011-07-05 01:05:48 -0700694 IP_DEFRAG_VS_FWD,
695 IP_DEFRAG_AF_PACKET,
Eric Dumazetbc416d92011-10-06 10:28:31 +0000696 IP_DEFRAG_MACVLAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
Andy Zhou5cf42282015-05-15 14:15:35 -0700699/* Return true if the value of 'user' is between 'lower_bond'
700 * and 'upper_bond' inclusively.
701 */
702static inline bool ip_defrag_user_in_between(u32 user,
703 enum ip_defrag_users lower_bond,
704 enum ip_defrag_users upper_bond)
705{
706 return user >= lower_bond && user <= upper_bond;
707}
708
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500709int ip_defrag(struct net *net, struct sk_buff *skb, u32 user);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000710#ifdef CONFIG_INET
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500711struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000712#else
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500713static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000714{
715 return skb;
716}
717#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719/*
720 * Functions provided by ip_forward.c
721 */
Stephen Hemminger82695b32018-02-27 15:48:21 -0800722
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700723int ip_forward(struct sk_buff *skb);
Stephen Hemminger82695b32018-02-27 15:48:21 -0800724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * Functions provided by ip_options.c
727 */
Stephen Hemminger82695b32018-02-27 15:48:21 -0800728
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700729void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
Jakub Kicinski4f0e3042022-01-28 08:06:54 -0800730 __be32 daddr, struct rtable *rt);
Eric Dumazet24a2d432014-09-27 09:50:55 -0700731
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200732int __ip_options_echo(struct net *net, struct ip_options *dopt,
733 struct sk_buff *skb, const struct ip_options *sopt);
734static inline int ip_options_echo(struct net *net, struct ip_options *dopt,
735 struct sk_buff *skb)
Eric Dumazet24a2d432014-09-27 09:50:55 -0700736{
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200737 return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt);
Eric Dumazet24a2d432014-09-27 09:50:55 -0700738}
739
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700740void ip_options_fragment(struct sk_buff *skb);
Nazarov Sergey3da1ed72019-02-25 19:27:15 +0300741int __ip_options_compile(struct net *net, struct ip_options *opt,
742 struct sk_buff *skb, __be32 *info);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700743int ip_options_compile(struct net *net, struct ip_options *opt,
744 struct sk_buff *skb);
745int ip_options_get(struct net *net, struct ip_options_rcu **optp,
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200746 sockptr_t data, int optlen);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700747void ip_options_undo(struct ip_options *opt);
748void ip_forward_options(struct sk_buff *skb);
Stephen Suryaputra8c83f2d2019-04-01 09:17:32 -0400749int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751/*
752 * Functions provided by ip_sockglue.c
753 */
754
Nicolas Dichteld2f1b7f2024-01-25 15:18:47 +0100755void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb, bool drop_dst);
Paolo Abeniad959032016-11-04 11:28:58 +0100756void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
757 struct sk_buff *skb, int tlen, int offset);
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400758int ip_cmsg_send(struct sock *sk, struct msghdr *msg,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100759 struct ipcm_cookie *ipc, bool allow_ipv6);
Eric Dumazet020e71a2021-10-25 09:48:24 -0700760DECLARE_STATIC_KEY_FALSE(ip4_min_ttl);
Martin KaFai Lauee7f1e12022-08-16 23:18:26 -0700761int do_ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
762 unsigned int optlen);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200763int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700764 unsigned int optlen);
Martin KaFai Laufd969f22022-09-01 17:29:25 -0700765int do_ip_getsockopt(struct sock *sk, int level, int optname,
766 sockptr_t optval, sockptr_t optlen);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700767int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
768 int __user *optlen);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700769int ip_ra_control(struct sock *sk, unsigned char on,
770 void (*destructor)(struct sock *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100772int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len);
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700773void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
774 u32 info, u8 *payload);
775void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport,
776 u32 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Tom Herbert5961de92015-01-05 13:56:16 -0800778static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
779{
Paolo Abeniad959032016-11-04 11:28:58 +0100780 ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0);
Tom Herbert5961de92015-01-05 13:56:16 -0800781}
782
Eric Dumazet4cdf5072014-09-19 07:38:40 -0700783bool icmp_global_allow(void);
784extern int sysctl_icmp_msgs_per_sec;
785extern int sysctl_icmp_msgs_burst;
786
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300787#ifdef CONFIG_PROC_FS
Joe Perches5c3a0fd2013-09-21 10:22:42 -0700788int ip_misc_proc_init(void);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300789#endif
790
Hangbin Liu5e1a99e2019-02-27 16:15:29 +0800791int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family,
Roopa Prabhu404eb772018-05-22 14:03:27 -0700792 struct netlink_ext_ack *extack);
793
Eric Dumazet501a90c2019-12-05 20:43:46 -0800794static inline bool inetdev_valid_mtu(unsigned int mtu)
795{
796 return likely(mtu >= IPV4_MIN_MTU);
797}
798
Christoph Hellwigc4e446b2020-05-28 07:12:27 +0200799void ip_sock_set_freebind(struct sock *sk);
Christoph Hellwig2de569b2020-05-28 07:12:29 +0200800int ip_sock_set_mtu_discover(struct sock *sk, int val);
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200801void ip_sock_set_pktinfo(struct sock *sk);
Christoph Hellwigdb45c0e2020-05-28 07:12:28 +0200802void ip_sock_set_recverr(struct sock *sk);
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200803void ip_sock_set_tos(struct sock *sk, int val);
Poorva Sonparote4f47d5d2021-11-19 12:41:34 -0800804void __ip_sock_set_tos(struct sock *sk, int val);
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806#endif /* _IP_H */