Process RARP packets with ethertype 0x8035 similar to ARP packets.
[openvswitch] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 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
19 #include "nx-match.h"
20
21 #include <netinet/icmp6.h>
22
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "ofp-actions.h"
27 #include "ofp-errors.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "openflow/nicira-ext.h"
31 #include "packets.h"
32 #include "unaligned.h"
33 #include "util.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(nx_match);
37
38 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
39  * peer and so there's not much point in showing a lot of them. */
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41
42 /* Returns the width of the data for a field with the given 'header', in
43  * bytes. */
44 int
45 nxm_field_bytes(uint32_t header)
46 {
47     unsigned int length = NXM_LENGTH(header);
48     return NXM_HASMASK(header) ? length / 2 : length;
49 }
50
51 /* Returns the width of the data for a field with the given 'header', in
52  * bits. */
53 int
54 nxm_field_bits(uint32_t header)
55 {
56     return nxm_field_bytes(header) * 8;
57 }
58 \f
59 /* nx_pull_match() and helpers. */
60
61 static uint32_t
62 nx_entry_ok(const void *p, unsigned int match_len)
63 {
64     unsigned int payload_len;
65     ovs_be32 header_be;
66     uint32_t header;
67
68     if (match_len < 4) {
69         if (match_len) {
70             VLOG_DBG_RL(&rl, "nx_match ends with partial (%u-byte) nxm_header",
71                         match_len);
72         }
73         return 0;
74     }
75     memcpy(&header_be, p, 4);
76     header = ntohl(header_be);
77
78     payload_len = NXM_LENGTH(header);
79     if (!payload_len) {
80         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
81                     "length 0", header);
82         return 0;
83     }
84     if (match_len < payload_len + 4) {
85         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
86                     "%u bytes left in nx_match", payload_len + 4, match_len);
87         return 0;
88     }
89
90     return header;
91 }
92
93 static enum ofperr
94 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
95             struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
96 {
97     uint32_t header;
98
99     assert((cookie != NULL) == (cookie_mask != NULL));
100
101     match_init_catchall(match);
102     if (cookie) {
103         *cookie = *cookie_mask = htonll(0);
104     }
105     if (!match_len) {
106         return 0;
107     }
108
109     for (;
110          (header = nx_entry_ok(p, match_len)) != 0;
111          p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
112         const struct mf_field *mf;
113         enum ofperr error;
114
115         mf = mf_from_nxm_header(header);
116         if (!mf) {
117             if (strict) {
118                 error = OFPERR_OFPBMC_BAD_FIELD;
119             } else {
120                 continue;
121             }
122         } else if (!mf_are_prereqs_ok(mf, &match->flow)) {
123             error = OFPERR_OFPBMC_BAD_PREREQ;
124         } else if (!mf_is_all_wild(mf, &match->wc)) {
125             error = OFPERR_OFPBMC_DUP_FIELD;
126         } else if (header != OXM_OF_IN_PORT) {
127             unsigned int width = mf->n_bytes;
128             union mf_value value;
129
130             memcpy(&value, p + 4, width);
131             if (!mf_is_value_valid(mf, &value)) {
132                 error = OFPERR_OFPBMC_BAD_VALUE;
133             } else if (!NXM_HASMASK(header)) {
134                 error = 0;
135                 mf_set_value(mf, &value, match);
136             } else {
137                 union mf_value mask;
138
139                 memcpy(&mask, p + 4 + width, width);
140                 if (!mf_is_mask_valid(mf, &mask)) {
141                     error = OFPERR_OFPBMC_BAD_MASK;
142                 } else {
143                     error = 0;
144                     mf_set(mf, &value, &mask, match);
145                 }
146             }
147         } else {
148             /* Special case for 32bit ports when using OXM,
149              * ports are 16 bits wide otherwise. */
150             ovs_be32 port_of11;
151             uint16_t port;
152
153             memcpy(&port_of11, p + 4, sizeof port_of11);
154             error = ofputil_port_from_ofp11(port_of11, &port);
155             if (!error) {
156                 match_set_in_port(match, port);
157             }
158         }
159
160         /* Check if the match is for a cookie rather than a classifier rule. */
161         if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
162             if (*cookie_mask) {
163                 error = OFPERR_OFPBMC_DUP_FIELD;
164             } else {
165                 unsigned int width = sizeof *cookie;
166
167                 memcpy(cookie, p + 4, width);
168                 if (NXM_HASMASK(header)) {
169                     memcpy(cookie_mask, p + 4 + width, width);
170                 } else {
171                     *cookie_mask = htonll(UINT64_MAX);
172                 }
173                 error = 0;
174             }
175         }
176
177         if (error) {
178             VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
179                         "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
180                         "(%s)", header,
181                         NXM_VENDOR(header), NXM_FIELD(header),
182                         NXM_HASMASK(header), NXM_LENGTH(header),
183                         ofperr_to_string(error));
184             return error;
185         }
186     }
187
188     return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
189 }
190
191 static enum ofperr
192 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
193                 struct match *match,
194                 ovs_be64 *cookie, ovs_be64 *cookie_mask)
195 {
196     uint8_t *p = NULL;
197
198     if (match_len) {
199         p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
200         if (!p) {
201             VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
202                         "multiple of 8, is longer than space in message (max "
203                         "length %zu)", match_len, b->size);
204             return OFPERR_OFPBMC_BAD_LEN;
205         }
206     }
207
208     return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
209 }
210
211 /* Parses the nx_match formatted match description in 'b' with length
212  * 'match_len'.  Stores the results in 'match'.  If 'cookie' and 'cookie_mask'
213  * are valid pointers, then stores the cookie and mask in them if 'b' contains
214  * a "NXM_NX_COOKIE*" match.  Otherwise, stores 0 in both.
215  *
216  * Fails with an error upon encountering an unknown NXM header.
217  *
218  * Returns 0 if successful, otherwise an OpenFlow error code. */
219 enum ofperr
220 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
221               ovs_be64 *cookie, ovs_be64 *cookie_mask)
222 {
223     return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
224 }
225
226 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
227  * instead of failing with an error. */
228 enum ofperr
229 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
230                     struct match *match,
231                     ovs_be64 *cookie, ovs_be64 *cookie_mask)
232 {
233     return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
234 }
235
236 static enum ofperr
237 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
238 {
239     struct ofp11_match_header *omh = b->data;
240     uint8_t *p;
241     uint16_t match_len;
242
243     if (b->size < sizeof *omh) {
244         return OFPERR_OFPBMC_BAD_LEN;
245     }
246
247     match_len = ntohs(omh->length);
248     if (match_len < sizeof *omh) {
249         return OFPERR_OFPBMC_BAD_LEN;
250     }
251
252     if (omh->type != htons(OFPMT_OXM)) {
253         return OFPERR_OFPBMC_BAD_TYPE;
254     }
255
256     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
257     if (!p) {
258         VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
259                     "multiple of 8, is longer than space in message (max "
260                     "length %zu)", match_len, b->size);
261         return OFPERR_OFPBMC_BAD_LEN;
262     }
263
264     return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
265                        strict, match, NULL, NULL);
266 }
267
268 /* Parses the oxm formatted match description preceeded by a struct ofp11_match
269  * in 'b' with length 'match_len'.  Stores the result in 'match'.
270  *
271  * Fails with an error when encountering unknown OXM headers.
272  *
273  * Returns 0 if successful, otherwise an OpenFlow error code. */
274 enum ofperr
275 oxm_pull_match(struct ofpbuf *b, struct match *match)
276 {
277     return oxm_pull_match__(b, true, match);
278 }
279
280 /* Behaves the same as oxm_pull_match() with one exception.  Skips over unknown
281  * PXM headers instead of failing with an error when they are encountered. */
282 enum ofperr
283 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
284 {
285     return oxm_pull_match__(b, false, match);
286 }
287 \f
288 /* nx_put_match() and helpers.
289  *
290  * 'put' functions whose names end in 'w' add a wildcarded field.
291  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
292  * Other 'put' functions add exact-match fields.
293  */
294
295 static void
296 nxm_put_header(struct ofpbuf *b, uint32_t header)
297 {
298     ovs_be32 n_header = htonl(header);
299     ofpbuf_put(b, &n_header, sizeof n_header);
300 }
301
302 static void
303 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
304 {
305     nxm_put_header(b, header);
306     ofpbuf_put(b, &value, sizeof value);
307 }
308
309 static void
310 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
311 {
312     switch (mask) {
313     case 0:
314         break;
315
316     case UINT8_MAX:
317         nxm_put_8(b, header, value);
318         break;
319
320     default:
321         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
322         ofpbuf_put(b, &value, sizeof value);
323         ofpbuf_put(b, &mask, sizeof mask);
324     }
325 }
326
327 static void
328 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
329 {
330     nxm_put_header(b, header);
331     ofpbuf_put(b, &value, sizeof value);
332 }
333
334 static void
335 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
336 {
337     nxm_put_header(b, header);
338     ofpbuf_put(b, &value, sizeof value);
339     ofpbuf_put(b, &mask, sizeof mask);
340 }
341
342 static void
343 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
344 {
345     switch (mask) {
346     case 0:
347         break;
348
349     case CONSTANT_HTONS(UINT16_MAX):
350         nxm_put_16(b, header, value);
351         break;
352
353     default:
354         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
355         break;
356     }
357 }
358
359 static void
360 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
361 {
362     nxm_put_header(b, header);
363     ofpbuf_put(b, &value, sizeof value);
364 }
365
366 static void
367 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
368 {
369     nxm_put_header(b, header);
370     ofpbuf_put(b, &value, sizeof value);
371     ofpbuf_put(b, &mask, sizeof mask);
372 }
373
374 static void
375 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
376 {
377     switch (mask) {
378     case 0:
379         break;
380
381     case CONSTANT_HTONL(UINT32_MAX):
382         nxm_put_32(b, header, value);
383         break;
384
385     default:
386         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
387         break;
388     }
389 }
390
391 static void
392 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
393 {
394     nxm_put_header(b, header);
395     ofpbuf_put(b, &value, sizeof value);
396 }
397
398 static void
399 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
400 {
401     nxm_put_header(b, header);
402     ofpbuf_put(b, &value, sizeof value);
403     ofpbuf_put(b, &mask, sizeof mask);
404 }
405
406 static void
407 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
408 {
409     switch (mask) {
410     case 0:
411         break;
412
413     case CONSTANT_HTONLL(UINT64_MAX):
414         nxm_put_64(b, header, value);
415         break;
416
417     default:
418         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
419         break;
420     }
421 }
422
423 static void
424 nxm_put_eth(struct ofpbuf *b, uint32_t header,
425             const uint8_t value[ETH_ADDR_LEN])
426 {
427     nxm_put_header(b, header);
428     ofpbuf_put(b, value, ETH_ADDR_LEN);
429 }
430
431 static void
432 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
433                    const uint8_t value[ETH_ADDR_LEN],
434                    const uint8_t mask[ETH_ADDR_LEN])
435 {
436     if (!eth_addr_is_zero(mask)) {
437         if (eth_mask_is_exact(mask)) {
438             nxm_put_eth(b, header, value);
439         } else {
440             nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
441             ofpbuf_put(b, value, ETH_ADDR_LEN);
442             ofpbuf_put(b, mask, ETH_ADDR_LEN);
443         }
444     }
445 }
446
447 static void
448 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
449              const struct in6_addr *value, const struct in6_addr *mask)
450 {
451     if (ipv6_mask_is_any(mask)) {
452         return;
453     } else if (ipv6_mask_is_exact(mask)) {
454         nxm_put_header(b, header);
455         ofpbuf_put(b, value, sizeof *value);
456     } else {
457         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
458         ofpbuf_put(b, value, sizeof *value);
459         ofpbuf_put(b, mask, sizeof *mask);
460     }
461 }
462
463 static void
464 nxm_put_frag(struct ofpbuf *b, const struct match *match)
465 {
466     uint8_t nw_frag = match->flow.nw_frag;
467     uint8_t nw_frag_mask = match->wc.masks.nw_frag;
468
469     switch (nw_frag_mask) {
470     case 0:
471         break;
472
473     case FLOW_NW_FRAG_MASK:
474         nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
475         break;
476
477     default:
478         nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
479                    nw_frag_mask & FLOW_NW_FRAG_MASK);
480         break;
481     }
482 }
483
484 static void
485 nxm_put_ip(struct ofpbuf *b, const struct match *match,
486            uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
487            bool oxm)
488 {
489     const struct flow *flow = &match->flow;
490
491     nxm_put_frag(b, match);
492
493     if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
494         nxm_put_8(b, oxm ? OXM_OF_IP_DSCP : NXM_OF_IP_TOS,
495                   flow->nw_tos & IP_DSCP_MASK);
496     }
497
498     if (match->wc.masks.nw_tos & IP_ECN_MASK) {
499         nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
500                   flow->nw_tos & IP_ECN_MASK);
501     }
502
503     if (!oxm && match->wc.masks.nw_ttl) {
504         nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
505     }
506
507     if (match->wc.masks.nw_proto) {
508         nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
509
510         if (flow->nw_proto == IPPROTO_TCP) {
511             nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
512                         flow->tp_src, match->wc.masks.tp_src);
513             nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
514                         flow->tp_dst, match->wc.masks.tp_dst);
515         } else if (flow->nw_proto == IPPROTO_UDP) {
516             nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
517                         flow->tp_src, match->wc.masks.tp_src);
518             nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
519                         flow->tp_dst, match->wc.masks.tp_dst);
520         } else if (flow->nw_proto == icmp_proto) {
521             if (match->wc.masks.tp_src) {
522                 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
523             }
524             if (match->wc.masks.tp_dst) {
525                 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
526             }
527         }
528     }
529 }
530
531 /* Appends to 'b' the nx_match format that expresses 'match'.  For Flow Mod and
532  * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
533  * Otherwise, 'cookie_mask' should be zero.
534  *
535  * This function can cause 'b''s data to be reallocated.
536  *
537  * Returns the number of bytes appended to 'b', excluding padding.
538  *
539  * If 'match' is a catch-all rule that matches every packet, then this function
540  * appends nothing to 'b' and returns 0. */
541 static int
542 nx_put_raw(struct ofpbuf *b, bool oxm, const struct match *match,
543            ovs_be64 cookie, ovs_be64 cookie_mask)
544 {
545     const struct flow *flow = &match->flow;
546     const size_t start_len = b->size;
547     int match_len;
548     int i;
549
550     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
551
552     /* Metadata. */
553     if (match->wc.masks.in_port) {
554         uint16_t in_port = flow->in_port;
555         if (oxm) {
556             nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
557         } else {
558             nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
559         }
560     }
561
562     /* Ethernet. */
563     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
564                        flow->dl_src, match->wc.masks.dl_src);
565     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
566                        flow->dl_dst, match->wc.masks.dl_dst);
567     nxm_put_16m(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
568                 ofputil_dl_type_to_openflow(flow->dl_type),
569                 match->wc.masks.dl_type);
570
571     /* 802.1Q. */
572     if (oxm) {
573         ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
574         ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
575         ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
576
577         if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
578             nxm_put_16(b, OXM_OF_VLAN_VID, vid);
579         } else if (mask) {
580             nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask);
581         }
582
583         if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
584             nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci));
585         }
586
587     } else {
588         nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci,
589                     match->wc.masks.vlan_tci);
590     }
591
592     /* L3. */
593     if (flow->dl_type == htons(ETH_TYPE_IP)) {
594         /* IP. */
595         nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
596                     flow->nw_src, match->wc.masks.nw_src);
597         nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
598                     flow->nw_dst, match->wc.masks.nw_dst);
599         nxm_put_ip(b, match, IPPROTO_ICMP,
600                    oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
601                    oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
602     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
603         /* IPv6. */
604         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
605                      &flow->ipv6_src, &match->wc.masks.ipv6_src);
606         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
607                      &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
608         nxm_put_ip(b, match, IPPROTO_ICMPV6,
609                    oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
610                    oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
611
612         nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
613                     flow->ipv6_label, match->wc.masks.ipv6_label);
614
615         if (flow->nw_proto == IPPROTO_ICMPV6
616             && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
617                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
618             nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
619                          &flow->nd_target, &match->wc.masks.nd_target);
620             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
621                 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
622                                    flow->arp_sha, match->wc.masks.arp_sha);
623             }
624             if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
625                 nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
626                                    flow->arp_tha, match->wc.masks.arp_tha);
627             }
628         }
629     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
630                flow->dl_type == htons(ETH_TYPE_RARP)) {
631         /* ARP. */
632         if (match->wc.masks.nw_proto) {
633             nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
634                        htons(flow->nw_proto));
635         }
636         nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
637                     flow->nw_src, match->wc.masks.nw_src);
638         nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
639                     flow->nw_dst, match->wc.masks.nw_dst);
640         nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
641                            flow->arp_sha, match->wc.masks.arp_sha);
642         nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
643                            flow->arp_tha, match->wc.masks.arp_tha);
644     }
645
646     /* Tunnel ID. */
647     nxm_put_64m(b, NXM_NX_TUN_ID, flow->tunnel.tun_id,
648                 match->wc.masks.tunnel.tun_id);
649
650     /* Registers. */
651     for (i = 0; i < FLOW_N_REGS; i++) {
652         nxm_put_32m(b, NXM_NX_REG(i),
653                     htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
654     }
655
656     /* OpenFlow 1.1+ Metadata. */
657     nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, match->wc.masks.metadata);
658
659     /* Cookie. */
660     nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
661
662     match_len = b->size - start_len;
663     return match_len;
664 }
665
666 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
667  * bytes to pad the nx_match out to a multiple of 8.  For Flow Mod and Flow
668  * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
669  * Otherwise, 'cookie_mask' should be zero.
670  *
671  * This function can cause 'b''s data to be reallocated.
672  *
673  * Returns the number of bytes appended to 'b', excluding padding.  The return
674  * value can be zero if it appended nothing at all to 'b' (which happens if
675  * 'cr' is a catch-all rule that matches every packet). */
676 int
677 nx_put_match(struct ofpbuf *b, const struct match *match,
678              ovs_be64 cookie, ovs_be64 cookie_mask)
679 {
680     int match_len = nx_put_raw(b, false, match, cookie, cookie_mask);
681
682     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
683     return match_len;
684 }
685
686
687 /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that
688  * expresses 'cr', plus enough zero bytes to pad the data appended out to a
689  * multiple of 8.
690  *
691  * This function can cause 'b''s data to be reallocated.
692  *
693  * Returns the number of bytes appended to 'b', excluding the padding.  Never
694  * returns zero. */
695 int
696 oxm_put_match(struct ofpbuf *b, const struct match *match)
697 {
698     int match_len;
699     struct ofp11_match_header *omh;
700     size_t start_len = b->size;
701     ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
702
703     ofpbuf_put_uninit(b, sizeof *omh);
704     match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
705     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
706
707     omh = (struct ofp11_match_header *)((char *)b->data + start_len);
708     omh->type = htons(OFPMT_OXM);
709     omh->length = htons(match_len);
710
711     return match_len;
712 }
713 \f
714 /* nx_match_to_string() and helpers. */
715
716 static void format_nxm_field_name(struct ds *, uint32_t header);
717
718 char *
719 nx_match_to_string(const uint8_t *p, unsigned int match_len)
720 {
721     uint32_t header;
722     struct ds s;
723
724     if (!match_len) {
725         return xstrdup("<any>");
726     }
727
728     ds_init(&s);
729     while ((header = nx_entry_ok(p, match_len)) != 0) {
730         unsigned int length = NXM_LENGTH(header);
731         unsigned int value_len = nxm_field_bytes(header);
732         const uint8_t *value = p + 4;
733         const uint8_t *mask = value + value_len;
734         unsigned int i;
735
736         if (s.length) {
737             ds_put_cstr(&s, ", ");
738         }
739
740         format_nxm_field_name(&s, header);
741         ds_put_char(&s, '(');
742
743         for (i = 0; i < value_len; i++) {
744             ds_put_format(&s, "%02x", value[i]);
745         }
746         if (NXM_HASMASK(header)) {
747             ds_put_char(&s, '/');
748             for (i = 0; i < value_len; i++) {
749                 ds_put_format(&s, "%02x", mask[i]);
750             }
751         }
752         ds_put_char(&s, ')');
753
754         p += 4 + length;
755         match_len -= 4 + length;
756     }
757
758     if (match_len) {
759         if (s.length) {
760             ds_put_cstr(&s, ", ");
761         }
762
763         ds_put_format(&s, "<%u invalid bytes>", match_len);
764     }
765
766     return ds_steal_cstr(&s);
767 }
768
769 char *
770 oxm_match_to_string(const uint8_t *p, unsigned int match_len)
771 {
772     const struct ofp11_match_header *omh = (struct ofp11_match_header *)p;
773     uint16_t match_len_;
774     struct ds s;
775
776     ds_init(&s);
777
778     if (match_len < sizeof *omh) {
779         ds_put_format(&s, "<match too short: %u>", match_len);
780         goto err;
781     }
782
783     if (omh->type != htons(OFPMT_OXM)) {
784         ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
785         goto err;
786     }
787
788     match_len_ = ntohs(omh->length);
789     if (match_len_ < sizeof *omh) {
790         ds_put_format(&s, "<match length field too short: %u>", match_len_);
791         goto err;
792     }
793
794     if (match_len_ != match_len) {
795         ds_put_format(&s, "<match length field incorrect: %u != %u>",
796                       match_len_, match_len);
797         goto err;
798     }
799
800     return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh);
801
802 err:
803     return ds_steal_cstr(&s);
804 }
805
806 static void
807 format_nxm_field_name(struct ds *s, uint32_t header)
808 {
809     const struct mf_field *mf = mf_from_nxm_header(header);
810     if (mf) {
811         ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
812         if (NXM_HASMASK(header)) {
813             ds_put_cstr(s, "_W");
814         }
815     } else if (header == NXM_NX_COOKIE) {
816         ds_put_cstr(s, "NXM_NX_COOKIE");
817     } else if (header == NXM_NX_COOKIE_W) {
818         ds_put_cstr(s, "NXM_NX_COOKIE_W");
819     } else {
820         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
821     }
822 }
823
824 static uint32_t
825 parse_nxm_field_name(const char *name, int name_len)
826 {
827     bool wild;
828     int i;
829
830     /* Check whether it's a field name. */
831     wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
832     if (wild) {
833         name_len -= 2;
834     }
835
836     for (i = 0; i < MFF_N_IDS; i++) {
837         const struct mf_field *mf = mf_from_id(i);
838         uint32_t header;
839
840         if (mf->nxm_name &&
841             !strncmp(mf->nxm_name, name, name_len) &&
842             mf->nxm_name[name_len] == '\0') {
843             header = mf->nxm_header;
844         } else if (mf->oxm_name &&
845                    !strncmp(mf->oxm_name, name, name_len) &&
846                    mf->oxm_name[name_len] == '\0') {
847             header = mf->oxm_header;
848         } else {
849             continue;
850         }
851
852         if (!wild) {
853             return header;
854         } else if (mf->maskable != MFM_NONE) {
855             return NXM_MAKE_WILD_HEADER(header);
856         }
857     }
858
859     if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
860         (name_len == strlen("NXM_NX_COOKIE"))) {
861         if (!wild) {
862             return NXM_NX_COOKIE;
863         } else {
864             return NXM_NX_COOKIE_W;
865         }
866     }
867
868     /* Check whether it's a 32-bit field header value as hex.
869      * (This isn't ordinarily useful except for testing error behavior.) */
870     if (name_len == 8) {
871         uint32_t header = hexits_value(name, name_len, NULL);
872         if (header != UINT_MAX) {
873             return header;
874         }
875     }
876
877     return 0;
878 }
879 \f
880 /* nx_match_from_string(). */
881
882 static int
883 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
884 {
885     const char *full_s = s;
886     const size_t start_len = b->size;
887
888     if (!strcmp(s, "<any>")) {
889         /* Ensure that 'b->data' isn't actually null. */
890         ofpbuf_prealloc_tailroom(b, 1);
891         return 0;
892     }
893
894     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
895         const char *name;
896         uint32_t header;
897         int name_len;
898         size_t n;
899
900         name = s;
901         name_len = strcspn(s, "(");
902         if (s[name_len] != '(') {
903             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
904         }
905
906         header = parse_nxm_field_name(name, name_len);
907         if (!header) {
908             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
909         }
910
911         s += name_len + 1;
912
913         nxm_put_header(b, header);
914         s = ofpbuf_put_hex(b, s, &n);
915         if (n != nxm_field_bytes(header)) {
916             ovs_fatal(0, "%.2s: hex digits expected", s);
917         }
918         if (NXM_HASMASK(header)) {
919             s += strspn(s, " ");
920             if (*s != '/') {
921                 ovs_fatal(0, "%s: missing / in masked field %.*s",
922                           full_s, name_len, name);
923             }
924             s = ofpbuf_put_hex(b, s + 1, &n);
925             if (n != nxm_field_bytes(header)) {
926                 ovs_fatal(0, "%.2s: hex digits expected", s);
927             }
928         }
929
930         s += strspn(s, " ");
931         if (*s != ')') {
932             ovs_fatal(0, "%s: missing ) following field %.*s",
933                       full_s, name_len, name);
934         }
935         s++;
936     }
937
938     return b->size - start_len;
939 }
940
941 int
942 nx_match_from_string(const char *s, struct ofpbuf *b)
943 {
944     int match_len = nx_match_from_string_raw(s, b);
945     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
946     return match_len;
947 }
948
949 int
950 oxm_match_from_string(const char *s, struct ofpbuf *b)
951 {
952     int match_len;
953     struct ofp11_match_header *omh;
954     size_t start_len = b->size;
955
956     ofpbuf_put_uninit(b, sizeof *omh);
957     match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
958     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
959
960     omh = (struct ofp11_match_header *)((char *)b->data + start_len);
961     omh->type = htons(OFPMT_OXM);
962     omh->length = htons(match_len);
963
964     return match_len;
965 }
966 \f
967 void
968 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
969 {
970     const char *full_s = s;
971
972     s = mf_parse_subfield(&move->src, s);
973     if (strncmp(s, "->", 2)) {
974         ovs_fatal(0, "%s: missing `->' following source", full_s);
975     }
976     s += 2;
977     s = mf_parse_subfield(&move->dst, s);
978     if (*s != '\0') {
979         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
980     }
981
982     if (move->src.n_bits != move->dst.n_bits) {
983         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
984                   "%d bits wide", full_s,
985                   move->src.n_bits, move->dst.n_bits);
986     }
987 }
988
989 void
990 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
991 {
992     const char *full_s = s;
993     uint64_t value = strtoull(s, (char **) &s, 0);
994
995     if (strncmp(s, "->", 2)) {
996         ovs_fatal(0, "%s: missing `->' following value", full_s);
997     }
998     s += 2;
999     s = mf_parse_subfield(&load->dst, s);
1000     if (*s != '\0') {
1001         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
1002     }
1003
1004     if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) {
1005         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
1006                   full_s, value, load->dst.n_bits);
1007     }
1008
1009     load->subvalue.be64[0] = htonll(0);
1010     load->subvalue.be64[1] = htonll(value);
1011 }
1012 \f
1013 /* nxm_format_reg_move(), nxm_format_reg_load(). */
1014
1015 void
1016 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1017 {
1018     ds_put_format(s, "move:");
1019     mf_format_subfield(&move->src, s);
1020     ds_put_cstr(s, "->");
1021     mf_format_subfield(&move->dst, s);
1022 }
1023
1024 static void
1025 set_field_format(const struct ofpact_reg_load *load, struct ds *s)
1026 {
1027     const struct mf_field *mf = load->dst.field;
1028     union mf_value value;
1029
1030     assert(load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD);
1031     ds_put_format(s, "set_field:");
1032     memset(&value, 0, sizeof value);
1033     bitwise_copy(&load->subvalue, sizeof load->subvalue, 0,
1034                  &value, mf->n_bytes, 0, load->dst.n_bits);
1035     mf_format(mf, &value, NULL, s);
1036     ds_put_format(s, "->%s", mf->name);
1037 }
1038
1039 static void
1040 load_format(const struct ofpact_reg_load *load, struct ds *s)
1041 {
1042     ds_put_cstr(s, "load:");
1043     mf_format_subvalue(&load->subvalue, s);
1044     ds_put_cstr(s, "->");
1045     mf_format_subfield(&load->dst, s);
1046 }
1047
1048 void
1049 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
1050 {
1051     if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1052         set_field_format(load, s);
1053     } else {
1054         load_format(load, s);
1055     }
1056 }
1057 \f
1058 enum ofperr
1059 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
1060                            struct ofpbuf *ofpacts)
1061 {
1062     struct ofpact_reg_move *move;
1063
1064     move = ofpact_put_REG_MOVE(ofpacts);
1065     move->src.field = mf_from_nxm_header(ntohl(narm->src));
1066     move->src.ofs = ntohs(narm->src_ofs);
1067     move->src.n_bits = ntohs(narm->n_bits);
1068     move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1069     move->dst.ofs = ntohs(narm->dst_ofs);
1070     move->dst.n_bits = ntohs(narm->n_bits);
1071
1072     return nxm_reg_move_check(move, NULL);
1073 }
1074
1075 enum ofperr
1076 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
1077                            struct ofpbuf *ofpacts)
1078 {
1079     struct ofpact_reg_load *load;
1080
1081     load = ofpact_put_REG_LOAD(ofpacts);
1082     load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
1083     load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
1084     load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
1085     load->subvalue.be64[1] = narl->value;
1086
1087     /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
1088      * narl->value. */
1089     if (load->dst.n_bits < 64 &&
1090         ntohll(narl->value) >> load->dst.n_bits) {
1091         return OFPERR_OFPBAC_BAD_ARGUMENT;
1092     }
1093
1094     return nxm_reg_load_check(load, NULL);
1095 }
1096
1097 enum ofperr
1098 nxm_reg_load_from_openflow12_set_field(
1099     const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts)
1100 {
1101     uint16_t oasf_len = ntohs(oasf->len);
1102     uint32_t oxm_header = ntohl(oasf->dst);
1103     uint8_t oxm_length = NXM_LENGTH(oxm_header);
1104     struct ofpact_reg_load *load;
1105     const struct mf_field *mf;
1106
1107     /* ofp12_action_set_field is padded to 64 bits by zero */
1108     if (oasf_len != ROUND_UP(sizeof(*oasf) + oxm_length, 8)) {
1109         return OFPERR_OFPBAC_BAD_ARGUMENT;
1110     }
1111     if (!is_all_zeros((const uint8_t *)(oasf) + sizeof *oasf + oxm_length,
1112                       oasf_len - oxm_length - sizeof *oasf)) {
1113         return OFPERR_OFPBAC_BAD_ARGUMENT;
1114     }
1115
1116     if (NXM_HASMASK(oxm_header)) {
1117         return OFPERR_OFPBAC_BAD_ARGUMENT;
1118     }
1119     mf = mf_from_nxm_header(oxm_header);
1120     if (!mf) {
1121         return OFPERR_OFPBAC_BAD_ARGUMENT;
1122     }
1123     load = ofpact_put_REG_LOAD(ofpacts);
1124     ofpact_set_field_init(load, mf, oasf + 1);
1125
1126     return nxm_reg_load_check(load, NULL);
1127 }
1128 \f
1129 enum ofperr
1130 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1131 {
1132     enum ofperr error;
1133
1134     error = mf_check_src(&move->src, flow);
1135     if (error) {
1136         return error;
1137     }
1138
1139     return mf_check_dst(&move->dst, NULL);
1140 }
1141
1142 enum ofperr
1143 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
1144 {
1145     return mf_check_dst(&load->dst, flow);
1146 }
1147 \f
1148 void
1149 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
1150                       struct ofpbuf *openflow)
1151 {
1152     struct nx_action_reg_move *narm;
1153
1154     narm = ofputil_put_NXAST_REG_MOVE(openflow);
1155     narm->n_bits = htons(move->dst.n_bits);
1156     narm->src_ofs = htons(move->src.ofs);
1157     narm->dst_ofs = htons(move->dst.ofs);
1158     narm->src = htonl(move->src.field->nxm_header);
1159     narm->dst = htonl(move->dst.field->nxm_header);
1160 }
1161
1162 static void
1163 reg_load_to_nxast(const struct ofpact_reg_load *load, struct ofpbuf *openflow)
1164 {
1165     struct nx_action_reg_load *narl;
1166
1167     narl = ofputil_put_NXAST_REG_LOAD(openflow);
1168     narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
1169     narl->dst = htonl(load->dst.field->nxm_header);
1170     narl->value = load->subvalue.be64[1];
1171 }
1172
1173 static void
1174 set_field_to_ofast(const struct ofpact_reg_load *load,
1175                       struct ofpbuf *openflow)
1176 {
1177     const struct mf_field *mf = load->dst.field;
1178     struct ofp12_action_set_field *oasf;
1179     uint16_t padded_value_len;
1180
1181     oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
1182     oasf->dst = htonl(mf->oxm_header);
1183
1184     /* Set field is the only action of variable length (so far),
1185      * so handling the variable length portion is open-coded here */
1186     padded_value_len = ROUND_UP(mf->n_bytes, 8);
1187     ofpbuf_put_uninit(openflow, padded_value_len);
1188     oasf->len = htons(ntohs(oasf->len) + padded_value_len);
1189     memset(oasf + 1, 0, padded_value_len);
1190
1191     bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs,
1192                  oasf + 1, mf->n_bytes, load->dst.ofs, load->dst.n_bits);
1193     return;
1194 }
1195
1196 void
1197 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
1198                       struct ofpbuf *openflow)
1199 {
1200
1201     if (load->ofpact.compat == OFPUTIL_OFPAT12_SET_FIELD) {
1202         struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1203
1204         switch(oh->version) {
1205         case OFP12_VERSION:
1206             set_field_to_ofast(load, openflow);
1207             break;
1208
1209         case OFP11_VERSION:
1210         case OFP10_VERSION:
1211             if (load->dst.n_bits < 64) {
1212                 reg_load_to_nxast(load, openflow);
1213             } else {
1214                 /* Split into 64bit chunks */
1215                 int chunk, ofs;
1216                 for (ofs = 0; ofs < load->dst.n_bits; ofs += chunk) {
1217                     struct ofpact_reg_load subload = *load;
1218
1219                     chunk = MIN(load->dst.n_bits - ofs, 64);
1220
1221                     subload.dst.field =  load->dst.field;
1222                     subload.dst.ofs = load->dst.ofs + ofs;
1223                     subload.dst.n_bits = chunk;
1224                     bitwise_copy(&load->subvalue, sizeof load->subvalue, ofs,
1225                                  &subload.subvalue, sizeof subload.subvalue, 0,
1226                                  chunk);
1227                     reg_load_to_nxast(&subload, openflow);
1228                 }
1229             }
1230             break;
1231
1232         default:
1233             NOT_REACHED();
1234         }
1235     } else {
1236         reg_load_to_nxast(load, openflow);
1237     }
1238 }
1239 \f
1240 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1241
1242 void
1243 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1244                      struct flow *flow)
1245 {
1246     union mf_value src_value;
1247     union mf_value dst_value;
1248
1249     mf_get_value(move->dst.field, flow, &dst_value);
1250     mf_get_value(move->src.field, flow, &src_value);
1251     bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1252                  &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1253                  move->src.n_bits);
1254     mf_set_flow_value(move->dst.field, &dst_value, flow);
1255 }
1256
1257 void
1258 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
1259 {
1260     mf_write_subfield_flow(&load->dst, &load->subvalue, flow);
1261 }
1262
1263 void
1264 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1265              struct flow *flow)
1266 {
1267     union mf_subvalue src_subvalue;
1268     ovs_be64 src_data_be = htonll(src_data);
1269
1270     bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1271                  &src_subvalue, sizeof src_subvalue, 0,
1272                  sizeof src_data_be * 8);
1273     mf_write_subfield_flow(dst, &src_subvalue, flow);
1274 }