1 #include <linux/types.h>
2 #include <linux/skbuff.h>
3 #include <linux/socket.h>
5 #include <linux/module.h>
6 #include <linux/if_arp.h>
7 #include <linux/ipv6.h>
8 #include <linux/mpls.h>
13 #include <net/ip_fib.h>
14 #include <net/netevent.h>
15 #include <net/netns/generic.h>
18 #define MAX_NEW_LABELS 2
20 /* This maximum ha length copied from the definition of struct neighbour */
21 #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
23 struct mpls_route { /* next hop label forwarding entry */
24 struct net_device *rt_dev;
25 struct rcu_head rt_rcu;
26 u32 rt_label[MAX_NEW_LABELS];
27 u8 rt_protocol; /* routing protocol that set this entry */
30 unsigned short rt_via_family;
34 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
36 struct mpls_route *rt = NULL;
38 if (index < net->mpls.platform_labels) {
39 struct mpls_route __rcu **platform_label =
40 rcu_dereference(net->mpls.platform_label);
41 rt = rcu_dereference(platform_label[index]);
46 static bool mpls_output_possible(const struct net_device *dev)
48 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
51 static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
53 /* The size of the layer 2.5 labels to be added for this route */
54 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
57 static unsigned int mpls_dev_mtu(const struct net_device *dev)
59 /* The amount of data the layer 2 frame can hold */
63 static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
68 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
74 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
75 struct mpls_entry_decoded dec)
77 /* RFC4385 and RFC5586 encode other packets in mpls such that
78 * they don't conflict with the ip version number, making
79 * decoding by examining the ip version correct in everything
80 * except for the strangest cases.
82 * The strange cases if we choose to support them will require
83 * manual configuration.
85 struct iphdr *hdr4 = ip_hdr(skb);
88 if (hdr4->version == 4) {
89 skb->protocol = htons(ETH_P_IP);
90 csum_replace2(&hdr4->check,
91 htons(hdr4->ttl << 8),
95 else if (hdr4->version == 6) {
96 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
97 skb->protocol = htons(ETH_P_IPV6);
98 hdr6->hop_limit = dec.ttl;
101 /* version 0 and version 1 are used by pseudo wires */
106 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
107 struct packet_type *pt, struct net_device *orig_dev)
109 struct net *net = dev_net(dev);
110 struct mpls_shim_hdr *hdr;
111 struct mpls_route *rt;
112 struct mpls_entry_decoded dec;
113 struct net_device *out_dev;
115 unsigned int new_header_size;
119 /* Careful this entire function runs inside of an rcu critical section */
121 if (skb->pkt_type != PACKET_HOST)
124 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
127 if (!pskb_may_pull(skb, sizeof(*hdr)))
130 /* Read and decode the label */
132 dec = mpls_entry_decode(hdr);
135 skb_pull(skb, sizeof(*hdr));
136 skb_reset_network_header(skb);
140 rt = mpls_route_input_rcu(net, dec.label);
144 /* Find the output device */
145 out_dev = rt->rt_dev;
146 if (!mpls_output_possible(out_dev))
149 if (skb_warn_if_lro(skb))
152 skb_forward_csum(skb);
154 /* Verify ttl is valid */
159 /* Verify the destination can hold the packet */
160 new_header_size = mpls_rt_header_size(rt);
161 mtu = mpls_dev_mtu(out_dev);
162 if (mpls_pkt_too_big(skb, mtu - new_header_size))
165 hh_len = LL_RESERVED_SPACE(out_dev);
166 if (!out_dev->header_ops)
169 /* Ensure there is enough space for the headers in the skb */
170 if (skb_cow(skb, hh_len + new_header_size))
174 skb->protocol = htons(ETH_P_MPLS_UC);
176 if (unlikely(!new_header_size && dec.bos)) {
177 /* Penultimate hop popping */
178 if (!mpls_egress(rt, skb, dec))
183 skb_push(skb, new_header_size);
184 skb_reset_network_header(skb);
185 /* Push the new labels */
188 for (i = rt->rt_labels - 1; i >= 0; i--) {
189 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
194 err = neigh_xmit(rt->rt_via_family, out_dev, rt->rt_via, skb);
196 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
205 static struct packet_type mpls_packet_type __read_mostly = {
206 .type = cpu_to_be16(ETH_P_MPLS_UC),
207 .func = mpls_forward,
210 static struct mpls_route *mpls_rt_alloc(size_t alen)
212 struct mpls_route *rt;
214 rt = kzalloc(GFP_KERNEL, sizeof(*rt) + alen);
216 rt->rt_via_alen = alen;
220 static void mpls_rt_free(struct mpls_route *rt)
223 kfree_rcu(rt, rt_rcu);
226 static void mpls_route_update(struct net *net, unsigned index,
227 struct net_device *dev, struct mpls_route *new,
228 const struct nl_info *info)
230 struct mpls_route *rt, *old = NULL;
234 rt = net->mpls.platform_label[index];
235 if (!dev || (rt && (rt->rt_dev == dev))) {
236 rcu_assign_pointer(net->mpls.platform_label[index], new);
240 /* If we removed a route free it now */
244 static void mpls_ifdown(struct net_device *dev)
246 struct net *net = dev_net(dev);
249 for (index = 0; index < net->mpls.platform_labels; index++) {
250 struct mpls_route *rt = net->mpls.platform_label[index];
253 if (rt->rt_dev != dev)
259 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
262 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
265 case NETDEV_UNREGISTER:
272 static struct notifier_block mpls_dev_notifier = {
273 .notifier_call = mpls_dev_notify,
276 static int mpls_net_init(struct net *net)
278 net->mpls.platform_labels = 0;
279 net->mpls.platform_label = NULL;
284 static void mpls_net_exit(struct net *net)
288 /* An rcu grace period haselapsed since there was a device in
289 * the network namespace (and thus the last in fqlight packet)
290 * left this network namespace. This is because
291 * unregister_netdevice_many and netdev_run_todo has completed
292 * for each network device that was in this network namespace.
294 * As such no additional rcu synchronization is necessary when
295 * freeing the platform_label table.
298 for (index = 0; index < net->mpls.platform_labels; index++) {
299 struct mpls_route *rt = net->mpls.platform_label[index];
300 rcu_assign_pointer(net->mpls.platform_label[index], NULL);
305 kvfree(net->mpls.platform_label);
308 static struct pernet_operations mpls_net_ops = {
309 .init = mpls_net_init,
310 .exit = mpls_net_exit,
313 static int __init mpls_init(void)
317 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
319 err = register_pernet_subsys(&mpls_net_ops);
323 err = register_netdevice_notifier(&mpls_dev_notifier);
325 goto out_unregister_pernet;
327 dev_add_pack(&mpls_packet_type);
333 out_unregister_pernet:
334 unregister_pernet_subsys(&mpls_net_ops);
337 module_init(mpls_init);
339 static void __exit mpls_exit(void)
341 dev_remove_pack(&mpls_packet_type);
342 unregister_netdevice_notifier(&mpls_dev_notifier);
343 unregister_pernet_subsys(&mpls_net_ops);
345 module_exit(mpls_exit);
347 MODULE_DESCRIPTION("MultiProtocol Label Switching");
348 MODULE_LICENSE("GPL v2");
349 MODULE_ALIAS_NETPROTO(PF_MPLS);