nsh.c 3.4 KB

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