/
vr_proto_ip6.c
419 lines (332 loc) · 10.7 KB
/
vr_proto_ip6.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* vr_proto_ip6.c -- ip6 handler
*
* Copyright (c) 2014, Juniper Networks, Inc.
* All rights reserved
*/
#include <vr_os.h>
#include <vr_types.h>
#include <vr_packet.h>
#include <vr_interface.h>
#include <vr_datapath.h>
#include <vr_ip_mtrie.h>
#include <vr_bridge.h>
struct vr_nexthop *
vr_inet6_ip_lookup(unsigned short vrf, uint8_t *ip6)
{
uint32_t rt_prefix[4];
struct vr_route_req rt;
rt.rtr_req.rtr_vrf_id = vrf;
rt.rtr_req.rtr_prefix = (uint8_t*)&rt_prefix;
memcpy(rt.rtr_req.rtr_prefix, ip6, 16);
rt.rtr_req.rtr_prefix_size = 16;
rt.rtr_req.rtr_prefix_len = IP6_PREFIX_LEN;
rt.rtr_req.rtr_family = AF_INET6;
rt.rtr_req.rtr_marker_size = 0;
rt.rtr_req.rtr_nh_id = 0;
return vr_inet_route_lookup(vrf, &rt);
}
static int
vr_v6_prefix_is_ll(uint8_t prefix[])
{
if ((prefix[0] == 0xFE) && (prefix[1] == 0x80)) {
return true;
}
return false;
}
/* TODO: consolidate all sum calculation routines */
static inline uint16_t
vr_sum(unsigned char *buf, unsigned int length)
{
int num_words;
uint32_t total;
uint16_t *ptr;
total = 0;
ptr = (uint16_t *)buf;
num_words = (length + 1) / 2;
while (num_words--)
total += *ptr++;
while (total & 0xffff0000)
total = (total >> 16) + (total & 0xffff);
return (uint16_t)total;
}
static inline uint16_t
vr_ip6_pseudo_header_sum(struct vr_ip6 *ip6)
{
struct vr_ip6_pseudo ip6_ph;
memcpy(ip6_ph.ip6_src, ip6->ip6_src, VR_IP6_ADDRESS_LEN);
memcpy(ip6_ph.ip6_dst, ip6->ip6_dst, VR_IP6_ADDRESS_LEN);
/*
* XXX: length should be the length of (l4 header + data). But here, we
* use the ip6 payload length, assuming that there are no extension
* headers. This asusmption has to be fixed when we extend ipv6 support.
*/
ip6_ph.ip6_l4_length = ip6->ip6_plen;
ip6_ph.ip6_zero = 0;
ip6_ph.ip6_zero_nh = (ip6->ip6_nxt << 24);
return vr_sum((unsigned char *)&ip6_ph, sizeof(ip6_ph));
}
uint16_t
vr_icmp6_checksum(struct vr_ip6 *ip6, struct vr_icmp *icmph)
{
uint16_t sum[2];
sum[0] = vr_ip6_pseudo_header_sum(ip6);
icmph->icmp_csum = 0;
sum[1] = vr_sum((unsigned char *)icmph, ntohs(ip6->ip6_plen));
return vr_sum((unsigned char *)sum, sizeof(sum));
}
static bool
vr_icmp6_input(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
bool handled = true;
struct vr_icmp *icmph;
icmph = (struct vr_icmp *)pkt_data(pkt);
switch (icmph->icmp_type) {
case VR_ICMP6_TYPE_ROUTER_SOL:
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_L3_PROTOCOLS, NULL);
break;
default:
handled = false;
break;
}
return handled;
}
int
vr_ip6_input(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
struct vr_ip6 *ip6;
unsigned short *t_hdr, sport, dport;
ip6 = (struct vr_ip6 *)pkt_network_header(pkt);
t_hdr = (unsigned short *)((char *)ip6 + sizeof(struct vr_ip6));
if (!pkt_pull(pkt, sizeof(struct vr_ip6))) {
vr_pfree(pkt, VP_DROP_PULL);
return 0;
}
switch (ip6->ip6_nxt) {
case VR_IP_PROTO_ICMP6:
if (vr_icmp6_input(router, pkt, fmd))
return 0;
break;
case VR_IP_PROTO_UDP:
sport = *t_hdr;
dport = *(t_hdr + 1);
if (vif_is_virtual(pkt->vp_if)) {
if ((sport == VR_DHCP6_SPORT) && (dport == VR_DHCP6_DPORT))
return vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_L3_PROTOCOLS, NULL);
}
break;
default:
break;
}
if (!pkt_push(pkt, sizeof(struct vr_ip6))) {
vr_pfree(pkt, VP_DROP_PUSH);
return 0;
}
return vr_forward(router, pkt, fmd);
}
void
vr_neighbor_proxy(struct vr_packet *pkt, struct vr_forwarding_md *fmd,
unsigned char *dmac)
{
struct vr_eth *eth;
struct vr_ip6 *ip6;
struct vr_icmp *icmph;
struct vr_neighbor_option *nopt;
struct vr_interface *vif = pkt->vp_if;
icmph = (struct vr_icmp *)pkt_data(pkt);
nopt = (struct vr_neighbor_option *)((unsigned char *)icmph +
sizeof(*icmph) + VR_IP6_ADDRESS_LEN);
eth = (struct vr_eth *)pkt_push(pkt, sizeof(*ip6) + sizeof(*eth));
if (!eth) {
vr_pfree(pkt, VP_DROP_PUSH);
return;
}
ip6 = (struct vr_ip6 *)((unsigned char *)eth + sizeof(*eth));
/* Update Ethernet headr */
VR_MAC_COPY(eth->eth_dmac, nopt->vno_value);
VR_MAC_COPY(eth->eth_smac, dmac);
eth->eth_proto = htons(VR_ETH_PROTO_IP6);
memcpy(ip6->ip6_dst, ip6->ip6_src, sizeof(ip6->ip6_src));
memcpy(ip6->ip6_src, &icmph->icmp_data, sizeof(ip6->ip6_src));
/* Mimic a different source ip */
ip6->ip6_src[15] = 0xFF;
/* Update ICMP header and options */
icmph->icmp_type = VR_ICMP6_TYPE_NEIGH_AD;
icmph->icmp_eid = htons(0x4000);
/* length in units of 8 octets */
nopt->vno_type = TARGET_LINK_LAYER_ADDRESS_OPTION;
nopt->vno_length = (sizeof(struct vr_neighbor_option) + VR_ETHER_ALEN) / 8;
VR_MAC_COPY(nopt->vno_value, dmac);
icmph->icmp_csum =
~(vr_icmp6_checksum(ip6, icmph));
if (vif->vif_flags & VIF_FLAG_NO_ARP_PROXY) {
vif->vif_tx(vif, pkt, fmd);
} else {
vr_bridge_input(vif->vif_router, pkt, fmd);
}
return;
}
mac_response_t
vm_neighbor_request(struct vr_interface *vif, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned char *dmac)
{
uint32_t rt6_prefix[4], pull_len;
unsigned char mac[VR_ETHER_ALEN];
struct vr_icmp *icmph;
struct vr_route_req rt;
struct vr_neighbor_option *nopt;
icmph = (struct vr_icmp *)pkt_data(pkt);
pull_len = sizeof(*icmph) + VR_IP6_ADDRESS_LEN;
nopt = (struct vr_neighbor_option *)(pkt_data(pkt) + pull_len);
/* We let DAD packets bridged */
if (IS_MAC_ZERO(nopt->vno_value))
return MR_NOT_ME;
memset(&rt, 0, sizeof(rt));
rt.rtr_req.rtr_vrf_id = fmd->fmd_dvrf;
rt.rtr_req.rtr_family = AF_INET6;
rt.rtr_req.rtr_prefix = (uint8_t *)&rt6_prefix;
memcpy(rt.rtr_req.rtr_prefix, icmph->icmp_data, 16);
rt.rtr_req.rtr_prefix_size = 16;
rt.rtr_req.rtr_prefix_len = IP6_PREFIX_LEN;
rt.rtr_req.rtr_mac = mac;
vr_inet_route_lookup(fmd->fmd_dvrf, &rt);
if (rt.rtr_req.rtr_label_flags & VR_RT_ARP_PROXY_FLAG)
return vr_get_proxy_mac(pkt, fmd, &rt, dmac);
return MR_FLOOD;
}
int
vr_neighbor_input(struct vr_packet *pkt, struct vr_forwarding_md *fmd,
unsigned char *eth_dmac)
{
int handled = 1;
uint32_t pull_len, len;
unsigned char dmac[VR_ETHER_ALEN];
mac_response_t ndisc_result;
struct vr_ip6 *ip6;
struct vr_icmp *icmph;
struct vr_packet *pkt_c;
struct vr_neighbor_option *nopt;
struct vr_interface *vif = pkt->vp_if;
pull_len = sizeof(*ip6);
if (pkt->vp_len < pull_len) {
vr_pfree(pkt, VP_DROP_INVALID_PACKET);
return handled;
}
ip6 = (struct vr_ip6 *)pkt_data(pkt);
if (ip6->ip6_nxt != VR_IP_PROTO_ICMP6)
return !handled;
/* Link local neighbour discovery is bridged */
if (vr_v6_prefix_is_ll(ip6->ip6_dst))
return !handled;
if (pkt->vp_len < pull_len + sizeof(struct vr_icmp))
goto drop;
pkt_pull(pkt, pull_len);
icmph = (struct vr_icmp *)pkt_data(pkt);
if (icmph->icmp_type != VR_ICMP6_TYPE_NEIGH_SOL) {
pkt_push(pkt, pull_len);
return !handled;
}
if (pkt->vp_len < (sizeof(struct vr_icmp) + VR_IP6_ADDRESS_LEN +
sizeof(struct vr_neighbor_option)))
goto drop;
len = sizeof(*icmph) + VR_IP6_ADDRESS_LEN;
nopt = (struct vr_neighbor_option *)(pkt_data(pkt) + len);
if (pkt->vp_len < (len + (nopt->vno_length * 8)))
goto drop;
if (nopt->vno_type != SOURCE_LINK_LAYER_ADDRESS_OPTION)
goto drop;
VR_MAC_COPY(dmac, eth_dmac);
ndisc_result = vif->vif_mac_request(vif, pkt, fmd, dmac);
switch (ndisc_result) {
case MR_PROXY:
vr_neighbor_proxy(pkt, fmd, dmac);
break;
case MR_XCONNECT:
vif_xconnect(pkt->vp_if, pkt, fmd);
break;
case MR_TRAP_X:
pkt_c = vr_pclone(pkt);
if (pkt_c)
vif_xconnect(pkt->vp_if, pkt_c, fmd);
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_ARP, NULL);
break;
case MR_MIRROR:
pkt_c = vr_pclone(pkt);
if (pkt_c)
vr_trap(pkt_c, fmd->fmd_dvrf, AGENT_TRAP_ARP, NULL);
handled = false;
break;
case MR_DROP:
vr_pfree(pkt, VP_DROP_INVALID_ARP);
break;
case MR_FLOOD:
default:
handled = false;
break;
}
if (!handled)
pkt_push(pkt, pull_len);
return handled;
drop:
vr_pfree(pkt, VP_DROP_INVALID_PACKET);
return handled;
}
bool
vr_ip6_dhcp_packet(struct vr_packet *pkt)
{
unsigned char *data = pkt_data(pkt);
struct vr_ip6 *ip6;
struct vr_udp *udph = NULL;
if ((pkt->vp_type != VP_TYPE_IP6) ||
(!(pkt->vp_flags & VP_FLAG_MULTICAST)))
return false;
ip6 = (struct vr_ip6 *)data;
if (vr_v6_prefix_is_ll(ip6->ip6_dst))
return false;
/* 0xFF02 is the multicast address used for NDP, DHCPv6 etc */
if (ip6->ip6_dst[0] == 0xFF && ip6->ip6_dst[1] == 0x02) {
/*
* Bridge neighbor solicit for link-local addresses
*/
if (ip6->ip6_nxt == VR_IP_PROTO_UDP)
udph = (struct vr_udp *)((char *)ip6 + sizeof(struct vr_ip6));
if (udph && (udph->udp_sport == htons(VR_DHCP6_SRC_PORT)))
return true;
}
return false;
}
l4_pkt_type_t
vr_ip6_well_known_packet(struct vr_packet *pkt)
{
unsigned char *data = pkt_data(pkt);
struct vr_ip6 *ip6;
struct vr_udp *udph = NULL;
struct vr_icmp *icmph = NULL;
if ((pkt->vp_type != VP_TYPE_IP6) ||
(!(pkt->vp_flags & VP_FLAG_MULTICAST)))
return L4_TYPE_UNKNOWN;
ip6 = (struct vr_ip6 *)data;
if (vr_v6_prefix_is_ll(ip6->ip6_dst))
return L4_TYPE_UNKNOWN;
/* 0xFF02 is the multicast address used for NDP, DHCPv6 etc */
if (ip6->ip6_dst[0] == 0xFF && ip6->ip6_dst[1] == 0x02) {
/*
* Bridge neighbor solicit for link-local addresses
*/
if (ip6->ip6_nxt == VR_IP_PROTO_ICMP6) {
icmph = (struct vr_icmp *)((char *)ip6 + sizeof(struct vr_ip6));
if (icmph && (icmph->icmp_type == VR_ICMP6_TYPE_ROUTER_SOL))
return L4_TYPE_ROUTER_SOLICITATION;
if (icmph && (icmph->icmp_type == VR_ICMP6_TYPE_NEIGH_SOL))
return L4_TYPE_NEIGHBOUR_SOLICITATION;
}
if (ip6->ip6_nxt == VR_IP_PROTO_UDP) {
udph = (struct vr_udp *)((char *)ip6 + sizeof(struct vr_ip6));
if (udph && (udph->udp_sport == htons(VR_DHCP6_SRC_PORT)))
return L4_TYPE_DHCP_REQUEST;
}
}
return L4_TYPE_UNKNOWN;
}