1.\" $OpenBSD: route.8,v 1.119 2023/08/02 23:34:13 aisha Exp $ 2.\" $NetBSD: route.8,v 1.6 1995/03/18 15:00:13 cgd Exp $ 3.\" 4.\" Copyright (c) 1983, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)route.8 8.3 (Berkeley) 3/19/94 32.\" 33.Dd $Mdocdate: August 2 2023 $ 34.Dt ROUTE 8 35.Os 36.Sh NAME 37.Nm route 38.Nd manually manipulate the routing tables 39.Sh SYNOPSIS 40.Nm route 41.Op Fl dnqtv 42.Op Fl T Ar rtable 43.Ar command 44.Oo 45.Op Ar modifier ... 46.Ar arg ... 47.Oc 48.Sh DESCRIPTION 49At system start, 50routing tables are initialised and configured by 51.Xr netstart 8 . 52The 53.Nm 54utility can be used to view or manually manipulate the network routing tables. 55Only the superuser may modify the routing tables. 56.Pp 57The options are as follows: 58.Bl -tag -width Ds 59.It Fl d 60Run in debug-only mode, i.e. don't actually modify the routing table. 61.It Fl n 62Bypass attempts to print host and network names symbolically 63when reporting actions. 64The process of translating between symbolic 65names and numerical equivalents can be quite time consuming, and 66may require correct operation of the network; thus it may be expedient 67to forgo this, especially when attempting to repair networking operations. 68.It Fl q 69Suppress all output. 70.It Fl T Ar rtable 71Select an alternate routing table to modify or query. 72The default is to use the current routing table. 73The current routing table can be displayed with 74.Xr id 1 . 75.It Fl t 76Write routing messages to a fake device 77.Pa ( /dev/null ) 78instead of a real routing socket to test route manipulation. 79.It Fl v 80Print additional details. 81.El 82.Pp 83The commands are as follows: 84.Pp 85.Bl -tag -width "XXXX" -compact 86.It Xo 87.Nm route 88.Op Fl dnqtv 89.Op Fl T Ar rtable 90.Tg 91.Cm add 92.Op Ar modifier 93.Ar destination 94.Ar gateway 95.Xc 96.It Xo 97.Nm route 98.Op Fl dnqtv 99.Op Fl T Ar rtable 100.Tg 101.Cm change 102.Op Ar modifier 103.Ar destination 104.Ar gateway 105.Xc 106Add a new route, or modify an existing route, to the host or network at 107.Ar destination . 108.Ar gateway 109is the next-hop intermediary by which packets should be routed. 110See 111.Sx Address notation and interpretation 112for more information. 113.Pp 114A number of modifiers can be used with 115.Cm add 116and 117.Cm change , 118and as documented with the other commands: 119.Pp 120.Bl -tag -width Ds -compact 121.It Ar flags 122Various flags can be set on routes 123(viewable using 124.Cm show ) : 125.Pp 126.Bl -tag -width -blackhole -compact 127.It Fl blackhole 128silently discard packets 129.It Fl cloning 130generates a new route on use 131.It Fl iface 132destination is directly reachable 133.It Fl llinfo 134validly translates address to link address 135.It Fl mpath 136multiple gateways for a destination exist 137.It Fl nostatic 138pretend route added by kernel or daemon 139.It Fl proto1 140sets protocol specific routing flag #1 141.It Fl proto2 142sets protocol specific routing flag #2 143.It Fl reject 144emits an ICMP unreachable when matched 145.It Fl static 146manually added route (default) 147.El 148.Pp 149The 150.Fl blackhole 151and 152.Fl reject 153flags require a 154.Ar gateway 155to the loopback interface, 156either 127.0.0.1 or ::1. 157.Pp 158.It Oo Fl lock | Fl lockrest Oc Fl expire Ar n 159.It Oo Fl lock | Fl lockrest Oc Fl mtu Ar n 160Specify the lifetime for the route (e.g. if generated by a redirect) 161or the Maximum Transmission Unit (MTU) size for this path, respectively. 162The value 163.Ar n 164is locked if preceded by 165.Fl lock ; 166if preceded by 167.Fl lockrest 168all following 169.Fl expire 170and 171.Fl mtu 172metrics are locked. 173.Pp 174.It Fl host | net 175Interpret 176.Ar destination 177as a host or network, respectively. 178.Pp 179.It Fl ifa Ar address 180.It Fl ifp Ar ifname 181Where the destination and gateway are not sufficient to specify 182the route, 183these modifiers may be used to determine the interface address 184.Pq Fl ifa 185or name 186.Pq Fl ifp . 187.Pp 188.It Fl label Ar label 189Associate the route with a 190.Ar label . 191Route labels can be used to attach arbitrary information to a route. 192.Pp 193.It Fl mpath 194Used to enter multiple gateways for the same destination address (multipath). 195When multiple routes exist for a destination, one route is selected based 196on the source address of the packet. 197The 198.Xr sysctl 8 199variables 200.Va net.inet.ip.multipath 201and 202.Va net.inet6.ip6.multipath 203are used to control multipath routing. 204If set to 1, 205multiple routes with the same priority are used equally; 206if set to 0, 207the first route selected will be used for subsequent packets to that 208destination regardless of source. 209.Pp 210.It Xo 211.Fl mplslabel in Ar label 212.Fl push Ns | Ns Fl pop Ns | Ns Fl swap 213.Op Fl out Ar label 214.Xc 215For MPLS routes, 216specify an ingress LSR to associate a particular label to an IPv4/IPv6 route. 217The MPLS traffic 218.Fl in 219and 220.Fl out 221modifiers are intended to identify the ingress label and, optionally, 222the outgoing one. 223Additionally, one of the following operations must be used: 224.Fl push , 225.Fl pop 226or 227.Fl swap . 228The route's gateway can be specified using the 229.Fl inet 230or 231.Fl inet6 232modifier before the address. 233.Pp 234.It Fl netmask Ar mask 235.It Fl prefixlen Ar len 236Used to add subnet routes with the specified netmask. 237The netmask should be specified after the 238.Ar destination 239parameter. 240If no netmask is specified, 241an implicit one is used for the 242.Dv AF_INET 243family. 244The network mask can also be specified as a prefix length, 245but in that case one of either 246.Fl inet 247or 248.Fl inet6 249must also be specified. 250.Pp 251.It Fl priority Ar n 252Specifies a routing priority. 253If no priority is specified, the kernel will set a priority depending on the 254.Dv RTF_STATIC 255flag to either 256.Dv RTP_STATIC 257or 258.Dv RTP_DEFAULT . 259Note that priority 1 is reserved for kernel use. 260.El 261.Pp 262.It Xo 263.Nm route 264.Op Fl dnqtv 265.Op Fl T Ar rtable 266.Tg delete 267.Tg 268.Cm del Ns Op Cm ete 269.Ar destination 270.Op Fl priority Ar n 271.Op Ar gateway 272.Xc 273Delete the route to 274.Ar destination . 275If multiple routes to the destination exist, 276a specific route must be selected by specifying the priority 277and/or a gateway. 278.Pp 279.It Xo 280.Nm route 281.Op Fl T Ar rtable 282.Tg 283.Cm exec 284.Ar command 285.Op Ar arg ... 286.Xc 287Execute a command, forcing the process and its children to use the 288routing table and appropriate routing domain as specified with the 289.Fl T Ar rtable 290option. 291.Pp 292.It Xo 293.Nm route 294.Op Fl nqv 295.Op Fl T Ar rtable 296.Tg 297.Cm flush 298.Op Ar family 299.Op Fl iface Ar ifname 300.Op Fl priority Ar n 301.Xc 302Delete all gateway entries from the routing table, 303optionally limited to a specific address family. 304Routes matching a specific interface or priority can be flushed 305by using the 306.Fl iface 307or 308.Fl priority 309modifiers. 310.Pp 311.It Xo 312.Bk -words 313.Nm route 314.Op Fl nv 315.Op Fl T Ar rtable 316.Tg 317.Cm get 318.Ar destination 319.Op Fl priority Ar n 320.Op Ar gateway 321.Ek 322.Xc 323Display the route to 324.Ar destination . 325If multiple routes to the destination exist, 326a specific route may be selected by specifying the priority 327and/or a gateway. 328.Pp 329.It Xo 330.Nm 331.Op Fl n 332.Op Fl T Ar rtable 333.Tg 334.Cm monitor 335.Op Ar family 336.Op Fl iface 337.Xc 338Continuously report any changes to the routing information base. 339The information reported can be limited to a specific address family, 340a specific routing table 341using the 342.Fl T 343option, 344or interface specific messages (link state changes) using the 345.Fl iface 346modifier. 347.Pp 348.It Xo 349.Ic route 350.Op Fl dtv 351.Op Fl T Ar rtable 352.Tg 353.Cm nameserver 354.Ar interface 355.Op Ar address ... 356.Xc 357Broadcast a list of up to five nameserver address proposals to 358.Xr resolvd 8 , 359which is used to update the list of nameservers for the given interface in 360.Xr resolv.conf 5 . 361If no address is given, 362a request to remove the nameservers for the given interface is sent. 363.Pp 364.It Xo 365.Nm route 366.Op Fl nv 367.Op Fl T Ar rtable 368.Tg 369.Cm show 370.Op Ar family 371.Op Fl gateway 372.Op Fl label Ar label 373.Op Fl priority Ar n 374.Xc 375Display the routing table. 376.Pp 377If 378.Fl gateway 379is specified, only routes whose gateway are in the 380same address family as the destination are shown. 381.Pp 382If 383.Fl label 384is specified, only routes with the specified label are shown. 385.Pp 386If 387.Fl priority 388is specified, only routes with the specified priority are shown. 389It may be specified by number or one of 390.Cm local , 391.Cm connected , 392.Cm static , 393.Cm ospf , 394.Cm rip , 395or 396.Cm bgp . 397If the priority is negative, then routes that do not match the numeric 398priority are shown. 399.Pp 400Within the output of 401.Cm show , 402the "Flags" column indicates what flags are set on the route. 403The mapping between letters and flags is: 404.Bl -column "1" "RTF_BLACKHOLE" "Protocol specific routing flag #1." 405.It Dv 1 Ta Dv RTF_PROTO1 Ta "Protocol specific routing flag #1." 406.It Dv 2 Ta Dv RTF_PROTO2 Ta "Protocol specific routing flag #2." 407.It Dv 3 Ta Dv RTF_PROTO3 Ta "Protocol specific routing flag #3." 408.It Dv B Ta Dv RTF_BLACKHOLE Ta "Just discard packets." 409.It Dv b Ta Dv RTF_BROADCAST Ta "Correspond to a local broadcast address." 410.It Dv C Ta Dv RTF_CLONING Ta "Generate new routes on use." 411.It Dv c Ta Dv RTF_CLONED Ta "Cloned routes (generated from RTF_CLONING)." 412.It Dv D Ta Dv RTF_DYNAMIC Ta "Created dynamically (by redirect)." 413.It Dv G Ta Dv RTF_GATEWAY Ta "Dest requires forwarding by intermediary." 414.It Dv H Ta Dv RTF_HOST Ta "Host entry (net otherwise)." 415.It Dv h Ta Dv RTF_CACHED Ta "Referenced by gateway route." 416.It Dv L Ta Dv RTF_LLINFO Ta "Valid protocol to link address translation." 417.It Dv l Ta Dv RTF_LOCAL Ta "Correspond to a local address." 418.It Dv M Ta Dv RTF_MODIFIED Ta "Modified dynamically (by redirect)." 419.It Dv m Ta Dv RTF_MULTICAST Ta "Correspond to a multicast address." 420.It Dv n Ta Dv RTF_CONNECTED Ta "Interface route." 421.It Dv P Ta Dv RTF_MPATH Ta "Multipath route." 422.It Dv R Ta Dv RTF_REJECT Ta "Host or net unreachable." 423.It Dv S Ta Dv RTF_STATIC Ta "Manually added." 424.It Dv T Ta Dv RTF_MPLS Ta "MPLS route." 425.It Dv U Ta Dv RTF_UP Ta "Route usable." 426.El 427.Pp 428.It Xo 429.Ic route 430.Op Fl T Ar rtable 431.Tg 432.Cm sourceaddr 433.Op Fl ifp Ar ifname | Ar address 434.Xc 435Set the preferred source address to 436.Ar address . 437The source address can be set to the address assigned to interface 438.Ar ifname 439if 440.Fl ifp 441is specified, 442or reset by setting the address to zero. 443If no arguments are given, 444the preferred source addresses are printed. 445The preferred source address is not used if the destination is on-link 446or the source address is assigned to a disabled interface. 447.El 448.Ss Address notation and interpretation 449Addresses are assumed to be IPv4 unless they contain a colon, 450in which case they are treated as IPv6. 451Alternatively they may be specified as belonging to a particular address family 452using one of the following modifiers: 453.Pp 454.Bl -tag -width "-inet6XXX" -offset indent -compact 455.It Fl inet 456IPv4 addresses; 457see 458.Xr ip 4 459.It Fl inet6 460IPv6 addresses; 461see 462.Xr ip6 4 463.It Fl link 464hardware (link-level) addresses 465.It Fl mpls 466MPLS addresses 467.It Fl sa 468actual 469.Vt sockaddr 470data, in hexadecimal format 471.El 472.Pp 473.Ar destination 474is assumed to be a route to a network 475if any of the following apply: 476.Pp 477.Bl -bullet -compact 478.It 479the 480.Fl net 481modifier is used 482.It 483it is the word "default", equivalent to 0/0 or ::/0 484.It 485it is an address with a 486.Dq / Ns Em XX 487suffix, where 488.Em XX 489is the number of bits in the network portion of the address 490.It 491it specifies the network portion either with 492.Fl netmask 493or 494.Fl prefixlen 495.El 496.Pp 497If 498.Ar destination 499is a valid IP address or host name, 500or the 501.Fl host 502modifier is used, 503it is assumed to be a route to a host. 504.Pp 505All symbolic names specified for a 506.Ar destination 507or 508.Ar gateway 509are looked up using 510.Xr gethostbyname 3 . 511.Sh FILES 512.Bl -tag -width "/etc/mygate" -compact 513.It Pa /etc/hosts 514host and network name database 515.It Pa /etc/mygate 516default gateway address 517.El 518.Sh EXIT STATUS 519For commands other than 520.Cm exec , 521the 522.Nm 523utility exits 0 on success, and >0 if an error occurs. 524.Pp 525For the 526.Cm exec 527command the 528.Nm 529utility exits with the exit status of 530.Ar command 531if it could be invoked. 532Otherwise the 533.Nm 534utility exits with one of the following values: 535.Bl -tag -width Ds 536.It 1 537An invalid command line option was passed to 538.Nm 539or setting the routing table failed. 540.It 126 541.Ar command 542was found but could not be invoked. 543.It 127 544.Ar command 545could not be found. 546.El 547.Sh EXAMPLES 548Show the current IPv4 routing tables, 549without attempting to print hostnames symbolically: 550.Pp 551.Dl $ route -n show -inet 552.Pp 553Add a static 554.Xr inet 4 555route to the 192.168.5.0/24 network via the 192.168.0.1 gateway: 556.Pp 557.Dl # route add -inet 192.168.5.0/24 192.168.0.1 558.Pp 559Amend the 560.Xr inet 4 561route to the 192.168.5.0/24 network to use the 192.168.0.2 gateway: 562.Pp 563.Dl # route change -inet 192.168.5.0/24 192.168.0.2 564.Pp 565Delete the 566.Xr inet 4 567route to the 192.168.5.0/24 network: 568.Pp 569.Dl # route delete -inet 192.168.5.0/24 570.Pp 571Add a static 572.Xr inet6 4 573route to a host which is on the vio0 interface that is outside your prefix, 574and use that host as a default gateway, as used by some hosting providers: 575.Pp 576.Dl # route add -inet6 2001:db8:efef::1 -cloning -link -iface vio0 577.Dl # route add -inet6 default 2001:db8:efef::1 578.Sh DIAGNOSTICS 579.Bl -diag 580.It "%s: gateway %s flags %x" 581The specified route is being added to or deleted from the tables. 582If the gateway address used was not the primary address of the gateway 583(the first one returned by 584.Xr gethostbyname 3 ) , 585the gateway address is printed numerically as well as symbolically. 586.It "%s %s done" 587When the 588.Cm flush 589command is specified, each routing table entry deleted 590is indicated with a message of this form. 591.It "Network is unreachable" 592An attempt to add a route failed because the gateway listed was not 593on a directly connected network. 594The next-hop gateway must be given. 595.It "not in table" 596A 597.Cm delete 598operation was attempted for an entry which 599wasn't present in the tables. 600.It "routing table overflow" 601An 602.Cm add 603operation was attempted, but the system was 604low on resources and was unable to allocate memory 605to create the new entry. 606.El 607.Sh SEE ALSO 608.Xr gethostbyname 3 , 609.Xr inet_net_pton 3 , 610.Xr inet_pton 3 , 611.Xr route 4 , 612.Xr rtable 4 , 613.Xr hosts 5 , 614.Xr mygate 5 , 615.Xr netstart 8 616.Sh HISTORY 617The 618.Nm 619command appeared in 620.Bx 4.2 . 621IPv6 support was added by WIDE/KAME project. 622.Pp 623The 624.Fl recvpipe , 625.Fl hopcount , 626.Fl sendpipe , 627.Fl ssthresh , 628.Fl rtt , 629and 630.Fl rttvar 631modifiers used to be used to initialize various quantities in routing 632table entries. 633The routing system no longer uses these values and the modifiers 634exist now only for compatibility with other operating systems. 635.Sh BUGS 636Some uses of the 637.Fl ifa 638or 639.Fl ifp 640modifiers with the 641.Cm add 642command will incorrectly fail with a 643.Dq Network is unreachable 644message if there is no default route. 645See case 646.Dv RTM_ADD 647in 648.Fn route_output 649from 650.Pa sys/net/rtsock.c 651for details. 652