1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner, Simon Wunderlich 5 */ 6 7 #include "main.h" 8 9 #include <linux/array_size.h> 10 #include <linux/atomic.h> 11 #include <linux/build_bug.h> 12 #include <linux/byteorder/generic.h> 13 #include <linux/container_of.h> 14 #include <linux/device.h> 15 #include <linux/errno.h> 16 #include <linux/gfp.h> 17 #include <linux/if_ether.h> 18 #include <linux/if_vlan.h> 19 #include <linux/init.h> 20 #include <linux/ip.h> 21 #include <linux/ipv6.h> 22 #include <linux/kobject.h> 23 #include <linux/kref.h> 24 #include <linux/list.h> 25 #include <linux/minmax.h> 26 #include <linux/module.h> 27 #include <linux/netdevice.h> 28 #include <linux/printk.h> 29 #include <linux/rcupdate.h> 30 #include <linux/skbuff.h> 31 #include <linux/slab.h> 32 #include <linux/spinlock.h> 33 #include <linux/sprintf.h> 34 #include <linux/stddef.h> 35 #include <linux/string.h> 36 #include <linux/workqueue.h> 37 #include <net/dsfield.h> 38 #include <net/genetlink.h> 39 #include <net/rtnetlink.h> 40 #include <uapi/linux/batadv_packet.h> 41 #include <uapi/linux/batman_adv.h> 42 43 #include "bat_algo.h" 44 #include "bat_iv_ogm.h" 45 #include "bat_v.h" 46 #include "bridge_loop_avoidance.h" 47 #include "distributed-arp-table.h" 48 #include "gateway_client.h" 49 #include "gateway_common.h" 50 #include "hard-interface.h" 51 #include "log.h" 52 #include "mesh-interface.h" 53 #include "multicast.h" 54 #include "netlink.h" 55 #include "originator.h" 56 #include "routing.h" 57 #include "send.h" 58 #include "tp_meter.h" 59 #include "translation-table.h" 60 61 /* List manipulations on hardif_list have to be rtnl_lock()'ed, 62 * list traversals just rcu-locked 63 */ 64 struct list_head batadv_hardif_list; 65 unsigned int batadv_hardif_generation; 66 static int (*batadv_rx_handler[256])(struct sk_buff *skb, 67 struct batadv_hard_iface *recv_if); 68 69 struct workqueue_struct *batadv_event_workqueue; 70 71 static void batadv_recv_handler_init(void); 72 73 #define BATADV_UEV_TYPE_VAR "BATTYPE=" 74 #define BATADV_UEV_ACTION_VAR "BATACTION=" 75 #define BATADV_UEV_DATA_VAR "BATDATA=" 76 77 static char *batadv_uev_action_str[] = { 78 "add", 79 "del", 80 "change", 81 "loopdetect", 82 }; 83 84 static char *batadv_uev_type_str[] = { 85 "gw", 86 "bla", 87 }; 88 89 static int __init batadv_init(void) 90 { 91 int ret; 92 93 ret = batadv_tt_cache_init(); 94 if (ret < 0) 95 return ret; 96 97 INIT_LIST_HEAD(&batadv_hardif_list); 98 batadv_algo_init(); 99 100 batadv_recv_handler_init(); 101 102 batadv_v_init(); 103 batadv_iv_init(); 104 batadv_tp_meter_init(); 105 106 batadv_event_workqueue = create_singlethread_workqueue("bat_events"); 107 if (!batadv_event_workqueue) 108 goto err_create_wq; 109 110 register_netdevice_notifier(&batadv_hard_if_notifier); 111 rtnl_link_register(&batadv_link_ops); 112 batadv_netlink_register(); 113 114 pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n", 115 BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION); 116 117 return 0; 118 119 err_create_wq: 120 batadv_tt_cache_destroy(); 121 122 return -ENOMEM; 123 } 124 125 static void __exit batadv_exit(void) 126 { 127 batadv_netlink_unregister(); 128 rtnl_link_unregister(&batadv_link_ops); 129 unregister_netdevice_notifier(&batadv_hard_if_notifier); 130 131 destroy_workqueue(batadv_event_workqueue); 132 batadv_event_workqueue = NULL; 133 134 rcu_barrier(); 135 136 batadv_tt_cache_destroy(); 137 } 138 139 /** 140 * batadv_mesh_init() - Initialize mesh interface 141 * @mesh_iface: netdev struct of the mesh interface 142 * 143 * Return: 0 on success or negative error number in case of failure 144 */ 145 int batadv_mesh_init(struct net_device *mesh_iface) 146 { 147 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 148 int ret; 149 150 spin_lock_init(&bat_priv->forw_bat_list_lock); 151 spin_lock_init(&bat_priv->forw_bcast_list_lock); 152 spin_lock_init(&bat_priv->tt.changes_list_lock); 153 spin_lock_init(&bat_priv->tt.req_list_lock); 154 spin_lock_init(&bat_priv->tt.roam_list_lock); 155 spin_lock_init(&bat_priv->tt.last_changeset_lock); 156 spin_lock_init(&bat_priv->tt.commit_lock); 157 spin_lock_init(&bat_priv->gw.list_lock); 158 #ifdef CONFIG_BATMAN_ADV_MCAST 159 spin_lock_init(&bat_priv->mcast.mla_lock); 160 spin_lock_init(&bat_priv->mcast.want_lists_lock); 161 #endif 162 spin_lock_init(&bat_priv->tvlv.container_list_lock); 163 spin_lock_init(&bat_priv->tvlv.handler_list_lock); 164 spin_lock_init(&bat_priv->meshif_vlan_list_lock); 165 spin_lock_init(&bat_priv->tp_list_lock); 166 167 INIT_HLIST_HEAD(&bat_priv->forw_bat_list); 168 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list); 169 INIT_HLIST_HEAD(&bat_priv->gw.gateway_list); 170 #ifdef CONFIG_BATMAN_ADV_MCAST 171 INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list); 172 INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list); 173 INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list); 174 #endif 175 INIT_LIST_HEAD(&bat_priv->tt.changes_list); 176 INIT_HLIST_HEAD(&bat_priv->tt.req_list); 177 INIT_LIST_HEAD(&bat_priv->tt.roam_list); 178 #ifdef CONFIG_BATMAN_ADV_MCAST 179 INIT_HLIST_HEAD(&bat_priv->mcast.mla_list); 180 #endif 181 INIT_HLIST_HEAD(&bat_priv->tvlv.container_list); 182 INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list); 183 INIT_HLIST_HEAD(&bat_priv->meshif_vlan_list); 184 INIT_HLIST_HEAD(&bat_priv->tp_list); 185 186 bat_priv->gw.generation = 0; 187 188 ret = batadv_originator_init(bat_priv); 189 if (ret < 0) { 190 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 191 goto err_orig; 192 } 193 194 ret = batadv_tt_init(bat_priv); 195 if (ret < 0) { 196 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 197 goto err_tt; 198 } 199 200 ret = batadv_v_mesh_init(bat_priv); 201 if (ret < 0) { 202 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 203 goto err_v; 204 } 205 206 ret = batadv_bla_init(bat_priv); 207 if (ret < 0) { 208 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 209 goto err_bla; 210 } 211 212 ret = batadv_dat_init(bat_priv); 213 if (ret < 0) { 214 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 215 goto err_dat; 216 } 217 218 batadv_gw_init(bat_priv); 219 batadv_mcast_init(bat_priv); 220 221 atomic_set(&bat_priv->gw.reselect, 0); 222 atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE); 223 224 return 0; 225 226 err_dat: 227 batadv_bla_free(bat_priv); 228 err_bla: 229 batadv_v_mesh_free(bat_priv); 230 err_v: 231 batadv_tt_free(bat_priv); 232 err_tt: 233 batadv_originator_free(bat_priv); 234 err_orig: 235 batadv_purge_outstanding_packets(bat_priv, NULL); 236 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE); 237 238 return ret; 239 } 240 241 /** 242 * batadv_mesh_free() - Deinitialize mesh interface 243 * @mesh_iface: netdev struct of the mesh interface 244 */ 245 void batadv_mesh_free(struct net_device *mesh_iface) 246 { 247 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 248 249 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); 250 251 batadv_purge_outstanding_packets(bat_priv, NULL); 252 batadv_tp_stop_all(bat_priv); 253 254 batadv_gw_node_free(bat_priv); 255 256 batadv_v_mesh_free(bat_priv); 257 batadv_dat_free(bat_priv); 258 batadv_bla_free(bat_priv); 259 260 batadv_mcast_free(bat_priv); 261 262 /* Free the TT and the originator tables only after having terminated 263 * all the other depending components which may use these structures for 264 * their purposes. 265 */ 266 batadv_tt_free(bat_priv); 267 268 /* Since the originator table clean up routine is accessing the TT 269 * tables as well, it has to be invoked after the TT tables have been 270 * freed and marked as empty. This ensures that no cleanup RCU callbacks 271 * accessing the TT data are scheduled for later execution. 272 */ 273 batadv_originator_free(bat_priv); 274 275 batadv_gw_free(bat_priv); 276 277 free_percpu(bat_priv->bat_counters); 278 bat_priv->bat_counters = NULL; 279 280 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE); 281 } 282 283 /** 284 * batadv_is_my_mac() - check if the given mac address belongs to any of the 285 * real interfaces in the current mesh 286 * @bat_priv: the bat priv with all the mesh interface information 287 * @addr: the address to check 288 * 289 * Return: 'true' if the mac address was found, false otherwise. 290 */ 291 bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr) 292 { 293 const struct batadv_hard_iface *hard_iface; 294 struct list_head *iter; 295 bool is_my_mac = false; 296 297 rcu_read_lock(); 298 netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) { 299 if (hard_iface->if_status != BATADV_IF_ACTIVE) 300 continue; 301 302 if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) { 303 is_my_mac = true; 304 break; 305 } 306 } 307 rcu_read_unlock(); 308 return is_my_mac; 309 } 310 311 /** 312 * batadv_max_header_len() - calculate maximum encapsulation overhead for a 313 * payload packet 314 * 315 * Return: the maximum encapsulation overhead in bytes. 316 */ 317 int batadv_max_header_len(void) 318 { 319 int header_len = 0; 320 321 header_len = max_t(int, header_len, 322 sizeof(struct batadv_unicast_packet)); 323 header_len = max_t(int, header_len, 324 sizeof(struct batadv_unicast_4addr_packet)); 325 header_len = max_t(int, header_len, 326 sizeof(struct batadv_bcast_packet)); 327 328 return header_len + ETH_HLEN; 329 } 330 331 /** 332 * batadv_skb_set_priority() - sets skb priority according to packet content 333 * @skb: the packet to be sent 334 * @offset: offset to the packet content 335 * 336 * This function sets a value between 256 and 263 (802.1d priority), which 337 * can be interpreted by the cfg80211 or other drivers. 338 */ 339 void batadv_skb_set_priority(struct sk_buff *skb, int offset) 340 { 341 struct iphdr ip_hdr_tmp, *ip_hdr; 342 struct ipv6hdr ip6_hdr_tmp, *ip6_hdr; 343 struct ethhdr ethhdr_tmp, *ethhdr; 344 struct vlan_ethhdr *vhdr, vhdr_tmp; 345 u32 prio; 346 347 /* already set, do nothing */ 348 if (skb->priority >= 256 && skb->priority <= 263) 349 return; 350 351 ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), ðhdr_tmp); 352 if (!ethhdr) 353 return; 354 355 switch (ethhdr->h_proto) { 356 case htons(ETH_P_8021Q): 357 vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr), 358 sizeof(*vhdr), &vhdr_tmp); 359 if (!vhdr) 360 return; 361 prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK; 362 prio = prio >> VLAN_PRIO_SHIFT; 363 break; 364 case htons(ETH_P_IP): 365 ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr), 366 sizeof(*ip_hdr), &ip_hdr_tmp); 367 if (!ip_hdr) 368 return; 369 prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5; 370 break; 371 case htons(ETH_P_IPV6): 372 ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr), 373 sizeof(*ip6_hdr), &ip6_hdr_tmp); 374 if (!ip6_hdr) 375 return; 376 prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5; 377 break; 378 default: 379 return; 380 } 381 382 skb->priority = prio + 256; 383 } 384 385 static int batadv_recv_unhandled_packet(struct sk_buff *skb, 386 struct batadv_hard_iface *recv_if) 387 { 388 kfree_skb(skb); 389 390 return NET_RX_DROP; 391 } 392 393 /* incoming packets with the batman ethertype received on any active hard 394 * interface 395 */ 396 397 /** 398 * batadv_batman_skb_recv() - Handle incoming message from an hard interface 399 * @skb: the received packet 400 * @dev: the net device that the packet was received on 401 * @ptype: packet type of incoming packet (ETH_P_BATMAN) 402 * @orig_dev: the original receive net device (e.g. bonded device) 403 * 404 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure 405 */ 406 int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev, 407 struct packet_type *ptype, 408 struct net_device *orig_dev) 409 { 410 struct batadv_priv *bat_priv; 411 struct batadv_ogm_packet *batadv_ogm_packet; 412 struct batadv_hard_iface *hard_iface; 413 u8 idx; 414 415 hard_iface = container_of(ptype, struct batadv_hard_iface, 416 batman_adv_ptype); 417 418 /* Prevent processing a packet received on an interface which is getting 419 * shut down otherwise the packet may trigger de-reference errors 420 * further down in the receive path. 421 */ 422 if (!kref_get_unless_zero(&hard_iface->refcount)) 423 goto err_out; 424 425 skb = skb_share_check(skb, GFP_ATOMIC); 426 427 /* skb was released by skb_share_check() */ 428 if (!skb) 429 goto err_put; 430 431 /* packet should hold at least type and version */ 432 if (unlikely(!pskb_may_pull(skb, 2))) 433 goto err_free; 434 435 /* expect a valid ethernet header here. */ 436 if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb))) 437 goto err_free; 438 439 if (!hard_iface->mesh_iface) 440 goto err_free; 441 442 bat_priv = netdev_priv(hard_iface->mesh_iface); 443 444 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 445 goto err_free; 446 447 /* discard frames on not active interfaces */ 448 if (hard_iface->if_status != BATADV_IF_ACTIVE) 449 goto err_free; 450 451 batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data; 452 453 if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) { 454 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 455 "Drop packet: incompatible batman version (%i)\n", 456 batadv_ogm_packet->version); 457 goto err_free; 458 } 459 460 /* reset control block to avoid left overs from previous users */ 461 memset(skb->cb, 0, sizeof(struct batadv_skb_cb)); 462 463 idx = batadv_ogm_packet->packet_type; 464 (*batadv_rx_handler[idx])(skb, hard_iface); 465 466 batadv_hardif_put(hard_iface); 467 468 /* return NET_RX_SUCCESS in any case as we 469 * most probably dropped the packet for 470 * routing-logical reasons. 471 */ 472 return NET_RX_SUCCESS; 473 474 err_free: 475 kfree_skb(skb); 476 err_put: 477 batadv_hardif_put(hard_iface); 478 err_out: 479 return NET_RX_DROP; 480 } 481 482 static void batadv_recv_handler_init(void) 483 { 484 int i; 485 486 for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++) 487 batadv_rx_handler[i] = batadv_recv_unhandled_packet; 488 489 for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++) 490 batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet; 491 492 /* compile time checks for sizes */ 493 BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6); 494 BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24); 495 BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20); 496 BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20); 497 BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116); 498 BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10); 499 BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18); 500 BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20); 501 BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14); 502 BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46); 503 BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20); 504 BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4); 505 BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8); 506 BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8); 507 BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12); 508 BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8); 509 510 i = sizeof_field(struct sk_buff, cb); 511 BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i); 512 513 /* broadcast packet */ 514 batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet; 515 /* multicast packet */ 516 batadv_rx_handler[BATADV_MCAST] = batadv_recv_mcast_packet; 517 518 /* unicast packets ... */ 519 /* unicast with 4 addresses packet */ 520 batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet; 521 /* unicast packet */ 522 batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet; 523 /* unicast tvlv packet */ 524 batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv; 525 /* batman icmp packet */ 526 batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet; 527 /* Fragmented packets */ 528 batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet; 529 } 530 531 /** 532 * batadv_recv_handler_register() - Register handler for batman-adv packet type 533 * @packet_type: batadv_packettype which should be handled 534 * @recv_handler: receive handler for the packet type 535 * 536 * Return: 0 on success or negative error number in case of failure 537 */ 538 int 539 batadv_recv_handler_register(u8 packet_type, 540 int (*recv_handler)(struct sk_buff *, 541 struct batadv_hard_iface *)) 542 { 543 int (*curr)(struct sk_buff *skb, 544 struct batadv_hard_iface *recv_if); 545 curr = batadv_rx_handler[packet_type]; 546 547 if (curr != batadv_recv_unhandled_packet && 548 curr != batadv_recv_unhandled_unicast_packet) 549 return -EBUSY; 550 551 batadv_rx_handler[packet_type] = recv_handler; 552 return 0; 553 } 554 555 /** 556 * batadv_recv_handler_unregister() - Unregister handler for packet type 557 * @packet_type: batadv_packettype which should no longer be handled 558 */ 559 void batadv_recv_handler_unregister(u8 packet_type) 560 { 561 batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet; 562 } 563 564 /** 565 * batadv_get_vid() - extract the VLAN identifier from skb if any 566 * @skb: the buffer containing the packet 567 * @header_len: length of the batman header preceding the ethernet header 568 * 569 * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the 570 * skb is vlan tagged. Otherwise BATADV_NO_FLAGS. 571 */ 572 unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len) 573 { 574 struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len); 575 struct vlan_ethhdr *vhdr; 576 unsigned short vid; 577 578 if (ethhdr->h_proto != htons(ETH_P_8021Q)) 579 return BATADV_NO_FLAGS; 580 581 if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN)) 582 return BATADV_NO_FLAGS; 583 584 vhdr = (struct vlan_ethhdr *)(skb->data + header_len); 585 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 586 587 /* VID 0 is only used to indicate "priority tag" frames which only 588 * contain priority information and no VID. 589 */ 590 if (vid == 0) 591 return BATADV_NO_FLAGS; 592 593 vid |= BATADV_VLAN_HAS_TAG; 594 595 return vid; 596 } 597 598 /** 599 * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan 600 * @bat_priv: the bat priv with all the mesh interface information 601 * @vid: the VLAN identifier for which the AP isolation attributed as to be 602 * looked up 603 * 604 * Return: true if AP isolation is on for the VLAN identified by vid, false 605 * otherwise 606 */ 607 bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid) 608 { 609 bool ap_isolation_enabled = false; 610 struct batadv_meshif_vlan *vlan; 611 612 /* if the AP isolation is requested on a VLAN, then check for its 613 * setting in the proper VLAN private data structure 614 */ 615 vlan = batadv_meshif_vlan_get(bat_priv, vid); 616 if (vlan) { 617 ap_isolation_enabled = atomic_read(&vlan->ap_isolation); 618 batadv_meshif_vlan_put(vlan); 619 } 620 621 return ap_isolation_enabled; 622 } 623 624 /** 625 * batadv_throw_uevent() - Send an uevent with batman-adv specific env data 626 * @bat_priv: the bat priv with all the mesh interface information 627 * @type: subsystem type of event. Stored in uevent's BATTYPE 628 * @action: action type of event. Stored in uevent's BATACTION 629 * @data: string with additional information to the event (ignored for 630 * BATADV_UEV_DEL). Stored in uevent's BATDATA 631 * 632 * Return: 0 on success or negative error number in case of failure 633 */ 634 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type, 635 enum batadv_uev_action action, const char *data) 636 { 637 int ret = -ENOMEM; 638 struct kobject *bat_kobj; 639 char *uevent_env[4] = { NULL, NULL, NULL, NULL }; 640 641 bat_kobj = &bat_priv->mesh_iface->dev.kobj; 642 643 uevent_env[0] = kasprintf(GFP_ATOMIC, 644 "%s%s", BATADV_UEV_TYPE_VAR, 645 batadv_uev_type_str[type]); 646 if (!uevent_env[0]) 647 goto report_error; 648 649 uevent_env[1] = kasprintf(GFP_ATOMIC, 650 "%s%s", BATADV_UEV_ACTION_VAR, 651 batadv_uev_action_str[action]); 652 if (!uevent_env[1]) 653 goto free_first_env; 654 655 /* If the event is DEL, ignore the data field */ 656 if (action != BATADV_UEV_DEL) { 657 uevent_env[2] = kasprintf(GFP_ATOMIC, 658 "%s%s", BATADV_UEV_DATA_VAR, data); 659 if (!uevent_env[2]) 660 goto free_second_env; 661 } 662 663 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env); 664 kfree(uevent_env[2]); 665 free_second_env: 666 kfree(uevent_env[1]); 667 free_first_env: 668 kfree(uevent_env[0]); 669 670 if (ret) 671 report_error: 672 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 673 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n", 674 batadv_uev_type_str[type], 675 batadv_uev_action_str[action], 676 (action == BATADV_UEV_DEL ? "NULL" : data), ret); 677 return ret; 678 } 679 680 module_init(batadv_init); 681 module_exit(batadv_exit); 682 683 MODULE_LICENSE("GPL"); 684 685 MODULE_AUTHOR(BATADV_DRIVER_AUTHOR); 686 MODULE_DESCRIPTION(BATADV_DRIVER_DESC); 687 MODULE_VERSION(BATADV_SOURCE_VERSION); 688 MODULE_ALIAS_RTNL_LINK("batadv"); 689 MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME); 690