ipv6.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Linux INET6 implementation
  3. *
  4. * Authors:
  5. * Pedro Roque <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef _NET_IPV6_H
  13. #define _NET_IPV6_H
  14. #include <linux/ipv6.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/jhash.h>
  17. #include <net/if_inet6.h>
  18. #include <net/ndisc.h>
  19. #include <net/flow.h>
  20. #include <net/flow_dissector.h>
  21. #include <net/snmp.h>
  22. #define SIN6_LEN_RFC2133 24
  23. #define IPV6_MAXPLEN 65535
  24. /*
  25. * NextHeader field of IPv6 header
  26. */
  27. #define NEXTHDR_HOP 0 /* Hop-by-hop option header. */
  28. #define NEXTHDR_TCP 6 /* TCP segment. */
  29. #define NEXTHDR_UDP 17 /* UDP message. */
  30. #define NEXTHDR_IPV6 41 /* IPv6 in IPv6 */
  31. #define NEXTHDR_ROUTING 43 /* Routing header. */
  32. #define NEXTHDR_FRAGMENT 44 /* Fragmentation/reassembly header. */
  33. #define NEXTHDR_GRE 47 /* GRE header. */
  34. #define NEXTHDR_ESP 50 /* Encapsulating security payload. */
  35. #define NEXTHDR_AUTH 51 /* Authentication header. */
  36. #define NEXTHDR_ICMP 58 /* ICMP for IPv6. */
  37. #define NEXTHDR_NONE 59 /* No next header */
  38. #define NEXTHDR_DEST 60 /* Destination options header. */
  39. #define NEXTHDR_SCTP 132 /* SCTP message. */
  40. #define NEXTHDR_MOBILITY 135 /* Mobility header. */
  41. #define NEXTHDR_MAX 255
  42. #define IPV6_DEFAULT_HOPLIMIT 64
  43. #define IPV6_DEFAULT_MCASTHOPS 1
  44. /*
  45. * Addr type
  46. *
  47. * type - unicast | multicast
  48. * scope - local | site | global
  49. * v4 - compat
  50. * v4mapped
  51. * any
  52. * loopback
  53. */
  54. #define IPV6_ADDR_ANY 0x0000U
  55. #define IPV6_ADDR_UNICAST 0x0001U
  56. #define IPV6_ADDR_MULTICAST 0x0002U
  57. #define IPV6_ADDR_LOOPBACK 0x0010U
  58. #define IPV6_ADDR_LINKLOCAL 0x0020U
  59. #define IPV6_ADDR_SITELOCAL 0x0040U
  60. #define IPV6_ADDR_COMPATv4 0x0080U
  61. #define IPV6_ADDR_SCOPE_MASK 0x00f0U
  62. #define IPV6_ADDR_MAPPED 0x1000U
  63. /*
  64. * Addr scopes
  65. */
  66. #define IPV6_ADDR_MC_SCOPE(a) \
  67. ((a)->s6_addr[1] & 0x0f) /* nonstandard */
  68. #define __IPV6_ADDR_SCOPE_INVALID -1
  69. #define IPV6_ADDR_SCOPE_NODELOCAL 0x01
  70. #define IPV6_ADDR_SCOPE_LINKLOCAL 0x02
  71. #define IPV6_ADDR_SCOPE_SITELOCAL 0x05
  72. #define IPV6_ADDR_SCOPE_ORGLOCAL 0x08
  73. #define IPV6_ADDR_SCOPE_GLOBAL 0x0e
  74. /*
  75. * Addr flags
  76. */
  77. #define IPV6_ADDR_MC_FLAG_TRANSIENT(a) \
  78. ((a)->s6_addr[1] & 0x10)
  79. #define IPV6_ADDR_MC_FLAG_PREFIX(a) \
  80. ((a)->s6_addr[1] & 0x20)
  81. #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a) \
  82. ((a)->s6_addr[1] & 0x40)
  83. /*
  84. * fragmentation header
  85. */
  86. struct frag_hdr {
  87. __u8 nexthdr;
  88. __u8 reserved;
  89. __be16 frag_off;
  90. __be32 identification;
  91. };
  92. #define IP6_MF 0x0001
  93. #define IP6_OFFSET 0xFFF8
  94. #define IP6_REPLY_MARK(net, mark) \
  95. ((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
  96. #include <net/sock.h>
  97. /* sysctls */
  98. extern int sysctl_mld_max_msf;
  99. extern int sysctl_mld_qrv;
  100. #define _DEVINC(net, statname, mod, idev, field) \
  101. ({ \
  102. struct inet6_dev *_idev = (idev); \
  103. if (likely(_idev != NULL)) \
  104. mod##SNMP_INC_STATS64((_idev)->stats.statname, (field));\
  105. mod##SNMP_INC_STATS64((net)->mib.statname##_statistics, (field));\
  106. })
  107. /* per device counters are atomic_long_t */
  108. #define _DEVINCATOMIC(net, statname, mod, idev, field) \
  109. ({ \
  110. struct inet6_dev *_idev = (idev); \
  111. if (likely(_idev != NULL)) \
  112. SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
  113. mod##SNMP_INC_STATS((net)->mib.statname##_statistics, (field));\
  114. })
  115. /* per device and per net counters are atomic_long_t */
  116. #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field) \
  117. ({ \
  118. struct inet6_dev *_idev = (idev); \
  119. if (likely(_idev != NULL)) \
  120. SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
  121. SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\
  122. })
  123. #define _DEVADD(net, statname, mod, idev, field, val) \
  124. ({ \
  125. struct inet6_dev *_idev = (idev); \
  126. if (likely(_idev != NULL)) \
  127. mod##SNMP_ADD_STATS((_idev)->stats.statname, (field), (val)); \
  128. mod##SNMP_ADD_STATS((net)->mib.statname##_statistics, (field), (val));\
  129. })
  130. #define _DEVUPD(net, statname, mod, idev, field, val) \
  131. ({ \
  132. struct inet6_dev *_idev = (idev); \
  133. if (likely(_idev != NULL)) \
  134. mod##SNMP_UPD_PO_STATS((_idev)->stats.statname, field, (val)); \
  135. mod##SNMP_UPD_PO_STATS((net)->mib.statname##_statistics, field, (val));\
  136. })
  137. /* MIBs */
  138. #define IP6_INC_STATS(net, idev,field) \
  139. _DEVINC(net, ipv6, , idev, field)
  140. #define __IP6_INC_STATS(net, idev,field) \
  141. _DEVINC(net, ipv6, __, idev, field)
  142. #define IP6_ADD_STATS(net, idev,field,val) \
  143. _DEVADD(net, ipv6, , idev, field, val)
  144. #define __IP6_ADD_STATS(net, idev,field,val) \
  145. _DEVADD(net, ipv6, __, idev, field, val)
  146. #define IP6_UPD_PO_STATS(net, idev,field,val) \
  147. _DEVUPD(net, ipv6, , idev, field, val)
  148. #define __IP6_UPD_PO_STATS(net, idev,field,val) \
  149. _DEVUPD(net, ipv6, __, idev, field, val)
  150. #define ICMP6_INC_STATS(net, idev, field) \
  151. _DEVINCATOMIC(net, icmpv6, , idev, field)
  152. #define __ICMP6_INC_STATS(net, idev, field) \
  153. _DEVINCATOMIC(net, icmpv6, __, idev, field)
  154. #define ICMP6MSGOUT_INC_STATS(net, idev, field) \
  155. _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
  156. #define ICMP6MSGIN_INC_STATS(net, idev, field) \
  157. _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field)
  158. struct ip6_ra_chain {
  159. struct ip6_ra_chain *next;
  160. struct sock *sk;
  161. int sel;
  162. void (*destructor)(struct sock *);
  163. };
  164. extern struct ip6_ra_chain *ip6_ra_chain;
  165. extern rwlock_t ip6_ra_lock;
  166. /*
  167. This structure is prepared by protocol, when parsing
  168. ancillary data and passed to IPv6.
  169. */
  170. struct ipv6_txoptions {
  171. atomic_t refcnt;
  172. /* Length of this structure */
  173. int tot_len;
  174. /* length of extension headers */
  175. __u16 opt_flen; /* after fragment hdr */
  176. __u16 opt_nflen; /* before fragment hdr */
  177. struct ipv6_opt_hdr *hopopt;
  178. struct ipv6_opt_hdr *dst0opt;
  179. struct ipv6_rt_hdr *srcrt; /* Routing Header */
  180. struct ipv6_opt_hdr *dst1opt;
  181. struct rcu_head rcu;
  182. /* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */
  183. };
  184. struct ip6_flowlabel {
  185. struct ip6_flowlabel __rcu *next;
  186. __be32 label;
  187. atomic_t users;
  188. struct in6_addr dst;
  189. struct ipv6_txoptions *opt;
  190. unsigned long linger;
  191. struct rcu_head rcu;
  192. u8 share;
  193. union {
  194. struct pid *pid;
  195. kuid_t uid;
  196. } owner;
  197. unsigned long lastuse;
  198. unsigned long expires;
  199. struct net *fl_net;
  200. };
  201. #define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
  202. #define IPV6_FLOWLABEL_MASK cpu_to_be32(0x000FFFFF)
  203. #define IPV6_FLOWLABEL_STATELESS_FLAG cpu_to_be32(0x00080000)
  204. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  205. #define IPV6_TCLASS_SHIFT 20
  206. struct ipv6_fl_socklist {
  207. struct ipv6_fl_socklist __rcu *next;
  208. struct ip6_flowlabel *fl;
  209. struct rcu_head rcu;
  210. };
  211. struct ipcm6_cookie {
  212. __s16 hlimit;
  213. __s16 tclass;
  214. __s8 dontfrag;
  215. struct ipv6_txoptions *opt;
  216. };
  217. static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
  218. {
  219. struct ipv6_txoptions *opt;
  220. rcu_read_lock();
  221. opt = rcu_dereference(np->opt);
  222. if (opt) {
  223. if (!atomic_inc_not_zero(&opt->refcnt))
  224. opt = NULL;
  225. else
  226. opt = rcu_pointer_handoff(opt);
  227. }
  228. rcu_read_unlock();
  229. return opt;
  230. }
  231. static inline void txopt_put(struct ipv6_txoptions *opt)
  232. {
  233. if (opt && atomic_dec_and_test(&opt->refcnt))
  234. kfree_rcu(opt, rcu);
  235. }
  236. struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk, __be32 label);
  237. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
  238. struct ip6_flowlabel *fl,
  239. struct ipv6_txoptions *fopt);
  240. void fl6_free_socklist(struct sock *sk);
  241. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen);
  242. int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
  243. int flags);
  244. int ip6_flowlabel_init(void);
  245. void ip6_flowlabel_cleanup(void);
  246. bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np);
  247. static inline void fl6_sock_release(struct ip6_flowlabel *fl)
  248. {
  249. if (fl)
  250. atomic_dec(&fl->users);
  251. }
  252. void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info);
  253. int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  254. struct icmp6hdr *thdr, int len);
  255. int ip6_ra_control(struct sock *sk, int sel);
  256. int ipv6_parse_hopopts(struct sk_buff *skb);
  257. struct ipv6_txoptions *ipv6_dup_options(struct sock *sk,
  258. struct ipv6_txoptions *opt);
  259. struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
  260. struct ipv6_txoptions *opt,
  261. int newtype,
  262. struct ipv6_opt_hdr *newopt);
  263. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  264. struct ipv6_txoptions *opt);
  265. bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
  266. const struct inet6_skb_parm *opt);
  267. struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
  268. struct ipv6_txoptions *opt);
  269. static inline bool ipv6_accept_ra(struct inet6_dev *idev)
  270. {
  271. /* If forwarding is enabled, RA are not accepted unless the special
  272. * hybrid mode (accept_ra=2) is enabled.
  273. */
  274. return idev->cnf.forwarding ? idev->cnf.accept_ra == 2 :
  275. idev->cnf.accept_ra;
  276. }
  277. #define IPV6_FRAG_HIGH_THRESH (4 * 1024*1024) /* 4194304 */
  278. #define IPV6_FRAG_LOW_THRESH (3 * 1024*1024) /* 3145728 */
  279. #define IPV6_FRAG_TIMEOUT (60 * HZ) /* 60 seconds */
  280. int __ipv6_addr_type(const struct in6_addr *addr);
  281. static inline int ipv6_addr_type(const struct in6_addr *addr)
  282. {
  283. return __ipv6_addr_type(addr) & 0xffff;
  284. }
  285. static inline int ipv6_addr_scope(const struct in6_addr *addr)
  286. {
  287. return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK;
  288. }
  289. static inline int __ipv6_addr_src_scope(int type)
  290. {
  291. return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16);
  292. }
  293. static inline int ipv6_addr_src_scope(const struct in6_addr *addr)
  294. {
  295. return __ipv6_addr_src_scope(__ipv6_addr_type(addr));
  296. }
  297. static inline bool __ipv6_addr_needs_scope_id(int type)
  298. {
  299. return type & IPV6_ADDR_LINKLOCAL ||
  300. (type & IPV6_ADDR_MULTICAST &&
  301. (type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)));
  302. }
  303. static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
  304. {
  305. return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
  306. }
  307. static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
  308. {
  309. return memcmp(a1, a2, sizeof(struct in6_addr));
  310. }
  311. static inline bool
  312. ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m,
  313. const struct in6_addr *a2)
  314. {
  315. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  316. const unsigned long *ul1 = (const unsigned long *)a1;
  317. const unsigned long *ulm = (const unsigned long *)m;
  318. const unsigned long *ul2 = (const unsigned long *)a2;
  319. return !!(((ul1[0] ^ ul2[0]) & ulm[0]) |
  320. ((ul1[1] ^ ul2[1]) & ulm[1]));
  321. #else
  322. return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) |
  323. ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) |
  324. ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) |
  325. ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3]));
  326. #endif
  327. }
  328. static inline void ipv6_addr_prefix(struct in6_addr *pfx,
  329. const struct in6_addr *addr,
  330. int plen)
  331. {
  332. /* caller must guarantee 0 <= plen <= 128 */
  333. int o = plen >> 3,
  334. b = plen & 0x7;
  335. memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr));
  336. memcpy(pfx->s6_addr, addr, o);
  337. if (b != 0)
  338. pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b);
  339. }
  340. static inline void ipv6_addr_prefix_copy(struct in6_addr *addr,
  341. const struct in6_addr *pfx,
  342. int plen)
  343. {
  344. /* caller must guarantee 0 <= plen <= 128 */
  345. int o = plen >> 3,
  346. b = plen & 0x7;
  347. memcpy(addr->s6_addr, pfx, o);
  348. if (b != 0) {
  349. addr->s6_addr[o] &= ~(0xff00 >> b);
  350. addr->s6_addr[o] |= (pfx->s6_addr[o] & (0xff00 >> b));
  351. }
  352. }
  353. static inline void __ipv6_addr_set_half(__be32 *addr,
  354. __be32 wh, __be32 wl)
  355. {
  356. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  357. #if defined(__BIG_ENDIAN)
  358. if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
  359. *(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
  360. return;
  361. }
  362. #elif defined(__LITTLE_ENDIAN)
  363. if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
  364. *(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
  365. return;
  366. }
  367. #endif
  368. #endif
  369. addr[0] = wh;
  370. addr[1] = wl;
  371. }
  372. static inline void ipv6_addr_set(struct in6_addr *addr,
  373. __be32 w1, __be32 w2,
  374. __be32 w3, __be32 w4)
  375. {
  376. __ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
  377. __ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
  378. }
  379. static inline bool ipv6_addr_equal(const struct in6_addr *a1,
  380. const struct in6_addr *a2)
  381. {
  382. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  383. const unsigned long *ul1 = (const unsigned long *)a1;
  384. const unsigned long *ul2 = (const unsigned long *)a2;
  385. return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
  386. #else
  387. return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
  388. (a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
  389. (a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
  390. (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0;
  391. #endif
  392. }
  393. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  394. static inline bool __ipv6_prefix_equal64_half(const __be64 *a1,
  395. const __be64 *a2,
  396. unsigned int len)
  397. {
  398. if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len))))
  399. return false;
  400. return true;
  401. }
  402. static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
  403. const struct in6_addr *addr2,
  404. unsigned int prefixlen)
  405. {
  406. const __be64 *a1 = (const __be64 *)addr1;
  407. const __be64 *a2 = (const __be64 *)addr2;
  408. if (prefixlen >= 64) {
  409. if (a1[0] ^ a2[0])
  410. return false;
  411. return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64);
  412. }
  413. return __ipv6_prefix_equal64_half(a1, a2, prefixlen);
  414. }
  415. #else
  416. static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
  417. const struct in6_addr *addr2,
  418. unsigned int prefixlen)
  419. {
  420. const __be32 *a1 = addr1->s6_addr32;
  421. const __be32 *a2 = addr2->s6_addr32;
  422. unsigned int pdw, pbi;
  423. /* check complete u32 in prefix */
  424. pdw = prefixlen >> 5;
  425. if (pdw && memcmp(a1, a2, pdw << 2))
  426. return false;
  427. /* check incomplete u32 in prefix */
  428. pbi = prefixlen & 0x1f;
  429. if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
  430. return false;
  431. return true;
  432. }
  433. #endif
  434. static inline bool ipv6_addr_any(const struct in6_addr *a)
  435. {
  436. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  437. const unsigned long *ul = (const unsigned long *)a;
  438. return (ul[0] | ul[1]) == 0UL;
  439. #else
  440. return (a->s6_addr32[0] | a->s6_addr32[1] |
  441. a->s6_addr32[2] | a->s6_addr32[3]) == 0;
  442. #endif
  443. }
  444. static inline u32 ipv6_addr_hash(const struct in6_addr *a)
  445. {
  446. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  447. const unsigned long *ul = (const unsigned long *)a;
  448. unsigned long x = ul[0] ^ ul[1];
  449. return (u32)(x ^ (x >> 32));
  450. #else
  451. return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^
  452. a->s6_addr32[2] ^ a->s6_addr32[3]);
  453. #endif
  454. }
  455. /* more secured version of ipv6_addr_hash() */
  456. static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval)
  457. {
  458. u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1];
  459. return jhash_3words(v,
  460. (__force u32)a->s6_addr32[2],
  461. (__force u32)a->s6_addr32[3],
  462. initval);
  463. }
  464. static inline bool ipv6_addr_loopback(const struct in6_addr *a)
  465. {
  466. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  467. const __be64 *be = (const __be64 *)a;
  468. return (be[0] | (be[1] ^ cpu_to_be64(1))) == 0UL;
  469. #else
  470. return (a->s6_addr32[0] | a->s6_addr32[1] |
  471. a->s6_addr32[2] | (a->s6_addr32[3] ^ cpu_to_be32(1))) == 0;
  472. #endif
  473. }
  474. /*
  475. * Note that we must __force cast these to unsigned long to make sparse happy,
  476. * since all of the endian-annotated types are fixed size regardless of arch.
  477. */
  478. static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
  479. {
  480. return (
  481. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  482. *(unsigned long *)a |
  483. #else
  484. (__force unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
  485. #endif
  486. (__force unsigned long)(a->s6_addr32[2] ^
  487. cpu_to_be32(0x0000ffff))) == 0UL;
  488. }
  489. /*
  490. * Check for a RFC 4843 ORCHID address
  491. * (Overlay Routable Cryptographic Hash Identifiers)
  492. */
  493. static inline bool ipv6_addr_orchid(const struct in6_addr *a)
  494. {
  495. return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010);
  496. }
  497. static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr)
  498. {
  499. return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000);
  500. }
  501. static inline void ipv6_addr_set_v4mapped(const __be32 addr,
  502. struct in6_addr *v4mapped)
  503. {
  504. ipv6_addr_set(v4mapped,
  505. 0, 0,
  506. htonl(0x0000FFFF),
  507. addr);
  508. }
  509. /*
  510. * find the first different bit between two addresses
  511. * length of address must be a multiple of 32bits
  512. */
  513. static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
  514. {
  515. const __be32 *a1 = token1, *a2 = token2;
  516. int i;
  517. addrlen >>= 2;
  518. for (i = 0; i < addrlen; i++) {
  519. __be32 xb = a1[i] ^ a2[i];
  520. if (xb)
  521. return i * 32 + 31 - __fls(ntohl(xb));
  522. }
  523. /*
  524. * we should *never* get to this point since that
  525. * would mean the addrs are equal
  526. *
  527. * However, we do get to it 8) And exacly, when
  528. * addresses are equal 8)
  529. *
  530. * ip route add 1111::/128 via ...
  531. * ip route add 1111::/64 via ...
  532. * and we are here.
  533. *
  534. * Ideally, this function should stop comparison
  535. * at prefix length. It does not, but it is still OK,
  536. * if returned value is greater than prefix length.
  537. * --ANK (980803)
  538. */
  539. return addrlen << 5;
  540. }
  541. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  542. static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen)
  543. {
  544. const __be64 *a1 = token1, *a2 = token2;
  545. int i;
  546. addrlen >>= 3;
  547. for (i = 0; i < addrlen; i++) {
  548. __be64 xb = a1[i] ^ a2[i];
  549. if (xb)
  550. return i * 64 + 63 - __fls(be64_to_cpu(xb));
  551. }
  552. return addrlen << 6;
  553. }
  554. #endif
  555. static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen)
  556. {
  557. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  558. if (__builtin_constant_p(addrlen) && !(addrlen & 7))
  559. return __ipv6_addr_diff64(token1, token2, addrlen);
  560. #endif
  561. return __ipv6_addr_diff32(token1, token2, addrlen);
  562. }
  563. static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
  564. {
  565. return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
  566. }
  567. __be32 ipv6_select_ident(struct net *net,
  568. const struct in6_addr *daddr,
  569. const struct in6_addr *saddr);
  570. void ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb);
  571. int ip6_dst_hoplimit(struct dst_entry *dst);
  572. static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6,
  573. struct dst_entry *dst)
  574. {
  575. int hlimit;
  576. if (ipv6_addr_is_multicast(&fl6->daddr))
  577. hlimit = np->mcast_hops;
  578. else
  579. hlimit = np->hop_limit;
  580. if (hlimit < 0)
  581. hlimit = ip6_dst_hoplimit(dst);
  582. return hlimit;
  583. }
  584. /* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store
  585. * Equivalent to : flow->v6addrs.src = iph->saddr;
  586. * flow->v6addrs.dst = iph->daddr;
  587. */
  588. static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow,
  589. const struct ipv6hdr *iph)
  590. {
  591. BUILD_BUG_ON(offsetof(typeof(flow->addrs), v6addrs.dst) !=
  592. offsetof(typeof(flow->addrs), v6addrs.src) +
  593. sizeof(flow->addrs.v6addrs.src));
  594. memcpy(&flow->addrs.v6addrs, &iph->saddr, sizeof(flow->addrs.v6addrs));
  595. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  596. }
  597. #if IS_ENABLED(CONFIG_IPV6)
  598. /* Sysctl settings for net ipv6.auto_flowlabels */
  599. #define IP6_AUTO_FLOW_LABEL_OFF 0
  600. #define IP6_AUTO_FLOW_LABEL_OPTOUT 1
  601. #define IP6_AUTO_FLOW_LABEL_OPTIN 2
  602. #define IP6_AUTO_FLOW_LABEL_FORCED 3
  603. #define IP6_AUTO_FLOW_LABEL_MAX IP6_AUTO_FLOW_LABEL_FORCED
  604. #define IP6_DEFAULT_AUTO_FLOW_LABELS IP6_AUTO_FLOW_LABEL_OPTOUT
  605. static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb,
  606. __be32 flowlabel, bool autolabel,
  607. struct flowi6 *fl6)
  608. {
  609. u32 hash;
  610. /* @flowlabel may include more than a flow label, eg, the traffic class.
  611. * Here we want only the flow label value.
  612. */
  613. flowlabel &= IPV6_FLOWLABEL_MASK;
  614. if (flowlabel ||
  615. net->ipv6.sysctl.auto_flowlabels == IP6_AUTO_FLOW_LABEL_OFF ||
  616. (!autolabel &&
  617. net->ipv6.sysctl.auto_flowlabels != IP6_AUTO_FLOW_LABEL_FORCED))
  618. return flowlabel;
  619. hash = skb_get_hash_flowi6(skb, fl6);
  620. /* Since this is being sent on the wire obfuscate hash a bit
  621. * to minimize possbility that any useful information to an
  622. * attacker is leaked. Only lower 20 bits are relevant.
  623. */
  624. hash = rol32(hash, 16);
  625. flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
  626. if (net->ipv6.sysctl.flowlabel_state_ranges)
  627. flowlabel |= IPV6_FLOWLABEL_STATELESS_FLAG;
  628. return flowlabel;
  629. }
  630. static inline int ip6_default_np_autolabel(struct net *net)
  631. {
  632. switch (net->ipv6.sysctl.auto_flowlabels) {
  633. case IP6_AUTO_FLOW_LABEL_OFF:
  634. case IP6_AUTO_FLOW_LABEL_OPTIN:
  635. default:
  636. return 0;
  637. case IP6_AUTO_FLOW_LABEL_OPTOUT:
  638. case IP6_AUTO_FLOW_LABEL_FORCED:
  639. return 1;
  640. }
  641. }
  642. #else
  643. static inline void ip6_set_txhash(struct sock *sk) { }
  644. static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb,
  645. __be32 flowlabel, bool autolabel,
  646. struct flowi6 *fl6)
  647. {
  648. return flowlabel;
  649. }
  650. static inline int ip6_default_np_autolabel(struct net *net)
  651. {
  652. return 0;
  653. }
  654. #endif
  655. /*
  656. * Header manipulation
  657. */
  658. static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
  659. __be32 flowlabel)
  660. {
  661. *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
  662. }
  663. static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
  664. {
  665. return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
  666. }
  667. static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
  668. {
  669. return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
  670. }
  671. static inline u8 ip6_tclass(__be32 flowinfo)
  672. {
  673. return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT;
  674. }
  675. static inline __be32 ip6_make_flowinfo(unsigned int tclass, __be32 flowlabel)
  676. {
  677. return htonl(tclass << IPV6_TCLASS_SHIFT) | flowlabel;
  678. }
  679. /*
  680. * Prototypes exported by ipv6
  681. */
  682. /*
  683. * rcv function (called from netdevice level)
  684. */
  685. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev,
  686. struct packet_type *pt, struct net_device *orig_dev);
  687. int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  688. /*
  689. * upper-layer output functions
  690. */
  691. int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  692. __u32 mark, struct ipv6_txoptions *opt, int tclass);
  693. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
  694. int ip6_append_data(struct sock *sk,
  695. int getfrag(void *from, char *to, int offset, int len,
  696. int odd, struct sk_buff *skb),
  697. void *from, int length, int transhdrlen,
  698. struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
  699. struct rt6_info *rt, unsigned int flags,
  700. const struct sockcm_cookie *sockc);
  701. int ip6_push_pending_frames(struct sock *sk);
  702. void ip6_flush_pending_frames(struct sock *sk);
  703. int ip6_send_skb(struct sk_buff *skb);
  704. struct sk_buff *__ip6_make_skb(struct sock *sk, struct sk_buff_head *queue,
  705. struct inet_cork_full *cork,
  706. struct inet6_cork *v6_cork);
  707. struct sk_buff *ip6_make_skb(struct sock *sk,
  708. int getfrag(void *from, char *to, int offset,
  709. int len, int odd, struct sk_buff *skb),
  710. void *from, int length, int transhdrlen,
  711. struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
  712. struct rt6_info *rt, unsigned int flags,
  713. const struct sockcm_cookie *sockc);
  714. static inline struct sk_buff *ip6_finish_skb(struct sock *sk)
  715. {
  716. return __ip6_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork,
  717. &inet6_sk(sk)->cork);
  718. }
  719. int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
  720. struct flowi6 *fl6);
  721. struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6,
  722. const struct in6_addr *final_dst);
  723. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  724. const struct in6_addr *final_dst);
  725. struct dst_entry *ip6_blackhole_route(struct net *net,
  726. struct dst_entry *orig_dst);
  727. /*
  728. * skb processing functions
  729. */
  730. int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  731. int ip6_forward(struct sk_buff *skb);
  732. int ip6_input(struct sk_buff *skb);
  733. int ip6_mc_input(struct sk_buff *skb);
  734. int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  735. int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  736. /*
  737. * Extension header (options) processing
  738. */
  739. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  740. u8 *proto, struct in6_addr **daddr_p);
  741. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  742. u8 *proto);
  743. int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp,
  744. __be16 *frag_offp);
  745. bool ipv6_ext_hdr(u8 nexthdr);
  746. enum {
  747. IP6_FH_F_FRAG = (1 << 0),
  748. IP6_FH_F_AUTH = (1 << 1),
  749. IP6_FH_F_SKIP_RH = (1 << 2),
  750. };
  751. /* find specified header and get offset to it */
  752. int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target,
  753. unsigned short *fragoff, int *fragflg);
  754. int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type);
  755. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  756. const struct ipv6_txoptions *opt,
  757. struct in6_addr *orig);
  758. /*
  759. * socket options (ipv6_sockglue.c)
  760. */
  761. int ipv6_setsockopt(struct sock *sk, int level, int optname,
  762. char __user *optval, unsigned int optlen);
  763. int ipv6_getsockopt(struct sock *sk, int level, int optname,
  764. char __user *optval, int __user *optlen);
  765. int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
  766. char __user *optval, unsigned int optlen);
  767. int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
  768. char __user *optval, int __user *optlen);
  769. int __ip6_datagram_connect(struct sock *sk, struct sockaddr *addr,
  770. int addr_len);
  771. int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len);
  772. int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *addr,
  773. int addr_len);
  774. int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
  775. void ip6_datagram_release_cb(struct sock *sk);
  776. int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
  777. int *addr_len);
  778. int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
  779. int *addr_len);
  780. void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
  781. u32 info, u8 *payload);
  782. void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info);
  783. void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu);
  784. int inet6_release(struct socket *sock);
  785. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len);
  786. int inet6_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len,
  787. int peer);
  788. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  789. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  790. struct sock *sk);
  791. /*
  792. * reassembly.c
  793. */
  794. extern const struct proto_ops inet6_stream_ops;
  795. extern const struct proto_ops inet6_dgram_ops;
  796. extern const struct proto_ops inet6_sockraw_ops;
  797. struct group_source_req;
  798. struct group_filter;
  799. int ip6_mc_source(int add, int omode, struct sock *sk,
  800. struct group_source_req *pgsr);
  801. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);
  802. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  803. struct group_filter __user *optval, int __user *optlen);
  804. #ifdef CONFIG_PROC_FS
  805. int ac6_proc_init(struct net *net);
  806. void ac6_proc_exit(struct net *net);
  807. int raw6_proc_init(void);
  808. void raw6_proc_exit(void);
  809. int tcp6_proc_init(struct net *net);
  810. void tcp6_proc_exit(struct net *net);
  811. int udp6_proc_init(struct net *net);
  812. void udp6_proc_exit(struct net *net);
  813. int udplite6_proc_init(void);
  814. void udplite6_proc_exit(void);
  815. int ipv6_misc_proc_init(void);
  816. void ipv6_misc_proc_exit(void);
  817. int snmp6_register_dev(struct inet6_dev *idev);
  818. int snmp6_unregister_dev(struct inet6_dev *idev);
  819. #else
  820. static inline int ac6_proc_init(struct net *net) { return 0; }
  821. static inline void ac6_proc_exit(struct net *net) { }
  822. static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; }
  823. static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
  824. #endif
  825. #ifdef CONFIG_SYSCTL
  826. extern struct ctl_table ipv6_route_table_template[];
  827. struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
  828. struct ctl_table *ipv6_route_sysctl_init(struct net *net);
  829. int ipv6_sysctl_register(void);
  830. void ipv6_sysctl_unregister(void);
  831. #endif
  832. int ipv6_sock_mc_join(struct sock *sk, int ifindex,
  833. const struct in6_addr *addr);
  834. int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
  835. const struct in6_addr *addr);
  836. #endif /* _NET_IPV6_H */