source: rtems/cpukit/libnetworking/netinet/if_ether.c @ c301570

4.104.114.84.95
Last change on this file since c301570 was c301570, checked in by Ralf Corsepius <ralf.corsepius@…>, on 05/10/07 at 05:12:54

Include <rtems/bsd/sys/queue.h> instead of <sys/queue.h>.

  • Property mode set to 100644
File size: 18.4 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 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_ether.c  8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.136 2005/03/13 11:23:22 glebius Exp $
31 */
32 
33/*
34 * $Id$
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 *      add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include "opt_inet.h"
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <rtems/bsd/sys/queue.h>
48#include <sys/sysctl.h>
49#include <sys/systm.h>
50#include <sys/mbuf.h>
51#include <sys/malloc.h>
52#include <sys/socket.h>
53#include <sys/syslog.h>
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_types.h>
58#include <net/route.h>
59#include <net/netisr.h>
60
61#include <netinet/in.h>
62#include <netinet/in_var.h>
63#include <netinet/if_ether.h>
64
65#define SIN(s) ((struct sockaddr_in *)s)
66#define SDL(s) ((struct sockaddr_dl *)s)
67
68SYSCTL_DECL(_net_link_ether);
69SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
70
71/* timer values */
72static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
73static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
74static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
75
76SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
77           &arpt_prune, 0, "");
78SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
79           &arpt_keep, 0, "");
80SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
81           &arpt_down, 0, "");
82
83#define rt_expire rt_rmx.rmx_expire
84
85struct llinfo_arp {
86        LIST_ENTRY(llinfo_arp) la_le;
87        struct  rtentry *la_rt;
88        struct  mbuf *la_hold;          /* last packet until resolved/timeout */
89        long    la_asked;               /* last time we QUERIED for this addr */
90#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
91};
92
93static  LIST_HEAD(, llinfo_arp) llinfo_arp;
94
95struct  ifqueue arpintrq = {0, 0, 0, 50};
96static int      arp_inuse, arp_allocated;
97
98static int      arp_maxtries = 5;
99static int      useloopback = 1; /* use loopback interface for local traffic */
100static int      arp_proxyall = 0;
101
102SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
103           &arp_maxtries, 0, "");
104SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
105           &useloopback, 0, "");
106SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
107           &arp_proxyall, 0, "");
108
109static void     arp_rtrequest(int, struct rtentry *, struct sockaddr *);
110static void     arprequest(struct arpcom *, u_long *, u_long *, u_char *);
111void    arpintr(void);
112static void     arptfree(struct llinfo_arp *);
113static void     arptimer(void *);
114static struct llinfo_arp
115                *arplookup(u_long, int, int);
116#ifdef INET
117static void     in_arpinput(struct mbuf *);
118#endif
119
120/*
121 * Timeout routine.  Age arp_tab entries periodically.
122 */
123/* ARGSUSED */
124static void
125arptimer(ignored_arg)
126        void *ignored_arg;
127{
128        int s = splnet();
129        struct llinfo_arp *la, *ola;
130
131        la = llinfo_arp.lh_first;
132        timeout(arptimer, (caddr_t)0, arpt_prune * hz);
133        while ((ola = la) != 0) {
134                register struct rtentry *rt = la->la_rt;
135                la = la->la_le.le_next;
136                if (rt->rt_expire && rt->rt_expire <= rtems_bsdnet_seconds_since_boot())
137                        arptfree(ola); /* timer has expired, clear */
138        }
139        splx(s);
140}
141
142/*
143 * Parallel to llc_rtrequest.
144 */
145static void
146arp_rtrequest(req, rt, sa)
147        int req;
148        struct rtentry *rt;
149        struct sockaddr *sa;
150{
151        struct sockaddr *gate;
152        struct llinfo_arp *la;
153        static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
154        static int arpinit_done;
155
156        if (!arpinit_done) {
157                arpinit_done = 1;
158                LIST_INIT(&llinfo_arp);
159                timeout(arptimer, (caddr_t)0, hz);
160        }
161        if (rt->rt_flags & RTF_GATEWAY)
162                return;
163        gate = rt->rt_gateway;
164        la = (struct llinfo_arp *)rt->rt_llinfo;
165        switch (req) {
166
167        case RTM_ADD:
168                /*
169                 * XXX: If this is a manually added route to interface
170                 * such as older version of routed or gated might provide,
171                 * restore cloning bit.
172                 */
173                if ((rt->rt_flags & RTF_HOST) == 0 &&
174                    rt_mask(rt) != NULL &&
175                    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
176                        rt->rt_flags |= RTF_CLONING;
177                if (rt->rt_flags & RTF_CLONING) {
178                        /*
179                         * Case 1: This route should come from a route to iface.
180                         */
181                        rt_setgate(rt, rt_key(rt),
182                                        (struct sockaddr *)&null_sdl);
183                        gate = rt->rt_gateway;
184                        SDL(gate)->sdl_type = rt->rt_ifp->if_type;
185                        SDL(gate)->sdl_index = rt->rt_ifp->if_index;
186                        rt->rt_expire = rtems_bsdnet_seconds_since_boot();
187                        break;
188                }
189                /* Announce a new entry if requested. */
190                if (rt->rt_flags & RTF_ANNOUNCE)
191                        arprequest((struct arpcom *)rt->rt_ifp,
192                            &SIN(rt_key(rt))->sin_addr.s_addr,
193                            &SIN(rt_key(rt))->sin_addr.s_addr,
194                            (u_char *)LLADDR(SDL(gate)));
195                /*FALLTHROUGH*/
196        case RTM_RESOLVE:
197                if (gate->sa_family != AF_LINK ||
198                    gate->sa_len < sizeof(null_sdl)) {
199                        log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
200                        break;
201                }
202                SDL(gate)->sdl_type = rt->rt_ifp->if_type;
203                SDL(gate)->sdl_index = rt->rt_ifp->if_index;
204                if (la != 0)
205                        break; /* This happens on a route change */
206                /*
207                 * Case 2:  This route may come from cloning, or a manual route
208                 * add with a LL address.
209                 */
210                R_Malloc(la, struct llinfo_arp *, sizeof(*la));
211                rt->rt_llinfo = (caddr_t)la;
212                if (la == 0) {
213                        log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
214                        break;
215                }
216                arp_inuse++;
217                arp_allocated++;
218                Bzero(la, sizeof(*la));
219                la->la_rt = rt;
220                rt->rt_flags |= RTF_LLINFO;
221                LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
222
223                /*
224                 * This keeps the multicast addresses from showing up
225                 * in `arp -a' listings as unresolved.  It's not actually
226                 * functional.  Then the same for broadcast.
227                 */
228                if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
229                        ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
230                                               LLADDR(SDL(gate)));
231                        SDL(gate)->sdl_alen = 6;
232                        rt->rt_expire = 0;
233                }
234                if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
235                        memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
236                        SDL(gate)->sdl_alen = 6;
237                        rt->rt_expire = 0;
238                }
239
240                if (SIN(rt_key(rt))->sin_addr.s_addr ==
241                    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
242                    /*
243                     * This test used to be
244                     *  if (loif.if_flags & IFF_UP)
245                     * It allowed local traffic to be forced
246                     * through the hardware by configuring the loopback down.
247                     * However, it causes problems during network configuration
248                     * for boards that can't receive packets they send.
249                     * It is now necessary to clear "useloopback" and remove
250                     * the route to force traffic out to the hardware.
251                     */
252                        rt->rt_expire = 0;
253                        Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
254                                LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
255                        if (useloopback)
256                                rt->rt_ifp = loif;
257
258                }
259                break;
260
261        case RTM_DELETE:
262                if (la == 0)
263                        break;
264                arp_inuse--;
265                LIST_REMOVE(la, la_le);
266                rt->rt_llinfo = 0;
267                rt->rt_flags &= ~RTF_LLINFO;
268                if (la->la_hold)
269                        m_freem(la->la_hold);
270                Free((caddr_t)la);
271        }
272}
273
274/*
275 * Broadcast an ARP request. Caller specifies:
276 *      - arp header source ip address
277 *      - arp header target ip address
278 *      - arp header source ethernet address
279 */
280static void
281arprequest(ac, sip, tip, enaddr)
282        struct arpcom *ac;
283        u_long *sip, *tip;
284        u_char *enaddr;
285{
286        struct mbuf *m;
287        struct ether_header *eh;
288        struct ether_arp *ea;
289        struct sockaddr sa;
290
291        if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
292                return;
293        m->m_len = sizeof(*ea);
294        m->m_pkthdr.len = sizeof(*ea);
295        MH_ALIGN(m, sizeof(*ea));
296        ea = mtod(m, struct ether_arp *);
297        eh = (struct ether_header *)sa.sa_data;
298        bzero((caddr_t)ea, sizeof (*ea));
299        (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
300        eh->ether_type = htons(ETHERTYPE_ARP);  /* if_output will not swap */
301        ea->arp_hrd = htons(ARPHRD_ETHER);
302        ea->arp_pro = htons(ETHERTYPE_IP);
303        ea->arp_hln = sizeof(ea->arp_sha);      /* hardware address length */
304        ea->arp_pln = sizeof(ea->arp_spa);      /* protocol address length */
305        ea->arp_op = htons(ARPOP_REQUEST);
306        (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
307        (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
308        (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
309        sa.sa_family = AF_UNSPEC;
310        sa.sa_len = sizeof(sa);
311        (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
312}
313
314/*
315 * Resolve an IP address into an ethernet address.  If success,
316 * desten is filled in.  If there is no entry in arptab,
317 * set one up and broadcast a request for the IP address.
318 * Hold onto this mbuf and resend it once the address
319 * is finally resolved.  A return value of 1 indicates
320 * that desten has been filled in and the packet should be sent
321 * normally; a 0 return indicates that the packet has been
322 * taken over here, either now or for later transmission.
323 */
324int
325arpresolve(ac, rt, m, dst, desten, rt0)
326        struct arpcom *ac;
327        struct rtentry *rt;
328        struct mbuf *m;
329        struct sockaddr *dst;
330        u_char *desten;
331        struct rtentry *rt0;
332{
333        struct llinfo_arp *la = 0;
334        struct sockaddr_dl *sdl;
335
336        if (m->m_flags & M_BCAST) {     /* broadcast */
337                (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
338                return (1);
339        }
340        if (m->m_flags & M_MCAST) {     /* multicast */
341                ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
342                return(1);
343        }
344        if (rt)
345                la = (struct llinfo_arp *)rt->rt_llinfo;
346        else {
347                la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
348                if (la)
349                        rt = la->la_rt;
350        }
351        if (la == 0 || rt == 0) {
352                log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s\n",
353                        inet_ntoa(SIN(dst)->sin_addr));
354                m_freem(m);
355                return (0);
356        }
357        sdl = SDL(rt->rt_gateway);
358        /*
359         * Check the address family and length is valid, the address
360         * is resolved; otherwise, try to resolve.
361         */
362        if ((rt->rt_expire == 0 || rt->rt_expire > rtems_bsdnet_seconds_since_boot()) &&
363            sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
364                bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
365                return 1;
366        }
367        /*
368         * There is an arptab entry, but no ethernet address
369         * response yet.  Replace the held mbuf with this
370         * latest one.
371         */
372        if (la->la_hold)
373                m_freem(la->la_hold);
374        la->la_hold = m;
375        if (rt->rt_expire) {
376                rt->rt_flags &= ~RTF_REJECT;
377                if (la->la_asked == 0 || rt->rt_expire != rtems_bsdnet_seconds_since_boot()) {
378                        rt->rt_expire = rtems_bsdnet_seconds_since_boot();
379                        if (la->la_asked++ < arp_maxtries)
380                            arprequest(ac,
381                                &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
382                                &(SIN(dst)->sin_addr.s_addr),
383                                ac->ac_enaddr);
384                        else {
385                                rt->rt_flags |= RTF_REJECT;
386                                rt->rt_expire += arpt_down;
387                                la->la_asked = 0;
388                        }
389
390                }
391        }
392        return (0);
393}
394
395/*
396 * Common length and type checks are done here,
397 * then the protocol-specific routine is called.
398 */
399void
400arpintr(void)
401{
402        struct mbuf *m;
403        struct arphdr *ar;
404        int s;
405
406        while (arpintrq.ifq_head) {
407                s = splimp();
408                IF_DEQUEUE(&arpintrq, m);
409                splx(s);
410                if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
411                        panic("arpintr");
412                if (m->m_len >= sizeof(struct arphdr) &&
413                    (ar = mtod(m, struct arphdr *)) &&
414                    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
415                    m->m_len >=
416                      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
417
418                            switch (ntohs(ar->ar_pro)) {
419
420                            case ETHERTYPE_IP:
421                                    in_arpinput(m);
422                                    continue;
423                            }
424                m_freem(m);
425        }
426}
427
428NETISR_SET(NETISR_ARP, arpintr);
429
430/*
431 * ARP for Internet protocols on 10 Mb/s Ethernet.
432 * Algorithm is that given in RFC 826.
433 * In addition, a sanity check is performed on the sender
434 * protocol address, to catch impersonators.
435 * We no longer handle negotiations for use of trailer protocol:
436 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
437 * along with IP replies if we wanted trailers sent to us,
438 * and also sent them in response to IP replies.
439 * This allowed either end to announce the desire to receive
440 * trailer packets.
441 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
442 * but formerly didn't normally send requests.
443 */
444static void
445in_arpinput(m)
446        struct mbuf *m;
447{
448        struct ether_arp *ea;
449        struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
450        struct ether_header *eh;
451        struct llinfo_arp *la = 0;
452        struct rtentry *rt;
453        struct in_ifaddr *ia;
454        struct in_ifaddr *maybe_ia = 0;
455        struct sockaddr_dl *sdl;
456        struct sockaddr sa;
457        struct in_addr isaddr, itaddr, myaddr;
458        int op;
459
460        ea = mtod(m, struct ether_arp *);
461        op = ntohs(ea->arp_op);
462        (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
463        (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
464        for (ia = in_ifaddr; ia; ia = ia->ia_next)
465                if (ia->ia_ifp == &ac->ac_if) {
466                        maybe_ia = ia;
467                        if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
468                             (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
469                                break;
470                }
471        if (maybe_ia == 0) {
472                m_freem(m);
473                return;
474        }
475        myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
476        if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
477            sizeof (ea->arp_sha))) {
478                m_freem(m);     /* it's from me, ignore it. */
479                return;
480        }
481        if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
482            sizeof (ea->arp_sha))) {
483                log(LOG_ERR,
484                    "arp: ether address is broadcast for IP address %s!\n",
485                    inet_ntoa(isaddr));
486                m_freem(m);
487                return;
488        }
489        if (isaddr.s_addr == myaddr.s_addr) {
490                log(LOG_ERR,
491                   "arp: %6D is using my IP address %s!\n",
492                   ea->arp_sha, ":", inet_ntoa(isaddr));
493                itaddr = myaddr;
494                goto reply;
495        }
496        la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
497        if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
498                if (sdl->sdl_alen &&
499                    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
500                        log(LOG_INFO, "arp: %s moved from %6D to %6D\n",
501                            inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
502                            ea->arp_sha, ":");
503                (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
504                sdl->sdl_alen = sizeof(ea->arp_sha);
505                if (rt->rt_expire)
506                        rt->rt_expire = rtems_bsdnet_seconds_since_boot() + arpt_keep;
507                rt->rt_flags &= ~RTF_REJECT;
508                la->la_asked = 0;
509                if (la->la_hold) {
510                        (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
511                                rt_key(rt), rt);
512                        la->la_hold = 0;
513                }
514        }
515reply:
516        if (op != ARPOP_REQUEST) {
517                m_freem(m);
518                return;
519        }
520        if (itaddr.s_addr == myaddr.s_addr) {
521                /* I am the target */
522                (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
523                (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
524        } else {
525                la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
526                if (la == NULL) {
527                        struct sockaddr_in sin;
528
529                        if (!arp_proxyall) {
530                                m_freem(m);
531                                return;
532                        }
533
534                        bzero(&sin, sizeof sin);
535                        sin.sin_family = AF_INET;
536                        sin.sin_len = sizeof sin;
537                        sin.sin_addr = itaddr;
538
539                        rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
540                        if (!rt) {
541                                m_freem(m);
542                                return;
543                        }
544                        /*
545                         * Don't send proxies for nodes on the same interface
546                         * as this one came out of, or we'll get into a fight
547                         * over who claims what Ether address.
548                         */
549                        if (rt->rt_ifp == &ac->ac_if) {
550                                rtfree(rt);
551                                m_freem(m);
552                                return;
553                        }
554                        (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
555                        (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
556                        rtfree(rt);
557#ifdef DEBUG_PROXY
558                        printf("arp: proxying for %s\n",
559                               inet_ntoa(itaddr));
560#endif
561                } else {
562                        rt = la->la_rt;
563                        (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
564                        sdl = SDL(rt->rt_gateway);
565                        (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
566                }
567        }
568
569        (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
570        (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
571        ea->arp_op = htons(ARPOP_REPLY);
572        ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
573        eh = (struct ether_header *)sa.sa_data;
574        (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
575        eh->ether_type = htons(ETHERTYPE_ARP);
576        sa.sa_family = AF_UNSPEC;
577        sa.sa_len = sizeof(sa);
578        (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
579        return;
580}
581
582/*
583 * Free an arp entry.
584 */
585static void
586arptfree(la)
587        struct llinfo_arp *la;
588{
589        struct rtentry *rt = la->la_rt;
590        struct sockaddr_dl *sdl;
591        if (rt == 0)
592                panic("arptfree");
593        if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
594            sdl->sdl_family == AF_LINK) {
595                sdl->sdl_alen = 0;
596                la->la_asked = 0;
597                rt->rt_flags &= ~RTF_REJECT;
598                return;
599        }
600        rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
601                        0, (struct rtentry **)0);
602}
603/*
604 * Lookup or enter a new address in arptab.
605 */
606static struct llinfo_arp *
607arplookup(addr, create, proxy)
608        u_long addr;
609        int create, proxy;
610{
611        struct rtentry *rt;
612        static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
613        const char *why = 0;
614
615        sin.sin_len = sizeof(sin);
616        sin.sin_family = AF_INET;
617        sin.sin_addr.s_addr = addr;
618        sin.sin_other = proxy ? SIN_PROXY : 0;
619        rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
620        if (rt == 0)
621                return (0);
622        rt->rt_refcnt--;
623
624        if (rt->rt_flags & RTF_GATEWAY)
625                why = "host is not on local network";
626        else if ((rt->rt_flags & RTF_LLINFO) == 0)
627                why = "could not allocate llinfo";
628        else if (rt->rt_gateway->sa_family != AF_LINK)
629                why = "gateway route is not ours";
630
631        if (why && create) {
632                log(LOG_DEBUG, "arplookup %s failed: %s\n",
633                    inet_ntoa(sin.sin_addr), why);
634                return 0;
635        } else if (why) {
636                return 0;
637        }
638        return ((struct llinfo_arp *)rt->rt_llinfo);
639}
640
641void
642arp_ifinit(ac, ifa)
643        struct arpcom *ac;
644        struct ifaddr *ifa;
645{
646        if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
647                arprequest(ac, &(IA_SIN(ifa)->sin_addr.s_addr),
648                               &(IA_SIN(ifa)->sin_addr.s_addr), ac->ac_enaddr);
649        ifa->ifa_rtrequest = arp_rtrequest;
650        ifa->ifa_flags |= RTF_CLONING;
651}
Note: See TracBrowser for help on using the repository browser.