Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_WAKE_ON_WIFI_H_ |
| 6 | #define SHILL_WAKE_ON_WIFI_H_ |
| 7 | |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 8 | #include <linux/if_ether.h> |
| 9 | #include <netinet/ip.h> |
| 10 | #include <netinet/ip6.h> |
| 11 | |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 15 | #include <base/basictypes.h> |
| 16 | |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 17 | #include "shill/refptr_types.h" |
| 18 | |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 19 | namespace shill { |
| 20 | |
| 21 | class ByteString; |
| 22 | class Error; |
| 23 | class IPAddress; |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 24 | class IPAddressStore; |
| 25 | class GetWakeOnPacketConnMessage; |
| 26 | class Nl80211Message; |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 27 | class SetWakeOnPacketConnMessage; |
| 28 | |
| 29 | namespace WakeOnWifi { |
| 30 | |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 31 | // Used for comparison of ByteString pairs in a set. |
| 32 | bool ByteStringPairIsLessThan(const std::pair<ByteString, ByteString> &lhs, |
| 33 | const std::pair<ByteString, ByteString> &rhs); |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 34 | // Creates a mask which specifies which bytes in pattern of length |pattern_len| |
| 35 | // to match against. Bits |offset| to |pattern_len| - 1 are set, which bits 0 to |
| 36 | // bits 0 to |offset| - 1 are unset. This mask is saved in |mask|. |
| 37 | void SetMask(ByteString *mask, uint32_t pattern_len, uint32_t offset); |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 38 | // Creates a pattern and mask for a NL80211 message that programs the NIC to |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 39 | // wake on packets originating from IP address |ip_addr|. The pattern and mask |
| 40 | // are saved in |pattern| and |mask| respectively. |
| 41 | bool CreateIPAddressPatternAndMask(const IPAddress &ip_addr, |
| 42 | ByteString *pattern, ByteString *mask); |
| 43 | void CreateIPV4PatternAndMask(const IPAddress &ip_addr, ByteString *pattern, |
| 44 | ByteString *mask); |
| 45 | void CreateIPV6PatternAndMask(const IPAddress &ip_addr, ByteString *pattern, |
| 46 | ByteString *mask); |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 47 | // Creates and sets an attribute in a NL80211 message |msg| which indicates the |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 48 | // index of the wiphy device to program. |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 49 | bool ConfigureWiphyIndex(Nl80211Message *msg, int32_t index); |
| 50 | // Creates and sets attributes in an SetWakeOnPacketConnMessage |msg| so that |
| 51 | // the message will deprogram all wake-on-packet rules from the NIC with wiphy |
| 52 | // index |wiphy_index|. |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 53 | // NOTE: Assumes that |msg| has not been altered since construction. |
| 54 | bool ConfigureRemoveAllWakeOnPacketMsg(SetWakeOnPacketConnMessage *msg, |
| 55 | uint32_t wiphy_index, Error *error); |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 56 | // Creates and sets attributes in a SetWakeOnPacketConnMessage |msg| |
| 57 | // so that the message will program the NIC with wiphy index |wiphy_index| to |
| 58 | // wake on packets originating from IP addresses in |addrs|. |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 59 | // NOTE: Assumes that |msg| has not been altered since construction. |
| 60 | bool ConfigureAddWakeOnPacketMsg(SetWakeOnPacketConnMessage *msg, |
Samuel Tan | 3a9c098 | 2014-08-20 13:01:17 -0700 | [diff] [blame] | 61 | const IPAddressStore &addrs, |
| 62 | uint32_t wiphy_index, Error *error); |
| 63 | // Helper function to ConfigureAddWakeOnPacketMsg that creates a single nested |
| 64 | // attribute inside the attribute list referenced by |patterns| representing |
| 65 | // a single wake-on-packet pattern matching rule with index |patnum|. |
| 66 | // NOTE: |patterns| is assumed to reference the nested attribute list |
| 67 | // NL80211_WOWLAN_TRIG_PKT_PATTERN. |
| 68 | // NOTE: |patnum| should be unique across multiple calls to this function to |
| 69 | // prevent the formation of a erroneous nl80211 message or the overwriting of |
| 70 | // pattern matching rules. |
| 71 | bool CreateSinglePattern(const IPAddress &ip_addr, AttributeListRefPtr patterns, |
| 72 | uint8_t patnum, Error *error); |
| 73 | // Creates and sets attributes in an GetWakeOnPacketConnMessage msg| so that |
| 74 | // the message will request for wake-on-packet settings information from the NIC |
| 75 | // with wiphy index |wiphy_index|. |
| 76 | // NOTE: Assumes that |msg| has not been altered since construction. |
| 77 | bool ConfigureGetWakeOnPacketMsg(GetWakeOnPacketConnMessage *msg, |
| 78 | uint32_t wiphy_index, Error *error); |
| 79 | // Given a NL80211_CMD_GET_WOWLAN response or NL80211_CMD_SET_WOWLAN request |
| 80 | // |msg|, returns true if the source IP addresses in |msg| match those in |
| 81 | // |addrs|. Returns false otherwise. |
| 82 | bool WakeOnPacketSettingsMatch(const Nl80211Message &msg, |
| 83 | const IPAddressStore &addrs); |
Samuel Tan | d03b95d | 2014-08-14 11:16:57 -0700 | [diff] [blame] | 84 | |
| 85 | } // namespace WakeOnWifi |
| 86 | |
| 87 | } // namespace shill |
| 88 | |
| 89 | #endif // SHILL_WAKE_ON_WIFI_H_ |