Debian Codegraph @

src/libsystemd-network/sd-ndisc-redirect.c

raw
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-option.h"
12 #include "ndisc-redirect-internal.h"
13 #include "set.h"
14
15 static sd_ndisc_redirect* ndisc_redirect_free(sd_ndisc_redirect *rd) {
16 if (!rd)
17 return NULL;
18
19 icmp6_packet_unref(rd->packet);
20 set_free(rd->options);
21 return mfree(rd);
22 }
23
24 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc_redirect, sd_ndisc_redirect, ndisc_redirect_free);
25
26 sd_ndisc_redirect* ndisc_redirect_new(ICMP6Packet *packet) {
27 sd_ndisc_redirect *rd;
28
29 assert(packet);
30
31 rd = new(sd_ndisc_redirect, 1);
32 if (!rd)
33 return NULL;
34
35 *rd = (sd_ndisc_redirect) {
36 .n_ref = 1,
37 .packet = icmp6_packet_ref(packet),
38 };
39
40 return rd;
41 }
42
43 int ndisc_redirect_parse(sd_ndisc *nd, sd_ndisc_redirect *rd) {
44 int r;
45
46 assert(rd);
47 assert(rd->packet);
48
49 if (rd->packet->raw_size < sizeof(struct nd_redirect))
50 return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG),
51 "Too small to be a redirect message, ignoring.");
52
53 const struct nd_redirect *a = (const struct nd_redirect*) rd->packet->raw_packet;
54 assert(a->nd_rd_type == ND_REDIRECT);
55 assert(a->nd_rd_code == 0);
56
57 rd->target_address = a->nd_rd_target;
58 rd->destination_address = a->nd_rd_dst;
59
60 /* RFC 4861 section 8.1
61 * The ICMP Destination Address field in the redirect message does not contain a multicast address. */
62 if (in6_addr_is_null(&rd->destination_address) || in6_addr_is_multicast(&rd->destination_address))
63 return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG),
64 "Received Redirect message with an invalid destination address, ignoring datagram.");
65
66 /* RFC 4861 section 8.1
67 * The ICMP Target Address is either a link-local address (when redirected to a router) or the same
68 * as the ICMP Destination Address (when redirected to the on-link destination). */
69 if (!in6_addr_is_link_local(&rd->target_address) && !in6_addr_equal(&rd->target_address, &rd->destination_address))
70 return log_ndisc_errno(nd, SYNTHETIC_ERRNO(EBADMSG),
71 "Received Redirect message with an invalid target address, ignoring datagram.");
72
73 r = ndisc_parse_options(rd->packet, &rd->options);
74 if (r < 0)
75 return log_ndisc_errno(nd, r, "Failed to parse NDisc options in Redirect message, ignoring datagram: %m");
76
77 return 0;
78 }
79
80 int sd_ndisc_redirect_set_sender_address(sd_ndisc_redirect *rd, const struct in6_addr *addr) {
81 assert_return(rd, -EINVAL);
82
83 return icmp6_packet_set_sender_address(rd->packet, addr);
84 }
85
86 int sd_ndisc_redirect_get_sender_address(sd_ndisc_redirect *rd, struct in6_addr *ret) {
87 assert_return(rd, -EINVAL);
88
89 return icmp6_packet_get_sender_address(rd->packet, ret);
90 }
91
92 int sd_ndisc_redirect_get_target_address(sd_ndisc_redirect *rd, struct in6_addr *ret) {
93 assert_return(rd, -EINVAL);
94
95 if (in6_addr_is_null(&rd->target_address))
96 return -ENODATA;
97
98 if (ret)
99 *ret = rd->target_address;
100 return 0;
101 }
102
103 int sd_ndisc_redirect_get_destination_address(sd_ndisc_redirect *rd, struct in6_addr *ret) {
104 assert_return(rd, -EINVAL);
105
106 if (in6_addr_is_null(&rd->destination_address))
107 return -ENODATA;
108
109 if (ret)
110 *ret = rd->destination_address;
111 return 0;
112 }
113
114 int sd_ndisc_redirect_get_target_mac(sd_ndisc_redirect *rd, struct ether_addr *ret) {
115 assert_return(rd, -EINVAL);
116
117 return ndisc_option_get_mac(rd->options, SD_NDISC_OPTION_TARGET_LL_ADDRESS, ret);
118 }
119
120 int sd_ndisc_redirect_get_redirected_header(sd_ndisc_redirect *rd, struct ip6_hdr *ret) {
121 assert_return(rd, -EINVAL);
122
123 sd_ndisc_option *p = ndisc_option_get_by_type(rd->options, SD_NDISC_OPTION_REDIRECTED_HEADER);
124 if (!p)
125 return -ENODATA;
126
127 if (ret)
128 *ret = p->hdr;
129 return 0;
130 }