nx-match: Update register check functions.
[sliver-openvswitch.git] / lib / packets.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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 <assert.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <stdlib.h>
23 #include "byte-order.h"
24 #include "dynamic-string.h"
25 #include "ofpbuf.h"
26
27 const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT;
28
29 /* Parses 's' as a 16-digit hexadecimal number representing a datapath ID.  On
30  * success stores the dpid into '*dpidp' and returns true, on failure stores 0
31  * into '*dpidp' and returns false.
32  *
33  * Rejects an all-zeros dpid as invalid. */
34 bool
35 dpid_from_string(const char *s, uint64_t *dpidp)
36 {
37     *dpidp = (strlen(s) == 16 && strspn(s, "0123456789abcdefABCDEF") == 16
38               ? strtoull(s, NULL, 16)
39               : 0);
40     return *dpidp != 0;
41 }
42
43 bool
44 eth_addr_from_string(const char *s, uint8_t ea[ETH_ADDR_LEN])
45 {
46     if (sscanf(s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))
47         == ETH_ADDR_SCAN_COUNT) {
48         return true;
49     } else {
50         memset(ea, 0, ETH_ADDR_LEN);
51         return false;
52     }
53 }
54
55 /* Fills 'b' with an 802.2 SNAP packet with Ethernet source address 'eth_src',
56  * the Nicira OUI as SNAP organization and 'snap_type' as SNAP type.  The text
57  * string in 'tag' is enclosed as the packet payload.
58  *
59  * This function is used by Open vSwitch to compose packets in cases where
60  * context is important but content doesn't (or shouldn't) matter.  For this
61  * purpose, 'snap_type' should be a random number and 'tag' should be an
62  * English phrase that explains the purpose of the packet.  (The English phrase
63  * gives hapless admins running Wireshark the opportunity to figure out what's
64  * going on.) */
65 void
66 compose_benign_packet(struct ofpbuf *b, const char *tag, uint16_t snap_type,
67                       const uint8_t eth_src[ETH_ADDR_LEN])
68 {
69     size_t tag_size = strlen(tag) + 1;
70     char *payload;
71
72     payload = snap_compose(b, eth_addr_broadcast, eth_src, 0x002320, snap_type,
73                            tag_size + ETH_ADDR_LEN);
74     memcpy(payload, tag, tag_size);
75     memcpy(payload + tag_size, eth_src, ETH_ADDR_LEN);
76 }
77
78 /* Modify the TCI field of 'packet', whose data must begin with an Ethernet
79  * header.  If a VLAN tag is present, its TCI field is replaced by 'tci'.  If a
80  * VLAN tag is not present, one is added with the TCI field set to 'tci'.
81  *
82  * Also sets 'packet->l2' to point to the new Ethernet header. */
83 void
84 eth_set_vlan_tci(struct ofpbuf *packet, ovs_be16 tci)
85 {
86     struct eth_header *eh = packet->data;
87     struct vlan_eth_header *veh;
88
89     if (packet->size >= sizeof(struct vlan_eth_header)
90         && eh->eth_type == htons(ETH_TYPE_VLAN)) {
91         veh = packet->data;
92         veh->veth_tci = tci;
93     } else {
94         /* Insert new 802.1Q header. */
95         struct vlan_eth_header tmp;
96         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
97         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
98         tmp.veth_type = htons(ETH_TYPE_VLAN);
99         tmp.veth_tci = tci;
100         tmp.veth_next_type = eh->eth_type;
101
102         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
103         memcpy(veh, &tmp, sizeof tmp);
104     }
105     packet->l2 = packet->data;
106 }
107
108 /* Stores the string representation of the IPv6 address 'addr' into the
109  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
110  * bytes long. */
111 void
112 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
113 {
114     inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
115 }
116
117 void
118 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
119 {
120     char addr_str[INET6_ADDRSTRLEN];
121
122     format_ipv6_addr(addr_str, addr);
123     ds_put_format(string, "%s", addr_str);
124 }
125
126 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
127                                  const struct in6_addr *b)
128 {
129     int i;
130     struct in6_addr dst;
131
132 #ifdef s6_addr32
133     for (i=0; i<4; i++) {
134         dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
135     }
136 #else
137     for (i=0; i<16; i++) {
138         dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
139     }
140 #endif
141
142     return dst;
143 }
144
145 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
146  * low-order 0-bits. */
147 struct in6_addr
148 ipv6_create_mask(int mask)
149 {
150     struct in6_addr netmask;
151     uint8_t *netmaskp = &netmask.s6_addr[0];
152
153     memset(&netmask, 0, sizeof netmask);
154     while (mask > 8) {
155         *netmaskp = 0xff;
156         netmaskp++;
157         mask -= 8;
158     }
159
160     if (mask) {
161         *netmaskp = 0xff << (8 - mask);
162     }
163
164     return netmask;
165 }
166
167 /* Given the IPv6 netmask 'netmask', returns the number of bits of the
168  * IPv6 address that it wildcards.  'netmask' must be a CIDR netmask (see
169  * ipv6_is_cidr()). */
170 int
171 ipv6_count_cidr_bits(const struct in6_addr *netmask)
172 {
173     int i;
174     int count = 0;
175     const uint8_t *netmaskp = &netmask->s6_addr[0];
176
177     assert(ipv6_is_cidr(netmask));
178
179     for (i=0; i<16; i++) {
180         if (netmaskp[i] == 0xff) {
181             count += 8;
182         } else {
183             uint8_t nm;
184
185             for(nm = netmaskp[i]; nm; nm <<= 1) {
186                 count++;
187             }
188             break;
189         }
190
191     }
192
193     return count;
194 }
195
196 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
197  * high-order 1-bits and 128-N low-order 0-bits. */
198 bool
199 ipv6_is_cidr(const struct in6_addr *netmask)
200 {
201     const uint8_t *netmaskp = &netmask->s6_addr[0];
202     int i;
203
204     for (i=0; i<16; i++) {
205         if (netmaskp[i] != 0xff) {
206             uint8_t x = ~netmaskp[i];
207             if (x & (x + 1)) {
208                 return false;
209             }
210             while (++i < 16) {
211                 if (netmaskp[i]) {
212                     return false;
213                 }
214             }
215         }
216     }
217
218     return true;
219 }
220
221 /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
222  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
223  * in 'b' and returned.  This payload may be populated with appropriate
224  * information by the caller.
225  *
226  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
227  * desired. */
228 void *
229 eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
230             const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
231             size_t size)
232 {
233     void *data;
234     struct eth_header *eth;
235
236     ofpbuf_clear(b);
237
238     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
239     ofpbuf_reserve(b, VLAN_HEADER_LEN);
240     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
241     data = ofpbuf_put_uninit(b, size);
242
243     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
244     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
245     eth->eth_type = htons(eth_type);
246
247     return data;
248 }
249
250 /* Populates 'b' with an Ethernet LLC+SNAP packet headed with the given
251  * 'eth_dst', 'eth_src', 'snap_org', and 'snap_type'.  A payload of 'size'
252  * bytes is allocated in 'b' and returned.  This payload may be populated with
253  * appropriate information by the caller.
254  *
255  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
256  * desired. */
257 void *
258 snap_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
259              const uint8_t eth_src[ETH_ADDR_LEN],
260              unsigned int oui, uint16_t snap_type, size_t size)
261 {
262     struct eth_header *eth;
263     struct llc_snap_header *llc_snap;
264     void *payload;
265
266     /* Compose basic packet structure.  (We need the payload size to stick into
267      * the 802.2 header.) */
268     ofpbuf_clear(b);
269     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
270                              + LLC_SNAP_HEADER_LEN + size);
271     ofpbuf_reserve(b, VLAN_HEADER_LEN);
272     eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
273     llc_snap = ofpbuf_put_zeros(b, LLC_SNAP_HEADER_LEN);
274     payload = ofpbuf_put_uninit(b, size);
275
276     /* Compose 802.2 header. */
277     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
278     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
279     eth->eth_type = htons(b->size - ETH_HEADER_LEN);
280
281     /* Compose LLC, SNAP headers. */
282     llc_snap->llc.llc_dsap = LLC_DSAP_SNAP;
283     llc_snap->llc.llc_ssap = LLC_SSAP_SNAP;
284     llc_snap->llc.llc_cntl = LLC_CNTL_SNAP;
285     llc_snap->snap.snap_org[0] = oui >> 16;
286     llc_snap->snap.snap_org[1] = oui >> 8;
287     llc_snap->snap.snap_org[2] = oui;
288     llc_snap->snap.snap_type = htons(snap_type);
289
290     return payload;
291 }