Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Kernel module to match the bridge port in and
  3 * out device for IP packets coming into contact with a bridge. */
  4
  5/* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
 
 
 
 
  6 */
  7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8
  9#include <linux/if.h>
 10#include <linux/module.h>
 11#include <linux/skbuff.h>
 12#include <linux/netfilter_bridge.h>
 
 13#include <linux/netfilter/x_tables.h>
 14#include <uapi/linux/netfilter/xt_physdev.h>
 15
 16MODULE_LICENSE("GPL");
 17MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
 18MODULE_DESCRIPTION("Xtables: Bridge physical device match");
 19MODULE_ALIAS("ipt_physdev");
 20MODULE_ALIAS("ip6t_physdev");
 21
 22
 23static bool
 24physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
 25{
 
 26	const struct xt_physdev_info *info = par->matchinfo;
 27	const struct net_device *physdev;
 28	unsigned long ret;
 29	const char *indev, *outdev;
 
 30
 31	/* Not a bridged IP packet or no info available yet:
 32	 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
 33	 * the destination device will be a bridge. */
 34	if (!nf_bridge_info_exists(skb)) {
 35		/* Return MATCH if the invert flags of the used options are on */
 36		if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
 37		    !(info->invert & XT_PHYSDEV_OP_BRIDGED))
 38			return false;
 39		if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
 40		    !(info->invert & XT_PHYSDEV_OP_ISIN))
 41			return false;
 42		if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
 43		    !(info->invert & XT_PHYSDEV_OP_ISOUT))
 44			return false;
 45		if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
 46		    !(info->invert & XT_PHYSDEV_OP_IN))
 47			return false;
 48		if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
 49		    !(info->invert & XT_PHYSDEV_OP_OUT))
 50			return false;
 51		return true;
 52	}
 53
 54	physdev = nf_bridge_get_physoutdev(skb);
 55	outdev = physdev ? physdev->name : NULL;
 56
 57	/* This only makes sense in the FORWARD and POSTROUTING chains */
 58	if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
 59	    (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
 
 60		return false;
 61
 62	physdev = nf_bridge_get_physindev(skb);
 63	indev = physdev ? physdev->name : NULL;
 64
 65	if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
 66	    (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
 67	    (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
 68	    (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
 69		return false;
 70
 71	if (!(info->bitmask & XT_PHYSDEV_OP_IN))
 72		goto match_outdev;
 
 
 73
 74	if (indev) {
 75		ret = ifname_compare_aligned(indev, info->physindev,
 76					     info->in_mask);
 77
 78		if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
 79			return false;
 80	}
 81
 82match_outdev:
 83	if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
 84		return true;
 85
 86	if (!outdev)
 87		return false;
 88
 89	ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
 90
 91	return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
 92}
 93
 94static int physdev_mt_check(const struct xt_mtchk_param *par)
 95{
 96	const struct xt_physdev_info *info = par->matchinfo;
 97	static bool brnf_probed __read_mostly;
 98
 99	if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
100	    info->bitmask & ~XT_PHYSDEV_OP_MASK)
101		return -EINVAL;
102	if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
103	    (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
104	     info->invert & XT_PHYSDEV_OP_BRIDGED) &&
105	    par->hook_mask & (1 << NF_INET_LOCAL_OUT)) {
106		pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
107		return -EINVAL;
 
 
 
 
108	}
109
110	if (!brnf_probed) {
111		brnf_probed = true;
112		request_module("br_netfilter");
113	}
114
115	return 0;
116}
117
118static struct xt_match physdev_mt_reg __read_mostly = {
119	.name       = "physdev",
120	.revision   = 0,
121	.family     = NFPROTO_UNSPEC,
122	.checkentry = physdev_mt_check,
123	.match      = physdev_mt,
124	.matchsize  = sizeof(struct xt_physdev_info),
125	.me         = THIS_MODULE,
126};
127
128static int __init physdev_mt_init(void)
129{
130	return xt_register_match(&physdev_mt_reg);
131}
132
133static void __exit physdev_mt_exit(void)
134{
135	xt_unregister_match(&physdev_mt_reg);
136}
137
138module_init(physdev_mt_init);
139module_exit(physdev_mt_exit);
v3.1
 
  1/* Kernel module to match the bridge port in and
  2 * out device for IP packets coming into contact with a bridge. */
  3
  4/* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
  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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 
 11#include <linux/module.h>
 12#include <linux/skbuff.h>
 13#include <linux/netfilter_bridge.h>
 14#include <linux/netfilter/xt_physdev.h>
 15#include <linux/netfilter/x_tables.h>
 
 16
 17MODULE_LICENSE("GPL");
 18MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
 19MODULE_DESCRIPTION("Xtables: Bridge physical device match");
 20MODULE_ALIAS("ipt_physdev");
 21MODULE_ALIAS("ip6t_physdev");
 22
 23
 24static bool
 25physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
 26{
 27	static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
 28	const struct xt_physdev_info *info = par->matchinfo;
 
 29	unsigned long ret;
 30	const char *indev, *outdev;
 31	const struct nf_bridge_info *nf_bridge;
 32
 33	/* Not a bridged IP packet or no info available yet:
 34	 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
 35	 * the destination device will be a bridge. */
 36	if (!(nf_bridge = skb->nf_bridge)) {
 37		/* Return MATCH if the invert flags of the used options are on */
 38		if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
 39		    !(info->invert & XT_PHYSDEV_OP_BRIDGED))
 40			return false;
 41		if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
 42		    !(info->invert & XT_PHYSDEV_OP_ISIN))
 43			return false;
 44		if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
 45		    !(info->invert & XT_PHYSDEV_OP_ISOUT))
 46			return false;
 47		if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
 48		    !(info->invert & XT_PHYSDEV_OP_IN))
 49			return false;
 50		if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
 51		    !(info->invert & XT_PHYSDEV_OP_OUT))
 52			return false;
 53		return true;
 54	}
 55
 
 
 
 56	/* This only makes sense in the FORWARD and POSTROUTING chains */
 57	if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
 58	    (!!(nf_bridge->mask & BRNF_BRIDGED) ^
 59	    !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
 60		return false;
 61
 
 
 
 62	if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
 63	    (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
 64	    (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
 65	    (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
 66		return false;
 67
 68	if (!(info->bitmask & XT_PHYSDEV_OP_IN))
 69		goto match_outdev;
 70	indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
 71	ret = ifname_compare_aligned(indev, info->physindev, info->in_mask);
 72
 73	if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
 74		return false;
 
 
 
 
 
 75
 76match_outdev:
 77	if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
 78		return true;
 79	outdev = nf_bridge->physoutdev ?
 80		 nf_bridge->physoutdev->name : nulldevname;
 
 
 81	ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
 82
 83	return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
 84}
 85
 86static int physdev_mt_check(const struct xt_mtchk_param *par)
 87{
 88	const struct xt_physdev_info *info = par->matchinfo;
 
 89
 90	if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
 91	    info->bitmask & ~XT_PHYSDEV_OP_MASK)
 92		return -EINVAL;
 93	if (info->bitmask & XT_PHYSDEV_OP_OUT &&
 94	    (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
 95	     info->invert & XT_PHYSDEV_OP_BRIDGED) &&
 96	    par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
 97	    (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
 98		pr_info("using --physdev-out in the OUTPUT, FORWARD and "
 99			"POSTROUTING chains for non-bridged traffic is not "
100			"supported anymore.\n");
101		if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
102			return -EINVAL;
103	}
 
 
 
 
 
 
104	return 0;
105}
106
107static struct xt_match physdev_mt_reg __read_mostly = {
108	.name       = "physdev",
109	.revision   = 0,
110	.family     = NFPROTO_UNSPEC,
111	.checkentry = physdev_mt_check,
112	.match      = physdev_mt,
113	.matchsize  = sizeof(struct xt_physdev_info),
114	.me         = THIS_MODULE,
115};
116
117static int __init physdev_mt_init(void)
118{
119	return xt_register_match(&physdev_mt_reg);
120}
121
122static void __exit physdev_mt_exit(void)
123{
124	xt_unregister_match(&physdev_mt_reg);
125}
126
127module_init(physdev_mt_init);
128module_exit(physdev_mt_exit);