create_table(): Register a new table for the inet family
| 458 | |
| 459 | // create_table(): Register a new table for the inet family |
| 460 | void create_table(int sock, const char *name) { |
| 461 | struct msghdr msg; |
| 462 | struct sockaddr_nl dest_snl; |
| 463 | struct iovec iov[3]; |
| 464 | struct nlmsghdr *nlh_batch_begin; |
| 465 | struct nlmsghdr *nlh; |
| 466 | struct nlmsghdr *nlh_batch_end; |
| 467 | struct nlattr *attr; |
| 468 | struct nfgenmsg *nfm; |
| 469 | |
| 470 | // Destination preparation |
| 471 | memset(&dest_snl, 0, sizeof(dest_snl)); |
| 472 | dest_snl.nl_family = AF_NETLINK; |
| 473 | memset(&msg, 0, sizeof(msg)); |
| 474 | |
| 475 | // 1. Netlink batch_begin message preparation |
| 476 | nlh_batch_begin = get_batch_begin_nlmsg(); |
| 477 | |
| 478 | // 2. Netlink table message preparation |
| 479 | nlh = (struct nlmsghdr *)malloc(TABLEMSG_SIZE); |
| 480 | if (!nlh) |
| 481 | error("malloc"); |
| 482 | |
| 483 | memset(nlh, 0, TABLEMSG_SIZE); |
| 484 | nlh->nlmsg_len = TABLEMSG_SIZE; |
| 485 | nlh->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWTABLE; |
| 486 | nlh->nlmsg_pid = getpid(); |
| 487 | nlh->nlmsg_flags = NLM_F_REQUEST; |
| 488 | nlh->nlmsg_seq = 0; |
| 489 | |
| 490 | nfm = NLMSG_DATA(nlh); |
| 491 | nfm->nfgen_family = NFPROTO_INET; |
| 492 | |
| 493 | // Prepare associated attribute |
| 494 | attr = (void *)nlh + NLMSG_SPACE(sizeof(struct nfgenmsg)); |
| 495 | set_str8_attr(attr, NFTA_TABLE_NAME, name); |
| 496 | |
| 497 | // 3. Netlink batch_end message preparation |
| 498 | nlh_batch_end = get_batch_end_nlmsg(); |
| 499 | |
| 500 | // IOV preparation |
| 501 | memset(iov, 0, sizeof(struct iovec) * 3); |
| 502 | iov[0].iov_base = (void *)nlh_batch_begin; |
| 503 | iov[0].iov_len = nlh_batch_begin->nlmsg_len; |
| 504 | iov[1].iov_base = (void *)nlh; |
| 505 | iov[1].iov_len = nlh->nlmsg_len; |
| 506 | iov[2].iov_base = (void *)nlh_batch_end; |
| 507 | iov[2].iov_len = nlh_batch_end->nlmsg_len; |
| 508 | |
| 509 | // Message header preparation |
| 510 | msg.msg_name = (void *)&dest_snl; |
| 511 | msg.msg_namelen = sizeof(struct sockaddr_nl); |
| 512 | msg.msg_iov = iov; |
| 513 | msg.msg_iovlen = 3; |
| 514 | |
| 515 | sendmsg(sock, &msg, 0); |
| 516 | |
| 517 | // Free used structures |
no test coverage detected