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