if_ethersubr.c revision 1.1.1.3 1 1.1 cgd /*
2 1.1.1.2 fvdl * Copyright (c) 1982, 1989, 1993
3 1.1.1.2 fvdl * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.1.1.3 fvdl * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
34 1.1 cgd */
35 1.1 cgd
36 1.1.1.2 fvdl #include <sys/param.h>
37 1.1.1.2 fvdl #include <sys/systm.h>
38 1.1.1.2 fvdl #include <sys/kernel.h>
39 1.1.1.2 fvdl #include <sys/malloc.h>
40 1.1.1.2 fvdl #include <sys/mbuf.h>
41 1.1.1.2 fvdl #include <sys/protosw.h>
42 1.1.1.2 fvdl #include <sys/socket.h>
43 1.1.1.2 fvdl #include <sys/ioctl.h>
44 1.1.1.2 fvdl #include <sys/errno.h>
45 1.1.1.2 fvdl #include <sys/syslog.h>
46 1.1.1.2 fvdl
47 1.1.1.2 fvdl #include <machine/cpu.h>
48 1.1.1.2 fvdl
49 1.1.1.2 fvdl #include <net/if.h>
50 1.1.1.2 fvdl #include <net/netisr.h>
51 1.1.1.2 fvdl #include <net/route.h>
52 1.1.1.2 fvdl #include <net/if_llc.h>
53 1.1.1.2 fvdl #include <net/if_dl.h>
54 1.1.1.2 fvdl #include <net/if_types.h>
55 1.1 cgd
56 1.1 cgd #ifdef INET
57 1.1.1.2 fvdl #include <netinet/in.h>
58 1.1.1.2 fvdl #include <netinet/in_var.h>
59 1.1 cgd #endif
60 1.1.1.2 fvdl #include <netinet/if_ether.h>
61 1.1 cgd
62 1.1 cgd #ifdef NS
63 1.1.1.2 fvdl #include <netns/ns.h>
64 1.1.1.2 fvdl #include <netns/ns_if.h>
65 1.1 cgd #endif
66 1.1 cgd
67 1.1 cgd #ifdef ISO
68 1.1.1.2 fvdl #include <netiso/argo_debug.h>
69 1.1.1.2 fvdl #include <netiso/iso.h>
70 1.1.1.2 fvdl #include <netiso/iso_var.h>
71 1.1.1.2 fvdl #include <netiso/iso_snpac.h>
72 1.1.1.2 fvdl #endif
73 1.1.1.2 fvdl
74 1.1.1.2 fvdl #ifdef LLC
75 1.1.1.2 fvdl #include <netccitt/dll.h>
76 1.1.1.2 fvdl #include <netccitt/llc_var.h>
77 1.1.1.2 fvdl #endif
78 1.1.1.2 fvdl
79 1.1.1.2 fvdl #if defined(LLC) && defined(CCITT)
80 1.1.1.2 fvdl extern struct ifqueue pkintrq;
81 1.1 cgd #endif
82 1.1 cgd
83 1.1 cgd u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
84 1.1 cgd extern struct ifnet loif;
85 1.1.1.2 fvdl #define senderr(e) { error = (e); goto bad;}
86 1.1 cgd
87 1.1 cgd /*
88 1.1 cgd * Ethernet output routine.
89 1.1 cgd * Encapsulate a packet of type family for the local net.
90 1.1 cgd * Use trailer local net encapsulation if enough data in first
91 1.1 cgd * packet leaves a multiple of 512 bytes of data in remainder.
92 1.1 cgd * Assumes that ifp is actually pointer to arpcom structure.
93 1.1 cgd */
94 1.1.1.2 fvdl int
95 1.1.1.2 fvdl ether_output(ifp, m0, dst, rt0)
96 1.1 cgd register struct ifnet *ifp;
97 1.1 cgd struct mbuf *m0;
98 1.1 cgd struct sockaddr *dst;
99 1.1.1.2 fvdl struct rtentry *rt0;
100 1.1 cgd {
101 1.1 cgd short type;
102 1.1 cgd int s, error = 0;
103 1.1 cgd u_char edst[6];
104 1.1 cgd register struct mbuf *m = m0;
105 1.1.1.2 fvdl register struct rtentry *rt;
106 1.1 cgd struct mbuf *mcopy = (struct mbuf *)0;
107 1.1 cgd register struct ether_header *eh;
108 1.1.1.2 fvdl int off, len = m->m_pkthdr.len;
109 1.1.1.2 fvdl struct arpcom *ac = (struct arpcom *)ifp;
110 1.1 cgd
111 1.1.1.2 fvdl if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
112 1.1.1.2 fvdl senderr(ENETDOWN);
113 1.1 cgd ifp->if_lastchange = time;
114 1.1.1.2 fvdl if (rt = rt0) {
115 1.1.1.2 fvdl if ((rt->rt_flags & RTF_UP) == 0) {
116 1.1.1.3 fvdl if (rt0 = rt = rtalloc1(dst, 1)) {
117 1.1.1.2 fvdl rt->rt_refcnt--;
118 1.1.1.3 fvdl if (rt->rt_ifp != ifp)
119 1.1.1.3 fvdl return (*rt->rt_ifp->if_output)
120 1.1.1.3 fvdl (ifp, m0, dst, rt);
121 1.1.1.3 fvdl } else
122 1.1.1.2 fvdl senderr(EHOSTUNREACH);
123 1.1.1.2 fvdl }
124 1.1.1.3 fvdl if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
125 1.1.1.2 fvdl if (rt->rt_gwroute == 0)
126 1.1.1.2 fvdl goto lookup;
127 1.1.1.2 fvdl if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
128 1.1.1.2 fvdl rtfree(rt); rt = rt0;
129 1.1.1.2 fvdl lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
130 1.1.1.2 fvdl if ((rt = rt->rt_gwroute) == 0)
131 1.1.1.2 fvdl senderr(EHOSTUNREACH);
132 1.1.1.3 fvdl /* the "G" test below also prevents rt == rt0 */
133 1.1.1.3 fvdl if ((rt->rt_flags & RTF_GATEWAY) ||
134 1.1.1.3 fvdl (rt->rt_ifp != ifp)) {
135 1.1.1.3 fvdl rt->rt_refcnt--;
136 1.1.1.3 fvdl rt0->rt_gwroute = 0;
137 1.1.1.3 fvdl senderr(EHOSTUNREACH);
138 1.1.1.3 fvdl }
139 1.1.1.2 fvdl }
140 1.1.1.2 fvdl }
141 1.1.1.2 fvdl if (rt->rt_flags & RTF_REJECT)
142 1.1.1.2 fvdl if (rt->rt_rmx.rmx_expire == 0 ||
143 1.1.1.2 fvdl time.tv_sec < rt->rt_rmx.rmx_expire)
144 1.1.1.2 fvdl senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
145 1.1.1.2 fvdl }
146 1.1 cgd switch (dst->sa_family) {
147 1.1 cgd
148 1.1 cgd #ifdef INET
149 1.1 cgd case AF_INET:
150 1.1.1.2 fvdl if (!arpresolve(ac, rt, m, dst, edst))
151 1.1 cgd return (0); /* if not yet resolved */
152 1.1.1.2 fvdl /* If broadcasting on a simplex interface, loopback a copy */
153 1.1.1.2 fvdl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
154 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
155 1.1 cgd off = m->m_pkthdr.len - m->m_len;
156 1.1 cgd type = ETHERTYPE_IP;
157 1.1.1.2 fvdl break;
158 1.1 cgd #endif
159 1.1 cgd #ifdef NS
160 1.1 cgd case AF_NS:
161 1.1 cgd type = ETHERTYPE_NS;
162 1.1 cgd bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
163 1.1 cgd (caddr_t)edst, sizeof (edst));
164 1.1 cgd if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
165 1.1 cgd return (looutput(ifp, m, dst, rt));
166 1.1.1.2 fvdl /* If broadcasting on a simplex interface, loopback a copy */
167 1.1.1.2 fvdl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
168 1.1 cgd mcopy = m_copy(m, 0, (int)M_COPYALL);
169 1.1.1.2 fvdl break;
170 1.1 cgd #endif
171 1.1 cgd #ifdef ISO
172 1.1 cgd case AF_ISO: {
173 1.1 cgd int snpalen;
174 1.1 cgd struct llc *l;
175 1.1.1.2 fvdl register struct sockaddr_dl *sdl;
176 1.1 cgd
177 1.1.1.2 fvdl if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
178 1.1.1.2 fvdl sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
179 1.1.1.2 fvdl bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
180 1.1.1.2 fvdl } else if (error =
181 1.1.1.2 fvdl iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
182 1.1.1.2 fvdl (char *)edst, &snpalen))
183 1.1 cgd goto bad; /* Not Resolved */
184 1.1.1.2 fvdl /* If broadcasting on a simplex interface, loopback a copy */
185 1.1.1.2 fvdl if (*edst & 1)
186 1.1.1.2 fvdl m->m_flags |= (M_BCAST|M_MCAST);
187 1.1.1.2 fvdl if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
188 1.1 cgd (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
189 1.1 cgd M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
190 1.1 cgd if (mcopy) {
191 1.1 cgd eh = mtod(mcopy, struct ether_header *);
192 1.1 cgd bcopy((caddr_t)edst,
193 1.1 cgd (caddr_t)eh->ether_dhost, sizeof (edst));
194 1.1 cgd bcopy((caddr_t)ac->ac_enaddr,
195 1.1 cgd (caddr_t)eh->ether_shost, sizeof (edst));
196 1.1 cgd }
197 1.1 cgd }
198 1.1 cgd M_PREPEND(m, 3, M_DONTWAIT);
199 1.1 cgd if (m == NULL)
200 1.1 cgd return (0);
201 1.1 cgd type = m->m_pkthdr.len;
202 1.1 cgd l = mtod(m, struct llc *);
203 1.1 cgd l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
204 1.1 cgd l->llc_control = LLC_UI;
205 1.1 cgd len += 3;
206 1.1 cgd IFDEBUG(D_ETHER)
207 1.1 cgd int i;
208 1.1 cgd printf("unoutput: sending pkt to: ");
209 1.1 cgd for (i=0; i<6; i++)
210 1.1 cgd printf("%x ", edst[i] & 0xff);
211 1.1 cgd printf("\n");
212 1.1 cgd ENDDEBUG
213 1.1.1.2 fvdl } break;
214 1.1.1.2 fvdl #endif /* ISO */
215 1.1.1.2 fvdl #ifdef LLC
216 1.1.1.2 fvdl /* case AF_NSAP: */
217 1.1.1.2 fvdl case AF_CCITT: {
218 1.1.1.2 fvdl register struct sockaddr_dl *sdl =
219 1.1.1.2 fvdl (struct sockaddr_dl *) rt -> rt_gateway;
220 1.1.1.2 fvdl
221 1.1.1.2 fvdl if (sdl && sdl->sdl_family == AF_LINK
222 1.1.1.2 fvdl && sdl->sdl_alen > 0) {
223 1.1.1.2 fvdl bcopy(LLADDR(sdl), (char *)edst,
224 1.1.1.2 fvdl sizeof(edst));
225 1.1.1.2 fvdl } else goto bad; /* Not a link interface ? Funny ... */
226 1.1.1.2 fvdl if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
227 1.1.1.2 fvdl (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
228 1.1.1.2 fvdl M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
229 1.1.1.2 fvdl if (mcopy) {
230 1.1.1.2 fvdl eh = mtod(mcopy, struct ether_header *);
231 1.1.1.2 fvdl bcopy((caddr_t)edst,
232 1.1.1.2 fvdl (caddr_t)eh->ether_dhost, sizeof (edst));
233 1.1.1.2 fvdl bcopy((caddr_t)ac->ac_enaddr,
234 1.1.1.2 fvdl (caddr_t)eh->ether_shost, sizeof (edst));
235 1.1.1.2 fvdl }
236 1.1.1.2 fvdl }
237 1.1.1.2 fvdl type = m->m_pkthdr.len;
238 1.1.1.2 fvdl #ifdef LLC_DEBUG
239 1.1.1.2 fvdl {
240 1.1.1.2 fvdl int i;
241 1.1.1.2 fvdl register struct llc *l = mtod(m, struct llc *);
242 1.1.1.2 fvdl
243 1.1.1.2 fvdl printf("ether_output: sending LLC2 pkt to: ");
244 1.1.1.2 fvdl for (i=0; i<6; i++)
245 1.1.1.2 fvdl printf("%x ", edst[i] & 0xff);
246 1.1.1.2 fvdl printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
247 1.1.1.2 fvdl type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
248 1.1.1.2 fvdl l->llc_control & 0xff);
249 1.1.1.2 fvdl
250 1.1.1.2 fvdl }
251 1.1.1.2 fvdl #endif /* LLC_DEBUG */
252 1.1.1.2 fvdl } break;
253 1.1.1.2 fvdl #endif /* LLC */
254 1.1 cgd
255 1.1 cgd case AF_UNSPEC:
256 1.1 cgd eh = (struct ether_header *)dst->sa_data;
257 1.1 cgd bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
258 1.1 cgd type = eh->ether_type;
259 1.1.1.2 fvdl break;
260 1.1 cgd
261 1.1 cgd default:
262 1.1 cgd printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
263 1.1 cgd dst->sa_family);
264 1.1.1.2 fvdl senderr(EAFNOSUPPORT);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd
268 1.1 cgd if (mcopy)
269 1.1 cgd (void) looutput(ifp, mcopy, dst, rt);
270 1.1 cgd /*
271 1.1 cgd * Add local net header. If no space in first mbuf,
272 1.1 cgd * allocate another.
273 1.1 cgd */
274 1.1 cgd M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
275 1.1.1.2 fvdl if (m == 0)
276 1.1.1.2 fvdl senderr(ENOBUFS);
277 1.1 cgd eh = mtod(m, struct ether_header *);
278 1.1 cgd type = htons((u_short)type);
279 1.1 cgd bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
280 1.1 cgd sizeof(eh->ether_type));
281 1.1 cgd bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
282 1.1 cgd bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
283 1.1 cgd sizeof(eh->ether_shost));
284 1.1 cgd s = splimp();
285 1.1 cgd /*
286 1.1 cgd * Queue message on interface, and start output if interface
287 1.1 cgd * not yet active.
288 1.1 cgd */
289 1.1 cgd if (IF_QFULL(&ifp->if_snd)) {
290 1.1 cgd IF_DROP(&ifp->if_snd);
291 1.1 cgd splx(s);
292 1.1.1.2 fvdl senderr(ENOBUFS);
293 1.1 cgd }
294 1.1 cgd IF_ENQUEUE(&ifp->if_snd, m);
295 1.1 cgd if ((ifp->if_flags & IFF_OACTIVE) == 0)
296 1.1 cgd (*ifp->if_start)(ifp);
297 1.1 cgd splx(s);
298 1.1 cgd ifp->if_obytes += len + sizeof (struct ether_header);
299 1.1.1.2 fvdl if (m->m_flags & M_MCAST)
300 1.1 cgd ifp->if_omcasts++;
301 1.1 cgd return (error);
302 1.1 cgd
303 1.1 cgd bad:
304 1.1 cgd if (m)
305 1.1 cgd m_freem(m);
306 1.1 cgd return (error);
307 1.1 cgd }
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * Process a received Ethernet packet;
311 1.1 cgd * the packet is in the mbuf chain m without
312 1.1 cgd * the ether header, which is provided separately.
313 1.1 cgd */
314 1.1.1.2 fvdl void
315 1.1 cgd ether_input(ifp, eh, m)
316 1.1 cgd struct ifnet *ifp;
317 1.1 cgd register struct ether_header *eh;
318 1.1 cgd struct mbuf *m;
319 1.1 cgd {
320 1.1 cgd register struct ifqueue *inq;
321 1.1 cgd register struct llc *l;
322 1.1.1.2 fvdl struct arpcom *ac = (struct arpcom *)ifp;
323 1.1 cgd int s;
324 1.1 cgd
325 1.1.1.2 fvdl if ((ifp->if_flags & IFF_UP) == 0) {
326 1.1.1.2 fvdl m_freem(m);
327 1.1.1.2 fvdl return;
328 1.1.1.2 fvdl }
329 1.1 cgd ifp->if_lastchange = time;
330 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
331 1.1 cgd if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
332 1.1 cgd sizeof(etherbroadcastaddr)) == 0)
333 1.1 cgd m->m_flags |= M_BCAST;
334 1.1 cgd else if (eh->ether_dhost[0] & 1)
335 1.1 cgd m->m_flags |= M_MCAST;
336 1.1 cgd if (m->m_flags & (M_BCAST|M_MCAST))
337 1.1 cgd ifp->if_imcasts++;
338 1.1 cgd
339 1.1 cgd switch (eh->ether_type) {
340 1.1 cgd #ifdef INET
341 1.1 cgd case ETHERTYPE_IP:
342 1.1 cgd schednetisr(NETISR_IP);
343 1.1 cgd inq = &ipintrq;
344 1.1 cgd break;
345 1.1 cgd
346 1.1 cgd case ETHERTYPE_ARP:
347 1.1.1.2 fvdl schednetisr(NETISR_ARP);
348 1.1.1.2 fvdl inq = &arpintrq;
349 1.1.1.2 fvdl break;
350 1.1 cgd #endif
351 1.1 cgd #ifdef NS
352 1.1 cgd case ETHERTYPE_NS:
353 1.1 cgd schednetisr(NETISR_NS);
354 1.1 cgd inq = &nsintrq;
355 1.1 cgd break;
356 1.1 cgd
357 1.1 cgd #endif
358 1.1 cgd default:
359 1.1.1.2 fvdl #if defined (ISO) || defined (LLC)
360 1.1 cgd if (eh->ether_type > ETHERMTU)
361 1.1 cgd goto dropanyway;
362 1.1 cgd l = mtod(m, struct llc *);
363 1.1.1.2 fvdl switch (l->llc_dsap) {
364 1.1.1.2 fvdl #ifdef ISO
365 1.1.1.2 fvdl case LLC_ISO_LSAP:
366 1.1.1.2 fvdl switch (l->llc_control) {
367 1.1.1.2 fvdl case LLC_UI:
368 1.1.1.2 fvdl /* LLC_UI_P forbidden in class 1 service */
369 1.1.1.2 fvdl if ((l->llc_dsap == LLC_ISO_LSAP) &&
370 1.1.1.2 fvdl (l->llc_ssap == LLC_ISO_LSAP)) {
371 1.1.1.2 fvdl /* LSAP for ISO */
372 1.1.1.2 fvdl if (m->m_pkthdr.len > eh->ether_type)
373 1.1.1.2 fvdl m_adj(m, eh->ether_type - m->m_pkthdr.len);
374 1.1.1.2 fvdl m->m_data += 3; /* XXX */
375 1.1.1.2 fvdl m->m_len -= 3; /* XXX */
376 1.1.1.2 fvdl m->m_pkthdr.len -= 3; /* XXX */
377 1.1.1.2 fvdl M_PREPEND(m, sizeof *eh, M_DONTWAIT);
378 1.1.1.2 fvdl if (m == 0)
379 1.1.1.2 fvdl return;
380 1.1.1.2 fvdl *mtod(m, struct ether_header *) = *eh;
381 1.1.1.2 fvdl IFDEBUG(D_ETHER)
382 1.1.1.2 fvdl printf("clnp packet");
383 1.1.1.2 fvdl ENDDEBUG
384 1.1.1.2 fvdl schednetisr(NETISR_ISO);
385 1.1.1.2 fvdl inq = &clnlintrq;
386 1.1.1.2 fvdl break;
387 1.1.1.2 fvdl }
388 1.1.1.2 fvdl goto dropanyway;
389 1.1.1.2 fvdl
390 1.1.1.2 fvdl case LLC_XID:
391 1.1.1.2 fvdl case LLC_XID_P:
392 1.1.1.2 fvdl if(m->m_len < 6)
393 1.1.1.2 fvdl goto dropanyway;
394 1.1.1.2 fvdl l->llc_window = 0;
395 1.1.1.2 fvdl l->llc_fid = 9;
396 1.1.1.2 fvdl l->llc_class = 1;
397 1.1.1.2 fvdl l->llc_dsap = l->llc_ssap = 0;
398 1.1.1.2 fvdl /* Fall through to */
399 1.1.1.2 fvdl case LLC_TEST:
400 1.1.1.2 fvdl case LLC_TEST_P:
401 1.1.1.2 fvdl {
402 1.1.1.2 fvdl struct sockaddr sa;
403 1.1.1.2 fvdl register struct ether_header *eh2;
404 1.1.1.2 fvdl int i;
405 1.1.1.2 fvdl u_char c = l->llc_dsap;
406 1.1.1.2 fvdl
407 1.1.1.2 fvdl l->llc_dsap = l->llc_ssap;
408 1.1.1.2 fvdl l->llc_ssap = c;
409 1.1.1.2 fvdl if (m->m_flags & (M_BCAST | M_MCAST))
410 1.1.1.2 fvdl bcopy((caddr_t)ac->ac_enaddr,
411 1.1.1.2 fvdl (caddr_t)eh->ether_dhost, 6);
412 1.1.1.2 fvdl sa.sa_family = AF_UNSPEC;
413 1.1.1.2 fvdl sa.sa_len = sizeof(sa);
414 1.1.1.2 fvdl eh2 = (struct ether_header *)sa.sa_data;
415 1.1.1.2 fvdl for (i = 0; i < 6; i++) {
416 1.1.1.2 fvdl eh2->ether_shost[i] = c = eh->ether_dhost[i];
417 1.1.1.2 fvdl eh2->ether_dhost[i] =
418 1.1.1.2 fvdl eh->ether_dhost[i] = eh->ether_shost[i];
419 1.1.1.2 fvdl eh->ether_shost[i] = c;
420 1.1.1.2 fvdl }
421 1.1.1.2 fvdl ifp->if_output(ifp, m, &sa, NULL);
422 1.1.1.2 fvdl return;
423 1.1.1.2 fvdl }
424 1.1.1.2 fvdl default:
425 1.1.1.2 fvdl m_freem(m);
426 1.1.1.2 fvdl return;
427 1.1.1.2 fvdl }
428 1.1.1.2 fvdl break;
429 1.1.1.2 fvdl #endif /* ISO */
430 1.1.1.2 fvdl #ifdef LLC
431 1.1.1.2 fvdl case LLC_X25_LSAP:
432 1.1.1.2 fvdl {
433 1.1 cgd if (m->m_pkthdr.len > eh->ether_type)
434 1.1 cgd m_adj(m, eh->ether_type - m->m_pkthdr.len);
435 1.1.1.2 fvdl M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
436 1.1 cgd if (m == 0)
437 1.1 cgd return;
438 1.1.1.2 fvdl if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
439 1.1.1.2 fvdl eh->ether_dhost, LLC_X25_LSAP, 6,
440 1.1.1.2 fvdl mtod(m, struct sdl_hdr *)))
441 1.1.1.2 fvdl panic("ETHER cons addr failure");
442 1.1.1.2 fvdl mtod(m, struct sdl_hdr *)->sdlhdr_len = eh->ether_type;
443 1.1.1.2 fvdl #ifdef LLC_DEBUG
444 1.1.1.2 fvdl printf("llc packet\n");
445 1.1.1.2 fvdl #endif /* LLC_DEBUG */
446 1.1.1.2 fvdl schednetisr(NETISR_CCITT);
447 1.1.1.2 fvdl inq = &llcintrq;
448 1.1 cgd break;
449 1.1 cgd }
450 1.1.1.2 fvdl #endif /* LLC */
451 1.1 cgd dropanyway:
452 1.1 cgd default:
453 1.1.1.2 fvdl m_freem(m);
454 1.1.1.2 fvdl return;
455 1.1.1.2 fvdl }
456 1.1.1.2 fvdl #else /* ISO || LLC */
457 1.1 cgd m_freem(m);
458 1.1 cgd return;
459 1.1.1.2 fvdl #endif /* ISO || LLC */
460 1.1 cgd }
461 1.1 cgd
462 1.1 cgd s = splimp();
463 1.1 cgd if (IF_QFULL(inq)) {
464 1.1 cgd IF_DROP(inq);
465 1.1 cgd m_freem(m);
466 1.1 cgd } else
467 1.1 cgd IF_ENQUEUE(inq, m);
468 1.1 cgd splx(s);
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd /*
472 1.1 cgd * Convert Ethernet address to printable (loggable) representation.
473 1.1 cgd */
474 1.1 cgd static char digits[] = "0123456789abcdef";
475 1.1 cgd char *
476 1.1 cgd ether_sprintf(ap)
477 1.1 cgd register u_char *ap;
478 1.1 cgd {
479 1.1 cgd register i;
480 1.1 cgd static char etherbuf[18];
481 1.1 cgd register char *cp = etherbuf;
482 1.1 cgd
483 1.1 cgd for (i = 0; i < 6; i++) {
484 1.1 cgd *cp++ = digits[*ap >> 4];
485 1.1 cgd *cp++ = digits[*ap++ & 0xf];
486 1.1 cgd *cp++ = ':';
487 1.1 cgd }
488 1.1 cgd *--cp = 0;
489 1.1 cgd return (etherbuf);
490 1.1.1.2 fvdl }
491 1.1.1.2 fvdl
492 1.1.1.2 fvdl /*
493 1.1.1.2 fvdl * Perform common duties while attaching to interface list
494 1.1.1.2 fvdl */
495 1.1.1.2 fvdl void
496 1.1.1.2 fvdl ether_ifattach(ifp)
497 1.1.1.2 fvdl register struct ifnet *ifp;
498 1.1.1.2 fvdl {
499 1.1.1.2 fvdl register struct ifaddr *ifa;
500 1.1.1.2 fvdl register struct sockaddr_dl *sdl;
501 1.1.1.2 fvdl
502 1.1.1.2 fvdl ifp->if_type = IFT_ETHER;
503 1.1.1.2 fvdl ifp->if_addrlen = 6;
504 1.1.1.2 fvdl ifp->if_hdrlen = 14;
505 1.1.1.2 fvdl ifp->if_mtu = ETHERMTU;
506 1.1.1.2 fvdl for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
507 1.1.1.2 fvdl if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
508 1.1.1.2 fvdl sdl->sdl_family == AF_LINK) {
509 1.1.1.2 fvdl sdl->sdl_type = IFT_ETHER;
510 1.1.1.2 fvdl sdl->sdl_alen = ifp->if_addrlen;
511 1.1.1.2 fvdl bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
512 1.1.1.2 fvdl LLADDR(sdl), ifp->if_addrlen);
513 1.1.1.2 fvdl break;
514 1.1.1.2 fvdl }
515 1.1.1.2 fvdl }
516 1.1.1.2 fvdl
517 1.1.1.2 fvdl u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
518 1.1.1.2 fvdl u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
519 1.1.1.2 fvdl /*
520 1.1.1.2 fvdl * Add an Ethernet multicast address or range of addresses to the list for a
521 1.1.1.2 fvdl * given interface.
522 1.1.1.2 fvdl */
523 1.1.1.2 fvdl int
524 1.1.1.2 fvdl ether_addmulti(ifr, ac)
525 1.1.1.2 fvdl struct ifreq *ifr;
526 1.1.1.2 fvdl register struct arpcom *ac;
527 1.1.1.2 fvdl {
528 1.1.1.2 fvdl register struct ether_multi *enm;
529 1.1.1.2 fvdl struct sockaddr_in *sin;
530 1.1.1.2 fvdl u_char addrlo[6];
531 1.1.1.2 fvdl u_char addrhi[6];
532 1.1.1.2 fvdl int s = splimp();
533 1.1.1.2 fvdl
534 1.1.1.2 fvdl switch (ifr->ifr_addr.sa_family) {
535 1.1.1.2 fvdl
536 1.1.1.2 fvdl case AF_UNSPEC:
537 1.1.1.2 fvdl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
538 1.1.1.2 fvdl bcopy(addrlo, addrhi, 6);
539 1.1.1.2 fvdl break;
540 1.1.1.2 fvdl
541 1.1.1.2 fvdl #ifdef INET
542 1.1.1.2 fvdl case AF_INET:
543 1.1.1.2 fvdl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
544 1.1.1.2 fvdl if (sin->sin_addr.s_addr == INADDR_ANY) {
545 1.1.1.2 fvdl /*
546 1.1.1.2 fvdl * An IP address of INADDR_ANY means listen to all
547 1.1.1.2 fvdl * of the Ethernet multicast addresses used for IP.
548 1.1.1.2 fvdl * (This is for the sake of IP multicast routers.)
549 1.1.1.2 fvdl */
550 1.1.1.2 fvdl bcopy(ether_ipmulticast_min, addrlo, 6);
551 1.1.1.2 fvdl bcopy(ether_ipmulticast_max, addrhi, 6);
552 1.1.1.2 fvdl }
553 1.1.1.2 fvdl else {
554 1.1.1.2 fvdl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
555 1.1.1.2 fvdl bcopy(addrlo, addrhi, 6);
556 1.1.1.2 fvdl }
557 1.1.1.2 fvdl break;
558 1.1.1.2 fvdl #endif
559 1.1.1.2 fvdl
560 1.1.1.2 fvdl default:
561 1.1.1.2 fvdl splx(s);
562 1.1.1.2 fvdl return (EAFNOSUPPORT);
563 1.1.1.2 fvdl }
564 1.1.1.2 fvdl
565 1.1.1.2 fvdl /*
566 1.1.1.2 fvdl * Verify that we have valid Ethernet multicast addresses.
567 1.1.1.2 fvdl */
568 1.1.1.2 fvdl if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
569 1.1.1.2 fvdl splx(s);
570 1.1.1.2 fvdl return (EINVAL);
571 1.1.1.2 fvdl }
572 1.1.1.2 fvdl /*
573 1.1.1.2 fvdl * See if the address range is already in the list.
574 1.1.1.2 fvdl */
575 1.1.1.2 fvdl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
576 1.1.1.2 fvdl if (enm != NULL) {
577 1.1.1.2 fvdl /*
578 1.1.1.2 fvdl * Found it; just increment the reference count.
579 1.1.1.2 fvdl */
580 1.1.1.2 fvdl ++enm->enm_refcount;
581 1.1.1.2 fvdl splx(s);
582 1.1.1.2 fvdl return (0);
583 1.1.1.2 fvdl }
584 1.1.1.2 fvdl /*
585 1.1.1.2 fvdl * New address or range; malloc a new multicast record
586 1.1.1.2 fvdl * and link it into the interface's multicast list.
587 1.1.1.2 fvdl */
588 1.1.1.2 fvdl enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
589 1.1.1.2 fvdl if (enm == NULL) {
590 1.1.1.2 fvdl splx(s);
591 1.1.1.2 fvdl return (ENOBUFS);
592 1.1.1.2 fvdl }
593 1.1.1.2 fvdl bcopy(addrlo, enm->enm_addrlo, 6);
594 1.1.1.2 fvdl bcopy(addrhi, enm->enm_addrhi, 6);
595 1.1.1.2 fvdl enm->enm_ac = ac;
596 1.1.1.2 fvdl enm->enm_refcount = 1;
597 1.1.1.2 fvdl enm->enm_next = ac->ac_multiaddrs;
598 1.1.1.2 fvdl ac->ac_multiaddrs = enm;
599 1.1.1.2 fvdl ac->ac_multicnt++;
600 1.1.1.2 fvdl splx(s);
601 1.1.1.2 fvdl /*
602 1.1.1.2 fvdl * Return ENETRESET to inform the driver that the list has changed
603 1.1.1.2 fvdl * and its reception filter should be adjusted accordingly.
604 1.1.1.2 fvdl */
605 1.1.1.2 fvdl return (ENETRESET);
606 1.1.1.2 fvdl }
607 1.1.1.2 fvdl
608 1.1.1.2 fvdl /*
609 1.1.1.2 fvdl * Delete a multicast address record.
610 1.1.1.2 fvdl */
611 1.1.1.2 fvdl int
612 1.1.1.2 fvdl ether_delmulti(ifr, ac)
613 1.1.1.2 fvdl struct ifreq *ifr;
614 1.1.1.2 fvdl register struct arpcom *ac;
615 1.1.1.2 fvdl {
616 1.1.1.2 fvdl register struct ether_multi *enm;
617 1.1.1.2 fvdl register struct ether_multi **p;
618 1.1.1.2 fvdl struct sockaddr_in *sin;
619 1.1.1.2 fvdl u_char addrlo[6];
620 1.1.1.2 fvdl u_char addrhi[6];
621 1.1.1.2 fvdl int s = splimp();
622 1.1.1.2 fvdl
623 1.1.1.2 fvdl switch (ifr->ifr_addr.sa_family) {
624 1.1.1.2 fvdl
625 1.1.1.2 fvdl case AF_UNSPEC:
626 1.1.1.2 fvdl bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
627 1.1.1.2 fvdl bcopy(addrlo, addrhi, 6);
628 1.1.1.2 fvdl break;
629 1.1.1.2 fvdl
630 1.1.1.2 fvdl #ifdef INET
631 1.1.1.2 fvdl case AF_INET:
632 1.1.1.2 fvdl sin = (struct sockaddr_in *)&(ifr->ifr_addr);
633 1.1.1.2 fvdl if (sin->sin_addr.s_addr == INADDR_ANY) {
634 1.1.1.2 fvdl /*
635 1.1.1.2 fvdl * An IP address of INADDR_ANY means stop listening
636 1.1.1.2 fvdl * to the range of Ethernet multicast addresses used
637 1.1.1.2 fvdl * for IP.
638 1.1.1.2 fvdl */
639 1.1.1.2 fvdl bcopy(ether_ipmulticast_min, addrlo, 6);
640 1.1.1.2 fvdl bcopy(ether_ipmulticast_max, addrhi, 6);
641 1.1.1.2 fvdl }
642 1.1.1.2 fvdl else {
643 1.1.1.2 fvdl ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
644 1.1.1.2 fvdl bcopy(addrlo, addrhi, 6);
645 1.1.1.2 fvdl }
646 1.1.1.2 fvdl break;
647 1.1.1.2 fvdl #endif
648 1.1.1.2 fvdl
649 1.1.1.2 fvdl default:
650 1.1.1.2 fvdl splx(s);
651 1.1.1.2 fvdl return (EAFNOSUPPORT);
652 1.1.1.2 fvdl }
653 1.1.1.2 fvdl
654 1.1.1.2 fvdl /*
655 1.1.1.2 fvdl * Look up the address in our list.
656 1.1.1.2 fvdl */
657 1.1.1.2 fvdl ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
658 1.1.1.2 fvdl if (enm == NULL) {
659 1.1.1.2 fvdl splx(s);
660 1.1.1.2 fvdl return (ENXIO);
661 1.1.1.2 fvdl }
662 1.1.1.2 fvdl if (--enm->enm_refcount != 0) {
663 1.1.1.2 fvdl /*
664 1.1.1.2 fvdl * Still some claims to this record.
665 1.1.1.2 fvdl */
666 1.1.1.2 fvdl splx(s);
667 1.1.1.2 fvdl return (0);
668 1.1.1.2 fvdl }
669 1.1.1.2 fvdl /*
670 1.1.1.2 fvdl * No remaining claims to this record; unlink and free it.
671 1.1.1.2 fvdl */
672 1.1.1.2 fvdl for (p = &enm->enm_ac->ac_multiaddrs;
673 1.1.1.2 fvdl *p != enm;
674 1.1.1.2 fvdl p = &(*p)->enm_next)
675 1.1.1.2 fvdl continue;
676 1.1.1.2 fvdl *p = (*p)->enm_next;
677 1.1.1.2 fvdl free(enm, M_IFMADDR);
678 1.1.1.2 fvdl ac->ac_multicnt--;
679 1.1.1.2 fvdl splx(s);
680 1.1.1.2 fvdl /*
681 1.1.1.2 fvdl * Return ENETRESET to inform the driver that the list has changed
682 1.1.1.2 fvdl * and its reception filter should be adjusted accordingly.
683 1.1.1.2 fvdl */
684 1.1.1.2 fvdl return (ENETRESET);
685 1.1 cgd }
686