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