---
name: "rtfree"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/rtfree"
---

# rtfree(9)

look up a route in the kernel routing table

## NAME

**rtalloc**, **rtalloc\_ign**, **rtalloc1**, **rtfree** – look up a route in the kernel routing table

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/socket.h\>**

**#include \<net/route.h\>**

*void*\

**rtalloc**(*struct route \*ro*);

*void*\

**rtalloc\_ign**(*struct route \*ro*, *u\_long flags*);

*struct rtentry \**\

**rtalloc1**(*struct sockaddr \*sa*, *int report*, *u\_long flags*);

*void*\

**rtfree**(*struct rt\_entry \*rt*);

**RTFREE**(*struct rt\_entry \*rt*);

**RT\_LOCK**(*struct rt\_entry \*rt*);

**RT\_UNLOCK**(*struct rt\_entry \*rt*);

**RT\_ADDREF**(*struct rt\_entry \*rt*);

**RT\_REMREF**(*struct rt\_entry \*rt*);

## DESCRIPTION

The kernel uses a radix tree structure to manage routes for the networking subsystem. The **rtalloc**() family of routines is used by protocols to query this structure for a route corresponding to a particular end-node address, and to cause certain protocol- and interface-specific actions to take place.

When a route with the flag RTF\_CLONING is retrieved, and the action of this flag is not masked, the **rtalloc** facility automatically generates a new route using information in the old route as a template, and sends an RTM\_RESOLVE message to the appropriate interface-address route-management routine (**ifa-\>ifa\_rtrequest**()). This generated route is called *cloned*, and has RTF\_WASCLONED flag set. RTF\_PRCLONING flag is obsolete and thus ignored by facility. If the RTF\_XRESOLVE flag is set, then the RTM\_RESOLVE message is sent instead on the [route(4)](/route/4.md) socket interface, requesting that an external program resolve the address in question and modify the route appropriately.

The default interface is **rtalloc**(). Its only argument is *ro*, a pointer to a “struct route”, which is defined as follows:

```
struct route {
	struct sockaddr ro_dst;
	struct rtentry *ro_rt;
};
```

Thus, this function can only be used for address families which are smaller than the default “struct sockaddr”. Before calling **rtalloc**() for the first time, callers should ensure that unused bits of the structure are set to zero. On subsequent calls, **rtalloc**() returns without performing a lookup if *ro-\>ro\_rt* is non-null and the RTF\_UP flag is set in the route's rt\_flags field.

The **rtalloc\_ign**() interface can be used when the default actions of **rtalloc**() in the presence of the RTF\_CLONING flag is undesired. The *ro* argument is the same as **rtalloc**(), but there is additionally a *flags* argument, which lists the flags in the route which are to be *ignored* (in most cases this is RTF\_CLONING flag). Both **rtalloc**() and **rtalloc\_ign**() functions return a pointer to an unlocked *struct rtentry*.

The **rtalloc1**() function is the most general form of **rtalloc**() (and both of the other forms are implemented as calls to rtalloc1). It does not use the “struct route”, and is therefore suitable for address families which require more space than is in a traditional “struct sockaddr”. Instead, it takes a “struct sockaddr \*” directly as the *sa* argument. The second argument, *report*, controls whether RTM\_RESOLVE requests are sent to the lower layers when an RTF\_CLONING or RTF\_PRCLONING route is cloned. Ordinarily a value of one should be passed, except in the processing of those lower layers which use the cloning facility. The third argument, *flags*, is a set of flags to ignore, as in **rtalloc\_ign**(). The **rtalloc1**() function returns a pointer to a locked *struct rtentry*.

The **rtfree**() function frees a locked route entry, e.g., a previously allocated by **rtalloc1**().

The **RTFREE**() macro is used to free unlocked route entries, previously allocated by **rtalloc**() or **rtalloc\_ign**(). The **RTFREE**() macro decrements the reference count on the routing table entry (see below), and frees it if the reference count has reached zero.

The preferred usage is allocating a route using **rtalloc**() or **rtalloc\_ign**() and freeing using **RTFREE**().

The **RT\_LOCK**() macro is used to lock a routing table entry. The **RT\_UNLOCK**() macro is used to unlock a routing table entry.

The **RT\_ADDREF**() macro increments the reference count on a previously locked route entry. The **RT\_REMREF**() macro decrements the reference count on a previously locked route entry.

## RETURN VALUES

The **rtalloc**(), **rtalloc\_ign**() and **rtfree**() functions do not return a value. The **rtalloc1**() function returns a pointer to a routing-table entry if it succeeds, otherwise a null pointer. Lack of a route should in most cases be translated to the [errno(2)](/errno/2.md) value EHOSTUNREACH.

## SEE ALSO

[route(4)](/route/4.md), [rtentry(9)](/rtentry/9.md)

## HISTORY

The **rtalloc** facility first appeared in 4.2BSD, although with much different internals. The **rtalloc\_ign**() function and the *flags* argument to **rtalloc1**() first appeared in FreeBSD 2.0. Routing table locking was introduced in FreeBSD 5.2.

## AUTHORS

This manual page was written by Garrett Wollman, as were the changes to implement RTF\_PRCLONING and the **rtalloc\_ign**() function and the *flags* argument to **rtalloc1**().
