发送数据包时如何设置iptables标记?
How to set iptables mark when sending a packet?
Linux 的 iptable 和 iproute 允许我们标记数据包并稍后匹配标记(fwmark),从而在配置路由和防火墙时具有很大的灵活性。
有没有办法在通过普通套接字接口或通过特定的 linux 系统调用从 C 程序发送数据包时设置这些标记?
我在 socket(7) 手册页中找到了 SO_MARK
套接字选项:
SO_MARK (since Linux 2.6.25)
Set the mark for each packet sent through this socket (similar
to the netfilter MARK target but socket-based). Changing the
mark can be used for mark-based routing without netfilter or
for packet filtering. Setting this option requires the
CAP_NET_ADMIN capability.
它不是按数据包计算的,正如我最初询问的那样,适合我的目的。您可以使用 setsockopt()
:
进行设置
int fwmark;
//fwmark = <some value>;
if (setsockopt(sockfd, SOL_SOCKET, SO_MARK, &fwmark, sizeof fwmark) == -1)
perror("failed setting mark for socket packets");
Linux 的 iptable 和 iproute 允许我们标记数据包并稍后匹配标记(fwmark),从而在配置路由和防火墙时具有很大的灵活性。
有没有办法在通过普通套接字接口或通过特定的 linux 系统调用从 C 程序发送数据包时设置这些标记?
我在 socket(7) 手册页中找到了 SO_MARK
套接字选项:
SO_MARK (since Linux 2.6.25)
Set the mark for each packet sent through this socket (similar
to the netfilter MARK target but socket-based). Changing the
mark can be used for mark-based routing without netfilter or
for packet filtering. Setting this option requires the
CAP_NET_ADMIN capability.
它不是按数据包计算的,正如我最初询问的那样,适合我的目的。您可以使用 setsockopt()
:
int fwmark;
//fwmark = <some value>;
if (setsockopt(sockfd, SOL_SOCKET, SO_MARK, &fwmark, sizeof fwmark) == -1)
perror("failed setting mark for socket packets");