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