aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/ip-neighbor/ip6_neighbor.c
blob: fd0dbfcd2bbd47955ef0bb14799423978277ad86 (plain)
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
/*
 * ip/ip6_neighbor.c: IP6 neighbor handling
 *
 * Copyright (c) 2010 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/ip-neighbor/ip6_neighbor.h>
#include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
#include <vnet/util/throttle.h>
#include <vnet/fib/fib_sas.h>
#include <vnet/ip/ip_sas.h>

/** ND throttling */
static throttle_t nd_throttle;

VLIB_REGISTER_LOG_CLASS (ip6_neighbor_log, static) = {
  .class_name = "ip6",
  .subclass_name = "neighbor",
};

#define log_debug(fmt, ...)                                                   \
  vlib_log_debug (ip6_neighbor_log.class, fmt, __VA_ARGS__)
void
ip6_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
			const ip6_address_t *dst)
{
  ip6_address_t src;

  if (fib_sas6_get (sw_if_index, dst, &src) ||
      ip6_sas_by_sw_if_index (sw_if_index, dst, &src))
    ip6_neighbor_probe (vlib_get_main (), vnet_get_main (), sw_if_index,
			thread_index, &src, dst);
}

void
ip6_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index,
			u32 thread_index, const ip6_address_t *addr)
{
  vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
  ip6_main_t *i6m = &ip6_main;
  u8 *rewrite, rewrite_len;
  u8 dst_address[6];

  if (NULL == addr)
    addr = ip6_interface_first_address (i6m, sw_if_index);

  if (addr)
    {
      log_debug ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
		 format_ip6_address, addr, sw_if_index);

      /* Form unsolicited neighbor advertisement packet from NS pkt template */
      int bogus_length;
      u32 bi = 0;
      icmp6_neighbor_solicitation_header_t *h =
	vlib_packet_template_get_packet (vm,
					 &ip6_neighbor_packet_template,
					 &bi);
      if (!h)
	return;

      ip6_set_reserved_multicast_address (&h->ip.dst_address,
					  IP6_MULTICAST_SCOPE_link_local,
					  IP6_MULTICAST_GROUP_ID_all_hosts);
      h->ip.src_address = addr[0];
      h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
      h->neighbor.target_address = addr[0];
      h->neighbor.advertisement_flags = clib_host_to_net_u32
	(ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
      h->link_layer_option.header.type =
	ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
      clib_memcpy (h->link_layer_option.ethernet_address,
		   hi->hw_address, vec_len (hi->hw_address));
      h->neighbor.icmp.checksum =
	ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
      ASSERT (bogus_length == 0);

      /* Setup MAC header with IP6 Etype and mcast DMAC */
      vlib_buffer_t *b = vlib_get_buffer (vm, bi);
      ip6_multicast_ethernet_address (dst_address,
				      IP6_MULTICAST_GROUP_ID_all_hosts);
      rewrite =
	ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
      rewrite_len = vec_len (rewrite);
      vlib_buffer_advance (b, -rewrite_len);
      ethernet_header_t *e = vlib_buffer_get_current (b);
      clib_memcpy (e->dst_address, rewrite, rewrite_len);
      vec_free (rewrite);

      /* Send unsolicited ND advertisement packet out the specified interface */
      vnet_buffer (b)->sw_if_index[VLIB_RX] =
	vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
      vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
      u32 *to_next = vlib_frame_vector_args (f);
      to_next[0] = bi;
      f->n_vectors = 1;
      vlib_put_frame_to_node (vm, hi->output_node_index, f);

      vlib_increment_simple_counter (
	&ip_neighbor_counters[AF_IP6].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT],
	thread_index, sw_if_index, 1);
    }
}

typedef enum
{
  IP6_NBR_NEXT_DROP,
  IP6_NBR_NEXT_REPLY_TX,
  IP6_NBR_N_NEXT,
} ip6_discover_neighbor_next_t;

static uword
ip6_discover_neighbor_inline (vlib_main_t * vm,
			      vlib_node_runtime_t * node,
			      vlib_frame_t * frame, int is_glean)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 *from, *to_next_drop;
  uword n_left_from, n_left_to_next_drop;
  u64 seed;
  u32 thread_index = vm->thread_index;

  if (node->flags & VLIB_NODE_FLAG_TRACE)
    ip6_forward_next_trace (vm, node, frame, VLIB_TX);

  seed = throttle_seed (&nd_throttle, thread_index, vlib_time_now (vm));

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;

  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, IP6_NBR_NEXT_DROP,
			   to_next_drop, n_left_to_next_drop);

      while (n_left_from > 0 && n_left_to_next_drop > 0)
	{
	  u32 pi0, adj_index0, sw_if_index0, drop0, r0;
	  vnet_hw_interface_t *hw_if0;
	  vlib_buffer_t *p0, *b0;
	  ip_adjacency_t *adj0;
	  ip6_address_t src;
	  ip6_header_t *ip0;

	  pi0 = from[0];

	  p0 = vlib_get_buffer (vm, pi0);

	  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];

	  ip0 = vlib_buffer_get_current (p0);

	  adj0 = adj_get (adj_index0);

	  if (!is_glean)
	    {
	      ip0->dst_address.as_u64[0] =
		adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
	      ip0->dst_address.as_u64[1] =
		adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
	    }

	  sw_if_index0 = adj0->rewrite_header.sw_if_index;
	  vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;

	  /* combine the address and interface for a hash */
	  r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;

	  drop0 = throttle_check (&nd_throttle, thread_index, r0, seed);

	  from += 1;
	  n_left_from -= 1;
	  to_next_drop[0] = pi0;
	  to_next_drop += 1;
	  n_left_to_next_drop -= 1;

	  if (drop0)
	    {
	      p0->error = node->errors[IP6_NEIGHBOR_ERROR_THROTTLED];
	      continue;
	    }

	  hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);

	  /* If the interface is link-down, drop the pkt */
	  if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
	    drop0 = 1;

	  if (!ip6_link_is_enabled (sw_if_index0))
	    drop0 = 1;

	  /*
	   * the adj has been updated to a rewrite but the node the DPO that got
	   * us here hasn't - yet. no big deal. we'll drop while we wait.
	   */
	  if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
	    drop0 = 1;

	  if (drop0)
	    {
	      p0->error = node->errors[IP6_NEIGHBOR_ERROR_DROP];
	      continue;
	    }

	  /*
	   * Choose source address based on destination lookup
	   * adjacency.
	   */
	  const ip6_address_t *ll = ip6_get_link_local_address (sw_if_index0);
	  if (!ll)
	    {
	      /* There is no address on the interface */
	      p0->error = node->errors[IP6_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
	      continue;
	    }
	  ip6_address_copy (&src, ll);

	  b0 = ip6_neighbor_probe (vm, vnm, sw_if_index0, thread_index, &src,
				   &ip0->dst_address);

	  if (PREDICT_TRUE (NULL != b0))
	    {
	      clib_memcpy_fast (b0->opaque2, p0->opaque2,
				sizeof (p0->opaque2));
	      b0->flags |= p0->flags & VLIB_BUFFER_IS_TRACED;
	      b0->trace_handle = p0->trace_handle;
	      p0->error = node->errors[IP6_NEIGHBOR_ERROR_REQUEST_SENT];
	    }
	  else
	    {
	      /* There is no address on the interface */
	      p0->error = node->errors[IP6_NEIGHBOR_ERROR_NO_BUFFERS];
	      continue;
	    }
	}

      vlib_put_next_frame (vm, node, IP6_NBR_NEXT_DROP, n_left_to_next_drop);
    }

  return frame->n_vectors;
}

static uword
ip6_discover_neighbor (vlib_main_t * vm,
		       vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (ip6_discover_neighbor_inline (vm, node, frame, 0));
}

static uword
ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (ip6_discover_neighbor_inline (vm, node, frame, 1));
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ip6_glean_node) =
{
  .function = ip6_glean,
  .name = "ip6-glean",
  .vector_size = sizeof (u32),
  .format_trace = format_ip6_forward_next_trace,
  .n_errors = IP6_NEIGHBOR_N_ERROR,
  .error_counters = ip6_neighbor_error_counters,
  .n_next_nodes = IP6_NBR_N_NEXT,
  .next_nodes =
  {
    [IP6_NBR_NEXT_DROP] = "ip6-drop",
    [IP6_NBR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
  },
};
VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
{
  .function = ip6_discover_neighbor,
  .name = "ip6-discover-neighbor",
  .vector_size = sizeof (u32),
  .format_trace = format_ip6_forward_next_trace,
  .n_errors = IP6_NEIGHBOR_N_ERROR,
  .error_counters = ip6_neighbor_error_counters,
  .n_next_nodes = IP6_NBR_N_NEXT,
  .next_nodes =
  {
    [IP6_NBR_NEXT_DROP] = "ip6-drop",
    [IP6_NBR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
  },
};
/* *INDENT-ON* */

/* Template used to generate IP6 neighbor solicitation packets. */
vlib_packet_template_t ip6_neighbor_packet_template;

static clib_error_t *
ip6_neighbor_init (vlib_main_t * vm)
{
  icmp6_neighbor_solicitation_header_t p;

  clib_memset (&p, 0, sizeof (p));

  p.ip.ip_version_traffic_class_and_flow_label =
    clib_host_to_net_u32 (0x6 << 28);
  p.ip.payload_length =
    clib_host_to_net_u16 (sizeof (p) -
			  STRUCT_OFFSET_OF
			  (icmp6_neighbor_solicitation_header_t, neighbor));
  p.ip.protocol = IP_PROTOCOL_ICMP6;
  p.ip.hop_limit = 255;
  ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);

  p.neighbor.icmp.type = ICMP6_neighbor_solicitation;

  p.link_layer_option.header.type =
    ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
  p.link_layer_option.header.n_data_u64s =
    sizeof (p.link_layer_option) / sizeof (u64);

  vlib_packet_template_init (vm,
			     &ip6_neighbor_packet_template, &p, sizeof (p),
			     /* alloc chunk size */ 8,
			     "ip6 neighbor discovery");

  return NULL;
}

VLIB_INIT_FUNCTION (ip6_neighbor_init);

static clib_error_t *
ip6_nd_main_loop_enter (vlib_main_t * vm)
{
  vlib_thread_main_t *tm = &vlib_thread_main;

  throttle_init (&nd_throttle, tm->n_vlib_mains, THROTTLE_BITS, 1e-3);

  return 0;
}

VLIB_MAIN_LOOP_ENTER_FUNCTION (ip6_nd_main_loop_enter);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */