]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/packets.c
2 * Copyright (c) 2009, 2010 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 <netinet/in.h>
23 #include "byte-order.h"
24 #include "dynamic-string.h"
27 const struct in6_addr in6addr_exact
= IN6ADDR_EXACT_INIT
;
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.
33 * Rejects an all-zeros dpid as invalid. */
35 dpid_from_string(const char *s
, uint64_t *dpidp
)
37 *dpidp
= (strlen(s
) == 16 && strspn(s
, "0123456789abcdefABCDEF") == 16
38 ? strtoull(s
, NULL
, 16)
44 eth_addr_from_string(const char *s
, uint8_t ea
[ETH_ADDR_LEN
])
46 if (sscanf(s
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(ea
))
47 == ETH_ADDR_SCAN_COUNT
) {
50 memset(ea
, 0, ETH_ADDR_LEN
);
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.
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
66 compose_benign_packet(struct ofpbuf
*b
, const char *tag
, uint16_t snap_type
,
67 const uint8_t eth_src
[ETH_ADDR_LEN
])
69 struct eth_header
*eth
;
70 struct llc_snap_header
*llc_snap
;
72 /* Compose basic packet structure. (We need the payload size to stick into
73 * the 802.2 header.) */
75 eth
= ofpbuf_put_zeros(b
, ETH_HEADER_LEN
);
76 llc_snap
= ofpbuf_put_zeros(b
, LLC_SNAP_HEADER_LEN
);
77 ofpbuf_put(b
, tag
, strlen(tag
) + 1); /* Includes null byte. */
78 ofpbuf_put(b
, eth_src
, ETH_ADDR_LEN
);
80 /* Compose 802.2 header. */
81 memcpy(eth
->eth_dst
, eth_addr_broadcast
, ETH_ADDR_LEN
);
82 memcpy(eth
->eth_src
, eth_src
, ETH_ADDR_LEN
);
83 eth
->eth_type
= htons(b
->size
- ETH_HEADER_LEN
);
85 /* Compose LLC, SNAP headers. */
86 llc_snap
->llc
.llc_dsap
= LLC_DSAP_SNAP
;
87 llc_snap
->llc
.llc_ssap
= LLC_SSAP_SNAP
;
88 llc_snap
->llc
.llc_cntl
= LLC_CNTL_SNAP
;
89 memcpy(llc_snap
->snap
.snap_org
, "\x00\x23\x20", 3);
90 llc_snap
->snap
.snap_type
= htons(snap_type
);
93 /* Stores the string representation of the IPv6 address 'addr' into the
94 * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
97 format_ipv6_addr(char *addr_str
, const struct in6_addr
*addr
)
99 inet_ntop(AF_INET6
, addr
, addr_str
, INET6_ADDRSTRLEN
);
103 print_ipv6_addr(struct ds
*string
, const struct in6_addr
*addr
)
105 char addr_str
[INET6_ADDRSTRLEN
];
107 format_ipv6_addr(addr_str
, addr
);
108 ds_put_format(string
, "%s", addr_str
);
111 struct in6_addr
ipv6_addr_bitand(const struct in6_addr
*a
,
112 const struct in6_addr
*b
)
118 for (i
=0; i
<4; i
++) {
119 dst
.s6_addr32
[i
] = a
->s6_addr32
[i
] & b
->s6_addr32
[i
];
122 for (i
=0; i
<16; i
++) {
123 dst
.s6_addr
[i
] = a
->s6_addr
[i
] & b
->s6_addr
[i
];
130 /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
131 * low-order 0-bits. */
133 ipv6_create_mask(int mask
)
135 struct in6_addr netmask
;
136 uint8_t *netmaskp
= &netmask
.s6_addr
[0];
138 memset(&netmask
, 0, sizeof netmask
);
146 *netmaskp
= 0xff << (8 - mask
);
152 /* Given the IPv6 netmask 'netmask', returns the number of bits of the
153 * IPv6 address that it wildcards. 'netmask' must be a CIDR netmask (see
154 * ipv6_is_cidr()). */
156 ipv6_count_cidr_bits(const struct in6_addr
*netmask
)
160 const uint8_t *netmaskp
= &netmask
->s6_addr
[0];
162 assert(ipv6_is_cidr(netmask
));
164 for (i
=0; i
<16; i
++) {
165 if (netmaskp
[i
] == 0xff) {
170 for(nm
= netmaskp
[i
]; nm
; nm
<<= 1) {
182 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
183 * high-order 1-bits and 128-N low-order 0-bits. */
185 ipv6_is_cidr(const struct in6_addr
*netmask
)
187 const uint8_t *netmaskp
= &netmask
->s6_addr
[0];
190 for (i
=0; i
<16; i
++) {
191 if (netmaskp
[i
] != 0xff) {
192 uint8_t x
= ~netmaskp
[i
];
207 /* Populates 'b' with a LACP packet containing 'pdu' with source address
210 compose_lacp_packet(struct ofpbuf
*b
, const uint8_t eth_src
[ETH_ADDR_LEN
],
211 const struct lacp_pdu
*pdu
)
213 struct eth_header
*eth
;
214 struct lacp_pdu
*eth_pdu
;
218 ofpbuf_prealloc_tailroom(b
, ETH_HEADER_LEN
+ LACP_PDU_LEN
);
219 eth
= ofpbuf_put_zeros(b
, ETH_HEADER_LEN
);
220 eth_pdu
= ofpbuf_put(b
, pdu
, LACP_PDU_LEN
);
222 memcpy(eth
->eth_dst
, eth_addr_lacp
, ETH_ADDR_LEN
);
223 memcpy(eth
->eth_src
, eth_src
, ETH_ADDR_LEN
);
224 eth
->eth_type
= htons(ETH_TYPE_LACP
);
227 /* Populates 'pdu' with a LACP PDU comprised of 'actor' and 'partner'. */
229 compose_lacp_pdu(const struct lacp_info
*actor
,
230 const struct lacp_info
*partner
, struct lacp_pdu
*pdu
)
232 memset(pdu
, 0, sizeof *pdu
);
241 pdu
->partner_type
= 2;
242 pdu
->partner_len
= 20;
243 pdu
->partner
= *partner
;
245 pdu
->collector_type
= 3;
246 pdu
->collector_len
= 16;
247 pdu
->collector_delay
= htons(0);
250 /* Parses 'b' which represents a packet containing a LACP PDU. This function
251 * returns NULL if 'b' is malformed, or does not represent a LACP PDU format
252 * supported by OVS. Otherwise, it returns a pointer to the lacp_pdu contained
254 const struct lacp_pdu
*
255 parse_lacp_packet(const struct ofpbuf
*b
)
257 const struct lacp_pdu
*pdu
;
259 pdu
= ofpbuf_at(b
, (uint8_t *)b
->l3
- (uint8_t *)b
->data
, LACP_PDU_LEN
);
261 if (pdu
&& pdu
->subtype
== 1
262 && pdu
->actor_type
== 1 && pdu
->actor_len
== 20
263 && pdu
->partner_type
== 2 && pdu
->partner_len
== 20) {