packets: Fix potential use-after-free in compose_benign_packet().
[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 /* Stores the string representation of the IPv6 address 'addr' into the
79  * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
80  * bytes long. */
81 void
82 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
83 {
84     inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
85 }
86
87 void
88 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
89 {
90     char addr_str[INET6_ADDRSTRLEN];
91
92     format_ipv6_addr(addr_str, addr);
93     ds_put_format(string, "%s", addr_str);
94 }
95
96 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
97                                  const struct in6_addr *b)
98 {
99     int i;
100     struct in6_addr dst;
101
102 #ifdef s6_addr32
103     for (i=0; i<4; i++) {
104         dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
105     }
106 #else
107     for (i=0; i<16; i++) {
108         dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
109     }
110 #endif
111
112     return dst;
113 }
114
115 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
116  * low-order 0-bits. */
117 struct in6_addr
118 ipv6_create_mask(int mask)
119 {
120     struct in6_addr netmask;
121     uint8_t *netmaskp = &netmask.s6_addr[0];
122
123     memset(&netmask, 0, sizeof netmask);
124     while (mask > 8) {
125         *netmaskp = 0xff;
126         netmaskp++;
127         mask -= 8;
128     }
129
130     if (mask) {
131         *netmaskp = 0xff << (8 - mask);
132     }
133
134     return netmask;
135 }
136
137 /* Given the IPv6 netmask 'netmask', returns the number of bits of the
138  * IPv6 address that it wildcards.  'netmask' must be a CIDR netmask (see
139  * ipv6_is_cidr()). */
140 int
141 ipv6_count_cidr_bits(const struct in6_addr *netmask)
142 {
143     int i;
144     int count = 0;
145     const uint8_t *netmaskp = &netmask->s6_addr[0];
146
147     assert(ipv6_is_cidr(netmask));
148
149     for (i=0; i<16; i++) {
150         if (netmaskp[i] == 0xff) {
151             count += 8;
152         } else {
153             uint8_t nm;
154
155             for(nm = netmaskp[i]; nm; nm <<= 1) {
156                 count++;
157             }
158             break;
159         }
160
161     }
162
163     return count;
164 }
165
166
167 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
168  * high-order 1-bits and 128-N low-order 0-bits. */
169 bool
170 ipv6_is_cidr(const struct in6_addr *netmask)
171 {
172     const uint8_t *netmaskp = &netmask->s6_addr[0];
173     int i;
174
175     for (i=0; i<16; i++) {
176         if (netmaskp[i] != 0xff) {
177             uint8_t x = ~netmaskp[i];
178             if (x & (x + 1)) {
179                 return false;
180             }
181             while (++i < 16) {
182                 if (netmaskp[i]) {
183                     return false;
184                 }
185             }
186         }
187     }
188
189     return true;
190 }
191
192 /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
193  * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
194  * in 'b' and returned.  This payload may be populated with appropriate
195  * information by the caller. */
196 void *
197 eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
198             const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
199             size_t size)
200 {
201     void *data;
202     struct eth_header *eth;
203
204     ofpbuf_clear(b);
205
206     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + size);
207     eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
208     data = ofpbuf_put_uninit(b, size);
209
210     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
211     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
212     eth->eth_type = htons(eth_type);
213
214     return data;
215 }
216
217 /* Populates 'b' with an Ethernet LLC+SNAP packet headed with the given
218  * 'eth_dst', 'eth_src', 'snap_org', and 'snap_type'.  A payload of 'size'
219  * bytes is allocated in 'b' and returned.  This payload may be populated with
220  * appropriate information by the caller. */
221 void *
222 snap_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
223              const uint8_t eth_src[ETH_ADDR_LEN],
224              unsigned int oui, uint16_t snap_type, size_t size)
225 {
226     struct eth_header *eth;
227     struct llc_snap_header *llc_snap;
228     void *payload;
229
230     /* Compose basic packet structure.  (We need the payload size to stick into
231      * the 802.2 header.) */
232     ofpbuf_clear(b);
233     ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + LLC_SNAP_HEADER_LEN + size);
234     eth = ofpbuf_put_zeros(b, ETH_HEADER_LEN);
235     llc_snap = ofpbuf_put_zeros(b, LLC_SNAP_HEADER_LEN);
236     payload = ofpbuf_put_uninit(b, size);
237
238     /* Compose 802.2 header. */
239     memcpy(eth->eth_dst, eth_dst, ETH_ADDR_LEN);
240     memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
241     eth->eth_type = htons(b->size - ETH_HEADER_LEN);
242
243     /* Compose LLC, SNAP headers. */
244     llc_snap->llc.llc_dsap = LLC_DSAP_SNAP;
245     llc_snap->llc.llc_ssap = LLC_SSAP_SNAP;
246     llc_snap->llc.llc_cntl = LLC_CNTL_SNAP;
247     llc_snap->snap.snap_org[0] = oui >> 16;
248     llc_snap->snap.snap_org[1] = oui >> 8;
249     llc_snap->snap.snap_org[2] = oui;
250     llc_snap->snap.snap_type = htons(snap_type);
251
252     return payload;
253 }
254
255 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
256 void
257 compose_lacp_pdu(const struct lacp_info *actor,
258                  const struct lacp_info *partner, struct lacp_pdu *pdu)
259 {
260     memset(pdu, 0, sizeof *pdu);
261
262     pdu->subtype = 1;
263     pdu->version = 1;
264
265     pdu->actor_type = 1;
266     pdu->actor_len = 20;
267     pdu->actor = *actor;
268
269     pdu->partner_type = 2;
270     pdu->partner_len = 20;
271     pdu->partner = *partner;
272
273     pdu->collector_type = 3;
274     pdu->collector_len = 16;
275     pdu->collector_delay = htons(0);
276 }
277
278 /* Parses 'b' which represents a packet containing a LACP PDU.  This function
279  * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
280  * supported by OVS.  Otherwise, it returns a pointer to the lacp_pdu contained
281  * within 'b'. */
282 const struct lacp_pdu *
283 parse_lacp_packet(const struct ofpbuf *b)
284 {
285     const struct lacp_pdu *pdu;
286
287     pdu = ofpbuf_at(b, (uint8_t *)b->l3 - (uint8_t *)b->data, LACP_PDU_LEN);
288
289     if (pdu && pdu->subtype == 1
290         && pdu->actor_type == 1 && pdu->actor_len == 20
291         && pdu->partner_type == 2 && pdu->partner_len == 20) {
292         return pdu;
293     } else {
294         return NULL;
295     }
296 }