Merge tag 'thermal-6.4-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-block.git] / net / bridge / netfilter / ebt_vlan.c
CommitLineData
1ccea77e 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Description: EBTables 802.1Q match extension kernelspace module.
4 * Authors: Nick Fedchik <nick@fedchik.org.ua>
5 * Bart De Schuymer <bdschuym@pandora.be>
1da177e4
LT
6 */
7
8#include <linux/if_ether.h>
9#include <linux/if_vlan.h>
10#include <linux/module.h>
11#include <linux/moduleparam.h>
18219d3f 12#include <linux/netfilter/x_tables.h>
1da177e4
LT
13#include <linux/netfilter_bridge/ebtables.h>
14#include <linux/netfilter_bridge/ebt_vlan.h>
15
1da177e4
LT
16#define MODULE_VERS "0.6"
17
1da177e4 18MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
f776c4cd 19MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match");
1da177e4
LT
20MODULE_LICENSE("GPL");
21
1da177e4 22#define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
8cc784ee 23#define EXIT_ON_MISMATCH(_MATCH_,_MASK_) {if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return false; }
1da177e4 24
8cc784ee 25static bool
62fc8051 26ebt_vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 27{
f7108a20 28 const struct ebt_vlan_info *info = par->matchinfo;
1da177e4
LT
29
30 unsigned short TCI; /* Whole TCI, given from parsed frame */
31 unsigned short id; /* VLAN ID, given from frame TCI */
32 unsigned char prio; /* user_priority, given from frame TCI */
33 /* VLAN encapsulated Type/Length field, given from orig frame */
47c183fa 34 __be16 encap;
1da177e4 35
df8a39de
JP
36 if (skb_vlan_tag_present(skb)) {
37 TCI = skb_vlan_tag_get(skb);
13937911
JG
38 encap = skb->protocol;
39 } else {
40 const struct vlan_hdr *fp;
41 struct vlan_hdr _frame;
42
43 fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame);
44 if (fp == NULL)
45 return false;
46
47 TCI = ntohs(fp->h_vlan_TCI);
48 encap = fp->h_vlan_encapsulated_proto;
49 }
1da177e4
LT
50
51 /* Tag Control Information (TCI) consists of the following elements:
52 * - User_priority. The user_priority field is three bits in length,
53 * interpreted as a binary number.
54 * - Canonical Format Indicator (CFI). The Canonical Format Indicator
55 * (CFI) is a single bit flag value. Currently ignored.
56 * - VLAN Identifier (VID). The VID is encoded as
7f495ad9
IM
57 * an unsigned binary number.
58 */
1da177e4
LT
59 id = TCI & VLAN_VID_MASK;
60 prio = (TCI >> 13) & 0x7;
1da177e4
LT
61
62 /* Checking VLAN Identifier (VID) */
63 if (GET_BITMASK(EBT_VLAN_ID))
64 EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
65
66 /* Checking user_priority */
67 if (GET_BITMASK(EBT_VLAN_PRIO))
68 EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
69
70 /* Checking Encapsulated Proto (Length/Type) field */
71 if (GET_BITMASK(EBT_VLAN_ENCAP))
72 EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
73
8cc784ee 74 return true;
1da177e4
LT
75}
76
b0f38452 77static int ebt_vlan_mt_check(const struct xt_mtchk_param *par)
1da177e4 78{
9b4fce7a
JE
79 struct ebt_vlan_info *info = par->matchinfo;
80 const struct ebt_entry *e = par->entryinfo;
1da177e4 81
1da177e4
LT
82 /* Is it 802.1Q frame checked? */
83 if (e->ethproto != htons(ETH_P_8021Q)) {
ff67e4e4
JE
84 pr_debug("passed entry proto %2.4X is not 802.1Q (8100)\n",
85 ntohs(e->ethproto));
bd414ee6 86 return -EINVAL;
1da177e4
LT
87 }
88
89 /* Check for bitmask range
7f495ad9
IM
90 * True if even one bit is out of mask
91 */
1da177e4 92 if (info->bitmask & ~EBT_VLAN_MASK) {
ff67e4e4
JE
93 pr_debug("bitmask %2X is out of mask (%2X)\n",
94 info->bitmask, EBT_VLAN_MASK);
bd414ee6 95 return -EINVAL;
1da177e4
LT
96 }
97
98 /* Check for inversion flags range */
99 if (info->invflags & ~EBT_VLAN_MASK) {
ff67e4e4
JE
100 pr_debug("inversion flags %2X is out of mask (%2X)\n",
101 info->invflags, EBT_VLAN_MASK);
bd414ee6 102 return -EINVAL;
1da177e4
LT
103 }
104
105 /* Reserved VLAN ID (VID) values
106 * -----------------------------
9d6f229f 107 * 0 - The null VLAN ID.
1da177e4 108 * 1 - The default Port VID (PVID)
9d6f229f 109 * 0x0FFF - Reserved for implementation use.
7f495ad9
IM
110 * if_vlan.h: VLAN_N_VID 4096.
111 */
1da177e4
LT
112 if (GET_BITMASK(EBT_VLAN_ID)) {
113 if (!!info->id) { /* if id!=0 => check vid range */
b738127d 114 if (info->id > VLAN_N_VID) {
ff67e4e4
JE
115 pr_debug("id %d is out of range (1-4096)\n",
116 info->id);
bd414ee6 117 return -EINVAL;
1da177e4
LT
118 }
119 /* Note: This is valid VLAN-tagged frame point.
9d6f229f 120 * Any value of user_priority are acceptable,
1da177e4 121 * but should be ignored according to 802.1Q Std.
7f495ad9
IM
122 * So we just drop the prio flag.
123 */
1da177e4
LT
124 info->bitmask &= ~EBT_VLAN_PRIO;
125 }
126 /* Else, id=0 (null VLAN ID) => user_priority range (any?) */
127 }
128
129 if (GET_BITMASK(EBT_VLAN_PRIO)) {
130 if ((unsigned char) info->prio > 7) {
ff67e4e4
JE
131 pr_debug("prio %d is out of range (0-7)\n",
132 info->prio);
bd414ee6 133 return -EINVAL;
1da177e4
LT
134 }
135 }
136 /* Check for encapsulated proto range - it is possible to be
137 * any value for u_short range.
7f495ad9
IM
138 * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS
139 */
1da177e4
LT
140 if (GET_BITMASK(EBT_VLAN_ENCAP)) {
141 if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
ff67e4e4
JE
142 pr_debug("encap frame length %d is less than "
143 "minimal\n", ntohs(info->encap));
bd414ee6 144 return -EINVAL;
1da177e4
LT
145 }
146 }
147
bd414ee6 148 return 0;
1da177e4
LT
149}
150
043ef46c
JE
151static struct xt_match ebt_vlan_mt_reg __read_mostly = {
152 .name = "vlan",
001a18d3
JE
153 .revision = 0,
154 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
155 .match = ebt_vlan_mt,
156 .checkentry = ebt_vlan_mt_check,
fc0e3df4 157 .matchsize = sizeof(struct ebt_vlan_info),
1da177e4
LT
158 .me = THIS_MODULE,
159};
160
65b4b4e8 161static int __init ebt_vlan_init(void)
1da177e4 162{
ff67e4e4 163 pr_debug("ebtables 802.1Q extension module v" MODULE_VERS "\n");
043ef46c 164 return xt_register_match(&ebt_vlan_mt_reg);
1da177e4
LT
165}
166
65b4b4e8 167static void __exit ebt_vlan_fini(void)
1da177e4 168{
043ef46c 169 xt_unregister_match(&ebt_vlan_mt_reg);
1da177e4
LT
170}
171
65b4b4e8
AM
172module_init(ebt_vlan_init);
173module_exit(ebt_vlan_fini);