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> 34b73c3d0eSTom Herbert #include <net/flow_keys.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 428cdfab8aSPatrick McHardy #define IPSKB_FORWARDED 1 438cdfab8aSPatrick McHardy #define IPSKB_XFRM_TUNNEL_SIZE 2 443e3850e9SPatrick McHardy #define IPSKB_XFRM_TRANSFORMED 4 453e3850e9SPatrick McHardy #define IPSKB_FRAG_COMPLETE 8 4648d5cad8SPatrick McHardy #define IPSKB_REROUTED 16 475f2d04f1SPatrick McHardy 485f2d04f1SPatrick McHardy u16 frag_max_size; 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 51c9bdd4b5SArnaldo Carvalho de Melo static inline unsigned int ip_hdrlen(const struct sk_buff *skb) 52c9bdd4b5SArnaldo Carvalho de Melo { 53eddc9ec5SArnaldo Carvalho de Melo return ip_hdr(skb)->ihl * 4; 54c9bdd4b5SArnaldo Carvalho de Melo } 55c9bdd4b5SArnaldo Carvalho de Melo 56fd2c3ef7SEric Dumazet struct ipcm_cookie { 57c1d18f9fSAl Viro __be32 addr; 581da177e4SLinus Torvalds int oif; 59f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 602244d07bSOliver Hartkopp __u8 tx_flags; 61f02db315SFrancesco Fusco __u8 ttl; 62f02db315SFrancesco Fusco __s16 tos; 63f02db315SFrancesco Fusco char priority; 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb)) 674b261c75SHannes Frederic Sowa #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb)) 681da177e4SLinus Torvalds 69fd2c3ef7SEric Dumazet struct ip_ra_chain { 7043a951e9SEric Dumazet struct ip_ra_chain __rcu *next; 711da177e4SLinus Torvalds struct sock *sk; 72592fcb9dSEric Dumazet union { 731da177e4SLinus Torvalds void (*destructor)(struct sock *); 74592fcb9dSEric Dumazet struct sock *saved_sk; 75592fcb9dSEric Dumazet }; 7666018506SEric Dumazet struct rcu_head rcu; 771da177e4SLinus Torvalds }; 781da177e4SLinus Torvalds 7943a951e9SEric Dumazet extern struct ip_ra_chain __rcu *ip_ra_chain; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* IP flags. */ 821da177e4SLinus Torvalds #define IP_CE 0x8000 /* Flag: "Congestion" */ 831da177e4SLinus Torvalds #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ 841da177e4SLinus Torvalds #define IP_MF 0x2000 /* Flag: "More Fragments" */ 851da177e4SLinus Torvalds #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */ 881da177e4SLinus Torvalds 8914c85021SArnaldo Carvalho de Melo struct msghdr; 9014c85021SArnaldo Carvalho de Melo struct net_device; 9114c85021SArnaldo Carvalho de Melo struct packet_type; 9214c85021SArnaldo Carvalho de Melo struct rtable; 9314c85021SArnaldo Carvalho de Melo struct sockaddr; 9414c85021SArnaldo Carvalho de Melo 9572c1d3bdSWANG Cong int igmp_mc_init(void); 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * Functions provided by ip.c 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds 1015c3a0fd7SJoe Perches int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, 10213d8eaa0SAl Viro __be32 saddr, __be32 daddr, 103f6d8bd05SEric Dumazet struct ip_options_rcu *opt); 1045c3a0fd7SJoe Perches int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, 1055c3a0fd7SJoe Perches struct net_device *orig_dev); 1065c3a0fd7SJoe Perches int ip_local_deliver(struct sk_buff *skb); 1075c3a0fd7SJoe Perches int ip_mr_input(struct sk_buff *skb); 108aad88724SEric Dumazet int ip_output(struct sock *sk, struct sk_buff *skb); 109aad88724SEric Dumazet int ip_mc_output(struct sock *sk, struct sk_buff *skb); 1105c3a0fd7SJoe Perches int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)); 1115c3a0fd7SJoe Perches int ip_do_nat(struct sk_buff *skb); 1125c3a0fd7SJoe Perches void ip_send_check(struct iphdr *ip); 1135c3a0fd7SJoe Perches int __ip_local_out(struct sk_buff *skb); 114aad88724SEric Dumazet int ip_local_out_sk(struct sock *sk, struct sk_buff *skb); 115aad88724SEric Dumazet static inline int ip_local_out(struct sk_buff *skb) 116aad88724SEric Dumazet { 117aad88724SEric Dumazet return ip_local_out_sk(skb->sk, skb); 118aad88724SEric Dumazet } 119aad88724SEric Dumazet 120b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl); 1215c3a0fd7SJoe Perches void ip_init(void); 1225c3a0fd7SJoe Perches int ip_append_data(struct sock *sk, struct flowi4 *fl4, 1231da177e4SLinus Torvalds int getfrag(void *from, char *to, int offset, int len, 1241da177e4SLinus Torvalds int odd, struct sk_buff *skb), 1251da177e4SLinus Torvalds void *from, int len, int protolen, 1261da177e4SLinus Torvalds struct ipcm_cookie *ipc, 1272e77d89bSEric Dumazet struct rtable **rt, 1281da177e4SLinus Torvalds unsigned int flags); 1295c3a0fd7SJoe Perches int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, 1305c3a0fd7SJoe Perches struct sk_buff *skb); 1315c3a0fd7SJoe Perches ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 1321da177e4SLinus Torvalds int offset, size_t size, int flags); 1335c3a0fd7SJoe Perches struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1341c32c5adSHerbert Xu struct sk_buff_head *queue, 1351c32c5adSHerbert Xu struct inet_cork *cork); 1365c3a0fd7SJoe Perches int ip_send_skb(struct net *net, struct sk_buff *skb); 1375c3a0fd7SJoe Perches int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4); 1385c3a0fd7SJoe Perches void ip_flush_pending_frames(struct sock *sk); 1395c3a0fd7SJoe Perches struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1405c3a0fd7SJoe Perches int getfrag(void *from, char *to, int offset, 1415c3a0fd7SJoe Perches int len, int odd, struct sk_buff *skb), 1421c32c5adSHerbert Xu void *from, int length, int transhdrlen, 1435c3a0fd7SJoe Perches struct ipcm_cookie *ipc, struct rtable **rtp, 1441c32c5adSHerbert Xu unsigned int flags); 1451c32c5adSHerbert Xu 14677968b78SDavid S. Miller static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4) 1471c32c5adSHerbert Xu { 14877968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 1491c32c5adSHerbert Xu } 1501da177e4SLinus Torvalds 151aa661581SFrancesco Fusco static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet) 152aa661581SFrancesco Fusco { 153aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos); 154aa661581SFrancesco Fusco } 155aa661581SFrancesco Fusco 156aa661581SFrancesco Fusco static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk) 157aa661581SFrancesco Fusco { 158aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk); 159aa661581SFrancesco Fusco } 160aa661581SFrancesco Fusco 1611da177e4SLinus Torvalds /* datagram.c */ 1625c3a0fd7SJoe Perches int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 1631da177e4SLinus Torvalds 1645c3a0fd7SJoe Perches void ip4_datagram_release_cb(struct sock *sk); 1658141ed9fSSteffen Klassert 1661da177e4SLinus Torvalds struct ip_reply_arg { 1671da177e4SLinus Torvalds struct kvec iov[1]; 16888ef4a5aSKOVACS Krisztian int flags; 169d6f5493cSAl Viro __wsum csum; 1701da177e4SLinus Torvalds int csumoffset; /* u16 offset of csum in iov[0].iov_base */ 1711da177e4SLinus Torvalds /* -1 if not needed */ 172f0e48dbfSPatrick McHardy int bound_dev_if; 17366b13d99SEric Dumazet u8 tos; 1741da177e4SLinus Torvalds }; 1751da177e4SLinus Torvalds 17688ef4a5aSKOVACS Krisztian #define IP_REPLY_ARG_NOSRCCHECK 1 17788ef4a5aSKOVACS Krisztian 17886b08d86SKOVACS Krisztian static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg) 17986b08d86SKOVACS Krisztian { 18086b08d86SKOVACS Krisztian return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0; 18186b08d86SKOVACS Krisztian } 18286b08d86SKOVACS Krisztian 183be9f4a44SEric Dumazet void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr, 18470e73416SDavid S. Miller __be32 saddr, const struct ip_reply_arg *arg, 18570e73416SDavid S. Miller unsigned int len); 1861da177e4SLinus Torvalds 1874ce3c183SEric Dumazet #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field) 1884ce3c183SEric Dumazet #define IP_INC_STATS_BH(net, field) SNMP_INC_STATS64_BH((net)->mib.ip_statistics, field) 1894ce3c183SEric Dumazet #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val) 1904ce3c183SEric Dumazet #define IP_ADD_STATS_BH(net, field, val) SNMP_ADD_STATS64_BH((net)->mib.ip_statistics, field, val) 1914ce3c183SEric Dumazet #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val) 1924ce3c183SEric Dumazet #define IP_UPD_PO_STATS_BH(net, field, val) SNMP_UPD_PO_STATS64_BH((net)->mib.ip_statistics, field, val) 19361a7e260SPavel Emelyanov #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field) 19461a7e260SPavel Emelyanov #define NET_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.net_statistics, field) 19561a7e260SPavel Emelyanov #define NET_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)->mib.net_statistics, field) 196f7324acdSDavid S. Miller #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd) 19761a7e260SPavel Emelyanov #define NET_ADD_STATS_BH(net, field, adnd) SNMP_ADD_STATS_BH((net)->mib.net_statistics, field, adnd) 19861a7e260SPavel Emelyanov #define NET_ADD_STATS_USER(net, field, adnd) SNMP_ADD_STATS_USER((net)->mib.net_statistics, field, adnd) 1991da177e4SLinus Torvalds 200698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt); 2014ce3c183SEric Dumazet #if BITS_PER_LONG==32 202698365faSWANG Cong u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off); 2034ce3c183SEric Dumazet #else 204698365faSWANG Cong static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off) 2054ce3c183SEric Dumazet { 2064ce3c183SEric Dumazet return snmp_fold_field(mib, offt); 2074ce3c183SEric Dumazet } 2084ce3c183SEric Dumazet #endif 20933490170SYOSHIFUJI Hideaki 2100bbf87d8SEric W. Biederman void inet_get_local_port_range(struct net *net, int *low, int *high); 211227b60f5SStephen Hemminger 212fcd77db0SDavid S. Miller #ifdef CONFIG_SYSCTL 213122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port) 214e3826f1eSAmerigo Wang { 215122ff243SWANG Cong if (!net->ipv4.sysctl_local_reserved_ports) 216122ff243SWANG Cong return 0; 217122ff243SWANG Cong return test_bit(port, net->ipv4.sysctl_local_reserved_ports); 218e3826f1eSAmerigo Wang } 21920e61da7SWANG Cong 22020e61da7SWANG Cong static inline bool sysctl_dev_name_is_allowed(const char *name) 22120e61da7SWANG Cong { 22220e61da7SWANG Cong return strcmp(name, "default") != 0 && strcmp(name, "all") != 0; 22320e61da7SWANG Cong } 22420e61da7SWANG Cong 225122ff243SWANG Cong #else 226122ff243SWANG Cong static inline int inet_is_local_reserved_port(struct net *net, int port) 227122ff243SWANG Cong { 228122ff243SWANG Cong return 0; 229122ff243SWANG Cong } 230122ff243SWANG Cong #endif 231e3826f1eSAmerigo Wang 232cdac4e07SNeil Horman extern int sysctl_ip_nonlocal_bind; 2331da177e4SLinus Torvalds 23420380731SArnaldo Carvalho de Melo /* From inetpeer.c */ 23520380731SArnaldo Carvalho de Melo extern int inet_peer_threshold; 23620380731SArnaldo Carvalho de Melo extern int inet_peer_minttl; 23720380731SArnaldo Carvalho de Melo extern int inet_peer_maxttl; 23820380731SArnaldo Carvalho de Melo 2396648bd7eSAlexander Duyck /* From ip_input.c */ 2406648bd7eSAlexander Duyck extern int sysctl_ip_early_demux; 2416648bd7eSAlexander Duyck 24220380731SArnaldo Carvalho de Melo /* From ip_output.c */ 24320380731SArnaldo Carvalho de Melo extern int sysctl_ip_dynaddr; 24420380731SArnaldo Carvalho de Melo 2455c3a0fd7SJoe Perches void ipfrag_init(void); 24620380731SArnaldo Carvalho de Melo 2475c3a0fd7SJoe Perches void ip_static_sysctl_init(void); 248bd7b1533SAl Viro 249e110861fSLorenzo Colitti #define IP4_REPLY_MARK(net, mark) \ 250e110861fSLorenzo Colitti ((net)->ipv4.sysctl_fwmark_reflect ? (mark) : 0) 251e110861fSLorenzo Colitti 252d18cd551SDavid S. Miller static inline bool ip_is_fragment(const struct iphdr *iph) 253d18cd551SDavid S. Miller { 254d18cd551SDavid S. Miller return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0; 255d18cd551SDavid S. Miller } 256d18cd551SDavid S. Miller 2571da177e4SLinus Torvalds #ifdef CONFIG_INET 25814c85021SArnaldo Carvalho de Melo #include <net/dst.h> 25914c85021SArnaldo Carvalho de Melo 2601da177e4SLinus Torvalds /* The function in 2.2 was invalid, producing wrong result for 2611da177e4SLinus Torvalds * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */ 2621da177e4SLinus Torvalds static inline 2631da177e4SLinus Torvalds int ip_decrease_ttl(struct iphdr *iph) 2641da177e4SLinus Torvalds { 2655c78f275SAl Viro u32 check = (__force u32)iph->check; 2665c78f275SAl Viro check += (__force u32)htons(0x0100); 2675c78f275SAl Viro iph->check = (__force __sum16)(check + (check>=0xFFFF)); 2681da177e4SLinus Torvalds return --iph->ttl; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static inline 2721da177e4SLinus Torvalds int ip_dont_fragment(struct sock *sk, struct dst_entry *dst) 2731da177e4SLinus Torvalds { 274a02cec21SEric Dumazet return inet_sk(sk)->pmtudisc == IP_PMTUDISC_DO || 2751da177e4SLinus Torvalds (inet_sk(sk)->pmtudisc == IP_PMTUDISC_WANT && 276a02cec21SEric Dumazet !(dst_metric_locked(dst, RTAX_MTU))); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279f87c10a8SHannes Frederic Sowa static inline bool ip_sk_accept_pmtu(const struct sock *sk) 280f87c10a8SHannes Frederic Sowa { 2811b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE && 2821b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT; 283f87c10a8SHannes Frederic Sowa } 284f87c10a8SHannes Frederic Sowa 285f87c10a8SHannes Frederic Sowa static inline bool ip_sk_use_pmtu(const struct sock *sk) 286f87c10a8SHannes Frederic Sowa { 287f87c10a8SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE; 288f87c10a8SHannes Frederic Sowa } 289f87c10a8SHannes Frederic Sowa 29060ff7467SWANG Cong static inline bool ip_sk_ignore_df(const struct sock *sk) 2911b346576SHannes Frederic Sowa { 2921b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO || 2931b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT; 2941b346576SHannes Frederic Sowa } 2951b346576SHannes Frederic Sowa 296f87c10a8SHannes Frederic Sowa static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst, 297f87c10a8SHannes Frederic Sowa bool forwarding) 298f87c10a8SHannes Frederic Sowa { 299f87c10a8SHannes Frederic Sowa struct net *net = dev_net(dst->dev); 300f87c10a8SHannes Frederic Sowa 301f87c10a8SHannes Frederic Sowa if (net->ipv4.sysctl_ip_fwd_use_pmtu || 302f87c10a8SHannes Frederic Sowa dst_metric_locked(dst, RTAX_MTU) || 303f87c10a8SHannes Frederic Sowa !forwarding) 304f87c10a8SHannes Frederic Sowa return dst_mtu(dst); 305f87c10a8SHannes Frederic Sowa 306f87c10a8SHannes Frederic Sowa return min(dst->dev->mtu, IP_MAX_MTU); 307f87c10a8SHannes Frederic Sowa } 308f87c10a8SHannes Frederic Sowa 309f87c10a8SHannes Frederic Sowa static inline unsigned int ip_skb_dst_mtu(const struct sk_buff *skb) 310f87c10a8SHannes Frederic Sowa { 311f87c10a8SHannes Frederic Sowa if (!skb->sk || ip_sk_use_pmtu(skb->sk)) { 312f87c10a8SHannes Frederic Sowa bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED; 313f87c10a8SHannes Frederic Sowa return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding); 314f87c10a8SHannes Frederic Sowa } else { 315f87c10a8SHannes Frederic Sowa return min(skb_dst(skb)->dev->mtu, IP_MAX_MTU); 316f87c10a8SHannes Frederic Sowa } 317f87c10a8SHannes Frederic Sowa } 318f87c10a8SHannes Frederic Sowa 31973f156a6SEric Dumazet #define IP_IDENTS_SZ 2048u 32073f156a6SEric Dumazet extern atomic_t *ip_idents; 3211da177e4SLinus Torvalds 32273f156a6SEric Dumazet static inline u32 ip_idents_reserve(u32 hash, int segs) 32373f156a6SEric Dumazet { 32473f156a6SEric Dumazet atomic_t *id_ptr = ip_idents + hash % IP_IDENTS_SZ; 32573f156a6SEric Dumazet 32673f156a6SEric Dumazet return atomic_add_return(segs, id_ptr) - segs; 32773f156a6SEric Dumazet } 32873f156a6SEric Dumazet 32973f156a6SEric Dumazet void __ip_select_ident(struct iphdr *iph, int segs); 33073f156a6SEric Dumazet 33173f156a6SEric Dumazet static inline void ip_select_ident_segs(struct sk_buff *skb, struct sock *sk, int segs) 3321da177e4SLinus Torvalds { 333703133deSAnsis Atteka struct iphdr *iph = ip_hdr(skb); 334703133deSAnsis Atteka 33560ff7467SWANG Cong if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) { 3361da177e4SLinus Torvalds /* This is only to work around buggy Windows95/2000 3371da177e4SLinus Torvalds * VJ compression implementations. If the ID field 3381da177e4SLinus Torvalds * does not change, they drop every other packet in 3391da177e4SLinus Torvalds * a TCP stream using header compression. 3401da177e4SLinus Torvalds */ 341c720c7e8SEric Dumazet if (sk && inet_sk(sk)->inet_daddr) { 342c720c7e8SEric Dumazet iph->id = htons(inet_sk(sk)->inet_id); 34373f156a6SEric Dumazet inet_sk(sk)->inet_id += segs; 34473f156a6SEric Dumazet } else { 3451da177e4SLinus Torvalds iph->id = 0; 34673f156a6SEric Dumazet } 34773f156a6SEric Dumazet } else { 34873f156a6SEric Dumazet __ip_select_ident(iph, segs); 34973f156a6SEric Dumazet } 35073f156a6SEric Dumazet } 35173f156a6SEric Dumazet 35273f156a6SEric Dumazet static inline void ip_select_ident(struct sk_buff *skb, struct sock *sk) 35373f156a6SEric Dumazet { 35473f156a6SEric Dumazet ip_select_ident_segs(skb, sk, 1); 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357ed70fcfcSTom Herbert static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto) 358ed70fcfcSTom Herbert { 359ed70fcfcSTom Herbert return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, 360ed70fcfcSTom Herbert skb->len, proto, 0); 361ed70fcfcSTom Herbert } 362ed70fcfcSTom Herbert 363b73c3d0eSTom Herbert static inline void inet_set_txhash(struct sock *sk) 364b73c3d0eSTom Herbert { 365b73c3d0eSTom Herbert struct inet_sock *inet = inet_sk(sk); 366b73c3d0eSTom Herbert struct flow_keys keys; 367b73c3d0eSTom Herbert 368b73c3d0eSTom Herbert keys.src = inet->inet_saddr; 369b73c3d0eSTom Herbert keys.dst = inet->inet_daddr; 370b73c3d0eSTom Herbert keys.port16[0] = inet->inet_sport; 371b73c3d0eSTom Herbert keys.port16[1] = inet->inet_dport; 372b73c3d0eSTom Herbert 373b73c3d0eSTom Herbert sk->sk_txhash = flow_hash_from_keys(&keys); 374b73c3d0eSTom Herbert } 375b73c3d0eSTom Herbert 3761da177e4SLinus Torvalds /* 3771da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type ethernet. 3781da177e4SLinus Torvalds */ 3791da177e4SLinus Torvalds 380714e85beSAl Viro static inline void ip_eth_mc_map(__be32 naddr, char *buf) 3811da177e4SLinus Torvalds { 382714e85beSAl Viro __u32 addr=ntohl(naddr); 3831da177e4SLinus Torvalds buf[0]=0x01; 3841da177e4SLinus Torvalds buf[1]=0x00; 3851da177e4SLinus Torvalds buf[2]=0x5e; 3861da177e4SLinus Torvalds buf[5]=addr&0xFF; 3871da177e4SLinus Torvalds addr>>=8; 3881da177e4SLinus Torvalds buf[4]=addr&0xFF; 3891da177e4SLinus Torvalds addr>>=8; 3901da177e4SLinus Torvalds buf[3]=addr&0x7F; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand. 3951da177e4SLinus Torvalds * Leave P_Key as 0 to be filled in by driver. 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds 398a9e527e3SRolf Manderscheid static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 3991da177e4SLinus Torvalds { 400714e85beSAl Viro __u32 addr; 401a9e527e3SRolf Manderscheid unsigned char scope = broadcast[5] & 0xF; 402a9e527e3SRolf Manderscheid 4031da177e4SLinus Torvalds buf[0] = 0; /* Reserved */ 4041da177e4SLinus Torvalds buf[1] = 0xff; /* Multicast QPN */ 4051da177e4SLinus Torvalds buf[2] = 0xff; 4061da177e4SLinus Torvalds buf[3] = 0xff; 407714e85beSAl Viro addr = ntohl(naddr); 4081da177e4SLinus Torvalds buf[4] = 0xff; 409a9e527e3SRolf Manderscheid buf[5] = 0x10 | scope; /* scope from broadcast address */ 4101da177e4SLinus Torvalds buf[6] = 0x40; /* IPv4 signature */ 4111da177e4SLinus Torvalds buf[7] = 0x1b; 412a9e527e3SRolf Manderscheid buf[8] = broadcast[8]; /* P_Key */ 413a9e527e3SRolf Manderscheid buf[9] = broadcast[9]; 4141da177e4SLinus Torvalds buf[10] = 0; 4151da177e4SLinus Torvalds buf[11] = 0; 4161da177e4SLinus Torvalds buf[12] = 0; 4171da177e4SLinus Torvalds buf[13] = 0; 4181da177e4SLinus Torvalds buf[14] = 0; 4191da177e4SLinus Torvalds buf[15] = 0; 4201da177e4SLinus Torvalds buf[19] = addr & 0xff; 4211da177e4SLinus Torvalds addr >>= 8; 4221da177e4SLinus Torvalds buf[18] = addr & 0xff; 4231da177e4SLinus Torvalds addr >>= 8; 4241da177e4SLinus Torvalds buf[17] = addr & 0xff; 4251da177e4SLinus Torvalds addr >>= 8; 4261da177e4SLinus Torvalds buf[16] = addr & 0x0f; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 42993ca3bb5STimo Teräs static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 43093ca3bb5STimo Teräs { 43193ca3bb5STimo Teräs if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) 43293ca3bb5STimo Teräs memcpy(buf, broadcast, 4); 43393ca3bb5STimo Teräs else 43493ca3bb5STimo Teräs memcpy(buf, &naddr, sizeof(naddr)); 43593ca3bb5STimo Teräs } 43693ca3bb5STimo Teräs 437dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4381da177e4SLinus Torvalds #include <linux/ipv6.h> 4391da177e4SLinus Torvalds #endif 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds static __inline__ void inet_reset_saddr(struct sock *sk) 4421da177e4SLinus Torvalds { 443c720c7e8SEric Dumazet inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0; 444dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4451da177e4SLinus Torvalds if (sk->sk_family == PF_INET6) { 4461da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds memset(&np->saddr, 0, sizeof(np->saddr)); 449efe4208fSEric Dumazet memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds #endif 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds #endif 4551da177e4SLinus Torvalds 4567ad6848cSOctavian Purdila static inline int sk_mc_loop(struct sock *sk) 4577ad6848cSOctavian Purdila { 4587ad6848cSOctavian Purdila if (!sk) 4597ad6848cSOctavian Purdila return 1; 4607ad6848cSOctavian Purdila switch (sk->sk_family) { 4617ad6848cSOctavian Purdila case AF_INET: 4627ad6848cSOctavian Purdila return inet_sk(sk)->mc_loop; 463dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4647ad6848cSOctavian Purdila case AF_INET6: 4657ad6848cSOctavian Purdila return inet6_sk(sk)->mc_loop; 4667ad6848cSOctavian Purdila #endif 4677ad6848cSOctavian Purdila } 468cd65c3c7SOctavian Purdila WARN_ON(1); 4697ad6848cSOctavian Purdila return 1; 4707ad6848cSOctavian Purdila } 4717ad6848cSOctavian Purdila 4725c3a0fd7SJoe Perches bool ip_call_ra_chain(struct sk_buff *skb); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 475b798232fSRami Rosen * Functions provided by ip_fragment.c 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds 478fd2c3ef7SEric Dumazet enum ip_defrag_users { 4791da177e4SLinus Torvalds IP_DEFRAG_LOCAL_DELIVER, 4801da177e4SLinus Torvalds IP_DEFRAG_CALL_RA_CHAIN, 4811da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_IN, 4824be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX, 4831da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_OUT, 4844be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 4858fa9ff68SPatrick McHardy IP_DEFRAG_CONNTRACK_BRIDGE_IN, 4864be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 4871da177e4SLinus Torvalds IP_DEFRAG_VS_IN, 4881da177e4SLinus Torvalds IP_DEFRAG_VS_OUT, 489595fc71bSDavid S. Miller IP_DEFRAG_VS_FWD, 490595fc71bSDavid S. Miller IP_DEFRAG_AF_PACKET, 491bc416d97SEric Dumazet IP_DEFRAG_MACVLAN, 4921da177e4SLinus Torvalds }; 4931da177e4SLinus Torvalds 494776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user); 495bc416d97SEric Dumazet #ifdef CONFIG_INET 496bc416d97SEric Dumazet struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user); 497bc416d97SEric Dumazet #else 498bc416d97SEric Dumazet static inline struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user) 499bc416d97SEric Dumazet { 500bc416d97SEric Dumazet return skb; 501bc416d97SEric Dumazet } 502bc416d97SEric Dumazet #endif 5036ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Functions provided by ip_forward.c 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds 5095c3a0fd7SJoe Perches int ip_forward(struct sk_buff *skb); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* 5121da177e4SLinus Torvalds * Functions provided by ip_options.c 5131da177e4SLinus Torvalds */ 5141da177e4SLinus Torvalds 5155c3a0fd7SJoe Perches void ip_options_build(struct sk_buff *skb, struct ip_options *opt, 516f6d8bd05SEric Dumazet __be32 daddr, struct rtable *rt, int is_frag); 5175c3a0fd7SJoe Perches int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb); 5185c3a0fd7SJoe Perches void ip_options_fragment(struct sk_buff *skb); 5195c3a0fd7SJoe Perches int ip_options_compile(struct net *net, struct ip_options *opt, 5205c3a0fd7SJoe Perches struct sk_buff *skb); 5215c3a0fd7SJoe Perches int ip_options_get(struct net *net, struct ip_options_rcu **optp, 5224c6ea29dSArnaldo Carvalho de Melo unsigned char *data, int optlen); 5235c3a0fd7SJoe Perches int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp, 5244c6ea29dSArnaldo Carvalho de Melo unsigned char __user *data, int optlen); 5255c3a0fd7SJoe Perches void ip_options_undo(struct ip_options *opt); 5265c3a0fd7SJoe Perches void ip_forward_options(struct sk_buff *skb); 5275c3a0fd7SJoe Perches int ip_options_rcv_srr(struct sk_buff *skb); 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* 5301da177e4SLinus Torvalds * Functions provided by ip_sockglue.c 5311da177e4SLinus Torvalds */ 5321da177e4SLinus Torvalds 533fbf8866dSShawn Bohrer void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb); 5345c3a0fd7SJoe Perches void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb); 535c8e6ad08SHannes Frederic Sowa int ip_cmsg_send(struct net *net, struct msghdr *msg, 536c8e6ad08SHannes Frederic Sowa struct ipcm_cookie *ipc, bool allow_ipv6); 5375c3a0fd7SJoe Perches int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval, 5385c3a0fd7SJoe Perches unsigned int optlen); 5395c3a0fd7SJoe Perches int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, 5405c3a0fd7SJoe Perches int __user *optlen); 5415c3a0fd7SJoe Perches int compat_ip_setsockopt(struct sock *sk, int level, int optname, 5425c3a0fd7SJoe Perches char __user *optval, unsigned int optlen); 5435c3a0fd7SJoe Perches int compat_ip_getsockopt(struct sock *sk, int level, int optname, 5445c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 5455c3a0fd7SJoe Perches int ip_ra_control(struct sock *sk, unsigned char on, 5465c3a0fd7SJoe Perches void (*destructor)(struct sock *)); 5471da177e4SLinus Torvalds 54885fbaa75SHannes Frederic Sowa int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len); 5495c3a0fd7SJoe Perches void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 5505c3a0fd7SJoe Perches u32 info, u8 *payload); 5515c3a0fd7SJoe Perches void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport, 5521da177e4SLinus Torvalds u32 info); 5531da177e4SLinus Torvalds 55420380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 5555c3a0fd7SJoe Perches int ip_misc_proc_init(void); 55620380731SArnaldo Carvalho de Melo #endif 55720380731SArnaldo Carvalho de Melo 5581da177e4SLinus Torvalds #endif /* _IP_H */ 559