source: rtems/cpukit/libnetworking/netinet/if_ether.c @ 39bad7e8

4.104.115
Last change on this file since 39bad7e8 was 404b1fb4, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/22/08 at 17:47:57

Add missing initializers.

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