1 /*
2 * $Id: ether_mod-2.4.c,v 1.2 1999/10/27 01:37:26 route Exp $
3 *
4 * libnet
5 * OpenBSD 2.4 ether_mod.c - lkm replacement for ether_output
6 *
7 * Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
8 *
9 * Original code and idea 1997 Thomas Ptacek <tqbf@pobox.com>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
20 *
21 *
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
32 * SUCH DAMAGE.
33 *
34 */
35
36 #define INET
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.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>
48
49 #include <machine/cpu.h>
50
51 #include <net/if.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>
57
58 #include <netinet/in.h>
59 #ifdef INET
60 #include <netinet/in_var.h>
61 #endif
62 #include <netinet/if_ether.h>
63
64 #ifdef NS
65 #include <netns/ns.h>
66 #include <netns/ns_if.h>
67 #endif
68
69 #ifdef IPX
70 #include <netipx/ipx.h>
71 #include <netipx/ipx_if.h>
72 #endif
73
74 #ifdef ISO
75 #include <netiso/argo_debug.h>
76 #include <netiso/iso.h>
77 #include <netiso/iso_var.h>
78 #include <netiso/iso_snpac.h>
79 #endif
80
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>
86
87 #ifdef NETATALK
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
91
92 #define llc_snap_org_code llc_un.type_snap.org_code
93 #define llc_snap_ether_type llc_un.type_snap.ether_type
94
95 extern u_char at_org_code[ 3 ];
96 extern u_char aarp_org_code[ 3 ];
97 #endif /* NETATALK */
98
99 #if defined(CCITT)
100 #include <sys/socketvar.h>
101 #endif
102
103 #define senderr(e) { error = (e); goto bad;}
104
105
106 int ether_output_spoof(register struct ifnet *, struct mbuf *,
107 struct sockaddr *, struct rtentry *);
108
109 /*
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.
113 */
114 int
ether_output_spoof(ifp,m0,dst,rt0)115 ether_output_spoof(ifp, m0, dst, rt0)
116 register struct ifnet *ifp;
117 struct mbuf *m0;
118 struct sockaddr *dst;
119 struct rtentry *rt0;
120 {
121 u_int16_t etype;
122 int s, error = 0;
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;
129
130 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
131 senderr(ENETDOWN);
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)
136 rt->rt_refcnt--;
137 else
138 senderr(EHOSTUNREACH);
139 }
140 if (rt->rt_flags & RTF_GATEWAY) {
141 if (rt->rt_gwroute == 0)
142 goto lookup;
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);
148 }
149 }
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);
154 }
155 switch (dst->sa_family) {
156
157 #ifdef INET
158 case AF_INET:
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));
166 break;
167 #endif
168 #ifdef NS
169 case AF_NS:
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);
178 break;
179 #endif
180 #ifdef IPX
181 case AF_IPX:
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);
190 break;
191 #endif
192 #ifdef NETATALK
193 case AF_APPLETALK: {
194 struct at_ifaddr *aa;
195
196 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
197 #ifdef NETATALKDEBUG
198 extern char *prsockaddr(struct sockaddr *);
199 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
200 #endif /* NETATALKDEBUG */
201 return (0);
202 }
203
204 /*
205 * ifaddr is the first thing in at_ifaddr
206 */
207 aa = (struct at_ifaddr *)at_ifawithnet(
208 (struct sockaddr_at *)dst,
209 ifp->if_addrlist.tqh_first);
210 if (aa == 0)
211 goto bad;
212
213 /*
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.
218 */
219 if ( aa->aa_flags & AFA_PHASE2 ) {
220 struct llc llc;
221
222 /* XXX Really this should use netisr too */
223 M_PREPEND(m, AT_LLC_SIZE, M_WAIT);
224 /*
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.
228 */
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);
236 } else {
237 etype = htons(ETHERTYPE_AT);
238 }
239 } break;
240 #endif /* NETATALK */
241 #ifdef ISO
242 case AF_ISO: {
243 int snpalen;
244 struct llc *l;
245 register struct sockaddr_dl *sdl;
246
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));
250 } else {
251 error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
252 (char *)edst, &snpalen);
253 if (error)
254 goto bad; /* Not Resolved */
255 }
256 /* If broadcasting on a simplex interface, loopback a copy */
257 if (*edst & 1)
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_DONETWAIT);
262 if (mcopy) {
263 eh = mtod(mcopy, struct ether_header *);
264 bcopy((caddr_t)edst,
265 (caddr_t)eh->ether_dhost, sizeof (edst));
266 bcopy((caddr_t)ac->ac_enaddr,
267 (caddr_t)eh->ether_shost, sizeof (edst));
268 }
269 }
270 M_PREPEND(m, 3, M_DONETWAIT);
271 if (m == NULL)
272 return (0);
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;
277 #ifdef ARGO_DEBUG
278 if (argo_debug[D_ETHER]) {
279 int i;
280 printf("unoutput: sending pkt to: ");
281 for (i=0; i<6; i++)
282 printf("%x ", edst[i] & 0xff);
283 printf("\n");
284 }
285 #endif
286 } break;
287 #endif /* ISO */
288 /* case AF_NSAP: */
289 case AF_CCITT: {
290 register struct sockaddr_dl *sdl =
291 (struct sockaddr_dl *) rt -> rt_gateway;
292
293 if (sdl && sdl->sdl_family == AF_LINK
294 && sdl->sdl_alen > 0) {
295 bcopy(LLADDR(sdl), (char *)edst,
296 sizeof(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_DONETWAIT);
301 if (mcopy) {
302 eh = mtod(mcopy, struct ether_header *);
303 bcopy((caddr_t)edst,
304 (caddr_t)eh->ether_dhost, sizeof (edst));
305 bcopy((caddr_t)ac->ac_enaddr,
306 (caddr_t)eh->ether_shost, sizeof (edst));
307 }
308 }
309 etype = htons(m->m_pkthdr.len);
310 #ifdef LLC_DEBUG
311 {
312 int i;
313 register struct llc *l = mtod(m, struct llc *);
314
315 printf("ether_output: sending LLC2 pkt to: ");
316 for (i=0; i<6; i++)
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);
321
322 }
323 #endif /* LLC_DEBUG */
324 } break;
325
326 case AF_UNSPEC:
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;
332 break;
333
334 default:
335 printf("%s: can't handle af%d\n", ifp->if_xname,
336 dst->sa_family);
337 senderr(EAFNOSUPPORT);
338 }
339
340 if (mcopy)
341 (void) looutput(ifp, mcopy, dst, rt);
342
343 /*
344 * Add local net header. If no space in first mbuf,
345 * allocate another.
346 */
347 M_PREPEND(m, sizeof (struct ether_header), M_DONETWAIT);
348 if (m == 0)
349 senderr(ENOBUFS);
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));
354
355 s = splimp();
356 /*
357 * Queue message on interface, and start output if interface
358 * not yet active.
359 */
360 if (IF_QFULL(&ifp->if_snd)) {
361 IF_DROP(&ifp->if_snd);
362 splx(s);
363 senderr(ENOBUFS);
364 }
365 ifp->if_obytes += m->m_pkthdr.len;
366 IF_ENQUEUE(&ifp->if_snd, m);
367 if (m->m_flags & M_MCAST)
368 ifp->if_omcasts++;
369 if ((ifp->if_flags & IFF_OACTIVE) == 0)
370 (*ifp->if_start)(ifp);
371 splx(s);
372 return (error);
373
374 bad:
375 if (m)
376 m_freem(m);
377 return (error);
378 }
379