1 // SPDX-License-Identifier: GPL-2.0-only
11 * added ip-sport and ip-dport
18 #include <linux/module.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_bridge/ebtables.h>
21 #include <linux/netfilter_bridge/ebt_ip.h>
38 ebt_ip_mt(const struct sk_buff *skb, struct xt_action_param *par)
40 const struct ebt_ip_info *info = par->matchinfo;
41 const struct iphdr *ih;
43 const union pkthdr *pptr;
46 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
49 if ((info->bitmask & EBT_IP_TOS) &&
50 NF_INVF(info, EBT_IP_TOS, info->tos != ih->tos))
52 if ((info->bitmask & EBT_IP_SOURCE) &&
53 NF_INVF(info, EBT_IP_SOURCE,
54 (ih->saddr & info->smsk) != info->saddr))
56 if ((info->bitmask & EBT_IP_DEST) &&
57 NF_INVF(info, EBT_IP_DEST,
58 (ih->daddr & info->dmsk) != info->daddr))
60 if (info->bitmask & EBT_IP_PROTO) {
61 if (NF_INVF(info, EBT_IP_PROTO, info->protocol != ih->protocol))
63 if (!(info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT |
64 EBT_IP_ICMP | EBT_IP_IGMP)))
66 if (ntohs(ih->frag_off) & IP_OFFSET)
69 /* min icmp/igmp headersize is 4, so sizeof(_pkthdr) is ok. */
70 pptr = skb_header_pointer(skb, ih->ihl*4,
71 sizeof(_pkthdr), &_pkthdr);
74 if (info->bitmask & EBT_IP_DPORT) {
75 u32 dst = ntohs(pptr->tcpudphdr.dst);
76 if (NF_INVF(info, EBT_IP_DPORT,
77 dst < info->dport[0] ||
78 dst > info->dport[1]))
81 if (info->bitmask & EBT_IP_SPORT) {
82 u32 src = ntohs(pptr->tcpudphdr.src);
83 if (NF_INVF(info, EBT_IP_SPORT,
84 src < info->sport[0] ||
85 src > info->sport[1]))
88 if ((info->bitmask & EBT_IP_ICMP) &&
89 NF_INVF(info, EBT_IP_ICMP,
90 pptr->icmphdr.type < info->icmp_type[0] ||
91 pptr->icmphdr.type > info->icmp_type[1] ||
92 pptr->icmphdr.code < info->icmp_code[0] ||
93 pptr->icmphdr.code > info->icmp_code[1]))
95 if ((info->bitmask & EBT_IP_IGMP) &&
96 NF_INVF(info, EBT_IP_IGMP,
97 pptr->igmphdr.type < info->igmp_type[0] ||
98 pptr->igmphdr.type > info->igmp_type[1]))
104 static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
106 const struct ebt_ip_info *info = par->matchinfo;
107 const struct ebt_entry *e = par->entryinfo;
109 if (e->ethproto != htons(ETH_P_IP) ||
110 e->invflags & EBT_IPROTO)
112 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
114 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
115 if (info->invflags & EBT_IP_PROTO)
117 if (info->protocol != IPPROTO_TCP &&
118 info->protocol != IPPROTO_UDP &&
119 info->protocol != IPPROTO_UDPLITE &&
120 info->protocol != IPPROTO_SCTP &&
121 info->protocol != IPPROTO_DCCP)
124 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
126 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
128 if (info->bitmask & EBT_IP_ICMP) {
129 if ((info->invflags & EBT_IP_PROTO) ||
130 info->protocol != IPPROTO_ICMP)
132 if (info->icmp_type[0] > info->icmp_type[1] ||
133 info->icmp_code[0] > info->icmp_code[1])
136 if (info->bitmask & EBT_IP_IGMP) {
137 if ((info->invflags & EBT_IP_PROTO) ||
138 info->protocol != IPPROTO_IGMP)
140 if (info->igmp_type[0] > info->igmp_type[1])
146 static struct xt_match ebt_ip_mt_reg __read_mostly = {
149 .family = NFPROTO_BRIDGE,
151 .checkentry = ebt_ip_mt_check,
152 .matchsize = sizeof(struct ebt_ip_info),
156 static int __init ebt_ip_init(void)
158 return xt_register_match(&ebt_ip_mt_reg);
161 static void __exit ebt_ip_fini(void)
163 xt_unregister_match(&ebt_ip_mt_reg);
166 module_init(ebt_ip_init);
167 module_exit(ebt_ip_fini);
168 MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
169 MODULE_LICENSE("GPL");