2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * ip/ip4_forward.c: IP v4 forwarding
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/ip-neighbor/ip4_neighbor.h>
41 #include <vnet/ethernet/ethernet.h>
44 ip4_neighbor_probe_dst (const ip_adjacency_t * adj, const ip4_address_t * dst)
46 ip_interface_address_t *ia;
49 src = ip4_interface_address_matching_destination
51 &adj->sub_type.nbr.next_hop.ip4, adj->rewrite_header.sw_if_index, &ia);
55 ip4_neighbor_probe (vlib_get_main (), vnet_get_main (), adj, src, dst);
59 ip4_neighbor_advertise (vlib_main_t * vm,
61 u32 sw_if_index, const ip4_address_t * addr)
63 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
64 ip4_main_t *i4m = &ip4_main;
65 u8 *rewrite, rewrite_len;
69 ip4_main_t *i4m = &ip4_main;
70 addr = ip4_interface_first_address (i4m, sw_if_index, 0);
75 clib_warning ("Sending GARP for IP4 address %U on sw_if_idex %d",
76 format_ip4_address, addr, sw_if_index);
78 /* Form GARP packet for output - Gratuitous ARP is an ARP request packet
79 where the interface IP/MAC pair is used for both source and request
80 MAC/IP pairs in the request */
82 ethernet_arp_header_t *h = vlib_packet_template_get_packet
83 (vm, &i4m->ip4_arp_request_packet_template, &bi);
88 mac_address_from_bytes (&h->ip4_over_ethernet[0].mac, hi->hw_address);
89 mac_address_from_bytes (&h->ip4_over_ethernet[1].mac, hi->hw_address);
90 h->ip4_over_ethernet[0].ip4 = addr[0];
91 h->ip4_over_ethernet[1].ip4 = addr[0];
93 /* Setup MAC header with ARP Etype and broadcast DMAC */
94 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
96 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_ARP,
97 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST);
98 rewrite_len = vec_len (rewrite);
99 vlib_buffer_advance (b, -rewrite_len);
100 ethernet_header_t *e = vlib_buffer_get_current (b);
101 clib_memcpy_fast (e->dst_address, rewrite, rewrite_len);
104 /* Send GARP packet out the specified interface */
105 vnet_buffer (b)->sw_if_index[VLIB_RX] =
106 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
107 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
108 u32 *to_next = vlib_frame_vector_args (f);
111 vlib_put_frame_to_node (vm, hi->output_node_index, f);
116 ip4_arp_inline (vlib_main_t * vm,
117 vlib_node_runtime_t * node,
118 vlib_frame_t * frame, int is_glean)
120 vnet_main_t *vnm = vnet_get_main ();
121 ip4_main_t *im = &ip4_main;
122 ip_lookup_main_t *lm = &im->lookup_main;
123 u32 *from, *to_next_drop;
124 uword n_left_from, n_left_to_next_drop, next_index;
125 u32 thread_index = vm->thread_index;
128 if (node->flags & VLIB_NODE_FLAG_TRACE)
129 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
131 seed = throttle_seed (&im->arp_throttle, thread_index, vlib_time_now (vm));
133 from = vlib_frame_vector_args (frame);
134 n_left_from = frame->n_vectors;
135 next_index = node->cached_next_index;
136 if (next_index == IP4_ARP_NEXT_DROP)
137 next_index = IP4_ARP_N_NEXT; /* point to first interface */
139 while (n_left_from > 0)
141 vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
142 to_next_drop, n_left_to_next_drop);
144 while (n_left_from > 0 && n_left_to_next_drop > 0)
146 u32 pi0, adj_index0, sw_if_index0;
147 ip4_address_t resolve0, src0;
148 vlib_buffer_t *p0, *b0;
149 ip_adjacency_t *adj0;
153 p0 = vlib_get_buffer (vm, pi0);
157 to_next_drop[0] = pi0;
159 n_left_to_next_drop -= 1;
161 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
162 adj0 = adj_get (adj_index0);
163 sw_if_index0 = adj0->rewrite_header.sw_if_index;
167 /* resolve the packet's destination */
168 ip4_header_t *ip0 = vlib_buffer_get_current (p0);
169 resolve0 = ip0->dst_address;
170 src0 = adj0->sub_type.glean.receive_addr.ip4;
174 /* resolve the incomplete adj */
175 resolve0 = adj0->sub_type.nbr.next_hop.ip4;
176 /* Src IP address in ARP header. */
177 if (ip4_src_address_for_packet (lm, sw_if_index0, &src0))
179 /* No source address available */
180 p0->error = node->errors[IP4_ARP_ERROR_NO_SOURCE_ADDRESS];
185 /* combine the address and interface for the hash key */
186 r0 = (u64) resolve0.data_u32 << 32;
189 if (throttle_check (&im->arp_throttle, thread_index, r0, seed))
191 p0->error = node->errors[IP4_ARP_ERROR_THROTTLED];
196 * the adj has been updated to a rewrite but the node the DPO that got
197 * us here hasn't - yet. no big deal. we'll drop while we wait.
199 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
201 p0->error = node->errors[IP4_ARP_ERROR_RESOLVED];
206 * Can happen if the control-plane is programming tables
207 * with traffic flowing; at least that's today's lame excuse.
209 if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN)
210 || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP))
212 p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
216 /* Send ARP request. */
217 b0 = ip4_neighbor_probe (vm, vnm, adj0, &src0, &resolve0);
219 if (PREDICT_TRUE (NULL != b0))
221 /* copy the persistent fields from the original */
222 clib_memcpy_fast (b0->opaque2, p0->opaque2,
223 sizeof (p0->opaque2));
224 p0->error = node->errors[IP4_ARP_ERROR_REQUEST_SENT];
228 p0->error = node->errors[IP4_ARP_ERROR_NO_BUFFERS];
233 vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
236 return frame->n_vectors;
239 VLIB_NODE_FN (ip4_arp_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
240 vlib_frame_t * frame)
242 return (ip4_arp_inline (vm, node, frame, 0));
245 VLIB_NODE_FN (ip4_glean_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
246 vlib_frame_t * frame)
248 return (ip4_arp_inline (vm, node, frame, 1));
251 static char *ip4_arp_error_strings[] = {
252 [IP4_ARP_ERROR_THROTTLED] = "ARP requests throttled",
253 [IP4_ARP_ERROR_RESOLVED] = "ARP requests resolved",
254 [IP4_ARP_ERROR_NO_BUFFERS] = "ARP requests out of buffer",
255 [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
256 [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
257 [IP4_ARP_ERROR_NO_SOURCE_ADDRESS] = "no source address for ARP request",
261 VLIB_REGISTER_NODE (ip4_arp_node) =
264 .vector_size = sizeof (u32),
265 .format_trace = format_ip4_forward_next_trace,
266 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
267 .error_strings = ip4_arp_error_strings,
268 .n_next_nodes = IP4_ARP_N_NEXT,
270 [IP4_ARP_NEXT_DROP] = "ip4-drop",
274 VLIB_REGISTER_NODE (ip4_glean_node) =
277 .vector_size = sizeof (u32),
278 .format_trace = format_ip4_forward_next_trace,
279 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
280 .error_strings = ip4_arp_error_strings,
281 .n_next_nodes = IP4_ARP_N_NEXT,
283 [IP4_ARP_NEXT_DROP] = "ip4-drop",
288 #define foreach_notrace_ip4_arp_error \
296 static clib_error_t *
297 arp_notrace_init (vlib_main_t * vm)
299 vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, ip4_arp_node.index);
301 /* don't trace ARP request packets */
303 vnet_pcap_drop_trace_filter_add_del \
304 (rt->errors[IP4_ARP_ERROR_##a], \
306 foreach_notrace_ip4_arp_error;
311 VLIB_INIT_FUNCTION (arp_notrace_init);
314 * fd.io coding-style-patch-verification: ON
317 * eval: (c-set-style "gnu")