1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-dhcp-lease.h"
5 #include "sd-netlink.h"
6 
7 #include "conf-parser.h"
8 #include "hashmap.h"
9 #include "log.h"
10 #include "macro.h"
11 #include "network-util.h"
12 #include "string-util.h"
13 
14 typedef struct Link Link;
15 
16 typedef enum NetworkConfigSource {
17         NETWORK_CONFIG_SOURCE_FOREIGN, /* configured by kernel */
18         NETWORK_CONFIG_SOURCE_STATIC,
19         NETWORK_CONFIG_SOURCE_IPV4LL,
20         NETWORK_CONFIG_SOURCE_DHCP4,
21         NETWORK_CONFIG_SOURCE_DHCP6,
22         NETWORK_CONFIG_SOURCE_DHCP_PD,
23         NETWORK_CONFIG_SOURCE_NDISC,
24         NETWORK_CONFIG_SOURCE_RUNTIME, /* through D-Bus method */
25         _NETWORK_CONFIG_SOURCE_MAX,
26         _NETWORK_CONFIG_SOURCE_INVALID = -EINVAL,
27 } NetworkConfigSource;
28 
29 typedef enum NetworkConfigState {
30         NETWORK_CONFIG_STATE_PROBING     = 1 << 0, /* address is probing by IPv4ACD */
31         NETWORK_CONFIG_STATE_REQUESTING  = 1 << 1, /* request is queued */
32         NETWORK_CONFIG_STATE_CONFIGURING = 1 << 2, /* e.g. address_configure() is called, but no response is received yet */
33         NETWORK_CONFIG_STATE_CONFIGURED  = 1 << 3, /* e.g. address_configure() is called and received a response from kernel.
34                                                     * Note that address may not be ready yet, so please use address_is_ready()
35                                                     * to check whether the address can be usable or not. */
36         NETWORK_CONFIG_STATE_MARKED      = 1 << 4, /* used GC'ing the old config */
37         NETWORK_CONFIG_STATE_REMOVING    = 1 << 5, /* e.g. address_remove() is called, but no response is received yet */
38 } NetworkConfigState;
39 
40 CONFIG_PARSER_PROTOTYPE(config_parse_link_local_address_family);
41 CONFIG_PARSER_PROTOTYPE(config_parse_address_family_with_kernel);
42 CONFIG_PARSER_PROTOTYPE(config_parse_ip_masquerade);
43 CONFIG_PARSER_PROTOTYPE(config_parse_mud_url);
44 
45 const char *network_config_source_to_string(NetworkConfigSource s) _const_;
46 
47 int network_config_state_to_string_alloc(NetworkConfigState s, char **ret);
48 
49 #define DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(type, name)               \
50         static inline void name##_update_state(                         \
51                         type *t,                                        \
52                         NetworkConfigState mask,                        \
53                         NetworkConfigState value) {                     \
54                                                                         \
55                 assert(t);                                              \
56                                                                         \
57                 t->state = (t->state & ~mask) | (value & mask);         \
58         }                                                               \
59         static inline bool name##_exists(type *t) {                     \
60                 assert(t);                                              \
61                                                                         \
62                 if ((t->state & (NETWORK_CONFIG_STATE_CONFIGURING |     \
63                                  NETWORK_CONFIG_STATE_CONFIGURED)) == 0) \
64                         return false; /* Not assigned yet. */           \
65                 if (FLAGS_SET(t->state, NETWORK_CONFIG_STATE_REMOVING)) \
66                         return false; /* Already removing. */           \
67                 return true;                                            \
68         }                                                               \
69         static inline void name##_enter_requesting(type *t) {           \
70                 name##_update_state(t,                                  \
71                                     NETWORK_CONFIG_STATE_REQUESTING,    \
72                                     NETWORK_CONFIG_STATE_REQUESTING);   \
73         }                                                               \
74         static inline void name##_cancel_requesting(type *t) {          \
75                 name##_update_state(t,                                  \
76                                     NETWORK_CONFIG_STATE_REQUESTING,    \
77                                     0);                                 \
78         }                                                               \
79         static inline bool name##_is_requesting(type *t) {              \
80                 return FLAGS_SET(t->state, NETWORK_CONFIG_STATE_REQUESTING); \
81         }                                                               \
82         static inline void name##_enter_configuring(type *t) {          \
83                 name##_update_state(t,                                  \
84                                     NETWORK_CONFIG_STATE_REQUESTING |   \
85                                     NETWORK_CONFIG_STATE_CONFIGURING,   \
86                                     NETWORK_CONFIG_STATE_CONFIGURING);  \
87         }                                                               \
88         static inline void name##_enter_configured(type *t) {           \
89                 name##_update_state(t,                                  \
90                                     NETWORK_CONFIG_STATE_CONFIGURING |  \
91                                     NETWORK_CONFIG_STATE_CONFIGURED,    \
92                                     NETWORK_CONFIG_STATE_CONFIGURED);   \
93         }                                                               \
94         static inline void name##_mark(type *t) {                       \
95                 name##_update_state(t,                                  \
96                                     NETWORK_CONFIG_STATE_MARKED,        \
97                                     NETWORK_CONFIG_STATE_MARKED);       \
98         }                                                               \
99         static inline void name##_unmark(type *t) {                     \
100                 name##_update_state(t, NETWORK_CONFIG_STATE_MARKED, 0); \
101         }                                                               \
102         static inline bool name##_is_marked(type *t) {                  \
103                 assert(t);                                              \
104                 return FLAGS_SET(t->state, NETWORK_CONFIG_STATE_MARKED); \
105         }                                                               \
106         static inline void name##_enter_removing(type *t) {             \
107                 name##_update_state(t,                                  \
108                                     NETWORK_CONFIG_STATE_MARKED |       \
109                                     NETWORK_CONFIG_STATE_REMOVING,      \
110                                     NETWORK_CONFIG_STATE_REMOVING);     \
111         }                                                               \
112         static inline void name##_enter_removed(type *t) {              \
113                 name##_update_state(t,                                  \
114                                     NETWORK_CONFIG_STATE_CONFIGURED |   \
115                                     NETWORK_CONFIG_STATE_REMOVING,      \
116                                     0);                                 \
117         }
118 
119 const char *address_family_to_string(AddressFamily b) _const_;
120 AddressFamily address_family_from_string(const char *s) _pure_;
121 
122 AddressFamily link_local_address_family_from_string(const char *s) _pure_;
123 
124 const char *routing_policy_rule_address_family_to_string(AddressFamily b) _const_;
125 AddressFamily routing_policy_rule_address_family_from_string(const char *s) _pure_;
126 
127 const char *nexthop_address_family_to_string(AddressFamily b) _const_;
128 AddressFamily nexthop_address_family_from_string(const char *s) _pure_;
129 
130 const char *duplicate_address_detection_address_family_to_string(AddressFamily b) _const_;
131 AddressFamily duplicate_address_detection_address_family_from_string(const char *s) _pure_;
132 
133 AddressFamily dhcp_deprecated_address_family_from_string(const char *s) _pure_;
134 
135 const char *dhcp_lease_server_type_to_string(sd_dhcp_lease_server_type_t t) _const_;
136 sd_dhcp_lease_server_type_t dhcp_lease_server_type_from_string(const char *s) _pure_;
137 
138 int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg);
139 #define log_link_message_error_errno(link, m, err, msg)   log_link_message_full_errno(link, m, LOG_ERR, err, msg)
140 #define log_link_message_warning_errno(link, m, err, msg) log_link_message_full_errno(link, m, LOG_WARNING, err, msg)
141 #define log_link_message_notice_errno(link, m, err, msg)  log_link_message_full_errno(link, m, LOG_NOTICE, err, msg)
142 #define log_link_message_info_errno(link, m, err, msg)    log_link_message_full_errno(link, m, LOG_INFO, err, msg)
143 #define log_link_message_debug_errno(link, m, err, msg)   log_link_message_full_errno(link, m, LOG_DEBUG, err, msg)
144 #define log_message_full_errno(m, level, err, msg)        log_link_message_full_errno(NULL, m, level, err, msg)
145 #define log_message_error_errno(m, err, msg)              log_message_full_errno(m, LOG_ERR, err, msg)
146 #define log_message_warning_errno(m, err, msg)            log_message_full_errno(m, LOG_WARNING, err, msg)
147 #define log_message_notice_errno(m, err, msg)             log_message_full_errno(m, LOG_NOTICE, err, msg)
148 #define log_message_info_errno(m, err, msg)               log_message_full_errno(m, LOG_INFO, err, msg)
149 #define log_message_debug_errno(m, err, msg)              log_message_full_errno(m, LOG_DEBUG, err, msg)
150