packets: New function eth_pop_vlan(), formerly dp_netdev_pop_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.
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;
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 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
125  * that it specifies, that is, the number of 1-bits in 'netmask'.  'netmask'
126  * must be a CIDR netmask (see ip_is_cidr()). */
127 int
128 ip_count_cidr_bits(ovs_be32 netmask)
129 {
130     assert(ip_is_cidr(netmask));
131     return 32 - ctz(ntohl(netmask));
132 }
133
134 void
135 ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
136 {
137     ds_put_format(s, IP_FMT, IP_ARGS(&ip));
138     if (mask != htonl(UINT32_MAX)) {
139         if (ip_is_cidr(mask)) {
140             ds_put_format(s, "/%d", ip_count_cidr_bits(mask));
141         } else {
142             ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
143         }
144     }
145 }
146
147
148 /* Stores the string representation of the IPv6 address 'addr' into the
149  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
150  * bytes long. */
151 void
152 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
153 {
154     inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
155 }
156
157 void
158 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
159 {
160     char *dst;
161
162     ds_reserve(string, string->length + INET6_ADDRSTRLEN);
163
164     dst = string->string + string->length;
165     format_ipv6_addr(dst, addr);
166     string->length += strlen(dst);
167 }
168
169 void
170 print_ipv6_masked(struct ds *s, const struct in6_addr *addr,
171                   const struct in6_addr *mask)
172 {
173     print_ipv6_addr(s, addr);
174     if (mask && !ipv6_mask_is_exact(mask)) {
175         if (ipv6_is_cidr(mask)) {
176             int cidr_bits = ipv6_count_cidr_bits(mask);
177             ds_put_format(s, "/%d", cidr_bits);
178         } else {
179             ds_put_char(s, '/');
180             print_ipv6_addr(s, mask);
181         }
182     }
183 }
184
185 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
186                                  const struct in6_addr *b)
187 {
188     int i;
189     struct in6_addr dst;
190
191 #ifdef s6_addr32
192     for (i=0; i<4; i++) {
193         dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
194     }
195 #else
196     for (i=0; i<16; i++) {
197         dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
198     }
199 #endif
200
201     return dst;
202 }
203
204 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
205  * low-order 0-bits. */
206 struct in6_addr
207 ipv6_create_mask(int mask)
208 {
209     struct in6_addr netmask;
210     uint8_t *netmaskp = &netmask.s6_addr[0];
211
212     memset(&netmask, 0, sizeof netmask);
213     while (mask > 8) {
214         *netmaskp = 0xff;
215         netmaskp++;
216         mask -= 8;
217     }
218
219     if (mask) {
220         *netmaskp = 0xff << (8 - mask);
221     }
222
223     return netmask;
224 }
225
226 /* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6
227  * address that it specifies, that is, the number of 1-bits in 'netmask'.
228  * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()). */
229 int
230 ipv6_count_cidr_bits(const struct in6_addr *netmask)
231 {
232     int i;
233     int count = 0;
234     const uint8_t *netmaskp = &netmask->s6_addr[0];
235
236     assert(ipv6_is_cidr(netmask));
237
238     for (i=0; i<16; i++) {
239         if (netmaskp[i] == 0xff) {
240             count += 8;
241         } else {
242             uint8_t nm;
243
244             for(nm = netmaskp[i]; nm; nm <<= 1) {
245                 count++;
246             }
247             break;
248         }
249
250     }
251
252     return count;
253 }
254
255 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
256  * high-order 1-bits and 128-N low-order 0-bits. */
257 bool
258 ipv6_is_cidr(const struct in6_addr *netmask)
259 {
260     const uint8_t *netmaskp = &netmask->s6_addr[0];
261     int i;
262
263     for (i=0; i<16; i++) {
264         if (netmaskp[i] != 0xff) {
265             uint8_t x = ~netmaskp[i];
266             if (x & (x + 1)) {
267                 return false;
268             }
269             while (++i < 16) {
270                 if (netmaskp[i]) {
271                     return false;
272                 }
273             }
274         }
275     }
276
277     return true;
278 }
279
280 /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
281  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
282  * in 'b' and returned.  This payload may be populated with appropriate
283  * information by the caller.  Sets 'b''s 'l2' and 'l3' pointers to the
284  * Ethernet header and payload respectively.
285  *
286  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
287  * desired. */
288 void *
289 eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
290             const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
291             size_t size)
292 {
293     void *data;
294     struct eth_header *eth;
295
296     ofpbuf_clear(b);
297
298     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
299     ofpbuf_reserve(b, VLAN_HEADER_LEN);
300     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
301     data = ofpbuf_put_uninit(b, size);
302
303     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
304     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
305     eth->eth_type = htons(eth_type);
306
307     b->l2 = eth;
308     b->l3 = data;
309
310     return data;
311 }
312
313 /* Populates 'b' with an Ethernet LLC+SNAP packet headed with the given
314  * 'eth_dst', 'eth_src', 'snap_org', and 'snap_type'.  A payload of 'size'
315  * bytes is allocated in 'b' and returned.  This payload may be populated with
316  * appropriate information by the caller.
317  *
318  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
319  * desired. */
320 void *
321 snap_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
322              const uint8_t eth_src[ETH_ADDR_LEN],
323              unsigned int oui, uint16_t snap_type, size_t size)
324 {
325     struct eth_header *eth;
326     struct llc_snap_header *llc_snap;
327     void *payload;
328
329     /* Compose basic packet structure.  (We need the payload size to stick into
330      * the 802.2 header.) */
331     ofpbuf_clear(b);
332     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
333                              + LLC_SNAP_HEADER_LEN + size);
334     ofpbuf_reserve(b, VLAN_HEADER_LEN);
335     eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
336     llc_snap = ofpbuf_put_zeros(b, LLC_SNAP_HEADER_LEN);
337     payload = ofpbuf_put_uninit(b, size);
338
339     /* Compose 802.2 header. */
340     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
341     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
342     eth->eth_type = htons(b->size - ETH_HEADER_LEN);
343
344     /* Compose LLC, SNAP headers. */
345     llc_snap->llc.llc_dsap = LLC_DSAP_SNAP;
346     llc_snap->llc.llc_ssap = LLC_SSAP_SNAP;
347     llc_snap->llc.llc_cntl = LLC_CNTL_SNAP;
348     llc_snap->snap.snap_org[0] = oui >> 16;
349     llc_snap->snap.snap_org[1] = oui >> 8;
350     llc_snap->snap.snap_org[2] = oui;
351     llc_snap->snap.snap_type = htons(snap_type);
352
353     return payload;
354 }