/* $NetBSD: mkarp.c,v 1.12 2017/04/12 16:57:14 roy Exp $ */
/*
* Copyright (c) 1984, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Sun Microsystems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1984, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
static int
is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
{
if (sdl->sdl_family != AF_LINK ||
(rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
return 0;
switch (sdl->sdl_type) {
case IFT_ETHER:
case IFT_FDDI:
case IFT_ISO88023:
case IFT_ISO88024:
case IFT_ISO88025:
case IFT_ARCNET:
return 1;
default:
return 0;
}
}
/*
* We need to close and open the socket to prevent routing updates
* building up such that when we send our message we never see our
* reply (and hang)
*/
s = socket(PF_ROUTE, SOCK_RAW, 0);
if (s < 0)
err(1, "socket");
#ifdef RO_MSGFILTER
if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
&msgfilter, sizeof(msgfilter)) < 0)
warn("RO_MSGFILTER");
#endif
rtm->rtm_flags = 0;
if (rtmsg(RTM_GET, s, rtm, &sin_m, &sdl_m) < 0) {
#if 0
warn("%s", host);
#endif
close(s);
return (1);
}
sin = (struct sockaddr_inarp *)(rtm + 1);
sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
if (is_llinfo(sdl, rtm->rtm_flags))
goto overwrite;
#if 0
(void)printf("set: can only proxy for %s\n", host);
#endif
close(s);
return (1);
}
overwrite:
if (sdl->sdl_family != AF_LINK) {
#if 0
(void)printf("cannot intuit interface index and type for %s\n",
host);
#endif
close(s);
return (1);
}
sdl_m.sdl_type = sdl->sdl_type;
sdl_m.sdl_index = sdl->sdl_index;
result = rtmsg(RTM_ADD, s, rtm, &sin_m, &sdl_m);
close(s);
return (result);
}
int
rtmsg(int cmd, int s, struct rt_msghdr *rtm, struct sockaddr_inarp *sin_m,
struct sockaddr_dl *sdl_m)
{
static int seq;
int rlen;
char *cp;
int l;
pid_t pid;
struct timeval tv;