This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif-linux.c
1739 lines (1519 loc) · 39.6 KB
/
if-linux.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Linux interface driver for dhcpcd
* Copyright (c) 2006-2016 Roy Marples <[email protected]>
* All rights reserved
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <asm/types.h> /* Needed for 2.4 kernels */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <linux/if_addr.h>
#include <linux/if_link.h>
#include <linux/if_packet.h>
#include <linux/filter.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <net/route.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#include "common.h"
#include "dev.h"
#include "dhcp.h"
#include "if.h"
#include "ipv4.h"
#include "ipv4ll.h"
#include "ipv6.h"
#include "ipv6nd.h"
#include "route.h"
#include "sa.h"
#ifdef HAVE_NL80211_H
#include <linux/genetlink.h>
#include <linux/nl80211.h>
#else
int if_getssid_wext(const char *ifname, uint8_t *ssid);
#endif
/* Support older kernels */
#ifndef IFLA_WIRELESS
#define IFLA_WIRELESS (IFLA_MASTER + 1)
#endif
/* For some reason, glibc doesn't include newer flags from linux/if.h
* However, we cannot include linux/if.h directly as it conflicts
* with the glibc version. D'oh! */
#ifndef IFF_LOWER_UP
#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
#endif
#define bpf_insn sock_filter
#define BPF_SKIPTYPE
#define BPF_ETHCOOK -ETH_HLEN
#define BPF_WHOLEPACKET 0x0fffffff /* work around buggy LPF filters */
#include "bpf-filter.h"
struct priv {
int route_fd;
uint32_t route_pid;
struct iovec sndrcv_iov[1];
};
/* Broadcast address for IPoIB */
static const uint8_t ipv4_bcast_addr[] = {
0x00, 0xff, 0xff, 0xff,
0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
};
#define PROC_INET6 "/proc/net/if_inet6"
#define PROC_PROMOTE "/proc/sys/net/ipv4/conf/%s/promote_secondaries"
#define SYS_LAYER2 "/sys/class/net/%s/device/layer2"
static const char *mproc =
#if defined(__alpha__)
"system type"
#elif defined(__arm__)
"Hardware"
#elif defined(__avr32__)
"cpu family"
#elif defined(__bfin__)
"BOARD Name"
#elif defined(__cris__)
"cpu model"
#elif defined(__frv__)
"System"
#elif defined(__i386__) || defined(__x86_64__)
"vendor_id"
#elif defined(__ia64__)
"vendor"
#elif defined(__hppa__)
"model"
#elif defined(__m68k__)
"MMU"
#elif defined(__mips__)
"system type"
#elif defined(__powerpc__) || defined(__powerpc64__)
"machine"
#elif defined(__s390__) || defined(__s390x__)
"Manufacturer"
#elif defined(__sh__)
"machine"
#elif defined(sparc) || defined(__sparc__)
"cpu"
#elif defined(__vax__)
"cpu"
#else
NULL
#endif
;
int
if_machinearch(char *str, size_t len)
{
FILE *fp;
char buf[256];
if (mproc == NULL) {
errno = EINVAL;
return -1;
}
fp = fopen("/proc/cpuinfo", "r");
if (fp == NULL)
return -1;
while (fscanf(fp, "%255s : ", buf) != EOF) {
if (strncmp(buf, mproc, strlen(mproc)) == 0 &&
fscanf(fp, "%255s", buf) == 1)
{
fclose(fp);
return snprintf(str, len, ":%s", buf);
}
}
fclose(fp);
errno = ESRCH;
return -1;
}
static int
check_proc_int(const char *path)
{
FILE *fp;
int i;
fp = fopen(path, "r");
if (fp == NULL)
return -1;
if (fscanf(fp, "%d", &i) != 1)
i = -1;
fclose(fp);
return i;
}
static ssize_t
write_path(const char *path, const char *val)
{
FILE *fp;
ssize_t r;
fp = fopen(path, "w");
if (fp == NULL)
return -1;
r = fprintf(fp, "%s\n", val);
fclose(fp);
return r;
}
int
if_init(struct interface *ifp)
{
char path[sizeof(PROC_PROMOTE) + IF_NAMESIZE];
int n;
/* We enable promote_secondaries so that we can do this
* add 192.168.1.2/24
* add 192.168.1.3/24
* del 192.168.1.2/24
* and the subnet mask moves onto 192.168.1.3/24
* This matches the behaviour of BSD which makes coding dhcpcd
* a little easier as there's just one behaviour. */
snprintf(path, sizeof(path), PROC_PROMOTE, ifp->name);
n = check_proc_int(path);
if (n == -1)
return errno == ENOENT ? 0 : -1;
if (n == 1)
return 0;
return write_path(path, "1") == -1 ? -1 : 0;
}
int
if_conf(struct interface *ifp)
{
char path[sizeof(SYS_LAYER2) + IF_NAMESIZE];
int n;
/* Some qeth setups require the use of the broadcast flag. */
snprintf(path, sizeof(path), SYS_LAYER2, ifp->name);
n = check_proc_int(path);
if (n == -1)
return errno == ENOENT ? 0 : -1;
if (n == 0)
ifp->options->options |= DHCPCD_BROADCAST;
return 0;
}
/* XXX work out Virtal Interface Masters */
int
if_vimaster(__unused const struct dhcpcd_ctx *ctx, __unused const char *ifname)
{
return 0;
}
static int
_open_link_socket(struct sockaddr_nl *nl, int protocol)
{
int fd;
if ((fd = xsocket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol)) == -1)
return -1;
nl->nl_family = AF_NETLINK;
if (bind(fd, (struct sockaddr *)nl, sizeof(*nl)) == -1) {
close(fd);
return -1;
}
return fd;
}
int
if_opensockets_os(struct dhcpcd_ctx *ctx)
{
struct priv *priv;
struct sockaddr_nl snl;
socklen_t len;
/* Open the link socket first so it gets pid() for the socket.
* Then open our persistent route socket so we get a unique
* pid that doesn't clash with a process id for after we fork. */
memset(&snl, 0, sizeof(snl));
snl.nl_groups = RTMGRP_LINK;
#ifdef INET
snl.nl_groups |= RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
#endif
#ifdef INET6
snl.nl_groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR | RTMGRP_NEIGH;
#endif
ctx->link_fd = _open_link_socket(&snl, NETLINK_ROUTE);
if (ctx->link_fd == -1)
return -1;
if ((priv = calloc(1, sizeof(*priv))) == NULL)
return -1;
ctx->priv = priv;
memset(&snl, 0, sizeof(snl));
priv->route_fd = _open_link_socket(&snl, NETLINK_ROUTE);
if (priv->route_fd == -1)
return -1;
len = sizeof(snl);
if (getsockname(priv->route_fd, (struct sockaddr *)&snl, &len) == -1)
return -1;
priv->route_pid = snl.nl_pid;
return 0;
}
void
if_closesockets_os(struct dhcpcd_ctx *ctx)
{
struct priv *priv;
if (ctx->priv != NULL) {
priv = (struct priv *)ctx->priv;
free(priv->sndrcv_iov[0].iov_base);
close(priv->route_fd);
}
}
static int
get_netlink(struct dhcpcd_ctx *ctx, struct iovec *iov,
struct interface *ifp, int fd, int flags,
int (*callback)(struct dhcpcd_ctx *, struct interface *, struct nlmsghdr *))
{
struct msghdr msg;
struct sockaddr_nl nladdr;
ssize_t len;
struct nlmsghdr *nlm;
int r;
unsigned int again;
memset(&msg, 0, sizeof(msg));
msg.msg_name = &nladdr;
msg.msg_namelen = sizeof(nladdr);
memset(&nladdr, 0, sizeof(nladdr));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
recv_again:
if ((len = recvmsg_realloc(fd, &msg, flags)) == -1)
return -1;
if (len == 0)
return 0;
/* Check sender */
if (msg.msg_namelen != sizeof(nladdr)) {
errno = EINVAL;
return -1;
}
/* Ignore message if it is not from kernel */
if (nladdr.nl_pid != 0)
return 0;
r = 0;
again = 0; /* Appease static analysis */
for (nlm = iov->iov_base;
nlm && NLMSG_OK(nlm, (size_t)len);
nlm = NLMSG_NEXT(nlm, len))
{
again = (nlm->nlmsg_flags & NLM_F_MULTI);
if (nlm->nlmsg_type == NLMSG_NOOP)
continue;
if (nlm->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err;
if (nlm->nlmsg_len - sizeof(*nlm) < sizeof(*err)) {
errno = EBADMSG;
return -1;
}
err = (struct nlmsgerr *)NLMSG_DATA(nlm);
if (err->error != 0) {
errno = -err->error;
return -1;
}
break;
}
if (nlm->nlmsg_type == NLMSG_DONE) {
again = 0;
break;
}
if (callback && (r = callback(ctx, ifp, nlm)) != 0)
break;
}
if (r == 0 && again)
goto recv_again;
return r;
}
static int
if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm)
{
size_t len;
struct rtmsg *rtm;
struct rtattr *rta;
unsigned int ifindex;
struct sockaddr *sa;
len = nlm->nlmsg_len - sizeof(*nlm);
if (len < sizeof(*rtm)) {
errno = EBADMSG;
return -1;
}
rtm = (struct rtmsg *)NLMSG_DATA(nlm);
if (rtm->rtm_table != RT_TABLE_MAIN)
return -1;
memset(rt, 0, sizeof(*rt));
if (rtm->rtm_type == RTN_UNREACHABLE)
rt->rt_flags |= RTF_REJECT;
if (rtm->rtm_scope == RT_SCOPE_HOST)
rt->rt_flags |= RTF_HOST;
rta = (struct rtattr *)RTM_RTA(rtm);
len = RTM_PAYLOAD(nlm);
while (RTA_OK(rta, len)) {
sa = NULL;
switch (rta->rta_type) {
case RTA_DST:
sa = &rt->rt_dest;
break;
case RTA_GATEWAY:
sa = &rt->rt_gateway;
break;
case RTA_PREFSRC:
sa = &rt->rt_ifa;
break;
case RTA_OIF:
ifindex = *(unsigned int *)RTA_DATA(rta);
rt->rt_ifp = if_findindex(ctx->ifaces, ifindex);
break;
case RTA_PRIORITY:
rt->rt_metric = *(unsigned int *)RTA_DATA(rta);
break;
case RTA_METRICS:
{
struct rtattr *r2;
size_t l2;
l2 = rta->rta_len;
r2 = (struct rtattr *)RTA_DATA(rta);
while (RTA_OK(r2, l2)) {
switch (r2->rta_type) {
case RTAX_MTU:
rt->rt_mtu = *(unsigned int *)RTA_DATA(r2);
break;
}
r2 = RTA_NEXT(r2, l2);
}
break;
}
}
if (sa != NULL) {
socklen_t salen;
sa->sa_family = rtm->rtm_family;
salen = sa_addrlen(sa);
memcpy((char *)sa + sa_addroffset(sa), RTA_DATA(rta),
MIN(salen, RTA_PAYLOAD(rta)));
}
rta = RTA_NEXT(rta, len);
}
/* If no RTA_DST set the unspecified address for the family. */
if (rt->rt_dest.sa_family == AF_UNSPEC)
rt->rt_dest.sa_family = rtm->rtm_family;
rt->rt_netmask.sa_family = rtm->rtm_family;
sa_fromprefix(&rt->rt_netmask, rtm->rtm_dst_len);
#if 0
if (rt->rtp_ifp == NULL && rt->src.s_addr != INADDR_ANY) {
struct ipv4_addr *ap;
/* For some reason the default route comes back with the
* loopback interface in RTA_OIF? Lets find it by
* preferred source address */
if ((ap = ipv4_findaddr(ctx, &rt->src)))
rt->iface = ap->iface;
}
#endif
if (rt->rt_ifp == NULL) {
errno = ESRCH;
return -1;
}
return 0;
}
static int
link_route(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
struct nlmsghdr *nlm)
{
size_t len;
int cmd;
struct priv *priv;
struct rt rt;
switch (nlm->nlmsg_type) {
case RTM_NEWROUTE:
cmd = RTM_ADD;
break;
case RTM_DELROUTE:
cmd = RTM_DELETE;
break;
default:
return 0;
}
len = nlm->nlmsg_len - sizeof(*nlm);
if (len < sizeof(struct rtmsg)) {
errno = EBADMSG;
return -1;
}
/* Ignore messages we sent. */
priv = (struct priv *)ctx->priv;
if (nlm->nlmsg_pid == priv->route_pid)
return 0;
if (if_copyrt(ctx, &rt, nlm) == 0)
rt_recvrt(cmd, &rt);
return 0;
}
static int
link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm)
{
size_t len;
struct rtattr *rta;
struct ifaddrmsg *ifa;
struct priv *priv;
#ifdef INET
struct in_addr addr, net, brd;
#endif
#ifdef INET6
struct in6_addr addr6;
#endif
if (nlm->nlmsg_type != RTM_DELADDR && nlm->nlmsg_type != RTM_NEWADDR)
return 0;
len = nlm->nlmsg_len - sizeof(*nlm);
if (len < sizeof(*ifa)) {
errno = EBADMSG;
return -1;
}
/* Ignore messages we sent. */
priv = (struct priv*)ctx->priv;
if (nlm->nlmsg_pid == priv->route_pid)
return 0;
ifa = NLMSG_DATA(nlm);
if ((ifp = if_findindex(ctx->ifaces, ifa->ifa_index)) == NULL) {
/* We don't know about the interface the address is for
* so it's not really an error */
return 1;
}
rta = (struct rtattr *)IFA_RTA(ifa);
len = NLMSG_PAYLOAD(nlm, sizeof(*ifa));
switch (ifa->ifa_family) {
#ifdef INET
case AF_INET:
addr.s_addr = brd.s_addr = INADDR_ANY;
inet_cidrtoaddr(ifa->ifa_prefixlen, &net);
while (RTA_OK(rta, len)) {
switch (rta->rta_type) {
case IFA_ADDRESS:
if (ifp->flags & IFF_POINTOPOINT) {
memcpy(&brd.s_addr, RTA_DATA(rta),
sizeof(brd.s_addr));
}
break;
case IFA_BROADCAST:
memcpy(&brd.s_addr, RTA_DATA(rta),
sizeof(brd.s_addr));
break;
case IFA_LOCAL:
memcpy(&addr.s_addr, RTA_DATA(rta),
sizeof(addr.s_addr));
break;
}
rta = RTA_NEXT(rta, len);
}
ipv4_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name,
&addr, &net, &brd, ifa->ifa_flags);
break;
#endif
#ifdef INET6
case AF_INET6:
memset(&addr6, 0, sizeof(addr6));
while (RTA_OK(rta, len)) {
switch (rta->rta_type) {
case IFA_ADDRESS:
memcpy(&addr6.s6_addr, RTA_DATA(rta),
sizeof(addr6.s6_addr));
break;
}
rta = RTA_NEXT(rta, len);
}
ipv6_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name,
&addr6, ifa->ifa_prefixlen, ifa->ifa_flags);
break;
#endif
}
return 0;
}
static uint8_t
l2addr_len(unsigned short if_type)
{
switch (if_type) {
case ARPHRD_ETHER: /* FALLTHROUGH */
case ARPHRD_IEEE802: /*FALLTHROUGH */
case ARPHRD_IEEE80211:
return 6;
case ARPHRD_IEEE1394:
return 8;
case ARPHRD_INFINIBAND:
return 20;
}
/* Impossible */
return 0;
}
static int
handle_rename(struct dhcpcd_ctx *ctx, unsigned int ifindex, const char *ifname)
{
struct interface *ifp;
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
if (ifp->index == ifindex && strcmp(ifp->name, ifname)) {
dhcpcd_handleinterface(ctx, -1, ifp->name);
/* Let dev announce the interface for renaming */
if (!dev_listening(ctx))
dhcpcd_handleinterface(ctx, 1, ifname);
return 1;
}
}
return 0;
}
#ifdef INET6
static int
link_neigh(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
struct nlmsghdr *nlm)
{
struct ndmsg *r;
struct rtattr *rta;
size_t len;
struct in6_addr addr6;
int flags;
if (nlm->nlmsg_type != RTM_NEWNEIGH && nlm->nlmsg_type != RTM_DELNEIGH)
return 0;
if (nlm->nlmsg_len < sizeof(*r))
return -1;
r = NLMSG_DATA(nlm);
rta = (struct rtattr *)RTM_RTA(r);
len = RTM_PAYLOAD(nlm);
if (r->ndm_family == AF_INET6) {
flags = 0;
if (r->ndm_flags & NTF_ROUTER)
flags |= IPV6ND_ROUTER;
if (nlm->nlmsg_type == RTM_NEWNEIGH &&
r->ndm_state &
(NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE |
NUD_PERMANENT))
flags |= IPV6ND_REACHABLE;
memset(&addr6, 0, sizeof(addr6));
while (RTA_OK(rta, len)) {
switch (rta->rta_type) {
case NDA_DST:
memcpy(&addr6.s6_addr, RTA_DATA(rta),
sizeof(addr6.s6_addr));
break;
}
rta = RTA_NEXT(rta, len);
}
ipv6nd_neighbour(ctx, &addr6, flags);
}
return 0;
}
#endif
static int
link_netlink(struct dhcpcd_ctx *ctx, struct interface *ifp,
struct nlmsghdr *nlm)
{
int r;
size_t len;
struct rtattr *rta, *hwaddr;
struct ifinfomsg *ifi;
char ifn[IF_NAMESIZE + 1];
r = link_route(ctx, ifp, nlm);
if (r != 0)
return r;
r = link_addr(ctx, ifp, nlm);
if (r != 0)
return r;
#ifdef INET6
r = link_neigh(ctx, ifp, nlm);
if (r != 0)
return r;
#endif
if (nlm->nlmsg_type != RTM_NEWLINK && nlm->nlmsg_type != RTM_DELLINK)
return 0;
len = nlm->nlmsg_len - sizeof(*nlm);
if ((size_t)len < sizeof(*ifi)) {
errno = EBADMSG;
return -1;
}
ifi = NLMSG_DATA(nlm);
if (ifi->ifi_flags & IFF_LOOPBACK)
return 0;
rta = (void *)((char *)ifi + NLMSG_ALIGN(sizeof(*ifi)));
len = NLMSG_PAYLOAD(nlm, sizeof(*ifi));
*ifn = '\0';
hwaddr = NULL;
while (RTA_OK(rta, len)) {
switch (rta->rta_type) {
case IFLA_WIRELESS:
/* Ignore wireless messages */
if (nlm->nlmsg_type == RTM_NEWLINK &&
ifi->ifi_change == 0)
return 0;
break;
case IFLA_IFNAME:
strlcpy(ifn, RTA_DATA(rta), sizeof(ifn));
break;
case IFLA_ADDRESS:
hwaddr = rta;
break;
}
rta = RTA_NEXT(rta, len);
}
if (nlm->nlmsg_type == RTM_DELLINK) {
dhcpcd_handleinterface(ctx, -1, ifn);
return 0;
}
/* Virtual interfaces may not get a valid hardware address
* at this point.
* To trigger a valid hardware address pickup we need to pretend
* that that don't exist until they have one. */
if (ifi->ifi_flags & IFF_MASTER && !hwaddr) {
dhcpcd_handleinterface(ctx, -1, ifn);
return 0;
}
/* Check for interface name change */
if (handle_rename(ctx, (unsigned int)ifi->ifi_index, ifn))
return 0;
/* Check for a new interface */
if ((ifp = if_find(ctx->ifaces, ifn)) == NULL) {
/* If are listening to a dev manager, let that announce
* the interface rather than the kernel. */
if (dev_listening(ctx) < 1)
dhcpcd_handleinterface(ctx, 1, ifn);
return 0;
}
/* Re-read hardware address and friends */
if (!(ifi->ifi_flags & IFF_UP) && hwaddr) {
uint8_t l;
l = l2addr_len(ifi->ifi_type);
if (hwaddr->rta_len == RTA_LENGTH(l))
dhcpcd_handlehwaddr(ctx, ifn, RTA_DATA(hwaddr), l);
}
dhcpcd_handlecarrier(ctx,
ifi->ifi_flags & IFF_RUNNING ? LINK_UP : LINK_DOWN,
ifi->ifi_flags, ifn);
return 0;
}
int
if_handlelink(struct dhcpcd_ctx *ctx)
{
return get_netlink(ctx, ctx->iov, NULL,
ctx->link_fd, MSG_DONTWAIT, &link_netlink);
}
static int
send_netlink(struct dhcpcd_ctx *ctx, struct interface *ifp,
int protocol, struct nlmsghdr *hdr,
int (*callback)(struct dhcpcd_ctx *, struct interface *, struct nlmsghdr *))
{
int s, r;
struct sockaddr_nl snl;
struct iovec iov[1];
struct msghdr msg;
memset(&snl, 0, sizeof(snl));
snl.nl_family = AF_NETLINK;
if (protocol == NETLINK_ROUTE) {
struct priv *priv;
priv = (struct priv *)ctx->priv;
s = priv->route_fd;
} else {
if ((s = _open_link_socket(&snl, protocol)) == -1)
return -1;
}
memset(&msg, 0, sizeof(msg));
msg.msg_name = &snl;
msg.msg_namelen = sizeof(snl);
memset(&iov, 0, sizeof(iov));
iov[0].iov_base = hdr;
iov[0].iov_len = hdr->nlmsg_len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
/* Request a reply */
hdr->nlmsg_flags |= NLM_F_ACK;
hdr->nlmsg_seq = (uint32_t)++ctx->seq;
if (sendmsg(s, &msg, 0) != -1) {
struct priv *priv;
priv = (struct priv *)ctx->priv;
ctx->sseq = ctx->seq;
r = get_netlink(ctx, priv->sndrcv_iov, ifp, s, 0, callback);
} else
r = -1;
if (protocol != NETLINK_ROUTE)
close(s);
return r;
}
#define NLMSG_TAIL(nmsg) \
((struct rtattr *)(((ptrdiff_t)(nmsg))+NLMSG_ALIGN((nmsg)->nlmsg_len)))
static int
add_attr_l(struct nlmsghdr *n, unsigned short maxlen, unsigned short type,
const void *data, unsigned short alen)
{
unsigned short len = (unsigned short)RTA_LENGTH(alen);
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
errno = ENOBUFS;
return -1;
}
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
if (alen)
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
return 0;
}
static int
add_attr_32(struct nlmsghdr *n, unsigned short maxlen, unsigned short type,
uint32_t data)
{
unsigned short len = RTA_LENGTH(sizeof(data));
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
errno = ENOBUFS;
return -1;
}
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
memcpy(RTA_DATA(rta), &data, sizeof(data));
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
return 0;
}
static int
rta_add_attr_32(struct rtattr *rta, unsigned short maxlen,
unsigned short type, uint32_t data)
{
unsigned short len = RTA_LENGTH(sizeof(data));
struct rtattr *subrta;
if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
errno = ENOBUFS;
return -1;
}
subrta = (void *)((char*)rta + RTA_ALIGN(rta->rta_len));
subrta->rta_type = type;
subrta->rta_len = len;
memcpy(RTA_DATA(subrta), &data, sizeof(data));
rta->rta_len = (unsigned short)(NLMSG_ALIGN(rta->rta_len) + len);
return 0;
}
#ifdef HAVE_NL80211_H
static struct nlattr *
nla_next(struct nlattr *nla, size_t *rem)
{
*rem -= (size_t)NLA_ALIGN(nla->nla_len);
return (void *)((char *)nla + NLA_ALIGN(nla->nla_len));
}
#define NLA_TYPE(nla) ((nla)->nla_type & NLA_TYPE_MASK)
#define NLA_LEN(nla) (unsigned int)((nla)->nla_len - NLA_HDRLEN)
#define NLA_OK(nla, rem) \
((rem) >= sizeof(struct nlattr) && \
(nla)->nla_len >= sizeof(struct nlattr) && \
(nla)->nla_len <= rem)
#define NLA_DATA(nla) (void *)((char *)(nla) + NLA_HDRLEN)
#define NLA_FOR_EACH_ATTR(pos, head, len, rem) \
for (pos = head, rem = len; \
NLA_OK(pos, rem); \
pos = nla_next(pos, &(rem)))
struct nlmg
{
struct nlmsghdr hdr;
struct genlmsghdr ghdr;
char buffer[64];
};
static int
nla_put_32(struct nlmsghdr *n, unsigned short maxlen,
unsigned short type, uint32_t data)
{
unsigned short len;
struct nlattr *nla;
len = NLA_ALIGN(NLA_HDRLEN + sizeof(data));
if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
errno = ENOBUFS;
return -1;
}
nla = (struct nlattr *)NLMSG_TAIL(n);
nla->nla_type = type;
nla->nla_len = len;
memcpy(NLA_DATA(nla), &data, sizeof(data));
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
return 0;
}
static int
nla_put_string(struct nlmsghdr *n, unsigned short maxlen,
unsigned short type, const char *data)
{
struct nlattr *nla;
size_t len, sl;
sl = strlen(data) + 1;
len = NLA_ALIGN(NLA_HDRLEN + sl);
if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
errno = ENOBUFS;
return -1;
}
nla = (struct nlattr *)NLMSG_TAIL(n);
nla->nla_type = type;
nla->nla_len = (unsigned short)len;
memcpy(NLA_DATA(nla), data, sl);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + (unsigned short)len;
return 0;
}
static int
nla_parse(struct nlattr *tb[], struct nlattr *head, size_t len, int maxtype)
{
struct nlattr *nla;
size_t rem;
int type;
memset(tb, 0, sizeof(*tb) * ((unsigned int)maxtype + 1));
NLA_FOR_EACH_ATTR(nla, head, len, rem) {
type = NLA_TYPE(nla);
if (type > maxtype)
continue;
tb[type] = nla;
}
return 0;