2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2009, 2010 Emmanuel Roullit.
5 * Copyright 2010 Marek Polacek.
6 * Subject to the GPL, version 2.
20 #include <arpa/inet.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
31 #include <sys/resource.h>
32 #include <sys/epoll.h>
33 #include <sys/syscall.h>
34 #include <asm/unistd.h>
36 #include <linux/socket.h>
37 #include <linux/types.h>
38 #include <linux/if_ether.h>
39 #include <linux/if_packet.h>
40 #include <linux/sockios.h>
41 #include <netinet/tcp.h>
42 #include <netinet/udp.h>
49 #define IOPRIO_CLASS_SHIFT 13
59 ioprio_who_process
= 1,
71 #define SMEM_SUG_MAX 104857600
72 #define SMEM_SUG_DEF 4194304
74 static const char *const to_prio
[] = {
81 static const char *const sock_mem
[] = {
82 "/proc/sys/net/core/rmem_max",
83 "/proc/sys/net/core/rmem_default",
84 "/proc/sys/net/core/wmem_max",
85 "/proc/sys/net/core/wmem_default",
92 if (unlikely(af
!= AF_INET
&& af
!= AF_INET6
))
93 panic("Wrong AF socket type!\n");
95 sock
= socket(af
, SOCK_DGRAM
, 0);
96 if (unlikely(sock
< 0))
97 panic("Creation AF socket failed!\n");
104 int sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
105 if (unlikely(sock
< 0))
106 panic("Creation of PF socket failed!\n");
111 void set_sock_prio(int fd
, int prio
)
115 ret
= setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &val
, sizeof(val
));
117 panic("Cannot set socket priority!\n");
120 void set_udp_cork(int fd
)
124 ret
= setsockopt(fd
, IPPROTO_UDP
, UDP_CORK
, &state
, sizeof(state
));
126 panic("Cannot cork UDP socket!\n");
129 void set_udp_uncork(int fd
)
133 ret
= setsockopt(fd
, IPPROTO_UDP
, UDP_CORK
, &state
, sizeof(state
));
135 panic("Cannot uncork UDP socket!\n");
138 void set_tcp_cork(int fd
)
142 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_CORK
, &state
, sizeof(state
));
144 panic("Cannot cork TCP socket!\n");
147 void set_tcp_uncork(int fd
)
151 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_CORK
, &state
, sizeof(state
));
153 panic("Cannot uncork TCP socket!\n");
156 void set_sock_cork(int fd
, int udp
)
164 void set_sock_uncork(int fd
, int udp
)
172 void set_nonblocking(int fd
)
174 int ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
175 if (unlikely(ret
< 0))
176 panic("Cannot fcntl!\n");
179 int set_nonblocking_sloppy(int fd
)
181 return fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFD
, 0) | O_NONBLOCK
);
184 void set_socket_keepalive(int fd
)
188 ret
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &one
, sizeof(one
));
190 panic("Cannot set TCP keepalive!\n");
193 void set_tcp_nodelay(int fd
)
196 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
199 int set_ipv6_only(int fd
)
202 return setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
205 int set_reuseaddr(int fd
)
209 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
210 if (unlikely(ret
< 0))
211 panic("Cannot reuse addr!\n");
216 void set_mtu_disc_dont(int fd
)
218 int mtu
= IP_PMTUDISC_DONT
, ret
;
220 ret
= setsockopt(fd
, SOL_IP
, IP_MTU_DISCOVER
, &mtu
, sizeof(mtu
));
222 panic("Cannot set MTU discovery options!\n");
225 void set_epoll_descriptor(int fd_epoll
, int action
, int fd_toadd
, int events
)
228 struct epoll_event ev
;
230 memset(&ev
, 0, sizeof(ev
));
232 ev
.data
.fd
= fd_toadd
;
234 ret
= epoll_ctl(fd_epoll
, action
, fd_toadd
, &ev
);
236 panic("Cannot add socket for epoll!\n");
239 int set_epoll_descriptor2(int fd_epoll
, int action
, int fd_toadd
, int events
)
241 struct epoll_event ev
;
243 memset(&ev
, 0, sizeof(ev
));
245 ev
.data
.fd
= fd_toadd
;
247 return epoll_ctl(fd_epoll
, action
, fd_toadd
, &ev
);
250 u32
wireless_bitrate(const char *ifname
)
252 int sock
, ret
, rate_in_mbit
;
255 sock
= af_socket(AF_INET
);
257 memset(&iwr
, 0, sizeof(iwr
));
258 strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
260 ret
= ioctl(sock
, SIOCGIWRATE
, &iwr
);
262 rate_in_mbit
= iwr
.u
.bitrate
.value
/ 1000000;
271 void drop_privileges(bool enforce
, uid_t uid
, gid_t gid
)
275 panic("Uid cannot be the same as the current user!\n");
277 panic("Gid cannot be the same as the current user!\n");
279 if (setgid(gid
) != 0)
280 panic("Unable to drop group privileges: %s!\n", strerror(errno
));
281 if (setuid(uid
) != 0)
282 panic("Unable to drop user privileges: %s!\n", strerror(errno
));
285 int get_system_socket_mem(int which
)
289 const char *file
= sock_mem
[which
];
292 fd
= open(file
, O_RDONLY
);
296 ret
= read(fd
, buff
, sizeof(buff
));
304 void set_system_socket_mem(int which
, int val
)
307 const char *file
= sock_mem
[which
];
311 fd
= open(file
, O_WRONLY
);
315 memset(buff
, 0, sizeof(buff
));
316 slprintf(buff
, sizeof(buff
), "%d", val
);
318 ret
= write(fd
, buff
, strlen(buff
));
324 int wireless_sigqual(const char *ifname
, struct iw_statistics
*stats
)
329 sock
= af_socket(AF_INET
);
331 memset(&iwr
, 0, sizeof(iwr
));
332 strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
334 iwr
.u
.data
.pointer
= (caddr_t
) stats
;
335 iwr
.u
.data
.length
= sizeof(*stats
);
336 iwr
.u
.data
.flags
= 1;
338 ret
= ioctl(sock
, SIOCGIWSTATS
, &iwr
);
345 int wireless_rangemax_sigqual(const char *ifname
)
347 int ret
, sock
, sigqual
;
349 struct iw_range iwrange
;
351 sock
= af_socket(AF_INET
);
353 memset(&iwrange
, 0, sizeof(iwrange
));
355 memset(&iwr
, 0, sizeof(iwr
));
356 strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
358 iwr
.u
.data
.pointer
= (caddr_t
) &iwrange
;
359 iwr
.u
.data
.length
= sizeof(iwrange
);
360 iwr
.u
.data
.flags
= 0;
362 ret
= ioctl(sock
, SIOCGIWRANGE
, &iwr
);
364 sigqual
= iwrange
.max_qual
.qual
;
373 u32
ethtool_bitrate(const char *ifname
)
375 int ret
, sock
, bitrate
;
377 struct ethtool_cmd ecmd
;
379 sock
= af_socket(AF_INET
);
381 memset(&ecmd
, 0, sizeof(ecmd
));
383 memset(&ifr
, 0, sizeof(ifr
));
384 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
386 ecmd
.cmd
= ETHTOOL_GSET
;
387 ifr
.ifr_data
= (char *) &ecmd
;
389 ret
= ioctl(sock
, SIOCETHTOOL
, &ifr
);
395 switch (ecmd
.speed
) {
401 bitrate
= ecmd
.speed
;
413 int ethtool_link(const char *ifname
)
417 struct ethtool_value ecmd
;
419 sock
= af_socket(AF_INET
);
421 memset(&ecmd
, 0, sizeof(ecmd
));
423 memset(&ifr
, 0, sizeof(ifr
));
424 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
426 ecmd
.cmd
= ETHTOOL_GLINK
;
427 ifr
.ifr_data
= (char *) &ecmd
;
429 ret
= ioctl(sock
, SIOCETHTOOL
, &ifr
);
439 int ethtool_drvinf(const char *ifname
, struct ethtool_drvinfo
*drvinf
)
444 sock
= af_socket(AF_INET
);
446 memset(drvinf
, 0, sizeof(*drvinf
));
448 memset(&ifr
, 0, sizeof(ifr
));
449 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
451 drvinf
->cmd
= ETHTOOL_GDRVINFO
;
452 ifr
.ifr_data
= (char *) drvinf
;
454 ret
= ioctl(sock
, SIOCETHTOOL
, &ifr
);
461 u32
device_bitrate(const char *ifname
)
463 u32 speed_c
, speed_w
;
465 speed_c
= ethtool_bitrate(ifname
);
466 speed_w
= wireless_bitrate(ifname
);
468 return (speed_c
== 0 ? speed_w
: speed_c
);
471 int device_ifindex(const char *ifname
)
473 int ret
, sock
, index
;
476 if (!strncmp("any", ifname
, strlen("any")))
479 sock
= af_socket(AF_INET
);
481 memset(&ifr
, 0, sizeof(ifr
));
482 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
484 ret
= ioctl(sock
, SIOCGIFINDEX
, &ifr
);
486 index
= ifr
.ifr_ifindex
;
495 static int __device_address6(const char *ifname
, struct sockaddr_storage
*ss
)
497 int ret
, family
, found
= -EINVAL
;
498 struct ifaddrs
*ifaddr
, *ifa
;
500 ret
= getifaddrs(&ifaddr
);
502 panic("Cannot get device addresses for IPv6!\n");
504 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
505 family
= ifa
->ifa_addr
->sa_family
;
506 if (family
!= AF_INET6
)
508 if (strcmp(ifa
->ifa_name
, ifname
))
511 memcpy(ss
, ifa
->ifa_addr
, sizeof(*ss
));
520 int device_address(const char *ifname
, int af
, struct sockaddr_storage
*ss
)
527 if (!strncmp("any", ifname
, strlen("any")))
530 return __device_address6(ifname
, ss
);
532 sock
= af_socket(af
);
534 memset(&ifr
, 0, sizeof(ifr
));
535 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
537 ifr
.ifr_addr
.sa_family
= af
;
539 ret
= ioctl(sock
, SIOCGIFADDR
, &ifr
);
541 memcpy(ss
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
548 int device_mtu(const char *ifname
)
553 sock
= af_socket(AF_INET
);
555 memset(&ifr
, 0, sizeof(ifr
));
556 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
558 ret
= ioctl(sock
, SIOCGIFMTU
, &ifr
);
569 short device_get_flags(const char *ifname
)
571 /* Really, it's short! Look at struct ifreq */
576 sock
= af_socket(AF_INET
);
578 memset(&ifr
, 0, sizeof(ifr
));
579 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
581 ret
= ioctl(sock
, SIOCGIFFLAGS
, &ifr
);
583 flags
= ifr
.ifr_flags
;
592 void device_set_flags(const char *ifname
, const short flags
)
597 sock
= af_socket(AF_INET
);
599 memset(&ifr
, 0, sizeof(ifr
));
600 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
602 ifr
.ifr_flags
= flags
;
604 ret
= ioctl(sock
, SIOCSIFFLAGS
, &ifr
);
606 panic("Cannot set NIC flags!\n");
611 int device_irq_number(const char *ifname
)
614 * Since fetching IRQ numbers from SIOCGIFMAP is deprecated and not
615 * supported anymore, we need to grab them from procfs
623 if (!strncmp("lo", ifname
, strlen("lo")))
626 fp
= fopen("/proc/interrupts", "r");
628 panic("Cannot open /proc/interrupts!\n");
630 memset(buff
, 0, sizeof(buff
));
631 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
632 buff
[sizeof(buff
) - 1] = 0;
634 if (strstr(buff
, ifname
) == NULL
)
638 while (*buffp
!= ':')
643 memset(buff
, 0, sizeof(buff
));
651 * Try sysfs as fallback. Probably wireless devices will be found
652 * here. We return silently if it fails ...
654 slprintf(sysname
, sizeof(sysname
), "/sys/class/net/%s/device/irq",
657 fp
= fopen(sysname
, "r");
661 memset(buff
, 0, sizeof(buff
));
662 if(fgets(buff
, sizeof(buff
), fp
) != NULL
) {
663 buff
[sizeof(buff
) - 1] = 0;
672 int device_set_irq_affinity_list(int irq
, unsigned long from
, unsigned long to
)
675 char file
[256], list
[64];
677 slprintf(file
, sizeof(file
), "/proc/irq/%d/smp_affinity_list", irq
);
678 slprintf(list
, sizeof(list
), "%lu-%lu\n", from
, to
);
680 fd
= open(file
, O_WRONLY
);
684 ret
= write(fd
, list
, strlen(list
));
690 int device_bind_irq_to_cpu(int irq
, int cpu
)
697 /* Note: first CPU begins with CPU 0 */
698 if (irq
< 0 || cpu
< 0)
701 memset(file
, 0, sizeof(file
));
702 memset(buff
, 0, sizeof(buff
));
704 /* smp_affinity starts counting with CPU 1, 2, ... */
706 sprintf(file
, "/proc/irq/%d/smp_affinity", irq
);
708 fp
= fopen(file
, "w");
712 sprintf(buff
, "%d", cpu
);
713 ret
= fwrite(buff
, sizeof(buff
), 1, fp
);
716 return (ret
> 0 ? 0 : ret
);
719 void sock_print_net_stats(int sock
, unsigned long skipped
)
722 struct tpacket_stats kstats
;
724 socklen_t slen
= sizeof(kstats
);
726 memset(&kstats
, 0, sizeof(kstats
));
727 ret
= getsockopt(sock
, SOL_PACKET
, PACKET_STATISTICS
, &kstats
, &slen
);
729 uint64_t packets
= kstats
.tp_packets
;
730 uint64_t drops
= kstats
.tp_drops
;
732 printf("\r%12ld packets incoming\n", packets
);
733 printf("\r%12ld packets passed filter\n", packets
- drops
- skipped
);
734 printf("\r%12ld packets failed filter (out of space)\n", drops
+ skipped
);
735 if (kstats
.tp_packets
> 0)
736 printf("\r%12.4lf%\% packet droprate\n", (1.0 * drops
/ packets
) * 100.0);
740 void register_signal(int signal
, void (*handler
)(int))
743 struct sigaction saction
;
745 sigfillset(&block_mask
);
747 saction
.sa_handler
= handler
;
748 saction
.sa_mask
= block_mask
;
749 saction
.sa_flags
= SA_RESTART
;
751 sigaction(signal
, &saction
, NULL
);
754 void register_signal_f(int signal
, void (*handler
)(int), int flags
)
757 struct sigaction saction
;
759 sigfillset(&block_mask
);
761 saction
.sa_handler
= handler
;
762 saction
.sa_mask
= block_mask
;
763 saction
.sa_flags
= flags
;
765 sigaction(signal
, &saction
, NULL
);
768 short enter_promiscuous_mode(char *ifname
)
772 if (!strncmp("any", ifname
, strlen("any")))
775 ifflags
= device_get_flags(ifname
);
776 device_set_flags(ifname
, ifflags
| IFF_PROMISC
);
781 void leave_promiscuous_mode(char *ifname
, short oldflags
)
783 if (!strncmp("any", ifname
, strlen("any")))
786 device_set_flags(ifname
, oldflags
);
789 int device_up_and_running(char *ifname
)
793 if (!strncmp("any", ifname
, strlen("any")))
796 return (device_get_flags(ifname
) & (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
);
799 void cpu_affinity(int cpu
)
802 cpu_set_t cpu_bitmask
;
804 CPU_ZERO(&cpu_bitmask
);
805 CPU_SET(cpu
, &cpu_bitmask
);
807 ret
= sched_setaffinity(getpid(), sizeof(cpu_bitmask
),
810 panic("Can't set this cpu affinity!\n");
813 int set_proc_prio(int priority
)
815 int ret
= setpriority(PRIO_PROCESS
, getpid(), priority
);
817 panic("Can't set nice val to %i!\n", priority
);
822 int set_sched_status(int policy
, int priority
)
824 int ret
, min_prio
, max_prio
;
825 struct sched_param sp
;
827 max_prio
= sched_get_priority_max(policy
);
828 min_prio
= sched_get_priority_min(policy
);
830 if (max_prio
== -1 || min_prio
== -1)
831 printf("Cannot determine scheduler prio limits!\n");
832 else if (priority
< min_prio
)
834 else if (priority
> max_prio
)
837 memset(&sp
, 0, sizeof(sp
));
838 sp
.sched_priority
= priority
;
840 ret
= sched_setscheduler(getpid(), policy
, &sp
);
842 printf("Cannot set scheduler policy!\n");
846 ret
= sched_setparam(getpid(), &sp
);
848 printf("Cannot set scheduler prio!\n");
855 static inline int ioprio_set(int which
, int who
, int ioprio
)
857 return syscall(SYS_ioprio_set
, which
, who
, ioprio
);
860 static inline int ioprio_get(int which
, int who
)
862 return syscall(SYS_ioprio_get
, which
, who
);
865 static void ioprio_setpid(pid_t pid
, int ioprio
, int ioclass
)
867 int ret
= ioprio_set(ioprio_who_process
, pid
,
868 ioprio
| ioclass
<< IOPRIO_CLASS_SHIFT
);
870 panic("Failed to set io prio for pid!\n");
873 void ioprio_print(void)
875 int ioprio
= ioprio_get(ioprio_who_process
, getpid());
877 panic("Failed to fetch io prio for pid!\n");
879 int ioclass
= ioprio
>> IOPRIO_CLASS_SHIFT
;
880 if (ioclass
!= ioprio_class_idle
) {
882 printf("%s: prio %d\n", to_prio
[ioclass
], ioprio
);
884 printf("%s\n", to_prio
[ioclass
]);
888 void set_ioprio_rt(void)
890 ioprio_setpid(getpid(), 4, ioprio_class_rt
);
893 void set_ioprio_be(void)
895 ioprio_setpid(getpid(), 4, ioprio_class_be
);
900 if (mlockall(MCL_CURRENT
| MCL_FUTURE
) != 0)
901 panic("Cannot lock pages!\n");
909 size_t strlcpy(char *dest
, const char *src
, size_t size
)
911 size_t ret
= strlen(src
);
914 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
916 memcpy(dest
, src
, len
);
923 static inline int vslprintf(char *dst
, size_t size
, const char *fmt
, va_list ap
)
927 ret
= vsnprintf(dst
, size
, fmt
, ap
);
928 dst
[size
- 1] = '\0';
933 int slprintf(char *dst
, size_t size
, const char *fmt
, ...)
939 ret
= vslprintf(dst
, size
, fmt
, ap
);
945 int slprintf_nocheck(char *dst
, size_t size
, const char *fmt
, ...)
951 ret
= vslprintf(dst
, size
, fmt
, ap
);
957 noinline
void *xmemset(void *s
, int c
, size_t n
)
962 for (i
= 0; i
< n
; ++i
)
963 ptr
[i
] = (uint8_t) c
;
968 char *strtrim_right(char *p
, char c
)
986 int get_default_sched_policy(void)
991 int get_default_sched_prio(void)
993 return sched_get_priority_max(get_default_sched_policy());
996 int get_number_cpus(void)
998 return sysconf(_SC_NPROCESSORS_CONF
);
1001 int get_number_cpus_online(void)
1003 return sysconf(_SC_NPROCESSORS_ONLN
);
1006 int get_default_proc_prio(void)
1011 void set_system_socket_memory(int *vals
, size_t len
)
1015 if ((vals
[0] = get_system_socket_mem(sock_rmem_max
)) < SMEM_SUG_MAX
)
1016 set_system_socket_mem(sock_rmem_max
, SMEM_SUG_MAX
);
1017 if ((vals
[1] = get_system_socket_mem(sock_rmem_def
)) < SMEM_SUG_DEF
)
1018 set_system_socket_mem(sock_rmem_def
, SMEM_SUG_DEF
);
1019 if ((vals
[2] = get_system_socket_mem(sock_wmem_max
)) < SMEM_SUG_MAX
)
1020 set_system_socket_mem(sock_wmem_max
, SMEM_SUG_MAX
);
1021 if ((vals
[3] = get_system_socket_mem(sock_wmem_def
)) < SMEM_SUG_DEF
)
1022 set_system_socket_mem(sock_wmem_def
, SMEM_SUG_DEF
);
1025 void reset_system_socket_memory(int *vals
, size_t len
)
1029 set_system_socket_mem(sock_rmem_max
, vals
[0]);
1030 set_system_socket_mem(sock_rmem_def
, vals
[1]);
1031 set_system_socket_mem(sock_wmem_max
, vals
[2]);
1032 set_system_socket_mem(sock_wmem_def
, vals
[3]);
1035 void set_itimer_interval_value(struct itimerval
*itimer
, unsigned long sec
,
1038 itimer
->it_interval
.tv_sec
= sec
;
1039 itimer
->it_interval
.tv_usec
= usec
;
1041 itimer
->it_value
.tv_sec
= sec
;
1042 itimer
->it_value
.tv_usec
= usec
;