1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/* $OpenBSD: if_enc.c,v 1.49 2010/06/29 21:28:37 reyk Exp $ */
/*
* Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "enc.h"
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <net/if.h>
#include <net/if_enc.h>
#include <net/if_types.h>
#include <net/route.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
TAILQ_HEAD(__enchead, enc_softc) enc_list; /* all enc interfaces */
struct ifnet **enc_ifps; /* rdomain-mapped enc ifs */
u_int enc_max_id;
void encattach(int);
int enc_clone_create(struct if_clone *, int);
int enc_clone_destroy(struct ifnet *);
void enc_start(struct ifnet *);
int enc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
struct rtentry *);
int enc_ioctl(struct ifnet *, u_long, caddr_t);
int enc_setif(struct ifnet *, u_int);
void enc_unsetif(struct ifnet *);
struct if_clone enc_cloner =
IF_CLONE_INITIALIZER("enc", enc_clone_create, enc_clone_destroy);
void
encattach(int count)
{
TAILQ_INIT(&enc_list);
/* Create enc0 by default */
(void)enc_clone_create(&enc_cloner, 0);
if_clone_attach(&enc_cloner);
}
int
enc_clone_create(struct if_clone *ifc, int unit)
{
struct enc_softc *sc;
struct ifnet *ifp;
if ((sc = malloc(sizeof(struct enc_softc),
M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
return (ENOMEM);
sc->sc_unit = unit;
ifp = &sc->sc_if;
ifp->if_softc = sc;
ifp->if_type = IFT_ENC;
ifp->if_start = enc_start;
ifp->if_output = enc_output;
ifp->if_ioctl = enc_ioctl;
ifp->if_hdrlen = ENC_HDRLEN;
snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
ifc->ifc_name, unit);
if_attach(ifp);
if_alloc_sadl(ifp);
#if NBPFILTER > 0
bpfattach(&ifp->if_bpf, ifp, DLT_ENC, ENC_HDRLEN);
#endif
if (enc_setif(ifp, 0) != 0) {
if_detach(ifp);
free(sc, M_DEVBUF);
return (-1);
}
TAILQ_INSERT_TAIL(&enc_list, sc, sc_entry);
return (0);
}
int
enc_clone_destroy(struct ifnet *ifp)
{
struct enc_softc *sc = ifp->if_softc;
int s;
/* Protect users from removing enc0 */
if (sc->sc_unit == 0)
return (EPERM);
s = splnet();
TAILQ_REMOVE(&enc_list, sc, sc_entry);
enc_unsetif(ifp);
if_detach(ifp);
free(sc, M_DEVBUF);
splx(s);
return (0);
}
void
enc_start(struct ifnet *ifp)
{
struct mbuf *m;
for (;;) {
IF_DROP(&ifp->if_snd);
IF_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
m_freem(m);
}
}
int
enc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
struct rtentry *rt)
{
m_freem(m); /* drop packet */
return (0);
}
int
enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct ifreq *ifr = (struct ifreq *)data;
int error = 0;
switch (cmd) {
case SIOCAIFADDR:
case SIOCSIFADDR:
case SIOCSIFDSTADDR:
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP)
ifp->if_flags |= IFF_RUNNING;
else
ifp->if_flags &= ~IFF_RUNNING;
break;
case SIOCSIFRTABLEID:
if ((error = enc_setif(ifp, ifr->ifr_rdomainid)) != 0)
return (error);
/* FALLTHROUGH */
default:
return (ENOTTY);
}
return (0);
}
struct ifnet *
enc_getif(u_int id)
{
if (enc_ifps == NULL)
return (NULL);
else if (id > RT_TABLEID_MAX)
return (NULL);
else if (id > enc_max_id)
return (NULL);
return (enc_ifps[id]);
}
int
enc_setif(struct ifnet *ifp, u_int id)
{
struct ifnet **new;
size_t newlen;
enc_unsetif(ifp);
/*
* There can only be one default encif per rdomain -
* Don't overwrite the existing enc iface that is stored
* for this rdomain, so only the first enc interface that
* was added for this rdomain becomes the default.
*/
if (enc_getif(id) != NULL)
return (0);
if (id > RT_TABLEID_MAX)
return (-1);
if (id == 0 || id > enc_max_id) {
newlen = sizeof(struct ifnet *) * (id + 1);
if ((new = malloc(newlen, M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
return (-1);
if (enc_ifps != NULL) {
memcpy(new, enc_ifps,
sizeof(struct ifnet *) * (enc_max_id + 1));
free(enc_ifps, M_DEVBUF);
}
enc_ifps = new;
enc_max_id = id;
}
enc_ifps[id] = ifp;
/* Indicate that this interface is the rdomain default */
ifp->if_link_state = LINK_STATE_UP;
return (0);
}
void
enc_unsetif(struct ifnet *ifp)
{
u_int id = ifp->if_rdomain;
struct ifnet *oifp;
struct enc_softc *sc;
if ((oifp = enc_getif(id)) == NULL || oifp != ifp)
return;
/* Clear slot for this rdomain */
enc_ifps[id] = NULL;
ifp->if_link_state = LINK_STATE_UNKNOWN;
/*
* Now find the next available encif to be the default interface
* for this rdomain.
*/
TAILQ_FOREACH(sc, &enc_list, sc_entry) {
if (&sc->sc_if == ifp || sc->sc_if.if_rdomain != id)
continue;
enc_ifps[id] = &sc->sc_if;
sc->sc_if.if_link_state = LINK_STATE_UP;
break;
}
}
|