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