]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-ndisc.c
dfbc5a866ecb327ee38e73e0ec3b99e68b170687
[thirdparty/systemd.git] / src / network / networkd-ndisc.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 <netinet/icmp6.h>
24 #include <linux/if.h>
25
26 #include "sd-ndisc.h"
27
28 #include "networkd-link.h"
29
30 static void ndisc_router_handler(sd_ndisc *nd, uint8_t flags, const struct in6_addr *gateway, unsigned lifetime, int pref, void *userdata) {
31 Link *link = userdata;
32
33 assert(link);
34 assert(link->network);
35
36 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
37 return;
38
39 if (flags & ND_RA_FLAG_MANAGED)
40 dhcp6_configure(link, false);
41 else if (flags & ND_RA_FLAG_OTHER)
42 dhcp6_configure(link, true);
43 }
44
45 static void ndisc_handler(sd_ndisc *nd, int event, void *userdata) {
46 Link *link = userdata;
47
48 assert(link);
49
50 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
51 return;
52
53 switch (event) {
54 case SD_NDISC_EVENT_TIMEOUT:
55 dhcp6_configure(link, false);
56 break;
57 case SD_NDISC_EVENT_STOP:
58 break;
59 default:
60 log_link_warning(link, "IPv6 Neighbor Discovery unknown event: %d", event);
61 }
62 }
63
64 int ndisc_configure(Link *link) {
65 int r;
66
67 assert_return(link, -EINVAL);
68
69 r = sd_ndisc_new(&link->ndisc_router_discovery);
70 if (r < 0)
71 return r;
72
73 r = sd_ndisc_attach_event(link->ndisc_router_discovery, NULL, 0);
74 if (r < 0)
75 return r;
76
77 r = sd_ndisc_set_mac(link->ndisc_router_discovery, &link->mac);
78 if (r < 0)
79 return r;
80
81 r = sd_ndisc_set_index(link->ndisc_router_discovery, link->ifindex);
82 if (r < 0)
83 return r;
84
85 r = sd_ndisc_set_callback(link->ndisc_router_discovery,
86 ndisc_router_handler,
87 NULL,
88 NULL,
89 ndisc_handler,
90 link);
91
92 return r;
93 }