1/*
2   BlueZ - Bluetooth protocol stack for Linux
3
4   Copyright (C) 2014 Intel Corporation
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License version 2 as
8   published by the Free Software Foundation;
9
10   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21   SOFTWARE IS DISCLAIMED.
22*/
23
24#include <linux/debugfs.h>
25#include <linux/kstrtox.h>
26
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29
30#include "smp.h"
31#include "hci_request.h"
32#include "hci_debugfs.h"
33
34#define DEFINE_QUIRK_ATTRIBUTE(__name, __quirk)				      \
35static ssize_t __name ## _read(struct file *file,			      \
36				char __user *user_buf,			      \
37				size_t count, loff_t *ppos)		      \
38{									      \
39	struct hci_dev *hdev = file->private_data;			      \
40	char buf[3];							      \
41									      \
42	buf[0] = test_bit(__quirk, &hdev->quirks) ? 'Y' : 'N';		      \
43	buf[1] = '\n';							      \
44	buf[2] = '\0';							      \
45	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);	      \
46}									      \
47									      \
48static ssize_t __name ## _write(struct file *file,			      \
49				 const char __user *user_buf,		      \
50				 size_t count, loff_t *ppos)		      \
51{									      \
52	struct hci_dev *hdev = file->private_data;			      \
53	bool enable;							      \
54	int err;							      \
55									      \
56	if (test_bit(HCI_UP, &hdev->flags))				      \
57		return -EBUSY;						      \
58									      \
59	err = kstrtobool_from_user(user_buf, count, &enable);		      \
60	if (err)							      \
61		return err;						      \
62									      \
63	if (enable == test_bit(__quirk, &hdev->quirks))			      \
64		return -EALREADY;					      \
65									      \
66	change_bit(__quirk, &hdev->quirks);				      \
67									      \
68	return count;							      \
69}									      \
70									      \
71static const struct file_operations __name ## _fops = {			      \
72	.open		= simple_open,					      \
73	.read		= __name ## _read,				      \
74	.write		= __name ## _write,				      \
75	.llseek		= default_llseek,				      \
76}									      \
77
78#define DEFINE_INFO_ATTRIBUTE(__name, __field)				      \
79static int __name ## _show(struct seq_file *f, void *ptr)		      \
80{									      \
81	struct hci_dev *hdev = f->private;				      \
82									      \
83	hci_dev_lock(hdev);						      \
84	seq_printf(f, "%s\n", hdev->__field ? : "");			      \
85	hci_dev_unlock(hdev);						      \
86									      \
87	return 0;							      \
88}									      \
89									      \
90DEFINE_SHOW_ATTRIBUTE(__name)
91
92static int features_show(struct seq_file *f, void *ptr)
93{
94	struct hci_dev *hdev = f->private;
95	u8 p;
96
97	hci_dev_lock(hdev);
98	for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++)
99		seq_printf(f, "%2u: %8ph\n", p, hdev->features[p]);
100	if (lmp_le_capable(hdev))
101		seq_printf(f, "LE: %8ph\n", hdev->le_features);
102	hci_dev_unlock(hdev);
103
104	return 0;
105}
106
107DEFINE_SHOW_ATTRIBUTE(features);
108
109static int device_id_show(struct seq_file *f, void *ptr)
110{
111	struct hci_dev *hdev = f->private;
112
113	hci_dev_lock(hdev);
114	seq_printf(f, "%4.4x:%4.4x:%4.4x:%4.4x\n", hdev->devid_source,
115		  hdev->devid_vendor, hdev->devid_product, hdev->devid_version);
116	hci_dev_unlock(hdev);
117
118	return 0;
119}
120
121DEFINE_SHOW_ATTRIBUTE(device_id);
122
123static int device_list_show(struct seq_file *f, void *ptr)
124{
125	struct hci_dev *hdev = f->private;
126	struct hci_conn_params *p;
127	struct bdaddr_list *b;
128
129	hci_dev_lock(hdev);
130	list_for_each_entry(b, &hdev->accept_list, list)
131		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
132	list_for_each_entry(p, &hdev->le_conn_params, list) {
133		seq_printf(f, "%pMR (type %u) %u\n", &p->addr, p->addr_type,
134			   p->auto_connect);
135	}
136	hci_dev_unlock(hdev);
137
138	return 0;
139}
140
141DEFINE_SHOW_ATTRIBUTE(device_list);
142
143static int blacklist_show(struct seq_file *f, void *p)
144{
145	struct hci_dev *hdev = f->private;
146	struct bdaddr_list *b;
147
148	hci_dev_lock(hdev);
149	list_for_each_entry(b, &hdev->reject_list, list)
150		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
151	hci_dev_unlock(hdev);
152
153	return 0;
154}
155
156DEFINE_SHOW_ATTRIBUTE(blacklist);
157
158static int blocked_keys_show(struct seq_file *f, void *p)
159{
160	struct hci_dev *hdev = f->private;
161	struct blocked_key *key;
162
163	rcu_read_lock();
164	list_for_each_entry_rcu(key, &hdev->blocked_keys, list)
165		seq_printf(f, "%u %*phN\n", key->type, 16, key->val);
166	rcu_read_unlock();
167
168	return 0;
169}
170
171DEFINE_SHOW_ATTRIBUTE(blocked_keys);
172
173static int uuids_show(struct seq_file *f, void *p)
174{
175	struct hci_dev *hdev = f->private;
176	struct bt_uuid *uuid;
177
178	hci_dev_lock(hdev);
179	list_for_each_entry(uuid, &hdev->uuids, list) {
180		u8 i, val[16];
181
182		/* The Bluetooth UUID values are stored in big endian,
183		 * but with reversed byte order. So convert them into
184		 * the right order for the %pUb modifier.
185		 */
186		for (i = 0; i < 16; i++)
187			val[i] = uuid->uuid[15 - i];
188
189		seq_printf(f, "%pUb\n", val);
190	}
191	hci_dev_unlock(hdev);
192
193	return 0;
194}
195
196DEFINE_SHOW_ATTRIBUTE(uuids);
197
198static int remote_oob_show(struct seq_file *f, void *ptr)
199{
200	struct hci_dev *hdev = f->private;
201	struct oob_data *data;
202
203	hci_dev_lock(hdev);
204	list_for_each_entry(data, &hdev->remote_oob_data, list) {
205		seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n",
206			   &data->bdaddr, data->bdaddr_type, data->present,
207			   16, data->hash192, 16, data->rand192,
208			   16, data->hash256, 16, data->rand256);
209	}
210	hci_dev_unlock(hdev);
211
212	return 0;
213}
214
215DEFINE_SHOW_ATTRIBUTE(remote_oob);
216
217static int conn_info_min_age_set(void *data, u64 val)
218{
219	struct hci_dev *hdev = data;
220
221	hci_dev_lock(hdev);
222	if (val == 0 || val > hdev->conn_info_max_age) {
223		hci_dev_unlock(hdev);
224		return -EINVAL;
225	}
226
227	hdev->conn_info_min_age = val;
228	hci_dev_unlock(hdev);
229
230	return 0;
231}
232
233static int conn_info_min_age_get(void *data, u64 *val)
234{
235	struct hci_dev *hdev = data;
236
237	hci_dev_lock(hdev);
238	*val = hdev->conn_info_min_age;
239	hci_dev_unlock(hdev);
240
241	return 0;
242}
243
244DEFINE_DEBUGFS_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
245			  conn_info_min_age_set, "%llu\n");
246
247static int conn_info_max_age_set(void *data, u64 val)
248{
249	struct hci_dev *hdev = data;
250
251	hci_dev_lock(hdev);
252	if (val == 0 || val < hdev->conn_info_min_age) {
253		hci_dev_unlock(hdev);
254		return -EINVAL;
255	}
256
257	hdev->conn_info_max_age = val;
258	hci_dev_unlock(hdev);
259
260	return 0;
261}
262
263static int conn_info_max_age_get(void *data, u64 *val)
264{
265	struct hci_dev *hdev = data;
266
267	hci_dev_lock(hdev);
268	*val = hdev->conn_info_max_age;
269	hci_dev_unlock(hdev);
270
271	return 0;
272}
273
274DEFINE_DEBUGFS_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
275			  conn_info_max_age_set, "%llu\n");
276
277static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
278				   size_t count, loff_t *ppos)
279{
280	struct hci_dev *hdev = file->private_data;
281	char buf[3];
282
283	buf[0] = hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS) ? 'Y' : 'N';
284	buf[1] = '\n';
285	buf[2] = '\0';
286	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
287}
288
289static const struct file_operations use_debug_keys_fops = {
290	.open		= simple_open,
291	.read		= use_debug_keys_read,
292	.llseek		= default_llseek,
293};
294
295static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
296				 size_t count, loff_t *ppos)
297{
298	struct hci_dev *hdev = file->private_data;
299	char buf[3];
300
301	buf[0] = hci_dev_test_flag(hdev, HCI_SC_ONLY) ? 'Y' : 'N';
302	buf[1] = '\n';
303	buf[2] = '\0';
304	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
305}
306
307static const struct file_operations sc_only_mode_fops = {
308	.open		= simple_open,
309	.read		= sc_only_mode_read,
310	.llseek		= default_llseek,
311};
312
313DEFINE_INFO_ATTRIBUTE(hardware_info, hw_info);
314DEFINE_INFO_ATTRIBUTE(firmware_info, fw_info);
315
316void hci_debugfs_create_common(struct hci_dev *hdev)
317{
318	debugfs_create_file("features", 0444, hdev->debugfs, hdev,
319			    &features_fops);
320	debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
321			   &hdev->manufacturer);
322	debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
323	debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
324	debugfs_create_u8("hardware_error", 0444, hdev->debugfs,
325			  &hdev->hw_error_code);
326	debugfs_create_file("device_id", 0444, hdev->debugfs, hdev,
327			    &device_id_fops);
328
329	debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
330			    &device_list_fops);
331	debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
332			    &blacklist_fops);
333	debugfs_create_file("blocked_keys", 0444, hdev->debugfs, hdev,
334			    &blocked_keys_fops);
335	debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
336	debugfs_create_file("remote_oob", 0400, hdev->debugfs, hdev,
337			    &remote_oob_fops);
338
339	debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev,
340			    &conn_info_min_age_fops);
341	debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev,
342			    &conn_info_max_age_fops);
343
344	if (lmp_ssp_capable(hdev) || lmp_le_capable(hdev))
345		debugfs_create_file("use_debug_keys", 0444, hdev->debugfs,
346				    hdev, &use_debug_keys_fops);
347
348	if (lmp_sc_capable(hdev) || lmp_le_capable(hdev))
349		debugfs_create_file("sc_only_mode", 0444, hdev->debugfs,
350				    hdev, &sc_only_mode_fops);
351
352	if (hdev->hw_info)
353		debugfs_create_file("hardware_info", 0444, hdev->debugfs,
354				    hdev, &hardware_info_fops);
355
356	if (hdev->fw_info)
357		debugfs_create_file("firmware_info", 0444, hdev->debugfs,
358				    hdev, &firmware_info_fops);
359}
360
361static int inquiry_cache_show(struct seq_file *f, void *p)
362{
363	struct hci_dev *hdev = f->private;
364	struct discovery_state *cache = &hdev->discovery;
365	struct inquiry_entry *e;
366
367	hci_dev_lock(hdev);
368
369	list_for_each_entry(e, &cache->all, all) {
370		struct inquiry_data *data = &e->data;
371		seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
372			   &data->bdaddr,
373			   data->pscan_rep_mode, data->pscan_period_mode,
374			   data->pscan_mode, data->dev_class[2],
375			   data->dev_class[1], data->dev_class[0],
376			   __le16_to_cpu(data->clock_offset),
377			   data->rssi, data->ssp_mode, e->timestamp);
378	}
379
380	hci_dev_unlock(hdev);
381
382	return 0;
383}
384
385DEFINE_SHOW_ATTRIBUTE(inquiry_cache);
386
387static int link_keys_show(struct seq_file *f, void *ptr)
388{
389	struct hci_dev *hdev = f->private;
390	struct link_key *key;
391
392	rcu_read_lock();
393	list_for_each_entry_rcu(key, &hdev->link_keys, list)
394		seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
395			   HCI_LINK_KEY_SIZE, key->val, key->pin_len);
396	rcu_read_unlock();
397
398	return 0;
399}
400
401DEFINE_SHOW_ATTRIBUTE(link_keys);
402
403static int dev_class_show(struct seq_file *f, void *ptr)
404{
405	struct hci_dev *hdev = f->private;
406
407	hci_dev_lock(hdev);
408	seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
409		   hdev->dev_class[1], hdev->dev_class[0]);
410	hci_dev_unlock(hdev);
411
412	return 0;
413}
414
415DEFINE_SHOW_ATTRIBUTE(dev_class);
416
417static int voice_setting_get(void *data, u64 *val)
418{
419	struct hci_dev *hdev = data;
420
421	hci_dev_lock(hdev);
422	*val = hdev->voice_setting;
423	hci_dev_unlock(hdev);
424
425	return 0;
426}
427
428DEFINE_DEBUGFS_ATTRIBUTE(voice_setting_fops, voice_setting_get,
429			  NULL, "0x%4.4llx\n");
430
431static ssize_t ssp_debug_mode_read(struct file *file, char __user *user_buf,
432				   size_t count, loff_t *ppos)
433{
434	struct hci_dev *hdev = file->private_data;
435	char buf[3];
436
437	buf[0] = hdev->ssp_debug_mode ? 'Y' : 'N';
438	buf[1] = '\n';
439	buf[2] = '\0';
440	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
441}
442
443static const struct file_operations ssp_debug_mode_fops = {
444	.open		= simple_open,
445	.read		= ssp_debug_mode_read,
446	.llseek		= default_llseek,
447};
448
449static int auto_accept_delay_set(void *data, u64 val)
450{
451	struct hci_dev *hdev = data;
452
453	hci_dev_lock(hdev);
454	hdev->auto_accept_delay = val;
455	hci_dev_unlock(hdev);
456
457	return 0;
458}
459
460static int min_encrypt_key_size_set(void *data, u64 val)
461{
462	struct hci_dev *hdev = data;
463
464	if (val < 1 || val > 16)
465		return -EINVAL;
466
467	hci_dev_lock(hdev);
468	hdev->min_enc_key_size = val;
469	hci_dev_unlock(hdev);
470
471	return 0;
472}
473
474static int min_encrypt_key_size_get(void *data, u64 *val)
475{
476	struct hci_dev *hdev = data;
477
478	hci_dev_lock(hdev);
479	*val = hdev->min_enc_key_size;
480	hci_dev_unlock(hdev);
481
482	return 0;
483}
484
485DEFINE_DEBUGFS_ATTRIBUTE(min_encrypt_key_size_fops,
486			  min_encrypt_key_size_get,
487			  min_encrypt_key_size_set, "%llu\n");
488
489static int auto_accept_delay_get(void *data, u64 *val)
490{
491	struct hci_dev *hdev = data;
492
493	hci_dev_lock(hdev);
494	*val = hdev->auto_accept_delay;
495	hci_dev_unlock(hdev);
496
497	return 0;
498}
499
500DEFINE_DEBUGFS_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
501			  auto_accept_delay_set, "%llu\n");
502
503static ssize_t force_bredr_smp_read(struct file *file,
504				    char __user *user_buf,
505				    size_t count, loff_t *ppos)
506{
507	struct hci_dev *hdev = file->private_data;
508	char buf[3];
509
510	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y' : 'N';
511	buf[1] = '\n';
512	buf[2] = '\0';
513	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
514}
515
516static ssize_t force_bredr_smp_write(struct file *file,
517				     const char __user *user_buf,
518				     size_t count, loff_t *ppos)
519{
520	struct hci_dev *hdev = file->private_data;
521	bool enable;
522	int err;
523
524	err = kstrtobool_from_user(user_buf, count, &enable);
525	if (err)
526		return err;
527
528	err = smp_force_bredr(hdev, enable);
529	if (err)
530		return err;
531
532	return count;
533}
534
535static const struct file_operations force_bredr_smp_fops = {
536	.open		= simple_open,
537	.read		= force_bredr_smp_read,
538	.write		= force_bredr_smp_write,
539	.llseek		= default_llseek,
540};
541
542static int idle_timeout_set(void *data, u64 val)
543{
544	struct hci_dev *hdev = data;
545
546	if (val != 0 && (val < 500 || val > 3600000))
547		return -EINVAL;
548
549	hci_dev_lock(hdev);
550	hdev->idle_timeout = val;
551	hci_dev_unlock(hdev);
552
553	return 0;
554}
555
556static int idle_timeout_get(void *data, u64 *val)
557{
558	struct hci_dev *hdev = data;
559
560	hci_dev_lock(hdev);
561	*val = hdev->idle_timeout;
562	hci_dev_unlock(hdev);
563
564	return 0;
565}
566
567DEFINE_DEBUGFS_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
568			  idle_timeout_set, "%llu\n");
569
570static int sniff_min_interval_set(void *data, u64 val)
571{
572	struct hci_dev *hdev = data;
573
574	hci_dev_lock(hdev);
575	if (val == 0 || val % 2 || val > hdev->sniff_max_interval) {
576		hci_dev_unlock(hdev);
577		return -EINVAL;
578	}
579
580	hdev->sniff_min_interval = val;
581	hci_dev_unlock(hdev);
582
583	return 0;
584}
585
586static int sniff_min_interval_get(void *data, u64 *val)
587{
588	struct hci_dev *hdev = data;
589
590	hci_dev_lock(hdev);
591	*val = hdev->sniff_min_interval;
592	hci_dev_unlock(hdev);
593
594	return 0;
595}
596
597DEFINE_DEBUGFS_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
598			  sniff_min_interval_set, "%llu\n");
599
600static int sniff_max_interval_set(void *data, u64 val)
601{
602	struct hci_dev *hdev = data;
603
604	hci_dev_lock(hdev);
605	if (val == 0 || val % 2 || val < hdev->sniff_min_interval) {
606		hci_dev_unlock(hdev);
607		return -EINVAL;
608	}
609
610	hdev->sniff_max_interval = val;
611	hci_dev_unlock(hdev);
612
613	return 0;
614}
615
616static int sniff_max_interval_get(void *data, u64 *val)
617{
618	struct hci_dev *hdev = data;
619
620	hci_dev_lock(hdev);
621	*val = hdev->sniff_max_interval;
622	hci_dev_unlock(hdev);
623
624	return 0;
625}
626
627DEFINE_DEBUGFS_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
628			  sniff_max_interval_set, "%llu\n");
629
630void hci_debugfs_create_bredr(struct hci_dev *hdev)
631{
632	debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, hdev,
633			    &inquiry_cache_fops);
634	debugfs_create_file("link_keys", 0400, hdev->debugfs, hdev,
635			    &link_keys_fops);
636	debugfs_create_file("dev_class", 0444, hdev->debugfs, hdev,
637			    &dev_class_fops);
638	debugfs_create_file("voice_setting", 0444, hdev->debugfs, hdev,
639			    &voice_setting_fops);
640
641	/* If the controller does not support BR/EDR Secure Connections
642	 * feature, then the BR/EDR SMP channel shall not be present.
643	 *
644	 * To test this with Bluetooth 4.0 controllers, create a debugfs
645	 * switch that allows forcing BR/EDR SMP support and accepting
646	 * cross-transport pairing on non-AES encrypted connections.
647	 */
648	if (!lmp_sc_capable(hdev))
649		debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
650				    hdev, &force_bredr_smp_fops);
651
652	if (lmp_ssp_capable(hdev)) {
653		debugfs_create_file("ssp_debug_mode", 0444, hdev->debugfs,
654				    hdev, &ssp_debug_mode_fops);
655		debugfs_create_file("min_encrypt_key_size", 0644, hdev->debugfs,
656				    hdev, &min_encrypt_key_size_fops);
657		debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
658				    hdev, &auto_accept_delay_fops);
659	}
660
661	if (lmp_sniff_capable(hdev)) {
662		debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
663				    hdev, &idle_timeout_fops);
664		debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
665				    hdev, &sniff_min_interval_fops);
666		debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
667				    hdev, &sniff_max_interval_fops);
668	}
669}
670
671static int identity_show(struct seq_file *f, void *p)
672{
673	struct hci_dev *hdev = f->private;
674	bdaddr_t addr;
675	u8 addr_type;
676
677	hci_dev_lock(hdev);
678
679	hci_copy_identity_address(hdev, &addr, &addr_type);
680
681	seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,
682		   16, hdev->irk, &hdev->rpa);
683
684	hci_dev_unlock(hdev);
685
686	return 0;
687}
688
689DEFINE_SHOW_ATTRIBUTE(identity);
690
691static int rpa_timeout_set(void *data, u64 val)
692{
693	struct hci_dev *hdev = data;
694
695	/* Require the RPA timeout to be at least 30 seconds and at most
696	 * 24 hours.
697	 */
698	if (val < 30 || val > (60 * 60 * 24))
699		return -EINVAL;
700
701	hci_dev_lock(hdev);
702	hdev->rpa_timeout = val;
703	hci_dev_unlock(hdev);
704
705	return 0;
706}
707
708static int rpa_timeout_get(void *data, u64 *val)
709{
710	struct hci_dev *hdev = data;
711
712	hci_dev_lock(hdev);
713	*val = hdev->rpa_timeout;
714	hci_dev_unlock(hdev);
715
716	return 0;
717}
718
719DEFINE_DEBUGFS_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
720			  rpa_timeout_set, "%llu\n");
721
722static int random_address_show(struct seq_file *f, void *p)
723{
724	struct hci_dev *hdev = f->private;
725
726	hci_dev_lock(hdev);
727	seq_printf(f, "%pMR\n", &hdev->random_addr);
728	hci_dev_unlock(hdev);
729
730	return 0;
731}
732
733DEFINE_SHOW_ATTRIBUTE(random_address);
734
735static int static_address_show(struct seq_file *f, void *p)
736{
737	struct hci_dev *hdev = f->private;
738
739	hci_dev_lock(hdev);
740	seq_printf(f, "%pMR\n", &hdev->static_addr);
741	hci_dev_unlock(hdev);
742
743	return 0;
744}
745
746DEFINE_SHOW_ATTRIBUTE(static_address);
747
748static ssize_t force_static_address_read(struct file *file,
749					 char __user *user_buf,
750					 size_t count, loff_t *ppos)
751{
752	struct hci_dev *hdev = file->private_data;
753	char buf[3];
754
755	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ? 'Y' : 'N';
756	buf[1] = '\n';
757	buf[2] = '\0';
758	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
759}
760
761static ssize_t force_static_address_write(struct file *file,
762					  const char __user *user_buf,
763					  size_t count, loff_t *ppos)
764{
765	struct hci_dev *hdev = file->private_data;
766	bool enable;
767	int err;
768
769	if (hdev_is_powered(hdev))
770		return -EBUSY;
771
772	err = kstrtobool_from_user(user_buf, count, &enable);
773	if (err)
774		return err;
775
776	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
777		return -EALREADY;
778
779	hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
780
781	return count;
782}
783
784static const struct file_operations force_static_address_fops = {
785	.open		= simple_open,
786	.read		= force_static_address_read,
787	.write		= force_static_address_write,
788	.llseek		= default_llseek,
789};
790
791static int white_list_show(struct seq_file *f, void *ptr)
792{
793	struct hci_dev *hdev = f->private;
794	struct bdaddr_list *b;
795
796	hci_dev_lock(hdev);
797	list_for_each_entry(b, &hdev->le_accept_list, list)
798		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
799	hci_dev_unlock(hdev);
800
801	return 0;
802}
803
804DEFINE_SHOW_ATTRIBUTE(white_list);
805
806static int resolv_list_show(struct seq_file *f, void *ptr)
807{
808	struct hci_dev *hdev = f->private;
809	struct bdaddr_list *b;
810
811	hci_dev_lock(hdev);
812	list_for_each_entry(b, &hdev->le_resolv_list, list)
813		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
814	hci_dev_unlock(hdev);
815
816	return 0;
817}
818
819DEFINE_SHOW_ATTRIBUTE(resolv_list);
820
821static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
822{
823	struct hci_dev *hdev = f->private;
824	struct smp_irk *irk;
825
826	rcu_read_lock();
827	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
828		seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
829			   &irk->bdaddr, irk->addr_type,
830			   16, irk->val, &irk->rpa);
831	}
832	rcu_read_unlock();
833
834	return 0;
835}
836
837DEFINE_SHOW_ATTRIBUTE(identity_resolving_keys);
838
839static int long_term_keys_show(struct seq_file *f, void *ptr)
840{
841	struct hci_dev *hdev = f->private;
842	struct smp_ltk *ltk;
843
844	rcu_read_lock();
845	list_for_each_entry_rcu(ltk, &hdev->long_term_keys, list)
846		seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
847			   &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
848			   ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
849			   __le64_to_cpu(ltk->rand), 16, ltk->val);
850	rcu_read_unlock();
851
852	return 0;
853}
854
855DEFINE_SHOW_ATTRIBUTE(long_term_keys);
856
857static int conn_min_interval_set(void *data, u64 val)
858{
859	struct hci_dev *hdev = data;
860
861	hci_dev_lock(hdev);
862	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) {
863		hci_dev_unlock(hdev);
864		return -EINVAL;
865	}
866
867	hdev->le_conn_min_interval = val;
868	hci_dev_unlock(hdev);
869
870	return 0;
871}
872
873static int conn_min_interval_get(void *data, u64 *val)
874{
875	struct hci_dev *hdev = data;
876
877	hci_dev_lock(hdev);
878	*val = hdev->le_conn_min_interval;
879	hci_dev_unlock(hdev);
880
881	return 0;
882}
883
884DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
885			  conn_min_interval_set, "%llu\n");
886
887static int conn_max_interval_set(void *data, u64 val)
888{
889	struct hci_dev *hdev = data;
890
891	hci_dev_lock(hdev);
892	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) {
893		hci_dev_unlock(hdev);
894		return -EINVAL;
895	}
896
897	hdev->le_conn_max_interval = val;
898	hci_dev_unlock(hdev);
899
900	return 0;
901}
902
903static int conn_max_interval_get(void *data, u64 *val)
904{
905	struct hci_dev *hdev = data;
906
907	hci_dev_lock(hdev);
908	*val = hdev->le_conn_max_interval;
909	hci_dev_unlock(hdev);
910
911	return 0;
912}
913
914DEFINE_DEBUGFS_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
915			  conn_max_interval_set, "%llu\n");
916
917static int conn_latency_set(void *data, u64 val)
918{
919	struct hci_dev *hdev = data;
920
921	if (val > 0x01f3)
922		return -EINVAL;
923
924	hci_dev_lock(hdev);
925	hdev->le_conn_latency = val;
926	hci_dev_unlock(hdev);
927
928	return 0;
929}
930
931static int conn_latency_get(void *data, u64 *val)
932{
933	struct hci_dev *hdev = data;
934
935	hci_dev_lock(hdev);
936	*val = hdev->le_conn_latency;
937	hci_dev_unlock(hdev);
938
939	return 0;
940}
941
942DEFINE_DEBUGFS_ATTRIBUTE(conn_latency_fops, conn_latency_get,
943			  conn_latency_set, "%llu\n");
944
945static int supervision_timeout_set(void *data, u64 val)
946{
947	struct hci_dev *hdev = data;
948
949	if (val < 0x000a || val > 0x0c80)
950		return -EINVAL;
951
952	hci_dev_lock(hdev);
953	hdev->le_supv_timeout = val;
954	hci_dev_unlock(hdev);
955
956	return 0;
957}
958
959static int supervision_timeout_get(void *data, u64 *val)
960{
961	struct hci_dev *hdev = data;
962
963	hci_dev_lock(hdev);
964	*val = hdev->le_supv_timeout;
965	hci_dev_unlock(hdev);
966
967	return 0;
968}
969
970DEFINE_DEBUGFS_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
971			  supervision_timeout_set, "%llu\n");
972
973static int adv_channel_map_set(void *data, u64 val)
974{
975	struct hci_dev *hdev = data;
976
977	if (val < 0x01 || val > 0x07)
978		return -EINVAL;
979
980	hci_dev_lock(hdev);
981	hdev->le_adv_channel_map = val;
982	hci_dev_unlock(hdev);
983
984	return 0;
985}
986
987static int adv_channel_map_get(void *data, u64 *val)
988{
989	struct hci_dev *hdev = data;
990
991	hci_dev_lock(hdev);
992	*val = hdev->le_adv_channel_map;
993	hci_dev_unlock(hdev);
994
995	return 0;
996}
997
998DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
999			  adv_channel_map_set, "%llu\n");
1000
1001static int adv_min_interval_set(void *data, u64 val)
1002{
1003	struct hci_dev *hdev = data;
1004
1005	hci_dev_lock(hdev);
1006	if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval) {
1007		hci_dev_unlock(hdev);
1008		return -EINVAL;
1009	}
1010
1011	hdev->le_adv_min_interval = val;
1012	hci_dev_unlock(hdev);
1013
1014	return 0;
1015}
1016
1017static int adv_min_interval_get(void *data, u64 *val)
1018{
1019	struct hci_dev *hdev = data;
1020
1021	hci_dev_lock(hdev);
1022	*val = hdev->le_adv_min_interval;
1023	hci_dev_unlock(hdev);
1024
1025	return 0;
1026}
1027
1028DEFINE_DEBUGFS_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
1029			  adv_min_interval_set, "%llu\n");
1030
1031static int adv_max_interval_set(void *data, u64 val)
1032{
1033	struct hci_dev *hdev = data;
1034
1035	hci_dev_lock(hdev);
1036	if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval) {
1037		hci_dev_unlock(hdev);
1038		return -EINVAL;
1039	}
1040
1041	hdev->le_adv_max_interval = val;
1042	hci_dev_unlock(hdev);
1043
1044	return 0;
1045}
1046
1047static int adv_max_interval_get(void *data, u64 *val)
1048{
1049	struct hci_dev *hdev = data;
1050
1051	hci_dev_lock(hdev);
1052	*val = hdev->le_adv_max_interval;
1053	hci_dev_unlock(hdev);
1054
1055	return 0;
1056}
1057
1058DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
1059			  adv_max_interval_set, "%llu\n");
1060
1061static int min_key_size_set(void *data, u64 val)
1062{
1063	struct hci_dev *hdev = data;
1064
1065	hci_dev_lock(hdev);
1066	if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) {
1067		hci_dev_unlock(hdev);
1068		return -EINVAL;
1069	}
1070
1071	hdev->le_min_key_size = val;
1072	hci_dev_unlock(hdev);
1073
1074	return 0;
1075}
1076
1077static int min_key_size_get(void *data, u64 *val)
1078{
1079	struct hci_dev *hdev = data;
1080
1081	hci_dev_lock(hdev);
1082	*val = hdev->le_min_key_size;
1083	hci_dev_unlock(hdev);
1084
1085	return 0;
1086}
1087
1088DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
1089			  min_key_size_set, "%llu\n");
1090
1091static int max_key_size_set(void *data, u64 val)
1092{
1093	struct hci_dev *hdev = data;
1094
1095	hci_dev_lock(hdev);
1096	if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) {
1097		hci_dev_unlock(hdev);
1098		return -EINVAL;
1099	}
1100
1101	hdev->le_max_key_size = val;
1102	hci_dev_unlock(hdev);
1103
1104	return 0;
1105}
1106
1107static int max_key_size_get(void *data, u64 *val)
1108{
1109	struct hci_dev *hdev = data;
1110
1111	hci_dev_lock(hdev);
1112	*val = hdev->le_max_key_size;
1113	hci_dev_unlock(hdev);
1114
1115	return 0;
1116}
1117
1118DEFINE_DEBUGFS_ATTRIBUTE(max_key_size_fops, max_key_size_get,
1119			  max_key_size_set, "%llu\n");
1120
1121static int auth_payload_timeout_set(void *data, u64 val)
1122{
1123	struct hci_dev *hdev = data;
1124
1125	if (val < 0x0001 || val > 0xffff)
1126		return -EINVAL;
1127
1128	hci_dev_lock(hdev);
1129	hdev->auth_payload_timeout = val;
1130	hci_dev_unlock(hdev);
1131
1132	return 0;
1133}
1134
1135static int auth_payload_timeout_get(void *data, u64 *val)
1136{
1137	struct hci_dev *hdev = data;
1138
1139	hci_dev_lock(hdev);
1140	*val = hdev->auth_payload_timeout;
1141	hci_dev_unlock(hdev);
1142
1143	return 0;
1144}
1145
1146DEFINE_DEBUGFS_ATTRIBUTE(auth_payload_timeout_fops,
1147			  auth_payload_timeout_get,
1148			  auth_payload_timeout_set, "%llu\n");
1149
1150static ssize_t force_no_mitm_read(struct file *file,
1151				  char __user *user_buf,
1152				  size_t count, loff_t *ppos)
1153{
1154	struct hci_dev *hdev = file->private_data;
1155	char buf[3];
1156
1157	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM) ? 'Y' : 'N';
1158	buf[1] = '\n';
1159	buf[2] = '\0';
1160	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1161}
1162
1163static ssize_t force_no_mitm_write(struct file *file,
1164				   const char __user *user_buf,
1165				   size_t count, loff_t *ppos)
1166{
1167	struct hci_dev *hdev = file->private_data;
1168	char buf[32];
1169	size_t buf_size = min(count, (sizeof(buf) - 1));
1170	bool enable;
1171
1172	if (copy_from_user(buf, user_buf, buf_size))
1173		return -EFAULT;
1174
1175	buf[buf_size] = '\0';
1176	if (kstrtobool(buf, &enable))
1177		return -EINVAL;
1178
1179	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
1180		return -EALREADY;
1181
1182	hci_dev_change_flag(hdev, HCI_FORCE_NO_MITM);
1183
1184	return count;
1185}
1186
1187static const struct file_operations force_no_mitm_fops = {
1188	.open		= simple_open,
1189	.read		= force_no_mitm_read,
1190	.write		= force_no_mitm_write,
1191	.llseek		= default_llseek,
1192};
1193
1194DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1195		       HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1196DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1197		       HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1198
1199void hci_debugfs_create_le(struct hci_dev *hdev)
1200{
1201	debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
1202			    &identity_fops);
1203	debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
1204			    &rpa_timeout_fops);
1205	debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
1206			    &random_address_fops);
1207	debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
1208			    &static_address_fops);
1209
1210	/* For controllers with a public address, provide a debug
1211	 * option to force the usage of the configured static
1212	 * address. By default the public address is used.
1213	 */
1214	if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1215		debugfs_create_file("force_static_address", 0644,
1216				    hdev->debugfs, hdev,
1217				    &force_static_address_fops);
1218
1219	debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1220			  &hdev->le_accept_list_size);
1221	debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1222			    &white_list_fops);
1223	debugfs_create_u8("resolv_list_size", 0444, hdev->debugfs,
1224			  &hdev->le_resolv_list_size);
1225	debugfs_create_file("resolv_list", 0444, hdev->debugfs, hdev,
1226			    &resolv_list_fops);
1227	debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
1228			    hdev, &identity_resolving_keys_fops);
1229	debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
1230			    &long_term_keys_fops);
1231	debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
1232			    &conn_min_interval_fops);
1233	debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
1234			    &conn_max_interval_fops);
1235	debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
1236			    &conn_latency_fops);
1237	debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
1238			    &supervision_timeout_fops);
1239	debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
1240			    &adv_channel_map_fops);
1241	debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
1242			    &adv_min_interval_fops);
1243	debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
1244			    &adv_max_interval_fops);
1245	debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1246			   &hdev->discov_interleaved_timeout);
1247	debugfs_create_file("min_key_size", 0644, hdev->debugfs, hdev,
1248			    &min_key_size_fops);
1249	debugfs_create_file("max_key_size", 0644, hdev->debugfs, hdev,
1250			    &max_key_size_fops);
1251	debugfs_create_file("auth_payload_timeout", 0644, hdev->debugfs, hdev,
1252			    &auth_payload_timeout_fops);
1253	debugfs_create_file("force_no_mitm", 0644, hdev->debugfs, hdev,
1254			    &force_no_mitm_fops);
1255
1256	debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1257			    hdev->debugfs, hdev,
1258			    &quirk_strict_duplicate_filter_fops);
1259	debugfs_create_file("quirk_simultaneous_discovery", 0644,
1260			    hdev->debugfs, hdev,
1261			    &quirk_simultaneous_discovery_fops);
1262}
1263
1264void hci_debugfs_create_conn(struct hci_conn *conn)
1265{
1266	struct hci_dev *hdev = conn->hdev;
1267	char name[6];
1268
1269	if (IS_ERR_OR_NULL(hdev->debugfs) || conn->debugfs)
1270		return;
1271
1272	snprintf(name, sizeof(name), "%u", conn->handle);
1273	conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
1274}
1275
1276static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
1277			     size_t count, loff_t *ppos)
1278{
1279	struct hci_dev *hdev = file->private_data;
1280	char buf[3];
1281
1282	buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N';
1283	buf[1] = '\n';
1284	buf[2] = '\0';
1285	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1286}
1287
1288static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
1289			      size_t count, loff_t *ppos)
1290{
1291	struct hci_dev *hdev = file->private_data;
1292	struct sk_buff *skb;
1293	bool enable;
1294	int err;
1295
1296	if (!test_bit(HCI_UP, &hdev->flags))
1297		return -ENETDOWN;
1298
1299	err = kstrtobool_from_user(user_buf, count, &enable);
1300	if (err)
1301		return err;
1302
1303	if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
1304		return -EALREADY;
1305
1306	hci_req_sync_lock(hdev);
1307	if (enable)
1308		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1309				     HCI_CMD_TIMEOUT);
1310	else
1311		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1312				     HCI_CMD_TIMEOUT);
1313	hci_req_sync_unlock(hdev);
1314
1315	if (IS_ERR(skb))
1316		return PTR_ERR(skb);
1317
1318	kfree_skb(skb);
1319
1320	hci_dev_change_flag(hdev, HCI_DUT_MODE);
1321
1322	return count;
1323}
1324
1325static const struct file_operations dut_mode_fops = {
1326	.open		= simple_open,
1327	.read		= dut_mode_read,
1328	.write		= dut_mode_write,
1329	.llseek		= default_llseek,
1330};
1331
1332static ssize_t vendor_diag_read(struct file *file, char __user *user_buf,
1333				size_t count, loff_t *ppos)
1334{
1335	struct hci_dev *hdev = file->private_data;
1336	char buf[3];
1337
1338	buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N';
1339	buf[1] = '\n';
1340	buf[2] = '\0';
1341	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1342}
1343
1344static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf,
1345				 size_t count, loff_t *ppos)
1346{
1347	struct hci_dev *hdev = file->private_data;
1348	bool enable;
1349	int err;
1350
1351	err = kstrtobool_from_user(user_buf, count, &enable);
1352	if (err)
1353		return err;
1354
1355	/* When the diagnostic flags are not persistent and the transport
1356	 * is not active or in user channel operation, then there is no need
1357	 * for the vendor callback. Instead just store the desired value and
1358	 * the setting will be programmed when the controller gets powered on.
1359	 */
1360	if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
1361	    (!test_bit(HCI_RUNNING, &hdev->flags) ||
1362	     hci_dev_test_flag(hdev, HCI_USER_CHANNEL)))
1363		goto done;
1364
1365	hci_req_sync_lock(hdev);
1366	err = hdev->set_diag(hdev, enable);
1367	hci_req_sync_unlock(hdev);
1368
1369	if (err < 0)
1370		return err;
1371
1372done:
1373	if (enable)
1374		hci_dev_set_flag(hdev, HCI_VENDOR_DIAG);
1375	else
1376		hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG);
1377
1378	return count;
1379}
1380
1381static const struct file_operations vendor_diag_fops = {
1382	.open		= simple_open,
1383	.read		= vendor_diag_read,
1384	.write		= vendor_diag_write,
1385	.llseek		= default_llseek,
1386};
1387
1388void hci_debugfs_create_basic(struct hci_dev *hdev)
1389{
1390	debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
1391			    &dut_mode_fops);
1392
1393	if (hdev->set_diag)
1394		debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev,
1395				    &vendor_diag_fops);
1396}
1397