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-bsd.c
1589 lines (1402 loc) · 37.6 KB
/
if-bsd.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
/*
* BSD 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 <sys/ioctl.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include "config.h"
#include <arpa/inet.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/route.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet6/in6_var.h>
#include <netinet6/nd6.h>
#ifdef __DragonFly__
# include <netproto/802_11/ieee80211_ioctl.h>
#elif __APPLE__
/* FIXME: Add apple includes so we can work out SSID */
#else
# include <net80211/ieee80211.h>
# include <net80211/ieee80211_ioctl.h>
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <paths.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(OpenBSD) && OpenBSD >= 201411
/* OpenBSD dropped the global setting from sysctl but left the #define
* which causes a EPERM error when trying to use it.
* I think both the error and keeping the define are wrong, so we #undef it. */
#undef IPV6CTL_ACCEPT_RTADV
#endif
#include "common.h"
#include "dhcp.h"
#include "if.h"
#include "if-options.h"
#include "ipv4.h"
#include "ipv4ll.h"
#include "ipv6.h"
#include "ipv6nd.h"
#include "route.h"
#include "sa.h"
#include "bpf-filter.h"
#ifndef RT_ROUNDUP
#define RT_ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
#endif
#ifdef INET6
static void
ifa_scope(struct sockaddr_in6 *, unsigned int);
#endif
struct priv {
int pf_inet6_fd;
};
int
if_init(__unused struct interface *iface)
{
/* BSD promotes secondary address by default */
return 0;
}
int
if_conf(__unused struct interface *iface)
{
/* No extra checks needed on BSD */
return 0;
}
int
if_opensockets_os(struct dhcpcd_ctx *ctx)
{
struct priv *priv;
if ((priv = malloc(sizeof(*priv))) == NULL)
return -1;
ctx->priv = priv;
#ifdef INET6
priv->pf_inet6_fd = xsocket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
/* Don't return an error so we at least work on kernels witout INET6
* even though we expect INET6 support.
* We will fail noisily elsewhere anyway. */
#else
priv->pf_inet6_fd = -1;
#endif
#define SOCK_FLAGS (SOCK_CLOEXEC | SOCK_NONBLOCK)
ctx->link_fd = xsocket(PF_ROUTE, SOCK_RAW | SOCK_FLAGS, AF_UNSPEC);
#undef SOCK_FLAGS
return ctx->link_fd == -1 ? -1 : 0;
}
void
if_closesockets_os(struct dhcpcd_ctx *ctx)
{
struct priv *priv;
priv = (struct priv *)ctx->priv;
if (priv->pf_inet6_fd != -1)
close(priv->pf_inet6_fd);
}
#if defined(INET) || defined(INET6)
static void
if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp)
{
memset(sdl, 0, sizeof(*sdl));
sdl->sdl_family = AF_LINK;
sdl->sdl_len = sizeof(*sdl);
sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0;
sdl->sdl_index = (unsigned short)ifp->index;
}
#endif
static int
if_getssid1(int s, const char *ifname, void *ssid)
{
int retval = -1;
#if defined(SIOCG80211NWID)
struct ifreq ifr;
struct ieee80211_nwid nwid;
#elif defined(IEEE80211_IOC_SSID)
struct ieee80211req ireq;
char nwid[IEEE80211_NWID_LEN];
#endif
#if defined(SIOCG80211NWID) /* NetBSD */
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
memset(&nwid, 0, sizeof(nwid));
ifr.ifr_data = (void *)&nwid;
if (ioctl(s, SIOCG80211NWID, &ifr) == 0) {
if (ssid == NULL)
retval = nwid.i_len;
else if (nwid.i_len > IF_SSIDLEN)
errno = ENOBUFS;
else {
retval = nwid.i_len;
memcpy(ssid, nwid.i_nwid, nwid.i_len);
}
}
#elif defined(IEEE80211_IOC_SSID) /* FreeBSD */
memset(&ireq, 0, sizeof(ireq));
strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
ireq.i_type = IEEE80211_IOC_SSID;
ireq.i_val = -1;
memset(nwid, 0, sizeof(nwid));
ireq.i_data = &nwid;
if (ioctl(s, SIOCG80211, &ireq) == 0) {
if (ssid == NULL)
retval = ireq.i_len;
else if (ireq.i_len > IF_SSIDLEN)
errno = ENOBUFS;
else {
retval = ireq.i_len;
memcpy(ssid, nwid, ireq.i_len);
}
}
#else
errno = ENOSYS;
#endif
return retval;
}
int
if_getssid(struct interface *ifp)
{
int r;
r = if_getssid1(ifp->ctx->pf_inet_fd, ifp->name, ifp->ssid);
if (r != -1)
ifp->ssid_len = (unsigned int)r;
else
ifp->ssid_len = 0;
ifp->ssid[ifp->ssid_len] = '\0';
return r;
}
/*
* FreeBSD allows for Virtual Access Points
* We need to check if the interface is a Virtual Interface Master
* and if so, don't use it.
* This check is made by virtue of being a IEEE80211 device but
* returning the SSID gives an error.
*/
int
if_vimaster(const struct dhcpcd_ctx *ctx, const char *ifname)
{
int r;
struct ifmediareq ifmr;
memset(&ifmr, 0, sizeof(ifmr));
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
r = ioctl(ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr);
if (r == -1)
return -1;
if (ifmr.ifm_status & IFM_AVALID &&
IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211)
{
if (if_getssid1(ctx->pf_inet_fd, ifname, NULL) == -1)
return 1;
}
return 0;
}
static void
get_addrs(int type, const void *data, const struct sockaddr **sa)
{
const char *cp;
int i;
cp = data;
for (i = 0; i < RTAX_MAX; i++) {
if (type & (1 << i)) {
sa[i] = (const struct sockaddr *)cp;
RT_ADVANCE(cp, sa[i]);
} else
sa[i] = NULL;
}
}
#if defined(INET) || defined(INET6)
static struct interface *
if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl)
{
if (sdl->sdl_index)
return if_findindex(ctx->ifaces, sdl->sdl_index);
if (sdl->sdl_nlen) {
char ifname[IF_NAMESIZE];
memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
ifname[sdl->sdl_nlen] = '\0';
return if_find(ctx->ifaces, ifname);
}
if (sdl->sdl_alen) {
struct interface *ifp;
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
if (ifp->hwlen == sdl->sdl_alen &&
memcmp(ifp->hwaddr,
sdl->sdl_data, sdl->sdl_alen) == 0)
return ifp;
}
}
errno = ENOENT;
return NULL;
}
static struct interface *
if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa)
{
if (sa == NULL) {
errno = EINVAL;
return NULL;
}
switch (sa->sa_family) {
case AF_LINK:
{
const struct sockaddr_dl *sdl;
sdl = (const void *)sa;
return if_findsdl(ctx, sdl);
}
#ifdef INET
case AF_INET:
{
const struct sockaddr_in *sin;
struct ipv4_addr *ia;
sin = (const void *)sa;
if ((ia = ipv4_findmaskaddr(ctx, &sin->sin_addr)))
return ia->iface;
break;
}
#endif
#ifdef INET6
case AF_INET6:
{
const struct sockaddr_in6 *sin;
struct ipv6_addr *ia;
sin = (const void *)sa;
if ((ia = ipv6_findmaskaddr(ctx, &sin->sin6_addr)))
return ia->iface;
break;
}
#endif
default:
errno = EAFNOSUPPORT;
return NULL;
}
errno = ENOENT;
return NULL;
}
static void
if_copysa(struct sockaddr *dst, const struct sockaddr *src)
{
assert(dst != NULL);
assert(src != NULL);
memcpy(dst, src, src->sa_len);
#ifdef __KAME__
if (dst->sa_family == AF_INET6) {
struct in6_addr *in6;
in6 = &satosin6(dst)->sin6_addr;
if (IN6_IS_ADDR_LINKLOCAL(in6))
in6->s6_addr[2] = in6->s6_addr[3] = '\0';
}
#endif
}
int
if_route(unsigned char cmd, const struct rt *rt)
{
struct dhcpcd_ctx *ctx;
struct rtm
{
struct rt_msghdr hdr;
char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX];
} rtmsg;
struct rt_msghdr *rtm = &rtmsg.hdr;
char *bp = rtmsg.buffer;
size_t l;
struct sockaddr_dl sdl;
bool gateway_unspec;
assert(rt != NULL);
ctx = rt->rt_ifp->ctx;
if ((cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE) &&
ctx->options & DHCPCD_DAEMONISE &&
!(ctx->options & DHCPCD_DAEMONISED))
ctx->options |= DHCPCD_RTM_PPID;
#define ADDSA(sa) do { \
l = RT_ROUNDUP(((sa)->sa_len)); \
memcpy(bp, (sa), l); \
bp += l; \
} while (0 /* CONSTCOND */)
memset(rtm, 0, sizeof(*rtm));
rtm->rtm_version = RTM_VERSION;
rtm->rtm_type = cmd;
#ifdef __OpenBSD__
rtm->rtm_pid = getpid();
#endif
rtm->rtm_seq = ++ctx->seq;
rtm->rtm_flags = (int)rt->rt_flags;
rtm->rtm_addrs = RTA_DST;
#ifdef RTF_PINNED
if (cmd != RTM_ADD)
rtm->rtm_flags |= RTF_PINNED;
#endif
gateway_unspec = sa_is_unspecified(&rt->rt_gateway);
if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
bool netmask_bcast = sa_is_allones(&rt->rt_netmask);
rtm->rtm_flags |= RTF_UP;
rtm->rtm_addrs |= RTA_GATEWAY;
if (!(rtm->rtm_flags & RTF_REJECT) &&
!sa_is_loopback(&rt->rt_gateway))
{
if (!gateway_unspec)
rtm->rtm_addrs |= RTA_IFP;
if (!sa_is_unspecified(&rt->rt_ifa))
rtm->rtm_addrs |= RTA_IFA;
}
if (netmask_bcast)
rtm->rtm_flags |= RTF_HOST;
/* Network routes are cloning or connected if supported.
* All other routes are static. */
if (gateway_unspec) {
#ifdef RTF_CLONING
rtm->rtm_flags |= RTF_CLONING;
#endif
#ifdef RTF_CONNECTED
rtm->rtm_flags |= RTF_CONNECTED;
#endif
#ifdef RTP_CONNECTED
rtm->rtm_priority = RTP_CONNECTED;
#endif
#ifdef RTF_CLONING
if (netmask_bcast) {
/*
* We add a cloning network route for a single
* host. Traffic to the host will generate a
* cloned route and the hardware address will
* resolve correctly.
* It might be more correct to use RTF_HOST
* instead of RTF_CLONING, and that does work,
* but some OS generate an arp warning
* diagnostic which we don't want to do.
*/
rtm->rtm_flags &= ~RTF_HOST;
}
#endif
} else
rtm->rtm_flags |= RTF_GATEWAY;
/* Emulate the kernel by marking address generated
* network routes non-static. */
if (!(rt->rt_dflags & RTDF_IFA_ROUTE))
rtm->rtm_flags |= RTF_STATIC;
if (rt->rt_mtu != 0) {
rtm->rtm_inits |= RTV_MTU;
rtm->rtm_rmx.rmx_mtu = rt->rt_mtu;
}
}
if (!(rtm->rtm_flags & RTF_HOST))
rtm->rtm_addrs |= RTA_NETMASK;
if_linkaddr(&sdl, rt->rt_ifp);
ADDSA(&rt->rt_dest);
if (rtm->rtm_addrs & RTA_GATEWAY) {
if (gateway_unspec)
ADDSA((struct sockaddr *)&sdl);
else {
union sa_ss gateway;
if_copysa(&gateway.sa, &rt->rt_gateway);
#ifdef INET6
if (gateway.sa.sa_family == AF_INET6)
ifa_scope(&gateway.sin6, rt->rt_ifp->index);
#endif
ADDSA(&gateway.sa);
}
}
if (rtm->rtm_addrs & RTA_NETMASK)
ADDSA(&rt->rt_netmask);
if (rtm->rtm_addrs & RTA_IFP) {
rtm->rtm_index = (unsigned short)rt->rt_ifp->index;
ADDSA((struct sockaddr *)&sdl);
}
if (rtm->rtm_addrs & RTA_IFA)
ADDSA(&rt->rt_ifa);
#undef ADDSA
rtm->rtm_msglen = (unsigned short)(bp - (char *)rtm);
if (write(ctx->link_fd, rtm, rtm->rtm_msglen) == -1)
return -1;
ctx->sseq = ctx->seq;
return 0;
}
static int
if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm)
{
const struct sockaddr *rti_info[RTAX_MAX];
if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY))
return -1;
#ifdef RTF_CLONED
if (rtm->rtm_flags & RTF_CLONED)
return -1;
#endif
#ifdef RTF_LOCAL
if (rtm->rtm_flags & RTF_LOCAL)
return -1;
#endif
#ifdef RTF_BROADCAST
if (rtm->rtm_flags & RTF_BROADCAST)
return -1;
#endif
get_addrs(rtm->rtm_addrs, rtm + 1, rti_info);
memset(rt, 0, sizeof(*rt));
rt->rt_flags = (unsigned int)rtm->rtm_flags;
if_copysa(&rt->rt_dest, rti_info[RTAX_DST]);
if (rtm->rtm_addrs & RTA_NETMASK) {
if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
if (rt->rt_netmask.sa_family == 255) /* Why? */
rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
}
/* dhcpcd likes an unspecified gateway to indicate via the link. */
if (rt->rt_flags & RTF_GATEWAY &&
rti_info[RTAX_GATEWAY]->sa_family != AF_LINK)
if_copysa(&rt->rt_gateway, rti_info[RTAX_GATEWAY]);
if (rtm->rtm_addrs & RTA_IFA)
if_copysa(&rt->rt_ifa, rti_info[RTAX_IFA]);
rt->rt_mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu;
if (rtm->rtm_index)
rt->rt_ifp = if_findindex(ctx->ifaces, rtm->rtm_index);
else if (rtm->rtm_addrs & RTA_IFP)
rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_IFP]);
else if (rtm->rtm_addrs & RTA_GATEWAY)
rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_GATEWAY]);
else
rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_DST]);
if (rt->rt_ifp == NULL) {
errno = ESRCH;
return -1;
}
return 0;
}
int
if_initrt(struct dhcpcd_ctx *ctx, int af)
{
struct rt_msghdr *rtm;
int mib[6];
size_t needed;
char *buf, *p, *end;
struct rt rt;
rt_headclear(&ctx->kroutes, af);
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = af;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
return -1;
if (needed == 0)
return 0;
if ((buf = malloc(needed)) == NULL)
return -1;
if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
free(buf);
return -1;
}
end = buf + needed;
for (p = buf; p < end; p += rtm->rtm_msglen) {
rtm = (void *)p;
if (if_copyrt(ctx, &rt, rtm) == 0) {
rt.rt_dflags |= RTDF_INIT;
rt_recvrt(RTM_ADD, &rt);
}
}
free(buf);
return 0;
}
#endif
#ifdef INET
const char *if_pfname = "Berkley Packet Filter";
void
if_closeraw(__unused struct interface *ifp, int fd)
{
close(fd);
}
int
if_openraw(struct interface *ifp, uint16_t protocol)
{
struct ipv4_state *state;
int fd = -1;
struct ifreq ifr;
int ibuf_len = 0;
size_t buf_len;
struct bpf_version pv;
struct bpf_program pf;
#ifdef BIOCIMMEDIATE
int flags;
#endif
#ifndef O_CLOEXEC
int fd_opts;
#endif
#ifdef _PATH_BPF
fd = open(_PATH_BPF, O_RDWR | O_NONBLOCK
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
);
#else
char device[32];
int n = 0;
do {
snprintf(device, sizeof(device), "/dev/bpf%d", n++);
fd = open(device, O_RDWR | O_NONBLOCK
#ifdef O_CLOEXEC
| O_CLOEXEC
#endif
);
} while (fd == -1 && errno == EBUSY);
#endif
if (fd == -1)
return -1;
#ifndef O_CLOEXEC
if ((fd_opts = fcntl(fd, F_GETFD)) == -1 ||
fcntl(fd, F_SETFD, fd_opts | FD_CLOEXEC) == -1) {
close(fd);
return -1;
}
#endif
state = IPV4_STATE(ifp);
memset(&pv, 0, sizeof(pv));
if (ioctl(fd, BIOCVERSION, &pv) == -1)
goto eexit;
if (pv.bv_major != BPF_MAJOR_VERSION ||
pv.bv_minor < BPF_MINOR_VERSION) {
logger(ifp->ctx, LOG_ERR, "BPF version mismatch - recompile");
goto eexit;
}
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
if (ioctl(fd, BIOCSETIF, &ifr) == -1)
goto eexit;
/* Get the required BPF buffer length from the kernel. */
if (ioctl(fd, BIOCGBLEN, &ibuf_len) == -1)
goto eexit;
buf_len = (size_t)ibuf_len;
if (state->buffer_size != buf_len) {
free(state->buffer);
state->buffer = malloc(buf_len);
if (state->buffer == NULL)
goto eexit;
state->buffer_size = buf_len;
state->buffer_len = state->buffer_pos = 0;
}
#ifdef BIOCIMMEDIATE
flags = 1;
if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1)
goto eexit;
#endif
/* Install the filter. */
memset(&pf, 0, sizeof(pf));
if (protocol == ETHERTYPE_ARP) {
pf.bf_insns = UNCONST(arp_bpf_filter);
pf.bf_len = arp_bpf_filter_len;
} else {
pf.bf_insns = UNCONST(bootp_bpf_filter);
pf.bf_len = bootp_bpf_filter_len;
}
if (ioctl(fd, BIOCSETF, &pf) == -1)
goto eexit;
return fd;
eexit:
free(state->buffer);
state->buffer = NULL;
close(fd);
return -1;
}
ssize_t
if_sendraw(__unused const struct interface *ifp, int fd, uint16_t protocol,
const void *data, size_t len)
{
struct iovec iov[2];
struct ether_header hw;
memset(&hw, 0, ETHER_HDR_LEN);
memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN);
hw.ether_type = htons(protocol);
iov[0].iov_base = &hw;
iov[0].iov_len = ETHER_HDR_LEN;
iov[1].iov_base = UNCONST(data);
iov[1].iov_len = len;
return writev(fd, iov, 2);
}
/* BPF requires that we read the entire buffer.
* So we pass the buffer in the API so we can loop on >1 packet. */
ssize_t
if_readraw(struct interface *ifp, int fd, void *data, size_t len, int *flags)
{
struct bpf_hdr packet;
ssize_t bytes;
const char *payload;
struct ipv4_state *state;
state = IPV4_STATE(ifp);
*flags = 0;
for (;;) {
if (state->buffer_len == 0) {
bytes = read(fd, state->buffer, state->buffer_size);
if (bytes == -1 || bytes == 0)
return bytes;
state->buffer_len = (size_t)bytes;
state->buffer_pos = 0;
}
bytes = -1;
memcpy(&packet, state->buffer + state->buffer_pos,
sizeof(packet));
if (packet.bh_caplen != packet.bh_datalen)
goto next; /* Incomplete packet, drop. */
if (state->buffer_pos + packet.bh_caplen + packet.bh_hdrlen >
state->buffer_len)
goto next; /* Packet beyond buffer, drop. */
payload = state->buffer + state->buffer_pos +
packet.bh_hdrlen + ETHER_HDR_LEN;
bytes = (ssize_t)packet.bh_caplen - ETHER_HDR_LEN;
if ((size_t)bytes > len)
bytes = (ssize_t)len;
memcpy(data, payload, (size_t)bytes);
next:
state->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen +
packet.bh_caplen);
if (state->buffer_pos >= state->buffer_len) {
state->buffer_len = state->buffer_pos = 0;
*flags |= RAW_EOF;
}
if (bytes != -1)
return bytes;
}
}
int
if_address(unsigned char cmd, const struct ipv4_addr *ia)
{
int r;
struct in_aliasreq ifra;
memset(&ifra, 0, sizeof(ifra));
strlcpy(ifra.ifra_name, ia->iface->name, sizeof(ifra.ifra_name));
#define ADDADDR(var, addr) do { \
(var)->sin_family = AF_INET; \
(var)->sin_len = sizeof(*(var)); \
(var)->sin_addr = *(addr); \
} while (/*CONSTCOND*/0)
ADDADDR(&ifra.ifra_addr, &ia->addr);
ADDADDR(&ifra.ifra_mask, &ia->mask);
if (cmd == RTM_NEWADDR && ia->brd.s_addr != INADDR_ANY)
ADDADDR(&ifra.ifra_broadaddr, &ia->brd);
#undef ADDADDR
r = ioctl(ia->iface->ctx->pf_inet_fd,
cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra);
return r;
}
#if !(defined(HAVE_IFADDRS_ADDRFLAGS) && defined(HAVE_IFAM_ADDRFLAGS))
int
if_addrflags(const struct interface *ifp, const struct in_addr *addr,
__unused const char *alias)
{
#ifdef SIOCGIFAFLAG_IN
struct ifreq ifr;
struct sockaddr_in *sin;
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
sin = (void *)&ifr.ifr_addr;
sin->sin_family = AF_INET;
sin->sin_addr = *addr;
if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFAFLAG_IN, &ifr) == -1)
return -1;
return ifr.ifr_addrflags;
#else
UNUSED(ifp);
UNUSED(addr);
return 0;
#endif
}
#endif
#endif /* INET */
#ifdef INET6
static void
ifa_scope(struct sockaddr_in6 *sin, unsigned int ifindex)
{
#ifdef __KAME__
/* KAME based systems want to store the scope inside the sin6_addr
* for link local addreses */
if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
uint16_t scope = htons((uint16_t)ifindex);
memcpy(&sin->sin6_addr.s6_addr[2], &scope,
sizeof(scope));
}
sin->sin6_scope_id = 0;
#else
if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr))
sin->sin6_scope_id = ifindex;
else
sin->sin6_scope_id = 0;
#endif
}
int
if_address6(unsigned char cmd, const struct ipv6_addr *ia)
{
struct in6_aliasreq ifa;
struct in6_addr mask;
struct priv *priv;
priv = (struct priv *)ia->iface->ctx->priv;
memset(&ifa, 0, sizeof(ifa));
strlcpy(ifa.ifra_name, ia->iface->name, sizeof(ifa.ifra_name));
/*
* We should not set IN6_IFF_TENTATIVE as the kernel should be
* able to work out if it's a new address or not.
*
* We should set IN6_IFF_AUTOCONF, but the kernel won't let us.
* This is probably a safety measure, but still it's not entirely right
* either.
*/
#if 0
if (ia->autoconf)
ifa.ifra_flags |= IN6_IFF_AUTOCONF;
#endif
#ifdef IPV6_MANGETEMPADDR
if (ia->flags & IPV6_AF_TEMPORARY)
ifa.ifra_flags |= IN6_IFF_TEMPORARY;
#endif
#define ADDADDR(v, addr) { \
(v)->sin6_family = AF_INET6; \
(v)->sin6_len = sizeof(*v); \
(v)->sin6_addr = *(addr); \
}
ADDADDR(&ifa.ifra_addr, &ia->addr);
ifa_scope(&ifa.ifra_addr, ia->iface->index);
ipv6_mask(&mask, ia->prefix_len);
ADDADDR(&ifa.ifra_prefixmask, &mask);
#undef ADDADDR
/*
* Every BSD kernel wants to add the prefix of the address to it's
* list of RA received prefixes.
* THIS IS WRONG because there (as the comments in the kernel state)
* is no API for managing prefix lifetime and the kernel should not
* pretend it's from a RA either.
*
* The issue is that the very first assigned prefix will inherit the
* lifetime of the address, but any subsequent alteration of the
* address OR it's lifetime will not affect the prefix lifetime.
* As such, we cannot stop the prefix from timing out and then
* constantly removing the prefix route dhcpcd is capable of adding
* in it's absense.
*
* What we can do to mitigate the issue is to add the address with
* infinite lifetimes, so the prefix route will never time out.
* Once done, we can then set lifetimes on the address and all is good.
* The downside of this approach is that we need to manually remove
* the kernel route because it has no lifetime, but this is OK as
* dhcpcd will handle this too.
*
* This issue is discussed on the NetBSD mailing lists here:
* http://mail-index.netbsd.org/tech-net/2016/08/05/msg006044.html
*
* Fixed in NetBSD-7.99.36
* NOT fixed in FreeBSD - bug 195197
* Fixed in OpenBSD-5.9
*/
#if !((defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003600) || \
(defined(__OpenBSD__)))
if (cmd == RTM_NEWADDR && !(ia->flags & IPV6_AF_ADDED)) {
ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
(void)ioctl(priv->pf_inet6_fd, SIOCAIFADDR_IN6, &ifa);
}
#endif
#if defined(__OpenBSD__)
/* BUT OpenBSD does not reset the address lifetime
* for subsequent calls...
* Luckily dhcpcd will remove the lease when it expires so
* just set an infinite lifetime, unless a temporary address. */
if (ifa.ifra_flags & IN6_IFF_PRIVACY) {
ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
} else {
ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
}
#else
ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
#endif
return ioctl(priv->pf_inet6_fd,
cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6, &ifa);
}
#if !(defined(HAVE_IFADDRS_ADDRFLAGS) && defined(HAVE_IFAM_ADDRFLAGS))
int
if_addrflags6(const struct interface *ifp, const struct in6_addr *addr,
__unused const char *alias)
{
int flags;
struct in6_ifreq ifr6;
struct priv *priv;
memset(&ifr6, 0, sizeof(ifr6));
strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr.sin6_family = AF_INET6;
ifr6.ifr_addr.sin6_addr = *addr;
ifa_scope(&ifr6.ifr_addr, ifp->index);
priv = (struct priv *)ifp->ctx->priv;
if (ioctl(priv->pf_inet6_fd, SIOCGIFAFLAG_IN6, &ifr6) != -1)
flags = ifr6.ifr_ifru.ifru_flags6;
else
flags = -1;
return flags;
}
#endif
int
if_getlifetime6(struct ipv6_addr *ia)
{
struct in6_ifreq ifr6;
time_t t;
struct in6_addrlifetime *lifetime;
struct priv *priv;
memset(&ifr6, 0, sizeof(ifr6));
strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
ifr6.ifr_addr.sin6_family = AF_INET6;
ifr6.ifr_addr.sin6_addr = ia->addr;
ifa_scope(&ifr6.ifr_addr, ia->iface->index);
priv = (struct priv *)ia->iface->ctx->priv;
if (ioctl(priv->pf_inet6_fd, SIOCGIFALIFETIME_IN6, &ifr6) == -1)