2 * iptables module for DCCP protocol header matching
4 * (C) 2005 by Harald Welte <laforge@netfilter.org>
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.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/spinlock.h>
15 #include <linux/dccp.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv4/ipt_dccp.h>
20 #define DCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
21 || (!!((invflag) & (option)) ^ (cond)))
23 static unsigned char *dccp_optbuf;
24 static DEFINE_SPINLOCK(dccp_buflock);
27 dccp_find_option(u_int8_t option,
28 const struct sk_buff *skb,
29 const struct dccp_hdr *dh,
32 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
34 unsigned int optoff = __dccp_hdr_len(dh);
35 unsigned int optlen = dh->dccph_doff*4 - __dccp_hdr_len(dh);
38 if (dh->dccph_doff * 4 < __dccp_hdr_len(dh)) {
46 spin_lock_bh(&dccp_buflock);
47 op = skb_header_pointer(skb,
48 skb->nh.iph->ihl*4 + optoff,
51 /* If we don't have the whole header, drop packet. */
52 spin_unlock_bh(&dccp_buflock);
57 for (i = 0; i < optlen; ) {
58 if (op[i] == option) {
59 spin_unlock_bh(&dccp_buflock);
69 spin_unlock_bh(&dccp_buflock);
75 match_types(const struct dccp_hdr *dh, u_int16_t typemask)
77 return (typemask & (1 << dh->dccph_type));
81 match_option(u_int8_t option, const struct sk_buff *skb,
82 const struct dccp_hdr *dh, int *hotdrop)
84 return dccp_find_option(option, skb, dh, hotdrop);
88 match(const struct sk_buff *skb,
89 const struct net_device *in,
90 const struct net_device *out,
91 const void *matchinfo,
95 const struct ipt_dccp_info *info =
96 (const struct ipt_dccp_info *)matchinfo;
97 struct dccp_hdr _dh, *dh;
102 dh = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_dh), &_dh);
108 return DCCHECK(((ntohs(dh->dccph_sport) >= info->spts[0])
109 && (ntohs(dh->dccph_sport) <= info->spts[1])),
110 IPT_DCCP_SRC_PORTS, info->flags, info->invflags)
111 && DCCHECK(((ntohs(dh->dccph_dport) >= info->dpts[0])
112 && (ntohs(dh->dccph_dport) <= info->dpts[1])),
113 IPT_DCCP_DEST_PORTS, info->flags, info->invflags)
114 && DCCHECK(match_types(dh, info->typemask),
115 IPT_DCCP_TYPE, info->flags, info->invflags)
116 && DCCHECK(match_option(info->option, skb, dh, hotdrop),
117 IPT_DCCP_OPTION, info->flags, info->invflags);
121 checkentry(const char *tablename,
122 const struct ipt_ip *ip,
124 unsigned int matchsize,
125 unsigned int hook_mask)
127 const struct ipt_dccp_info *info;
129 info = (const struct ipt_dccp_info *)matchinfo;
131 return ip->proto == IPPROTO_DCCP
132 && !(ip->invflags & IPT_INV_PROTO)
133 && matchsize == IPT_ALIGN(sizeof(struct ipt_dccp_info))
134 && !(info->flags & ~IPT_DCCP_VALID_FLAGS)
135 && !(info->invflags & ~IPT_DCCP_VALID_FLAGS)
136 && !(info->invflags & ~info->flags);
139 static struct ipt_match dccp_match =
143 .checkentry = &checkentry,
147 static int __init init(void)
151 /* doff is 8 bits, so the maximum option size is (4*256). Don't put
152 * this in BSS since DaveM is worried about locked TLB's for kernel
154 dccp_optbuf = kmalloc(256 * 4, GFP_KERNEL);
157 ret = ipt_register_match(&dccp_match);
164 static void __exit fini(void)
166 ipt_unregister_match(&dccp_match);
173 MODULE_LICENSE("GPL");
174 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
175 MODULE_DESCRIPTION("Match for DCCP protocol packets");