2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include "byte-order.h"
25 #include "dynamic-string.h"
28 const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT;
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.
34 * Rejects an all-zeros dpid as invalid. */
36 dpid_from_string(const char *s, uint64_t *dpidp)
38 *dpidp = (strlen(s) == 16 && strspn(s, "0123456789abcdefABCDEF") == 16
39 ? strtoull(s, NULL, 16)
45 eth_addr_from_string(const char *s, uint8_t ea[ETH_ADDR_LEN])
47 if (sscanf(s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))
48 == ETH_ADDR_SCAN_COUNT) {
51 memset(ea, 0, ETH_ADDR_LEN);
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.
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
67 compose_benign_packet(struct ofpbuf *b, const char *tag, uint16_t snap_type,
68 const uint8_t eth_src[ETH_ADDR_LEN])
70 size_t tag_size = strlen(tag) + 1;
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);
79 /* Insert VLAN header according to given TCI. Packet passed must be Ethernet
80 * packet. Ignores the CFI bit of 'tci' using 0 instead.
82 * Also sets 'packet->l2' to point to the new Ethernet header. */
84 eth_push_vlan(struct ofpbuf *packet, ovs_be16 tci)
86 struct eth_header *eh = packet->data;
87 struct vlan_eth_header *veh;
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;
97 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
98 memcpy(veh, &tmp, sizeof tmp);
100 packet->l2 = packet->data;
103 /* Removes outermost VLAN header (if any is present) from 'packet'.
105 * 'packet->l2' must initially point to 'packet''s Ethernet header. */
107 eth_pop_vlan(struct ofpbuf *packet)
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;
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;
118 ofpbuf_pull(packet, VLAN_HEADER_LEN);
119 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
120 memcpy(packet->data, &tmp, sizeof tmp);
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'. */
128 eth_from_hex(const char *hex, struct ofpbuf **packetp)
130 struct ofpbuf *packet;
132 packet = *packetp = ofpbuf_new(strlen(hex) / 2);
134 if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
135 ofpbuf_delete(packet);
137 return "Trailing garbage in packet data";
140 if (packet->size < ETH_HEADER_LEN) {
141 ofpbuf_delete(packet);
143 return "Packet data too short for Ethernet";
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()). */
153 ip_count_cidr_bits(ovs_be32 netmask)
155 assert(ip_is_cidr(netmask));
156 return 32 - ctz(ntohl(netmask));
160 ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
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));
167 ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
173 /* Stores the string representation of the IPv6 address 'addr' into the
174 * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
177 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
179 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
183 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
187 ds_reserve(string, string->length + INET6_ADDRSTRLEN);
189 dst = string->string + string->length;
190 format_ipv6_addr(dst, addr);
191 string->length += strlen(dst);
195 print_ipv6_masked(struct ds *s, const struct in6_addr *addr,
196 const struct in6_addr *mask)
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);
205 print_ipv6_addr(s, mask);
210 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
211 const struct in6_addr *b)
217 for (i=0; i<4; i++) {
218 dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
221 for (i=0; i<16; i++) {
222 dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
229 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
230 * low-order 0-bits. */
232 ipv6_create_mask(int mask)
234 struct in6_addr netmask;
235 uint8_t *netmaskp = &netmask.s6_addr[0];
237 memset(&netmask, 0, sizeof netmask);
245 *netmaskp = 0xff << (8 - mask);
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()). */
255 ipv6_count_cidr_bits(const struct in6_addr *netmask)
259 const uint8_t *netmaskp = &netmask->s6_addr[0];
261 assert(ipv6_is_cidr(netmask));
263 for (i=0; i<16; i++) {
264 if (netmaskp[i] == 0xff) {
269 for(nm = netmaskp[i]; nm; nm <<= 1) {
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. */
283 ipv6_is_cidr(const struct in6_addr *netmask)
285 const uint8_t *netmaskp = &netmask->s6_addr[0];
288 for (i=0; i<16; i++) {
289 if (netmaskp[i] != 0xff) {
290 uint8_t x = ~netmaskp[i];
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.
311 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
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,
319 struct eth_header *eth;
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);
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);
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.
343 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
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)
350 struct eth_header *eth;
351 struct llc_snap_header *llc_snap;
354 /* Compose basic packet structure. (We need the payload size to stick into
355 * the 802.2 header.) */
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);
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);
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);