-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.c
243 lines (223 loc) · 5.57 KB
/
address.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <net/net_namespace.h>
#include <net/if_inet6.h>
static void print_ip4addr(const __be32 *addr)
{
char address[16], *p;
const u_char *addrp;
p = address;
for (addrp = (u_char *)addr;
addrp - (u_char *)addr < sizeof(__be32);
addrp++)
{
int n;
sprintf(p, "%d%n", *addrp, &n);
p += n;
if (addrp < (u_char *)addr + sizeof(__be32) - 1)
*p++ = '.';
}
printk(KERN_INFO "IPv4 address %s\n", address);
}
static void print_ip6addr(const struct in6_addr *addr)
{
char address[46], *p;
int i, in_zero = 0;
p = address;
for (i = 0; i < 7; i++)
{
if (!addr->s6_addr16[i])
{
if (i == 0)
*p++ = ':';
if (!in_zero)
{
*p++ = ':';
in_zero = 1;
}
}
else
{
int n;
sprintf(p, "%x:%n", ntohs(addr->s6_addr16[i]), &n);
p += n;
in_zero = 0;
}
}
sprintf(p, "%x", ntohs(addr->s6_addr16[7]));
printk(KERN_INFO "IPv6 address %s\n", address);
}
/**
* Select local-hosts addresses
* \param num_v6_addresses is a pointer to an int. Will contain the amount of found v6 addresses
* \param v6_addresses is a pointer to an array of struct in6_addr containing the found v6 addresses
* \param num_v4_addresses is a pointer to an array of _be32* v4_addresses containing the found v4 addresses
* pointer to an int. Will contain the amount of found v4 addresses
* \param
*/
/* FIXME: this should use some heuristic to determine a preferred
* interface/address. For now, it simply chooses the first up, non-loopback
* address as the "best".
* It also should determine a public/reachable address for an interface.
*/
int choose_addresses(int *num_v6_addresses, struct in6_addr **v6_addresses,
int *num_v4_addresses, __be32 **v4_addresses)
{
struct net *net = &init_net;
struct net_device *dev;
int n_v6_addresses = 0;
int n_v4_addresses = 0;
struct in6_addr *pv6;
__be32 *pv4;
int err;
/* FIXME: lock net? */
for_each_netdev(net, dev) {
if (!(dev->flags & IFF_UP))
continue;
if (dev->flags & IFF_LOOPBACK)
continue;
if (dev->ip6_ptr) {
struct inet6_dev *in6 = dev->ip6_ptr;
struct inet6_ifaddr *addr;
for (addr = in6->addr_list; addr; addr = addr->if_next)
n_v6_addresses++;
}
if (dev->ip_ptr) {
struct in_device *in4 = dev->ip_ptr;
struct in_ifaddr *addr;
for (addr = in4->ifa_list; addr; addr = addr->ifa_next)
n_v4_addresses++;
}
}
err = -ENOMEM;
if (n_v6_addresses) {
*v6_addresses = kmalloc(n_v6_addresses *
sizeof(struct inet6_ifaddr),
GFP_ATOMIC);
if (!*v6_addresses)
goto out;
else
*num_v6_addresses = n_v6_addresses;
}
else {
*v6_addresses = NULL;
*num_v6_addresses = 0;
}
if (n_v4_addresses) {
*v4_addresses = kmalloc(n_v4_addresses *
sizeof(struct in_ifaddr),
GFP_ATOMIC);
if (!*v4_addresses) {
kfree(*v6_addresses);
goto out;
}
else
*num_v4_addresses = n_v4_addresses;
}
else {
*v4_addresses = NULL;
*num_v4_addresses = 0;
}
err = 0;
pv6 = *v6_addresses;
pv4 = *v4_addresses;
for_each_netdev(net, dev) {
if (!(dev->flags & IFF_UP))
continue;
if (dev->flags & IFF_LOOPBACK)
continue;
printk(KERN_INFO "adding addresses from %s\n", dev->name);
if (dev->ip6_ptr) {
struct inet6_dev *in6 = dev->ip6_ptr;
struct inet6_ifaddr *addr;
for (addr = in6->addr_list; addr;
addr = addr->if_next) {
print_ip6addr(&addr->addr);
*pv6 = addr->addr;
pv6++;
}
}
if (dev->ip_ptr) {
struct in_device *in4 = dev->ip_ptr;
struct in_ifaddr *addr;
for (addr = in4->ifa_list; addr;
addr = addr->ifa_next) {
print_ip4addr(&addr->ifa_address);
*pv4 = addr->ifa_address;
pv4++;
}
}
}
out:
return err;
}
int match_v6_address_to_scope(struct sockaddr_in6 *sin6)
{
struct net *net = &init_net;
struct net_device *dev;
/* FIXME: lock net? */
for_each_netdev(net, dev) {
if (!(dev->flags & IFF_UP))
continue;
if (dev->flags & IFF_LOOPBACK)
continue;
if (dev->ip6_ptr) {
struct inet6_dev *in6 = dev->ip6_ptr;
struct inet6_ifaddr *addr;
for (addr = in6->addr_list; addr; addr = addr->if_next)
if (!memcmp(&addr->addr,
sin6->sin6_addr.s6_addr,
sizeof(addr->addr)))
{
sin6->sin6_scope_id = dev->ifindex;
return 0;
}
}
}
return -ENODEV;
}
int choose_scope_for_v6_address(struct sockaddr_in6 *sin6)
{
/* FIXME: for now, always picks the first interface with an IPv6
* address, or the first up interface if that fails. Should instead:
* 1. Use the source name's scope ID, if the socket is bound to a local
* name and the local name is a link-local address.
* 2. Allow choosing among multiple possible interfaces.
*/
struct net *net = &init_net;
struct net_device *dev;
/* FIXME: lock net? */
for_each_netdev(net, dev) {
if (!(dev->flags & IFF_UP))
continue;
if (dev->flags & IFF_LOOPBACK)
continue;
if (dev->ip6_ptr) {
struct inet6_dev *in6 = dev->ip6_ptr;
struct inet6_ifaddr *addr;
for (addr = in6->addr_list; addr; addr = addr->if_next) {
printk(KERN_INFO "using scope id %d for %s\n",
dev->ifindex, dev->name);
sin6->sin6_scope_id = dev->ifindex;
return 0;
}
}
}
/* If no IPv6 address was configured, hope for the best with the first
* up interface.
*/
for_each_netdev(net, dev) {
if (!(dev->flags & IFF_UP))
continue;
if (dev->flags & IFF_LOOPBACK)
continue;
printk(KERN_INFO "using scope id %d for %s\n",
dev->ifindex, dev->name);
sin6->sin6_scope_id = dev->ifindex;
return 0;
}
/* No interface, that'll definitely fail */
return -ENODEV;
}