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