about summary refs log tree commit diff stats
path: root/core/network.html
blob: 5913845bf4e3386bf066b7bdc26265157a619654 (plain) (blame)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>2.3. Network</title>
    </head>
    <body>
        <a href="index.html">Core OS Index</a>

        <h1>2.3. Network</h1>

        <p>Operation of the network can be handle with init scripts;</p>

        <dl>
            <dt><a href="conf/rc.d/iptables">/etc/rc.d/iptables</a></dt>
            <dd>Configure <a href="#iptables">iptables</a>, start option
            loads set of rules from file /etc/iptables/net.v4, open option
            allows everything to outside and blocks everything from outside,
            stop will block and log everything.</dd>
            <dt><a href="conf/rc.d/net">/etc/rc.d/net</a></dt>
            <dd>Configure Ethernet interface with static or dynamic (dhcp)
            IP, set default route and add default gateway.</dd>
            <dt><a href="conf/rc.d/wlan">/etc/rc.d/wlan</a></dt>
            <dd>Configure Wireless interface, launch wpa_supplicant to handle
            wireless authenticationand dynamic (dhcp)
            connection to router and add as default gateway.</dd>
        </dl>

        <p>Choose wireless or net as connection to outside world and configure
        <a href="conf/rc.conf">/etc/rc.conf</a> to run at startup, example
        connecting using wireless interface;</p>

        <pre>
        #
        # /etc/rc.conf: system configuration
        #

        FONT=default
        KEYMAP=dvorak
        TIMEZONE="Europe/Lisbon"
        HOSTNAME=machine
        SYSLOG=sysklogd
        SERVICES=(lo iptables wlan crond)

        # End of file
        </pre>

        <p>If is first boot after install configure iptables and one of above
        described scripts then proceed to
        <a href="package.html#sysup">update system.</a></p>

        <h2 id="resolv">2.3.1. Resolver</h2>

        <p>This example will use
        <a href="http://www.chaoscomputerclub.de/en/censorship/dns-howto">Chaos Computer Club</a>
        server, edit /etc/resolv.conf and make it immutable;</p>

        <pre>
        # /etc/resolv.conf.head can replace this line
        nameserver 213.73.91.35
        # /etc/resolv.conf.tail can replace this line
        </pre>

        <pre>
        # chattr +i /etc/resolv.conf
        </pre>

        <h2 id="static">2.3.2. Static IP</h2>

        <p>Current example of <a href="conf/rc.d/net">/etc/rc.d/net</a>;</p>

        <pre>
        Address:   192.168.0.1           11000000.10101000.00000000 .00000001
        Netmask:   255.255.255.0 = 24    11111111.11111111.11111111 .00000000
        Wildcard:  0.0.0.255             00000000.00000000.00000000 .11111111
        =>
        Network:   192.168.0.0/24        11000000.10101000.00000000 .00000000 (Class C)
        Broadcast: 192.168.0.255         11000000.10101000.00000000 .11111111
        HostMin:   192.168.0.1           11000000.10101000.00000000 .00000001
        HostMax:   192.168.0.254         11000000.10101000.00000000 .11111110
        Hosts/Net: 254                   (Private Internet)
        </pre>

        <p>Other IP class that can used for private network;</p>

        <pre>
        Address:   10.0.0.1              00001010.00000000.00000000 .00000001
        Netmask:   255.255.255.0 = 24    11111111.11111111.11111111 .00000000
        Wildcard:  0.0.0.255             00000000.00000000.00000000 .11111111
        =>
        Network:   10.0.0.0/24           00001010.00000000.00000000 .00000000 (Class A)
        Broadcast: 10.0.0.255            00001010.00000000.00000000 .11111111
        HostMin:   10.0.0.1              00001010.00000000.00000000 .00000001
        HostMax:   10.0.0.254            00001010.00000000.00000000 .11111110
        Hosts/Net: 254                   (Private Internet)
        </pre>

        <p>Manual configuring like net script;</p>

        <pre>
        # DEV=enp8s0
        # ADDR=192.168.1.9
        # MASK=24
        # GW=192.168.1.254
        </pre>

        <pre>
        # ip addr flush dev ${DEV}
        # ip route flush dev ${DEV}
        # ip addr add ${ADDR}/${MASK} dev ${DEV} broadcast +
        # ip link set ${DEV} up
        # ip route add default via ${GW}
        </pre>

        <h2 id="iptables">2.3.3. Iptables</h2>

        <p>For more information about firewall systems read arch wiki
        <a href="https://wiki.archlinux.org/index.php/Iptables">iptables</a>
        an <a href="https://wiki.archlinux.org/index.php/nftables">nftables</a>.</p>

        <p>To setup iptables rules a set of scripts is used, init script
        <a href="conf/rc.d/iptables">/etc/rc.d/iptables</a>
        loads set of rules from file /etc/iptables/net.v4 at boot time.
        Start option "open" option allows everything to outside
        and blocks everything from outside, "stop" will block and log
        everything. Setup init script and rules ;</p>

        <pre>
        # mkdir /etc/iptables
        # cp core/conf/iptables/net.v4 /etc/iptables/
        # cp core/conf/rc.d/iptables /etc/rc.d/
        # chmod +x /etc/rc.d/iptables
        </pre>

        <p>Change /etc/rc.conf and add iptables;</p>

        <pre>
        SERVICES=(iptables lo net crond)
        </pre>

        <p>See current rules and packets counts;</p>

        <pre>
        # iptables -L -n -v | less
        </pre>

        <p>Diagram of a package route throw iptables;</p>

        <pre>

                                 XXXXXXXXXXXXXXXXX
                                 XXXX Network XXXX
                                 XXXXXXXXXXXXXXXXX
                                         +
                                         |
                                         v
   +-------------+              +------------------+
   |table: filter| &gt;---+        | table: nat       |
   |chain: INPUT |     |        | chain: PREROUTING|
   +-----+-------+     |        +--------+---------+
         |             |                 |
         v             |                 v
   [local process]     |           ****************          +--------------+
         |             +---------+ Routing decision +------&lt; |table: filter |
         v                         ****************          |chain: FORWARD|
  ****************                                           +------+-------+
  Routing decision                                                  |
  ****************                                                  |
         |                                                          |
         v                        ****************                  |
  +-------------+       +------&lt;  Routing decision  &gt;---------------+
  |table: nat   |       |         ****************
  |chain: OUTPUT|       |               +
  +-----+-------+       |               |
        |               |               v
        v               |      +-------------------+
  +--------------+      |      | table: nat        |
  |table: filter | +----+      | chain: POSTROUTING|
  |chain: OUTPUT |             +--------+----------+
  +--------------+                      |
                                        v
                                XXXXXXXXXXXXXXXXX
                                XXXX Network XXXX
                                XXXXXXXXXXXXXXXXX

        </pre>

        <p>Command line usage;</p>

        <pre>
        iptables [-t table] {-A|-C|-D} chain rule-specification
        iptables [-t table] {-A|-C|-D} chain  rule-specification
        iptables  [-t table] -I chain [rulenum] rule-specification
        iptables [-t table] -R chain rulenum  rule-specification
        iptables [-t table] -D chain rulenum
        iptables [-t table] -S [chain [rulenum]]
        iptables  [-t  table]  {-F|-L|-Z} [chain [rulenum]] [options...]
        iptables [-t table] -N chain
        iptables [-t table] -X [chain]
        iptables [-t table] -P chain target
        iptables [-t table]  -E  old-chain-name  new-chain-name
        rule-specification = [matches...] [target]
        match = -m matchname [per-match-options]
        </pre>

        <p>Targets, can be a user defined chain;</p>

        <pre>
        ACCEPT - accepts the packet
        DROP   - drop the packet on the floor
        QUEUE  - packet will be stent to queue
        RETURN - stop traversing this chain and
                 resume ate the next rule in the
                 previeus (calling) chain.

        if packet reach the end of the chain or
        a target RETURN, default policy for that
        chain is applayed.
        </pre>

        <p>Target Extensions</p>

        <pre>
        AUDIT
        CHECKSUM
        CLASSIFY
        DNAT
        DSCP
        LOG
            Torn on kernel logging, will print some
            some information on all matching packets.
            Log data can be read with dmesg or syslogd.
            This is a non-terminating target and a rule
            should be created with matching criteria.

            --log-level level
                  Level of logging (numeric or see sys-
                  log.conf(5)

            --log-prefix prefix
                  Prefix log messages with specified prefix
                  up to 29 chars log

            --log-uid
                  Log the userid of the process with gener-
                  ated the packet
        NFLOG
            This target pass the packet to loaded logging
            backend to log the packet. One or more userspace
            processes may subscribe to the group to receive
            the packets.

        ULOG
            This target provides userspace logging of maching
            packets. One or more userspace processes may then
            then subscribe to various multicast groups and
            then receive the packets.
        </pre>

        <p>Commands</p>
        <pre>
         -A, --append chain rule-specification
         -C, --check chain rule-specification
         -D, --delete chain rule-specification
         -D, --delete chain rulenum
         -I, --insert chain [rulenum] rule-specification
         -R, --replace chain rulenum rule-specification
         -L, --list [chain]
         -P, --policy chain target
        </pre>

        <p>Parameters</p>
        <pre>
         -p, --protocol protocol
               tcp, udp, udplite, icmp, esp, ah, sctp, all
         -s, --source address[/mask][,...]
         -d, --destination address[/mask][,...]
         -j, --jump target
         -g, --goto chain
         -i, --in-interface name
         -o, --out-interface name
         -f, --fragment
         -m, --match options module-name
               iptables can use extended packet matching
               modules.
         -c, --set-counters packets bytes
        </pre>

        <h3 id="ipt_server">2.3.3.1. Server iptables</h3>

        <p>Adjust <a href="scripts/iptables.sh">iptables.sh</a> with
        your network configuration then run it;</p>

        <p>Default configuration;</p>

        <pre>
    server)

        echo "Setting server network..."
        ####### Input Chain ######
        $IPT -A INPUT -j blocker

	$IPT -A INPUT -i ${PUB_IF} -d ${PUB_IP} -s ${DNS} -j cli_dns_in
	$IPT -A INPUT -i ${PUB_IF} -d ${PUB_IP} -s ${BR_NET} -j srv_https_in
	$IPT -A INPUT -i ${PUB_IF} -d ${PUB_IP} -s ${BR_NET} -j srv_ssh_in
	$IPT -A INPUT -i ${PUB_IF} -d ${PUB_IP} -s ${BR_NET} -j srv_git_in
	$IPT -A INPUT -i ${PUB_IF} -d ${PUB_IP} -j cli_https_in

        ####### Output Chain ######
        $IPT -A OUTPUT -j blocker

	$IPT -A OUTPUT -o ${PUB_IF} -d ${DNS} -s ${PUB_IP} -j cli_dns_out
	$IPT -A OUTPUT -o ${PUB_IF} -d ${BR_NET} -s ${PUB_IP} -j srv_https_out
	$IPT -A OUTPUT -o ${PUB_IF} -d ${BR_NET} -s ${PUB_IP} -j srv_ssh_out
	$IPT -A OUTPUT -o ${PUB_IF} -d ${BR_NET} -s ${PUB_IP} -j srv_git_out
	$IPT -A OUTPUT -o ${PUB_IF} -s ${PUB_IP} -j cli_https_out

        ## log everything else and drop
        iptables_log

        iptables-save > /etc/iptables/net.v4
        exit 0

        ;;
        </pre>

        <pre>
        # bash core/scripts/iptables.sh
        </pre>

        <h3 id="ipt_client">2.3.3.2. Client iptables </h3>

        <p></p>
        <h3 id="ipt_client">2.3.3.3. Bridge iptables</h3>

        <pre>
        $IPT -A FORWARD -j blocker
        $IPT -A FORWARD -i lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT
        $IPT -A FORWARD -o lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT

        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in ${PUB_IF} --physdev-out tap2 -d ${BR_NET} -j srv_ssh_in
        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in tap2 --physdev-out ${PUB_IF} -j srv_ssh_out

        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in ${PUB_IF} --physdev-out tap2 -j srv_https_in
        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in tap2 --physdev-out ${PUB_IF} -j srv_https_out

        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in ${PUB_IF} --physdev-out tap2 -j srv_rip

        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in tap2 --physdev-out ${PUB_IF} -j srv_dhcp
        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in ${PUB_IF} --physdev-out tap2 -j srv_dhcp

        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in tap2 --physdev-out ${PUB_IF} -j cli_https_out
        $IPT -A FORWARD -i ${BR_IF} -o ${BR_IF} -m physdev --physdev-in ${PUB_IF} --physdev-out tap2 -j cli_https_in
       <pre>

        <h2 id="wpa">2.3.4. Wpa and dhcpd</h2>

        <p>There is more information on
        <a href="http://crux.nu/Wiki/WifiStartScripts">Wiki Wifi Start Scripts</a> and
        see <a href="conf/rc.d/wlan">/etc/rc.d/wlan</a>. Manual or first time configuration;</p>

        <pre>
        # ip link
        </pre>

        <pre>
        # iwlist wlp2s0 scan
        </pre>

        <pre>
        # iwconfig wlp2s0 essid NAME key s:ABCDE12345
        </pre>

        <h3>2.3.4.1. Wpa Supplicant</h3>

        <p>Configure wpa supplicant edit;</p>

        <pre>
        # vim /etc/wpa_supplicant.conf
        </pre>

        <pre>
        ctrl_interface=/var/run/wpa_supplicant
        update_config=1
        fast_reauth=1
        ap_scan=1
        </pre>

        <pre>
        # wpa_passphrase &lt;ssid&gt; &lt;password&gt; &gt;&gt; /etc/wpa_supplicant.conf
        </pre>

        <p>Now start wpa_supplicant with:</p>

        <pre>
        # wpa_supplicant -B -i wlp2s0 -c /etc/wpa_supplicant.conf
        Successfully initialized wpa_supplicant
        </pre>

        <p>Use <a href="conf/rc.d/wlan">/etc/rc.d/wlan</a>
        init script to auto load wpa configuration and dhcp
        client.</p>

        <h3>2.3.4.2. Wpa Cli</h3>

        <pre>
        # wpa_cli
        &gt; status
        </pre>

        <pre>
        &gt; add_network
        3
        </pre>

        <pre>
        &gt; set_network 3 ssid "Crux-Network"
        OK
        </pre>

        <pre>
        &gt; set_network 3 psk "uber-secret-pass"
        OK
        </pre>

        <pre>
        &gt; enable_network 3
        OK
        </pre>

        <pre>
        &gt; list_networks
        </pre>

        <pre>
        &gt; select_network 3
        </pre>

        <pre>
        &gt; save_config
        </pre>


        <a href="index.html">Core OS Index</a>
        <p>
        This is part of the Hive System Documentation.
        Copyright (C) 2018
        Hive Team.
        See the file <a href="../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>

    </body>
</html>