fwmark 如何与 ip 规则命令中的掩码一起使用

fwmark 如何与 ip 规则命令中的掩码一起使用

我试图理解它们是如何ip rule add fwmark一起工作的mask

例如: ip rule命令显示以下规则: 100:从所有 fwmark 0xd00/0xffffff00 查找 6017

因此,如果有标记为“0xd70”的数据包到达,它将尝试匹配 ip 规则中的标记,但是如何使用 fwmark 和掩码“0xd00/0xffffff00”计算“0xd70”?

谢谢你!

答案1

没有太多关于fwmask来自iproute或内核的发行说明的信息。最后,我必须fwmask从内核代码中获取 的真正含义,如下所示,

static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops, struct flowi *fl, int flags, struct fib_lookup_arg *arg)
{
  int ret = 0;
  if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
     goto out;
  if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
     goto out;
  if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)  <<<<<<<<<<<
     goto out;
  if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
     goto out;
  if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
     goto out;
  if (uid_lt(fl->flowi_uid, rule->uid_range.start) || uid_gt(fl->flowi_uid, rule->uid_range.end))
     goto out;
    
  ret = ops->match(rule, fl, flags);

  out:
  return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
}

具体来说,fwmark//表示fwmask 0xXYZ只有0xFFFFFFFF标记本身才能匹配规则。

int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack)
{
  struct net *net = sock_net(skb->sk);
  struct fib_rule_hdr *frh = nlmsg_data(nlh);
  ...

  if (tb[FRA_FWMARK]) {
    rule->mark = nla_get_u32(tb[FRA_FWMARK]);
    if (rule->mark)
      /* compatibility: if the mark value is non-zero all bits
       * are compared unless a mask is explicitly specified.
       */
     rule->mark_mask = 0xFFFFFFFF; <<<<<<<<<<<
  }

相关内容