/build/buildd/libnl-1.0~pre6/lib/route/rule.c

00001 /*
00002  * lib/route/rule.c          Routing Rules
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup rtnl
00014  * @defgroup rule Routing Rules
00015  * @brief
00016  * @{
00017  */
00018 
00019 #include <netlink-local.h>
00020 #include <netlink/netlink.h>
00021 #include <netlink/utils.h>
00022 #include <netlink/route/rtnl.h>
00023 #include <netlink/route/rule.h>
00024 #include <inttypes.h>
00025 
00026 /** @cond SKIP */
00027 #define RULE_ATTR_FAMILY  0x0001
00028 #define RULE_ATTR_PRIO    0x0002
00029 #define RULE_ATTR_FWMARK  0x0004
00030 #define RULE_ATTR_IIF     0x0008
00031 #define RULE_ATTR_REALMS  0x0010
00032 #define RULE_ATTR_SRC     0x0020
00033 #define RULE_ATTR_DST     0x0040
00034 #define RULE_ATTR_DSFIELD 0x0080
00035 #define RULE_ATTR_TABLE   0x0100
00036 #define RULE_ATTR_TYPE    0x0200
00037 #define RULE_ATTR_SRC_LEN 0x0400
00038 #define RULE_ATTR_DST_LEN 0x0800
00039 #define RULE_ATTR_SRCMAP  0x1000
00040 
00041 static struct nl_cache_ops rtnl_rule_ops;
00042 /** @endcond */
00043 
00044 static void rule_free_data(struct nl_object *c)
00045 {
00046         struct rtnl_rule *rule = nl_object_priv(c);
00047 
00048         if (!rule)
00049                 return;
00050 
00051         nl_addr_put(rule->r_src);
00052         nl_addr_put(rule->r_dst);
00053 }
00054 
00055 static struct nla_policy rule_policy[RTA_MAX+1] = {
00056         [RTA_PRIORITY]  = { .type = NLA_U32 },
00057         [RTA_FLOW]      = { .type = NLA_U32 },
00058         [RTA_PROTOINFO] = { .type = NLA_U32 },
00059         [RTA_IIF]       = { .type = NLA_STRING,
00060                             .maxlen = IFNAMSIZ, },
00061 };
00062 
00063 static int rule_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *n,
00064                            void *arg)
00065 {
00066         struct rtnl_rule *rule;
00067         struct rtmsg *r;
00068         struct nlattr *tb[RTA_MAX+1];
00069         struct nl_parser_param *pp = arg;
00070         int err = 1;
00071 
00072         rule = rtnl_rule_alloc();
00073         if (!rule) {
00074                 err = nl_errno(ENOMEM);
00075                 goto errout;
00076         }
00077 
00078         rule->ce_msgtype = n->nlmsg_type;
00079         r = nlmsg_data(n);
00080 
00081         err = nlmsg_parse(n, sizeof(*r), tb, RTA_MAX, rule_policy);
00082         if (err < 0)
00083                 goto errout;
00084 
00085         rule->r_family = r->rtm_family;
00086         rule->r_type = r->rtm_type;
00087         rule->r_dsfield = r->rtm_tos;
00088         rule->r_src_len = r->rtm_src_len;
00089         rule->r_dst_len = r->rtm_dst_len;
00090         rule->r_table = r->rtm_table;
00091         rule->r_mask = (RULE_ATTR_FAMILY | RULE_ATTR_TYPE | RULE_ATTR_DSFIELD |
00092                         RULE_ATTR_SRC_LEN | RULE_ATTR_DST_LEN | RULE_ATTR_TYPE);
00093 
00094         if (tb[RTA_PRIORITY]) {
00095                 rule->r_prio = nla_get_u32(tb[RTA_PRIORITY]);
00096                 rule->r_mask |= RULE_ATTR_PRIO;
00097         }
00098 
00099         if (tb[RTA_SRC]) {
00100                 rule->r_src = nla_get_addr(tb[RTA_SRC], r->rtm_family);
00101                 if (!rule->r_src) {
00102                         err = nl_errno(ENOMEM);
00103                         goto errout;
00104                 }
00105                 nl_addr_set_prefixlen(rule->r_src, r->rtm_src_len);
00106                 rule->r_mask |= RULE_ATTR_SRC;
00107         }
00108 
00109         if (tb[RTA_DST]) {
00110                 rule->r_dst = nla_get_addr(tb[RTA_DST], r->rtm_family);
00111                 if (!rule->r_dst) {
00112                         err = nl_errno(ENOMEM);
00113                         goto errout;
00114                 }
00115                 nl_addr_set_prefixlen(rule->r_dst, r->rtm_dst_len);
00116                 rule->r_mask |= RULE_ATTR_DST;
00117         }
00118 
00119         if (tb[RTA_PROTOINFO]) {
00120                 rule->r_fwmark = nla_get_u32(tb[RTA_PROTOINFO]);
00121                 rule->r_mask |= RULE_ATTR_FWMARK;
00122         }
00123 
00124         if (tb[RTA_IIF]) {
00125                 nla_strlcpy(rule->r_iif, tb[RTA_IIF], IFNAMSIZ);
00126                 rule->r_mask |= RULE_ATTR_IIF;
00127         }
00128 
00129         if (tb[RTA_FLOW]) {
00130                 rule->r_realms = nla_get_u32(tb[RTA_FLOW]);
00131                 rule->r_mask |= RULE_ATTR_REALMS;
00132         }
00133 
00134         if (tb[RTA_GATEWAY]) {
00135                 rule->r_srcmap = nla_get_addr(tb[RTA_GATEWAY], r->rtm_family);
00136                 if (!rule->r_srcmap) {
00137                         err = nl_errno(ENOMEM);
00138                         goto errout;
00139                 }
00140                 rule->r_mask |= RULE_ATTR_SRCMAP;
00141         }
00142 
00143         err = pp->pp_cb((struct nl_object *) rule, pp);
00144         if (err < 0)
00145                 goto errout;
00146 
00147         return P_ACCEPT;
00148 
00149 errout:
00150         rtnl_rule_put(rule);
00151         return err;
00152 }
00153 
00154 static int rule_request_update(struct nl_cache *c, struct nl_handle *h)
00155 {
00156         return nl_rtgen_request(h, RTM_GETRULE, AF_UNSPEC, NLM_F_DUMP);
00157 }
00158 
00159 static int rule_dump_brief(struct nl_object *o, struct nl_dump_params *p)
00160 {
00161         struct rtnl_rule *r = (struct rtnl_rule *) o;
00162         char buf[128];
00163 
00164         if (r->r_mask & RULE_ATTR_PRIO)
00165                 dp_dump(p, "%d:\t", r->r_prio);
00166         else
00167                 dp_dump(p, "0:\t");
00168 
00169         if (r->r_mask & RULE_ATTR_SRC)
00170                 dp_dump(p, "from %s ",
00171                         nl_addr2str(r->r_src, buf, sizeof(buf)));
00172         else if (r->r_mask & RULE_ATTR_SRC_LEN && r->r_src_len)
00173                 dp_dump(p, "from 0/%d ", r->r_src_len);
00174 
00175         if (r->r_mask & RULE_ATTR_DST)
00176                 dp_dump(p, "to %s ",
00177                         nl_addr2str(r->r_dst, buf, sizeof(buf)));
00178         else if (r->r_mask & RULE_ATTR_DST_LEN && r->r_dst_len)
00179                 dp_dump(p, "to 0/%d ", r->r_dst_len);
00180 
00181         if (r->r_mask & RULE_ATTR_DSFIELD && r->r_dsfield)
00182                 dp_dump(p, "tos %d ", r->r_dsfield);
00183 
00184         if (r->r_mask & RULE_ATTR_FWMARK)
00185                 dp_dump(p, "fwmark %" PRIx64 , r->r_fwmark);
00186 
00187         if (r->r_mask & RULE_ATTR_IIF)
00188                 dp_dump(p, "iif %s ", r->r_iif);
00189 
00190         if (r->r_mask & RULE_ATTR_TABLE)
00191                 dp_dump(p, "lookup %s ",
00192                         rtnl_route_table2str(r->r_table, buf, sizeof(buf)));
00193 
00194         if (r->r_mask & RULE_ATTR_REALMS)
00195                 dp_dump(p, "realms %s ",
00196                         rtnl_realms2str(r->r_realms, buf, sizeof(buf)));
00197 
00198         dp_dump(p, "action %s\n",
00199                 nl_rtntype2str(r->r_type, buf, sizeof(buf)));
00200 
00201         return 1;
00202 }
00203 
00204 static int rule_dump_full(struct nl_object *obj, struct nl_dump_params *p)
00205 {
00206         struct rtnl_rule *rule = (struct rtnl_rule *) obj;
00207         char buf[128];
00208         int line;
00209 
00210         line = rule_dump_brief(obj, p);
00211 
00212         dp_dump_line(p, line++, "  family %s",
00213                      nl_af2str(rule->r_family, buf, sizeof(buf)));
00214 
00215         if (rule->r_mask & RULE_ATTR_SRCMAP)
00216                 dp_dump(p, " srcmap %s",
00217                         nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
00218 
00219         dp_dump(p, "\n");
00220 
00221         return line;
00222 }
00223 
00224 static int rule_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
00225 {
00226         return rule_dump_full(obj, p);
00227 }
00228 
00229 static int rule_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
00230 {
00231         struct rtnl_rule *rule = (struct rtnl_rule *) obj;
00232         char buf[128];
00233         int line = 0;
00234         
00235         dp_dump_line(p, line++, "<rule>\n");
00236 
00237         dp_dump_line(p, line++, "  <priority>%u</priority>\n",
00238                      rule->r_prio);
00239         dp_dump_line(p, line++, "  <family>%s</family>\n",
00240                      nl_af2str(rule->r_family, buf, sizeof(buf)));
00241 
00242         if (rule->r_mask & RULE_ATTR_DST)
00243                 dp_dump_line(p, line++, "  <dst>%s</dst>\n",
00244                              nl_addr2str(rule->r_dst, buf, sizeof(buf)));
00245 
00246         if (rule->r_mask & RULE_ATTR_DST_LEN)
00247                 dp_dump_line(p, line++, "  <dstlen>%u</dstlen>\n",
00248                              rule->r_dst_len);
00249 
00250         if (rule->r_mask & RULE_ATTR_SRC)
00251                 dp_dump_line(p, line++, "  <src>%s</src>\n",
00252                              nl_addr2str(rule->r_src, buf, sizeof(buf)));
00253 
00254         if (rule->r_mask & RULE_ATTR_SRC_LEN)
00255                 dp_dump_line(p, line++, "  <srclen>%u</srclen>\n",
00256                              rule->r_src_len);
00257 
00258         if (rule->r_mask & RULE_ATTR_IIF)
00259                 dp_dump_line(p, line++, "  <iif>%s</iif>\n", rule->r_iif);
00260 
00261         if (rule->r_mask & RULE_ATTR_TABLE)
00262                 dp_dump_line(p, line++, "  <table>%u</table>\n",
00263                              rule->r_table);
00264 
00265         if (rule->r_mask & RULE_ATTR_REALMS)
00266                 dp_dump_line(p, line++, "  <realms>%u</realms>\n",
00267                              rule->r_realms);
00268 
00269         if (rule->r_mask & RULE_ATTR_FWMARK)
00270                 dp_dump_line(p, line++, "  <fwmark>%" PRIx64 "</fwmark>\n",
00271                              rule->r_fwmark);
00272 
00273         if (rule->r_mask & RULE_ATTR_DSFIELD)
00274                 dp_dump_line(p, line++, "  <dsfield>%u</dsfield>\n",
00275                              rule->r_dsfield);
00276 
00277         if (rule->r_mask & RULE_ATTR_TYPE)
00278                 dp_dump_line(p, line++, "<type>%s</type>\n",
00279                              nl_rtntype2str(rule->r_type, buf, sizeof(buf)));
00280 
00281         if (rule->r_mask & RULE_ATTR_SRCMAP)
00282                 dp_dump_line(p, line++, "<srcmap>%s</srcmap>\n",
00283                              nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
00284 
00285         dp_dump_line(p, line++, "</rule>\n");
00286 
00287         return line;
00288 }
00289 
00290 static int rule_dump_env(struct nl_object *obj, struct nl_dump_params *p)
00291 {
00292         struct rtnl_rule *rule = (struct rtnl_rule *) obj;
00293         char buf[128];
00294         int line = 0;
00295 
00296         dp_dump_line(p, line++, "RULE_PRIORITY=%u\n",
00297                      rule->r_prio);
00298         dp_dump_line(p, line++, "RULE_FAMILY=%s\n",
00299                      nl_af2str(rule->r_family, buf, sizeof(buf)));
00300 
00301         if (rule->r_mask & RULE_ATTR_DST)
00302                 dp_dump_line(p, line++, "RULE_DST=%s\n",
00303                              nl_addr2str(rule->r_dst, buf, sizeof(buf)));
00304 
00305         if (rule->r_mask & RULE_ATTR_DST_LEN)
00306                 dp_dump_line(p, line++, "RULE_DSTLEN=%u\n",
00307                              rule->r_dst_len);
00308 
00309         if (rule->r_mask & RULE_ATTR_SRC)
00310                 dp_dump_line(p, line++, "RULE_SRC=%s\n",
00311                              nl_addr2str(rule->r_src, buf, sizeof(buf)));
00312 
00313         if (rule->r_mask & RULE_ATTR_SRC_LEN)
00314                 dp_dump_line(p, line++, "RULE_SRCLEN=%u\n",
00315                              rule->r_src_len);
00316 
00317         if (rule->r_mask & RULE_ATTR_IIF)
00318                 dp_dump_line(p, line++, "RULE_IIF=%s\n", rule->r_iif);
00319 
00320         if (rule->r_mask & RULE_ATTR_TABLE)
00321                 dp_dump_line(p, line++, "RULE_TABLE=%u\n",
00322                              rule->r_table);
00323 
00324         if (rule->r_mask & RULE_ATTR_REALMS)
00325                 dp_dump_line(p, line++, "RULE_REALM=%u\n",
00326                              rule->r_realms);
00327 
00328         if (rule->r_mask & RULE_ATTR_FWMARK)
00329                 dp_dump_line(p, line++, "RULE_FWMARK=0x%" PRIx64 "\n",
00330                              rule->r_fwmark);
00331 
00332         if (rule->r_mask & RULE_ATTR_DSFIELD)
00333                 dp_dump_line(p, line++, "RULE_DSFIELD=%u\n",
00334                              rule->r_dsfield);
00335 
00336         if (rule->r_mask & RULE_ATTR_TYPE)
00337                 dp_dump_line(p, line++, "RULE_TYPE=%s\n",
00338                              nl_rtntype2str(rule->r_type, buf, sizeof(buf)));
00339 
00340         if (rule->r_mask & RULE_ATTR_SRCMAP)
00341                 dp_dump_line(p, line++, "RULE_SRCMAP=%s\n",
00342                              nl_addr2str(rule->r_srcmap, buf, sizeof(buf)));
00343 
00344         return line;
00345 }
00346 
00347 
00348 static int rule_filter(struct nl_object *obj, struct nl_object *filter)
00349 {
00350         struct rtnl_rule *o = (struct rtnl_rule *) obj;
00351         struct rtnl_rule *f = (struct rtnl_rule *) filter;
00352 
00353 #define REQ(F) (f->r_mask & RULE_ATTR_##F)
00354 #define AVAIL(F) (o->r_mask & RULE_ATTR_##F)
00355 #define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
00356 #define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
00357         if (_C(FAMILY,  r_family)                                       ||
00358             _C(TABLE,   r_table)                                        ||
00359             _C(REALMS,  r_realms)                                       ||
00360             _C(DSFIELD, r_dsfield)                                      ||
00361             _C(TYPE,    r_type)                                         ||
00362             _C(PRIO,    r_prio)                                         ||
00363             _C(FWMARK,  r_fwmark)                                       ||
00364             _C(SRC_LEN, r_src_len)                                      ||
00365             _C(DST_LEN, r_dst_len)                                      ||
00366             _O(SRC,     nl_addr_cmp(o->r_src, f->r_src))                ||
00367             _O(DST,     nl_addr_cmp(o->r_dst, f->r_dst))                ||
00368             _O(IIF,     strcmp(o->r_iif, f->r_iif)))
00369                 return 0;
00370 #undef REQ
00371 #undef AVAIL
00372 #undef _O
00373 #undef _C
00374 
00375         return 1;
00376 }
00377 
00378 /**
00379  * @name Routing Rule Object Allocation/Freeage
00380  * @{
00381  */
00382 
00383 /**
00384  * Allocate a new rule object
00385  * @return New rule object
00386  */
00387 struct rtnl_rule *rtnl_rule_alloc(void)
00388 {
00389         return (struct rtnl_rule *) nl_object_alloc_from_ops(&rtnl_rule_ops);
00390 }
00391 
00392 /**
00393  * Give back reference on routing rule object.
00394  * @arg rule            Routing rule object to be given back.
00395  *
00396  * Decrements the reference counter and frees the object if the
00397  * last reference has been released.
00398  */
00399 void rtnl_rule_put(struct rtnl_rule *rule)
00400 {
00401         nl_object_put((struct nl_object *) rule);
00402 }
00403 /**
00404  * Free routing rule object.
00405  * @arg rule            Routing rule object to be freed.
00406  *
00407  * @note Always use rtnl_rule_put() unless you're absolutely sure
00408  *       that no other user may have a reference on this object.
00409  */
00410 void rtnl_rule_free(struct rtnl_rule *rule)
00411 {
00412         nl_object_free((struct nl_object *) rule);
00413 }
00414 
00415 /** @} */
00416 
00417 /**
00418  * @name Routing Rule Cache Management
00419  * @{
00420  */
00421 
00422 /**
00423  * Build a rule cache including all rules of the specified family currently configured in the kernel.
00424  * @arg handle          netlink handle
00425  * @arg family          address family
00426  *
00427  * Allocates a new rule cache, initializes it properly and updates it
00428  * to include all rules of the specified address family currently
00429  * configured in the kernel.
00430  *
00431  * @note The caller is responsible for destroying and freeing the
00432  *       cache after using it. (nl_cache_destroy_and_free())
00433  * @return The new cache or NULL if an error occured.
00434  */
00435 struct nl_cache * rtnl_rule_alloc_cache_by_family(struct nl_handle *handle,
00436                                                   int family)
00437 {
00438         struct nl_cache * cache = nl_cache_alloc_from_ops(&rtnl_rule_ops);
00439 
00440         if (cache == NULL)
00441                 return NULL;
00442 
00443         /* XXX RULE_CACHE_FAMILY(cache) = family; */
00444 
00445         if (nl_cache_update(handle, cache) < 0) {
00446                 free(cache);
00447                 return NULL;
00448         }
00449 
00450         return cache;
00451 }
00452 
00453 /**
00454  * Build a rule cache including all rules currently configured in the kernel.
00455  * @arg handle          netlink handle
00456  *
00457  * Allocates a new rule cache, initializes it properly and updates it
00458  * to include all rules currently configured in the kernel.
00459  *
00460  * @note The caller is responsible for destroying and freeing the
00461  *       cache after using it. (nl_cache_destroy_and_free())
00462  * @return The new cache or NULL if an error occured.
00463  */
00464 struct nl_cache * rtnl_rule_alloc_cache(struct nl_handle *handle)
00465 {
00466         return rtnl_rule_alloc_cache_by_family(handle, AF_UNSPEC);
00467 }
00468 
00469 /** @} */
00470 
00471 /**
00472  * @name Rule Addition
00473  * @{
00474  */
00475 
00476 static struct nl_msg *build_rule_msg(struct rtnl_rule *tmpl, int cmd, int flags)
00477 {
00478         struct nl_msg *msg;
00479         struct rtmsg rtm = {
00480                 .rtm_type = RTN_UNSPEC
00481         };
00482 
00483         if (cmd == RTM_NEWRULE)
00484                 rtm.rtm_type = RTN_UNICAST;
00485                 
00486         if (tmpl->r_mask & RULE_ATTR_FAMILY)
00487                 rtm.rtm_family = tmpl->r_family;
00488 
00489         if (tmpl->r_mask & RULE_ATTR_TABLE)
00490                 rtm.rtm_table = tmpl->r_table;
00491 
00492         if (tmpl->r_mask & RULE_ATTR_DSFIELD)
00493                 rtm.rtm_tos = tmpl->r_dsfield;
00494 
00495         if (tmpl->r_mask & RULE_ATTR_TYPE)
00496                 rtm.rtm_type = tmpl->r_type;
00497 
00498         if (tmpl->r_mask & RULE_ATTR_SRC_LEN)
00499                 rtm.rtm_src_len = tmpl->r_src_len;
00500 
00501         if (tmpl->r_mask & RULE_ATTR_DST_LEN)
00502                 rtm.rtm_dst_len = tmpl->r_dst_len;
00503 
00504         msg = nlmsg_build_simple(cmd, flags);
00505         if (!msg)
00506                 goto nla_put_failure;
00507 
00508         if (nlmsg_append(msg, &rtm, sizeof(rtm), 1) < 0)
00509                 goto nla_put_failure;
00510 
00511         if (tmpl->r_mask & RULE_ATTR_SRC)
00512                 NLA_PUT_ADDR(msg, RTA_SRC, tmpl->r_src);
00513 
00514         if (tmpl->r_mask & RULE_ATTR_DST)
00515                 NLA_PUT_ADDR(msg, RTA_DST, tmpl->r_dst);
00516 
00517         if (tmpl->r_mask & RULE_ATTR_PRIO)
00518                 NLA_PUT_U32(msg, RTA_PRIORITY, tmpl->r_prio);
00519 
00520         if (tmpl->r_mask & RULE_ATTR_FWMARK)
00521                 NLA_PUT_U32(msg, RTA_PROTOINFO, tmpl->r_fwmark);
00522 
00523         if (tmpl->r_mask & RULE_ATTR_REALMS)
00524                 NLA_PUT_U32(msg, RTA_FLOW, tmpl->r_realms);
00525 
00526         if (tmpl->r_mask & RULE_ATTR_IIF)
00527                 NLA_PUT_STRING(msg, RTA_IIF, tmpl->r_iif);
00528 
00529         return msg;
00530 
00531 nla_put_failure:
00532         nlmsg_free(msg);
00533         return NULL;
00534 }
00535 
00536 /**
00537  * Build netlink request message to add a new rule
00538  * @arg tmpl            template with data of new rule
00539  * @arg flags           additional netlink message flags
00540  *
00541  * Builds a new netlink message requesting a addition of a new
00542  * rule. The netlink message header isn't fully equipped with
00543  * all relevant fields and must thus be sent out via nl_send_auto_complete()
00544  * or supplemented as needed. \a tmpl must contain the attributes of the new
00545  * address set via \c rtnl_rule_set_* functions.
00546  * 
00547  * @return The netlink message
00548  */
00549 struct nl_msg *rtnl_rule_build_add_request(struct rtnl_rule *tmpl, int flags)
00550 {
00551         return build_rule_msg(tmpl, RTM_NEWRULE, NLM_F_CREATE | flags);
00552 }
00553 
00554 /**
00555  * Add a new rule
00556  * @arg handle          netlink handle
00557  * @arg tmpl            template with requested changes
00558  * @arg flags           additional netlink message flags
00559  *
00560  * Builds a netlink message by calling rtnl_rule_build_add_request(),
00561  * sends the request to the kernel and waits for the next ACK to be
00562  * received and thus blocks until the request has been fullfilled.
00563  *
00564  * @return 0 on sucess or a negative error if an error occured.
00565  */
00566 int rtnl_rule_add(struct nl_handle *handle, struct rtnl_rule *tmpl, int flags)
00567 {
00568         int err;
00569         struct nl_msg *msg;
00570         
00571         msg = rtnl_rule_build_add_request(tmpl, flags);
00572         if (!msg)
00573                 return nl_errno(ENOMEM);
00574 
00575         err = nl_send_auto_complete(handle, msg);
00576         if (err < 0)
00577                 return err;
00578 
00579         nlmsg_free(msg);
00580         return nl_wait_for_ack(handle);
00581 }
00582 
00583 /** @} */
00584 
00585 /**
00586  * @name Rule Deletion
00587  * @{
00588  */
00589 
00590 /**
00591  * Build a netlink request message to delete a rule
00592  * @arg rule            rule to delete
00593  * @arg flags           additional netlink message flags
00594  *
00595  * Builds a new netlink message requesting a deletion of a rule.
00596  * The netlink message header isn't fully equipped with all relevant
00597  * fields and must thus be sent out via nl_send_auto_complete()
00598  * or supplemented as needed. \a rule must point to an existing
00599  * address.
00600  *
00601  * @return The netlink message
00602  */
00603 struct nl_msg *rtnl_rule_build_delete_request(struct rtnl_rule *rule, int flags)
00604 {
00605         return build_rule_msg(rule, RTM_DELRULE, flags);
00606 }
00607 
00608 /**
00609  * Delete a rule
00610  * @arg handle          netlink handle
00611  * @arg rule            rule to delete
00612  * @arg flags           additional netlink message flags
00613  *
00614  * Builds a netlink message by calling rtnl_rule_build_delete_request(),
00615  * sends the request to the kernel and waits for the next ACK to be
00616  * received and thus blocks until the request has been fullfilled.
00617  *
00618  * @return 0 on sucess or a negative error if an error occured.
00619  */
00620 int rtnl_rule_delete(struct nl_handle *handle, struct rtnl_rule *rule,
00621                      int flags)
00622 {
00623         int err;
00624         struct nl_msg *msg;
00625         
00626         msg = rtnl_rule_build_delete_request(rule, flags);
00627         if (!msg)
00628                 return nl_errno(ENOMEM);
00629 
00630         err = nl_send_auto_complete(handle, msg);
00631         if (err < 0)
00632                 return err;
00633 
00634         nlmsg_free(msg);
00635         return nl_wait_for_ack(handle);
00636 }
00637 
00638 /** @} */
00639 
00640 /**
00641  * @name Attribute Modification
00642  * @{
00643  */
00644 
00645 /**
00646  * Set the address family of a rule to the specified value
00647  * @arg rule            rule to change
00648  * @arg family          new address family
00649  */
00650 void rtnl_rule_set_family(struct rtnl_rule *rule, int family)
00651 {
00652         rule->r_family = family;
00653         rule->r_mask |= RULE_ATTR_FAMILY;
00654 }
00655 
00656 /**
00657  * Get the address family of a rule
00658  * @arg rule            rule handle
00659  * @return Address family or AF_UNSPEC if not set
00660  */
00661 int rtnl_rule_get_family(struct rtnl_rule *rule)
00662 {
00663         if (rule->r_mask & RULE_ATTR_FAMILY)
00664                 return rule->r_family;
00665         else
00666                 return AF_UNSPEC;
00667 }
00668 
00669 /**
00670  * Set the priority of a rule to the specified value
00671  * @arg rule            rule to change
00672  * @arg prio            new priority
00673  */
00674 void rtnl_rule_set_prio(struct rtnl_rule *rule, int prio)
00675 {
00676         rule->r_prio = prio;
00677         rule->r_mask |= RULE_ATTR_PRIO;
00678 }
00679 
00680 /**
00681  * Get the priority of a rule
00682  * @arg rule            rule handle
00683  * @return Priority or -1 if not set
00684  */
00685 int rtnl_rule_get_prio(struct rtnl_rule *rule)
00686 {
00687         if (rule->r_mask & RULE_ATTR_PRIO)
00688                 return rule->r_prio;
00689         else
00690                 return -1;
00691 }
00692 
00693 /**
00694  * Set the firewall mark of a rule to the specified value
00695  * @arg rule            rule to change
00696  * @arg fwmark          new firewall mark
00697  */
00698 void rtnl_rule_set_fwmark(struct rtnl_rule *rule, uint64_t fwmark)
00699 {
00700         rule->r_fwmark = fwmark;
00701         rule->r_mask |= RULE_ATTR_FWMARK;
00702 }
00703 
00704 /**
00705  * Get the firewall mark of a rule
00706  * @arg rule            rule handle
00707  * @return Firewall mark or UINT_LEAST64_MAX if not set
00708  */
00709 uint64_t rtnl_rule_get_fwmark(struct rtnl_rule *rule)
00710 {
00711         if (rule->r_mask & RULE_ATTR_FWMARK)
00712                 return rule->r_fwmark;
00713         else
00714                 return UINT_LEAST64_MAX;
00715 }
00716 
00717 /**
00718  * Set the table index of a rule to the specified value
00719  * @arg rule            rule to change
00720  * @arg table           new table
00721  */
00722 void rtnl_rule_set_table(struct rtnl_rule *rule, int table)
00723 {
00724         rule->r_table = table;
00725         rule->r_mask |= RULE_ATTR_TABLE;
00726 }
00727 
00728 /**
00729  * Get the table index of a rule
00730  * @arg rule            rule handle
00731  * @return Table index or -1 if not set
00732  */
00733 int rtnl_rule_get_table(struct rtnl_rule *rule)
00734 {
00735         if (rule->r_mask & RULE_ATTR_TABLE)
00736                 return rule->r_table;
00737         else
00738                 return -1;
00739 }
00740 
00741 /**
00742  * Set the dsfield of a rule to the specified value
00743  * @arg rule            rule to change
00744  * @arg dsfield         new dsfield value
00745  */
00746 void rtnl_rule_set_dsfield(struct rtnl_rule *rule, int dsfield)
00747 {
00748         rule->r_dsfield = dsfield;
00749         rule->r_mask |= RULE_ATTR_DSFIELD;
00750 }
00751 
00752 /**
00753  * Get the dsfield of a rule
00754  * @arg rule            rule handle
00755  * @return dsfield or -1 if not set
00756  */
00757 int rtnl_rule_get_dsfield(struct rtnl_rule *rule)
00758 {
00759         if (rule->r_mask & RULE_ATTR_DSFIELD)
00760                 return rule->r_dsfield;
00761         else
00762                 return -1;
00763 }
00764 
00765 /**
00766  * Set the source address prefix length of a rule to the specified value
00767  * @arg rule            rule to change
00768  * @arg len             new source address length
00769  */
00770 void rtnl_rule_set_src_len(struct rtnl_rule *rule, int len)
00771 {
00772         rule->r_src_len = len;
00773         if (rule->r_mask & RULE_ATTR_SRC)
00774                 nl_addr_set_prefixlen(rule->r_src, len);
00775         rule->r_mask |= RULE_ATTR_SRC_LEN;
00776 }
00777 
00778 /**
00779  * Get the source address prefix length of a rule
00780  * @arg rule            rule handle
00781  * @return Prefix length of source address or -1 if not set
00782  */
00783 int rtnl_rule_get_src_len(struct rtnl_rule *rule)
00784 {
00785         if (rule->r_mask & RULE_ATTR_SRC_LEN)
00786                 return rule->r_src_len;
00787         else
00788                 return -1;
00789 }
00790 
00791 /**
00792  * Set the destination address prefix length of a rule to the specified value
00793  * @arg rule            rule to change
00794  * @arg len             new destination address length
00795  */
00796 void rtnl_rule_set_dst_len(struct rtnl_rule *rule, int len)
00797 {
00798         rule->r_dst_len = len;
00799         if (rule->r_mask & RULE_ATTR_DST)
00800                 nl_addr_set_prefixlen(rule->r_dst, len);
00801         rule->r_mask |= RULE_ATTR_DST_LEN;
00802 }
00803 
00804 /**
00805  * Get the destination address prefix length of a rule
00806  * @arg rule            rule handle
00807  * @return Prefix length of destination address or -1 if not set
00808  */
00809 int rtnl_rule_get_dst_len(struct rtnl_rule *rule)
00810 {
00811         if (rule->r_mask & RULE_ATTR_DST_LEN)
00812                 return rule->r_dst_len;
00813         else
00814                 return -1;
00815 }
00816 
00817 static inline int __assign_addr(struct rtnl_rule *rule, struct nl_addr **pos,
00818                                 struct nl_addr *new, uint8_t *len, int flag)
00819 {
00820         if (rule->r_mask & RULE_ATTR_FAMILY) {
00821                 if (new->a_family != rule->r_family)
00822                         return nl_error(EINVAL, "Address family mismatch");
00823         } else
00824                 rule->r_family = new->a_family;
00825 
00826         if (*pos)
00827                 nl_addr_put(*pos);
00828 
00829         nl_addr_get(new);
00830         *pos = new;
00831         *len = nl_addr_get_prefixlen(new);
00832 
00833         rule->r_mask |= (flag | RULE_ATTR_FAMILY);
00834 
00835         return 0;
00836 }
00837 /**
00838  * Set the source address of a rule
00839  * @arg rule            rule to change
00840  * @arg src             new source address
00841  *
00842  * Assigns the new source address to the specified rule handle. The
00843  * address is validated against the address family if set already via
00844  * either rtnl_rule_set_family() or by setting one of the other addresses.
00845  * The assignment fails if the address families mismatch. In case the
00846  * address family has not been specified yet, the address family of this
00847  * new address is elected to be the requirement.
00848  * 
00849  * @return 0 on success or a negative error code.
00850  */
00851 int rtnl_rule_set_src(struct rtnl_rule *rule, struct nl_addr *src)
00852 {
00853         return __assign_addr(rule, &rule->r_src, src, &rule->r_src_len,
00854                              RULE_ATTR_SRC | RULE_ATTR_SRC_LEN);
00855 }
00856 
00857 /**
00858  * Get the source address of a rule
00859  * @arg rule            rule handle
00860  * @return Source address or NULL if not set
00861  */
00862 struct nl_addr *rtnl_rule_get_src(struct rtnl_rule *rule)
00863 {
00864         if (rule->r_mask & RULE_ATTR_SRC)
00865                 return rule->r_src;
00866         else
00867                 return NULL;
00868 }
00869 
00870 /**
00871  * Set the destination address of a rule
00872  * @arg rule            rule to change
00873  * @arg dst             new destination address
00874  *
00875  * Assigns the new destination address to the specified rule handle. The
00876  * address is validated against the address family if set already via
00877  * either rtnl_rule_set_family() or by setting one of the other addresses.
00878  * The assignment fails if the address families mismatch. In case the
00879  * address family has not been specified yet, the address family of this
00880  * new address is elected to be the requirement.
00881  * 
00882  * @return 0 on success or a negative error code.
00883  */
00884 int rtnl_rule_set_dst(struct rtnl_rule *rule, struct nl_addr *dst)
00885 {
00886         return __assign_addr(rule, &rule->r_dst, dst, &rule->r_dst_len,
00887                              RULE_ATTR_DST | RULE_ATTR_DST_LEN);
00888 }
00889 
00890 /**
00891  * Get the destination address of a rule
00892  * @arg rule            rule handle
00893  * @return Destination address or NULL if not set
00894  */
00895 struct nl_addr *rtnl_rule_get_dst(struct rtnl_rule *rule)
00896 {
00897         if (rule->r_mask & RULE_ATTR_DST)
00898                 return rule->r_dst;
00899         else
00900                 return NULL;
00901 }
00902 
00903 /**
00904  * Set incoming interface of routing rule object.
00905  * @arg rule            Routing rule object to be modified.
00906  * @arg dev             Name of incoming interface.
00907  *
00908  * @return 0 on success or a negative error code.
00909  */
00910 int rtnl_rule_set_iif(struct rtnl_rule *rule, const char *dev)
00911 {
00912         if (strlen(dev) > IFNAMSIZ-1)
00913                 return nl_errno(ERANGE);
00914 
00915         strcpy(rule->r_iif, dev);
00916         rule->r_mask |= RULE_ATTR_IIF;
00917         return 0;
00918 }
00919 
00920 /**
00921  * Get incoming interface of routing rule object.
00922  * @arg rule            Routing rule object.
00923  * @return Name of incoming interface or NULL if not available.
00924  */
00925 char *rtnl_rule_get_iif(struct rtnl_rule *rule)
00926 {
00927         if (rule->r_mask & RULE_ATTR_IIF)
00928                 return rule->r_iif;
00929         else
00930                 return NULL;
00931 }
00932 
00933 /**
00934  * Set action of routing rule object.
00935  * @arg rule            Routing rule object to be modified.
00936  * @arg type            New routing type specifying an action.
00937  */
00938 void rtnl_rule_set_action(struct rtnl_rule *rule, int type)
00939 {
00940         rule->r_type = type;
00941         rule->r_mask |= RULE_ATTR_TYPE;
00942 }
00943 
00944 /**
00945  * Get action of routing rule object.
00946  * @arg rule            Routing rule object.
00947  * @return Routing type or a negative error code.
00948  */
00949 int rtnl_rule_get_action(struct rtnl_rule *rule)
00950 {
00951         if (rule->r_mask & RULE_ATTR_TYPE)
00952                 return rule->r_type;
00953         else
00954                 return nl_errno(ENOENT);
00955 }
00956 
00957 /**
00958  * Set realms of routing rule object.
00959  * @arg rule            Routing rule object to be modified.
00960  * @arg realms          New realms value.
00961  */
00962 void rtnl_rule_set_realms(struct rtnl_rule *rule, realm_t realms)
00963 {
00964         rule->r_realms = realms;
00965         rule->r_mask |= RULE_ATTR_REALMS;
00966 }
00967 
00968 /**
00969  * Get realms of routing rule object.
00970  * @arg rule            Routing rule object.
00971  * @return Realms value or 0 if not set.
00972  */
00973 realm_t rtnl_rule_get_realms(struct rtnl_rule *rule)
00974 {
00975         if (rule->r_mask & RULE_ATTR_REALMS)
00976                 return rule->r_realms;
00977         else
00978                 return 0;
00979 }
00980 
00981 /** @} */
00982 
00983 static struct nl_cache_ops rtnl_rule_ops = {
00984         .co_name                = "route/rule",
00985         .co_size                = sizeof(struct rtnl_rule),
00986         .co_hdrsize             = sizeof(struct rtmsg),
00987         .co_msgtypes            = {
00988                                         { RTM_NEWRULE, "new" },
00989                                         { RTM_DELRULE, "delete" },
00990                                         { RTM_GETRULE, "get" },
00991                                         { -1, NULL },
00992                                   },
00993         .co_protocol            = NETLINK_ROUTE,
00994         .co_request_update      = rule_request_update,
00995         .co_msg_parser          = rule_msg_parser,
00996         .co_free_data           = rule_free_data,
00997         .co_dump[NL_DUMP_BRIEF] = rule_dump_brief,
00998         .co_dump[NL_DUMP_FULL]  = rule_dump_full,
00999         .co_dump[NL_DUMP_STATS] = rule_dump_stats,
01000         .co_dump[NL_DUMP_XML]   = rule_dump_xml,
01001         .co_dump[NL_DUMP_ENV]   = rule_dump_env,
01002         .co_filter              = rule_filter,
01003 };
01004 
01005 static void __init rule_init(void)
01006 {
01007         nl_cache_mngt_register(&rtnl_rule_ops);
01008 }
01009 
01010 static void __exit rule_exit(void)
01011 {
01012         nl_cache_mngt_unregister(&rtnl_rule_ops);
01013 }
01014 
01015 /** @} */

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1