2 * isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
4 * WARNING: This is ALPHA code for discussions and first tests that should
5 * not be used in productive environments.
7 * In the discussion the Socket-API to the userspace or the ISO-TP socket
8 * options or the return values we may change! Current behaviour:
10 * - no ISO-TP specific return values are provided to the userspace
11 * - when a transfer (tx) is on the run the next write() blocks until it's done
12 * - no support for sending wait frames to the data source in the rx path
14 * Copyright (c) 2008 Volkswagen Group Electronic Research
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of Volkswagen nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * Alternatively, provided that this notice is retained in full, this
30 * software may be distributed under the terms of the GNU General
31 * Public License ("GPL") version 2, in which case the provisions of the
32 * GPL apply INSTEAD OF those given above.
34 * The provided data structures and external interfaces from this code
35 * are not restricted to be used by modules with a GPL compatible license.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50 * Send feedback to <socketcan-users@lists.berlios.de>
54 #include <linux/module.h>
55 #include <linux/version.h>
56 #include <linux/init.h>
57 #include <linux/wait.h>
58 #include <linux/uio.h>
59 #include <linux/net.h>
60 #include <linux/netdevice.h>
61 #include <linux/socket.h>
62 #include <linux/if_arp.h>
63 #include <linux/skbuff.h>
64 #include <linux/can.h>
65 #include <linux/can/core.h>
66 #include <linux/can/isotp.h>
68 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
69 #include <net/net_namespace.h>
71 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
75 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
78 #define CAN_ISOTP_VERSION CAN_VERSION
79 static __initdata const char banner[] =
80 KERN_INFO "can: isotp protocol (rev " CAN_ISOTP_VERSION " alpha)\n";
82 MODULE_DESCRIPTION("PF_CAN isotp 15765-2 protocol");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
85 MODULE_ALIAS("can-proto-6");
87 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
88 #error This modules needs hrtimers (available since Kernel 2.6.22)
91 #define DBG(fmt, args...) (printk( KERN_DEBUG "can-isotp: %s: " fmt, \
94 #define DBG(fmt, args...)
96 #define SINGLE_MASK(id) ((id & CAN_EFF_FLAG) ? \
97 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
98 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
100 /* N_PCI type values in bits 7-4 of N_PCI bytes */
101 #define N_PCI_SF 0x00 /* single frame */
102 #define N_PCI_FF 0x10 /* first frame */
103 #define N_PCI_CF 0x20 /* consecutive frame */
104 #define N_PCI_FC 0x30 /* flow control */
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 */
135 struct hrtimer rxtimer, txtimer;
136 struct tasklet_struct txtsklet;
137 struct can_isotp_options opt;
138 struct can_isotp_fc_options rxfc, txfc;
140 struct notifier_block notifier;
141 wait_queue_head_t wait;
144 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
146 return (struct isotp_sock *)sk;
149 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
151 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
153 if (so->rx.state == ISOTP_WAIT_DATA) {
155 struct sock *sk = &so->sk;
157 /* report 'timeout' */
159 if (!sock_flag(sk, SOCK_DEAD))
160 sk->sk_error_report(sk);
162 DBG("we did not get new data frames in time.\n");
165 so->rx.state = ISOTP_IDLE;
168 return HRTIMER_NORESTART;
171 static int isotp_send_fc(struct sock *sk, int ae)
173 struct net_device *dev;
174 struct sk_buff *nskb;
175 struct can_frame *ncf;
176 struct isotp_sock *so = isotp_sk(sk);
178 nskb = alloc_skb(sizeof(struct can_frame), gfp_any());
182 dev = dev_get_by_index(&init_net, so->ifindex);
189 ncf = (struct can_frame *) nskb->data;
190 skb_put(nskb, sizeof(struct can_frame));
192 /* create & send flow control reply */
193 ncf->can_id = so->txid;
195 if (so->opt.flags & CAN_ISOTP_RX_PADDING) {
196 memset(ncf->data, so->opt.rxpad_content, 8);
201 ncf->data[ae] = N_PCI_FC | ISOTP_FC_CTS;
202 ncf->data[ae+1] = so->rxfc.bs;
203 ncf->data[ae+2] = so->rxfc.stmin;
206 ncf->data[0] = so->opt.ext_address;
211 /* reset blocksize counter */
214 /* start rx timeout watchdog */
215 hrtimer_start(&so->rxtimer, ktime_set(1,0), HRTIMER_MODE_REL);
219 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
221 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
223 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
227 memset(addr, 0, sizeof(*addr));
228 addr->can_family = AF_CAN;
229 addr->can_ifindex = skb->dev->ifindex;
231 if (sock_queue_rcv_skb(sk, skb) < 0)
235 static int check_pad(struct isotp_sock *so, struct can_frame *cf,
236 int start_index, __u8 content)
240 /* check datalength code */
241 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && cf->can_dlc != 8)
244 /* check padding content */
245 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
246 for (i = start_index; i < 8; i++)
247 if (cf->data[i] != content)
253 static int isotp_rcv_fc(struct isotp_sock *so, struct can_frame *cf, int ae)
255 if (so->tx.state != ISOTP_WAIT_FC &&
256 so->tx.state != ISOTP_WAIT_FIRST_FC)
259 hrtimer_cancel(&so->txtimer);
261 if ((so->opt.flags & CAN_ISOTP_TX_PADDING) &&
262 check_pad(so, cf, ae+3, so->opt.txpad_content)) {
263 so->tx.state = ISOTP_IDLE;
264 wake_up_interruptible(&so->wait);
268 /* get communication parameters only from the first FC frame */
269 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
271 so->txfc.bs = cf->data[ae+1];
272 so->txfc.stmin = cf->data[ae+2];
274 /* fix wrong STmin values according spec */
275 if ((so->txfc.stmin > 0x7F) &&
276 ((so->txfc.stmin < 0xF1) || (so->txfc.stmin > 0xF9)))
277 so->txfc.stmin = 0x7F;
279 so->tx_gap = ktime_set(0,0);
280 /* add transmission time for CAN frame N_As */
281 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
282 /* add waiting time for consecutive frames N_Cs */
283 if (so->txfc.stmin < 0x80)
284 so->tx_gap = ktime_add_ns(so->tx_gap,
285 so->txfc.stmin * 1000000);
287 so->tx_gap = ktime_add_ns(so->tx_gap,
288 (so->txfc.stmin - 0xF0)
290 so->tx.state = ISOTP_WAIT_FC;
293 DBG("FC frame: FS %d, BS %d, STmin 0x%02X, tx_gap %lld\n",
294 cf->data[ae] & 0x0F & 0x0F, so->txfc.bs, so->txfc.stmin,
295 (long long)so->tx_gap.tv64);
297 switch (cf->data[ae] & 0x0F) {
301 so->tx.state = ISOTP_SENDING;
302 DBG("starting txtimer for sending\n");
303 /* start cyclic timer for sending CF frame */
304 hrtimer_start(&so->txtimer, so->tx_gap,
309 DBG("starting waiting for next FC\n");
310 /* start timer to wait for next FC frame */
311 hrtimer_start(&so->txtimer, ktime_set(1,0),
316 DBG("overflow in receiver side\n");
319 /* stop this tx job. TODO: error reporting? */
320 so->tx.state = ISOTP_IDLE;
321 wake_up_interruptible(&so->wait);
326 static int isotp_rcv_sf(struct sock *sk, struct can_frame *cf, int ae,
329 struct isotp_sock *so = isotp_sk(sk);
330 int len = cf->data[ae] & 0x0F;
331 struct sk_buff *nskb;
333 hrtimer_cancel(&so->rxtimer);
334 so->rx.state = ISOTP_IDLE;
336 if (!len || len > 7 || (ae && len > 6))
339 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
340 check_pad(so, cf, 1+ae+len, so->opt.rxpad_content))
343 nskb = alloc_skb(len, gfp_any());
347 memcpy(skb_put(nskb, len), &cf->data[1+ae], len);
349 nskb->tstamp = skb->tstamp;
350 nskb->dev = skb->dev;
351 isotp_rcv_skb(nskb, sk);
355 static int isotp_rcv_ff(struct sock *sk, struct can_frame *cf, int ae)
357 struct isotp_sock *so = isotp_sk(sk);
360 hrtimer_cancel(&so->rxtimer);
361 so->rx.state = ISOTP_IDLE;
363 if (cf->can_dlc != 8)
366 so->rx.len = (cf->data[ae] & 0x0F) << 8;
367 so->rx.len += cf->data[ae+1];
369 if (so->rx.len + ae < 8)
372 /* copy the first received data bytes */
374 for (i = ae+2; i < 8; i++)
375 so->rx.buf[so->rx.idx++] = cf->data[i];
377 /* initial setup for this pdu receiption */
379 so->rx.state = ISOTP_WAIT_DATA;
381 /* no creation of flow control frames */
382 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
385 /* send our first FC frame */
386 isotp_send_fc(sk, ae);
390 static int isotp_rcv_cf(struct sock *sk, struct can_frame *cf, int ae,
393 struct isotp_sock *so = isotp_sk(sk);
394 struct sk_buff *nskb;
397 if (so->rx.state != ISOTP_WAIT_DATA)
400 hrtimer_cancel(&so->rxtimer);
402 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
403 DBG("wrong sn %d. expected %d.\n",
404 cf->data[ae] & 0x0F, so->rx.sn);
405 /* some error reporting? */
406 so->rx.state = ISOTP_IDLE;
412 for (i = ae+1; i < 8; i++) {
413 so->rx.buf[so->rx.idx++] = cf->data[i];
414 if (so->rx.idx >= so->rx.len)
418 if (so->rx.idx >= so->rx.len) {
421 so->rx.state = ISOTP_IDLE;
423 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
424 check_pad(so, cf, i+1, so->opt.rxpad_content))
427 nskb = alloc_skb(so->rx.len, gfp_any());
431 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
434 nskb->tstamp = skb->tstamp;
435 nskb->dev = skb->dev;
436 isotp_rcv_skb(nskb, sk);
440 /* no creation of flow control frames */
441 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
444 /* perform blocksize handling, if enabled */
445 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
447 /* start rx timeout watchdog */
448 hrtimer_start(&so->rxtimer, ktime_set(1,0),
453 /* we reached the specified blocksize so->rxfc.bs */
454 isotp_send_fc(sk, ae);
458 static void isotp_rcv(struct sk_buff *skb, void *data)
460 struct sock *sk = (struct sock *)data;
461 struct isotp_sock *so = isotp_sk(sk);
462 struct can_frame *cf;
463 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
466 /* read CAN frame and free skbuff */
467 BUG_ON(skb->len != sizeof(struct can_frame));
468 cf = (struct can_frame *) skb->data;
470 /* if enabled: check receiption of my configured extended address */
471 if (ae && cf->data[0] != so->opt.ext_address)
474 n_pci_type = cf->data[ae] & 0xF0;
476 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
477 /* check rx/tx path half duplex expectations */
478 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
479 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
483 switch (n_pci_type) {
485 /* tx path: flow control frame containing the FC parameters */
486 isotp_rcv_fc(so, cf, ae);
490 /* rx path: single frame */
491 isotp_rcv_sf(sk, cf, ae, skb);
495 /* rx path: first frame */
496 isotp_rcv_ff(sk, cf, ae);
500 /* rx path: consecutive frame */
501 isotp_rcv_cf(sk, cf, ae, skb);
506 static void isotp_fill_dataframe(struct can_frame *cf, struct isotp_sock *so,
509 unsigned char space = 7 - ae;
510 int num = min_t(int, so->tx.len - so->tx.idx, space);
513 cf->can_id = so->txid;
515 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
517 memset(cf->data, so->opt.txpad_content, 8);
521 cf->can_dlc = num + 1 + ae;
524 for (i = 0; i < num; i++)
525 cf->data[i+ae+1] = so->tx.buf[so->tx.idx++];
528 cf->data[0] = so->opt.ext_address;
531 static void isotp_create_fframe(struct can_frame *cf, struct isotp_sock *so,
536 cf->can_id = so->txid;
539 cf->data[0] = so->opt.ext_address;
541 /* N_PCI bytes with FF_DL data length */
542 cf->data[ae] = (u8) (so->tx.len>>8) | N_PCI_FF;
543 cf->data[ae+1] = (u8) so->tx.len & 0xFFU;
545 /* add first 5 or 6 data bytes depending on ae */
546 for (i = ae+2; i < 8; i++)
547 cf->data[i] = so->tx.buf[so->tx.idx++];
550 so->tx.state = ISOTP_WAIT_FIRST_FC;
553 static void isotp_tx_timer_tsklet(unsigned long data)
555 struct isotp_sock *so = (struct isotp_sock *)data;
556 struct sock *sk = &so->sk;
558 struct net_device *dev;
559 struct can_frame *cf;
560 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
562 switch (so->tx.state) {
565 case ISOTP_WAIT_FIRST_FC:
567 /* we did not get any flow control frame in time */
569 DBG("we did not get FC frame in time.\n");
572 /* report 'communication error on send' */
574 if (!sock_flag(sk, SOCK_DEAD))
575 sk->sk_error_report(sk);
578 so->tx.state = ISOTP_IDLE;
579 wake_up_interruptible(&so->wait);
584 /* push out the next segmented pdu */
586 DBG("next pdu to send.\n");
588 dev = dev_get_by_index(&init_net, so->ifindex);
593 skb = alloc_skb(sizeof(*cf), gfp_any());
599 cf = (struct can_frame *)skb->data;
600 skb_put(skb, sizeof(*cf));
602 /* create consecutive frame */
603 isotp_fill_dataframe(cf, so, ae);
605 /* place consecutive frame N_PCI in appropriate index */
606 cf->data[ae] = N_PCI_CF | so->tx.sn++;
614 if (so->tx.idx >= so->tx.len) {
616 DBG("we are done\n");
617 so->tx.state = ISOTP_IDLE;
619 wake_up_interruptible(&so->wait);
623 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
624 /* stop and wait for FC */
625 DBG("BS stop and wait for FC\n");
626 so->tx.state = ISOTP_WAIT_FC;
628 hrtimer_start(&so->txtimer,
629 ktime_add(ktime_get(), ktime_set(1,0)),
634 /* no gap between data frames needed => use burst mode */
635 if (!so->tx_gap.tv64)
638 /* start timer to send next data frame with correct delay */
640 hrtimer_start(&so->txtimer,
641 ktime_add(ktime_get(), so->tx_gap),
650 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
652 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
654 tasklet_schedule(&so->txtsklet);
656 return HRTIMER_NORESTART;
659 static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
660 struct msghdr *msg, size_t size)
662 struct sock *sk = sock->sk;
663 struct isotp_sock *so = isotp_sk(sk);
665 struct net_device *dev;
666 struct can_frame *cf;
667 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
671 return -EADDRNOTAVAIL;
673 /* we do not support multiple buffers - for now */
674 if (so->tx.state != ISOTP_IDLE) {
675 if (msg->msg_flags & MSG_DONTWAIT)
678 /* wait for complete transmission of current pdu */
679 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
682 if (!size || size > 4095)
685 err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
689 dev = dev_get_by_index(&init_net, so->ifindex);
693 skb = sock_alloc_send_skb(sk, sizeof(*cf),
694 msg->msg_flags & MSG_DONTWAIT, &err);
700 so->tx.state = ISOTP_SENDING;
704 cf = (struct can_frame *)skb->data;
705 skb_put(skb, sizeof(*cf));
707 /* check for single frame transmission */
708 if (size <= 7 - ae) {
710 isotp_fill_dataframe(cf, so, ae);
712 /* place single frame N_PCI in appropriate index */
713 cf->data[ae] = size | N_PCI_SF;
715 so->tx.state = ISOTP_IDLE;
716 wake_up_interruptible(&so->wait);
718 /* send first frame and wait for FC */
720 isotp_create_fframe(cf, so, ae);
722 DBG("starting txtimer for fc\n");
723 /* start timeout for FC */
724 hrtimer_start(&so->txtimer, ktime_set(1,0), HRTIMER_MODE_REL);
727 /* send the first or only CAN frame */
730 err = can_send(skb, 1);
738 static int isotp_recvmsg(struct kiocb *iocb, struct socket *sock,
739 struct msghdr *msg, size_t size, int flags)
741 struct sock *sk = sock->sk;
746 noblock = flags & MSG_DONTWAIT;
747 flags &= ~MSG_DONTWAIT;
749 skb = skb_recv_datagram(sk, flags, noblock, &err);
754 msg->msg_flags |= MSG_TRUNC;
758 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
760 skb_free_datagram(sk, skb);
764 sock_recv_timestamp(msg, sk, skb);
767 msg->msg_namelen = sizeof(struct sockaddr_can);
768 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
771 skb_free_datagram(sk, skb);
776 static int isotp_release(struct socket *sock)
778 struct sock *sk = sock->sk;
779 struct isotp_sock *so = isotp_sk(sk);
781 /* wait for complete transmission of current pdu */
782 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
784 unregister_netdevice_notifier(&so->notifier);
788 hrtimer_cancel(&so->txtimer);
789 hrtimer_cancel(&so->rxtimer);
790 tasklet_kill(&so->txtsklet);
792 /* remove current filters & unregister */
795 struct net_device *dev;
797 dev = dev_get_by_index(&init_net, so->ifindex);
799 can_rx_unregister(dev, so->rxid,
800 SINGLE_MASK(so->rxid),
819 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
821 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
822 struct sock *sk = sock->sk;
823 struct isotp_sock *so = isotp_sk(sk);
825 struct net_device *dev;
827 int notify_enetdown = 0;
829 if (len < sizeof(*addr))
832 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
833 return -EADDRNOTAVAIL;
835 if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) &
836 (CAN_ERR_FLAG | CAN_RTR_FLAG))
837 return -EADDRNOTAVAIL;
839 if (!addr->can_ifindex)
844 if (so->bound && addr->can_ifindex == so->ifindex &&
845 addr->can_addr.tp.rx_id == so->rxid &&
846 addr->can_addr.tp.tx_id == so->txid)
849 dev = dev_get_by_index(&init_net, addr->can_ifindex);
854 if (dev->type != ARPHRD_CAN) {
859 if (!(dev->flags & IFF_UP))
862 ifindex = dev->ifindex;
864 can_rx_register(dev, addr->can_addr.tp.rx_id,
865 SINGLE_MASK(addr->can_addr.tp.rx_id),
866 isotp_rcv, sk, "isotp");
870 /* unregister old filter */
872 dev = dev_get_by_index(&init_net, so->ifindex);
874 can_rx_unregister(dev, so->rxid,
875 SINGLE_MASK(so->rxid),
882 /* switch to new settings */
883 so->ifindex = ifindex;
884 so->rxid = addr->can_addr.tp.rx_id;
885 so->txid = addr->can_addr.tp.tx_id;
891 if (notify_enetdown) {
892 sk->sk_err = ENETDOWN;
893 if (!sock_flag(sk, SOCK_DEAD))
894 sk->sk_error_report(sk);
900 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr,
903 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
904 struct sock *sk = sock->sk;
905 struct isotp_sock *so = isotp_sk(sk);
910 addr->can_family = AF_CAN;
911 addr->can_ifindex = so->ifindex;
913 *len = sizeof(*addr);
918 static int isotp_setsockopt(struct socket *sock, int level, int optname,
919 char __user *optval, int optlen)
921 struct sock *sk = sock->sk;
922 struct isotp_sock *so = isotp_sk(sk);
925 if (level != SOL_CAN_ISOTP)
933 if (optlen != sizeof(struct can_isotp_options))
936 if (copy_from_user(&so->opt, optval, optlen))
940 case CAN_ISOTP_RECV_FC:
941 if (optlen != sizeof(struct can_isotp_fc_options))
944 if (copy_from_user(&so->rxfc, optval, optlen))
955 static int isotp_getsockopt(struct socket *sock, int level, int optname,
956 char __user *optval, int __user *optlen)
958 struct sock *sk = sock->sk;
959 struct isotp_sock *so = isotp_sk(sk);
963 if (level != SOL_CAN_ISOTP)
965 if (get_user(len, optlen))
973 len = min_t(int, len, sizeof(struct can_isotp_options));
977 case CAN_ISOTP_RECV_FC:
978 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
986 if (put_user(len, optlen))
988 if (copy_to_user(optval, val, len))
994 static int isotp_notifier(struct notifier_block *nb,
995 unsigned long msg, void *data)
997 struct net_device *dev = (struct net_device *)data;
998 struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
999 struct sock *sk = &so->sk;
1001 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1002 if (dev_net(dev) != &init_net)
1004 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1005 if (dev->nd_net != &init_net)
1009 if (dev->type != ARPHRD_CAN)
1012 if (so->ifindex != dev->ifindex)
1017 case NETDEV_UNREGISTER:
1019 /* remove current filters & unregister */
1021 can_rx_unregister(dev, so->rxid, SINGLE_MASK(so->rxid),
1028 sk->sk_err = ENODEV;
1029 if (!sock_flag(sk, SOCK_DEAD))
1030 sk->sk_error_report(sk);
1034 sk->sk_err = ENETDOWN;
1035 if (!sock_flag(sk, SOCK_DEAD))
1036 sk->sk_error_report(sk);
1044 static int isotp_init(struct sock *sk)
1046 struct isotp_sock *so = isotp_sk(sk);
1051 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1052 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1053 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_RXPAD_CONTENT;
1054 so->opt.txpad_content = CAN_ISOTP_DEFAULT_TXPAD_CONTENT;
1055 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1056 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1057 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1058 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1060 so->rx.state = ISOTP_IDLE;
1061 so->tx.state = ISOTP_IDLE;
1063 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1064 so->rxtimer.function = isotp_rx_timer_handler;
1065 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1066 so->txtimer.function = isotp_tx_timer_handler;
1068 tasklet_init(&so->txtsklet, isotp_tx_timer_tsklet, (unsigned long)so);
1070 init_waitqueue_head(&so->wait);
1072 so->notifier.notifier_call = isotp_notifier;
1073 register_netdevice_notifier(&so->notifier);
1079 static struct proto_ops isotp_ops __read_mostly = {
1081 .release = isotp_release,
1083 .connect = sock_no_connect,
1084 .socketpair = sock_no_socketpair,
1085 .accept = sock_no_accept,
1086 .getname = isotp_getname,
1087 .poll = datagram_poll,
1088 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1089 .listen = sock_no_listen,
1090 .shutdown = sock_no_shutdown,
1091 .setsockopt = isotp_setsockopt,
1092 .getsockopt = isotp_getsockopt,
1093 .sendmsg = isotp_sendmsg,
1094 .recvmsg = isotp_recvmsg,
1095 .mmap = sock_no_mmap,
1096 .sendpage = sock_no_sendpage,
1099 static struct proto isotp_proto __read_mostly = {
1100 .name = "CAN_ISOTP",
1101 .owner = THIS_MODULE,
1102 .obj_size = sizeof(struct isotp_sock),
1106 static struct can_proto isotp_can_proto __read_mostly = {
1108 .protocol = CAN_ISOTP,
1111 .prot = &isotp_proto,
1114 static __init int isotp_module_init(void)
1120 err = can_proto_register(&isotp_can_proto);
1122 printk(KERN_ERR "can: registration of isotp protocol failed\n");
1127 static __exit void isotp_module_exit(void)
1129 can_proto_unregister(&isotp_can_proto);
1132 module_init(isotp_module_init);
1133 module_exit(isotp_module_exit);