From 2fe68810990c0455a15538e64aaa474be22c0bfc Mon Sep 17 00:00:00 2001
From: Alexander Chernikov <melifaro@FreeBSD.org>
Date: Thu, 15 Dec 2022 19:58:35 +0000
Subject: [PATCH 3/3] netlink: move OS-specific headers and defines to sysdep.

---
 sysdep/bsd/netlink-nosup.h |  4 +++
 sysdep/bsd/netlink-sys.h   | 29 ++++++++++++++++++
 sysdep/cf/bsd-netlink.h    | 11 +------
 sysdep/cf/bsd.h            |  1 +
 sysdep/cf/linux.h          |  1 +
 sysdep/linux/netlink-sys.h | 63 ++++++++++++++++++++++++++++++++++++++
 sysdep/linux/netlink.c     | 53 ++------------------------------
 7 files changed, 101 insertions(+), 61 deletions(-)
 create mode 100644 sysdep/bsd/netlink-nosup.h
 create mode 100644 sysdep/bsd/netlink-sys.h
 create mode 100644 sysdep/linux/netlink-sys.h

diff --git a/sysdep/bsd/netlink-nosup.h b/sysdep/bsd/netlink-nosup.h
new file mode 100644
index 00000000..eb6b7943
--- /dev/null
+++ b/sysdep/bsd/netlink-nosup.h
@@ -0,0 +1,4 @@
+#ifndef _BIRD_NETLINK_SYS_H_
+#define _BIRD_NETLINK_SYS_H_
+
+#endif
diff --git a/sysdep/bsd/netlink-sys.h b/sysdep/bsd/netlink-sys.h
new file mode 100644
index 00000000..a6e5052b
--- /dev/null
+++ b/sysdep/bsd/netlink-sys.h
@@ -0,0 +1,29 @@
+/*
+ *	Netlink FreeBSD-specific functions
+ *
+ *	(c) 2022 Alexander Chernikov <melifaro@FreeBSD.org>
+ *
+ *	Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_NETLINK_SYS_H_
+#define _BIRD_NETLINK_SYS_H_
+
+#include <netlink/netlink.h>
+#include <netlink/netlink_route.h>
+
+#ifndef	AF_MPLS
+#define	AF_MPLS	39
+#endif
+
+#ifndef	SO_RCVBUFFORCE
+#define	SO_RCVBUFFORCE	SO_RCVBUF
+#endif
+
+static inline int
+netlink_error_to_os(int error)
+{
+	return (error);
+}
+
+#endif
diff --git a/sysdep/cf/bsd-netlink.h b/sysdep/cf/bsd-netlink.h
index 09149dc4..c00f2fd8 100644
--- a/sysdep/cf/bsd-netlink.h
+++ b/sysdep/cf/bsd-netlink.h
@@ -17,16 +17,7 @@
 
 #define CONFIG_INCLUDE_SYSIO_H "sysdep/bsd/sysio.h"
 #define CONFIG_INCLUDE_KRTSYS_H "sysdep/linux/krt-sys.h"
-
-#define	CONFIG_FREEBSD_NETLINK
-
-#ifndef	AF_MPLS
-#define	AF_MPLS	39
-#endif
-
-#ifndef	SO_RCVBUFFORCE
-#define	SO_RCVBUFFORCE	SO_RCVBUF
-#endif
+#define CONFIG_INCLUDE_NLSYS_H "sysdep/bsd/netlink-sys.h"
 
 /*
 Link: sysdep/unix
diff --git a/sysdep/cf/bsd.h b/sysdep/cf/bsd.h
index 22c54277..048b1718 100644
--- a/sysdep/cf/bsd.h
+++ b/sysdep/cf/bsd.h
@@ -17,6 +17,7 @@
 
 #define CONFIG_INCLUDE_SYSIO_H "sysdep/bsd/sysio.h"
 #define CONFIG_INCLUDE_KRTSYS_H "sysdep/bsd/krt-sys.h"
+#define CONFIG_INCLUDE_NLSYS_H "sysdep/bsd/netlink-nosup.h"
 
 /*
 Link: sysdep/unix
diff --git a/sysdep/cf/linux.h b/sysdep/cf/linux.h
index 85598c10..9c37dd8a 100644
--- a/sysdep/cf/linux.h
+++ b/sysdep/cf/linux.h
@@ -16,6 +16,7 @@
 
 #define CONFIG_INCLUDE_SYSIO_H "sysdep/linux/sysio.h"
 #define CONFIG_INCLUDE_KRTSYS_H "sysdep/linux/krt-sys.h"
+#define CONFIG_INCLUDE_NLSYS_H "sysdep/linux/netlink-sys.h"
 
 #define CONFIG_LINUX_NETLINK
 
diff --git a/sysdep/linux/netlink-sys.h b/sysdep/linux/netlink-sys.h
new file mode 100644
index 00000000..4c99307b
--- /dev/null
+++ b/sysdep/linux/netlink-sys.h
@@ -0,0 +1,63 @@
+/*
+ *	BIRD -- Linux Netlink Interface
+ *
+ *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
+ *
+ *	Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_NETLINK_SYS_H_
+#define _BIRD_NETLINK_SYS_H_
+
+#include <asm/types.h>
+#include <linux/if.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+
+#ifdef HAVE_MPLS_KERNEL
+#include <linux/lwtunnel.h>
+#endif
+
+#ifndef MSG_TRUNC			/* Hack: Several versions of glibc miss this one :( */
+#define MSG_TRUNC 0x20
+#endif
+
+#ifndef IFA_FLAGS
+#define IFA_FLAGS 8
+#endif
+
+#ifndef IFF_LOWER_UP
+#define IFF_LOWER_UP 0x10000
+#endif
+
+#ifndef RTA_TABLE
+#define RTA_TABLE  15
+#endif
+
+#ifndef RTA_VIA
+#define RTA_VIA	 18
+#endif
+
+#ifndef RTA_NEWDST
+#define RTA_NEWDST  19
+#endif
+
+#ifndef RTA_ENCAP_TYPE
+#define RTA_ENCAP_TYPE	21
+#endif
+
+#ifndef RTA_ENCAP
+#define RTA_ENCAP  22
+#endif
+
+#ifndef NETLINK_GET_STRICT_CHK
+#define NETLINK_GET_STRICT_CHK 12
+#endif
+
+static inline int
+netlink_error_to_os(int error)
+{
+	return -error;
+}
+
+#endif
diff --git a/sysdep/linux/netlink.c b/sysdep/linux/netlink.c
index dcb25911..7f0d4736 100644
--- a/sysdep/linux/netlink.c
+++ b/sysdep/linux/netlink.c
@@ -27,56 +27,7 @@
 #include "lib/hash.h"
 #include "conf/conf.h"
 
-#ifdef CONFIG_LINUX_NETLINK
-#include <asm/types.h>
-#include <linux/if.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#endif
-#ifdef CONFIG_FREEBSD_NETLINK
-#include <netlink/netlink.h>
-#include <netlink/netlink_route.h>
-#endif
-
-#ifdef HAVE_MPLS_KERNEL
-#include <linux/lwtunnel.h>
-#endif
-
-#ifndef MSG_TRUNC			/* Hack: Several versions of glibc miss this one :( */
-#define MSG_TRUNC 0x20
-#endif
-
-#ifndef IFA_FLAGS
-#define IFA_FLAGS 8
-#endif
-
-#ifndef IFF_LOWER_UP
-#define IFF_LOWER_UP 0x10000
-#endif
-
-#ifndef RTA_TABLE
-#define RTA_TABLE  15
-#endif
-
-#ifndef RTA_VIA
-#define RTA_VIA	 18
-#endif
-
-#ifndef RTA_NEWDST
-#define RTA_NEWDST  19
-#endif
-
-#ifndef RTA_ENCAP_TYPE
-#define RTA_ENCAP_TYPE	21
-#endif
-
-#ifndef RTA_ENCAP
-#define RTA_ENCAP  22
-#endif
-
-#ifndef NETLINK_GET_STRICT_CHK
-#define NETLINK_GET_STRICT_CHK 12
-#endif
+#include CONFIG_INCLUDE_NLSYS_H
 
 #define krt_ipv4(p) ((p)->af == AF_INET)
 
@@ -320,7 +271,7 @@ nl_error(struct nlmsghdr *h, int ignore_esrch)
       return ENOBUFS;
     }
   e = (struct nlmsgerr *) NLMSG_DATA(h);
-  ec = -e->error;
+  ec = netlink_error_to_os(e->error);
   if (ec && !(ignore_esrch && (ec == ESRCH)))
     log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec));
   return ec;
-- 
2.37.1

