Debian Codegraph @

src/libsystemd-network/sd-ndisc-router-solicit.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/icmp6.h>
4
5 #include "sd-ndisc-protocol.h"
6 #include "sd-ndisc-router-solicit.h"
7 #include "sd-radv.h"
8
9 #include "alloc-util.h"
10 #include "icmp6-packet.h"
11 #include "ndisc-option.h"
12 #include "ndisc-router-solicit-internal.h"
13 #include "radv-internal.h"
14 #include "set.h"
15
16 static sd_ndisc_router_solicit* ndisc_router_solicit_free(sd_ndisc_router_solicit *rs) {
17 if (!rs)
18 return NULL;
19
20 icmp6_packet_unref(rs->packet);
21 set_free(rs->options);
22 return mfree(rs);
23 }
24
25 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc_router_solicit, sd_ndisc_router_solicit, ndisc_router_solicit_free);
26
27 sd_ndisc_router_solicit* ndisc_router_solicit_new(ICMP6Packet *packet) {
28 sd_ndisc_router_solicit *rs;
29
30 assert(packet);
31
32 rs = new(sd_ndisc_router_solicit, 1);
33 if (!rs)
34 return NULL;
35
36 *rs = (sd_ndisc_router_solicit) {
37 .n_ref = 1,
38 .packet = icmp6_packet_ref(packet),
39 };
40
41 return rs;
42 }
43
44 int ndisc_router_solicit_parse(sd_radv *ra, sd_ndisc_router_solicit *rs) {
45 int r;
46
47 assert(rs);
48 assert(rs->packet);
49
50 if (rs->packet->raw_size < sizeof(struct nd_router_solicit))
51 return log_radv_errno(ra, SYNTHETIC_ERRNO(EBADMSG),
52 "Too small to be a router solicit, ignoring.");
53
54 _unused_ const struct nd_router_solicit *a = (const struct nd_router_solicit*) rs->packet->raw_packet;
55 assert(a);
56 assert(a->nd_rs_type == ND_ROUTER_SOLICIT);
57 assert(a->nd_rs_code == 0);
58
59 r = ndisc_parse_options(rs->packet, &rs->options);
60 if (r < 0)
61 return log_radv_errno(ra, r, "Failed to parse NDisc options in router solicit, ignoring datagram: %m");
62
63 /* RFC 4861 section 4.1.
64 * Source link-layer address:
65 * The link-layer address of the sender, if known. MUST NOT be included if the Source
66 * Address is the unspecified address. Otherwise, it SHOULD be included on link
67 * layers that have addresses. */
68 if (ndisc_option_get_mac(rs->options, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, NULL) >= 0&&
69 sd_ndisc_router_solicit_get_sender_address(rs, NULL) == -ENODATA)
70 return log_radv_errno(ra, SYNTHETIC_ERRNO(EBADMSG),
71 "Router Solicitation message from null address unexpectedly contains source link-layer address option, ignoring datagaram.");
72
73 return 0;
74 }
75
76 int sd_ndisc_router_solicit_get_sender_address(sd_ndisc_router_solicit *rs, struct in6_addr *ret) {
77 assert_return(rs, -EINVAL);
78
79 return icmp6_packet_get_sender_address(rs->packet, ret);
80 }
81
82 int sd_ndisc_router_solicit_get_sender_mac(sd_ndisc_router_solicit *rs, struct ether_addr *ret) {
83 assert_return(rs, -EINVAL);
84
85 return ndisc_option_get_mac(rs->options, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, ret);
86 }