DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Linux network interface functions

Implement various interface routines from libnetlink. More...

Files

file  interface.c
 Wrapper arround Linux libnetlink for managing network interfaces.
 

Data Structures

struct  iplink_req
 IP Netlink request. More...
 
struct  ipaddr_req
 IP Netlink IP addr request. More...
 

Enumerations

enum  ipv4_score { IPV4_SCORE_ZEROCONF = 1 << 0, IPV4_SCORE_RESERVED = 1 << 1, IPV4_SCORE_ROUTABLE = 1 << 2 }
 Order of precidence of ipv4. More...
 
enum  ipv6_score { IPV6_SCORE_RESERVED = 1 << 0, IPV6_SCORE_SIXIN4 = 1 << 1, IPV6_SCORE_ROUTABLE = 1 << 2 }
 Return best ipv6 address in order of FFC/7 2002/16 ... More...
 

Functions

void closenetlink ()
 Close netlink socket on application termination. More...
 
int get_iface_index (const char *ifname)
 Get the netlink interface for a named interface. More...
 
int delete_kernvlan (char *ifname, int vid)
 Delete a VLAN. More...
 
int create_kernvlan (char *ifname, unsigned short vid)
 Create a VLAN on a interface. More...
 
int delete_kernmac (char *ifname)
 Delete Kernel MAC VLAN. More...
 
int create_kernmac (char *ifname, char *macdev, unsigned char *mac)
 Create a kernal MAC VLAN. More...
 
int set_interface_flags (int ifindex, int set, int clear)
 Alter interface flags. More...
 
int set_interface_addr (int ifindex, const unsigned char *hwaddr)
 Set interface MAC addr. More...
 
int set_interface_name (int ifindex, const char *name)
 Rename interface. More...
 
int interface_bind (char *iface, int protocol)
 Bind to device fd may be a existing socket. More...
 
void randhwaddr (unsigned char *addr)
 create random MAC address More...
 
int create_tun (const char *ifname, const unsigned char *hwaddr, int flags)
 Create a tunnel device. More...
 
int ifdown (const char *ifname, int flags)
 Set interface down. More...
 
int ifup (const char *ifname, int flags)
 Set interface up. More...
 
int ifrename (const char *oldname, const char *newname)
 Rename interface helper. More...
 
int ifhwaddr (const char *ifname, unsigned char *hwaddr)
 Get MAC addr for interface. More...
 
int set_interface_ipaddr (char *ifname, char *ipaddr)
 Set IP addr on interface. More...
 
const char * get_ifipaddr (const char *iface, int family)
 Find best IP adress for a interface. More...
 

Detailed Description

Implement various interface routines from libnetlink.

Enumeration Type Documentation

enum ipv4_score

Order of precidence of ipv4.

Enumerator
IPV4_SCORE_ZEROCONF 

Zeroconf IP's 169.254/16.

IPV4_SCORE_RESERVED 

Reseverd "private" ip addresses.

IPV4_SCORE_ROUTABLE 

Routable IP's.

Definition at line 63 of file interface.c.

63  {
65  IPV4_SCORE_ZEROCONF = 1 << 0,
67  IPV4_SCORE_RESERVED = 1 << 1,
69  IPV4_SCORE_ROUTABLE = 1 << 2
70 };
Reseverd &quot;private&quot; ip addresses.
Definition: interface.c:67
Routable IP&#39;s.
Definition: interface.c:69
Zeroconf IP&#39;s 169.254/16.
Definition: interface.c:65
enum ipv6_score

Return best ipv6 address in order of FFC/7 2002/16 ...

Enumerator
IPV6_SCORE_RESERVED 

Adminstrivly allocated addresses (FC/7)

IPV6_SCORE_SIXIN4 

6in4 address space

IPV6_SCORE_ROUTABLE 

Other routable addresses.

Definition at line 73 of file interface.c.

73  {
75  IPV6_SCORE_RESERVED = 1 << 0,
77  IPV6_SCORE_SIXIN4 = 1 << 1,
79  IPV6_SCORE_ROUTABLE = 1 << 2
80 };
Adminstrivly allocated addresses (FC/7)
Definition: interface.c:75
6in4 address space
Definition: interface.c:77
Other routable addresses.
Definition: interface.c:79

Function Documentation

void closenetlink ( void  )

Close netlink socket on application termination.

Definition at line 130 of file interface.c.

References objunref().

130  {
131  if (nlh) {
132  objunref(nlh);
133  }
134 }
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int create_kernmac ( char *  ifname,
char *  macdev,
unsigned char *  mac 
)

Create a kernal MAC VLAN.

Parameters
ifnameInterface name to create
macdevBase interface
macMAC address to use or random if NULL.
Returns
-1 on error.

Definition at line 282 of file interface.c.

References get_iface_index(), iplink_req::n, objalloc(), objlock(), objref(), objunlock(), objunref(), randhwaddr(), and strlenzero().

282  {
283  struct iplink_req *req;
284  struct rtattr *data, *linkinfo;
285  unsigned char lmac[ETH_ALEN];
286  char *type = "macvlan";
287  int ifindex, ret;
288 
289  if (strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
290  strlenzero(macdev) || (strlen(macdev) > IFNAMSIZ) ||
291  (!objref(nlh) && !(nlh = nlhandle(0)))) {
292  return (-1);
293  }
294 
295  /*set the index of base interface*/
296  if (!(ifindex = get_iface_index(ifname))) {
297  objunref(nlh);
298  return (-1);
299  }
300 
301  if (!mac) {
302  randhwaddr(lmac);
303  } else {
304  strncpy((char *)lmac, (char *)mac, ETH_ALEN);
305  }
306 
307  if (!(req = objalloc(sizeof(*req), NULL))) {
308  objunref(nlh);
309  return (-1);
310  }
311 
312  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
313  req->n.nlmsg_type = RTM_NEWLINK;
314  req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL | NLM_F_REQUEST;
315 
316  /*config base/dev/mac*/
317  addattr_l(&req->n, sizeof(*req), IFLA_LINK, &ifindex, 4);
318  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, macdev, strlen(macdev));
319  addattr_l(&req->n, sizeof(*req), IFLA_ADDRESS, lmac, ETH_ALEN);
320 
321  /*type*/
322  linkinfo = NLMSG_TAIL(&req->n);
323  addattr_l(&req->n, sizeof(*req), IFLA_LINKINFO, NULL, 0);
324  addattr_l(&req->n, sizeof(*req), IFLA_INFO_KIND, type, strlen(type));
325 
326  /*mode*/
327  data = NLMSG_TAIL(&req->n);
328  addattr_l(&req->n, sizeof(*req), IFLA_INFO_DATA, NULL, 0);
329  addattr32(&req->n, sizeof(*req), IFLA_MACVLAN_MODE, MACVLAN_MODE_PRIVATE);
330  data->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)data;
331  linkinfo->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)linkinfo;
332 
333  objlock(nlh);
334  ret = rtnl_talk(nlh, &req->n, 0, 0, NULL);
335  objunlock(nlh);
336 
337  objunref(nlh);
338  objunref(req);
339 
340  return (ret);
341 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
void randhwaddr(unsigned char *addr)
create random MAC address
Definition: interface.c:485
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int create_kernvlan ( char *  ifname,
unsigned short  vid 
)

Create a VLAN on a interface.

Parameters
ifnameInterface to add VLAN to.
vidVLAN id to add.
Returns
-1 on error.

Definition at line 214 of file interface.c.

References get_iface_index(), iplink_req::n, objalloc(), objlock(), objref(), objunlock(), objunref(), and strlenzero().

214  {
215  struct iplink_req *req;
216  char iface[IFNAMSIZ+1];
217  struct rtattr *data, *linkinfo;
218  char *type = "vlan";
219  int ifindex, ret;
220 
221  if (strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
222  (!objref(nlh) && !(nlh = nlhandle(0)))) {
223  return (-1);
224  }
225 
226  /*set the index of base interface*/
227  if (!(ifindex = get_iface_index(ifname))) {
228  objunref(nlh);
229  return (-1);
230  }
231 
232  if (!(req = objalloc(sizeof(*req), NULL))) {
233  objunref(nlh);
234  return (-1);
235  }
236 
237  snprintf(iface, IFNAMSIZ, "%s.%i", ifname, vid);
238  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
239  req->n.nlmsg_type = RTM_NEWLINK;
240  req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL | NLM_F_REQUEST;
241 
242  /*config base/dev/mac*/
243  addattr_l(&req->n, sizeof(*req), IFLA_LINK, &ifindex, sizeof(ifindex));
244  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, iface, strlen(iface));
245 
246  /*type*/
247  linkinfo = NLMSG_TAIL(&req->n);
248  addattr_l(&req->n, sizeof(*req), IFLA_LINKINFO, NULL, 0);
249  addattr_l(&req->n, sizeof(*req), IFLA_INFO_KIND, type, strlen(type));
250 
251  /*vid*/
252  data = NLMSG_TAIL(&req->n);
253  addattr_l(&req->n, sizeof(*req), IFLA_INFO_DATA, NULL, 0);
254  addattr_l(&req->n, sizeof(*req), IFLA_VLAN_ID, &vid, sizeof(vid));
255 
256  data->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)data;
257  linkinfo->rta_len = (char *)NLMSG_TAIL(&req->n) - (char *)linkinfo;
258 
259  objlock(nlh);
260  ret = rtnl_talk(nlh, &req->n, 0, 0, NULL);
261  objunlock(nlh);
262 
263  objunref(nlh);
264  objunref(req);
265 
266  return (ret);
267 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int create_tun ( const char *  ifname,
const unsigned char *  hwaddr,
int  flags 
)

Create a tunnel device.

Parameters
ifnameInterface name to create..
hwaddrHardware address to assign (optionally).
flagsFlags to set device properties.
Returns
Tunnel FD or -1 on error.

Definition at line 496 of file interface.c.

References get_iface_index(), set_interface_addr(), and set_interface_flags().

496  {
497  struct ifreq ifr;
498  int fd, ifindex;
499  char *tundev = "/dev/net/tun";
500 
501  /* open the tun/tap clone dev*/
502  if ((fd = open(tundev, O_RDWR)) < 0) {
503  return (-1);
504  }
505 
506  /* configure the device*/
507  memset(&ifr, 0, sizeof(ifr));
508  ifr.ifr_flags = flags;
509  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
510  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0 ) {
511  perror("ioctl(TUNSETIFF) failed\n");
512  close(fd);
513  return (-1);
514  }
515 
516  if (!(ifindex = get_iface_index(ifname))) {
517  return (-1);
518  }
519 
520  /* set the MAC address*/
521  if (hwaddr) {
522  set_interface_addr(ifindex, hwaddr);
523  }
524 
525  /*set the network dev up*/
526  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING | IFF_MULTICAST | IFF_BROADCAST, 0);
527 
528  return (fd);
529 }
int set_interface_addr(int ifindex, const unsigned char *hwaddr)
Set interface MAC addr.
Definition: interface.c:388
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int set_interface_flags(int ifindex, int set, int clear)
Alter interface flags.
Definition: interface.c:348
int delete_kernmac ( char *  ifname)

Delete Kernel MAC VLAN.

Parameters
ifnameInterface to delete.
Returns
-1 on error.

Definition at line 272 of file interface.c.

272  {
273 
274  return (delete_interface(ifname));
275 }
int delete_kernvlan ( char *  ifname,
int  vid 
)

Delete a VLAN.

Parameters
ifnameInterface we deleting vlan from.
vidVLAN id to delete.
Returns
-1 on error.

Definition at line 201 of file interface.c.

201  {
202  char iface[IFNAMSIZ+1];
203 
204  /*check ifname grab a ref to nlh or open it*/
205  snprintf(iface, IFNAMSIZ, "%s.%i", ifname, vid);
206  return (delete_interface(iface));
207 }
int get_iface_index ( const char *  ifname)

Get the netlink interface for a named interface.

Parameters
ifnameInterface name.
Returns
Index of the interface.

Definition at line 139 of file interface.c.

References objlock(), objref(), objunlock(), and objunref().

Referenced by create_kernmac(), create_kernvlan(), create_tun(), ifdown(), ifhwaddr(), ifrename(), ifup(), interface_bind(), mcast_socket(), and set_interface_ipaddr().

139  {
140  int ifindex;
141 
142  if (!objref(nlh) && !(nlh = nlhandle(0))) {
143  return (0);
144  }
145 
146  objlock(nlh);
147  ll_init_map(nlh, 1);
148  objunlock(nlh);
149 
150  ifindex = ll_name_to_index(ifname);
151 
152  objunref(nlh);
153  return (ifindex);
154 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
const char* get_ifipaddr ( const char *  iface,
int  family 
)

Find best IP adress for a interface.

Todo:
WIN32 Support
Parameters
ifaceInterface name.
familyPF_INET or PF_INET6.
Returns
Best matching IP address for the interface.

Definition at line 783 of file interface.c.

References score_ipv4(), score_ipv6(), and strlenzero().

Referenced by mcast_socket().

783  {
784  struct ifaddrs *ifaddr, *ifa;
785  struct sockaddr_in *ipv4addr;
786  int score = 0, nscore, iflen;
787  uint32_t subnet = 0, match;
788  char host[NI_MAXHOST] = "", tmp[NI_MAXHOST];
789 
790  if (!iface || getifaddrs(&ifaddr) == -1) {
791  return NULL;
792  }
793 
794  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
795  iflen = strlen(iface);
796  if ((ifa->ifa_addr == NULL) || strncmp(ifa->ifa_name, iface, iflen) || (ifa->ifa_addr->sa_family != family)) {
797  continue;
798  }
799 
800  /* Match aliases not vlans*/
801  if ((strlen(ifa->ifa_name) > iflen) && (ifa->ifa_name[iflen] != ':')) {
802  continue;
803  }
804 
805  switch (ifa->ifa_addr->sa_family) {
806  case AF_INET:
807  /* Find best ip address for a interface lowest priority is given to zeroconf then reserved ip's
808  * finally find hte ip with shortest subnet bits.*/
809  ipv4addr = (struct sockaddr_in*)ifa->ifa_netmask;
810  match = ntohl(~ipv4addr->sin_addr.s_addr);
811 
812  nscore = score_ipv4((struct sockaddr_in*)ifa->ifa_addr, tmp, NI_MAXHOST);
813 
814  /* match score and subnet*/
815  if ((nscore > score) || ((nscore == score) && (match > subnet))) {
816  score = nscore;
817  subnet = match;
818  strncpy(host, tmp, NI_MAXHOST);
819  }
820  break;
821  case AF_INET6:
822  nscore = score_ipv6((struct sockaddr_in6*)ifa->ifa_addr, tmp, NI_MAXHOST);
823 
824  if (nscore > score) {
825  score = nscore;
826  strncpy(host, tmp, NI_MAXHOST);
827  }
828  break;
829  }
830  }
831  freeifaddrs(ifaddr);
832  return (strlenzero(host)) ? NULL : strdup(host);
833 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int score_ipv6(struct sockaddr_in6 *sa6, char *ipaddr, int iplen)
Return a score for a IPv6 addrress.
Definition: interface.c:746
int score_ipv4(struct sockaddr_in *sa4, char *ipaddr, int iplen)
Return a score for a IPv4 addrress.
Definition: interface.c:718
int ifdown ( const char *  ifname,
int  flags 
)

Set interface down.

Parameters
ifnameInterface name.
flagsAdditional flags to clear.
Returns
-1 on error 0 on success.

Definition at line 535 of file interface.c.

References get_iface_index(), and set_interface_flags().

Referenced by ifrename().

535  {
536  int ifindex;
537 
538  /*down the device*/
539  if (!(ifindex = get_iface_index(ifname))) {
540  return (-1);
541  }
542 
543  /*set the network dev up*/
544  set_interface_flags(ifindex, 0, IFF_UP | IFF_RUNNING | flags);
545 
546  return (0);
547 }
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int set_interface_flags(int ifindex, int set, int clear)
Alter interface flags.
Definition: interface.c:348
int ifhwaddr ( const char *  ifname,
unsigned char *  hwaddr 
)

Get MAC addr for interface.

Parameters
ifnameInterface name
hwaddrBuffer to place MAC in char[ETH_ALEN]
Returns
0 on success.

Definition at line 588 of file interface.c.

References get_iface_index(), objref(), objunref(), and strlenzero().

Referenced by get_ip6_addrprefix().

588  {
589  int ifindex;
590 
591  if (!hwaddr || strlenzero(ifname) || (strlen(ifname) > IFNAMSIZ) ||
592  (!objref(nlh) && !(nlh = nlhandle(0)))) {
593  return (-1);
594  }
595 
596  /*set the index of base interface*/
597  if (!(ifindex = get_iface_index(ifname))) {
598  objunref(nlh);
599  return (-1);
600  }
601 
602  ll_index_to_addr(ifindex, hwaddr, ETH_ALEN);
603  objunref(nlh);
604  return (0);
605 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ifrename ( const char *  oldname,
const char *  newname 
)

Rename interface helper.

Parameters
oldnameOriginal name.
newnameNew name.
Returns
0 on success.

Definition at line 571 of file interface.c.

References get_iface_index(), ifdown(), and set_interface_name().

571  {
572  int ifindex;
573 
574  ifdown(oldname, 0);
575 
576  if (!(ifindex = get_iface_index(oldname))) {
577  return (-1);
578  }
579  set_interface_name(ifindex, newname);
580 
581  return (0);
582 }
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int set_interface_name(int ifindex, const char *name)
Rename interface.
Definition: interface.c:421
int ifdown(const char *ifname, int flags)
Set interface down.
Definition: interface.c:535
int ifup ( const char *  ifname,
int  flags 
)

Set interface up.

Parameters
ifnameInterface name.
flagsAdditional flags to set.
Returns
-1 on error 0 on success.

Definition at line 553 of file interface.c.

References get_iface_index(), and set_interface_flags().

553  {
554  int ifindex;
555 
556  /*down the device*/
557  if (!(ifindex = get_iface_index(ifname))) {
558  return (-1);
559  }
560 
561  /*set the network dev up*/
562  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING | flags, 0);
563 
564  return (0);
565 }
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int set_interface_flags(int ifindex, int set, int clear)
Alter interface flags.
Definition: interface.c:348
int interface_bind ( char *  iface,
int  protocol 
)

Bind to device fd may be a existing socket.

Parameters
ifaceInterface to bind too.
protocolProtocol to use.
Returns
-1 on error.

Definition at line 453 of file interface.c.

References get_iface_index(), and set_interface_flags().

453  {
454  struct sockaddr_ll sll;
455  int proto = htons(protocol);
456  int fd, ifindex;
457 
458  /*set the network dev up*/
459  if (!(ifindex = get_iface_index(iface))) {
460  return (-1);
461  }
462  set_interface_flags(ifindex, IFF_UP | IFF_RUNNING, 0);
463 
464  /* open network raw socket */
465  if ((fd = socket(PF_PACKET, SOCK_RAW, proto)) < 0) {
466  return (-1);
467  }
468 
469  /*bind to the interface*/
470  memset(&sll, 0, sizeof(sll));
471  sll.sll_family = PF_PACKET;
472  sll.sll_protocol = proto;
473  sll.sll_ifindex = ifindex;
474  if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
475  perror("bind failed");
476  close(fd);
477  return (-1);
478  }
479 
480  return (fd);
481 }
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
int set_interface_flags(int ifindex, int set, int clear)
Alter interface flags.
Definition: interface.c:348
void randhwaddr ( unsigned char *  addr)

create random MAC address

Parameters
addrBuffer char[ETH_ALEN] filled with the new address.

Definition at line 485 of file interface.c.

References genrand().

Referenced by create_kernmac().

485  {
486  genrand(addr, ETH_ALEN);
487  addr [0] &= 0xfe; /* clear multicast bit */
488  addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
489 }
int genrand(void *buf, int len)
Generate random sequence.
Definition: util.c:82
int set_interface_addr ( int  ifindex,
const unsigned char *  hwaddr 
)

Set interface MAC addr.

Parameters
ifindexInterface index.
hwaddrMAC address to set.
Returns
-1 on error.

Definition at line 388 of file interface.c.

References iplink_req::i, iplink_req::n, objalloc(), objlock(), objref(), objunlock(), and objunref().

Referenced by create_tun().

388  {
389  struct iplink_req *req;
390 
391  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
392  return (-1);
393  }
394 
395  if (!(req = objalloc(sizeof(*req), NULL))) {
396  objunref(nlh);
397  return (-1);
398  }
399 
400  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
401  req->n.nlmsg_type = RTM_NEWLINK;
402  req->n.nlmsg_flags = NLM_F_REQUEST;
403  req->i.ifi_index = ifindex;
404 
405  /*config base/dev/mac*/
406  addattr_l(&req->n, sizeof(*req), IFLA_ADDRESS, hwaddr, ETH_ALEN);
407 
408  objlock(nlh);
409  rtnl_talk(nlh, &req->n, 0, 0, NULL);
410  objunlock(nlh);
411 
412  objunref(nlh);
413  objunref(req);
414  return (0);
415 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int set_interface_flags ( int  ifindex,
int  set,
int  clear 
)

Alter interface flags.

Parameters
ifindexInterface index.
setFlags to set.
clearFlags to clear.
Returns
-1 on error.

Definition at line 348 of file interface.c.

References iplink_req::i, iplink_req::n, objalloc(), objlock(), objref(), objunlock(), and objunref().

Referenced by create_tun(), ifdown(), ifup(), and interface_bind().

348  {
349  struct iplink_req *req;
350  int flags;
351 
352  if (!objref(nlh) && !(nlh = nlhandle(0))) {
353  return (-1);
354  }
355 
356  flags = ll_index_to_flags(ifindex);
357 
358  flags |= set;
359  flags &= ~(clear);
360 
361  if (!(req = objalloc(sizeof(*req), NULL))) {
362  objunref(nlh);
363  return (-1);
364  }
365 
366  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
367  req->n.nlmsg_type = RTM_NEWLINK;
368  req->n.nlmsg_flags = NLM_F_REQUEST;
369 
370  /*config base/dev/mac*/
371  req->i.ifi_index = ifindex;
372  req->i.ifi_flags = flags;
373  req->i.ifi_change = set | clear;
374 
375  objlock(nlh);
376  rtnl_talk(nlh, &req->n, 0, 0, NULL);
377  objunlock(nlh);
378 
379  objunref(nlh);
380  objunref(req);
381  return (0);
382 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int set_interface_ipaddr ( char *  ifname,
char *  ipaddr 
)

Set IP addr on interface.

Parameters
ifnameInterface to assign IP to
ipaddrIP Addr to assign.
Returns
-1 on error.

Definition at line 611 of file interface.c.

References get_iface_index(), ipaddr_req::i, ipaddr_req::n, objalloc(), objlock(), objref(), objunlock(), and objunref().

611  {
612  struct ipaddr_req *req;
613  inet_prefix lcl;
614  int ifindex, bcast;
615 
616  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
617  return (-1);
618  }
619 
620  if (!(req = objalloc(sizeof(*req), NULL))) {
621  objunref(nlh);
622  return (-1);
623  }
624 
625  /*set the index of base interface*/
626  if (!(ifindex = get_iface_index(ifname))) {
627  objunref(nlh);
628  return (-1);
629  }
630 
631  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
632  req->n.nlmsg_type = RTM_NEWADDR;
633  req->n.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE;
634 
635  req->i.ifa_scope = RT_SCOPE_HOST;
636  req->i.ifa_index = ifindex;
637 
638  get_prefix(&lcl, ipaddr, AF_UNSPEC);
639  req->i.ifa_family = lcl.family;
640  req->i.ifa_prefixlen = lcl.bitlen;
641 
642  addattr_l(&req->n, sizeof(*req), IFA_LOCAL, &lcl.data, lcl.bytelen);
643  addattr_l(&req->n, sizeof(*req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
644  if (lcl.family == AF_INET) {
645  bcast = htonl((1 << (32 - lcl.bitlen)) - 1);
646  addattr32(&req->n, sizeof(*req), IFA_BROADCAST, lcl.data[0] | bcast);
647  }
648 
649  objlock(nlh);
650  rtnl_talk(nlh, &req->n, 0, 0, NULL);
651  objunlock(nlh);
652 
653  objunref(nlh);
654  objunref(req);
655  return (0);
656 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int get_iface_index(const char *ifname)
Get the netlink interface for a named interface.
Definition: interface.c:139
struct nlmsghdr n
Netlink message header.
Definition: interface.c:97
IP Netlink IP addr request.
Definition: interface.c:95
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct ifaddrmsg i
Interface addr message.
Definition: interface.c:99
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int set_interface_name ( int  ifindex,
const char *  name 
)

Rename interface.

Parameters
ifindexInterface index.
nameNew interface name.
Returns
-1 on error.

Definition at line 421 of file interface.c.

References iplink_req::i, iplink_req::n, objalloc(), objlock(), objref(), objunlock(), and objunref().

Referenced by ifrename().

421  {
422  struct iplink_req *req;
423 
424  if ((!objref(nlh) && !(nlh = nlhandle(0)))) {
425  return (-1);
426  }
427 
428  if (!(req = objalloc(sizeof(*req), NULL))) {
429  objunref(nlh);
430  return (-1);
431  }
432 
433  req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
434  req->n.nlmsg_type = RTM_NEWLINK;
435  req->n.nlmsg_flags = NLM_F_REQUEST;
436  req->i.ifi_index = ifindex;
437 
438  addattr_l(&req->n, sizeof(*req), IFLA_IFNAME, name, strlen((char *)name));
439 
440  objlock(nlh);
441  rtnl_talk(nlh, &req->n, 0, 0, NULL);
442  objunlock(nlh);
443 
444  objunref(nlh);
445  objunref(req);
446  return (0);
447 }
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184