forked from cbsd/cbsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnet.subr
155 lines (126 loc) · 3.55 KB
/
vnet.subr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Network interface=related funcion, for vnet feature
init_vnet()
{
if ! kldstat -qm if_bridge; then
kldload if_bridge
fi
gw_enable
}
# $1 - nicname (eg: bridge)
# if nicname=epair we search as epairXa
# show first available nic by type
# example:
# ttt=$( find_first_freenic bridge )
# return 1 when error
find_first_freenic()
{
local _i _epair _A _num=0
[ -z $1 ] && return 1
[ "$1" = "epair" ] && _epair="a"
for _i in $( /sbin/ifconfig -l ); do
case "${_i}" in
${1}*${_epair})
/sbin/ifconfig ${1}${_epair}${_num} >/dev/null 2>&1
[ $? -eq 1 ] && echo "${1}${_num}" && return 0
_num=$(( _num + 1 ))
[ ${_num} -gt 1000 ] && return 1
;;
esac
done
echo "${1}${_num}"
}
# $1 - nicname (eg: bridge)
# show nicX if exist
# example:
# for i in $( show_all_nic_by_name bridge ); do
# echo ${i}
# done
show_all_nic_by_name()
{
local _i _mynic _A _epair
[ -z $1 ] && return 1
[ "${1}" = "epair" ] && _epair="a" # we check only one of pair
for _i in $(/sbin/ifconfig -l); do
case "${_i}" in
${1}*${_epair})
echo ${_i}
;;
esac
done
[ -n "${_mynic}" ] && echo "${_mynic}"
return 0
}
gw_enable()
{
sysctl -qn net.inet.ip.forwarding=1 > /dev/null 2>&1
sysctl -qn net.inet6.ip6.forwarding=1 > /dev/null 2>&1
}
# cbsd store uplink interface in description area ;)
# this func extract and show this
# $1 - iface (eg: bridge0)
# example:
# ttt=$( get_device_uplink bridge0 )
get_device_uplink()
{
local _desc
[ -z "${1}" ] && return 1
_desc=$( /sbin/ifconfig ${1} 2>/dev/null|awk '/description:/{print $2}' )
[ -n "${_desc}" ] && echo "${_desc}"
}
# function search for available bridges in system who have uplink to ${interface}
# when not - create one
# out bridge name when it exist
# $1 - type (bridge or epair) , $2 - uplink interface
# return 1 when error
# example:
# if ! ttt=$( get_my_device bridge nfe0 ); then
# echo "Error: $ttt"
# fi
get_my_device()
{
local _i _uplink _firstfree _ret _test _dev _desc
[ -z "${1}" ] && echo "set device" && return 1
[ -z "${2}" ] && echo "No uplink" && return 1
_dev=$1
_desc=$2
for _i in $( show_all_nic_by_name ${_dev} ); do
_uplink=$( get_device_uplink ${_i} )
[ "${_uplink}" = "${_desc}" ] && echo "${_i}" && return 0
done
# we need for new bridge with ${_dev} uplink
_firstfree=$( find_first_freenic ${_dev} )
[ -z "${_firstfree}" ] && echo "Cant find first available ${_dev}" && return 1
if [ "${_dev}" = "bridge" ]; then
_test=$( ifconfig ${_firstfree} create addm ${_desc} up description ${_desc} 2>&1 )
_ret=$?
[ ${_ret} -ne 0 ] && echo "${_test}" && return 1
fi
echo "${_firstfree}"
}
# create epair and switch epairXa to bridge $1
# out of created epair
get_my_epair()
{
local _firstfree
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic epair )
[ $? -eq 1 ] && echo "No free available epair" && return 1
ifconfig ${_firstfree} create >/dev/null 2>/dev/null
ifconfig ${1} addm ${_firstfree}a
ifconfig ${_firstfree}a up
echo ${_firstfree}
}
# create tap and attach to to bridge $1
# out of created tap
get_my_tap()
{
local _firstfree
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic tap )
[ $? -eq 1 ] && echo "No free available tap" && return 1
ifconfig ${_firstfree} create >/dev/null 2>/dev/null
ifconfig ${1} addm ${_firstfree}
# sysctl -w net.link.tap.up_on_open=1 > /dev/null 2>&1
ifconfig ${_firstfree} up
echo ${_firstfree}
}