]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd-network/sd-radv.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libsystemd-network / sd-radv.c
index 70772b4f150285ca2bd94c7aea86151725f4f1b9..46704acdefecda888b45adee09d4da0f61d54a8b 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
 
 #include "macro.h"
 #include "alloc-util.h"
+#include "dns-domain.h"
 #include "fd-util.h"
 #include "icmp6-util.h"
 #include "in-addr-util.h"
 #include "radv-internal.h"
 #include "socket-util.h"
 #include "string-util.h"
+#include "strv.h"
 #include "util.h"
 #include "random-util.h"
 
@@ -127,6 +130,7 @@ _public_ sd_radv *sd_radv_unref(sd_radv *ra) {
         }
 
         free(ra->rdnss);
+        free(ra->dnssl);
 
         radv_reset(ra);
 
@@ -157,8 +161,9 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst,
                 .nd_opt_mtu_type = ND_OPT_MTU,
                 .nd_opt_mtu_len = 1,
         };
-        /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, RDNSS */
-        struct iovec iov[4 + ra->n_prefixes];
+        /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, RDNSS
+           and DNSSL */
+        struct iovec iov[5 + ra->n_prefixes];
         struct msghdr msg = {
                 .msg_name = &dst_addr,
                 .msg_namelen = sizeof(dst_addr),
@@ -204,6 +209,12 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst,
                 msg.msg_iovlen++;
         }
 
+        if (ra->dnssl) {
+                iov[msg.msg_iovlen].iov_base = ra->dnssl;
+                iov[msg.msg_iovlen].iov_len = ra->dnssl->length * 8;
+                msg.msg_iovlen++;
+        }
+
         if (sendmsg(ra->fd, &msg, 0) < 0)
                 return -errno;
 
@@ -590,6 +601,58 @@ _public_ int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime,
         return 0;
 }
 
+_public_ int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime,
+                               char **search_list) {
+        _cleanup_free_ struct sd_radv_opt_dns *opt_dnssl = NULL;
+        size_t len = 0;
+        char **s;
+        uint8_t *p;
+
+        assert_return(ra, -EINVAL);
+
+        if (!search_list || *search_list == NULL) {
+                ra->dnssl = mfree(ra->dnssl);
+
+                return 0;
+        }
+
+        STRV_FOREACH(s, search_list)
+                len += strlen(*s) + 2;
+
+        len = (sizeof(struct sd_radv_opt_dns) + len + 7) & ~0x7;
+
+        opt_dnssl = malloc0(len);
+        if (!opt_dnssl)
+                return -ENOMEM;
+
+        opt_dnssl->type = SD_RADV_OPT_DNSSL;
+        opt_dnssl->length = len / 8;
+        opt_dnssl->lifetime = htobe32(lifetime);
+
+        p = (uint8_t *)(opt_dnssl + 1);
+        len -= sizeof(struct sd_radv_opt_dns);
+
+        STRV_FOREACH(s, search_list) {
+                int r;
+
+                r = dns_name_to_wire_format(*s, p, len, false);
+                if (r < 0)
+                        return r;
+
+                if (len < (size_t)r)
+                        return -ENOBUFS;
+
+                p += r;
+                len -= r;
+        }
+
+        free(ra->dnssl);
+        ra->dnssl = opt_dnssl;
+        opt_dnssl = NULL;
+
+        return 0;
+}
+
 _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) {
         _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;