Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * dhcpcd - DHCP client daemon
      3  * SPDX-License-Identifier: BSD-2-Clause
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) marples.name>
      5  * All rights reserved
      6 
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #ifndef DHCPCOMMON_H
     30 #define DHCPCOMMON_H
     31 
     32 #include <netinet/in.h>
     33 
     34 #include <arpa/inet.h>
     35 #include <arpa/nameser.h> /* after normal includes for sunos */
     36 #include <stdint.h>
     37 
     38 #include "common.h"
     39 #include "dhcpcd.h"
     40 
     41 /* Support very old arpa/nameser.h as found in OpenBSD */
     42 #ifndef NS_MAXDNAME
     43 #define NS_MAXCDNAME MAXCDNAME
     44 #define NS_MAXDNAME  MAXDNAME
     45 #define NS_MAXLABEL  MAXLABEL
     46 #endif
     47 
     48 #define OT_REQUEST   (1 << 0)
     49 #define OT_UINT8     (1 << 1)
     50 #define OT_INT8	     (1 << 2)
     51 #define OT_UINT16    (1 << 3)
     52 #define OT_INT16     (1 << 4)
     53 #define OT_UINT32    (1 << 5)
     54 #define OT_INT32     (1 << 6)
     55 #define OT_ADDRIPV4  (1 << 7)
     56 #define OT_STRING    (1 << 8)
     57 #define OT_ARRAY     (1 << 9)
     58 #define OT_RFC3361   (1 << 10)
     59 #define OT_RFC1035   (1 << 11)
     60 #define OT_RFC3442   (1 << 12)
     61 #define OT_OPTIONAL  (1 << 13)
     62 #define OT_ADDRIPV6  (1 << 14)
     63 #define OT_BINHEX    (1 << 15)
     64 #define OT_FLAG	     (1 << 16)
     65 #define OT_NOREQ     (1 << 17)
     66 #define OT_EMBED     (1 << 18)
     67 #define OT_ENCAP     (1 << 19)
     68 #define OT_INDEX     (1 << 20)
     69 #define OT_OPTION    (1 << 21)
     70 #define OT_DOMAIN    (1 << 22)
     71 #define OT_ASCII     (1 << 23)
     72 #define OT_RAW	     (1 << 24)
     73 #define OT_ESCSTRING (1 << 25)
     74 #define OT_ESCFILE   (1 << 26)
     75 #define OT_BITFLAG   (1 << 27)
     76 #define OT_RESERVED  (1 << 28)
     77 #define OT_URI	     (1 << 29)
     78 #define OT_TRUNCATED (1 << 30)
     79 
     80 struct dhcp_opt {
     81 	uint32_t option; /* Also used for IANA Enterpise Number */
     82 	int type;
     83 	size_t len;
     84 	char *var;
     85 
     86 	int index; /* Index counter for many instances of the same option */
     87 	char bitflags[8];
     88 
     89 	/* Embedded options.
     90 	 * The option code is irrelevant here. */
     91 	struct dhcp_opt *embopts;
     92 	size_t embopts_len;
     93 
     94 	/* Encapsulated options */
     95 	struct dhcp_opt *encopts;
     96 	size_t encopts_len;
     97 };
     98 
     99 struct dho_policy_ctx {
    100 	const struct dhcp_opt *dopts;
    101 	size_t dopts_len;
    102 	const struct dhcp_opt *odopts;
    103 	size_t odopts_len;
    104 };
    105 
    106 const char *dhcp_get_hostname(struct dhcpcd_ctx *, char *, size_t,
    107     const struct if_options *);
    108 struct dhcp_opt *vivso_find(uint32_t, const void *);
    109 
    110 ssize_t dhcp_vendor(char *, size_t);
    111 
    112 void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols);
    113 const char *dhcp_option_string(const struct dhcp_opt *, size_t, uint32_t);
    114 
    115 int dho_policy_has(const struct dho_policy *policy, uint32_t option);
    116 int dho_policy_add(struct dho_policy *policy, uint32_t option);
    117 int dho_policy_del(struct dho_policy *policy, uint32_t option);
    118 int dho_policy_set(const struct dho_policy_ctx *, struct dho_policy *policy,
    119     const char *opts, int add);
    120 void dho_policy_free(struct dho_policy);
    121 void dho_policy_group_free(struct dho_policy_group);
    122 int dho_policy_check(const struct dho_policy *, int (*)(uint32_t, void *),
    123     void *);
    124 
    125 int dho_policy_requested(const struct dho_policy_group *, uint32_t);
    126 int dho_policy_opt_requested(const struct dho_policy_group *,
    127     const struct dhcp_opt *);
    128 int dho_policy_removed(const struct dho_policy_group *, uint32_t);
    129 int dho_policy_allowed(const struct dho_policy_group *, uint32_t);
    130 
    131 size_t encode_rfc1035(const char *src, uint8_t *dst);
    132 ssize_t decode_rfc1035(char *, size_t, const uint8_t *, size_t);
    133 ssize_t print_string(char *, size_t, int, const void *, size_t);
    134 int dhcp_set_leasefile(char *, size_t, int, const struct interface *);
    135 
    136 void dhcp_envoption(struct dhcpcd_ctx *, FILE *, const char *, const char *,
    137     struct dhcp_opt *,
    138     const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, size_t *, unsigned int *,
    139 	size_t *, const uint8_t *, size_t, struct dhcp_opt **),
    140     const uint8_t *od, size_t ol);
    141 void dhcp_zero_index(struct dhcp_opt *);
    142 
    143 ssize_t dhcp_readfile(struct dhcpcd_ctx *, const char *, void **, size_t *);
    144 ssize_t dhcp_writefile(struct dhcpcd_ctx *, const char *, mode_t, const void *,
    145     size_t);
    146 int dhcp_filemtime(struct dhcpcd_ctx *, const char *, time_t *);
    147 int dhcp_unlink(struct dhcpcd_ctx *, const char *);
    148 size_t dhcp_read_hwaddr_aton(struct dhcpcd_ctx *, uint8_t **, const char *);
    149 #endif
    150