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> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 19 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 23 | MODULE_DESCRIPTION("iptables addrtype match"); |
| 24 | |
Al Viro | fd68322 | 2006-09-26 22:17:51 -0700 | [diff] [blame] | 25 | static inline int match_type(__be32 addr, u_int16_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | return !!(mask & (1 << inet_addr_type(addr))); |
| 28 | } |
| 29 | |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 30 | static int match(const struct sk_buff *skb, |
| 31 | const struct net_device *in, const struct net_device *out, |
| 32 | const struct xt_match *match, const void *matchinfo, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 33 | int offset, unsigned int protoff, int *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | const struct ipt_addrtype_info *info = matchinfo; |
| 36 | const struct iphdr *iph = skb->nh.iph; |
| 37 | int ret = 1; |
| 38 | |
| 39 | if (info->source) |
| 40 | ret &= match_type(iph->saddr, info->source)^info->invert_source; |
| 41 | if (info->dest) |
| 42 | ret &= match_type(iph->daddr, info->dest)^info->invert_dest; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return ret; |
| 45 | } |
| 46 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 47 | static struct xt_match addrtype_match = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | .name = "addrtype", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 49 | .family = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | .match = match, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 51 | .matchsize = sizeof(struct ipt_addrtype_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .me = THIS_MODULE |
| 53 | }; |
| 54 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 55 | static int __init ipt_addrtype_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 57 | return xt_register_match(&addrtype_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 60 | static void __exit ipt_addrtype_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 62 | xt_unregister_match(&addrtype_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 65 | module_init(ipt_addrtype_init); |
| 66 | module_exit(ipt_addrtype_fini); |