30 #include <linux/version.h>
31 #include <net/genetlink.h>
32 #include <net/netlink.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/netlink.h>
38 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
39 #include <uapi/linux/netlink.h>
41 #include <asm-generic/errno-base.h>
42 #include <asm-generic/errno.h>
47 #define CE_GW_GE_FAMILY_NAME "CE_GW"
48 #define CE_GW_GE_FAMILY_VERSION 2
49 #define CE_GW_USER_HDR_SIZE 0
50 #define CE_GW_NO_FLAG 0
70 #define CE_GW_A_MAX (__CE_GW_A_MAX - 1)
76 static struct nla_policy ce_gw_genl_policy[CE_GW_A_MAX + 1] = {
93 .id = GENL_ID_GENERATE,
117 #define CE_GW_C_MAX (__CE_GW_C_MAX - 1)
127 int ce_gw_netlink_echo(struct sk_buff *skb_info, struct genl_info *info)
134 printk(KERN_ERR
"ce_gw: info attribute is missing."\
135 " No Massage received.\n");
140 char *nla_a_msg_pay = (
char *) nla_data(nla_a_msg);
142 if (nla_a_msg_pay == NULL) {
143 pr_warning(
"ce_gw: String Message is missing.\n");
145 pr_info(
"ce_gw: Messege received: %s\n", nla_a_msg_pay);
149 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
152 pr_err(
"ce_gw: Socket allocation failed.\n");
153 goto ce_gw_add_error;
156 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
157 user_hdr = genlmsg_put(skb, info->snd_pid, info->snd_seq,
160 user_hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
164 if (user_hdr == NULL) {
166 pr_err(
"ce_gw: Error during putting haeder\n");
167 goto ce_gw_add_error;
171 "hello world from kernel space \n");
173 pr_err(
"ce_gw: Putting Netlink Attribute Failed.\n");
174 goto ce_gw_add_error;
177 genlmsg_end(skb, user_hdr);
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
180 err = genlmsg_unicast(genl_info_net(info),skb,info->snd_pid);
182 err = genlmsg_unicast(genl_info_net(info),skb,info->snd_portid);
185 pr_err(
"ce_gw: Message sending failed.\n");
186 goto ce_gw_add_error;
221 pr_err(
"ce_gw: info attribute is missing."\
222 " No Massage received.\n");
227 char *nla_src_data = (
char *) nla_data(nla_src);
230 char *nla_dst_data = (
char *) nla_data(nla_dst);
233 u32 *nla_flags_data = (u32 *) nla_data(nla_flags);
236 u8 *nla_type_data = (u8 *) nla_data(nla_type);
238 if (nla_src == NULL) {
239 pr_info(
"ce_gw: add dev is called\n");
240 struct net_device *dev;
242 if (nla_dst == NULL ||
243 nla_type == NULL || nla_flags == NULL) {
244 pr_err(
"ce_gw: DST or TYPE is missing.\n");
246 goto ce_gw_add_error;
252 pr_err(
"ce_gw_netlink: Device allocation failed.");
254 goto ce_gw_add_error;
259 pr_err(
"ce_gw_netlink: Device registration failed.");
261 goto ce_gw_add_error;
265 if (nla_src == NULL || nla_dst == NULL ||
266 nla_type == NULL || nla_flags == NULL) {
267 pr_err(
"ce_gw: SRC or DST or TYPE is missing.\n");
269 goto ce_gw_add_error;
272 pr_info(
"ce_gw: Add : from %s to %s"
273 "(Type %d; Flags %d)\n", nla_src_data,
274 nla_dst_data, *nla_type_data, *nla_flags_data);
277 struct net_device *src_dev;
278 src_dev = dev_get_by_name(&init_net, nla_src_data);
279 if (src_dev == NULL) {
281 pr_err(
"ce_gw_netlink: src dev not found: %d\n", err);
282 goto ce_gw_add_error;
284 int src_dev_ifindex = src_dev->ifindex;
287 struct net_device *dst_dev;
288 dst_dev = dev_get_by_name(&init_net, nla_dst_data);
289 if (dst_dev == NULL) {
291 pr_err(
"ce_gw_netlink: dst dev not found: %d\n", err);
292 goto ce_gw_add_error;
294 int dst_dev_ifindex = dst_dev->ifindex;
298 *nla_type_data, *nla_flags_data);
300 goto ce_gw_add_error;
312 netlink_ack(skb_info, info->nlhdr, -err);
338 pr_err(
"ce_gw: info attribute is missing."\
339 " No Massage received: %d\n", err);
343 struct nlattr *nla_id = info->attrs[
CE_GW_A_ID];
344 __u32 *nla_id_data = (__u32 *) nla_data(nla_id);
348 char *nla_dst_data = (
char *) nla_data(nla_dst);
350 if (nla_dst == NULL) {
351 if (*nla_id_data == 0) {
353 pr_warning(
"ce_gw: ID is missing: %d\n", err);
354 goto ce_gw_del_error;
356 pr_debug(
"ce_gw: del device: %d\n", *nla_id_data);
362 struct net_device *dev;
363 dev = dev_get_by_name(&init_net, nla_dst_data);
366 pr_err(
"ce_gw_netlink: No such Device: %d\n", err);
367 goto ce_gw_del_error;
376 netlink_ack(skb_info, info->nlhdr, -err);
407 pr_debug(
"ce_gw_netlink: ce_gw_netlink_list is called.\n");
409 struct nlattr *nla_id = info->attrs[
CE_GW_A_ID];
410 __u32 *nla_id_data = (__u32 *) nla_data(nla_id);
414 struct hlist_node *node;
416 # if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
420 struct hlist_node *pos;
423 if (*nla_id_data != 0 && cgj->
id != *nla_id_data) {
427 pr_debug(
"ce_gw_netlink: Job List entry is send.\n");
429 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
431 pr_err(
"ce_gw: Socket allocation failed.\n");
432 goto ce_gw_list_error;
435 # if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
436 user_hdr = genlmsg_put(skb, info->snd_pid,
441 user_hdr = genlmsg_put(skb, info->snd_portid,
445 if (user_hdr == NULL) {
447 pr_err(
"ce_gw: Error during putting haeder\n");
448 goto ce_gw_list_error;
459 pr_err(
"ce_gw: Putting Netlink Attribute Failed.\n");
460 goto ce_gw_list_error;
463 genlmsg_end(skb, user_hdr);
465 # if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
466 err = genlmsg_unicast(genl_info_net(info), skb,
470 err = genlmsg_unicast(genl_info_net(info), skb,
474 pr_err(
"ce_gw: Message sending failed.\n");
475 goto ce_gw_list_error;
480 struct nlmsghdr *nlhdr;
481 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
484 pr_err(
"ce_gw: Socket allocation failed.\n");
485 goto ce_gw_list_error;
488 # if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
489 nlhdr = nlmsg_put(skb, info->snd_pid, info->snd_seq,
493 nlhdr = nlmsg_put(skb, info->snd_portid, info->snd_seq,
498 pr_err(
"ce_gw: Error during putting haeder\n");
499 goto ce_gw_list_error;
502 nlmsg_end(skb, nlhdr);
504 # if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
505 err = nlmsg_unicast(genl_info_net(info)->genl_sock, skb,
509 err = nlmsg_unicast(genl_info_net(info)->genl_sock, skb,
513 pr_err(
"ce_gw: Message sending failed.\n");
514 goto ce_gw_list_error;
585 pr_err(
"ce_gw: Error during registering family ce_gw: %i\n",
587 goto ce_gw_init_family_err;
592 pr_err(
"ce_gw: Error during registering operation echo: %i\n",
594 goto ce_gw_init_echo_err;
599 pr_err(
"ce_gw: Error during registering operation del: %i\n",
601 goto ce_gw_init_del_err;
606 pr_err(
"ce_gw: Error during registering operation add: %i\n",
608 goto ce_gw_init_add_err;
613 pr_err(
"ce_gw: Error during registering operation list: %i\n",
615 goto ce_gw_init_list_err;
635 ce_gw_init_family_err:
644 pr_err(
"ce_gw: Error during unregistering operation echo: %i\n",
650 pr_err(
"ce_gw: Error during unregistering operation add: %i\n",
656 pr_err(
"ce_gw: Error during unregistering operation del: %i\n",
662 pr_err(
"ce_gw: Error during unregistering operation del: %i\n",
668 pr_err(
"ce_gw: Error during unregistering family ce_gw: %i\n",