Somebody on IRC mentioned that using ifconfig to set wgallowedips wasn't
working on macppc. I don't have a macppc to test this on, but it seems
like the code is assuming that the two values printed out by this test
program must always be the same:

  struct s {
      int i;
  };

  struct p {
      long l;
      char c;
      struct s a[];
  };

  int main(int argc, char *argv[])
  {
      printf("%zu %zu\n", sizeof(struct p), (size_t)&((struct p *)0)->a[0]);
      return 0;
  }

But actually, on my amd64 system, that little test prints out "16 12".
This patch fixes up ifconfig.c to do the right thing, so that it
corresponds with how the kernel handles iteration.

I don't have a macppc in order to test this, but it works on amd64.
---
 sbin/ifconfig/ifconfig.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git sbin/ifconfig/ifconfig.c sbin/ifconfig/ifconfig.c
index 2ccbac6f4ce..ade6e4943b7 100644
--- sbin/ifconfig/ifconfig.c
+++ sbin/ifconfig/ifconfig.c
@@ -5696,11 +5696,10 @@ ensurewginterface(void)
                err(1, "calloc");
 }
 
-void *
+void
 growwgdata(size_t by)
 {
        ptrdiff_t peer_offset, aip_offset;
-       void *ret;
 
        if (wg_interface == NULL)
                wgdata.wgd_size = sizeof(*wg_interface);
@@ -5721,16 +5720,18 @@ growwgdata(size_t by)
        if (wg_aip != NULL)
                wg_aip = (void *)wg_interface + aip_offset;
 
-       ret = (void *)wg_interface + wgdata.wgd_size - by;
-       bzero(ret, by);
-
-       return ret;
+       bzero((void *)wg_interface + wgdata.wgd_size - by, by);
 }
 
 void
 setwgpeer(const char *peerkey_b64, int param)
 {
-       wg_peer = growwgdata(sizeof(*wg_peer));
+       growwgdata(sizeof(*wg_peer));
+       if (wg_aip)
+               wg_peer = (struct wg_peer_io *)wg_aip;
+       else
+               wg_peer = &wg_interface->i_peers[0];
+       wg_aip = &wg_peer->p_aips[0];
        wg_peer->p_flags |= WG_PEER_HAS_PUBLIC;
        WG_LOAD_KEY(wg_peer->p_public, peerkey_b64, "wgpeer");
        wg_interface->i_peers_count++;
@@ -5743,7 +5744,7 @@ setwgpeeraip(const char *aip, int param)
        if (wg_peer == NULL)
                errx(1, "wgaip: wgpeer not set");
 
-       wg_aip = growwgdata(sizeof(*wg_aip));
+       growwgdata(sizeof(*wg_aip));
 
        if ((res = inet_net_pton(AF_INET, aip, &wg_aip->a_ipv4,
            sizeof(wg_aip->a_ipv4))) != -1) {
@@ -5759,6 +5760,8 @@ setwgpeeraip(const char *aip, int param)
 
        wg_peer->p_flags |= WG_PEER_REPLACE_AIPS;
        wg_peer->p_aips_count++;
+
+       wg_aip++;
 }
 
 void
-- 
2.29.1

Reply via email to