yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm_input.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * YOSHIFUJI Hideaki @USAGI |
| 6 | * Split up af-specific portion |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <net/dst.h> |
| 14 | #include <net/ip.h> |
| 15 | #include <net/xfrm.h> |
| 16 | |
| 17 | static struct kmem_cache *secpath_cachep __read_mostly; |
| 18 | |
| 19 | atomic_t xfrm_input_slab = ATOMIC_INIT(0); |
| 20 | void __secpath_destroy(struct sec_path *sp) |
| 21 | { |
| 22 | int i; |
| 23 | for (i = 0; i < sp->len; i++) |
| 24 | xfrm_state_put(sp->xvec[i]); |
| 25 | atomic_dec(&xfrm_input_slab); |
| 26 | kmem_cache_free(secpath_cachep, sp); |
| 27 | } |
| 28 | EXPORT_SYMBOL(__secpath_destroy); |
| 29 | |
| 30 | struct sec_path *secpath_dup(struct sec_path *src) |
| 31 | { |
| 32 | struct sec_path *sp; |
| 33 | sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC); |
| 34 | if (!sp) |
| 35 | return NULL; |
| 36 | atomic_inc(&xfrm_input_slab); |
| 37 | sp->len = 0; |
| 38 | if (src) { |
| 39 | int i; |
| 40 | |
| 41 | memcpy(sp, src, sizeof(*sp)); |
| 42 | for (i = 0; i < sp->len; i++) |
| 43 | xfrm_state_hold(sp->xvec[i]); |
| 44 | } |
| 45 | atomic_set(&sp->refcnt, 1); |
| 46 | return sp; |
| 47 | } |
| 48 | EXPORT_SYMBOL(secpath_dup); |
| 49 | |
| 50 | /* Fetch spi and seq from ipsec header */ |
| 51 | |
| 52 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
| 53 | { |
| 54 | int offset, offset_seq; |
| 55 | int hlen; |
| 56 | |
| 57 | switch (nexthdr) { |
| 58 | case IPPROTO_AH: |
| 59 | hlen = sizeof(struct ip_auth_hdr); |
| 60 | offset = offsetof(struct ip_auth_hdr, spi); |
| 61 | offset_seq = offsetof(struct ip_auth_hdr, seq_no); |
| 62 | break; |
| 63 | case IPPROTO_ESP: |
| 64 | hlen = sizeof(struct ip_esp_hdr); |
| 65 | offset = offsetof(struct ip_esp_hdr, spi); |
| 66 | offset_seq = offsetof(struct ip_esp_hdr, seq_no); |
| 67 | break; |
| 68 | case IPPROTO_COMP: |
| 69 | if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr))) |
| 70 | return -EINVAL; |
| 71 | *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2))); |
| 72 | *seq = 0; |
| 73 | return 0; |
| 74 | default: |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | if (!pskb_may_pull(skb, hlen)) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | *spi = *(__be32*)(skb_transport_header(skb) + offset); |
| 82 | *seq = *(__be32*)(skb_transport_header(skb) + offset_seq); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb) |
| 87 | { |
| 88 | struct xfrm_mode *inner_mode = x->inner_mode; |
| 89 | int err; |
| 90 | |
| 91 | err = x->outer_mode->afinfo->extract_input(x, skb); |
| 92 | if (err) |
| 93 | return err; |
| 94 | |
| 95 | if (x->sel.family == AF_UNSPEC) { |
| 96 | inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol); |
| 97 | if (inner_mode == NULL) |
| 98 | return -EAFNOSUPPORT; |
| 99 | } |
| 100 | |
| 101 | skb->protocol = inner_mode->afinfo->eth_proto; |
| 102 | return inner_mode->input2(x, skb); |
| 103 | } |
| 104 | EXPORT_SYMBOL(xfrm_prepare_input); |
| 105 | |
| 106 | int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) |
| 107 | { |
| 108 | struct net *net = dev_net(skb->dev); |
| 109 | int err; |
| 110 | __be32 seq; |
| 111 | __be32 seq_hi; |
| 112 | struct xfrm_state *x; |
| 113 | xfrm_address_t *daddr; |
| 114 | struct xfrm_mode *inner_mode; |
| 115 | unsigned int family; |
| 116 | int decaps = 0; |
| 117 | int async = 0; |
| 118 | |
| 119 | /* A negative encap_type indicates async resumption. */ |
| 120 | if (encap_type < 0) { |
| 121 | async = 1; |
| 122 | x = xfrm_input_state(skb); |
| 123 | seq = XFRM_SKB_CB(skb)->seq.input.low; |
| 124 | goto resume; |
| 125 | } |
| 126 | |
| 127 | /* Allocate new secpath or COW existing one. */ |
| 128 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 129 | struct sec_path *sp; |
| 130 | |
| 131 | sp = secpath_dup(skb->sp); |
| 132 | if (!sp) { |
| 133 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); |
| 134 | goto drop; |
| 135 | } |
| 136 | if (skb->sp) |
| 137 | secpath_put(skb->sp); |
| 138 | skb->sp = sp; |
| 139 | } |
| 140 | |
| 141 | daddr = (xfrm_address_t *)(skb_network_header(skb) + |
| 142 | XFRM_SPI_SKB_CB(skb)->daddroff); |
| 143 | family = XFRM_SPI_SKB_CB(skb)->family; |
| 144 | |
| 145 | seq = 0; |
| 146 | if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) { |
| 147 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
| 148 | goto drop; |
| 149 | } |
| 150 | |
| 151 | do { |
| 152 | if (skb->sp->len == XFRM_MAX_DEPTH) { |
| 153 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
| 154 | goto drop; |
| 155 | } |
| 156 | |
| 157 | x = xfrm_state_lookup(net, skb->mark, daddr, spi, nexthdr, family); |
| 158 | if (x == NULL) { |
| 159 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); |
| 160 | xfrm_audit_state_notfound(skb, family, spi, seq); |
| 161 | goto drop; |
| 162 | } |
| 163 | |
| 164 | skb->sp->xvec[skb->sp->len++] = x; |
| 165 | |
| 166 | spin_lock(&x->lock); |
| 167 | if (unlikely(x->km.state != XFRM_STATE_VALID)) { |
| 168 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEINVALID); |
| 169 | goto drop_unlock; |
| 170 | } |
| 171 | |
| 172 | if ((x->encap ? x->encap->encap_type : 0) != encap_type) { |
| 173 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH); |
| 174 | goto drop_unlock; |
| 175 | } |
| 176 | |
| 177 | if (x->repl->check(x, skb, seq)) { |
| 178 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); |
| 179 | goto drop_unlock; |
| 180 | } |
| 181 | |
| 182 | if (xfrm_state_check_expire(x)) { |
| 183 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED); |
| 184 | goto drop_unlock; |
| 185 | } |
| 186 | |
| 187 | spin_unlock(&x->lock); |
| 188 | |
| 189 | seq_hi = htonl(xfrm_replay_seqhi(x, seq)); |
| 190 | |
| 191 | XFRM_SKB_CB(skb)->seq.input.low = seq; |
| 192 | XFRM_SKB_CB(skb)->seq.input.hi = seq_hi; |
| 193 | |
| 194 | skb_dst_force(skb); |
| 195 | |
| 196 | nexthdr = x->type->input(x, skb); |
| 197 | |
| 198 | if (nexthdr == -EINPROGRESS) |
| 199 | return 0; |
| 200 | |
| 201 | resume: |
| 202 | spin_lock(&x->lock); |
| 203 | if (nexthdr <= 0) { |
| 204 | if (nexthdr == -EBADMSG) { |
| 205 | xfrm_audit_state_icvfail(x, skb, |
| 206 | x->type->proto); |
| 207 | x->stats.integrity_failed++; |
| 208 | } |
| 209 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); |
| 210 | goto drop_unlock; |
| 211 | } |
| 212 | |
| 213 | /* only the first xfrm gets the encap type */ |
| 214 | encap_type = 0; |
| 215 | |
| 216 | if (async && x->repl->recheck(x, skb, seq)) { |
| 217 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); |
| 218 | goto drop_unlock; |
| 219 | } |
| 220 | |
| 221 | x->repl->advance(x, seq); |
| 222 | |
| 223 | x->curlft.bytes += skb->len; |
| 224 | x->curlft.packets++; |
| 225 | |
| 226 | spin_unlock(&x->lock); |
| 227 | |
| 228 | XFRM_MODE_SKB_CB(skb)->protocol = nexthdr; |
| 229 | |
| 230 | inner_mode = x->inner_mode; |
| 231 | |
| 232 | if (x->sel.family == AF_UNSPEC) { |
| 233 | inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol); |
| 234 | if (inner_mode == NULL) |
| 235 | goto drop; |
| 236 | } |
| 237 | |
| 238 | if (inner_mode->input(x, skb)) { |
| 239 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); |
| 240 | goto drop; |
| 241 | } |
| 242 | |
| 243 | if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) { |
| 244 | decaps = 1; |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * We need the inner address. However, we only get here for |
| 250 | * transport mode so the outer address is identical. |
| 251 | */ |
| 252 | daddr = &x->id.daddr; |
| 253 | family = x->outer_mode->afinfo->family; |
| 254 | |
| 255 | err = xfrm_parse_spi(skb, nexthdr, &spi, &seq); |
| 256 | if (err < 0) { |
| 257 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
| 258 | goto drop; |
| 259 | } |
| 260 | } while (!err); |
| 261 | |
| 262 | nf_reset(skb); |
| 263 | |
| 264 | if (decaps) { |
| 265 | skb_dst_drop(skb); |
| 266 | netif_rx(skb); |
| 267 | return 0; |
| 268 | } else { |
| 269 | return x->inner_mode->afinfo->transport_finish(skb, async); |
| 270 | } |
| 271 | |
| 272 | drop_unlock: |
| 273 | spin_unlock(&x->lock); |
| 274 | drop: |
| 275 | kfree_skb(skb); |
| 276 | return 0; |
| 277 | } |
| 278 | EXPORT_SYMBOL(xfrm_input); |
| 279 | |
| 280 | int xfrm_input_resume(struct sk_buff *skb, int nexthdr) |
| 281 | { |
| 282 | return xfrm_input(skb, nexthdr, 0, -1); |
| 283 | } |
| 284 | EXPORT_SYMBOL(xfrm_input_resume); |
| 285 | |
| 286 | void __init xfrm_input_init(void) |
| 287 | { |
| 288 | secpath_cachep = kmem_cache_create("secpath_cache", |
| 289 | sizeof(struct sec_path), |
| 290 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
| 291 | NULL); |
| 292 | } |