packets: Mask out CFI bit in eth_push_vlan().
[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 <sys/socket.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include "byte-order.h"
25 #include "dynamic-string.h"
26 #include "ofpbuf.h"
27
28 const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT;
29
30 /* Parses 's' as a 16-digit hexadecimal number representing a datapath ID.  On
31  * success stores the dpid into '*dpidp' and returns true, on failure stores 0
32  * into '*dpidp' and returns false.
33  *
34  * Rejects an all-zeros dpid as invalid. */
35 bool
36 dpid_from_string(const char *s, uint64_t *dpidp)
37 {
38     *dpidp = (strlen(s) == 16 && strspn(s, "0123456789abcdefABCDEF") == 16
39               ? strtoull(s, NULL, 16)
40               : 0);
41     return *dpidp != 0;
42 }
43
44 bool
45 eth_addr_from_string(const char *s, uint8_t ea[ETH_ADDR_LEN])
46 {
47     if (sscanf(s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))
48         == ETH_ADDR_SCAN_COUNT) {
49         return true;
50     } else {
51         memset(ea, 0, ETH_ADDR_LEN);
52         return false;
53     }
54 }
55
56 /* Fills 'b' with an 802.2 SNAP packet with Ethernet source address 'eth_src',
57  * the Nicira OUI as SNAP organization and 'snap_type' as SNAP type.  The text
58  * string in 'tag' is enclosed as the packet payload.
59  *
60  * This function is used by Open vSwitch to compose packets in cases where
61  * context is important but content doesn't (or shouldn't) matter.  For this
62  * purpose, 'snap_type' should be a random number and 'tag' should be an
63  * English phrase that explains the purpose of the packet.  (The English phrase
64  * gives hapless admins running Wireshark the opportunity to figure out what's
65  * going on.) */
66 void
67 compose_benign_packet(struct ofpbuf *b, const char *tag, uint16_t snap_type,
68                       const uint8_t eth_src[ETH_ADDR_LEN])
69 {
70     size_t tag_size = strlen(tag) + 1;
71     char *payload;
72
73     payload = snap_compose(b, eth_addr_broadcast, eth_src, 0x002320, snap_type,
74                            tag_size + ETH_ADDR_LEN);
75     memcpy(payload, tag, tag_size);
76     memcpy(payload + tag_size, eth_src, ETH_ADDR_LEN);
77 }
78
79 /* Insert VLAN header according to given TCI. Packet passed must be Ethernet
80  * packet.  Ignores the CFI bit of 'tci' using 0 instead.
81  *
82  * Also sets 'packet->l2' to point to the new Ethernet header. */
83 void
84 eth_push_vlan(struct ofpbuf *packet, ovs_be16 tci)
85 {
86     struct eth_header *eh = packet->data;
87     struct vlan_eth_header *veh;
88
89     /* Insert new 802.1Q header. */
90     struct vlan_eth_header tmp;
91     memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
92     memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
93     tmp.veth_type = htons(ETH_TYPE_VLAN);
94     tmp.veth_tci = tci & htons(~VLAN_CFI);
95     tmp.veth_next_type = eh->eth_type;
96
97     veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
98     memcpy(veh, &tmp, sizeof tmp);
99
100     packet->l2 = packet->data;
101 }
102
103 /* Removes outermost VLAN header (if any is present) from 'packet'.
104  *
105  * 'packet->l2' must initially point to 'packet''s Ethernet header. */
106 void
107 eth_pop_vlan(struct ofpbuf *packet)
108 {
109     struct vlan_eth_header *veh = packet->l2;
110     if (packet->size >= sizeof *veh
111         && veh->veth_type == htons(ETH_TYPE_VLAN)) {
112         struct eth_header tmp;
113
114         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
115         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
116         tmp.eth_type = veh->veth_next_type;
117
118         ofpbuf_pull(packet, VLAN_HEADER_LEN);
119         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
120         memcpy(packet->data, &tmp, sizeof tmp);
121     }
122 }
123
124 /* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'.  The
125  * caller must free '*packetp'.  On success, returns NULL.  On failure, returns
126  * an error message and stores NULL in '*packetp'. */
127 const char *
128 eth_from_hex(const char *hex, struct ofpbuf **packetp)
129 {
130     struct ofpbuf *packet;
131
132     packet = *packetp = ofpbuf_new(strlen(hex) / 2);
133
134     if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
135         ofpbuf_delete(packet);
136         *packetp = NULL;
137         return "Trailing garbage in packet data";
138     }
139
140     if (packet->size < ETH_HEADER_LEN) {
141         ofpbuf_delete(packet);
142         *packetp = NULL;
143         return "Packet data too short for Ethernet";
144     }
145
146     return NULL;
147 }
148
149 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
150  * that it specifies, that is, the number of 1-bits in 'netmask'.  'netmask'
151  * must be a CIDR netmask (see ip_is_cidr()). */
152 int
153 ip_count_cidr_bits(ovs_be32 netmask)
154 {
155     assert(ip_is_cidr(netmask));
156     return 32 - ctz(ntohl(netmask));
157 }
158
159 void
160 ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
161 {
162     ds_put_format(s, IP_FMT, IP_ARGS(&ip));
163     if (mask != htonl(UINT32_MAX)) {
164         if (ip_is_cidr(mask)) {
165             ds_put_format(s, "/%d", ip_count_cidr_bits(mask));
166         } else {
167             ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
168         }
169     }
170 }
171
172
173 /* Stores the string representation of the IPv6 address 'addr' into the
174  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
175  * bytes long. */
176 void
177 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
178 {
179     inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
180 }
181
182 void
183 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
184 {
185     char *dst;
186
187     ds_reserve(string, string->length + INET6_ADDRSTRLEN);
188
189     dst = string->string + string->length;
190     format_ipv6_addr(dst, addr);
191     string->length += strlen(dst);
192 }
193
194 void
195 print_ipv6_masked(struct ds *s, const struct in6_addr *addr,
196                   const struct in6_addr *mask)
197 {
198     print_ipv6_addr(s, addr);
199     if (mask && !ipv6_mask_is_exact(mask)) {
200         if (ipv6_is_cidr(mask)) {
201             int cidr_bits = ipv6_count_cidr_bits(mask);
202             ds_put_format(s, "/%d", cidr_bits);
203         } else {
204             ds_put_char(s, '/');
205             print_ipv6_addr(s, mask);
206         }
207     }
208 }
209
210 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
211                                  const struct in6_addr *b)
212 {
213     int i;
214     struct in6_addr dst;
215
216 #ifdef s6_addr32
217     for (i=0; i<4; i++) {
218         dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
219     }
220 #else
221     for (i=0; i<16; i++) {
222         dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
223     }
224 #endif
225
226     return dst;
227 }
228
229 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
230  * low-order 0-bits. */
231 struct in6_addr
232 ipv6_create_mask(int mask)
233 {
234     struct in6_addr netmask;
235     uint8_t *netmaskp = &netmask.s6_addr[0];
236
237     memset(&netmask, 0, sizeof netmask);
238     while (mask > 8) {
239         *netmaskp = 0xff;
240         netmaskp++;
241         mask -= 8;
242     }
243
244     if (mask) {
245         *netmaskp = 0xff << (8 - mask);
246     }
247
248     return netmask;
249 }
250
251 /* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6
252  * address that it specifies, that is, the number of 1-bits in 'netmask'.
253  * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()). */
254 int
255 ipv6_count_cidr_bits(const struct in6_addr *netmask)
256 {
257     int i;
258     int count = 0;
259     const uint8_t *netmaskp = &netmask->s6_addr[0];
260
261     assert(ipv6_is_cidr(netmask));
262
263     for (i=0; i<16; i++) {
264         if (netmaskp[i] == 0xff) {
265             count += 8;
266         } else {
267             uint8_t nm;
268
269             for(nm = netmaskp[i]; nm; nm <<= 1) {
270                 count++;
271             }
272             break;
273         }
274
275     }
276
277     return count;
278 }
279
280 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
281  * high-order 1-bits and 128-N low-order 0-bits. */
282 bool
283 ipv6_is_cidr(const struct in6_addr *netmask)
284 {
285     const uint8_t *netmaskp = &netmask->s6_addr[0];
286     int i;
287
288     for (i=0; i<16; i++) {
289         if (netmaskp[i] != 0xff) {
290             uint8_t x = ~netmaskp[i];
291             if (x & (x + 1)) {
292                 return false;
293             }
294             while (++i < 16) {
295                 if (netmaskp[i]) {
296                     return false;
297                 }
298             }
299         }
300     }
301
302     return true;
303 }
304
305 /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
306  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
307  * in 'b' and returned.  This payload may be populated with appropriate
308  * information by the caller.  Sets 'b''s 'l2' and 'l3' pointers to the
309  * Ethernet header and payload respectively.
310  *
311  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
312  * desired. */
313 void *
314 eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
315             const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
316             size_t size)
317 {
318     void *data;
319     struct eth_header *eth;
320
321     ofpbuf_clear(b);
322
323     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
324     ofpbuf_reserve(b, VLAN_HEADER_LEN);
325     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
326     data = ofpbuf_put_uninit(b, size);
327
328     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
329     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
330     eth->eth_type = htons(eth_type);
331
332     b->l2 = eth;
333     b->l3 = data;
334
335     return data;
336 }
337
338 /* Populates 'b' with an Ethernet LLC+SNAP packet headed with the given
339  * 'eth_dst', 'eth_src', 'snap_org', and 'snap_type'.  A payload of 'size'
340  * bytes is allocated in 'b' and returned.  This payload may be populated with
341  * appropriate information by the caller.
342  *
343  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
344  * desired. */
345 void *
346 snap_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
347              const uint8_t eth_src[ETH_ADDR_LEN],
348              unsigned int oui, uint16_t snap_type, size_t size)
349 {
350     struct eth_header *eth;
351     struct llc_snap_header *llc_snap;
352     void *payload;
353
354     /* Compose basic packet structure.  (We need the payload size to stick into
355      * the 802.2 header.) */
356     ofpbuf_clear(b);
357     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
358                              + LLC_SNAP_HEADER_LEN + size);
359     ofpbuf_reserve(b, VLAN_HEADER_LEN);
360     eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
361     llc_snap = ofpbuf_put_zeros(b, LLC_SNAP_HEADER_LEN);
362     payload = ofpbuf_put_uninit(b, size);
363
364     /* Compose 802.2 header. */
365     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
366     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
367     eth->eth_type = htons(b->size - ETH_HEADER_LEN);
368
369     /* Compose LLC, SNAP headers. */
370     llc_snap->llc.llc_dsap = LLC_DSAP_SNAP;
371     llc_snap->llc.llc_ssap = LLC_SSAP_SNAP;
372     llc_snap->llc.llc_cntl = LLC_CNTL_SNAP;
373     llc_snap->snap.snap_org[0] = oui >> 16;
374     llc_snap->snap.snap_org[1] = oui >> 8;
375     llc_snap->snap.snap_org[2] = oui;
376     llc_snap->snap.snap_type = htons(snap_type);
377
378     return payload;
379 }