]>
Commit | Line | Data |
---|---|---|
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. | |
e3169126 PF |
6 | ***/ |
7 | ||
8 | #include <errno.h> | |
3ffd4af2 LP |
9 | #include <netinet/in.h> |
10 | #include <netinet/ip6.h> | |
e3169126 | 11 | #include <stdio.h> |
3ffd4af2 LP |
12 | #include <string.h> |
13 | #include <sys/socket.h> | |
14 | #include <sys/types.h> | |
e3169126 | 15 | #include <unistd.h> |
3ffd4af2 | 16 | #include <linux/if_packet.h> |
e3169126 PF |
17 | |
18 | #include "dhcp6-internal.h" | |
34e8c5a2 | 19 | #include "dhcp6-protocol.h" |
3ffd4af2 LP |
20 | #include "fd-util.h" |
21 | #include "socket-util.h" | |
e3169126 | 22 | |
34e8c5a2 | 23 | int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) { |
34e8c5a2 PF |
24 | union sockaddr_union src = { |
25 | .in6.sin6_family = AF_INET6, | |
26 | .in6.sin6_port = htobe16(DHCP6_PORT_CLIENT), | |
c601ebf7 | 27 | .in6.sin6_scope_id = index, |
34e8c5a2 PF |
28 | }; |
29 | _cleanup_close_ int s = -1; | |
30 | int r, off = 0, on = 1; | |
31 | ||
c601ebf7 TG |
32 | assert(index > 0); |
33 | assert(local_address); | |
34 | ||
35 | src.in6.sin6_addr = *local_address; | |
34e8c5a2 | 36 | |
6506063f | 37 | s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_UDP); |
34e8c5a2 PF |
38 | if (s < 0) |
39 | return -errno; | |
40 | ||
34e8c5a2 PF |
41 | r = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)); |
42 | if (r < 0) | |
43 | return -errno; | |
44 | ||
45 | r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off)); | |
46 | if (r < 0) | |
47 | return -errno; | |
48 | ||
6506063f TG |
49 | r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
50 | if (r < 0) | |
51 | return -errno; | |
52 | ||
34e8c5a2 PF |
53 | r = bind(s, &src.sa, sizeof(src.in6)); |
54 | if (r < 0) | |
55 | return -errno; | |
56 | ||
c10d6bdb | 57 | return TAKE_FD(s); |
34e8c5a2 PF |
58 | } |
59 | ||
60 | int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address, | |
61 | const void *packet, size_t len) { | |
62 | union sockaddr_union dest = { | |
63 | .in6.sin6_family = AF_INET6, | |
64 | .in6.sin6_port = htobe16(DHCP6_PORT_SERVER), | |
65 | }; | |
66 | int r; | |
67 | ||
68 | assert(server_address); | |
69 | ||
70 | memcpy(&dest.in6.sin6_addr, server_address, sizeof(dest.in6.sin6_addr)); | |
71 | ||
72 | r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6)); | |
73 | if (r < 0) | |
74 | return -errno; | |
75 | ||
76 | return 0; | |
77 | } |