Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm6_input.c: based on net/ipv4/xfrm4_input.c |
| 3 | * |
| 4 | * Authors: |
| 5 | * Mitsuru KANDA @USAGI |
| 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 8 | * YOSHIFUJI Hideaki @USAGI |
| 9 | * IPv6 support |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/string.h> |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/netfilter_ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <net/ipv6.h> |
| 17 | #include <net/xfrm.h> |
| 18 | |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 19 | int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | int err; |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 22 | __be32 seq; |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 23 | struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | struct xfrm_state *x; |
| 25 | int xfrm_nr = 0; |
| 26 | int decaps = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | unsigned int nhoff; |
| 28 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 29 | nhoff = IP6CB(skb)->nhoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | seq = 0; |
| 32 | if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) |
| 33 | goto drop; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | do { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 36 | struct ipv6hdr *iph = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | if (xfrm_nr == XFRM_MAX_DEPTH) |
| 39 | goto drop; |
| 40 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 41 | x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 42 | nexthdr, AF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (x == NULL) |
| 44 | goto drop; |
| 45 | spin_lock(&x->lock); |
| 46 | if (unlikely(x->km.state != XFRM_STATE_VALID)) |
| 47 | goto drop_unlock; |
| 48 | |
| 49 | if (x->props.replay_window && xfrm_replay_check(x, seq)) |
| 50 | goto drop_unlock; |
| 51 | |
| 52 | if (xfrm_state_check_expire(x)) |
| 53 | goto drop_unlock; |
| 54 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 55 | nexthdr = x->type->input(x, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (nexthdr <= 0) |
| 57 | goto drop_unlock; |
| 58 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 59 | skb_network_header(skb)[nhoff] = nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (x->props.replay_window) |
| 62 | xfrm_replay_advance(x, seq); |
| 63 | |
| 64 | x->curlft.bytes += skb->len; |
| 65 | x->curlft.packets++; |
| 66 | |
| 67 | spin_unlock(&x->lock); |
| 68 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 69 | xfrm_vec[xfrm_nr++] = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 71 | if (x->outer_mode->input(x, skb)) |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 72 | goto drop; |
| 73 | |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 74 | if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | decaps = 1; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) < 0) |
| 80 | goto drop; |
| 81 | } while (!err); |
| 82 | |
| 83 | /* Allocate new secpath or COW existing one. */ |
| 84 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 85 | struct sec_path *sp; |
| 86 | sp = secpath_dup(skb->sp); |
| 87 | if (!sp) |
| 88 | goto drop; |
| 89 | if (skb->sp) |
| 90 | secpath_put(skb->sp); |
| 91 | skb->sp = sp; |
| 92 | } |
| 93 | |
| 94 | if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH) |
| 95 | goto drop; |
| 96 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 97 | memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec, |
| 98 | xfrm_nr * sizeof(xfrm_vec[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | skb->sp->len += xfrm_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 101 | nf_reset(skb); |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (decaps) { |
Kazunori MIYAZAWA | f282d45 | 2007-05-29 13:03:17 -0700 | [diff] [blame] | 104 | dst_release(skb->dst); |
| 105 | skb->dst = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | netif_rx(skb); |
| 107 | return -1; |
| 108 | } else { |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 109 | #ifdef CONFIG_NETFILTER |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 110 | ipv6_hdr(skb)->payload_len = htons(skb->len); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 111 | __skb_push(skb, skb->data - skb_network_header(skb)); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 112 | |
| 113 | NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 114 | ip6_rcv_finish); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 115 | return -1; |
| 116 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 1; |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 118 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | drop_unlock: |
| 122 | spin_unlock(&x->lock); |
| 123 | xfrm_state_put(x); |
| 124 | drop: |
| 125 | while (--xfrm_nr >= 0) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 126 | xfrm_state_put(xfrm_vec[xfrm_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | kfree_skb(skb); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | EXPORT_SYMBOL(xfrm6_rcv_spi); |
| 132 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 133 | int xfrm6_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 135 | return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], |
| 136 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 138 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 139 | EXPORT_SYMBOL(xfrm6_rcv); |
| 140 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 141 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, |
| 142 | xfrm_address_t *saddr, u8 proto) |
| 143 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 144 | struct xfrm_state *x = NULL; |
| 145 | int wildcard = 0; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 146 | xfrm_address_t *xany; |
| 147 | struct xfrm_state *xfrm_vec_one = NULL; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 148 | int nh = 0; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 149 | int i = 0; |
| 150 | |
YOSHIFUJI Hideaki | c53b359 | 2007-04-24 20:44:50 +0900 | [diff] [blame] | 151 | xany = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 152 | |
| 153 | for (i = 0; i < 3; i++) { |
| 154 | xfrm_address_t *dst, *src; |
| 155 | switch (i) { |
| 156 | case 0: |
| 157 | dst = daddr; |
| 158 | src = saddr; |
| 159 | break; |
| 160 | case 1: |
| 161 | /* lookup state with wild-card source address */ |
| 162 | wildcard = 1; |
| 163 | dst = daddr; |
| 164 | src = xany; |
| 165 | break; |
| 166 | case 2: |
| 167 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 168 | /* lookup state with wild-card addresses */ |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 169 | wildcard = 1; /* XXX */ |
| 170 | dst = xany; |
| 171 | src = xany; |
| 172 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 173 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 174 | |
| 175 | x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6); |
| 176 | if (!x) |
| 177 | continue; |
| 178 | |
| 179 | spin_lock(&x->lock); |
| 180 | |
| 181 | if (wildcard) { |
| 182 | if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) { |
| 183 | spin_unlock(&x->lock); |
| 184 | xfrm_state_put(x); |
| 185 | x = NULL; |
| 186 | continue; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (unlikely(x->km.state != XFRM_STATE_VALID)) { |
| 191 | spin_unlock(&x->lock); |
| 192 | xfrm_state_put(x); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 193 | x = NULL; |
| 194 | continue; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 195 | } |
| 196 | if (xfrm_state_check_expire(x)) { |
| 197 | spin_unlock(&x->lock); |
| 198 | xfrm_state_put(x); |
| 199 | x = NULL; |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | nh = x->type->input(x, skb); |
| 204 | if (nh <= 0) { |
| 205 | spin_unlock(&x->lock); |
| 206 | xfrm_state_put(x); |
| 207 | x = NULL; |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | x->curlft.bytes += skb->len; |
| 212 | x->curlft.packets++; |
| 213 | |
| 214 | spin_unlock(&x->lock); |
| 215 | |
| 216 | xfrm_vec_one = x; |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | if (!xfrm_vec_one) |
| 221 | goto drop; |
| 222 | |
| 223 | /* Allocate new secpath or COW existing one. */ |
| 224 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 225 | struct sec_path *sp; |
| 226 | sp = secpath_dup(skb->sp); |
| 227 | if (!sp) |
| 228 | goto drop; |
| 229 | if (skb->sp) |
| 230 | secpath_put(skb->sp); |
| 231 | skb->sp = sp; |
| 232 | } |
| 233 | |
| 234 | if (1 + skb->sp->len > XFRM_MAX_DEPTH) |
| 235 | goto drop; |
| 236 | |
| 237 | skb->sp->xvec[skb->sp->len] = xfrm_vec_one; |
| 238 | skb->sp->len ++; |
| 239 | |
| 240 | return 1; |
| 241 | drop: |
| 242 | if (xfrm_vec_one) |
| 243 | xfrm_state_put(xfrm_vec_one); |
| 244 | return -1; |
| 245 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 246 | |
| 247 | EXPORT_SYMBOL(xfrm6_input_addr); |