blob: afe941e9415cdd895c08059f9ca76770187f9c2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hideaki1ab14572007-02-09 23:24:49 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <net/xfrm.h>
15#include <linux/pfkeyv2.h>
16#include <linux/ipsec.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080017#include <linux/netfilter_ipv6.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080018#include <net/dsfield.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/ipv6.h>
Patrick McHardyee51b1b2006-01-13 14:34:36 -080020#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static void
David S. Miller73e5ebb2011-02-22 17:51:44 -080023__xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050025 const struct flowi6 *fl6 = &fl->u.ip6;
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 /* Initialize temporary selector matching only
28 * to current session. */
David S. Miller7e1dc7b2011-03-12 02:42:11 -050029 ipv6_addr_copy((struct in6_addr *)&sel->daddr, &fl6->daddr);
30 ipv6_addr_copy((struct in6_addr *)&sel->saddr, &fl6->saddr);
31 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070032 sel->dport_mask = htons(0xffff);
David S. Miller7e1dc7b2011-03-12 02:42:11 -050033 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
Thomas Egerer8444cf72010-09-20 11:11:38 -070034 sel->sport_mask = htons(0xffff);
35 sel->family = AF_INET6;
36 sel->prefixlen_d = 128;
37 sel->prefixlen_s = 128;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050038 sel->proto = fl6->flowi6_proto;
39 sel->ifindex = fl6->flowi6_oif;
Thomas Egerer8444cf72010-09-20 11:11:38 -070040}
41
42static void
David S. Miller19bd6242011-02-24 00:07:20 -050043xfrm6_init_temprop(struct xfrm_state *x, const struct xfrm_tmpl *tmpl,
44 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
Thomas Egerer8444cf72010-09-20 11:11:38 -070045{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 x->id = tmpl->id;
47 if (ipv6_addr_any((struct in6_addr*)&x->id.daddr))
48 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
49 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
50 if (ipv6_addr_any((struct in6_addr*)&x->props.saddr))
51 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 x->props.mode = tmpl->mode;
53 x->props.reqid = tmpl->reqid;
54 x->props.family = AF_INET6;
55}
56
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +090057/* distribution counting sort function for xfrm_state and xfrm_tmpl */
58static int
59__xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
60{
61 int i;
62 int class[XFRM_MAX_DEPTH];
63 int count[maxclass];
64
65 memset(count, 0, sizeof(count));
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];
Hannes Eder66da8c52009-02-21 23:37:10 -080078 src[i] = NULL;
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +090079 }
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 */
93static 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 defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
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
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700115static int
116__xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
117{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900118 return __xfrm6_sort((void **)dst, (void **)src, n,
119 __xfrm6_state_sort_cmp, 6);
120}
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700121
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900122/*
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 */
130static 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;
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700136#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900137 case XFRM_MODE_ROUTEOPTIMIZATION:
138 case XFRM_MODE_IN_TRIGGER:
139 return 2;
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -0700140#endif
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900141 case XFRM_MODE_TUNNEL:
142 case XFRM_MODE_BEET:
143 return 3;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700144 }
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900145 return 4;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700146}
147
148static int
149__xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
150{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900151 return __xfrm6_sort((void **)dst, (void **)src, n,
152 __xfrm6_tmpl_sort_cmp, 5);
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700153}
154
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800155int xfrm6_extract_header(struct sk_buff *skb)
156{
157 struct ipv6hdr *iph = ipv6_hdr(skb);
158
Herbert Xu732c8bd2008-03-26 16:51:09 -0700159 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800160 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;
Herbert Xu732c8bd2008-03-26 16:51:09 -0700164 XFRM_MODE_SKB_CB(skb)->optlen = 0;
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800165 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static struct xfrm_state_afinfo xfrm6_state_afinfo = {
172 .family = AF_INET6,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800173 .proto = IPPROTO_IPV6,
Herbert Xu227620e2007-11-13 21:41:28 -0800174 .eth_proto = htons(ETH_P_IPV6),
Herbert Xu17c2a422007-10-17 21:33:12 -0700175 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .init_tempsel = __xfrm6_init_tempsel,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700177 .init_temprop = xfrm6_init_temprop,
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700178 .tmpl_sort = __xfrm6_tmpl_sort,
179 .state_sort = __xfrm6_state_sort,
Miika Komucdca7262007-02-06 14:24:56 -0800180 .output = xfrm6_output,
Herbert Xu227620e2007-11-13 21:41:28 -0800181 .extract_input = xfrm6_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800182 .extract_output = xfrm6_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -0800183 .transport_finish = xfrm6_transport_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800186int __init xfrm6_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800188 return xfrm_state_register_afinfo(&xfrm6_state_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191void xfrm6_state_fini(void)
192{
193 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
194}
195