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
);
53 ret
= fblock_set_option(fb
, msg
->option
);
59 static int userctl_replace(struct lananlmsg
*lmsg
)
62 struct fblock
*fb1
, *fb2
;
63 struct lananlmsg_replace
*msg
= (struct lananlmsg_replace
*) lmsg
->buff
;
65 fb1
= search_fblock_n(msg
->name1
);
68 fb2
= search_fblock_n(msg
->name2
);
74 if (atomic_read(&fb2
->refcnt
) > 2) {
75 /* Still in use by others */
76 printk(KERN_ERR
"[lana] %s is still in use by others. "
77 "Drop refs first!\n", fb2
->name
);
83 unregister_fblock_namespace_no_rcu(fb2
);
85 if (!strncmp(fb1
->factory
->type
, fb2
->factory
->type
,
86 sizeof(fb1
->factory
->type
)) && !msg
->drop_priv
)
87 fblock_migrate_p(fb2
, fb1
);
88 fblock_migrate_r(fb2
, fb1
);
90 unregister_fblock(fb1
);
92 ret
= register_fblock(fb2
, fb2
->idp
);
99 static int userctl_subscribe(struct lananlmsg
*lmsg
)
102 struct fblock
*fb1
, *fb2
;
103 struct lananlmsg_subscribe
*msg
= (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
);
124 static int userctl_unsubscribe(struct lananlmsg
*lmsg
)
126 struct fblock
*fb1
, *fb2
;
127 struct lananlmsg_unsubscribe
*msg
=
128 (struct lananlmsg_unsubscribe
*) lmsg
->buff
;
130 fb1
= search_fblock_n(msg
->name1
);
133 fb2
= search_fblock_n(msg
->name2
);
139 unsubscribe_from_remote_fblock(fb2
, fb1
);
146 static int userctl_remove(struct lananlmsg
*lmsg
)
149 struct lananlmsg_rm
*msg
= (struct lananlmsg_rm
*) lmsg
->buff
;
151 fb
= search_fblock_n(msg
->name
);
155 if (atomic_read(&fb
->refcnt
) > 2) {
156 /* Still in use by others */
161 unregister_fblock_namespace(fb
);
167 static int userctl_bind(struct lananlmsg
*lmsg
)
170 struct fblock
*fb1
, *fb2
;
171 struct lananlmsg_bind
*msg
= (struct lananlmsg_bind
*) lmsg
->buff
;
173 fb1
= search_fblock_n(msg
->name1
);
177 fb2
= search_fblock_n(msg
->name2
);
183 ret
= fblock_bind(fb1
, fb2
);
191 static int userctl_unbind(struct lananlmsg
*lmsg
)
194 struct fblock
*fb1
, *fb2
;
195 struct lananlmsg_unbind
*msg
= (struct lananlmsg_unbind
*) lmsg
->buff
;
197 fb1
= search_fblock_n(msg
->name1
);
201 fb2
= search_fblock_n(msg
->name2
);
207 ret
= fblock_unbind(fb1
, fb2
);
215 static int __userctl_rcv(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
218 struct lananlmsg
*lmsg
;
220 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
222 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct lananlmsg
)))
225 lmsg
= NLMSG_DATA(nlh
);
228 case NETLINK_USERCTL_CMD_ADD
:
229 ret
= userctl_add(lmsg
);
231 case NETLINK_USERCTL_CMD_SET
:
232 ret
= userctl_set(lmsg
);
234 case NETLINK_USERCTL_CMD_REPLACE
:
235 ret
= userctl_replace(lmsg
);
237 case NETLINK_USERCTL_CMD_SUBSCRIBE
:
238 ret
= userctl_subscribe(lmsg
);
240 case NETLINK_USERCTL_CMD_UNSUBSCRIBE
:
241 ret
= userctl_unsubscribe(lmsg
);
243 case NETLINK_USERCTL_CMD_RM
:
244 ret
= userctl_remove(lmsg
);
246 case NETLINK_USERCTL_CMD_BIND
:
247 ret
= userctl_bind(lmsg
);
249 case NETLINK_USERCTL_CMD_UNBIND
:
250 ret
= userctl_unbind(lmsg
);
253 printk(KERN_INFO
"[lana] Unknown command!\n");
261 static void userctl_rcv(struct sk_buff
*skb
)
263 netlink_rcv_skb(skb
, &__userctl_rcv
);
266 int init_userctl_system(void)
268 userctl_sock
= netlink_kernel_create(&init_net
, NETLINK_USERCTL
,
269 USERCTLGRP_MAX
, userctl_rcv
,
271 return !userctl_sock
? -ENOMEM
: 0;
273 EXPORT_SYMBOL_GPL(init_userctl_system
);
275 void cleanup_userctl_system(void)
277 netlink_kernel_release(userctl_sock
);
279 EXPORT_SYMBOL_GPL(cleanup_userctl_system
);