前段时间我实现了基于 Ashwin Kashyap 的 vnat 模块的 ebtables 模块(http://www.research.rutgers.edu/~ashwink/misc_projs/ebt_vnat.html)。模块可用于 broute 表中的 BROUTING 链,用于剥离 vlan 标签并将 vlan id 放入 nf 标记中。模块还允许根据 nat 表的 POSTROUTING 链中的 nf 标记值添加 vlan 标记。模块在处理桥接流量时没有任何问题。系统坚如磐石,稳定。一旦我将 tproxy 拦截添加到混合中,问题就开始了。这会在运行一段时间后导致内核恐慌。我强调这样一个事实,即在经过几分钟的 tproxied 流量流之后,我在第一个数据包后没有看到内核恐慌。看来我处理 SKB 的方式对于 L3+ 处理来说不够干净。顺便说一句,我正在开发 2.6.32 内核。请在下面的评论中找到模块的关键部分:
// code for adding vlan tag based on skb->mark value
if (!skb_make_writable(skb, 0))
return EBT_DROP;
if(skb->mark > 0){
// maybe we should always seek VLAN_HLEN+ETH_HLEN instead of using condition?
if (skb_headroom(skb) < (skb->mac_len == 0 ? VLAN_HLEN + ETH_HLEN : VLAN_HLEN ) ) {
struct sk_buff *sk_tmp = skb;
skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN );
kfree_skb(sk_tmp);
if (skb == NULL) {
return EBT_DROP;
}
}
// we need 4 more bytes for 802.1q header, so push!...I can almost see the head(er)
skb_push(skb, VLAN_HLEN);
skb->mac_header-=VLAN_HLEN;
skb->network_header-=VLAN_HLEN;
skb->transport_header-=VLAN_HLEN;
veth = (struct vlan_ethhdr *) eth_hdr(skb);
// move dst/src mac addresses (12b of header) 4 bytes back to make room for
// 802.1q header
memmove( skb->head + skb->mac_header, skb->head + skb->mac_header + VLAN_HLEN, 12);
// fill 802.1q header
veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
veth_TCI = skb->mark & 0xfff;
veth->h_vlan_TCI = htons(veth_TCI);
}
// code for stripping vlan tag and putting it into skb->mark value
veth = (struct vlan_ethhdr *)eth_hdr(skb);
if(veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)){
if (!skb_make_writable(skb, 0))
return EBT_DROP;
// determine vlan id
vid=(ntohs(veth->h_vlan_TCI) & 0xfff);
mark = vid;
// copy dst/src mac addresses (12b) 4 bytes fwd, so it covers 802.1q header
memmove(skb->head + skb->mac_header + VLAN_HLEN, skb->head + skb->mac_header, 12);
// adapt header pointers
skb->mac_header+=VLAN_HLEN;
skb->mac_len = ETH_HLEN;
skb->network_header+=VLAN_HLEN;
skb->transport_header+=VLAN_HLEN;
skb->data += VLAN_HLEN;
skb->len -= VLAN_HLEN;
eth = eth_hdr(skb);
skb->protocol=eth->h_proto;
}
skb->mark=mark;
我将不胜感激任何指针(只要它们至少为 64 位长)。谢谢!