blob: b63d2a3d80ba5f6a601b23a031f7a4889978dd01 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/*
2 * iptables module for DCCP protocol header matching
3 *
4 * (C) 2005 by Harald Welte <laforge@netfilter.org>
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/module.h>
12#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/spinlock.h>
15#include <net/ip.h>
16#include <linux/dccp.h>
17
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter/xt_dccp.h>
20
21#include <linux/netfilter_ipv4/ip_tables.h>
22#include <linux/netfilter_ipv6/ip6_tables.h>
23
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080026MODULE_DESCRIPTION("Xtables: DCCP protocol packet match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080027MODULE_ALIAS("ipt_dccp");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070028MODULE_ALIAS("ip6t_dccp");
Harald Welte2e4e6a12006-01-12 13:30:04 -080029
30#define DCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080031 || (!!((invflag) & (option)) ^ (cond)))
Harald Welte2e4e6a12006-01-12 13:30:04 -080032
33static unsigned char *dccp_optbuf;
34static DEFINE_SPINLOCK(dccp_buflock);
35
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070036static inline bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080037dccp_find_option(u_int8_t option,
38 const struct sk_buff *skb,
39 unsigned int protoff,
40 const struct dccp_hdr *dh,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070041 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080042{
43 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
Jan Engelhardta47362a2007-07-07 22:16:55 -070044 const unsigned char *op;
Harald Welte2e4e6a12006-01-12 13:30:04 -080045 unsigned int optoff = __dccp_hdr_len(dh);
46 unsigned int optlen = dh->dccph_doff*4 - __dccp_hdr_len(dh);
47 unsigned int i;
48
Ilpo Järvinen79f55f12008-12-14 23:19:02 -080049 if (dh->dccph_doff * 4 < __dccp_hdr_len(dh))
50 goto invalid;
Harald Welte2e4e6a12006-01-12 13:30:04 -080051
52 if (!optlen)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080054
55 spin_lock_bh(&dccp_buflock);
56 op = skb_header_pointer(skb, protoff + optoff, optlen, dccp_optbuf);
57 if (op == NULL) {
58 /* If we don't have the whole header, drop packet. */
Ilpo Järvinen79f55f12008-12-14 23:19:02 -080059 goto partial;
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 }
61
62 for (i = 0; i < optlen; ) {
63 if (op[i] == option) {
64 spin_unlock_bh(&dccp_buflock);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070065 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080066 }
67
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068 if (op[i] < 2)
Harald Welte2e4e6a12006-01-12 13:30:04 -080069 i++;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080070 else
Harald Welte2e4e6a12006-01-12 13:30:04 -080071 i += op[i+1]?:1;
72 }
73
74 spin_unlock_bh(&dccp_buflock);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070075 return false;
Ilpo Järvinen79f55f12008-12-14 23:19:02 -080076
77partial:
78 spin_unlock_bh(&dccp_buflock);
79invalid:
80 *hotdrop = true;
81 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080082}
83
84
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070085static inline bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080086match_types(const struct dccp_hdr *dh, u_int16_t typemask)
87{
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070088 return typemask & (1 << dh->dccph_type);
Harald Welte2e4e6a12006-01-12 13:30:04 -080089}
90
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070091static inline bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080092match_option(u_int8_t option, const struct sk_buff *skb, unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070093 const struct dccp_hdr *dh, bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080094{
95 return dccp_find_option(option, skb, protoff, dh, hotdrop);
96}
97
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070098static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020099dccp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800100{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200101 const struct xt_dccp_info *info = par->matchinfo;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200102 const struct dccp_hdr *dh;
103 struct dccp_hdr _dh;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200105 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700106 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800107
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200108 dh = skb_header_pointer(skb, par->thoff, sizeof(_dh), &_dh);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800109 if (dh == NULL) {
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200110 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700111 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800112 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800113
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700114 return DCCHECK(ntohs(dh->dccph_sport) >= info->spts[0]
115 && ntohs(dh->dccph_sport) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800116 XT_DCCP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700117 && DCCHECK(ntohs(dh->dccph_dport) >= info->dpts[0]
118 && ntohs(dh->dccph_dport) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800119 XT_DCCP_DEST_PORTS, info->flags, info->invflags)
120 && DCCHECK(match_types(dh, info->typemask),
121 XT_DCCP_TYPE, info->flags, info->invflags)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200122 && DCCHECK(match_option(info->option, skb, par->thoff, dh,
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200123 &par->hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124 XT_DCCP_OPTION, info->flags, info->invflags);
125}
126
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100127static int dccp_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800128{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200129 const struct xt_dccp_info *info = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130
Jan Engelhardt9f567312010-03-23 17:40:13 +0100131 if (info->flags & ~XT_DCCP_VALID_FLAGS)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100132 return -EINVAL;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100133 if (info->invflags & ~XT_DCCP_VALID_FLAGS)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100134 return -EINVAL;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100135 if (info->invflags & ~info->flags)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100136 return -EINVAL;
137 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138}
139
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800140static struct xt_match dccp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700141 {
142 .name = "dccp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200143 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144 .checkentry = dccp_mt_check,
145 .match = dccp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700146 .matchsize = sizeof(struct xt_dccp_info),
147 .proto = IPPROTO_DCCP,
148 .me = THIS_MODULE,
149 },
150 {
151 .name = "dccp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200152 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800153 .checkentry = dccp_mt_check,
154 .match = dccp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700155 .matchsize = sizeof(struct xt_dccp_info),
156 .proto = IPPROTO_DCCP,
157 .me = THIS_MODULE,
158 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800159};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800160
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800161static int __init dccp_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800162{
163 int ret;
164
165 /* doff is 8 bits, so the maximum option size is (4*256). Don't put
166 * this in BSS since DaveM is worried about locked TLB's for kernel
167 * BSS. */
168 dccp_optbuf = kmalloc(256 * 4, GFP_KERNEL);
169 if (!dccp_optbuf)
170 return -ENOMEM;
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800171 ret = xt_register_matches(dccp_mt_reg, ARRAY_SIZE(dccp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172 if (ret)
173 goto out_kfree;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800174 return ret;
175
Harald Welte2e4e6a12006-01-12 13:30:04 -0800176out_kfree:
177 kfree(dccp_optbuf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800178 return ret;
179}
180
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800181static void __exit dccp_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800182{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800183 xt_unregister_matches(dccp_mt_reg, ARRAY_SIZE(dccp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184 kfree(dccp_optbuf);
185}
186
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800187module_init(dccp_mt_init);
188module_exit(dccp_mt_exit);