2 * mpls tunnels An implementation mpls tunnels using the light weight tunnel
5 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
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.
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>
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>
30 static const struct nla_policy mpls_iptunnel_policy
[MPLS_IPTUNNEL_MAX
+ 1] = {
31 [MPLS_IPTUNNEL_DST
] = { .type
= NLA_U32
},
34 static unsigned int mpls_encap_size(struct mpls_iptunnel_encap
*en
)
36 /* The size of the layer 2.5 labels to be added for this route */
37 return en
->labels
* sizeof(struct mpls_shim_hdr
);
40 static int mpls_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
42 struct mpls_iptunnel_encap
*tun_encap_info
;
43 struct mpls_shim_hdr
*hdr
;
44 struct net_device
*out_dev
;
46 unsigned int new_header_size
;
48 struct dst_entry
*dst
= skb_dst(skb
);
49 struct rtable
*rt
= NULL
;
50 struct rt6_info
*rt6
= NULL
;
57 if (dst
->ops
->family
== AF_INET
) {
58 ttl
= ip_hdr(skb
)->ttl
;
59 rt
= (struct rtable
*)dst
;
60 } else if (dst
->ops
->family
== AF_INET6
) {
61 ttl
= ipv6_hdr(skb
)->hop_limit
;
62 rt6
= (struct rt6_info
*)dst
;
69 /* Find the output device */
71 if (!mpls_output_possible(out_dev
) ||
72 !dst
->lwtstate
|| skb_warn_if_lro(skb
))
75 skb_forward_csum(skb
);
77 tun_encap_info
= mpls_lwtunnel_encap(dst
->lwtstate
);
79 /* Verify the destination can hold the packet */
80 new_header_size
= mpls_encap_size(tun_encap_info
);
81 mtu
= mpls_dev_mtu(out_dev
);
82 if (mpls_pkt_too_big(skb
, mtu
- new_header_size
))
85 hh_len
= LL_RESERVED_SPACE(out_dev
);
86 if (!out_dev
->header_ops
)
89 /* Ensure there is enough space for the headers in the skb */
90 if (skb_cow(skb
, hh_len
+ new_header_size
))
93 skb_push(skb
, new_header_size
);
94 skb_reset_network_header(skb
);
97 skb
->protocol
= htons(ETH_P_MPLS_UC
);
99 /* Push the new labels */
102 for (i
= tun_encap_info
->labels
- 1; i
>= 0; i
--) {
103 hdr
[i
] = mpls_entry_encode(tun_encap_info
->label
[i
],
109 err
= neigh_xmit(NEIGH_ARP_TABLE
, out_dev
, &rt
->rt_gateway
,
112 err
= neigh_xmit(NEIGH_ND_TABLE
, out_dev
, &rt6
->rt6i_gateway
,
115 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
125 static int mpls_build_state(struct net_device
*dev
, struct nlattr
*nla
,
126 unsigned int family
, const void *cfg
,
127 struct lwtunnel_state
**ts
)
129 struct mpls_iptunnel_encap
*tun_encap_info
;
130 struct nlattr
*tb
[MPLS_IPTUNNEL_MAX
+ 1];
131 struct lwtunnel_state
*newts
;
132 int tun_encap_info_len
;
135 ret
= nla_parse_nested(tb
, MPLS_IPTUNNEL_MAX
, nla
,
136 mpls_iptunnel_policy
);
140 if (!tb
[MPLS_IPTUNNEL_DST
])
143 tun_encap_info_len
= sizeof(*tun_encap_info
);
145 newts
= lwtunnel_state_alloc(tun_encap_info_len
);
149 newts
->len
= tun_encap_info_len
;
150 tun_encap_info
= mpls_lwtunnel_encap(newts
);
151 ret
= nla_get_labels(tb
[MPLS_IPTUNNEL_DST
], MAX_NEW_LABELS
,
152 &tun_encap_info
->labels
, tun_encap_info
->label
);
155 newts
->type
= LWTUNNEL_ENCAP_MPLS
;
156 newts
->flags
|= LWTUNNEL_STATE_OUTPUT_REDIRECT
;
169 static int mpls_fill_encap_info(struct sk_buff
*skb
,
170 struct lwtunnel_state
*lwtstate
)
172 struct mpls_iptunnel_encap
*tun_encap_info
;
174 tun_encap_info
= mpls_lwtunnel_encap(lwtstate
);
176 if (nla_put_labels(skb
, MPLS_IPTUNNEL_DST
, tun_encap_info
->labels
,
177 tun_encap_info
->label
))
178 goto nla_put_failure
;
186 static int mpls_encap_nlsize(struct lwtunnel_state
*lwtstate
)
188 struct mpls_iptunnel_encap
*tun_encap_info
;
190 tun_encap_info
= mpls_lwtunnel_encap(lwtstate
);
192 return nla_total_size(tun_encap_info
->labels
* 4);
195 static int mpls_encap_cmp(struct lwtunnel_state
*a
, struct lwtunnel_state
*b
)
197 struct mpls_iptunnel_encap
*a_hdr
= mpls_lwtunnel_encap(a
);
198 struct mpls_iptunnel_encap
*b_hdr
= mpls_lwtunnel_encap(b
);
201 if (a_hdr
->labels
!= b_hdr
->labels
)
204 for (l
= 0; l
< MAX_NEW_LABELS
; l
++)
205 if (a_hdr
->label
[l
] != b_hdr
->label
[l
])
210 static const struct lwtunnel_encap_ops mpls_iptun_ops
= {
211 .build_state
= mpls_build_state
,
212 .output
= mpls_output
,
213 .fill_encap
= mpls_fill_encap_info
,
214 .get_encap_size
= mpls_encap_nlsize
,
215 .cmp_encap
= mpls_encap_cmp
,
218 static int __init
mpls_iptunnel_init(void)
220 return lwtunnel_encap_add_ops(&mpls_iptun_ops
, LWTUNNEL_ENCAP_MPLS
);
222 module_init(mpls_iptunnel_init
);
224 static void __exit
mpls_iptunnel_exit(void)
226 lwtunnel_encap_del_ops(&mpls_iptun_ops
, LWTUNNEL_ENCAP_MPLS
);
228 module_exit(mpls_iptunnel_exit
);
230 MODULE_DESCRIPTION("MultiProtocol Label Switching IP Tunnels");
231 MODULE_LICENSE("GPL v2");