nsh.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
  16. netdev_features_t features)
  17. {
  18. struct sk_buff *segs = ERR_PTR(-EINVAL);
  19. unsigned int nsh_len, mac_len;
  20. __be16 proto;
  21. int nhoff;
  22. skb_reset_network_header(skb);
  23. nhoff = skb->network_header - skb->mac_header;
  24. mac_len = skb->mac_len;
  25. if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
  26. goto out;
  27. nsh_len = nsh_hdr_len(nsh_hdr(skb));
  28. if (unlikely(!pskb_may_pull(skb, nsh_len)))
  29. goto out;
  30. proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
  31. if (!proto)
  32. goto out;
  33. __skb_pull(skb, nsh_len);
  34. skb_reset_mac_header(skb);
  35. skb_reset_mac_len(skb);
  36. skb->protocol = proto;
  37. features &= NETIF_F_SG;
  38. segs = skb_mac_gso_segment(skb, features);
  39. if (IS_ERR_OR_NULL(segs)) {
  40. skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
  41. skb->network_header - nhoff,
  42. mac_len);
  43. goto out;
  44. }
  45. for (skb = segs; skb; skb = skb->next) {
  46. skb->protocol = htons(ETH_P_NSH);
  47. __skb_push(skb, nsh_len);
  48. skb_set_mac_header(skb, -nhoff);
  49. skb->network_header = skb->mac_header + mac_len;
  50. skb->mac_len = mac_len;
  51. }
  52. out:
  53. return segs;
  54. }
  55. static struct packet_offload nsh_packet_offload __read_mostly = {
  56. .type = htons(ETH_P_NSH),
  57. .priority = 15,
  58. .callbacks = {
  59. .gso_segment = nsh_gso_segment,
  60. },
  61. };
  62. static int __init nsh_init_module(void)
  63. {
  64. dev_add_offload(&nsh_packet_offload);
  65. return 0;
  66. }
  67. static void __exit nsh_cleanup_module(void)
  68. {
  69. dev_remove_offload(&nsh_packet_offload);
  70. }
  71. module_init(nsh_init_module);
  72. module_exit(nsh_cleanup_module);
  73. MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
  74. MODULE_DESCRIPTION("NSH protocol");
  75. MODULE_LICENSE("GPL v2");