The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2009-2012 Broadcom Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 19 | /******************************************************************************* |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 20 | * |
| 21 | * Filename: btif_pan.c |
| 22 | * |
| 23 | * Description: PAN Profile Bluetooth Interface |
| 24 | * |
| 25 | * |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 26 | ******************************************************************************/ |
Marie Janssen | 49120dc | 2015-07-07 16:47:20 -0700 | [diff] [blame] | 27 | |
| 28 | #define LOG_TAG "bt_btif_pan" |
| 29 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 30 | #include <base/logging.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | #include <ctype.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 34 | #include <linux/if_ether.h> |
| 35 | #include <linux/if_tun.h> |
| 36 | #include <linux/sockios.h> |
| 37 | #include <net/if.h> |
| 38 | #include <netdb.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <signal.h> |
| 41 | #include <stdio.h> |
| 42 | #include <string.h> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 43 | #include <sys/ioctl.h> |
| 44 | #include <sys/poll.h> |
| 45 | #include <sys/prctl.h> |
| 46 | #include <sys/select.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 47 | #include <sys/socket.h> |
| 48 | #include <sys/wait.h> |
Arman Uguray | d30195c | 2015-05-29 15:27:58 -0700 | [diff] [blame] | 49 | #include <unistd.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 51 | #include <hardware/bluetooth.h> |
| 52 | #include <hardware/bt_pan.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 53 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 54 | #include "bt_common.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 55 | #include "bta_api.h" |
| 56 | #include "bta_pan_api.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 57 | #include "btcore/include/bdaddr.h" |
| 58 | #include "btif_common.h" |
| 59 | #include "btif_pan_internal.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 60 | #include "btif_sock_thread.h" |
| 61 | #include "btif_sock_util.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 62 | #include "btif_util.h" |
| 63 | #include "btm_api.h" |
| 64 | #include "device/include/controller.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 65 | #include "osi/include/log.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 66 | #include "osi/include/osi.h" |
| 67 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 68 | #define FORWARD_IGNORE 1 |
| 69 | #define FORWARD_SUCCESS 0 |
| 70 | #define FORWARD_FAILURE (-1) |
| 71 | #define FORWARD_CONGEST (-2) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 72 | |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 73 | #if (PAN_NAP_DISABLED == TRUE && PANU_DISABLED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 74 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_NONE |
| 75 | #elif PAN_NAP_DISABLED == TRUE |
| 76 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANU |
| 77 | #elif PANU_DISABLED == TRUE |
| 78 | #define BTPAN_LOCAL_ROLE BTPAN_ROLE_PANNAP |
| 79 | #else |
| 80 | #define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP) |
| 81 | #endif |
| 82 | |
Myles Watson | 40cde56 | 2016-10-21 09:39:13 -0700 | [diff] [blame] | 83 | #define asrt(s) \ |
| 84 | do { \ |
| 85 | if (!(s)) \ |
| 86 | BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##", \ |
| 87 | __func__, #s, __LINE__) \ |
| 88 | } while (0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 90 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 91 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 92 | btpan_cb_t btpan_cb; |
| 93 | |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 94 | static bool jni_initialized; |
| 95 | static bool stack_initialized; |
| 96 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 97 | static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); |
| 98 | static void btpan_jni_cleanup(); |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 99 | static bt_status_t btpan_connect(const RawAddress* bd_addr, int local_role, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 100 | int remote_role); |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 101 | static bt_status_t btpan_disconnect(const RawAddress* bd_addr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 102 | static bt_status_t btpan_enable(int local_role); |
| 103 | static int btpan_get_local_role(void); |
| 104 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 105 | static void btpan_tap_fd_signaled(int fd, int type, int flags, |
| 106 | uint32_t user_id); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 107 | static void btpan_cleanup_conn(btpan_conn_t* conn); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 108 | static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data); |
| 109 | static void btu_exec_tap_fd_read(void* p_param); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 111 | static btpan_interface_t pan_if = { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 112 | sizeof(pan_if), btpan_jni_init, btpan_enable, btpan_get_local_role, |
| 113 | btpan_connect, btpan_disconnect, btpan_jni_cleanup}; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 114 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 115 | btpan_interface_t* btif_pan_get_interface() { return &pan_if; } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 116 | |
| 117 | /******************************************************************************* |
| 118 | ** |
| 119 | ** Function btif_pan_init |
| 120 | ** |
| 121 | ** Description initializes the pan interface |
| 122 | ** |
| 123 | ** Returns bt_status_t |
| 124 | ** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 125 | ******************************************************************************/ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 126 | void btif_pan_init() { |
| 127 | BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, |
| 128 | btpan_cb.enabled); |
| 129 | stack_initialized = true; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 130 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 131 | if (jni_initialized && !btpan_cb.enabled) { |
| 132 | BTIF_TRACE_DEBUG("Enabling PAN...."); |
| 133 | memset(&btpan_cb, 0, sizeof(btpan_cb)); |
| 134 | btpan_cb.tap_fd = INVALID_FD; |
| 135 | btpan_cb.flow = 1; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 136 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 137 | btpan_cleanup_conn(&btpan_cb.conns[i]); |
| 138 | BTA_PanEnable(bta_pan_callback); |
| 139 | btpan_cb.enabled = 1; |
| 140 | btpan_enable(BTPAN_LOCAL_ROLE); |
| 141 | } |
| 142 | } |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 143 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 144 | static void pan_disable() { |
| 145 | if (btpan_cb.enabled) { |
| 146 | btpan_cb.enabled = 0; |
| 147 | BTA_PanDisable(); |
| 148 | if (btpan_cb.tap_fd != INVALID_FD) { |
| 149 | btpan_tap_close(btpan_cb.tap_fd); |
| 150 | btpan_cb.tap_fd = INVALID_FD; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void btif_pan_cleanup() { |
| 156 | if (!stack_initialized) return; |
| 157 | |
| 158 | // Bluetooth is shuting down, invalidate all BTA PAN handles |
| 159 | for (int i = 0; i < MAX_PAN_CONNS; i++) |
| 160 | btpan_cleanup_conn(&btpan_cb.conns[i]); |
| 161 | |
| 162 | pan_disable(); |
| 163 | stack_initialized = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static btpan_callbacks_t callback; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 167 | static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) { |
| 168 | BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", |
| 169 | stack_initialized, btpan_cb.enabled); |
| 170 | callback = *callbacks; |
| 171 | jni_initialized = true; |
| 172 | if (stack_initialized && !btpan_cb.enabled) btif_pan_init(); |
| 173 | return BT_STATUS_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 176 | static void btpan_jni_cleanup() { |
| 177 | pan_disable(); |
| 178 | jni_initialized = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 179 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 180 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 181 | static inline int bta_role_to_btpan(int bta_pan_role) { |
| 182 | int btpan_role = 0; |
| 183 | BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role); |
| 184 | if (bta_pan_role & PAN_ROLE_NAP_SERVER) btpan_role |= BTPAN_ROLE_PANNAP; |
| 185 | if (bta_pan_role & PAN_ROLE_CLIENT) btpan_role |= BTPAN_ROLE_PANU; |
| 186 | return btpan_role; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 187 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 188 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 189 | static inline int btpan_role_to_bta(int btpan_role) { |
| 190 | int bta_pan_role = PAN_ROLE_INACTIVE; |
| 191 | BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role); |
| 192 | if (btpan_role & BTPAN_ROLE_PANNAP) bta_pan_role |= PAN_ROLE_NAP_SERVER; |
| 193 | if (btpan_role & BTPAN_ROLE_PANU) bta_pan_role |= PAN_ROLE_CLIENT; |
| 194 | return bta_pan_role; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 195 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 196 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 197 | static volatile int btpan_dev_local_role; |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 198 | #if (BTA_PAN_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 199 | static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 200 | static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, |
| 201 | PAN_SECURITY}; |
Pavlin Radoslavov | a9ea43b | 2016-02-15 11:47:37 -0800 | [diff] [blame] | 202 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 203 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 204 | static bt_status_t btpan_enable(int local_role) { |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 205 | #if (BTA_PAN_INCLUDED == TRUE) |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 206 | BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role); |
| 207 | int bta_pan_role = btpan_role_to_bta(local_role); |
| 208 | BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info); |
| 209 | btpan_dev_local_role = local_role; |
| 210 | return BT_STATUS_SUCCESS; |
Mike J. Chen | 3576c56 | 2014-04-01 12:54:27 -0700 | [diff] [blame] | 211 | #else |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 212 | return BT_STATUS_FAIL; |
Mike J. Chen | 3576c56 | 2014-04-01 12:54:27 -0700 | [diff] [blame] | 213 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 214 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 215 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 216 | static int btpan_get_local_role() { |
| 217 | BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role); |
| 218 | return btpan_dev_local_role; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 219 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 220 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 221 | static bt_status_t btpan_connect(const RawAddress* bd_addr, int local_role, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 222 | int remote_role) { |
| 223 | BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role); |
| 224 | int bta_local_role = btpan_role_to_bta(local_role); |
| 225 | int bta_remote_role = btpan_role_to_bta(remote_role); |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 226 | btpan_new_conn(-1, *bd_addr, bta_local_role, bta_remote_role); |
| 227 | BTA_PanOpen(*bd_addr, bta_local_role, bta_remote_role); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 228 | return BT_STATUS_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | static void btif_in_pan_generic_evt(uint16_t event, char* p_param) { |
| 232 | BTIF_TRACE_EVENT("%s: event=%d", __func__, event); |
| 233 | switch (event) { |
| 234 | case BTIF_PAN_CB_DISCONNECTING: { |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 235 | RawAddress* bd_addr = (RawAddress*)p_param; |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 236 | btpan_conn_t* conn = btpan_find_conn_addr(*bd_addr); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 237 | int btpan_conn_local_role; |
| 238 | int btpan_remote_role; |
| 239 | asrt(conn != NULL); |
| 240 | if (conn) { |
| 241 | btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 242 | btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
| 243 | callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 244 | BT_STATUS_SUCCESS, &conn->peer, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 245 | btpan_conn_local_role, btpan_remote_role); |
| 246 | } |
| 247 | } break; |
| 248 | default: { |
| 249 | BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); |
| 250 | } break; |
| 251 | } |
| 252 | } |
| 253 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 254 | static bt_status_t btpan_disconnect(const RawAddress* bd_addr) { |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 255 | btpan_conn_t* conn = btpan_find_conn_addr(*bd_addr); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 256 | if (conn && conn->handle >= 0) { |
| 257 | /* Inform the application that the disconnect has been initiated |
| 258 | * successfully */ |
| 259 | btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 260 | (char*)bd_addr, sizeof(RawAddress), NULL); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 261 | BTA_PanClose(conn->handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 262 | return BT_STATUS_SUCCESS; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 263 | } |
| 264 | return BT_STATUS_FAIL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 265 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 266 | |
| 267 | static int pan_pth = -1; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 268 | void create_tap_read_thread(int tap_fd) { |
| 269 | if (pan_pth < 0) pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); |
| 270 | if (pan_pth >= 0) |
| 271 | btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 272 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 273 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 274 | void destroy_tap_read_thread(void) { |
| 275 | if (pan_pth >= 0) { |
| 276 | btsock_thread_exit(pan_pth); |
| 277 | pan_pth = -1; |
| 278 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 279 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 280 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 281 | static int tap_if_up(const char* devname, const RawAddress* addr) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 282 | struct ifreq ifr; |
| 283 | int sk, err; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 284 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 285 | sk = socket(AF_INET, SOCK_DGRAM, 0); |
| 286 | if (sk < 0) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 287 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 288 | // set mac addr |
| 289 | memset(&ifr, 0, sizeof(ifr)); |
| 290 | strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); |
| 291 | err = ioctl(sk, SIOCGIFHWADDR, &ifr); |
| 292 | if (err < 0) { |
| 293 | BTIF_TRACE_ERROR( |
| 294 | "Could not get network hardware for interface:%s, errno:%s", devname, |
| 295 | strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 296 | close(sk); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); |
| 301 | memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6); |
| 302 | |
| 303 | /* The IEEE has specified that the most significant bit of the most |
| 304 | * significant byte is used to |
| 305 | * determine a multicast address. If its a 1, that means multicast, 0 means |
| 306 | * unicast. |
| 307 | * Kernel returns an error if we try to set a multicast address for the |
| 308 | * tun-tap ethernet interface. |
| 309 | * Mask this bit to avoid any issue with auto generated address. |
| 310 | */ |
| 311 | if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { |
| 312 | BTIF_TRACE_WARNING( |
| 313 | "Not a unicast MAC address, force multicast bit flipping"); |
| 314 | ifr.ifr_hwaddr.sa_data[0] &= ~0x01; |
| 315 | } |
| 316 | |
| 317 | err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); |
| 318 | |
| 319 | if (err < 0) { |
| 320 | BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", |
| 321 | devname, strerror(errno)); |
| 322 | close(sk); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | // bring it up |
| 327 | memset(&ifr, 0, sizeof(ifr)); |
| 328 | strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); |
| 329 | |
| 330 | ifr.ifr_flags |= IFF_UP; |
| 331 | ifr.ifr_flags |= IFF_MULTICAST; |
| 332 | |
| 333 | err = ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr); |
| 334 | |
| 335 | if (err < 0) { |
| 336 | BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", |
| 337 | devname, errno); |
| 338 | close(sk); |
| 339 | return -1; |
| 340 | } |
| 341 | close(sk); |
| 342 | BTIF_TRACE_DEBUG("network interface: %s is up", devname); |
| 343 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 346 | static int tap_if_down(const char* devname) { |
| 347 | struct ifreq ifr; |
| 348 | int sk; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 349 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 350 | sk = socket(AF_INET, SOCK_DGRAM, 0); |
| 351 | if (sk < 0) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 352 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 353 | memset(&ifr, 0, sizeof(ifr)); |
| 354 | strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 355 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 356 | ifr.ifr_flags &= ~IFF_UP; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 357 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 358 | ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 359 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 360 | close(sk); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 361 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 362 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 363 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 364 | |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 365 | void btpan_set_flow_control(bool enable) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 366 | if (btpan_cb.tap_fd == -1) return; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 367 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 368 | btpan_cb.flow = enable; |
| 369 | if (enable) { |
| 370 | btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0); |
| 371 | bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(btpan_cb.tap_fd)); |
| 372 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 375 | int btpan_tap_open() { |
| 376 | struct ifreq ifr; |
| 377 | int fd, err; |
| 378 | const char* clonedev = "/dev/tun"; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 379 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 380 | /* open the clone device */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 381 | |
Marie Janssen | f33b6f4 | 2016-11-22 15:01:42 -0800 | [diff] [blame] | 382 | fd = open(clonedev, O_RDWR); |
| 383 | if (fd < 0) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 384 | BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno); |
| 385 | return fd; |
| 386 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 387 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 388 | memset(&ifr, 0, sizeof(ifr)); |
| 389 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 390 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 391 | strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 392 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 393 | /* try to create the device */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 394 | err = ioctl(fd, TUNSETIFF, (void*)&ifr); |
Marie Janssen | f33b6f4 | 2016-11-22 15:01:42 -0800 | [diff] [blame] | 395 | if (err < 0) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 396 | BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 397 | close(fd); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 398 | return err; |
| 399 | } |
| 400 | if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) { |
| 401 | int flags = fcntl(fd, F_GETFL, 0); |
| 402 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 403 | return fd; |
| 404 | } |
| 405 | BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME); |
| 406 | close(fd); |
| 407 | return INVALID_FD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 408 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 409 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 410 | int btpan_tap_send(int tap_fd, const RawAddress& src, const RawAddress& dst, |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 411 | uint16_t proto, const char* buf, uint16_t len, |
| 412 | UNUSED_ATTR bool ext, UNUSED_ATTR bool forward) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 413 | if (tap_fd != INVALID_FD) { |
| 414 | tETH_HDR eth_hdr; |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 415 | eth_hdr.h_dest = dst; |
| 416 | eth_hdr.h_src = src; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 417 | eth_hdr.h_proto = htons(proto); |
| 418 | char packet[TAP_MAX_PKT_WRITE_LEN + sizeof(tETH_HDR)]; |
| 419 | memcpy(packet, ð_hdr, sizeof(tETH_HDR)); |
| 420 | if (len > TAP_MAX_PKT_WRITE_LEN) { |
| 421 | LOG_ERROR(LOG_TAG, "btpan_tap_send eth packet size:%d is exceeded limit!", |
| 422 | len); |
| 423 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 424 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 425 | memcpy(packet + sizeof(tETH_HDR), buf, len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 426 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 427 | /* Send data to network interface */ |
| 428 | ssize_t ret; |
| 429 | OSI_NO_INTR(ret = write(tap_fd, packet, len + sizeof(tETH_HDR))); |
| 430 | BTIF_TRACE_DEBUG("ret:%d", ret); |
| 431 | return (int)ret; |
| 432 | } |
| 433 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 434 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 435 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 436 | int btpan_tap_close(int fd) { |
| 437 | if (tap_if_down(TAP_IF_NAME) == 0) close(fd); |
| 438 | if (pan_pth >= 0) btsock_thread_wakeup(pan_pth); |
| 439 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 440 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 441 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 442 | btpan_conn_t* btpan_find_conn_handle(uint16_t handle) { |
| 443 | for (int i = 0; i < MAX_PAN_CONNS; i++) { |
| 444 | if (btpan_cb.conns[i].handle == handle) return &btpan_cb.conns[i]; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 449 | btpan_conn_t* btpan_find_conn_addr(const RawAddress& addr) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 450 | for (int i = 0; i < MAX_PAN_CONNS; i++) { |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 451 | if (btpan_cb.conns[i].peer == addr) return &btpan_cb.conns[i]; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 452 | } |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | static void btpan_open_conn(btpan_conn_t* conn, tBTA_PAN* p_data) { |
| 457 | BTIF_TRACE_API( |
| 458 | "btpan_open_conn: local_role:%d, peer_role: %d, handle:%d, conn: %p", |
| 459 | p_data->open.local_role, p_data->open.peer_role, p_data->open.handle, |
| 460 | conn); |
| 461 | |
| 462 | if (conn == NULL) |
| 463 | conn = btpan_new_conn(p_data->open.handle, p_data->open.bd_addr, |
| 464 | p_data->open.local_role, p_data->open.peer_role); |
| 465 | if (conn) { |
| 466 | BTIF_TRACE_DEBUG( |
| 467 | "btpan_open_conn:tap_fd:%d, open_count:%d, " |
| 468 | "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", |
| 469 | btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, p_data->open.handle, |
| 470 | conn->local_role, conn->remote_role); |
| 471 | |
| 472 | btpan_cb.open_count++; |
| 473 | conn->handle = p_data->open.handle; |
| 474 | if (btpan_cb.tap_fd < 0) { |
| 475 | btpan_cb.tap_fd = btpan_tap_open(); |
| 476 | if (btpan_cb.tap_fd >= 0) create_tap_read_thread(btpan_cb.tap_fd); |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 477 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 478 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 479 | if (btpan_cb.tap_fd >= 0) { |
| 480 | btpan_cb.flow = 1; |
| 481 | conn->state = PAN_STATE_OPEN; |
Andre Eisenbach | 59606cc | 2015-05-05 11:39:28 -0700 | [diff] [blame] | 482 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 483 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 484 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 485 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 486 | static void btpan_close_conn(btpan_conn_t* conn) { |
| 487 | BTIF_TRACE_API("btpan_close_conn: %p", conn); |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 488 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 489 | if (conn && conn->state == PAN_STATE_OPEN) { |
| 490 | BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN"); |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 491 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 492 | conn->state = PAN_STATE_CLOSE; |
| 493 | btpan_cb.open_count--; |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 494 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 495 | if (btpan_cb.open_count == 0) { |
| 496 | destroy_tap_read_thread(); |
| 497 | if (btpan_cb.tap_fd != INVALID_FD) { |
| 498 | btpan_tap_close(btpan_cb.tap_fd); |
| 499 | btpan_cb.tap_fd = INVALID_FD; |
| 500 | } |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 501 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 502 | } |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 503 | } |
| 504 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 505 | static void btpan_cleanup_conn(btpan_conn_t* conn) { |
| 506 | if (conn) { |
| 507 | conn->handle = -1; |
| 508 | conn->state = -1; |
| 509 | memset(&conn->peer, 0, sizeof(conn->peer)); |
| 510 | memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); |
| 511 | conn->local_role = conn->remote_role = 0; |
| 512 | } |
| 513 | } |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 514 | |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 515 | btpan_conn_t* btpan_new_conn(int handle, const RawAddress& addr, int local_role, |
| 516 | int remote_role) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 517 | for (int i = 0; i < MAX_PAN_CONNS; i++) { |
| 518 | BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle); |
| 519 | if (btpan_cb.conns[i].handle == -1) { |
| 520 | BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, |
| 521 | local_role, remote_role); |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 522 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 523 | btpan_cb.conns[i].handle = handle; |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 524 | btpan_cb.conns[i].peer = addr; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 525 | btpan_cb.conns[i].local_role = local_role; |
| 526 | btpan_cb.conns[i].remote_role = remote_role; |
| 527 | return &btpan_cb.conns[i]; |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 528 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 529 | } |
| 530 | BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", |
| 531 | MAX_PAN_CONNS); |
| 532 | return NULL; |
Nitin Shivpure | bb2a4fa | 2015-09-06 15:04:01 +0530 | [diff] [blame] | 533 | } |
| 534 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 535 | void btpan_close_handle(btpan_conn_t* p) { |
| 536 | BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle); |
| 537 | p->handle = -1; |
| 538 | p->local_role = -1; |
| 539 | p->remote_role = -1; |
| 540 | memset(&p->peer, 0, 6); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 541 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 542 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 543 | static inline bool should_forward(tETH_HDR* hdr) { |
| 544 | uint16_t proto = ntohs(hdr->h_proto); |
| 545 | if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6) |
| 546 | return true; |
| 547 | BTIF_TRACE_DEBUG("unknown proto:%x", proto); |
| 548 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 551 | static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR* hdr) { |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 552 | int broadcast = eth_hdr->h_dest.address[0] & 1; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 553 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 554 | // Find the right connection to send this frame over. |
| 555 | for (int i = 0; i < MAX_PAN_CONNS; i++) { |
| 556 | uint16_t handle = btpan_cb.conns[i].handle; |
| 557 | if (handle != (uint16_t)-1 && |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 558 | (broadcast || btpan_cb.conns[i].eth_addr == eth_hdr->h_dest || |
| 559 | btpan_cb.conns[i].peer == eth_hdr->h_dest)) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 560 | int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, |
| 561 | ntohs(eth_hdr->h_proto), hdr, 0); |
| 562 | switch (result) { |
| 563 | case PAN_Q_SIZE_EXCEEDED: |
| 564 | return FORWARD_CONGEST; |
| 565 | case PAN_SUCCESS: |
| 566 | return FORWARD_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 567 | default: |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 568 | return FORWARD_FAILURE; |
| 569 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 570 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 571 | } |
| 572 | osi_free(hdr); |
| 573 | return FORWARD_IGNORE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 576 | static void bta_pan_callback_transfer(uint16_t event, char* p_param) { |
| 577 | tBTA_PAN* p_data = (tBTA_PAN*)p_param; |
| 578 | |
| 579 | switch (event) { |
| 580 | case BTA_PAN_ENABLE_EVT: |
| 581 | BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT"); |
| 582 | break; |
| 583 | case BTA_PAN_SET_ROLE_EVT: { |
| 584 | int btpan_role = bta_role_to_btpan(p_data->set_role.role); |
| 585 | bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS |
| 586 | ? BT_STATUS_SUCCESS |
| 587 | : BT_STATUS_FAIL; |
| 588 | btpan_control_state_t state = |
| 589 | btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; |
| 590 | callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); |
| 591 | break; |
| 592 | } |
| 593 | case BTA_PAN_OPENING_EVT: { |
| 594 | btpan_conn_t* conn; |
| 595 | bdstr_t bds; |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 596 | bdaddr_to_string(&p_data->opening.bd_addr, bds, sizeof(bds)); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 597 | BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", |
| 598 | p_data->opening.handle, bds); |
| 599 | conn = btpan_find_conn_addr(p_data->opening.bd_addr); |
| 600 | |
| 601 | asrt(conn != NULL); |
| 602 | if (conn) { |
| 603 | conn->handle = p_data->opening.handle; |
| 604 | int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 605 | int btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 606 | callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, |
| 607 | &p_data->opening.bd_addr, |
| 608 | btpan_conn_local_role, btpan_remote_role); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 609 | } else |
| 610 | BTIF_TRACE_ERROR("connection not found"); |
| 611 | break; |
| 612 | } |
| 613 | case BTA_PAN_OPEN_EVT: { |
| 614 | btpan_connection_state_t state; |
| 615 | bt_status_t status; |
| 616 | btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle); |
| 617 | |
| 618 | LOG_VERBOSE(LOG_TAG, "%s pan connection open status: %d", __func__, |
| 619 | p_data->open.status); |
| 620 | if (p_data->open.status == BTA_PAN_SUCCESS) { |
| 621 | state = BTPAN_STATE_CONNECTED; |
| 622 | status = BT_STATUS_SUCCESS; |
| 623 | btpan_open_conn(conn, p_data); |
| 624 | } else { |
| 625 | state = BTPAN_STATE_DISCONNECTED; |
| 626 | status = BT_STATUS_FAIL; |
| 627 | btpan_cleanup_conn(conn); |
| 628 | } |
| 629 | /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, |
| 630 | * conn); */ |
| 631 | /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, |
| 632 | * conn->remote_role); */ |
| 633 | int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); |
| 634 | int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 635 | callback.connection_state_cb(state, status, &p_data->open.bd_addr, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 636 | btpan_conn_local_role, btpan_remote_role); |
| 637 | break; |
| 638 | } |
| 639 | case BTA_PAN_CLOSE_EVT: { |
| 640 | LOG_INFO(LOG_TAG, "%s: event = BTA_PAN_CLOSE_EVT handle %d", __func__, |
| 641 | p_data->close.handle); |
| 642 | btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); |
| 643 | btpan_close_conn(conn); |
| 644 | |
| 645 | if (conn && conn->handle >= 0) { |
| 646 | int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); |
| 647 | int btpan_remote_role = bta_role_to_btpan(conn->remote_role); |
| 648 | callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, (bt_status_t)0, |
Jakub Pawlowski | a83ac12 | 2017-06-15 11:53:33 -0700 | [diff] [blame] | 649 | &conn->peer, btpan_conn_local_role, |
| 650 | btpan_remote_role); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 651 | btpan_cleanup_conn(conn); |
| 652 | } else |
| 653 | BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle); |
| 654 | break; |
| 655 | } |
| 656 | default: |
| 657 | BTIF_TRACE_WARNING("Unknown pan event %d", event); |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data) { |
| 663 | btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, |
| 664 | sizeof(tBTA_PAN), NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 665 | } |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 666 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 667 | #define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 668 | static void btu_exec_tap_fd_read(void* p_param) { |
| 669 | struct pollfd ufd; |
| 670 | int fd = PTR_TO_INT(p_param); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 671 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 672 | if (fd == INVALID_FD || fd != btpan_cb.tap_fd) return; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 673 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 674 | // Don't occupy BTU context too long, avoid buffer overruns and |
| 675 | // give other profiles a chance to run by limiting the amount of memory |
| 676 | // PAN can use. |
| 677 | for (int i = 0; i < PAN_BUF_MAX && btif_is_enabled() && btpan_cb.flow; i++) { |
| 678 | BT_HDR* buffer = (BT_HDR*)osi_malloc(PAN_BUF_SIZE); |
| 679 | buffer->offset = PAN_MINIMUM_OFFSET; |
| 680 | buffer->len = PAN_BUF_SIZE - sizeof(BT_HDR) - buffer->offset; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 681 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 682 | uint8_t* packet = (uint8_t*)buffer + sizeof(BT_HDR) + buffer->offset; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 683 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 684 | // If we don't have an undelivered packet left over, pull one from the TAP |
| 685 | // driver. |
| 686 | // We save it in the congest_packet right away in case we can't deliver it |
| 687 | // in this |
| 688 | // attempt. |
| 689 | if (!btpan_cb.congest_packet_size) { |
| 690 | ssize_t ret; |
| 691 | OSI_NO_INTR(ret = read(fd, btpan_cb.congest_packet, |
| 692 | sizeof(btpan_cb.congest_packet))); |
| 693 | switch (ret) { |
| 694 | case -1: |
| 695 | BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, |
| 696 | strerror(errno)); |
| 697 | osi_free(buffer); |
| 698 | // add fd back to monitor thread to try it again later |
| 699 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
| 700 | return; |
| 701 | case 0: |
| 702 | BTIF_TRACE_WARNING("%s end of file reached.", __func__); |
| 703 | osi_free(buffer); |
| 704 | // add fd back to monitor thread to process the exception |
| 705 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
| 706 | return; |
| 707 | default: |
| 708 | btpan_cb.congest_packet_size = ret; |
| 709 | break; |
| 710 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 711 | } |
Srinu Jella | 7baa1e1 | 2015-08-11 19:16:52 +0530 | [diff] [blame] | 712 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 713 | memcpy(packet, btpan_cb.congest_packet, |
| 714 | MIN(btpan_cb.congest_packet_size, buffer->len)); |
| 715 | buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len); |
| 716 | |
| 717 | if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR*)packet)) { |
| 718 | // Extract the ethernet header from the buffer since the PAN_WriteBuf |
| 719 | // inside |
| 720 | // forward_bnep can't handle two pointers that point inside the same GKI |
| 721 | // buffer. |
| 722 | tETH_HDR hdr; |
| 723 | memcpy(&hdr, packet, sizeof(tETH_HDR)); |
| 724 | |
| 725 | // Skip the ethernet header. |
| 726 | buffer->len -= sizeof(tETH_HDR); |
| 727 | buffer->offset += sizeof(tETH_HDR); |
| 728 | if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST) |
| 729 | btpan_cb.congest_packet_size = 0; |
| 730 | } else { |
| 731 | BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, |
| 732 | buffer->len); |
| 733 | btpan_cb.congest_packet_size = 0; |
| 734 | osi_free(buffer); |
Srinu Jella | 7baa1e1 | 2015-08-11 19:16:52 +0530 | [diff] [blame] | 735 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 736 | |
| 737 | // Bail out of the loop if reading from the TAP fd would block. |
| 738 | ufd.fd = fd; |
| 739 | ufd.events = POLLIN; |
| 740 | ufd.revents = 0; |
| 741 | |
| 742 | int ret; |
| 743 | OSI_NO_INTR(ret = poll(&ufd, 1, 0)); |
| 744 | if (ret <= 0 || IS_EXCEPTION(ufd.revents)) break; |
| 745 | } |
| 746 | |
| 747 | if (btpan_cb.flow) { |
| 748 | // add fd back to monitor thread when the flow is on |
| 749 | btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); |
| 750 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 753 | static void btif_pan_close_all_conns() { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 754 | if (!stack_initialized) return; |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 755 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 756 | for (int i = 0; i < MAX_PAN_CONNS; ++i) { |
| 757 | if (btpan_cb.conns[i].handle != -1) BTA_PanClose(btpan_cb.conns[i].handle); |
| 758 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 761 | static void btpan_tap_fd_signaled(int fd, int type, int flags, |
| 762 | uint32_t user_id) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 763 | CHECK(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 764 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 765 | if (btpan_cb.tap_fd != fd) { |
| 766 | BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n", |
| 767 | __func__, btpan_cb.tap_fd, fd); |
| 768 | return; |
| 769 | } |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 770 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 771 | if (flags & SOCK_THREAD_FD_EXCEPTION) { |
| 772 | btpan_cb.tap_fd = INVALID_FD; |
| 773 | btpan_tap_close(fd); |
| 774 | btif_pan_close_all_conns(); |
| 775 | } else if (flags & SOCK_THREAD_FD_RD) |
| 776 | bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(fd)); |
Sharvil Nanavati | 4186b0e | 2014-04-07 13:52:37 -0700 | [diff] [blame] | 777 | } |