Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * ppp.c: ppp support |
| 17 | * |
| 18 | * Copyright (c) 2010 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/vnet.h> |
| 41 | #include <vnet/ppp/ppp.h> |
| 42 | |
| 43 | /* Global main structure. */ |
| 44 | ppp_main_t ppp_main; |
| 45 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 46 | u8 * |
| 47 | format_ppp_protocol (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | { |
| 49 | ppp_protocol_t p = va_arg (*args, u32); |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 50 | ppp_main_t *pm = &ppp_main; |
| 51 | ppp_protocol_info_t *pi = ppp_get_protocol_info (pm, p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
| 53 | if (pi) |
| 54 | s = format (s, "%s", pi->name); |
| 55 | else |
| 56 | s = format (s, "0x%04x", p); |
| 57 | |
| 58 | return s; |
| 59 | } |
| 60 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 61 | u8 * |
| 62 | format_ppp_header_with_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 64 | ppp_main_t *pm = &ppp_main; |
| 65 | ppp_header_t *h = va_arg (*args, ppp_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | u32 max_header_bytes = va_arg (*args, u32); |
| 67 | ppp_protocol_t p = clib_net_to_host_u16 (h->protocol); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 68 | u32 indent, header_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
| 70 | header_bytes = sizeof (h[0]); |
| 71 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 72 | return format (s, "ppp header truncated"); |
| 73 | |
| 74 | indent = format_get_indent (s); |
| 75 | |
| 76 | s = format (s, "PPP %U", format_ppp_protocol, p); |
| 77 | |
| 78 | if (h->address != 0xff) |
| 79 | s = format (s, ", address 0x%02x", h->address); |
| 80 | if (h->control != 0x03) |
| 81 | s = format (s, ", control 0x%02x", h->control); |
| 82 | |
| 83 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 84 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 85 | ppp_protocol_info_t *pi = ppp_get_protocol_info (pm, p); |
| 86 | vlib_node_t *node = vlib_get_node (pm->vlib_main, pi->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | if (node->format_buffer) |
| 88 | s = format (s, "\n%U%U", |
| 89 | format_white_space, indent, |
| 90 | node->format_buffer, (void *) (h + 1), |
| 91 | max_header_bytes - header_bytes); |
| 92 | } |
| 93 | |
| 94 | return s; |
| 95 | } |
| 96 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 97 | u8 * |
| 98 | format_ppp_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 100 | ppp_header_t *h = va_arg (*args, ppp_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | return format (s, "%U", format_ppp_header_with_length, h, 0); |
| 102 | } |
| 103 | |
| 104 | /* Returns ppp protocol as an int in host byte order. */ |
| 105 | uword |
| 106 | unformat_ppp_protocol_host_byte_order (unformat_input_t * input, |
| 107 | va_list * args) |
| 108 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 109 | u16 *result = va_arg (*args, u16 *); |
| 110 | ppp_main_t *pm = &ppp_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | int p, i; |
| 112 | |
| 113 | /* Numeric type. */ |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 114 | if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
| 116 | if (p >= (1 << 16)) |
| 117 | return 0; |
| 118 | *result = p; |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | /* Named type. */ |
| 123 | if (unformat_user (input, unformat_vlib_number_by_name, |
| 124 | pm->protocol_info_by_name, &i)) |
| 125 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 126 | ppp_protocol_info_t *pi = vec_elt_at_index (pm->protocol_infos, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | *result = pi->protocol; |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | uword |
| 135 | unformat_ppp_protocol_net_byte_order (unformat_input_t * input, |
| 136 | va_list * args) |
| 137 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 138 | u16 *result = va_arg (*args, u16 *); |
| 139 | if (!unformat_user (input, unformat_ppp_protocol_host_byte_order, result)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | return 0; |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 141 | *result = clib_host_to_net_u16 ((u16) * result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | uword |
| 146 | unformat_ppp_header (unformat_input_t * input, va_list * args) |
| 147 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 148 | u8 **result = va_arg (*args, u8 **); |
| 149 | ppp_header_t _h, *h = &_h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | u16 p; |
| 151 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 152 | if (!unformat (input, "%U", unformat_ppp_protocol_host_byte_order, &p)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | |
| 155 | h->address = 0xff; |
| 156 | h->control = 0x03; |
| 157 | h->protocol = clib_host_to_net_u16 (p); |
| 158 | |
| 159 | /* Add header to result. */ |
| 160 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 161 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | u32 n_bytes = sizeof (h[0]); |
| 163 | |
| 164 | vec_add2 (*result, p, n_bytes); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 165 | clib_memcpy (p, h, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | } |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 167 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | return 1; |
| 169 | } |
| 170 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 171 | static u8 * |
| 172 | ppp_build_rewrite (vnet_main_t * vnm, |
| 173 | u32 sw_if_index, |
| 174 | vnet_link_t link_type, const void *dst_hw_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 176 | ppp_header_t *h; |
| 177 | u8 *rewrite = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | ppp_protocol_t protocol; |
| 179 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 180 | switch (link_type) |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 181 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 182 | #define _(a,b) case VNET_LINK_##a: protocol = PPP_PROTOCOL_##b; break |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 183 | _(IP4, ip4); |
| 184 | _(IP6, ip6); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 185 | _(MPLS, mpls_unicast); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | #undef _ |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 187 | default: |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 188 | return (NULL); |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 189 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | vec_validate (rewrite, sizeof (*h) - 1); |
| 192 | h = (ppp_header_t *) rewrite; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | h->address = 0xff; |
| 194 | h->control = 0x03; |
| 195 | h->protocol = clib_host_to_net_u16 (protocol); |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 196 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 197 | return (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 200 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | VNET_HW_INTERFACE_CLASS (ppp_hw_interface_class) = { |
| 202 | .name = "PPP", |
| 203 | .format_header = format_ppp_header_with_length, |
| 204 | .unformat_header = unformat_ppp_header, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 205 | .build_rewrite = ppp_build_rewrite, |
| 206 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | }; |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 208 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 210 | static void |
| 211 | add_protocol (ppp_main_t * pm, ppp_protocol_t protocol, char *protocol_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 213 | ppp_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | u32 i; |
| 215 | |
| 216 | vec_add2 (pm->protocol_infos, pi, 1); |
| 217 | i = pi - pm->protocol_infos; |
| 218 | |
| 219 | pi->name = protocol_name; |
| 220 | pi->protocol = protocol; |
| 221 | pi->next_index = pi->node_index = ~0; |
| 222 | |
| 223 | hash_set (pm->protocol_info_by_protocol, protocol, i); |
| 224 | hash_set_mem (pm->protocol_info_by_name, pi->name, i); |
| 225 | } |
| 226 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 227 | static clib_error_t * |
| 228 | ppp_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | { |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 230 | ppp_main_t *pm = &ppp_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
| 232 | memset (pm, 0, sizeof (pm[0])); |
| 233 | pm->vlib_main = vm; |
| 234 | |
| 235 | pm->protocol_info_by_name = hash_create_string (0, sizeof (uword)); |
| 236 | pm->protocol_info_by_protocol = hash_create (0, sizeof (uword)); |
| 237 | |
| 238 | #define _(n,s) add_protocol (pm, PPP_PROTOCOL_##s, #s); |
| 239 | foreach_ppp_protocol; |
| 240 | #undef _ |
| 241 | |
| 242 | return vlib_call_init_function (vm, ppp_input_init); |
| 243 | } |
| 244 | |
| 245 | VLIB_INIT_FUNCTION (ppp_init); |
| 246 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 247 | ppp_main_t * |
| 248 | ppp_get_main (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | { |
| 250 | vlib_call_init_function (vm, ppp_init); |
| 251 | return &ppp_main; |
| 252 | } |
| 253 | |
Calvin | aba0fe4 | 2016-08-19 13:43:59 -0400 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * fd.io coding-style-patch-verification: ON |
| 257 | * |
| 258 | * Local Variables: |
| 259 | * eval: (c-set-style "gnu") |
| 260 | * End: |
| 261 | */ |