ipv6.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 proxy_ndp;
  137. __s32 accept_source_route;
  138. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  139. __s32 optimistic_dad;
  140. #endif
  141. #ifdef CONFIG_IPV6_MROUTE
  142. __s32 mc_forwarding;
  143. #endif
  144. __s32 disable_ipv6;
  145. __s32 accept_dad;
  146. __s32 force_tllao;
  147. void *sysctl;
  148. };
  149. struct ipv6_params {
  150. __s32 disable_ipv6;
  151. __s32 autoconf;
  152. };
  153. extern struct ipv6_params ipv6_defaults;
  154. #endif
  155. /* index values for the variables in ipv6_devconf */
  156. enum {
  157. DEVCONF_FORWARDING = 0,
  158. DEVCONF_HOPLIMIT,
  159. DEVCONF_MTU6,
  160. DEVCONF_ACCEPT_RA,
  161. DEVCONF_ACCEPT_REDIRECTS,
  162. DEVCONF_AUTOCONF,
  163. DEVCONF_DAD_TRANSMITS,
  164. DEVCONF_RTR_SOLICITS,
  165. DEVCONF_RTR_SOLICIT_INTERVAL,
  166. DEVCONF_RTR_SOLICIT_DELAY,
  167. DEVCONF_USE_TEMPADDR,
  168. DEVCONF_TEMP_VALID_LFT,
  169. DEVCONF_TEMP_PREFERED_LFT,
  170. DEVCONF_REGEN_MAX_RETRY,
  171. DEVCONF_MAX_DESYNC_FACTOR,
  172. DEVCONF_MAX_ADDRESSES,
  173. DEVCONF_FORCE_MLD_VERSION,
  174. DEVCONF_ACCEPT_RA_DEFRTR,
  175. DEVCONF_ACCEPT_RA_PINFO,
  176. DEVCONF_ACCEPT_RA_RTR_PREF,
  177. DEVCONF_RTR_PROBE_INTERVAL,
  178. DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN,
  179. DEVCONF_PROXY_NDP,
  180. DEVCONF_OPTIMISTIC_DAD,
  181. DEVCONF_ACCEPT_SOURCE_ROUTE,
  182. DEVCONF_MC_FORWARDING,
  183. DEVCONF_DISABLE_IPV6,
  184. DEVCONF_ACCEPT_DAD,
  185. DEVCONF_FORCE_TLLAO,
  186. DEVCONF_MAX
  187. };
  188. #ifdef __KERNEL__
  189. #include <linux/icmpv6.h>
  190. #include <linux/tcp.h>
  191. #include <linux/udp.h>
  192. #include <net/inet_sock.h>
  193. static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
  194. {
  195. return (struct ipv6hdr *)skb_network_header(skb);
  196. }
  197. static inline struct ipv6hdr *ipipv6_hdr(const struct sk_buff *skb)
  198. {
  199. return (struct ipv6hdr *)skb_transport_header(skb);
  200. }
  201. /*
  202. This structure contains results of exthdrs parsing
  203. as offsets from skb->nh.
  204. */
  205. struct inet6_skb_parm {
  206. int iif;
  207. __u16 ra;
  208. __u16 hop;
  209. __u16 dst0;
  210. __u16 srcrt;
  211. __u16 dst1;
  212. __u16 lastopt;
  213. __u32 nhoff;
  214. __u16 flags;
  215. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  216. __u16 dsthao;
  217. #endif
  218. #define IP6SKB_XFRM_TRANSFORMED 1
  219. #define IP6SKB_FORWARDED 2
  220. };
  221. #define IP6CB(skb) ((struct inet6_skb_parm*)((skb)->cb))
  222. #define IP6CBMTU(skb) ((struct ip6_mtuinfo *)((skb)->cb))
  223. static inline int inet6_iif(const struct sk_buff *skb)
  224. {
  225. return IP6CB(skb)->iif;
  226. }
  227. struct inet6_request_sock {
  228. struct in6_addr loc_addr;
  229. struct in6_addr rmt_addr;
  230. struct sk_buff *pktopts;
  231. int iif;
  232. };
  233. struct tcp6_request_sock {
  234. struct tcp_request_sock tcp6rsk_tcp;
  235. struct inet6_request_sock tcp6rsk_inet6;
  236. };
  237. struct ipv6_mc_socklist;
  238. struct ipv6_ac_socklist;
  239. struct ipv6_fl_socklist;
  240. /**
  241. * struct ipv6_pinfo - ipv6 private area
  242. *
  243. * In the struct sock hierarchy (tcp6_sock, upd6_sock, etc)
  244. * this _must_ be the last member, so that inet6_sk_generic
  245. * is able to calculate its offset from the base struct sock
  246. * by using the struct proto->slab_obj_size member. -acme
  247. */
  248. struct ipv6_pinfo {
  249. struct in6_addr saddr;
  250. struct in6_addr rcv_saddr;
  251. struct in6_addr daddr;
  252. struct in6_pktinfo sticky_pktinfo;
  253. struct in6_addr *daddr_cache;
  254. #ifdef CONFIG_IPV6_SUBTREES
  255. struct in6_addr *saddr_cache;
  256. #endif
  257. __be32 flow_label;
  258. __u32 frag_size;
  259. /*
  260. * Packed in 16bits.
  261. * Omit one shift by by putting the signed field at MSB.
  262. */
  263. #if defined(__BIG_ENDIAN_BITFIELD)
  264. __s16 hop_limit:9;
  265. __u16 __unused_1:7;
  266. #else
  267. __u16 __unused_1:7;
  268. __s16 hop_limit:9;
  269. #endif
  270. #if defined(__BIG_ENDIAN_BITFIELD)
  271. /* Packed in 16bits. */
  272. __s16 mcast_hops:9;
  273. __u16 __unused_2:6,
  274. mc_loop:1;
  275. #else
  276. __u16 mc_loop:1,
  277. __unused_2:6;
  278. __s16 mcast_hops:9;
  279. #endif
  280. int mcast_oif;
  281. /* pktoption flags */
  282. union {
  283. struct {
  284. __u16 srcrt:1,
  285. osrcrt:1,
  286. rxinfo:1,
  287. rxoinfo:1,
  288. rxhlim:1,
  289. rxohlim:1,
  290. hopopts:1,
  291. ohopopts:1,
  292. dstopts:1,
  293. odstopts:1,
  294. rxflow:1,
  295. rxtclass:1,
  296. rxpmtu:1;
  297. } bits;
  298. __u16 all;
  299. } rxopt;
  300. /* sockopt flags */
  301. __u16 recverr:1,
  302. sndflow:1,
  303. pmtudisc:2,
  304. ipv6only:1,
  305. srcprefs:3, /* 001: prefer temporary address
  306. * 010: prefer public address
  307. * 100: prefer care-of address
  308. */
  309. dontfrag:1;
  310. __u8 min_hopcount;
  311. __u8 tclass;
  312. __u8 padding;
  313. __u32 dst_cookie;
  314. struct ipv6_mc_socklist *ipv6_mc_list;
  315. struct ipv6_ac_socklist *ipv6_ac_list;
  316. struct ipv6_fl_socklist *ipv6_fl_list;
  317. struct ipv6_txoptions *opt;
  318. struct sk_buff *pktoptions;
  319. struct sk_buff *rxpmtu;
  320. struct {
  321. struct ipv6_txoptions *opt;
  322. u8 hop_limit;
  323. u8 tclass;
  324. } cork;
  325. };
  326. /* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
  327. struct raw6_sock {
  328. /* inet_sock has to be the first member of raw6_sock */
  329. struct inet_sock inet;
  330. __u32 checksum; /* perform checksum */
  331. __u32 offset; /* checksum offset */
  332. struct icmp6_filter filter;
  333. /* ipv6_pinfo has to be the last member of raw6_sock, see inet6_sk_generic */
  334. struct ipv6_pinfo inet6;
  335. };
  336. struct udp6_sock {
  337. struct udp_sock udp;
  338. /* ipv6_pinfo has to be the last member of udp6_sock, see inet6_sk_generic */
  339. struct ipv6_pinfo inet6;
  340. };
  341. struct tcp6_sock {
  342. struct tcp_sock tcp;
  343. /* ipv6_pinfo has to be the last member of tcp6_sock, see inet6_sk_generic */
  344. struct ipv6_pinfo inet6;
  345. };
  346. extern int inet6_sk_rebuild_header(struct sock *sk);
  347. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  348. static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
  349. {
  350. return inet_sk(__sk)->pinet6;
  351. }
  352. static inline struct inet6_request_sock *
  353. inet6_rsk(const struct request_sock *rsk)
  354. {
  355. return (struct inet6_request_sock *)(((u8 *)rsk) +
  356. inet_rsk(rsk)->inet6_rsk_offset);
  357. }
  358. static inline u32 inet6_rsk_offset(struct request_sock *rsk)
  359. {
  360. return rsk->rsk_ops->obj_size - sizeof(struct inet6_request_sock);
  361. }
  362. static inline struct request_sock *inet6_reqsk_alloc(struct request_sock_ops *ops)
  363. {
  364. struct request_sock *req = reqsk_alloc(ops);
  365. if (req != NULL) {
  366. inet_rsk(req)->inet6_rsk_offset = inet6_rsk_offset(req);
  367. inet6_rsk(req)->pktopts = NULL;
  368. }
  369. return req;
  370. }
  371. static inline struct raw6_sock *raw6_sk(const struct sock *sk)
  372. {
  373. return (struct raw6_sock *)sk;
  374. }
  375. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  376. const struct sock *sk_from)
  377. {
  378. int ancestor_size = sizeof(struct inet_sock);
  379. if (sk_from->sk_family == PF_INET6)
  380. ancestor_size += sizeof(struct ipv6_pinfo);
  381. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  382. }
  383. #define __ipv6_only_sock(sk) (inet6_sk(sk)->ipv6only)
  384. #define ipv6_only_sock(sk) ((sk)->sk_family == PF_INET6 && __ipv6_only_sock(sk))
  385. struct inet6_timewait_sock {
  386. struct in6_addr tw_v6_daddr;
  387. struct in6_addr tw_v6_rcv_saddr;
  388. };
  389. struct tcp6_timewait_sock {
  390. struct tcp_timewait_sock tcp6tw_tcp;
  391. struct inet6_timewait_sock tcp6tw_inet6;
  392. };
  393. static inline u16 inet6_tw_offset(const struct proto *prot)
  394. {
  395. return prot->twsk_prot->twsk_obj_size -
  396. sizeof(struct inet6_timewait_sock);
  397. }
  398. static inline struct inet6_timewait_sock *inet6_twsk(const struct sock *sk)
  399. {
  400. return (struct inet6_timewait_sock *)(((u8 *)sk) +
  401. inet_twsk(sk)->tw_ipv6_offset);
  402. }
  403. static inline struct in6_addr *__inet6_rcv_saddr(const struct sock *sk)
  404. {
  405. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  406. &inet6_sk(sk)->rcv_saddr : &inet6_twsk(sk)->tw_v6_rcv_saddr;
  407. }
  408. static inline struct in6_addr *inet6_rcv_saddr(const struct sock *sk)
  409. {
  410. return sk->sk_family == AF_INET6 ? __inet6_rcv_saddr(sk) : NULL;
  411. }
  412. static inline int inet_v6_ipv6only(const struct sock *sk)
  413. {
  414. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  415. ipv6_only_sock(sk) : inet_twsk(sk)->tw_ipv6only;
  416. }
  417. #else
  418. #define __ipv6_only_sock(sk) 0
  419. #define ipv6_only_sock(sk) 0
  420. static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
  421. {
  422. return NULL;
  423. }
  424. static inline struct inet6_request_sock *
  425. inet6_rsk(const struct request_sock *rsk)
  426. {
  427. return NULL;
  428. }
  429. static inline struct raw6_sock *raw6_sk(const struct sock *sk)
  430. {
  431. return NULL;
  432. }
  433. #define __inet6_rcv_saddr(__sk) NULL
  434. #define inet6_rcv_saddr(__sk) NULL
  435. #define tcp_twsk_ipv6only(__sk) 0
  436. #define inet_v6_ipv6only(__sk) 0
  437. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  438. #define INET6_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif)\
  439. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  440. ((*((__portpair *)&(inet_sk(__sk)->inet_dport))) == (__ports)) && \
  441. ((__sk)->sk_family == AF_INET6) && \
  442. ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
  443. ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
  444. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  445. #define INET6_TW_MATCH(__sk, __net, __hash, __saddr, __daddr, __ports, __dif) \
  446. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  447. (*((__portpair *)&(inet_twsk(__sk)->tw_dport)) == (__ports)) && \
  448. ((__sk)->sk_family == PF_INET6) && \
  449. (ipv6_addr_equal(&inet6_twsk(__sk)->tw_v6_daddr, (__saddr))) && \
  450. (ipv6_addr_equal(&inet6_twsk(__sk)->tw_v6_rcv_saddr, (__daddr))) && \
  451. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  452. #endif /* __KERNEL__ */
  453. #endif /* _IPV6_H */