Commit | Line | Data |
---|---|---|
e3e4712e RP |
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 }, | |
a59166e4 | 32 | [MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 }, |
e3e4712e RP |
33 | }; |
34 | ||
35 | static 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 | ||
14972cbd | 41 | static int mpls_xmit(struct sk_buff *skb) |
e3e4712e RP |
42 | { |
43 | struct mpls_iptunnel_encap *tun_encap_info; | |
44 | struct mpls_shim_hdr *hdr; | |
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; | |
27d69105 | 52 | struct mpls_dev *out_mdev; |
a59166e4 | 53 | struct net *net; |
e3e4712e RP |
54 | int err = 0; |
55 | bool bos; | |
56 | int i; | |
57 | unsigned int ttl; | |
58 | ||
27d69105 RS |
59 | /* Find the output device */ |
60 | out_dev = dst->dev; | |
a59166e4 | 61 | net = dev_net(out_dev); |
e3e4712e RP |
62 | |
63 | skb_orphan(skb); | |
64 | ||
e3e4712e | 65 | if (!mpls_output_possible(out_dev) || |
61adedf3 | 66 | !dst->lwtstate || skb_warn_if_lro(skb)) |
e3e4712e RP |
67 | goto drop; |
68 | ||
69 | skb_forward_csum(skb); | |
70 | ||
61adedf3 | 71 | tun_encap_info = mpls_lwtunnel_encap(dst->lwtstate); |
e3e4712e | 72 | |
a59166e4 RS |
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 | ||
e3e4712e RP |
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) | |
113 | hh_len = 0; | |
114 | ||
115 | /* Ensure there is enough space for the headers in the skb */ | |
116 | if (skb_cow(skb, hh_len + new_header_size)) | |
117 | goto drop; | |
118 | ||
48d2ab60 DA |
119 | skb_set_inner_protocol(skb, skb->protocol); |
120 | skb_reset_inner_network_header(skb); | |
121 | ||
e3e4712e | 122 | skb_push(skb, new_header_size); |
48d2ab60 | 123 | |
e3e4712e RP |
124 | skb_reset_network_header(skb); |
125 | ||
126 | skb->dev = out_dev; | |
127 | skb->protocol = htons(ETH_P_MPLS_UC); | |
128 | ||
129 | /* Push the new labels */ | |
130 | hdr = mpls_hdr(skb); | |
131 | bos = true; | |
132 | for (i = tun_encap_info->labels - 1; i >= 0; i--) { | |
133 | hdr[i] = mpls_entry_encode(tun_encap_info->label[i], | |
134 | ttl, 0, bos); | |
135 | bos = false; | |
136 | } | |
137 | ||
27d69105 RS |
138 | mpls_stats_inc_outucastpkts(out_dev, skb); |
139 | ||
e3e4712e RP |
140 | if (rt) |
141 | err = neigh_xmit(NEIGH_ARP_TABLE, out_dev, &rt->rt_gateway, | |
142 | skb); | |
143 | else if (rt6) | |
144 | err = neigh_xmit(NEIGH_ND_TABLE, out_dev, &rt6->rt6i_gateway, | |
145 | skb); | |
146 | if (err) | |
147 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", | |
148 | __func__, err); | |
149 | ||
14972cbd | 150 | return LWTUNNEL_XMIT_DONE; |
e3e4712e RP |
151 | |
152 | drop: | |
27d69105 RS |
153 | out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL; |
154 | if (out_mdev) | |
155 | MPLS_INC_STATS(out_mdev, tx_errors); | |
e3e4712e RP |
156 | kfree_skb(skb); |
157 | return -EINVAL; | |
158 | } | |
159 | ||
30357d7d | 160 | static int mpls_build_state(struct nlattr *nla, |
127eb7cd | 161 | unsigned int family, const void *cfg, |
e3e4712e RP |
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; | |
e3e4712e RP |
167 | int ret; |
168 | ||
169 | ret = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla, | |
170 | mpls_iptunnel_policy); | |
171 | if (ret < 0) | |
172 | return ret; | |
173 | ||
174 | if (!tb[MPLS_IPTUNNEL_DST]) | |
175 | return -EINVAL; | |
176 | ||
e3e4712e | 177 | |
f76a9db3 | 178 | newts = lwtunnel_state_alloc(sizeof(*tun_encap_info)); |
e3e4712e RP |
179 | if (!newts) |
180 | return -ENOMEM; | |
181 | ||
e3e4712e RP |
182 | tun_encap_info = mpls_lwtunnel_encap(newts); |
183 | ret = nla_get_labels(tb[MPLS_IPTUNNEL_DST], MAX_NEW_LABELS, | |
184 | &tun_encap_info->labels, tun_encap_info->label); | |
185 | if (ret) | |
186 | goto errout; | |
a59166e4 RS |
187 | |
188 | tun_encap_info->ttl_propagate = MPLS_TTL_PROP_DEFAULT; | |
189 | ||
190 | if (tb[MPLS_IPTUNNEL_TTL]) { | |
191 | tun_encap_info->default_ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]); | |
192 | /* TTL 0 implies propagate from IP header */ | |
193 | tun_encap_info->ttl_propagate = tun_encap_info->default_ttl ? | |
194 | MPLS_TTL_PROP_DISABLED : | |
195 | MPLS_TTL_PROP_ENABLED; | |
196 | } | |
197 | ||
e3e4712e | 198 | newts->type = LWTUNNEL_ENCAP_MPLS; |
14972cbd RP |
199 | newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT; |
200 | newts->headroom = mpls_encap_size(tun_encap_info); | |
e3e4712e RP |
201 | |
202 | *ts = newts; | |
203 | ||
204 | return 0; | |
205 | ||
206 | errout: | |
207 | kfree(newts); | |
208 | *ts = NULL; | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
213 | static int mpls_fill_encap_info(struct sk_buff *skb, | |
214 | struct lwtunnel_state *lwtstate) | |
215 | { | |
216 | struct mpls_iptunnel_encap *tun_encap_info; | |
217 | ||
218 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); | |
219 | ||
220 | if (nla_put_labels(skb, MPLS_IPTUNNEL_DST, tun_encap_info->labels, | |
221 | tun_encap_info->label)) | |
222 | goto nla_put_failure; | |
223 | ||
a59166e4 RS |
224 | if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT && |
225 | nla_put_u8(skb, MPLS_IPTUNNEL_TTL, tun_encap_info->default_ttl)) | |
226 | goto nla_put_failure; | |
227 | ||
e3e4712e RP |
228 | return 0; |
229 | ||
230 | nla_put_failure: | |
231 | return -EMSGSIZE; | |
232 | } | |
233 | ||
234 | static int mpls_encap_nlsize(struct lwtunnel_state *lwtstate) | |
235 | { | |
236 | struct mpls_iptunnel_encap *tun_encap_info; | |
a59166e4 | 237 | int nlsize; |
e3e4712e RP |
238 | |
239 | tun_encap_info = mpls_lwtunnel_encap(lwtstate); | |
240 | ||
a59166e4 RS |
241 | nlsize = nla_total_size(tun_encap_info->labels * 4); |
242 | ||
243 | if (tun_encap_info->ttl_propagate != MPLS_TTL_PROP_DEFAULT) | |
244 | nlsize += nla_total_size(1); | |
245 | ||
246 | return nlsize; | |
e3e4712e RP |
247 | } |
248 | ||
249 | static int mpls_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) | |
250 | { | |
251 | struct mpls_iptunnel_encap *a_hdr = mpls_lwtunnel_encap(a); | |
252 | struct mpls_iptunnel_encap *b_hdr = mpls_lwtunnel_encap(b); | |
253 | int l; | |
254 | ||
a59166e4 RS |
255 | if (a_hdr->labels != b_hdr->labels || |
256 | a_hdr->ttl_propagate != b_hdr->ttl_propagate || | |
257 | a_hdr->default_ttl != b_hdr->default_ttl) | |
e3e4712e RP |
258 | return 1; |
259 | ||
260 | for (l = 0; l < MAX_NEW_LABELS; l++) | |
261 | if (a_hdr->label[l] != b_hdr->label[l]) | |
262 | return 1; | |
263 | return 0; | |
264 | } | |
265 | ||
266 | static const struct lwtunnel_encap_ops mpls_iptun_ops = { | |
267 | .build_state = mpls_build_state, | |
14972cbd | 268 | .xmit = mpls_xmit, |
e3e4712e RP |
269 | .fill_encap = mpls_fill_encap_info, |
270 | .get_encap_size = mpls_encap_nlsize, | |
271 | .cmp_encap = mpls_encap_cmp, | |
88ff7334 | 272 | .owner = THIS_MODULE, |
e3e4712e RP |
273 | }; |
274 | ||
275 | static int __init mpls_iptunnel_init(void) | |
276 | { | |
277 | return lwtunnel_encap_add_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); | |
278 | } | |
279 | module_init(mpls_iptunnel_init); | |
280 | ||
281 | static void __exit mpls_iptunnel_exit(void) | |
282 | { | |
283 | lwtunnel_encap_del_ops(&mpls_iptun_ops, LWTUNNEL_ENCAP_MPLS); | |
284 | } | |
285 | module_exit(mpls_iptunnel_exit); | |
286 | ||
b2b04edc | 287 | MODULE_ALIAS_RTNL_LWT(MPLS); |
e3e4712e RP |
288 | MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels"); |
289 | MODULE_LICENSE("GPL v2"); |