Blame view

net/nfc/llcp_core.c 34.5 KB
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1
2
  /*
   * Copyright (C) 2011  Intel Corporation. All rights reserved.
966efbfb0   Julien Lefrique   NFC: Fix a memory...
3
   * Copyright (C) 2014 Marvell International Ltd.
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
4
5
6
7
8
9
10
11
12
13
14
15
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
98b32decc   Jeff Kirsher   nfc: Fix FSF addr...
16
   * along with this program; if not, see <http://www.gnu.org/licenses/>.
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
17
18
19
20
21
22
23
24
   */
  
  #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
  
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/list.h>
  #include <linux/nfc.h>
30cc45876   Samuel Ortiz   NFC: Move LLCP co...
25
  #include "nfc.h"
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
26
27
28
  #include "llcp.h"
  
  static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
0b51fc563   Axel Lin   NFC: Use LIST_HEA...
29
  static LIST_HEAD(llcp_devices);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
30

098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
31
  static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
32
  void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
33
  {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
34
35
36
37
  	write_lock(&l->lock);
  	sk_add_node(sk, &l->head);
  	write_unlock(&l->lock);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
38

a69f32af8   Samuel Ortiz   NFC: Socket linke...
39
40
41
42
43
44
  void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
  {
  	write_lock(&l->lock);
  	sk_del_node_init(sk);
  	write_unlock(&l->lock);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
45

abd18d433   Thierry Escande   NFC: llcp: Reset ...
46
47
48
49
50
  void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
  {
  	sock->remote_rw = LLCP_DEFAULT_RW;
  	sock->remote_miu = LLCP_MAX_MIU + 1;
  }
f31652a58   Samuel Ortiz   NFC: Purge LLCP s...
51
52
53
54
55
56
57
58
59
60
  static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
  {
  	struct nfc_llcp_local *local = sock->local;
  	struct sk_buff *s, *tmp;
  
  	pr_debug("%p
  ", &sock->sk);
  
  	skb_queue_purge(&sock->tx_queue);
  	skb_queue_purge(&sock->tx_pending_queue);
f31652a58   Samuel Ortiz   NFC: Purge LLCP s...
61
62
63
64
65
66
67
68
69
70
71
72
73
  
  	if (local == NULL)
  		return;
  
  	/* Search for local pending SKBs that are related to this socket */
  	skb_queue_walk_safe(&local->tx_queue, s, tmp) {
  		if (s->sk != &sock->sk)
  			continue;
  
  		skb_unlink(s, &local->tx_queue);
  		kfree_skb(s);
  	}
  }
b436a13de   Samuel Ortiz   NFC: llcp: Only k...
74
  static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
75
  				    int err)
a69f32af8   Samuel Ortiz   NFC: Socket linke...
76
77
  {
  	struct sock *sk;
b67bfe0d4   Sasha Levin   hlist: drop the n...
78
  	struct hlist_node *tmp;
a69f32af8   Samuel Ortiz   NFC: Socket linke...
79
  	struct nfc_llcp_sock *llcp_sock;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
80

f31652a58   Samuel Ortiz   NFC: Purge LLCP s...
81
  	skb_queue_purge(&local->tx_queue);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
82
  	write_lock(&local->sockets.lock);
40c75f81d   Samuel Ortiz   NFC: Fix LLCP soc...
83

b67bfe0d4   Sasha Levin   hlist: drop the n...
84
  	sk_for_each_safe(sk, tmp, &local->sockets.head) {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
85
  		llcp_sock = nfc_llcp_sock(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
86

50b78b2a6   Szymon Janc   NFC: Fix sleeping...
87
  		bh_lock_sock(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
88

f31652a58   Samuel Ortiz   NFC: Purge LLCP s...
89
  		nfc_llcp_socket_purge(llcp_sock);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
90
91
  		if (sk->sk_state == LLCP_CONNECTED)
  			nfc_put_device(llcp_sock->dev);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
92

a69f32af8   Samuel Ortiz   NFC: Socket linke...
93
  		if (sk->sk_state == LLCP_LISTEN) {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
94
95
  			struct nfc_llcp_sock *lsk, *n;
  			struct sock *accept_sk;
0f4507722   Szymon Janc   NFC: Fix some cod...
96
97
  			list_for_each_entry_safe(lsk, n,
  						 &llcp_sock->accept_queue,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
98
  						 accept_queue) {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
99
  				accept_sk = &lsk->sk;
50b78b2a6   Szymon Janc   NFC: Fix sleeping...
100
  				bh_lock_sock(accept_sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
101
102
  
  				nfc_llcp_accept_unlink(accept_sk);
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
103
104
  				if (err)
  					accept_sk->sk_err = err;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
105
  				accept_sk->sk_state = LLCP_CLOSED;
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
106
  				accept_sk->sk_state_change(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
107

50b78b2a6   Szymon Janc   NFC: Fix sleeping...
108
  				bh_unlock_sock(accept_sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
109
  			}
c8512be63   Samuel Ortiz   NFC: Keep connect...
110
  		}
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
111
112
  		if (err)
  			sk->sk_err = err;
a69f32af8   Samuel Ortiz   NFC: Socket linke...
113
  		sk->sk_state = LLCP_CLOSED;
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
114
  		sk->sk_state_change(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
115

50b78b2a6   Szymon Janc   NFC: Fix sleeping...
116
  		bh_unlock_sock(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
117

a69f32af8   Samuel Ortiz   NFC: Socket linke...
118
  		sk_del_node_init(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
119
  	}
a69f32af8   Samuel Ortiz   NFC: Socket linke...
120
  	write_unlock(&local->sockets.lock);
e6a3a4bb8   Samuel Ortiz   NFC: llcp: Clean ...
121

b436a13de   Samuel Ortiz   NFC: llcp: Only k...
122
123
  	/* If we still have a device, we keep the RAW sockets alive */
  	if (device == true)
e6a3a4bb8   Samuel Ortiz   NFC: llcp: Clean ...
124
125
126
127
128
129
130
131
132
133
  		return;
  
  	write_lock(&local->raw_sockets.lock);
  
  	sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
  		llcp_sock = nfc_llcp_sock(sk);
  
  		bh_lock_sock(sk);
  
  		nfc_llcp_socket_purge(llcp_sock);
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
134
135
  		if (err)
  			sk->sk_err = err;
e6a3a4bb8   Samuel Ortiz   NFC: llcp: Clean ...
136
  		sk->sk_state = LLCP_CLOSED;
e6a3a4bb8   Samuel Ortiz   NFC: llcp: Clean ...
137
138
139
  		sk->sk_state_change(sk);
  
  		bh_unlock_sock(sk);
e6a3a4bb8   Samuel Ortiz   NFC: llcp: Clean ...
140
141
142
143
  		sk_del_node_init(sk);
  	}
  
  	write_unlock(&local->raw_sockets.lock);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
144
  }
c7aa12252   Samuel Ortiz   NFC: Take a refer...
145
146
147
148
149
150
  struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
  {
  	kref_get(&local->ref);
  
  	return local;
  }
c470e319b   Samuel Ortiz   NFC: llcp: Remove...
151
  static void local_cleanup(struct nfc_llcp_local *local)
c7aa12252   Samuel Ortiz   NFC: Take a refer...
152
  {
c470e319b   Samuel Ortiz   NFC: llcp: Remove...
153
  	nfc_llcp_socket_release(local, false, ENXIO);
c7aa12252   Samuel Ortiz   NFC: Take a refer...
154
155
  	del_timer_sync(&local->link_timer);
  	skb_queue_purge(&local->tx_queue);
474fee3db   Tejun Heo   NFC: Use system_n...
156
157
158
  	cancel_work_sync(&local->tx_work);
  	cancel_work_sync(&local->rx_work);
  	cancel_work_sync(&local->timeout_work);
c7aa12252   Samuel Ortiz   NFC: Take a refer...
159
  	kfree_skb(local->rx_pending);
40213fa85   Thierry Escande   NFC: llcp: Add cl...
160
161
  	del_timer_sync(&local->sdreq_timer);
  	cancel_work_sync(&local->sdreq_timeout_work);
d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
162
  	nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
3536da06d   Samuel Ortiz   NFC: llcp: Clean ...
163
164
165
166
167
168
169
170
171
  }
  
  static void local_release(struct kref *ref)
  {
  	struct nfc_llcp_local *local;
  
  	local = container_of(ref, struct nfc_llcp_local, ref);
  
  	list_del(&local->list);
c470e319b   Samuel Ortiz   NFC: llcp: Remove...
172
  	local_cleanup(local);
c7aa12252   Samuel Ortiz   NFC: Take a refer...
173
174
175
176
177
  	kfree(local);
  }
  
  int nfc_llcp_local_put(struct nfc_llcp_local *local)
  {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
178
179
  	if (local == NULL)
  		return 0;
c7aa12252   Samuel Ortiz   NFC: Take a refer...
180
181
  	return kref_put(&local->ref, local_release);
  }
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
182
183
184
185
  static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
  					       u8 ssap, u8 dsap)
  {
  	struct sock *sk;
a8df0f379   Samuel Ortiz   NFC: Return NULL ...
186
  	struct nfc_llcp_sock *llcp_sock, *tmp_sock;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
187
188
189
190
191
192
193
194
195
196
  
  	pr_debug("ssap dsap %d %d
  ", ssap, dsap);
  
  	if (ssap == 0 && dsap == 0)
  		return NULL;
  
  	read_lock(&local->sockets.lock);
  
  	llcp_sock = NULL;
b67bfe0d4   Sasha Levin   hlist: drop the n...
197
  	sk_for_each(sk, &local->sockets.head) {
a8df0f379   Samuel Ortiz   NFC: Return NULL ...
198
  		tmp_sock = nfc_llcp_sock(sk);
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
199

a8df0f379   Samuel Ortiz   NFC: Return NULL ...
200
201
  		if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
  			llcp_sock = tmp_sock;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
202
  			break;
a8df0f379   Samuel Ortiz   NFC: Return NULL ...
203
  		}
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  	}
  
  	read_unlock(&local->sockets.lock);
  
  	if (llcp_sock == NULL)
  		return NULL;
  
  	sock_hold(&llcp_sock->sk);
  
  	return llcp_sock;
  }
  
  static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
  {
  	sock_put(&sock->sk);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
220
221
222
  static void nfc_llcp_timeout_work(struct work_struct *work)
  {
  	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
223
  						    timeout_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
224
225
226
227
228
229
230
231
232
233
  
  	nfc_dep_link_down(local->dev);
  }
  
  static void nfc_llcp_symm_timer(unsigned long data)
  {
  	struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  
  	pr_err("SYMM timeout
  ");
916082b07   Linus Torvalds   workqueue: avoid ...
234
  	schedule_work(&local->timeout_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
235
  }
40213fa85   Thierry Escande   NFC: llcp: Add cl...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
  {
  	unsigned long time;
  	HLIST_HEAD(nl_sdres_list);
  	struct hlist_node *n;
  	struct nfc_llcp_sdp_tlv *sdp;
  	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  						    sdreq_timeout_work);
  
  	mutex_lock(&local->sdreq_lock);
  
  	time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
  
  	hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
  		if (time_after(sdp->time, time))
  			continue;
  
  		sdp->sap = LLCP_SDP_UNBOUND;
  
  		hlist_del(&sdp->node);
  
  		hlist_add_head(&sdp->node, &nl_sdres_list);
  	}
  
  	if (!hlist_empty(&local->pending_sdreqs))
  		mod_timer(&local->sdreq_timer,
  			  jiffies + msecs_to_jiffies(3 * local->remote_lto));
  
  	mutex_unlock(&local->sdreq_lock);
  
  	if (!hlist_empty(&nl_sdres_list))
  		nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
  }
  
  static void nfc_llcp_sdreq_timer(unsigned long data)
  {
  	struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  
  	schedule_work(&local->sdreq_timeout_work);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
276
277
  struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
  {
29e27dd86   Axel Lin   NFC: llcp: Use li...
278
  	struct nfc_llcp_local *local;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
279

29e27dd86   Axel Lin   NFC: llcp: Use li...
280
  	list_for_each_entry(local, &llcp_devices, list)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  		if (local->dev == dev)
  			return local;
  
  	pr_debug("No device found
  ");
  
  	return NULL;
  }
  
  static char *wks[] = {
  	NULL,
  	NULL, /* SDP */
  	"urn:nfc:sn:ip",
  	"urn:nfc:sn:obex",
  	"urn:nfc:sn:snep",
  };
  
  static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
  {
  	int sap, num_wks;
  
  	pr_debug("%s
  ", service_name);
  
  	if (service_name == NULL)
  		return -EINVAL;
  
  	num_wks = ARRAY_SIZE(wks);
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
309
  	for (sap = 0; sap < num_wks; sap++) {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
310
311
312
313
314
315
316
317
318
  		if (wks[sap] == NULL)
  			continue;
  
  		if (strncmp(wks[sap], service_name, service_name_len) == 0)
  			return sap;
  	}
  
  	return -EINVAL;
  }
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
319
320
321
322
323
  static
  struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
  					    u8 *sn, size_t sn_len)
  {
  	struct sock *sk;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
324
325
326
327
328
329
330
331
332
333
334
  	struct nfc_llcp_sock *llcp_sock, *tmp_sock;
  
  	pr_debug("sn %zd %p
  ", sn_len, sn);
  
  	if (sn == NULL || sn_len == 0)
  		return NULL;
  
  	read_lock(&local->sockets.lock);
  
  	llcp_sock = NULL;
b67bfe0d4   Sasha Levin   hlist: drop the n...
335
  	sk_for_each(sk, &local->sockets.head) {
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
336
337
338
339
  		tmp_sock = nfc_llcp_sock(sk);
  
  		pr_debug("llcp sock %p
  ", tmp_sock);
54292d64e   Samuel Ortiz   NFC: Check for co...
340
341
342
343
344
345
  		if (tmp_sock->sk.sk_type == SOCK_STREAM &&
  		    tmp_sock->sk.sk_state != LLCP_LISTEN)
  			continue;
  
  		if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
  		    tmp_sock->sk.sk_state != LLCP_BOUND)
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  			continue;
  
  		if (tmp_sock->service_name == NULL ||
  		    tmp_sock->service_name_len == 0)
  			continue;
  
  		if (tmp_sock->service_name_len != sn_len)
  			continue;
  
  		if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
  			llcp_sock = tmp_sock;
  			break;
  		}
  	}
  
  	read_unlock(&local->sockets.lock);
  
  	pr_debug("Found llcp sock %p
  ", llcp_sock);
  
  	return llcp_sock;
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
368
  u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
369
  			 struct nfc_llcp_sock *sock)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
370
371
372
373
374
  {
  	mutex_lock(&local->sdp_lock);
  
  	if (sock->service_name != NULL && sock->service_name_len > 0) {
  		int ssap = nfc_llcp_wks_sap(sock->service_name,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
375
  					    sock->service_name_len);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
376
377
378
379
380
381
382
383
384
385
386
  
  		if (ssap > 0) {
  			pr_debug("WKS %d
  ", ssap);
  
  			/* This is a WKS, let's check if it's free */
  			if (local->local_wks & BIT(ssap)) {
  				mutex_unlock(&local->sdp_lock);
  
  				return LLCP_SAP_MAX;
  			}
1762c17c9   Samuel Ortiz   NFC: Fix bitops u...
387
  			set_bit(ssap, &local->local_wks);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
388
389
390
391
392
393
  			mutex_unlock(&local->sdp_lock);
  
  			return ssap;
  		}
  
  		/*
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
394
395
  		 * Check if there already is a non WKS socket bound
  		 * to this service name.
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
396
  		 */
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
397
398
  		if (nfc_llcp_sock_from_sn(local, sock->service_name,
  					  sock->service_name_len) != NULL) {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
399
400
401
402
  			mutex_unlock(&local->sdp_lock);
  
  			return LLCP_SAP_MAX;
  		}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
403
  		mutex_unlock(&local->sdp_lock);
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
404
  		return LLCP_SDP_UNBOUND;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
405

ebbb16d9e   Samuel Ortiz   NFC: Forbid SSAP ...
406
407
408
409
  	} else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
  		if (!test_bit(sock->ssap, &local->local_wks)) {
  			set_bit(sock->ssap, &local->local_wks);
  			mutex_unlock(&local->sdp_lock);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
410

ebbb16d9e   Samuel Ortiz   NFC: Forbid SSAP ...
411
  			return sock->ssap;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  		}
  	}
  
  	mutex_unlock(&local->sdp_lock);
  
  	return LLCP_SAP_MAX;
  }
  
  u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
  {
  	u8 local_ssap;
  
  	mutex_lock(&local->sdp_lock);
  
  	local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
  	if (local_ssap == LLCP_LOCAL_NUM_SAP) {
  		mutex_unlock(&local->sdp_lock);
  		return LLCP_SAP_MAX;
  	}
1762c17c9   Samuel Ortiz   NFC: Fix bitops u...
431
  	set_bit(local_ssap, &local->local_sap);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  
  	mutex_unlock(&local->sdp_lock);
  
  	return local_ssap + LLCP_LOCAL_SAP_OFFSET;
  }
  
  void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
  {
  	u8 local_ssap;
  	unsigned long *sdp;
  
  	if (ssap < LLCP_WKS_NUM_SAP) {
  		local_ssap = ssap;
  		sdp = &local->local_wks;
  	} else if (ssap < LLCP_LOCAL_NUM_SAP) {
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
447
  		atomic_t *client_cnt;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
448
449
  		local_ssap = ssap - LLCP_WKS_NUM_SAP;
  		sdp = &local->local_sdp;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  		client_cnt = &local->local_sdp_cnt[local_ssap];
  
  		pr_debug("%d clients
  ", atomic_read(client_cnt));
  
  		mutex_lock(&local->sdp_lock);
  
  		if (atomic_dec_and_test(client_cnt)) {
  			struct nfc_llcp_sock *l_sock;
  
  			pr_debug("No more clients for SAP %d
  ", ssap);
  
  			clear_bit(local_ssap, sdp);
  
  			/* Find the listening sock and set it back to UNBOUND */
  			l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
  			if (l_sock) {
  				l_sock->ssap = LLCP_SDP_UNBOUND;
  				nfc_llcp_sock_put(l_sock);
  			}
  		}
  
  		mutex_unlock(&local->sdp_lock);
  
  		return;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
476
477
478
479
480
481
482
483
  	} else if (ssap < LLCP_MAX_SAP) {
  		local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
  		sdp = &local->local_sap;
  	} else {
  		return;
  	}
  
  	mutex_lock(&local->sdp_lock);
1762c17c9   Samuel Ortiz   NFC: Fix bitops u...
484
  	clear_bit(local_ssap, sdp);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
485
486
487
  
  	mutex_unlock(&local->sdp_lock);
  }
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
  static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
  {
  	u8 ssap;
  
  	mutex_lock(&local->sdp_lock);
  
  	ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
  	if (ssap == LLCP_SDP_NUM_SAP) {
  		mutex_unlock(&local->sdp_lock);
  
  		return LLCP_SAP_MAX;
  	}
  
  	pr_debug("SDP ssap %d
  ", LLCP_WKS_NUM_SAP + ssap);
  
  	set_bit(ssap, &local->local_sdp);
  
  	mutex_unlock(&local->sdp_lock);
  
  	return LLCP_WKS_NUM_SAP + ssap;
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
510
511
512
  static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
  {
  	u8 *gb_cur, *version_tlv, version, version_length;
52feb444a   Thierry Escande   NFC: Extend netli...
513
  	u8 *lto_tlv, lto_length;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
514
  	u8 *wks_tlv, wks_length;
56d5876a2   Samuel Ortiz   NFC: Add MIUX to ...
515
  	u8 *miux_tlv, miux_length;
4ca546e55   Samuel Ortiz   NFC: llcp: Fix th...
516
  	__be16 wks = cpu_to_be16(local->local_wks);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
517
  	u8 gb_len = 0;
52da2449e   Wei Yongjun   NFC: Fix possible...
518
  	int ret = 0;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
519
520
521
  
  	version = LLCP_VERSION_11;
  	version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
522
  					 1, &version_length);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
523
  	gb_len += version_length;
52feb444a   Thierry Escande   NFC: Extend netli...
524
  	lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
525
526
527
528
  	gb_len += lto_length;
  
  	pr_debug("Local wks 0x%lx
  ", local->local_wks);
4ca546e55   Samuel Ortiz   NFC: llcp: Fix th...
529
  	wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
530
  	gb_len += wks_length;
52feb444a   Thierry Escande   NFC: Extend netli...
531
  	miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
56d5876a2   Samuel Ortiz   NFC: Add MIUX to ...
532
533
  				      &miux_length);
  	gb_len += miux_length;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
534
535
536
  	gb_len += ARRAY_SIZE(llcp_magic);
  
  	if (gb_len > NFC_MAX_GT_LEN) {
52da2449e   Wei Yongjun   NFC: Fix possible...
537
538
  		ret = -EINVAL;
  		goto out;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  	}
  
  	gb_cur = local->gb;
  
  	memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
  	gb_cur += ARRAY_SIZE(llcp_magic);
  
  	memcpy(gb_cur, version_tlv, version_length);
  	gb_cur += version_length;
  
  	memcpy(gb_cur, lto_tlv, lto_length);
  	gb_cur += lto_length;
  
  	memcpy(gb_cur, wks_tlv, wks_length);
  	gb_cur += wks_length;
56d5876a2   Samuel Ortiz   NFC: Add MIUX to ...
554
555
  	memcpy(gb_cur, miux_tlv, miux_length);
  	gb_cur += miux_length;
52da2449e   Wei Yongjun   NFC: Fix possible...
556
557
558
  	local->gb_len = gb_len;
  
  out:
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
559
560
  	kfree(version_tlv);
  	kfree(lto_tlv);
52da2449e   Wei Yongjun   NFC: Fix possible...
561
562
  	kfree(wks_tlv);
  	kfree(miux_tlv);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
563

52da2449e   Wei Yongjun   NFC: Fix possible...
564
  	return ret;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
565
  }
b8e7a06d9   Samuel Ortiz   NFC: Build LLCP g...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
  {
  	struct nfc_llcp_local *local;
  
  	local = nfc_llcp_find_local(dev);
  	if (local == NULL) {
  		*general_bytes_len = 0;
  		return NULL;
  	}
  
  	nfc_llcp_build_gb(local);
  
  	*general_bytes_len = local->gb_len;
  
  	return local->gb;
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
582
583
  int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  {
3143a4ca6   Axel Lin   NFC: Move checkin...
584
585
586
587
  	struct nfc_llcp_local *local;
  
  	if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
  		return -EINVAL;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
588

3143a4ca6   Axel Lin   NFC: Move checkin...
589
  	local = nfc_llcp_find_local(dev);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
590
591
592
593
594
595
596
597
598
  	if (local == NULL) {
  		pr_err("No LLCP device
  ");
  		return -ENODEV;
  	}
  
  	memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
  	memcpy(local->remote_gb, gb, gb_len);
  	local->remote_gb_len = gb_len;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
599
600
601
602
603
  	if (memcmp(local->remote_gb, llcp_magic, 3)) {
  		pr_err("MAC does not support LLCP
  ");
  		return -EINVAL;
  	}
7a06e586b   Samuel Ortiz   NFC: Move LLCP re...
604
605
606
  	return nfc_llcp_parse_gb_tlv(local,
  				     &local->remote_gb[3],
  				     local->remote_gb_len - 3);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
607
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  static u8 nfc_llcp_dsap(struct sk_buff *pdu)
  {
  	return (pdu->data[0] & 0xfc) >> 2;
  }
  
  static u8 nfc_llcp_ptype(struct sk_buff *pdu)
  {
  	return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
  }
  
  static u8 nfc_llcp_ssap(struct sk_buff *pdu)
  {
  	return pdu->data[1] & 0x3f;
  }
  
  static u8 nfc_llcp_ns(struct sk_buff *pdu)
  {
  	return pdu->data[2] >> 4;
  }
  
  static u8 nfc_llcp_nr(struct sk_buff *pdu)
  {
  	return pdu->data[2] & 0xf;
  }
  
  static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
  {
279cf174a   Samuel Ortiz   NFC: No need to a...
635
  	pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
636
637
638
  	sock->send_n = (sock->send_n + 1) % 16;
  	sock->recv_ack_n = (sock->recv_n - 1) % 16;
  }
4463523be   Thierry Escande   NFC: LLCP raw soc...
639
640
641
  void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
  			       struct sk_buff *skb, u8 direction)
  {
4463523be   Thierry Escande   NFC: LLCP raw soc...
642
643
644
645
646
  	struct sk_buff *skb_copy = NULL, *nskb;
  	struct sock *sk;
  	u8 *data;
  
  	read_lock(&local->raw_sockets.lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
647
  	sk_for_each(sk, &local->raw_sockets.head) {
4463523be   Thierry Escande   NFC: LLCP raw soc...
648
649
650
651
  		if (sk->sk_state != LLCP_BOUND)
  			continue;
  
  		if (skb_copy == NULL) {
bad93e9d4   Octavian Purdila   net: add __pskb_c...
652
653
  			skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
  						      GFP_ATOMIC, true);
4463523be   Thierry Escande   NFC: LLCP raw soc...
654
655
656
  
  			if (skb_copy == NULL)
  				continue;
57be1f3f3   Hiren Tandel   NFC: Add RAW sock...
657
  			data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
4463523be   Thierry Escande   NFC: LLCP raw soc...
658
659
  
  			data[0] = local->dev ? local->dev->idx : 0xFF;
57be1f3f3   Hiren Tandel   NFC: Add RAW sock...
660
661
  			data[1] = direction & 0x01;
  			data[1] |= (RAW_PAYLOAD_LLCP << 1);
4463523be   Thierry Escande   NFC: LLCP raw soc...
662
663
664
665
666
667
668
669
670
671
672
673
674
675
  		}
  
  		nskb = skb_clone(skb_copy, GFP_ATOMIC);
  		if (!nskb)
  			continue;
  
  		if (sock_queue_rcv_skb(sk, nskb))
  			kfree_skb(nskb);
  	}
  
  	read_unlock(&local->raw_sockets.lock);
  
  	kfree_skb(skb_copy);
  }
844579603   Samuel Ortiz   NFC: Requeue lost...
676
677
678
679
680
681
682
683
684
685
686
687
  static void nfc_llcp_tx_work(struct work_struct *work)
  {
  	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  						    tx_work);
  	struct sk_buff *skb;
  	struct sock *sk;
  	struct nfc_llcp_sock *llcp_sock;
  
  	skb = skb_dequeue(&local->tx_queue);
  	if (skb != NULL) {
  		sk = skb->sk;
  		llcp_sock = nfc_llcp_sock(sk);
a6a0915f8   Samuel Ortiz   NFC: Avoid fallin...
688
689
  
  		if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
f1b79dc89   Thierry Escande   NFC: Fix a potent...
690
  			kfree_skb(skb);
a6a0915f8   Samuel Ortiz   NFC: Avoid fallin...
691
  			nfc_llcp_send_symm(local->dev);
2635a4bdf   Samuel Ortiz   NFC: llcp: Do not...
692
693
694
  		} else if (llcp_sock && !llcp_sock->remote_ready) {
  			skb_queue_head(&local->tx_queue, skb);
  			nfc_llcp_send_symm(local->dev);
a6a0915f8   Samuel Ortiz   NFC: Avoid fallin...
695
  		} else {
be02b6b62   Samuel Ortiz   NFC: Queue a copy...
696
697
  			struct sk_buff *copy_skb = NULL;
  			u8 ptype = nfc_llcp_ptype(skb);
844579603   Samuel Ortiz   NFC: Requeue lost...
698
699
700
701
  			int ret;
  
  			pr_debug("Sending pending skb
  ");
806bfe31c   Thierry Escande   NFC: llcp: Use dy...
702
703
  			print_hex_dump_debug("LLCP Tx: ", DUMP_PREFIX_OFFSET,
  					     16, 1, skb->data, skb->len, true);
844579603   Samuel Ortiz   NFC: Requeue lost...
704

17f7ae16a   Thierry Escande   NFC: Keep socket ...
705
706
707
708
709
710
  			if (ptype == LLCP_PDU_DISC && sk != NULL &&
  			    sk->sk_state == LLCP_DISCONNECTING) {
  				nfc_llcp_sock_unlink(&local->sockets, sk);
  				sock_orphan(sk);
  				sock_put(sk);
  			}
be02b6b62   Samuel Ortiz   NFC: Queue a copy...
711
712
  			if (ptype == LLCP_PDU_I)
  				copy_skb = skb_copy(skb, GFP_ATOMIC);
2c2d45bdc   Thierry Escande   NFC: Add support ...
713
  			__net_timestamp(skb);
4463523be   Thierry Escande   NFC: LLCP raw soc...
714
  			nfc_llcp_send_to_raw_sock(local, skb,
57be1f3f3   Hiren Tandel   NFC: Add RAW sock...
715
  						  NFC_DIRECTION_TX);
4463523be   Thierry Escande   NFC: LLCP raw soc...
716

844579603   Samuel Ortiz   NFC: Requeue lost...
717
718
  			ret = nfc_data_exchange(local->dev, local->target_idx,
  						skb, nfc_llcp_recv, local);
be02b6b62   Samuel Ortiz   NFC: Queue a copy...
719
720
721
  			if (ret) {
  				kfree_skb(copy_skb);
  				goto out;
844579603   Samuel Ortiz   NFC: Requeue lost...
722
  			}
be02b6b62   Samuel Ortiz   NFC: Queue a copy...
723
724
725
726
  
  			if (ptype == LLCP_PDU_I && copy_skb)
  				skb_queue_tail(&llcp_sock->tx_pending_queue,
  					       copy_skb);
844579603   Samuel Ortiz   NFC: Requeue lost...
727
728
729
730
  		}
  	} else {
  		nfc_llcp_send_symm(local->dev);
  	}
be02b6b62   Samuel Ortiz   NFC: Queue a copy...
731
  out:
844579603   Samuel Ortiz   NFC: Requeue lost...
732
733
734
  	mod_timer(&local->link_timer,
  		  jiffies + msecs_to_jiffies(2 * local->remote_lto));
  }
a69f32af8   Samuel Ortiz   NFC: Socket linke...
735
736
737
738
739
  static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
  							  u8 ssap)
  {
  	struct sock *sk;
  	struct nfc_llcp_sock *llcp_sock;
a69f32af8   Samuel Ortiz   NFC: Socket linke...
740
741
  
  	read_lock(&local->connecting_sockets.lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
742
  	sk_for_each(sk, &local->connecting_sockets.head) {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
743
  		llcp_sock = nfc_llcp_sock(sk);
5a0f6f3b4   Samuel Ortiz   NFC: Don't hold a...
744
745
  		if (llcp_sock->ssap == ssap) {
  			sock_hold(&llcp_sock->sk);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
746
  			goto out;
5a0f6f3b4   Samuel Ortiz   NFC: Don't hold a...
747
  		}
a69f32af8   Samuel Ortiz   NFC: Socket linke...
748
749
750
751
752
753
  	}
  
  	llcp_sock = NULL;
  
  out:
  	read_unlock(&local->connecting_sockets.lock);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
754
755
  	return llcp_sock;
  }
a69f32af8   Samuel Ortiz   NFC: Socket linke...
756
757
758
  static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
  						  u8 *sn, size_t sn_len)
  {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
759
  	struct nfc_llcp_sock *llcp_sock;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
760
  	llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
761

a69f32af8   Samuel Ortiz   NFC: Socket linke...
762
763
  	if (llcp_sock == NULL)
  		return NULL;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
764

a69f32af8   Samuel Ortiz   NFC: Socket linke...
765
766
767
  	sock_hold(&llcp_sock->sk);
  
  	return llcp_sock;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
768
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
  {
  	u8 *tlv = &skb->data[2], type, length;
  	size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
  
  	while (offset < tlv_array_len) {
  		type = tlv[0];
  		length = tlv[1];
  
  		pr_debug("type 0x%x length %d
  ", type, length);
  
  		if (type == LLCP_TLV_SN) {
  			*sn_len = length;
  			return &tlv[2];
  		}
  
  		offset += length + 2;
  		tlv += length + 2;
  	}
  
  	return NULL;
  }
968272bf0   Samuel Ortiz   NFC: Handle LLCP ...
792
793
794
795
796
797
798
799
800
801
802
803
804
  static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
  			     struct sk_buff *skb)
  {
  	struct nfc_llcp_sock *llcp_sock;
  	struct nfc_llcp_ui_cb *ui_cb;
  	u8 dsap, ssap;
  
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  
  	ui_cb = nfc_llcp_ui_skb_cb(skb);
  	ui_cb->dsap = dsap;
  	ui_cb->ssap = ssap;
968272bf0   Samuel Ortiz   NFC: Handle LLCP ...
805
806
807
808
809
810
811
812
813
814
  	pr_debug("%d %d
  ", dsap, ssap);
  
  	/* We're looking for a bound socket, not a client one */
  	llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  	if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
  		return;
  
  	/* There is no sequence with UI frames */
  	skb_pull(skb, LLCP_HEADER_SIZE);
1727cf937   Samuel Ortiz   NFC: llcp: Fix Rx...
815
816
817
818
819
820
821
822
823
  	if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  		/*
  		 * UI frames will be freed from the socket layer, so we
  		 * need to keep them alive until someone receives them.
  		 */
  		skb_get(skb);
  	} else {
  		pr_err("Receive queue is full
  ");
968272bf0   Samuel Ortiz   NFC: Handle LLCP ...
824
825
826
827
  	}
  
  	nfc_llcp_sock_put(llcp_sock);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
828
  static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
829
  				  struct sk_buff *skb)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
830
831
832
  {
  	struct sock *new_sk, *parent;
  	struct nfc_llcp_sock *sock, *new_sock;
a69f32af8   Samuel Ortiz   NFC: Socket linke...
833
  	u8 dsap, ssap, reason;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
834
835
836
837
838
839
  
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  
  	pr_debug("%d %d
  ", dsap, ssap);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
840
  	if (dsap != LLCP_SAP_SDP) {
a69f32af8   Samuel Ortiz   NFC: Socket linke...
841
842
  		sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  		if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
843
844
845
  			reason = LLCP_DM_NOBOUND;
  			goto fail;
  		}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
846
847
848
849
850
851
852
853
854
855
856
857
  	} else {
  		u8 *sn;
  		size_t sn_len;
  
  		sn = nfc_llcp_connect_sn(skb, &sn_len);
  		if (sn == NULL) {
  			reason = LLCP_DM_NOBOUND;
  			goto fail;
  		}
  
  		pr_debug("Service name length %zu
  ", sn_len);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
858
859
860
861
  		sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
  		if (sock == NULL) {
  			reason = LLCP_DM_NOBOUND;
  			goto fail;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
862
  		}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
863
  	}
a69f32af8   Samuel Ortiz   NFC: Socket linke...
864
  	lock_sock(&sock->sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
865

d646960f7   Samuel Ortiz   NFC: Initial LLCP...
866
867
868
869
870
871
872
873
  	parent = &sock->sk;
  
  	if (sk_acceptq_is_full(parent)) {
  		reason = LLCP_DM_REJ;
  		release_sock(&sock->sk);
  		sock_put(&sock->sk);
  		goto fail;
  	}
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
  	if (sock->ssap == LLCP_SDP_UNBOUND) {
  		u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
  
  		pr_debug("First client, reserving %d
  ", ssap);
  
  		if (ssap == LLCP_SAP_MAX) {
  			reason = LLCP_DM_REJ;
  			release_sock(&sock->sk);
  			sock_put(&sock->sk);
  			goto fail;
  		}
  
  		sock->ssap = ssap;
  	}
11aa9c28b   Eric W. Biederman   net: Pass kern fr...
889
  	new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
890
891
892
893
894
895
896
897
898
  	if (new_sk == NULL) {
  		reason = LLCP_DM_REJ;
  		release_sock(&sock->sk);
  		sock_put(&sock->sk);
  		goto fail;
  	}
  
  	new_sock = nfc_llcp_sock(new_sk);
  	new_sock->dev = local->dev;
c7aa12252   Samuel Ortiz   NFC: Take a refer...
899
  	new_sock->local = nfc_llcp_local_get(local);
06d44f806   Samuel Ortiz   NFC: llcp: Use so...
900
901
  	new_sock->rw = sock->rw;
  	new_sock->miux = sock->miux;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
902
  	new_sock->nfc_protocol = sock->nfc_protocol;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
903
  	new_sock->dsap = ssap;
025f15204   Samuel Ortiz   NFC: Update LLCP ...
904
  	new_sock->target_idx = local->target_idx;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
905
  	new_sock->parent = parent;
8f50020ed   Samuel Ortiz   NFC: LLCP late bi...
906
907
908
909
910
911
912
913
914
915
916
917
918
  	new_sock->ssap = sock->ssap;
  	if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
  		atomic_t *client_count;
  
  		pr_debug("reserved_ssap %d for %p
  ", sock->ssap, new_sock);
  
  		client_count =
  			&local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
  
  		atomic_inc(client_count);
  		new_sock->reserved_ssap = sock->ssap;
  	}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
919

7a06e586b   Samuel Ortiz   NFC: Move LLCP re...
920
921
  	nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
  				      skb->len - LLCP_HEADER_SIZE);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
922
923
  	pr_debug("new sock %p sk %p
  ", new_sock, &new_sock->sk);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
924
  	nfc_llcp_sock_link(&local->sockets, new_sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
925
926
927
928
929
930
931
932
  
  	nfc_llcp_accept_enqueue(&sock->sk, new_sk);
  
  	nfc_get_device(local->dev->idx);
  
  	new_sk->sk_state = LLCP_CONNECTED;
  
  	/* Wake the listening processes */
676d23690   David S. Miller   net: Fix use afte...
933
  	parent->sk_data_ready(parent);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
934
935
936
937
938
939
940
941
942
943
944
945
  
  	/* Send CC */
  	nfc_llcp_send_cc(new_sock);
  
  	release_sock(&sock->sk);
  	sock_put(&sock->sk);
  
  	return;
  
  fail:
  	/* Send DM */
  	nfc_llcp_send_dm(local, dsap, ssap, reason);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
946
  }
d094afa15   Samuel Ortiz   NFC: Send LLCP RR...
947
  int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
4722d2b70   Samuel Ortiz   NFC: Factorize th...
948
  {
d094afa15   Samuel Ortiz   NFC: Send LLCP RR...
949
  	int nr_frames = 0;
4722d2b70   Samuel Ortiz   NFC: Factorize th...
950
951
952
  	struct nfc_llcp_local *local = sock->local;
  
  	pr_debug("Remote ready %d tx queue len %d remote rw %d",
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
953
  		 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
e4306bec4   Samuel Ortiz   NFC: llcp: Rename...
954
  		 sock->remote_rw);
4722d2b70   Samuel Ortiz   NFC: Factorize th...
955
956
957
  
  	/* Try to queue some I frames for transmission */
  	while (sock->remote_ready &&
e4306bec4   Samuel Ortiz   NFC: llcp: Rename...
958
  	       skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
844579603   Samuel Ortiz   NFC: Requeue lost...
959
  		struct sk_buff *pdu;
4722d2b70   Samuel Ortiz   NFC: Factorize th...
960
961
962
963
964
965
966
  
  		pdu = skb_dequeue(&sock->tx_queue);
  		if (pdu == NULL)
  			break;
  
  		/* Update N(S)/N(R) */
  		nfc_llcp_set_nrns(sock, pdu);
4722d2b70   Samuel Ortiz   NFC: Factorize th...
967
  		skb_queue_tail(&local->tx_queue, pdu);
d094afa15   Samuel Ortiz   NFC: Send LLCP RR...
968
  		nr_frames++;
4722d2b70   Samuel Ortiz   NFC: Factorize th...
969
  	}
d094afa15   Samuel Ortiz   NFC: Send LLCP RR...
970
971
  
  	return nr_frames;
4722d2b70   Samuel Ortiz   NFC: Factorize th...
972
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
973
  static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
974
  			       struct sk_buff *skb)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  {
  	struct nfc_llcp_sock *llcp_sock;
  	struct sock *sk;
  	u8 dsap, ssap, ptype, ns, nr;
  
  	ptype = nfc_llcp_ptype(skb);
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  	ns = nfc_llcp_ns(skb);
  	nr = nfc_llcp_nr(skb);
  
  	pr_debug("%d %d R %d S %d
  ", dsap, ssap, nr, ns);
  
  	llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  	if (llcp_sock == NULL) {
  		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  		return;
  	}
  
  	sk = &llcp_sock->sk;
  	lock_sock(sk);
  	if (sk->sk_state == LLCP_CLOSED) {
  		release_sock(sk);
  		nfc_llcp_sock_put(llcp_sock);
  	}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1001
1002
1003
1004
  	/* Pass the payload upstream */
  	if (ptype == LLCP_PDU_I) {
  		pr_debug("I frame, queueing on %p
  ", &llcp_sock->sk);
53aef9205   Samuel Ortiz   NFC: Handle Recei...
1005
1006
1007
1008
1009
  		if (ns == llcp_sock->recv_n)
  			llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
  		else
  			pr_err("Received out of sequence I PDU
  ");
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1010
  		skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
1727cf937   Samuel Ortiz   NFC: llcp: Fix Rx...
1011
1012
1013
1014
1015
1016
1017
1018
1019
  		if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  			/*
  			 * I frames will be freed from the socket layer, so we
  			 * need to keep them alive until someone receives them.
  			 */
  			skb_get(skb);
  		} else {
  			pr_err("Receive queue is full
  ");
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1020
1021
1022
1023
1024
1025
  		}
  	}
  
  	/* Remove skbs from the pending queue */
  	if (llcp_sock->send_ack_n != nr) {
  		struct sk_buff *s, *tmp;
289814918   Waldemar Rymarkiewicz   NFC: Fix incorrec...
1026
  		u8 n;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1027
1028
  
  		llcp_sock->send_ack_n = nr;
844579603   Samuel Ortiz   NFC: Requeue lost...
1029
1030
  		/* Remove and free all skbs until ns == nr */
  		skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
289814918   Waldemar Rymarkiewicz   NFC: Fix incorrec...
1031
  			n = nfc_llcp_ns(s);
844579603   Samuel Ortiz   NFC: Requeue lost...
1032
1033
  			skb_unlink(s, &llcp_sock->tx_pending_queue);
  			kfree_skb(s);
289814918   Waldemar Rymarkiewicz   NFC: Fix incorrec...
1034
  			if (n == nr)
844579603   Samuel Ortiz   NFC: Requeue lost...
1035
1036
1037
1038
1039
1040
1041
1042
1043
  				break;
  		}
  
  		/* Re-queue the remaining skbs for transmission */
  		skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
  					    s, tmp) {
  			skb_unlink(s, &llcp_sock->tx_pending_queue);
  			skb_queue_head(&local->tx_queue, s);
  		}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1044
  	}
53aef9205   Samuel Ortiz   NFC: Handle Recei...
1045
1046
  	if (ptype == LLCP_PDU_RR)
  		llcp_sock->remote_ready = true;
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
1047
  	else if (ptype == LLCP_PDU_RNR)
53aef9205   Samuel Ortiz   NFC: Handle Recei...
1048
  		llcp_sock->remote_ready = false;
56af2568c   Samuel Ortiz   NFC: Send a recei...
1049
  	if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
d094afa15   Samuel Ortiz   NFC: Send LLCP RR...
1050
  		nfc_llcp_send_rr(llcp_sock);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1051
1052
1053
1054
1055
1056
  
  	release_sock(sk);
  	nfc_llcp_sock_put(llcp_sock);
  }
  
  static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
1057
  			       struct sk_buff *skb)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1058
1059
1060
1061
1062
1063
1064
  {
  	struct nfc_llcp_sock *llcp_sock;
  	struct sock *sk;
  	u8 dsap, ssap;
  
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
6d2cd978e   Samuel Ortiz   NFC: llcp: Termin...
1065
1066
1067
1068
1069
  	if ((dsap == 0) && (ssap == 0)) {
  		pr_debug("Connection termination");
  		nfc_dep_link_down(local->dev);
  		return;
  	}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1070
1071
1072
1073
1074
1075
1076
1077
  	llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  	if (llcp_sock == NULL) {
  		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  		return;
  	}
  
  	sk = &llcp_sock->sk;
  	lock_sock(sk);
f31652a58   Samuel Ortiz   NFC: Purge LLCP s...
1078
1079
  
  	nfc_llcp_socket_purge(llcp_sock);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1080
1081
1082
1083
  	if (sk->sk_state == LLCP_CLOSED) {
  		release_sock(sk);
  		nfc_llcp_sock_put(llcp_sock);
  	}
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
  	if (sk->sk_state == LLCP_CONNECTED) {
  		nfc_put_device(local->dev);
  		sk->sk_state = LLCP_CLOSED;
  		sk->sk_state_change(sk);
  	}
  
  	nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
  
  	release_sock(sk);
  	nfc_llcp_sock_put(llcp_sock);
  }
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
1095
  static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1096
1097
  {
  	struct nfc_llcp_sock *llcp_sock;
ff353d86a   Samuel Ortiz   NFC: LLCP connect...
1098
  	struct sock *sk;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1099
  	u8 dsap, ssap;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1100
1101
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
1102
  	llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1103
1104
1105
1106
1107
1108
1109
  	if (llcp_sock == NULL) {
  		pr_err("Invalid CC
  ");
  		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  
  		return;
  	}
ff353d86a   Samuel Ortiz   NFC: LLCP connect...
1110
  	sk = &llcp_sock->sk;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1111

a69f32af8   Samuel Ortiz   NFC: Socket linke...
1112
1113
1114
1115
  	/* Unlink from connecting and link to the client array */
  	nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
  	nfc_llcp_sock_link(&local->sockets, sk);
  	llcp_sock->dsap = ssap;
7a06e586b   Samuel Ortiz   NFC: Move LLCP re...
1116
1117
  	nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
  				      skb->len - LLCP_HEADER_SIZE);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1118

ff353d86a   Samuel Ortiz   NFC: LLCP connect...
1119
1120
  	sk->sk_state = LLCP_CONNECTED;
  	sk->sk_state_change(sk);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1121
1122
  	nfc_llcp_sock_put(llcp_sock);
  }
5c0560b7a   Samuel Ortiz   NFC: Handle LLCP ...
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
  {
  	struct nfc_llcp_sock *llcp_sock;
  	struct sock *sk;
  	u8 dsap, ssap, reason;
  
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  	reason = skb->data[2];
  
  	pr_debug("%d %d reason %d
  ", ssap, dsap, reason);
  
  	switch (reason) {
  	case LLCP_DM_NOBOUND:
  	case LLCP_DM_REJ:
  		llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
  		break;
  
  	default:
  		llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  		break;
  	}
  
  	if (llcp_sock == NULL) {
a8df0f379   Samuel Ortiz   NFC: Return NULL ...
1148
1149
  		pr_debug("Already closed
  ");
5c0560b7a   Samuel Ortiz   NFC: Handle LLCP ...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
  		return;
  	}
  
  	sk = &llcp_sock->sk;
  
  	sk->sk_err = ENXIO;
  	sk->sk_state = LLCP_CLOSED;
  	sk->sk_state_change(sk);
  
  	nfc_llcp_sock_put(llcp_sock);
5c0560b7a   Samuel Ortiz   NFC: Handle LLCP ...
1160
  }
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1161
1162
1163
1164
1165
1166
1167
1168
  static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
  			      struct sk_buff *skb)
  {
  	struct nfc_llcp_sock *llcp_sock;
  	u8 dsap, ssap, *tlv, type, length, tid, sap;
  	u16 tlv_len, offset;
  	char *service_name;
  	size_t service_name_len;
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1169
1170
1171
  	struct nfc_llcp_sdp_tlv *sdp;
  	HLIST_HEAD(llc_sdres_list);
  	size_t sdres_tlvs_len;
d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
1172
  	HLIST_HEAD(nl_sdres_list);
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
  
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  
  	pr_debug("%d %d
  ", dsap, ssap);
  
  	if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
  		pr_err("Wrong SNL SAP
  ");
  		return;
  	}
  
  	tlv = &skb->data[LLCP_HEADER_SIZE];
  	tlv_len = skb->len - LLCP_HEADER_SIZE;
  	offset = 0;
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1189
  	sdres_tlvs_len = 0;
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1190

0f4507722   Szymon Janc   NFC: Fix some cod...
1191
  	while (offset < tlv_len) {
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1192
1193
1194
1195
1196
1197
1198
1199
  		type = tlv[0];
  		length = tlv[1];
  
  		switch (type) {
  		case LLCP_TLV_SDREQ:
  			tid = tlv[2];
  			service_name = (char *) &tlv[3];
  			service_name_len = length - 1;
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1200
1201
  			pr_debug("Looking for %.16s
  ", service_name);
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1202
1203
1204
1205
1206
  
  			if (service_name_len == strlen("urn:nfc:sn:sdp") &&
  			    !strncmp(service_name, "urn:nfc:sn:sdp",
  				     service_name_len)) {
  				sap = 1;
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1207
  				goto add_snl;
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1208
1209
1210
1211
1212
1213
  			}
  
  			llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
  							  service_name_len);
  			if (!llcp_sock) {
  				sap = 0;
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1214
  				goto add_snl;
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
  			}
  
  			/*
  			 * We found a socket but its ssap has not been reserved
  			 * yet. We need to assign it for good and send a reply.
  			 * The ssap will be freed when the socket is closed.
  			 */
  			if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
  				atomic_t *client_count;
  
  				sap = nfc_llcp_reserve_sdp_ssap(local);
  
  				pr_debug("Reserving %d
  ", sap);
  
  				if (sap == LLCP_SAP_MAX) {
  					sap = 0;
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1232
  					goto add_snl;
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
  				}
  
  				client_count =
  					&local->local_sdp_cnt[sap -
  							      LLCP_WKS_NUM_SAP];
  
  				atomic_inc(client_count);
  
  				llcp_sock->ssap = sap;
  				llcp_sock->reserved_ssap = sap;
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1243
  			} else {
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1244
  				sap = llcp_sock->ssap;
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1245
  			}
e6904081d   Samuel Ortiz   NFC: Reserve LLCP...
1246
1247
  			pr_debug("%p %d
  ", llcp_sock, sap);
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1248
1249
1250
1251
1252
1253
1254
  add_snl:
  			sdp = nfc_llcp_build_sdres_tlv(tid, sap);
  			if (sdp == NULL)
  				goto exit;
  
  			sdres_tlvs_len += sdp->tlv_len;
  			hlist_add_head(&sdp->node, &llc_sdres_list);
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1255
  			break;
d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
  		case LLCP_TLV_SDRES:
  			mutex_lock(&local->sdreq_lock);
  
  			pr_debug("LLCP_TLV_SDRES: searching tid %d
  ", tlv[2]);
  
  			hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
  				if (sdp->tid != tlv[2])
  					continue;
  
  				sdp->sap = tlv[3];
  
  				pr_debug("Found: uri=%s, sap=%d
  ",
  					 sdp->uri, sdp->sap);
  
  				hlist_del(&sdp->node);
  
  				hlist_add_head(&sdp->node, &nl_sdres_list);
  
  				break;
  			}
  
  			mutex_unlock(&local->sdreq_lock);
  			break;
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1281
1282
1283
1284
1285
1286
1287
1288
1289
  		default:
  			pr_err("Invalid SNL tlv value 0x%x
  ", type);
  			break;
  		}
  
  		offset += length + 2;
  		tlv += length + 2;
  	}
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1290
1291
  
  exit:
d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
1292
1293
  	if (!hlist_empty(&nl_sdres_list))
  		nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
e0ae7bac0   Thierry Escande   NFC: llcp: Servic...
1294
1295
  	if (!hlist_empty(&llc_sdres_list))
  		nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1296
  }
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1297
  static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1298
  {
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1299
1300
1301
  	u8 ptype;
  	u16 pdu_len;
  	struct sk_buff *new_skb;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1302

098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1303
1304
1305
  	if (skb->len <= LLCP_HEADER_SIZE) {
  		pr_err("Malformed AGF PDU
  ");
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1306
1307
  		return;
  	}
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
  	skb_pull(skb, LLCP_HEADER_SIZE);
  
  	while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
  		pdu_len = skb->data[0] << 8 | skb->data[1];
  
  		skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
  
  		if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
  			pr_err("Malformed AGF PDU
  ");
  			return;
  		}
  
  		ptype = nfc_llcp_ptype(skb);
  
  		if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
  			goto next;
  
  		new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
  		if (new_skb == NULL) {
  			pr_err("Could not allocate PDU
  ");
  			return;
  		}
59ae1d127   Johannes Berg   networking: intro...
1332
  		skb_put_data(new_skb, skb->data, pdu_len);
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
  
  		nfc_llcp_rx_skb(local, new_skb);
  
  		kfree_skb(new_skb);
  next:
  		skb_pull(skb, pdu_len);
  	}
  }
  
  static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
  {
  	u8 dsap, ssap, ptype;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1345
1346
1347
1348
1349
1350
  	ptype = nfc_llcp_ptype(skb);
  	dsap = nfc_llcp_dsap(skb);
  	ssap = nfc_llcp_ssap(skb);
  
  	pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x
  ", ptype, dsap, ssap);
4be646ecc   Samuel Ortiz   NFC: Dump LLCP fr...
1351
  	if (ptype != LLCP_PDU_SYMM)
806bfe31c   Thierry Escande   NFC: llcp: Use dy...
1352
1353
  		print_hex_dump_debug("LLCP Rx: ", DUMP_PREFIX_OFFSET, 16, 1,
  				     skb->data, skb->len, true);
4be646ecc   Samuel Ortiz   NFC: Dump LLCP fr...
1354

d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1355
1356
1357
1358
1359
  	switch (ptype) {
  	case LLCP_PDU_SYMM:
  		pr_debug("SYMM
  ");
  		break;
968272bf0   Samuel Ortiz   NFC: Handle LLCP ...
1360
1361
1362
1363
1364
  	case LLCP_PDU_UI:
  		pr_debug("UI
  ");
  		nfc_llcp_recv_ui(local, skb);
  		break;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
  	case LLCP_PDU_CONNECT:
  		pr_debug("CONNECT
  ");
  		nfc_llcp_recv_connect(local, skb);
  		break;
  
  	case LLCP_PDU_DISC:
  		pr_debug("DISC
  ");
  		nfc_llcp_recv_disc(local, skb);
  		break;
  
  	case LLCP_PDU_CC:
  		pr_debug("CC
  ");
  		nfc_llcp_recv_cc(local, skb);
  		break;
5c0560b7a   Samuel Ortiz   NFC: Handle LLCP ...
1382
1383
1384
1385
1386
  	case LLCP_PDU_DM:
  		pr_debug("DM
  ");
  		nfc_llcp_recv_dm(local, skb);
  		break;
19cfe5843   Samuel Ortiz   NFC: Initial SNL ...
1387
1388
1389
1390
1391
  	case LLCP_PDU_SNL:
  		pr_debug("SNL
  ");
  		nfc_llcp_recv_snl(local, skb);
  		break;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1392
1393
  	case LLCP_PDU_I:
  	case LLCP_PDU_RR:
53aef9205   Samuel Ortiz   NFC: Handle Recei...
1394
  	case LLCP_PDU_RNR:
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1395
1396
1397
1398
  		pr_debug("I frame
  ");
  		nfc_llcp_recv_hdlc(local, skb);
  		break;
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1399
1400
1401
1402
1403
  	case LLCP_PDU_AGF:
  		pr_debug("AGF frame
  ");
  		nfc_llcp_recv_agf(local, skb);
  		break;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1404
  	}
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
  }
  
  static void nfc_llcp_rx_work(struct work_struct *work)
  {
  	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  						    rx_work);
  	struct sk_buff *skb;
  
  	skb = local->rx_pending;
  	if (skb == NULL) {
  		pr_debug("No pending SKB
  ");
  		return;
  	}
  
  	__net_timestamp(skb);
57be1f3f3   Hiren Tandel   NFC: Add RAW sock...
1421
  	nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
098dafcfb   Thierry Escande   NFC: llcp: Aggreg...
1422
1423
  
  	nfc_llcp_rx_skb(local, skb);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1424

916082b07   Linus Torvalds   workqueue: avoid ...
1425
  	schedule_work(&local->tx_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1426
1427
  	kfree_skb(local->rx_pending);
  	local->rx_pending = NULL;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1428
  }
1727cf937   Samuel Ortiz   NFC: llcp: Fix Rx...
1429
1430
1431
1432
1433
1434
  static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
  {
  	local->rx_pending = skb;
  	del_timer(&local->link_timer);
  	schedule_work(&local->rx_work);
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1435
1436
1437
1438
1439
1440
1441
  void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
  {
  	struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  
  	pr_debug("Received an LLCP PDU
  ");
  	if (err < 0) {
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
1442
1443
  		pr_err("err %d
  ", err);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1444
1445
  		return;
  	}
1727cf937   Samuel Ortiz   NFC: llcp: Fix Rx...
1446
  	__nfc_llcp_recv(local, skb);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1447
  }
73167ced3   Samuel Ortiz   NFC: Introduce ta...
1448
1449
1450
1451
1452
  int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  {
  	struct nfc_llcp_local *local;
  
  	local = nfc_llcp_find_local(dev);
966efbfb0   Julien Lefrique   NFC: Fix a memory...
1453
1454
  	if (local == NULL) {
  		kfree_skb(skb);
73167ced3   Samuel Ortiz   NFC: Introduce ta...
1455
  		return -ENODEV;
966efbfb0   Julien Lefrique   NFC: Fix a memory...
1456
  	}
73167ced3   Samuel Ortiz   NFC: Introduce ta...
1457

1727cf937   Samuel Ortiz   NFC: llcp: Fix Rx...
1458
  	__nfc_llcp_recv(local, skb);
73167ced3   Samuel Ortiz   NFC: Introduce ta...
1459
1460
1461
  
  	return 0;
  }
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1462
1463
1464
1465
1466
1467
1468
  void nfc_llcp_mac_is_down(struct nfc_dev *dev)
  {
  	struct nfc_llcp_local *local;
  
  	local = nfc_llcp_find_local(dev);
  	if (local == NULL)
  		return;
abd18d433   Thierry Escande   NFC: llcp: Reset ...
1469
1470
  	local->remote_miu = LLCP_DEFAULT_MIU;
  	local->remote_lto = LLCP_DEFAULT_LTO;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1471
  	/* Close and purge all existing sockets */
3bbc0ceb7   Samuel Ortiz   NFC: llcp: Report...
1472
  	nfc_llcp_socket_release(local, true, 0);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  }
  
  void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
  			u8 comm_mode, u8 rf_mode)
  {
  	struct nfc_llcp_local *local;
  
  	pr_debug("rf mode %d
  ", rf_mode);
  
  	local = nfc_llcp_find_local(dev);
  	if (local == NULL)
  		return;
  
  	local->target_idx = target_idx;
  	local->comm_mode = comm_mode;
  	local->rf_mode = rf_mode;
  
  	if (rf_mode == NFC_RF_INITIATOR) {
  		pr_debug("Queueing Tx work
  ");
916082b07   Linus Torvalds   workqueue: avoid ...
1494
  		schedule_work(&local->tx_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1495
1496
  	} else {
  		mod_timer(&local->link_timer,
427a2eb1f   Samuel Ortiz   NFC: LLCP code id...
1497
  			  jiffies + msecs_to_jiffies(local->remote_lto));
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1498
1499
1500
1501
1502
  	}
  }
  
  int nfc_llcp_register_device(struct nfc_dev *ndev)
  {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1503
  	struct nfc_llcp_local *local;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1504
1505
1506
1507
1508
1509
1510
  
  	local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
  	if (local == NULL)
  		return -ENOMEM;
  
  	local->dev = ndev;
  	INIT_LIST_HEAD(&local->list);
c7aa12252   Samuel Ortiz   NFC: Take a refer...
1511
  	kref_init(&local->ref);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1512
  	mutex_init(&local->sdp_lock);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1513
1514
1515
1516
1517
1518
  	init_timer(&local->link_timer);
  	local->link_timer.data = (unsigned long) local;
  	local->link_timer.function = nfc_llcp_symm_timer;
  
  	skb_queue_head_init(&local->tx_queue);
  	INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1519
1520
1521
  
  	local->rx_pending = NULL;
  	INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1522
1523
  
  	INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1524

fe235b58d   Szymon Janc   NFC: Use dynamic ...
1525
1526
  	rwlock_init(&local->sockets.lock);
  	rwlock_init(&local->connecting_sockets.lock);
4463523be   Thierry Escande   NFC: LLCP raw soc...
1527
  	rwlock_init(&local->raw_sockets.lock);
a69f32af8   Samuel Ortiz   NFC: Socket linke...
1528

52feb444a   Thierry Escande   NFC: Extend netli...
1529
1530
1531
  	local->lto = 150; /* 1500 ms */
  	local->rw = LLCP_MAX_RW;
  	local->miux = cpu_to_be16(LLCP_MAX_MIUX);
f768b3401   Samuel Ortiz   NFC: llcp: Set th...
1532
  	local->local_wks = 0x1; /* LLC Link Management */
52feb444a   Thierry Escande   NFC: Extend netli...
1533

d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1534
1535
1536
1537
  	nfc_llcp_build_gb(local);
  
  	local->remote_miu = LLCP_DEFAULT_MIU;
  	local->remote_lto = LLCP_DEFAULT_LTO;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1538

d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
1539
1540
  	mutex_init(&local->sdreq_lock);
  	INIT_HLIST_HEAD(&local->pending_sdreqs);
40213fa85   Thierry Escande   NFC: llcp: Add cl...
1541
1542
1543
1544
  	init_timer(&local->sdreq_timer);
  	local->sdreq_timer.data = (unsigned long) local;
  	local->sdreq_timer.function = nfc_llcp_sdreq_timer;
  	INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
d9b8d8e19   Thierry Escande   NFC: llcp: Servic...
1545

16a78e9fe   Thierry Escande   NFC: Fix nfc_llcp...
1546
  	list_add(&local->list, &llcp_devices);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1547
1548
  
  	return 0;
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
  }
  
  void nfc_llcp_unregister_device(struct nfc_dev *dev)
  {
  	struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
  
  	if (local == NULL) {
  		pr_debug("No such device
  ");
  		return;
  	}
c470e319b   Samuel Ortiz   NFC: llcp: Remove...
1560
  	local_cleanup(local);
3536da06d   Samuel Ortiz   NFC: llcp: Clean ...
1561

c7aa12252   Samuel Ortiz   NFC: Take a refer...
1562
  	nfc_llcp_local_put(local);
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1563
1564
1565
1566
  }
  
  int __init nfc_llcp_init(void)
  {
d646960f7   Samuel Ortiz   NFC: Initial LLCP...
1567
1568
1569
1570
1571
1572
1573
  	return nfc_llcp_sock_init();
  }
  
  void nfc_llcp_exit(void)
  {
  	nfc_llcp_sock_exit();
  }