]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
1da177e4 LT |
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 | * | |
02c30a84 | 11 | * Authors: Ross Biro |
1da177e4 LT |
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 | |
1da177e4 LT |
17 | */ |
18 | #ifndef _IP_H | |
19 | #define _IP_H | |
20 | ||
1da177e4 | 21 | #include <linux/types.h> |
1da177e4 LT |
22 | #include <linux/ip.h> |
23 | #include <linux/in.h> | |
c9bdd4b5 | 24 | #include <linux/skbuff.h> |
f0b1e64c | 25 | #include <linux/jhash.h> |
14c85021 ACM |
26 | |
27 | #include <net/inet_sock.h> | |
aa661581 | 28 | #include <net/route.h> |
1da177e4 | 29 | #include <net/snmp.h> |
86b08d86 | 30 | #include <net/flow.h> |
1bd758eb | 31 | #include <net/flow_dissector.h> |
f0b1e64c | 32 | #include <net/netns/hash.h> |
1da177e4 | 33 | |
7ed14d97 | 34 | #define IPV4_MAX_PMTU 65535U /* RFC 2675, Section 5.1 */ |
b5476022 | 35 | #define IPV4_MIN_MTU 68 /* RFC 791 */ |
7ed14d97 | 36 | |
9ab948a9 DA |
37 | extern unsigned int sysctl_fib_sync_mem; |
38 | extern unsigned int sysctl_fib_sync_mem_min; | |
39 | extern unsigned int sysctl_fib_sync_mem_max; | |
40 | ||
1da177e4 LT |
41 | struct sock; |
42 | ||
fd2c3ef7 | 43 | struct inet_skb_parm { |
0b922b7a | 44 | int iif; |
1da177e4 | 45 | struct ip_options opt; /* Compiled IP options */ |
a04a480d | 46 | u16 flags; |
1da177e4 | 47 | |
df4d9254 HFS |
48 | #define IPSKB_FORWARDED BIT(0) |
49 | #define IPSKB_XFRM_TUNNEL_SIZE BIT(1) | |
50 | #define IPSKB_XFRM_TRANSFORMED BIT(2) | |
51 | #define IPSKB_FRAG_COMPLETE BIT(3) | |
52 | #define IPSKB_REROUTED BIT(4) | |
53 | #define IPSKB_DOREDIRECT BIT(5) | |
d6b915e2 | 54 | #define IPSKB_FRAG_PMTU BIT(6) |
9ee6c5dc | 55 | #define IPSKB_L3SLAVE BIT(7) |
5f2d04f1 PM |
56 | |
57 | u16 frag_max_size; | |
1da177e4 LT |
58 | }; |
59 | ||
a04a480d DA |
60 | static inline bool ipv4_l3mdev_skb(u16 flags) |
61 | { | |
62 | return !!(flags & IPSKB_L3SLAVE); | |
63 | } | |
64 | ||
c9bdd4b5 ACM |
65 | static inline unsigned int ip_hdrlen(const struct sk_buff *skb) |
66 | { | |
eddc9ec5 | 67 | return ip_hdr(skb)->ihl * 4; |
c9bdd4b5 ACM |
68 | } |
69 | ||
fd2c3ef7 | 70 | struct ipcm_cookie { |
24025c46 | 71 | struct sockcm_cookie sockc; |
c1d18f9f | 72 | __be32 addr; |
1da177e4 | 73 | int oif; |
f6d8bd05 | 74 | struct ip_options_rcu *opt; |
f02db315 FF |
75 | __u8 ttl; |
76 | __s16 tos; | |
77 | char priority; | |
bec1f6f6 | 78 | __u16 gso_size; |
1da177e4 LT |
79 | }; |
80 | ||
35178206 WB |
81 | static inline void ipcm_init(struct ipcm_cookie *ipcm) |
82 | { | |
83 | *ipcm = (struct ipcm_cookie) { .tos = -1 }; | |
84 | } | |
85 | ||
86 | static inline void ipcm_init_sk(struct ipcm_cookie *ipcm, | |
87 | const struct inet_sock *inet) | |
88 | { | |
89 | ipcm_init(ipcm); | |
90 | ||
91 | ipcm->sockc.tsflags = inet->sk.sk_tsflags; | |
92 | ipcm->oif = inet->sk.sk_bound_dev_if; | |
93 | ipcm->addr = inet->inet_saddr; | |
94 | } | |
95 | ||
1da177e4 | 96 | #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb)) |
4b261c75 | 97 | #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb)) |
1da177e4 | 98 | |
fb74c277 DA |
99 | /* return enslaved device index if relevant */ |
100 | static inline int inet_sdif(struct sk_buff *skb) | |
101 | { | |
102 | #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) | |
103 | if (skb && ipv4_l3mdev_skb(IPCB(skb)->flags)) | |
104 | return IPCB(skb)->iif; | |
105 | #endif | |
106 | return 0; | |
107 | } | |
108 | ||
5796ef75 KT |
109 | /* Special input handler for packets caught by router alert option. |
110 | They are selected only by protocol field, and then processed likely | |
111 | local ones; but only if someone wants them! Otherwise, router | |
112 | not running rsvpd will kill RSVP. | |
113 | ||
114 | It is user level problem, what it will make with them. | |
115 | I have no idea, how it will masquearde or NAT them (it is joke, joke :-)), | |
116 | but receiver should be enough clever f.e. to forward mtrace requests, | |
117 | sent to multicast group to reach destination designated router. | |
118 | */ | |
119 | ||
fd2c3ef7 | 120 | struct ip_ra_chain { |
43a951e9 | 121 | struct ip_ra_chain __rcu *next; |
1da177e4 | 122 | struct sock *sk; |
592fcb9d ED |
123 | union { |
124 | void (*destructor)(struct sock *); | |
125 | struct sock *saved_sk; | |
126 | }; | |
66018506 | 127 | struct rcu_head rcu; |
1da177e4 LT |
128 | }; |
129 | ||
1da177e4 LT |
130 | /* IP flags. */ |
131 | #define IP_CE 0x8000 /* Flag: "Congestion" */ | |
132 | #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ | |
133 | #define IP_MF 0x2000 /* Flag: "More Fragments" */ | |
134 | #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */ | |
135 | ||
136 | #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */ | |
137 | ||
14c85021 ACM |
138 | struct msghdr; |
139 | struct net_device; | |
140 | struct packet_type; | |
141 | struct rtable; | |
14c85021 ACM |
142 | struct sockaddr; |
143 | ||
72c1d3bd | 144 | int igmp_mc_init(void); |
1da177e4 LT |
145 | |
146 | /* | |
147 | * Functions provided by ip.c | |
148 | */ | |
149 | ||
cfe673b0 | 150 | int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, |
5c3a0fd7 JP |
151 | __be32 saddr, __be32 daddr, |
152 | struct ip_options_rcu *opt); | |
153 | int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, | |
154 | struct net_device *orig_dev); | |
17266ee9 EC |
155 | void ip_list_rcv(struct list_head *head, struct packet_type *pt, |
156 | struct net_device *orig_dev); | |
5c3a0fd7 | 157 | int ip_local_deliver(struct sk_buff *skb); |
68cb7d53 | 158 | void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int proto); |
5c3a0fd7 | 159 | int ip_mr_input(struct sk_buff *skb); |
ede2059d EB |
160 | int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
161 | int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb); | |
694869b3 EB |
162 | int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, |
163 | int (*output)(struct net *, struct sock *, struct sk_buff *)); | |
5c3a0fd7 | 164 | void ip_send_check(struct iphdr *ip); |
cf91a99d | 165 | int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
33224b16 | 166 | int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
aad88724 | 167 | |
69b9e1e0 XL |
168 | int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl, |
169 | __u8 tos); | |
5c3a0fd7 JP |
170 | void ip_init(void); |
171 | int ip_append_data(struct sock *sk, struct flowi4 *fl4, | |
172 | int getfrag(void *from, char *to, int offset, int len, | |
173 | int odd, struct sk_buff *skb), | |
174 | void *from, int len, int protolen, | |
175 | struct ipcm_cookie *ipc, | |
176 | struct rtable **rt, | |
177 | unsigned int flags); | |
178 | int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, | |
179 | struct sk_buff *skb); | |
180 | ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, | |
181 | int offset, size_t size, int flags); | |
182 | struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4, | |
183 | struct sk_buff_head *queue, | |
184 | struct inet_cork *cork); | |
185 | int ip_send_skb(struct net *net, struct sk_buff *skb); | |
186 | int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4); | |
187 | void ip_flush_pending_frames(struct sock *sk); | |
188 | struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4, | |
189 | int getfrag(void *from, char *to, int offset, | |
190 | int len, int odd, struct sk_buff *skb), | |
191 | void *from, int length, int transhdrlen, | |
192 | struct ipcm_cookie *ipc, struct rtable **rtp, | |
1cd7884d | 193 | struct inet_cork *cork, unsigned int flags); |
1c32c5ad | 194 | |
69b9e1e0 XL |
195 | static inline int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, |
196 | struct flowi *fl) | |
197 | { | |
198 | return __ip_queue_xmit(sk, skb, fl, inet_sk(sk)->tos); | |
199 | } | |
200 | ||
77968b78 | 201 | static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4) |
1c32c5ad | 202 | { |
77968b78 | 203 | return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base); |
1c32c5ad | 204 | } |
1da177e4 | 205 | |
aa661581 FF |
206 | static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet) |
207 | { | |
208 | return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos); | |
209 | } | |
210 | ||
211 | static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk) | |
212 | { | |
213 | return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk); | |
214 | } | |
215 | ||
1da177e4 | 216 | /* datagram.c */ |
03645a11 | 217 | int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); |
5c3a0fd7 | 218 | int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); |
1da177e4 | 219 | |
5c3a0fd7 | 220 | void ip4_datagram_release_cb(struct sock *sk); |
8141ed9f | 221 | |
1da177e4 | 222 | struct ip_reply_arg { |
82695b30 | 223 | struct kvec iov[1]; |
88ef4a5a | 224 | int flags; |
d6f5493c | 225 | __wsum csum; |
1da177e4 | 226 | int csumoffset; /* u16 offset of csum in iov[0].iov_base */ |
82695b30 | 227 | /* -1 if not needed */ |
f0e48dbf | 228 | int bound_dev_if; |
66b13d99 | 229 | u8 tos; |
e2d118a1 | 230 | kuid_t uid; |
82695b30 | 231 | }; |
1da177e4 | 232 | |
88ef4a5a KK |
233 | #define IP_REPLY_ARG_NOSRCCHECK 1 |
234 | ||
86b08d86 KK |
235 | static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg) |
236 | { | |
237 | return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0; | |
238 | } | |
239 | ||
bdbbb852 | 240 | void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, |
24a2d43d ED |
241 | const struct ip_options *sopt, |
242 | __be32 daddr, __be32 saddr, | |
243 | const struct ip_reply_arg *arg, | |
70e73416 | 244 | unsigned int len); |
1da177e4 | 245 | |
4ce3c183 | 246 | #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field) |
13415e46 | 247 | #define __IP_INC_STATS(net, field) __SNMP_INC_STATS64((net)->mib.ip_statistics, field) |
4ce3c183 | 248 | #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val) |
13415e46 | 249 | #define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val) |
4ce3c183 | 250 | #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val) |
13415e46 | 251 | #define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val) |
61a7e260 | 252 | #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field) |
13415e46 | 253 | #define __NET_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.net_statistics, field) |
f7324acd | 254 | #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd) |
13415e46 | 255 | #define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd) |
1da177e4 | 256 | |
c4c6bc31 | 257 | u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offct); |
698365fa | 258 | unsigned long snmp_fold_field(void __percpu *mib, int offt); |
4ce3c183 | 259 | #if BITS_PER_LONG==32 |
c4c6bc31 R |
260 | u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct, |
261 | size_t syncp_offset); | |
698365fa | 262 | u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off); |
4ce3c183 | 263 | #else |
c4c6bc31 R |
264 | static inline u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct, |
265 | size_t syncp_offset) | |
266 | { | |
267 | return snmp_get_cpu_field(mib, cpu, offct); | |
268 | ||
269 | } | |
270 | ||
698365fa | 271 | static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off) |
4ce3c183 ED |
272 | { |
273 | return snmp_fold_field(mib, offt); | |
274 | } | |
275 | #endif | |
33490170 | 276 | |
6348ef2d JH |
277 | #define snmp_get_cpu_field64_batch(buff64, stats_list, mib_statistic, offset) \ |
278 | { \ | |
279 | int i, c; \ | |
280 | for_each_possible_cpu(c) { \ | |
281 | for (i = 0; stats_list[i].name; i++) \ | |
282 | buff64[i] += snmp_get_cpu_field64( \ | |
283 | mib_statistic, \ | |
284 | c, stats_list[i].entry, \ | |
285 | offset); \ | |
286 | } \ | |
287 | } | |
288 | ||
289 | #define snmp_get_cpu_field_batch(buff, stats_list, mib_statistic) \ | |
290 | { \ | |
291 | int i, c; \ | |
292 | for_each_possible_cpu(c) { \ | |
293 | for (i = 0; stats_list[i].name; i++) \ | |
294 | buff[i] += snmp_get_cpu_field( \ | |
295 | mib_statistic, \ | |
296 | c, stats_list[i].entry); \ | |
297 | } \ | |
298 | } | |
299 | ||
0bbf87d8 | 300 | void inet_get_local_port_range(struct net *net, int *low, int *high); |
227b60f5 | 301 | |
fcd77db0 | 302 | #ifdef CONFIG_SYSCTL |
122ff243 | 303 | static inline int inet_is_local_reserved_port(struct net *net, int port) |
e3826f1e | 304 | { |
122ff243 WC |
305 | if (!net->ipv4.sysctl_local_reserved_ports) |
306 | return 0; | |
307 | return test_bit(port, net->ipv4.sysctl_local_reserved_ports); | |
e3826f1e | 308 | } |
20e61da7 WC |
309 | |
310 | static inline bool sysctl_dev_name_is_allowed(const char *name) | |
311 | { | |
312 | return strcmp(name, "default") != 0 && strcmp(name, "all") != 0; | |
313 | } | |
314 | ||
4548b683 KJ |
315 | static inline int inet_prot_sock(struct net *net) |
316 | { | |
317 | return net->ipv4.sysctl_ip_prot_sock; | |
318 | } | |
319 | ||
122ff243 WC |
320 | #else |
321 | static inline int inet_is_local_reserved_port(struct net *net, int port) | |
322 | { | |
323 | return 0; | |
324 | } | |
4548b683 KJ |
325 | |
326 | static inline int inet_prot_sock(struct net *net) | |
327 | { | |
328 | return PROT_SOCK; | |
329 | } | |
122ff243 | 330 | #endif |
e3826f1e | 331 | |
822c8685 DD |
332 | __be32 inet_current_timestamp(void); |
333 | ||
20380731 ACM |
334 | /* From inetpeer.c */ |
335 | extern int inet_peer_threshold; | |
336 | extern int inet_peer_minttl; | |
337 | extern int inet_peer_maxttl; | |
20380731 | 338 | |
5c3a0fd7 | 339 | void ipfrag_init(void); |
20380731 | 340 | |
5c3a0fd7 | 341 | void ip_static_sysctl_init(void); |
bd7b1533 | 342 | |
e110861f LC |
343 | #define IP4_REPLY_MARK(net, mark) \ |
344 | ((net)->ipv4.sysctl_fwmark_reflect ? (mark) : 0) | |
345 | ||
d18cd551 DM |
346 | static inline bool ip_is_fragment(const struct iphdr *iph) |
347 | { | |
348 | return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0; | |
349 | } | |
350 | ||
1da177e4 | 351 | #ifdef CONFIG_INET |
14c85021 ACM |
352 | #include <net/dst.h> |
353 | ||
1da177e4 LT |
354 | /* The function in 2.2 was invalid, producing wrong result for |
355 | * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */ | |
356 | static inline | |
357 | int ip_decrease_ttl(struct iphdr *iph) | |
358 | { | |
5c78f275 AV |
359 | u32 check = (__force u32)iph->check; |
360 | check += (__force u32)htons(0x0100); | |
361 | iph->check = (__force __sum16)(check + (check>=0xFFFF)); | |
1da177e4 LT |
362 | return --iph->ttl; |
363 | } | |
364 | ||
d52e5a7e SD |
365 | static inline int ip_mtu_locked(const struct dst_entry *dst) |
366 | { | |
367 | const struct rtable *rt = (const struct rtable *)dst; | |
368 | ||
369 | return rt->rt_mtu_locked || dst_metric_locked(dst, RTAX_MTU); | |
370 | } | |
371 | ||
1da177e4 | 372 | static inline |
4e3f5d72 | 373 | int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst) |
1da177e4 | 374 | { |
4e3f5d72 ED |
375 | u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc); |
376 | ||
377 | return pmtudisc == IP_PMTUDISC_DO || | |
378 | (pmtudisc == IP_PMTUDISC_WANT && | |
d52e5a7e | 379 | !ip_mtu_locked(dst)); |
1da177e4 LT |
380 | } |
381 | ||
f87c10a8 HFS |
382 | static inline bool ip_sk_accept_pmtu(const struct sock *sk) |
383 | { | |
1b346576 HFS |
384 | return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE && |
385 | inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT; | |
f87c10a8 HFS |
386 | } |
387 | ||
388 | static inline bool ip_sk_use_pmtu(const struct sock *sk) | |
389 | { | |
390 | return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE; | |
391 | } | |
392 | ||
60ff7467 | 393 | static inline bool ip_sk_ignore_df(const struct sock *sk) |
1b346576 HFS |
394 | { |
395 | return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO || | |
396 | inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT; | |
397 | } | |
398 | ||
f87c10a8 HFS |
399 | static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst, |
400 | bool forwarding) | |
401 | { | |
402 | struct net *net = dev_net(dst->dev); | |
403 | ||
404 | if (net->ipv4.sysctl_ip_fwd_use_pmtu || | |
d52e5a7e | 405 | ip_mtu_locked(dst) || |
f87c10a8 HFS |
406 | !forwarding) |
407 | return dst_mtu(dst); | |
408 | ||
c780a049 | 409 | return min(READ_ONCE(dst->dev->mtu), IP_MAX_MTU); |
f87c10a8 HFS |
410 | } |
411 | ||
fedbb6b4 SL |
412 | static inline unsigned int ip_skb_dst_mtu(struct sock *sk, |
413 | const struct sk_buff *skb) | |
f87c10a8 | 414 | { |
caf3f267 | 415 | if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) { |
f87c10a8 | 416 | bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED; |
caf3f267 | 417 | |
f87c10a8 | 418 | return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding); |
f87c10a8 | 419 | } |
caf3f267 | 420 | |
c780a049 | 421 | return min(READ_ONCE(skb_dst(skb)->dev->mtu), IP_MAX_MTU); |
f87c10a8 HFS |
422 | } |
423 | ||
767a2217 | 424 | struct dst_metrics *ip_fib_metrics_init(struct net *net, struct nlattr *fc_mx, |
d7e774f3 DA |
425 | int fc_mx_len, |
426 | struct netlink_ext_ack *extack); | |
cc5f0eb2 DA |
427 | static inline void ip_fib_metrics_put(struct dst_metrics *fib_metrics) |
428 | { | |
429 | if (fib_metrics != &dst_default_metrics && | |
430 | refcount_dec_and_test(&fib_metrics->refcnt)) | |
431 | kfree(fib_metrics); | |
432 | } | |
a919525a | 433 | |
e1255ed4 DA |
434 | /* ipv4 and ipv6 both use refcounted metrics if it is not the default */ |
435 | static inline | |
436 | void ip_dst_init_metrics(struct dst_entry *dst, struct dst_metrics *fib_metrics) | |
437 | { | |
438 | dst_init_metrics(dst, fib_metrics->metrics, true); | |
439 | ||
440 | if (fib_metrics != &dst_default_metrics) { | |
441 | dst->_metrics |= DST_METRICS_REFCOUNTED; | |
442 | refcount_inc(&fib_metrics->refcnt); | |
443 | } | |
444 | } | |
445 | ||
1620a336 DA |
446 | static inline |
447 | void ip_dst_metrics_put(struct dst_entry *dst) | |
448 | { | |
449 | struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst); | |
450 | ||
451 | if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt)) | |
452 | kfree(p); | |
453 | } | |
454 | ||
04ca6973 | 455 | u32 ip_idents_reserve(u32 hash, int segs); |
b6a7719a | 456 | void __ip_select_ident(struct net *net, struct iphdr *iph, int segs); |
73f156a6 | 457 | |
b6a7719a HFS |
458 | static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb, |
459 | struct sock *sk, int segs) | |
1da177e4 | 460 | { |
703133de AA |
461 | struct iphdr *iph = ip_hdr(skb); |
462 | ||
60ff7467 | 463 | if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) { |
1da177e4 LT |
464 | /* This is only to work around buggy Windows95/2000 |
465 | * VJ compression implementations. If the ID field | |
466 | * does not change, they drop every other packet in | |
467 | * a TCP stream using header compression. | |
468 | */ | |
c720c7e8 ED |
469 | if (sk && inet_sk(sk)->inet_daddr) { |
470 | iph->id = htons(inet_sk(sk)->inet_id); | |
73f156a6 ED |
471 | inet_sk(sk)->inet_id += segs; |
472 | } else { | |
1da177e4 | 473 | iph->id = 0; |
73f156a6 ED |
474 | } |
475 | } else { | |
b6a7719a | 476 | __ip_select_ident(net, iph, segs); |
73f156a6 ED |
477 | } |
478 | } | |
479 | ||
b6a7719a HFS |
480 | static inline void ip_select_ident(struct net *net, struct sk_buff *skb, |
481 | struct sock *sk) | |
73f156a6 | 482 | { |
b6a7719a | 483 | ip_select_ident_segs(net, skb, sk, 1); |
1da177e4 LT |
484 | } |
485 | ||
ed70fcfc TH |
486 | static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto) |
487 | { | |
488 | return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, | |
489 | skb->len, proto, 0); | |
490 | } | |
491 | ||
c3f83241 TH |
492 | /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store |
493 | * Equivalent to : flow->v4addrs.src = iph->saddr; | |
494 | * flow->v4addrs.dst = iph->daddr; | |
495 | */ | |
496 | static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow, | |
497 | const struct iphdr *iph) | |
498 | { | |
499 | BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) != | |
500 | offsetof(typeof(flow->addrs), v4addrs.src) + | |
501 | sizeof(flow->addrs.v4addrs.src)); | |
502 | memcpy(&flow->addrs.v4addrs, &iph->saddr, sizeof(flow->addrs.v4addrs)); | |
503 | flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
504 | } | |
505 | ||
1933a785 TH |
506 | static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto) |
507 | { | |
508 | const struct iphdr *iph = skb_gro_network_header(skb); | |
509 | ||
510 | return csum_tcpudp_nofold(iph->saddr, iph->daddr, | |
511 | skb_gro_len(skb), proto, 0); | |
512 | } | |
513 | ||
1da177e4 LT |
514 | /* |
515 | * Map a multicast IP onto multicast MAC for type ethernet. | |
516 | */ | |
517 | ||
714e85be | 518 | static inline void ip_eth_mc_map(__be32 naddr, char *buf) |
1da177e4 | 519 | { |
714e85be | 520 | __u32 addr=ntohl(naddr); |
1da177e4 LT |
521 | buf[0]=0x01; |
522 | buf[1]=0x00; | |
523 | buf[2]=0x5e; | |
524 | buf[5]=addr&0xFF; | |
525 | addr>>=8; | |
526 | buf[4]=addr&0xFF; | |
527 | addr>>=8; | |
528 | buf[3]=addr&0x7F; | |
529 | } | |
530 | ||
531 | /* | |
532 | * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand. | |
533 | * Leave P_Key as 0 to be filled in by driver. | |
534 | */ | |
535 | ||
a9e527e3 | 536 | static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) |
1da177e4 | 537 | { |
714e85be | 538 | __u32 addr; |
a9e527e3 RM |
539 | unsigned char scope = broadcast[5] & 0xF; |
540 | ||
1da177e4 LT |
541 | buf[0] = 0; /* Reserved */ |
542 | buf[1] = 0xff; /* Multicast QPN */ | |
543 | buf[2] = 0xff; | |
544 | buf[3] = 0xff; | |
714e85be | 545 | addr = ntohl(naddr); |
1da177e4 | 546 | buf[4] = 0xff; |
a9e527e3 | 547 | buf[5] = 0x10 | scope; /* scope from broadcast address */ |
1da177e4 LT |
548 | buf[6] = 0x40; /* IPv4 signature */ |
549 | buf[7] = 0x1b; | |
a9e527e3 RM |
550 | buf[8] = broadcast[8]; /* P_Key */ |
551 | buf[9] = broadcast[9]; | |
1da177e4 LT |
552 | buf[10] = 0; |
553 | buf[11] = 0; | |
554 | buf[12] = 0; | |
555 | buf[13] = 0; | |
556 | buf[14] = 0; | |
557 | buf[15] = 0; | |
558 | buf[19] = addr & 0xff; | |
559 | addr >>= 8; | |
560 | buf[18] = addr & 0xff; | |
561 | addr >>= 8; | |
562 | buf[17] = addr & 0xff; | |
563 | addr >>= 8; | |
564 | buf[16] = addr & 0x0f; | |
565 | } | |
566 | ||
93ca3bb5 TT |
567 | static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) |
568 | { | |
569 | if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) | |
570 | memcpy(buf, broadcast, 4); | |
571 | else | |
572 | memcpy(buf, &naddr, sizeof(naddr)); | |
573 | } | |
574 | ||
dfd56b8b | 575 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 LT |
576 | #include <linux/ipv6.h> |
577 | #endif | |
578 | ||
579 | static __inline__ void inet_reset_saddr(struct sock *sk) | |
580 | { | |
c720c7e8 | 581 | inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0; |
dfd56b8b | 582 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 LT |
583 | if (sk->sk_family == PF_INET6) { |
584 | struct ipv6_pinfo *np = inet6_sk(sk); | |
585 | ||
586 | memset(&np->saddr, 0, sizeof(np->saddr)); | |
efe4208f | 587 | memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); |
1da177e4 LT |
588 | } |
589 | #endif | |
590 | } | |
591 | ||
592 | #endif | |
593 | ||
72afa352 DA |
594 | static inline unsigned int ipv4_addr_hash(__be32 ip) |
595 | { | |
596 | return (__force unsigned int) ip; | |
597 | } | |
598 | ||
f0b1e64c MKL |
599 | static inline u32 ipv4_portaddr_hash(const struct net *net, |
600 | __be32 saddr, | |
601 | unsigned int port) | |
602 | { | |
603 | return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; | |
604 | } | |
605 | ||
5c3a0fd7 | 606 | bool ip_call_ra_chain(struct sk_buff *skb); |
1da177e4 LT |
607 | |
608 | /* | |
b798232f | 609 | * Functions provided by ip_fragment.c |
1da177e4 LT |
610 | */ |
611 | ||
fd2c3ef7 | 612 | enum ip_defrag_users { |
1da177e4 LT |
613 | IP_DEFRAG_LOCAL_DELIVER, |
614 | IP_DEFRAG_CALL_RA_CHAIN, | |
615 | IP_DEFRAG_CONNTRACK_IN, | |
4be929be | 616 | __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX, |
1da177e4 | 617 | IP_DEFRAG_CONNTRACK_OUT, |
4be929be | 618 | __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX, |
8fa9ff68 | 619 | IP_DEFRAG_CONNTRACK_BRIDGE_IN, |
4be929be | 620 | __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, |
1da177e4 LT |
621 | IP_DEFRAG_VS_IN, |
622 | IP_DEFRAG_VS_OUT, | |
595fc71b DM |
623 | IP_DEFRAG_VS_FWD, |
624 | IP_DEFRAG_AF_PACKET, | |
bc416d97 | 625 | IP_DEFRAG_MACVLAN, |
1da177e4 LT |
626 | }; |
627 | ||
5cf42280 AZ |
628 | /* Return true if the value of 'user' is between 'lower_bond' |
629 | * and 'upper_bond' inclusively. | |
630 | */ | |
631 | static inline bool ip_defrag_user_in_between(u32 user, | |
632 | enum ip_defrag_users lower_bond, | |
633 | enum ip_defrag_users upper_bond) | |
634 | { | |
635 | return user >= lower_bond && user <= upper_bond; | |
636 | } | |
637 | ||
19bcf9f2 | 638 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user); |
bc416d97 | 639 | #ifdef CONFIG_INET |
19bcf9f2 | 640 | struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user); |
bc416d97 | 641 | #else |
19bcf9f2 | 642 | static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) |
bc416d97 ED |
643 | { |
644 | return skb; | |
645 | } | |
646 | #endif | |
1da177e4 LT |
647 | |
648 | /* | |
649 | * Functions provided by ip_forward.c | |
650 | */ | |
82695b30 | 651 | |
5c3a0fd7 | 652 | int ip_forward(struct sk_buff *skb); |
82695b30 | 653 | |
1da177e4 LT |
654 | /* |
655 | * Functions provided by ip_options.c | |
656 | */ | |
82695b30 | 657 | |
5c3a0fd7 JP |
658 | void ip_options_build(struct sk_buff *skb, struct ip_options *opt, |
659 | __be32 daddr, struct rtable *rt, int is_frag); | |
24a2d43d | 660 | |
91ed1e66 PA |
661 | int __ip_options_echo(struct net *net, struct ip_options *dopt, |
662 | struct sk_buff *skb, const struct ip_options *sopt); | |
663 | static inline int ip_options_echo(struct net *net, struct ip_options *dopt, | |
664 | struct sk_buff *skb) | |
24a2d43d | 665 | { |
91ed1e66 | 666 | return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt); |
24a2d43d ED |
667 | } |
668 | ||
5c3a0fd7 | 669 | void ip_options_fragment(struct sk_buff *skb); |
3da1ed7a NS |
670 | int __ip_options_compile(struct net *net, struct ip_options *opt, |
671 | struct sk_buff *skb, __be32 *info); | |
5c3a0fd7 JP |
672 | int ip_options_compile(struct net *net, struct ip_options *opt, |
673 | struct sk_buff *skb); | |
674 | int ip_options_get(struct net *net, struct ip_options_rcu **optp, | |
675 | unsigned char *data, int optlen); | |
676 | int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp, | |
677 | unsigned char __user *data, int optlen); | |
678 | void ip_options_undo(struct ip_options *opt); | |
679 | void ip_forward_options(struct sk_buff *skb); | |
8c83f2df | 680 | int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev); |
1da177e4 LT |
681 | |
682 | /* | |
683 | * Functions provided by ip_sockglue.c | |
684 | */ | |
685 | ||
fbf8866d | 686 | void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb); |
ad959036 PA |
687 | void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, |
688 | struct sk_buff *skb, int tlen, int offset); | |
24025c46 | 689 | int ip_cmsg_send(struct sock *sk, struct msghdr *msg, |
c8e6ad08 | 690 | struct ipcm_cookie *ipc, bool allow_ipv6); |
5c3a0fd7 JP |
691 | int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval, |
692 | unsigned int optlen); | |
693 | int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, | |
694 | int __user *optlen); | |
695 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, | |
696 | char __user *optval, unsigned int optlen); | |
697 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, | |
698 | char __user *optval, int __user *optlen); | |
699 | int ip_ra_control(struct sock *sk, unsigned char on, | |
700 | void (*destructor)(struct sock *)); | |
701 | ||
85fbaa75 | 702 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len); |
5c3a0fd7 JP |
703 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, |
704 | u32 info, u8 *payload); | |
705 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport, | |
706 | u32 info); | |
1da177e4 | 707 | |
5961de9f TH |
708 | static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) |
709 | { | |
ad959036 | 710 | ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0); |
5961de9f TH |
711 | } |
712 | ||
4cdf507d ED |
713 | bool icmp_global_allow(void); |
714 | extern int sysctl_icmp_msgs_per_sec; | |
715 | extern int sysctl_icmp_msgs_burst; | |
716 | ||
20380731 | 717 | #ifdef CONFIG_PROC_FS |
5c3a0fd7 | 718 | int ip_misc_proc_init(void); |
20380731 ACM |
719 | #endif |
720 | ||
5e1a99ea | 721 | int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family, |
404eb77e RP |
722 | struct netlink_ext_ack *extack); |
723 | ||
1da177e4 | 724 | #endif /* _IP_H */ |