aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2012-06-19 17:47:13 -0400
committerDavid S. Miller <davem@davemloft.net>2012-06-19 17:47:13 -0400
commit3879d4e3977329607a2a8042483140af3581b5c3 (patch)
treec9e8cd2b3a99ddc917bced812fa2265150d45c44 /net
parenta77f4b4acf5b77f038bc11d3ca9b3af6f0124015 (diff)
parentea53fe0c667ad3cae61d4d71d2be41908ac5c0a4 (diff)
Merge branch 'master' of git://gitorious.org/linux-can/linux-can-next
Marc Kleine-Budde says: ==================== here is our second pull request for net-next. In this series Federico Vaga adds a pci driver for c_can/d_can hardware using the existing generic c_can driver. The remaining 6 patches are by Oliver Hartkopp. He adds CANFD support to the CAN stack while keeping binary compatibility for existing applications. CANFD is an extension to the existing CAN standard, it allows longer CAN frames and/or higher data rates. There's no real hardware available yet, but this series adds CANFD support to the vcan driver. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net')
-rw-r--r--net/can/af_can.c116
-rw-r--r--net/can/raw.c50
2 files changed, 135 insertions, 31 deletions
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 6efcd37b4bd..821022a7214 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -41,6 +41,7 @@
41 */ 41 */
42 42
43#include <linux/module.h> 43#include <linux/module.h>
44#include <linux/stddef.h>
44#include <linux/init.h> 45#include <linux/init.h>
45#include <linux/kmod.h> 46#include <linux/kmod.h>
46#include <linux/slab.h> 47#include <linux/slab.h>
@@ -220,30 +221,46 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
220 * -ENOBUFS on full driver queue (see net_xmit_errno()) 221 * -ENOBUFS on full driver queue (see net_xmit_errno())
221 * -ENOMEM when local loopback failed at calling skb_clone() 222 * -ENOMEM when local loopback failed at calling skb_clone()
222 * -EPERM when trying to send on a non-CAN interface 223 * -EPERM when trying to send on a non-CAN interface
224 * -EMSGSIZE CAN frame size is bigger than CAN interface MTU
223 * -EINVAL when the skb->data does not contain a valid CAN frame 225 * -EINVAL when the skb->data does not contain a valid CAN frame
224 */ 226 */
225int can_send(struct sk_buff *skb, int loop) 227int can_send(struct sk_buff *skb, int loop)
226{ 228{
227 struct sk_buff *newskb = NULL; 229 struct sk_buff *newskb = NULL;
228 struct can_frame *cf = (struct can_frame *)skb->data; 230 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
229 int err; 231 int err = -EINVAL;
232
233 if (skb->len == CAN_MTU) {
234 skb->protocol = htons(ETH_P_CAN);
235 if (unlikely(cfd->len > CAN_MAX_DLEN))
236 goto inval_skb;
237 } else if (skb->len == CANFD_MTU) {
238 skb->protocol = htons(ETH_P_CANFD);
239 if (unlikely(cfd->len > CANFD_MAX_DLEN))
240 goto inval_skb;
241 } else
242 goto inval_skb;
230 243
231 if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) { 244 /*
232 kfree_skb(skb); 245 * Make sure the CAN frame can pass the selected CAN netdevice.
233 return -EINVAL; 246 * As structs can_frame and canfd_frame are similar, we can provide
247 * CAN FD frames to legacy CAN drivers as long as the length is <= 8
248 */
249 if (unlikely(skb->len > skb->dev->mtu && cfd->len > CAN_MAX_DLEN)) {
250 err = -EMSGSIZE;
251 goto inval_skb;
234 } 252 }
235 253
236 if (skb->dev->type != ARPHRD_CAN) { 254 if (unlikely(skb->dev->type != ARPHRD_CAN)) {
237 kfree_skb(skb); 255 err = -EPERM;
238 return -EPERM; 256 goto inval_skb;
239 } 257 }
240 258
241 if (!(skb->dev->flags & IFF_UP)) { 259 if (unlikely(!(skb->dev->flags & IFF_UP))) {
242 kfree_skb(skb); 260 err = -ENETDOWN;
243 return -ENETDOWN; 261 goto inval_skb;
244 } 262 }
245 263
246 skb->protocol = htons(ETH_P_CAN);
247 skb_reset_network_header(skb); 264 skb_reset_network_header(skb);
248 skb_reset_transport_header(skb); 265 skb_reset_transport_header(skb);
249 266
@@ -300,6 +317,10 @@ int can_send(struct sk_buff *skb, int loop)
300 can_stats.tx_frames_delta++; 317 can_stats.tx_frames_delta++;
301 318
302 return 0; 319 return 0;
320
321inval_skb:
322 kfree_skb(skb);
323 return err;
303} 324}
304EXPORT_SYMBOL(can_send); 325EXPORT_SYMBOL(can_send);
305 326
@@ -632,24 +653,11 @@ static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
632 return matches; 653 return matches;
633} 654}
634 655
635static int can_rcv(struct sk_buff *skb, struct net_device *dev, 656static void can_receive(struct sk_buff *skb, struct net_device *dev)
636 struct packet_type *pt, struct net_device *orig_dev)
637{ 657{
638 struct dev_rcv_lists *d; 658 struct dev_rcv_lists *d;
639 struct can_frame *cf = (struct can_frame *)skb->data;
640 int matches; 659 int matches;
641 660
642 if (!net_eq(dev_net(dev), &init_net))
643 goto drop;
644
645 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
646 skb->len != sizeof(struct can_frame) ||
647 cf->can_dlc > 8,
648 "PF_CAN: dropped non conform skbuf: "
649 "dev type %d, len %d, can_dlc %d\n",
650 dev->type, skb->len, cf->can_dlc))
651 goto drop;
652
653 /* update statistics */ 661 /* update statistics */
654 can_stats.rx_frames++; 662 can_stats.rx_frames++;
655 can_stats.rx_frames_delta++; 663 can_stats.rx_frames_delta++;
@@ -673,7 +681,49 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
673 can_stats.matches++; 681 can_stats.matches++;
674 can_stats.matches_delta++; 682 can_stats.matches_delta++;
675 } 683 }
684}
676 685
686static int can_rcv(struct sk_buff *skb, struct net_device *dev,
687 struct packet_type *pt, struct net_device *orig_dev)
688{
689 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
690
691 if (unlikely(!net_eq(dev_net(dev), &init_net)))
692 goto drop;
693
694 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
695 skb->len != CAN_MTU ||
696 cfd->len > CAN_MAX_DLEN,
697 "PF_CAN: dropped non conform CAN skbuf: "
698 "dev type %d, len %d, datalen %d\n",
699 dev->type, skb->len, cfd->len))
700 goto drop;
701
702 can_receive(skb, dev);
703 return NET_RX_SUCCESS;
704
705drop:
706 kfree_skb(skb);
707 return NET_RX_DROP;
708}
709
710static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
711 struct packet_type *pt, struct net_device *orig_dev)
712{
713 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
714
715 if (unlikely(!net_eq(dev_net(dev), &init_net)))
716 goto drop;
717
718 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
719 skb->len != CANFD_MTU ||
720 cfd->len > CANFD_MAX_DLEN,
721 "PF_CAN: dropped non conform CAN FD skbuf: "
722 "dev type %d, len %d, datalen %d\n",
723 dev->type, skb->len, cfd->len))
724 goto drop;
725
726 can_receive(skb, dev);
677 return NET_RX_SUCCESS; 727 return NET_RX_SUCCESS;
678 728
679drop: 729drop:
@@ -807,10 +857,14 @@ static int can_notifier(struct notifier_block *nb, unsigned long msg,
807 857
808static struct packet_type can_packet __read_mostly = { 858static struct packet_type can_packet __read_mostly = {
809 .type = cpu_to_be16(ETH_P_CAN), 859 .type = cpu_to_be16(ETH_P_CAN),
810 .dev = NULL,
811 .func = can_rcv, 860 .func = can_rcv,
812}; 861};
813 862
863static struct packet_type canfd_packet __read_mostly = {
864 .type = cpu_to_be16(ETH_P_CANFD),
865 .func = canfd_rcv,
866};
867
814static const struct net_proto_family can_family_ops = { 868static const struct net_proto_family can_family_ops = {
815 .family = PF_CAN, 869 .family = PF_CAN,
816 .create = can_create, 870 .create = can_create,
@@ -824,6 +878,12 @@ static struct notifier_block can_netdev_notifier __read_mostly = {
824 878
825static __init int can_init(void) 879static __init int can_init(void)
826{ 880{
881 /* check for correct padding to be able to use the structs similarly */
882 BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
883 offsetof(struct canfd_frame, len) ||
884 offsetof(struct can_frame, data) !=
885 offsetof(struct canfd_frame, data));
886
827 printk(banner); 887 printk(banner);
828 888
829 memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list)); 889 memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list));
@@ -846,6 +906,7 @@ static __init int can_init(void)
846 sock_register(&can_family_ops); 906 sock_register(&can_family_ops);
847 register_netdevice_notifier(&can_netdev_notifier); 907 register_netdevice_notifier(&can_netdev_notifier);
848 dev_add_pack(&can_packet); 908 dev_add_pack(&can_packet);
909 dev_add_pack(&canfd_packet);
849 910
850 return 0; 911 return 0;
851} 912}
@@ -860,6 +921,7 @@ static __exit void can_exit(void)
860 can_remove_proc(); 921 can_remove_proc();
861 922
862 /* protocol unregister */ 923 /* protocol unregister */
924 dev_remove_pack(&canfd_packet);
863 dev_remove_pack(&can_packet); 925 dev_remove_pack(&can_packet);
864 unregister_netdevice_notifier(&can_netdev_notifier); 926 unregister_netdevice_notifier(&can_netdev_notifier);
865 sock_unregister(PF_CAN); 927 sock_unregister(PF_CAN);
diff --git a/net/can/raw.c b/net/can/raw.c
index 46cca3a91d1..3e9c89356a9 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -82,6 +82,7 @@ struct raw_sock {
82 struct notifier_block notifier; 82 struct notifier_block notifier;
83 int loopback; 83 int loopback;
84 int recv_own_msgs; 84 int recv_own_msgs;
85 int fd_frames;
85 int count; /* number of active filters */ 86 int count; /* number of active filters */
86 struct can_filter dfilter; /* default/single filter */ 87 struct can_filter dfilter; /* default/single filter */
87 struct can_filter *filter; /* pointer to filter(s) */ 88 struct can_filter *filter; /* pointer to filter(s) */
@@ -119,6 +120,14 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
119 if (!ro->recv_own_msgs && oskb->sk == sk) 120 if (!ro->recv_own_msgs && oskb->sk == sk)
120 return; 121 return;
121 122
123 /* do not pass frames with DLC > 8 to a legacy socket */
124 if (!ro->fd_frames) {
125 struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;
126
127 if (unlikely(cfd->len > CAN_MAX_DLEN))
128 return;
129 }
130
122 /* clone the given skb to be able to enqueue it into the rcv queue */ 131 /* clone the given skb to be able to enqueue it into the rcv queue */
123 skb = skb_clone(oskb, GFP_ATOMIC); 132 skb = skb_clone(oskb, GFP_ATOMIC);
124 if (!skb) 133 if (!skb)
@@ -291,6 +300,7 @@ static int raw_init(struct sock *sk)
291 /* set default loopback behaviour */ 300 /* set default loopback behaviour */
292 ro->loopback = 1; 301 ro->loopback = 1;
293 ro->recv_own_msgs = 0; 302 ro->recv_own_msgs = 0;
303 ro->fd_frames = 0;
294 304
295 /* set notifier */ 305 /* set notifier */
296 ro->notifier.notifier_call = raw_notifier; 306 ro->notifier.notifier_call = raw_notifier;
@@ -569,6 +579,15 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
569 579
570 break; 580 break;
571 581
582 case CAN_RAW_FD_FRAMES:
583 if (optlen != sizeof(ro->fd_frames))
584 return -EINVAL;
585
586 if (copy_from_user(&ro->fd_frames, optval, optlen))
587 return -EFAULT;
588
589 break;
590
572 default: 591 default:
573 return -ENOPROTOOPT; 592 return -ENOPROTOOPT;
574 } 593 }
@@ -627,6 +646,12 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
627 val = &ro->recv_own_msgs; 646 val = &ro->recv_own_msgs;
628 break; 647 break;
629 648
649 case CAN_RAW_FD_FRAMES:
650 if (len > sizeof(int))
651 len = sizeof(int);
652 val = &ro->fd_frames;
653 break;
654
630 default: 655 default:
631 return -ENOPROTOOPT; 656 return -ENOPROTOOPT;
632 } 657 }
@@ -662,8 +687,13 @@ static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
662 } else 687 } else
663 ifindex = ro->ifindex; 688 ifindex = ro->ifindex;
664 689
665 if (size != sizeof(struct can_frame)) 690 if (ro->fd_frames) {
666 return -EINVAL; 691 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
692 return -EINVAL;
693 } else {
694 if (unlikely(size != CAN_MTU))
695 return -EINVAL;
696 }
667 697
668 dev = dev_get_by_index(&init_net, ifindex); 698 dev = dev_get_by_index(&init_net, ifindex);
669 if (!dev) 699 if (!dev)
@@ -705,7 +735,9 @@ static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
705 struct msghdr *msg, size_t size, int flags) 735 struct msghdr *msg, size_t size, int flags)
706{ 736{
707 struct sock *sk = sock->sk; 737 struct sock *sk = sock->sk;
738 struct raw_sock *ro = raw_sk(sk);
708 struct sk_buff *skb; 739 struct sk_buff *skb;
740 int rxmtu;
709 int err = 0; 741 int err = 0;
710 int noblock; 742 int noblock;
711 743
@@ -716,10 +748,20 @@ static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
716 if (!skb) 748 if (!skb)
717 return err; 749 return err;
718 750
719 if (size < skb->len) 751 /*
752 * when serving a legacy socket the DLC <= 8 is already checked inside
753 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
754 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
755 */
756 if (!ro->fd_frames)
757 rxmtu = CAN_MTU;
758 else
759 rxmtu = skb->len;
760
761 if (size < rxmtu)
720 msg->msg_flags |= MSG_TRUNC; 762 msg->msg_flags |= MSG_TRUNC;
721 else 763 else
722 size = skb->len; 764 size = rxmtu;
723 765
724 err = memcpy_toiovec(msg->msg_iov, skb->data, size); 766 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
725 if (err < 0) { 767 if (err < 0) {