freebsd-dev/sys/netlink/netlink_generic.h
Alexander V. Chernikov 7e5bf68495 netlink: add netlink support
Netlinks is a communication protocol currently used in Linux kernel to modify,
 read and subscribe for nearly all networking state. Interfaces, addresses, routes,
 firewall, fibs, vnets, etc are controlled via netlink.
It is async, TLV-based protocol, providing 1-1 and 1-many communications.

The current implementation supports the subset of NETLINK_ROUTE
family. To be more specific, the following is supported:
* Dumps:
 - routes
 - nexthops / nexthop groups
 - interfaces
 - interface addresses
 - neighbors (arp/ndp)
* Notifications:
 - interface arrival/departure
 - interface address arrival/departure
 - route addition/deletion
* Modifications:
 - adding/deleting routes
 - adding/deleting nexthops/nexthops groups
 - adding/deleting neghbors
 - adding/deleting interfaces (basic support only)
* Rtsock interaction
 - route events are bridged both ways

The implementation also supports the NETLINK_GENERIC family framework.

Implementation notes:
Netlink is implemented via loadable/unloadable kernel module,
 not touching many kernel parts.
Each netlink socket uses dedicated taskqueue to support async operations
 that can sleep, such as interface creation. All message processing is
 performed within these taskqueues.

Compatibility:
Most of the Netlink data models specified above maps to FreeBSD concepts
 nicely. Unmodified ip(8) binary correctly works with
interfaces, addresses, routes, nexthops and nexthop groups. Some
software such as net/bird require header-only modifications to compile
and work with FreeBSD netlink.

Reviewed by:	imp
Differential Revision: https://reviews.freebsd.org/D36002
MFC after:	2 months
2022-10-01 14:15:35 +00:00

113 lines
3.8 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Generic netlink message header and attributes
*/
#ifndef _NETLINK_NETLINK_GENERIC_H_
#define _NETLINK_NETLINK_GENERIC_H_
/* Base header for all of the relevant messages */
struct genlmsghdr {
uint8_t cmd; /* CTRL_CMD_ */
uint8_t version; /* ABI version for the cmd */
uint16_t reserved; /* reserved: set to 0 */
};
#define GENL_HDRLEN NL_ITEM_ALIGN(sizeof(struct genlmsghdr))
/* Dynamic family number range, inclusive */
#define GENL_MIN_ID NLMSG_MIN_TYPE
#define GENL_MAX_ID 1023
/* Pre-defined family numbers */
#define GENL_ID_CTRL GENL_MIN_ID
/* Available commands */
enum {
CTRL_CMD_UNSPEC = 0,
CTRL_CMD_NEWFAMILY = 1,
CTRL_CMD_DELFAMILY = 2,
CTRL_CMD_GETFAMILY = 3, /* lists all (or matching) genetlink families */
CTRL_CMD_NEWOPS = 4,
CTRL_CMD_DELOPS = 5,
CTRL_CMD_GETOPS = 6,
CTRL_CMD_NEWMCAST_GRP = 7,
CTRL_CMD_DELMCAST_GRP = 8,
CTRL_CMD_GETMCAST_GRP = 9,
CTRL_CMD_GETPOLICY = 10,
__CTRL_CMD_MAX,
};
#define CTRL_CMD_MAX (__CTRL_CMD_MAX - 1)
/* Generic attributes */
enum {
CTRL_ATTR_UNSPEC,
CTRL_ATTR_FAMILY_ID = 1, /* u16, dynamically-assigned ID */
CTRL_ATTR_FAMILY_NAME = 2, /* string, family name */
CTRL_ATTR_VERSION = 3, /* u32, command version */
CTRL_ATTR_HDRSIZE = 4, /* u32, family header size */
CTRL_ATTR_MAXATTR = 5, /* u32, maximum family attr # */
CTRL_ATTR_OPS = 6, /* nested, available operations */
CTRL_ATTR_MCAST_GROUPS = 7,
CTRL_ATTR_POLICY = 8,
CTRL_ATTR_OP_POLICY = 9,
CTRL_ATTR_OP = 10,
__CTRL_ATTR_MAX,
};
#define CTRL_ATTR_MAX (__CTRL_ATTR_MAX - 1)
#define GENL_NAMSIZ 16 /* max family name length including \0 */
/* CTRL_ATTR_OPS attributes */
enum {
CTRL_ATTR_OP_UNSPEC,
CTRL_ATTR_OP_ID = 1, /* u32, operation # */
CTRL_ATTR_OP_FLAGS = 2, /* u32, flags-based op description */
__CTRL_ATTR_OP_MAX,
};
#define CTRL_ATTR_OP_MAX (__CTRL_ATTR_OP_MAX - 1)
/* CTRL_ATTR_OP_FLAGS values */
#define GENL_ADMIN_PERM 0x0001 /* Requires elevated permissions */
#define GENL_CMD_CAP_DO 0x0002 /* Operation is a modification request */
#define GENL_CMD_CAP_DUMP 0x0004 /* Operation is a get/dump request */
#define GENL_CMD_CAP_HASPOL 0x0008 /* Operation has a validation policy */
#define GENL_UNS_ADMIN_PERM 0x0010
/* CTRL_ATTR_MCAST_GROUPS attributes */
enum {
CTRL_ATTR_MCAST_GRP_UNSPEC,
CTRL_ATTR_MCAST_GRP_NAME, /* string, group name */
CTRL_ATTR_MCAST_GRP_ID, /* u32, dynamically-assigned group id */
__CTRL_ATTR_MCAST_GRP_MAX,
};
#define CTRL_ATTR_MCAST_GRP_MAX (CTRL_ATTR_MCAST_GRP_MAX - 1)
#endif