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 static inline unsigned int ip_skb_dst_mtu(struct sock *sk, 517 const struct sk_buff *skb) 518 { 519 const struct dst_entry *dst = skb_dst(skb); 520 unsigned int mtu; 521 522 if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) { 523 bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED; 524 525 return ip_dst_mtu_maybe_forward(dst, forwarding); 526 } 527 528 mtu = min(READ_ONCE(dst_dev(dst)->mtu), IP_MAX_MTU); 529 return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 530 } 531 532 struct dst_metrics *ip_fib_metrics_init(struct nlattr *fc_mx, int fc_mx_len, 533 struct netlink_ext_ack *extack); 534 static inline void ip_fib_metrics_put(struct dst_metrics *fib_metrics) 535 { 536 if (fib_metrics != &dst_default_metrics && 537 refcount_dec_and_test(&fib_metrics->refcnt)) 538 kfree(fib_metrics); 539 } 540 541 /* ipv4 and ipv6 both use refcounted metrics if it is not the default */ 542 static inline 543 void ip_dst_init_metrics(struct dst_entry *dst, struct dst_metrics *fib_metrics) 544 { 545 dst_init_metrics(dst, fib_metrics->metrics, true); 546 547 if (fib_metrics != &dst_default_metrics) { 548 dst->_metrics |= DST_METRICS_REFCOUNTED; 549 refcount_inc(&fib_metrics->refcnt); 550 } 551 } 552 553 static inline 554 void ip_dst_metrics_put(struct dst_entry *dst) 555 { 556 struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst); 557 558 if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt)) 559 kfree(p); 560 } 561 562 void __ip_select_ident(struct net *net, struct iphdr *iph, int segs); 563 564 static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb, 565 struct sock *sk, int segs) 566 { 567 struct iphdr *iph = ip_hdr(skb); 568 569 /* RFC 6864: the IPv4 ID of atomic datagrams has no meaning. 570 * DF packets without ignore_df can not be fragmented. 571 */ 572 if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) { 573 iph->id = 0; 574 return; 575 } 576 /* We had many attacks based on IPID, use the private 577 * generator as much as we can. 578 */ 579 if (sk && inet_sk(sk)->inet_daddr) { 580 int val; 581 582 /* avoid atomic operations for TCP, 583 * as we hold socket lock at this point. 584 */ 585 if (sk_is_tcp(sk)) { 586 sock_owned_by_me(sk); 587 val = atomic_read(&inet_sk(sk)->inet_id); 588 atomic_set(&inet_sk(sk)->inet_id, val + segs); 589 } else { 590 val = atomic_fetch_add(segs, &inet_sk(sk)->inet_id); 591 } 592 iph->id = htons(val); 593 return; 594 } 595 /* Unfortunately we need the big hammer to get a suitable IPID */ 596 __ip_select_ident(net, iph, segs); 597 } 598 599 static inline void ip_select_ident(struct net *net, struct sk_buff *skb, 600 struct sock *sk) 601 { 602 ip_select_ident_segs(net, skb, sk, 1); 603 } 604 605 static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto) 606 { 607 return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, 608 skb->len, proto, 0); 609 } 610 611 /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store 612 * Equivalent to : flow->v4addrs.src = iph->saddr; 613 * flow->v4addrs.dst = iph->daddr; 614 */ 615 static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow, 616 const struct iphdr *iph) 617 { 618 BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) != 619 offsetof(typeof(flow->addrs), v4addrs.src) + 620 sizeof(flow->addrs.v4addrs.src)); 621 memcpy(&flow->addrs.v4addrs, &iph->addrs, sizeof(flow->addrs.v4addrs)); 622 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 623 } 624 625 /* 626 * Map a multicast IP onto multicast MAC for type ethernet. 627 */ 628 629 static inline void ip_eth_mc_map(__be32 naddr, char *buf) 630 { 631 __u32 addr=ntohl(naddr); 632 buf[0]=0x01; 633 buf[1]=0x00; 634 buf[2]=0x5e; 635 buf[5]=addr&0xFF; 636 addr>>=8; 637 buf[4]=addr&0xFF; 638 addr>>=8; 639 buf[3]=addr&0x7F; 640 } 641 642 /* 643 * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand. 644 * Leave P_Key as 0 to be filled in by driver. 645 */ 646 647 static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 648 { 649 __u32 addr; 650 unsigned char scope = broadcast[5] & 0xF; 651 652 buf[0] = 0; /* Reserved */ 653 buf[1] = 0xff; /* Multicast QPN */ 654 buf[2] = 0xff; 655 buf[3] = 0xff; 656 addr = ntohl(naddr); 657 buf[4] = 0xff; 658 buf[5] = 0x10 | scope; /* scope from broadcast address */ 659 buf[6] = 0x40; /* IPv4 signature */ 660 buf[7] = 0x1b; 661 buf[8] = broadcast[8]; /* P_Key */ 662 buf[9] = broadcast[9]; 663 buf[10] = 0; 664 buf[11] = 0; 665 buf[12] = 0; 666 buf[13] = 0; 667 buf[14] = 0; 668 buf[15] = 0; 669 buf[19] = addr & 0xff; 670 addr >>= 8; 671 buf[18] = addr & 0xff; 672 addr >>= 8; 673 buf[17] = addr & 0xff; 674 addr >>= 8; 675 buf[16] = addr & 0x0f; 676 } 677 678 static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 679 { 680 if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) 681 memcpy(buf, broadcast, 4); 682 else 683 memcpy(buf, &naddr, sizeof(naddr)); 684 } 685 686 #if IS_ENABLED(CONFIG_IPV6) 687 #include <linux/ipv6.h> 688 #endif 689 690 static __inline__ void inet_reset_saddr(struct sock *sk) 691 { 692 inet_sk(sk)->inet_saddr = 0; 693 WRITE_ONCE(inet_sk(sk)->inet_rcv_saddr, 0); 694 #if IS_ENABLED(CONFIG_IPV6) 695 if (sk->sk_family == PF_INET6) { 696 struct ipv6_pinfo *np = inet6_sk(sk); 697 698 memset(&np->saddr, 0, sizeof(np->saddr)); 699 memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); 700 } 701 #endif 702 } 703 704 #endif 705 706 static inline unsigned int ipv4_addr_hash(__be32 ip) 707 { 708 return (__force unsigned int) ip; 709 } 710 711 static inline u32 __ipv4_addr_hash(const __be32 ip, const u32 initval) 712 { 713 return jhash_1word((__force u32)ip, initval); 714 } 715 716 static inline u32 ipv4_portaddr_hash(const struct net *net, 717 __be32 saddr, 718 unsigned int port) 719 { 720 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; 721 } 722 723 bool ip_call_ra_chain(struct sk_buff *skb); 724 725 /* 726 * Functions provided by ip_fragment.c 727 */ 728 729 enum ip_defrag_users { 730 IP_DEFRAG_LOCAL_DELIVER, 731 IP_DEFRAG_CALL_RA_CHAIN, 732 IP_DEFRAG_CONNTRACK_IN, 733 __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX, 734 IP_DEFRAG_CONNTRACK_OUT, 735 __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 736 IP_DEFRAG_CONNTRACK_BRIDGE_IN, 737 __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 738 IP_DEFRAG_VS_IN, 739 IP_DEFRAG_VS_OUT, 740 IP_DEFRAG_VS_FWD, 741 IP_DEFRAG_AF_PACKET, 742 IP_DEFRAG_MACVLAN, 743 }; 744 745 /* Return true if the value of 'user' is between 'lower_bond' 746 * and 'upper_bond' inclusively. 747 */ 748 static inline bool ip_defrag_user_in_between(u32 user, 749 enum ip_defrag_users lower_bond, 750 enum ip_defrag_users upper_bond) 751 { 752 return user >= lower_bond && user <= upper_bond; 753 } 754 755 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user); 756 #ifdef CONFIG_INET 757 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user); 758 #else 759 static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) 760 { 761 return skb; 762 } 763 #endif 764 765 /* 766 * Functions provided by ip_forward.c 767 */ 768 769 int ip_forward(struct sk_buff *skb); 770 771 /* 772 * Functions provided by ip_options.c 773 */ 774 775 void ip_options_build(struct sk_buff *skb, struct ip_options *opt, 776 __be32 daddr, struct rtable *rt); 777 778 int __ip_options_echo(struct net *net, struct ip_options *dopt, 779 struct sk_buff *skb, const struct ip_options *sopt); 780 static inline int ip_options_echo(struct net *net, struct ip_options *dopt, 781 struct sk_buff *skb) 782 { 783 return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt); 784 } 785 786 void ip_options_fragment(struct sk_buff *skb); 787 int __ip_options_compile(struct net *net, struct ip_options *opt, 788 struct sk_buff *skb, __be32 *info); 789 int ip_options_compile(struct net *net, struct ip_options *opt, 790 struct sk_buff *skb); 791 int ip_options_get(struct net *net, struct ip_options_rcu **optp, 792 sockptr_t data, int optlen); 793 void ip_options_undo(struct ip_options *opt); 794 void ip_forward_options(struct sk_buff *skb); 795 int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev); 796 797 /* 798 * Functions provided by ip_sockglue.c 799 */ 800 801 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb, bool drop_dst); 802 void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, 803 struct sk_buff *skb, int tlen, int offset); 804 int ip_cmsg_send(struct sock *sk, struct msghdr *msg, 805 struct ipcm_cookie *ipc, bool allow_ipv6); 806 DECLARE_STATIC_KEY_FALSE(ip4_min_ttl); 807 int do_ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 808 unsigned int optlen); 809 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 810 unsigned int optlen); 811 int do_ip_getsockopt(struct sock *sk, int level, int optname, 812 sockptr_t optval, sockptr_t optlen); 813 int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, 814 int __user *optlen); 815 int ip_ra_control(struct sock *sk, unsigned char on, 816 void (*destructor)(struct sock *)); 817 818 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len); 819 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 820 u32 info, u8 *payload); 821 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport, 822 u32 info); 823 824 static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) 825 { 826 ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0); 827 } 828 829 bool icmp_global_allow(struct net *net); 830 void icmp_global_consume(struct net *net); 831 832 #ifdef CONFIG_PROC_FS 833 int ip_misc_proc_init(void); 834 #endif 835 836 int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family, 837 struct netlink_ext_ack *extack); 838 839 static inline bool inetdev_valid_mtu(unsigned int mtu) 840 { 841 return likely(mtu >= IPV4_MIN_MTU); 842 } 843 844 void ip_sock_set_freebind(struct sock *sk); 845 int ip_sock_set_mtu_discover(struct sock *sk, int val); 846 void ip_sock_set_pktinfo(struct sock *sk); 847 void ip_sock_set_recverr(struct sock *sk); 848 void ip_sock_set_tos(struct sock *sk, int val); 849 void __ip_sock_set_tos(struct sock *sk, int val); 850 851 #endif /* _IP_H */ 852