netstack3_ip/
ipv6.rs

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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! IPv6 specific functionality.

use netstack3_base::{AnyDevice, DeviceIdContext, FrameDestination};
use packet_formats::ipv6::ext_hdrs::{
    DestinationOptionData, ExtensionHeaderOption, FragmentData, HopByHopOptionData,
    Ipv6ExtensionHeaderData,
};
use packet_formats::ipv6::Ipv6Packet;
use zerocopy::SplitByteSlice;

/// What to do with an IPv6 packet after parsing an extension header.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Ipv6PacketAction {
    /// Discard the packet.
    _Discard,

    /// Continue processing the next extension header (if any are
    /// available and the processing node is the destination node)
    /// or continue processing the packet (if the extension headers
    /// have been exhausted or the processing node is not the
    /// destination node).
    Continue,

    /// Stop processing extension headers and consider the
    /// packet fragmented. The node must attempt to handle
    /// the fragmented packet (attempt reassembly).
    ProcessFragment,
}

/// Handle IPv6 extension headers.
///
/// What this function does depends on whether or not the `at_destination` flag
/// is set. If it is `true`, then we will attempt to process all the extension
/// headers in `packet`. Otherwise, we will only attempt to process the
/// hop-by-hop extension header (which MUST be the first extension header if
/// present) as per RFC 8200 section 4.
pub(crate) fn handle_extension_headers<CC: DeviceIdContext<AnyDevice>, B: SplitByteSlice>(
    core_ctx: &mut CC,
    device: &CC::DeviceId,
    frame_dst: Option<FrameDestination>,
    packet: &Ipv6Packet<B>,
    at_destination: bool,
) -> Ipv6PacketAction {
    // The next action we need to do after processing an extension header.
    //
    // Initialized to `Ipv6PacketAction::Continue` so we start off processing
    // extension headers.
    let mut action = Ipv6PacketAction::Continue;
    let mut iter = packet.iter_extension_hdrs();

    if at_destination {
        // Keep looping while we are okay to just continue parsing extension
        // headers.
        while action == Ipv6PacketAction::Continue {
            let ext_hdr = match iter.next() {
                None => break,
                Some(x) => x,
            };

            match ext_hdr.data() {
                Ipv6ExtensionHeaderData::HopByHopOptions { options } => {
                    action = handle_hop_by_hop_options_ext_hdr(
                        core_ctx,
                        device,
                        frame_dst,
                        packet,
                        options.iter(),
                    );
                }
                Ipv6ExtensionHeaderData::Fragment { fragment_data } => {
                    action =
                        handle_fragment_ext_hdr(core_ctx, device, frame_dst, packet, fragment_data);
                }
                Ipv6ExtensionHeaderData::DestinationOptions { options } => {
                    action = handle_destination_options_ext_hdr(
                        core_ctx,
                        device,
                        frame_dst,
                        packet,
                        options.iter(),
                    );
                }
            }
        }
    } else {
        // Packet is not yet at the destination, so only process the hop-by-hop
        // options extension header (which MUST be the first extension header if
        // it is present) as per RFC 8200 section 4.
        if let Some(ext_hdr) = iter.next() {
            if let Ipv6ExtensionHeaderData::HopByHopOptions { options } = ext_hdr.data() {
                action = handle_hop_by_hop_options_ext_hdr(
                    core_ctx,
                    device,
                    frame_dst,
                    packet,
                    options.iter(),
                );
            }
        }
    }

    action
}

/// Handles a Hop By Hop extension header for a `packet`.
// For now, we do not support any options. If parsing succeeds we are guaranteed
// that the options present are safely skippable. If they aren't safely
// skippable, we must have resulted in a parsing error when parsing the packet,
// and so this function will never be called.
fn handle_hop_by_hop_options_ext_hdr<
    'a,
    CC: DeviceIdContext<AnyDevice>,
    B: SplitByteSlice,
    I: Iterator<Item = ExtensionHeaderOption<HopByHopOptionData<'a>>>,
>(
    _bindings_ctx: &mut CC,
    _device: &CC::DeviceId,
    _frame_dst: Option<FrameDestination>,
    _packet: &Ipv6Packet<B>,
    options: I,
) -> Ipv6PacketAction {
    for option in options {
        match option.data {
            // Safely skip and continue, as we know that if we parsed an
            // unrecognized option, the option's action was set to skip and
            // continue.
            HopByHopOptionData::Unrecognized { .. } => {}
            // Also skip RouterAlert because router part of MLD is not
            // implemented.
            HopByHopOptionData::RouterAlert { .. } => {}
        }
    }

    Ipv6PacketAction::Continue
}

/// Handles a fragment extension header for a `packet`.
fn handle_fragment_ext_hdr<'a, CC: DeviceIdContext<AnyDevice>, B: SplitByteSlice>(
    _bindings_ctx: &mut CC,
    _device: &CC::DeviceId,
    _frame_dst: Option<FrameDestination>,
    _packet: &Ipv6Packet<B>,
    _fragment_data: &FragmentData<'a>,
) -> Ipv6PacketAction {
    Ipv6PacketAction::ProcessFragment
}

/// Handles a destination extension header for a `packet`.
// For now, we do not support any options. If parsing succeeds we are guaranteed
// that the options present are safely skippable. If they aren't safely
// skippable, we must have resulted in a parsing error when parsing the packet,
// and so this function will never be called.
fn handle_destination_options_ext_hdr<
    'a,
    CC: DeviceIdContext<AnyDevice>,
    B: SplitByteSlice,
    I: Iterator<Item = ExtensionHeaderOption<DestinationOptionData<'a>>>,
>(
    _bindings_ctx: &mut CC,
    _device: &CC::DeviceId,
    _frame_dst: Option<FrameDestination>,
    _packet: &Ipv6Packet<B>,
    options: I,
) -> Ipv6PacketAction {
    for option in options {
        match option.data {
            // Safely skip and continue, as we know that if we parsed an
            // unrecognized option, the option's action was set to skip and
            // continue.
            DestinationOptionData::Unrecognized { .. } => {}
        }
    }

    Ipv6PacketAction::Continue
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use netstack3_base::testutil::{FakeDeviceId, TEST_ADDRS_V6};
    use packet::serialize::{Buf, Serializer};
    use packet::ParseBuffer;
    use packet_formats::ip::IpProto;
    use packet_formats::ipv6::Ipv6PacketBuilder;

    use super::*;

    type FakeCoreCtx = netstack3_base::testutil::FakeCoreCtx<(), (), FakeDeviceId>;

    #[test]
    fn test_no_extension_headers() {
        // Test that if we have no extension headers, we continue.
        let mut core_ctx = FakeCoreCtx::default();
        let builder = Ipv6PacketBuilder::new(
            TEST_ADDRS_V6.remote_ip,
            TEST_ADDRS_V6.local_ip,
            10,
            IpProto::Tcp.into(),
        );
        let frame_dst = FrameDestination::Individual { local: true };
        let mut buffer =
            Buf::new(vec![1, 2, 3, 4, 5], ..).encapsulate(builder).serialize_vec_outer().unwrap();
        let packet = buffer.parse::<Ipv6Packet<_>>().unwrap();

        assert_eq!(
            handle_extension_headers(&mut core_ctx, &FakeDeviceId, Some(frame_dst), &packet, false),
            Ipv6PacketAction::Continue
        );
    }
}