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