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 "20080902-alpha"
79 static __initdata const char banner[] =
80 KERN_INFO "can: isotp protocol (rev " CAN_ISOTP_VERSION ")\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_SFF_MASK
98 /* N_PCI type values in bits 7-4 of N_PCI bytes */
99 #define N_PCI_SF 0x00 /* single frame */
100 #define N_PCI_FF 0x10 /* first frame */
101 #define N_PCI_CF 0x20 /* consecutive frame */
102 #define N_PCI_FC 0x30 /* flow control */
104 /* Flow Status given in FC frame */
105 #define ISOTP_FC_CTS 0 /* clear to send */
106 #define ISOTP_FC_WT 1 /* wait */
107 #define ISOTP_FC_OVFLW 2 /* overflow */
133 struct hrtimer rxtimer, txtimer;
134 struct can_isotp_options opt;
135 struct can_isotp_fc_options rxfc, txfc;
137 struct notifier_block notifier;
138 wait_queue_head_t wait;
141 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
143 return (struct isotp_sock *)sk;
146 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
148 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
150 if (so->rx.state == ISOTP_WAIT_DATA) {
152 struct sock *sk = &so->sk;
154 /* report 'timeout' */
156 if (!sock_flag(sk, SOCK_DEAD))
157 sk->sk_error_report(sk);
159 DBG("we did not get new data frames in time.\n");
162 so->rx.state = ISOTP_IDLE;
165 return HRTIMER_NORESTART;
168 static int isotp_send_fc(struct sock *sk, int ae)
170 struct net_device *dev;
171 struct sk_buff *nskb;
172 struct can_frame *ncf;
173 struct isotp_sock *so = isotp_sk(sk);
175 nskb = alloc_skb(sizeof(struct can_frame), gfp_any());
179 dev = dev_get_by_index(&init_net, so->ifindex);
186 ncf = (struct can_frame *) nskb->data;
187 skb_put(nskb, sizeof(struct can_frame));
189 /* create & send flow control reply */
190 ncf->can_id = so->txid;
192 if (so->opt.flags & CAN_ISOTP_RX_PADDING) {
193 memset(ncf->data, so->opt.rxpad_content, 8);
198 ncf->data[ae] = N_PCI_FC | ISOTP_FC_CTS;
199 ncf->data[ae+1] = so->rxfc.bs;
200 ncf->data[ae+2] = so->rxfc.stmin;
203 ncf->data[0] = so->opt.ext_address;
208 /* reset blocksize counter */
211 /* start rx timeout watchdog */
212 hrtimer_start(&so->rxtimer, ktime_set(1,0), HRTIMER_MODE_REL);
216 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
218 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
220 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
224 memset(addr, 0, sizeof(*addr));
225 addr->can_family = AF_CAN;
226 addr->can_ifindex = skb->dev->ifindex;
228 if (sock_queue_rcv_skb(sk, skb) < 0)
232 static int check_pad(struct isotp_sock *so, struct can_frame *cf,
233 int start_index, __u8 content)
237 /* check datalength code */
238 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && cf->can_dlc != 8)
241 /* check padding content */
242 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
243 for (i = start_index; i < 8; i++)
244 if (cf->data[i] != content)
250 static int isotp_rcv_fc(struct isotp_sock *so, struct can_frame *cf, int ae)
252 if (so->tx.state != ISOTP_WAIT_FC &&
253 so->tx.state != ISOTP_WAIT_FIRST_FC)
256 hrtimer_cancel(&so->txtimer);
258 if ((so->opt.flags & CAN_ISOTP_TX_PADDING) &&
259 check_pad(so, cf, ae+3, so->opt.txpad_content)) {
260 so->tx.state = ISOTP_IDLE;
261 wake_up_interruptible(&so->wait);
265 /* get communication parameters only from the first FC frame */
266 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
268 so->txfc.bs = cf->data[ae+1];
269 so->txfc.stmin = cf->data[ae+2];
271 /* fix wrong STmin values according spec */
272 if ((so->txfc.stmin > 0x7F) &&
273 ((so->txfc.stmin < 0xF1) || (so->txfc.stmin > 0xF9)))
274 so->txfc.stmin = 0x7F;
276 so->tx_gap = ktime_set(0,0);
277 /* add transmission time for CAN frame N_As */
278 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
279 /* add waiting time for consecutive frames N_Cs */
280 if (so->txfc.stmin < 0x80)
281 so->tx_gap = ktime_add_ns(so->tx_gap,
282 so->txfc.stmin * 1000000);
284 so->tx_gap = ktime_add_ns(so->tx_gap,
285 (so->txfc.stmin - 0xF0)
287 so->tx.state = ISOTP_WAIT_FC;
290 DBG("FC frame: FS %d, BS %d, STmin 0x%02X, tx_gap %lld\n",
291 cf->data[ae] & 0x0F & 0x0F, so->txfc.bs, so->txfc.stmin,
292 (long long)so->tx_gap.tv64);
294 switch (cf->data[ae] & 0x0F) {
298 so->tx.state = ISOTP_SENDING;
299 DBG("starting txtimer for sending\n");
300 /* start cyclic timer for sending CF frame */
301 hrtimer_start(&so->txtimer, so->tx_gap,
306 DBG("starting waiting for next FC\n");
307 /* start timer to wait for next FC frame */
308 hrtimer_start(&so->txtimer, ktime_set(1,0),
313 DBG("overflow in receiver side\n");
316 /* stop this tx job. TODO: error reporting? */
317 so->tx.state = ISOTP_IDLE;
318 wake_up_interruptible(&so->wait);
323 static int isotp_rcv_sf(struct sock *sk, struct can_frame *cf, int ae,
326 struct isotp_sock *so = isotp_sk(sk);
327 int len = cf->data[ae] & 0x0F;
328 struct sk_buff *nskb;
330 hrtimer_cancel(&so->rxtimer);
331 so->rx.state = ISOTP_IDLE;
333 if (!len || len > 7 || (ae && len > 6))
336 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
337 check_pad(so, cf, 1+ae+len, so->opt.rxpad_content))
340 nskb = alloc_skb(len, gfp_any());
344 memcpy(skb_put(nskb, len), &cf->data[1+ae], len);
346 nskb->tstamp = skb->tstamp;
347 nskb->dev = skb->dev;
348 isotp_rcv_skb(nskb, sk);
352 static int isotp_rcv_ff(struct sock *sk, struct can_frame *cf, int ae)
354 struct isotp_sock *so = isotp_sk(sk);
357 hrtimer_cancel(&so->rxtimer);
358 so->rx.state = ISOTP_IDLE;
360 if (cf->can_dlc != 8)
363 so->rx.len = (cf->data[ae] & 0x0F) << 8;
364 so->rx.len += cf->data[ae+1];
366 if (so->rx.len + ae < 8)
369 /* copy the first received data bytes */
371 for (i = ae+2; i < 8; i++)
372 so->rx.buf[so->rx.idx++] = cf->data[i];
374 /* initial setup for this pdu receiption */
376 so->rx.state = ISOTP_WAIT_DATA;
378 /* no creation of flow control frames */
379 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
382 /* send our first FC frame */
383 isotp_send_fc(sk, ae);
387 static int isotp_rcv_cf(struct sock *sk, struct can_frame *cf, int ae,
390 struct isotp_sock *so = isotp_sk(sk);
391 struct sk_buff *nskb;
394 if (so->rx.state != ISOTP_WAIT_DATA)
397 hrtimer_cancel(&so->rxtimer);
399 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
400 DBG("wrong sn %d. expected %d.\n",
401 cf->data[ae] & 0x0F, so->rx.sn);
402 /* some error reporting? */
403 so->rx.state = ISOTP_IDLE;
409 for (i = ae+1; i < 8; i++) {
410 so->rx.buf[so->rx.idx++] = cf->data[i];
411 if (so->rx.idx >= so->rx.len)
415 if (so->rx.idx >= so->rx.len) {
418 so->rx.state = ISOTP_IDLE;
420 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
421 check_pad(so, cf, i+1, so->opt.rxpad_content))
424 nskb = alloc_skb(so->rx.len, gfp_any());
428 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
431 nskb->tstamp = skb->tstamp;
432 nskb->dev = skb->dev;
433 isotp_rcv_skb(nskb, sk);
437 /* no creation of flow control frames */
438 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
441 /* perform blocksize handling, if enabled */
442 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
444 /* start rx timeout watchdog */
445 hrtimer_start(&so->rxtimer, ktime_set(1,0),
450 /* we reached the specified blocksize so->rxfc.bs */
451 isotp_send_fc(sk, ae);
455 static void isotp_rcv(struct sk_buff *skb, void *data)
457 struct sock *sk = (struct sock *)data;
458 struct isotp_sock *so = isotp_sk(sk);
459 struct can_frame *cf;
460 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
463 /* read CAN frame and free skbuff */
464 BUG_ON(skb->len != sizeof(struct can_frame));
465 cf = (struct can_frame *) skb->data;
467 /* if enabled: check receiption of my configured extended address */
468 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)) {
484 switch (n_pci_type) {
486 /* tx path: flow control frame containing the FC parameters */
487 isotp_rcv_fc(so, cf, ae);
491 /* rx path: single frame */
492 isotp_rcv_sf(sk, cf, ae, skb);
496 /* rx path: first frame */
497 isotp_rcv_ff(sk, cf, ae);
501 /* rx path: consecutive frame */
502 isotp_rcv_cf(sk, cf, ae, skb);
509 static void isotp_fill_dataframe(struct can_frame *cf, struct isotp_sock *so,
512 unsigned char space = 7 - ae;
513 int num = min_t(int, so->tx.len - so->tx.idx, space);
516 cf->can_id = so->txid;
518 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
520 memset(cf->data, so->opt.txpad_content, 8);
524 cf->can_dlc = num + 1 + ae;
527 for (i = 0; i < num; i++)
528 cf->data[i+ae+1] = so->tx.buf[so->tx.idx++];
531 cf->data[0] = so->opt.ext_address;
534 static void isotp_create_fframe(struct can_frame *cf, struct isotp_sock *so,
539 cf->can_id = so->txid;
542 cf->data[0] = so->opt.ext_address;
544 /* N_PCI bytes with FF_DL data length */
545 cf->data[ae] = (u8) (so->tx.len>>8) | N_PCI_FF;
546 cf->data[ae+1] = (u8) so->tx.len & 0xFFU;
548 /* add first 5 or 6 data bytes depending on ae */
549 for (i = ae+2; i < 8; i++)
550 cf->data[i] = so->tx.buf[so->tx.idx++];
553 so->tx.state = ISOTP_WAIT_FIRST_FC;
556 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
558 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
560 struct sock *sk = &so->sk;
562 struct net_device *dev;
563 struct can_frame *cf;
564 enum hrtimer_restart ret;
565 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
567 switch (so->tx.state) {
570 case ISOTP_WAIT_FIRST_FC:
572 /* we did not get any flow control frame in time */
574 DBG("we did not get FC frame in time.\n");
577 /* report 'communication error on send' */
579 if (!sock_flag(sk, SOCK_DEAD))
580 sk->sk_error_report(sk);
583 so->tx.state = ISOTP_IDLE;
584 wake_up_interruptible(&so->wait);
586 ret = HRTIMER_NORESTART;
591 /* push out the next segmented pdu */
593 DBG("next pdu to send.\n");
595 dev = dev_get_by_index(&init_net, so->ifindex);
597 return HRTIMER_NORESTART;
599 skb = alloc_skb(sizeof(*cf), gfp_any());
602 return HRTIMER_NORESTART;
605 cf = (struct can_frame *)skb->data;
606 skb_put(skb, sizeof(*cf));
608 /* create consecutive frame */
609 isotp_fill_dataframe(cf, so, ae);
611 /* place consecutive frame N_PCI in appropriate index */
612 cf->data[ae] = N_PCI_CF | so->tx.sn++;
621 if (so->tx.idx >= so->tx.len) {
623 DBG("we are done\n");
624 so->tx.state = ISOTP_IDLE;
625 wake_up_interruptible(&so->wait);
626 return HRTIMER_NORESTART;
629 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
630 /* stop and wait for FC */
631 DBG("BS stop and wait for FC\n");
632 so->tx.state = ISOTP_WAIT_FC;
633 hrtimer_forward(hrtimer, ktime_get(), ktime_set(1,0));
635 hrtimer_forward(hrtimer, ktime_get(), so->tx_gap);
637 ret = HRTIMER_RESTART;
647 static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
648 struct msghdr *msg, size_t size)
650 struct sock *sk = sock->sk;
651 struct isotp_sock *so = isotp_sk(sk);
653 struct net_device *dev;
654 struct can_frame *cf;
655 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
659 return -EADDRNOTAVAIL;
661 /* we do not support multiple buffers - for now */
662 if (so->tx.state != ISOTP_IDLE) {
663 if (msg->msg_flags & MSG_DONTWAIT)
666 /* wait for complete transmission of current pdu */
667 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
670 if (!size || size > 4095)
673 err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
677 dev = dev_get_by_index(&init_net, so->ifindex);
681 skb = sock_alloc_send_skb(sk, sizeof(*cf),
682 msg->msg_flags & MSG_DONTWAIT, &err);
688 so->tx.state = ISOTP_SENDING;
692 cf = (struct can_frame *)skb->data;
693 skb_put(skb, sizeof(*cf));
695 /* check for single frame transmission */
696 if (size <= 7 - ae) {
698 isotp_fill_dataframe(cf, so, ae);
700 /* place single frame N_PCI in appropriate index */
701 cf->data[ae] = size | N_PCI_SF;
703 so->tx.state = ISOTP_IDLE;
704 wake_up_interruptible(&so->wait);
706 /* send first frame and wait for FC */
708 isotp_create_fframe(cf, so, ae);
710 DBG("starting txtimer for fc\n");
711 /* start timeout for FC */
712 hrtimer_start(&so->txtimer, ktime_set(1,0), HRTIMER_MODE_REL);
715 /* send the first or only CAN frame */
718 err = can_send(skb, 1);
726 static int isotp_recvmsg(struct kiocb *iocb, struct socket *sock,
727 struct msghdr *msg, size_t size, int flags)
729 struct sock *sk = sock->sk;
734 noblock = flags & MSG_DONTWAIT;
735 flags &= ~MSG_DONTWAIT;
737 skb = skb_recv_datagram(sk, flags, noblock, &err);
742 msg->msg_flags |= MSG_TRUNC;
746 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
748 skb_free_datagram(sk, skb);
752 sock_recv_timestamp(msg, sk, skb);
755 msg->msg_namelen = sizeof(struct sockaddr_can);
756 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
759 skb_free_datagram(sk, skb);
764 static int isotp_release(struct socket *sock)
766 struct sock *sk = sock->sk;
767 struct isotp_sock *so = isotp_sk(sk);
769 /* wait for complete transmission of current pdu */
770 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
772 unregister_netdevice_notifier(&so->notifier);
776 hrtimer_cancel(&so->txtimer);
777 hrtimer_cancel(&so->rxtimer);
779 /* remove current filters & unregister */
782 struct net_device *dev;
784 dev = dev_get_by_index(&init_net, so->ifindex);
786 can_rx_unregister(dev, so->rxid,
787 SINGLE_MASK(so->rxid),
803 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
805 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
806 struct sock *sk = sock->sk;
807 struct isotp_sock *so = isotp_sk(sk);
809 struct net_device *dev;
811 int notify_enetdown = 0;
813 if (len < sizeof(*addr))
816 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
817 return -EADDRNOTAVAIL;
819 if (!addr->can_ifindex)
824 if (so->bound && addr->can_ifindex == so->ifindex &&
825 addr->can_addr.tp.rx_id == so->rxid &&
826 addr->can_addr.tp.tx_id == so->txid)
829 dev = dev_get_by_index(&init_net, addr->can_ifindex);
834 if (dev->type != ARPHRD_CAN) {
839 if (!(dev->flags & IFF_UP))
842 ifindex = dev->ifindex;
844 can_rx_register(dev, addr->can_addr.tp.rx_id,
845 SINGLE_MASK(addr->can_addr.tp.rx_id),
846 isotp_rcv, sk, "isotp");
850 /* unregister old filter */
852 dev = dev_get_by_index(&init_net, so->ifindex);
854 can_rx_unregister(dev, so->rxid,
855 SINGLE_MASK(so->rxid),
862 /* switch to new settings */
863 so->ifindex = ifindex;
864 so->rxid = addr->can_addr.tp.rx_id;
865 so->txid = addr->can_addr.tp.tx_id;
871 if (notify_enetdown) {
872 sk->sk_err = ENETDOWN;
873 if (!sock_flag(sk, SOCK_DEAD))
874 sk->sk_error_report(sk);
880 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr,
883 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
884 struct sock *sk = sock->sk;
885 struct isotp_sock *so = isotp_sk(sk);
890 addr->can_family = AF_CAN;
891 addr->can_ifindex = so->ifindex;
893 *len = sizeof(*addr);
898 static int isotp_setsockopt(struct socket *sock, int level, int optname,
899 char __user *optval, int optlen)
901 struct sock *sk = sock->sk;
902 struct isotp_sock *so = isotp_sk(sk);
905 if (level != SOL_CAN_ISOTP)
913 if (optlen != sizeof(struct can_isotp_options))
916 if (copy_from_user(&so->opt, optval, optlen))
920 case CAN_ISOTP_RECV_FC:
921 if (optlen != sizeof(struct can_isotp_fc_options))
924 if (copy_from_user(&so->rxfc, optval, optlen))
935 static int isotp_getsockopt(struct socket *sock, int level, int optname,
936 char __user *optval, int __user *optlen)
938 struct sock *sk = sock->sk;
939 struct isotp_sock *so = isotp_sk(sk);
943 if (level != SOL_CAN_ISOTP)
945 if (get_user(len, optlen))
953 len = min_t(int, len, sizeof(struct can_isotp_options));
957 case CAN_ISOTP_RECV_FC:
958 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
966 if (put_user(len, optlen))
968 if (copy_to_user(optval, val, len))
974 static int isotp_notifier(struct notifier_block *nb,
975 unsigned long msg, void *data)
977 struct net_device *dev = (struct net_device *)data;
978 struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
979 struct sock *sk = &so->sk;
981 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
982 if (dev_net(dev) != &init_net)
984 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
985 if (dev->nd_net != &init_net)
989 if (dev->type != ARPHRD_CAN)
992 if (so->ifindex != dev->ifindex)
997 case NETDEV_UNREGISTER:
999 /* remove current filters & unregister */
1001 can_rx_unregister(dev, so->rxid, SINGLE_MASK(so->rxid),
1008 sk->sk_err = ENODEV;
1009 if (!sock_flag(sk, SOCK_DEAD))
1010 sk->sk_error_report(sk);
1014 sk->sk_err = ENETDOWN;
1015 if (!sock_flag(sk, SOCK_DEAD))
1016 sk->sk_error_report(sk);
1024 static int isotp_init(struct sock *sk)
1026 struct isotp_sock *so = isotp_sk(sk);
1031 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1032 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1033 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_RXPAD_CONTENT;
1034 so->opt.txpad_content = CAN_ISOTP_DEFAULT_TXPAD_CONTENT;
1035 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1036 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1037 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1038 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1040 so->rx.state = ISOTP_IDLE;
1041 so->tx.state = ISOTP_IDLE;
1043 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1044 so->rxtimer.function = isotp_rx_timer_handler;
1045 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1046 so->txtimer.function = isotp_tx_timer_handler;
1048 init_waitqueue_head(&so->wait);
1050 so->notifier.notifier_call = isotp_notifier;
1051 register_netdevice_notifier(&so->notifier);
1057 static struct proto_ops isotp_ops __read_mostly = {
1059 .release = isotp_release,
1061 .connect = sock_no_connect,
1062 .socketpair = sock_no_socketpair,
1063 .accept = sock_no_accept,
1064 .getname = isotp_getname,
1065 .poll = datagram_poll,
1066 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1067 .listen = sock_no_listen,
1068 .shutdown = sock_no_shutdown,
1069 .setsockopt = isotp_setsockopt,
1070 .getsockopt = isotp_getsockopt,
1071 .sendmsg = isotp_sendmsg,
1072 .recvmsg = isotp_recvmsg,
1073 .mmap = sock_no_mmap,
1074 .sendpage = sock_no_sendpage,
1077 static struct proto isotp_proto __read_mostly = {
1078 .name = "CAN_ISOTP",
1079 .owner = THIS_MODULE,
1080 .obj_size = sizeof(struct isotp_sock),
1084 static struct can_proto isotp_can_proto __read_mostly = {
1086 .protocol = CAN_ISOTP,
1089 .prot = &isotp_proto,
1092 static __init int isotp_module_init(void)
1098 err = can_proto_register(&isotp_can_proto);
1100 printk(KERN_ERR "can: registration of isotp protocol failed\n");
1105 static __exit void isotp_module_exit(void)
1107 can_proto_unregister(&isotp_can_proto);
1110 module_init(isotp_module_init);
1111 module_exit(isotp_module_exit);