blob: f417b77fa0e15762715a9498974fbd8f5071401d [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
23__xfrm6_init_tempsel(struct xfrm_state *x, struct flowi *fl,
24 struct xfrm_tmpl *tmpl,
25 xfrm_address_t *daddr, xfrm_address_t *saddr)
26{
27 /* Initialize temporary selector matching only
28 * to current session. */
29 ipv6_addr_copy((struct in6_addr *)&x->sel.daddr, &fl->fl6_dst);
30 ipv6_addr_copy((struct in6_addr *)&x->sel.saddr, &fl->fl6_src);
31 x->sel.dport = xfrm_flowi_dport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070032 x->sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 x->sel.sport = xfrm_flowi_sport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070034 x->sel.sport_mask = htons(0xffff);
Andreas Steffen79654a72008-11-04 14:49:19 -080035 x->sel.family = AF_INET6;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 x->sel.prefixlen_d = 128;
37 x->sel.prefixlen_s = 128;
38 x->sel.proto = fl->proto;
39 x->sel.ifindex = fl->oif;
40 x->id = tmpl->id;
41 if (ipv6_addr_any((struct in6_addr*)&x->id.daddr))
42 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
43 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
44 if (ipv6_addr_any((struct in6_addr*)&x->props.saddr))
45 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 x->props.mode = tmpl->mode;
47 x->props.reqid = tmpl->reqid;
48 x->props.family = AF_INET6;
49}
50
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +090051/* distribution counting sort function for xfrm_state and xfrm_tmpl */
52static int
53__xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
54{
55 int i;
56 int class[XFRM_MAX_DEPTH];
57 int count[maxclass];
58
59 memset(count, 0, sizeof(count));
60
61 for (i = 0; i < n; i++) {
62 int c;
63 class[i] = c = cmp(src[i]);
64 count[c]++;
65 }
66
67 for (i = 2; i < maxclass; i++)
68 count[i] += count[i - 1];
69
70 for (i = 0; i < n; i++) {
71 dst[count[class[i] - 1]++] = src[i];
Hannes Eder66da8c52009-02-21 23:37:10 -080072 src[i] = NULL;
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +090073 }
74
75 return 0;
76}
77
78/*
79 * Rule for xfrm_state:
80 *
81 * rule 1: select IPsec transport except AH
82 * rule 2: select MIPv6 RO or inbound trigger
83 * rule 3: select IPsec transport AH
84 * rule 4: select IPsec tunnel
85 * rule 5: others
86 */
87static int __xfrm6_state_sort_cmp(void *p)
88{
89 struct xfrm_state *v = p;
90
91 switch (v->props.mode) {
92 case XFRM_MODE_TRANSPORT:
93 if (v->id.proto != IPPROTO_AH)
94 return 1;
95 else
96 return 3;
97#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
98 case XFRM_MODE_ROUTEOPTIMIZATION:
99 case XFRM_MODE_IN_TRIGGER:
100 return 2;
101#endif
102 case XFRM_MODE_TUNNEL:
103 case XFRM_MODE_BEET:
104 return 4;
105 }
106 return 5;
107}
108
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700109static int
110__xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
111{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900112 return __xfrm6_sort((void **)dst, (void **)src, n,
113 __xfrm6_state_sort_cmp, 6);
114}
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700115
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900116/*
117 * Rule for xfrm_tmpl:
118 *
119 * rule 1: select IPsec transport
120 * rule 2: select MIPv6 RO or inbound trigger
121 * rule 3: select IPsec tunnel
122 * rule 4: others
123 */
124static int __xfrm6_tmpl_sort_cmp(void *p)
125{
126 struct xfrm_tmpl *v = p;
127 switch (v->mode) {
128 case XFRM_MODE_TRANSPORT:
129 return 1;
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700130#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900131 case XFRM_MODE_ROUTEOPTIMIZATION:
132 case XFRM_MODE_IN_TRIGGER:
133 return 2;
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -0700134#endif
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900135 case XFRM_MODE_TUNNEL:
136 case XFRM_MODE_BEET:
137 return 3;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700138 }
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900139 return 4;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700140}
141
142static int
143__xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
144{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900145 return __xfrm6_sort((void **)dst, (void **)src, n,
146 __xfrm6_tmpl_sort_cmp, 5);
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700147}
148
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800149int xfrm6_extract_header(struct sk_buff *skb)
150{
151 struct ipv6hdr *iph = ipv6_hdr(skb);
152
Herbert Xu732c8bd2008-03-26 16:51:09 -0700153 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800154 XFRM_MODE_SKB_CB(skb)->id = 0;
155 XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF);
156 XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph);
157 XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit;
Herbert Xu732c8bd2008-03-26 16:51:09 -0700158 XFRM_MODE_SKB_CB(skb)->optlen = 0;
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800159 memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl,
160 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
161
162 return 0;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static struct xfrm_state_afinfo xfrm6_state_afinfo = {
166 .family = AF_INET6,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800167 .proto = IPPROTO_IPV6,
Herbert Xu227620e2007-11-13 21:41:28 -0800168 .eth_proto = htons(ETH_P_IPV6),
Herbert Xu17c2a422007-10-17 21:33:12 -0700169 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .init_tempsel = __xfrm6_init_tempsel,
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700171 .tmpl_sort = __xfrm6_tmpl_sort,
172 .state_sort = __xfrm6_state_sort,
Miika Komucdca7262007-02-06 14:24:56 -0800173 .output = xfrm6_output,
Herbert Xu227620e2007-11-13 21:41:28 -0800174 .extract_input = xfrm6_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800175 .extract_output = xfrm6_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -0800176 .transport_finish = xfrm6_transport_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800179int __init xfrm6_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800181 return xfrm_state_register_afinfo(&xfrm6_state_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184void xfrm6_state_fini(void)
185{
186 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
187}
188