chiark / gitweb /
04a56ea8c8bb73d08fa921d18cb74a8f849836dd
[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         struct ether_addr mac;
117
118         unsigned flags;
119
120         Network *network;
121
122         LinkState state;
123
124         unsigned rtnl_messages;
125 };
126
127 struct Manager {
128         sd_rtnl *rtnl;
129         sd_event *event;
130         struct udev *udev;
131         struct udev_monitor *udev_monitor;
132         sd_event_source *udev_event_source;
133
134         Hashmap *links;
135         LIST_HEAD(Network, networks);
136
137         char **network_dirs;
138         usec_t network_dirs_ts_usec;
139 };
140
141 /* Manager */
142
143 int manager_new(Manager **ret);
144 void manager_free(Manager *m);
145
146 int manager_udev_enumerate_links(Manager *m);
147 int manager_udev_listen(Manager *m);
148
149 int manager_rtnl_listen(Manager *m);
150
151 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
152 #define _cleanup_manager_free_ _cleanup_(manager_freep)
153
154 /* Network */
155
156 int network_load(Manager *manager);
157 bool network_should_reload(Manager *manager);
158
159 void network_free(Network *network);
160
161 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
162 #define _cleanup_network_free_ _cleanup_(network_freep)
163
164 int network_get(Manager *manager, struct udev_device *device, Network **ret);
165 int network_apply(Manager *manager, Network *network, Link *link);
166
167 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
168
169 /* Route */
170 int route_new(Network *network, unsigned section, Route **ret);
171 void route_free(Route *route);
172 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
173
174 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
175 #define _cleanup_route_free_ _cleanup_(route_freep)
176
177 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
178                          const char *section, unsigned section_line, const char *lvalue,
179                          int ltype, const char *rvalue, void *data, void *userdata);
180
181 int config_parse_destination(const char *unit, const char *filename, unsigned line,
182                              const char *section, unsigned section_line, const char *lvalue,
183                              int ltype, const char *rvalue, void *data, void *userdata);
184
185 /* Address */
186 int address_new(Network *network, unsigned section, Address **ret);
187 void address_free(Address *address);
188 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
189
190 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
191 #define _cleanup_address_free_ _cleanup_(address_freep)
192
193 int config_parse_address(const char *unit, const char *filename, unsigned line,
194                          const char *section, unsigned section_line, const char *lvalue,
195                          int ltype, const char *rvalue, void *data, void *userdata);
196
197 int config_parse_label(const char *unit, const char *filename, unsigned line,
198                        const char *section, unsigned section_line, const char *lvalue,
199                        int ltype, const char *rvalue, void *data, void *userdata);
200
201 /* Link */
202
203 int link_new(Manager *manager, struct udev_device *device, Link **ret);
204 void link_free(Link *link);
205 int link_add(Manager *manager, struct udev_device *device);
206 int link_configure(Link *link);
207
208 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
209 #define _cleanup_link_free_ _cleanup_(link_freep)