【问题标题】:How to set iptables mark when sending a packet?发送数据包时如何设置iptables mark?
【发布时间】:2018-12-07 13:48:32
【问题描述】:

Linux 的 iptable 和 iproute 允许我们标记数据包并在以后匹配标记(fwmark),从而在配置路由和防火墙方面具有很大的灵活性。

有没有办法在从 C 程序发送数据包时设置这些标记,无论是通过普通套接字接口还是通过特定的 linux 系统调用?

【问题讨论】:

  • 操作标记是需要root权限的事情之一。
  • @BenVoigt 有很多东西需要root权限,很多程序需要以root身份运行。你的观点是什么?
  • 如果您的“C 程序”不以 root 身份运行,您可能永远找不到方法。

标签: c linux sockets networking iptables


【解决方案1】:

我在 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");

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多