1 | /* |
---|
2 | * Copyright (c) 1982, 1989, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 |
---|
30 | * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.189 2005/03/06 22:59:40 sobomax Exp $ |
---|
31 | */ |
---|
32 | |
---|
33 | /* |
---|
34 | * $Id$ |
---|
35 | */ |
---|
36 | |
---|
37 | #include "opt_atalk.h" |
---|
38 | #include "opt_inet.h" |
---|
39 | #include "opt_inet6.h" |
---|
40 | #include "opt_ipx.h" |
---|
41 | #include "opt_bdg.h" |
---|
42 | #include "opt_mac.h" |
---|
43 | #include "opt_netgraph.h" |
---|
44 | |
---|
45 | #include <sys/param.h> |
---|
46 | #include <sys/systm.h> |
---|
47 | #include <sys/kernel.h> |
---|
48 | #include <sys/malloc.h> |
---|
49 | #include <sys/mbuf.h> |
---|
50 | #include <sys/protosw.h> |
---|
51 | #include <sys/socket.h> |
---|
52 | #include <sys/ioctl.h> |
---|
53 | #include <sys/errno.h> |
---|
54 | #include <sys/syslog.h> |
---|
55 | #include <sys/sysctl.h> |
---|
56 | |
---|
57 | #include <net/if.h> |
---|
58 | #include <net/if_arp.h> |
---|
59 | #include <net/netisr.h> |
---|
60 | #include <net/route.h> |
---|
61 | #include <net/if_llc.h> |
---|
62 | #include <net/if_dl.h> |
---|
63 | #include <net/if_types.h> |
---|
64 | #include <net/ethernet.h> |
---|
65 | |
---|
66 | #if defined(INET) || defined(INET6) |
---|
67 | #include <netinet/in.h> |
---|
68 | #include <netinet/in_var.h> |
---|
69 | #include <netinet/if_ether.h> |
---|
70 | #include <netinet/ip_fw.h> |
---|
71 | #ifndef __rtems__ |
---|
72 | #include <netinet/ip_dummynet.h> |
---|
73 | #endif |
---|
74 | #endif |
---|
75 | #ifdef INET6 |
---|
76 | #include <netinet6/nd6.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | #ifdef DEV_CARP |
---|
80 | #include <netinet/ip_carp.h> |
---|
81 | #endif |
---|
82 | |
---|
83 | #ifdef IPX |
---|
84 | #include <netipx/ipx.h> |
---|
85 | #include <netipx/ipx_if.h> |
---|
86 | #endif |
---|
87 | |
---|
88 | #ifdef NETATALK |
---|
89 | #include <netatalk/at.h> |
---|
90 | #include <netatalk/at_var.h> |
---|
91 | #include <netatalk/at_extern.h> |
---|
92 | |
---|
93 | #define llc_snap_org_code llc_un.type_snap.org_code |
---|
94 | #define llc_snap_ether_type llc_un.type_snap.ether_type |
---|
95 | |
---|
96 | extern u_char at_org_code[3]; |
---|
97 | extern u_char aarp_org_code[3]; |
---|
98 | #endif /* NETATALK */ |
---|
99 | |
---|
100 | u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
---|
101 | #define senderr(e) do { error = (e); goto bad;} while (0) |
---|
102 | |
---|
103 | /* |
---|
104 | * Ethernet output routine. |
---|
105 | * Encapsulate a packet of type family for the local net. |
---|
106 | * Use trailer local net encapsulation if enough data in first |
---|
107 | * packet leaves a multiple of 512 bytes of data in remainder. |
---|
108 | * Assumes that ifp is actually pointer to arpcom structure. |
---|
109 | */ |
---|
110 | int |
---|
111 | ether_output(struct ifnet *ifp, struct mbuf *m, |
---|
112 | struct sockaddr *dst, struct rtentry *rt0) |
---|
113 | { |
---|
114 | short type; |
---|
115 | int s, error = 0; |
---|
116 | u_char edst[6]; |
---|
117 | register struct rtentry *rt; |
---|
118 | struct mbuf *mcopy = (struct mbuf *)0; |
---|
119 | register struct ether_header *eh; |
---|
120 | int off, len = m->m_pkthdr.len; |
---|
121 | struct arpcom *ac = (struct arpcom *)ifp; |
---|
122 | #ifdef NETATALK |
---|
123 | struct at_ifaddr *aa; |
---|
124 | #endif /* NETATALK */ |
---|
125 | int hlen; /* link layer header length */ |
---|
126 | |
---|
127 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) |
---|
128 | senderr(ENETDOWN); |
---|
129 | rt = rt0; |
---|
130 | if (rt) { |
---|
131 | if ((rt->rt_flags & RTF_UP) == 0) { |
---|
132 | rt0 = rt = rtalloc1(dst, 1, 0UL); |
---|
133 | if (rt0) |
---|
134 | rt->rt_refcnt--; |
---|
135 | else |
---|
136 | senderr(EHOSTUNREACH); |
---|
137 | } |
---|
138 | if (rt->rt_flags & RTF_GATEWAY) { |
---|
139 | if (rt->rt_gwroute == 0) |
---|
140 | goto lookup; |
---|
141 | if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { |
---|
142 | rtfree(rt); rt = rt0; |
---|
143 | lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, |
---|
144 | 0UL); |
---|
145 | if ((rt = rt->rt_gwroute) == 0) |
---|
146 | senderr(EHOSTUNREACH); |
---|
147 | } |
---|
148 | } |
---|
149 | if (rt->rt_flags & RTF_REJECT) |
---|
150 | if (rt->rt_rmx.rmx_expire == 0 || |
---|
151 | rtems_bsdnet_seconds_since_boot() < rt->rt_rmx.rmx_expire) |
---|
152 | senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); |
---|
153 | } |
---|
154 | hlen = ETHER_HDR_LEN; |
---|
155 | switch (dst->sa_family) { |
---|
156 | |
---|
157 | #ifdef INET |
---|
158 | case AF_INET: |
---|
159 | if (!arpresolve(ac, rt, m, dst, edst, rt0)) |
---|
160 | return (0); /* if not yet resolved */ |
---|
161 | /* If broadcasting on a simplex interface, loopback a copy */ |
---|
162 | if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) |
---|
163 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
---|
164 | off = m->m_pkthdr.len - m->m_len; |
---|
165 | type = htons(ETHERTYPE_IP); |
---|
166 | break; |
---|
167 | #endif |
---|
168 | #ifdef IPX |
---|
169 | case AF_IPX: |
---|
170 | { |
---|
171 | struct ifaddr *ia; |
---|
172 | |
---|
173 | type = htons(ETHERTYPE_IPX); |
---|
174 | bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), |
---|
175 | (caddr_t)edst, sizeof (edst)); |
---|
176 | for (ia = ifp->if_addrlist; ia != NULL; ia = ia->ifa_next) |
---|
177 | if(ia->ifa_addr->sa_family == AF_IPX && |
---|
178 | !bcmp((caddr_t)edst, |
---|
179 | (caddr_t)&((struct ipx_ifaddr *)ia)->ia_addr.sipx_addr.x_host, |
---|
180 | sizeof(edst))) |
---|
181 | return (looutput(ifp, m, dst, rt)); |
---|
182 | /* If broadcasting on a simplex interface, loopback a copy */ |
---|
183 | if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) |
---|
184 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
---|
185 | break; |
---|
186 | } |
---|
187 | #endif |
---|
188 | #ifdef NETATALK |
---|
189 | case AF_APPLETALK: |
---|
190 | { |
---|
191 | struct sockaddr_at *sat = (struct sockaddr_at *)dst; |
---|
192 | |
---|
193 | /* |
---|
194 | * super hack.. |
---|
195 | * Most of this loopback code should move into the appletalk |
---|
196 | * code, but it's here for now.. remember to move it! [JRE] |
---|
197 | * This may not get the same interface we started with |
---|
198 | * fix asap. XXX |
---|
199 | */ |
---|
200 | aa = at_ifawithnet( sat ); |
---|
201 | if (aa == NULL) { |
---|
202 | goto bad; |
---|
203 | } |
---|
204 | if( aa->aa_ifa.ifa_ifp != ifp ) { |
---|
205 | (*aa->aa_ifa.ifa_ifp->if_output)(aa->aa_ifa.ifa_ifp, |
---|
206 | m,dst,rt); |
---|
207 | } |
---|
208 | if (((sat->sat_addr.s_net == ATADDR_ANYNET) |
---|
209 | && (sat->sat_addr.s_node == ATADDR_ANYNODE)) |
---|
210 | || ((sat->sat_addr.s_net == aa->aa_addr.sat_addr.s_net ) |
---|
211 | && (sat->sat_addr.s_node == aa->aa_addr.sat_addr.s_node))) { |
---|
212 | (void) looutput(ifp, m, dst, rt); |
---|
213 | return(0); |
---|
214 | } |
---|
215 | |
---|
216 | if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) { |
---|
217 | #ifdef NETATALKDEBUG |
---|
218 | extern char *prsockaddr(struct sockaddr *); |
---|
219 | printf("aarpresolv: failed for %s\n", prsockaddr(dst)); |
---|
220 | #endif /* NETATALKDEBUG */ |
---|
221 | return (0); |
---|
222 | } |
---|
223 | |
---|
224 | /* |
---|
225 | * If broadcasting on a simplex interface, loopback a copy |
---|
226 | */ |
---|
227 | if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) |
---|
228 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
---|
229 | } |
---|
230 | /* |
---|
231 | * In the phase 2 case, we need to prepend an mbuf for the llc header. |
---|
232 | * Since we must preserve the value of m, which is passed to us by |
---|
233 | * value, we m_copy() the first mbuf, and use it for our llc header. |
---|
234 | */ |
---|
235 | if ( aa->aa_flags & AFA_PHASE2 ) { |
---|
236 | struct llc llc; |
---|
237 | |
---|
238 | M_PREPEND(m, sizeof(struct llc), M_WAIT); |
---|
239 | len += sizeof(struct llc); |
---|
240 | llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; |
---|
241 | llc.llc_control = LLC_UI; |
---|
242 | bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); |
---|
243 | llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); |
---|
244 | bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc)); |
---|
245 | type = htons(m->m_pkthdr.len); |
---|
246 | } else { |
---|
247 | type = htons(ETHERTYPE_AT); |
---|
248 | } |
---|
249 | break; |
---|
250 | #endif /* NETATALK */ |
---|
251 | |
---|
252 | case AF_UNSPEC: |
---|
253 | eh = (struct ether_header *)dst->sa_data; |
---|
254 | (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); |
---|
255 | type = eh->ether_type; |
---|
256 | break; |
---|
257 | |
---|
258 | default: |
---|
259 | printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, |
---|
260 | dst->sa_family); |
---|
261 | senderr(EAFNOSUPPORT); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | if (mcopy) |
---|
266 | (void) looutput(ifp, mcopy, dst, rt); |
---|
267 | /* |
---|
268 | * Add local net header. If no space in first mbuf, |
---|
269 | * allocate another. |
---|
270 | */ |
---|
271 | M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); |
---|
272 | if (m == NULL) |
---|
273 | senderr(ENOBUFS); |
---|
274 | eh = mtod(m, struct ether_header *); |
---|
275 | (void)memcpy(&eh->ether_type, &type, |
---|
276 | sizeof(eh->ether_type)); |
---|
277 | (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); |
---|
278 | (void)memcpy(eh->ether_shost, ac->ac_enaddr, |
---|
279 | sizeof(eh->ether_shost)); |
---|
280 | s = splimp(); |
---|
281 | /* |
---|
282 | * Queue message on interface, and start output if interface |
---|
283 | * not yet active. |
---|
284 | */ |
---|
285 | if (IF_QFULL(&ifp->if_snd)) { |
---|
286 | IF_DROP(&ifp->if_snd); |
---|
287 | splx(s); |
---|
288 | senderr(ENOBUFS); |
---|
289 | } |
---|
290 | IF_ENQUEUE(&ifp->if_snd, m); |
---|
291 | if ((ifp->if_flags & IFF_OACTIVE) == 0) |
---|
292 | (*ifp->if_start)(ifp); |
---|
293 | splx(s); |
---|
294 | ifp->if_obytes += len + sizeof (struct ether_header); |
---|
295 | if (m->m_flags & M_MCAST) |
---|
296 | ifp->if_omcasts++; |
---|
297 | return (error); |
---|
298 | |
---|
299 | bad: |
---|
300 | if (m) |
---|
301 | m_freem(m); |
---|
302 | return (error); |
---|
303 | } |
---|
304 | |
---|
305 | /* |
---|
306 | * Process a received Ethernet packet; |
---|
307 | * the packet is in the mbuf chain m without |
---|
308 | * the ether header, which is provided separately. |
---|
309 | */ |
---|
310 | void |
---|
311 | ether_input(ifp, eh, m) |
---|
312 | struct ifnet *ifp; |
---|
313 | register struct ether_header *eh; |
---|
314 | struct mbuf *m; |
---|
315 | { |
---|
316 | register struct ifqueue *inq; |
---|
317 | u_short ether_type; |
---|
318 | int s; |
---|
319 | #if defined(NETATALK) |
---|
320 | struct llc *l; |
---|
321 | #endif |
---|
322 | |
---|
323 | if ((ifp->if_flags & IFF_UP) == 0) { |
---|
324 | m_freem(m); |
---|
325 | return; |
---|
326 | } |
---|
327 | ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); |
---|
328 | if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, |
---|
329 | sizeof(etherbroadcastaddr)) == 0) |
---|
330 | m->m_flags |= M_BCAST; |
---|
331 | else if (eh->ether_dhost[0] & 1) |
---|
332 | m->m_flags |= M_MCAST; |
---|
333 | if (m->m_flags & (M_BCAST|M_MCAST)) |
---|
334 | ifp->if_imcasts++; |
---|
335 | |
---|
336 | /* |
---|
337 | * RTEMS addition -- allow application to `tap into' |
---|
338 | * the incoming packet stream. |
---|
339 | */ |
---|
340 | if (ifp->if_tap && (*ifp->if_tap)(ifp, eh, m)) { |
---|
341 | m_freem(m); |
---|
342 | return; |
---|
343 | } |
---|
344 | |
---|
345 | ether_type = ntohs(eh->ether_type); |
---|
346 | |
---|
347 | switch (ether_type) { |
---|
348 | #ifdef INET |
---|
349 | case ETHERTYPE_IP: |
---|
350 | schednetisr(NETISR_IP); |
---|
351 | inq = &ipintrq; |
---|
352 | break; |
---|
353 | |
---|
354 | case ETHERTYPE_ARP: |
---|
355 | schednetisr(NETISR_ARP); |
---|
356 | inq = &arpintrq; |
---|
357 | break; |
---|
358 | #endif |
---|
359 | #ifdef IPX |
---|
360 | case ETHERTYPE_IPX: |
---|
361 | schednetisr(NETISR_IPX); |
---|
362 | inq = &ipxintrq; |
---|
363 | break; |
---|
364 | #endif |
---|
365 | #ifdef NETATALK |
---|
366 | case ETHERTYPE_AT: |
---|
367 | schednetisr(NETISR_ATALK); |
---|
368 | inq = &atintrq1; |
---|
369 | break; |
---|
370 | case ETHERTYPE_AARP: |
---|
371 | /* probably this should be done with a NETISR as well */ |
---|
372 | aarpinput((struct arpcom *)ifp, m); /* XXX */ |
---|
373 | return; |
---|
374 | #endif /* NETATALK */ |
---|
375 | default: |
---|
376 | #if defined (ISO) || defined (LLC) || defined(NETATALK) |
---|
377 | if (ether_type > ETHERMTU) |
---|
378 | goto dropanyway; |
---|
379 | l = mtod(m, struct llc *); |
---|
380 | switch (l->llc_dsap) { |
---|
381 | #ifdef NETATALK |
---|
382 | case LLC_SNAP_LSAP: |
---|
383 | switch (l->llc_control) { |
---|
384 | case LLC_UI: |
---|
385 | if (l->llc_ssap != LLC_SNAP_LSAP) |
---|
386 | goto dropanyway; |
---|
387 | |
---|
388 | if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code, |
---|
389 | sizeof(at_org_code)) == 0 && |
---|
390 | ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { |
---|
391 | inq = &atintrq2; |
---|
392 | m_adj( m, sizeof( struct llc )); |
---|
393 | schednetisr(NETISR_ATALK); |
---|
394 | break; |
---|
395 | } |
---|
396 | |
---|
397 | if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, |
---|
398 | sizeof(aarp_org_code)) == 0 && |
---|
399 | ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { |
---|
400 | m_adj( m, sizeof( struct llc )); |
---|
401 | aarpinput((struct arpcom *)ifp, m); /* XXX */ |
---|
402 | return; |
---|
403 | } |
---|
404 | |
---|
405 | default: |
---|
406 | goto dropanyway; |
---|
407 | } |
---|
408 | break; |
---|
409 | #endif /* NETATALK */ |
---|
410 | #ifdef ISO |
---|
411 | case LLC_ISO_LSAP: |
---|
412 | switch (l->llc_control) { |
---|
413 | case LLC_UI: |
---|
414 | /* LLC_UI_P forbidden in class 1 service */ |
---|
415 | if ((l->llc_dsap == LLC_ISO_LSAP) && |
---|
416 | (l->llc_ssap == LLC_ISO_LSAP)) { |
---|
417 | /* LSAP for ISO */ |
---|
418 | if (m->m_pkthdr.len > ether_type) |
---|
419 | m_adj(m, ether_type - m->m_pkthdr.len); |
---|
420 | m->m_data += 3; /* XXX */ |
---|
421 | m->m_len -= 3; /* XXX */ |
---|
422 | m->m_pkthdr.len -= 3; /* XXX */ |
---|
423 | M_PREPEND(m, sizeof *eh, M_DONTWAIT); |
---|
424 | if (m == 0) |
---|
425 | return; |
---|
426 | *mtod(m, struct ether_header *) = *eh; |
---|
427 | IFDEBUG(D_ETHER) |
---|
428 | printf("clnp packet"); |
---|
429 | ENDDEBUG |
---|
430 | schednetisr(NETISR_ISO); |
---|
431 | inq = &clnlintrq; |
---|
432 | break; |
---|
433 | } |
---|
434 | goto dropanyway; |
---|
435 | |
---|
436 | case LLC_XID: |
---|
437 | case LLC_XID_P: |
---|
438 | if(m->m_len < 6) |
---|
439 | goto dropanyway; |
---|
440 | l->llc_window = 0; |
---|
441 | l->llc_fid = 9; |
---|
442 | l->llc_class = 1; |
---|
443 | l->llc_dsap = l->llc_ssap = 0; |
---|
444 | /* Fall through to */ |
---|
445 | case LLC_TEST: |
---|
446 | case LLC_TEST_P: |
---|
447 | { |
---|
448 | struct sockaddr sa; |
---|
449 | register struct ether_header *eh2; |
---|
450 | int i; |
---|
451 | u_char c = l->llc_dsap; |
---|
452 | |
---|
453 | l->llc_dsap = l->llc_ssap; |
---|
454 | l->llc_ssap = c; |
---|
455 | if (m->m_flags & (M_BCAST | M_MCAST)) |
---|
456 | bcopy((caddr_t)ac->ac_enaddr, |
---|
457 | (caddr_t)eh->ether_dhost, 6); |
---|
458 | sa.sa_family = AF_UNSPEC; |
---|
459 | sa.sa_len = sizeof(sa); |
---|
460 | eh2 = (struct ether_header *)sa.sa_data; |
---|
461 | for (i = 0; i < 6; i++) { |
---|
462 | eh2->ether_shost[i] = c = eh->ether_dhost[i]; |
---|
463 | eh2->ether_dhost[i] = |
---|
464 | eh->ether_dhost[i] = eh->ether_shost[i]; |
---|
465 | eh->ether_shost[i] = c; |
---|
466 | } |
---|
467 | ifp->if_output(ifp, m, &sa, NULL); |
---|
468 | return; |
---|
469 | } |
---|
470 | default: |
---|
471 | m_freem(m); |
---|
472 | return; |
---|
473 | } |
---|
474 | break; |
---|
475 | #endif /* ISO */ |
---|
476 | #ifdef LLC |
---|
477 | case LLC_X25_LSAP: |
---|
478 | { |
---|
479 | if (m->m_pkthdr.len > ether_type) |
---|
480 | m_adj(m, ether_type - m->m_pkthdr.len); |
---|
481 | M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); |
---|
482 | if (m == 0) |
---|
483 | return; |
---|
484 | if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, |
---|
485 | eh->ether_dhost, LLC_X25_LSAP, 6, |
---|
486 | mtod(m, struct sdl_hdr *))) |
---|
487 | panic("ETHER cons addr failure"); |
---|
488 | mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type; |
---|
489 | #ifdef LLC_DEBUG |
---|
490 | printf("llc packet\n"); |
---|
491 | #endif /* LLC_DEBUG */ |
---|
492 | schednetisr(NETISR_CCITT); |
---|
493 | inq = &llcintrq; |
---|
494 | break; |
---|
495 | } |
---|
496 | #endif /* LLC */ |
---|
497 | dropanyway: |
---|
498 | default: |
---|
499 | m_freem(m); |
---|
500 | return; |
---|
501 | } |
---|
502 | #else /* ISO || LLC || NETATALK */ |
---|
503 | m_freem(m); |
---|
504 | return; |
---|
505 | #endif /* ISO || LLC || NETATALK */ |
---|
506 | } |
---|
507 | |
---|
508 | s = splimp(); |
---|
509 | if (IF_QFULL(inq)) { |
---|
510 | IF_DROP(inq); |
---|
511 | m_freem(m); |
---|
512 | } else |
---|
513 | IF_ENQUEUE(inq, m); |
---|
514 | splx(s); |
---|
515 | } |
---|
516 | |
---|
517 | /* |
---|
518 | * Convert Ethernet address to printable (loggable) representation. |
---|
519 | * The static buffer isn't a really huge problem since this code |
---|
520 | * is protected by the RTEMS network mutex. |
---|
521 | */ |
---|
522 | char * |
---|
523 | ether_sprintf(const u_int8_t *addr) |
---|
524 | { |
---|
525 | static char buf[32]; |
---|
526 | char *b = buf; |
---|
527 | int i; |
---|
528 | |
---|
529 | for (i = 0; i < ETHER_ADDR_LEN; i++, b+=3) |
---|
530 | sprintf(b, "%02x:", *addr++); |
---|
531 | *--b = '\0'; |
---|
532 | return buf; |
---|
533 | } |
---|
534 | |
---|
535 | /* |
---|
536 | * Perform common duties while attaching to interface list |
---|
537 | */ |
---|
538 | void |
---|
539 | ether_ifattach(struct ifnet *ifp) |
---|
540 | { |
---|
541 | struct ifaddr *ifa; |
---|
542 | struct sockaddr_dl *sdl; |
---|
543 | |
---|
544 | ifp->if_type = IFT_ETHER; |
---|
545 | ifp->if_addrlen = ETHER_ADDR_LEN; |
---|
546 | ifp->if_hdrlen = ETHER_HDR_LEN; |
---|
547 | ifp->if_mtu = ETHERMTU; |
---|
548 | if (ifp->if_baudrate == 0) |
---|
549 | ifp->if_baudrate = 10000000; |
---|
550 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
---|
551 | if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && |
---|
552 | sdl->sdl_family == AF_LINK) { |
---|
553 | sdl->sdl_type = IFT_ETHER; |
---|
554 | sdl->sdl_alen = ifp->if_addrlen; |
---|
555 | bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, |
---|
556 | LLADDR(sdl), ifp->if_addrlen); |
---|
557 | break; |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | #if defined(__rtems__) |
---|
562 | u_char ether_ipmulticast_min[6] = |
---|
563 | { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; |
---|
564 | u_char ether_ipmulticast_max[6] = |
---|
565 | { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; |
---|
566 | #else |
---|
567 | static u_char ether_ipmulticast_min[6] = |
---|
568 | { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; |
---|
569 | static u_char ether_ipmulticast_max[6] = |
---|
570 | { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; |
---|
571 | #endif |
---|
572 | |
---|
573 | /* |
---|
574 | * Add an Ethernet multicast address or range of addresses to the list for a |
---|
575 | * given interface. |
---|
576 | */ |
---|
577 | int |
---|
578 | ether_addmulti(ifr, ac) |
---|
579 | struct ifreq *ifr; |
---|
580 | register struct arpcom *ac; |
---|
581 | { |
---|
582 | register struct ether_multi *enm; |
---|
583 | struct sockaddr_in *sin; |
---|
584 | u_char addrlo[6]; |
---|
585 | u_char addrhi[6]; |
---|
586 | int set_allmulti = 0; |
---|
587 | int s = splimp(); |
---|
588 | |
---|
589 | switch (ifr->ifr_addr.sa_family) { |
---|
590 | |
---|
591 | case AF_UNSPEC: |
---|
592 | bcopy(ifr->ifr_addr.sa_data, addrlo, 6); |
---|
593 | bcopy(addrlo, addrhi, 6); |
---|
594 | break; |
---|
595 | |
---|
596 | #ifdef INET |
---|
597 | case AF_INET: |
---|
598 | sin = (struct sockaddr_in *)&(ifr->ifr_addr); |
---|
599 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
---|
600 | /* |
---|
601 | * An IP address of INADDR_ANY means listen to all |
---|
602 | * of the Ethernet multicast addresses used for IP. |
---|
603 | * (This is for the sake of IP multicast routers.) |
---|
604 | */ |
---|
605 | bcopy(ether_ipmulticast_min, addrlo, 6); |
---|
606 | bcopy(ether_ipmulticast_max, addrhi, 6); |
---|
607 | set_allmulti = 1; |
---|
608 | } |
---|
609 | else { |
---|
610 | ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); |
---|
611 | bcopy(addrlo, addrhi, 6); |
---|
612 | } |
---|
613 | break; |
---|
614 | #endif |
---|
615 | |
---|
616 | default: |
---|
617 | splx(s); |
---|
618 | return (EAFNOSUPPORT); |
---|
619 | } |
---|
620 | |
---|
621 | /* |
---|
622 | * Verify that we have valid Ethernet multicast addresses. |
---|
623 | */ |
---|
624 | if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { |
---|
625 | splx(s); |
---|
626 | return (EINVAL); |
---|
627 | } |
---|
628 | /* |
---|
629 | * See if the address range is already in the list. |
---|
630 | */ |
---|
631 | ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); |
---|
632 | if (enm != NULL) { |
---|
633 | /* |
---|
634 | * Found it; just increment the reference count. |
---|
635 | */ |
---|
636 | ++enm->enm_refcount; |
---|
637 | splx(s); |
---|
638 | return (0); |
---|
639 | } |
---|
640 | /* |
---|
641 | * New address or range; malloc a new multicast record |
---|
642 | * and link it into the interface's multicast list. |
---|
643 | */ |
---|
644 | enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); |
---|
645 | if (enm == NULL) { |
---|
646 | splx(s); |
---|
647 | return (ENOBUFS); |
---|
648 | } |
---|
649 | bcopy(addrlo, enm->enm_addrlo, 6); |
---|
650 | bcopy(addrhi, enm->enm_addrhi, 6); |
---|
651 | enm->enm_ac = ac; |
---|
652 | enm->enm_refcount = 1; |
---|
653 | enm->enm_next = ac->ac_multiaddrs; |
---|
654 | ac->ac_multiaddrs = enm; |
---|
655 | ac->ac_multicnt++; |
---|
656 | splx(s); |
---|
657 | if (set_allmulti) |
---|
658 | ac->ac_if.if_flags |= IFF_ALLMULTI; |
---|
659 | |
---|
660 | /* |
---|
661 | * Return ENETRESET to inform the driver that the list has changed |
---|
662 | * and its reception filter should be adjusted accordingly. |
---|
663 | */ |
---|
664 | return (ENETRESET); |
---|
665 | } |
---|
666 | |
---|
667 | /* |
---|
668 | * Delete a multicast address record. |
---|
669 | */ |
---|
670 | int |
---|
671 | ether_delmulti(ifr, ac) |
---|
672 | struct ifreq *ifr; |
---|
673 | register struct arpcom *ac; |
---|
674 | { |
---|
675 | register struct ether_multi *enm; |
---|
676 | register struct ether_multi **p; |
---|
677 | struct sockaddr_in *sin; |
---|
678 | u_char addrlo[6]; |
---|
679 | u_char addrhi[6]; |
---|
680 | int unset_allmulti = 0; |
---|
681 | int s = splimp(); |
---|
682 | |
---|
683 | switch (ifr->ifr_addr.sa_family) { |
---|
684 | |
---|
685 | case AF_UNSPEC: |
---|
686 | bcopy(ifr->ifr_addr.sa_data, addrlo, 6); |
---|
687 | bcopy(addrlo, addrhi, 6); |
---|
688 | break; |
---|
689 | |
---|
690 | #ifdef INET |
---|
691 | case AF_INET: |
---|
692 | sin = (struct sockaddr_in *)&(ifr->ifr_addr); |
---|
693 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
---|
694 | /* |
---|
695 | * An IP address of INADDR_ANY means stop listening |
---|
696 | * to the range of Ethernet multicast addresses used |
---|
697 | * for IP. |
---|
698 | */ |
---|
699 | bcopy(ether_ipmulticast_min, addrlo, 6); |
---|
700 | bcopy(ether_ipmulticast_max, addrhi, 6); |
---|
701 | unset_allmulti = 1; |
---|
702 | } |
---|
703 | else { |
---|
704 | ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); |
---|
705 | bcopy(addrlo, addrhi, 6); |
---|
706 | } |
---|
707 | break; |
---|
708 | #endif |
---|
709 | |
---|
710 | default: |
---|
711 | splx(s); |
---|
712 | return (EAFNOSUPPORT); |
---|
713 | } |
---|
714 | |
---|
715 | /* |
---|
716 | * Look up the address in our list. |
---|
717 | */ |
---|
718 | ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); |
---|
719 | if (enm == NULL) { |
---|
720 | splx(s); |
---|
721 | return (ENXIO); |
---|
722 | } |
---|
723 | if (--enm->enm_refcount != 0) { |
---|
724 | /* |
---|
725 | * Still some claims to this record. |
---|
726 | */ |
---|
727 | splx(s); |
---|
728 | return (0); |
---|
729 | } |
---|
730 | /* |
---|
731 | * No remaining claims to this record; unlink and free it. |
---|
732 | */ |
---|
733 | for (p = &enm->enm_ac->ac_multiaddrs; |
---|
734 | *p != enm; |
---|
735 | p = &(*p)->enm_next) |
---|
736 | continue; |
---|
737 | *p = (*p)->enm_next; |
---|
738 | free(enm, M_IFMADDR); |
---|
739 | ac->ac_multicnt--; |
---|
740 | splx(s); |
---|
741 | if (unset_allmulti) |
---|
742 | ac->ac_if.if_flags &= ~IFF_ALLMULTI; |
---|
743 | |
---|
744 | /* |
---|
745 | * Return ENETRESET to inform the driver that the list has changed |
---|
746 | * and its reception filter should be adjusted accordingly. |
---|
747 | */ |
---|
748 | return (ENETRESET); |
---|
749 | } |
---|
750 | |
---|
751 | SYSCTL_DECL(_net_link); |
---|
752 | SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); |
---|
753 | |
---|
754 | #if 0 |
---|
755 | /* |
---|
756 | * This is for reference. We have a table-driven version |
---|
757 | * of the little-endian crc32 generator, which is faster |
---|
758 | * than the double-loop. |
---|
759 | */ |
---|
760 | uint32_t |
---|
761 | ether_crc32_le(const uint8_t *buf, size_t len) |
---|
762 | { |
---|
763 | size_t i; |
---|
764 | uint32_t crc; |
---|
765 | int bit; |
---|
766 | uint8_t data; |
---|
767 | |
---|
768 | crc = 0xffffffff; /* initial value */ |
---|
769 | |
---|
770 | for (i = 0; i < len; i++) { |
---|
771 | for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) |
---|
772 | carry = (crc ^ data) & 1; |
---|
773 | crc >>= 1; |
---|
774 | if (carry) |
---|
775 | crc = (crc ^ ETHER_CRC_POLY_LE); |
---|
776 | } |
---|
777 | |
---|
778 | return (crc); |
---|
779 | } |
---|
780 | #else |
---|
781 | uint32_t |
---|
782 | ether_crc32_le(const uint8_t *buf, size_t len) |
---|
783 | { |
---|
784 | static const uint32_t crctab[] = { |
---|
785 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
---|
786 | 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
---|
787 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
---|
788 | 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
---|
789 | }; |
---|
790 | size_t i; |
---|
791 | uint32_t crc; |
---|
792 | |
---|
793 | crc = 0xffffffff; /* initial value */ |
---|
794 | |
---|
795 | for (i = 0; i < len; i++) { |
---|
796 | crc ^= buf[i]; |
---|
797 | crc = (crc >> 4) ^ crctab[crc & 0xf]; |
---|
798 | crc = (crc >> 4) ^ crctab[crc & 0xf]; |
---|
799 | } |
---|
800 | |
---|
801 | return (crc); |
---|
802 | } |
---|
803 | #endif |
---|
804 | |
---|
805 | uint32_t |
---|
806 | ether_crc32_be(const uint8_t *buf, size_t len) |
---|
807 | { |
---|
808 | size_t i; |
---|
809 | uint32_t crc, carry; |
---|
810 | int bit; |
---|
811 | uint8_t data; |
---|
812 | |
---|
813 | crc = 0xffffffff; /* initial value */ |
---|
814 | |
---|
815 | for (i = 0; i < len; i++) { |
---|
816 | for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { |
---|
817 | carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); |
---|
818 | crc <<= 1; |
---|
819 | if (carry) |
---|
820 | crc = (crc ^ ETHER_CRC_POLY_BE) | carry; |
---|
821 | } |
---|
822 | } |
---|
823 | |
---|
824 | return (crc); |
---|
825 | } |
---|
826 | |
---|
827 | int |
---|
828 | ether_ioctl(struct ifnet *ifp, int command, caddr_t data) |
---|
829 | { |
---|
830 | struct ifaddr *ifa = (struct ifaddr *) data; |
---|
831 | struct ifreq *ifr = (struct ifreq *) data; |
---|
832 | int error = 0; |
---|
833 | |
---|
834 | switch (command) { |
---|
835 | case SIOCSIFADDR: |
---|
836 | ifp->if_flags |= IFF_UP; |
---|
837 | |
---|
838 | switch (ifa->ifa_addr->sa_family) { |
---|
839 | #ifdef INET |
---|
840 | case AF_INET: |
---|
841 | ifp->if_init(ifp->if_softc); /* before arpwhohas */ |
---|
842 | arp_ifinit((struct arpcom *)ifp, ifa); |
---|
843 | break; |
---|
844 | #endif |
---|
845 | #ifdef IPX |
---|
846 | /* |
---|
847 | * XXX - This code is probably wrong |
---|
848 | */ |
---|
849 | case AF_IPX: |
---|
850 | { |
---|
851 | register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); |
---|
852 | struct arpcom *ac = (struct arpcom *) (ifp->if_softc); |
---|
853 | |
---|
854 | if (ipx_nullhost(*ina)) |
---|
855 | ina->x_host = |
---|
856 | *(union ipx_host *) |
---|
857 | ac->ac_enaddr; |
---|
858 | else { |
---|
859 | bcopy((caddr_t) ina->x_host.c_host, |
---|
860 | (caddr_t) ac->ac_enaddr, |
---|
861 | sizeof(ac->ac_enaddr)); |
---|
862 | } |
---|
863 | |
---|
864 | /* |
---|
865 | * Set new address |
---|
866 | */ |
---|
867 | ifp->if_init(ifp->if_softc); |
---|
868 | break; |
---|
869 | } |
---|
870 | #endif |
---|
871 | default: |
---|
872 | ifp->if_init(ifp->if_softc); |
---|
873 | break; |
---|
874 | } |
---|
875 | break; |
---|
876 | |
---|
877 | case SIOCGIFADDR: |
---|
878 | { |
---|
879 | struct sockaddr *sa; |
---|
880 | |
---|
881 | sa = (struct sockaddr *) & ifr->ifr_data; |
---|
882 | bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, |
---|
883 | (caddr_t) sa->sa_data, ETHER_ADDR_LEN); |
---|
884 | } |
---|
885 | break; |
---|
886 | |
---|
887 | case SIOCSIFMTU: |
---|
888 | /* |
---|
889 | * Set the interface MTU. |
---|
890 | */ |
---|
891 | if (ifr->ifr_mtu > ETHERMTU) { |
---|
892 | error = EINVAL; |
---|
893 | } else { |
---|
894 | ifp->if_mtu = ifr->ifr_mtu; |
---|
895 | } |
---|
896 | break; |
---|
897 | } |
---|
898 | return (error); |
---|
899 | } |
---|