Commit Graph

59 Commits

Author SHA1 Message Date
Kristof Provost
fa554de774 netlink: reduce default log levels
Reduce the default log level for netlink to LOG_INFO. This removes a
number of messages such as

> [nl_iface] dump_sa: unsupported family: 0, skipping
or
> [nl_iface] get_operstate_ether: error calling SIOCGIFMEDIA on vlan0: 22

that are useful for debugging, but not for most users.

Reviewed by:	melifaro
Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D40062
2023-05-12 14:32:57 +02:00
Alexander V. Chernikov
3c851dc19b netlink: provide original interface lladdr in the interface dump.
* Store lladdr in the FreeBSD-specific IFLAF_ORIG_HWADDR attr
* Do not export empty IFLA_ADDRESS for interfaces w/o lladdrs.

MFC after:	2 weeks
2023-05-10 09:57:01 +00:00
Alexander V. Chernikov
30d0fc6f33 netlink: export more IPv6 ifa info
* Fill in IFA_CACHEINFO with prefix lifetime data
* Map IPv6 IN6_IFF_ flags to Netlink IFA_F_ flags
* Store original ia6_flags in the FreeBSD-specific IFAF_FLAGS field

MFC after:	2 weeks
2023-05-10 09:57:01 +00:00
Alexander V. Chernikov
1224878016 netlink: export carp VHID when dumping interface addresses.
MFC after:	2 weeks
2023-05-10 09:57:01 +00:00
Alexander V. Chernikov
6a09a7f1e9 netlink: always zero provided header buffers.
Reported by:	KMSAN
MFC after:	3 days
2023-05-01 14:09:47 +00:00
Alexander V. Chernikov
dc81813497 netlink: properly zero all fields in the ndmsg header.
MFC after:	3 days
2023-04-28 14:04:47 +00:00
Alexander V. Chernikov
9e79038c50 netlink: fix netlink interface operations when netlink is loaded as a module.
This change completes 089104e0e0.

MFC after:	2 weeks
2023-04-28 13:35:58 +00:00
Alexander V. Chernikov
20b6945c7f netlink: fix IPv6 proxy ndp deletion.
* Move LLT_ADDEDPROXY handling into lltable_link_entry() to
 reduct duplication
* Use standard lltable_delete_addr() for entry deletion
* Add (forgotten) call to llt_post_resolved handler after
 adding the entry via netlink.

MFC after:	2 weeks
2023-04-26 13:26:37 +00:00
Alexander V. Chernikov
04349d3094 netlink: remove now-unused rtnl_iface_find_cloner_locked(). 2023-04-25 15:04:11 +00:00
Alexander V. Chernikov
9e81e2c452 netlink: fix powerpc build. 2023-04-25 14:59:04 +00:00
Alexander V. Chernikov
089104e0e0 netlink: add netlink interfaces to if_clone
This change adds netlink create/modify/dump interfaces to the `if_clone.c`.
The previous attempt with storing the logic inside `netlink/route/iface_drivers.c`
 did not quite work, as, for example, dumping interface-specific state
 (like vlan id or vlan parent) required some peeking into the private interfaces.

The new interfaces are added in a compatible way - callers don't have to do anything
unless they are extended with Netlink.

Reviewed by:	kp
Differential Revision: https://reviews.freebsd.org/D39032
MFC after:	1 month
2023-04-25 12:34:46 +00:00
Alexander V. Chernikov
acc65df45a netlink: require proper privileges when adding neighbor.
MFC after:	3 days
2023-04-25 12:28:22 +00:00
Alexander V. Chernikov
896e22fbc6 netlink: fix neighbour deleting for IPv6.
MFC after:	2 weeks
2023-04-25 12:27:02 +00:00
Alexander V. Chernikov
b32cf15d86 netlink: add support for dumping kernel nexthops.
MFC after:	2 weeks
2023-04-25 11:12:18 +00:00
Alexander V. Chernikov
a2728a9a5b netlink: allow creation of temporary lle entries.
MFC after:	2 weeks
2023-04-25 11:08:47 +00:00
Alexander V. Chernikov
28abf63277 netlink: sync interface IFLA attributes
MFC after:	2 weeks
2023-04-18 12:34:05 +00:00
Alexander V. Chernikov
9742519b22 netlink: fix operations with link-local routes/gateways.
MFC after:	3 days
2023-04-17 12:04:43 +00:00
Alexander V. Chernikov
cc3793b1c5 netlink: improve source ifa selection algorithm when adding routes.
Use route destination sockaddr when the gateway is eiter AF_LINK or
 has the different family (IPv4 over IPv6). This change ensures
 the nexthop IFA has the same family as the destination.

Reported by:	Dmitriy Smirnov <fox@sage.su>
Tested by:	Dmitriy Smirnov <fox@sage.su>
MFC after:	3 days
2023-04-09 13:33:22 +00:00
Alexander V. Chernikov
0d4038e301 netlink: set prefix-related flags to the created nexthop.
This fixes incorrect flag combinations when adding IPv4/IPv6 host
routes.

MFC after:	3 days
2023-04-09 09:26:12 +00:00
Alexander V. Chernikov
75379ea2e4 netlink: do not print "unknown sa family" warnings at the default debug
level.

MFC after:	2 weeks
2023-04-08 19:40:32 +00:00
Alexander V. Chernikov
39c0036d88 netlink: fix !INET6 warning
Reported by:	Gary Jennejohn <garyj@gmx.de>
MFC after:	2 weeks
2023-04-08 19:39:37 +00:00
Alexander V. Chernikov
c35a43b261 netlink: allow exact-match route lookups via RTM_GETROUTE.
Use already-existing RTM_F_PREFIX rtm_flag to indicate that the
 request assumes exact-prefix lookup instead of the
 longest-prefix-match.

MFC after:	2 weeks
2023-04-02 13:47:10 +00:00
Alexander V. Chernikov
b755f1a009 netlink: Fix adding routes with nexthops on p2p interfaces.
Use full-featured ifa_ifwithroute() to guess route ifa/ifp
 instead of ifa_ifwithnet(). This change makes the route addition
 logic closer to the rt_getifa_fib() used by rtsock.

Reported by:	glebius
Tested by:	glebius
Differential Revision: https://reviews.freebsd.org/D39335
MFC after:	2 weeks
2023-03-30 09:53:50 +00:00
Alexander V. Chernikov
19e43c163c netlink: add netlink KPI to the kernel by default
This change does the following:

Base Netlink KPIs (ability to register the family, parse and/or
 write a Netlink message) are always present in the kernel. Specifically,
* Implementation of genetlink family/group registration/removal,
  some base accessors (netlink_generic_kpi.c, 260 LoC) are compiled in
  unconditionally.
* Basic TLV parser functions (netlink_message_parser.c, 507 LoC) are
  compiled in unconditionally.
* Glue functions (netlink<>rtsock), malloc/core sysctl definitions
 (netlink_glue.c, 259 LoC) are compiled in unconditionally.
* The rest of the KPI _functions_ are defined in the netlink_glue.c,
 but their implementation calls a pointer to either the stub function
 or the actual function, depending on whether the module is loaded or not.

This approach allows to have only 1k LoC out of ~3.7k LoC (current
 sys/netlink implementation) in the kernel, which will not grow further.
It also allows for the generic netlink kernel customers to load
 successfully without requiring Netlink module and operate correctly
 once Netlink module is loaded.

Reviewed by:	imp
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D39269
2023-03-27 13:55:44 +00:00
Alexander V. Chernikov
6dc858d84c netlink: remove forgotten debug message in handle_rtm_getroute().
MFC after:	2 weeks
2023-03-27 10:49:40 +00:00
Alexander V. Chernikov
04f75b9802 netlink: allow netlink sockets in non-vnet jails.
This change allow to open Netlink sockets in the non-vnet jails, even for
 unpriviledged processes.
The security model largely follows the existing one. To be more specific:
* by default, every `NETLINK_ROUTE` command is **NOT** allowed in non-VNET
 jail UNLESS `RTNL_F_ALLOW_NONVNET_JAIL` flag is specified in the command
 handler.
* All notifications are **disabled** for non-vnet jails (requests to
 subscribe for the notifications are ignored). This will change to be more
 fine-grained model once the first netlink provider requiring this gets
 committed.
* Listing interfaces (RTM_GETLINK) is **allowed** w/o limits (**including**
 interfaces w/o any addresses attached to the jail). The value of this is
 questionable, but it follows the existing approach.
* Listing ARP/NDP neighbours is **forbidden**. This is a **change** from the
 current approach - currently we list static ARP/ND entries belonging to the
 addresses attached to the jail.
* Listing interface addresses is **allowed**, but the addresses are filtered
 to match only ones attached to the jail.
* Listing routes is **allowed**, but the routes are filtered to provide only
 host routes matching the addresses attached to the jail.
* By default, every `NETLINK_GENERIC` command is **allowed** in non-VNET jail
 (as sub-families may be unrelated to network at all).
 It is the goal of the family author to implement the restriction if
 necessary.

Differential Revision: https://reviews.freebsd.org/D39206
MFC after:	1 month
2023-03-26 08:44:09 +00:00
Alexander V. Chernikov
a74998f38a netlink: reduce the default debugging levels
Reported by:	kp
MFC after:	2 weeks
2023-03-21 18:55:00 +00:00
Alexander V. Chernikov
055776c84a netlink: use newly-added if_foreach_sleep() to iterate over the interface list.
Summary: MFC after:	2 weeks

Subscribers: imp, glebius

Differential Revision: https://reviews.freebsd.org/D38907
2023-03-06 15:08:08 +00:00
Alexander V. Chernikov
ce9f95bd83 netlink: fix vlan interface creation
MFC after:	2 weeks
2023-03-03 16:02:05 +00:00
Alexander V. Chernikov
e6b4177c47 netlink: add userland name mappings to all NETLINK_ROUTE messages.
MFC after:	1 day
2023-03-01 15:24:45 +00:00
Alexander V. Chernikov
d2deebe21b netlink: fix addition of blackhole/reject routes.
* Make nhop_set_blackhole() set all necessary properties for the
 nexthop
* Make nexthops blackhole/reject based on the rtm_type netlink
 property instead of using rtflags.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-23 17:43:18 +00:00
Alexander V. Chernikov
b9b2184322 netlink: clear IPv6 embedded scope when dumping route gateways.
Reported by:	zarychtam@plan-b.pwste.edu.pl
MFC after:	3 days
2023-02-21 12:27:26 +00:00
Alexander V. Chernikov
c7c3481621 netlink: fix IPv6 route addition with link-local gateway
Currently kernel assumes that IPv6 gateway address is in "embedded"
 form - that is, for the link-local IPv6 addresses, interface index
 is embedded in bytes 2 and 3 of the address.
Fix address embedding in netlink by wrapping nhop_set_gw() in the
 netlink-specific nl_set_nexthop_gw(), which does such embedding
 automatically.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-20 14:29:48 +00:00
Alexander V. Chernikov
1ea58b4135 netlink: remove redundant check.
MFC after:	2 weeks
CID:		1498867
2023-02-18 17:53:05 +00:00
Alexander V. Chernikov
45356a1864 netlink: simplify temporary address allocation in rtnl_handle_getlink().
MFC after:	3 days
2023-02-17 18:16:07 +00:00
Alexander V. Chernikov
6694cb1f57 netlink: fix if_allocdescr() malloc options argument.
The current value was based on the initial revision of D37566 and
 was not updated afterwards.

MFC after:	1 day
2023-02-17 18:00:37 +00:00
Alexander V. Chernikov
f2f7911c55 netlink: validate rtable value in RTM_<NEW|DEL|GET>ROUTE.
Reported by:	Stefan Grundmann <sg2342@googlemail.com>
MFC after:	1 day
2023-02-17 18:00:37 +00:00
Alexander V. Chernikov
86fd0bdba5 netlink: fix interface dump.
The current code missed interface addition when reallocating
 temporary buffer.
Tweak the code to perform the reallocation first and add
 interface afterwards unconditionally.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-16 13:20:45 +00:00
Alexander V. Chernikov
5dd48f7184 netlink: use ifmedia to provide vlan interface operstate.
Netlink customers rely on admin and operational state when
 working with interfaces. The current implementation retuns
 "unknown" operstate for all interface types except IFT_ETHER
 and IFT_LOOP.

This change updates the code to fetch vlan operstate in the same way
 as for the ether interfaces. For the rest of the interface types,
 operstate is now mapped to the admin state.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-15 20:22:47 +00:00
Alexander V. Chernikov
25c2dd2f2c netlink: return optional metadata with the operation result.
Some operations like interface creation may need to return metadata
 - in this case, interface name - back to the caller if the operation
 is successful.
This change implements attaching an `NLMSGERR_ATTR_COOKIE` nla to the
operation reply message via `nlmsg_report_cookie()`.
Additionally, on successful interface creation, interface index and
 interface name are returned in the `IFLA_NEW_IFINDEX` and `IFLA_IFNAME
 TLVs, encapsulated in the `NLMSGERR_ATTR_COOKIE`.

Reviewed By: pauamma
Differential Revision: https://reviews.freebsd.org/D38283
MFC after:	1 week
2023-02-09 15:30:00 +00:00
Alexander V. Chernikov
5a5436eb5d netlink: do not require elevated privileges when reading arp/ndp state.
MFC after:	1 day
2023-02-09 14:12:33 +00:00
Alexander V. Chernikov
3ebccb20d5 netlink: allow path weight manipulations for single-path routes.
Add support for the scenario when user adds/deletes paths for a single
 prefix one-by-one, all with different weights.
This change adds a new FreeBSD-specific RTA attribute, NL_RTA_WEIGHT.
When dumping non-multipath routes, this attribute is added if the
 route weight is not RT_DEFAULT_WEIGHT.
When adding a new route, this attribute is parsed as a relative path
 weight.

MFC after:	2 weeks
2023-01-29 16:04:10 +00:00
Alexander V. Chernikov
95b47ba629 netlink: export nextop group kernel index when dumping multipath route.
MFC after:	2 weeks
2023-01-28 15:39:25 +00:00
Mark Johnston
7a78ae8865 netlink: Zero-initialize writer structures allocated on the stack
The prevailing pattern seems to be to simply initialize all fields to
zero.  Without this, it's possible to trigger a branch on uninitialized
memory, specifically, when testing nw->ignore_limit in
nlmsg_refill_buffer().

Initialize the writer structure in a couple of functions where this is
necessary.

Reported by:	KMSAN
Reviewed by:	melifaro
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38213
2023-01-26 10:46:19 -05:00
Alexander V. Chernikov
c1871a3372 netlink: improve RTM_GETADDR handling.
* Allow filtering by ifa_family & ifa_index.
* Add common RTM_<NEW|DEL|GET>ADDR parser
* Add tests verifying RTM_GETADDR filtering behaviour & output
* Factor out common netlink socket test methods into NetlinkTestTemplate
* Add NLMSG_DONE message handler

Reviewed By: pauamma
Differential Revision: https://reviews.freebsd.org/D37970
2023-01-08 15:06:34 +00:00
Alexander V. Chernikov
13214c601a netlink: fix ifaddr reporting.
Output the proper attributes for IPv4/IPvv6 ifaddrs:
* IFA_ADDRESS contains local address in every case except p2p,
   in that case it contains the peer address
* IFA_LOCAL contains local address. It is always present in IPv4,
   or in IPv6/p2p.
* IFA_BROADCAST contains the network broadcast address (if any)

Reported by:	Adam Wood <aswood@gmail.com>
Tested by:	Adam Wood <aswood@gmail.com>
2023-01-06 19:18:54 +00:00
Alexander V. Chernikov
c9313a0bad netlink: allow netlink to be build in the kernel
Differential Revision: https://reviews.freebsd.org/D37781
2022-12-23 15:24:44 +00:00
Zhenlei Huang
ab591c874b netlink: Use NET_EPOCH_[CALL|WAIT] macros
Reviewed by:	melifaro, kp
Approved by:	kp (mentor)
Differential Revision:	https://reviews.freebsd.org/D37730
2022-12-21 09:40:05 +08:00
Alexander V. Chernikov
80f03e63d6 netlink: improve interface handling
* Separate interface creation from interface modification code
* Support setting some interface attributes (ifdescr, mtu, up/down, promisc)
* Improve interaction with the cloners requiring to parse/write custom
 interface attributes
* Add bitmask-based way of checking if the attribute is present in the
message
* Don't use multipart RTM_GETLINK replies when searching for the
specific interface names
* Use ENODEV instead of ENOENT in case of failed RTM_GETLINK search
* Add python netlink test helpers
* Add some netlink interface tests

Differential Revision: https://reviews.freebsd.org/D37668
2022-12-14 19:52:35 +00: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