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 | |
| 19 | int xfrm4_rcv(struct sk_buff *skb) |
| 20 | { |
| 21 | return xfrm4_rcv_encap(skb, 0); |
| 22 | } |
| 23 | |
| 24 | EXPORT_SYMBOL(xfrm4_rcv); |
| 25 | |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 26 | 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] | 27 | { |
| 28 | switch (nexthdr) { |
| 29 | case IPPROTO_IPIP: |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 30 | case IPPROTO_IPV6: |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 31 | *spi = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | *seq = 0; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | return xfrm_parse_spi(skb, nexthdr, spi, seq); |
| 37 | } |
| 38 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 39 | #ifdef CONFIG_NETFILTER |
| 40 | static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb) |
| 41 | { |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 42 | if (skb->dst == NULL) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 43 | const struct iphdr *iph = ip_hdr(skb); |
| 44 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 45 | if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 46 | skb->dev)) |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 47 | goto drop; |
| 48 | } |
| 49 | return dst_input(skb); |
| 50 | drop: |
| 51 | kfree_skb(skb); |
| 52 | return NET_RX_DROP; |
| 53 | } |
| 54 | #endif |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
| 57 | { |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 58 | __be32 spi, seq; |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 59 | struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct xfrm_state *x; |
| 61 | int xfrm_nr = 0; |
| 62 | int decaps = 0; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 63 | int err = xfrm4_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 65 | if (err != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | goto drop; |
| 67 | |
| 68 | do { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 69 | const struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | if (xfrm_nr == XFRM_MAX_DEPTH) |
| 72 | goto drop; |
| 73 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 74 | x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, |
| 75 | iph->protocol != IPPROTO_IPV6 ? iph->protocol : IPPROTO_IPIP, AF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (x == NULL) |
| 77 | goto drop; |
| 78 | |
| 79 | spin_lock(&x->lock); |
| 80 | if (unlikely(x->km.state != XFRM_STATE_VALID)) |
| 81 | goto drop_unlock; |
| 82 | |
Herbert Xu | 8bf4b8a | 2006-04-04 12:51:05 -0700 | [diff] [blame] | 83 | if ((x->encap ? x->encap->encap_type : 0) != encap_type) |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 84 | goto drop_unlock; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (x->props.replay_window && xfrm_replay_check(x, seq)) |
| 87 | goto drop_unlock; |
| 88 | |
| 89 | if (xfrm_state_check_expire(x)) |
| 90 | goto drop_unlock; |
| 91 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 92 | if (x->type->input(x, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | goto drop_unlock; |
| 94 | |
| 95 | /* only the first xfrm gets the encap type */ |
| 96 | encap_type = 0; |
| 97 | |
| 98 | if (x->props.replay_window) |
| 99 | xfrm_replay_advance(x, seq); |
| 100 | |
| 101 | x->curlft.bytes += skb->len; |
| 102 | x->curlft.packets++; |
| 103 | |
| 104 | spin_unlock(&x->lock); |
| 105 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 106 | xfrm_vec[xfrm_nr++] = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 108 | if (x->mode->input(x, skb)) |
| 109 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 111 | if (x->props.mode == XFRM_MODE_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | decaps = 1; |
| 113 | break; |
| 114 | } |
| 115 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 116 | err = xfrm_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq); |
| 117 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | goto drop; |
| 119 | } while (!err); |
| 120 | |
| 121 | /* Allocate new secpath or COW existing one. */ |
| 122 | |
| 123 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { |
| 124 | struct sec_path *sp; |
| 125 | sp = secpath_dup(skb->sp); |
| 126 | if (!sp) |
| 127 | goto drop; |
| 128 | if (skb->sp) |
| 129 | secpath_put(skb->sp); |
| 130 | skb->sp = sp; |
| 131 | } |
| 132 | if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH) |
| 133 | goto drop; |
| 134 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 135 | memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec, |
| 136 | xfrm_nr * sizeof(xfrm_vec[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | skb->sp->len += xfrm_nr; |
| 138 | |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 139 | nf_reset(skb); |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (decaps) { |
| 142 | if (!(skb->dev->flags&IFF_LOOPBACK)) { |
| 143 | dst_release(skb->dst); |
| 144 | skb->dst = NULL; |
| 145 | } |
| 146 | netif_rx(skb); |
| 147 | return 0; |
| 148 | } else { |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 149 | #ifdef CONFIG_NETFILTER |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 150 | __skb_push(skb, skb->data - skb_network_header(skb)); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 151 | ip_hdr(skb)->tot_len = htons(skb->len); |
| 152 | ip_send_check(ip_hdr(skb)); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 153 | |
| 154 | NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 155 | xfrm4_rcv_encap_finish); |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | #else |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame^] | 158 | return -ip_hdr(skb)->protocol; |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 159 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | drop_unlock: |
| 163 | spin_unlock(&x->lock); |
| 164 | xfrm_state_put(x); |
| 165 | drop: |
| 166 | while (--xfrm_nr >= 0) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 167 | xfrm_state_put(xfrm_vec[xfrm_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | kfree_skb(skb); |
| 170 | return 0; |
| 171 | } |