打印

QNX网络之二:io-sock

[复制链接]
406|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
QNX(7.1SDP-October 26, 2022,doc)网络部分分析
1 架构1.1 io-stack整体架构
QNX Neutrino RTOS的高性能网络协议栈被称为io-stack,其架构如上图所示。
底层是驱动程序,提供向硬件传递数据和从硬件接收数据的机制。驱动程序连接到一个多线程数据包处理层,该层将它们传递给适当的多线程IP和上层协议处理组件(TCP和UDP)。


使用特权

评论回复

相关帖子

沙发
keer_zu|  楼主 | 2023-6-7 15:44 | 只看该作者
在QNX Neutrino中,资源管理器在协议栈顶部形成一个层。资源管理器充当协议栈和用户应用程序之间的消息传递中介。它提供了一种标准化类型的接口,包括open()、read()、write()和ioctl(),使用消息流与网络应用程序通信。用户编写的网络应用程序与套接字库链接,套接字库将协议栈公开的消息传递接口转换为标准BSD风格的套接字层API。
除了套接字级API之外,协议栈中还有其他编程接口,用于进行过滤。
在驱动程序层,有用于以太网流量的接口(所有以太网驱动程序都使用这个),以及用于无线驱动程序的802.11管理帧的协议栈接口,可以使用io-sock选项-d将驱动程序(构建为动态链接的DLL,前缀为devs-*)加载到协议栈中。

使用特权

评论回复
板凳
keer_zu|  楼主 | 2023-6-7 15:55 | 只看该作者
协议栈还包括用于包过滤的钩子。过滤支持的主要接口有:
  • Berkeley Packet Filter (BPF) interface——A socket-level interface that lets you read and write, but not modify or block, packets, and that you access by using a socket interface at the application layer (see http://en.wikipedia.org/wiki/Berkeley_Packet_Filter). This is the interface of choice for basic, raw packet interception and transmission and gives applications outside of the stack process domain access to raw data streams.
  • Packet Filter (PF) interface——A read/write/modify/block interface that gives complete control over which packets are received by or transmitted from the upper layers.


使用特权

评论回复
地板
keer_zu|  楼主 | 2023-6-7 15:57 | 只看该作者
1.2 io-stack一些特性1.2.1 多线程模式
The default mode of operation for io-sock is to start with multiple threads for:
  • the stack
  • each CPU
  • resource managers on each CPU
In addition, each driver creates one (e.g., devs-re.so) or several (e.g., devs-ixgbe.so) threads per CPU.
io-sock的线程模型结合了FreeBSD内核和QNX资源管理器(If io-sock runs out of threads, it blocks. There is a limit of 700 resource manager threads and 1000 total threads.)
The io-sock 管理器允许可配置从硬件收报的线程的优先级,默认优先级都是21,Driver-specific thread priorities are not supported.


使用特权

评论回复
5
keer_zu|  楼主 | 2023-6-7 15:59 | 只看该作者
1.2.2 创建多个协议栈使用
io-sock networking 协议栈允许去创建多个协议栈实例(比如提供给外部用户一个协议栈,内部用户一个协议栈)。
创建一个协议栈使用prefix=path,这样协议栈创建的资源都会在此path为基础上创建,比如,io-sock -o prefix=/alt
This io-sock command creates the following resources:
  • /alt/dev/socket
  • /alt/dev/io-sock
  • /alt/dev/pf
  • /alt/dev/bpf
去挂载使用这个协议栈使用如下命令:
mount -T io-sock -o prefix=/alt devs-em.so
在io sock中,默认情况下启用即插即用。使用即插即用,如果加载了支持接口的驱动程序,则接口在插入总线后会自动连接。

使用特权

评论回复
6
keer_zu|  楼主 | 2023-6-7 16:00 | 只看该作者
在某些情况下,要使用多个相同类型的NIC运行协议栈的多个实例,应禁用即插即用。然后,可以识别每个接口(例如,使用总线:设备:功能编号)并指定它应该连接到的协议栈。
根据设备类型和使用方式,禁用即插即用可能是必要的,也可能不是必要的。
PCI:协议栈通过PCI服务器连接到PCI设备。创建后,连接是独占的,很少插入和删除设备。启用“即插即用”后,可以使用两个NIC启动第一个协议栈,并在启动第二个协议栈之前删除一个NIC。然后可以在启动第二个协议栈时连接该NIC。但是,如果插入了新设备,两个协议栈将竞相连接到该设备。如果要控制哪个协议栈连接到设备,则必须禁用即插即用。
USB:USB设备也以独占方式连接到协议栈,可以使用针对带有USB NIC的多个PCI NIC描述的umount方法。然而,由于USB设备的插入和移除频率更高,连接竞争也更频繁。如果要避免这种情况,必须禁用即插即用。
OFW(开放式固件总线):OFW设备不以独占方式连接。必须禁用即插即用,才能与具有两个或更多协议栈的同类型NIC一起工作。如果未禁用即插即用,协议栈将覆盖彼此的值。这种覆盖可能会导致io sock实例崩溃。
命令举例:io-sock -o npnp_pci 禁用pci设备即插即用。如果io-stack已经先启动,执行sysctl qnx.pnp.pci=0来关闭。
整体命令如下:mount -T io-sock -o prefix=/alt qnxdev=pci0:0:25:0 devs-em.so

使用特权

评论回复
7
keer_zu|  楼主 | 2023-6-7 16:02 | 只看该作者
2 驱动相关
是否载入驱动都可以开启io-stack。

使用特权

评论回复
评论
keer_zu 2023-6-7 16:27 回复TA
https://zhuanlan.zhihu.com/p/599714335 
8
keer_zu|  楼主 | 2023-6-7 16:03 | 只看该作者
2.1 启动时不载入驱动
直接执行io-stack命令,这个命令默认会创建如下资源:
  • /dev/socket
  • /dev/io-sock
  • /dev/pf
  • /dev/bpf

使用特权

评论回复
9
keer_zu|  楼主 | 2023-6-7 16:03 | 只看该作者
2.2 启动时候载入驱动
可以在启动io-sock时加载一个或多个驱动程序。因为所有io-sock驱动程序默认为即插即用,所以可以在没有接口的情况下加载它们。
例如io-sock -d devs-em.so,对于64位系统,没有资源限制,可以同时指定多个驱动,且不需要加devs-前缀和.so后缀,比如,io-sock -d em -d re -d axe -d axge -d cdce

使用特权

评论回复
10
keer_zu|  楼主 | 2023-6-7 19:21 | 只看该作者
2.3 开启io-stack后载入驱动
比如 mount -T io-sock em(此命令是假设devs-em.so 驱动路径在LD_LIBRARY_PATH),如果没在环境设置中,可以执行mount -T io-sock /lib/dll/devs-em.so(使用绝对路径方式)
以上命令执行会创建如下资源
  • /dev/io-sock/devs-em.so
  • /dev/io-sock/devs-em.so/em0
  • /dev/io-sock/devs-em.so/em1
  • ...
  • /dev/io-sock/devs-em.so/emn
因此,卸载时候需要反执行,
  • umount /dev/io-sock/devs-em.so/em0
  • umount /dev/io-sock/devs-em.so/em1
  • umount /dev/io-sock/devs-em.so/emn
  • ...
  • umount /dev/io-sock/devs-em.so

使用特权

评论回复
11
keer_zu|  楼主 | 2023-6-7 19:22 | 只看该作者
2.4 一些命令
ls /dev/io-sock/(find /dev/io-sock/)可以显示载入的驱动。
cat /dev/io-sock/devs-em.so/em0 查看驱动信息
或者devinfo -v


使用特权

评论回复
12
keer_zu|  楼主 | 2023-6-7 19:22 | 只看该作者
3 应用与协议栈交互
使用libsocket库,和Unix的一样的,从freeBSD移植来的,不多介绍。

使用特权

评论回复
13
keer_zu|  楼主 | 2023-6-7 19:23 | 只看该作者
4 网络安全相关
QNX使用的很多系统软件都是来自BSD的,其中涉及网络安全的是BPF和PF,其中BPF是作为观察者身份参与(适用于IDS),PF是可以参与修改数据包,执行阻塞动作的。
4.1 BPF
BPF接口,可以提供给编写应用来读取和发送数据包,提供api用于编码。

使用特权

评论回复
14
keer_zu|  楼主 | 2023-6-7 19:25 | 只看该作者
4.2 PF
包过滤,可以作为一款主机FW应用,提供到状态过滤等级,但是无法做自定义应用协议的DPI检测,可能需要自己开发(对套接字/dev/pf进行读取收数据包解析,再发送命令阻塞或者放行)。
PF最早来源于OpenBSD,之后从freeBSD5.3以后合入到freeBSD作为基础软件之一。PF是一个完整的、功能齐全的防火墙,可选地支持ALTQ(流量整形),它提供服务质量(QoS)。
使能/关闭PF,# pfctl –e # pfctl –d;但是在QNX中因为是io-stack调用PF,所以不需要任何设置。

使用特权

评论回复
15
keer_zu|  楼主 | 2023-6-7 19:26 | 只看该作者
4.2.1 pfctl
使用pfctl命令行工具可以配置PF的开/关、规则,其中规则是存放在/etc/pf.conf中,具体规则编写可参考pf.conf.5
The pf.conf file has multiple parts:
  • Macros(自定义变量): user-defined variables that can hold IP addresses, interface names, etc.
  • Tables(存放ip地址的结构): a structure used to hold lists of IP addresses
  • Options: various options to control how PF works.
  • Filter Rules: allows the selective filtering or blocking of packets as they pass through any of the interfaces
Pfctl命令的使用手册参考pfctl(8) man。
# pfctl -f /etc/pf.conf# Load the pf.conf file
# pfctl -nf /etc/pf.conf# Parse the file, but don't load it
# pfctl -sr# Show the current ruleset
# pfctl -ss# Show the current state table
# pfctl -si# Show filter stats and counters
# pfctl -sa# Show everything it can show
4.2.1.1 pf.conf
List: 列表允许在规则中指定多个类似条件。例如,多个协议、端口号、地址等。因此,可以通过在列表中指定IP地址来编写一条规则,而不是为需要阻止的每个IP地址编写一条过滤规则。通过在{}括号内指定项目来定义列表。当pfctl(8)在加载规则集期间遇到列表时,它会创建多个规则,每个规则对应列表中的每个项目。例如:
block out on fxp0 from { 192.168.0.1, 10.5.32.6 } to any
实际是创建2个规则
block out on fxp0 from 192.168.0.1 to any
block out on fxp0 from 10.5.32.6 to any
Macros:宏是用户定义的变量,可以保存IP地址、端口号、接口名称等。宏可以降低PF规则集的复杂性,并使维护更容易。宏名称必须以字母开头,并且可以包含字母、数字和下划线。宏名称不能是保留字,例如pass、out或queue。
ext_if = "fxp0"
block in on $ext_if from any to any
Macros can also expand to lists, such as:
friends = "{ 192.168.1.1, 10.0.2.5, 192.168.43.53 }"
host1 = "192.168.1.1"
host2 = "192.168.1.2"
all_hosts = "{" $host1 $host2 "}"
Tables: 表用于保存一组IPv4/IPv6地址。与List相比,对表的查找速度非常快,占用的内存和处理器时间更少。由于这个原因,一个表非常适合保存一大组地址,因为一个保存50000个地址的表的查找时间只比一个保存50个地址的查找时间略长。表可以以下列方式使用:
  • Source and/or destination address in rules
  • Translation and redirection addresses nat-to and rdr-to rule options, respectively
  • Destination address in route-to, reply-to and dup-to rule options
表是使用pf.conf中的table指令创建的。可以为每个表指定以下属性:
  • const - 一旦创建了表,就不能更改表的内容。如果未指定此属性,则可以随时使用pfctl从表中添加或删除地址,即使在securelevel为2或更高时也是如此。.
  • persist - 即使没有规则引用该表,内核也会将其保存在内存中。如果没有此属性,内核将在刷新引用该表的最后一个规则时自动删除该表.
Example:
table <goodguys> { 192.0.2.0/24 }
table <rfc1918> const { 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8 }
table <spammers> persist
block in on fxp0 from { <rfc1918>, <spammers> } to any
pass in on fxp0 from <goodguys> to any
Addresses can also be specified using the negation (or "not") modifier, such as:
The goodguys表将会匹配所有属于192.0.2.0/24地址除了 192.0.2.5.
table <goodguys> { 192.0.2.0/24, !192.0.2.5 }
表也可以从文件导入:比如, /etc/spammers文件包含一系列ip地址或者CIDR地址快,一行一个。
table <spammers> persist file "/etc/spammers"
block in on fxp0 from <spammers> to any
表也可以使pfctl命令操作,具体可看mac手册。
特殊地址:One limitation when specifying addresses is that 0.0.0.0/0 and 0/0 will not work in tables. The alternative is to hard code that address or use a macro.
查询匹配:针对表的地址查找将返回最窄匹配的条目。这允许创建表,例如:
table <goodguys> { 172.16.0.0/16, !172.16.1.0/24, 172.16.1.100 }
block in on dc0
pass in on dc0 from <goodguys>
Any packet coming in through dc0 will have its source address matched against the table <goodguys>:
172.16.50.5 - narrowest match is 172.16.0.0/16; packet matches the table and will be passed
172.16.1.25 - narrowest match is !172.16.1.0/24; packet matches an entry in the table but that entry is negated (uses the "!" modifier); packet does not match the table and will be blocked
172.16.1.100 - exactly matches 172.16.1.100; packet matches the table and will be passed
10.1.4.55 - does not match the table and will be blocked
4.2.1.2 Packet Filter
数据包过滤是在数据包通过网络接口时选择性地传递或阻止数据包。pf(4)在检查数据包时使用的标准基于第3层(IPv4和IPv6)和第4层(TCP、UDP、ICMP和ICMPv6)报头。最常用的标准是源和目标地址、源和目标端口以及协议。
过滤规则指定数据包必须匹配的条件,以及在找到匹配时所采取的结果操作(阻止或通过),过滤规则按照从前到后的顺序计算。除非数据包与包含快速关键字的规则匹配,否则在执行最终操作之前,将根据所有过滤规则对数据包进行评估。最后要匹配的规则是“获胜者”,并将决定对数据包采取什么行动。在过滤规则集的开头有一个隐式传递all,这意味着如果数据包与任何筛选规则都不匹配,则生成的操作将被传递。
规则语法:
筛选器规则的通用高度简化语法为:
action [direction] [log] [quick] [on interface] [af]
[proto protocol] [from src_addr [port src_port]]
[to dst_addr [port dst_port]] [flags tcp_flags] [state]
4.2.2 编码
包过滤具体实现在内核中进行,生成的伪设备/dev/pf允许用户进程通过ioctl接口控制数据包过滤器的行为。有一些命令可以启用和禁用筛选器、加载规则集、添加和删除单个规则或状态表条目以及检索统计信息。pfctl涵盖了最常用的函数,像加载包含多个ioctl调用的规则集这样的操作需要一个所谓的ticket,这可以防止多个并发操作的发生。
ioctl参数结构中引用数据包结构数据(如地址和端口)的字段通常按网络字节顺序排列。规则和地址表包含在锚点中。在为ioctl(2)请求提供服务时,如果参数结构的锚点字段为空,内核将在操作中使用默认锚点(即主规则集)。锚点由名称指定,可以嵌套,组件之间用“/”字符分隔,类似于文件系统层次结构的布局。锚路径的最后一个组件是将在其下执行操作的锚。终止空字节后有字符的锚点名称被视为无效;如果在ioctl中使用,将返回EINVAL。
pf 支持如下的ioctl命令, 在 <net/pfvar.h>可查:
DIOCSTART Start the packet filter.
DIOCSTOP Stop the packet filter.
DIOCADDRULE struct pfioc_rule *pr
struct pfioc_rule {
u_int32_taction;
u_int32_tticket;
u_int32_tnr;
charanchor[PATH_MAX];
charanchor_call[PATH_MAX];
struct pf_rulerule;
};
Add rule at the end of the inactive(不活跃) ruleset. This call requires a ticket obtained through a preceding DIOCXBEGIN call. The optional anchor name indicates the anchor in which to append the rule. nr and action are ignored.
DIOCADDQUEUE struct pfioc_queue *q (Add a queue).
struct pfioc_queue {
u_int32_tticket;
u_intnr;
struct pf_queuespecqueue;
};
DIOCGETRULES struct pfioc_rule *pr
Get a ticket for subsequent DIOCGETRULE calls and the number nr of rules in the active ruleset.
DIOCGETRULE struct pfioc_rule *pr
Get a rule by its number nr using the ticket obtained through a preceding DIOCGETRULES call. If action is set to PF_GET_CLR_CNTR, the per-rule statistics on the requested rule are cleared.
DIOCGETQUEUES struct pfioc_queue *pq
Get a ticket for subsequent DIOCGETQUEUE calls and the number nr of queues in the active list.
DIOCGETQUEUE struct pfioc_queue *pq
Get the queueing discipline by its number nr using the ticket obtained through a preceding DIOCGETQUEUES call.
DIOCGETQSTATS struct pfioc_qstats *pq
Get the statistics on a queue.
struct pfioc_qstats {
u_int32_t ticket;
u_int32_t nr;
struct pf_queuespec queue;
void*buf;
int nbytes;
};
This call fills in a pointer to the buffer of statistics buf, of length nbytes, for the queue specified by nr.
DIOCGETRULESETS struct pfioc_ruleset *pr
struct pfioc_ruleset {
u_int32_t nr;
char path[PATH_MAX];
char name[PF_ANCHOR_NAME_SIZE];
};
Get the number nr of rulesets (i.e., anchors) directly attached to the anchor named by path for use in subsequent DIOCGETRULESET calls. Nested anchors, since they are not directly attached to the given anchor, will not be included. This ioctl returns EINVAL if the given anchor does not exist.
DIOCGETRULESET struct pfioc_ruleset *pr
Get a ruleset (i.e., an anchor) name by its number nr from the given anchor path, the maximum number of which can be obtained from a preceding DIOCGETRULESETS call. This ioctl returns EINVAL if the given anchor does not exist or EBUSY if another process is concurrently updating a ruleset.
DIOCADDSTATE struct pfioc_state *ps
Add a state entry.
struct pfioc_state {
struct pfsync_statestate;
};
DIOCGETSTATE struct pfioc_state *ps
Extract the entry identified by the id and creatorid fields of the state structure from the state table.
DIOCKILLSTATES struct pfioc_state_kill *psk
Remove matching entries from the state table. This ioctl returns the number of killed states in psk_killed.
struct pfioc_state_kill {
struct pf_state_cmppsk_pfcmp;
sa_family_tpsk_af;
intpsk_proto;
struct pf_rule_addrpsk_src;
struct pf_rule_addrpsk_dst;
charpsk_ifname[IFNAMSIZ];
charpsk_label[PF_RULE_LABEL_SIZE];
u_intpsk_killed;
u_int16_tpsk_rdomain;
};
DIOCCLRSTATES struct pfioc_state_kill *psk
Clear all states. It works like DIOCKILLSTATES, but ignores all fields of the pfioc_state_kill structure, except psk_ifname.
DIOCGETSTATUS struct pf_status *s
Get the internal packet filter statistics.
struct pf_status {
u_int64_tcounters[PFRES_MAX];
u_int64_tlcounters[LCNT_MAX];/* limit counters */
u_int64_tfcounters[FCNT_MAX];
u_int64_tscounters[SCNT_MAX];
u_int64_tpcounters[2][2][3];
u_int64_tbcounters[2][2];
u_int64_tstateid;
u_int64_tsyncookies_inflight[2]; /* unACKed SYNcookies */
time_tsince;
u_int32_trunning;
u_int32_tstates;
u_int32_tstates_halfopen;
u_int32_tsrc_nodes;
u_int32_tdebug;
u_int32_thostid;
u_int32_treass;/* reassembly */
u_int8_tsyncookies_active;
u_int8_tsyncookies_mode;/* never/always/adaptive */
u_int8_tpad[2];
charifname[IFNAMSIZ];
u_int8_tpf_chksum[PF_MD5_DIGEST_LENGTH];
};
DIOCCLRSTATUS Clear the internal packet filter statistics.
DIOCNATLOOK struct pfioc_natlook *pnl
Look up a state table entry by source and destination addresses and ports.
struct pfioc_natlook {
struct pf_addr saddr;
struct pf_addr daddr;
struct pf_addr rsaddr;
struct pf_addr rdaddr;
u_int16_t rdomain;
u_int16_t rrdomain;
u_int16_t sport;
u_int16_t dport;
u_int16_t rsport;
u_int16_t rdport;
sa_family_t af;
u_int8_t proto;
u_int8_t direction;
};
This was primarily used to support transparent proxies with rdr-to rules. New proxies should use divert-to rules instead. These do not require access to the privileged /dev/pf device and preserve the original destination address for getsockname(2). For SOCK_DGRAM sockets, the ip(4) socket options IP_RECVDSTADDR and IP_RECVDSTPORT can be used to retrieve the destination address and port.
DIOCSETDEBUG u_int32_t *level
Set the debug level. See the syslog(3) man page for a list of valid debug levels.
DIOCGETSTATES struct pfioc_states *ps
Get state table entries.
struct pfioc_states {
size_tps_len;
union {
caddr_t psu_buf;
struct pfsync_state *psu_states;
} ps_u;
#define ps_bufps_u.psu_buf
#define ps_statesps_u.psu_states
};
If ps_len is non-zero on entry, as many states as possible that can fit into this size will be copied into the supplied buffer ps_states. On exit, ps_len is always set to the total size required to hold all state table entries (i.e., it is set to sizeof(struct pfsync_state) * nr).
DIOCCHANGERULE struct pfioc_rule *pcr
Add or remove the rule in the ruleset specified by rule.action.
The type of operation to be performed is indicated by action, which can be any of the following:
enum{ PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
ticket must be set to the value obtained with PF_CHANGE_GET_TICKET for all actions except PF_CHANGE_GET_TICKET. anchor indicates to which anchor the operation applies. nr indicates the rule number against which PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER, or PF_CHANGE_REMOVE actions are applied.
DIOCSETTIMEOUT struct pfioc_tm *pt
struct pfioc_tm {
int timeout;
int seconds;
};
Set the state timeout of timeout to seconds. The old value will be placed into seconds. For possible values of timeout, consult the PFTM_* values in <net/pfvar.h>.
DIOCGETTIMEOUT struct pfioc_tm *pt
Get the state timeout of timeout. The value will be placed into the seconds field.
DIOCSETLIMIT struct pfioc_limit *pl
Set the hard limits on the memory pools used by the packet filter.
struct pfioc_limit {
intindex;
unsignedlimit;
};
enum{ PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_PKTDELAY_PKTS,
PF_LIMIT_ANCHORS, PF_LIMIT_MAX };
DIOCGETLIMIT struct pfioc_limit *pl
Get the hard limit for the memory pool indicated by index.
DIOCRCLRTABLES struct pfioc_table *io
Clear all tables. All the ioctls that manipulate radix tables use the same structure described below. For DIOCRCLRTABLES, pfrio_ndel contains on exit the number of tables deleted.
struct pfioc_table {
struct pfr_table pfrio_table;
void*pfrio_buffer;
int pfrio_esize;
int pfrio_size;
int pfrio_size2;
int pfrio_nadd;
int pfrio_ndel;
int pfrio_nchange;
int pfrio_flags;
u_int32_t pfrio_ticket;
};
#define pfrio_exists pfrio_nadd
#define pfrio_nzero pfrio_nadd
#define pfrio_nmatch pfrio_nadd
#define pfrio_naddr pfrio_size2
#define pfrio_setflag pfrio_size2
#define pfrio_clrflag pfrio_nadd
DIOCRADDTABLES struct pfioc_table *io
Create one or more tables. On entry, pfrio_buffer must point to an array of struct pfr_table containing at least pfrio_size elements. pfrio_esize must be the size of struct pfr_table. On exit, pfrio_nadd contains the number of tables effectively created.
struct pfr_table {
charpfrt_anchor[PATH_MAX];
charpfrt_name[PF_TABLE_NAME_SIZE];
u_int32_tpfrt_flags;
u_int8_tpfrt_fback;
};
DIOCRDELTABLES struct pfioc_table *io
Delete one or more tables. On entry, pfrio_buffer must point to an array of struct pfr_table containing at least pfrio_size elements. pfrio_esize must be the size of struct pfr_table. On exit, pfrio_ndel contains the number of tables effectively deleted.
DIOCRGETTABLES struct pfioc_table *io
Get the list of all tables. On entry, pfrio_buffer[pfrio_size] contains a valid writeable buffer for pfr_table structures. On exit, pfrio_size contains the number of tables written into the buffer. If the buffer is too small, the kernel does not store anything but just returns the required buffer size, without error.
DIOCRGETTSTATS struct pfioc_table *io
This call is like DIOCRGETTABLES but is used to get an array of pfr_tstats structures.
struct pfr_tstats {
struct pfr_table pfrts_t;
u_int64_t pfrts_packets
[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
u_int64_t pfrts_bytes
[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
u_int64_t pfrts_match;
u_int64_t pfrts_nomatch;
time_t pfrts_tzero;
int pfrts_cnt;
int pfrts_refcnt[PFR_REFCNT_MAX];
};
#define pfrts_name pfrts_t.pfrt_name
#define pfrts_flags pfrts_t.pfrt_flags
DIOCRCLRTSTATS struct pfioc_table *io
Clear the statistics of one or more tables. On entry, pfrio_buffer must point to an array of struct pfr_table containing at least pfrio_size elements. pfrio_esize must be the size of struct pfr_table. On exit, pfrio_nzero contains the number of tables effectively cleared.
DIOCRCLRADDRS struct pfioc_table *io
Clear all addresses in a table. On entry, pfrio_table contains the table to clear. On exit, pfrio_ndel contains the number of addresses removed.
DIOCRADDADDRS struct pfioc_table *io
Add one or more addresses to a table. On entry, pfrio_table contains the table ID and pfrio_buffer must point to an array of struct pfr_addr containing at least pfrio_size elements to add to the table. pfrio_esize must be the size of struct pfr_addr. On exit, pfrio_nadd contains the number of addresses effectively added.
struct pfr_addr {
union {
struct in_addr _pfra_ip4addr;
struct in6_addr _pfra_ip6addr;
} pfra_u;
char pfra_ifname[IFNAMSIZ];
u_int32_t pfra_states;
u_int16_t pfra_weight;
u_int8_t pfra_af;
u_int8_t pfra_net;
u_int8_t pfra_not;
u_int8_t pfra_fback;
u_int8_t pfra_type;
u_int8_t pad[7];
};
#define pfra_ip4addr pfra_u._pfra_ip4addr
#define pfra_ip6addr pfra_u._pfra_ip6addr
DIOCRDELADDRS struct pfioc_table *io
Delete one or more addresses from a table. On entry, pfrio_table contains the table ID and pfrio_buffer must point to an array of struct pfr_addr containing at least pfrio_size elements to delete from the table. pfrio_esize must be the size of struct pfr_addr. On exit, pfrio_ndel contains the number of addresses effectively deleted.
DIOCRSETADDRS struct pfioc_table *io
Replace the content of a table by a new address list. This is the most complicated command, which uses all the structure members.
On entry, pfrio_table contains the table ID and pfrio_buffer must point to an array of struct pfr_addr containing at least pfrio_size elements which become the new contents of the table. pfrio_esize must be the size of struct pfr_addr. Additionally, if pfrio_size2 is non-zero, pfrio_buffer[pfrio_size..pfrio_size2] must be a writeable buffer, into which the kernel can copy the addresses that have been deleted during the replace operation. On exit, pfrio_ndel, pfrio_nadd, and pfrio_nchange contain the number of addresses deleted, added, and changed by the kernel.
If pfrio_size2 was set on entry, pfrio_size2 will point to the size of the buffer used, exactly like DIOCRGETADDRS.
DIOCRGETADDRS struct pfioc_table *io
Get all the addresses of a table. On entry, pfrio_table contains the table ID and pfrio_buffer[pfrio_size] contains a valid writeable buffer for pfr_addr structures. On exit, pfrio_size contains the number of addresses written into the buffer. If the buffer was too small, the kernel does not store anything but just returns the required buffer size, without returning an error.
DIOCRGETASTATS struct pfioc_table *io
This call is like DIOCRGETADDRS but is used to get an array of pfr_astats structures.
struct pfr_astats {
struct pfr_addr pfras_a;
u_int64_t pfras_packets
[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
u_int64_t pfras_bytes
[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
time_t pfras_tzero;
};
DIOCRCLRASTATS struct pfioc_table *io
Clear the statistics of one or more addresses. On entry, pfrio_table contains the table ID and pfrio_buffer must point to an array of struct pfr_addr containing at least pfrio_size elements to be cleared from the table. pfrio_esize must be the size of struct pfr_addr. On exit, pfrio_nzero contains the number of addresses effectively cleared.
DIOCRTSTADDRS struct pfioc_table *io
Test if the given addresses match a table. On entry, pfrio_table contains the table ID and pfrio_buffer must point to an array of struct pfr_addr containing at least pfrio_size elements, each of which will be tested for a match in the table. pfrio_esize must be the size of struct pfr_addr. On exit, the kernel updates the pfr_addr array by setting the pfra_fback member appropriately.
DIOCRSETTFLAGS struct pfioc_table *io
Change the PFR_TFLAG_CONST or PFR_TFLAG_PERSIST flags of a table. On entry, pfrio_buffer must point to an array of struct pfr_table containing at least pfrio_size elements. pfrio_esize must be the size of struct pfr_table. pfrio_setflag must contain the flags to add, while pfrio_clrflag must contain the flags to remove. On exit, pfrio_nchange and pfrio_ndel contain the number of tables altered or deleted by the kernel. Yes, tables can be deleted if one removes the PFR_TFLAG_PERSIST flag of an unreferenced table.
DIOCRINADEFINE struct pfioc_table *io
Defines a table in the inactive set. On entry, pfrio_table contains the table ID and pfrio_buffer[pfrio_size] contains an array of pfr_addr structures to put in the table. A valid ticket must also be supplied to pfrio_ticket. On exit, pfrio_nadd contains 0 if the table was already defined in the inactive list or 1 if a new table has been created. pfrio_naddr contains the number of addresses effectively put in the table.
DIOCXBEGIN struct pfioc_trans *io
struct pfioc_trans {
int size;/* number of elements */
int esize;/* size of each element in bytes */
struct pfioc_trans_e {
inttype;
charanchor[PATH_MAX];
u_int32_tticket;
}*array;
};
Clear all the inactive rulesets specified in the pfioc_trans_e array. For each ruleset, a ticket is returned for subsequent "add rule" ioctls, as well as for the DIOCXCOMMIT and DIOCXROLLBACK calls.
Ruleset types, identified by type, can be one of the following:
Filter rules.
Address tables.
DIOCXCOMMIT struct pfioc_trans *io
Atomically switch a vector of inactive rulesets to the active rulesets. This call is implemented as a standard two-phase commit, which will either fail for all rulesets or completely succeed. All tickets need to be valid. This ioctl returns EBUSY if another process is concurrently updating some of the same rulesets.
DIOCXROLLBACK struct pfioc_trans *io
Clean up the kernel by undoing all changes that have taken place on the inactive rulesets since the last DIOCXBEGIN. DIOCXROLLBACK will silently ignore rulesets for which the ticket is invalid.
DIOCSETHOSTID u_int32_t *hosted
Set the host ID, which is used by pfsync(4) to identify which host created state table entries.
DIOCOSFPFLUSH Flush the passive OS fingerprint table.
DIOCOSFPADD struct pf_osfp_ioctl *io
struct pf_osfp_ioctl {
struct pf_osfp_entryfp_os;
pf_tcpopts_tfp_tcpopts;/* packed TCP options */
u_int16_tfp_wsize;/* TCP window size */
u_int16_tfp_psize;/* ip->ip_len */
u_int16_tfp_mss;/* TCP MSS */
u_int16_tfp_flags;
u_int8_tfp_optcnt;/* TCP option count */
u_int8_tfp_wscale;/* TCP window scaling */
u_int8_tfp_ttl;/* IPv4 TTL */
intfp_getnum;/* DIOCOSFPGET number */
};
struct pf_osfp_entry {
SLIST_ENTRY(pf_osfp_entry) fp_entry;
pf_osfp_tfp_os;
intfp_enflags;
#define PF_OSFP_EXPANDED0x001/* expanded entry */
#define PF_OSFP_GENERIC0x002/* generic signature */
#define PF_OSFP_NODETAIL0x004/* no p0f details */
#define PF_OSFP_LEN32
u_charfp_class_nm[PF_OSFP_LEN];
u_charfp_version_nm[PF_OSFP_LEN];
u_charfp_subtype_nm[PF_OSFP_LEN];
};
Add a passive OS fingerprint to the table. Set fp_os.fp_os to the packed fingerprint, fp_os.fp_class_nm to the name of the class (Linux, Windows, etc), fp_os.fp_version_nm to the name of the version (NT, 95, 98), and fp_os.fp_subtype_nm to the name of the subtype or patchlevel. The members fp_mss, fp_wsize, fp_psize, fp_ttl, fp_optcnt, and fp_wscale are set to the TCP MSS, the TCP window size, the IP length, the IP TTL, the number of TCP options, and the TCP window scaling constant of the TCP SYN packet, respectively.
The fp_flags member is filled according to the <net/pfvar.h> include file PF_OSFP_* defines. The fp_tcpopts member contains packed TCP options. Each option uses PF_OSFP_TCPOPT_BITS bits in the packed value. Options include any of PF_OSFP_TCPOPT_NOP, PF_OSFP_TCPOPT_SACK, PF_OSFP_TCPOPT_WSCALE, PF_OSFP_TCPOPT_MSS, or PF_OSFP_TCPOPT_TS.
The fp_getnum member is not used with this ioctl.
The structure's slack space must be zeroed for correct operation; memset(3) the whole structure to zero before filling and sending to the kernel.
DIOCOSFPGET struct pf_osfp_ioctl *io
Get the passive OS fingerprint number fp_getnum from the kernel's fingerprint list. The rest of the structure members will come back filled. Get the whole list by repeatedly incrementing the fp_getnum number until the ioctl returns EBUSY.
DIOCGETSRCNODES struct pfioc_src_nodes *psn
struct pfioc_src_nodes {
size_tpsn_len;
union {
caddr_tpsu_buf;
struct pf_src_node*psu_src_nodes;
} psn_u;
#define psn_bufpsn_u.psu_buf
#define psn_src_nodespsn_u.psu_src_nodes
};
Get the list of source nodes kept by sticky addresses and source tracking. The ioctl must be called once with psn_len set to 0. If the ioctl returns without error, psn_len will be set to the size of the buffer required to hold all the pf_src_node structures held in the table. A buffer of this size should then be allocated, and a pointer to this buffer placed in psn_buf. The ioctl must then be called again to fill this buffer with the actual source node data. After that call, psn_len will be set to the length of the buffer actually used.
DIOCCLRSRCNODES Clear the tree of source tracking nodes.
DIOCIGETIFACES struct pfioc_iface *io
Get the list of interfaces and interface groups known to pf. All the ioctls that manipulate interfaces use the same structure described below:
struct pfioc_iface {
char pfiio_name[IFNAMSIZ];
void*pfiio_buffer;
int pfiio_esize;
int pfiio_size;
int pfiio_nzero;
int pfiio_flags;
};
If not empty, pfiio_name can be used to restrict the search to a specific interface or group. pfiio_buffer[pfiio_size] is the user-supplied buffer for returning the data. On entry, pfiio_size contains the number of pfi_kif entries that can fit into the buffer. The kernel will replace this value by the real number of entries it wants to return. pfiio_esize should be set to sizeof(struct pfi_kif).
The data is returned in the pfi_kif structure described below:
struct pfi_kif {
char pfik_name[IFNAMSIZ];
RB_ENTRY(pfi_kif) pfik_tree;
u_int64_t pfik_packets[2][2][2];
u_int64_t pfik_bytes[2][2][2];
time_t pfik_tzero;
int pfik_flags;
int pfik_flags_new;
void*pfik_ah_cookie;
struct ifnet*pfik_ifp;
struct ifg_group*pfik_group;
int pfik_states;
int pfik_rules;
int pfik_routes;
int pfik_srcnodes;
int pfik_flagrefs;
TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs;
};
DIOCSETSTATUSIF struct pfioc_iface *pi
Specify the interface for which statistics are accumulated.
DIOCSETIFFLAG struct pfioc_iface *io
Set the user settable flags (described above) of the pf internal interface description. The filtering process is the same as for DIOCIGETIFACES.
#define PFI_IFLAG_SKIP0x0100/* skip filtering on interface */
DIOCCLRIFFLAG struct pfioc_iface *io
Works as DIOCSETIFFLAG above but clears the flags.
DIOCKILLSRCNODES struct pfioc_src_node_kill *psnk
Explicitly remove source tracking nodes.
struct pfioc_src_node_kill {
sa_family_t psnk_af;
struct pf_rule_addr psnk_src;
struct pf_rule_addr psnk_dst;
u_int psnk_killed;
};
编码样例:使用 DIOCGETLIMIT 命令去显示包过滤的内存池使用限制
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/pfvar.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const struct {
const char*name;
intindex;
} pf_limits[] = {
{ "states",PF_LIMIT_STATES },
{ "src-nodes",PF_LIMIT_SRC_NODES },
{ "frags",PF_LIMIT_FRAGS },
{ "tables",PF_LIMIT_TABLES },
{ "table-entries",PF_LIMIT_TABLE_ENTRIES },
{ "anchors",PF_LIMIT_ANCHORS },
{ NULL,0 }
};
void usage(void)
{
int i;
fprintf(stderr, "usage: %s [", getprogname());
for (i = 0; pf_limits.name; i++)
fprintf(stderr, "%s%s", (i > 0 ? "|" : ""), pf_limits.name);
fprintf(stderr, "]\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct pfioc_limit pl;
int i, dev;
int pool_index = -1;
if (argc != 2)
usage();
for (i = 0; pf_limits.name; i++)
if (strcmp(argv[1], pf_limits.name) == 0) {
pool_index = pf_limits.index;
break;
}
if (pool_index == -1) {
warnx("no such memory pool: %s", argv[1]);
usage();
}
dev = open("/dev/pf", O_RDWR);
if (dev == -1)
err(1, "open(\"/dev/pf\") failed");
memset(&pl, 0, sizeof(struct pfioc_limit));
pl.index = pool_index;
if (ioctl(dev, DIOCGETLIMIT, &pl) == -1)
err(1, "DIOCGETLIMIT");
printf("The %s memory pool has ", pf_limits.name);
if (pl.limit == UINT_MAX)
printf("unlimited entries.\n");
else
printf("a hard limit of %u entries.\n", pl.limit);
return 0;
}
4.2.3 架构
暂无

使用特权

评论回复
16
keer_zu|  楼主 | 2023-6-7 19:27 | 只看该作者
5 其他
QNX7.1版本对网络协议栈稍作改变,使用了io-stack代替原有的io-pkt(7.0版本),但是能够实现平替,只是在开发时候需要注意7.1以下版本没有io-stack

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1314

主题

12271

帖子

53

粉丝