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 { |
| 23 | uint16_t src; |
| 24 | uint16_t dst; |
| 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 | { |
| 31 | struct ebt_ip_info *info = (struct ebt_ip_info *)data; |
| 32 | struct iphdr _iph, *ih; |
| 33 | struct tcpudphdr _ports, *pptr; |
| 34 | |
| 35 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 36 | if (ih == NULL) |
| 37 | return EBT_NOMATCH; |
| 38 | if (info->bitmask & EBT_IP_TOS && |
| 39 | FWINV(info->tos != ih->tos, EBT_IP_TOS)) |
| 40 | return EBT_NOMATCH; |
| 41 | if (info->bitmask & EBT_IP_SOURCE && |
| 42 | FWINV((ih->saddr & info->smsk) != |
| 43 | info->saddr, EBT_IP_SOURCE)) |
| 44 | return EBT_NOMATCH; |
| 45 | if ((info->bitmask & EBT_IP_DEST) && |
| 46 | FWINV((ih->daddr & info->dmsk) != |
| 47 | info->daddr, EBT_IP_DEST)) |
| 48 | return EBT_NOMATCH; |
| 49 | if (info->bitmask & EBT_IP_PROTO) { |
| 50 | if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO)) |
| 51 | return EBT_NOMATCH; |
| 52 | if (!(info->bitmask & EBT_IP_DPORT) && |
| 53 | !(info->bitmask & EBT_IP_SPORT)) |
| 54 | return EBT_MATCH; |
Bart De Schuymer | 8a4c8a9 | 2006-01-10 13:12:22 -0800 | [diff] [blame^] | 55 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 56 | return EBT_NOMATCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 58 | sizeof(_ports), &_ports); |
| 59 | if (pptr == NULL) |
| 60 | return EBT_NOMATCH; |
| 61 | if (info->bitmask & EBT_IP_DPORT) { |
| 62 | u32 dst = ntohs(pptr->dst); |
| 63 | if (FWINV(dst < info->dport[0] || |
| 64 | dst > info->dport[1], |
| 65 | EBT_IP_DPORT)) |
| 66 | return EBT_NOMATCH; |
| 67 | } |
| 68 | if (info->bitmask & EBT_IP_SPORT) { |
| 69 | u32 src = ntohs(pptr->src); |
| 70 | if (FWINV(src < info->sport[0] || |
| 71 | src > info->sport[1], |
| 72 | EBT_IP_SPORT)) |
| 73 | return EBT_NOMATCH; |
| 74 | } |
| 75 | } |
| 76 | return EBT_MATCH; |
| 77 | } |
| 78 | |
| 79 | static int ebt_ip_check(const char *tablename, unsigned int hookmask, |
| 80 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 81 | { |
| 82 | struct ebt_ip_info *info = (struct ebt_ip_info *)data; |
| 83 | |
| 84 | if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info))) |
| 85 | return -EINVAL; |
| 86 | if (e->ethproto != htons(ETH_P_IP) || |
| 87 | e->invflags & EBT_IPROTO) |
| 88 | return -EINVAL; |
| 89 | if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK) |
| 90 | return -EINVAL; |
| 91 | if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) { |
| 92 | if (info->invflags & EBT_IP_PROTO) |
| 93 | return -EINVAL; |
| 94 | if (info->protocol != IPPROTO_TCP && |
| 95 | info->protocol != IPPROTO_UDP) |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1]) |
| 99 | return -EINVAL; |
| 100 | if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1]) |
| 101 | return -EINVAL; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static struct ebt_match filter_ip = |
| 106 | { |
| 107 | .name = EBT_IP_MATCH, |
| 108 | .match = ebt_filter_ip, |
| 109 | .check = ebt_ip_check, |
| 110 | .me = THIS_MODULE, |
| 111 | }; |
| 112 | |
| 113 | static int __init init(void) |
| 114 | { |
| 115 | return ebt_register_match(&filter_ip); |
| 116 | } |
| 117 | |
| 118 | static void __exit fini(void) |
| 119 | { |
| 120 | ebt_unregister_match(&filter_ip); |
| 121 | } |
| 122 | |
| 123 | module_init(init); |
| 124 | module_exit(fini); |
| 125 | MODULE_LICENSE("GPL"); |