Commit Graph

691 Commits

Author SHA1 Message Date
Konstantin Belousov
7170774e2a ifcapnv: cap_bit in ifcap2_nv_bit_names[] is bit, not index
Sponsored by:	Nvidia networking
2023-03-31 02:08:15 +03:00
Alexander V. Chernikov
badcb3fd57 routing: fix panic when adding an interface route to the p2p interface
without and inet/inet6 addresses attached.

MFC after:      3 days
2023-03-29 20:28:24 +00:00
Justin Hibbits
e2427c6917 IfAPI: Add iterator to complement if_foreach()
Summary:
Sometimes an if_foreach() callback can be trivial, or need a lot of
outer context.  In this case a regular `for` loop makes more sense.  To
keep things hidden in the new API, use an opaque `if_iter` structure
that can still be instantiated on the stack.  The current implementation
uses just a single pointer out of the 4 alotted to the opaque context,
and the cleanup does nothing, but may be used in the future.

Reviewed by:	melifaro
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D39138
2023-03-23 09:39:26 -04:00
Alexander V. Chernikov
df2b419a41 ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep.
Subscribers: imp, ae, glebius

Differential Revision: https://reviews.freebsd.org/D38904
2023-03-06 15:08:08 +00:00
Alexander V. Chernikov
66bdbcd544 net: unify mtu update code
Subscribers: imp, ae, glebius

Differential Revision: https://reviews.freebsd.org/D38893
2023-03-06 15:08:08 +00:00
Justin Hibbits
aac2d19d93 IfAPI: Style cleanup
Summary:
Clean up style issues from IfAPI additions.

Casts to (struct ifnet *) made sense when `if_t` was a `void *`, but
since it's a `struct ifnet *` it no longer makes sense.  Fix whitespace
errors, among others.

Reviewed by:	kib, glebius
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38499
2023-02-14 10:21:20 -05:00
Justin Hibbits
a3a76c3d90 IfAPI: Add capabilities2/capenable2 accessors
Summary:
As a stopgap measure add basic accessors for the if_capabilities2 and
if_capenable2 members to further hide the ifnet details.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	glebius, kib
Differential Revision: https://reviews.freebsd.org/D38487
2023-02-14 10:21:20 -05:00
Justin Hibbits
189c3729d8 IfAPI: More accessors
Summary:
Add the following accessors needed by infiniband drivers:
* if_getaddrlen()
* if_setbroadcastaddr()
* if_resolvemulti()

With these accessors, and additional changes on the drivers' side, an
amd64 kernel can be compiled with `struct ifnet` completely hidden.

Reviewed by:	melifaro
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38488
2023-02-14 10:21:19 -05:00
Justin Hibbits
1e6131bad6 IfAPI: Add needed APIs for mbuf support
Summary:
Add 2 new APIs for supporting recent mbuf changes:
* 36e0a362ac added the m_snd_tag_alloc() wrapper around
  if_snd_tag_alloc().  Push this down to the ifnet level.
* 4d7a1361ef adds the m_rcvif_serialize()/m_rcvif_restore() KPIs to
  serialize and restore an ifnet pointer.  Add the necessary wrapper to
  get the index generation for this.

Reviewed By:	jhb
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38340
2023-02-06 12:32:04 -05:00
Justin Hibbits
2eeb808361 IfAPI: Add iterator to loop over all interfaces
Summary:
Sometimes it's useful to iterate over all interfaces in the current
VNET, as the linuxulator does in several places.

Unlike other iterators in the IfAPI this propagates any error received
up to the caller, instead of returning a count.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	glebius, melifaro
Differential Revision: https://reviews.freebsd.org/D38348
2023-02-03 09:38:02 -05:00
Justin Hibbits
d79539e6f2 IfAPI: Add if_altq_is_enabled() interface.
Summary:
The only user of the ALTQ_IS_ENABLED() in a driver checks against the
ifnet queue.  Abstract that all out and present the interface to check
if ALTQ is enabled on the interface.

Sponsored by:	Juniper Networks, Inc.
Reviewed By:	glebius
Differential Revision: https://reviews.freebsd.org/D38204
2023-01-31 15:02:17 -05:00
Justin Hibbits
31cfaf191b IfAPI: Add l2com accessor for firewire.
Summary:
Firewire is the only device driver that accesses the l2com member, all
other accesses are handled within the netstack itself.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	glebius, melifaro
Differential Revision: https://reviews.freebsd.org/D38203
2023-01-31 15:02:17 -05:00
Justin Hibbits
0d2684e15e IfAPI: Add some more accessors
Summary:
* if_setreassignfn for wireguard.
* if_getinputfn() and if_getstartfn() for various drivers.  Use the
  function descriptor typedefs for these and the setters.
* vlantrunk accessor.  This is used by VLAN_CAPABILITIES() used by
  several drivers, as well as directly by mxge(4).
* if_pcp member accessor, used by cxgbe.
* accessors for netmap adapter.

Sponsored by:	Juniper Networks, Inc.
Reviewed By:	glebius
Differential Revision: https://reviews.freebsd.org/D38202
2023-01-31 15:02:17 -05:00
Justin Hibbits
c255d1a401 IfAPI: Add if_llsoftc member accessors for TOEDEV
Summary:
Keep TOEDEV() macro for backwards compatibility, and add a SETTOEDEV()
macro to complement with the new accessors.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	glebius
Differential Revision: https://reviews.freebsd.org/D38199
2023-01-31 15:02:16 -05:00
Justin Hibbits
30af2c131b IfAPI: Add if_get/setmaclabel() and use it.
Summary:
Port the MAC modules to use the IfAPI APIs as part of this.

Sponsored by:	Juniper Networks, Inc.
Reviewed by:	glebius
Differential Revision: https://reviews.freebsd.org/D38197
2023-01-31 15:02:15 -05:00
Justin Hibbits
113af4fd2b IfAPI: Add if_gettype() API and use it for vlan
Sponsored by:	Juniper Networks, Inc.
Reviewed by:	#network, glebius
Differential Revision: https://reviews.freebsd.org/D38198
2023-01-31 15:02:15 -05:00
Justin Hibbits
053a24d12c debugnet: Add ifnet accessor to set debugnet methods
As part of the effort to hide the internals of the ifnet struct, convert
the DEBUGNET_SET() macro to use an accessor instead of directly touching
the methods member.

Reviewed by:	glebius (older version)
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38105
2023-01-24 15:03:35 -05:00
Justin Hibbits
2c2b37ad25 ifnet/API: Move struct ifnet definition to a <net/if_private.h>
Hide the ifnet structure definition, no user serviceable parts inside,
it's a netstack implementation detail.  Include it temporarily in
<net/if_var.h> until all drivers are updated to use the accessors
exclusively.

Reviewed by:	glebius
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38046
2023-01-24 14:36:30 -05:00
Justin Hibbits
fa25dbfd98 ifnet API: Change if_init() to take context argument
Some drivers, like iflib drivers, take a 'context' argument instead of a
ifnet argument, as a single interface may have multiple contexts.
Follow this scheme by passing the context argument down.  Most drivers
will likely pass 'ifp' as the context.

Reviewed by:	glebius
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D38102
2023-01-24 14:36:30 -05:00
Justin Hibbits
d34678395f ifnet/API: Change if_set*bit accessors to clear first
Summary:
A common pattern has been to:

	if (foo)
		caps = IFCAP_FOO;
	ifp->if_capenable &= ~IFCAP_FOO;
	ifp->if_capenable |= caps;

which in the new order of things would be:

	if (foo)
		caps = IF_FOO;
	if_setcapenablebits(ifp, 0, IFCAP_FOO);
	if_setcapenablebits(ifp, caps, 0);

This change streamlines this into:

	if (foo)
		caps = IF_FOO;
	if_setcapenablebits(ifp, caps, IFCAP_FOO);

Reviewed by:	melifaro
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D37993
2023-01-09 16:00:22 -05:00
Justin Hibbits
74abe47e32 ifnet/DrvAPI: Implement if_setoutputfn() accessor
Fixes:		eb1da3e525
Sponsored by:	Juniper Networks, Inc.
2023-01-04 15:15:10 -05:00
Justin Hibbits
eb1da3e525 DrvAPI: Extend driver KPI with more accessors
Summary:
Add the following accessors to hide some more netstack details:
* if_get/setcapabilities2 and *bits analogue
* if_setdname
* if_getxname
* if_transmit - wrapper for call to ifp->if_transmit()
- This required changing the existing if_transmit to
if_transmit_default, since that's its purpose.
* if_getalloctype
* if_getindex
* if_foreach_addr_type - Like if_foreach_lladdr() but for any address
  family type.  Used by some drivers to iterate over all AF_INET
  addresses.
* if_init() - wrapper for ifp->if_init() call
* if_setinputfn
* if_setsndtagallocfn
* if_togglehwassist

Reviewers: #transport, #network, glebius, melifaro

Reviewed by:	#network, melifaro
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D37664
2022-12-20 11:18:50 -05:00
Nick Reilly
bfeef0d32a pf: fix pfi_ifnet leak on interface removal
The detach of the interface and group were leaving pfi_ifnet memory
behind. Check if the kif still has references, and clean it up if it
doesn't

On interface detach, the group deletion was notified first and then a
change notification was sent. This would recreate the group in the kif
layer. Reorder the change to before the delete.

PR:		257218
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D37569
2022-12-14 10:19:01 +01:00
Alexander V. Chernikov
1bcd230f95 netlink: add interface notification on link status / flags change.
* Add link-state change notifications by subscribing to ifnet_link_event.
 In the Linux netlink model, link state is reported in 2 places: first is
 the IFLA_OPERSTATE, which stores state per RFC2863.
 The second is an IFF_LOWER_UP interface flag. As many applications rely
 on the latter, reserve 1 bit from if_flags, named as IFF_NETLINK_1.
 This flag is mapped to IFF_LOWER_UP in the netlink headers. This is done
 to avoid making applications think this flag is actually
 supported / presented in non-netlink outputs.
* Add flag change notifications, by hooking into rt_ifmsg().
 In the netlink model, notification should include the bitmask for the
 change flags. Update rt_ifmsg() to include such bitmask.

Differential Revision: https://reviews.freebsd.org/D37597
2022-12-09 11:20:07 +00:00
Alexander V. Chernikov
984b27d879 net: add if_allocdescr() to permit updating iface description from the kernel
Reviewed by:	kp,zlei
Differential Revision: https://reviews.freebsd.org/D37566
MFC after:	2 weeks
2022-12-01 11:31:36 +00:00
Alexander V. Chernikov
9a7c520a78 ifp: add if_setdescr() / if_freedesrt() methods
Add methods for setting and removing the description from the interface,
 so the external users can manage it without using ioctl API.

MFC after:      2 weeks
2022-09-24 19:42:42 +00:00
Gleb Smirnoff
e18c5816ea domains: use queue(9) SLIST for linked list of domains 2022-08-29 19:15:01 -07:00
Gleb Smirnoff
e7d02be19d protosw: refactor protosw and domain static declaration and load
o Assert that every protosw has pr_attach.  Now this structure is
  only for socket protocols declarations and nothing else.
o Merge struct pr_usrreqs into struct protosw.  This was suggested
  in 1996 by wollman@ (see 7b187005d1), and later reiterated
  in 2006 by rwatson@ (see 6fbb9cf860).
o Make struct domain hold a variable sized array of protosw pointers.
  For most protocols these pointers are initialized statically.
  Those domains that may have loadable protocols have spacers. IPv4
  and IPv6 have 8 spacers each (andre@ dff3237ee5).
o For inetsw and inet6sw leave a comment noting that many protosw
  entries very likely are dead code.
o Refactor pf_proto_[un]register() into protosw_[un]register().
o Isolate pr_*_notsupp() methods into uipc_domain.c

Reviewed by:		melifaro
Differential revision:	https://reviews.freebsd.org/D36232
2022-08-17 11:50:32 -07:00
Alexander V. Chernikov
d8b42ddcac rtsock: subscribe to ifnet eventhandlers instead of direct calls.
Stop treating rtsock as a "special" consumer and use already-provided
 ifaddr arrival/departure notifications.

MFC after:	2 weeks

Test Plan:
```
21:05 [0] m@devel0 route -n monitor

-> ifconfig vtnet0.2 create

got message of size 24 on Tue Aug  9 21:05:44 2022
RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 3, what: arrival

got message of size 168 on Tue Aug  9 21:05:54 2022
RTM_IFINFO: iface status change: len 168, if# 3, link: up, flags:<BROADCAST,RUNNING,SIMPLEX,MULTICAST>

-> ifconfig vtnet0.2 destroy

got message of size 24 on Tue Aug  9 21:05:54 2022
RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 3, what: departure

```

Reviewed By: glebius
Differential Revision: https://reviews.freebsd.org/D36095
MFC after:	2 weeks
2022-08-11 20:36:59 +00:00
Gleb Smirnoff
b8103ca76d netinet: get interface event notifications directly via EVENTHANDLER(9)
The old mechanism of getting them via domains/protocols control input
is a relict from the previous century, when nothing like EVENTHANDLER(9)
existed yet.  Retire PRC_IFDOWN/PRC_IFUP as netinet was the only one
to use them.

Reviewed by:		melifaro
Differential revision:	https://reviews.freebsd.org/D36116
2022-08-11 09:19:36 -07:00
Zhenlei Huang
150486f6a9 Introduce and use the NET_EPOCH_DRAIN_CALLBACKS() macro
Reviewed by:	melifao, kp
Differential Revision:	https://reviews.freebsd.org/D35968
2022-07-29 21:21:10 +02:00
KUROSAWA Takahiro
d6cd20cc5c netinet6: fix ndp proxying
We could insert proxy NDP entries by the ndp command, but the host
with proxy ndp entries had not responded to Neighbor Solicitations.
Change the following points for proxy NDP to work as expected:
* join solicited-node multicast addresses for proxy NDP entries
  in order to receive Neighbor Solicitations.
* look up proxy NDP entries not on the routing table but on the
  link-level address table when receiving Neighbor Solicitations.

Reviewed By: melifaro
Differential Revision: https://reviews.freebsd.org/D35307
MFC after:	2 weeks
2022-05-30 10:53:33 +00:00
Konstantin Belousov
051e7d78b0 Kernel-side infrastructure to implement nvlist-based set/get ifcaps
Reviewed by:	hselasky, jhb, kp (previous version)
Sponsored by:	NVIDIA Networking
MFC after:	3 weeks
Differential revision:	https://reviews.freebsd.org/D32551
2022-05-24 23:59:32 +03:00
Kristof Provost
868bf82153 if: avoid interface destroy race
When we destroy an interface while the jail containing it is being
destroyed we risk seeing a race between if_vmove() and the destruction
code, which results in us trying to move a destroyed interface.

Protect against this by using the ifnet_detach_sxlock to also covert
if_vmove() (and not just detach).

PR:		262829
MFC after:	3 weeks
Differential Revision:	https://reviews.freebsd.org/D34704
2022-05-06 13:55:08 +02:00
Gleb Smirnoff
4d7a1361ef ifnet/mbuf: provide KPI to serialize/restore m->m_pkthdr.rcvif
Supplement ifindex table with generation count and use it to
serialize & restore an ifnet pointer.

Reviewed by:		kp
Differential revision:	https://reviews.freebsd.org/D33266
Fun note:		git show e6abef0918

(cherry picked from commit e1882428dc)
2022-05-05 14:38:07 -04:00
Gleb Smirnoff
80e60e236d ifnet: make if_index global
Now that ifindex is static to if.c we can unvirtualize it.  For lifetime
of an ifnet its index never changes.  To avoid leaking foreign interfaces
the net.link.generic.system.ifcount sysctl and the ifnet_byindex() KPI
filter their returned value on curvnet.  Since if_vmove() no longer
changes the if_index, inline ifindex_alloc() and ifindex_free() into
if_alloc() and if_free() respectively.

API wise the only change is that now minimum interface index can be
greater than 1.  The holes in interface indexes were always allowed.

Reviewed by:		kp
Differential revision:	https://reviews.freebsd.org/D33672

(cherry picked from commit 91f44749c6)
2022-05-05 14:38:07 -04:00
Marko Zec
d461deeaa4 VNET: Revert "ifnet: make if_index global"
This reverts commit 91f44749c6.

Devirtualization of V_if_index and V_ifindex_table was rushed into
the tree lacking proper context, discussion, and declaration of intent,
so I'm backing it out as harmful to VNET on the following grounds:

1) The change repurposed the decades-old and stable if_index KBI for
new, unclear goals which were omitted from the commit note.

2) The change opened up a new resource exhaustion vector where any vnet
could starve the system of ifnet indices, including vnet0.

3) To circumvent the newly introduced problem of separating ifnets
belonging to different vnets from the globalized ifindex_table, the
author introduced sysctl_ifcount() which does a linear traversal over
the (potentially huge) global ifnet list just to return a simple upper
bound on existing ifnet indices.

4) The change effectively led to nonuniform ifnet index allocation
among vnets.

5) The commit note clearly stated that the patch changed the implicit
if_index ABI contract where ifnet indices were assumed to be starting
from one.  The commit note also included a correct observation that
holes in interface indices were always allowed, but failed to declare
that the userland-observable ifindex tables could now include huge
empty spans even under modest operating conditions.

6) The author had an earlier proposal in the works which did not
affect per-vnet ifnet lists (D33265) but which he abandoned without
providing the rationale behind his decision to do so, at the expense
of sacrificing the vnet isolation contract and if_index ABI / KBI.

Furthermore, the author agreed to back out his changes himself and
to follow up with a proposal for a less intrusive alternative, but
later silently declined to act.  Therefore, I decided to resolve the
status-quo by backing this out myself.  This in no way precludes a
future proposal aiming to mitigate ifnet-removal related system
crashes or panics to be accepted, provided it would not unnecessarily
compromise the goal of as strict as possible isolation between vnets.

Obtained from: github.com/glebius/FreeBSD/commits/backout-ifindex
2022-05-03 19:27:57 +02:00
Marko Zec
6c741ffbfa Revert "mbuf: do not restore dying interfaces"
This reverts commit 703e533da5.

Revert "ifnet/mbuf: provide KPI to serialize/restore m->m_pkthdr.rcvif"

This reverts commit e1882428dc.

Obtained from: github.com/glebius/FreeBSD/commits/backout-ifindex
2022-05-03 19:11:40 +02:00
Gordon Bergling
1a15a383a6 net: Fix a typo in a source code comment
- s/peform/perform/

MFC after:	3 days
2022-04-09 11:37:57 +02:00
Gleb Smirnoff
964b8f8b99 ifnet: garbage collect unused function ifaddr_byindex().
Last use was removed in 5adea417d4.
2022-01-28 09:51:52 -08:00
Gleb Smirnoff
e1882428dc ifnet/mbuf: provide KPI to serialize/restore m->m_pkthdr.rcvif
Supplement ifindex table with generation count and use it to
serialize & restore an ifnet pointer.

Reviewed by:		kp
Differential revision:	https://reviews.freebsd.org/D33266
Fun note:		git show e6abef0918
2022-01-26 21:58:50 -08:00
Gleb Smirnoff
91f44749c6 ifnet: make if_index global
Now that ifindex is static to if.c we can unvirtualize it.  For lifetime
of an ifnet its index never changes.  To avoid leaking foreign interfaces
the net.link.generic.system.ifcount sysctl and the ifnet_byindex() KPI
filter their returned value on curvnet.  Since if_vmove() no longer
changes the if_index, inline ifindex_alloc() and ifindex_free() into
if_alloc() and if_free() respectively.

API wise the only change is that now minimum interface index can be
greater than 1.  The holes in interface indexes were always allowed.

Reviewed by:		kp
Differential revision:	https://reviews.freebsd.org/D33672
2022-01-26 21:58:44 -08:00
Gleb Smirnoff
54712fc423 if_vmove: improve restoration in cloner's ifgroup membership
* Do a single call into if_clone.c instead of two.  The cloner
  can't disappear since the interface sits on its list.
* Make restoration smarter - check that cloner with same name
  exists in the new vnet.

Differential revision:	https://reviews.freebsd.org/D33941
2022-01-24 21:06:59 -08:00
Ryan Stone
5adea417d4 Fix ifa refcount leak in ifa_ifwithnet()
In 4f6c66cc9c, ifa_ifwithnet() was changed to no longer
ifa_ref() the returned ifaddr, and instead the caller was required
to stay in the net_epoch for as long as they wanted the ifaddr
to remain valid.  However, this missed the case where an AF_LINK
lookup would call ifaddr_byindex(), which still does ifa_ref()
the ifaddr.  This would cause a refcount leak.

Fix this by inlining the relevant parts of ifaddr_byindex() here,
with the ifa_ref() call removed.  This also avoids an unnecessary
entry and exit from the net_epoch for this case.

I've audited all in-tree consumers of ifa_ifwithnet() that could
possibly perform an AF_LINK lookup and confirmed that none of them
will expect the ifaddr to have a reference that they need to
release.

MFC after: 2 months
Sponsored by: Dell Inc
Differential Revision:	https://reviews.freebsd.org/D28705
Reviewed by: melifaro
2022-01-06 15:04:24 -05:00
Mateusz Guzik
e735fa3212 net/if.c: plug set-but-not-unused vars
Sponsored by:	Rubicon Communications, LLC ("Netgate")
2021-12-09 20:39:40 +00:00
Gleb Smirnoff
7e0bba4d80 ifnet: make V_if_index static to if.c
This requires moving net.link.generic sysctl declaration from if_mib.c
to if.c.  Ideally if_mib.c needs just to be merged to if.c, but they
have different license texts.

Differential revision:	https://reviews.freebsd.org/D33263
2021-12-06 09:32:31 -08:00
Gleb Smirnoff
d74b7baeb0 ifnet_byindex() actually requires network epoch
Sweep over potentially unsafe calls to ifnet_byindex() and wrap them
in epoch.  Most of the code touched remains unsafe, as the returned
pointer is being used after epoch exit.  Mark that with a comment.

Validate the index argument inside the function, reducing argument
validation requirement from the callers and making V_if_index
private to if.c.

Reviewed by:		melifaro
Differential revision:	https://reviews.freebsd.org/D33263
2021-12-06 09:32:31 -08:00
Gleb Smirnoff
7b40b00fad ifnet: merge ifindex_alloc(), ifnet_setbyindex(), if_grow() and call magic
Now it is possible to just merge all this complexity into single
linear function.  Note that IFNET_WLOCK() is a sleepable lock, so
we can M_WAITOK and epoch_wait_preempt().

Reviewed by:		melifaro, bz, kp
Differential revision:	https://reviews.freebsd.org/D33262
2021-12-06 09:32:31 -08:00
Gleb Smirnoff
6ff4cac2ee ifnet: initial if_grow() shall always succeed
So let's just call malloc() directly.  This also avoids hidden
doubling of default V_if_indexlim.

Reviewed by:		melifaro, bz, kp
Differential revision:	https://reviews.freebsd.org/D33261
2021-12-06 09:32:31 -08:00
Gleb Smirnoff
450394af27 ifnet: use ck_pr(3) store & load setting ifnet pointer in ifindex
The lockless access to the array is protected by the network epoch.

Reviewed by:		bz, kp
Differential revision:	https://reviews.freebsd.org/D33260
2021-12-06 09:32:30 -08:00