ipv6.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. */
  8. #ifndef _NET_IPV6_H
  9. #define _NET_IPV6_H
  10. #include <linux/ipv6.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/jhash.h>
  13. #include <linux/refcount.h>
  14. #include <linux/jump_label_ratelimit.h>
  15. #include <net/if_inet6.h>
  16. #include <net/flow.h>
  17. #include <net/flow_dissector.h>
  18. #include <net/inet_dscp.h>
  19. #include <net/snmp.h>
  20. #include <net/netns/hash.h>
  21. struct ip_tunnel_info;
  22. #define SIN6_LEN_RFC2133 24
  23. /*
  24. * NextHeader field of IPv6 header
  25. */
  26. #define NEXTHDR_HOP 0 /* Hop-by-hop option header. */
  27. #define NEXTHDR_IPV4 4 /* IPv4 in IPv6 */
  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. /* Limits on Hop-by-Hop and Destination options.
  45. *
  46. * Per RFC8200 there is no limit on the maximum number or lengths of options in
  47. * Hop-by-Hop or Destination options other then the packet must fit in an MTU.
  48. * We allow configurable limits in order to mitigate potential denial of
  49. * service attacks.
  50. *
  51. * There are three limits that may be set:
  52. * - Limit the number of options in a Hop-by-Hop or Destination options
  53. * extension header
  54. * - Limit the byte length of a Hop-by-Hop or Destination options extension
  55. * header
  56. * - Disallow unknown options
  57. *
  58. * The limits are expressed in corresponding sysctls:
  59. *
  60. * ipv6.sysctl.max_dst_opts_cnt
  61. * ipv6.sysctl.max_hbh_opts_cnt
  62. * ipv6.sysctl.max_dst_opts_len
  63. * ipv6.sysctl.max_hbh_opts_len
  64. *
  65. * max_*_opts_cnt is the number of TLVs that are allowed for Destination
  66. * options or Hop-by-Hop options. If the number is less than zero then unknown
  67. * TLVs are disallowed and the number of known options that are allowed is the
  68. * absolute value. Setting the value to INT_MAX indicates no limit.
  69. *
  70. * max_*_opts_len is the length limit in bytes of a Destination or
  71. * Hop-by-Hop options extension header. Setting the value to INT_MAX
  72. * indicates no length limit.
  73. *
  74. * If a limit is exceeded when processing an extension header the packet is
  75. * silently discarded.
  76. */
  77. /* Default limits for Hop-by-Hop and Destination options */
  78. #define IP6_DEFAULT_MAX_DST_OPTS_CNT 8
  79. #define IP6_DEFAULT_MAX_HBH_OPTS_CNT 8
  80. #define IP6_DEFAULT_MAX_DST_OPTS_LEN INT_MAX /* No limit */
  81. #define IP6_DEFAULT_MAX_HBH_OPTS_LEN INT_MAX /* No limit */
  82. /*
  83. * Addr type
  84. *
  85. * type - unicast | multicast
  86. * scope - local | site | global
  87. * v4 - compat
  88. * v4mapped
  89. * any
  90. * loopback
  91. */
  92. #define IPV6_ADDR_ANY 0x0000U
  93. #define IPV6_ADDR_UNICAST 0x0001U
  94. #define IPV6_ADDR_MULTICAST 0x0002U
  95. #define IPV6_ADDR_LOOPBACK 0x0010U
  96. #define IPV6_ADDR_LINKLOCAL 0x0020U
  97. #define IPV6_ADDR_SITELOCAL 0x0040U
  98. #define IPV6_ADDR_COMPATv4 0x0080U
  99. #define IPV6_ADDR_SCOPE_MASK 0x00f0U
  100. #define IPV6_ADDR_MAPPED 0x1000U
  101. /*
  102. * Addr scopes
  103. */
  104. #define IPV6_ADDR_MC_SCOPE(a) \
  105. ((a)->s6_addr[1] & 0x0f) /* nonstandard */
  106. #define __IPV6_ADDR_SCOPE_INVALID -1
  107. #define IPV6_ADDR_SCOPE_NODELOCAL 0x01
  108. #define IPV6_ADDR_SCOPE_LINKLOCAL 0x02
  109. #define IPV6_ADDR_SCOPE_SITELOCAL 0x05
  110. #define IPV6_ADDR_SCOPE_ORGLOCAL 0x08
  111. #define IPV6_ADDR_SCOPE_GLOBAL 0x0e
  112. /*
  113. * Addr flags
  114. */
  115. #define IPV6_ADDR_MC_FLAG_TRANSIENT(a) \
  116. ((a)->s6_addr[1] & 0x10)
  117. #define IPV6_ADDR_MC_FLAG_PREFIX(a) \
  118. ((a)->s6_addr[1] & 0x20)
  119. #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a) \
  120. ((a)->s6_addr[1] & 0x40)
  121. /*
  122. * fragmentation header
  123. */
  124. struct frag_hdr {
  125. __u8 nexthdr;
  126. __u8 reserved;
  127. __be16 frag_off;
  128. __be32 identification;
  129. };
  130. #define IP6_MF 0x0001
  131. #define IP6_OFFSET 0xFFF8
  132. struct ip6_fraglist_iter {
  133. struct ipv6hdr *tmp_hdr;
  134. struct sk_buff *frag;
  135. int offset;
  136. unsigned int hlen;
  137. __be32 frag_id;
  138. u8 nexthdr;
  139. };
  140. int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
  141. u8 nexthdr, __be32 frag_id,
  142. struct ip6_fraglist_iter *iter);
  143. void ip6_fraglist_prepare(struct sk_buff *skb, struct ip6_fraglist_iter *iter);
  144. static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter)
  145. {
  146. struct sk_buff *skb = iter->frag;
  147. iter->frag = skb->next;
  148. skb_mark_not_on_list(skb);
  149. return skb;
  150. }
  151. struct ip6_frag_state {
  152. u8 *prevhdr;
  153. unsigned int hlen;
  154. unsigned int mtu;
  155. unsigned int left;
  156. int offset;
  157. int ptr;
  158. int hroom;
  159. int troom;
  160. __be32 frag_id;
  161. u8 nexthdr;
  162. };
  163. void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
  164. unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
  165. u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state);
  166. struct sk_buff *ip6_frag_next(struct sk_buff *skb,
  167. struct ip6_frag_state *state);
  168. #define IP6_REPLY_MARK(net, mark) \
  169. ((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
  170. #include <net/sock.h>
  171. /* sysctls */
  172. extern int sysctl_mld_max_msf;
  173. extern int sysctl_mld_qrv;
  174. #define _DEVINC(net, statname, mod, idev, field) \
  175. ({ \
  176. struct inet6_dev *_idev = (idev); \
  177. if (likely(_idev != NULL)) \
  178. mod##SNMP_INC_STATS64((_idev)->stats.statname, (field));\
  179. mod##SNMP_INC_STATS64((net)->mib.statname##_statistics, (field));\
  180. })
  181. /* per device counters are atomic_long_t */
  182. #define _DEVINCATOMIC(net, statname, mod, idev, field) \
  183. ({ \
  184. struct inet6_dev *_idev = (idev); \
  185. if (likely(_idev != NULL)) \
  186. SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
  187. mod##SNMP_INC_STATS((net)->mib.statname##_statistics, (field));\
  188. })
  189. /* per device and per net counters are atomic_long_t */
  190. #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field) \
  191. ({ \
  192. struct inet6_dev *_idev = (idev); \
  193. if (likely(_idev != NULL)) \
  194. SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
  195. SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\
  196. })
  197. #define _DEVADD(net, statname, mod, idev, field, val) \
  198. ({ \
  199. struct inet6_dev *_idev = (idev); \
  200. unsigned long _field = (field); \
  201. unsigned long _val = (val); \
  202. if (likely(_idev != NULL)) \
  203. mod##SNMP_ADD_STATS((_idev)->stats.statname, _field, _val); \
  204. mod##SNMP_ADD_STATS((net)->mib.statname##_statistics, _field, _val);\
  205. })
  206. #define _DEVUPD(net, statname, mod, idev, field, val) \
  207. ({ \
  208. struct inet6_dev *_idev = (idev); \
  209. unsigned long _val = (val); \
  210. if (likely(_idev != NULL)) \
  211. mod##SNMP_UPD_PO_STATS((_idev)->stats.statname, field, _val); \
  212. mod##SNMP_UPD_PO_STATS((net)->mib.statname##_statistics, field, _val);\
  213. })
  214. /* MIBs */
  215. #define IP6_INC_STATS(net, idev,field) \
  216. _DEVINC(net, ipv6, , idev, field)
  217. #define __IP6_INC_STATS(net, idev,field) \
  218. _DEVINC(net, ipv6, __, idev, field)
  219. #define IP6_ADD_STATS(net, idev,field,val) \
  220. _DEVADD(net, ipv6, , idev, field, val)
  221. #define __IP6_ADD_STATS(net, idev,field,val) \
  222. _DEVADD(net, ipv6, __, idev, field, val)
  223. #define IP6_UPD_PO_STATS(net, idev,field,val) \
  224. _DEVUPD(net, ipv6, , idev, field, val)
  225. #define __IP6_UPD_PO_STATS(net, idev,field,val) \
  226. _DEVUPD(net, ipv6, __, idev, field, val)
  227. #define ICMP6_INC_STATS(net, idev, field) \
  228. _DEVINCATOMIC(net, icmpv6, , idev, field)
  229. #define __ICMP6_INC_STATS(net, idev, field) \
  230. _DEVINCATOMIC(net, icmpv6, __, idev, field)
  231. #define ICMP6MSGOUT_INC_STATS(net, idev, field) \
  232. _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
  233. #define ICMP6MSGIN_INC_STATS(net, idev, field) \
  234. _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field)
  235. struct ip6_ra_chain {
  236. struct ip6_ra_chain *next;
  237. struct sock *sk;
  238. int sel;
  239. void (*destructor)(struct sock *);
  240. };
  241. extern struct ip6_ra_chain *ip6_ra_chain;
  242. extern rwlock_t ip6_ra_lock;
  243. /*
  244. This structure is prepared by protocol, when parsing
  245. ancillary data and passed to IPv6.
  246. */
  247. struct ipv6_txoptions {
  248. refcount_t refcnt;
  249. /* Length of this structure */
  250. int tot_len;
  251. /* length of extension headers */
  252. __u16 opt_flen; /* after fragment hdr */
  253. __u16 opt_nflen; /* before fragment hdr */
  254. struct ipv6_opt_hdr *hopopt;
  255. struct ipv6_opt_hdr *dst0opt;
  256. struct ipv6_rt_hdr *srcrt; /* Routing Header */
  257. struct ipv6_opt_hdr *dst1opt;
  258. struct rcu_head rcu;
  259. /* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */
  260. };
  261. /* flowlabel_reflect sysctl values */
  262. enum flowlabel_reflect {
  263. FLOWLABEL_REFLECT_ESTABLISHED = 1,
  264. FLOWLABEL_REFLECT_TCP_RESET = 2,
  265. FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES = 4,
  266. };
  267. struct ip6_flowlabel {
  268. struct ip6_flowlabel __rcu *next;
  269. __be32 label;
  270. atomic_t users;
  271. struct in6_addr dst;
  272. struct ipv6_txoptions *opt;
  273. unsigned long linger;
  274. struct rcu_head rcu;
  275. u8 share;
  276. union {
  277. struct pid *pid;
  278. kuid_t uid;
  279. } owner;
  280. unsigned long lastuse;
  281. unsigned long expires;
  282. struct net *fl_net;
  283. };
  284. #define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
  285. #define IPV6_FLOWLABEL_MASK cpu_to_be32(0x000FFFFF)
  286. #define IPV6_FLOWLABEL_STATELESS_FLAG cpu_to_be32(0x00080000)
  287. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  288. #define IPV6_TCLASS_SHIFT 20
  289. struct ipv6_fl_socklist {
  290. struct ipv6_fl_socklist __rcu *next;
  291. struct ip6_flowlabel *fl;
  292. struct rcu_head rcu;
  293. };
  294. struct ipcm6_cookie {
  295. struct sockcm_cookie sockc;
  296. __s16 hlimit;
  297. __s16 tclass;
  298. __u16 gso_size;
  299. __s8 dontfrag;
  300. struct ipv6_txoptions *opt;
  301. };
  302. static inline void ipcm6_init_sk(struct ipcm6_cookie *ipc6,
  303. const struct sock *sk)
  304. {
  305. *ipc6 = (struct ipcm6_cookie) {
  306. .hlimit = -1,
  307. .tclass = inet6_sk(sk)->tclass,
  308. .dontfrag = inet6_test_bit(DONTFRAG, sk),
  309. };
  310. sockcm_init(&ipc6->sockc, sk);
  311. }
  312. static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
  313. {
  314. struct ipv6_txoptions *opt;
  315. rcu_read_lock();
  316. opt = rcu_dereference(np->opt);
  317. if (opt) {
  318. if (!refcount_inc_not_zero(&opt->refcnt))
  319. opt = NULL;
  320. else
  321. opt = rcu_pointer_handoff(opt);
  322. }
  323. rcu_read_unlock();
  324. return opt;
  325. }
  326. static inline void txopt_put(struct ipv6_txoptions *opt)
  327. {
  328. if (opt && refcount_dec_and_test(&opt->refcnt))
  329. kfree_rcu(opt, rcu);
  330. }
  331. #if IS_ENABLED(CONFIG_IPV6)
  332. struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label);
  333. extern struct static_key_false_deferred ipv6_flowlabel_exclusive;
  334. static inline struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk,
  335. __be32 label)
  336. {
  337. if (static_branch_unlikely(&ipv6_flowlabel_exclusive.key) &&
  338. READ_ONCE(sock_net(sk)->ipv6.flowlabel_has_excl))
  339. return __fl6_sock_lookup(sk, label) ? : ERR_PTR(-ENOENT);
  340. return NULL;
  341. }
  342. #endif
  343. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
  344. struct ip6_flowlabel *fl,
  345. struct ipv6_txoptions *fopt);
  346. void fl6_free_socklist(struct sock *sk);
  347. int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen);
  348. int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
  349. int flags);
  350. int ip6_flowlabel_init(void);
  351. void ip6_flowlabel_cleanup(void);
  352. bool ip6_autoflowlabel(struct net *net, const struct sock *sk);
  353. static inline void fl6_sock_release(struct ip6_flowlabel *fl)
  354. {
  355. if (fl)
  356. atomic_dec(&fl->users);
  357. }
  358. enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
  359. u8 code, __be32 info);
  360. void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  361. struct icmp6hdr *thdr, int len);
  362. int ip6_ra_control(struct sock *sk, int sel);
  363. int ipv6_parse_hopopts(struct sk_buff *skb);
  364. struct ipv6_txoptions *ipv6_dup_options(struct sock *sk,
  365. struct ipv6_txoptions *opt);
  366. struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
  367. struct ipv6_txoptions *opt,
  368. int newtype,
  369. struct ipv6_opt_hdr *newopt);
  370. struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  371. struct ipv6_txoptions *opt);
  372. static inline struct ipv6_txoptions *
  373. ipv6_fixup_options(struct ipv6_txoptions *opt_space, struct ipv6_txoptions *opt)
  374. {
  375. if (!opt)
  376. return NULL;
  377. return __ipv6_fixup_options(opt_space, opt);
  378. }
  379. bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
  380. const struct inet6_skb_parm *opt);
  381. struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
  382. struct ipv6_txoptions *opt);
  383. static inline bool ipv6_accept_ra(const struct inet6_dev *idev)
  384. {
  385. s32 accept_ra = READ_ONCE(idev->cnf.accept_ra);
  386. /* If forwarding is enabled, RA are not accepted unless the special
  387. * hybrid mode (accept_ra=2) is enabled.
  388. */
  389. return READ_ONCE(idev->cnf.forwarding) ? accept_ra == 2 :
  390. accept_ra;
  391. }
  392. #define IPV6_FRAG_HIGH_THRESH (4 * 1024*1024) /* 4194304 */
  393. #define IPV6_FRAG_LOW_THRESH (3 * 1024*1024) /* 3145728 */
  394. #define IPV6_FRAG_TIMEOUT (60 * HZ) /* 60 seconds */
  395. int __ipv6_addr_type(const struct in6_addr *addr);
  396. static inline int ipv6_addr_type(const struct in6_addr *addr)
  397. {
  398. return __ipv6_addr_type(addr) & 0xffff;
  399. }
  400. static inline int ipv6_addr_scope(const struct in6_addr *addr)
  401. {
  402. return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK;
  403. }
  404. static inline int __ipv6_addr_src_scope(int type)
  405. {
  406. return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16);
  407. }
  408. static inline int ipv6_addr_src_scope(const struct in6_addr *addr)
  409. {
  410. return __ipv6_addr_src_scope(__ipv6_addr_type(addr));
  411. }
  412. static inline bool __ipv6_addr_needs_scope_id(int type)
  413. {
  414. return type & IPV6_ADDR_LINKLOCAL ||
  415. (type & IPV6_ADDR_MULTICAST &&
  416. (type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)));
  417. }
  418. static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
  419. {
  420. return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
  421. }
  422. static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
  423. {
  424. return memcmp(a1, a2, sizeof(struct in6_addr));
  425. }
  426. static inline bool
  427. ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m,
  428. const struct in6_addr *a2)
  429. {
  430. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  431. const unsigned long *ul1 = (const unsigned long *)a1;
  432. const unsigned long *ulm = (const unsigned long *)m;
  433. const unsigned long *ul2 = (const unsigned long *)a2;
  434. return !!(((ul1[0] ^ ul2[0]) & ulm[0]) |
  435. ((ul1[1] ^ ul2[1]) & ulm[1]));
  436. #else
  437. return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) |
  438. ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) |
  439. ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) |
  440. ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3]));
  441. #endif
  442. }
  443. static inline void ipv6_addr_prefix(struct in6_addr *pfx,
  444. const struct in6_addr *addr,
  445. int plen)
  446. {
  447. /* caller must guarantee 0 <= plen <= 128 */
  448. int o = plen >> 3,
  449. b = plen & 0x7;
  450. memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr));
  451. memcpy(pfx->s6_addr, addr, o);
  452. if (b != 0)
  453. pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b);
  454. }
  455. static inline void ipv6_addr_prefix_copy(struct in6_addr *addr,
  456. const struct in6_addr *pfx,
  457. int plen)
  458. {
  459. /* caller must guarantee 0 <= plen <= 128 */
  460. int o = plen >> 3,
  461. b = plen & 0x7;
  462. memcpy(addr->s6_addr, pfx, o);
  463. if (b != 0) {
  464. addr->s6_addr[o] &= ~(0xff00 >> b);
  465. addr->s6_addr[o] |= (pfx->s6_addr[o] & (0xff00 >> b));
  466. }
  467. }
  468. static inline void __ipv6_addr_set_half(__be32 *addr,
  469. __be32 wh, __be32 wl)
  470. {
  471. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  472. #if defined(__BIG_ENDIAN)
  473. if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
  474. *(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
  475. return;
  476. }
  477. #elif defined(__LITTLE_ENDIAN)
  478. if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
  479. *(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
  480. return;
  481. }
  482. #endif
  483. #endif
  484. addr[0] = wh;
  485. addr[1] = wl;
  486. }
  487. static inline void ipv6_addr_set(struct in6_addr *addr,
  488. __be32 w1, __be32 w2,
  489. __be32 w3, __be32 w4)
  490. {
  491. __ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
  492. __ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
  493. }
  494. static inline bool ipv6_addr_equal(const struct in6_addr *a1,
  495. const struct in6_addr *a2)
  496. {
  497. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  498. const unsigned long *ul1 = (const unsigned long *)a1;
  499. const unsigned long *ul2 = (const unsigned long *)a2;
  500. return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
  501. #else
  502. return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
  503. (a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
  504. (a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
  505. (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0;
  506. #endif
  507. }
  508. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  509. static inline bool __ipv6_prefix_equal64_half(const __be64 *a1,
  510. const __be64 *a2,
  511. unsigned int len)
  512. {
  513. if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len))))
  514. return false;
  515. return true;
  516. }
  517. static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
  518. const struct in6_addr *addr2,
  519. unsigned int prefixlen)
  520. {
  521. const __be64 *a1 = (const __be64 *)addr1;
  522. const __be64 *a2 = (const __be64 *)addr2;
  523. if (prefixlen >= 64) {
  524. if (a1[0] ^ a2[0])
  525. return false;
  526. return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64);
  527. }
  528. return __ipv6_prefix_equal64_half(a1, a2, prefixlen);
  529. }
  530. #else
  531. static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
  532. const struct in6_addr *addr2,
  533. unsigned int prefixlen)
  534. {
  535. const __be32 *a1 = addr1->s6_addr32;
  536. const __be32 *a2 = addr2->s6_addr32;
  537. unsigned int pdw, pbi;
  538. /* check complete u32 in prefix */
  539. pdw = prefixlen >> 5;
  540. if (pdw && memcmp(a1, a2, pdw << 2))
  541. return false;
  542. /* check incomplete u32 in prefix */
  543. pbi = prefixlen & 0x1f;
  544. if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
  545. return false;
  546. return true;
  547. }
  548. #endif
  549. static inline bool ipv6_addr_any(const struct in6_addr *a)
  550. {
  551. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  552. const unsigned long *ul = (const unsigned long *)a;
  553. return (ul[0] | ul[1]) == 0UL;
  554. #else
  555. return (a->s6_addr32[0] | a->s6_addr32[1] |
  556. a->s6_addr32[2] | a->s6_addr32[3]) == 0;
  557. #endif
  558. }
  559. static inline u32 ipv6_addr_hash(const struct in6_addr *a)
  560. {
  561. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  562. const unsigned long *ul = (const unsigned long *)a;
  563. unsigned long x = ul[0] ^ ul[1];
  564. return (u32)(x ^ (x >> 32));
  565. #else
  566. return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^
  567. a->s6_addr32[2] ^ a->s6_addr32[3]);
  568. #endif
  569. }
  570. /* more secured version of ipv6_addr_hash() */
  571. static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval)
  572. {
  573. return jhash2((__force const u32 *)a->s6_addr32,
  574. ARRAY_SIZE(a->s6_addr32), initval);
  575. }
  576. static inline bool ipv6_addr_loopback(const struct in6_addr *a)
  577. {
  578. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  579. const __be64 *be = (const __be64 *)a;
  580. return (be[0] | (be[1] ^ cpu_to_be64(1))) == 0UL;
  581. #else
  582. return (a->s6_addr32[0] | a->s6_addr32[1] |
  583. a->s6_addr32[2] | (a->s6_addr32[3] ^ cpu_to_be32(1))) == 0;
  584. #endif
  585. }
  586. /*
  587. * Note that we must __force cast these to unsigned long to make sparse happy,
  588. * since all of the endian-annotated types are fixed size regardless of arch.
  589. */
  590. static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
  591. {
  592. return (
  593. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  594. *(unsigned long *)a |
  595. #else
  596. (__force unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
  597. #endif
  598. (__force unsigned long)(a->s6_addr32[2] ^
  599. cpu_to_be32(0x0000ffff))) == 0UL;
  600. }
  601. static inline bool ipv6_addr_v4mapped_loopback(const struct in6_addr *a)
  602. {
  603. return ipv6_addr_v4mapped(a) && ipv4_is_loopback(a->s6_addr32[3]);
  604. }
  605. static inline u32 ipv6_portaddr_hash(const struct net *net,
  606. const struct in6_addr *addr6,
  607. unsigned int port)
  608. {
  609. unsigned int hash, mix = net_hash_mix(net);
  610. if (ipv6_addr_any(addr6))
  611. hash = jhash_1word(0, mix);
  612. else if (ipv6_addr_v4mapped(addr6))
  613. hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
  614. else
  615. hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
  616. return hash ^ port;
  617. }
  618. /*
  619. * Check for a RFC 4843 ORCHID address
  620. * (Overlay Routable Cryptographic Hash Identifiers)
  621. */
  622. static inline bool ipv6_addr_orchid(const struct in6_addr *a)
  623. {
  624. return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010);
  625. }
  626. static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr)
  627. {
  628. return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000);
  629. }
  630. static inline void ipv6_addr_set_v4mapped(const __be32 addr,
  631. struct in6_addr *v4mapped)
  632. {
  633. ipv6_addr_set(v4mapped,
  634. 0, 0,
  635. htonl(0x0000FFFF),
  636. addr);
  637. }
  638. /*
  639. * find the first different bit between two addresses
  640. * length of address must be a multiple of 32bits
  641. */
  642. static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
  643. {
  644. const __be32 *a1 = token1, *a2 = token2;
  645. int i;
  646. addrlen >>= 2;
  647. for (i = 0; i < addrlen; i++) {
  648. __be32 xb = a1[i] ^ a2[i];
  649. if (xb)
  650. return i * 32 + 31 - __fls(ntohl(xb));
  651. }
  652. /*
  653. * we should *never* get to this point since that
  654. * would mean the addrs are equal
  655. *
  656. * However, we do get to it 8) And exactly, when
  657. * addresses are equal 8)
  658. *
  659. * ip route add 1111::/128 via ...
  660. * ip route add 1111::/64 via ...
  661. * and we are here.
  662. *
  663. * Ideally, this function should stop comparison
  664. * at prefix length. It does not, but it is still OK,
  665. * if returned value is greater than prefix length.
  666. * --ANK (980803)
  667. */
  668. return addrlen << 5;
  669. }
  670. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  671. static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen)
  672. {
  673. const __be64 *a1 = token1, *a2 = token2;
  674. int i;
  675. addrlen >>= 3;
  676. for (i = 0; i < addrlen; i++) {
  677. __be64 xb = a1[i] ^ a2[i];
  678. if (xb)
  679. return i * 64 + 63 - __fls(be64_to_cpu(xb));
  680. }
  681. return addrlen << 6;
  682. }
  683. #endif
  684. static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen)
  685. {
  686. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  687. if (__builtin_constant_p(addrlen) && !(addrlen & 7))
  688. return __ipv6_addr_diff64(token1, token2, addrlen);
  689. #endif
  690. return __ipv6_addr_diff32(token1, token2, addrlen);
  691. }
  692. static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
  693. {
  694. return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
  695. }
  696. __be32 ipv6_select_ident(struct net *net,
  697. const struct in6_addr *daddr,
  698. const struct in6_addr *saddr);
  699. __be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb);
  700. int ip6_dst_hoplimit(struct dst_entry *dst);
  701. static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6,
  702. struct dst_entry *dst)
  703. {
  704. int hlimit;
  705. if (ipv6_addr_is_multicast(&fl6->daddr))
  706. hlimit = READ_ONCE(np->mcast_hops);
  707. else
  708. hlimit = READ_ONCE(np->hop_limit);
  709. if (hlimit < 0)
  710. hlimit = ip6_dst_hoplimit(dst);
  711. return hlimit;
  712. }
  713. /* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store
  714. * Equivalent to : flow->v6addrs.src = iph->saddr;
  715. * flow->v6addrs.dst = iph->daddr;
  716. */
  717. static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow,
  718. const struct ipv6hdr *iph)
  719. {
  720. BUILD_BUG_ON(offsetof(typeof(flow->addrs), v6addrs.dst) !=
  721. offsetof(typeof(flow->addrs), v6addrs.src) +
  722. sizeof(flow->addrs.v6addrs.src));
  723. memcpy(&flow->addrs.v6addrs, &iph->addrs, sizeof(flow->addrs.v6addrs));
  724. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  725. }
  726. #if IS_ENABLED(CONFIG_IPV6)
  727. static inline bool ipv6_can_nonlocal_bind(const struct net *net,
  728. const struct inet_sock *inet)
  729. {
  730. return READ_ONCE(net->ipv6.sysctl.ip_nonlocal_bind) ||
  731. test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
  732. test_bit(INET_FLAGS_TRANSPARENT, &inet->inet_flags);
  733. }
  734. /* Sysctl settings for net ipv6.auto_flowlabels */
  735. #define IP6_AUTO_FLOW_LABEL_OFF 0
  736. #define IP6_AUTO_FLOW_LABEL_OPTOUT 1
  737. #define IP6_AUTO_FLOW_LABEL_OPTIN 2
  738. #define IP6_AUTO_FLOW_LABEL_FORCED 3
  739. #define IP6_AUTO_FLOW_LABEL_MAX IP6_AUTO_FLOW_LABEL_FORCED
  740. #define IP6_DEFAULT_AUTO_FLOW_LABELS IP6_AUTO_FLOW_LABEL_OPTOUT
  741. static inline __be32 ip6_make_flowlabel(const struct net *net,
  742. struct sk_buff *skb,
  743. __be32 flowlabel, bool autolabel,
  744. struct flowi6 *fl6)
  745. {
  746. u8 auto_flowlabels;
  747. u32 hash;
  748. /* @flowlabel may include more than a flow label, eg, the traffic class.
  749. * Here we want only the flow label value.
  750. */
  751. flowlabel &= IPV6_FLOWLABEL_MASK;
  752. if (flowlabel)
  753. return flowlabel;
  754. auto_flowlabels = READ_ONCE(net->ipv6.sysctl.auto_flowlabels);
  755. if (auto_flowlabels == IP6_AUTO_FLOW_LABEL_OFF ||
  756. (!autolabel && auto_flowlabels != IP6_AUTO_FLOW_LABEL_FORCED))
  757. return flowlabel;
  758. hash = skb_get_hash_flowi6(skb, fl6);
  759. /* Since this is being sent on the wire obfuscate hash a bit
  760. * to minimize possibility that any useful information to an
  761. * attacker is leaked. Only lower 20 bits are relevant.
  762. */
  763. hash = rol32(hash, 16);
  764. flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
  765. if (READ_ONCE(net->ipv6.sysctl.flowlabel_state_ranges))
  766. flowlabel |= IPV6_FLOWLABEL_STATELESS_FLAG;
  767. return flowlabel;
  768. }
  769. static inline int ip6_default_np_autolabel(const struct net *net)
  770. {
  771. switch (READ_ONCE(net->ipv6.sysctl.auto_flowlabels)) {
  772. case IP6_AUTO_FLOW_LABEL_OFF:
  773. case IP6_AUTO_FLOW_LABEL_OPTIN:
  774. default:
  775. return 0;
  776. case IP6_AUTO_FLOW_LABEL_OPTOUT:
  777. case IP6_AUTO_FLOW_LABEL_FORCED:
  778. return 1;
  779. }
  780. }
  781. #else
  782. static inline __be32 ip6_make_flowlabel(const struct net *net, struct sk_buff *skb,
  783. __be32 flowlabel, bool autolabel,
  784. struct flowi6 *fl6)
  785. {
  786. return flowlabel;
  787. }
  788. static inline int ip6_default_np_autolabel(const struct net *net)
  789. {
  790. return 0;
  791. }
  792. #endif
  793. #if IS_ENABLED(CONFIG_IPV6)
  794. static inline int ip6_multipath_hash_policy(const struct net *net)
  795. {
  796. return READ_ONCE(net->ipv6.sysctl.multipath_hash_policy);
  797. }
  798. static inline u32 ip6_multipath_hash_fields(const struct net *net)
  799. {
  800. return READ_ONCE(net->ipv6.sysctl.multipath_hash_fields);
  801. }
  802. #else
  803. static inline int ip6_multipath_hash_policy(const struct net *net)
  804. {
  805. return 0;
  806. }
  807. static inline u32 ip6_multipath_hash_fields(const struct net *net)
  808. {
  809. return 0;
  810. }
  811. #endif
  812. /*
  813. * Header manipulation
  814. */
  815. static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
  816. __be32 flowlabel)
  817. {
  818. *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
  819. }
  820. static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
  821. {
  822. return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
  823. }
  824. static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
  825. {
  826. return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
  827. }
  828. static inline u8 ip6_tclass(__be32 flowinfo)
  829. {
  830. return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT;
  831. }
  832. static inline dscp_t ip6_dscp(__be32 flowinfo)
  833. {
  834. return inet_dsfield_to_dscp(ip6_tclass(flowinfo));
  835. }
  836. static inline __be32 ip6_make_flowinfo(unsigned int tclass, __be32 flowlabel)
  837. {
  838. return htonl(tclass << IPV6_TCLASS_SHIFT) | flowlabel;
  839. }
  840. static inline __be32 flowi6_get_flowlabel(const struct flowi6 *fl6)
  841. {
  842. return fl6->flowlabel & IPV6_FLOWLABEL_MASK;
  843. }
  844. /*
  845. * Prototypes exported by ipv6
  846. */
  847. /*
  848. * rcv function (called from netdevice level)
  849. */
  850. int ipv6_rcv(struct sk_buff *skb, struct net_device *dev,
  851. struct packet_type *pt, struct net_device *orig_dev);
  852. void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
  853. struct net_device *orig_dev);
  854. int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  855. /*
  856. * upper-layer output functions
  857. */
  858. int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  859. __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority);
  860. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
  861. int ip6_append_data(struct sock *sk,
  862. int getfrag(void *from, char *to, int offset, int len,
  863. int odd, struct sk_buff *skb),
  864. void *from, size_t length, int transhdrlen,
  865. struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
  866. struct rt6_info *rt, unsigned int flags);
  867. int ip6_push_pending_frames(struct sock *sk);
  868. void ip6_flush_pending_frames(struct sock *sk);
  869. int ip6_send_skb(struct sk_buff *skb);
  870. struct sk_buff *__ip6_make_skb(struct sock *sk, struct sk_buff_head *queue,
  871. struct inet_cork_full *cork);
  872. struct sk_buff *ip6_make_skb(struct sock *sk,
  873. int getfrag(void *from, char *to, int offset,
  874. int len, int odd, struct sk_buff *skb),
  875. void *from, size_t length, int transhdrlen,
  876. struct ipcm6_cookie *ipc6,
  877. struct rt6_info *rt, unsigned int flags,
  878. struct inet_cork_full *cork);
  879. static inline struct sk_buff *ip6_finish_skb(struct sock *sk)
  880. {
  881. return __ip6_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork);
  882. }
  883. int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
  884. struct flowi6 *fl6);
  885. struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
  886. const struct in6_addr *final_dst);
  887. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  888. const struct in6_addr *final_dst,
  889. bool connected);
  890. struct dst_entry *ip6_blackhole_route(struct net *net,
  891. struct dst_entry *orig_dst);
  892. /*
  893. * skb processing functions
  894. */
  895. int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  896. int ip6_forward(struct sk_buff *skb);
  897. int ip6_input(struct sk_buff *skb);
  898. int ip6_mc_input(struct sk_buff *skb);
  899. void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
  900. bool have_final);
  901. int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  902. int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  903. /*
  904. * Extension header (options) processing
  905. */
  906. u8 ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  907. u8 proto, struct in6_addr **daddr_p,
  908. struct in6_addr *saddr);
  909. u8 ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  910. u8 proto);
  911. int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp,
  912. __be16 *frag_offp);
  913. bool ipv6_ext_hdr(u8 nexthdr);
  914. enum {
  915. IP6_FH_F_FRAG = (1 << 0),
  916. IP6_FH_F_AUTH = (1 << 1),
  917. IP6_FH_F_SKIP_RH = (1 << 2),
  918. };
  919. /* find specified header and get offset to it */
  920. int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target,
  921. unsigned short *fragoff, int *fragflg);
  922. int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type);
  923. struct in6_addr *__fl6_update_dst(struct flowi6 *fl6,
  924. const struct ipv6_txoptions *opt,
  925. struct in6_addr *orig);
  926. static inline struct in6_addr *
  927. fl6_update_dst(struct flowi6 *fl6, const struct ipv6_txoptions *opt,
  928. struct in6_addr *orig)
  929. {
  930. if (likely(!opt))
  931. return NULL;
  932. return __fl6_update_dst(fl6, opt, orig);
  933. }
  934. /*
  935. * socket options (ipv6_sockglue.c)
  936. */
  937. DECLARE_STATIC_KEY_FALSE(ip6_min_hopcount);
  938. int do_ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  939. unsigned int optlen);
  940. int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  941. unsigned int optlen);
  942. int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
  943. sockptr_t optval, sockptr_t optlen);
  944. int ipv6_getsockopt(struct sock *sk, int level, int optname,
  945. char __user *optval, int __user *optlen);
  946. int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,
  947. int addr_len);
  948. int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr, int addr_len);
  949. int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *addr,
  950. int addr_len);
  951. int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
  952. void ip6_datagram_release_cb(struct sock *sk);
  953. int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
  954. int *addr_len);
  955. int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
  956. int *addr_len);
  957. void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
  958. u32 info, u8 *payload);
  959. void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info);
  960. void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu);
  961. void inet6_cleanup_sock(struct sock *sk);
  962. void inet6_sock_destruct(struct sock *sk);
  963. int inet6_release(struct socket *sock);
  964. int inet6_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len);
  965. int inet6_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
  966. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  967. int peer);
  968. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  969. int inet6_compat_ioctl(struct socket *sock, unsigned int cmd,
  970. unsigned long arg);
  971. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  972. struct sock *sk);
  973. int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size);
  974. int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  975. int flags);
  976. /*
  977. * reassembly.c
  978. */
  979. extern const struct proto_ops inet6_stream_ops;
  980. extern const struct proto_ops inet6_dgram_ops;
  981. extern const struct proto_ops inet6_sockraw_ops;
  982. struct group_source_req;
  983. struct group_filter;
  984. int ip6_mc_source(int add, int omode, struct sock *sk,
  985. struct group_source_req *pgsr);
  986. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
  987. struct sockaddr_storage *list);
  988. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  989. sockptr_t optval, size_t ss_offset);
  990. #ifdef CONFIG_PROC_FS
  991. int ac6_proc_init(struct net *net);
  992. void ac6_proc_exit(struct net *net);
  993. int raw6_proc_init(void);
  994. void raw6_proc_exit(void);
  995. int tcp6_proc_init(struct net *net);
  996. void tcp6_proc_exit(struct net *net);
  997. int udp6_proc_init(struct net *net);
  998. void udp6_proc_exit(struct net *net);
  999. int udplite6_proc_init(void);
  1000. void udplite6_proc_exit(void);
  1001. int ipv6_misc_proc_init(void);
  1002. void ipv6_misc_proc_exit(void);
  1003. int snmp6_register_dev(struct inet6_dev *idev);
  1004. int snmp6_unregister_dev(struct inet6_dev *idev);
  1005. #else
  1006. static inline int ac6_proc_init(struct net *net) { return 0; }
  1007. static inline void ac6_proc_exit(struct net *net) { }
  1008. static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; }
  1009. static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
  1010. #endif
  1011. #ifdef CONFIG_SYSCTL
  1012. struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
  1013. size_t ipv6_icmp_sysctl_table_size(void);
  1014. struct ctl_table *ipv6_route_sysctl_init(struct net *net);
  1015. size_t ipv6_route_sysctl_table_size(struct net *net);
  1016. int ipv6_sysctl_register(void);
  1017. void ipv6_sysctl_unregister(void);
  1018. #endif
  1019. int ipv6_sock_mc_join(struct sock *sk, int ifindex,
  1020. const struct in6_addr *addr);
  1021. int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
  1022. const struct in6_addr *addr, unsigned int mode);
  1023. int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
  1024. const struct in6_addr *addr);
  1025. static inline int ip6_sock_set_v6only(struct sock *sk)
  1026. {
  1027. int ret = 0;
  1028. lock_sock(sk);
  1029. if (inet_sk(sk)->inet_num)
  1030. ret = -EINVAL;
  1031. else
  1032. sk->sk_ipv6only = true;
  1033. release_sock(sk);
  1034. return ret;
  1035. }
  1036. static inline void ip6_sock_set_recverr(struct sock *sk)
  1037. {
  1038. inet6_set_bit(RECVERR6, sk);
  1039. }
  1040. #define IPV6_PREFER_SRC_MASK (IPV6_PREFER_SRC_TMP | IPV6_PREFER_SRC_PUBLIC | \
  1041. IPV6_PREFER_SRC_COA)
  1042. static inline int ip6_sock_set_addr_preferences(struct sock *sk, int val)
  1043. {
  1044. unsigned int prefmask = ~IPV6_PREFER_SRC_MASK;
  1045. unsigned int pref = 0;
  1046. /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
  1047. switch (val & (IPV6_PREFER_SRC_PUBLIC |
  1048. IPV6_PREFER_SRC_TMP |
  1049. IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
  1050. case IPV6_PREFER_SRC_PUBLIC:
  1051. pref |= IPV6_PREFER_SRC_PUBLIC;
  1052. prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
  1053. IPV6_PREFER_SRC_TMP);
  1054. break;
  1055. case IPV6_PREFER_SRC_TMP:
  1056. pref |= IPV6_PREFER_SRC_TMP;
  1057. prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
  1058. IPV6_PREFER_SRC_TMP);
  1059. break;
  1060. case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
  1061. prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
  1062. IPV6_PREFER_SRC_TMP);
  1063. break;
  1064. case 0:
  1065. break;
  1066. default:
  1067. return -EINVAL;
  1068. }
  1069. /* check HOME/COA conflicts */
  1070. switch (val & (IPV6_PREFER_SRC_HOME | IPV6_PREFER_SRC_COA)) {
  1071. case IPV6_PREFER_SRC_HOME:
  1072. prefmask &= ~IPV6_PREFER_SRC_COA;
  1073. break;
  1074. case IPV6_PREFER_SRC_COA:
  1075. pref |= IPV6_PREFER_SRC_COA;
  1076. break;
  1077. case 0:
  1078. break;
  1079. default:
  1080. return -EINVAL;
  1081. }
  1082. /* check CGA/NONCGA conflicts */
  1083. switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
  1084. case IPV6_PREFER_SRC_CGA:
  1085. case IPV6_PREFER_SRC_NONCGA:
  1086. case 0:
  1087. break;
  1088. default:
  1089. return -EINVAL;
  1090. }
  1091. WRITE_ONCE(inet6_sk(sk)->srcprefs,
  1092. (READ_ONCE(inet6_sk(sk)->srcprefs) & prefmask) | pref);
  1093. return 0;
  1094. }
  1095. static inline void ip6_sock_set_recvpktinfo(struct sock *sk)
  1096. {
  1097. lock_sock(sk);
  1098. inet6_sk(sk)->rxopt.bits.rxinfo = true;
  1099. release_sock(sk);
  1100. }
  1101. #define IPV6_ADDR_WORDS 4
  1102. static inline void ipv6_addr_cpu_to_be32(__be32 *dst, const u32 *src)
  1103. {
  1104. cpu_to_be32_array(dst, src, IPV6_ADDR_WORDS);
  1105. }
  1106. static inline void ipv6_addr_be32_to_cpu(u32 *dst, const __be32 *src)
  1107. {
  1108. be32_to_cpu_array(dst, src, IPV6_ADDR_WORDS);
  1109. }
  1110. #endif /* _NET_IPV6_H */