b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven.eckelmann@gmx.de>
To: b.a.t.m.a.n@open-mesh.net
Subject: [B.A.T.M.A.N.] [PATCH 2/2] [batman] Don't add size for netlink header twice in netlink request
Date: Wed, 20 May 2009 13:33:52 +0200	[thread overview]
Message-ID: <1242819232-614-3-git-send-email-sven.eckelmann@gmx.de> (raw)
In-Reply-To: <1242819232-614-1-git-send-email-sven.eckelmann@gmx.de>

The parameter len of NLMSG_LENGTH is only the size of the payload of
the request. If we use this function to calculate the size of the buffer
we send to the kernel we would add (sizeof(nlmsghdr)+padding) and the
size of our own structure which also has a nlmsghdr included. We must
split the req structure into a header part and the payload to calculate
the correct size of our buffer and having the payload always start at
the correct address.

Signed-off-by: Sven Eckelmann <sven.eckelmann@gmx.de>
---
 batman/linux/route.c |   59 ++++++++++++++++++++++++++-----------------------
 1 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/batman/linux/route.c b/batman/linux/route.c
index 3ddce2e..4be0179 100644
--- a/batman/linux/route.c
+++ b/batman/linux/route.c
@@ -182,8 +182,8 @@ void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src
 	struct iovec iov;
 	struct msghdr msg;
 	struct nlmsghdr *nh;
+	struct nlmsghdr *nlh;
 	struct req_s {
-		struct nlmsghdr nlh;
 		struct rtmsg rtm;
 		char buff[4 * (sizeof(struct rtattr) + 4)];
 	} *req;
@@ -228,9 +228,10 @@ void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src
 	}
 
 
-	req = (struct req_s*)req_buf;
+	nlh = (struct nlmsghdr *)req_buf;
+	req = (struct req_s*)NLMSG_DATA(req_buf);
 	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
-	memset(req, 0, NLMSG_LENGTH(sizeof(struct req_s)));
+	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
 	memset(&msg, 0, sizeof(struct msghdr));
 
 	nladdr.nl_family = AF_NETLINK;
@@ -244,22 +245,22 @@ void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src
 	if (src_ip != 0)
 		len += sizeof(struct rtattr) + 4;
 
-	req->nlh.nlmsg_len = NLMSG_LENGTH(len);
-	req->nlh.nlmsg_pid = getpid();
+	nlh->nlmsg_len = NLMSG_LENGTH(len);
+	nlh->nlmsg_pid = getpid();
 	req->rtm.rtm_family = AF_INET;
 	req->rtm.rtm_table = rt_table;
 	req->rtm.rtm_dst_len = netmask;
 
 	if (route_action == ROUTE_DEL) {
 
-		req->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
-		req->nlh.nlmsg_type = RTM_DELROUTE;
+		nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+		nlh->nlmsg_type = RTM_DELROUTE;
 		req->rtm.rtm_scope = RT_SCOPE_NOWHERE;
 
 	} else {
 
-		req->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_APPEND;
-		req->nlh.nlmsg_type = RTM_NEWROUTE;
+		nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_APPEND;
+		nlh->nlmsg_type = RTM_NEWROUTE;
 
 		if (route_type == ROUTE_TYPE_UNICAST && my_router == 0 && src_ip != 0)
 			req->rtm.rtm_scope = RT_SCOPE_LINK;
@@ -317,7 +318,7 @@ void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src
 
 	}
 
-	if (sendto(netlink_sock, req, req->nlh.nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {
+	if (sendto(netlink_sock, req_buf, nlh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {
 
 		debug_output(0, "Error - can't send message to kernel via netlink socket for routing table manipulation: %s\n", strerror(errno));
 		close(netlink_sock);
@@ -366,8 +367,8 @@ void add_del_rule(uint32_t network, uint8_t netmask, int8_t rt_table, uint32_t p
 	struct iovec iov;
 	struct msghdr msg;
 	struct nlmsghdr *nh;
+	struct nlmsghdr *nlh;
 	struct req_s {
-		struct nlmsghdr nlh;
 		struct rtmsg rtm;
 		char buff[2 * (sizeof(struct rtattr) + 4)];
 	} *req;
@@ -385,9 +386,10 @@ void add_del_rule(uint32_t network, uint8_t netmask, int8_t rt_table, uint32_t p
 	}
 
 
-	req = (struct req_s*)req_buf;
+	nlh = (struct nlmsghdr *)req_buf;
+	req = (struct req_s*)NLMSG_DATA(req_buf);
 	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
-	memset(req, 0, NLMSG_LENGTH(sizeof(struct req_s)));
+	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
 	memset(&msg, 0, sizeof(struct msghdr));
 
 	nladdr.nl_family = AF_NETLINK;
@@ -397,22 +399,22 @@ void add_del_rule(uint32_t network, uint8_t netmask, int8_t rt_table, uint32_t p
 	if (prio != 0)
 		len += sizeof(struct rtattr) + 4;
 
-	req->nlh.nlmsg_len = NLMSG_LENGTH(len);
-	req->nlh.nlmsg_pid = getpid();
+	nlh->nlmsg_len = NLMSG_LENGTH(len);
+	nlh->nlmsg_pid = getpid();
 	req->rtm.rtm_family = AF_INET;
 	req->rtm.rtm_table = rt_table;
 
 
 	if (rule_action == RULE_DEL) {
 
-		req->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
-		req->nlh.nlmsg_type = RTM_DELRULE;
+		nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+		nlh->nlmsg_type = RTM_DELRULE;
 		req->rtm.rtm_scope = RT_SCOPE_NOWHERE;
 
 	} else {
 
-		req->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
-		req->nlh.nlmsg_type = RTM_NEWRULE;
+		nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
+		nlh->nlmsg_type = RTM_NEWRULE;
 		req->rtm.rtm_scope = RT_SCOPE_UNIVERSE;
 		req->rtm.rtm_protocol = RTPROT_STATIC;
 		req->rtm.rtm_type = RTN_UNICAST;
@@ -467,7 +469,7 @@ void add_del_rule(uint32_t network, uint8_t netmask, int8_t rt_table, uint32_t p
 	}
 
 
-	if (sendto(netlink_sock, req, req->nlh.nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0)  {
+	if (sendto(netlink_sock, req_buf, nlh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0)  {
 
 		debug_output( 0, "Error - can't send message to kernel via netlink socket for routing rule manipulation: %s\n", strerror(errno));
 		close(netlink_sock);
@@ -632,8 +634,8 @@ int flush_routes_rules(int8_t is_rule)
 	struct msghdr msg;
 	struct nlmsghdr *nh;
 	struct rtmsg *rtm;
+	struct nlmsghdr *nlh;
 	struct req_s {
-		struct nlmsghdr nlh;
 		struct rtmsg rtm;
 	} *req;
 	char req_buf[NLMSG_LENGTH(sizeof(struct req_s))];
@@ -643,19 +645,20 @@ int flush_routes_rules(int8_t is_rule)
 	iov.iov_base = buf;
 	iov.iov_len  = sizeof(buf);
 
-	req = (struct req_s*)req_buf;
+	nlh = (struct nlmsghdr *)req_buf;
+	req = (struct req_s*)NLMSG_DATA(req_buf);
 	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
-	memset(req, 0, NLMSG_LENGTH(sizeof(struct req_s)));
+	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
 	memset(&msg, 0, sizeof(struct msghdr));
 
 	nladdr.nl_family = AF_NETLINK;
 
-	req->nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct req_s));
-	req->nlh.nlmsg_pid = getpid();
+	nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct req_s));
+	nlh->nlmsg_pid = getpid();
 	req->rtm.rtm_family = AF_INET;
 
-	req->nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
-	req->nlh.nlmsg_type = (is_rule ? RTM_GETRULE : RTM_GETROUTE);
+	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+	nlh->nlmsg_type = (is_rule ? RTM_GETRULE : RTM_GETROUTE);
 	req->rtm.rtm_scope = RTN_UNICAST;
 
 	if ((netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
@@ -666,7 +669,7 @@ int flush_routes_rules(int8_t is_rule)
 	}
 
 
-	if (sendto(netlink_sock, req, req->nlh.nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {
+	if (sendto(netlink_sock, req_buf, nlh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {
 
 		debug_output(0, "Error - can't send message to kernel via netlink socket for flushing the routing table: %s\n", strerror(errno));
 		close(netlink_sock);
-- 
1.6.3.1


  parent reply	other threads:[~2009-05-20 11:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-20 11:33 [B.A.T.M.A.N.] Possible page faults at route.c Sven Eckelmann
2009-05-20 11:33 ` [B.A.T.M.A.N.] [PATCH 1/2] [batman] Reserve enough space for aligned netlink requests Sven Eckelmann
2009-05-20 11:33 ` Sven Eckelmann [this message]
2009-05-20 16:25   ` [B.A.T.M.A.N.] [PATCH2/2v2] [batman] Don't add size for netlink header twice in netlink request Sven Eckelmann
2009-05-20 17:30     ` Marek Lindner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1242819232-614-3-git-send-email-sven.eckelmann@gmx.de \
    --to=sven.eckelmann@gmx.de \
    --cc=b.a.t.m.a.n@open-mesh.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).