xfrm6_state.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <linux/export.h>
  18. #include <net/dsfield.h>
  19. #include <net/ipv6.h>
  20. #include <net/addrconf.h>
  21. static void
  22. __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
  23. {
  24. const struct flowi6 *fl6 = &fl->u.ip6;
  25. /* Initialize temporary selector matching only
  26. * to current session. */
  27. *(struct in6_addr *)&sel->daddr = fl6->daddr;
  28. *(struct in6_addr *)&sel->saddr = fl6->saddr;
  29. sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
  30. sel->dport_mask = htons(0xffff);
  31. sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
  32. sel->sport_mask = htons(0xffff);
  33. sel->family = AF_INET6;
  34. sel->prefixlen_d = 128;
  35. sel->prefixlen_s = 128;
  36. sel->proto = fl6->flowi6_proto;
  37. sel->ifindex = fl6->flowi6_oif;
  38. }
  39. static void
  40. xfrm6_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl,
  41. const xfrm_address_t *daddr, const xfrm_address_t *saddr)
  42. {
  43. x->id = tmpl->id;
  44. if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
  45. memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
  46. memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
  47. if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
  48. memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
  49. x->props.mode = tmpl->mode;
  50. x->props.reqid = tmpl->reqid;
  51. x->props.family = AF_INET6;
  52. }
  53. /* distribution counting sort function for xfrm_state and xfrm_tmpl */
  54. static int
  55. __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
  56. {
  57. int i;
  58. int class[XFRM_MAX_DEPTH];
  59. int count[maxclass];
  60. memset(count, 0, sizeof(count));
  61. for (i = 0; i < n; i++) {
  62. int c;
  63. class[i] = c = cmp(src[i]);
  64. count[c]++;
  65. }
  66. for (i = 2; i < maxclass; i++)
  67. count[i] += count[i - 1];
  68. for (i = 0; i < n; i++) {
  69. dst[count[class[i] - 1]++] = src[i];
  70. src[i] = NULL;
  71. }
  72. return 0;
  73. }
  74. /*
  75. * Rule for xfrm_state:
  76. *
  77. * rule 1: select IPsec transport except AH
  78. * rule 2: select MIPv6 RO or inbound trigger
  79. * rule 3: select IPsec transport AH
  80. * rule 4: select IPsec tunnel
  81. * rule 5: others
  82. */
  83. static int __xfrm6_state_sort_cmp(void *p)
  84. {
  85. struct xfrm_state *v = p;
  86. switch (v->props.mode) {
  87. case XFRM_MODE_TRANSPORT:
  88. if (v->id.proto != IPPROTO_AH)
  89. return 1;
  90. else
  91. return 3;
  92. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  93. case XFRM_MODE_ROUTEOPTIMIZATION:
  94. case XFRM_MODE_IN_TRIGGER:
  95. return 2;
  96. #endif
  97. case XFRM_MODE_TUNNEL:
  98. case XFRM_MODE_BEET:
  99. return 4;
  100. }
  101. return 5;
  102. }
  103. static int
  104. __xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
  105. {
  106. return __xfrm6_sort((void **)dst, (void **)src, n,
  107. __xfrm6_state_sort_cmp, 6);
  108. }
  109. /*
  110. * Rule for xfrm_tmpl:
  111. *
  112. * rule 1: select IPsec transport
  113. * rule 2: select MIPv6 RO or inbound trigger
  114. * rule 3: select IPsec tunnel
  115. * rule 4: others
  116. */
  117. static int __xfrm6_tmpl_sort_cmp(void *p)
  118. {
  119. struct xfrm_tmpl *v = p;
  120. switch (v->mode) {
  121. case XFRM_MODE_TRANSPORT:
  122. return 1;
  123. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  124. case XFRM_MODE_ROUTEOPTIMIZATION:
  125. case XFRM_MODE_IN_TRIGGER:
  126. return 2;
  127. #endif
  128. case XFRM_MODE_TUNNEL:
  129. case XFRM_MODE_BEET:
  130. return 3;
  131. }
  132. return 4;
  133. }
  134. static int
  135. __xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
  136. {
  137. return __xfrm6_sort((void **)dst, (void **)src, n,
  138. __xfrm6_tmpl_sort_cmp, 5);
  139. }
  140. int xfrm6_extract_header(struct sk_buff *skb)
  141. {
  142. struct ipv6hdr *iph = ipv6_hdr(skb);
  143. XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
  144. XFRM_MODE_SKB_CB(skb)->id = 0;
  145. XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF);
  146. XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph);
  147. XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit;
  148. XFRM_MODE_SKB_CB(skb)->optlen = 0;
  149. memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl,
  150. sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
  151. return 0;
  152. }
  153. static struct xfrm_state_afinfo xfrm6_state_afinfo = {
  154. .family = AF_INET6,
  155. .proto = IPPROTO_IPV6,
  156. .eth_proto = htons(ETH_P_IPV6),
  157. .owner = THIS_MODULE,
  158. .init_tempsel = __xfrm6_init_tempsel,
  159. .init_temprop = xfrm6_init_temprop,
  160. .tmpl_sort = __xfrm6_tmpl_sort,
  161. .state_sort = __xfrm6_state_sort,
  162. .output = xfrm6_output,
  163. .output_finish = xfrm6_output_finish,
  164. .extract_input = xfrm6_extract_input,
  165. .extract_output = xfrm6_extract_output,
  166. .transport_finish = xfrm6_transport_finish,
  167. .local_error = xfrm6_local_error,
  168. };
  169. int __init xfrm6_state_init(void)
  170. {
  171. return xfrm_state_register_afinfo(&xfrm6_state_afinfo);
  172. }
  173. void xfrm6_state_fini(void)
  174. {
  175. xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
  176. }