if_vlan.c revision 1.2 1 /* $NetBSD: if_vlan.c,v 1.2 2000/09/28 06:13:35 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 if (ifv == NULL)
149 return (ENOMEM);
150 memset(ifv, 0, sizeof(struct ifvlan));
151 ifp = &ifv->ifv_ec.ec_if;
152 SLIST_INIT(&ifv->ifv_mc_listhead);
153 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
154
155 sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
156 ifp->if_softc = ifv;
157 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
158 ifp->if_start = vlan_start;
159 ifp->if_ioctl = vlan_ioctl;
160
161 if_attach(ifp);
162 memset(eaddr, 0, sizeof(eaddr));
163 ether_ifattach(ifp, eaddr);
164
165 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
166 ifp->if_mtu = ETHERMTU - EVL_ENCAPLEN;
167
168 #if NBPFILTER > 0
169 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
170 sizeof(struct ether_header));
171 #endif
172
173 return (0);
174 }
175
176 static void
177 vlan_clone_destroy(struct ifnet *ifp)
178 {
179 struct ifvlan *ifv;
180 int s;
181
182 ifv = (struct ifvlan *)ifp->if_softc;
183 s = splsoftnet();
184
185 LIST_REMOVE(ifv, ifv_list);
186 vlan_unconfig(ifp);
187
188 #if NBPFILTER > 0
189 bpfdetach(ifp);
190 #endif
191 ether_ifdetach(ifp);
192 if_detach(ifp);
193 free(ifv, M_DEVBUF);
194
195 splx(s);
196 }
197
198 static int
199 vlan_config(struct ifvlan *ifv, struct ifnet *p)
200 {
201 struct ifaddr *ifa1, *ifa2;
202 struct sockaddr_dl *sdl1, *sdl2;
203
204 if (p->if_data.ifi_type != IFT_ETHER)
205 return (EPROTONOSUPPORT);
206 if (ifv->ifv_p != NULL)
207 return (EBUSY);
208 ifv->ifv_p = p;
209 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
210 ifv->ifv_if.if_flags = p->if_flags;
211
212 /*
213 * Set up our ``Ethernet address'' to match the underlying
214 * physical interface's.
215 */
216 ifa1 = ifnet_addrs[ifv->ifv_if.if_index];
217 ifa2 = ifnet_addrs[p->if_index];
218 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
219 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
220 sdl1->sdl_type = IFT_ETHER;
221 sdl1->sdl_alen = ETHER_ADDR_LEN;
222 memcpy(LLADDR(sdl1), LLADDR(sdl2), ETHER_ADDR_LEN);
223 memcpy(LLADDR(ifv->ifv_ec.ec_if.if_sadl), LLADDR(sdl2), ETHER_ADDR_LEN);
224 return (0);
225 }
226
227 static int
228 vlan_unconfig(struct ifnet *ifp)
229 {
230 struct ifaddr *ifa;
231 struct sockaddr_dl *sdl;
232 struct ifvlan *ifv;
233 struct ifnet *p;
234 struct ifreq *ifr, *ifr_p;
235 struct vlan_mc_entry *mc;
236 int error, s;
237
238 s = splsoftnet();
239
240 ifv = ifp->if_softc;
241 p = ifv->ifv_p;
242 ifr = (struct ifreq *)&ifp->if_data;
243 ifr_p = (struct ifreq *)&ifv->ifv_p->if_data;
244
245 /*
246 * Since the interface is being unconfigured, we need to empty the
247 * list of multicast groups that we may have joined while we were
248 * alive and remove them from the parent's list also.
249 */
250 while ((mc = SLIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
251 if ((error = ether_delmulti(ifr_p, &ifv->ifv_ec)) != 0) {
252 splx(s);
253 return (error);
254 }
255 if ((ether_delmulti(ifr, &ifv->ifv_ec)) != 0) {
256 splx(s);
257 return(error);
258 }
259 SLIST_REMOVE_HEAD(&ifv->ifv_mc_listhead, mc_entries);
260 free(mc, M_DEVBUF);
261 }
262
263 /* Disconnect from parent. */
264 ifv->ifv_p = NULL;
265 ifv->ifv_if.if_mtu = ETHERMTU - EVL_ENCAPLEN;
266
267 /* Clear our MAC address. */
268 ifa = ifnet_addrs[ifv->ifv_if.if_index];
269 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
270 sdl->sdl_type = IFT_ETHER;
271 sdl->sdl_alen = ETHER_ADDR_LEN;
272 memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
273 memset(LLADDR(ifv->ifv_ec.ec_if.if_sadl), 0, ETHER_ADDR_LEN);
274
275 splx(s);
276 return (0);
277 }
278
279 static int
280 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
281 {
282 struct proc *p = curproc; /* XXX */
283 struct ifaddr *ifa;
284 struct ifnet *pr;
285 struct ifreq *ifr;
286 struct ifvlan *ifv;
287 struct vlanreq vlr;
288 struct sockaddr *sa;
289 int error;
290
291 error = 0;
292 ifr = (struct ifreq *)data;
293 ifa = (struct ifaddr *)data;
294 ifv = ifp->if_softc;
295
296 switch (cmd) {
297 case SIOCSIFADDR:
298 ifp->if_flags |= IFF_UP;
299
300 switch (ifa->ifa_addr->sa_family) {
301 #ifdef INET
302 case AF_INET:
303 arp_ifinit(ifp, ifa);
304 break;
305 #endif
306 default:
307 break;
308 }
309 break;
310
311 case SIOCGIFADDR:
312 sa = (struct sockaddr *)&ifr->ifr_data;
313 memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
314 break;
315
316 case SIOCSIFMTU:
317 if (ifv->ifv_p != NULL) {
318 if (ifr->ifr_mtu > ifv->ifv_p->if_mtu - EVL_ENCAPLEN ||
319 ifr->ifr_mtu < ETHERMIN + EVL_ENCAPLEN)
320 error = EINVAL;
321 else
322 ifp->if_mtu = ifr->ifr_mtu;
323 } else
324 error = EINVAL;
325 break;
326
327 case SIOCSETVLAN:
328 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
329 break;
330 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
331 break;
332 if (vlr.vlr_parent[0] == '\0') {
333 vlan_unconfig(ifp);
334 if_down(ifp);
335 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
336 break;
337 }
338 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
339 error = EINVAL; /* check for valid tag */
340 break;
341 }
342 if ((pr = ifunit(vlr.vlr_parent)) == 0) {
343 error = ENOENT;
344 break;
345 }
346 if ((error = vlan_config(ifv, pr)) != 0)
347 break;
348 ifv->ifv_tag = vlr.vlr_tag;
349 ifp->if_flags |= IFF_RUNNING;
350 break;
351
352 case SIOCGETVLAN:
353 memset(&vlr, 0, sizeof(vlr));
354 if (ifv->ifv_p != NULL) {
355 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
356 ifv->ifv_p->if_xname);
357 vlr.vlr_tag = ifv->ifv_tag;
358 }
359 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
360 break;
361
362 case SIOCSIFFLAGS:
363 /*
364 * XXX We don't support promiscuous mode right now because
365 * it would require help from the underlying drivers, which
366 * hasn't been implemented.
367 */
368 if ((ifr->ifr_flags & IFF_PROMISC) != 0) {
369 ifp->if_flags &= ~(IFF_PROMISC);
370 error = EINVAL;
371 }
372 break;
373
374 case SIOCADDMULTI:
375 case SIOCDELMULTI:
376 error = vlan_setmulti(ifp);
377 break;
378
379 default:
380 error = EINVAL;
381 }
382
383 return (error);
384 }
385
386 /*
387 * Program our multicast filter. What we're actually doing is programming
388 * the multicast filter of the parent. This has the side effect of causing
389 * the parent interface to receive multicast traffic that it doesn't really
390 * want, which ends up being discarded later by the upper protocol layers.
391 * Unfortunately, there's no way to avoid this: there really is only one
392 * physical interface.
393 */
394 static int
395 vlan_setmulti(struct ifnet *ifp)
396 {
397 struct ifreq *ifr_p;
398 struct ether_multi *enm;
399 struct ether_multistep step;
400 struct ifvlan *ifv;
401 struct vlan_mc_entry *mc;
402 int error;
403
404 /* Find the parent. */
405 mc = NULL;
406 ifv = ifp->if_softc;
407 ifr_p = (struct ifreq *)&ifv->ifv_p->if_data;
408
409 /* First, remove any existing filter entries. */
410 while ((mc = SLIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
411 if ((error = ether_delmulti(ifr_p, &ifv->ifv_ec)) != 0)
412 return(error);
413 SLIST_REMOVE_HEAD(&ifv->ifv_mc_listhead, mc_entries);
414 free(mc, M_DEVBUF);
415 }
416
417 /* Now program new ones. */
418 ETHER_FIRST_MULTI(step, &ifv->ifv_ec, enm);
419 while (enm != NULL) {
420 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
421 memcpy(&mc->mc_addr, enm->enm_addrlo, ETHER_ADDR_LEN);
422 SLIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
423 if ((error = ether_addmulti(ifr_p, &ifv->ifv_ec)) != 0)
424 return(error);
425 ETHER_NEXT_MULTI(step, enm);
426 }
427
428 return(0);
429 }
430
431 static void
432 vlan_start(struct ifnet *ifp)
433 {
434 struct ifvlan *ifv;
435 struct ifnet *p;
436 struct ether_vlan_header *evl;
437 struct mbuf *m;
438
439 ifv = ifp->if_softc;
440 p = ifv->ifv_p;
441 ifp->if_flags |= IFF_OACTIVE;
442
443 for (;;) {
444 IF_DEQUEUE(&ifp->if_snd, m);
445 if (m == NULL)
446 break;
447
448 #if NBPFILTER > 0
449 if (ifp->if_bpf)
450 bpf_mtap(ifp->if_bpf, m);
451 #endif
452
453 /*
454 * XXX Should handle the case where the underlying hardware
455 * interface can do VLAN tag insertion itself.
456 */
457 M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
458 if (m == NULL) {
459 printf("%s: M_PREPEND failed", ifv->ifv_p->if_xname);
460 ifp->if_ierrors++;
461 continue;
462 }
463
464 if (m->m_len < sizeof(struct ether_vlan_header) &&
465 (m = m_pullup(m,
466 sizeof(struct ether_vlan_header))) == NULL) {
467 printf("%s: m_pullup failed", ifv->ifv_p->if_xname);
468 ifp->if_ierrors++;
469 continue;
470 }
471
472 /*
473 * Transform the Ethernet header into an Ethernet header
474 * with 802.1Q encapsulation.
475 */
476 memmove(mtod(m, caddr_t), mtod(m, caddr_t) + EVL_ENCAPLEN,
477 sizeof(struct ether_header));
478 evl = mtod(m, struct ether_vlan_header *);
479 evl->evl_proto = evl->evl_encap_proto;
480 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
481 evl->evl_tag = htons(ifv->ifv_tag);
482
483 /*
484 * Send it, precisely as ether_output() would have. We are
485 * already running at splimp.
486 */
487 if (IF_QFULL(&p->if_snd)) {
488 IF_DROP(&p->if_snd);
489 /* XXX stats */
490 ifp->if_oerrors++;
491 m_freem(m);
492 continue;
493 }
494
495 IF_ENQUEUE(&p->if_snd, m);
496 if ((p->if_flags & IFF_OACTIVE) == 0) {
497 p->if_start(p);
498 ifp->if_opackets++;
499 }
500 }
501
502 ifp->if_flags &= ~IFF_OACTIVE;
503 }
504
505 /*
506 * Given an Ethernet frame, find a valid vlan interface corresponding to the
507 * given source interface and tag, then run the the real packet through
508 * the parent's input routine.
509 */
510 void
511 vlan_input(struct ifnet *ifp, struct mbuf *m)
512 {
513 struct ether_vlan_header *evl;
514 struct ifvlan *ifv;
515 u_int tag;
516
517 if (m->m_len < sizeof(struct ether_vlan_header) &&
518 (m = m_pullup(m, sizeof(struct ether_vlan_header))) == NULL) {
519 printf("%s: no memory for VLAN header, dropping packet.\n",
520 ifp->if_xname);
521 return;
522 }
523 evl = mtod(m, struct ether_vlan_header *);
524 KASSERT(htons(evl->evl_encap_proto) == ETHERTYPE_VLAN);
525
526 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
527
528 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
529 ifv = LIST_NEXT(ifv, ifv_list))
530 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
531 break;
532
533 if (ifv == NULL || (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
534 (IFF_UP|IFF_RUNNING)) {
535 m_free(m);
536 ifp->if_data.ifi_noproto++;
537 return;
538 }
539
540 /*
541 * Having found a valid vlan interface corresponding to the given
542 * source interface and vlan tag, remove the encapsulation.
543 */
544 evl->evl_encap_proto = evl->evl_proto;
545 memmove(mtod(m, caddr_t) + EVL_ENCAPLEN, mtod(m, caddr_t),
546 EVL_ENCAPLEN);
547 m_adj(m, EVL_ENCAPLEN);
548
549 m->m_pkthdr.rcvif = &ifv->ifv_if;
550 ifv->ifv_if.if_ipackets++;
551
552 #if NBPFILTER > 0
553 if (ifv->ifv_if.if_bpf)
554 bpf_mtap(ifv->ifv_if.if_bpf, m);
555 #endif
556
557 /* Pass it back through the parent's input routine. */
558 (*ifp->if_input)(&ifv->ifv_if, m);
559 }
560