nsh.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Network Service Header
  4. *
  5. * Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <net/gso.h>
  11. #include <net/nsh.h>
  12. #include <net/tun_proto.h>
  13. int nsh_push(struct sk_buff *skb, const struct nshhdr *pushed_nh)
  14. {
  15. struct nshhdr *nh;
  16. size_t length = nsh_hdr_len(pushed_nh);
  17. u8 next_proto;
  18. if (skb->mac_len) {
  19. next_proto = TUN_P_ETHERNET;
  20. } else {
  21. next_proto = tun_p_from_eth_p(skb->protocol);
  22. if (!next_proto)
  23. return -EAFNOSUPPORT;
  24. }
  25. /* Add the NSH header */
  26. if (skb_cow_head(skb, length) < 0)
  27. return -ENOMEM;
  28. skb_push(skb, length);
  29. nh = (struct nshhdr *)(skb->data);
  30. memcpy(nh, pushed_nh, length);
  31. nh->np = next_proto;
  32. skb_postpush_rcsum(skb, nh, length);
  33. skb->protocol = htons(ETH_P_NSH);
  34. skb_reset_mac_header(skb);
  35. skb_reset_network_header(skb);
  36. skb_reset_mac_len(skb);
  37. return 0;
  38. }
  39. EXPORT_SYMBOL_GPL(nsh_push);
  40. int nsh_pop(struct sk_buff *skb)
  41. {
  42. struct nshhdr *nh;
  43. size_t length;
  44. __be16 inner_proto;
  45. if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
  46. return -ENOMEM;
  47. nh = (struct nshhdr *)(skb->data);
  48. length = nsh_hdr_len(nh);
  49. if (length < NSH_BASE_HDR_LEN)
  50. return -EINVAL;
  51. inner_proto = tun_p_to_eth_p(nh->np);
  52. if (!pskb_may_pull(skb, length))
  53. return -ENOMEM;
  54. if (!inner_proto)
  55. return -EAFNOSUPPORT;
  56. skb_pull_rcsum(skb, length);
  57. skb_reset_mac_header(skb);
  58. skb_reset_network_header(skb);
  59. skb_reset_mac_len(skb);
  60. skb->protocol = inner_proto;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(nsh_pop);
  64. static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
  65. netdev_features_t features)
  66. {
  67. unsigned int outer_hlen, mac_len, nsh_len;
  68. struct sk_buff *segs = ERR_PTR(-EINVAL);
  69. u16 mac_offset = skb->mac_header;
  70. __be16 outer_proto, proto;
  71. skb_reset_network_header(skb);
  72. outer_proto = skb->protocol;
  73. outer_hlen = skb_mac_header_len(skb);
  74. mac_len = skb->mac_len;
  75. if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
  76. goto out;
  77. nsh_len = nsh_hdr_len(nsh_hdr(skb));
  78. if (nsh_len < NSH_BASE_HDR_LEN)
  79. goto out;
  80. if (unlikely(!pskb_may_pull(skb, nsh_len)))
  81. goto out;
  82. proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
  83. if (!proto)
  84. goto out;
  85. __skb_pull(skb, nsh_len);
  86. skb_reset_mac_header(skb);
  87. skb->mac_len = proto == htons(ETH_P_TEB) ? ETH_HLEN : 0;
  88. skb->protocol = proto;
  89. features &= NETIF_F_SG;
  90. segs = skb_mac_gso_segment(skb, features);
  91. if (IS_ERR_OR_NULL(segs)) {
  92. skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
  93. mac_offset, mac_len);
  94. goto out;
  95. }
  96. for (skb = segs; skb; skb = skb->next) {
  97. skb->protocol = outer_proto;
  98. __skb_push(skb, nsh_len + outer_hlen);
  99. skb_reset_mac_header(skb);
  100. skb_set_network_header(skb, outer_hlen);
  101. skb->mac_len = mac_len;
  102. }
  103. out:
  104. return segs;
  105. }
  106. static struct packet_offload nsh_packet_offload __read_mostly = {
  107. .type = htons(ETH_P_NSH),
  108. .priority = 15,
  109. .callbacks = {
  110. .gso_segment = nsh_gso_segment,
  111. },
  112. };
  113. static int __init nsh_init_module(void)
  114. {
  115. dev_add_offload(&nsh_packet_offload);
  116. return 0;
  117. }
  118. static void __exit nsh_cleanup_module(void)
  119. {
  120. dev_remove_offload(&nsh_packet_offload);
  121. }
  122. module_init(nsh_init_module);
  123. module_exit(nsh_cleanup_module);
  124. MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
  125. MODULE_DESCRIPTION("NSH protocol");
  126. MODULE_LICENSE("GPL v2");