0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/err.h>
0016 #include <linux/kernel.h>
0017 #include <linux/netdevice.h>
0018 #include <net/addrconf.h>
0019 #include <net/dst.h>
0020 #include <net/xfrm.h>
0021 #include <net/ip.h>
0022 #include <net/ipv6.h>
0023 #include <net/ip6_route.h>
0024 #include <net/l3mdev.h>
0025
0026 static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
0027 const xfrm_address_t *saddr,
0028 const xfrm_address_t *daddr,
0029 u32 mark)
0030 {
0031 struct flowi6 fl6;
0032 struct dst_entry *dst;
0033 int err;
0034
0035 memset(&fl6, 0, sizeof(fl6));
0036 fl6.flowi6_l3mdev = l3mdev_master_ifindex_by_index(net, oif);
0037 fl6.flowi6_mark = mark;
0038 memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
0039 if (saddr)
0040 memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
0041
0042 dst = ip6_route_output(net, NULL, &fl6);
0043
0044 err = dst->error;
0045 if (dst->error) {
0046 dst_release(dst);
0047 dst = ERR_PTR(err);
0048 }
0049
0050 return dst;
0051 }
0052
0053 static int xfrm6_get_saddr(struct net *net, int oif,
0054 xfrm_address_t *saddr, xfrm_address_t *daddr,
0055 u32 mark)
0056 {
0057 struct dst_entry *dst;
0058 struct net_device *dev;
0059
0060 dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr, mark);
0061 if (IS_ERR(dst))
0062 return -EHOSTUNREACH;
0063
0064 dev = ip6_dst_idev(dst)->dev;
0065 ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
0066 dst_release(dst);
0067 return 0;
0068 }
0069
0070 static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
0071 const struct flowi *fl)
0072 {
0073 struct rt6_info *rt = (struct rt6_info *)xdst->route;
0074
0075 xdst->u.dst.dev = dev;
0076 netdev_hold(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
0077
0078 xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
0079 if (!xdst->u.rt6.rt6i_idev) {
0080 netdev_put(dev, &xdst->u.dst.dev_tracker);
0081 return -ENODEV;
0082 }
0083
0084
0085
0086 xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
0087 RTF_LOCAL);
0088 xdst->route_cookie = rt6_get_cookie(rt);
0089 xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
0090 xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
0091 xdst->u.rt6.rt6i_src = rt->rt6i_src;
0092 INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
0093 rt6_uncached_list_add(&xdst->u.rt6);
0094
0095 return 0;
0096 }
0097
0098 static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
0099 struct sk_buff *skb, u32 mtu,
0100 bool confirm_neigh)
0101 {
0102 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
0103 struct dst_entry *path = xdst->route;
0104
0105 path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
0106 }
0107
0108 static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
0109 struct sk_buff *skb)
0110 {
0111 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
0112 struct dst_entry *path = xdst->route;
0113
0114 path->ops->redirect(path, sk, skb);
0115 }
0116
0117 static void xfrm6_dst_destroy(struct dst_entry *dst)
0118 {
0119 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
0120
0121 if (likely(xdst->u.rt6.rt6i_idev))
0122 in6_dev_put(xdst->u.rt6.rt6i_idev);
0123 dst_destroy_metrics_generic(dst);
0124 if (xdst->u.rt6.rt6i_uncached_list)
0125 rt6_uncached_list_del(&xdst->u.rt6);
0126 xfrm_dst_destroy(xdst);
0127 }
0128
0129 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
0130 int unregister)
0131 {
0132 struct xfrm_dst *xdst;
0133
0134 if (!unregister)
0135 return;
0136
0137 xdst = (struct xfrm_dst *)dst;
0138 if (xdst->u.rt6.rt6i_idev->dev == dev) {
0139 struct inet6_dev *loopback_idev =
0140 in6_dev_get(dev_net(dev)->loopback_dev);
0141
0142 do {
0143 in6_dev_put(xdst->u.rt6.rt6i_idev);
0144 xdst->u.rt6.rt6i_idev = loopback_idev;
0145 in6_dev_hold(loopback_idev);
0146 xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
0147 } while (xdst->u.dst.xfrm);
0148
0149 __in6_dev_put(loopback_idev);
0150 }
0151
0152 xfrm_dst_ifdown(dst, dev);
0153 }
0154
0155 static struct dst_ops xfrm6_dst_ops_template = {
0156 .family = AF_INET6,
0157 .update_pmtu = xfrm6_update_pmtu,
0158 .redirect = xfrm6_redirect,
0159 .cow_metrics = dst_cow_metrics_generic,
0160 .destroy = xfrm6_dst_destroy,
0161 .ifdown = xfrm6_dst_ifdown,
0162 .local_out = __ip6_local_out,
0163 .gc_thresh = 32768,
0164 };
0165
0166 static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
0167 .dst_ops = &xfrm6_dst_ops_template,
0168 .dst_lookup = xfrm6_dst_lookup,
0169 .get_saddr = xfrm6_get_saddr,
0170 .fill_dst = xfrm6_fill_dst,
0171 .blackhole_route = ip6_blackhole_route,
0172 };
0173
0174 static int __init xfrm6_policy_init(void)
0175 {
0176 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
0177 }
0178
0179 static void xfrm6_policy_fini(void)
0180 {
0181 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
0182 }
0183
0184 #ifdef CONFIG_SYSCTL
0185 static struct ctl_table xfrm6_policy_table[] = {
0186 {
0187 .procname = "xfrm6_gc_thresh",
0188 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
0189 .maxlen = sizeof(int),
0190 .mode = 0644,
0191 .proc_handler = proc_dointvec,
0192 },
0193 { }
0194 };
0195
0196 static int __net_init xfrm6_net_sysctl_init(struct net *net)
0197 {
0198 struct ctl_table *table;
0199 struct ctl_table_header *hdr;
0200
0201 table = xfrm6_policy_table;
0202 if (!net_eq(net, &init_net)) {
0203 table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
0204 if (!table)
0205 goto err_alloc;
0206
0207 table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
0208 }
0209
0210 hdr = register_net_sysctl(net, "net/ipv6", table);
0211 if (!hdr)
0212 goto err_reg;
0213
0214 net->ipv6.sysctl.xfrm6_hdr = hdr;
0215 return 0;
0216
0217 err_reg:
0218 if (!net_eq(net, &init_net))
0219 kfree(table);
0220 err_alloc:
0221 return -ENOMEM;
0222 }
0223
0224 static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
0225 {
0226 struct ctl_table *table;
0227
0228 if (!net->ipv6.sysctl.xfrm6_hdr)
0229 return;
0230
0231 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
0232 unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
0233 if (!net_eq(net, &init_net))
0234 kfree(table);
0235 }
0236 #else
0237 static inline int xfrm6_net_sysctl_init(struct net *net)
0238 {
0239 return 0;
0240 }
0241
0242 static inline void xfrm6_net_sysctl_exit(struct net *net)
0243 {
0244 }
0245 #endif
0246
0247 static int __net_init xfrm6_net_init(struct net *net)
0248 {
0249 int ret;
0250
0251 memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
0252 sizeof(xfrm6_dst_ops_template));
0253 ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
0254 if (ret)
0255 return ret;
0256
0257 ret = xfrm6_net_sysctl_init(net);
0258 if (ret)
0259 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
0260
0261 return ret;
0262 }
0263
0264 static void __net_exit xfrm6_net_exit(struct net *net)
0265 {
0266 xfrm6_net_sysctl_exit(net);
0267 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
0268 }
0269
0270 static struct pernet_operations xfrm6_net_ops = {
0271 .init = xfrm6_net_init,
0272 .exit = xfrm6_net_exit,
0273 };
0274
0275 int __init xfrm6_init(void)
0276 {
0277 int ret;
0278
0279 ret = xfrm6_policy_init();
0280 if (ret)
0281 goto out;
0282 ret = xfrm6_state_init();
0283 if (ret)
0284 goto out_policy;
0285
0286 ret = xfrm6_protocol_init();
0287 if (ret)
0288 goto out_state;
0289
0290 register_pernet_subsys(&xfrm6_net_ops);
0291 out:
0292 return ret;
0293 out_state:
0294 xfrm6_state_fini();
0295 out_policy:
0296 xfrm6_policy_fini();
0297 goto out;
0298 }
0299
0300 void xfrm6_fini(void)
0301 {
0302 unregister_pernet_subsys(&xfrm6_net_ops);
0303 xfrm6_protocol_fini();
0304 xfrm6_policy_fini();
0305 xfrm6_state_fini();
0306 }