git: 1a5f7b44690a - stable/12 - libpfctl: relocate implementations of pfr_add/get/set_addrs
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 11 Apr 2022 14:53:19 UTC
The branch stable/12 has been updated by kp:
URL: https://cgit.FreeBSD.org/src/commit/?id=1a5f7b44690af620291a8e4110942ceebab585b0
commit 1a5f7b44690af620291a8e4110942ceebab585b0
Author: Reid Linnemann <rlinnemann@netgate.com>
AuthorDate: 2022-04-04 14:43:38 +0000
Commit: Kristof Provost <kp@FreeBSD.org>
CommitDate: 2022-04-11 14:52:52 +0000
libpfctl: relocate implementations of pfr_add/get/set_addrs
Reviewed by: kp
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D34740
(cherry picked from commit 4823489ab61dbaef4405cf03d2a48e77e593ce9c)
---
lib/libpfctl/libpfctl.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++
lib/libpfctl/libpfctl.h | 10 ++++-
sbin/pfctl/pfctl_radix.c | 76 +++++++++-----------------------------
3 files changed, 122 insertions(+), 60 deletions(-)
diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c
index 3a06600f396c..638f1de7a070 100644
--- a/lib/libpfctl/libpfctl.c
+++ b/lib/libpfctl/libpfctl.c
@@ -1127,3 +1127,99 @@ pfctl_get_syncookies(int dev, struct pfctl_syncookies *s)
return (0);
}
+
+int
+pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *nadd, int flags)
+{
+ struct pfioc_table io;
+
+ if (tbl == NULL || size < 0 || (size && addr == NULL)) {
+ return (EINVAL);
+ }
+ bzero(&io, sizeof io);
+ io.pfrio_flags = flags;
+ io.pfrio_table = *tbl;
+ io.pfrio_buffer = addr;
+ io.pfrio_esize = sizeof(*addr);
+ io.pfrio_size = size;
+
+ if (ioctl(dev, DIOCRADDADDRS, &io))
+ return (errno);
+ if (nadd != NULL)
+ *nadd = io.pfrio_nadd;
+ return (0);
+}
+
+int
+pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *ndel, int flags)
+{
+ struct pfioc_table io;
+
+ if (tbl == NULL || size < 0 || (size && addr == NULL)) {
+ return (EINVAL);
+ }
+ bzero(&io, sizeof io);
+ io.pfrio_flags = flags;
+ io.pfrio_table = *tbl;
+ io.pfrio_buffer = addr;
+ io.pfrio_esize = sizeof(*addr);
+ io.pfrio_size = size;
+
+ if (ioctl(dev, DIOCRDELADDRS, &io))
+ return (errno);
+ if (ndel != NULL)
+ *ndel = io.pfrio_ndel;
+ return (0);
+}
+
+int
+pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *size2, int *nadd, int *ndel, int *nchange, int flags)
+{
+ struct pfioc_table io;
+
+ if (tbl == NULL || size < 0 || (size && addr == NULL)) {
+ return (EINVAL);
+ }
+ bzero(&io, sizeof io);
+ io.pfrio_flags = flags;
+ io.pfrio_table = *tbl;
+ io.pfrio_buffer = addr;
+ io.pfrio_esize = sizeof(*addr);
+ io.pfrio_size = size;
+ io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
+ if (ioctl(dev, DIOCRSETADDRS, &io))
+ return (-1);
+ if (nadd != NULL)
+ *nadd = io.pfrio_nadd;
+ if (ndel != NULL)
+ *ndel = io.pfrio_ndel;
+ if (nchange != NULL)
+ *nchange = io.pfrio_nchange;
+ if (size2 != NULL)
+ *size2 = io.pfrio_size2;
+ return (0);
+}
+
+int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr *addr,
+ int *size, int flags)
+{
+ struct pfioc_table io;
+
+ if (tbl == NULL || size == NULL || *size < 0 ||
+ (*size && addr == NULL)) {
+ return (EINVAL);
+ }
+ bzero(&io, sizeof io);
+ io.pfrio_flags = flags;
+ io.pfrio_table = *tbl;
+ io.pfrio_buffer = addr;
+ io.pfrio_esize = sizeof(*addr);
+ io.pfrio_size = *size;
+ if (ioctl(dev, DIOCRGETADDRS, &io))
+ return (-1);
+ *size = io.pfrio_size;
+ return (0);
+}
diff --git a/lib/libpfctl/libpfctl.h b/lib/libpfctl/libpfctl.h
index 726ec89cd3d6..f9893566c2dd 100644
--- a/lib/libpfctl/libpfctl.h
+++ b/lib/libpfctl/libpfctl.h
@@ -315,5 +315,13 @@ int pfctl_clear_rules(int dev, const char *anchorname);
int pfctl_clear_nat(int dev, const char *anchorname);
int pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s);
int pfctl_get_syncookies(int dev, struct pfctl_syncookies *s);
-
+int pfctl_table_add_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *nadd, int flags);
+int pfctl_table_del_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *ndel, int flags);
+int pfctl_table_set_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int size, int *size2, int *nadd, int *ndel, int *nchange,
+ int flags);
+int pfctl_table_get_addrs(int dev, struct pfr_table *tbl, struct pfr_addr
+ *addr, int *size, int flags);
#endif
diff --git a/sbin/pfctl/pfctl_radix.c b/sbin/pfctl/pfctl_radix.c
index 6b303c6211f5..f26382e9ef24 100644
--- a/sbin/pfctl/pfctl_radix.c
+++ b/sbin/pfctl/pfctl_radix.c
@@ -186,22 +186,13 @@ int
pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
int *nadd, int flags)
{
- struct pfioc_table io;
+ int ret;
- if (tbl == NULL || size < 0 || (size && addr == NULL)) {
- errno = EINVAL;
+ ret = pfctl_table_add_addrs(dev, tbl, addr, size, nadd, flags);
+ if (ret) {
+ errno = ret;
return (-1);
}
- bzero(&io, sizeof io);
- io.pfrio_flags = flags;
- io.pfrio_table = *tbl;
- io.pfrio_buffer = addr;
- io.pfrio_esize = sizeof(*addr);
- io.pfrio_size = size;
- if (ioctl(dev, DIOCRADDADDRS, &io))
- return (-1);
- if (nadd != NULL)
- *nadd = io.pfrio_nadd;
return (0);
}
@@ -209,22 +200,13 @@ int
pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
int *ndel, int flags)
{
- struct pfioc_table io;
+ int ret;
- if (tbl == NULL || size < 0 || (size && addr == NULL)) {
- errno = EINVAL;
+ ret = pfctl_table_del_addrs(dev, tbl, addr, size, ndel, flags);
+ if (ret) {
+ errno = ret;
return (-1);
}
- bzero(&io, sizeof io);
- io.pfrio_flags = flags;
- io.pfrio_table = *tbl;
- io.pfrio_buffer = addr;
- io.pfrio_esize = sizeof(*addr);
- io.pfrio_size = size;
- if (ioctl(dev, DIOCRDELADDRS, &io))
- return (-1);
- if (ndel != NULL)
- *ndel = io.pfrio_ndel;
return (0);
}
@@ -232,29 +214,14 @@ int
pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
int *size2, int *nadd, int *ndel, int *nchange, int flags)
{
- struct pfioc_table io;
+ int ret;
- if (tbl == NULL || size < 0 || (size && addr == NULL)) {
- errno = EINVAL;
+ ret = pfctl_table_set_addrs(dev, tbl, addr, size, size2, nadd, ndel,
+ nchange, flags);
+ if (ret) {
+ errno = ret;
return (-1);
}
- bzero(&io, sizeof io);
- io.pfrio_flags = flags;
- io.pfrio_table = *tbl;
- io.pfrio_buffer = addr;
- io.pfrio_esize = sizeof(*addr);
- io.pfrio_size = size;
- io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
- if (ioctl(dev, DIOCRSETADDRS, &io))
- return (-1);
- if (nadd != NULL)
- *nadd = io.pfrio_nadd;
- if (ndel != NULL)
- *ndel = io.pfrio_ndel;
- if (nchange != NULL)
- *nchange = io.pfrio_nchange;
- if (size2 != NULL)
- *size2 = io.pfrio_size2;
return (0);
}
@@ -262,22 +229,13 @@ int
pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
int flags)
{
- struct pfioc_table io;
+ int ret;
- if (tbl == NULL || size == NULL || *size < 0 ||
- (*size && addr == NULL)) {
- errno = EINVAL;
+ ret = pfctl_table_get_addrs(dev, tbl, addr, size, flags);
+ if (ret) {
+ errno = ret;
return (-1);
}
- bzero(&io, sizeof io);
- io.pfrio_flags = flags;
- io.pfrio_table = *tbl;
- io.pfrio_buffer = addr;
- io.pfrio_esize = sizeof(*addr);
- io.pfrio_size = *size;
- if (ioctl(dev, DIOCRGETADDRS, &io))
- return (-1);
- *size = io.pfrio_size;
return (0);
}