iface_mgr_linux.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. /// @file
  15. /// Access to interface information on Linux is via netlink, a socket-based
  16. /// method for transferring information between the kernel and user processes.
  17. ///
  18. /// For detailed information about netlink interface, please refer to
  19. /// http://en.wikipedia.org/wiki/Netlink and RFC3549. Comments in the
  20. /// detectIfaces() method (towards the end of this file) provide an overview
  21. /// on how the netlink interface is used here.
  22. ///
  23. /// Note that this interface is very robust and allows many operations:
  24. /// add/get/set/delete links, addresses, routes, queuing, manipulation of
  25. /// traffic classes, manipulation of neighbourhood tables and even the ability
  26. /// to do something with address labels. Getting a list of interfaces with
  27. /// addresses configured on it is just a small subset of all possible actions.
  28. #include <config.h>
  29. #if defined(OS_LINUX)
  30. #include <asiolink/io_address.h>
  31. #include <dhcp/iface_mgr.h>
  32. #include <exceptions/exceptions.h>
  33. #include <util/io/sockaddr_util.h>
  34. #include <boost/array.hpp>
  35. #include <boost/static_assert.hpp>
  36. #include <stdint.h>
  37. #include <net/if.h>
  38. #include <linux/rtnetlink.h>
  39. using namespace std;
  40. using namespace isc;
  41. using namespace isc::asiolink;
  42. using namespace isc::dhcp;
  43. using namespace isc::util::io::internal;
  44. BOOST_STATIC_ASSERT(IFLA_MAX>=IFA_MAX);
  45. namespace {
  46. /// @brief This class offers utility methods for netlink connection.
  47. ///
  48. /// See IfaceMgr::detectIfaces() (Linux implementation, towards the end of this
  49. /// file) for example usage.
  50. class Netlink
  51. {
  52. public:
  53. /// @brief Holds pointers to netlink messages.
  54. ///
  55. /// netlink (a Linux interface for getting information about network
  56. /// interfaces) uses memory aliasing. Linux kernel returns a memory
  57. /// blob that should be interpreted as series of nlmessages. There
  58. /// are different nlmsg structures defined with varying size. They
  59. /// have one thing common - inital fields are laid out in the same
  60. /// way as nlmsghdr. Therefore different messages can be represented
  61. /// as nlmsghdr with followed variable number of bytes that are
  62. /// message-specific. The only reasonable way to represent this in
  63. /// C++ is to use vector of pointers to nlmsghdr (the common structure).
  64. typedef vector<nlmsghdr*> NetlinkMessages;
  65. /// @brief Holds pointers to interface or address attributes.
  66. ///
  67. /// Note that to get address info, a shorter (IFA_MAX rather than IFLA_MAX)
  68. /// table could be used, but we will use the bigger one anyway to
  69. /// make the code reusable.
  70. ///
  71. /// rtattr is a generic structure, similar to sockaddr. It is defined
  72. /// in linux/rtnetlink.h and shown here for documentation purposes only:
  73. ///
  74. /// struct rtattr {
  75. /// unsigned short<>rta_len;
  76. /// unsigned short<>rta_type;
  77. /// };
  78. typedef boost::array<struct rtattr*, IFLA_MAX + 1> RTattribPtrs;
  79. Netlink() : fd_(-1), seq_(0), dump_(0) {
  80. memset(&local_, 0, sizeof(struct sockaddr_nl));
  81. memset(&peer_, 0, sizeof(struct sockaddr_nl));
  82. }
  83. ~Netlink() {
  84. rtnl_close_socket();
  85. }
  86. void rtnl_open_socket();
  87. void rtnl_send_request(int family, int type);
  88. void rtnl_store_reply(NetlinkMessages& storage, const nlmsghdr* msg);
  89. void parse_rtattr(RTattribPtrs& table, rtattr* rta, int len);
  90. void ipaddrs_get(IfaceMgr::Iface& iface, NetlinkMessages& addr_info);
  91. void rtnl_process_reply(NetlinkMessages& info);
  92. void release_list(NetlinkMessages& messages);
  93. void rtnl_close_socket();
  94. private:
  95. int fd_; // Netlink file descriptor
  96. sockaddr_nl local_; // Local addresses
  97. sockaddr_nl peer_; // Remote address
  98. uint32_t seq_; // Counter used for generating unique sequence numbers
  99. uint32_t dump_; // Number of expected message response
  100. };
  101. /// @brief defines a size of a sent netlink buffer
  102. const static size_t SNDBUF_SIZE = 32768;
  103. /// @brief defines a size of a received netlink buffer
  104. const static size_t RCVBUF_SIZE = 32768;
  105. /// @brief Opens netlink socket and initializes handle structure.
  106. ///
  107. /// @throw isc::Unexpected Thrown if socket configuration fails.
  108. void Netlink::rtnl_open_socket() {
  109. fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  110. if (fd_ < 0) {
  111. isc_throw(Unexpected, "Failed to create NETLINK socket.");
  112. }
  113. if (setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &SNDBUF_SIZE, sizeof(SNDBUF_SIZE)) < 0) {
  114. isc_throw(Unexpected, "Failed to set send buffer in NETLINK socket.");
  115. }
  116. if (setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &RCVBUF_SIZE, sizeof(RCVBUF_SIZE)) < 0) {
  117. isc_throw(Unexpected, "Failed to set receive buffer in NETLINK socket.");
  118. }
  119. local_.nl_family = AF_NETLINK;
  120. local_.nl_groups = 0;
  121. if (bind(fd_, convertSockAddr(&local_), sizeof(local_)) < 0) {
  122. isc_throw(Unexpected, "Failed to bind netlink socket.");
  123. }
  124. socklen_t addr_len = sizeof(local_);
  125. if (getsockname(fd_, convertSockAddr(&local_), &addr_len) < 0) {
  126. isc_throw(Unexpected, "Getsockname for netlink socket failed.");
  127. }
  128. // just 2 sanity checks and we are done
  129. if ( (addr_len != sizeof(local_)) ||
  130. (local_.nl_family != AF_NETLINK) ) {
  131. isc_throw(Unexpected, "getsockname() returned unexpected data for netlink socket.");
  132. }
  133. }
  134. /// @brief Closes netlink communication socket
  135. void Netlink::rtnl_close_socket() {
  136. if (fd_ != -1) {
  137. close(fd_);
  138. }
  139. fd_ = -1;
  140. }
  141. /// @brief Sends request over NETLINK socket.
  142. ///
  143. /// @param family requested information family.
  144. /// @param type request type (RTM_GETLINK or RTM_GETADDR).
  145. void Netlink::rtnl_send_request(int family, int type) {
  146. struct Req {
  147. nlmsghdr netlink_header;
  148. rtgenmsg generic;
  149. };
  150. Req req; // we need this type named for offsetof() used in assert
  151. struct sockaddr_nl nladdr;
  152. // do a sanity check. Verify that Req structure is aligned properly
  153. BOOST_STATIC_ASSERT(sizeof(nlmsghdr) == offsetof(Req, generic));
  154. memset(&nladdr, 0, sizeof(nladdr));
  155. nladdr.nl_family = AF_NETLINK;
  156. // According to netlink(7) manpage, mlmsg_seq must be set to a sequence
  157. // number and is used to track messages. That is just a value that is
  158. // opaque to kernel, and user-space code is supposed to use it to match
  159. // incoming responses to sent requests. That is not really useful as we
  160. // send a single request and get a single response at a time. However, we
  161. // obey the man page suggestion and just set this to monotonically
  162. // increasing numbers.
  163. seq_++;
  164. // This will be used to finding correct response (responses
  165. // sent by kernel are supposed to have the same sequence number
  166. // as the request we sent).
  167. dump_ = seq_;
  168. memset(&req, 0, sizeof(req));
  169. req.netlink_header.nlmsg_len = sizeof(req);
  170. req.netlink_header.nlmsg_type = type;
  171. req.netlink_header.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  172. req.netlink_header.nlmsg_pid = 0;
  173. req.netlink_header.nlmsg_seq = seq_;
  174. req.generic.rtgen_family = family;
  175. int status = sendto(fd_, static_cast<void*>(&req), sizeof(req), 0,
  176. static_cast<struct sockaddr*>(static_cast<void*>(&nladdr)),
  177. sizeof(nladdr));
  178. if (status<0) {
  179. isc_throw(Unexpected, "Failed to send " << sizeof(nladdr)
  180. << " bytes over netlink socket.");
  181. }
  182. }
  183. /// @brief Appends nlmsg to a storage.
  184. ///
  185. /// This method copies pointed nlmsg to a newly allocated memory
  186. /// and adds it to storage.
  187. ///
  188. /// @param storage A vector that holds pointers to netlink messages. The caller
  189. /// is responsible for freeing the pointed-to messages.
  190. /// @param msg A netlink message to be added.
  191. void Netlink::rtnl_store_reply(NetlinkMessages& storage, const struct nlmsghdr *msg)
  192. {
  193. // we need to make a copy of this message. We really can't allocate
  194. // nlmsghdr directly as it is only part of the structure. There are
  195. // many message types with varying lengths and a common header.
  196. struct nlmsghdr* copy = reinterpret_cast<struct nlmsghdr*>(new char[msg->nlmsg_len]);
  197. memcpy(copy, msg, msg->nlmsg_len);
  198. // push_back copies only pointer content, not the pointed-to object.
  199. storage.push_back(copy);
  200. }
  201. /// @brief Parses rtattr message.
  202. ///
  203. /// Some netlink messages represent address information. Such messages
  204. /// are concatenated collection of rtaddr structures. This function
  205. /// iterates over that list and stores pointers to those messages in
  206. /// flat array (table).
  207. ///
  208. /// @param table rtattr Messages will be stored here
  209. /// @param rta Pointer to first rtattr object
  210. /// @param len Length (in bytes) of concatenated rtattr list.
  211. void Netlink::parse_rtattr(RTattribPtrs& table, struct rtattr* rta, int len)
  212. {
  213. std::fill(table.begin(), table.end(), static_cast<struct rtattr*>(NULL));
  214. // RTA_OK and RTA_NEXT() are macros defined in linux/rtnetlink.h
  215. // they are used to handle rtattributes. RTA_OK checks if the structure
  216. // pointed by rta is reasonable and passes all sanity checks.
  217. // RTA_NEXT() returns pointer to the next rtattr structure that
  218. // immediately follows pointed rta structure. See aforementioned
  219. // header for details.
  220. while (RTA_OK(rta, len)) {
  221. if (rta->rta_type < table.size()) {
  222. table[rta->rta_type] = rta;
  223. }
  224. rta = RTA_NEXT(rta,len);
  225. }
  226. if (len) {
  227. isc_throw(Unexpected, "Failed to parse RTATTR in netlink message.");
  228. }
  229. }
  230. /// @brief Parses addr_info and appends appropriate addresses to Iface object.
  231. ///
  232. /// Netlink is a fine, but convoluted interface. It returns a concatenated
  233. /// collection of netlink messages. Some of those messages convey information
  234. /// about addresses. Those messages are in fact appropriate header followed
  235. /// by concatenated lists of rtattr structures that define various pieces
  236. /// of address information.
  237. ///
  238. /// @param iface interface representation (addresses will be added here)
  239. /// @param addr_info collection of parsed netlink messages
  240. void Netlink::ipaddrs_get(IfaceMgr::Iface& iface, NetlinkMessages& addr_info) {
  241. uint8_t addr[V6ADDRESS_LEN];
  242. RTattribPtrs rta_tb;
  243. for (NetlinkMessages::const_iterator msg = addr_info.begin();
  244. msg != addr_info.end(); ++msg) {
  245. ifaddrmsg* ifa = static_cast<ifaddrmsg*>(NLMSG_DATA(*msg));
  246. // These are not the addresses you are looking for
  247. if (ifa->ifa_index != iface.getIndex()) {
  248. continue;
  249. }
  250. if ((ifa->ifa_family == AF_INET6) || (ifa->ifa_family == AF_INET)) {
  251. std::fill(rta_tb.begin(), rta_tb.end(), static_cast<rtattr*>(NULL));
  252. parse_rtattr(rta_tb, IFA_RTA(ifa), (*msg)->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
  253. if (!rta_tb[IFA_LOCAL]) {
  254. rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
  255. }
  256. if (!rta_tb[IFA_ADDRESS]) {
  257. rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
  258. }
  259. memcpy(addr, RTA_DATA(rta_tb[IFLA_ADDRESS]),
  260. ifa->ifa_family==AF_INET?V4ADDRESS_LEN:V6ADDRESS_LEN);
  261. IOAddress a = IOAddress::from_bytes(ifa->ifa_family, addr);
  262. iface.addAddress(a);
  263. /// TODO: Read lifetimes of configured IPv6 addresses
  264. }
  265. }
  266. }
  267. /// @brief Processes reply received over netlink socket.
  268. ///
  269. /// This method parses the received buffer (a collection of concatenated
  270. /// netlink messages), copies each received message to newly allocated
  271. /// memory and stores pointers to it in the "info" container.
  272. ///
  273. /// @param info received netlink messages will be stored here. It is the
  274. /// caller's responsibility to release the memory associated with the
  275. /// messages by calling the release_list() method.
  276. void Netlink::rtnl_process_reply(NetlinkMessages& info) {
  277. sockaddr_nl nladdr;
  278. iovec iov;
  279. msghdr msg;
  280. memset(&msg, 0, sizeof(msghdr));
  281. msg.msg_name = &nladdr;
  282. msg.msg_namelen = sizeof(nladdr);
  283. msg.msg_iov = &iov;
  284. msg.msg_iovlen = 1;
  285. char buf[RCVBUF_SIZE];
  286. iov.iov_base = buf;
  287. iov.iov_len = sizeof(buf);
  288. while (true) {
  289. int status = recvmsg(fd_, &msg, 0);
  290. if (status < 0) {
  291. if (errno == EINTR) {
  292. continue;
  293. }
  294. isc_throw(Unexpected, "Error " << errno
  295. << " while processing reply from netlink socket.");
  296. }
  297. if (status == 0) {
  298. isc_throw(Unexpected, "EOF while reading netlink socket.");
  299. }
  300. nlmsghdr* header = static_cast<nlmsghdr*>(static_cast<void*>(buf));
  301. while (NLMSG_OK(header, status)) {
  302. // Received a message not addressed to our process, or not
  303. // with a sequence number we are expecting. Ignore, and
  304. // look at the next one.
  305. if (nladdr.nl_pid != 0 ||
  306. header->nlmsg_pid != local_.nl_pid ||
  307. header->nlmsg_seq != dump_) {
  308. header = NLMSG_NEXT(header, status);
  309. continue;
  310. }
  311. if (header->nlmsg_type == NLMSG_DONE) {
  312. // End of message.
  313. return;
  314. }
  315. if (header->nlmsg_type == NLMSG_ERROR) {
  316. nlmsgerr* err = static_cast<nlmsgerr*>(NLMSG_DATA(header));
  317. if (header->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
  318. // We are really out of luck here. We can't even say what is
  319. // wrong as error message is truncated. D'oh.
  320. isc_throw(Unexpected, "Netlink reply read failed.");
  321. } else {
  322. isc_throw(Unexpected, "Netlink reply read error " << -err->error);
  323. }
  324. // Never happens we throw before we reach here
  325. return;
  326. }
  327. // store the data
  328. rtnl_store_reply(info, header);
  329. header = NLMSG_NEXT(header, status);
  330. }
  331. if (msg.msg_flags & MSG_TRUNC) {
  332. isc_throw(Unexpected, "Message received over netlink truncated.");
  333. }
  334. if (status) {
  335. isc_throw(Unexpected, "Trailing garbage of " << status << " bytes received over netlink.");
  336. }
  337. }
  338. }
  339. /// @brief releases nlmsg structure
  340. ///
  341. /// @param messages Set of messages to be freed.
  342. void Netlink::release_list(NetlinkMessages& messages) {
  343. // let's free local copies of stored messages
  344. for (NetlinkMessages::iterator msg = messages.begin(); msg != messages.end(); ++msg) {
  345. delete[] (*msg);
  346. }
  347. // ang get rid of the message pointers as well
  348. messages.clear();
  349. }
  350. } // end of anonymous namespace
  351. namespace isc {
  352. namespace dhcp {
  353. /// @brief Detect available interfaces on Linux systems.
  354. ///
  355. /// Uses the socket-based netlink protocol to retrieve the list of interfaces
  356. /// from the Linux kernel.
  357. void IfaceMgr::detectIfaces() {
  358. // Copies of netlink messages about links will be stored here.
  359. Netlink::NetlinkMessages link_info;
  360. // Copies of netlink messages about addresses will be stored here.
  361. Netlink::NetlinkMessages addr_info;
  362. // Socket descriptors and other rtnl-related parameters.
  363. Netlink nl;
  364. // Table with pointers to address attributes.
  365. Netlink::RTattribPtrs attribs_table;
  366. std::fill(attribs_table.begin(), attribs_table.end(),
  367. static_cast<struct rtattr*>(NULL));
  368. // Open socket
  369. nl.rtnl_open_socket();
  370. // Now we have open functional socket, let's use it!
  371. // Ask for list of network interfaces...
  372. nl.rtnl_send_request(AF_PACKET, RTM_GETLINK);
  373. // Get reply and store it in link_info list:
  374. // response is received as with any other socket - just a series
  375. // of bytes. They are representing collection of netlink messages
  376. // concatenated together. rtnl_process_reply will parse this
  377. // buffer, copy each message to a newly allocated memory and
  378. // store pointers to it in link_info. This allocated memory will
  379. // be released later. See release_info(link_info) below.
  380. nl.rtnl_process_reply(link_info);
  381. // Now ask for list of addresses (AF_UNSPEC = of any family)
  382. // Let's repeat, but this time ask for any addresses.
  383. // That includes IPv4, IPv6 and any other address families that
  384. // are happen to be supported by this system.
  385. nl.rtnl_send_request(AF_UNSPEC, RTM_GETADDR);
  386. // Get reply and store it in addr_info list.
  387. // Again, we will allocate new memory and store messages in
  388. // addr_info. It will be released later using release_info(addr_info).
  389. nl.rtnl_process_reply(addr_info);
  390. // Now build list with interface names
  391. for (Netlink::NetlinkMessages::iterator msg = link_info.begin();
  392. msg != link_info.end(); ++msg) {
  393. // Required to display information about interface
  394. struct ifinfomsg* interface_info = static_cast<ifinfomsg*>(NLMSG_DATA(*msg));
  395. int len = (*msg)->nlmsg_len;
  396. len -= NLMSG_LENGTH(sizeof(*interface_info));
  397. nl.parse_rtattr(attribs_table, IFLA_RTA(interface_info), len);
  398. // valgrind reports *possible* memory leak in the line below, but it is
  399. // bogus. Nevertheless, the whole interface definition has been split
  400. // into three separate steps for easier debugging.
  401. const char* tmp = static_cast<const char*>(RTA_DATA(attribs_table[IFLA_IFNAME]));
  402. string iface_name(tmp); // <--- bogus valgrind warning here
  403. Iface iface = Iface(iface_name, interface_info->ifi_index);
  404. iface.setHWType(interface_info->ifi_type);
  405. iface.setFlags(interface_info->ifi_flags);
  406. // Does inetface have LL_ADDR?
  407. if (attribs_table[IFLA_ADDRESS]) {
  408. iface.setMac(static_cast<const uint8_t*>(RTA_DATA(attribs_table[IFLA_ADDRESS])),
  409. RTA_PAYLOAD(attribs_table[IFLA_ADDRESS]));
  410. }
  411. else {
  412. // Tunnels can have no LL_ADDR. RTA_PAYLOAD doesn't check it and
  413. // try to dereference it in this manner
  414. }
  415. nl.ipaddrs_get(iface, addr_info);
  416. ifaces_.push_back(iface);
  417. }
  418. nl.release_list(link_info);
  419. nl.release_list(addr_info);
  420. }
  421. /// @brief sets flag_*_ fields.
  422. ///
  423. /// This implementation is OS-specific as bits have different meaning
  424. /// on different OSes.
  425. ///
  426. /// @param flags flags bitfield read from OS
  427. void IfaceMgr::Iface::setFlags(uint32_t flags) {
  428. flags_ = flags;
  429. flag_loopback_ = flags & IFF_LOOPBACK;
  430. flag_up_ = flags & IFF_UP;
  431. flag_running_ = flags & IFF_RUNNING;
  432. flag_multicast_ = flags & IFF_MULTICAST;
  433. flag_broadcast_ = flags & IFF_BROADCAST;
  434. }
  435. void IfaceMgr::os_send4(struct msghdr& m, boost::scoped_array<char>& control_buf,
  436. size_t control_buf_len, const Pkt4Ptr& pkt) {
  437. // Setting the interface is a bit more involved.
  438. //
  439. // We have to create a "control message", and set that to
  440. // define the IPv4 packet information. We could set the
  441. // source address if we wanted, but we can safely let the
  442. // kernel decide what that should be.
  443. m.msg_control = &control_buf[0];
  444. m.msg_controllen = control_buf_len;
  445. struct cmsghdr* cmsg = CMSG_FIRSTHDR(&m);
  446. cmsg->cmsg_level = IPPROTO_IP;
  447. cmsg->cmsg_type = IP_PKTINFO;
  448. cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  449. struct in_pktinfo* pktinfo =(struct in_pktinfo *)CMSG_DATA(cmsg);
  450. memset(pktinfo, 0, sizeof(struct in_pktinfo));
  451. pktinfo->ipi_ifindex = pkt->getIndex();
  452. m.msg_controllen = cmsg->cmsg_len;
  453. }
  454. bool IfaceMgr::os_receive4(struct msghdr& m, Pkt4Ptr& pkt) {
  455. struct cmsghdr* cmsg;
  456. struct in_pktinfo* pktinfo;
  457. struct in_addr to_addr;
  458. memset(&to_addr, 0, sizeof(to_addr));
  459. cmsg = CMSG_FIRSTHDR(&m);
  460. while (cmsg != NULL) {
  461. if ((cmsg->cmsg_level == IPPROTO_IP) &&
  462. (cmsg->cmsg_type == IP_PKTINFO)) {
  463. pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
  464. pkt->setIndex(pktinfo->ipi_ifindex);
  465. pkt->setLocalAddr(IOAddress(htonl(pktinfo->ipi_addr.s_addr)));
  466. return (true);
  467. // This field is useful, when we are bound to unicast
  468. // address e.g. 192.0.2.1 and the packet was sent to
  469. // broadcast. This will return broadcast address, not
  470. // the address we are bound to.
  471. // XXX: Perhaps we should uncomment this:
  472. // to_addr = pktinfo->ipi_spec_dst;
  473. }
  474. cmsg = CMSG_NXTHDR(&m, cmsg);
  475. }
  476. return (false);
  477. }
  478. } // end of isc::dhcp namespace
  479. } // end of isc namespace
  480. #endif // if defined(LINUX)