162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/eth_media.c: Ethernet bearer support for TIPC
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2001-2007, 2013-2014, Ericsson AB
562306a36Sopenharmony_ci * Copyright (c) 2005-2008, 2011-2013, Wind River Systems
662306a36Sopenharmony_ci * All rights reserved.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
962306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1262306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1362306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1462306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1562306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
1662306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
1762306a36Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
1862306a36Sopenharmony_ci *    this software without specific prior written permission.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2162306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2262306a36Sopenharmony_ci * Software Foundation.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2562306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2662306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2762306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2862306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2962306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3062306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3162306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3262306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3362306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3462306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include "core.h"
3862306a36Sopenharmony_ci#include "bearer.h"
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Convert Ethernet address (media address format) to string */
4162306a36Sopenharmony_cistatic int tipc_eth_addr2str(struct tipc_media_addr *addr,
4262306a36Sopenharmony_ci			     char *strbuf, int bufsz)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	if (bufsz < 18)	/* 18 = strlen("aa:bb:cc:dd:ee:ff\0") */
4562306a36Sopenharmony_ci		return 1;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	sprintf(strbuf, "%pM", addr->value);
4862306a36Sopenharmony_ci	return 0;
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* Convert from media address format to discovery message addr format */
5262306a36Sopenharmony_cistatic int tipc_eth_addr2msg(char *msg, struct tipc_media_addr *addr)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
5562306a36Sopenharmony_ci	msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_ETH;
5662306a36Sopenharmony_ci	memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, addr->value, ETH_ALEN);
5762306a36Sopenharmony_ci	return 0;
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/* Convert raw mac address format to media addr format */
6162306a36Sopenharmony_cistatic int tipc_eth_raw2addr(struct tipc_bearer *b,
6262306a36Sopenharmony_ci			     struct tipc_media_addr *addr,
6362306a36Sopenharmony_ci			     const char *msg)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	memset(addr, 0, sizeof(*addr));
6662306a36Sopenharmony_ci	ether_addr_copy(addr->value, msg);
6762306a36Sopenharmony_ci	addr->media_id = TIPC_MEDIA_TYPE_ETH;
6862306a36Sopenharmony_ci	addr->broadcast = is_broadcast_ether_addr(addr->value);
6962306a36Sopenharmony_ci	return 0;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* Convert discovery msg addr format to Ethernet media addr format */
7362306a36Sopenharmony_cistatic int tipc_eth_msg2addr(struct tipc_bearer *b,
7462306a36Sopenharmony_ci			     struct tipc_media_addr *addr,
7562306a36Sopenharmony_ci			     char *msg)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	/* Skip past preamble: */
7862306a36Sopenharmony_ci	msg += TIPC_MEDIA_ADDR_OFFSET;
7962306a36Sopenharmony_ci	return tipc_eth_raw2addr(b, addr, msg);
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/* Ethernet media registration info */
8362306a36Sopenharmony_cistruct tipc_media eth_media_info = {
8462306a36Sopenharmony_ci	.send_msg	= tipc_l2_send_msg,
8562306a36Sopenharmony_ci	.enable_media	= tipc_enable_l2_media,
8662306a36Sopenharmony_ci	.disable_media	= tipc_disable_l2_media,
8762306a36Sopenharmony_ci	.addr2str	= tipc_eth_addr2str,
8862306a36Sopenharmony_ci	.addr2msg	= tipc_eth_addr2msg,
8962306a36Sopenharmony_ci	.msg2addr	= tipc_eth_msg2addr,
9062306a36Sopenharmony_ci	.raw2addr	= tipc_eth_raw2addr,
9162306a36Sopenharmony_ci	.priority	= TIPC_DEF_LINK_PRI,
9262306a36Sopenharmony_ci	.tolerance	= TIPC_DEF_LINK_TOL,
9362306a36Sopenharmony_ci	.min_win	= TIPC_DEF_LINK_WIN,
9462306a36Sopenharmony_ci	.max_win	= TIPC_MAX_LINK_WIN,
9562306a36Sopenharmony_ci	.type_id	= TIPC_MEDIA_TYPE_ETH,
9662306a36Sopenharmony_ci	.hwaddr_len	= ETH_ALEN,
9762306a36Sopenharmony_ci	.name		= "eth"
9862306a36Sopenharmony_ci};
99