]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/dhcp6-network.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libsystemd-network / dhcp6-network.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e3169126
PF
2/***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Intel Corporation. All rights reserved.
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include <errno.h>
3ffd4af2
LP
22#include <netinet/in.h>
23#include <netinet/ip6.h>
e3169126 24#include <stdio.h>
3ffd4af2
LP
25#include <string.h>
26#include <sys/socket.h>
27#include <sys/types.h>
e3169126 28#include <unistd.h>
3ffd4af2 29#include <linux/if_packet.h>
e3169126
PF
30
31#include "dhcp6-internal.h"
34e8c5a2 32#include "dhcp6-protocol.h"
3ffd4af2
LP
33#include "fd-util.h"
34#include "socket-util.h"
e3169126 35
34e8c5a2 36int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
34e8c5a2
PF
37 union sockaddr_union src = {
38 .in6.sin6_family = AF_INET6,
39 .in6.sin6_port = htobe16(DHCP6_PORT_CLIENT),
c601ebf7 40 .in6.sin6_scope_id = index,
34e8c5a2
PF
41 };
42 _cleanup_close_ int s = -1;
43 int r, off = 0, on = 1;
44
c601ebf7
TG
45 assert(index > 0);
46 assert(local_address);
47
48 src.in6.sin6_addr = *local_address;
34e8c5a2 49
6506063f 50 s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_UDP);
34e8c5a2
PF
51 if (s < 0)
52 return -errno;
53
34e8c5a2
PF
54 r = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
55 if (r < 0)
56 return -errno;
57
58 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off));
59 if (r < 0)
60 return -errno;
61
6506063f
TG
62 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
63 if (r < 0)
64 return -errno;
65
34e8c5a2
PF
66 r = bind(s, &src.sa, sizeof(src.in6));
67 if (r < 0)
68 return -errno;
69
70 r = s;
71 s = -1;
72 return r;
73}
74
75int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
76 const void *packet, size_t len) {
77 union sockaddr_union dest = {
78 .in6.sin6_family = AF_INET6,
79 .in6.sin6_port = htobe16(DHCP6_PORT_SERVER),
80 };
81 int r;
82
83 assert(server_address);
84
85 memcpy(&dest.in6.sin6_addr, server_address, sizeof(dest.in6.sin6_addr));
86
87 r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6));
88 if (r < 0)
89 return -errno;
90
91 return 0;
92}