Commit 062b3e1b authored by Samuel Mendoza-Jonas's avatar Samuel Mendoza-Jonas Committed by David S. Miller

net/ncsi: Refactor MAC, VLAN filters

The NCSI driver defines a generic ncsi_channel_filter struct that can be
used to store arbitrarily formatted filters, and several generic methods
of accessing data stored in such a filter.
However in both the driver and as defined in the NCSI specification
there are only two actual filters: VLAN ID filters and MAC address
filters. The splitting of the MAC filter into unicast, multicast, and
mixed is also technically not necessary as these are stored in the same
location in hardware.

To save complexity, particularly in the set up and accessing of these
generic filters, remove them in favour of two specific structs. These
can be acted on directly and do not need several generic helper
functions to use.

This also fixes a memory error found by KASAN on ARM32 (which is not
upstream yet), where response handlers accessing a filter's data field
could write past allocated memory.

[  114.926512] ==================================================================
[  114.933861] BUG: KASAN: slab-out-of-bounds in ncsi_configure_channel+0x4b8/0xc58
[  114.941304] Read of size 2 at addr 94888558 by task kworker/0:2/546
[  114.947593]
[  114.949146] CPU: 0 PID: 546 Comm: kworker/0:2 Not tainted 4.16.0-rc6-00119-ge156398bfcad #13
...
[  115.170233] The buggy address belongs to the object at 94888540
[  115.170233]  which belongs to the cache kmalloc-32 of size 32
[  115.181917] The buggy address is located 24 bytes inside of
[  115.181917]  32-byte region [94888540, 94888560)
[  115.192115] The buggy address belongs to the page:
[  115.196943] page:9eeac100 count:1 mapcount:0 mapping:94888000 index:0x94888fc1
[  115.204200] flags: 0x100(slab)
[  115.207330] raw: 00000100 94888000 94888fc1 0000003f 00000001 9eea2014 9eecaa74 96c003e0
[  115.215444] page dumped because: kasan: bad access detected
[  115.221036]
[  115.222544] Memory state around the buggy address:
[  115.227384]  94888400: fb fb fb fb fc fc fc fc 04 fc fc fc fc fc fc fc
[  115.233959]  94888480: 00 00 00 fc fc fc fc fc 00 04 fc fc fc fc fc fc
[  115.240529] >94888500: 00 00 04 fc fc fc fc fc 00 00 04 fc fc fc fc fc
[  115.247077]                                             ^
[  115.252523]  94888580: 00 04 fc fc fc fc fc fc 06 fc fc fc fc fc fc fc
[  115.259093]  94888600: 00 00 06 fc fc fc fc fc 00 00 04 fc fc fc fc fc
[  115.265639] ==================================================================
Reported-by: default avatarJoel Stanley <joel@jms.id.au>
Signed-off-by: default avatarSamuel Mendoza-Jonas <sam@mendozajonas.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c210f7b4
...@@ -68,15 +68,6 @@ enum { ...@@ -68,15 +68,6 @@ enum {
NCSI_MODE_MAX NCSI_MODE_MAX
}; };
enum {
NCSI_FILTER_BASE = 0,
NCSI_FILTER_VLAN = 0,
NCSI_FILTER_UC,
NCSI_FILTER_MC,
NCSI_FILTER_MIXED,
NCSI_FILTER_MAX
};
struct ncsi_channel_version { struct ncsi_channel_version {
u32 version; /* Supported BCD encoded NCSI version */ u32 version; /* Supported BCD encoded NCSI version */
u32 alpha2; /* Supported BCD encoded NCSI version */ u32 alpha2; /* Supported BCD encoded NCSI version */
...@@ -98,11 +89,18 @@ struct ncsi_channel_mode { ...@@ -98,11 +89,18 @@ struct ncsi_channel_mode {
u32 data[8]; /* Data entries */ u32 data[8]; /* Data entries */
}; };
struct ncsi_channel_filter { struct ncsi_channel_mac_filter {
u32 index; /* Index of channel filters */ u8 n_uc;
u32 total; /* Total entries in the filter table */ u8 n_mc;
u64 bitmap; /* Bitmap of valid entries */ u8 n_mixed;
u32 data[]; /* Data for the valid entries */ u64 bitmap;
unsigned char *addrs;
};
struct ncsi_channel_vlan_filter {
u8 n_vids;
u64 bitmap;
u16 *vids;
}; };
struct ncsi_channel_stats { struct ncsi_channel_stats {
...@@ -186,7 +184,9 @@ struct ncsi_channel { ...@@ -186,7 +184,9 @@ struct ncsi_channel {
struct ncsi_channel_version version; struct ncsi_channel_version version;
struct ncsi_channel_cap caps[NCSI_CAP_MAX]; struct ncsi_channel_cap caps[NCSI_CAP_MAX];
struct ncsi_channel_mode modes[NCSI_MODE_MAX]; struct ncsi_channel_mode modes[NCSI_MODE_MAX];
struct ncsi_channel_filter *filters[NCSI_FILTER_MAX]; /* Filtering Settings */
struct ncsi_channel_mac_filter mac_filter;
struct ncsi_channel_vlan_filter vlan_filter;
struct ncsi_channel_stats stats; struct ncsi_channel_stats stats;
struct { struct {
struct timer_list timer; struct timer_list timer;
...@@ -320,10 +320,6 @@ extern spinlock_t ncsi_dev_lock; ...@@ -320,10 +320,6 @@ extern spinlock_t ncsi_dev_lock;
list_for_each_entry_rcu(nc, &np->channels, node) list_for_each_entry_rcu(nc, &np->channels, node)
/* Resources */ /* Resources */
u32 *ncsi_get_filter(struct ncsi_channel *nc, int table, int index);
int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data);
int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data);
int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index);
void ncsi_start_channel_monitor(struct ncsi_channel *nc); void ncsi_start_channel_monitor(struct ncsi_channel *nc);
void ncsi_stop_channel_monitor(struct ncsi_channel *nc); void ncsi_stop_channel_monitor(struct ncsi_channel *nc);
struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np, struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
......
...@@ -27,125 +27,6 @@ ...@@ -27,125 +27,6 @@
LIST_HEAD(ncsi_dev_list); LIST_HEAD(ncsi_dev_list);
DEFINE_SPINLOCK(ncsi_dev_lock); DEFINE_SPINLOCK(ncsi_dev_lock);
static inline int ncsi_filter_size(int table)
{
int sizes[] = { 2, 6, 6, 6 };
BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX);
if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX)
return -EINVAL;
return sizes[table];
}
u32 *ncsi_get_filter(struct ncsi_channel *nc, int table, int index)
{
struct ncsi_channel_filter *ncf;
int size;
ncf = nc->filters[table];
if (!ncf)
return NULL;
size = ncsi_filter_size(table);
if (size < 0)
return NULL;
return ncf->data + size * index;
}
/* Find the first active filter in a filter table that matches the given
* data parameter. If data is NULL, this returns the first active filter.
*/
int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
{
struct ncsi_channel_filter *ncf;
void *bitmap;
int index, size;
unsigned long flags;
ncf = nc->filters[table];
if (!ncf)
return -ENXIO;
size = ncsi_filter_size(table);
if (size < 0)
return size;
spin_lock_irqsave(&nc->lock, flags);
bitmap = (void *)&ncf->bitmap;
index = -1;
while ((index = find_next_bit(bitmap, ncf->total, index + 1))
< ncf->total) {
if (!data || !memcmp(ncf->data + size * index, data, size)) {
spin_unlock_irqrestore(&nc->lock, flags);
return index;
}
}
spin_unlock_irqrestore(&nc->lock, flags);
return -ENOENT;
}
int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
{
struct ncsi_channel_filter *ncf;
int index, size;
void *bitmap;
unsigned long flags;
size = ncsi_filter_size(table);
if (size < 0)
return size;
index = ncsi_find_filter(nc, table, data);
if (index >= 0)
return index;
ncf = nc->filters[table];
if (!ncf)
return -ENODEV;
spin_lock_irqsave(&nc->lock, flags);
bitmap = (void *)&ncf->bitmap;
do {
index = find_next_zero_bit(bitmap, ncf->total, 0);
if (index >= ncf->total) {
spin_unlock_irqrestore(&nc->lock, flags);
return -ENOSPC;
}
} while (test_and_set_bit(index, bitmap));
memcpy(ncf->data + size * index, data, size);
spin_unlock_irqrestore(&nc->lock, flags);
return index;
}
int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
{
struct ncsi_channel_filter *ncf;
int size;
void *bitmap;
unsigned long flags;
size = ncsi_filter_size(table);
if (size < 0)
return size;
ncf = nc->filters[table];
if (!ncf || index >= ncf->total)
return -ENODEV;
spin_lock_irqsave(&nc->lock, flags);
bitmap = (void *)&ncf->bitmap;
if (test_and_clear_bit(index, bitmap))
memset(ncf->data + size * index, 0, size);
spin_unlock_irqrestore(&nc->lock, flags);
return 0;
}
static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down) static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
{ {
struct ncsi_dev *nd = &ndp->ndev; struct ncsi_dev *nd = &ndp->ndev;
...@@ -339,20 +220,13 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id) ...@@ -339,20 +220,13 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
static void ncsi_remove_channel(struct ncsi_channel *nc) static void ncsi_remove_channel(struct ncsi_channel *nc)
{ {
struct ncsi_package *np = nc->package; struct ncsi_package *np = nc->package;
struct ncsi_channel_filter *ncf;
unsigned long flags; unsigned long flags;
int i;
/* Release filters */
spin_lock_irqsave(&nc->lock, flags); spin_lock_irqsave(&nc->lock, flags);
for (i = 0; i < NCSI_FILTER_MAX; i++) {
ncf = nc->filters[i];
if (!ncf)
continue;
nc->filters[i] = NULL; /* Release filters */
kfree(ncf); kfree(nc->mac_filter.addrs);
} kfree(nc->vlan_filter.vids);
nc->state = NCSI_CHANNEL_INACTIVE; nc->state = NCSI_CHANNEL_INACTIVE;
spin_unlock_irqrestore(&nc->lock, flags); spin_unlock_irqrestore(&nc->lock, flags);
...@@ -670,32 +544,26 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp) ...@@ -670,32 +544,26 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc, static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
struct ncsi_cmd_arg *nca) struct ncsi_cmd_arg *nca)
{ {
struct ncsi_channel_vlan_filter *ncf;
unsigned long flags;
void *bitmap;
int index; int index;
u32 *data;
u16 vid; u16 vid;
index = ncsi_find_filter(nc, NCSI_FILTER_VLAN, NULL); ncf = &nc->vlan_filter;
if (index < 0) { bitmap = &ncf->bitmap;
/* Filter table empty */
return -1;
}
data = ncsi_get_filter(nc, NCSI_FILTER_VLAN, index); spin_lock_irqsave(&nc->lock, flags);
if (!data) { index = find_next_bit(bitmap, ncf->n_vids, 0);
netdev_err(ndp->ndev.dev, if (index >= ncf->n_vids) {
"NCSI: failed to retrieve filter %d\n", index); spin_unlock_irqrestore(&nc->lock, flags);
/* Set the VLAN id to 0 - this will still disable the entry in return -1;
* the filter table, but we won't know what it was.
*/
vid = 0;
} else {
vid = *(u16 *)data;
} }
vid = ncf->vids[index];
netdev_printk(KERN_DEBUG, ndp->ndev.dev, clear_bit(index, bitmap);
"NCSI: removed vlan tag %u at index %d\n", ncf->vids[index] = 0;
vid, index + 1); spin_unlock_irqrestore(&nc->lock, flags);
ncsi_remove_filter(nc, NCSI_FILTER_VLAN, index);
nca->type = NCSI_PKT_CMD_SVF; nca->type = NCSI_PKT_CMD_SVF;
nca->words[1] = vid; nca->words[1] = vid;
...@@ -711,45 +579,55 @@ static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc, ...@@ -711,45 +579,55 @@ static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc, static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
struct ncsi_cmd_arg *nca) struct ncsi_cmd_arg *nca)
{ {
struct ncsi_channel_vlan_filter *ncf;
struct vlan_vid *vlan = NULL; struct vlan_vid *vlan = NULL;
int index = 0; unsigned long flags;
int i, index;
void *bitmap;
u16 vid;
if (list_empty(&ndp->vlan_vids))
return -1;
ncf = &nc->vlan_filter;
bitmap = &ncf->bitmap;
spin_lock_irqsave(&nc->lock, flags);
rcu_read_lock();
list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) { list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
index = ncsi_find_filter(nc, NCSI_FILTER_VLAN, &vlan->vid); vid = vlan->vid;
if (index < 0) { for (i = 0; i < ncf->n_vids; i++)
/* New tag to add */ if (ncf->vids[i] == vid) {
netdev_printk(KERN_DEBUG, ndp->ndev.dev, vid = 0;
"NCSI: new vlan id to set: %u\n",
vlan->vid);
break; break;
} }
netdev_printk(KERN_DEBUG, ndp->ndev.dev, if (vid)
"vid %u already at filter pos %d\n", break;
vlan->vid, index);
} }
rcu_read_unlock();
if (!vlan || index >= 0) { if (!vid) {
netdev_printk(KERN_DEBUG, ndp->ndev.dev, /* No VLAN ID is not set */
"no vlan ids left to set\n"); spin_unlock_irqrestore(&nc->lock, flags);
return -1; return -1;
} }
index = ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan->vid); index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
if (index < 0) { if (index < 0 || index >= ncf->n_vids) {
netdev_err(ndp->ndev.dev,
"Failed to add new VLAN tag, error %d\n", index);
if (index == -ENOSPC)
netdev_err(ndp->ndev.dev, netdev_err(ndp->ndev.dev,
"Channel %u already has all VLAN filters set\n", "Channel %u already has all VLAN filters set\n",
nc->id); nc->id);
spin_unlock_irqrestore(&nc->lock, flags);
return -1; return -1;
} }
netdev_printk(KERN_DEBUG, ndp->ndev.dev, ncf->vids[index] = vid;
"NCSI: set vid %u in packet, index %u\n", set_bit(index, bitmap);
vlan->vid, index + 1); spin_unlock_irqrestore(&nc->lock, flags);
nca->type = NCSI_PKT_CMD_SVF; nca->type = NCSI_PKT_CMD_SVF;
nca->words[1] = vlan->vid; nca->words[1] = vid;
/* HW filter index starts at 1 */ /* HW filter index starts at 1 */
nca->bytes[6] = index + 1; nca->bytes[6] = index + 1;
nca->bytes[7] = 0x01; nca->bytes[7] = 0x01;
......
...@@ -58,10 +58,9 @@ static int ncsi_write_channel_info(struct sk_buff *skb, ...@@ -58,10 +58,9 @@ static int ncsi_write_channel_info(struct sk_buff *skb,
struct ncsi_dev_priv *ndp, struct ncsi_dev_priv *ndp,
struct ncsi_channel *nc) struct ncsi_channel *nc)
{ {
struct nlattr *vid_nest; struct ncsi_channel_vlan_filter *ncf;
struct ncsi_channel_filter *ncf;
struct ncsi_channel_mode *m; struct ncsi_channel_mode *m;
u32 *data; struct nlattr *vid_nest;
int i; int i;
nla_put_u32(skb, NCSI_CHANNEL_ATTR_ID, nc->id); nla_put_u32(skb, NCSI_CHANNEL_ATTR_ID, nc->id);
...@@ -79,18 +78,13 @@ static int ncsi_write_channel_info(struct sk_buff *skb, ...@@ -79,18 +78,13 @@ static int ncsi_write_channel_info(struct sk_buff *skb,
vid_nest = nla_nest_start(skb, NCSI_CHANNEL_ATTR_VLAN_LIST); vid_nest = nla_nest_start(skb, NCSI_CHANNEL_ATTR_VLAN_LIST);
if (!vid_nest) if (!vid_nest)
return -ENOMEM; return -ENOMEM;
ncf = nc->filters[NCSI_FILTER_VLAN]; ncf = &nc->vlan_filter;
i = -1; i = -1;
if (ncf) { while ((i = find_next_bit((void *)&ncf->bitmap, ncf->n_vids,
while ((i = find_next_bit((void *)&ncf->bitmap, ncf->total, i + 1)) < ncf->n_vids) {
i + 1)) < ncf->total) { if (ncf->vids[i])
data = ncsi_get_filter(nc, NCSI_FILTER_VLAN, i);
/* Uninitialised channels will have 'zero' vlan ids */
if (!data || !*data)
continue;
nla_put_u16(skb, NCSI_CHANNEL_ATTR_VLAN_ID, nla_put_u16(skb, NCSI_CHANNEL_ATTR_VLAN_ID,
*(u16 *)data); ncf->vids[i]);
}
} }
nla_nest_end(skb, vid_nest); nla_nest_end(skb, vid_nest);
......
...@@ -334,9 +334,9 @@ static int ncsi_rsp_handler_svf(struct ncsi_request *nr) ...@@ -334,9 +334,9 @@ static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
struct ncsi_rsp_pkt *rsp; struct ncsi_rsp_pkt *rsp;
struct ncsi_dev_priv *ndp = nr->ndp; struct ncsi_dev_priv *ndp = nr->ndp;
struct ncsi_channel *nc; struct ncsi_channel *nc;
struct ncsi_channel_filter *ncf; struct ncsi_channel_vlan_filter *ncf;
unsigned short vlan; unsigned long flags;
int ret; void *bitmap;
/* Find the package and channel */ /* Find the package and channel */
rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp); rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
...@@ -346,22 +346,23 @@ static int ncsi_rsp_handler_svf(struct ncsi_request *nr) ...@@ -346,22 +346,23 @@ static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
return -ENODEV; return -ENODEV;
cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd); cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
ncf = nc->filters[NCSI_FILTER_VLAN]; ncf = &nc->vlan_filter;
if (!ncf) if (cmd->index > ncf->n_vids)
return -ENOENT;
if (cmd->index >= ncf->total)
return -ERANGE; return -ERANGE;
/* Add or remove the VLAN filter */ /* Add or remove the VLAN filter. Remember HW indexes from 1 */
spin_lock_irqsave(&nc->lock, flags);
bitmap = &ncf->bitmap;
if (!(cmd->enable & 0x1)) { if (!(cmd->enable & 0x1)) {
/* HW indexes from 1 */ if (test_and_clear_bit(cmd->index - 1, bitmap))
ret = ncsi_remove_filter(nc, NCSI_FILTER_VLAN, cmd->index - 1); ncf->vids[cmd->index - 1] = 0;
} else { } else {
vlan = ntohs(cmd->vlan); set_bit(cmd->index - 1, bitmap);
ret = ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan); ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
} }
spin_unlock_irqrestore(&nc->lock, flags);
return ret; return 0;
} }
static int ncsi_rsp_handler_ev(struct ncsi_request *nr) static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
...@@ -422,8 +423,12 @@ static int ncsi_rsp_handler_sma(struct ncsi_request *nr) ...@@ -422,8 +423,12 @@ static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
struct ncsi_rsp_pkt *rsp; struct ncsi_rsp_pkt *rsp;
struct ncsi_dev_priv *ndp = nr->ndp; struct ncsi_dev_priv *ndp = nr->ndp;
struct ncsi_channel *nc; struct ncsi_channel *nc;
struct ncsi_channel_filter *ncf; struct ncsi_channel_mac_filter *ncf;
unsigned long flags;
void *bitmap; void *bitmap;
bool enabled;
int index;
/* Find the package and channel */ /* Find the package and channel */
rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp); rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
...@@ -436,31 +441,23 @@ static int ncsi_rsp_handler_sma(struct ncsi_request *nr) ...@@ -436,31 +441,23 @@ static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
* isn't supported yet. * isn't supported yet.
*/ */
cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd); cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
switch (cmd->at_e >> 5) { enabled = cmd->at_e & 0x1;
case 0x0: /* UC address */ ncf = &nc->mac_filter;
ncf = nc->filters[NCSI_FILTER_UC]; bitmap = &ncf->bitmap;
break;
case 0x1: /* MC address */
ncf = nc->filters[NCSI_FILTER_MC];
break;
default:
return -EINVAL;
}
/* Sanity check on the filter */ if (cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
if (!ncf)
return -ENOENT;
else if (cmd->index >= ncf->total)
return -ERANGE; return -ERANGE;
bitmap = &ncf->bitmap; index = (cmd->index - 1) * ETH_ALEN;
if (cmd->at_e & 0x1) { spin_lock_irqsave(&nc->lock, flags);
set_bit(cmd->index, bitmap); if (enabled) {
memcpy(ncf->data + 6 * cmd->index, cmd->mac, 6); set_bit(cmd->index - 1, bitmap);
memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
} else { } else {
clear_bit(cmd->index, bitmap); clear_bit(cmd->index - 1, bitmap);
memset(ncf->data + 6 * cmd->index, 0, 6); memset(&ncf->addrs[index], 0, ETH_ALEN);
} }
spin_unlock_irqrestore(&nc->lock, flags);
return 0; return 0;
} }
...@@ -631,9 +628,7 @@ static int ncsi_rsp_handler_gc(struct ncsi_request *nr) ...@@ -631,9 +628,7 @@ static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
struct ncsi_rsp_gc_pkt *rsp; struct ncsi_rsp_gc_pkt *rsp;
struct ncsi_dev_priv *ndp = nr->ndp; struct ncsi_dev_priv *ndp = nr->ndp;
struct ncsi_channel *nc; struct ncsi_channel *nc;
struct ncsi_channel_filter *ncf; size_t size;
size_t size, entry_size;
int cnt, i;
/* Find the channel */ /* Find the channel */
rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp); rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
...@@ -655,64 +650,40 @@ static int ncsi_rsp_handler_gc(struct ncsi_request *nr) ...@@ -655,64 +650,40 @@ static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode & nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
NCSI_CAP_VLAN_MASK; NCSI_CAP_VLAN_MASK;
/* Build filters */ size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
for (i = 0; i < NCSI_FILTER_MAX; i++) { nc->mac_filter.addrs = kzalloc(size, GFP_KERNEL);
switch (i) { if (!nc->mac_filter.addrs)
case NCSI_FILTER_VLAN:
cnt = rsp->vlan_cnt;
entry_size = 2;
break;
case NCSI_FILTER_MIXED:
cnt = rsp->mixed_cnt;
entry_size = 6;
break;
case NCSI_FILTER_MC:
cnt = rsp->mc_cnt;
entry_size = 6;
break;
case NCSI_FILTER_UC:
cnt = rsp->uc_cnt;
entry_size = 6;
break;
default:
continue;
}
if (!cnt || nc->filters[i])
continue;
size = sizeof(*ncf) + cnt * entry_size;
ncf = kzalloc(size, GFP_ATOMIC);
if (!ncf) {
pr_warn("%s: Cannot alloc filter table (%d)\n",
__func__, i);
return -ENOMEM; return -ENOMEM;
} nc->mac_filter.n_uc = rsp->uc_cnt;
nc->mac_filter.n_mc = rsp->mc_cnt;
ncf->index = i; nc->mac_filter.n_mixed = rsp->mixed_cnt;
ncf->total = cnt;
if (i == NCSI_FILTER_VLAN) { nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
/* Set VLAN filters active so they are cleared in sizeof(*nc->vlan_filter.vids),
* first configuration state GFP_KERNEL);
if (!nc->vlan_filter.vids)
return -ENOMEM;
/* Set VLAN filters active so they are cleared in the first
* configuration state
*/ */
ncf->bitmap = U64_MAX; nc->vlan_filter.bitmap = U64_MAX;
} else { nc->vlan_filter.n_vids = rsp->vlan_cnt;
ncf->bitmap = 0x0ul;
}
nc->filters[i] = ncf;
}
return 0; return 0;
} }
static int ncsi_rsp_handler_gp(struct ncsi_request *nr) static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
{ {
struct ncsi_rsp_gp_pkt *rsp; struct ncsi_channel_vlan_filter *ncvf;
struct ncsi_channel_mac_filter *ncmf;
struct ncsi_dev_priv *ndp = nr->ndp; struct ncsi_dev_priv *ndp = nr->ndp;
struct ncsi_rsp_gp_pkt *rsp;
struct ncsi_channel *nc; struct ncsi_channel *nc;
unsigned short enable, vlan; unsigned short enable;
unsigned char *pdata; unsigned char *pdata;
int table, i; unsigned long flags;
void *bitmap;
int i;
/* Find the channel */ /* Find the channel */
rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp); rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
...@@ -746,36 +717,33 @@ static int ncsi_rsp_handler_gp(struct ncsi_request *nr) ...@@ -746,36 +717,33 @@ static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
/* MAC addresses filter table */ /* MAC addresses filter table */
pdata = (unsigned char *)rsp + 48; pdata = (unsigned char *)rsp + 48;
enable = rsp->mac_enable; enable = rsp->mac_enable;
ncmf = &nc->mac_filter;
spin_lock_irqsave(&nc->lock, flags);
bitmap = &ncmf->bitmap;
for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) { for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
if (i >= (nc->filters[NCSI_FILTER_UC]->total +
nc->filters[NCSI_FILTER_MC]->total))
table = NCSI_FILTER_MIXED;
else if (i >= nc->filters[NCSI_FILTER_UC]->total)
table = NCSI_FILTER_MC;
else
table = NCSI_FILTER_UC;
if (!(enable & (0x1 << i))) if (!(enable & (0x1 << i)))
continue; clear_bit(i, bitmap);
else
if (ncsi_find_filter(nc, table, pdata) >= 0) set_bit(i, bitmap);
continue;
ncsi_add_filter(nc, table, pdata); memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
} }
spin_unlock_irqrestore(&nc->lock, flags);
/* VLAN filter table */ /* VLAN filter table */
enable = ntohs(rsp->vlan_enable); enable = ntohs(rsp->vlan_enable);
ncvf = &nc->vlan_filter;
bitmap = &ncvf->bitmap;
spin_lock_irqsave(&nc->lock, flags);
for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) { for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
if (!(enable & (0x1 << i))) if (!(enable & (0x1 << i)))
continue; clear_bit(i, bitmap);
else
vlan = ntohs(*(__be16 *)pdata); set_bit(i, bitmap);
if (ncsi_find_filter(nc, NCSI_FILTER_VLAN, &vlan) >= 0)
continue;
ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan); ncvf->vids[i] = ntohs(*(__be16 *)pdata);
} }
spin_unlock_irqrestore(&nc->lock, flags);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment