Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
v5.14.15
  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 <fan.du@windriver.com>
  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 <fan.du@windriver.com>");
 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	bool r;
 33	pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
 34		 invert ? '!' : ' ', min, spi, max);
 35	r = (spi >= min && spi <= max) ^ invert;
 36	pr_debug(" result %s\n", r ? "PASS" : "FAILED");
 37	return r;
 38}
 39
 40static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
 41{
 42	struct ip_comp_hdr _comphdr;
 43	const struct ip_comp_hdr *chdr;
 44	const struct xt_ipcomp *compinfo = par->matchinfo;
 45
 46	/* Must not be a fragment. */
 47	if (par->fragoff != 0)
 48		return false;
 49
 50	chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
 51	if (chdr == NULL) {
 52		/* We've been asked to examine this packet, and we
 53		 * can't.  Hence, no choice but to drop.
 54		 */
 55		pr_debug("Dropping evil IPComp tinygram.\n");
 56		par->hotdrop = true;
 57		return false;
 58	}
 59
 60	return spi_match(compinfo->spis[0], compinfo->spis[1],
 61			 ntohs(chdr->cpi),
 62			 !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
 63}
 64
 65static int comp_mt_check(const struct xt_mtchk_param *par)
 66{
 67	const struct xt_ipcomp *compinfo = par->matchinfo;
 68
 69	/* Must specify no unknown invflags */
 70	if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
 71		pr_info_ratelimited("unknown flags %X\n", compinfo->invflags);
 72		return -EINVAL;
 73	}
 74	return 0;
 75}
 76
 77static struct xt_match comp_mt_reg[] __read_mostly = {
 78	{
 79		.name		= "ipcomp",
 80		.family		= NFPROTO_IPV4,
 81		.match		= comp_mt,
 82		.matchsize	= sizeof(struct xt_ipcomp),
 83		.proto		= IPPROTO_COMP,
 84		.checkentry	= comp_mt_check,
 85		.me		= THIS_MODULE,
 86	},
 87	{
 88		.name		= "ipcomp",
 89		.family		= NFPROTO_IPV6,
 90		.match		= comp_mt,
 91		.matchsize	= sizeof(struct xt_ipcomp),
 92		.proto		= IPPROTO_COMP,
 93		.checkentry	= comp_mt_check,
 94		.me		= THIS_MODULE,
 95	},
 96};
 97
 98static int __init comp_mt_init(void)
 99{
100	return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
101}
102
103static void __exit comp_mt_exit(void)
104{
105	xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
106}
107
108module_init(comp_mt_init);
109module_exit(comp_mt_exit);
v4.6
 
  1/*  Kernel module to match IPComp parameters for IPv4 and IPv6
  2 *
  3 *  Copyright (C) 2013 WindRiver
  4 *
  5 *  Author:
  6 *  Fan Du <fan.du@windriver.com>
  7 *
  8 *  Based on:
  9 *  net/netfilter/xt_esp.c
 10 *
 11 *  This program is free software; you can redistribute it and/or
 12 *  modify it under the terms of the GNU General Public License
 13 *  as published by the Free Software Foundation; either version
 14 *  2 of the License, or (at your option) any later version.
 15 */
 16
 17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 18#include <linux/in.h>
 19#include <linux/module.h>
 20#include <linux/skbuff.h>
 21#include <linux/ip.h>
 22
 23#include <linux/netfilter/xt_ipcomp.h>
 24#include <linux/netfilter/x_tables.h>
 25
 26MODULE_LICENSE("GPL");
 27MODULE_AUTHOR("Fan Du <fan.du@windriver.com>");
 28MODULE_DESCRIPTION("Xtables: IPv4/6 IPsec-IPComp SPI match");
 
 
 29
 30/* Returns 1 if the spi is matched by the range, 0 otherwise */
 31static inline bool
 32spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
 33{
 34	bool r;
 35	pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
 36		 invert ? '!' : ' ', min, spi, max);
 37	r = (spi >= min && spi <= max) ^ invert;
 38	pr_debug(" result %s\n", r ? "PASS" : "FAILED");
 39	return r;
 40}
 41
 42static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
 43{
 44	struct ip_comp_hdr _comphdr;
 45	const struct ip_comp_hdr *chdr;
 46	const struct xt_ipcomp *compinfo = par->matchinfo;
 47
 48	/* Must not be a fragment. */
 49	if (par->fragoff != 0)
 50		return false;
 51
 52	chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
 53	if (chdr == NULL) {
 54		/* We've been asked to examine this packet, and we
 55		 * can't.  Hence, no choice but to drop.
 56		 */
 57		pr_debug("Dropping evil IPComp tinygram.\n");
 58		par->hotdrop = true;
 59		return 0;
 60	}
 61
 62	return spi_match(compinfo->spis[0], compinfo->spis[1],
 63			 ntohs(chdr->cpi),
 64			 !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
 65}
 66
 67static int comp_mt_check(const struct xt_mtchk_param *par)
 68{
 69	const struct xt_ipcomp *compinfo = par->matchinfo;
 70
 71	/* Must specify no unknown invflags */
 72	if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
 73		pr_err("unknown flags %X\n", compinfo->invflags);
 74		return -EINVAL;
 75	}
 76	return 0;
 77}
 78
 79static struct xt_match comp_mt_reg[] __read_mostly = {
 80	{
 81		.name		= "ipcomp",
 82		.family		= NFPROTO_IPV4,
 83		.match		= comp_mt,
 84		.matchsize	= sizeof(struct xt_ipcomp),
 85		.proto		= IPPROTO_COMP,
 86		.checkentry	= comp_mt_check,
 87		.me		= THIS_MODULE,
 88	},
 89	{
 90		.name		= "ipcomp",
 91		.family		= NFPROTO_IPV6,
 92		.match		= comp_mt,
 93		.matchsize	= sizeof(struct xt_ipcomp),
 94		.proto		= IPPROTO_COMP,
 95		.checkentry	= comp_mt_check,
 96		.me		= THIS_MODULE,
 97	},
 98};
 99
100static int __init comp_mt_init(void)
101{
102	return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
103}
104
105static void __exit comp_mt_exit(void)
106{
107	xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
108}
109
110module_init(comp_mt_init);
111module_exit(comp_mt_exit);