2 * $Id: ether_mod.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * FreeBSD ether_mod.c - lkm replacement for ether_output
7 * Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
8 * Original code and idea 1997 Thomas Ptacek <tqbf@pobox.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/errno.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 #include <net/if_llc.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/ethernet.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
63 #include <netipx/ipx.h>
64 #include <netipx/ipx_if.h>
69 #include <netns/ns_if.h>
71 int ether_outputdebug = 0;
72 int ether_inputdebug = 0;
76 #include <netiso/argo_debug.h>
77 #include <netiso/iso.h>
78 #include <netiso/iso_var.h>
79 #include <netiso/iso_snpac.h>
83 #include <netccitt/dll.h>
84 #include <netccitt/llc_var.h>
87 #if defined(LLC) && defined(CCITT)
88 extern struct ifqueue pkintrq;
92 #include <netatalk/at.h>
93 #include <netatalk/at_var.h>
94 #include <netatalk/at_extern.h>
96 #define llc_snap_org_code llc_un.type_snap.org_code
97 #define llc_snap_ether_type llc_un.type_snap.ether_type
99 extern u_char at_org_code[ 3 ];
100 extern u_char aarp_org_code[ 3 ];
103 #define senderr(e) { error = (e); goto bad;}
105 int ether_output_spoof(register struct ifnet *, struct mbuf *,
106 struct sockaddr *, struct rtentry *);
109 ether_output_spoof(ifp, m0, dst, rt0)
110 register struct ifnet *ifp;
112 struct sockaddr *dst;
117 u_char *cp, edst[6], esrc[6];
118 register struct mbuf *m2, *m = m0;
119 register struct rtentry *rt;
120 struct mbuf *mcopy = (struct mbuf *)0;
121 register struct ether_header *eh;
122 int off, len = m->m_pkthdr.len;
123 struct arpcom *ac = (struct arpcom *)ifp;
124 register struct ifqueue *inq;
126 struct at_ifaddr *aa;
129 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
133 if ((rt->rt_flags & RTF_UP) == 0) {
134 rt0 = rt = rtalloc1(dst, 1, 0UL);
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,
147 if ((rt = rt->rt_gwroute) == 0)
148 senderr(EHOSTUNREACH);
151 if (rt->rt_flags & RTF_REJECT)
152 if (rt->rt_rmx.rmx_expire == 0 /*||
153 time.tv_sec < rt->rt_rmx.rmx_expire*/)
154 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
156 switch (dst->sa_family) {
160 if (!arpresolve(ac, rt, m, dst, edst, rt0))
161 return (0); /* if not yet resolved */
162 /* If broadcasting on a simplex interface, loopback a copy */
163 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
164 mcopy = m_copy(m, 0, (int)M_COPYALL);
165 off = m->m_pkthdr.len - m->m_len;
166 type = htons(ETHERTYPE_IP);
174 type = htons(ETHERTYPE_IPX);
175 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
176 (caddr_t)edst, sizeof (edst));
178 for(ia = ifp->if_addrhead.tqh_first; ia != 0;
179 ia = ia->ifa_link.tqe_next) {
180 if(ia->ifa_addr->sa_family == AF_IPX &&
182 (caddr_t)&((struct ipx_ifaddr *)ia)->ia_addr.sipx_addr.x_host,
184 return (/*looutput(ifp, m, dst, rt)*/1);
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);
195 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
197 extern char *prsockaddr(struct sockaddr *);
198 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
203 * ifaddr is the first thing in at_ifaddr
205 if ((aa = (struct at_ifaddr *)at_ifawithnet(
206 (struct sockaddr_at *)dst, &ifp->if_addrhead))
211 * In the phase 2 case, we need to prepend an mbuf for the llc header.
212 * Since we must preserve the value of m, which is passed to us by
213 * value, we m_copy() the first mbuf, and use it for our llc header.
215 if ( aa->aa_flags & AFA_PHASE2 ) {
218 M_PREPEND(m, sizeof(struct llc), M_WAIT);
219 len += sizeof(struct llc);
220 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
221 llc.llc_control = LLC_UI;
222 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
223 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
224 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
225 type = htons(m->m_pkthdr.len);
227 type = htons(ETHERTYPE_AT);
235 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
238 case 0x0: /* Novell 802.3 */
239 type = htons( m->m_pkthdr.len);
241 case 0xe0e0: /* Novell 802.2 and Token-Ring */
242 M_PREPEND(m, 3, M_WAIT);
243 type = htons( m->m_pkthdr.len);
244 cp = mtod(m, u_char *);
250 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
251 (caddr_t)edst, sizeof (edst));
252 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
253 m->m_pkthdr.rcvif = ifp;
254 schednetisr(NETISR_NS);
265 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
266 m2 = m_copy(m, 0, (int)M_COPYALL);
267 m2->m_pkthdr.rcvif = ifp;
268 schednetisr(NETISR_NS);
278 /* If broadcasting on a simplex interface, loopback a copy */
279 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)){
280 mcopy = m_copy(m, 0, (int)M_COPYALL);
288 register struct sockaddr_dl *sdl;
290 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
291 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
292 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
294 iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
295 (char *)edst, &snpalen))
296 goto bad; /* Not Resolved */
297 /* If broadcasting on a simplex interface, loopback a copy */
299 m->m_flags |= (M_BCAST|M_MCAST);
300 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
301 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
302 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
304 eh = mtod(mcopy, struct ether_header *);
306 (caddr_t)eh->ether_dhost, sizeof (edst));
307 bcopy((caddr_t)ac->ac_enaddr,
308 (caddr_t)eh->ether_shost, sizeof (edst));
311 M_PREPEND(m, 3, M_DONTWAIT);
314 type = htons(m->m_pkthdr.len);
315 l = mtod(m, struct llc *);
316 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
317 l->llc_control = LLC_UI;
321 printf("unoutput: sending pkt to: ");
323 printf("%x ", edst[i] & 0xff);
331 register struct sockaddr_dl *sdl =
332 (struct sockaddr_dl *) rt -> rt_gateway;
334 if (sdl && sdl->sdl_family == AF_LINK
335 && sdl->sdl_alen > 0) {
336 bcopy(LLADDR(sdl), (char *)edst,
338 } else goto bad; /* Not a link interface ? Funny ... */
339 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
340 (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
341 M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
343 eh = mtod(mcopy, struct ether_header *);
345 (caddr_t)eh->ether_dhost, sizeof (edst));
346 bcopy((caddr_t)ac->ac_enaddr,
347 (caddr_t)eh->ether_shost, sizeof (edst));
350 type = htons(m->m_pkthdr.len);
354 register struct llc *l = mtod(m, struct llc *);
356 printf("ether_output: sending LLC2 pkt to: ");
358 printf("%x ", edst[i] & 0xff);
359 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
360 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
361 l->llc_control & 0xff);
364 #endif /* LLC_DEBUG */
370 eh = (struct ether_header *)dst->sa_data;
371 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
372 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
373 type = eh->ether_type;
377 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
379 senderr(EAFNOSUPPORT);
384 (void) looutput(ifp, mcopy, dst, rt);*/
386 * Add local net header. If no space in first mbuf,
389 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
392 eh = mtod(m, struct ether_header *);
393 (void)memcpy(&eh->ether_type, &type,
394 sizeof(eh->ether_type));
395 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
396 (void)memcpy(eh->ether_shost, esrc, sizeof (esrc));
399 * Queue message on interface, and start output if interface
402 if (IF_QFULL(&ifp->if_snd)) {
403 IF_DROP(&ifp->if_snd);
407 IF_ENQUEUE(&ifp->if_snd, m);
408 if ((ifp->if_flags & IFF_OACTIVE) == 0)
409 (*ifp->if_start)(ifp);
411 ifp->if_obytes += len + sizeof (struct ether_header);
412 if (m->m_flags & M_MCAST)