1 // SPDX-License-Identifier: GPL-2.0
2 #include <net/genetlink.h>
3 #include <net/ila.h>
4 #include <net/netns/generic.h>
5 #include <uapi/linux/genetlink.h>
6 #include "ila.h"
7 
8 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
9 	[ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
10 	[ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
11 	[ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
12 	[ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
13 	[ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
14 };
15 
16 static const struct genl_ops ila_nl_ops[] = {
17 	{
18 		.cmd = ILA_CMD_ADD,
19 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
20 		.doit = ila_xlat_nl_cmd_add_mapping,
21 		.flags = GENL_ADMIN_PERM,
22 	},
23 	{
24 		.cmd = ILA_CMD_DEL,
25 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
26 		.doit = ila_xlat_nl_cmd_del_mapping,
27 		.flags = GENL_ADMIN_PERM,
28 	},
29 	{
30 		.cmd = ILA_CMD_FLUSH,
31 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
32 		.doit = ila_xlat_nl_cmd_flush,
33 		.flags = GENL_ADMIN_PERM,
34 	},
35 	{
36 		.cmd = ILA_CMD_GET,
37 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
38 		.doit = ila_xlat_nl_cmd_get_mapping,
39 		.start = ila_xlat_nl_dump_start,
40 		.dumpit = ila_xlat_nl_dump,
41 		.done = ila_xlat_nl_dump_done,
42 	},
43 };
44 
45 unsigned int ila_net_id;
46 
47 struct genl_family ila_nl_family __ro_after_init = {
48 	.hdrsize	= 0,
49 	.name		= ILA_GENL_NAME,
50 	.version	= ILA_GENL_VERSION,
51 	.maxattr	= ILA_ATTR_MAX,
52 	.policy = ila_nl_policy,
53 	.netnsok	= true,
54 	.parallel_ops	= true,
55 	.module		= THIS_MODULE,
56 	.ops		= ila_nl_ops,
57 	.n_ops		= ARRAY_SIZE(ila_nl_ops),
58 	.resv_start_op	= ILA_CMD_FLUSH + 1,
59 };
60 
ila_init_net(struct net * net)61 static __net_init int ila_init_net(struct net *net)
62 {
63 	int err;
64 
65 	err = ila_xlat_init_net(net);
66 	if (err)
67 		goto ila_xlat_init_fail;
68 
69 	return 0;
70 
71 ila_xlat_init_fail:
72 	return err;
73 }
74 
ila_exit_net(struct net * net)75 static __net_exit void ila_exit_net(struct net *net)
76 {
77 	ila_xlat_exit_net(net);
78 }
79 
80 static struct pernet_operations ila_net_ops = {
81 	.init = ila_init_net,
82 	.exit = ila_exit_net,
83 	.id   = &ila_net_id,
84 	.size = sizeof(struct ila_net),
85 };
86 
ila_init(void)87 static int __init ila_init(void)
88 {
89 	int ret;
90 
91 	ret = register_pernet_device(&ila_net_ops);
92 	if (ret)
93 		goto register_device_fail;
94 
95 	ret = genl_register_family(&ila_nl_family);
96 	if (ret)
97 		goto register_family_fail;
98 
99 	ret = ila_lwt_init();
100 	if (ret)
101 		goto fail_lwt;
102 
103 	return 0;
104 
105 fail_lwt:
106 	genl_unregister_family(&ila_nl_family);
107 register_family_fail:
108 	unregister_pernet_device(&ila_net_ops);
109 register_device_fail:
110 	return ret;
111 }
112 
ila_fini(void)113 static void __exit ila_fini(void)
114 {
115 	ila_lwt_fini();
116 	genl_unregister_family(&ila_nl_family);
117 	unregister_pernet_device(&ila_net_ops);
118 }
119 
120 module_init(ila_init);
121 module_exit(ila_fini);
122 MODULE_AUTHOR("Tom Herbert <tom@herbertland.com>");
123 MODULE_LICENSE("GPL");
124 MODULE_DESCRIPTION("IPv6: Identifier Locator Addressing (ILA)");
125