ofproto: Fix use-after-free error when ports disappear.
[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 /* Insert VLAN header according to given TCI. Packet passed must be Ethernet
79  * packet.
80  *
81  * Also sets 'packet->l2' to point to the new Ethernet header. */
82 void
83 eth_push_vlan(struct ofpbuf *packet, ovs_be16 tci)
84 {
85     struct eth_header *eh = packet->data;
86     struct vlan_eth_header *veh;
87
88     /* Insert new 802.1Q header. */
89     struct vlan_eth_header tmp;
90     memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
91     memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
92     tmp.veth_type = htons(ETH_TYPE_VLAN);
93     tmp.veth_tci = tci;
94     tmp.veth_next_type = eh->eth_type;
95
96     veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
97     memcpy(veh, &tmp, sizeof tmp);
98
99     packet->l2 = packet->data;
100 }
101
102 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
103  * that it specifies, that is, the number of 1-bits in 'netmask'.  'netmask'
104  * must be a CIDR netmask (see ip_is_cidr()). */
105 int
106 ip_count_cidr_bits(ovs_be32 netmask)
107 {
108     assert(ip_is_cidr(netmask));
109     return 32 - ctz(ntohl(netmask));
110 }
111
112 void
113 ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
114 {
115     ds_put_format(s, IP_FMT, IP_ARGS(&ip));
116     if (mask != htonl(UINT32_MAX)) {
117         if (ip_is_cidr(mask)) {
118             ds_put_format(s, "/%d", ip_count_cidr_bits(mask));
119         } else {
120             ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
121         }
122     }
123 }
124
125
126 /* Stores the string representation of the IPv6 address 'addr' into the
127  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
128  * bytes long. */
129 void
130 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
131 {
132     inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
133 }
134
135 void
136 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
137 {
138     char *dst;
139
140     ds_reserve(string, string->length + INET6_ADDRSTRLEN);
141
142     dst = string->string + string->length;
143     format_ipv6_addr(dst, addr);
144     string->length += strlen(dst);
145 }
146
147 void
148 print_ipv6_masked(struct ds *s, const struct in6_addr *addr,
149                   const struct in6_addr *mask)
150 {
151     print_ipv6_addr(s, addr);
152     if (mask && !ipv6_mask_is_exact(mask)) {
153         if (ipv6_is_cidr(mask)) {
154             int cidr_bits = ipv6_count_cidr_bits(mask);
155             ds_put_format(s, "/%d", cidr_bits);
156         } else {
157             ds_put_char(s, '/');
158             print_ipv6_addr(s, mask);
159         }
160     }
161 }
162
163 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
164                                  const struct in6_addr *b)
165 {
166     int i;
167     struct in6_addr dst;
168
169 #ifdef s6_addr32
170     for (i=0; i<4; i++) {
171         dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
172     }
173 #else
174     for (i=0; i<16; i++) {
175         dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
176     }
177 #endif
178
179     return dst;
180 }
181
182 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
183  * low-order 0-bits. */
184 struct in6_addr
185 ipv6_create_mask(int mask)
186 {
187     struct in6_addr netmask;
188     uint8_t *netmaskp = &netmask.s6_addr[0];
189
190     memset(&netmask, 0, sizeof netmask);
191     while (mask > 8) {
192         *netmaskp = 0xff;
193         netmaskp++;
194         mask -= 8;
195     }
196
197     if (mask) {
198         *netmaskp = 0xff << (8 - mask);
199     }
200
201     return netmask;
202 }
203
204 /* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6
205  * address that it specifies, that is, the number of 1-bits in 'netmask'.
206  * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()). */
207 int
208 ipv6_count_cidr_bits(const struct in6_addr *netmask)
209 {
210     int i;
211     int count = 0;
212     const uint8_t *netmaskp = &netmask->s6_addr[0];
213
214     assert(ipv6_is_cidr(netmask));
215
216     for (i=0; i<16; i++) {
217         if (netmaskp[i] == 0xff) {
218             count += 8;
219         } else {
220             uint8_t nm;
221
222             for(nm = netmaskp[i]; nm; nm <<= 1) {
223                 count++;
224             }
225             break;
226         }
227
228     }
229
230     return count;
231 }
232
233 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
234  * high-order 1-bits and 128-N low-order 0-bits. */
235 bool
236 ipv6_is_cidr(const struct in6_addr *netmask)
237 {
238     const uint8_t *netmaskp = &netmask->s6_addr[0];
239     int i;
240
241     for (i=0; i<16; i++) {
242         if (netmaskp[i] != 0xff) {
243             uint8_t x = ~netmaskp[i];
244             if (x & (x + 1)) {
245                 return false;
246             }
247             while (++i < 16) {
248                 if (netmaskp[i]) {
249                     return false;
250                 }
251             }
252         }
253     }
254
255     return true;
256 }
257
258 /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
259  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
260  * in 'b' and returned.  This payload may be populated with appropriate
261  * information by the caller.
262  *
263  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
264  * desired. */
265 void *
266 eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
267             const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
268             size_t size)
269 {
270     void *data;
271     struct eth_header *eth;
272
273     ofpbuf_clear(b);
274
275     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
276     ofpbuf_reserve(b, VLAN_HEADER_LEN);
277     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
278     data = ofpbuf_put_uninit(b, size);
279
280     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
281     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
282     eth->eth_type = htons(eth_type);
283
284     return data;
285 }
286
287 /* Populates 'b' with an Ethernet LLC+SNAP packet headed with the given
288  * 'eth_dst', 'eth_src', 'snap_org', and 'snap_type'.  A payload of 'size'
289  * bytes is allocated in 'b' and returned.  This payload may be populated with
290  * appropriate information by the caller.
291  *
292  * The returned packet has enough headroom to insert an 802.1Q VLAN header if
293  * desired. */
294 void *
295 snap_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
296              const uint8_t eth_src[ETH_ADDR_LEN],
297              unsigned int oui, uint16_t snap_type, size_t size)
298 {
299     struct eth_header *eth;
300     struct llc_snap_header *llc_snap;
301     void *payload;
302
303     /* Compose basic packet structure.  (We need the payload size to stick into
304      * the 802.2 header.) */
305     ofpbuf_clear(b);
306     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
307                              + LLC_SNAP_HEADER_LEN + size);
308     ofpbuf_reserve(b, VLAN_HEADER_LEN);
309     eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
310     llc_snap = ofpbuf_put_zeros(b, LLC_SNAP_HEADER_LEN);
311     payload = ofpbuf_put_uninit(b, size);
312
313     /* Compose 802.2 header. */
314     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
315     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
316     eth->eth_type = htons(b->size - ETH_HEADER_LEN);
317
318     /* Compose LLC, SNAP headers. */
319     llc_snap->llc.llc_dsap = LLC_DSAP_SNAP;
320     llc_snap->llc.llc_ssap = LLC_SSAP_SNAP;
321     llc_snap->llc.llc_cntl = LLC_CNTL_SNAP;
322     llc_snap->snap.snap_org[0] = oui >> 16;
323     llc_snap->snap.snap_org[1] = oui >> 8;
324     llc_snap->snap.snap_org[2] = oui;
325     llc_snap->snap.snap_type = htons(snap_type);
326
327     return payload;
328 }