Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <net/xfrm.h> |
| 15 | #include <linux/pfkeyv2.h> |
| 16 | #include <linux/ipsec.h> |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 17 | #include <linux/netfilter_ipv6.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 18 | #include <net/dsfield.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/ipv6.h> |
Patrick McHardy | ee51b1b | 2006-01-13 14:34:36 -0800 | [diff] [blame] | 20 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static void |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 23 | __xfrm6_init_tempsel(struct xfrm_selector *sel, struct flowi *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | /* Initialize temporary selector matching only |
| 26 | * to current session. */ |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 27 | ipv6_addr_copy((struct in6_addr *)&sel->daddr, &fl->fl6_dst); |
| 28 | ipv6_addr_copy((struct in6_addr *)&sel->saddr, &fl->fl6_src); |
| 29 | sel->dport = xfrm_flowi_dport(fl); |
| 30 | sel->dport_mask = htons(0xffff); |
| 31 | sel->sport = xfrm_flowi_sport(fl); |
| 32 | sel->sport_mask = htons(0xffff); |
| 33 | sel->family = AF_INET6; |
| 34 | sel->prefixlen_d = 128; |
| 35 | sel->prefixlen_s = 128; |
| 36 | sel->proto = fl->proto; |
| 37 | sel->ifindex = fl->oif; |
| 38 | } |
| 39 | |
| 40 | static void |
| 41 | xfrm6_init_temprop(struct xfrm_state *x, struct xfrm_tmpl *tmpl, |
| 42 | xfrm_address_t *daddr, xfrm_address_t *saddr) |
| 43 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | x->id = tmpl->id; |
| 45 | if (ipv6_addr_any((struct in6_addr*)&x->id.daddr)) |
| 46 | memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr)); |
| 47 | memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr)); |
| 48 | if (ipv6_addr_any((struct in6_addr*)&x->props.saddr)) |
| 49 | memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | x->props.mode = tmpl->mode; |
| 51 | x->props.reqid = tmpl->reqid; |
| 52 | x->props.family = AF_INET6; |
| 53 | } |
| 54 | |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 55 | /* distribution counting sort function for xfrm_state and xfrm_tmpl */ |
| 56 | static int |
| 57 | __xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass) |
| 58 | { |
| 59 | int i; |
| 60 | int class[XFRM_MAX_DEPTH]; |
| 61 | int count[maxclass]; |
| 62 | |
| 63 | memset(count, 0, sizeof(count)); |
| 64 | |
| 65 | for (i = 0; i < n; i++) { |
| 66 | int c; |
| 67 | class[i] = c = cmp(src[i]); |
| 68 | count[c]++; |
| 69 | } |
| 70 | |
| 71 | for (i = 2; i < maxclass; i++) |
| 72 | count[i] += count[i - 1]; |
| 73 | |
| 74 | for (i = 0; i < n; i++) { |
| 75 | dst[count[class[i] - 1]++] = src[i]; |
Hannes Eder | 66da8c5 | 2009-02-21 23:37:10 -0800 | [diff] [blame] | 76 | src[i] = NULL; |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Rule for xfrm_state: |
| 84 | * |
| 85 | * rule 1: select IPsec transport except AH |
| 86 | * rule 2: select MIPv6 RO or inbound trigger |
| 87 | * rule 3: select IPsec transport AH |
| 88 | * rule 4: select IPsec tunnel |
| 89 | * rule 5: others |
| 90 | */ |
| 91 | static int __xfrm6_state_sort_cmp(void *p) |
| 92 | { |
| 93 | struct xfrm_state *v = p; |
| 94 | |
| 95 | switch (v->props.mode) { |
| 96 | case XFRM_MODE_TRANSPORT: |
| 97 | if (v->id.proto != IPPROTO_AH) |
| 98 | return 1; |
| 99 | else |
| 100 | return 3; |
| 101 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
| 102 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 103 | case XFRM_MODE_IN_TRIGGER: |
| 104 | return 2; |
| 105 | #endif |
| 106 | case XFRM_MODE_TUNNEL: |
| 107 | case XFRM_MODE_BEET: |
| 108 | return 4; |
| 109 | } |
| 110 | return 5; |
| 111 | } |
| 112 | |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 113 | static int |
| 114 | __xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n) |
| 115 | { |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 116 | return __xfrm6_sort((void **)dst, (void **)src, n, |
| 117 | __xfrm6_state_sort_cmp, 6); |
| 118 | } |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 119 | |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 120 | /* |
| 121 | * Rule for xfrm_tmpl: |
| 122 | * |
| 123 | * rule 1: select IPsec transport |
| 124 | * rule 2: select MIPv6 RO or inbound trigger |
| 125 | * rule 3: select IPsec tunnel |
| 126 | * rule 4: others |
| 127 | */ |
| 128 | static int __xfrm6_tmpl_sort_cmp(void *p) |
| 129 | { |
| 130 | struct xfrm_tmpl *v = p; |
| 131 | switch (v->mode) { |
| 132 | case XFRM_MODE_TRANSPORT: |
| 133 | return 1; |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 134 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 135 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 136 | case XFRM_MODE_IN_TRIGGER: |
| 137 | return 2; |
Masahide NAKAMURA | 64d9fdd | 2006-08-23 22:54:07 -0700 | [diff] [blame] | 138 | #endif |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 139 | case XFRM_MODE_TUNNEL: |
| 140 | case XFRM_MODE_BEET: |
| 141 | return 3; |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 142 | } |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 143 | return 4; |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int |
| 147 | __xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n) |
| 148 | { |
YOSHIFUJI Hideaki | 3b6cdf9 | 2008-02-19 10:15:27 +0900 | [diff] [blame] | 149 | return __xfrm6_sort((void **)dst, (void **)src, n, |
| 150 | __xfrm6_tmpl_sort_cmp, 5); |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 153 | int xfrm6_extract_header(struct sk_buff *skb) |
| 154 | { |
| 155 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 156 | |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 157 | XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph); |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 158 | XFRM_MODE_SKB_CB(skb)->id = 0; |
| 159 | XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF); |
| 160 | XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph); |
| 161 | XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit; |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 162 | XFRM_MODE_SKB_CB(skb)->optlen = 0; |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 163 | memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl, |
| 164 | sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl)); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static struct xfrm_state_afinfo xfrm6_state_afinfo = { |
| 170 | .family = AF_INET6, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 171 | .proto = IPPROTO_IPV6, |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 172 | .eth_proto = htons(ETH_P_IPV6), |
Herbert Xu | 17c2a42 | 2007-10-17 21:33:12 -0700 | [diff] [blame] | 173 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .init_tempsel = __xfrm6_init_tempsel, |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 175 | .init_temprop = xfrm6_init_temprop, |
Masahide NAKAMURA | 58c949d | 2006-08-23 22:51:02 -0700 | [diff] [blame] | 176 | .tmpl_sort = __xfrm6_tmpl_sort, |
| 177 | .state_sort = __xfrm6_state_sort, |
Miika Komu | cdca726 | 2007-02-06 14:24:56 -0800 | [diff] [blame] | 178 | .output = xfrm6_output, |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 179 | .extract_input = xfrm6_extract_input, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 180 | .extract_output = xfrm6_extract_output, |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 181 | .transport_finish = xfrm6_transport_finish, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Daniel Lezcano | 0013cab | 2007-12-07 00:42:11 -0800 | [diff] [blame] | 184 | int __init xfrm6_state_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Daniel Lezcano | 0013cab | 2007-12-07 00:42:11 -0800 | [diff] [blame] | 186 | return xfrm_state_register_afinfo(&xfrm6_state_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void xfrm6_state_fini(void) |
| 190 | { |
| 191 | xfrm_state_unregister_afinfo(&xfrm6_state_afinfo); |
| 192 | } |
| 193 | |