大网数据广告活动
服务器运维问答

nftables set of couples { IP/MAC address }

· 52CE 我爱测

Is it possible to do something like this : set authorized { type ipv4_addr ether_addr flags constant elements = { { ipaddr: 192.168.1.xx, etheraddr: xx:xx:xx:xx:xx:xx }, { ipaddr: 192.168.1.xx, etheraddr: xx:xx:xx:xx:xx:xx }, { i…

问题原文

原帖链接 · John Doe · CC BY-SA 4.0

Is it possible to do something like this :

    set authorized {
    type ipv4_addr ether_addr
    flags constant

    elements = {
        { ipaddr: 192.168.1.xx, etheraddr: xx:xx:xx:xx:xx:xx },
        { ipaddr: 192.168.1.xx, etheraddr: xx:xx:xx:xx:xx:xx },
        { ipaddr: 192.168.1.xx, etheraddr: xx:xx:xx:xx:xx:xx },
    }
}

This returns a syntax error. Is there a valid syntax to do it ? Nothing show on nftables website set page.

Thank you

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

OP's set definition has multiple syntax errors.

A set of elements where each element is a combination of two base types, IPv4 + Ethernet, is needed: it's not a set of simple elements, but a set of concatenations:

Concatenations

Since Linux kernel 4.1, nftables supports concatenations.

This new feature allows you to put two or more selectors together to perform very fast lookups in sets, maps, vmaps and meters.

Currently, in all of nft's man page, a single occurrence of the word concatenations appears for a non-trivial use but it's not defined there. So while nftables' wiki sometimes includes outdated information, it still includes useful documentation to complement the manual.

The . character is used to concatenate multiple base values. Also, a set element doesn't use an additional pair of { } nor uses the : character which is for maps.

This could be defined and used like below, reusing OP's information with concrete values. An host will allow only incoming traffic from matching pairs of source IPv4 address plus Ethernet source MAC address, for example as a way to enforce such association between MAC and IP address:

table ip t {
    set authorized {
        type ipv4_addr . ether_addr
        flags constant
        elements = {
            192.168.1.1 . 02:00:00:00:00:01,
            192.168.1.2 . 02:00:00:00:00:02,
            192.168.1.3 . 02:00:00:00:00:03
        }
    }

    chain input { type filter hook input priority 0; policy drop;
        ip saddr . ether saddr @authorized accept
    }
}

Note: newer versions of nftables and kernel allow an easier syntax. Instead of:

type ipv4_addr . ether_addr

where figuring out ipv4_addr or ether_addr requires to find it in some additional documentation, one can use instead:

typeof ip saddr . ether saddr

reusing the same keywords used in rule syntax.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-03-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。