16a03354eSMatthew Dillon /*
2*4408d548SBill Yuan * Copyright (c) 2014 - 2018 The DragonFly Project. All rights reserved.
36a03354eSMatthew Dillon *
46a03354eSMatthew Dillon * This code is derived from software contributed to The DragonFly Project
59187b359SBill Yuan * by Bill Yuan <bycn82@dragonflybsd.org>
66a03354eSMatthew Dillon *
76a03354eSMatthew Dillon * Redistribution and use in source and binary forms, with or without
86a03354eSMatthew Dillon * modification, are permitted provided that the following conditions
96a03354eSMatthew Dillon * are met:
106a03354eSMatthew Dillon *
116a03354eSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
126a03354eSMatthew Dillon * notice, this list of conditions and the following disclaimer.
136a03354eSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
146a03354eSMatthew Dillon * notice, this list of conditions and the following disclaimer in
156a03354eSMatthew Dillon * the documentation and/or other materials provided with the
166a03354eSMatthew Dillon * distribution.
176a03354eSMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
186a03354eSMatthew Dillon * contributors may be used to endorse or promote products derived
196a03354eSMatthew Dillon * from this software without specific, prior written permission.
206a03354eSMatthew Dillon *
216a03354eSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
226a03354eSMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
236a03354eSMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
246a03354eSMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
256a03354eSMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
266a03354eSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
276a03354eSMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
286a03354eSMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
296a03354eSMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
306a03354eSMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
316a03354eSMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326a03354eSMatthew Dillon * SUCH DAMAGE.
336a03354eSMatthew Dillon */
346a03354eSMatthew Dillon
356a03354eSMatthew Dillon #include <err.h>
366a03354eSMatthew Dillon #include <errno.h>
376a03354eSMatthew Dillon #include <stdio.h>
386a03354eSMatthew Dillon #include <stdlib.h>
396a03354eSMatthew Dillon #include <string.h>
406a03354eSMatthew Dillon #include <sysexits.h>
416a03354eSMatthew Dillon
426a03354eSMatthew Dillon #include <net/if.h>
436a03354eSMatthew Dillon #include <net/route.h>
446a03354eSMatthew Dillon #include <net/pfil.h>
456a03354eSMatthew Dillon #include <netinet/in.h>
466a03354eSMatthew Dillon
4759ea0e34SBill Yuan #include <net/ipfw3/ip_fw3.h>
489187b359SBill Yuan #include "../../../sbin/ipfw3/ipfw3.h"
496a03354eSMatthew Dillon #include "ipfw3_layer2.h"
506a03354eSMatthew Dillon
516a03354eSMatthew Dillon /*
526a03354eSMatthew Dillon * Returns the number of bits set (from left) in a contiguous bitmask,
536a03354eSMatthew Dillon * or -1 if the mask is not contiguous.
546a03354eSMatthew Dillon * XXX this needs a proper fix.
556a03354eSMatthew Dillon * This effectively works on masks in big-endian (network) format.
566a03354eSMatthew Dillon * when compiled on little endian architectures.
576a03354eSMatthew Dillon *
586a03354eSMatthew Dillon * First bit is bit 7 of the first byte -- note, for MAC addresses,
596a03354eSMatthew Dillon * the first bit on the wire is bit 0 of the first byte.
606a03354eSMatthew Dillon * len is the max length in bits.
616a03354eSMatthew Dillon */
626a03354eSMatthew Dillon static int
contigmask(u_char * p,int len)636a03354eSMatthew Dillon contigmask(u_char *p, int len)
646a03354eSMatthew Dillon {
656a03354eSMatthew Dillon int i, n;
666a03354eSMatthew Dillon for (i = 0; i < len ; i++) {
676a03354eSMatthew Dillon if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */
686a03354eSMatthew Dillon break;
696a03354eSMatthew Dillon }
706a03354eSMatthew Dillon for (n = i + 1; n < len; n++) {
716a03354eSMatthew Dillon if ( (p[n/8] & (1 << (7 - (n%8)))) != 0)
726a03354eSMatthew Dillon return -1; /* mask not contiguous */
736a03354eSMatthew Dillon }
746a03354eSMatthew Dillon return i;
756a03354eSMatthew Dillon }
766a03354eSMatthew Dillon
776a03354eSMatthew Dillon /*
786a03354eSMatthew Dillon * prints a MAC address/mask pair
796a03354eSMatthew Dillon */
806a03354eSMatthew Dillon static void
print_mac(u_char * addr,u_char * mask)816a03354eSMatthew Dillon print_mac(u_char *addr, u_char *mask)
826a03354eSMatthew Dillon {
836a03354eSMatthew Dillon int l = contigmask(mask, 48);
846a03354eSMatthew Dillon
856a03354eSMatthew Dillon if (l == 0) {
866a03354eSMatthew Dillon printf(" any");
876a03354eSMatthew Dillon } else {
886a03354eSMatthew Dillon printf(" %02x:%02x:%02x:%02x:%02x:%02x",
896a03354eSMatthew Dillon addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
906a03354eSMatthew Dillon if (l == -1) {
916a03354eSMatthew Dillon printf("&%02x:%02x:%02x:%02x:%02x:%02x",
926a03354eSMatthew Dillon mask[0], mask[1], mask[2],
936a03354eSMatthew Dillon mask[3], mask[4], mask[5]);
946a03354eSMatthew Dillon } else if (l < 48) {
956a03354eSMatthew Dillon printf("/%d", l);
966a03354eSMatthew Dillon }
976a03354eSMatthew Dillon }
986a03354eSMatthew Dillon }
996a03354eSMatthew Dillon
1006a03354eSMatthew Dillon static void
get_mac_addr_mask(char * p,u_char * addr,u_char * mask)1016a03354eSMatthew Dillon get_mac_addr_mask(char *p, u_char *addr, u_char *mask)
1026a03354eSMatthew Dillon {
1036a03354eSMatthew Dillon int i, l;
1046a03354eSMatthew Dillon
1056a03354eSMatthew Dillon for (i = 0; i < 6; i++)
1066a03354eSMatthew Dillon addr[i] = mask[i] = 0;
1076a03354eSMatthew Dillon if (!strcmp(p, "any"))
1086a03354eSMatthew Dillon return;
1096a03354eSMatthew Dillon
1106a03354eSMatthew Dillon for (i = 0; *p && i < 6; i++, p++) {
1116a03354eSMatthew Dillon addr[i] = strtol(p, &p, 16);
1126a03354eSMatthew Dillon if (*p != ':') /* we start with the mask */
1136a03354eSMatthew Dillon break;
1146a03354eSMatthew Dillon }
1156a03354eSMatthew Dillon if (*p == '/') { /* mask len */
1166a03354eSMatthew Dillon l = strtol(p + 1, &p, 0);
1176a03354eSMatthew Dillon for (i = 0; l > 0; l -= 8, i++)
1186a03354eSMatthew Dillon mask[i] = (l >=8) ? 0xff : (~0) << (8-l);
1196a03354eSMatthew Dillon } else if (*p == '&') { /* mask */
1206a03354eSMatthew Dillon for (i = 0, p++; *p && i < 6; i++, p++) {
1216a03354eSMatthew Dillon mask[i] = strtol(p, &p, 16);
1226a03354eSMatthew Dillon if (*p != ':')
1236a03354eSMatthew Dillon break;
1246a03354eSMatthew Dillon }
1256a03354eSMatthew Dillon } else if (*p == '\0') {
1266a03354eSMatthew Dillon for (i = 0; i < 6; i++)
1276a03354eSMatthew Dillon mask[i] = 0xff;
1286a03354eSMatthew Dillon }
1296a03354eSMatthew Dillon for (i = 0; i < 6; i++)
1306a03354eSMatthew Dillon addr[i] &= mask[i];
1316a03354eSMatthew Dillon }
1326a03354eSMatthew Dillon
1336a03354eSMatthew Dillon void
parse_layer2(ipfw_insn ** cmd,int * ac,char ** av[])1346a03354eSMatthew Dillon parse_layer2(ipfw_insn **cmd, int *ac, char **av[])
1356a03354eSMatthew Dillon {
1366a03354eSMatthew Dillon (*cmd)->opcode = O_LAYER2_LAYER2;
1376a03354eSMatthew Dillon (*cmd)->module = MODULE_LAYER2_ID;
1385f62bb52SBill Yuan (*cmd)->len |= LEN_OF_IPFWINSN;
1396a03354eSMatthew Dillon NEXT_ARG1;
1406a03354eSMatthew Dillon }
1416a03354eSMatthew Dillon
1426a03354eSMatthew Dillon void
parse_mac_from(ipfw_insn ** cmd,int * ac,char ** av[])143ab88ebe4SBill Yuan parse_mac_from(ipfw_insn **cmd, int *ac, char **av[])
144ab88ebe4SBill Yuan {
145ab88ebe4SBill Yuan NEED(*ac, 2, "mac-from src");
146ab88ebe4SBill Yuan NEXT_ARG1;
147ab88ebe4SBill Yuan if (strcmp(**av, "table") == 0) {
148ab88ebe4SBill Yuan NEED(*ac, 2, "mac-from table N");
149ab88ebe4SBill Yuan NEXT_ARG1;
150ab88ebe4SBill Yuan (*cmd)->opcode = O_LAYER2_MAC_SRC_LOOKUP;
151ab88ebe4SBill Yuan (*cmd)->module = MODULE_LAYER2_ID;
1525f62bb52SBill Yuan (*cmd)->len |= F_INSN_SIZE(ipfw_insn);
153ab88ebe4SBill Yuan (*cmd)->arg1 = strtoul(**av, NULL, 10);
154ab88ebe4SBill Yuan NEXT_ARG1;
155ab88ebe4SBill Yuan } else {
156ab88ebe4SBill Yuan (*cmd)->opcode = O_LAYER2_MAC_SRC;
157ab88ebe4SBill Yuan (*cmd)->module = MODULE_LAYER2_ID;
1585f62bb52SBill Yuan (*cmd)->len |= F_INSN_SIZE(ipfw_insn_mac);
159ab88ebe4SBill Yuan ipfw_insn_mac *mac = (ipfw_insn_mac *)(*cmd);
160ab88ebe4SBill Yuan /* src */
161ab88ebe4SBill Yuan get_mac_addr_mask(**av, &(mac->addr[6]), &(mac->mask[6]));
162ab88ebe4SBill Yuan NEXT_ARG1;
163ab88ebe4SBill Yuan }
164ab88ebe4SBill Yuan }
165ab88ebe4SBill Yuan
166ab88ebe4SBill Yuan void
parse_mac_to(ipfw_insn ** cmd,int * ac,char ** av[])167ab88ebe4SBill Yuan parse_mac_to(ipfw_insn **cmd, int *ac, char **av[])
168ab88ebe4SBill Yuan {
169ab88ebe4SBill Yuan NEED(*ac, 2, "mac-to dst");
170ab88ebe4SBill Yuan NEXT_ARG1;
171ab88ebe4SBill Yuan if (strcmp(**av, "table") == 0) {
172ab88ebe4SBill Yuan NEED(*ac, 2, "mac-to table N");
173ab88ebe4SBill Yuan NEXT_ARG1;
174ab88ebe4SBill Yuan (*cmd)->opcode = O_LAYER2_MAC_DST_LOOKUP;
175ab88ebe4SBill Yuan (*cmd)->module = MODULE_LAYER2_ID;
1765f62bb52SBill Yuan (*cmd)->len |= F_INSN_SIZE(ipfw_insn);
177ab88ebe4SBill Yuan (*cmd)->arg1 = strtoul(**av, NULL, 10);
178ab88ebe4SBill Yuan NEXT_ARG1;
179ab88ebe4SBill Yuan } else {
180ab88ebe4SBill Yuan (*cmd)->opcode = O_LAYER2_MAC_DST;
181ab88ebe4SBill Yuan (*cmd)->module = MODULE_LAYER2_ID;
1825f62bb52SBill Yuan (*cmd)->len |= F_INSN_SIZE(ipfw_insn_mac);
183ab88ebe4SBill Yuan ipfw_insn_mac *mac = (ipfw_insn_mac *)(*cmd);
184ab88ebe4SBill Yuan /* dst */
185ab88ebe4SBill Yuan get_mac_addr_mask(**av, mac->addr, mac->mask);
186ab88ebe4SBill Yuan NEXT_ARG1;
187ab88ebe4SBill Yuan }
188ab88ebe4SBill Yuan }
189ab88ebe4SBill Yuan
190ab88ebe4SBill Yuan
191ab88ebe4SBill Yuan void
parse_mac(ipfw_insn ** cmd,int * ac,char ** av[])1926a03354eSMatthew Dillon parse_mac(ipfw_insn **cmd, int *ac, char **av[])
1936a03354eSMatthew Dillon {
1946a03354eSMatthew Dillon NEED(*ac, 3, "mac dst src");
1956a03354eSMatthew Dillon NEXT_ARG1;
1966a03354eSMatthew Dillon (*cmd)->opcode = O_LAYER2_MAC;
1976a03354eSMatthew Dillon (*cmd)->module = MODULE_LAYER2_ID;
1985f62bb52SBill Yuan (*cmd)->len |= F_INSN_SIZE(ipfw_insn_mac);
1996a03354eSMatthew Dillon ipfw_insn_mac *mac = (ipfw_insn_mac *)(*cmd);
2006a03354eSMatthew Dillon get_mac_addr_mask(**av, mac->addr, mac->mask); /* dst */
2016a03354eSMatthew Dillon NEXT_ARG1;
2026a03354eSMatthew Dillon get_mac_addr_mask(**av, &(mac->addr[6]), &(mac->mask[6])); /* src */
2036a03354eSMatthew Dillon NEXT_ARG1;
2046a03354eSMatthew Dillon }
2056a03354eSMatthew Dillon
2066a03354eSMatthew Dillon void
show_layer2(ipfw_insn * cmd,int show_or)2076ce8c93fSBill Yuan show_layer2(ipfw_insn *cmd, int show_or)
2086a03354eSMatthew Dillon {
2096a03354eSMatthew Dillon printf(" layer2");
2106a03354eSMatthew Dillon }
2116a03354eSMatthew Dillon
2126a03354eSMatthew Dillon void
show_mac(ipfw_insn * cmd,int show_or)2136ce8c93fSBill Yuan show_mac(ipfw_insn *cmd, int show_or)
2146a03354eSMatthew Dillon {
2156a03354eSMatthew Dillon ipfw_insn_mac *m = (ipfw_insn_mac *)cmd;
2165f62bb52SBill Yuan if (show_or)
2175f62bb52SBill Yuan printf(" or");
2185f62bb52SBill Yuan else
2196a03354eSMatthew Dillon printf(" mac");
2206a03354eSMatthew Dillon print_mac( m->addr, m->mask);
2216a03354eSMatthew Dillon print_mac( m->addr + 6, m->mask + 6);
2226a03354eSMatthew Dillon }
2236a03354eSMatthew Dillon
2246a03354eSMatthew Dillon void
show_mac_from(ipfw_insn * cmd,int show_or)225ab88ebe4SBill Yuan show_mac_from(ipfw_insn *cmd, int show_or)
226ab88ebe4SBill Yuan {
227ab88ebe4SBill Yuan ipfw_insn_mac *m = (ipfw_insn_mac *)cmd;
2285f62bb52SBill Yuan if (show_or)
2295f62bb52SBill Yuan printf(" or");
2305f62bb52SBill Yuan else
231ab88ebe4SBill Yuan printf(" mac-from");
232ab88ebe4SBill Yuan print_mac( m->addr + 6, m->mask + 6);
233ab88ebe4SBill Yuan }
234ab88ebe4SBill Yuan
235ab88ebe4SBill Yuan void
show_mac_from_lookup(ipfw_insn * cmd,int show_or)236ab88ebe4SBill Yuan show_mac_from_lookup(ipfw_insn *cmd, int show_or)
237ab88ebe4SBill Yuan {
238ab88ebe4SBill Yuan printf(" mac-from table %d", cmd->arg1);
239ab88ebe4SBill Yuan }
240ab88ebe4SBill Yuan
241ab88ebe4SBill Yuan void
show_mac_to(ipfw_insn * cmd,int show_or)242ab88ebe4SBill Yuan show_mac_to(ipfw_insn *cmd, int show_or)
243ab88ebe4SBill Yuan {
244ab88ebe4SBill Yuan ipfw_insn_mac *m = (ipfw_insn_mac *)cmd;
2455f62bb52SBill Yuan if (show_or)
2465f62bb52SBill Yuan printf(" or");
2475f62bb52SBill Yuan else
248ab88ebe4SBill Yuan printf(" mac-to");
249ab88ebe4SBill Yuan print_mac( m->addr, m->mask);
250ab88ebe4SBill Yuan }
251ab88ebe4SBill Yuan
252ab88ebe4SBill Yuan void
show_mac_to_lookup(ipfw_insn * cmd,int show_or)253ab88ebe4SBill Yuan show_mac_to_lookup(ipfw_insn *cmd, int show_or)
254ab88ebe4SBill Yuan {
255ab88ebe4SBill Yuan printf(" mac-to table %d", cmd->arg1);
256ab88ebe4SBill Yuan }
257ab88ebe4SBill Yuan
258ab88ebe4SBill Yuan void
load_module(register_func function,register_keyword keyword)2596a03354eSMatthew Dillon load_module(register_func function, register_keyword keyword)
2606a03354eSMatthew Dillon {
2613b6ebdc3SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_LAYER2, "layer2", FILTER);
2626a03354eSMatthew Dillon function(MODULE_LAYER2_ID, O_LAYER2_LAYER2,
2636a03354eSMatthew Dillon (parser_func)parse_layer2, (shower_func)show_layer2);
2646a03354eSMatthew Dillon
2653b6ebdc3SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_MAC, "mac", FILTER);
2666a03354eSMatthew Dillon function(MODULE_LAYER2_ID, O_LAYER2_MAC,
2676a03354eSMatthew Dillon (parser_func)parse_mac,(shower_func)show_mac);
268ab88ebe4SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_MAC_SRC, "mac-from", FROM);
269ab88ebe4SBill Yuan function(MODULE_LAYER2_ID, O_LAYER2_MAC_SRC,
270ab88ebe4SBill Yuan (parser_func)parse_mac_from,(shower_func)show_mac_from);
271ab88ebe4SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_MAC_SRC_LOOKUP,
27262f8e481SBill Yuan "mac-from-[table]", FROM);
273ab88ebe4SBill Yuan function(MODULE_LAYER2_ID, O_LAYER2_MAC_SRC_LOOKUP,
274ab88ebe4SBill Yuan (parser_func)parse_mac_from,
275ab88ebe4SBill Yuan (shower_func)show_mac_from_lookup);
276ab88ebe4SBill Yuan
277ab88ebe4SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_MAC_DST, "mac-to", TO);
278ab88ebe4SBill Yuan function(MODULE_LAYER2_ID, O_LAYER2_MAC_DST,
279ab88ebe4SBill Yuan (parser_func)parse_mac_to,(shower_func)show_mac_to);
280ab88ebe4SBill Yuan keyword(MODULE_LAYER2_ID, O_LAYER2_MAC_DST_LOOKUP,
28162f8e481SBill Yuan "mac-to-[table]", TO);
282ab88ebe4SBill Yuan function(MODULE_LAYER2_ID, O_LAYER2_MAC_DST_LOOKUP,
283ab88ebe4SBill Yuan (parser_func)parse_mac_to,
284ab88ebe4SBill Yuan (shower_func)show_mac_to_lookup);
2856a03354eSMatthew Dillon }
286