nsh.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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. struct sk_buff *segs = ERR_PTR(-EINVAL);
  68. unsigned int nsh_len, mac_len;
  69. __be16 proto;
  70. int nhoff;
  71. skb_reset_network_header(skb);
  72. nhoff = skb->network_header - skb->mac_header;
  73. mac_len = skb->mac_len;
  74. if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
  75. goto out;
  76. nsh_len = nsh_hdr_len(nsh_hdr(skb));
  77. if (unlikely(!pskb_may_pull(skb, nsh_len)))
  78. goto out;
  79. proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
  80. if (!proto)
  81. goto out;
  82. __skb_pull(skb, nsh_len);
  83. skb_reset_mac_header(skb);
  84. skb_reset_mac_len(skb);
  85. skb->protocol = proto;
  86. features &= NETIF_F_SG;
  87. segs = skb_mac_gso_segment(skb, features);
  88. if (IS_ERR_OR_NULL(segs)) {
  89. skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
  90. skb->network_header - nhoff,
  91. mac_len);
  92. goto out;
  93. }
  94. for (skb = segs; skb; skb = skb->next) {
  95. skb->protocol = htons(ETH_P_NSH);
  96. __skb_push(skb, nsh_len);
  97. skb_set_mac_header(skb, -nhoff);
  98. skb->network_header = skb->mac_header + mac_len;
  99. skb->mac_len = mac_len;
  100. }
  101. out:
  102. return segs;
  103. }
  104. static struct packet_offload nsh_packet_offload __read_mostly = {
  105. .type = htons(ETH_P_NSH),
  106. .priority = 15,
  107. .callbacks = {
  108. .gso_segment = nsh_gso_segment,
  109. },
  110. };
  111. static int __init nsh_init_module(void)
  112. {
  113. dev_add_offload(&nsh_packet_offload);
  114. return 0;
  115. }
  116. static void __exit nsh_cleanup_module(void)
  117. {
  118. dev_remove_offload(&nsh_packet_offload);
  119. }
  120. module_init(nsh_init_module);
  121. module_exit(nsh_cleanup_module);
  122. MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
  123. MODULE_DESCRIPTION("NSH protocol");
  124. MODULE_LICENSE("GPL v2");