/* * net/tipc/bearer.c: TIPC bearer code * * Copyright (c) 1996-2006, 2013-2016, Ericsson AB * Copyright (c) 2004-2006, 2010-2013, Wind River Systems * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the names of the copyright holders nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * GNU General Public License ("GPL") version 2 as published by the Free * Software Foundation. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE.
*/
/** * tipc_media_find - locates specified media object by name * @name: name to locate
*/ struct tipc_media *tipc_media_find(constchar *name)
{
u32 i;
for (i = 0; media_info_array[i] != NULL; i++) { if (!strcmp(media_info_array[i]->name, name)) break;
} return media_info_array[i];
}
/** * media_find_id - locates specified media object by type identifier * @type: type identifier to locate
*/ staticstruct tipc_media *media_find_id(u8 type)
{
u32 i;
for (i = 0; media_info_array[i] != NULL; i++) { if (media_info_array[i]->type_id == type) break;
} return media_info_array[i];
}
/** * tipc_media_addr_printf - record media address in print buffer * @buf: output buffer * @len: output buffer size remaining * @a: input media address
*/ int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
{ char addr_str[MAX_ADDR_STR]; struct tipc_media *m; int ret;
m = media_find_id(a->media_id);
if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str); else {
u32 i;
ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id); for (i = 0; i < sizeof(a->value); i++)
ret += scnprintf(buf + ret, len - ret, "-%x", a->value[i]);
} return ret;
}
/** * bearer_name_validate - validate & (optionally) deconstruct bearer name * @name: ptr to bearer name string * @name_parts: ptr to area for bearer name components (or NULL if not needed) * * Return: 1 if bearer name is valid, otherwise 0.
*/ staticint bearer_name_validate(constchar *name, struct tipc_bearer_names *name_parts)
{ char name_copy[TIPC_MAX_BEARER_NAME]; char *media_name; char *if_name;
u32 media_len;
u32 if_len;
/* copy bearer name & ensure length is OK */ if (strscpy(name_copy, name, TIPC_MAX_BEARER_NAME) < 0) return 0;
/* ensure all component parts of bearer name are present */
media_name = name_copy;
if_name = strchr(media_name, ':'); if (if_name == NULL) return 0;
*(if_name++) = 0;
media_len = if_name - media_name;
if_len = strlen(if_name) + 1;
/* validate component parts of bearer name */ if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
(if_len <= 1) || (if_len > TIPC_MAX_IF_NAME)) return 0;
/* return bearer name components, if necessary */ if (name_parts) { if (strscpy(name_parts->media_name, media_name,
TIPC_MAX_MEDIA_NAME) < 0) return 0; if (strscpy(name_parts->if_name, if_name,
TIPC_MAX_IF_NAME) < 0) return 0;
} return 1;
}
/** * tipc_bearer_find - locates bearer object with matching bearer name * @net: the applicable net namespace * @name: bearer name to locate
*/ struct tipc_bearer *tipc_bearer_find(struct net *net, constchar *name)
{ struct tipc_net *tn = tipc_net(net); struct tipc_bearer *b;
u32 i;
for (i = 0; i < MAX_BEARERS; i++) {
b = rtnl_dereference(tn->bearer_list[i]); if (b && (!strcmp(b->name, name))) return b;
} return NULL;
}
/* tipc_bearer_get_name - get the bearer name from its id. * @net: network namespace * @name: a pointer to the buffer where the name will be stored. * @bearer_id: the id to get the name from.
*/ int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
{ struct tipc_net *tn = tipc_net(net); struct tipc_bearer *b;
if (bearer_id >= MAX_BEARERS) return -EINVAL;
b = rtnl_dereference(tn->bearer_list[bearer_id]); if (!b) return -EINVAL;
m = tipc_media_find(b_names.media_name); if (!m) {
errstr = "media not registered";
NL_SET_ERR_MSG(extack, "Media not registered"); goto rejected;
}
if (prio == TIPC_MEDIA_LINK_PRI)
prio = m->priority;
/* Check new bearer vs existing ones and find free bearer id if any */
bearer_id = MAX_BEARERS;
i = MAX_BEARERS; while (i-- != 0) {
b = rtnl_dereference(tn->bearer_list[i]); if (!b) {
bearer_id = i; continue;
} if (!strcmp(name, b->name)) {
errstr = "already enabled";
NL_SET_ERR_MSG(extack, "Already enabled"); goto rejected;
}
if (b->priority == prio &&
(++with_this_prio > 2)) {
pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
name, prio);
if (prio == TIPC_MIN_LINK_PRI) {
errstr = "cannot adjust to lower";
NL_SET_ERR_MSG(extack, "Cannot adjust to lower"); goto rejected;
}
pr_warn("Bearer <%s>: trying with adjusted priority\n",
name);
prio--;
bearer_id = MAX_BEARERS;
i = MAX_BEARERS;
with_this_prio = 1;
}
}
/* tipc_disable_l2_media - detach TIPC bearer from an L2 interface * @b: the target bearer * * Mark L2 bearer as inactive so that incoming buffers are thrown away
*/ void tipc_disable_l2_media(struct tipc_bearer *b)
{ struct net_device *dev;
/** * tipc_l2_send_msg - send a TIPC packet out over an L2 interface * @net: the associated network namespace * @skb: the packet to be sent * @b: the bearer through which the packet is to be sent * @dest: peer destination address
*/ int tipc_l2_send_msg(struct net *net, struct sk_buff *skb, struct tipc_bearer *b, struct tipc_media_addr *dest)
{ struct net_device *dev; int delta;
dev = (struct net_device *)rcu_dereference(b->media_ptr); if (!dev) return 0;
/** * tipc_l2_rcv_msg - handle incoming TIPC message from an interface * @skb: the received message * @dev: the net device that the packet was received on * @pt: the packet_type structure which was used to register this handler * @orig_dev: the original receive net device in case the device is a bond * * Accept only packets explicitly sent to this node, or broadcast packets; * ignores packets sent using interface multicast, and traffic sent to other * nodes (which can happen if interface is running in promiscuous mode).
*/ staticint tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{ struct tipc_bearer *b;
/** * tipc_l2_device_event - handle device events from network device * @nb: the context of the notification * @evt: the type of event * @ptr: the net device that the event was on * * This function is called by the Ethernet driver in case of link * change event.
*/ staticint tipc_l2_device_event(struct notifier_block *nb, unsignedlong evt, void *ptr)
{ struct net_device *dev = netdev_notifier_info_to_dev(ptr); struct net *net = dev_net(dev); struct tipc_bearer *b;
b = rtnl_dereference(dev->tipc_ptr); if (!b) return NOTIFY_DONE;
trace_tipc_l2_device_event(dev, b, evt); switch (evt) { case NETDEV_CHANGE: if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
test_and_set_bit_lock(0, &b->up); break;
}
fallthrough; case NETDEV_GOING_DOWN:
clear_bit_unlock(0, &b->up);
tipc_reset_bearer(net, b); break; case NETDEV_UP:
test_and_set_bit_lock(0, &b->up); break; case NETDEV_CHANGEMTU: if (tipc_mtu_bad(dev)) {
bearer_disable(net, b); break;
}
b->mtu = dev->mtu;
tipc_reset_bearer(net, b); break; case NETDEV_CHANGEADDR:
b->media->raw2addr(b, &b->addr,
(constchar *)dev->dev_addr);
tipc_reset_bearer(net, b); break; case NETDEV_UNREGISTER: case NETDEV_CHANGENAME:
bearer_disable(net, b); break;
} return NOTIFY_OK;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.