类型 : 选项类型,源链路层地址为 1,目标链路层地址为 2
长度 : 选项长度,如 IEEE 802 地址值为 1
链路层地址 : 源链路层地址(发送者地址,用于 NS、RS 和 RA)或目标链路层地址(目标地址,用于 NA 和 RD)
类型 : 选项类型,固定为 3
长度 : 选项长度,固定为 4
前缀长度 : 取值范围是 0 到 128
L(on-link flag) : 值为 1 时,表示该 prefix 组成的地址是为 on-link,host 必须在在效时间到期后,才能将该 prefix 视为 off-link,但是为 0 并不意味就是 off-link
A(autonomous address-configuration flag) : 主机是否可以使用该 prefix 自动配置 IPv6 地址
有效时间 : 该 prefix 自动配置地址的有效时间和 on-link 地址的有效时间,单位秒,0xFFFFFFFF 表示无限长
偏好时间 : 该 prefix 自动配置的地址处于preferred(偏好)状态的时间(秒数),该值不能大于有效时间,0xFFFFFFFF 表示无限长
前缀 : 前缀内容
类型 : 选项类型,固定为 4
长度 : 选项长度
IP 头 + 数据 : 导致 router 发送重定向消息的原始封包的内容或部分内容,重定向消息长度不能大于1280 字节
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L5PZDNzz-1577417885530)(img/NDP_Opt_MTU.png)]
类型 : 选项类型,固定为 5
长度 : 选项长度,固定为 1
MTU : 接收到该消息的主机应该使用的 IPv6 MTU
# 查看所有邻居节点
ip -6 neigh show
# 删除指定设备上所有邻居节点
sudo ip neigh flush dev eth0
#ifndef __ndp_h_
#define __ndp_h_
/* 参考 linux /usr/include/netinet/icmp6.h */
#define ND_ROUTER_SOLICIT 133
#define ND_ROUTER_ADVERT 134
#define ND_NEIGHBOR_SOLICIT 135
#define ND_NEIGHBOR_ADVERT 136
#define ND_REDIRECT 137
#define ND_OPT_SOURCE_LINKADDR 1
#define ND_OPT_TARGET_LINKADDR 2
#define ND_OPT_PREFIX_INFORMATION 3
#define ND_OPT_REDIRECTED_HEADER 4
#define ND_OPT_MTU 5
#define ND_OPT_RTR_ADV_INTERVAL 7
#define ND_OPT_HOME_AGENT_INFO 8
struct icmp6_hdr {
uint8_t icmp6_type; /* type field */
uint8_t icmp6_code; /* code field */
uint16_t icmp6_cksum; /* checksum field */
union {
uint32_t icmp6_un_data32[1]; /* type-specific field */
uint16_t icmp6_un_data16[2]; /* type-specific field */
uint8_t icmp6_un_data8[4]; /* type-specific field */
} icmp6_dataun;
};
struct nd_router_solicit /* router solicitation */
{
struct icmp6_hdr nd_rs_hdr;
/* could be followed by options */
};
#define nd_rs_type nd_rs_hdr.icmp6_type
#define nd_rs_code nd_rs_hdr.icmp6_code
#define nd_rs_cksum nd_rs_hdr.icmp6_cksum
#define nd_rs_reserved nd_rs_hdr.icmp6_data32[0]
struct nd_router_advert /* router advertisement */
{
struct icmp6_hdr nd_ra_hdr;
uint32_t nd_ra_reachable; /* reachable time */
uint32_t nd_ra_retransmit; /* retransmit timer */
/* could be followed by options */
};
#define nd_ra_type nd_ra_hdr.icmp6_type
#define nd_ra_code nd_ra_hdr.icmp6_code
#define nd_ra_cksum nd_ra_hdr.icmp6_cksum
#define nd_ra_curhoplimit nd_ra_hdr.icmp6_data8[0]
#define nd_ra_flags_reserved nd_ra_hdr.icmp6_data8[1]
#define ND_RA_FLAG_MANAGED 0x80
#define ND_RA_FLAG_OTHER 0x40
#define ND_RA_FLAG_HOME_AGENT 0x20
#define nd_ra_router_lifetime nd_ra_hdr.icmp6_data16[1]
struct nd_neighbor_solicit /* neighbor solicitation */
{
struct icmp6_hdr nd_ns_hdr;
uint8_t nd_ns_target[16]; /* target address */
uint8_t nd_ns_options[0];
};
#define nd_ns_type nd_ns_hdr.icmp6_type
#define nd_ns_code nd_ns_hdr.icmp6_code
#define nd_ns_cksum nd_ns_hdr.icmp6_cksum
#define nd_ns_reserved nd_ns_hdr.icmp6_data32[0]
struct nd_neighbor_advert /* neighbor advertisement */
{
struct icmp6_hdr nd_na_hdr;
uint8_t nd_na_target[16]; /* target address */
uint8_t nd_na_options[0]; /* could be followed by options */
};
#define nd_na_type nd_na_hdr.icmp6_type
#define nd_na_code nd_na_hdr.icmp6_code
#define nd_na_cksum nd_na_hdr.icmp6_cksum
#define nd_na_flags_reserved nd_na_hdr.icmp6_data32[0]
#define ND_NA_FLAG_ROUTER 0x00000080
#define ND_NA_FLAG_SOLICITED 0x00000040
#define ND_NA_FLAG_OVERRIDE 0x00000020
struct nd_redirect /* redirect */
{
struct icmp6_hdr nd_rd_hdr;
uint8_t nd_rd_target[16]; /* target address */
uint8_t nd_rd_dst[16]; /* destination address */
/* could be followed by options */
};
#define nd_rd_type nd_rd_hdr.icmp6_type
#define nd_rd_code nd_rd_hdr.icmp6_code
#define nd_rd_cksum nd_rd_hdr.icmp6_cksum
#define nd_rd_reserved nd_rd_hdr.icmp6_data32[0]
struct nd_opt_hdr /* Neighbor discovery option header */
{
uint8_t nd_opt_type;
uint8_t nd_opt_len; /* in units of 8 octets */
uint8_t nd_opt_data[0]; /* followed by option specific data */
};
struct nd_neighbor_solicit* nd_alloc_ns(const char *taddr,
const struct nd_opt_hdr *opt, size_t size);
void nd_free_ns(struct nd_neighbor_solicit **ns);
void nd_print_na(const struct nd_neighbor_advert *na,
const struct nd_opt_hdr *tar_opt);
struct nd_opt_hdr *nd_alloc_opt_src(const char *smac, int *size);
void nd_free_opt(struct nd_opt_hdr **opt);
int nd_socket(uint8_t hop_limit);
ssize_t nd_send(int sockfd, const void *data, size_t size,
const char *daddr, int flags);
ssize_t nd_recv(int sockfd, void *buf, size_t size,
const char *daddr, int flags);
void nd_close(int sockfd);
#endif /* __ndp_h_ */
#include
#include
#include
#include
#include
#include
#include /* for ether_aton */
#include "ndp.h"
#include "common.h"
struct nd_neighbor_solicit* nd_alloc_ns(const char *taddr,
const struct nd_opt_hdr *opt, size_t size)
{
struct sockaddr_in6 addr;
struct nd_neighbor_solicit *ns;
ns = (struct nd_neighbor_solicit *) calloc(1, sizeof(struct nd_neighbor_solicit) + size);
ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
ns->nd_ns_code = 0;
if (inet_pton(AF_INET6, taddr, &addr.sin6_addr) == 0)
handle_error_en(EINVAL, "taddr");
memcpy(ns->nd_ns_target, &addr.sin6_addr, sizeof(addr.sin6_addr));
if (NULL != opt && size > 0)
memcpy(ns->nd_ns_options, opt, size);
return ns;
}
void nd_free_ns(struct nd_neighbor_solicit **ns)
{
if (NULL != ns && NULL != *ns) {
free(*ns);
*ns = NULL;
}
}
void nd_print_na(const struct nd_neighbor_advert *na,
const struct nd_opt_hdr *tar_opt)
{
char buffer[INET6_ADDRSTRLEN];
printf("%02x\n", na->nd_na_type);
printf("%02x\n", na->nd_na_code);
printf("%04x\n", htons(na->nd_na_cksum));
if (inet_ntop(AF_INET6, na->nd_na_target, buffer, INET6_ADDRSTRLEN) == NULL)
handle_error("inet_ntop");
printf("%s\n", buffer);
printf("%s\n", ether_ntoa((struct ether_addr *) tar_opt->nd_opt_data));
}
struct nd_opt_hdr *nd_alloc_opt_src(const char *smac, int *size)
{
struct ether_addr *addr;
struct nd_opt_hdr *opt;
int tot_len = sizeof(struct nd_opt_hdr) + sizeof(struct ether_addr);
opt = (struct nd_opt_hdr *) calloc(1, tot_len);
opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
opt->nd_opt_len = 1;
addr = ether_aton(smac);
memcpy(opt->nd_opt_data, addr->ether_addr_octet, sizeof(addr->ether_addr_octet));
*size = tot_len;
return opt;
}
void nd_free_opt(struct nd_opt_hdr **opt)
{
if (NULL != opt && NULL != *opt) {
free(*opt);
*opt = NULL;
}
}
int nd_socket(uint8_t hop_limit)
{
int sockfd;
int hops = hop_limit;
if ((sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
handle_error("socket");
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops)) == -1)
handle_error("setsockopt : IPV6_HOPLIMIT");
return sockfd;
}
ssize_t nd_send(int sockfd, const void *data, size_t size,
const char *daddr, int flags)
{
ssize_t count;
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
inet_pton(addr.sin6_family, daddr, &addr.sin6_addr);
if ((count = sendto(sockfd, data, size, flags, (struct sockaddr *)&addr, sizeof(addr))) == -1)
handle_error("sendto");
return count;
}
ssize_t nd_recv(int sockfd, void *buf, size_t size,
const char *daddr, int flags)
{
ssize_t count;
struct sockaddr_in6 addr;
socklen_t socklen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
inet_pton(addr.sin6_family, daddr, &addr.sin6_addr);
if ((count = recvfrom(sockfd, buf, size, flags, (struct sockaddr *)&addr, &socklen)) == -1)
handle_error("recvfrom");
return count;
}
void nd_close(int sockfd)
{
if (close(sockfd) == -1)
handle_error("close");
}
#include
#include
#include
#include
#include
#include "ndp.h"
#include "ipv6.h"
#define BUFFER_SIZE 1500
static void ndp_addr_resolution(const char *smac, const char *saddr,
const char *daddr, const char *taddr)
{
int sockfd, opt_len, tot_len;
struct nd_opt_hdr *opt;
struct nd_neighbor_advert *na;
struct nd_neighbor_solicit *ns;
char buffer[BUFFER_SIZE];
sockfd = nd_socket(255); // 跳数限制为 255,保证不会跑远(不能转发或者路由)
// 构建消息
opt = nd_alloc_opt_src(smac, &opt_len); // 设置自己的链接层地址
ns = nd_alloc_ns(taddr, opt, opt_len);
tot_len = sizeof(struct nd_neighbor_solicit) + opt_len;
ns->nd_ns_cksum = ipv6_cksum(saddr, daddr, IPPROTO_ICMPV6, ns, tot_len);
// 发送消息
nd_send(sockfd, ns, tot_len, daddr, 0);
nd_free_ns(&ns);
nd_free_opt(&opt);
// 接收消息
memset(buffer, 0, sizeof(buffer));
nd_recv(sockfd, buffer, sizeof(buffer), taddr, 0);
// 解析消息
na = (struct nd_neighbor_advert *) buffer;
opt = (struct nd_opt_hdr *) (buffer + sizeof(struct nd_neighbor_advert));
nd_print_na(na, opt);
nd_close(sockfd);
}
int main(int argc, char *argv[])
{
const char *smac = "00:0c:0c:0c:0c:0c"; // 发送者链路层地址
const char *saddr = "fe80::20c:cff:fe0c:c0c"; // 本机 IPv6 地址
const char *daddr = "ff02::1:ff0d:d0d"; // 发给组播地址
const char *taddr = "fe80::20d:dff:fe0d:d0d"; // 待解析 IPv6 地址
ndp_addr_resolution(smac, saddr, daddr, taddr);
return 0;
}
192.168.2.100> gcc -Wall -g -o ndp ipv6.c ndp.c cksum.c main.c && watch sudo ./ndp
88
00
b087
fe80::20d:dff:fe0d:d0d
0:d:d:d:d:d # 解析出来的 link 层地址
192.168.2.200> sudo tcpdump -nt -XX icmp6
IP6 fe80::20c:cff:fe0c:c0c > ff02::1:ff0d:d0d: ICMP6, neighbor solicitation, who has fe80::20d:dff:fe0d:d0d, length 32
0x0000: 3333 ff0d 0d0d 000c 0c0c 0c0c 86dd 6005 # 以太网地址第一位为奇数,表示组播地址
0x0010: cbe6 0020 3aff fe80 0000 0000 0000 020c ....:...........
0x0020: 0cff fe0c 0c0c ff02 0000 0000 0000 0000 ................
0x0030: 0001 ff0d 0d0d 8700 2314 0000 0000 fe80 ........#.......
0x0040: 0000 0000 0000 020d 0dff fe0d 0d0d 0101 ................
0x0050: 000c 0c0c 0c0c ......
IP6 fe80::20d:dff:fe0d:d0d > fe80::20c:cff:fe0c:c0c: ICMP6, neighbor advertisement, tgt is fe80::20d:dff:fe0d:d0d, length 32
0x0000: 000c 0c0c 0c0c 000d 0d0d 0d0d 86dd 6000 ..............`.
0x0010: 0000 0020 3aff fe80 0000 0000 0000 020d ....:...........
0x0020: 0dff fe0d 0d0d fe80 0000 0000 0000 020c ................
0x0030: 0cff fe0c 0c0c 8800 b087 6000 0000 fe80 ..........`.....
0x0040: 0000 0000 0000 020d 0dff fe0d 0d0d 0201 ................
0x0050: 000d 0d0d 0d0d ......
https://tools.ietf.org/html/rfc4861
https://tools.ietf.org/html/rfc2461
http://hanteye01.blog.fc2.com/blog-entry-4.html
http://www.voidcn.com/article/p-gbyohzqn-vs.html
https://wenku.baidu.com/view/ebd52dc089eb172dec63b702.html?sxts=1571573449497
http://xq.dropsec.xyz/2018/10/19/IPv6-NDP%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/
https://cshihong.github.io/2018/01/29/IPv6%E9%82%BB%E5%B1%85%E5%8F%91%E7%8E%B0%E5%8D%8F%E8%AE%AE/