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 <net/netlink.h>
22 #include "xt_fblock.h"
23 #include "xt_builder.h"
25 static struct sock
*userctl_sock
= NULL
;
27 static int __userctl_rcv(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
29 struct lananlmsg
*lmsg
;
31 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
33 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct lananlmsg
)))
36 lmsg
= NLMSG_DATA(nlh
);
39 case NETLINK_USERCTL_CMD_ADD
: {
41 struct lananlmsg_add
*msg
=
42 (struct lananlmsg_add
*) lmsg
->buff
;
43 fb
= build_fblock_object(msg
->type
, msg
->name
);
47 case NETLINK_USERCTL_CMD_SET
: {
50 struct lananlmsg_set
*msg
=
51 (struct lananlmsg_set
*) lmsg
->buff
;
52 fb
= search_fblock_n(msg
->name
);
55 ret
= fblock_set_option(fb
, msg
->option
);
59 case NETLINK_USERCTL_CMD_REPLACE
: {
61 struct fblock
*fb1
, *fb2
;
62 struct lananlmsg_replace
*msg
=
63 (struct lananlmsg_replace
*) lmsg
->buff
;
64 fb1
= search_fblock_n(msg
->name1
);
67 fb2
= search_fblock_n(msg
->name2
);
72 if (atomic_read(&fb2
->refcnt
) > 2) {
73 /* Still in use by others */
75 printk(KERN_ERR
"[lana] %s is still in use by others. "
76 "Drop refs first!\n", fb2
->name
);
80 unregister_fblock_namespace_no_rcu(fb2
);
81 if (!strncmp(fb1
->factory
->type
, fb2
->factory
->type
,
82 sizeof(fb1
->factory
->type
)) && !msg
->drop_priv
) {
83 ret
= fblock_migrate(fb2
, fb1
);
87 printk("[lana] fblock migration failed! "
88 "Destination fblock lost!\n");
94 unregister_fblock(fb1
);
96 ret
= register_fblock(fb2
, fb2
->idp
);
100 case NETLINK_USERCTL_CMD_SUBSCRIBE
: {
102 struct fblock
*fb1
, *fb2
;
103 struct lananlmsg_subscribe
*msg
=
104 (struct lananlmsg_subscribe
*) lmsg
->buff
;
105 fb1
= search_fblock_n(msg
->name1
);
108 fb2
= search_fblock_n(msg
->name2
);
114 * fb1 is remote block, fb2 is the one that
115 * wishes to be notified.
117 ret
= subscribe_to_remote_fblock(fb2
, fb1
);
122 case NETLINK_USERCTL_CMD_UNSUBSCRIBE
: {
123 struct fblock
*fb1
, *fb2
;
124 struct lananlmsg_unsubscribe
*msg
=
125 (struct lananlmsg_unsubscribe
*) lmsg
->buff
;
126 fb1
= search_fblock_n(msg
->name1
);
129 fb2
= search_fblock_n(msg
->name2
);
134 unsubscribe_from_remote_fblock(fb2
, fb1
);
138 case NETLINK_USERCTL_CMD_RM
: {
140 struct lananlmsg_rm
*msg
=
141 (struct lananlmsg_rm
*) lmsg
->buff
;
142 fb
= search_fblock_n(msg
->name
);
145 if (atomic_read(&fb
->refcnt
) > 2) {
146 /* Still in use by others */
150 unregister_fblock_namespace(fb
);
153 case NETLINK_USERCTL_CMD_BIND
: {
155 struct fblock
*fb1
, *fb2
;
156 struct lananlmsg_bind
*msg
=
157 (struct lananlmsg_bind
*) lmsg
->buff
;
158 fb1
= search_fblock_n(msg
->name1
);
161 fb2
= search_fblock_n(msg
->name2
);
166 ret
= fblock_bind(fb1
, fb2
);
175 case NETLINK_USERCTL_CMD_UNBIND
: {
177 struct fblock
*fb1
, *fb2
;
178 struct lananlmsg_unbind
*msg
=
179 (struct lananlmsg_unbind
*) lmsg
->buff
;
180 fb1
= search_fblock_n(msg
->name1
);
183 fb2
= search_fblock_n(msg
->name2
);
188 ret
= fblock_unbind(fb1
, fb2
);
198 printk("[lana] Unknown command!\n");
205 static void userctl_rcv(struct sk_buff
*skb
)
207 netlink_rcv_skb(skb
, &__userctl_rcv
);
210 int init_userctl_system(void)
212 userctl_sock
= netlink_kernel_create(&init_net
, NETLINK_USERCTL
,
213 USERCTLGRP_MAX
, userctl_rcv
,
219 EXPORT_SYMBOL_GPL(init_userctl_system
);
221 void cleanup_userctl_system(void)
223 netlink_kernel_release(userctl_sock
);
225 EXPORT_SYMBOL_GPL(cleanup_userctl_system
);