1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match ROUTING parameters. */
4 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/ipv6.h>
10 #include <linux/types.h>
11 #include <net/checksum.h>
14 #include <asm/byteorder.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_rt.h>
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("Xtables: IPv6 Routing Header match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
24 /* Returns 1 if the id is matched by the range, 0 otherwise */
26 segsleft_match(u_int32_t min
, u_int32_t max
, u_int32_t id
, bool invert
)
28 return (id
>= min
&& id
<= max
) ^ invert
;
31 static bool rt_mt6(const struct sk_buff
*skb
, struct xt_action_param
*par
)
33 struct ipv6_rt_hdr _route
;
34 const struct ipv6_rt_hdr
*rh
;
35 const struct ip6t_rt
*rtinfo
= par
->matchinfo
;
38 unsigned int hdrlen
= 0;
40 struct in6_addr _addr
;
41 const struct in6_addr
*ap
;
44 err
= ipv6_find_hdr(skb
, &ptr
, NEXTHDR_ROUTING
, NULL
, NULL
);
51 rh
= skb_header_pointer(skb
, ptr
, sizeof(_route
), &_route
);
57 hdrlen
= ipv6_optlen(rh
);
58 if (skb
->len
- ptr
< hdrlen
) {
59 /* Pcket smaller than its length field */
63 ret
= (segsleft_match(rtinfo
->segsleft
[0], rtinfo
->segsleft
[1],
65 !!(rtinfo
->invflags
& IP6T_RT_INV_SGS
))) &&
66 (!(rtinfo
->flags
& IP6T_RT_LEN
) ||
67 ((rtinfo
->hdrlen
== hdrlen
) ^
68 !!(rtinfo
->invflags
& IP6T_RT_INV_LEN
))) &&
69 (!(rtinfo
->flags
& IP6T_RT_TYP
) ||
70 ((rtinfo
->rt_type
== rh
->type
) ^
71 !!(rtinfo
->invflags
& IP6T_RT_INV_TYP
)));
73 if (ret
&& (rtinfo
->flags
& IP6T_RT_RES
)) {
76 rp
= skb_header_pointer(skb
,
77 ptr
+ offsetof(struct rt0_hdr
,
89 if (!(rtinfo
->flags
& IP6T_RT_FST
)) {
91 } else if (rtinfo
->flags
& IP6T_RT_FST_NSTRICT
) {
92 if (rtinfo
->addrnr
> (unsigned int)((hdrlen
- 8) / 16)) {
98 temp
< (unsigned int)((hdrlen
- 8) / 16);
100 ap
= skb_header_pointer(skb
,
102 + sizeof(struct rt0_hdr
)
103 + temp
* sizeof(_addr
),
112 if (ipv6_addr_equal(ap
, &rtinfo
->addrs
[i
]))
114 if (i
== rtinfo
->addrnr
)
117 if (i
== rtinfo
->addrnr
)
123 if (rtinfo
->addrnr
> (unsigned int)((hdrlen
- 8) / 16)) {
126 for (temp
= 0; temp
< rtinfo
->addrnr
; temp
++) {
127 ap
= skb_header_pointer(skb
,
129 + sizeof(struct rt0_hdr
)
130 + temp
* sizeof(_addr
),
138 if (!ipv6_addr_equal(ap
, &rtinfo
->addrs
[temp
]))
141 if (temp
== rtinfo
->addrnr
&&
142 temp
== (unsigned int)((hdrlen
- 8) / 16))
152 static int rt_mt6_check(const struct xt_mtchk_param
*par
)
154 const struct ip6t_rt
*rtinfo
= par
->matchinfo
;
156 if (rtinfo
->invflags
& ~IP6T_RT_INV_MASK
) {
157 pr_debug("unknown flags %X\n", rtinfo
->invflags
);
160 if ((rtinfo
->flags
& (IP6T_RT_RES
| IP6T_RT_FST_MASK
)) &&
161 (!(rtinfo
->flags
& IP6T_RT_TYP
) ||
162 (rtinfo
->rt_type
!= 0) ||
163 (rtinfo
->invflags
& IP6T_RT_INV_TYP
))) {
164 pr_debug("`--rt-type 0' required before `--rt-0-*'");
171 static struct xt_match rt_mt6_reg __read_mostly
= {
173 .family
= NFPROTO_IPV6
,
175 .matchsize
= sizeof(struct ip6t_rt
),
176 .checkentry
= rt_mt6_check
,
180 static int __init
rt_mt6_init(void)
182 return xt_register_match(&rt_mt6_reg
);
185 static void __exit
rt_mt6_exit(void)
187 xt_unregister_match(&rt_mt6_reg
);
190 module_init(rt_mt6_init
);
191 module_exit(rt_mt6_exit
);