xref: /openbmc/linux/net/can/isotp.c (revision c5c87812)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
3  *
4  * This implementation does not provide ISO-TP specific return values to the
5  * userspace.
6  *
7  * - RX path timeout of data reception leads to -ETIMEDOUT
8  * - RX path SN mismatch leads to -EILSEQ
9  * - RX path data reception with wrong padding leads to -EBADMSG
10  * - TX path flowcontrol reception timeout leads to -ECOMM
11  * - TX path flowcontrol reception overflow leads to -EMSGSIZE
12  * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG
13  * - when a transfer (tx) is on the run the next write() blocks until it's done
14  * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent
15  * - as we have static buffers the check whether the PDU fits into the buffer
16  *   is done at FF reception time (no support for sending 'wait frames')
17  * - take care of the tx-queue-len as traffic shaping is still on the TODO list
18  *
19  * Copyright (c) 2020 Volkswagen Group Electronic Research
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. Neither the name of Volkswagen nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * Alternatively, provided that this notice is retained in full, this
35  * software may be distributed under the terms of the GNU General
36  * Public License ("GPL") version 2, in which case the provisions of the
37  * GPL apply INSTEAD OF those given above.
38  *
39  * The provided data structures and external interfaces from this code
40  * are not restricted to be used by modules with a GPL compatible license.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
53  * DAMAGE.
54  */
55 
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/interrupt.h>
59 #include <linux/hrtimer.h>
60 #include <linux/wait.h>
61 #include <linux/uio.h>
62 #include <linux/net.h>
63 #include <linux/netdevice.h>
64 #include <linux/socket.h>
65 #include <linux/if_arp.h>
66 #include <linux/skbuff.h>
67 #include <linux/can.h>
68 #include <linux/can/core.h>
69 #include <linux/can/skb.h>
70 #include <linux/can/isotp.h>
71 #include <linux/slab.h>
72 #include <net/sock.h>
73 #include <net/net_namespace.h>
74 
75 MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
78 MODULE_ALIAS("can-proto-6");
79 
80 #define SINGLE_MASK(id) (((id) & CAN_EFF_FLAG) ? \
81 			 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
82 			 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
83 
84 /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
85  * take full 32 bit values (4 Gbyte). We would need some good concept to handle
86  * this between user space and kernel space. For now increase the static buffer
87  * to something about 8 kbyte to be able to test this new functionality.
88  */
89 #define MAX_MSG_LENGTH 8200
90 
91 /* N_PCI type values in bits 7-4 of N_PCI bytes */
92 #define N_PCI_SF 0x00	/* single frame */
93 #define N_PCI_FF 0x10	/* first frame */
94 #define N_PCI_CF 0x20	/* consecutive frame */
95 #define N_PCI_FC 0x30	/* flow control */
96 
97 #define N_PCI_SZ 1	/* size of the PCI byte #1 */
98 #define SF_PCI_SZ4 1	/* size of SingleFrame PCI including 4 bit SF_DL */
99 #define SF_PCI_SZ8 2	/* size of SingleFrame PCI including 8 bit SF_DL */
100 #define FF_PCI_SZ12 2	/* size of FirstFrame PCI including 12 bit FF_DL */
101 #define FF_PCI_SZ32 6	/* size of FirstFrame PCI including 32 bit FF_DL */
102 #define FC_CONTENT_SZ 3	/* flow control content size in byte (FS/BS/STmin) */
103 
104 #define ISOTP_CHECK_PADDING (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA)
105 
106 /* Flow Status given in FC frame */
107 #define ISOTP_FC_CTS 0		/* clear to send */
108 #define ISOTP_FC_WT 1		/* wait */
109 #define ISOTP_FC_OVFLW 2	/* overflow */
110 
111 enum {
112 	ISOTP_IDLE = 0,
113 	ISOTP_WAIT_FIRST_FC,
114 	ISOTP_WAIT_FC,
115 	ISOTP_WAIT_DATA,
116 	ISOTP_SENDING
117 };
118 
119 struct tpcon {
120 	int idx;
121 	int len;
122 	u8 state;
123 	u8 bs;
124 	u8 sn;
125 	u8 ll_dl;
126 	u8 buf[MAX_MSG_LENGTH + 1];
127 };
128 
129 struct isotp_sock {
130 	struct sock sk;
131 	int bound;
132 	int ifindex;
133 	canid_t txid;
134 	canid_t rxid;
135 	ktime_t tx_gap;
136 	ktime_t lastrxcf_tstamp;
137 	struct hrtimer rxtimer, txtimer;
138 	struct can_isotp_options opt;
139 	struct can_isotp_fc_options rxfc, txfc;
140 	struct can_isotp_ll_options ll;
141 	u32 force_tx_stmin;
142 	u32 force_rx_stmin;
143 	struct tpcon rx, tx;
144 	struct notifier_block notifier;
145 	wait_queue_head_t wait;
146 };
147 
148 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
149 {
150 	return (struct isotp_sock *)sk;
151 }
152 
153 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
154 {
155 	struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
156 					     rxtimer);
157 	struct sock *sk = &so->sk;
158 
159 	if (so->rx.state == ISOTP_WAIT_DATA) {
160 		/* we did not get new data frames in time */
161 
162 		/* report 'connection timed out' */
163 		sk->sk_err = ETIMEDOUT;
164 		if (!sock_flag(sk, SOCK_DEAD))
165 			sk->sk_error_report(sk);
166 
167 		/* reset rx state */
168 		so->rx.state = ISOTP_IDLE;
169 	}
170 
171 	return HRTIMER_NORESTART;
172 }
173 
174 static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
175 {
176 	struct net_device *dev;
177 	struct sk_buff *nskb;
178 	struct canfd_frame *ncf;
179 	struct isotp_sock *so = isotp_sk(sk);
180 	int can_send_ret;
181 
182 	nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
183 	if (!nskb)
184 		return 1;
185 
186 	dev = dev_get_by_index(sock_net(sk), so->ifindex);
187 	if (!dev) {
188 		kfree_skb(nskb);
189 		return 1;
190 	}
191 
192 	can_skb_reserve(nskb);
193 	can_skb_prv(nskb)->ifindex = dev->ifindex;
194 	can_skb_prv(nskb)->skbcnt = 0;
195 
196 	nskb->dev = dev;
197 	can_skb_set_owner(nskb, sk);
198 	ncf = (struct canfd_frame *)nskb->data;
199 	skb_put(nskb, so->ll.mtu);
200 
201 	/* create & send flow control reply */
202 	ncf->can_id = so->txid;
203 
204 	if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
205 		memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
206 		ncf->len = CAN_MAX_DLEN;
207 	} else {
208 		ncf->len = ae + FC_CONTENT_SZ;
209 	}
210 
211 	ncf->data[ae] = N_PCI_FC | flowstatus;
212 	ncf->data[ae + 1] = so->rxfc.bs;
213 	ncf->data[ae + 2] = so->rxfc.stmin;
214 
215 	if (ae)
216 		ncf->data[0] = so->opt.ext_address;
217 
218 	if (so->ll.mtu == CANFD_MTU)
219 		ncf->flags = so->ll.tx_flags;
220 
221 	can_send_ret = can_send(nskb, 1);
222 	if (can_send_ret)
223 		pr_notice_once("can-isotp: %s: can_send_ret %d\n",
224 			       __func__, can_send_ret);
225 
226 	dev_put(dev);
227 
228 	/* reset blocksize counter */
229 	so->rx.bs = 0;
230 
231 	/* reset last CF frame rx timestamp for rx stmin enforcement */
232 	so->lastrxcf_tstamp = ktime_set(0, 0);
233 
234 	/* start rx timeout watchdog */
235 	hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
236 	return 0;
237 }
238 
239 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
240 {
241 	struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
242 
243 	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
244 
245 	memset(addr, 0, sizeof(*addr));
246 	addr->can_family = AF_CAN;
247 	addr->can_ifindex = skb->dev->ifindex;
248 
249 	if (sock_queue_rcv_skb(sk, skb) < 0)
250 		kfree_skb(skb);
251 }
252 
253 static u8 padlen(u8 datalen)
254 {
255 	const u8 plen[] = {8, 8, 8, 8, 8, 8, 8, 8, 8,		/* 0 - 8 */
256 			   12, 12, 12, 12,			/* 9 - 12 */
257 			   16, 16, 16, 16,			/* 13 - 16 */
258 			   20, 20, 20, 20,			/* 17 - 20 */
259 			   24, 24, 24, 24,			/* 21 - 24 */
260 			   32, 32, 32, 32, 32, 32, 32, 32,	/* 25 - 32 */
261 			   48, 48, 48, 48, 48, 48, 48, 48,	/* 33 - 40 */
262 			   48, 48, 48, 48, 48, 48, 48, 48};	/* 41 - 48 */
263 
264 	if (datalen > 48)
265 		return 64;
266 
267 	return plen[datalen];
268 }
269 
270 /* check for length optimization and return 1/true when the check fails */
271 static int check_optimized(struct canfd_frame *cf, int start_index)
272 {
273 	/* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
274 	 * padding would start at this point. E.g. if the padding would
275 	 * start at cf.data[7] cf->len has to be 7 to be optimal.
276 	 * Note: The data[] index starts with zero.
277 	 */
278 	if (cf->len <= CAN_MAX_DLEN)
279 		return (cf->len != start_index);
280 
281 	/* This relation is also valid in the non-linear DLC range, where
282 	 * we need to take care of the minimal next possible CAN_DL.
283 	 * The correct check would be (padlen(cf->len) != padlen(start_index)).
284 	 * But as cf->len can only take discrete values from 12, .., 64 at this
285 	 * point the padlen(cf->len) is always equal to cf->len.
286 	 */
287 	return (cf->len != padlen(start_index));
288 }
289 
290 /* check padding and return 1/true when the check fails */
291 static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
292 		     int start_index, u8 content)
293 {
294 	int i;
295 
296 	/* no RX_PADDING value => check length of optimized frame length */
297 	if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
298 		if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
299 			return check_optimized(cf, start_index);
300 
301 		/* no valid test against empty value => ignore frame */
302 		return 1;
303 	}
304 
305 	/* check datalength of correctly padded CAN frame */
306 	if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
307 	    cf->len != padlen(cf->len))
308 		return 1;
309 
310 	/* check padding content */
311 	if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
312 		for (i = start_index; i < cf->len; i++)
313 			if (cf->data[i] != content)
314 				return 1;
315 	}
316 	return 0;
317 }
318 
319 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
320 {
321 	struct sock *sk = &so->sk;
322 
323 	if (so->tx.state != ISOTP_WAIT_FC &&
324 	    so->tx.state != ISOTP_WAIT_FIRST_FC)
325 		return 0;
326 
327 	hrtimer_cancel(&so->txtimer);
328 
329 	if ((cf->len < ae + FC_CONTENT_SZ) ||
330 	    ((so->opt.flags & ISOTP_CHECK_PADDING) &&
331 	     check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
332 		/* malformed PDU - report 'not a data message' */
333 		sk->sk_err = EBADMSG;
334 		if (!sock_flag(sk, SOCK_DEAD))
335 			sk->sk_error_report(sk);
336 
337 		so->tx.state = ISOTP_IDLE;
338 		wake_up_interruptible(&so->wait);
339 		return 1;
340 	}
341 
342 	/* get communication parameters only from the first FC frame */
343 	if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
344 		so->txfc.bs = cf->data[ae + 1];
345 		so->txfc.stmin = cf->data[ae + 2];
346 
347 		/* fix wrong STmin values according spec */
348 		if (so->txfc.stmin > 0x7F &&
349 		    (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
350 			so->txfc.stmin = 0x7F;
351 
352 		so->tx_gap = ktime_set(0, 0);
353 		/* add transmission time for CAN frame N_As */
354 		so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
355 		/* add waiting time for consecutive frames N_Cs */
356 		if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
357 			so->tx_gap = ktime_add_ns(so->tx_gap,
358 						  so->force_tx_stmin);
359 		else if (so->txfc.stmin < 0x80)
360 			so->tx_gap = ktime_add_ns(so->tx_gap,
361 						  so->txfc.stmin * 1000000);
362 		else
363 			so->tx_gap = ktime_add_ns(so->tx_gap,
364 						  (so->txfc.stmin - 0xF0)
365 						  * 100000);
366 		so->tx.state = ISOTP_WAIT_FC;
367 	}
368 
369 	switch (cf->data[ae] & 0x0F) {
370 	case ISOTP_FC_CTS:
371 		so->tx.bs = 0;
372 		so->tx.state = ISOTP_SENDING;
373 		/* start cyclic timer for sending CF frame */
374 		hrtimer_start(&so->txtimer, so->tx_gap,
375 			      HRTIMER_MODE_REL_SOFT);
376 		break;
377 
378 	case ISOTP_FC_WT:
379 		/* start timer to wait for next FC frame */
380 		hrtimer_start(&so->txtimer, ktime_set(1, 0),
381 			      HRTIMER_MODE_REL_SOFT);
382 		break;
383 
384 	case ISOTP_FC_OVFLW:
385 		/* overflow on receiver side - report 'message too long' */
386 		sk->sk_err = EMSGSIZE;
387 		if (!sock_flag(sk, SOCK_DEAD))
388 			sk->sk_error_report(sk);
389 		fallthrough;
390 
391 	default:
392 		/* stop this tx job */
393 		so->tx.state = ISOTP_IDLE;
394 		wake_up_interruptible(&so->wait);
395 	}
396 	return 0;
397 }
398 
399 static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
400 			struct sk_buff *skb, int len)
401 {
402 	struct isotp_sock *so = isotp_sk(sk);
403 	struct sk_buff *nskb;
404 
405 	hrtimer_cancel(&so->rxtimer);
406 	so->rx.state = ISOTP_IDLE;
407 
408 	if (!len || len > cf->len - pcilen)
409 		return 1;
410 
411 	if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
412 	    check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
413 		/* malformed PDU - report 'not a data message' */
414 		sk->sk_err = EBADMSG;
415 		if (!sock_flag(sk, SOCK_DEAD))
416 			sk->sk_error_report(sk);
417 		return 1;
418 	}
419 
420 	nskb = alloc_skb(len, gfp_any());
421 	if (!nskb)
422 		return 1;
423 
424 	memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
425 
426 	nskb->tstamp = skb->tstamp;
427 	nskb->dev = skb->dev;
428 	isotp_rcv_skb(nskb, sk);
429 	return 0;
430 }
431 
432 static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
433 {
434 	struct isotp_sock *so = isotp_sk(sk);
435 	int i;
436 	int off;
437 	int ff_pci_sz;
438 
439 	hrtimer_cancel(&so->rxtimer);
440 	so->rx.state = ISOTP_IDLE;
441 
442 	/* get the used sender LL_DL from the (first) CAN frame data length */
443 	so->rx.ll_dl = padlen(cf->len);
444 
445 	/* the first frame has to use the entire frame up to LL_DL length */
446 	if (cf->len != so->rx.ll_dl)
447 		return 1;
448 
449 	/* get the FF_DL */
450 	so->rx.len = (cf->data[ae] & 0x0F) << 8;
451 	so->rx.len += cf->data[ae + 1];
452 
453 	/* Check for FF_DL escape sequence supporting 32 bit PDU length */
454 	if (so->rx.len) {
455 		ff_pci_sz = FF_PCI_SZ12;
456 	} else {
457 		/* FF_DL = 0 => get real length from next 4 bytes */
458 		so->rx.len = cf->data[ae + 2] << 24;
459 		so->rx.len += cf->data[ae + 3] << 16;
460 		so->rx.len += cf->data[ae + 4] << 8;
461 		so->rx.len += cf->data[ae + 5];
462 		ff_pci_sz = FF_PCI_SZ32;
463 	}
464 
465 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
466 	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
467 
468 	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
469 		return 1;
470 
471 	if (so->rx.len > MAX_MSG_LENGTH) {
472 		/* send FC frame with overflow status */
473 		isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
474 		return 1;
475 	}
476 
477 	/* copy the first received data bytes */
478 	so->rx.idx = 0;
479 	for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
480 		so->rx.buf[so->rx.idx++] = cf->data[i];
481 
482 	/* initial setup for this pdu reception */
483 	so->rx.sn = 1;
484 	so->rx.state = ISOTP_WAIT_DATA;
485 
486 	/* no creation of flow control frames */
487 	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
488 		return 0;
489 
490 	/* send our first FC frame */
491 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
492 	return 0;
493 }
494 
495 static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
496 			struct sk_buff *skb)
497 {
498 	struct isotp_sock *so = isotp_sk(sk);
499 	struct sk_buff *nskb;
500 	int i;
501 
502 	if (so->rx.state != ISOTP_WAIT_DATA)
503 		return 0;
504 
505 	/* drop if timestamp gap is less than force_rx_stmin nano secs */
506 	if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
507 		if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
508 		    so->force_rx_stmin)
509 			return 0;
510 
511 		so->lastrxcf_tstamp = skb->tstamp;
512 	}
513 
514 	hrtimer_cancel(&so->rxtimer);
515 
516 	/* CFs are never longer than the FF */
517 	if (cf->len > so->rx.ll_dl)
518 		return 1;
519 
520 	/* CFs have usually the LL_DL length */
521 	if (cf->len < so->rx.ll_dl) {
522 		/* this is only allowed for the last CF */
523 		if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
524 			return 1;
525 	}
526 
527 	if ((cf->data[ae] & 0x0F) != so->rx.sn) {
528 		/* wrong sn detected - report 'illegal byte sequence' */
529 		sk->sk_err = EILSEQ;
530 		if (!sock_flag(sk, SOCK_DEAD))
531 			sk->sk_error_report(sk);
532 
533 		/* reset rx state */
534 		so->rx.state = ISOTP_IDLE;
535 		return 1;
536 	}
537 	so->rx.sn++;
538 	so->rx.sn %= 16;
539 
540 	for (i = ae + N_PCI_SZ; i < cf->len; i++) {
541 		so->rx.buf[so->rx.idx++] = cf->data[i];
542 		if (so->rx.idx >= so->rx.len)
543 			break;
544 	}
545 
546 	if (so->rx.idx >= so->rx.len) {
547 		/* we are done */
548 		so->rx.state = ISOTP_IDLE;
549 
550 		if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
551 		    check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
552 			/* malformed PDU - report 'not a data message' */
553 			sk->sk_err = EBADMSG;
554 			if (!sock_flag(sk, SOCK_DEAD))
555 				sk->sk_error_report(sk);
556 			return 1;
557 		}
558 
559 		nskb = alloc_skb(so->rx.len, gfp_any());
560 		if (!nskb)
561 			return 1;
562 
563 		memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
564 		       so->rx.len);
565 
566 		nskb->tstamp = skb->tstamp;
567 		nskb->dev = skb->dev;
568 		isotp_rcv_skb(nskb, sk);
569 		return 0;
570 	}
571 
572 	/* no creation of flow control frames */
573 	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
574 		return 0;
575 
576 	/* perform blocksize handling, if enabled */
577 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
578 		/* start rx timeout watchdog */
579 		hrtimer_start(&so->rxtimer, ktime_set(1, 0),
580 			      HRTIMER_MODE_REL_SOFT);
581 		return 0;
582 	}
583 
584 	/* we reached the specified blocksize so->rxfc.bs */
585 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
586 	return 0;
587 }
588 
589 static void isotp_rcv(struct sk_buff *skb, void *data)
590 {
591 	struct sock *sk = (struct sock *)data;
592 	struct isotp_sock *so = isotp_sk(sk);
593 	struct canfd_frame *cf;
594 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
595 	u8 n_pci_type, sf_dl;
596 
597 	/* Strictly receive only frames with the configured MTU size
598 	 * => clear separation of CAN2.0 / CAN FD transport channels
599 	 */
600 	if (skb->len != so->ll.mtu)
601 		return;
602 
603 	cf = (struct canfd_frame *)skb->data;
604 
605 	/* if enabled: check reception of my configured extended address */
606 	if (ae && cf->data[0] != so->opt.rx_ext_address)
607 		return;
608 
609 	n_pci_type = cf->data[ae] & 0xF0;
610 
611 	if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
612 		/* check rx/tx path half duplex expectations */
613 		if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
614 		    (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
615 			return;
616 	}
617 
618 	switch (n_pci_type) {
619 	case N_PCI_FC:
620 		/* tx path: flow control frame containing the FC parameters */
621 		isotp_rcv_fc(so, cf, ae);
622 		break;
623 
624 	case N_PCI_SF:
625 		/* rx path: single frame
626 		 *
627 		 * As we do not have a rx.ll_dl configuration, we can only test
628 		 * if the CAN frames payload length matches the LL_DL == 8
629 		 * requirements - no matter if it's CAN 2.0 or CAN FD
630 		 */
631 
632 		/* get the SF_DL from the N_PCI byte */
633 		sf_dl = cf->data[ae] & 0x0F;
634 
635 		if (cf->len <= CAN_MAX_DLEN) {
636 			isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl);
637 		} else {
638 			if (skb->len == CANFD_MTU) {
639 				/* We have a CAN FD frame and CAN_DL is greater than 8:
640 				 * Only frames with the SF_DL == 0 ESC value are valid.
641 				 *
642 				 * If so take care of the increased SF PCI size
643 				 * (SF_PCI_SZ8) to point to the message content behind
644 				 * the extended SF PCI info and get the real SF_DL
645 				 * length value from the formerly first data byte.
646 				 */
647 				if (sf_dl == 0)
648 					isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb,
649 						     cf->data[SF_PCI_SZ4 + ae]);
650 			}
651 		}
652 		break;
653 
654 	case N_PCI_FF:
655 		/* rx path: first frame */
656 		isotp_rcv_ff(sk, cf, ae);
657 		break;
658 
659 	case N_PCI_CF:
660 		/* rx path: consecutive frame */
661 		isotp_rcv_cf(sk, cf, ae, skb);
662 		break;
663 	}
664 }
665 
666 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
667 				 int ae, int off)
668 {
669 	int pcilen = N_PCI_SZ + ae + off;
670 	int space = so->tx.ll_dl - pcilen;
671 	int num = min_t(int, so->tx.len - so->tx.idx, space);
672 	int i;
673 
674 	cf->can_id = so->txid;
675 	cf->len = num + pcilen;
676 
677 	if (num < space) {
678 		if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
679 			/* user requested padding */
680 			cf->len = padlen(cf->len);
681 			memset(cf->data, so->opt.txpad_content, cf->len);
682 		} else if (cf->len > CAN_MAX_DLEN) {
683 			/* mandatory padding for CAN FD frames */
684 			cf->len = padlen(cf->len);
685 			memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
686 			       cf->len);
687 		}
688 	}
689 
690 	for (i = 0; i < num; i++)
691 		cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
692 
693 	if (ae)
694 		cf->data[0] = so->opt.ext_address;
695 }
696 
697 static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
698 				int ae)
699 {
700 	int i;
701 	int ff_pci_sz;
702 
703 	cf->can_id = so->txid;
704 	cf->len = so->tx.ll_dl;
705 	if (ae)
706 		cf->data[0] = so->opt.ext_address;
707 
708 	/* create N_PCI bytes with 12/32 bit FF_DL data length */
709 	if (so->tx.len > 4095) {
710 		/* use 32 bit FF_DL notation */
711 		cf->data[ae] = N_PCI_FF;
712 		cf->data[ae + 1] = 0;
713 		cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
714 		cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
715 		cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
716 		cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
717 		ff_pci_sz = FF_PCI_SZ32;
718 	} else {
719 		/* use 12 bit FF_DL notation */
720 		cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
721 		cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
722 		ff_pci_sz = FF_PCI_SZ12;
723 	}
724 
725 	/* add first data bytes depending on ae */
726 	for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
727 		cf->data[i] = so->tx.buf[so->tx.idx++];
728 
729 	so->tx.sn = 1;
730 	so->tx.state = ISOTP_WAIT_FIRST_FC;
731 }
732 
733 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
734 {
735 	struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
736 					     txtimer);
737 	struct sock *sk = &so->sk;
738 	struct sk_buff *skb;
739 	struct net_device *dev;
740 	struct canfd_frame *cf;
741 	enum hrtimer_restart restart = HRTIMER_NORESTART;
742 	int can_send_ret;
743 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
744 
745 	switch (so->tx.state) {
746 	case ISOTP_WAIT_FC:
747 	case ISOTP_WAIT_FIRST_FC:
748 
749 		/* we did not get any flow control frame in time */
750 
751 		/* report 'communication error on send' */
752 		sk->sk_err = ECOMM;
753 		if (!sock_flag(sk, SOCK_DEAD))
754 			sk->sk_error_report(sk);
755 
756 		/* reset tx state */
757 		so->tx.state = ISOTP_IDLE;
758 		wake_up_interruptible(&so->wait);
759 		break;
760 
761 	case ISOTP_SENDING:
762 
763 		/* push out the next segmented pdu */
764 		dev = dev_get_by_index(sock_net(sk), so->ifindex);
765 		if (!dev)
766 			break;
767 
768 isotp_tx_burst:
769 		skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv),
770 				GFP_ATOMIC);
771 		if (!skb) {
772 			dev_put(dev);
773 			break;
774 		}
775 
776 		can_skb_reserve(skb);
777 		can_skb_prv(skb)->ifindex = dev->ifindex;
778 		can_skb_prv(skb)->skbcnt = 0;
779 
780 		cf = (struct canfd_frame *)skb->data;
781 		skb_put(skb, so->ll.mtu);
782 
783 		/* create consecutive frame */
784 		isotp_fill_dataframe(cf, so, ae, 0);
785 
786 		/* place consecutive frame N_PCI in appropriate index */
787 		cf->data[ae] = N_PCI_CF | so->tx.sn++;
788 		so->tx.sn %= 16;
789 		so->tx.bs++;
790 
791 		if (so->ll.mtu == CANFD_MTU)
792 			cf->flags = so->ll.tx_flags;
793 
794 		skb->dev = dev;
795 		can_skb_set_owner(skb, sk);
796 
797 		can_send_ret = can_send(skb, 1);
798 		if (can_send_ret)
799 			pr_notice_once("can-isotp: %s: can_send_ret %d\n",
800 				       __func__, can_send_ret);
801 
802 		if (so->tx.idx >= so->tx.len) {
803 			/* we are done */
804 			so->tx.state = ISOTP_IDLE;
805 			dev_put(dev);
806 			wake_up_interruptible(&so->wait);
807 			break;
808 		}
809 
810 		if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
811 			/* stop and wait for FC */
812 			so->tx.state = ISOTP_WAIT_FC;
813 			dev_put(dev);
814 			hrtimer_set_expires(&so->txtimer,
815 					    ktime_add(ktime_get(),
816 						      ktime_set(1, 0)));
817 			restart = HRTIMER_RESTART;
818 			break;
819 		}
820 
821 		/* no gap between data frames needed => use burst mode */
822 		if (!so->tx_gap)
823 			goto isotp_tx_burst;
824 
825 		/* start timer to send next data frame with correct delay */
826 		dev_put(dev);
827 		hrtimer_set_expires(&so->txtimer,
828 				    ktime_add(ktime_get(), so->tx_gap));
829 		restart = HRTIMER_RESTART;
830 		break;
831 
832 	default:
833 		WARN_ON_ONCE(1);
834 	}
835 
836 	return restart;
837 }
838 
839 static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
840 {
841 	struct sock *sk = sock->sk;
842 	struct isotp_sock *so = isotp_sk(sk);
843 	struct sk_buff *skb;
844 	struct net_device *dev;
845 	struct canfd_frame *cf;
846 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
847 	int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
848 	int off;
849 	int err;
850 
851 	if (!so->bound)
852 		return -EADDRNOTAVAIL;
853 
854 	/* we do not support multiple buffers - for now */
855 	if (so->tx.state != ISOTP_IDLE || wq_has_sleeper(&so->wait)) {
856 		if (msg->msg_flags & MSG_DONTWAIT)
857 			return -EAGAIN;
858 
859 		/* wait for complete transmission of current pdu */
860 		wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
861 	}
862 
863 	if (!size || size > MAX_MSG_LENGTH)
864 		return -EINVAL;
865 
866 	err = memcpy_from_msg(so->tx.buf, msg, size);
867 	if (err < 0)
868 		return err;
869 
870 	dev = dev_get_by_index(sock_net(sk), so->ifindex);
871 	if (!dev)
872 		return -ENXIO;
873 
874 	skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
875 				  msg->msg_flags & MSG_DONTWAIT, &err);
876 	if (!skb) {
877 		dev_put(dev);
878 		return err;
879 	}
880 
881 	can_skb_reserve(skb);
882 	can_skb_prv(skb)->ifindex = dev->ifindex;
883 	can_skb_prv(skb)->skbcnt = 0;
884 
885 	so->tx.state = ISOTP_SENDING;
886 	so->tx.len = size;
887 	so->tx.idx = 0;
888 
889 	cf = (struct canfd_frame *)skb->data;
890 	skb_put(skb, so->ll.mtu);
891 
892 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
893 	off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
894 
895 	/* check for single frame transmission depending on TX_DL */
896 	if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
897 		/* The message size generally fits into a SingleFrame - good.
898 		 *
899 		 * SF_DL ESC offset optimization:
900 		 *
901 		 * When TX_DL is greater 8 but the message would still fit
902 		 * into a 8 byte CAN frame, we can omit the offset.
903 		 * This prevents a protocol caused length extension from
904 		 * CAN_DL = 8 to CAN_DL = 12 due to the SF_SL ESC handling.
905 		 */
906 		if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
907 			off = 0;
908 
909 		isotp_fill_dataframe(cf, so, ae, off);
910 
911 		/* place single frame N_PCI w/o length in appropriate index */
912 		cf->data[ae] = N_PCI_SF;
913 
914 		/* place SF_DL size value depending on the SF_DL ESC offset */
915 		if (off)
916 			cf->data[SF_PCI_SZ4 + ae] = size;
917 		else
918 			cf->data[ae] |= size;
919 
920 		so->tx.state = ISOTP_IDLE;
921 		wake_up_interruptible(&so->wait);
922 
923 		/* don't enable wait queue for a single frame transmission */
924 		wait_tx_done = 0;
925 	} else {
926 		/* send first frame and wait for FC */
927 
928 		isotp_create_fframe(cf, so, ae);
929 
930 		/* start timeout for FC */
931 		hrtimer_start(&so->txtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
932 	}
933 
934 	/* send the first or only CAN frame */
935 	if (so->ll.mtu == CANFD_MTU)
936 		cf->flags = so->ll.tx_flags;
937 
938 	skb->dev = dev;
939 	skb->sk = sk;
940 	err = can_send(skb, 1);
941 	dev_put(dev);
942 	if (err) {
943 		pr_notice_once("can-isotp: %s: can_send_ret %d\n",
944 			       __func__, err);
945 		return err;
946 	}
947 
948 	if (wait_tx_done) {
949 		/* wait for complete transmission of current pdu */
950 		wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
951 	}
952 
953 	return size;
954 }
955 
956 static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
957 			 int flags)
958 {
959 	struct sock *sk = sock->sk;
960 	struct sk_buff *skb;
961 	int err = 0;
962 	int noblock;
963 
964 	noblock = flags & MSG_DONTWAIT;
965 	flags &= ~MSG_DONTWAIT;
966 
967 	skb = skb_recv_datagram(sk, flags, noblock, &err);
968 	if (!skb)
969 		return err;
970 
971 	if (size < skb->len)
972 		msg->msg_flags |= MSG_TRUNC;
973 	else
974 		size = skb->len;
975 
976 	err = memcpy_to_msg(msg, skb->data, size);
977 	if (err < 0) {
978 		skb_free_datagram(sk, skb);
979 		return err;
980 	}
981 
982 	sock_recv_timestamp(msg, sk, skb);
983 
984 	if (msg->msg_name) {
985 		msg->msg_namelen = sizeof(struct sockaddr_can);
986 		memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
987 	}
988 
989 	skb_free_datagram(sk, skb);
990 
991 	return size;
992 }
993 
994 static int isotp_release(struct socket *sock)
995 {
996 	struct sock *sk = sock->sk;
997 	struct isotp_sock *so;
998 	struct net *net;
999 
1000 	if (!sk)
1001 		return 0;
1002 
1003 	so = isotp_sk(sk);
1004 	net = sock_net(sk);
1005 
1006 	/* wait for complete transmission of current pdu */
1007 	wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1008 
1009 	unregister_netdevice_notifier(&so->notifier);
1010 
1011 	lock_sock(sk);
1012 
1013 	hrtimer_cancel(&so->txtimer);
1014 	hrtimer_cancel(&so->rxtimer);
1015 
1016 	/* remove current filters & unregister */
1017 	if (so->bound) {
1018 		if (so->ifindex) {
1019 			struct net_device *dev;
1020 
1021 			dev = dev_get_by_index(net, so->ifindex);
1022 			if (dev) {
1023 				can_rx_unregister(net, dev, so->rxid,
1024 						  SINGLE_MASK(so->rxid),
1025 						  isotp_rcv, sk);
1026 				dev_put(dev);
1027 			}
1028 		}
1029 	}
1030 
1031 	so->ifindex = 0;
1032 	so->bound = 0;
1033 
1034 	sock_orphan(sk);
1035 	sock->sk = NULL;
1036 
1037 	release_sock(sk);
1038 	sock_put(sk);
1039 
1040 	return 0;
1041 }
1042 
1043 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
1044 {
1045 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1046 	struct sock *sk = sock->sk;
1047 	struct isotp_sock *so = isotp_sk(sk);
1048 	struct net *net = sock_net(sk);
1049 	int ifindex;
1050 	struct net_device *dev;
1051 	int err = 0;
1052 	int notify_enetdown = 0;
1053 
1054 	if (len < CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.tp))
1055 		return -EINVAL;
1056 
1057 	if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
1058 		return -EADDRNOTAVAIL;
1059 
1060 	if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) &
1061 	    (CAN_ERR_FLAG | CAN_RTR_FLAG))
1062 		return -EADDRNOTAVAIL;
1063 
1064 	if (!addr->can_ifindex)
1065 		return -ENODEV;
1066 
1067 	lock_sock(sk);
1068 
1069 	if (so->bound && addr->can_ifindex == so->ifindex &&
1070 	    addr->can_addr.tp.rx_id == so->rxid &&
1071 	    addr->can_addr.tp.tx_id == so->txid)
1072 		goto out;
1073 
1074 	dev = dev_get_by_index(net, addr->can_ifindex);
1075 	if (!dev) {
1076 		err = -ENODEV;
1077 		goto out;
1078 	}
1079 	if (dev->type != ARPHRD_CAN) {
1080 		dev_put(dev);
1081 		err = -ENODEV;
1082 		goto out;
1083 	}
1084 	if (dev->mtu < so->ll.mtu) {
1085 		dev_put(dev);
1086 		err = -EINVAL;
1087 		goto out;
1088 	}
1089 	if (!(dev->flags & IFF_UP))
1090 		notify_enetdown = 1;
1091 
1092 	ifindex = dev->ifindex;
1093 
1094 	can_rx_register(net, dev, addr->can_addr.tp.rx_id,
1095 			SINGLE_MASK(addr->can_addr.tp.rx_id), isotp_rcv, sk,
1096 			"isotp", sk);
1097 
1098 	dev_put(dev);
1099 
1100 	if (so->bound) {
1101 		/* unregister old filter */
1102 		if (so->ifindex) {
1103 			dev = dev_get_by_index(net, so->ifindex);
1104 			if (dev) {
1105 				can_rx_unregister(net, dev, so->rxid,
1106 						  SINGLE_MASK(so->rxid),
1107 						  isotp_rcv, sk);
1108 				dev_put(dev);
1109 			}
1110 		}
1111 	}
1112 
1113 	/* switch to new settings */
1114 	so->ifindex = ifindex;
1115 	so->rxid = addr->can_addr.tp.rx_id;
1116 	so->txid = addr->can_addr.tp.tx_id;
1117 	so->bound = 1;
1118 
1119 out:
1120 	release_sock(sk);
1121 
1122 	if (notify_enetdown) {
1123 		sk->sk_err = ENETDOWN;
1124 		if (!sock_flag(sk, SOCK_DEAD))
1125 			sk->sk_error_report(sk);
1126 	}
1127 
1128 	return err;
1129 }
1130 
1131 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
1132 {
1133 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1134 	struct sock *sk = sock->sk;
1135 	struct isotp_sock *so = isotp_sk(sk);
1136 
1137 	if (peer)
1138 		return -EOPNOTSUPP;
1139 
1140 	addr->can_family = AF_CAN;
1141 	addr->can_ifindex = so->ifindex;
1142 	addr->can_addr.tp.rx_id = so->rxid;
1143 	addr->can_addr.tp.tx_id = so->txid;
1144 
1145 	return sizeof(*addr);
1146 }
1147 
1148 static int isotp_setsockopt(struct socket *sock, int level, int optname,
1149 			    sockptr_t optval, unsigned int optlen)
1150 {
1151 	struct sock *sk = sock->sk;
1152 	struct isotp_sock *so = isotp_sk(sk);
1153 	int ret = 0;
1154 
1155 	if (level != SOL_CAN_ISOTP)
1156 		return -EINVAL;
1157 
1158 	switch (optname) {
1159 	case CAN_ISOTP_OPTS:
1160 		if (optlen != sizeof(struct can_isotp_options))
1161 			return -EINVAL;
1162 
1163 		if (copy_from_sockptr(&so->opt, optval, optlen))
1164 			return -EFAULT;
1165 
1166 		/* no separate rx_ext_address is given => use ext_address */
1167 		if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1168 			so->opt.rx_ext_address = so->opt.ext_address;
1169 		break;
1170 
1171 	case CAN_ISOTP_RECV_FC:
1172 		if (optlen != sizeof(struct can_isotp_fc_options))
1173 			return -EINVAL;
1174 
1175 		if (copy_from_sockptr(&so->rxfc, optval, optlen))
1176 			return -EFAULT;
1177 		break;
1178 
1179 	case CAN_ISOTP_TX_STMIN:
1180 		if (optlen != sizeof(u32))
1181 			return -EINVAL;
1182 
1183 		if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1184 			return -EFAULT;
1185 		break;
1186 
1187 	case CAN_ISOTP_RX_STMIN:
1188 		if (optlen != sizeof(u32))
1189 			return -EINVAL;
1190 
1191 		if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1192 			return -EFAULT;
1193 		break;
1194 
1195 	case CAN_ISOTP_LL_OPTS:
1196 		if (optlen == sizeof(struct can_isotp_ll_options)) {
1197 			struct can_isotp_ll_options ll;
1198 
1199 			if (copy_from_sockptr(&ll, optval, optlen))
1200 				return -EFAULT;
1201 
1202 			/* check for correct ISO 11898-1 DLC data length */
1203 			if (ll.tx_dl != padlen(ll.tx_dl))
1204 				return -EINVAL;
1205 
1206 			if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU)
1207 				return -EINVAL;
1208 
1209 			if (ll.mtu == CAN_MTU && ll.tx_dl > CAN_MAX_DLEN)
1210 				return -EINVAL;
1211 
1212 			memcpy(&so->ll, &ll, sizeof(ll));
1213 
1214 			/* set ll_dl for tx path to similar place as for rx */
1215 			so->tx.ll_dl = ll.tx_dl;
1216 		} else {
1217 			return -EINVAL;
1218 		}
1219 		break;
1220 
1221 	default:
1222 		ret = -ENOPROTOOPT;
1223 	}
1224 
1225 	return ret;
1226 }
1227 
1228 static int isotp_getsockopt(struct socket *sock, int level, int optname,
1229 			    char __user *optval, int __user *optlen)
1230 {
1231 	struct sock *sk = sock->sk;
1232 	struct isotp_sock *so = isotp_sk(sk);
1233 	int len;
1234 	void *val;
1235 
1236 	if (level != SOL_CAN_ISOTP)
1237 		return -EINVAL;
1238 	if (get_user(len, optlen))
1239 		return -EFAULT;
1240 	if (len < 0)
1241 		return -EINVAL;
1242 
1243 	switch (optname) {
1244 	case CAN_ISOTP_OPTS:
1245 		len = min_t(int, len, sizeof(struct can_isotp_options));
1246 		val = &so->opt;
1247 		break;
1248 
1249 	case CAN_ISOTP_RECV_FC:
1250 		len = min_t(int, len, sizeof(struct can_isotp_fc_options));
1251 		val = &so->rxfc;
1252 		break;
1253 
1254 	case CAN_ISOTP_TX_STMIN:
1255 		len = min_t(int, len, sizeof(u32));
1256 		val = &so->force_tx_stmin;
1257 		break;
1258 
1259 	case CAN_ISOTP_RX_STMIN:
1260 		len = min_t(int, len, sizeof(u32));
1261 		val = &so->force_rx_stmin;
1262 		break;
1263 
1264 	case CAN_ISOTP_LL_OPTS:
1265 		len = min_t(int, len, sizeof(struct can_isotp_ll_options));
1266 		val = &so->ll;
1267 		break;
1268 
1269 	default:
1270 		return -ENOPROTOOPT;
1271 	}
1272 
1273 	if (put_user(len, optlen))
1274 		return -EFAULT;
1275 	if (copy_to_user(optval, val, len))
1276 		return -EFAULT;
1277 	return 0;
1278 }
1279 
1280 static int isotp_notifier(struct notifier_block *nb, unsigned long msg,
1281 			  void *ptr)
1282 {
1283 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1284 	struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
1285 	struct sock *sk = &so->sk;
1286 
1287 	if (!net_eq(dev_net(dev), sock_net(sk)))
1288 		return NOTIFY_DONE;
1289 
1290 	if (dev->type != ARPHRD_CAN)
1291 		return NOTIFY_DONE;
1292 
1293 	if (so->ifindex != dev->ifindex)
1294 		return NOTIFY_DONE;
1295 
1296 	switch (msg) {
1297 	case NETDEV_UNREGISTER:
1298 		lock_sock(sk);
1299 		/* remove current filters & unregister */
1300 		if (so->bound)
1301 			can_rx_unregister(dev_net(dev), dev, so->rxid,
1302 					  SINGLE_MASK(so->rxid),
1303 					  isotp_rcv, sk);
1304 
1305 		so->ifindex = 0;
1306 		so->bound  = 0;
1307 		release_sock(sk);
1308 
1309 		sk->sk_err = ENODEV;
1310 		if (!sock_flag(sk, SOCK_DEAD))
1311 			sk->sk_error_report(sk);
1312 		break;
1313 
1314 	case NETDEV_DOWN:
1315 		sk->sk_err = ENETDOWN;
1316 		if (!sock_flag(sk, SOCK_DEAD))
1317 			sk->sk_error_report(sk);
1318 		break;
1319 	}
1320 
1321 	return NOTIFY_DONE;
1322 }
1323 
1324 static int isotp_init(struct sock *sk)
1325 {
1326 	struct isotp_sock *so = isotp_sk(sk);
1327 
1328 	so->ifindex = 0;
1329 	so->bound = 0;
1330 
1331 	so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1332 	so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1333 	so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1334 	so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1335 	so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1336 	so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1337 	so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1338 	so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1339 	so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1340 	so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1341 	so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1342 	so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1343 
1344 	/* set ll_dl for tx path to similar place as for rx */
1345 	so->tx.ll_dl = so->ll.tx_dl;
1346 
1347 	so->rx.state = ISOTP_IDLE;
1348 	so->tx.state = ISOTP_IDLE;
1349 
1350 	hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1351 	so->rxtimer.function = isotp_rx_timer_handler;
1352 	hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1353 	so->txtimer.function = isotp_tx_timer_handler;
1354 
1355 	init_waitqueue_head(&so->wait);
1356 
1357 	so->notifier.notifier_call = isotp_notifier;
1358 	register_netdevice_notifier(&so->notifier);
1359 
1360 	return 0;
1361 }
1362 
1363 static int isotp_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1364 				  unsigned long arg)
1365 {
1366 	/* no ioctls for socket layer -> hand it down to NIC layer */
1367 	return -ENOIOCTLCMD;
1368 }
1369 
1370 static const struct proto_ops isotp_ops = {
1371 	.family = PF_CAN,
1372 	.release = isotp_release,
1373 	.bind = isotp_bind,
1374 	.connect = sock_no_connect,
1375 	.socketpair = sock_no_socketpair,
1376 	.accept = sock_no_accept,
1377 	.getname = isotp_getname,
1378 	.poll = datagram_poll,
1379 	.ioctl = isotp_sock_no_ioctlcmd,
1380 	.gettstamp = sock_gettstamp,
1381 	.listen = sock_no_listen,
1382 	.shutdown = sock_no_shutdown,
1383 	.setsockopt = isotp_setsockopt,
1384 	.getsockopt = isotp_getsockopt,
1385 	.sendmsg = isotp_sendmsg,
1386 	.recvmsg = isotp_recvmsg,
1387 	.mmap = sock_no_mmap,
1388 	.sendpage = sock_no_sendpage,
1389 };
1390 
1391 static struct proto isotp_proto __read_mostly = {
1392 	.name = "CAN_ISOTP",
1393 	.owner = THIS_MODULE,
1394 	.obj_size = sizeof(struct isotp_sock),
1395 	.init = isotp_init,
1396 };
1397 
1398 static const struct can_proto isotp_can_proto = {
1399 	.type = SOCK_DGRAM,
1400 	.protocol = CAN_ISOTP,
1401 	.ops = &isotp_ops,
1402 	.prot = &isotp_proto,
1403 };
1404 
1405 static __init int isotp_module_init(void)
1406 {
1407 	int err;
1408 
1409 	pr_info("can: isotp protocol\n");
1410 
1411 	err = can_proto_register(&isotp_can_proto);
1412 	if (err < 0)
1413 		pr_err("can: registration of isotp protocol failed\n");
1414 
1415 	return err;
1416 }
1417 
1418 static __exit void isotp_module_exit(void)
1419 {
1420 	can_proto_unregister(&isotp_can_proto);
1421 }
1422 
1423 module_init(isotp_module_init);
1424 module_exit(isotp_module_exit);
1425