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> |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 5 | * (C) 2007 Laszlo Attila Toth <panther@balabit.hu> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/ip.h> |
| 17 | #include <net/route.h> |
| 18 | |
| 19 | #include <linux/netfilter_ipv4/ipt_addrtype.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 20 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 24 | MODULE_DESCRIPTION("Xtables: address type match for IPv4"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 26 | static inline bool match_type(const struct net_device *dev, __be32 addr, |
| 27 | u_int16_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Eric W. Biederman | 6b175b2 | 2008-01-10 03:25:28 -0800 | [diff] [blame] | 29 | return !!(mask & (1 << inet_dev_addr_type(&init_net, dev, addr))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 32 | static bool |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 33 | addrtype_mt_v0(const struct sk_buff *skb, const struct net_device *in, |
| 34 | const struct net_device *out, const struct xt_match *match, |
| 35 | const void *matchinfo, int offset, unsigned int protoff, |
| 36 | bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | const struct ipt_addrtype_info *info = matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 39 | const struct iphdr *iph = ip_hdr(skb); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 40 | bool ret = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | if (info->source) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 43 | ret &= match_type(NULL, iph->saddr, info->source) ^ |
| 44 | info->invert_source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (info->dest) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 46 | ret &= match_type(NULL, iph->daddr, info->dest) ^ |
| 47 | info->invert_dest; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return ret; |
| 50 | } |
| 51 | |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 52 | static bool |
| 53 | addrtype_mt_v1(const struct sk_buff *skb, const struct net_device *in, |
| 54 | const struct net_device *out, const struct xt_match *match, |
| 55 | const void *matchinfo, int offset, unsigned int protoff, |
| 56 | bool *hotdrop) |
| 57 | { |
| 58 | const struct ipt_addrtype_info_v1 *info = matchinfo; |
| 59 | const struct iphdr *iph = ip_hdr(skb); |
| 60 | const struct net_device *dev = NULL; |
| 61 | bool ret = true; |
| 62 | |
| 63 | if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) |
| 64 | dev = in; |
| 65 | else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) |
| 66 | dev = out; |
| 67 | |
| 68 | if (info->source) |
| 69 | ret &= match_type(dev, iph->saddr, info->source) ^ |
| 70 | (info->flags & IPT_ADDRTYPE_INVERT_SOURCE); |
| 71 | if (ret && info->dest) |
| 72 | ret &= match_type(dev, iph->daddr, info->dest) ^ |
| 73 | (info->flags & IPT_ADDRTYPE_INVERT_DEST); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static bool |
| 78 | addrtype_mt_checkentry_v1(const char *tablename, const void *ip_void, |
| 79 | const struct xt_match *match, void *matchinfo, |
| 80 | unsigned int hook_mask) |
| 81 | { |
| 82 | struct ipt_addrtype_info_v1 *info = matchinfo; |
| 83 | |
| 84 | if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN && |
| 85 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { |
| 86 | printk(KERN_ERR "ipt_addrtype: both incoming and outgoing " |
| 87 | "interface limitation cannot be selected\n"); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (hook_mask & (1 << NF_INET_PRE_ROUTING | 1 << NF_INET_LOCAL_IN) && |
| 92 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { |
| 93 | printk(KERN_ERR "ipt_addrtype: output interface limitation " |
| 94 | "not valid in PRE_ROUTING and INPUT\n"); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | if (hook_mask & (1 << NF_INET_POST_ROUTING | 1 << NF_INET_LOCAL_OUT) && |
| 99 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) { |
| 100 | printk(KERN_ERR "ipt_addrtype: input interface limitation " |
| 101 | "not valid in POST_ROUTING and OUTPUT\n"); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | static struct xt_match addrtype_mt_reg[] __read_mostly = { |
| 109 | { |
| 110 | .name = "addrtype", |
| 111 | .family = AF_INET, |
| 112 | .match = addrtype_mt_v0, |
| 113 | .matchsize = sizeof(struct ipt_addrtype_info), |
| 114 | .me = THIS_MODULE |
| 115 | }, |
| 116 | { |
| 117 | .name = "addrtype", |
| 118 | .family = AF_INET, |
| 119 | .revision = 1, |
| 120 | .match = addrtype_mt_v1, |
| 121 | .checkentry = addrtype_mt_checkentry_v1, |
| 122 | .matchsize = sizeof(struct ipt_addrtype_info_v1), |
| 123 | .me = THIS_MODULE |
| 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 127 | static int __init addrtype_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 129 | return xt_register_matches(addrtype_mt_reg, |
| 130 | ARRAY_SIZE(addrtype_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 133 | static void __exit addrtype_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 135 | xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 138 | module_init(addrtype_mt_init); |
| 139 | module_exit(addrtype_mt_exit); |