33b374
From 396750cef533cf72c7e6a72e47a9c93e2e431cb7 Mon Sep 17 00:00:00 2001
33b374
From: Simon Kelley <simon@thekelleys.org.uk>
33b374
Date: Sat, 13 Aug 2016 22:34:11 +0100
33b374
Subject: [PATCH] Refactor openBSD pftables code to remove blatant copyright
33b374
 violation.
33b374
33b374
---
33b374
 src/tables.c | 90 +++++++++++++++++++++---------------------------------------
33b374
 1 file changed, 32 insertions(+), 58 deletions(-)
33b374
33b374
diff --git a/src/tables.c b/src/tables.c
33b374
index aae1252..4fa3487 100644
33b374
--- a/src/tables.c
33b374
+++ b/src/tables.c
33b374
@@ -53,52 +53,6 @@ static char *pfr_strerror(int errnum)
33b374
     }
33b374
 }
33b374
 
33b374
-static int pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
33b374
-{
33b374
-  struct pfioc_table io;
33b374
-  
33b374
-  if (size < 0 || (size && tbl == NULL)) 
33b374
-    {
33b374
-      errno = EINVAL;
33b374
-      return (-1);
33b374
-    }
33b374
-  bzero(&io, sizeof io);
33b374
-  io.pfrio_flags = flags;
33b374
-  io.pfrio_buffer = tbl;
33b374
-  io.pfrio_esize = sizeof(*tbl);
33b374
-  io.pfrio_size = size;
33b374
-  if (ioctl(dev, DIOCRADDTABLES, &io))
33b374
-    return (-1);
33b374
-  if (nadd != NULL)
33b374
-    *nadd = io.pfrio_nadd;
33b374
-  return (0);
33b374
-}
33b374
-
33b374
-static int fill_addr(const struct all_addr *ipaddr, int flags, struct pfr_addr* addr) {
33b374
-  if ( !addr || !ipaddr)
33b374
-    {
33b374
-      my_syslog(LOG_ERR, _("error: fill_addr missused"));
33b374
-      return -1;
33b374
-    }
33b374
-  bzero(addr, sizeof(*addr));
33b374
-#ifdef HAVE_IPV6
33b374
-  if (flags & F_IPV6) 
33b374
-    {
33b374
-      addr->pfra_af = AF_INET6;
33b374
-      addr->pfra_net = 0x80;
33b374
-      memcpy(&(addr->pfra_ip6addr), &(ipaddr->addr), sizeof(struct in6_addr));
33b374
-    } 
33b374
-  else 
33b374
-#endif
33b374
-    {
33b374
-      addr->pfra_af = AF_INET;
33b374
-      addr->pfra_net = 0x20;
33b374
-      addr->pfra_ip4addr.s_addr = ipaddr->addr.addr4.s_addr;
33b374
-    }
33b374
-  return 1;
33b374
-}
33b374
-
33b374
-/*****************************************************************************/
33b374
 
33b374
 void ipset_init(void) 
33b374
 {
33b374
@@ -111,14 +65,13 @@ void ipset_init(void)
33b374
 }
33b374
 
33b374
 int add_to_ipset(const char *setname, const struct all_addr *ipaddr,
33b374
-		      int flags, int remove)
33b374
+		 int flags, int remove)
33b374
 {
33b374
   struct pfr_addr addr;
33b374
   struct pfioc_table io;
33b374
   struct pfr_table table;
33b374
-  int n = 0, rc = 0;
33b374
 
33b374
-  if ( dev == -1 ) 
33b374
+  if (dev == -1) 
33b374
     {
33b374
       my_syslog(LOG_ERR, _("warning: no opened pf devices %s"), pf_device);
33b374
       return -1;
33b374
@@ -126,31 +79,52 @@ int add_to_ipset(const char *setname, const struct all_addr *ipaddr,
33b374
 
33b374
   bzero(&table, sizeof(struct pfr_table));
33b374
   table.pfrt_flags |= PFR_TFLAG_PERSIST;
33b374
-  if ( strlen(setname) >= PF_TABLE_NAME_SIZE )
33b374
+  if (strlen(setname) >= PF_TABLE_NAME_SIZE)
33b374
     {
33b374
       my_syslog(LOG_ERR, _("error: cannot use table name %s"), setname);
33b374
       errno = ENAMETOOLONG;
33b374
       return -1;
33b374
     }
33b374
   
33b374
-  if ( strlcpy(table.pfrt_name, setname,
33b374
-               sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name)) 
33b374
+  if (strlcpy(table.pfrt_name, setname,
33b374
+	      sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name)) 
33b374
     {
33b374
       my_syslog(LOG_ERR, _("error: cannot strlcpy table name %s"), setname);
33b374
       return -1;
33b374
     }
33b374
   
33b374
-  if ((rc = pfr_add_tables(&table, 1, &n, 0))) 
33b374
+  bzero(&io, sizeof io);
33b374
+  io.pfrio_flags = 0;
33b374
+  io.pfrio_buffer = &table;
33b374
+  io.pfrio_esize = sizeof(table);
33b374
+  io.pfrio_size = 1;
33b374
+  if (ioctl(dev, DIOCRADDTABLES, &io))
33b374
     {
33b374
-      my_syslog(LOG_WARNING, _("warning: pfr_add_tables: %s(%d)"),
33b374
-		pfr_strerror(errno),rc);
33b374
+      my_syslog(LOG_WARNING, _("IPset: error:%s"), pfr_strerror(errno));
33b374
+      
33b374
       return -1;
33b374
     }
33b374
+  
33b374
   table.pfrt_flags &= ~PFR_TFLAG_PERSIST;
33b374
-  if (n)
33b374
+  if (io.pfrio_nadd)
33b374
     my_syslog(LOG_INFO, _("info: table created"));
33b374
-  
33b374
-  fill_addr(ipaddr,flags,&addr);
33b374
+ 
33b374
+  bzero(&addr, sizeof(addr));
33b374
+#ifdef HAVE_IPV6
33b374
+  if (flags & F_IPV6) 
33b374
+    {
33b374
+      addr.pfra_af = AF_INET6;
33b374
+      addr.pfra_net = 0x80;
33b374
+      memcpy(&(addr.pfra_ip6addr), &(ipaddr->addr), sizeof(struct in6_addr));
33b374
+    } 
33b374
+  else 
33b374
+#endif
33b374
+    {
33b374
+      addr.pfra_af = AF_INET;
33b374
+      addr.pfra_net = 0x20;
33b374
+      addr.pfra_ip4addr.s_addr = ipaddr->addr.addr4.s_addr;
33b374
+    }
33b374
+
33b374
   bzero(&io, sizeof(io));
33b374
   io.pfrio_flags = 0;
33b374
   io.pfrio_table = table;
33b374
-- 
33b374
2.9.3
33b374