Coverage Report

Created: 2026-03-21 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/zebra/if_netlink.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * Interface looking up by netlink.
4
 * Copyright (C) 1998 Kunihiro Ishiguro
5
 */
6
7
#include <zebra.h>
8
9
#ifdef GNU_LINUX
10
11
/* The following definition is to workaround an issue in the Linux kernel
12
 * header files with redefinition of 'struct in6_addr' in both
13
 * netinet/in.h and linux/in6.h.
14
 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
15
 */
16
#define _LINUX_IN6_H
17
#define _LINUX_IF_H
18
#define _LINUX_IP_H
19
20
#include <netinet/if_ether.h>
21
#include <linux/if_bridge.h>
22
#include <linux/if_link.h>
23
#include <linux/if_tunnel.h>
24
#include <net/if_arp.h>
25
#include <linux/sockios.h>
26
#include <linux/ethtool.h>
27
28
#include "linklist.h"
29
#include "if.h"
30
#include "log.h"
31
#include "prefix.h"
32
#include "connected.h"
33
#include "table.h"
34
#include "memory.h"
35
#include "rib.h"
36
#include "frrevent.h"
37
#include "privs.h"
38
#include "nexthop.h"
39
#include "vrf.h"
40
#include "vrf_int.h"
41
#include "mpls.h"
42
#include "lib_errors.h"
43
44
#include "vty.h"
45
#include "zebra/zserv.h"
46
#include "zebra/zebra_ns.h"
47
#include "zebra/zebra_vrf.h"
48
#include "zebra/rt.h"
49
#include "zebra/redistribute.h"
50
#include "zebra/interface.h"
51
#include "zebra/debug.h"
52
#include "zebra/rtadv.h"
53
#include "zebra/zebra_ptm.h"
54
#include "zebra/zebra_mpls.h"
55
#include "zebra/kernel_netlink.h"
56
#include "zebra/rt_netlink.h"
57
#include "zebra/if_netlink.h"
58
#include "zebra/zebra_errors.h"
59
#include "zebra/zebra_vxlan.h"
60
#include "zebra/zebra_evpn_mh.h"
61
#include "zebra/zebra_l2.h"
62
#include "zebra/netconf_netlink.h"
63
#include "zebra/zebra_trace.h"
64
65
extern struct zebra_privs_t zserv_privs;
66
uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
67
68
/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
69
   names and ifindex values. */
70
static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
71
      struct zebra_ns *zns)
72
0
{
73
0
  struct interface *oifp;
74
75
0
  if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
76
0
      && (oifp != ifp)) {
77
0
    if (ifi_index == IFINDEX_INTERNAL)
78
0
      flog_err(
79
0
        EC_LIB_INTERFACE,
80
0
        "Netlink is setting interface %s ifindex to reserved internal value %u",
81
0
        ifp->name, ifi_index);
82
0
    else {
83
0
      if (IS_ZEBRA_DEBUG_KERNEL)
84
0
        zlog_debug(
85
0
          "interface index %d was renamed from %s to %s",
86
0
          ifi_index, oifp->name, ifp->name);
87
0
      if (if_is_up(oifp))
88
0
        flog_err(
89
0
          EC_LIB_INTERFACE,
90
0
          "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
91
0
          ifi_index, oifp->name, ifp->name);
92
0
      if_delete_update(&oifp);
93
0
    }
94
0
  }
95
0
  if_set_index(ifp, ifi_index);
96
0
}
97
98
/* Utility function to parse hardware link-layer address and update ifp */
99
static void netlink_interface_update_hw_addr(struct rtattr **tb,
100
               struct interface *ifp)
101
0
{
102
0
  int i;
103
104
0
  if (tb[IFLA_ADDRESS]) {
105
0
    int hw_addr_len;
106
107
0
    hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
108
109
0
    if (hw_addr_len > INTERFACE_HWADDR_MAX)
110
0
      zlog_debug("Hardware address is too large: %d",
111
0
           hw_addr_len);
112
0
    else {
113
0
      ifp->hw_addr_len = hw_addr_len;
114
0
      memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
115
0
             hw_addr_len);
116
117
0
      for (i = 0; i < hw_addr_len; i++)
118
0
        if (ifp->hw_addr[i] != 0)
119
0
          break;
120
121
0
      if (i == hw_addr_len)
122
0
        ifp->hw_addr_len = 0;
123
0
      else
124
0
        ifp->hw_addr_len = hw_addr_len;
125
0
    }
126
0
  }
127
0
}
128
129
static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
130
0
{
131
0
  switch (hwt) {
132
0
  case ARPHRD_ETHER:
133
0
    return ZEBRA_LLT_ETHER;
134
0
  case ARPHRD_EETHER:
135
0
    return ZEBRA_LLT_EETHER;
136
0
  case ARPHRD_AX25:
137
0
    return ZEBRA_LLT_AX25;
138
0
  case ARPHRD_PRONET:
139
0
    return ZEBRA_LLT_PRONET;
140
0
  case ARPHRD_IEEE802:
141
0
    return ZEBRA_LLT_IEEE802;
142
0
  case ARPHRD_ARCNET:
143
0
    return ZEBRA_LLT_ARCNET;
144
0
  case ARPHRD_APPLETLK:
145
0
    return ZEBRA_LLT_APPLETLK;
146
0
  case ARPHRD_DLCI:
147
0
    return ZEBRA_LLT_DLCI;
148
0
  case ARPHRD_ATM:
149
0
    return ZEBRA_LLT_ATM;
150
0
  case ARPHRD_METRICOM:
151
0
    return ZEBRA_LLT_METRICOM;
152
0
  case ARPHRD_IEEE1394:
153
0
    return ZEBRA_LLT_IEEE1394;
154
0
  case ARPHRD_EUI64:
155
0
    return ZEBRA_LLT_EUI64;
156
0
  case ARPHRD_INFINIBAND:
157
0
    return ZEBRA_LLT_INFINIBAND;
158
0
  case ARPHRD_SLIP:
159
0
    return ZEBRA_LLT_SLIP;
160
0
  case ARPHRD_CSLIP:
161
0
    return ZEBRA_LLT_CSLIP;
162
0
  case ARPHRD_SLIP6:
163
0
    return ZEBRA_LLT_SLIP6;
164
0
  case ARPHRD_CSLIP6:
165
0
    return ZEBRA_LLT_CSLIP6;
166
0
  case ARPHRD_RSRVD:
167
0
    return ZEBRA_LLT_RSRVD;
168
0
  case ARPHRD_ADAPT:
169
0
    return ZEBRA_LLT_ADAPT;
170
0
  case ARPHRD_ROSE:
171
0
    return ZEBRA_LLT_ROSE;
172
0
  case ARPHRD_X25:
173
0
    return ZEBRA_LLT_X25;
174
0
  case ARPHRD_PPP:
175
0
    return ZEBRA_LLT_PPP;
176
0
  case ARPHRD_CISCO:
177
0
    return ZEBRA_LLT_CHDLC;
178
0
  case ARPHRD_LAPB:
179
0
    return ZEBRA_LLT_LAPB;
180
0
  case ARPHRD_RAWHDLC:
181
0
    return ZEBRA_LLT_RAWHDLC;
182
0
  case ARPHRD_TUNNEL:
183
0
    return ZEBRA_LLT_IPIP;
184
0
  case ARPHRD_TUNNEL6:
185
0
    return ZEBRA_LLT_IPIP6;
186
0
  case ARPHRD_FRAD:
187
0
    return ZEBRA_LLT_FRAD;
188
0
  case ARPHRD_SKIP:
189
0
    return ZEBRA_LLT_SKIP;
190
0
  case ARPHRD_LOOPBACK:
191
0
    return ZEBRA_LLT_LOOPBACK;
192
0
  case ARPHRD_LOCALTLK:
193
0
    return ZEBRA_LLT_LOCALTLK;
194
0
  case ARPHRD_FDDI:
195
0
    return ZEBRA_LLT_FDDI;
196
0
  case ARPHRD_SIT:
197
0
    return ZEBRA_LLT_SIT;
198
0
  case ARPHRD_IPDDP:
199
0
    return ZEBRA_LLT_IPDDP;
200
0
  case ARPHRD_IPGRE:
201
0
    return ZEBRA_LLT_IPGRE;
202
0
  case ARPHRD_PIMREG:
203
0
    return ZEBRA_LLT_PIMREG;
204
0
  case ARPHRD_HIPPI:
205
0
    return ZEBRA_LLT_HIPPI;
206
0
  case ARPHRD_ECONET:
207
0
    return ZEBRA_LLT_ECONET;
208
0
  case ARPHRD_IRDA:
209
0
    return ZEBRA_LLT_IRDA;
210
0
  case ARPHRD_FCPP:
211
0
    return ZEBRA_LLT_FCPP;
212
0
  case ARPHRD_FCAL:
213
0
    return ZEBRA_LLT_FCAL;
214
0
  case ARPHRD_FCPL:
215
0
    return ZEBRA_LLT_FCPL;
216
0
  case ARPHRD_FCFABRIC:
217
0
    return ZEBRA_LLT_FCFABRIC;
218
0
  case ARPHRD_IEEE802_TR:
219
0
    return ZEBRA_LLT_IEEE802_TR;
220
0
  case ARPHRD_IEEE80211:
221
0
    return ZEBRA_LLT_IEEE80211;
222
0
#ifdef ARPHRD_IEEE802154
223
0
  case ARPHRD_IEEE802154:
224
0
    return ZEBRA_LLT_IEEE802154;
225
0
#endif
226
#ifdef ARPHRD_IP6GRE
227
  case ARPHRD_IP6GRE:
228
    return ZEBRA_LLT_IP6GRE;
229
#endif
230
0
#ifdef ARPHRD_IEEE802154_PHY
231
0
  case ARPHRD_IEEE802154_PHY:
232
0
    return ZEBRA_LLT_IEEE802154_PHY;
233
0
#endif
234
235
0
  default:
236
0
    return ZEBRA_LLT_UNKNOWN;
237
0
  }
238
0
}
239
240
static inline void zebra_if_set_ziftype(struct interface *ifp,
241
          enum zebra_iftype zif_type,
242
          enum zebra_slave_iftype zif_slave_type)
243
0
{
244
0
  struct zebra_if *zif;
245
246
0
  zif = (struct zebra_if *)ifp->info;
247
0
  zif->zif_slave_type = zif_slave_type;
248
249
0
  if (zif->zif_type != zif_type) {
250
0
    zif->zif_type = zif_type;
251
    /* If the if_type has been set to bond initialize ES info
252
     * against it. XXX - note that we don't handle the case where
253
     * a zif changes from bond to non-bond; it is really
254
     * an unexpected/error condition.
255
     */
256
0
    zebra_evpn_if_init(zif);
257
0
  }
258
0
}
259
260
static void netlink_determine_zebra_iftype(const char *kind,
261
             enum zebra_iftype *zif_type)
262
0
{
263
0
  *zif_type = ZEBRA_IF_OTHER;
264
265
0
  if (!kind)
266
0
    return;
267
268
0
  if (strcmp(kind, "vrf") == 0)
269
0
    *zif_type = ZEBRA_IF_VRF;
270
0
  else if (strcmp(kind, "bridge") == 0)
271
0
    *zif_type = ZEBRA_IF_BRIDGE;
272
0
  else if (strcmp(kind, "vlan") == 0)
273
0
    *zif_type = ZEBRA_IF_VLAN;
274
0
  else if (strcmp(kind, "vxlan") == 0)
275
0
    *zif_type = ZEBRA_IF_VXLAN;
276
0
  else if (strcmp(kind, "macvlan") == 0)
277
0
    *zif_type = ZEBRA_IF_MACVLAN;
278
0
  else if (strcmp(kind, "veth") == 0)
279
0
    *zif_type = ZEBRA_IF_VETH;
280
0
  else if (strcmp(kind, "bond") == 0)
281
0
    *zif_type = ZEBRA_IF_BOND;
282
0
  else if (strcmp(kind, "bond_slave") == 0)
283
0
    *zif_type = ZEBRA_IF_BOND_SLAVE;
284
0
  else if (strcmp(kind, "gre") == 0)
285
0
    *zif_type = ZEBRA_IF_GRE;
286
0
}
287
288
static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
289
             uint32_t ns_id, const char *name)
290
0
{
291
0
  struct ifinfomsg *ifi;
292
0
  struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
293
0
  struct rtattr *attr[IFLA_VRF_MAX + 1];
294
0
  struct vrf *vrf = NULL;
295
0
  struct zebra_vrf *zvrf;
296
0
  uint32_t nl_table_id;
297
298
0
  ifi = NLMSG_DATA(h);
299
300
0
  netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
301
302
0
  if (!linkinfo[IFLA_INFO_DATA]) {
303
0
    if (IS_ZEBRA_DEBUG_KERNEL)
304
0
      zlog_debug(
305
0
        "%s: IFLA_INFO_DATA missing from VRF message: %s",
306
0
        __func__, name);
307
0
    return;
308
0
  }
309
310
0
  netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
311
0
            linkinfo[IFLA_INFO_DATA]);
312
0
  if (!attr[IFLA_VRF_TABLE]) {
313
0
    if (IS_ZEBRA_DEBUG_KERNEL)
314
0
      zlog_debug(
315
0
        "%s: IFLA_VRF_TABLE missing from VRF message: %s",
316
0
        __func__, name);
317
0
    return;
318
0
  }
319
320
0
  nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
321
322
0
  if (h->nlmsg_type == RTM_NEWLINK) {
323
0
    if (IS_ZEBRA_DEBUG_KERNEL)
324
0
      zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
325
0
           ifi->ifi_index, nl_table_id);
326
327
0
    if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
328
0
      vrf_id_t exist_id;
329
330
0
      exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
331
0
      if (exist_id != VRF_DEFAULT) {
332
0
        vrf = vrf_lookup_by_id(exist_id);
333
334
0
        flog_err(
335
0
          EC_ZEBRA_VRF_MISCONFIGURED,
336
0
          "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
337
0
          name, ifi->ifi_index, vrf->name);
338
0
        exit(-1);
339
0
      }
340
0
    }
341
342
0
    vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
343
0
    if (!vrf) {
344
0
      flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
345
0
         name, ifi->ifi_index);
346
0
      return;
347
0
    }
348
349
    /*
350
     * This is the only place that we get the actual kernel table_id
351
     * being used.  We need it to set the table_id of the routes
352
     * we are passing to the kernel.... And to throw some totally
353
     * awesome parties. that too.
354
     *
355
     * At this point we *must* have a zvrf because the vrf_create
356
     * callback creates one.  We *must* set the table id
357
     * before the vrf_enable because of( at the very least )
358
     * static routes being delayed for installation until
359
     * during the vrf_enable callbacks.
360
     */
361
0
    zvrf = (struct zebra_vrf *)vrf->info;
362
0
    zvrf->table_id = nl_table_id;
363
364
    /* Enable the created VRF. */
365
0
    if (!vrf_enable(vrf)) {
366
0
      flog_err(EC_LIB_INTERFACE,
367
0
         "Failed to enable VRF %s id %u", name,
368
0
         ifi->ifi_index);
369
0
      return;
370
0
    }
371
372
0
  } else // h->nlmsg_type == RTM_DELLINK
373
0
  {
374
0
    if (IS_ZEBRA_DEBUG_KERNEL)
375
0
      zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
376
0
           ifi->ifi_index);
377
378
0
    vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
379
380
0
    if (!vrf) {
381
0
      flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
382
0
          __func__);
383
0
      return;
384
0
    }
385
386
0
    vrf_delete(vrf);
387
0
  }
388
0
}
389
390
static uint32_t get_iflink_speed(struct interface *interface, int *error)
391
0
{
392
0
  struct ifreq ifdata;
393
0
  struct ethtool_cmd ecmd;
394
0
  int sd;
395
0
  int rc;
396
0
  const char *ifname = interface->name;
397
398
0
  if (error)
399
0
    *error = 0;
400
  /* initialize struct */
401
0
  memset(&ifdata, 0, sizeof(ifdata));
402
403
  /* set interface name */
404
0
  strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
405
406
  /* initialize ethtool interface */
407
0
  memset(&ecmd, 0, sizeof(ecmd));
408
0
  ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
409
0
  ifdata.ifr_data = (caddr_t)&ecmd;
410
411
  /* use ioctl to get speed of an interface */
412
0
  frr_with_privs(&zserv_privs) {
413
0
    sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
414
0
        interface->vrf->vrf_id, NULL);
415
0
    if (sd < 0) {
416
0
      if (IS_ZEBRA_DEBUG_KERNEL)
417
0
        zlog_debug("Failure to read interface %s speed: %d %s",
418
0
             ifname, errno, safe_strerror(errno));
419
      /* no vrf socket creation may probably mean vrf issue */
420
0
      if (error)
421
0
        *error = -1;
422
0
      return 0;
423
0
    }
424
    /* Get the current link state for the interface */
425
0
    rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
426
0
             (char *)&ifdata);
427
0
  }
428
0
  if (rc < 0) {
429
0
    if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
430
0
      zlog_debug(
431
0
        "IOCTL failure to read interface %s speed: %d %s",
432
0
        ifname, errno, safe_strerror(errno));
433
    /* no device means interface unreachable */
434
0
    if (errno == ENODEV && error)
435
0
      *error = -1;
436
0
    ecmd.speed_hi = 0;
437
0
    ecmd.speed = 0;
438
0
  }
439
440
0
  close(sd);
441
442
0
  return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
443
0
}
444
445
uint32_t kernel_get_speed(struct interface *ifp, int *error)
446
0
{
447
0
  return get_iflink_speed(ifp, error);
448
0
}
449
450
static ssize_t
451
netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
452
          size_t buflen)
453
0
{
454
0
  struct {
455
0
    struct nlmsghdr n;
456
0
    struct ifinfomsg ifi;
457
0
    char buf[];
458
0
  } *req = buf;
459
0
  uint32_t link_idx;
460
0
  unsigned int mtu;
461
0
  struct rtattr *rta_info, *rta_data;
462
0
  const struct zebra_l2info_gre *gre_info;
463
464
0
  if (buflen < sizeof(*req))
465
0
    return 0;
466
0
  memset(req, 0, sizeof(*req));
467
468
0
  req->n.nlmsg_type =  RTM_NEWLINK;
469
0
  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
470
0
  req->n.nlmsg_flags = NLM_F_REQUEST;
471
472
0
  req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
473
474
0
  gre_info = dplane_ctx_gre_get_info(ctx);
475
0
  if (!gre_info)
476
0
    return 0;
477
478
0
  req->ifi.ifi_change = 0xFFFFFFFF;
479
0
  link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
480
0
  mtu = dplane_ctx_gre_get_mtu(ctx);
481
482
0
  if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
483
0
    return 0;
484
485
0
  rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
486
0
  if (!rta_info)
487
0
    return 0;
488
489
0
  if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
490
0
    return 0;
491
492
0
  rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
493
0
  if (!rta_data)
494
0
    return 0;
495
496
0
  if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
497
0
    return 0;
498
499
0
  if (gre_info->vtep_ip.s_addr &&
500
0
      !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
501
0
         gre_info->vtep_ip.s_addr))
502
0
    return 0;
503
504
0
  if (gre_info->vtep_ip_remote.s_addr &&
505
0
      !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
506
0
         gre_info->vtep_ip_remote.s_addr))
507
0
    return 0;
508
509
0
  if (gre_info->ikey &&
510
0
      !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
511
0
         gre_info->ikey))
512
0
    return 0;
513
0
  if (gre_info->okey &&
514
0
      !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
515
0
         gre_info->okey))
516
0
    return 0;
517
518
0
  nl_attr_nest_end(&req->n, rta_data);
519
0
  nl_attr_nest_end(&req->n, rta_info);
520
521
0
  return NLMSG_ALIGN(req->n.nlmsg_len);
522
0
}
523
524
static int netlink_extract_bridge_info(struct rtattr *link_data,
525
               struct zebra_l2info_bridge *bridge_info)
526
0
{
527
0
  struct rtattr *attr[IFLA_BR_MAX + 1];
528
529
0
  memset(bridge_info, 0, sizeof(*bridge_info));
530
0
  netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
531
0
  if (attr[IFLA_BR_VLAN_FILTERING])
532
0
    bridge_info->bridge.vlan_aware =
533
0
      *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
534
0
  return 0;
535
0
}
536
537
static int netlink_extract_vlan_info(struct rtattr *link_data,
538
             struct zebra_l2info_vlan *vlan_info)
539
0
{
540
0
  struct rtattr *attr[IFLA_VLAN_MAX + 1];
541
0
  vlanid_t vid_in_msg;
542
543
0
  memset(vlan_info, 0, sizeof(*vlan_info));
544
0
  netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
545
0
  if (!attr[IFLA_VLAN_ID]) {
546
0
    if (IS_ZEBRA_DEBUG_KERNEL)
547
0
      zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
548
0
    return -1;
549
0
  }
550
551
0
  vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
552
0
  vlan_info->vid = vid_in_msg;
553
0
  return 0;
554
0
}
555
556
static int netlink_extract_gre_info(struct rtattr *link_data,
557
            struct zebra_l2info_gre *gre_info)
558
0
{
559
0
  struct rtattr *attr[IFLA_GRE_MAX + 1];
560
561
0
  memset(gre_info, 0, sizeof(*gre_info));
562
0
  memset(attr, 0, sizeof(attr));
563
0
  netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
564
565
0
  if (!attr[IFLA_GRE_LOCAL]) {
566
0
    if (IS_ZEBRA_DEBUG_KERNEL)
567
0
      zlog_debug(
568
0
        "IFLA_GRE_LOCAL missing from GRE IF message");
569
0
  } else
570
0
    gre_info->vtep_ip =
571
0
      *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
572
0
  if (!attr[IFLA_GRE_REMOTE]) {
573
0
    if (IS_ZEBRA_DEBUG_KERNEL)
574
0
      zlog_debug(
575
0
        "IFLA_GRE_REMOTE missing from GRE IF message");
576
0
  } else
577
0
    gre_info->vtep_ip_remote =
578
0
      *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
579
580
0
  if (!attr[IFLA_GRE_LINK]) {
581
0
    if (IS_ZEBRA_DEBUG_KERNEL)
582
0
      zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
583
0
  } else {
584
0
    gre_info->ifindex_link =
585
0
      *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
586
0
    if (IS_ZEBRA_DEBUG_KERNEL)
587
0
      zlog_debug("IFLA_GRE_LINK obtained is %u",
588
0
           gre_info->ifindex_link);
589
0
  }
590
0
  if (attr[IFLA_GRE_IKEY])
591
0
    gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
592
0
  if (attr[IFLA_GRE_OKEY])
593
0
    gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
594
0
  return 0;
595
0
}
596
597
static int netlink_extract_vxlan_info(struct rtattr *link_data,
598
              struct zebra_l2info_vxlan *vxl_info)
599
0
{
600
0
  uint8_t svd = 0;
601
0
  struct rtattr *attr[IFLA_VXLAN_MAX + 1];
602
0
  vni_t vni_in_msg;
603
0
  struct in_addr vtep_ip_in_msg;
604
0
  ifindex_t ifindex_link;
605
606
0
  memset(vxl_info, 0, sizeof(*vxl_info));
607
0
  netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
608
0
  if (attr[IFLA_VXLAN_COLLECT_METADATA]) {
609
0
    svd = *(uint8_t *)RTA_DATA(attr[IFLA_VXLAN_COLLECT_METADATA]);
610
0
    if (IS_ZEBRA_DEBUG_KERNEL)
611
0
      zlog_debug(
612
0
        "IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
613
0
        svd);
614
0
  }
615
616
0
  if (!svd) {
617
    /*
618
     * In case of svd we will not get vni info directly from the
619
     * device
620
     */
621
0
    if (!attr[IFLA_VXLAN_ID]) {
622
0
      if (IS_ZEBRA_DEBUG_KERNEL)
623
0
        zlog_debug(
624
0
          "IFLA_VXLAN_ID missing from VXLAN IF message");
625
0
      return -1;
626
0
    }
627
628
0
    vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_VNI;
629
0
    vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
630
0
    vxl_info->vni_info.vni.vni = vni_in_msg;
631
0
  } else {
632
0
    vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_SVD;
633
0
  }
634
635
0
  if (!attr[IFLA_VXLAN_LOCAL]) {
636
0
    if (IS_ZEBRA_DEBUG_KERNEL)
637
0
      zlog_debug(
638
0
        "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
639
0
  } else {
640
0
    vtep_ip_in_msg =
641
0
      *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
642
0
    vxl_info->vtep_ip = vtep_ip_in_msg;
643
0
  }
644
645
0
  if (attr[IFLA_VXLAN_GROUP]) {
646
0
    if (!svd)
647
0
      vxl_info->vni_info.vni.mcast_grp =
648
0
        *(struct in_addr *)RTA_DATA(
649
0
          attr[IFLA_VXLAN_GROUP]);
650
0
  }
651
652
0
  if (!attr[IFLA_VXLAN_LINK]) {
653
0
    if (IS_ZEBRA_DEBUG_KERNEL)
654
0
      zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
655
0
  } else {
656
0
    ifindex_link =
657
0
      *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
658
0
    vxl_info->ifindex_link = ifindex_link;
659
0
  }
660
0
  return 0;
661
0
}
662
663
/*
664
 * Extract and save L2 params (of interest) for an interface. When a
665
 * bridge interface is added or updated, take further actions to map
666
 * its members. Likewise, for VxLAN interface.
667
 */
668
static void netlink_interface_update_l2info(struct interface *ifp,
669
              struct rtattr *link_data, int add,
670
              ns_id_t link_nsid)
671
0
{
672
0
  if (!link_data)
673
0
    return;
674
675
0
  if (IS_ZEBRA_IF_BRIDGE(ifp)) {
676
0
    struct zebra_l2info_bridge bridge_info;
677
678
0
    netlink_extract_bridge_info(link_data, &bridge_info);
679
0
    zebra_l2_bridge_add_update(ifp, &bridge_info, add);
680
0
  } else if (IS_ZEBRA_IF_VLAN(ifp)) {
681
0
    struct zebra_l2info_vlan vlan_info;
682
683
0
    netlink_extract_vlan_info(link_data, &vlan_info);
684
0
    zebra_l2_vlanif_update(ifp, &vlan_info);
685
0
    zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
686
0
            !!if_is_operative(ifp));
687
0
  } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
688
0
    struct zebra_l2info_vxlan vxlan_info;
689
690
0
    netlink_extract_vxlan_info(link_data, &vxlan_info);
691
0
    vxlan_info.link_nsid = link_nsid;
692
0
    zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
693
0
    if (link_nsid != NS_UNKNOWN &&
694
0
        vxlan_info.ifindex_link)
695
0
      zebra_if_update_link(ifp, vxlan_info.ifindex_link,
696
0
               link_nsid);
697
0
  } else if (IS_ZEBRA_IF_GRE(ifp)) {
698
0
    struct zebra_l2info_gre gre_info;
699
700
0
    netlink_extract_gre_info(link_data, &gre_info);
701
0
    gre_info.link_nsid = link_nsid;
702
0
    zebra_l2_greif_add_update(ifp, &gre_info, add);
703
0
    if (link_nsid != NS_UNKNOWN &&
704
0
        gre_info.ifindex_link)
705
0
      zebra_if_update_link(ifp, gre_info.ifindex_link,
706
0
               link_nsid);
707
0
  }
708
0
}
709
710
static int netlink_bridge_vxlan_vlan_vni_map_update(struct interface *ifp,
711
                struct rtattr *af_spec)
712
0
{
713
0
  int rem;
714
0
  vni_t vni_id;
715
0
  vlanid_t vid;
716
0
  uint16_t flags;
717
0
  struct rtattr *i;
718
0
  struct zebra_vxlan_vni vni;
719
0
  struct zebra_vxlan_vni *vnip;
720
0
  struct hash *vni_table = NULL;
721
0
  struct zebra_vxlan_vni vni_end;
722
0
  struct zebra_vxlan_vni vni_start;
723
0
  struct rtattr *aftb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
724
725
0
  memset(&vni_start, 0, sizeof(vni_start));
726
0
  memset(&vni_end, 0, sizeof(vni_end));
727
728
0
  for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); RTA_OK(i, rem);
729
0
       i = RTA_NEXT(i, rem)) {
730
731
0
    if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO)
732
0
      continue;
733
734
0
    memset(aftb, 0, sizeof(aftb));
735
0
    netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
736
0
              i);
737
0
    if (!aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
738
0
        !aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
739
      /* vlan-vni info missing */
740
0
      return 0;
741
742
0
    flags = 0;
743
0
    memset(&vni, 0, sizeof(vni));
744
745
0
    vni.vni = *(vni_t *)RTA_DATA(aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
746
0
    vni.access_vlan = *(vlanid_t *)RTA_DATA(
747
0
      aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
748
749
0
    if (aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
750
0
      flags = *(uint16_t *)RTA_DATA(
751
0
        aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
752
753
0
    if (flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
754
0
      vni_start = vni;
755
0
      continue;
756
0
    }
757
758
0
    if (flags & BRIDGE_VLAN_INFO_RANGE_END)
759
0
      vni_end = vni;
760
761
0
    if (!(flags & BRIDGE_VLAN_INFO_RANGE_END)) {
762
0
      vni_start = vni;
763
0
      vni_end = vni;
764
0
    }
765
766
0
    if (IS_ZEBRA_DEBUG_KERNEL)
767
0
      zlog_debug(
768
0
        "Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
769
0
        vni_start.access_vlan, vni_end.access_vlan,
770
0
        vni_start.vni, vni_end.vni, ifp->name,
771
0
        ifp->ifindex);
772
773
0
    if (!vni_table) {
774
0
      vni_table = zebra_vxlan_vni_table_create();
775
0
      if (!vni_table)
776
0
        return 0;
777
0
    }
778
779
0
    for (vid = vni_start.access_vlan, vni_id = vni_start.vni;
780
0
         vid <= vni_end.access_vlan; vid++, vni_id++) {
781
782
0
      memset(&vni, 0, sizeof(vni));
783
0
      vni.vni = vni_id;
784
0
      vni.access_vlan = vid;
785
0
      vnip = hash_get(vni_table, &vni, zebra_vxlan_vni_alloc);
786
0
      if (!vnip)
787
0
        return 0;
788
0
    }
789
790
0
    memset(&vni_start, 0, sizeof(vni_start));
791
0
    memset(&vni_end, 0, sizeof(vni_end));
792
0
  }
793
794
0
  if (vni_table)
795
0
    zebra_vxlan_if_vni_table_add_update(ifp, vni_table);
796
797
0
  return 0;
798
0
}
799
800
static int netlink_bridge_vxlan_update(struct interface *ifp,
801
    struct rtattr *af_spec)
802
0
{
803
0
  struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
804
0
  struct bridge_vlan_info *vinfo;
805
0
  struct zebra_if *zif;
806
0
  vlanid_t access_vlan;
807
808
0
  if (!af_spec)
809
0
    return 0;
810
811
0
  zif = (struct zebra_if *)ifp->info;
812
813
  /* Single vxlan devices has vni-vlan range to update */
814
0
  if (IS_ZEBRA_VXLAN_IF_SVD(zif))
815
0
    return netlink_bridge_vxlan_vlan_vni_map_update(ifp, af_spec);
816
817
  /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
818
   * only 1 access VLAN is accepted.
819
   */
820
0
  netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
821
0
  if (!aftb[IFLA_BRIDGE_VLAN_INFO])
822
0
    return 0;
823
824
0
  vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
825
0
  if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
826
0
    return 0;
827
828
0
  access_vlan = (vlanid_t)vinfo->vid;
829
0
  if (IS_ZEBRA_DEBUG_KERNEL)
830
0
    zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
831
0
        ifp->name, ifp->ifindex);
832
0
  zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
833
0
  return 0;
834
0
}
835
836
static void netlink_bridge_vlan_update(struct interface *ifp,
837
    struct rtattr *af_spec)
838
0
{
839
0
  struct rtattr *i;
840
0
  int rem;
841
0
  uint16_t vid_range_start = 0;
842
0
  struct zebra_if *zif;
843
0
  bitfield_t old_vlan_bitmap;
844
0
  struct bridge_vlan_info *vinfo;
845
846
0
  zif = (struct zebra_if *)ifp->info;
847
848
  /* cache the old bitmap addrs */
849
0
  old_vlan_bitmap = zif->vlan_bitmap;
850
  /* create a new bitmap space for re-eval */
851
0
  bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
852
853
0
  if (af_spec) {
854
0
    for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
855
0
         RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
856
857
0
      if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
858
0
        continue;
859
860
0
      vinfo = RTA_DATA(i);
861
862
0
      if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
863
0
        vid_range_start = vinfo->vid;
864
0
        continue;
865
0
      }
866
867
0
      if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
868
0
        vid_range_start = vinfo->vid;
869
870
0
      zebra_vlan_bitmap_compute(ifp, vid_range_start,
871
0
              vinfo->vid);
872
0
    }
873
0
  }
874
875
0
  zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
876
877
0
  bf_free(old_vlan_bitmap);
878
0
}
879
880
static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
881
            int startup)
882
0
{
883
0
  char *name = NULL;
884
0
  struct ifinfomsg *ifi;
885
0
  struct rtattr *tb[IFLA_MAX + 1];
886
0
  struct interface *ifp;
887
0
  struct zebra_if *zif;
888
0
  struct rtattr *af_spec;
889
890
  /* Fetch name and ifindex */
891
0
  ifi = NLMSG_DATA(h);
892
0
  netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
893
894
0
  if (tb[IFLA_IFNAME] == NULL)
895
0
    return -1;
896
0
  name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
897
898
  /* The interface should already be known, if not discard. */
899
0
  ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
900
0
  if (!ifp) {
901
0
    zlog_debug("Cannot find bridge IF %s(%u)", name,
902
0
         ifi->ifi_index);
903
0
    return 0;
904
0
  }
905
906
  /* We are only interested in the access VLAN i.e., AF_SPEC */
907
0
  af_spec = tb[IFLA_AF_SPEC];
908
909
0
  if (IS_ZEBRA_IF_VXLAN(ifp))
910
0
    return netlink_bridge_vxlan_update(ifp, af_spec);
911
912
  /* build vlan bitmap associated with this interface if that
913
   * device type is interested in the vlans
914
   */
915
0
  zif = (struct zebra_if *)ifp->info;
916
0
  if (bf_is_inited(zif->vlan_bitmap))
917
0
    netlink_bridge_vlan_update(ifp, af_spec);
918
919
0
  return 0;
920
0
}
921
922
static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
923
0
{
924
  /* This shouldn't be possible */
925
0
  assert(frr_protodown_r_bit < 32);
926
0
  return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
927
0
}
928
929
/*
930
 * Process interface protodown dplane update.
931
 *
932
 * If the interface is an es bond member then it must follow EVPN's
933
 * protodown setting.
934
 */
935
static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
936
               struct rtattr **tb)
937
0
{
938
0
  bool protodown;
939
0
  bool old_protodown;
940
0
  uint32_t rc_bitfield = 0;
941
0
  struct rtattr *pd_reason_info[IFLA_MAX + 1];
942
943
0
  protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
944
945
0
  if (tb[IFLA_PROTO_DOWN_REASON]) {
946
0
    netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
947
0
              tb[IFLA_PROTO_DOWN_REASON]);
948
949
0
    if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
950
0
      rc_bitfield = *(uint32_t *)RTA_DATA(
951
0
        pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
952
0
  }
953
954
  /*
955
   * Set our reason code to note it wasn't us.
956
   * If the reason we got from the kernel is ONLY frr though, don't
957
   * set it.
958
   */
959
0
  COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
960
0
      protodown && rc_bitfield &&
961
0
        !is_if_protodown_reason_only_frr(rc_bitfield));
962
963
964
0
  old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
965
0
  if (protodown == old_protodown)
966
0
    return;
967
968
0
  if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
969
0
    zlog_debug("interface %s dplane change, protdown %s",
970
0
         zif->ifp->name, protodown ? "on" : "off");
971
972
  /* Set protodown, respectively */
973
0
  COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);
974
975
0
  if (zebra_evpn_is_es_bond_member(zif->ifp)) {
976
    /* Check it's not already being sent to the dplane first */
977
0
    if (protodown &&
978
0
        CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
979
0
      if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
980
0
        zlog_debug(
981
0
          "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
982
0
          zif->ifp->name);
983
0
      return;
984
0
    }
985
986
0
    if (!protodown &&
987
0
        CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
988
0
      if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
989
0
        zlog_debug(
990
0
          "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
991
0
          zif->ifp->name);
992
0
      return;
993
0
    }
994
995
0
    if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
996
0
      zlog_debug(
997
0
        "bond mbr %s reinstate protodown %s in the dplane",
998
0
        zif->ifp->name, old_protodown ? "on" : "off");
999
1000
0
    if (old_protodown)
1001
0
      SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
1002
0
    else
1003
0
      SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
1004
1005
0
    dplane_intf_update(zif->ifp);
1006
0
  }
1007
0
}
1008
1009
static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
1010
0
{
1011
0
  uint8_t bypass = 0;
1012
0
  struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
1013
1014
0
  netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
1015
0
            linkinfo[IFLA_INFO_SLAVE_DATA]);
1016
0
  if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
1017
0
    bypass = *(uint8_t *)RTA_DATA(
1018
0
      mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
1019
1020
0
  return bypass;
1021
0
}
1022
1023
/*
1024
 * Only called at startup to cleanup leftover protodown reasons we may
1025
 * have not cleaned up. We leave protodown set though.
1026
 */
1027
static void if_sweep_protodown(struct zebra_if *zif)
1028
0
{
1029
0
  bool protodown;
1030
1031
0
  protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
1032
1033
0
  if (!protodown)
1034
0
    return;
1035
1036
0
  if (IS_ZEBRA_DEBUG_KERNEL)
1037
0
    zlog_debug("interface %s sweeping protodown %s reason 0x%x",
1038
0
         zif->ifp->name, protodown ? "on" : "off",
1039
0
         zif->protodown_rc);
1040
1041
  /* Only clear our reason codes, leave external if it was set */
1042
0
  UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
1043
0
  dplane_intf_update(zif->ifp);
1044
0
}
1045
1046
/*
1047
 * Called from interface_lookup_netlink().  This function is only used
1048
 * during bootstrap.
1049
 */
1050
static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1051
0
{
1052
0
  int len;
1053
0
  struct ifinfomsg *ifi;
1054
0
  struct rtattr *tb[IFLA_MAX + 1];
1055
0
  struct rtattr *linkinfo[IFLA_MAX + 1];
1056
0
  struct interface *ifp;
1057
0
  char *name = NULL;
1058
0
  char *kind = NULL;
1059
0
  char *desc = NULL;
1060
0
  char *slave_kind = NULL;
1061
0
  struct zebra_ns *zns = NULL;
1062
0
  vrf_id_t vrf_id = VRF_DEFAULT;
1063
0
  enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1064
0
  enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1065
0
  ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1066
0
  ifindex_t link_ifindex = IFINDEX_INTERNAL;
1067
0
  ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1068
0
  struct zebra_if *zif;
1069
0
  ns_id_t link_nsid = ns_id;
1070
0
  uint8_t bypass = 0;
1071
1072
0
  frrtrace(3, frr_zebra, netlink_interface, h, ns_id, startup);
1073
1074
0
  zns = zebra_ns_lookup(ns_id);
1075
0
  ifi = NLMSG_DATA(h);
1076
1077
0
  if (h->nlmsg_type != RTM_NEWLINK)
1078
0
    return 0;
1079
1080
0
  len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1081
0
  if (len < 0) {
1082
0
    zlog_err(
1083
0
      "%s: Message received from netlink is of a broken size: %d %zu",
1084
0
      __func__, h->nlmsg_len,
1085
0
      (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
1086
0
    return -1;
1087
0
  }
1088
1089
  /* We are interested in some AF_BRIDGE notifications. */
1090
0
  if (ifi->ifi_family == AF_BRIDGE)
1091
0
    return netlink_bridge_interface(h, len, ns_id, startup);
1092
1093
  /* Looking up interface name. */
1094
0
  memset(linkinfo, 0, sizeof(linkinfo));
1095
0
  netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
1096
0
           NLA_F_NESTED);
1097
1098
  /* check for wireless messages to ignore */
1099
0
  if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1100
0
    if (IS_ZEBRA_DEBUG_KERNEL)
1101
0
      zlog_debug("%s: ignoring IFLA_WIRELESS message",
1102
0
           __func__);
1103
0
    return 0;
1104
0
  }
1105
1106
0
  if (tb[IFLA_IFNAME] == NULL)
1107
0
    return -1;
1108
0
  name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1109
1110
0
  if (tb[IFLA_IFALIAS])
1111
0
    desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1112
1113
0
  if (tb[IFLA_LINKINFO]) {
1114
0
    netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1115
0
              tb[IFLA_LINKINFO]);
1116
1117
0
    if (linkinfo[IFLA_INFO_KIND])
1118
0
      kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1119
1120
0
    if (linkinfo[IFLA_INFO_SLAVE_KIND])
1121
0
      slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1122
1123
0
    if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
1124
0
      netlink_determine_zebra_iftype("bond_slave", &zif_type);
1125
0
    else
1126
0
      netlink_determine_zebra_iftype(kind, &zif_type);
1127
0
  }
1128
1129
  /* If VRF, create the VRF structure itself. */
1130
0
  if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1131
0
    netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1132
0
    vrf_id = (vrf_id_t)ifi->ifi_index;
1133
0
  }
1134
1135
0
  if (tb[IFLA_MASTER]) {
1136
0
    if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1137
0
        && !vrf_is_backend_netns()) {
1138
0
      zif_slave_type = ZEBRA_IF_SLAVE_VRF;
1139
0
      vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
1140
0
    } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
1141
0
      zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1142
0
      bridge_ifindex =
1143
0
        *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1144
0
    } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
1145
0
      zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1146
0
      bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1147
0
      bypass = netlink_parse_lacp_bypass(linkinfo);
1148
0
    } else
1149
0
      zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1150
0
  }
1151
0
  if (vrf_is_backend_netns())
1152
0
    vrf_id = (vrf_id_t)ns_id;
1153
1154
  /* If linking to another interface, note it. */
1155
0
  if (tb[IFLA_LINK])
1156
0
    link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1157
1158
0
  if (tb[IFLA_LINK_NETNSID]) {
1159
0
    link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
1160
0
    link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1161
0
  }
1162
1163
0
  ifp = if_get_by_name(name, vrf_id, NULL);
1164
0
  set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
1165
1166
0
  ifp->flags = ifi->ifi_flags & 0x0000fffff;
1167
0
  ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
1168
0
  ifp->metric = 0;
1169
0
  ifp->speed = get_iflink_speed(ifp, NULL);
1170
0
  ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1171
1172
  /* Set zebra interface type */
1173
0
  zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1174
0
  if (IS_ZEBRA_IF_VRF(ifp))
1175
0
    SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1176
1177
  /*
1178
   * Just set the @link/lower-device ifindex. During nldump interfaces are
1179
   * not ordered in any fashion so we may end up getting upper devices
1180
   * before lower devices. We will setup the real linkage once the dump
1181
   * is complete.
1182
   */
1183
0
  zif = (struct zebra_if *)ifp->info;
1184
0
  zif->link_ifindex = link_ifindex;
1185
1186
0
  if (desc) {
1187
0
    XFREE(MTYPE_ZIF_DESC, zif->desc);
1188
0
    zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
1189
0
  }
1190
1191
  /* Hardware type and address. */
1192
0
  ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
1193
1194
0
  netlink_interface_update_hw_addr(tb, ifp);
1195
1196
0
  if_add_update(ifp);
1197
1198
  /* Extract and save L2 interface information, take additional actions.
1199
   */
1200
0
  netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1201
0
          1, link_nsid);
1202
0
  if (IS_ZEBRA_IF_BOND(ifp))
1203
0
    zebra_l2if_update_bond(ifp, true);
1204
0
  if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1205
0
    zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1206
0
                 ZEBRA_BRIDGE_NO_ACTION);
1207
0
  else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1208
0
    zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
1209
1210
0
  if (tb[IFLA_PROTO_DOWN]) {
1211
0
    netlink_proc_dplane_if_protodown(zif, tb);
1212
0
    if_sweep_protodown(zif);
1213
0
  }
1214
1215
0
  return 0;
1216
0
}
1217
1218
/* Request for specific interface or address information from the kernel */
1219
static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1220
             int type, uint32_t filter_mask)
1221
2
{
1222
2
  struct {
1223
2
    struct nlmsghdr n;
1224
2
    struct ifinfomsg ifm;
1225
2
    char buf[256];
1226
2
  } req;
1227
1228
2
  frrtrace(4, frr_zebra, netlink_request_intf_addr, netlink_cmd, family,
1229
2
     type, filter_mask);
1230
1231
  /* Form the request, specifying filter (rtattr) if needed. */
1232
2
  memset(&req, 0, sizeof(req));
1233
2
  req.n.nlmsg_type = type;
1234
2
  req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
1235
2
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1236
2
  req.ifm.ifi_family = family;
1237
1238
  /* Include filter, if specified. */
1239
2
  if (filter_mask)
1240
0
    nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
1241
1242
2
  return netlink_request(netlink_cmd, &req);
1243
2
}
1244
1245
enum netlink_msg_status
1246
netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1247
0
{
1248
0
  enum dplane_op_e op;
1249
0
  enum netlink_msg_status ret;
1250
1251
0
  op = dplane_ctx_get_op(ctx);
1252
0
  assert(op == DPLANE_OP_GRE_SET);
1253
1254
0
  ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1255
1256
0
  return ret;
1257
0
}
1258
1259
/* Interface lookup by netlink socket. */
1260
int interface_lookup_netlink(struct zebra_ns *zns)
1261
1
{
1262
1
  int ret;
1263
1
  struct zebra_dplane_info dp_info;
1264
1
  struct nlsock *netlink_cmd = &zns->netlink_cmd;
1265
1266
  /* Capture key info from ns struct */
1267
1
  zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1268
1269
  /* Get interface information. */
1270
1
  ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
1271
1
  if (ret < 0)
1272
1
    return ret;
1273
0
  ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1274
0
         true);
1275
0
  if (ret < 0)
1276
0
    return ret;
1277
1278
  /* Get interface information - for bridge interfaces. */
1279
0
  ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
1280
0
          RTEXT_FILTER_BRVLAN);
1281
0
  if (ret < 0)
1282
0
    return ret;
1283
0
  ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1284
0
         true);
1285
0
  if (ret < 0)
1286
0
    return ret;
1287
1288
  /*
1289
   * So netlink_tunneldump_read will initiate a request
1290
   * per tunnel to get data.  If we are on a kernel that
1291
   * does not support this then we will get X error messages
1292
   * (one per tunnel request )back which netlink_parse_info will
1293
   * stop after the first one.  So we need to read equivalent
1294
   * error messages per tunnel then we can continue.
1295
   * if we do not gather all the read failures then
1296
   * later requests will not work right.
1297
   */
1298
0
  ret = netlink_tunneldump_read(zns);
1299
0
  if (ret < 0)
1300
0
    return ret;
1301
1302
  /* fixup linkages */
1303
0
  zebra_if_update_all_links(zns);
1304
0
  return 0;
1305
0
}
1306
1307
/**
1308
 * interface_addr_lookup_netlink() - Look up interface addresses
1309
 *
1310
 * @zns:  Zebra netlink socket
1311
 * Return:  Result status
1312
 */
1313
static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1314
1
{
1315
1
  int ret;
1316
1
  struct zebra_dplane_info dp_info;
1317
1
  struct nlsock *netlink_cmd = &zns->netlink_cmd;
1318
1319
  /* Capture key info from ns struct */
1320
1
  zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1321
1322
  /* Get IPv4 address of the interfaces. */
1323
1
  ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
1324
1
  if (ret < 0)
1325
1
    return ret;
1326
0
  ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1327
0
         0, true);
1328
0
  if (ret < 0)
1329
0
    return ret;
1330
1331
  /* Get IPv6 address of the interfaces. */
1332
0
  ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
1333
0
  if (ret < 0)
1334
0
    return ret;
1335
0
  ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1336
0
         0, true);
1337
0
  if (ret < 0)
1338
0
    return ret;
1339
1340
0
  return 0;
1341
0
}
1342
1343
int kernel_interface_set_master(struct interface *master,
1344
        struct interface *slave)
1345
0
{
1346
0
  struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1347
1348
0
  struct {
1349
0
    struct nlmsghdr n;
1350
0
    struct ifinfomsg ifa;
1351
0
    char buf[NL_PKT_BUF_SIZE];
1352
0
  } req;
1353
1354
0
  memset(&req, 0, sizeof(req));
1355
1356
0
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1357
0
  req.n.nlmsg_flags = NLM_F_REQUEST;
1358
0
  req.n.nlmsg_type = RTM_SETLINK;
1359
0
  req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1360
1361
0
  req.ifa.ifi_index = slave->ifindex;
1362
1363
0
  nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1364
0
  nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
1365
1366
0
  return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1367
0
          false);
1368
0
}
1369
1370
/* Interface address modification. */
1371
static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1372
             void *buf, size_t buflen)
1373
0
{
1374
0
  int bytelen;
1375
0
  const struct prefix *p;
1376
0
  int cmd;
1377
0
  const char *label;
1378
1379
0
  struct {
1380
0
    struct nlmsghdr n;
1381
0
    struct ifaddrmsg ifa;
1382
0
    char buf[0];
1383
0
  } *req = buf;
1384
1385
0
  if (buflen < sizeof(*req))
1386
0
    return 0;
1387
1388
0
  p = dplane_ctx_get_intf_addr(ctx);
1389
0
  memset(req, 0, sizeof(*req));
1390
1391
0
  bytelen = (p->family == AF_INET ? 4 : 16);
1392
1393
0
  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1394
0
  req->n.nlmsg_flags = NLM_F_REQUEST;
1395
1396
0
  if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1397
0
    cmd = RTM_NEWADDR;
1398
0
  else
1399
0
    cmd = RTM_DELADDR;
1400
1401
0
  req->n.nlmsg_type = cmd;
1402
0
  req->ifa.ifa_family = p->family;
1403
1404
0
  req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1405
1406
0
  if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1407
0
    return 0;
1408
1409
0
  if (p->family == AF_INET) {
1410
0
    if (dplane_ctx_intf_is_connected(ctx)) {
1411
0
      p = dplane_ctx_get_intf_dest(ctx);
1412
0
      if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1413
0
           &p->u.prefix, bytelen))
1414
0
        return 0;
1415
0
    } else if (cmd == RTM_NEWADDR) {
1416
0
      struct in_addr broad = {
1417
0
        .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1418
0
              p->prefixlen)
1419
0
      };
1420
0
      if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1421
0
           bytelen))
1422
0
        return 0;
1423
0
    }
1424
0
  }
1425
1426
  /* p is now either address or destination/bcast addr */
1427
0
  req->ifa.ifa_prefixlen = p->prefixlen;
1428
1429
0
  if (dplane_ctx_intf_is_secondary(ctx))
1430
0
    SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1431
1432
0
  if (dplane_ctx_intf_has_label(ctx)) {
1433
0
    label = dplane_ctx_get_intf_label(ctx);
1434
0
    if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1435
0
         strlen(label) + 1))
1436
0
      return 0;
1437
0
  }
1438
1439
0
  return NLMSG_ALIGN(req->n.nlmsg_len);
1440
0
}
1441
1442
enum netlink_msg_status
1443
netlink_put_address_update_msg(struct nl_batch *bth,
1444
             struct zebra_dplane_ctx *ctx)
1445
0
{
1446
0
  return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1447
0
             false);
1448
0
}
1449
1450
static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1451
          size_t buflen)
1452
0
{
1453
0
  enum dplane_op_e op;
1454
0
  int cmd = 0;
1455
1456
0
  op = dplane_ctx_get_op(ctx);
1457
1458
0
  switch (op) {
1459
0
  case DPLANE_OP_INTF_UPDATE:
1460
0
    cmd = RTM_SETLINK;
1461
0
    break;
1462
0
  case DPLANE_OP_INTF_INSTALL:
1463
0
    cmd = RTM_NEWLINK;
1464
0
    break;
1465
0
  case DPLANE_OP_INTF_DELETE:
1466
0
    cmd = RTM_DELLINK;
1467
0
    break;
1468
0
  case DPLANE_OP_NONE:
1469
0
  case DPLANE_OP_ROUTE_INSTALL:
1470
0
  case DPLANE_OP_ROUTE_UPDATE:
1471
0
  case DPLANE_OP_ROUTE_DELETE:
1472
0
  case DPLANE_OP_ROUTE_NOTIFY:
1473
0
  case DPLANE_OP_NH_INSTALL:
1474
0
  case DPLANE_OP_NH_UPDATE:
1475
0
  case DPLANE_OP_NH_DELETE:
1476
0
  case DPLANE_OP_LSP_INSTALL:
1477
0
  case DPLANE_OP_LSP_DELETE:
1478
0
  case DPLANE_OP_LSP_NOTIFY:
1479
0
  case DPLANE_OP_LSP_UPDATE:
1480
0
  case DPLANE_OP_PW_INSTALL:
1481
0
  case DPLANE_OP_PW_UNINSTALL:
1482
0
  case DPLANE_OP_SYS_ROUTE_ADD:
1483
0
  case DPLANE_OP_SYS_ROUTE_DELETE:
1484
0
  case DPLANE_OP_ADDR_INSTALL:
1485
0
  case DPLANE_OP_ADDR_UNINSTALL:
1486
0
  case DPLANE_OP_MAC_INSTALL:
1487
0
  case DPLANE_OP_MAC_DELETE:
1488
0
  case DPLANE_OP_NEIGH_INSTALL:
1489
0
  case DPLANE_OP_NEIGH_UPDATE:
1490
0
  case DPLANE_OP_NEIGH_DELETE:
1491
0
  case DPLANE_OP_NEIGH_DISCOVER:
1492
0
  case DPLANE_OP_VTEP_ADD:
1493
0
  case DPLANE_OP_VTEP_DELETE:
1494
0
  case DPLANE_OP_RULE_ADD:
1495
0
  case DPLANE_OP_RULE_DELETE:
1496
0
  case DPLANE_OP_RULE_UPDATE:
1497
0
  case DPLANE_OP_BR_PORT_UPDATE:
1498
0
  case DPLANE_OP_IPTABLE_ADD:
1499
0
  case DPLANE_OP_IPTABLE_DELETE:
1500
0
  case DPLANE_OP_IPSET_ADD:
1501
0
  case DPLANE_OP_IPSET_ENTRY_ADD:
1502
0
  case DPLANE_OP_IPSET_ENTRY_DELETE:
1503
0
  case DPLANE_OP_IPSET_DELETE:
1504
0
  case DPLANE_OP_NEIGH_IP_INSTALL:
1505
0
  case DPLANE_OP_NEIGH_IP_DELETE:
1506
0
  case DPLANE_OP_NEIGH_TABLE_UPDATE:
1507
0
  case DPLANE_OP_GRE_SET:
1508
0
  case DPLANE_OP_INTF_ADDR_ADD:
1509
0
  case DPLANE_OP_INTF_ADDR_DEL:
1510
0
  case DPLANE_OP_INTF_NETCONFIG:
1511
0
  case DPLANE_OP_TC_QDISC_INSTALL:
1512
0
  case DPLANE_OP_TC_QDISC_UNINSTALL:
1513
0
  case DPLANE_OP_TC_CLASS_ADD:
1514
0
  case DPLANE_OP_TC_CLASS_DELETE:
1515
0
  case DPLANE_OP_TC_CLASS_UPDATE:
1516
0
  case DPLANE_OP_TC_FILTER_ADD:
1517
0
  case DPLANE_OP_TC_FILTER_DELETE:
1518
0
  case DPLANE_OP_TC_FILTER_UPDATE:
1519
0
    flog_err(
1520
0
      EC_ZEBRA_NHG_FIB_UPDATE,
1521
0
      "Context received for kernel interface update with incorrect OP code (%u)",
1522
0
      op);
1523
0
    return -1;
1524
0
  }
1525
1526
0
  return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1527
0
}
1528
1529
enum netlink_msg_status
1530
netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1531
0
{
1532
0
  return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1533
0
}
1534
1535
int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1536
0
{
1537
0
  int len;
1538
0
  struct ifaddrmsg *ifa;
1539
0
  struct rtattr *tb[IFA_MAX + 1];
1540
0
  struct interface *ifp;
1541
0
  void *addr;
1542
0
  void *broad;
1543
0
  uint8_t flags = 0;
1544
0
  char *label = NULL;
1545
0
  struct zebra_ns *zns;
1546
0
  uint32_t metric = METRIC_MAX;
1547
0
  uint32_t kernel_flags = 0;
1548
1549
0
  frrtrace(3, frr_zebra, netlink_interface_addr, h, ns_id, startup);
1550
1551
0
  zns = zebra_ns_lookup(ns_id);
1552
0
  ifa = NLMSG_DATA(h);
1553
1554
0
  if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1555
0
    flog_warn(
1556
0
      EC_ZEBRA_UNKNOWN_FAMILY,
1557
0
      "Invalid address family: %u received from kernel interface addr change: %s",
1558
0
      ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
1559
0
    return 0;
1560
0
  }
1561
1562
0
  if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1563
0
    return 0;
1564
1565
0
  len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1566
0
  if (len < 0) {
1567
0
    zlog_err(
1568
0
      "%s: Message received from netlink is of a broken size: %d %zu",
1569
0
      __func__, h->nlmsg_len,
1570
0
      (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
1571
0
    return -1;
1572
0
  }
1573
1574
0
  netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1575
1576
0
  ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1577
0
  if (ifp == NULL) {
1578
0
    if (startup) {
1579
      /* During startup, failure to lookup the referenced
1580
       * interface should not be an error, so we have
1581
       * downgraded this condition to warning, and we permit
1582
       * the startup interface state retrieval to continue.
1583
       */
1584
0
      flog_warn(EC_LIB_INTERFACE,
1585
0
          "%s: can't find interface by index %d",
1586
0
          __func__, ifa->ifa_index);
1587
0
      return 0;
1588
0
    } else {
1589
0
      flog_err(EC_LIB_INTERFACE,
1590
0
         "%s: can't find interface by index %d",
1591
0
         __func__, ifa->ifa_index);
1592
0
      return -1;
1593
0
    }
1594
0
  }
1595
1596
  /* Flags passed through */
1597
0
  if (tb[IFA_FLAGS])
1598
0
    kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1599
0
  else
1600
0
    kernel_flags = ifa->ifa_flags;
1601
1602
0
  if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1603
0
  {
1604
0
    char buf[BUFSIZ];
1605
0
    zlog_debug("%s %s %s flags 0x%x:", __func__,
1606
0
         nl_msg_type_to_str(h->nlmsg_type), ifp->name,
1607
0
         kernel_flags);
1608
0
    if (tb[IFA_LOCAL])
1609
0
      zlog_debug("  IFA_LOCAL     %s/%d",
1610
0
           inet_ntop(ifa->ifa_family,
1611
0
               RTA_DATA(tb[IFA_LOCAL]), buf,
1612
0
               BUFSIZ),
1613
0
           ifa->ifa_prefixlen);
1614
0
    if (tb[IFA_ADDRESS])
1615
0
      zlog_debug("  IFA_ADDRESS   %s/%d",
1616
0
           inet_ntop(ifa->ifa_family,
1617
0
               RTA_DATA(tb[IFA_ADDRESS]), buf,
1618
0
               BUFSIZ),
1619
0
           ifa->ifa_prefixlen);
1620
0
    if (tb[IFA_BROADCAST])
1621
0
      zlog_debug("  IFA_BROADCAST %s/%d",
1622
0
           inet_ntop(ifa->ifa_family,
1623
0
               RTA_DATA(tb[IFA_BROADCAST]), buf,
1624
0
               BUFSIZ),
1625
0
           ifa->ifa_prefixlen);
1626
0
    if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1627
0
      zlog_debug("  IFA_LABEL     %s",
1628
0
           (char *)RTA_DATA(tb[IFA_LABEL]));
1629
1630
0
    if (tb[IFA_CACHEINFO]) {
1631
0
      struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1632
0
      zlog_debug("  IFA_CACHEINFO pref %d, valid %d",
1633
0
           ci->ifa_prefered, ci->ifa_valid);
1634
0
    }
1635
0
  }
1636
1637
  /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1638
0
  if (tb[IFA_LOCAL] == NULL)
1639
0
    tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1640
0
  if (tb[IFA_ADDRESS] == NULL)
1641
0
    tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1642
1643
  /* local interface address */
1644
0
  addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1645
1646
  /* is there a peer address? */
1647
0
  if (tb[IFA_ADDRESS]
1648
0
      && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1649
0
          RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1650
0
    broad = RTA_DATA(tb[IFA_ADDRESS]);
1651
0
    SET_FLAG(flags, ZEBRA_IFA_PEER);
1652
0
  } else
1653
    /* seeking a broadcast address */
1654
0
    broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1655
0
             : NULL);
1656
1657
  /* addr is primary key, SOL if we don't have one */
1658
0
  if (addr == NULL) {
1659
0
    zlog_debug("%s: Local Interface Address is NULL for %s",
1660
0
         __func__, ifp->name);
1661
0
    return -1;
1662
0
  }
1663
1664
  /* Flags. */
1665
0
  if (kernel_flags & IFA_F_SECONDARY)
1666
0
    SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1667
1668
  /* Label */
1669
0
  if (tb[IFA_LABEL])
1670
0
    label = (char *)RTA_DATA(tb[IFA_LABEL]);
1671
1672
0
  if (label && strcmp(ifp->name, label) == 0)
1673
0
    label = NULL;
1674
1675
0
  if (tb[IFA_RT_PRIORITY])
1676
0
    metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1677
1678
  /* Register interface address to the interface. */
1679
0
  if (ifa->ifa_family == AF_INET) {
1680
0
    if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1681
0
      zlog_err(
1682
0
        "Invalid prefix length: %u received from kernel interface addr change: %s",
1683
0
        ifa->ifa_prefixlen,
1684
0
        nl_msg_type_to_str(h->nlmsg_type));
1685
0
      return -1;
1686
0
    }
1687
1688
0
    if (h->nlmsg_type == RTM_NEWADDR)
1689
0
      connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1690
0
             ifa->ifa_prefixlen,
1691
0
             (struct in_addr *)broad, label,
1692
0
             metric);
1693
0
    else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1694
      /* Delete with a peer address */
1695
0
      connected_delete_ipv4(
1696
0
        ifp, flags, (struct in_addr *)addr,
1697
0
        ifa->ifa_prefixlen, broad);
1698
0
    } else
1699
0
      connected_delete_ipv4(
1700
0
        ifp, flags, (struct in_addr *)addr,
1701
0
        ifa->ifa_prefixlen, NULL);
1702
0
  }
1703
1704
0
  if (ifa->ifa_family == AF_INET6) {
1705
0
    if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1706
0
      zlog_err(
1707
0
        "Invalid prefix length: %u received from kernel interface addr change: %s",
1708
0
        ifa->ifa_prefixlen,
1709
0
        nl_msg_type_to_str(h->nlmsg_type));
1710
0
      return -1;
1711
0
    }
1712
0
    if (h->nlmsg_type == RTM_NEWADDR) {
1713
      /* Only consider valid addresses; we'll not get a
1714
       * notification from
1715
       * the kernel till IPv6 DAD has completed, but at init
1716
       * time, Quagga
1717
       * does query for and will receive all addresses.
1718
       */
1719
0
      if (!(kernel_flags
1720
0
            & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
1721
0
        connected_add_ipv6(ifp, flags,
1722
0
               (struct in6_addr *)addr,
1723
0
               (struct in6_addr *)broad,
1724
0
               ifa->ifa_prefixlen, label,
1725
0
               metric);
1726
0
    } else
1727
0
      connected_delete_ipv6(ifp, (struct in6_addr *)addr,
1728
0
                NULL, ifa->ifa_prefixlen);
1729
0
  }
1730
1731
  /*
1732
   * Linux kernel does not send route delete on interface down/addr del
1733
   * so we have to re-process routes it owns (i.e. kernel routes)
1734
   */
1735
0
  if (h->nlmsg_type != RTM_NEWADDR)
1736
0
    rib_update(RIB_UPDATE_KERNEL);
1737
1738
0
  return 0;
1739
0
}
1740
1741
/*
1742
 * Parse and validate an incoming interface address change message,
1743
 * generating a dplane context object.
1744
 * This runs in the dplane pthread; the context is enqueued to the
1745
 * main pthread for processing.
1746
 */
1747
int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1748
          int startup /*ignored*/)
1749
0
{
1750
0
  int len;
1751
0
  struct ifaddrmsg *ifa;
1752
0
  struct rtattr *tb[IFA_MAX + 1];
1753
0
  void *addr;
1754
0
  void *broad;
1755
0
  char *label = NULL;
1756
0
  uint32_t metric = METRIC_MAX;
1757
0
  uint32_t kernel_flags = 0;
1758
0
  struct zebra_dplane_ctx *ctx;
1759
0
  struct prefix p;
1760
1761
0
  ifa = NLMSG_DATA(h);
1762
1763
  /* Validate message types */
1764
0
  if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1765
0
    return 0;
1766
1767
0
  if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1768
0
    if (IS_ZEBRA_DEBUG_KERNEL)
1769
0
      zlog_debug("%s: %s: Invalid address family: %u",
1770
0
           __func__, nl_msg_type_to_str(h->nlmsg_type),
1771
0
           ifa->ifa_family);
1772
0
    return 0;
1773
0
  }
1774
1775
0
  len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1776
0
  if (len < 0) {
1777
0
    if (IS_ZEBRA_DEBUG_KERNEL)
1778
0
      zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1779
0
           __func__, nl_msg_type_to_str(h->nlmsg_type),
1780
0
           h->nlmsg_len,
1781
0
           (size_t)NLMSG_LENGTH(
1782
0
             sizeof(struct ifaddrmsg)));
1783
0
    return -1;
1784
0
  }
1785
1786
0
  netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1787
1788
  /* Flags passed through */
1789
0
  if (tb[IFA_FLAGS])
1790
0
    kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1791
0
  else
1792
0
    kernel_flags = ifa->ifa_flags;
1793
1794
0
  if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1795
0
    char buf[PREFIX_STRLEN];
1796
1797
0
    zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1798
0
         nl_msg_type_to_str(h->nlmsg_type), ns_id,
1799
0
         ifa->ifa_index, kernel_flags);
1800
0
    if (tb[IFA_LOCAL])
1801
0
      zlog_debug("  IFA_LOCAL     %s/%d",
1802
0
           inet_ntop(ifa->ifa_family,
1803
0
               RTA_DATA(tb[IFA_LOCAL]), buf,
1804
0
               sizeof(buf)),
1805
0
           ifa->ifa_prefixlen);
1806
0
    if (tb[IFA_ADDRESS])
1807
0
      zlog_debug("  IFA_ADDRESS   %s/%d",
1808
0
           inet_ntop(ifa->ifa_family,
1809
0
               RTA_DATA(tb[IFA_ADDRESS]), buf,
1810
0
               sizeof(buf)),
1811
0
           ifa->ifa_prefixlen);
1812
0
    if (tb[IFA_BROADCAST])
1813
0
      zlog_debug("  IFA_BROADCAST %s/%d",
1814
0
           inet_ntop(ifa->ifa_family,
1815
0
               RTA_DATA(tb[IFA_BROADCAST]), buf,
1816
0
               sizeof(buf)),
1817
0
           ifa->ifa_prefixlen);
1818
0
    if (tb[IFA_LABEL])
1819
0
      zlog_debug("  IFA_LABEL     %s",
1820
0
           (const char *)RTA_DATA(tb[IFA_LABEL]));
1821
1822
0
    if (tb[IFA_CACHEINFO]) {
1823
0
      struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1824
1825
0
      zlog_debug("  IFA_CACHEINFO pref %d, valid %d",
1826
0
           ci->ifa_prefered, ci->ifa_valid);
1827
0
    }
1828
0
  }
1829
1830
  /* Validate prefix length */
1831
1832
0
  if (ifa->ifa_family == AF_INET
1833
0
      && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1834
0
    if (IS_ZEBRA_DEBUG_KERNEL)
1835
0
      zlog_debug("%s: %s: Invalid prefix length: %u",
1836
0
           __func__, nl_msg_type_to_str(h->nlmsg_type),
1837
0
           ifa->ifa_prefixlen);
1838
0
    return -1;
1839
0
  }
1840
1841
0
  if (ifa->ifa_family == AF_INET6) {
1842
0
    if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1843
0
      if (IS_ZEBRA_DEBUG_KERNEL)
1844
0
        zlog_debug("%s: %s: Invalid prefix length: %u",
1845
0
             __func__,
1846
0
             nl_msg_type_to_str(h->nlmsg_type),
1847
0
             ifa->ifa_prefixlen);
1848
0
      return -1;
1849
0
    }
1850
1851
    /* Only consider valid addresses; we'll not get a kernel
1852
     * notification till IPv6 DAD has completed, but at init
1853
     * time, FRR does query for and will receive all addresses.
1854
     */
1855
0
    if (h->nlmsg_type == RTM_NEWADDR
1856
0
        && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1857
0
      if (IS_ZEBRA_DEBUG_KERNEL)
1858
0
        zlog_debug("%s: %s: Invalid/tentative addr",
1859
0
             __func__,
1860
0
             nl_msg_type_to_str(h->nlmsg_type));
1861
0
      return 0;
1862
0
    }
1863
0
  }
1864
1865
  /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1866
0
  if (tb[IFA_LOCAL] == NULL)
1867
0
    tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1868
0
  if (tb[IFA_ADDRESS] == NULL)
1869
0
    tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1870
1871
  /* local interface address */
1872
0
  addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1873
1874
  /* addr is primary key, SOL if we don't have one */
1875
0
  if (addr == NULL) {
1876
0
    if (IS_ZEBRA_DEBUG_KERNEL)
1877
0
      zlog_debug("%s: %s: No local interface address",
1878
0
           __func__, nl_msg_type_to_str(h->nlmsg_type));
1879
0
    return -1;
1880
0
  }
1881
1882
  /* Allocate a context object, now that validation is done. */
1883
0
  ctx = dplane_ctx_alloc();
1884
0
  if (h->nlmsg_type == RTM_NEWADDR)
1885
0
    dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1886
0
  else
1887
0
    dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1888
1889
0
  dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1890
0
  dplane_ctx_set_ns_id(ctx, ns_id);
1891
1892
  /* Convert addr to prefix */
1893
0
  memset(&p, 0, sizeof(p));
1894
0
  p.family = ifa->ifa_family;
1895
0
  p.prefixlen = ifa->ifa_prefixlen;
1896
0
  if (p.family == AF_INET)
1897
0
    p.u.prefix4 = *(struct in_addr *)addr;
1898
0
  else
1899
0
    p.u.prefix6 = *(struct in6_addr *)addr;
1900
1901
0
  dplane_ctx_set_intf_addr(ctx, &p);
1902
1903
  /* is there a peer address? */
1904
0
  if (tb[IFA_ADDRESS]
1905
0
      && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1906
0
          RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1907
0
    broad = RTA_DATA(tb[IFA_ADDRESS]);
1908
0
    dplane_ctx_intf_set_connected(ctx);
1909
0
  } else if (tb[IFA_BROADCAST]) {
1910
    /* seeking a broadcast address */
1911
0
    broad = RTA_DATA(tb[IFA_BROADCAST]);
1912
0
    dplane_ctx_intf_set_broadcast(ctx);
1913
0
  } else
1914
0
    broad = NULL;
1915
1916
0
  if (broad) {
1917
    /* Convert addr to prefix */
1918
0
    memset(&p, 0, sizeof(p));
1919
0
    p.family = ifa->ifa_family;
1920
0
    p.prefixlen = ifa->ifa_prefixlen;
1921
0
    if (p.family == AF_INET)
1922
0
      p.u.prefix4 = *(struct in_addr *)broad;
1923
0
    else
1924
0
      p.u.prefix6 = *(struct in6_addr *)broad;
1925
1926
0
    dplane_ctx_set_intf_dest(ctx, &p);
1927
0
  }
1928
1929
  /* Flags. */
1930
0
  if (kernel_flags & IFA_F_SECONDARY)
1931
0
    dplane_ctx_intf_set_secondary(ctx);
1932
1933
  /* Label */
1934
0
  if (tb[IFA_LABEL]) {
1935
0
    label = (char *)RTA_DATA(tb[IFA_LABEL]);
1936
0
    dplane_ctx_set_intf_label(ctx, label);
1937
0
  }
1938
1939
0
  if (tb[IFA_RT_PRIORITY])
1940
0
    metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1941
1942
0
  dplane_ctx_set_intf_metric(ctx, metric);
1943
1944
  /* Enqueue ctx for main pthread to process */
1945
0
  dplane_provider_enqueue_to_zebra(ctx);
1946
1947
0
  return 0;
1948
0
}
1949
1950
int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1951
0
{
1952
0
  int len;
1953
0
  struct ifinfomsg *ifi;
1954
0
  struct rtattr *tb[IFLA_MAX + 1];
1955
0
  struct rtattr *linkinfo[IFLA_MAX + 1];
1956
0
  struct interface *ifp;
1957
0
  char *name = NULL;
1958
0
  char *kind = NULL;
1959
0
  char *desc = NULL;
1960
0
  char *slave_kind = NULL;
1961
0
  struct zebra_ns *zns;
1962
0
  vrf_id_t vrf_id = VRF_DEFAULT;
1963
0
  enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1964
0
  enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1965
0
  ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1966
0
  ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1967
0
  ifindex_t link_ifindex = IFINDEX_INTERNAL;
1968
0
  uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
1969
0
  struct zebra_if *zif;
1970
0
  ns_id_t link_nsid = ns_id;
1971
0
  ifindex_t master_infindex = IFINDEX_INTERNAL;
1972
0
  uint8_t bypass = 0;
1973
1974
0
  zns = zebra_ns_lookup(ns_id);
1975
0
  ifi = NLMSG_DATA(h);
1976
1977
  /* assume if not default zns, then new VRF */
1978
0
  if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1979
    /* If this is not link add/delete message so print warning. */
1980
0
    zlog_debug("%s: wrong kernel message %s", __func__,
1981
0
         nl_msg_type_to_str(h->nlmsg_type));
1982
0
    return 0;
1983
0
  }
1984
1985
0
  if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1986
0
        || ifi->ifi_family == AF_INET6)) {
1987
0
    flog_warn(
1988
0
      EC_ZEBRA_UNKNOWN_FAMILY,
1989
0
      "Invalid address family: %u received from kernel link change: %s",
1990
0
      ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
1991
0
    return 0;
1992
0
  }
1993
1994
0
  len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1995
0
  if (len < 0) {
1996
0
    zlog_err(
1997
0
      "%s: Message received from netlink is of a broken size %d %zu",
1998
0
      __func__, h->nlmsg_len,
1999
0
      (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
2000
0
    return -1;
2001
0
  }
2002
2003
  /* We are interested in some AF_BRIDGE notifications. */
2004
0
  if (ifi->ifi_family == AF_BRIDGE)
2005
0
    return netlink_bridge_interface(h, len, ns_id, startup);
2006
2007
  /* Looking up interface name. */
2008
0
  memset(linkinfo, 0, sizeof(linkinfo));
2009
0
  netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
2010
0
           NLA_F_NESTED);
2011
2012
  /* check for wireless messages to ignore */
2013
0
  if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
2014
0
    if (IS_ZEBRA_DEBUG_KERNEL)
2015
0
      zlog_debug("%s: ignoring IFLA_WIRELESS message",
2016
0
           __func__);
2017
0
    return 0;
2018
0
  }
2019
2020
0
  if (tb[IFLA_IFNAME] == NULL)
2021
0
    return -1;
2022
0
  name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
2023
2024
  /* Must be valid string. */
2025
0
  len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
2026
0
  if (len < 2 || name[len - 1] != '\0') {
2027
0
    if (IS_ZEBRA_DEBUG_KERNEL)
2028
0
      zlog_debug("%s: invalid intf name", __func__);
2029
0
    return -1;
2030
0
  }
2031
2032
0
  if (tb[IFLA_LINKINFO]) {
2033
0
    netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
2034
0
              tb[IFLA_LINKINFO]);
2035
2036
0
    if (linkinfo[IFLA_INFO_KIND])
2037
0
      kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
2038
2039
0
    if (linkinfo[IFLA_INFO_SLAVE_KIND])
2040
0
      slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
2041
2042
0
    netlink_determine_zebra_iftype(kind, &zif_type);
2043
0
  }
2044
2045
  /* If linking to another interface, note it. */
2046
0
  if (tb[IFLA_LINK])
2047
0
    link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
2048
2049
0
  if (tb[IFLA_LINK_NETNSID]) {
2050
0
    link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
2051
0
    link_nsid = ns_id_get_absolute(ns_id, link_nsid);
2052
0
  }
2053
0
  if (tb[IFLA_IFALIAS]) {
2054
0
    desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
2055
0
  }
2056
2057
  /* See if interface is present. */
2058
0
  ifp = if_lookup_by_name_per_ns(zns, name);
2059
2060
0
  if (h->nlmsg_type == RTM_NEWLINK) {
2061
    /* If VRF, create or update the VRF structure itself. */
2062
0
    if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
2063
0
      netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2064
0
      vrf_id = (vrf_id_t)ifi->ifi_index;
2065
0
    }
2066
2067
0
    if (tb[IFLA_MASTER]) {
2068
0
      if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
2069
0
          && !vrf_is_backend_netns()) {
2070
0
        zif_slave_type = ZEBRA_IF_SLAVE_VRF;
2071
0
        master_infindex = vrf_id =
2072
0
          *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
2073
0
      } else if (slave_kind
2074
0
           && (strcmp(slave_kind, "bridge") == 0)) {
2075
0
        zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
2076
0
        master_infindex = bridge_ifindex =
2077
0
          *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
2078
0
      } else if (slave_kind
2079
0
           && (strcmp(slave_kind, "bond") == 0)) {
2080
0
        zif_slave_type = ZEBRA_IF_SLAVE_BOND;
2081
0
        master_infindex = bond_ifindex =
2082
0
          *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
2083
0
        bypass = netlink_parse_lacp_bypass(linkinfo);
2084
0
      } else
2085
0
        zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
2086
0
    }
2087
0
    if (vrf_is_backend_netns())
2088
0
      vrf_id = (vrf_id_t)ns_id;
2089
0
    if (ifp == NULL
2090
0
        || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2091
      /* Add interface notification from kernel */
2092
0
      if (IS_ZEBRA_DEBUG_KERNEL)
2093
0
        zlog_debug(
2094
0
          "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
2095
0
          name, ifi->ifi_index, vrf_id, zif_type,
2096
0
          zif_slave_type, master_infindex,
2097
0
          ifi->ifi_flags);
2098
2099
0
      if (ifp == NULL) {
2100
        /* unknown interface */
2101
0
        ifp = if_get_by_name(name, vrf_id, NULL);
2102
0
      } else {
2103
        /* pre-configured interface, learnt now */
2104
0
        if (ifp->vrf->vrf_id != vrf_id)
2105
0
          if_update_to_new_vrf(ifp, vrf_id);
2106
0
      }
2107
2108
      /* Update interface information. */
2109
0
      set_ifindex(ifp, ifi->ifi_index, zns);
2110
0
      ifp->flags = ifi->ifi_flags & 0x0000fffff;
2111
0
      if (!tb[IFLA_MTU]) {
2112
0
        zlog_debug(
2113
0
          "RTM_NEWLINK for interface %s(%u) without MTU set",
2114
0
          name, ifi->ifi_index);
2115
0
        return 0;
2116
0
      }
2117
0
      ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2118
0
      ifp->metric = 0;
2119
0
      ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
2120
2121
      /* Set interface type */
2122
0
      zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2123
0
      if (IS_ZEBRA_IF_VRF(ifp))
2124
0
        SET_FLAG(ifp->status,
2125
0
           ZEBRA_INTERFACE_VRF_LOOPBACK);
2126
2127
      /* Update link. */
2128
0
      zebra_if_update_link(ifp, link_ifindex, link_nsid);
2129
2130
0
      ifp->ll_type =
2131
0
        netlink_to_zebra_link_type(ifi->ifi_type);
2132
0
      netlink_interface_update_hw_addr(tb, ifp);
2133
2134
      /* Inform clients, install any configured addresses. */
2135
0
      if_add_update(ifp);
2136
2137
      /* Extract and save L2 interface information, take
2138
       * additional actions. */
2139
0
      netlink_interface_update_l2info(
2140
0
        ifp, linkinfo[IFLA_INFO_DATA],
2141
0
        1, link_nsid);
2142
0
      if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
2143
0
        zebra_l2if_update_bridge_slave(
2144
0
          ifp, bridge_ifindex, ns_id,
2145
0
          ZEBRA_BRIDGE_NO_ACTION);
2146
0
      else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2147
0
        zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2148
0
                   !!bypass);
2149
2150
0
      if (tb[IFLA_PROTO_DOWN])
2151
0
        netlink_proc_dplane_if_protodown(ifp->info, tb);
2152
0
      if (IS_ZEBRA_IF_BRIDGE(ifp)) {
2153
0
        zif = ifp->info;
2154
0
        if (IS_ZEBRA_DEBUG_KERNEL)
2155
0
          zlog_debug(
2156
0
            "RTM_NEWLINK ADD for %s(%u), vlan-aware %d",
2157
0
            name, ifp->ifindex,
2158
0
            IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2159
0
              zif));
2160
0
      }
2161
0
    } else if (ifp->vrf->vrf_id != vrf_id) {
2162
      /* VRF change for an interface. */
2163
0
      if (IS_ZEBRA_DEBUG_KERNEL)
2164
0
        zlog_debug(
2165
0
          "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
2166
0
          name, ifp->ifindex, ifp->vrf->vrf_id,
2167
0
          vrf_id, ifi->ifi_flags);
2168
2169
0
      if_handle_vrf_change(ifp, vrf_id);
2170
0
    } else {
2171
0
      bool was_bridge_slave, was_bond_slave;
2172
0
      uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
2173
0
      zif = ifp->info;
2174
2175
      /* Interface update. */
2176
0
      if (IS_ZEBRA_DEBUG_KERNEL)
2177
0
        zlog_debug(
2178
0
          "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
2179
0
          name, ifp->ifindex, zif_slave_type,
2180
0
          master_infindex, ifi->ifi_flags);
2181
2182
0
      set_ifindex(ifp, ifi->ifi_index, zns);
2183
0
      if (!tb[IFLA_MTU]) {
2184
0
        zlog_debug(
2185
0
          "RTM_NEWLINK for interface %s(%u) without MTU set",
2186
0
          name, ifi->ifi_index);
2187
0
        return 0;
2188
0
      }
2189
0
      ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2190
0
      ifp->metric = 0;
2191
2192
      /* Update interface type - NOTE: Only slave_type can
2193
       * change. */
2194
0
      was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
2195
0
      was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
2196
0
      zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2197
2198
0
      memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
2199
2200
      /* Update link. */
2201
0
      zebra_if_update_link(ifp, link_ifindex, link_nsid);
2202
2203
0
      ifp->ll_type =
2204
0
        netlink_to_zebra_link_type(ifi->ifi_type);
2205
0
      netlink_interface_update_hw_addr(tb, ifp);
2206
2207
0
      if (tb[IFLA_PROTO_DOWN])
2208
0
        netlink_proc_dplane_if_protodown(ifp->info, tb);
2209
2210
0
      if (if_is_no_ptm_operative(ifp)) {
2211
0
        bool is_up = if_is_operative(ifp);
2212
0
        ifp->flags = ifi->ifi_flags & 0x0000fffff;
2213
0
        if (!if_is_no_ptm_operative(ifp) ||
2214
0
            CHECK_FLAG(zif->flags,
2215
0
                 ZIF_FLAG_PROTODOWN)) {
2216
0
          if (IS_ZEBRA_DEBUG_KERNEL)
2217
0
            zlog_debug(
2218
0
              "Intf %s(%u) has gone DOWN",
2219
0
              name, ifp->ifindex);
2220
0
          if_down(ifp);
2221
0
          rib_update(RIB_UPDATE_KERNEL);
2222
0
        } else if (if_is_operative(ifp)) {
2223
0
          bool mac_updated = false;
2224
2225
          /* Must notify client daemons of new
2226
           * interface status. */
2227
0
          if (IS_ZEBRA_DEBUG_KERNEL)
2228
0
            zlog_debug(
2229
0
              "Intf %s(%u) PTM up, notifying clients",
2230
0
              name, ifp->ifindex);
2231
0
          if_up(ifp, !is_up);
2232
2233
          /* Update EVPN VNI when SVI MAC change
2234
           */
2235
0
          if (memcmp(old_hw_addr, ifp->hw_addr,
2236
0
               INTERFACE_HWADDR_MAX))
2237
0
            mac_updated = true;
2238
0
          if (IS_ZEBRA_IF_VLAN(ifp)
2239
0
              && mac_updated) {
2240
0
            struct interface *link_if;
2241
2242
0
            link_if =
2243
0
            if_lookup_by_index_per_ns(
2244
0
            zebra_ns_lookup(NS_DEFAULT),
2245
0
                link_ifindex);
2246
0
            if (link_if)
2247
0
              zebra_vxlan_svi_up(ifp,
2248
0
                link_if);
2249
0
          } else if (mac_updated
2250
0
               && IS_ZEBRA_IF_BRIDGE(ifp)) {
2251
0
            zlog_debug(
2252
0
              "Intf %s(%u) bridge changed MAC address",
2253
0
              name, ifp->ifindex);
2254
0
            chgflags =
2255
0
              ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
2256
0
          }
2257
0
        }
2258
0
      } else {
2259
0
        ifp->flags = ifi->ifi_flags & 0x0000fffff;
2260
0
        if (if_is_operative(ifp) &&
2261
0
            !CHECK_FLAG(zif->flags,
2262
0
            ZIF_FLAG_PROTODOWN)) {
2263
0
          if (IS_ZEBRA_DEBUG_KERNEL)
2264
0
            zlog_debug(
2265
0
              "Intf %s(%u) has come UP",
2266
0
              name, ifp->ifindex);
2267
0
          if_up(ifp, true);
2268
0
          if (IS_ZEBRA_IF_BRIDGE(ifp))
2269
0
            chgflags =
2270
0
              ZEBRA_BRIDGE_MASTER_UP;
2271
0
        } else {
2272
0
          if (IS_ZEBRA_DEBUG_KERNEL)
2273
0
            zlog_debug(
2274
0
              "Intf %s(%u) has gone DOWN",
2275
0
              name, ifp->ifindex);
2276
0
          if_down(ifp);
2277
0
          rib_update(RIB_UPDATE_KERNEL);
2278
0
        }
2279
0
      }
2280
2281
      /* Extract and save L2 interface information, take
2282
       * additional actions. */
2283
0
      netlink_interface_update_l2info(
2284
0
        ifp, linkinfo[IFLA_INFO_DATA],
2285
0
        0, link_nsid);
2286
0
      if (IS_ZEBRA_IF_BRIDGE(ifp))
2287
0
        zebra_l2if_update_bridge(ifp, chgflags);
2288
0
      if (IS_ZEBRA_IF_BOND(ifp))
2289
0
        zebra_l2if_update_bond(ifp, true);
2290
0
      if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
2291
0
        zebra_l2if_update_bridge_slave(
2292
0
          ifp, bridge_ifindex, ns_id, chgflags);
2293
0
      else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
2294
0
        zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2295
0
                   !!bypass);
2296
0
      if (IS_ZEBRA_IF_BRIDGE(ifp)) {
2297
0
        if (IS_ZEBRA_DEBUG_KERNEL)
2298
0
          zlog_debug(
2299
0
            "RTM_NEWLINK update for %s(%u), vlan-aware %d",
2300
0
            name, ifp->ifindex,
2301
0
            IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2302
0
              zif));
2303
0
      }
2304
0
    }
2305
2306
0
    zif = ifp->info;
2307
0
    if (zif) {
2308
0
      XFREE(MTYPE_ZIF_DESC, zif->desc);
2309
0
      if (desc)
2310
0
        zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
2311
0
    }
2312
0
  } else {
2313
    /* Delete interface notification from kernel */
2314
0
    if (ifp == NULL) {
2315
0
      if (IS_ZEBRA_DEBUG_KERNEL)
2316
0
        zlog_debug(
2317
0
          "RTM_DELLINK for unknown interface %s(%u)",
2318
0
          name, ifi->ifi_index);
2319
0
      return 0;
2320
0
    }
2321
2322
0
    if (IS_ZEBRA_DEBUG_KERNEL)
2323
0
      zlog_debug("RTM_DELLINK for %s(%u)", name,
2324
0
           ifp->ifindex);
2325
2326
0
    if (IS_ZEBRA_IF_BOND(ifp))
2327
0
      zebra_l2if_update_bond(ifp, false);
2328
0
    if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2329
0
      zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
2330
    /* Special handling for bridge or VxLAN interfaces. */
2331
0
    if (IS_ZEBRA_IF_BRIDGE(ifp))
2332
0
      zebra_l2_bridge_del(ifp);
2333
0
    else if (IS_ZEBRA_IF_VXLAN(ifp))
2334
0
      zebra_l2_vxlanif_del(ifp);
2335
2336
0
    if_delete_update(&ifp);
2337
2338
    /* If VRF, delete the VRF structure itself. */
2339
0
    if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2340
0
      netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2341
0
  }
2342
2343
0
  return 0;
2344
0
}
2345
2346
/**
2347
 * Interface encoding helper function.
2348
 *
2349
 * \param[in] cmd netlink command.
2350
 * \param[in] ctx dataplane context (information snapshot).
2351
 * \param[out] buf buffer to hold the packet.
2352
 * \param[in] buflen amount of buffer bytes.
2353
 */
2354
2355
ssize_t netlink_intf_msg_encode(uint16_t cmd,
2356
        const struct zebra_dplane_ctx *ctx, void *buf,
2357
        size_t buflen)
2358
0
{
2359
0
  struct {
2360
0
    struct nlmsghdr n;
2361
0
    struct ifinfomsg ifa;
2362
0
    char buf[];
2363
0
  } *req = buf;
2364
2365
0
  struct rtattr *nest_protodown_reason;
2366
0
  ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
2367
0
  bool down = dplane_ctx_intf_is_protodown(ctx);
2368
0
  bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
2369
0
  struct nlsock *nl =
2370
0
    kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
2371
2372
0
  if (buflen < sizeof(*req))
2373
0
    return 0;
2374
2375
0
  memset(req, 0, sizeof(*req));
2376
2377
0
  if (cmd != RTM_SETLINK)
2378
0
    flog_err(
2379
0
      EC_ZEBRA_INTF_UPDATE_FAILURE,
2380
0
      "Only RTM_SETLINK message type currently supported in dplane pthread");
2381
2382
0
  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2383
0
  req->n.nlmsg_flags = NLM_F_REQUEST;
2384
0
  req->n.nlmsg_type = cmd;
2385
0
  req->n.nlmsg_pid = nl->snl.nl_pid;
2386
2387
0
  req->ifa.ifi_index = ifindex;
2388
2389
0
  nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2390
0
  nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2391
2392
  /* Reason info nest */
2393
0
  nest_protodown_reason =
2394
0
    nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
2395
2396
0
  if (!nest_protodown_reason)
2397
0
    return -1;
2398
2399
0
  nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2400
0
          (1 << frr_protodown_r_bit));
2401
0
  nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
2402
0
          ((int)pd_reason_val) << frr_protodown_r_bit);
2403
2404
0
  nl_attr_nest_end(&req->n, nest_protodown_reason);
2405
2406
0
  if (IS_ZEBRA_DEBUG_KERNEL)
2407
0
    zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2408
0
         __func__, nl_msg_type_to_str(cmd), down,
2409
0
         pd_reason_val, ifindex);
2410
2411
0
  return NLMSG_ALIGN(req->n.nlmsg_len);
2412
0
}
2413
2414
/* Interface information read by netlink. */
2415
void interface_list(struct zebra_ns *zns)
2416
1
{
2417
1
  interface_lookup_netlink(zns);
2418
  /* We add routes for interface address,
2419
   * so we need to get the nexthop info
2420
   * from the kernel before we can do that
2421
   */
2422
1
  netlink_nexthop_read(zns);
2423
2424
1
  interface_addr_lookup_netlink(zns);
2425
1
}
2426
2427
void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2428
0
{
2429
0
  if (IS_ZEBRA_DEBUG_KERNEL)
2430
0
    zlog_debug(
2431
0
      "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2432
0
      frr_protodown_r_bit, bit);
2433
2434
0
  frr_protodown_r_bit = bit;
2435
0
}
2436
2437
void if_netlink_unset_frr_protodown_r_bit(void)
2438
0
{
2439
0
  if (IS_ZEBRA_DEBUG_KERNEL)
2440
0
    zlog_debug(
2441
0
      "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2442
0
      frr_protodown_r_bit, FRR_PROTODOWN_REASON_DEFAULT_BIT);
2443
2444
0
  frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2445
0
}
2446
2447
2448
bool if_netlink_frr_protodown_r_bit_is_set(void)
2449
0
{
2450
0
  return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2451
0
}
2452
2453
uint8_t if_netlink_get_frr_protodown_r_bit(void)
2454
0
{
2455
0
  return frr_protodown_r_bit;
2456
0
}
2457
2458
/**
2459
 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2460
 *
2461
 * @zns:  Zebra namespace
2462
 * @family: AF_* netlink family
2463
 * @type: RTM_* (RTM_GETTUNNEL) route type
2464
 *
2465
 * Return:  Result status
2466
 */
2467
static int netlink_request_tunneldump(struct zebra_ns *zns, int family,
2468
              int ifindex)
2469
0
{
2470
0
  struct {
2471
0
    struct nlmsghdr n;
2472
0
    struct tunnel_msg tmsg;
2473
0
    char buf[256];
2474
0
  } req;
2475
2476
  /* Form the request */
2477
0
  memset(&req, 0, sizeof(req));
2478
0
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg));
2479
0
  req.n.nlmsg_type = RTM_GETTUNNEL;
2480
0
  req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2481
0
  req.tmsg.family = family;
2482
0
  req.tmsg.ifindex = ifindex;
2483
2484
0
  return netlink_request(&zns->netlink_cmd, &req);
2485
0
}
2486
2487
/*
2488
 * Currently we only ask for vxlan l3svd vni information.
2489
 * In the future this can be expanded.
2490
 */
2491
int netlink_tunneldump_read(struct zebra_ns *zns)
2492
0
{
2493
0
  int ret = 0;
2494
0
  struct zebra_dplane_info dp_info;
2495
0
  struct route_node *rn;
2496
0
  struct interface *tmp_if = NULL;
2497
0
  struct zebra_if *zif;
2498
0
  struct nlsock *netlink_cmd = &zns->netlink_cmd;
2499
2500
0
  zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2501
2502
0
  for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2503
0
    tmp_if = (struct interface *)rn->info;
2504
0
    if (!tmp_if)
2505
0
      continue;
2506
0
    zif = tmp_if->info;
2507
0
    if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2508
0
      continue;
2509
2510
0
    ret = netlink_request_tunneldump(zns, PF_BRIDGE,
2511
0
             tmp_if->ifindex);
2512
0
    if (ret < 0)
2513
0
      return ret;
2514
2515
0
    ret = netlink_parse_info(netlink_interface, netlink_cmd,
2516
0
           &dp_info, 0, true);
2517
2518
0
    if (ret < 0)
2519
0
      return ret;
2520
0
  }
2521
2522
0
  return 0;
2523
0
}
2524
2525
static const char *port_state2str(uint8_t state)
2526
0
{
2527
0
  switch (state) {
2528
0
  case BR_STATE_DISABLED:
2529
0
    return "DISABLED";
2530
0
  case BR_STATE_LISTENING:
2531
0
    return "LISTENING";
2532
0
  case BR_STATE_LEARNING:
2533
0
    return "LEARNING";
2534
0
  case BR_STATE_FORWARDING:
2535
0
    return "FORWARDING";
2536
0
  case BR_STATE_BLOCKING:
2537
0
    return "BLOCKING";
2538
0
  }
2539
0
2540
0
  return "UNKNOWN";
2541
0
}
2542
2543
static void vxlan_vni_state_change(struct zebra_if *zif, uint16_t id,
2544
           uint8_t state)
2545
0
{
2546
0
  struct zebra_vxlan_vni *vnip;
2547
2548
0
  vnip = zebra_vxlan_if_vlanid_vni_find(zif, id);
2549
2550
0
  if (!vnip) {
2551
0
    if (IS_ZEBRA_DEBUG_VXLAN)
2552
0
      zlog_debug(
2553
0
        "Cannot find VNI for VID (%u) IF %s for vlan state update",
2554
0
        id, zif->ifp->name);
2555
2556
0
    return;
2557
0
  }
2558
2559
0
  switch (state) {
2560
0
  case BR_STATE_FORWARDING:
2561
0
    zebra_vxlan_if_vni_up(zif->ifp, vnip);
2562
0
    break;
2563
0
  case BR_STATE_BLOCKING:
2564
0
    zebra_vxlan_if_vni_down(zif->ifp, vnip);
2565
0
    break;
2566
0
  case BR_STATE_DISABLED:
2567
0
  case BR_STATE_LISTENING:
2568
0
  case BR_STATE_LEARNING:
2569
0
  default:
2570
    /* Not used for anything at the moment */
2571
0
    break;
2572
0
  }
2573
0
}
2574
2575
static void vlan_id_range_state_change(struct interface *ifp, uint16_t id_start,
2576
               uint16_t id_end, uint8_t state)
2577
0
{
2578
0
  struct zebra_if *zif;
2579
2580
0
  zif = (struct zebra_if *)ifp->info;
2581
2582
0
  if (!zif)
2583
0
    return;
2584
2585
0
  for (uint16_t i = id_start; i <= id_end; i++)
2586
0
    vxlan_vni_state_change(zif, i, state);
2587
0
}
2588
2589
/**
2590
 * netlink_vlan_change() - Read in change about vlans from the kernel
2591
 *
2592
 * @h:    Netlink message header
2593
 * @ns_id:  Namspace id
2594
 * @startup:  Are we reading under startup conditions?
2595
 *
2596
 * Return:  Result status
2597
 */
2598
int netlink_vlan_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2599
0
{
2600
0
  int len, rem;
2601
0
  struct interface *ifp;
2602
0
  struct br_vlan_msg *bvm;
2603
0
  struct bridge_vlan_info *vinfo;
2604
0
  struct rtattr *vtb[BRIDGE_VLANDB_ENTRY_MAX + 1] = {};
2605
0
  struct rtattr *attr;
2606
0
  uint8_t state;
2607
0
  uint32_t vrange;
2608
0
  int type;
2609
2610
  /* We only care about state changes for now */
2611
0
  if (!(h->nlmsg_type == RTM_NEWVLAN))
2612
0
    return 0;
2613
2614
0
  len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2615
0
  if (len < 0) {
2616
0
    zlog_warn(
2617
0
      "%s: Message received from netlink is of a broken size %d %zu",
2618
0
      __func__, h->nlmsg_len,
2619
0
      (size_t)NLMSG_LENGTH(sizeof(struct br_vlan_msg)));
2620
0
    return -1;
2621
0
  }
2622
2623
0
  bvm = NLMSG_DATA(h);
2624
2625
0
  if (bvm->family != AF_BRIDGE)
2626
0
    return 0;
2627
2628
0
  ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), bvm->ifindex);
2629
0
  if (!ifp) {
2630
0
    zlog_debug("Cannot find bridge-vlan IF (%u) for vlan update",
2631
0
         bvm->ifindex);
2632
0
    return 0;
2633
0
  }
2634
2635
0
  if (!IS_ZEBRA_IF_VXLAN(ifp)) {
2636
0
    if (IS_ZEBRA_DEBUG_KERNEL)
2637
0
      zlog_debug("Ignoring non-vxlan IF (%s) for vlan update",
2638
0
           ifp->name);
2639
2640
0
    return 0;
2641
0
  }
2642
2643
0
  if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN)
2644
0
    zlog_debug("%s %s IF %s NS %u",
2645
0
         nl_msg_type_to_str(h->nlmsg_type),
2646
0
         nl_family_to_str(bvm->family), ifp->name, ns_id);
2647
2648
  /* Loop over "ALL" BRIDGE_VLANDB_ENTRY */
2649
0
  rem = len;
2650
0
  for (attr = BRVLAN_RTA(bvm); RTA_OK(attr, rem);
2651
0
       attr = RTA_NEXT(attr, rem)) {
2652
0
    vinfo = NULL;
2653
0
    vrange = 0;
2654
2655
0
    type = attr->rta_type & NLA_TYPE_MASK;
2656
2657
0
    if (type != BRIDGE_VLANDB_ENTRY)
2658
0
      continue;
2659
2660
    /* Parse nested entry data */
2661
0
    netlink_parse_rtattr_nested(vtb, BRIDGE_VLANDB_ENTRY_MAX, attr);
2662
2663
    /* It must have info for the ID */
2664
0
    if (!vtb[BRIDGE_VLANDB_ENTRY_INFO])
2665
0
      continue;
2666
2667
0
    vinfo = (struct bridge_vlan_info *)RTA_DATA(
2668
0
      vtb[BRIDGE_VLANDB_ENTRY_INFO]);
2669
2670
    /*
2671
     * We only care about state info, if there is none, just ignore
2672
     * it.
2673
     */
2674
0
    if (!vtb[BRIDGE_VLANDB_ENTRY_STATE])
2675
0
      continue;
2676
2677
0
    state = *(uint8_t *)RTA_DATA(vtb[BRIDGE_VLANDB_ENTRY_STATE]);
2678
2679
0
    if (vtb[BRIDGE_VLANDB_ENTRY_RANGE])
2680
0
      vrange = *(uint32_t *)RTA_DATA(
2681
0
        vtb[BRIDGE_VLANDB_ENTRY_RANGE]);
2682
2683
0
    if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN) {
2684
0
      if (vrange)
2685
0
        zlog_debug("VLANDB_ENTRY: VID (%u-%u) state=%s",
2686
0
             vinfo->vid, vrange,
2687
0
             port_state2str(state));
2688
0
      else
2689
0
        zlog_debug("VLANDB_ENTRY: VID (%u) state=%s",
2690
0
             vinfo->vid, port_state2str(state));
2691
0
    }
2692
2693
0
    vlan_id_range_state_change(
2694
0
      ifp, vinfo->vid, (vrange ? vrange : vinfo->vid), state);
2695
0
  }
2696
2697
0
  return 0;
2698
0
}
2699
2700
/**
2701
 * netlink_request_vlan() - Request vlan information from the kernel
2702
 * @zns:  Zebra namespace
2703
 * @family: AF_* netlink family
2704
 * @type: RTM_* type
2705
 *
2706
 * Return:  Result status
2707
 */
2708
static int netlink_request_vlan(struct zebra_ns *zns, int family, int type)
2709
1
{
2710
1
  struct {
2711
1
    struct nlmsghdr n;
2712
1
    struct br_vlan_msg bvm;
2713
1
    char buf[256];
2714
1
  } req;
2715
2716
  /* Form the request, specifying filter (rtattr) if needed. */
2717
1
  memset(&req, 0, sizeof(req));
2718
1
  req.n.nlmsg_type = type;
2719
1
  req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2720
1
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2721
1
  req.bvm.family = family;
2722
2723
1
  nl_attr_put32(&req.n, sizeof(req), BRIDGE_VLANDB_DUMP_FLAGS,
2724
1
          BRIDGE_VLANDB_DUMPF_STATS);
2725
2726
1
  return netlink_request(&zns->netlink_cmd, &req);
2727
1
}
2728
2729
/**
2730
 * netlink_vlan_read() - Vlan read function using netlink interface
2731
 *
2732
 * @zns:  Zebra name space
2733
 *
2734
 * Return:  Result status
2735
 * Only called at bootstrap time.
2736
 */
2737
int netlink_vlan_read(struct zebra_ns *zns)
2738
1
{
2739
1
  int ret;
2740
1
  struct zebra_dplane_info dp_info;
2741
2742
1
  zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2743
2744
  /* Get bridg vlan info */
2745
1
  ret = netlink_request_vlan(zns, PF_BRIDGE, RTM_GETVLAN);
2746
1
  if (ret < 0)
2747
1
    return ret;
2748
2749
0
  ret = netlink_parse_info(netlink_vlan_change, &zns->netlink_cmd,
2750
0
         &dp_info, 0, 1);
2751
2752
0
  return ret;
2753
1
}
2754
2755
#endif /* GNU_LINUX */