Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iptables module to match inet_addr_type() of an ip. |
| 3 | * |
| 4 | * Copyright (c) 2004 Patrick McHardy <kaber@trash.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/ip.h> |
| 16 | #include <net/route.h> |
| 17 | |
| 18 | #include <linux/netfilter_ipv4/ipt_addrtype.h> |
| 19 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 23 | MODULE_DESCRIPTION("iptables addrtype match"); |
| 24 | |
| 25 | static inline int match_type(u_int32_t addr, u_int16_t mask) |
| 26 | { |
| 27 | return !!(mask & (1 << inet_addr_type(addr))); |
| 28 | } |
| 29 | |
| 30 | static int match(const struct sk_buff *skb, const struct net_device *in, |
| 31 | const struct net_device *out, const void *matchinfo, |
| 32 | int offset, int *hotdrop) |
| 33 | { |
| 34 | const struct ipt_addrtype_info *info = matchinfo; |
| 35 | const struct iphdr *iph = skb->nh.iph; |
| 36 | int ret = 1; |
| 37 | |
| 38 | if (info->source) |
| 39 | ret &= match_type(iph->saddr, info->source)^info->invert_source; |
| 40 | if (info->dest) |
| 41 | ret &= match_type(iph->daddr, info->dest)^info->invert_dest; |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static int checkentry(const char *tablename, const struct ipt_ip *ip, |
| 47 | void *matchinfo, unsigned int matchsize, |
| 48 | unsigned int hook_mask) |
| 49 | { |
| 50 | if (matchsize != IPT_ALIGN(sizeof(struct ipt_addrtype_info))) { |
| 51 | printk(KERN_ERR "ipt_addrtype: invalid size (%u != %Zu)\n.", |
| 52 | matchsize, IPT_ALIGN(sizeof(struct ipt_addrtype_info))); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | static struct ipt_match addrtype_match = { |
| 60 | .name = "addrtype", |
| 61 | .match = match, |
| 62 | .checkentry = checkentry, |
| 63 | .me = THIS_MODULE |
| 64 | }; |
| 65 | |
| 66 | static int __init init(void) |
| 67 | { |
| 68 | return ipt_register_match(&addrtype_match); |
| 69 | } |
| 70 | |
| 71 | static void __exit fini(void) |
| 72 | { |
| 73 | ipt_unregister_match(&addrtype_match); |
| 74 | } |
| 75 | |
| 76 | module_init(init); |
| 77 | module_exit(fini); |