Skip to content

Commit cae0285

Browse files
committed
wg: Port DragonFlyBSD#15: adapt 'struct ifnet' related code and routines
Remove unsupported routines, like wg_qflush() and wg_reassign(). Also remove the unsupported 'IFF_DYING' workaround for IPv6. If we also have such an issue, should fix the IPv6 code. TODO: rework the if_output() and if_start() routines.
1 parent 6c9ab66 commit cae0285

File tree

1 file changed

+54
-74
lines changed

1 file changed

+54
-74
lines changed

sys/net/wg/if_wg.c

Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <net/if_clone.h>
3636
#include <net/if_types.h>
3737
#include <net/if_var.h>
38+
#include <net/ifq_var.h>
3839
#include <net/netisr.h>
3940
#include <net/radix.h>
4041
#include <netinet/in.h>
@@ -65,7 +66,9 @@
6566
#define NEW_HANDSHAKE_TIMEOUT (REKEY_TIMEOUT + KEEPALIVE_TIMEOUT)
6667
#define UNDERLOAD_TIMEOUT 1
6768

68-
#define DPRINTF(sc, ...) if (if_getflags(sc->sc_ifp) & IFF_DEBUG) if_printf(sc->sc_ifp, ##__VA_ARGS__)
69+
#define DPRINTF(sc, ...) \
70+
if (sc->sc_ifp->if_flags & IFF_DEBUG) \
71+
if_printf(sc->sc_ifp, ##__VA_ARGS__)
6972

7073
/* First byte indicating packet type on the wire */
7174
#define WG_PKT_INITIATION htole32(1)
@@ -216,7 +219,7 @@ struct wg_socket {
216219

217220
struct wg_softc {
218221
LIST_ENTRY(wg_softc) sc_entry;
219-
if_t sc_ifp;
222+
struct ifnet *sc_ifp;
220223
int sc_flags;
221224

222225
struct ucred *sc_ucred;
@@ -363,19 +366,17 @@ static struct wg_packet *wg_queue_dequeue_serial(struct wg_queue *);
363366
static struct wg_packet *wg_queue_dequeue_parallel(struct wg_queue *);
364367
static bool wg_input(struct mbuf *, int, struct inpcb *, const struct sockaddr *, void *);
365368
static void wg_peer_send_staged(struct wg_peer *);
366-
static void wg_qflush(if_t);
367369
static inline int determine_af_and_pullup(struct mbuf **m, sa_family_t *af);
368-
static int wg_xmit(if_t, struct mbuf *, sa_family_t, uint32_t);
369-
static int wg_transmit(if_t, struct mbuf *);
370-
static int wg_output(if_t, struct mbuf *, const struct sockaddr *, struct route *);
370+
static int wg_xmit(struct ifnet *, struct mbuf *, sa_family_t, uint32_t);
371+
static int wg_transmit(struct ifnet *, struct mbuf *);
372+
static int wg_output(struct ifnet *, struct mbuf *, const struct sockaddr *, struct rtentry *);
371373
static bool wgc_privileged(struct wg_softc *);
372374
static int wgc_get(struct wg_softc *, struct wg_data_io *);
373375
static int wgc_set(struct wg_softc *, struct wg_data_io *);
374376
static int wg_up(struct wg_softc *);
375377
static void wg_down(struct wg_softc *);
376-
static void wg_reassign(if_t, struct vnet *, char *unused);
377378
static void wg_init(void *);
378-
static int wg_ioctl(if_t, u_long, caddr_t);
379+
static int wg_ioctl(struct ifnet *, u_long, caddr_t);
379380
static int wg_module_init(void);
380381
static int wg_module_deinit(void);
381382

@@ -1660,7 +1661,7 @@ static void
16601661
wg_deliver_in(struct wg_peer *peer)
16611662
{
16621663
struct wg_softc *sc = peer->p_sc;
1663-
if_t ifp = sc->sc_ifp;
1664+
struct ifnet *ifp = sc->sc_ifp;
16641665
struct wg_packet *pkt;
16651666
struct mbuf *m;
16661667
struct epoch_tracker et;
@@ -2031,7 +2032,7 @@ wg_peer_send_staged(struct wg_peer *peer)
20312032
}
20322033

20332034
static inline void
2034-
xmit_err(if_t ifp, struct mbuf *m, struct wg_packet *pkt, sa_family_t af)
2035+
xmit_err(struct ifnet *ifp, struct mbuf *m, struct wg_packet *pkt, sa_family_t af)
20352036
{
20362037
IFNET_STAT_INC(ifp, oerrors, 1);
20372038
switch (af) {
@@ -2059,20 +2060,14 @@ xmit_err(if_t ifp, struct mbuf *m, struct wg_packet *pkt, sa_family_t af)
20592060
}
20602061

20612062
static int
2062-
wg_xmit(if_t ifp, struct mbuf *m, sa_family_t af, uint32_t mtu)
2063+
wg_xmit(struct ifnet *ifp, struct mbuf *m, sa_family_t af, uint32_t mtu)
20632064
{
20642065
struct wg_packet *pkt = NULL;
2065-
struct wg_softc *sc = if_getsoftc(ifp);
2066+
struct wg_softc *sc = ifp->if_softc;
20662067
struct wg_peer *peer;
20672068
int rc = 0;
20682069
sa_family_t peer_af;
20692070

2070-
/* Work around lifetime issue in the ipv6 mld code. */
2071-
if (__predict_false((if_getflags(ifp) & IFF_DYING) || !sc)) {
2072-
rc = ENXIO;
2073-
goto err_xmit;
2074-
}
2075-
20762071
if ((pkt = wg_packet_alloc(m)) == NULL) {
20772072
rc = ENOBUFS;
20782073
goto err_xmit;
@@ -2145,7 +2140,7 @@ determine_af_and_pullup(struct mbuf **m, sa_family_t *af)
21452140
}
21462141

21472142
static int
2148-
wg_transmit(if_t ifp, struct mbuf *m)
2143+
wg_transmit(struct ifnet *ifp, struct mbuf *m)
21492144
{
21502145
sa_family_t af;
21512146
int ret;
@@ -2165,11 +2160,11 @@ wg_transmit(if_t ifp, struct mbuf *m)
21652160
xmit_err(ifp, m, NULL, AF_UNSPEC);
21662161
return (ret);
21672162
}
2168-
return (wg_xmit(ifp, m, af, if_getmtu(ifp)));
2163+
return (wg_xmit(ifp, m, af, ifp->if_mtu));
21692164
}
21702165

21712166
static int
2172-
wg_output(if_t ifp, struct mbuf *m, const struct sockaddr *dst, struct route *ro)
2167+
wg_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, struct rtentry *rt)
21732168
{
21742169
sa_family_t parsed_af;
21752170
uint32_t af, mtu;
@@ -2203,7 +2198,7 @@ wg_output(if_t ifp, struct mbuf *m, const struct sockaddr *dst, struct route *ro
22032198
xmit_err(ifp, m, NULL, AF_UNSPEC);
22042199
return (EAFNOSUPPORT);
22052200
}
2206-
mtu = (ro != NULL && ro->ro_mtu > 0) ? ro->ro_mtu : if_getmtu(ifp);
2201+
mtu = (ro != NULL && ro->ro_mtu > 0) ? ro->ro_mtu : ifp->if_mtu;
22072202
return (wg_xmit(ifp, m, parsed_af, mtu));
22082203
}
22092204

@@ -2313,7 +2308,7 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl)
23132308
goto out;
23142309
TAILQ_INSERT_TAIL(&sc->sc_peers, peer, p_entry);
23152310
sc->sc_peers_num++;
2316-
if (if_getlinkstate(sc->sc_ifp) == LINK_STATE_UP)
2311+
if (sc->sc_ifp->if_link_state == LINK_STATE_UP)
23172312
wg_timers_enable(peer);
23182313
}
23192314
if (remote != NULL)
@@ -2331,7 +2326,7 @@ static int
23312326
wgc_set(struct wg_softc *sc, struct wg_data_io *wgd)
23322327
{
23332328
uint8_t public[WG_KEY_SIZE], private[WG_KEY_SIZE];
2334-
if_t ifp;
2329+
struct ifnet *ifp;
23352330
void *nvlpacked;
23362331
nvlist_t *nvl;
23372332
ssize_t size;
@@ -2367,7 +2362,7 @@ wgc_set(struct wg_softc *sc, struct wg_data_io *wgd)
23672362
goto out_locked;
23682363
}
23692364
if (new_port != sc->sc_socket.so_port) {
2370-
if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2365+
if ((ifp->if_flags & IFF_RUNNING) != 0) {
23712366
if ((err = wg_socket_init(sc, new_port)) != 0)
23722367
goto out_locked;
23732368
} else
@@ -2575,15 +2570,15 @@ wgc_get(struct wg_softc *sc, struct wg_data_io *wgd)
25752570
}
25762571

25772572
static int
2578-
wg_ioctl(if_t ifp, u_long cmd, caddr_t data)
2573+
wg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
25792574
{
25802575
struct wg_data_io *wgd = (struct wg_data_io *)data;
25812576
struct ifreq *ifr = (struct ifreq *)data;
25822577
struct wg_softc *sc;
25832578
int ret = 0;
25842579

25852580
lockmgr(&wg_lock, LK_SHARED);
2586-
sc = if_getsoftc(ifp);
2581+
sc = ifp->if_softc;
25872582
if (!sc) {
25882583
ret = ENXIO;
25892584
goto out;
@@ -2606,7 +2601,7 @@ wg_ioctl(if_t ifp, u_long cmd, caddr_t data)
26062601
*/
26072602
break;
26082603
case SIOCSIFFLAGS:
2609-
if (if_getflags(ifp) & IFF_UP)
2604+
if (ifp->if_flags & IFF_UP)
26102605
ret = wg_up(sc);
26112606
else
26122607
wg_down(sc);
@@ -2615,7 +2610,7 @@ wg_ioctl(if_t ifp, u_long cmd, caddr_t data)
26152610
if (ifr->ifr_mtu <= 0 || ifr->ifr_mtu > MAX_MTU)
26162611
ret = EINVAL;
26172612
else
2618-
if_setmtu(ifp, ifr->ifr_mtu);
2613+
ifp->if_mtu = ifr->ifr_mtu;
26192614
break;
26202615
case SIOCADDMULTI:
26212616
case SIOCDELMULTI:
@@ -2632,7 +2627,7 @@ wg_ioctl(if_t ifp, u_long cmd, caddr_t data)
26322627
static int
26332628
wg_up(struct wg_softc *sc)
26342629
{
2635-
if_t ifp = sc->sc_ifp;
2630+
struct ifnet *ifp = sc->sc_ifp;
26362631
struct wg_peer *peer;
26372632
int rc = EBUSY;
26382633

@@ -2644,17 +2639,18 @@ wg_up(struct wg_softc *sc)
26442639

26452640
/* Silent success if we're already running. */
26462641
rc = 0;
2647-
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2642+
if (ifp->if_flags & IFF_RUNNING)
26482643
goto out;
2649-
if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
2644+
ifp->if_flags |= IFF_RUNNING;
26502645

26512646
rc = wg_socket_init(sc, sc->sc_socket.so_port);
26522647
if (rc == 0) {
26532648
TAILQ_FOREACH(peer, &sc->sc_peers, p_entry)
26542649
wg_timers_enable(peer);
2655-
if_link_state_change(sc->sc_ifp, LINK_STATE_UP);
2650+
ifp->if_link_state = LINK_STATE_UP;
2651+
if_link_state_change(ifp);
26562652
} else {
2657-
if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2653+
ifp->if_flags &= ~IFF_RUNNING;
26582654
DPRINTF(sc, "Unable to initialize sockets: %d\n", rc);
26592655
}
26602656
out:
@@ -2665,15 +2661,15 @@ wg_up(struct wg_softc *sc)
26652661
static void
26662662
wg_down(struct wg_softc *sc)
26672663
{
2668-
if_t ifp = sc->sc_ifp;
2664+
struct ifnet *ifp = sc->sc_ifp;
26692665
struct wg_peer *peer;
26702666

26712667
lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
2672-
if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
2668+
if ((ifp->if_flags & IFF_RUNNING) == 0) {
26732669
lockmgr(&sc->sc_lock, LK_RELEASE);
26742670
return;
26752671
}
2676-
if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2672+
ifp->if_flags &= ~IFF_RUNNING;
26772673

26782674
TAILQ_FOREACH(peer, &sc->sc_peers, p_entry) {
26792675
wg_queue_purge(&peer->p_stage_queue);
@@ -2687,7 +2683,8 @@ wg_down(struct wg_softc *sc)
26872683
noise_remote_keypairs_clear(peer->p_remote);
26882684
}
26892685

2690-
if_link_state_change(sc->sc_ifp, LINK_STATE_DOWN);
2686+
ifp->if_link_state = LINK_STATE_DOWN;
2687+
if_link_state_change(ifp);
26912688
wg_socket_uninit(sc);
26922689

26932690
lockmgr(&sc->sc_lock, LK_RELEASE);
@@ -2746,25 +2743,22 @@ wg_clone_create(struct if_clone *ifc __unused, int unit,
27462743

27472744
lockinit(&sc->sc_lock, "wg softc lock", 0, 0);
27482745

2749-
if_setsoftc(ifp, sc);
2750-
if_setcapabilities(ifp, WG_CAPS);
2751-
if_setcapenable(ifp, WG_CAPS);
27522746
if_initname(ifp, wgname, unit);
2753-
2754-
if_setmtu(ifp, DEFAULT_MTU);
2755-
if_setflags(ifp, IFF_NOARP | IFF_MULTICAST);
2756-
if_setinitfn(ifp, wg_init);
2757-
if_setreassignfn(ifp, wg_reassign);
2758-
if_setqflushfn(ifp, wg_qflush);
2759-
if_settransmitfn(ifp, wg_transmit);
2760-
if_setoutputfn(ifp, wg_output);
2761-
if_setioctlfn(ifp, wg_ioctl);
2762-
if_attach(ifp);
2747+
ifp->if_softc = sc;
2748+
ifp->if_capabilities = WG_CAPS;
2749+
ifp->if_capenable = WG_CAPS;
2750+
ifp->if_mtu = DEFAULT_MTU;
2751+
ifp->if_flags = IFF_NOARP | IFF_MULTICAST;
2752+
ifp->if_init = wg_init;
2753+
ifp->if_output = wg_output;
2754+
ifp->if_start = wg_start; /* TODO(ALY) */
2755+
ifp->if_ioctl = wg_ioctl;
2756+
ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
2757+
ifq_set_ready(&ifp->if_snd);
2758+
2759+
if_attach(ifp, NULL);
27632760
bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
2764-
#ifdef INET6
2765-
ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
2766-
ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD;
2767-
#endif
2761+
27682762
lockmgr(&wg_lock, LK_EXCLUSIVE);
27692763
LIST_INSERT_HEAD(&wg_list, sc, sc_entry);
27702764
lockmgr(&wg_lock, LK_RELEASE);
@@ -2792,11 +2786,11 @@ wg_clone_deferred_free(struct noise_local *l)
27922786
static int
27932787
wg_clone_destroy(struct ifnet *ifp)
27942788
{
2795-
struct wg_softc *sc = if_getsoftc(ifp);
2789+
struct wg_softc *sc = ifp->if_softc;
27962790
struct ucred *cred;
27972791

27982792
lockmgr(&wg_lock, LK_EXCLUSIVE);
2799-
if_setsoftc(ifp, NULL);
2793+
ifp->if_softc = NULL;
28002794
lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
28012795
sc->sc_flags |= WGF_DYING;
28022796
cred = sc->sc_ucred;
@@ -2805,8 +2799,9 @@ wg_clone_destroy(struct ifnet *ifp)
28052799
LIST_REMOVE(sc, sc_entry);
28062800
lockmgr(&wg_lock, LK_RELEASE);
28072801

2808-
if_link_state_change(sc->sc_ifp, LINK_STATE_DOWN);
2809-
if_purgeaddrs(sc->sc_ifp);
2802+
ifp->if_link_state = LINK_STATE_DOWN;
2803+
if_link_state_change(ifp);
2804+
if_purgeaddrs_nolink(ifp);
28102805

28112806
lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
28122807
wg_socket_uninit(sc);
@@ -2854,11 +2849,6 @@ wg_clone_destroy(struct ifnet *ifp)
28542849
return (0);
28552850
}
28562851

2857-
static void
2858-
wg_qflush(if_t ifp __unused)
2859-
{
2860-
}
2861-
28622852
/*
28632853
* Privileged information (private-key, preshared-key) are only exported for
28642854
* root by default.
@@ -2872,16 +2862,6 @@ wgc_privileged(struct wg_softc *sc)
28722862
return (priv_check(td, PRIV_NET_WG) == 0);
28732863
}
28742864

2875-
static void
2876-
wg_reassign(if_t ifp, struct vnet *new_vnet __unused,
2877-
char *unused __unused)
2878-
{
2879-
struct wg_softc *sc;
2880-
2881-
sc = if_getsoftc(ifp);
2882-
wg_down(sc);
2883-
}
2884-
28852865
static void
28862866
wg_init(void *xsc)
28872867
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy