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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
#include "config.h"
#ifndef HAVE_GETIFADDRS
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <netdb.h>
#if !defined (_AIX)
#include <nss_dbdefs.h>
#endif
#include <netinet/in.h>
#include <sys/socket.h>
#if defined (_AIX)
#include <sys/errno.h>
#include <sys/ioctl.h>
#endif
#include <string.h>
#include <stdio.h>
#if defined (_AIX)
#include <netdb.h>
#endif
#if !defined (_AIX)
#include <sys/sockio.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <net/if.h>
#include <ifaddrs.h>
#if defined (_AIX)
#include <netinet/in6_var.h>
#endif
/* Normally this is defined in <net/if.h> but was new for Solaris 11 */
#ifndef LIFC_ENABLED
#define LIFC_ENABLED 0x20
#endif
#if defined (_AIX) /* Use ifaddrs_rsys instead of ifaddrs and ifreq instead of lifreq */
int getallifaddrs(sa_family_t af, struct ifaddrs_rsys **ifap, int64_t flags);
int getallifs(int s, sa_family_t af, struct ifreq **ifr, int *numifs,
int64_t ifc_flags);
#else
int getallifaddrs(sa_family_t af, struct ifaddrs **ifap, int64_t flags);
int getallifs(int s, sa_family_t af, struct lifreq **lifr, int *numifs,
int64_t lifc_flags);
#endif
/*
* Create a linked list of `struct ifaddrs_rsys' structures, one for each
* address that is UP. If successful, store the list in *ifap and
* return 0. On errors, return -1 and set `errno'.
*
* The storage returned in *ifap is allocated dynamically and can
* only be properly freed by passing it to `freeifaddrs'.
*/
int
#if defined (_AIX)
getifaddrs(struct ifaddrs_rsys **ifap)
#else
getifaddrs(struct ifaddrs **ifap)
#endif
{
int err;
char *cp;
#if defined (_AIX)
struct ifaddrs_rsys *curr;
#else
struct ifaddrs *curr;
#endif
if (ifap == NULL) {
errno = EINVAL;
return (-1);
}
*ifap = NULL;
err = getallifaddrs(AF_UNSPEC, ifap, LIFC_ENABLED);
if (err == 0) {
for (curr = *ifap; curr != NULL; curr = curr->ifa_next) {
if ((cp = strchr(curr->ifa_name, ':')) != NULL)
*cp = '\0';
}
}
return (err);
}
void
#if defined (_AIX)
freeifaddrs(struct ifaddrs_rsys *ifa)
#else
freeifaddrs(struct ifaddrs *ifa)
#endif
{
#if defined (_AIX)
struct ifaddrs_rsys *curr;
#else
struct ifaddrs *curr;
#endif
while (ifa != NULL) {
curr = ifa;
ifa = ifa->ifa_next;
free(curr->ifa_name);
free(curr->ifa_addr);
free(curr->ifa_netmask);
free(curr->ifa_dstaddr);
free(curr);
}
}
/*
* Returns all addresses configured on the system. If flags contain
* LIFC_ENABLED, only the addresses that are UP are returned.
* Address list that is returned by this function must be freed
* using freeifaddrs().
*/
#if defined (_AIX)
int
getallifaddrs(sa_family_t af, struct ifaddrs_rsys **ifap, int64_t flags)
{
struct ifreq *buf = NULL;
struct ifreq *ifrp;
struct ifreq ifrl;
struct in6_ifreq ifrl6;
int ret;
int s, n, iflen;
struct ifaddrs_rsys *curr, *prev;
sa_family_t ifr_af;
int sock4;
int sock6;
int err;
int ifsize;
char *s_ifrp, *e_ifrp;
int flag;
if ((sock4 = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return (-1);
if ((sock6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
err = errno;
close(sock4);
errno = err;
return (-1);
}
retry:
/* Get all interfaces from SIOCGIFCONF */
ret = getallifs(sock4, af, &buf, &iflen, (flags & ~LIFC_ENABLED));
if (ret != 0)
goto fail;
/*
* Loop through the interfaces obtained from SIOCGIFCOMF
* and retrieve the addresses, netmask and flags.
*/
prev = NULL;
s_ifrp = (char *)buf;
e_ifrp = (char *)buf + iflen;
*ifap = NULL;
while (s_ifrp < e_ifrp)
{
ifrp = (struct ifreq *)s_ifrp;
ifsize = sizeof(struct ifreq);
if (ifrp->ifr_addr.sa_len > sizeof(ifrp->ifr_ifru)) {
ifsize += ifrp->ifr_addr.sa_len - sizeof(ifrp->ifr_ifru);
}
/* Prepare for the ioctl call */
(void) strncpy(ifrl.ifr_name, ifrp->ifr_name,
sizeof (ifrl.ifr_name));
(void) strncpy(ifrl6.ifr_name, ifrp->ifr_name,
sizeof (ifrl.ifr_name));
ifr_af = ifrp->ifr_addr.sa_family;
if (ifr_af != AF_INET && ifr_af != AF_INET6)
goto next;
s = (ifr_af == AF_INET ? sock4 : sock6);
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifrl) < 0)
goto fail;
if ((flags & LIFC_ENABLED) && !(ifrl.ifr_flags & IFF_UP)) {
goto next;
}
/*
* Allocate the current list node. Each node contains data
* for one ifaddrs structure.
*/
curr = calloc(1, sizeof (struct ifaddrs_rsys));
if (curr == NULL)
goto fail;
if (prev != NULL) {
prev->ifa_next = curr;
} else {
/* First node in the linked list */
*ifap = curr;
}
prev = curr;
/* AIXPORT : ifreq field names used instead of linux lifreq field names */
curr->ifa_flags = ifrl.ifr_flags;
if ((curr->ifa_name = strdup(ifrp->ifr_name)) == NULL)
goto fail;
curr->ifa_addr = malloc(sizeof (struct sockaddr_storage));
if (curr->ifa_addr == NULL)
goto fail;
(void) memcpy(curr->ifa_addr, &ifrp->ifr_addr,
sizeof (struct sockaddr_storage));
/* Get the netmask */
if (ifr_af == AF_INET) {
if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifrl) < 0) {
goto fail;
}
curr->ifa_netmask = malloc(sizeof (struct sockaddr_storage));
if (curr->ifa_netmask == NULL)
goto fail;
(void) memcpy(curr->ifa_netmask, &ifrl.ifr_addr,
sizeof (struct sockaddr_storage));
} else {
if (ioctl(s, SIOCGIFNETMASK6, (caddr_t)&ifrl6) < 0) {
goto fail;
}
curr->ifa_netmask = malloc(sizeof (struct sockaddr_storage));
if (curr->ifa_netmask == NULL)
goto fail;
(void) memcpy(curr->ifa_netmask, &ifrl6.ifr_Addr,
sizeof (struct sockaddr_storage));
}
/* Get the destination for a pt-pt interface */
if (curr->ifa_flags & IFF_POINTOPOINT) {
if (ifr_af == AF_INET) {
if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifrl) < 0)
goto fail;
curr->ifa_dstaddr = malloc(
sizeof (struct sockaddr_storage));
if (curr->ifa_dstaddr == NULL)
goto fail;
(void) memcpy(curr->ifa_dstaddr, &ifrl.ifr_addr,
sizeof (struct sockaddr_storage));
} else {
if (ioctl(s, SIOCGIFDSTADDR6, (caddr_t)&ifrl6) < 0)
goto fail;
curr->ifa_dstaddr = malloc(
sizeof (struct sockaddr_storage));
if (curr->ifa_dstaddr == NULL)
goto fail;
(void) memcpy(curr->ifa_dstaddr, &ifrl6.ifr_Addr,
sizeof (struct sockaddr_storage));
}
/* Do not get broadcast address for IPv6 */
} else if ((curr->ifa_flags & IFF_BROADCAST) && (ifr_af == AF_INET)) {
if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifrl) < 0)
goto fail;
curr->ifa_broadaddr = malloc(
sizeof (struct sockaddr_storage));
if (curr->ifa_broadaddr == NULL)
goto fail;
(void) memcpy(curr->ifa_broadaddr, &ifrl.ifr_addr,
sizeof (struct sockaddr_storage));
}
next:
s_ifrp += ifsize;
}
free(buf);
close(sock4);
close(sock6);
return (0);
fail:
err = errno;
free(buf);
freeifaddrs(*ifap);
*ifap = NULL;
if (err == ENXIO)
goto retry;
close(sock4);
close(sock6);
errno = err;
return (-1);
}
/*
* Do a SIOCGIFCONF and store all the interfaces in `buf'.
*/
int
getallifs(int s, sa_family_t af, struct ifreq **ifr, int *iflen,
int64_t ifc_flags)
{
int ifsize;
struct ifconf ifc;
size_t bufsize;
char *tmp;
caddr_t *buf = (caddr_t *)ifr;
*buf = NULL;
retry:
if (ioctl(s, SIOCGSIZIFCONF, &ifsize) < 0)
goto fail;
/*
* When calculating the buffer size needed, add a small number
* of interfaces to those we counted. We do this to capture
* the interface status of potential interfaces which may have
* been plumbed between the SIOCGSIZIFCONF and the SIOCGIFCONF.
*/
bufsize = ifsize + (4 * sizeof (struct in6_ifreq));
if ((tmp = realloc(*buf, bufsize)) == NULL)
goto fail;
*buf = tmp;
ifc.ifc_buf = *buf;
ifc.ifc_len = bufsize;
if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
goto fail;
*iflen = ifc.ifc_len;
if (*iflen >= bufsize) {
/*
* If every entry was filled, there are probably
* more interfaces than (ifn + 4)
* Redo the ioctls SIOCGSIZIFCONF and SIOCGIFCONF to
* get all the interfaces.
*/
goto retry;
}
return (0);
fail:
free(*buf);
*buf = NULL;
return (-1);
}
#else /* _AIX */
int
getallifaddrs(sa_family_t af, struct ifaddrs **ifap, int64_t flags)
{
struct lifreq *buf = NULL;
struct lifreq *lifrp;
struct lifreq lifrl;
int ret;
int s, n, numifs;
struct ifaddrs *curr, *prev;
sa_family_t lifr_af;
int sock4;
int sock6;
int err;
if ((sock4 = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return (-1);
if ((sock6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
err = errno;
close(sock4);
errno = err;
return (-1);
}
retry:
/* Get all interfaces from SIOCGLIFCONF */
ret = getallifs(sock4, af, &buf, &numifs, (flags & ~LIFC_ENABLED));
if (ret != 0)
goto fail;
/*
* Loop through the interfaces obtained from SIOCGLIFCOMF
* and retrieve the addresses, netmask and flags.
*/
prev = NULL;
lifrp = buf;
*ifap = NULL;
for (n = 0; n < numifs; n++, lifrp++) {
/* Prepare for the ioctl call */
(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
sizeof (lifrl.lifr_name));
lifr_af = lifrp->lifr_addr.ss_family;
if (af != AF_UNSPEC && lifr_af != af)
continue;
s = (lifr_af == AF_INET ? sock4 : sock6);
if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifrl) < 0)
goto fail;
if ((flags & LIFC_ENABLED) && !(lifrl.lifr_flags & IFF_UP))
continue;
/*
* Allocate the current list node. Each node contains data
* for one ifaddrs structure.
*/
curr = calloc(1, sizeof (struct ifaddrs));
if (curr == NULL)
goto fail;
if (prev != NULL) {
prev->ifa_next = curr;
} else {
/* First node in the linked list */
*ifap = curr;
}
prev = curr;
curr->ifa_flags = lifrl.lifr_flags;
if ((curr->ifa_name = strdup(lifrp->lifr_name)) == NULL)
goto fail;
curr->ifa_addr = malloc(sizeof (struct sockaddr_storage));
if (curr->ifa_addr == NULL)
goto fail;
(void) memcpy(curr->ifa_addr, &lifrp->lifr_addr,
sizeof (struct sockaddr_storage));
/* Get the netmask */
if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifrl) < 0)
goto fail;
curr->ifa_netmask = malloc(sizeof (struct sockaddr_storage));
if (curr->ifa_netmask == NULL)
goto fail;
(void) memcpy(curr->ifa_netmask, &lifrl.lifr_addr,
sizeof (struct sockaddr_storage));
/* Get the destination for a pt-pt interface */
if (curr->ifa_flags & IFF_POINTOPOINT) {
if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifrl) < 0)
goto fail;
curr->ifa_dstaddr = malloc(
sizeof (struct sockaddr_storage));
if (curr->ifa_dstaddr == NULL)
goto fail;
(void) memcpy(curr->ifa_dstaddr, &lifrl.lifr_addr,
sizeof (struct sockaddr_storage));
} else if (curr->ifa_flags & IFF_BROADCAST) {
if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifrl) < 0)
goto fail;
curr->ifa_broadaddr = malloc(
sizeof (struct sockaddr_storage));
if (curr->ifa_broadaddr == NULL)
goto fail;
(void) memcpy(curr->ifa_broadaddr, &lifrl.lifr_addr,
sizeof (struct sockaddr_storage));
}
}
free(buf);
close(sock4);
close(sock6);
return (0);
fail:
err = errno;
free(buf);
freeifaddrs(*ifap);
*ifap = NULL;
if (err == ENXIO)
goto retry;
close(sock4);
close(sock6);
errno = err;
return (-1);
}
/*
* Do a SIOCGLIFCONF and store all the interfaces in `buf'.
*/
int
getallifs(int s, sa_family_t af, struct lifreq **lifr, int *numifs,
int64_t lifc_flags)
{
struct lifnum lifn;
struct lifconf lifc;
size_t bufsize;
char *tmp;
caddr_t *buf = (caddr_t *)lifr;
lifn.lifn_family = af;
lifn.lifn_flags = lifc_flags;
*buf = NULL;
retry:
if (ioctl(s, SIOCGLIFNUM, &lifn) < 0)
goto fail;
/*
* When calculating the buffer size needed, add a small number
* of interfaces to those we counted. We do this to capture
* the interface status of potential interfaces which may have
* been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
*/
bufsize = (lifn.lifn_count + 4) * sizeof (struct lifreq);
if ((tmp = realloc(*buf, bufsize)) == NULL)
goto fail;
*buf = tmp;
lifc.lifc_family = af;
lifc.lifc_flags = lifc_flags;
lifc.lifc_len = bufsize;
lifc.lifc_buf = *buf;
if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0)
goto fail;
*numifs = lifc.lifc_len / sizeof (struct lifreq);
if (*numifs >= (lifn.lifn_count + 4)) {
/*
* If every entry was filled, there are probably
* more interfaces than (lifn.lifn_count + 4).
* Redo the ioctls SIOCGLIFNUM and SIOCGLIFCONF to
* get all the interfaces.
*/
goto retry;
}
return (0);
fail:
free(*buf);
*buf = NULL;
return (-1);
}
#endif /* _AIX */
#endif /* HAVE_GETIFADDRS */
|