OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / support / bpf-lkm / FreeBSD / ether_mod.c
1 /*
2  *  $Id: ether_mod.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  FreeBSD ether_mod.c - lkm replacement for ether_output
6  *
7  *  Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
8  *  Original code and idea 1997 Thomas Ptacek <tqbf@pobox.com>
9  *  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  */
33
34 #define INET
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.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>
47
48 #include <net/if.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>
55
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #endif
60 #include <netinet/if_ether.h>
61
62 #ifdef IPX
63 #include <netipx/ipx.h>
64 #include <netipx/ipx_if.h>
65 #endif
66
67 #ifdef NS
68 #include <netns/ns.h>
69 #include <netns/ns_if.h>
70 ushort ns_nettype;
71 int ether_outputdebug = 0;
72 int ether_inputdebug = 0;
73 #endif
74
75 #ifdef ISO
76 #include <netiso/argo_debug.h>
77 #include <netiso/iso.h>
78 #include <netiso/iso_var.h>
79 #include <netiso/iso_snpac.h>
80 #endif
81
82 /*#ifdef LLC
83 #include <netccitt/dll.h>
84 #include <netccitt/llc_var.h>
85 #endif*/
86
87 #if defined(LLC) && defined(CCITT)
88 extern struct ifqueue pkintrq;
89 #endif
90
91 #ifdef NETATALK
92 #include <netatalk/at.h>
93 #include <netatalk/at_var.h>
94 #include <netatalk/at_extern.h>
95
96 #define llc_snap_org_code llc_un.type_snap.org_code
97 #define llc_snap_ether_type llc_un.type_snap.ether_type
98
99 extern u_char   at_org_code[ 3 ];
100 extern u_char   aarp_org_code[ 3 ];
101 #endif NETATALK
102
103 #define senderr(e) { error = (e); goto bad;}
104
105 int ether_output_spoof(register struct ifnet *, struct mbuf *, 
106                         struct sockaddr *, struct rtentry *);
107
108 int
109 ether_output_spoof(ifp, m0, dst, rt0)
110         register struct ifnet *ifp;
111         struct mbuf *m0;
112         struct sockaddr *dst;
113         struct rtentry *rt0;
114 {
115         short type;
116         int s, error = 0;
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;
125 #ifdef NETATALK
126         struct at_ifaddr *aa;
127 #endif NETATALK
128
129         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
130                 senderr(ENETDOWN);
131         rt = rt0;
132         if (rt) {
133                 if ((rt->rt_flags & RTF_UP) == 0) {
134                         rt0 = rt = rtalloc1(dst, 1, 0UL);
135                         if (rt0)
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                                                           0UL);
147                                 if ((rt = rt->rt_gwroute) == 0)
148                                         senderr(EHOSTUNREACH);
149                         }
150                 }
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);
155         }
156         switch (dst->sa_family) {
157
158 #ifdef INET
159         case AF_INET:
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);
167                 break;
168 #endif
169 #ifdef IPX
170         case AF_IPX:
171                 {
172                 struct ifaddr *ia;
173
174                 type = htons(ETHERTYPE_IPX);
175                 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
176                     (caddr_t)edst, sizeof (edst));
177
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 &&
181                             !bcmp((caddr_t)edst,
182                                   (caddr_t)&((struct ipx_ifaddr *)ia)->ia_addr.sipx_addr.x_host,
183                                   sizeof(edst)) )
184                                 return (/*looutput(ifp, m, dst, rt)*/1);
185                         }
186
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                 }
192 #endif
193 #ifdef NETATALK
194         case AF_APPLETALK:
195             if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
196 #ifdef NETATALKDEBUG
197                 extern char *prsockaddr(struct sockaddr *);
198                 printf("aarpresolv: failed for %s\n", prsockaddr(dst));
199 #endif NETATALKDEBUG
200                 return (0);
201             }
202             /*
203              * ifaddr is the first thing in at_ifaddr
204              */
205             if ((aa = (struct at_ifaddr *)at_ifawithnet(
206                         (struct sockaddr_at *)dst, &ifp->if_addrhead))
207                 == 0)
208                 goto bad;
209             
210             /*
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.
214              */
215             if ( aa->aa_flags & AFA_PHASE2 ) {
216                 struct llc llc;
217
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);
226             } else {
227                 type = htons(ETHERTYPE_AT);
228             }
229             break;
230 #endif NETATALK
231 #ifdef NS
232         case AF_NS:
233                 switch(ns_nettype){
234                 default:
235                 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
236                         type = 0x8137;
237                         break;
238                 case 0x0: /* Novell 802.3 */
239                         type = htons( m->m_pkthdr.len);
240                         break;
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 *);
245                         *cp++ = 0xE0;
246                         *cp++ = 0xE0;
247                         *cp++ = 0x03;
248                         break;
249                 }
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);
255                         inq = &nsintrq;
256                         s = splimp();
257                         if (IF_QFULL(inq)) {
258                                 IF_DROP(inq);
259                                 m_freem(m);
260                         } else
261                                 IF_ENQUEUE(inq, m);
262                         splx(s);
263                         return (error);
264                 }
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);
269                         inq = &nsintrq;
270                         s = splimp();
271                         if (IF_QFULL(inq)) {
272                                 IF_DROP(inq);
273                                 m_freem(m2);
274                         } else
275                                 IF_ENQUEUE(inq, m2);
276                         splx(s);
277                 }
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);
281                 }
282                 break;
283 #endif /* NS */
284 #ifdef  ISO
285         case AF_ISO: {
286                 int     snpalen;
287                 struct  llc *l;
288                 register struct sockaddr_dl *sdl;
289
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));
293                 } else if (error =
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 */
298                 if (*edst & 1)
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);
303                         if (mcopy) {
304                                 eh = mtod(mcopy, struct ether_header *);
305                                 bcopy((caddr_t)edst,
306                                       (caddr_t)eh->ether_dhost, sizeof (edst));
307                                 bcopy((caddr_t)ac->ac_enaddr,
308                                       (caddr_t)eh->ether_shost, sizeof (edst));
309                         }
310                 }
311                 M_PREPEND(m, 3, M_DONTWAIT);
312                 if (m == NULL)
313                         return (0);
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;
318                 len += 3;
319                 IFDEBUG(D_ETHER)
320                         int i;
321                         printf("unoutput: sending pkt to: ");
322                         for (i=0; i<6; i++)
323                                 printf("%x ", edst[i] & 0xff);
324                         printf("\n");
325                 ENDDEBUG
326                 } break;
327 #endif /* ISO */
328 #ifdef  LLC
329 /*      case AF_NSAP: */
330         case AF_CCITT: {
331                 register struct sockaddr_dl *sdl =
332                         (struct sockaddr_dl *) rt -> rt_gateway;
333
334                 if (sdl && sdl->sdl_family == AF_LINK
335                     && sdl->sdl_alen > 0) {
336                         bcopy(LLADDR(sdl), (char *)edst,
337                                 sizeof(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);
342                         if (mcopy) {
343                                 eh = mtod(mcopy, struct ether_header *);
344                                 bcopy((caddr_t)edst,
345                                       (caddr_t)eh->ether_dhost, sizeof (edst));
346                                 bcopy((caddr_t)ac->ac_enaddr,
347                                       (caddr_t)eh->ether_shost, sizeof (edst));
348                         }
349                 }
350                 type = htons(m->m_pkthdr.len);
351 #ifdef LLC_DEBUG
352                 {
353                         int i;
354                         register struct llc *l = mtod(m, struct llc *);
355
356                         printf("ether_output: sending LLC2 pkt to: ");
357                         for (i=0; i<6; i++)
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);
362
363                 }
364 #endif /* LLC_DEBUG */
365                 } break;
366 #endif /* LLC */
367
368         case AF_UNSPEC:
369                 /* XXX */
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;
374                 break;
375
376         default:
377                 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
378                         dst->sa_family);
379                 senderr(EAFNOSUPPORT);
380         }
381
382
383 /*      if (mcopy)
384                 (void) looutput(ifp, mcopy, dst, rt);*/
385         /*
386          * Add local net header.  If no space in first mbuf,
387          * allocate another.
388          */
389         M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
390         if (m == 0)
391                 senderr(ENOBUFS);
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));
397         s = splimp();
398         /*
399          * Queue message on interface, and start output if interface
400          * not yet active.
401          */
402         if (IF_QFULL(&ifp->if_snd)) {
403                 IF_DROP(&ifp->if_snd);
404                 splx(s);
405                 senderr(ENOBUFS);
406         }
407         IF_ENQUEUE(&ifp->if_snd, m);
408         if ((ifp->if_flags & IFF_OACTIVE) == 0)
409                 (*ifp->if_start)(ifp);
410         splx(s);
411         ifp->if_obytes += len + sizeof (struct ether_header);
412         if (m->m_flags & M_MCAST)
413                 ifp->if_omcasts++;
414         return (error);
415
416 bad:
417         if (m)
418                 m_freem(m);
419         return (error);
420 }
421