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 | */ |
| 14 | |
| 15 | #include <linux/netfilter_bridge/ebtables.h> |
| 16 | #include <linux/netfilter_bridge/ebt_ip.h> |
| 17 | #include <linux/ip.h> |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame] | 18 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/in.h> |
| 20 | #include <linux/module.h> |
| 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 | |
| 27 | static int ebt_filter_ip(const struct sk_buff *skb, const struct net_device *in, |
| 28 | const struct net_device *out, const void *data, |
| 29 | unsigned int datalen) |
| 30 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame^] | 31 | const struct ebt_ip_info *info = data; |
| 32 | const struct iphdr *ih; |
| 33 | struct iphdr _iph; |
| 34 | const struct tcpudphdr *pptr; |
| 35 | struct tcpudphdr _ports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 38 | if (ih == NULL) |
| 39 | return EBT_NOMATCH; |
| 40 | if (info->bitmask & EBT_IP_TOS && |
| 41 | FWINV(info->tos != ih->tos, EBT_IP_TOS)) |
| 42 | return EBT_NOMATCH; |
| 43 | if (info->bitmask & EBT_IP_SOURCE && |
| 44 | FWINV((ih->saddr & info->smsk) != |
| 45 | info->saddr, EBT_IP_SOURCE)) |
| 46 | return EBT_NOMATCH; |
| 47 | if ((info->bitmask & EBT_IP_DEST) && |
| 48 | FWINV((ih->daddr & info->dmsk) != |
| 49 | info->daddr, EBT_IP_DEST)) |
| 50 | return EBT_NOMATCH; |
| 51 | if (info->bitmask & EBT_IP_PROTO) { |
| 52 | if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO)) |
| 53 | return EBT_NOMATCH; |
| 54 | if (!(info->bitmask & EBT_IP_DPORT) && |
| 55 | !(info->bitmask & EBT_IP_SPORT)) |
| 56 | return EBT_MATCH; |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame] | 57 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 58 | return EBT_NOMATCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 60 | sizeof(_ports), &_ports); |
| 61 | if (pptr == NULL) |
| 62 | return EBT_NOMATCH; |
| 63 | if (info->bitmask & EBT_IP_DPORT) { |
| 64 | u32 dst = ntohs(pptr->dst); |
| 65 | if (FWINV(dst < info->dport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 66 | dst > info->dport[1], |
| 67 | EBT_IP_DPORT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return EBT_NOMATCH; |
| 69 | } |
| 70 | if (info->bitmask & EBT_IP_SPORT) { |
| 71 | u32 src = ntohs(pptr->src); |
| 72 | if (FWINV(src < info->sport[0] || |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 73 | src > info->sport[1], |
| 74 | EBT_IP_SPORT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return EBT_NOMATCH; |
| 76 | } |
| 77 | } |
| 78 | return EBT_MATCH; |
| 79 | } |
| 80 | |
| 81 | static int ebt_ip_check(const char *tablename, unsigned int hookmask, |
| 82 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 83 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame^] | 84 | const struct ebt_ip_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info))) |
| 87 | return -EINVAL; |
| 88 | if (e->ethproto != htons(ETH_P_IP) || |
| 89 | e->invflags & EBT_IPROTO) |
| 90 | return -EINVAL; |
| 91 | if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK) |
| 92 | return -EINVAL; |
| 93 | if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) { |
| 94 | if (info->invflags & EBT_IP_PROTO) |
| 95 | return -EINVAL; |
| 96 | if (info->protocol != IPPROTO_TCP && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 97 | info->protocol != IPPROTO_UDP && |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 98 | info->protocol != IPPROTO_UDPLITE && |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 99 | info->protocol != IPPROTO_SCTP && |
| 100 | info->protocol != IPPROTO_DCCP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | } |
| 103 | if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1]) |
| 104 | return -EINVAL; |
| 105 | if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1]) |
| 106 | return -EINVAL; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static struct ebt_match filter_ip = |
| 111 | { |
| 112 | .name = EBT_IP_MATCH, |
| 113 | .match = ebt_filter_ip, |
| 114 | .check = ebt_ip_check, |
| 115 | .me = THIS_MODULE, |
| 116 | }; |
| 117 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 118 | static int __init ebt_ip_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | return ebt_register_match(&filter_ip); |
| 121 | } |
| 122 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 123 | static void __exit ebt_ip_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | ebt_unregister_match(&filter_ip); |
| 126 | } |
| 127 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 128 | module_init(ebt_ip_init); |
| 129 | module_exit(ebt_ip_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | MODULE_LICENSE("GPL"); |