1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <arpa/inet.h>
29 #include "sd-dhcp-client.h"
30 #include "sd-dhcp-server.h"
31 #include "sd-ipv4ll.h"
34 #include "rtnl-util.h"
38 #include "condition-util.h"
40 #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU
42 typedef struct NetDev NetDev;
43 typedef struct Network Network;
44 typedef struct Link Link;
45 typedef struct Address Address;
46 typedef struct Route Route;
47 typedef struct Manager Manager;
49 typedef struct netdev_enslave_callback netdev_enslave_callback;
51 struct netdev_enslave_callback {
52 sd_rtnl_message_handler_t callback;
55 LIST_FIELDS(netdev_enslave_callback, callbacks);
58 typedef enum MacVlanMode {
59 NETDEV_MACVLAN_MODE_PRIVATE = MACVLAN_MODE_PRIVATE,
60 NETDEV_MACVLAN_MODE_VEPA = MACVLAN_MODE_VEPA,
61 NETDEV_MACVLAN_MODE_BRIDGE = MACVLAN_MODE_BRIDGE,
62 NETDEV_MACVLAN_MODE_PASSTHRU = MACVLAN_MODE_PASSTHRU,
63 _NETDEV_MACVLAN_MODE_MAX,
64 _NETDEV_MACVLAN_MODE_INVALID = -1
67 typedef enum NetDevKind {
78 _NETDEV_KIND_INVALID = -1
81 typedef enum NetDevState {
83 NETDEV_STATE_CREATING,
87 _NETDEV_STATE_INVALID = -1,
97 Condition *match_host;
98 Condition *match_virt;
99 Condition *match_kernel;
100 Condition *match_arch;
106 struct ether_addr *mac;
107 struct ether_addr *mac_peer;
111 int32_t macvlan_mode;
116 bool tunnel_pmtudisc;
119 struct in_addr tunnel_local;
120 struct in_addr tunnel_remote;
122 LIST_HEAD(netdev_enslave_callback, callbacks);
130 struct ether_addr *match_mac;
135 Condition *match_host;
136 Condition *match_virt;
137 Condition *match_kernel;
138 Condition *match_arch;
151 bool dhcp_domainname;
157 LIST_HEAD(Address, static_addresses);
158 LIST_HEAD(Route, static_routes);
160 Hashmap *addresses_by_section;
161 Hashmap *routes_by_section;
163 LIST_HEAD(Address, dns);
164 LIST_HEAD(Address, ntp);
166 LIST_FIELDS(Network, networks);
173 unsigned char family;
174 unsigned char prefixlen;
178 struct in_addr broadcast;
179 struct ifa_cacheinfo cinfo;
186 LIST_FIELDS(Address, addresses);
193 unsigned char family;
194 unsigned char dst_prefixlen;
208 LIST_FIELDS(Route, routes);
211 typedef enum LinkState {
212 LINK_STATE_INITIALIZING,
213 LINK_STATE_ENSLAVING,
214 LINK_STATE_SETTING_ADDRESSES,
215 LINK_STATE_SETTING_ROUTES,
216 LINK_STATE_CONFIGURED,
217 LINK_STATE_UNMANAGED,
221 _LINK_STATE_INVALID = -1
224 typedef enum LinkOperationalState {
225 LINK_OPERSTATE_UNKNOWN,
226 LINK_OPERSTATE_DORMANT,
227 LINK_OPERSTATE_CARRIER,
228 LINK_OPERSTATE_DEGRADED,
229 LINK_OPERSTATE_ROUTABLE,
231 _LINK_OPERSTATE_INVALID = -1
232 } LinkOperationalState;
242 struct ether_addr mac;
243 struct udev_device *udev_device;
246 uint8_t kernel_operstate;
251 LinkOperationalState operstate;
253 unsigned addr_messages;
254 unsigned route_messages;
257 LIST_HEAD(Address, addresses);
259 sd_dhcp_client *dhcp_client;
260 sd_dhcp_lease *dhcp_lease;
262 uint16_t original_mtu;
265 sd_dhcp_server *dhcp_server;
273 struct udev_monitor *udev_monitor;
274 sd_event_source *udev_event_source;
275 sd_event_source *sigterm_event_source;
276 sd_event_source *sigint_event_source;
282 LIST_HEAD(Network, networks);
284 usec_t network_dirs_ts_usec;
287 extern const char* const network_dirs[];
291 int manager_new(Manager **ret);
292 void manager_free(Manager *m);
294 int manager_load_config(Manager *m);
295 bool manager_should_reload(Manager *m);
297 int manager_rtnl_enumerate_links(Manager *m);
299 int manager_rtnl_listen(Manager *m);
300 int manager_udev_listen(Manager *m);
301 int manager_bus_listen(Manager *m);
303 int manager_save(Manager *m);
305 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
306 #define _cleanup_manager_free_ _cleanup_(manager_freep)
310 int netdev_load(Manager *manager);
311 void netdev_drop(NetDev *netdev);
313 NetDev *netdev_unref(NetDev *netdev);
314 NetDev *netdev_ref(NetDev *netdev);
316 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
317 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
319 int netdev_get(Manager *manager, const char *name, NetDev **ret);
320 int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
321 int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
322 int netdev_create_tunnel(Link *link, sd_rtnl_message_handler_t callback);
323 int netdev_create_veth(NetDev *netdev, sd_rtnl_message_handler_t callback);
325 const char *netdev_kind_to_string(NetDevKind d) _const_;
326 NetDevKind netdev_kind_from_string(const char *d) _pure_;
328 const char *macvlan_mode_to_string(MacVlanMode d) _const_;
329 MacVlanMode macvlan_mode_from_string(const char *d) _pure_;
331 int config_parse_netdev_kind(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);
333 int config_parse_macvlan_mode(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);
336 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
340 int network_load(Manager *manager);
342 void network_free(Network *network);
344 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
345 #define _cleanup_network_free_ _cleanup_(network_freep)
347 int network_get(Manager *manager, struct udev_device *device,
348 const char *ifname, const struct ether_addr *mac,
350 int network_apply(Manager *manager, Network *network, Link *link);
352 int config_parse_netdev(const char *unit, const char *filename, unsigned line,
353 const char *section, unsigned section_line, const char *lvalue,
354 int ltype, const char *rvalue, void *data, void *userdata);
356 int config_parse_tunnel(const char *unit,
357 const char *filename,
360 unsigned section_line,
367 int config_parse_tunnel_address(const char *unit,
368 const char *filename,
371 unsigned section_line,
379 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);
382 int route_new_static(Network *network, unsigned section, Route **ret);
383 int route_new_dynamic(Route **ret);
384 void route_free(Route *route);
385 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
386 int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
389 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
390 #define _cleanup_route_free_ _cleanup_(route_freep)
392 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
393 const char *section, unsigned section_line, const char *lvalue,
394 int ltype, const char *rvalue, void *data, void *userdata);
396 int config_parse_destination(const char *unit, const char *filename, unsigned line,
397 const char *section, unsigned section_line, const char *lvalue,
398 int ltype, const char *rvalue, void *data, void *userdata);
401 int address_new_static(Network *network, unsigned section, Address **ret);
402 int address_new_dynamic(Address **ret);
403 void address_free(Address *address);
404 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
405 int address_update(Address *address, Link *link, sd_rtnl_message_handler_t callback);
406 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
407 bool address_equal(Address *a1, Address *a2);
409 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
410 #define _cleanup_address_free_ _cleanup_(address_freep)
412 int config_parse_dns(const char *unit, const char *filename, unsigned line,
413 const char *section, unsigned section_line, const char *lvalue,
414 int ltype, const char *rvalue, void *data, void *userdata);
416 int config_parse_address(const char *unit, const char *filename, unsigned line,
417 const char *section, unsigned section_line, const char *lvalue,
418 int ltype, const char *rvalue, void *data, void *userdata);
420 int config_parse_broadcast(const char *unit, const char *filename, unsigned line,
421 const char *section, unsigned section_line, const char *lvalue,
422 int ltype, const char *rvalue, void *data, void *userdata);
424 int config_parse_label(const char *unit, const char *filename, unsigned line,
425 const char *section, unsigned section_line, const char *lvalue,
426 int ltype, const char *rvalue, void *data, void *userdata);
430 Link *link_unref(Link *link);
431 Link *link_ref(Link *link);
432 int link_get(Manager *m, int ifindex, Link **ret);
433 int link_add(Manager *manager, sd_rtnl_message *message, Link **ret);
434 void link_drop(Link *link);
436 int link_update(Link *link, sd_rtnl_message *message);
437 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata);
439 int link_initialized(Link *link, struct udev_device *device);
441 int link_save(Link *link);
443 bool link_has_carrier(unsigned flags, uint8_t operstate);
445 const char* link_state_to_string(LinkState s) _const_;
446 LinkState link_state_from_string(const char *s) _pure_;
448 const char* link_operstate_to_string(LinkOperationalState s) _const_;
449 LinkOperationalState link_operstate_from_string(const char *s) _pure_;
451 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
452 #define _cleanup_link_unref_ _cleanup_(link_unrefp)
454 /* Macros which append INTERFACE= to the message */
456 #define log_full_link(level, link, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", link->ifname, "%-*s: " fmt, IFNAMSIZ, link->ifname, ##__VA_ARGS__)
457 #define log_debug_link(link, ...) log_full_link(LOG_DEBUG, link, ##__VA_ARGS__)
458 #define log_info_link(link, ...) log_full_link(LOG_INFO, link, ##__VA_ARGS__)
459 #define log_notice_link(link, ...) log_full_link(LOG_NOTICE, link, ##__VA_ARGS__)
460 #define log_warning_link(link, ...) log_full_link(LOG_WARNING, link, ##__VA_ARGS__)
461 #define log_error_link(link, ...) log_full_link(LOG_ERR, link, ##__VA_ARGS__)
463 #define log_struct_link(level, link, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__)
465 /* More macros which append INTERFACE= to the message */
467 #define log_full_netdev(level, netdev, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", netdev->ifname, "%-*s: " fmt, IFNAMSIZ, netdev->ifname, ##__VA_ARGS__)
468 #define log_debug_netdev(netdev, ...) log_full_netdev(LOG_DEBUG, netdev, ##__VA_ARGS__)
469 #define log_info_netdev(netdev, ...) log_full_netdev(LOG_INFO, netdev, ##__VA_ARGS__)
470 #define log_notice_netdev(netdev, ...) log_full_netdev(LOG_NOTICE, netdev, ##__VA_ARGS__)
471 #define log_warning_netdev(netdev, ...) log_full_netdev(LOG_WARNING, netdev,## __VA_ARGS__)
472 #define log_error_netdev(netdev, ...) log_full_netdev(LOG_ERR, netdev, ##__VA_ARGS__)
474 #define log_struct_netdev(level, netdev, ...) log_struct(level, "INTERFACE=%s", netdev->ifname, __VA_ARGS__)
476 #define NETDEV(netdev) "INTERFACE=%s", netdev->ifname
477 #define ADDRESS_FMT_VAL(address) \
478 (address).s_addr & 0xFF, \
479 ((address).s_addr >> 8) & 0xFF, \
480 ((address).s_addr >> 16) & 0xFF, \
481 (address).s_addr >> 24