mpls_iptunnel.c (b70366e5d31788650b2a5cec5cd13ea80ac7e44a) mpls_iptunnel.c (1511009cd68015c2e04135bfefa4bf5020baa8d9)
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

--- 15 unchanged lines hidden (view full) ---

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
30static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = {
31 [MPLS_IPTUNNEL_DST] = { .type = NLA_U32 },
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

--- 15 unchanged lines hidden (view full) ---

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
30static const struct nla_policy mpls_iptunnel_policy[MPLS_IPTUNNEL_MAX + 1] = {
31 [MPLS_IPTUNNEL_DST] = { .type = NLA_U32 },
32 [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 },
32};
33
34static 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

--- 4 unchanged lines hidden (view full) ---

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;
33};
34
35static unsigned int mpls_encap_size(struct mpls_iptunnel_encap *en)
36{
37 /* The size of the layer 2.5 labels to be added for this route */
38 return en->labels * sizeof(struct mpls_shim_hdr);
39}
40

--- 4 unchanged lines hidden (view full) ---

45 struct net_device *out_dev;
46 unsigned int hh_len;
47 unsigned int new_header_size;
48 unsigned int mtu;
49 struct dst_entry *dst = skb_dst(skb);
50 struct rtable *rt = NULL;
51 struct rt6_info *rt6 = NULL;
52 struct mpls_dev *out_mdev;
53 struct net *net;
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;
54 int err = 0;
55 bool bos;
56 int i;
57 unsigned int ttl;
58
59 /* Find the output device */
60 out_dev = dst->dev;
61 net = dev_net(out_dev);
59
62
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
63 skb_orphan(skb);
64
65 if (!mpls_output_possible(out_dev) ||
66 !dst->lwtstate || skb_warn_if_lro(skb))
67 goto drop;
68
69 skb_forward_csum(skb);
70
71 tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate);
72
73 /* Obtain the ttl using the following set of rules.
74 *
75 * LWT ttl propagation setting:
76 * - disabled => use default TTL value from LWT
77 * - enabled => use TTL value from IPv4/IPv6 header
78 * - default =>
79 * Global ttl propagation setting:
80 * - disabled => use default TTL value from global setting
81 * - enabled => use TTL value from IPv4/IPv6 header
82 */
83 if (dst->ops->family == AF_INET) {
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 = ip_hdr(skb)->ttl;
91 rt = (struct rtable *)dst;
92 } else if (dst->ops->family == AF_INET6) {
93 if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DISABLED)
94 ttl = tun_encap_info->default_ttl;
95 else if (tun_encap_info->ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
96 !net->mpls.ip_ttl_propagate)
97 ttl = net->mpls.default_ttl;
98 else
99 ttl = ipv6_hdr(skb)->hop_limit;
100 rt6 = (struct rt6_info *)dst;
101 } else {
102 goto drop;
103 }
104
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)

--- 46 unchanged lines hidden (view full) ---

135
136static 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;
105 /* Verify the destination can hold the packet */
106 new_header_size = mpls_encap_size(tun_encap_info);
107 mtu = mpls_dev_mtu(out_dev);
108 if (mpls_pkt_too_big(skb, mtu - new_header_size))
109 goto drop;
110
111 hh_len = LL_RESERVED_SPACE(out_dev);
112 if (!out_dev->header_ops)

--- 46 unchanged lines hidden (view full) ---

159
160static int mpls_build_state(struct nlattr *nla,
161 unsigned int family, const void *cfg,
162 struct lwtunnel_state **ts)
163{
164 struct mpls_iptunnel_encap *tun_encap_info;
165 struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
166 struct lwtunnel_state *newts;
167 u8 n_labels;
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
168 int ret;
169
170 ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla,
171 mpls_iptunnel_policy);
172 if (ret < 0)
173 return ret;
174
175 if (!tb[MPLS_IPTUNNEL_DST])
176 return -EINVAL;
177
178
154 newts = lwtunnel_state_alloc(sizeof(*tun_encap_info));
179 /* determine number of labels */
180 if (nla_get_labels(tb[MPLS_IPTUNNEL_DST],
181 MAX_NEW_LABELS, &n_labels, NULL))
182 return -EINVAL;
183
184 newts = lwtunnel_state_alloc(sizeof(*tun_encap_info) +
185 n_labels * sizeof(u32));
155 if (!newts)
156 return -ENOMEM;
157
158 tun_encap_info = mpls_lwtunnel_encap(newts);
186 if (!newts)
187 return -ENOMEM;
188
189 tun_encap_info = mpls_lwtunnel_encap(newts);
159 ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS,
190 ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], n_labels,
160 &tun_encap_info->labels, tun_encap_info->label);
161 if (ret)
162 goto errout;
191 &tun_encap_info->labels, tun_encap_info->label);
192 if (ret)
193 goto errout;
194
195 tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT;
196
197 if (tb[MPLS_IPTUNNEL_TTL]) {
198 tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]);
199 /* TTL 0 implies propagate from IP header */
200 tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ?
201 MPLS_TTL_PROP_DISABLED :
202 MPLS_TTL_PROP_ENABLED;
203 }
204
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

--- 10 unchanged lines hidden (view full) ---

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
205 newts->type = LWTUNNEL_ENCAP_MPLS;
206 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
207 newts->headroom = mpls_encap_size(tun_encap_info);
208
209 *ts = newts;
210
211 return 0;
212

--- 10 unchanged lines hidden (view full) ---

223 struct mpls_iptunnel_encap *tun_encap_info;
224
225 tun_encap_info = mpls_lwtunnel_encap(lwtstate);
226
227 if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels,
228 tun_encap_info->label))
229 goto nla_put_failure;
230
231 if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT &&
232 nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl))
233 goto nla_put_failure;
234
189 return 0;
190
191nla_put_failure:
192 return -EMSGSIZE;
193}
194
195static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
196{
197 struct mpls_iptunnel_encap *tun_encap_info;
235 return 0;
236
237nla_put_failure:
238 return -EMSGSIZE;
239}
240
241static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate)
242{
243 struct mpls_iptunnel_encap *tun_encap_info;
244 int nlsize;
198
199 tun_encap_info = mpls_lwtunnel_encap(lwtstate);
200
245
246 tun_encap_info = mpls_lwtunnel_encap(lwtstate);
247
201 return nla_total_size(tun_encap_info->labels * 4);
248 nlsize = nla_total_size(tun_encap_info->labels * 4);
249
250 if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT)
251 nlsize += nla_total_size(1);
252
253 return nlsize;
202}
203
204static 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
254}
255
256static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
257{
258 struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a);
259 struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b);
260 int l;
261
210 if (a_hdr->labels != b_hdr->labels)
262 if (a_hdr->labels != b_hdr->labels ||
263 a_hdr->ttl_propagate != b_hdr->ttl_propagate ||
264 a_hdr->default_ttl != b_hdr->default_ttl)
211 return 1;
212
265 return 1;
266
213 for (l = 0; l < MAX_NEW_LABELS; l++)
267 for (l = 0; l < a_hdr->labels; l++)
214 if (a_hdr->label[l] != b_hdr->label[l])
215 return 1;
216 return 0;
217}
218
219static const struct lwtunnel_encap_ops mpls_iptun_ops = {
220 .build_state = mpls_build_state,
221 .xmit = mpls_xmit,

--- 21 unchanged lines hidden ---
268 if (a_hdr->label[l] != b_hdr->label[l])
269 return 1;
270 return 0;
271}
272
273static const struct lwtunnel_encap_ops mpls_iptun_ops = {
274 .build_state = mpls_build_state,
275 .xmit = mpls_xmit,

--- 21 unchanged lines hidden ---