Merge "Ensure netlink messages come from the kernel."
diff --git a/netlink_msg.c b/netlink_msg.c
index 7363028..2ba237d 100644
--- a/netlink_msg.c
+++ b/netlink_msg.c
@@ -103,6 +103,21 @@
return nlmsg_alloc_generic(type, flags, rt, sizeof(*rt));
}
+/* function: netlink_set_kernel_only
+ * sets a socket to receive messages only from the kernel
+ * sock - socket to connect
+ */
+int netlink_set_kernel_only(struct nl_sock *nl_sk) {
+ struct sockaddr_nl addr = { AF_NETLINK, 0, 0, 0 };
+
+ if (!nl_sk) {
+ return -EFAULT;
+ }
+
+ int sockfd = nl_socket_get_fd(nl_sk);
+ return connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
+}
+
/* function: send_netlink_msg
* sends a netlink message, reads a response, and hands the response(s) to the callbacks
* msg - netlink message to send
@@ -121,6 +136,9 @@
if(nl_send_auto_complete(nl_sk, msg) < 0)
goto cleanup;
+ if(netlink_set_kernel_only(nl_sk) < 0)
+ goto cleanup;
+
nl_recvmsgs(nl_sk, callbacks);
cleanup:
diff --git a/netlink_msg.h b/netlink_msg.h
index bc85b2d..13e1f28 100644
--- a/netlink_msg.h
+++ b/netlink_msg.h
@@ -25,5 +25,6 @@
void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks);
void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks);
int netlink_sendrecv(struct nl_msg *msg);
+int netlink_set_kernel_only(struct nl_sock *nl_sk);
#endif