chiark / gitweb /
913f17d3a0281efd909c08eb15fde1da91dbae31
[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         Hashmap *addresses_by_section;
58         Hashmap *routes_by_section;
59
60         LIST_FIELDS(Network, networks);
61 };
62
63 struct Address {
64         Network *network;
65         uint64_t section;
66
67         unsigned char family;
68         unsigned char prefixlen;
69         char *label;
70
71         struct in_addr netmask;
72
73         union {
74                 struct in_addr in;
75                 struct in6_addr in6;
76         } in_addr;
77
78         LIST_FIELDS(Address, addresses);
79 };
80
81 struct Route {
82         Network *network;
83         uint64_t section;
84
85         unsigned char family;
86         unsigned char dst_prefixlen;
87
88         union {
89                 struct in_addr in;
90                 struct in6_addr in6;
91         } in_addr;
92
93         union {
94                 struct in_addr in;
95                 struct in6_addr in6;
96         } dst_addr;
97
98         LIST_FIELDS(Route, routes);
99 };
100
101 typedef enum LinkState {
102         LINK_STATE_SET_ADDRESSES,
103         LINK_STATE_ADDRESSES_SET,
104         LINK_STATE_SET_ROUTES,
105         LINK_STATE_ROUTES_SET,
106         LINK_STATE_CONFIGURED,
107         LINK_STATE_FAILED,
108         _LINK_STATE_MAX,
109         _LINK_STATE_INVALID = -1
110 } LinkState;
111
112 struct Link {
113         Manager *manager;
114
115         uint64_t ifindex;
116         char *ifname;
117         struct ether_addr mac;
118
119         unsigned flags;
120
121         Network *network;
122
123         LinkState state;
124
125         unsigned rtnl_messages;
126 };
127
128 struct Manager {
129         sd_rtnl *rtnl;
130         sd_event *event;
131         struct udev *udev;
132         struct udev_monitor *udev_monitor;
133         sd_event_source *udev_event_source;
134
135         Hashmap *links;
136         LIST_HEAD(Network, networks);
137
138         char **network_dirs;
139         usec_t network_dirs_ts_usec;
140 };
141
142 /* Manager */
143
144 int manager_new(Manager **ret);
145 void manager_free(Manager *m);
146
147 int manager_udev_enumerate_links(Manager *m);
148 int manager_udev_listen(Manager *m);
149
150 int manager_rtnl_listen(Manager *m);
151
152 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
153 #define _cleanup_manager_free_ _cleanup_(manager_freep)
154
155 /* Network */
156
157 int network_load(Manager *manager);
158 bool network_should_reload(Manager *manager);
159
160 void network_free(Network *network);
161
162 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
163 #define _cleanup_network_free_ _cleanup_(network_freep)
164
165 int network_get(Manager *manager, struct udev_device *device, Network **ret);
166 int network_apply(Manager *manager, Network *network, Link *link);
167
168 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
169
170 /* Route */
171 int route_new(Network *network, unsigned section, Route **ret);
172 void route_free(Route *route);
173 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
174
175 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
176 #define _cleanup_route_free_ _cleanup_(route_freep)
177
178 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
179                          const char *section, unsigned section_line, const char *lvalue,
180                          int ltype, const char *rvalue, void *data, void *userdata);
181
182 int config_parse_destination(const char *unit, const char *filename, unsigned line,
183                              const char *section, unsigned section_line, const char *lvalue,
184                              int ltype, const char *rvalue, void *data, void *userdata);
185
186 /* Address */
187 int address_new(Network *network, unsigned section, Address **ret);
188 void address_free(Address *address);
189 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
190
191 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
192 #define _cleanup_address_free_ _cleanup_(address_freep)
193
194 int config_parse_address(const char *unit, const char *filename, unsigned line,
195                          const char *section, unsigned section_line, const char *lvalue,
196                          int ltype, const char *rvalue, void *data, void *userdata);
197
198 int config_parse_label(const char *unit, const char *filename, unsigned line,
199                        const char *section, unsigned section_line, const char *lvalue,
200                        int ltype, const char *rvalue, void *data, void *userdata);
201
202 /* Link */
203
204 int link_new(Manager *manager, struct udev_device *device, Link **ret);
205 void link_free(Link *link);
206 int link_add(Manager *manager, struct udev_device *device);
207 int link_configure(Link *link);
208
209 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
210 #define _cleanup_link_free_ _cleanup_(link_freep)