-
Notifications
You must be signed in to change notification settings - Fork 64
feat: add support for setting/getting the wireless regulatory region #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -372,6 +372,76 @@ func (c *client) SetWriteDeadline(t time.Time) error { | |
| return c.c.SetWriteDeadline(t) | ||
| } | ||
|
|
||
| // ReloadRegulatoryDatabase reloads the wireless regulatory database. | ||
| // | ||
| // This can be used if cfg80211 was built into the kernel and the wireless regulatory database | ||
| // was not available during early boot. | ||
| // | ||
| // See https://wireless.docs.kernel.org/en/latest/en/developers/regulatory/wireless-regdb.html | ||
| func (c *client) ReloadRegulatoryDatabase() error { | ||
| _, err := c.get( | ||
| unix.NL80211_CMD_RELOAD_REGDB, | ||
| netlink.Acknowledge, | ||
| nil, | ||
| nil, | ||
| ) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| // GetRegulatoryDomain gets the system-wide regulatory region used by all nl80211 devices. | ||
| // See | ||
| // - https://wireless.docs.kernel.org/en/latest/en/developers/regulatory/wireless-regdb.html | ||
| // - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/wireless/nl80211.c?h=a55f7f5f29b32c2c53cc291899cf9b0c25a07f7c#n9920 | ||
| func (c *client) GetRegulatoryDomain() (*RegulatoryDomain, error) { | ||
| msgs, err := c.get( | ||
| unix.NL80211_CMD_GET_REG, | ||
| netlink.Request, | ||
| nil, | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // We expect one message which represents the global regulatory domain. | ||
| if len(msgs) == 0 { | ||
| return nil, os.ErrNotExist | ||
| } | ||
|
|
||
| attrs, err := netlink.UnmarshalAttributes(msgs[0].Data) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var domain RegulatoryDomain | ||
| if err := domain.parseAttributes(attrs); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &domain, nil | ||
| } | ||
|
|
||
| // SetRegulatoryRegion sets the system-wide regulatory region used by all nl80211 devices. | ||
| // You may need to call [client.ReloadRegulatoryDatabase] first to ensure the region is updated correctly. | ||
| // | ||
| // region must be an ISO 3166-1 alpha-2 country code (e.g. "GB" or "US"). | ||
| // | ||
| // See https://wireless.docs.kernel.org/en/latest/en/developers/regulatory/wireless-regdb.html | ||
| func (c *client) SetRegulatoryRegion(region string, hint RegulatoryHint) error { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense for I had a look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also worth checking for the rest of the requests
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one currently implemented only has the hint, the one linked is very similar but slightly different 😅 It seems the |
||
| _, err := c.get( | ||
| unix.NL80211_CMD_REQ_SET_REG, | ||
| netlink.Acknowledge, | ||
| nil, | ||
| func(ae *netlink.AttributeEncoder) { | ||
| ae.String(unix.NL80211_ATTR_REG_ALPHA2, region) | ||
|
tomasff marked this conversation as resolved.
|
||
| ae.Uint32(unix.NL80211_ATTR_USER_REG_HINT_TYPE, uint32(hint)) | ||
| }, | ||
| ) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| // get performs a request/response interaction with nl80211. | ||
| func (c *client) get( | ||
| cmd uint8, | ||
|
|
@@ -1063,3 +1133,15 @@ found: | |
|
|
||
| return (features[feature/8]&(1<<(feature%8)) != 0), nil | ||
| } | ||
|
|
||
| // parseAttributes parses netlink attributes into a RegulatoryDomain's fields. | ||
| func (d *RegulatoryDomain) parseAttributes(attrs []netlink.Attribute) error { | ||
| for _, a := range attrs { | ||
| switch a.Type { | ||
| case unix.NL80211_ATTR_REG_ALPHA2: | ||
| d.Region = nlenc.String(a.Data) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,3 +150,52 @@ func TestClient_AccessPoints(t *testing.T) { | |
|
|
||
| } | ||
| } | ||
|
|
||
| func TestClient_ReloadRegulatoryDatabase(t *testing.T) { | ||
| if os.Geteuid() != 0 { | ||
| t.Skipf("skipping, must be run as root") | ||
| } | ||
|
|
||
| c, err := wifi.New() | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| err = c.ReloadRegulatoryDatabase() | ||
| if err != nil { | ||
| t.Fatalf("failed to reload the regulatory database") | ||
| } | ||
| } | ||
|
|
||
| func TestClient_SetRegulatoryRegion(t *testing.T) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentionally simple as we cannot assume the regulatory database is available. Could maybe check the region after with the caveat that the test may be flaky if the regulatory DB is not available. |
||
| if os.Geteuid() != 0 { | ||
| t.Skipf("skipping, must be run as root") | ||
| } | ||
|
|
||
| c, err := wifi.New() | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| err = c.SetRegulatoryRegion("00", wifi.RegulatoryHintUser) | ||
| if err != nil { | ||
| t.Fatalf("failed to set the regulatory domain") | ||
| } | ||
| } | ||
|
|
||
| func TestClient_GetRegulatoryDomain(t *testing.T) { | ||
| c, err := wifi.New() | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| domain, err := c.GetRegulatoryDomain() | ||
| if err != nil { | ||
| t.Fatalf("failed to retrieve regulatory domain") | ||
| } | ||
|
|
||
| // At a minimum, the region will be 00. | ||
| if len(domain.Region) != 2 { | ||
| t.Fatalf("failed to retrieve regulatory domain region") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This supports specifying the radio instead of globally scoped, however, given SetRegulatoryRegion is already globally scopped it seemed appropriate to make it consistent.
If needed, we can add a
GetInterfaceRegulatoryDomain(*Interface)later and extract this to a common handler - let me know what you think