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
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);
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 /* 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()). */
128 ip_count_cidr_bits(ovs_be32 netmask)
130 assert(ip_is_cidr(netmask));
131 return 32 - ctz(ntohl(netmask));
135 ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
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));
142 ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
148 /* Stores the string representation of the IPv6 address 'addr' into the
149 * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
152 format_ipv6_addr(char *addr_str, const struct in6_addr *addr)
154 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
158 print_ipv6_addr(struct ds *string, const struct in6_addr *addr)
162 ds_reserve(string, string->length + INET6_ADDRSTRLEN);
164 dst = string->string + string->length;
165 format_ipv6_addr(dst, addr);
166 string->length += strlen(dst);
170 print_ipv6_masked(struct ds *s, const struct in6_addr *addr,
171 const struct in6_addr *mask)
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);
180 print_ipv6_addr(s, mask);
185 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
186 const struct in6_addr *b)
192 for (i=0; i<4; i++) {
193 dst.s6_addr32[i] = a->s6_addr32[i] & b->s6_addr32[i];
196 for (i=0; i<16; i++) {
197 dst.s6_addr[i] = a->s6_addr[i] & b->s6_addr[i];
204 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
205 * low-order 0-bits. */
207 ipv6_create_mask(int mask)
209 struct in6_addr netmask;
210 uint8_t *netmaskp = &netmask.s6_addr[0];
212 memset(&netmask, 0, sizeof netmask);
220 *netmaskp = 0xff << (8 - mask);
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()). */
230 ipv6_count_cidr_bits(const struct in6_addr *netmask)
234 const uint8_t *netmaskp = &netmask->s6_addr[0];
236 assert(ipv6_is_cidr(netmask));
238 for (i=0; i<16; i++) {
239 if (netmaskp[i] == 0xff) {
244 for(nm = netmaskp[i]; nm; nm <<= 1) {
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. */
258 ipv6_is_cidr(const struct in6_addr *netmask)
260 const uint8_t *netmaskp = &netmask->s6_addr[0];
263 for (i=0; i<16; i++) {
264 if (netmaskp[i] != 0xff) {
265 uint8_t x = ~netmaskp[i];
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.
286 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
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,
294 struct eth_header *eth;
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);
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);
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.
318 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
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)
325 struct eth_header *eth;
326 struct llc_snap_header *llc_snap;
329 /* Compose basic packet structure. (We need the payload size to stick into
330 * the 802.2 header.) */
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);
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);
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);