mactime: upstream new features
[vpp.git] / src / plugins / mactime / node.c
1 /*
2  * node.c - skeleton vpp engine plug-in dual-loop node skeleton
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <mactime/mactime.h>
22 #include <vnet/ip/ip4.h>
23
24 typedef struct
25 {
26   u32 next_index;
27   u32 device_index;
28   u8 src_mac[6];
29   u8 device_name[64];
30 } mactime_trace_t;
31
32 vlib_node_registration_t mactime_node;
33 vlib_node_registration_t mactime_tx_node;
34
35 #define foreach_mactime_error                   \
36 _(OK, "Permitted packets")                      \
37 _(STATIC_DROP, "Static drop packets")           \
38 _(RANGE_DROP, "Range drop packets")             \
39 _(QUOTA_DROP, "Data quota drop packets")        \
40 _(DROP_10001, "Dropped UDP DST-port 10001")
41
42 typedef enum
43 {
44 #define _(sym,str) MACTIME_ERROR_##sym,
45   foreach_mactime_error
46 #undef _
47     MACTIME_N_ERROR,
48 } mactime_error_t;
49
50 static char *mactime_error_strings[] = {
51 #define _(sym,string) string,
52   foreach_mactime_error
53 #undef _
54 };
55
56 typedef enum
57 {
58   MACTIME_NEXT_DROP,
59   MACTIME_NEXT_ETHERNET_INPUT,
60   MACTIME_N_NEXT,
61 } mactime_next_t;
62
63 /* packet trace format function */
64 static u8 *
65 format_mactime_trace (u8 * s, va_list * args)
66 {
67   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69   mactime_trace_t *t = va_arg (*args, mactime_trace_t *);
70
71   s = format (s, "MACTIME: src mac %U device %s result %s\n",
72               format_mac_address, t->src_mac,
73               (t->device_index != ~0) ? t->device_name : (u8 *) "unknown",
74               t->next_index == MACTIME_NEXT_DROP ? "drop" : "pass");
75   return s;
76 }
77
78 static uword
79 mactime_node_inline (vlib_main_t * vm,
80                      vlib_node_runtime_t * node, vlib_frame_t * frame,
81                      int is_tx)
82 {
83   u32 n_left_from, *from, *to_next;
84   mactime_next_t next_index;
85   mactime_main_t *mm = &mactime_main;
86   mactime_device_t *dp;
87   clib_bihash_kv_8_8_t kv;
88   clib_bihash_8_8_t *lut = &mm->lookup_table;
89   u32 packets_ok = 0;
90   f64 now;
91   u32 thread_index = vm->thread_index;
92   vnet_main_t *vnm = vnet_get_main ();
93   vnet_interface_main_t *im = &vnm->interface_main;
94   u8 arc = im->output_feature_arc_index;
95   vnet_feature_config_main_t *fcm;
96
97   if (is_tx)
98     fcm = vnet_feature_get_config_main (arc);
99
100   now = clib_timebase_now (&mm->timebase);
101
102   if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
103     mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
104
105   from = vlib_frame_vector_args (frame);
106   n_left_from = frame->n_vectors;
107   next_index = node->cached_next_index;
108
109   while (n_left_from > 0)
110     {
111       u32 n_left_to_next;
112
113       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
114
115       while (n_left_from > 0 && n_left_to_next > 0)
116         {
117           u32 bi0;
118           vlib_buffer_t *b0;
119           u32 next0;
120           u32 device_index0;
121           u32 len0;
122           ethernet_header_t *en0;
123           int i;
124
125           /* speculatively enqueue b0 to the current next frame */
126           bi0 = from[0];
127           to_next[0] = bi0;
128           from += 1;
129           to_next += 1;
130           n_left_from -= 1;
131           n_left_to_next -= 1;
132
133           b0 = vlib_get_buffer (vm, bi0);
134
135           /* Set next0 to e.g. interface-tx */
136           if (is_tx)
137             vnet_get_config_data (&fcm->config_main,
138                                   &b0->current_config_index, &next0,
139                                   /* # bytes of config data */ 0);
140           else
141             next0 = MACTIME_NEXT_ETHERNET_INPUT;
142
143           vlib_buffer_advance (b0, -(word) vnet_buffer (b0)->l2_hdr_offset);
144
145           len0 = vlib_buffer_length_in_chain (vm, b0);
146           en0 = vlib_buffer_get_current (b0);
147           kv.key = 0;
148           if (is_tx)
149             clib_memcpy_fast (&kv.key, en0->dst_address, 6);
150           else
151             clib_memcpy_fast (&kv.key, en0->src_address, 6);
152
153           /* Lookup the src/dst mac address */
154           if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
155             {
156               /* Create a table entry... */
157               mactime_send_create_entry_message
158                 (is_tx ? en0->dst_address : en0->src_address);
159
160               /* and let this packet pass */
161               device_index0 = ~0;
162               dp = 0;
163               packets_ok++;
164               goto trace0;
165             }
166           else
167             device_index0 = kv.value;
168
169           dp = pool_elt_at_index (mm->devices, device_index0);
170
171           /* Known device, check for a traffic quota */
172           if (PREDICT_FALSE (dp->data_quota))
173             {
174               vlib_counter_t device_current_count;
175               vlib_get_combined_counter (&mm->allow_counters,
176                                          dp - mm->devices,
177                                          &device_current_count);
178               if (device_current_count.bytes >= dp->data_quota)
179                 {
180                   next0 = MACTIME_NEXT_DROP;
181                   b0->error = node->errors[MACTIME_ERROR_QUOTA_DROP];
182                   vlib_increment_combined_counter
183                     (&mm->drop_counters, thread_index, dp - mm->devices, 1,
184                      len0);
185                   goto trace0;
186                 }
187             }
188
189           /* Static drop / allow? */
190           if (PREDICT_FALSE
191               (dp->flags &
192                (MACTIME_DEVICE_FLAG_STATIC_DROP
193                 | MACTIME_DEVICE_FLAG_STATIC_ALLOW)))
194             {
195               if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
196                 {
197                   next0 = MACTIME_NEXT_DROP;
198                   b0->error = node->errors[MACTIME_ERROR_STATIC_DROP];
199                   vlib_increment_combined_counter
200                     (&mm->drop_counters, thread_index, dp - mm->devices, 1,
201                      len0);
202                 }
203               else              /* note next0 set to allow */
204                 {
205                   /*
206                    * Special-case mini-ACL for a certain species of
207                    * home security DVR which likes to "call home."
208                    */
209                   if (PREDICT_FALSE
210                       (dp->flags & MACTIME_DEVICE_FLAG_DROP_UDP_10001))
211                     {
212                       ip4_header_t *ip = (void *) (((u8 *) en0) + 14);
213                       udp_header_t *udp = (udp_header_t *) (ip + 1);
214                       if (ip->protocol != IP_PROTOCOL_UDP)
215                         goto pass;
216                       if (clib_net_to_host_u16 (udp->dst_port) == 10001 ||
217                           clib_net_to_host_u16 (udp->dst_port) == 9603)
218                         {
219                           next0 = MACTIME_NEXT_DROP;
220                           b0->error = node->errors[MACTIME_ERROR_DROP_10001];
221                         }
222                       else
223                         goto pass;
224                     }
225                   else
226                     {
227                     pass:
228                       vlib_increment_combined_counter
229                         (&mm->allow_counters, thread_index, dp - mm->devices,
230                          1, len0);
231                       packets_ok++;
232                     }
233                 }
234               goto trace0;
235             }
236
237           /* Known device, see if traffic allowed at the moment */
238           for (i = 0; i < vec_len (dp->ranges); i++)
239             {
240               clib_timebase_range_t *r = dp->ranges + i;
241               f64 start0, end0;
242
243               start0 = r->start + mm->sunday_midnight;
244               end0 = r->end + mm->sunday_midnight;
245               /* Packet within time range */
246               if (now >= start0 && now <= end0)
247                 {
248                   /* And it's a drop range, drop it */
249                   if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
250                     {
251                       vlib_increment_combined_counter
252                         (&mm->drop_counters, thread_index,
253                          dp - mm->devices, 1, len0);
254                       next0 = MACTIME_NEXT_DROP;
255                       b0->error = node->errors[MACTIME_ERROR_RANGE_DROP];
256                     }
257                   else          /* it's an allow range, allow it */
258                     {
259                       vlib_increment_combined_counter
260                         (&mm->allow_counters, thread_index,
261                          dp - mm->devices, 1, len0);
262                       packets_ok++;
263                     }
264                   goto trace0;
265                 }
266             }
267           /*
268            * Didn't hit a range, so *drop* if allow configured, or
269            * *allow* if drop configured.
270            */
271           if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
272             {
273               next0 = MACTIME_NEXT_DROP;
274               b0->error = node->errors[MACTIME_ERROR_STATIC_DROP];
275               vlib_increment_combined_counter
276                 (&mm->drop_counters, thread_index, dp - mm->devices, 1, len0);
277             }
278           else
279             {
280               vlib_increment_combined_counter
281                 (&mm->allow_counters, thread_index, dp - mm->devices, 1,
282                  len0);
283               packets_ok++;
284             }
285
286         trace0:
287           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
288                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
289             {
290               mactime_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
291               clib_memcpy_fast (t->src_mac, en0->src_address,
292                                 sizeof (t->src_mac));
293
294               t->next_index = next0;
295               t->device_index = device_index0;
296
297               if (dp)
298                 {
299                   clib_memcpy_fast (t->device_name, dp->device_name,
300                                     ARRAY_LEN (t->device_name));
301                   t->device_name[ARRAY_LEN (t->device_name) - 1] = 0;
302                 }
303             }
304
305           /* verify speculative enqueue, maybe switch current next frame */
306           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
307                                            to_next, n_left_to_next,
308                                            bi0, next0);
309         }
310
311       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
312     }
313
314   vlib_node_increment_counter (vm, node->node_index,
315                                MACTIME_ERROR_OK, packets_ok);
316   return frame->n_vectors;
317 }
318
319 static uword
320 mactime_node_fn (vlib_main_t * vm,
321                  vlib_node_runtime_t * node, vlib_frame_t * frame)
322 {
323   return mactime_node_inline (vm, node, frame, 0 /* is_tx */ );
324 }
325
326 /* *INDENT-OFF* */
327 VLIB_REGISTER_NODE (mactime_node) =
328 {
329   .function = mactime_node_fn,
330   .name = "mactime",
331   .vector_size = sizeof (u32),
332   .format_trace = format_mactime_trace,
333   .type = VLIB_NODE_TYPE_INTERNAL,
334
335   .n_errors = ARRAY_LEN(mactime_error_strings),
336   .error_strings = mactime_error_strings,
337
338   .n_next_nodes = MACTIME_N_NEXT,
339
340   /* edit / add dispositions here */
341   .next_nodes =
342   {
343     [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input",
344     [MACTIME_NEXT_DROP] = "error-drop",
345   },
346 };
347 /* *INDENT-ON* */
348
349 static uword
350 mactime_tx_node_fn (vlib_main_t * vm,
351                     vlib_node_runtime_t * node, vlib_frame_t * frame)
352 {
353   return mactime_node_inline (vm, node, frame, 1 /* is_tx */ );
354 }
355
356 /* *INDENT-OFF* */
357 VLIB_REGISTER_NODE (mactime_tx_node) =
358 {
359   .function = mactime_tx_node_fn,
360   .name = "mactime-tx",
361   .vector_size = sizeof (u32),
362   .format_trace = format_mactime_trace,
363   .type = VLIB_NODE_TYPE_INTERNAL,
364
365   .n_errors = ARRAY_LEN(mactime_error_strings),
366   .error_strings = mactime_error_strings,
367
368   .n_next_nodes = MACTIME_N_NEXT,
369
370   /* edit / add dispositions here */
371   .next_nodes =
372   {
373     [MACTIME_NEXT_DROP] = "error-drop",
374     [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input", /* notused */
375   },
376 };
377 /* *INDENT-ON* */
378
379 /*
380  * fd.io coding-style-patch-verification: ON
381  *
382  * Local Variables:
383  * eval: (c-set-style "gnu")
384  * End:
385  */