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