skb.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
  2. /*
  3. * linux/can/skb.h
  4. *
  5. * Definitions for the CAN network socket buffer
  6. *
  7. * Copyright (C) 2012 Oliver Hartkopp <[email protected]>
  8. *
  9. */
  10. #ifndef _CAN_SKB_H
  11. #define _CAN_SKB_H
  12. #include <linux/types.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/can.h>
  15. #include <net/sock.h>
  16. void can_flush_echo_skb(struct net_device *dev);
  17. int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  18. unsigned int idx, unsigned int frame_len);
  19. struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
  20. unsigned int *len_ptr,
  21. unsigned int *frame_len_ptr);
  22. unsigned int __must_check can_get_echo_skb(struct net_device *dev,
  23. unsigned int idx,
  24. unsigned int *frame_len_ptr);
  25. void can_free_echo_skb(struct net_device *dev, unsigned int idx,
  26. unsigned int *frame_len_ptr);
  27. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
  28. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  29. struct canfd_frame **cfd);
  30. struct sk_buff *alloc_canxl_skb(struct net_device *dev,
  31. struct canxl_frame **cxl,
  32. unsigned int data_len);
  33. struct sk_buff *alloc_can_err_skb(struct net_device *dev,
  34. struct can_frame **cf);
  35. bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb);
  36. /*
  37. * The struct can_skb_priv is used to transport additional information along
  38. * with the stored struct can(fd)_frame that can not be contained in existing
  39. * struct sk_buff elements.
  40. * N.B. that this information must not be modified in cloned CAN sk_buffs.
  41. * To modify the CAN frame content or the struct can_skb_priv content
  42. * skb_copy() needs to be used instead of skb_clone().
  43. */
  44. /**
  45. * struct can_skb_priv - private additional data inside CAN sk_buffs
  46. * @ifindex: ifindex of the first interface the CAN frame appeared on
  47. * @skbcnt: atomic counter to have an unique id together with skb pointer
  48. * @frame_len: length of CAN frame in data link layer
  49. * @cf: align to the following CAN frame at skb->data
  50. */
  51. struct can_skb_priv {
  52. int ifindex;
  53. int skbcnt;
  54. unsigned int frame_len;
  55. struct can_frame cf[];
  56. };
  57. static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
  58. {
  59. return (struct can_skb_priv *)(skb->head);
  60. }
  61. static inline void can_skb_reserve(struct sk_buff *skb)
  62. {
  63. skb_reserve(skb, sizeof(struct can_skb_priv));
  64. }
  65. static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
  66. {
  67. /* If the socket has already been closed by user space, the
  68. * refcount may already be 0 (and the socket will be freed
  69. * after the last TX skb has been freed). So only increase
  70. * socket refcount if the refcount is > 0.
  71. */
  72. if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
  73. skb->destructor = sock_efree;
  74. skb->sk = sk;
  75. }
  76. }
  77. /*
  78. * returns an unshared skb owned by the original sock to be echo'ed back
  79. */
  80. static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
  81. {
  82. struct sk_buff *nskb;
  83. nskb = skb_clone(skb, GFP_ATOMIC);
  84. if (unlikely(!nskb)) {
  85. kfree_skb(skb);
  86. return NULL;
  87. }
  88. can_skb_set_owner(nskb, skb->sk);
  89. consume_skb(skb);
  90. return nskb;
  91. }
  92. static inline bool can_is_can_skb(const struct sk_buff *skb)
  93. {
  94. struct can_frame *cf = (struct can_frame *)skb->data;
  95. /* the CAN specific type of skb is identified by its data length */
  96. return (skb->len == CAN_MTU && cf->len <= CAN_MAX_DLEN);
  97. }
  98. static inline bool can_is_canfd_skb(const struct sk_buff *skb)
  99. {
  100. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  101. /* the CAN specific type of skb is identified by its data length */
  102. return (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN);
  103. }
  104. static inline bool can_is_canxl_skb(const struct sk_buff *skb)
  105. {
  106. const struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
  107. if (skb->len < CANXL_HDR_SIZE + CANXL_MIN_DLEN || skb->len > CANXL_MTU)
  108. return false;
  109. /* this also checks valid CAN XL data length boundaries */
  110. if (skb->len != CANXL_HDR_SIZE + cxl->len)
  111. return false;
  112. return cxl->flags & CANXL_XLF;
  113. }
  114. /* get length element value from can[|fd|xl]_frame structure */
  115. static inline unsigned int can_skb_get_len_val(struct sk_buff *skb)
  116. {
  117. const struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
  118. const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  119. if (can_is_canxl_skb(skb))
  120. return cxl->len;
  121. return cfd->len;
  122. }
  123. /* get needed data length inside CAN frame for all frame types (RTR aware) */
  124. static inline unsigned int can_skb_get_data_len(struct sk_buff *skb)
  125. {
  126. unsigned int len = can_skb_get_len_val(skb);
  127. const struct can_frame *cf = (struct can_frame *)skb->data;
  128. /* RTR frames have an actual length of zero */
  129. if (can_is_can_skb(skb) && cf->can_id & CAN_RTR_FLAG)
  130. return 0;
  131. return len;
  132. }
  133. #endif /* !_CAN_SKB_H */