本文主要是介绍ebtables配置IPMAC绑定关系,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主机192.168.1.201的MAC地址如下:
$ ip address
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:38:40:6b brd ff:ff:ff:ff:ff:ffinet 192.168.1.201/24 brd 192.168.1.255 scope global eth0valid_lft forever preferred_lft forever
在另外一台主机上配置如下的规则,如果检测到192.168.1.201的MAC地址不等于00:0c:29:38:40:6b,表明为假冒的主机192.168.1.201,将报文丢弃。
# ebtables -A INPUT -p IPv4 --ip-src 192.168.1.201 -s ! 00:0c:29:38:40:6b -j DROP
#
# ebtables -t filter -L --Lc --Ln
Bridge table: filterBridge chain: INPUT, entries: 1, policy: ACCEPT
1 -p IPv4 -s ! 00:0c:29:38:40:6b --ip-src 192.168.1.201 -j DROP , pcnt = 0 -- bcnt = 0
将第三台主机的接口IP地址设置为192.168.1.201,ping以上配置了IPMAC绑定的主机,没有响应。查看规则信息,如下丢弃了5个报文:
# ebtables -t filter -L --Lc --Ln
Bridge table: filterBridge chain: INPUT, entries: 1, policy: ACCEPT
1 -p IPv4 -s ! 00:0c:29:38:40:6b --ip-src 192.168.1.201 -j DROP , pcnt = 5 -- bcnt = 300
注意,以上规则配置于INPUT点,对于ARP请求是不生效的。另外,IPMAC绑定也可以配置在FORWARD点,用于检测转发的流量。当然,也可以配置在OUTPUT点,不过意义不大。
# ebtables -A FORWARD -p IPv4 --ip-src 192.168.1.201 -s ! 00:0c:29:38:40:6b -j DROP
ip匹配
注册以下的匹配结构ebt_ip_mt_reg。
static struct xt_match ebt_ip_mt_reg __read_mostly = {.name = "ip",.revision = 0,.family = NFPROTO_BRIDGE,.match = ebt_ip_mt,.checkentry = ebt_ip_mt_check,.matchsize = sizeof(struct ebt_ip_info),.me = THIS_MODULE,
};
static int __init ebt_ip_init(void)
{return xt_register_match(&ebt_ip_mt_reg);
}
函数ebt_ip_mt_check检测配置项的合法性,协议必须为ETH_P_IP,协议字段必须为EBT_IP_MASK所包含,包括以下几项:
#define EBT_IP_SOURCE 0x01
#define EBT_IP_DEST 0x02
#define EBT_IP_TOS 0x04
#define EBT_IP_PROTO 0x08
#define EBT_IP_SPORT 0x10
#define EBT_IP_DPORT 0x20
#define EBT_IP_ICMP 0x40
#define EBT_IP_IGMP 0x80static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
{const struct ebt_ip_info *info = par->matchinfo;const struct ebt_entry *e = par->entryinfo;if (e->ethproto != htons(ETH_P_IP) ||e->invflags & EBT_IPROTO)return -EINVAL;if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)return -EINVAL;
如果指定了目的端口或者源端口(EBT_IP_DPORT | EBT_IP_SPORT),那么EBT_IP_PROTO字段(–ip-protocol)指定的四层协议必须满足以下要求。
if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {if (info->invflags & EBT_IP_PROTO)return -EINVAL;if (info->protocol != IPPROTO_TCP &&info->protocol != IPPROTO_UDP &&info->protocol != IPPROTO_UDPLITE &&info->protocol != IPPROTO_SCTP &&info->protocol != IPPROTO_DCCP)return -EINVAL;}
确保合法的端口范围。
if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])return -EINVAL;if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])return -EINVAL;
对于ICMP协议参数(–ip-icmp-type [!] type[[:type]/code[:code]]),确保type和code的范围合法。对于IGMP协议(–ip-igmp-type [!] type[:type]),确保type范围合法。
if (info->bitmask & EBT_IP_ICMP) {if ((info->invflags & EBT_IP_PROTO) ||info->protocol != IPPROTO_ICMP)return -EINVAL;if (info->icmp_type[0] > info->icmp_type[1] ||info->icmp_code[0] > info->icmp_code[1])return -EINVAL;}if (info->bitmask & EBT_IP_IGMP) {if ((info->invflags & EBT_IP_PROTO) ||info->protocol != IPPROTO_IGMP)return -EINVAL;if (info->igmp_type[0] > info->igmp_type[1])return -EINVAL;}
以下为数据流程中的匹配处理函数,依次配置值与报文中的相应字段进行对比,如IP头部TOS字段,源地址/目的地址。
static bool
ebt_ip_mt(const struct sk_buff *skb, struct xt_action_param *par)
{const struct ebt_ip_info *info = par->matchinfo;const struct iphdr *ih;struct iphdr _iph;const union pkthdr *pptr;union pkthdr _pkthdr;ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);if (ih == NULL)return false;if ((info->bitmask & EBT_IP_TOS) &&NF_INVF(info, EBT_IP_TOS, info->tos != ih->tos))return false;if ((info->bitmask & EBT_IP_SOURCE) &&NF_INVF(info, EBT_IP_SOURCE,(ih->saddr & info->smsk) != info->saddr))return false;if ((info->bitmask & EBT_IP_DEST) &&NF_INVF(info, EBT_IP_DEST,(ih->daddr & info->dmsk) != info->daddr))return false;
比较四层协议,如果相等,进行四层协议字段的比较,对于分片报文,由于可能没有四层头部信息,直接返回false。
if (info->bitmask & EBT_IP_PROTO) {if (NF_INVF(info, EBT_IP_PROTO, info->protocol != ih->protocol))return false;if (!(info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT |EBT_IP_ICMP | EBT_IP_IGMP)))return true;if (ntohs(ih->frag_off) & IP_OFFSET)return false;
否则,进行源端口范围,目的端口范围,或者ICMP、IGMP相关的对比。
/* min icmp/igmp headersize is 4, so sizeof(_pkthdr) is ok. */pptr = skb_header_pointer(skb, ih->ihl*4, sizeof(_pkthdr), &_pkthdr);if (pptr == NULL) return false;if (info->bitmask & EBT_IP_DPORT) {u32 dst = ntohs(pptr->tcpudphdr.dst);if (NF_INVF(info, EBT_IP_DPORT,dst < info->dport[0] || dst > info->dport[1]))return false;}if (info->bitmask & EBT_IP_SPORT) {u32 src = ntohs(pptr->tcpudphdr.src);if (NF_INVF(info, EBT_IP_SPORT,src < info->sport[0] || src > info->sport[1]))return false;}if ((info->bitmask & EBT_IP_ICMP) && NF_INVF(info, EBT_IP_ICMP,pptr->icmphdr.type < info->icmp_type[0] ||pptr->icmphdr.type > info->icmp_type[1] ||pptr->icmphdr.code < info->icmp_code[0] ||pptr->icmphdr.code > info->icmp_code[1]))return false;if ((info->bitmask & EBT_IP_IGMP) &&NF_INVF(info, EBT_IP_IGMP,pptr->igmphdr.type < info->igmp_type[0] ||pptr->igmphdr.type > info->igmp_type[1]))return false;
ebtables基础规则,以上命令种的-s选项就属于基础匹配规则,判断报文中的源MAC地址是否与指定规则中的MAC地址相同。
static inline int
ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,const struct net_device *in, const struct net_device *out)
{const struct ethhdr *h = eth_hdr(skb);const struct net_bridge_port *p;__be16 ethproto;if (e->bitmask & EBT_SOURCEMAC) {if (NF_INVF(e, EBT_ISOURCE,!ether_addr_equal_masked(h->h_source, e->sourcemac,e->sourcemsk)))return 1;}
内核版本 5.10
这篇关于ebtables配置IPMAC绑定关系的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!