HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-dhcp-common.c

51,400 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 3368145981f9f19958f8cb810dea65ab916e8f50

Lines 1001–1517 of 1,517

                        return log_oom();
 
                return 1;
        }
        case DHCP_OPTION_DATA_UINT32: {
                uint32_t u;
 
                r = safe_atou32(p, &u);
                if (r < 0)
                        return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
                u = htobe32(u);
                r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&u, sizeof(u)));
                if (r < 0)
                        return log_oom();
 
                return 1;
        }
        case DHCP_OPTION_DATA_IPV4ADDRESS: {
                union in_addr_union a;
 
                r = in_addr_from_string(AF_INET, p, &a);
                if (r < 0)
                        return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
                r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&a.in, sizeof(a.in)));
                if (r < 0)
                        return log_oom();
 
                return 1;
        }
        case DHCP_OPTION_DATA_IPV6ADDRESS: {
                union in_addr_union a;
 
                r = in_addr_from_string(AF_INET6, p, &a);
                if (r < 0)
                        return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
                r = iovec_done_and_memdup(iov, &IOVEC_MAKE(&a.in6, sizeof(a.in6)));
                if (r < 0)
                        return log_oom();
 
                return 1;
        }
        case DHCP_OPTION_DATA_STRING: {
                _cleanup_free_ char *s = NULL;
                ssize_t sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &s);
                if (sz < 0)
                        return log_syntax_parse_error(unit, filename, line, sz, lvalue, rvalue);
                if (check_length && sz > UINT8_MAX)
                        return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
 
                iovec_done(iov);
                *iov = IOVEC_MAKE(TAKE_PTR(s), sz);
                return 1;
        }
        default:
                return -EINVAL;
        }
}
 
int config_parse_dhcp_option_tlv(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        TLV *options = ASSERT_PTR(data);
        int r;
 
        if (isempty(rvalue)) {
                tlv_done(options);
                return 0;
        }
 
        _cleanup_free_ char *word = NULL;
        const char *p = rvalue;
        r = extract_first_word(&p, &word, ":", 0);
        if (r == -ENOMEM)
                return log_oom();
        if (r <= 0 || isempty(p))
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        uint8_t code;
        r = safe_atou8(word, &code);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
        if (code < 1 || code >= UINT8_MAX)
                return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
 
        _cleanup_(iovec_done) struct iovec iov = {};
        r = config_parse_dhcp_option(unit, filename, line, section, section_line, lvalue, ltype, p, &iov, userdata);
        if (r <= 0)
                return r;
 
        r = tlv_append_iov(options, code, &iov);
        if (r < 0)
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to store '%s=%s', ignoring assignment: %m", lvalue, rvalue);
 
        return 0;
}
 
int config_parse_dhcp_request_options(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Network *network = userdata;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);
 
        if (isempty(rvalue)) {
                if (ltype == AF_INET)
                        network->dhcp_request_options = set_free(network->dhcp_request_options);
                else
                        network->dhcp6_request_options = set_free(network->dhcp6_request_options);
 
                return 0;
        }
 
        for (const char *p = rvalue;;) {
                _cleanup_free_ char *n = NULL;
                uint32_t i;
 
                r = extract_first_word(&p, &n, NULL, 0);
                if (r == -ENOMEM)
                        return log_oom();
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to parse DHCP request option, ignoring assignment: %s",
                                   rvalue);
                        return 0;
                }
                if (r == 0)
                        return 0;
 
                r = safe_atou32(n, &i);
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "DHCP request option is invalid, ignoring assignment: %s", n);
                        continue;
                }
 
                if (i < 1 || i >= UINT8_MAX) {
                        log_syntax(unit, LOG_WARNING, filename, line, 0,
                                   "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
                        continue;
                }
 
                r = set_ensure_put(ltype == AF_INET ? &network->dhcp_request_options : &network->dhcp6_request_options,
                                   NULL, UINT32_TO_PTR(i));
                if (r < 0)
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to store DHCP request option '%s', ignoring assignment: %m", n);
        }
}
 
static const char * const dhcp_option_data_type_table[_DHCP_OPTION_DATA_MAX] = {
        [DHCP_OPTION_DATA_UINT8]       = "uint8",
        [DHCP_OPTION_DATA_UINT16]      = "uint16",
        [DHCP_OPTION_DATA_UINT32]      = "uint32",
        [DHCP_OPTION_DATA_STRING]      = "string",
        [DHCP_OPTION_DATA_IPV4ADDRESS] = "ipv4address",
        [DHCP_OPTION_DATA_IPV6ADDRESS] = "ipv6address",
};
 
DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type, DHCPOptionDataType);
 
static const char* const duid_type_table[_DUID_TYPE_MAX] = {
        [DUID_TYPE_LLT]  = "link-layer-time",
        [DUID_TYPE_EN]   = "vendor",
        [DUID_TYPE_LL]   = "link-layer",
        [DUID_TYPE_UUID] = "uuid",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
 
int config_parse_duid_type(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        _cleanup_free_ char *type_string = NULL;
        const char *p = ASSERT_PTR(rvalue);
        bool force = ltype;
        DUID *duid = ASSERT_PTR(data);
        DUIDType type;
        int r;
 
        assert(filename);
        assert(lvalue);
 
        if (!force && duid->set)
                return 0;
 
        r = extract_first_word(&p, &type_string, ":", 0);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Invalid syntax, ignoring: %s", rvalue);
                return 0;
        }
        if (r == 0) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Failed to extract DUID type from '%s', ignoring.", rvalue);
                return 0;
        }
 
        type = duid_type_from_string(type_string);
        if (type < 0) {
                uint16_t t;
 
                r = safe_atou16(type_string, &t);
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to parse DUID type '%s', ignoring.", type_string);
                        return 0;
                }
 
                type = t;
                assert(type == t); /* Check if type can store uint16_t. */
        }
 
        if (!isempty(p)) {
                usec_t u;
 
                if (type != DUID_TYPE_LLT) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Invalid syntax, ignoring: %s", rvalue);
                        return 0;
                }
 
                r = parse_timestamp(p, &u);
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to parse timestamp, ignoring: %s", p);
                        return 0;
                }
 
                duid->llt_time = u;
        }
 
        duid->type = type;
        duid->set = force;
 
        return 0;
}
 
int config_parse_manager_duid_type(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Manager *manager = ASSERT_PTR(userdata);
        int r;
 
        /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
 
        r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
        if (r < 0)
                return r;
 
        return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
}
 
int config_parse_network_duid_type(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Network *network = ASSERT_PTR(userdata);
        int r;
 
        r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
        if (r < 0)
                return r;
 
        /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
        return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
}
 
int config_parse_duid_rawdata(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        uint8_t raw_data[MAX_DUID_DATA_LEN];
        unsigned count = 0;
        bool force = ltype;
        DUID *duid = ASSERT_PTR(data);
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (!force && duid->set)
                return 0;
 
        /* RawData contains DUID in format "NN:NN:NN..." */
        for (const char *p = rvalue;;) {
                int n1, n2, len, r;
                uint32_t byte;
                _cleanup_free_ char *cbyte = NULL;
 
                r = extract_first_word(&p, &cbyte, ":", 0);
                if (r == -ENOMEM)
                        return log_oom();
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue);
                        return 0;
                }
                if (r == 0)
                        break;
 
                if (count >= MAX_DUID_DATA_LEN) {
                        log_syntax(unit, LOG_WARNING, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue);
                        return 0;
                }
 
                len = strlen(cbyte);
                if (!IN_SET(len, 1, 2)) {
                        log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte, rvalue);
                        return 0;
                }
                n1 = unhexchar(cbyte[0]);
                if (len == 2)
                        n2 = unhexchar(cbyte[1]);
                else
                        n2 = 0;
 
                if (n1 < 0 || n2 < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte, rvalue);
                        return 0;
                }
 
                byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2;
                raw_data[count++] = byte;
        }
 
        assert_cc(sizeof(raw_data) == sizeof(duid->raw_data));
        memcpy(duid->raw_data, raw_data, count);
        duid->raw_data_len = count;
        duid->set = force;
 
        return 0;
}
 
int config_parse_manager_duid_rawdata(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Manager *manager = ASSERT_PTR(userdata);
        int r;
 
        /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
 
        r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
        if (r < 0)
                return r;
 
        return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
}
 
int config_parse_network_duid_rawdata(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Network *network = ASSERT_PTR(userdata);
        int r;
 
        r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
        if (r < 0)
                return r;
 
        /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
        return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
}
 
int config_parse_uplink(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Network *network = ASSERT_PTR(userdata);
        bool accept_none = true;
        int *index, r;
        char **name;
 
        assert(filename);
        assert(section);
        assert(lvalue);
        assert(rvalue);
 
        if (streq(section, "DHCPServer")) {
                index = &network->dhcp_server_uplink_index;
                name = &network->dhcp_server_uplink_name;
        } else if (streq(section, "IPv6SendRA")) {
                index = &network->router_uplink_index;
                name = &network->router_uplink_name;
        } else if (STR_IN_SET(section, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
                index = &network->dhcp_pd_uplink_index;
                name = &network->dhcp_pd_uplink_name;
                accept_none = false;
        } else
                assert_not_reached();
 
        if (isempty(rvalue) || streq(rvalue, ":auto")) {
                *index = UPLINK_INDEX_AUTO;
                *name = mfree(*name);
                return 0;
        }
 
        if (accept_none && streq(rvalue, ":none")) {
                *index = UPLINK_INDEX_NONE;
                *name = mfree(*name);
                return 0;
        }
 
        if (!accept_none && streq(rvalue, ":self")) {
                *index = UPLINK_INDEX_SELF;
                *name = mfree(*name);
                return 0;
        }
 
        r = parse_ifindex(rvalue);
        if (r > 0) {
                *index = r;
                *name = mfree(*name);
                return 0;
        }
 
        if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue);
                return 0;
        }
 
        /* The interface name will be resolved later. */
        r = free_and_strdup_warn(name, rvalue);
        if (r < 0)
                return r;
 
        /* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
         * an uplink interface will be selected automatically. */
        *index = UPLINK_INDEX_AUTO;
        return 0;
}