Syed Jahanzaib – Personal Blog to Share Knowledge !

October 27, 2011

MIKROTIK Policy Routing based on Client IP Address


 

 ~!!!~   Article  By  Syed Jahanzaib   ~!!!~

Recently at a local cable.network setup @ gulzar-e-hijri, an OP asked told me that he wanted to add an extra DSL line just for some specific users (VIP users who are paying some extra money for better speed, as the current single dsl is getting clogged by over subscribed users. He wanted to manage all users / link via single Mikrotik router-board.
I accomplished this task by adding 2nd DSL line with the Mikrotik BOX and few rules, and Alhamdolillah it worked fine :). I am just sharing basic logic on how i achieved it.

You can take this idea and modify it according to your requirement, either use this logic and mix it with hotspot or pppoe base setup. In this example, user MAC-IP was binded.

Let us assume that we have 2 Users lists.

DSL1_USERS_LIST = 192.168.2.6
DSL2_USERS_LIST = 192.168.2.7

 

DSL1 Router GW IP = 192.168.5.2
DSL2 Router GW IP = 192.168.6.2

Now we will route users through DSL-1 OR DSL-2 wan links based on there ip addresses.

To accomplish this task, the simple logic is …

  1. STEP#1: First we will create ADDRESS-LIST, and add our users in the list,
  2. STEP#2: Then We have to add two IP Firewall Mangle rules to mark the packets originated from user 1 and user2.
  3. STEP#3: Then we should specify two default routes (destination 0.0.0.0/0) with appropriate routing marks and gateways.
  4. STEP#4: Then simple add one NAT rule for local ip series and Action masquerade.

Code:

# Adding IP Address on interfaces like lan/wan
# IP for LAN User Network Connectivity

/ip address
add address=192.168.2.1/24 disabled=no interface=LAN network=192.168.2.0
# IP for WAN DSL Connectivity

add address=192.168.5.1/24 disabled=no interface=WAN1 network=192.168.5.0
add address=192.168.6.1/24 disabled=no interface=WAN2 network=192.168.6.0

# Create 2 Address lists and add ip as per required, you can add Range as well.
# I am adding just 2 ips only

/ip firewall address-list
add address=192.168.2.6 disabled=no list=DSL1_USERS_LIST
add address=192.168.2.7 disabled=no list=DSL2_USERS_LIST
# Marking Users connection coming from specific address lists
/ip firewall mangle
add action=mark-routing chain=prerouting disabled=no new-routing-mark=wan1_user passthrough=no src-address-list=DSL1_USERS_LIST
add action=mark-routing chain=prerouting disabled=no new-routing-mark=wan2_user passthrough=no src-address-list=DSL2_USERS_LIST
# Create Routes for above marked packets so each marked packets goes via specific wan link only
/ip route
add disabled=no distance=1 dst-address=0.0.0.0/0 gateway=192.168.5.2 routing-mark=wan1_user scope=30 target-scope=10
add disabled=no distance=2 dst-address=0.0.0.0/0 gateway=192.168.6.2 routing-mark=wan2_user scope=30 target-scope=10
# Finally create NAT rule so that users (ip range) internet can work
/ip firewall nat
add action=masquerade chain=srcnat disabled=no src-address=192.168.2.0/24

TESTING

From Client PC # 1 whose ip address is 192.168.2.6, run TRACEROUTE command

For example traceroute yahoo.com

 

Results


TESTING FROM DSL1_USERS_LIST = 192.168.2.6

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . :
Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC
Physical Address. . . . . . . . . : 00-xx-xx-xx-xx-xx
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.2.6
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.2.1
DNS Servers . . . . . . . . . . . : 192.168.2.1

C:\Documents and Settings\zaib>tracert yahoo.com

Tracing route to yahoo.com [209.191.122.70]
over a maximum of 30 hops

1     <1 ms     <1 ms     2 ms  192.168.2.1
2      2 ms      2 ms     3 ms  192.168.5.2
.
.


TESTING FROM DSL2_USERS_LIST = 192.168.2.7

Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . :
Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC
Physical Address. . . . . . . . . : 00-xx-xx-xx-xx-xx
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.2.7
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.2.1
DNS Servers . . . . . . . . . . . : 192.168.2.1

C:\Documents and Settings\zaib>tracert yahoo.com

Tracing route to yahoo.com [209.191.122.70]
over a maximum of 30 hops

1     <1 ms     <1 ms     2 ms  192.168.2.1
2      2 ms      2 ms     3 ms  192.168.6.2
.
.


 

Regard’s
SYED JAHANZAIB

51 Comments »

  1. Nice sharing Jahanzaib bhai! can we do this with any other open source linux distros???

    Like

    Comment by faizan — October 28, 2011 @ 12:22 AM

    • sir do this work with hotspot on same pc?

      Like

      Comment by adeel ahmed — November 13, 2011 @ 9:03 PM

      • I never tested this in on HOTSPOT particularly, But The theory says it can work 🙂
        It will work I am sure.

        Like

        Comment by Pinochio~:) — November 14, 2011 @ 10:35 AM

  2. sir…i interested with this consep…how to setup this concep with external proxy…
    may u help me sir

    Like

    Comment by Mukhsin — January 3, 2012 @ 4:45 PM

    • Wan1 & Wan2 —- (eth1&2) RB450 (eth4) —- Lan
      (eth3)
      ‘ |
      ‘ Proxy

      Like

      Comment by Mukhsin — January 3, 2012 @ 4:49 PM

  3. just wanna to ask you one question..

    1) can i change the default ip (192.168.88.1) to different ip like 10.200.0.1?

    2) Here my connection

    ISP RB750GL switch

    How i want to config my router? I have to use 10.200.0.1/24 ip for my LAN

    Like

    Comment by nisa — January 9, 2012 @ 11:20 AM

  4. am so sorry that I post my recent post in “About Me” page
    now
    how I can split the IP range of (for example 10.0.0.2-10.0.0.254) in to two groups (A&B)
    Group A take NET from ADSL1 ,and Group B take NET from ADSL2 ?
    do I have to enter the IP for each Group ?
    as I mentioned before ,I use the article “http://wiki.mikrotik.com/wiki/Load_Balancing_over_Multiple_Gateways”
    but it didn’t work for me
    I need it in PPoE and Hotspot
    am very grateful to you ,and to any one who can assist me in this
    My Best Regards
    (also I send you email about that)

    Like

    Comment by Firas A.Kareem — February 9, 2012 @ 8:18 PM

    • Dear its easy to achieve your goal. If you read the article thoroughly , you will be able to make it.

      First create two address list, DSL1_Users and DSL2_Users , then add ips in these address list as per your requirements, now mark packets for these address list, and define route for specific marked packets to go through your desired wan link.
      Just follow the guide.

      in Address list, use your user ip pool (i.e hotspot or pppoe pool)

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — February 10, 2012 @ 9:05 AM

      • you are right Mr.Sayd ,I did it ,I put in user list 1:10.0.0.0/25 and 10.0.0.128/25 in user list 2
        many thanks to your wonderful ideas !
        I have simple question ..or request …
        can I put welcome screen in PPoE servers (like login screen of Hotspot)
        I want to put a page ,so my clients learn how to setup a PPoE connection
        i.e ,when some one connect to my router,rather than Hotspot Login page ,he see that page only,so he learn how to setup PPoE connection,and then connect
        Accept my Regards

        Like

        Comment by Firas A.Kareem — February 11, 2012 @ 6:09 AM

      • PPPoE doesn’t support this feature.

        However you can create custom pppoe dialer, through which when user connects, it opens your local intranet page with instructions. You can use auto-it to create automatd dialer, use your own logic with the following guide.

        https://aacable.wordpress.com/2011/09/27/howto-create-windows-7-pppoe-dialer-installer-package-using-auto-it/

        Or another approach is to block internet ads and put your own ad with your local intranet page which have all the required links and your service info, for example like the following.
        https://aacable.wordpress.com/2011/06/01/squid-howto-block-ads/

        Like

        Comment by Syed Jahanzaib / Pinochio~:) — February 11, 2012 @ 11:42 AM

      • Hi, I’ve been able to follow through your tutorials many times to solve my problems. I must say you are doing an excellent job.

        However, for this particular one, I use a RB1200 and have setup 2 hotspot on 2 interfaces earlier. Now I’m adding a second internet connection (ISP) and want to route traffic from each hotspot through different internet connection. The last instruction in your blog post tells me to add NAT rules for the scr address but I found out the rules have been placed there already by the hotspot setup.

        I could do a trace route when I finished the setup which means the setup should be ok, however I cannot do PING and cannot browse any site. Is it about the hotspot setup or what could have been the problem? Please what suggestion do you have for me to be able to get through this.

        Thank you!

        Like

        Comment by Sim1 — April 5, 2013 @ 2:59 PM

  5. Salaam Brother,

    Sorry my previous post does not make sence!

    ******************************************************************************************************************
    I am thinking if the following scenrio is possible.

    Is it possible in policy routing? That it spread clients equally on wan links. For example:-

    If I have 3 wan links and there are 3 users are online, then each client should me on each wan etc etc…

    Hope you understand.

    Like

    Comment by nominet — May 8, 2012 @ 12:57 PM

    • You can’t do it using PCC. PCC have its algorithm to distribute users among multiple wan links. try with src-address.

      Or use policy base routing to distribute users using pools assignment.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — May 8, 2012 @ 3:09 PM

  6. It can be achieved by using pools assignments but this is not exactly thing I wanted to do.

    I want mikrotik to automatically spread users on wan’s. Is there no other ways other than PCC?

    Thanks…

    Like

    Comment by nominet — May 8, 2012 @ 4:08 PM

  7. Jahanzaib Bro , how can i use it with failover ..

    Like

    Comment by AHMED — July 10, 2012 @ 7:31 PM

  8. Salaam, is it possible to do as following?

    ip addresses: 192.168.0.1 to 192.168.199 go to DSL1
    ip addresses: 192.168.0.200 to 192.168.0.254 go to DSL2

    If yes, then can you please explain how?

    Thanks

    Like

    Comment by nominet — July 28, 2012 @ 4:27 PM

  9. Assalaamu Alaikum Syed Jahanzaib bhai aapnay youtube cache server ka jo project rakha howa hay wo menay kar lihay kaam bhi kar raha hay ALLAH aap ko kush rakhay salama rakhay per ek masla aa raha hay aghar main client ban kar use karta hoon to mujhay proxy lagani parti hay jo insterner explorer main lagai jati hay address main phiar port dena perta hay main chahta hoon auto detect chal jahay proxy internet explorer main use na karni paray is ka plz mujhay tareeqa batain

    Like

    Comment by abdulsami — September 5, 2012 @ 4:00 AM

  10. Mikrotik mujhay use nhi karna jahanziab bhai srif mujhay squid youtube cache server bana kar
    client site use kar raha hoon to proxy use karni perti hay bas itna bata dain yai proxy hat nhi sakti auto detect client side chal jahay plz help me

    Like

    Comment by abdulsami — September 5, 2012 @ 9:32 PM

  11. how to connect printer in mikrotik router

    Like

    Comment by rasheed — October 17, 2012 @ 5:20 PM

  12. Thanks a lot for sharing your idea. I learned many things from you Sir.

    Like

    Comment by Rodel Saludares — February 14, 2013 @ 3:32 PM

  13. thank you very much for your work, following your site I learned a lot on mikrotik and networking in general.

    I wanted to ask you a question,
    I followed the guide and everything works perfectly

    I want to make sure that if the line DSL1 does not work , the group DSL1_USER must be connected to DSL2 line .
    when the line DSL1 back to work, the users DSL1_USER return to DSL1 line
    Thank you very much for your work

    Like

    Comment by Erasmo — February 22, 2013 @ 7:21 PM

  14. Jahanzaib Sir i want to know how to two vpn client network combine in one network

    Like

    Comment by Ghufran Khizar — May 1, 2013 @ 4:36 AM

  15. can we add fail over function. i.e. if DSL 1 is down. DSL 2 will all serve dsl 1 userlist and dsl 2 userlist.

    Like

    Comment by Alex Adamos — June 30, 2013 @ 7:14 AM

  16. How Can I mark the connections so as to know which or who is going through a particular host without having access to the client’s system to do a trace?
    Secondly I need the script for failover so that if DSL 1 is down. DSL 2 will all serve dsl 1 userlist and dsl 2 userlist.

    Like

    Comment by Jones Cosmos — July 26, 2013 @ 1:22 AM

  17. hi can u describe how i can i split browsing and mail with 2 isp in 1 routerboard (mikrotik)
    1 isp for mail and 1 other isp for browsing

    ether1 = WAN1
    ether2 = WAN2
    ether3 = LAN1
    ether4 = LAN2

    LAN1 is farm server
    LAN2 is client

    i want split client (LAN2) for browsing is via WAN2
    and for server farm (LAN1) i want via WAN1

    Like

    Comment by krak — December 16, 2013 @ 9:58 PM

  18. Assalam Syed Jahanzaib

    i have follow this tutorial and working like i want now i want if dsl1 is down i want list user in dsl1_user can use link dsl2 and if dsl1 is up then the user in list dsl1 go to link dsl1
    can u write script for failover for this one tutorial if can implement in this tutorial
    or can u email for me

    Wassalam
    mozax

    Like

    Comment by mozax — December 21, 2013 @ 9:31 PM

  19. Assalam o alikum.

    how to configure this rule with squid proxy server and mikrotik proxy ?

    Like

    Comment by asad — September 25, 2014 @ 11:56 PM

  20. السلام عليكم

    Dear Syed Jahanzaib is it posible to use pppoe client instead of ip as Wan2, i tested it and found problems with this method and tried other methods still got no working result.
    here’s what i’m trying to make..
    Wan01 is ip I use it to provide a pppoe server..
    Wan02 is a pppoe client1 to use it with single ip on lan, only mark rooting couldnt gave a clean result, thanks in advice bro.

    Like

    Comment by Red Heart — October 22, 2014 @ 7:45 AM

  21. My Dear Syed Jahanzaib,

    Your solution worked with me perfectly,
    Thanks a lot.

    You are a life saver 🙂

    Jazak Allah

    Like

    Comment by Asim Khan — April 15, 2015 @ 6:56 PM

  22. Dear Syed Jahanzaib; i can’t access all network on this policy routing load balance methods. This is perfect for me but I can’t access other dsl list network. How can I do?

    Like

    Comment by eray — April 19, 2015 @ 8:13 AM

  23. Thx man I had to make isolation for 3 IP from my network company and pass them through second unused WAN. I read few articles but they were about how to combine all WAN together and use them as one. Only this article showed it is easy and you don’t have to mark any connections and packets 🙂 just mark routing and pass it for address-list. Thx

    Like

    Comment by Konrad — July 29, 2015 @ 11:34 AM

  24. Hai..
    Thanks for your tutorial i use same as your configuration but i have problem when i ping from the ousite (from http://network-tools.com) to my wan1 and wan2, i get timed Out. but when i set with no routing-mark in wan ip route configuration and i ping from outsite i get normally reply ping.

    There any one have solution for this problem ?

    Like

    Comment by Wahyu — October 30, 2015 @ 2:16 PM

  25. I have this configuration but with a pppoe server.

    lan ether1 192.168.1.1/24 – pppoe client: 192.168.20.1/24

    wan1 ether2 192.168.5.1
    wan2 ether3 192.168.6.1

    everything works perfectly.

    if I try to communicate from 192.168.1.5 to 192.168.20.2 can not.
    If I disable the rule mangle can communicate from 192.168.1.5 to 192.168.20.2 but if active rule mangle can not communicate between 192.168.1.5 and 192.168.20.2

    Like

    Comment by Erasmo — November 22, 2015 @ 5:36 PM

    • you can use a simple NAT MASQUERADE rule to enable communication from SRC to DST subnet.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — November 23, 2015 @ 8:49 AM

      • I created a nat masquerade rule but the problem persists.
        The problem comes when I enable the rule mangle with routing mark

        Like

        Comment by Erasmo — November 23, 2015 @ 4:26 PM

      • any other ideas?

        Like

        Comment by Erasmo — November 27, 2015 @ 7:48 PM

  26. same problem with my network . users can’t communicate with each others ip or can’t open web server but if i enable ICMP option in mangle then they can communicate but they can’t use internet .. pls share if you have any solution i try every think on NAT and MANGLE

    Like

    Comment by Ahmed — August 7, 2016 @ 4:24 AM

  27. wan 6 link pcc

    Like

    Comment by زيد الصليحي — August 12, 2016 @ 11:25 PM

  28. the tutorial worked automagically but l2tp servers didn’t work after having the two lines up through mikrotik. how can i make remote l2tp connections to connect to the mikrotiks l2tp vpn connections using the scenario.

    Like

    Comment by Samuel — July 25, 2017 @ 9:58 PM

  29. Sir your topic is really helpful but if somehow you create video of your topics and upload on YouTube that’s would be very good for all.

    Like

    Comment by ahmed — December 15, 2017 @ 10:05 AM

  30. youtube bdix kasa add karunga sazid bro

    Like

    Comment by robin — June 8, 2018 @ 9:43 PM

  31. […] other scenario’s, you may want to look into prerouting […]

    Like

    Pingback by Mikrotik Remote Access via Multiple WAN Links | Syed Jahanzaib Personal Blog to Share Knowledge ! — November 19, 2018 @ 3:58 PM

  32. Can please let me know, using two WAN, can we balance it like, use uplink from WAN A and downlink from WAN B?

    Like

    Comment by Usama Zafar — January 29, 2019 @ 11:01 PM


RSS feed for comments on this post. TrackBack URI

Leave a comment