]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link.c
networkd: add a basic network daemon
[thirdparty/systemd.git] / src / network / networkd-link.c
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 #include <netinet/ether.h>
23 #include <linux/if.h>
24
25 #include "networkd.h"
26 #include "libudev-private.h"
27 #include "util.h"
28
29 int link_new(Manager *manager, struct udev_device *device, Link **ret) {
30 _cleanup_link_free_ Link *link = NULL;
31 uint64_t ifindex;
32 int r;
33
34 assert(device);
35 assert(ret);
36
37 link = new0(Link, 1);
38 if (!link)
39 return -ENOMEM;
40
41 ifindex = udev_device_get_ifindex(device);
42 if (ifindex <= 0)
43 return -EINVAL;
44
45 link->ifindex = ifindex;
46 link->manager = manager;
47
48 r = hashmap_put(manager->links, &ifindex, link);
49 if (r < 0)
50 return r;
51
52 *ret = link;
53 link = NULL;
54
55 return 0;
56 }
57
58 void link_free(Link *link) {
59 if (!link)
60 return;
61
62 network_free(link->network);
63
64 hashmap_remove(link->manager->links, link);
65
66 free(link);
67 }
68
69 int link_add(Manager *m, struct udev_device *device) {
70 Link *link;
71 Network *network;
72 int r;
73 uint64_t ifindex;
74
75 assert(m);
76 assert(device);
77
78 ifindex = udev_device_get_ifindex(device);
79 link = hashmap_get(m->links, &ifindex);
80 if (link)
81 return 0;
82
83 r = link_new(m, device, &link);
84 if (r < 0) {
85 log_error("could not create link: %s", strerror(-r));
86 return r;
87 }
88
89 r = network_get(m, device, &network);
90 if (r < 0)
91 return r == -ENOENT ? 0 : r;
92
93 r = network_apply(m, network, link);
94 if (r < 0)
95 return r;
96
97 return 0;
98 }
99
100 int link_up(Manager *manager, Link *link) {
101 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
102 int r;
103
104 r = sd_rtnl_message_link_new(RTM_NEWLINK, link->ifindex, 0, IFF_UP, &req);
105 if (r < 0) {
106 log_error("Could not allocate RTM_NEWLINK message");
107 return r;
108 }
109
110 r = sd_rtnl_send_with_reply_and_block(manager->rtnl, req, 0, NULL);
111 if (r < 0) {
112 log_error("Could not UP link: %s", strerror(-r));
113 return r;
114 }
115
116 log_info("Link is UP");
117
118 return 0;
119 }