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
//! illumos C ABI bindings: constants, FFI declarations, and `repr(C)`
//! struct mirrors.
use ;
// libdladm constants
/// `dladm_status_t::DLADM_STATUS_OK`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdladm.h#L104>
pub const DLADM_STATUS_OK: u32 = 0;
/// `dladm_status_t::DLADM_STATUS_NOTFOUND`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdladm.h#L109>
pub const DLADM_STATUS_NOTFOUND: u32 = 5;
/// `DLADM_OPT_ACTIVE`, apply changes to the active configuration only
/// (not the persistent SMF-managed state).
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdladm.h#L86>
pub const DLADM_OPT_ACTIVE: c_uint = 0x0000_0001;
/// `IPTUN_PARAM_*`, bitfield in `IpTunParams.flags` telling libdladm which
/// fields of the struct are populated.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdliptun.h#L49-L51>
pub const IPTUN_PARAM_TYPE: c_uint = 0x0000_0001;
pub const IPTUN_PARAM_LADDR: c_uint = 0x0000_0002;
pub const IPTUN_PARAM_RADDR: c_uint = 0x0000_0004;
/// `dladm_prop_type_t::DLADM_PROP_VAL_CURRENT`, request the property's
/// currently active value.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdllink.h#L57-L63>
pub const DLADM_PROP_VAL_CURRENT: u32 = 1;
/// `DLADM_PROP_VAL_MAX`, minimum size in bytes of each property value
/// buffer passed to `dladm_get_linkprop`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdllink.h#L77-L82>
pub const DLADM_PROP_VAL_MAX: usize = 128;
// libipadm constants
/// `ipadm_status_t::IPADM_SUCCESS`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libipadm/common/libipadm.h#L62>
pub const IPADM_STATUS_OK: u32 = 0;
/// `IPADM_OPT_ACTIVE`, apply to live config only.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libipadm/common/libipadm.h#L162>
pub const IPADM_OPT_ACTIVE: u32 = 0x0000_0002;
// POSIX socket / netdb constants
pub const AF_INET: u16 = AF_INET as u16;
pub const NI_MAXHOST: usize = NI_MAXHOST as usize;
// libdladm + libipadm FFI
//
// Linkage configured in build.rs (-ldladm, -lipadm).
// Source: usr/src/lib/libdladm/, usr/src/lib/libipadm/ in illumos-gate.
unsafe extern "C"
// `struct lifreq` mirror
//
// `lifreq` is an illumos-specific extension of POSIX `ifreq`.
//
// Used for the SIOCSLIF* ioctls that assign the IPv4 address directly,
// bypassing libipadm (issue 17851). The kernel reads/writes our memory
// at this layout, so it must match C exactly. Layout verified on OmniOS
// r151054 via crates/dslite-b4/scripts/probe_lifreq.c, re-run that probe if the size
// assertion below ever fails.
//
// Initialized via `MaybeUninit::zeroed().assume_init()` rather than a
// struct literal. A struct-literal init of the embedded `LifrLifru`
// union picks one variant (e.g. `flags: 0u64`), but bytes beyond that
// variant's size are padding with unspecified contents. The kernel ABI
// expects a fully-zeroed buffer for unused bytes, both to avoid leaking
// stack data into the kernel and to remain correct across SIOCSLIF*
// variants that read different shapes from the same union slot.
/// `_LIFNAMSIZ`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/if.h#L345>
pub const LIFNAMSIZ: usize = 32;
/// `_pad` pulls the union size up to 336 bytes, matching the
/// largest C variant (`lif_nd_req` / `lif_ifinfo_req`).
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/if.h#L372-L389>
pub union LifrLifru
/// `struct lifreq`, argument for SIOCSLIF* ioctls. `lifr_lifru1` is a
/// placeholder for the not used union.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/if.h#L359-L401>
const _: = assert!;
// kernel ioctl request encoding
// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/sys/ioccom.h>
/// `IOCPARM_MASK`, low byte of the encoded struct size.
pub const IOCPARM_MASK: u32 = 0xff;
/// `IOC_OUT`, kernel writes the user buffer.
pub const IOC_OUT: u32 = 0x4000_0000;
/// `IOC_IN`, kernel reads the user buffer.
pub const IOC_IN: u32 = 0x8000_0000;
/// `IOC_INOUT`, kernel reads then writes.
pub const IOC_INOUT: u32 = IOC_IN | IOC_OUT;
/// Encode an ioctl request number, mirroring `_IOW`/`_IOWR` from
/// `<sys/ioccom.h>`. `direction` is one of `IOC_IN`/`IOC_OUT`/`IOC_INOUT`,
/// `group` is the magic letter (e.g. `b'i'` for IP), `num` is the request
/// number within that group. Struct size is taken from `Lifreq` since every
/// SIOCSLIF* ioctl uses it.
pub const
// SIOCSLIF* ioctl numbers
// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/sys/sockio.h#L158-L175>
/// `SIOCSLIFADDR`, set local IP address.
pub const SIOCSLIFADDR: u32 = ioc;
/// `SIOCSLIFDSTADDR`, set point-to-point peer address.
pub const SIOCSLIFDSTADDR: u32 = ioc;
/// `SIOCSLIFFLAGS`, set interface flags (writes `lifr_lifru.flags`).
pub const SIOCSLIFFLAGS: u32 = ioc;
/// `SIOCGLIFFLAGS`, get interface flags.
pub const SIOCGLIFFLAGS: u32 = ioc;
/// `SIOCSLIFMTU`, set interface MTU.
pub const SIOCSLIFMTU: u32 = ioc;
/// `SIOCGLIFMTU`, get interface MTU.
pub const SIOCGLIFMTU: u32 = ioc;
/// `SIOCSLIFNETMASK`, set subnet mask.
pub const SIOCSLIFNETMASK: u32 = ioc;
/// `IFF_UP`, address is up. The flags slot in `lifr_lifru` is 64-bit.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/if.h#L106>
pub const IFF_UP: u64 = 0x0000_0000_0000_0001;
// libdladm IPsec / iptun parameter structs
//
// Argument types for `dladm_iptun_create`. The IPsec fields are unused,
// DS-Lite is plain IPv4-in-IPv6 but the struct shape has to match.
/// `ipsec_req_t`.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/netinet/in.h#L962>
/// `iptun_type_t`.
///
/// This FFI type is represented as an integer rather than a Rust enum
/// because C may write any value into it. In particular, an all-zero
/// `IpTunParams` output buffer must be valid before
/// `dladm_iptun_getparams` populates it.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/inet/iptun.h#L58>
pub type IpTunType = u32;
/// `IPTUN_TYPE_IPV6`, IPv4-in-IPv6 for DS-Lite.
pub const IPTUN_TYPE_IPV6: IpTunType = 2;
/// `iptun_params_t`. Address fields are NUL-terminated printable strings up to NI_MAXHOST bytes.
/// Libdladm calls getaddrinfo internally to parse them.
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/lib/libdladm/common/libdliptun.h#L46>
// Build a zero-initialized `Lifreq` with `lifr_name` populated from a
// `&CStr`. Shared between `set_lifr_addr` and `bring_up` as the prelude
// before issuing SIOCSLIF* ioctls. Returns `InvalidInput` when `name`
// would not fit (>= LIFNAMSIZ bytes).
// Shared body for the SIOCSLIF* ioctls that take an IPv4 address in the
// `lifr_lifru.addr` slot (SIOCSLIFADDR, SIOCSLIFDSTADDR, SIOCSLIFNETMASK).
// Builds a `Lifreq` shaped like this and fires `ioctl_num`:
//
// offset size
// 0 +--------------------------------+
// | lifr_name "<name>\0..." | 32
// 32 +--------------------------------+
// | lifr_lifru1 0 | 4
// 36 +--------------------------------+
// | lifr_type 0 | 4
// 40 +--------------------------------+
// | lifr_lifru.addr (union slot) | 336
// | as sockaddr_in: |
// | sin_family = AF_INET +0 |
// | sin_port = 0 +2 |
// | sin_addr = addr (BE) +4 |
// | sin_zero = 0... +8 |
// | ...rest of the 336 zero... |
// 376 +--------------------------------+
//
// `lifr_lifru` is a union: the same 336 bytes can be read as
// `sockaddr_storage`, `u64` (flags), etc. The variant used here is
// `sockaddr_in`. The kernel reads `sin_family` and only consumes the
// first 16 bytes. Everything past that stays zero from the zeroed init.
//
// Pointer casts on the address copy:
//
// `copy_nonoverlapping<T>(src, dst, count)` copies count*sizeof(T)
// bytes and needs src and dst to agree on T. The source is
// `*const sockaddr_in` (16B), the destination is
// `*mut sockaddr_storage` (128B). Casting both to `*u8` makes T = u8
// and `count` a byte count, so the types unify and 16 bytes are
// copied.
//
// `&raw const x` / `&raw mut x` give a raw pointer without the
// initialized + aligned + exclusive-borrow promises that `&` / `&mut`
// carry. That matters for union fields, where no single variant is
// "the" live one, and a `&mut lr.lifr_lifru.addr` would be claiming
// the bytes are validly a sockaddr_storage right now.
/// Issue a SIOCSLIF* ioctl that takes an IPv4 address in the
/// `lifr_lifru.addr` slot.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls. AF_INET / SOCK_DGRAM is the standard
/// choice on illumos.
/// - `ioctl_num` must be one of `SIOCSLIFADDR`, `SIOCSLIFDSTADDR`, or
/// `SIOCSLIFNETMASK`. Each of these expects a `Lifreq` with the
/// address slot of `lifr_lifru` populated. Other SIOCSLIF* numbers
/// (e.g. `SIOCSLIFFLAGS`) consume a different union slot and would
/// misinterpret the address-shaped buffer this function builds.
unsafe
/// Set the local IPv4 address on `name` (SIOCSLIFADDR).
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Set the point-to-point peer (destination) address on `name` (SIOCSLIFDSTADDR).
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Set the IPv4 subnet mask on `name` (SIOCSLIFNETMASK). `mask` is the
/// bit pattern (e.g. `255.255.255.248` for /29), not a prefix length.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Set the MTU configured for `name`.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Bring `name` up by OR-ing IFF_UP into its existing flags. Read-modify-write
/// pair (SIOCGLIFFLAGS then SIOCSLIFFLAGS) so other kernel-set flags
/// (RUNNING, BROADCAST, MULTICAST, ...) are not clobbered. The flags slot
/// is a different variant of `lifr_lifru` than the address slot used by the
/// SIOCSLIFADDR family.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Return whether `name` has `IFF_UP` set.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
/// Return the MTU configured for `name`.
///
/// # Safety
///
/// - `sock_fd` must be a valid, open file descriptor for a socket that
/// accepts SIOCSLIF* ioctls (AF_INET / SOCK_DGRAM is the standard
/// choice on illumos).
pub unsafe
// PF_ROUTE <net/route.h>
// Typed to match the destination fields in rt_msghdr
pub const RTM_VERSION: c_uchar = 3;
pub const RTM_ADD: c_uchar = 0x1;
pub const RTM_DELETE: c_uchar = 0x2;
pub const RTM_CHANGE: c_uchar = 0x3;
pub const RTM_NEWADDR: c_uchar = 0xc;
pub const RTM_DELADDR: c_uchar = 0xd;
pub const RTM_IFINFO: c_uchar = 0xe;
pub const RTM_CHGADDR: c_uchar = 0xf;
pub const RTM_FREEADDR: c_uchar = 0x10;
pub const RTF_UP: c_int = 0x1;
pub const RTF_GATEWAY: c_int = 0x2;
pub const RTF_STATIC: c_int = 0x800;
pub const RTA_DST: c_int = 0x1;
pub const RTA_GATEWAY: c_int = 0x2;
pub const RTA_NETMASK: c_int = 0x4;
/// Routing metrics carried inside `rt_msghdr` (10 × u32, 40 bytes).
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/route.h#L72-L84>
const _: = assert!;
/// <https://github.com/illumos/illumos-gate/blob/0764e87f4a667f36d63262fcdd690064929acc48/usr/src/uts/common/net/route.h#L152-L165>
const _: = assert!;