/*-1* Copyright 1994, 1995 Massachusetts Institute of Technology2*3* Permission to use, copy, modify, and distribute this software and4* its documentation for any purpose and without fee is hereby5* granted, provided that both the above copyright notice and this6* permission notice appear in all copies, that both the above7* copyright notice and this permission notice appear in all8* supporting documentation, and that the name of M.I.T. not be used9* in advertising or publicity pertaining to distribution of the10* software without specific, written prior permission. M.I.T. makes11* no representations about the suitability of this software for any12* purpose. It is provided "as is" without express or implied13* warranty.14*15* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS16* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,17* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF18* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT19* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF22* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND23* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,24* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT25* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/kernel.h>32#include <sys/sysctl.h>33#include <sys/socket.h>34#include <sys/mbuf.h>3536#include <net/if.h>37#include <net/if_var.h>38#include <net/if_private.h>39#include <net/route.h>40#include <net/route/route_ctl.h>41#include <net/route/route_var.h>42#include <net/route/nhop.h>43#include <net/vnet.h>4445#include <netinet/in.h>46#include <netinet/in_var.h>47#include <netinet/ip.h>48#include <netinet/ip_icmp.h>49#include <netinet/ip_var.h>5051static int52rib4_set_nh_pfxflags(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,53struct nhop_object *nh)54{55const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;56const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;57bool is_broadcast = false;5859if (mask == NULL) {60nhop_set_pxtype_flag(nh, NHF_HOST);61/*62* Backward compatibility:63* if the destination is broadcast,64* mark route as broadcast.65* This behavior was useful when route cloning66* was in place, so there was an explicit cloned67* route for every broadcasted address.68* Currently (2020-04) there is no kernel machinery69* to do route cloning, though someone might explicitly70* add these routes to support some cases with active-active71* load balancing. Given that, retain this support.72*/73if (in_ifnet_broadcast(addr4->sin_addr, nh->nh_ifp))74is_broadcast = true;75} else if (mask4->sin_addr.s_addr == 0)76nhop_set_pxtype_flag(nh, NHF_DEFAULT);77else78nhop_set_pxtype_flag(nh, 0);7980nhop_set_broadcast(nh, is_broadcast);8182return (0);83}8485static int86rib4_augment_nh(u_int fibnum, struct nhop_object *nh)87{88/*89* Check route MTU:90* inherit interface MTU if not set or91* check if MTU is too large.92*/93if (nh->nh_mtu == 0) {94nh->nh_mtu = nh->nh_ifp->if_mtu;95} else if (nh->nh_mtu > nh->nh_ifp->if_mtu)96nh->nh_mtu = nh->nh_ifp->if_mtu;9798/* Set nhop type to basic per-AF nhop */99if (nhop_get_type(nh) == 0) {100uint16_t nh_type;101if (nh->nh_flags & NHF_GATEWAY)102nh_type = NH_TYPE_IPV4_ETHER_NHOP;103else104nh_type = NH_TYPE_IPV4_ETHER_RSLV;105106nhop_set_type(nh, nh_type);107}108109return (0);110}111112/*113* Initialize our routing tree.114*/115struct rib_head *116in_inithead(uint32_t fibnum)117{118struct rib_head *rh;119120rh = rt_table_init(32, AF_INET, fibnum);121rh->rnh_set_nh_pfxflags = rib4_set_nh_pfxflags;122rh->rnh_augment_nh = rib4_augment_nh;123124return (rh);125}126127#ifdef VIMAGE128void129in_detachhead(struct rib_head *rh)130{131132rt_table_destroy(rh);133}134#endif135136/*137* This zaps old routes when the interface goes down or interface138* address is deleted. In the latter case, it deletes static routes139* that point to this address. If we don't do this, we may end up140* using the old address in the future. The ones we always want to141* get rid of are things like ARP entries, since the user might down142* the interface, walk over to a completely different network, and143* plug back in.144*/145struct in_ifadown_arg {146struct ifaddr *ifa;147int del;148};149150static int151in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,152void *xap)153{154struct in_ifadown_arg *ap = xap;155156if (nh->nh_ifa != ap->ifa)157return (0);158159if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)160return (0);161162return (1);163}164165void166in_ifadown(struct ifaddr *ifa, int delete)167{168struct in_ifadown_arg arg;169170KASSERT(ifa->ifa_addr->sa_family == AF_INET,171("%s: wrong family", __func__));172173arg.ifa = ifa;174arg.del = delete;175176rib_foreach_table_walk_del(AF_INET, in_ifadownkill, &arg);177ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */178}179180181