1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match Hop-by-Hop and Destination parameters. */
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_opts.h>
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("Xtables: IPv6 Hop-By-Hop and Destination Header match");
23 MODULE_ALIAS("ip6t_dst");
28 * 1 -> must drop the packet
29 * 2 -> send ICMP PARM PROB regardless and drop packet
30 * 3 -> Send ICMP if not a multicast address and drop packet
33 * 1 -> can change the routing
35 * 0 -> Pad1 (only 1 byte!)
36 * 1 -> PadN LENGTH info (total length = length + 2)
37 * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
41 static struct xt_match hbh_mt6_reg[] __read_mostly;
44 hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
46 struct ipv6_opt_hdr _optsh;
47 const struct ipv6_opt_hdr *oh;
48 const struct ip6t_opts *optinfo = par->matchinfo;
51 unsigned int hdrlen = 0;
55 const u_int8_t *tp = NULL;
56 const u_int8_t *lp = NULL;
60 err = ipv6_find_hdr(skb, &ptr,
61 (par->match == &hbh_mt6_reg[0]) ?
62 NEXTHDR_HOP : NEXTHDR_DEST, NULL, NULL);
69 oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh);
75 hdrlen = ipv6_optlen(oh);
76 if (skb->len - ptr < hdrlen) {
77 /* Packet smaller than it's length field */
81 pr_debug("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen);
83 pr_debug("len %02X %04X %02X ",
84 optinfo->hdrlen, hdrlen,
85 (!(optinfo->flags & IP6T_OPTS_LEN) ||
86 ((optinfo->hdrlen == hdrlen) ^
87 !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
89 ret = (!(optinfo->flags & IP6T_OPTS_LEN) ||
90 ((optinfo->hdrlen == hdrlen) ^
91 !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
95 if (!(optinfo->flags & IP6T_OPTS_OPTS)) {
99 pr_debug("#%d ", optinfo->optsnr);
100 for (temp = 0; temp < optinfo->optsnr; temp++) {
101 /* type field exists ? */
104 tp = skb_header_pointer(skb, ptr, sizeof(_opttype),
110 if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) {
111 pr_debug("Tbad %02X %02X\n", *tp,
112 (optinfo->opts[temp] & 0xFF00) >> 8);
121 /* length field exists ? */
124 lp = skb_header_pointer(skb, ptr + 1,
129 spec_len = optinfo->opts[temp] & 0x00FF;
131 if (spec_len != 0x00FF && spec_len != *lp) {
132 pr_debug("Lbad %02X %04X\n", *lp,
143 /* Step to the next */
144 pr_debug("len%04X\n", optlen);
146 if ((ptr > skb->len - optlen || hdrlen < optlen) &&
147 temp < optinfo->optsnr - 1) {
148 pr_debug("new pointer is too large!\n");
154 if (temp == optinfo->optsnr)
163 static int hbh_mt6_check(const struct xt_mtchk_param *par)
165 const struct ip6t_opts *optsinfo = par->matchinfo;
167 if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
168 pr_debug("unknown flags %X\n", optsinfo->invflags);
172 if (optsinfo->flags & IP6T_OPTS_NSTRICT) {
173 pr_debug("Not strict - not implemented");
180 static struct xt_match hbh_mt6_reg[] __read_mostly = {
182 /* Note, hbh_mt6 relies on the order of hbh_mt6_reg */
184 .family = NFPROTO_IPV6,
186 .matchsize = sizeof(struct ip6t_opts),
187 .checkentry = hbh_mt6_check,
192 .family = NFPROTO_IPV6,
194 .matchsize = sizeof(struct ip6t_opts),
195 .checkentry = hbh_mt6_check,
200 static int __init hbh_mt6_init(void)
202 return xt_register_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg));
205 static void __exit hbh_mt6_exit(void)
207 xt_unregister_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg));
210 module_init(hbh_mt6_init);
211 module_exit(hbh_mt6_exit);