]>
Commit | Line | Data |
---|---|---|
f579559b TG |
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; | |
8cd11a0f | 32 | const char *mac; |
f579559b TG |
33 | int r; |
34 | ||
35 | assert(device); | |
36 | assert(ret); | |
37 | ||
38 | link = new0(Link, 1); | |
39 | if (!link) | |
40 | return -ENOMEM; | |
41 | ||
42 | ifindex = udev_device_get_ifindex(device); | |
43 | if (ifindex <= 0) | |
44 | return -EINVAL; | |
45 | ||
8cd11a0f TG |
46 | mac = udev_device_get_sysattr_value(device, "address"); |
47 | if (!mac) | |
48 | return -EINVAL; | |
49 | ||
50 | memcpy(&link->mac.ether_addr_octet[0], ether_aton(mac), ETH_ALEN); | |
f579559b TG |
51 | link->ifindex = ifindex; |
52 | link->manager = manager; | |
53 | ||
54 | r = hashmap_put(manager->links, &ifindex, link); | |
55 | if (r < 0) | |
56 | return r; | |
57 | ||
58 | *ret = link; | |
59 | link = NULL; | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | void link_free(Link *link) { | |
65 | if (!link) | |
66 | return; | |
67 | ||
68 | network_free(link->network); | |
69 | ||
70 | hashmap_remove(link->manager->links, link); | |
71 | ||
72 | free(link); | |
73 | } | |
74 | ||
75 | int link_add(Manager *m, struct udev_device *device) { | |
76 | Link *link; | |
77 | Network *network; | |
78 | int r; | |
79 | uint64_t ifindex; | |
80 | ||
81 | assert(m); | |
82 | assert(device); | |
83 | ||
84 | ifindex = udev_device_get_ifindex(device); | |
85 | link = hashmap_get(m->links, &ifindex); | |
86 | if (link) | |
87 | return 0; | |
88 | ||
89 | r = link_new(m, device, &link); | |
90 | if (r < 0) { | |
91 | log_error("could not create link: %s", strerror(-r)); | |
92 | return r; | |
93 | } | |
94 | ||
95 | r = network_get(m, device, &network); | |
96 | if (r < 0) | |
97 | return r == -ENOENT ? 0 : r; | |
98 | ||
99 | r = network_apply(m, network, link); | |
100 | if (r < 0) | |
101 | return r; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | int link_up(Manager *manager, Link *link) { | |
107 | _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
108 | int r; | |
109 | ||
110 | r = sd_rtnl_message_link_new(RTM_NEWLINK, link->ifindex, 0, IFF_UP, &req); | |
111 | if (r < 0) { | |
112 | log_error("Could not allocate RTM_NEWLINK message"); | |
113 | return r; | |
114 | } | |
115 | ||
fe4824e0 | 116 | r = sd_rtnl_call(manager->rtnl, req, 0, NULL); |
f579559b TG |
117 | if (r < 0) { |
118 | log_error("Could not UP link: %s", strerror(-r)); | |
119 | return r; | |
120 | } | |
121 | ||
122 | log_info("Link is UP"); | |
123 | ||
124 | return 0; | |
125 | } |