Lines Matching +full:sock +full:- +full:priv
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2010-2011 EIA Electronics,
4 // Copyright (c) 2010-2011 EIA Electronics,
8 // Copyright (c) 2017-2019 Pengutronix,
9 // Marc Kleine-Budde <kernel@pengutronix.de>
10 // Copyright (c) 2017-2019 Pengutronix,
15 #include <linux/can/can-ml.h>
21 #include "j1939-priv.h"
25 /* conversion function between struct sock::sk_priority from linux and
32 return 7 - sk_priority; in j1939_prio()
37 return 7 - prio; in j1939_to_sk_priority()
46 /* test function to avoid non-zero DA placeholder for pdu1 pgn's */
55 static inline void j1939_sock_pending_add(struct sock *sk) in j1939_sock_pending_add()
59 atomic_inc(&jsk->skb_pending); in j1939_sock_pending_add()
62 static int j1939_sock_pending_get(struct sock *sk) in j1939_sock_pending_get()
66 return atomic_read(&jsk->skb_pending); in j1939_sock_pending_get()
69 void j1939_sock_pending_del(struct sock *sk) in j1939_sock_pending_del()
74 if (!atomic_dec_return(&jsk->skb_pending)) in j1939_sock_pending_del()
75 wake_up(&jsk->waitq); /* no pending SKB's */ in j1939_sock_pending_del()
78 static void j1939_jsk_add(struct j1939_priv *priv, struct j1939_sock *jsk) in j1939_jsk_add() argument
80 jsk->state |= J1939_SOCK_BOUND; in j1939_jsk_add()
81 j1939_priv_get(priv); in j1939_jsk_add()
83 write_lock_bh(&priv->j1939_socks_lock); in j1939_jsk_add()
84 list_add_tail(&jsk->list, &priv->j1939_socks); in j1939_jsk_add()
85 write_unlock_bh(&priv->j1939_socks_lock); in j1939_jsk_add()
88 static void j1939_jsk_del(struct j1939_priv *priv, struct j1939_sock *jsk) in j1939_jsk_del() argument
90 write_lock_bh(&priv->j1939_socks_lock); in j1939_jsk_del()
91 list_del_init(&jsk->list); in j1939_jsk_del()
92 write_unlock_bh(&priv->j1939_socks_lock); in j1939_jsk_del()
94 j1939_priv_put(priv); in j1939_jsk_del()
95 jsk->state &= ~J1939_SOCK_BOUND; in j1939_jsk_del()
100 struct j1939_sock *jsk = j1939_sk(session->sk); in j1939_sk_queue_session()
103 spin_lock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_session()
104 empty = list_empty(&jsk->sk_session_queue); in j1939_sk_queue_session()
106 list_add_tail(&session->sk_session_queue_entry, &jsk->sk_session_queue); in j1939_sk_queue_session()
107 spin_unlock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_session()
108 j1939_sock_pending_add(&jsk->sk); in j1939_sk_queue_session()
118 spin_lock_bh(&jsk->sk_session_queue_lock); in j1939_sk_get_incomplete_session()
119 if (!list_empty(&jsk->sk_session_queue)) { in j1939_sk_get_incomplete_session()
120 session = list_last_entry(&jsk->sk_session_queue, in j1939_sk_get_incomplete_session()
123 if (session->total_queued_size == session->total_message_size) in j1939_sk_get_incomplete_session()
128 spin_unlock_bh(&jsk->sk_session_queue_lock); in j1939_sk_get_incomplete_session()
133 static void j1939_sk_queue_drop_all(struct j1939_priv *priv, in j1939_sk_queue_drop_all() argument
138 netdev_dbg(priv->ndev, "%s: err: %i\n", __func__, err); in j1939_sk_queue_drop_all()
139 spin_lock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_drop_all()
140 list_for_each_entry_safe(session, tmp, &jsk->sk_session_queue, in j1939_sk_queue_drop_all()
142 list_del_init(&session->sk_session_queue_entry); in j1939_sk_queue_drop_all()
143 session->err = err; in j1939_sk_queue_drop_all()
146 spin_unlock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_drop_all()
155 /* RX-Session don't have a socket (yet) */ in j1939_sk_queue_activate_next_locked()
156 if (!session->sk) in j1939_sk_queue_activate_next_locked()
159 jsk = j1939_sk(session->sk); in j1939_sk_queue_activate_next_locked()
160 lockdep_assert_held(&jsk->sk_session_queue_lock); in j1939_sk_queue_activate_next_locked()
162 err = session->err; in j1939_sk_queue_activate_next_locked()
164 first = list_first_entry_or_null(&jsk->sk_session_queue, in j1939_sk_queue_activate_next_locked()
173 list_del_init(&first->sk_session_queue_entry); in j1939_sk_queue_activate_next_locked()
175 first = list_first_entry_or_null(&jsk->sk_session_queue, in j1939_sk_queue_activate_next_locked()
182 netdev_warn_once(first->priv->ndev, in j1939_sk_queue_activate_next_locked()
185 first->err = -EBUSY; in j1939_sk_queue_activate_next_locked()
202 if (!session->sk) in j1939_sk_queue_activate_next()
205 jsk = j1939_sk(session->sk); in j1939_sk_queue_activate_next()
207 spin_lock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_activate_next()
209 spin_unlock_bh(&jsk->sk_session_queue_lock); in j1939_sk_queue_activate_next()
215 if ((jsk->state & J1939_SOCK_PROMISC)) in j1939_sk_match_dst()
219 if (jsk->addr.src_name && skcb->addr.dst_name) { in j1939_sk_match_dst()
220 if (jsk->addr.src_name != skcb->addr.dst_name) in j1939_sk_match_dst()
224 * - all packages that match our bind() address in j1939_sk_match_dst()
225 * - all broadcast on a socket if SO_BROADCAST in j1939_sk_match_dst()
228 if (j1939_address_is_unicast(skcb->addr.da)) { in j1939_sk_match_dst()
229 if (jsk->addr.sa != skcb->addr.da) in j1939_sk_match_dst()
231 } else if (!sock_flag(&jsk->sk, SOCK_BROADCAST)) { in j1939_sk_match_dst()
240 if (jsk->state & J1939_SOCK_CONNECTED) { in j1939_sk_match_dst()
242 * - all packages that match our connect() name or address in j1939_sk_match_dst()
244 if (jsk->addr.dst_name && skcb->addr.src_name) { in j1939_sk_match_dst()
245 if (jsk->addr.dst_name != skcb->addr.src_name) in j1939_sk_match_dst()
248 if (jsk->addr.da != skcb->addr.sa) in j1939_sk_match_dst()
254 if (j1939_pgn_is_valid(jsk->pgn_rx_filter) && in j1939_sk_match_dst()
255 jsk->pgn_rx_filter != skcb->addr.pgn) in j1939_sk_match_dst()
268 spin_lock_bh(&jsk->filters_lock); in j1939_sk_match_filter()
270 f = jsk->filters; in j1939_sk_match_filter()
271 nfilter = jsk->nfilters; in j1939_sk_match_filter()
277 for (; nfilter; ++f, --nfilter) { in j1939_sk_match_filter()
278 if ((skcb->addr.pgn & f->pgn_mask) != f->pgn) in j1939_sk_match_filter()
280 if ((skcb->addr.sa & f->addr_mask) != f->addr) in j1939_sk_match_filter()
282 if ((skcb->addr.src_name & f->name_mask) != f->name) in j1939_sk_match_filter()
287 spin_unlock_bh(&jsk->filters_lock); in j1939_sk_match_filter()
291 spin_unlock_bh(&jsk->filters_lock); in j1939_sk_match_filter()
298 if (!(jsk->state & J1939_SOCK_BOUND)) in j1939_sk_recv_match_one()
316 if (oskb->sk == &jsk->sk) in j1939_sk_recv_one()
327 can_skb_set_owner(skb, oskb->sk); in j1939_sk_recv_one()
330 skcb->msg_flags &= ~(MSG_DONTROUTE); in j1939_sk_recv_one()
331 if (skb->sk) in j1939_sk_recv_one()
332 skcb->msg_flags |= MSG_DONTROUTE; in j1939_sk_recv_one()
334 if (sock_queue_rcv_skb(&jsk->sk, skb) < 0) in j1939_sk_recv_one()
338 bool j1939_sk_recv_match(struct j1939_priv *priv, struct j1939_sk_buff_cb *skcb) in j1939_sk_recv_match() argument
343 read_lock_bh(&priv->j1939_socks_lock); in j1939_sk_recv_match()
344 list_for_each_entry(jsk, &priv->j1939_socks, list) { in j1939_sk_recv_match()
349 read_unlock_bh(&priv->j1939_socks_lock); in j1939_sk_recv_match()
354 void j1939_sk_recv(struct j1939_priv *priv, struct sk_buff *skb) in j1939_sk_recv() argument
358 read_lock_bh(&priv->j1939_socks_lock); in j1939_sk_recv()
359 list_for_each_entry(jsk, &priv->j1939_socks, list) { in j1939_sk_recv()
362 read_unlock_bh(&priv->j1939_socks_lock); in j1939_sk_recv()
365 static void j1939_sk_sock_destruct(struct sock *sk) in j1939_sk_sock_destruct()
370 * the socket is ultimately closed (sk->sk_destruct). in j1939_sk_sock_destruct()
373 * - processing a received CAN frame in j1939_sk_sock_destruct()
374 * (can_receive -> j1939_can_recv) in j1939_sk_sock_destruct()
377 * - closing a socket in j1939_sk_sock_destruct()
378 * (j1939_can_rx_unregister -> can_rx_unregister) in j1939_sk_sock_destruct()
384 if (jsk->priv) { in j1939_sk_sock_destruct()
385 j1939_priv_put(jsk->priv); in j1939_sk_sock_destruct()
386 jsk->priv = NULL; in j1939_sk_sock_destruct()
389 /* call generic CAN sock destruct */ in j1939_sk_sock_destruct()
393 static int j1939_sk_init(struct sock *sk) in j1939_sk_init()
401 memset((void *)jsk + sizeof(jsk->sk), 0x0, in j1939_sk_init()
402 sizeof(*jsk) - sizeof(jsk->sk)); in j1939_sk_init()
404 INIT_LIST_HEAD(&jsk->list); in j1939_sk_init()
405 init_waitqueue_head(&jsk->waitq); in j1939_sk_init()
406 jsk->sk.sk_priority = j1939_to_sk_priority(6); in j1939_sk_init()
407 jsk->sk.sk_reuse = 1; /* per default */ in j1939_sk_init()
408 jsk->addr.sa = J1939_NO_ADDR; in j1939_sk_init()
409 jsk->addr.da = J1939_NO_ADDR; in j1939_sk_init()
410 jsk->addr.pgn = J1939_NO_PGN; in j1939_sk_init()
411 jsk->pgn_rx_filter = J1939_NO_PGN; in j1939_sk_init()
412 atomic_set(&jsk->skb_pending, 0); in j1939_sk_init()
413 spin_lock_init(&jsk->sk_session_queue_lock); in j1939_sk_init()
414 INIT_LIST_HEAD(&jsk->sk_session_queue); in j1939_sk_init()
415 spin_lock_init(&jsk->filters_lock); in j1939_sk_init()
419 sk->sk_destruct = j1939_sk_sock_destruct; in j1939_sk_init()
420 sk->sk_protocol = CAN_J1939; in j1939_sk_init()
428 return -EDESTADDRREQ; in j1939_sk_sanity_check()
430 return -EINVAL; in j1939_sk_sanity_check()
431 if (addr->can_family != AF_CAN) in j1939_sk_sanity_check()
432 return -EINVAL; in j1939_sk_sanity_check()
433 if (!addr->can_ifindex) in j1939_sk_sanity_check()
434 return -ENODEV; in j1939_sk_sanity_check()
435 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) && in j1939_sk_sanity_check()
436 !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn)) in j1939_sk_sanity_check()
437 return -EINVAL; in j1939_sk_sanity_check()
442 static int j1939_sk_bind(struct socket *sock, struct sockaddr *uaddr, int len) in j1939_sk_bind() argument
445 struct j1939_sock *jsk = j1939_sk(sock->sk); in j1939_sk_bind()
446 struct j1939_priv *priv; in j1939_sk_bind() local
447 struct sock *sk; in j1939_sk_bind()
455 lock_sock(sock->sk); in j1939_sk_bind()
457 priv = jsk->priv; in j1939_sk_bind()
458 sk = sock->sk; in j1939_sk_bind()
462 if (jsk->state & J1939_SOCK_BOUND) { in j1939_sk_bind()
463 /* A re-bind() to a different interface is not in j1939_sk_bind()
466 if (jsk->ifindex != addr->can_ifindex) { in j1939_sk_bind()
467 ret = -EINVAL; in j1939_sk_bind()
472 j1939_jsk_del(priv, jsk); in j1939_sk_bind()
473 j1939_local_ecu_put(priv, jsk->addr.src_name, jsk->addr.sa); in j1939_sk_bind()
478 ndev = dev_get_by_index(net, addr->can_ifindex); in j1939_sk_bind()
480 ret = -ENODEV; in j1939_sk_bind()
487 ret = -ENODEV; in j1939_sk_bind()
491 if (!(ndev->flags & IFF_UP)) { in j1939_sk_bind()
493 ret = -ENETDOWN; in j1939_sk_bind()
497 priv = j1939_netdev_start(ndev); in j1939_sk_bind()
499 if (IS_ERR(priv)) { in j1939_sk_bind()
500 ret = PTR_ERR(priv); in j1939_sk_bind()
504 jsk->ifindex = addr->can_ifindex; in j1939_sk_bind()
507 * sk->sk_destruct, which points to j1939_sk_sock_destruct() in j1939_sk_bind()
509 j1939_priv_get(priv); in j1939_sk_bind()
510 jsk->priv = priv; in j1939_sk_bind()
514 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn)) in j1939_sk_bind()
515 jsk->pgn_rx_filter = addr->can_addr.j1939.pgn; in j1939_sk_bind()
516 jsk->addr.src_name = addr->can_addr.j1939.name; in j1939_sk_bind()
517 jsk->addr.sa = addr->can_addr.j1939.addr; in j1939_sk_bind()
520 ret = j1939_local_ecu_get(priv, jsk->addr.src_name, jsk->addr.sa); in j1939_sk_bind()
522 j1939_netdev_stop(priv); in j1939_sk_bind()
526 j1939_jsk_add(priv, jsk); in j1939_sk_bind()
529 release_sock(sock->sk); in j1939_sk_bind()
534 static int j1939_sk_connect(struct socket *sock, struct sockaddr *uaddr, in j1939_sk_connect() argument
538 struct j1939_sock *jsk = j1939_sk(sock->sk); in j1939_sk_connect()
545 lock_sock(sock->sk); in j1939_sk_connect()
548 if (!(jsk->state & J1939_SOCK_BOUND)) { in j1939_sk_connect()
549 ret = -EINVAL; in j1939_sk_connect()
554 if (jsk->ifindex != addr->can_ifindex) { in j1939_sk_connect()
555 ret = -EINVAL; in j1939_sk_connect()
559 if (!addr->can_addr.j1939.name && in j1939_sk_connect()
560 addr->can_addr.j1939.addr == J1939_NO_ADDR && in j1939_sk_connect()
561 !sock_flag(&jsk->sk, SOCK_BROADCAST)) { in j1939_sk_connect()
563 ret = -EACCES; in j1939_sk_connect()
567 jsk->addr.dst_name = addr->can_addr.j1939.name; in j1939_sk_connect()
568 jsk->addr.da = addr->can_addr.j1939.addr; in j1939_sk_connect()
570 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn)) in j1939_sk_connect()
571 jsk->addr.pgn = addr->can_addr.j1939.pgn; in j1939_sk_connect()
573 jsk->state |= J1939_SOCK_CONNECTED; in j1939_sk_connect()
576 release_sock(sock->sk); in j1939_sk_connect()
589 addr->can_family = AF_CAN; in j1939_sk_sock2sockaddr_can()
590 addr->can_ifindex = jsk->ifindex; in j1939_sk_sock2sockaddr_can()
591 addr->can_addr.j1939.pgn = jsk->addr.pgn; in j1939_sk_sock2sockaddr_can()
593 addr->can_addr.j1939.name = jsk->addr.dst_name; in j1939_sk_sock2sockaddr_can()
594 addr->can_addr.j1939.addr = jsk->addr.da; in j1939_sk_sock2sockaddr_can()
596 addr->can_addr.j1939.name = jsk->addr.src_name; in j1939_sk_sock2sockaddr_can()
597 addr->can_addr.j1939.addr = jsk->addr.sa; in j1939_sk_sock2sockaddr_can()
601 static int j1939_sk_getname(struct socket *sock, struct sockaddr *uaddr, in j1939_sk_getname() argument
605 struct sock *sk = sock->sk; in j1939_sk_getname()
611 if (peer && !(jsk->state & J1939_SOCK_CONNECTED)) { in j1939_sk_getname()
612 ret = -EADDRNOTAVAIL; in j1939_sk_getname()
625 static int j1939_sk_release(struct socket *sock) in j1939_sk_release() argument
627 struct sock *sk = sock->sk; in j1939_sk_release()
636 if (jsk->state & J1939_SOCK_BOUND) { in j1939_sk_release()
637 struct j1939_priv *priv = jsk->priv; in j1939_sk_release() local
639 if (wait_event_interruptible(jsk->waitq, in j1939_sk_release()
640 !j1939_sock_pending_get(&jsk->sk))) { in j1939_sk_release()
641 j1939_cancel_active_session(priv, sk); in j1939_sk_release()
642 j1939_sk_queue_drop_all(priv, jsk, ESHUTDOWN); in j1939_sk_release()
645 j1939_jsk_del(priv, jsk); in j1939_sk_release()
647 j1939_local_ecu_put(priv, jsk->addr.src_name, in j1939_sk_release()
648 jsk->addr.sa); in j1939_sk_release()
650 j1939_netdev_stop(priv); in j1939_sk_release()
653 kfree(jsk->filters); in j1939_sk_release()
655 sock->sk = NULL; in j1939_sk_release()
669 return -EINVAL; in j1939_sk_setsockopt_flag()
671 return -EFAULT; in j1939_sk_setsockopt_flag()
672 lock_sock(&jsk->sk); in j1939_sk_setsockopt_flag()
674 jsk->state |= flag; in j1939_sk_setsockopt_flag()
676 jsk->state &= ~flag; in j1939_sk_setsockopt_flag()
677 release_sock(&jsk->sk); in j1939_sk_setsockopt_flag()
681 static int j1939_sk_setsockopt(struct socket *sock, int level, int optname, in j1939_sk_setsockopt() argument
684 struct sock *sk = sock->sk; in j1939_sk_setsockopt()
690 return -EINVAL; in j1939_sk_setsockopt()
699 return -EINVAL; in j1939_sk_setsockopt()
703 return -EINVAL; in j1939_sk_setsockopt()
710 for (f = filters, c = count; c; f++, c--) { in j1939_sk_setsockopt()
711 f->name &= f->name_mask; in j1939_sk_setsockopt()
712 f->pgn &= f->pgn_mask; in j1939_sk_setsockopt()
713 f->addr &= f->addr_mask; in j1939_sk_setsockopt()
717 lock_sock(&jsk->sk); in j1939_sk_setsockopt()
718 spin_lock_bh(&jsk->filters_lock); in j1939_sk_setsockopt()
719 ofilters = jsk->filters; in j1939_sk_setsockopt()
720 jsk->filters = filters; in j1939_sk_setsockopt()
721 jsk->nfilters = count; in j1939_sk_setsockopt()
722 spin_unlock_bh(&jsk->filters_lock); in j1939_sk_setsockopt()
723 release_sock(&jsk->sk); in j1939_sk_setsockopt()
735 if (!(jsk->state & J1939_SOCK_ERRQUEUE)) in j1939_sk_setsockopt()
736 skb_queue_purge(&sk->sk_error_queue); in j1939_sk_setsockopt()
740 return -EINVAL; in j1939_sk_setsockopt()
742 return -EFAULT; in j1939_sk_setsockopt()
744 return -EDOM; in j1939_sk_setsockopt()
746 return -EPERM; in j1939_sk_setsockopt()
747 lock_sock(&jsk->sk); in j1939_sk_setsockopt()
748 jsk->sk.sk_priority = j1939_to_sk_priority(tmp); in j1939_sk_setsockopt()
749 release_sock(&jsk->sk); in j1939_sk_setsockopt()
752 return -ENOPROTOOPT; in j1939_sk_setsockopt()
756 static int j1939_sk_getsockopt(struct socket *sock, int level, int optname, in j1939_sk_getsockopt() argument
759 struct sock *sk = sock->sk; in j1939_sk_getsockopt()
768 return -EINVAL; in j1939_sk_getsockopt()
770 return -EFAULT; in j1939_sk_getsockopt()
772 return -EINVAL; in j1939_sk_getsockopt()
774 lock_sock(&jsk->sk); in j1939_sk_getsockopt()
777 tmp = (jsk->state & J1939_SOCK_PROMISC) ? 1 : 0; in j1939_sk_getsockopt()
780 tmp = (jsk->state & J1939_SOCK_ERRQUEUE) ? 1 : 0; in j1939_sk_getsockopt()
783 tmp = j1939_prio(jsk->sk.sk_priority); in j1939_sk_getsockopt()
786 ret = -ENOPROTOOPT; in j1939_sk_getsockopt()
795 ret = -EFAULT; in j1939_sk_getsockopt()
797 ret = -EFAULT; in j1939_sk_getsockopt()
799 ret = -EFAULT; in j1939_sk_getsockopt()
803 release_sock(&jsk->sk); in j1939_sk_getsockopt()
807 static int j1939_sk_recvmsg(struct socket *sock, struct msghdr *msg, in j1939_sk_recvmsg() argument
810 struct sock *sk = sock->sk; in j1939_sk_recvmsg()
816 return -EINVAL; in j1939_sk_recvmsg()
819 return sock_recv_errqueue(sock->sk, msg, size, SOL_CAN_J1939, in j1939_sk_recvmsg()
826 if (size < skb->len) in j1939_sk_recvmsg()
827 msg->msg_flags |= MSG_TRUNC; in j1939_sk_recvmsg()
829 size = skb->len; in j1939_sk_recvmsg()
831 ret = memcpy_to_msg(msg, skb->data, size); in j1939_sk_recvmsg()
838 if (j1939_address_is_valid(skcb->addr.da)) in j1939_sk_recvmsg()
840 sizeof(skcb->addr.da), &skcb->addr.da); in j1939_sk_recvmsg()
842 if (skcb->addr.dst_name) in j1939_sk_recvmsg()
844 sizeof(skcb->addr.dst_name), &skcb->addr.dst_name); in j1939_sk_recvmsg()
847 sizeof(skcb->priority), &skcb->priority); in j1939_sk_recvmsg()
849 if (msg->msg_name) { in j1939_sk_recvmsg()
850 struct sockaddr_can *paddr = msg->msg_name; in j1939_sk_recvmsg()
852 msg->msg_namelen = J1939_MIN_NAMELEN; in j1939_sk_recvmsg()
853 memset(msg->msg_name, 0, msg->msg_namelen); in j1939_sk_recvmsg()
854 paddr->can_family = AF_CAN; in j1939_sk_recvmsg()
855 paddr->can_ifindex = skb->skb_iif; in j1939_sk_recvmsg()
856 paddr->can_addr.j1939.name = skcb->addr.src_name; in j1939_sk_recvmsg()
857 paddr->can_addr.j1939.addr = skcb->addr.sa; in j1939_sk_recvmsg()
858 paddr->can_addr.j1939.pgn = skcb->addr.pgn; in j1939_sk_recvmsg()
862 msg->msg_flags |= skcb->msg_flags; in j1939_sk_recvmsg()
869 struct sock *sk, in j1939_sk_alloc_skb()
880 sizeof(struct can_frame) - in j1939_sk_alloc_skb()
881 sizeof(((struct can_frame *)NULL)->data) + in j1939_sk_alloc_skb()
883 msg->msg_flags & MSG_DONTWAIT, &ret); in j1939_sk_alloc_skb()
888 can_skb_prv(skb)->ifindex = ndev->ifindex; in j1939_sk_alloc_skb()
889 can_skb_prv(skb)->skbcnt = 0; in j1939_sk_alloc_skb()
896 skb->dev = ndev; in j1939_sk_alloc_skb()
900 skcb->addr = jsk->addr; in j1939_sk_alloc_skb()
901 skcb->priority = j1939_prio(READ_ONCE(sk->sk_priority)); in j1939_sk_alloc_skb()
903 if (msg->msg_name) { in j1939_sk_alloc_skb()
904 struct sockaddr_can *addr = msg->msg_name; in j1939_sk_alloc_skb()
906 if (addr->can_addr.j1939.name || in j1939_sk_alloc_skb()
907 addr->can_addr.j1939.addr != J1939_NO_ADDR) { in j1939_sk_alloc_skb()
908 skcb->addr.dst_name = addr->can_addr.j1939.name; in j1939_sk_alloc_skb()
909 skcb->addr.da = addr->can_addr.j1939.addr; in j1939_sk_alloc_skb()
911 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn)) in j1939_sk_alloc_skb()
912 skcb->addr.pgn = addr->can_addr.j1939.pgn; in j1939_sk_alloc_skb()
955 if (session->skcb.addr.type == J1939_SIMPLE) in j1939_sk_get_timestamping_opt_stats()
956 size = session->total_message_size; in j1939_sk_get_timestamping_opt_stats()
958 size = min(session->pkt.tx_acked * 7, in j1939_sk_get_timestamping_opt_stats()
959 session->total_message_size); in j1939_sk_get_timestamping_opt_stats()
964 session->total_message_size); in j1939_sk_get_timestamping_opt_stats()
966 session->skcb.addr.pgn); in j1939_sk_get_timestamping_opt_stats()
968 session->skcb.addr.src_name, J1939_NLA_PAD); in j1939_sk_get_timestamping_opt_stats()
970 session->skcb.addr.dst_name, J1939_NLA_PAD); in j1939_sk_get_timestamping_opt_stats()
972 session->skcb.addr.sa); in j1939_sk_get_timestamping_opt_stats()
974 session->skcb.addr.da); in j1939_sk_get_timestamping_opt_stats()
983 static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk, in __j1939_sk_errqueue()
986 struct j1939_priv *priv = session->priv; in __j1939_sk_errqueue() local
996 if (!(jsk->state & J1939_SOCK_ERRQUEUE)) in __j1939_sk_errqueue()
999 tsflags = READ_ONCE(sk->sk_tsflags); in __j1939_sk_errqueue()
1020 netdev_err(priv->ndev, "Unknown errqueue type %i\n", type); in __j1939_sk_errqueue()
1027 skb->tstamp = ktime_get_real(); in __j1939_sk_errqueue()
1029 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); in __j1939_sk_errqueue()
1035 serr->ee.ee_errno = ENOMSG; in __j1939_sk_errqueue()
1036 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; in __j1939_sk_errqueue()
1037 serr->ee.ee_info = SCM_TSTAMP_ACK; in __j1939_sk_errqueue()
1041 serr->ee.ee_errno = ENOMSG; in __j1939_sk_errqueue()
1042 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; in __j1939_sk_errqueue()
1043 serr->ee.ee_info = SCM_TSTAMP_SCHED; in __j1939_sk_errqueue()
1047 serr->ee.ee_errno = session->err; in __j1939_sk_errqueue()
1048 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; in __j1939_sk_errqueue()
1049 serr->ee.ee_info = J1939_EE_INFO_TX_ABORT; in __j1939_sk_errqueue()
1053 serr->ee.ee_errno = ENOMSG; in __j1939_sk_errqueue()
1054 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; in __j1939_sk_errqueue()
1055 serr->ee.ee_info = J1939_EE_INFO_RX_RTS; in __j1939_sk_errqueue()
1059 serr->ee.ee_errno = ENOMSG; in __j1939_sk_errqueue()
1060 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; in __j1939_sk_errqueue()
1061 serr->ee.ee_info = J1939_EE_INFO_RX_DPO; in __j1939_sk_errqueue()
1065 serr->ee.ee_errno = session->err; in __j1939_sk_errqueue()
1066 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; in __j1939_sk_errqueue()
1067 serr->ee.ee_info = J1939_EE_INFO_RX_ABORT; in __j1939_sk_errqueue()
1072 serr->opt_stats = true; in __j1939_sk_errqueue()
1074 serr->ee.ee_data = session->tskey; in __j1939_sk_errqueue()
1076 netdev_dbg(session->priv->ndev, "%s: 0x%p tskey: %i, state: %s\n", in __j1939_sk_errqueue()
1077 __func__, session, session->tskey, state); in __j1939_sk_errqueue()
1087 struct j1939_priv *priv = session->priv; in j1939_sk_errqueue() local
1090 if (session->sk) { in j1939_sk_errqueue()
1092 __j1939_sk_errqueue(session, session->sk, type); in j1939_sk_errqueue()
1097 read_lock_bh(&priv->j1939_socks_lock); in j1939_sk_errqueue()
1098 list_for_each_entry(jsk, &priv->j1939_socks, list) { in j1939_sk_errqueue()
1099 if (j1939_sk_recv_match_one(jsk, &session->skcb)) in j1939_sk_errqueue()
1100 __j1939_sk_errqueue(session, &jsk->sk, type); in j1939_sk_errqueue()
1102 read_unlock_bh(&priv->j1939_socks_lock); in j1939_sk_errqueue()
1105 void j1939_sk_send_loop_abort(struct sock *sk, int err) in j1939_sk_send_loop_abort()
1109 if (jsk->state & J1939_SOCK_ERRQUEUE) in j1939_sk_send_loop_abort()
1112 sk->sk_err = err; in j1939_sk_send_loop_abort()
1117 static int j1939_sk_send_loop(struct j1939_priv *priv, struct sock *sk, in j1939_sk_send_loop() argument
1128 session->total_message_size != session->total_queued_size + size) { in j1939_sk_send_loop()
1130 return -EIO; in j1939_sk_send_loop()
1142 skb = j1939_sk_alloc_skb(priv->ndev, sk, msg, segment_size, in j1939_sk_send_loop()
1153 skcb->offset = 0; in j1939_sk_send_loop()
1154 session = j1939_tp_send(priv, skb, size); in j1939_sk_send_loop()
1166 ret = -EBUSY; in j1939_sk_send_loop()
1167 session->err = ret; in j1939_sk_send_loop()
1168 j1939_sk_queue_drop_all(priv, jsk, in j1939_sk_send_loop()
1174 skcb->offset = session->total_queued_size; in j1939_sk_send_loop()
1178 todo_size -= segment_size; in j1939_sk_send_loop()
1179 session->total_queued_size += segment_size; in j1939_sk_send_loop()
1185 netdev_warn(priv->ndev, in j1939_sk_send_loop()
1190 case -ERESTARTSYS: in j1939_sk_send_loop()
1191 ret = -EINTR; in j1939_sk_send_loop()
1193 case -EAGAIN: /* OK */ in j1939_sk_send_loop()
1195 ret = size - todo_size; in j1939_sk_send_loop()
1211 static int j1939_sk_sendmsg(struct socket *sock, struct msghdr *msg, in j1939_sk_sendmsg() argument
1214 struct sock *sk = sock->sk; in j1939_sk_sendmsg()
1216 struct j1939_priv *priv; in j1939_sk_sendmsg() local
1220 lock_sock(sock->sk); in j1939_sk_sendmsg()
1222 if (!(jsk->state & J1939_SOCK_BOUND)) { in j1939_sk_sendmsg()
1223 ret = -EBADFD; in j1939_sk_sendmsg()
1227 priv = jsk->priv; in j1939_sk_sendmsg()
1228 ifindex = jsk->ifindex; in j1939_sk_sendmsg()
1230 if (!jsk->addr.src_name && jsk->addr.sa == J1939_NO_ADDR) { in j1939_sk_sendmsg()
1232 ret = -EBADFD; in j1939_sk_sendmsg()
1237 if (msg->msg_name) { in j1939_sk_sendmsg()
1238 struct sockaddr_can *addr = msg->msg_name; in j1939_sk_sendmsg()
1240 if (msg->msg_namelen < J1939_MIN_NAMELEN) { in j1939_sk_sendmsg()
1241 ret = -EINVAL; in j1939_sk_sendmsg()
1245 if (addr->can_family != AF_CAN) { in j1939_sk_sendmsg()
1246 ret = -EINVAL; in j1939_sk_sendmsg()
1250 if (addr->can_ifindex && addr->can_ifindex != ifindex) { in j1939_sk_sendmsg()
1251 ret = -EBADFD; in j1939_sk_sendmsg()
1255 if (j1939_pgn_is_valid(addr->can_addr.j1939.pgn) && in j1939_sk_sendmsg()
1256 !j1939_pgn_is_clean_pdu(addr->can_addr.j1939.pgn)) { in j1939_sk_sendmsg()
1257 ret = -EINVAL; in j1939_sk_sendmsg()
1261 if (!addr->can_addr.j1939.name && in j1939_sk_sendmsg()
1262 addr->can_addr.j1939.addr == J1939_NO_ADDR && in j1939_sk_sendmsg()
1265 ret = -EACCES; in j1939_sk_sendmsg()
1269 if (!jsk->addr.dst_name && jsk->addr.da == J1939_NO_ADDR && in j1939_sk_sendmsg()
1272 ret = -EACCES; in j1939_sk_sendmsg()
1277 ret = j1939_sk_send_loop(priv, sk, msg, size); in j1939_sk_sendmsg()
1280 release_sock(sock->sk); in j1939_sk_sendmsg()
1285 void j1939_sk_netdev_event_netdown(struct j1939_priv *priv) in j1939_sk_netdev_event_netdown() argument
1290 read_lock_bh(&priv->j1939_socks_lock); in j1939_sk_netdev_event_netdown()
1291 list_for_each_entry(jsk, &priv->j1939_socks, list) { in j1939_sk_netdev_event_netdown()
1292 jsk->sk.sk_err = error_code; in j1939_sk_netdev_event_netdown()
1293 if (!sock_flag(&jsk->sk, SOCK_DEAD)) in j1939_sk_netdev_event_netdown()
1294 sk_error_report(&jsk->sk); in j1939_sk_netdev_event_netdown()
1296 j1939_sk_queue_drop_all(priv, jsk, error_code); in j1939_sk_netdev_event_netdown()
1298 read_unlock_bh(&priv->j1939_socks_lock); in j1939_sk_netdev_event_netdown()
1301 static int j1939_sk_no_ioctlcmd(struct socket *sock, unsigned int cmd, in j1939_sk_no_ioctlcmd() argument
1304 /* no ioctls for socket layer -> hand it down to NIC layer */ in j1939_sk_no_ioctlcmd()
1305 return -ENOIOCTLCMD; in j1939_sk_no_ioctlcmd()