npf_ifaddr.c revision 1.3 1 1.3 ozaki /* $NetBSD: npf_ifaddr.c,v 1.3 2017/12/11 03:25:46 ozaki-r Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2014 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
36 1.1 rmind #include <sys/cdefs.h>
37 1.3 ozaki __KERNEL_RCSID(0, "$NetBSD: npf_ifaddr.c,v 1.3 2017/12/11 03:25:46 ozaki-r Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/types.h>
41 1.1 rmind #include <sys/kmem.h>
42 1.1 rmind
43 1.1 rmind #include <net/if.h>
44 1.1 rmind #include <netinet/in.h>
45 1.1 rmind #include <netinet6/in6_var.h>
46 1.1 rmind
47 1.1 rmind #include "npf_impl.h"
48 1.1 rmind
49 1.1 rmind static npf_table_t *
50 1.1 rmind lookup_ifnet_table(npf_t *npf, ifnet_t *ifp)
51 1.1 rmind {
52 1.1 rmind const npf_ifops_t *ifops = npf->ifops;
53 1.1 rmind char tname[NPF_TABLE_MAXNAMELEN];
54 1.1 rmind npf_tableset_t *ts;
55 1.1 rmind const char *ifname;
56 1.1 rmind npf_table_t *t;
57 1.1 rmind u_int tid;
58 1.1 rmind
59 1.1 rmind /* Get the interface name and prefix it. */
60 1.1 rmind ifname = ifops->getname(ifp);
61 1.1 rmind snprintf(tname, sizeof(tname), ".ifnet-%s", ifname);
62 1.1 rmind
63 1.1 rmind KERNEL_LOCK(1, NULL);
64 1.1 rmind npf_config_enter(npf);
65 1.1 rmind ts = npf_config_tableset(npf);
66 1.1 rmind
67 1.1 rmind /*
68 1.1 rmind * Check whether this interface is of any interest to us.
69 1.1 rmind */
70 1.1 rmind t = npf_tableset_getbyname(ts, tname);
71 1.1 rmind if (!t) {
72 1.1 rmind goto out;
73 1.1 rmind }
74 1.1 rmind tid = npf_table_getid(t);
75 1.1 rmind
76 1.1 rmind /* Create a new NPF table for the interface. */
77 1.1 rmind t = npf_table_create(tname, tid, NPF_TABLE_HASH, NULL, 16);
78 1.1 rmind if (!t) {
79 1.1 rmind goto out;
80 1.1 rmind }
81 1.1 rmind return t;
82 1.1 rmind out:
83 1.1 rmind npf_config_exit(npf);
84 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
85 1.1 rmind return NULL;
86 1.1 rmind }
87 1.1 rmind
88 1.1 rmind static void
89 1.1 rmind replace_ifnet_table(npf_t *npf, npf_table_t *newt)
90 1.1 rmind {
91 1.1 rmind npf_tableset_t *ts = npf_config_tableset(npf);
92 1.1 rmind npf_table_t *oldt;
93 1.1 rmind
94 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
95 1.1 rmind
96 1.1 rmind /*
97 1.1 rmind * Finally, swap the tables and issue a sync barrier.
98 1.1 rmind */
99 1.1 rmind oldt = npf_tableset_swap(ts, newt);
100 1.1 rmind npf_config_sync(npf);
101 1.1 rmind npf_config_exit(npf);
102 1.1 rmind
103 1.1 rmind /* At this point, it is safe to destroy the old table. */
104 1.1 rmind npf_table_destroy(oldt);
105 1.1 rmind }
106 1.1 rmind
107 1.1 rmind void
108 1.1 rmind npf_ifaddr_sync(npf_t *npf, ifnet_t *ifp)
109 1.1 rmind {
110 1.1 rmind npf_table_t *t;
111 1.1 rmind struct ifaddr *ifa;
112 1.1 rmind
113 1.1 rmind /*
114 1.1 rmind * First, check whether this interface is of any interest to us.
115 1.1 rmind *
116 1.1 rmind * => Acquires npf-config-lock and kernel-lock on success.
117 1.1 rmind */
118 1.1 rmind t = lookup_ifnet_table(npf, ifp);
119 1.1 rmind if (!t)
120 1.1 rmind return;
121 1.1 rmind
122 1.1 rmind /*
123 1.1 rmind * Populate the table with the interface addresses.
124 1.1 rmind * Note: currently, this list is protected by the kernel-lock.
125 1.1 rmind */
126 1.1 rmind IFADDR_FOREACH(ifa, ifp) {
127 1.1 rmind struct sockaddr *sa = ifa->ifa_addr;
128 1.1 rmind const void *p = NULL;
129 1.1 rmind int alen = 0;
130 1.1 rmind
131 1.1 rmind if (sa->sa_family == AF_INET) {
132 1.1 rmind const struct sockaddr_in *sin4 = satosin(sa);
133 1.1 rmind alen = sizeof(struct in_addr);
134 1.1 rmind p = &sin4->sin_addr;
135 1.1 rmind }
136 1.1 rmind if (sa->sa_family == AF_INET6) {
137 1.1 rmind const struct sockaddr_in6 *sin6 = satosin6(sa);
138 1.1 rmind alen = sizeof(struct in6_addr);
139 1.1 rmind p = &sin6->sin6_addr;
140 1.1 rmind }
141 1.1 rmind if (alen) {
142 1.1 rmind npf_addr_t addr;
143 1.1 rmind memcpy(&addr, p, alen);
144 1.1 rmind npf_table_insert(t, alen, &addr, NPF_NO_NETMASK);
145 1.1 rmind }
146 1.1 rmind }
147 1.1 rmind
148 1.1 rmind /* Publish the new table. */
149 1.1 rmind replace_ifnet_table(npf, t);
150 1.1 rmind }
151 1.1 rmind
152 1.1 rmind void
153 1.1 rmind npf_ifaddr_flush(npf_t *npf, ifnet_t *ifp)
154 1.1 rmind {
155 1.1 rmind npf_table_t *t;
156 1.1 rmind
157 1.1 rmind /*
158 1.1 rmind * Flush: just load an empty table.
159 1.1 rmind */
160 1.1 rmind t = lookup_ifnet_table(npf, ifp);
161 1.1 rmind if (!t) {
162 1.1 rmind return;
163 1.1 rmind }
164 1.1 rmind replace_ifnet_table(npf, t);
165 1.1 rmind }
166 1.2 rmind
167 1.2 rmind void
168 1.2 rmind npf_ifaddr_syncall(npf_t *npf)
169 1.2 rmind {
170 1.2 rmind ifnet_t *ifp;
171 1.2 rmind
172 1.2 rmind KERNEL_LOCK(1, NULL);
173 1.3 ozaki IFNET_GLOBAL_LOCK();
174 1.2 rmind IFNET_WRITER_FOREACH(ifp) {
175 1.2 rmind npf_ifaddr_sync(npf, ifp);
176 1.2 rmind }
177 1.3 ozaki IFNET_GLOBAL_UNLOCK();
178 1.2 rmind KERNEL_UNLOCK_ONE(NULL);
179 1.2 rmind }
180 1.2 rmind
181 1.2 rmind
182