xfrm6_state.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * xfrm6_state.c: based on xfrm4_state.c
  3. *
  4. * Authors:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * YOSHIFUJI Hideaki @USAGI
  10. * Split up af-specific portion
  11. *
  12. */
  13. #include <net/xfrm.h>
  14. #include <linux/pfkeyv2.h>
  15. #include <linux/ipsec.h>
  16. #include <linux/netfilter_ipv6.h>
  17. #include <net/dsfield.h>
  18. #include <net/ipv6.h>
  19. #include <net/addrconf.h>
  20. static struct xfrm_state_afinfo xfrm6_state_afinfo;
  21. static void
  22. __xfrm6_init_tempsel(struct xfrm_state *x, struct flowi *fl,
  23. struct xfrm_tmpl *tmpl,
  24. xfrm_address_t *daddr, xfrm_address_t *saddr)
  25. {
  26. /* Initialize temporary selector matching only
  27. * to current session. */
  28. ipv6_addr_copy((struct in6_addr *)&x->sel.daddr, &fl->fl6_dst);
  29. ipv6_addr_copy((struct in6_addr *)&x->sel.saddr, &fl->fl6_src);
  30. x->sel.dport = xfrm_flowi_dport(fl);
  31. x->sel.dport_mask = htons(0xffff);
  32. x->sel.sport = xfrm_flowi_sport(fl);
  33. x->sel.sport_mask = htons(0xffff);
  34. x->sel.prefixlen_d = 128;
  35. x->sel.prefixlen_s = 128;
  36. x->sel.proto = fl->proto;
  37. x->sel.ifindex = fl->oif;
  38. x->id = tmpl->id;
  39. if (ipv6_addr_any((struct in6_addr*)&x->id.daddr))
  40. memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
  41. memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
  42. if (ipv6_addr_any((struct in6_addr*)&x->props.saddr))
  43. memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
  44. x->props.mode = tmpl->mode;
  45. x->props.reqid = tmpl->reqid;
  46. x->props.family = AF_INET6;
  47. }
  48. static int
  49. __xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
  50. {
  51. int i;
  52. int j = 0;
  53. /* Rule 1: select IPsec transport except AH */
  54. for (i = 0; i < n; i++) {
  55. if (src[i]->props.mode == XFRM_MODE_TRANSPORT &&
  56. src[i]->id.proto != IPPROTO_AH) {
  57. dst[j++] = src[i];
  58. src[i] = NULL;
  59. }
  60. }
  61. if (j == n)
  62. goto end;
  63. /* Rule 2: select MIPv6 RO or inbound trigger */
  64. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  65. for (i = 0; i < n; i++) {
  66. if (src[i] &&
  67. (src[i]->props.mode == XFRM_MODE_ROUTEOPTIMIZATION ||
  68. src[i]->props.mode == XFRM_MODE_IN_TRIGGER)) {
  69. dst[j++] = src[i];
  70. src[i] = NULL;
  71. }
  72. }
  73. if (j == n)
  74. goto end;
  75. #endif
  76. /* Rule 3: select IPsec transport AH */
  77. for (i = 0; i < n; i++) {
  78. if (src[i] &&
  79. src[i]->props.mode == XFRM_MODE_TRANSPORT &&
  80. src[i]->id.proto == IPPROTO_AH) {
  81. dst[j++] = src[i];
  82. src[i] = NULL;
  83. }
  84. }
  85. if (j == n)
  86. goto end;
  87. /* Rule 4: select IPsec tunnel */
  88. for (i = 0; i < n; i++) {
  89. if (src[i] &&
  90. (src[i]->props.mode == XFRM_MODE_TUNNEL ||
  91. src[i]->props.mode == XFRM_MODE_BEET)) {
  92. dst[j++] = src[i];
  93. src[i] = NULL;
  94. }
  95. }
  96. if (likely(j == n))
  97. goto end;
  98. /* Final rule */
  99. for (i = 0; i < n; i++) {
  100. if (src[i]) {
  101. dst[j++] = src[i];
  102. src[i] = NULL;
  103. }
  104. }
  105. end:
  106. return 0;
  107. }
  108. static int
  109. __xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
  110. {
  111. int i;
  112. int j = 0;
  113. /* Rule 1: select IPsec transport */
  114. for (i = 0; i < n; i++) {
  115. if (src[i]->mode == XFRM_MODE_TRANSPORT) {
  116. dst[j++] = src[i];
  117. src[i] = NULL;
  118. }
  119. }
  120. if (j == n)
  121. goto end;
  122. /* Rule 2: select MIPv6 RO or inbound trigger */
  123. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  124. for (i = 0; i < n; i++) {
  125. if (src[i] &&
  126. (src[i]->mode == XFRM_MODE_ROUTEOPTIMIZATION ||
  127. src[i]->mode == XFRM_MODE_IN_TRIGGER)) {
  128. dst[j++] = src[i];
  129. src[i] = NULL;
  130. }
  131. }
  132. if (j == n)
  133. goto end;
  134. #endif
  135. /* Rule 3: select IPsec tunnel */
  136. for (i = 0; i < n; i++) {
  137. if (src[i] &&
  138. (src[i]->mode == XFRM_MODE_TUNNEL ||
  139. src[i]->mode == XFRM_MODE_BEET)) {
  140. dst[j++] = src[i];
  141. src[i] = NULL;
  142. }
  143. }
  144. if (likely(j == n))
  145. goto end;
  146. /* Final rule */
  147. for (i = 0; i < n; i++) {
  148. if (src[i]) {
  149. dst[j++] = src[i];
  150. src[i] = NULL;
  151. }
  152. }
  153. end:
  154. return 0;
  155. }
  156. int xfrm6_extract_header(struct sk_buff *skb)
  157. {
  158. struct ipv6hdr *iph = ipv6_hdr(skb);
  159. XFRM_MODE_SKB_CB(skb)->id = 0;
  160. XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF);
  161. XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph);
  162. XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit;
  163. memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl,
  164. sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
  165. return 0;
  166. }
  167. static struct xfrm_state_afinfo xfrm6_state_afinfo = {
  168. .family = AF_INET6,
  169. .proto = IPPROTO_IPV6,
  170. .eth_proto = htons(ETH_P_IPV6),
  171. .owner = THIS_MODULE,
  172. .init_tempsel = __xfrm6_init_tempsel,
  173. .tmpl_sort = __xfrm6_tmpl_sort,
  174. .state_sort = __xfrm6_state_sort,
  175. .output = xfrm6_output,
  176. .extract_input = xfrm6_extract_input,
  177. .extract_output = xfrm6_extract_output,
  178. .transport_finish = xfrm6_transport_finish,
  179. };
  180. int __init xfrm6_state_init(void)
  181. {
  182. return xfrm_state_register_afinfo(&xfrm6_state_afinfo);
  183. }
  184. void xfrm6_state_fini(void)
  185. {
  186. xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
  187. }