c9686f1f01f88c0f067bb53e16f17c3e8735bacc
[openwrt/openwrt.git] / target / linux / generic / pending-3.18 / 666-Add-support-for-MAP-E-FMRs-mesh-mode.patch
1 From 775d6fe74d1eaec2ba387535b068dde2dc89de9e Mon Sep 17 00:00:00 2001
2 From: Steven Barth <steven@midlink.org>
3 Date: Thu, 22 May 2014 09:49:05 +0200
4 Subject: [PATCH] Add support for MAP-E FMRs (mesh mode)
5
6 MAP-E FMRs (draft-ietf-softwire-map-10) are rules for IPv4-communication
7 between MAP CEs (mesh mode) without the need to forward such data to a
8 border relay. This is similar to how 6rd works but for IPv4 over IPv6.
9
10 Signed-off-by: Steven Barth <cyrus@openwrt.org>
11 ---
12 include/net/ip6_tunnel.h | 13 ++
13 include/uapi/linux/if_tunnel.h | 13 ++
14 net/ipv6/ip6_tunnel.c | 276 +++++++++++++++++++++++++++++++++++++++--
15 3 files changed, 291 insertions(+), 11 deletions(-)
16
17 --- a/include/net/ip6_tunnel.h
18 +++ b/include/net/ip6_tunnel.h
19 @@ -15,6 +15,18 @@
20 /* determine capability on a per-packet basis */
21 #define IP6_TNL_F_CAP_PER_PACKET 0x40000
22
23 +/* IPv6 tunnel FMR */
24 +struct __ip6_tnl_fmr {
25 + struct __ip6_tnl_fmr *next; /* next fmr in list */
26 + struct in6_addr ip6_prefix;
27 + struct in_addr ip4_prefix;
28 +
29 + __u8 ip6_prefix_len;
30 + __u8 ip4_prefix_len;
31 + __u8 ea_len;
32 + __u8 offset;
33 +};
34 +
35 struct __ip6_tnl_parm {
36 char name[IFNAMSIZ]; /* name of tunnel device */
37 int link; /* ifindex of underlying L2 interface */
38 @@ -25,6 +37,7 @@ struct __ip6_tnl_parm {
39 __u32 flags; /* tunnel flags */
40 struct in6_addr laddr; /* local tunnel end-point address */
41 struct in6_addr raddr; /* remote tunnel end-point address */
42 + struct __ip6_tnl_fmr *fmrs; /* FMRs */
43
44 __be16 i_flags;
45 __be16 o_flags;
46 --- a/include/uapi/linux/if_tunnel.h
47 +++ b/include/uapi/linux/if_tunnel.h
48 @@ -57,10 +57,23 @@ enum {
49 IFLA_IPTUN_ENCAP_FLAGS,
50 IFLA_IPTUN_ENCAP_SPORT,
51 IFLA_IPTUN_ENCAP_DPORT,
52 + IFLA_IPTUN_FMRS,
53 __IFLA_IPTUN_MAX,
54 };
55 #define IFLA_IPTUN_MAX (__IFLA_IPTUN_MAX - 1)
56
57 +enum {
58 + IFLA_IPTUN_FMR_UNSPEC,
59 + IFLA_IPTUN_FMR_IP6_PREFIX,
60 + IFLA_IPTUN_FMR_IP4_PREFIX,
61 + IFLA_IPTUN_FMR_IP6_PREFIX_LEN,
62 + IFLA_IPTUN_FMR_IP4_PREFIX_LEN,
63 + IFLA_IPTUN_FMR_EA_LEN,
64 + IFLA_IPTUN_FMR_OFFSET,
65 + __IFLA_IPTUN_FMR_MAX,
66 +};
67 +#define IFLA_IPTUN_FMR_MAX (__IFLA_IPTUN_FMR_MAX - 1)
68 +
69 enum tunnel_encap_types {
70 TUNNEL_ENCAP_NONE,
71 TUNNEL_ENCAP_FOU,
72 --- a/net/ipv6/ip6_tunnel.c
73 +++ b/net/ipv6/ip6_tunnel.c
74 @@ -16,6 +16,8 @@
75 * as published by the Free Software Foundation; either version
76 * 2 of the License, or (at your option) any later version.
77 *
78 + * Changes:
79 + * Steven Barth <cyrus@openwrt.org>: MAP-E FMR support
80 */
81
82 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
83 @@ -77,11 +79,9 @@ static bool log_ecn_error = true;
84 module_param(log_ecn_error, bool, 0644);
85 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
86
87 -static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
88 +static u32 HASH(const struct in6_addr *addr)
89 {
90 - u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
91 -
92 - return hash_32(hash, HASH_SIZE_SHIFT);
93 + return hash_32(ipv6_addr_hash(addr), HASH_SIZE_SHIFT);
94 }
95
96 static int ip6_tnl_dev_init(struct net_device *dev);
97 @@ -180,15 +180,24 @@ EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
98 static struct ip6_tnl *
99 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
100 {
101 - unsigned int hash = HASH(remote, local);
102 + unsigned int hash = HASH(local);
103 struct ip6_tnl *t;
104 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
105 + struct __ip6_tnl_fmr *fmr;
106
107 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
108 - if (ipv6_addr_equal(local, &t->parms.laddr) &&
109 - ipv6_addr_equal(remote, &t->parms.raddr) &&
110 - (t->dev->flags & IFF_UP))
111 + if (!ipv6_addr_equal(local, &t->parms.laddr) ||
112 + !(t->dev->flags & IFF_UP))
113 + continue;
114 +
115 + if (ipv6_addr_equal(remote, &t->parms.raddr))
116 return t;
117 +
118 + for (fmr = t->parms.fmrs; fmr; fmr = fmr->next) {
119 + if (ipv6_prefix_equal(remote, &fmr->ip6_prefix,
120 + fmr->ip6_prefix_len))
121 + return t;
122 + }
123 }
124 t = rcu_dereference(ip6n->tnls_wc[0]);
125 if (t && (t->dev->flags & IFF_UP))
126 @@ -218,7 +227,7 @@ ip6_tnl_bucket(struct ip6_tnl_net *ip6n,
127
128 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
129 prio = 1;
130 - h = HASH(remote, local);
131 + h = HASH(local);
132 }
133 return &ip6n->tnls[prio][h];
134 }
135 @@ -391,6 +400,12 @@ ip6_tnl_dev_uninit(struct net_device *de
136 struct net *net = t->net;
137 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
138
139 + while (t->parms.fmrs) {
140 + struct __ip6_tnl_fmr *next = t->parms.fmrs->next;
141 + kfree(t->parms.fmrs);
142 + t->parms.fmrs = next;
143 + }
144 +
145 if (dev == ip6n->fb_tnl_dev)
146 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
147 else
148 @@ -784,6 +799,108 @@ int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
149 }
150 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
151
152 +
153 +/**
154 + * ip4ip6_fmr_calc - calculate target / source IPv6-address based on FMR
155 + * @dest: destination IPv6 address buffer
156 + * @skb: received socket buffer
157 + * @fmr: MAP FMR
158 + * @xmit: Calculate for xmit or rcv
159 + **/
160 +static void ip4ip6_fmr_calc(struct in6_addr *dest,
161 + const struct iphdr *iph, const uint8_t *end,
162 + const struct __ip6_tnl_fmr *fmr, bool xmit)
163 +{
164 + int psidlen = fmr->ea_len - (32 - fmr->ip4_prefix_len);
165 + u8 *portp = NULL;
166 + bool use_dest_addr;
167 + const struct iphdr *dsth = iph;
168 +
169 + if ((u8*)dsth >= end)
170 + return;
171 +
172 + /* find significant IP header */
173 + if (iph->protocol == IPPROTO_ICMP) {
174 + struct icmphdr *ih = (struct icmphdr*)(((u8*)dsth) + dsth->ihl * 4);
175 + if (ih && ((u8*)&ih[1]) <= end && (
176 + ih->type == ICMP_DEST_UNREACH ||
177 + ih->type == ICMP_SOURCE_QUENCH ||
178 + ih->type == ICMP_TIME_EXCEEDED ||
179 + ih->type == ICMP_PARAMETERPROB ||
180 + ih->type == ICMP_REDIRECT))
181 + dsth = (const struct iphdr*)&ih[1];
182 + }
183 +
184 + /* in xmit-path use dest port by default and source port only if
185 + this is an ICMP reply to something else; vice versa in rcv-path */
186 + use_dest_addr = (xmit && dsth == iph) || (!xmit && dsth != iph);
187 +
188 + /* get dst port */
189 + if (((u8*)&dsth[1]) <= end && (
190 + dsth->protocol == IPPROTO_UDP ||
191 + dsth->protocol == IPPROTO_TCP ||
192 + dsth->protocol == IPPROTO_SCTP ||
193 + dsth->protocol == IPPROTO_DCCP)) {
194 + /* for UDP, TCP, SCTP and DCCP source and dest port
195 + follow IPv4 header directly */
196 + portp = ((u8*)dsth) + dsth->ihl * 4;
197 +
198 + if (use_dest_addr)
199 + portp += sizeof(u16);
200 + } else if (iph->protocol == IPPROTO_ICMP) {
201 + struct icmphdr *ih = (struct icmphdr*)(((u8*)dsth) + dsth->ihl * 4);
202 +
203 + /* use icmp identifier as port */
204 + if (((u8*)&ih) <= end && (
205 + (use_dest_addr && (
206 + ih->type == ICMP_ECHOREPLY ||
207 + ih->type == ICMP_TIMESTAMPREPLY ||
208 + ih->type == ICMP_INFO_REPLY ||
209 + ih->type == ICMP_ADDRESSREPLY)) ||
210 + (!use_dest_addr && (
211 + ih->type == ICMP_ECHO ||
212 + ih->type == ICMP_TIMESTAMP ||
213 + ih->type == ICMP_INFO_REQUEST ||
214 + ih->type == ICMP_ADDRESS)
215 + )))
216 + portp = (u8*)&ih->un.echo.id;
217 + }
218 +
219 + if ((portp && &portp[2] <= end) || psidlen == 0) {
220 + int frombyte = fmr->ip6_prefix_len / 8;
221 + int fromrem = fmr->ip6_prefix_len % 8;
222 + int bytes = sizeof(struct in6_addr) - frombyte;
223 + const u32 *addr = (use_dest_addr) ? &iph->daddr : &iph->saddr;
224 + u64 eabits = ((u64)ntohl(*addr)) << (32 + fmr->ip4_prefix_len);
225 + u64 t = 0;
226 +
227 + /* extract PSID from port and add it to eabits */
228 + u16 psidbits = 0;
229 + if (psidlen > 0) {
230 + psidbits = ((u16)portp[0]) << 8 | ((u16)portp[1]);
231 + psidbits >>= 16 - psidlen - fmr->offset;
232 + psidbits = (u16)(psidbits << (16 - psidlen));
233 + eabits |= ((u64)psidbits) << (48 - (fmr->ea_len - psidlen));
234 + }
235 +
236 + /* rewrite destination address */
237 + *dest = fmr->ip6_prefix;
238 + memcpy(&dest->s6_addr[10], addr, sizeof(*addr));
239 + dest->s6_addr16[7] = htons(psidbits >> (16 - psidlen));
240 +
241 + if (bytes > sizeof(u64))
242 + bytes = sizeof(u64);
243 +
244 + /* insert eabits */
245 + memcpy(&t, &dest->s6_addr[frombyte], bytes);
246 + t = be64_to_cpu(t) & ~(((((u64)1) << fmr->ea_len) - 1)
247 + << (64 - fmr->ea_len - fromrem));
248 + t = cpu_to_be64(t | (eabits >> fromrem));
249 + memcpy(&dest->s6_addr[frombyte], &t, bytes);
250 + }
251 +}
252 +
253 +
254 /**
255 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
256 * @skb: received socket buffer
257 @@ -828,6 +945,26 @@ static int ip6_tnl_rcv(struct sk_buff *s
258 skb_reset_network_header(skb);
259 skb->protocol = htons(protocol);
260 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
261 + if (protocol == ETH_P_IP && t->parms.fmrs &&
262 + !ipv6_addr_equal(&ipv6h->saddr, &t->parms.raddr)) {
263 + /* Packet didn't come from BR, so lookup FMR */
264 + struct __ip6_tnl_fmr *fmr;
265 + struct in6_addr expected = t->parms.raddr;
266 + for (fmr = t->parms.fmrs; fmr; fmr = fmr->next)
267 + if (ipv6_prefix_equal(&ipv6h->saddr,
268 + &fmr->ip6_prefix, fmr->ip6_prefix_len))
269 + break;
270 +
271 + /* Check that IPv6 matches IPv4 source to prevent spoofing */
272 + if (fmr)
273 + ip4ip6_fmr_calc(&expected, ip_hdr(skb),
274 + skb_tail_pointer(skb), fmr, false);
275 +
276 + if (!ipv6_addr_equal(&ipv6h->saddr, &expected)) {
277 + rcu_read_unlock();
278 + goto discard;
279 + }
280 + }
281
282 __skb_tunnel_rx(skb, t->dev, t->net);
283
284 @@ -1089,6 +1226,7 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, str
285 __u8 dsfield;
286 __u32 mtu;
287 int err;
288 + struct __ip6_tnl_fmr *fmr;
289
290 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
291 !ip6_tnl_xmit_ctl(t))
292 @@ -1108,6 +1246,18 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, str
293 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
294 fl6.flowi6_mark = skb->mark;
295
296 + /* try to find matching FMR */
297 + for (fmr = t->parms.fmrs; fmr; fmr = fmr->next) {
298 + unsigned mshift = 32 - fmr->ip4_prefix_len;
299 + if (ntohl(fmr->ip4_prefix.s_addr) >> mshift ==
300 + ntohl(iph->daddr) >> mshift)
301 + break;
302 + }
303 +
304 + /* change dstaddr according to FMR */
305 + if (fmr)
306 + ip4ip6_fmr_calc(&fl6.daddr, iph, skb_tail_pointer(skb), fmr, true);
307 +
308 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
309 if (err != 0) {
310 /* XXX: send ICMP error even if DF is not set. */
311 @@ -1276,6 +1426,14 @@ ip6_tnl_change(struct ip6_tnl *t, const
312 t->parms.flowinfo = p->flowinfo;
313 t->parms.link = p->link;
314 t->parms.proto = p->proto;
315 +
316 + while (t->parms.fmrs) {
317 + struct __ip6_tnl_fmr *next = t->parms.fmrs->next;
318 + kfree(t->parms.fmrs);
319 + t->parms.fmrs = next;
320 + }
321 + t->parms.fmrs = p->fmrs;
322 +
323 ip6_tnl_dst_reset(t);
324 ip6_tnl_link_config(t);
325 return 0;
326 @@ -1306,6 +1464,7 @@ ip6_tnl_parm_from_user(struct __ip6_tnl_
327 p->flowinfo = u->flowinfo;
328 p->link = u->link;
329 p->proto = u->proto;
330 + p->fmrs = NULL;
331 memcpy(p->name, u->name, sizeof(u->name));
332 }
333
334 @@ -1581,6 +1740,15 @@ static int ip6_tnl_validate(struct nlatt
335 return 0;
336 }
337
338 +static const struct nla_policy ip6_tnl_fmr_policy[IFLA_IPTUN_FMR_MAX + 1] = {
339 + [IFLA_IPTUN_FMR_IP6_PREFIX] = { .len = sizeof(struct in6_addr) },
340 + [IFLA_IPTUN_FMR_IP4_PREFIX] = { .len = sizeof(struct in_addr) },
341 + [IFLA_IPTUN_FMR_IP6_PREFIX_LEN] = { .type = NLA_U8 },
342 + [IFLA_IPTUN_FMR_IP4_PREFIX_LEN] = { .type = NLA_U8 },
343 + [IFLA_IPTUN_FMR_EA_LEN] = { .type = NLA_U8 },
344 + [IFLA_IPTUN_FMR_OFFSET] = { .type = NLA_U8 }
345 +};
346 +
347 static void ip6_tnl_netlink_parms(struct nlattr *data[],
348 struct __ip6_tnl_parm *parms)
349 {
350 @@ -1614,6 +1782,46 @@ static void ip6_tnl_netlink_parms(struct
351
352 if (data[IFLA_IPTUN_PROTO])
353 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
354 +
355 + if (data[IFLA_IPTUN_FMRS]) {
356 + unsigned rem;
357 + struct nlattr *fmr;
358 + nla_for_each_nested(fmr, data[IFLA_IPTUN_FMRS], rem) {
359 + struct nlattr *fmrd[IFLA_IPTUN_FMR_MAX + 1], *c;
360 + struct __ip6_tnl_fmr *nfmr;
361 +
362 + nla_parse_nested(fmrd, IFLA_IPTUN_FMR_MAX,
363 + fmr, ip6_tnl_fmr_policy);
364 +
365 + if (!(nfmr = kzalloc(sizeof(*nfmr), GFP_KERNEL)))
366 + continue;
367 +
368 + nfmr->offset = 6;
369 +
370 + if ((c = fmrd[IFLA_IPTUN_FMR_IP6_PREFIX]))
371 + nla_memcpy(&nfmr->ip6_prefix, fmrd[IFLA_IPTUN_FMR_IP6_PREFIX],
372 + sizeof(nfmr->ip6_prefix));
373 +
374 + if ((c = fmrd[IFLA_IPTUN_FMR_IP4_PREFIX]))
375 + nla_memcpy(&nfmr->ip4_prefix, fmrd[IFLA_IPTUN_FMR_IP4_PREFIX],
376 + sizeof(nfmr->ip4_prefix));
377 +
378 + if ((c = fmrd[IFLA_IPTUN_FMR_IP6_PREFIX_LEN]))
379 + nfmr->ip6_prefix_len = nla_get_u8(c);
380 +
381 + if ((c = fmrd[IFLA_IPTUN_FMR_IP4_PREFIX_LEN]))
382 + nfmr->ip4_prefix_len = nla_get_u8(c);
383 +
384 + if ((c = fmrd[IFLA_IPTUN_FMR_EA_LEN]))
385 + nfmr->ea_len = nla_get_u8(c);
386 +
387 + if ((c = fmrd[IFLA_IPTUN_FMR_OFFSET]))
388 + nfmr->offset = nla_get_u8(c);
389 +
390 + nfmr->next = parms->fmrs;
391 + parms->fmrs = nfmr;
392 + }
393 + }
394 }
395
396 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
397 @@ -1666,6 +1874,12 @@ static void ip6_tnl_dellink(struct net_d
398
399 static size_t ip6_tnl_get_size(const struct net_device *dev)
400 {
401 + const struct ip6_tnl *t = netdev_priv(dev);
402 + struct __ip6_tnl_fmr *c;
403 + int fmrs = 0;
404 + for (c = t->parms.fmrs; c; c = c->next)
405 + ++fmrs;
406 +
407 return
408 /* IFLA_IPTUN_LINK */
409 nla_total_size(4) +
410 @@ -1683,6 +1897,24 @@ static size_t ip6_tnl_get_size(const str
411 nla_total_size(4) +
412 /* IFLA_IPTUN_PROTO */
413 nla_total_size(1) +
414 + /* IFLA_IPTUN_FMRS */
415 + nla_total_size(0) +
416 + (
417 + /* nest */
418 + nla_total_size(0) +
419 + /* IFLA_IPTUN_FMR_IP6_PREFIX */
420 + nla_total_size(sizeof(struct in6_addr)) +
421 + /* IFLA_IPTUN_FMR_IP4_PREFIX */
422 + nla_total_size(sizeof(struct in_addr)) +
423 + /* IFLA_IPTUN_FMR_EA_LEN */
424 + nla_total_size(1) +
425 + /* IFLA_IPTUN_FMR_IP6_PREFIX_LEN */
426 + nla_total_size(1) +
427 + /* IFLA_IPTUN_FMR_IP4_PREFIX_LEN */
428 + nla_total_size(1) +
429 + /* IFLA_IPTUN_FMR_OFFSET */
430 + nla_total_size(1)
431 + ) * fmrs +
432 0;
433 }
434
435 @@ -1690,6 +1922,9 @@ static int ip6_tnl_fill_info(struct sk_b
436 {
437 struct ip6_tnl *tunnel = netdev_priv(dev);
438 struct __ip6_tnl_parm *parm = &tunnel->parms;
439 + struct __ip6_tnl_fmr *c;
440 + int fmrcnt = 0;
441 + struct nlattr *fmrs;
442
443 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
444 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
445 @@ -1700,8 +1935,27 @@ static int ip6_tnl_fill_info(struct sk_b
446 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
447 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
448 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
449 - nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
450 + nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
451 + !(fmrs = nla_nest_start(skb, IFLA_IPTUN_FMRS)))
452 goto nla_put_failure;
453 +
454 + for (c = parm->fmrs; c; c = c->next) {
455 + struct nlattr *fmr = nla_nest_start(skb, ++fmrcnt);
456 + if (!fmr ||
457 + nla_put(skb, IFLA_IPTUN_FMR_IP6_PREFIX,
458 + sizeof(c->ip6_prefix), &c->ip6_prefix) ||
459 + nla_put(skb, IFLA_IPTUN_FMR_IP4_PREFIX,
460 + sizeof(c->ip4_prefix), &c->ip4_prefix) ||
461 + nla_put_u8(skb, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, c->ip6_prefix_len) ||
462 + nla_put_u8(skb, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, c->ip4_prefix_len) ||
463 + nla_put_u8(skb, IFLA_IPTUN_FMR_EA_LEN, c->ea_len) ||
464 + nla_put_u8(skb, IFLA_IPTUN_FMR_OFFSET, c->offset))
465 + goto nla_put_failure;
466 +
467 + nla_nest_end(skb, fmr);
468 + }
469 + nla_nest_end(skb, fmrs);
470 +
471 return 0;
472
473 nla_put_failure:
474 @@ -1717,6 +1971,7 @@ static const struct nla_policy ip6_tnl_p
475 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
476 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
477 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
478 + [IFLA_IPTUN_FMRS] = { .type = NLA_NESTED },
479 };
480
481 static struct rtnl_link_ops ip6_link_ops __read_mostly = {