Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8
  3 *
  4 * (C) 2002 by Harald Welte <laforge@netfilter.org>
  5 * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
  6 *
 
 
 
 
  7 * See RFC2474 for a description of the DSCP field within the IP Header.
  8*/
  9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 10#include <linux/module.h>
 11#include <linux/skbuff.h>
 12#include <linux/ip.h>
 13#include <linux/ipv6.h>
 14#include <net/dsfield.h>
 15
 16#include <linux/netfilter/x_tables.h>
 17#include <linux/netfilter/xt_DSCP.h>
 18
 19MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
 20MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification");
 21MODULE_LICENSE("GPL");
 22MODULE_ALIAS("ipt_DSCP");
 23MODULE_ALIAS("ip6t_DSCP");
 24MODULE_ALIAS("ipt_TOS");
 25MODULE_ALIAS("ip6t_TOS");
 26
 27static unsigned int
 28dscp_tg(struct sk_buff *skb, const struct xt_action_param *par)
 29{
 30	const struct xt_DSCP_info *dinfo = par->targinfo;
 31	u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
 32
 33	if (dscp != dinfo->dscp) {
 34		if (skb_ensure_writable(skb, sizeof(struct iphdr)))
 35			return NF_DROP;
 36
 37		ipv4_change_dsfield(ip_hdr(skb),
 38				    (__force __u8)(~XT_DSCP_MASK),
 39				    dinfo->dscp << XT_DSCP_SHIFT);
 40
 41	}
 42	return XT_CONTINUE;
 43}
 44
 45static unsigned int
 46dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)
 47{
 48	const struct xt_DSCP_info *dinfo = par->targinfo;
 49	u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
 50
 51	if (dscp != dinfo->dscp) {
 52		if (skb_ensure_writable(skb, sizeof(struct ipv6hdr)))
 53			return NF_DROP;
 54
 55		ipv6_change_dsfield(ipv6_hdr(skb),
 56				    (__force __u8)(~XT_DSCP_MASK),
 57				    dinfo->dscp << XT_DSCP_SHIFT);
 58	}
 59	return XT_CONTINUE;
 60}
 61
 62static int dscp_tg_check(const struct xt_tgchk_param *par)
 63{
 64	const struct xt_DSCP_info *info = par->targinfo;
 65
 66	if (info->dscp > XT_DSCP_MAX)
 67		return -EDOM;
 68	return 0;
 69}
 70
 71static unsigned int
 72tos_tg(struct sk_buff *skb, const struct xt_action_param *par)
 73{
 74	const struct xt_tos_target_info *info = par->targinfo;
 75	struct iphdr *iph = ip_hdr(skb);
 76	u_int8_t orig, nv;
 77
 78	orig = ipv4_get_dsfield(iph);
 79	nv   = (orig & ~info->tos_mask) ^ info->tos_value;
 80
 81	if (orig != nv) {
 82		if (skb_ensure_writable(skb, sizeof(struct iphdr)))
 83			return NF_DROP;
 84		iph = ip_hdr(skb);
 85		ipv4_change_dsfield(iph, 0, nv);
 86	}
 87
 88	return XT_CONTINUE;
 89}
 90
 91static unsigned int
 92tos_tg6(struct sk_buff *skb, const struct xt_action_param *par)
 93{
 94	const struct xt_tos_target_info *info = par->targinfo;
 95	struct ipv6hdr *iph = ipv6_hdr(skb);
 96	u_int8_t orig, nv;
 97
 98	orig = ipv6_get_dsfield(iph);
 99	nv   = (orig & ~info->tos_mask) ^ info->tos_value;
100
101	if (orig != nv) {
102		if (skb_ensure_writable(skb, sizeof(struct iphdr)))
103			return NF_DROP;
104		iph = ipv6_hdr(skb);
105		ipv6_change_dsfield(iph, 0, nv);
106	}
107
108	return XT_CONTINUE;
109}
110
111static struct xt_target dscp_tg_reg[] __read_mostly = {
112	{
113		.name		= "DSCP",
114		.family		= NFPROTO_IPV4,
115		.checkentry	= dscp_tg_check,
116		.target		= dscp_tg,
117		.targetsize	= sizeof(struct xt_DSCP_info),
118		.table		= "mangle",
119		.me		= THIS_MODULE,
120	},
121	{
122		.name		= "DSCP",
123		.family		= NFPROTO_IPV6,
124		.checkentry	= dscp_tg_check,
125		.target		= dscp_tg6,
126		.targetsize	= sizeof(struct xt_DSCP_info),
127		.table		= "mangle",
128		.me		= THIS_MODULE,
129	},
130	{
131		.name		= "TOS",
132		.revision	= 1,
133		.family		= NFPROTO_IPV4,
134		.table		= "mangle",
135		.target		= tos_tg,
136		.targetsize	= sizeof(struct xt_tos_target_info),
137		.me		= THIS_MODULE,
138	},
139	{
140		.name		= "TOS",
141		.revision	= 1,
142		.family		= NFPROTO_IPV6,
143		.table		= "mangle",
144		.target		= tos_tg6,
145		.targetsize	= sizeof(struct xt_tos_target_info),
146		.me		= THIS_MODULE,
147	},
148};
149
150static int __init dscp_tg_init(void)
151{
152	return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
153}
154
155static void __exit dscp_tg_exit(void)
156{
157	xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
158}
159
160module_init(dscp_tg_init);
161module_exit(dscp_tg_exit);
v4.17
 
  1/* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8
  2 *
  3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
  4 * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 * See RFC2474 for a description of the DSCP field within the IP Header.
 11*/
 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13#include <linux/module.h>
 14#include <linux/skbuff.h>
 15#include <linux/ip.h>
 16#include <linux/ipv6.h>
 17#include <net/dsfield.h>
 18
 19#include <linux/netfilter/x_tables.h>
 20#include <linux/netfilter/xt_DSCP.h>
 21
 22MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
 23MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification");
 24MODULE_LICENSE("GPL");
 25MODULE_ALIAS("ipt_DSCP");
 26MODULE_ALIAS("ip6t_DSCP");
 27MODULE_ALIAS("ipt_TOS");
 28MODULE_ALIAS("ip6t_TOS");
 29
 30static unsigned int
 31dscp_tg(struct sk_buff *skb, const struct xt_action_param *par)
 32{
 33	const struct xt_DSCP_info *dinfo = par->targinfo;
 34	u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
 35
 36	if (dscp != dinfo->dscp) {
 37		if (!skb_make_writable(skb, sizeof(struct iphdr)))
 38			return NF_DROP;
 39
 40		ipv4_change_dsfield(ip_hdr(skb),
 41				    (__force __u8)(~XT_DSCP_MASK),
 42				    dinfo->dscp << XT_DSCP_SHIFT);
 43
 44	}
 45	return XT_CONTINUE;
 46}
 47
 48static unsigned int
 49dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)
 50{
 51	const struct xt_DSCP_info *dinfo = par->targinfo;
 52	u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
 53
 54	if (dscp != dinfo->dscp) {
 55		if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
 56			return NF_DROP;
 57
 58		ipv6_change_dsfield(ipv6_hdr(skb),
 59				    (__force __u8)(~XT_DSCP_MASK),
 60				    dinfo->dscp << XT_DSCP_SHIFT);
 61	}
 62	return XT_CONTINUE;
 63}
 64
 65static int dscp_tg_check(const struct xt_tgchk_param *par)
 66{
 67	const struct xt_DSCP_info *info = par->targinfo;
 68
 69	if (info->dscp > XT_DSCP_MAX)
 70		return -EDOM;
 71	return 0;
 72}
 73
 74static unsigned int
 75tos_tg(struct sk_buff *skb, const struct xt_action_param *par)
 76{
 77	const struct xt_tos_target_info *info = par->targinfo;
 78	struct iphdr *iph = ip_hdr(skb);
 79	u_int8_t orig, nv;
 80
 81	orig = ipv4_get_dsfield(iph);
 82	nv   = (orig & ~info->tos_mask) ^ info->tos_value;
 83
 84	if (orig != nv) {
 85		if (!skb_make_writable(skb, sizeof(struct iphdr)))
 86			return NF_DROP;
 87		iph = ip_hdr(skb);
 88		ipv4_change_dsfield(iph, 0, nv);
 89	}
 90
 91	return XT_CONTINUE;
 92}
 93
 94static unsigned int
 95tos_tg6(struct sk_buff *skb, const struct xt_action_param *par)
 96{
 97	const struct xt_tos_target_info *info = par->targinfo;
 98	struct ipv6hdr *iph = ipv6_hdr(skb);
 99	u_int8_t orig, nv;
100
101	orig = ipv6_get_dsfield(iph);
102	nv   = (orig & ~info->tos_mask) ^ info->tos_value;
103
104	if (orig != nv) {
105		if (!skb_make_writable(skb, sizeof(struct iphdr)))
106			return NF_DROP;
107		iph = ipv6_hdr(skb);
108		ipv6_change_dsfield(iph, 0, nv);
109	}
110
111	return XT_CONTINUE;
112}
113
114static struct xt_target dscp_tg_reg[] __read_mostly = {
115	{
116		.name		= "DSCP",
117		.family		= NFPROTO_IPV4,
118		.checkentry	= dscp_tg_check,
119		.target		= dscp_tg,
120		.targetsize	= sizeof(struct xt_DSCP_info),
121		.table		= "mangle",
122		.me		= THIS_MODULE,
123	},
124	{
125		.name		= "DSCP",
126		.family		= NFPROTO_IPV6,
127		.checkentry	= dscp_tg_check,
128		.target		= dscp_tg6,
129		.targetsize	= sizeof(struct xt_DSCP_info),
130		.table		= "mangle",
131		.me		= THIS_MODULE,
132	},
133	{
134		.name		= "TOS",
135		.revision	= 1,
136		.family		= NFPROTO_IPV4,
137		.table		= "mangle",
138		.target		= tos_tg,
139		.targetsize	= sizeof(struct xt_tos_target_info),
140		.me		= THIS_MODULE,
141	},
142	{
143		.name		= "TOS",
144		.revision	= 1,
145		.family		= NFPROTO_IPV6,
146		.table		= "mangle",
147		.target		= tos_tg6,
148		.targetsize	= sizeof(struct xt_tos_target_info),
149		.me		= THIS_MODULE,
150	},
151};
152
153static int __init dscp_tg_init(void)
154{
155	return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
156}
157
158static void __exit dscp_tg_exit(void)
159{
160	xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
161}
162
163module_init(dscp_tg_init);
164module_exit(dscp_tg_exit);