mildsunrise / node_netlink

⚒ Use Netlink from Node.js
MIT License
22 stars 2 forks source link

unable to add route #25

Open MorningLightMountain713 opened 4 months ago

MorningLightMountain713 commented 4 months ago

Hi there, I was able to add an address to an interface and bring it up, but I can't add a route for some reason.

Here is a route similar to the one I'm adding, obtained via getRoutes()

[
  {
    kind: 'route',
    data: {
      family: 2,
      dstLen: 24,
      srcLen: 0,
      tos: 0,
      table: 254,
      protocol: 'BOOT',
      scope: 'UNIVERSE',
      type: 'UNICAST',
      flags: {}
    },
    attrs: {
      table: 254,
      dst: <Buffer ac 17 2c 00>,
      gateway: <Buffer 0a 0a 09 00>,
      oif: 66
    }
  }
]

Here is the route using ip route:

davew@chud:~$ ip route | grep 172.23.44
172.23.44.0/24 via 10.10.9.0 dev wgB

I'm basically copying the route above, just using a different dst address. However, when I do this, I get an error.

Here is the interface (and index) I'm using as the gateway:

davew@chud:~$ ip addr show wgB
66: wgB: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
    link/none
    inet 10.10.9.0/31 scope global wgB
       valid_lft forever preferred_lft forever

The first object printed below is the Route object i'm passing to newRoute and the second object is the RouteAttrs object. The oif is the interface index.

{
  family: 2,
  dstLen: 24,
  srcLen: 0,
  tos: 0,
  table: 254,
  protocol: 'BOOT',
  scope: 'UNIVERSE',
  type: 'UNICAST',
  flags: {}
}
{
  table: 254,
  dst: <Buffer ac 10 4d 00>,
  gateway: <Buffer 0a 0a 09 00>,
  oif: 66
}
dst: 172.16.77.0
gateway: 10.10.9.0
/home/davew/netlink/node_modules/netlink/dist/netlink.js:323
    throw Error(`Request rejected: ${code}`);
          ^

Error: Request rejected: ENOENT
    at checkError (/home/davew/netlink/node_modules/netlink/dist/netlink.js:323:11)
    at /home/davew/netlink/node_modules/netlink/dist/netlink.js:201:42
    at async RtNetlinkSocket.request (/home/davew/netlink/node_modules/netlink/dist/rt/rt.js:89:30)
    at async RtNetlinkSocket.newRoute (/home/davew/netlink/node_modules/netlink/dist/rt/rt.js:332:22)
    at async file:///home/davew/netlink/getDev.js:99:1

Node.js v20.9.0

Here is the code:

import { createRtNetlink } from 'netlink'

const socket = createRtNetlink();

const route = {
  family: 2,
  dstLen: 24,
  srcLen: 0,
  tos: 0,
  table: 254,
  protocol: "BOOT",
  scope: "UNIVERSE",
  type: "UNICAST",
  flags: {},
};
const dstAdd = "172.16.77.0"
const gatewayAdd = "10.10.9.0"
const dst = Buffer.from(dstAdd.split("."))
const gateway = Buffer.from(gatewayAdd.split("."))

const routeAttrs = { table: 254, dst, gateway, oif: 66 };

console.log(route);
console.log(routeAttrs);
console.log("dst:", dstAdd);
console.log("gateway:", gatewayAdd);

await socket.newRoute(route, routeAttrs)

What am I doing wrong? Thanks.

MorningLightMountain713 commented 4 months ago

I figured this out with strace.

About 6 hours later lol.

I was missing the create flag. Here is working code

import { createRtNetlink, FlagsNew } from 'netlink'
const socket = createRtNetlink()

const route = { family: 2, dstLen: 24, table: 254, protocol: 'BOOT', scope: 'UNIVERSE', type: 'UNICAST' }
const dstAdd = "192.168.99.0"
const gatewayAdd = "10.10.9.0"
const dst = Buffer.from(dstAdd.split("."))
const gateway = Buffer.from(gatewayAdd.split("."))

const routeAttrs = { dst, gateway }
const options = { flags: FlagsNew.CREATE }

await socket.newRoute(route, routeAttrs, options)

works like a charm now :)