summaryrefslogtreecommitdiffstats
path: root/src/plugins/http_static/static_server.c
blob: cd54670884a0cd17c6b54e9d907d7a5b5f2bbb0c (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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
 * Copyright (c) 2017-2022 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 <http_static/http_static.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/** @file static_server.c
 *  Static http server, sufficient to serve .html / .css / .js content.
 */
/*? %%clicmd:group_label Static HTTP Server %% ?*/

#define HSS_FIFO_THRESH (16 << 10)

hss_main_t hss_main;

static hss_session_t *
hss_session_alloc (u32 thread_index)
{
  hss_main_t *hsm = &hss_main;
  hss_session_t *hs;

  pool_get_zero (hsm->sessions[thread_index], hs);
  hs->session_index = hs - hsm->sessions[thread_index];
  hs->thread_index = thread_index;
  hs->cache_pool_index = ~0;
  return hs;
}

static hss_session_t *
hss_session_get (u32 thread_index, u32 hs_index)
{
  hss_main_t *hsm = &hss_main;
  if (pool_is_free_index (hsm->sessions[thread_index], hs_index))
    return 0;
  return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
}

static void
hss_session_free (hss_session_t *hs)
{
  hss_main_t *hsm = &hss_main;

  pool_put (hsm->sessions[hs->thread_index], hs);

  if (CLIB_DEBUG)
    {
      u32 save_thread_index;
      save_thread_index = hs->thread_index;
      /* Poison the entry, preserve timer state and thread index */
      memset (hs, 0xfa, sizeof (*hs));
      hs->thread_index = save_thread_index;
    }
}

/** \brief Disconnect a session
 */
static void
hss_session_disconnect_transport (hss_session_t *hs)
{
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
  a->handle = hs->vpp_session_handle;
  a->app_index = hss_main.app_index;
  vnet_disconnect_session (a);
}

static void
start_send_data (hss_session_t *hs, http_status_code_t status)
{
  http_msg_t msg;
  session_t *ts;
  int rv;

  ts = session_get (hs->vpp_session_index, hs->thread_index);

  msg.type = HTTP_MSG_REPLY;
  msg.code = status;
  msg.content_type = HTTP_CONTENT_TEXT_HTML;
  msg.data.len = hs->data_len;

  if (hs->data_len > hss_main.use_ptr_thresh)
    {
      msg.data.type = HTTP_MSG_DATA_PTR;
      rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
      ASSERT (rv == sizeof (msg));

      uword data = pointer_to_uword (hs->data);
      rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (data), (u8 *) &data);
      ASSERT (rv == sizeof (sizeof (data)));

      goto done;
    }

  msg.data.type = HTTP_MSG_DATA_INLINE;

  rv = svm_fifo_enqueue (ts->tx_fifo, sizeof (msg), (u8 *) &msg);
  ASSERT (rv == sizeof (msg));

  if (!msg.data.len)
    goto done;

  rv = svm_fifo_enqueue (ts->tx_fifo, hs->data_len, hs->data);

  if (rv != hs->data_len)
    {
      hs->data_offset = rv;
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
    }

done:

  if (svm_fifo_set_event (ts->tx_fifo))
    session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
}

__clib_export void
hss_session_send_data (hss_url_handler_args_t *args)
{
  hss_session_t *hs;

  hs = hss_session_get (args->sh.thread_index, args->sh.session_index);

  if (hs->data && hs->free_data)
    vec_free (hs->data);

  hs->data = args->data;
  hs->data_len = args->data_len;
  hs->free_data = args->free_vec_data;
  start_send_data (hs, args->sc);
}

static int
try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
		 u8 *request)
{
  http_status_code_t sc = HTTP_STATUS_OK;
  hss_url_handler_args_t args = {};
  uword *p, *url_table;
  int rv;

  if (!hsm->enable_url_handlers || !request)
    return -1;

  /* Look for built-in GET / POST handlers */
  url_table =
    (rt == HTTP_REQ_GET) ? hsm->get_url_handlers : hsm->post_url_handlers;

  p = hash_get_mem (url_table, request);
  if (!p)
    return -1;

  hs->path = 0;
  hs->data_offset = 0;
  hs->cache_pool_index = ~0;

  if (hsm->debug_level > 0)
    clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", request);

  args.reqtype = rt;
  args.request = request;
  args.sh.thread_index = hs->thread_index;
  args.sh.session_index = hs->session_index;

  rv = ((hss_url_handler_fn) p[0]) (&args);

  /* Wait for data from handler */
  if (rv == HSS_URL_HANDLER_ASYNC)
    return 0;

  if (rv == HSS_URL_HANDLER_ERROR)
    {
      clib_warning ("builtin handler %llx hit on %s '%s' but failed!", p[0],
		    (rt == HTTP_REQ_GET) ? "GET" : "POST", request);
      sc = HTTP_STATUS_NOT_FOUND;
    }

  hs->data = args.data;
  hs->data_len = args.data_len;
  hs->free_data = args.free_vec_data;

  start_send_data (hs, sc);

  if (!hs->data)
    hss_session_disconnect_transport (hs);

  return 0;
}

static u8
file_path_is_valid (u8 *path)
{
  struct stat _sb, *sb = &_sb;

  if (stat ((char *) path, sb) < 0 /* can't stat the file */
      || (sb->st_mode & S_IFMT) != S_IFREG /* not a regular file */)
    return 0;

  return 1;
}

static u32
try_index_file (hss_main_t *hsm, hss_session_t *hs, u8 *path)
{
  u8 *port_str = 0, *redirect;
  transport_endpoint_t endpt;
  transport_proto_t proto;
  int print_port = 0;
  u16 local_port;
  session_t *ts;
  u32 plen;

  /* Remove the trailing space */
  _vec_len (path) -= 1;
  plen = vec_len (path);

  /* Append "index.html" */
  if (path[plen - 1] != '/')
    path = format (path, "/index.html%c", 0);
  else
    path = format (path, "index.html%c", 0);

  if (hsm->debug_level > 0)
    clib_warning ("trying to find index: %s", path);

  if (!file_path_is_valid (path))
    return HTTP_STATUS_NOT_FOUND;

  /*
   * We found an index.html file, build a redirect
   */
  vec_delete (path, vec_len (hsm->www_root) - 1, 0);

  ts = session_get (hs->vpp_session_index, hs->thread_index);
  session_get_endpoint (ts, &endpt, 1 /* is_local */);

  local_port = clib_net_to_host_u16 (endpt.port);
  proto = session_type_transport_proto (ts->session_type);

  if ((proto == TRANSPORT_PROTO_TCP && local_port != 80) ||
      (proto == TRANSPORT_PROTO_TLS && local_port != 443))
    {
      print_port = 1;
      port_str = format (0, ":%u", (u32) local_port);
    }

  redirect =
    format (0,
	    "HTTP/1.1 301 Moved Permanently\r\n"
	    "Location: http%s://%U%s%s\r\n\r\n",
	    proto == TRANSPORT_PROTO_TLS ? "s" : "", format_ip46_address,
	    &endpt.ip, endpt.is_ip4, print_port ? port_str : (u8 *) "", path);

  if (hsm->debug_level > 0)
    clib_warning ("redirect: %s", redirect);

  vec_free (port_str);

  hs->data = redirect;
  hs->data_len = vec_len (redirect);
  hs->free_data = 1;

  return HTTP_STATUS_OK;
}

static int
try_file_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
		  u8 *request)
{
  http_status_code_t sc = HTTP_STATUS_OK;
  u8 *path;
  u32 ce_index;

  /* Feature not enabled */
  if (!hsm->www_root)
    return -1;

  /*
   * Construct the file to open
   * Browsers are capable of sporadically including a leading '/'
   */
  if (!request)
    path = format (0, "%s%c", hsm->www_root, 0);
  else if (request[0] == '/')
    path = format (0, "%s%s%c", hsm->www_root, request, 0);
  else
    path = format (0, "%s/%s%c", hsm->www_root, request, 0);

  if (hsm->debug_level > 0)
    clib_warning ("%s '%s'", (rt == HTTP_REQ_GET) ? "GET" : "POST", path);

  if (hs->data && hs->free_data)
    vec_free (hs->data);

  hs->path = path;
  hs->data_offset = 0;

  ce_index =
    hss_cache_lookup_and_attach (&hsm->cache, path, &hs->data, &hs->data_len);
  if (ce_index == ~0)
    {
      if (!file_path_is_valid (path))
	{
	  sc = try_index_file (hsm, hs, path);
	  goto done;
	}
      ce_index =
	hss_cache_add_and_attach (&hsm->cache, path, &hs->data, &hs->data_len);
      if (ce_index == ~0)
	{
	  sc = HTTP_STATUS_INTERNAL_ERROR;
	  goto done;
	}
    }

  hs->cache_pool_index = ce_index;

done:

  start_send_data (hs, sc);
  if (!hs->data)
    hss_session_disconnect_transport (hs);

  return 0;
}

static int
handle_request (hss_session_t *hs, http_req_method_t rt, u8 *request)
{
  hss_main_t *hsm = &hss_main;

  if (!try_url_handler (hsm, hs, rt, request))
    return 0;

  if (!try_file_handler (hsm, hs, rt, request))
    return 0;

  /* Handler did not find anything return 404 */
  start_send_data (hs, HTTP_STATUS_NOT_FOUND);
  hss_session_disconnect_transport (hs);

  return 0;
}

static int
hss_ts_rx_callback (session_t *ts)
{
  hss_session_t *hs;
  u8 *request = 0;
  http_msg_t msg;
  int rv;

  hs = hss_session_get (ts->thread_index, ts->opaque);

  /* Read the http message header */
  rv = svm_fifo_dequeue (ts->rx_fifo, sizeof (msg), (u8 *) &msg);
  ASSERT (rv == sizeof (msg));

  if (msg.type != HTTP_MSG_REQUEST ||
      (msg.method_type != HTTP_REQ_GET && msg.method_type != HTTP_REQ_POST))
    {
      hs->data = 0;
      start_send_data (hs, HTTP_STATUS_METHOD_NOT_ALLOWED);
      return 0;
    }

  /* Read request */
  if (msg.data.len)
    {
      vec_validate (request, msg.data.len - 1);
      rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
      ASSERT (rv == msg.data.len);
    }

  /* Find and send data */
  handle_request (hs, msg.method_type, request);

  vec_free (request);

  return 0;
}

static int
hss_ts_tx_callback (session_t *ts)
{
  hss_session_t *hs;
  u32 to_send;
  int rv;

  hs = hss_session_get (ts->thread_index, ts->opaque);
  if (!hs || !hs->data)
    return 0;

  to_send = hs->data_len - hs->data_offset;
  rv = svm_fifo_enqueue (ts->tx_fifo, to_send, hs->data + hs->data_offset);

  if (rv <= 0)
    {
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
      return 0;
    }

  if (rv < to_send)
    {
      hs->data_offset += rv;
      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
    }

  if (svm_fifo_set_event (ts->tx_fifo))
    session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);

  return 0;
}

/** \brief Session accept callback
 */
static int
hss_ts_accept_callback (session_t *ts)
{
  hss_session_t *hs;
  u32 thresh;

  hs = hss_session_alloc (ts->thread_index);

  hs->vpp_session_index = ts->session_index;
  hs->vpp_session_handle = session_handle (ts);

  /* The application sets a threshold for it's fifo to get notified when
   * additional data can be enqueued. We want to keep the TX fifo reasonably
   * full, however avoid entering a state where the
   * fifo is full all the time and small chunks of data are being enqueued
   * each time. If the fifo is small (under 16K) we set
   * the threshold to it's size, meaning a notification will be given when the
   * fifo empties.
   */
  thresh = clib_min (svm_fifo_size (ts->tx_fifo), HSS_FIFO_THRESH);
  svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);

  ts->opaque = hs->session_index;
  ts->session_state = SESSION_STATE_READY;
  return 0;
}

static void
hss_ts_disconnect_callback (session_t *ts)
{
  hss_main_t *hsm = &hss_main;
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;

  a->handle = session_handle (ts);
  a->app_index = hsm->app_index;
  vnet_disconnect_session (a);
}

static void
hss_ts_reset_callback (session_t *ts)
{
  hss_main_t *hsm = &hss_main;
  vnet_disconnect_args_t _a = { 0 }, *a = &_a;

  a->handle = session_handle (ts);
  a->app_index = hsm->app_index;
  vnet_disconnect_session (a);
}

static int
hss_ts_connected_callback (u32 app_index, u32 api_context, session_t *ts,
			   session_error_t err)
{
  clib_warning ("called...");
  return -1;
}

static int
hss_add_segment_callback (u32 client_index, u64 segment_handle)
{
  return 0;
}

static void
hss_ts_cleanup (session_t *s, session_cleanup_ntf_t ntf)
{
  hss_main_t *hsm = &hss_main;
  hss_session_t *hs;

  if (ntf == SESSION_CLEANUP_TRANSPORT)
    return;

  hs = hss_session_get (s->thread_index, s->opaque);
  if (!hs)
    return;

  if (hs->cache_pool_index != ~0)
    {
      hss_cache_detach_entry (&hsm->cache, hs->cache_pool_index);
      hs->cache_pool_index = ~0;
    }

  if (hs->free_data)
    vec_free (hs->data);
  hs->data = 0;
  hs->data_offset = 0;
  hs->free_data = 0;
  vec_free (hs->path);

  hss_session_free (hs);
}

static session_cb_vft_t hss_cb_vft = {
  .session_accept_callback = hss_ts_accept_callback,
  .session_disconnect_callback = hss_ts_disconnect_callback,
  .session_connected_callback = hss_ts_connected_callback,
  .add_segment_callback = hss_add_segment_callback,
  .builtin_app_rx_callback = hss_ts_rx_callback,
  .builtin_app_tx_callback = hss_ts_tx_callback,
  .session_reset_callback = hss_ts_reset_callback,
  .session_cleanup_callback = hss_ts_cleanup,
};

static int
hss_attach ()
{
  vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
  hss_main_t *hsm = &hss_main;
  u64 options[APP_OPTIONS_N_OPTIONS];
  vnet_app_attach_args_t _a, *a = &_a;
  u32 segment_size = 128 << 20;

  clib_memset (a, 0, sizeof (*a));
  clib_memset (options, 0, sizeof (options));

  if (hsm->private_segment_size)
    segment_size = hsm->private_segment_size;

  a->api_client_index = ~0;
  a->name = format (0, "http_static_server");
  a->session_cb_vft = &hss_cb_vft;
  a->options = options;
  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
  a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
  a->options[APP_OPTIONS_RX_FIFO_SIZE] =
    hsm->fifo_size ? hsm->fifo_size : 8 << 10;
  a->options[APP_OPTIONS_TX_FIFO_SIZE] =
    hsm->fifo_size ? hsm->fifo_size : 32 << 10;
  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
  a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
  a->options[APP_OPTIONS_TLS_ENGINE] = CRYPTO_ENGINE_OPENSSL;

  if (vnet_application_attach (a))
    {
      vec_free (a->name);
      clib_warning ("failed to attach server");
      return -1;
    }
  vec_free (a->name);
  hsm->app_index = a->app_index;

  clib_memset (ck_pair, 0, sizeof (*ck_pair));
  ck_pair->cert = (u8 *) test_srv_crt_rsa;
  ck_pair->key = (u8 *) test_srv_key_rsa;
  ck_pair->cert_len = test_srv_crt_rsa_len;
  ck_pair->key_len = test_srv_key_rsa_len;
  vnet_app_add_cert_key_pair (ck_pair);
  hsm->ckpair_index = ck_pair->index;

  return 0;
}

static int
hss_transport_needs_crypto (transport_proto_t proto)
{
  return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
	 proto == TRANSPORT_PROTO_QUIC;
}

static int
hss_listen (void)
{
  hss_main_t *hsm = &hss_main;
  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
  vnet_listen_args_t _a, *a = &_a;
  char *uri = "tcp://0.0.0.0/80";
  u8 need_crypto;
  int rv;

  clib_memset (a, 0, sizeof (*a));
  a->app_index = hsm->app_index;

  if (hsm->uri)
    uri = (char *) hsm->uri;

  if (parse_uri (uri, &sep))
    return -1;

  need_crypto = hss_transport_needs_crypto (sep.transport_proto);

  sep.transport_proto = TRANSPORT_PROTO_HTTP;
  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));

  if (need_crypto)
    {
      session_endpoint_alloc_ext_cfg (&a->sep_ext,
				      TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
      a->sep_ext.ext_cfg->crypto.ckpair_index = hsm->ckpair_index;
    }

  rv = vnet_listen (a);

  if (need_crypto)
    clib_mem_free (a->sep_ext.ext_cfg);

  return rv;
}

static void
hss_url_handlers_init (hss_main_t *hsm)
{
  if (!hsm->get_url_handlers)
    {
      hsm->get_url_handlers = hash_create_string (0, sizeof (uword));
      hsm->post_url_handlers = hash_create_string (0, sizeof (uword));
    }

  hss_builtinurl_json_handlers_init ();
}

int
hss_create (vlib_main_t *vm)
{
  vlib_thread_main_t *vtm = vlib_get_thread_main ();
  hss_main_t *hsm = &hss_main;
  u32 num_threads;

  num_threads = 1 /* main thread */  + vtm->n_threads;
  vec_validate (hsm->sessions, num_threads - 1);

  if (hss_attach ())
    {
      clib_warning ("failed to attach server");
      return -1;
    }
  if (hss_listen ())
    {
      clib_warning ("failed to start listening");
      return -1;
    }

  if (hsm->www_root)
    hss_cache_init (&hsm->cache, hsm->cache_size, hsm->debug_level);

  if (hsm->enable_url_handlers)
    hss_url_handlers_init (hsm);

  return 0;
}

static clib_error_t *
hss_create_command_fn (vlib_main_t *vm, unformat_input_t *input,
		       vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  hss_main_t *hsm = &hss_main;
  clib_error_t *error = 0;
  u64 seg_size;
  int rv;

  if (hsm->app_index != (u32) ~0)
    return clib_error_return (0, "http server already running...");

  hsm->prealloc_fifos = 0;
  hsm->private_segment_size = 0;
  hsm->fifo_size = 0;
  hsm->cache_size = 10 << 20;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    goto no_input;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "www-root %s", &hsm->www_root))
	;
      else
	if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos))
	;
      else if (unformat (line_input, "private-segment-size %U",
			 unformat_memory_size, &seg_size))
	hsm->private_segment_size = seg_size;
      else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size))
	hsm->fifo_size <<= 10;
      else if (unformat (line_input, "cache-size %U", unformat_memory_size,
			 &hsm->cache_size))
	;
      else if (unformat (line_input, "uri %s", &hsm->uri))
	;
      else if (unformat (line_input, "debug %d", &hsm->debug_level))
	;
      else if (unformat (line_input, "debug"))
	hsm->debug_level = 1;
      else if (unformat (line_input, "ptr-thresh %U", unformat_memory_size,
			 &hsm->use_ptr_thresh))
	;
      else if (unformat (line_input, "url-handlers"))
	hsm->enable_url_handlers = 1;
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, line_input);
	}
    }

  unformat_free (line_input);

no_input:

  if (error)
    goto done;

  if (hsm->www_root == 0 && !hsm->enable_url_handlers)
    {
      error = clib_error_return (0, "Must set www-root or url-handlers");
      goto done;
    }

  if (hsm->cache_size < (128 << 10))
    {
      error = clib_error_return (0, "cache-size must be at least 128kb");
      vec_free (hsm->www_root);
      goto done;
    }

  vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );

  if ((rv = hss_create (vm)))
    {
      error = clib_error_return (0, "server_create returned %d", rv);
      vec_free (hsm->www_root);
    }

done:

  return error;
}

/*?
 * Enable the static http server
 *
 * @cliexpar
 * This command enables the static http server. Only the www-root
 * parameter is required
 * @clistart
 * http static server www-root /tmp/www uri tcp://0.0.0.0/80 cache-size 2m
 * @cliend
 * @cliexcmd{http static server www-root <path> [prealloc-fios <nn>]
 *   [private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]}
?*/
VLIB_CLI_COMMAND (hss_create_command, static) = {
  .path = "http static server",
  .short_help =
    "http static server www-root <path> [prealloc-fifos <nn>]\n"
    "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n"
    "[ptr-thresh <nn>] [url-handlers] [debug [nn]]\n",
  .function = hss_create_command_fn,
};

static u8 *
format_hss_session (u8 *s, va_list *args)
{
  hss_session_t *hs = va_arg (*args, hss_session_t *);
  int __clib_unused verbose = va_arg (*args, int);

  s = format (s, "\n path %s, data length %u, data_offset %u",
	      hs->path ? hs->path : (u8 *) "[none]", hs->data_len,
	      hs->data_offset);
  return s;
}

static clib_error_t *
hss_show_command_fn (vlib_main_t *vm, unformat_input_t *input,
		     vlib_cli_command_t *cmd)
{
  int verbose = 0, show_cache = 0, show_sessions = 0;
  hss_main_t *hsm = &hss_main;

  if (hsm->www_root == 0)
    return clib_error_return (0, "Static server disabled");

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "verbose %d", &verbose))
	;
      else if (unformat (input, "verbose"))
	verbose = 1;
      else if (unformat (input, "cache"))
	show_cache = 1;
      else if (unformat (input, "sessions"))
	show_sessions = 1;
      else
	break;
    }

  if ((show_cache + show_sessions) == 0)
    return clib_error_return (0, "specify one or more of cache, sessions");

  if (show_cache)
    vlib_cli_output (vm, "%U", format_hss_cache, &hsm->cache, verbose);

  if (show_sessions)
    {
      u32 *session_indices = 0;
      hss_session_t *hs;
      int i, j;


      for (i = 0; i < vec_len (hsm->sessions); i++)
	{
	  pool_foreach (hs, hsm->sessions[i])
            vec_add1 (session_indices, hs - hsm->sessions[i]);

	  for (j = 0; j < vec_len (session_indices); j++)
	    {
	      vlib_cli_output (
		vm, "%U", format_hss_session,
		pool_elt_at_index (hsm->sessions[i], session_indices[j]),
		verbose);
	    }
	  vec_reset_length (session_indices);
	}
      vec_free (session_indices);
    }
  return 0;
}

/*?
 * Display static http server cache statistics
 *
 * @cliexpar
 * This command shows the contents of the static http server cache
 * @clistart
 * show http static server
 * @cliend
 * @cliexcmd{show http static server sessions cache [verbose [nn]]}
?*/
VLIB_CLI_COMMAND (hss_show_command, static) = {
  .path = "show http static server",
  .short_help = "show http static server sessions cache [verbose [<nn>]]",
  .function = hss_show_command_fn,
};

static clib_error_t *
hss_clear_cache_command_fn (vlib_main_t *vm, unformat_input_t *input,
			    vlib_cli_command_t *cmd)
{
  hss_main_t *hsm = &hss_main;
  u32 busy_items = 0;

  if (hsm->www_root == 0)
    return clib_error_return (0, "Static server disabled");

  busy_items = hss_cache_clear (&hsm->cache);

  if (busy_items > 0)
    vlib_cli_output (vm, "Note: %d busy items still in cache...", busy_items);
  else
    vlib_cli_output (vm, "Cache cleared...");
  return 0;
}

/*?
 * Clear the static http server cache, to force the server to
 * reload content from backing files
 *
 * @cliexpar
 * This command clear the static http server cache
 * @clistart
 * clear http static cache
 * @cliend
 * @cliexcmd{clear http static cache}
?*/
VLIB_CLI_COMMAND (clear_hss_cache_command, static) = {
  .path = "clear http static cache",
  .short_help = "clear http static cache",
  .function = hss_clear_cache_command_fn,
};

static clib_error_t *
hss_main_init (vlib_main_t *vm)
{
  hss_main_t *hsm = &hss_main;

  hsm->app_index = ~0;
  hsm->vlib_main = vm;

  return 0;
}

VLIB_INIT_FUNCTION (hss_main_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
"n">f); to_next[0] = bi; f->n_vectors = 1; vlib_put_frame_to_node (vm, ip4_lookup_node.index, f); ep->retry_timer = now + 2.0; } void vnet_dns_send_dns6_request (dns_main_t * dm, dns_cache_entry_t * ep, ip6_address_t * server) { vlib_main_t *vm = dm->vlib_main; f64 now = vlib_time_now (vm); u32 bi; vlib_buffer_t *b; ip6_header_t *ip; fib_prefix_t prefix; fib_node_index_t fei; u32 sw_if_index, fib_index; udp_header_t *udp; ip6_main_t *im6 = &ip6_main; ip_lookup_main_t *lm6 = &im6->lookup_main; ip_interface_address_t *ia = 0; ip6_address_t *src_address; u8 *dns_request; vlib_frame_t *f; u32 *to_next; int junk __attribute__ ((unused)); ASSERT (ep->dns_request); /* Find a FIB path to the server */ clib_memcpy (&prefix.fp_addr, server, sizeof (*server)); prefix.fp_proto = FIB_PROTOCOL_IP6; prefix.fp_len = 32; fib_index = fib_table_find (prefix.fp_proto, 0 /* default VRF for now */ ); if (fib_index == (u32) ~ 0) { clib_warning ("no fib table"); return; } fei = fib_table_lookup (fib_index, &prefix); /* Couldn't find route to destination. Bail out. */ if (fei == FIB_NODE_INDEX_INVALID) { clib_warning ("no route to DNS server"); } sw_if_index = fib_entry_get_resolving_interface (fei); /* *INDENT-OFF* */ foreach_ip_interface_address(lm6, ia, sw_if_index, 1 /* honor unnumbered */, ({ src_address = ip_interface_address_get_address (lm6, ia); goto found_src_address; })); /* *INDENT-ON* */ clib_warning ("FIB BUG"); return; found_src_address: /* Go get a buffer */ if (vlib_buffer_alloc (dm->vlib_main, &bi, 1) != 1) return; b = vlib_get_buffer (vm, bi); b->current_length = sizeof (ip6_header_t) + sizeof (udp_header_t) + vec_len (ep->dns_request); b->total_length_not_including_first_buffer = 0; b->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_LOCALLY_ORIGINATED; ip = vlib_buffer_get_current (b); clib_memset (ip, 0, sizeof (*ip)); udp = (udp_header_t *) (ip + 1); clib_memset (udp, 0, sizeof (*udp)); dns_request = (u8 *) (udp + 1); /* IP header */ ip->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28); ip->payload_length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - sizeof (ip6_header_t)); ip->hop_limit = 255; ip->protocol = IP_PROTOCOL_UDP; clib_memcpy (&ip->src_address, src_address, sizeof (ip6_address_t)); clib_memcpy (&ip->dst_address, server, sizeof (ip6_address_t)); /* UDP header */ udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dns_reply); udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_dns); udp->length = clib_host_to_net_u16 (sizeof (udp_header_t) + vec_len (ep->dns_request)); udp->checksum = 0; udp->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip, &junk); /* The actual DNS request */ clib_memcpy (dns_request, ep->dns_request, vec_len (ep->dns_request)); /* Ship it to ip6_lookup */ f = vlib_get_frame_to_node (vm, ip6_lookup_node.index); to_next = vlib_frame_vector_args (f); to_next[0] = bi; f->n_vectors = 1; ep->retry_timer = now + 2.0; } /** * Translate "foo.com" into "0x3 f o o 0x3 c o m 0x0" * A historical / hysterical micro-TLV scheme. DGMS. */ u8 * name_to_labels (u8 * name) { int i; int last_label_index; u8 *rv; rv = vec_dup (name); /* punch in space for the first length */ vec_insert (rv, 1, 0); last_label_index = 0; i = 1; while (i < vec_len (rv)) { if (rv[i] == '.') { rv[last_label_index] = (i - last_label_index) - 1; if ((i - last_label_index) > 63) clib_warning ("stupid name, label length %d", i - last_label_index); last_label_index = i; rv[i] = 0; } i++; } /* Set the last real label length */ rv[last_label_index] = (i - last_label_index) - 1; /* * Add a [sic] NULL root label. Otherwise, the name parser can't figure out * where to stop. */ vec_add1 (rv, 0); return rv; } /** * arc-function for the above. * Translate "0x3 f o o 0x3 c o m 0x0" into "foo.com" * Produces a non-NULL-terminated u8 *vector. %v format is your friend. */ u8 * vnet_dns_labels_to_name (u8 * label, u8 * full_text, u8 ** parse_from_here) { u8 *reply = 0; u16 offset; u8 len; int i; *parse_from_here = 0; /* chase initial pointer? */ if ((label[0] & 0xC0) == 0xC0) { *parse_from_here = label + 2; offset = ((label[0] & 0x3f) << 8) + label[1]; label = full_text + offset; } len = *label++; while (len) { for (i = 0; i < len; i++) vec_add1 (reply, *label++); /* chase pointer? */ if ((label[0] & 0xC0) == 0xC0) { *parse_from_here = label + 2; offset = ((label[0] & 0x3f) << 8) + label[1]; label = full_text + offset; } len = *label++; if (len) vec_add1 (reply, '.'); } if (*parse_from_here == 0) *parse_from_here = label; return reply; } void vnet_send_dns_request (dns_main_t * dm, dns_cache_entry_t * ep) { dns_header_t *h; dns_query_t *qp; u16 tmp; u8 *request, *name_copy; u32 qp_offset; /* This can easily happen if sitting in GDB, etc. */ if (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID || ep->server_fails > 1) return; /* Construct the dns request, if we haven't been here already */ if (vec_len (ep->dns_request) == 0) { /* * Start with the variadic portion of the exercise. * Turn the name into a set of DNS "labels". Max length * per label is 63, enforce that. */ request = name_to_labels (ep->name); name_copy = vec_dup (request); qp_offset = vec_len (request); /* * At least when testing against "known good" DNS servers: * it turns out that sending 2x requests - one for an A-record * and another for a AAAA-record - seems to work better than * sending a DNS_TYPE_ALL request. */ /* Add space for the query header */ vec_validate (request, 2 * qp_offset + 2 * sizeof (dns_query_t) - 1); qp = (dns_query_t *) (request + qp_offset); qp->type = clib_host_to_net_u16 (DNS_TYPE_A); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); qp++; clib_memcpy (qp, name_copy, vec_len (name_copy)); qp = (dns_query_t *) (((u8 *) qp) + vec_len (name_copy)); vec_free (name_copy); qp->type = clib_host_to_net_u16 (DNS_TYPE_AAAA); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); /* Punch in space for the dns_header_t */ vec_insert (request, sizeof (dns_header_t), 0); h = (dns_header_t *) request; /* Transaction ID = pool index */ h->id = clib_host_to_net_u16 (ep - dm->entries); /* Ask for a recursive lookup */ tmp = DNS_RD | DNS_OPCODE_QUERY; h->flags = clib_host_to_net_u16 (tmp); h->qdcount = clib_host_to_net_u16 (2); h->nscount = 0; h->arcount = 0; ep->dns_request = request; } /* Work out which server / address family we're going to use */ /* Retry using current server */ if (ep->retry_count++ < DNS_RETRIES_PER_SERVER) { if (ep->server_af == 1 /* ip6 */ ) { if (vec_len (dm->ip6_name_servers)) { vnet_dns_send_dns6_request (dm, ep, dm->ip6_name_servers + ep->server_rotor); goto out; } else ep->server_af = 0; } if (vec_len (dm->ip4_name_servers)) { vnet_dns_send_dns4_request (dm, ep, dm->ip4_name_servers + ep->server_rotor); goto out; } } else /* switch to a new server */ { ep->retry_count = 1; ep->server_rotor++; if (ep->server_af == 1 /* ip6 */ ) { if (ep->server_rotor >= vec_len (dm->ip6_name_servers)) { ep->server_rotor = 0; ep->server_af = vec_len (dm->ip4_name_servers) > 0 ? 0 : 1; } } else { if (ep->server_rotor >= vec_len (dm->ip4_name_servers)) { ep->server_rotor = 0; ep->server_af = vec_len (dm->ip6_name_servers) > 0 ? 1 : 0; } } } if (ep->server_af == 1 /* ip6 */ ) vnet_dns_send_dns6_request (dm, ep, dm->ip6_name_servers + ep->server_rotor); else vnet_dns_send_dns4_request (dm, ep, dm->ip4_name_servers + ep->server_rotor); out: vlib_process_signal_event_mt (dm->vlib_main, dns_resolver_node.index, DNS_RESOLVER_EVENT_PENDING, 0); } int vnet_dns_delete_entry_by_index_nolock (dns_main_t * dm, u32 index) { dns_cache_entry_t *ep; int i; if (dm->is_enabled == 0) return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED; if (pool_is_free_index (dm->entries, index)) return VNET_API_ERROR_NO_SUCH_ENTRY; ep = pool_elt_at_index (dm->entries, index); if (!(ep->flags & DNS_CACHE_ENTRY_FLAG_VALID)) { for (i = 0; i < vec_len (dm->unresolved_entries); i++) if (index == dm->unresolved_entries[i]) { vec_delete (dm->unresolved_entries, 1, i); goto found; } clib_warning ("pool elt %d supposedly pending, but not found...", index); } found: hash_unset_mem (dm->cache_entry_by_name, ep->name); vec_free (ep->name); vec_free (ep->pending_requests); pool_put (dm->entries, ep); return 0; } static int dns_delete_by_name (dns_main_t * dm, u8 * name) { int rv; uword *p; if (dm->is_enabled == 0) return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED; dns_cache_lock (dm); p = hash_get_mem (dm->cache_entry_by_name, name); if (!p) { dns_cache_unlock (dm); return VNET_API_ERROR_NO_SUCH_ENTRY; } rv = vnet_dns_delete_entry_by_index_nolock (dm, p[0]); dns_cache_unlock (dm); return rv; } static int delete_random_entry (dns_main_t * dm) { int rv; u32 victim_index, start_index, i; u32 limit; dns_cache_entry_t *ep; if (dm->is_enabled == 0) return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED; /* * Silence spurious coverity warning. We know pool_elts >> 0, or * we wouldn't be here... */ #ifdef __COVERITY__ if (pool_elts (dm->entries) == 0) return VNET_API_ERROR_UNSPECIFIED; #endif dns_cache_lock (dm); limit = pool_elts (dm->entries); start_index = random_u32 (&dm->random_seed) % limit; for (i = 0; i < limit; i++) { victim_index = (start_index + i) % limit; if (!pool_is_free_index (dm->entries, victim_index)) { ep = pool_elt_at_index (dm->entries, victim_index); /* Delete only valid, non-static entries */ if ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) && ((ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC) == 0)) { rv = vnet_dns_delete_entry_by_index_nolock (dm, victim_index); dns_cache_unlock (dm); return rv; } } } dns_cache_unlock (dm); clib_warning ("Couldn't find an entry to delete?"); return VNET_API_ERROR_UNSPECIFIED; } static int dns_add_static_entry (dns_main_t * dm, u8 * name, u8 * dns_reply_data) { dns_cache_entry_t *ep; uword *p; int rv; if (dm->is_enabled == 0) return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED; dns_cache_lock (dm); p = hash_get_mem (dm->cache_entry_by_name, name); if (p) { dns_cache_unlock (dm); return VNET_API_ERROR_ENTRY_ALREADY_EXISTS; } if (pool_elts (dm->entries) == dm->name_cache_size) { /* Will only fail if the cache is totally filled w/ static entries... */ rv = delete_random_entry (dm); if (rv) { dns_cache_unlock (dm); return rv; } } pool_get (dm->entries, ep); clib_memset (ep, 0, sizeof (*ep)); /* Note: consumes the name vector */ ep->name = name; hash_set_mem (dm->cache_entry_by_name, ep->name, ep - dm->entries); ep->flags = DNS_CACHE_ENTRY_FLAG_VALID | DNS_CACHE_ENTRY_FLAG_STATIC; ep->dns_response = dns_reply_data; dns_cache_unlock (dm); return 0; } int vnet_dns_resolve_name (dns_main_t * dm, u8 * name, dns_pending_request_t * t, dns_cache_entry_t ** retp) { dns_cache_entry_t *ep; int rv; f64 now; uword *p; dns_pending_request_t *pr; int count; now = vlib_time_now (dm->vlib_main); /* In case we can't actually answer the question right now... */ *retp = 0; dns_cache_lock (dm); search_again: p = hash_get_mem (dm->cache_entry_by_name, name); if (p) { ep = pool_elt_at_index (dm->entries, p[0]); if (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) { /* Has the entry expired? */ if (((ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC) == 0) && (now > ep->expiration_time)) { int i; u32 *indices_to_delete = 0; /* * Take out the rest of the resolution chain * This isn't optimal, but it won't happen very often. */ while (ep) { if ((ep->flags & DNS_CACHE_ENTRY_FLAG_CNAME)) { vec_add1 (indices_to_delete, ep - dm->entries); p = hash_get_mem (dm->cache_entry_by_name, ep->cname); if (!p) break; ep = pool_elt_at_index (dm->entries, p[0]); } else { vec_add1 (indices_to_delete, ep - dm->entries); break; } } for (i = 0; i < vec_len (indices_to_delete); i++) { /* Reenable to watch re-resolutions */ if (0) { ep = pool_elt_at_index (dm->entries, indices_to_delete[i]); clib_warning ("Re-resolve %s", ep->name); } vnet_dns_delete_entry_by_index_nolock (dm, indices_to_delete[i]); } vec_free (indices_to_delete); /* Yes, kill it... */ goto re_resolve; } if (ep->flags & DNS_CACHE_ENTRY_FLAG_CNAME) { name = ep->cname; goto search_again; } /* Note: caller must drop the lock! */ *retp = ep; return (0); } else { /* * Resolution pending. Add request to the pending vector * by copying the template request */ vec_add2 (ep->pending_requests, pr, 1); memcpy (pr, t, sizeof (*pr)); dns_cache_unlock (dm); return (0); } } re_resolve: if (pool_elts (dm->entries) == dm->name_cache_size) { /* Will only fail if the cache is totally filled w/ static entries... */ rv = delete_random_entry (dm); if (rv) { dns_cache_unlock (dm); return rv; } } /* add new hash table entry */ pool_get (dm->entries, ep); clib_memset (ep, 0, sizeof (*ep)); ep->name = format (0, "%s%c", name, 0); _vec_len (ep->name) = vec_len (ep->name) - 1; hash_set_mem (dm->cache_entry_by_name, ep->name, ep - dm->entries); vec_add1 (dm->unresolved_entries, ep - dm->entries); vec_add2 (ep->pending_requests, pr, 1); pr->request_type = t->request_type; /* Remember details so we can reply later... */ if (t->request_type == DNS_API_PENDING_NAME_TO_IP || t->request_type == DNS_API_PENDING_IP_TO_NAME) { pr->client_index = t->client_index; pr->client_context = t->client_context; } else { pr->client_index = ~0; pr->is_ip6 = t->is_ip6; pr->dst_port = t->dst_port; pr->id = t->id; pr->name = t->name; if (t->is_ip6) count = 16; else count = 4; clib_memcpy (pr->dst_address, t->dst_address, count); } vnet_send_dns_request (dm, ep); dns_cache_unlock (dm); return 0; } #define foreach_notification_to_move \ _(pending_requests) /** * Handle cname indirection. JFC. Called with the cache locked. * returns 0 if the reply is not a CNAME. */ int vnet_dns_cname_indirection_nolock (dns_main_t * dm, u32 ep_index, u8 * reply) { dns_header_t *h; dns_query_t *qp; dns_rr_t *rr; u8 *curpos; u8 *pos, *pos2; u8 *cname_pos = 0; int len, i; u8 *cname = 0; u8 *request = 0; u8 *name_copy; u32 qp_offset; u16 flags; u16 rcode; dns_cache_entry_t *ep, *next_ep; f64 now; h = (dns_header_t *) reply; flags = clib_net_to_host_u16 (h->flags); rcode = flags & DNS_RCODE_MASK; /* See if the response is OK */ switch (rcode) { case DNS_RCODE_NO_ERROR: break; case DNS_RCODE_NAME_ERROR: case DNS_RCODE_FORMAT_ERROR: case DNS_RCODE_SERVER_FAILURE: case DNS_RCODE_NOT_IMPLEMENTED: case DNS_RCODE_REFUSED: return -1; } curpos = (u8 *) (h + 1); pos = curpos; len = *pos++; /* Skip the questions */ for (i = 0; i < clib_net_to_host_u16 (h->qdcount); i++) { while (len) { pos += len; len = *pos++; } pos += sizeof (dns_query_t); } pos2 = pos; /* expect a pointer chase here for a CNAME record */ if ((pos2[0] & 0xC0) == 0xC0) pos += 2; else return 0; /* Walk the answer(s) to see what to do next */ for (i = 0; i < clib_net_to_host_u16 (h->anscount); i++) { rr = (dns_rr_t *) pos; switch (clib_net_to_host_u16 (rr->type)) { /* Real address record? Done.. */ case DNS_TYPE_A: case DNS_TYPE_AAAA: return 0; /* * Maybe chase a CNAME pointer? * It's not unheard-of for name-servers to return * both CNAME and A/AAAA records... */ case DNS_TYPE_CNAME: cname_pos = pos; break; /* Some other junk, e.g. a nameserver... */ default: break; } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); /* Skip name... */ if ((pos2[0] & 0xc0) == 0xc0) pos += 2; } /* Neither a CNAME nor a real address. Try another server */ if (cname_pos == 0) { flags &= ~DNS_RCODE_MASK; flags |= DNS_RCODE_NAME_ERROR; h->flags = clib_host_to_net_u16 (flags); return -1; } /* This is a CNAME record, chase the name chain. */ pos = cname_pos; /* The last request is no longer pending.. */ for (i = 0; i < vec_len (dm->unresolved_entries); i++) if (ep_index == dm->unresolved_entries[i]) { vec_delete (dm->unresolved_entries, 1, i); goto found_last_request; } clib_warning ("pool elt %d supposedly pending, but not found...", ep_index); found_last_request: now = vlib_time_now (dm->vlib_main); cname = vnet_dns_labels_to_name (rr->rdata, reply, &pos2); /* Save the cname */ vec_add1 (cname, 0); _vec_len (cname) -= 1; ep = pool_elt_at_index (dm->entries, ep_index); ep->cname = cname; ep->flags |= (DNS_CACHE_ENTRY_FLAG_CNAME | DNS_CACHE_ENTRY_FLAG_VALID); /* Save the response */ if (ep->dns_response) vec_free (ep->dns_response); ep->dns_response = reply; /* Set up expiration time */ ep->expiration_time = now + clib_net_to_host_u32 (rr->ttl); pool_get (dm->entries, next_ep); /* Need to recompute ep post pool-get */ ep = pool_elt_at_index (dm->entries, ep_index); clib_memset (next_ep, 0, sizeof (*next_ep)); next_ep->name = vec_dup (cname); vec_add1 (next_ep->name, 0); _vec_len (next_ep->name) -= 1; hash_set_mem (dm->cache_entry_by_name, next_ep->name, next_ep - dm->entries); /* Use the same server */ next_ep->server_rotor = ep->server_rotor; next_ep->server_af = ep->server_af; /* Move notification data to the next name in the chain */ #define _(a) next_ep->a = ep->a; ep->a = 0; foreach_notification_to_move; #undef _ request = name_to_labels (cname); name_copy = vec_dup (request); qp_offset = vec_len (request); /* Add space for the query header */ vec_validate (request, 2 * qp_offset + 2 * sizeof (dns_query_t) - 1); qp = (dns_query_t *) (request + qp_offset); qp->type = clib_host_to_net_u16 (DNS_TYPE_A); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); clib_memcpy (qp, name_copy, vec_len (name_copy)); qp = (dns_query_t *) (((u8 *) qp) + vec_len (name_copy)); vec_free (name_copy); qp->type = clib_host_to_net_u16 (DNS_TYPE_AAAA); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); /* Punch in space for the dns_header_t */ vec_insert (request, sizeof (dns_header_t), 0); h = (dns_header_t *) request; /* Transaction ID = pool index */ h->id = clib_host_to_net_u16 (next_ep - dm->entries); /* Ask for a recursive lookup */ h->flags = clib_host_to_net_u16 (DNS_RD | DNS_OPCODE_QUERY); h->qdcount = clib_host_to_net_u16 (2); h->nscount = 0; h->arcount = 0; next_ep->dns_request = request; next_ep->retry_timer = now + 2.0; next_ep->retry_count = 0; /* * Enable this to watch recursive resolution happen... * fformat (stdout, "%U", format_dns_reply, request, 2); */ vec_add1 (dm->unresolved_entries, next_ep - dm->entries); vnet_send_dns_request (dm, next_ep); return (1); } int vnet_dns_response_to_reply (u8 * response, vl_api_dns_resolve_name_reply_t * rmp, u32 * min_ttlp) { dns_header_t *h; dns_query_t *qp; dns_rr_t *rr; int i, limit; u8 len; u8 *curpos, *pos, *pos2; u16 flags; u16 rcode; u32 ttl; int pointer_chase; h = (dns_header_t *) response; flags = clib_net_to_host_u16 (h->flags); rcode = flags & DNS_RCODE_MASK; /* See if the response is OK, etc. */ switch (rcode) { default: case DNS_RCODE_NO_ERROR: break; case DNS_RCODE_NAME_ERROR: case DNS_RCODE_FORMAT_ERROR: return VNET_API_ERROR_NAME_SERVER_NO_SUCH_NAME; case DNS_RCODE_SERVER_FAILURE: case DNS_RCODE_NOT_IMPLEMENTED: case DNS_RCODE_REFUSED: return VNET_API_ERROR_NAME_SERVER_NEXT_SERVER; } /* No answers? Loser... */ if (clib_net_to_host_u16 (h->anscount) < 1) return VNET_API_ERROR_NAME_SERVER_NO_ADDRESSES; curpos = (u8 *) (h + 1); /* Skip the name we asked about */ pos = curpos; len = *pos++; /* Should never happen, but stil... */ if ((len & 0xC0) == 0xC0) curpos += 2; else { /* skip the name / label-set */ while (len) { pos += len; len = *pos++; } curpos = pos; } /* Skip queries */ limit = clib_net_to_host_u16 (h->qdcount); qp = (dns_query_t *) curpos; qp += limit; curpos = (u8 *) qp; /* Parse answers */ limit = clib_net_to_host_u16 (h->anscount); for (i = 0; i < limit; i++) { pos = pos2 = curpos; pointer_chase = 0; /* Expect pointer chases in the answer section... */ if ((pos2[0] & 0xC0) == 0xC0) { pos = pos2 + 2; pos2 = response + ((pos2[0] & 0x3f) << 8) + pos2[1]; pointer_chase = 1; } len = *pos2++; while (len) { pos2 += len; if ((pos2[0] & 0xc0) == 0xc0) { /* * If we've already done one pointer chase, * do not move the pos pointer. */ if (pointer_chase == 0) pos = pos2 + 2; pos2 = response + ((pos2[0] & 0x3f) << 8) + pos2[1]; len = *pos2++; pointer_chase = 1; } else len = *pos2++; } if (pointer_chase == 0) pos = pos2; rr = (dns_rr_t *) pos; switch (clib_net_to_host_u16 (rr->type)) { case DNS_TYPE_A: /* Collect an ip4 address. Do not pass go. Do not collect $200 */ memcpy (rmp->ip4_address, rr->rdata, sizeof (ip4_address_t)); rmp->ip4_set = 1; ttl = clib_net_to_host_u32 (rr->ttl); if (min_ttlp && *min_ttlp > ttl) *min_ttlp = ttl; break; case DNS_TYPE_AAAA: /* Collect an ip6 address. Do not pass go. Do not collect $200 */ memcpy (rmp->ip6_address, rr->rdata, sizeof (ip6_address_t)); ttl = clib_net_to_host_u32 (rr->ttl); if (min_ttlp && *min_ttlp > ttl) *min_ttlp = ttl; rmp->ip6_set = 1; break; default: break; } /* Might as well stop ASAP */ if (rmp->ip4_set && rmp->ip6_set) break; pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); curpos = pos; } if ((rmp->ip4_set + rmp->ip6_set) == 0) return VNET_API_ERROR_NAME_SERVER_NO_ADDRESSES; return 0; } int vnet_dns_response_to_name (u8 * response, vl_api_dns_resolve_ip_reply_t * rmp, u32 * min_ttlp) { dns_header_t *h; dns_query_t *qp; dns_rr_t *rr; int i, limit; u8 len; u8 *curpos, *pos, *pos2; u16 flags; u16 rcode; u8 *name; u32 ttl; u8 *junk __attribute__ ((unused)); int name_set = 0; int pointer_chase; h = (dns_header_t *) response; flags = clib_net_to_host_u16 (h->flags); rcode = flags & DNS_RCODE_MASK; /* See if the response is OK, etc. */ switch (rcode) { default: case DNS_RCODE_NO_ERROR: break; case DNS_RCODE_NAME_ERROR: case DNS_RCODE_FORMAT_ERROR: return VNET_API_ERROR_NAME_SERVER_NO_SUCH_NAME; case DNS_RCODE_SERVER_FAILURE: case DNS_RCODE_NOT_IMPLEMENTED: case DNS_RCODE_REFUSED: return VNET_API_ERROR_NAME_SERVER_NEXT_SERVER; } /* No answers? Loser... */ if (clib_net_to_host_u16 (h->anscount) < 1) return VNET_API_ERROR_NAME_SERVER_NO_ADDRESSES; curpos = (u8 *) (h + 1); /* Skip the name we asked about */ pos = curpos; len = *pos++; /* Should never happen, but stil... */ if ((len & 0xC0) == 0xC0) curpos += 2; else { /* skip the name / label-set */ while (len) { pos += len; len = *pos++; } curpos = pos; } /* Skip queries */ limit = clib_net_to_host_u16 (h->qdcount); qp = (dns_query_t *) curpos; qp += limit; curpos = (u8 *) qp; /* Parse answers */ limit = clib_net_to_host_u16 (h->anscount); for (i = 0; i < limit; i++) { pos = pos2 = curpos; pointer_chase = 0; /* Expect pointer chases in the answer section... */ if ((pos2[0] & 0xC0) == 0xC0) { pos = pos2 + 2; pos2 = response + ((pos2[0] & 0x3f) << 8) + pos2[1]; pointer_chase = 1; } len = *pos2++; while (len) { pos2 += len; if ((pos2[0] & 0xc0) == 0xc0) { /* * If we've already done one pointer chase, * do not move the pos pointer. */ if (pointer_chase == 0) pos = pos2 + 2; pos2 = response + ((pos2[0] & 0x3f) << 8) + pos2[1]; len = *pos2++; pointer_chase = 1; } else len = *pos2++; } if (pointer_chase == 0) pos = pos2; rr = (dns_rr_t *) pos; switch (clib_net_to_host_u16 (rr->type)) { case DNS_TYPE_PTR: name = vnet_dns_labels_to_name (rr->rdata, response, &junk); memcpy (rmp->name, name, vec_len (name)); ttl = clib_net_to_host_u32 (rr->ttl); if (min_ttlp) *min_ttlp = ttl; rmp->name[vec_len (name)] = 0; name_set = 1; break; default: break; } /* Might as well stop ASAP */ if (name_set == 1) break; pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); curpos = pos; } if (name_set == 0) return VNET_API_ERROR_NAME_SERVER_NO_SUCH_NAME; return 0; } static void vl_api_dns_resolve_name_t_handler (vl_api_dns_resolve_name_t * mp) { dns_main_t *dm = &dns_main; vl_api_dns_resolve_name_reply_t *rmp; dns_cache_entry_t *ep; dns_pending_request_t _t0, *t0 = &_t0; int rv; /* Sanitize the name slightly */ mp->name[ARRAY_LEN (mp->name) - 1] = 0; t0->request_type = DNS_API_PENDING_NAME_TO_IP; t0->client_index = mp->client_index; t0->client_context = mp->context; rv = vnet_dns_resolve_name (dm, mp->name, t0, &ep); /* Error, e.g. not enabled? Tell the user */ if (rv < 0) { REPLY_MACRO (VL_API_DNS_RESOLVE_NAME_REPLY); return; } /* Resolution pending? Don't reply... */ if (ep == 0) return; /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_DNS_RESOLVE_NAME_REPLY, ({ rv = vnet_dns_response_to_reply (ep->dns_response, rmp, 0 /* ttl-ptr */); rmp->retval = clib_host_to_net_u32 (rv); })); /* *INDENT-ON* */ /* * dns_resolve_name leaves the cache locked when it returns * a cached result, so unlock it here. */ dns_cache_unlock (dm); } static void vl_api_dns_resolve_ip_t_handler (vl_api_dns_resolve_ip_t * mp) { dns_main_t *dm = &dns_main; vl_api_dns_resolve_ip_reply_t *rmp; dns_cache_entry_t *ep; int rv; int i, len; u8 *lookup_name = 0; u8 digit, nybble; dns_pending_request_t _t0, *t0 = &_t0; if (mp->is_ip6) { for (i = 15; i >= 0; i--) { digit = mp->address[i]; nybble = (digit & 0x0F); if (nybble > 9) vec_add1 (lookup_name, (nybble - 10) + 'a'); else vec_add1 (lookup_name, nybble + '0'); vec_add1 (lookup_name, '.'); nybble = (digit & 0xF0) >> 4; if (nybble > 9) vec_add1 (lookup_name, (nybble - 10) + 'a'); else vec_add1 (lookup_name, nybble + '0'); vec_add1 (lookup_name, '.'); } len = vec_len (lookup_name); vec_validate (lookup_name, len + 8); memcpy (lookup_name + len, "ip6.arpa", 8); } else { for (i = 3; i >= 0; i--) { digit = mp->address[i]; lookup_name = format (lookup_name, "%d.", digit); } lookup_name = format (lookup_name, "in-addr.arpa"); } vec_add1 (lookup_name, 0); t0->request_type = DNS_API_PENDING_IP_TO_NAME; t0->client_index = mp->client_index; t0->client_context = mp->context; rv = vnet_dns_resolve_name (dm, lookup_name, t0, &ep); vec_free (lookup_name); /* Error, e.g. not enabled? Tell the user */ if (rv < 0) { REPLY_MACRO (VL_API_DNS_RESOLVE_IP_REPLY); return; } /* Resolution pending? Don't reply... */ if (ep == 0) return; /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_DNS_RESOLVE_IP_REPLY, ({ rv = vnet_dns_response_to_name (ep->dns_response, rmp, 0 /* ttl-ptr */); rmp->retval = clib_host_to_net_u32 (rv); })); /* *INDENT-ON* */ /* * vnet_dns_resolve_name leaves the cache locked when it returns * a cached result, so unlock it here. */ dns_cache_unlock (dm); } #define vl_msg_name_crc_list #include <vpp/api/vpe_all_api_h.h> #undef vl_msg_name_crc_list static void setup_message_id_table (api_main_t * am) { #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); foreach_vl_msg_name_crc_dns; #undef _ } #define foreach_dns_api_msg \ _(DNS_ENABLE_DISABLE, dns_enable_disable) \ _(DNS_NAME_SERVER_ADD_DEL, dns_name_server_add_del) \ _(DNS_RESOLVE_NAME, dns_resolve_name) \ _(DNS_RESOLVE_IP, dns_resolve_ip) static clib_error_t * dns_api_hookup (vlib_main_t * vm) { #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_dns_api_msg; #undef _ setup_message_id_table (&api_main); return 0; } VLIB_API_INIT_FUNCTION (dns_api_hookup); static clib_error_t * dns_config_fn (vlib_main_t * vm, unformat_input_t * input) { dns_main_t *dm = &dns_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "max-cache-size %u", &dm->name_cache_size)) ; else if (unformat (input, "max-ttl %u", &dm->max_ttl_in_seconds)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; } VLIB_CONFIG_FUNCTION (dns_config_fn, "dns"); static clib_error_t * dns_init (vlib_main_t * vm) { dns_main_t *dm = &dns_main; dm->vlib_main = vm; dm->vnet_main = vnet_get_main (); dm->name_cache_size = 65535; dm->max_ttl_in_seconds = 86400; dm->random_seed = 0xDEADDABE; return 0; } VLIB_INIT_FUNCTION (dns_init); uword unformat_dns_reply (unformat_input_t * input, va_list * args) { u8 **result = va_arg (*args, u8 **); u8 **namep = va_arg (*args, u8 **); ip4_address_t a4; ip6_address_t a6; int a4_set = 0; int a6_set = 0; u8 *name; int name_set = 0; u8 *ce; u32 qp_offset; dns_header_t *h; dns_query_t *qp; dns_rr_t *rr; u8 *rru8; if (unformat (input, "%v", &name)) name_set = 1; if (unformat (input, "%U", unformat_ip4_address, &a4)) { a4_set = 1; if (unformat (input, "%U", unformat_ip6_address, &a6)) a6_set = 1; } if (unformat (input, "%U", unformat_ip6_address, &a6)) { a6_set = 1; if (unformat (input, "%U", unformat_ip4_address, &a6)) a4_set = 1; } /* Must have a name */ if (!name_set) return 0; /* Must have at least one address */ if (!(a4_set + a6_set)) return 0; /* Build a fake DNS cache entry string, one hemorrhoid at a time */ ce = name_to_labels (name); qp_offset = vec_len (ce); /* Add space for the query header */ vec_validate (ce, qp_offset + sizeof (dns_query_t) - 1); qp = (dns_query_t *) (ce + qp_offset); qp->type = clib_host_to_net_u16 (DNS_TYPE_ALL); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); /* Punch in space for the dns_header_t */ vec_insert (ce, sizeof (dns_header_t), 0); h = (dns_header_t *) ce; /* Fake Transaction ID */ h->id = 0xFFFF; h->flags = clib_host_to_net_u16 (DNS_RD | DNS_RA); h->qdcount = clib_host_to_net_u16 (1); h->anscount = clib_host_to_net_u16 (a4_set + a6_set); h->nscount = 0; h->arcount = 0; /* Now append one or two A/AAAA RR's... */ if (a4_set) { /* Pointer to the name (DGMS) */ vec_add1 (ce, 0xC0); vec_add1 (ce, 0x0C); vec_add2 (ce, rru8, sizeof (*rr) + 4); rr = (void *) rru8; rr->type = clib_host_to_net_u16 (DNS_TYPE_A); rr->class = clib_host_to_net_u16 (DNS_CLASS_IN); rr->ttl = clib_host_to_net_u32 (86400); rr->rdlength = clib_host_to_net_u16 (4); memcpy (rr->rdata, &a4, sizeof (a4)); } if (a6_set) { /* Pointer to the name (DGMS) */ vec_add1 (ce, 0xC0); vec_add1 (ce, 0x0C); vec_add2 (ce, rru8, sizeof (*rr) + 16); rr = (void *) rru8; rr->type = clib_host_to_net_u16 (DNS_TYPE_AAAA); rr->class = clib_host_to_net_u16 (DNS_CLASS_IN); rr->ttl = clib_host_to_net_u32 (86400); rr->rdlength = clib_host_to_net_u16 (16); memcpy (rr->rdata, &a6, sizeof (a6)); } *result = ce; if (namep) *namep = name; else vec_free (name); return 1; } u8 * format_dns_query (u8 * s, va_list * args) { u8 **curpos = va_arg (*args, u8 **); int verbose = va_arg (*args, int); u8 *pos; dns_query_t *qp; int len, i; if (verbose > 1) s = format (s, " Name: "); /* Unwind execrated counted-label sheit */ pos = *curpos; len = *pos++; while (len) { for (i = 0; i < len; i++) vec_add1 (s, *pos++); len = *pos++; if (len) vec_add1 (s, '.'); else { vec_add1 (s, ':'); vec_add1 (s, ' '); } } qp = (dns_query_t *) pos; if (verbose > 1) { switch (clib_net_to_host_u16 (qp->type)) { case DNS_TYPE_A: s = format (s, "type A\n"); break; case DNS_TYPE_AAAA: s = format (s, "type AAAA\n"); break; case DNS_TYPE_ALL: s = format (s, "type ALL\n"); break; default: s = format (s, "type %d\n", clib_net_to_host_u16 (qp->type)); break; } } pos += sizeof (*qp); *curpos = pos; return s; } /** * format dns reply data * verbose > 1, dump everything * verbose == 1, dump all A and AAAA records * verbose == 0, dump one A record, and one AAAA record */ u8 * format_dns_reply_data (u8 * s, va_list * args) { u8 *reply = va_arg (*args, u8 *); u8 **curpos = va_arg (*args, u8 **); int verbose = va_arg (*args, int); int *print_ip4 = va_arg (*args, int *); int *print_ip6 = va_arg (*args, int *); int len; u8 *pos, *pos2; dns_rr_t *rr; int i; int pointer_chase = 0; u16 *tp; u16 rrtype_host_byte_order; pos = pos2 = *curpos; if (verbose > 1) s = format (s, " "); /* chase pointer? almost always yes here... */ if ((pos2[0] & 0xc0) == 0xc0) { pos = pos2 + 2; pos2 = reply + ((pos2[0] & 0x3f) << 8) + pos2[1]; pointer_chase = 1; } len = *pos2++; while (len) { for (i = 0; i < len; i++) { if (verbose > 1) vec_add1 (s, *pos2); pos2++; } if ((pos2[0] & 0xc0) == 0xc0) { /* * If we've already done one pointer chase, * do not move the pos pointer. */ if (pointer_chase == 0) pos = pos2 + 2; pos2 = reply + ((pos2[0] & 0x3f) << 8) + pos2[1]; len = *pos2++; pointer_chase = 1; } else len = *pos2++; if (len) { if (verbose > 1) vec_add1 (s, '.'); } else { if (verbose > 1) vec_add1 (s, ' '); } } if (pointer_chase == 0) pos = pos2; rr = (dns_rr_t *) pos; rrtype_host_byte_order = clib_net_to_host_u16 (rr->type); switch (rrtype_host_byte_order) { case DNS_TYPE_A: if (verbose > 1) { s = format (s, "A: ttl %d %U\n", clib_net_to_host_u32 (rr->ttl), format_ip4_address, rr->rdata); } else { if (*print_ip4) s = format (s, "%U [%u] ", format_ip4_address, rr->rdata, clib_net_to_host_u32 (rr->ttl)); if (verbose == 0) *print_ip4 = 0; } pos += sizeof (*rr) + 4; break; case DNS_TYPE_AAAA: if (verbose > 1) { s = format (s, "AAAA: ttl %d %U\n", clib_net_to_host_u32 (rr->ttl), format_ip6_address, rr->rdata); } else { if (*print_ip6) s = format (s, "%U [%u] ", format_ip6_address, rr->rdata, clib_net_to_host_u32 (rr->ttl)); if (verbose == 0) *print_ip6 = 0; } pos += sizeof (*rr) + 16; break; case DNS_TYPE_TEXT: if (verbose > 1) { s = format (s, "TEXT: "); for (i = 0; i < clib_net_to_host_u16 (rr->rdlength); i++) vec_add1 (s, rr->rdata[i]); vec_add1 (s, '\n'); } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; case DNS_TYPE_HINFO: { /* Two counted strings. DGMS */ u8 *len; u8 *curpos; int i; if (verbose > 1) { s = format (s, "HINFO: "); len = rr->rdata; curpos = len + 1; for (i = 0; i < *len; i++) vec_add1 (s, *curpos++); vec_add1 (s, ' '); len = curpos++; for (i = 0; i < *len; i++) vec_add1 (s, *curpos++); vec_add1 (s, '\n'); } } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; case DNS_TYPE_NAMESERVER: if (verbose > 1) { s = format (s, "Nameserver: "); pos2 = rr->rdata; /* chase pointer? */ if ((pos2[0] & 0xc0) == 0xc0) { pos = pos2 + 2; pos2 = reply + ((pos2[0] & 0x3f) << 8) + pos2[1]; } len = *pos2++; while (len) { for (i = 0; i < len; i++) vec_add1 (s, *pos2++); /* chase pointer, typically to offset 12... */ if (pos2[0] == 0xC0) pos2 = reply + pos2[1]; len = *pos2++; if (len) vec_add1 (s, '.'); else vec_add1 (s, '\n'); } } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; case DNS_TYPE_MAIL_EXCHANGE: if (verbose > 1) { tp = (u16 *) rr->rdata; s = format (s, "Mail Exchange: Preference %d ", (u32) clib_net_to_host_u16 (*tp)); pos2 = rr->rdata + 2; /* chase pointer? */ if (pos2[0] == 0xc0) pos2 = reply + pos2[1]; len = *pos2++; while (len) { for (i = 0; i < len; i++) vec_add1 (s, *pos2++); /* chase pointer */ if (pos2[0] == 0xC0) pos2 = reply + pos2[1]; len = *pos2++; if (len) vec_add1 (s, '.'); else vec_add1 (s, '\n'); } } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; case DNS_TYPE_PTR: case DNS_TYPE_CNAME: if (verbose > 1) { tp = (u16 *) rr->rdata; if (rrtype_host_byte_order == DNS_TYPE_CNAME) s = format (s, "CNAME: "); else s = format (s, "PTR: "); pos2 = rr->rdata; /* chase pointer? */ if (pos2[0] == 0xc0) pos2 = reply + pos2[1]; len = *pos2++; while (len) { for (i = 0; i < len; i++) vec_add1 (s, *pos2++); /* chase pointer */ if (pos2[0] == 0xC0) pos2 = reply + pos2[1]; len = *pos2++; if (len) vec_add1 (s, '.'); else vec_add1 (s, '\n'); } } pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; default: if (verbose > 1) s = format (s, "type %d: len %d\n", (int) clib_net_to_host_u16 (rr->type), sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength)); pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength); break; } *curpos = pos; return s; } u8 * format_dns_reply (u8 * s, va_list * args) { u8 *reply_as_u8 = va_arg (*args, u8 *); int verbose = va_arg (*args, int); dns_header_t *h; u16 id, flags; u8 *curpos; int i; int print_ip4 = 1; int print_ip6 = 1; h = (dns_header_t *) reply_as_u8; id = clib_net_to_host_u16 (h->id); flags = clib_net_to_host_u16 (h->flags); if (verbose > 1) { s = format (s, "DNS %s: id %d\n", (flags & DNS_QR) ? "reply" : "query", id); s = format (s, " %s %s %s %s\n", (flags & DNS_RA) ? "recur" : "no-recur", (flags & DNS_RD) ? "recur-des" : "no-recur-des", (flags & DNS_TC) ? "trunc" : "no-trunc", (flags & DNS_AA) ? "auth" : "non-auth"); s = format (s, " %d queries, %d answers, %d name-servers," " %d add'l recs\n", clib_net_to_host_u16 (h->qdcount), clib_net_to_host_u16 (h->anscount), clib_net_to_host_u16 (h->nscount), clib_net_to_host_u16 (h->arcount)); } curpos = (u8 *) (h + 1); if (h->qdcount) { if (verbose > 1) s = format (s, " Queries:\n"); for (i = 0; i < clib_net_to_host_u16 (h->qdcount); i++) { /* The query is variable-length, so curpos is a value-result parm */ s = format (s, "%U", format_dns_query, &curpos, verbose); } } if (h->anscount) { if (verbose > 1) s = format (s, " Replies:\n"); for (i = 0; i < clib_net_to_host_u16 (h->anscount); i++) { /* curpos is a value-result parm */ s = format (s, "%U", format_dns_reply_data, reply_as_u8, &curpos, verbose, &print_ip4, &print_ip6); } } return s; } u8 * format_dns_cache (u8 * s, va_list * args) { dns_main_t *dm = va_arg (*args, dns_main_t *); f64 now = va_arg (*args, f64); int verbose = va_arg (*args, int); u8 *name = va_arg (*args, u8 *); dns_cache_entry_t *ep; char *ss; uword *p; if (dm->is_enabled == 0) { s = format (s, "The DNS cache is disabled..."); return s; } if (pool_elts (dm->entries) == 0) { s = format (s, "The DNS cache is empty..."); return s; } dns_cache_lock (dm); if (name) { p = hash_get_mem (dm->cache_entry_by_name, name); if (!p) { s = format (s, "%s is not in the cache...", name); dns_cache_unlock (dm); return (s); } ep = pool_elt_at_index (dm->entries, p[0]); /* Magic to spit out a C-initializer to research hemorrhoids... */ if (verbose == 3) { int i, j; s = format (s, "static u8 dns_reply_data_initializer[] =\n"); s = format (s, "{\n"); j = 0; for (i = 0; i < vec_len (ep->dns_response); i++) { if (j++ == 8) { j = 0; vec_add1 (s, '\n'); } s = format (s, "0x%02x, ", ep->dns_response[i]); } s = format (s, "};\n"); } else { if (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) { ASSERT (ep->dns_response); if (ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC) ss = "[S] "; else ss = " "; if (verbose < 2 && ep->flags & DNS_CACHE_ENTRY_FLAG_CNAME) s = format (s, "%s%s -> %s", ss, ep->name, ep->cname); else s = format (s, "%s%s -> %U", ss, ep->name, format_dns_reply, ep->dns_response, verbose); if (!(ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC)) { f64 time_left = ep->expiration_time - now; if (time_left > 0.0) s = format (s, " TTL left %.1f", time_left); else s = format (s, " EXPIRED"); } } else { ASSERT (ep->dns_request); s = format (s, "[P] %U", format_dns_reply, ep->dns_request, verbose); } vec_add1 (s, '\n'); } return s; } /* *INDENT-OFF* */ pool_foreach (ep, dm->entries, ({ if (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) { ASSERT (ep->dns_response); if (ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC) ss = "[S] "; else ss = " "; if (verbose < 2 && ep->flags & DNS_CACHE_ENTRY_FLAG_CNAME) s = format (s, "%s%s -> %s", ss, ep->name, ep->cname); else s = format (s, "%s%s -> %U", ss, ep->name, format_dns_reply, ep->dns_response, verbose); if (!(ep->flags & DNS_CACHE_ENTRY_FLAG_STATIC)) { f64 time_left = ep->expiration_time - now; if (time_left > 0.0) s = format (s, " TTL left %.1f", time_left); else s = format (s, " EXPIRED"); if (verbose > 2) s = format (s, " %d client notifications pending\n", vec_len(ep->pending_requests)); } } else { ASSERT (ep->dns_request); s = format (s, "[P] %U", format_dns_reply, ep->dns_request, verbose); } vec_add1 (s, '\n'); })); /* *INDENT-ON* */ dns_cache_unlock (dm); return s; } static clib_error_t * show_dns_cache_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { dns_main_t *dm = &dns_main; int verbose = 0; u8 *name = 0; f64 now = vlib_time_now (vm); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose %d", &verbose)) ; else if (unformat (input, "verbose")) verbose = 1; else if (unformat (input, "name %s", &name)) ; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } vlib_cli_output (vm, "%U", format_dns_cache, dm, now, verbose, name); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_dns_cache_command) = { .path = "show dns cache", .short_help = "show dns cache [verbose [nn]]", .function = show_dns_cache_command_fn, }; /* *INDENT-ON* */ static clib_error_t * dns_cache_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { dns_main_t *dm = &dns_main; u8 *dns_reply_data; u8 *name; int is_add = -1; int is_clear = -1; int rv; clib_error_t *error; if (unformat (input, "add")) is_add = 1; if (unformat (input, "del")) is_add = 0; if (unformat (input, "clear")) is_clear = 1; if (is_add == -1 && is_clear == -1) return clib_error_return (0, "add / del / clear required..."); if (is_clear == 1) { rv = dns_cache_clear (dm); switch (rv) { case 0: return 0; case VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED: error = clib_error_return (0, "Name resolution not enabled"); return error; } } /* Delete (by name)? */ if (is_add == 0) { if (unformat (input, "%v", &name)) { rv = dns_delete_by_name (dm, name); switch (rv) { case VNET_API_ERROR_NO_SUCH_ENTRY: error = clib_error_return (0, "%v not in the cache...", name); vec_free (name); return error; case VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED: error = clib_error_return (0, "Name resolution not enabled"); vec_free (name); return error; case 0: vec_free (name); return 0; default: error = clib_error_return (0, "dns_delete_by_name returned %d", rv); vec_free (name); return error; } } return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } /* Note: dns_add_static_entry consumes the name vector if OK... */ if (unformat (input, "%U", unformat_dns_reply, &dns_reply_data, &name)) { rv = dns_add_static_entry (dm, name, dns_reply_data); switch (rv) { case VNET_API_ERROR_ENTRY_ALREADY_EXISTS: vec_free (name); vec_free (dns_reply_data); return clib_error_return (0, "%v already in the cache...", name); case 0: return 0; default: return clib_error_return (0, "dns_add_static_entry returned %d", rv); } } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (dns_cache_add_del_command) = { .path = "dns cache", .short_help = "dns cache [add|del|clear] <name> [ip4][ip6]", .function = dns_cache_add_del_command_fn, }; /* *INDENT-ON* */ #define DNS_FORMAT_TEST 1 #if DNS_FORMAT_TEST > 0 #if 0 /* yahoo.com */ static u8 dns_reply_data_initializer[] = { 0x0, 0x0, 0x81, 0x80, 0x0, 0x1, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x5, 0x79, 0x61, 0x68, 0x6f, 0x6f, 0x3, 0x63, 0x6f, 0x6d, 0x0, /* null lbl */ 0x0, 0xff, /* type ALL */ 0x0, 0x1, /* class IN */ 0xc0, 0xc, /* pointer to yahoo.com name */ 0x0, 0x10, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x24, 0x23, 0x76, 0x3d, 0x73, 0x70, 0x66, 0x31, 0x20, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x3d, 0x5f, 0x73, 0x70, 0x66, 0x2e, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x79, 0x61, 0x68, 0x6f, 0x6f, 0x2e, 0x63, 0x6f, 0x6d, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x50, 0xd4, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x35, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x50, 0xd4, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x34, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x50, 0xd4, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x31, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x50, 0xd4, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x32, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x50, 0xd4, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x33, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x19, 0x0, 0x1, 0x4, 0x6d, 0x74, 0x61, 0x36, 0x3, 0x61, 0x6d, 0x30, 0x8, 0x79, 0x61, 0x68, 0x6f, 0x6f, 0x64, 0x6e, 0x73, 0x3, 0x6e, 0x65, 0x74, 0x0, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x9, 0x0, 0x1, 0x4, 0x6d, 0x74, 0x61, 0x37, 0xc0, 0xb8, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x9, 0x0, 0x1, 0x4, 0x6d, 0x74, 0x61, 0x35, 0xc0, 0xb8, 0xc0, 0xc, 0x0, 0x1c, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x10, 0x20, 0x1, 0x49, 0x98, 0x0, 0x44, 0x2, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa7, 0xc0, 0xc, 0x0, 0x1c, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x10, 0x20, 0x1, 0x49, 0x98, 0x0, 0xc, 0xa, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x40, 0x8, 0xc0, 0xc, 0x0, 0x1c, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x10, 0x20, 0x1, 0x49, 0x98, 0x0, 0x58, 0xc, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa9, 0xc0, 0xc, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x4, 0x62, 0x8a, 0xfd, 0x6d, 0xc0, 0xc, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x4, 0xce, 0xbe, 0x24, 0x2d, 0xc0, 0xc, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x4, 0x62, 0x8b, 0xb4, 0x95, 0xc0, 0xc, 0x0, 0x6, 0x0, 0x1, 0x0, 0x0, 0x6, 0x5c, 0x0, 0x2d, 0xc0, 0x7b, 0xa, 0x68, 0x6f, 0x73, 0x74, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x9, 0x79, 0x61, 0x68, 0x6f, 0x6f, 0x2d, 0x69, 0x6e, 0x63, 0xc0, 0x12, 0x78, 0x3a, 0x85, 0x44, 0x0, 0x0, 0xe, 0x10, 0x0, 0x0, 0x1, 0x2c, 0x0, 0x1b, 0xaf, 0x80, 0x0, 0x0, 0x2, 0x58 }; /* www.cisco.com, has no addresses in reply */ static u8 dns_reply_data_initializer[] = { 0x00, 0x01, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77, 0x05, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0xff, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x0b, 0xd3, 0x00, 0x1a, 0x03, 0x77, 0x77, 0x77, 0x05, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x03, 0x63, 0x6f, 0x6d, 0x06, 0x61, 0x6b, 0x61, 0x64, 0x6e, 0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, }; /* bind8 (linux widget, w/ nasty double pointer chasees */ static u8 dns_reply_data_initializer[] = { /* 0 */ 0x00, 0x01, 0x81, 0x80, 0x00, 0x01, 0x00, 0x08, /* 8 */ 0x00, 0x06, 0x00, 0x06, 0x0a, 0x6f, 0x72, 0x69, /* 16 */ 0x67, 0x69, 0x6e, 0x2d, 0x77, 0x77, 0x77, 0x05, /* 24 */ 0x63, 0x69, 0x73, 0x63, 0x6f, 0x03, 0x63, 0x6f, /* 32 */ 0x6d, 0x00, 0x00, 0xff, 0x00, 0x01, 0x0a, 0x6f, /* 40 */ 0x72, 0x69, 0x67, 0x69, 0x6e, 0x2d, 0x77, 0x77, /* 48 */ 0x77, 0x05, 0x43, 0x49, 0x53, 0x43, 0x4f, 0xc0, /* 56 */ 0x1d, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, /* 64 */ 0x9a, 0x00, 0x18, 0x15, 0x72, 0x63, 0x64, 0x6e, 0x39, 0x2d, 0x31, 0x34, 0x70, 0x2d, 0x64, 0x63, 0x7a, 0x30, 0x35, 0x6e, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x1a, 0x17, 0x61, 0x6c, 0x6c, 0x6e, 0x30, 0x31, 0x2d, 0x61, 0x67, 0x30, 0x39, 0x2d, 0x64, 0x63, 0x7a, 0x30, 0x33, 0x6e, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x10, 0x0d, 0x72, 0x74, 0x70, 0x35, 0x2d, 0x64, 0x6d, 0x7a, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x18, 0x15, 0x6d, 0x74, 0x76, 0x35, 0x2d, 0x61, 0x70, 0x31, 0x30, 0x2d, 0x64, 0x63, 0x7a, 0x30, 0x36, 0x6e, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x1b, 0x18, 0x73, 0x6e, 0x67, 0x64, 0x63, 0x30, 0x31, 0x2d, 0x61, 0x62, 0x30, 0x37, 0x2d, 0x64, 0x63, 0x7a, 0x30, 0x31, 0x6e, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x1a, 0x17, 0x61, 0x65, 0x72, 0x30, 0x31, 0x2d, 0x72, 0x34, 0x63, 0x32, 0x35, 0x2d, 0x64, 0x63, 0x7a, 0x30, 0x31, 0x6e, 0x2d, 0x67, 0x73, 0x73, 0x31, 0xc0, 0x17, 0xc0, 0x26, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x81, 0x00, 0x04, 0x48, 0xa3, 0x04, 0xa1, 0xc0, 0x26, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x82, 0x00, 0x10, 0x20, 0x01, 0x04, 0x20, 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0xf4, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0xcd, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0x8d, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0x43, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0xa9, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x05, 0x9a, 0x00, 0x02, 0xc0, 0x67, 0xc0, 0x8d, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0x40, 0x66, 0xf6, 0x05, 0xc0, 0xa9, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0xad, 0x24, 0xe0, 0x64, 0xc0, 0x43, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0x48, 0xa3, 0x04, 0x1c, 0xc0, 0xf4, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0xad, 0x26, 0xd4, 0x6c, 0xc0, 0x67, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0xad, 0x25, 0x90, 0x64, 0xc0, 0xcd, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x07, 0x08, 0x00, 0x04, 0xad, 0x27, 0x70, 0x44, }; /* google.com */ static u8 dns_reply_data_initializer[] = { 0x0, 0x0, 0x81, 0x80, 0x0, 0x1, 0x0, 0xe, 0x0, 0x0, 0x0, 0x0, 0x6, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x3, 0x63, 0x6f, 0x6d, 0x0, 0x0, 0xff, 0x0, 0x1, 0xc0, 0xc, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x1, 0x2b, 0x0, 0x4, 0xac, 0xd9, 0x3, 0x2e, 0xc0, 0xc, 0x0, 0x1c, 0x0, 0x1, 0x0, 0x0, 0x1, 0x2b, 0x0, 0x10, 0x26, 0x7, 0xf8, 0xb0, 0x40, 0x4, 0x8, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0xe, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x51, 0x7f, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x31, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0x6, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3b, 0x0, 0x22, 0xc0, 0x54, 0x9, 0x64, 0x6e, 0x73, 0x2d, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0xc0, 0xc, 0xa, 0x3d, 0xc7, 0x30, 0x0, 0x0, 0x3, 0x84, 0x0, 0x0, 0x3, 0x84, 0x0, 0x0, 0x7, 0x8, 0x0, 0x0, 0x0, 0x3c, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x57, 0x0, 0x11, 0x0, 0x1e, 0x4, 0x61, 0x6c, 0x74, 0x32, 0x5, 0x61, 0x73, 0x70, 0x6d, 0x78, 0x1, 0x6c, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x57, 0x0, 0x4, 0x0, 0xa, 0xc0, 0x9b, 0xc0, 0xc, 0x0, 0x10, 0x0, 0x1, 0x0, 0x0, 0xe, 0xf, 0x0, 0x24, 0x23, 0x76, 0x3d, 0x73, 0x70, 0x66, 0x31, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x3a, 0x5f, 0x73, 0x70, 0x66, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x20, 0x7e, 0x61, 0x6c, 0x6c, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x51, 0x7f, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x32, 0xc0, 0xc, 0xc0, 0xc, 0x1, 0x1, 0x0, 0x1, 0x0, 0x1, 0x51, 0x7f, 0x0, 0xf, 0x0, 0x5, 0x69, 0x73, 0x73, 0x75, 0x65, 0x70, 0x6b, 0x69, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x51, 0x7f, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x34, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x57, 0x0, 0x9, 0x0, 0x28, 0x4, 0x61, 0x6c, 0x74, 0x33, 0xc0, 0x9b, 0xc0, 0xc, 0x0, 0x2, 0x0, 0x1, 0x0, 0x1, 0x51, 0x7f, 0x0, 0x6, 0x3, 0x6e, 0x73, 0x33, 0xc0, 0xc, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x57, 0x0, 0x9, 0x0, 0x32, 0x4, 0x61, 0x6c, 0x74, 0x34, 0xc0, 0x9b, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x57, 0x0, 0x9, 0x0, 0x14, 0x4, 0x61, 0x6c, 0x74, 0x31, 0xc0, 0x9b }; #else /* www.weatherlink.com */ static u8 dns_reply_data_initializer[] = { 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77, 0x0b, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x6b, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0xff, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x9e, 0x00, 0x1f, 0x0e, 0x64, 0x33, 0x6b, 0x72, 0x30, 0x67, 0x75, 0x62, 0x61, 0x31, 0x64, 0x76, 0x77, 0x66, 0x0a, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x03, 0x6e, 0x65, 0x74, 0x00, }; #endif static clib_error_t * test_dns_fmt_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u8 *dns_reply_data = 0; int verbose = 0; int rv; vl_api_dns_resolve_name_reply_t _rm, *rmp = &_rm; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose %d", &verbose)) ; else if (unformat (input, "verbose")) verbose = 1; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } vec_validate (dns_reply_data, ARRAY_LEN (dns_reply_data_initializer) - 1); memcpy (dns_reply_data, dns_reply_data_initializer, ARRAY_LEN (dns_reply_data_initializer)); vlib_cli_output (vm, "%U", format_dns_reply, dns_reply_data, verbose); clib_memset (rmp, 0, sizeof (*rmp)); rv = vnet_dns_response_to_reply (dns_reply_data, rmp, 0 /* ttl-ptr */ ); switch (rv) { case VNET_API_ERROR_NAME_SERVER_NO_ADDRESSES: vlib_cli_output (vm, "no addresses found..."); break; default: vlib_cli_output (vm, "response to reply returned %d", rv); break; case 0: if (rmp->ip4_set) vlib_cli_output (vm, "ip4 address: %U", format_ip4_address, (ip4_address_t *) rmp->ip4_address); if (rmp->ip6_set) vlib_cli_output (vm, "ip6 address: %U", format_ip6_address, (ip6_address_t *) rmp->ip6_address); break; } vec_free (dns_reply_data); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (test_dns_fmt_command) = { .path = "test dns format", .short_help = "test dns format", .function = test_dns_fmt_command_fn, }; /* *INDENT-ON* */ static clib_error_t * test_dns_unfmt_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u8 *dns_reply_data = 0; int verbose = 0; int reply_set = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose %d", &verbose)) ; else if (unformat (input, "verbose")) verbose = 1; else if (unformat (input, "%U", unformat_dns_reply, &dns_reply_data)) reply_set = 1; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } if (reply_set == 0) return clib_error_return (0, "dns data not set..."); vlib_cli_output (vm, "%U", format_dns_reply, dns_reply_data, verbose); vec_free (dns_reply_data); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (test_dns_unfmt_command) = { .path = "test dns unformat", .short_help = "test dns unformat <name> [ip4][ip6]", .function = test_dns_unfmt_command_fn, }; /* *INDENT-ON* */ static clib_error_t * test_dns_expire_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { dns_main_t *dm = &dns_main; u8 *name = 0; uword *p; clib_error_t *e; dns_cache_entry_t *ep; if (unformat (input, "%v", &name)) { vec_add1 (name, 0); _vec_len (name) -= 1; } else return clib_error_return (0, "no name provided"); dns_cache_lock (dm); p = hash_get_mem (dm->cache_entry_by_name, name); if (!p) { dns_cache_unlock (dm); e = clib_error_return (0, "%s is not in the cache...", name); vec_free (name); return e; } ep = pool_elt_at_index (dm->entries, p[0]); ep->expiration_time = 0; return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (test_dns_expire_command) = { .path = "test dns expire", .short_help = "test dns expire <name>", .function = test_dns_expire_command_fn, }; /* *INDENT-ON* */ #endif void vnet_send_dns6_reply (dns_main_t * dm, dns_pending_request_t * pr, dns_cache_entry_t * ep, vlib_buffer_t * b0) { clib_warning ("Unimplemented..."); } void vnet_send_dns4_reply (dns_main_t * dm, dns_pending_request_t * pr, dns_cache_entry_t * ep, vlib_buffer_t * b0) { vlib_main_t *vm = dm->vlib_main; u32 bi = 0; fib_prefix_t prefix; fib_node_index_t fei; u32 sw_if_index, fib_index; ip4_main_t *im4 = &ip4_main; ip_lookup_main_t *lm4 = &im4->lookup_main; ip_interface_address_t *ia = 0; ip4_address_t *src_address; ip4_header_t *ip; udp_header_t *udp; dns_header_t *dh; vlib_frame_t *f; u32 *to_next; u8 *dns_response; u8 *reply; vl_api_dns_resolve_name_reply_t _rnr, *rnr = &_rnr; vl_api_dns_resolve_ip_reply_t _rir, *rir = &_rir; u32 ttl, tmp; u32 qp_offset; dns_query_t *qp; dns_rr_t *rr; u8 *rrptr; int is_fail = 0; ASSERT (ep && ep->dns_response); if (pr->request_type == DNS_PEER_PENDING_NAME_TO_IP) { /* Quick and dirty way to dig up the A-record address. $$ FIXME */ clib_memset (rnr, 0, sizeof (*rnr)); if (vnet_dns_response_to_reply (ep->dns_response, rnr, &ttl)) { /* clib_warning ("response_to_reply failed..."); */ is_fail = 1; } if (rnr->ip4_set == 0) { /* clib_warning ("No A-record..."); */ is_fail = 1; } } else if (pr->request_type == DNS_PEER_PENDING_IP_TO_NAME) { clib_memset (rir, 0, sizeof (*rir)); if (vnet_dns_response_to_name (ep->dns_response, rir, &ttl)) { /* clib_warning ("response_to_name failed..."); */ is_fail = 1; } } else { clib_warning ("Unknown request type %d", pr->request_type); return; } /* Initialize a buffer */ if (b0 == 0) { if (vlib_buffer_alloc (vm, &bi, 1) != 1) return; b0 = vlib_get_buffer (vm, bi); } if (b0->flags & VLIB_BUFFER_NEXT_PRESENT) vlib_buffer_free_one (vm, b0->next_buffer); /* * Reset the buffer. We recycle the DNS request packet in the cache * hit case, and reply immediately from the request node. * * In the resolution-required / deferred case, resetting a freshly-allocated * buffer won't hurt. We hope. */ b0->flags |= (VNET_BUFFER_F_LOCALLY_ORIGINATED | VLIB_BUFFER_TOTAL_LENGTH_VALID); b0->current_data = 0; b0->current_length = 0; b0->total_length_not_including_first_buffer = 0; vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; /* "local0" */ vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0; /* default VRF for now */ /* Find a FIB path to the peer we're trying to answer */ clib_memcpy (&prefix.fp_addr.ip4, pr->dst_address, sizeof (ip4_address_t)); prefix.fp_proto = FIB_PROTOCOL_IP4; prefix.fp_len = 32; fib_index = fib_table_find (prefix.fp_proto, 0 /* default VRF for now */ ); if (fib_index == (u32) ~ 0) { clib_warning ("no fib table"); return; } fei = fib_table_lookup (fib_index, &prefix); /* Couldn't find route to destination. Bail out. */ if (fei == FIB_NODE_INDEX_INVALID) { clib_warning ("no route to DNS server"); return; } sw_if_index = fib_entry_get_resolving_interface (fei); if (sw_if_index == ~0) { clib_warning ("route to %U exists, fei %d, get_resolving_interface returned" " ~0", fei, format_ip4_address, &prefix.fp_addr); return; } /* *INDENT-OFF* */ foreach_ip_interface_address(lm4, ia, sw_if_index, 1 /* honor unnumbered */, ({ src_address = ip_interface_address_get_address (lm4, ia); goto found_src_address; })); /* *INDENT-ON* */ clib_warning ("FIB BUG"); return; found_src_address: ip = vlib_buffer_get_current (b0); udp = (udp_header_t *) (ip + 1); dns_response = (u8 *) (udp + 1); clib_memset (ip, 0, sizeof (*ip) + sizeof (*udp)); /* * Start with the variadic portion of the exercise. * Turn the name into a set of DNS "labels". Max length * per label is 63, enforce that. */ reply = name_to_labels (pr->name); vec_free (pr->name); qp_offset = vec_len (reply); /* Add space for the query header */ vec_validate (reply, qp_offset + sizeof (dns_query_t) - 1); qp = (dns_query_t *) (reply + qp_offset); if (pr->request_type == DNS_PEER_PENDING_NAME_TO_IP) qp->type = clib_host_to_net_u16 (DNS_TYPE_A); else qp->type = clib_host_to_net_u16 (DNS_TYPE_PTR); qp->class = clib_host_to_net_u16 (DNS_CLASS_IN); /* Punch in space for the dns_header_t */ vec_insert (reply, sizeof (dns_header_t), 0); dh = (dns_header_t *) reply; /* Transaction ID = pool index */ dh->id = pr->id; /* Announce that we did a recursive lookup */ tmp = DNS_AA | DNS_RA | DNS_RD | DNS_OPCODE_QUERY | DNS_QR; if (is_fail) tmp |= DNS_RCODE_NAME_ERROR; dh->flags = clib_host_to_net_u16 (tmp); dh->qdcount = clib_host_to_net_u16 (1); dh->anscount = (is_fail == 0) ? clib_host_to_net_u16 (1) : 0; dh->nscount = 0; dh->arcount = 0; /* If the name resolution worked, cough up an appropriate RR */ if (is_fail == 0) { /* Add the answer. First, a name pointer (0xC00C) */ vec_add1 (reply, 0xC0); vec_add1 (reply, 0x0C); /* Now, add single A-rec RR */ if (pr->request_type == DNS_PEER_PENDING_NAME_TO_IP) { vec_add2 (reply, rrptr, sizeof (dns_rr_t) + sizeof (ip4_address_t)); rr = (dns_rr_t *) rrptr; rr->type = clib_host_to_net_u16 (DNS_TYPE_A); rr->class = clib_host_to_net_u16 (1 /* internet */ ); rr->ttl = clib_host_to_net_u32 (ttl); rr->rdlength = clib_host_to_net_u16 (sizeof (ip4_address_t)); clib_memcpy (rr->rdata, rnr->ip4_address, sizeof (ip4_address_t)); } else { /* Or a single PTR RR */ u8 *vecname = format (0, "%s", rir->name); u8 *label_vec = name_to_labels (vecname); vec_free (vecname); vec_add2 (reply, rrptr, sizeof (dns_rr_t) + vec_len (label_vec)); rr = (dns_rr_t *) rrptr; rr->type = clib_host_to_net_u16 (DNS_TYPE_PTR); rr->class = clib_host_to_net_u16 (1 /* internet */ ); rr->ttl = clib_host_to_net_u32 (ttl); rr->rdlength = clib_host_to_net_u16 (vec_len (label_vec)); clib_memcpy (rr->rdata, label_vec, vec_len (label_vec)); vec_free (label_vec); } } clib_memcpy (dns_response, reply, vec_len (reply)); /* Set the packet length */ b0->current_length = sizeof (*ip) + sizeof (*udp) + vec_len (reply); /* IP header */ ip->ip_version_and_header_length = 0x45; ip->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); ip->ttl = 255; ip->protocol = IP_PROTOCOL_UDP; ip->src_address.as_u32 = src_address->as_u32; clib_memcpy (ip->dst_address.as_u8, pr->dst_address, sizeof (ip4_address_t)); ip->checksum = ip4_header_checksum (ip); /* UDP header */ udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_dns); udp->dst_port = pr->dst_port; udp->length = clib_host_to_net_u16 (sizeof (udp_header_t) + vec_len (reply)); udp->checksum = 0; vec_free (reply); /* Ship it to ip4_lookup */ f = vlib_get_frame_to_node (vm, ip4_lookup_node.index); to_next = vlib_frame_vector_args (f); to_next[0] = bi; f->n_vectors = 1; vlib_put_frame_to_node (vm, ip4_lookup_node.index, f); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */