2 * $Id: ether_mod-2.4.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * OpenBSD 2.4 ether_mod.c - lkm replacement for ether_output
7 * Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
9 * Original code and idea 1997 Thomas Ptacek <tqbf@pobox.com>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
49 #include <machine/cpu.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <net/if_llc.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
58 #include <netinet/in.h>
60 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
66 #include <netns/ns_if.h>
70 #include <netipx/ipx.h>
71 #include <netipx/ipx_if.h>
75 #include <netiso/argo_debug.h>
76 #include <netiso/iso.h>
77 #include <netiso/iso_var.h>
78 #include <netiso/iso_snpac.h>
81 #include <netccitt/x25.h>
82 #include <netccitt/pk.h>
83 #include <netccitt/pk_extern.h>
84 #include <netccitt/dll.h>
85 #include <netccitt/llc_var.h>
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
92 #define llc_snap_org_code llc_un.type_snap.org_code
93 #define llc_snap_ether_type llc_un.type_snap.ether_type
95 extern u_char at_org_code[ 3 ];
96 extern u_char aarp_org_code[ 3 ];
100 #include <sys/socketvar.h>
103 #define senderr(e) { error = (e); goto bad;}
106 int ether_output_spoof(register struct ifnet *, struct mbuf *,
107 struct sockaddr *, struct rtentry *);
110 * Ethernet output routine.
111 * Encapsulate a packet of type family for the local net.
112 * Assumes that ifp is actually pointer to arpcom structure.
115 ether_output_spoof(ifp, m0, dst, rt0)
116 register struct ifnet *ifp;
118 struct sockaddr *dst;
123 u_char edst[6], esrc[6];
124 register struct mbuf *m = m0;
125 register struct rtentry *rt;
126 struct mbuf *mcopy = (struct mbuf *)0;
127 register struct ether_header *eh;
128 struct arpcom *ac = (struct arpcom *)ifp;
130 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
132 ifp->if_lastchange = time;
133 if ((rt = rt0) != NULL) {
134 if ((rt->rt_flags & RTF_UP) == 0) {
135 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL)
138 senderr(EHOSTUNREACH);
140 if (rt->rt_flags & RTF_GATEWAY) {
141 if (rt->rt_gwroute == 0)
143 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
144 rtfree(rt); rt = rt0;
145 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
146 if ((rt = rt->rt_gwroute) == 0)
147 senderr(EHOSTUNREACH);
150 if (rt->rt_flags & RTF_REJECT)
151 if (rt->rt_rmx.rmx_expire == 0 ||
152 time.tv_sec < rt->rt_rmx.rmx_expire)
153 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
155 switch (dst->sa_family) {
159 if (!arpresolve(ac, rt, m, dst, edst))
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 etype = htons(ETHERTYPE_IP);
165 bcopy((caddr_t)ac->ac_enaddr,esrc,sizeof(esrc));
170 etype = htons(ETHERTYPE_NS);
171 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
172 (caddr_t)edst, sizeof (edst));
173 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
174 return (looutput(ifp, m, dst, rt));
175 /* If broadcasting on a simplex interface, loopback a copy */
176 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
177 mcopy = m_copy(m, 0, (int)M_COPYALL);
182 etype = htons(ETHERTYPE_IPX);
183 bcopy((caddr_t)&satosipx(dst)->sipx_addr.ipx_host,
184 (caddr_t)edst, sizeof (edst));
185 if (!bcmp((caddr_t)edst, (caddr_t)&ipx_thishost, sizeof(edst)))
186 return (looutput(ifp, m, dst, rt));
187 /* If broadcasting on a simplex interface, loopback a copy */
188 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
189 mcopy = m_copy(m, 0, (int)M_COPYALL);
194 struct at_ifaddr *aa;
196 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
198 extern char *prsockaddr(struct sockaddr *);
199 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
200 #endif /* NETATALKDEBUG */
205 * ifaddr is the first thing in at_ifaddr
207 aa = (struct at_ifaddr *)at_ifawithnet(
208 (struct sockaddr_at *)dst,
209 ifp->if_addrlist.tqh_first);
214 * In the phase 2 case, we need to prepend an mbuf for the llc
215 * header. Since we must preserve the value of m, which is
216 * passed to us by value, we m_copy() the first mbuf,
217 * and use it for our llc header.
219 if ( aa->aa_flags & AFA_PHASE2 ) {
222 /* XXX Really this should use netisr too */
223 M_PREPEND(m, AT_LLC_SIZE, M_WAIT);
225 * FreeBSD doesn't count the LLC len in
226 * ifp->obytes, so they increment a length
227 * field here. We don't do this.
229 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
230 llc.llc_control = LLC_UI;
231 bcopy(at_org_code, llc.llc_snap_org_code,
232 sizeof(at_org_code));
233 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
234 bcopy(&llc, mtod(m, caddr_t), AT_LLC_SIZE);
235 etype = htons(m->m_pkthdr.len);
237 etype = htons(ETHERTYPE_AT);
240 #endif /* NETATALK */
245 register struct sockaddr_dl *sdl;
247 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
248 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
249 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
251 error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
252 (char *)edst, &snpalen);
254 goto bad; /* Not Resolved */
256 /* If broadcasting on a simplex interface, loopback a copy */
258 m->m_flags |= (M_BCAST|M_MCAST);
259 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
260 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
261 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
263 eh = mtod(mcopy, struct ether_header *);
265 (caddr_t)eh->ether_dhost, sizeof (edst));
266 bcopy((caddr_t)ac->ac_enaddr,
267 (caddr_t)eh->ether_shost, sizeof (edst));
270 M_PREPEND(m, 3, M_DONTWAIT);
273 etype = htons(m->m_pkthdr.len);
274 l = mtod(m, struct llc *);
275 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
276 l->llc_control = LLC_UI;
278 if (argo_debug[D_ETHER]) {
280 printf("unoutput: sending pkt to: ");
282 printf("%x ", edst[i] & 0xff);
290 register struct sockaddr_dl *sdl =
291 (struct sockaddr_dl *) rt -> rt_gateway;
293 if (sdl && sdl->sdl_family == AF_LINK
294 && sdl->sdl_alen > 0) {
295 bcopy(LLADDR(sdl), (char *)edst,
297 } else goto bad; /* Not a link interface ? Funny ... */
298 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
299 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
300 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
302 eh = mtod(mcopy, struct ether_header *);
304 (caddr_t)eh->ether_dhost, sizeof (edst));
305 bcopy((caddr_t)ac->ac_enaddr,
306 (caddr_t)eh->ether_shost, sizeof (edst));
309 etype = htons(m->m_pkthdr.len);
313 register struct llc *l = mtod(m, struct llc *);
315 printf("ether_output: sending LLC2 pkt to: ");
317 printf("%x ", edst[i] & 0xff);
318 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
319 m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
320 l->llc_control & 0xff);
323 #endif /* LLC_DEBUG */
327 eh = (struct ether_header *)dst->sa_data;
328 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
329 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc));
330 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
331 etype = eh->ether_type;
335 printf("%s: can't handle af%d\n", ifp->if_xname,
337 senderr(EAFNOSUPPORT);
341 (void) looutput(ifp, mcopy, dst, rt);
344 * Add local net header. If no space in first mbuf,
347 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
350 eh = mtod(m, struct ether_header *);
351 bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type, sizeof(eh->ether_type));
352 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
353 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
357 * Queue message on interface, and start output if interface
360 if (IF_QFULL(&ifp->if_snd)) {
361 IF_DROP(&ifp->if_snd);
365 ifp->if_obytes += m->m_pkthdr.len;
366 IF_ENQUEUE(&ifp->if_snd, m);
367 if (m->m_flags & M_MCAST)
369 if ((ifp->if_flags & IFF_OACTIVE) == 0)
370 (*ifp->if_start)(ifp);