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 production 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/interrupt.h>
58 #include <linux/hrtimer.h>
59 #include <linux/wait.h>
60 #include <linux/uio.h>
61 #include <linux/net.h>
62 #include <linux/netdevice.h>
63 #include <linux/socket.h>
64 #include <linux/if_arp.h>
65 #include <linux/skbuff.h>
66 #include <socketcan/can.h>
67 #include <socketcan/can/core.h>
68 #include <socketcan/can/isotp.h>
70 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
71 #include <net/net_namespace.h>
73 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
77 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
80 #define CAN_ISOTP_VERSION CAN_VERSION
81 static __initdata const char banner[] =
82 KERN_INFO "can: isotp protocol (rev " CAN_ISOTP_VERSION " alpha)\n";
84 MODULE_DESCRIPTION("PF_CAN isotp 15765-2 protocol");
85 MODULE_LICENSE("Dual BSD/GPL");
86 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
87 MODULE_ALIAS("can-proto-6");
89 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
90 #error This modules needs hrtimers (available since Kernel 2.6.22)
93 #define DBG(fmt, args...) (printk( KERN_DEBUG "can-isotp: %s: " fmt, \
96 #define DBG(fmt, args...)
98 #define SINGLE_MASK(id) ((id & CAN_EFF_FLAG) ? \
99 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
100 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
102 /* N_PCI type values in bits 7-4 of N_PCI bytes */
103 #define N_PCI_SF 0x00 /* single frame */
104 #define N_PCI_FF 0x10 /* first frame */
105 #define N_PCI_CF 0x20 /* consecutive frame */
106 #define N_PCI_FC 0x30 /* flow control */
108 /* Flow Status given in FC frame */
109 #define ISOTP_FC_CTS 0 /* clear to send */
110 #define ISOTP_FC_WT 1 /* wait */
111 #define ISOTP_FC_OVFLW 2 /* overflow */
137 ktime_t lastrxcf_tstamp;
138 struct hrtimer rxtimer, txtimer;
139 struct tasklet_struct txtsklet;
140 struct can_isotp_options opt;
141 struct can_isotp_fc_options rxfc, txfc;
142 __u32 force_tx_stmin;
143 __u32 force_rx_stmin;
145 struct notifier_block notifier;
146 wait_queue_head_t wait;
149 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
151 return (struct isotp_sock *)sk;
154 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
156 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
158 if (so->rx.state == ISOTP_WAIT_DATA) {
160 struct sock *sk = &so->sk;
162 /* report 'timeout' */
164 if (!sock_flag(sk, SOCK_DEAD))
165 sk->sk_error_report(sk);
167 DBG("we did not get new data frames in time.\n");
170 so->rx.state = ISOTP_IDLE;
173 return HRTIMER_NORESTART;
176 static int isotp_send_fc(struct sock *sk, int ae)
178 struct net_device *dev;
179 struct sk_buff *nskb;
180 struct can_frame *ncf;
181 struct isotp_sock *so = isotp_sk(sk);
183 nskb = alloc_skb(sizeof(struct can_frame), gfp_any());
187 dev = dev_get_by_index(&init_net, so->ifindex);
194 ncf = (struct can_frame *) nskb->data;
195 skb_put(nskb, sizeof(struct can_frame));
197 /* create & send flow control reply */
198 ncf->can_id = so->txid;
200 if (so->opt.flags & CAN_ISOTP_RX_PADDING) {
201 memset(ncf->data, so->opt.rxpad_content, 8);
206 ncf->data[ae] = N_PCI_FC | ISOTP_FC_CTS;
207 ncf->data[ae+1] = so->rxfc.bs;
208 ncf->data[ae+2] = so->rxfc.stmin;
211 ncf->data[0] = so->opt.ext_address;
216 /* reset blocksize counter */
219 /* reset last CF frame rx timestamp for rx stmin enforcement */
220 so->lastrxcf_tstamp = ktime_set(0,0);
222 /* start rx timeout watchdog */
223 hrtimer_start(&so->rxtimer, ktime_set(1,0), HRTIMER_MODE_REL);
227 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
229 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
231 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
235 memset(addr, 0, sizeof(*addr));
236 addr->can_family = AF_CAN;
237 addr->can_ifindex = skb->dev->ifindex;
239 if (sock_queue_rcv_skb(sk, skb) < 0)
243 static int check_pad(struct isotp_sock *so, struct can_frame *cf,
244 int start_index, __u8 content)
248 /* check datalength code */
249 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && cf->can_dlc != 8)
252 /* check padding content */
253 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
254 for (i = start_index; i < 8; i++)
255 if (cf->data[i] != content)
261 static int isotp_rcv_fc(struct isotp_sock *so, struct can_frame *cf, int ae)
263 if (so->tx.state != ISOTP_WAIT_FC &&
264 so->tx.state != ISOTP_WAIT_FIRST_FC)
267 hrtimer_cancel(&so->txtimer);
269 if ((so->opt.flags & CAN_ISOTP_TX_PADDING) &&
270 check_pad(so, cf, ae+3, so->opt.txpad_content)) {
271 so->tx.state = ISOTP_IDLE;
272 wake_up_interruptible(&so->wait);
276 /* get communication parameters only from the first FC frame */
277 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
279 so->txfc.bs = cf->data[ae+1];
280 so->txfc.stmin = cf->data[ae+2];
282 /* fix wrong STmin values according spec */
283 if ((so->txfc.stmin > 0x7F) &&
284 ((so->txfc.stmin < 0xF1) || (so->txfc.stmin > 0xF9)))
285 so->txfc.stmin = 0x7F;
287 so->tx_gap = ktime_set(0,0);
288 /* add transmission time for CAN frame N_As */
289 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
290 /* add waiting time for consecutive frames N_Cs */
291 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
292 so->tx_gap = ktime_add_ns(so->tx_gap,
294 else if (so->txfc.stmin < 0x80)
295 so->tx_gap = ktime_add_ns(so->tx_gap,
296 so->txfc.stmin * 1000000);
298 so->tx_gap = ktime_add_ns(so->tx_gap,
299 (so->txfc.stmin - 0xF0)
301 so->tx.state = ISOTP_WAIT_FC;
304 DBG("FC frame: FS %d, BS %d, STmin 0x%02X, tx_gap %lld\n",
305 cf->data[ae] & 0x0F & 0x0F, so->txfc.bs, so->txfc.stmin,
306 (long long)so->tx_gap.tv64);
308 switch (cf->data[ae] & 0x0F) {
312 so->tx.state = ISOTP_SENDING;
313 DBG("starting txtimer for sending\n");
314 /* start cyclic timer for sending CF frame */
315 hrtimer_start(&so->txtimer, so->tx_gap,
320 DBG("starting waiting for next FC\n");
321 /* start timer to wait for next FC frame */
322 hrtimer_start(&so->txtimer, ktime_set(1,0),
327 DBG("overflow in receiver side\n");
330 /* stop this tx job. TODO: error reporting? */
331 so->tx.state = ISOTP_IDLE;
332 wake_up_interruptible(&so->wait);
337 static int isotp_rcv_sf(struct sock *sk, struct can_frame *cf, int ae,
340 struct isotp_sock *so = isotp_sk(sk);
341 int len = cf->data[ae] & 0x0F;
342 struct sk_buff *nskb;
344 hrtimer_cancel(&so->rxtimer);
345 so->rx.state = ISOTP_IDLE;
347 if (!len || len > 7 || (ae && len > 6))
350 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
351 check_pad(so, cf, 1+ae+len, so->opt.rxpad_content))
354 nskb = alloc_skb(len, gfp_any());
358 memcpy(skb_put(nskb, len), &cf->data[1+ae], len);
360 nskb->tstamp = skb->tstamp;
361 nskb->dev = skb->dev;
362 isotp_rcv_skb(nskb, sk);
366 static int isotp_rcv_ff(struct sock *sk, struct can_frame *cf, int ae)
368 struct isotp_sock *so = isotp_sk(sk);
371 hrtimer_cancel(&so->rxtimer);
372 so->rx.state = ISOTP_IDLE;
374 if (cf->can_dlc != 8)
377 so->rx.len = (cf->data[ae] & 0x0F) << 8;
378 so->rx.len += cf->data[ae+1];
380 if (so->rx.len + ae < 8)
383 /* copy the first received data bytes */
385 for (i = ae+2; i < 8; i++)
386 so->rx.buf[so->rx.idx++] = cf->data[i];
388 /* initial setup for this pdu receiption */
390 so->rx.state = ISOTP_WAIT_DATA;
392 /* no creation of flow control frames */
393 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
396 /* send our first FC frame */
397 isotp_send_fc(sk, ae);
401 static int isotp_rcv_cf(struct sock *sk, struct can_frame *cf, int ae,
404 struct isotp_sock *so = isotp_sk(sk);
405 struct sk_buff *nskb;
408 if (so->rx.state != ISOTP_WAIT_DATA)
411 /* drop if timestamp gap is less than force_rx_stmin nano secs */
412 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
414 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
418 so->lastrxcf_tstamp = skb->tstamp;
421 hrtimer_cancel(&so->rxtimer);
423 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
424 DBG("wrong sn %d. expected %d.\n",
425 cf->data[ae] & 0x0F, so->rx.sn);
426 /* some error reporting? */
427 so->rx.state = ISOTP_IDLE;
433 for (i = ae+1; i < 8; i++) {
434 so->rx.buf[so->rx.idx++] = cf->data[i];
435 if (so->rx.idx >= so->rx.len)
439 if (so->rx.idx >= so->rx.len) {
442 so->rx.state = ISOTP_IDLE;
444 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
445 check_pad(so, cf, i+1, so->opt.rxpad_content))
448 nskb = alloc_skb(so->rx.len, gfp_any());
452 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
455 nskb->tstamp = skb->tstamp;
456 nskb->dev = skb->dev;
457 isotp_rcv_skb(nskb, sk);
461 /* no creation of flow control frames */
462 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
465 /* perform blocksize handling, if enabled */
466 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
468 /* start rx timeout watchdog */
469 hrtimer_start(&so->rxtimer, ktime_set(1,0),
474 /* we reached the specified blocksize so->rxfc.bs */
475 isotp_send_fc(sk, ae);
479 static void isotp_rcv(struct sk_buff *skb, void *data)
481 struct sock *sk = (struct sock *)data;
482 struct isotp_sock *so = isotp_sk(sk);
483 struct can_frame *cf;
484 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
487 /* read CAN frame and free skbuff */
488 BUG_ON(skb->len != sizeof(struct can_frame));
489 cf = (struct can_frame *) skb->data;
491 /* if enabled: check receiption of my configured extended address */
492 if (ae && cf->data[0] != so->opt.ext_address)
495 n_pci_type = cf->data[ae] & 0xF0;
497 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
498 /* check rx/tx path half duplex expectations */
499 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
500 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
504 switch (n_pci_type) {
506 /* tx path: flow control frame containing the FC parameters */
507 isotp_rcv_fc(so, cf, ae);
511 /* rx path: single frame */
512 isotp_rcv_sf(sk, cf, ae, skb);
516 /* rx path: first frame */
517 isotp_rcv_ff(sk, cf, ae);
521 /* rx path: consecutive frame */
522 isotp_rcv_cf(sk, cf, ae, skb);
527 static void isotp_fill_dataframe(struct can_frame *cf, struct isotp_sock *so,
530 unsigned char space = 7 - ae;
531 int num = min_t(int, so->tx.len - so->tx.idx, space);
534 cf->can_id = so->txid;
536 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
538 memset(cf->data, so->opt.txpad_content, 8);
542 cf->can_dlc = num + 1 + ae;
545 for (i = 0; i < num; i++)
546 cf->data[i+ae+1] = so->tx.buf[so->tx.idx++];
549 cf->data[0] = so->opt.ext_address;
552 static void isotp_create_fframe(struct can_frame *cf, struct isotp_sock *so,
557 cf->can_id = so->txid;
560 cf->data[0] = so->opt.ext_address;
562 /* N_PCI bytes with FF_DL data length */
563 cf->data[ae] = (u8) (so->tx.len>>8) | N_PCI_FF;
564 cf->data[ae+1] = (u8) so->tx.len & 0xFFU;
566 /* add first 5 or 6 data bytes depending on ae */
567 for (i = ae+2; i < 8; i++)
568 cf->data[i] = so->tx.buf[so->tx.idx++];
571 so->tx.state = ISOTP_WAIT_FIRST_FC;
574 static void isotp_tx_timer_tsklet(unsigned long data)
576 struct isotp_sock *so = (struct isotp_sock *)data;
577 struct sock *sk = &so->sk;
579 struct net_device *dev;
580 struct can_frame *cf;
581 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
583 switch (so->tx.state) {
586 case ISOTP_WAIT_FIRST_FC:
588 /* we did not get any flow control frame in time */
590 DBG("we did not get FC frame in time.\n");
593 /* report 'communication error on send' */
595 if (!sock_flag(sk, SOCK_DEAD))
596 sk->sk_error_report(sk);
599 so->tx.state = ISOTP_IDLE;
600 wake_up_interruptible(&so->wait);
605 /* push out the next segmented pdu */
607 DBG("next pdu to send.\n");
609 dev = dev_get_by_index(&init_net, so->ifindex);
614 skb = alloc_skb(sizeof(*cf), gfp_any());
620 cf = (struct can_frame *)skb->data;
621 skb_put(skb, sizeof(*cf));
623 /* create consecutive frame */
624 isotp_fill_dataframe(cf, so, ae);
626 /* place consecutive frame N_PCI in appropriate index */
627 cf->data[ae] = N_PCI_CF | so->tx.sn++;
635 if (so->tx.idx >= so->tx.len) {
637 DBG("we are done\n");
638 so->tx.state = ISOTP_IDLE;
640 wake_up_interruptible(&so->wait);
644 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
645 /* stop and wait for FC */
646 DBG("BS stop and wait for FC\n");
647 so->tx.state = ISOTP_WAIT_FC;
649 hrtimer_start(&so->txtimer,
650 ktime_add(ktime_get(), ktime_set(1,0)),
655 /* no gap between data frames needed => use burst mode */
656 if (!so->tx_gap.tv64)
659 /* start timer to send next data frame with correct delay */
661 hrtimer_start(&so->txtimer,
662 ktime_add(ktime_get(), so->tx_gap),
671 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
673 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
675 tasklet_schedule(&so->txtsklet);
677 return HRTIMER_NORESTART;
680 static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
681 struct msghdr *msg, size_t size)
683 struct sock *sk = sock->sk;
684 struct isotp_sock *so = isotp_sk(sk);
686 struct net_device *dev;
687 struct can_frame *cf;
688 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
692 return -EADDRNOTAVAIL;
694 /* we do not support multiple buffers - for now */
695 if (so->tx.state != ISOTP_IDLE) {
696 if (msg->msg_flags & MSG_DONTWAIT)
699 /* wait for complete transmission of current pdu */
700 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
703 if (!size || size > 4095)
706 err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
710 dev = dev_get_by_index(&init_net, so->ifindex);
714 skb = sock_alloc_send_skb(sk, sizeof(*cf),
715 msg->msg_flags & MSG_DONTWAIT, &err);
721 so->tx.state = ISOTP_SENDING;
725 cf = (struct can_frame *)skb->data;
726 skb_put(skb, sizeof(*cf));
728 /* check for single frame transmission */
729 if (size <= 7 - ae) {
731 isotp_fill_dataframe(cf, so, ae);
733 /* place single frame N_PCI in appropriate index */
734 cf->data[ae] = size | N_PCI_SF;
736 so->tx.state = ISOTP_IDLE;
737 wake_up_interruptible(&so->wait);
739 /* send first frame and wait for FC */
741 isotp_create_fframe(cf, so, ae);
743 DBG("starting txtimer for fc\n");
744 /* start timeout for FC */
745 hrtimer_start(&so->txtimer, ktime_set(1,0), HRTIMER_MODE_REL);
748 /* send the first or only CAN frame */
751 err = can_send(skb, 1);
759 static int isotp_recvmsg(struct kiocb *iocb, struct socket *sock,
760 struct msghdr *msg, size_t size, int flags)
762 struct sock *sk = sock->sk;
767 noblock = flags & MSG_DONTWAIT;
768 flags &= ~MSG_DONTWAIT;
770 skb = skb_recv_datagram(sk, flags, noblock, &err);
775 msg->msg_flags |= MSG_TRUNC;
779 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
781 skb_free_datagram(sk, skb);
785 sock_recv_timestamp(msg, sk, skb);
788 msg->msg_namelen = sizeof(struct sockaddr_can);
789 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
792 skb_free_datagram(sk, skb);
797 static int isotp_release(struct socket *sock)
799 struct sock *sk = sock->sk;
800 struct isotp_sock *so;
807 /* wait for complete transmission of current pdu */
808 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
810 unregister_netdevice_notifier(&so->notifier);
814 hrtimer_cancel(&so->txtimer);
815 hrtimer_cancel(&so->rxtimer);
816 tasklet_kill(&so->txtsklet);
818 /* remove current filters & unregister */
821 struct net_device *dev;
823 dev = dev_get_by_index(&init_net, so->ifindex);
825 can_rx_unregister(dev, so->rxid,
826 SINGLE_MASK(so->rxid),
845 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
847 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
848 struct sock *sk = sock->sk;
849 struct isotp_sock *so = isotp_sk(sk);
851 struct net_device *dev;
853 int notify_enetdown = 0;
855 if (len < sizeof(*addr))
858 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
859 return -EADDRNOTAVAIL;
861 if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) &
862 (CAN_ERR_FLAG | CAN_RTR_FLAG))
863 return -EADDRNOTAVAIL;
865 if (!addr->can_ifindex)
870 if (so->bound && addr->can_ifindex == so->ifindex &&
871 addr->can_addr.tp.rx_id == so->rxid &&
872 addr->can_addr.tp.tx_id == so->txid)
875 dev = dev_get_by_index(&init_net, addr->can_ifindex);
880 if (dev->type != ARPHRD_CAN) {
885 if (!(dev->flags & IFF_UP))
888 ifindex = dev->ifindex;
890 can_rx_register(dev, addr->can_addr.tp.rx_id,
891 SINGLE_MASK(addr->can_addr.tp.rx_id),
892 isotp_rcv, sk, "isotp");
896 /* unregister old filter */
898 dev = dev_get_by_index(&init_net, so->ifindex);
900 can_rx_unregister(dev, so->rxid,
901 SINGLE_MASK(so->rxid),
908 /* switch to new settings */
909 so->ifindex = ifindex;
910 so->rxid = addr->can_addr.tp.rx_id;
911 so->txid = addr->can_addr.tp.tx_id;
917 if (notify_enetdown) {
918 sk->sk_err = ENETDOWN;
919 if (!sock_flag(sk, SOCK_DEAD))
920 sk->sk_error_report(sk);
926 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr,
929 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
930 struct sock *sk = sock->sk;
931 struct isotp_sock *so = isotp_sk(sk);
936 addr->can_family = AF_CAN;
937 addr->can_ifindex = so->ifindex;
939 *len = sizeof(*addr);
944 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
945 static int isotp_setsockopt(struct socket *sock, int level, int optname,
946 char __user *optval, unsigned int optlen)
948 static int isotp_setsockopt(struct socket *sock, int level, int optname,
949 char __user *optval, int optlen)
952 struct sock *sk = sock->sk;
953 struct isotp_sock *so = isotp_sk(sk);
956 if (level != SOL_CAN_ISOTP)
964 if (optlen != sizeof(struct can_isotp_options))
967 if (copy_from_user(&so->opt, optval, optlen))
971 case CAN_ISOTP_RECV_FC:
972 if (optlen != sizeof(struct can_isotp_fc_options))
975 if (copy_from_user(&so->rxfc, optval, optlen))
979 case CAN_ISOTP_TX_STMIN:
980 if (optlen != sizeof(__u32))
983 if (copy_from_user(&so->force_tx_stmin, optval, optlen))
987 case CAN_ISOTP_RX_STMIN:
988 if (optlen != sizeof(__u32))
991 if (copy_from_user(&so->force_rx_stmin, optval, optlen))
1002 static int isotp_getsockopt(struct socket *sock, int level, int optname,
1003 char __user *optval, int __user *optlen)
1005 struct sock *sk = sock->sk;
1006 struct isotp_sock *so = isotp_sk(sk);
1010 if (level != SOL_CAN_ISOTP)
1012 if (get_user(len, optlen))
1019 case CAN_ISOTP_OPTS:
1020 len = min_t(int, len, sizeof(struct can_isotp_options));
1024 case CAN_ISOTP_RECV_FC:
1025 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
1029 case CAN_ISOTP_TX_STMIN:
1030 len = min_t(int, len, sizeof(__u32));
1031 val = &so->force_tx_stmin;
1034 case CAN_ISOTP_RX_STMIN:
1035 len = min_t(int, len, sizeof(__u32));
1036 val = &so->force_rx_stmin;
1040 return -ENOPROTOOPT;
1043 if (put_user(len, optlen))
1045 if (copy_to_user(optval, val, len))
1051 static int isotp_notifier(struct notifier_block *nb,
1052 unsigned long msg, void *data)
1054 struct net_device *dev = (struct net_device *)data;
1055 struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
1056 struct sock *sk = &so->sk;
1058 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1059 if (dev_net(dev) != &init_net)
1061 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1062 if (dev->nd_net != &init_net)
1066 if (dev->type != ARPHRD_CAN)
1069 if (so->ifindex != dev->ifindex)
1074 case NETDEV_UNREGISTER:
1076 /* remove current filters & unregister */
1078 can_rx_unregister(dev, so->rxid, SINGLE_MASK(so->rxid),
1085 sk->sk_err = ENODEV;
1086 if (!sock_flag(sk, SOCK_DEAD))
1087 sk->sk_error_report(sk);
1091 sk->sk_err = ENETDOWN;
1092 if (!sock_flag(sk, SOCK_DEAD))
1093 sk->sk_error_report(sk);
1101 static int isotp_init(struct sock *sk)
1103 struct isotp_sock *so = isotp_sk(sk);
1108 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1109 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1110 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_RXPAD_CONTENT;
1111 so->opt.txpad_content = CAN_ISOTP_DEFAULT_TXPAD_CONTENT;
1112 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1113 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1114 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1115 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1117 so->rx.state = ISOTP_IDLE;
1118 so->tx.state = ISOTP_IDLE;
1120 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1121 so->rxtimer.function = isotp_rx_timer_handler;
1122 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1123 so->txtimer.function = isotp_tx_timer_handler;
1125 tasklet_init(&so->txtsklet, isotp_tx_timer_tsklet, (unsigned long)so);
1127 init_waitqueue_head(&so->wait);
1129 so->notifier.notifier_call = isotp_notifier;
1130 register_netdevice_notifier(&so->notifier);
1136 static const struct proto_ops isotp_ops = {
1138 .release = isotp_release,
1140 .connect = sock_no_connect,
1141 .socketpair = sock_no_socketpair,
1142 .accept = sock_no_accept,
1143 .getname = isotp_getname,
1144 .poll = datagram_poll,
1145 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1146 .listen = sock_no_listen,
1147 .shutdown = sock_no_shutdown,
1148 .setsockopt = isotp_setsockopt,
1149 .getsockopt = isotp_getsockopt,
1150 .sendmsg = isotp_sendmsg,
1151 .recvmsg = isotp_recvmsg,
1152 .mmap = sock_no_mmap,
1153 .sendpage = sock_no_sendpage,
1156 static struct proto isotp_proto __read_mostly = {
1157 .name = "CAN_ISOTP",
1158 .owner = THIS_MODULE,
1159 .obj_size = sizeof(struct isotp_sock),
1163 static const struct can_proto isotp_can_proto = {
1165 .protocol = CAN_ISOTP,
1166 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
1170 .prot = &isotp_proto,
1173 static __init int isotp_module_init(void)
1179 err = can_proto_register(&isotp_can_proto);
1181 printk(KERN_ERR "can: registration of isotp protocol failed\n");
1186 static __exit void isotp_module_exit(void)
1188 can_proto_unregister(&isotp_can_proto);
1191 module_init(isotp_module_init);
1192 module_exit(isotp_module_exit);