xref: /netbsd-src/usr.sbin/btpand/channel.c (revision a14e16bfe0cd7334f67c9ca4305e409996d4935f)
1*a14e16bfSplunky /*	$NetBSD: channel.c,v 1.4 2011/01/27 11:13:59 plunky Exp $	*/
21fc74d21Splunky 
31fc74d21Splunky /*-
41fc74d21Splunky  * Copyright (c) 2008 Iain Hibbert
51fc74d21Splunky  * All rights reserved.
61fc74d21Splunky  *
71fc74d21Splunky  * Redistribution and use in source and binary forms, with or without
81fc74d21Splunky  * modification, are permitted provided that the following conditions
91fc74d21Splunky  * are met:
101fc74d21Splunky  * 1. Redistributions of source code must retain the above copyright
111fc74d21Splunky  *    notice, this list of conditions and the following disclaimer.
121fc74d21Splunky  * 2. Redistributions in binary form must reproduce the above copyright
131fc74d21Splunky  *    notice, this list of conditions and the following disclaimer in the
141fc74d21Splunky  *    documentation and/or other materials provided with the distribution.
151fc74d21Splunky  *
161fc74d21Splunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171fc74d21Splunky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181fc74d21Splunky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191fc74d21Splunky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201fc74d21Splunky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211fc74d21Splunky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221fc74d21Splunky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231fc74d21Splunky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241fc74d21Splunky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251fc74d21Splunky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261fc74d21Splunky  */
271fc74d21Splunky 
281fc74d21Splunky #include <sys/cdefs.h>
29*a14e16bfSplunky __RCSID("$NetBSD: channel.c,v 1.4 2011/01/27 11:13:59 plunky Exp $");
301fc74d21Splunky 
311fc74d21Splunky #include <sys/ioctl.h>
321fc74d21Splunky 
331fc74d21Splunky #include <unistd.h>
341fc74d21Splunky 
351fc74d21Splunky #include "btpand.h"
361fc74d21Splunky 
371fc74d21Splunky static struct chlist	channel_list;
381fc74d21Splunky static int		channel_tick;
391fc74d21Splunky 
401fc74d21Splunky static void channel_start(int, short, void *);
411fc74d21Splunky static void channel_read(int, short, void *);
421fc74d21Splunky static void channel_dispatch(packet_t *);
431fc74d21Splunky static void channel_watchdog(int, short, void *);
441fc74d21Splunky 
451fc74d21Splunky void
channel_init(void)461fc74d21Splunky channel_init(void)
471fc74d21Splunky {
481fc74d21Splunky 
491fc74d21Splunky 	LIST_INIT(&channel_list);
501fc74d21Splunky }
511fc74d21Splunky 
521fc74d21Splunky channel_t *
channel_alloc(void)531fc74d21Splunky channel_alloc(void)
541fc74d21Splunky {
551fc74d21Splunky 	channel_t *chan;
561fc74d21Splunky 
571fc74d21Splunky 	chan = malloc(sizeof(channel_t));
581fc74d21Splunky 	if (chan == NULL) {
591fc74d21Splunky 		log_err("%s() failed: %m", __func__);
601fc74d21Splunky 		return NULL;
611fc74d21Splunky 	}
621fc74d21Splunky 
631fc74d21Splunky 	memset(chan, 0, sizeof(channel_t));
641fc74d21Splunky 	STAILQ_INIT(&chan->pktlist);
651fc74d21Splunky 	chan->state = CHANNEL_CLOSED;
661fc74d21Splunky 	LIST_INSERT_HEAD(&channel_list, chan, next);
671fc74d21Splunky 
681fc74d21Splunky 	return chan;
691fc74d21Splunky }
701fc74d21Splunky 
711fc74d21Splunky bool
channel_open(channel_t * chan,int fd)721fc74d21Splunky channel_open(channel_t *chan, int fd)
731fc74d21Splunky {
741fc74d21Splunky 	int n;
751fc74d21Splunky 
7602f520acSplunky 	assert(chan->refcnt == 0);
7702f520acSplunky 	assert(chan->state != CHANNEL_CLOSED);
78493f204dSplunky 	assert(chan->send != NULL);
79493f204dSplunky 	assert(chan->recv != NULL);
80493f204dSplunky 	assert(chan->down != NULL);
811fc74d21Splunky 
821fc74d21Splunky 	if (chan->mtu > 0) {
831fc74d21Splunky 		chan->sendbuf = malloc(chan->mtu);
841fc74d21Splunky 		if (chan->sendbuf == NULL) {
851fc74d21Splunky 			log_err("Could not malloc channel sendbuf: %m");
861fc74d21Splunky 			return false;
871fc74d21Splunky 		}
881fc74d21Splunky 	}
891fc74d21Splunky 
901fc74d21Splunky 	n = 1;
911fc74d21Splunky 	if (ioctl(fd, FIONBIO, &n) == -1) {
921fc74d21Splunky 		log_err("Could not set non-blocking IO: %m");
931fc74d21Splunky 		return false;
941fc74d21Splunky 	}
951fc74d21Splunky 
961fc74d21Splunky 	event_set(&chan->rd_ev, fd, EV_READ | EV_PERSIST, channel_read, chan);
971fc74d21Splunky 	if (event_add(&chan->rd_ev, NULL) == -1) {
981fc74d21Splunky 		log_err("Could not add channel read event: %m");
991fc74d21Splunky 		return false;
1001fc74d21Splunky 	}
1011fc74d21Splunky 
1021fc74d21Splunky 	event_set(&chan->wr_ev, fd, EV_WRITE, channel_start, chan);
1031fc74d21Splunky 
1041fc74d21Splunky 	chan->refcnt++;
1051fc74d21Splunky 	chan->fd = fd;
1061fc74d21Splunky 
1071fc74d21Splunky 	log_debug("(fd#%d)", chan->fd);
1081fc74d21Splunky 
1091fc74d21Splunky 	return true;
1101fc74d21Splunky }
1111fc74d21Splunky 
1121fc74d21Splunky void
channel_close(channel_t * chan)1131fc74d21Splunky channel_close(channel_t *chan)
1141fc74d21Splunky {
1151fc74d21Splunky 	pkthdr_t *ph;
1161fc74d21Splunky 
11702f520acSplunky 	assert(chan->state != CHANNEL_CLOSED);
1181fc74d21Splunky 
1191fc74d21Splunky 	log_debug("(fd#%d)", chan->fd);
1201fc74d21Splunky 
1211fc74d21Splunky 	chan->state = CHANNEL_CLOSED;
1221fc74d21Splunky 	event_del(&chan->rd_ev);
1231fc74d21Splunky 	event_del(&chan->wr_ev);
1241fc74d21Splunky 	close(chan->fd);
1251fc74d21Splunky 	chan->refcnt--;
1261fc74d21Splunky 	chan->tick = 0;
1271fc74d21Splunky 
1281fc74d21Splunky 	while ((ph = STAILQ_FIRST(&chan->pktlist)) != NULL) {
1291fc74d21Splunky 		STAILQ_REMOVE_HEAD(&chan->pktlist, next);
1301fc74d21Splunky 		pkthdr_free(ph);
1311fc74d21Splunky 		chan->qlen--;
1321fc74d21Splunky 	}
1331fc74d21Splunky 
1341fc74d21Splunky 	if (chan->refcnt == 0)
1351fc74d21Splunky 		channel_free(chan);
1361fc74d21Splunky }
1371fc74d21Splunky 
1381fc74d21Splunky void
channel_free(channel_t * chan)1391fc74d21Splunky channel_free(channel_t *chan)
1401fc74d21Splunky {
1411fc74d21Splunky 
14202f520acSplunky 	assert(chan->refcnt == 0);
14302f520acSplunky 	assert(chan->state == CHANNEL_CLOSED);
14402f520acSplunky 	assert(chan->qlen == 0);
14502f520acSplunky 	assert(STAILQ_EMPTY(&chan->pktlist));
1461fc74d21Splunky 
1471fc74d21Splunky 	LIST_REMOVE(chan, next);
1481fc74d21Splunky 	free(chan->pfilter);
1491fc74d21Splunky 	free(chan->mfilter);
1501fc74d21Splunky 	free(chan->sendbuf);
1511fc74d21Splunky 	free(chan);
1521fc74d21Splunky }
1531fc74d21Splunky 
1541fc74d21Splunky static void
channel_start(int fd,short ev,void * arg)1551fc74d21Splunky channel_start(int fd, short ev, void *arg)
1561fc74d21Splunky {
1571fc74d21Splunky 	channel_t *chan = arg;
1581fc74d21Splunky 	pkthdr_t *ph;
1591fc74d21Splunky 
1601fc74d21Splunky 	chan->oactive = true;
1611fc74d21Splunky 
1621fc74d21Splunky 	while (chan->qlen > 0) {
1631fc74d21Splunky 		ph = STAILQ_FIRST(&chan->pktlist);
1641fc74d21Splunky 
1651fc74d21Splunky 		channel_timeout(chan, 10);
1661fc74d21Splunky 		if (chan->send(chan, ph->data) == false) {
1671fc74d21Splunky 			if (event_add(&chan->wr_ev, NULL) == -1) {
1681fc74d21Splunky 				log_err("Could not add channel write event: %m");
169493f204dSplunky 				chan->down(chan);
1701fc74d21Splunky 			}
1711fc74d21Splunky 			return;
1721fc74d21Splunky 		}
1731fc74d21Splunky 
1741fc74d21Splunky 		STAILQ_REMOVE_HEAD(&chan->pktlist, next);
1751fc74d21Splunky 		pkthdr_free(ph);
1761fc74d21Splunky 		chan->qlen--;
1771fc74d21Splunky 	}
1781fc74d21Splunky 
1791fc74d21Splunky 	channel_timeout(chan, 0);
1801fc74d21Splunky 	chan->oactive = false;
1811fc74d21Splunky }
1821fc74d21Splunky 
1831fc74d21Splunky static void
channel_read(int fd,short ev,void * arg)1841fc74d21Splunky channel_read(int fd, short ev, void *arg)
1851fc74d21Splunky {
1861fc74d21Splunky 	channel_t *chan = arg;
1871fc74d21Splunky 	packet_t *pkt;
1881fc74d21Splunky 	ssize_t nr;
1891fc74d21Splunky 
1901fc74d21Splunky 	pkt = packet_alloc(chan);
1911fc74d21Splunky 	if (pkt == NULL) {
192493f204dSplunky 		chan->down(chan);
1931fc74d21Splunky 		return;
1941fc74d21Splunky 	}
1951fc74d21Splunky 
1961fc74d21Splunky 	nr = read(fd, pkt->buf, chan->mru);
1971fc74d21Splunky 	if (nr == -1) {
1981fc74d21Splunky 		log_err("channel read error: %m");
1991fc74d21Splunky 		packet_free(pkt);
200493f204dSplunky 		chan->down(chan);
2011fc74d21Splunky 		return;
2021fc74d21Splunky 	}
2031fc74d21Splunky 	if (nr == 0) {	/* EOF */
2041fc74d21Splunky 		log_debug("(fd#%d) EOF", fd);
2051fc74d21Splunky 		packet_free(pkt);
206493f204dSplunky 		chan->down(chan);
2071fc74d21Splunky 		return;
2081fc74d21Splunky 	}
2091fc74d21Splunky 	pkt->len = nr;
2101fc74d21Splunky 
2111fc74d21Splunky 	if (chan->recv(pkt) == true)
2121fc74d21Splunky 		channel_dispatch(pkt);
2131fc74d21Splunky 
2141fc74d21Splunky 	packet_free(pkt);
2151fc74d21Splunky }
2161fc74d21Splunky 
2171fc74d21Splunky static void
channel_dispatch(packet_t * pkt)2181fc74d21Splunky channel_dispatch(packet_t *pkt)
2191fc74d21Splunky {
2201fc74d21Splunky 	channel_t *chan;
2211fc74d21Splunky 
2221fc74d21Splunky 	/*
2231fc74d21Splunky 	 * This is simple routing. I'm not sure if its allowed by
2241fc74d21Splunky 	 * the PAN or BNEP specifications, but it seems logical
2251fc74d21Splunky 	 * to send unicast packets to connected destinations where
2261fc74d21Splunky 	 * possible.
2271fc74d21Splunky 	 */
2281fc74d21Splunky 	if (!ETHER_IS_MULTICAST(pkt->dst)) {
2291fc74d21Splunky 		LIST_FOREACH(chan, &channel_list, next) {
2301fc74d21Splunky 			if (chan == pkt->chan
2311fc74d21Splunky 			    || chan->state != CHANNEL_OPEN)
2321fc74d21Splunky 				continue;
2331fc74d21Splunky 
2341fc74d21Splunky 			if (memcmp(pkt->dst, chan->raddr, ETHER_ADDR_LEN) == 0) {
2351fc74d21Splunky 				if (chan->qlen > CHANNEL_MAXQLEN)
2361fc74d21Splunky 					log_notice("Queue overflow");
2371fc74d21Splunky 				else
2381fc74d21Splunky 					channel_put(chan, pkt);
2391fc74d21Splunky 
2401fc74d21Splunky 				return;
2411fc74d21Splunky 			}
2421fc74d21Splunky 		}
2431fc74d21Splunky 	}
2441fc74d21Splunky 
2451fc74d21Splunky 	LIST_FOREACH(chan, &channel_list, next) {
2461fc74d21Splunky 		if (chan == pkt->chan
2471fc74d21Splunky 		    || chan->state != CHANNEL_OPEN)
2481fc74d21Splunky 			continue;
2491fc74d21Splunky 
2501fc74d21Splunky 		if (chan->qlen > CHANNEL_MAXQLEN) {
2511fc74d21Splunky 			log_notice("Queue overflow");
2521fc74d21Splunky 			continue;
2531fc74d21Splunky 		}
2541fc74d21Splunky 
2551fc74d21Splunky 		channel_put(chan, pkt);
2561fc74d21Splunky 	}
2571fc74d21Splunky }
2581fc74d21Splunky 
2591fc74d21Splunky void
channel_put(channel_t * chan,packet_t * pkt)2601fc74d21Splunky channel_put(channel_t *chan, packet_t *pkt)
2611fc74d21Splunky {
2621fc74d21Splunky 	pkthdr_t *ph;
2631fc74d21Splunky 
2641fc74d21Splunky 	ph = pkthdr_alloc(pkt);
2651fc74d21Splunky 	if (ph == NULL)
2661fc74d21Splunky 		return;
2671fc74d21Splunky 
2681fc74d21Splunky 	chan->qlen++;
2691fc74d21Splunky 	STAILQ_INSERT_TAIL(&chan->pktlist, ph, next);
2701fc74d21Splunky 
2711fc74d21Splunky 	if (!chan->oactive)
2721fc74d21Splunky 		channel_start(chan->fd, EV_WRITE, chan);
2731fc74d21Splunky }
2741fc74d21Splunky 
2751fc74d21Splunky /*
2761fc74d21Splunky  * Simple watchdog timer, only ticks when it is required and
2771fc74d21Splunky  * closes the channel down if it times out.
2781fc74d21Splunky  */
2791fc74d21Splunky void
channel_timeout(channel_t * chan,int to)2801fc74d21Splunky channel_timeout(channel_t *chan, int to)
2811fc74d21Splunky {
2821fc74d21Splunky 	static struct event ev;
2831fc74d21Splunky 
2841fc74d21Splunky 	if (to == 0)
2851fc74d21Splunky 		chan->tick = 0;
2861fc74d21Splunky 	else
2871fc74d21Splunky 		chan->tick = (channel_tick + to) % 60;
2881fc74d21Splunky 
2891fc74d21Splunky 	if (channel_tick == 0) {
2901fc74d21Splunky 		evtimer_set(&ev, channel_watchdog, &ev);
2911fc74d21Splunky 		channel_watchdog(0, 0, &ev);
2921fc74d21Splunky 	}
2931fc74d21Splunky }
2941fc74d21Splunky 
2951fc74d21Splunky static void
channel_watchdog(int fd,short ev,void * arg)2961fc74d21Splunky channel_watchdog(int fd, short ev, void *arg)
2971fc74d21Splunky {
2981fc74d21Splunky 	static struct timeval tv = { .tv_sec = 1 };
2991fc74d21Splunky 	channel_t *chan, *next;
3001fc74d21Splunky 	int tick;
3011fc74d21Splunky 
3021fc74d21Splunky 	tick = (channel_tick % 60) + 1;
3031fc74d21Splunky 	channel_tick = 0;
3041fc74d21Splunky 
3051fc74d21Splunky 	next = LIST_FIRST(&channel_list);
3061fc74d21Splunky 	while ((chan = next) != NULL) {
3071fc74d21Splunky 		next = LIST_NEXT(chan, next);
3081fc74d21Splunky 
3091fc74d21Splunky 		if (chan->tick == tick)
310493f204dSplunky 			chan->down(chan);
3111fc74d21Splunky 		else if (chan->tick != 0)
3121fc74d21Splunky 			channel_tick = tick;
3131fc74d21Splunky 	}
3141fc74d21Splunky 
315*a14e16bfSplunky 	if (channel_tick != 0 && evtimer_add(arg, &tv) == -1) {
3161fc74d21Splunky 		log_err("Could not add watchdog event: %m");
3171fc74d21Splunky 		exit(EXIT_FAILURE);
3181fc74d21Splunky 	}
3191fc74d21Splunky }
320