xref: /haiku/src/libs/compat/freebsd_network/if.c (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 /*
2  * Copyright 2009, Colin Günther, coling@gmx.de.
3  * Copyright 2007-2009, Axel Dörfler, axeld@pinc-software.de.
4  * Copyright 2007, Hugo Santos. All Rights Reserved.
5  * Copyright 2004, Marcus Overhagen. All Rights Reserved.
6  *
7  * Distributed under the terms of the MIT License.
8  */
9 
10 
11 #include "device.h"
12 
13 #include <stdio.h>
14 #include <net/if_types.h>
15 #include <sys/sockio.h>
16 
17 #include <compat/sys/bus.h>
18 #include <compat/sys/kernel.h>
19 #include <compat/sys/taskqueue.h>
20 
21 #include <compat/net/bpf.h>
22 #include <compat/net/ethernet.h>
23 #include <compat/net/if.h>
24 #include <compat/net/if_arp.h>
25 #include <compat/net/if_media.h>
26 #include <compat/net/if_var.h>
27 #include <compat/net/if_vlan_var.h>
28 #include <compat/sys/malloc.h>
29 
30 
31 
32 int ifqmaxlen = IFQ_MAXLEN;
33 
34 static void	if_input_default(struct ifnet *, struct mbuf *);
35 static int	if_requestencap_default(struct ifnet *, struct if_encap_req *);
36 
37 
38 
39 #define IFNET_HOLD (void *)(uintptr_t)(-1)
40 
41 
42 static void
43 insert_into_device_name_list(struct ifnet * ifp)
44 {
45 	int i;
46 	for (i = 0; i < MAX_DEVICES; i++) {
47 		if (gDeviceNameList[i] == NULL) {
48 			gDeviceNameList[i] = ifp->device_name;
49 			return;
50 		}
51 	}
52 
53 	panic("too many devices");
54 }
55 
56 
57 static void
58 remove_from_device_name_list(struct ifnet * ifp)
59 {
60 	int i;
61 	for (i = 0; i < MAX_DEVICES; i++) {
62 		if (ifp->device_name == gDeviceNameList[i]) {
63 			int last;
64 			for (last = i + 1; last < MAX_DEVICES; last++) {
65 				if (gDeviceNameList[last] == NULL)
66 					break;
67 			}
68 			last--;
69 
70 			if (i == last)
71 				gDeviceNameList[i] = NULL;
72 			else {
73 				// switch positions with the last entry
74 				gDeviceNameList[i] = gDeviceNameList[last];
75 				gDeviceNameList[last] = NULL;
76 			}
77 			break;
78 		}
79 	}
80 }
81 
82 
83 struct ifnet *
84 ifnet_byindex(u_short idx)
85 {
86 	struct ifnet *ifp;
87 
88 	IFNET_RLOCK_NOSLEEP();
89 	ifp = ifnet_byindex_locked(idx);
90 	IFNET_RUNLOCK_NOSLEEP();
91 
92 	return (ifp);
93 }
94 
95 
96 struct ifnet *
97 ifnet_byindex_locked(u_short idx)
98 {
99 	struct ifnet *ifp;
100 
101 	ifp = gDevices[idx];
102 
103 	return (ifp);
104 }
105 
106 
107 static void
108 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
109 {
110 	gDevices[idx] = ifp;
111 }
112 
113 
114 static void
115 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
116 {
117 	IFNET_WLOCK();
118 	ifnet_setbyindex_locked(idx, ifp);
119 	IFNET_WUNLOCK();
120 }
121 
122 
123 static int
124 ifindex_alloc_locked(u_short *idxp)
125 {
126 	u_short index;
127 
128 	for (index = 0; index < MAX_DEVICES; index++) {
129 		if (gDevices[index] == NULL) {
130 			break;
131 		}
132 	}
133 
134 	if (index == MAX_DEVICES)
135 		return ENOSPC;
136 
137 	gDeviceCount++;
138 	*idxp = index;
139 
140 	return ENOERR;
141 }
142 
143 
144 static void
145 ifindex_free_locked(u_short idx)
146 {
147 	gDevices[idx] = NULL;
148 	gDeviceCount--;
149 }
150 
151 
152 struct ifnet *
153 if_alloc(u_char type)
154 {
155 	char semName[64];
156 	u_short index;
157 
158 	struct ifnet *ifp = _kernel_malloc(sizeof(struct ifnet), M_ZERO);
159 	if (ifp == NULL)
160 		return NULL;
161 
162 	snprintf(semName, sizeof(semName), "%s receive", gDriverName);
163 
164 	ifp->receive_sem = create_sem(0, semName);
165 	if (ifp->receive_sem < B_OK)
166 		goto err1;
167 
168 	ifp->link_state_sem = -1;
169 	ifp->open_count = 0;
170 	ifp->flags = 0;
171 	ifp->if_type = type;
172 	ifq_init(&ifp->receive_queue, semName);
173 
174 	ifp->scan_done_sem = -1;
175 		// WLAN specific, doesn't hurt when initialized for other devices
176 
177 	// Search for the first free device slot, and use that one
178 	IFNET_WLOCK();
179 	if (ifindex_alloc_locked(&index) != ENOERR) {
180 		IFNET_WUNLOCK();
181 		panic("too many devices");
182 		goto err3;
183 	}
184 	ifnet_setbyindex_locked(index, IFNET_HOLD);
185 	IFNET_WUNLOCK();
186 
187 	ifp->if_index = index;
188 	ifnet_setbyindex(ifp->if_index, ifp);
189 
190 	IF_ADDR_LOCK_INIT(ifp);
191 	return ifp;
192 
193 err3:
194 	switch (type) {
195 		case IFT_ETHER:
196 			_kernel_free(ifp->if_l2com);
197 			break;
198 	}
199 
200 err2:
201 	delete_sem(ifp->receive_sem);
202 
203 err1:
204 	_kernel_free(ifp);
205 	return NULL;
206 }
207 
208 
209 void
210 if_free(struct ifnet *ifp)
211 {
212 	// IEEE80211 devices won't be in this list,
213 	// so don't try to remove them.
214 	if (ifp->if_type == IFT_ETHER)
215 		remove_from_device_name_list(ifp);
216 
217 	IFNET_WLOCK();
218 	ifindex_free_locked(ifp->if_index);
219 	IFNET_WUNLOCK();
220 
221 	IF_ADDR_LOCK_DESTROY(ifp);
222 	switch (ifp->if_type) {
223 		case IFT_ETHER:
224 			_kernel_free(ifp->if_l2com);
225 			break;
226 	}
227 
228 	delete_sem(ifp->receive_sem);
229 	ifq_uninit(&ifp->receive_queue);
230 
231 	_kernel_free(ifp);
232 }
233 
234 
235 void
236 if_initname(struct ifnet *ifp, const char *name, int unit)
237 {
238 	dprintf("if_initname(%p, %s, %d)\n", ifp, name, unit);
239 
240 	if (name == NULL || name[0] == '\0')
241 		panic("interface goes unnamed");
242 
243 	ifp->if_dname = name;
244 	ifp->if_dunit = unit;
245 
246 	strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
247 
248 	snprintf(ifp->device_name, sizeof(ifp->device_name), "net/%s/%i",
249 		gDriverName, ifp->if_index);
250 
251 	driver_printf("%s: /dev/%s\n", gDriverName, ifp->device_name);
252 	insert_into_device_name_list(ifp);
253 
254 	ifp->root_device = find_root_device(unit);
255 }
256 
257 
258 void
259 ifq_init(struct ifqueue *ifq, const char *name)
260 {
261 	ifq->ifq_head = NULL;
262 	ifq->ifq_tail = NULL;
263 	ifq->ifq_len = 0;
264 	ifq->ifq_maxlen = IFQ_MAXLEN;
265 	ifq->ifq_drops = 0;
266 
267 	mtx_init(&ifq->ifq_mtx, name, NULL, MTX_DEF);
268 }
269 
270 
271 void
272 ifq_uninit(struct ifqueue *ifq)
273 {
274 	mtx_destroy(&ifq->ifq_mtx);
275 }
276 
277 
278 static int
279 if_transmit(struct ifnet *ifp, struct mbuf *m)
280 {
281 	int error;
282 
283 	IFQ_HANDOFF(ifp, m, error);
284 	return (error);
285 }
286 
287 
288 static void
289 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
290 {
291 
292 	m_freem(m);
293 }
294 
295 
296 /*
297  * Flush an interface queue.
298  */
299 void
300 if_qflush(struct ifnet *ifp)
301 {
302 	struct mbuf *m, *n;
303 	struct ifaltq *ifq;
304 
305 	ifq = &ifp->if_snd;
306 	IFQ_LOCK(ifq);
307 #ifdef ALTQ
308 	if (ALTQ_IS_ENABLED(ifq))
309 		ALTQ_PURGE(ifq);
310 #endif
311 	n = ifq->ifq_head;
312 	while ((m = n) != NULL) {
313 		n = m->m_nextpkt;
314 		m_freem(m);
315 	}
316 	ifq->ifq_head = 0;
317 	ifq->ifq_tail = 0;
318 	ifq->ifq_len = 0;
319 	IFQ_UNLOCK(ifq);
320 }
321 
322 
323 void
324 if_attach(struct ifnet *ifp)
325 {
326 	unsigned socksize, ifasize;
327 	int namelen, masklen;
328 	struct sockaddr_dl *sdl;
329 	struct ifaddr *ifa;
330 
331 	TAILQ_INIT(&ifp->if_addrhead);
332 	TAILQ_INIT(&ifp->if_prefixhead);
333 	TAILQ_INIT(&ifp->if_multiaddrs);
334 
335 	IF_ADDR_LOCK_INIT(ifp);
336 
337 	ifp->if_lladdr.sdl_family = AF_LINK;
338 
339 	ifq_init((struct ifqueue *) &ifp->if_snd, ifp->if_xname);
340 
341 	if (ifp->if_transmit == NULL) {
342 		ifp->if_transmit = if_transmit;
343 		ifp->if_qflush = if_qflush;
344 	}
345 	if (ifp->if_input == NULL)
346 		ifp->if_input = if_input_default;
347 
348 	if (ifp->if_requestencap == NULL)
349 		ifp->if_requestencap = if_requestencap_default;
350 
351 	/*
352 	 * Create a Link Level name for this device.
353 	 */
354 	namelen = strlen(ifp->if_xname);
355 	/*
356 	 * Always save enough space for any possiable name so we
357 	 * can do a rename in place later.
358 	 */
359 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
360 	socksize = masklen + ifp->if_addrlen;
361 	if (socksize < sizeof(*sdl))
362 		socksize = sizeof(*sdl);
363 	socksize = roundup2(socksize, sizeof(long));
364 	ifasize = sizeof(*ifa) + 2 * socksize;
365 	ifa = ifa_alloc(ifasize, M_WAITOK);
366 	sdl = (struct sockaddr_dl *)(ifa + 1);
367 	sdl->sdl_len = socksize;
368 	sdl->sdl_family = AF_LINK;
369 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
370 	sdl->sdl_nlen = namelen;
371 	sdl->sdl_index = ifp->if_index;
372 	sdl->sdl_type = ifp->if_type;
373 	ifp->if_addr = ifa;
374 	ifa->ifa_ifp = ifp;
375 	//ifa->ifa_rtrequest = link_rtrequest;
376 	ifa->ifa_addr = (struct sockaddr *)sdl;
377 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
378 	ifa->ifa_netmask = (struct sockaddr *)sdl;
379 	sdl->sdl_len = masklen;
380 	while (namelen != 0)
381 		sdl->sdl_data[--namelen] = 0xff;
382 	dprintf("if_attach %p\n", ifa->ifa_addr);
383 }
384 
385 
386 void
387 if_detach(struct ifnet *ifp)
388 {
389 	if (HAIKU_DRIVER_REQUIRES(FBSD_SWI_TASKQUEUE))
390 		taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
391 
392 	IF_ADDR_LOCK_DESTROY(ifp);
393 	ifq_uninit((struct ifqueue *) &ifp->if_snd);
394 }
395 
396 
397 void
398 if_start(struct ifnet *ifp)
399 {
400 #ifdef IFF_NEEDSGIANT
401 	if (ifp->if_flags & IFF_NEEDSGIANT)
402 	panic("freebsd compat.: unsupported giant requirement");
403 #endif
404 	ifp->if_start(ifp);
405 }
406 
407 
408 int
409 if_printf(struct ifnet *ifp, const char *format, ...)
410 {
411 	char buf[256];
412 	va_list vl;
413 	va_start(vl, format);
414 	vsnprintf(buf, sizeof(buf), format, vl);
415 	va_end(vl);
416 
417 	dprintf("[%s] %s", ifp->device_name, buf);
418 	return 0;
419 }
420 
421 
422 /*
423  * Compat function for handling basic encapsulation requests.
424  * Not converted stacks (FDDI, IB, ..) supports traditional
425  * output model: ARP (and other similar L2 protocols) are handled
426  * inside output routine, arpresolve/nd6_resolve() returns MAC
427  * address instead of full prepend.
428  *
429  * This function creates calculated header==MAC for IPv4/IPv6 and
430  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
431  * address families.
432  */
433 static int
434 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
435 {
436 
437 	if (req->rtype != IFENCAP_LL)
438 		return (EOPNOTSUPP);
439 
440 	if (req->bufsize < req->lladdr_len)
441 		return (ENOMEM);
442 
443 	switch (req->family) {
444 	case AF_INET:
445 	case AF_INET6:
446 		break;
447 	default:
448 		return (EAFNOSUPPORT);
449 	}
450 
451 	/* Copy lladdr to storage as is */
452 	memmove(req->buf, req->lladdr, req->lladdr_len);
453 	req->bufsize = req->lladdr_len;
454 	req->lladdr_off = 0;
455 
456 	return (0);
457 }
458 
459 
460 void
461 if_link_state_change(struct ifnet *ifp, int linkState)
462 {
463 	if (ifp->if_link_state == linkState)
464 		return;
465 
466 	ifp->if_link_state = linkState;
467 	release_sem_etc(ifp->link_state_sem, 1, B_DO_NOT_RESCHEDULE);
468 }
469 
470 static struct ifmultiaddr *
471 if_findmulti(struct ifnet *ifp, struct sockaddr *_address)
472 {
473 	struct sockaddr_dl *address = (struct sockaddr_dl *) _address;
474 	struct ifmultiaddr *ifma;
475 
476 	TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) {
477 		if (memcmp(LLADDR(address),
478 			LLADDR((struct sockaddr_dl *)ifma->ifma_addr), ETHER_ADDR_LEN) == 0)
479 			return ifma;
480 	}
481 
482 	return NULL;
483 }
484 
485 
486 /*
487  * if_freemulti: free ifmultiaddr structure and possibly attached related
488  * addresses.  The caller is responsible for implementing reference
489  * counting, notifying the driver, handling routing messages, and releasing
490  * any dependent link layer state.
491  */
492 static void
493 if_freemulti(struct ifmultiaddr *ifma)
494 {
495 
496 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
497 	    ifma->ifma_refcount));
498 	KASSERT(ifma->ifma_protospec == NULL,
499 	    ("if_freemulti: protospec not NULL"));
500 
501 	if (ifma->ifma_lladdr != NULL)
502 		free(ifma->ifma_lladdr);
503 
504 	// Haiku note: We use a field in the ifmultiaddr struct (ifma_addr_storage)
505 	// to store the address and let ifma_addr point to that. We therefore do not
506 	// free it here, as it will be freed as part of freeing the if_multiaddr.
507 	//free(ifma->ifma_addr);
508 
509 	free(ifma);
510 }
511 
512 
513 static struct ifmultiaddr *
514 _if_addmulti(struct ifnet *ifp, struct sockaddr *address)
515 {
516 	struct ifmultiaddr *addr = if_findmulti(ifp, address);
517 
518 	if (addr != NULL) {
519 		addr->ifma_refcount++;
520 		return addr;
521 	}
522 
523 	addr = (struct ifmultiaddr *) malloc(sizeof(struct ifmultiaddr));
524 	if (addr == NULL)
525 		return NULL;
526 
527 	addr->ifma_lladdr = NULL;
528 	addr->ifma_ifp = ifp;
529 	addr->ifma_protospec = NULL;
530 
531 	memcpy(&addr->ifma_addr_storage, address, sizeof(struct sockaddr_dl));
532 	addr->ifma_addr = (struct sockaddr *) &addr->ifma_addr_storage;
533 
534 	addr->ifma_refcount = 1;
535 
536 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, addr, ifma_link);
537 
538 	return addr;
539 }
540 
541 
542 int
543 if_addmulti(struct ifnet *ifp, struct sockaddr *address,
544 	struct ifmultiaddr **out)
545 {
546 	struct ifmultiaddr *result;
547 	int refcount = 0;
548 
549 	IF_ADDR_LOCK(ifp);
550 	result = _if_addmulti(ifp, address);
551 	if (result)
552 		refcount = result->ifma_refcount;
553 	IF_ADDR_UNLOCK(ifp);
554 
555 	if (result == NULL)
556 		return ENOBUFS;
557 
558 	if (refcount == 1 && ifp->if_ioctl != NULL)
559 		ifp->if_ioctl(ifp, SIOCADDMULTI, NULL);
560 
561 	if (out)
562 		(*out) = result;
563 
564 	return 0;
565 }
566 
567 
568 static int
569 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
570 {
571 	struct ifmultiaddr *ll_ifma;
572 
573 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
574 		KASSERT(ifma->ifma_ifp == ifp,
575 		    ("%s: inconsistent ifp %p", __func__, ifp));
576 		IF_ADDR_LOCK_ASSERT(ifp);
577 	}
578 
579 	ifp = ifma->ifma_ifp;
580 
581 	/*
582 	 * If the ifnet is detaching, null out references to ifnet,
583 	 * so that upper protocol layers will notice, and not attempt
584 	 * to obtain locks for an ifnet which no longer exists. The
585 	 * routing socket announcement must happen before the ifnet
586 	 * instance is detached from the system.
587 	 */
588 	if (detaching) {
589 #ifdef DIAGNOSTIC
590 		printf("%s: detaching ifnet instance %p\n", __func__, ifp);
591 #endif
592 		/*
593 		 * ifp may already be nulled out if we are being reentered
594 		 * to delete the ll_ifma.
595 		 */
596 		if (ifp != NULL) {
597 #ifndef __HAIKU__
598 			rt_newmaddrmsg(RTM_DELMADDR, ifma);
599 #endif
600 			ifma->ifma_ifp = NULL;
601 		}
602 	}
603 
604 	if (--ifma->ifma_refcount > 0)
605 		return 0;
606 
607 #ifndef __HAIKU__
608 	/*
609 	 * If this ifma is a network-layer ifma, a link-layer ifma may
610 	 * have been associated with it. Release it first if so.
611 	 */
612 	ll_ifma = ifma->ifma_llifma;
613 	if (ll_ifma != NULL) {
614 		KASSERT(ifma->ifma_lladdr != NULL,
615 		    ("%s: llifma w/o lladdr", __func__));
616 		if (detaching)
617 			ll_ifma->ifma_ifp = NULL;	/* XXX */
618 		if (--ll_ifma->ifma_refcount == 0) {
619 			if (ifp != NULL) {
620 				TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
621 				    ifma_link);
622 			}
623 			if_freemulti(ll_ifma);
624 		}
625 	}
626 #endif
627 
628 	if (ifp != NULL)
629 		TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
630 
631 	if_freemulti(ifma);
632 
633 	/*
634 	 * The last reference to this instance of struct ifmultiaddr
635 	 * was released; the hardware should be notified of this change.
636 	 */
637 	return 1;
638 }
639 
640 
641 /*
642  * Delete all multicast group membership for an interface.
643  * Should be used to quickly flush all multicast filters.
644  */
645 void
646 if_delallmulti(struct ifnet *ifp)
647 {
648 	struct ifmultiaddr *ifma;
649 	struct ifmultiaddr *next;
650 
651 	IF_ADDR_LOCK(ifp);
652 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
653 		if_delmulti_locked(ifp, ifma, 0);
654 	IF_ADDR_UNLOCK(ifp);
655 }
656 
657 
658 static void
659 if_delete_multiaddr(struct ifnet *ifp, struct ifmultiaddr *ifma)
660 {
661 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
662 	free(ifma);
663 }
664 
665 
666 int
667 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
668 {
669 	struct ifmultiaddr *ifma;
670 	int lastref;
671 #if 0 /* def INVARIANTS */
672 	struct ifnet *oifp;
673 
674 	IFNET_RLOCK_NOSLEEP();
675 	TAILQ_FOREACH(oifp, &V_ifnet, if_link)
676 		if (ifp == oifp)
677 			break;
678 	if (ifp != oifp)
679 		ifp = NULL;
680 	IFNET_RUNLOCK_NOSLEEP();
681 
682 	KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
683 #endif
684 	if (ifp == NULL)
685 		return (ENOENT);
686 
687 	IF_ADDR_LOCK(ifp);
688 	lastref = 0;
689 	ifma = if_findmulti(ifp, sa);
690 	if (ifma != NULL)
691 		lastref = if_delmulti_locked(ifp, ifma, 0);
692 	IF_ADDR_UNLOCK(ifp);
693 
694 	if (ifma == NULL)
695 		return (ENOENT);
696 
697 	if (lastref && ifp->if_ioctl != NULL) {
698 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
699 	}
700 
701 	return (0);
702 }
703 
704 
705 void
706 if_purgemaddrs(struct ifnet *ifp)
707 {
708 	struct ifmultiaddr *ifma;
709 	struct ifmultiaddr *next;
710 
711 	IF_ADDR_LOCK(ifp);
712 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
713 		if_delmulti_locked(ifp, ifma, 1);
714 	IF_ADDR_UNLOCK(ifp);
715 }
716 
717 /*
718  * Return counter values from counter(9)s stored in ifnet.
719  */
720 uint64_t
721 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
722 {
723 
724 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
725 
726 	switch (cnt) {
727 		case IFCOUNTER_IPACKETS:
728 			return atomic_get64((int64 *)&ifp->if_ipackets);
729 		case IFCOUNTER_IERRORS:
730 			return atomic_get64((int64 *)&ifp->if_ierrors);
731 		case IFCOUNTER_OPACKETS:
732 			return atomic_get64((int64 *)&ifp->if_opackets);
733 		case IFCOUNTER_OERRORS:
734 			return atomic_get64((int64 *)&ifp->if_oerrors);
735 		case IFCOUNTER_COLLISIONS:
736 			return atomic_get64((int64 *)&ifp->if_collisions);
737 		case IFCOUNTER_IBYTES:
738 			return atomic_get64((int64 *)&ifp->if_ibytes);
739 		case IFCOUNTER_OBYTES:
740 			return atomic_get64((int64 *)&ifp->if_obytes);
741 		case IFCOUNTER_IMCASTS:
742 			return atomic_get64((int64 *)&ifp->if_imcasts);
743 		case IFCOUNTER_OMCASTS:
744 			return atomic_get64((int64 *)&ifp->if_omcasts);
745 		case IFCOUNTER_IQDROPS:
746 			return atomic_get64((int64 *)&ifp->if_iqdrops);
747 		case IFCOUNTER_OQDROPS:
748 			return atomic_get64((int64 *)&ifp->if_oqdrops);
749 		case IFCOUNTER_NOPROTO:
750 			return atomic_get64((int64 *)&ifp->if_noproto);
751 		case IFCOUNTERS:
752 			KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
753 	}
754 	return 0;
755 }
756 
757 void
758 if_addr_rlock(struct ifnet *ifp)
759 {
760 	IF_ADDR_LOCK(ifp);
761 }
762 
763 
764 void
765 if_addr_runlock(struct ifnet *ifp)
766 {
767 	IF_ADDR_UNLOCK(ifp);
768 }
769 
770 
771 void
772 if_maddr_rlock(struct ifnet *ifp)
773 {
774 	IF_ADDR_LOCK(ifp);
775 }
776 
777 
778 void
779 if_maddr_runlock(struct ifnet *ifp)
780 {
781 	IF_ADDR_UNLOCK(ifp);
782 }
783 
784 
785 int
786 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
787 	struct route *ro)
788 {
789 	int error = 0;
790 	IFQ_HANDOFF(ifp, m, error);
791 	return error;
792 }
793 
794 
795 static void ether_input(struct ifnet *ifp, struct mbuf *m)
796 {
797 	IF_ENQUEUE(&ifp->receive_queue, m);
798 	release_sem_etc(ifp->receive_sem, 1, B_DO_NOT_RESCHEDULE);
799 }
800 
801 
802 void
803 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
804 {
805 	struct ifaddr *ifa;
806 	struct sockaddr_dl *sdl;
807 
808 	ifp->if_addrlen = ETHER_ADDR_LEN;
809 	ifp->if_hdrlen = ETHER_HDR_LEN;
810 	if_attach(ifp);
811 	ifp->if_mtu = ETHERMTU;
812 	ifp->if_output = ether_output;
813 	ifp->if_input = ether_input;
814 	ifp->if_resolvemulti = NULL; // done in the stack
815 	ifp->if_get_counter = NULL;
816 	ifp->if_broadcastaddr = etherbroadcastaddr;
817 
818 	ifa = ifp->if_addr;
819 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
820 	sdl->sdl_type = IFT_ETHER;
821 	sdl->sdl_alen = ifp->if_addrlen;
822 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
823 }
824 
825 
826 void
827 ether_ifdetach(struct ifnet *ifp)
828 {
829 	if_detach(ifp);
830 }
831 
832 
833 int
834 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
835 {
836 	struct ifreq *ifr = (struct ifreq *) data;
837 
838 	//dprintf("ether_ioctl: received %d\n", command);
839 
840 	switch (command) {
841 		case SIOCSIFMTU:
842 			if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
843 				return EINVAL;
844 			ifp->if_mtu = ifr->ifr_mtu;
845 			break;
846 
847 		default:
848 			return EINVAL;
849 	}
850 
851 	return 0;
852 }
853 
854 
855 /*
856  * Initialization, destruction and refcounting functions for ifaddrs.
857  */
858 struct ifaddr *
859 ifa_alloc(size_t size, int flags)
860 {
861 	struct ifaddr *ifa;
862 
863 	KASSERT(size >= sizeof(struct ifaddr),
864 	    ("%s: invalid size %zu", __func__, size));
865 
866 	ifa = _kernel_malloc(size, M_ZERO | flags);
867 	if (ifa == NULL)
868 		return (NULL);
869 
870 	//refcount_init(&ifa->ifa_refcnt, 1);
871 
872 	return (ifa);
873 
874 fail:
875 	/* free(NULL) is okay */
876 	free(ifa);
877 
878 	return (NULL);
879 }
880 
881 void
882 ifa_ref(struct ifaddr *ifa)
883 {
884 	//refcount_acquire(&ifa->ifa_refcnt);
885 }
886 
887 void
888 ifa_free(struct ifaddr *ifa)
889 {
890 
891 	//if (refcount_release(&ifa->ifa_refcnt)) {
892 	//	free(ifa);
893 	//}
894 }
895 
896 void
897 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
898 {
899 	switch (cnt) {
900 		case IFCOUNTER_IPACKETS:
901 			atomic_add64((int64 *)&ifp->if_ipackets, inc);
902 			break;
903 		case IFCOUNTER_IERRORS:
904 			atomic_add64((int64 *)&ifp->if_ierrors, inc);
905 			break;
906 		case IFCOUNTER_OPACKETS:
907 			atomic_add64((int64 *)&ifp->if_opackets, inc);
908 			break;
909 		case IFCOUNTER_OERRORS:
910 			atomic_add64((int64 *)&ifp->if_oerrors, inc);
911 			break;
912 		case IFCOUNTER_COLLISIONS:
913 			atomic_add64((int64 *)&ifp->if_collisions, inc);
914 			break;
915 		case IFCOUNTER_IBYTES:
916 			atomic_add64((int64 *)&ifp->if_ibytes, inc);
917 			break;
918 		case IFCOUNTER_OBYTES:
919 			atomic_add64((int64 *)&ifp->if_obytes, inc);
920 			break;
921 		case IFCOUNTER_IMCASTS:
922 			atomic_add64((int64 *)&ifp->if_imcasts, inc);
923 			break;
924 		case IFCOUNTER_OMCASTS:
925 			atomic_add64((int64 *)&ifp->if_omcasts, inc);
926 			break;
927 		case IFCOUNTER_IQDROPS:
928 			atomic_add64((int64 *)&ifp->if_iqdrops, inc);
929 			break;
930 		case IFCOUNTER_OQDROPS:
931 			atomic_add64((int64 *)&ifp->if_oqdrops, inc);
932 			break;
933 		case IFCOUNTER_NOPROTO:
934 			atomic_add64((int64 *)&ifp->if_noproto, inc);
935 			break;
936 		case IFCOUNTERS:
937 			KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
938 	}
939 }
940 
941 
942 /* API for driver access to network stack owned ifnet.*/
943 uint64_t
944 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
945 {
946 	uint64_t oldbrate;
947 
948 	oldbrate = ifp->if_baudrate;
949 	ifp->if_baudrate = baudrate;
950 	return (oldbrate);
951 }
952 
953 uint64_t
954 if_getbaudrate(if_t ifp)
955 {
956 
957 	return (((struct ifnet *)ifp)->if_baudrate);
958 }
959 
960 int
961 if_setcapabilities(if_t ifp, int capabilities)
962 {
963 	((struct ifnet *)ifp)->if_capabilities = capabilities;
964 	return (0);
965 }
966 
967 int
968 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
969 {
970 	((struct ifnet *)ifp)->if_capabilities |= setbit;
971 	((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
972 
973 	return (0);
974 }
975 
976 int
977 if_getcapabilities(if_t ifp)
978 {
979 	return ((struct ifnet *)ifp)->if_capabilities;
980 }
981 
982 int
983 if_setcapenable(if_t ifp, int capabilities)
984 {
985 	((struct ifnet *)ifp)->if_capenable = capabilities;
986 	return (0);
987 }
988 
989 int
990 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
991 {
992 	if(setcap)
993 		((struct ifnet *)ifp)->if_capenable |= setcap;
994 	if(clearcap)
995 		((struct ifnet *)ifp)->if_capenable &= ~clearcap;
996 
997 	return (0);
998 }
999 
1000 const char *
1001 if_getdname(if_t ifp)
1002 {
1003 	return ((struct ifnet *)ifp)->if_dname;
1004 }
1005 
1006 int
1007 if_togglecapenable(if_t ifp, int togglecap)
1008 {
1009 	((struct ifnet *)ifp)->if_capenable ^= togglecap;
1010 	return (0);
1011 }
1012 
1013 int
1014 if_getcapenable(if_t ifp)
1015 {
1016 	return ((struct ifnet *)ifp)->if_capenable;
1017 }
1018 
1019 /*
1020  * This is largely undesirable because it ties ifnet to a device, but does
1021  * provide flexiblity for an embedded product vendor. Should be used with
1022  * the understanding that it violates the interface boundaries, and should be
1023  * a last resort only.
1024  */
1025 int
1026 if_setdev(if_t ifp, void *dev)
1027 {
1028 	return (0);
1029 }
1030 
1031 int
1032 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
1033 {
1034 	((struct ifnet *)ifp)->if_drv_flags |= set_flags;
1035 	((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
1036 
1037 	return (0);
1038 }
1039 
1040 int
1041 if_getdrvflags(if_t ifp)
1042 {
1043 	if ((struct ifnet *)ifp == NULL)
1044 		return 0;
1045 	return ((struct ifnet *)ifp)->if_drv_flags;
1046 }
1047 
1048 int
1049 if_setdrvflags(if_t ifp, int flags)
1050 {
1051 	((struct ifnet *)ifp)->if_drv_flags = flags;
1052 	return (0);
1053 }
1054 
1055 
1056 int
1057 if_setflags(if_t ifp, int flags)
1058 {
1059 	((struct ifnet *)ifp)->if_flags = flags;
1060 	return (0);
1061 }
1062 
1063 int
1064 if_setflagbits(if_t ifp, int set, int clear)
1065 {
1066 	((struct ifnet *)ifp)->if_flags |= set;
1067 	((struct ifnet *)ifp)->if_flags &= ~clear;
1068 
1069 	return (0);
1070 }
1071 
1072 int
1073 if_getflags(if_t ifp)
1074 {
1075 	return ((struct ifnet *)ifp)->if_flags;
1076 }
1077 
1078 int
1079 if_clearhwassist(if_t ifp)
1080 {
1081 	((struct ifnet *)ifp)->if_hwassist = 0;
1082 	return (0);
1083 }
1084 
1085 int
1086 if_sethwassistbits(if_t ifp, int toset, int toclear)
1087 {
1088 	((struct ifnet *)ifp)->if_hwassist |= toset;
1089 	((struct ifnet *)ifp)->if_hwassist &= ~toclear;
1090 
1091 	return (0);
1092 }
1093 
1094 int
1095 if_sethwassist(if_t ifp, int hwassist_bit)
1096 {
1097 	((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
1098 	return (0);
1099 }
1100 
1101 int
1102 if_gethwassist(if_t ifp)
1103 {
1104 	return ((struct ifnet *)ifp)->if_hwassist;
1105 }
1106 
1107 int
1108 if_setmtu(if_t ifp, int mtu)
1109 {
1110 	((struct ifnet *)ifp)->if_mtu = mtu;
1111 	return (0);
1112 }
1113 
1114 int
1115 if_getmtu(if_t ifp)
1116 {
1117 	return ((struct ifnet *)ifp)->if_mtu;
1118 }
1119 
1120 int
1121 if_setsoftc(if_t ifp, void *softc)
1122 {
1123 	((struct ifnet *)ifp)->if_softc = softc;
1124 	return (0);
1125 }
1126 
1127 void *
1128 if_getsoftc(if_t ifp)
1129 {
1130 	return ((struct ifnet *)ifp)->if_softc;
1131 }
1132 
1133 void
1134 if_setrcvif(struct mbuf *m, if_t ifp)
1135 {
1136 	m->m_pkthdr.rcvif = (struct ifnet *)ifp;
1137 }
1138 
1139 void
1140 if_setvtag(struct mbuf *m, uint16_t tag)
1141 {
1142 	m->m_pkthdr.ether_vtag = tag;
1143 }
1144 
1145 uint16_t
1146 if_getvtag(struct mbuf *m)
1147 {
1148 
1149 	return (m->m_pkthdr.ether_vtag);
1150 }
1151 
1152 int
1153 if_sendq_empty(if_t ifp)
1154 {
1155 	return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
1156 }
1157 
1158 int
1159 if_getamcount(if_t ifp)
1160 {
1161 	return ((struct ifnet *)ifp)->if_amcount;
1162 }
1163 
1164 
1165 int
1166 if_setsendqready(if_t ifp)
1167 {
1168 	IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
1169 	return (0);
1170 }
1171 
1172 int
1173 if_setsendqlen(if_t ifp, int tx_desc_count)
1174 {
1175 	IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
1176 	((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
1177 
1178 	return (0);
1179 }
1180 
1181 int
1182 if_vlantrunkinuse(if_t ifp)
1183 {
1184 	return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
1185 }
1186 
1187 int
1188 if_input(if_t ifp, struct mbuf* sendmp)
1189 {
1190 	(*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
1191 	return (0);
1192 
1193 }
1194 
1195 /* XXX */
1196 #ifndef ETH_ADDR_LEN
1197 #define ETH_ADDR_LEN 6
1198 #endif
1199 
1200 int
1201 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
1202 {
1203 	struct ifmultiaddr *ifma;
1204 	uint8_t *lmta = (uint8_t *)mta;
1205 	int mcnt = 0;
1206 
1207 	TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
1208 		if (ifma->ifma_addr->sa_family != AF_LINK)
1209 			continue;
1210 
1211 		if (mcnt == max)
1212 			break;
1213 
1214 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1215 		    &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
1216 		mcnt++;
1217 	}
1218 	*cnt = mcnt;
1219 
1220 	return (0);
1221 }
1222 
1223 int
1224 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
1225 {
1226 	int error;
1227 
1228 	if_maddr_rlock(ifp);
1229 	error = if_setupmultiaddr(ifp, mta, cnt, max);
1230 	if_maddr_runlock(ifp);
1231 	return (error);
1232 }
1233 
1234 int
1235 if_multiaddr_count(if_t ifp, int max)
1236 {
1237 	struct ifmultiaddr *ifma;
1238 	int count;
1239 
1240 	count = 0;
1241 	if_maddr_rlock(ifp);
1242 	TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
1243 		if (ifma->ifma_addr->sa_family != AF_LINK)
1244 			continue;
1245 		count++;
1246 		if (count == max)
1247 			break;
1248 	}
1249 	if_maddr_runlock(ifp);
1250 	return (count);
1251 }
1252 
1253 u_int
1254 if_llmaddr_count(if_t ifp)
1255 {
1256 	struct ifmultiaddr *ifma;
1257 	int count;
1258 
1259 	count = 0;
1260 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1261 		if (ifma->ifma_addr->sa_family == AF_LINK)
1262 			count++;
1263 	}
1264 
1265 	return (count);
1266 }
1267 
1268 u_int
1269 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
1270 {
1271 	struct ifmultiaddr *ifma;
1272 	u_int count;
1273 
1274 	count = 0;
1275 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1276 		if (ifma->ifma_addr->sa_family != AF_LINK)
1277 			continue;
1278 		count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
1279 			count);
1280 	}
1281 
1282 	return (count);
1283 }
1284 
1285 struct mbuf *
1286 if_dequeue(if_t ifp)
1287 {
1288 	struct mbuf *m;
1289 	IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
1290 
1291 	return (m);
1292 }
1293 
1294 int
1295 if_sendq_prepend(if_t ifp, struct mbuf *m)
1296 {
1297 	IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
1298 	return (0);
1299 }
1300 
1301 int
1302 if_setifheaderlen(if_t ifp, int len)
1303 {
1304 	((struct ifnet *)ifp)->if_hdrlen = len;
1305 	return (0);
1306 }
1307 
1308 caddr_t
1309 if_getlladdr(if_t ifp)
1310 {
1311 	return (IF_LLADDR((struct ifnet *)ifp));
1312 }
1313 
1314 void *
1315 if_gethandle(u_char type)
1316 {
1317 	return (if_alloc(type));
1318 }
1319 
1320 void
1321 if_bpfmtap(if_t ifh, struct mbuf *m)
1322 {
1323 	struct ifnet *ifp = (struct ifnet *)ifh;
1324 
1325 	BPF_MTAP(ifp, m);
1326 }
1327 
1328 void
1329 if_etherbpfmtap(if_t ifh, struct mbuf *m)
1330 {
1331 	struct ifnet *ifp = (struct ifnet *)ifh;
1332 
1333 	ETHER_BPF_MTAP(ifp, m);
1334 }
1335 
1336 void
1337 if_vlancap(if_t ifh)
1338 {
1339 	struct ifnet *ifp = (struct ifnet *)ifh;
1340 	VLAN_CAPABILITIES(ifp);
1341 }
1342 
1343 void
1344 if_setinitfn(if_t ifp, void (*init_fn)(void *))
1345 {
1346 	((struct ifnet *)ifp)->if_init = init_fn;
1347 }
1348 
1349 void
1350 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
1351 {
1352 	((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
1353 }
1354 
1355 void
1356 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
1357 {
1358 	((struct ifnet *)ifp)->if_start = (void *)start_fn;
1359 }
1360 
1361 void
1362 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
1363 {
1364 	((struct ifnet *)ifp)->if_transmit = start_fn;
1365 }
1366 
1367 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
1368 {
1369 	((struct ifnet *)ifp)->if_qflush = flush_fn;
1370 }
1371 
1372 void
1373 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
1374 {
1375 
1376 	ifp->if_get_counter = fn;
1377 }
1378 
1379