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