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