ipfw2 - Rename FreeBSD ipfw port to ipfw3
[dragonfly.git] / sys / net / ipfw3_layer2 / ip_fw3_layer2.c
1 /*
2  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Bill Yuan <bycn82@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socketvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/systimer.h>
42 #include <sys/thread2.h>
43
44 #include <net/ethernet.h>
45 #include <net/netmsg2.h>
46 #include <net/netisr2.h>
47 #include <net/route.h>
48
49 #include <netinet/in_var.h>
50 #include <netinet/ip_var.h>
51
52 #include <net/ipfw3/ip_fw.h>
53
54 #include "ip_fw3_layer2.h"
55
56
57 void check_layer2(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
58                 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len);
59 void check_mac(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
60                 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len);
61
62 void
63 check_layer2(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
64                 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len)
65 {
66         *cmd_val = ((*args)->eh != NULL);
67         *cmd_ctl = IP_FW_CTL_NO;
68 }
69
70 void
71 check_mac(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args,
72                 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len)
73 {
74         *cmd_ctl = IP_FW_CTL_NO;
75         if ((*args)->eh != NULL) {
76                 uint32_t *want = (uint32_t *)((ipfw_insn_mac *)cmd)->addr;
77                 uint32_t *mask = (uint32_t *)((ipfw_insn_mac *)cmd)->mask;
78                 uint32_t *hdr = (uint32_t *)(*args)->eh;
79                 *cmd_val =
80                         (want[0] == (hdr[0] & mask[0]) &&
81                          want[1] == (hdr[1] & mask[1]) &&
82                          want[2] == (hdr[2] & mask[2]));
83         } else {
84                 *cmd_val = IP_FW_NOT_MATCH;
85         }
86 }
87
88 static int
89 ipfw3_layer2_init(void)
90 {
91         register_ipfw_module(MODULE_LAYER2_ID, MODULE_LAYER2_NAME);
92         register_ipfw_filter_funcs(MODULE_LAYER2_ID,
93                         O_LAYER2_LAYER2, (filter_func)check_layer2);
94         register_ipfw_filter_funcs(MODULE_LAYER2_ID,
95                         O_LAYER2_MAC, (filter_func)check_mac);
96         return 0;
97 }
98
99 static int
100 ipfw3_layer2_stop(void)
101 {
102         return unregister_ipfw_module(MODULE_LAYER2_ID);
103 }
104
105 static int
106 ipfw3_layer2_modevent(module_t mod, int type, void *data)
107 {
108         switch (type) {
109                 case MOD_LOAD:
110                         return ipfw3_layer2_init();
111                 case MOD_UNLOAD:
112                         return ipfw3_layer2_stop();
113                 default:
114                         break;
115         }
116         return 0;
117 }
118
119 static moduledata_t ipfw3_layer2_mod = {
120         "ipfw3_layer2",
121         ipfw3_layer2_modevent,
122         NULL
123 };
124 DECLARE_MODULE(ipfw3_layer2, ipfw3_layer2_mod, SI_SUB_PROTO_END, SI_ORDER_ANY);
125 MODULE_DEPEND(ipfw3_layer2, ipfw3_basic, 1, 1, 1);
126 MODULE_VERSION(ipfw3_layer2, 1);