xref: /netbsd-src/usr.sbin/mopd/common/loop-bsd.c (revision 14fa6887c593aea58abfdfa86a3a0f6c866991cd)
1*14fa6887Sjoerg /*	$NetBSD: loop-bsd.c,v 1.13 2020/04/22 23:55:29 joerg Exp $	*/
2fcab4c33Sthorpej 
3ed137f7cScjs /*
4ed137f7cScjs  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
5ed137f7cScjs  *
6ed137f7cScjs  * Redistribution and use in source and binary forms, with or without
7ed137f7cScjs  * modification, are permitted provided that the following conditions
8ed137f7cScjs  * are met:
9ed137f7cScjs  * 1. Redistributions of source code must retain the above copyright
10ed137f7cScjs  *    notice, this list of conditions and the following disclaimer.
11ed137f7cScjs  * 2. Redistributions in binary form must reproduce the above copyright
12ed137f7cScjs  *    notice, this list of conditions and the following disclaimer in the
13ed137f7cScjs  *    documentation and/or other materials provided with the distribution.
14ed137f7cScjs  *
15ed137f7cScjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16ed137f7cScjs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17ed137f7cScjs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18ed137f7cScjs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19ed137f7cScjs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20ed137f7cScjs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21ed137f7cScjs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22ed137f7cScjs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23ed137f7cScjs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24ed137f7cScjs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ed137f7cScjs  */
26ed137f7cScjs 
27cdd21bd3Schristos #include "port.h"
28b3f8f590Slukem #ifndef lint
29*14fa6887Sjoerg __RCSID("$NetBSD: loop-bsd.c,v 1.13 2020/04/22 23:55:29 joerg Exp $");
30ed137f7cScjs #endif
31ed137f7cScjs 
323ed4fcf6Skleink #include <errno.h>
33ed137f7cScjs #include <stdlib.h>
34ed137f7cScjs #include <strings.h>
35ed137f7cScjs #include <unistd.h>
366cbd3cb0Sperry #if defined(__bsdi__) || defined(__FreeBSD__) || defined(__NetBSD__)
37ed137f7cScjs #include <sys/time.h>
38ed137f7cScjs #endif
39ed137f7cScjs #include <net/bpf.h>
40ed137f7cScjs #include <sys/ioctl.h>
41ae69ed2bSmycroft #include <sys/poll.h>
42ae69ed2bSmycroft #include <assert.h>
43ed137f7cScjs 
44ed137f7cScjs #include "os.h"
45b3f8f590Slukem #include "common.h"
46b3f8f590Slukem #include "device.h"
47b3f8f590Slukem #include "mopdef.h"
4810dd0ebeSchristos #include "log.h"
49ed137f7cScjs 
50ed137f7cScjs int
mopOpenRC(struct if_info * p,int trans)51732d96f1Sjoerg mopOpenRC(struct if_info *p, int trans)
52ed137f7cScjs {
53ed137f7cScjs #ifndef NORC
54ed137f7cScjs 	return (*(p->iopen))(p->if_name,
55ed137f7cScjs 			     O_RDWR,
56ed137f7cScjs 			     MOP_K_PROTO_RC,
57ed137f7cScjs 			     trans);
58ed137f7cScjs #else
59ed137f7cScjs 	return -1;
60ed137f7cScjs #endif
61ed137f7cScjs }
62ed137f7cScjs 
63ed137f7cScjs int
mopOpenDL(struct if_info * p,int trans)64732d96f1Sjoerg mopOpenDL(struct if_info *p, int trans)
65ed137f7cScjs {
66ed137f7cScjs #ifndef NODL
67ed137f7cScjs 	return (*(p->iopen))(p->if_name,
68ed137f7cScjs 			     O_RDWR,
69ed137f7cScjs 			     MOP_K_PROTO_DL,
70ed137f7cScjs 			     trans);
71ed137f7cScjs #else
72ed137f7cScjs 	return -1;
73ed137f7cScjs #endif
74ed137f7cScjs }
75ed137f7cScjs 
76ed137f7cScjs void
mopReadRC(void)77732d96f1Sjoerg mopReadRC(void)
78ed137f7cScjs {
79ed137f7cScjs }
80ed137f7cScjs 
81ed137f7cScjs void
mopReadDL(void)82732d96f1Sjoerg mopReadDL(void)
83ed137f7cScjs {
84ed137f7cScjs }
85ed137f7cScjs 
86ed137f7cScjs /*
87ed137f7cScjs  * The list of all interfaces that are being listened to.  loop()
881232ea27Selad  * "polls" on the descriptors in this list.
89ed137f7cScjs  */
90*14fa6887Sjoerg extern struct if_info *iflist;
91ed137f7cScjs 
92732d96f1Sjoerg void   mopProcess(struct if_info *, u_char *);
93ed137f7cScjs 
94ed137f7cScjs /*
95ed137f7cScjs  * Loop indefinitely listening for MOP requests on the
96ed137f7cScjs  * interfaces in 'iflist'.
97ed137f7cScjs  */
98ed137f7cScjs void
Loop(void)99732d96f1Sjoerg Loop(void)
100ed137f7cScjs {
101ed137f7cScjs 	u_char *buf, *bp, *ep;
102ae69ed2bSmycroft 	int     cc, n, m;
103ae69ed2bSmycroft 	struct	pollfd *set;
104ae69ed2bSmycroft 	int     bufsize;
105ed137f7cScjs 	struct	if_info *ii;
106ed137f7cScjs 
10710dd0ebeSchristos 	if (iflist == 0)
10810dd0ebeSchristos 		mopLogErrX("no interfaces");
109ed137f7cScjs 	if (iflist->fd != -1) {
11010dd0ebeSchristos 		if (ioctl(iflist->fd, BIOCGBLEN, (caddr_t) & bufsize) < 0)
11110dd0ebeSchristos 			mopLogErr("BIOCGBLEN");
112bd007562Schristos 	} else
113bd007562Schristos 		mopLogErrX("cannot get buffer size");
114ed137f7cScjs 	buf = (u_char *) malloc((unsigned) bufsize);
11510dd0ebeSchristos 	if (buf == 0)
11610dd0ebeSchristos 		mopLogErr("malloc");
117ed137f7cScjs 	/*
1181232ea27Selad          * Find the highest numbered file descriptor for poll().
119ed137f7cScjs          * Initialize the set of descriptors to listen to.
120ed137f7cScjs          */
121ae69ed2bSmycroft 	for (ii = iflist, n = 0; ii; ii = ii->next, n++)
122ae69ed2bSmycroft 		;
123ae69ed2bSmycroft 	set = malloc(n * sizeof(*set));
124ae69ed2bSmycroft 	for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
125ae69ed2bSmycroft 		assert(ii->fd != -1);
126ae69ed2bSmycroft 		set[m].fd = ii->fd;
127ae69ed2bSmycroft 		set[m].events = POLLIN;
128ed137f7cScjs 	}
129ae69ed2bSmycroft 	for (;;) {
13010dd0ebeSchristos 		if (poll(set, n, INFTIM) < 0)
13110dd0ebeSchristos 			mopLogErr("poll");
132ae69ed2bSmycroft 		for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
133ae69ed2bSmycroft 			if (!(set[m].revents & POLLIN))
134ed137f7cScjs 				continue;
135ed137f7cScjs 	again:
136ed137f7cScjs 			cc = read(ii->fd, (char *) buf, bufsize);
137ed137f7cScjs 			/* Don't choke when we get ptraced */
138ed137f7cScjs 			if (cc < 0 && errno == EINTR)
139ed137f7cScjs 				goto again;
140ed137f7cScjs 			/* Due to a SunOS bug, after 2^31 bytes, the file
141ed137f7cScjs 			 * offset overflows and read fails with EINVAL.  The
142ed137f7cScjs 			 * lseek() to 0 will fix things. */
143ed137f7cScjs 			if (cc < 0) {
144ed137f7cScjs 				if (errno == EINVAL &&
145ed137f7cScjs 				    (lseek(ii->fd, 0, SEEK_CUR) + bufsize) < 0) {
146ed137f7cScjs 					(void) lseek(ii->fd, 0, 0);
147ed137f7cScjs 					goto again;
148ed137f7cScjs 				}
14910dd0ebeSchristos 				mopLogErr("read");
150ed137f7cScjs 			}
151ed137f7cScjs 			/* Loop through the packet(s) */
152ed137f7cScjs #define bhp ((struct bpf_hdr *)bp)
153ed137f7cScjs 			bp = buf;
154ed137f7cScjs 			ep = bp + cc;
155ed137f7cScjs 			while (bp < ep) {
156b3f8f590Slukem 				int caplen, hdrlen;
157ed137f7cScjs 
158ed137f7cScjs 				caplen = bhp->bh_caplen;
159ed137f7cScjs 				hdrlen = bhp->bh_hdrlen;
160ed137f7cScjs 				mopProcess(ii, bp + hdrlen);
161ed137f7cScjs 				bp += BPF_WORDALIGN(hdrlen + caplen);
162ed137f7cScjs 			}
163ed137f7cScjs 		}
164ed137f7cScjs 	}
165ed137f7cScjs }
166