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