Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm6_policy.c: based on xfrm4_policy.c |
| 3 | * |
| 4 | * Authors: |
| 5 | * Mitsuru KANDA @USAGI |
| 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 8 | * IPv6 support |
| 9 | * YOSHIFUJI Hideaki |
| 10 | * Split up af-specific portion |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
| 16 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/xfrm.h> |
| 18 | #include <net/ip.h> |
| 19 | #include <net/ipv6.h> |
| 20 | #include <net/ip6_route.h> |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 21 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 2ce4272 | 2006-08-23 20:39:03 -0700 | [diff] [blame] | 22 | #include <net/mip6.h> |
| 23 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static struct dst_ops xfrm6_dst_ops; |
| 26 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo; |
| 27 | |
Ville Nuorvala | 4251320 | 2006-10-16 22:10:05 -0700 | [diff] [blame] | 28 | static int xfrm6_dst_lookup(struct xfrm_dst **xdst, struct flowi *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
Ville Nuorvala | 4251320 | 2006-10-16 22:10:05 -0700 | [diff] [blame] | 30 | struct dst_entry *dst = ip6_route_output(NULL, fl); |
| 31 | int err = dst->error; |
| 32 | if (!err) |
| 33 | *xdst = (struct xfrm_dst *) dst; |
| 34 | else |
| 35 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | return err; |
| 37 | } |
| 38 | |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 39 | static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr) |
| 40 | { |
| 41 | struct rt6_info *rt; |
| 42 | struct flowi fl_tunnel = { |
| 43 | .nl_u = { |
| 44 | .ip6_u = { |
| 45 | .daddr = *(struct in6_addr *)&daddr->a6, |
| 46 | }, |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | if (!xfrm6_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) { |
| 51 | ipv6_get_saddr(&rt->u.dst, (struct in6_addr *)&daddr->a6, |
| 52 | (struct in6_addr *)&saddr->a6); |
| 53 | dst_release(&rt->u.dst); |
| 54 | return 0; |
| 55 | } |
| 56 | return -EHOSTUNREACH; |
| 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static struct dst_entry * |
| 60 | __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy) |
| 61 | { |
| 62 | struct dst_entry *dst; |
| 63 | |
| 64 | /* Still not clear if we should set fl->fl6_{src,dst}... */ |
| 65 | read_lock_bh(&policy->lock); |
| 66 | for (dst = policy->bundles; dst; dst = dst->next) { |
| 67 | struct xfrm_dst *xdst = (struct xfrm_dst*)dst; |
| 68 | struct in6_addr fl_dst_prefix, fl_src_prefix; |
| 69 | |
| 70 | ipv6_addr_prefix(&fl_dst_prefix, |
| 71 | &fl->fl6_dst, |
| 72 | xdst->u.rt6.rt6i_dst.plen); |
| 73 | ipv6_addr_prefix(&fl_src_prefix, |
| 74 | &fl->fl6_src, |
| 75 | xdst->u.rt6.rt6i_src.plen); |
| 76 | if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) && |
| 77 | ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) && |
Venkat Yekkirala | 5b368e6 | 2006-10-05 15:42:18 -0500 | [diff] [blame] | 78 | xfrm_bundle_ok(policy, xdst, fl, AF_INET6, |
Masahide NAKAMURA | e53820d | 2006-08-23 19:12:01 -0700 | [diff] [blame] | 79 | (xdst->u.rt6.rt6i_dst.plen != 128 || |
| 80 | xdst->u.rt6.rt6i_src.plen != 128))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | dst_clone(dst); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | read_unlock_bh(&policy->lock); |
| 86 | return dst; |
| 87 | } |
| 88 | |
Masahide NAKAMURA | 99505a8 | 2006-08-23 18:10:33 -0700 | [diff] [blame] | 89 | static inline struct in6_addr* |
| 90 | __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr) |
| 91 | { |
| 92 | return (x->type->remote_addr) ? |
| 93 | (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) : |
| 94 | (struct in6_addr*)&x->id.daddr; |
| 95 | } |
| 96 | |
| 97 | static inline struct in6_addr* |
| 98 | __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr) |
| 99 | { |
| 100 | return (x->type->local_addr) ? |
| 101 | (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) : |
| 102 | (struct in6_addr*)&x->props.saddr; |
| 103 | } |
| 104 | |
Masahide NAKAMURA | 1b5c229 | 2006-08-23 18:11:50 -0700 | [diff] [blame] | 105 | static inline void |
| 106 | __xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x) |
| 107 | { |
| 108 | if (x->type->flags & XFRM_TYPE_NON_FRAGMENT) |
| 109 | *nflen += x->props.header_len; |
| 110 | else |
| 111 | *len += x->props.header_len; |
| 112 | } |
| 113 | |
| 114 | static inline void |
| 115 | __xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x) |
| 116 | { |
| 117 | if (x->type->flags & XFRM_TYPE_NON_FRAGMENT) |
| 118 | *nflen -= x->props.header_len; |
| 119 | else |
| 120 | *len -= x->props.header_len; |
| 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 124 | * all the metrics... Shortly, bundle a bundle. |
| 125 | */ |
| 126 | |
| 127 | static int |
| 128 | __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx, |
| 129 | struct flowi *fl, struct dst_entry **dst_p) |
| 130 | { |
| 131 | struct dst_entry *dst, *dst_prev; |
| 132 | struct rt6_info *rt0 = (struct rt6_info*)(*dst_p); |
| 133 | struct rt6_info *rt = rt0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | struct flowi fl_tunnel = { |
| 135 | .nl_u = { |
| 136 | .ip6_u = { |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 137 | .saddr = fl->fl6_src, |
| 138 | .daddr = fl->fl6_dst, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | }; |
| 142 | int i; |
| 143 | int err = 0; |
| 144 | int header_len = 0; |
Masahide NAKAMURA | 1b5c229 | 2006-08-23 18:11:50 -0700 | [diff] [blame] | 145 | int nfheader_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | int trailer_len = 0; |
| 147 | |
| 148 | dst = dst_prev = NULL; |
| 149 | dst_hold(&rt->u.dst); |
| 150 | |
| 151 | for (i = 0; i < nx; i++) { |
| 152 | struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops); |
| 153 | struct xfrm_dst *xdst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | if (unlikely(dst1 == NULL)) { |
| 156 | err = -ENOBUFS; |
| 157 | dst_release(&rt->u.dst); |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | if (!dst) |
| 162 | dst = dst1; |
| 163 | else { |
| 164 | dst_prev->child = dst1; |
| 165 | dst1->flags |= DST_NOHASH; |
| 166 | dst_clone(dst1); |
| 167 | } |
| 168 | |
| 169 | xdst = (struct xfrm_dst *)dst1; |
| 170 | xdst->route = &rt->u.dst; |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 171 | xdst->genid = xfrm[i]->genid; |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 172 | if (rt->rt6i_node) |
| 173 | xdst->route_cookie = rt->rt6i_node->fn_sernum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | dst1->next = dst_prev; |
| 176 | dst_prev = dst1; |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 177 | |
Masahide NAKAMURA | 1b5c229 | 2006-08-23 18:11:50 -0700 | [diff] [blame] | 178 | __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | trailer_len += xfrm[i]->props.trailer_len; |
| 180 | |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 181 | if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) { |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 182 | unsigned short encap_family = xfrm[i]->props.family; |
| 183 | switch(encap_family) { |
| 184 | case AF_INET: |
| 185 | fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4; |
| 186 | fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4; |
| 187 | break; |
| 188 | case AF_INET6: |
Masahide NAKAMURA | bda390d | 2007-02-09 21:22:16 -0800 | [diff] [blame] | 189 | ipv6_addr_copy(&fl_tunnel.fl6_dst, __xfrm6_bundle_addr_remote(xfrm[i], &fl->fl6_dst)); |
| 190 | |
Noriaki TAKAMIYA | d3f23df | 2007-02-20 01:08:41 -0800 | [diff] [blame] | 191 | ipv6_addr_copy(&fl_tunnel.fl6_src, __xfrm6_bundle_addr_local(xfrm[i], &fl->fl6_src)); |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 192 | break; |
| 193 | default: |
| 194 | BUG_ON(1); |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | err = xfrm_dst_lookup((struct xfrm_dst **) &rt, |
Miika Komu | c82f963 | 2007-02-06 14:27:02 -0800 | [diff] [blame] | 198 | &fl_tunnel, encap_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (err) |
| 200 | goto error; |
| 201 | } else |
| 202 | dst_hold(&rt->u.dst); |
| 203 | } |
| 204 | |
| 205 | dst_prev->child = &rt->u.dst; |
| 206 | dst->path = &rt->u.dst; |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 207 | if (rt->rt6i_node) |
| 208 | ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | *dst_p = dst; |
| 211 | dst = dst_prev; |
| 212 | |
| 213 | dst_prev = *dst_p; |
| 214 | i = 0; |
| 215 | for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) { |
| 216 | struct xfrm_dst *x = (struct xfrm_dst*)dst_prev; |
| 217 | |
| 218 | dst_prev->xfrm = xfrm[i++]; |
| 219 | dst_prev->dev = rt->u.dst.dev; |
| 220 | if (rt->u.dst.dev) |
| 221 | dev_hold(rt->u.dst.dev); |
| 222 | dst_prev->obsolete = -1; |
| 223 | dst_prev->flags |= DST_HOST; |
| 224 | dst_prev->lastuse = jiffies; |
| 225 | dst_prev->header_len = header_len; |
Masahide NAKAMURA | 1b5c229 | 2006-08-23 18:11:50 -0700 | [diff] [blame] | 226 | dst_prev->nfheader_len = nfheader_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | dst_prev->trailer_len = trailer_len; |
| 228 | memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics)); |
| 229 | |
| 230 | /* Copy neighbour for reachability confirmation */ |
| 231 | dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour); |
| 232 | dst_prev->input = rt->u.dst.input; |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 233 | dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* Sheit... I remember I did this right. Apparently, |
| 235 | * it was magically lost, so this code needs audit */ |
YOSHIFUJI Hideaki | 1df2e44 | 2007-12-07 23:50:40 -0800 | [diff] [blame] | 236 | x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | x->u.rt6.rt6i_metric = rt0->rt6i_metric; |
| 238 | x->u.rt6.rt6i_node = rt0->rt6i_node; |
| 239 | x->u.rt6.rt6i_gateway = rt0->rt6i_gateway; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 240 | memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | x->u.rt6.rt6i_dst = rt0->rt6i_dst; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 242 | x->u.rt6.rt6i_src = rt0->rt6i_src; |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 243 | x->u.rt6.rt6i_idev = rt0->rt6i_idev; |
| 244 | in6_dev_hold(rt0->rt6i_idev); |
Masahide NAKAMURA | 1b5c229 | 2006-08-23 18:11:50 -0700 | [diff] [blame] | 245 | __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | trailer_len -= x->u.dst.xfrm->props.trailer_len; |
| 247 | } |
| 248 | |
| 249 | xfrm_init_pmtu(dst); |
| 250 | return 0; |
| 251 | |
| 252 | error: |
| 253 | if (dst) |
| 254 | dst_free(dst); |
| 255 | return err; |
| 256 | } |
| 257 | |
| 258 | static inline void |
| 259 | _decode_session6(struct sk_buff *skb, struct flowi *fl) |
| 260 | { |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 261 | u16 offset = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 262 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | e3cae90 | 2006-04-18 14:46:52 -0700 | [diff] [blame] | 263 | struct ipv6_opt_hdr *exthdr; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 264 | const unsigned char *nh = skb_network_header(skb); |
| 265 | u8 nexthdr = nh[IP6CB(skb)->nhoff]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | memset(fl, 0, sizeof(struct flowi)); |
| 268 | ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr); |
| 269 | ipv6_addr_copy(&fl->fl6_src, &hdr->saddr); |
| 270 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 271 | while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) { |
| 272 | nh = skb_network_header(skb); |
| 273 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
YOSHIFUJI Hideaki | e3cae90 | 2006-04-18 14:46:52 -0700 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | switch (nexthdr) { |
| 276 | case NEXTHDR_ROUTING: |
| 277 | case NEXTHDR_HOP: |
| 278 | case NEXTHDR_DEST: |
| 279 | offset += ipv6_optlen(exthdr); |
| 280 | nexthdr = exthdr->nexthdr; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 281 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | break; |
| 283 | |
| 284 | case IPPROTO_UDP: |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 285 | case IPPROTO_UDPLITE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | case IPPROTO_TCP: |
| 287 | case IPPROTO_SCTP: |
Patrick McHardy | 9e99999 | 2005-12-19 14:03:46 -0800 | [diff] [blame] | 288 | case IPPROTO_DCCP: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 289 | if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) { |
Al Viro | 8c689a6 | 2006-11-08 00:20:21 -0800 | [diff] [blame] | 290 | __be16 *ports = (__be16 *)exthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | fl->fl_ip_sport = ports[0]; |
| 293 | fl->fl_ip_dport = ports[1]; |
| 294 | } |
| 295 | fl->proto = nexthdr; |
| 296 | return; |
| 297 | |
| 298 | case IPPROTO_ICMPV6: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 299 | if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | u8 *icmp = (u8 *)exthdr; |
| 301 | |
| 302 | fl->fl_icmp_type = icmp[0]; |
| 303 | fl->fl_icmp_code = icmp[1]; |
| 304 | } |
| 305 | fl->proto = nexthdr; |
| 306 | return; |
| 307 | |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 308 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 2ce4272 | 2006-08-23 20:39:03 -0700 | [diff] [blame] | 309 | case IPPROTO_MH: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 310 | if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) { |
Masahide NAKAMURA | 2ce4272 | 2006-08-23 20:39:03 -0700 | [diff] [blame] | 311 | struct ip6_mh *mh; |
| 312 | mh = (struct ip6_mh *)exthdr; |
| 313 | |
| 314 | fl->fl_mh_type = mh->ip6mh_type; |
| 315 | } |
| 316 | fl->proto = nexthdr; |
| 317 | return; |
| 318 | #endif |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* XXX Why are there these headers? */ |
| 321 | case IPPROTO_AH: |
| 322 | case IPPROTO_ESP: |
| 323 | case IPPROTO_COMP: |
| 324 | default: |
| 325 | fl->fl_ipsec_spi = 0; |
| 326 | fl->proto = nexthdr; |
| 327 | return; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | static inline int xfrm6_garbage_collect(void) |
| 333 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | xfrm6_policy_afinfo.garbage_collect(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2); |
| 336 | } |
| 337 | |
| 338 | static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu) |
| 339 | { |
| 340 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 341 | struct dst_entry *path = xdst->route; |
| 342 | |
| 343 | path->ops->update_pmtu(path, mtu); |
| 344 | } |
| 345 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 346 | static void xfrm6_dst_destroy(struct dst_entry *dst) |
| 347 | { |
| 348 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 349 | |
| 350 | if (likely(xdst->u.rt6.rt6i_idev)) |
| 351 | in6_dev_put(xdst->u.rt6.rt6i_idev); |
| 352 | xfrm_dst_destroy(xdst); |
| 353 | } |
| 354 | |
| 355 | static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 356 | int unregister) |
| 357 | { |
| 358 | struct xfrm_dst *xdst; |
| 359 | |
| 360 | if (!unregister) |
| 361 | return; |
| 362 | |
| 363 | xdst = (struct xfrm_dst *)dst; |
| 364 | if (xdst->u.rt6.rt6i_idev->dev == dev) { |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 365 | struct inet6_dev *loopback_idev = in6_dev_get(init_net.loopback_dev); |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 366 | BUG_ON(!loopback_idev); |
| 367 | |
| 368 | do { |
| 369 | in6_dev_put(xdst->u.rt6.rt6i_idev); |
| 370 | xdst->u.rt6.rt6i_idev = loopback_idev; |
| 371 | in6_dev_hold(loopback_idev); |
| 372 | xdst = (struct xfrm_dst *)xdst->u.dst.child; |
| 373 | } while (xdst->u.dst.xfrm); |
| 374 | |
| 375 | __in6_dev_put(loopback_idev); |
| 376 | } |
| 377 | |
| 378 | xfrm_dst_ifdown(dst, dev); |
| 379 | } |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | static struct dst_ops xfrm6_dst_ops = { |
| 382 | .family = AF_INET6, |
| 383 | .protocol = __constant_htons(ETH_P_IPV6), |
| 384 | .gc = xfrm6_garbage_collect, |
| 385 | .update_pmtu = xfrm6_update_pmtu, |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 386 | .destroy = xfrm6_dst_destroy, |
| 387 | .ifdown = xfrm6_dst_ifdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | .gc_thresh = 1024, |
| 389 | .entry_size = sizeof(struct xfrm_dst), |
| 390 | }; |
| 391 | |
| 392 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { |
| 393 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | .dst_ops = &xfrm6_dst_ops, |
| 395 | .dst_lookup = xfrm6_dst_lookup, |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 396 | .get_saddr = xfrm6_get_saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | .find_bundle = __xfrm6_find_bundle, |
| 398 | .bundle_create = __xfrm6_bundle_create, |
| 399 | .decode_session = _decode_session6, |
| 400 | }; |
| 401 | |
| 402 | static void __init xfrm6_policy_init(void) |
| 403 | { |
| 404 | xfrm_policy_register_afinfo(&xfrm6_policy_afinfo); |
| 405 | } |
| 406 | |
| 407 | static void xfrm6_policy_fini(void) |
| 408 | { |
| 409 | xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo); |
| 410 | } |
| 411 | |
| 412 | void __init xfrm6_init(void) |
| 413 | { |
| 414 | xfrm6_policy_init(); |
| 415 | xfrm6_state_init(); |
| 416 | } |
| 417 | |
| 418 | void xfrm6_fini(void) |
| 419 | { |
| 420 | //xfrm6_input_fini(); |
| 421 | xfrm6_policy_fini(); |
| 422 | xfrm6_state_fini(); |
| 423 | } |