Blame view

RIOT/sys/shell/commands/sc_fib.c 7.86 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2015 Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_shell_commands
   * @{
   *
   * @file
   * @brief       Provides shell commands to manage and show FIB Entries
   *
   * @author      2015 Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de>
   *
   * @}
   */
  
  #include <arpa/inet.h>
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <stdlib.h>
  #include "thread.h"
  #include "net/af.h"
  #ifdef MODULE_GNRC_NETIF
  #include "net/gnrc/netif.h"
  #endif
  #include "net/fib.h"
  #include "net/gnrc/ipv6.h"
  
  #define INFO1_TXT "fibroute add <destination> via <next hop> [dev <device>]"
  #define INFO2_TXT " [lifetime <lifetime>]"
  #define INFO3_TXT "       <destination> - the destination address with optional prefix size, e.g. /116\n" \
                    "       <next hop>    - the address of the next-hop towards the <destination>\n" \
                    "       <device>      - the device id of the Interface to use." \
                    " Optional if only one interface is available.\n"
  #define INFO4_TXT "       <lifetime>    - optional lifetime in ms when the entry automatically invalidates\n"
  #define INFO5_TXT "fibroute del <destination>\n" \
                    "       <destination> - the destination address of the entry to be deleted\n"
  
  static unsigned char tmp_ipv4_dst[INADDRSZ];  /**< buffer for ipv4 address conversion */
  static unsigned char tmp_ipv4_nxt[INADDRSZ];  /**< buffer for ipv4 address conversion */
  static unsigned char tmp_ipv6_dst[IN6ADDRSZ]; /**< buffer for ipv6 address conversion */
  static unsigned char tmp_ipv6_nxt[IN6ADDRSZ]; /**< buffer for ipv6 address conversion */
  
  static void _fib_usage(int info)
  {
      switch (info) {
          case 0: {
              puts("\nsee <fibroute [add|del]> for more information\n"
                   "<fibroute flush [interface]> removes all entries [associated with interface]\n");
              break;
          }
          case 1: {
              puts("\nbrief: adds a new entry to the FIB.\nusage: "
                   INFO1_TXT "\n" INFO3_TXT);
              break;
          }
  
          case 2: {
              puts("\nbrief: adds a new entry to the FIB.\nusage: "
                   INFO1_TXT INFO2_TXT "\n" INFO3_TXT INFO4_TXT);
              break;
          }
  
          case 3: {
              puts("\nbrief: deletes an entry from the FIB.\nusage: "
                   INFO5_TXT);
              break;
          }
  
          default:
              break;
      };
  }
  
  static void _fib_add(const char *dest, const char *next, kernel_pid_t pid, uint32_t lifetime)
  {
      uint32_t prefix = 0;
      /* Get the prefix length */
      size_t i = 0;
      for (i = strlen(dest); i > 0; --i) {
          if (dest[i] == '/') {
             prefix = atoi(&dest[i+1]);
             break;
          }
          if (dest[i] == ':' || dest[i] == '.') {
             i = strlen(dest);
             break;
          }
      }
  
      size_t dst_size = (i+1);
      unsigned char dst_arr[dst_size];
      memset(dst_arr, 0, dst_size);
      memcpy(dst_arr, dest, i);
      unsigned char *dst = &dst_arr[0];
      uint32_t dst_flags = 0;
  
      unsigned char *nxt = (unsigned char *)next;
      size_t nxt_size = (strlen(next));
      uint32_t nxt_flags = 0;
  
      /* determine destination address */
      if (inet_pton(AF_INET6, (char*)dst, tmp_ipv6_dst)) {
          dst = tmp_ipv6_dst;
          dst_size = IN6ADDRSZ;
      }
      else if (inet_pton(AF_INET, dest, tmp_ipv4_dst)) {
          dst = tmp_ipv4_dst;
          dst_size = INADDRSZ;
      }
  
      /* determine next-hop address */
      if (inet_pton(AF_INET6, next, tmp_ipv6_nxt)) {
          nxt = tmp_ipv6_nxt;
          nxt_size = IN6ADDRSZ;
      }
      else if (inet_pton(AF_INET, next, tmp_ipv4_nxt)) {
          nxt = tmp_ipv4_nxt;
          nxt_size = INADDRSZ;
      }
  
      dst_flags |= (prefix << FIB_FLAG_NET_PREFIX_SHIFT);
      fib_add_entry(&gnrc_ipv6_fib_table, pid, dst, dst_size, dst_flags, nxt,
                    nxt_size, nxt_flags, lifetime);
  }
  
  int _fib_route_handler(int argc, char **argv)
  {
      /* e.g. fibroute right now dont care about the adress/protocol family */
      if (argc == 1) {
          fib_print_routes(&gnrc_ipv6_fib_table);
          return 0;
      }
  
      /* e.g. firoute [add|del] */
      if (argc == 2) {
          if ((strcmp("add", argv[1]) == 0)) {
              _fib_usage(2);
          }
          else if ((strcmp("del", argv[1]) == 0)) {
              _fib_usage(3);
          }
          else if ((strcmp("flush", argv[1]) == 0)) {
              fib_flush(&gnrc_ipv6_fib_table, KERNEL_PID_UNDEF);
              puts("successfully flushed all entries");
          }
          else {
              _fib_usage(0);
          }
  
          return 1;
      }
  
      if (argc > 2 && !((strcmp("add", argv[1]) == 0) ||
                        (strcmp("del", argv[1]) == 0) ||
                        (strcmp("flush", argv[1]) == 0))) {
          puts("\nunrecognized parameter2.\nPlease enter fibroute [add|del] for more information.");
          return 1;
      }
  
      /* e.g. fibroute del <destination> */
      if (argc == 3) {
          if ((strcmp("flush", argv[1]) == 0)) {
              kernel_pid_t iface = atoi(argv[2]);
              if (gnrc_netif_exist(iface)) {
                  fib_flush(&gnrc_ipv6_fib_table, iface);
                  printf("successfully flushed all entries for interface %" PRIu16"\n", iface);
              }
              else {
                  printf("interface %" PRIu16" does not exist\n", iface);
              }
          }
          else if (inet_pton(AF_INET6, argv[2], tmp_ipv6_dst)) {
              fib_remove_entry(&gnrc_ipv6_fib_table, tmp_ipv6_dst, IN6ADDRSZ);
          }
          else if (inet_pton(AF_INET, argv[2], tmp_ipv4_dst)) {
              fib_remove_entry(&gnrc_ipv6_fib_table, tmp_ipv4_dst, INADDRSZ);
          }
          else {
              fib_remove_entry(&gnrc_ipv6_fib_table, (uint8_t *)argv[2],
                               (strlen(argv[2])));
          }
  
          return 0;
      }
  
  #ifdef MODULE_GNRC_NETIF
      /* e.g. fibroute add <destination> via <next hop> */
      if ((argc == 5) && (strcmp("add", argv[1]) == 0) && (strcmp("via", argv[3]) == 0)) {
          kernel_pid_t ifs[GNRC_NETIF_NUMOF];
          size_t ifnum = gnrc_netif_get(ifs);
          if (ifnum == 1) {
              _fib_add(argv[2], argv[4], ifs[0], (uint32_t)FIB_LIFETIME_NO_EXPIRE);
          }
          else {
              _fib_usage(1);
              return 1;
          }
  
          return 0;
      }
  
      /* e.g. fibroute add <destination> via <next hop> lifetime <lifetime> */
      if ((argc == 7) && (strcmp("add", argv[1]) == 0) && (strcmp("via", argv[3]) == 0)
              && (strcmp("lifetime", argv[5]) == 0)) {
          kernel_pid_t ifs[GNRC_NETIF_NUMOF];
          size_t ifnum = gnrc_netif_get(ifs);
          if (ifnum == 1) {
              _fib_add(argv[2], argv[4], ifs[0], (uint32_t)atoi(argv[6]));
          }
          else {
              _fib_usage(1);
              return 1;
          }
  
          return 0;
      }
  #endif
  
      /* e.g. fibroute add <destination> via <next hop> dev <device> */
      if (argc == 7) {
          if ((strcmp("add", argv[1]) == 0) && (strcmp("via", argv[3]) == 0)
              && (strcmp("dev", argv[5]) == 0)) {
              _fib_add(argv[2], argv[4], (kernel_pid_t)atoi(argv[6]), (uint32_t)FIB_LIFETIME_NO_EXPIRE);
          }
          else {
              _fib_usage(1);
              return 1;
          }
  
          return 0;
      }
  
      /* e.g. fibroute add <destination> via <next hop> dev <device> lifetime <lifetime> */
      if (argc == 9) {
          if ((strcmp("add", argv[1]) == 0) && (strcmp("via", argv[3]) == 0)
              && (strcmp("dev", argv[5]) == 0)
              && (strcmp("lifetime", argv[7]) == 0)) {
              _fib_add(argv[2], argv[4], (kernel_pid_t )atoi(argv[6]), (uint32_t)atoi(argv[8]));
          }
          else {
              _fib_usage(2);
              return 1;
          }
  
          return 0;
      }
  
      puts("\nunrecognized parameters.\nPlease enter fibroute [add|del] for more information.");
      return 1;
  }