proto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2012 Steven Barth <steven@midlink.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <limits.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include "netifd.h"
  22. #include "system.h"
  23. #include "interface.h"
  24. #include "interface-ip.h"
  25. #include "proto.h"
  26. static struct avl_tree handlers;
  27. enum {
  28. OPT_IPADDR,
  29. OPT_IP6ADDR,
  30. OPT_NETMASK,
  31. OPT_BROADCAST,
  32. OPT_PTPADDR,
  33. OPT_GATEWAY,
  34. OPT_IP6GW,
  35. OPT_IP6PREFIX,
  36. OPT_IP6DEPRECATED,
  37. __OPT_MAX,
  38. };
  39. static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
  40. [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
  41. [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
  42. [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
  43. [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
  44. [OPT_PTPADDR] = { .name = "ptpaddr", .type = BLOBMSG_TYPE_STRING },
  45. [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
  46. [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
  47. [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
  48. [OPT_IP6DEPRECATED] = { .name = "ip6deprecated", .type = BLOBMSG_TYPE_BOOL },
  49. };
  50. static const struct uci_blob_param_info proto_ip_attr_info[__OPT_MAX] = {
  51. [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
  52. [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
  53. [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
  54. };
  55. static const char * const proto_ip_validate[__OPT_MAX] = {
  56. [OPT_IPADDR] = "ip4addr",
  57. [OPT_IP6ADDR] = "ip6addr",
  58. [OPT_NETMASK] = "netmask",
  59. [OPT_BROADCAST] = "ipaddr",
  60. [OPT_PTPADDR] = "ip4addr",
  61. [OPT_GATEWAY] = "ip4addr",
  62. [OPT_IP6GW] = "ip6addr",
  63. [OPT_IP6PREFIX] = "ip6addr",
  64. };
  65. const struct uci_blob_param_list proto_ip_attr = {
  66. .n_params = __OPT_MAX,
  67. .params = proto_ip_attributes,
  68. .validate = proto_ip_validate,
  69. .info = proto_ip_attr_info,
  70. };
  71. enum {
  72. ADDR_IPADDR,
  73. ADDR_MASK,
  74. ADDR_BROADCAST,
  75. ADDR_PTP,
  76. ADDR_PREFERRED,
  77. ADDR_VALID,
  78. ADDR_OFFLINK,
  79. ADDR_CLASS,
  80. __ADDR_MAX
  81. };
  82. static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
  83. [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
  84. [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
  85. [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
  86. [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
  87. [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
  88. [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
  89. [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
  90. [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
  91. };
  92. static struct device_addr *
  93. alloc_device_addr(bool v6, bool ext)
  94. {
  95. struct device_addr *addr;
  96. addr = calloc(1, sizeof(*addr));
  97. if (!addr)
  98. return NULL;
  99. addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  100. if (ext)
  101. addr->flags |= DEVADDR_EXTERNAL;
  102. return addr;
  103. }
  104. static bool
  105. parse_addr(struct interface *iface, const char *str, bool v6, int mask,
  106. bool ext, uint32_t broadcast, uint32_t ptp, bool deprecated)
  107. {
  108. struct device_addr *addr;
  109. int af = v6 ? AF_INET6 : AF_INET;
  110. addr = alloc_device_addr(v6, ext);
  111. if (!addr)
  112. return false;
  113. addr->mask = mask;
  114. if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask))
  115. goto error;
  116. if (v6 && IN6_IS_ADDR_MULTICAST(&addr->addr.in6))
  117. goto error;
  118. if (broadcast)
  119. addr->broadcast = broadcast;
  120. if (ptp)
  121. addr->point_to_point = ptp;
  122. if (deprecated)
  123. addr->preferred_until = system_get_rtime();
  124. vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
  125. return true;
  126. error:
  127. interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
  128. free(addr);
  129. return false;
  130. }
  131. static int
  132. parse_static_address_option(struct interface *iface, struct blob_attr *attr,
  133. bool v6, int netmask, bool ext, uint32_t broadcast,
  134. uint32_t ptp, bool deprecated)
  135. {
  136. struct blob_attr *cur;
  137. int n_addr = 0;
  138. int rem;
  139. blobmsg_for_each_attr(cur, attr, rem) {
  140. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  141. return -1;
  142. n_addr++;
  143. if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
  144. broadcast, ptp, deprecated))
  145. return -1;
  146. }
  147. return n_addr;
  148. }
  149. static struct device_addr *
  150. parse_address_item(struct blob_attr *attr, bool v6, bool ext)
  151. {
  152. struct device_addr *addr;
  153. struct blob_attr *tb[__ADDR_MAX];
  154. struct blob_attr *cur;
  155. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  156. return NULL;
  157. addr = alloc_device_addr(v6, ext);
  158. if (!addr)
  159. return NULL;
  160. blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  161. addr->mask = v6 ? 128 : 32;
  162. if ((cur = tb[ADDR_MASK])) {
  163. unsigned int new_mask;
  164. new_mask = parse_netmask_string(blobmsg_data(cur), v6);
  165. if (new_mask > addr->mask)
  166. goto error;
  167. addr->mask = new_mask;
  168. }
  169. cur = tb[ADDR_IPADDR];
  170. if (!cur)
  171. goto error;
  172. if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
  173. goto error;
  174. if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
  175. addr->flags |= DEVADDR_OFFLINK;
  176. if (!v6) {
  177. if ((cur = tb[ADDR_BROADCAST]) &&
  178. !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
  179. goto error;
  180. if ((cur = tb[ADDR_PTP]) &&
  181. !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
  182. goto error;
  183. } else {
  184. time_t now = system_get_rtime();
  185. if ((cur = tb[ADDR_PREFERRED])) {
  186. int64_t preferred = blobmsg_get_u32(cur);
  187. int64_t preferred_until = preferred + (int64_t)now;
  188. if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
  189. addr->preferred_until = preferred_until;
  190. }
  191. if ((cur = tb[ADDR_VALID])) {
  192. int64_t valid = blobmsg_get_u32(cur);
  193. int64_t valid_until = valid + (int64_t)now;
  194. if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
  195. addr->valid_until = valid_until;
  196. }
  197. if (addr->valid_until) {
  198. if (!addr->preferred_until)
  199. addr->preferred_until = addr->valid_until;
  200. else if (addr->preferred_until > addr->valid_until)
  201. goto error;
  202. }
  203. if ((cur = tb[ADDR_CLASS]))
  204. addr->pclass = strdup(blobmsg_get_string(cur));
  205. }
  206. return addr;
  207. error:
  208. free(addr);
  209. return NULL;
  210. }
  211. static int
  212. parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
  213. bool ext)
  214. {
  215. struct device_addr *addr;
  216. struct blob_attr *cur;
  217. int n_addr = 0;
  218. int rem;
  219. blobmsg_for_each_attr(cur, attr, rem) {
  220. addr = parse_address_item(cur, v6, ext);
  221. if (!addr)
  222. return -1;
  223. n_addr++;
  224. vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
  225. }
  226. return n_addr;
  227. }
  228. static bool
  229. parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
  230. {
  231. struct device_route *route;
  232. const char *str = blobmsg_data(attr);
  233. int af = v6 ? AF_INET6 : AF_INET;
  234. route = calloc(1, sizeof(*route));
  235. if (!route)
  236. return NULL;
  237. if (!inet_pton(af, str, &route->nexthop)) {
  238. interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
  239. free(route);
  240. return false;
  241. }
  242. route->mask = 0;
  243. route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
  244. route->metric = iface->metric;
  245. unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
  246. if (table) {
  247. route->table = table;
  248. route->flags |= DEVROUTE_SRCTABLE;
  249. }
  250. vlist_add(&iface->proto_ip.route, &route->node, route);
  251. return true;
  252. }
  253. static bool
  254. parse_prefix_option(struct interface *iface, const char *str, size_t len)
  255. {
  256. char buf[128] = {0}, *saveptr;
  257. if (len >= sizeof(buf))
  258. return false;
  259. memcpy(buf, str, len);
  260. char *addrstr = strtok_r(buf, "/", &saveptr);
  261. if (!addrstr)
  262. return false;
  263. char *lengthstr = strtok_r(NULL, ",", &saveptr);
  264. if (!lengthstr)
  265. return false;
  266. char *prefstr = strtok_r(NULL, ",", &saveptr);
  267. char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  268. char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  269. const char *pclass = NULL;
  270. int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
  271. int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
  272. uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
  273. if (length < 1 || length > 64)
  274. return false;
  275. struct in6_addr addr, excluded, *excludedp = NULL;
  276. if (inet_pton(AF_INET6, addrstr, &addr) < 1)
  277. return false;
  278. for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
  279. char *key = NULL, *val = NULL, *addsaveptr;
  280. if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
  281. !(val = strtok_r(NULL, ",", &addsaveptr)))
  282. continue;
  283. if (!strcmp(key, "excluded")) {
  284. char *sep = strchr(val, '/');
  285. if (!sep)
  286. return false;
  287. *sep = 0;
  288. excl_length = atoi(sep + 1);
  289. if (inet_pton(AF_INET6, val, &excluded) < 1)
  290. return false;
  291. excludedp = &excluded;
  292. } else if (!strcmp(key, "class")) {
  293. pclass = val;
  294. }
  295. }
  296. int64_t now = system_get_rtime();
  297. time_t preferred_until = 0;
  298. if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
  299. preferred_until = pref + now;
  300. time_t valid_until = 0;
  301. if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
  302. valid_until = valid + now;
  303. interface_ip_add_device_prefix(iface, &addr, length,
  304. valid_until, preferred_until,
  305. excludedp, excl_length, pclass);
  306. return true;
  307. }
  308. static int
  309. parse_prefix_list(struct interface *iface, struct blob_attr *attr)
  310. {
  311. struct blob_attr *cur;
  312. int n_addr = 0;
  313. int rem;
  314. blobmsg_for_each_attr(cur, attr, rem) {
  315. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  316. return -1;
  317. n_addr++;
  318. if (!parse_prefix_option(iface, blobmsg_data(cur),
  319. blobmsg_data_len(cur)))
  320. return -1;
  321. }
  322. return n_addr;
  323. }
  324. int
  325. proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
  326. {
  327. struct blob_attr *tb[__OPT_MAX];
  328. struct blob_attr *cur;
  329. const char *error;
  330. unsigned int netmask = 32;
  331. bool ip6deprecated;
  332. int n_v4 = 0, n_v6 = 0;
  333. struct in_addr bcast = {}, ptp = {};
  334. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  335. if ((cur = tb[OPT_NETMASK])) {
  336. netmask = parse_netmask_string(blobmsg_data(cur), false);
  337. if (netmask > 32) {
  338. error = "INVALID_NETMASK";
  339. goto error;
  340. }
  341. }
  342. if ((cur = tb[OPT_BROADCAST])) {
  343. if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
  344. error = "INVALID_BROADCAST";
  345. goto error;
  346. }
  347. }
  348. if ((cur = tb[OPT_PTPADDR])) {
  349. if (!inet_pton(AF_INET, blobmsg_data(cur), &ptp)) {
  350. error = "INVALID_PTPADDR";
  351. goto error;
  352. }
  353. }
  354. ip6deprecated = blobmsg_get_bool_default(tb[OPT_IP6DEPRECATED], false);
  355. if ((cur = tb[OPT_IPADDR]))
  356. n_v4 = parse_static_address_option(iface, cur, false,
  357. netmask, false, bcast.s_addr, ptp.s_addr, false);
  358. if ((cur = tb[OPT_IP6ADDR]))
  359. n_v6 = parse_static_address_option(iface, cur, true,
  360. 128, false, 0, 0, ip6deprecated);
  361. if ((cur = tb[OPT_IP6PREFIX]))
  362. if (parse_prefix_list(iface, cur) < 0)
  363. goto out;
  364. if (n_v4 < 0 || n_v6 < 0)
  365. goto out;
  366. if ((cur = tb[OPT_GATEWAY])) {
  367. if (n_v4 && !parse_gateway_option(iface, cur, false))
  368. goto out;
  369. }
  370. if ((cur = tb[OPT_IP6GW])) {
  371. if (n_v6 && !parse_gateway_option(iface, cur, true))
  372. goto out;
  373. }
  374. return 0;
  375. error:
  376. interface_add_error(iface, "proto", error, NULL, 0);
  377. out:
  378. return -1;
  379. }
  380. int
  381. proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
  382. {
  383. struct blob_attr *tb[__OPT_MAX];
  384. struct blob_attr *cur;
  385. int n_v4 = 0, n_v6 = 0;
  386. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  387. if ((cur = tb[OPT_IPADDR]))
  388. n_v4 = parse_address_list(iface, cur, false, ext);
  389. if ((cur = tb[OPT_IP6ADDR]))
  390. n_v6 = parse_address_list(iface, cur, true, ext);
  391. if ((cur = tb[OPT_IP6PREFIX]))
  392. if (parse_prefix_list(iface, cur) < 0)
  393. goto out;
  394. if (n_v4 < 0 || n_v6 < 0)
  395. goto out;
  396. if ((cur = tb[OPT_GATEWAY])) {
  397. if (n_v4 && !parse_gateway_option(iface, cur, false))
  398. goto out;
  399. }
  400. if ((cur = tb[OPT_IP6GW])) {
  401. if (n_v6 && !parse_gateway_option(iface, cur, true))
  402. goto out;
  403. }
  404. return 0;
  405. out:
  406. return -1;
  407. }
  408. void add_proto_handler(struct proto_handler *p)
  409. {
  410. if (!handlers.comp)
  411. avl_init(&handlers, avl_strcmp, false, NULL);
  412. if (p->avl.key)
  413. return;
  414. p->avl.key = p->name;
  415. avl_insert(&handlers, &p->avl);
  416. }
  417. static void
  418. default_proto_free(struct interface_proto_state *proto)
  419. {
  420. free(proto);
  421. }
  422. static int
  423. invalid_proto_handler(struct interface_proto_state *proto,
  424. enum interface_proto_cmd cmd, bool force)
  425. {
  426. return -1;
  427. }
  428. static int
  429. no_proto_handler(struct interface_proto_state *proto,
  430. enum interface_proto_cmd cmd, bool force)
  431. {
  432. return 0;
  433. }
  434. static struct interface_proto_state *
  435. default_proto_attach(const struct proto_handler *h,
  436. struct interface *iface, struct blob_attr *attr)
  437. {
  438. struct interface_proto_state *proto;
  439. proto = calloc(1, sizeof(*proto));
  440. if (!proto)
  441. return NULL;
  442. proto->free = default_proto_free;
  443. proto->cb = no_proto_handler;
  444. return proto;
  445. }
  446. static const struct proto_handler no_proto = {
  447. .name = "none",
  448. .flags = PROTO_FLAG_IMMEDIATE,
  449. .attach = default_proto_attach,
  450. };
  451. static const struct proto_handler *
  452. get_proto_handler(const char *name)
  453. {
  454. struct proto_handler *proto;
  455. if (!strcmp(name, "none"))
  456. return &no_proto;
  457. if (!handlers.comp)
  458. return NULL;
  459. return avl_find_element(&handlers, name, proto, avl);
  460. }
  461. void
  462. proto_dump_handlers(struct blob_buf *b)
  463. {
  464. struct proto_handler *p;
  465. void *c;
  466. avl_for_each_element(&handlers, p, avl) {
  467. void *v;
  468. c = blobmsg_open_table(b, p->name);
  469. if (p->config_params && p->config_params->validate) {
  470. int i;
  471. v = blobmsg_open_table(b, "validate");
  472. for (i = 0; i < p->config_params->n_params; i++)
  473. blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i));
  474. blobmsg_close_table(b, v);
  475. }
  476. blobmsg_add_u8(b, "immediate", !!(p->flags & PROTO_FLAG_IMMEDIATE));
  477. blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
  478. blobmsg_add_u8(b, "init_available", !!(p->flags & PROTO_FLAG_INIT_AVAILABLE));
  479. blobmsg_add_u8(b, "renew_available", !!(p->flags & PROTO_FLAG_RENEW_AVAILABLE));
  480. blobmsg_add_u8(b, "force_link_default", !!(p->flags & PROTO_FLAG_FORCE_LINK_DEFAULT));
  481. blobmsg_add_u8(b, "last_error", !!(p->flags & PROTO_FLAG_LASTERROR));
  482. blobmsg_add_u8(b, "teardown_on_l3_link_down", !!(p->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN));
  483. blobmsg_add_u8(b, "no_task", !!(p->flags & PROTO_FLAG_NO_TASK));
  484. blobmsg_close_table(b, c);
  485. }
  486. }
  487. void
  488. proto_init_interface(struct interface *iface, struct blob_attr *attr)
  489. {
  490. const struct proto_handler *proto = iface->proto_handler;
  491. struct interface_proto_state *state = NULL;
  492. if (!proto)
  493. proto = &no_proto;
  494. state = proto->attach(proto, iface, attr);
  495. if (!state) {
  496. state = no_proto.attach(&no_proto, iface, attr);
  497. state->cb = invalid_proto_handler;
  498. }
  499. state->handler = proto;
  500. interface_set_proto_state(iface, state);
  501. }
  502. void
  503. proto_attach_interface(struct interface *iface, const char *proto_name)
  504. {
  505. const struct proto_handler *proto = &no_proto;
  506. const char *error = NULL;
  507. if (proto_name) {
  508. proto = get_proto_handler(proto_name);
  509. if (!proto) {
  510. error = "INVALID_PROTO";
  511. proto = &no_proto;
  512. }
  513. }
  514. iface->proto_handler = proto;
  515. if (error)
  516. interface_add_error(iface, "proto", error, NULL, 0);
  517. }
  518. int
  519. interface_proto_event(struct interface_proto_state *proto,
  520. enum interface_proto_cmd cmd, bool force)
  521. {
  522. enum interface_proto_event ev;
  523. int ret;
  524. ret = proto->cb(proto, cmd, force);
  525. if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
  526. goto out;
  527. switch(cmd) {
  528. case PROTO_CMD_SETUP:
  529. ev = IFPEV_UP;
  530. break;
  531. case PROTO_CMD_TEARDOWN:
  532. ev = IFPEV_DOWN;
  533. break;
  534. case PROTO_CMD_RENEW:
  535. ev = IFPEV_RENEW;
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. proto->proto_event(proto, ev);
  541. out:
  542. return ret;
  543. }