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 |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 19 | int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb) |
| 20 | { |
| 21 | return xfrm6_extract_header(skb); |
| 22 | } |
| 23 | |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 24 | int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Herbert Xu | 2fcb45b | 2007-12-03 22:54:12 -0800 | [diff] [blame] | 26 | XFRM_SPI_SKB_CB(skb)->family = AF_INET6; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 27 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); |
| 28 | return xfrm_input(skb, nexthdr, spi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | EXPORT_SYMBOL(xfrm6_rcv_spi); |
| 31 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 32 | int xfrm6_transport_finish(struct sk_buff *skb, int async) |
| 33 | { |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 34 | skb_network_header(skb)[IP6CB(skb)->nhoff] = |
| 35 | XFRM_MODE_SKB_CB(skb)->protocol; |
| 36 | |
Herbert Xu | 0883ae0 | 2007-12-30 21:10:14 -0800 | [diff] [blame] | 37 | #ifndef CONFIG_NETFILTER |
| 38 | if (!async) |
| 39 | return 1; |
| 40 | #endif |
| 41 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 42 | ipv6_hdr(skb)->payload_len = htons(skb->len); |
| 43 | __skb_push(skb, skb->data - skb_network_header(skb)); |
| 44 | |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 45 | NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL, |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 46 | ip6_rcv_finish); |
| 47 | return -1; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 50 | int xfrm6_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 52 | return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], |
| 53 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 55 | EXPORT_SYMBOL(xfrm6_rcv); |
| 56 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 57 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, |
| 58 | xfrm_address_t *saddr, u8 proto) |
| 59 | { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 60 | struct net *net = dev_net(skb->dev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 61 | struct xfrm_state *x = NULL; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 62 | int i = 0; |
| 63 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 64 | /* Allocate new secpath or COW existing one. */ |
| 65 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 66 | struct sec_path *sp; |
| 67 | |
| 68 | sp = secpath_dup(skb->sp); |
| 69 | if (!sp) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 70 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 71 | goto drop; |
| 72 | } |
| 73 | if (skb->sp) |
| 74 | secpath_put(skb->sp); |
| 75 | skb->sp = sp; |
| 76 | } |
| 77 | |
| 78 | if (1 + skb->sp->len == XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 79 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 80 | goto drop; |
| 81 | } |
| 82 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 83 | for (i = 0; i < 3; i++) { |
| 84 | xfrm_address_t *dst, *src; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 85 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 86 | switch (i) { |
| 87 | case 0: |
| 88 | dst = daddr; |
| 89 | src = saddr; |
| 90 | break; |
| 91 | case 1: |
| 92 | /* lookup state with wild-card source address */ |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 93 | dst = daddr; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 94 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 95 | break; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 96 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 97 | /* lookup state with wild-card addresses */ |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 98 | dst = (xfrm_address_t *)&in6addr_any; |
| 99 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 100 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 101 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 102 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 103 | x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 104 | if (!x) |
| 105 | continue; |
| 106 | |
| 107 | spin_lock(&x->lock); |
| 108 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 109 | if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) && |
| 110 | likely(x->km.state == XFRM_STATE_VALID) && |
| 111 | !xfrm_state_check_expire(x)) { |
| 112 | spin_unlock(&x->lock); |
| 113 | if (x->type->input(x, skb) > 0) { |
| 114 | /* found a valid state */ |
| 115 | break; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 116 | } |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 117 | } else |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 118 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 119 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 120 | xfrm_state_put(x); |
| 121 | x = NULL; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 124 | if (!x) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 125 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 126 | xfrm_audit_state_notfound_simple(skb, AF_INET6); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 127 | goto drop; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 130 | skb->sp->xvec[skb->sp->len++] = x; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 131 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 132 | spin_lock(&x->lock); |
| 133 | |
| 134 | x->curlft.bytes += skb->len; |
| 135 | x->curlft.packets++; |
| 136 | |
| 137 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 138 | |
| 139 | return 1; |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 140 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 141 | drop: |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 142 | return -1; |
| 143 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 144 | EXPORT_SYMBOL(xfrm6_input_addr); |