11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Definitions for the IP module. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Version: @(#)ip.h 1.0.2 05/07/93 91da177e4SLinus Torvalds * 1002c30a84SJesper Juhl * Authors: Ross Biro 111da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 121da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Changes: 151da177e4SLinus Torvalds * Mike McLagan : Routing by source 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 181da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 191da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 201da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds #ifndef _IP_H 231da177e4SLinus Torvalds #define _IP_H 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/types.h> 261da177e4SLinus Torvalds #include <linux/ip.h> 271da177e4SLinus Torvalds #include <linux/in.h> 28c9bdd4b5SArnaldo Carvalho de Melo #include <linux/skbuff.h> 2914c85021SArnaldo Carvalho de Melo 3014c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 31aa661581SFrancesco Fusco #include <net/route.h> 321da177e4SLinus Torvalds #include <net/snmp.h> 3386b08d86SKOVACS Krisztian #include <net/flow.h> 341bd758ebSJiri Pirko #include <net/flow_dissector.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds struct sock; 371da177e4SLinus Torvalds 38fd2c3ef7SEric Dumazet struct inet_skb_parm { 391da177e4SLinus Torvalds struct ip_options opt; /* Compiled IP options */ 401da177e4SLinus Torvalds unsigned char flags; 411da177e4SLinus Torvalds 42df4d9254SHannes Frederic Sowa #define IPSKB_FORWARDED BIT(0) 43df4d9254SHannes Frederic Sowa #define IPSKB_XFRM_TUNNEL_SIZE BIT(1) 44df4d9254SHannes Frederic Sowa #define IPSKB_XFRM_TRANSFORMED BIT(2) 45df4d9254SHannes Frederic Sowa #define IPSKB_FRAG_COMPLETE BIT(3) 46df4d9254SHannes Frederic Sowa #define IPSKB_REROUTED BIT(4) 47df4d9254SHannes Frederic Sowa #define IPSKB_DOREDIRECT BIT(5) 485f2d04f1SPatrick McHardy 495f2d04f1SPatrick McHardy u16 frag_max_size; 501da177e4SLinus Torvalds }; 511da177e4SLinus Torvalds 52c9bdd4b5SArnaldo Carvalho de Melo static inline unsigned int ip_hdrlen(const struct sk_buff *skb) 53c9bdd4b5SArnaldo Carvalho de Melo { 54eddc9ec5SArnaldo Carvalho de Melo return ip_hdr(skb)->ihl * 4; 55c9bdd4b5SArnaldo Carvalho de Melo } 56c9bdd4b5SArnaldo Carvalho de Melo 57fd2c3ef7SEric Dumazet struct ipcm_cookie { 58c1d18f9fSAl Viro __be32 addr; 591da177e4SLinus Torvalds int oif; 60f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 612244d07bSOliver Hartkopp __u8 tx_flags; 62f02db315SFrancesco Fusco __u8 ttl; 63f02db315SFrancesco Fusco __s16 tos; 64f02db315SFrancesco Fusco char priority; 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb)) 684b261c75SHannes Frederic Sowa #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb)) 691da177e4SLinus Torvalds 70fd2c3ef7SEric Dumazet struct ip_ra_chain { 7143a951e9SEric Dumazet struct ip_ra_chain __rcu *next; 721da177e4SLinus Torvalds struct sock *sk; 73592fcb9dSEric Dumazet union { 741da177e4SLinus Torvalds void (*destructor)(struct sock *); 75592fcb9dSEric Dumazet struct sock *saved_sk; 76592fcb9dSEric Dumazet }; 7766018506SEric Dumazet struct rcu_head rcu; 781da177e4SLinus Torvalds }; 791da177e4SLinus Torvalds 8043a951e9SEric Dumazet extern struct ip_ra_chain __rcu *ip_ra_chain; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds /* IP flags. */ 831da177e4SLinus Torvalds #define IP_CE 0x8000 /* Flag: "Congestion" */ 841da177e4SLinus Torvalds #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ 851da177e4SLinus Torvalds #define IP_MF 0x2000 /* Flag: "More Fragments" */ 861da177e4SLinus Torvalds #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */ 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */ 891da177e4SLinus Torvalds 9014c85021SArnaldo Carvalho de Melo struct msghdr; 9114c85021SArnaldo Carvalho de Melo struct net_device; 9214c85021SArnaldo Carvalho de Melo struct packet_type; 9314c85021SArnaldo Carvalho de Melo struct rtable; 9414c85021SArnaldo Carvalho de Melo struct sockaddr; 9514c85021SArnaldo Carvalho de Melo 9672c1d3bdSWANG Cong int igmp_mc_init(void); 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * Functions provided by ip.c 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds 1025c3a0fd7SJoe Perches int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, 10313d8eaa0SAl Viro __be32 saddr, __be32 daddr, 104f6d8bd05SEric Dumazet struct ip_options_rcu *opt); 1055c3a0fd7SJoe Perches int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, 1065c3a0fd7SJoe Perches struct net_device *orig_dev); 1075c3a0fd7SJoe Perches int ip_local_deliver(struct sk_buff *skb); 1085c3a0fd7SJoe Perches int ip_mr_input(struct sk_buff *skb); 109aad88724SEric Dumazet int ip_output(struct sock *sk, struct sk_buff *skb); 110aad88724SEric Dumazet int ip_mc_output(struct sock *sk, struct sk_buff *skb); 1117026b1ddSDavid Miller int ip_fragment(struct sock *sk, struct sk_buff *skb, 1127026b1ddSDavid Miller int (*output)(struct sock *, struct sk_buff *)); 1135c3a0fd7SJoe Perches int ip_do_nat(struct sk_buff *skb); 1145c3a0fd7SJoe Perches void ip_send_check(struct iphdr *ip); 1155c3a0fd7SJoe Perches int __ip_local_out(struct sk_buff *skb); 116aad88724SEric Dumazet int ip_local_out_sk(struct sock *sk, struct sk_buff *skb); 117aad88724SEric Dumazet static inline int ip_local_out(struct sk_buff *skb) 118aad88724SEric Dumazet { 119aad88724SEric Dumazet return ip_local_out_sk(skb->sk, skb); 120aad88724SEric Dumazet } 121aad88724SEric Dumazet 122b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl); 1235c3a0fd7SJoe Perches void ip_init(void); 1245c3a0fd7SJoe Perches int ip_append_data(struct sock *sk, struct flowi4 *fl4, 1251da177e4SLinus Torvalds int getfrag(void *from, char *to, int offset, int len, 1261da177e4SLinus Torvalds int odd, struct sk_buff *skb), 1271da177e4SLinus Torvalds void *from, int len, int protolen, 1281da177e4SLinus Torvalds struct ipcm_cookie *ipc, 1292e77d89bSEric Dumazet struct rtable **rt, 1301da177e4SLinus Torvalds unsigned int flags); 1315c3a0fd7SJoe Perches int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, 1325c3a0fd7SJoe Perches struct sk_buff *skb); 1335c3a0fd7SJoe Perches ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 1341da177e4SLinus Torvalds int offset, size_t size, int flags); 1355c3a0fd7SJoe Perches struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1361c32c5adSHerbert Xu struct sk_buff_head *queue, 1371c32c5adSHerbert Xu struct inet_cork *cork); 1385c3a0fd7SJoe Perches int ip_send_skb(struct net *net, struct sk_buff *skb); 1395c3a0fd7SJoe Perches int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4); 1405c3a0fd7SJoe Perches void ip_flush_pending_frames(struct sock *sk); 1415c3a0fd7SJoe Perches struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1425c3a0fd7SJoe Perches int getfrag(void *from, char *to, int offset, 1435c3a0fd7SJoe Perches int len, int odd, struct sk_buff *skb), 1441c32c5adSHerbert Xu void *from, int length, int transhdrlen, 1455c3a0fd7SJoe Perches struct ipcm_cookie *ipc, struct rtable **rtp, 1461c32c5adSHerbert Xu unsigned int flags); 1471c32c5adSHerbert Xu 14877968b78SDavid S. Miller static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4) 1491c32c5adSHerbert Xu { 15077968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 1511c32c5adSHerbert Xu } 1521da177e4SLinus Torvalds 153aa661581SFrancesco Fusco static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet) 154aa661581SFrancesco Fusco { 155aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos); 156aa661581SFrancesco Fusco } 157aa661581SFrancesco Fusco 158aa661581SFrancesco Fusco static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk) 159aa661581SFrancesco Fusco { 160aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk); 161aa661581SFrancesco Fusco } 162aa661581SFrancesco Fusco 1631da177e4SLinus Torvalds /* datagram.c */ 1645c3a0fd7SJoe Perches int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 1651da177e4SLinus Torvalds 1665c3a0fd7SJoe Perches void ip4_datagram_release_cb(struct sock *sk); 1678141ed9fSSteffen Klassert 1681da177e4SLinus Torvalds struct ip_reply_arg { 1691da177e4SLinus Torvalds struct kvec iov[1]; 17088ef4a5aSKOVACS Krisztian int flags; 171d6f5493cSAl Viro __wsum csum; 1721da177e4SLinus Torvalds int csumoffset; /* u16 offset of csum in iov[0].iov_base */ 1731da177e4SLinus Torvalds /* -1 if not needed */ 174f0e48dbfSPatrick McHardy int bound_dev_if; 17566b13d99SEric Dumazet u8 tos; 1761da177e4SLinus Torvalds }; 1771da177e4SLinus Torvalds 17888ef4a5aSKOVACS Krisztian #define IP_REPLY_ARG_NOSRCCHECK 1 17988ef4a5aSKOVACS Krisztian 18086b08d86SKOVACS Krisztian static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg) 18186b08d86SKOVACS Krisztian { 18286b08d86SKOVACS Krisztian return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0; 18386b08d86SKOVACS Krisztian } 18486b08d86SKOVACS Krisztian 185bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, 18624a2d43dSEric Dumazet const struct ip_options *sopt, 18724a2d43dSEric Dumazet __be32 daddr, __be32 saddr, 18824a2d43dSEric Dumazet const struct ip_reply_arg *arg, 18970e73416SDavid S. Miller unsigned int len); 1901da177e4SLinus Torvalds 1914ce3c183SEric Dumazet #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field) 1924ce3c183SEric Dumazet #define IP_INC_STATS_BH(net, field) SNMP_INC_STATS64_BH((net)->mib.ip_statistics, field) 1934ce3c183SEric Dumazet #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val) 1944ce3c183SEric Dumazet #define IP_ADD_STATS_BH(net, field, val) SNMP_ADD_STATS64_BH((net)->mib.ip_statistics, field, val) 1954ce3c183SEric Dumazet #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val) 1964ce3c183SEric Dumazet #define IP_UPD_PO_STATS_BH(net, field, val) SNMP_UPD_PO_STATS64_BH((net)->mib.ip_statistics, field, val) 19761a7e260SPavel Emelyanov #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field) 19861a7e260SPavel Emelyanov #define NET_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.net_statistics, field) 19961a7e260SPavel Emelyanov #define NET_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)->mib.net_statistics, field) 200f7324acdSDavid S. Miller #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd) 20161a7e260SPavel Emelyanov #define NET_ADD_STATS_BH(net, field, adnd) SNMP_ADD_STATS_BH((net)->mib.net_statistics, field, adnd) 20261a7e260SPavel Emelyanov #define NET_ADD_STATS_USER(net, field, adnd) SNMP_ADD_STATS_USER((net)->mib.net_statistics, field, adnd) 2031da177e4SLinus Torvalds 204698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt); 2054ce3c183SEric Dumazet #if BITS_PER_LONG==32 206698365faSWANG Cong u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off); 2074ce3c183SEric Dumazet #else 208698365faSWANG Cong static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off) 2094ce3c183SEric Dumazet { 2104ce3c183SEric Dumazet return snmp_fold_field(mib, offt); 2114ce3c183SEric Dumazet } 2124ce3c183SEric Dumazet #endif 21333490170SYOSHIFUJI Hideaki 2140bbf87d8SEric W. Biederman void inet_get_local_port_range(struct net *net, int *low, int *high); 215227b60f5SStephen Hemminger 216fcd77db0SDavid S. Miller #ifdef CONFIG_SYSCTL 217122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port) 218e3826f1eSAmerigo Wang { 219122ff243SWANG Cong if (!net->ipv4.sysctl_local_reserved_ports) 220122ff243SWANG Cong return 0; 221122ff243SWANG Cong return test_bit(port, net->ipv4.sysctl_local_reserved_ports); 222e3826f1eSAmerigo Wang } 22320e61da7SWANG Cong 22420e61da7SWANG Cong static inline bool sysctl_dev_name_is_allowed(const char *name) 22520e61da7SWANG Cong { 22620e61da7SWANG Cong return strcmp(name, "default") != 0 && strcmp(name, "all") != 0; 22720e61da7SWANG Cong } 22820e61da7SWANG Cong 229122ff243SWANG Cong #else 230122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port) 231122ff243SWANG Cong { 232122ff243SWANG Cong return 0; 233122ff243SWANG Cong } 234122ff243SWANG Cong #endif 235e3826f1eSAmerigo Wang 23620380731SArnaldo Carvalho de Melo /* From inetpeer.c */ 23720380731SArnaldo Carvalho de Melo extern int inet_peer_threshold; 23820380731SArnaldo Carvalho de Melo extern int inet_peer_minttl; 23920380731SArnaldo Carvalho de Melo extern int inet_peer_maxttl; 24020380731SArnaldo Carvalho de Melo 2416648bd7eSAlexander Duyck /* From ip_input.c */ 2426648bd7eSAlexander Duyck extern int sysctl_ip_early_demux; 2436648bd7eSAlexander Duyck 24420380731SArnaldo Carvalho de Melo /* From ip_output.c */ 24520380731SArnaldo Carvalho de Melo extern int sysctl_ip_dynaddr; 24620380731SArnaldo Carvalho de Melo 2475c3a0fd7SJoe Perches void ipfrag_init(void); 24820380731SArnaldo Carvalho de Melo 2495c3a0fd7SJoe Perches void ip_static_sysctl_init(void); 250bd7b1533SAl Viro 251e110861fSLorenzo Colitti #define IP4_REPLY_MARK(net, mark) \ 252e110861fSLorenzo Colitti ((net)->ipv4.sysctl_fwmark_reflect ? (mark) : 0) 253e110861fSLorenzo Colitti 254d18cd551SDavid S. Miller static inline bool ip_is_fragment(const struct iphdr *iph) 255d18cd551SDavid S. Miller { 256d18cd551SDavid S. Miller return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0; 257d18cd551SDavid S. Miller } 258d18cd551SDavid S. Miller 2591da177e4SLinus Torvalds #ifdef CONFIG_INET 26014c85021SArnaldo Carvalho de Melo #include <net/dst.h> 26114c85021SArnaldo Carvalho de Melo 2621da177e4SLinus Torvalds /* The function in 2.2 was invalid, producing wrong result for 2631da177e4SLinus Torvalds * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */ 2641da177e4SLinus Torvalds static inline 2651da177e4SLinus Torvalds int ip_decrease_ttl(struct iphdr *iph) 2661da177e4SLinus Torvalds { 2675c78f275SAl Viro u32 check = (__force u32)iph->check; 2685c78f275SAl Viro check += (__force u32)htons(0x0100); 2695c78f275SAl Viro iph->check = (__force __sum16)(check + (check>=0xFFFF)); 2701da177e4SLinus Torvalds return --iph->ttl; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds static inline 2741da177e4SLinus Torvalds int ip_dont_fragment(struct sock *sk, struct dst_entry *dst) 2751da177e4SLinus Torvalds { 276a02cec21SEric Dumazet return inet_sk(sk)->pmtudisc == IP_PMTUDISC_DO || 2771da177e4SLinus Torvalds (inet_sk(sk)->pmtudisc == IP_PMTUDISC_WANT && 278a02cec21SEric Dumazet !(dst_metric_locked(dst, RTAX_MTU))); 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 281f87c10a8SHannes Frederic Sowa static inline bool ip_sk_accept_pmtu(const struct sock *sk) 282f87c10a8SHannes Frederic Sowa { 2831b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE && 2841b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT; 285f87c10a8SHannes Frederic Sowa } 286f87c10a8SHannes Frederic Sowa 287f87c10a8SHannes Frederic Sowa static inline bool ip_sk_use_pmtu(const struct sock *sk) 288f87c10a8SHannes Frederic Sowa { 289f87c10a8SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE; 290f87c10a8SHannes Frederic Sowa } 291f87c10a8SHannes Frederic Sowa 29260ff7467SWANG Cong static inline bool ip_sk_ignore_df(const struct sock *sk) 2931b346576SHannes Frederic Sowa { 2941b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO || 2951b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT; 2961b346576SHannes Frederic Sowa } 2971b346576SHannes Frederic Sowa 298f87c10a8SHannes Frederic Sowa static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst, 299f87c10a8SHannes Frederic Sowa bool forwarding) 300f87c10a8SHannes Frederic Sowa { 301f87c10a8SHannes Frederic Sowa struct net *net = dev_net(dst->dev); 302f87c10a8SHannes Frederic Sowa 303f87c10a8SHannes Frederic Sowa if (net->ipv4.sysctl_ip_fwd_use_pmtu || 304f87c10a8SHannes Frederic Sowa dst_metric_locked(dst, RTAX_MTU) || 305f87c10a8SHannes Frederic Sowa !forwarding) 306f87c10a8SHannes Frederic Sowa return dst_mtu(dst); 307f87c10a8SHannes Frederic Sowa 308f87c10a8SHannes Frederic Sowa return min(dst->dev->mtu, IP_MAX_MTU); 309f87c10a8SHannes Frederic Sowa } 310f87c10a8SHannes Frederic Sowa 311f87c10a8SHannes Frederic Sowa static inline unsigned int ip_skb_dst_mtu(const struct sk_buff *skb) 312f87c10a8SHannes Frederic Sowa { 313f87c10a8SHannes Frederic Sowa if (!skb->sk || ip_sk_use_pmtu(skb->sk)) { 314f87c10a8SHannes Frederic Sowa bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED; 315f87c10a8SHannes Frederic Sowa return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding); 316f87c10a8SHannes Frederic Sowa } else { 317f87c10a8SHannes Frederic Sowa return min(skb_dst(skb)->dev->mtu, IP_MAX_MTU); 318f87c10a8SHannes Frederic Sowa } 319f87c10a8SHannes Frederic Sowa } 320f87c10a8SHannes Frederic Sowa 32104ca6973SEric Dumazet u32 ip_idents_reserve(u32 hash, int segs); 322b6a7719aSHannes Frederic Sowa void __ip_select_ident(struct net *net, struct iphdr *iph, int segs); 32373f156a6SEric Dumazet 324b6a7719aSHannes Frederic Sowa static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb, 325b6a7719aSHannes Frederic Sowa struct sock *sk, int segs) 3261da177e4SLinus Torvalds { 327703133deSAnsis Atteka struct iphdr *iph = ip_hdr(skb); 328703133deSAnsis Atteka 32960ff7467SWANG Cong if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) { 3301da177e4SLinus Torvalds /* This is only to work around buggy Windows95/2000 3311da177e4SLinus Torvalds * VJ compression implementations. If the ID field 3321da177e4SLinus Torvalds * does not change, they drop every other packet in 3331da177e4SLinus Torvalds * a TCP stream using header compression. 3341da177e4SLinus Torvalds */ 335c720c7e8SEric Dumazet if (sk && inet_sk(sk)->inet_daddr) { 336c720c7e8SEric Dumazet iph->id = htons(inet_sk(sk)->inet_id); 33773f156a6SEric Dumazet inet_sk(sk)->inet_id += segs; 33873f156a6SEric Dumazet } else { 3391da177e4SLinus Torvalds iph->id = 0; 34073f156a6SEric Dumazet } 34173f156a6SEric Dumazet } else { 342b6a7719aSHannes Frederic Sowa __ip_select_ident(net, iph, segs); 34373f156a6SEric Dumazet } 34473f156a6SEric Dumazet } 34573f156a6SEric Dumazet 346b6a7719aSHannes Frederic Sowa static inline void ip_select_ident(struct net *net, struct sk_buff *skb, 347b6a7719aSHannes Frederic Sowa struct sock *sk) 34873f156a6SEric Dumazet { 349b6a7719aSHannes Frederic Sowa ip_select_ident_segs(net, skb, sk, 1); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 352ed70fcfcSTom Herbert static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto) 353ed70fcfcSTom Herbert { 354ed70fcfcSTom Herbert return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, 355ed70fcfcSTom Herbert skb->len, proto, 0); 356ed70fcfcSTom Herbert } 357ed70fcfcSTom Herbert 358b73c3d0eSTom Herbert static inline void inet_set_txhash(struct sock *sk) 359b73c3d0eSTom Herbert { 360b73c3d0eSTom Herbert struct inet_sock *inet = inet_sk(sk); 361b73c3d0eSTom Herbert struct flow_keys keys; 362b73c3d0eSTom Herbert 363b73c3d0eSTom Herbert keys.src = inet->inet_saddr; 364b73c3d0eSTom Herbert keys.dst = inet->inet_daddr; 365b73c3d0eSTom Herbert keys.port16[0] = inet->inet_sport; 366b73c3d0eSTom Herbert keys.port16[1] = inet->inet_dport; 367b73c3d0eSTom Herbert 368b73c3d0eSTom Herbert sk->sk_txhash = flow_hash_from_keys(&keys); 369b73c3d0eSTom Herbert } 370b73c3d0eSTom Herbert 3711933a785STom Herbert static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto) 3721933a785STom Herbert { 3731933a785STom Herbert const struct iphdr *iph = skb_gro_network_header(skb); 3741933a785STom Herbert 3751933a785STom Herbert return csum_tcpudp_nofold(iph->saddr, iph->daddr, 3761933a785STom Herbert skb_gro_len(skb), proto, 0); 3771933a785STom Herbert } 3781933a785STom Herbert 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type ethernet. 3811da177e4SLinus Torvalds */ 3821da177e4SLinus Torvalds 383714e85beSAl Viro static inline void ip_eth_mc_map(__be32 naddr, char *buf) 3841da177e4SLinus Torvalds { 385714e85beSAl Viro __u32 addr=ntohl(naddr); 3861da177e4SLinus Torvalds buf[0]=0x01; 3871da177e4SLinus Torvalds buf[1]=0x00; 3881da177e4SLinus Torvalds buf[2]=0x5e; 3891da177e4SLinus Torvalds buf[5]=addr&0xFF; 3901da177e4SLinus Torvalds addr>>=8; 3911da177e4SLinus Torvalds buf[4]=addr&0xFF; 3921da177e4SLinus Torvalds addr>>=8; 3931da177e4SLinus Torvalds buf[3]=addr&0x7F; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 3971da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand. 3981da177e4SLinus Torvalds * Leave P_Key as 0 to be filled in by driver. 3991da177e4SLinus Torvalds */ 4001da177e4SLinus Torvalds 401a9e527e3SRolf Manderscheid static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 4021da177e4SLinus Torvalds { 403714e85beSAl Viro __u32 addr; 404a9e527e3SRolf Manderscheid unsigned char scope = broadcast[5] & 0xF; 405a9e527e3SRolf Manderscheid 4061da177e4SLinus Torvalds buf[0] = 0; /* Reserved */ 4071da177e4SLinus Torvalds buf[1] = 0xff; /* Multicast QPN */ 4081da177e4SLinus Torvalds buf[2] = 0xff; 4091da177e4SLinus Torvalds buf[3] = 0xff; 410714e85beSAl Viro addr = ntohl(naddr); 4111da177e4SLinus Torvalds buf[4] = 0xff; 412a9e527e3SRolf Manderscheid buf[5] = 0x10 | scope; /* scope from broadcast address */ 4131da177e4SLinus Torvalds buf[6] = 0x40; /* IPv4 signature */ 4141da177e4SLinus Torvalds buf[7] = 0x1b; 415a9e527e3SRolf Manderscheid buf[8] = broadcast[8]; /* P_Key */ 416a9e527e3SRolf Manderscheid buf[9] = broadcast[9]; 4171da177e4SLinus Torvalds buf[10] = 0; 4181da177e4SLinus Torvalds buf[11] = 0; 4191da177e4SLinus Torvalds buf[12] = 0; 4201da177e4SLinus Torvalds buf[13] = 0; 4211da177e4SLinus Torvalds buf[14] = 0; 4221da177e4SLinus Torvalds buf[15] = 0; 4231da177e4SLinus Torvalds buf[19] = addr & 0xff; 4241da177e4SLinus Torvalds addr >>= 8; 4251da177e4SLinus Torvalds buf[18] = addr & 0xff; 4261da177e4SLinus Torvalds addr >>= 8; 4271da177e4SLinus Torvalds buf[17] = addr & 0xff; 4281da177e4SLinus Torvalds addr >>= 8; 4291da177e4SLinus Torvalds buf[16] = addr & 0x0f; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43293ca3bb5STimo Teräs static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 43393ca3bb5STimo Teräs { 43493ca3bb5STimo Teräs if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) 43593ca3bb5STimo Teräs memcpy(buf, broadcast, 4); 43693ca3bb5STimo Teräs else 43793ca3bb5STimo Teräs memcpy(buf, &naddr, sizeof(naddr)); 43893ca3bb5STimo Teräs } 43993ca3bb5STimo Teräs 440dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4411da177e4SLinus Torvalds #include <linux/ipv6.h> 4421da177e4SLinus Torvalds #endif 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds static __inline__ void inet_reset_saddr(struct sock *sk) 4451da177e4SLinus Torvalds { 446c720c7e8SEric Dumazet inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0; 447dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4481da177e4SLinus Torvalds if (sk->sk_family == PF_INET6) { 4491da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds memset(&np->saddr, 0, sizeof(np->saddr)); 452efe4208fSEric Dumazet memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds #endif 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds #endif 4581da177e4SLinus Torvalds 4595c3a0fd7SJoe Perches bool ip_call_ra_chain(struct sk_buff *skb); 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* 462b798232fSRami Rosen * Functions provided by ip_fragment.c 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 465fd2c3ef7SEric Dumazet enum ip_defrag_users { 4661da177e4SLinus Torvalds IP_DEFRAG_LOCAL_DELIVER, 4671da177e4SLinus Torvalds IP_DEFRAG_CALL_RA_CHAIN, 4681da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_IN, 4694be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX, 4701da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_OUT, 4714be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 4728fa9ff68SPatrick McHardy IP_DEFRAG_CONNTRACK_BRIDGE_IN, 4734be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 4741da177e4SLinus Torvalds IP_DEFRAG_VS_IN, 4751da177e4SLinus Torvalds IP_DEFRAG_VS_OUT, 476595fc71bSDavid S. Miller IP_DEFRAG_VS_FWD, 477595fc71bSDavid S. Miller IP_DEFRAG_AF_PACKET, 478bc416d97SEric Dumazet IP_DEFRAG_MACVLAN, 4791da177e4SLinus Torvalds }; 4801da177e4SLinus Torvalds 481776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user); 482bc416d97SEric Dumazet #ifdef CONFIG_INET 483bc416d97SEric Dumazet struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user); 484bc416d97SEric Dumazet #else 485bc416d97SEric Dumazet static inline struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user) 486bc416d97SEric Dumazet { 487bc416d97SEric Dumazet return skb; 488bc416d97SEric Dumazet } 489bc416d97SEric Dumazet #endif 4906ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Functions provided by ip_forward.c 4941da177e4SLinus Torvalds */ 4951da177e4SLinus Torvalds 4965c3a0fd7SJoe Perches int ip_forward(struct sk_buff *skb); 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* 4991da177e4SLinus Torvalds * Functions provided by ip_options.c 5001da177e4SLinus Torvalds */ 5011da177e4SLinus Torvalds 5025c3a0fd7SJoe Perches void ip_options_build(struct sk_buff *skb, struct ip_options *opt, 503f6d8bd05SEric Dumazet __be32 daddr, struct rtable *rt, int is_frag); 50424a2d43dSEric Dumazet 50524a2d43dSEric Dumazet int __ip_options_echo(struct ip_options *dopt, struct sk_buff *skb, 50624a2d43dSEric Dumazet const struct ip_options *sopt); 50724a2d43dSEric Dumazet static inline int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb) 50824a2d43dSEric Dumazet { 50924a2d43dSEric Dumazet return __ip_options_echo(dopt, skb, &IPCB(skb)->opt); 51024a2d43dSEric Dumazet } 51124a2d43dSEric Dumazet 5125c3a0fd7SJoe Perches void ip_options_fragment(struct sk_buff *skb); 5135c3a0fd7SJoe Perches int ip_options_compile(struct net *net, struct ip_options *opt, 5145c3a0fd7SJoe Perches struct sk_buff *skb); 5155c3a0fd7SJoe Perches int ip_options_get(struct net *net, struct ip_options_rcu **optp, 5164c6ea29dSArnaldo Carvalho de Melo unsigned char *data, int optlen); 5175c3a0fd7SJoe Perches int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp, 5184c6ea29dSArnaldo Carvalho de Melo unsigned char __user *data, int optlen); 5195c3a0fd7SJoe Perches void ip_options_undo(struct ip_options *opt); 5205c3a0fd7SJoe Perches void ip_forward_options(struct sk_buff *skb); 5215c3a0fd7SJoe Perches int ip_options_rcv_srr(struct sk_buff *skb); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* 5241da177e4SLinus Torvalds * Functions provided by ip_sockglue.c 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds 527fbf8866dSShawn Bohrer void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb); 5285961de9fSTom Herbert void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb, int offset); 529c8e6ad08SHannes Frederic Sowa int ip_cmsg_send(struct net *net, struct msghdr *msg, 530c8e6ad08SHannes Frederic Sowa struct ipcm_cookie *ipc, bool allow_ipv6); 5315c3a0fd7SJoe Perches int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval, 5325c3a0fd7SJoe Perches unsigned int optlen); 5335c3a0fd7SJoe Perches int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, 5345c3a0fd7SJoe Perches int __user *optlen); 5355c3a0fd7SJoe Perches int compat_ip_setsockopt(struct sock *sk, int level, int optname, 5365c3a0fd7SJoe Perches char __user *optval, unsigned int optlen); 5375c3a0fd7SJoe Perches int compat_ip_getsockopt(struct sock *sk, int level, int optname, 5385c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 5395c3a0fd7SJoe Perches int ip_ra_control(struct sock *sk, unsigned char on, 5405c3a0fd7SJoe Perches void (*destructor)(struct sock *)); 5411da177e4SLinus Torvalds 54285fbaa75SHannes Frederic Sowa int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len); 5435c3a0fd7SJoe Perches void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 5445c3a0fd7SJoe Perches u32 info, u8 *payload); 5455c3a0fd7SJoe Perches void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport, 5461da177e4SLinus Torvalds u32 info); 5471da177e4SLinus Torvalds 5485961de9fSTom Herbert static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) 5495961de9fSTom Herbert { 5505961de9fSTom Herbert ip_cmsg_recv_offset(msg, skb, 0); 5515961de9fSTom Herbert } 5525961de9fSTom Herbert 5534cdf507dSEric Dumazet bool icmp_global_allow(void); 5544cdf507dSEric Dumazet extern int sysctl_icmp_msgs_per_sec; 5554cdf507dSEric Dumazet extern int sysctl_icmp_msgs_burst; 5564cdf507dSEric Dumazet 55720380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 5585c3a0fd7SJoe Perches int ip_misc_proc_init(void); 55920380731SArnaldo Carvalho de Melo #endif 56020380731SArnaldo Carvalho de Melo 5611da177e4SLinus Torvalds #endif /* _IP_H */ 562