diff options
author | Vladislav Grishenko <themiron@mail.ru> | 2011-02-14 04:50:30 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-14 04:50:30 +0100 |
commit | 713e6d78e1cb567848805e8dd0c9c0cadbfa787a (patch) | |
tree | e26cbdd5f61efe3a55913c0c72146ee7e16f704b | |
parent | 48a5219f39a7fcb8b9670d20439a3ab89c2b9bac (diff) | |
download | busybox-w32-713e6d78e1cb567848805e8dd0c9c0cadbfa787a.tar.gz busybox-w32-713e6d78e1cb567848805e8dd0c9c0cadbfa787a.tar.bz2 busybox-w32-713e6d78e1cb567848805e8dd0c9c0cadbfa787a.zip |
udhcpc: use more correct, and more importantly, more understandable BPF code
Signed-off-by: Vladislav Grishenko <themiron@mail.ru>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/udhcp/dhcpc.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 7e5ab61fd..d97a404fa 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -714,22 +714,25 @@ static int udhcp_raw_socket(int ifindex) | |||
714 | * | 714 | * |
715 | * TODO: make conditional? | 715 | * TODO: make conditional? |
716 | */ | 716 | */ |
717 | #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68) | ||
718 | static const struct sock_filter filter_instr[] = { | 717 | static const struct sock_filter filter_instr[] = { |
719 | /* check for udp */ | 718 | /* load 9th byte (protocol) */ |
720 | BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9), | 719 | BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9), |
721 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0), /* L5, L1, is UDP? */ | 720 | /* jump to L1 if it is IPPROTO_UDP, else to L4 */ |
722 | /* ugly check for arp on ethernet-like and IPv4 */ | 721 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6), |
723 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */ | 722 | /* L1: load halfword from offset 6 (flags and frag offset) */ |
724 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4), /* L3, L4 */ | 723 | BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6), |
725 | /* skip IP header */ | 724 | /* jump to L4 if any bits in frag offset field are set, else to L2 */ |
726 | BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */ | 725 | BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0), |
727 | /* check udp source and destination ports */ | 726 | /* L2: skip IP header (load index reg with header len) */ |
728 | BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0), | 727 | BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), |
729 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */ | 728 | /* load udp destination port from halfword[header_len + 2] */ |
730 | /* returns */ | 729 | BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2), |
731 | BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ), /* L3: pass */ | 730 | /* jump to L3 if udp dport is CLIENT_PORT, else to L4 */ |
732 | BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */ | 731 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1), |
732 | /* L3: accept packet */ | ||
733 | BPF_STMT(BPF_RET|BPF_K, 0xffffffff), | ||
734 | /* L4: discard packet */ | ||
735 | BPF_STMT(BPF_RET|BPF_K, 0), | ||
733 | }; | 736 | }; |
734 | static const struct sock_fprog filter_prog = { | 737 | static const struct sock_fprog filter_prog = { |
735 | .len = sizeof(filter_instr) / sizeof(filter_instr[0]), | 738 | .len = sizeof(filter_instr) / sizeof(filter_instr[0]), |
@@ -742,18 +745,19 @@ static int udhcp_raw_socket(int ifindex) | |||
742 | fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); | 745 | fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
743 | log1("Got raw socket fd %d", fd); //log2? | 746 | log1("Got raw socket fd %d", fd); //log2? |
744 | 747 | ||
745 | if (SERVER_PORT == 67 && CLIENT_PORT == 68) { | 748 | sock.sll_family = AF_PACKET; |
746 | /* Use only if standard ports are in use */ | 749 | sock.sll_protocol = htons(ETH_P_IP); |
750 | sock.sll_ifindex = ifindex; | ||
751 | xbind(fd, (struct sockaddr *) &sock, sizeof(sock)); | ||
752 | |||
753 | if (CLIENT_PORT == 68) { | ||
754 | /* Use only if standard port is in use */ | ||
747 | /* Ignoring error (kernel may lack support for this) */ | 755 | /* Ignoring error (kernel may lack support for this) */ |
748 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, | 756 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, |
749 | sizeof(filter_prog)) >= 0) | 757 | sizeof(filter_prog)) >= 0) |
750 | log1("Attached filter to raw socket fd %d", fd); // log? | 758 | log1("Attached filter to raw socket fd %d", fd); // log? |
751 | } | 759 | } |
752 | 760 | ||
753 | sock.sll_family = AF_PACKET; | ||
754 | sock.sll_protocol = htons(ETH_P_IP); | ||
755 | sock.sll_ifindex = ifindex; | ||
756 | xbind(fd, (struct sockaddr *) &sock, sizeof(sock)); | ||
757 | log1("Created raw socket"); | 761 | log1("Created raw socket"); |
758 | 762 | ||
759 | return fd; | 763 | return fd; |