chiark / gitweb /
bf8d78b6d09d36818d685931181771bb4ddddab1
[elogind.git] / src / network / networkd-dhcp6.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2014 Intel Corporation. All rights reserved.
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-link.h"
26 #include "network-internal.h"
27
28 #include "sd-icmp6-nd.h"
29 #include "sd-dhcp6-client.h"
30
31 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
32         Link *link = userdata;
33
34         assert(link);
35         assert(link->network);
36         assert(link->manager);
37
38         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
39                 return;
40
41         switch(event) {
42         case DHCP6_EVENT_STOP:
43         case DHCP6_EVENT_RESEND_EXPIRE:
44         case DHCP6_EVENT_RETRANS_MAX:
45         case DHCP6_EVENT_IP_ACQUIRE:
46                 log_link_debug(link, "DHCPv6 event %d", event);
47
48                 break;
49
50         default:
51                 if (event < 0)
52                         log_link_warning(link, "DHCPv6 error: %s",
53                                          strerror(-event));
54                 else
55                         log_link_warning(link, "DHCPv6 unknown event: %d",
56                                          event);
57                 return;
58         }
59 }
60
61 static int dhcp6_configure(Link *link) {
62         int r;
63
64         assert_return(link, -EINVAL);
65
66         if (link->dhcp6_client)
67                 return 0;
68
69         r = sd_dhcp6_client_new(&link->dhcp6_client);
70         if (r < 0)
71                 return r;
72
73         r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
74         if (r < 0) {
75                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
76                 return r;
77         }
78
79         r = sd_dhcp6_client_set_mac(link->dhcp6_client,
80                                     (const uint8_t *) &link->mac,
81                                     sizeof (link->mac), ARPHRD_ETHER);
82         if (r < 0) {
83                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
84                 return r;
85         }
86
87         r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
88         if (r < 0) {
89                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
90                 return r;
91         }
92
93         r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
94                                          link);
95         if (r < 0) {
96                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
97                 return r;
98         }
99
100         r = sd_dhcp6_client_start(link->dhcp6_client);
101         if (r < 0)
102                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
103
104         return r;
105 }
106
107 static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
108         Link *link = userdata;
109
110         assert(link);
111         assert(link->network);
112         assert(link->manager);
113
114         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
115                 return;
116
117         switch(event) {
118         case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
119         case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
120                 return;
121
122         case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
123         case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
124                 break;
125
126         default:
127                 if (event < 0)
128                         log_link_warning(link, "ICMPv6 error: %s",
129                                          strerror(-event));
130                 else
131                         log_link_warning(link, "ICMPv6 unknown event: %d",
132                                          event);
133
134                 return;
135         }
136
137         dhcp6_configure(link);
138 }
139
140 int icmp6_configure(Link *link) {
141         int r;
142
143         assert_return(link, -EINVAL);
144
145         r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
146         if (r < 0)
147                 return r;
148
149         r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery, NULL, 0);
150         if (r < 0)
151                 return r;
152
153         r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery, &link->mac);
154         if (r < 0)
155                 return r;
156
157         r = sd_icmp6_nd_set_index(link->icmp6_router_discovery, link->ifindex);
158         if (r < 0)
159                 return r;
160
161         r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
162                                 icmp6_router_handler, link);
163
164         return r;
165 }