ipset เบื้องต้น

ตามที่มีคนถามเข้ามา ด้วยส่วนตัวใช้ ipset มาบ้างพอสมควร จึงจะลองเล่าให้ฟังละกันครับ

ipset เป็นชุดโปรแกรมที่ทำงานทั้งในส่วน kernelspace และ userspace และจากที่มีการทำงานในส่วน kernelspace จึงต้องมี module สำหรับ kernel ที่เรากำลังใช้งานอยู่ด้วย ดังนั้น การใช้งาน ipset จะต้องเตรียม 2 ส่วน ดังนี้

  1. เตรียม kernel
    # aptitude install netfilter-extensions module-assistant

    เริ่ม build module ด้วยการเตรียมความพร้อมก่อน

    # m-a prepare

    build จริง

    # m-a a-i netfilter-extensions

    หลังจากทำการ build สำเร็จ ระบบจะติดตั้ง module ให้โดยอัตโนมัติ

  2. เตรียม userspace

    ขั้นตอนนี้ไม่มีอะไรมาก เนื่องจากมีคนเตรียมไว้ให้แล้วใน Debian

    # aptitude install ipset

ต่อไปลองทดสอบ

# iptables -m set
iptables v1.4.1.1: You must specify `--set' with proper arguments
Try `iptables -h' or 'iptables --help' for more information.

ขึ้นลักษณะนี้ น่าจะใช้ได้แล้วครับ

วิธีใช้งาน

  1. สร้าง set ก่อน ในที่นี้จะแนะนำการใช้ macipmap
    # ipset -N [setnam] [settype] --from [ip1] --to [ip2]
    # ipset -N myset macipmap --from 192.168.0.10 --to 192.168.0.250
  2. ดู set
    # ipset -nL
    Name: myset
    Type: macipmap
    References: 0
    Default binding: 
    Header: from: 192.168.0.10 to: 192.168.0.250
    Members:
    Bindings:
  3. เพิ่ม ip+mac เข้าไปใน set
    # ipset -A myset 192.168.0.11:AA:BB:CC:DD:EE:FF
    # ipset -nL
    Name: myset
    Type: macipmap
    References: 0
    Default binding: 
    Header: from: 192.168.0.10 to: 192.168.0.250
    Members:
    192.168.0.11:AA:BB:CC:DD:EE:FF
    Bindings:

    จะเห็นว่ามี IP และ MAC เพิ่มเข้ามาใน set แล้ว :P

  4. ใช้งานร่วมกับ iptables
    # iptables -A FORWARD -m set --set myset src -j ACCEPT

    ผลที่ได้คือ ถ้า packet ไหนที่ผ่านเข้ามา แล้วพบใน set ก็จะยอมให้ผ่านไปได้ครับ

  5. ถ้าจะลบ ip+mac ออกจาก set ก็ทำได้ง่าย คือ
    # ipset -D myset 192.168.0.11:AA:BB:CC:DD:EE:FF

    เท่านี้ ก็ถูกลบออกจาก set แล้วครับ

ข้อดี ของ ipset คือ "ความเร็ว" เพราะในการตรวจสอบ ทำผ่าน iptables rule แค่ที่เดียว และอีกอย่างคือ สามารถ ควบคุม rule ได้แบบ dynamic โดยที่ไม่ต้องโหลด iptables rule ใหม่ แก้ ip+mac ผ่านทาง ipset ได้โดยตรง

ipset เป็นอีกหนึ่งชุดโปรแกรม ที่ผมใช้แล้วชอบ โดยขณะนี้ได้นำมาดัดแปลงทำเป็น NAS (Network Access Service) คือทำลักษณะเดียวกับ CoovaChilli ถ้าผ่านช่วง alpha เมื่อไร จะเปิดโปรเจกต์ให้ผู้ที่สนใจ นำไปทดสอบครับ

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <blockquote> <img> <h3> <h4> <h5>
  • Lines and paragraphs break automatically.
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.