понедельник, 3 ноября 2014 г.

Нестандартная маска в правилах PCF

Механизм Packet Content Filtering, как это и следует из названия, позволяет фильтровать пакеты по их содержимому. Конкретное действие определяется правилом. Группа однотипных правил помещается в профиль. В профиле указывается смещение, в котором следует искать нужную последовательность байт, и маска, под которую они должны попадать. Пример создания правил для DES-3200-28/A1/B1 можно посмотреть вот тут.

Здесь я хочу рассказать не о самих правилах, а об используемой маске. В большинстве примеров используется маска, где выбираются или отбрасываются байты целиком. Например, по ссылке выше можно увидеть маску 0x00ff. Это означает, что первый байт в этой паре может быть любым, а второй должен быть задан явно. Однако ничего не мешает задавать маску побитово. Это может быть использовано для управления последовательностью данных. Например, так можно отфильтровать несколько идущих подряд IP-адресов.

Посмотрим на этот пример:

create access_profile packet_content_mask c_tag 0xFFF0 offset1 l2 0 0x0 profile_id 1


config access_profile profile_id 1 add access_id auto_assign packet_content c_tag 0x0000 port all permit


config access_profile profile_id 1 add access_id auto_assign packet_content c_tag 0x0010 port all permit


Оффсет здесь задается для совместимости с "недокументированными особенностями", но больше нас интересует C-tag, который является тегом 802.1Q и состоит из Priority, Canonical Format Indicator (CFI) и VLAN Identifier (VID). Для удобства будем считать приоритет не заданным (0b000), а CFI - каноническим (0b0), тем более, в большинстве случаев так и получается. Оставшиеся 12 бит (16 минус 3 Priority и минус 1 CFI) относятся к VID.

Обратим внимание на маску:

1111 1111  1111 0000 - FFF0

Такая маска означает, что 12 бит (считая слева) должны быть заданы явно, а вот последние 4 могут быть любыми. Эти 4 бита позволяют нам нарезать вланы блоками по 16 штук (2^4).

Сравним маску FFF0 с первым правилом, где c_tag указан как 0x0000:

1111 1111  1111 0000 - FFF0
0000 0000  0000 0000 - 0000
....
0000 0000  0000 1111 - 000F

Под такую маску для первого правила попадут все VID от 0 до F, т.е. от 0 до 15

Аналогично проведем сравнение со вторым правилом, где c_tag указан как 0x0010:

1111 1111  1111 0000 - FFF0
0000 0000  0001 0000 - 0010
....
0000 0000  0001 1111 - 001F

Под такую маску для попадут все VID от 10 до 1F, т.е. от 15 до 31

Таким образом мы описали 32 влан двумя правилами.

Аналогичный пример можно привести и для работы с ARP:

#permit ARP with correct 4th octet (between port*8 and port*8+7)
create access_profile packet_content_mask offset1 l2 0 0xFFFF offset2 l3 16 0xF8 profile_id 5
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x08 port 1 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x10 port 2 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x20 port 4 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x28 port 5 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x30 port 6 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x38 port 7 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x40 port 8 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x48 port 9 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x50 port 10 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x58 port 11 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x60 port 12 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x68 port 13 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x70 port 14 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x78 port 15 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x80 port 16 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x88 port 17 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x90 port 18 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0x98 port 19 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0xa0 port 20 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0xa8 port 21 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0xb0 port 22 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0xb8 port 23 permit
config access_profile profile_id 5 add access_id auto_assign packet_content offset1 0x0806 offset2 0xc0 port 24 permit

#deny arp
create access_profile packet_content_mask offset1 l2 0 0xFFFF profile_id 6
config access_profile profile_id 6 add access_id auto_assign packet_content offset1 0x0806 port 1-24 deny


В профиле №5 разрешаются ARP-ответы из диапазона Port*8 <= 4th_oct <= Port*8+7. Всего 8 возможных вариантов IP для каждого порта. 24 правилами описаны 192 адреса. Это становится возможным из-за маски 0xF8 (0x11111000), которой явно определяются только первые 5 бит адреса. Затем, в профиле №6 мы блокируем все остальные ARP-ответы.

Как видно, использование "побитовой" маски в сочетании с планированием адресного пространства с учетом "компьютерной" логики позволяет экономить правила, которые не бесконечны.

Комментариев нет:

Отправить комментарий