Conner Huff | 2357fa5 | 2017-02-03 11:19:02 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved. |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * RMNET Data configuration engine |
| 13 | */ |
| 14 | |
| 15 | #include <net/sock.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/netlink.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/skbuff.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/rmnet_data.h> |
| 22 | #include <net/rmnet_config.h> |
| 23 | #include "rmnet_data_config.h" |
| 24 | #include "rmnet_data_handlers.h" |
| 25 | #include "rmnet_data_vnd.h" |
| 26 | #include "rmnet_data_private.h" |
| 27 | #include "rmnet_data_trace.h" |
Subash Abhinov Kasiviswanathan | 08fed02 | 2017-11-17 17:54:49 -0700 | [diff] [blame] | 28 | #include "rmnet_map.h" |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 29 | |
| 30 | RMNET_LOG_MODULE(RMNET_DATA_LOGMASK_CONFIG); |
| 31 | |
| 32 | /* Local Definitions and Declarations */ |
| 33 | static struct sock *nl_socket_handle; |
| 34 | |
| 35 | #ifndef RMNET_KERNEL_PRE_3_8 |
| 36 | static struct netlink_kernel_cfg rmnet_netlink_cfg = { |
| 37 | .input = rmnet_config_netlink_msg_handler |
| 38 | }; |
| 39 | #endif |
| 40 | |
| 41 | static struct notifier_block rmnet_dev_notifier = { |
| 42 | .notifier_call = rmnet_config_notify_cb, |
| 43 | .next = 0, |
| 44 | .priority = 0 |
| 45 | }; |
| 46 | |
| 47 | struct rmnet_free_vnd_work { |
| 48 | struct work_struct work; |
| 49 | int vnd_id[RMNET_DATA_MAX_VND]; |
| 50 | int count; |
| 51 | }; |
| 52 | |
| 53 | /* Init and Cleanup */ |
| 54 | |
| 55 | #ifdef RMNET_KERNEL_PRE_3_8 |
| 56 | static struct sock *_rmnet_config_start_netlink(void) |
| 57 | { |
| 58 | return netlink_kernel_create(&init_net, |
| 59 | RMNET_NETLINK_PROTO, |
| 60 | 0, |
| 61 | rmnet_config_netlink_msg_handler, |
| 62 | NULL, |
| 63 | THIS_MODULE); |
| 64 | } |
| 65 | #else |
| 66 | static struct sock *_rmnet_config_start_netlink(void) |
| 67 | { |
| 68 | return netlink_kernel_create(&init_net, |
| 69 | RMNET_NETLINK_PROTO, |
| 70 | &rmnet_netlink_cfg); |
| 71 | } |
| 72 | #endif /* RMNET_KERNEL_PRE_3_8 */ |
| 73 | |
| 74 | /* rmnet_config_init() - Startup init |
| 75 | * |
| 76 | * Registers netlink protocol with kernel and opens socket. Netlink handler is |
| 77 | * registered with kernel. |
| 78 | */ |
| 79 | int rmnet_config_init(void) |
| 80 | { |
| 81 | int rc; |
| 82 | |
| 83 | nl_socket_handle = _rmnet_config_start_netlink(); |
| 84 | if (!nl_socket_handle) { |
| 85 | LOGE("%s", "Failed to init netlink socket"); |
| 86 | return RMNET_INIT_ERROR; |
| 87 | } |
| 88 | |
| 89 | rc = register_netdevice_notifier(&rmnet_dev_notifier); |
| 90 | if (rc != 0) { |
| 91 | LOGE("Failed to register device notifier; rc=%d", rc); |
| 92 | /* TODO: Cleanup the nl socket */ |
| 93 | return RMNET_INIT_ERROR; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* rmnet_config_exit() - Cleans up all netlink related resources */ |
| 100 | void rmnet_config_exit(void) |
| 101 | { |
| 102 | int rc; |
| 103 | |
| 104 | netlink_kernel_release(nl_socket_handle); |
| 105 | rc = unregister_netdevice_notifier(&rmnet_dev_notifier); |
| 106 | if (rc != 0) |
| 107 | LOGE("Failed to unregister device notifier; rc=%d", rc); |
| 108 | } |
| 109 | |
| 110 | /* Helper Functions */ |
| 111 | |
| 112 | /* _rmnet_is_physical_endpoint_associated() - Determines if device is associated |
| 113 | * @dev: Device to get check |
| 114 | * |
| 115 | * Compares device rx_handler callback pointer against known function |
| 116 | * |
| 117 | * Return: |
| 118 | * - 1 if associated |
| 119 | * - 0 if NOT associated |
| 120 | */ |
| 121 | static inline int _rmnet_is_physical_endpoint_associated(struct net_device *dev) |
| 122 | { |
| 123 | rx_handler_func_t *rx_handler; |
| 124 | |
| 125 | rx_handler = rcu_dereference(dev->rx_handler); |
| 126 | |
| 127 | if (rx_handler == rmnet_rx_handler) |
| 128 | return 1; |
| 129 | else |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* _rmnet_get_phys_ep_config() - Get physical ep config for an associated device |
| 134 | * @dev: Device to get endpoint configuration from |
| 135 | * |
| 136 | * Return: |
| 137 | * - pointer to configuration if successful |
| 138 | * - 0 (null) if device is not associated |
| 139 | */ |
| 140 | struct rmnet_phys_ep_config *_rmnet_get_phys_ep_config |
| 141 | (struct net_device *dev) |
| 142 | { |
| 143 | struct rmnet_phys_ep_conf_s *_rmnet_phys_ep_config; |
| 144 | |
| 145 | if (_rmnet_is_physical_endpoint_associated(dev)) { |
| 146 | _rmnet_phys_ep_config = (struct rmnet_phys_ep_conf_s *) |
| 147 | rcu_dereference(dev->rx_handler_data); |
| 148 | if (_rmnet_phys_ep_config && _rmnet_phys_ep_config->config) |
| 149 | return (struct rmnet_phys_ep_config *) |
| 150 | _rmnet_phys_ep_config->config; |
| 151 | else |
| 152 | return 0; |
| 153 | } else { |
| 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* _rmnet_get_logical_ep() - Gets the logical end point configuration |
| 159 | * structure for a network device |
| 160 | * @dev: Device to get endpoint configuration from |
| 161 | * @config_id: Logical endpoint id on device |
| 162 | * Retrieves the logical_endpoint_config structure. |
| 163 | * |
| 164 | * Return: |
| 165 | * - End point configuration structure |
| 166 | * - NULL in case of an error |
| 167 | */ |
| 168 | struct rmnet_logical_ep_conf_s *_rmnet_get_logical_ep(struct net_device *dev, |
| 169 | int config_id) |
| 170 | { |
| 171 | struct rmnet_phys_ep_config *config; |
| 172 | struct rmnet_logical_ep_conf_s *epconfig_l; |
| 173 | |
| 174 | if (rmnet_vnd_is_vnd(dev)) { |
| 175 | epconfig_l = rmnet_vnd_get_le_config(dev); |
| 176 | } else { |
| 177 | config = _rmnet_get_phys_ep_config(dev); |
| 178 | |
| 179 | if (!config) |
| 180 | return NULL; |
| 181 | |
| 182 | if (config_id == RMNET_LOCAL_LOGICAL_ENDPOINT) |
| 183 | epconfig_l = &config->local_ep; |
| 184 | else |
| 185 | epconfig_l = &config->muxed_ep[config_id]; |
| 186 | } |
| 187 | |
| 188 | return epconfig_l; |
| 189 | } |
| 190 | |
| 191 | /* Netlink Handler */ |
| 192 | static void _rmnet_netlink_set_link_egress_data_format |
| 193 | (struct rmnet_nl_msg_s *rmnet_header, |
| 194 | struct rmnet_nl_msg_s *resp_rmnet) |
| 195 | { |
| 196 | struct net_device *dev; |
| 197 | |
| 198 | if (!rmnet_header || !resp_rmnet) |
| 199 | return; |
| 200 | |
| 201 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 202 | dev = dev_get_by_name(&init_net, rmnet_header->data_format.dev); |
| 203 | |
| 204 | if (!dev) { |
| 205 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | resp_rmnet->return_code = |
| 210 | rmnet_set_egress_data_format(dev, |
| 211 | rmnet_header->data_format.flags, |
| 212 | rmnet_header->data_format.agg_size, |
| 213 | rmnet_header->data_format.agg_count |
| 214 | ); |
| 215 | dev_put(dev); |
| 216 | } |
| 217 | |
| 218 | static void _rmnet_netlink_set_link_ingress_data_format |
| 219 | (struct rmnet_nl_msg_s *rmnet_header, |
| 220 | struct rmnet_nl_msg_s *resp_rmnet) |
| 221 | { |
| 222 | struct net_device *dev; |
| 223 | |
| 224 | if (!rmnet_header || !resp_rmnet) |
| 225 | return; |
| 226 | |
| 227 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 228 | |
| 229 | dev = dev_get_by_name(&init_net, rmnet_header->data_format.dev); |
| 230 | if (!dev) { |
| 231 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | resp_rmnet->return_code = rmnet_set_ingress_data_format( |
| 236 | dev, |
| 237 | rmnet_header->data_format.flags, |
| 238 | rmnet_header->data_format.tail_spacing); |
| 239 | dev_put(dev); |
| 240 | } |
| 241 | |
| 242 | static void _rmnet_netlink_set_logical_ep_config |
| 243 | (struct rmnet_nl_msg_s *rmnet_header, |
| 244 | struct rmnet_nl_msg_s *resp_rmnet) |
| 245 | { |
| 246 | struct net_device *dev, *dev2; |
| 247 | |
| 248 | if (!rmnet_header || !resp_rmnet) |
| 249 | return; |
| 250 | |
| 251 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 252 | if (rmnet_header->local_ep_config.ep_id < -1 || |
| 253 | rmnet_header->local_ep_config.ep_id > 254) { |
| 254 | resp_rmnet->return_code = RMNET_CONFIG_BAD_ARGUMENTS; |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | dev = dev_get_by_name(&init_net, |
| 259 | rmnet_header->local_ep_config.dev); |
| 260 | |
| 261 | dev2 = dev_get_by_name(&init_net, |
| 262 | rmnet_header->local_ep_config.next_dev); |
| 263 | |
| 264 | if (dev && dev2) |
| 265 | resp_rmnet->return_code = |
| 266 | rmnet_set_logical_endpoint_config( |
| 267 | dev, |
| 268 | rmnet_header->local_ep_config.ep_id, |
| 269 | rmnet_header->local_ep_config.operating_mode, |
| 270 | dev2); |
| 271 | else |
| 272 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 273 | |
| 274 | if (dev) |
| 275 | dev_put(dev); |
| 276 | if (dev2) |
| 277 | dev_put(dev2); |
| 278 | } |
| 279 | |
| 280 | static void _rmnet_netlink_unset_logical_ep_config |
| 281 | (struct rmnet_nl_msg_s *rmnet_header, |
| 282 | struct rmnet_nl_msg_s *resp_rmnet) |
| 283 | { |
| 284 | struct net_device *dev; |
| 285 | |
| 286 | if (!rmnet_header || !resp_rmnet) |
| 287 | return; |
| 288 | |
| 289 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 290 | if (rmnet_header->local_ep_config.ep_id < -1 || |
| 291 | rmnet_header->local_ep_config.ep_id > 254) { |
| 292 | resp_rmnet->return_code = RMNET_CONFIG_BAD_ARGUMENTS; |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | dev = dev_get_by_name(&init_net, |
| 297 | rmnet_header->local_ep_config.dev); |
| 298 | |
| 299 | if (dev) { |
| 300 | resp_rmnet->return_code = |
| 301 | rmnet_unset_logical_endpoint_config( |
| 302 | dev, |
| 303 | rmnet_header->local_ep_config.ep_id); |
| 304 | dev_put(dev); |
| 305 | } else { |
| 306 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static void _rmnet_netlink_get_logical_ep_config |
| 311 | (struct rmnet_nl_msg_s *rmnet_header, |
| 312 | struct rmnet_nl_msg_s *resp_rmnet) |
| 313 | { |
| 314 | struct net_device *dev; |
| 315 | |
| 316 | if (!rmnet_header || !resp_rmnet) |
| 317 | return; |
| 318 | |
| 319 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 320 | if (rmnet_header->local_ep_config.ep_id < -1 || |
| 321 | rmnet_header->local_ep_config.ep_id > 254) { |
| 322 | resp_rmnet->return_code = RMNET_CONFIG_BAD_ARGUMENTS; |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | dev = dev_get_by_name(&init_net, |
| 327 | rmnet_header->local_ep_config.dev); |
| 328 | |
| 329 | if (dev) |
| 330 | resp_rmnet->return_code = |
| 331 | rmnet_get_logical_endpoint_config( |
| 332 | dev, |
| 333 | rmnet_header->local_ep_config.ep_id, |
| 334 | &resp_rmnet->local_ep_config.operating_mode, |
| 335 | resp_rmnet->local_ep_config.next_dev, |
| 336 | sizeof(resp_rmnet->local_ep_config.next_dev)); |
| 337 | else { |
| 338 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | if (resp_rmnet->return_code == RMNET_CONFIG_OK) { |
| 343 | /* Begin Data */ |
| 344 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNDATA; |
| 345 | resp_rmnet->arg_length = sizeof(((struct rmnet_nl_msg_s *)0) |
| 346 | ->local_ep_config); |
| 347 | } |
| 348 | dev_put(dev); |
| 349 | } |
| 350 | |
| 351 | static void _rmnet_netlink_associate_network_device |
| 352 | (struct rmnet_nl_msg_s *rmnet_header, |
| 353 | struct rmnet_nl_msg_s *resp_rmnet) |
| 354 | { |
| 355 | struct net_device *dev; |
| 356 | |
| 357 | if (!rmnet_header || !resp_rmnet) |
| 358 | return; |
| 359 | |
| 360 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 361 | |
| 362 | dev = dev_get_by_name(&init_net, rmnet_header->data); |
| 363 | if (!dev) { |
| 364 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | resp_rmnet->return_code = rmnet_associate_network_device(dev); |
| 369 | dev_put(dev); |
| 370 | } |
| 371 | |
| 372 | static void _rmnet_netlink_unassociate_network_device |
| 373 | (struct rmnet_nl_msg_s *rmnet_header, |
| 374 | struct rmnet_nl_msg_s *resp_rmnet) |
| 375 | { |
| 376 | struct net_device *dev; |
| 377 | |
| 378 | if (!rmnet_header || !resp_rmnet) |
| 379 | return; |
| 380 | |
| 381 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 382 | |
| 383 | dev = dev_get_by_name(&init_net, rmnet_header->data); |
| 384 | if (!dev) { |
| 385 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | resp_rmnet->return_code = rmnet_unassociate_network_device(dev); |
| 390 | dev_put(dev); |
| 391 | } |
| 392 | |
| 393 | static void _rmnet_netlink_get_network_device_associated |
| 394 | (struct rmnet_nl_msg_s *rmnet_header, |
| 395 | struct rmnet_nl_msg_s *resp_rmnet) |
| 396 | { |
| 397 | struct net_device *dev; |
| 398 | |
| 399 | if (!rmnet_header || !resp_rmnet) |
| 400 | return; |
| 401 | |
| 402 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 403 | |
| 404 | dev = dev_get_by_name(&init_net, rmnet_header->data); |
| 405 | if (!dev) { |
| 406 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | resp_rmnet->return_code = _rmnet_is_physical_endpoint_associated(dev); |
| 411 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNDATA; |
| 412 | dev_put(dev); |
| 413 | } |
| 414 | |
| 415 | static void _rmnet_netlink_get_link_egress_data_format |
| 416 | (struct rmnet_nl_msg_s *rmnet_header, |
| 417 | struct rmnet_nl_msg_s *resp_rmnet) |
| 418 | { |
| 419 | struct net_device *dev; |
| 420 | struct rmnet_phys_ep_config *config; |
| 421 | |
| 422 | if (!rmnet_header || !resp_rmnet) |
| 423 | return; |
| 424 | |
| 425 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 426 | |
| 427 | dev = dev_get_by_name(&init_net, rmnet_header->data_format.dev); |
| 428 | if (!dev) { |
| 429 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | config = _rmnet_get_phys_ep_config(dev); |
| 434 | if (!config) { |
| 435 | resp_rmnet->return_code = RMNET_CONFIG_INVALID_REQUEST; |
| 436 | dev_put(dev); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | /* Begin Data */ |
| 441 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNDATA; |
| 442 | resp_rmnet->arg_length = sizeof(((struct rmnet_nl_msg_s *)0) |
| 443 | ->data_format); |
| 444 | resp_rmnet->data_format.flags = config->egress_data_format; |
| 445 | resp_rmnet->data_format.agg_count = config->egress_agg_count; |
| 446 | resp_rmnet->data_format.agg_size = config->egress_agg_size; |
| 447 | dev_put(dev); |
| 448 | } |
| 449 | |
| 450 | static void _rmnet_netlink_get_link_ingress_data_format |
| 451 | (struct rmnet_nl_msg_s *rmnet_header, |
| 452 | struct rmnet_nl_msg_s *resp_rmnet) |
| 453 | { |
| 454 | struct net_device *dev; |
| 455 | struct rmnet_phys_ep_config *config; |
| 456 | |
| 457 | if (!rmnet_header || !resp_rmnet) |
| 458 | return; |
| 459 | |
| 460 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 461 | |
| 462 | dev = dev_get_by_name(&init_net, rmnet_header->data_format.dev); |
| 463 | if (!dev) { |
| 464 | resp_rmnet->return_code = RMNET_CONFIG_NO_SUCH_DEVICE; |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | config = _rmnet_get_phys_ep_config(dev); |
| 469 | if (!config) { |
| 470 | resp_rmnet->return_code = RMNET_CONFIG_INVALID_REQUEST; |
| 471 | dev_put(dev); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | /* Begin Data */ |
| 476 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNDATA; |
| 477 | resp_rmnet->arg_length = sizeof(((struct rmnet_nl_msg_s *)0) |
| 478 | ->data_format); |
| 479 | resp_rmnet->data_format.flags = config->ingress_data_format; |
| 480 | resp_rmnet->data_format.tail_spacing = config->tail_spacing; |
| 481 | dev_put(dev); |
| 482 | } |
| 483 | |
| 484 | static void _rmnet_netlink_get_vnd_name |
| 485 | (struct rmnet_nl_msg_s *rmnet_header, |
| 486 | struct rmnet_nl_msg_s *resp_rmnet) |
| 487 | { |
| 488 | int r; |
| 489 | |
| 490 | if (!rmnet_header || !resp_rmnet) |
| 491 | return; |
| 492 | |
| 493 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 494 | |
| 495 | r = rmnet_vnd_get_name(rmnet_header->vnd.id, resp_rmnet->vnd.vnd_name, |
| 496 | RMNET_MAX_STR_LEN); |
| 497 | |
| 498 | if (r != 0) { |
| 499 | resp_rmnet->return_code = RMNET_CONFIG_INVALID_REQUEST; |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | /* Begin Data */ |
| 504 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNDATA; |
| 505 | resp_rmnet->arg_length = sizeof(((struct rmnet_nl_msg_s *)0)->vnd); |
| 506 | } |
| 507 | |
| 508 | static void _rmnet_netlink_add_del_vnd_tc_flow |
| 509 | (u32 command, |
| 510 | struct rmnet_nl_msg_s *rmnet_header, |
| 511 | struct rmnet_nl_msg_s *resp_rmnet) |
| 512 | { |
| 513 | u32 id; |
| 514 | u32 map_flow_id; |
| 515 | u32 tc_flow_id; |
| 516 | |
| 517 | if (!rmnet_header || !resp_rmnet) |
| 518 | return; |
| 519 | |
| 520 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 521 | |
| 522 | id = rmnet_header->flow_control.id; |
| 523 | map_flow_id = rmnet_header->flow_control.map_flow_id; |
| 524 | tc_flow_id = rmnet_header->flow_control.tc_flow_id; |
| 525 | |
| 526 | switch (command) { |
| 527 | case RMNET_NETLINK_ADD_VND_TC_FLOW: |
| 528 | resp_rmnet->return_code = rmnet_vnd_add_tc_flow(id, |
| 529 | map_flow_id, |
| 530 | tc_flow_id); |
| 531 | break; |
| 532 | case RMNET_NETLINK_DEL_VND_TC_FLOW: |
| 533 | resp_rmnet->return_code = rmnet_vnd_del_tc_flow(id, |
| 534 | map_flow_id, |
| 535 | tc_flow_id); |
| 536 | break; |
| 537 | default: |
| 538 | LOGM("Called with unhandled command %d", command); |
| 539 | resp_rmnet->return_code = RMNET_CONFIG_INVALID_REQUEST; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* rmnet_config_netlink_msg_handler() - Netlink message handler callback |
| 545 | * @skb: Packet containing netlink messages |
| 546 | * |
| 547 | * Standard kernel-expected format for a netlink message handler. Processes SKBs |
| 548 | * which contain RmNet data specific netlink messages. |
| 549 | */ |
| 550 | void rmnet_config_netlink_msg_handler(struct sk_buff *skb) |
| 551 | { |
| 552 | struct nlmsghdr *nlmsg_header, *resp_nlmsg; |
| 553 | struct rmnet_nl_msg_s *rmnet_header, *resp_rmnet; |
| 554 | int return_pid, response_data_length; |
| 555 | struct sk_buff *skb_response; |
| 556 | |
| 557 | response_data_length = 0; |
| 558 | nlmsg_header = (struct nlmsghdr *)skb->data; |
| 559 | rmnet_header = (struct rmnet_nl_msg_s *)nlmsg_data(nlmsg_header); |
| 560 | |
Conner Huff | 2357fa5 | 2017-02-03 11:19:02 -0800 | [diff] [blame] | 561 | if (!nlmsg_header->nlmsg_pid || |
| 562 | (nlmsg_header->nlmsg_len < sizeof(struct nlmsghdr) + |
| 563 | sizeof(struct rmnet_nl_msg_s))) |
| 564 | return; |
| 565 | |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 566 | LOGL("Netlink message pid=%d, seq=%d, length=%d, rmnet_type=%d", |
| 567 | nlmsg_header->nlmsg_pid, |
| 568 | nlmsg_header->nlmsg_seq, |
| 569 | nlmsg_header->nlmsg_len, |
| 570 | rmnet_header->message_type); |
| 571 | |
| 572 | return_pid = nlmsg_header->nlmsg_pid; |
| 573 | |
| 574 | skb_response = nlmsg_new(sizeof(struct nlmsghdr) |
| 575 | + sizeof(struct rmnet_nl_msg_s), |
| 576 | GFP_KERNEL); |
| 577 | |
| 578 | if (!skb_response) { |
| 579 | LOGH("%s", "Failed to allocate response buffer"); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | resp_nlmsg = nlmsg_put(skb_response, |
| 584 | 0, |
| 585 | nlmsg_header->nlmsg_seq, |
| 586 | NLMSG_DONE, |
| 587 | sizeof(struct rmnet_nl_msg_s), |
| 588 | 0); |
| 589 | |
| 590 | resp_rmnet = nlmsg_data(resp_nlmsg); |
| 591 | |
| 592 | if (!resp_rmnet) |
| 593 | return; |
| 594 | |
| 595 | resp_rmnet->message_type = rmnet_header->message_type; |
| 596 | rtnl_lock(); |
| 597 | switch (rmnet_header->message_type) { |
| 598 | case RMNET_NETLINK_ASSOCIATE_NETWORK_DEVICE: |
| 599 | _rmnet_netlink_associate_network_device |
| 600 | (rmnet_header, resp_rmnet); |
| 601 | break; |
| 602 | |
| 603 | case RMNET_NETLINK_UNASSOCIATE_NETWORK_DEVICE: |
| 604 | _rmnet_netlink_unassociate_network_device |
| 605 | (rmnet_header, resp_rmnet); |
| 606 | break; |
| 607 | |
| 608 | case RMNET_NETLINK_GET_NETWORK_DEVICE_ASSOCIATED: |
| 609 | _rmnet_netlink_get_network_device_associated |
| 610 | (rmnet_header, resp_rmnet); |
| 611 | break; |
| 612 | |
| 613 | case RMNET_NETLINK_SET_LINK_EGRESS_DATA_FORMAT: |
| 614 | _rmnet_netlink_set_link_egress_data_format |
| 615 | (rmnet_header, resp_rmnet); |
| 616 | break; |
| 617 | |
| 618 | case RMNET_NETLINK_GET_LINK_EGRESS_DATA_FORMAT: |
| 619 | _rmnet_netlink_get_link_egress_data_format |
| 620 | (rmnet_header, resp_rmnet); |
| 621 | break; |
| 622 | |
| 623 | case RMNET_NETLINK_SET_LINK_INGRESS_DATA_FORMAT: |
| 624 | _rmnet_netlink_set_link_ingress_data_format |
| 625 | (rmnet_header, resp_rmnet); |
| 626 | break; |
| 627 | |
| 628 | case RMNET_NETLINK_GET_LINK_INGRESS_DATA_FORMAT: |
| 629 | _rmnet_netlink_get_link_ingress_data_format |
| 630 | (rmnet_header, resp_rmnet); |
| 631 | break; |
| 632 | |
| 633 | case RMNET_NETLINK_SET_LOGICAL_EP_CONFIG: |
| 634 | _rmnet_netlink_set_logical_ep_config(rmnet_header, resp_rmnet); |
| 635 | break; |
| 636 | |
| 637 | case RMNET_NETLINK_UNSET_LOGICAL_EP_CONFIG: |
| 638 | _rmnet_netlink_unset_logical_ep_config(rmnet_header, |
| 639 | resp_rmnet); |
| 640 | break; |
| 641 | |
| 642 | case RMNET_NETLINK_GET_LOGICAL_EP_CONFIG: |
| 643 | _rmnet_netlink_get_logical_ep_config(rmnet_header, resp_rmnet); |
| 644 | break; |
| 645 | |
| 646 | case RMNET_NETLINK_NEW_VND: |
| 647 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 648 | resp_rmnet->return_code = |
| 649 | rmnet_create_vnd(rmnet_header->vnd.id); |
| 650 | break; |
| 651 | |
| 652 | case RMNET_NETLINK_NEW_VND_WITH_PREFIX: |
| 653 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 654 | resp_rmnet->return_code = rmnet_create_vnd_prefix( |
| 655 | rmnet_header->vnd.id, |
| 656 | rmnet_header->vnd.vnd_name); |
| 657 | break; |
| 658 | |
Subash Abhinov Kasiviswanathan | 5859863 | 2017-02-23 18:24:42 -0700 | [diff] [blame] | 659 | case RMNET_NETLINK_NEW_VND_WITH_NAME: |
| 660 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 661 | resp_rmnet->return_code = rmnet_create_vnd_name( |
| 662 | rmnet_header->vnd.id, |
| 663 | rmnet_header->vnd.vnd_name); |
| 664 | break; |
| 665 | |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 666 | case RMNET_NETLINK_FREE_VND: |
| 667 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 668 | /* Please check rmnet_vnd_free_dev documentation regarding |
| 669 | * the below locking sequence |
| 670 | */ |
| 671 | rtnl_unlock(); |
| 672 | resp_rmnet->return_code = rmnet_free_vnd(rmnet_header->vnd.id); |
| 673 | rtnl_lock(); |
| 674 | break; |
| 675 | |
| 676 | case RMNET_NETLINK_GET_VND_NAME: |
| 677 | _rmnet_netlink_get_vnd_name(rmnet_header, resp_rmnet); |
| 678 | break; |
| 679 | |
| 680 | case RMNET_NETLINK_DEL_VND_TC_FLOW: |
| 681 | case RMNET_NETLINK_ADD_VND_TC_FLOW: |
| 682 | _rmnet_netlink_add_del_vnd_tc_flow(rmnet_header->message_type, |
| 683 | rmnet_header, |
| 684 | resp_rmnet); |
| 685 | break; |
| 686 | |
| 687 | default: |
| 688 | resp_rmnet->crd = RMNET_NETLINK_MSG_RETURNCODE; |
| 689 | resp_rmnet->return_code = RMNET_CONFIG_UNKNOWN_MESSAGE; |
| 690 | break; |
| 691 | } |
| 692 | rtnl_unlock(); |
| 693 | nlmsg_unicast(nl_socket_handle, skb_response, return_pid); |
| 694 | LOGD("%s", "Done processing command"); |
| 695 | } |
| 696 | |
| 697 | /* Configuration API */ |
| 698 | |
| 699 | /* rmnet_unassociate_network_device() - Unassociate network device |
| 700 | * @dev: Device to unassociate |
| 701 | * |
| 702 | * Frees all structures generate for device. Unregisters rx_handler |
| 703 | * todo: needs to do some sanity verification first (is device in use, etc...) |
| 704 | * |
| 705 | * Return: |
| 706 | * - RMNET_CONFIG_OK if successful |
| 707 | * - RMNET_CONFIG_NO_SUCH_DEVICE dev is null |
| 708 | * - RMNET_CONFIG_INVALID_REQUEST if device is not already associated |
| 709 | * - RMNET_CONFIG_DEVICE_IN_USE if device has logical ep that wasn't unset |
| 710 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 711 | */ |
| 712 | int rmnet_unassociate_network_device(struct net_device *dev) |
| 713 | { |
| 714 | struct rmnet_phys_ep_conf_s *config; |
| 715 | int config_id = RMNET_LOCAL_LOGICAL_ENDPOINT; |
| 716 | struct rmnet_logical_ep_conf_s *epconfig_l; |
| 717 | |
| 718 | ASSERT_RTNL(); |
| 719 | |
| 720 | LOGL("(%s);", dev->name); |
| 721 | |
| 722 | if (!dev) |
| 723 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 724 | |
| 725 | if (!_rmnet_is_physical_endpoint_associated(dev)) |
| 726 | return RMNET_CONFIG_INVALID_REQUEST; |
| 727 | |
| 728 | for (; config_id < RMNET_DATA_MAX_LOGICAL_EP; config_id++) { |
| 729 | epconfig_l = _rmnet_get_logical_ep(dev, config_id); |
| 730 | if (epconfig_l && epconfig_l->refcount) |
| 731 | return RMNET_CONFIG_DEVICE_IN_USE; |
| 732 | } |
| 733 | |
| 734 | config = (struct rmnet_phys_ep_conf_s *) |
| 735 | rcu_dereference(dev->rx_handler_data); |
| 736 | |
| 737 | if (!config) |
| 738 | return RMNET_CONFIG_UNKNOWN_ERROR; |
| 739 | |
| 740 | kfree(config); |
| 741 | |
| 742 | netdev_rx_handler_unregister(dev); |
| 743 | |
| 744 | /* Explicitly release the reference from the device */ |
| 745 | dev_put(dev); |
| 746 | trace_rmnet_unassociate(dev); |
| 747 | return RMNET_CONFIG_OK; |
| 748 | } |
| 749 | |
| 750 | /* rmnet_set_ingress_data_format() - Set ingress data format on network device |
| 751 | * @dev: Device to ingress data format on |
| 752 | * @egress_data_format: 32-bit unsigned bitmask of ingress format |
| 753 | * |
| 754 | * Network device must already have association with RmNet Data driver |
| 755 | * |
| 756 | * Return: |
| 757 | * - RMNET_CONFIG_OK if successful |
| 758 | * - RMNET_CONFIG_NO_SUCH_DEVICE dev is null |
| 759 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 760 | */ |
| 761 | int rmnet_set_ingress_data_format(struct net_device *dev, |
| 762 | u32 ingress_data_format, |
| 763 | uint8_t tail_spacing) |
| 764 | { |
| 765 | struct rmnet_phys_ep_config *config; |
| 766 | |
| 767 | ASSERT_RTNL(); |
| 768 | |
| 769 | LOGL("(%s,0x%08X);", dev->name, ingress_data_format); |
| 770 | |
| 771 | if (!dev) |
| 772 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 773 | |
| 774 | config = _rmnet_get_phys_ep_config(dev); |
| 775 | |
| 776 | if (!config) |
| 777 | return RMNET_CONFIG_INVALID_REQUEST; |
| 778 | |
| 779 | config->ingress_data_format = ingress_data_format; |
| 780 | config->tail_spacing = tail_spacing; |
| 781 | |
| 782 | return RMNET_CONFIG_OK; |
| 783 | } |
| 784 | |
| 785 | /* rmnet_set_egress_data_format() - Set egress data format on network device |
| 786 | * @dev: Device to egress data format on |
| 787 | * @egress_data_format: 32-bit unsigned bitmask of egress format |
| 788 | * |
| 789 | * Network device must already have association with RmNet Data driver |
| 790 | * todo: Bounds check on agg_* |
| 791 | * |
| 792 | * Return: |
| 793 | * - RMNET_CONFIG_OK if successful |
| 794 | * - RMNET_CONFIG_NO_SUCH_DEVICE dev is null |
| 795 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 796 | */ |
| 797 | int rmnet_set_egress_data_format(struct net_device *dev, |
| 798 | u32 egress_data_format, |
| 799 | u16 agg_size, |
| 800 | u16 agg_count) |
| 801 | { |
| 802 | struct rmnet_phys_ep_config *config; |
| 803 | |
| 804 | ASSERT_RTNL(); |
| 805 | |
| 806 | LOGL("(%s,0x%08X, %d, %d);", |
| 807 | dev->name, egress_data_format, agg_size, agg_count); |
| 808 | |
| 809 | if (!dev) |
| 810 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 811 | |
| 812 | config = _rmnet_get_phys_ep_config(dev); |
| 813 | |
| 814 | if (!config) |
| 815 | return RMNET_CONFIG_UNKNOWN_ERROR; |
| 816 | |
| 817 | config->egress_data_format = egress_data_format; |
| 818 | config->egress_agg_size = agg_size; |
| 819 | config->egress_agg_count = agg_count; |
| 820 | |
| 821 | return RMNET_CONFIG_OK; |
| 822 | } |
| 823 | |
| 824 | /* rmnet_associate_network_device() - Associate network device |
| 825 | * @dev: Device to register with RmNet data |
| 826 | * |
| 827 | * Typically used on physical network devices. Registers RX handler and private |
| 828 | * metadata structures. |
| 829 | * |
| 830 | * Return: |
| 831 | * - RMNET_CONFIG_OK if successful |
| 832 | * - RMNET_CONFIG_NO_SUCH_DEVICE dev is null |
| 833 | * - RMNET_CONFIG_INVALID_REQUEST if the device to be associated is a vnd |
| 834 | * - RMNET_CONFIG_DEVICE_IN_USE if dev rx_handler is already filled |
| 835 | * - RMNET_CONFIG_DEVICE_IN_USE if netdev_rx_handler_register() fails |
| 836 | */ |
| 837 | int rmnet_associate_network_device(struct net_device *dev) |
| 838 | { |
| 839 | struct rmnet_phys_ep_conf_s *config; |
| 840 | struct rmnet_phys_ep_config *conf; |
| 841 | int rc; |
| 842 | |
| 843 | ASSERT_RTNL(); |
| 844 | |
| 845 | LOGL("(%s);\n", dev->name); |
| 846 | |
| 847 | if (!dev) |
| 848 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 849 | |
| 850 | if (_rmnet_is_physical_endpoint_associated(dev)) { |
| 851 | LOGM("%s is already regestered", dev->name); |
| 852 | return RMNET_CONFIG_DEVICE_IN_USE; |
| 853 | } |
| 854 | |
| 855 | if (rmnet_vnd_is_vnd(dev)) { |
| 856 | LOGM("%s is a vnd", dev->name); |
| 857 | return RMNET_CONFIG_INVALID_REQUEST; |
| 858 | } |
| 859 | |
| 860 | config = kmalloc(sizeof(*config), GFP_ATOMIC); |
| 861 | conf = kmalloc(sizeof(*conf), GFP_ATOMIC); |
| 862 | |
| 863 | if (!config || !conf) |
| 864 | return RMNET_CONFIG_NOMEM; |
| 865 | |
| 866 | memset(config, 0, sizeof(struct rmnet_phys_ep_conf_s)); |
| 867 | memset(conf, 0, sizeof(struct rmnet_phys_ep_config)); |
| 868 | |
| 869 | config->config = conf; |
| 870 | conf->dev = dev; |
| 871 | spin_lock_init(&conf->agg_lock); |
| 872 | config->recycle = kfree_skb; |
Subash Abhinov Kasiviswanathan | 08fed02 | 2017-11-17 17:54:49 -0700 | [diff] [blame] | 873 | hrtimer_init(&conf->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 874 | conf->hrtimer.function = rmnet_map_flush_packet_queue; |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 875 | rc = netdev_rx_handler_register(dev, rmnet_rx_handler, config); |
| 876 | |
| 877 | if (rc) { |
| 878 | LOGM("netdev_rx_handler_register returns %d", rc); |
| 879 | kfree(config); |
| 880 | kfree(conf); |
| 881 | return RMNET_CONFIG_DEVICE_IN_USE; |
| 882 | } |
| 883 | |
| 884 | /* Explicitly hold a reference to the device */ |
| 885 | dev_hold(dev); |
| 886 | trace_rmnet_associate(dev); |
| 887 | return RMNET_CONFIG_OK; |
| 888 | } |
| 889 | |
| 890 | /* _rmnet_set_logical_endpoint_config() - Set logical endpoing config on device |
| 891 | * @dev: Device to set endpoint configuration on |
| 892 | * @config_id: logical endpoint id on device |
| 893 | * @epconfig: endpoing configuration structure to set |
| 894 | * |
| 895 | * Return: |
| 896 | * - RMNET_CONFIG_OK if successful |
| 897 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 898 | * - RMNET_CONFIG_NO_SUCH_DEVICE if device to set config on is null |
| 899 | * - RMNET_CONFIG_DEVICE_IN_USE if device already has a logical ep |
| 900 | * - RMNET_CONFIG_BAD_ARGUMENTS if logical endpoint id is out of range |
| 901 | */ |
| 902 | int _rmnet_set_logical_endpoint_config(struct net_device *dev, |
| 903 | int config_id, |
| 904 | struct rmnet_logical_ep_conf_s *epconfig) |
| 905 | { |
| 906 | struct rmnet_logical_ep_conf_s *epconfig_l; |
| 907 | |
| 908 | ASSERT_RTNL(); |
| 909 | |
| 910 | if (!dev) |
| 911 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 912 | |
| 913 | if (config_id < RMNET_LOCAL_LOGICAL_ENDPOINT || |
| 914 | config_id >= RMNET_DATA_MAX_LOGICAL_EP) |
| 915 | return RMNET_CONFIG_BAD_ARGUMENTS; |
| 916 | |
| 917 | epconfig_l = _rmnet_get_logical_ep(dev, config_id); |
| 918 | |
| 919 | if (!epconfig_l) |
| 920 | return RMNET_CONFIG_UNKNOWN_ERROR; |
| 921 | |
| 922 | if (epconfig_l->refcount) |
| 923 | return RMNET_CONFIG_DEVICE_IN_USE; |
| 924 | |
| 925 | memcpy(epconfig_l, epconfig, sizeof(struct rmnet_logical_ep_conf_s)); |
| 926 | if (config_id == RMNET_LOCAL_LOGICAL_ENDPOINT) |
| 927 | epconfig_l->mux_id = 0; |
| 928 | else |
| 929 | epconfig_l->mux_id = config_id; |
| 930 | |
| 931 | /* Explicitly hold a reference to the egress device */ |
| 932 | dev_hold(epconfig_l->egress_dev); |
| 933 | return RMNET_CONFIG_OK; |
| 934 | } |
| 935 | |
| 936 | /* _rmnet_unset_logical_endpoint_config() - Un-set the logical endpoing config |
| 937 | * on device |
| 938 | * @dev: Device to set endpoint configuration on |
| 939 | * @config_id: logical endpoint id on device |
| 940 | * |
| 941 | * Return: |
| 942 | * - RMNET_CONFIG_OK if successful |
| 943 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 944 | * - RMNET_CONFIG_NO_SUCH_DEVICE if device to set config on is null |
| 945 | * - RMNET_CONFIG_BAD_ARGUMENTS if logical endpoint id is out of range |
| 946 | */ |
| 947 | int _rmnet_unset_logical_endpoint_config(struct net_device *dev, |
| 948 | int config_id) |
| 949 | { |
| 950 | struct rmnet_logical_ep_conf_s *epconfig_l = 0; |
| 951 | |
| 952 | ASSERT_RTNL(); |
| 953 | |
| 954 | if (!dev) |
| 955 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 956 | |
| 957 | if (config_id < RMNET_LOCAL_LOGICAL_ENDPOINT || |
| 958 | config_id >= RMNET_DATA_MAX_LOGICAL_EP) |
| 959 | return RMNET_CONFIG_BAD_ARGUMENTS; |
| 960 | |
| 961 | epconfig_l = _rmnet_get_logical_ep(dev, config_id); |
| 962 | |
| 963 | if (!epconfig_l || !epconfig_l->refcount) |
| 964 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 965 | |
| 966 | /* Explicitly release the reference from the egress device */ |
| 967 | dev_put(epconfig_l->egress_dev); |
| 968 | memset(epconfig_l, 0, sizeof(struct rmnet_logical_ep_conf_s)); |
| 969 | |
| 970 | return RMNET_CONFIG_OK; |
| 971 | } |
| 972 | |
| 973 | /* rmnet_set_logical_endpoint_config() - Set logical endpoint config on a device |
| 974 | * @dev: Device to set endpoint configuration on |
| 975 | * @config_id: logical endpoint id on device |
| 976 | * @rmnet_mode: endpoint mode. Values from: rmnet_config_endpoint_modes_e |
| 977 | * @egress_device: device node to forward packet to once done processing in |
| 978 | * ingress/egress handlers |
| 979 | * |
| 980 | * Creates a logical_endpoint_config structure and fills in the information from |
| 981 | * function arguments. Calls _rmnet_set_logical_endpoint_config() to finish |
| 982 | * configuration. Network device must already have association with RmNet Data |
| 983 | * driver |
| 984 | * |
| 985 | * Return: |
| 986 | * - RMNET_CONFIG_OK if successful |
| 987 | * - RMNET_CONFIG_BAD_EGRESS_DEVICE if egress device is null |
| 988 | * - RMNET_CONFIG_BAD_EGRESS_DEVICE if egress device is not handled by |
| 989 | * RmNet data module |
| 990 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 991 | * - RMNET_CONFIG_NO_SUCH_DEVICE if device to set config on is null |
| 992 | * - RMNET_CONFIG_BAD_ARGUMENTS if logical endpoint id is out of range |
| 993 | */ |
| 994 | int rmnet_set_logical_endpoint_config(struct net_device *dev, |
| 995 | int config_id, |
| 996 | u8 rmnet_mode, |
| 997 | struct net_device *egress_dev) |
| 998 | { |
| 999 | struct rmnet_logical_ep_conf_s epconfig; |
| 1000 | |
| 1001 | LOGL("(%s, %d, %d, %s);", |
| 1002 | dev->name, config_id, rmnet_mode, egress_dev->name); |
| 1003 | |
| 1004 | if (!egress_dev || |
| 1005 | ((!_rmnet_is_physical_endpoint_associated(egress_dev)) && |
| 1006 | (!rmnet_vnd_is_vnd(egress_dev)))) { |
| 1007 | return RMNET_CONFIG_BAD_EGRESS_DEVICE; |
| 1008 | } |
| 1009 | |
| 1010 | memset(&epconfig, 0, sizeof(struct rmnet_logical_ep_conf_s)); |
| 1011 | epconfig.refcount = 1; |
| 1012 | epconfig.rmnet_mode = rmnet_mode; |
| 1013 | epconfig.egress_dev = egress_dev; |
| 1014 | |
| 1015 | return _rmnet_set_logical_endpoint_config(dev, config_id, &epconfig); |
| 1016 | } |
| 1017 | |
| 1018 | /* rmnet_unset_logical_endpoint_config() - Un-set logical endpoing configuration |
| 1019 | * on a device |
| 1020 | * @dev: Device to set endpoint configuration on |
| 1021 | * @config_id: logical endpoint id on device |
| 1022 | * |
| 1023 | * Retrieves the logical_endpoint_config structure and frees the egress device. |
| 1024 | * Network device must already have association with RmNet Data driver |
| 1025 | * |
| 1026 | * Return: |
| 1027 | * - RMNET_CONFIG_OK if successful |
| 1028 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 1029 | * - RMNET_CONFIG_NO_SUCH_DEVICE device is not associated |
| 1030 | * - RMNET_CONFIG_BAD_ARGUMENTS if logical endpoint id is out of range |
| 1031 | */ |
| 1032 | int rmnet_unset_logical_endpoint_config(struct net_device *dev, |
| 1033 | int config_id) |
| 1034 | { |
| 1035 | LOGL("(%s, %d);", dev->name, config_id); |
| 1036 | |
| 1037 | if (!dev || ((!_rmnet_is_physical_endpoint_associated(dev)) && |
| 1038 | (!rmnet_vnd_is_vnd(dev)))) { |
| 1039 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 1040 | } |
| 1041 | |
| 1042 | return _rmnet_unset_logical_endpoint_config(dev, config_id); |
| 1043 | } |
| 1044 | |
| 1045 | /* rmnet_get_logical_endpoint_config() - Gets logical endpoing configuration |
| 1046 | * for a device |
| 1047 | * @dev: Device to get endpoint configuration on |
| 1048 | * @config_id: logical endpoint id on device |
| 1049 | * @rmnet_mode: (I/O) logical endpoint mode |
| 1050 | * @egress_dev_name: (I/O) logical endpoint egress device name |
| 1051 | * @egress_dev_name_size: The maximal size of the I/O egress_dev_name |
| 1052 | * |
| 1053 | * Retrieves the logical_endpoint_config structure. |
| 1054 | * Network device must already have association with RmNet Data driver |
| 1055 | * |
| 1056 | * Return: |
| 1057 | * - RMNET_CONFIG_OK if successful |
| 1058 | * - RMNET_CONFIG_UNKNOWN_ERROR net_device private section is null |
| 1059 | * - RMNET_CONFIG_NO_SUCH_DEVICE device is not associated |
| 1060 | * - RMNET_CONFIG_BAD_ARGUMENTS if logical endpoint id is out of range or |
| 1061 | * if the provided buffer size for egress dev name is too short |
| 1062 | */ |
| 1063 | int rmnet_get_logical_endpoint_config(struct net_device *dev, |
| 1064 | int config_id, |
| 1065 | u8 *rmnet_mode, |
| 1066 | u8 *egress_dev_name, |
| 1067 | size_t egress_dev_name_size) |
| 1068 | { |
| 1069 | struct rmnet_logical_ep_conf_s *epconfig_l = 0; |
| 1070 | size_t strlcpy_res = 0; |
| 1071 | |
| 1072 | LOGL("(%s, %d);", dev->name, config_id); |
| 1073 | |
| 1074 | if (!egress_dev_name || !rmnet_mode) |
| 1075 | return RMNET_CONFIG_BAD_ARGUMENTS; |
| 1076 | if (config_id < RMNET_LOCAL_LOGICAL_ENDPOINT || |
| 1077 | config_id >= RMNET_DATA_MAX_LOGICAL_EP) |
| 1078 | return RMNET_CONFIG_BAD_ARGUMENTS; |
| 1079 | |
| 1080 | epconfig_l = _rmnet_get_logical_ep(dev, config_id); |
| 1081 | |
| 1082 | if (!epconfig_l || !epconfig_l->refcount) |
| 1083 | return RMNET_CONFIG_NO_SUCH_DEVICE; |
| 1084 | |
| 1085 | *rmnet_mode = epconfig_l->rmnet_mode; |
| 1086 | |
| 1087 | strlcpy_res = strlcpy(egress_dev_name, epconfig_l->egress_dev->name, |
| 1088 | egress_dev_name_size); |
| 1089 | |
| 1090 | if (strlcpy_res >= egress_dev_name_size) |
| 1091 | return RMNET_CONFIG_BAD_ARGUMENTS; |
| 1092 | |
| 1093 | return RMNET_CONFIG_OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* rmnet_create_vnd() - Create virtual network device node |
| 1097 | * @id: RmNet virtual device node id |
| 1098 | * |
| 1099 | * Return: |
| 1100 | * - result of rmnet_vnd_create_dev() |
| 1101 | */ |
| 1102 | int rmnet_create_vnd(int id) |
| 1103 | { |
| 1104 | struct net_device *dev; |
| 1105 | |
| 1106 | ASSERT_RTNL(); |
| 1107 | LOGL("(%d);", id); |
Subash Abhinov Kasiviswanathan | 5859863 | 2017-02-23 18:24:42 -0700 | [diff] [blame] | 1108 | return rmnet_vnd_create_dev(id, &dev, NULL, 0); |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 1109 | } |
| 1110 | |
Subash Abhinov Kasiviswanathan | 5859863 | 2017-02-23 18:24:42 -0700 | [diff] [blame] | 1111 | /* rmnet_create_vnd_prefix() - Create virtual network device node |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 1112 | * @id: RmNet virtual device node id |
| 1113 | * @prefix: String prefix for device name |
| 1114 | * |
| 1115 | * Return: |
| 1116 | * - result of rmnet_vnd_create_dev() |
| 1117 | */ |
| 1118 | int rmnet_create_vnd_prefix(int id, const char *prefix) |
| 1119 | { |
| 1120 | struct net_device *dev; |
| 1121 | |
| 1122 | ASSERT_RTNL(); |
| 1123 | LOGL("(%d, \"%s\");", id, prefix); |
Subash Abhinov Kasiviswanathan | 5859863 | 2017-02-23 18:24:42 -0700 | [diff] [blame] | 1124 | return rmnet_vnd_create_dev(id, &dev, prefix, 0); |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * rmnet_create_vnd_name() - Create virtual network device node |
| 1129 | * @id: RmNet virtual device node id |
| 1130 | * @prefix: String prefix for device name |
| 1131 | * |
| 1132 | * Return: |
| 1133 | * - result of rmnet_vnd_create_dev() |
| 1134 | */ |
| 1135 | int rmnet_create_vnd_name(int id, const char *name) |
| 1136 | { |
| 1137 | struct net_device *dev; |
| 1138 | |
| 1139 | ASSERT_RTNL(); |
| 1140 | LOGL("(%d, \"%s\");", id, name); |
| 1141 | return rmnet_vnd_create_dev(id, &dev, name, 1); |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* rmnet_free_vnd() - Free virtual network device node |
| 1145 | * @id: RmNet virtual device node id |
| 1146 | * |
| 1147 | * Return: |
| 1148 | * - result of rmnet_vnd_free_dev() |
| 1149 | */ |
| 1150 | int rmnet_free_vnd(int id) |
| 1151 | { |
| 1152 | LOGL("(%d);", id); |
| 1153 | return rmnet_vnd_free_dev(id); |
| 1154 | } |
| 1155 | |
| 1156 | static void _rmnet_free_vnd_later(struct work_struct *work) |
| 1157 | { |
| 1158 | int i; |
| 1159 | struct rmnet_free_vnd_work *fwork; |
| 1160 | |
| 1161 | fwork = container_of(work, struct rmnet_free_vnd_work, work); |
| 1162 | |
| 1163 | for (i = 0; i < fwork->count; i++) |
| 1164 | rmnet_free_vnd(fwork->vnd_id[i]); |
| 1165 | kfree(fwork); |
| 1166 | } |
| 1167 | |
| 1168 | /* rmnet_force_unassociate_device() - Force a device to unassociate |
| 1169 | * @dev: Device to unassociate |
| 1170 | * |
| 1171 | * Return: |
| 1172 | * - void |
| 1173 | */ |
| 1174 | static void rmnet_force_unassociate_device(struct net_device *dev) |
| 1175 | { |
| 1176 | int i, j; |
| 1177 | struct net_device *vndev; |
Ashwanth Goli | c85bfbe | 2017-02-24 11:03:03 -0700 | [diff] [blame] | 1178 | struct rmnet_phys_ep_config *config; |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 1179 | struct rmnet_logical_ep_conf_s *cfg; |
| 1180 | struct rmnet_free_vnd_work *vnd_work; |
| 1181 | |
| 1182 | ASSERT_RTNL(); |
| 1183 | |
| 1184 | if (!dev) |
| 1185 | return; |
| 1186 | |
| 1187 | if (!_rmnet_is_physical_endpoint_associated(dev)) { |
| 1188 | LOGM("%s", "Called on unassociated device, skipping"); |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | trace_rmnet_unregister_cb_clear_vnds(dev); |
| 1193 | vnd_work = kmalloc(sizeof(*vnd_work), GFP_KERNEL); |
| 1194 | if (!vnd_work) { |
| 1195 | LOGH("%s", "Out of Memory"); |
| 1196 | return; |
| 1197 | } |
| 1198 | INIT_WORK(&vnd_work->work, _rmnet_free_vnd_later); |
| 1199 | vnd_work->count = 0; |
| 1200 | |
| 1201 | /* Check the VNDs for offending mappings */ |
| 1202 | for (i = 0, j = 0; i < RMNET_DATA_MAX_VND && |
| 1203 | j < RMNET_DATA_MAX_VND; i++) { |
| 1204 | vndev = rmnet_vnd_get_by_id(i); |
| 1205 | if (!vndev) { |
| 1206 | LOGL("VND %d not in use; skipping", i); |
| 1207 | continue; |
| 1208 | } |
| 1209 | cfg = rmnet_vnd_get_le_config(vndev); |
| 1210 | if (!cfg) { |
| 1211 | LOGH("Got NULL config from VND %d", i); |
| 1212 | continue; |
| 1213 | } |
| 1214 | if (cfg->refcount && (cfg->egress_dev == dev)) { |
| 1215 | /* Make sure the device is down before clearing any of |
| 1216 | * the mappings. Otherwise we could see a potential |
| 1217 | * race condition if packets are actively being |
| 1218 | * transmitted. |
| 1219 | */ |
| 1220 | dev_close(vndev); |
| 1221 | rmnet_unset_logical_endpoint_config |
| 1222 | (vndev, RMNET_LOCAL_LOGICAL_ENDPOINT); |
| 1223 | vnd_work->vnd_id[j] = i; |
| 1224 | j++; |
| 1225 | } |
| 1226 | } |
| 1227 | if (j > 0) { |
| 1228 | vnd_work->count = j; |
| 1229 | schedule_work(&vnd_work->work); |
| 1230 | } else { |
| 1231 | kfree(vnd_work); |
| 1232 | } |
| 1233 | |
Ashwanth Goli | c85bfbe | 2017-02-24 11:03:03 -0700 | [diff] [blame] | 1234 | config = _rmnet_get_phys_ep_config(dev); |
| 1235 | |
| 1236 | if (config) { |
Subash Abhinov Kasiviswanathan | 08fed02 | 2017-11-17 17:54:49 -0700 | [diff] [blame] | 1237 | unsigned long flags; |
| 1238 | |
| 1239 | hrtimer_cancel(&config->hrtimer); |
| 1240 | spin_lock_irqsave(&config->agg_lock, flags); |
| 1241 | if (config->agg_state == RMNET_MAP_TXFER_SCHEDULED) { |
| 1242 | if (config->agg_skb) { |
| 1243 | kfree_skb(config->agg_skb); |
| 1244 | config->agg_skb = NULL; |
| 1245 | config->agg_count = 0; |
| 1246 | memset(&config->agg_time, 0, |
| 1247 | sizeof(struct timespec)); |
| 1248 | } |
| 1249 | config->agg_state = RMNET_MAP_AGG_IDLE; |
| 1250 | } |
| 1251 | spin_unlock_irqrestore(&config->agg_lock, flags); |
| 1252 | |
Ashwanth Goli | c85bfbe | 2017-02-24 11:03:03 -0700 | [diff] [blame] | 1253 | cfg = &config->local_ep; |
| 1254 | |
| 1255 | if (cfg && cfg->refcount) |
| 1256 | rmnet_unset_logical_endpoint_config |
| 1257 | (cfg->egress_dev, RMNET_LOCAL_LOGICAL_ENDPOINT); |
| 1258 | } |
| 1259 | |
Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame] | 1260 | /* Clear the mappings on the phys ep */ |
| 1261 | trace_rmnet_unregister_cb_clear_lepcs(dev); |
| 1262 | rmnet_unset_logical_endpoint_config(dev, RMNET_LOCAL_LOGICAL_ENDPOINT); |
| 1263 | for (i = 0; i < RMNET_DATA_MAX_LOGICAL_EP; i++) |
| 1264 | rmnet_unset_logical_endpoint_config(dev, i); |
| 1265 | rmnet_unassociate_network_device(dev); |
| 1266 | } |
| 1267 | |
| 1268 | /* rmnet_config_notify_cb() - Callback for netdevice notifier chain |
| 1269 | * @nb: Notifier block data |
| 1270 | * @event: Netdevice notifier event ID |
| 1271 | * @data: Contains a net device for which we are getting notified |
| 1272 | * |
| 1273 | * Return: |
| 1274 | * - result of NOTIFY_DONE() |
| 1275 | */ |
| 1276 | int rmnet_config_notify_cb(struct notifier_block *nb, |
| 1277 | unsigned long event, void *data) |
| 1278 | { |
| 1279 | struct net_device *dev = netdev_notifier_info_to_dev(data); |
| 1280 | |
| 1281 | if (!dev) |
| 1282 | return NOTIFY_DONE; |
| 1283 | |
| 1284 | LOGL("(..., %lu, %s)", event, dev->name); |
| 1285 | |
| 1286 | switch (event) { |
| 1287 | case NETDEV_UNREGISTER_FINAL: |
| 1288 | case NETDEV_UNREGISTER: |
| 1289 | trace_rmnet_unregister_cb_entry(dev); |
| 1290 | LOGH("Kernel is trying to unregister %s", dev->name); |
| 1291 | rmnet_force_unassociate_device(dev); |
| 1292 | trace_rmnet_unregister_cb_exit(dev); |
| 1293 | break; |
| 1294 | |
| 1295 | default: |
| 1296 | trace_rmnet_unregister_cb_unhandled(dev); |
| 1297 | LOGD("Unhandeled event [%lu]", event); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | return NOTIFY_DONE; |
| 1302 | } |