Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm4_policy.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * Kazunori MIYAZAWA @USAGI |
| 6 | * YOSHIFUJI Hideaki @USAGI |
| 7 | * Split up af-specific portion |
| 8 | * |
| 9 | */ |
| 10 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/config.h> |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 13 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <net/xfrm.h> |
| 15 | #include <net/ip.h> |
| 16 | |
| 17 | static struct dst_ops xfrm4_dst_ops; |
| 18 | static struct xfrm_policy_afinfo xfrm4_policy_afinfo; |
| 19 | |
| 20 | static struct xfrm_type_map xfrm4_type_map = { .lock = RW_LOCK_UNLOCKED }; |
| 21 | |
| 22 | static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl) |
| 23 | { |
| 24 | return __ip_route_output_key((struct rtable**)dst, fl); |
| 25 | } |
| 26 | |
| 27 | static struct dst_entry * |
| 28 | __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy) |
| 29 | { |
| 30 | struct dst_entry *dst; |
| 31 | |
| 32 | read_lock_bh(&policy->lock); |
| 33 | for (dst = policy->bundles; dst; dst = dst->next) { |
| 34 | struct xfrm_dst *xdst = (struct xfrm_dst*)dst; |
| 35 | if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/ |
| 36 | xdst->u.rt.fl.fl4_dst == fl->fl4_dst && |
| 37 | xdst->u.rt.fl.fl4_src == fl->fl4_src && |
Herbert Xu | 4da3089 | 2006-02-23 16:19:26 -0800 | [diff] [blame] | 38 | xdst->u.rt.fl.fl4_tos == fl->fl4_tos && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | xfrm_bundle_ok(xdst, fl, AF_INET)) { |
| 40 | dst_clone(dst); |
| 41 | break; |
| 42 | } |
| 43 | } |
| 44 | read_unlock_bh(&policy->lock); |
| 45 | return dst; |
| 46 | } |
| 47 | |
| 48 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 49 | * all the metrics... Shortly, bundle a bundle. |
| 50 | */ |
| 51 | |
| 52 | static int |
| 53 | __xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx, |
| 54 | struct flowi *fl, struct dst_entry **dst_p) |
| 55 | { |
| 56 | struct dst_entry *dst, *dst_prev; |
| 57 | struct rtable *rt0 = (struct rtable*)(*dst_p); |
| 58 | struct rtable *rt = rt0; |
| 59 | u32 remote = fl->fl4_dst; |
| 60 | u32 local = fl->fl4_src; |
| 61 | struct flowi fl_tunnel = { |
| 62 | .nl_u = { |
| 63 | .ip4_u = { |
| 64 | .saddr = local, |
Herbert Xu | 4da3089 | 2006-02-23 16:19:26 -0800 | [diff] [blame] | 65 | .daddr = remote, |
| 66 | .tos = fl->fl4_tos |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | }; |
| 70 | int i; |
| 71 | int err; |
| 72 | int header_len = 0; |
| 73 | int trailer_len = 0; |
| 74 | |
| 75 | dst = dst_prev = NULL; |
| 76 | dst_hold(&rt->u.dst); |
| 77 | |
| 78 | for (i = 0; i < nx; i++) { |
| 79 | struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops); |
| 80 | struct xfrm_dst *xdst; |
| 81 | int tunnel = 0; |
| 82 | |
| 83 | if (unlikely(dst1 == NULL)) { |
| 84 | err = -ENOBUFS; |
| 85 | dst_release(&rt->u.dst); |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | if (!dst) |
| 90 | dst = dst1; |
| 91 | else { |
| 92 | dst_prev->child = dst1; |
| 93 | dst1->flags |= DST_NOHASH; |
| 94 | dst_clone(dst1); |
| 95 | } |
| 96 | |
| 97 | xdst = (struct xfrm_dst *)dst1; |
| 98 | xdst->route = &rt->u.dst; |
| 99 | |
| 100 | dst1->next = dst_prev; |
| 101 | dst_prev = dst1; |
| 102 | if (xfrm[i]->props.mode) { |
| 103 | remote = xfrm[i]->id.daddr.a4; |
| 104 | local = xfrm[i]->props.saddr.a4; |
| 105 | tunnel = 1; |
| 106 | } |
| 107 | header_len += xfrm[i]->props.header_len; |
| 108 | trailer_len += xfrm[i]->props.trailer_len; |
| 109 | |
| 110 | if (tunnel) { |
| 111 | fl_tunnel.fl4_src = local; |
| 112 | fl_tunnel.fl4_dst = remote; |
| 113 | err = xfrm_dst_lookup((struct xfrm_dst **)&rt, |
| 114 | &fl_tunnel, AF_INET); |
| 115 | if (err) |
| 116 | goto error; |
| 117 | } else |
| 118 | dst_hold(&rt->u.dst); |
| 119 | } |
| 120 | |
| 121 | dst_prev->child = &rt->u.dst; |
| 122 | dst->path = &rt->u.dst; |
| 123 | |
| 124 | *dst_p = dst; |
| 125 | dst = dst_prev; |
| 126 | |
| 127 | dst_prev = *dst_p; |
| 128 | i = 0; |
| 129 | for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) { |
| 130 | struct xfrm_dst *x = (struct xfrm_dst*)dst_prev; |
| 131 | x->u.rt.fl = *fl; |
| 132 | |
| 133 | dst_prev->xfrm = xfrm[i++]; |
| 134 | dst_prev->dev = rt->u.dst.dev; |
| 135 | if (rt->u.dst.dev) |
| 136 | dev_hold(rt->u.dst.dev); |
| 137 | dst_prev->obsolete = -1; |
| 138 | dst_prev->flags |= DST_HOST; |
| 139 | dst_prev->lastuse = jiffies; |
| 140 | dst_prev->header_len = header_len; |
| 141 | dst_prev->trailer_len = trailer_len; |
| 142 | memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics)); |
| 143 | |
| 144 | /* Copy neighbout for reachability confirmation */ |
| 145 | dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour); |
| 146 | dst_prev->input = rt->u.dst.input; |
| 147 | dst_prev->output = xfrm4_output; |
| 148 | if (rt->peer) |
| 149 | atomic_inc(&rt->peer->refcnt); |
| 150 | x->u.rt.peer = rt->peer; |
| 151 | /* Sheit... I remember I did this right. Apparently, |
| 152 | * it was magically lost, so this code needs audit */ |
| 153 | x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL); |
| 154 | x->u.rt.rt_type = rt->rt_type; |
| 155 | x->u.rt.rt_src = rt0->rt_src; |
| 156 | x->u.rt.rt_dst = rt0->rt_dst; |
| 157 | x->u.rt.rt_gateway = rt->rt_gateway; |
| 158 | x->u.rt.rt_spec_dst = rt0->rt_spec_dst; |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 159 | x->u.rt.idev = rt0->idev; |
| 160 | in_dev_hold(rt0->idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | header_len -= x->u.dst.xfrm->props.header_len; |
| 162 | trailer_len -= x->u.dst.xfrm->props.trailer_len; |
| 163 | } |
| 164 | |
| 165 | xfrm_init_pmtu(dst); |
| 166 | return 0; |
| 167 | |
| 168 | error: |
| 169 | if (dst) |
| 170 | dst_free(dst); |
| 171 | return err; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | _decode_session4(struct sk_buff *skb, struct flowi *fl) |
| 176 | { |
| 177 | struct iphdr *iph = skb->nh.iph; |
| 178 | u8 *xprth = skb->nh.raw + iph->ihl*4; |
| 179 | |
| 180 | memset(fl, 0, sizeof(struct flowi)); |
| 181 | if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) { |
| 182 | switch (iph->protocol) { |
| 183 | case IPPROTO_UDP: |
| 184 | case IPPROTO_TCP: |
| 185 | case IPPROTO_SCTP: |
Patrick McHardy | 9e99999 | 2005-12-19 14:03:46 -0800 | [diff] [blame] | 186 | case IPPROTO_DCCP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 188 | u16 *ports = (u16 *)xprth; |
| 189 | |
| 190 | fl->fl_ip_sport = ports[0]; |
| 191 | fl->fl_ip_dport = ports[1]; |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | case IPPROTO_ICMP: |
| 196 | if (pskb_may_pull(skb, xprth + 2 - skb->data)) { |
| 197 | u8 *icmp = xprth; |
| 198 | |
| 199 | fl->fl_icmp_type = icmp[0]; |
| 200 | fl->fl_icmp_code = icmp[1]; |
| 201 | } |
| 202 | break; |
| 203 | |
| 204 | case IPPROTO_ESP: |
| 205 | if (pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 206 | u32 *ehdr = (u32 *)xprth; |
| 207 | |
| 208 | fl->fl_ipsec_spi = ehdr[0]; |
| 209 | } |
| 210 | break; |
| 211 | |
| 212 | case IPPROTO_AH: |
| 213 | if (pskb_may_pull(skb, xprth + 8 - skb->data)) { |
| 214 | u32 *ah_hdr = (u32*)xprth; |
| 215 | |
| 216 | fl->fl_ipsec_spi = ah_hdr[1]; |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | case IPPROTO_COMP: |
| 221 | if (pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 222 | u16 *ipcomp_hdr = (u16 *)xprth; |
| 223 | |
| 224 | fl->fl_ipsec_spi = ntohl(ntohs(ipcomp_hdr[1])); |
| 225 | } |
| 226 | break; |
| 227 | default: |
| 228 | fl->fl_ipsec_spi = 0; |
| 229 | break; |
| 230 | }; |
| 231 | } |
| 232 | fl->proto = iph->protocol; |
| 233 | fl->fl4_dst = iph->daddr; |
| 234 | fl->fl4_src = iph->saddr; |
Herbert Xu | 4da3089 | 2006-02-23 16:19:26 -0800 | [diff] [blame] | 235 | fl->fl4_tos = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static inline int xfrm4_garbage_collect(void) |
| 239 | { |
| 240 | read_lock(&xfrm4_policy_afinfo.lock); |
| 241 | xfrm4_policy_afinfo.garbage_collect(); |
| 242 | read_unlock(&xfrm4_policy_afinfo.lock); |
| 243 | return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2); |
| 244 | } |
| 245 | |
| 246 | static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu) |
| 247 | { |
| 248 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 249 | struct dst_entry *path = xdst->route; |
| 250 | |
| 251 | path->ops->update_pmtu(path, mtu); |
| 252 | } |
| 253 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 254 | static void xfrm4_dst_destroy(struct dst_entry *dst) |
| 255 | { |
| 256 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 257 | |
| 258 | if (likely(xdst->u.rt.idev)) |
| 259 | in_dev_put(xdst->u.rt.idev); |
| 260 | xfrm_dst_destroy(xdst); |
| 261 | } |
| 262 | |
| 263 | static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 264 | int unregister) |
| 265 | { |
| 266 | struct xfrm_dst *xdst; |
| 267 | |
| 268 | if (!unregister) |
| 269 | return; |
| 270 | |
| 271 | xdst = (struct xfrm_dst *)dst; |
| 272 | if (xdst->u.rt.idev->dev == dev) { |
| 273 | struct in_device *loopback_idev = in_dev_get(&loopback_dev); |
| 274 | BUG_ON(!loopback_idev); |
| 275 | |
| 276 | do { |
| 277 | in_dev_put(xdst->u.rt.idev); |
| 278 | xdst->u.rt.idev = loopback_idev; |
| 279 | in_dev_hold(loopback_idev); |
| 280 | xdst = (struct xfrm_dst *)xdst->u.dst.child; |
| 281 | } while (xdst->u.dst.xfrm); |
| 282 | |
| 283 | __in_dev_put(loopback_idev); |
| 284 | } |
| 285 | |
| 286 | xfrm_dst_ifdown(dst, dev); |
| 287 | } |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | static struct dst_ops xfrm4_dst_ops = { |
| 290 | .family = AF_INET, |
| 291 | .protocol = __constant_htons(ETH_P_IP), |
| 292 | .gc = xfrm4_garbage_collect, |
| 293 | .update_pmtu = xfrm4_update_pmtu, |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 294 | .destroy = xfrm4_dst_destroy, |
| 295 | .ifdown = xfrm4_dst_ifdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | .gc_thresh = 1024, |
| 297 | .entry_size = sizeof(struct xfrm_dst), |
| 298 | }; |
| 299 | |
| 300 | static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { |
| 301 | .family = AF_INET, |
| 302 | .lock = RW_LOCK_UNLOCKED, |
| 303 | .type_map = &xfrm4_type_map, |
| 304 | .dst_ops = &xfrm4_dst_ops, |
| 305 | .dst_lookup = xfrm4_dst_lookup, |
| 306 | .find_bundle = __xfrm4_find_bundle, |
| 307 | .bundle_create = __xfrm4_bundle_create, |
| 308 | .decode_session = _decode_session4, |
| 309 | }; |
| 310 | |
| 311 | static void __init xfrm4_policy_init(void) |
| 312 | { |
| 313 | xfrm_policy_register_afinfo(&xfrm4_policy_afinfo); |
| 314 | } |
| 315 | |
| 316 | static void __exit xfrm4_policy_fini(void) |
| 317 | { |
| 318 | xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo); |
| 319 | } |
| 320 | |
| 321 | void __init xfrm4_init(void) |
| 322 | { |
| 323 | xfrm4_state_init(); |
| 324 | xfrm4_policy_init(); |
| 325 | } |
| 326 | |