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"
32 #include "sd-icmp6-nd.h"
33 #include "sd-dhcp6-client.h"
36 #include "rtnl-util.h"
40 #include "condition-util.h"
41 #include "socket-util.h"
43 #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU
44 #define VXLAN_VID_MAX (1u << 24) - 1
46 typedef struct NetDev NetDev;
47 typedef struct Network Network;
48 typedef struct Link Link;
49 typedef struct Address Address;
50 typedef struct Route Route;
51 typedef struct Manager Manager;
52 typedef struct AddressPool AddressPool;
54 typedef struct netdev_enslave_callback netdev_enslave_callback;
56 struct netdev_enslave_callback {
57 sd_rtnl_message_handler_t callback;
60 LIST_FIELDS(netdev_enslave_callback, callbacks);
63 typedef enum MacVlanMode {
64 NETDEV_MACVLAN_MODE_PRIVATE = MACVLAN_MODE_PRIVATE,
65 NETDEV_MACVLAN_MODE_VEPA = MACVLAN_MODE_VEPA,
66 NETDEV_MACVLAN_MODE_BRIDGE = MACVLAN_MODE_BRIDGE,
67 NETDEV_MACVLAN_MODE_PASSTHRU = MACVLAN_MODE_PASSTHRU,
68 _NETDEV_MACVLAN_MODE_MAX,
69 _NETDEV_MACVLAN_MODE_INVALID = -1
72 typedef enum NetDevKind {
84 _NETDEV_KIND_INVALID = -1
87 typedef enum NetDevState {
89 NETDEV_STATE_CREATING,
93 _NETDEV_STATE_INVALID = -1,
103 Condition *match_host;
104 Condition *match_virt;
105 Condition *match_kernel;
106 Condition *match_arch;
112 struct ether_addr *mac;
113 struct ether_addr *mac_peer;
118 int32_t macvlan_mode;
123 bool tunnel_pmtudisc;
127 struct in_addr local;
128 struct in_addr remote;
129 struct in_addr group;
131 LIST_HEAD(netdev_enslave_callback, callbacks);
139 struct ether_addr *match_mac;
144 Condition *match_host;
145 Condition *match_virt;
146 Condition *match_kernel;
147 Condition *match_arch;
161 bool dhcp_domainname;
168 LIST_HEAD(Address, static_addresses);
169 LIST_HEAD(Route, static_routes);
171 Hashmap *addresses_by_section;
172 Hashmap *routes_by_section;
174 LIST_HEAD(Address, dns);
175 LIST_HEAD(Address, ntp);
177 LIST_FIELDS(Network, networks);
184 unsigned char family;
185 unsigned char prefixlen;
189 struct in_addr broadcast;
190 struct ifa_cacheinfo cinfo;
192 union in_addr_union in_addr;
194 LIST_FIELDS(Address, addresses);
201 unsigned char family;
202 unsigned char dst_prefixlen;
206 union in_addr_union in_addr;
207 union in_addr_union dst_addr;
209 LIST_FIELDS(Route, routes);
212 typedef enum LinkState {
213 LINK_STATE_INITIALIZING,
214 LINK_STATE_ENSLAVING,
215 LINK_STATE_SETTING_ADDRESSES,
216 LINK_STATE_SETTING_ROUTES,
217 LINK_STATE_CONFIGURED,
218 LINK_STATE_UNMANAGED,
222 _LINK_STATE_INVALID = -1
225 typedef enum LinkOperationalState {
226 LINK_OPERSTATE_UNKNOWN,
227 LINK_OPERSTATE_DORMANT,
228 LINK_OPERSTATE_CARRIER,
229 LINK_OPERSTATE_DEGRADED,
230 LINK_OPERSTATE_ROUTABLE,
232 _LINK_OPERSTATE_INVALID = -1
233 } LinkOperationalState;
243 struct ether_addr mac;
244 struct udev_device *udev_device;
247 uint8_t kernel_operstate;
252 LinkOperationalState operstate;
254 unsigned addr_messages;
255 unsigned route_messages;
258 LIST_HEAD(Address, addresses);
260 sd_dhcp_client *dhcp_client;
261 sd_dhcp_lease *dhcp_lease;
263 uint16_t original_mtu;
266 LIST_HEAD(Address, pool_addresses);
268 sd_dhcp_server *dhcp_server;
270 sd_icmp6_nd *icmp6_router_discovery;
271 sd_dhcp6_client *dhcp6_client;
280 union in_addr_union in_addr;
282 LIST_FIELDS(AddressPool, address_pools);
290 struct udev_monitor *udev_monitor;
291 sd_event_source *udev_event_source;
292 sd_event_source *sigterm_event_source;
293 sd_event_source *sigint_event_source;
299 LIST_HEAD(Network, networks);
300 LIST_HEAD(AddressPool, address_pools);
302 usec_t network_dirs_ts_usec;
305 extern const char* const network_dirs[];
309 int manager_new(Manager **ret);
310 void manager_free(Manager *m);
312 int manager_load_config(Manager *m);
313 bool manager_should_reload(Manager *m);
315 int manager_rtnl_enumerate_links(Manager *m);
317 int manager_rtnl_listen(Manager *m);
318 int manager_udev_listen(Manager *m);
319 int manager_bus_listen(Manager *m);
321 int manager_save(Manager *m);
323 int manager_address_pool_acquire(Manager *m, unsigned family, unsigned prefixlen, union in_addr_union *found);
325 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
326 #define _cleanup_manager_free_ _cleanup_(manager_freep)
330 int netdev_load(Manager *manager);
331 void netdev_drop(NetDev *netdev);
333 NetDev *netdev_unref(NetDev *netdev);
334 NetDev *netdev_ref(NetDev *netdev);
336 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
337 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
339 int netdev_get(Manager *manager, const char *name, NetDev **ret);
340 int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
341 int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
342 int netdev_create_tunnel(Link *link, sd_rtnl_message_handler_t callback);
343 int netdev_create_veth(NetDev *netdev, sd_rtnl_message_handler_t callback);
344 int netdev_create_vxlan(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback);
346 const char *netdev_kind_to_string(NetDevKind d) _const_;
347 NetDevKind netdev_kind_from_string(const char *d) _pure_;
349 const char *macvlan_mode_to_string(MacVlanMode d) _const_;
350 MacVlanMode macvlan_mode_from_string(const char *d) _pure_;
352 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);
354 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);
357 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
361 int network_load(Manager *manager);
363 void network_free(Network *network);
365 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
366 #define _cleanup_network_free_ _cleanup_(network_freep)
368 int network_get(Manager *manager, struct udev_device *device,
369 const char *ifname, const struct ether_addr *mac,
371 int network_apply(Manager *manager, Network *network, Link *link);
373 int config_parse_netdev(const char *unit, const char *filename, unsigned line,
374 const char *section, unsigned section_line, const char *lvalue,
375 int ltype, const char *rvalue, void *data, void *userdata);
377 int config_parse_tunnel(const char *unit,
378 const char *filename,
381 unsigned section_line,
388 int config_parse_tunnel_address(const char *unit,
389 const char *filename,
392 unsigned section_line,
400 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);
403 int route_new_static(Network *network, unsigned section, Route **ret);
404 int route_new_dynamic(Route **ret);
405 void route_free(Route *route);
406 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
407 int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
410 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
411 #define _cleanup_route_free_ _cleanup_(route_freep)
413 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
414 const char *section, unsigned section_line, const char *lvalue,
415 int ltype, const char *rvalue, void *data, void *userdata);
417 int config_parse_destination(const char *unit, const char *filename, unsigned line,
418 const char *section, unsigned section_line, const char *lvalue,
419 int ltype, const char *rvalue, void *data, void *userdata);
422 int address_new_static(Network *network, unsigned section, Address **ret);
423 int address_new_dynamic(Address **ret);
424 void address_free(Address *address);
425 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
426 int address_update(Address *address, Link *link, sd_rtnl_message_handler_t callback);
427 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
428 bool address_equal(Address *a1, Address *a2);
430 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
431 #define _cleanup_address_free_ _cleanup_(address_freep)
433 int config_parse_dns(const char *unit, const char *filename, unsigned line,
434 const char *section, unsigned section_line, const char *lvalue,
435 int ltype, const char *rvalue, void *data, void *userdata);
437 int config_parse_address(const char *unit, const char *filename, unsigned line,
438 const char *section, unsigned section_line, const char *lvalue,
439 int ltype, const char *rvalue, void *data, void *userdata);
441 int config_parse_broadcast(const char *unit, const char *filename, unsigned line,
442 const char *section, unsigned section_line, const char *lvalue,
443 int ltype, const char *rvalue, void *data, void *userdata);
445 int config_parse_label(const char *unit, const char *filename, unsigned line,
446 const char *section, unsigned section_line, const char *lvalue,
447 int ltype, const char *rvalue, void *data, void *userdata);
451 Link *link_unref(Link *link);
452 Link *link_ref(Link *link);
453 int link_get(Manager *m, int ifindex, Link **ret);
454 int link_add(Manager *manager, sd_rtnl_message *message, Link **ret);
455 void link_drop(Link *link);
457 int link_update(Link *link, sd_rtnl_message *message);
458 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata);
460 int link_initialized(Link *link, struct udev_device *device);
462 int link_save(Link *link);
464 bool link_has_carrier(unsigned flags, uint8_t operstate);
466 const char* link_state_to_string(LinkState s) _const_;
467 LinkState link_state_from_string(const char *s) _pure_;
469 const char* link_operstate_to_string(LinkOperationalState s) _const_;
470 LinkOperationalState link_operstate_from_string(const char *s) _pure_;
472 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
473 #define _cleanup_link_unref_ _cleanup_(link_unrefp)
477 int address_pool_new(Manager *m, AddressPool **ret, unsigned family, const union in_addr_union *u, unsigned prefixlen);
478 int address_pool_new_from_string(Manager *m, AddressPool **ret, unsigned family, const char *p, unsigned prefixlen);
479 void address_pool_free(AddressPool *p);
481 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found);
483 /* Macros which append INTERFACE= to the message */
485 #define log_full_link(level, link, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", link->ifname, "%-*s: " fmt, IFNAMSIZ, link->ifname, ##__VA_ARGS__)
486 #define log_debug_link(link, ...) log_full_link(LOG_DEBUG, link, ##__VA_ARGS__)
487 #define log_info_link(link, ...) log_full_link(LOG_INFO, link, ##__VA_ARGS__)
488 #define log_notice_link(link, ...) log_full_link(LOG_NOTICE, link, ##__VA_ARGS__)
489 #define log_warning_link(link, ...) log_full_link(LOG_WARNING, link, ##__VA_ARGS__)
490 #define log_error_link(link, ...) log_full_link(LOG_ERR, link, ##__VA_ARGS__)
492 #define log_struct_link(level, link, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__)
494 /* More macros which append INTERFACE= to the message */
496 #define log_full_netdev(level, netdev, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", netdev->ifname, "%-*s: " fmt, IFNAMSIZ, netdev->ifname, ##__VA_ARGS__)
497 #define log_debug_netdev(netdev, ...) log_full_netdev(LOG_DEBUG, netdev, ##__VA_ARGS__)
498 #define log_info_netdev(netdev, ...) log_full_netdev(LOG_INFO, netdev, ##__VA_ARGS__)
499 #define log_notice_netdev(netdev, ...) log_full_netdev(LOG_NOTICE, netdev, ##__VA_ARGS__)
500 #define log_warning_netdev(netdev, ...) log_full_netdev(LOG_WARNING, netdev,## __VA_ARGS__)
501 #define log_error_netdev(netdev, ...) log_full_netdev(LOG_ERR, netdev, ##__VA_ARGS__)
503 #define log_struct_netdev(level, netdev, ...) log_struct(level, "INTERFACE=%s", netdev->ifname, __VA_ARGS__)
505 #define NETDEV(netdev) "INTERFACE=%s", netdev->ifname
506 #define ADDRESS_FMT_VAL(address) \
507 (address).s_addr & 0xFF, \
508 ((address).s_addr >> 8) & 0xFF, \
509 ((address).s_addr >> 16) & 0xFF, \
510 (address).s_addr >> 24