summaryrefslogtreecommitdiffstats
path: root/kernel/include/linux/netfilter/ip_set_getport.h
blob: ffa89f115eea7faa5b4380ff5dc398dbfe62b133 (plain)
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
#ifndef _IP_SET_GETPORT_H
#define _IP_SET_GETPORT_H

#ifdef __KERNEL__
#include <linux/netfilter_ipv6/ip6_tables.h>
#include <net/ip.h>

#define IPSET_INVALID_PORT	65536

/* We must handle non-linear skbs */
static bool
get_port(u8 pf, const struct sk_buff *skb, bool src, u16 *port)
{
	unsigned short protocol;
	unsigned int protoff;
	int fragoff;
	
	switch (pf) {
	case AF_INET: {
		const struct iphdr *iph = ip_hdr(skb);

		protocol = iph->protocol;
		fragoff = ntohs(iph->frag_off) & IP_OFFSET;
		protoff = ip_hdrlen(skb);
		break;
	}
	case AF_INET6: {
		int protohdr;
		unsigned short frag_off;
		
		protohdr = ipv6_find_hdr(skb, &protoff, -1, &frag_off);
		if (protohdr < 0)
			return false;

		protocol = protohdr;
		fragoff = frag_off;
		break;
	}
	default:
		return false;
	}

	/* See comments at tcp_match in ip_tables.c */
	if (fragoff)
		return false;

	switch (protocol) {
	case IPPROTO_TCP: {
		struct tcphdr _tcph;
		const struct tcphdr *th;
		
		th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
		if (th == NULL)
			/* No choice either */
			return false;
	     	
	     	*port = src ? th->source : th->dest;
	     	break;
	    }
	case IPPROTO_UDP: {
		struct udphdr _udph;
		const struct udphdr *uh;

		uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
		if (uh == NULL)
			/* No choice either */
			return false;
	     	
	     	*port = src ? uh->source : uh->dest;
	     	break;
	    }
	default:
		return false;
	}
	return true;
}
#endif /* __KERNEL__ */

#endif /*_IP_SET_GETPORT_H*/