Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm4_input.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * YOSHIFUJI Hideaki @USAGI |
| 6 | * Split up af-specific portion |
| 7 | * Derek Atkins <derek@ihtfp.com> |
| 8 | * Add Encapsulation support |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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_ipv4.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <net/ip.h> |
| 17 | #include <net/xfrm.h> |
| 18 | |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 19 | static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
| 21 | switch (nexthdr) { |
| 22 | case IPPROTO_IPIP: |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 23 | case IPPROTO_IPV6: |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 24 | *spi = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | *seq = 0; |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | return xfrm_parse_spi(skb, nexthdr, spi, seq); |
| 30 | } |
| 31 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_NETFILTER |
| 33 | static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb) |
| 34 | { |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 35 | if (skb->dst == NULL) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 36 | const struct iphdr *iph = ip_hdr(skb); |
| 37 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 38 | if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 39 | skb->dev)) |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 40 | goto drop; |
| 41 | } |
| 42 | return dst_input(skb); |
| 43 | drop: |
| 44 | kfree_skb(skb); |
| 45 | return NET_RX_DROP; |
| 46 | } |
| 47 | #endif |
| 48 | |
James Chapman | 067b207 | 2007-07-05 17:08:05 -0700 | [diff] [blame] | 49 | static int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 51 | __be32 spi, seq; |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 52 | struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | struct xfrm_state *x; |
| 54 | int xfrm_nr = 0; |
| 55 | int decaps = 0; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 56 | int err = xfrm4_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq); |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 57 | unsigned int nhoff = offsetof(struct iphdr, protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 59 | if (err != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | goto drop; |
| 61 | |
| 62 | do { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 63 | const struct iphdr *iph = ip_hdr(skb); |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 64 | int nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | if (xfrm_nr == XFRM_MAX_DEPTH) |
| 67 | goto drop; |
| 68 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 69 | x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, |
| 70 | iph->protocol != IPPROTO_IPV6 ? iph->protocol : IPPROTO_IPIP, AF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (x == NULL) |
| 72 | goto drop; |
| 73 | |
| 74 | spin_lock(&x->lock); |
| 75 | if (unlikely(x->km.state != XFRM_STATE_VALID)) |
| 76 | goto drop_unlock; |
| 77 | |
Herbert Xu | 8bf4b8a | 2006-04-04 12:51:05 -0700 | [diff] [blame] | 78 | if ((x->encap ? x->encap->encap_type : 0) != encap_type) |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 79 | goto drop_unlock; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (x->props.replay_window && xfrm_replay_check(x, seq)) |
| 82 | goto drop_unlock; |
| 83 | |
| 84 | if (xfrm_state_check_expire(x)) |
| 85 | goto drop_unlock; |
| 86 | |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 87 | nexthdr = x->type->input(x, skb); |
| 88 | if (nexthdr <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | goto drop_unlock; |
| 90 | |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 91 | skb_network_header(skb)[nhoff] = nexthdr; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* only the first xfrm gets the encap type */ |
| 94 | encap_type = 0; |
| 95 | |
| 96 | if (x->props.replay_window) |
| 97 | xfrm_replay_advance(x, seq); |
| 98 | |
| 99 | x->curlft.bytes += skb->len; |
| 100 | x->curlft.packets++; |
| 101 | |
| 102 | spin_unlock(&x->lock); |
| 103 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 104 | xfrm_vec[xfrm_nr++] = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 106 | if (x->mode->input(x, skb)) |
| 107 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 109 | if (x->props.mode == XFRM_MODE_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | decaps = 1; |
| 111 | break; |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 114 | err = xfrm_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq); |
| 115 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | goto drop; |
| 117 | } while (!err); |
| 118 | |
| 119 | /* Allocate new secpath or COW existing one. */ |
| 120 | |
| 121 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 122 | struct sec_path *sp; |
| 123 | sp = secpath_dup(skb->sp); |
| 124 | if (!sp) |
| 125 | goto drop; |
| 126 | if (skb->sp) |
| 127 | secpath_put(skb->sp); |
| 128 | skb->sp = sp; |
| 129 | } |
| 130 | if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH) |
| 131 | goto drop; |
| 132 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 133 | memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec, |
| 134 | xfrm_nr * sizeof(xfrm_vec[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | skb->sp->len += xfrm_nr; |
| 136 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 137 | nf_reset(skb); |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (decaps) { |
Kazunori MIYAZAWA | f282d45 | 2007-05-29 13:03:17 -0700 | [diff] [blame] | 140 | dst_release(skb->dst); |
| 141 | skb->dst = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | netif_rx(skb); |
| 143 | return 0; |
| 144 | } else { |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 145 | #ifdef CONFIG_NETFILTER |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 146 | __skb_push(skb, skb->data - skb_network_header(skb)); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 147 | ip_hdr(skb)->tot_len = htons(skb->len); |
| 148 | ip_send_check(ip_hdr(skb)); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 149 | |
| 150 | NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 151 | xfrm4_rcv_encap_finish); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 152 | return 0; |
| 153 | #else |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 154 | return -ip_hdr(skb)->protocol; |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 155 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | drop_unlock: |
| 159 | spin_unlock(&x->lock); |
| 160 | xfrm_state_put(x); |
| 161 | drop: |
| 162 | while (--xfrm_nr >= 0) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 163 | xfrm_state_put(xfrm_vec[xfrm_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | kfree_skb(skb); |
| 166 | return 0; |
| 167 | } |
James Chapman | 067b207 | 2007-07-05 17:08:05 -0700 | [diff] [blame] | 168 | |
| 169 | /* If it's a keepalive packet, then just eat it. |
| 170 | * If it's an encapsulated packet, then pass it to the |
| 171 | * IPsec xfrm input. |
| 172 | * Returns 0 if skb passed to xfrm or was dropped. |
| 173 | * Returns >0 if skb should be passed to UDP. |
| 174 | * Returns <0 if skb should be resubmitted (-ret is protocol) |
| 175 | */ |
| 176 | int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) |
| 177 | { |
| 178 | struct udp_sock *up = udp_sk(sk); |
| 179 | struct udphdr *uh; |
| 180 | struct iphdr *iph; |
| 181 | int iphlen, len; |
| 182 | int ret; |
| 183 | |
| 184 | __u8 *udpdata; |
| 185 | __be32 *udpdata32; |
| 186 | __u16 encap_type = up->encap_type; |
| 187 | |
| 188 | /* if this is not encapsulated socket, then just return now */ |
| 189 | if (!encap_type) |
| 190 | return 1; |
| 191 | |
| 192 | /* If this is a paged skb, make sure we pull up |
| 193 | * whatever data we need to look at. */ |
| 194 | len = skb->len - sizeof(struct udphdr); |
| 195 | if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) |
| 196 | return 1; |
| 197 | |
| 198 | /* Now we can get the pointers */ |
| 199 | uh = udp_hdr(skb); |
| 200 | udpdata = (__u8 *)uh + sizeof(struct udphdr); |
| 201 | udpdata32 = (__be32 *)udpdata; |
| 202 | |
| 203 | switch (encap_type) { |
| 204 | default: |
| 205 | case UDP_ENCAP_ESPINUDP: |
| 206 | /* Check if this is a keepalive packet. If so, eat it. */ |
| 207 | if (len == 1 && udpdata[0] == 0xff) { |
| 208 | goto drop; |
| 209 | } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { |
| 210 | /* ESP Packet without Non-ESP header */ |
| 211 | len = sizeof(struct udphdr); |
| 212 | } else |
| 213 | /* Must be an IKE packet.. pass it through */ |
| 214 | return 1; |
| 215 | break; |
| 216 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
| 217 | /* Check if this is a keepalive packet. If so, eat it. */ |
| 218 | if (len == 1 && udpdata[0] == 0xff) { |
| 219 | goto drop; |
| 220 | } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && |
| 221 | udpdata32[0] == 0 && udpdata32[1] == 0) { |
| 222 | |
| 223 | /* ESP Packet with Non-IKE marker */ |
| 224 | len = sizeof(struct udphdr) + 2 * sizeof(u32); |
| 225 | } else |
| 226 | /* Must be an IKE packet.. pass it through */ |
| 227 | return 1; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | /* At this point we are sure that this is an ESPinUDP packet, |
| 232 | * so we need to remove 'len' bytes from the packet (the UDP |
| 233 | * header and optional ESP marker bytes) and then modify the |
| 234 | * protocol to ESP, and then call into the transform receiver. |
| 235 | */ |
| 236 | if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
| 237 | goto drop; |
| 238 | |
| 239 | /* Now we can update and verify the packet length... */ |
| 240 | iph = ip_hdr(skb); |
| 241 | iphlen = iph->ihl << 2; |
| 242 | iph->tot_len = htons(ntohs(iph->tot_len) - len); |
| 243 | if (skb->len < iphlen + len) { |
| 244 | /* packet is too small!?! */ |
| 245 | goto drop; |
| 246 | } |
| 247 | |
| 248 | /* pull the data buffer up to the ESP header and set the |
| 249 | * transport header to point to ESP. Keep UDP on the stack |
| 250 | * for later. |
| 251 | */ |
| 252 | __skb_pull(skb, len); |
| 253 | skb_reset_transport_header(skb); |
| 254 | |
| 255 | /* modify the protocol (it's ESP!) */ |
| 256 | iph->protocol = IPPROTO_ESP; |
| 257 | |
| 258 | /* process ESP */ |
| 259 | ret = xfrm4_rcv_encap(skb, encap_type); |
| 260 | return ret; |
| 261 | |
| 262 | drop: |
| 263 | kfree_skb(skb); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int xfrm4_rcv(struct sk_buff *skb) |
| 268 | { |
| 269 | return xfrm4_rcv_encap(skb, 0); |
| 270 | } |
| 271 | |
| 272 | EXPORT_SYMBOL(xfrm4_rcv); |