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> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds struct sock; 361da177e4SLinus Torvalds 37fd2c3ef7SEric Dumazet struct inet_skb_parm { 381da177e4SLinus Torvalds struct ip_options opt; /* Compiled IP options */ 391da177e4SLinus Torvalds unsigned char flags; 401da177e4SLinus Torvalds 418cdfab8aSPatrick McHardy #define IPSKB_FORWARDED 1 428cdfab8aSPatrick McHardy #define IPSKB_XFRM_TUNNEL_SIZE 2 433e3850e9SPatrick McHardy #define IPSKB_XFRM_TRANSFORMED 4 443e3850e9SPatrick McHardy #define IPSKB_FRAG_COMPLETE 8 4548d5cad8SPatrick McHardy #define IPSKB_REROUTED 16 465f2d04f1SPatrick McHardy 475f2d04f1SPatrick McHardy u16 frag_max_size; 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 50c9bdd4b5SArnaldo Carvalho de Melo static inline unsigned int ip_hdrlen(const struct sk_buff *skb) 51c9bdd4b5SArnaldo Carvalho de Melo { 52eddc9ec5SArnaldo Carvalho de Melo return ip_hdr(skb)->ihl * 4; 53c9bdd4b5SArnaldo Carvalho de Melo } 54c9bdd4b5SArnaldo Carvalho de Melo 55fd2c3ef7SEric Dumazet struct ipcm_cookie { 56c1d18f9fSAl Viro __be32 addr; 571da177e4SLinus Torvalds int oif; 58f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 592244d07bSOliver Hartkopp __u8 tx_flags; 60f02db315SFrancesco Fusco __u8 ttl; 61f02db315SFrancesco Fusco __s16 tos; 62f02db315SFrancesco Fusco char priority; 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb)) 664b261c75SHannes Frederic Sowa #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb)) 671da177e4SLinus Torvalds 68fd2c3ef7SEric Dumazet struct ip_ra_chain { 6943a951e9SEric Dumazet struct ip_ra_chain __rcu *next; 701da177e4SLinus Torvalds struct sock *sk; 71592fcb9dSEric Dumazet union { 721da177e4SLinus Torvalds void (*destructor)(struct sock *); 73592fcb9dSEric Dumazet struct sock *saved_sk; 74592fcb9dSEric Dumazet }; 7566018506SEric Dumazet struct rcu_head rcu; 761da177e4SLinus Torvalds }; 771da177e4SLinus Torvalds 7843a951e9SEric Dumazet extern struct ip_ra_chain __rcu *ip_ra_chain; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* IP flags. */ 811da177e4SLinus Torvalds #define IP_CE 0x8000 /* Flag: "Congestion" */ 821da177e4SLinus Torvalds #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ 831da177e4SLinus Torvalds #define IP_MF 0x2000 /* Flag: "More Fragments" */ 841da177e4SLinus Torvalds #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */ 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */ 871da177e4SLinus Torvalds 8814c85021SArnaldo Carvalho de Melo struct msghdr; 8914c85021SArnaldo Carvalho de Melo struct net_device; 9014c85021SArnaldo Carvalho de Melo struct packet_type; 9114c85021SArnaldo Carvalho de Melo struct rtable; 9214c85021SArnaldo Carvalho de Melo struct sockaddr; 9314c85021SArnaldo Carvalho de Melo 9472c1d3bdSWANG Cong int igmp_mc_init(void); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * Functions provided by ip.c 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds 1005c3a0fd7SJoe Perches int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, 10113d8eaa0SAl Viro __be32 saddr, __be32 daddr, 102f6d8bd05SEric Dumazet struct ip_options_rcu *opt); 1035c3a0fd7SJoe Perches int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, 1045c3a0fd7SJoe Perches struct net_device *orig_dev); 1055c3a0fd7SJoe Perches int ip_local_deliver(struct sk_buff *skb); 1065c3a0fd7SJoe Perches int ip_mr_input(struct sk_buff *skb); 1075c3a0fd7SJoe Perches int ip_output(struct sk_buff *skb); 1085c3a0fd7SJoe Perches int ip_mc_output(struct sk_buff *skb); 1095c3a0fd7SJoe Perches int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)); 1105c3a0fd7SJoe Perches int ip_do_nat(struct sk_buff *skb); 1115c3a0fd7SJoe Perches void ip_send_check(struct iphdr *ip); 1125c3a0fd7SJoe Perches int __ip_local_out(struct sk_buff *skb); 1135c3a0fd7SJoe Perches int ip_local_out(struct sk_buff *skb); 1145c3a0fd7SJoe Perches int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl); 1155c3a0fd7SJoe Perches void ip_init(void); 1165c3a0fd7SJoe Perches int ip_append_data(struct sock *sk, struct flowi4 *fl4, 1171da177e4SLinus Torvalds int getfrag(void *from, char *to, int offset, int len, 1181da177e4SLinus Torvalds int odd, struct sk_buff *skb), 1191da177e4SLinus Torvalds void *from, int len, int protolen, 1201da177e4SLinus Torvalds struct ipcm_cookie *ipc, 1212e77d89bSEric Dumazet struct rtable **rt, 1221da177e4SLinus Torvalds unsigned int flags); 1235c3a0fd7SJoe Perches int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, 1245c3a0fd7SJoe Perches struct sk_buff *skb); 1255c3a0fd7SJoe Perches ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 1261da177e4SLinus Torvalds int offset, size_t size, int flags); 1275c3a0fd7SJoe Perches struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1281c32c5adSHerbert Xu struct sk_buff_head *queue, 1291c32c5adSHerbert Xu struct inet_cork *cork); 1305c3a0fd7SJoe Perches int ip_send_skb(struct net *net, struct sk_buff *skb); 1315c3a0fd7SJoe Perches int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4); 1325c3a0fd7SJoe Perches void ip_flush_pending_frames(struct sock *sk); 1335c3a0fd7SJoe Perches struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4, 1345c3a0fd7SJoe Perches int getfrag(void *from, char *to, int offset, 1355c3a0fd7SJoe Perches int len, int odd, struct sk_buff *skb), 1361c32c5adSHerbert Xu void *from, int length, int transhdrlen, 1375c3a0fd7SJoe Perches struct ipcm_cookie *ipc, struct rtable **rtp, 1381c32c5adSHerbert Xu unsigned int flags); 1391c32c5adSHerbert Xu 14077968b78SDavid S. Miller static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4) 1411c32c5adSHerbert Xu { 14277968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 1431c32c5adSHerbert Xu } 1441da177e4SLinus Torvalds 145aa661581SFrancesco Fusco static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet) 146aa661581SFrancesco Fusco { 147aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos); 148aa661581SFrancesco Fusco } 149aa661581SFrancesco Fusco 150aa661581SFrancesco Fusco static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk) 151aa661581SFrancesco Fusco { 152aa661581SFrancesco Fusco return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk); 153aa661581SFrancesco Fusco } 154aa661581SFrancesco Fusco 1551da177e4SLinus Torvalds /* datagram.c */ 1565c3a0fd7SJoe Perches int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 1571da177e4SLinus Torvalds 1585c3a0fd7SJoe Perches void ip4_datagram_release_cb(struct sock *sk); 1598141ed9fSSteffen Klassert 1601da177e4SLinus Torvalds struct ip_reply_arg { 1611da177e4SLinus Torvalds struct kvec iov[1]; 16288ef4a5aSKOVACS Krisztian int flags; 163d6f5493cSAl Viro __wsum csum; 1641da177e4SLinus Torvalds int csumoffset; /* u16 offset of csum in iov[0].iov_base */ 1651da177e4SLinus Torvalds /* -1 if not needed */ 166f0e48dbfSPatrick McHardy int bound_dev_if; 16766b13d99SEric Dumazet u8 tos; 1681da177e4SLinus Torvalds }; 1691da177e4SLinus Torvalds 17088ef4a5aSKOVACS Krisztian #define IP_REPLY_ARG_NOSRCCHECK 1 17188ef4a5aSKOVACS Krisztian 17286b08d86SKOVACS Krisztian static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg) 17386b08d86SKOVACS Krisztian { 17486b08d86SKOVACS Krisztian return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0; 17586b08d86SKOVACS Krisztian } 17686b08d86SKOVACS Krisztian 177be9f4a44SEric Dumazet void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr, 17870e73416SDavid S. Miller __be32 saddr, const struct ip_reply_arg *arg, 17970e73416SDavid S. Miller unsigned int len); 1801da177e4SLinus Torvalds 1814ce3c183SEric Dumazet #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field) 1824ce3c183SEric Dumazet #define IP_INC_STATS_BH(net, field) SNMP_INC_STATS64_BH((net)->mib.ip_statistics, field) 1834ce3c183SEric Dumazet #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val) 1844ce3c183SEric Dumazet #define IP_ADD_STATS_BH(net, field, val) SNMP_ADD_STATS64_BH((net)->mib.ip_statistics, field, val) 1854ce3c183SEric Dumazet #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val) 1864ce3c183SEric Dumazet #define IP_UPD_PO_STATS_BH(net, field, val) SNMP_UPD_PO_STATS64_BH((net)->mib.ip_statistics, field, val) 18761a7e260SPavel Emelyanov #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field) 18861a7e260SPavel Emelyanov #define NET_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.net_statistics, field) 18961a7e260SPavel Emelyanov #define NET_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)->mib.net_statistics, field) 190f7324acdSDavid S. Miller #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd) 19161a7e260SPavel Emelyanov #define NET_ADD_STATS_BH(net, field, adnd) SNMP_ADD_STATS_BH((net)->mib.net_statistics, field, adnd) 19261a7e260SPavel Emelyanov #define NET_ADD_STATS_USER(net, field, adnd) SNMP_ADD_STATS_USER((net)->mib.net_statistics, field, adnd) 1931da177e4SLinus Torvalds 1945c3a0fd7SJoe Perches unsigned long snmp_fold_field(void __percpu *mib[], int offt); 1954ce3c183SEric Dumazet #if BITS_PER_LONG==32 1965c3a0fd7SJoe Perches u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t sync_off); 1974ce3c183SEric Dumazet #else 1984ce3c183SEric Dumazet static inline u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_off) 1994ce3c183SEric Dumazet { 2004ce3c183SEric Dumazet return snmp_fold_field(mib, offt); 2014ce3c183SEric Dumazet } 2024ce3c183SEric Dumazet #endif 2035c3a0fd7SJoe Perches int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align); 2045a17a390SCong Wang 2055a17a390SCong Wang static inline void snmp_mib_free(void __percpu *ptr[SNMP_ARRAY_SZ]) 2065a17a390SCong Wang { 2075a17a390SCong Wang int i; 2085a17a390SCong Wang 2095a17a390SCong Wang BUG_ON(ptr == NULL); 2105a17a390SCong Wang for (i = 0; i < SNMP_ARRAY_SZ; i++) { 2115a17a390SCong Wang free_percpu(ptr[i]); 2125a17a390SCong Wang ptr[i] = NULL; 2135a17a390SCong Wang } 2145a17a390SCong Wang } 21533490170SYOSHIFUJI Hideaki 2160bbf87d8SEric W. Biederman void inet_get_local_port_range(struct net *net, int *low, int *high); 217227b60f5SStephen Hemminger 218e3826f1eSAmerigo Wang extern unsigned long *sysctl_local_reserved_ports; 219e3826f1eSAmerigo Wang static inline int inet_is_reserved_local_port(int port) 220e3826f1eSAmerigo Wang { 221e3826f1eSAmerigo Wang return test_bit(port, sysctl_local_reserved_ports); 222e3826f1eSAmerigo Wang } 223e3826f1eSAmerigo Wang 224cdac4e07SNeil Horman extern int sysctl_ip_nonlocal_bind; 2251da177e4SLinus Torvalds 22620380731SArnaldo Carvalho de Melo /* From inetpeer.c */ 22720380731SArnaldo Carvalho de Melo extern int inet_peer_threshold; 22820380731SArnaldo Carvalho de Melo extern int inet_peer_minttl; 22920380731SArnaldo Carvalho de Melo extern int inet_peer_maxttl; 23020380731SArnaldo Carvalho de Melo 2316648bd7eSAlexander Duyck /* From ip_input.c */ 2326648bd7eSAlexander Duyck extern int sysctl_ip_early_demux; 2336648bd7eSAlexander Duyck 23420380731SArnaldo Carvalho de Melo /* From ip_output.c */ 23520380731SArnaldo Carvalho de Melo extern int sysctl_ip_dynaddr; 23620380731SArnaldo Carvalho de Melo 2375c3a0fd7SJoe Perches void ipfrag_init(void); 23820380731SArnaldo Carvalho de Melo 2395c3a0fd7SJoe Perches void ip_static_sysctl_init(void); 240bd7b1533SAl Viro 241d18cd551SDavid S. Miller static inline bool ip_is_fragment(const struct iphdr *iph) 242d18cd551SDavid S. Miller { 243d18cd551SDavid S. Miller return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0; 244d18cd551SDavid S. Miller } 245d18cd551SDavid S. Miller 2461da177e4SLinus Torvalds #ifdef CONFIG_INET 24714c85021SArnaldo Carvalho de Melo #include <net/dst.h> 24814c85021SArnaldo Carvalho de Melo 2491da177e4SLinus Torvalds /* The function in 2.2 was invalid, producing wrong result for 2501da177e4SLinus Torvalds * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */ 2511da177e4SLinus Torvalds static inline 2521da177e4SLinus Torvalds int ip_decrease_ttl(struct iphdr *iph) 2531da177e4SLinus Torvalds { 2545c78f275SAl Viro u32 check = (__force u32)iph->check; 2555c78f275SAl Viro check += (__force u32)htons(0x0100); 2565c78f275SAl Viro iph->check = (__force __sum16)(check + (check>=0xFFFF)); 2571da177e4SLinus Torvalds return --iph->ttl; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static inline 2611da177e4SLinus Torvalds int ip_dont_fragment(struct sock *sk, struct dst_entry *dst) 2621da177e4SLinus Torvalds { 263a02cec21SEric Dumazet return inet_sk(sk)->pmtudisc == IP_PMTUDISC_DO || 2641da177e4SLinus Torvalds (inet_sk(sk)->pmtudisc == IP_PMTUDISC_WANT && 265a02cec21SEric Dumazet !(dst_metric_locked(dst, RTAX_MTU))); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 268f87c10a8SHannes Frederic Sowa static inline bool ip_sk_accept_pmtu(const struct sock *sk) 269f87c10a8SHannes Frederic Sowa { 2701b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE && 2711b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT; 272f87c10a8SHannes Frederic Sowa } 273f87c10a8SHannes Frederic Sowa 274f87c10a8SHannes Frederic Sowa static inline bool ip_sk_use_pmtu(const struct sock *sk) 275f87c10a8SHannes Frederic Sowa { 276f87c10a8SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE; 277f87c10a8SHannes Frederic Sowa } 278f87c10a8SHannes Frederic Sowa 2791b346576SHannes Frederic Sowa static inline bool ip_sk_local_df(const struct sock *sk) 2801b346576SHannes Frederic Sowa { 2811b346576SHannes Frederic Sowa return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO || 2821b346576SHannes Frederic Sowa inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT; 2831b346576SHannes Frederic Sowa } 2841b346576SHannes Frederic Sowa 285f87c10a8SHannes Frederic Sowa static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst, 286f87c10a8SHannes Frederic Sowa bool forwarding) 287f87c10a8SHannes Frederic Sowa { 288f87c10a8SHannes Frederic Sowa struct net *net = dev_net(dst->dev); 289f87c10a8SHannes Frederic Sowa 290f87c10a8SHannes Frederic Sowa if (net->ipv4.sysctl_ip_fwd_use_pmtu || 291f87c10a8SHannes Frederic Sowa dst_metric_locked(dst, RTAX_MTU) || 292f87c10a8SHannes Frederic Sowa !forwarding) 293f87c10a8SHannes Frederic Sowa return dst_mtu(dst); 294f87c10a8SHannes Frederic Sowa 295f87c10a8SHannes Frederic Sowa return min(dst->dev->mtu, IP_MAX_MTU); 296f87c10a8SHannes Frederic Sowa } 297f87c10a8SHannes Frederic Sowa 298f87c10a8SHannes Frederic Sowa static inline unsigned int ip_skb_dst_mtu(const struct sk_buff *skb) 299f87c10a8SHannes Frederic Sowa { 300f87c10a8SHannes Frederic Sowa if (!skb->sk || ip_sk_use_pmtu(skb->sk)) { 301f87c10a8SHannes Frederic Sowa bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED; 302f87c10a8SHannes Frederic Sowa return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding); 303f87c10a8SHannes Frederic Sowa } else { 304f87c10a8SHannes Frederic Sowa return min(skb_dst(skb)->dev->mtu, IP_MAX_MTU); 305f87c10a8SHannes Frederic Sowa } 306f87c10a8SHannes Frederic Sowa } 307f87c10a8SHannes Frederic Sowa 3085c3a0fd7SJoe Perches void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more); 3091da177e4SLinus Torvalds 310703133deSAnsis Atteka static inline void ip_select_ident(struct sk_buff *skb, struct dst_entry *dst, struct sock *sk) 3111da177e4SLinus Torvalds { 312703133deSAnsis Atteka struct iphdr *iph = ip_hdr(skb); 313703133deSAnsis Atteka 314703133deSAnsis Atteka if ((iph->frag_off & htons(IP_DF)) && !skb->local_df) { 3151da177e4SLinus Torvalds /* This is only to work around buggy Windows95/2000 3161da177e4SLinus Torvalds * VJ compression implementations. If the ID field 3171da177e4SLinus Torvalds * does not change, they drop every other packet in 3181da177e4SLinus Torvalds * a TCP stream using header compression. 3191da177e4SLinus Torvalds */ 320c720c7e8SEric Dumazet iph->id = (sk && inet_sk(sk)->inet_daddr) ? 321c720c7e8SEric Dumazet htons(inet_sk(sk)->inet_id++) : 0; 3221da177e4SLinus Torvalds } else 3231da177e4SLinus Torvalds __ip_select_ident(iph, dst, 0); 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 326703133deSAnsis Atteka static inline void ip_select_ident_more(struct sk_buff *skb, struct dst_entry *dst, struct sock *sk, int more) 3271da177e4SLinus Torvalds { 328703133deSAnsis Atteka struct iphdr *iph = ip_hdr(skb); 329703133deSAnsis Atteka 330703133deSAnsis Atteka if ((iph->frag_off & htons(IP_DF)) && !skb->local_df) { 331c720c7e8SEric Dumazet if (sk && inet_sk(sk)->inet_daddr) { 332c720c7e8SEric Dumazet iph->id = htons(inet_sk(sk)->inet_id); 333c720c7e8SEric Dumazet inet_sk(sk)->inet_id += 1 + more; 3341da177e4SLinus Torvalds } else 3351da177e4SLinus Torvalds iph->id = 0; 3361da177e4SLinus Torvalds } else 3371da177e4SLinus Torvalds __ip_select_ident(iph, dst, more); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type ethernet. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds 344714e85beSAl Viro static inline void ip_eth_mc_map(__be32 naddr, char *buf) 3451da177e4SLinus Torvalds { 346714e85beSAl Viro __u32 addr=ntohl(naddr); 3471da177e4SLinus Torvalds buf[0]=0x01; 3481da177e4SLinus Torvalds buf[1]=0x00; 3491da177e4SLinus Torvalds buf[2]=0x5e; 3501da177e4SLinus Torvalds buf[5]=addr&0xFF; 3511da177e4SLinus Torvalds addr>>=8; 3521da177e4SLinus Torvalds buf[4]=addr&0xFF; 3531da177e4SLinus Torvalds addr>>=8; 3541da177e4SLinus Torvalds buf[3]=addr&0x7F; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand. 3591da177e4SLinus Torvalds * Leave P_Key as 0 to be filled in by driver. 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds 362a9e527e3SRolf Manderscheid static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 3631da177e4SLinus Torvalds { 364714e85beSAl Viro __u32 addr; 365a9e527e3SRolf Manderscheid unsigned char scope = broadcast[5] & 0xF; 366a9e527e3SRolf Manderscheid 3671da177e4SLinus Torvalds buf[0] = 0; /* Reserved */ 3681da177e4SLinus Torvalds buf[1] = 0xff; /* Multicast QPN */ 3691da177e4SLinus Torvalds buf[2] = 0xff; 3701da177e4SLinus Torvalds buf[3] = 0xff; 371714e85beSAl Viro addr = ntohl(naddr); 3721da177e4SLinus Torvalds buf[4] = 0xff; 373a9e527e3SRolf Manderscheid buf[5] = 0x10 | scope; /* scope from broadcast address */ 3741da177e4SLinus Torvalds buf[6] = 0x40; /* IPv4 signature */ 3751da177e4SLinus Torvalds buf[7] = 0x1b; 376a9e527e3SRolf Manderscheid buf[8] = broadcast[8]; /* P_Key */ 377a9e527e3SRolf Manderscheid buf[9] = broadcast[9]; 3781da177e4SLinus Torvalds buf[10] = 0; 3791da177e4SLinus Torvalds buf[11] = 0; 3801da177e4SLinus Torvalds buf[12] = 0; 3811da177e4SLinus Torvalds buf[13] = 0; 3821da177e4SLinus Torvalds buf[14] = 0; 3831da177e4SLinus Torvalds buf[15] = 0; 3841da177e4SLinus Torvalds buf[19] = addr & 0xff; 3851da177e4SLinus Torvalds addr >>= 8; 3861da177e4SLinus Torvalds buf[18] = addr & 0xff; 3871da177e4SLinus Torvalds addr >>= 8; 3881da177e4SLinus Torvalds buf[17] = addr & 0xff; 3891da177e4SLinus Torvalds addr >>= 8; 3901da177e4SLinus Torvalds buf[16] = addr & 0x0f; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 39393ca3bb5STimo Teräs static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf) 39493ca3bb5STimo Teräs { 39593ca3bb5STimo Teräs if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) 39693ca3bb5STimo Teräs memcpy(buf, broadcast, 4); 39793ca3bb5STimo Teräs else 39893ca3bb5STimo Teräs memcpy(buf, &naddr, sizeof(naddr)); 39993ca3bb5STimo Teräs } 40093ca3bb5STimo Teräs 401dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4021da177e4SLinus Torvalds #include <linux/ipv6.h> 4031da177e4SLinus Torvalds #endif 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds static __inline__ void inet_reset_saddr(struct sock *sk) 4061da177e4SLinus Torvalds { 407c720c7e8SEric Dumazet inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0; 408dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4091da177e4SLinus Torvalds if (sk->sk_family == PF_INET6) { 4101da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds memset(&np->saddr, 0, sizeof(np->saddr)); 413efe4208fSEric Dumazet memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds #endif 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds #endif 4191da177e4SLinus Torvalds 4207ad6848cSOctavian Purdila static inline int sk_mc_loop(struct sock *sk) 4217ad6848cSOctavian Purdila { 4227ad6848cSOctavian Purdila if (!sk) 4237ad6848cSOctavian Purdila return 1; 4247ad6848cSOctavian Purdila switch (sk->sk_family) { 4257ad6848cSOctavian Purdila case AF_INET: 4267ad6848cSOctavian Purdila return inet_sk(sk)->mc_loop; 427dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4287ad6848cSOctavian Purdila case AF_INET6: 4297ad6848cSOctavian Purdila return inet6_sk(sk)->mc_loop; 4307ad6848cSOctavian Purdila #endif 4317ad6848cSOctavian Purdila } 432cd65c3c7SOctavian Purdila WARN_ON(1); 4337ad6848cSOctavian Purdila return 1; 4347ad6848cSOctavian Purdila } 4357ad6848cSOctavian Purdila 4365c3a0fd7SJoe Perches bool ip_call_ra_chain(struct sk_buff *skb); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* 439b798232fSRami Rosen * Functions provided by ip_fragment.c 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds 442fd2c3ef7SEric Dumazet enum ip_defrag_users { 4431da177e4SLinus Torvalds IP_DEFRAG_LOCAL_DELIVER, 4441da177e4SLinus Torvalds IP_DEFRAG_CALL_RA_CHAIN, 4451da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_IN, 4464be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX, 4471da177e4SLinus Torvalds IP_DEFRAG_CONNTRACK_OUT, 4484be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 4498fa9ff68SPatrick McHardy IP_DEFRAG_CONNTRACK_BRIDGE_IN, 4504be929beSAlexey Dobriyan __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 4511da177e4SLinus Torvalds IP_DEFRAG_VS_IN, 4521da177e4SLinus Torvalds IP_DEFRAG_VS_OUT, 453595fc71bSDavid S. Miller IP_DEFRAG_VS_FWD, 454595fc71bSDavid S. Miller IP_DEFRAG_AF_PACKET, 455bc416d97SEric Dumazet IP_DEFRAG_MACVLAN, 4561da177e4SLinus Torvalds }; 4571da177e4SLinus Torvalds 458776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user); 459bc416d97SEric Dumazet #ifdef CONFIG_INET 460bc416d97SEric Dumazet struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user); 461bc416d97SEric Dumazet #else 462bc416d97SEric Dumazet static inline struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user) 463bc416d97SEric Dumazet { 464bc416d97SEric Dumazet return skb; 465bc416d97SEric Dumazet } 466bc416d97SEric Dumazet #endif 4676ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net); 468e5a2bb84SPavel Emelyanov int ip_frag_nqueues(struct net *net); 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* 4711da177e4SLinus Torvalds * Functions provided by ip_forward.c 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds 4745c3a0fd7SJoe Perches int ip_forward(struct sk_buff *skb); 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* 4771da177e4SLinus Torvalds * Functions provided by ip_options.c 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds 4805c3a0fd7SJoe Perches void ip_options_build(struct sk_buff *skb, struct ip_options *opt, 481f6d8bd05SEric Dumazet __be32 daddr, struct rtable *rt, int is_frag); 4825c3a0fd7SJoe Perches int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb); 4835c3a0fd7SJoe Perches void ip_options_fragment(struct sk_buff *skb); 4845c3a0fd7SJoe Perches int ip_options_compile(struct net *net, struct ip_options *opt, 4855c3a0fd7SJoe Perches struct sk_buff *skb); 4865c3a0fd7SJoe Perches int ip_options_get(struct net *net, struct ip_options_rcu **optp, 4874c6ea29dSArnaldo Carvalho de Melo unsigned char *data, int optlen); 4885c3a0fd7SJoe Perches int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp, 4894c6ea29dSArnaldo Carvalho de Melo unsigned char __user *data, int optlen); 4905c3a0fd7SJoe Perches void ip_options_undo(struct ip_options *opt); 4915c3a0fd7SJoe Perches void ip_forward_options(struct sk_buff *skb); 4925c3a0fd7SJoe Perches int ip_options_rcv_srr(struct sk_buff *skb); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* 4951da177e4SLinus Torvalds * Functions provided by ip_sockglue.c 4961da177e4SLinus Torvalds */ 4971da177e4SLinus Torvalds 498fbf8866dSShawn Bohrer void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb); 4995c3a0fd7SJoe Perches void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb); 500c8e6ad08SHannes Frederic Sowa int ip_cmsg_send(struct net *net, struct msghdr *msg, 501c8e6ad08SHannes Frederic Sowa struct ipcm_cookie *ipc, bool allow_ipv6); 5025c3a0fd7SJoe Perches int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval, 5035c3a0fd7SJoe Perches unsigned int optlen); 5045c3a0fd7SJoe Perches int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, 5055c3a0fd7SJoe Perches int __user *optlen); 5065c3a0fd7SJoe Perches int compat_ip_setsockopt(struct sock *sk, int level, int optname, 5075c3a0fd7SJoe Perches char __user *optval, unsigned int optlen); 5085c3a0fd7SJoe Perches int compat_ip_getsockopt(struct sock *sk, int level, int optname, 5095c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 5105c3a0fd7SJoe Perches int ip_ra_control(struct sock *sk, unsigned char on, 5115c3a0fd7SJoe Perches void (*destructor)(struct sock *)); 5121da177e4SLinus Torvalds 51385fbaa75SHannes Frederic Sowa int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len); 5145c3a0fd7SJoe Perches void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 5155c3a0fd7SJoe Perches u32 info, u8 *payload); 5165c3a0fd7SJoe Perches void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport, 5171da177e4SLinus Torvalds u32 info); 5181da177e4SLinus Torvalds 51920380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 5205c3a0fd7SJoe Perches int ip_misc_proc_init(void); 52120380731SArnaldo Carvalho de Melo #endif 52220380731SArnaldo Carvalho de Melo 5231da177e4SLinus Torvalds #endif /* _IP_H */ 524