/*-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);121if (rh == NULL)122return (NULL);123124rh->rnh_set_nh_pfxflags = rib4_set_nh_pfxflags;125rh->rnh_augment_nh = rib4_augment_nh;126127return (rh);128}129130#ifdef VIMAGE131void132in_detachhead(struct rib_head *rh)133{134135rt_table_destroy(rh);136}137#endif138139/*140* This zaps old routes when the interface goes down or interface141* address is deleted. In the latter case, it deletes static routes142* that point to this address. If we don't do this, we may end up143* using the old address in the future. The ones we always want to144* get rid of are things like ARP entries, since the user might down145* the interface, walk over to a completely different network, and146* plug back in.147*/148struct in_ifadown_arg {149struct ifaddr *ifa;150int del;151};152153static int154in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,155void *xap)156{157struct in_ifadown_arg *ap = xap;158159if (nh->nh_ifa != ap->ifa)160return (0);161162if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)163return (0);164165return (1);166}167168void169in_ifadown(struct ifaddr *ifa, int delete)170{171struct in_ifadown_arg arg;172173KASSERT(ifa->ifa_addr->sa_family == AF_INET,174("%s: wrong family", __func__));175176arg.ifa = ifa;177arg.del = delete;178179rib_foreach_table_walk_del(AF_INET, in_ifadownkill, &arg);180ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */181}182183184