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>");
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
87 #error This modules needs hrtimers (available since Kernel 2.6.22)
90 #define DBG(fmt, args...) (printk( KERN_DEBUG "can-isotp: %s: " fmt, \
93 #define DBG(fmt, args...)
95 #define SINGLE_MASK(id) ((id & CAN_EFF_FLAG) ? \
96 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
97 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
99 /* N_PCI type values in bits 7-4 of N_PCI bytes */
100 #define N_PCI_SF 0x00 /* single frame */
101 #define N_PCI_FF 0x10 /* first frame */
102 #define N_PCI_CF 0x20 /* consecutive frame */
103 #define N_PCI_FC 0x30 /* flow control */
105 /* Flow Status given in FC frame */
106 #define ISOTP_FC_CTS 0 /* clear to send */
107 #define ISOTP_FC_WT 1 /* wait */
108 #define ISOTP_FC_OVFLW 2 /* overflow */
134 struct hrtimer rxtimer, txtimer;
135 struct tasklet_struct txtsklet;
136 struct can_isotp_options opt;
137 struct can_isotp_fc_options rxfc, txfc;
139 struct notifier_block notifier;
140 wait_queue_head_t wait;
143 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
145 return (struct isotp_sock *)sk;
148 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
150 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
152 if (so->rx.state == ISOTP_WAIT_DATA) {
154 struct sock *sk = &so->sk;
156 /* report 'timeout' */
158 if (!sock_flag(sk, SOCK_DEAD))
159 sk->sk_error_report(sk);
161 DBG("we did not get new data frames in time.\n");
164 so->rx.state = ISOTP_IDLE;
167 return HRTIMER_NORESTART;
170 static int isotp_send_fc(struct sock *sk, int ae)
172 struct net_device *dev;
173 struct sk_buff *nskb;
174 struct can_frame *ncf;
175 struct isotp_sock *so = isotp_sk(sk);
177 nskb = alloc_skb(sizeof(struct can_frame), gfp_any());
181 dev = dev_get_by_index(&init_net, so->ifindex);
188 ncf = (struct can_frame *) nskb->data;
189 skb_put(nskb, sizeof(struct can_frame));
191 /* create & send flow control reply */
192 ncf->can_id = so->txid;
194 if (so->opt.flags & CAN_ISOTP_RX_PADDING) {
195 memset(ncf->data, so->opt.rxpad_content, 8);
200 ncf->data[ae] = N_PCI_FC | ISOTP_FC_CTS;
201 ncf->data[ae+1] = so->rxfc.bs;
202 ncf->data[ae+2] = so->rxfc.stmin;
205 ncf->data[0] = so->opt.ext_address;
210 /* reset blocksize counter */
213 /* start rx timeout watchdog */
214 hrtimer_start(&so->rxtimer, ktime_set(1,0), HRTIMER_MODE_REL);
218 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
220 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
222 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
226 memset(addr, 0, sizeof(*addr));
227 addr->can_family = AF_CAN;
228 addr->can_ifindex = skb->dev->ifindex;
230 if (sock_queue_rcv_skb(sk, skb) < 0)
234 static int check_pad(struct isotp_sock *so, struct can_frame *cf,
235 int start_index, __u8 content)
239 /* check datalength code */
240 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && cf->can_dlc != 8)
243 /* check padding content */
244 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
245 for (i = start_index; i < 8; i++)
246 if (cf->data[i] != content)
252 static int isotp_rcv_fc(struct isotp_sock *so, struct can_frame *cf, int ae)
254 if (so->tx.state != ISOTP_WAIT_FC &&
255 so->tx.state != ISOTP_WAIT_FIRST_FC)
258 hrtimer_cancel(&so->txtimer);
260 if ((so->opt.flags & CAN_ISOTP_TX_PADDING) &&
261 check_pad(so, cf, ae+3, so->opt.txpad_content)) {
262 so->tx.state = ISOTP_IDLE;
263 wake_up_interruptible(&so->wait);
267 /* get communication parameters only from the first FC frame */
268 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
270 so->txfc.bs = cf->data[ae+1];
271 so->txfc.stmin = cf->data[ae+2];
273 /* fix wrong STmin values according spec */
274 if ((so->txfc.stmin > 0x7F) &&
275 ((so->txfc.stmin < 0xF1) || (so->txfc.stmin > 0xF9)))
276 so->txfc.stmin = 0x7F;
278 so->tx_gap = ktime_set(0,0);
279 /* add transmission time for CAN frame N_As */
280 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
281 /* add waiting time for consecutive frames N_Cs */
282 if (so->txfc.stmin < 0x80)
283 so->tx_gap = ktime_add_ns(so->tx_gap,
284 so->txfc.stmin * 1000000);
286 so->tx_gap = ktime_add_ns(so->tx_gap,
287 (so->txfc.stmin - 0xF0)
289 so->tx.state = ISOTP_WAIT_FC;
292 DBG("FC frame: FS %d, BS %d, STmin 0x%02X, tx_gap %lld\n",
293 cf->data[ae] & 0x0F & 0x0F, so->txfc.bs, so->txfc.stmin,
294 (long long)so->tx_gap.tv64);
296 switch (cf->data[ae] & 0x0F) {
300 so->tx.state = ISOTP_SENDING;
301 DBG("starting txtimer for sending\n");
302 /* start cyclic timer for sending CF frame */
303 hrtimer_start(&so->txtimer, so->tx_gap,
308 DBG("starting waiting for next FC\n");
309 /* start timer to wait for next FC frame */
310 hrtimer_start(&so->txtimer, ktime_set(1,0),
315 DBG("overflow in receiver side\n");
318 /* stop this tx job. TODO: error reporting? */
319 so->tx.state = ISOTP_IDLE;
320 wake_up_interruptible(&so->wait);
325 static int isotp_rcv_sf(struct sock *sk, struct can_frame *cf, int ae,
328 struct isotp_sock *so = isotp_sk(sk);
329 int len = cf->data[ae] & 0x0F;
330 struct sk_buff *nskb;
332 hrtimer_cancel(&so->rxtimer);
333 so->rx.state = ISOTP_IDLE;
335 if (!len || len > 7 || (ae && len > 6))
338 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
339 check_pad(so, cf, 1+ae+len, so->opt.rxpad_content))
342 nskb = alloc_skb(len, gfp_any());
346 memcpy(skb_put(nskb, len), &cf->data[1+ae], len);
348 nskb->tstamp = skb->tstamp;
349 nskb->dev = skb->dev;
350 isotp_rcv_skb(nskb, sk);
354 static int isotp_rcv_ff(struct sock *sk, struct can_frame *cf, int ae)
356 struct isotp_sock *so = isotp_sk(sk);
359 hrtimer_cancel(&so->rxtimer);
360 so->rx.state = ISOTP_IDLE;
362 if (cf->can_dlc != 8)
365 so->rx.len = (cf->data[ae] & 0x0F) << 8;
366 so->rx.len += cf->data[ae+1];
368 if (so->rx.len + ae < 8)
371 /* copy the first received data bytes */
373 for (i = ae+2; i < 8; i++)
374 so->rx.buf[so->rx.idx++] = cf->data[i];
376 /* initial setup for this pdu receiption */
378 so->rx.state = ISOTP_WAIT_DATA;
380 /* no creation of flow control frames */
381 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
384 /* send our first FC frame */
385 isotp_send_fc(sk, ae);
389 static int isotp_rcv_cf(struct sock *sk, struct can_frame *cf, int ae,
392 struct isotp_sock *so = isotp_sk(sk);
393 struct sk_buff *nskb;
396 if (so->rx.state != ISOTP_WAIT_DATA)
399 hrtimer_cancel(&so->rxtimer);
401 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
402 DBG("wrong sn %d. expected %d.\n",
403 cf->data[ae] & 0x0F, so->rx.sn);
404 /* some error reporting? */
405 so->rx.state = ISOTP_IDLE;
411 for (i = ae+1; i < 8; i++) {
412 so->rx.buf[so->rx.idx++] = cf->data[i];
413 if (so->rx.idx >= so->rx.len)
417 if (so->rx.idx >= so->rx.len) {
420 so->rx.state = ISOTP_IDLE;
422 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
423 check_pad(so, cf, i+1, so->opt.rxpad_content))
426 nskb = alloc_skb(so->rx.len, gfp_any());
430 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
433 nskb->tstamp = skb->tstamp;
434 nskb->dev = skb->dev;
435 isotp_rcv_skb(nskb, sk);
439 /* no creation of flow control frames */
440 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
443 /* perform blocksize handling, if enabled */
444 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
446 /* start rx timeout watchdog */
447 hrtimer_start(&so->rxtimer, ktime_set(1,0),
452 /* we reached the specified blocksize so->rxfc.bs */
453 isotp_send_fc(sk, ae);
457 static void isotp_rcv(struct sk_buff *skb, void *data)
459 struct sock *sk = (struct sock *)data;
460 struct isotp_sock *so = isotp_sk(sk);
461 struct can_frame *cf;
462 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
465 /* read CAN frame and free skbuff */
466 BUG_ON(skb->len != sizeof(struct can_frame));
467 cf = (struct can_frame *) skb->data;
469 /* if enabled: check receiption of my configured extended address */
470 if (ae && cf->data[0] != so->opt.ext_address)
473 n_pci_type = cf->data[ae] & 0xF0;
475 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
476 /* check rx/tx path half duplex expectations */
477 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
478 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
482 switch (n_pci_type) {
484 /* tx path: flow control frame containing the FC parameters */
485 isotp_rcv_fc(so, cf, ae);
489 /* rx path: single frame */
490 isotp_rcv_sf(sk, cf, ae, skb);
494 /* rx path: first frame */
495 isotp_rcv_ff(sk, cf, ae);
499 /* rx path: consecutive frame */
500 isotp_rcv_cf(sk, cf, ae, skb);
505 static void isotp_fill_dataframe(struct can_frame *cf, struct isotp_sock *so,
508 unsigned char space = 7 - ae;
509 int num = min_t(int, so->tx.len - so->tx.idx, space);
512 cf->can_id = so->txid;
514 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
516 memset(cf->data, so->opt.txpad_content, 8);
520 cf->can_dlc = num + 1 + ae;
523 for (i = 0; i < num; i++)
524 cf->data[i+ae+1] = so->tx.buf[so->tx.idx++];
527 cf->data[0] = so->opt.ext_address;
530 static void isotp_create_fframe(struct can_frame *cf, struct isotp_sock *so,
535 cf->can_id = so->txid;
538 cf->data[0] = so->opt.ext_address;
540 /* N_PCI bytes with FF_DL data length */
541 cf->data[ae] = (u8) (so->tx.len>>8) | N_PCI_FF;
542 cf->data[ae+1] = (u8) so->tx.len & 0xFFU;
544 /* add first 5 or 6 data bytes depending on ae */
545 for (i = ae+2; i < 8; i++)
546 cf->data[i] = so->tx.buf[so->tx.idx++];
549 so->tx.state = ISOTP_WAIT_FIRST_FC;
552 static void isotp_tx_timer_tsklet(unsigned long data)
554 struct isotp_sock *so = (struct isotp_sock *)data;
555 struct sock *sk = &so->sk;
557 struct net_device *dev;
558 struct can_frame *cf;
559 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
561 switch (so->tx.state) {
564 case ISOTP_WAIT_FIRST_FC:
566 /* we did not get any flow control frame in time */
568 DBG("we did not get FC frame in time.\n");
571 /* report 'communication error on send' */
573 if (!sock_flag(sk, SOCK_DEAD))
574 sk->sk_error_report(sk);
577 so->tx.state = ISOTP_IDLE;
578 wake_up_interruptible(&so->wait);
583 /* push out the next segmented pdu */
585 DBG("next pdu to send.\n");
587 dev = dev_get_by_index(&init_net, so->ifindex);
592 skb = alloc_skb(sizeof(*cf), gfp_any());
598 cf = (struct can_frame *)skb->data;
599 skb_put(skb, sizeof(*cf));
601 /* create consecutive frame */
602 isotp_fill_dataframe(cf, so, ae);
604 /* place consecutive frame N_PCI in appropriate index */
605 cf->data[ae] = N_PCI_CF | so->tx.sn++;
613 if (so->tx.idx >= so->tx.len) {
615 DBG("we are done\n");
616 so->tx.state = ISOTP_IDLE;
618 wake_up_interruptible(&so->wait);
622 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
623 /* stop and wait for FC */
624 DBG("BS stop and wait for FC\n");
625 so->tx.state = ISOTP_WAIT_FC;
627 hrtimer_start(&so->txtimer,
628 ktime_add(ktime_get(), ktime_set(1,0)),
633 /* no gap between data frames needed => use burst mode */
634 if (!so->tx_gap.tv64)
637 /* start timer to send next data frame with correct delay */
639 hrtimer_start(&so->txtimer,
640 ktime_add(ktime_get(), so->tx_gap),
649 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
651 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
653 tasklet_schedule(&so->txtsklet);
655 return HRTIMER_NORESTART;
658 static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
659 struct msghdr *msg, size_t size)
661 struct sock *sk = sock->sk;
662 struct isotp_sock *so = isotp_sk(sk);
664 struct net_device *dev;
665 struct can_frame *cf;
666 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
670 return -EADDRNOTAVAIL;
672 /* we do not support multiple buffers - for now */
673 if (so->tx.state != ISOTP_IDLE) {
674 if (msg->msg_flags & MSG_DONTWAIT)
677 /* wait for complete transmission of current pdu */
678 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
681 if (!size || size > 4095)
684 err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
688 dev = dev_get_by_index(&init_net, so->ifindex);
692 skb = sock_alloc_send_skb(sk, sizeof(*cf),
693 msg->msg_flags & MSG_DONTWAIT, &err);
699 so->tx.state = ISOTP_SENDING;
703 cf = (struct can_frame *)skb->data;
704 skb_put(skb, sizeof(*cf));
706 /* check for single frame transmission */
707 if (size <= 7 - ae) {
709 isotp_fill_dataframe(cf, so, ae);
711 /* place single frame N_PCI in appropriate index */
712 cf->data[ae] = size | N_PCI_SF;
714 so->tx.state = ISOTP_IDLE;
715 wake_up_interruptible(&so->wait);
717 /* send first frame and wait for FC */
719 isotp_create_fframe(cf, so, ae);
721 DBG("starting txtimer for fc\n");
722 /* start timeout for FC */
723 hrtimer_start(&so->txtimer, ktime_set(1,0), HRTIMER_MODE_REL);
726 /* send the first or only CAN frame */
729 err = can_send(skb, 1);
737 static int isotp_recvmsg(struct kiocb *iocb, struct socket *sock,
738 struct msghdr *msg, size_t size, int flags)
740 struct sock *sk = sock->sk;
745 noblock = flags & MSG_DONTWAIT;
746 flags &= ~MSG_DONTWAIT;
748 skb = skb_recv_datagram(sk, flags, noblock, &err);
753 msg->msg_flags |= MSG_TRUNC;
757 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
759 skb_free_datagram(sk, skb);
763 sock_recv_timestamp(msg, sk, skb);
766 msg->msg_namelen = sizeof(struct sockaddr_can);
767 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
770 skb_free_datagram(sk, skb);
775 static int isotp_release(struct socket *sock)
777 struct sock *sk = sock->sk;
778 struct isotp_sock *so = isotp_sk(sk);
780 /* wait for complete transmission of current pdu */
781 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
783 unregister_netdevice_notifier(&so->notifier);
787 hrtimer_cancel(&so->txtimer);
788 hrtimer_cancel(&so->rxtimer);
789 tasklet_kill(&so->txtsklet);
791 /* remove current filters & unregister */
794 struct net_device *dev;
796 dev = dev_get_by_index(&init_net, so->ifindex);
798 can_rx_unregister(dev, so->rxid,
799 SINGLE_MASK(so->rxid),
815 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
817 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
818 struct sock *sk = sock->sk;
819 struct isotp_sock *so = isotp_sk(sk);
821 struct net_device *dev;
823 int notify_enetdown = 0;
825 if (len < sizeof(*addr))
828 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
829 return -EADDRNOTAVAIL;
831 if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) &
832 (CAN_ERR_FLAG | CAN_RTR_FLAG))
833 return -EADDRNOTAVAIL;
835 if (!addr->can_ifindex)
840 if (so->bound && addr->can_ifindex == so->ifindex &&
841 addr->can_addr.tp.rx_id == so->rxid &&
842 addr->can_addr.tp.tx_id == so->txid)
845 dev = dev_get_by_index(&init_net, addr->can_ifindex);
850 if (dev->type != ARPHRD_CAN) {
855 if (!(dev->flags & IFF_UP))
858 ifindex = dev->ifindex;
860 can_rx_register(dev, addr->can_addr.tp.rx_id,
861 SINGLE_MASK(addr->can_addr.tp.rx_id),
862 isotp_rcv, sk, "isotp");
866 /* unregister old filter */
868 dev = dev_get_by_index(&init_net, so->ifindex);
870 can_rx_unregister(dev, so->rxid,
871 SINGLE_MASK(so->rxid),
878 /* switch to new settings */
879 so->ifindex = ifindex;
880 so->rxid = addr->can_addr.tp.rx_id;
881 so->txid = addr->can_addr.tp.tx_id;
887 if (notify_enetdown) {
888 sk->sk_err = ENETDOWN;
889 if (!sock_flag(sk, SOCK_DEAD))
890 sk->sk_error_report(sk);
896 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr,
899 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
900 struct sock *sk = sock->sk;
901 struct isotp_sock *so = isotp_sk(sk);
906 addr->can_family = AF_CAN;
907 addr->can_ifindex = so->ifindex;
909 *len = sizeof(*addr);
914 static int isotp_setsockopt(struct socket *sock, int level, int optname,
915 char __user *optval, int optlen)
917 struct sock *sk = sock->sk;
918 struct isotp_sock *so = isotp_sk(sk);
921 if (level != SOL_CAN_ISOTP)
929 if (optlen != sizeof(struct can_isotp_options))
932 if (copy_from_user(&so->opt, optval, optlen))
936 case CAN_ISOTP_RECV_FC:
937 if (optlen != sizeof(struct can_isotp_fc_options))
940 if (copy_from_user(&so->rxfc, optval, optlen))
951 static int isotp_getsockopt(struct socket *sock, int level, int optname,
952 char __user *optval, int __user *optlen)
954 struct sock *sk = sock->sk;
955 struct isotp_sock *so = isotp_sk(sk);
959 if (level != SOL_CAN_ISOTP)
961 if (get_user(len, optlen))
969 len = min_t(int, len, sizeof(struct can_isotp_options));
973 case CAN_ISOTP_RECV_FC:
974 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
982 if (put_user(len, optlen))
984 if (copy_to_user(optval, val, len))
990 static int isotp_notifier(struct notifier_block *nb,
991 unsigned long msg, void *data)
993 struct net_device *dev = (struct net_device *)data;
994 struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
995 struct sock *sk = &so->sk;
997 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
998 if (dev_net(dev) != &init_net)
1000 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1001 if (dev->nd_net != &init_net)
1005 if (dev->type != ARPHRD_CAN)
1008 if (so->ifindex != dev->ifindex)
1013 case NETDEV_UNREGISTER:
1015 /* remove current filters & unregister */
1017 can_rx_unregister(dev, so->rxid, SINGLE_MASK(so->rxid),
1024 sk->sk_err = ENODEV;
1025 if (!sock_flag(sk, SOCK_DEAD))
1026 sk->sk_error_report(sk);
1030 sk->sk_err = ENETDOWN;
1031 if (!sock_flag(sk, SOCK_DEAD))
1032 sk->sk_error_report(sk);
1040 static int isotp_init(struct sock *sk)
1042 struct isotp_sock *so = isotp_sk(sk);
1047 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1048 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1049 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_RXPAD_CONTENT;
1050 so->opt.txpad_content = CAN_ISOTP_DEFAULT_TXPAD_CONTENT;
1051 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1052 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1053 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1054 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1056 so->rx.state = ISOTP_IDLE;
1057 so->tx.state = ISOTP_IDLE;
1059 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1060 so->rxtimer.function = isotp_rx_timer_handler;
1061 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1062 so->txtimer.function = isotp_tx_timer_handler;
1064 tasklet_init(&so->txtsklet, isotp_tx_timer_tsklet, (unsigned long)so);
1066 init_waitqueue_head(&so->wait);
1068 so->notifier.notifier_call = isotp_notifier;
1069 register_netdevice_notifier(&so->notifier);
1075 static struct proto_ops isotp_ops __read_mostly = {
1077 .release = isotp_release,
1079 .connect = sock_no_connect,
1080 .socketpair = sock_no_socketpair,
1081 .accept = sock_no_accept,
1082 .getname = isotp_getname,
1083 .poll = datagram_poll,
1084 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1085 .listen = sock_no_listen,
1086 .shutdown = sock_no_shutdown,
1087 .setsockopt = isotp_setsockopt,
1088 .getsockopt = isotp_getsockopt,
1089 .sendmsg = isotp_sendmsg,
1090 .recvmsg = isotp_recvmsg,
1091 .mmap = sock_no_mmap,
1092 .sendpage = sock_no_sendpage,
1095 static struct proto isotp_proto __read_mostly = {
1096 .name = "CAN_ISOTP",
1097 .owner = THIS_MODULE,
1098 .obj_size = sizeof(struct isotp_sock),
1102 static struct can_proto isotp_can_proto __read_mostly = {
1104 .protocol = CAN_ISOTP,
1107 .prot = &isotp_proto,
1110 static __init int isotp_module_init(void)
1116 err = can_proto_register(&isotp_can_proto);
1118 printk(KERN_ERR "can: registration of isotp protocol failed\n");
1123 static __exit void isotp_module_exit(void)
1125 can_proto_unregister(&isotp_can_proto);
1128 module_init(isotp_module_init);
1129 module_exit(isotp_module_exit);