Commit Graph

42 Commits

Author SHA1 Message Date
eriknordmark
f9d8f911db more consistent error handling
Signed-off-by: eriknordmark <erik@zededa.com>
2021-05-30 12:55:19 +02:00
Tobias Klauser
4ef7bcbf15 Don't re-initialize or shadow package level var native to fix data race
The package level var native (holding the native endianness) is
initialized at package load time. Thus there is no need to re-initalize
it in functions using it, e.g. (*Handle).filterModify, parseU32Data,
parseFwData, parseBpfData and parseMatchAllData.

This fixes a data race between these functions and any read access of
var native, e.g. in LinkDeserialize as reported in issue #633.

Also don't re-declare local variables shadowing the global package-level
var.

Fixes #633

Signed-off-by: Tobias Klauser <tobias@cilium.io>
2021-05-10 07:24:37 -07:00
Martynas Pumputis
96dce1cb9f Add LinkIndex to Addr struct
Currently, it's cumbersome to get a link by an IP addr - one needs to
list all links and then call AddrList() for each of them.

Considering that ifindex is already available to to the parseAddr()
helper function, we can expose it to a user via the newly added
Addr.LinkIndex field. This makes the retrieving link by IP addr much
more simple.

Signed-off-by: Martynas Pumputis <m@lambda.lt>
2020-06-03 11:20:50 -07:00
Tobias Klauser
8f32382eaa Use IfaCacheinfo and IFA_* consts from golang.org/x/sys/unix
Use the IfaCacheinfo type and the IFA_* consts from
golang.org/x/sys/unix instead of locally duplicating them.
2020-01-27 15:51:47 -08:00
J. Brandt Buckley
aad0baef28 Don't auto-set broadcast unless subnet larger than /31
Since [vishvananda/netlink#248](https://github.com/vishvananda/netlink/pull/248), adding an address automatically sets the broadcast if the broadcast address was not specified. This is undesirable when adding an IP with a prefixlen of /31 or /32. (Additional details in the issues linked below.)

This changes the behavior so that the broadcast is only automatically set if the prefixlen is /30 or larger.

Issue reported in:

- https://github.com/vishvananda/netlink/issues/329
- https://github.com/vishvananda/netlink/issues/471

See also:

- [RFC 3021](http://tools.ietf.org/html/rfc3021)

Alternatives to this PR:

A. https://github.com/vishvananda/netlink/issues/472 - Adds `AddrAddWithoutCalculatedBroadcast`.
B. 9a85a619d2 - Breaking change to make auto-setting the broadcast address an opt-in feature.
C. already works - Suppress setting the broadcast when addr's broadcast address is set to `0.0.0.0`. (This works today, but I'm not sure the behavior can be relied upon as a public API.)
2019-11-13 10:28:39 -08:00
Daniel Borkmann
b1e9859792 netlink: enforce similar pid checks as in iproute2
iproute2's own netlink library asserts that the sockaddr sender pid
has to be the one of the kernel [0]. It also doesn't bail out on pid
mismatch but only skips the message instead. We've seen cases where
the latter had a pid 0; in such case we should skip to the next nl
message instead of hard bail out.

  [0] https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/lib/libnetlink.c
      rtnl_dump_filter_l(), __rtnl_talk_iov()

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-07-18 17:26:53 -07:00
eriknordmark
3a1f6536f6 Make AddrSubscribe more robust against kernel reporting errors 2019-06-10 08:41:22 -07:00
semicomplete
b1cc70dea2 fix prefixlen/local IP, incl. PtP addresses
From libnl addr.c comment:
IPv6 sends the local address as IFA_ADDRESS with no
IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
with IFA_ADDRESS being the peer address if they differ

But obviously, IPv6 sends IFA_LOCAL in case of PtP.

From iproute2 manual:
If a peer address is specified, the local address
cannot have a prefix length. The network prefix is
associated with the peer rather than with the local
address.
2018-06-17 08:27:03 -07:00
eriknordmark
5f5d5cddcf Add a 'ListExisting' option to get the existing entries in the
route/addr/link tables as part of RouteSubscribeWithOptions,
AddrSubscribeWithOptions, and LinkSubscribeWithOptions.
2018-01-23 14:25:48 -08:00
Anatole Denis
7291c36428 addr_linux: Implement CacheInfo installation
Currently, Validity and preference information values are ignored in
addrHandle. This adds them to the netlink message when they are passed
by the caller
2018-01-17 12:00:10 -08:00
Anatole Denis
422ffe659e addr_linux: Skip BROADCAST and LABEL for non-ipv4
The attributes IFA_BROADCAST and IFA_LABEL are only valid for ipv4: they
are not defined for ipv6 here:
http://elixir.free-electrons.com/linux/v4.6/source/net/ipv6/addrconf.c#L4231

This ignores addr.Broadcast and addr.Label for non-ipv4 addresses.
2018-01-17 12:00:10 -08:00
Ian Bishop
0e3b74dbe2 replace syscall with golang.org/x/sys/unix 2017-10-26 09:45:08 -07:00
Vincent Bernat
c684918a4f Add more variant with options to subscribe functions
For link, address, route, add a `WithOptions` variant to the
`*Subscribe()` function to specify a namespace and an error
callback. Those options can be extended in the future without adding
more functions. For example, it could be possible to subscribe only
for a given family by adding a `Family` member to the appropriate
struct.

As a minor change, the private function is always suffixed by `At`,
since it was the case for route and raw netlink functions (but not for
address and link).
2017-09-24 11:01:49 -07:00
Vincent Bernat
46962a8c5d Optionally report subscription fatal errors to a callback function
When a fatal error happens in a `*Subscribe*()` function, the error
was not available to the user. We add a callback function that will be
invoked when such an error happens.

This also modifies the behavior of `AddrSubscribe*()` function to turn
parse errors into fatal errors, as it happens with the other functions.
2017-09-24 11:01:49 -07:00
tcely
f5a6f697a5 Calculate the broadcast address
This is like "ip addr add 127.0.0.1/8 broadcast +" and prevents interfaces without broadcast addresses.

Configuring the IP and netmask, but not the broadcast address leaves many applications in a broken state on Linux interfaces.
2017-08-08 08:43:08 -07:00
Pengfei Ni
1e045880fb Get broadcast addr in AddrList
Signed-off-by: Pengfei Ni <feiskyer@gmail.com>
2017-05-02 09:48:45 -07:00
Toke Høiland-Jørgensen
1d1fee5b18 AddrUpdate: Include flags, scope and lifetimes
Having the extra data available in the notification for new addresses is useful
to, for instance, filter out temporary addresses or keep track of address
lifetimes.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
2017-04-12 14:03:27 -07:00
Toke Høiland-Jørgensen
69df5c75fb addr: Parse address lifetime information from IFA_CACHEINFO attr
This adds parsing of the preferred and valid lifetime information from the
netlink IFA_CACHEINFO attribute. They are stored as PreferedLft and ValidLft in
the Addr struct if found.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
2017-04-12 11:24:37 -07:00
Stuart McLaren
aec6f885c2 Add AddrReplace
Add an AddrReplace call (and tests).

Equivalent to:
 $ ip addr replace $addr dev $link
2017-03-31 10:10:22 -07:00
kishiguro
6b895203a7 Broadcast address can be specified when adding address. 2017-02-02 13:05:14 -08:00
JC Martin
9438c6ff27 Add support for VTI and IPIP 2016-11-22 17:06:50 -08:00
Alessandro Boch
fea1da6ef5 Allow to subscribe to events on a specfic netns (#142)
Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-06-29 11:11:35 -05:00
Alessandro Boch
259a962435 Properly pass IP address flags (#121)
- during ip address configuration

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-05-12 10:51:33 -07:00
Alessandro Boch
f116a3048a Use package empty handle for pkg APIs (#117)
- Package methods only need an empty handle.
  Not a regular Handle with a couple of
  sockets creation/delete.

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-05-09 16:55:00 -07:00
Alessandro Boch
14f41c27fa Provide netlink handle (#104)
- Ties to a netlink socket. All client requests
  will re-use same socket. Socket released at
  handle deletion.
- Also network namespace can be specified during
  handle creation. Socket will be opened on the
  specified network namespace.

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-05-08 11:35:49 -07:00
Everton Marques
7f8533eaaf Filter address family correctly in AddrList() based on message family. 2016-03-22 21:46:16 -03:00
Everton Marques
148ac55d0a Fix return of address family in parseAddr helper.
I broke return of family in commit 9462941794
2016-03-21 14:14:59 -03:00
Everton Marques
988716e658 Return correct underlying error. 2016-03-18 18:08:51 -03:00
Everton Marques
9462941794 Move common code from AddrList,AddrSubscribe into new helper func parseAddr. 2016-03-18 17:44:53 -03:00
Everton Marques
0499fc4776 Merge remote-tracking branch 'upstream/master' 2016-03-18 14:45:31 -03:00
Liu Hua
89b71e9bbf force AddList to get rid of unexpected information
Signed-off-by: Liu Hua <sdu.liu@huawei.com>
2016-02-14 14:04:38 +00:00
Everton Marques
afa33d45e9 AddrSubscribe(): receive updates for changes in link addresses. 2016-01-28 17:27:24 -02:00
Hubert Krauze
f351153998 Remove FlagsMask from Addr struct 2015-12-09 11:30:21 +01:00
Hubert Krauze
87df994490 Add scope and flags support for netlink address 2015-11-26 12:47:18 +01:00
Alexander Morozov
12f4097df1 Fix some style issues as suggested by golint
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
2015-06-18 17:41:46 -07:00
Eugene Yakubovich
efb2ec546f prefer IFA_LOCAL address when dumping addresses
IFA_LOCAL and IFA_ADDRESS are the same for bcast interfaces,
however for point-to-point links, IFA_ADDRESS is the remote
address while IFA_LOCAL is the interface address.
2015-05-09 18:37:25 -07:00
xuzhaokui
c3cc0540f8 fix addradd docs 2014-12-01 11:31:00 +08:00
Vishvananda Ishaya
f87c54f846 Make links have different types 2014-09-24 16:32:16 -07:00
Vishvananda Ishaya
1a26b9f251 Move all low level calls into nl subpackage 2014-09-18 19:04:48 -07:00
Vishvananda Ishaya
349cca1850 Formatting cleanup 2014-09-15 17:06:08 -07:00
Vishvananda Ishaya
0e7e6d493a Convert addr and xfrm to use IPNet pointers 2014-09-07 11:27:46 -07:00
Vishvananda Ishaya
8dab8b7462 Initial commit of netlink package 2014-08-31 20:34:46 -07:00