-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsocket.c
159 lines (134 loc) · 4.03 KB
/
socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Authors:
* Alexander Aring <alex.aring@gmail.com>
*
* Original Authors:
* Pedro Roque <roque@di.fc.ul.pt>
* Lars Fenneberg <lf@elemental.net>
*
* This software is Copyright 1996,1997,2019 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <reubenhwk@gmail.com>.
*
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/icmp6.h>
#include "helpers.h"
#include "socket.h"
#include "config.h"
#include "log.h"
#include "rpl.h"
/* Note: these are applicable to receiving sockopts only */
#if defined IPV6_HOPLIMIT && !defined IPV6_RECVHOPLIMIT
#define IPV6_RECVHOPLIMIT IPV6_HOPLIMIT
#endif
#if defined IPV6_PKTINFO && !defined IPV6_RECVPKTINFO
#define IPV6_RECVPKTINFO IPV6_PKTINFO
#endif
static int rpl_multicast_handler(int sock, const struct list_head *ifaces,
int optname)
{
struct ipv6_mreq mreq;
struct iface *iface;
struct list *e;
int rc;
DL_FOREACH(ifaces->head, e) {
iface = container_of(e, struct iface, list);
memset(&mreq, 0, sizeof(mreq));
mreq.ipv6mr_interface = iface->ifindex;
/* all-rpl-nodes: ff02::1a */
memcpy(&mreq.ipv6mr_multiaddr.s6_addr[0], &all_rpl_addr,
sizeof(mreq.ipv6mr_multiaddr));
rc = setsockopt(sock, SOL_IPV6, optname, &mreq,
sizeof(mreq));
if (rc < 0)
return -1;
flog(LOG_INFO, "interface %s %s to rpl multicast",
iface->ifname, (optname == IPV6_ADD_MEMBERSHIP)?
"subscribe":"unsubscribe");
}
return 0;
}
static int subscribe_rpl_multicast(int sock, const struct list_head *ifaces)
{
return rpl_multicast_handler(sock, ifaces, IPV6_ADD_MEMBERSHIP);
}
static void unsubscribe_rpl_multicast(int sock, const struct list_head *ifaces)
{
rpl_multicast_handler(sock, ifaces, IPV6_DROP_MEMBERSHIP);
}
int open_icmpv6_socket(const struct list_head *ifaces)
{
struct icmp6_filter filter;
int sock;
int err;
sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (sock < 0) {
flog(LOG_ERR, "can't create socket(AF_INET6): %s", strerror(errno));
return -1;
}
err = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, (int[]){1}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_RECVPKTINFO): %s", strerror(errno));
close(sock);
return -1;
}
err = setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, (int[]){2}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_CHECKSUM): %s", strerror(errno));
close(sock);
return -1;
}
err = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (int[]){255}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
close(sock);
return -1;
}
err = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (int[]){1}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
close(sock);
return -1;
}
err = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (int[]){0}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
close(sock);
return -1;
}
#ifdef IPV6_RECVHOPLIMIT
err = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, (int[]){1}, sizeof(int));
if (err < 0) {
flog(LOG_ERR, "setsockopt(IPV6_RECVHOPLIMIT): %s", strerror(errno));
close(sock);
return -1;
}
#endif
err = subscribe_rpl_multicast(sock, ifaces);
if (err < 0) {
flog(LOG_ERR, "Failed to subscribe rpl multicast: %s", strerror(errno));
close(sock);
return -1;
}
ICMP6_FILTER_SETBLOCKALL(&filter);
ICMP6_FILTER_SETPASS(ND_RPL_MESSAGE, &filter);
#if 0
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
ICMP6_FILTER_SETPASS(ND_NEIGHBOR_SOLICIT, &filter);
ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter);
#endif
return sock;
}
void close_icmpv6_socket(int sock, const struct list_head *ifaces)
{
unsubscribe_rpl_multicast(sock, ifaces);
close(sock);
}