12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <[email protected]> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #ifndef _NET_IPV6_H 101da177e4SLinus Torvalds #define _NET_IPV6_H 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/ipv6.h> 131da177e4SLinus Torvalds #include <linux/hardirq.h> 1408dcdbf6SEric Dumazet #include <linux/jhash.h> 150aeea21aSReshetova, Elena #include <linux/refcount.h> 1659c820b2SWillem de Bruijn #include <linux/jump_label_ratelimit.h> 1720283d84SHerbert Xu #include <net/if_inet6.h> 181da177e4SLinus Torvalds #include <net/ndisc.h> 191da177e4SLinus Torvalds #include <net/flow.h> 201bd758ebSJiri Pirko #include <net/flow_dissector.h> 211da177e4SLinus Torvalds #include <net/snmp.h> 22f0b1e64cSMartin KaFai Lau #include <net/netns/hash.h> 231da177e4SLinus Torvalds 24b6459415SJakub Kicinski struct ip_tunnel_info; 25b6459415SJakub Kicinski 261da177e4SLinus Torvalds #define SIN6_LEN_RFC2133 24 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #define IPV6_MAXPLEN 65535 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * NextHeader field of IPv6 header 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define NEXTHDR_HOP 0 /* Hop-by-hop option header. */ 35ee90c6baSJulien Massonneau #define NEXTHDR_IPV4 4 /* IPv4 in IPv6 */ 361da177e4SLinus Torvalds #define NEXTHDR_TCP 6 /* TCP segment. */ 371da177e4SLinus Torvalds #define NEXTHDR_UDP 17 /* UDP message. */ 381da177e4SLinus Torvalds #define NEXTHDR_IPV6 41 /* IPv6 in IPv6 */ 391da177e4SLinus Torvalds #define NEXTHDR_ROUTING 43 /* Routing header. */ 401da177e4SLinus Torvalds #define NEXTHDR_FRAGMENT 44 /* Fragmentation/reassembly header. */ 41c12b395aS[email protected] #define NEXTHDR_GRE 47 /* GRE header. */ 421da177e4SLinus Torvalds #define NEXTHDR_ESP 50 /* Encapsulating security payload. */ 431da177e4SLinus Torvalds #define NEXTHDR_AUTH 51 /* Authentication header. */ 441da177e4SLinus Torvalds #define NEXTHDR_ICMP 58 /* ICMP for IPv6. */ 451da177e4SLinus Torvalds #define NEXTHDR_NONE 59 /* No next header */ 461da177e4SLinus Torvalds #define NEXTHDR_DEST 60 /* Destination options header. */ 47280c571eSJoe Stringer #define NEXTHDR_SCTP 132 /* SCTP message. */ 482b741653SMasahide NAKAMURA #define NEXTHDR_MOBILITY 135 /* Mobility header. */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define NEXTHDR_MAX 255 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #define IPV6_DEFAULT_HOPLIMIT 64 531da177e4SLinus Torvalds #define IPV6_DEFAULT_MCASTHOPS 1 541da177e4SLinus Torvalds 5547d3d7acSTom Herbert /* Limits on Hop-by-Hop and Destination options. 5647d3d7acSTom Herbert * 5747d3d7acSTom Herbert * Per RFC8200 there is no limit on the maximum number or lengths of options in 5847d3d7acSTom Herbert * Hop-by-Hop or Destination options other then the packet must fit in an MTU. 5947d3d7acSTom Herbert * We allow configurable limits in order to mitigate potential denial of 6047d3d7acSTom Herbert * service attacks. 6147d3d7acSTom Herbert * 6247d3d7acSTom Herbert * There are three limits that may be set: 6347d3d7acSTom Herbert * - Limit the number of options in a Hop-by-Hop or Destination options 6447d3d7acSTom Herbert * extension header 6547d3d7acSTom Herbert * - Limit the byte length of a Hop-by-Hop or Destination options extension 6647d3d7acSTom Herbert * header 6747d3d7acSTom Herbert * - Disallow unknown options 6847d3d7acSTom Herbert * 6947d3d7acSTom Herbert * The limits are expressed in corresponding sysctls: 7047d3d7acSTom Herbert * 7147d3d7acSTom Herbert * ipv6.sysctl.max_dst_opts_cnt 7247d3d7acSTom Herbert * ipv6.sysctl.max_hbh_opts_cnt 7347d3d7acSTom Herbert * ipv6.sysctl.max_dst_opts_len 7447d3d7acSTom Herbert * ipv6.sysctl.max_hbh_opts_len 7547d3d7acSTom Herbert * 7647d3d7acSTom Herbert * max_*_opts_cnt is the number of TLVs that are allowed for Destination 7747d3d7acSTom Herbert * options or Hop-by-Hop options. If the number is less than zero then unknown 7847d3d7acSTom Herbert * TLVs are disallowed and the number of known options that are allowed is the 7947d3d7acSTom Herbert * absolute value. Setting the value to INT_MAX indicates no limit. 8047d3d7acSTom Herbert * 8147d3d7acSTom Herbert * max_*_opts_len is the length limit in bytes of a Destination or 8247d3d7acSTom Herbert * Hop-by-Hop options extension header. Setting the value to INT_MAX 8347d3d7acSTom Herbert * indicates no length limit. 8447d3d7acSTom Herbert * 8547d3d7acSTom Herbert * If a limit is exceeded when processing an extension header the packet is 8647d3d7acSTom Herbert * silently discarded. 8747d3d7acSTom Herbert */ 8847d3d7acSTom Herbert 8947d3d7acSTom Herbert /* Default limits for Hop-by-Hop and Destination options */ 9047d3d7acSTom Herbert #define IP6_DEFAULT_MAX_DST_OPTS_CNT 8 9147d3d7acSTom Herbert #define IP6_DEFAULT_MAX_HBH_OPTS_CNT 8 9247d3d7acSTom Herbert #define IP6_DEFAULT_MAX_DST_OPTS_LEN INT_MAX /* No limit */ 9347d3d7acSTom Herbert #define IP6_DEFAULT_MAX_HBH_OPTS_LEN INT_MAX /* No limit */ 9447d3d7acSTom Herbert 951da177e4SLinus Torvalds /* 961da177e4SLinus Torvalds * Addr type 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * type - unicast | multicast 991da177e4SLinus Torvalds * scope - local | site | global 1001da177e4SLinus Torvalds * v4 - compat 1011da177e4SLinus Torvalds * v4mapped 1021da177e4SLinus Torvalds * any 1031da177e4SLinus Torvalds * loopback 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds #define IPV6_ADDR_ANY 0x0000U 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds #define IPV6_ADDR_UNICAST 0x0001U 1091da177e4SLinus Torvalds #define IPV6_ADDR_MULTICAST 0x0002U 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #define IPV6_ADDR_LOOPBACK 0x0010U 1121da177e4SLinus Torvalds #define IPV6_ADDR_LINKLOCAL 0x0020U 1131da177e4SLinus Torvalds #define IPV6_ADDR_SITELOCAL 0x0040U 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #define IPV6_ADDR_COMPATv4 0x0080U 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_MASK 0x00f0U 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds #define IPV6_ADDR_MAPPED 0x1000U 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Addr scopes 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds #define IPV6_ADDR_MC_SCOPE(a) \ 1251da177e4SLinus Torvalds ((a)->s6_addr[1] & 0x0f) /* nonstandard */ 1261da177e4SLinus Torvalds #define __IPV6_ADDR_SCOPE_INVALID -1 1271da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_NODELOCAL 0x01 1281da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_LINKLOCAL 0x02 1291da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_SITELOCAL 0x05 1301da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_ORGLOCAL 0x08 1311da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_GLOBAL 0x0e 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 1345ced1339SLinus Lüssing * Addr flags 1355ced1339SLinus Lüssing */ 1365ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_TRANSIENT(a) \ 1375ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x10) 1385ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_PREFIX(a) \ 1395ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x20) 1405ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a) \ 1415ced1339SLinus Lüssing ((a)->s6_addr[1] & 0x40) 1425ced1339SLinus Lüssing 1435ced1339SLinus Lüssing /* 1441da177e4SLinus Torvalds * fragmentation header 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds struct frag_hdr { 14844473a6bSAl Viro __u8 nexthdr; 14944473a6bSAl Viro __u8 reserved; 15044473a6bSAl Viro __be16 frag_off; 15144473a6bSAl Viro __be32 identification; 1521da177e4SLinus Torvalds }; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds #define IP6_MF 0x0001 1551431fb31SPaul Durrant #define IP6_OFFSET 0xFFF8 1561da177e4SLinus Torvalds 1570feca619SPablo Neira Ayuso struct ip6_fraglist_iter { 1580feca619SPablo Neira Ayuso struct ipv6hdr *tmp_hdr; 1590feca619SPablo Neira Ayuso struct sk_buff *frag; 1600feca619SPablo Neira Ayuso int offset; 1610feca619SPablo Neira Ayuso unsigned int hlen; 1620feca619SPablo Neira Ayuso __be32 frag_id; 1630feca619SPablo Neira Ayuso u8 nexthdr; 1640feca619SPablo Neira Ayuso }; 1650feca619SPablo Neira Ayuso 1660feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr, 1670feca619SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, 1680feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter); 1690feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb, struct ip6_fraglist_iter *iter); 1700feca619SPablo Neira Ayuso 1710feca619SPablo Neira Ayuso static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter) 1720feca619SPablo Neira Ayuso { 1730feca619SPablo Neira Ayuso struct sk_buff *skb = iter->frag; 1740feca619SPablo Neira Ayuso 1750feca619SPablo Neira Ayuso iter->frag = skb->next; 1760feca619SPablo Neira Ayuso skb_mark_not_on_list(skb); 1770feca619SPablo Neira Ayuso 1780feca619SPablo Neira Ayuso return skb; 1790feca619SPablo Neira Ayuso } 1800feca619SPablo Neira Ayuso 1818a6a1f17SPablo Neira Ayuso struct ip6_frag_state { 1828a6a1f17SPablo Neira Ayuso u8 *prevhdr; 1838a6a1f17SPablo Neira Ayuso unsigned int hlen; 1848a6a1f17SPablo Neira Ayuso unsigned int mtu; 1858a6a1f17SPablo Neira Ayuso unsigned int left; 1868a6a1f17SPablo Neira Ayuso int offset; 1878a6a1f17SPablo Neira Ayuso int ptr; 1888a6a1f17SPablo Neira Ayuso int hroom; 1898a6a1f17SPablo Neira Ayuso int troom; 1908a6a1f17SPablo Neira Ayuso __be32 frag_id; 1918a6a1f17SPablo Neira Ayuso u8 nexthdr; 1928a6a1f17SPablo Neira Ayuso }; 1938a6a1f17SPablo Neira Ayuso 1948a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu, 1958a6a1f17SPablo Neira Ayuso unsigned short needed_tailroom, int hdr_room, u8 *prevhdr, 1968a6a1f17SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state); 1978a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, 1988a6a1f17SPablo Neira Ayuso struct ip6_frag_state *state); 1998a6a1f17SPablo Neira Ayuso 200e110861fSLorenzo Colitti #define IP6_REPLY_MARK(net, mark) \ 201e110861fSLorenzo Colitti ((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0) 202e110861fSLorenzo Colitti 2031da177e4SLinus Torvalds #include <net/sock.h> 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* sysctls */ 2061da177e4SLinus Torvalds extern int sysctl_mld_max_msf; 2072f711939SHannes Frederic Sowa extern int sysctl_mld_qrv; 2083d7cc2baSPavel Emelyanov 20913415e46SEric Dumazet #define _DEVINC(net, statname, mod, idev, field) \ 21014878f75SDavid L Stevens ({ \ 21114878f75SDavid L Stevens struct inet6_dev *_idev = (idev); \ 21214878f75SDavid L Stevens if (likely(_idev != NULL)) \ 21313415e46SEric Dumazet mod##SNMP_INC_STATS64((_idev)->stats.statname, (field));\ 21413415e46SEric Dumazet mod##SNMP_INC_STATS64((net)->mib.statname##_statistics, (field));\ 21514878f75SDavid L Stevens }) 21614878f75SDavid L Stevens 217be281e55SEric Dumazet /* per device counters are atomic_long_t */ 21813415e46SEric Dumazet #define _DEVINCATOMIC(net, statname, mod, idev, field) \ 219be281e55SEric Dumazet ({ \ 220be281e55SEric Dumazet struct inet6_dev *_idev = (idev); \ 221be281e55SEric Dumazet if (likely(_idev != NULL)) \ 222be281e55SEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \ 22313415e46SEric Dumazet mod##SNMP_INC_STATS((net)->mib.statname##_statistics, (field));\ 224be281e55SEric Dumazet }) 225be281e55SEric Dumazet 2262a24444fSEric Dumazet /* per device and per net counters are atomic_long_t */ 2272a24444fSEric Dumazet #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field) \ 2282a24444fSEric Dumazet ({ \ 2292a24444fSEric Dumazet struct inet6_dev *_idev = (idev); \ 2302a24444fSEric Dumazet if (likely(_idev != NULL)) \ 2312a24444fSEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \ 2322a24444fSEric Dumazet SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\ 2332a24444fSEric Dumazet }) 2342a24444fSEric Dumazet 23513415e46SEric Dumazet #define _DEVADD(net, statname, mod, idev, field, val) \ 2368e7999c4SPavel Emelyanov ({ \ 2378e7999c4SPavel Emelyanov struct inet6_dev *_idev = (idev); \ 2388e7999c4SPavel Emelyanov if (likely(_idev != NULL)) \ 23913415e46SEric Dumazet mod##SNMP_ADD_STATS((_idev)->stats.statname, (field), (val)); \ 24013415e46SEric Dumazet mod##SNMP_ADD_STATS((net)->mib.statname##_statistics, (field), (val));\ 2418e7999c4SPavel Emelyanov }) 2428e7999c4SPavel Emelyanov 24313415e46SEric Dumazet #define _DEVUPD(net, statname, mod, idev, field, val) \ 244edf391ffSNeil Horman ({ \ 245edf391ffSNeil Horman struct inet6_dev *_idev = (idev); \ 246edf391ffSNeil Horman if (likely(_idev != NULL)) \ 24713415e46SEric Dumazet mod##SNMP_UPD_PO_STATS((_idev)->stats.statname, field, (val)); \ 24813415e46SEric Dumazet mod##SNMP_UPD_PO_STATS((net)->mib.statname##_statistics, field, (val));\ 249edf391ffSNeil Horman }) 250edf391ffSNeil Horman 2511da177e4SLinus Torvalds /* MIBs */ 25214878f75SDavid L Stevens 253087fe240SDenis V. Lunev #define IP6_INC_STATS(net, idev,field) \ 25413415e46SEric Dumazet _DEVINC(net, ipv6, , idev, field) 2551d015503SEric Dumazet #define __IP6_INC_STATS(net, idev,field) \ 25613415e46SEric Dumazet _DEVINC(net, ipv6, __, idev, field) 257edf391ffSNeil Horman #define IP6_ADD_STATS(net, idev,field,val) \ 25813415e46SEric Dumazet _DEVADD(net, ipv6, , idev, field, val) 2591d015503SEric Dumazet #define __IP6_ADD_STATS(net, idev,field,val) \ 26013415e46SEric Dumazet _DEVADD(net, ipv6, __, idev, field, val) 261edf391ffSNeil Horman #define IP6_UPD_PO_STATS(net, idev,field,val) \ 26213415e46SEric Dumazet _DEVUPD(net, ipv6, , idev, field, val) 263c2005eb0SEric Dumazet #define __IP6_UPD_PO_STATS(net, idev,field,val) \ 26413415e46SEric Dumazet _DEVUPD(net, ipv6, __, idev, field, val) 265087fe240SDenis V. Lunev #define ICMP6_INC_STATS(net, idev, field) \ 266be281e55SEric Dumazet _DEVINCATOMIC(net, icmpv6, , idev, field) 267a16292a0SEric Dumazet #define __ICMP6_INC_STATS(net, idev, field) \ 26813415e46SEric Dumazet _DEVINCATOMIC(net, icmpv6, __, idev, field) 26914878f75SDavid L Stevens 270087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS(net, idev, field) \ 2712a24444fSEric Dumazet _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256) 272f3832ed2SEric Dumazet #define ICMP6MSGIN_INC_STATS(net, idev, field) \ 2732a24444fSEric Dumazet _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field) 27414878f75SDavid L Stevens 275fd2c3ef7SEric Dumazet struct ip6_ra_chain { 2761da177e4SLinus Torvalds struct ip6_ra_chain *next; 2771da177e4SLinus Torvalds struct sock *sk; 2781da177e4SLinus Torvalds int sel; 2791da177e4SLinus Torvalds void (*destructor)(struct sock *); 2801da177e4SLinus Torvalds }; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds extern struct ip6_ra_chain *ip6_ra_chain; 2831da177e4SLinus Torvalds extern rwlock_t ip6_ra_lock; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* 2861da177e4SLinus Torvalds This structure is prepared by protocol, when parsing 2871da177e4SLinus Torvalds ancillary data and passed to IPv6. 2881da177e4SLinus Torvalds */ 2891da177e4SLinus Torvalds 290fd2c3ef7SEric Dumazet struct ipv6_txoptions { 2910aeea21aSReshetova, Elena refcount_t refcnt; 2921da177e4SLinus Torvalds /* Length of this structure */ 2931da177e4SLinus Torvalds int tot_len; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* length of extension headers */ 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds __u16 opt_flen; /* after fragment hdr */ 2981da177e4SLinus Torvalds __u16 opt_nflen; /* before fragment hdr */ 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds struct ipv6_opt_hdr *hopopt; 3011da177e4SLinus Torvalds struct ipv6_opt_hdr *dst0opt; 3021da177e4SLinus Torvalds struct ipv6_rt_hdr *srcrt; /* Routing Header */ 3031da177e4SLinus Torvalds struct ipv6_opt_hdr *dst1opt; 30445f6fad8SEric Dumazet struct rcu_head rcu; 3051da177e4SLinus Torvalds /* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */ 3061da177e4SLinus Torvalds }; 3071da177e4SLinus Torvalds 308a346abe0SEric Dumazet /* flowlabel_reflect sysctl values */ 309a346abe0SEric Dumazet enum flowlabel_reflect { 310a346abe0SEric Dumazet FLOWLABEL_REFLECT_ESTABLISHED = 1, 311a346abe0SEric Dumazet FLOWLABEL_REFLECT_TCP_RESET = 2, 312a346abe0SEric Dumazet FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES = 4, 313a346abe0SEric Dumazet }; 314a346abe0SEric Dumazet 315fd2c3ef7SEric Dumazet struct ip6_flowlabel { 3167f0e44acSEric Dumazet struct ip6_flowlabel __rcu *next; 31790bcaf7bSAl Viro __be32 label; 318db3459d1SEric Dumazet atomic_t users; 3191da177e4SLinus Torvalds struct in6_addr dst; 3201da177e4SLinus Torvalds struct ipv6_txoptions *opt; 3211da177e4SLinus Torvalds unsigned long linger; 322d3aedd5eSYOSHIFUJI Hideaki / 吉藤英明 struct rcu_head rcu; 3231da177e4SLinus Torvalds u8 share; 3244f82f457SEric W. Biederman union { 3254f82f457SEric W. Biederman struct pid *pid; 3264f82f457SEric W. Biederman kuid_t uid; 3274f82f457SEric W. Biederman } owner; 3281da177e4SLinus Torvalds unsigned long lastuse; 3291da177e4SLinus Torvalds unsigned long expires; 33060e8fbc4SBenjamin Thery struct net *fl_net; 3311da177e4SLinus Torvalds }; 3321da177e4SLinus Torvalds 333f3a7c66bSHarvey Harrison #define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) 334f3a7c66bSHarvey Harrison #define IPV6_FLOWLABEL_MASK cpu_to_be32(0x000FFFFF) 33582a584b7STom Herbert #define IPV6_FLOWLABEL_STATELESS_FLAG cpu_to_be32(0x00080000) 33682a584b7STom Herbert 33737cfee90SFlorent Fourcot #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) 338d76ed22bSLi RongQing #define IPV6_TCLASS_SHIFT 20 3391da177e4SLinus Torvalds 340fd2c3ef7SEric Dumazet struct ipv6_fl_socklist { 3417f0e44acSEric Dumazet struct ipv6_fl_socklist __rcu *next; 3421da177e4SLinus Torvalds struct ip6_flowlabel *fl; 34318367681SYOSHIFUJI Hideaki / 吉藤英明 struct rcu_head rcu; 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 34626879da5SWei Wang struct ipcm6_cookie { 3475fdaa88dSWillem de Bruijn struct sockcm_cookie sockc; 34826879da5SWei Wang __s16 hlimit; 34926879da5SWei Wang __s16 tclass; 3501b31debcSEric Dumazet __u16 gso_size; 35126879da5SWei Wang __s8 dontfrag; 35226879da5SWei Wang struct ipv6_txoptions *opt; 35326879da5SWei Wang }; 35426879da5SWei Wang 355b515430aSWillem de Bruijn static inline void ipcm6_init(struct ipcm6_cookie *ipc6) 356b515430aSWillem de Bruijn { 357b515430aSWillem de Bruijn *ipc6 = (struct ipcm6_cookie) { 358b515430aSWillem de Bruijn .hlimit = -1, 359b515430aSWillem de Bruijn .tclass = -1, 360b515430aSWillem de Bruijn .dontfrag = -1, 361b515430aSWillem de Bruijn }; 362b515430aSWillem de Bruijn } 363b515430aSWillem de Bruijn 364b515430aSWillem de Bruijn static inline void ipcm6_init_sk(struct ipcm6_cookie *ipc6, 365b515430aSWillem de Bruijn const struct ipv6_pinfo *np) 366b515430aSWillem de Bruijn { 367b515430aSWillem de Bruijn *ipc6 = (struct ipcm6_cookie) { 368b515430aSWillem de Bruijn .hlimit = -1, 369b515430aSWillem de Bruijn .tclass = np->tclass, 370b515430aSWillem de Bruijn .dontfrag = np->dontfrag, 371b515430aSWillem de Bruijn }; 372b515430aSWillem de Bruijn } 373b515430aSWillem de Bruijn 37445f6fad8SEric Dumazet static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np) 37545f6fad8SEric Dumazet { 37645f6fad8SEric Dumazet struct ipv6_txoptions *opt; 37745f6fad8SEric Dumazet 37845f6fad8SEric Dumazet rcu_read_lock(); 37945f6fad8SEric Dumazet opt = rcu_dereference(np->opt); 380e550785cSBenjamin Poirier if (opt) { 3810aeea21aSReshetova, Elena if (!refcount_inc_not_zero(&opt->refcnt)) 38245f6fad8SEric Dumazet opt = NULL; 383e550785cSBenjamin Poirier else 384e550785cSBenjamin Poirier opt = rcu_pointer_handoff(opt); 385e550785cSBenjamin Poirier } 38645f6fad8SEric Dumazet rcu_read_unlock(); 38745f6fad8SEric Dumazet return opt; 38845f6fad8SEric Dumazet } 38945f6fad8SEric Dumazet 39045f6fad8SEric Dumazet static inline void txopt_put(struct ipv6_txoptions *opt) 39145f6fad8SEric Dumazet { 3920aeea21aSReshetova, Elena if (opt && refcount_dec_and_test(&opt->refcnt)) 39345f6fad8SEric Dumazet kfree_rcu(opt, rcu); 39445f6fad8SEric Dumazet } 39545f6fad8SEric Dumazet 39659c820b2SWillem de Bruijn struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label); 39759c820b2SWillem de Bruijn 39859c820b2SWillem de Bruijn extern struct static_key_false_deferred ipv6_flowlabel_exclusive; 39959c820b2SWillem de Bruijn static inline struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk, 40059c820b2SWillem de Bruijn __be32 label) 40159c820b2SWillem de Bruijn { 40259c820b2SWillem de Bruijn if (static_branch_unlikely(&ipv6_flowlabel_exclusive.key)) 40359c820b2SWillem de Bruijn return __fl6_sock_lookup(sk, label) ? : ERR_PTR(-ENOENT); 40459c820b2SWillem de Bruijn 40559c820b2SWillem de Bruijn return NULL; 40659c820b2SWillem de Bruijn } 40759c820b2SWillem de Bruijn 4085c3a0fd7SJoe Perches struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space, 4091da177e4SLinus Torvalds struct ip6_flowlabel *fl, 4101da177e4SLinus Torvalds struct ipv6_txoptions *fopt); 4115c3a0fd7SJoe Perches void fl6_free_socklist(struct sock *sk); 41286298285SChristoph Hellwig int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen); 41346e5f401SFlorent Fourcot int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq, 41446e5f401SFlorent Fourcot int flags); 4155c3a0fd7SJoe Perches int ip6_flowlabel_init(void); 4165c3a0fd7SJoe Perches void ip6_flowlabel_cleanup(void); 417e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds static inline void fl6_sock_release(struct ip6_flowlabel *fl) 4201da177e4SLinus Torvalds { 4211da177e4SLinus Torvalds if (fl) 4221da177e4SLinus Torvalds atomic_dec(&fl->users); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4255c3a0fd7SJoe Perches void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info); 426b94f1c09SDavid S. Miller 4274e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 4286d0bfe22SLorenzo Colitti struct icmp6hdr *thdr, int len); 4296d0bfe22SLorenzo Colitti 4305c3a0fd7SJoe Perches int ip6_ra_control(struct sock *sk, int sel); 4311da177e4SLinus Torvalds 4325c3a0fd7SJoe Perches int ipv6_parse_hopopts(struct sk_buff *skb); 4331da177e4SLinus Torvalds 4345c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_dup_options(struct sock *sk, 4355c3a0fd7SJoe Perches struct ipv6_txoptions *opt); 4365c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_renew_options(struct sock *sk, 4375c3a0fd7SJoe Perches struct ipv6_txoptions *opt, 438333fad53SYOSHIFUJI Hideaki int newtype, 439a9ba23d4SPaul Moore struct ipv6_opt_hdr *newopt); 440df9890c3SYOSHIFUJI Hideaki struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, 441df9890c3SYOSHIFUJI Hideaki struct ipv6_txoptions *opt); 4421da177e4SLinus Torvalds 443a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 444a224772dSEric Dumazet const struct inet6_skb_parm *opt); 445ceba1832SHuw Davies struct ipv6_txoptions *ipv6_update_options(struct sock *sk, 446ceba1832SHuw Davies struct ipv6_txoptions *opt); 447399c07deSArnaldo Carvalho de Melo 448aeaf6e9dSShmulik Ladkani static inline bool ipv6_accept_ra(struct inet6_dev *idev) 449aeaf6e9dSShmulik Ladkani { 450aeaf6e9dSShmulik Ladkani /* If forwarding is enabled, RA are not accepted unless the special 451aeaf6e9dSShmulik Ladkani * hybrid mode (accept_ra=2) is enabled. 452aeaf6e9dSShmulik Ladkani */ 453aeaf6e9dSShmulik Ladkani return idev->cnf.forwarding ? idev->cnf.accept_ra == 2 : 454aeaf6e9dSShmulik Ladkani idev->cnf.accept_ra; 455aeaf6e9dSShmulik Ladkani } 456aeaf6e9dSShmulik Ladkani 457c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_HIGH_THRESH (4 * 1024*1024) /* 4194304 */ 458c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_LOW_THRESH (3 * 1024*1024) /* 3145728 */ 4591da177e4SLinus Torvalds #define IPV6_FRAG_TIMEOUT (60 * HZ) /* 60 seconds */ 4601da177e4SLinus Torvalds 4615c3a0fd7SJoe Perches int __ipv6_addr_type(const struct in6_addr *addr); 462b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_type(const struct in6_addr *addr) 463b1cacb68SYOSHIFUJI Hideaki { 464b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_type(addr) & 0xffff; 465b1cacb68SYOSHIFUJI Hideaki } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds static inline int ipv6_addr_scope(const struct in6_addr *addr) 4681da177e4SLinus Torvalds { 469b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK; 470b1cacb68SYOSHIFUJI Hideaki } 471b1cacb68SYOSHIFUJI Hideaki 472b1cacb68SYOSHIFUJI Hideaki static inline int __ipv6_addr_src_scope(int type) 473b1cacb68SYOSHIFUJI Hideaki { 474a02cec21SEric Dumazet return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16); 475b1cacb68SYOSHIFUJI Hideaki } 476b1cacb68SYOSHIFUJI Hideaki 477b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_src_scope(const struct in6_addr *addr) 478b1cacb68SYOSHIFUJI Hideaki { 479b1cacb68SYOSHIFUJI Hideaki return __ipv6_addr_src_scope(__ipv6_addr_type(addr)); 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 482b7ef213eSHannes Frederic Sowa static inline bool __ipv6_addr_needs_scope_id(int type) 483b7ef213eSHannes Frederic Sowa { 484b7ef213eSHannes Frederic Sowa return type & IPV6_ADDR_LINKLOCAL || 485b7ef213eSHannes Frederic Sowa (type & IPV6_ADDR_MULTICAST && 486b7ef213eSHannes Frederic Sowa (type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL))); 487b7ef213eSHannes Frederic Sowa } 488b7ef213eSHannes Frederic Sowa 489b7ef213eSHannes Frederic Sowa static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface) 490b7ef213eSHannes Frederic Sowa { 491b7ef213eSHannes Frederic Sowa return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0; 492b7ef213eSHannes Frederic Sowa } 493b7ef213eSHannes Frederic Sowa 4941da177e4SLinus Torvalds static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2) 4951da177e4SLinus Torvalds { 496db3459d1SEric Dumazet return memcmp(a1, a2, sizeof(struct in6_addr)); 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991a203cb3SEric Dumazet static inline bool 500f2ffd9eeSPatrick McHardy ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m, 501f2ffd9eeSPatrick McHardy const struct in6_addr *a2) 502f2ffd9eeSPatrick McHardy { 5031a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 5041a203cb3SEric Dumazet const unsigned long *ul1 = (const unsigned long *)a1; 5051a203cb3SEric Dumazet const unsigned long *ulm = (const unsigned long *)m; 5061a203cb3SEric Dumazet const unsigned long *ul2 = (const unsigned long *)a2; 5071a203cb3SEric Dumazet 5081a203cb3SEric Dumazet return !!(((ul1[0] ^ ul2[0]) & ulm[0]) | 5091a203cb3SEric Dumazet ((ul1[1] ^ ul2[1]) & ulm[1])); 5101a203cb3SEric Dumazet #else 511a02cec21SEric Dumazet return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) | 512fed85383SYOSHIFUJI Hideaki ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) | 513fed85383SYOSHIFUJI Hideaki ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) | 514a02cec21SEric Dumazet ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3])); 5151a203cb3SEric Dumazet #endif 516f2ffd9eeSPatrick McHardy } 517f2ffd9eeSPatrick McHardy 5181da177e4SLinus Torvalds static inline void ipv6_addr_prefix(struct in6_addr *pfx, 5191da177e4SLinus Torvalds const struct in6_addr *addr, 5201da177e4SLinus Torvalds int plen) 5211da177e4SLinus Torvalds { 5221da177e4SLinus Torvalds /* caller must guarantee 0 <= plen <= 128 */ 5231da177e4SLinus Torvalds int o = plen >> 3, 5241da177e4SLinus Torvalds b = plen & 0x7; 5251da177e4SLinus Torvalds 526db3459d1SEric Dumazet memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr)); 5271da177e4SLinus Torvalds memcpy(pfx->s6_addr, addr, o); 528db3459d1SEric Dumazet if (b != 0) 5291da177e4SLinus Torvalds pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b); 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 532818f1f3eSAlexander Aring static inline void ipv6_addr_prefix_copy(struct in6_addr *addr, 533818f1f3eSAlexander Aring const struct in6_addr *pfx, 534818f1f3eSAlexander Aring int plen) 535818f1f3eSAlexander Aring { 536818f1f3eSAlexander Aring /* caller must guarantee 0 <= plen <= 128 */ 537818f1f3eSAlexander Aring int o = plen >> 3, 538818f1f3eSAlexander Aring b = plen & 0x7; 539818f1f3eSAlexander Aring 540818f1f3eSAlexander Aring memcpy(addr->s6_addr, pfx, o); 541818f1f3eSAlexander Aring if (b != 0) { 542818f1f3eSAlexander Aring addr->s6_addr[o] &= ~(0xff00 >> b); 543818f1f3eSAlexander Aring addr->s6_addr[o] |= (pfx->s6_addr[o] & (0xff00 >> b)); 544818f1f3eSAlexander Aring } 545818f1f3eSAlexander Aring } 546818f1f3eSAlexander Aring 5475206c579SYOSHIFUJI Hideaki / 吉藤英明 static inline void __ipv6_addr_set_half(__be32 *addr, 5485206c579SYOSHIFUJI Hideaki / 吉藤英明 __be32 wh, __be32 wl) 5495206c579SYOSHIFUJI Hideaki / 吉藤英明 { 5505206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 5515206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(__BIG_ENDIAN) 5525206c579SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) { 5535206c579SYOSHIFUJI Hideaki / 吉藤英明 *(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl)); 5545206c579SYOSHIFUJI Hideaki / 吉藤英明 return; 5555206c579SYOSHIFUJI Hideaki / 吉藤英明 } 5565206c579SYOSHIFUJI Hideaki / 吉藤英明 #elif defined(__LITTLE_ENDIAN) 5575206c579SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) { 5585206c579SYOSHIFUJI Hideaki / 吉藤英明 *(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh)); 5595206c579SYOSHIFUJI Hideaki / 吉藤英明 return; 5605206c579SYOSHIFUJI Hideaki / 吉藤英明 } 5615206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif 5625206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif 5635206c579SYOSHIFUJI Hideaki / 吉藤英明 addr[0] = wh; 5645206c579SYOSHIFUJI Hideaki / 吉藤英明 addr[1] = wl; 5655206c579SYOSHIFUJI Hideaki / 吉藤英明 } 5665206c579SYOSHIFUJI Hideaki / 吉藤英明 5671da177e4SLinus Torvalds static inline void ipv6_addr_set(struct in6_addr *addr, 56848818f82SAl Viro __be32 w1, __be32 w2, 56948818f82SAl Viro __be32 w3, __be32 w4) 5701da177e4SLinus Torvalds { 5715206c579SYOSHIFUJI Hideaki / 吉藤英明 __ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2); 5725206c579SYOSHIFUJI Hideaki / 吉藤英明 __ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4); 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 57592113bfdSEric Dumazet static inline bool ipv6_addr_equal(const struct in6_addr *a1, 5761da177e4SLinus Torvalds const struct in6_addr *a2) 5771da177e4SLinus Torvalds { 5781a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 5791a203cb3SEric Dumazet const unsigned long *ul1 = (const unsigned long *)a1; 5801a203cb3SEric Dumazet const unsigned long *ul2 = (const unsigned long *)a2; 5811a203cb3SEric Dumazet 5821a203cb3SEric Dumazet return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL; 5831a203cb3SEric Dumazet #else 584a02cec21SEric Dumazet return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) | 585fed85383SYOSHIFUJI Hideaki (a1->s6_addr32[1] ^ a2->s6_addr32[1]) | 586fed85383SYOSHIFUJI Hideaki (a1->s6_addr32[2] ^ a2->s6_addr32[2]) | 587a02cec21SEric Dumazet (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0; 5881a203cb3SEric Dumazet #endif 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 59138675170SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 59238675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool __ipv6_prefix_equal64_half(const __be64 *a1, 59338675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a2, 59438675170SYOSHIFUJI Hideaki / 吉藤英明 unsigned int len) 59538675170SYOSHIFUJI Hideaki / 吉藤英明 { 596512613d7SFabio Baltieri if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len)))) 59738675170SYOSHIFUJI Hideaki / 吉藤英明 return false; 59838675170SYOSHIFUJI Hideaki / 吉藤英明 return true; 59938675170SYOSHIFUJI Hideaki / 吉藤英明 } 60038675170SYOSHIFUJI Hideaki / 吉藤英明 60138675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1, 60238675170SYOSHIFUJI Hideaki / 吉藤英明 const struct in6_addr *addr2, 60338675170SYOSHIFUJI Hideaki / 吉藤英明 unsigned int prefixlen) 60438675170SYOSHIFUJI Hideaki / 吉藤英明 { 60538675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a1 = (const __be64 *)addr1; 60638675170SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a2 = (const __be64 *)addr2; 60738675170SYOSHIFUJI Hideaki / 吉藤英明 60838675170SYOSHIFUJI Hideaki / 吉藤英明 if (prefixlen >= 64) { 60938675170SYOSHIFUJI Hideaki / 吉藤英明 if (a1[0] ^ a2[0]) 61038675170SYOSHIFUJI Hideaki / 吉藤英明 return false; 61138675170SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64); 61238675170SYOSHIFUJI Hideaki / 吉藤英明 } 61338675170SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_prefix_equal64_half(a1, a2, prefixlen); 61438675170SYOSHIFUJI Hideaki / 吉藤英明 } 61538675170SYOSHIFUJI Hideaki / 吉藤英明 #else 6162ef97332SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1, 6172ef97332SYOSHIFUJI Hideaki / 吉藤英明 const struct in6_addr *addr2, 6181da177e4SLinus Torvalds unsigned int prefixlen) 6191da177e4SLinus Torvalds { 6202ef97332SYOSHIFUJI Hideaki / 吉藤英明 const __be32 *a1 = addr1->s6_addr32; 6212ef97332SYOSHIFUJI Hideaki / 吉藤英明 const __be32 *a2 = addr2->s6_addr32; 62295c96174SEric Dumazet unsigned int pdw, pbi; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* check complete u32 in prefix */ 6251da177e4SLinus Torvalds pdw = prefixlen >> 5; 6261da177e4SLinus Torvalds if (pdw && memcmp(a1, a2, pdw << 2)) 62792113bfdSEric Dumazet return false; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* check incomplete u32 in prefix */ 6301da177e4SLinus Torvalds pbi = prefixlen & 0x1f; 6311da177e4SLinus Torvalds if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi)))) 63292113bfdSEric Dumazet return false; 6331da177e4SLinus Torvalds 63492113bfdSEric Dumazet return true; 6351da177e4SLinus Torvalds } 63638675170SYOSHIFUJI Hideaki / 吉藤英明 #endif 6371da177e4SLinus Torvalds 63892113bfdSEric Dumazet static inline bool ipv6_addr_any(const struct in6_addr *a) 6391da177e4SLinus Torvalds { 6401a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 6411a203cb3SEric Dumazet const unsigned long *ul = (const unsigned long *)a; 6421a203cb3SEric Dumazet 6431a203cb3SEric Dumazet return (ul[0] | ul[1]) == 0UL; 6441a203cb3SEric Dumazet #else 645a02cec21SEric Dumazet return (a->s6_addr32[0] | a->s6_addr32[1] | 646a02cec21SEric Dumazet a->s6_addr32[2] | a->s6_addr32[3]) == 0; 6471a203cb3SEric Dumazet #endif 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650ddbe5032SEric Dumazet static inline u32 ipv6_addr_hash(const struct in6_addr *a) 651ddbe5032SEric Dumazet { 652ddbe5032SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 653ddbe5032SEric Dumazet const unsigned long *ul = (const unsigned long *)a; 654ddbe5032SEric Dumazet unsigned long x = ul[0] ^ ul[1]; 655ddbe5032SEric Dumazet 656ddbe5032SEric Dumazet return (u32)(x ^ (x >> 32)); 657ddbe5032SEric Dumazet #else 658ddbe5032SEric Dumazet return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^ 659ddbe5032SEric Dumazet a->s6_addr32[2] ^ a->s6_addr32[3]); 660ddbe5032SEric Dumazet #endif 661ddbe5032SEric Dumazet } 662ddbe5032SEric Dumazet 66308dcdbf6SEric Dumazet /* more secured version of ipv6_addr_hash() */ 664b50026b5SHannes Frederic Sowa static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval) 66508dcdbf6SEric Dumazet { 66608dcdbf6SEric Dumazet u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1]; 66708dcdbf6SEric Dumazet 66808dcdbf6SEric Dumazet return jhash_3words(v, 66908dcdbf6SEric Dumazet (__force u32)a->s6_addr32[2], 67008dcdbf6SEric Dumazet (__force u32)a->s6_addr32[3], 671b50026b5SHannes Frederic Sowa initval); 67208dcdbf6SEric Dumazet } 67308dcdbf6SEric Dumazet 67492113bfdSEric Dumazet static inline bool ipv6_addr_loopback(const struct in6_addr *a) 675f630e43aSYOSHIFUJI Hideaki { 676e287656bSYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 6771373a773SJeff Layton const __be64 *be = (const __be64 *)a; 678e287656bSYOSHIFUJI Hideaki / 吉藤英明 6791373a773SJeff Layton return (be[0] | (be[1] ^ cpu_to_be64(1))) == 0UL; 680e287656bSYOSHIFUJI Hideaki / 吉藤英明 #else 681a02cec21SEric Dumazet return (a->s6_addr32[0] | a->s6_addr32[1] | 6821373a773SJeff Layton a->s6_addr32[2] | (a->s6_addr32[3] ^ cpu_to_be32(1))) == 0; 683e287656bSYOSHIFUJI Hideaki / 吉藤英明 #endif 684f630e43aSYOSHIFUJI Hideaki } 685f630e43aSYOSHIFUJI Hideaki 6861373a773SJeff Layton /* 6871373a773SJeff Layton * Note that we must __force cast these to unsigned long to make sparse happy, 6881373a773SJeff Layton * since all of the endian-annotated types are fixed size regardless of arch. 6891373a773SJeff Layton */ 69092113bfdSEric Dumazet static inline bool ipv6_addr_v4mapped(const struct in6_addr *a) 691e773e4faSBrian Haley { 692a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 return ( 693a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 6941373a773SJeff Layton *(unsigned long *)a | 695a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #else 6961373a773SJeff Layton (__force unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) | 697a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #endif 6981373a773SJeff Layton (__force unsigned long)(a->s6_addr32[2] ^ 6991373a773SJeff Layton cpu_to_be32(0x0000ffff))) == 0UL; 700e773e4faSBrian Haley } 701e773e4faSBrian Haley 702be2644aaSEric Dumazet static inline bool ipv6_addr_v4mapped_loopback(const struct in6_addr *a) 703be2644aaSEric Dumazet { 704be2644aaSEric Dumazet return ipv6_addr_v4mapped(a) && ipv4_is_loopback(a->s6_addr32[3]); 705be2644aaSEric Dumazet } 706be2644aaSEric Dumazet 707f0b1e64cSMartin KaFai Lau static inline u32 ipv6_portaddr_hash(const struct net *net, 708f0b1e64cSMartin KaFai Lau const struct in6_addr *addr6, 709f0b1e64cSMartin KaFai Lau unsigned int port) 710f0b1e64cSMartin KaFai Lau { 711f0b1e64cSMartin KaFai Lau unsigned int hash, mix = net_hash_mix(net); 712f0b1e64cSMartin KaFai Lau 713f0b1e64cSMartin KaFai Lau if (ipv6_addr_any(addr6)) 714f0b1e64cSMartin KaFai Lau hash = jhash_1word(0, mix); 715f0b1e64cSMartin KaFai Lau else if (ipv6_addr_v4mapped(addr6)) 716f0b1e64cSMartin KaFai Lau hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix); 717f0b1e64cSMartin KaFai Lau else 718f0b1e64cSMartin KaFai Lau hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix); 719f0b1e64cSMartin KaFai Lau 720f0b1e64cSMartin KaFai Lau return hash ^ port; 721f0b1e64cSMartin KaFai Lau } 722f0b1e64cSMartin KaFai Lau 7231da177e4SLinus Torvalds /* 72499cd07a5SJuha-Matti Tapio * Check for a RFC 4843 ORCHID address 72599cd07a5SJuha-Matti Tapio * (Overlay Routable Cryptographic Hash Identifiers) 72699cd07a5SJuha-Matti Tapio */ 72792113bfdSEric Dumazet static inline bool ipv6_addr_orchid(const struct in6_addr *a) 72899cd07a5SJuha-Matti Tapio { 729a02cec21SEric Dumazet return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010); 73099cd07a5SJuha-Matti Tapio } 73199cd07a5SJuha-Matti Tapio 7325c98631cSLorenzo Colitti static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr) 7335c98631cSLorenzo Colitti { 7345c98631cSLorenzo Colitti return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000); 7355c98631cSLorenzo Colitti } 7365c98631cSLorenzo Colitti 737f15364bdSAurélien Charbon static inline void ipv6_addr_set_v4mapped(const __be32 addr, 738f15364bdSAurélien Charbon struct in6_addr *v4mapped) 739f15364bdSAurélien Charbon { 740f15364bdSAurélien Charbon ipv6_addr_set(v4mapped, 741f15364bdSAurélien Charbon 0, 0, 742f15364bdSAurélien Charbon htonl(0x0000FFFF), 743f15364bdSAurélien Charbon addr); 744f15364bdSAurélien Charbon } 745f15364bdSAurélien Charbon 74699cd07a5SJuha-Matti Tapio /* 747971f359dSYOSHIFUJI Hideaki * find the first different bit between two addresses 748971f359dSYOSHIFUJI Hideaki * length of address must be a multiple of 32bits 749971f359dSYOSHIFUJI Hideaki */ 7509f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen) 751971f359dSYOSHIFUJI Hideaki { 752ef296f56SAl Viro const __be32 *a1 = token1, *a2 = token2; 753971f359dSYOSHIFUJI Hideaki int i; 754971f359dSYOSHIFUJI Hideaki 755971f359dSYOSHIFUJI Hideaki addrlen >>= 2; 756971f359dSYOSHIFUJI Hideaki 757971f359dSYOSHIFUJI Hideaki for (i = 0; i < addrlen; i++) { 758ef296f56SAl Viro __be32 xb = a1[i] ^ a2[i]; 759ef296f56SAl Viro if (xb) 760d57b8fb8SYOSHIFUJI Hideaki / 吉藤英明 return i * 32 + 31 - __fls(ntohl(xb)); 761971f359dSYOSHIFUJI Hideaki } 762971f359dSYOSHIFUJI Hideaki 763971f359dSYOSHIFUJI Hideaki /* 764971f359dSYOSHIFUJI Hideaki * we should *never* get to this point since that 765971f359dSYOSHIFUJI Hideaki * would mean the addrs are equal 766971f359dSYOSHIFUJI Hideaki * 767971f359dSYOSHIFUJI Hideaki * However, we do get to it 8) And exacly, when 768971f359dSYOSHIFUJI Hideaki * addresses are equal 8) 769971f359dSYOSHIFUJI Hideaki * 770971f359dSYOSHIFUJI Hideaki * ip route add 1111::/128 via ... 771971f359dSYOSHIFUJI Hideaki * ip route add 1111::/64 via ... 772971f359dSYOSHIFUJI Hideaki * and we are here. 773971f359dSYOSHIFUJI Hideaki * 774971f359dSYOSHIFUJI Hideaki * Ideally, this function should stop comparison 775971f359dSYOSHIFUJI Hideaki * at prefix length. It does not, but it is still OK, 776971f359dSYOSHIFUJI Hideaki * if returned value is greater than prefix length. 777971f359dSYOSHIFUJI Hideaki * --ANK (980803) 778971f359dSYOSHIFUJI Hideaki */ 779a02cec21SEric Dumazet return addrlen << 5; 780971f359dSYOSHIFUJI Hideaki } 781971f359dSYOSHIFUJI Hideaki 7829f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 7839f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen) 7849f2e7334SYOSHIFUJI Hideaki / 吉藤英明 { 7859f2e7334SYOSHIFUJI Hideaki / 吉藤英明 const __be64 *a1 = token1, *a2 = token2; 7869f2e7334SYOSHIFUJI Hideaki / 吉藤英明 int i; 7879f2e7334SYOSHIFUJI Hideaki / 吉藤英明 7889f2e7334SYOSHIFUJI Hideaki / 吉藤英明 addrlen >>= 3; 7899f2e7334SYOSHIFUJI Hideaki / 吉藤英明 7909f2e7334SYOSHIFUJI Hideaki / 吉藤英明 for (i = 0; i < addrlen; i++) { 7919f2e7334SYOSHIFUJI Hideaki / 吉藤英明 __be64 xb = a1[i] ^ a2[i]; 7929f2e7334SYOSHIFUJI Hideaki / 吉藤英明 if (xb) 7939f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return i * 64 + 63 - __fls(be64_to_cpu(xb)); 7949f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 7959f2e7334SYOSHIFUJI Hideaki / 吉藤英明 7969f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return addrlen << 6; 7979f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 7989f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif 7999f2e7334SYOSHIFUJI Hideaki / 吉藤英明 8009f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen) 8019f2e7334SYOSHIFUJI Hideaki / 吉藤英明 { 8029f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 8039f2e7334SYOSHIFUJI Hideaki / 吉藤英明 if (__builtin_constant_p(addrlen) && !(addrlen & 7)) 8049f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_addr_diff64(token1, token2, addrlen); 8059f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif 8069f2e7334SYOSHIFUJI Hideaki / 吉藤英明 return __ipv6_addr_diff32(token1, token2, addrlen); 8079f2e7334SYOSHIFUJI Hideaki / 吉藤英明 } 8089f2e7334SYOSHIFUJI Hideaki / 吉藤英明 809971f359dSYOSHIFUJI Hideaki static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2) 810971f359dSYOSHIFUJI Hideaki { 811971f359dSYOSHIFUJI Hideaki return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr)); 812971f359dSYOSHIFUJI Hideaki } 813971f359dSYOSHIFUJI Hideaki 8147f159867SEric Dumazet __be32 ipv6_select_ident(struct net *net, 815fd0273d7SMartin KaFai Lau const struct in6_addr *daddr, 816fd0273d7SMartin KaFai Lau const struct in6_addr *saddr); 8170c19f846SWillem de Bruijn __be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb); 8185188cd44SBen Hutchings 8195c3a0fd7SJoe Perches int ip6_dst_hoplimit(struct dst_entry *dst); 8203ce9b35fSCong Wang 8215c98631cSLorenzo Colitti static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6, 8225c98631cSLorenzo Colitti struct dst_entry *dst) 8235c98631cSLorenzo Colitti { 8245c98631cSLorenzo Colitti int hlimit; 8255c98631cSLorenzo Colitti 8265c98631cSLorenzo Colitti if (ipv6_addr_is_multicast(&fl6->daddr)) 8275c98631cSLorenzo Colitti hlimit = np->mcast_hops; 8285c98631cSLorenzo Colitti else 8295c98631cSLorenzo Colitti hlimit = np->hop_limit; 8305c98631cSLorenzo Colitti if (hlimit < 0) 8315c98631cSLorenzo Colitti hlimit = ip6_dst_hoplimit(dst); 8325c98631cSLorenzo Colitti return hlimit; 8335c98631cSLorenzo Colitti } 8345c98631cSLorenzo Colitti 835c3f83241STom Herbert /* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store 836c3f83241STom Herbert * Equivalent to : flow->v6addrs.src = iph->saddr; 837c3f83241STom Herbert * flow->v6addrs.dst = iph->daddr; 838c3f83241STom Herbert */ 839c3f83241STom Herbert static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow, 840c3f83241STom Herbert const struct ipv6hdr *iph) 841c3f83241STom Herbert { 842c3f83241STom Herbert BUILD_BUG_ON(offsetof(typeof(flow->addrs), v6addrs.dst) != 843c3f83241STom Herbert offsetof(typeof(flow->addrs), v6addrs.src) + 844c3f83241STom Herbert sizeof(flow->addrs.v6addrs.src)); 845c3f83241STom Herbert memcpy(&flow->addrs.v6addrs, &iph->saddr, sizeof(flow->addrs.v6addrs)); 846c3f83241STom Herbert flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 847c3f83241STom Herbert } 848c3f83241STom Herbert 849a37934fcSFlorian Fainelli #if IS_ENABLED(CONFIG_IPV6) 85042240901STom Herbert 851db57dc7cSVincent Bernat static inline bool ipv6_can_nonlocal_bind(struct net *net, 852db57dc7cSVincent Bernat struct inet_sock *inet) 853db57dc7cSVincent Bernat { 854db57dc7cSVincent Bernat return net->ipv6.sysctl.ip_nonlocal_bind || 855db57dc7cSVincent Bernat inet->freebind || inet->transparent; 856db57dc7cSVincent Bernat } 857db57dc7cSVincent Bernat 85842240901STom Herbert /* Sysctl settings for net ipv6.auto_flowlabels */ 85942240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OFF 0 86042240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OPTOUT 1 86142240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OPTIN 2 86242240901STom Herbert #define IP6_AUTO_FLOW_LABEL_FORCED 3 86342240901STom Herbert 86442240901STom Herbert #define IP6_AUTO_FLOW_LABEL_MAX IP6_AUTO_FLOW_LABEL_FORCED 86542240901STom Herbert 866b5677416STom Herbert #define IP6_DEFAULT_AUTO_FLOW_LABELS IP6_AUTO_FLOW_LABEL_OPTOUT 86742240901STom Herbert 868cb1ce2efSTom Herbert static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb, 86967800f9bSTom Herbert __be32 flowlabel, bool autolabel, 87067800f9bSTom Herbert struct flowi6 *fl6) 871cb1ce2efSTom Herbert { 87267765146SEric Dumazet u32 hash; 873cb1ce2efSTom Herbert 87490427ef5SDimitris Michailidis /* @flowlabel may include more than a flow label, eg, the traffic class. 87590427ef5SDimitris Michailidis * Here we want only the flow label value. 87690427ef5SDimitris Michailidis */ 87790427ef5SDimitris Michailidis flowlabel &= IPV6_FLOWLABEL_MASK; 87890427ef5SDimitris Michailidis 87942240901STom Herbert if (flowlabel || 88042240901STom Herbert net->ipv6.sysctl.auto_flowlabels == IP6_AUTO_FLOW_LABEL_OFF || 88142240901STom Herbert (!autolabel && 88242240901STom Herbert net->ipv6.sysctl.auto_flowlabels != IP6_AUTO_FLOW_LABEL_FORCED)) 88342240901STom Herbert return flowlabel; 88442240901STom Herbert 88567800f9bSTom Herbert hash = skb_get_hash_flowi6(skb, fl6); 886cb1ce2efSTom Herbert 887cb1ce2efSTom Herbert /* Since this is being sent on the wire obfuscate hash a bit 888cb1ce2efSTom Herbert * to minimize possbility that any useful information to an 889cb1ce2efSTom Herbert * attacker is leaked. Only lower 20 bits are relevant. 890cb1ce2efSTom Herbert */ 891169dc027SColin Ian King hash = rol32(hash, 16); 892cb1ce2efSTom Herbert 89367765146SEric Dumazet flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK; 89482a584b7STom Herbert 89582a584b7STom Herbert if (net->ipv6.sysctl.flowlabel_state_ranges) 89682a584b7STom Herbert flowlabel |= IPV6_FLOWLABEL_STATELESS_FLAG; 897cb1ce2efSTom Herbert 898cb1ce2efSTom Herbert return flowlabel; 899cb1ce2efSTom Herbert } 90042240901STom Herbert 90142240901STom Herbert static inline int ip6_default_np_autolabel(struct net *net) 90242240901STom Herbert { 90342240901STom Herbert switch (net->ipv6.sysctl.auto_flowlabels) { 90442240901STom Herbert case IP6_AUTO_FLOW_LABEL_OFF: 90542240901STom Herbert case IP6_AUTO_FLOW_LABEL_OPTIN: 90642240901STom Herbert default: 90742240901STom Herbert return 0; 90842240901STom Herbert case IP6_AUTO_FLOW_LABEL_OPTOUT: 90942240901STom Herbert case IP6_AUTO_FLOW_LABEL_FORCED: 91042240901STom Herbert return 1; 91142240901STom Herbert } 91242240901STom Herbert } 913a37934fcSFlorian Fainelli #else 914a37934fcSFlorian Fainelli static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb, 91542240901STom Herbert __be32 flowlabel, bool autolabel, 91642240901STom Herbert struct flowi6 *fl6) 917a37934fcSFlorian Fainelli { 918a37934fcSFlorian Fainelli return flowlabel; 919a37934fcSFlorian Fainelli } 92042240901STom Herbert static inline int ip6_default_np_autolabel(struct net *net) 92142240901STom Herbert { 92242240901STom Herbert return 0; 92342240901STom Herbert } 924a37934fcSFlorian Fainelli #endif 925a37934fcSFlorian Fainelli 926918ee507SPetr Machata #if IS_ENABLED(CONFIG_IPV6) 927918ee507SPetr Machata static inline int ip6_multipath_hash_policy(const struct net *net) 928918ee507SPetr Machata { 929918ee507SPetr Machata return net->ipv6.sysctl.multipath_hash_policy; 930918ee507SPetr Machata } 931ed13923fSIdo Schimmel static inline u32 ip6_multipath_hash_fields(const struct net *net) 932ed13923fSIdo Schimmel { 933ed13923fSIdo Schimmel return net->ipv6.sysctl.multipath_hash_fields; 934ed13923fSIdo Schimmel } 935918ee507SPetr Machata #else 936918ee507SPetr Machata static inline int ip6_multipath_hash_policy(const struct net *net) 937918ee507SPetr Machata { 938918ee507SPetr Machata return 0; 939918ee507SPetr Machata } 940ed13923fSIdo Schimmel static inline u32 ip6_multipath_hash_fields(const struct net *net) 941ed13923fSIdo Schimmel { 942ed13923fSIdo Schimmel return 0; 943ed13923fSIdo Schimmel } 944918ee507SPetr Machata #endif 945cb1ce2efSTom Herbert 946971f359dSYOSHIFUJI Hideaki /* 9473e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 * Header manipulation 9483e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 */ 9493e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass, 9503e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 __be32 flowlabel) 9513e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 { 95207f623d3SYOSHIFUJI Hideaki *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel; 9533e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 } 9543e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 9556502ca52SYOSHIFUJI Hideaki / 吉藤英明 static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr) 9566502ca52SYOSHIFUJI Hideaki / 吉藤英明 { 9576502ca52SYOSHIFUJI Hideaki / 吉藤英明 return *(__be32 *)hdr & IPV6_FLOWINFO_MASK; 9586502ca52SYOSHIFUJI Hideaki / 吉藤英明 } 9596502ca52SYOSHIFUJI Hideaki / 吉藤英明 9603308de2bSFlorent Fourcot static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr) 9613308de2bSFlorent Fourcot { 9623308de2bSFlorent Fourcot return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK; 9633308de2bSFlorent Fourcot } 9643308de2bSFlorent Fourcot 965d76ed22bSLi RongQing static inline u8 ip6_tclass(__be32 flowinfo) 966d76ed22bSLi RongQing { 967d76ed22bSLi RongQing return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT; 968d76ed22bSLi RongQing } 969eaa93bf4SDaniel Borkmann 970eaa93bf4SDaniel Borkmann static inline __be32 ip6_make_flowinfo(unsigned int tclass, __be32 flowlabel) 971eaa93bf4SDaniel Borkmann { 972eaa93bf4SDaniel Borkmann return htonl(tclass << IPV6_TCLASS_SHIFT) | flowlabel; 973eaa93bf4SDaniel Borkmann } 974eaa93bf4SDaniel Borkmann 975fa1be7e0SMichal Kubecek static inline __be32 flowi6_get_flowlabel(const struct flowi6 *fl6) 976fa1be7e0SMichal Kubecek { 977fa1be7e0SMichal Kubecek return fl6->flowlabel & IPV6_FLOWLABEL_MASK; 978fa1be7e0SMichal Kubecek } 979fa1be7e0SMichal Kubecek 9803e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 /* 9811da177e4SLinus Torvalds * Prototypes exported by ipv6 9821da177e4SLinus Torvalds */ 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* 9851da177e4SLinus Torvalds * rcv function (called from netdevice level) 9861da177e4SLinus Torvalds */ 9871da177e4SLinus Torvalds 9885c3a0fd7SJoe Perches int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, 9895c3a0fd7SJoe Perches struct packet_type *pt, struct net_device *orig_dev); 990d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, 991d8269e2cSEdward Cree struct net_device *orig_dev); 9921da177e4SLinus Torvalds 9930c4b51f0SEric W. Biederman int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb); 994b05e1066SPatrick McHardy 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * upper-layer output functions 9971da177e4SLinus Torvalds */ 9981c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, 9994f6570d7SEric Dumazet __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority); 10001da177e4SLinus Torvalds 10015c3a0fd7SJoe Perches int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr); 10021da177e4SLinus Torvalds 10035c3a0fd7SJoe Perches int ip6_append_data(struct sock *sk, 10045c3a0fd7SJoe Perches int getfrag(void *from, char *to, int offset, int len, 10055c3a0fd7SJoe Perches int odd, struct sk_buff *skb), 100626879da5SWei Wang void *from, int length, int transhdrlen, 100726879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 10085fdaa88dSWillem de Bruijn struct rt6_info *rt, unsigned int flags); 10091da177e4SLinus Torvalds 10105c3a0fd7SJoe Perches int ip6_push_pending_frames(struct sock *sk); 10111da177e4SLinus Torvalds 10125c3a0fd7SJoe Perches void ip6_flush_pending_frames(struct sock *sk); 10131da177e4SLinus Torvalds 10146422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb); 10156422398cSVlad Yasevich 10166422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk, struct sk_buff_head *queue, 10176422398cSVlad Yasevich struct inet_cork_full *cork, 10186422398cSVlad Yasevich struct inet6_cork *v6_cork); 10196422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk, 10206422398cSVlad Yasevich int getfrag(void *from, char *to, int offset, 10216422398cSVlad Yasevich int len, int odd, struct sk_buff *skb), 10226422398cSVlad Yasevich void *from, int length, int transhdrlen, 1023*f37a4cc6SPavel Begunkov struct ipcm6_cookie *ipc6, 102426879da5SWei Wang struct rt6_info *rt, unsigned int flags, 10255fdaa88dSWillem de Bruijn struct inet_cork_full *cork); 10266422398cSVlad Yasevich 10276422398cSVlad Yasevich static inline struct sk_buff *ip6_finish_skb(struct sock *sk) 10286422398cSVlad Yasevich { 10296422398cSVlad Yasevich return __ip6_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork, 10306422398cSVlad Yasevich &inet6_sk(sk)->cork); 10316422398cSVlad Yasevich } 10326422398cSVlad Yasevich 1033343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst, 1034343d60aaSRoopa Prabhu struct flowi6 *fl6); 1035c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, 10360e0d44abSSteffen Klassert const struct in6_addr *final_dst); 10375c3a0fd7SJoe Perches struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 103896818159SAlexey Kodanev const struct in6_addr *final_dst, 103996818159SAlexey Kodanev bool connected); 1040571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb, 1041571912c6SMartin Varghese struct net_device *dev, 1042571912c6SMartin Varghese struct net *net, struct socket *sock, 1043571912c6SMartin Varghese struct in6_addr *saddr, 1044571912c6SMartin Varghese const struct ip_tunnel_info *info, 1045571912c6SMartin Varghese u8 protocol, bool use_cache); 10465c3a0fd7SJoe Perches struct dst_entry *ip6_blackhole_route(struct net *net, 104769ead7afSDavid S. Miller struct dst_entry *orig_dst); 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* 10501da177e4SLinus Torvalds * skb processing functions 10511da177e4SLinus Torvalds */ 10521da177e4SLinus Torvalds 1053ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb); 10545c3a0fd7SJoe Perches int ip6_forward(struct sk_buff *skb); 10555c3a0fd7SJoe Perches int ip6_input(struct sk_buff *skb); 10565c3a0fd7SJoe Perches int ip6_mc_input(struct sk_buff *skb); 105780bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, 105880bde363SPaolo Abeni bool have_final); 10591da177e4SLinus Torvalds 1060cf91a99dSEric W. Biederman int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb); 106133224b16SEric W. Biederman int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb); 1062ef76bc23SHerbert Xu 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * Extension header (options) processing 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds 10675c3a0fd7SJoe Perches void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, 1068613fa3caSDavid Lebrun u8 *proto, struct in6_addr **daddr_p, 1069613fa3caSDavid Lebrun struct in6_addr *saddr); 10705c3a0fd7SJoe Perches void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, 10711da177e4SLinus Torvalds u8 *proto); 10721da177e4SLinus Torvalds 10735c3a0fd7SJoe Perches int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp, 10745c3a0fd7SJoe Perches __be16 *frag_offp); 10751da177e4SLinus Torvalds 10765c3a0fd7SJoe Perches bool ipv6_ext_hdr(u8 nexthdr); 10771da177e4SLinus Torvalds 1078f8f62675SJesse Gross enum { 1079f8f62675SJesse Gross IP6_FH_F_FRAG = (1 << 0), 1080f8f62675SJesse Gross IP6_FH_F_AUTH = (1 << 1), 10819195bb8eSAnsis Atteka IP6_FH_F_SKIP_RH = (1 << 2), 1082f8f62675SJesse Gross }; 1083f8f62675SJesse Gross 1084f8f62675SJesse Gross /* find specified header and get offset to it */ 10855c3a0fd7SJoe Perches int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target, 10865c3a0fd7SJoe Perches unsigned short *fragoff, int *fragflg); 1087f8f62675SJesse Gross 10880868383bSHuw Davies int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type); 1089c61a4043SMasahide NAKAMURA 10905c3a0fd7SJoe Perches struct in6_addr *fl6_update_dst(struct flowi6 *fl6, 109120c59de2SArnaud Ebalard const struct ipv6_txoptions *opt, 109220c59de2SArnaud Ebalard struct in6_addr *orig); 109320c59de2SArnaud Ebalard 10941da177e4SLinus Torvalds /* 10951da177e4SLinus Torvalds * socket options (ipv6_sockglue.c) 10961da177e4SLinus Torvalds */ 1097790eb673SEric Dumazet DECLARE_STATIC_KEY_FALSE(ip6_min_hopcount); 10981da177e4SLinus Torvalds 1099a7b75c5aSChristoph Hellwig int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1100a7b75c5aSChristoph Hellwig unsigned int optlen); 11015c3a0fd7SJoe Perches int ipv6_getsockopt(struct sock *sk, int level, int optname, 11025c3a0fd7SJoe Perches char __user *optval, int __user *optlen); 11031da177e4SLinus Torvalds 11040382a25aSGuillaume Nault int __ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, 11050382a25aSGuillaume Nault int addr_len); 11065c3a0fd7SJoe Perches int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len); 110782b276cdSHannes Frederic Sowa int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *addr, 110882b276cdSHannes Frederic Sowa int addr_len); 110933c162a9SMartin KaFai Lau int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr); 1110e646b657SMartin KaFai Lau void ip6_datagram_release_cb(struct sock *sk); 11111da177e4SLinus Torvalds 111285fbaa75SHannes Frederic Sowa int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, 111385fbaa75SHannes Frederic Sowa int *addr_len); 111485fbaa75SHannes Frederic Sowa int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len, 111585fbaa75SHannes Frederic Sowa int *addr_len); 11165c3a0fd7SJoe Perches void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port, 11171da177e4SLinus Torvalds u32 info, u8 *payload); 11185c3a0fd7SJoe Perches void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info); 11195c3a0fd7SJoe Perches void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu); 11201da177e4SLinus Torvalds 11215c3a0fd7SJoe Perches int inet6_release(struct socket *sock); 11225c3a0fd7SJoe Perches int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len); 11239b2c45d4SDenys Vlasenko int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 11245c3a0fd7SJoe Perches int peer); 11255c3a0fd7SJoe Perches int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 11263986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, 11273986912fSChristoph Hellwig unsigned long arg); 11281da177e4SLinus Torvalds 11295c3a0fd7SJoe Perches int inet6_hash_connect(struct inet_timewait_death_row *death_row, 1130d8313f5cSArnaldo Carvalho de Melo struct sock *sk); 1131e42f1ac6SFlorian Westphal int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size); 1132e42f1ac6SFlorian Westphal int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 1133e42f1ac6SFlorian Westphal int flags); 1134d8313f5cSArnaldo Carvalho de Melo 11351da177e4SLinus Torvalds /* 11361da177e4SLinus Torvalds * reassembly.c 11371da177e4SLinus Torvalds */ 113890ddc4f0SEric Dumazet extern const struct proto_ops inet6_stream_ops; 113990ddc4f0SEric Dumazet extern const struct proto_ops inet6_dgram_ops; 114077d4b1d3SEric Dumazet extern const struct proto_ops inet6_sockraw_ops; 114120380731SArnaldo Carvalho de Melo 114214c85021SArnaldo Carvalho de Melo struct group_source_req; 114314c85021SArnaldo Carvalho de Melo struct group_filter; 114414c85021SArnaldo Carvalho de Melo 11455c3a0fd7SJoe Perches int ip6_mc_source(int add, int omode, struct sock *sk, 114620380731SArnaldo Carvalho de Melo struct group_source_req *pgsr); 1147d59eb177SAl Viro int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf, 1148d59eb177SAl Viro struct sockaddr_storage *list); 11495c3a0fd7SJoe Perches int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf, 1150931ca7abSAl Viro struct sockaddr_storage __user *p); 115120380731SArnaldo Carvalho de Melo 115220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 11535c3a0fd7SJoe Perches int ac6_proc_init(struct net *net); 11545c3a0fd7SJoe Perches void ac6_proc_exit(struct net *net); 11555c3a0fd7SJoe Perches int raw6_proc_init(void); 11565c3a0fd7SJoe Perches void raw6_proc_exit(void); 11575c3a0fd7SJoe Perches int tcp6_proc_init(struct net *net); 11585c3a0fd7SJoe Perches void tcp6_proc_exit(struct net *net); 11595c3a0fd7SJoe Perches int udp6_proc_init(struct net *net); 11605c3a0fd7SJoe Perches void udp6_proc_exit(struct net *net); 11615c3a0fd7SJoe Perches int udplite6_proc_init(void); 11625c3a0fd7SJoe Perches void udplite6_proc_exit(void); 11635c3a0fd7SJoe Perches int ipv6_misc_proc_init(void); 11645c3a0fd7SJoe Perches void ipv6_misc_proc_exit(void); 11655c3a0fd7SJoe Perches int snmp6_register_dev(struct inet6_dev *idev); 11665c3a0fd7SJoe Perches int snmp6_unregister_dev(struct inet6_dev *idev); 116720380731SArnaldo Carvalho de Melo 11687f7d9a6bSHerbert Xu #else 11696ab57e7eSDaniel Lezcano static inline int ac6_proc_init(struct net *net) { return 0; } 11706ab57e7eSDaniel Lezcano static inline void ac6_proc_exit(struct net *net) { } 11716ab57e7eSDaniel Lezcano static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; } 11726ab57e7eSDaniel Lezcano static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; } 117320380731SArnaldo Carvalho de Melo #endif 117420380731SArnaldo Carvalho de Melo 117520380731SArnaldo Carvalho de Melo #ifdef CONFIG_SYSCTL 11765c3a0fd7SJoe Perches struct ctl_table *ipv6_icmp_sysctl_init(struct net *net); 11775c3a0fd7SJoe Perches struct ctl_table *ipv6_route_sysctl_init(struct net *net); 11785c3a0fd7SJoe Perches int ipv6_sysctl_register(void); 11795c3a0fd7SJoe Perches void ipv6_sysctl_unregister(void); 118020380731SArnaldo Carvalho de Melo #endif 118120380731SArnaldo Carvalho de Melo 118246a4dee0SMadhu Challa int ipv6_sock_mc_join(struct sock *sk, int ifindex, 118346a4dee0SMadhu Challa const struct in6_addr *addr); 1184c7ea20c9SHangbin Liu int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex, 1185c7ea20c9SHangbin Liu const struct in6_addr *addr, unsigned int mode); 118646a4dee0SMadhu Challa int ipv6_sock_mc_drop(struct sock *sk, int ifindex, 118746a4dee0SMadhu Challa const struct in6_addr *addr); 11889b115749SChristoph Hellwig 11899b115749SChristoph Hellwig static inline int ip6_sock_set_v6only(struct sock *sk) 11909b115749SChristoph Hellwig { 11919b115749SChristoph Hellwig if (inet_sk(sk)->inet_num) 11929b115749SChristoph Hellwig return -EINVAL; 11939b115749SChristoph Hellwig lock_sock(sk); 11949b115749SChristoph Hellwig sk->sk_ipv6only = true; 11959b115749SChristoph Hellwig release_sock(sk); 11969b115749SChristoph Hellwig return 0; 11979b115749SChristoph Hellwig } 11989b115749SChristoph Hellwig 1199fce93494SChristoph Hellwig static inline void ip6_sock_set_recverr(struct sock *sk) 1200fce93494SChristoph Hellwig { 1201fce93494SChristoph Hellwig lock_sock(sk); 1202fce93494SChristoph Hellwig inet6_sk(sk)->recverr = true; 1203fce93494SChristoph Hellwig release_sock(sk); 1204fce93494SChristoph Hellwig } 1205fce93494SChristoph Hellwig 120618d5ad62SChristoph Hellwig static inline int __ip6_sock_set_addr_preferences(struct sock *sk, int val) 120718d5ad62SChristoph Hellwig { 120818d5ad62SChristoph Hellwig unsigned int pref = 0; 120918d5ad62SChristoph Hellwig unsigned int prefmask = ~0; 121018d5ad62SChristoph Hellwig 121118d5ad62SChristoph Hellwig /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */ 121218d5ad62SChristoph Hellwig switch (val & (IPV6_PREFER_SRC_PUBLIC | 121318d5ad62SChristoph Hellwig IPV6_PREFER_SRC_TMP | 121418d5ad62SChristoph Hellwig IPV6_PREFER_SRC_PUBTMP_DEFAULT)) { 121518d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_PUBLIC: 121618d5ad62SChristoph Hellwig pref |= IPV6_PREFER_SRC_PUBLIC; 121718d5ad62SChristoph Hellwig prefmask &= ~(IPV6_PREFER_SRC_PUBLIC | 121818d5ad62SChristoph Hellwig IPV6_PREFER_SRC_TMP); 121918d5ad62SChristoph Hellwig break; 122018d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_TMP: 122118d5ad62SChristoph Hellwig pref |= IPV6_PREFER_SRC_TMP; 122218d5ad62SChristoph Hellwig prefmask &= ~(IPV6_PREFER_SRC_PUBLIC | 122318d5ad62SChristoph Hellwig IPV6_PREFER_SRC_TMP); 122418d5ad62SChristoph Hellwig break; 122518d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_PUBTMP_DEFAULT: 122618d5ad62SChristoph Hellwig prefmask &= ~(IPV6_PREFER_SRC_PUBLIC | 122718d5ad62SChristoph Hellwig IPV6_PREFER_SRC_TMP); 122818d5ad62SChristoph Hellwig break; 122918d5ad62SChristoph Hellwig case 0: 123018d5ad62SChristoph Hellwig break; 123118d5ad62SChristoph Hellwig default: 123218d5ad62SChristoph Hellwig return -EINVAL; 123318d5ad62SChristoph Hellwig } 123418d5ad62SChristoph Hellwig 123518d5ad62SChristoph Hellwig /* check HOME/COA conflicts */ 123618d5ad62SChristoph Hellwig switch (val & (IPV6_PREFER_SRC_HOME | IPV6_PREFER_SRC_COA)) { 123718d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_HOME: 123818d5ad62SChristoph Hellwig prefmask &= ~IPV6_PREFER_SRC_COA; 123918d5ad62SChristoph Hellwig break; 124018d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_COA: 124118d5ad62SChristoph Hellwig pref |= IPV6_PREFER_SRC_COA; 124218d5ad62SChristoph Hellwig break; 124318d5ad62SChristoph Hellwig case 0: 124418d5ad62SChristoph Hellwig break; 124518d5ad62SChristoph Hellwig default: 124618d5ad62SChristoph Hellwig return -EINVAL; 124718d5ad62SChristoph Hellwig } 124818d5ad62SChristoph Hellwig 124918d5ad62SChristoph Hellwig /* check CGA/NONCGA conflicts */ 125018d5ad62SChristoph Hellwig switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) { 125118d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_CGA: 125218d5ad62SChristoph Hellwig case IPV6_PREFER_SRC_NONCGA: 125318d5ad62SChristoph Hellwig case 0: 125418d5ad62SChristoph Hellwig break; 125518d5ad62SChristoph Hellwig default: 125618d5ad62SChristoph Hellwig return -EINVAL; 125718d5ad62SChristoph Hellwig } 125818d5ad62SChristoph Hellwig 125918d5ad62SChristoph Hellwig inet6_sk(sk)->srcprefs = (inet6_sk(sk)->srcprefs & prefmask) | pref; 126018d5ad62SChristoph Hellwig return 0; 126118d5ad62SChristoph Hellwig } 126218d5ad62SChristoph Hellwig 126318d5ad62SChristoph Hellwig static inline int ip6_sock_set_addr_preferences(struct sock *sk, bool val) 126418d5ad62SChristoph Hellwig { 126518d5ad62SChristoph Hellwig int ret; 126618d5ad62SChristoph Hellwig 126718d5ad62SChristoph Hellwig lock_sock(sk); 126818d5ad62SChristoph Hellwig ret = __ip6_sock_set_addr_preferences(sk, val); 126918d5ad62SChristoph Hellwig release_sock(sk); 127018d5ad62SChristoph Hellwig return ret; 127118d5ad62SChristoph Hellwig } 127218d5ad62SChristoph Hellwig 12737d7207c2SChristoph Hellwig static inline void ip6_sock_set_recvpktinfo(struct sock *sk) 12747d7207c2SChristoph Hellwig { 12757d7207c2SChristoph Hellwig lock_sock(sk); 12767d7207c2SChristoph Hellwig inet6_sk(sk)->rxopt.bits.rxinfo = true; 12777d7207c2SChristoph Hellwig release_sock(sk); 12787d7207c2SChristoph Hellwig } 12797d7207c2SChristoph Hellwig 12801da177e4SLinus Torvalds #endif /* _NET_IPV6_H */ 1281