projects/linux·blob master · refs

103 lines · 2.5 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/* Kernel module to match IPComp parameters for IPv4 and IPv6
3 *
4 * Copyright (C) 2013 WindRiver
5 *
6 * Author:
7 * Fan Du <[email protected]>
8 *
9 * Based on:
10 * net/netfilter/xt_esp.c
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/in.h>
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/ip.h>
18
19#include <linux/netfilter/xt_ipcomp.h>
20#include <linux/netfilter/x_tables.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Fan Du <[email protected]>");
24MODULE_DESCRIPTION("Xtables: IPv4/6 IPsec-IPComp SPI match");
25MODULE_ALIAS("ipt_ipcomp");
26MODULE_ALIAS("ip6t_ipcomp");
27
28/* Returns 1 if the spi is matched by the range, 0 otherwise */
29static inline bool
30spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
31{
32 return (spi >= min && spi <= max) ^ invert;
33}
34
35static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
36{
37 struct ip_comp_hdr _comphdr;
38 const struct ip_comp_hdr *chdr;
39 const struct xt_ipcomp *compinfo = par->matchinfo;
40
41 /* Must not be a fragment. */
42 if (par->fragoff != 0)
43 return false;
44
45 chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
46 if (chdr == NULL) {
47 /* We've been asked to examine this packet, and we
48 * can't. Hence, no choice but to drop.
49 */
50 par->hotdrop = true;
51 return false;
52 }
53
54 return spi_match(compinfo->spis[0], compinfo->spis[1],
55 ntohs(chdr->cpi),
56 !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
57}
58
59static int comp_mt_check(const struct xt_mtchk_param *par)
60{
61 const struct xt_ipcomp *compinfo = par->matchinfo;
62
63 /* Must specify no unknown invflags */
64 if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
65 pr_info_ratelimited("unknown flags %X\n", compinfo->invflags);
66 return -EINVAL;
67 }
68 return 0;
69}
70
71static struct xt_match comp_mt_reg[] __read_mostly = {
72 {
73 .name = "ipcomp",
74 .family = NFPROTO_IPV4,
75 .match = comp_mt,
76 .matchsize = sizeof(struct xt_ipcomp),
77 .proto = IPPROTO_COMP,
78 .checkentry = comp_mt_check,
79 .me = THIS_MODULE,
80 },
81 {
82 .name = "ipcomp",
83 .family = NFPROTO_IPV6,
84 .match = comp_mt,
85 .matchsize = sizeof(struct xt_ipcomp),
86 .proto = IPPROTO_COMP,
87 .checkentry = comp_mt_check,
88 .me = THIS_MODULE,
89 },
90};
91
92static int __init comp_mt_init(void)
93{
94 return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
95}
96
97static void __exit comp_mt_exit(void)
98{
99 xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
100}
101
102module_init(comp_mt_init);
103module_exit(comp_mt_exit);