1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 #include <linux/module.h>
3 #include <linux/skbuff.h>
6 #include <linux/sctp.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/xt_sctp.h>
10 #include <linux/netfilter_ipv4/ip_tables.h>
11 #include <linux/netfilter_ipv6/ip6_tables.h>
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("Kiran Kumar Immidi");
15 MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
16 MODULE_ALIAS("ipt_sctp");
17 MODULE_ALIAS("ip6t_sctp");
19 #define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
20 || (!!((invflag) & (option)) ^ (cond)))
23 match_flags(const struct xt_sctp_flag_info
*flag_info
,
30 for (i
= 0; i
< flag_count
; i
++)
31 if (flag_info
[i
].chunktype
== chunktype
)
32 return (chunkflags
& flag_info
[i
].flag_mask
) == flag_info
[i
].flag
;
38 match_packet(const struct sk_buff
*skb
,
40 const struct xt_sctp_info
*info
,
43 u_int32_t chunkmapcopy
[256 / sizeof (u_int32_t
)];
44 const sctp_chunkhdr_t
*sch
;
46 int chunk_match_type
= info
->chunk_match_type
;
47 const struct xt_sctp_flag_info
*flag_info
= info
->flag_info
;
48 int flag_count
= info
->flag_count
;
54 if (chunk_match_type
== SCTP_CHUNK_MATCH_ALL
)
55 SCTP_CHUNKMAP_COPY(chunkmapcopy
, info
->chunkmap
);
58 sch
= skb_header_pointer(skb
, offset
, sizeof(_sch
), &_sch
);
59 if (sch
== NULL
|| sch
->length
== 0) {
60 pr_debug("Dropping invalid SCTP packet.\n");
65 pr_debug("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d"
67 ++i
, offset
, sch
->type
, htons(sch
->length
),
70 offset
+= (ntohs(sch
->length
) + 3) & ~3;
72 pr_debug("skb->len: %d\toffset: %d\n", skb
->len
, offset
);
74 if (SCTP_CHUNKMAP_IS_SET(info
->chunkmap
, sch
->type
)) {
75 switch (chunk_match_type
) {
76 case SCTP_CHUNK_MATCH_ANY
:
77 if (match_flags(flag_info
, flag_count
,
78 sch
->type
, sch
->flags
)) {
83 case SCTP_CHUNK_MATCH_ALL
:
84 if (match_flags(flag_info
, flag_count
,
85 sch
->type
, sch
->flags
))
86 SCTP_CHUNKMAP_CLEAR(chunkmapcopy
, sch
->type
);
89 case SCTP_CHUNK_MATCH_ONLY
:
90 if (!match_flags(flag_info
, flag_count
,
91 sch
->type
, sch
->flags
))
96 switch (chunk_match_type
) {
97 case SCTP_CHUNK_MATCH_ONLY
:
101 } while (offset
< skb
->len
);
103 switch (chunk_match_type
) {
104 case SCTP_CHUNK_MATCH_ALL
:
105 return SCTP_CHUNKMAP_IS_CLEAR(chunkmapcopy
);
106 case SCTP_CHUNK_MATCH_ANY
:
108 case SCTP_CHUNK_MATCH_ONLY
:
112 /* This will never be reached, but required to stop compiler whine */
117 sctp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
119 const struct xt_sctp_info
*info
= par
->matchinfo
;
120 const sctp_sctphdr_t
*sh
;
123 if (par
->fragoff
!= 0) {
124 pr_debug("Dropping non-first fragment.. FIXME\n");
128 sh
= skb_header_pointer(skb
, par
->thoff
, sizeof(_sh
), &_sh
);
130 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
134 pr_debug("spt: %d\tdpt: %d\n", ntohs(sh
->source
), ntohs(sh
->dest
));
136 return SCCHECK(ntohs(sh
->source
) >= info
->spts
[0]
137 && ntohs(sh
->source
) <= info
->spts
[1],
138 XT_SCTP_SRC_PORTS
, info
->flags
, info
->invflags
)
139 && SCCHECK(ntohs(sh
->dest
) >= info
->dpts
[0]
140 && ntohs(sh
->dest
) <= info
->dpts
[1],
141 XT_SCTP_DEST_PORTS
, info
->flags
, info
->invflags
)
142 && SCCHECK(match_packet(skb
, par
->thoff
+ sizeof(sctp_sctphdr_t
),
143 info
, &par
->hotdrop
),
144 XT_SCTP_CHUNK_TYPES
, info
->flags
, info
->invflags
);
147 static int sctp_mt_check(const struct xt_mtchk_param
*par
)
149 const struct xt_sctp_info
*info
= par
->matchinfo
;
151 if (info
->flags
& ~XT_SCTP_VALID_FLAGS
)
153 if (info
->invflags
& ~XT_SCTP_VALID_FLAGS
)
155 if (info
->invflags
& ~info
->flags
)
157 if (!(info
->flags
& XT_SCTP_CHUNK_TYPES
))
159 if (info
->chunk_match_type
& (SCTP_CHUNK_MATCH_ALL
|
160 SCTP_CHUNK_MATCH_ANY
| SCTP_CHUNK_MATCH_ONLY
))
165 static struct xt_match sctp_mt_reg
[] __read_mostly
= {
168 .family
= NFPROTO_IPV4
,
169 .checkentry
= sctp_mt_check
,
171 .matchsize
= sizeof(struct xt_sctp_info
),
172 .proto
= IPPROTO_SCTP
,
177 .family
= NFPROTO_IPV6
,
178 .checkentry
= sctp_mt_check
,
180 .matchsize
= sizeof(struct xt_sctp_info
),
181 .proto
= IPPROTO_SCTP
,
186 static int __init
sctp_mt_init(void)
188 return xt_register_matches(sctp_mt_reg
, ARRAY_SIZE(sctp_mt_reg
));
191 static void __exit
sctp_mt_exit(void)
193 xt_unregister_matches(sctp_mt_reg
, ARRAY_SIZE(sctp_mt_reg
));
196 module_init(sctp_mt_init
);
197 module_exit(sctp_mt_exit
);