/src/openvswitch/lib/packets.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | #include "packets.h" |
19 | | #include <sys/types.h> |
20 | | #include <netinet/in.h> |
21 | | #include <arpa/inet.h> |
22 | | #include <sys/socket.h> |
23 | | #include <netinet/ip6.h> |
24 | | #include <netinet/icmp6.h> |
25 | | #include <stdlib.h> |
26 | | #include <netdb.h> |
27 | | #include "byte-order.h" |
28 | | #include "csum.h" |
29 | | #include "crc32c.h" |
30 | | #include "flow.h" |
31 | | #include "openvswitch/hmap.h" |
32 | | #include "openvswitch/dynamic-string.h" |
33 | | #include "ovs-thread.h" |
34 | | #include "odp-util.h" |
35 | | #include "dp-packet.h" |
36 | | #include "dp-packet-gso.h" |
37 | | #include "unaligned.h" |
38 | | |
39 | | const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT; |
40 | | const struct in6_addr in6addr_all_hosts = IN6ADDR_ALL_HOSTS_INIT; |
41 | | const struct in6_addr in6addr_all_routers = IN6ADDR_ALL_ROUTERS_INIT; |
42 | | const struct in6_addr in6addr_v4mapped_any = IN6ADDR_V4MAPPED_ANY_INIT; |
43 | | |
44 | | struct in6_addr |
45 | | flow_tnl_dst(const struct flow_tnl *tnl) |
46 | 0 | { |
47 | 0 | return tnl->ip_dst ? in6_addr_mapped_ipv4(tnl->ip_dst) : tnl->ipv6_dst; |
48 | 0 | } |
49 | | |
50 | | struct in6_addr |
51 | | flow_tnl_src(const struct flow_tnl *tnl) |
52 | 0 | { |
53 | 0 | return tnl->ip_src ? in6_addr_mapped_ipv4(tnl->ip_src) : tnl->ipv6_src; |
54 | 0 | } |
55 | | |
56 | | /* Returns true if 's' consists entirely of hex digits, false otherwise. */ |
57 | | static bool |
58 | | is_all_hex(const char *s) |
59 | 0 | { |
60 | 0 | return s[strspn(s, "0123456789abcdefABCDEF")] == '\0'; |
61 | 0 | } |
62 | | |
63 | | /* Parses 's' as a 16-digit hexadecimal number representing a datapath ID. On |
64 | | * success stores the dpid into '*dpidp' and returns true, on failure stores 0 |
65 | | * into '*dpidp' and returns false. |
66 | | * |
67 | | * Rejects an all-zeros dpid as invalid. */ |
68 | | bool |
69 | | dpid_from_string(const char *s, uint64_t *dpidp) |
70 | 0 | { |
71 | 0 | size_t len = strlen(s); |
72 | 0 | *dpidp = ((len == 16 && is_all_hex(s)) |
73 | 0 | || (len <= 18 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') |
74 | 0 | && is_all_hex(s + 2)) |
75 | 0 | ? strtoull(s, NULL, 16) |
76 | 0 | : 0); |
77 | 0 | return *dpidp != 0; |
78 | 0 | } |
79 | | |
80 | | uint64_t |
81 | | eth_addr_to_uint64(const struct eth_addr ea) |
82 | 520 | { |
83 | 520 | return (((uint64_t) ntohs(ea.be16[0]) << 32) |
84 | 520 | | ((uint64_t) ntohs(ea.be16[1]) << 16) |
85 | 520 | | ntohs(ea.be16[2])); |
86 | 520 | } |
87 | | |
88 | | void |
89 | | eth_addr_from_uint64(uint64_t x, struct eth_addr *ea) |
90 | 0 | { |
91 | 0 | ea->be16[0] = htons(x >> 32); |
92 | 0 | ea->be16[1] = htons((x & 0xFFFF0000) >> 16); |
93 | 0 | ea->be16[2] = htons(x & 0xFFFF); |
94 | 0 | } |
95 | | |
96 | | void |
97 | | eth_addr_mark_random(struct eth_addr *ea) |
98 | 0 | { |
99 | 0 | ea->ea[0] &= ~1; /* Unicast. */ |
100 | 0 | ea->ea[0] |= 2; /* Private. */ |
101 | 0 | } |
102 | | |
103 | | /* Returns true if 'ea' is a reserved address, that a bridge must never |
104 | | * forward, false otherwise. |
105 | | * |
106 | | * If you change this function's behavior, please update corresponding |
107 | | * documentation in vswitch.xml at the same time. */ |
108 | | bool |
109 | | eth_addr_is_reserved(const struct eth_addr ea) |
110 | 0 | { |
111 | 0 | struct eth_addr_node { |
112 | 0 | struct hmap_node hmap_node; |
113 | 0 | const uint64_t ea64; |
114 | 0 | }; |
115 | |
|
116 | 0 | static struct eth_addr_node nodes[] = { |
117 | | /* STP, IEEE pause frames, and other reserved protocols. */ |
118 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000000ULL }, |
119 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000001ULL }, |
120 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000002ULL }, |
121 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000003ULL }, |
122 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000004ULL }, |
123 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000005ULL }, |
124 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000006ULL }, |
125 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000007ULL }, |
126 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000008ULL }, |
127 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000009ULL }, |
128 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000aULL }, |
129 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000bULL }, |
130 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000cULL }, |
131 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000dULL }, |
132 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000eULL }, |
133 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000fULL }, |
134 | | |
135 | | /* Extreme protocols. */ |
136 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000000ULL }, /* EDP. */ |
137 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000004ULL }, /* EAPS. */ |
138 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000006ULL }, /* EAPS. */ |
139 | | |
140 | | /* Cisco protocols. */ |
141 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000c000000ULL }, /* ISL. */ |
142 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccccULL }, /* PAgP, UDLD, CDP, |
143 | | * DTP, VTP. */ |
144 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000ccccccdULL }, /* PVST+. */ |
145 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000ccdcdcdULL }, /* STP Uplink Fast, |
146 | | * FlexLink. */ |
147 | | |
148 | | /* Cisco CFM. */ |
149 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc0ULL }, |
150 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc1ULL }, |
151 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc2ULL }, |
152 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc3ULL }, |
153 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc4ULL }, |
154 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc5ULL }, |
155 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc6ULL }, |
156 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc7ULL }, |
157 | 0 | }; |
158 | |
|
159 | 0 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
160 | 0 | struct eth_addr_node *node; |
161 | 0 | static struct hmap addrs; |
162 | 0 | uint64_t ea64; |
163 | |
|
164 | 0 | if (ovsthread_once_start(&once)) { |
165 | 0 | hmap_init(&addrs); |
166 | 0 | for (node = nodes; node < &nodes[ARRAY_SIZE(nodes)]; node++) { |
167 | 0 | hmap_insert(&addrs, &node->hmap_node, hash_uint64(node->ea64)); |
168 | 0 | } |
169 | 0 | ovsthread_once_done(&once); |
170 | 0 | } |
171 | |
|
172 | 0 | ea64 = eth_addr_to_uint64(ea); |
173 | 0 | HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_uint64(ea64), &addrs) { |
174 | 0 | if (node->ea64 == ea64) { |
175 | 0 | return true; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | return false; |
179 | 0 | } |
180 | | |
181 | | /* Attempts to parse 's' as an Ethernet address. If successful, stores the |
182 | | * address in 'ea' and returns true, otherwise zeros 'ea' and returns |
183 | | * false. This function checks trailing characters. */ |
184 | | bool |
185 | | eth_addr_from_string(const char *s, struct eth_addr *ea) |
186 | 0 | { |
187 | 0 | int n = 0; |
188 | 0 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*ea), &n) |
189 | 0 | && !s[n]) { |
190 | 0 | return true; |
191 | 0 | } else { |
192 | 0 | *ea = eth_addr_zero; |
193 | 0 | return false; |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | /* Fills 'b' with a Reverse ARP packet with Ethernet source address 'eth_src'. |
198 | | * This function is used by Open vSwitch to compose packets in cases where |
199 | | * context is important but content doesn't (or shouldn't) matter. |
200 | | * |
201 | | * The returned packet has enough headroom to insert an 802.1Q VLAN header if |
202 | | * desired. */ |
203 | | void |
204 | | compose_rarp(struct dp_packet *b, const struct eth_addr eth_src) |
205 | 0 | { |
206 | 0 | struct eth_header *eth; |
207 | 0 | struct arp_eth_header *arp; |
208 | |
|
209 | 0 | dp_packet_clear(b); |
210 | 0 | dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN |
211 | 0 | + ARP_ETH_HEADER_LEN); |
212 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
213 | 0 | eth = dp_packet_put_uninit(b, sizeof *eth); |
214 | 0 | eth->eth_dst = eth_addr_broadcast; |
215 | 0 | eth->eth_src = eth_src; |
216 | 0 | eth->eth_type = htons(ETH_TYPE_RARP); |
217 | |
|
218 | 0 | arp = dp_packet_put_uninit(b, sizeof *arp); |
219 | 0 | arp->ar_hrd = htons(ARP_HRD_ETHERNET); |
220 | 0 | arp->ar_pro = htons(ARP_PRO_IP); |
221 | 0 | arp->ar_hln = sizeof arp->ar_sha; |
222 | 0 | arp->ar_pln = sizeof arp->ar_spa; |
223 | 0 | arp->ar_op = htons(ARP_OP_RARP); |
224 | 0 | arp->ar_sha = eth_src; |
225 | 0 | put_16aligned_be32(&arp->ar_spa, htonl(0)); |
226 | 0 | arp->ar_tha = eth_src; |
227 | 0 | put_16aligned_be32(&arp->ar_tpa, htonl(0)); |
228 | |
|
229 | 0 | dp_packet_set_l3(b, arp); |
230 | 0 | b->packet_type = htonl(PT_ETH); |
231 | 0 | } |
232 | | |
233 | | /* Insert VLAN header according to given TCI. Packet passed must be Ethernet |
234 | | * packet. Ignores the CFI bit of 'tci' using 0 instead. |
235 | | * |
236 | | * Also adjusts the layer offsets accordingly. */ |
237 | | void |
238 | | eth_push_vlan(struct dp_packet *packet, ovs_be16 tpid, ovs_be16 tci) |
239 | 0 | { |
240 | 0 | struct vlan_eth_header *veh; |
241 | | |
242 | | /* Insert new 802.1Q header. */ |
243 | 0 | veh = dp_packet_resize_l2(packet, VLAN_HEADER_LEN); |
244 | 0 | memmove(veh, (char *)veh + VLAN_HEADER_LEN, 2 * ETH_ADDR_LEN); |
245 | 0 | veh->veth_type = tpid; |
246 | 0 | veh->veth_tci = tci & htons(~VLAN_CFI); |
247 | 0 | } |
248 | | |
249 | | /* Removes outermost VLAN header (if any is present) from 'packet'. |
250 | | * |
251 | | * 'packet->l2_5' should initially point to 'packet''s outer-most VLAN header |
252 | | * or may be NULL if there are no VLAN headers. */ |
253 | | void |
254 | | eth_pop_vlan(struct dp_packet *packet) |
255 | 0 | { |
256 | 0 | struct vlan_eth_header *veh = dp_packet_eth(packet); |
257 | |
|
258 | 0 | if (veh && dp_packet_size(packet) >= sizeof *veh |
259 | 0 | && eth_type_vlan(veh->veth_type)) { |
260 | |
|
261 | 0 | memmove((char *)veh + VLAN_HEADER_LEN, veh, 2 * ETH_ADDR_LEN); |
262 | 0 | dp_packet_resize_l2(packet, -VLAN_HEADER_LEN); |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | | /* Push Ethernet header onto 'packet' assuming it is layer 3 */ |
267 | | void |
268 | | push_eth(struct dp_packet *packet, const struct eth_addr *dst, |
269 | | const struct eth_addr *src) |
270 | 0 | { |
271 | 0 | struct eth_header *eh; |
272 | |
|
273 | 0 | ovs_assert(!dp_packet_is_eth(packet)); |
274 | 0 | eh = dp_packet_resize_l2(packet, ETH_HEADER_LEN); |
275 | 0 | eh->eth_dst = *dst; |
276 | 0 | eh->eth_src = *src; |
277 | 0 | eh->eth_type = pt_ns_type_be(packet->packet_type); |
278 | 0 | packet->packet_type = htonl(PT_ETH); |
279 | 0 | } |
280 | | |
281 | | /* Removes Ethernet header, including VLAN header, from 'packet'. |
282 | | * |
283 | | * Previous to calling this function, 'ofpbuf_l3(packet)' must not be NULL */ |
284 | | void |
285 | | pop_eth(struct dp_packet *packet) |
286 | 0 | { |
287 | 0 | char *l2_5 = dp_packet_l2_5(packet); |
288 | 0 | char *l3 = dp_packet_l3(packet); |
289 | 0 | ovs_be16 ethertype; |
290 | 0 | int increment; |
291 | |
|
292 | 0 | ovs_assert(dp_packet_is_eth(packet)); |
293 | 0 | ovs_assert(l3 != NULL); |
294 | |
|
295 | 0 | if (l2_5) { |
296 | 0 | increment = packet->l2_5_ofs; |
297 | 0 | ethertype = *(ALIGNED_CAST(ovs_be16 *, (l2_5 - 2))); |
298 | 0 | } else { |
299 | 0 | increment = packet->l3_ofs; |
300 | 0 | ethertype = *(ALIGNED_CAST(ovs_be16 *, (l3 - 2))); |
301 | 0 | } |
302 | |
|
303 | 0 | dp_packet_resize_l2(packet, -increment); |
304 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, ntohs(ethertype)); |
305 | 0 | } |
306 | | |
307 | | /* Set ethertype of the packet. */ |
308 | | static void |
309 | | set_ethertype(struct dp_packet *packet, ovs_be16 eth_type) |
310 | 0 | { |
311 | 0 | struct eth_header *eh = dp_packet_eth(packet); |
312 | |
|
313 | 0 | if (!eh) { |
314 | 0 | return; |
315 | 0 | } |
316 | | |
317 | 0 | if (eth_type_vlan(eh->eth_type)) { |
318 | 0 | ovs_be16 *p; |
319 | 0 | char *l2_5 = dp_packet_l2_5(packet); |
320 | |
|
321 | 0 | p = ALIGNED_CAST(ovs_be16 *, |
322 | 0 | (l2_5 ? l2_5 : (char *)dp_packet_l3(packet)) - 2); |
323 | 0 | *p = eth_type; |
324 | 0 | } else { |
325 | 0 | eh->eth_type = eth_type; |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | static bool is_mpls(struct dp_packet *packet) |
330 | 0 | { |
331 | 0 | return packet->l2_5_ofs != UINT16_MAX; |
332 | 0 | } |
333 | | |
334 | | /* Set time to live (TTL) of an MPLS label stack entry (LSE). */ |
335 | | void |
336 | | set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl) |
337 | 866 | { |
338 | 866 | *lse &= ~htonl(MPLS_TTL_MASK); |
339 | 866 | *lse |= htonl((ttl << MPLS_TTL_SHIFT) & MPLS_TTL_MASK); |
340 | 866 | } |
341 | | |
342 | | /* Set traffic class (TC) of an MPLS label stack entry (LSE). */ |
343 | | void |
344 | | set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc) |
345 | 1.03k | { |
346 | 1.03k | *lse &= ~htonl(MPLS_TC_MASK); |
347 | 1.03k | *lse |= htonl((tc << MPLS_TC_SHIFT) & MPLS_TC_MASK); |
348 | 1.03k | } |
349 | | |
350 | | /* Set label of an MPLS label stack entry (LSE). */ |
351 | | void |
352 | | set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label) |
353 | 2.36k | { |
354 | 2.36k | *lse &= ~htonl(MPLS_LABEL_MASK); |
355 | 2.36k | *lse |= htonl((ntohl(label) << MPLS_LABEL_SHIFT) & MPLS_LABEL_MASK); |
356 | 2.36k | } |
357 | | |
358 | | /* Set bottom of stack (BoS) bit of an MPLS label stack entry (LSE). */ |
359 | | void |
360 | | set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos) |
361 | 2.32k | { |
362 | 2.32k | *lse &= ~htonl(MPLS_BOS_MASK); |
363 | 2.32k | *lse |= htonl((bos << MPLS_BOS_SHIFT) & MPLS_BOS_MASK); |
364 | 2.32k | } |
365 | | |
366 | | /* Compose an MPLS label stack entry (LSE) from its components: |
367 | | * label, traffic class (TC), time to live (TTL) and |
368 | | * bottom of stack (BoS) bit. */ |
369 | | ovs_be32 |
370 | | set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos, ovs_be32 label) |
371 | 0 | { |
372 | 0 | ovs_be32 lse = htonl(0); |
373 | 0 | set_mpls_lse_ttl(&lse, ttl); |
374 | 0 | set_mpls_lse_tc(&lse, tc); |
375 | 0 | set_mpls_lse_bos(&lse, bos); |
376 | 0 | set_mpls_lse_label(&lse, label); |
377 | 0 | return lse; |
378 | 0 | } |
379 | | |
380 | | /* Set MPLS label stack entry to outermost MPLS header.*/ |
381 | | void |
382 | | set_mpls_lse(struct dp_packet *packet, ovs_be32 mpls_lse) |
383 | 0 | { |
384 | | /* Packet type should be MPLS to set label stack entry. */ |
385 | 0 | if (is_mpls(packet)) { |
386 | 0 | struct mpls_hdr *mh = dp_packet_l2_5(packet); |
387 | | |
388 | | /* Update mpls label stack entry. */ |
389 | 0 | put_16aligned_be32(&mh->mpls_lse, mpls_lse); |
390 | 0 | } |
391 | 0 | } |
392 | | |
393 | | /* Push MPLS label stack entry 'lse' onto 'packet' as the outermost MPLS |
394 | | * header. If 'packet' does not already have any MPLS labels, then its |
395 | | * Ethertype is changed to 'ethtype' (which must be an MPLS Ethertype). */ |
396 | | void |
397 | | push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse) |
398 | 0 | { |
399 | 0 | char * header; |
400 | 0 | size_t len; |
401 | |
|
402 | 0 | if (!eth_type_mpls(ethtype)) { |
403 | 0 | return; |
404 | 0 | } |
405 | | |
406 | 0 | if (!is_mpls(packet)) { |
407 | | /* Set MPLS label stack offset. */ |
408 | 0 | packet->l2_5_ofs = packet->l3_ofs; |
409 | 0 | } |
410 | |
|
411 | 0 | set_ethertype(packet, ethtype); |
412 | | |
413 | | /* Push new MPLS shim header onto packet. */ |
414 | 0 | len = packet->l2_5_ofs; |
415 | 0 | header = dp_packet_resize_l2_5(packet, MPLS_HLEN); |
416 | 0 | memmove(header, header + MPLS_HLEN, len); |
417 | 0 | memcpy(header + len, &lse, sizeof lse); |
418 | |
|
419 | 0 | pkt_metadata_init_conn(&packet->md); |
420 | 0 | } |
421 | | |
422 | | void |
423 | | add_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse, |
424 | | bool l3_encap) |
425 | 0 | { |
426 | 0 | if (!eth_type_mpls(ethtype)) { |
427 | 0 | return; |
428 | 0 | } |
429 | | |
430 | 0 | if (!l3_encap) { |
431 | 0 | struct mpls_hdr *header = dp_packet_resize_l2(packet, MPLS_HLEN); |
432 | |
|
433 | 0 | put_16aligned_be32(&header->mpls_lse, lse); |
434 | 0 | packet->l2_5_ofs = 0; |
435 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, |
436 | 0 | ntohs(ethtype)); |
437 | 0 | } else { |
438 | 0 | size_t len; |
439 | 0 | char *header; |
440 | |
|
441 | 0 | if (!is_mpls(packet)) { |
442 | | /* Set MPLS label stack offset. */ |
443 | 0 | packet->l2_5_ofs = packet->l3_ofs; |
444 | 0 | } |
445 | 0 | set_ethertype(packet, ethtype); |
446 | | |
447 | | /* Push new MPLS shim header onto packet. */ |
448 | 0 | len = packet->l2_5_ofs; |
449 | 0 | header = dp_packet_resize_l2_5(packet, MPLS_HLEN); |
450 | 0 | memmove(header, header + MPLS_HLEN, len); |
451 | 0 | memcpy(header + len, &lse, sizeof lse); |
452 | 0 | } |
453 | 0 | pkt_metadata_init_conn(&packet->md); |
454 | 0 | } |
455 | | |
456 | | /* If 'packet' is an MPLS packet, removes its outermost MPLS label stack entry. |
457 | | * If the label that was removed was the only MPLS label, changes 'packet''s |
458 | | * Ethertype to 'ethtype' (which ordinarily should not be an MPLS |
459 | | * Ethertype). */ |
460 | | void |
461 | | pop_mpls(struct dp_packet *packet, ovs_be16 ethtype) |
462 | 0 | { |
463 | 0 | if (is_mpls(packet)) { |
464 | 0 | struct mpls_hdr *mh = dp_packet_l2_5(packet); |
465 | 0 | size_t len = packet->l2_5_ofs; |
466 | |
|
467 | 0 | set_ethertype(packet, ethtype); |
468 | 0 | if (get_16aligned_be32(&mh->mpls_lse) & htonl(MPLS_BOS_MASK)) { |
469 | 0 | dp_packet_set_l2_5(packet, NULL); |
470 | 0 | } |
471 | | /* Shift the l2 header forward. */ |
472 | 0 | memmove((char*)dp_packet_data(packet) + MPLS_HLEN, dp_packet_data(packet), len); |
473 | 0 | dp_packet_resize_l2_5(packet, -MPLS_HLEN); |
474 | | |
475 | | /* Invalidate offload flags as they are not valid after |
476 | | * decapsulation of MPLS header. */ |
477 | 0 | dp_packet_reset_offload(packet); |
478 | | |
479 | | /* packet_type must be reset for the MPLS packets with no l2 header */ |
480 | 0 | if (!len) { |
481 | 0 | if (ethtype == htons(ETH_TYPE_TEB)) { |
482 | | /* The inner packet must be classified as ethernet if the |
483 | | * ethtype is ETH_TYPE_TEB. */ |
484 | 0 | packet->packet_type = htonl(PT_ETH); |
485 | 0 | } else { |
486 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, |
487 | 0 | ntohs(ethtype)); |
488 | 0 | } |
489 | 0 | } |
490 | 0 | } |
491 | 0 | } |
492 | | |
493 | | void |
494 | | push_nsh(struct dp_packet *packet, const struct nsh_hdr *nsh_hdr_src) |
495 | 0 | { |
496 | 0 | struct nsh_hdr *nsh; |
497 | 0 | size_t length = nsh_hdr_len(nsh_hdr_src); |
498 | 0 | uint8_t next_proto; |
499 | |
|
500 | 0 | switch (ntohl(packet->packet_type)) { |
501 | 0 | case PT_ETH: |
502 | 0 | next_proto = NSH_P_ETHERNET; |
503 | 0 | break; |
504 | 0 | case PT_IPV4: |
505 | 0 | next_proto = NSH_P_IPV4; |
506 | 0 | break; |
507 | 0 | case PT_IPV6: |
508 | 0 | next_proto = NSH_P_IPV6; |
509 | 0 | break; |
510 | 0 | case PT_NSH: |
511 | 0 | next_proto = NSH_P_NSH; |
512 | 0 | break; |
513 | 0 | default: |
514 | 0 | OVS_NOT_REACHED(); |
515 | 0 | } |
516 | | |
517 | 0 | nsh = (struct nsh_hdr *) dp_packet_resize_l2(packet, length); |
518 | 0 | memcpy(nsh, nsh_hdr_src, length); |
519 | 0 | nsh->next_proto = next_proto; |
520 | 0 | packet->packet_type = htonl(PT_NSH); |
521 | 0 | dp_packet_reset_offsets(packet); |
522 | 0 | packet->l3_ofs = 0; |
523 | 0 | } |
524 | | |
525 | | bool |
526 | | pop_nsh(struct dp_packet *packet) |
527 | 0 | { |
528 | 0 | struct nsh_hdr *nsh = (struct nsh_hdr *) dp_packet_l3(packet); |
529 | 0 | size_t length; |
530 | 0 | uint32_t next_pt; |
531 | |
|
532 | 0 | if (packet->packet_type == htonl(PT_NSH) && nsh) { |
533 | 0 | switch (nsh->next_proto) { |
534 | 0 | case NSH_P_ETHERNET: |
535 | 0 | next_pt = PT_ETH; |
536 | 0 | break; |
537 | 0 | case NSH_P_IPV4: |
538 | 0 | next_pt = PT_IPV4; |
539 | 0 | break; |
540 | 0 | case NSH_P_IPV6: |
541 | 0 | next_pt = PT_IPV6; |
542 | 0 | break; |
543 | 0 | case NSH_P_NSH: |
544 | 0 | next_pt = PT_NSH; |
545 | 0 | break; |
546 | 0 | default: |
547 | | /* Unknown inner packet type. Drop packet. */ |
548 | 0 | return false; |
549 | 0 | } |
550 | | |
551 | 0 | length = nsh_hdr_len(nsh); |
552 | 0 | dp_packet_reset_packet(packet, length); |
553 | 0 | packet->packet_type = htonl(next_pt); |
554 | | /* Packet must be recirculated for further processing. */ |
555 | 0 | } |
556 | 0 | return true; |
557 | 0 | } |
558 | | |
559 | | /* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'. The |
560 | | * caller must free '*packetp'. On success, returns NULL. On failure, returns |
561 | | * an error message and stores NULL in '*packetp'. |
562 | | * |
563 | | * Aligns the L3 header of '*packetp' on a 32-bit boundary. */ |
564 | | const char * |
565 | | eth_from_hex(const char *hex, struct dp_packet **packetp) |
566 | 0 | { |
567 | 0 | struct dp_packet *packet; |
568 | | |
569 | | /* Use 2 bytes of headroom to 32-bit align the L3 header. */ |
570 | 0 | packet = *packetp = dp_packet_new_with_headroom(strlen(hex) / 2, 2); |
571 | |
|
572 | 0 | if (dp_packet_put_hex(packet, hex, NULL)[0] != '\0') { |
573 | 0 | dp_packet_delete(packet); |
574 | 0 | *packetp = NULL; |
575 | 0 | return "Trailing garbage in packet data"; |
576 | 0 | } |
577 | | |
578 | 0 | if (dp_packet_size(packet) < ETH_HEADER_LEN) { |
579 | 0 | dp_packet_delete(packet); |
580 | 0 | *packetp = NULL; |
581 | 0 | return "Packet data too short for Ethernet"; |
582 | 0 | } |
583 | | |
584 | 0 | return NULL; |
585 | 0 | } |
586 | | |
587 | | void |
588 | | eth_format_masked(const struct eth_addr eth, |
589 | | const struct eth_addr *mask, struct ds *s) |
590 | 148k | { |
591 | 148k | ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth)); |
592 | 148k | if (mask && !eth_mask_is_exact(*mask)) { |
593 | 8.65k | ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(*mask)); |
594 | 8.65k | } |
595 | 148k | } |
596 | | |
597 | | void |
598 | | in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6) |
599 | 0 | { |
600 | 0 | union ovs_16aligned_in6_addr *taddr = |
601 | 0 | (union ovs_16aligned_in6_addr *) addr; |
602 | 0 | memset(taddr->be16, 0, sizeof(taddr->be16)); |
603 | 0 | taddr->be16[0] = htons(0xff02); |
604 | 0 | taddr->be16[5] = htons(0x1); |
605 | 0 | taddr->be16[6] = htons(0xff00); |
606 | 0 | memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3); |
607 | 0 | } |
608 | | |
609 | | /* |
610 | | * Generates ipv6 EUI64 address from the given eth addr |
611 | | * and prefix and stores it in 'lla' |
612 | | */ |
613 | | void |
614 | | in6_generate_eui64(struct eth_addr ea, const struct in6_addr *prefix, |
615 | | struct in6_addr *lla) |
616 | 0 | { |
617 | 0 | union ovs_16aligned_in6_addr *taddr = |
618 | 0 | (union ovs_16aligned_in6_addr *) lla; |
619 | 0 | union ovs_16aligned_in6_addr *prefix_taddr = |
620 | 0 | (union ovs_16aligned_in6_addr *) prefix; |
621 | 0 | taddr->be16[0] = prefix_taddr->be16[0]; |
622 | 0 | taddr->be16[1] = prefix_taddr->be16[1]; |
623 | 0 | taddr->be16[2] = prefix_taddr->be16[2]; |
624 | 0 | taddr->be16[3] = prefix_taddr->be16[3]; |
625 | 0 | taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]); |
626 | 0 | taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff); |
627 | 0 | taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]); |
628 | 0 | taddr->be16[7] = ea.be16[2]; |
629 | 0 | } |
630 | | |
631 | | /* Generates ipv6 link local address from the given eth addr |
632 | | * with prefix 'fe80::/64' and stores it in 'lla'. */ |
633 | | void |
634 | | in6_generate_lla(struct eth_addr ea, struct in6_addr *lla) |
635 | 0 | { |
636 | 0 | union ovs_16aligned_in6_addr *taddr = |
637 | 0 | (union ovs_16aligned_in6_addr *) lla; |
638 | 0 | memset(taddr->be16, 0, sizeof(taddr->be16)); |
639 | 0 | taddr->be16[0] = htons(0xfe80); |
640 | 0 | taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]); |
641 | 0 | taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff); |
642 | 0 | taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]); |
643 | 0 | taddr->be16[7] = ea.be16[2]; |
644 | 0 | } |
645 | | |
646 | | /* Returns true if 'addr' is a link local address. Otherwise, false. */ |
647 | | bool |
648 | | in6_is_lla(struct in6_addr *addr) |
649 | 0 | { |
650 | 0 | #ifdef s6_addr32 |
651 | 0 | return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]); |
652 | | #else |
653 | | return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 && |
654 | | !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] | |
655 | | addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]); |
656 | | #endif |
657 | 0 | } |
658 | | |
659 | | void |
660 | | ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6) |
661 | 0 | { |
662 | 0 | eth->ea[0] = 0x33; |
663 | 0 | eth->ea[1] = 0x33; |
664 | 0 | eth->ea[2] = ip6->s6_addr[12]; |
665 | 0 | eth->ea[3] = ip6->s6_addr[13]; |
666 | 0 | eth->ea[4] = ip6->s6_addr[14]; |
667 | 0 | eth->ea[5] = ip6->s6_addr[15]; |
668 | 0 | } |
669 | | |
670 | | /* Given the IP netmask 'netmask', returns the number of bits of the IP address |
671 | | * that it specifies, that is, the number of 1-bits in 'netmask'. |
672 | | * |
673 | | * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will |
674 | | * still be in the valid range but isn't otherwise meaningful. */ |
675 | | int |
676 | | ip_count_cidr_bits(ovs_be32 netmask) |
677 | 21.2k | { |
678 | 21.2k | return 32 - ctz32(ntohl(netmask)); |
679 | 21.2k | } |
680 | | |
681 | | void |
682 | | ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s) |
683 | 23.4k | { |
684 | 23.4k | ds_put_format(s, IP_FMT, IP_ARGS(ip)); |
685 | 23.4k | if (mask != OVS_BE32_MAX) { |
686 | 7.83k | if (ip_is_cidr(mask)) { |
687 | 6.29k | ds_put_format(s, "/%d", ip_count_cidr_bits(mask)); |
688 | 6.29k | } else { |
689 | 1.54k | ds_put_format(s, "/"IP_FMT, IP_ARGS(mask)); |
690 | 1.54k | } |
691 | 7.83k | } |
692 | 23.4k | } |
693 | | |
694 | | /* Parses string 's', which must be an IP address. Stores the IP address into |
695 | | * '*ip'. Returns true if successful, otherwise false. */ |
696 | | bool |
697 | | ip_parse(const char *s, ovs_be32 *ip) |
698 | 1.79k | { |
699 | 1.79k | return inet_pton(AF_INET, s, ip) == 1; |
700 | 1.79k | } |
701 | | |
702 | | /* Parses string 's', which must be an IP address with a port number |
703 | | * with ":" as a separator (e.g.: 192.168.1.2:80). |
704 | | * Stores the IP address into '*ip' and port number to '*port'. |
705 | | * |
706 | | * Returns NULL if successful, otherwise an error message that the caller must |
707 | | * free(). */ |
708 | | char * OVS_WARN_UNUSED_RESULT |
709 | | ip_parse_port(const char *s, ovs_be32 *ip, ovs_be16 *port) |
710 | 0 | { |
711 | 0 | int n = 0; |
712 | 0 | if (ovs_scan(s, IP_PORT_SCAN_FMT"%n", IP_PORT_SCAN_ARGS(ip, port), &n) |
713 | 0 | && !s[n]) { |
714 | 0 | return NULL; |
715 | 0 | } |
716 | | |
717 | 0 | return xasprintf("%s: invalid IP address or port number", s); |
718 | 0 | } |
719 | | |
720 | | /* Parses string 's', which must be an IP address with an optional netmask or |
721 | | * CIDR prefix length. Stores the IP address into '*ip', netmask into '*mask', |
722 | | * (255.255.255.255, if 's' lacks a netmask), and number of scanned characters |
723 | | * into '*n'. |
724 | | * |
725 | | * Returns NULL if successful, otherwise an error message that the caller must |
726 | | * free(). */ |
727 | | char * OVS_WARN_UNUSED_RESULT |
728 | | ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, |
729 | | ovs_be32 *mask) |
730 | 1.01k | { |
731 | 1.01k | int prefix; |
732 | | |
733 | 1.01k | if (ovs_scan_len(s, n, IP_SCAN_FMT"/"IP_SCAN_FMT, |
734 | 1.01k | IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) { |
735 | | /* OK. */ |
736 | 924 | } else if (ovs_scan_len(s, n, IP_SCAN_FMT"/%d", |
737 | 924 | IP_SCAN_ARGS(ip), &prefix)) { |
738 | 568 | if (prefix < 0 || prefix > 32) { |
739 | 49 | return xasprintf("%s: IPv4 network prefix bits not between 0 and " |
740 | 49 | "32, inclusive", s); |
741 | 49 | } |
742 | 519 | *mask = be32_prefix_mask(prefix); |
743 | 519 | } else if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) { |
744 | 321 | *mask = OVS_BE32_MAX; |
745 | 321 | } else { |
746 | 35 | return xasprintf("%s: invalid IP address", s); |
747 | 35 | } |
748 | 934 | return NULL; |
749 | 1.01k | } |
750 | | |
751 | | /* This function is similar to ip_parse_masked_len(), but doesn't return the |
752 | | * number of scanned characters and expects 's' to end after the ip/(optional) |
753 | | * mask. |
754 | | * |
755 | | * Returns NULL if successful, otherwise an error message that the caller must |
756 | | * free(). */ |
757 | | char * OVS_WARN_UNUSED_RESULT |
758 | | ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask) |
759 | 1.01k | { |
760 | 1.01k | int n = 0; |
761 | | |
762 | 1.01k | char *error = ip_parse_masked_len(s, &n, ip, mask); |
763 | 1.01k | if (!error && s[n]) { |
764 | 11 | return xasprintf("%s: invalid IP address", s); |
765 | 11 | } |
766 | 1.00k | return error; |
767 | 1.01k | } |
768 | | |
769 | | /* Similar to ip_parse_masked_len(), but the mask, if present, must be a CIDR |
770 | | * mask and is returned as a prefix len in '*plen'. */ |
771 | | char * OVS_WARN_UNUSED_RESULT |
772 | | ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip, unsigned int *plen) |
773 | 0 | { |
774 | 0 | ovs_be32 mask; |
775 | 0 | char *error; |
776 | |
|
777 | 0 | error = ip_parse_masked_len(s, n, ip, &mask); |
778 | 0 | if (error) { |
779 | 0 | return error; |
780 | 0 | } |
781 | | |
782 | 0 | if (!ip_is_cidr(mask)) { |
783 | 0 | return xasprintf("%s: CIDR network required", s); |
784 | 0 | } |
785 | 0 | *plen = ip_count_cidr_bits(mask); |
786 | 0 | return NULL; |
787 | 0 | } |
788 | | |
789 | | /* Similar to ip_parse_cidr_len(), but doesn't return the number of scanned |
790 | | * characters and expects 's' to be NULL terminated at the end of the |
791 | | * ip/(optional) cidr. */ |
792 | | char * OVS_WARN_UNUSED_RESULT |
793 | | ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen) |
794 | 0 | { |
795 | 0 | int n = 0; |
796 | |
|
797 | 0 | char *error = ip_parse_cidr_len(s, &n, ip, plen); |
798 | 0 | if (!error && s[n]) { |
799 | 0 | return xasprintf("%s: invalid IP address", s); |
800 | 0 | } |
801 | 0 | return error; |
802 | 0 | } |
803 | | |
804 | | /* Parses string 's', which must be an IPv6 address. Stores the IPv6 address |
805 | | * into '*ip'. Returns true if successful, otherwise false. */ |
806 | | bool |
807 | | ipv6_parse(const char *s, struct in6_addr *ip) |
808 | 3.21k | { |
809 | 3.21k | return inet_pton(AF_INET6, s, ip) == 1; |
810 | 3.21k | } |
811 | | |
812 | | /* Parses string 's', which must be an IPv6 address with an optional netmask or |
813 | | * CIDR prefix length. Stores the IPv6 address into '*ip' and the netmask into |
814 | | * '*mask' (if 's' does not contain a netmask, all-one-bits is assumed), and |
815 | | * number of scanned characters into '*n'. |
816 | | * |
817 | | * Returns NULL if successful, otherwise an error message that the caller must |
818 | | * free(). */ |
819 | | char * OVS_WARN_UNUSED_RESULT |
820 | | ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ip, |
821 | | struct in6_addr *mask) |
822 | 2.95k | { |
823 | 2.95k | char ipv6_s[IPV6_SCAN_LEN + 1]; |
824 | 2.95k | int prefix; |
825 | | |
826 | 2.95k | if (ovs_scan_len(s, n, " "IPV6_SCAN_FMT, ipv6_s) |
827 | 2.93k | && ipv6_parse(ipv6_s, ip)) { |
828 | 2.92k | if (ovs_scan_len(s, n, "/%d", &prefix)) { |
829 | 1.94k | if (prefix < 0 || prefix > 128) { |
830 | 56 | return xasprintf("%s: IPv6 network prefix bits not between 0 " |
831 | 56 | "and 128, inclusive", s); |
832 | 56 | } |
833 | 1.89k | *mask = ipv6_create_mask(prefix); |
834 | 1.89k | } else if (ovs_scan_len(s, n, "/"IPV6_SCAN_FMT, ipv6_s)) { |
835 | 277 | if (!ipv6_parse(ipv6_s, mask)) { |
836 | 2 | return xasprintf("%s: Invalid IPv6 mask", s); |
837 | 2 | } |
838 | | /* OK. */ |
839 | 704 | } else { |
840 | | /* OK. No mask. */ |
841 | 704 | *mask = in6addr_exact; |
842 | 704 | } |
843 | 2.87k | return NULL; |
844 | 2.92k | } |
845 | 23 | return xasprintf("%s: invalid IPv6 address", s); |
846 | 2.95k | } |
847 | | |
848 | | /* This function is similar to ipv6_parse_masked_len(), but doesn't return the |
849 | | * number of scanned characters and expects 's' to end following the |
850 | | * ipv6/(optional) mask. */ |
851 | | char * OVS_WARN_UNUSED_RESULT |
852 | | ipv6_parse_masked(const char *s, struct in6_addr *ip, struct in6_addr *mask) |
853 | 2.95k | { |
854 | 2.95k | int n = 0; |
855 | | |
856 | 2.95k | char *error = ipv6_parse_masked_len(s, &n, ip, mask); |
857 | 2.95k | if (!error && s[n]) { |
858 | 10 | return xasprintf("%s: invalid IPv6 address", s); |
859 | 10 | } |
860 | 2.94k | return error; |
861 | 2.95k | } |
862 | | |
863 | | /* Similar to ipv6_parse_masked_len(), but the mask, if present, must be a CIDR |
864 | | * mask and is returned as a prefix length in '*plen'. */ |
865 | | char * OVS_WARN_UNUSED_RESULT |
866 | | ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip, |
867 | | unsigned int *plen) |
868 | 0 | { |
869 | 0 | struct in6_addr mask; |
870 | 0 | char *error; |
871 | |
|
872 | 0 | error = ipv6_parse_masked_len(s, n, ip, &mask); |
873 | 0 | if (error) { |
874 | 0 | return error; |
875 | 0 | } |
876 | | |
877 | 0 | if (!ipv6_is_cidr(&mask)) { |
878 | 0 | return xasprintf("%s: IPv6 CIDR network required", s); |
879 | 0 | } |
880 | 0 | *plen = ipv6_count_cidr_bits(&mask); |
881 | 0 | return NULL; |
882 | 0 | } |
883 | | |
884 | | /* Similar to ipv6_parse_cidr_len(), but doesn't return the number of scanned |
885 | | * characters and expects 's' to end after the ipv6/(optional) cidr. */ |
886 | | char * OVS_WARN_UNUSED_RESULT |
887 | | ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen) |
888 | 0 | { |
889 | 0 | int n = 0; |
890 | |
|
891 | 0 | char *error = ipv6_parse_cidr_len(s, &n, ip, plen); |
892 | 0 | if (!error && s[n]) { |
893 | 0 | return xasprintf("%s: invalid IPv6 address", s); |
894 | 0 | } |
895 | 0 | return error; |
896 | 0 | } |
897 | | |
898 | | /* Stores the string representation of the IPv6 address 'addr' into the |
899 | | * character array 'addr_str', which must be at least INET6_ADDRSTRLEN |
900 | | * bytes long. */ |
901 | | void |
902 | | ipv6_format_addr(const struct in6_addr *addr, struct ds *s) |
903 | 64.7k | { |
904 | 64.7k | char *dst; |
905 | | |
906 | 64.7k | ds_reserve(s, s->length + INET6_ADDRSTRLEN); |
907 | | |
908 | 64.7k | dst = s->string + s->length; |
909 | 64.7k | inet_ntop(AF_INET6, addr, dst, INET6_ADDRSTRLEN); |
910 | 64.7k | s->length += strlen(dst); |
911 | 64.7k | } |
912 | | |
913 | | /* Same as print_ipv6_addr, but optionally encloses the address in square |
914 | | * brackets. */ |
915 | | void |
916 | | ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *s, |
917 | | bool bracket) |
918 | 6.39k | { |
919 | 6.39k | if (bracket) { |
920 | 2.98k | ds_put_char(s, '['); |
921 | 2.98k | } |
922 | 6.39k | ipv6_format_addr(addr, s); |
923 | 6.39k | if (bracket) { |
924 | 2.98k | ds_put_char(s, ']'); |
925 | 2.98k | } |
926 | 6.39k | } |
927 | | |
928 | | void |
929 | | ipv6_format_mapped(const struct in6_addr *addr, struct ds *s) |
930 | 17.6k | { |
931 | 17.6k | if (IN6_IS_ADDR_V4MAPPED(addr)) { |
932 | 508 | ds_put_format(s, IP_FMT, addr->s6_addr[12], addr->s6_addr[13], |
933 | 508 | addr->s6_addr[14], addr->s6_addr[15]); |
934 | 17.1k | } else { |
935 | 17.1k | ipv6_format_addr(addr, s); |
936 | 17.1k | } |
937 | 17.6k | } |
938 | | |
939 | | void |
940 | | ipv6_format_masked(const struct in6_addr *addr, const struct in6_addr *mask, |
941 | | struct ds *s) |
942 | 40.7k | { |
943 | 40.7k | ipv6_format_addr(addr, s); |
944 | 40.7k | if (mask && !ipv6_mask_is_exact(mask)) { |
945 | 1.00k | if (ipv6_is_cidr(mask)) { |
946 | 456 | int cidr_bits = ipv6_count_cidr_bits(mask); |
947 | 456 | ds_put_format(s, "/%d", cidr_bits); |
948 | 545 | } else { |
949 | 545 | ds_put_char(s, '/'); |
950 | 545 | ipv6_format_addr(mask, s); |
951 | 545 | } |
952 | 1.00k | } |
953 | 40.7k | } |
954 | | |
955 | | /* Stores the string representation of the IPv6 address 'addr' into the |
956 | | * character array 'addr_str', which must be at least INET6_ADDRSTRLEN |
957 | | * bytes long. If addr is IPv4-mapped, store an IPv4 dotted-decimal string. */ |
958 | | const char * |
959 | | ipv6_string_mapped(char *addr_str, const struct in6_addr *addr) |
960 | 0 | { |
961 | 0 | ovs_be32 ip; |
962 | 0 | ip = in6_addr_get_mapped_ipv4(addr); |
963 | 0 | if (ip) { |
964 | 0 | return inet_ntop(AF_INET, &ip, addr_str, INET6_ADDRSTRLEN); |
965 | 0 | } else { |
966 | 0 | return inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN); |
967 | 0 | } |
968 | 0 | } |
969 | | |
970 | | #ifdef s6_addr32 |
971 | 90.5k | #define s6_addrX s6_addr32 |
972 | 49.7k | #define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 4; VAR++) |
973 | | #else |
974 | | #define s6_addrX s6_addr |
975 | | #define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 16; VAR++) |
976 | | #endif |
977 | | |
978 | | struct in6_addr |
979 | | ipv6_addr_bitand(const struct in6_addr *a, const struct in6_addr *b) |
980 | 5.92k | { |
981 | 5.92k | struct in6_addr dst; |
982 | 23.6k | IPV6_FOR_EACH (i) { |
983 | 23.6k | dst.s6_addrX[i] = a->s6_addrX[i] & b->s6_addrX[i]; |
984 | 23.6k | } |
985 | 5.92k | return dst; |
986 | 5.92k | } |
987 | | |
988 | | struct in6_addr |
989 | | ipv6_addr_bitxor(const struct in6_addr *a, const struct in6_addr *b) |
990 | 0 | { |
991 | 0 | struct in6_addr dst; |
992 | 0 | IPV6_FOR_EACH (i) { |
993 | 0 | dst.s6_addrX[i] = a->s6_addrX[i] ^ b->s6_addrX[i]; |
994 | 0 | } |
995 | 0 | return dst; |
996 | 0 | } |
997 | | |
998 | | bool |
999 | | ipv6_is_zero(const struct in6_addr *a) |
1000 | 9.87k | { |
1001 | 19.5k | IPV6_FOR_EACH (i) { |
1002 | 19.5k | if (a->s6_addrX[i]) { |
1003 | 9.21k | return false; |
1004 | 9.21k | } |
1005 | 19.5k | } |
1006 | 662 | return true; |
1007 | 9.87k | } |
1008 | | |
1009 | | /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N |
1010 | | * low-order 0-bits. */ |
1011 | | struct in6_addr |
1012 | | ipv6_create_mask(int mask) |
1013 | 1.89k | { |
1014 | 1.89k | struct in6_addr netmask; |
1015 | 1.89k | uint8_t *netmaskp = &netmask.s6_addr[0]; |
1016 | | |
1017 | 1.89k | memset(&netmask, 0, sizeof netmask); |
1018 | 3.51k | while (mask > 8) { |
1019 | 1.62k | *netmaskp = 0xff; |
1020 | 1.62k | netmaskp++; |
1021 | 1.62k | mask -= 8; |
1022 | 1.62k | } |
1023 | | |
1024 | 1.89k | if (mask) { |
1025 | 1.30k | *netmaskp = 0xff << (8 - mask); |
1026 | 1.30k | } |
1027 | | |
1028 | 1.89k | return netmask; |
1029 | 1.89k | } |
1030 | | |
1031 | | /* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6 |
1032 | | * address that it specifies, that is, the number of 1-bits in 'netmask'. |
1033 | | * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()). |
1034 | | * |
1035 | | * If 'netmask' is not a CIDR netmask (see ipv6_is_cidr()), the return value |
1036 | | * will still be in the valid range but isn't otherwise meaningful. */ |
1037 | | int |
1038 | | ipv6_count_cidr_bits(const struct in6_addr *netmask) |
1039 | 456 | { |
1040 | 456 | int i; |
1041 | 456 | int count = 0; |
1042 | 456 | const uint8_t *netmaskp = &netmask->s6_addr[0]; |
1043 | | |
1044 | 4.85k | for (i=0; i<16; i++) { |
1045 | 4.85k | if (netmaskp[i] == 0xff) { |
1046 | 4.39k | count += 8; |
1047 | 4.39k | } else { |
1048 | 456 | uint8_t nm; |
1049 | | |
1050 | 2.14k | for(nm = netmaskp[i]; nm; nm <<= 1) { |
1051 | 1.69k | count++; |
1052 | 1.69k | } |
1053 | 456 | break; |
1054 | 456 | } |
1055 | | |
1056 | 4.85k | } |
1057 | | |
1058 | 456 | return count; |
1059 | 456 | } |
1060 | | |
1061 | | /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N |
1062 | | * high-order 1-bits and 128-N low-order 0-bits. */ |
1063 | | bool |
1064 | | ipv6_is_cidr(const struct in6_addr *netmask) |
1065 | 1.00k | { |
1066 | 1.00k | const uint8_t *netmaskp = &netmask->s6_addr[0]; |
1067 | 1.00k | int i; |
1068 | | |
1069 | 8.21k | for (i=0; i<16; i++) { |
1070 | 7.75k | if (netmaskp[i] != 0xff) { |
1071 | 1.00k | uint8_t x = ~netmaskp[i]; |
1072 | 1.00k | if (x & (x + 1)) { |
1073 | 220 | return false; |
1074 | 220 | } |
1075 | 4.61k | while (++i < 16) { |
1076 | 4.16k | if (netmaskp[i]) { |
1077 | 325 | return false; |
1078 | 325 | } |
1079 | 4.16k | } |
1080 | 781 | } |
1081 | 7.75k | } |
1082 | | |
1083 | 456 | return true; |
1084 | 1.00k | } |
1085 | | |
1086 | | bool |
1087 | | ipv6_addr_equals_masked(const struct in6_addr *a, const struct in6_addr *b, |
1088 | | int plen) |
1089 | 0 | { |
1090 | 0 | struct in6_addr mask; |
1091 | 0 | struct in6_addr ma; |
1092 | 0 | struct in6_addr mb; |
1093 | |
|
1094 | 0 | if (plen == 128) { |
1095 | 0 | return ipv6_addr_equals(a, b); |
1096 | 0 | } |
1097 | | |
1098 | 0 | mask = ipv6_create_mask(plen); |
1099 | 0 | ma = ipv6_addr_bitand(a, &mask); |
1100 | 0 | mb = ipv6_addr_bitand(b, &mask); |
1101 | |
|
1102 | 0 | return ipv6_addr_equals(&ma, &mb); |
1103 | 0 | } |
1104 | | |
1105 | | /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst', |
1106 | | * 'eth_src' and 'eth_type' parameters. A payload of 'size' bytes is allocated |
1107 | | * in 'b' and returned. This payload may be populated with appropriate |
1108 | | * information by the caller. Sets 'b''s 'frame' pointer and 'l3' offset to |
1109 | | * the Ethernet header and payload respectively. Aligns b->l3 on a 32-bit |
1110 | | * boundary. |
1111 | | * |
1112 | | * The returned packet has enough headroom to insert an 802.1Q VLAN header if |
1113 | | * desired. */ |
1114 | | void * |
1115 | | eth_compose(struct dp_packet *b, const struct eth_addr eth_dst, |
1116 | | const struct eth_addr eth_src, uint16_t eth_type, |
1117 | | size_t size) |
1118 | 0 | { |
1119 | 0 | void *data; |
1120 | 0 | struct eth_header *eth; |
1121 | | |
1122 | |
|
1123 | 0 | dp_packet_clear(b); |
1124 | | |
1125 | | /* The magic 2 here ensures that the L3 header (when it is added later) |
1126 | | * will be 32-bit aligned. */ |
1127 | 0 | dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + size); |
1128 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
1129 | 0 | eth = dp_packet_put_uninit(b, ETH_HEADER_LEN); |
1130 | 0 | data = dp_packet_put_zeros(b, size); |
1131 | |
|
1132 | 0 | eth->eth_dst = eth_dst; |
1133 | 0 | eth->eth_src = eth_src; |
1134 | 0 | eth->eth_type = htons(eth_type); |
1135 | |
|
1136 | 0 | b->packet_type = htonl(PT_ETH); |
1137 | 0 | dp_packet_set_l3(b, data); |
1138 | |
|
1139 | 0 | return data; |
1140 | 0 | } |
1141 | | |
1142 | | void |
1143 | | packet_set_ipv4_addr(struct dp_packet *packet, |
1144 | | ovs_16aligned_be32 *addr, ovs_be32 new_addr) |
1145 | 0 | { |
1146 | 0 | struct ip_header *nh = dp_packet_l3(packet); |
1147 | 0 | ovs_be32 old_addr = get_16aligned_be32(addr); |
1148 | 0 | size_t l4_size = dp_packet_l4_size(packet); |
1149 | |
|
1150 | 0 | pkt_metadata_init_conn(&packet->md); |
1151 | |
|
1152 | 0 | if (nh->ip_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) { |
1153 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1154 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1155 | 0 | } else { |
1156 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1157 | 0 | th->tcp_csum = recalc_csum32(th->tcp_csum, old_addr, new_addr); |
1158 | 0 | } |
1159 | 0 | } else if (nh->ip_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN ) { |
1160 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1161 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1162 | 0 | } else { |
1163 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1164 | 0 | if (uh->udp_csum) { |
1165 | 0 | uh->udp_csum = recalc_csum32(uh->udp_csum, old_addr, new_addr); |
1166 | 0 | if (!uh->udp_csum) { |
1167 | 0 | uh->udp_csum = htons(0xffff); |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | } |
1171 | 0 | } |
1172 | |
|
1173 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1174 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1175 | 0 | } else { |
1176 | 0 | nh->ip_csum = recalc_csum32(nh->ip_csum, old_addr, new_addr); |
1177 | 0 | } |
1178 | 0 | put_16aligned_be32(addr, new_addr); |
1179 | 0 | } |
1180 | | |
1181 | | /* Returns true, if packet contains at least one routing header where |
1182 | | * segements_left > 0. |
1183 | | * |
1184 | | * This function assumes that L3 and L4 offsets are set in the packet. */ |
1185 | | bool |
1186 | | packet_rh_present(struct dp_packet *packet, uint8_t *nexthdr, bool *first_frag) |
1187 | 0 | { |
1188 | 0 | const struct ovs_16aligned_ip6_hdr *nh; |
1189 | 0 | size_t len; |
1190 | 0 | size_t remaining; |
1191 | 0 | uint8_t *data = dp_packet_l3(packet); |
1192 | |
|
1193 | 0 | remaining = packet->l4_ofs - packet->l3_ofs; |
1194 | 0 | if (remaining < sizeof *nh) { |
1195 | 0 | return false; |
1196 | 0 | } |
1197 | 0 | nh = ALIGNED_CAST(struct ovs_16aligned_ip6_hdr *, data); |
1198 | 0 | data += sizeof *nh; |
1199 | 0 | remaining -= sizeof *nh; |
1200 | 0 | *nexthdr = nh->ip6_nxt; |
1201 | |
|
1202 | 0 | while (1) { |
1203 | 0 | if ((*nexthdr != IPPROTO_HOPOPTS) |
1204 | 0 | && (*nexthdr != IPPROTO_ROUTING) |
1205 | 0 | && (*nexthdr != IPPROTO_DSTOPTS) |
1206 | 0 | && (*nexthdr != IPPROTO_AH) |
1207 | 0 | && (*nexthdr != IPPROTO_FRAGMENT)) { |
1208 | | /* It's either a terminal header (e.g., TCP, UDP) or one we |
1209 | | * don't understand. In either case, we're done with the |
1210 | | * packet, so use it to fill in 'nw_proto'. */ |
1211 | 0 | break; |
1212 | 0 | } |
1213 | | |
1214 | | /* We only verify that at least 8 bytes of the next header are |
1215 | | * available, but many of these headers are longer. Ensure that |
1216 | | * accesses within the extension header are within those first 8 |
1217 | | * bytes. All extension headers are required to be at least 8 |
1218 | | * bytes. */ |
1219 | 0 | if (remaining < 8) { |
1220 | 0 | return false; |
1221 | 0 | } |
1222 | | |
1223 | 0 | if (*nexthdr == IPPROTO_AH) { |
1224 | | /* A standard AH definition isn't available, but the fields |
1225 | | * we care about are in the same location as the generic |
1226 | | * option header--only the header length is calculated |
1227 | | * differently. */ |
1228 | 0 | const struct ip6_ext *ext_hdr = (struct ip6_ext *)data; |
1229 | |
|
1230 | 0 | *nexthdr = ext_hdr->ip6e_nxt; |
1231 | 0 | len = (ext_hdr->ip6e_len + 2) * 4; |
1232 | 0 | } else if (*nexthdr == IPPROTO_FRAGMENT) { |
1233 | 0 | const struct ovs_16aligned_ip6_frag *frag_hdr |
1234 | 0 | = ALIGNED_CAST(struct ovs_16aligned_ip6_frag *, data); |
1235 | |
|
1236 | 0 | *first_frag = !(frag_hdr->ip6f_offlg & IP6F_OFF_MASK) && |
1237 | 0 | (frag_hdr->ip6f_offlg & IP6F_MORE_FRAG); |
1238 | 0 | *nexthdr = frag_hdr->ip6f_nxt; |
1239 | 0 | len = sizeof *frag_hdr; |
1240 | 0 | } else if (*nexthdr == IPPROTO_ROUTING) { |
1241 | 0 | const struct ip6_rthdr *rh = (struct ip6_rthdr *)data; |
1242 | |
|
1243 | 0 | if (rh->ip6r_segleft > 0) { |
1244 | 0 | return true; |
1245 | 0 | } |
1246 | | |
1247 | 0 | *nexthdr = rh->ip6r_nxt; |
1248 | 0 | len = (rh->ip6r_len + 1) * 8; |
1249 | 0 | } else { |
1250 | 0 | const struct ip6_ext *ext_hdr = (struct ip6_ext *)data; |
1251 | |
|
1252 | 0 | *nexthdr = ext_hdr->ip6e_nxt; |
1253 | 0 | len = (ext_hdr->ip6e_len + 1) * 8; |
1254 | 0 | } |
1255 | | |
1256 | 0 | if (remaining < len) { |
1257 | 0 | return false; |
1258 | 0 | } |
1259 | 0 | remaining -= len; |
1260 | 0 | data += len; |
1261 | 0 | } |
1262 | | |
1263 | 0 | return false; |
1264 | 0 | } |
1265 | | |
1266 | | static void |
1267 | | packet_update_csum128(struct dp_packet *packet, uint8_t proto, |
1268 | | ovs_16aligned_be32 addr[4], |
1269 | | const struct in6_addr *new_addr) |
1270 | 0 | { |
1271 | 0 | size_t l4_size = dp_packet_l4_size(packet); |
1272 | |
|
1273 | 0 | if (proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) { |
1274 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1275 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1276 | 0 | } else { |
1277 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1278 | |
|
1279 | 0 | th->tcp_csum = recalc_csum128(th->tcp_csum, addr, new_addr); |
1280 | 0 | } |
1281 | 0 | } else if (proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) { |
1282 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1283 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1284 | 0 | } else { |
1285 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1286 | |
|
1287 | 0 | if (uh->udp_csum) { |
1288 | 0 | uh->udp_csum = recalc_csum128(uh->udp_csum, addr, new_addr); |
1289 | 0 | if (!uh->udp_csum) { |
1290 | 0 | uh->udp_csum = htons(0xffff); |
1291 | 0 | } |
1292 | 0 | } |
1293 | 0 | } |
1294 | 0 | } else if (proto == IPPROTO_ICMPV6 && |
1295 | 0 | l4_size >= sizeof(struct icmp6_header)) { |
1296 | 0 | struct icmp6_header *icmp = dp_packet_l4(packet); |
1297 | |
|
1298 | 0 | icmp->icmp6_cksum = recalc_csum128(icmp->icmp6_cksum, addr, new_addr); |
1299 | 0 | } |
1300 | 0 | } |
1301 | | |
1302 | | void |
1303 | | packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto, |
1304 | | ovs_16aligned_be32 addr[4], |
1305 | | const struct in6_addr *new_addr, |
1306 | | bool recalculate_csum) |
1307 | 0 | { |
1308 | 0 | if (recalculate_csum) { |
1309 | 0 | packet_update_csum128(packet, proto, addr, new_addr); |
1310 | 0 | } |
1311 | 0 | memcpy(addr, new_addr, sizeof(ovs_be32[4])); |
1312 | 0 | pkt_metadata_init_conn(&packet->md); |
1313 | 0 | } |
1314 | | |
1315 | | void |
1316 | | packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key) |
1317 | 0 | { |
1318 | 0 | ovs_be32 old_label = get_16aligned_be32(flow_label); |
1319 | 0 | ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key; |
1320 | 0 | put_16aligned_be32(flow_label, new_label); |
1321 | 0 | } |
1322 | | |
1323 | | void |
1324 | | packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc) |
1325 | 0 | { |
1326 | 0 | ovs_be32 old_label = get_16aligned_be32(flow_label); |
1327 | 0 | ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20); |
1328 | 0 | put_16aligned_be32(flow_label, new_label); |
1329 | 0 | } |
1330 | | |
1331 | | /* Modifies the IPv4 header fields of 'packet' to be consistent with 'src', |
1332 | | * 'dst', 'tos', and 'ttl'. Updates 'packet''s L4 checksums as appropriate. |
1333 | | * 'packet' must contain a valid IPv4 packet with correctly populated l[347] |
1334 | | * markers. */ |
1335 | | void |
1336 | | packet_set_ipv4(struct dp_packet *packet, ovs_be32 src, ovs_be32 dst, |
1337 | | uint8_t tos, uint8_t ttl) |
1338 | 0 | { |
1339 | 0 | struct ip_header *nh = dp_packet_l3(packet); |
1340 | |
|
1341 | 0 | if (get_16aligned_be32(&nh->ip_src) != src) { |
1342 | 0 | packet_set_ipv4_addr(packet, &nh->ip_src, src); |
1343 | 0 | } |
1344 | |
|
1345 | 0 | if (get_16aligned_be32(&nh->ip_dst) != dst) { |
1346 | 0 | packet_set_ipv4_addr(packet, &nh->ip_dst, dst); |
1347 | 0 | } |
1348 | |
|
1349 | 0 | if (nh->ip_tos != tos) { |
1350 | 0 | uint8_t *field = &nh->ip_tos; |
1351 | |
|
1352 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1353 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1354 | 0 | } else { |
1355 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t) *field), |
1356 | 0 | htons((uint16_t) tos)); |
1357 | 0 | } |
1358 | |
|
1359 | 0 | *field = tos; |
1360 | 0 | } |
1361 | |
|
1362 | 0 | if (nh->ip_ttl != ttl) { |
1363 | 0 | uint8_t *field = &nh->ip_ttl; |
1364 | |
|
1365 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1366 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1367 | 0 | } else { |
1368 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons(*field << 8), |
1369 | 0 | htons(ttl << 8)); |
1370 | 0 | } |
1371 | |
|
1372 | 0 | *field = ttl; |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | | /* Modifies the IPv6 header fields of 'packet' to be consistent with 'src', |
1377 | | * 'dst', 'traffic class', and 'next hop'. Updates 'packet''s L4 checksums as |
1378 | | * appropriate. 'packet' must contain a valid IPv6 packet with correctly |
1379 | | * populated l[34] offsets. */ |
1380 | | void |
1381 | | packet_set_ipv6(struct dp_packet *packet, const struct in6_addr *src, |
1382 | | const struct in6_addr *dst, uint8_t key_tc, ovs_be32 key_fl, |
1383 | | uint8_t key_hl) |
1384 | 0 | { |
1385 | 0 | struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet); |
1386 | 0 | bool recalc_csum = true; |
1387 | 0 | uint8_t proto = 0; |
1388 | 0 | bool rh_present; |
1389 | |
|
1390 | 0 | rh_present = packet_rh_present(packet, &proto, &recalc_csum); |
1391 | |
|
1392 | 0 | if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) { |
1393 | 0 | packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32, |
1394 | 0 | src, recalc_csum); |
1395 | 0 | } |
1396 | |
|
1397 | 0 | if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) { |
1398 | 0 | packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst, |
1399 | 0 | !rh_present && recalc_csum); |
1400 | 0 | } |
1401 | |
|
1402 | 0 | packet_set_ipv6_tc(&nh->ip6_flow, key_tc); |
1403 | 0 | packet_set_ipv6_flow_label(&nh->ip6_flow, key_fl); |
1404 | 0 | nh->ip6_hlim = key_hl; |
1405 | 0 | } |
1406 | | |
1407 | | static void |
1408 | | packet_set_port(ovs_be16 *port, ovs_be16 new_port, ovs_be16 *csum) |
1409 | 0 | { |
1410 | 0 | if (*port != new_port) { |
1411 | 0 | if (csum) { |
1412 | 0 | *csum = recalc_csum16(*csum, *port, new_port); |
1413 | 0 | } |
1414 | 0 | *port = new_port; |
1415 | 0 | } |
1416 | 0 | } |
1417 | | |
1418 | | /* Sets the TCP source and destination port ('src' and 'dst' respectively) of |
1419 | | * the TCP header contained in 'packet'. 'packet' must be a valid TCP packet |
1420 | | * with its l4 offset properly populated. */ |
1421 | | void |
1422 | | packet_set_tcp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1423 | 0 | { |
1424 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1425 | 0 | ovs_be16 *csum = NULL; |
1426 | |
|
1427 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1428 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1429 | 0 | } else { |
1430 | 0 | csum = &th->tcp_csum; |
1431 | 0 | } |
1432 | |
|
1433 | 0 | packet_set_port(&th->tcp_src, src, csum); |
1434 | 0 | packet_set_port(&th->tcp_dst, dst, csum); |
1435 | 0 | pkt_metadata_init_conn(&packet->md); |
1436 | 0 | } |
1437 | | |
1438 | | /* Sets the UDP source and destination port ('src' and 'dst' respectively) of |
1439 | | * the UDP header contained in 'packet'. 'packet' must be a valid UDP packet |
1440 | | * with its l4 offset properly populated. */ |
1441 | | void |
1442 | | packet_set_udp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1443 | 0 | { |
1444 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1445 | |
|
1446 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1447 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1448 | 0 | packet_set_port(&uh->udp_src, src, NULL); |
1449 | 0 | packet_set_port(&uh->udp_dst, dst, NULL); |
1450 | 0 | } else { |
1451 | 0 | ovs_be16 *csum = uh->udp_csum ? &uh->udp_csum : NULL; |
1452 | |
|
1453 | 0 | packet_set_port(&uh->udp_src, src, csum); |
1454 | 0 | packet_set_port(&uh->udp_dst, dst, csum); |
1455 | |
|
1456 | 0 | if (csum && !uh->udp_csum) { |
1457 | 0 | uh->udp_csum = htons(0xffff); |
1458 | 0 | } |
1459 | 0 | } |
1460 | |
|
1461 | 0 | pkt_metadata_init_conn(&packet->md); |
1462 | 0 | } |
1463 | | |
1464 | | /* Sets the SCTP source and destination port ('src' and 'dst' respectively) of |
1465 | | * the SCTP header contained in 'packet'. 'packet' must be a valid SCTP packet |
1466 | | * with its l4 offset properly populated. */ |
1467 | | void |
1468 | | packet_set_sctp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1469 | 0 | { |
1470 | 0 | struct sctp_header *sh = dp_packet_l4(packet); |
1471 | |
|
1472 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1473 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1474 | 0 | sh->sctp_src = src; |
1475 | 0 | sh->sctp_dst = dst; |
1476 | 0 | } else { |
1477 | 0 | ovs_be32 old_csum, old_correct_csum, new_csum; |
1478 | 0 | uint16_t tp_len = dp_packet_l4_size(packet); |
1479 | |
|
1480 | 0 | old_csum = get_16aligned_be32(&sh->sctp_csum); |
1481 | 0 | put_16aligned_be32(&sh->sctp_csum, 0); |
1482 | 0 | old_correct_csum = crc32c((void *) sh, tp_len); |
1483 | |
|
1484 | 0 | sh->sctp_src = src; |
1485 | 0 | sh->sctp_dst = dst; |
1486 | |
|
1487 | 0 | new_csum = crc32c((void *) sh, tp_len); |
1488 | 0 | put_16aligned_be32(&sh->sctp_csum, old_csum ^ old_correct_csum |
1489 | 0 | ^ new_csum); |
1490 | 0 | } |
1491 | |
|
1492 | 0 | pkt_metadata_init_conn(&packet->md); |
1493 | 0 | } |
1494 | | |
1495 | | /* Sets the ICMP type and code of the ICMP header contained in 'packet'. |
1496 | | * 'packet' must be a valid ICMP packet with its l4 offset properly |
1497 | | * populated. */ |
1498 | | void |
1499 | | packet_set_icmp(struct dp_packet *packet, uint8_t type, uint8_t code) |
1500 | 0 | { |
1501 | 0 | struct icmp_header *ih = dp_packet_l4(packet); |
1502 | 0 | ovs_be16 orig_tc = htons(ih->icmp_type << 8 | ih->icmp_code); |
1503 | 0 | ovs_be16 new_tc = htons(type << 8 | code); |
1504 | |
|
1505 | 0 | if (orig_tc != new_tc) { |
1506 | 0 | ih->icmp_type = type; |
1507 | 0 | ih->icmp_code = code; |
1508 | |
|
1509 | 0 | ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_tc, new_tc); |
1510 | 0 | } |
1511 | 0 | pkt_metadata_init_conn(&packet->md); |
1512 | 0 | } |
1513 | | |
1514 | | /* Sets the IGMP type to IGMP_HOST_MEMBERSHIP_QUERY and populates the |
1515 | | * v3 query header fields in 'packet'. 'packet' must be a valid IGMPv3 |
1516 | | * query packet with its l4 offset properly populated. |
1517 | | */ |
1518 | | void |
1519 | | packet_set_igmp3_query(struct dp_packet *packet, uint8_t max_resp, |
1520 | | ovs_be32 group, bool srs, uint8_t qrv, uint8_t qqic) |
1521 | 0 | { |
1522 | 0 | struct igmpv3_query_header *igh = dp_packet_l4(packet); |
1523 | 0 | ovs_be16 orig_type_max_resp = |
1524 | 0 | htons(igh->type << 8 | igh->max_resp); |
1525 | 0 | ovs_be16 new_type_max_resp = |
1526 | 0 | htons(IGMP_HOST_MEMBERSHIP_QUERY << 8 | max_resp); |
1527 | |
|
1528 | 0 | if (orig_type_max_resp != new_type_max_resp) { |
1529 | 0 | igh->type = IGMP_HOST_MEMBERSHIP_QUERY; |
1530 | 0 | igh->max_resp = max_resp; |
1531 | 0 | igh->csum = recalc_csum16(igh->csum, orig_type_max_resp, |
1532 | 0 | new_type_max_resp); |
1533 | 0 | } |
1534 | |
|
1535 | 0 | ovs_be32 old_group = get_16aligned_be32(&igh->group); |
1536 | |
|
1537 | 0 | if (old_group != group) { |
1538 | 0 | put_16aligned_be32(&igh->group, group); |
1539 | 0 | igh->csum = recalc_csum32(igh->csum, old_group, group); |
1540 | 0 | } |
1541 | | |
1542 | | /* See RFC 3376 4.1.6. */ |
1543 | 0 | if (qrv > 7) { |
1544 | 0 | qrv = 0; |
1545 | 0 | } |
1546 | |
|
1547 | 0 | ovs_be16 orig_srs_qrv_qqic = htons(igh->srs_qrv << 8 | igh->qqic); |
1548 | 0 | ovs_be16 new_srs_qrv_qqic = htons(srs << 11 | qrv << 8 | qqic); |
1549 | |
|
1550 | 0 | if (orig_srs_qrv_qqic != new_srs_qrv_qqic) { |
1551 | 0 | igh->srs_qrv = (srs << 3 | qrv); |
1552 | 0 | igh->qqic = qqic; |
1553 | 0 | igh->csum = recalc_csum16(igh->csum, orig_srs_qrv_qqic, |
1554 | 0 | new_srs_qrv_qqic); |
1555 | 0 | } |
1556 | 0 | } |
1557 | | |
1558 | | void |
1559 | | packet_set_nd_ext(struct dp_packet *packet, const ovs_16aligned_be32 rso_flags, |
1560 | | const uint8_t opt_type) |
1561 | 0 | { |
1562 | 0 | struct ovs_nd_msg *ns; |
1563 | 0 | struct ovs_nd_lla_opt *opt; |
1564 | 0 | int bytes_remain = dp_packet_l4_size(packet); |
1565 | 0 | struct ovs_16aligned_ip6_hdr * nh = dp_packet_l3(packet); |
1566 | 0 | uint32_t pseudo_hdr_csum = 0; |
1567 | |
|
1568 | 0 | if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) { |
1569 | 0 | return; |
1570 | 0 | } |
1571 | | |
1572 | 0 | if (nh) { |
1573 | 0 | pseudo_hdr_csum = packet_csum_pseudoheader6(nh); |
1574 | 0 | } |
1575 | |
|
1576 | 0 | ns = dp_packet_l4(packet); |
1577 | 0 | opt = &ns->options[0]; |
1578 | | |
1579 | | /* set RSO flags and option type */ |
1580 | 0 | ns->rso_flags = rso_flags; |
1581 | 0 | opt->type = opt_type; |
1582 | | |
1583 | | /* recalculate checksum */ |
1584 | 0 | ovs_be16 *csum_value = &(ns->icmph.icmp6_cksum); |
1585 | 0 | *csum_value = 0; |
1586 | 0 | *csum_value = csum_finish(csum_continue(pseudo_hdr_csum, |
1587 | 0 | &(ns->icmph), bytes_remain)); |
1588 | |
|
1589 | 0 | } |
1590 | | |
1591 | | void |
1592 | | packet_set_nd(struct dp_packet *packet, const struct in6_addr *target, |
1593 | | const struct eth_addr sll, const struct eth_addr tll) |
1594 | 0 | { |
1595 | 0 | struct ovs_nd_msg *ns; |
1596 | 0 | struct ovs_nd_lla_opt *opt; |
1597 | 0 | int bytes_remain = dp_packet_l4_size(packet); |
1598 | |
|
1599 | 0 | if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) { |
1600 | 0 | return; |
1601 | 0 | } |
1602 | | |
1603 | 0 | ns = dp_packet_l4(packet); |
1604 | 0 | opt = &ns->options[0]; |
1605 | 0 | bytes_remain -= sizeof(*ns); |
1606 | |
|
1607 | 0 | if (memcmp(&ns->target, target, sizeof(ovs_be32[4]))) { |
1608 | 0 | packet_set_ipv6_addr(packet, IPPROTO_ICMPV6, ns->target.be32, target, |
1609 | 0 | true); |
1610 | 0 | } |
1611 | |
|
1612 | 0 | while (bytes_remain >= ND_LLA_OPT_LEN && opt->len != 0) { |
1613 | 0 | if (opt->type == ND_OPT_SOURCE_LINKADDR && opt->len == 1) { |
1614 | 0 | if (!eth_addr_equals(opt->mac, sll)) { |
1615 | 0 | ovs_be16 *csum = &(ns->icmph.icmp6_cksum); |
1616 | |
|
1617 | 0 | *csum = recalc_csum48(*csum, opt->mac, sll); |
1618 | 0 | opt->mac = sll; |
1619 | 0 | } |
1620 | | |
1621 | | /* A packet can only contain one SLL or TLL option */ |
1622 | 0 | break; |
1623 | 0 | } else if (opt->type == ND_OPT_TARGET_LINKADDR && opt->len == 1) { |
1624 | 0 | if (!eth_addr_equals(opt->mac, tll)) { |
1625 | 0 | ovs_be16 *csum = &(ns->icmph.icmp6_cksum); |
1626 | |
|
1627 | 0 | *csum = recalc_csum48(*csum, opt->mac, tll); |
1628 | 0 | opt->mac = tll; |
1629 | 0 | } |
1630 | | |
1631 | | /* A packet can only contain one SLL or TLL option */ |
1632 | 0 | break; |
1633 | 0 | } |
1634 | | |
1635 | 0 | opt += opt->len; |
1636 | 0 | bytes_remain -= opt->len * ND_LLA_OPT_LEN; |
1637 | 0 | } |
1638 | 0 | } |
1639 | | |
1640 | | const char * |
1641 | | packet_tcp_flag_to_string(uint32_t flag) |
1642 | 529k | { |
1643 | 529k | switch (flag) { |
1644 | 39.2k | case TCP_FIN: |
1645 | 39.2k | return "fin"; |
1646 | 44.4k | case TCP_SYN: |
1647 | 44.4k | return "syn"; |
1648 | 37.8k | case TCP_RST: |
1649 | 37.8k | return "rst"; |
1650 | 32.0k | case TCP_PSH: |
1651 | 32.0k | return "psh"; |
1652 | 34.7k | case TCP_ACK: |
1653 | 34.7k | return "ack"; |
1654 | 34.9k | case TCP_URG: |
1655 | 34.9k | return "urg"; |
1656 | 32.7k | case TCP_ECE: |
1657 | 32.7k | return "ece"; |
1658 | 30.6k | case TCP_CWR: |
1659 | 30.6k | return "cwr"; |
1660 | 35.4k | case TCP_NS: |
1661 | 35.4k | return "ns"; |
1662 | 30.4k | case 0x200: |
1663 | 30.4k | return "[200]"; |
1664 | 34.1k | case 0x400: |
1665 | 34.1k | return "[400]"; |
1666 | 32.2k | case 0x800: |
1667 | 32.2k | return "[800]"; |
1668 | 110k | default: |
1669 | 110k | return NULL; |
1670 | 529k | } |
1671 | 529k | } |
1672 | | |
1673 | | /* Appends a string representation of the TCP flags value 'tcp_flags' |
1674 | | * (e.g. from struct flow.tcp_flags or obtained via TCP_FLAGS) to 's', in the |
1675 | | * format used by tcpdump. */ |
1676 | | void |
1677 | | packet_format_tcp_flags(struct ds *s, uint16_t tcp_flags) |
1678 | 0 | { |
1679 | 0 | if (!tcp_flags) { |
1680 | 0 | ds_put_cstr(s, "none"); |
1681 | 0 | return; |
1682 | 0 | } |
1683 | | |
1684 | 0 | if (tcp_flags & TCP_SYN) { |
1685 | 0 | ds_put_char(s, 'S'); |
1686 | 0 | } |
1687 | 0 | if (tcp_flags & TCP_FIN) { |
1688 | 0 | ds_put_char(s, 'F'); |
1689 | 0 | } |
1690 | 0 | if (tcp_flags & TCP_PSH) { |
1691 | 0 | ds_put_char(s, 'P'); |
1692 | 0 | } |
1693 | 0 | if (tcp_flags & TCP_RST) { |
1694 | 0 | ds_put_char(s, 'R'); |
1695 | 0 | } |
1696 | 0 | if (tcp_flags & TCP_URG) { |
1697 | 0 | ds_put_char(s, 'U'); |
1698 | 0 | } |
1699 | 0 | if (tcp_flags & TCP_ACK) { |
1700 | 0 | ds_put_char(s, '.'); |
1701 | 0 | } |
1702 | 0 | if (tcp_flags & TCP_ECE) { |
1703 | 0 | ds_put_cstr(s, "E"); |
1704 | 0 | } |
1705 | 0 | if (tcp_flags & TCP_CWR) { |
1706 | 0 | ds_put_cstr(s, "C"); |
1707 | 0 | } |
1708 | 0 | if (tcp_flags & TCP_NS) { |
1709 | 0 | ds_put_cstr(s, "N"); |
1710 | 0 | } |
1711 | 0 | if (tcp_flags & 0x200) { |
1712 | 0 | ds_put_cstr(s, "[200]"); |
1713 | 0 | } |
1714 | 0 | if (tcp_flags & 0x400) { |
1715 | 0 | ds_put_cstr(s, "[400]"); |
1716 | 0 | } |
1717 | 0 | if (tcp_flags & 0x800) { |
1718 | 0 | ds_put_cstr(s, "[800]"); |
1719 | 0 | } |
1720 | 0 | } |
1721 | | |
1722 | 0 | #define ARP_PACKET_SIZE (2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + \ |
1723 | 0 | ARP_ETH_HEADER_LEN) |
1724 | | |
1725 | | /* Clears 'b' and replaces its contents by an ARP frame with the specified |
1726 | | * 'arp_op', 'arp_sha', 'arp_tha', 'arp_spa', and 'arp_tpa'. The outer |
1727 | | * Ethernet frame is initialized with Ethernet source 'arp_sha' and destination |
1728 | | * 'arp_tha', except that destination ff:ff:ff:ff:ff:ff is used instead if |
1729 | | * 'broadcast' is true. Points the L3 header to the ARP header. */ |
1730 | | void |
1731 | | compose_arp(struct dp_packet *b, uint16_t arp_op, |
1732 | | const struct eth_addr arp_sha, const struct eth_addr arp_tha, |
1733 | | bool broadcast, ovs_be32 arp_spa, ovs_be32 arp_tpa) |
1734 | 0 | { |
1735 | 0 | compose_arp__(b); |
1736 | |
|
1737 | 0 | struct eth_header *eth = dp_packet_eth(b); |
1738 | 0 | eth->eth_dst = broadcast ? eth_addr_broadcast : arp_tha; |
1739 | 0 | eth->eth_src = arp_sha; |
1740 | |
|
1741 | 0 | struct arp_eth_header *arp = dp_packet_l3(b); |
1742 | 0 | arp->ar_op = htons(arp_op); |
1743 | 0 | arp->ar_sha = arp_sha; |
1744 | 0 | arp->ar_tha = arp_tha; |
1745 | 0 | put_16aligned_be32(&arp->ar_spa, arp_spa); |
1746 | 0 | put_16aligned_be32(&arp->ar_tpa, arp_tpa); |
1747 | 0 | } |
1748 | | |
1749 | | /* Clears 'b' and replaces its contents by an ARP frame. Sets the fields in |
1750 | | * the Ethernet and ARP headers that are fixed for ARP frames to those fixed |
1751 | | * values, and zeroes the other fields. Points the L3 header to the ARP |
1752 | | * header. */ |
1753 | | void |
1754 | | compose_arp__(struct dp_packet *b) |
1755 | 0 | { |
1756 | 0 | dp_packet_clear(b); |
1757 | 0 | dp_packet_prealloc_tailroom(b, ARP_PACKET_SIZE); |
1758 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
1759 | |
|
1760 | 0 | struct eth_header *eth = dp_packet_put_zeros(b, sizeof *eth); |
1761 | 0 | eth->eth_type = htons(ETH_TYPE_ARP); |
1762 | |
|
1763 | 0 | struct arp_eth_header *arp = dp_packet_put_zeros(b, sizeof *arp); |
1764 | 0 | arp->ar_hrd = htons(ARP_HRD_ETHERNET); |
1765 | 0 | arp->ar_pro = htons(ARP_PRO_IP); |
1766 | 0 | arp->ar_hln = sizeof arp->ar_sha; |
1767 | 0 | arp->ar_pln = sizeof arp->ar_spa; |
1768 | |
|
1769 | 0 | dp_packet_set_l3(b, arp); |
1770 | |
|
1771 | 0 | b->packet_type = htonl(PT_ETH); |
1772 | 0 | } |
1773 | | |
1774 | | /* This function expects packet with ethernet header with correct |
1775 | | * l3 pointer set. */ |
1776 | | void * |
1777 | | compose_ipv6(struct dp_packet *packet, uint8_t proto, |
1778 | | const struct in6_addr *src, const struct in6_addr *dst, |
1779 | | uint8_t key_tc, ovs_be32 key_fl, uint8_t key_hl, int size) |
1780 | 0 | { |
1781 | 0 | struct ovs_16aligned_ip6_hdr *nh; |
1782 | 0 | void *data; |
1783 | |
|
1784 | 0 | nh = dp_packet_l3(packet); |
1785 | 0 | nh->ip6_vfc = 0x60; |
1786 | 0 | nh->ip6_nxt = proto; |
1787 | 0 | nh->ip6_plen = htons(size); |
1788 | 0 | data = dp_packet_put_zeros(packet, size); |
1789 | 0 | dp_packet_set_l4(packet, data); |
1790 | 0 | packet_set_ipv6(packet, src, dst, key_tc, key_fl, key_hl); |
1791 | 0 | return data; |
1792 | 0 | } |
1793 | | |
1794 | | /* Compose an IPv6 Neighbor Discovery Neighbor Solicitation message. */ |
1795 | | void |
1796 | | compose_nd_ns(struct dp_packet *b, const struct eth_addr eth_src, |
1797 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst) |
1798 | 0 | { |
1799 | 0 | struct in6_addr sn_addr; |
1800 | 0 | struct eth_addr eth_dst; |
1801 | 0 | struct ovs_nd_msg *ns; |
1802 | 0 | struct ovs_nd_lla_opt *lla_opt; |
1803 | 0 | uint32_t icmp_csum; |
1804 | |
|
1805 | 0 | in6_addr_solicited_node(&sn_addr, ipv6_dst); |
1806 | 0 | ipv6_multicast_to_ethernet(ð_dst, &sn_addr); |
1807 | |
|
1808 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1809 | 0 | ns = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, &sn_addr, |
1810 | 0 | 0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN); |
1811 | |
|
1812 | 0 | ns->icmph.icmp6_type = ND_NEIGHBOR_SOLICIT; |
1813 | 0 | ns->icmph.icmp6_code = 0; |
1814 | 0 | put_16aligned_be32(&ns->rso_flags, htonl(0)); |
1815 | |
|
1816 | 0 | lla_opt = &ns->options[0]; |
1817 | 0 | lla_opt->type = ND_OPT_SOURCE_LINKADDR; |
1818 | 0 | lla_opt->len = 1; |
1819 | |
|
1820 | 0 | packet_set_nd(b, ipv6_dst, eth_src, eth_addr_zero); |
1821 | |
|
1822 | 0 | ns->icmph.icmp6_cksum = 0; |
1823 | 0 | icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1824 | 0 | ns->icmph.icmp6_cksum = csum_finish( |
1825 | 0 | csum_continue(icmp_csum, ns, ND_MSG_LEN + ND_LLA_OPT_LEN)); |
1826 | 0 | } |
1827 | | |
1828 | | /* Compose an IPv6 Neighbor Discovery Neighbor Advertisement message. */ |
1829 | | void |
1830 | | compose_nd_na(struct dp_packet *b, |
1831 | | const struct eth_addr eth_src, const struct eth_addr eth_dst, |
1832 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst, |
1833 | | ovs_be32 rso_flags) |
1834 | 0 | { |
1835 | 0 | struct ovs_nd_msg *na; |
1836 | 0 | struct ovs_nd_lla_opt *lla_opt; |
1837 | 0 | uint32_t icmp_csum; |
1838 | |
|
1839 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1840 | 0 | na = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, |
1841 | 0 | 0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN); |
1842 | |
|
1843 | 0 | na->icmph.icmp6_type = ND_NEIGHBOR_ADVERT; |
1844 | 0 | na->icmph.icmp6_code = 0; |
1845 | 0 | put_16aligned_be32(&na->rso_flags, rso_flags); |
1846 | |
|
1847 | 0 | lla_opt = &na->options[0]; |
1848 | 0 | lla_opt->type = ND_OPT_TARGET_LINKADDR; |
1849 | 0 | lla_opt->len = 1; |
1850 | |
|
1851 | 0 | packet_set_nd(b, ipv6_src, eth_addr_zero, eth_src); |
1852 | |
|
1853 | 0 | na->icmph.icmp6_cksum = 0; |
1854 | 0 | icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1855 | 0 | na->icmph.icmp6_cksum = csum_finish(csum_continue( |
1856 | 0 | icmp_csum, na, ND_MSG_LEN + ND_LLA_OPT_LEN)); |
1857 | 0 | } |
1858 | | |
1859 | | /* Compose an IPv6 Neighbor Discovery Router Advertisement message with |
1860 | | * Source Link-layer Address Option and MTU Option. |
1861 | | * Caller can call packet_put_ra_prefix_opt to append Prefix Information |
1862 | | * Options to composed messags in 'b'. */ |
1863 | | void |
1864 | | compose_nd_ra(struct dp_packet *b, |
1865 | | const struct eth_addr eth_src, const struct eth_addr eth_dst, |
1866 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst, |
1867 | | uint8_t cur_hop_limit, uint8_t mo_flags, |
1868 | | ovs_be16 router_lt, ovs_be32 reachable_time, |
1869 | | ovs_be32 retrans_timer, uint32_t mtu) |
1870 | 0 | { |
1871 | | /* Don't compose Router Advertisement packet with MTU Option if mtu |
1872 | | * value is 0. */ |
1873 | 0 | bool with_mtu = mtu != 0; |
1874 | 0 | size_t mtu_opt_len = with_mtu ? ND_MTU_OPT_LEN : 0; |
1875 | |
|
1876 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1877 | |
|
1878 | 0 | struct ovs_ra_msg *ra = compose_ipv6( |
1879 | 0 | b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, 0, 0, 255, |
1880 | 0 | RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len); |
1881 | 0 | ra->icmph.icmp6_type = ND_ROUTER_ADVERT; |
1882 | 0 | ra->icmph.icmp6_code = 0; |
1883 | 0 | ra->cur_hop_limit = cur_hop_limit; |
1884 | 0 | ra->mo_flags = mo_flags; |
1885 | 0 | ra->router_lifetime = router_lt; |
1886 | 0 | ra->reachable_time = reachable_time; |
1887 | 0 | ra->retrans_timer = retrans_timer; |
1888 | |
|
1889 | 0 | struct ovs_nd_lla_opt *lla_opt = ra->options; |
1890 | 0 | lla_opt->type = ND_OPT_SOURCE_LINKADDR; |
1891 | 0 | lla_opt->len = 1; |
1892 | 0 | lla_opt->mac = eth_src; |
1893 | |
|
1894 | 0 | if (with_mtu) { |
1895 | | /* ovs_nd_mtu_opt has the same size with ovs_nd_lla_opt. */ |
1896 | 0 | struct ovs_nd_mtu_opt *mtu_opt |
1897 | 0 | = (struct ovs_nd_mtu_opt *)(lla_opt + 1); |
1898 | 0 | mtu_opt->type = ND_OPT_MTU; |
1899 | 0 | mtu_opt->len = 1; |
1900 | 0 | mtu_opt->reserved = 0; |
1901 | 0 | put_16aligned_be32(&mtu_opt->mtu, htonl(mtu)); |
1902 | 0 | } |
1903 | |
|
1904 | 0 | ra->icmph.icmp6_cksum = 0; |
1905 | 0 | uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1906 | 0 | ra->icmph.icmp6_cksum = csum_finish(csum_continue( |
1907 | 0 | icmp_csum, ra, RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len)); |
1908 | 0 | } |
1909 | | |
1910 | | /* Append an IPv6 Neighbor Discovery Prefix Information option to a |
1911 | | * Router Advertisement message. */ |
1912 | | void |
1913 | | packet_put_ra_prefix_opt(struct dp_packet *b, |
1914 | | uint8_t plen, uint8_t la_flags, |
1915 | | ovs_be32 valid_lifetime, ovs_be32 preferred_lifetime, |
1916 | | const ovs_be128 prefix) |
1917 | 0 | { |
1918 | 0 | size_t prev_l4_size = dp_packet_l4_size(b); |
1919 | 0 | struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(b); |
1920 | 0 | nh->ip6_plen = htons(prev_l4_size + ND_PREFIX_OPT_LEN); |
1921 | |
|
1922 | 0 | struct ovs_nd_prefix_opt *prefix_opt = |
1923 | 0 | dp_packet_put_uninit(b, sizeof *prefix_opt); |
1924 | 0 | prefix_opt->type = ND_OPT_PREFIX_INFORMATION; |
1925 | 0 | prefix_opt->len = 4; |
1926 | 0 | prefix_opt->prefix_len = plen; |
1927 | 0 | prefix_opt->la_flags = la_flags; |
1928 | 0 | put_16aligned_be32(&prefix_opt->valid_lifetime, valid_lifetime); |
1929 | 0 | put_16aligned_be32(&prefix_opt->preferred_lifetime, preferred_lifetime); |
1930 | 0 | put_16aligned_be32(&prefix_opt->reserved, 0); |
1931 | 0 | memcpy(prefix_opt->prefix.be32, prefix.be32, sizeof(ovs_be32[4])); |
1932 | |
|
1933 | 0 | struct ovs_ra_msg *ra = dp_packet_l4(b); |
1934 | 0 | ra->icmph.icmp6_cksum = 0; |
1935 | 0 | uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1936 | 0 | ra->icmph.icmp6_cksum = csum_finish(csum_continue( |
1937 | 0 | icmp_csum, ra, prev_l4_size + ND_PREFIX_OPT_LEN)); |
1938 | 0 | } |
1939 | | |
1940 | | uint32_t |
1941 | | packet_csum_pseudoheader(const struct ip_header *ip) |
1942 | 0 | { |
1943 | 0 | uint32_t partial = 0; |
1944 | |
|
1945 | 0 | partial = csum_add32(partial, get_16aligned_be32(&ip->ip_src)); |
1946 | 0 | partial = csum_add32(partial, get_16aligned_be32(&ip->ip_dst)); |
1947 | 0 | partial = csum_add16(partial, htons(ip->ip_proto)); |
1948 | 0 | partial = csum_add16(partial, htons(ntohs(ip->ip_tot_len) - |
1949 | 0 | IP_IHL(ip->ip_ihl_ver) * 4)); |
1950 | |
|
1951 | 0 | return partial; |
1952 | 0 | } |
1953 | | |
1954 | | #ifndef __CHECKER__ |
1955 | | uint32_t |
1956 | | packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *ip6) |
1957 | 0 | { |
1958 | 0 | uint32_t partial = 0; |
1959 | |
|
1960 | 0 | partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src); |
1961 | 0 | partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst); |
1962 | 0 | partial = csum_add16(partial, htons(ip6->ip6_nxt)); |
1963 | 0 | partial = csum_add16(partial, ip6->ip6_plen); |
1964 | |
|
1965 | 0 | return partial; |
1966 | 0 | } |
1967 | | |
1968 | | /* Calculate the IPv6 upper layer checksum according to RFC2460. We pass the |
1969 | | ip6_nxt and ip6_plen values, so it will also work if extension headers |
1970 | | are present. */ |
1971 | | ovs_be16 |
1972 | | packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *ip6, |
1973 | | const void *data, uint8_t l4_protocol, |
1974 | | uint16_t l4_size) |
1975 | 0 | { |
1976 | 0 | uint32_t partial = 0; |
1977 | |
|
1978 | 0 | partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src); |
1979 | 0 | partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst); |
1980 | 0 | partial = csum_add16(partial, htons(l4_protocol)); |
1981 | 0 | partial = csum_add16(partial, htons(l4_size)); |
1982 | |
|
1983 | 0 | partial = csum_continue(partial, data, l4_size); |
1984 | |
|
1985 | 0 | return csum_finish(partial); |
1986 | 0 | } |
1987 | | #endif |
1988 | | |
1989 | | void |
1990 | | IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6) |
1991 | 0 | { |
1992 | 0 | if (is_ipv6) { |
1993 | 0 | ovs_16aligned_be32 *ip6 = dp_packet_l3(pkt); |
1994 | |
|
1995 | 0 | put_16aligned_be32(ip6, get_16aligned_be32(ip6) | |
1996 | 0 | htonl(IP_ECN_CE << 20)); |
1997 | 0 | } else { |
1998 | 0 | struct ip_header *nh = dp_packet_l3(pkt); |
1999 | 0 | uint8_t tos = nh->ip_tos; |
2000 | |
|
2001 | 0 | tos |= IP_ECN_CE; |
2002 | 0 | if (nh->ip_tos != tos) { |
2003 | 0 | if (dp_packet_ip_checksum_valid(pkt)) { |
2004 | 0 | dp_packet_ip_checksum_set_partial(pkt); |
2005 | 0 | } else { |
2006 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_tos), |
2007 | 0 | htons((uint16_t) tos)); |
2008 | 0 | } |
2009 | |
|
2010 | 0 | nh->ip_tos = tos; |
2011 | 0 | } |
2012 | 0 | } |
2013 | 0 | } |
2014 | | |
2015 | | /* Set TCP checksum field in packet 'p' with complete checksum. |
2016 | | * The packet must have the L3 and L4 offsets. */ |
2017 | | void |
2018 | | packet_tcp_complete_csum(struct dp_packet *p, bool inner) |
2019 | 0 | { |
2020 | 0 | struct tcp_header *tcp; |
2021 | 0 | size_t tcp_sz; |
2022 | 0 | void *ip_hdr; |
2023 | |
|
2024 | 0 | if (inner) { |
2025 | 0 | tcp = dp_packet_inner_l4(p); |
2026 | 0 | ip_hdr = dp_packet_inner_l3(p); |
2027 | 0 | tcp_sz = dp_packet_inner_l4_size(p); |
2028 | 0 | } else { |
2029 | 0 | tcp = dp_packet_l4(p); |
2030 | 0 | ip_hdr = dp_packet_l3(p); |
2031 | 0 | tcp_sz = dp_packet_l4_size(p); |
2032 | 0 | } |
2033 | |
|
2034 | 0 | ovs_assert(tcp); |
2035 | 0 | ovs_assert(ip_hdr); |
2036 | |
|
2037 | 0 | tcp->tcp_csum = 0; |
2038 | 0 | if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) { |
2039 | 0 | struct ip_header *ip = ip_hdr; |
2040 | |
|
2041 | 0 | tcp->tcp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip), |
2042 | 0 | tcp, tcp_sz)); |
2043 | 0 | } else { |
2044 | 0 | struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr; |
2045 | |
|
2046 | 0 | tcp->tcp_csum = packet_csum_upperlayer6(ip6, tcp, ip6->ip6_nxt, |
2047 | 0 | tcp_sz); |
2048 | 0 | } |
2049 | |
|
2050 | 0 | if (inner) { |
2051 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2052 | 0 | } else { |
2053 | 0 | dp_packet_l4_checksum_set_good(p); |
2054 | 0 | } |
2055 | 0 | } |
2056 | | |
2057 | | /* Set UDP checksum field in packet 'p' with complete checksum. |
2058 | | * The packet must have the L3 and L4 offsets. */ |
2059 | | void |
2060 | | packet_udp_complete_csum(struct dp_packet *p, bool inner) |
2061 | 0 | { |
2062 | 0 | struct udp_header *udp; |
2063 | 0 | size_t udp_sz; |
2064 | 0 | void *ip_hdr; |
2065 | |
|
2066 | 0 | if (inner) { |
2067 | 0 | udp = dp_packet_inner_l4(p); |
2068 | 0 | ip_hdr = dp_packet_inner_l3(p); |
2069 | 0 | udp_sz = dp_packet_inner_l4_size(p); |
2070 | 0 | } else { |
2071 | 0 | udp = dp_packet_l4(p); |
2072 | 0 | ip_hdr = dp_packet_l3(p); |
2073 | 0 | udp_sz = dp_packet_l4_size(p); |
2074 | 0 | } |
2075 | |
|
2076 | 0 | ovs_assert(udp); |
2077 | 0 | ovs_assert(ip_hdr); |
2078 | | |
2079 | | /* Skip csum calculation if the udp_csum is zero. */ |
2080 | 0 | if (!udp->udp_csum) { |
2081 | 0 | goto out; |
2082 | 0 | } |
2083 | | |
2084 | 0 | udp->udp_csum = 0; |
2085 | 0 | if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) { |
2086 | 0 | struct ip_header *ip = ip_hdr; |
2087 | |
|
2088 | 0 | udp->udp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip), |
2089 | 0 | udp, udp_sz)); |
2090 | 0 | } else { |
2091 | 0 | struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr; |
2092 | |
|
2093 | 0 | udp->udp_csum = packet_csum_upperlayer6(ip6, udp, ip6->ip6_nxt, |
2094 | 0 | udp_sz); |
2095 | 0 | } |
2096 | |
|
2097 | 0 | if (!udp->udp_csum) { |
2098 | 0 | udp->udp_csum = htons(0xffff); |
2099 | 0 | } |
2100 | |
|
2101 | 0 | out: |
2102 | 0 | if (inner) { |
2103 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2104 | 0 | } else { |
2105 | 0 | dp_packet_l4_checksum_set_good(p); |
2106 | 0 | } |
2107 | 0 | } |
2108 | | |
2109 | | /* This helper computes a "constant" UDP checksum without looking at the |
2110 | | * L4 payload. |
2111 | | * |
2112 | | * This is possible when L4 is either TCP or UDP: the L4 payload checksum |
2113 | | * is either computed in SW or in HW later, but its contribution to the |
2114 | | * outer checksum is cancelled by the L4 payload being part of the global |
2115 | | * packet sum. */ |
2116 | | bool |
2117 | | packet_udp_tunnel_csum(struct dp_packet *p) |
2118 | 0 | { |
2119 | 0 | struct ip_header *inner_ip; |
2120 | 0 | const void *inner_l4_data; |
2121 | 0 | char *after_inner_l4_csum; |
2122 | 0 | size_t inner_l4_csum_off; |
2123 | 0 | struct udp_header *udp; |
2124 | 0 | ovs_be16 inner_l4_csum; |
2125 | 0 | uint32_t partial_csum; |
2126 | 0 | struct ip_header *ip; |
2127 | 0 | uint32_t inner_csum; |
2128 | 0 | uint16_t tso_segsz; |
2129 | 0 | bool inner_ipv4; |
2130 | 0 | void *inner_l4; |
2131 | |
|
2132 | 0 | inner_ip = dp_packet_inner_l3(p); |
2133 | 0 | inner_l4 = dp_packet_inner_l4(p); |
2134 | 0 | ip = dp_packet_l3(p); |
2135 | 0 | udp = dp_packet_l4(p); |
2136 | |
|
2137 | 0 | if (dp_packet_inner_l4_proto_tcp(p)) { |
2138 | 0 | inner_l4_csum_off = offsetof(struct tcp_header, tcp_csum); |
2139 | 0 | inner_l4_data = dp_packet_get_inner_tcp_payload(p); |
2140 | 0 | if (!inner_l4_data) { |
2141 | | /* Malformed packet. */ |
2142 | 0 | return false; |
2143 | 0 | } |
2144 | 0 | } else if (dp_packet_inner_l4_proto_udp(p)) { |
2145 | 0 | inner_l4_csum_off = offsetof(struct udp_header, udp_csum); |
2146 | 0 | inner_l4_data = (char *) inner_l4 + sizeof (struct udp_header); |
2147 | 0 | if (((struct udp_header *) inner_l4)->udp_csum == 0) { |
2148 | | /* There is no nested checksum. |
2149 | | * No choice but compute a full checksum. */ |
2150 | 0 | return false; |
2151 | 0 | } |
2152 | 0 | } else { |
2153 | | /* This optimisation applies only to inner TCP/UDP. */ |
2154 | 0 | return false; |
2155 | 0 | } |
2156 | | |
2157 | 0 | if (!dp_packet_inner_l4_checksum_valid(p)) { |
2158 | | /* We have no idea about the contribution of the payload data |
2159 | | * and what the L4 checksum put in the packet data looks like. |
2160 | | * Simpler is to let a full checksum happen. */ |
2161 | 0 | return false; |
2162 | 0 | } |
2163 | | |
2164 | 0 | inner_ipv4 = IP_VER(inner_ip->ip_ihl_ver) == 4; |
2165 | 0 | if (inner_ipv4) { |
2166 | 0 | inner_csum = packet_csum_pseudoheader(inner_ip); |
2167 | 0 | } else { |
2168 | 0 | struct ovs_16aligned_ip6_hdr *inner_ip6 = dp_packet_inner_l3(p); |
2169 | |
|
2170 | 0 | inner_csum = packet_csum_pseudoheader6(inner_ip6); |
2171 | 0 | } |
2172 | |
|
2173 | 0 | inner_csum = csum_continue(inner_csum, inner_l4, inner_l4_csum_off); |
2174 | 0 | after_inner_l4_csum = (char *) inner_l4 + inner_l4_csum_off + 2; |
2175 | 0 | inner_l4_csum = csum_finish(csum_continue(inner_csum, after_inner_l4_csum, |
2176 | 0 | (char *) inner_l4_data - after_inner_l4_csum)); |
2177 | | /* Important: for inner UDP, a null inner_l4_csum here should in theory be |
2178 | | * replaced with 0xffff. However, since the only use of inner_l4_csum is |
2179 | | * for the final outer checksum with a csum_add16() below, we can skip this |
2180 | | * entirely because adding 0xffff will have the same effect as adding 0x0 |
2181 | | * after reducing in csum_finish. */ |
2182 | |
|
2183 | 0 | udp->udp_csum = 0; |
2184 | 0 | if (IP_VER(ip->ip_ihl_ver) == 4) { |
2185 | 0 | partial_csum = packet_csum_pseudoheader(ip); |
2186 | 0 | } else { |
2187 | 0 | struct ovs_16aligned_ip6_hdr *ip6 = dp_packet_l3(p); |
2188 | |
|
2189 | 0 | partial_csum = packet_csum_pseudoheader6(ip6); |
2190 | 0 | } |
2191 | |
|
2192 | 0 | partial_csum = csum_continue(partial_csum, udp, |
2193 | 0 | (char *) inner_ip - (char *) udp); |
2194 | 0 | if (!inner_ipv4 || !dp_packet_inner_ip_checksum_valid(p)) { |
2195 | | /* IPv6 has no checksum, so for inner IPv6, we need to sum the header. |
2196 | | * |
2197 | | * In IPv4 case, if inner checksum is already good or HW offload |
2198 | | * has been requested, the (final) sum of the IPv4 header will be 0. |
2199 | | * Otherwise, we need to sum the header like for IPv6. */ |
2200 | 0 | partial_csum = csum_continue(partial_csum, inner_ip, |
2201 | 0 | (char *) inner_l4 - (char *) inner_ip); |
2202 | 0 | } |
2203 | 0 | partial_csum = csum_continue(partial_csum, inner_l4, inner_l4_csum_off); |
2204 | 0 | partial_csum = csum_add16(partial_csum, inner_l4_csum); |
2205 | 0 | partial_csum = csum_continue(partial_csum, after_inner_l4_csum, |
2206 | 0 | (char *) inner_l4_data - after_inner_l4_csum); |
2207 | 0 | udp->udp_csum = csum_finish(partial_csum); |
2208 | 0 | tso_segsz = dp_packet_get_tso_segsz(p); |
2209 | 0 | if (tso_segsz) { |
2210 | 0 | uint16_t payload_len = dp_packet_get_inner_tcp_payload_length(p); |
2211 | |
|
2212 | 0 | ovs_assert(payload_len == tso_segsz * dp_packet_gso_nr_segs(p)); |
2213 | | |
2214 | | /* The pseudo header used in the outer UDP checksum is dependent on |
2215 | | * the ip_tot_len / ip6_plen which was a reflection of the TSO frame |
2216 | | * size. The segmented packet will be shorter. */ |
2217 | 0 | udp->udp_csum = recalc_csum16(udp->udp_csum, htons(payload_len), |
2218 | 0 | htons(tso_segsz)); |
2219 | | |
2220 | | /* When segmenting the packet, various headers get updated: |
2221 | | * - inner L3 |
2222 | | * - for IPv4, ip_tot_len is updated, BUT it is not affecting the |
2223 | | * outer UDP checksum because the IPv4 header itself contains |
2224 | | * a checksum that compensates for this update, |
2225 | | * - for IPv6, ip6_plen is updated, and this must be considered, |
2226 | | * - inner L4 |
2227 | | * - inner pseudo header used in the TCP checksum is dependent on |
2228 | | * the inner ip_tot_len / ip6_plen, |
2229 | | * - TCP seq number is updated, |
2230 | | * - the HW may change some TCP flags (think PSH/FIN), |
2231 | | * BUT the TCP checksum will compensate for those updates, |
2232 | | * |
2233 | | * Summary: we only care about the inner IPv6 header update. |
2234 | | */ |
2235 | 0 | if (IP_VER(inner_ip->ip_ihl_ver) != 4) { |
2236 | 0 | udp->udp_csum = recalc_csum16(udp->udp_csum, htons(payload_len), |
2237 | 0 | htons(tso_segsz)); |
2238 | 0 | } |
2239 | 0 | } |
2240 | 0 | if (!udp->udp_csum) { |
2241 | 0 | udp->udp_csum = htons(0xffff); |
2242 | 0 | } |
2243 | 0 | dp_packet_l4_checksum_set_good(p); |
2244 | |
|
2245 | 0 | return true; |
2246 | 0 | } |
2247 | | |
2248 | | /* Set SCTP checksum field in packet 'p' with complete checksum. |
2249 | | * The packet must have the L3 and L4 offsets. */ |
2250 | | void |
2251 | | packet_sctp_complete_csum(struct dp_packet *p, bool inner) |
2252 | 0 | { |
2253 | 0 | struct sctp_header *sh; |
2254 | 0 | uint16_t tp_len; |
2255 | 0 | ovs_be32 csum; |
2256 | |
|
2257 | 0 | if (inner) { |
2258 | 0 | sh = dp_packet_inner_l4(p); |
2259 | 0 | tp_len = dp_packet_inner_l4_size(p); |
2260 | 0 | } else { |
2261 | 0 | sh = dp_packet_l4(p); |
2262 | 0 | tp_len = dp_packet_l4_size(p); |
2263 | 0 | } |
2264 | |
|
2265 | 0 | ovs_assert(sh); |
2266 | |
|
2267 | 0 | put_16aligned_be32(&sh->sctp_csum, 0); |
2268 | 0 | csum = crc32c((void *) sh, tp_len); |
2269 | 0 | put_16aligned_be32(&sh->sctp_csum, csum); |
2270 | |
|
2271 | 0 | if (inner) { |
2272 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2273 | 0 | } else { |
2274 | 0 | dp_packet_l4_checksum_set_good(p); |
2275 | 0 | } |
2276 | 0 | } |