git: 40beb4ae59ce - stable/12 - vxlan: Add support for socket ioctls SIOC[SG]TUNFIB

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Fri, 13 Jan 2023 04:33:34 UTC
The branch stable/12 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=40beb4ae59ce85c3139539d87ad52a777ee2cbf4

commit 40beb4ae59ce85c3139539d87ad52a777ee2cbf4
Author:     Zhenlei Huang <zlei.huang@gmail.com>
AuthorDate: 2022-07-08 18:12:14 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2023-01-13 04:30:20 +0000

    vxlan: Add support for socket ioctls SIOC[SG]TUNFIB
    
    Submitted by: Luiz Amaral <email@luiz.eng.br>
    PR:     244004
    Reviewed by:    gbe (manpages), melifaro, pauamma (manpages)
    Approved by:    kp (mentor)
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D32820
    
    (cherry picked from commit 7f7a804ae077a0c43d8c3f1444f90bdfb841c6b1)
    (cherry picked from commit 8819353dd0dfddb9bcd156a2773efc3770d09eff)
---
 sbin/ifconfig/ifconfig.8 |  5 +++--
 sys/net/if_vxlan.c       | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8
index f1c09885be0f..5c3a554c2b46 100644
--- a/sbin/ifconfig/ifconfig.8
+++ b/sbin/ifconfig/ifconfig.8
@@ -390,9 +390,10 @@ Specify tunnel FIB.
 A FIB
 .Ar fib_number
 is assigned to all packets encapsulated by tunnel interface, e.g.,
-.Xr gif 4
+.Xr gif 4 ,
+.Xr gre 4
 and
-.Xr gre 4 .
+.Xr vxlan 4 .
 .It Cm maclabel Ar label
 If Mandatory Access Control support is enabled in the kernel,
 set the MAC label to
diff --git a/sys/net/if_vxlan.c b/sys/net/if_vxlan.c
index 67aa760f7fcf..7cb0471b8667 100644
--- a/sys/net/if_vxlan.c
+++ b/sys/net/if_vxlan.c
@@ -141,6 +141,7 @@ struct vxlan_statistics {
 
 struct vxlan_softc {
 	struct ifnet			*vxl_ifp;
+	u_int				 vxl_fibnum;
 	struct vxlan_socket		*vxl_sock;
 	uint32_t			 vxl_vni;
 	union vxlan_sockaddr		 vxl_src_addr;
@@ -2238,6 +2239,7 @@ vxlan_ioctl_drvspec(struct vxlan_softc *sc, struct ifdrv *ifd, int get)
 static int
 vxlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 {
+	struct rm_priotracker tracker;
 	struct vxlan_softc *sc;
 	struct ifreq *ifr;
 	struct ifdrv *ifd;
@@ -2247,10 +2249,11 @@ vxlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 	ifr = (struct ifreq *) data;
 	ifd = (struct ifdrv *) data;
 
+	error = 0;
+
 	switch (cmd) {
 	case SIOCADDMULTI:
 	case SIOCDELMULTI:
-		error = 0;
 		break;
 
 	case SIOCGDRVSPEC:
@@ -2267,6 +2270,25 @@ vxlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 		error = ifmedia_ioctl(ifp, ifr, &sc->vxl_media, cmd);
 		break;
 
+	case SIOCGTUNFIB:
+		VXLAN_RLOCK(sc, &tracker);
+		ifr->ifr_fib = sc->vxl_fibnum;
+		VXLAN_RUNLOCK(sc, &tracker);
+		break;
+
+	case SIOCSTUNFIB:
+		if ((error = priv_check(curthread, PRIV_NET_VXLAN)) != 0)
+			break;
+
+		if (ifr->ifr_fib >= rt_numfibs)
+			error = EINVAL;
+		else {
+			VXLAN_WLOCK(sc);
+			sc->vxl_fibnum = ifr->ifr_fib;
+			VXLAN_WUNLOCK(sc);
+		}
+		break;
+
 	default:
 		error = ether_ioctl(ifp, cmd, data);
 		break;
@@ -2462,6 +2484,7 @@ vxlan_transmit(struct ifnet *ifp, struct mbuf *m)
 	ETHER_BPF_MTAP(ifp, m);
 
 	VXLAN_RLOCK(sc, &tracker);
+	M_SETFIB(m, sc->vxl_fibnum);
 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 		VXLAN_RUNLOCK(sc, &tracker);
 		m_freem(m);
@@ -2721,6 +2744,7 @@ vxlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
 
 	sc = malloc(sizeof(struct vxlan_softc), M_VXLAN, M_WAITOK | M_ZERO);
 	sc->vxl_unit = unit;
+	sc->vxl_fibnum = curthread->td_proc->p_fibnum;
 	vxlan_set_default_config(sc);
 
 	if (params != 0) {