Skip to content

Commit

Permalink
Add sysctl entry to control to send routing message for RTM_DYNAMIC.
Browse files Browse the repository at this point in the history
Some routing daemons require such routing message to keep coherency.

If we want to let kernel send such message, set net.inet.icmp.dynamic_rt_msg=1
for IPv4, net.inet6.icmp6.dynamic_rt_msg=1 for IPv6.
Default(=0) is the same as before, that is, not send such routing message.
  • Loading branch information
knakahara authored and knakahara committed Aug 29, 2022
1 parent 5768387 commit 33eb59b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 18 deletions.
13 changes: 10 additions & 3 deletions share/man/man7/sysctl.7
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $NetBSD: sysctl.7,v 1.160 2022/08/22 09:25:55 knakahara Exp $
.\" $NetBSD: sysctl.7,v 1.161 2022/08/29 09:14:02 knakahara Exp $
.\"
.\" Copyright (c) 1993
.\" The Regents of the University of California. All rights reserved.
Expand Down Expand Up @@ -29,7 +29,7 @@
.\"
.\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95
.\"
.Dd August 9, 2022
.Dd August 29, 2022
.Dt SYSCTL 7
.Os
.Sh NAME
Expand Down Expand Up @@ -1457,6 +1457,7 @@ The currently defined protocols and names are:
.It icmp rediraccept integer yes
.It icmp redirtimeout integer yes
.It icmp bmcastecho integer yes
.It icmp dynamic_rt_msg boolean yes
.It ip allowsrcrt integer yes
.It ip anonportalgo.selected string yes
.It ip anonportalgo.available string yes
Expand Down Expand Up @@ -1703,6 +1704,9 @@ Number of bytes to return in an ICMP error message.
.It Li icmp.bmcastecho
If set to 1, enables responding to ICMP echo or timestamp request to the
broadcast address.
.It Li icmp.dynamic_rt_msg
A boolean that the kernel sends routing message for RTM_DYNAMIC or not.
If set to true, sends such routing message.
.It Li tcp.ack_on_push
If set to 1, TCP is to immediately transmit an ACK upon reception of
a packet with PUSH set.
Expand Down Expand Up @@ -1856,6 +1860,7 @@ The currently defined protocols and names are:
.It icmp6 rediraccept integer yes
.It icmp6 redirtimeout integer yes
.It icmp6 reflect_pmtu boolean yes
.It icmp6 dynamic_rt_msg boolean yes
.It ip6 accept_rtadv integer yes
.It ip6 addctlpolicy struct in6_addrpolicy no
.It ip6 anonportalgo.selected string yes
Expand Down Expand Up @@ -2120,7 +2125,9 @@ ICMPv6 redirect.
.It Li icmp6.reflect_pmtu
A boolean that icmpv6 reflecting uses path MTU discovery or not.
When not, icmpv6 reflecting uses IPV6_MINMTU.
ICMPv6 redirect.
.It Li icmp6.dynamic_rt_msg
A boolean that the kernel sends routing message for RTM_DYNAMIC or not.
If set to true, sends such routing message.
.It Li udp6.do_loopback_cksum
Perform UDP checksum on loopback.
.It Li udp6.recvspace
Expand Down
45 changes: 43 additions & 2 deletions sys/net/route.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: route.c,v 1.231 2022/08/26 08:32:22 knakahara Exp $ */
/* $NetBSD: route.c,v 1.232 2022/08/29 09:14:02 knakahara Exp $ */

/*-
* Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -97,7 +97,7 @@
#endif

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: route.c,v 1.231 2022/08/26 08:32:22 knakahara Exp $");
__KERNEL_RCSID(0, "$NetBSD: route.c,v 1.232 2022/08/29 09:14:02 knakahara Exp $");

#include <sys/param.h>
#ifdef RTFLUSH_DEBUG
Expand Down Expand Up @@ -884,6 +884,8 @@ rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
error = rtrequest1(RTM_ADD, &info, &rt);
if (rt != NULL)
flags = rt->rt_flags;
if (error == 0)
rt_newmsg_dynamic(RTM_ADD, rt);
stat = &rtstat.rts_dynamic;
} else {
/*
Expand Down Expand Up @@ -1545,6 +1547,45 @@ rt_newmsg(const int cmd, const struct rtentry *rt)
rt_missmsg(cmd, &info, rt->rt_flags, 0);
}

/*
* Inform the routing socket of a route change for RTF_DYNAMIC.
*/
void
rt_newmsg_dynamic(const int cmd, const struct rtentry *rt)
{
extern bool icmp_dynamic_rt_msg;
extern bool icmp6_dynamic_rt_msg;
struct rt_addrinfo info;
struct sockaddr *gateway = rt->rt_gateway;

if (gateway == NULL)
return;

switch(gateway->sa_family){
case AF_INET:
if (!icmp_dynamic_rt_msg)
return;
break;
case AF_INET6:
if (!icmp6_dynamic_rt_msg)
return;
break;
default:
return;
}

memset((void *)&info, 0, sizeof(info));
info.rti_info[RTAX_DST] = rt_getkey(rt);
info.rti_info[RTAX_GATEWAY] = gateway;
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
if (rt->rt_ifp) {
info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
}

rt_missmsg(cmd, &info, rt->rt_flags, 0);
}

/*
* Set up or tear down a routing table entry, normally
* for an interface.
Expand Down
3 changes: 2 additions & 1 deletion sys/net/route.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: route.h,v 1.130 2022/08/26 08:32:22 knakahara Exp $ */
/* $NetBSD: route.h,v 1.131 2022/08/29 09:14:02 knakahara Exp $ */

/*
* Copyright (c) 1980, 1986, 1993
Expand Down Expand Up @@ -437,6 +437,7 @@ int rt_update_prepare(struct rtentry *);
void rt_update_finish(struct rtentry *);

void rt_newmsg(const int, const struct rtentry *);
void rt_newmsg_dynamic(const int, const struct rtentry *);
struct rtentry *
rtalloc1(const struct sockaddr *, int);
int rtinit(struct ifaddr *, int, int);
Expand Down
3 changes: 2 additions & 1 deletion sys/netinet/icmp6.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: icmp6.h,v 1.58 2022/08/22 09:25:55 knakahara Exp $ */
/* $NetBSD: icmp6.h,v 1.59 2022/08/29 09:14:02 knakahara Exp $ */
/* $KAME: icmp6.h,v 1.84 2003/04/23 10:26:51 itojun Exp $ */


Expand Down Expand Up @@ -641,6 +641,7 @@ struct icmp6_filter {
#endif
#define ICMPV6CTL_ND6_MAXQLEN 24
#define ICMPV6CTL_REFLECT_PMTU 25
#define ICMPV6CTL_DYNAMIC_RT_MSG 26

#ifdef _KERNEL
struct rtentry;
Expand Down
3 changes: 2 additions & 1 deletion sys/netinet/icmp_var.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: icmp_var.h,v 1.31 2018/08/22 01:05:24 msaitoh Exp $ */
/* $NetBSD: icmp_var.h,v 1.32 2022/08/29 09:14:02 knakahara Exp $ */

/*
* Copyright (c) 1982, 1986, 1993
Expand Down Expand Up @@ -72,6 +72,7 @@
#define ICMPCTL_REDIRTIMEOUT 6 /* Remove routes added via redirects */
#define ICMPCTL_STATS 7 /* ICMP statistics */
#define ICMPCTL_BMCASTECHO 8 /* allow broad/mult-cast echo */
#define ICMPCTL_DYNAMIC_RT_MSG 9 /* send routing message for RTM_DYNAMIC */

#ifdef _KERNEL

Expand Down
8 changes: 5 additions & 3 deletions sys/netinet/in_pcb.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: in_pcb.c,v 1.189 2022/07/29 07:35:16 knakahara Exp $ */
/* $NetBSD: in_pcb.c,v 1.190 2022/08/29 09:14:02 knakahara Exp $ */

/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
Expand Down Expand Up @@ -93,7 +93,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.189 2022/07/29 07:35:16 knakahara Exp $");
__KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.190 2022/08/29 09:14:02 knakahara Exp $");

#ifdef _KERNEL_OPT
#include "opt_inet.h"
Expand Down Expand Up @@ -841,8 +841,10 @@ in_losing(struct inpcb *inp)
error = rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
rtcache_unref(rt, &inp->inp_route);
if (error == 0)
if (error == 0) {
rt_newmsg_dynamic(RTM_DELETE, nrt);
rt_free(nrt);
}
} else
rtcache_unref(rt, &inp->inp_route);
/*
Expand Down
16 changes: 14 additions & 2 deletions sys/netinet/ip_icmp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: ip_icmp.c,v 1.177 2018/12/22 14:28:57 maxv Exp $ */
/* $NetBSD: ip_icmp.c,v 1.178 2022/08/29 09:14:02 knakahara Exp $ */

/*
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -94,7 +94,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.177 2018/12/22 14:28:57 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.178 2022/08/29 09:14:02 knakahara Exp $");

#ifdef _KERNEL_OPT
#include "opt_ipsec.h"
Expand Down Expand Up @@ -158,6 +158,8 @@ LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
/* unused... */
u_int ip_next_mtu(u_int, int);

bool icmp_dynamic_rt_msg = false;

static int icmperrppslim = 100; /* 100pps */
static int icmperrpps_count = 0;
static struct timeval icmperrppslim_last;
Expand Down Expand Up @@ -1118,6 +1120,13 @@ sysctl_netinet_icmp_setup(struct sysctllog **clog)
NULL, 0, &icmpbmcastecho, 0,
CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_BMCASTECHO,
CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_BOOL, "dynamic_rt_msg",
SYSCTL_DESCR("Send routing message for RTF_DYNAMIC"),
NULL, 0, &icmp_dynamic_rt_msg, 0,
CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_DYNAMIC_RT_MSG,
CTL_EOL);
}

void
Expand Down Expand Up @@ -1158,6 +1167,7 @@ icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
return;
}
nrt->rt_rmx = rt->rt_rmx;
rt_newmsg_dynamic(RTM_ADD, nrt);
rt_unref(rt);
rt = nrt;
}
Expand Down Expand Up @@ -1271,6 +1281,7 @@ icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
(RTF_DYNAMIC | RTF_HOST)) {
rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt);
rt_newmsg_dynamic(RTM_DELETE, retrt);
rt_unref(rt);
rt_free(retrt);
} else {
Expand All @@ -1292,6 +1303,7 @@ icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
(RTF_DYNAMIC | RTF_HOST)) {
rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt);
rt_newmsg_dynamic(RTM_DELETE, retrt);
rt_unref(rt);
rt_free(retrt);
}
Expand Down
16 changes: 14 additions & 2 deletions sys/netinet6/icmp6.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: icmp6.c,v 1.251 2022/08/22 09:25:55 knakahara Exp $ */
/* $NetBSD: icmp6.c,v 1.252 2022/08/29 09:14:02 knakahara Exp $ */
/* $KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $ */

/*
Expand Down Expand Up @@ -62,7 +62,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.251 2022/08/22 09:25:55 knakahara Exp $");
__KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.252 2022/08/29 09:14:02 knakahara Exp $");

#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
Expand Down Expand Up @@ -151,6 +151,8 @@ static int icmp6errpps_count = 0;
static struct timeval icmp6errppslim_last;
extern int icmp6_nodeinfo;

bool icmp6_dynamic_rt_msg = false;

/*
* List of callbacks to notify when Path MTU changes are made.
*/
Expand Down Expand Up @@ -2838,6 +2840,7 @@ icmp6_mtudisc_clone(struct sockaddr *dst)
return NULL;
}
nrt->rt_rmx = rt->rt_rmx;
rt_newmsg_dynamic(RTM_ADD, nrt);
rt_unref(rt);
rt = nrt;
}
Expand Down Expand Up @@ -2867,6 +2870,7 @@ icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
(RTF_DYNAMIC | RTF_HOST)) {
rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt);
rt_newmsg_dynamic(RTM_DELETE, retrt);
rt_unref(rt);
rt_free(retrt);
} else {
Expand All @@ -2887,6 +2891,7 @@ icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
(RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt);
rt_newmsg_dynamic(RTM_DELETE, retrt);
rt_unref(rt);
rt_free(retrt);
}
Expand Down Expand Up @@ -3117,6 +3122,13 @@ sysctl_net_inet6_icmp6_setup(struct sysctllog **clog)
NULL, 0, &icmp6_reflect_pmtu, 0,
CTL_NET, PF_INET6, IPPROTO_ICMPV6,
ICMPV6CTL_REFLECT_PMTU, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_BOOL, "dynamic_rt_msg",
SYSCTL_DESCR("Send routing message for RTF_DYNAMIC"),
NULL, 0, &icmp6_dynamic_rt_msg, 0,
CTL_NET, PF_INET6, IPPROTO_ICMPV6,
ICMPV6CTL_DYNAMIC_RT_MSG, CTL_EOL);
}

void
Expand Down
8 changes: 5 additions & 3 deletions sys/netinet6/in6_pcb.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: in6_pcb.c,v 1.169 2022/07/29 07:35:16 knakahara Exp $ */
/* $NetBSD: in6_pcb.c,v 1.170 2022/08/29 09:14:02 knakahara Exp $ */
/* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $ */

/*
Expand Down Expand Up @@ -62,7 +62,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.169 2022/07/29 07:35:16 knakahara Exp $");
__KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.170 2022/08/29 09:14:02 knakahara Exp $");

#ifdef _KERNEL_OPT
#include "opt_inet.h"
Expand Down Expand Up @@ -940,8 +940,10 @@ in6_losing(struct in6pcb *in6p)
error = rtrequest(RTM_DELETE, rt_getkey(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
rtcache_unref(rt, &in6p->in6p_route);
if (error == 0)
if (error == 0) {
rt_newmsg_dynamic(RTM_DELETE, nrt);
rt_free(nrt);
}
} else
rtcache_unref(rt, &in6p->in6p_route);
/*
Expand Down

0 comments on commit 33eb59b

Please sign in to comment.