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 | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 27 | static bool ebt_filter_ip(const struct sk_buff *skb, |
| 28 | const struct net_device *in, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | const struct net_device *out, const void *data, |
| 30 | unsigned int datalen) |
| 31 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 32 | const struct ebt_ip_info *info = data; |
| 33 | const struct iphdr *ih; |
| 34 | struct iphdr _iph; |
| 35 | const struct tcpudphdr *pptr; |
| 36 | struct tcpudphdr _ports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 39 | if (ih == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 40 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | if (info->bitmask & EBT_IP_TOS && |
| 42 | FWINV(info->tos != ih->tos, EBT_IP_TOS)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 43 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (info->bitmask & EBT_IP_SOURCE && |
| 45 | FWINV((ih->saddr & info->smsk) != |
| 46 | info->saddr, EBT_IP_SOURCE)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 47 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if ((info->bitmask & EBT_IP_DEST) && |
| 49 | FWINV((ih->daddr & info->dmsk) != |
| 50 | info->daddr, EBT_IP_DEST)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 51 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (info->bitmask & EBT_IP_PROTO) { |
| 53 | if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 54 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (!(info->bitmask & EBT_IP_DPORT) && |
| 56 | !(info->bitmask & EBT_IP_SPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 57 | return true; |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame] | 58 | if (ntohs(ih->frag_off) & IP_OFFSET) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 59 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 61 | sizeof(_ports), &_ports); |
| 62 | if (pptr == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 63 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (info->bitmask & EBT_IP_DPORT) { |
| 65 | u32 dst = ntohs(pptr->dst); |
| 66 | if (FWINV(dst < info->dport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 67 | dst > info->dport[1], |
| 68 | EBT_IP_DPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 69 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | if (info->bitmask & EBT_IP_SPORT) { |
| 72 | u32 src = ntohs(pptr->src); |
| 73 | if (FWINV(src < info->sport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 74 | src > info->sport[1], |
| 75 | EBT_IP_SPORT)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 76 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 79 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 82 | static bool ebt_ip_check(const char *tablename, unsigned int hookmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 84 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 85 | const struct ebt_ip_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (e->ethproto != htons(ETH_P_IP) || |
| 88 | e->invflags & EBT_IPROTO) |
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_MASK || info->invflags & ~EBT_IP_MASK) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 91 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) { |
| 93 | if (info->invflags & EBT_IP_PROTO) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 94 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (info->protocol != IPPROTO_TCP && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 96 | info->protocol != IPPROTO_UDP && |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 97 | info->protocol != IPPROTO_UDPLITE && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 98 | info->protocol != IPPROTO_SCTP && |
| 99 | info->protocol != IPPROTO_DCCP) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 100 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1]) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 103 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1]) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 105 | return false; |
| 106 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jan Engelhardt | 30083c9 | 2008-01-31 04:00:59 -0800 | [diff] [blame] | 109 | static struct ebt_match filter_ip __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | .name = EBT_IP_MATCH, |
| 111 | .match = ebt_filter_ip, |
| 112 | .check = ebt_ip_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 113 | .matchsize = XT_ALIGN(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 | { |
| 119 | return ebt_register_match(&filter_ip); |
| 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 | { |
| 124 | ebt_unregister_match(&filter_ip); |
| 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"); |