summaryrefslogtreecommitdiff
path: root/python/vyos/ifconfig/dhcp.py
blob: d4ff9c2cd2daf740b2a1a0f4dfdf74b7bb5623dd (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
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
# Copyright 2020 VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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, see <http://www.gnu.org/licenses/>.

import os

from vyos.dicts import FixedDict
from vyos.ifconfig.control import Control
from vyos.template import render


class _DHCP (Control):
    client_base = r'/var/lib/dhcp/dhclient_'

    def __init__(self, ifname, version, **kargs):
        super().__init__(**kargs)
        self.version = version
        self.file = {
            'ifname': ifname,
            'conf': self.client_base + ifname + '.' + version + 'conf',
            'pid': self.client_base + ifname + '.' + version + 'pid',
            'lease': self.client_base + ifname + '.' + version + 'leases',
        }

class _DHCPv4 (_DHCP):
    def __init__(self, ifname):
        super().__init__(ifname, '')
        self.options = FixedDict(**{
            'ifname': ifname,
            'hostname': '',
            'client_id': '',
            'vendor_class_id': ''
        })

    # replace dhcpv4/v6 with systemd.networkd?
    def set(self):
        """
        Configure interface as DHCP client. The dhclient binary is automatically
        started in background!

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v4.set()
        """

        if not self.options['hostname']:
            # read configured system hostname.
            # maybe change to vyos hostd client ???
            with open('/etc/hostname', 'r') as f:
                self.options['hostname'] = f.read().rstrip('\n')

        render(self.file['conf'], 'dhcp-client/ipv4.tmpl' ,self.options)

        cmd = 'start-stop-daemon'
        cmd += ' --start'
        cmd += ' --oknodo'
        cmd += ' --quiet'
        cmd += ' --pidfile {pid}'
        cmd += ' --exec /sbin/dhclient'
        cmd += ' --'
        # now pass arguments to dhclient binary
        cmd += ' -4 -nw -cf {conf} -pf {pid} -lf {lease} {ifname}'
        return self._cmd(cmd.format(**self.file))

    def delete(self):
        """
        De-configure interface as DHCP clinet. All auto generated files like
        pid, config and lease will be removed.

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.dhcp.v4.delete()
        """
        if not os.path.isfile(self.file['pid']):
            self._debug_msg('No DHCP client PID found')
            return None

	# with open(self.file['pid'], 'r') as f:
	# 	pid = int(f.read())

        # stop dhclient, we need to call dhclient and tell it should release the
        # aquired IP address. tcpdump tells me:
        # 172.16.35.103.68 > 172.16.35.254.67: [bad udp cksum 0xa0cb -> 0xb943!] BOOTP/DHCP, Request from 00:50:56:9d:11:df, length 300, xid 0x620e6946, Flags [none] (0x0000)
        #  Client-IP 172.16.35.103
        #  Client-Ethernet-Address 00:50:56:9d:11:df
        #  Vendor-rfc1048 Extensions
        #    Magic Cookie 0x63825363
        #    DHCP-Message Option 53, length 1: Release
        #    Server-ID Option 54, length 4: 172.16.35.254
        #    Hostname Option 12, length 10: "vyos"
        #
        cmd = '/sbin/dhclient -cf {conf} -pf {pid} -lf {lease} -r {ifname}'
        self._cmd(cmd.format(**self.file))

        # cleanup old config files
        for name in ('conf', 'pid', 'lease'):
            if os.path.isfile(self.file[name]):
                os.remove(self.file[name])


class _DHCPv6 (_DHCP):
    def __init__(self, ifname):
        super().__init__(ifname, 'v6')
        self.options = FixedDict(**{
            'ifname': ifname,
            'dhcpv6_prm_only': False,
            'dhcpv6_temporary': False,
        })
        self.file.update({
            'accept_ra': f'/proc/sys/net/ipv6/conf/{ifname}/accept_ra',
        })

    def set(self):
        """
        Configure interface as DHCPv6 client. The dhclient binary is automatically
        started in background!

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.set_dhcpv6()
        """

        # better save then sorry .. should be checked in interface script
        # but if you missed it we are safe!
        if self.options['dhcpv6_prm_only'] and self.options['dhcpv6_temporary']:
            raise Exception(
                'DHCPv6 temporary and parameters-only options are mutually exclusive!')

        render(self.file['conf'], 'dhcp-client/ipv6.tmpl', self.options)

        # no longer accept router announcements on this interface
        self._write_sysfs(self.file['accept_ra'], 0)

        # assemble command-line to start DHCPv6 client (dhclient)
        cmd = 'start-stop-daemon'
        cmd += ' --start'
        cmd += ' --oknodo'
        cmd += ' --quiet'
        cmd += ' --pidfile {pid}'
        cmd += ' --exec /sbin/dhclient'
        cmd += ' --'
        # now pass arguments to dhclient binary
        cmd += ' -6 -nw -cf {conf} -pf {pid} -lf {lease}'
        # add optional arguments
        if self.options['dhcpv6_prm_only']:
            cmd += ' -S'
        if self.options['dhcpv6_temporary']:
            cmd += ' -T'
        cmd += ' {ifname}'

        return self._cmd(cmd.format(**self.file))

    def delete(self):
        """
        De-configure interface as DHCPv6 clinet. All auto generated files like
        pid, config and lease will be removed.

        Example:

        >>> from vyos.ifconfig import Interface
        >>> j = Interface('eth0')
        >>> j.del_dhcpv6()
        """
        if not os.path.isfile(self.file['pid']):
            self._debug_msg('No DHCPv6 client PID found')
            return None

        # with open(self.file['pid'], 'r') as f:
        # 	pid = int(f.read())

        # stop dhclient
        cmd = 'start-stop-daemon'
        cmd += ' --start'
        cmd += ' --oknodo'
        cmd += ' --quiet'
        cmd += ' --pidfile {pid}'
        self._cmd(cmd.format(**self.file))

        # accept router announcements on this interface
        self._write_sysfs(self.options['accept_ra'], 1)

        # cleanup old config files
        for name in ('conf', 'pid', 'lease'):
            if os.path.isfile(self.file[name]):
                os.remove(self.file[name])


class DHCP (object):
    def __init__(self, ifname):
        self.v4 = _DHCPv4(ifname)
        self.v6 = _DHCPv6(ifname)