mpls_iptunnel.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * mpls tunnels An implementation mpls tunnels using the light weight tunnel
  3. * infrastructure
  4. *
  5. * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. */
  13. #include <linux/types.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/net.h>
  16. #include <linux/module.h>
  17. #include <linux/mpls.h>
  18. #include <linux/vmalloc.h>
  19. #include <net/ip.h>
  20. #include <net/dst.h>
  21. #include <net/lwtunnel.h>
  22. #include <net/netevent.h>
  23. #include <net/netns/generic.h>
  24. #include <net/ip6_fib.h>
  25. #include <net/route.h>
  26. #include <net/mpls_iptunnel.h>
  27. #include <linux/mpls_iptunnel.h>
  28. #include "internal.h"
  29. static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = {
  30. [MPLS_IPTUNNEL_DST] = { .type = NLA_U32 },
  31. };
  32. static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en)
  33. {
  34. /* The size of the layer 2.5 labels to be added for this route */
  35. return en->labels * sizeof(struct mpls_shim_hdr);
  36. }
  37. int mpls_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  38. {
  39. struct mpls_iptunnel_encap *tun_encap_info;
  40. struct mpls_shim_hdr *hdr;
  41. struct net_device *out_dev;
  42. unsigned int hh_len;
  43. unsigned int new_header_size;
  44. unsigned int mtu;
  45. struct dst_entry *dst = skb_dst(skb);
  46. struct rtable *rt = NULL;
  47. struct rt6_info *rt6 = NULL;
  48. int err = 0;
  49. bool bos;
  50. int i;
  51. unsigned int ttl;
  52. /* Obtain the ttl */
  53. if (dst->ops->family == AF_INET) {
  54. ttl = ip_hdr(skb)->ttl;
  55. rt = (struct rtable *)dst;
  56. } else if (dst->ops->family == AF_INET6) {
  57. ttl = ipv6_hdr(skb)->hop_limit;
  58. rt6 = (struct rt6_info *)dst;
  59. } else {
  60. goto drop;
  61. }
  62. skb_orphan(skb);
  63. /* Find the output device */
  64. out_dev = dst->dev;
  65. if (!mpls_output_possible(out_dev) ||
  66. !dst->lwtstate || skb_warn_if_lro(skb))
  67. goto drop;
  68. skb_forward_csum(skb);
  69. tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate);
  70. /* Verify the destination can hold the packet */
  71. new_header_size = mpls_encap_size(tun_encap_info);
  72. mtu = mpls_dev_mtu(out_dev);
  73. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  74. goto drop;
  75. hh_len = LL_RESERVED_SPACE(out_dev);
  76. if (!out_dev->header_ops)
  77. hh_len = 0;
  78. /* Ensure there is enough space for the headers in the skb */
  79. if (skb_cow(skb, hh_len + new_header_size))
  80. goto drop;
  81. skb_push(skb, new_header_size);
  82. skb_reset_network_header(skb);
  83. skb->dev = out_dev;
  84. skb->protocol = htons(ETH_P_MPLS_UC);
  85. /* Push the new labels */
  86. hdr = mpls_hdr(skb);
  87. bos = true;
  88. for (i = tun_encap_info->labels - 1; i >= 0; i--) {
  89. hdr[i] = mpls_entry_encode(tun_encap_info->label[i],
  90. ttl, 0, bos);
  91. bos = false;
  92. }
  93. if (rt)
  94. err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway,
  95. skb);
  96. else if (rt6)
  97. err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway,
  98. skb);
  99. if (err)
  100. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  101. __func__, err);
  102. return 0;
  103. drop:
  104. kfree_skb(skb);
  105. return -EINVAL;
  106. }
  107. static int mpls_build_state(struct net_device *dev, struct nlattr *nla,
  108. unsigned int family, const void *cfg,
  109. struct lwtunnel_state **ts)
  110. {
  111. struct mpls_iptunnel_encap *tun_encap_info;
  112. struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
  113. struct lwtunnel_state *newts;
  114. int tun_encap_info_len;
  115. int ret;
  116. ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla,
  117. mpls_iptunnel_policy);
  118. if (ret < 0)
  119. return ret;
  120. if (!tb[MPLS_IPTUNNEL_DST])
  121. return -EINVAL;
  122. tun_encap_info_len = sizeof(*tun_encap_info);
  123. newts = lwtunnel_state_alloc(tun_encap_info_len);
  124. if (!newts)
  125. return -ENOMEM;
  126. newts->len = tun_encap_info_len;
  127. tun_encap_info = mpls_lwtunnel_encap(newts);
  128. ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS,
  129. &tun_encap_info->labels, tun_encap_info->label);
  130. if (ret)
  131. goto errout;
  132. newts->type = LWTUNNEL_ENCAP_MPLS;
  133. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  134. *ts = newts;
  135. return 0;
  136. errout:
  137. kfree(newts);
  138. *ts = NULL;
  139. return ret;
  140. }
  141. static int mpls_fill_encap_info(struct sk_buff *skb,
  142. struct lwtunnel_state *lwtstate)
  143. {
  144. struct mpls_iptunnel_encap *tun_encap_info;
  145. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  146. if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
  147. tun_encap_info->label))
  148. goto nla_put_failure;
  149. return 0;
  150. nla_put_failure:
  151. return -EMSGSIZE;
  152. }
  153. static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
  154. {
  155. struct mpls_iptunnel_encap *tun_encap_info;
  156. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  157. return nla_total_size(tun_encap_info->labels * 4);
  158. }
  159. static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  160. {
  161. struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
  162. struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
  163. int l;
  164. if (a_hdr->labels != b_hdr->labels)
  165. return 1;
  166. for (l = 0; l < MAX_NEW_LABELS; l++)
  167. if (a_hdr->label[l] != b_hdr->label[l])
  168. return 1;
  169. return 0;
  170. }
  171. static const struct lwtunnel_encap_ops mpls_iptun_ops = {
  172. .build_state = mpls_build_state,
  173. .output = mpls_output,
  174. .fill_encap = mpls_fill_encap_info,
  175. .get_encap_size = mpls_encap_nlsize,
  176. .cmp_encap = mpls_encap_cmp,
  177. };
  178. static int __init mpls_iptunnel_init(void)
  179. {
  180. return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  181. }
  182. module_init(mpls_iptunnel_init);
  183. static void __exit mpls_iptunnel_exit(void)
  184. {
  185. lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  186. }
  187. module_exit(mpls_iptunnel_exit);
  188. MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
  189. MODULE_LICENSE("GPL v2");