if_tap.c revision 1.1 1 /* $NetBSD: if_tap.c,v 1.1 2005/01/08 22:27:54 cube Exp $ */
2
3 /*
4 * Copyright (c) 2003, 2004 The NetBSD Foundation.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to the NetBSD Foundation
8 * by Quentin Garnier.
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 * tap(4) is a virtual Ethernet interface. It appears as a real Ethernet
41 * device to the system, but can also be accessed by userland through a
42 * character device interface, which allows reading and injecting frames.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.1 2005/01/08 22:27:54 cube Exp $");
47
48 #include "bpfilter.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/file.h>
57 #include <sys/filedesc.h>
58 #include <sys/ksyms.h>
59 #include <sys/poll.h>
60 #include <sys/select.h>
61 #include <sys/sockio.h>
62 #include <sys/sysctl.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_ether.h>
67 #include <net/if_media.h>
68 #include <net/if_tap.h>
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72
73 /*
74 * sysctl node management
75 *
76 * It's not really possible to use a SYSCTL_SETUP block with
77 * current LKM implementation, so it is easier to just define
78 * our own function.
79 *
80 * The handler function is a "helper" in Andrew Brown's sysctl
81 * framework terminology. It is used as a gateway for sysctl
82 * requests over the nodes.
83 *
84 * tap_log allows the module to log creations of nodes and
85 * destroy them all at once using sysctl_teardown.
86 */
87 static int tap_node;
88 static int tap_sysctl_handler(SYSCTLFN_PROTO);
89
90 /*
91 * Since we're an Ethernet device, we need the 3 following
92 * components: a leading struct device, a struct ethercom,
93 * and also a struct ifmedia since we don't attach a PHY to
94 * ourselves. We could emulate one, but there's no real
95 * point.
96 */
97
98 struct tap_softc {
99 struct device sc_dev;
100 struct ifmedia sc_im;
101 struct ethercom sc_ec;
102 int sc_flags;
103 #define TAP_INUSE 0x00000001 /* tap device can only be opened once */
104 #define TAP_ASYNCIO 0x00000002 /* user is using async I/O (SIGIO) on the device */
105 #define TAP_NBIO 0x00000004 /* user wants calls to avoid blocking */
106 #define TAP_GOING 0x00000008 /* interface is being destroyed */
107 struct selinfo sc_rsel;
108 pid_t sc_pgid; /* For async. IO */
109 struct lock sc_rdlock;
110 struct simplelock sc_kqlock;
111 };
112
113 /* autoconf(9) glue */
114
115 void tapattach(int);
116
117 static int tap_match(struct device *, struct cfdata *, void *);
118 static void tap_attach(struct device *, struct device *, void *);
119 static int tap_detach(struct device*, int);
120
121 /* Ethernet address helper functions */
122
123 static char *tap_ether_sprintf(char *, const u_char *);
124 static int tap_ether_aton(u_char *, char *);
125
126 CFATTACH_DECL(tap, sizeof(struct tap_softc),
127 tap_match, tap_attach, tap_detach, NULL);
128 extern struct cfdriver tap_cd;
129
130 /* Real device access routines */
131 static int tap_dev_close(struct tap_softc *);
132 static int tap_dev_read(int, struct uio *, int);
133 static int tap_dev_write(int, struct uio *, int);
134 static int tap_dev_ioctl(int, u_long, caddr_t, struct proc *);
135 static int tap_dev_poll(int, int, struct proc *);
136 static int tap_dev_kqfilter(int, struct knote *);
137
138 /* Fileops access routines */
139 static int tap_fops_close(struct file *, struct proc *);
140 static int tap_fops_read(struct file *, off_t *, struct uio *,
141 struct ucred *, int);
142 static int tap_fops_write(struct file *, off_t *, struct uio *,
143 struct ucred *, int);
144 static int tap_fops_ioctl(struct file *, u_long, void *,
145 struct proc *);
146 static int tap_fops_poll(struct file *, int, struct proc *);
147 static int tap_fops_kqfilter(struct file *, struct knote *);
148
149 static const struct fileops tap_fileops = {
150 tap_fops_read,
151 tap_fops_write,
152 tap_fops_ioctl,
153 fnullop_fcntl,
154 tap_fops_poll,
155 fbadop_stat,
156 tap_fops_close,
157 tap_fops_kqfilter,
158 };
159
160 /* Helper for cloning open() */
161 static int tap_dev_cloner(struct proc *);
162
163 /* Character device routines */
164 static int tap_cdev_open(dev_t, int, int, struct proc *);
165 static int tap_cdev_close(dev_t, int, int, struct proc *);
166 static int tap_cdev_read(dev_t, struct uio *, int);
167 static int tap_cdev_write(dev_t, struct uio *, int);
168 static int tap_cdev_ioctl(dev_t, u_long, caddr_t, int, struct proc *);
169 static int tap_cdev_poll(dev_t, int, struct proc *);
170 static int tap_cdev_kqfilter(dev_t, struct knote *);
171
172 const struct cdevsw tap_cdevsw = {
173 tap_cdev_open, tap_cdev_close,
174 tap_cdev_read, tap_cdev_write,
175 tap_cdev_ioctl, nostop, notty,
176 tap_cdev_poll, nommap,
177 tap_cdev_kqfilter,
178 };
179
180 #define TAP_CLONER 0xfffff /* Maximal minor value */
181
182 /* kqueue-related routines */
183 static void tap_kqdetach(struct knote *);
184 static int tap_kqread(struct knote *, long);
185
186 /*
187 * Those are needed by the if_media interface.
188 */
189
190 static int tap_mediachange(struct ifnet *);
191 static void tap_mediastatus(struct ifnet *, struct ifmediareq *);
192
193 /*
194 * Those are needed by the ifnet interface, and would typically be
195 * there for any network interface driver.
196 * Some other routines are optional: watchdog and drain.
197 */
198
199 static void tap_start(struct ifnet *);
200 static void tap_stop(struct ifnet *, int);
201 static int tap_init(struct ifnet *);
202 static int tap_ioctl(struct ifnet *, u_long, caddr_t);
203
204 /* This is an internal function to keep tap_ioctl readable */
205 static int tap_lifaddr(struct ifnet *, u_long, struct ifaliasreq *);
206
207 /*
208 * tap is a clonable interface, although it is highly unrealistic for
209 * an Ethernet device.
210 *
211 * Here are the bits needed for a clonable interface.
212 */
213 static int tap_clone_create(struct if_clone *, int);
214 static int tap_clone_destroy(struct ifnet *);
215
216 struct if_clone tap_cloners = IF_CLONE_INITIALIZER("tap",
217 tap_clone_create,
218 tap_clone_destroy);
219
220 /* Helper functionis shared by the two cloning code paths */
221 static struct tap_softc * tap_clone_creator(int);
222 static int tap_clone_destroyer(struct device *);
223
224 void
225 tapattach(int n)
226 {
227 int error;
228
229 error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
230 if (error) {
231 aprint_error("%s: unable to register cfattach\n",
232 tap_cd.cd_name);
233 (void)config_cfdriver_detach(&tap_cd);
234 return;
235 }
236
237 if_clone_attach(&tap_cloners);
238 }
239
240 /* Pretty much useless for a pseudo-device */
241 static int
242 tap_match(struct device *self, struct cfdata *cfdata, void *arg)
243 {
244 return (1);
245 }
246
247 void
248 tap_attach(struct device *parent, struct device *self, void *aux)
249 {
250 struct tap_softc *sc = (struct tap_softc *)self;
251 struct ifnet *ifp;
252 u_int8_t enaddr[ETHER_ADDR_LEN] =
253 { 0xf0, 0x0b, 0xa4, 0xff, 0xff, 0xff };
254 char enaddrstr[18];
255 uint32_t ui;
256 int error;
257 struct sysctlnode *node;
258
259 aprint_normal("%s: faking Ethernet device\n",
260 self->dv_xname);
261
262 /*
263 * In order to obtain unique initial Ethernet address on a host,
264 * do some randomisation using mono_time. It's not meant for anything
265 * but avoiding hard-coding an address.
266 */
267 ui = (mono_time.tv_sec ^ mono_time.tv_usec) & 0xffffff;
268 memcpy(enaddr+3, (u_int8_t *)&ui, 3);
269
270 aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
271 tap_ether_sprintf(enaddrstr, enaddr));
272
273 /*
274 * Why 1000baseT? Why not? You can add more.
275 *
276 * Note that there are 3 steps: init, one or several additions to
277 * list of supported media, and in the end, the selection of one
278 * of them.
279 */
280 ifmedia_init(&sc->sc_im, 0, tap_mediachange, tap_mediastatus);
281 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T, 0, NULL);
282 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
283 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX, 0, NULL);
284 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
285 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T, 0, NULL);
286 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
287 ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
288 ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
289
290 /*
291 * One should note that an interface must do multicast in order
292 * to support IPv6.
293 */
294 ifp = &sc->sc_ec.ec_if;
295 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
296 ifp->if_softc = sc;
297 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
298 ifp->if_ioctl = tap_ioctl;
299 ifp->if_start = tap_start;
300 ifp->if_stop = tap_stop;
301 ifp->if_init = tap_init;
302 IFQ_SET_READY(&ifp->if_snd);
303
304 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
305
306 /* Those steps are mandatory for an Ethernet driver, the fisrt call
307 * being common to all network interface drivers. */
308 if_attach(ifp);
309 ether_ifattach(ifp, enaddr);
310
311 sc->sc_flags = 0;
312
313 /*
314 * Add a sysctl node for that interface.
315 *
316 * The pointer transmitted is not a string, but instead a pointer to
317 * the softc structure, which we can use to build the string value on
318 * the fly in the helper function of the node. See the comments for
319 * tap_sysctl_handler for details.
320 */
321 if ((error = sysctl_createv(NULL, 0, NULL,
322 &node, CTLFLAG_READWRITE,
323 CTLTYPE_STRING, sc->sc_dev.dv_xname, NULL,
324 tap_sysctl_handler, 0, sc, 18,
325 CTL_NET, PF_LINK, tap_node, sc->sc_dev.dv_unit, CTL_EOL)) != 0)
326 aprint_error("%s: sysctl_createv returned %d, ignoring\n",
327 sc->sc_dev.dv_xname, error);
328
329 /*
330 * Initialize the two locks for the device.
331 *
332 * We need a lock here because even though the tap device can be
333 * opened only once, the file descriptor might be passed to another
334 * process, say a fork(2)ed child.
335 *
336 * The Giant saves us from most of the hassle, but since the read
337 * operation can sleep, we don't want two processes to wake up at
338 * the same moment and both try and dequeue a single packet.
339 *
340 * The queue for event listeners (used by kqueue(9), see below) has
341 * to be protected, too, but we don't need the same level of
342 * complexity for that lock, so a simple spinning lock is fine.
343 */
344 lockinit(&sc->sc_rdlock, PSOCK|PCATCH, "tapl", 0, LK_SLEEPFAIL);
345 simple_lock_init(&sc->sc_kqlock);
346 }
347
348 /*
349 * When detaching, we do the inverse of what is done in the attach
350 * routine, in reversed order.
351 */
352 static int
353 tap_detach(struct device* self, int flags)
354 {
355 struct tap_softc *sc = (struct tap_softc *)self;
356 struct ifnet *ifp = &sc->sc_ec.ec_if;
357 int error, s;
358
359 /*
360 * Some processes might be sleeping on "tap", so we have to make
361 * them release their hold on the device.
362 *
363 * The LK_DRAIN operation will wait for every locked process to
364 * release their hold.
365 */
366 sc->sc_flags |= TAP_GOING;
367 s = splnet();
368 tap_stop(ifp, 1);
369 if_down(ifp);
370 splx(s);
371 lockmgr(&sc->sc_rdlock, LK_DRAIN, NULL);
372
373 /*
374 * Destroying a single leaf is a very straightforward operation using
375 * sysctl_destroyv. One should be sure to always end the path with
376 * CTL_EOL.
377 */
378 if ((error = sysctl_destroyv(NULL, CTL_NET, PF_LINK, tap_node,
379 sc->sc_dev.dv_unit, CTL_EOL)) != 0)
380 aprint_error("%s: sysctl_destroyv returned %d, ignoring\n",
381 sc->sc_dev.dv_xname, error);
382 ether_ifdetach(ifp);
383 if_detach(ifp);
384 ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
385
386 return (0);
387 }
388
389 /*
390 * This function is called by the ifmedia layer to notify the driver
391 * that the user requested a media change. A real driver would
392 * reconfigure the hardware.
393 */
394 static int
395 tap_mediachange(struct ifnet *ifp)
396 {
397 return (0);
398 }
399
400 /*
401 * Here the user asks for the currently used media.
402 */
403 static void
404 tap_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
405 {
406 struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
407 imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
408 }
409
410 /*
411 * This is the function where we SEND packets.
412 *
413 * There is no 'receive' equivalent. A typical driver will get
414 * interrupts from the hardware, and from there will inject new packets
415 * into the network stack.
416 *
417 * Once handled, a packet must be freed. A real driver might not be able
418 * to fit all the pending packets into the hardware, and is allowed to
419 * return before having sent all the packets. It should then use the
420 * if_flags flag IFF_OACTIVE to notify the upper layer.
421 *
422 * There are also other flags one should check, such as IFF_PAUSE.
423 *
424 * It is our duty to make packets available to BPF listeners.
425 *
426 * You should be aware that this function is called by the Ethernet layer
427 * at splnet().
428 *
429 * When the device is opened, we have to pass the packet(s) to the
430 * userland. For that we stay in OACTIVE mode while the userland gets
431 * the packets, and we send a signal to the processes waiting to read.
432 *
433 * wakeup(sc) is the counterpart to the tsleep call in
434 * tap_dev_read, while selnotify() is used for kevent(2) and
435 * poll(2) (which includes select(2)) listeners.
436 */
437 static void
438 tap_start(struct ifnet *ifp)
439 {
440 struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
441 struct mbuf *m0;
442
443 if ((sc->sc_flags & TAP_INUSE) == 0) {
444 /* Simply drop packets */
445 for(;;) {
446 IFQ_DEQUEUE(&ifp->if_snd, m0);
447 if (m0 == NULL)
448 return;
449
450 ifp->if_opackets++;
451 #if NBPFILTER > 0
452 if (ifp->if_bpf)
453 bpf_mtap(ifp->if_bpf, m0);
454 #endif
455
456 m_freem(m0);
457 }
458 } else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
459 ifp->if_flags |= IFF_OACTIVE;
460 wakeup(sc);
461 selnotify(&sc->sc_rsel, 1);
462 if (sc->sc_flags & TAP_ASYNCIO)
463 fownsignal(sc->sc_pgid, SIGIO, POLL_IN,
464 POLLIN|POLLRDNORM, NULL);
465 }
466 }
467
468 /*
469 * A typical driver will only contain the following handlers for
470 * ioctl calls, except SIOCSIFPHYADDR.
471 * The latter is a hack I used to set the Ethernet address of the
472 * faked device.
473 *
474 * Note that both ifmedia_ioctl() and ether_ioctl() have to be
475 * called under splnet().
476 */
477 static int
478 tap_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
479 {
480 struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
481 struct ifreq *ifr = (struct ifreq *)data;
482 int s, error;
483
484 s = splnet();
485
486 switch (cmd) {
487 case SIOCSIFMEDIA:
488 case SIOCGIFMEDIA:
489 error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
490 break;
491 case SIOCSIFPHYADDR:
492 error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
493 break;
494 default:
495 error = ether_ioctl(ifp, cmd, data);
496 if (error == ENETRESET)
497 error = 0;
498 break;
499 }
500
501 splx(s);
502
503 return (error);
504 }
505
506 /*
507 * Helper function to set Ethernet address. This shouldn't be done there,
508 * and should actually be available to all Ethernet drivers, real or not.
509 */
510 static int
511 tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
512 {
513 struct sockaddr *sa = (struct sockaddr *)&ifra->ifra_addr;
514
515 if (sa->sa_family != AF_LINK)
516 return (EINVAL);
517
518 memcpy(LLADDR(ifp->if_sadl), sa->sa_data, ETHER_ADDR_LEN);
519
520 return (0);
521 }
522
523 /*
524 * _init() would typically be called when an interface goes up,
525 * meaning it should configure itself into the state in which it
526 * can send packets.
527 */
528 static int
529 tap_init(struct ifnet *ifp)
530 {
531 ifp->if_flags |= IFF_RUNNING;
532
533 tap_start(ifp);
534
535 return (0);
536 }
537
538 /*
539 * _stop() is called when an interface goes down. It is our
540 * responsability to validate that state by clearing the
541 * IFF_RUNNING flag.
542 *
543 * We have to wake up all the sleeping processes to have the pending
544 * read requests cancelled.
545 */
546 static void
547 tap_stop(struct ifnet *ifp, int disable)
548 {
549 struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
550
551 ifp->if_flags &= ~IFF_RUNNING;
552 wakeup(sc);
553 selnotify(&sc->sc_rsel, 1);
554 if (sc->sc_flags & TAP_ASYNCIO)
555 fownsignal(sc->sc_pgid, SIGIO, POLL_HUP, 0, NULL);
556 }
557
558 /*
559 * The 'create' command of ifconfig can be used to create
560 * any numbered instance of a given device. Thus we have to
561 * make sure we have enough room in cd_devs to create the
562 * user-specified instance. config_attach_pseudo will do this
563 * for us.
564 */
565 static int
566 tap_clone_create(struct if_clone *ifc, int unit)
567 {
568 if (tap_clone_creator(unit) == NULL) {
569 aprint_error("%s%d: unable to attach an instance\n",
570 tap_cd.cd_name, unit);
571 return (ENXIO);
572 }
573
574 return (0);
575 }
576
577 /*
578 * tap(4) can be cloned by two ways:
579 * using 'ifconfig tap0 create', which will use the network
580 * interface cloning API, and call tap_clone_create above.
581 * opening the cloning device node, whose minor number is TAP_CLONER.
582 * See below for an explanation on how this part work.
583 *
584 * config_attach_pseudo can be called with unit = DVUNIT_ANY to have
585 * autoconf(9) choose a unit number for us. This is what happens when
586 * the cloner is openend, while the ifcloner interface creates a device
587 * with a specific unit number.
588 */
589 static struct tap_softc *
590 tap_clone_creator(int unit)
591 {
592 struct cfdata *cf;
593
594 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
595 cf->cf_name = tap_cd.cd_name;
596 cf->cf_atname = tap_ca.ca_name;
597 cf->cf_unit = unit;
598 cf->cf_fstate = FSTATE_STAR;
599
600 return (struct tap_softc *)config_attach_pseudo(cf);
601 }
602
603 /*
604 * The clean design of if_clone and autoconf(9) makes that part
605 * really straightforward. The second argument of config_detach
606 * means neither QUIET nor FORCED.
607 */
608 static int
609 tap_clone_destroy(struct ifnet *ifp)
610 {
611 return tap_clone_destroyer((struct device *)ifp->if_softc);
612 }
613
614 static int
615 tap_clone_destroyer(struct device *dev)
616 {
617 struct cfdata *cf = dev->dv_cfdata;
618 int error;
619
620 if ((error = config_detach(dev, 0)) != 0)
621 aprint_error("%s: unable to detach instance\n",
622 dev->dv_xname);
623 free(cf, M_DEVBUF);
624
625 return (error);
626 }
627
628 /*
629 * tap(4) is a bit of an hybrid device. It can be used in two different
630 * ways:
631 * 1. ifconfig tapN create, then use /dev/tapN to read/write off it.
632 * 2. open /dev/tap, get a new interface created and read/write off it.
633 * That interface is destroyed when the process that had it created exits.
634 *
635 * The first way is managed by the cdevsw structure, and you access interfaces
636 * through a (major, minor) mapping: tap4 is obtained by the minor number
637 * 4. The entry points for the cdevsw interface are prefixed by tap_cdev_.
638 *
639 * The second way is the so-called "cloning" device. It's a special minor
640 * number (chosen as the maximal number, to allow as much tap devices as
641 * possible). The user first opens the cloner (e.g., /dev/tap), and that
642 * call ends in tap_cdev_open. The actual place where it is handled is
643 * tap_dev_cloner.
644 *
645 * An tap device cannot be opened more than once at a time, so the cdevsw
646 * part of open() does nothing but noting that the interface is being used and
647 * hence ready to actually handle packets.
648 */
649
650 static int
651 tap_cdev_open(dev_t dev, int flags, int fmt, struct proc *p)
652 {
653 struct tap_softc *sc;
654
655 if (minor(dev) == TAP_CLONER)
656 return tap_dev_cloner(p);
657
658 sc = (struct tap_softc *)device_lookup(&tap_cd, minor(dev));
659 if (sc == NULL)
660 return (ENXIO);
661
662 /* The device can only be opened once */
663 if (sc->sc_flags & TAP_INUSE)
664 return (EBUSY);
665 sc->sc_flags |= TAP_INUSE;
666 return (0);
667 }
668
669 /*
670 * There are several kinds of cloning devices, and the most simple is the one
671 * tap(4) uses. What it does is change the file descriptor with a new one,
672 * with its own fileops structure (which maps to the various read, write,
673 * ioctl functions). It starts allocating a new file descriptor with falloc,
674 * then actually creates the new tap devices.
675 *
676 * Once those two steps are successful, we can re-wire the existing file
677 * descriptor to its new self. This is done with fdclone(): it fills the fp
678 * structure as needed (notably f_data gets filled with the fifth parameter
679 * passed, the unit of the tap device which will allows us identifying the
680 * device later), and returns EMOVEFD.
681 *
682 * That magic value is interpreted by sys_open() which then replaces the
683 * current file descriptor by the new one (through a magic member of struct
684 * proc, p_dupfd).
685 *
686 * The tap device is flagged as being busy since it otherwise could be
687 * externally accessed through the corresponding device node with the cdevsw
688 * interface.
689 */
690
691 static int
692 tap_dev_cloner(struct proc *p)
693 {
694 struct tap_softc *sc;
695 struct file *fp;
696 int error, fd;
697
698 if ((error = falloc(p, &fp, &fd)) != 0)
699 return (error);
700
701 if ((sc = tap_clone_creator(DVUNIT_ANY)) == NULL) {
702 FILE_UNUSE(fp, p);
703 ffree(fp);
704 return (ENXIO);
705 }
706
707 sc->sc_flags |= TAP_INUSE;
708
709 return fdclone(p, fp, fd, &tap_fileops, (void *)(intptr_t)sc->sc_dev.dv_unit);
710 }
711
712 /*
713 * While all other operations (read, write, ioctl, poll and kqfilter) are
714 * really the same whether we are in cdevsw or fileops mode, the close()
715 * function is slightly different in the two cases.
716 *
717 * As for the other, the core of it is shared in tap_dev_close. What
718 * it does is sufficient for the cdevsw interface, but the cloning interface
719 * needs another thing: the interface is destroyed when the processes that
720 * created it closes it.
721 */
722 static int
723 tap_cdev_close(dev_t dev, int flags, int fmt, struct proc *p)
724 {
725 struct tap_softc *sc =
726 (struct tap_softc *)device_lookup(&tap_cd, minor(dev));
727
728 if (sc == NULL)
729 return (ENXIO);
730
731 return tap_dev_close(sc);
732 }
733
734 /*
735 * It might happen that the administrator used ifconfig to externally destroy
736 * the interface. In that case, tap_fops_close will be called while
737 * tap_detach is already happening. If we called it again from here, we
738 * would dead lock. TAP_GOING ensures that this situation doesn't happen.
739 */
740 static int
741 tap_fops_close(struct file *fp, struct proc *p)
742 {
743 int unit = (intptr_t)fp->f_data;
744 struct tap_softc *sc;
745 int error;
746
747 sc = (struct tap_softc *)device_lookup(&tap_cd, unit);
748 if (sc == NULL)
749 return (ENXIO);
750
751 /* tap_dev_close currently always succeeds, but it might not
752 * always be the case. */
753 if ((error = tap_dev_close(sc)) != 0)
754 return (error);
755
756 /* Destroy the device now that it is no longer useful,
757 * unless it's already being destroyed. */
758 if ((sc->sc_flags & TAP_GOING) != 0)
759 return (0);
760
761 return tap_clone_destroyer((struct device *)sc);
762 }
763
764 static int
765 tap_dev_close(struct tap_softc *sc)
766 {
767 struct ifnet *ifp;
768 int s;
769
770 s = splnet();
771 /* Let tap_start handle packets again */
772 ifp = &sc->sc_ec.ec_if;
773 ifp->if_flags &= ~IFF_OACTIVE;
774
775 /* Purge output queue */
776 if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
777 struct mbuf *m;
778
779 for (;;) {
780 IFQ_DEQUEUE(&ifp->if_snd, m);
781 if (m == NULL)
782 break;
783
784 ifp->if_opackets++;
785 #if NBPFILTER > 0
786 if (ifp->if_bpf)
787 bpf_mtap(ifp->if_bpf, m);
788 #endif
789 }
790 }
791 splx(s);
792
793 sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
794
795 return (0);
796 }
797
798 static int
799 tap_cdev_read(dev_t dev, struct uio *uio, int flags)
800 {
801 return tap_dev_read(minor(dev), uio, flags);
802 }
803
804 static int
805 tap_fops_read(struct file *fp, off_t *offp, struct uio *uio,
806 struct ucred *cred, int flags)
807 {
808 return tap_dev_read((intptr_t)fp->f_data, uio, flags);
809 }
810
811 static int
812 tap_dev_read(int unit, struct uio *uio, int flags)
813 {
814 struct tap_softc *sc =
815 (struct tap_softc *)device_lookup(&tap_cd, unit);
816 struct ifnet *ifp;
817 struct mbuf *m, *n;
818 int error = 0, s;
819
820 if (sc == NULL)
821 return (ENXIO);
822
823 ifp = &sc->sc_ec.ec_if;
824 if ((ifp->if_flags & IFF_UP) == 0)
825 return (EHOSTDOWN);
826
827 /*
828 * In the TAP_NBIO case, we have to make sure we won't be sleeping
829 */
830 if ((sc->sc_flags & TAP_NBIO) &&
831 lockstatus(&sc->sc_rdlock) == LK_EXCLUSIVE)
832 return (EWOULDBLOCK);
833 error = lockmgr(&sc->sc_rdlock, LK_EXCLUSIVE, NULL);
834 if (error != 0)
835 return (error);
836
837 s = splnet();
838 if (IFQ_IS_EMPTY(&ifp->if_snd)) {
839 ifp->if_flags &= ~IFF_OACTIVE;
840 splx(s);
841 /*
842 * We must release the lock before sleeping, and re-acquire it
843 * after.
844 */
845 (void)lockmgr(&sc->sc_rdlock, LK_RELEASE, NULL);
846 if (sc->sc_flags & TAP_NBIO)
847 error = EWOULDBLOCK;
848 else
849 error = tsleep(sc, PSOCK|PCATCH, "tap", 0);
850
851 if (error != 0)
852 return (error);
853 /* The device might have been downed */
854 if ((ifp->if_flags & IFF_UP) == 0)
855 return (EHOSTDOWN);
856 if ((sc->sc_flags & TAP_NBIO) &&
857 lockstatus(&sc->sc_rdlock) == LK_EXCLUSIVE)
858 return (EWOULDBLOCK);
859 error = lockmgr(&sc->sc_rdlock, LK_EXCLUSIVE, NULL);
860 if (error != 0)
861 return (error);
862 s = splnet();
863 }
864
865 IFQ_DEQUEUE(&ifp->if_snd, m);
866 ifp->if_flags &= ~IFF_OACTIVE;
867 splx(s);
868 if (m == NULL) {
869 error = 0;
870 goto out;
871 }
872
873 ifp->if_opackets++;
874 #if NBPFILTER > 0
875 if (ifp->if_bpf)
876 bpf_mtap(ifp->if_bpf, m);
877 #endif
878
879 /*
880 * One read is one packet.
881 */
882 do {
883 error = uiomove(mtod(m, caddr_t),
884 min(m->m_len, uio->uio_resid), uio);
885 MFREE(m, n);
886 m = n;
887 } while (m != NULL && uio->uio_resid > 0 && error == 0);
888
889 if (m != NULL)
890 m_freem(m);
891
892 out:
893 (void)lockmgr(&sc->sc_rdlock, LK_RELEASE, NULL);
894 return (error);
895 }
896
897 static int
898 tap_cdev_write(dev_t dev, struct uio *uio, int flags)
899 {
900 return tap_dev_write(minor(dev), uio, flags);
901 }
902
903 static int
904 tap_fops_write(struct file *fp, off_t *offp, struct uio *uio,
905 struct ucred *cred, int flags)
906 {
907 return tap_dev_write((intptr_t)fp->f_data, uio, flags);
908 }
909
910 static int
911 tap_dev_write(int unit, struct uio *uio, int flags)
912 {
913 struct tap_softc *sc =
914 (struct tap_softc *)device_lookup(&tap_cd, unit);
915 struct ifnet *ifp;
916 struct mbuf *m, **mp;
917 int error = 0;
918
919 if (sc == NULL)
920 return (ENXIO);
921
922 ifp = &sc->sc_ec.ec_if;
923
924 /* One write, one packet, that's the rule */
925 MGETHDR(m, M_DONTWAIT, MT_DATA);
926 if (m == NULL) {
927 ifp->if_ierrors++;
928 return (ENOBUFS);
929 }
930 m->m_pkthdr.len = uio->uio_resid;
931
932 mp = &m;
933 while (error == 0 && uio->uio_resid > 0) {
934 if (*mp != m) {
935 MGET(*mp, M_DONTWAIT, MT_DATA);
936 if (*mp == NULL) {
937 error = ENOBUFS;
938 break;
939 }
940 }
941 (*mp)->m_len = min(MHLEN, uio->uio_resid);
942 error = uiomove(mtod(*mp, caddr_t), (*mp)->m_len, uio);
943 mp = &(*mp)->m_next;
944 }
945 if (error) {
946 ifp->if_ierrors++;
947 m_freem(m);
948 return (error);
949 }
950
951 ifp->if_ipackets++;
952 m->m_pkthdr.rcvif = ifp;
953
954 #if NBPFILTER > 0
955 if (ifp->if_bpf)
956 bpf_mtap(ifp->if_bpf, m);
957 #endif
958 (*ifp->if_input)(ifp, m);
959
960 return (0);
961 }
962
963 static int
964 tap_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
965 struct proc *p)
966 {
967 return tap_dev_ioctl(minor(dev), cmd, data, p);
968 }
969
970 static int
971 tap_fops_ioctl(struct file *fp, u_long cmd, void *data, struct proc *p)
972 {
973 return tap_dev_ioctl((intptr_t)fp->f_data, cmd, (caddr_t)data, p);
974 }
975
976 static int
977 tap_dev_ioctl(int unit, u_long cmd, caddr_t data, struct proc *p)
978 {
979 struct tap_softc *sc =
980 (struct tap_softc *)device_lookup(&tap_cd, unit);
981 int error = 0;
982
983 if (sc == NULL)
984 return (ENXIO);
985
986 switch (cmd) {
987 case FIONREAD:
988 {
989 struct ifnet *ifp = &sc->sc_ec.ec_if;
990 struct mbuf *m;
991 int s;
992
993 s = splnet();
994 IFQ_POLL(&ifp->if_snd, m);
995
996 if (m == NULL)
997 *(int *)data = 0;
998 else
999 *(int *)data = m->m_pkthdr.len;
1000 splx(s);
1001 } break;
1002 case TIOCSPGRP:
1003 case FIOSETOWN:
1004 error = fsetown(p, &sc->sc_pgid, cmd, data);
1005 break;
1006 case TIOCGPGRP:
1007 case FIOGETOWN:
1008 error = fgetown(p, sc->sc_pgid, cmd, data);
1009 break;
1010 case FIOASYNC:
1011 if (*(int *)data)
1012 sc->sc_flags |= TAP_ASYNCIO;
1013 else
1014 sc->sc_flags &= ~TAP_ASYNCIO;
1015 break;
1016 case FIONBIO:
1017 if (*(int *)data)
1018 sc->sc_flags |= TAP_NBIO;
1019 else
1020 sc->sc_flags &= ~TAP_NBIO;
1021 break;
1022 case TAPGIFNAME:
1023 {
1024 struct ifreq *ifr = (struct ifreq *)data;
1025 struct ifnet *ifp = &sc->sc_ec.ec_if;
1026
1027 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1028 } break;
1029 default:
1030 error = ENOTTY;
1031 break;
1032 }
1033
1034 return (0);
1035 }
1036
1037 static int
1038 tap_cdev_poll(dev_t dev, int events, struct proc *p)
1039 {
1040 return tap_dev_poll(minor(dev), events, p);
1041 }
1042
1043 static int
1044 tap_fops_poll(struct file *fp, int events, struct proc *p)
1045 {
1046 return tap_dev_poll((intptr_t)fp->f_data, events, p);
1047 }
1048
1049 static int
1050 tap_dev_poll(int unit, int events, struct proc *p)
1051 {
1052 struct tap_softc *sc =
1053 (struct tap_softc *)device_lookup(&tap_cd, unit);
1054 int revents = 0;
1055
1056 if (sc == NULL)
1057 return (ENXIO);
1058
1059 if (events & (POLLIN|POLLRDNORM)) {
1060 struct ifnet *ifp = &sc->sc_ec.ec_if;
1061 struct mbuf *m;
1062 int s;
1063
1064 s = splnet();
1065 IFQ_POLL(&ifp->if_snd, m);
1066 splx(s);
1067
1068 if (m != NULL)
1069 revents |= events & (POLLIN|POLLRDNORM);
1070 else {
1071 (void)simple_lock(&sc->sc_kqlock);
1072 selrecord(p, &sc->sc_rsel);
1073 simple_unlock(&sc->sc_kqlock);
1074 }
1075 }
1076 revents |= events & (POLLOUT|POLLWRNORM);
1077
1078 return (revents);
1079 }
1080
1081 static struct filterops tap_read_filterops = { 1, NULL, tap_kqdetach,
1082 tap_kqread };
1083 static struct filterops tap_seltrue_filterops = { 1, NULL, tap_kqdetach,
1084 filt_seltrue };
1085
1086 static int
1087 tap_cdev_kqfilter(dev_t dev, struct knote *kn)
1088 {
1089 return tap_dev_kqfilter(minor(dev), kn);
1090 }
1091
1092 static int
1093 tap_fops_kqfilter(struct file *fp, struct knote *kn)
1094 {
1095 return tap_dev_kqfilter((intptr_t)fp->f_data, kn);
1096 }
1097
1098 static int
1099 tap_dev_kqfilter(int unit, struct knote *kn)
1100 {
1101 struct tap_softc *sc =
1102 (struct tap_softc *)device_lookup(&tap_cd, unit);
1103
1104 if (sc == NULL)
1105 return (ENXIO);
1106
1107 switch(kn->kn_filter) {
1108 case EVFILT_READ:
1109 kn->kn_fop = &tap_read_filterops;
1110 break;
1111 case EVFILT_WRITE:
1112 kn->kn_fop = &tap_seltrue_filterops;
1113 break;
1114 default:
1115 return (1);
1116 }
1117
1118 kn->kn_hook = sc;
1119 (void)simple_lock(&sc->sc_kqlock);
1120 SLIST_INSERT_HEAD(&sc->sc_rsel.sel_klist, kn, kn_selnext);
1121 simple_unlock(&sc->sc_kqlock);
1122 return (0);
1123 }
1124
1125 static void
1126 tap_kqdetach(struct knote *kn)
1127 {
1128 struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1129
1130 (void)simple_lock(&sc->sc_kqlock);
1131 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
1132 simple_unlock(&sc->sc_kqlock);
1133 }
1134
1135 static int
1136 tap_kqread(struct knote *kn, long hint)
1137 {
1138 struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1139 struct ifnet *ifp = &sc->sc_ec.ec_if;
1140 struct mbuf *m;
1141 int s;
1142
1143 s = splnet();
1144 IFQ_POLL(&ifp->if_snd, m);
1145
1146 if (m == NULL)
1147 kn->kn_data = 0;
1148 else
1149 kn->kn_data = m->m_pkthdr.len;
1150 splx(s);
1151 return (kn->kn_data != 0 ? 1 : 0);
1152 }
1153
1154 /*
1155 * sysctl management routines
1156 * You can set the address of an interface through:
1157 * net.link.tap.tap<number>
1158 *
1159 * Note the consistent use of tap_log in order to use
1160 * sysctl_teardown at unload time.
1161 *
1162 * In the kernel you will find a lot of SYSCTL_SETUP blocks. Those
1163 * blocks register a function in a special section of the kernel
1164 * (called a link set) which is used at init_sysctl() time to cycle
1165 * through all those functions to create the kernel's sysctl tree.
1166 *
1167 * It is not (currently) possible to use link sets in a LKM, so the
1168 * easiest is to simply call our own setup routine at load time.
1169 *
1170 * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
1171 * CTLFLAG_PERMANENT flag, meaning they cannot be removed. Once the
1172 * whole kernel sysctl tree is built, it is not possible to add any
1173 * permanent node.
1174 *
1175 * It should be noted that we're not saving the sysctlnode pointer
1176 * we are returned when creating the "tap" node. That structure
1177 * cannot be trusted once out of the calling function, as it might
1178 * get reused. So we just save the MIB number, and always give the
1179 * full path starting from the root for later calls to sysctl_createv
1180 * and sysctl_destroyv.
1181 */
1182 SYSCTL_SETUP(sysctl_tap_setup, "sysctl net.link.tap subtree setup")
1183 {
1184 struct sysctlnode *node;
1185 int error = 0;
1186
1187 if ((error = sysctl_createv(clog, 0, NULL, NULL,
1188 CTLFLAG_PERMANENT,
1189 CTLTYPE_NODE, "net", NULL,
1190 NULL, 0, NULL, 0,
1191 CTL_NET, CTL_EOL)) != 0)
1192 return;
1193
1194 if ((error = sysctl_createv(clog, 0, NULL, NULL,
1195 CTLFLAG_PERMANENT,
1196 CTLTYPE_NODE, "link", NULL,
1197 NULL, 0, NULL, 0,
1198 CTL_NET, PF_LINK, CTL_EOL)) != 0)
1199 return;
1200
1201 /*
1202 * The first four parameters of sysctl_createv are for management.
1203 *
1204 * The four that follows, here starting with a '0' for the flags,
1205 * describe the node.
1206 *
1207 * The next series of four set its value, through various possible
1208 * means.
1209 *
1210 * Last but not least, the path to the node is described. That path
1211 * is relative to the given root (third argument). Here we're
1212 * starting from the root.
1213 */
1214 if ((error = sysctl_createv(clog, 0, NULL, &node,
1215 CTLFLAG_PERMANENT,
1216 CTLTYPE_NODE, "tap", NULL,
1217 NULL, 0, NULL, 0,
1218 CTL_NET, PF_LINK, CTL_CREATE, CTL_EOL)) != 0)
1219 return;
1220 tap_node = node->sysctl_num;
1221 }
1222
1223 /*
1224 * The helper functions make Andrew Brown's interface really
1225 * shine. It makes possible to create value on the fly whether
1226 * the sysctl value is read or written.
1227 *
1228 * As shown as an example in the man page, the first step is to
1229 * create a copy of the node to have sysctl_lookup work on it.
1230 *
1231 * Here, we have more work to do than just a copy, since we have
1232 * to create the string. The first step is to collect the actual
1233 * value of the node, which is a convenient pointer to the softc
1234 * of the interface. From there we create the string and use it
1235 * as the value, but only for the *copy* of the node.
1236 *
1237 * Then we let sysctl_lookup do the magic, which consists in
1238 * setting oldp and newp as required by the operation. When the
1239 * value is read, that means that the string will be copied to
1240 * the user, and when it is written, the new value will be copied
1241 * over in the addr array.
1242 *
1243 * If newp is NULL, the user was reading the value, so we don't
1244 * have anything else to do. If a new value was written, we
1245 * have to check it.
1246 *
1247 * If it is incorrect, we can return an error and leave 'node' as
1248 * it is: since it is a copy of the actual node, the change will
1249 * be forgotten.
1250 *
1251 * Upon a correct input, we commit the change to the ifnet
1252 * structure of our interface.
1253 */
1254 static int
1255 tap_sysctl_handler(SYSCTLFN_ARGS)
1256 {
1257 struct sysctlnode node;
1258 struct tap_softc *sc;
1259 struct ifnet *ifp;
1260 int error;
1261 size_t len;
1262 char addr[18];
1263
1264 node = *rnode;
1265 sc = node.sysctl_data;
1266 ifp = &sc->sc_ec.ec_if;
1267 (void)tap_ether_sprintf(addr, LLADDR(ifp->if_sadl));
1268 node.sysctl_data = addr;
1269 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1270 if (error || newp == NULL)
1271 return (error);
1272
1273 len = strlen(addr);
1274 if (len < 11 || len > 17)
1275 return (EINVAL);
1276
1277 /* Commit change */
1278 if (tap_ether_aton(LLADDR(ifp->if_sadl), addr) != 0)
1279 return (EINVAL);
1280 return (error);
1281 }
1282
1283 /*
1284 * ether_aton implementation, not using a static buffer.
1285 */
1286 static int
1287 tap_ether_aton(u_char *dest, char *str)
1288 {
1289 int i;
1290 char *cp = str;
1291 u_char val[6];
1292
1293 #define set_value \
1294 if (*cp > '9' && *cp < 'a') \
1295 *cp -= 'A' - 10; \
1296 else if (*cp > '9') \
1297 *cp -= 'a' - 10; \
1298 else \
1299 *cp -= '0'
1300
1301 for (i = 0; i < 6; i++, cp++) {
1302 if (!isxdigit(*cp))
1303 return (1);
1304 set_value;
1305 val[i] = *cp++;
1306 if (isxdigit(*cp)) {
1307 set_value;
1308 val[i] *= 16;
1309 val[i] += *cp++;
1310 }
1311 if (*cp == ':' || i == 5)
1312 continue;
1313 else
1314 return (1);
1315 }
1316 memcpy(dest, val, 6);
1317 return (0);
1318 }
1319
1320 /*
1321 * ether_sprintf made thread-safer.
1322 *
1323 * Copied over from sys/net/if_ethersubr.c, with a change to avoid the use
1324 * of a static buffer.
1325 */
1326
1327 /*
1328 * Copyright (c) 1982, 1989, 1993
1329 * The Regents of the University of California. All rights reserved.
1330 *
1331 * Redistribution and use in source and binary forms, with or without
1332 * modification, are permitted provided that the following conditions
1333 * are met:
1334 * 1. Redistributions of source code must retain the above copyright
1335 * notice, this list of conditions and the following disclaimer.
1336 * 2. Redistributions in binary form must reproduce the above copyright
1337 * notice, this list of conditions and the following disclaimer in the
1338 * documentation and/or other materials provided with the distribution.
1339 * 3. Neither the name of the University nor the names of its contributors
1340 * may be used to endorse or promote products derived from this software
1341 * without specific prior written permission.
1342 *
1343 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1344 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1345 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1346 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1347 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1348 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1349 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1350 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1351 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1352 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1353 * SUCH DAMAGE.
1354 *
1355 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
1356 */
1357
1358 static char digits[] = "0123456789abcdef";
1359 static char *
1360 tap_ether_sprintf(char *dest, const u_char *ap)
1361 {
1362 char *cp = dest;
1363 int i;
1364
1365 for (i = 0; i < 6; i++) {
1366 *cp++ = digits[*ap >> 4];
1367 *cp++ = digits[*ap++ & 0xf];
1368 *cp++ = ':';
1369 }
1370 *--cp = 0;
1371 return (dest);
1372 }
1373