mpls_iptunnel.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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] = { .len = sizeof(u32) },
  31. [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 },
  32. };
  33. static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en)
  34. {
  35. /* The size of the layer 2.5 labels to be added for this route */
  36. return en->labels * sizeof(struct mpls_shim_hdr);
  37. }
  38. static int mpls_xmit(struct sk_buff *skb)
  39. {
  40. struct mpls_iptunnel_encap *tun_encap_info;
  41. struct mpls_shim_hdr *hdr;
  42. struct net_device *out_dev;
  43. unsigned int hh_len;
  44. unsigned int new_header_size;
  45. unsigned int mtu;
  46. struct dst_entry *dst = skb_dst(skb);
  47. struct rtable *rt = NULL;
  48. struct rt6_info *rt6 = NULL;
  49. struct mpls_dev *out_mdev;
  50. struct net *net;
  51. int err = 0;
  52. bool bos;
  53. int i;
  54. unsigned int ttl;
  55. /* Find the output device */
  56. out_dev = dst->dev;
  57. net = dev_net(out_dev);
  58. skb_orphan(skb);
  59. if (!mpls_output_possible(out_dev) ||
  60. !dst->lwtstate || skb_warn_if_lro(skb))
  61. goto drop;
  62. skb_forward_csum(skb);
  63. tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate);
  64. /* Obtain the ttl using the following set of rules.
  65. *
  66. * LWT ttl propagation setting:
  67. * - disabled => use default TTL value from LWT
  68. * - enabled => use TTL value from IPv4/IPv6 header
  69. * - default =>
  70. * Global ttl propagation setting:
  71. * - disabled => use default TTL value from global setting
  72. * - enabled => use TTL value from IPv4/IPv6 header
  73. */
  74. if (dst->ops->family == AF_INET) {
  75. if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED)
  76. ttl = tun_encap_info->default_ttl;
  77. else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  78. !net->mpls.ip_ttl_propagate)
  79. ttl = net->mpls.default_ttl;
  80. else
  81. ttl = ip_hdr(skb)->ttl;
  82. rt = (struct rtable *)dst;
  83. } else if (dst->ops->family == AF_INET6) {
  84. if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED)
  85. ttl = tun_encap_info->default_ttl;
  86. else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  87. !net->mpls.ip_ttl_propagate)
  88. ttl = net->mpls.default_ttl;
  89. else
  90. ttl = ipv6_hdr(skb)->hop_limit;
  91. rt6 = (struct rt6_info *)dst;
  92. } else {
  93. goto drop;
  94. }
  95. /* Verify the destination can hold the packet */
  96. new_header_size = mpls_encap_size(tun_encap_info);
  97. mtu = mpls_dev_mtu(out_dev);
  98. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  99. goto drop;
  100. hh_len = LL_RESERVED_SPACE(out_dev);
  101. if (!out_dev->header_ops)
  102. hh_len = 0;
  103. /* Ensure there is enough space for the headers in the skb */
  104. if (skb_cow(skb, hh_len + new_header_size))
  105. goto drop;
  106. skb_set_inner_protocol(skb, skb->protocol);
  107. skb_reset_inner_network_header(skb);
  108. skb_push(skb, new_header_size);
  109. skb_reset_network_header(skb);
  110. skb->dev = out_dev;
  111. skb->protocol = htons(ETH_P_MPLS_UC);
  112. /* Push the new labels */
  113. hdr = mpls_hdr(skb);
  114. bos = true;
  115. for (i = tun_encap_info->labels - 1; i >= 0; i--) {
  116. hdr[i] = mpls_entry_encode(tun_encap_info->label[i],
  117. ttl, 0, bos);
  118. bos = false;
  119. }
  120. mpls_stats_inc_outucastpkts(out_dev, skb);
  121. if (rt)
  122. err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway,
  123. skb);
  124. else if (rt6)
  125. err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway,
  126. skb);
  127. if (err)
  128. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  129. __func__, err);
  130. return LWTUNNEL_XMIT_DONE;
  131. drop:
  132. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  133. if (out_mdev)
  134. MPLS_INC_STATS(out_mdev, tx_errors);
  135. kfree_skb(skb);
  136. return -EINVAL;
  137. }
  138. static int mpls_build_state(struct nlattr *nla,
  139. unsigned int family, const void *cfg,
  140. struct lwtunnel_state **ts,
  141. struct netlink_ext_ack *extack)
  142. {
  143. struct mpls_iptunnel_encap *tun_encap_info;
  144. struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
  145. struct lwtunnel_state *newts;
  146. u8 n_labels;
  147. int ret;
  148. ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla,
  149. mpls_iptunnel_policy, extack);
  150. if (ret < 0)
  151. return ret;
  152. if (!tb[MPLS_IPTUNNEL_DST]) {
  153. NL_SET_ERR_MSG(extack, "MPLS_IPTUNNEL_DST attribute is missing");
  154. return -EINVAL;
  155. }
  156. /* determine number of labels */
  157. if (nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS,
  158. &n_labels, NULL, extack))
  159. return -EINVAL;
  160. newts = lwtunnel_state_alloc(sizeof(*tun_encap_info) +
  161. n_labels * sizeof(u32));
  162. if (!newts)
  163. return -ENOMEM;
  164. tun_encap_info = mpls_lwtunnel_encap(newts);
  165. ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], n_labels,
  166. &tun_encap_info->labels, tun_encap_info->label,
  167. extack);
  168. if (ret)
  169. goto errout;
  170. tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  171. if (tb[MPLS_IPTUNNEL_TTL]) {
  172. tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]);
  173. /* TTL 0 implies propagate from IP header */
  174. tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ?
  175. MPLS_TTL_PROP_DISABLED :
  176. MPLS_TTL_PROP_ENABLED;
  177. }
  178. newts->type = LWTUNNEL_ENCAP_MPLS;
  179. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  180. newts->headroom = mpls_encap_size(tun_encap_info);
  181. *ts = newts;
  182. return 0;
  183. errout:
  184. kfree(newts);
  185. *ts = NULL;
  186. return ret;
  187. }
  188. static int mpls_fill_encap_info(struct sk_buff *skb,
  189. struct lwtunnel_state *lwtstate)
  190. {
  191. struct mpls_iptunnel_encap *tun_encap_info;
  192. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  193. if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
  194. tun_encap_info->label))
  195. goto nla_put_failure;
  196. if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT &&
  197. nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl))
  198. goto nla_put_failure;
  199. return 0;
  200. nla_put_failure:
  201. return -EMSGSIZE;
  202. }
  203. static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
  204. {
  205. struct mpls_iptunnel_encap *tun_encap_info;
  206. int nlsize;
  207. tun_encap_info = mpls_lwtunnel_encap(lwtstate);
  208. nlsize = nla_total_size(tun_encap_info->labels * 4);
  209. if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT)
  210. nlsize += nla_total_size(1);
  211. return nlsize;
  212. }
  213. static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  214. {
  215. struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
  216. struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
  217. int l;
  218. if (a_hdr->labels != b_hdr->labels ||
  219. a_hdr->ttl_propagate != b_hdr->ttl_propagate ||
  220. a_hdr->default_ttl != b_hdr->default_ttl)
  221. return 1;
  222. for (l = 0; l < a_hdr->labels; l++)
  223. if (a_hdr->label[l] != b_hdr->label[l])
  224. return 1;
  225. return 0;
  226. }
  227. static const struct lwtunnel_encap_ops mpls_iptun_ops = {
  228. .build_state = mpls_build_state,
  229. .xmit = mpls_xmit,
  230. .fill_encap = mpls_fill_encap_info,
  231. .get_encap_size = mpls_encap_nlsize,
  232. .cmp_encap = mpls_encap_cmp,
  233. .owner = THIS_MODULE,
  234. };
  235. static int __init mpls_iptunnel_init(void)
  236. {
  237. return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  238. }
  239. module_init(mpls_iptunnel_init);
  240. static void __exit mpls_iptunnel_exit(void)
  241. {
  242. lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS);
  243. }
  244. module_exit(mpls_iptunnel_exit);
  245. MODULE_ALIAS_RTNL_LWT(MPLS);
  246. MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
  247. MODULE_LICENSE("GPL v2");