2 * Lightweight Autonomic Network Architecture
4 * LANA NETLINK handler for Functional Block userspace control.
6 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
7 * Swiss federal institute of technology (ETH Zurich)
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/socket.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/rcupdate.h>
18 #include <net/netlink.h>
23 #include "xt_fblock.h"
24 #include "xt_builder.h"
26 static struct sock
*userctl_sock
= NULL
;
28 static int userctl_add(struct lananlmsg
*lmsg
)
31 struct lananlmsg_add
*msg
= (struct lananlmsg_add
*) lmsg
->buff
;
33 fb
= search_fblock_n(msg
->name
);
39 fb
= build_fblock_object(msg
->type
, msg
->name
);
41 return !fb
? -ENOMEM
: 0;
44 static int userctl_set(struct lananlmsg
*lmsg
)
48 struct lananlmsg_set
*msg
= (struct lananlmsg_set
*) lmsg
->buff
;
50 fb
= search_fblock_n(msg
->name
);
54 ret
= fblock_set_option(fb
, msg
->option
);
61 static int userctl_replace(struct lananlmsg
*lmsg
)
64 struct fblock
*fb1
, *fb2
;
65 struct lananlmsg_replace
*msg
= (struct lananlmsg_replace
*) lmsg
->buff
;
67 fb1
= search_fblock_n(msg
->name1
);
71 fb2
= search_fblock_n(msg
->name2
);
77 if (atomic_read(&fb2
->refcnt
) > 2) {
78 /* Still in use by others */
79 printk(KERN_ERR
"[lana] %s is still in use by others. "
80 "Drop refs first!\n", fb2
->name
);
86 unregister_fblock_namespace_no_rcu(fb2
);
88 if (!strncmp(fb1
->factory
->type
, fb2
->factory
->type
,
89 sizeof(fb1
->factory
->type
)) && !msg
->drop_priv
)
90 fblock_migrate_p(fb2
, fb1
);
91 fblock_migrate_r(fb2
, fb1
);
93 unregister_fblock(fb1
);
95 ret
= register_fblock(fb2
, fb2
->idp
);
103 static int userctl_subscribe(struct lananlmsg
*lmsg
)
106 struct fblock
*fb1
, *fb2
;
107 struct lananlmsg_tuple
*msg
= (struct lananlmsg_tuple
*) lmsg
->buff
;
109 fb1
= search_fblock_n(msg
->name1
);
113 fb2
= search_fblock_n(msg
->name2
);
119 * fb1 is remote block, fb2 is the one that
120 * wishes to be notified.
122 ret
= subscribe_to_remote_fblock(fb2
, fb1
);
130 static int userctl_unsubscribe(struct lananlmsg
*lmsg
)
132 struct fblock
*fb1
, *fb2
;
133 struct lananlmsg_tuple
*msg
= (struct lananlmsg_tuple
*) lmsg
->buff
;
135 fb1
= search_fblock_n(msg
->name1
);
139 fb2
= search_fblock_n(msg
->name2
);
145 unsubscribe_from_remote_fblock(fb2
, fb1
);
153 static int userctl_remove(struct lananlmsg
*lmsg
)
156 struct lananlmsg_rm
*msg
= (struct lananlmsg_rm
*) lmsg
->buff
;
158 fb
= search_fblock_n(msg
->name
);
162 if (atomic_read(&fb
->refcnt
) > 2) {
163 /* Still in use by others */
168 unregister_fblock_namespace(fb
);
174 static int userctl_bind(struct lananlmsg
*lmsg
)
177 struct fblock
*fb1
, *fb2
;
178 struct lananlmsg_tuple
*msg
= (struct lananlmsg_tuple
*) lmsg
->buff
;
180 fb1
= search_fblock_n(msg
->name1
);
184 fb2
= search_fblock_n(msg
->name2
);
190 ret
= fblock_bind(fb1
, fb2
);
198 static int userctl_unbind(struct lananlmsg
*lmsg
)
201 struct fblock
*fb1
, *fb2
;
202 struct lananlmsg_tuple
*msg
= (struct lananlmsg_tuple
*) lmsg
->buff
;
204 fb1
= search_fblock_n(msg
->name1
);
208 fb2
= search_fblock_n(msg
->name2
);
214 ret
= fblock_unbind(fb1
, fb2
);
222 static int __userctl_rcv(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
225 struct lananlmsg
*lmsg
;
227 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
229 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct lananlmsg
)))
232 lmsg
= NLMSG_DATA(nlh
);
235 case NETLINK_USERCTL_CMD_ADD
:
236 ret
= userctl_add(lmsg
);
238 case NETLINK_USERCTL_CMD_SET
:
239 ret
= userctl_set(lmsg
);
241 case NETLINK_USERCTL_CMD_REPLACE
:
242 ret
= userctl_replace(lmsg
);
244 case NETLINK_USERCTL_CMD_SUBSCRIBE
:
245 ret
= userctl_subscribe(lmsg
);
247 case NETLINK_USERCTL_CMD_UNSUBSCRIBE
:
248 ret
= userctl_unsubscribe(lmsg
);
250 case NETLINK_USERCTL_CMD_RM
:
251 ret
= userctl_remove(lmsg
);
253 case NETLINK_USERCTL_CMD_BIND
:
254 ret
= userctl_bind(lmsg
);
256 case NETLINK_USERCTL_CMD_UNBIND
:
257 ret
= userctl_unbind(lmsg
);
260 printk(KERN_INFO
"[lana] Unknown command!\n");
268 static void userctl_rcv(struct sk_buff
*skb
)
270 netlink_rcv_skb(skb
, &__userctl_rcv
);
273 int init_userctl_system(void)
275 userctl_sock
= netlink_kernel_create(&init_net
, NETLINK_USERCTL
,
276 USERCTLGRP_MAX
, userctl_rcv
,
278 return !userctl_sock
? -ENOMEM
: 0;
280 EXPORT_SYMBOL_GPL(init_userctl_system
);
282 void cleanup_userctl_system(void)
284 netlink_kernel_release(userctl_sock
);
286 EXPORT_SYMBOL_GPL(cleanup_userctl_system
);