blob: 89884a4f23aa27992e006846bf14dd5673fea675 [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
22static struct xfrm_state_afinfo xfrm6_state_afinfo;
23
24static void
25__xfrm6_init_tempsel(struct xfrm_state *x, struct flowi *fl,
26 struct xfrm_tmpl *tmpl,
27 xfrm_address_t *daddr, xfrm_address_t *saddr)
28{
29 /* Initialize temporary selector matching only
30 * to current session. */
31 ipv6_addr_copy((struct in6_addr *)&x->sel.daddr, &fl->fl6_dst);
32 ipv6_addr_copy((struct in6_addr *)&x->sel.saddr, &fl->fl6_src);
33 x->sel.dport = xfrm_flowi_dport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070034 x->sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 x->sel.sport = xfrm_flowi_sport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070036 x->sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 x->sel.prefixlen_d = 128;
38 x->sel.prefixlen_s = 128;
39 x->sel.proto = fl->proto;
40 x->sel.ifindex = fl->oif;
41 x->id = tmpl->id;
42 if (ipv6_addr_any((struct in6_addr*)&x->id.daddr))
43 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
44 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
45 if (ipv6_addr_any((struct in6_addr*)&x->props.saddr))
46 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 x->props.mode = tmpl->mode;
48 x->props.reqid = tmpl->reqid;
49 x->props.family = AF_INET6;
50}
51
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +090052/* distribution counting sort function for xfrm_state and xfrm_tmpl */
53static int
54__xfrm6_sort(void **dst, void **src, int n, int (*cmp)(void *p), int maxclass)
55{
56 int i;
57 int class[XFRM_MAX_DEPTH];
58 int count[maxclass];
59
60 memset(count, 0, sizeof(count));
61
62 for (i = 0; i < n; i++) {
63 int c;
64 class[i] = c = cmp(src[i]);
65 count[c]++;
66 }
67
68 for (i = 2; i < maxclass; i++)
69 count[i] += count[i - 1];
70
71 for (i = 0; i < n; i++) {
72 dst[count[class[i] - 1]++] = src[i];
73 src[i] = 0;
74 }
75
76 return 0;
77}
78
79/*
80 * Rule for xfrm_state:
81 *
82 * rule 1: select IPsec transport except AH
83 * rule 2: select MIPv6 RO or inbound trigger
84 * rule 3: select IPsec transport AH
85 * rule 4: select IPsec tunnel
86 * rule 5: others
87 */
88static int __xfrm6_state_sort_cmp(void *p)
89{
90 struct xfrm_state *v = p;
91
92 switch (v->props.mode) {
93 case XFRM_MODE_TRANSPORT:
94 if (v->id.proto != IPPROTO_AH)
95 return 1;
96 else
97 return 3;
98#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
99 case XFRM_MODE_ROUTEOPTIMIZATION:
100 case XFRM_MODE_IN_TRIGGER:
101 return 2;
102#endif
103 case XFRM_MODE_TUNNEL:
104 case XFRM_MODE_BEET:
105 return 4;
106 }
107 return 5;
108}
109
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700110static int
111__xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
112{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900113 return __xfrm6_sort((void **)dst, (void **)src, n,
114 __xfrm6_state_sort_cmp, 6);
115}
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700116
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900117/*
118 * Rule for xfrm_tmpl:
119 *
120 * rule 1: select IPsec transport
121 * rule 2: select MIPv6 RO or inbound trigger
122 * rule 3: select IPsec tunnel
123 * rule 4: others
124 */
125static int __xfrm6_tmpl_sort_cmp(void *p)
126{
127 struct xfrm_tmpl *v = p;
128 switch (v->mode) {
129 case XFRM_MODE_TRANSPORT:
130 return 1;
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700131#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900132 case XFRM_MODE_ROUTEOPTIMIZATION:
133 case XFRM_MODE_IN_TRIGGER:
134 return 2;
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -0700135#endif
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900136 case XFRM_MODE_TUNNEL:
137 case XFRM_MODE_BEET:
138 return 3;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700139 }
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900140 return 4;
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700141}
142
143static int
144__xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
145{
YOSHIFUJI Hideaki3b6cdf92008-02-19 10:15:27 +0900146 return __xfrm6_sort((void **)dst, (void **)src, n,
147 __xfrm6_tmpl_sort_cmp, 5);
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700148}
149
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800150int xfrm6_extract_header(struct sk_buff *skb)
151{
152 struct ipv6hdr *iph = ipv6_hdr(skb);
153
Herbert Xu732c8bd2008-03-26 16:51:09 -0700154 XFRM_MODE_SKB_CB(skb)->ihl = sizeof(*iph);
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800155 XFRM_MODE_SKB_CB(skb)->id = 0;
156 XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF);
157 XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph);
158 XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit;
Herbert Xu732c8bd2008-03-26 16:51:09 -0700159 XFRM_MODE_SKB_CB(skb)->optlen = 0;
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800160 memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl,
161 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
162
163 return 0;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static struct xfrm_state_afinfo xfrm6_state_afinfo = {
167 .family = AF_INET6,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800168 .proto = IPPROTO_IPV6,
Herbert Xu227620e2007-11-13 21:41:28 -0800169 .eth_proto = htons(ETH_P_IPV6),
Herbert Xu17c2a422007-10-17 21:33:12 -0700170 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .init_tempsel = __xfrm6_init_tempsel,
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700172 .tmpl_sort = __xfrm6_tmpl_sort,
173 .state_sort = __xfrm6_state_sort,
Miika Komucdca7262007-02-06 14:24:56 -0800174 .output = xfrm6_output,
Herbert Xu227620e2007-11-13 21:41:28 -0800175 .extract_input = xfrm6_extract_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800176 .extract_output = xfrm6_extract_output,
Herbert Xu716062f2007-11-13 21:44:23 -0800177 .transport_finish = xfrm6_transport_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800180int __init xfrm6_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800182 return xfrm_state_register_afinfo(&xfrm6_state_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185void xfrm6_state_fini(void)
186{
187 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
188}
189