> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man4/genetlink.4.md).

# genetlink.4

`genetlink` — 通用 Netlink

## 名称

`genetlink`

## 概要

`#include <netlink/netlink.h>`

`#include <netlink/netlink_generic.h>`

`Ft int Fn socket AF_NETLINK SOCK_DGRAM NETLINK_GENERIC`

## 描述

`NETLINK_GENERIC` 是一个“容器”族，用于动态注册属于各子系统的其他族。这些子系统在注册时提供一个字符串族名，并接收一个动态分配的族 ID。然后，应用程序使用分配的族标识符通过 netlink 访问该子系统提供的功能。有将字符串族名解析为族标识符的标准方法。这些族提供的通知组也有类似的机制。

所有通用 netlink 族共享一个公共头：

```sh
struct genlmsghdr {
	uint8_t		cmd;		/* 族内的命令 */
	uint8_t		version;	/* cmd 的 ABI 版本 */
	uint16_t	reserved;	/* 保留：设置为 0 */
};
```

族 ID 编码在基础 netlink 头的 `nlmsg_type` 中。`cmd` 字段是族内的命令标识符。`version` 字段是命令版本。

## 方法

通用 Netlink 框架提供基础族 `GENL_ID_CTRL`（"nlctrl"），具有固定的族 ID。此族用于列出所有已注册族的详细信息。

框架支持以下消息：

### CTRL\_CMD\_GETFAMILY

根据 `NLM_F_DUMP` 标志获取单个族或所有已注册的族。每个族以 `CTRL_CMD_NEWFAMILY` 消息的形式报告。内核识别以下过滤器：

CTRL\_ATTR\_FAMILY\_ID (uint16\_t) 内核分配的当前族 ID CTRL\_ATTR\_FAMILY\_NAME (string) 族名

### TLV

GENL\_ADMIN\_PERM 需要提升的权限 GENL\_CMD\_CAP\_DO 操作是修改请求 GENL\_CMD\_CAP\_DUMP 操作是获取/转储请求

**`CTRL_ATTR_OP_ID`** 操作（消息）编号。

**`CTRL_ATTR_OP_FLAGS`** 操作标志。支持以下标志：

**`CTRL_ATTR_MCAST_GRP_ID`** 可在 `NETLINK_ADD_MEMBERSHIP` setsockopt(2) 中使用的组 ID。

**`CTRL_ATTR_MCAST_GRP_NAME`** (string) 人类可读的组名。

**`CTRL_ATTR_FAMILY_ID`** (uint16\_t) 动态分配的族标识符。

**`CTRL_ATTR_FAMILY_NAME`** (string) 族名。

**`CTRL_ATTR_HDRSIZE`** (uint32\_t) 族强制头大小（通常为 0）。

**`CTRL_ATTR_MAXATTR`** (uint32\_t) 该族有效的最大属性编号。

**`CTRL_ATTR_OPS`** (nested) 该族支持的操作列表。该属性由嵌套 TLV 列表组成，属性值从 0 开始单调递增。每个 TLV 中存在以下属性：

**`CTRL_ATTR_MCAST_GROUPS`** (nested) 该族支持的通知组列表。该属性由嵌套 TLV 列表组成，属性值从 0 开始单调递增。每个 TLV 中存在以下属性：

### 组

定义了以下组：

"notify" 在族注册/移除时通知。

## 参见

[snl(3)](/man/misc/snl.3.md), [netlink(4)](/man/man4/netlink.4.md)

## 历史

`NETLINK_GENERIC` 协议族出现于 FreeBSD 13.2。

## 作者

netlink 由 Alexander Chernikov <melifaro@FreeBSD.org> 实现。它源自 Ng Peng Nam Sean 在 2021 年 Google Summer of Code 中的项目。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man4/genetlink.4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
