Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/wtap/wtap_hal/hal.c
/*- | /*- | ||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||||
* | * | ||||
* Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB | * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* Copyright (c) 2023 The FreeBSD Foundation | |||||
* | |||||
* Portions of this software were developed by En-Wei Wu | |||||
* under sponsorship from the FreeBSD Foundation. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer, | * notice, this list of conditions and the following disclaimer, | ||||
* without modification. | * without modification. | ||||
* 2. Redistributions in binary form must reproduce at minimum a disclaimer | * 2. Redistributions in binary form must reproduce at minimum a disclaimer | ||||
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any | * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | #endif | ||||
} | } | ||||
} | } | ||||
void | void | ||||
init_hal(struct wtap_hal *hal) | init_hal(struct wtap_hal *hal) | ||||
{ | { | ||||
DWTAP_PRINTF("%s\n", __func__); | DWTAP_PRINTF("%s\n", __func__); | ||||
memset(hal->hal_devs_set, 0, sizeof(uint32_t) * ARRAY_SIZE); | |||||
mtx_init(&hal->hal_mtx, "wtap_hal mtx", NULL, MTX_DEF | MTX_RECURSE); | mtx_init(&hal->hal_mtx, "wtap_hal mtx", NULL, MTX_DEF | MTX_RECURSE); | ||||
hal->hal_md = (struct wtap_medium *)malloc(sizeof(struct wtap_medium), | hal->hal_md = (struct wtap_medium *)malloc(sizeof(struct wtap_medium), | ||||
M_WTAP, M_NOWAIT | M_ZERO); | M_WTAP, M_NOWAIT | M_ZERO); | ||||
init_medium(hal->hal_md); | init_medium(hal->hal_md); | ||||
/* register event handler for packets */ | /* register event handler for packets */ | ||||
TASK_INIT(&hal->hal_md->tx_handler->proc, 0, hal_tx_proc, hal); | TASK_INIT(&hal->hal_md->tx_handler->proc, 0, hal_tx_proc, hal); | ||||
Show All 26 Lines | deinit_hal(struct wtap_hal *hal) | ||||
deinit_medium(hal->hal_md); | deinit_medium(hal->hal_md); | ||||
free(hal->hal_md, M_WTAP); | free(hal->hal_md, M_WTAP); | ||||
mtx_destroy(&hal->hal_mtx); | mtx_destroy(&hal->hal_mtx); | ||||
} | } | ||||
int32_t | int32_t | ||||
new_wtap(struct wtap_hal *hal, int32_t id) | new_wtap(struct wtap_hal *hal, int32_t id) | ||||
{ | { | ||||
static const uint8_t mac_pool[64][IEEE80211_ADDR_LEN] = { | static const uint8_t mac_pool[MAX_NBR_WTAP][IEEE80211_ADDR_LEN] = { | ||||
{0,152,154,152,150,151}, | {0,152,154,152,150,151}, | ||||
{0,152,154,152,150,152}, | {0,152,154,152,150,152}, | ||||
{0,152,154,152,150,153}, | {0,152,154,152,150,153}, | ||||
{0,152,154,152,150,154}, | {0,152,154,152,150,154}, | ||||
{0,152,154,152,150,155}, | {0,152,154,152,150,155}, | ||||
{0,152,154,152,150,156}, | {0,152,154,152,150,156}, | ||||
{0,152,154,152,150,157}, | {0,152,154,152,150,157}, | ||||
{0,152,154,152,150,158}, | {0,152,154,152,150,158}, | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | static const uint8_t mac_pool[MAX_NBR_WTAP][IEEE80211_ADDR_LEN] = { | ||||
{0,152,154,152,157,154}, | {0,152,154,152,157,154}, | ||||
{0,152,154,152,157,155}, | {0,152,154,152,157,155}, | ||||
{0,152,154,152,157,156}, | {0,152,154,152,157,156}, | ||||
{0,152,154,152,157,157}, | {0,152,154,152,157,157}, | ||||
{0,152,154,152,157,158} | {0,152,154,152,157,158} | ||||
}; | }; | ||||
DWTAP_PRINTF("%s\n", __func__); | DWTAP_PRINTF("%s\n", __func__); | ||||
/* When id < 0, automate the assignmanet of wtap device by | |||||
* finding the least significant clear bit in hal->hal_devs_set | |||||
*/ | |||||
if (id < 0) { | |||||
int i; | |||||
for (i = 0; i < MAX_NBR_WTAP; i++) { | |||||
if (!isset(hal->hal_devs_set, i)) { | |||||
id = i; | |||||
break; | |||||
} | |||||
} | |||||
if (i == MAX_NBR_WTAP) { | |||||
DWTAP_PRINTF("%s: wtap device is full\n", __func__); | |||||
return -1; | |||||
} | |||||
} | |||||
uint8_t const *macaddr = mac_pool[id]; | uint8_t const *macaddr = mac_pool[id]; | ||||
if(hal->hal_devs[id] != NULL){ | if (isset(hal->hal_devs_set, id)) { | ||||
printf("error, wtap_id=%d already created\n", id); | printf("error, wtap_id=%d already created\n", id); | ||||
return -1; | return -1; | ||||
} | } | ||||
if (id >= MAX_NBR_WTAP) { | |||||
DWTAP_PRINTF("error, wtap_id=%d must be between 0 and 63\n", id); | |||||
return -1; | |||||
} | |||||
setbit(hal->hal_devs_set, id); | |||||
hal->hal_devs[id] = (struct wtap_softc *)malloc( | hal->hal_devs[id] = (struct wtap_softc *)malloc( | ||||
sizeof(struct wtap_softc), M_WTAP, M_NOWAIT | M_ZERO); | sizeof(struct wtap_softc), M_WTAP, M_NOWAIT | M_ZERO); | ||||
hal->hal_devs[id]->sc_md = hal->hal_md; | hal->hal_devs[id]->sc_md = hal->hal_md; | ||||
hal->hal_devs[id]->id = id; | hal->hal_devs[id]->id = id; | ||||
hal->hal_devs[id]->hal = hal; | hal->hal_devs[id]->hal = hal; | ||||
snprintf(hal->hal_devs[id]->name, sizeof(hal->hal_devs[id]->name), | snprintf(hal->hal_devs[id]->name, sizeof(hal->hal_devs[id]->name), | ||||
"wtap%d", id); | "wtap%d", id); | ||||
mtx_init(&hal->hal_devs[id]->sc_mtx, "wtap_softc mtx", NULL, | mtx_init(&hal->hal_devs[id]->sc_mtx, "wtap_softc mtx", NULL, | ||||
MTX_DEF | MTX_RECURSE); | MTX_DEF | MTX_RECURSE); | ||||
if(wtap_attach(hal->hal_devs[id], macaddr)){ | if(wtap_attach(hal->hal_devs[id], macaddr)){ | ||||
printf("%s, cant alloc new wtap\n", __func__); | printf("%s, cant alloc new wtap\n", __func__); | ||||
return -1; | return -1; | ||||
} | } | ||||
return 0; | return id; | ||||
} | } | ||||
int32_t | int32_t | ||||
free_wtap(struct wtap_hal *hal, int32_t id) | free_wtap(struct wtap_hal *hal, int32_t id) | ||||
{ | { | ||||
DWTAP_PRINTF("%s\n", __func__); | DWTAP_PRINTF("%s\n", __func__); | ||||
if(hal->hal_devs[id] == NULL){ | |||||
if (id < 0 || id >= MAX_NBR_WTAP) { | |||||
DWTAP_PRINTF("error, wtap_id=%d must be between 0 and 63\n", id); | |||||
return -1; | |||||
} | |||||
if(!isset(hal->hal_devs_set, id)){ | |||||
printf("error, wtap_id=%d never created\n", id); | printf("error, wtap_id=%d never created\n", id); | ||||
return -1; | return -1; | ||||
} | } | ||||
if(wtap_detach(hal->hal_devs[id])) | if(wtap_detach(hal->hal_devs[id])) | ||||
printf("%s, cant alloc new wtap\n", __func__); | printf("%s, cant alloc new wtap\n", __func__); | ||||
clrbit(hal->hal_devs_set, id); | |||||
mtx_destroy(&hal->hal_devs[id]->sc_mtx); | mtx_destroy(&hal->hal_devs[id]->sc_mtx); | ||||
free(hal->hal_devs[id], M_WTAP); | free(hal->hal_devs[id], M_WTAP); | ||||
hal->hal_devs[id] = NULL; | hal->hal_devs[id] = NULL; | ||||
return 0; | return 0; | ||||
} | } | ||||
void | void | ||||
wtap_hal_timer_intr(void *arg) | wtap_hal_timer_intr(void *arg) | ||||
Show All 27 Lines |