1#![doc = "Auxilary types not porovided by any particular family\n"]
2#![allow(clippy::all)]
3#![allow(unused_imports)]
4#![allow(unused_assignments)]
5#![allow(non_snake_case)]
6#![allow(unused_variables)]
7#![allow(irrefutable_let_patterns)]
8#![allow(unreachable_code)]
9#![allow(unreachable_patterns)]
10#[cfg(test)]
11mod tests;
12use crate::{
13 consts,
14 traits::{NetlinkRequest, Protocol},
15 utils::*,
16};
17pub const PROTONAME: &CStr = c"builtin";
18#[doc = "Generic family header"]
19#[doc = "Wrapper for bitfield32 type"]
20#[doc = "Header of a Netlink message"]
21#[derive(Clone)]
22pub enum Dummy {}
23impl<'a> IterableDummy<'a> {}
24impl Dummy {
25 pub fn new(buf: &'_ [u8]) -> IterableDummy<'_> {
26 IterableDummy::with_loc(buf, buf.as_ptr() as usize)
27 }
28 fn attr_from_type(r#type: u16) -> Option<&'static str> {
29 None
30 }
31}
32#[derive(Clone, Copy, Default)]
33pub struct IterableDummy<'a> {
34 buf: &'a [u8],
35 pos: usize,
36 orig_loc: usize,
37}
38impl<'a> IterableDummy<'a> {
39 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
40 Self {
41 buf,
42 pos: 0,
43 orig_loc,
44 }
45 }
46 pub fn get_buf(&self) -> &'a [u8] {
47 self.buf
48 }
49}
50impl<'a> Iterator for IterableDummy<'a> {
51 type Item = Result<Dummy, ErrorContext>;
52 fn next(&mut self) -> Option<Self::Item> {
53 if self.buf.len() == self.pos {
54 return None;
55 }
56 let pos = self.pos;
57 let mut r#type = None;
58 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
59 r#type = Some(header.r#type);
60 let res = match header.r#type {
61 n => {
62 if cfg!(any(test, feature = "deny-unknown-attrs")) {
63 break;
64 } else {
65 continue;
66 }
67 }
68 };
69 return Some(Ok(res));
70 }
71 Some(Err(ErrorContext::new(
72 "Dummy",
73 r#type.and_then(|t| Dummy::attr_from_type(t)),
74 self.orig_loc,
75 self.buf.as_ptr().wrapping_add(pos) as usize,
76 )))
77 }
78}
79impl std::fmt::Debug for IterableDummy<'_> {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 let mut fmt = f.debug_struct("Dummy");
82 for attr in self.clone() {
83 let attr = match attr {
84 Ok(a) => a,
85 Err(err) => {
86 fmt.finish()?;
87 f.write_str("Err(")?;
88 err.fmt(f)?;
89 return f.write_str(")");
90 }
91 };
92 match attr {};
93 }
94 fmt.finish()
95 }
96}
97impl IterableDummy<'_> {
98 pub fn lookup_attr(
99 &self,
100 offset: usize,
101 missing_type: Option<u16>,
102 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
103 let mut stack = Vec::new();
104 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
105 if cur == offset {
106 stack.push(("Dummy", offset));
107 return (stack, missing_type.and_then(|t| Dummy::attr_from_type(t)));
108 }
109 (stack, None)
110 }
111}
112#[derive(Clone)]
113pub enum NlmsgerrAttrs<'a> {
114 #[doc = "error message string (string)"]
115 Msg(&'a CStr),
116 #[doc = "offset of the invalid attribute in the original message, counting from the beginning of the header (u32)"]
117 Offset(u32),
118 #[doc = "arbitrary subsystem specific cookie to be used - in the success case - to identify a created object or operation or similar (binary)"]
119 Cookie(&'a [u8]),
120 #[doc = "policy for a rejected attribute"]
121 Policy(IterablePolicyTypeAttrs<'a>),
122 #[doc = "type of a missing required attribute, NLMSGERR_ATTR_MISS_NEST will not be present if the attribute was missing at the message level"]
123 MissingType(u16),
124 #[doc = "offset of the nest where attribute was missing"]
125 MissingNest(u32),
126}
127impl<'a> IterableNlmsgerrAttrs<'a> {
128 #[doc = "error message string (string)"]
129 pub fn get_msg(&self) -> Result<&'a CStr, ErrorContext> {
130 let mut iter = self.clone();
131 iter.pos = 0;
132 for attr in iter {
133 if let NlmsgerrAttrs::Msg(val) = attr? {
134 return Ok(val);
135 }
136 }
137 Err(ErrorContext::new_missing(
138 "NlmsgerrAttrs",
139 "Msg",
140 self.orig_loc,
141 self.buf.as_ptr() as usize,
142 ))
143 }
144 #[doc = "offset of the invalid attribute in the original message, counting from the beginning of the header (u32)"]
145 pub fn get_offset(&self) -> Result<u32, ErrorContext> {
146 let mut iter = self.clone();
147 iter.pos = 0;
148 for attr in iter {
149 if let NlmsgerrAttrs::Offset(val) = attr? {
150 return Ok(val);
151 }
152 }
153 Err(ErrorContext::new_missing(
154 "NlmsgerrAttrs",
155 "Offset",
156 self.orig_loc,
157 self.buf.as_ptr() as usize,
158 ))
159 }
160 #[doc = "arbitrary subsystem specific cookie to be used - in the success case - to identify a created object or operation or similar (binary)"]
161 pub fn get_cookie(&self) -> Result<&'a [u8], ErrorContext> {
162 let mut iter = self.clone();
163 iter.pos = 0;
164 for attr in iter {
165 if let NlmsgerrAttrs::Cookie(val) = attr? {
166 return Ok(val);
167 }
168 }
169 Err(ErrorContext::new_missing(
170 "NlmsgerrAttrs",
171 "Cookie",
172 self.orig_loc,
173 self.buf.as_ptr() as usize,
174 ))
175 }
176 #[doc = "policy for a rejected attribute"]
177 pub fn get_policy(&self) -> Result<IterablePolicyTypeAttrs<'a>, ErrorContext> {
178 let mut iter = self.clone();
179 iter.pos = 0;
180 for attr in iter {
181 if let NlmsgerrAttrs::Policy(val) = attr? {
182 return Ok(val);
183 }
184 }
185 Err(ErrorContext::new_missing(
186 "NlmsgerrAttrs",
187 "Policy",
188 self.orig_loc,
189 self.buf.as_ptr() as usize,
190 ))
191 }
192 #[doc = "type of a missing required attribute, NLMSGERR_ATTR_MISS_NEST will not be present if the attribute was missing at the message level"]
193 pub fn get_missing_type(&self) -> Result<u16, ErrorContext> {
194 let mut iter = self.clone();
195 iter.pos = 0;
196 for attr in iter {
197 if let NlmsgerrAttrs::MissingType(val) = attr? {
198 return Ok(val);
199 }
200 }
201 Err(ErrorContext::new_missing(
202 "NlmsgerrAttrs",
203 "MissingType",
204 self.orig_loc,
205 self.buf.as_ptr() as usize,
206 ))
207 }
208 #[doc = "offset of the nest where attribute was missing"]
209 pub fn get_missing_nest(&self) -> Result<u32, ErrorContext> {
210 let mut iter = self.clone();
211 iter.pos = 0;
212 for attr in iter {
213 if let NlmsgerrAttrs::MissingNest(val) = attr? {
214 return Ok(val);
215 }
216 }
217 Err(ErrorContext::new_missing(
218 "NlmsgerrAttrs",
219 "MissingNest",
220 self.orig_loc,
221 self.buf.as_ptr() as usize,
222 ))
223 }
224}
225impl<'a> NlmsgerrAttrs<'a> {
226 pub fn new(buf: &'a [u8]) -> IterableNlmsgerrAttrs<'a> {
227 IterableNlmsgerrAttrs::with_loc(buf, buf.as_ptr() as usize)
228 }
229 fn attr_from_type(r#type: u16) -> Option<&'static str> {
230 let res = match r#type {
231 0u16 => "Unused",
232 1u16 => "Msg",
233 2u16 => "Offset",
234 3u16 => "Cookie",
235 4u16 => "Policy",
236 5u16 => "MissingType",
237 6u16 => "MissingNest",
238 _ => return None,
239 };
240 Some(res)
241 }
242}
243#[derive(Clone, Copy, Default)]
244pub struct IterableNlmsgerrAttrs<'a> {
245 buf: &'a [u8],
246 pos: usize,
247 orig_loc: usize,
248}
249impl<'a> IterableNlmsgerrAttrs<'a> {
250 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
251 Self {
252 buf,
253 pos: 0,
254 orig_loc,
255 }
256 }
257 pub fn get_buf(&self) -> &'a [u8] {
258 self.buf
259 }
260}
261impl<'a> Iterator for IterableNlmsgerrAttrs<'a> {
262 type Item = Result<NlmsgerrAttrs<'a>, ErrorContext>;
263 fn next(&mut self) -> Option<Self::Item> {
264 if self.buf.len() == self.pos {
265 return None;
266 }
267 let pos = self.pos;
268 let mut r#type = None;
269 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
270 r#type = Some(header.r#type);
271 let res = match header.r#type {
272 1u16 => NlmsgerrAttrs::Msg({
273 let res = CStr::from_bytes_with_nul(next).ok();
274 let Some(val) = res else { break };
275 val
276 }),
277 2u16 => NlmsgerrAttrs::Offset({
278 let res = parse_u32(next);
279 let Some(val) = res else { break };
280 val
281 }),
282 3u16 => NlmsgerrAttrs::Cookie({
283 let res = Some(next);
284 let Some(val) = res else { break };
285 val
286 }),
287 4u16 => NlmsgerrAttrs::Policy({
288 let res = Some(IterablePolicyTypeAttrs::with_loc(next, self.orig_loc));
289 let Some(val) = res else { break };
290 val
291 }),
292 5u16 => NlmsgerrAttrs::MissingType({
293 let res = parse_u16(next);
294 let Some(val) = res else { break };
295 val
296 }),
297 6u16 => NlmsgerrAttrs::MissingNest({
298 let res = parse_u32(next);
299 let Some(val) = res else { break };
300 val
301 }),
302 n => {
303 if cfg!(any(test, feature = "deny-unknown-attrs")) {
304 break;
305 } else {
306 continue;
307 }
308 }
309 };
310 return Some(Ok(res));
311 }
312 Some(Err(ErrorContext::new(
313 "NlmsgerrAttrs",
314 r#type.and_then(|t| NlmsgerrAttrs::attr_from_type(t)),
315 self.orig_loc,
316 self.buf.as_ptr().wrapping_add(pos) as usize,
317 )))
318 }
319}
320impl<'a> std::fmt::Debug for IterableNlmsgerrAttrs<'_> {
321 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
322 let mut fmt = f.debug_struct("NlmsgerrAttrs");
323 for attr in self.clone() {
324 let attr = match attr {
325 Ok(a) => a,
326 Err(err) => {
327 fmt.finish()?;
328 f.write_str("Err(")?;
329 err.fmt(f)?;
330 return f.write_str(")");
331 }
332 };
333 match attr {
334 NlmsgerrAttrs::Msg(val) => fmt.field("Msg", &val),
335 NlmsgerrAttrs::Offset(val) => fmt.field("Offset", &val),
336 NlmsgerrAttrs::Cookie(val) => fmt.field("Cookie", &val),
337 NlmsgerrAttrs::Policy(val) => fmt.field("Policy", &val),
338 NlmsgerrAttrs::MissingType(val) => fmt.field("MissingType", &val),
339 NlmsgerrAttrs::MissingNest(val) => fmt.field("MissingNest", &val),
340 };
341 }
342 fmt.finish()
343 }
344}
345impl IterableNlmsgerrAttrs<'_> {
346 pub fn lookup_attr(
347 &self,
348 offset: usize,
349 missing_type: Option<u16>,
350 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
351 let mut stack = Vec::new();
352 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
353 if cur == offset {
354 stack.push(("NlmsgerrAttrs", offset));
355 return (
356 stack,
357 missing_type.and_then(|t| NlmsgerrAttrs::attr_from_type(t)),
358 );
359 }
360 if cur > offset || cur + self.buf.len() < offset {
361 return (stack, None);
362 }
363 let mut attrs = self.clone();
364 let mut last_off = cur + attrs.pos;
365 let mut missing = None;
366 while let Some(attr) = attrs.next() {
367 let Ok(attr) = attr else { break };
368 match attr {
369 NlmsgerrAttrs::Msg(val) => {
370 if last_off == offset {
371 stack.push(("Msg", last_off));
372 break;
373 }
374 }
375 NlmsgerrAttrs::Offset(val) => {
376 if last_off == offset {
377 stack.push(("Offset", last_off));
378 break;
379 }
380 }
381 NlmsgerrAttrs::Cookie(val) => {
382 if last_off == offset {
383 stack.push(("Cookie", last_off));
384 break;
385 }
386 }
387 NlmsgerrAttrs::Policy(val) => {
388 (stack, missing) = val.lookup_attr(offset, missing_type);
389 if !stack.is_empty() {
390 break;
391 }
392 }
393 NlmsgerrAttrs::MissingType(val) => {
394 if last_off == offset {
395 stack.push(("MissingType", last_off));
396 break;
397 }
398 }
399 NlmsgerrAttrs::MissingNest(val) => {
400 if last_off == offset {
401 stack.push(("MissingNest", last_off));
402 break;
403 }
404 }
405 _ => {}
406 };
407 last_off = cur + attrs.pos;
408 }
409 if !stack.is_empty() {
410 stack.push(("NlmsgerrAttrs", cur));
411 }
412 (stack, missing)
413 }
414}
415#[derive(Clone)]
416pub enum PolicyTypeAttrs<'a> {
417 #[doc = "type of the attribute, enum netlink_attribute_type (U32)"]
418 Type(u32),
419 #[doc = "minimum value for signed integers (S64)"]
420 MinValueSigned(i64),
421 #[doc = "maximum value for signed integers (S64)"]
422 MaxValueSigned(i64),
423 #[doc = "minimum value for unsigned integers (U64)"]
424 MinValueU(u64),
425 #[doc = "maximum value for unsigned integers (U64)"]
426 MaxValueU(u64),
427 #[doc = "minimum length for binary attributes, no minimum if not given (U32)"]
428 MinLength(u32),
429 #[doc = "maximum length for binary attributes, no maximum if not given (U32)"]
430 MaxLength(u32),
431 #[doc = "sub policy for nested and nested array types (U32)"]
432 PolicyIdx(u32),
433 #[doc = "maximum sub policy attribute for nested and nested array types, this can in theory be < the size of the policy pointed to by the index, if limited inside the nesting (U32)"]
434 PolicyMaxtype(u32),
435 #[doc = "valid mask for the bitfield32 type (U32)"]
436 Bitfield32Mask(u32),
437 #[doc = "pad attribute for 64-bit alignment"]
438 Pad(&'a [u8]),
439 #[doc = "mask of valid bits for unsigned integers (U64)"]
440 Mask(u64),
441}
442impl<'a> IterablePolicyTypeAttrs<'a> {
443 #[doc = "type of the attribute, enum netlink_attribute_type (U32)"]
444 pub fn get_type(&self) -> Result<u32, ErrorContext> {
445 let mut iter = self.clone();
446 iter.pos = 0;
447 for attr in iter {
448 if let PolicyTypeAttrs::Type(val) = attr? {
449 return Ok(val);
450 }
451 }
452 Err(ErrorContext::new_missing(
453 "PolicyTypeAttrs",
454 "Type",
455 self.orig_loc,
456 self.buf.as_ptr() as usize,
457 ))
458 }
459 #[doc = "minimum value for signed integers (S64)"]
460 pub fn get_min_value_signed(&self) -> Result<i64, ErrorContext> {
461 let mut iter = self.clone();
462 iter.pos = 0;
463 for attr in iter {
464 if let PolicyTypeAttrs::MinValueSigned(val) = attr? {
465 return Ok(val);
466 }
467 }
468 Err(ErrorContext::new_missing(
469 "PolicyTypeAttrs",
470 "MinValueSigned",
471 self.orig_loc,
472 self.buf.as_ptr() as usize,
473 ))
474 }
475 #[doc = "maximum value for signed integers (S64)"]
476 pub fn get_max_value_signed(&self) -> Result<i64, ErrorContext> {
477 let mut iter = self.clone();
478 iter.pos = 0;
479 for attr in iter {
480 if let PolicyTypeAttrs::MaxValueSigned(val) = attr? {
481 return Ok(val);
482 }
483 }
484 Err(ErrorContext::new_missing(
485 "PolicyTypeAttrs",
486 "MaxValueSigned",
487 self.orig_loc,
488 self.buf.as_ptr() as usize,
489 ))
490 }
491 #[doc = "minimum value for unsigned integers (U64)"]
492 pub fn get_min_value_u(&self) -> Result<u64, ErrorContext> {
493 let mut iter = self.clone();
494 iter.pos = 0;
495 for attr in iter {
496 if let PolicyTypeAttrs::MinValueU(val) = attr? {
497 return Ok(val);
498 }
499 }
500 Err(ErrorContext::new_missing(
501 "PolicyTypeAttrs",
502 "MinValueU",
503 self.orig_loc,
504 self.buf.as_ptr() as usize,
505 ))
506 }
507 #[doc = "maximum value for unsigned integers (U64)"]
508 pub fn get_max_value_u(&self) -> Result<u64, ErrorContext> {
509 let mut iter = self.clone();
510 iter.pos = 0;
511 for attr in iter {
512 if let PolicyTypeAttrs::MaxValueU(val) = attr? {
513 return Ok(val);
514 }
515 }
516 Err(ErrorContext::new_missing(
517 "PolicyTypeAttrs",
518 "MaxValueU",
519 self.orig_loc,
520 self.buf.as_ptr() as usize,
521 ))
522 }
523 #[doc = "minimum length for binary attributes, no minimum if not given (U32)"]
524 pub fn get_min_length(&self) -> Result<u32, ErrorContext> {
525 let mut iter = self.clone();
526 iter.pos = 0;
527 for attr in iter {
528 if let PolicyTypeAttrs::MinLength(val) = attr? {
529 return Ok(val);
530 }
531 }
532 Err(ErrorContext::new_missing(
533 "PolicyTypeAttrs",
534 "MinLength",
535 self.orig_loc,
536 self.buf.as_ptr() as usize,
537 ))
538 }
539 #[doc = "maximum length for binary attributes, no maximum if not given (U32)"]
540 pub fn get_max_length(&self) -> Result<u32, ErrorContext> {
541 let mut iter = self.clone();
542 iter.pos = 0;
543 for attr in iter {
544 if let PolicyTypeAttrs::MaxLength(val) = attr? {
545 return Ok(val);
546 }
547 }
548 Err(ErrorContext::new_missing(
549 "PolicyTypeAttrs",
550 "MaxLength",
551 self.orig_loc,
552 self.buf.as_ptr() as usize,
553 ))
554 }
555 #[doc = "sub policy for nested and nested array types (U32)"]
556 pub fn get_policy_idx(&self) -> Result<u32, ErrorContext> {
557 let mut iter = self.clone();
558 iter.pos = 0;
559 for attr in iter {
560 if let PolicyTypeAttrs::PolicyIdx(val) = attr? {
561 return Ok(val);
562 }
563 }
564 Err(ErrorContext::new_missing(
565 "PolicyTypeAttrs",
566 "PolicyIdx",
567 self.orig_loc,
568 self.buf.as_ptr() as usize,
569 ))
570 }
571 #[doc = "maximum sub policy attribute for nested and nested array types, this can in theory be < the size of the policy pointed to by the index, if limited inside the nesting (U32)"]
572 pub fn get_policy_maxtype(&self) -> Result<u32, ErrorContext> {
573 let mut iter = self.clone();
574 iter.pos = 0;
575 for attr in iter {
576 if let PolicyTypeAttrs::PolicyMaxtype(val) = attr? {
577 return Ok(val);
578 }
579 }
580 Err(ErrorContext::new_missing(
581 "PolicyTypeAttrs",
582 "PolicyMaxtype",
583 self.orig_loc,
584 self.buf.as_ptr() as usize,
585 ))
586 }
587 #[doc = "valid mask for the bitfield32 type (U32)"]
588 pub fn get_bitfield32_mask(&self) -> Result<u32, ErrorContext> {
589 let mut iter = self.clone();
590 iter.pos = 0;
591 for attr in iter {
592 if let PolicyTypeAttrs::Bitfield32Mask(val) = attr? {
593 return Ok(val);
594 }
595 }
596 Err(ErrorContext::new_missing(
597 "PolicyTypeAttrs",
598 "Bitfield32Mask",
599 self.orig_loc,
600 self.buf.as_ptr() as usize,
601 ))
602 }
603 #[doc = "pad attribute for 64-bit alignment"]
604 pub fn get_pad(&self) -> Result<&'a [u8], ErrorContext> {
605 let mut iter = self.clone();
606 iter.pos = 0;
607 for attr in iter {
608 if let PolicyTypeAttrs::Pad(val) = attr? {
609 return Ok(val);
610 }
611 }
612 Err(ErrorContext::new_missing(
613 "PolicyTypeAttrs",
614 "Pad",
615 self.orig_loc,
616 self.buf.as_ptr() as usize,
617 ))
618 }
619 #[doc = "mask of valid bits for unsigned integers (U64)"]
620 pub fn get_mask(&self) -> Result<u64, ErrorContext> {
621 let mut iter = self.clone();
622 iter.pos = 0;
623 for attr in iter {
624 if let PolicyTypeAttrs::Mask(val) = attr? {
625 return Ok(val);
626 }
627 }
628 Err(ErrorContext::new_missing(
629 "PolicyTypeAttrs",
630 "Mask",
631 self.orig_loc,
632 self.buf.as_ptr() as usize,
633 ))
634 }
635}
636impl<'a> PolicyTypeAttrs<'a> {
637 pub fn new(buf: &'a [u8]) -> IterablePolicyTypeAttrs<'a> {
638 IterablePolicyTypeAttrs::with_loc(buf, buf.as_ptr() as usize)
639 }
640 fn attr_from_type(r#type: u16) -> Option<&'static str> {
641 let res = match r#type {
642 0u16 => "Unspec",
643 1u16 => "Type",
644 2u16 => "MinValueSigned",
645 3u16 => "MaxValueSigned",
646 4u16 => "MinValueU",
647 5u16 => "MaxValueU",
648 6u16 => "MinLength",
649 7u16 => "MaxLength",
650 8u16 => "PolicyIdx",
651 9u16 => "PolicyMaxtype",
652 10u16 => "Bitfield32Mask",
653 11u16 => "Pad",
654 12u16 => "Mask",
655 _ => return None,
656 };
657 Some(res)
658 }
659}
660#[derive(Clone, Copy, Default)]
661pub struct IterablePolicyTypeAttrs<'a> {
662 buf: &'a [u8],
663 pos: usize,
664 orig_loc: usize,
665}
666impl<'a> IterablePolicyTypeAttrs<'a> {
667 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
668 Self {
669 buf,
670 pos: 0,
671 orig_loc,
672 }
673 }
674 pub fn get_buf(&self) -> &'a [u8] {
675 self.buf
676 }
677}
678impl<'a> Iterator for IterablePolicyTypeAttrs<'a> {
679 type Item = Result<PolicyTypeAttrs<'a>, ErrorContext>;
680 fn next(&mut self) -> Option<Self::Item> {
681 if self.buf.len() == self.pos {
682 return None;
683 }
684 let pos = self.pos;
685 let mut r#type = None;
686 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
687 r#type = Some(header.r#type);
688 let res = match header.r#type {
689 1u16 => PolicyTypeAttrs::Type({
690 let res = parse_u32(next);
691 let Some(val) = res else { break };
692 val
693 }),
694 2u16 => PolicyTypeAttrs::MinValueSigned({
695 let res = parse_i64(next);
696 let Some(val) = res else { break };
697 val
698 }),
699 3u16 => PolicyTypeAttrs::MaxValueSigned({
700 let res = parse_i64(next);
701 let Some(val) = res else { break };
702 val
703 }),
704 4u16 => PolicyTypeAttrs::MinValueU({
705 let res = parse_u64(next);
706 let Some(val) = res else { break };
707 val
708 }),
709 5u16 => PolicyTypeAttrs::MaxValueU({
710 let res = parse_u64(next);
711 let Some(val) = res else { break };
712 val
713 }),
714 6u16 => PolicyTypeAttrs::MinLength({
715 let res = parse_u32(next);
716 let Some(val) = res else { break };
717 val
718 }),
719 7u16 => PolicyTypeAttrs::MaxLength({
720 let res = parse_u32(next);
721 let Some(val) = res else { break };
722 val
723 }),
724 8u16 => PolicyTypeAttrs::PolicyIdx({
725 let res = parse_u32(next);
726 let Some(val) = res else { break };
727 val
728 }),
729 9u16 => PolicyTypeAttrs::PolicyMaxtype({
730 let res = parse_u32(next);
731 let Some(val) = res else { break };
732 val
733 }),
734 10u16 => PolicyTypeAttrs::Bitfield32Mask({
735 let res = parse_u32(next);
736 let Some(val) = res else { break };
737 val
738 }),
739 11u16 => PolicyTypeAttrs::Pad({
740 let res = Some(next);
741 let Some(val) = res else { break };
742 val
743 }),
744 12u16 => PolicyTypeAttrs::Mask({
745 let res = parse_u64(next);
746 let Some(val) = res else { break };
747 val
748 }),
749 n => {
750 if cfg!(any(test, feature = "deny-unknown-attrs")) {
751 break;
752 } else {
753 continue;
754 }
755 }
756 };
757 return Some(Ok(res));
758 }
759 Some(Err(ErrorContext::new(
760 "PolicyTypeAttrs",
761 r#type.and_then(|t| PolicyTypeAttrs::attr_from_type(t)),
762 self.orig_loc,
763 self.buf.as_ptr().wrapping_add(pos) as usize,
764 )))
765 }
766}
767impl<'a> std::fmt::Debug for IterablePolicyTypeAttrs<'_> {
768 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
769 let mut fmt = f.debug_struct("PolicyTypeAttrs");
770 for attr in self.clone() {
771 let attr = match attr {
772 Ok(a) => a,
773 Err(err) => {
774 fmt.finish()?;
775 f.write_str("Err(")?;
776 err.fmt(f)?;
777 return f.write_str(")");
778 }
779 };
780 match attr {
781 PolicyTypeAttrs::Type(val) => fmt.field("Type", &val),
782 PolicyTypeAttrs::MinValueSigned(val) => fmt.field("MinValueSigned", &val),
783 PolicyTypeAttrs::MaxValueSigned(val) => fmt.field("MaxValueSigned", &val),
784 PolicyTypeAttrs::MinValueU(val) => fmt.field("MinValueU", &val),
785 PolicyTypeAttrs::MaxValueU(val) => fmt.field("MaxValueU", &val),
786 PolicyTypeAttrs::MinLength(val) => fmt.field("MinLength", &val),
787 PolicyTypeAttrs::MaxLength(val) => fmt.field("MaxLength", &val),
788 PolicyTypeAttrs::PolicyIdx(val) => fmt.field("PolicyIdx", &val),
789 PolicyTypeAttrs::PolicyMaxtype(val) => fmt.field("PolicyMaxtype", &val),
790 PolicyTypeAttrs::Bitfield32Mask(val) => fmt.field("Bitfield32Mask", &val),
791 PolicyTypeAttrs::Pad(val) => fmt.field("Pad", &val),
792 PolicyTypeAttrs::Mask(val) => fmt.field("Mask", &val),
793 };
794 }
795 fmt.finish()
796 }
797}
798impl IterablePolicyTypeAttrs<'_> {
799 pub fn lookup_attr(
800 &self,
801 offset: usize,
802 missing_type: Option<u16>,
803 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
804 let mut stack = Vec::new();
805 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
806 if cur == offset {
807 stack.push(("PolicyTypeAttrs", offset));
808 return (
809 stack,
810 missing_type.and_then(|t| PolicyTypeAttrs::attr_from_type(t)),
811 );
812 }
813 if cur > offset || cur + self.buf.len() < offset {
814 return (stack, None);
815 }
816 let mut attrs = self.clone();
817 let mut last_off = cur + attrs.pos;
818 while let Some(attr) = attrs.next() {
819 let Ok(attr) = attr else { break };
820 match attr {
821 PolicyTypeAttrs::Type(val) => {
822 if last_off == offset {
823 stack.push(("Type", last_off));
824 break;
825 }
826 }
827 PolicyTypeAttrs::MinValueSigned(val) => {
828 if last_off == offset {
829 stack.push(("MinValueSigned", last_off));
830 break;
831 }
832 }
833 PolicyTypeAttrs::MaxValueSigned(val) => {
834 if last_off == offset {
835 stack.push(("MaxValueSigned", last_off));
836 break;
837 }
838 }
839 PolicyTypeAttrs::MinValueU(val) => {
840 if last_off == offset {
841 stack.push(("MinValueU", last_off));
842 break;
843 }
844 }
845 PolicyTypeAttrs::MaxValueU(val) => {
846 if last_off == offset {
847 stack.push(("MaxValueU", last_off));
848 break;
849 }
850 }
851 PolicyTypeAttrs::MinLength(val) => {
852 if last_off == offset {
853 stack.push(("MinLength", last_off));
854 break;
855 }
856 }
857 PolicyTypeAttrs::MaxLength(val) => {
858 if last_off == offset {
859 stack.push(("MaxLength", last_off));
860 break;
861 }
862 }
863 PolicyTypeAttrs::PolicyIdx(val) => {
864 if last_off == offset {
865 stack.push(("PolicyIdx", last_off));
866 break;
867 }
868 }
869 PolicyTypeAttrs::PolicyMaxtype(val) => {
870 if last_off == offset {
871 stack.push(("PolicyMaxtype", last_off));
872 break;
873 }
874 }
875 PolicyTypeAttrs::Bitfield32Mask(val) => {
876 if last_off == offset {
877 stack.push(("Bitfield32Mask", last_off));
878 break;
879 }
880 }
881 PolicyTypeAttrs::Pad(val) => {
882 if last_off == offset {
883 stack.push(("Pad", last_off));
884 break;
885 }
886 }
887 PolicyTypeAttrs::Mask(val) => {
888 if last_off == offset {
889 stack.push(("Mask", last_off));
890 break;
891 }
892 }
893 _ => {}
894 };
895 last_off = cur + attrs.pos;
896 }
897 if !stack.is_empty() {
898 stack.push(("PolicyTypeAttrs", cur));
899 }
900 (stack, None)
901 }
902}
903pub struct PushDummy<Prev: Rec> {
904 pub(crate) prev: Option<Prev>,
905 pub(crate) header_offset: Option<usize>,
906}
907impl<Prev: Rec> Rec for PushDummy<Prev> {
908 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
909 self.prev.as_mut().unwrap().as_rec_mut()
910 }
911}
912impl<Prev: Rec> PushDummy<Prev> {
913 pub fn new(prev: Prev) -> Self {
914 Self {
915 prev: Some(prev),
916 header_offset: None,
917 }
918 }
919 pub fn end_nested(mut self) -> Prev {
920 let mut prev = self.prev.take().unwrap();
921 if let Some(header_offset) = &self.header_offset {
922 finalize_nested_header(prev.as_rec_mut(), *header_offset);
923 }
924 prev
925 }
926}
927impl<Prev: Rec> Drop for PushDummy<Prev> {
928 fn drop(&mut self) {
929 if let Some(prev) = &mut self.prev {
930 if let Some(header_offset) = &self.header_offset {
931 finalize_nested_header(prev.as_rec_mut(), *header_offset);
932 }
933 }
934 }
935}
936pub struct PushNlmsgerrAttrs<Prev: Rec> {
937 pub(crate) prev: Option<Prev>,
938 pub(crate) header_offset: Option<usize>,
939}
940impl<Prev: Rec> Rec for PushNlmsgerrAttrs<Prev> {
941 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
942 self.prev.as_mut().unwrap().as_rec_mut()
943 }
944}
945impl<Prev: Rec> PushNlmsgerrAttrs<Prev> {
946 pub fn new(prev: Prev) -> Self {
947 Self {
948 prev: Some(prev),
949 header_offset: None,
950 }
951 }
952 pub fn end_nested(mut self) -> Prev {
953 let mut prev = self.prev.take().unwrap();
954 if let Some(header_offset) = &self.header_offset {
955 finalize_nested_header(prev.as_rec_mut(), *header_offset);
956 }
957 prev
958 }
959 #[doc = "error message string (string)"]
960 pub fn push_msg(mut self, value: &CStr) -> Self {
961 push_header(
962 self.as_rec_mut(),
963 1u16,
964 value.to_bytes_with_nul().len() as u16,
965 );
966 self.as_rec_mut().extend(value.to_bytes_with_nul());
967 self
968 }
969 #[doc = "error message string (string)"]
970 pub fn push_msg_bytes(mut self, value: &[u8]) -> Self {
971 push_header(self.as_rec_mut(), 1u16, (value.len() + 1) as u16);
972 self.as_rec_mut().extend(value);
973 self.as_rec_mut().push(0);
974 self
975 }
976 #[doc = "offset of the invalid attribute in the original message, counting from the beginning of the header (u32)"]
977 pub fn push_offset(mut self, value: u32) -> Self {
978 push_header(self.as_rec_mut(), 2u16, 4 as u16);
979 self.as_rec_mut().extend(value.to_ne_bytes());
980 self
981 }
982 #[doc = "arbitrary subsystem specific cookie to be used - in the success case - to identify a created object or operation or similar (binary)"]
983 pub fn push_cookie(mut self, value: &[u8]) -> Self {
984 push_header(self.as_rec_mut(), 3u16, value.len() as u16);
985 self.as_rec_mut().extend(value);
986 self
987 }
988 #[doc = "policy for a rejected attribute"]
989 pub fn nested_policy(mut self) -> PushPolicyTypeAttrs<Self> {
990 let header_offset = push_nested_header(self.as_rec_mut(), 4u16);
991 PushPolicyTypeAttrs {
992 prev: Some(self),
993 header_offset: Some(header_offset),
994 }
995 }
996 #[doc = "type of a missing required attribute, NLMSGERR_ATTR_MISS_NEST will not be present if the attribute was missing at the message level"]
997 pub fn push_missing_type(mut self, value: u16) -> Self {
998 push_header(self.as_rec_mut(), 5u16, 2 as u16);
999 self.as_rec_mut().extend(value.to_ne_bytes());
1000 self
1001 }
1002 #[doc = "offset of the nest where attribute was missing"]
1003 pub fn push_missing_nest(mut self, value: u32) -> Self {
1004 push_header(self.as_rec_mut(), 6u16, 4 as u16);
1005 self.as_rec_mut().extend(value.to_ne_bytes());
1006 self
1007 }
1008}
1009impl<Prev: Rec> Drop for PushNlmsgerrAttrs<Prev> {
1010 fn drop(&mut self) {
1011 if let Some(prev) = &mut self.prev {
1012 if let Some(header_offset) = &self.header_offset {
1013 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1014 }
1015 }
1016 }
1017}
1018pub struct PushPolicyTypeAttrs<Prev: Rec> {
1019 pub(crate) prev: Option<Prev>,
1020 pub(crate) header_offset: Option<usize>,
1021}
1022impl<Prev: Rec> Rec for PushPolicyTypeAttrs<Prev> {
1023 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1024 self.prev.as_mut().unwrap().as_rec_mut()
1025 }
1026}
1027impl<Prev: Rec> PushPolicyTypeAttrs<Prev> {
1028 pub fn new(prev: Prev) -> Self {
1029 Self {
1030 prev: Some(prev),
1031 header_offset: None,
1032 }
1033 }
1034 pub fn end_nested(mut self) -> Prev {
1035 let mut prev = self.prev.take().unwrap();
1036 if let Some(header_offset) = &self.header_offset {
1037 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1038 }
1039 prev
1040 }
1041 #[doc = "type of the attribute, enum netlink_attribute_type (U32)"]
1042 pub fn push_type(mut self, value: u32) -> Self {
1043 push_header(self.as_rec_mut(), 1u16, 4 as u16);
1044 self.as_rec_mut().extend(value.to_ne_bytes());
1045 self
1046 }
1047 #[doc = "minimum value for signed integers (S64)"]
1048 pub fn push_min_value_signed(mut self, value: i64) -> Self {
1049 push_header(self.as_rec_mut(), 2u16, 8 as u16);
1050 self.as_rec_mut().extend(value.to_ne_bytes());
1051 self
1052 }
1053 #[doc = "maximum value for signed integers (S64)"]
1054 pub fn push_max_value_signed(mut self, value: i64) -> Self {
1055 push_header(self.as_rec_mut(), 3u16, 8 as u16);
1056 self.as_rec_mut().extend(value.to_ne_bytes());
1057 self
1058 }
1059 #[doc = "minimum value for unsigned integers (U64)"]
1060 pub fn push_min_value_u(mut self, value: u64) -> Self {
1061 push_header(self.as_rec_mut(), 4u16, 8 as u16);
1062 self.as_rec_mut().extend(value.to_ne_bytes());
1063 self
1064 }
1065 #[doc = "maximum value for unsigned integers (U64)"]
1066 pub fn push_max_value_u(mut self, value: u64) -> Self {
1067 push_header(self.as_rec_mut(), 5u16, 8 as u16);
1068 self.as_rec_mut().extend(value.to_ne_bytes());
1069 self
1070 }
1071 #[doc = "minimum length for binary attributes, no minimum if not given (U32)"]
1072 pub fn push_min_length(mut self, value: u32) -> Self {
1073 push_header(self.as_rec_mut(), 6u16, 4 as u16);
1074 self.as_rec_mut().extend(value.to_ne_bytes());
1075 self
1076 }
1077 #[doc = "maximum length for binary attributes, no maximum if not given (U32)"]
1078 pub fn push_max_length(mut self, value: u32) -> Self {
1079 push_header(self.as_rec_mut(), 7u16, 4 as u16);
1080 self.as_rec_mut().extend(value.to_ne_bytes());
1081 self
1082 }
1083 #[doc = "sub policy for nested and nested array types (U32)"]
1084 pub fn push_policy_idx(mut self, value: u32) -> Self {
1085 push_header(self.as_rec_mut(), 8u16, 4 as u16);
1086 self.as_rec_mut().extend(value.to_ne_bytes());
1087 self
1088 }
1089 #[doc = "maximum sub policy attribute for nested and nested array types, this can in theory be < the size of the policy pointed to by the index, if limited inside the nesting (U32)"]
1090 pub fn push_policy_maxtype(mut self, value: u32) -> Self {
1091 push_header(self.as_rec_mut(), 9u16, 4 as u16);
1092 self.as_rec_mut().extend(value.to_ne_bytes());
1093 self
1094 }
1095 #[doc = "valid mask for the bitfield32 type (U32)"]
1096 pub fn push_bitfield32_mask(mut self, value: u32) -> Self {
1097 push_header(self.as_rec_mut(), 10u16, 4 as u16);
1098 self.as_rec_mut().extend(value.to_ne_bytes());
1099 self
1100 }
1101 #[doc = "pad attribute for 64-bit alignment"]
1102 pub fn push_pad(mut self, value: &[u8]) -> Self {
1103 push_header(self.as_rec_mut(), 11u16, value.len() as u16);
1104 self.as_rec_mut().extend(value);
1105 self
1106 }
1107 #[doc = "mask of valid bits for unsigned integers (U64)"]
1108 pub fn push_mask(mut self, value: u64) -> Self {
1109 push_header(self.as_rec_mut(), 12u16, 8 as u16);
1110 self.as_rec_mut().extend(value.to_ne_bytes());
1111 self
1112 }
1113}
1114impl<Prev: Rec> Drop for PushPolicyTypeAttrs<Prev> {
1115 fn drop(&mut self) {
1116 if let Some(prev) = &mut self.prev {
1117 if let Some(header_offset) = &self.header_offset {
1118 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1119 }
1120 }
1121 }
1122}
1123#[derive(Clone)]
1124pub struct PushBuiltinNfgenmsg {
1125 pub(crate) buf: [u8; 4usize],
1126}
1127#[doc = "Create zero-initialized struct"]
1128impl Default for PushBuiltinNfgenmsg {
1129 fn default() -> Self {
1130 Self { buf: [0u8; 4usize] }
1131 }
1132}
1133impl PushBuiltinNfgenmsg {
1134 #[doc = "Create zero-initialized struct"]
1135 pub fn new() -> Self {
1136 Default::default()
1137 }
1138 #[doc = "Copy from contents from other slice"]
1139 pub fn new_from_slice(other: &[u8]) -> Option<Self> {
1140 if other.len() != Self::len() {
1141 return None;
1142 }
1143 let mut buf = [0u8; Self::len()];
1144 buf.clone_from_slice(other);
1145 Some(Self { buf })
1146 }
1147 pub fn as_slice(&self) -> &[u8] {
1148 &self.buf
1149 }
1150 pub fn as_mut_slice(&mut self) -> &mut [u8] {
1151 &mut self.buf
1152 }
1153 pub const fn len() -> usize {
1154 4usize
1155 }
1156 pub fn cmd(&self) -> u8 {
1157 parse_u8(&self.buf[0usize..1usize]).unwrap()
1158 }
1159 pub fn set_cmd(&mut self, value: u8) {
1160 self.buf[0usize..1usize].copy_from_slice(&value.to_ne_bytes())
1161 }
1162 pub fn version(&self) -> u8 {
1163 parse_u8(&self.buf[1usize..2usize]).unwrap()
1164 }
1165 pub fn set_version(&mut self, value: u8) {
1166 self.buf[1usize..2usize].copy_from_slice(&value.to_ne_bytes())
1167 }
1168 pub fn reserved(&self) -> u16 {
1169 parse_u16(&self.buf[2usize..4usize]).unwrap()
1170 }
1171 pub fn set_reserved(&mut self, value: u16) {
1172 self.buf[2usize..4usize].copy_from_slice(&value.to_ne_bytes())
1173 }
1174}
1175impl std::fmt::Debug for PushBuiltinNfgenmsg {
1176 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1177 fmt.debug_struct("BuiltinNfgenmsg")
1178 .field("cmd", &self.cmd())
1179 .field("version", &self.version())
1180 .field("reserved", &self.reserved())
1181 .finish()
1182 }
1183}
1184#[derive(Clone)]
1185pub struct PushBuiltinBitfield32 {
1186 pub(crate) buf: [u8; 8usize],
1187}
1188#[doc = "Create zero-initialized struct"]
1189impl Default for PushBuiltinBitfield32 {
1190 fn default() -> Self {
1191 Self { buf: [0u8; 8usize] }
1192 }
1193}
1194impl PushBuiltinBitfield32 {
1195 #[doc = "Create zero-initialized struct"]
1196 pub fn new() -> Self {
1197 Default::default()
1198 }
1199 #[doc = "Copy from contents from other slice"]
1200 pub fn new_from_slice(other: &[u8]) -> Option<Self> {
1201 if other.len() != Self::len() {
1202 return None;
1203 }
1204 let mut buf = [0u8; Self::len()];
1205 buf.clone_from_slice(other);
1206 Some(Self { buf })
1207 }
1208 pub fn as_slice(&self) -> &[u8] {
1209 &self.buf
1210 }
1211 pub fn as_mut_slice(&mut self) -> &mut [u8] {
1212 &mut self.buf
1213 }
1214 pub const fn len() -> usize {
1215 8usize
1216 }
1217 pub fn value(&self) -> u32 {
1218 parse_u32(&self.buf[0usize..4usize]).unwrap()
1219 }
1220 pub fn set_value(&mut self, value: u32) {
1221 self.buf[0usize..4usize].copy_from_slice(&value.to_ne_bytes())
1222 }
1223 pub fn selector(&self) -> u32 {
1224 parse_u32(&self.buf[4usize..8usize]).unwrap()
1225 }
1226 pub fn set_selector(&mut self, value: u32) {
1227 self.buf[4usize..8usize].copy_from_slice(&value.to_ne_bytes())
1228 }
1229}
1230impl std::fmt::Debug for PushBuiltinBitfield32 {
1231 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1232 fmt.debug_struct("BuiltinBitfield32")
1233 .field("value", &self.value())
1234 .field("selector", &self.selector())
1235 .finish()
1236 }
1237}
1238#[derive(Clone)]
1239pub struct PushNlmsghdr {
1240 pub(crate) buf: [u8; 16usize],
1241}
1242#[doc = "Create zero-initialized struct"]
1243impl Default for PushNlmsghdr {
1244 fn default() -> Self {
1245 Self {
1246 buf: [0u8; 16usize],
1247 }
1248 }
1249}
1250impl PushNlmsghdr {
1251 #[doc = "Create zero-initialized struct"]
1252 pub fn new() -> Self {
1253 Default::default()
1254 }
1255 #[doc = "Copy from contents from other slice"]
1256 pub fn new_from_slice(other: &[u8]) -> Option<Self> {
1257 if other.len() != Self::len() {
1258 return None;
1259 }
1260 let mut buf = [0u8; Self::len()];
1261 buf.clone_from_slice(other);
1262 Some(Self { buf })
1263 }
1264 pub fn as_slice(&self) -> &[u8] {
1265 &self.buf
1266 }
1267 pub fn as_mut_slice(&mut self) -> &mut [u8] {
1268 &mut self.buf
1269 }
1270 pub const fn len() -> usize {
1271 16usize
1272 }
1273 pub fn get_len(&self) -> u32 {
1274 parse_u32(&self.buf[0usize..4usize]).unwrap()
1275 }
1276 pub fn set_len(&mut self, value: u32) {
1277 self.buf[0usize..4usize].copy_from_slice(&value.to_ne_bytes())
1278 }
1279 pub fn get_type(&self) -> u16 {
1280 parse_u16(&self.buf[4usize..6usize]).unwrap()
1281 }
1282 pub fn set_type(&mut self, value: u16) {
1283 self.buf[4usize..6usize].copy_from_slice(&value.to_ne_bytes())
1284 }
1285 pub fn flags(&self) -> u16 {
1286 parse_u16(&self.buf[6usize..8usize]).unwrap()
1287 }
1288 pub fn set_flags(&mut self, value: u16) {
1289 self.buf[6usize..8usize].copy_from_slice(&value.to_ne_bytes())
1290 }
1291 pub fn seq(&self) -> u32 {
1292 parse_u32(&self.buf[8usize..12usize]).unwrap()
1293 }
1294 pub fn set_seq(&mut self, value: u32) {
1295 self.buf[8usize..12usize].copy_from_slice(&value.to_ne_bytes())
1296 }
1297 pub fn pid(&self) -> u32 {
1298 parse_u32(&self.buf[12usize..16usize]).unwrap()
1299 }
1300 pub fn set_pid(&mut self, value: u32) {
1301 self.buf[12usize..16usize].copy_from_slice(&value.to_ne_bytes())
1302 }
1303}
1304impl std::fmt::Debug for PushNlmsghdr {
1305 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1306 fmt.debug_struct("Nlmsghdr")
1307 .field("len", &self.get_len())
1308 .field("type", &self.get_type())
1309 .field("flags", &self.flags())
1310 .field("seq", &self.seq())
1311 .field("pid", &self.pid())
1312 .finish()
1313 }
1314}