| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <netinet/icmp6.h> |
| 4 |
|
| 5 |
#include "sd-ndisc.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "icmp6-packet.h" |
| 9 |
#include "in-addr-util.h" |
| 10 |
#include "ndisc-internal.h" |
| 11 |
#include "ndisc-neighbor-internal.h" |
| 12 |
#include "ndisc-option.h" |
| 13 |
#include "set.h" |
| 14 |
|
| 15 |
static sd_ndisc_neighbor* ndisc_neighbor_free(sd_ndisc_neighbor *na) { |
| 16 |
if (!na) |
| 17 |
return NULL; |
| 18 |
|
| 19 |
icmp6_packet_unref(na->packet); |
| 20 |
set_free(na->options); |
| 21 |
return mfree(na); |
| 22 |
} |
| 23 |
|
| 24 |
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc_neighbor, sd_ndisc_neighbor, ndisc_neighbor_free); |
| 25 |
|
| 26 |
sd_ndisc_neighbor* ndisc_neighbor_new(ICMP6Packet *packet) { |
| 27 |
sd_ndisc_neighbor *na; |
| 28 |
|
| 29 |
assert(packet); |
| 30 |
|
| 31 |
na = new(sd_ndisc_neighbor, 1); |
| 32 |
if (!na) |
| 33 |
return NULL; |
| 34 |
|
| 35 |
*na = (sd_ndisc_neighbor) { |
| 36 |
.n_ref = 1, |
| 37 |
.packet = icmp6_packet_ref(packet), |
| 38 |
}; |
| 39 |
|
| 40 |
return na; |
| 41 |
} |
| 42 |
|
| 43 |
int ndisc_neighbor_parse(sd_ndisc *nd, sd_ndisc_neighbor *na) { |
| 44 |
int r; |
| 45 |
|
| 46 |
assert(na); |
| 47 |
|
| 48 |
if (na->packet->raw_size < sizeof(struct nd_neighbor_advert)) |
| 49 |
return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), |
| 50 |
"Too small to be a neighbor advertisement, ignoring datagram."); |
| 51 |
|
| 52 |
/* Neighbor advertisement packets are neatly aligned to 64-bit boundaries, hence we can access them directly */ |
| 53 |
const struct nd_neighbor_advert *a = (const struct nd_neighbor_advert*) na->packet->raw_packet; |
| 54 |
assert(a->nd_na_type == ND_NEIGHBOR_ADVERT); |
| 55 |
assert(a->nd_na_code == 0); |
| 56 |
|
| 57 |
na->flags = a->nd_na_flags_reserved; /* the first 3 bits */ |
| 58 |
na->target_address = a->nd_na_target; |
| 59 |
|
| 60 |
/* RFC 4861 section 4.4: |
| 61 |
* For solicited advertisements, the Target Address field in the Neighbor Solicitation message that |
| 62 |
* prompted this advertisement. For an unsolicited advertisement, the address whose link-layer |
| 63 |
* address has changed. The Target Address MUST NOT be a multicast address. |
| 64 |
* |
| 65 |
* Here, we only check if the target address is a link-layer address (or a null address, for safety) |
| 66 |
* when the message is an unsolicited neighbor advertisement. */ |
| 67 |
if (!FLAGS_SET(na->flags, ND_NA_FLAG_SOLICITED)) |
| 68 |
if (!in6_addr_is_link_local(&na->target_address) && !in6_addr_is_null(&na->target_address)) |
| 69 |
return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG), |
| 70 |
"Received ND packet with an invalid target address (%s), ignoring datagram.", |
| 71 |
IN6_ADDR_TO_STRING(&na->target_address)); |
| 72 |
|
| 73 |
r = ndisc_parse_options(na->packet, &na->options); |
| 74 |
if (r < 0) |
| 75 |
return log_ndisc_errno(nd, r, "Failed to parse NDisc options in neighbor advertisement message, ignoring: %m"); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
int sd_ndisc_neighbor_get_sender_address(sd_ndisc_neighbor *na, struct in6_addr *ret) { |
| 81 |
assert_return(na, -EINVAL); |
| 82 |
|
| 83 |
return icmp6_packet_get_sender_address(na->packet, ret); |
| 84 |
} |
| 85 |
|
| 86 |
int sd_ndisc_neighbor_get_target_address(sd_ndisc_neighbor *na, struct in6_addr *ret) { |
| 87 |
assert_return(na, -EINVAL); |
| 88 |
|
| 89 |
if (in6_addr_is_null(&na->target_address)) |
| 90 |
/* fall back to the sender address, for safety. */ |
| 91 |
return sd_ndisc_neighbor_get_sender_address(na, ret); |
| 92 |
|
| 93 |
if (ret) |
| 94 |
*ret = na->target_address; |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
int sd_ndisc_neighbor_get_target_mac(sd_ndisc_neighbor *na, struct ether_addr *ret) { |
| 99 |
assert_return(na, -EINVAL); |
| 100 |
|
| 101 |
return ndisc_option_get_mac(na->options, SD_NDISC_OPTION_TARGET_LL_ADDRESS, ret); |
| 102 |
} |
| 103 |
|
| 104 |
int sd_ndisc_neighbor_get_flags(sd_ndisc_neighbor *na, uint32_t *ret) { |
| 105 |
assert_return(na, -EINVAL); |
| 106 |
|
| 107 |
if (ret) |
| 108 |
*ret = na->flags; |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
int sd_ndisc_neighbor_is_router(sd_ndisc_neighbor *na) { |
| 113 |
assert_return(na, -EINVAL); |
| 114 |
|
| 115 |
return FLAGS_SET(na->flags, ND_NA_FLAG_ROUTER); |
| 116 |
} |
| 117 |
|
| 118 |
int sd_ndisc_neighbor_is_solicited(sd_ndisc_neighbor *na) { |
| 119 |
assert_return(na, -EINVAL); |
| 120 |
|
| 121 |
return FLAGS_SET(na->flags, ND_NA_FLAG_SOLICITED); |
| 122 |
} |
| 123 |
|
| 124 |
int sd_ndisc_neighbor_is_override(sd_ndisc_neighbor *na) { |
| 125 |
assert_return(na, -EINVAL); |
| 126 |
|
| 127 |
return FLAGS_SET(na->flags, ND_NA_FLAG_OVERRIDE); |
| 128 |
} |