npf_if.c revision 1.2 1 1.2 martin /* $NetBSD: npf_if.c,v 1.2 2013/11/11 15:28:37 martin Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1 rmind * by Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF network interface handling module.
34 1.1 rmind *
35 1.1 rmind * NPF uses its own interface IDs (npf-if-id). When NPF configuration is
36 1.1 rmind * (re)loaded, each required interface name is registered and a matching
37 1.1 rmind * network interface gets an ID assigned. If an interface is not present,
38 1.1 rmind * it gets an ID on attach. Any other interfaces get INACTIVE_ID.
39 1.1 rmind *
40 1.1 rmind * The IDs are mapped synchronously based on interface events which are
41 1.1 rmind * monitored using pfil(9) hooks.
42 1.1 rmind */
43 1.1 rmind
44 1.1 rmind #include <sys/cdefs.h>
45 1.2 martin __KERNEL_RCSID(0, "$NetBSD: npf_if.c,v 1.2 2013/11/11 15:28:37 martin Exp $");
46 1.1 rmind
47 1.1 rmind #ifdef _KERNEL_OPT
48 1.1 rmind #include "pf.h"
49 1.1 rmind #if NPF > 0
50 1.1 rmind #error "NPF and PF are mutually exclusive; please select one"
51 1.1 rmind #endif
52 1.1 rmind #endif
53 1.1 rmind
54 1.1 rmind #include <sys/param.h>
55 1.1 rmind #include <sys/types.h>
56 1.1 rmind #include <sys/kmem.h>
57 1.1 rmind
58 1.1 rmind #include <net/if.h>
59 1.1 rmind
60 1.1 rmind #include "npf_impl.h"
61 1.1 rmind
62 1.1 rmind #define INACTIVE_ID ((u_int)-1)
63 1.1 rmind
64 1.1 rmind typedef struct {
65 1.1 rmind char n_ifname[IFNAMSIZ];
66 1.1 rmind } npf_ifmap_t;
67 1.1 rmind
68 1.1 rmind static npf_ifmap_t npf_ifmap[NPF_MAX_IFMAP] __read_mostly;
69 1.1 rmind static u_int npf_ifmap_cnt __read_mostly;
70 1.1 rmind
71 1.1 rmind /*
72 1.1 rmind * NOTE: IDs start from 1. Zero is reseved for "no interface" and
73 1.1 rmind * (unsigned)-1 for "inactive interface". Therefore, an interface
74 1.1 rmind * can have either INACTIVE_ID or non-zero ID.
75 1.1 rmind */
76 1.1 rmind
77 1.1 rmind static u_int
78 1.1 rmind npf_ifmap_new(void)
79 1.1 rmind {
80 1.1 rmind KASSERT(npf_config_locked_p());
81 1.1 rmind
82 1.1 rmind for (u_int i = 0; i < npf_ifmap_cnt; i++)
83 1.1 rmind if (npf_ifmap[i].n_ifname[0] == '\0')
84 1.1 rmind return i + 1;
85 1.1 rmind
86 1.1 rmind if (npf_ifmap_cnt == NPF_MAX_IFMAP) {
87 1.1 rmind printf("npf_ifmap_new: out of slots; bump NPF_MAX_IFMAP\n");
88 1.1 rmind return INACTIVE_ID;
89 1.1 rmind }
90 1.1 rmind return ++npf_ifmap_cnt;
91 1.1 rmind }
92 1.1 rmind
93 1.1 rmind static u_int
94 1.1 rmind npf_ifmap_lookup(const char *ifname)
95 1.1 rmind {
96 1.1 rmind KASSERT(npf_config_locked_p());
97 1.1 rmind
98 1.1 rmind for (u_int i = 0; i < npf_ifmap_cnt; i++) {
99 1.1 rmind npf_ifmap_t *nim = &npf_ifmap[i];
100 1.1 rmind
101 1.2 martin if (nim->n_ifname[0] && strcmp(nim->n_ifname, ifname) == 0)
102 1.1 rmind return i + 1;
103 1.1 rmind }
104 1.1 rmind return INACTIVE_ID;
105 1.1 rmind }
106 1.1 rmind
107 1.1 rmind u_int
108 1.1 rmind npf_ifmap_register(const char *ifname)
109 1.1 rmind {
110 1.1 rmind npf_ifmap_t *nim;
111 1.1 rmind ifnet_t *ifp;
112 1.1 rmind u_int i;
113 1.1 rmind
114 1.1 rmind npf_config_enter();
115 1.1 rmind if ((i = npf_ifmap_lookup(ifname)) != INACTIVE_ID) {
116 1.1 rmind goto out;
117 1.1 rmind }
118 1.1 rmind if ((i = npf_ifmap_new()) == INACTIVE_ID) {
119 1.1 rmind i = INACTIVE_ID;
120 1.1 rmind goto out;
121 1.1 rmind }
122 1.1 rmind nim = &npf_ifmap[i - 1];
123 1.1 rmind strlcpy(nim->n_ifname, ifname, IFNAMSIZ);
124 1.1 rmind
125 1.1 rmind KERNEL_LOCK(1, NULL);
126 1.1 rmind if ((ifp = ifunit(ifname)) != NULL) {
127 1.1 rmind ifp->if_pf_kif = (void *)(uintptr_t)i;
128 1.1 rmind }
129 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
130 1.1 rmind out:
131 1.1 rmind npf_config_exit();
132 1.1 rmind return i;
133 1.1 rmind }
134 1.1 rmind
135 1.1 rmind void
136 1.1 rmind npf_ifmap_flush(void)
137 1.1 rmind {
138 1.1 rmind ifnet_t *ifp;
139 1.1 rmind
140 1.1 rmind KASSERT(npf_config_locked_p());
141 1.1 rmind
142 1.1 rmind for (u_int i = 0; i < npf_ifmap_cnt; i++) {
143 1.1 rmind npf_ifmap[i].n_ifname[0] = '\0';
144 1.1 rmind }
145 1.1 rmind npf_ifmap_cnt = 0;
146 1.1 rmind
147 1.1 rmind KERNEL_LOCK(1, NULL);
148 1.1 rmind IFNET_FOREACH(ifp) {
149 1.1 rmind ifp->if_pf_kif = (void *)(uintptr_t)INACTIVE_ID;
150 1.1 rmind }
151 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
152 1.1 rmind }
153 1.1 rmind
154 1.1 rmind u_int
155 1.1 rmind npf_ifmap_id(const ifnet_t *ifp)
156 1.1 rmind {
157 1.1 rmind const u_int i = (uintptr_t)ifp->if_pf_kif;
158 1.1 rmind
159 1.1 rmind KASSERT(i == INACTIVE_ID || (i > 0 && i <= npf_ifmap_cnt));
160 1.1 rmind return i;
161 1.1 rmind }
162 1.1 rmind
163 1.1 rmind void
164 1.1 rmind npf_ifmap_attach(ifnet_t *ifp)
165 1.1 rmind {
166 1.1 rmind npf_config_enter();
167 1.1 rmind ifp->if_pf_kif = (void *)(uintptr_t)npf_ifmap_lookup(ifp->if_xname);
168 1.1 rmind npf_config_exit();
169 1.1 rmind }
170 1.1 rmind
171 1.1 rmind void
172 1.1 rmind npf_ifmap_detach(ifnet_t *ifp)
173 1.1 rmind {
174 1.1 rmind npf_config_enter();
175 1.1 rmind ifp->if_pf_kif = (void *)(uintptr_t)INACTIVE_ID; /* diagnostic */
176 1.1 rmind npf_config_exit();
177 1.1 rmind }
178