Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_ip |
| 3 | * |
| 4 | * Authors: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
| 6 | * |
| 7 | * April, 2002 |
| 8 | * |
| 9 | * Changes: |
| 10 | * added ip-sport and ip-dport |
| 11 | * Innominate Security Technologies AG <mhopf@innominate.com> |
| 12 | * September, 2002 |
| 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/ip.h> |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame] | 15 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/in.h> |
| 17 | #include <linux/module.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 18 | #include <linux/netfilter/x_tables.h> |
| 19 | #include <linux/netfilter_bridge/ebtables.h> |
| 20 | #include <linux/netfilter_bridge/ebt_ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | struct tcpudphdr { |
Al Viro | 47c183f | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 23 | __be16 src; |
| 24 | __be16 dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 27 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 28 | ebt_ip_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 30 | const struct ebt_ip_info *info = par->matchinfo; |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 31 | const struct iphdr *ih; |
| 32 | struct iphdr _iph; |
| 33 | const struct tcpudphdr *pptr; |
| 34 | struct tcpudphdr _ports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 37 | if (ih == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 38 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | if (info->bitmask & EBT_IP_TOS && |
| 40 | FWINV(info->tos != ih->tos, EBT_IP_TOS)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 41 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (info->bitmask & EBT_IP_SOURCE && |
| 43 | FWINV((ih->saddr & info->smsk) != |
| 44 | info->saddr, EBT_IP_SOURCE)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 45 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if ((info->bitmask & EBT_IP_DEST) && |
| 47 | FWINV((ih->daddr & info->dmsk) != |
| 48 | info->daddr, EBT_IP_DEST)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 49 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | if (info->bitmask & EBT_IP_PROTO) { |
| 51 | if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 52 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (!(info->bitmask & EBT_IP_DPORT) && |
| 54 | !(info->bitmask & EBT_IP_SPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 55 | return true; |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame] | 56 | if (ntohs(ih->frag_off) & IP_OFFSET) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 57 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 59 | sizeof(_ports), &_ports); |
| 60 | if (pptr == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 61 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | if (info->bitmask & EBT_IP_DPORT) { |
| 63 | u32 dst = ntohs(pptr->dst); |
| 64 | if (FWINV(dst < info->dport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 65 | dst > info->dport[1], |
| 66 | EBT_IP_DPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 67 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | if (info->bitmask & EBT_IP_SPORT) { |
| 70 | u32 src = ntohs(pptr->src); |
| 71 | if (FWINV(src < info->sport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 72 | src > info->sport[1], |
| 73 | EBT_IP_SPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 74 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 77 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 80 | static bool ebt_ip_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 82 | const struct ebt_ip_info *info = par->matchinfo; |
| 83 | const struct ebt_entry *e = par->entryinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (e->ethproto != htons(ETH_P_IP) || |
| 86 | e->invflags & EBT_IPROTO) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 87 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 89 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) { |
| 91 | if (info->invflags & EBT_IP_PROTO) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 92 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (info->protocol != IPPROTO_TCP && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 94 | info->protocol != IPPROTO_UDP && |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 95 | info->protocol != IPPROTO_UDPLITE && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 96 | info->protocol != IPPROTO_SCTP && |
| 97 | info->protocol != IPPROTO_DCCP) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 98 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1]) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 101 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1]) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 103 | return false; |
| 104 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 107 | static struct xt_match ebt_ip_mt_reg __read_mostly = { |
| 108 | .name = "ip", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 109 | .revision = 0, |
| 110 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 111 | .match = ebt_ip_mt, |
| 112 | .checkentry = ebt_ip_mt_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 113 | .matchsize = sizeof(struct ebt_ip_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .me = THIS_MODULE, |
| 115 | }; |
| 116 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 117 | static int __init ebt_ip_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 119 | return xt_register_match(&ebt_ip_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 122 | static void __exit ebt_ip_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 124 | xt_unregister_match(&ebt_ip_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 127 | module_init(ebt_ip_init); |
| 128 | module_exit(ebt_ip_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 129 | MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | MODULE_LICENSE("GPL"); |