if_arcsubr.c revision 1.2 1 1.2 mycroft /* $NetBSD: if_arcsubr.c,v 1.2 1995/04/11 04:32:09 mycroft Exp $ */
2 1.1 glass
3 1.1 glass /*
4 1.1 glass * Copyright (c) 1994, 1995 Ignatios Souvatzis
5 1.1 glass * Copyright (c) 1982, 1989, 1993
6 1.1 glass * The Regents of the University of California. All rights reserved.
7 1.1 glass *
8 1.1 glass * Redistribution and use in source and binary forms, with or without
9 1.1 glass * modification, are permitted provided that the following conditions
10 1.1 glass * are met:
11 1.1 glass * 1. Redistributions of source code must retain the above copyright
12 1.1 glass * notice, this list of conditions and the following disclaimer.
13 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 glass * notice, this list of conditions and the following disclaimer in the
15 1.1 glass * documentation and/or other materials provided with the distribution.
16 1.1 glass * 3. All advertising materials mentioning features or use of this software
17 1.1 glass * must display the following acknowledgement:
18 1.1 glass * This product includes software developed by the University of
19 1.1 glass * California, Berkeley and its contributors.
20 1.1 glass * 4. Neither the name of the University nor the names of its contributors
21 1.1 glass * may be used to endorse or promote products derived from this software
22 1.1 glass * without specific prior written permission.
23 1.1 glass *
24 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 glass * SUCH DAMAGE.
35 1.1 glass *
36 1.1 glass * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
37 1.1 glass * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
38 1.1 glass *
39 1.1 glass */
40 1.1 glass
41 1.1 glass #include <sys/param.h>
42 1.1 glass #include <sys/systm.h>
43 1.1 glass #include <sys/kernel.h>
44 1.1 glass #include <sys/malloc.h>
45 1.1 glass #include <sys/mbuf.h>
46 1.1 glass #include <sys/socket.h>
47 1.1 glass #include <sys/errno.h>
48 1.1 glass
49 1.1 glass #include <machine/cpu.h>
50 1.1 glass
51 1.1 glass #include <net/if.h>
52 1.1 glass #include <net/netisr.h>
53 1.1 glass #include <net/route.h>
54 1.1 glass #include <net/if_dl.h>
55 1.1 glass #include <net/if_types.h>
56 1.1 glass
57 1.1 glass #ifdef INET
58 1.1 glass #include <netinet/in.h>
59 1.1 glass #include <netinet/in_var.h>
60 1.1 glass #endif
61 1.1 glass #include <netinet/if_arc.h>
62 1.1 glass
63 1.1 glass u_char arcbroadcastaddr = 0;
64 1.1 glass
65 1.1 glass #define senderr(e) { error = (e); goto bad;}
66 1.1 glass
67 1.1 glass #define SIN(s) ((struct sockaddr_in *)s)
68 1.1 glass
69 1.1 glass /*
70 1.1 glass * ARCnet output routine.
71 1.1 glass * Encapsulate a packet of type family for the local net.
72 1.1 glass * Assumes that ifp is actually pointer to arccom structure.
73 1.1 glass */
74 1.1 glass int
75 1.1 glass arc_output(ifp, m0, dst, rt0)
76 1.1 glass register struct ifnet *ifp;
77 1.1 glass struct mbuf *m0;
78 1.1 glass struct sockaddr *dst;
79 1.1 glass struct rtentry *rt0;
80 1.1 glass {
81 1.1 glass int s, error = 0;
82 1.1 glass u_char atype, adst;
83 1.1 glass register struct mbuf *m = m0;
84 1.1 glass register struct rtentry *rt;
85 1.1 glass struct mbuf *mcopy = (struct mbuf *)0;
86 1.1 glass register struct arc_header *ah;
87 1.1 glass int off, len = m->m_pkthdr.len;
88 1.1 glass struct arccom *ac = (struct arccom *)ifp;
89 1.1 glass
90 1.1 glass if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
91 1.1 glass senderr(ENETDOWN);
92 1.1 glass ifp->if_lastchange = time;
93 1.1 glass if (rt = rt0) {
94 1.1 glass if ((rt->rt_flags & RTF_UP) == 0) {
95 1.1 glass if (rt0 = rt = rtalloc1(dst, 1))
96 1.1 glass rt->rt_refcnt--;
97 1.1 glass else
98 1.1 glass senderr(EHOSTUNREACH);
99 1.1 glass }
100 1.1 glass if (rt->rt_flags & RTF_GATEWAY) {
101 1.1 glass if (rt->rt_gwroute == 0)
102 1.1 glass goto lookup;
103 1.1 glass if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
104 1.1 glass rtfree(rt); rt = rt0;
105 1.1 glass lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
106 1.1 glass if ((rt = rt->rt_gwroute) == 0)
107 1.1 glass senderr(EHOSTUNREACH);
108 1.1 glass }
109 1.1 glass }
110 1.1 glass if (rt->rt_flags & RTF_REJECT)
111 1.1 glass if (rt->rt_rmx.rmx_expire == 0 ||
112 1.1 glass time.tv_sec < rt->rt_rmx.rmx_expire)
113 1.1 glass senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
114 1.1 glass }
115 1.1 glass switch (dst->sa_family) {
116 1.1 glass
117 1.1 glass #ifdef INET
118 1.1 glass case AF_INET:
119 1.1 glass
120 1.1 glass /*
121 1.1 glass * For now, use the simple IP addr -> ARCnet addr mapping
122 1.1 glass */
123 1.1 glass if (m->m_flags & M_BCAST)
124 1.1 glass adst = arcbroadcastaddr; /* ARCnet broadcast address */
125 1.1 glass else
126 1.1 glass adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
127 1.1 glass
128 1.1 glass /* If broadcasting on a simplex interface, loopback a copy */
129 1.1 glass if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
130 1.1 glass mcopy = m_copy(m, 0, (int)M_COPYALL);
131 1.1 glass off = m->m_pkthdr.len - m->m_len;
132 1.1 glass atype = ARCTYPE_IP_OLD;
133 1.1 glass break;
134 1.1 glass #endif
135 1.1 glass case AF_UNSPEC:
136 1.1 glass ah = (struct arc_header *)dst->sa_data;
137 1.1 glass adst = ah->arc_dhost;
138 1.1 glass atype = ah->arc_type;
139 1.1 glass break;
140 1.1 glass
141 1.1 glass default:
142 1.1 glass printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
143 1.1 glass dst->sa_family);
144 1.1 glass senderr(EAFNOSUPPORT);
145 1.1 glass }
146 1.1 glass
147 1.1 glass
148 1.1 glass if (mcopy)
149 1.1 glass (void) looutput(ifp, mcopy, dst, rt);
150 1.1 glass /*
151 1.1 glass * Add local net header. If no space in first mbuf,
152 1.1 glass * allocate another.
153 1.1 glass *
154 1.1 glass * For ARCnet, this is just symbolic. The header changes
155 1.1 glass * form and position on its way into the hardware and out of
156 1.1 glass * the wire. At this point, it contains source, destination and
157 1.1 glass * packet type.
158 1.1 glass */
159 1.1 glass M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
160 1.1 glass if (m == 0)
161 1.1 glass senderr(ENOBUFS);
162 1.1 glass ah = mtod(m, struct arc_header *);
163 1.1 glass ah->arc_type = atype;
164 1.1 glass ah->arc_dhost= adst;
165 1.1 glass ah->arc_shost= ac->ac_anaddr;
166 1.1 glass s = splimp();
167 1.1 glass /*
168 1.1 glass * Queue message on interface, and start output if interface
169 1.1 glass * not yet active.
170 1.1 glass */
171 1.1 glass if (IF_QFULL(&ifp->if_snd)) {
172 1.1 glass IF_DROP(&ifp->if_snd);
173 1.1 glass splx(s);
174 1.1 glass senderr(ENOBUFS);
175 1.1 glass }
176 1.1 glass IF_ENQUEUE(&ifp->if_snd, m);
177 1.1 glass if ((ifp->if_flags & IFF_OACTIVE) == 0)
178 1.1 glass (*ifp->if_start)(ifp);
179 1.1 glass splx(s);
180 1.1 glass
181 1.1 glass ifp->if_obytes += len + ARC_HDRLEN;
182 1.1 glass return (error);
183 1.1 glass
184 1.1 glass bad:
185 1.1 glass if (m)
186 1.1 glass m_freem(m);
187 1.1 glass return (error);
188 1.1 glass }
189 1.1 glass
190 1.1 glass /*
191 1.1 glass * Process a received Arcnet packet;
192 1.1 glass * the packet is in the mbuf chain m without
193 1.1 glass * the ARCnet header, which is provided separately.
194 1.1 glass */
195 1.1 glass void
196 1.1 glass arc_input(ifp, ah, m)
197 1.1 glass struct ifnet *ifp;
198 1.1 glass register struct arc_header *ah;
199 1.1 glass struct mbuf *m;
200 1.1 glass {
201 1.1 glass register struct ifqueue *inq;
202 1.1 glass u_char atype;
203 1.1 glass int s;
204 1.1 glass
205 1.1 glass if ((ifp->if_flags & IFF_UP) == 0) {
206 1.1 glass m_freem(m);
207 1.1 glass return;
208 1.1 glass }
209 1.1 glass ifp->if_lastchange = time;
210 1.1 glass ifp->if_ibytes += m->m_pkthdr.len + sizeof (*ah);
211 1.1 glass
212 1.1 glass if (arcbroadcastaddr == ah->arc_dhost) {
213 1.1 glass m->m_flags |= M_BCAST;
214 1.1 glass ifp->if_imcasts++;
215 1.1 glass }
216 1.1 glass
217 1.1 glass atype = ah->arc_type;
218 1.1 glass switch (atype) {
219 1.1 glass #ifdef INET
220 1.1 glass case ARCTYPE_IP_OLD:
221 1.1 glass schednetisr(NETISR_IP);
222 1.1 glass inq = &ipintrq;
223 1.1 glass break;
224 1.1 glass #endif
225 1.1 glass default:
226 1.1 glass m_freem(m);
227 1.1 glass return;
228 1.1 glass }
229 1.1 glass
230 1.1 glass s = splimp();
231 1.1 glass if (IF_QFULL(inq)) {
232 1.1 glass IF_DROP(inq);
233 1.1 glass m_freem(m);
234 1.1 glass } else
235 1.1 glass IF_ENQUEUE(inq, m);
236 1.1 glass splx(s);
237 1.1 glass }
238 1.1 glass
239 1.1 glass /*
240 1.1 glass * Convert Arcnet address to printable (loggable) representation.
241 1.1 glass */
242 1.1 glass static char digits[] = "0123456789abcdef";
243 1.1 glass char *
244 1.1 glass arc_sprintf(ap)
245 1.1 glass register u_char *ap;
246 1.1 glass {
247 1.1 glass static char arcbuf[3];
248 1.1 glass register char *cp = arcbuf;
249 1.1 glass
250 1.1 glass *cp++ = digits[*ap >> 4];
251 1.1 glass *cp++ = digits[*ap++ & 0xf];
252 1.1 glass *cp = 0;
253 1.1 glass return (arcbuf);
254 1.1 glass }
255 1.1 glass
256 1.1 glass /*
257 1.1 glass * Perform common duties while attaching to interface list
258 1.1 glass */
259 1.1 glass void
260 1.1 glass arc_ifattach(ifp)
261 1.1 glass register struct ifnet *ifp;
262 1.1 glass {
263 1.1 glass register struct ifaddr *ifa;
264 1.1 glass register struct sockaddr_dl *sdl;
265 1.1 glass
266 1.1 glass ifp->if_type = IFT_ARCNET;
267 1.1 glass ifp->if_addrlen = 1;
268 1.1 glass ifp->if_hdrlen = ARC_HDRLEN;
269 1.1 glass ifp->if_mtu = ARCMTU;
270 1.1 glass for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
271 1.1 glass if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
272 1.1 glass sdl->sdl_family == AF_LINK) {
273 1.1 glass sdl->sdl_type = IFT_ARCNET;
274 1.1 glass sdl->sdl_alen = ifp->if_addrlen;
275 1.1 glass bcopy((caddr_t)&((struct arccom *)ifp)->ac_anaddr,
276 1.1 glass LLADDR(sdl), ifp->if_addrlen);
277 1.1 glass break;
278 1.1 glass }
279 1.1 glass }
280