ipv6.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #ifndef _IPV6_H
  2. #define _IPV6_H
  3. #include <linux/types.h>
  4. #include <linux/in6.h>
  5. #include <asm/byteorder.h>
  6. /* The latest drafts declared increase in minimal mtu up to 1280. */
  7. #define IPV6_MIN_MTU 1280
  8. /*
  9. * Advanced API
  10. * source interface/address selection, source routing, etc...
  11. * *under construction*
  12. */
  13. struct in6_pktinfo {
  14. struct in6_addr ipi6_addr;
  15. int ipi6_ifindex;
  16. };
  17. struct ip6_mtuinfo {
  18. struct sockaddr_in6 ip6m_addr;
  19. __u32 ip6m_mtu;
  20. };
  21. struct in6_ifreq {
  22. struct in6_addr ifr6_addr;
  23. __u32 ifr6_prefixlen;
  24. int ifr6_ifindex;
  25. };
  26. #define IPV6_SRCRT_STRICT 0x01 /* Deprecated; will be removed */
  27. #define IPV6_SRCRT_TYPE_0 0 /* Deprecated; will be removed */
  28. #define IPV6_SRCRT_TYPE_2 2 /* IPv6 type 2 Routing Header */
  29. /*
  30. * routing header
  31. */
  32. struct ipv6_rt_hdr {
  33. __u8 nexthdr;
  34. __u8 hdrlen;
  35. __u8 type;
  36. __u8 segments_left;
  37. /*
  38. * type specific data
  39. * variable length field
  40. */
  41. };
  42. struct ipv6_opt_hdr {
  43. __u8 nexthdr;
  44. __u8 hdrlen;
  45. /*
  46. * TLV encoded option data follows.
  47. */
  48. } __attribute__((packed)); /* required for some archs */
  49. #define ipv6_destopt_hdr ipv6_opt_hdr
  50. #define ipv6_hopopt_hdr ipv6_opt_hdr
  51. #ifdef __KERNEL__
  52. #define ipv6_optlen(p) (((p)->hdrlen+1) << 3)
  53. #endif
  54. /*
  55. * routing header type 0 (used in cmsghdr struct)
  56. */
  57. struct rt0_hdr {
  58. struct ipv6_rt_hdr rt_hdr;
  59. __u32 reserved;
  60. struct in6_addr addr[0];
  61. #define rt0_type rt_hdr.type
  62. };
  63. /*
  64. * routing header type 2
  65. */
  66. struct rt2_hdr {
  67. struct ipv6_rt_hdr rt_hdr;
  68. __u32 reserved;
  69. struct in6_addr addr;
  70. #define rt2_type rt_hdr.type
  71. };
  72. /*
  73. * home address option in destination options header
  74. */
  75. struct ipv6_destopt_hao {
  76. __u8 type;
  77. __u8 length;
  78. struct in6_addr addr;
  79. } __attribute__((packed));
  80. /*
  81. * IPv6 fixed header
  82. *
  83. * BEWARE, it is incorrect. The first 4 bits of flow_lbl
  84. * are glued to priority now, forming "class".
  85. */
  86. struct ipv6hdr {
  87. #if defined(__LITTLE_ENDIAN_BITFIELD)
  88. __u8 priority:4,
  89. version:4;
  90. #elif defined(__BIG_ENDIAN_BITFIELD)
  91. __u8 version:4,
  92. priority:4;
  93. #else
  94. #error "Please fix <asm/byteorder.h>"
  95. #endif
  96. __u8 flow_lbl[3];
  97. __be16 payload_len;
  98. __u8 nexthdr;
  99. __u8 hop_limit;
  100. struct in6_addr saddr;
  101. struct in6_addr daddr;
  102. };
  103. #ifdef __KERNEL__
  104. /*
  105. * This structure contains configuration options per IPv6 link.
  106. */
  107. struct ipv6_devconf {
  108. __s32 forwarding;
  109. __s32 hop_limit;
  110. __s32 mtu6;
  111. __s32 accept_ra;
  112. __s32 accept_redirects;
  113. __s32 autoconf;
  114. __s32 dad_transmits;
  115. __s32 rtr_solicits;
  116. __s32 rtr_solicit_interval;
  117. __s32 rtr_solicit_delay;
  118. __s32 force_mld_version;
  119. #ifdef CONFIG_IPV6_PRIVACY
  120. __s32 use_tempaddr;
  121. __s32 temp_valid_lft;
  122. __s32 temp_prefered_lft;
  123. __s32 regen_max_retry;
  124. __s32 max_desync_factor;
  125. #endif
  126. __s32 max_addresses;
  127. __s32 accept_ra_defrtr;
  128. __s32 accept_ra_pinfo;
  129. #ifdef CONFIG_IPV6_ROUTER_PREF
  130. __s32 accept_ra_rtr_pref;
  131. __s32 rtr_probe_interval;
  132. #ifdef CONFIG_IPV6_ROUTE_INFO
  133. __s32 accept_ra_rt_info_max_plen;
  134. #endif
  135. #endif
  136. __s32 accept_ra_rt_table;
  137. __s32 proxy_ndp;
  138. __s32 accept_source_route;
  139. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  140. __s32 optimistic_dad;
  141. __s32 use_optimistic;
  142. #endif
  143. #ifdef CONFIG_IPV6_MROUTE
  144. __s32 mc_forwarding;
  145. #endif
  146. __s32 disable_ipv6;
  147. __s32 drop_unicast_in_l2_multicast;
  148. __s32 accept_dad;
  149. __s32 force_tllao;
  150. __s32 accept_ra_prefix_route;
  151. __s32 accept_ra_mtu;
  152. __s32 use_oif_addrs_only;
  153. __s32 drop_unsolicited_na;
  154. void *sysctl;
  155. };
  156. struct ipv6_params {
  157. __s32 disable_ipv6;
  158. __s32 autoconf;
  159. };
  160. extern struct ipv6_params ipv6_defaults;
  161. #endif
  162. /* index values for the variables in ipv6_devconf */
  163. enum {
  164. DEVCONF_FORWARDING = 0,
  165. DEVCONF_HOPLIMIT,
  166. DEVCONF_MTU6,
  167. DEVCONF_ACCEPT_RA,
  168. DEVCONF_ACCEPT_REDIRECTS,
  169. DEVCONF_AUTOCONF,
  170. DEVCONF_DAD_TRANSMITS,
  171. DEVCONF_RTR_SOLICITS,
  172. DEVCONF_RTR_SOLICIT_INTERVAL,
  173. DEVCONF_RTR_SOLICIT_DELAY,
  174. DEVCONF_USE_TEMPADDR,
  175. DEVCONF_TEMP_VALID_LFT,
  176. DEVCONF_TEMP_PREFERED_LFT,
  177. DEVCONF_REGEN_MAX_RETRY,
  178. DEVCONF_MAX_DESYNC_FACTOR,
  179. DEVCONF_MAX_ADDRESSES,
  180. DEVCONF_FORCE_MLD_VERSION,
  181. DEVCONF_ACCEPT_RA_DEFRTR,
  182. DEVCONF_ACCEPT_RA_PINFO,
  183. DEVCONF_ACCEPT_RA_RTR_PREF,
  184. DEVCONF_RTR_PROBE_INTERVAL,
  185. DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN,
  186. DEVCONF_PROXY_NDP,
  187. DEVCONF_OPTIMISTIC_DAD,
  188. DEVCONF_ACCEPT_SOURCE_ROUTE,
  189. DEVCONF_MC_FORWARDING,
  190. DEVCONF_DISABLE_IPV6,
  191. DEVCONF_ACCEPT_DAD,
  192. DEVCONF_FORCE_TLLAO,
  193. DEVCONF_ACCEPT_RA_PREFIX_ROUTE,
  194. DEVCONF_ACCEPT_RA_RT_TABLE,
  195. DEVCONF_ACCEPT_RA_MTU,
  196. DEVCONF_USE_OPTIMISTIC,
  197. DEVCONF_USE_OIF_ADDRS_ONLY,
  198. DEVCONF_DROP_UNICAST_IN_L2_MULTICAST,
  199. DEVCONF_DROP_UNSOLICITED_NA,
  200. DEVCONF_MAX
  201. };
  202. #ifdef __KERNEL__
  203. #include <linux/icmpv6.h>
  204. #include <linux/tcp.h>
  205. #include <linux/udp.h>
  206. #include <net/inet_sock.h>
  207. static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
  208. {
  209. return (struct ipv6hdr *)skb_network_header(skb);
  210. }
  211. static inline struct ipv6hdr *ipipv6_hdr(const struct sk_buff *skb)
  212. {
  213. return (struct ipv6hdr *)skb_transport_header(skb);
  214. }
  215. static inline __u8 ipv6_tclass(const struct ipv6hdr *iph)
  216. {
  217. return (ntohl(*(__be32 *)iph) >> 20) & 0xff;
  218. }
  219. /*
  220. This structure contains results of exthdrs parsing
  221. as offsets from skb->nh.
  222. */
  223. struct inet6_skb_parm {
  224. int iif;
  225. __u16 ra;
  226. __u16 hop;
  227. __u16 dst0;
  228. __u16 srcrt;
  229. __u16 dst1;
  230. __u16 lastopt;
  231. __u16 nhoff;
  232. __u16 flags;
  233. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  234. __u16 dsthao;
  235. #endif
  236. __u16 frag_max_size;
  237. #define IP6SKB_XFRM_TRANSFORMED 1
  238. #define IP6SKB_FORWARDED 2
  239. #define IP6SKB_REROUTED 4
  240. #define IP6SKB_FRAGMENTED 16
  241. };
  242. #define IP6CB(skb) ((struct inet6_skb_parm*)((skb)->cb))
  243. #define IP6CBMTU(skb) ((struct ip6_mtuinfo *)((skb)->cb))
  244. static inline int inet6_iif(const struct sk_buff *skb)
  245. {
  246. return IP6CB(skb)->iif;
  247. }
  248. struct inet6_request_sock {
  249. struct in6_addr loc_addr;
  250. struct in6_addr rmt_addr;
  251. struct sk_buff *pktopts;
  252. int iif;
  253. };
  254. struct tcp6_request_sock {
  255. struct tcp_request_sock tcp6rsk_tcp;
  256. struct inet6_request_sock tcp6rsk_inet6;
  257. };
  258. struct ipv6_mc_socklist;
  259. struct ipv6_ac_socklist;
  260. struct ipv6_fl_socklist;
  261. /**
  262. * struct ipv6_pinfo - ipv6 private area
  263. *
  264. * In the struct sock hierarchy (tcp6_sock, upd6_sock, etc)
  265. * this _must_ be the last member, so that inet6_sk_generic
  266. * is able to calculate its offset from the base struct sock
  267. * by using the struct proto->slab_obj_size member. -acme
  268. */
  269. struct ipv6_pinfo {
  270. struct in6_addr saddr;
  271. struct in6_addr rcv_saddr;
  272. struct in6_addr daddr;
  273. struct in6_pktinfo sticky_pktinfo;
  274. struct in6_addr *daddr_cache;
  275. #ifdef CONFIG_IPV6_SUBTREES
  276. struct in6_addr *saddr_cache;
  277. #endif
  278. __be32 flow_label;
  279. __u32 frag_size;
  280. /*
  281. * Packed in 16bits.
  282. * Omit one shift by by putting the signed field at MSB.
  283. */
  284. #if defined(__BIG_ENDIAN_BITFIELD)
  285. __s16 hop_limit:9;
  286. __u16 __unused_1:7;
  287. #else
  288. __u16 __unused_1:7;
  289. __s16 hop_limit:9;
  290. #endif
  291. #if defined(__BIG_ENDIAN_BITFIELD)
  292. /* Packed in 16bits. */
  293. __s16 mcast_hops:9;
  294. __u16 __unused_2:6,
  295. mc_loop:1;
  296. #else
  297. __u16 mc_loop:1,
  298. __unused_2:6;
  299. __s16 mcast_hops:9;
  300. #endif
  301. int ucast_oif;
  302. int mcast_oif;
  303. /* pktoption flags */
  304. union {
  305. struct {
  306. __u16 srcrt:1,
  307. osrcrt:1,
  308. rxinfo:1,
  309. rxoinfo:1,
  310. rxhlim:1,
  311. rxohlim:1,
  312. hopopts:1,
  313. ohopopts:1,
  314. dstopts:1,
  315. odstopts:1,
  316. rxflow:1,
  317. rxtclass:1,
  318. rxpmtu:1,
  319. rxorigdstaddr:1;
  320. /* 2 bits hole */
  321. } bits;
  322. __u16 all;
  323. } rxopt;
  324. /* sockopt flags */
  325. __u16 recverr:1,
  326. sndflow:1,
  327. pmtudisc:2,
  328. ipv6only:1,
  329. srcprefs:3, /* 001: prefer temporary address
  330. * 010: prefer public address
  331. * 100: prefer care-of address
  332. */
  333. dontfrag:1;
  334. __u8 min_hopcount;
  335. __u8 tclass;
  336. __u8 rcv_tclass;
  337. __u32 dst_cookie;
  338. struct ipv6_mc_socklist __rcu *ipv6_mc_list;
  339. struct ipv6_ac_socklist *ipv6_ac_list;
  340. struct ipv6_fl_socklist *ipv6_fl_list;
  341. struct ipv6_txoptions __rcu *opt;
  342. struct sk_buff *pktoptions;
  343. struct sk_buff *rxpmtu;
  344. struct {
  345. struct ipv6_txoptions *opt;
  346. u8 hop_limit;
  347. u8 tclass;
  348. } cork;
  349. };
  350. /* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
  351. struct raw6_sock {
  352. /* inet_sock has to be the first member of raw6_sock */
  353. struct inet_sock inet;
  354. __u32 checksum; /* perform checksum */
  355. __u32 offset; /* checksum offset */
  356. struct icmp6_filter filter;
  357. __u32 ip6mr_table;
  358. /* ipv6_pinfo has to be the last member of raw6_sock, see inet6_sk_generic */
  359. struct ipv6_pinfo inet6;
  360. };
  361. struct udp6_sock {
  362. struct udp_sock udp;
  363. /* ipv6_pinfo has to be the last member of udp6_sock, see inet6_sk_generic */
  364. struct ipv6_pinfo inet6;
  365. };
  366. struct tcp6_sock {
  367. struct tcp_sock tcp;
  368. /* ipv6_pinfo has to be the last member of tcp6_sock, see inet6_sk_generic */
  369. struct ipv6_pinfo inet6;
  370. };
  371. extern int inet6_sk_rebuild_header(struct sock *sk);
  372. #if IS_ENABLED(CONFIG_IPV6)
  373. static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
  374. {
  375. return inet_sk(__sk)->pinet6;
  376. }
  377. static inline struct inet6_request_sock *
  378. inet6_rsk(const struct request_sock *rsk)
  379. {
  380. return (struct inet6_request_sock *)(((u8 *)rsk) +
  381. inet_rsk(rsk)->inet6_rsk_offset);
  382. }
  383. static inline u32 inet6_rsk_offset(struct request_sock *rsk)
  384. {
  385. return rsk->rsk_ops->obj_size - sizeof(struct inet6_request_sock);
  386. }
  387. static inline struct request_sock *inet6_reqsk_alloc(struct request_sock_ops *ops)
  388. {
  389. struct request_sock *req = reqsk_alloc(ops);
  390. if (req != NULL) {
  391. inet_rsk(req)->inet6_rsk_offset = inet6_rsk_offset(req);
  392. inet6_rsk(req)->pktopts = NULL;
  393. }
  394. return req;
  395. }
  396. static inline struct raw6_sock *raw6_sk(const struct sock *sk)
  397. {
  398. return (struct raw6_sock *)sk;
  399. }
  400. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  401. const struct sock *sk_from)
  402. {
  403. int ancestor_size = sizeof(struct inet_sock);
  404. if (sk_from->sk_family == PF_INET6)
  405. ancestor_size += sizeof(struct ipv6_pinfo);
  406. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  407. }
  408. #define __ipv6_only_sock(sk) (inet6_sk(sk)->ipv6only)
  409. #define ipv6_only_sock(sk) ((sk)->sk_family == PF_INET6 && __ipv6_only_sock(sk))
  410. struct inet6_timewait_sock {
  411. struct in6_addr tw_v6_daddr;
  412. struct in6_addr tw_v6_rcv_saddr;
  413. };
  414. struct tcp6_timewait_sock {
  415. struct tcp_timewait_sock tcp6tw_tcp;
  416. struct inet6_timewait_sock tcp6tw_inet6;
  417. };
  418. static inline u16 inet6_tw_offset(const struct proto *prot)
  419. {
  420. return prot->twsk_prot->twsk_obj_size -
  421. sizeof(struct inet6_timewait_sock);
  422. }
  423. static inline struct inet6_timewait_sock *inet6_twsk(const struct sock *sk)
  424. {
  425. return (struct inet6_timewait_sock *)(((u8 *)sk) +
  426. inet_twsk(sk)->tw_ipv6_offset);
  427. }
  428. static inline struct in6_addr *__inet6_rcv_saddr(const struct sock *sk)
  429. {
  430. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  431. &inet6_sk(sk)->rcv_saddr : &inet6_twsk(sk)->tw_v6_rcv_saddr;
  432. }
  433. static inline struct in6_addr *inet6_rcv_saddr(const struct sock *sk)
  434. {
  435. return sk->sk_family == AF_INET6 ? __inet6_rcv_saddr(sk) : NULL;
  436. }
  437. static inline int inet_v6_ipv6only(const struct sock *sk)
  438. {
  439. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  440. ipv6_only_sock(sk) : inet_twsk(sk)->tw_ipv6only;
  441. }
  442. #else
  443. #define __ipv6_only_sock(sk) 0
  444. #define ipv6_only_sock(sk) 0
  445. static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
  446. {
  447. return NULL;
  448. }
  449. static inline struct inet6_request_sock *
  450. inet6_rsk(const struct request_sock *rsk)
  451. {
  452. return NULL;
  453. }
  454. static inline struct raw6_sock *raw6_sk(const struct sock *sk)
  455. {
  456. return NULL;
  457. }
  458. #define __inet6_rcv_saddr(__sk) NULL
  459. #define inet6_rcv_saddr(__sk) NULL
  460. #define tcp_twsk_ipv6only(__sk) 0
  461. #define inet_v6_ipv6only(__sk) 0
  462. #endif /* IS_ENABLED(CONFIG_IPV6) */
  463. #define INET6_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif)\
  464. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  465. ((*((__portpair *)&(inet_sk(__sk)->inet_dport))) == (__ports)) && \
  466. ((__sk)->sk_family == AF_INET6) && \
  467. ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
  468. ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
  469. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  470. #define INET6_TW_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif) \
  471. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  472. (*((__portpair *)&(inet_twsk(__sk)->tw_dport)) == (__ports)) && \
  473. ((__sk)->sk_family == PF_INET6) && \
  474. (ipv6_addr_equal(&inet6_twsk(__sk)->tw_v6_daddr, (__saddr))) && \
  475. (ipv6_addr_equal(&inet6_twsk(__sk)->tw_v6_rcv_saddr, (__daddr))) && \
  476. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  477. #endif /* __KERNEL__ */
  478. #endif /* _IPV6_H */