nsh.c 2.1 KB

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