34 #include <netlink/netlink.h>
35 #include <netlink/cache.h>
36 #include <netlink/attr.h>
37 #include <netlink/addr.h>
38 #include <netlink/msg.h>
39 #include <netlink/errno.h>
41 #include <netlink/genl/ctrl.h>
42 #include <netlink/genl/family.h>
43 #include <netlink/genl/genl.h>
44 #include <netlink/genl/mngt.h>
50 #define GE_FAMILY_NAME "CE_GW"
51 #define GE_FAMILY_VERSION 1
52 #define USER_HDR_SIZE 0
54 #define IFACE_VERSION 0
73 #define CE_GW_A_MAX (__CE_GW_A_MAX - 1)
78 static struct nla_policy ce_gw_genl_policy[CE_GW_A_MAX + 1] = {
119 char *str = malloc(size);
124 for (
int i = 0; i <
sizeof(uint32_t) * 8 && flags[i].name != 0; ++i) {
126 int b = ((bits >> i) & 1);
130 strcat(str, flags[i].name);
134 int len = strlen(str);
175 char *str = malloc(size+1);
179 for (
int i = 0; i <= max && enums[i].
name != 0; ++i) {
181 strcat(str, enums[i].name);
204 struct nlmsgerr *nlerr,
void *arg)
206 int err = nlerr->error;
207 fprintf(stderr,
"NETLINK returned Error: %s\n", strerror(err));
220 fprintf(stderr,
"add: Message allocation failed.\n");
225 user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ,
228 if (user_hdr == NULL)
229 fprintf(stderr,
"add: Message Haeder creation failed\n");
231 if (src_name != NULL) {
241 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
245 fprintf(stderr,
"add: Validation of Message Failed: %i\n", err);
250 nl_send_auto(
nl_sk, msg);
252 err = nl_wait_for_ack(
nl_sk);
255 "add: ACK is missing or Error returned. "
256 "Operation might fail: %i\n", err);
264 fprintf(stderr,
"Attribute Modification failed: %d\n",-EMSGSIZE);
273 if (
id != 0 && dev_name != NULL) {
281 fprintf(stderr,
"del: Message allocation failed.\n");
286 user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ,
289 if (user_hdr == NULL)
290 fprintf(stderr,
"del: Message Haeder creation failed\n");
293 if (dev_name != NULL) {
298 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
302 fprintf(stderr,
"del: Validation of Message Failed: %i\n", err);
307 nl_send_auto(
nl_sk, msg);
309 err = nl_wait_for_ack(
nl_sk);
312 "add: ACK is missing or Error returned. "
313 "Operation might fail: %i\n", err);
320 fprintf(stderr,
"Attribute Modification failed: %d\n",-EMSGSIZE);
339 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
345 fprintf(stderr,
"ERROR Kernel Message Parsing Failed\n");
347 char *a_msg_src_data = (
char *)nla_data(attrs[
CE_GW_A_SRC]);
348 char *a_msg_dst_data = (
char *)nla_data(attrs[
CE_GW_A_DST]);
349 uint32_t *a_msg_id_data = (uint32_t *)nla_data(attrs[
CE_GW_A_ID]);
350 uint32_t *a_msg_flags_data = (uint32_t *)nla_data(attrs[
CE_GW_A_FLAGS]);
351 uint8_t *a_msg_type_data = (uint8_t *)nla_data(attrs[
CE_GW_A_TYPE]);
352 uint32_t *a_msg_hndl_data = (uint32_t *)nla_data(attrs[
CE_GW_A_HNDL]);
353 uint32_t *a_msg_drop_data = (uint32_t *)nla_data(attrs[
CE_GW_A_DROP]);
361 printf(
" %-8d %-6s %-6s %-6s %-8d %-8d %s\n", *a_msg_id_data,
362 a_msg_src_data, a_msg_dst_data, type_str, *a_msg_hndl_data,
363 *a_msg_drop_data, flags_str);
392 fprintf(stderr,
"echo: Message allocation failed.\n");
397 user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ,
400 if (user_hdr == NULL)
401 fprintf(stderr,
"echo: Message Haeder creation failed.\n");
406 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
409 fprintf(stderr,
"echo: Validation of Message Failed: %i\n",
415 nl_socket_disable_auto_ack(
nl_sk);
416 nl_send_auto(
nl_sk, msg);
419 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
424 printf(
" ID SRC DST TYPE HANDLED DROPPED FLAGS\n");
426 nl_recvmsgs(
nl_sk, cb);
434 fprintf(stderr,
"Attribute Modification failed: %d\n",-EMSGSIZE);
450 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
451 struct genlmsghdr *gemsghdr = nlmsg_data(msghdr);
453 struct nlattr *a_msg = genlmsg_attrdata(gemsghdr, 0);
454 char * a_msg_data = (
char *) nla_data(a_msg);
455 printf(
"kernel says: %s\n", a_msg_data);
468 fprintf(stderr,
"echo: Message allocation failed.\n");
473 user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ,
476 if (user_hdr == NULL)
477 fprintf(stderr,
"echo: Message Haeder creation failed.\n");
482 struct nlmsghdr *msghdr = nlmsg_hdr(msg);
484 fprintf(stderr,
"echo: Validation of Message Failed: %i\n", rc);
489 nl_socket_disable_auto_ack(
nl_sk);
490 nl_send_auto(
nl_sk, msg);
493 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEBUG);
495 nl_recvmsgs(
nl_sk, cb);
504 fprintf(stderr,
"Attribute Modification failed: %d\n",-EMSGSIZE);
515 nl_sk = nl_socket_alloc();
516 if (
nl_sk == NULL ) {
517 fprintf (stderr,
"Socket allocation failed.\n");
521 err = genl_connect(
nl_sk);
523 fprintf (stderr,
"Connetion to socket failed: %d\n", err);
527 err = nl_socket_modify_err_cb(
nl_sk, NL_CB_CUSTOM,
530 fprintf (stderr,
"Error Callback modification failed: %d\n",
537 fprintf (stderr,
"Family allocation failed.\n");
550 "Could not resolve Netlink Family ID from kernel. "
551 "Is the module loaded?: %d\n", err);
563 nl_socket_free(
nl_sk);