ip: Protocol Independent IP Neighbors
[vpp.git] / src / vnet / ip6-nd / ip6_nd_proxy.c
1 /*
2  * ip/ip6_neighbor.c: IP6 neighbor handling
3  *
4  * Copyright (c) 2010 Cisco and/or its affiliates.
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
18 #include <vnet/ip6-nd/ip6_nd.h>
19 #include <vnet/ip-neighbor/ip_neighbor.h>
20
21 #include <vnet/fib/ip6_fib.h>
22
23 static int
24 ip6_nd_proxy_add_del (u32 sw_if_index, const ip6_address_t * addr, u8 is_del)
25 {
26   /* *INDENT-OFF* */
27   u32 fib_index;
28   fib_prefix_t pfx = {
29     .fp_len = 128,
30     .fp_proto = FIB_PROTOCOL_IP6,
31     .fp_addr = {
32       .ip6 = *addr,
33     },
34   };
35   ip46_address_t nh = {
36     .ip6 = *addr,
37   };
38   /* *INDENT-ON* */
39
40   fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
41
42   if (~0 == fib_index)
43     return VNET_API_ERROR_NO_SUCH_FIB;
44
45   if (is_del)
46     {
47       fib_table_entry_path_remove (fib_index,
48                                    &pfx,
49                                    FIB_SOURCE_IP6_ND_PROXY,
50                                    DPO_PROTO_IP6,
51                                    &nh,
52                                    sw_if_index,
53                                    ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
54       /* flush the ND cache of this address if it's there */
55       ip_neighbor_del (&nh, IP46_TYPE_IP6, sw_if_index);
56     }
57   else
58     {
59       fib_table_entry_path_add (fib_index,
60                                 &pfx,
61                                 FIB_SOURCE_IP6_ND_PROXY,
62                                 FIB_ENTRY_FLAG_NONE,
63                                 DPO_PROTO_IP6,
64                                 &nh,
65                                 sw_if_index,
66                                 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
67     }
68   return (0);
69 }
70
71 int
72 ip6_nd_proxy_add (u32 sw_if_index, const ip6_address_t * addr)
73 {
74   return (ip6_nd_proxy_add_del (sw_if_index, addr, 0));
75 }
76
77 int
78 ip6_nd_proxy_del (u32 sw_if_index, const ip6_address_t * addr)
79 {
80   return (ip6_nd_proxy_add_del (sw_if_index, addr, 1));
81 }
82
83 static clib_error_t *
84 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
85                       unformat_input_t * input, vlib_cli_command_t * cmd)
86 {
87   vnet_main_t *vnm = vnet_get_main ();
88   clib_error_t *error = 0;
89   ip6_address_t addr;
90   u32 sw_if_index;
91   u8 is_del = 0;
92
93   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
94     {
95       /* get the rest of the command */
96       while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
97         {
98           if (unformat (input, "%U", unformat_ip6_address, &addr))
99             break;
100           else if (unformat (input, "delete") || unformat (input, "del"))
101             is_del = 1;
102           else
103             return (unformat_parse_error (input));
104         }
105     }
106
107   ip6_nd_proxy_add_del (sw_if_index, &addr, is_del);
108
109   return error;
110 }
111
112 /* *INDENT-OFF* */
113 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
114 {
115   .path = "set ip6 nd proxy",
116   .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
117   .function = set_ip6_nd_proxy_cmd,
118 };
119 /* *INDENT-ON* */
120
121 /*
122  * fd.io coding-style-patch-verification: ON
123  *
124  * Local Variables:
125  * eval: (c-set-style "gnu")
126  * End:
127  */