mpls_iptunnel.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. static int mpls_xmit(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_set_inner_protocol(skb, skb->protocol);
  82. skb_reset_inner_network_header(skb);
  83. skb_push(skb, new_header_size);
  84. skb_reset_network_header(skb);
  85. skb->dev = out_dev;
  86. skb->protocol = htons(ETH_P_MPLS_UC);
  87. /* Push the new labels */
  88. hdr = mpls_hdr(skb);
  89. bos = true;
  90. for (i = tun_encap_info->labels - 1; i >= 0; i--) {
  91. hdr[i] = mpls_entry_encode(tun_encap_info->label[i],
  92. ttl, 0, bos);
  93. bos = false;
  94. }
  95. if (rt)
  96. err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway,
  97. skb);
  98. else if (rt6)
  99. err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway,
  100. skb);
  101. if (err)
  102. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  103. __func__, err);
  104. return LWTUNNEL_XMIT_DONE;
  105. drop:
  106. kfree_skb(skb);
  107. return -EINVAL;
  108. }
  109. static int mpls_build_state(struct net_device *dev, struct nlattr *nla,
  110. unsigned int family, const void *cfg,
  111. struct lwtunnel_state **ts)
  112. {
  113. struct mpls_iptunnel_encap *tun_encap_info;
  114. struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
  115. struct lwtunnel_state *newts;
  116. int tun_encap_info_len;
  117. int ret;
  118. ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla,
  119. mpls_iptunnel_policy);
  120. if (ret < 0)
  121. return ret;
  122. if (!tb[MPLS_IPTUNNEL_DST])
  123. return -EINVAL;
  124. tun_encap_info_len = sizeof(*tun_encap_info);
  125. newts = lwtunnel_state_alloc(tun_encap_info_len);
  126. if (!newts)
  127. return -ENOMEM;
  128. newts->len = tun_encap_info_len;
  129. tun_encap_info = mpls_lwtunnel_encap(newts);
  130. ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS,
  131. &tun_encap_info->labels, tun_encap_info->label);
  132. if (ret)
  133. goto errout;
  134. newts->type = LWTUNNEL_ENCAP_MPLS;
  135. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  136. newts->headroom = mpls_encap_size(tun_encap_info);
  137. *ts = newts;
  138. return 0;
  139. errout:
  140. kfree(newts);
  141. *ts = NULL;
  142. return ret;
  143. }
  144. static int mpls_fill_encap_info(struct sk_buff *skb,
  145. struct lwtunnel_state *lwtstate)
  146. {
  147. struct mpls_iptunnel_encap *tun_encap_info;
  148. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  149. if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
  150. tun_encap_info->label))
  151. goto nla_put_failure;
  152. return 0;
  153. nla_put_failure:
  154. return -EMSGSIZE;
  155. }
  156. static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
  157. {
  158. struct mpls_iptunnel_encap *tun_encap_info;
  159. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  160. return nla_total_size(tun_encap_info->labels * 4);
  161. }
  162. static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  163. {
  164. struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
  165. struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
  166. int l;
  167. if (a_hdr->labels != b_hdr->labels)
  168. return 1;
  169. for (l = 0; l < MAX_NEW_LABELS; l++)
  170. if (a_hdr->label[l] != b_hdr->label[l])
  171. return 1;
  172. return 0;
  173. }
  174. static const struct lwtunnel_encap_ops mpls_iptun_ops = {
  175. .build_state = mpls_build_state,
  176. .xmit = mpls_xmit,
  177. .fill_encap = mpls_fill_encap_info,
  178. .get_encap_size = mpls_encap_nlsize,
  179. .cmp_encap = mpls_encap_cmp,
  180. .owner = THIS_MODULE,
  181. };
  182. static int __init mpls_iptunnel_init(void)
  183. {
  184. return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  185. }
  186. module_init(mpls_iptunnel_init);
  187. static void __exit mpls_iptunnel_exit(void)
  188. {
  189. lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  190. }
  191. module_exit(mpls_iptunnel_exit);
  192. MODULE_ALIAS_RTNL_LWT(MPLS);
  193. MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
  194. MODULE_LICENSE("GPL v2");