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