summaryrefslogtreecommitdiff
path: root/lib/route/nh_encap_mpls.c
blob: a7802917caf40a92483388e11bd71e25c8e7c9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* SPDX-License-Identifier: LGPL-2.1-only */

#include "nl-default.h"

#include <linux/mpls_iptunnel.h>
#include <linux/lwtunnel.h>

#include <netlink/route/nexthop.h>

#include "nl-route.h"
#include "nexthop-encap.h"

struct mpls_iptunnel_encap {
	struct nl_addr *dst;
	uint8_t ttl;
};

static void mpls_encap_dump(void *priv, struct nl_dump_params *dp)
{
	struct mpls_iptunnel_encap *encap_info = priv;
	char buf[256];

	nl_dump(dp, "%s ", nl_addr2str(encap_info->dst, buf, sizeof(buf)));

	if (encap_info->ttl)
		nl_dump(dp, "ttl %u ", encap_info->ttl);
}

static int mpls_encap_build_msg(struct nl_msg *msg, void *priv)
{
	struct mpls_iptunnel_encap *encap_info = priv;

	NLA_PUT_ADDR(msg, MPLS_IPTUNNEL_DST, encap_info->dst);
	if (encap_info->ttl)
		NLA_PUT_U8(msg, MPLS_IPTUNNEL_TTL, encap_info->ttl);

	return 0;

nla_put_failure:
	return -NLE_MSGSIZE;
}

static void mpls_encap_destructor(void *priv)
{
	struct mpls_iptunnel_encap *encap_info = priv;

	nl_addr_put(encap_info->dst);
}

static struct nla_policy mpls_encap_policy[MPLS_IPTUNNEL_MAX + 1] = {
	[MPLS_IPTUNNEL_DST]     = { .type = NLA_U32 },
	[MPLS_IPTUNNEL_TTL]     = { .type = NLA_U8 },
};

static int mpls_encap_parse_msg(struct nlattr *nla, struct rtnl_nexthop *nh)
{
	struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
	struct nl_addr *labels;
	uint8_t ttl = 0;
	int err;

	err = nla_parse_nested(tb, MPLS_IPTUNNEL_MAX, nla, mpls_encap_policy);
	if (err < 0)
		return err;

	if (!tb[MPLS_IPTUNNEL_DST])
		return -NLE_INVAL;

	labels = nl_addr_alloc_attr(tb[MPLS_IPTUNNEL_DST], AF_MPLS);
	if (!labels)
		return -NLE_NOMEM;

	if (tb[MPLS_IPTUNNEL_TTL])
		ttl = nla_get_u8(tb[MPLS_IPTUNNEL_TTL]);

	err = rtnl_route_nh_encap_mpls(nh, labels, ttl);

	nl_addr_put(labels);

	return err;
}

static int mpls_encap_compare(void *_a, void *_b)
{
	struct mpls_iptunnel_encap *a = _a;
	struct mpls_iptunnel_encap *b = _b;
	int diff = 0;

	diff |= (a->ttl != b->ttl);
	diff |= nl_addr_cmp(a->dst, b->dst);

	return diff;
}

struct nh_encap_ops mpls_encap_ops = {
	.encap_type	= LWTUNNEL_ENCAP_MPLS,
	.build_msg	= mpls_encap_build_msg,
	.parse_msg	= mpls_encap_parse_msg,
	.compare	= mpls_encap_compare,
	.dump		= mpls_encap_dump,
	.destructor	= mpls_encap_destructor,
};

int rtnl_route_nh_encap_mpls(struct rtnl_nexthop *nh,
			     struct nl_addr *addr,
			     uint8_t ttl)
{
	struct mpls_iptunnel_encap *mpls_encap;
	struct rtnl_nh_encap *rtnh_encap;

	if (!addr)
		return -NLE_INVAL;

	rtnh_encap = calloc(1, sizeof(*rtnh_encap));
	if (!rtnh_encap)
		return -NLE_NOMEM;

	mpls_encap = calloc(1, sizeof(*mpls_encap));
	if (!mpls_encap) {
		free(rtnh_encap);
		return -NLE_NOMEM;
	}

	mpls_encap->dst = nl_addr_get(addr);
	mpls_encap->ttl = ttl;

	rtnh_encap->priv = mpls_encap;
	rtnh_encap->ops = &mpls_encap_ops;

	nh_set_encap(nh, rtnh_encap);

	return 0;
}

struct nl_addr *rtnl_route_nh_get_encap_mpls_dst(struct rtnl_nexthop *nh)
{
	struct mpls_iptunnel_encap *mpls_encap;

	if (!nh->rtnh_encap || nh->rtnh_encap->ops->encap_type != LWTUNNEL_ENCAP_MPLS)
		return NULL;

	mpls_encap = (struct mpls_iptunnel_encap *)nh->rtnh_encap->priv;
	if (!mpls_encap)
		return NULL;

	return mpls_encap->dst;
}

uint8_t rtnl_route_nh_get_encap_mpls_ttl(struct rtnl_nexthop *nh)
{
	struct mpls_iptunnel_encap *mpls_encap;

	if (!nh->rtnh_encap || nh->rtnh_encap->ops->encap_type != LWTUNNEL_ENCAP_MPLS)
		return 0;

	mpls_encap = (struct mpls_iptunnel_encap *)nh->rtnh_encap->priv;
	if (!mpls_encap)
		return 0;

	return mpls_encap->ttl;
}