blob: e0f71c01d7289d7ba6213af131d647c8bae322e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_policy.c: based on xfrm4_policy.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
Ian Morris67ba4152014-08-24 21:53:10 +01006 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki
10 * Split up af-specific portion
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Herbert Xu66cdb3c2007-11-13 21:37:28 -080014#include <linux/err.h>
15#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070016#include <linux/netdevice.h>
17#include <net/addrconf.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080018#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/xfrm.h>
20#include <net/ip.h>
21#include <net/ipv6.h>
22#include <net/ip6_route.h>
David Ahern385add92015-09-29 20:07:13 -070023#include <net/l3mdev.h>
Amerigo Wang07a93622012-10-29 16:23:10 +000024#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -070025#include <net/mip6.h>
26#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
29
David Ahern42a7b322015-08-10 16:58:11 -060030static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
David S. Miller5e6b9302011-02-24 00:14:45 -050031 const xfrm_address_t *saddr,
32 const xfrm_address_t *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050034 struct flowi6 fl6;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080035 struct dst_entry *dst;
36 int err;
37
David S. Miller7e1dc7b2011-03-12 02:42:11 -050038 memset(&fl6, 0, sizeof(fl6));
David Ahern11d7a0b2016-08-14 19:52:56 -070039 fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
David Ahern41489872015-10-05 08:32:51 -060040 fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050041 memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080042 if (saddr)
David S. Miller7e1dc7b2011-03-12 02:42:11 -050043 memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080044
David S. Miller4c9483b2011-03-12 16:22:43 -050045 dst = ip6_route_output(net, NULL, &fl6);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080046
47 err = dst->error;
48 if (dst->error) {
Ville Nuorvala42513202006-10-16 22:10:05 -070049 dst_release(dst);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080050 dst = ERR_PTR(err);
51 }
52
53 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
David Ahern42a7b322015-08-10 16:58:11 -060056static int xfrm6_get_saddr(struct net *net, int oif,
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -080057 xfrm_address_t *saddr, xfrm_address_t *daddr)
Patrick McHardya1e59ab2006-09-19 12:57:34 -070058{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080059 struct dst_entry *dst;
Brian Haley191cd582008-08-14 15:33:21 -070060 struct net_device *dev;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070061
David Ahern42a7b322015-08-10 16:58:11 -060062 dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080063 if (IS_ERR(dst))
64 return -EHOSTUNREACH;
65
Brian Haley191cd582008-08-14 15:33:21 -070066 dev = ip6_dst_idev(dst)->dev;
Jiri Benc15e318b2015-03-29 16:59:24 +020067 ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080068 dst_release(dst);
69 return 0;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070070}
71
David S. Miller05d84022011-02-22 17:47:10 -080072static int xfrm6_get_tos(const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
Herbert Xu25ee3282007-12-11 09:32:34 -080075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -080077static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
78 int nfheader_len)
79{
80 if (dst->ops->family == AF_INET6) {
Ian Morris67ba4152014-08-24 21:53:10 +010081 struct rt6_info *rt = (struct rt6_info *)dst;
Martin KaFai Laub197df42015-05-22 20:56:01 -070082 path->path_cookie = rt6_get_cookie(rt);
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -080083 }
84
85 path->u.rt6.rt6i_nfheader_len = nfheader_len;
86
87 return 0;
88}
89
Herbert Xu87c1e122010-03-02 02:51:56 +000090static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -080091 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -080092{
Ian Morris67ba4152014-08-24 21:53:10 +010093 struct rt6_info *rt = (struct rt6_info *)xdst->route;
Herbert Xu25ee3282007-12-11 09:32:34 -080094
95 xdst->u.dst.dev = dev;
96 dev_hold(dev);
97
Nicolas Dichtelbc8e4b92010-04-21 16:25:30 -070098 xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
Cong Wang84c4a9d2013-05-09 22:40:00 +000099 if (!xdst->u.rt6.rt6i_idev) {
100 dev_put(dev);
Herbert Xu25ee3282007-12-11 09:32:34 -0800101 return -ENODEV;
Cong Wang84c4a9d2013-05-09 22:40:00 +0000102 }
Herbert Xu25ee3282007-12-11 09:32:34 -0800103
104 /* Sheit... I remember I did this right. Apparently,
105 * it was magically lost, so this code needs audit */
106 xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
107 RTF_LOCAL);
108 xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
109 xdst->u.rt6.rt6i_node = rt->rt6i_node;
Martin KaFai Laub197df42015-05-22 20:56:01 -0700110 xdst->route_cookie = rt6_get_cookie(rt);
Herbert Xu25ee3282007-12-11 09:32:34 -0800111 xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
112 xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
113 xdst->u.rt6.rt6i_src = rt->rt6i_src;
114
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static inline void
Herbert Xud5422ef2007-12-12 10:44:16 -0800119_decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500121 struct flowi6 *fl6 = &fl->u.ip6;
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700122 int onlyproto = 0;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000123 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Steffen Klassertde3b7a02014-12-04 09:46:20 +0100124 u16 offset = sizeof(*hdr);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700125 struct ipv6_opt_hdr *exthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700126 const unsigned char *nh = skb_network_header(skb);
Steffen Klassertf8556912014-12-08 07:56:18 +0100127 u16 nhoff = IP6CB(skb)->nhoff;
Steffen Klassert84502b52013-10-30 11:16:28 +0100128 int oif = 0;
Steffen Klassertf8556912014-12-08 07:56:18 +0100129 u8 nexthdr;
130
131 if (!nhoff)
132 nhoff = offsetof(struct ipv6hdr, nexthdr);
133
134 nexthdr = nh[nhoff];
Steffen Klassert84502b52013-10-30 11:16:28 +0100135
David Ahern385add92015-09-29 20:07:13 -0700136 if (skb_dst(skb))
David Aherne0d56fd2016-09-10 12:09:57 -0700137 oif = skb_dst(skb)->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500139 memset(fl6, 0, sizeof(struct flowi6));
140 fl6->flowi6_mark = skb->mark;
Steffen Klassert84502b52013-10-30 11:16:28 +0100141 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
Peter Kosyh44b451f2010-07-02 07:47:55 +0000142
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000143 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
144 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Wei Yongjun59cae002009-07-02 16:59:49 +0000146 while (nh + offset + 1 < skb->data ||
147 pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700148 nh = skb_network_header(skb);
149 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 switch (nexthdr) {
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700152 case NEXTHDR_FRAGMENT:
153 onlyproto = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 case NEXTHDR_ROUTING:
155 case NEXTHDR_HOP:
156 case NEXTHDR_DEST:
157 offset += ipv6_optlen(exthdr);
158 nexthdr = exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700159 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161
162 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800163 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 case IPPROTO_TCP:
165 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800166 case IPPROTO_DCCP:
Wei Yongjun59cae002009-07-02 16:59:49 +0000167 if (!onlyproto && (nh + offset + 4 < skb->data ||
168 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
Li RongQing789f2022014-10-22 17:09:53 +0800169 __be16 *ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Li RongQing789f2022014-10-22 17:09:53 +0800171 nh = skb_network_header(skb);
172 ports = (__be16 *)(nh + offset);
David S. Miller1958b852011-03-12 16:36:19 -0500173 fl6->fl6_sport = ports[!!reverse];
174 fl6->fl6_dport = ports[!reverse];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500176 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178
179 case IPPROTO_ICMPV6:
Mathias Krause04a6b8b2015-09-11 09:57:20 +0200180 if (!onlyproto && (nh + offset + 2 < skb->data ||
181 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
Li RongQing789f2022014-10-22 17:09:53 +0800182 u8 *icmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Li RongQing789f2022014-10-22 17:09:53 +0800184 nh = skb_network_header(skb);
185 icmp = (u8 *)(nh + offset);
David S. Miller1958b852011-03-12 16:36:19 -0500186 fl6->fl6_icmp_type = icmp[0];
187 fl6->fl6_icmp_code = icmp[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500189 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return;
191
Amerigo Wang07a93622012-10-29 16:23:10 +0000192#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700193 case IPPROTO_MH:
Hajime Tazakicd3bafc2015-02-04 23:31:10 +0900194 offset += ipv6_optlen(exthdr);
Mathias Krause04a6b8b2015-09-11 09:57:20 +0200195 if (!onlyproto && (nh + offset + 3 < skb->data ||
196 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700197 struct ip6_mh *mh;
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700198
Li RongQing789f2022014-10-22 17:09:53 +0800199 nh = skb_network_header(skb);
200 mh = (struct ip6_mh *)(nh + offset);
David S. Miller1958b852011-03-12 16:36:19 -0500201 fl6->fl6_mh_type = mh->ip6mh_type;
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700202 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500203 fl6->flowi6_proto = nexthdr;
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700204 return;
205#endif
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* XXX Why are there these headers? */
208 case IPPROTO_AH:
209 case IPPROTO_ESP:
210 case IPPROTO_COMP:
211 default:
David S. Miller1958b852011-03-12 16:36:19 -0500212 fl6->fl6_ipsec_spi = 0;
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500213 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217}
218
Daniel Lezcano569d3642008-01-18 03:56:57 -0800219static inline int xfrm6_garbage_collect(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800221 struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
222
223 xfrm6_policy_afinfo.garbage_collect(net);
Eric Dumazetfc66f952010-10-08 06:37:34 +0000224 return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
David S. Miller6700c272012-07-17 03:29:28 -0700227static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
228 struct sk_buff *skb, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
231 struct dst_entry *path = xdst->route;
232
David S. Miller6700c272012-07-17 03:29:28 -0700233 path->ops->update_pmtu(path, sk, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
David S. Miller6700c272012-07-17 03:29:28 -0700236static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
237 struct sk_buff *skb)
David S. Millerec18d9a2012-07-12 00:25:15 -0700238{
239 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
240 struct dst_entry *path = xdst->route;
241
David S. Miller6700c272012-07-17 03:29:28 -0700242 path->ops->redirect(path, sk, skb);
David S. Millerec18d9a2012-07-12 00:25:15 -0700243}
244
Herbert Xuaabc9762005-05-03 16:27:10 -0700245static void xfrm6_dst_destroy(struct dst_entry *dst)
246{
247 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
248
249 if (likely(xdst->u.rt6.rt6i_idev))
250 in6_dev_put(xdst->u.rt6.rt6i_idev);
David S. Miller62fa8a82011-01-26 20:51:05 -0800251 dst_destroy_metrics_generic(dst);
Herbert Xuaabc9762005-05-03 16:27:10 -0700252 xfrm_dst_destroy(xdst);
253}
254
255static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
256 int unregister)
257{
258 struct xfrm_dst *xdst;
259
260 if (!unregister)
261 return;
262
263 xdst = (struct xfrm_dst *)dst;
264 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800265 struct inet6_dev *loopback_idev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900266 in6_dev_get(dev_net(dev)->loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700267 BUG_ON(!loopback_idev);
268
269 do {
270 in6_dev_put(xdst->u.rt6.rt6i_idev);
271 xdst->u.rt6.rt6i_idev = loopback_idev;
272 in6_dev_hold(loopback_idev);
273 xdst = (struct xfrm_dst *)xdst->u.dst.child;
274 } while (xdst->u.dst.xfrm);
275
276 __in6_dev_put(loopback_idev);
277 }
278
279 xfrm_dst_ifdown(dst, dev);
280}
281
Dan Streetmana8a572a2015-10-29 09:51:16 -0400282static struct dst_ops xfrm6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .gc = xfrm6_garbage_collect,
285 .update_pmtu = xfrm6_update_pmtu,
David S. Millerec18d9a2012-07-12 00:25:15 -0700286 .redirect = xfrm6_redirect,
David S. Miller62fa8a82011-01-26 20:51:05 -0800287 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700288 .destroy = xfrm6_dst_destroy,
289 .ifdown = xfrm6_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800290 .local_out = __ip6_local_out,
Steffen Klassertc3865782015-09-29 11:40:49 +0200291 .gc_thresh = INT_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
294static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
295 .family = AF_INET6,
Dan Streetmana8a572a2015-10-29 09:51:16 -0400296 .dst_ops = &xfrm6_dst_ops_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .dst_lookup = xfrm6_dst_lookup,
Ian Morris67ba4152014-08-24 21:53:10 +0100298 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .decode_session = _decode_session6,
Herbert Xu25ee3282007-12-11 09:32:34 -0800300 .get_tos = xfrm6_get_tos,
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -0800301 .init_path = xfrm6_init_path,
Herbert Xu25ee3282007-12-11 09:32:34 -0800302 .fill_dst = xfrm6_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800303 .blackhole_route = ip6_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800306static int __init xfrm6_policy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800308 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311static void xfrm6_policy_fini(void)
312{
313 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
314}
315
David S. Millerdb717892009-08-04 20:32:16 -0700316#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000317static struct ctl_table xfrm6_policy_table[] = {
318 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000319 .procname = "xfrm6_gc_thresh",
Ian Morris67ba4152014-08-24 21:53:10 +0100320 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
321 .maxlen = sizeof(int),
322 .mode = 0644,
Neil Hormana44a4a02009-07-27 08:22:46 +0000323 .proc_handler = proc_dointvec,
324 },
325 { }
326};
327
Dan Streetmana8a572a2015-10-29 09:51:16 -0400328static int __net_init xfrm6_net_sysctl_init(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100329{
330 struct ctl_table *table;
331 struct ctl_table_header *hdr;
332
333 table = xfrm6_policy_table;
334 if (!net_eq(net, &init_net)) {
335 table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
336 if (!table)
337 goto err_alloc;
338
339 table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
340 }
341
342 hdr = register_net_sysctl(net, "net/ipv6", table);
343 if (!hdr)
344 goto err_reg;
345
346 net->ipv6.sysctl.xfrm6_hdr = hdr;
347 return 0;
348
349err_reg:
350 if (!net_eq(net, &init_net))
351 kfree(table);
352err_alloc:
353 return -ENOMEM;
354}
355
Dan Streetmana8a572a2015-10-29 09:51:16 -0400356static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100357{
358 struct ctl_table *table;
359
Ian Morris63159f22015-03-29 14:00:04 +0100360 if (!net->ipv6.sysctl.xfrm6_hdr)
Michal Kubecek8d068872013-02-06 10:46:33 +0100361 return;
362
363 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
364 unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
365 if (!net_eq(net, &init_net))
366 kfree(table);
367}
Dan Streetmana8a572a2015-10-29 09:51:16 -0400368#else /* CONFIG_SYSCTL */
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200369static inline int xfrm6_net_sysctl_init(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400370{
371 return 0;
372}
373
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200374static inline void xfrm6_net_sysctl_exit(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400375{
376}
377#endif
378
379static int __net_init xfrm6_net_init(struct net *net)
380{
381 int ret;
382
383 memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
384 sizeof(xfrm6_dst_ops_template));
385 ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
386 if (ret)
387 return ret;
388
389 ret = xfrm6_net_sysctl_init(net);
390 if (ret)
391 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
392
393 return ret;
394}
395
396static void __net_exit xfrm6_net_exit(struct net *net)
397{
398 xfrm6_net_sysctl_exit(net);
399 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
400}
Michal Kubecek8d068872013-02-06 10:46:33 +0100401
402static struct pernet_operations xfrm6_net_ops = {
403 .init = xfrm6_net_init,
404 .exit = xfrm6_net_exit,
405};
Neil Hormana44a4a02009-07-27 08:22:46 +0000406
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800407int __init xfrm6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800409 int ret;
410
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800411 ret = xfrm6_policy_init();
Dan Streetmana8a572a2015-10-29 09:51:16 -0400412 if (ret)
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800413 goto out;
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800414 ret = xfrm6_state_init();
415 if (ret)
416 goto out_policy;
417
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100418 ret = xfrm6_protocol_init();
419 if (ret)
420 goto out_state;
421
Michal Kubecek8d068872013-02-06 10:46:33 +0100422 register_pernet_subsys(&xfrm6_net_ops);
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800423out:
424 return ret;
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100425out_state:
426 xfrm6_state_fini();
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800427out_policy:
428 xfrm6_policy_fini();
429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432void xfrm6_fini(void)
433{
Michal Kubecek8d068872013-02-06 10:46:33 +0100434 unregister_pernet_subsys(&xfrm6_net_ops);
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100435 xfrm6_protocol_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 xfrm6_policy_fini();
437 xfrm6_state_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}