1 /*
2   sniff.c
3 
4   Dug Song <dugsong@anzen.com>
5 
6   Copyright (c) 1999 Anzen Computing. All rights reserved.
7 
8   Redistribution and use in source and binary forms, with or without
9   modification, are permitted provided that the following conditions
10   are met:
11 
12   1. Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14   2. Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17   3. All advertising materials mentioning features or use of this software
18      must display the following acknowledgement:
19      This product includes software developed by Anzen Computing.
20   4. Neither the name of Anzen Computing nor the names of its
21      contributors may be used to endorse or promote products derived
22      from this software without specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36   $Id: sniff.c,v 1.25 1999/07/30 13:02:35 dugsong Exp $
37 */
38 
39 #include "config.h"
40 
41 #ifdef STDC_HEADERS
42 #include <stdio.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <sys/param.h>
46 #endif
47 #include <pcap.h>
48 #include <libnet.h>
49 #include "sniff.h"
50 
51 pcap_t *sniff_pd;
52 int sniff_ll_len;
53 
54 int
sniff_init(char * intf,char * ebuf)55 sniff_init(char *intf, char *ebuf)
56 {
57   char *dev, filter[BUFSIZ];
58   struct link_int *llif;
59   struct ether_addr *llmac;
60   u_long llip;
61   u_int net, mask;
62   struct bpf_program fcode;
63 
64   /* Get interface. */
65   if (intf) dev = intf;
66   else if (!(dev = pcap_lookupdev(ebuf)))
67     return 0;
68 
69   /* Get interface IP and MAC address and link layer header length. */
70   if (!(llif = libnet_open_link_interface(dev, ebuf)))
71     return 0;
72 
73   if (!(llip = libnet_get_ipaddr(llif, dev, ebuf)))
74     return 0;
75 
76   if (!(llmac = libnet_get_hwaddr(llif, dev, ebuf)))
77     return 0;
78 
79   libnet_close_link_interface(llif);
80 
81   llip = ntohl(llip);
82 
83   if (!llif->linkoffset)
84     sniff_ll_len = 14; /* XXX - assume Ethernet, if libnet fails us here. */
85   else
86     sniff_ll_len = llif->linkoffset;
87 
88   /* Generate packet filter. We aren't sniffing in promiscuous mode,
89      but be specific in case someone else is. */
90   snprintf(filter, sizeof(filter),
91 	   "ip and ether dst %x:%x:%x:%x:%x:%x and not dst %s and not ip broadcast",
92 	   llmac->ether_addr_octet[0], llmac->ether_addr_octet[1],
93 	   llmac->ether_addr_octet[2], llmac->ether_addr_octet[3],
94 	   llmac->ether_addr_octet[4], llmac->ether_addr_octet[5],
95 	   libnet_host_lookup(llip, 0));
96 #ifdef DEBUG
97   fprintf(stderr, "fragrouter: %s\n", filter);
98 #endif /* DEBUG */
99 
100   /* Open interface for sniffing, don't set promiscuous mode. */
101   if (pcap_lookupnet(dev, &net, &mask, ebuf) == -1)
102     return 0;
103 
104   if (!(sniff_pd = pcap_open_live(dev, 2048, 0, 1024, ebuf)))
105     return 0;
106 
107   if (pcap_compile(sniff_pd, &fcode, filter, 1, mask) < 0) {
108     strcpy(ebuf, pcap_geterr(sniff_pd));
109     return 0;
110   }
111   if (pcap_setfilter(sniff_pd, &fcode) == -1) {
112     strcpy(ebuf, pcap_geterr(sniff_pd));
113     return 0;
114   }
115 #ifdef DEBUG
116   fprintf(stderr, "fragrouter: sniffing on %s\n", dev);
117 #endif /* DEBUG */
118 
119   return 1;
120 }
121 
122 void
sniff_loop(sniff_handler attack)123 sniff_loop(sniff_handler attack)
124 {
125   struct pcap_pkthdr pkthdr;
126   struct ip *iph;
127   u_char *pkt;
128   int len;
129 
130   for (;;) {
131     if ((pkt = (char *)pcap_next(sniff_pd, &pkthdr)) != NULL) {
132       iph = (struct ip *)(pkt + sniff_ll_len);
133 
134       len = ntohs(iph->ip_len);
135       if (len > pkthdr.len)
136 	len = pkthdr.len;
137 
138       attack(pkt + sniff_ll_len, len);
139     }
140   }
141 }
142