]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/bpf-filter.cc
rec: ensure correct service user on debian
[thirdparty/pdns.git] / pdns / bpf-filter.cc
index 020d97c3e2d7caa3264948c39140c9f7d6ce56eb..e797a04c9b6e6802da70bb5e593426208d48715e 100644 (file)
@@ -1,3 +1,24 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 #include "bpf-filter.hh"
 
 #ifdef HAVE_EBPF
@@ -15,7 +36,8 @@ static __u64 ptr_to_u64(void *ptr)
 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
                   int max_entries)
 {
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.map_type = map_type;
   attr.key_size = key_size;
   attr.value_size = value_size;
@@ -25,7 +47,8 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
 
 int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
 {
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.map_fd = fd;
   attr.key = ptr_to_u64(key);
   attr.value = ptr_to_u64(value);
@@ -35,7 +58,8 @@ int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
 
 int bpf_lookup_elem(int fd, void *key, void *value)
 {
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.map_fd = fd;
   attr.key = ptr_to_u64(key);
   attr.value = ptr_to_u64(value);
@@ -44,7 +68,8 @@ int bpf_lookup_elem(int fd, void *key, void *value)
 
 int bpf_delete_elem(int fd, void *key)
 {
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.map_fd = fd;
   attr.key = ptr_to_u64(key);
   return syscall(SYS_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
@@ -52,7 +77,8 @@ int bpf_delete_elem(int fd, void *key)
 
 int bpf_get_next_key(int fd, void *key, void *next_key)
 {
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.map_fd = fd;
   attr.key = ptr_to_u64(key);
   attr.next_key = ptr_to_u64(next_key);
@@ -64,7 +90,8 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
                  const char *license, int kern_version)
 {
   char log_buf[65535];
-  union bpf_attr attr = { 0 };
+  union bpf_attr attr;
+  memset(&attr, 0, sizeof(attr));
   attr.prog_type = prog_type;
   attr.insns = ptr_to_u64((void *) insns);
   attr.insn_cnt = prog_len / sizeof(struct bpf_insn);
@@ -273,19 +300,19 @@ void BPFFilter::block(const DNSName& qname, uint16_t qtype)
 
   std::string keyStr = qname.toDNSStringLC();
   if (keyStr.size() > sizeof(key.qname)) {
-    throw std::runtime_error("Invalid QName to block " + qname.toString());
+    throw std::runtime_error("Invalid QName to block " + qname.toLogString());
   }
   memcpy(key.qname, keyStr.c_str(), keyStr.size());
 
   {
     std::unique_lock<std::mutex> lock(d_mutex);
     if (d_qNamesCount >= d_maxQNames) {
-      throw std::runtime_error("Table full when trying to block " + qname.toString());
+      throw std::runtime_error("Table full when trying to block " + qname.toLogString());
     }
 
     int res = bpf_lookup_elem(d_qnamemap.fd, &key, &value);
     if (res != -1) {
-      throw std::runtime_error("Trying to block an already blocked qname: " + qname.toString());
+      throw std::runtime_error("Trying to block an already blocked qname: " + qname.toLogString());
     }
 
     res = bpf_update_elem(d_qnamemap.fd, &key, &value, BPF_NOEXIST);
@@ -294,19 +321,19 @@ void BPFFilter::block(const DNSName& qname, uint16_t qtype)
     }
 
     if (res != 0) {
-      throw std::runtime_error("Error adding blocked qname " + qname.toString() + ": " + std::string(strerror(errno)));
+      throw std::runtime_error("Error adding blocked qname " + qname.toLogString() + ": " + std::string(strerror(errno)));
     }
   }
 }
 
 void BPFFilter::unblock(const DNSName& qname, uint16_t qtype)
 {
-  struct QNameKey key = { 0 };
+  struct QNameKey key = { { 0 } };
   std::string keyStr = qname.toDNSStringLC();
   (void) qtype;
 
   if (keyStr.size() > sizeof(key.qname)) {
-    throw std::runtime_error("Invalid QName to block " + qname.toString());
+    throw std::runtime_error("Invalid QName to block " + qname.toLogString());
   }
   memcpy(key.qname, keyStr.c_str(), keyStr.size());
 
@@ -318,7 +345,7 @@ void BPFFilter::unblock(const DNSName& qname, uint16_t qtype)
       d_qNamesCount--;
     }
     else {
-      throw std::runtime_error("Error removing qname address " + qname.toString() + ": " + std::string(strerror(errno)));
+      throw std::runtime_error("Error removing qname address " + qname.toLogString() + ": " + std::string(strerror(errno)));
     }
   }
 }
@@ -332,8 +359,8 @@ std::vector<std::pair<ComboAddress, uint64_t> > BPFFilter::getAddrStats()
   uint32_t nextV4Key;
   uint64_t value;
   int res = bpf_get_next_key(d_v4map.fd, &v4Key, &nextV4Key);
-  sockaddr_in v4Addr = { 0 };
-  v4Addr.sin_port = 0;
+  sockaddr_in v4Addr;
+  memset(&v4Addr, 0, sizeof(v4Addr));
   v4Addr.sin_family = AF_INET;
 
   while (res == 0) {
@@ -348,9 +375,10 @@ std::vector<std::pair<ComboAddress, uint64_t> > BPFFilter::getAddrStats()
 
   uint8_t v6Key[16];
   uint8_t nextV6Key[16];
-  sockaddr_in6 v6Addr = { 0 };
+  sockaddr_in6 v6Addr;
+  memset(&v6Addr, 0, sizeof(v6Addr));
   v6Addr.sin6_family = AF_INET6;
-  v6Addr.sin6_port = 0;
+
   static_assert(sizeof(v6Addr.sin6_addr.s6_addr) == sizeof(v6Key), "POSIX mandates s6_addr to be an array of 16 uint8_t");
   for (size_t idx = 0; idx < sizeof(v6Key); idx++) {
     v6Key[idx] = 0;
@@ -376,8 +404,8 @@ std::vector<std::tuple<DNSName, uint16_t, uint64_t> > BPFFilter::getQNameStats()
   std::vector<std::tuple<DNSName, uint16_t, uint64_t> > result;
   std::unique_lock<std::mutex> lock(d_mutex);
 
-  struct QNameKey key = {0};
-  struct QNameKey nextKey = {0};
+  struct QNameKey key = { { 0 } };
+  struct QNameKey nextKey = { { 0 } };
   struct QNameValue value;
 
   int res = bpf_get_next_key(d_qnamemap.fd, &key, &nextKey);