Browse Source

Add BCP38 implementation package (from CeroWrt).

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
Toke Høiland-Jørgensen 10 years ago
parent
commit
0a38785592
4 changed files with 201 additions and 0 deletions
  1. 62
    0
      net/bcp38/Makefile
  2. 22
    0
      net/bcp38/files/bcp38.config
  3. 13
    0
      net/bcp38/files/bcp38.defaults
  4. 104
    0
      net/bcp38/files/run.sh

+ 62
- 0
net/bcp38/Makefile View File

@@ -0,0 +1,62 @@
1
+#
2
+# Copyright (C) 2014 Openwrt.org
3
+#
4
+# This is free software, licensed under the GNU General Public License v2.
5
+
6
+include $(TOPDIR)/rules.mk
7
+
8
+PKG_NAME:=bcp38
9
+PKG_VERSION:=4
10
+PKG_RELEASE:=1
11
+PKG_LICENCE:=GPLv3
12
+
13
+include $(INCLUDE_DIR)/package.mk
14
+
15
+define Package/bcp38
16
+  SECTION:=net
17
+  CATEGORY:=Network
18
+  SUBMENU:=Routing and Redirection
19
+  TITLE:=BCP38 compliance
20
+  URL:=https://github.com/dtaht/ceropackages-3.10
21
+  MAINTAINER:=Toke Høiland-Jørgensen <toke@toke.dk>
22
+  DEPENDS:=+ipset
23
+endef
24
+
25
+define Package/bcp38/description
26
+ bcp38 implements IETF BCP38 for home routers. See https://tools.ietf.org/html/bcp38.
27
+endef
28
+
29
+define Package/bcp38/conffiles
30
+/etc/config/bcp38
31
+endef
32
+
33
+define Build/Prepare
34
+endef
35
+
36
+define Build/Configure
37
+endef
38
+
39
+define Build/Compile
40
+endef
41
+
42
+define Package/bcp38/install
43
+	$(INSTALL_DIR) $(1)/etc/config
44
+	$(INSTALL_CONF) ./files/bcp38.config $(1)/etc/config/bcp38
45
+	$(INSTALL_DIR) $(1)/usr/lib/bcp38
46
+	$(INSTALL_BIN) ./files/run.sh $(1)/usr/lib/bcp38/run.sh
47
+	$(INSTALL_DIR) $(1)/etc/uci-defaults
48
+	$(INSTALL_BIN) ./files/bcp38.defaults $(1)/etc/uci-defaults/bcp38
49
+endef
50
+
51
+define Package/bcp38/postinst
52
+#!/bin/sh
53
+[ -x /etc/uci-defaults/bcp38 ] && /etc/uci-defaults/bcp38 || exit 0
54
+endef
55
+
56
+define Package/bcp38/postrm
57
+#!/bin/sh
58
+uci delete firewall.bcp38
59
+uci commit
60
+endef
61
+
62
+$(eval $(call BuildPackage,bcp38))

+ 22
- 0
net/bcp38/files/bcp38.config View File

@@ -0,0 +1,22 @@
1
+config bcp38
2
+	option enabled 1
3
+	option interface 'ge00'
4
+	option detect_upstream 1
5
+	list match '127.0.0.0/8'
6
+	list match '0.0.0.0/8'       # RFC 1700
7
+	list match '240.0.0.0/4'     # RFC 5745
8
+	list match '192.0.2.0/24'    # RFC 5737
9
+	list match '198.51.100.0/24' # RFC 5737
10
+	list match '203.0.113.0/24'  # RFC 5737
11
+	list match '192.168.0.0/16'  # RFC 1918
12
+	list match '10.0.0.0/8'      # RFC 1918
13
+	list match '172.16.0.0/12'   # RFC 1918
14
+	list match '169.254.0.0/16'  # RFC 3927
15
+
16
+# 	list nomatch '172.26.0.0/21' # Example of something not to match
17
+#	There is a dhcp trigger to do this for the netmask of a 
18
+#	double natted connection needed
19
+
20
+#	I will argue that this level of indirection doesn't scale
21
+# 	very well - see how to block china as an example
22
+#	http://www.okean.com/china.txt

+ 13
- 0
net/bcp38/files/bcp38.defaults View File

@@ -0,0 +1,13 @@
1
+#!/bin/sh
2
+
3
+uci -q batch <<-EOT
4
+	delete firewall.bcp38
5
+	set firewall.bcp38=include
6
+	set firewall.bcp38.type=script
7
+	set firewall.bcp38.path=/usr/lib/bcp38/run.sh
8
+	set firewall.bcp38.family=IPv4
9
+	set firewall.bcp38.reload=1
10
+	commit firewall
11
+EOT
12
+
13
+exit 0

+ 104
- 0
net/bcp38/files/run.sh View File

@@ -0,0 +1,104 @@
1
+#!/bin/sh
2
+# BCP38 filtering implementation for CeroWrt.
3
+#
4
+# This program is free software; you can redistribute it and/or modify it under
5
+# the terms of the GNU General Public License as published by the Free Software
6
+# Foundation; either version 3 of the License, or (at your option) any later
7
+# version.
8
+#
9
+# Author: Toke Høiland-Jørgensen <toke@toke.dk>
10
+
11
+STOP=$1
12
+IPSET_NAME=bcp38-ipv4
13
+IPTABLES_CHAIN=BCP38
14
+
15
+. /lib/functions.sh
16
+
17
+config_load bcp38
18
+
19
+add_bcp38_rule()
20
+{
21
+	local subnet="$1"
22
+	local action="$2"
23
+
24
+	if [ "$action" == "nomatch" ]; then
25
+		ipset add "$IPSET_NAME" "$subnet" nomatch
26
+	else
27
+		ipset add "$IPSET_NAME" "$subnet"
28
+	fi
29
+}
30
+
31
+detect_upstream()
32
+{
33
+	local interface="$1"
34
+
35
+	subnets=$(ip route show dev "$interface"  | grep 'scope link' | awk '{print $1}')
36
+	for subnet in $subnets; do
37
+		# ipset test doesn't work for subnets, so strip out the subnet part
38
+		# and test for that; add as exception if there's a match
39
+		addr=$(echo $subnet | sed 's|/[0-9]\+$||')
40
+		ipset test "$IPSET_NAME" $addr 2>/dev/null && add_bcp38_rule $subnet nomatch
41
+	done
42
+}
43
+
44
+run() {
45
+    	local section="$1"
46
+    	local enabled
47
+	local interface
48
+	local detect_upstream
49
+	config_get_bool enabled "$section" enabled 0
50
+	config_get interface "$section" interface
51
+	config_get detect_upstream "$section" detect_upstream
52
+
53
+	if [ "$enabled" -eq "1" -a -n "$interface" -a -z "$STOP" ] ; then
54
+		setup_ipset
55
+		setup_iptables "$interface"
56
+		config_list_foreach "$section" match add_bcp38_rule match
57
+		config_list_foreach "$section" nomatch add_bcp38_rule nomatch
58
+		[ "$detect_upstream" -eq "1" ] && detect_upstream "$interface"
59
+	fi
60
+	exit 0
61
+}
62
+
63
+setup_ipset()
64
+{
65
+	ipset create "$IPSET_NAME" hash:net family ipv4
66
+	ipset flush "$IPSET_NAME"
67
+}
68
+
69
+setup_iptables()
70
+{
71
+	local interface="$1"
72
+	iptables -N "$IPTABLES_CHAIN" 2>/dev/null
73
+	iptables -F "$IPTABLES_CHAIN" 2>/dev/null
74
+
75
+	iptables -I output_rule -j "$IPTABLES_CHAIN"
76
+	iptables -I input_rule -j "$IPTABLES_CHAIN"
77
+	iptables -I forwarding_rule -j "$IPTABLES_CHAIN"
78
+
79
+	# always accept DHCP traffic
80
+	iptables -A "$IPTABLES_CHAIN" -p udp --dport 67:68 --sport 67:68 -j RETURN
81
+	iptables -A "$IPTABLES_CHAIN" -o "$interface" -m set --match-set "$IPSET_NAME" dst -j REJECT --reject-with icmp-net-unreachable
82
+	iptables -A "$IPTABLES_CHAIN" -i "$interface" -m set --match-set "$IPSET_NAME" src -j DROP
83
+}
84
+
85
+destroy_ipset()
86
+{
87
+	ipset flush "$IPSET_NAME" 2>/dev/null
88
+	ipset destroy "$IPSET_NAME" 2>/dev/null
89
+}
90
+
91
+destroy_iptables()
92
+{
93
+	iptables -D output_rule -j "$IPTABLES_CHAIN" 2>/dev/null
94
+	iptables -D input_rule -j "$IPTABLES_CHAIN" 2>/dev/null
95
+	iptables -D forwarding_rule -j "$IPTABLES_CHAIN" 2>/dev/null
96
+	iptables -F "$IPTABLES_CHAIN" 2>/dev/null
97
+	iptables -X "$IPTABLES_CHAIN" 2>/dev/null
98
+}
99
+
100
+destroy_iptables
101
+destroy_ipset
102
+config_foreach run bcp38
103
+
104
+exit 0