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 | 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 | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 26 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); |
| 27 | return xfrm_input(skb, nexthdr, spi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | EXPORT_SYMBOL(xfrm6_rcv_spi); |
| 30 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 31 | int xfrm6_transport_finish(struct sk_buff *skb, int async) |
| 32 | { |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 33 | skb_network_header(skb)[IP6CB(skb)->nhoff] = |
| 34 | XFRM_MODE_SKB_CB(skb)->protocol; |
| 35 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 36 | #ifdef CONFIG_NETFILTER |
| 37 | ipv6_hdr(skb)->payload_len = htons(skb->len); |
| 38 | __skb_push(skb, skb->data - skb_network_header(skb)); |
| 39 | |
| 40 | NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL, |
| 41 | ip6_rcv_finish); |
| 42 | return -1; |
| 43 | #else |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame^] | 44 | if (async) |
| 45 | return ip6_rcv_finish(skb); |
| 46 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 47 | return 1; |
| 48 | #endif |
| 49 | } |
| 50 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 51 | int xfrm6_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 53 | return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], |
| 54 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 56 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 57 | EXPORT_SYMBOL(xfrm6_rcv); |
| 58 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 59 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, |
| 60 | xfrm_address_t *saddr, u8 proto) |
| 61 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 62 | struct xfrm_state *x = NULL; |
| 63 | int wildcard = 0; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 64 | xfrm_address_t *xany; |
| 65 | struct xfrm_state *xfrm_vec_one = NULL; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 66 | int nh = 0; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 67 | int i = 0; |
| 68 | |
YOSHIFUJI Hideaki | c53b359 | 2007-04-24 20:44:50 +0900 | [diff] [blame] | 69 | xany = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 70 | |
| 71 | for (i = 0; i < 3; i++) { |
| 72 | xfrm_address_t *dst, *src; |
| 73 | switch (i) { |
| 74 | case 0: |
| 75 | dst = daddr; |
| 76 | src = saddr; |
| 77 | break; |
| 78 | case 1: |
| 79 | /* lookup state with wild-card source address */ |
| 80 | wildcard = 1; |
| 81 | dst = daddr; |
| 82 | src = xany; |
| 83 | break; |
| 84 | case 2: |
| 85 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 86 | /* lookup state with wild-card addresses */ |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 87 | wildcard = 1; /* XXX */ |
| 88 | dst = xany; |
| 89 | src = xany; |
| 90 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 91 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 92 | |
| 93 | x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6); |
| 94 | if (!x) |
| 95 | continue; |
| 96 | |
| 97 | spin_lock(&x->lock); |
| 98 | |
| 99 | if (wildcard) { |
| 100 | if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) { |
| 101 | spin_unlock(&x->lock); |
| 102 | xfrm_state_put(x); |
| 103 | x = NULL; |
| 104 | continue; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (unlikely(x->km.state != XFRM_STATE_VALID)) { |
| 109 | spin_unlock(&x->lock); |
| 110 | xfrm_state_put(x); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 111 | x = NULL; |
| 112 | continue; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 113 | } |
| 114 | if (xfrm_state_check_expire(x)) { |
| 115 | spin_unlock(&x->lock); |
| 116 | xfrm_state_put(x); |
| 117 | x = NULL; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | nh = x->type->input(x, skb); |
| 122 | if (nh <= 0) { |
| 123 | spin_unlock(&x->lock); |
| 124 | xfrm_state_put(x); |
| 125 | x = NULL; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | x->curlft.bytes += skb->len; |
| 130 | x->curlft.packets++; |
| 131 | |
| 132 | spin_unlock(&x->lock); |
| 133 | |
| 134 | xfrm_vec_one = x; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (!xfrm_vec_one) |
| 139 | goto drop; |
| 140 | |
| 141 | /* Allocate new secpath or COW existing one. */ |
| 142 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 143 | struct sec_path *sp; |
| 144 | sp = secpath_dup(skb->sp); |
| 145 | if (!sp) |
| 146 | goto drop; |
| 147 | if (skb->sp) |
| 148 | secpath_put(skb->sp); |
| 149 | skb->sp = sp; |
| 150 | } |
| 151 | |
| 152 | if (1 + skb->sp->len > XFRM_MAX_DEPTH) |
| 153 | goto drop; |
| 154 | |
| 155 | skb->sp->xvec[skb->sp->len] = xfrm_vec_one; |
| 156 | skb->sp->len ++; |
| 157 | |
| 158 | return 1; |
| 159 | drop: |
| 160 | if (xfrm_vec_one) |
| 161 | xfrm_state_put(xfrm_vec_one); |
| 162 | return -1; |
| 163 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 164 | |
| 165 | EXPORT_SYMBOL(xfrm6_input_addr); |