File: monitoring.c

package info (click to toggle)
ndpmon 1.4.0-2.1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 4,292 kB
  • ctags: 455
  • sloc: ansic: 5,250; sh: 207; xml: 177; perl: 128; makefile: 105; python: 56
file content (237 lines) | stat: -rw-r--r-- 6,228 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/********************************************************************************
  NDPMon - Neighbor Discovery Protocol Monitor
  Copyright (C) 2006 MADYNES Project, LORIA - INRIA Lorraine (France)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

  Author Info:
Name: Thibault Cholez
Mail: thibault.cholez@esial.uhp-nancy.fr

Maintainer:
Name: Frederic Beck
Mail: frederic.beck@loria.fr

MADYNES Project, LORIA-INRIA Lorraine, hereby disclaims all copyright interest in
the tool 'NDPMon' (Neighbor Discovery Protocol Monitor) written by Thibault Cholez.

Olivier Festor, Scientific Leader of the MADYNEs Project, 20 August 2006
 ***********************************************************************************/

#include "membounds.h"
#include "monitoring.h"


/*Look for mismatch between the source link layer addr and the one anounced
 *in the icmp option*/
int watch_eth_mismatch(char* buffer,  const u_char* packet, struct ether_header* eptr, struct ip6_hdr* ipptr, struct icmp6_hdr* icmpptr, int packet_len)
{
	int jump=0;
	uint8_t  opt_type;
	const u_char* pos;
	struct nd_opt_hdr* optptr;  /*netinet/icmp6.h*/
	struct ether_addr* addr1, *addr2;
	char str_ip[IP6_STR_SIZE];

	switch (icmpptr->icmp6_type)
	{
		case  ND_ROUTER_SOLICIT :
			jump = sizeof(struct nd_router_solicit);
			opt_type=1;
			break;
		case ND_ROUTER_ADVERT:
			jump = sizeof(struct nd_router_advert);
			opt_type=1;
			break;
		case ND_NEIGHBOR_SOLICIT:
			jump = sizeof(struct  nd_neighbor_solicit);
			opt_type=1;
			break;
		case ND_NEIGHBOR_ADVERT:
			jump = sizeof(struct nd_neighbor_advert);
			opt_type=2;
			break;
		case ND_REDIRECT:
			return 0;
			break;
		default:
			return 0;
			break;

	}/*end switch*/


	/*We have to search the link layer option among the others options*/
	pos = packet + ETHERNET_SIZE + IPV6_SIZE + jump;
	optptr = (struct nd_opt_hdr*) ( pos ); 

	while((optptr->nd_opt_type != 0) &&((u_char*)optptr < (packet+packet_len)))
	{
		if(DEBUG)
			print_opt(*optptr);

		if(optptr->nd_opt_type ==  opt_type)
		{
			addr1 = (struct ether_addr*) eptr->ether_shost;
			addr2 = (struct ether_addr*) (pos + sizeof(struct nd_opt_hdr));
			ipv6_ntoa(str_ip, ipptr->ip6_src);

			/*mac addr = 48bits: 6Bytes*8*/
			if(MEMCMP(addr1,addr2,6)!=0)
			{
				char eth1[MAC_STR_SIZE];
				strncpy( eth1, ether_ntoa(addr1), MAC_STR_SIZE); 
				snprintf (buffer, NOTIFY_BUFFER_SIZE, "ethernet mismatch %s %s %s", ether_ntoa(addr2),eth1, str_ip);
				notify(1, buffer, "ethernet mismatch", addr1, str_ip, addr2);
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			/*Next option field*/
			pos += (optptr->nd_opt_len)*8;
			optptr = (struct nd_opt_hdr*) ( pos ); 
		}
	}

	return 0;

}


/*Look if the source mac address is a broadcast addr or is all zeros*/
int watch_eth_broadcast(char* buffer, struct ether_header* eptr, struct ip6_hdr* ipptr)
{
	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct ether_addr* test = malloc(sizeof(struct ether_addr));
	char str_ip[IP6_STR_SIZE];
	int broad =0;


	bzero(test,6);
	if (MEMCMP(eth_addr, test,6) ==0)
		broad=1;
	else
	{
		memset(test,255,6);
		if(MEMCMP(eth_addr, test,6)==0)
			broad= 1; 
		else
		{
			char* test2= "33:33:0:0:0:1";
			if(strcmp(ether_ntoa(eth_addr), test2)==0)
				broad=1;
		}
	}

	if(broad)
	{
		ipv6_ntoa(str_ip, ipptr->ip6_src);
		snprintf (buffer, NOTIFY_BUFFER_SIZE,  "ethernet broadcast %s %s",ether_ntoa(eth_addr), str_ip);
		free(test);
		notify(1, buffer, "ethernet broadcast", eth_addr, str_ip, NULL);
		return 1;
	} else {
	    free(test);
	    return 0;	
	}
	
}


/*Look if the source ip address is a broadcast addr*/
int watch_ip_broadcast(char* buffer, struct ether_header* eptr, struct ip6_hdr* ipptr)
{
	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct in6_addr* ip_addr = &ipptr->ip6_src;
	char str_ip[IP6_STR_SIZE];

	ipv6_ntoa(str_ip, *ip_addr);

	if (IN6_IS_ADDR_MULTICAST(ip_addr))
	{

		snprintf (buffer, NOTIFY_BUFFER_SIZE, "ip multicast %s %s",ether_ntoa(eth_addr),str_ip);
		notify(1, buffer, "ip multicast", eth_addr, str_ip, NULL);
		return 1;

	}
	else
		return 0;
}


/*Look if the source ip address is local to the subnet*/
int watch_bogon(char* buffer, struct ether_header* eptr, struct ip6_hdr* ipptr)
{

	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct in6_addr* ip_addr = &ipptr->ip6_src;
	char str_ip[IP6_STR_SIZE];

	router_list_t *tmp = routers;
	int find = 0;

	ipv6_ntoa(str_ip, *ip_addr);

	while( tmp != NULL)
	{
		prefix_t *ptmp = tmp->prefixes;
		while(ptmp != NULL)
		{
			if(IN6_ARE_PRE_EQUAL(ip_addr, &(ptmp->prefix)))
				find = 1;

			ptmp = ptmp->next;
		}
		tmp = tmp->next;
	}

	if (!find && !IN6_IS_ADDR_UNSPECIFIED(ip_addr)&&!IN6_IS_ADDR_LINKLOCAL(ip_addr)&&!IN6_IS_ADDR_MULTICAST(ip_addr)&&!IN6_IS_ADDR_SITELOCAL(ip_addr))
	{
		snprintf (buffer, NOTIFY_BUFFER_SIZE, "bogon %s %s",ether_ntoa(eth_addr),str_ip);
		notify(1, buffer, "bogon", eth_addr, str_ip, NULL);
		return 1;
	}
	else
		return 0;
}


/* Look if the hop limit is set to 255 */
int watch_hop_limit(char* buffer, struct ether_header* eptr, struct ip6_hdr* ipptr)
{
	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct in6_addr* ip_addr = &ipptr->ip6_src;
	char str_ip[IP6_STR_SIZE];
	int hlim;

	ipv6_ntoa(str_ip, *ip_addr);

	hlim = ipptr->ip6_hlim;

	if(hlim != 255)
	{
		snprintf (buffer, NOTIFY_BUFFER_SIZE, "IPv6 Hop Limit %d", hlim);
		notify(1, buffer, "wrong ipv6 hop limit", eth_addr, str_ip, NULL);
		return 1;
	}

	return 0;
}