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 | |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 26 | static inline bool match_type(struct net *net, const struct net_device *dev, |
| 27 | __be32 addr, u_int16_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 29 | return !!(mask & (1 << inet_dev_addr_type(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 |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 33 | addrtype_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 35 | struct net *net = dev_net(par->in ? par->in : par->out); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 36 | const struct ipt_addrtype_info *info = par->matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 37 | const struct iphdr *iph = ip_hdr(skb); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 38 | bool ret = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | if (info->source) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 41 | ret &= match_type(net, NULL, iph->saddr, info->source) ^ |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 42 | info->invert_source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (info->dest) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 44 | ret &= match_type(net, NULL, iph->daddr, info->dest) ^ |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 45 | info->invert_dest; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return ret; |
| 48 | } |
| 49 | |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 50 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 51 | addrtype_mt_v1(const struct sk_buff *skb, const struct xt_match_param *par) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 52 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 53 | struct net *net = dev_net(par->in ? par->in : par->out); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 54 | const struct ipt_addrtype_info_v1 *info = par->matchinfo; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 55 | const struct iphdr *iph = ip_hdr(skb); |
| 56 | const struct net_device *dev = NULL; |
| 57 | bool ret = true; |
| 58 | |
| 59 | if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 60 | dev = par->in; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 61 | else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 62 | dev = par->out; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 63 | |
| 64 | if (info->source) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 65 | ret &= match_type(net, dev, iph->saddr, info->source) ^ |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 66 | (info->flags & IPT_ADDRTYPE_INVERT_SOURCE); |
| 67 | if (ret && info->dest) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 68 | ret &= match_type(net, dev, iph->daddr, info->dest) ^ |
Anders Grafström | 46faec9 | 2008-08-18 21:29:57 -0700 | [diff] [blame] | 69 | !!(info->flags & IPT_ADDRTYPE_INVERT_DEST); |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 70 | return ret; |
| 71 | } |
| 72 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 73 | static bool addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 74 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 75 | struct ipt_addrtype_info_v1 *info = par->matchinfo; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 76 | |
| 77 | if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN && |
| 78 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { |
| 79 | printk(KERN_ERR "ipt_addrtype: both incoming and outgoing " |
| 80 | "interface limitation cannot be selected\n"); |
| 81 | return false; |
| 82 | } |
| 83 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 84 | if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) | |
| 85 | (1 << NF_INET_LOCAL_IN)) && |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 86 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { |
| 87 | printk(KERN_ERR "ipt_addrtype: output interface limitation " |
| 88 | "not valid in PRE_ROUTING and INPUT\n"); |
| 89 | return false; |
| 90 | } |
| 91 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 92 | if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) | |
| 93 | (1 << NF_INET_LOCAL_OUT)) && |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 94 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) { |
| 95 | printk(KERN_ERR "ipt_addrtype: input interface limitation " |
| 96 | "not valid in POST_ROUTING and OUTPUT\n"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | static struct xt_match addrtype_mt_reg[] __read_mostly = { |
| 104 | { |
| 105 | .name = "addrtype", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 106 | .family = NFPROTO_IPV4, |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 107 | .match = addrtype_mt_v0, |
| 108 | .matchsize = sizeof(struct ipt_addrtype_info), |
| 109 | .me = THIS_MODULE |
| 110 | }, |
| 111 | { |
| 112 | .name = "addrtype", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 113 | .family = NFPROTO_IPV4, |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 114 | .revision = 1, |
| 115 | .match = addrtype_mt_v1, |
| 116 | .checkentry = addrtype_mt_checkentry_v1, |
| 117 | .matchsize = sizeof(struct ipt_addrtype_info_v1), |
| 118 | .me = THIS_MODULE |
| 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 122 | static int __init addrtype_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 124 | return xt_register_matches(addrtype_mt_reg, |
| 125 | ARRAY_SIZE(addrtype_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 128 | static void __exit addrtype_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 130 | xt_unregister_matches(addrtype_mt_reg, 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 | module_init(addrtype_mt_init); |
| 134 | module_exit(addrtype_mt_exit); |