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/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 <socketcan/can.h>
65 #include <socketcan/can/core.h>
66 #include <socketcan/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 <socketcan/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 ktime_t lastrxcf_tstamp;
136 struct hrtimer rxtimer, txtimer;
137 struct tasklet_struct txtsklet;
138 struct can_isotp_options opt;
139 struct can_isotp_fc_options rxfc, txfc;
140 __u32 force_tx_stmin;
141 __u32 force_rx_stmin;
143 struct notifier_block notifier;
144 wait_queue_head_t wait;
147 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
149 return (struct isotp_sock *)sk;
152 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
154 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
156 if (so->rx.state == ISOTP_WAIT_DATA) {
158 struct sock *sk = &so->sk;
160 /* report 'timeout' */
162 if (!sock_flag(sk, SOCK_DEAD))
163 sk->sk_error_report(sk);
165 DBG("we did not get new data frames in time.\n");
168 so->rx.state = ISOTP_IDLE;
171 return HRTIMER_NORESTART;
174 static int isotp_send_fc(struct sock *sk, int ae)
176 struct net_device *dev;
177 struct sk_buff *nskb;
178 struct can_frame *ncf;
179 struct isotp_sock *so = isotp_sk(sk);
181 nskb = alloc_skb(sizeof(struct can_frame), gfp_any());
185 dev = dev_get_by_index(&init_net, so->ifindex);
192 ncf = (struct can_frame *) nskb->data;
193 skb_put(nskb, sizeof(struct can_frame));
195 /* create & send flow control reply */
196 ncf->can_id = so->txid;
198 if (so->opt.flags & CAN_ISOTP_RX_PADDING) {
199 memset(ncf->data, so->opt.rxpad_content, 8);
204 ncf->data[ae] = N_PCI_FC | ISOTP_FC_CTS;
205 ncf->data[ae+1] = so->rxfc.bs;
206 ncf->data[ae+2] = so->rxfc.stmin;
209 ncf->data[0] = so->opt.ext_address;
214 /* reset blocksize counter */
217 /* reset last CF frame rx timestamp for rx stmin enforcement */
218 so->lastrxcf_tstamp = ktime_set(0,0);
220 /* start rx timeout watchdog */
221 hrtimer_start(&so->rxtimer, ktime_set(1,0), HRTIMER_MODE_REL);
225 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
227 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
229 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
233 memset(addr, 0, sizeof(*addr));
234 addr->can_family = AF_CAN;
235 addr->can_ifindex = skb->dev->ifindex;
237 if (sock_queue_rcv_skb(sk, skb) < 0)
241 static int check_pad(struct isotp_sock *so, struct can_frame *cf,
242 int start_index, __u8 content)
246 /* check datalength code */
247 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && cf->can_dlc != 8)
250 /* check padding content */
251 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
252 for (i = start_index; i < 8; i++)
253 if (cf->data[i] != content)
259 static int isotp_rcv_fc(struct isotp_sock *so, struct can_frame *cf, int ae)
261 if (so->tx.state != ISOTP_WAIT_FC &&
262 so->tx.state != ISOTP_WAIT_FIRST_FC)
265 hrtimer_cancel(&so->txtimer);
267 if ((so->opt.flags & CAN_ISOTP_TX_PADDING) &&
268 check_pad(so, cf, ae+3, so->opt.txpad_content)) {
269 so->tx.state = ISOTP_IDLE;
270 wake_up_interruptible(&so->wait);
274 /* get communication parameters only from the first FC frame */
275 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
277 so->txfc.bs = cf->data[ae+1];
278 so->txfc.stmin = cf->data[ae+2];
280 /* fix wrong STmin values according spec */
281 if ((so->txfc.stmin > 0x7F) &&
282 ((so->txfc.stmin < 0xF1) || (so->txfc.stmin > 0xF9)))
283 so->txfc.stmin = 0x7F;
285 so->tx_gap = ktime_set(0,0);
286 /* add transmission time for CAN frame N_As */
287 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
288 /* add waiting time for consecutive frames N_Cs */
289 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
290 so->tx_gap = ktime_add_ns(so->tx_gap,
292 else if (so->txfc.stmin < 0x80)
293 so->tx_gap = ktime_add_ns(so->tx_gap,
294 so->txfc.stmin * 1000000);
296 so->tx_gap = ktime_add_ns(so->tx_gap,
297 (so->txfc.stmin - 0xF0)
299 so->tx.state = ISOTP_WAIT_FC;
302 DBG("FC frame: FS %d, BS %d, STmin 0x%02X, tx_gap %lld\n",
303 cf->data[ae] & 0x0F & 0x0F, so->txfc.bs, so->txfc.stmin,
304 (long long)so->tx_gap.tv64);
306 switch (cf->data[ae] & 0x0F) {
310 so->tx.state = ISOTP_SENDING;
311 DBG("starting txtimer for sending\n");
312 /* start cyclic timer for sending CF frame */
313 hrtimer_start(&so->txtimer, so->tx_gap,
318 DBG("starting waiting for next FC\n");
319 /* start timer to wait for next FC frame */
320 hrtimer_start(&so->txtimer, ktime_set(1,0),
325 DBG("overflow in receiver side\n");
328 /* stop this tx job. TODO: error reporting? */
329 so->tx.state = ISOTP_IDLE;
330 wake_up_interruptible(&so->wait);
335 static int isotp_rcv_sf(struct sock *sk, struct can_frame *cf, int ae,
338 struct isotp_sock *so = isotp_sk(sk);
339 int len = cf->data[ae] & 0x0F;
340 struct sk_buff *nskb;
342 hrtimer_cancel(&so->rxtimer);
343 so->rx.state = ISOTP_IDLE;
345 if (!len || len > 7 || (ae && len > 6))
348 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
349 check_pad(so, cf, 1+ae+len, so->opt.rxpad_content))
352 nskb = alloc_skb(len, gfp_any());
356 memcpy(skb_put(nskb, len), &cf->data[1+ae], len);
358 nskb->tstamp = skb->tstamp;
359 nskb->dev = skb->dev;
360 isotp_rcv_skb(nskb, sk);
364 static int isotp_rcv_ff(struct sock *sk, struct can_frame *cf, int ae)
366 struct isotp_sock *so = isotp_sk(sk);
369 hrtimer_cancel(&so->rxtimer);
370 so->rx.state = ISOTP_IDLE;
372 if (cf->can_dlc != 8)
375 so->rx.len = (cf->data[ae] & 0x0F) << 8;
376 so->rx.len += cf->data[ae+1];
378 if (so->rx.len + ae < 8)
381 /* copy the first received data bytes */
383 for (i = ae+2; i < 8; i++)
384 so->rx.buf[so->rx.idx++] = cf->data[i];
386 /* initial setup for this pdu receiption */
388 so->rx.state = ISOTP_WAIT_DATA;
390 /* no creation of flow control frames */
391 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
394 /* send our first FC frame */
395 isotp_send_fc(sk, ae);
399 static int isotp_rcv_cf(struct sock *sk, struct can_frame *cf, int ae,
402 struct isotp_sock *so = isotp_sk(sk);
403 struct sk_buff *nskb;
406 if (so->rx.state != ISOTP_WAIT_DATA)
409 /* drop if timestamp gap is less than force_rx_stmin nano secs */
410 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
412 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
416 so->lastrxcf_tstamp = skb->tstamp;
419 hrtimer_cancel(&so->rxtimer);
421 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
422 DBG("wrong sn %d. expected %d.\n",
423 cf->data[ae] & 0x0F, so->rx.sn);
424 /* some error reporting? */
425 so->rx.state = ISOTP_IDLE;
431 for (i = ae+1; i < 8; i++) {
432 so->rx.buf[so->rx.idx++] = cf->data[i];
433 if (so->rx.idx >= so->rx.len)
437 if (so->rx.idx >= so->rx.len) {
440 so->rx.state = ISOTP_IDLE;
442 if ((so->opt.flags & CAN_ISOTP_RX_PADDING) &&
443 check_pad(so, cf, i+1, so->opt.rxpad_content))
446 nskb = alloc_skb(so->rx.len, gfp_any());
450 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
453 nskb->tstamp = skb->tstamp;
454 nskb->dev = skb->dev;
455 isotp_rcv_skb(nskb, sk);
459 /* no creation of flow control frames */
460 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
463 /* perform blocksize handling, if enabled */
464 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
466 /* start rx timeout watchdog */
467 hrtimer_start(&so->rxtimer, ktime_set(1,0),
472 /* we reached the specified blocksize so->rxfc.bs */
473 isotp_send_fc(sk, ae);
477 static void isotp_rcv(struct sk_buff *skb, void *data)
479 struct sock *sk = (struct sock *)data;
480 struct isotp_sock *so = isotp_sk(sk);
481 struct can_frame *cf;
482 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
485 /* read CAN frame and free skbuff */
486 BUG_ON(skb->len != sizeof(struct can_frame));
487 cf = (struct can_frame *) skb->data;
489 /* if enabled: check receiption of my configured extended address */
490 if (ae && cf->data[0] != so->opt.ext_address)
493 n_pci_type = cf->data[ae] & 0xF0;
495 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
496 /* check rx/tx path half duplex expectations */
497 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
498 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
502 switch (n_pci_type) {
504 /* tx path: flow control frame containing the FC parameters */
505 isotp_rcv_fc(so, cf, ae);
509 /* rx path: single frame */
510 isotp_rcv_sf(sk, cf, ae, skb);
514 /* rx path: first frame */
515 isotp_rcv_ff(sk, cf, ae);
519 /* rx path: consecutive frame */
520 isotp_rcv_cf(sk, cf, ae, skb);
525 static void isotp_fill_dataframe(struct can_frame *cf, struct isotp_sock *so,
528 unsigned char space = 7 - ae;
529 int num = min_t(int, so->tx.len - so->tx.idx, space);
532 cf->can_id = so->txid;
534 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
536 memset(cf->data, so->opt.txpad_content, 8);
540 cf->can_dlc = num + 1 + ae;
543 for (i = 0; i < num; i++)
544 cf->data[i+ae+1] = so->tx.buf[so->tx.idx++];
547 cf->data[0] = so->opt.ext_address;
550 static void isotp_create_fframe(struct can_frame *cf, struct isotp_sock *so,
555 cf->can_id = so->txid;
558 cf->data[0] = so->opt.ext_address;
560 /* N_PCI bytes with FF_DL data length */
561 cf->data[ae] = (u8) (so->tx.len>>8) | N_PCI_FF;
562 cf->data[ae+1] = (u8) so->tx.len & 0xFFU;
564 /* add first 5 or 6 data bytes depending on ae */
565 for (i = ae+2; i < 8; i++)
566 cf->data[i] = so->tx.buf[so->tx.idx++];
569 so->tx.state = ISOTP_WAIT_FIRST_FC;
572 static void isotp_tx_timer_tsklet(unsigned long data)
574 struct isotp_sock *so = (struct isotp_sock *)data;
575 struct sock *sk = &so->sk;
577 struct net_device *dev;
578 struct can_frame *cf;
579 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
581 switch (so->tx.state) {
584 case ISOTP_WAIT_FIRST_FC:
586 /* we did not get any flow control frame in time */
588 DBG("we did not get FC frame in time.\n");
591 /* report 'communication error on send' */
593 if (!sock_flag(sk, SOCK_DEAD))
594 sk->sk_error_report(sk);
597 so->tx.state = ISOTP_IDLE;
598 wake_up_interruptible(&so->wait);
603 /* push out the next segmented pdu */
605 DBG("next pdu to send.\n");
607 dev = dev_get_by_index(&init_net, so->ifindex);
612 skb = alloc_skb(sizeof(*cf), gfp_any());
618 cf = (struct can_frame *)skb->data;
619 skb_put(skb, sizeof(*cf));
621 /* create consecutive frame */
622 isotp_fill_dataframe(cf, so, ae);
624 /* place consecutive frame N_PCI in appropriate index */
625 cf->data[ae] = N_PCI_CF | so->tx.sn++;
633 if (so->tx.idx >= so->tx.len) {
635 DBG("we are done\n");
636 so->tx.state = ISOTP_IDLE;
638 wake_up_interruptible(&so->wait);
642 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
643 /* stop and wait for FC */
644 DBG("BS stop and wait for FC\n");
645 so->tx.state = ISOTP_WAIT_FC;
647 hrtimer_start(&so->txtimer,
648 ktime_add(ktime_get(), ktime_set(1,0)),
653 /* no gap between data frames needed => use burst mode */
654 if (!so->tx_gap.tv64)
657 /* start timer to send next data frame with correct delay */
659 hrtimer_start(&so->txtimer,
660 ktime_add(ktime_get(), so->tx_gap),
669 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
671 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
673 tasklet_schedule(&so->txtsklet);
675 return HRTIMER_NORESTART;
678 static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
679 struct msghdr *msg, size_t size)
681 struct sock *sk = sock->sk;
682 struct isotp_sock *so = isotp_sk(sk);
684 struct net_device *dev;
685 struct can_frame *cf;
686 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR)? 1:0;
690 return -EADDRNOTAVAIL;
692 /* we do not support multiple buffers - for now */
693 if (so->tx.state != ISOTP_IDLE) {
694 if (msg->msg_flags & MSG_DONTWAIT)
697 /* wait for complete transmission of current pdu */
698 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
701 if (!size || size > 4095)
704 err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
708 dev = dev_get_by_index(&init_net, so->ifindex);
712 skb = sock_alloc_send_skb(sk, sizeof(*cf),
713 msg->msg_flags & MSG_DONTWAIT, &err);
719 so->tx.state = ISOTP_SENDING;
723 cf = (struct can_frame *)skb->data;
724 skb_put(skb, sizeof(*cf));
726 /* check for single frame transmission */
727 if (size <= 7 - ae) {
729 isotp_fill_dataframe(cf, so, ae);
731 /* place single frame N_PCI in appropriate index */
732 cf->data[ae] = size | N_PCI_SF;
734 so->tx.state = ISOTP_IDLE;
735 wake_up_interruptible(&so->wait);
737 /* send first frame and wait for FC */
739 isotp_create_fframe(cf, so, ae);
741 DBG("starting txtimer for fc\n");
742 /* start timeout for FC */
743 hrtimer_start(&so->txtimer, ktime_set(1,0), HRTIMER_MODE_REL);
746 /* send the first or only CAN frame */
749 err = can_send(skb, 1);
757 static int isotp_recvmsg(struct kiocb *iocb, struct socket *sock,
758 struct msghdr *msg, size_t size, int flags)
760 struct sock *sk = sock->sk;
765 noblock = flags & MSG_DONTWAIT;
766 flags &= ~MSG_DONTWAIT;
768 skb = skb_recv_datagram(sk, flags, noblock, &err);
773 msg->msg_flags |= MSG_TRUNC;
777 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
779 skb_free_datagram(sk, skb);
783 sock_recv_timestamp(msg, sk, skb);
786 msg->msg_namelen = sizeof(struct sockaddr_can);
787 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
790 skb_free_datagram(sk, skb);
795 static int isotp_release(struct socket *sock)
797 struct sock *sk = sock->sk;
798 struct isotp_sock *so = isotp_sk(sk);
800 /* wait for complete transmission of current pdu */
801 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
803 unregister_netdevice_notifier(&so->notifier);
807 hrtimer_cancel(&so->txtimer);
808 hrtimer_cancel(&so->rxtimer);
809 tasklet_kill(&so->txtsklet);
811 /* remove current filters & unregister */
814 struct net_device *dev;
816 dev = dev_get_by_index(&init_net, so->ifindex);
818 can_rx_unregister(dev, so->rxid,
819 SINGLE_MASK(so->rxid),
838 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
840 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
841 struct sock *sk = sock->sk;
842 struct isotp_sock *so = isotp_sk(sk);
844 struct net_device *dev;
846 int notify_enetdown = 0;
848 if (len < sizeof(*addr))
851 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id)
852 return -EADDRNOTAVAIL;
854 if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) &
855 (CAN_ERR_FLAG | CAN_RTR_FLAG))
856 return -EADDRNOTAVAIL;
858 if (!addr->can_ifindex)
863 if (so->bound && addr->can_ifindex == so->ifindex &&
864 addr->can_addr.tp.rx_id == so->rxid &&
865 addr->can_addr.tp.tx_id == so->txid)
868 dev = dev_get_by_index(&init_net, addr->can_ifindex);
873 if (dev->type != ARPHRD_CAN) {
878 if (!(dev->flags & IFF_UP))
881 ifindex = dev->ifindex;
883 can_rx_register(dev, addr->can_addr.tp.rx_id,
884 SINGLE_MASK(addr->can_addr.tp.rx_id),
885 isotp_rcv, sk, "isotp");
889 /* unregister old filter */
891 dev = dev_get_by_index(&init_net, so->ifindex);
893 can_rx_unregister(dev, so->rxid,
894 SINGLE_MASK(so->rxid),
901 /* switch to new settings */
902 so->ifindex = ifindex;
903 so->rxid = addr->can_addr.tp.rx_id;
904 so->txid = addr->can_addr.tp.tx_id;
910 if (notify_enetdown) {
911 sk->sk_err = ENETDOWN;
912 if (!sock_flag(sk, SOCK_DEAD))
913 sk->sk_error_report(sk);
919 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr,
922 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
923 struct sock *sk = sock->sk;
924 struct isotp_sock *so = isotp_sk(sk);
929 addr->can_family = AF_CAN;
930 addr->can_ifindex = so->ifindex;
932 *len = sizeof(*addr);
937 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
938 static int isotp_setsockopt(struct socket *sock, int level, int optname,
939 char __user *optval, unsigned int optlen)
941 static int isotp_setsockopt(struct socket *sock, int level, int optname,
942 char __user *optval, int optlen)
945 struct sock *sk = sock->sk;
946 struct isotp_sock *so = isotp_sk(sk);
949 if (level != SOL_CAN_ISOTP)
957 if (optlen != sizeof(struct can_isotp_options))
960 if (copy_from_user(&so->opt, optval, optlen))
964 case CAN_ISOTP_RECV_FC:
965 if (optlen != sizeof(struct can_isotp_fc_options))
968 if (copy_from_user(&so->rxfc, optval, optlen))
972 case CAN_ISOTP_TX_STMIN:
973 if (optlen != sizeof(__u32))
976 if (copy_from_user(&so->force_tx_stmin, optval, optlen))
980 case CAN_ISOTP_RX_STMIN:
981 if (optlen != sizeof(__u32))
984 if (copy_from_user(&so->force_rx_stmin, optval, optlen))
995 static int isotp_getsockopt(struct socket *sock, int level, int optname,
996 char __user *optval, int __user *optlen)
998 struct sock *sk = sock->sk;
999 struct isotp_sock *so = isotp_sk(sk);
1003 if (level != SOL_CAN_ISOTP)
1005 if (get_user(len, optlen))
1012 case CAN_ISOTP_OPTS:
1013 len = min_t(int, len, sizeof(struct can_isotp_options));
1017 case CAN_ISOTP_RECV_FC:
1018 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
1022 case CAN_ISOTP_TX_STMIN:
1023 len = min_t(int, len, sizeof(__u32));
1024 val = &so->force_tx_stmin;
1027 case CAN_ISOTP_RX_STMIN:
1028 len = min_t(int, len, sizeof(__u32));
1029 val = &so->force_rx_stmin;
1033 return -ENOPROTOOPT;
1036 if (put_user(len, optlen))
1038 if (copy_to_user(optval, val, len))
1044 static int isotp_notifier(struct notifier_block *nb,
1045 unsigned long msg, void *data)
1047 struct net_device *dev = (struct net_device *)data;
1048 struct isotp_sock *so = container_of(nb, struct isotp_sock, notifier);
1049 struct sock *sk = &so->sk;
1051 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1052 if (dev_net(dev) != &init_net)
1054 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1055 if (dev->nd_net != &init_net)
1059 if (dev->type != ARPHRD_CAN)
1062 if (so->ifindex != dev->ifindex)
1067 case NETDEV_UNREGISTER:
1069 /* remove current filters & unregister */
1071 can_rx_unregister(dev, so->rxid, SINGLE_MASK(so->rxid),
1078 sk->sk_err = ENODEV;
1079 if (!sock_flag(sk, SOCK_DEAD))
1080 sk->sk_error_report(sk);
1084 sk->sk_err = ENETDOWN;
1085 if (!sock_flag(sk, SOCK_DEAD))
1086 sk->sk_error_report(sk);
1094 static int isotp_init(struct sock *sk)
1096 struct isotp_sock *so = isotp_sk(sk);
1101 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1102 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1103 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_RXPAD_CONTENT;
1104 so->opt.txpad_content = CAN_ISOTP_DEFAULT_TXPAD_CONTENT;
1105 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1106 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1107 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1108 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1110 so->rx.state = ISOTP_IDLE;
1111 so->tx.state = ISOTP_IDLE;
1113 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1114 so->rxtimer.function = isotp_rx_timer_handler;
1115 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1116 so->txtimer.function = isotp_tx_timer_handler;
1118 tasklet_init(&so->txtsklet, isotp_tx_timer_tsklet, (unsigned long)so);
1120 init_waitqueue_head(&so->wait);
1122 so->notifier.notifier_call = isotp_notifier;
1123 register_netdevice_notifier(&so->notifier);
1129 static struct proto_ops isotp_ops __read_mostly = {
1131 .release = isotp_release,
1133 .connect = sock_no_connect,
1134 .socketpair = sock_no_socketpair,
1135 .accept = sock_no_accept,
1136 .getname = isotp_getname,
1137 .poll = datagram_poll,
1138 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1139 .listen = sock_no_listen,
1140 .shutdown = sock_no_shutdown,
1141 .setsockopt = isotp_setsockopt,
1142 .getsockopt = isotp_getsockopt,
1143 .sendmsg = isotp_sendmsg,
1144 .recvmsg = isotp_recvmsg,
1145 .mmap = sock_no_mmap,
1146 .sendpage = sock_no_sendpage,
1149 static struct proto isotp_proto __read_mostly = {
1150 .name = "CAN_ISOTP",
1151 .owner = THIS_MODULE,
1152 .obj_size = sizeof(struct isotp_sock),
1156 static struct can_proto isotp_can_proto __read_mostly = {
1158 .protocol = CAN_ISOTP,
1161 .prot = &isotp_proto,
1164 static __init int isotp_module_init(void)
1170 err = can_proto_register(&isotp_can_proto);
1172 printk(KERN_ERR "can: registration of isotp protocol failed\n");
1177 static __exit void isotp_module_exit(void)
1179 can_proto_unregister(&isotp_can_proto);
1182 module_init(isotp_module_init);
1183 module_exit(isotp_module_exit);