Line data Source code
1 : /*
2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #ifndef vnsw_agent_dhcp_handler_base_hpp
7 : #define vnsw_agent_dhcp_handler_base_hpp
8 :
9 : #include <bitset>
10 : #include "pkt/proto_handler.h"
11 : #include "vnc_cfg_types.h"
12 :
13 : #define DHCP_BASE_TRACE(arg) \
14 : do { \
15 : std::ostringstream _str; \
16 : _str << arg; \
17 : DhcpTrace(_str.str()); \
18 : } while (false) \
19 :
20 : // DHCP protocol handler Base Class
21 : class DhcpHandlerBase : public ProtoHandler {
22 : public:
23 : enum DhcpOptionCategory {
24 : None,
25 : NoData, // no data for this option
26 : Bool, // data has bool value ( 0 or 1)
27 : Byte, // data has byte value
28 : ByteArray, // data has array of bytes
29 : ByteString, // data has byte followed by string
30 : ByteOneIPPlus, // data has byte followed by one or more IPs
31 : String, // data has a string
32 : Int32bit, // data 32 bit int
33 : Uint32bit, // data has 32 bit uint
34 : Uint16bit, // data has 16 bit uint
35 : Uint16bitArray, // data has array of 16 bit uint
36 : OneIPv4, // data has one IPv4
37 : ZeroIPv4Plus, // data has zero or more IPv4s
38 : OneIPv4Plus, // data has one or more IPv4s
39 : TwoIPv4Plus, // data has multiples of two IPv4s
40 : OneIPv6, // data has one IPv6
41 : OneIPv6Plus, // data has one or more IPv6s
42 : ClasslessRoute, // data is classless route option
43 : NameCompression, // data is name compressed (rfc1035)
44 : NameCompressionArray, // data is name compressed (rfc1035)
45 : ByteNameCompression // data is byte followed by name compressed (rfc1035)
46 : };
47 :
48 : enum DhcpOptionLevel {
49 : Invalid,
50 : InterfaceLevel,
51 : SubnetLevel,
52 : IpamLevel
53 : };
54 :
55 : struct ConfigRecord {
56 0 : ConfigRecord() : plen(0), lease_time(-1), valid_time(-1), preferred_time(-1) {}
57 :
58 : IpAddress ip_addr;
59 : uint32_t subnet_mask;
60 : uint32_t bcast_addr;
61 : IpAddress gw_addr;
62 : IpAddress dns_addr;
63 : uint32_t plen;
64 : uint32_t lease_time;
65 : uint32_t valid_time;
66 : uint32_t preferred_time;
67 : std::string client_name_;
68 : std::string domain_name_;
69 : };
70 :
71 : struct DhcpOptionHandler {
72 0 : virtual ~DhcpOptionHandler() {}
73 : virtual void WriteData(uint8_t c, uint8_t l, const void *d,
74 : uint16_t *optlen) = 0;
75 : virtual void AppendData(uint16_t l, const void *d,
76 : uint16_t *optlen) = 0;
77 : virtual uint16_t GetCode() const = 0;
78 : virtual uint16_t GetLen() const = 0;
79 : virtual uint16_t GetFixedLen() const = 0;
80 : virtual uint8_t *GetData() = 0;
81 : virtual void SetCode(uint16_t len) = 0;
82 : virtual void SetLen(uint16_t len) = 0;
83 : virtual void AddLen(uint16_t len) = 0;
84 : virtual void SetNextOptionPtr(uint16_t optlen) = 0;
85 : virtual void SetDhcpOptionPtr(uint8_t *hdr) = 0;
86 : };
87 :
88 : DhcpHandlerBase(Agent *agent, boost::shared_ptr<PktInfo> info,
89 : boost::asio::io_context &io);
90 : virtual ~DhcpHandlerBase();
91 :
92 : protected:
93 : uint16_t AddNoDataOption(uint32_t option, uint16_t opt_len);
94 : uint16_t AddByteOption(uint32_t option, uint16_t opt_len,
95 : const std::string &input);
96 : uint16_t AddByteArrayOption(uint32_t option, uint16_t opt_len,
97 : const std::string &input);
98 : uint16_t AddByteStringOption(uint32_t option, uint16_t opt_len,
99 : const std::string &input);
100 : uint16_t AddByteIPOption(uint32_t option, uint16_t opt_len,
101 : const std::string &input);
102 : uint16_t AddStringOption(uint32_t option, uint16_t opt_len,
103 : const std::string &input);
104 : uint16_t AddIntegerOption(uint32_t option, uint16_t opt_len,
105 : const std::string &input);
106 : uint16_t AddShortArrayOption(uint32_t option, uint16_t opt_len,
107 : const std::string &input, bool array);
108 : bool IsValidIpOption(uint32_t option, const std::string &ipstr, bool is_v4);
109 : bool IsValidDnsOption(uint32_t option, const std::string &ipstr);
110 : uint16_t AddIpv4Option(uint32_t option, uint16_t opt_len,
111 : const std::string &input, uint8_t min_count,
112 : uint8_t max_count, uint8_t multiples);
113 : uint16_t AddIpv6Option(uint32_t option, uint16_t opt_len,
114 : const std::string &input, bool list);
115 : virtual uint16_t AddIP(uint16_t opt_len,
116 : const std::string &input) = 0;
117 : uint16_t AddCompressedNameOption(uint32_t option, uint16_t opt_len,
118 : const std::string &input, bool list);
119 : uint16_t AddByteCompressedNameOption(uint32_t option, uint16_t opt_len,
120 : const std::string &input);
121 : uint16_t AddCompressedName(uint16_t opt_len, const std::string &input);
122 : uint16_t AddDnsServers(uint16_t opt_len);
123 : void ReadClasslessRoute(uint32_t option, uint16_t opt_len,
124 : const std::string &input);
125 : uint16_t AddClasslessRouteOption(uint16_t opt_len);
126 : uint16_t AddConfigDhcpOptions(uint16_t opt_len, bool is_v6);
127 : uint16_t AddDhcpOptions(uint16_t opt_len,
128 : std::vector<autogen::DhcpOptionType> &options,
129 : DhcpOptionLevel level);
130 : void FindDomainName(const IpAddress &vm_addr);
131 : bool CanOverrideWithBytes(DhcpOptionCategory category);
132 :
133 : virtual void DhcpTrace(const std::string &msg) const = 0;
134 :
135 : virtual DhcpOptionCategory OptionCategory(uint32_t option) const = 0;
136 : virtual uint32_t OptionCode(const std::string &option) const = 0;
137 :
138 0 : bool is_flag_set(uint8_t flag) const { return flags_[flag]; }
139 0 : void set_flag(uint8_t flag) { flags_.set(flag); }
140 0 : bool is_dns_enabled() const { return dns_enable_; }
141 :
142 : VmInterface *vm_itf_;
143 : uint32_t vm_itf_index_;
144 : ConfigRecord config_;
145 : boost::scoped_ptr<DhcpOptionHandler> option_; // option being processed
146 :
147 : // bitset to indicate whether these options are added to the response or not
148 : std::bitset<256> flags_;
149 : // flag to indicate if vrouter based DNS proxy is enabled or not
150 : bool dns_enable_;
151 :
152 : // store the DHCP routers coming in config
153 : std::string routers_;
154 :
155 : // parse and store the host routes coming in config
156 : std::vector<OperDhcpOptions::HostRoute> host_routes_;
157 : DhcpOptionLevel host_routes_level_;
158 :
159 : std::string ipam_name_;
160 : autogen::IpamType ipam_type_;
161 : autogen::VirtualDnsType vdns_type_;
162 :
163 : // siaddr for tftp used for baremetal deployment
164 : uint32_t siaddr_tftp_;
165 :
166 : DISALLOW_COPY_AND_ASSIGN(DhcpHandlerBase);
167 : };
168 :
169 : #endif // vnsw_agent_dhcp_handler_base_hpp
|