1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2010-2011 EIA Electronics,
3 // Pieter Beyens <pieter.beyens@eia.be>
4 // Copyright (c) 2010-2011 EIA Electronics,
5 // Kurt Van Dijck <kurt.van.dijck@eia.be>
6 // Copyright (c) 2018 Protonic,
7 // Robin van der Gracht <robin@protonic.nl>
8 // Copyright (c) 2017-2019 Pengutronix,
9 // Marc Kleine-Budde <kernel@pengutronix.de>
10 // Copyright (c) 2017-2019 Pengutronix,
11 // Oleksij Rempel <kernel@pengutronix.de>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/can/can-ml.h>
16 #include <linux/can/core.h>
17 #include <linux/can/skb.h>
18 #include <linux/errqueue.h>
19 #include <linux/if_arp.h>
21 #include "j1939-priv.h"
23 #define J1939_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.j1939)
25 /* conversion function between struct sock::sk_priority from linux and
26 * j1939 priority field
28 static inline priority_t j1939_prio(u32 sk_priority)
30 sk_priority = min(sk_priority, 7U);
32 return 7 - sk_priority;
35 static inline u32 j1939_to_sk_priority(priority_t prio)
40 /* function to see if pgn is to be evaluated */
41 static inline bool j1939_pgn_is_valid(pgn_t pgn)
43 return pgn <= J1939_PGN_MAX;
46 /* test function to avoid non-zero DA placeholder for pdu1 pgn's */
47 static inline bool j1939_pgn_is_clean_pdu(pgn_t pgn)
49 if (j1939_pgn_is_pdu1(pgn))
55 static inline void j1939_sock_pending_add(struct sock *sk)
57 struct j1939_sock *jsk = j1939_sk(sk);
59 atomic_inc(&jsk->skb_pending);
62 static int j1939_sock_pending_get(struct sock *sk)
64 struct j1939_sock *jsk = j1939_sk(sk);
66 return atomic_read(&jsk->skb_pending);
69 void j1939_sock_pending_del(struct sock *sk)
71 struct j1939_sock *jsk = j1939_sk(sk);
73 /* atomic_dec_return returns the new value */
74 if (!atomic_dec_return(&jsk->skb_pending))
75 wake_up(&jsk->waitq); /* no pending SKB's */
78 static void j1939_jsk_add(struct j1939_priv *priv, struct j1939_sock *jsk)
80 jsk->state |= J1939_SOCK_BOUND;
83 spin_lock_bh(&priv->j1939_socks_lock);
84 list_add_tail(&jsk->list, &priv->j1939_socks);
85 spin_unlock_bh(&priv->j1939_socks_lock);
88 static void j1939_jsk_del(struct j1939_priv *priv, struct j1939_sock *jsk)
90 spin_lock_bh(&priv->j1939_socks_lock);
91 list_del_init(&jsk->list);
92 spin_unlock_bh(&priv->j1939_socks_lock);
95 jsk->state &= ~J1939_SOCK_BOUND;
98 static bool j1939_sk_queue_session(struct j1939_session *session)
100 struct j1939_sock *jsk = j1939_sk(session->sk);
103 spin_lock_bh(&jsk->sk_session_queue_lock);
104 empty = list_empty(&jsk->sk_session_queue);
105 j1939_session_get(session);
106 list_add_tail(&session->sk_session_queue_entry, &jsk->sk_session_queue);
107 spin_unlock_bh(&jsk->sk_session_queue_lock);
108 j1939_sock_pending_add(&jsk->sk);
114 j1939_session *j1939_sk_get_incomplete_session(struct j1939_sock *jsk)
116 struct j1939_session *session = NULL;
118 spin_lock_bh(&jsk->sk_session_queue_lock);
119 if (!list_empty(&jsk->sk_session_queue)) {
120 session = list_last_entry(&jsk->sk_session_queue,
121 struct j1939_session,
122 sk_session_queue_entry);
123 if (session->total_queued_size == session->total_message_size)
126 j1939_session_get(session);
128 spin_unlock_bh(&jsk->sk_session_queue_lock);
133 static void j1939_sk_queue_drop_all(struct j1939_priv *priv,
134 struct j1939_sock *jsk, int err)
136 struct j1939_session *session, *tmp;
138 netdev_dbg(priv->ndev, "%s: err: %i\n", __func__, err);
139 spin_lock_bh(&jsk->sk_session_queue_lock);
140 list_for_each_entry_safe(session, tmp, &jsk->sk_session_queue,
141 sk_session_queue_entry) {
142 list_del_init(&session->sk_session_queue_entry);
144 j1939_session_put(session);
146 spin_unlock_bh(&jsk->sk_session_queue_lock);
149 static void j1939_sk_queue_activate_next_locked(struct j1939_session *session)
151 struct j1939_sock *jsk;
152 struct j1939_session *first;
155 /* RX-Session don't have a socket (yet) */
159 jsk = j1939_sk(session->sk);
160 lockdep_assert_held(&jsk->sk_session_queue_lock);
164 first = list_first_entry_or_null(&jsk->sk_session_queue,
165 struct j1939_session,
166 sk_session_queue_entry);
168 /* Some else has already activated the next session */
169 if (first != session)
173 list_del_init(&first->sk_session_queue_entry);
174 j1939_session_put(first);
175 first = list_first_entry_or_null(&jsk->sk_session_queue,
176 struct j1939_session,
177 sk_session_queue_entry);
181 if (j1939_session_activate(first)) {
182 netdev_warn_once(first->priv->ndev,
183 "%s: 0x%p: Identical session is already activated.\n",
188 /* Give receiver some time (arbitrary chosen) to recover */
192 time_ms = 10 + get_random_u32_below(16);
194 j1939_tp_schedule_txtimer(first, time_ms);
198 void j1939_sk_queue_activate_next(struct j1939_session *session)
200 struct j1939_sock *jsk;
205 jsk = j1939_sk(session->sk);
207 spin_lock_bh(&jsk->sk_session_queue_lock);
208 j1939_sk_queue_activate_next_locked(session);
209 spin_unlock_bh(&jsk->sk_session_queue_lock);
212 static bool j1939_sk_match_dst(struct j1939_sock *jsk,
213 const struct j1939_sk_buff_cb *skcb)
215 if ((jsk->state & J1939_SOCK_PROMISC))
218 /* Destination address filter */
219 if (jsk->addr.src_name && skcb->addr.dst_name) {
220 if (jsk->addr.src_name != skcb->addr.dst_name)
223 /* receive (all sockets) if
224 * - all packages that match our bind() address
225 * - all broadcast on a socket if SO_BROADCAST
228 if (j1939_address_is_unicast(skcb->addr.da)) {
229 if (jsk->addr.sa != skcb->addr.da)
231 } else if (!sock_flag(&jsk->sk, SOCK_BROADCAST)) {
232 /* receiving broadcast without SO_BROADCAST
233 * flag is not allowed
239 /* Source address filter */
240 if (jsk->state & J1939_SOCK_CONNECTED) {
241 /* receive (all sockets) if
242 * - all packages that match our connect() name or address
244 if (jsk->addr.dst_name && skcb->addr.src_name) {
245 if (jsk->addr.dst_name != skcb->addr.src_name)
248 if (jsk->addr.da != skcb->addr.sa)
254 if (j1939_pgn_is_valid(jsk->pgn_rx_filter) &&
255 jsk->pgn_rx_filter != skcb->addr.pgn)
261 /* matches skb control buffer (addr) with a j1939 filter */
262 static bool j1939_sk_match_filter(struct j1939_sock *jsk,
263 const struct j1939_sk_buff_cb *skcb)
265 const struct j1939_filter *f = jsk->filters;
266 int nfilter = jsk->nfilters;
269 /* receive all when no filters are assigned */
272 for (; nfilter; ++f, --nfilter) {
273 if ((skcb->addr.pgn & f->pgn_mask) != f->pgn)
275 if ((skcb->addr.sa & f->addr_mask) != f->addr)
277 if ((skcb->addr.src_name & f->name_mask) != f->name)
284 static bool j1939_sk_recv_match_one(struct j1939_sock *jsk,
285 const struct j1939_sk_buff_cb *skcb)
287 if (!(jsk->state & J1939_SOCK_BOUND))
290 if (!j1939_sk_match_dst(jsk, skcb))
293 if (!j1939_sk_match_filter(jsk, skcb))
299 static void j1939_sk_recv_one(struct j1939_sock *jsk, struct sk_buff *oskb)
301 const struct j1939_sk_buff_cb *oskcb = j1939_skb_to_cb(oskb);
302 struct j1939_sk_buff_cb *skcb;
305 if (oskb->sk == &jsk->sk)
308 if (!j1939_sk_recv_match_one(jsk, oskcb))
311 skb = skb_clone(oskb, GFP_ATOMIC);
313 pr_warn("skb clone failed\n");
316 can_skb_set_owner(skb, oskb->sk);
318 skcb = j1939_skb_to_cb(skb);
319 skcb->msg_flags &= ~(MSG_DONTROUTE);
321 skcb->msg_flags |= MSG_DONTROUTE;
323 if (sock_queue_rcv_skb(&jsk->sk, skb) < 0)
327 bool j1939_sk_recv_match(struct j1939_priv *priv, struct j1939_sk_buff_cb *skcb)
329 struct j1939_sock *jsk;
332 spin_lock_bh(&priv->j1939_socks_lock);
333 list_for_each_entry(jsk, &priv->j1939_socks, list) {
334 match = j1939_sk_recv_match_one(jsk, skcb);
338 spin_unlock_bh(&priv->j1939_socks_lock);
343 void j1939_sk_recv(struct j1939_priv *priv, struct sk_buff *skb)
345 struct j1939_sock *jsk;
347 spin_lock_bh(&priv->j1939_socks_lock);
348 list_for_each_entry(jsk, &priv->j1939_socks, list) {
349 j1939_sk_recv_one(jsk, skb);
351 spin_unlock_bh(&priv->j1939_socks_lock);
354 static void j1939_sk_sock_destruct(struct sock *sk)
356 struct j1939_sock *jsk = j1939_sk(sk);
358 /* This function will be called by the generic networking code, when
359 * the socket is ultimately closed (sk->sk_destruct).
362 * - processing a received CAN frame
363 * (can_receive -> j1939_can_recv)
364 * and accessing j1939_priv
367 * (j1939_can_rx_unregister -> can_rx_unregister)
368 * and calling the final j1939_priv_put()
370 * is avoided by calling the final j1939_priv_put() from this
371 * RCU deferred cleanup call.
374 j1939_priv_put(jsk->priv);
378 /* call generic CAN sock destruct */
379 can_sock_destruct(sk);
382 static int j1939_sk_init(struct sock *sk)
384 struct j1939_sock *jsk = j1939_sk(sk);
386 /* Ensure that "sk" is first member in "struct j1939_sock", so that we
387 * can skip it during memset().
389 BUILD_BUG_ON(offsetof(struct j1939_sock, sk) != 0);
390 memset((void *)jsk + sizeof(jsk->sk), 0x0,
391 sizeof(*jsk) - sizeof(jsk->sk));
393 INIT_LIST_HEAD(&jsk->list);
394 init_waitqueue_head(&jsk->waitq);
395 jsk->sk.sk_priority = j1939_to_sk_priority(6);
396 jsk->sk.sk_reuse = 1; /* per default */
397 jsk->addr.sa = J1939_NO_ADDR;
398 jsk->addr.da = J1939_NO_ADDR;
399 jsk->addr.pgn = J1939_NO_PGN;
400 jsk->pgn_rx_filter = J1939_NO_PGN;
401 atomic_set(&jsk->skb_pending, 0);
402 spin_lock_init(&jsk->sk_session_queue_lock);
403 INIT_LIST_HEAD(&jsk->sk_session_queue);
405 /* j1939_sk_sock_destruct() depends on SOCK_RCU_FREE flag */
406 sock_set_flag(sk, SOCK_RCU_FREE);
407 sk->sk_destruct = j1939_sk_sock_destruct;
408 sk->sk_protocol = CAN_J1939;
413 static int j1939_sk_sanity_check(struct sockaddr_can *addr, int len)
416 return -EDESTADDRREQ;
417 if (len < J1939_MIN_NAMELEN)
419 if (addr->can_family != AF_CAN)
421 if (!addr->can_ifindex)
423 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) &&
424 !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn))
430 static int j1939_sk_bind(struct socket *sock, struct sockaddr *uaddr, int len)
432 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
433 struct j1939_sock *jsk = j1939_sk(sock->sk);
434 struct j1939_priv *priv;
439 ret = j1939_sk_sanity_check(addr, len);
449 /* Already bound to an interface? */
450 if (jsk->state & J1939_SOCK_BOUND) {
451 /* A re-bind() to a different interface is not
454 if (jsk->ifindex != addr->can_ifindex) {
456 goto out_release_sock;
459 /* drop old references */
460 j1939_jsk_del(priv, jsk);
461 j1939_local_ecu_put(priv, jsk->addr.src_name, jsk->addr.sa);
463 struct can_ml_priv *can_ml;
464 struct net_device *ndev;
466 ndev = dev_get_by_index(net, addr->can_ifindex);
469 goto out_release_sock;
472 can_ml = can_get_ml_priv(ndev);
476 goto out_release_sock;
479 if (!(ndev->flags & IFF_UP)) {
482 goto out_release_sock;
485 priv = j1939_netdev_start(ndev);
489 goto out_release_sock;
492 jsk->ifindex = addr->can_ifindex;
494 /* the corresponding j1939_priv_put() is called via
495 * sk->sk_destruct, which points to j1939_sk_sock_destruct()
497 j1939_priv_get(priv);
501 /* set default transmit pgn */
502 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
503 jsk->pgn_rx_filter = addr->can_addr.j1939.pgn;
504 jsk->addr.src_name = addr->can_addr.j1939.name;
505 jsk->addr.sa = addr->can_addr.j1939.addr;
507 /* get new references */
508 ret = j1939_local_ecu_get(priv, jsk->addr.src_name, jsk->addr.sa);
510 j1939_netdev_stop(priv);
511 goto out_release_sock;
514 j1939_jsk_add(priv, jsk);
516 out_release_sock: /* fall through */
517 release_sock(sock->sk);
522 static int j1939_sk_connect(struct socket *sock, struct sockaddr *uaddr,
525 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
526 struct j1939_sock *jsk = j1939_sk(sock->sk);
529 ret = j1939_sk_sanity_check(addr, len);
535 /* bind() before connect() is mandatory */
536 if (!(jsk->state & J1939_SOCK_BOUND)) {
538 goto out_release_sock;
541 /* A connect() to a different interface is not supported. */
542 if (jsk->ifindex != addr->can_ifindex) {
544 goto out_release_sock;
547 if (!addr->can_addr.j1939.name &&
548 addr->can_addr.j1939.addr == J1939_NO_ADDR &&
549 !sock_flag(&jsk->sk, SOCK_BROADCAST)) {
550 /* broadcast, but SO_BROADCAST not set */
552 goto out_release_sock;
555 jsk->addr.dst_name = addr->can_addr.j1939.name;
556 jsk->addr.da = addr->can_addr.j1939.addr;
558 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
559 jsk->addr.pgn = addr->can_addr.j1939.pgn;
561 jsk->state |= J1939_SOCK_CONNECTED;
563 out_release_sock: /* fall through */
564 release_sock(sock->sk);
569 static void j1939_sk_sock2sockaddr_can(struct sockaddr_can *addr,
570 const struct j1939_sock *jsk, int peer)
572 /* There are two holes (2 bytes and 3 bytes) to clear to avoid
573 * leaking kernel information to user space.
575 memset(addr, 0, J1939_MIN_NAMELEN);
577 addr->can_family = AF_CAN;
578 addr->can_ifindex = jsk->ifindex;
579 addr->can_addr.j1939.pgn = jsk->addr.pgn;
581 addr->can_addr.j1939.name = jsk->addr.dst_name;
582 addr->can_addr.j1939.addr = jsk->addr.da;
584 addr->can_addr.j1939.name = jsk->addr.src_name;
585 addr->can_addr.j1939.addr = jsk->addr.sa;
589 static int j1939_sk_getname(struct socket *sock, struct sockaddr *uaddr,
592 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
593 struct sock *sk = sock->sk;
594 struct j1939_sock *jsk = j1939_sk(sk);
599 if (peer && !(jsk->state & J1939_SOCK_CONNECTED)) {
600 ret = -EADDRNOTAVAIL;
604 j1939_sk_sock2sockaddr_can(addr, jsk, peer);
605 ret = J1939_MIN_NAMELEN;
613 static int j1939_sk_release(struct socket *sock)
615 struct sock *sk = sock->sk;
616 struct j1939_sock *jsk;
624 if (jsk->state & J1939_SOCK_BOUND) {
625 struct j1939_priv *priv = jsk->priv;
627 if (wait_event_interruptible(jsk->waitq,
628 !j1939_sock_pending_get(&jsk->sk))) {
629 j1939_cancel_active_session(priv, sk);
630 j1939_sk_queue_drop_all(priv, jsk, ESHUTDOWN);
633 j1939_jsk_del(priv, jsk);
635 j1939_local_ecu_put(priv, jsk->addr.src_name,
638 j1939_netdev_stop(priv);
651 static int j1939_sk_setsockopt_flag(struct j1939_sock *jsk, sockptr_t optval,
652 unsigned int optlen, int flag)
656 if (optlen != sizeof(tmp))
658 if (copy_from_sockptr(&tmp, optval, optlen))
665 release_sock(&jsk->sk);
669 static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
670 sockptr_t optval, unsigned int optlen)
672 struct sock *sk = sock->sk;
673 struct j1939_sock *jsk = j1939_sk(sk);
674 int tmp, count = 0, ret = 0;
675 struct j1939_filter *filters = NULL, *ofilters;
677 if (level != SOL_CAN_J1939)
681 case SO_J1939_FILTER:
682 if (!sockptr_is_null(optval) && optlen != 0) {
683 struct j1939_filter *f;
686 if (optlen % sizeof(*filters) != 0)
689 if (optlen > J1939_FILTER_MAX *
690 sizeof(struct j1939_filter))
693 count = optlen / sizeof(*filters);
694 filters = memdup_sockptr(optval, optlen);
696 return PTR_ERR(filters);
698 for (f = filters, c = count; c; f++, c--) {
699 f->name &= f->name_mask;
700 f->pgn &= f->pgn_mask;
701 f->addr &= f->addr_mask;
706 ofilters = jsk->filters;
707 jsk->filters = filters;
708 jsk->nfilters = count;
709 release_sock(&jsk->sk);
712 case SO_J1939_PROMISC:
713 return j1939_sk_setsockopt_flag(jsk, optval, optlen,
715 case SO_J1939_ERRQUEUE:
716 ret = j1939_sk_setsockopt_flag(jsk, optval, optlen,
717 J1939_SOCK_ERRQUEUE);
721 if (!(jsk->state & J1939_SOCK_ERRQUEUE))
722 skb_queue_purge(&sk->sk_error_queue);
724 case SO_J1939_SEND_PRIO:
725 if (optlen != sizeof(tmp))
727 if (copy_from_sockptr(&tmp, optval, optlen))
729 if (tmp < 0 || tmp > 7)
731 if (tmp < 2 && !capable(CAP_NET_ADMIN))
734 jsk->sk.sk_priority = j1939_to_sk_priority(tmp);
735 release_sock(&jsk->sk);
742 static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
743 char __user *optval, int __user *optlen)
745 struct sock *sk = sock->sk;
746 struct j1939_sock *jsk = j1939_sk(sk);
748 /* set defaults for using 'int' properties */
750 int len = sizeof(tmp);
753 if (level != SOL_CAN_J1939)
755 if (get_user(ulen, optlen))
762 case SO_J1939_PROMISC:
763 tmp = (jsk->state & J1939_SOCK_PROMISC) ? 1 : 0;
765 case SO_J1939_ERRQUEUE:
766 tmp = (jsk->state & J1939_SOCK_ERRQUEUE) ? 1 : 0;
768 case SO_J1939_SEND_PRIO:
769 tmp = j1939_prio(jsk->sk.sk_priority);
776 /* copy to user, based on 'len' & 'val'
777 * but most sockopt's are 'int' properties, and have 'len' & 'val'
778 * left unchanged, but instead modified 'tmp'
782 else if (put_user(len, optlen))
784 else if (copy_to_user(optval, val, len))
789 release_sock(&jsk->sk);
793 static int j1939_sk_recvmsg(struct socket *sock, struct msghdr *msg,
794 size_t size, int flags)
796 struct sock *sk = sock->sk;
798 struct j1939_sk_buff_cb *skcb;
801 if (flags & ~(MSG_DONTWAIT | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
804 if (flags & MSG_ERRQUEUE)
805 return sock_recv_errqueue(sock->sk, msg, size, SOL_CAN_J1939,
808 skb = skb_recv_datagram(sk, flags, &ret);
813 msg->msg_flags |= MSG_TRUNC;
817 ret = memcpy_to_msg(msg, skb->data, size);
819 skb_free_datagram(sk, skb);
823 skcb = j1939_skb_to_cb(skb);
824 if (j1939_address_is_valid(skcb->addr.da))
825 put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_DEST_ADDR,
826 sizeof(skcb->addr.da), &skcb->addr.da);
828 if (skcb->addr.dst_name)
829 put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_DEST_NAME,
830 sizeof(skcb->addr.dst_name), &skcb->addr.dst_name);
832 put_cmsg(msg, SOL_CAN_J1939, SCM_J1939_PRIO,
833 sizeof(skcb->priority), &skcb->priority);
836 struct sockaddr_can *paddr = msg->msg_name;
838 msg->msg_namelen = J1939_MIN_NAMELEN;
839 memset(msg->msg_name, 0, msg->msg_namelen);
840 paddr->can_family = AF_CAN;
841 paddr->can_ifindex = skb->skb_iif;
842 paddr->can_addr.j1939.name = skcb->addr.src_name;
843 paddr->can_addr.j1939.addr = skcb->addr.sa;
844 paddr->can_addr.j1939.pgn = skcb->addr.pgn;
847 sock_recv_cmsgs(msg, sk, skb);
848 msg->msg_flags |= skcb->msg_flags;
849 skb_free_datagram(sk, skb);
854 static struct sk_buff *j1939_sk_alloc_skb(struct net_device *ndev,
856 struct msghdr *msg, size_t size,
859 struct j1939_sock *jsk = j1939_sk(sk);
860 struct j1939_sk_buff_cb *skcb;
864 skb = sock_alloc_send_skb(sk,
866 sizeof(struct can_frame) -
867 sizeof(((struct can_frame *)NULL)->data) +
868 sizeof(struct can_skb_priv),
869 msg->msg_flags & MSG_DONTWAIT, &ret);
873 can_skb_reserve(skb);
874 can_skb_prv(skb)->ifindex = ndev->ifindex;
875 can_skb_prv(skb)->skbcnt = 0;
876 skb_reserve(skb, offsetof(struct can_frame, data));
878 ret = memcpy_from_msg(skb_put(skb, size), msg, size);
884 skcb = j1939_skb_to_cb(skb);
885 memset(skcb, 0, sizeof(*skcb));
886 skcb->addr = jsk->addr;
887 skcb->priority = j1939_prio(sk->sk_priority);
890 struct sockaddr_can *addr = msg->msg_name;
892 if (addr->can_addr.j1939.name ||
893 addr->can_addr.j1939.addr != J1939_NO_ADDR) {
894 skcb->addr.dst_name = addr->can_addr.j1939.name;
895 skcb->addr.da = addr->can_addr.j1939.addr;
897 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn))
898 skcb->addr.pgn = addr->can_addr.j1939.pgn;
911 static size_t j1939_sk_opt_stats_get_size(enum j1939_sk_errqueue_type type)
914 case J1939_ERRQUEUE_RX_RTS:
916 nla_total_size(sizeof(u32)) + /* J1939_NLA_TOTAL_SIZE */
917 nla_total_size(sizeof(u32)) + /* J1939_NLA_PGN */
918 nla_total_size(sizeof(u64)) + /* J1939_NLA_SRC_NAME */
919 nla_total_size(sizeof(u64)) + /* J1939_NLA_DEST_NAME */
920 nla_total_size(sizeof(u8)) + /* J1939_NLA_SRC_ADDR */
921 nla_total_size(sizeof(u8)) + /* J1939_NLA_DEST_ADDR */
925 nla_total_size(sizeof(u32)) + /* J1939_NLA_BYTES_ACKED */
930 static struct sk_buff *
931 j1939_sk_get_timestamping_opt_stats(struct j1939_session *session,
932 enum j1939_sk_errqueue_type type)
934 struct sk_buff *stats;
937 stats = alloc_skb(j1939_sk_opt_stats_get_size(type), GFP_ATOMIC);
941 if (session->skcb.addr.type == J1939_SIMPLE)
942 size = session->total_message_size;
944 size = min(session->pkt.tx_acked * 7,
945 session->total_message_size);
948 case J1939_ERRQUEUE_RX_RTS:
949 nla_put_u32(stats, J1939_NLA_TOTAL_SIZE,
950 session->total_message_size);
951 nla_put_u32(stats, J1939_NLA_PGN,
952 session->skcb.addr.pgn);
953 nla_put_u64_64bit(stats, J1939_NLA_SRC_NAME,
954 session->skcb.addr.src_name, J1939_NLA_PAD);
955 nla_put_u64_64bit(stats, J1939_NLA_DEST_NAME,
956 session->skcb.addr.dst_name, J1939_NLA_PAD);
957 nla_put_u8(stats, J1939_NLA_SRC_ADDR,
958 session->skcb.addr.sa);
959 nla_put_u8(stats, J1939_NLA_DEST_ADDR,
960 session->skcb.addr.da);
963 nla_put_u32(stats, J1939_NLA_BYTES_ACKED, size);
969 static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
970 enum j1939_sk_errqueue_type type)
972 struct j1939_priv *priv = session->priv;
973 struct j1939_sock *jsk;
974 struct sock_exterr_skb *serr;
981 if (!(jsk->state & J1939_SOCK_ERRQUEUE))
985 case J1939_ERRQUEUE_TX_ACK:
986 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK))
989 case J1939_ERRQUEUE_TX_SCHED:
990 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED))
993 case J1939_ERRQUEUE_TX_ABORT:
995 case J1939_ERRQUEUE_RX_RTS:
997 case J1939_ERRQUEUE_RX_DPO:
999 case J1939_ERRQUEUE_RX_ABORT:
1000 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE))
1004 netdev_err(priv->ndev, "Unknown errqueue type %i\n", type);
1007 skb = j1939_sk_get_timestamping_opt_stats(session, type);
1011 skb->tstamp = ktime_get_real();
1013 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
1015 serr = SKB_EXT_ERR(skb);
1016 memset(serr, 0, sizeof(*serr));
1018 case J1939_ERRQUEUE_TX_ACK:
1019 serr->ee.ee_errno = ENOMSG;
1020 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
1021 serr->ee.ee_info = SCM_TSTAMP_ACK;
1024 case J1939_ERRQUEUE_TX_SCHED:
1025 serr->ee.ee_errno = ENOMSG;
1026 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
1027 serr->ee.ee_info = SCM_TSTAMP_SCHED;
1030 case J1939_ERRQUEUE_TX_ABORT:
1031 serr->ee.ee_errno = session->err;
1032 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
1033 serr->ee.ee_info = J1939_EE_INFO_TX_ABORT;
1036 case J1939_ERRQUEUE_RX_RTS:
1037 serr->ee.ee_errno = ENOMSG;
1038 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
1039 serr->ee.ee_info = J1939_EE_INFO_RX_RTS;
1042 case J1939_ERRQUEUE_RX_DPO:
1043 serr->ee.ee_errno = ENOMSG;
1044 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
1045 serr->ee.ee_info = J1939_EE_INFO_RX_DPO;
1048 case J1939_ERRQUEUE_RX_ABORT:
1049 serr->ee.ee_errno = session->err;
1050 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
1051 serr->ee.ee_info = J1939_EE_INFO_RX_ABORT;
1056 serr->opt_stats = true;
1057 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1058 serr->ee.ee_data = session->tskey;
1060 netdev_dbg(session->priv->ndev, "%s: 0x%p tskey: %i, state: %s\n",
1061 __func__, session, session->tskey, state);
1062 err = sock_queue_err_skb(sk, skb);
1068 void j1939_sk_errqueue(struct j1939_session *session,
1069 enum j1939_sk_errqueue_type type)
1071 struct j1939_priv *priv = session->priv;
1072 struct j1939_sock *jsk;
1075 /* send TX notifications to the socket of origin */
1076 __j1939_sk_errqueue(session, session->sk, type);
1080 /* spread RX notifications to all sockets subscribed to this session */
1081 spin_lock_bh(&priv->j1939_socks_lock);
1082 list_for_each_entry(jsk, &priv->j1939_socks, list) {
1083 if (j1939_sk_recv_match_one(jsk, &session->skcb))
1084 __j1939_sk_errqueue(session, &jsk->sk, type);
1086 spin_unlock_bh(&priv->j1939_socks_lock);
1089 void j1939_sk_send_loop_abort(struct sock *sk, int err)
1093 sk_error_report(sk);
1096 static int j1939_sk_send_loop(struct j1939_priv *priv, struct sock *sk,
1097 struct msghdr *msg, size_t size)
1100 struct j1939_sock *jsk = j1939_sk(sk);
1101 struct j1939_session *session = j1939_sk_get_incomplete_session(jsk);
1102 struct sk_buff *skb;
1103 size_t segment_size, todo_size;
1107 session->total_message_size != session->total_queued_size + size) {
1108 j1939_session_put(session);
1115 struct j1939_sk_buff_cb *skcb;
1117 segment_size = min_t(size_t, J1939_MAX_TP_PACKET_SIZE,
1120 /* Allocate skb for one segment */
1121 skb = j1939_sk_alloc_skb(priv->ndev, sk, msg, segment_size,
1126 skcb = j1939_skb_to_cb(skb);
1129 /* at this point the size should be full size
1133 session = j1939_tp_send(priv, skb, size);
1134 if (IS_ERR(session)) {
1135 ret = PTR_ERR(session);
1138 if (j1939_sk_queue_session(session)) {
1139 /* try to activate session if we a
1142 if (!j1939_session_activate(session)) {
1143 j1939_tp_schedule_txtimer(session, 0);
1147 j1939_sk_queue_drop_all(priv, jsk,
1153 skcb->offset = session->total_queued_size;
1154 j1939_session_skb_queue(session, skb);
1157 todo_size -= segment_size;
1158 session->total_queued_size += segment_size;
1164 netdev_warn(priv->ndev,
1165 "no error found and not completely queued?! %zu\n",
1172 case -EAGAIN: /* OK */
1173 if (todo_size != size)
1174 ret = size - todo_size;
1176 default: /* ERROR */
1181 j1939_session_put(session);
1190 static int j1939_sk_sendmsg(struct socket *sock, struct msghdr *msg,
1193 struct sock *sk = sock->sk;
1194 struct j1939_sock *jsk = j1939_sk(sk);
1195 struct j1939_priv *priv;
1199 lock_sock(sock->sk);
1200 /* various socket state tests */
1201 if (!(jsk->state & J1939_SOCK_BOUND)) {
1207 ifindex = jsk->ifindex;
1209 if (!jsk->addr.src_name && jsk->addr.sa == J1939_NO_ADDR) {
1210 /* no source address assigned yet */
1215 /* deal with provided destination address info */
1216 if (msg->msg_name) {
1217 struct sockaddr_can *addr = msg->msg_name;
1219 if (msg->msg_namelen < J1939_MIN_NAMELEN) {
1224 if (addr->can_family != AF_CAN) {
1229 if (addr->can_ifindex && addr->can_ifindex != ifindex) {
1234 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) &&
1235 !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn)) {
1240 if (!addr->can_addr.j1939.name &&
1241 addr->can_addr.j1939.addr == J1939_NO_ADDR &&
1242 !sock_flag(sk, SOCK_BROADCAST)) {
1243 /* broadcast, but SO_BROADCAST not set */
1248 if (!jsk->addr.dst_name && jsk->addr.da == J1939_NO_ADDR &&
1249 !sock_flag(sk, SOCK_BROADCAST)) {
1250 /* broadcast, but SO_BROADCAST not set */
1256 ret = j1939_sk_send_loop(priv, sk, msg, size);
1259 release_sock(sock->sk);
1264 void j1939_sk_netdev_event_netdown(struct j1939_priv *priv)
1266 struct j1939_sock *jsk;
1267 int error_code = ENETDOWN;
1269 spin_lock_bh(&priv->j1939_socks_lock);
1270 list_for_each_entry(jsk, &priv->j1939_socks, list) {
1271 jsk->sk.sk_err = error_code;
1272 if (!sock_flag(&jsk->sk, SOCK_DEAD))
1273 sk_error_report(&jsk->sk);
1275 j1939_sk_queue_drop_all(priv, jsk, error_code);
1277 spin_unlock_bh(&priv->j1939_socks_lock);
1280 static int j1939_sk_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1283 /* no ioctls for socket layer -> hand it down to NIC layer */
1284 return -ENOIOCTLCMD;
1287 static const struct proto_ops j1939_ops = {
1289 .release = j1939_sk_release,
1290 .bind = j1939_sk_bind,
1291 .connect = j1939_sk_connect,
1292 .socketpair = sock_no_socketpair,
1293 .accept = sock_no_accept,
1294 .getname = j1939_sk_getname,
1295 .poll = datagram_poll,
1296 .ioctl = j1939_sk_no_ioctlcmd,
1297 .listen = sock_no_listen,
1298 .shutdown = sock_no_shutdown,
1299 .setsockopt = j1939_sk_setsockopt,
1300 .getsockopt = j1939_sk_getsockopt,
1301 .sendmsg = j1939_sk_sendmsg,
1302 .recvmsg = j1939_sk_recvmsg,
1303 .mmap = sock_no_mmap,
1304 .sendpage = sock_no_sendpage,
1307 static struct proto j1939_proto __read_mostly = {
1308 .name = "CAN_J1939",
1309 .owner = THIS_MODULE,
1310 .obj_size = sizeof(struct j1939_sock),
1311 .init = j1939_sk_init,
1314 const struct can_proto j1939_can_proto = {
1316 .protocol = CAN_J1939,
1318 .prot = &j1939_proto,