11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: 51da177e4SLinus Torvalds * Pedro Roque <[email protected]> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 91da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 101da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #ifndef _NET_IPV6_H 141da177e4SLinus Torvalds #define _NET_IPV6_H 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/ipv6.h> 171da177e4SLinus Torvalds #include <linux/hardirq.h> 1808dcdbf6SEric Dumazet #include <linux/jhash.h> 1920283d84SHerbert Xu #include <net/if_inet6.h> 201da177e4SLinus Torvalds #include <net/ndisc.h> 211da177e4SLinus Torvalds #include <net/flow.h> 22b73c3d0eSTom Herbert #include <net/flow_keys.h> 231da177e4SLinus Torvalds #include <net/snmp.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #define SIN6_LEN_RFC2133 24 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #define IPV6_MAXPLEN 65535 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * NextHeader field of IPv6 header 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define NEXTHDR_HOP 0 /* Hop-by-hop option header. */ 341da177e4SLinus Torvalds #define NEXTHDR_TCP 6 /* TCP segment. */ 351da177e4SLinus Torvalds #define NEXTHDR_UDP 17 /* UDP message. */ 361da177e4SLinus Torvalds #define NEXTHDR_IPV6 41 /* IPv6 in IPv6 */ 371da177e4SLinus Torvalds #define NEXTHDR_ROUTING 43 /* Routing header. */ 381da177e4SLinus Torvalds #define NEXTHDR_FRAGMENT 44 /* Fragmentation/reassembly header. */ 39c12b395aS[email protected] #define NEXTHDR_GRE 47 /* GRE header. */ 401da177e4SLinus Torvalds #define NEXTHDR_ESP 50 /* Encapsulating security payload. */ 411da177e4SLinus Torvalds #define NEXTHDR_AUTH 51 /* Authentication header. */ 421da177e4SLinus Torvalds #define NEXTHDR_ICMP 58 /* ICMP for IPv6. */ 431da177e4SLinus Torvalds #define NEXTHDR_NONE 59 /* No next header */ 441da177e4SLinus Torvalds #define NEXTHDR_DEST 60 /* Destination options header. */ 45280c571eSJoe Stringer #define NEXTHDR_SCTP 132 /* SCTP message. */ 462b741653SMasahide NAKAMURA #define NEXTHDR_MOBILITY 135 /* Mobility header. */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #define NEXTHDR_MAX 255 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #define IPV6_DEFAULT_HOPLIMIT 64 531da177e4SLinus Torvalds #define IPV6_DEFAULT_MCASTHOPS 1 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds * Addr type 571da177e4SLinus Torvalds * 581da177e4SLinus Torvalds * type - unicast | multicast 591da177e4SLinus Torvalds * scope - local | site | global 601da177e4SLinus Torvalds * v4 - compat 611da177e4SLinus Torvalds * v4mapped 621da177e4SLinus Torvalds * any 631da177e4SLinus Torvalds * loopback 641da177e4SLinus Torvalds */ 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #define IPV6_ADDR_ANY 0x0000U 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #define IPV6_ADDR_UNICAST 0x0001U 691da177e4SLinus Torvalds #define IPV6_ADDR_MULTICAST 0x0002U 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds #define IPV6_ADDR_LOOPBACK 0x0010U 721da177e4SLinus Torvalds #define IPV6_ADDR_LINKLOCAL 0x0020U 731da177e4SLinus Torvalds #define IPV6_ADDR_SITELOCAL 0x0040U 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds #define IPV6_ADDR_COMPATv4 0x0080U 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_MASK 0x00f0U 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #define IPV6_ADDR_MAPPED 0x1000U 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * Addr scopes 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds #define IPV6_ADDR_MC_SCOPE(a) \ 851da177e4SLinus Torvalds ((a)->s6_addr[1] & 0x0f) /* nonstandard */ 861da177e4SLinus Torvalds #define __IPV6_ADDR_SCOPE_INVALID -1 871da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_NODELOCAL 0x01 881da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_LINKLOCAL 0x02 891da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_SITELOCAL 0x05 901da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_ORGLOCAL 0x08 911da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_GLOBAL 0x0e 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* 945ced1339SLinus Lüssing * Addr flags 955ced1339SLinus Lüssing */ 965ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_TRANSIENT(a) \ 975ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x10) 985ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_PREFIX(a) \ 995ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x20) 1005ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a) \ 1015ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x40) 1025ced1339SLinus Lüssing 1035ced1339SLinus Lüssing /* 1041da177e4SLinus Torvalds * fragmentation header 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds struct frag_hdr { 10844473a6bSAl Viro __u8 nexthdr; 10944473a6bSAl Viro __u8 reserved; 11044473a6bSAl Viro __be16 frag_off; 11144473a6bSAl Viro __be32 identification; 1121da177e4SLinus Torvalds }; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #define IP6_MF 0x0001 1151431fb31SPaul Durrant #define IP6_OFFSET 0xFFF8 1161da177e4SLinus Torvalds 117e110861fSLorenzo Colitti #define IP6_REPLY_MARK(net, mark) \ 118e110861fSLorenzo Colitti ((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0) 119e110861fSLorenzo Colitti 1201da177e4SLinus Torvalds #include <net/sock.h> 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* sysctls */ 1231da177e4SLinus Torvalds extern int sysctl_mld_max_msf; 1243d7cc2baSPavel Emelyanov 125087fe240SDenis V. Lunev #define _DEVINC(net, statname, modifier, idev, field) \ 12614878f75SDavid L Stevens ({ \ 12714878f75SDavid L Stevens struct inet6_dev *_idev = (idev); \ 12814878f75SDavid L Stevens if (likely(_idev != NULL)) \ 12914878f75SDavid L Stevens SNMP_INC_STATS##modifier((_idev)->stats.statname, (field)); \ 1309261e537SDenis V. Lunev SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\ 13114878f75SDavid L Stevens }) 13214878f75SDavid L Stevens 133be281e55SEric Dumazet /* per device counters are atomic_long_t */ 134be281e55SEric Dumazet #define _DEVINCATOMIC(net, statname, modifier, idev, field) \ 135be281e55SEric Dumazet ({ \ 136be281e55SEric Dumazet struct inet6_dev *_idev = (idev); \ 137be281e55SEric Dumazet if (likely(_idev != NULL)) \ 138be281e55SEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \ 139be281e55SEric Dumazet SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\ 140be281e55SEric Dumazet }) 141be281e55SEric Dumazet 1422a24444fSEric Dumazet /* per device and per net counters are atomic_long_t */ 1432a24444fSEric Dumazet #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field) \ 1442a24444fSEric Dumazet ({ \ 1452a24444fSEric Dumazet struct inet6_dev *_idev = (idev); \ 1462a24444fSEric Dumazet if (likely(_idev != NULL)) \ 1472a24444fSEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \ 1482a24444fSEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\ 1492a24444fSEric Dumazet }) 1502a24444fSEric Dumazet 151087fe240SDenis V. Lunev #define _DEVADD(net, statname, modifier, idev, field, val) \ 1528e7999c4SPavel Emelyanov ({ \ 1538e7999c4SPavel Emelyanov struct inet6_dev *_idev = (idev); \ 1548e7999c4SPavel Emelyanov if (likely(_idev != NULL)) \ 1558e7999c4SPavel Emelyanov SNMP_ADD_STATS##modifier((_idev)->stats.statname, (field), (val)); \ 1569261e537SDenis V. Lunev SNMP_ADD_STATS##modifier((net)->mib.statname##_statistics, (field), (val));\ 1578e7999c4SPavel Emelyanov }) 1588e7999c4SPavel Emelyanov 159edf391ffSNeil Horman #define _DEVUPD(net, statname, modifier, idev, field, val) \ 160edf391ffSNeil Horman ({ \ 161edf391ffSNeil Horman struct inet6_dev *_idev = (idev); \ 162edf391ffSNeil Horman if (likely(_idev != NULL)) \ 163edf391ffSNeil Horman SNMP_UPD_PO_STATS##modifier((_idev)->stats.statname, field, (val)); \ 164edf391ffSNeil Horman SNMP_UPD_PO_STATS##modifier((net)->mib.statname##_statistics, field, (val));\ 165edf391ffSNeil Horman }) 166edf391ffSNeil Horman 1671da177e4SLinus Torvalds /* MIBs */ 16814878f75SDavid L Stevens 169087fe240SDenis V. Lunev #define IP6_INC_STATS(net, idev,field) \ 1704ce3c183SEric Dumazet _DEVINC(net, ipv6, 64, idev, field) 171087fe240SDenis V. Lunev #define IP6_INC_STATS_BH(net, idev,field) \ 1724ce3c183SEric Dumazet _DEVINC(net, ipv6, 64_BH, idev, field) 173edf391ffSNeil Horman #define IP6_ADD_STATS(net, idev,field,val) \ 1744ce3c183SEric Dumazet _DEVADD(net, ipv6, 64, idev, field, val) 175087fe240SDenis V. Lunev #define IP6_ADD_STATS_BH(net, idev,field,val) \ 1764ce3c183SEric Dumazet _DEVADD(net, ipv6, 64_BH, idev, field, val) 177edf391ffSNeil Horman #define IP6_UPD_PO_STATS(net, idev,field,val) \ 1784ce3c183SEric Dumazet _DEVUPD(net, ipv6, 64, idev, field, val) 179edf391ffSNeil Horman #define IP6_UPD_PO_STATS_BH(net, idev,field,val) \ 1804ce3c183SEric Dumazet _DEVUPD(net, ipv6, 64_BH, idev, field, val) 181087fe240SDenis V. Lunev #define ICMP6_INC_STATS(net, idev, field) \ 182be281e55SEric Dumazet _DEVINCATOMIC(net, icmpv6, , idev, field) 183087fe240SDenis V. Lunev #define ICMP6_INC_STATS_BH(net, idev, field) \ 184be281e55SEric Dumazet _DEVINCATOMIC(net, icmpv6, _BH, idev, field) 18514878f75SDavid L Stevens 186087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS(net, idev, field) \ 1872a24444fSEric Dumazet _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256) 188087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS_BH(net, idev, field) \ 1892a24444fSEric Dumazet _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256) 190087fe240SDenis V. Lunev #define ICMP6MSGIN_INC_STATS_BH(net, idev, field) \ 1912a24444fSEric Dumazet _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field) 19214878f75SDavid L Stevens 193fd2c3ef7SEric Dumazet struct ip6_ra_chain { 1941da177e4SLinus Torvalds struct ip6_ra_chain *next; 1951da177e4SLinus Torvalds struct sock *sk; 1961da177e4SLinus Torvalds int sel; 1971da177e4SLinus Torvalds void (*destructor)(struct sock *); 1981da177e4SLinus Torvalds }; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds extern struct ip6_ra_chain *ip6_ra_chain; 2011da177e4SLinus Torvalds extern rwlock_t ip6_ra_lock; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds This structure is prepared by protocol, when parsing 2051da177e4SLinus Torvalds ancillary data and passed to IPv6. 2061da177e4SLinus Torvalds */ 2071da177e4SLinus Torvalds 208fd2c3ef7SEric Dumazet struct ipv6_txoptions { 2091da177e4SLinus Torvalds /* Length of this structure */ 2101da177e4SLinus Torvalds int tot_len; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* length of extension headers */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds __u16 opt_flen; /* after fragment hdr */ 2151da177e4SLinus Torvalds __u16 opt_nflen; /* before fragment hdr */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds struct ipv6_opt_hdr *hopopt; 2181da177e4SLinus Torvalds struct ipv6_opt_hdr *dst0opt; 2191da177e4SLinus Torvalds struct ipv6_rt_hdr *srcrt; /* Routing Header */ 2201da177e4SLinus Torvalds struct ipv6_opt_hdr *dst1opt; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */ 2231da177e4SLinus Torvalds }; 2241da177e4SLinus Torvalds 225fd2c3ef7SEric Dumazet struct ip6_flowlabel { 2267f0e44acSEric Dumazet struct ip6_flowlabel __rcu *next; 22790bcaf7bSAl Viro __be32 label; 228db3459d1SEric Dumazet atomic_t users; 2291da177e4SLinus Torvalds struct in6_addr dst; 2301da177e4SLinus Torvalds struct ipv6_txoptions *opt; 2311da177e4SLinus Torvalds unsigned long linger; 232d3aedd5eSYOSHIFUJI Hideaki / 吉藤英明 struct rcu_head rcu; 2331da177e4SLinus Torvalds u8 share; 2344f82f457SEric W. Biederman union { 2354f82f457SEric W. Biederman struct pid *pid; 2364f82f457SEric W. Biederman kuid_t uid; 2374f82f457SEric W. Biederman } owner; 2381da177e4SLinus Torvalds unsigned long lastuse; 2391da177e4SLinus Torvalds unsigned long expires; 24060e8fbc4SBenjamin Thery struct net *fl_net; 2411da177e4SLinus Torvalds }; 2421da177e4SLinus Torvalds 243f3a7c66bSHarvey Harrison #define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) 244f3a7c66bSHarvey Harrison #define IPV6_FLOWLABEL_MASK cpu_to_be32(0x000FFFFF) 24537cfee90SFlorent Fourcot #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) 246d76ed22bSLi RongQing #define IPV6_TCLASS_SHIFT 20 2471da177e4SLinus Torvalds 248fd2c3ef7SEric Dumazet struct ipv6_fl_socklist { 2497f0e44acSEric Dumazet struct ipv6_fl_socklist __rcu *next; 2501da177e4SLinus Torvalds struct ip6_flowlabel *fl; 25118367681SYOSHIFUJI Hideaki / 吉藤英明 struct rcu_head rcu; 2521da177e4SLinus Torvalds }; 2531da177e4SLinus Torvalds 2545c3a0fd7SJoe Perches struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk, __be32 label); 2555c3a0fd7SJoe Perches struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space, 2561da177e4SLinus Torvalds struct ip6_flowlabel *fl, 2571da177e4SLinus Torvalds struct ipv6_txoptions *fopt); 2585c3a0fd7SJoe Perches void fl6_free_socklist(struct sock *sk); 2595c3a0fd7SJoe Perches int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen); 26046e5f401SFlorent Fourcot int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq, 26146e5f401SFlorent Fourcot int flags); 2625c3a0fd7SJoe Perches int ip6_flowlabel_init(void); 2635c3a0fd7SJoe Perches void ip6_flowlabel_cleanup(void); 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds static inline void fl6_sock_release(struct ip6_flowlabel *fl) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds if (fl) 2681da177e4SLinus Torvalds atomic_dec(&fl->users); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2715c3a0fd7SJoe Perches void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info); 272b94f1c09SDavid S. Miller 2736d0bfe22SLorenzo Colitti int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 2746d0bfe22SLorenzo Colitti struct icmp6hdr *thdr, int len); 2756d0bfe22SLorenzo Colitti 2765c3a0fd7SJoe Perches int ip6_ra_control(struct sock *sk, int sel); 2771da177e4SLinus Torvalds 2785c3a0fd7SJoe Perches int ipv6_parse_hopopts(struct sk_buff *skb); 2791da177e4SLinus Torvalds 2805c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_dup_options(struct sock *sk, 2815c3a0fd7SJoe Perches struct ipv6_txoptions *opt); 2825c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_renew_options(struct sock *sk, 2835c3a0fd7SJoe Perches struct ipv6_txoptions *opt, 284333fad53SYOSHIFUJI Hideaki int newtype, 285333fad53SYOSHIFUJI Hideaki struct ipv6_opt_hdr __user *newopt, 286333fad53SYOSHIFUJI Hideaki int newoptlen); 287df9890c3SYOSHIFUJI Hideaki struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, 288df9890c3SYOSHIFUJI Hideaki struct ipv6_txoptions *opt); 2891da177e4SLinus Torvalds 2905c3a0fd7SJoe Perches bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb); 291399c07deSArnaldo Carvalho de Melo 292aeaf6e9dSShmulik Ladkani static inline bool ipv6_accept_ra(struct inet6_dev *idev) 293aeaf6e9dSShmulik Ladkani { 294aeaf6e9dSShmulik Ladkani /* If forwarding is enabled, RA are not accepted unless the special 295aeaf6e9dSShmulik Ladkani * hybrid mode (accept_ra=2) is enabled. 296aeaf6e9dSShmulik Ladkani */ 297aeaf6e9dSShmulik Ladkani return idev->cnf.forwarding ? idev->cnf.accept_ra == 2 : 298aeaf6e9dSShmulik Ladkani idev->cnf.accept_ra; 299aeaf6e9dSShmulik Ladkani } 300aeaf6e9dSShmulik Ladkani 301d4915c08SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6) 302d4915c08SAmerigo Wang static inline int ip6_frag_nqueues(struct net *net) 303d4915c08SAmerigo Wang { 304d4915c08SAmerigo Wang return net->ipv6.frags.nqueues; 305d4915c08SAmerigo Wang } 306d4915c08SAmerigo Wang 307d4915c08SAmerigo Wang static inline int ip6_frag_mem(struct net *net) 308d4915c08SAmerigo Wang { 309d433673eSJesper Dangaard Brouer return sum_frag_mem_limit(&net->ipv6.frags); 310d4915c08SAmerigo Wang } 311d4915c08SAmerigo Wang #endif 3121da177e4SLinus Torvalds 313c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_HIGH_THRESH (4 * 1024*1024) /* 4194304 */ 314c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_LOW_THRESH (3 * 1024*1024) /* 3145728 */ 3151da177e4SLinus Torvalds #define IPV6_FRAG_TIMEOUT (60 * HZ) /* 60 seconds */ 3161da177e4SLinus Torvalds 3175c3a0fd7SJoe Perches int __ipv6_addr_type(const struct in6_addr *addr); 318b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_type(const struct in6_addr *addr) 319b1cacb68SYOSHIFUJI Hideaki { 320b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_type(addr) & 0xffff; 321b1cacb68SYOSHIFUJI Hideaki } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static inline int ipv6_addr_scope(const struct in6_addr *addr) 3241da177e4SLinus Torvalds { 325b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK; 326b1cacb68SYOSHIFUJI Hideaki } 327b1cacb68SYOSHIFUJI Hideaki 328b1cacb68SYOSHIFUJI Hideaki static inline int __ipv6_addr_src_scope(int type) 329b1cacb68SYOSHIFUJI Hideaki { 330a02cec21SEric Dumazet return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16); 331b1cacb68SYOSHIFUJI Hideaki } 332b1cacb68SYOSHIFUJI Hideaki 333b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_src_scope(const struct in6_addr *addr) 334b1cacb68SYOSHIFUJI Hideaki { 335b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_src_scope(__ipv6_addr_type(addr)); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 338b7ef213eSHannes Frederic Sowa static inline bool __ipv6_addr_needs_scope_id(int type) 339b7ef213eSHannes Frederic Sowa { 340b7ef213eSHannes Frederic Sowa return type & IPV6_ADDR_LINKLOCAL || 341b7ef213eSHannes Frederic Sowa (type & IPV6_ADDR_MULTICAST && 342b7ef213eSHannes Frederic Sowa (type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL))); 343b7ef213eSHannes Frederic Sowa } 344b7ef213eSHannes Frederic Sowa 345b7ef213eSHannes Frederic Sowa static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface) 346b7ef213eSHannes Frederic Sowa { 347b7ef213eSHannes Frederic Sowa return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0; 348b7ef213eSHannes Frederic Sowa } 349b7ef213eSHannes Frederic Sowa 3501da177e4SLinus Torvalds static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2) 3511da177e4SLinus Torvalds { 352db3459d1SEric Dumazet return memcmp(a1, a2, sizeof(struct in6_addr)); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551a203cb3SEric Dumazet static inline bool 356f2ffd9eeSPatrick McHardy ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m, 357f2ffd9eeSPatrick McHardy const struct in6_addr *a2) 358f2ffd9eeSPatrick McHardy { 3591a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 3601a203cb3SEric Dumazet const unsigned long *ul1 = (const unsigned long *)a1; 3611a203cb3SEric Dumazet const unsigned long *ulm = (const unsigned long *)m; 3621a203cb3SEric Dumazet const unsigned long *ul2 = (const unsigned long *)a2; 3631a203cb3SEric Dumazet 3641a203cb3SEric Dumazet return !!(((ul1[0] ^ ul2[0]) & ulm[0]) | 3651a203cb3SEric Dumazet ((ul1[1] ^ ul2[1]) & ulm[1])); 3661a203cb3SEric Dumazet #else 367a02cec21SEric Dumazet return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) | 368fed85383SYOSHIFUJI Hideaki ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) | 369fed85383SYOSHIFUJI Hideaki ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) | 370a02cec21SEric Dumazet ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3])); 3711a203cb3SEric Dumazet #endif 372f2ffd9eeSPatrick McHardy } 373f2ffd9eeSPatrick McHardy 3741da177e4SLinus Torvalds static inline void ipv6_addr_prefix(struct in6_addr *pfx, 3751da177e4SLinus Torvalds const struct in6_addr *addr, 3761da177e4SLinus Torvalds int plen) 3771da177e4SLinus Torvalds { 3781da177e4SLinus Torvalds /* caller must guarantee 0 <= plen <= 128 */ 3791da177e4SLinus Torvalds int o = plen >> 3, 3801da177e4SLinus Torvalds b = plen & 0x7; 3811da177e4SLinus Torvalds 382db3459d1SEric Dumazet memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr)); 3831da177e4SLinus Torvalds memcpy(pfx->s6_addr, addr, o); 384db3459d1SEric Dumazet if (b != 0) 3851da177e4SLinus Torvalds pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3885206c579SYOSHIFUJI Hideaki / 吉藤英明 static inline void __ipv6_addr_set_half(__be32 *addr, 3895206c579SYOSHIFUJI Hideaki / 吉藤英明 __be32 wh, __be32 wl) 3905206c579SYOSHIFUJI Hideaki / 吉藤英明 { 3915206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 3925206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(__BIG_ENDIAN) 3935206c579SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) { 3945206c579SYOSHIFUJI Hideaki / 吉藤英明 *(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl)); 3955206c579SYOSHIFUJI Hideaki / 吉藤英明 return; 3965206c579SYOSHIFUJI Hideaki / 吉藤英明 } 3975206c579SYOSHIFUJI Hideaki / 吉藤英明 #elif defined(__LITTLE_ENDIAN) 3985206c579SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) { 3995206c579SYOSHIFUJI Hideaki / 吉藤英明 *(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh)); 4005206c579SYOSHIFUJI Hideaki / 吉藤英明 return; 4015206c579SYOSHIFUJI Hideaki / 吉藤英明 } 4025206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif 4035206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif 4045206c579SYOSHIFUJI Hideaki / 吉藤英明 addr[0] = wh; 4055206c579SYOSHIFUJI Hideaki / 吉藤英明 addr[1] = wl; 4065206c579SYOSHIFUJI Hideaki / 吉藤英明 } 4075206c579SYOSHIFUJI Hideaki / 吉藤英明 4081da177e4SLinus Torvalds static inline void ipv6_addr_set(struct in6_addr *addr, 40948818f82SAl Viro __be32 w1, __be32 w2, 41048818f82SAl Viro __be32 w3, __be32 w4) 4111da177e4SLinus Torvalds { 4125206c579SYOSHIFUJI Hideaki / 吉藤英明 __ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2); 4135206c579SYOSHIFUJI Hideaki / 吉藤英明 __ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 41692113bfdSEric Dumazet static inline bool ipv6_addr_equal(const struct in6_addr *a1, 4171da177e4SLinus Torvalds const struct in6_addr *a2) 4181da177e4SLinus Torvalds { 4191a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 4201a203cb3SEric Dumazet const unsigned long *ul1 = (const unsigned long *)a1; 4211a203cb3SEric Dumazet const unsigned long *ul2 = (const unsigned long *)a2; 4221a203cb3SEric Dumazet 4231a203cb3SEric Dumazet return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL; 4241a203cb3SEric Dumazet #else 425a02cec21SEric Dumazet return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) | 426fed85383SYOSHIFUJI Hideaki (a1->s6_addr32[1] ^ a2->s6_addr32[1]) | 427fed85383SYOSHIFUJI Hideaki (a1->s6_addr32[2] ^ a2->s6_addr32[2]) | 428a02cec21SEric Dumazet (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0; 4291a203cb3SEric Dumazet #endif 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43238675170SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 43338675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool __ipv6_prefix_equal64_half(const __be64 *a1, 43438675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a2, 43538675170SYOSHIFUJI Hideaki / 吉藤英明 unsigned int len) 43638675170SYOSHIFUJI Hideaki / 吉藤英明 { 437512613d7SFabio Baltieri if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len)))) 43838675170SYOSHIFUJI Hideaki / 吉藤英明 return false; 43938675170SYOSHIFUJI Hideaki / 吉藤英明 return true; 44038675170SYOSHIFUJI Hideaki / 吉藤英明 } 44138675170SYOSHIFUJI Hideaki / 吉藤英明 44238675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1, 44338675170SYOSHIFUJI Hideaki / 吉藤英明 const struct in6_addr *addr2, 44438675170SYOSHIFUJI Hideaki / 吉藤英明 unsigned int prefixlen) 44538675170SYOSHIFUJI Hideaki / 吉藤英明 { 44638675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a1 = (const __be64 *)addr1; 44738675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a2 = (const __be64 *)addr2; 44838675170SYOSHIFUJI Hideaki / 吉藤英明 44938675170SYOSHIFUJI Hideaki / 吉藤英明 if (prefixlen >= 64) { 45038675170SYOSHIFUJI Hideaki / 吉藤英明 if (a1[0] ^ a2[0]) 45138675170SYOSHIFUJI Hideaki / 吉藤英明 return false; 45238675170SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64); 45338675170SYOSHIFUJI Hideaki / 吉藤英明 } 45438675170SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_prefix_equal64_half(a1, a2, prefixlen); 45538675170SYOSHIFUJI Hideaki / 吉藤英明 } 45638675170SYOSHIFUJI Hideaki / 吉藤英明 #else 4572ef97332SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1, 4582ef97332SYOSHIFUJI Hideaki / 吉藤英明 const struct in6_addr *addr2, 4591da177e4SLinus Torvalds unsigned int prefixlen) 4601da177e4SLinus Torvalds { 4612ef97332SYOSHIFUJI Hideaki / 吉藤英明 const __be32 *a1 = addr1->s6_addr32; 4622ef97332SYOSHIFUJI Hideaki / 吉藤英明 const __be32 *a2 = addr2->s6_addr32; 46395c96174SEric Dumazet unsigned int pdw, pbi; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* check complete u32 in prefix */ 4661da177e4SLinus Torvalds pdw = prefixlen >> 5; 4671da177e4SLinus Torvalds if (pdw && memcmp(a1, a2, pdw << 2)) 46892113bfdSEric Dumazet return false; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* check incomplete u32 in prefix */ 4711da177e4SLinus Torvalds pbi = prefixlen & 0x1f; 4721da177e4SLinus Torvalds if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi)))) 47392113bfdSEric Dumazet return false; 4741da177e4SLinus Torvalds 47592113bfdSEric Dumazet return true; 4761da177e4SLinus Torvalds } 47738675170SYOSHIFUJI Hideaki / 吉藤英明 #endif 4781da177e4SLinus Torvalds 4792588fe1dSPavel Emelyanov struct inet_frag_queue; 4802588fe1dSPavel Emelyanov 4810b5ccb2eSPatrick McHardy enum ip6_defrag_users { 4820b5ccb2eSPatrick McHardy IP6_DEFRAG_LOCAL_DELIVER, 4830b5ccb2eSPatrick McHardy IP6_DEFRAG_CONNTRACK_IN, 4844be929beSAlexey Dobriyan __IP6_DEFRAG_CONNTRACK_IN = IP6_DEFRAG_CONNTRACK_IN + USHRT_MAX, 4850b5ccb2eSPatrick McHardy IP6_DEFRAG_CONNTRACK_OUT, 4864be929beSAlexey Dobriyan __IP6_DEFRAG_CONNTRACK_OUT = IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 4878fa9ff68SPatrick McHardy IP6_DEFRAG_CONNTRACK_BRIDGE_IN, 4884be929beSAlexey Dobriyan __IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 4890b5ccb2eSPatrick McHardy }; 4900b5ccb2eSPatrick McHardy 491c6fda282SPavel Emelyanov struct ip6_create_arg { 492c6fda282SPavel Emelyanov __be32 id; 4930b5ccb2eSPatrick McHardy u32 user; 494b71d1d42SEric Dumazet const struct in6_addr *src; 495b71d1d42SEric Dumazet const struct in6_addr *dst; 496eec2e618SHannes Frederic Sowa u8 ecn; 497c6fda282SPavel Emelyanov }; 498c6fda282SPavel Emelyanov 499c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a); 500cbc264caSEric Dumazet bool ip6_frag_match(struct inet_frag_queue *q, void *a); 501c6fda282SPavel Emelyanov 502b836c99fSAmerigo Wang /* 503b836c99fSAmerigo Wang * Equivalent of ipv4 struct ip 504b836c99fSAmerigo Wang */ 505b836c99fSAmerigo Wang struct frag_queue { 506b836c99fSAmerigo Wang struct inet_frag_queue q; 507b836c99fSAmerigo Wang 508b836c99fSAmerigo Wang __be32 id; /* fragment id */ 509b836c99fSAmerigo Wang u32 user; 510b836c99fSAmerigo Wang struct in6_addr saddr; 511b836c99fSAmerigo Wang struct in6_addr daddr; 512b836c99fSAmerigo Wang 513b836c99fSAmerigo Wang int iif; 514b836c99fSAmerigo Wang unsigned int csum; 515b836c99fSAmerigo Wang __u16 nhoffset; 516eec2e618SHannes Frederic Sowa u8 ecn; 517b836c99fSAmerigo Wang }; 518b836c99fSAmerigo Wang 519b836c99fSAmerigo Wang void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq, 520b836c99fSAmerigo Wang struct inet_frags *frags); 521b836c99fSAmerigo Wang 52292113bfdSEric Dumazet static inline bool ipv6_addr_any(const struct in6_addr *a) 5231da177e4SLinus Torvalds { 5241a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 5251a203cb3SEric Dumazet const unsigned long *ul = (const unsigned long *)a; 5261a203cb3SEric Dumazet 5271a203cb3SEric Dumazet return (ul[0] | ul[1]) == 0UL; 5281a203cb3SEric Dumazet #else 529a02cec21SEric Dumazet return (a->s6_addr32[0] | a->s6_addr32[1] | 530a02cec21SEric Dumazet a->s6_addr32[2] | a->s6_addr32[3]) == 0; 5311a203cb3SEric Dumazet #endif 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 534ddbe5032SEric Dumazet static inline u32 ipv6_addr_hash(const struct in6_addr *a) 535ddbe5032SEric Dumazet { 536ddbe5032SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 537ddbe5032SEric Dumazet const unsigned long *ul = (const unsigned long *)a; 538ddbe5032SEric Dumazet unsigned long x = ul[0] ^ ul[1]; 539ddbe5032SEric Dumazet 540ddbe5032SEric Dumazet return (u32)(x ^ (x >> 32)); 541ddbe5032SEric Dumazet #else 542ddbe5032SEric Dumazet return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^ 543ddbe5032SEric Dumazet a->s6_addr32[2] ^ a->s6_addr32[3]); 544ddbe5032SEric Dumazet #endif 545ddbe5032SEric Dumazet } 546ddbe5032SEric Dumazet 54708dcdbf6SEric Dumazet /* more secured version of ipv6_addr_hash() */ 548b50026b5SHannes Frederic Sowa static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval) 54908dcdbf6SEric Dumazet { 55008dcdbf6SEric Dumazet u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1]; 55108dcdbf6SEric Dumazet 55208dcdbf6SEric Dumazet return jhash_3words(v, 55308dcdbf6SEric Dumazet (__force u32)a->s6_addr32[2], 55408dcdbf6SEric Dumazet (__force u32)a->s6_addr32[3], 555b50026b5SHannes Frederic Sowa initval); 55608dcdbf6SEric Dumazet } 55708dcdbf6SEric Dumazet 55892113bfdSEric Dumazet static inline bool ipv6_addr_loopback(const struct in6_addr *a) 559f630e43aSYOSHIFUJI Hideaki { 560e287656bSYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 561e287656bSYOSHIFUJI Hideaki / 吉藤英明 const unsigned long *ul = (const unsigned long *)a; 562e287656bSYOSHIFUJI Hideaki / 吉藤英明 563e287656bSYOSHIFUJI Hideaki / 吉藤英明 return (ul[0] | (ul[1] ^ cpu_to_be64(1))) == 0UL; 564e287656bSYOSHIFUJI Hideaki / 吉藤英明 #else 565a02cec21SEric Dumazet return (a->s6_addr32[0] | a->s6_addr32[1] | 566a02cec21SEric Dumazet a->s6_addr32[2] | (a->s6_addr32[3] ^ htonl(1))) == 0; 567e287656bSYOSHIFUJI Hideaki / 吉藤英明 #endif 568f630e43aSYOSHIFUJI Hideaki } 569f630e43aSYOSHIFUJI Hideaki 57092113bfdSEric Dumazet static inline bool ipv6_addr_v4mapped(const struct in6_addr *a) 571e773e4faSBrian Haley { 572a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 return ( 573a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 574a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 *(__be64 *)a | 575a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #else 576a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 (a->s6_addr32[0] | a->s6_addr32[1]) | 577a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #endif 578a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 (a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL; 579e773e4faSBrian Haley } 580e773e4faSBrian Haley 5811da177e4SLinus Torvalds /* 58299cd07a5SJuha-Matti Tapio * Check for a RFC 4843 ORCHID address 58399cd07a5SJuha-Matti Tapio * (Overlay Routable Cryptographic Hash Identifiers) 58499cd07a5SJuha-Matti Tapio */ 58592113bfdSEric Dumazet static inline bool ipv6_addr_orchid(const struct in6_addr *a) 58699cd07a5SJuha-Matti Tapio { 587a02cec21SEric Dumazet return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010); 58899cd07a5SJuha-Matti Tapio } 58999cd07a5SJuha-Matti Tapio 5905c98631cSLorenzo Colitti static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr) 5915c98631cSLorenzo Colitti { 5925c98631cSLorenzo Colitti return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000); 5935c98631cSLorenzo Colitti } 5945c98631cSLorenzo Colitti 595f15364bdSAurélien Charbon static inline void ipv6_addr_set_v4mapped(const __be32 addr, 596f15364bdSAurélien Charbon struct in6_addr *v4mapped) 597f15364bdSAurélien Charbon { 598f15364bdSAurélien Charbon ipv6_addr_set(v4mapped, 599f15364bdSAurélien Charbon 0, 0, 600f15364bdSAurélien Charbon htonl(0x0000FFFF), 601f15364bdSAurélien Charbon addr); 602f15364bdSAurélien Charbon } 603f15364bdSAurélien Charbon 60499cd07a5SJuha-Matti Tapio /* 605971f359dSYOSHIFUJI Hideaki * find the first different bit between two addresses 606971f359dSYOSHIFUJI Hideaki * length of address must be a multiple of 32bits 607971f359dSYOSHIFUJI Hideaki */ 6089f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen) 609971f359dSYOSHIFUJI Hideaki { 610ef296f56SAl Viro const __be32 *a1 = token1, *a2 = token2; 611971f359dSYOSHIFUJI Hideaki int i; 612971f359dSYOSHIFUJI Hideaki 613971f359dSYOSHIFUJI Hideaki addrlen >>= 2; 614971f359dSYOSHIFUJI Hideaki 615971f359dSYOSHIFUJI Hideaki for (i = 0; i < addrlen; i++) { 616ef296f56SAl Viro __be32 xb = a1[i] ^ a2[i]; 617ef296f56SAl Viro if (xb) 618d57b8fb8SYOSHIFUJI Hideaki / 吉藤英明 return i * 32 + 31 - __fls(ntohl(xb)); 619971f359dSYOSHIFUJI Hideaki } 620971f359dSYOSHIFUJI Hideaki 621971f359dSYOSHIFUJI Hideaki /* 622971f359dSYOSHIFUJI Hideaki * we should *never* get to this point since that 623971f359dSYOSHIFUJI Hideaki * would mean the addrs are equal 624971f359dSYOSHIFUJI Hideaki * 625971f359dSYOSHIFUJI Hideaki * However, we do get to it 8) And exacly, when 626971f359dSYOSHIFUJI Hideaki * addresses are equal 8) 627971f359dSYOSHIFUJI Hideaki * 628971f359dSYOSHIFUJI Hideaki * ip route add 1111::/128 via ... 629971f359dSYOSHIFUJI Hideaki * ip route add 1111::/64 via ... 630971f359dSYOSHIFUJI Hideaki * and we are here. 631971f359dSYOSHIFUJI Hideaki * 632971f359dSYOSHIFUJI Hideaki * Ideally, this function should stop comparison 633971f359dSYOSHIFUJI Hideaki * at prefix length. It does not, but it is still OK, 634971f359dSYOSHIFUJI Hideaki * if returned value is greater than prefix length. 635971f359dSYOSHIFUJI Hideaki * --ANK (980803) 636971f359dSYOSHIFUJI Hideaki */ 637a02cec21SEric Dumazet return addrlen << 5; 638971f359dSYOSHIFUJI Hideaki } 639971f359dSYOSHIFUJI Hideaki 6409f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 6419f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen) 6429f2e7334SYOSHIFUJI Hideaki / 吉藤英明 { 6439f2e7334SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a1 = token1, *a2 = token2; 6449f2e7334SYOSHIFUJI Hideaki / 吉藤英明 int i; 6459f2e7334SYOSHIFUJI Hideaki / 吉藤英明 6469f2e7334SYOSHIFUJI Hideaki / 吉藤英明 addrlen >>= 3; 6479f2e7334SYOSHIFUJI Hideaki / 吉藤英明 6489f2e7334SYOSHIFUJI Hideaki / 吉藤英明 for (i = 0; i < addrlen; i++) { 6499f2e7334SYOSHIFUJI Hideaki / 吉藤英明 __be64 xb = a1[i] ^ a2[i]; 6509f2e7334SYOSHIFUJI Hideaki / 吉藤英明 if (xb) 6519f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return i * 64 + 63 - __fls(be64_to_cpu(xb)); 6529f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 6539f2e7334SYOSHIFUJI Hideaki / 吉藤英明 6549f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return addrlen << 6; 6559f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 6569f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif 6579f2e7334SYOSHIFUJI Hideaki / 吉藤英明 6589f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen) 6599f2e7334SYOSHIFUJI Hideaki / 吉藤英明 { 6609f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 6619f2e7334SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(addrlen) && !(addrlen & 7)) 6629f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_addr_diff64(token1, token2, addrlen); 6639f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif 6649f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_addr_diff32(token1, token2, addrlen); 6659f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 6669f2e7334SYOSHIFUJI Hideaki / 吉藤英明 667971f359dSYOSHIFUJI Hideaki static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2) 668971f359dSYOSHIFUJI Hideaki { 669971f359dSYOSHIFUJI Hideaki return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr)); 670971f359dSYOSHIFUJI Hideaki } 671971f359dSYOSHIFUJI Hideaki 6725c3a0fd7SJoe Perches int ip6_dst_hoplimit(struct dst_entry *dst); 6733ce9b35fSCong Wang 6745c98631cSLorenzo Colitti static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6, 6755c98631cSLorenzo Colitti struct dst_entry *dst) 6765c98631cSLorenzo Colitti { 6775c98631cSLorenzo Colitti int hlimit; 6785c98631cSLorenzo Colitti 6795c98631cSLorenzo Colitti if (ipv6_addr_is_multicast(&fl6->daddr)) 6805c98631cSLorenzo Colitti hlimit = np->mcast_hops; 6815c98631cSLorenzo Colitti else 6825c98631cSLorenzo Colitti hlimit = np->hop_limit; 6835c98631cSLorenzo Colitti if (hlimit < 0) 6845c98631cSLorenzo Colitti hlimit = ip6_dst_hoplimit(dst); 6855c98631cSLorenzo Colitti return hlimit; 6865c98631cSLorenzo Colitti } 6875c98631cSLorenzo Colitti 688b73c3d0eSTom Herbert static inline void ip6_set_txhash(struct sock *sk) 689b73c3d0eSTom Herbert { 690b73c3d0eSTom Herbert struct inet_sock *inet = inet_sk(sk); 691b73c3d0eSTom Herbert struct ipv6_pinfo *np = inet6_sk(sk); 692b73c3d0eSTom Herbert struct flow_keys keys; 693b73c3d0eSTom Herbert 694b73c3d0eSTom Herbert keys.src = (__force __be32)ipv6_addr_hash(&np->saddr); 695b73c3d0eSTom Herbert keys.dst = (__force __be32)ipv6_addr_hash(&sk->sk_v6_daddr); 696b73c3d0eSTom Herbert keys.port16[0] = inet->inet_sport; 697b73c3d0eSTom Herbert keys.port16[1] = inet->inet_dport; 698b73c3d0eSTom Herbert 699b73c3d0eSTom Herbert sk->sk_txhash = flow_hash_from_keys(&keys); 700b73c3d0eSTom Herbert } 701b73c3d0eSTom Herbert 702*cb1ce2efSTom Herbert static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb, 703*cb1ce2efSTom Herbert __be32 flowlabel, bool autolabel) 704*cb1ce2efSTom Herbert { 705*cb1ce2efSTom Herbert if (!flowlabel && (autolabel || net->ipv6.sysctl.auto_flowlabels)) { 706*cb1ce2efSTom Herbert __be32 hash; 707*cb1ce2efSTom Herbert 708*cb1ce2efSTom Herbert hash = skb_get_hash(skb); 709*cb1ce2efSTom Herbert 710*cb1ce2efSTom Herbert /* Since this is being sent on the wire obfuscate hash a bit 711*cb1ce2efSTom Herbert * to minimize possbility that any useful information to an 712*cb1ce2efSTom Herbert * attacker is leaked. Only lower 20 bits are relevant. 713*cb1ce2efSTom Herbert */ 714*cb1ce2efSTom Herbert hash ^= hash >> 12; 715*cb1ce2efSTom Herbert 716*cb1ce2efSTom Herbert flowlabel = hash & IPV6_FLOWLABEL_MASK; 717*cb1ce2efSTom Herbert } 718*cb1ce2efSTom Herbert 719*cb1ce2efSTom Herbert return flowlabel; 720*cb1ce2efSTom Herbert } 721*cb1ce2efSTom Herbert 722971f359dSYOSHIFUJI Hideaki /* 7233e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 * Header manipulation 7243e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 */ 7253e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass, 7263e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 __be32 flowlabel) 7273e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 { 72807f623d3SYOSHIFUJI Hideaki *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel; 7293e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 } 7303e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 7316502ca52SYOSHIFUJI Hideaki / 吉藤英明 static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr) 7326502ca52SYOSHIFUJI Hideaki / 吉藤英明 { 7336502ca52SYOSHIFUJI Hideaki / 吉藤英明 return *(__be32 *)hdr & IPV6_FLOWINFO_MASK; 7346502ca52SYOSHIFUJI Hideaki / 吉藤英明 } 7356502ca52SYOSHIFUJI Hideaki / 吉藤英明 7363308de2bSFlorent Fourcot static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr) 7373308de2bSFlorent Fourcot { 7383308de2bSFlorent Fourcot return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK; 7393308de2bSFlorent Fourcot } 7403308de2bSFlorent Fourcot 741d76ed22bSLi RongQing static inline u8 ip6_tclass(__be32 flowinfo) 742d76ed22bSLi RongQing { 743d76ed22bSLi RongQing return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT; 744d76ed22bSLi RongQing } 7453e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 /* 7461da177e4SLinus Torvalds * Prototypes exported by ipv6 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * rcv function (called from netdevice level) 7511da177e4SLinus Torvalds */ 7521da177e4SLinus Torvalds 7535c3a0fd7SJoe Perches int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, 7545c3a0fd7SJoe Perches struct packet_type *pt, struct net_device *orig_dev); 7551da177e4SLinus Torvalds 7565c3a0fd7SJoe Perches int ip6_rcv_finish(struct sk_buff *skb); 757b05e1066SPatrick McHardy 7581da177e4SLinus Torvalds /* 7591da177e4SLinus Torvalds * upper-layer output functions 7601da177e4SLinus Torvalds */ 7615c3a0fd7SJoe Perches int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, 7625c3a0fd7SJoe Perches struct ipv6_txoptions *opt, int tclass); 7631da177e4SLinus Torvalds 7645c3a0fd7SJoe Perches int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr); 7651da177e4SLinus Torvalds 7665c3a0fd7SJoe Perches int ip6_append_data(struct sock *sk, 7675c3a0fd7SJoe Perches int getfrag(void *from, char *to, int offset, int len, 7685c3a0fd7SJoe Perches int odd, struct sk_buff *skb), 7695c3a0fd7SJoe Perches void *from, int length, int transhdrlen, int hlimit, 7705c3a0fd7SJoe Perches int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6, 7715c3a0fd7SJoe Perches struct rt6_info *rt, unsigned int flags, int dontfrag); 7721da177e4SLinus Torvalds 7735c3a0fd7SJoe Perches int ip6_push_pending_frames(struct sock *sk); 7741da177e4SLinus Torvalds 7755c3a0fd7SJoe Perches void ip6_flush_pending_frames(struct sock *sk); 7761da177e4SLinus Torvalds 7775c3a0fd7SJoe Perches int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6); 7785c3a0fd7SJoe Perches struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 7790e0d44abSSteffen Klassert const struct in6_addr *final_dst); 7805c3a0fd7SJoe Perches struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 7810e0d44abSSteffen Klassert const struct in6_addr *final_dst); 7825c3a0fd7SJoe Perches struct dst_entry *ip6_blackhole_route(struct net *net, 78369ead7afSDavid S. Miller struct dst_entry *orig_dst); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * skb processing functions 7871da177e4SLinus Torvalds */ 7881da177e4SLinus Torvalds 789aad88724SEric Dumazet int ip6_output(struct sock *sk, struct sk_buff *skb); 7905c3a0fd7SJoe Perches int ip6_forward(struct sk_buff *skb); 7915c3a0fd7SJoe Perches int ip6_input(struct sk_buff *skb); 7925c3a0fd7SJoe Perches int ip6_mc_input(struct sk_buff *skb); 7931da177e4SLinus Torvalds 7945c3a0fd7SJoe Perches int __ip6_local_out(struct sk_buff *skb); 7955c3a0fd7SJoe Perches int ip6_local_out(struct sk_buff *skb); 796ef76bc23SHerbert Xu 7971da177e4SLinus Torvalds /* 7981da177e4SLinus Torvalds * Extension header (options) processing 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds 8015c3a0fd7SJoe Perches void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, 8025c3a0fd7SJoe Perches u8 *proto, struct in6_addr **daddr_p); 8035c3a0fd7SJoe Perches void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, 8041da177e4SLinus Torvalds u8 *proto); 8051da177e4SLinus Torvalds 8065c3a0fd7SJoe Perches int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp, 8075c3a0fd7SJoe Perches __be16 *frag_offp); 8081da177e4SLinus Torvalds 8095c3a0fd7SJoe Perches bool ipv6_ext_hdr(u8 nexthdr); 8101da177e4SLinus Torvalds 811f8f62675SJesse Gross enum { 812f8f62675SJesse Gross IP6_FH_F_FRAG = (1 << 0), 813f8f62675SJesse Gross IP6_FH_F_AUTH = (1 << 1), 8149195bb8eSAnsis Atteka IP6_FH_F_SKIP_RH = (1 << 2), 815f8f62675SJesse Gross }; 816f8f62675SJesse Gross 817f8f62675SJesse Gross /* find specified header and get offset to it */ 8185c3a0fd7SJoe Perches int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target, 8195c3a0fd7SJoe Perches unsigned short *fragoff, int *fragflg); 820f8f62675SJesse Gross 8215c3a0fd7SJoe Perches int ipv6_find_tlv(struct sk_buff *skb, int offset, int type); 822c61a4043SMasahide NAKAMURA 8235c3a0fd7SJoe Perches struct in6_addr *fl6_update_dst(struct flowi6 *fl6, 82420c59de2SArnaud Ebalard const struct ipv6_txoptions *opt, 82520c59de2SArnaud Ebalard struct in6_addr *orig); 82620c59de2SArnaud Ebalard 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * socket options (ipv6_sockglue.c) 8291da177e4SLinus Torvalds */ 8301da177e4SLinus Torvalds 8315c3a0fd7SJoe Perches int ipv6_setsockopt(struct sock *sk, int level, int optname, 8325c3a0fd7SJoe Perches char __user *optval, unsigned int optlen); 8335c3a0fd7SJoe Perches int ipv6_getsockopt(struct sock *sk, int level, int optname, 8345c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 8355c3a0fd7SJoe Perches int compat_ipv6_setsockopt(struct sock *sk, int level, int optname, 8365c3a0fd7SJoe Perches char __user *optval, unsigned int optlen); 8375c3a0fd7SJoe Perches int compat_ipv6_getsockopt(struct sock *sk, int level, int optname, 8385c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 8391da177e4SLinus Torvalds 8405c3a0fd7SJoe Perches int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len); 84182b276cdSHannes Frederic Sowa int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *addr, 84282b276cdSHannes Frederic Sowa int addr_len); 8431da177e4SLinus Torvalds 84485fbaa75SHannes Frederic Sowa int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, 84585fbaa75SHannes Frederic Sowa int *addr_len); 84685fbaa75SHannes Frederic Sowa int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, 84785fbaa75SHannes Frederic Sowa int *addr_len); 8485c3a0fd7SJoe Perches void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 8491da177e4SLinus Torvalds u32 info, u8 *payload); 8505c3a0fd7SJoe Perches void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info); 8515c3a0fd7SJoe Perches void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu); 8521da177e4SLinus Torvalds 8535c3a0fd7SJoe Perches int inet6_release(struct socket *sock); 8545c3a0fd7SJoe Perches int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len); 8555c3a0fd7SJoe Perches int inet6_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, 8565c3a0fd7SJoe Perches int peer); 8575c3a0fd7SJoe Perches int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 8581da177e4SLinus Torvalds 8595c3a0fd7SJoe Perches int inet6_hash_connect(struct inet_timewait_death_row *death_row, 860d8313f5cSArnaldo Carvalho de Melo struct sock *sk); 861d8313f5cSArnaldo Carvalho de Melo 8621da177e4SLinus Torvalds /* 8631da177e4SLinus Torvalds * reassembly.c 8641da177e4SLinus Torvalds */ 86590ddc4f0SEric Dumazet extern const struct proto_ops inet6_stream_ops; 86690ddc4f0SEric Dumazet extern const struct proto_ops inet6_dgram_ops; 86720380731SArnaldo Carvalho de Melo 86814c85021SArnaldo Carvalho de Melo struct group_source_req; 86914c85021SArnaldo Carvalho de Melo struct group_filter; 87014c85021SArnaldo Carvalho de Melo 8715c3a0fd7SJoe Perches int ip6_mc_source(int add, int omode, struct sock *sk, 87220380731SArnaldo Carvalho de Melo struct group_source_req *pgsr); 8735c3a0fd7SJoe Perches int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf); 8745c3a0fd7SJoe Perches int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf, 8755c3a0fd7SJoe Perches struct group_filter __user *optval, int __user *optlen); 87620380731SArnaldo Carvalho de Melo 87720380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 8785c3a0fd7SJoe Perches int ac6_proc_init(struct net *net); 8795c3a0fd7SJoe Perches void ac6_proc_exit(struct net *net); 8805c3a0fd7SJoe Perches int raw6_proc_init(void); 8815c3a0fd7SJoe Perches void raw6_proc_exit(void); 8825c3a0fd7SJoe Perches int tcp6_proc_init(struct net *net); 8835c3a0fd7SJoe Perches void tcp6_proc_exit(struct net *net); 8845c3a0fd7SJoe Perches int udp6_proc_init(struct net *net); 8855c3a0fd7SJoe Perches void udp6_proc_exit(struct net *net); 8865c3a0fd7SJoe Perches int udplite6_proc_init(void); 8875c3a0fd7SJoe Perches void udplite6_proc_exit(void); 8885c3a0fd7SJoe Perches int ipv6_misc_proc_init(void); 8895c3a0fd7SJoe Perches void ipv6_misc_proc_exit(void); 8905c3a0fd7SJoe Perches int snmp6_register_dev(struct inet6_dev *idev); 8915c3a0fd7SJoe Perches int snmp6_unregister_dev(struct inet6_dev *idev); 89220380731SArnaldo Carvalho de Melo 8937f7d9a6bSHerbert Xu #else 8946ab57e7eSDaniel Lezcano static inline int ac6_proc_init(struct net *net) { return 0; } 8956ab57e7eSDaniel Lezcano static inline void ac6_proc_exit(struct net *net) { } 8966ab57e7eSDaniel Lezcano static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; } 8976ab57e7eSDaniel Lezcano static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; } 89820380731SArnaldo Carvalho de Melo #endif 89920380731SArnaldo Carvalho de Melo 90020380731SArnaldo Carvalho de Melo #ifdef CONFIG_SYSCTL 9019e8cda3bSJoe Perches extern struct ctl_table ipv6_route_table_template[]; 90220380731SArnaldo Carvalho de Melo 9035c3a0fd7SJoe Perches struct ctl_table *ipv6_icmp_sysctl_init(struct net *net); 9045c3a0fd7SJoe Perches struct ctl_table *ipv6_route_sysctl_init(struct net *net); 9055c3a0fd7SJoe Perches int ipv6_sysctl_register(void); 9065c3a0fd7SJoe Perches void ipv6_sysctl_unregister(void); 90720380731SArnaldo Carvalho de Melo #endif 90820380731SArnaldo Carvalho de Melo 9091da177e4SLinus Torvalds #endif /* _NET_IPV6_H */ 910