skb.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * linux/can/skb.h
  3. *
  4. * Definitions for the CAN network socket buffer
  5. *
  6. * Copyright (C) 2012 Oliver Hartkopp <socketcan@hartkopp.net>
  7. *
  8. */
  9. #ifndef _CAN_SKB_H
  10. #define _CAN_SKB_H
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/can.h>
  14. #include <net/sock.h>
  15. /*
  16. * The struct can_skb_priv is used to transport additional information along
  17. * with the stored struct can(fd)_frame that can not be contained in existing
  18. * struct sk_buff elements.
  19. * N.B. that this information must not be modified in cloned CAN sk_buffs.
  20. * To modify the CAN frame content or the struct can_skb_priv content
  21. * skb_copy() needs to be used instead of skb_clone().
  22. */
  23. /**
  24. * struct can_skb_priv - private additional data inside CAN sk_buffs
  25. * @ifindex: ifindex of the first interface the CAN frame appeared on
  26. * @skbcnt: atomic counter to have an unique id together with skb pointer
  27. * @cf: align to the following CAN frame at skb->data
  28. */
  29. struct can_skb_priv {
  30. int ifindex;
  31. int skbcnt;
  32. struct can_frame cf[0];
  33. };
  34. static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
  35. {
  36. return (struct can_skb_priv *)(skb->head);
  37. }
  38. static inline void can_skb_reserve(struct sk_buff *skb)
  39. {
  40. skb_reserve(skb, sizeof(struct can_skb_priv));
  41. }
  42. static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
  43. {
  44. if (sk) {
  45. sock_hold(sk);
  46. skb->destructor = sock_efree;
  47. skb->sk = sk;
  48. }
  49. }
  50. /*
  51. * returns an unshared skb owned by the original sock to be echo'ed back
  52. */
  53. static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
  54. {
  55. if (skb_shared(skb)) {
  56. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  57. if (likely(nskb)) {
  58. can_skb_set_owner(nskb, skb->sk);
  59. consume_skb(skb);
  60. return nskb;
  61. } else {
  62. kfree_skb(skb);
  63. return NULL;
  64. }
  65. }
  66. /* we can assume to have an unshared skb with proper owner */
  67. return skb;
  68. }
  69. #endif /* !_CAN_SKB_H */