Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 19 | /** |
| 20 | * checkPNI - starts with the system manager input CLI command in ONOS console that enable/disable the PPPoE service. |
| 21 | */ |
| 22 | var Promise=require('bluebird'); |
| 23 | var execAsync=Promise.promisify(require('child_process').exec); |
| 24 | var fs=require('fs'); |
| 25 | var os=require('os'); |
| 26 | var ifaces = os.networkInterfaces(); |
| 27 | var oriGw = '10.6.1.129'; |
| 28 | var dnsFwdr = '8.8.8.8'; |
| 29 | |
| 30 | |
| 31 | //ip6tables -I FORWARD 1 -i eth1 -j DROP |
| 32 | function disconnDev(iface) { |
| 33 | |
| 34 | var cmd = 'ip6tables -w -I FORWARD 1 -i ' + iface + ' -j DROP'; |
| 35 | var checkCmd = 'ip6tables -w -v -L FORWARD 1'; |
| 36 | var check = 'DROP all ' + iface; |
| 37 | |
| 38 | console.log(checkCmd + ' to check ' + check); |
| 39 | execAsync(checkCmd).then(function (result) { |
| 40 | console.log("indexOf:" + result.indexOf(check)); |
| 41 | if (result.indexOf(check) > 0) { |
| 42 | console.log("Forward rule exists."); |
| 43 | } |
| 44 | else { |
| 45 | execAsync(cmd); |
| 46 | console.log(cmd); |
| 47 | } |
| 48 | }, function (err) {console.error(err);}); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | //ip6tables -D FORWARD -i eth1 -j DROP |
| 53 | function connDev(iface) { |
| 54 | var cmd = 'ip6tables -w -D FORWARD -i ' + iface + ' -j DROP'; |
| 55 | var checkCmd = 'ip6tables -w -v -L FORWARD 1'; |
| 56 | var check = 'DROP all ' + iface; |
| 57 | |
| 58 | console.log(checkCmd + ' to check ' + check); |
| 59 | execAsync(checkCmd).then(function (result) { |
| 60 | console.log("indexOf:" + result.indexOf(check)); |
| 61 | if (result.indexOf(check) > 0) { |
| 62 | execAsync(cmd); |
| 63 | console.log(cmd); |
| 64 | } |
| 65 | else { |
| 66 | console.log("Forward rule not exist."); |
| 67 | } |
| 68 | }, function (err) {console.error(err);}); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | function natRedirectPkt(iface, ipAddr, port) { |
| 73 | |
| 74 | var cmd = 'ip6tables -w -t nat -A PREROUTING -i ' + iface + |
| 75 | ' -p tcp --dport ' + port + ' -j DNAT --to-destination [' |
| 76 | + ipAddr + ']:' + port; |
| 77 | var checkCmd = 'ip6tables -w -t nat -v -L PREROUTING'; |
| 78 | var check = 'to:[' + ipAddr + ']:' + port; |
| 79 | |
| 80 | console.log(checkCmd + ' to check ' + check); |
| 81 | execAsync(checkCmd).then(function (result) { |
| 82 | console.log("indexOf:" + result.indexOf(check)); |
| 83 | if (result.indexOf(check) > 0) { |
| 84 | console.log("nat rule exists."); |
| 85 | } |
| 86 | else { |
| 87 | execAsync(cmd); |
| 88 | console.log(cmd); |
| 89 | } |
| 90 | }, function (err) {console.error(err);}); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | function natRecoverPkt(iface, ipAddr, port) { |
| 95 | |
| 96 | var cmd = 'ip6tables -w -t nat -D PREROUTING -i ' + iface + |
| 97 | ' -p tcp --dport ' + port + ' -j DNAT --to-destination [' |
| 98 | + ipAddr + ']:' + port; |
| 99 | var checkCmd = 'ip6tables -w -t nat -v -L PREROUTING'; |
| 100 | var check = 'to:[' + ipAddr + ']:' + port; |
| 101 | |
| 102 | console.log(checkCmd + ' to check ' + check); |
| 103 | execAsync(checkCmd).then(function (result) { |
| 104 | console.log("indexOf:" + result.indexOf(check)); |
| 105 | if (result.indexOf(check) > 0) { |
| 106 | execAsync(cmd); |
| 107 | console.log(cmd); |
| 108 | } |
| 109 | else { |
| 110 | console.log("nat rule not exist."); |
| 111 | } |
| 112 | }, function (err) {console.error(err);}); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | function natMasquerade(iface) { |
| 117 | |
| 118 | var cmd = 'iptables -w -t nat -A POSTROUTING --out-interface ' |
| 119 | + iface + ' -j MASQUERADE'; |
| 120 | var checkCmd = 'iptables -w -t nat -v -L POSTROUTING'; |
| 121 | var check = 'MASQUERADE all -- any ' + iface; |
| 122 | |
| 123 | console.log(checkCmd + ' to check ' + check); |
| 124 | execAsync(checkCmd).then(function (result) { |
| 125 | console.log("indexOf:" + result.indexOf(check)); |
| 126 | if (result.indexOf(check) > 0) { |
| 127 | console.log("nat rule exists."); |
| 128 | } |
| 129 | else { |
| 130 | execAsync(cmd); |
| 131 | console.log(cmd); |
| 132 | } |
| 133 | }, function (err) {console.error(err);}); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | function natRmMasquerade(iface) { |
| 138 | |
| 139 | var cmd = 'iptables -w -t nat -D POSTROUTING --out-interface ' |
| 140 | + iface + ' -j MASQUERADE'; |
| 141 | var checkCmd = 'iptables -w -t nat -v -L POSTROUTING'; |
| 142 | var check = 'MASQUERADE all -- any ' + iface; |
| 143 | |
| 144 | console.log(checkCmd + ' to check ' + check); |
| 145 | execAsync(checkCmd).then(function (result) { |
| 146 | console.log("indexOf:" + result.indexOf(check)); |
| 147 | if (result.indexOf(check) > 0) { |
| 148 | execAsync(cmd); |
| 149 | console.log(cmd); |
| 150 | } |
| 151 | else { |
| 152 | console.log("nat rule not exist."); |
| 153 | } |
| 154 | }, function (err) {console.error(err);}); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | function natRmDfltGw(ipAddr) { |
| 159 | |
| 160 | var cmd = 'route del default gw ' + ipAddr; |
| 161 | var checkCmd = 'ip route'; |
| 162 | var check = 'default via ' + ipAddr; |
| 163 | |
| 164 | console.log(checkCmd + ' to check ' + check); |
| 165 | |
| 166 | execAsync(checkCmd).then(function (result) { |
| 167 | console.log("indexOf:" + result.indexOf(check)); |
| 168 | if (result.indexOf(check) >= 0) { |
| 169 | execAsync(cmd); |
| 170 | console.log(cmd); |
| 171 | } |
| 172 | else { |
| 173 | console.log("default gw " + ipAddr + " not exists."); |
| 174 | } |
| 175 | }, function (err) {console.error(err);}); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | function natAddDfltGw(ipAddr, dev) { |
| 180 | |
| 181 | var cmd = 'route add default gw ' + ipAddr + ' dev ' + dev; |
| 182 | var checkCmd = 'ip route'; |
| 183 | var check = 'default via ' + ipAddr; |
| 184 | |
| 185 | console.log(checkCmd + ' to check ' + check); |
| 186 | |
| 187 | execAsync(checkCmd).then(function (result) { |
| 188 | console.log("indexOf:" + result.indexOf(check)); |
| 189 | if (result.indexOf(check) >= 0) { |
| 190 | console.log("default gw " + ipAddr + " exists."); |
| 191 | } |
| 192 | else { |
| 193 | execAsync(cmd); |
| 194 | console.log(cmd); |
| 195 | } |
| 196 | }, function (err) {console.error(err);}); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | function setDnsRoute(ipAddr, gw, dev) { |
| 201 | |
| 202 | var cmd = 'route add ' + ipAddr + ' gw ' + gw + ' dev ' + dev; |
| 203 | var checkCmd = 'ip route'; |
| 204 | var check = ipAddr + ' via ' + gw; |
| 205 | |
| 206 | console.log(checkCmd + ' to check ' + check); |
| 207 | |
| 208 | execAsync(checkCmd).then(function (result) { |
| 209 | console.log("indexOf:" + result.indexOf(check)); |
| 210 | if (result.indexOf(check) >= 0) { |
| 211 | console.log("route " + ipAddr + " exists."); |
| 212 | } |
| 213 | else { |
| 214 | execAsync(cmd); |
| 215 | console.log(cmd); |
| 216 | } |
| 217 | }, function (err) {console.error(err);}); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | function rmDnsRoute(ipAddr) { |
| 222 | |
| 223 | var cmd = 'route del ' + ipAddr; |
| 224 | var checkCmd = 'ip route'; |
| 225 | var check = ipAddr + ' via'; |
| 226 | |
| 227 | console.log(checkCmd + ' to check ' + check); |
| 228 | |
| 229 | execAsync(checkCmd).then(function (result) { |
| 230 | console.log("indexOf:" + result.indexOf(check)); |
| 231 | if (result.indexOf(check) >= 0) { |
| 232 | execAsync(cmd); |
| 233 | console.log(cmd); |
| 234 | } |
| 235 | else { |
| 236 | console.log("route " + ipAddr + " not exists."); |
| 237 | } |
| 238 | }, function (err) {console.error(err);}); |
| 239 | |
| 240 | } |
| 241 | |
| 242 | function pppoeMasquerade() { |
| 243 | natMasquerade('ppp0'); |
| 244 | } |
| 245 | |
| 246 | function pppoeRmMasquerade() { |
| 247 | natRmMasquerade('ppp0'); |
| 248 | } |
| 249 | |
| 250 | function pppoeRedirectPkt(ipAddr) { |
| 251 | natRedirectPkt('eth1', ipAddr, '80'); |
| 252 | natRedirectPkt('eth1', ipAddr, '443'); |
| 253 | disconnDev('eth1'); |
| 254 | } |
| 255 | |
| 256 | function pppoeRmRedirectPkt(ipAddr) { |
| 257 | natRecoverPkt('eth1', ipAddr, '80'); |
| 258 | natRecoverPkt('eth1', ipAddr, '443'); |
| 259 | connDev('eth1'); |
| 260 | } |
| 261 | |
| 262 | function pppoeSetRoute() { |
| 263 | natRmDfltGw(oriGw); |
| 264 | setDnsRoute(dnsFwdr, oriGw, 'eth0'); |
| 265 | } |
| 266 | |
| 267 | function pppoeRmRoute() { |
| 268 | natAddDfltGw(oriGw, 'eth0'); |
| 269 | rmDnsRoute(dnsFwdr); |
| 270 | } |
| 271 | |
| 272 | module.exports = { |
| 273 | check:function(adminState,endSession){ |
| 274 | var adminEnable = "enable"; |
| 275 | var adminDisable = "disable"; |
| 276 | return execAsync("cat adminState.txt ").then(function (result) { |
| 277 | var i = false; |
| 278 | ifaces['eth1'].forEach(function(details){ |
| 279 | if (details.family=='IPv6' && i == false) |
| 280 | { |
| 281 | i = true; |
| 282 | eh1ip = details.address; |
| 283 | console.log('eh1ip:'+eh1ip); |
| 284 | } |
| 285 | |
| 286 | }); |
| 287 | console.log("result:"+ result); |
| 288 | if(adminState == adminEnable) |
| 289 | { |
| 290 | if(result.indexOf(adminDisable) > 0 && endSession == false) |
| 291 | { |
| 292 | pppoeRedirectPkt(eh1ip); |
| 293 | pppoeMasquerade(); |
| 294 | pppoeSetRoute(); |
| 295 | var writeData = '"admin-state": "enable","end-session": "false"'; |
| 296 | console.log("writeData :"+writeData); |
| 297 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 298 | if(err) throw err; |
| 299 | console.log("write success."); |
| 300 | }); |
| 301 | return true; |
| 302 | } |
| 303 | else if (result.indexOf(adminEnable) > 0 && endSession == true) |
| 304 | { |
| 305 | return execAsync('ps -ef|grep pppd').then(function (result) { |
| 306 | console.log("indexOf:"+result.indexOf("call provider")); |
| 307 | if (result.indexOf("call provider") > 0) |
| 308 | { |
| 309 | execAsync("poff -a"); |
| 310 | pppoeRedirectPkt(eh1ip); |
| 311 | pppoeSetRoute(); |
| 312 | console.log("poff ok."); |
| 313 | var writeData = '"admin-state": "enable","end-session": "true"'; |
| 314 | console.log("writeData :"+writeData); |
| 315 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 316 | if(err) throw err; |
| 317 | console.log("write success."); |
| 318 | |
| 319 | }); |
| 320 | return true; |
| 321 | } |
| 322 | }, function (err) { |
| 323 | console.error(err); |
| 324 | return false; |
| 325 | }); |
| 326 | |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | console.log("NO enable case"); |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | else if(adminState == adminDisable) |
| 335 | { |
| 336 | if (result.indexOf(adminEnable) > 0 && endSession == false) |
| 337 | { |
| 338 | return execAsync('ps -ef|grep pppd').then(function (result) { |
| 339 | console.log("indexOf:"+result.indexOf("call provider")); |
| 340 | if (result.indexOf("call provider") > 0) |
| 341 | { |
| 342 | execAsync("poff -a"); |
| 343 | console.log("poff ok."); |
| 344 | } |
| 345 | |
| 346 | pppoeRmRedirectPkt(eh1ip); |
| 347 | pppoeRmMasquerade(); |
| 348 | pppoeRmRoute(); |
| 349 | |
| 350 | var writeData = '"admin-state": "disable","end-session": "false"'; |
| 351 | console.log("writeData :"+writeData); |
| 352 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 353 | if(err) throw err; |
| 354 | console.log("write success."); |
| 355 | }); |
| 356 | return true; |
| 357 | }, function (err) { |
| 358 | console.error(err); |
| 359 | return false; |
| 360 | }); |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | console.log("NO disable case"); |
| 365 | return false; |
| 366 | } |
| 367 | } |
| 368 | }, function (err) { |
| 369 | console.log("cat adminState.txt fail"); |
| 370 | return false; |
| 371 | }); |
| 372 | } |
| 373 | } |