2 .\" This file and its contents are supplied under the terms of the
3 .\" Common Development and Distribution License ("CDDL"), version 1.0.
4 .\" You may only use this file in accordance with the terms of version
7 .\" A full copy of the text of the CDDL should have accompanied this
8 .\" source. A copy of the CDDL is also available via the Internet at
9 .\" http://www.illumos.org/license/CDDL.
12 .\" Copyright (c) 2015, Joyent, Inc. All rights reserved.
20 .Nd Neighbor Discovery Protocol
27 s = socket(PF_INET6, SOCK_DGRAM, 0);
30 ioctl(s, SIOCLIFGETND, &lifr);
31 ioctl(s, SIOCLIFSETND, &lifr);
32 ioctl(s, SIOCLIFDELND, &lifr);
35 The Neighbor Discovery Protocol (NDP) is a protocol used to distribute and request
36 information about neighboring IPv6 systems on the local network, much like
38 for IPv4. NDP is also responsible for spreading information about the network
39 gateway and how hosts should configure themselves
40 .Pq see Xr in.ndpd 1M for more on how this happens .
41 .Sh APPLICATION PROGRAMMING INTERFACE
42 The operating system provides several ioctls to help manipulate the mappings
43 obtained through NDP. They are
48 for getting, setting, and deleting respectively. Each of these ioctls takes a
50 .Pq see Xr if 7P for details ,
54 .Vt struct lif_nd_req :
55 .Bd -literal -offset 2m
56 typedef struct lif_nd_req {
57 struct sockaddr_storage lnr_addr;
58 uint8_t lnr_state_create;
59 uint8_t lnr_state_same_lla;
60 uint8_t lnr_state_diff_lla;
64 char lnr_hdw_addr[ND_MAX_HDW_LEN];
70 field should be filled in with an IPv6 address
71 .Pq see Xr sockaddr_in6 3SOCKET ,
74 is the link-layer address of length
78 .Fa lnr_state_create ,
79 .Fa lnr_state_same_lla ,
81 .Fa lnr_state_diff_lla
82 can be set to one of the following values:
83 .Bl -tag -offset indent -width 16m
85 For ioctls that don't modify state
87 Address resolution is currently in progress
89 The link-layer address has recently been reachable
91 The link-layer address may be unreachable, and the system shouldn't do anything
93 This entry hasn't yet started sending Neighbor Solicitations
95 The operating system is currently sending out Neighbor Solicitations for the address
97 The link-layer address is unreachable, and this entry is going to be deleted.
100 When creating a new entry, the only valid values for
106 Any other value will return
109 .Fa lnr_state_same_lla
111 .Fa lnr_state_diff_lla
112 fields are reserved for future use and can be safely set to
118 Flags that can be placed in
121 .Bl -tag -offset indent -width 16m
122 .It Sy NDF_ISROUTER_ON
123 Mark this entry as being a router. This will cause Neighbor Advertisements for
124 this address to be sent with the R-bit (Router).
125 .It Sy NDF_ISROUTER_OFF
126 If this entry was flagged as being a router, remove the flag.
127 .It Sy NDF_ANYCAST_ON
128 Mark this entry as being for an anycast address. This prevents sending Neighbor
129 Advertisements with the O-bit (Override).
130 .It Sy NDF_ANYCAST_OFF
131 If this entry was flagged as an anycast address, remove the flag.
133 Prevent this entry from being deleted by the system.
138 these flags represent the current state of the corresponding Neighbor Cache
141 these flags represent what changes should be applied to the underlying entry.
143 The only fields that need to be set for the
151 All other fields should be zeroed out. After successfully getting an entry, the
152 other fields will be filled in. When using
154 all fields should be set to an appropriate value, as described above, with the
157 which is unused and only exists for padding purposes.
159 After performing the ioctl, the following errors may be returned through the
163 .Bl -tag -offset indent -width 16m
165 A non-IPv6 socket was used to perform the ioctl.
167 The request contents were bad. This could be because conflicting flags were
168 used, the specified interface wasn't logical unit zero, or another reason.
170 The system ran out of memory for internal data structures.
172 The specified interface does not exist.
174 The caller does not have permission to modify the Neighbor Cache Entries
175 associated with this interface. They may be lacking the
176 .Sy PRIV_SYS_NET_CONFIG
178 .Po see Xr privileges 5 Pc ,
179 or the interface is managed by IPMP (IP Network Multipathing).
181 There is no entry matching the specified address.
184 The following examples demonstrate how to get and set NDP mappings using the
186 .Ss Example 1: Getting a mapping
187 .Bd -literal -offset indent
189 * Example of getting a mapping for a node name.
194 #include <sys/socket.h>
195 #include <sys/sockio.h>
200 int get(char *host) {
202 struct addrinfo hints, *serverinfo, *p;
205 bzero(&hints, sizeof (struct addrinfo));
206 hints.ai_family = PF_INET6;
207 hints.ai_protocol = IPPROTO_IPV6;
209 if ((err = getaddrinfo(host, NULL, &hints, &serverinfo)) != 0) {
210 (void) fprintf(stderr, "Unable to lookup %s: %s\\n", host,
215 s = socket(AF_INET6, SOCK_DGRAM, 0);
217 perror("Failed to open IPv6 socket");
221 for (p = serverinfo; p != NULL; p = p->ai_next) {
222 /* Zero out structure */
223 bzero(&lifr, sizeof (struct lifreq));
224 (void) strlcpy(lifr.lifr_name, "net0",
225 sizeof (lifr.lifr_name));
226 (void) memcpy(&lifr.lifr_nd.lnr_addr, p->ai_addr,
227 sizeof (struct sockaddr_storage));
230 if (ioctl(s, SIOCLIFGETND, &lifr) < 0) {
231 perror("Unable to get NDP mapping");
236 * lifr.lifr_nd.lnr_hdw_addr now contains the MAC address,
237 * and can be used as desired.
242 * Clean up linked list.
244 freeaddrinfo(serverinfo);
248 int main(int argc, char *argv[]) {
251 return (get(argv[1]));
255 Deleting a mapping would work similarly, except that instead of using
257 you would instead use the
260 .Ss Example 2: Adding a mapping
261 .Bd -literal -offset indent
263 * Example of setting a mapping to an all-zero Ethernet address.
268 #include <sys/socket.h>
269 #include <sys/sockio.h>
274 int set(char *host) {
276 struct addrinfo hints, *serverinfo, *p;
279 bzero(&hints, sizeof (struct addrinfo));
280 hints.ai_family = PF_INET6;
281 hints.ai_protocol = IPPROTO_IPV6;
283 if ((err = getaddrinfo(host, NULL, &hints, &serverinfo)) != 0) {
284 (void) fprintf(stderr, "Unable to lookup %s: %s\\n", host,
289 s = socket(AF_INET6, SOCK_DGRAM, 0);
291 perror("Failed to open IPv6 socket");
295 for (p = serverinfo; p != NULL; p = p->ai_next) {
296 /* Zero out structure */
297 bzero(&lifr, sizeof (struct lifreq));
298 (void) strlcpy(lifr.lifr_name, "net0",
299 sizeof (lifr.lifr_name));
300 (void) memcpy(&lifr.lifr_nd.lnr_addr, p->ai_addr,
301 sizeof (struct sockaddr_storage));
303 lifr.lifr_nd.lnr_state_create = ND_REACHABLE;
304 lifr.lifr_nd.lnr_flags = NDF_STATIC;
307 if (ioctl(s, SIOCLIFSETND, &lifr) < 0) {
308 perror("Unable to set NDP mapping");
314 * Clean up linked list.
316 freeaddrinfo(serverinfo);
320 int main(int argc, char *argv[]) {
323 return (set(argv[1]));
330 .Xr sockaddr_in6 3SOCKET ,
337 .%R Neighbor Discovery for IP version 6