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