Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xfrm6_state.c: based on xfrm4_state.c |
| 4 | * |
| 5 | * Authors: |
| 6 | * Mitsuru KANDA @USAGI |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 7 | * Kazunori MIYAZAWA @USAGI |
| 8 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 9 | * IPv6 support |
| 10 | * YOSHIFUJI Hideaki @USAGI |
| 11 | * Split up af-specific portion |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <net/xfrm.h> |
| 16 | #include <linux/pfkeyv2.h> |
| 17 | #include <linux/ipsec.h> |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 18 | #include <linux/netfilter_ipv6.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 20 | #include <net/dsfield.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <net/ipv6.h> |
Patrick McHardy | ee51b1b | 2006-01-13 14:34:36 -0800 | [diff] [blame] | 22 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static void |
David S. Miller | 73e5ebb | 2011-02-22 17:51:44 -0800 | [diff] [blame] | 25 | __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 27 | const struct flowi6 *fl6 = &fl->u.ip6; |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* Initialize temporary selector matching only |
| 30 | * to current session. */ |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 31 | *(struct in6_addr *)&sel->daddr = fl6->daddr; |
| 32 | *(struct in6_addr *)&sel->saddr = fl6->saddr; |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 33 | sel->dport = xfrm_flowi_dport(fl, &fl6->uli); |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 34 | sel->dport_mask = htons(0xffff); |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 35 | sel->sport = xfrm_flowi_sport(fl, &fl6->uli); |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 36 | sel->sport_mask = htons(0xffff); |
| 37 | sel->family = AF_INET6; |
| 38 | sel->prefixlen_d = 128; |
| 39 | sel->prefixlen_s = 128; |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 40 | sel->proto = fl6->flowi6_proto; |
| 41 | sel->ifindex = fl6->flowi6_oif; |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void |
David S. Miller | 19bd624 | 2011-02-24 00:07:20 -0500 | [diff] [blame] | 45 | xfrm6_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl, |
| 46 | const xfrm_address_t *daddr, const xfrm_address_t *saddr) |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 47 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | x->id = tmpl->id; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 49 | if (ipv6_addr_any((struct in6_addr *)&x->id.daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr)); |
| 51 | memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr)); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 52 | if (ipv6_addr_any((struct in6_addr *)&x->props.saddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | x->props.mode = tmpl->mode; |
| 55 | x->props.reqid = tmpl->reqid; |
| 56 | x->props.family = AF_INET6; |
| 57 | } |
| 58 | |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 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 i; |
| 64 | int class[XFRM_MAX_DEPTH]; |
| 65 | int count[maxclass]; |
| 66 | |
| 67 | memset(count, 0, sizeof(count)); |
| 68 | |
| 69 | for (i = 0; i < n; i++) { |
| 70 | int c; |
| 71 | class[i] = c = cmp(src[i]); |
| 72 | count[c]++; |
| 73 | } |
| 74 | |
| 75 | for (i = 2; i < maxclass; i++) |
| 76 | count[i] += count[i - 1]; |
| 77 | |
| 78 | for (i = 0; i < n; i++) { |
| 79 | dst[count[class[i] - 1]++] = src[i]; |
Hannes Eder | 66da8c5 | 2009-02-21 23:37:10 -0800 | [diff] [blame] | 80 | src[i] = NULL; |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Rule for xfrm_state: |
| 88 | * |
| 89 | * rule 1: select IPsec transport except AH |
| 90 | * rule 2: select MIPv6 RO or inbound trigger |
| 91 | * rule 3: select IPsec transport AH |
| 92 | * rule 4: select IPsec tunnel |
| 93 | * rule 5: others |
| 94 | */ |
| 95 | static int __xfrm6_state_sort_cmp(void *p) |
| 96 | { |
| 97 | struct xfrm_state *v = p; |
| 98 | |
| 99 | switch (v->props.mode) { |
| 100 | case XFRM_MODE_TRANSPORT: |
| 101 | if (v->id.proto != IPPROTO_AH) |
| 102 | return 1; |
| 103 | else |
| 104 | return 3; |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 105 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 106 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 107 | case XFRM_MODE_IN_TRIGGER: |
| 108 | return 2; |
| 109 | #endif |
| 110 | case XFRM_MODE_TUNNEL: |
| 111 | case XFRM_MODE_BEET: |
| 112 | return 4; |
| 113 | } |
| 114 | return 5; |
| 115 | } |
| 116 | |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 117 | static int |
| 118 | __xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n) |
| 119 | { |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 120 | return __xfrm6_sort((void **)dst, (void **)src, n, |
| 121 | __xfrm6_state_sort_cmp, 6); |
| 122 | } |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 123 | |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 124 | /* |
| 125 | * Rule for xfrm_tmpl: |
| 126 | * |
| 127 | * rule 1: select IPsec transport |
| 128 | * rule 2: select MIPv6 RO or inbound trigger |
| 129 | * rule 3: select IPsec tunnel |
| 130 | * rule 4: others |
| 131 | */ |
| 132 | static int __xfrm6_tmpl_sort_cmp(void *p) |
| 133 | { |
| 134 | struct xfrm_tmpl *v = p; |
| 135 | switch (v->mode) { |
| 136 | case XFRM_MODE_TRANSPORT: |
| 137 | return 1; |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 138 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 139 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 140 | case XFRM_MODE_IN_TRIGGER: |
| 141 | return 2; |
Masahide NAKAMURA | 64d9fdd | 2006-08-23 22:54:07 -0700 | [diff] [blame] | 142 | #endif |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 143 | case XFRM_MODE_TUNNEL: |
| 144 | case XFRM_MODE_BEET: |
| 145 | return 3; |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 146 | } |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 147 | return 4; |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int |
| 151 | __xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n) |
| 152 | { |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 153 | return __xfrm6_sort((void **)dst, (void **)src, n, |
| 154 | __xfrm6_tmpl_sort_cmp, 5); |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 157 | int xfrm6_extract_header(struct sk_buff *skb) |
| 158 | { |
| 159 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 160 | |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 161 | XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph); |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 162 | XFRM_MODE_SKB_CB(skb)->id = 0; |
| 163 | XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF); |
| 164 | XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph); |
| 165 | XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit; |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 166 | XFRM_MODE_SKB_CB(skb)->optlen = 0; |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 167 | memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl, |
| 168 | sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl)); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static struct xfrm_state_afinfo xfrm6_state_afinfo = { |
| 174 | .family = AF_INET6, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 175 | .proto = IPPROTO_IPV6, |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 176 | .eth_proto = htons(ETH_P_IPV6), |
Herbert Xu | 17c2a42 | 2007-10-17 21:33:12 -0700 | [diff] [blame] | 177 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | .init_tempsel = __xfrm6_init_tempsel, |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 179 | .init_temprop = xfrm6_init_temprop, |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 180 | .tmpl_sort = __xfrm6_tmpl_sort, |
| 181 | .state_sort = __xfrm6_state_sort, |
Miika Komu | cdca726 | 2007-02-06 14:24:56 -0800 | [diff] [blame] | 182 | .output = xfrm6_output, |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 183 | .output_finish = xfrm6_output_finish, |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 184 | .extract_input = xfrm6_extract_input, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 185 | .extract_output = xfrm6_extract_output, |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 186 | .transport_finish = xfrm6_transport_finish, |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 187 | .local_error = xfrm6_local_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Daniel Lezcano | 0013cab | 2007-12-07 00:42:11 -0800 | [diff] [blame] | 190 | int __init xfrm6_state_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Daniel Lezcano | 0013cab | 2007-12-07 00:42:11 -0800 | [diff] [blame] | 192 | return xfrm_state_register_afinfo(&xfrm6_state_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void xfrm6_state_fini(void) |
| 196 | { |
| 197 | xfrm_state_unregister_afinfo(&xfrm6_state_afinfo); |
| 198 | } |
| 199 | |