if_vlan.c revision 1.3 1 /* $NetBSD: if_vlan.c,v 1.3 2000/09/28 07:00:53 enami Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 1998 Massachusetts Institute of Technology
41 *
42 * Permission to use, copy, modify, and distribute this software and
43 * its documentation for any purpose and without fee is hereby
44 * granted, provided that both the above copyright notice and this
45 * permission notice appear in all copies, that both the above
46 * copyright notice and this permission notice appear in all
47 * supporting documentation, and that the name of M.I.T. not be used
48 * in advertising or publicity pertaining to distribution of the
49 * software without specific, written prior permission. M.I.T. makes
50 * no representations about the suitability of this software for any
51 * purpose. It is provided "as is" without express or implied
52 * warranty.
53 *
54 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
55 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
56 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
58 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
61 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
68 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
69 */
70
71 /*
72 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
73 * extended some day to also handle IEEE 802.1P priority tagging. This is
74 * sort of sneaky in the implementation, since we need to pretend to be
75 * enough of an Ethernet implementation to make ARP work. The way we do
76 * this is by telling everyone that we are an Ethernet interface, and then
77 * catch the packets that ether_output() left on our output queue when it
78 * calls if_start(), rewrite them for use by the real outgoing interface,
79 * and ask it to send them.
80 *
81 * TODO:
82 *
83 * - Need some way to notify vlan interfaces when the parent
84 * interface changes MTU.
85 *
86 * - Need to make promiscuous mode work.
87 */
88
89 #include "opt_inet.h"
90 #include "bpfilter.h"
91
92 #include <sys/param.h>
93 #include <sys/kernel.h>
94 #include <sys/mbuf.h>
95 #include <sys/queue.h>
96 #include <sys/socket.h>
97 #include <sys/sockio.h>
98 #include <sys/systm.h>
99 #include <sys/proc.h>
100
101 #if NBPFILTER > 0
102 #include <net/bpf.h>
103 #endif
104 #include <net/if.h>
105 #include <net/if_dl.h>
106 #include <net/if_types.h>
107 #include <net/if_ether.h>
108 #include <net/if_vlanvar.h>
109
110 #ifdef INET
111 #include <netinet/in.h>
112 #include <netinet/if_inarp.h>
113 #endif
114
115 extern struct ifaddr **ifnet_addrs; /* XXX if.c */
116
117 static int vlan_clone_create(struct if_clone *, int);
118 static void vlan_clone_destroy(struct ifnet *);
119 static int vlan_config(struct ifvlan *, struct ifnet *);
120 static int vlan_ioctl(struct ifnet *, u_long, caddr_t);
121 static int vlan_setmulti(struct ifnet *);
122 static void vlan_start(struct ifnet *);
123 static int vlan_unconfig(struct ifnet *);
124 void vlanattach(int);
125
126 /* XXX This should be a hash table with the tag as the basis of the key. */
127 static LIST_HEAD(, ifvlan) ifv_list;
128
129 struct if_clone vlan_cloner =
130 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
131
132 void
133 vlanattach(int n)
134 {
135
136 LIST_INIT(&ifv_list);
137 if_clone_attach(&vlan_cloner);
138 }
139
140 static int
141 vlan_clone_create(struct if_clone *ifc, int unit)
142 {
143 struct ifvlan *ifv;
144 struct ifnet *ifp;
145 u_int8_t eaddr[6];
146
147 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAIT);
148 memset(ifv, 0, sizeof(struct ifvlan));
149 ifp = &ifv->ifv_ec.ec_if;
150 SLIST_INIT(&ifv->ifv_mc_listhead);
151 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
152
153 sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
154 ifp->if_softc = ifv;
155 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
156 ifp->if_start = vlan_start;
157 ifp->if_ioctl = vlan_ioctl;
158
159 if_attach(ifp);
160 memset(eaddr, 0, sizeof(eaddr));
161 ether_ifattach(ifp, eaddr);
162
163 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
164 ifp->if_mtu = ETHERMTU - EVL_ENCAPLEN;
165
166 #if NBPFILTER > 0
167 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
168 sizeof(struct ether_header));
169 #endif
170
171 return (0);
172 }
173
174 static void
175 vlan_clone_destroy(struct ifnet *ifp)
176 {
177 struct ifvlan *ifv;
178 int s;
179
180 ifv = (struct ifvlan *)ifp->if_softc;
181 s = splsoftnet();
182
183 LIST_REMOVE(ifv, ifv_list);
184 vlan_unconfig(ifp);
185
186 #if NBPFILTER > 0
187 bpfdetach(ifp);
188 #endif
189 ether_ifdetach(ifp);
190 if_detach(ifp);
191 free(ifv, M_DEVBUF);
192
193 splx(s);
194 }
195
196 static int
197 vlan_config(struct ifvlan *ifv, struct ifnet *p)
198 {
199 struct ifaddr *ifa1, *ifa2;
200 struct sockaddr_dl *sdl1, *sdl2;
201
202 if (p->if_data.ifi_type != IFT_ETHER)
203 return (EPROTONOSUPPORT);
204 if (ifv->ifv_p != NULL)
205 return (EBUSY);
206 ifv->ifv_p = p;
207 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
208 ifv->ifv_if.if_flags = p->if_flags;
209
210 /*
211 * Set up our ``Ethernet address'' to match the underlying
212 * physical interface's.
213 */
214 ifa1 = ifnet_addrs[ifv->ifv_if.if_index];
215 ifa2 = ifnet_addrs[p->if_index];
216 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
217 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
218 sdl1->sdl_type = IFT_ETHER;
219 sdl1->sdl_alen = ETHER_ADDR_LEN;
220 memcpy(LLADDR(sdl1), LLADDR(sdl2), ETHER_ADDR_LEN);
221 memcpy(LLADDR(ifv->ifv_ec.ec_if.if_sadl), LLADDR(sdl2), ETHER_ADDR_LEN);
222 return (0);
223 }
224
225 static int
226 vlan_unconfig(struct ifnet *ifp)
227 {
228 struct ifaddr *ifa;
229 struct sockaddr_dl *sdl;
230 struct ifvlan *ifv;
231 struct ifnet *p;
232 struct ifreq *ifr, *ifr_p;
233 struct vlan_mc_entry *mc;
234 int error, s;
235
236 s = splsoftnet();
237
238 ifv = ifp->if_softc;
239 p = ifv->ifv_p;
240 ifr = (struct ifreq *)&ifp->if_data;
241 ifr_p = (struct ifreq *)&ifv->ifv_p->if_data;
242
243 /*
244 * Since the interface is being unconfigured, we need to empty the
245 * list of multicast groups that we may have joined while we were
246 * alive and remove them from the parent's list also.
247 */
248 while ((mc = SLIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
249 if ((error = ether_delmulti(ifr_p, &ifv->ifv_ec)) != 0) {
250 splx(s);
251 return (error);
252 }
253 if ((ether_delmulti(ifr, &ifv->ifv_ec)) != 0) {
254 splx(s);
255 return(error);
256 }
257 SLIST_REMOVE_HEAD(&ifv->ifv_mc_listhead, mc_entries);
258 free(mc, M_DEVBUF);
259 }
260
261 /* Disconnect from parent. */
262 ifv->ifv_p = NULL;
263 ifv->ifv_if.if_mtu = ETHERMTU - EVL_ENCAPLEN;
264
265 /* Clear our MAC address. */
266 ifa = ifnet_addrs[ifv->ifv_if.if_index];
267 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
268 sdl->sdl_type = IFT_ETHER;
269 sdl->sdl_alen = ETHER_ADDR_LEN;
270 memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
271 memset(LLADDR(ifv->ifv_ec.ec_if.if_sadl), 0, ETHER_ADDR_LEN);
272
273 splx(s);
274 return (0);
275 }
276
277 static int
278 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
279 {
280 struct proc *p = curproc; /* XXX */
281 struct ifaddr *ifa;
282 struct ifnet *pr;
283 struct ifreq *ifr;
284 struct ifvlan *ifv;
285 struct vlanreq vlr;
286 struct sockaddr *sa;
287 int error;
288
289 error = 0;
290 ifr = (struct ifreq *)data;
291 ifa = (struct ifaddr *)data;
292 ifv = ifp->if_softc;
293
294 switch (cmd) {
295 case SIOCSIFADDR:
296 ifp->if_flags |= IFF_UP;
297
298 switch (ifa->ifa_addr->sa_family) {
299 #ifdef INET
300 case AF_INET:
301 arp_ifinit(ifp, ifa);
302 break;
303 #endif
304 default:
305 break;
306 }
307 break;
308
309 case SIOCGIFADDR:
310 sa = (struct sockaddr *)&ifr->ifr_data;
311 memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
312 break;
313
314 case SIOCSIFMTU:
315 if (ifv->ifv_p != NULL) {
316 if (ifr->ifr_mtu > ifv->ifv_p->if_mtu - EVL_ENCAPLEN ||
317 ifr->ifr_mtu < ETHERMIN + EVL_ENCAPLEN)
318 error = EINVAL;
319 else
320 ifp->if_mtu = ifr->ifr_mtu;
321 } else
322 error = EINVAL;
323 break;
324
325 case SIOCSETVLAN:
326 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
327 break;
328 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
329 break;
330 if (vlr.vlr_parent[0] == '\0') {
331 vlan_unconfig(ifp);
332 if_down(ifp);
333 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
334 break;
335 }
336 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
337 error = EINVAL; /* check for valid tag */
338 break;
339 }
340 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
341 error = ENOENT;
342 break;
343 }
344 if ((error = vlan_config(ifv, pr)) != 0)
345 break;
346 ifv->ifv_tag = vlr.vlr_tag;
347 ifp->if_flags |= IFF_RUNNING;
348 break;
349
350 case SIOCGETVLAN:
351 memset(&vlr, 0, sizeof(vlr));
352 if (ifv->ifv_p != NULL) {
353 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
354 ifv->ifv_p->if_xname);
355 vlr.vlr_tag = ifv->ifv_tag;
356 }
357 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
358 break;
359
360 case SIOCSIFFLAGS:
361 /*
362 * XXX We don't support promiscuous mode right now because
363 * it would require help from the underlying drivers, which
364 * hasn't been implemented.
365 */
366 if ((ifr->ifr_flags & IFF_PROMISC) != 0) {
367 ifp->if_flags &= ~(IFF_PROMISC);
368 error = EINVAL;
369 }
370 break;
371
372 case SIOCADDMULTI:
373 case SIOCDELMULTI:
374 error = vlan_setmulti(ifp);
375 break;
376
377 default:
378 error = EINVAL;
379 }
380
381 return (error);
382 }
383
384 /*
385 * Program our multicast filter. What we're actually doing is programming
386 * the multicast filter of the parent. This has the side effect of causing
387 * the parent interface to receive multicast traffic that it doesn't really
388 * want, which ends up being discarded later by the upper protocol layers.
389 * Unfortunately, there's no way to avoid this: there really is only one
390 * physical interface.
391 */
392 static int
393 vlan_setmulti(struct ifnet *ifp)
394 {
395 struct ifreq *ifr_p;
396 struct ether_multi *enm;
397 struct ether_multistep step;
398 struct ifvlan *ifv;
399 struct vlan_mc_entry *mc;
400 int error;
401
402 /* Find the parent. */
403 mc = NULL;
404 ifv = ifp->if_softc;
405 ifr_p = (struct ifreq *)&ifv->ifv_p->if_data;
406
407 /* First, remove any existing filter entries. */
408 while ((mc = SLIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
409 if ((error = ether_delmulti(ifr_p, &ifv->ifv_ec)) != 0)
410 return(error);
411 SLIST_REMOVE_HEAD(&ifv->ifv_mc_listhead, mc_entries);
412 free(mc, M_DEVBUF);
413 }
414
415 /* Now program new ones. */
416 ETHER_FIRST_MULTI(step, &ifv->ifv_ec, enm);
417 while (enm != NULL) {
418 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
419 memcpy(&mc->mc_addr, enm->enm_addrlo, ETHER_ADDR_LEN);
420 SLIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
421 if ((error = ether_addmulti(ifr_p, &ifv->ifv_ec)) != 0)
422 return(error);
423 ETHER_NEXT_MULTI(step, enm);
424 }
425
426 return(0);
427 }
428
429 static void
430 vlan_start(struct ifnet *ifp)
431 {
432 struct ifvlan *ifv;
433 struct ifnet *p;
434 struct ether_vlan_header *evl;
435 struct mbuf *m;
436
437 ifv = ifp->if_softc;
438 p = ifv->ifv_p;
439 ifp->if_flags |= IFF_OACTIVE;
440
441 for (;;) {
442 IF_DEQUEUE(&ifp->if_snd, m);
443 if (m == NULL)
444 break;
445
446 #if NBPFILTER > 0
447 if (ifp->if_bpf)
448 bpf_mtap(ifp->if_bpf, m);
449 #endif
450
451 /*
452 * XXX Should handle the case where the underlying hardware
453 * interface can do VLAN tag insertion itself.
454 */
455 M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
456 if (m == NULL) {
457 printf("%s: M_PREPEND failed", ifv->ifv_p->if_xname);
458 ifp->if_ierrors++;
459 continue;
460 }
461
462 if (m->m_len < sizeof(struct ether_vlan_header) &&
463 (m = m_pullup(m,
464 sizeof(struct ether_vlan_header))) == NULL) {
465 printf("%s: m_pullup failed", ifv->ifv_p->if_xname);
466 ifp->if_ierrors++;
467 continue;
468 }
469
470 /*
471 * Transform the Ethernet header into an Ethernet header
472 * with 802.1Q encapsulation.
473 */
474 memmove(mtod(m, caddr_t), mtod(m, caddr_t) + EVL_ENCAPLEN,
475 sizeof(struct ether_header));
476 evl = mtod(m, struct ether_vlan_header *);
477 evl->evl_proto = evl->evl_encap_proto;
478 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
479 evl->evl_tag = htons(ifv->ifv_tag);
480
481 /*
482 * Send it, precisely as ether_output() would have. We are
483 * already running at splimp.
484 */
485 if (IF_QFULL(&p->if_snd)) {
486 IF_DROP(&p->if_snd);
487 /* XXX stats */
488 ifp->if_oerrors++;
489 m_freem(m);
490 continue;
491 }
492
493 IF_ENQUEUE(&p->if_snd, m);
494 if ((p->if_flags & IFF_OACTIVE) == 0) {
495 p->if_start(p);
496 ifp->if_opackets++;
497 }
498 }
499
500 ifp->if_flags &= ~IFF_OACTIVE;
501 }
502
503 /*
504 * Given an Ethernet frame, find a valid vlan interface corresponding to the
505 * given source interface and tag, then run the the real packet through
506 * the parent's input routine.
507 */
508 void
509 vlan_input(struct ifnet *ifp, struct mbuf *m)
510 {
511 struct ether_vlan_header *evl;
512 struct ifvlan *ifv;
513 u_int tag;
514
515 if (m->m_len < sizeof(struct ether_vlan_header) &&
516 (m = m_pullup(m, sizeof(struct ether_vlan_header))) == NULL) {
517 printf("%s: no memory for VLAN header, dropping packet.\n",
518 ifp->if_xname);
519 return;
520 }
521 evl = mtod(m, struct ether_vlan_header *);
522 KASSERT(htons(evl->evl_encap_proto) == ETHERTYPE_VLAN);
523
524 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
525
526 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
527 ifv = LIST_NEXT(ifv, ifv_list))
528 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
529 break;
530
531 if (ifv == NULL || (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
532 (IFF_UP|IFF_RUNNING)) {
533 m_free(m);
534 ifp->if_data.ifi_noproto++;
535 return;
536 }
537
538 /*
539 * Having found a valid vlan interface corresponding to the given
540 * source interface and vlan tag, remove the encapsulation.
541 */
542 evl->evl_encap_proto = evl->evl_proto;
543 memmove(mtod(m, caddr_t) + EVL_ENCAPLEN, mtod(m, caddr_t),
544 EVL_ENCAPLEN);
545 m_adj(m, EVL_ENCAPLEN);
546
547 m->m_pkthdr.rcvif = &ifv->ifv_if;
548 ifv->ifv_if.if_ipackets++;
549
550 #if NBPFILTER > 0
551 if (ifv->ifv_if.if_bpf)
552 bpf_mtap(ifv->ifv_if.if_bpf, m);
553 #endif
554
555 /* Pass it back through the parent's input routine. */
556 (*ifp->if_input)(&ifv->ifv_if, m);
557 }
558