1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/types.h>
15#include <linux/module.h>
16#include <net/ip.h>
17#include <linux/ipv6.h>
18#include <net/ipv6.h>
19#include <net/mip6.h>
20
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter_ipv6/ip6t_mh.h>
23
24MODULE_DESCRIPTION("Xtables: IPv6 Mobility Header match");
25MODULE_LICENSE("GPL");
26
27#ifdef DEBUG_IP_FIREWALL_USER
28#define duprintf(format, args...) printk(format , ## args)
29#else
30#define duprintf(format, args...)
31#endif
32
33
34static inline bool
35type_match(u_int8_t min, u_int8_t max, u_int8_t type, bool invert)
36{
37 return (type >= min && type <= max) ^ invert;
38}
39
40static bool mh_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
41{
42 struct ip6_mh _mh;
43 const struct ip6_mh *mh;
44 const struct ip6t_mh *mhinfo = par->matchinfo;
45
46
47 if (par->fragoff != 0)
48 return false;
49
50 mh = skb_header_pointer(skb, par->thoff, sizeof(_mh), &_mh);
51 if (mh == NULL) {
52
53
54 duprintf("Dropping evil MH tinygram.\n");
55 *par->hotdrop = true;
56 return false;
57 }
58
59 if (mh->ip6mh_proto != IPPROTO_NONE) {
60 duprintf("Dropping invalid MH Payload Proto: %u\n",
61 mh->ip6mh_proto);
62 *par->hotdrop = true;
63 return false;
64 }
65
66 return type_match(mhinfo->types[0], mhinfo->types[1], mh->ip6mh_type,
67 !!(mhinfo->invflags & IP6T_MH_INV_TYPE));
68}
69
70static bool mh_mt6_check(const struct xt_mtchk_param *par)
71{
72 const struct ip6t_mh *mhinfo = par->matchinfo;
73
74
75 return !(mhinfo->invflags & ~IP6T_MH_INV_MASK);
76}
77
78static struct xt_match mh_mt6_reg __read_mostly = {
79 .name = "mh",
80 .family = NFPROTO_IPV6,
81 .checkentry = mh_mt6_check,
82 .match = mh_mt6,
83 .matchsize = sizeof(struct ip6t_mh),
84 .proto = IPPROTO_MH,
85 .me = THIS_MODULE,
86};
87
88static int __init mh_mt6_init(void)
89{
90 return xt_register_match(&mh_mt6_reg);
91}
92
93static void __exit mh_mt6_exit(void)
94{
95 xt_unregister_match(&mh_mt6_reg);
96}
97
98module_init(mh_mt6_init);
99module_exit(mh_mt6_exit);
100