chiark / gitweb /
7c23793cf92557d1c86c16cf614b3653cd003191
[elogind.git] / src / network / networkd.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #pragma once
23
24 #include <arpa/inet.h>
25 #include <linux/rtnetlink.h>
26
27 #include "sd-event.h"
28 #include "sd-rtnl.h"
29 #include "udev.h"
30
31 #include "rtnl-util.h"
32 #include "hashmap.h"
33 #include "list.h"
34
35 typedef struct Network Network;
36 typedef struct Link Link;
37 typedef struct Address Address;
38 typedef struct Route Route;
39 typedef struct Manager Manager;
40
41 struct Network {
42         Manager *manager;
43
44         char *filename;
45
46         struct ether_addr *match_mac;
47         char *match_path;
48         char *match_driver;
49         char *match_type;
50         char *match_name;
51
52         char *description;
53
54         LIST_HEAD(Address, addresses);
55         LIST_HEAD(Route, routes);
56
57         LIST_FIELDS(Network, networks);
58 };
59
60 struct Address {
61         Network *network;
62
63         unsigned char family;
64         unsigned char prefixlen;
65         char *label;
66
67         struct in_addr netmask;
68
69         union {
70                 struct in_addr in;
71                 struct in6_addr in6;
72         } in_addr;
73
74         LIST_FIELDS(Address, addresses);
75 };
76
77 struct Route {
78         Network *network;
79
80         unsigned char family;
81
82         union {
83                 struct in_addr in;
84                 struct in6_addr in6;
85         } in_addr;
86
87         LIST_FIELDS(Route, routes);
88 };
89
90 typedef enum LinkState {
91         LINK_STATE_SET_ADDRESSES,
92         LINK_STATE_ADDRESSES_SET,
93         LINK_STATE_SET_ROUTES,
94         LINK_STATE_ROUTES_SET,
95         LINK_STATE_CONFIGURED,
96         LINK_STATE_FAILED,
97         _LINK_STATE_MAX,
98         _LINK_STATE_INVALID = -1
99 } LinkState;
100
101 struct Link {
102         Manager *manager;
103
104         int ifindex;
105         struct ether_addr mac;
106
107         unsigned flags;
108
109         Network *network;
110
111         LinkState state;
112
113         unsigned rtnl_messages;
114 };
115
116 struct Manager {
117         sd_rtnl *rtnl;
118         sd_event *event;
119         struct udev *udev;
120         struct udev_monitor *udev_monitor;
121         sd_event_source *udev_event_source;
122
123         Hashmap *links;
124         LIST_HEAD(Network, networks);
125
126         char **network_dirs;
127         usec_t network_dirs_ts_usec;
128 };
129
130 /* Manager */
131
132 int manager_new(Manager **ret);
133 void manager_free(Manager *m);
134
135 int manager_udev_enumerate_links(Manager *m);
136 int manager_udev_listen(Manager *m);
137
138 int manager_rtnl_listen(Manager *m);
139
140 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
141 #define _cleanup_manager_free_ _cleanup_(manager_freep)
142
143 /* Network */
144
145 int network_load(Manager *manager);
146 bool network_should_reload(Manager *manager);
147
148 void network_free(Network *network);
149
150 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
151 #define _cleanup_network_free_ _cleanup_(network_freep)
152
153 int network_get(Manager *manager, struct udev_device *device, Network **ret);
154 int network_apply(Manager *manager, Network *network, Link *link);
155
156 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
157
158 /* Route */
159 int route_new(Network *network, Route **ret);
160 void route_free(Route *route);
161 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
162
163 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
164 #define _cleanup_route_free_ _cleanup_(route_freep)
165
166 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
167                          const char *section, const char *lvalue, int ltype,
168                          const char *rvalue, void *data, void *userdata);
169
170 /* Address */
171 int address_new(Network *network, Address **ret);
172 void address_free(Address *address);
173 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
174
175 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
176 #define _cleanup_address_free_ _cleanup_(address_freep)
177
178 int config_parse_address(const char *unit, const char *filename, unsigned line,
179                          const char *section, const char *lvalue, int ltype,
180                          const char *rvalue, void *data, void *userdata);
181
182 /* Link */
183
184 int link_new(Manager *manager, struct udev_device *device, Link **ret);
185 void link_free(Link *link);
186 int link_add(Manager *manager, struct udev_device *device);
187 int link_configure(Link *link);
188
189 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
190 #define _cleanup_link_free_ _cleanup_(link_freep)