kernel-netlink: Use larger buffer for event messages
authorTobias Brunner <tobias@strongswan.org>
Fri, 10 Aug 2018 12:41:16 +0000 (14:41 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 31 Aug 2018 10:15:12 +0000 (12:15 +0200)
src/libcharon/plugins/kernel_netlink/kernel_netlink_ipsec.c
src/libcharon/plugins/kernel_netlink/kernel_netlink_net.c
src/libcharon/plugins/kernel_netlink/kernel_netlink_shared.c
src/libcharon/plugins/kernel_netlink/kernel_netlink_shared.h

index 70edef6..33e982d 100644 (file)
@@ -1131,7 +1131,7 @@ static void process_mapping(private_kernel_netlink_ipsec_t *this,
 static bool receive_events(private_kernel_netlink_ipsec_t *this, int fd,
                                                   watcher_event_t event)
 {
-       char response[1024];
+       char response[netlink_get_buflen()];
        struct nlmsghdr *hdr = (struct nlmsghdr*)response;
        struct sockaddr_nl addr;
        socklen_t addr_len = sizeof(addr);
index b6eb543..3ef3dc7 100644 (file)
@@ -1504,7 +1504,7 @@ static void process_rule(private_kernel_netlink_net_t *this, struct nlmsghdr *hd
 static bool receive_events(private_kernel_netlink_net_t *this, int fd,
                                                   watcher_event_t event)
 {
-       char response[1536];
+       char response[netlink_get_buflen()];
        struct nlmsghdr *hdr = (struct nlmsghdr*)response;
        struct sockaddr_nl addr;
        socklen_t addr_len = sizeof(addr);
index 2327d75..84d78ec 100644 (file)
@@ -587,8 +587,31 @@ METHOD(netlink_socket_t, destroy, void,
        free(this);
 }
 
-/**
- * Described in header.
+/*
+ * Described in header
+ */
+u_int netlink_get_buflen()
+{
+       u_int buflen;
+
+       buflen = lib->settings->get_int(lib->settings,
+                                                               "%s.plugins.kernel-netlink.buflen", 0, lib->ns);
+       if (!buflen)
+       {
+               long pagesize = sysconf(_SC_PAGESIZE);
+
+               if (pagesize == -1)
+               {
+                       pagesize = 4096;
+               }
+               /* base this on NLMSG_GOODSIZE */
+               buflen = min(pagesize, 8192);
+       }
+       return buflen;
+}
+
+/*
+ * Described in header
  */
 netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names,
                                                                                bool parallel)
@@ -612,8 +635,7 @@ netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names,
                .entries = hashtable_create(hashtable_hash_ptr, hashtable_equals_ptr, 4),
                .protocol = protocol,
                .names = names,
-               .buflen = lib->settings->get_int(lib->settings,
-                                                       "%s.plugins.kernel-netlink.buflen", 0, lib->ns),
+               .buflen = netlink_get_buflen(),
                .timeout = lib->settings->get_int(lib->settings,
                                                        "%s.plugins.kernel-netlink.timeout", 0, lib->ns),
                .retries = lib->settings->get_int(lib->settings,
@@ -624,16 +646,6 @@ netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names,
                .parallel = parallel,
        );
 
-       if (!this->buflen)
-       {
-               long pagesize = sysconf(_SC_PAGESIZE);
-               if (pagesize == -1)
-               {
-                       pagesize = 4096;
-               }
-               /* base this on NLMSG_GOODSIZE */
-               this->buflen = min(pagesize, 8192);
-       }
        if (this->socket == -1)
        {
                DBG1(DBG_KNL, "unable to create netlink socket: %s (%d)",
index 7056e6c..82dce4c 100644 (file)
@@ -101,4 +101,11 @@ void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
  */
 void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len);
 
+/**
+ * Determine buffer size for received messages (e.g. events).
+ *
+ * @return                             buffer size
+ */
+u_int netlink_get_buflen();
+
 #endif /* KERNEL_NETLINK_SHARED_H_ */