]> Pileus Git - ~andy/linux/blob - net/bridge/netfilter/ebt_ip.c
netfilter: change return types of match functions for ebtables extensions
[~andy/linux] / net / bridge / netfilter / ebt_ip.c
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 #include <linux/ip.h>
15 #include <net/ip.h>
16 #include <linux/in.h>
17 #include <linux/module.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_bridge/ebtables.h>
20 #include <linux/netfilter_bridge/ebt_ip.h>
21
22 struct tcpudphdr {
23         __be16 src;
24         __be16 dst;
25 };
26
27 static bool ebt_filter_ip(const struct sk_buff *skb,
28    const struct net_device *in,
29    const struct net_device *out, const void *data,
30    unsigned int datalen)
31 {
32         const struct ebt_ip_info *info = data;
33         const struct iphdr *ih;
34         struct iphdr _iph;
35         const struct tcpudphdr *pptr;
36         struct tcpudphdr _ports;
37
38         ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
39         if (ih == NULL)
40                 return false;
41         if (info->bitmask & EBT_IP_TOS &&
42            FWINV(info->tos != ih->tos, EBT_IP_TOS))
43                 return false;
44         if (info->bitmask & EBT_IP_SOURCE &&
45            FWINV((ih->saddr & info->smsk) !=
46            info->saddr, EBT_IP_SOURCE))
47                 return false;
48         if ((info->bitmask & EBT_IP_DEST) &&
49            FWINV((ih->daddr & info->dmsk) !=
50            info->daddr, EBT_IP_DEST))
51                 return false;
52         if (info->bitmask & EBT_IP_PROTO) {
53                 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
54                         return false;
55                 if (!(info->bitmask & EBT_IP_DPORT) &&
56                     !(info->bitmask & EBT_IP_SPORT))
57                         return true;
58                 if (ntohs(ih->frag_off) & IP_OFFSET)
59                         return false;
60                 pptr = skb_header_pointer(skb, ih->ihl*4,
61                                           sizeof(_ports), &_ports);
62                 if (pptr == NULL)
63                         return false;
64                 if (info->bitmask & EBT_IP_DPORT) {
65                         u32 dst = ntohs(pptr->dst);
66                         if (FWINV(dst < info->dport[0] ||
67                                   dst > info->dport[1],
68                                   EBT_IP_DPORT))
69                         return false;
70                 }
71                 if (info->bitmask & EBT_IP_SPORT) {
72                         u32 src = ntohs(pptr->src);
73                         if (FWINV(src < info->sport[0] ||
74                                   src > info->sport[1],
75                                   EBT_IP_SPORT))
76                         return false;
77                 }
78         }
79         return true;
80 }
81
82 static bool ebt_ip_check(const char *tablename, unsigned int hookmask,
83    const struct ebt_entry *e, void *data, unsigned int datalen)
84 {
85         const struct ebt_ip_info *info = data;
86
87         if (e->ethproto != htons(ETH_P_IP) ||
88            e->invflags & EBT_IPROTO)
89                 return false;
90         if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
91                 return false;
92         if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
93                 if (info->invflags & EBT_IP_PROTO)
94                         return false;
95                 if (info->protocol != IPPROTO_TCP &&
96                     info->protocol != IPPROTO_UDP &&
97                     info->protocol != IPPROTO_UDPLITE &&
98                     info->protocol != IPPROTO_SCTP &&
99                     info->protocol != IPPROTO_DCCP)
100                          return false;
101         }
102         if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
103                 return false;
104         if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
105                 return false;
106         return true;
107 }
108
109 static struct ebt_match filter_ip __read_mostly = {
110         .name           = EBT_IP_MATCH,
111         .match          = ebt_filter_ip,
112         .check          = ebt_ip_check,
113         .matchsize      = XT_ALIGN(sizeof(struct ebt_ip_info)),
114         .me             = THIS_MODULE,
115 };
116
117 static int __init ebt_ip_init(void)
118 {
119         return ebt_register_match(&filter_ip);
120 }
121
122 static void __exit ebt_ip_fini(void)
123 {
124         ebt_unregister_match(&filter_ip);
125 }
126
127 module_init(ebt_ip_init);
128 module_exit(ebt_ip_fini);
129 MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
130 MODULE_LICENSE("GPL");