xref: /linux/net/bridge/netfilter/ebt_802_3.c (revision 06ed6aa56ffac9241e03a24649e8d048f8f1b10c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * 802_3
4  *
5  * Author:
6  * Chris Vitale csv@bluetail.com
7  *
8  * May 2003
9  *
10  */
11 #include <linux/module.h>
12 #include <linux/netfilter/x_tables.h>
13 #include <linux/netfilter_bridge/ebtables.h>
14 #include <linux/skbuff.h>
15 #include <uapi/linux/netfilter_bridge/ebt_802_3.h>
16 
17 static struct ebt_802_3_hdr *ebt_802_3_hdr(const struct sk_buff *skb)
18 {
19 	return (struct ebt_802_3_hdr *)skb_mac_header(skb);
20 }
21 
22 static bool
23 ebt_802_3_mt(const struct sk_buff *skb, struct xt_action_param *par)
24 {
25 	const struct ebt_802_3_info *info = par->matchinfo;
26 	const struct ebt_802_3_hdr *hdr = ebt_802_3_hdr(skb);
27 	__be16 type = hdr->llc.ui.ctrl & IS_UI ? hdr->llc.ui.type : hdr->llc.ni.type;
28 
29 	if (info->bitmask & EBT_802_3_SAP) {
30 		if (NF_INVF(info, EBT_802_3_SAP, info->sap != hdr->llc.ui.ssap))
31 			return false;
32 		if (NF_INVF(info, EBT_802_3_SAP, info->sap != hdr->llc.ui.dsap))
33 			return false;
34 	}
35 
36 	if (info->bitmask & EBT_802_3_TYPE) {
37 		if (!(hdr->llc.ui.dsap == CHECK_TYPE && hdr->llc.ui.ssap == CHECK_TYPE))
38 			return false;
39 		if (NF_INVF(info, EBT_802_3_TYPE, info->type != type))
40 			return false;
41 	}
42 
43 	return true;
44 }
45 
46 static int ebt_802_3_mt_check(const struct xt_mtchk_param *par)
47 {
48 	const struct ebt_802_3_info *info = par->matchinfo;
49 
50 	if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
51 		return -EINVAL;
52 
53 	return 0;
54 }
55 
56 static struct xt_match ebt_802_3_mt_reg __read_mostly = {
57 	.name		= "802_3",
58 	.revision	= 0,
59 	.family		= NFPROTO_BRIDGE,
60 	.match		= ebt_802_3_mt,
61 	.checkentry	= ebt_802_3_mt_check,
62 	.matchsize	= sizeof(struct ebt_802_3_info),
63 	.me		= THIS_MODULE,
64 };
65 
66 static int __init ebt_802_3_init(void)
67 {
68 	return xt_register_match(&ebt_802_3_mt_reg);
69 }
70 
71 static void __exit ebt_802_3_fini(void)
72 {
73 	xt_unregister_match(&ebt_802_3_mt_reg);
74 }
75 
76 module_init(ebt_802_3_init);
77 module_exit(ebt_802_3_fini);
78 MODULE_DESCRIPTION("Ebtables: DSAP/SSAP field and SNAP type matching");
79 MODULE_LICENSE("GPL");
80