proto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 struct device_addr *
  105. parse_addr(const char *str, bool v6, int mask, bool ext, uint32_t broadcast,
  106. 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 NULL;
  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. return addr;
  125. error:
  126. free(addr);
  127. return NULL;
  128. }
  129. static int
  130. parse_static_address_option(struct interface *iface, struct blob_attr *attr,
  131. bool v6, int netmask, bool ext, uint32_t broadcast,
  132. uint32_t ptp, bool deprecated)
  133. {
  134. struct blob_attr *cur;
  135. struct device_addr *addr;
  136. const char *str;
  137. int n_addr = 0;
  138. size_t rem;
  139. blobmsg_for_each_attr(cur, attr, rem) {
  140. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  141. return -1;
  142. str = blobmsg_data(cur);
  143. addr = parse_addr(str, v6, netmask, ext, broadcast, ptp, deprecated);
  144. if (addr == NULL) {
  145. interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
  146. return -1;
  147. }
  148. addr->index = n_addr;
  149. n_addr++;
  150. vlist_add(&iface->proto_ip.addr, &addr->node, addr);
  151. }
  152. return n_addr;
  153. }
  154. static struct device_addr *
  155. parse_address_item(struct blob_attr *attr, bool v6, bool ext)
  156. {
  157. struct device_addr *addr;
  158. struct blob_attr *tb[__ADDR_MAX];
  159. struct blob_attr *cur;
  160. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  161. return NULL;
  162. addr = alloc_device_addr(v6, ext);
  163. if (!addr)
  164. return NULL;
  165. blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  166. addr->mask = v6 ? 128 : 32;
  167. if ((cur = tb[ADDR_MASK])) {
  168. unsigned int new_mask;
  169. new_mask = parse_netmask_string(blobmsg_data(cur), v6);
  170. if (new_mask > addr->mask)
  171. goto error;
  172. addr->mask = new_mask;
  173. }
  174. cur = tb[ADDR_IPADDR];
  175. if (!cur)
  176. goto error;
  177. if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
  178. goto error;
  179. if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
  180. addr->flags |= DEVADDR_OFFLINK;
  181. if (!v6) {
  182. if ((cur = tb[ADDR_BROADCAST]) &&
  183. !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
  184. goto error;
  185. if ((cur = tb[ADDR_PTP]) &&
  186. !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
  187. goto error;
  188. } else {
  189. time_t now = system_get_rtime();
  190. if ((cur = tb[ADDR_PREFERRED])) {
  191. int64_t preferred = blobmsg_get_u32(cur);
  192. int64_t preferred_until = preferred + (int64_t)now;
  193. if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
  194. addr->preferred_until = preferred_until;
  195. }
  196. if ((cur = tb[ADDR_VALID])) {
  197. int64_t valid = blobmsg_get_u32(cur);
  198. int64_t valid_until = valid + (int64_t)now;
  199. if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
  200. addr->valid_until = valid_until;
  201. }
  202. if (addr->valid_until) {
  203. if (!addr->preferred_until)
  204. addr->preferred_until = addr->valid_until;
  205. else if (addr->preferred_until > addr->valid_until)
  206. goto error;
  207. }
  208. if ((cur = tb[ADDR_CLASS]))
  209. addr->pclass = strdup(blobmsg_get_string(cur));
  210. }
  211. return addr;
  212. error:
  213. free(addr);
  214. return NULL;
  215. }
  216. static int
  217. parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
  218. bool ext)
  219. {
  220. struct device_addr *addr;
  221. struct blob_attr *cur;
  222. int n_addr = 0;
  223. size_t rem;
  224. blobmsg_for_each_attr(cur, attr, rem) {
  225. addr = parse_address_item(cur, v6, ext);
  226. if (!addr)
  227. return -1;
  228. addr->index = n_addr;
  229. n_addr++;
  230. vlist_add(&iface->proto_ip.addr, &addr->node, addr);
  231. }
  232. return n_addr;
  233. }
  234. static bool
  235. parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
  236. {
  237. struct device_route *route;
  238. const char *str = blobmsg_data(attr);
  239. int af = v6 ? AF_INET6 : AF_INET;
  240. route = calloc(1, sizeof(*route));
  241. if (!route)
  242. return NULL;
  243. if (!inet_pton(af, str, &route->nexthop)) {
  244. interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
  245. free(route);
  246. return false;
  247. }
  248. route->mask = 0;
  249. route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
  250. route->metric = iface->metric;
  251. unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
  252. if (table) {
  253. route->table = table;
  254. route->flags |= DEVROUTE_SRCTABLE;
  255. }
  256. vlist_add(&iface->proto_ip.route, &route->node, route);
  257. return true;
  258. }
  259. static bool
  260. parse_prefix_option(struct interface *iface, const char *str, size_t len)
  261. {
  262. char buf[128] = {0}, *saveptr;
  263. if (len >= sizeof(buf))
  264. return false;
  265. memcpy(buf, str, len);
  266. char *addrstr = strtok_r(buf, "/", &saveptr);
  267. if (!addrstr)
  268. return false;
  269. char *lengthstr = strtok_r(NULL, ",", &saveptr);
  270. if (!lengthstr)
  271. return false;
  272. char *prefstr = strtok_r(NULL, ",", &saveptr);
  273. char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  274. char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  275. const char *pclass = NULL;
  276. int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
  277. int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
  278. uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
  279. if (length < 1 || length > 64)
  280. return false;
  281. struct in6_addr addr, excluded, *excludedp = NULL;
  282. if (inet_pton(AF_INET6, addrstr, &addr) < 1)
  283. return false;
  284. for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
  285. char *key = NULL, *val = NULL, *addsaveptr;
  286. if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
  287. !(val = strtok_r(NULL, ",", &addsaveptr)))
  288. continue;
  289. if (!strcmp(key, "excluded")) {
  290. char *sep = strchr(val, '/');
  291. if (!sep)
  292. return false;
  293. *sep = 0;
  294. excl_length = atoi(sep + 1);
  295. if (inet_pton(AF_INET6, val, &excluded) < 1)
  296. return false;
  297. excludedp = &excluded;
  298. } else if (!strcmp(key, "class")) {
  299. pclass = val;
  300. }
  301. }
  302. int64_t now = system_get_rtime();
  303. time_t preferred_until = 0;
  304. if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
  305. preferred_until = pref + now;
  306. time_t valid_until = 0;
  307. if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
  308. valid_until = valid + now;
  309. interface_ip_add_device_prefix(iface, &addr, length,
  310. valid_until, preferred_until,
  311. excludedp, excl_length, pclass);
  312. return true;
  313. }
  314. static int
  315. parse_prefix_list(struct interface *iface, struct blob_attr *attr)
  316. {
  317. struct blob_attr *cur;
  318. int n_addr = 0;
  319. size_t rem;
  320. blobmsg_for_each_attr(cur, attr, rem) {
  321. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  322. return -1;
  323. n_addr++;
  324. if (!parse_prefix_option(iface, blobmsg_data(cur),
  325. blobmsg_data_len(cur)))
  326. return -1;
  327. }
  328. return n_addr;
  329. }
  330. int
  331. proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
  332. {
  333. struct blob_attr *tb[__OPT_MAX];
  334. struct blob_attr *cur;
  335. const char *error;
  336. unsigned int netmask = 32;
  337. bool ip6deprecated;
  338. int n_v4 = 0, n_v6 = 0;
  339. struct in_addr bcast = {0,}, ptp = {0,};
  340. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  341. if ((cur = tb[OPT_NETMASK])) {
  342. netmask = parse_netmask_string(blobmsg_data(cur), false);
  343. if (netmask > 32) {
  344. error = "INVALID_NETMASK";
  345. goto error;
  346. }
  347. }
  348. if ((cur = tb[OPT_BROADCAST])) {
  349. if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
  350. error = "INVALID_BROADCAST";
  351. goto error;
  352. }
  353. }
  354. if ((cur = tb[OPT_PTPADDR])) {
  355. if (!inet_pton(AF_INET, blobmsg_data(cur), &ptp)) {
  356. error = "INVALID_PTPADDR";
  357. goto error;
  358. }
  359. }
  360. ip6deprecated = blobmsg_get_bool_default(tb[OPT_IP6DEPRECATED], false);
  361. if ((cur = tb[OPT_IPADDR]))
  362. n_v4 = parse_static_address_option(iface, cur, false,
  363. netmask, false, bcast.s_addr, ptp.s_addr, false);
  364. if ((cur = tb[OPT_IP6ADDR]))
  365. n_v6 = parse_static_address_option(iface, cur, true,
  366. 128, false, 0, 0, ip6deprecated);
  367. if ((cur = tb[OPT_IP6PREFIX]))
  368. if (parse_prefix_list(iface, cur) < 0)
  369. goto out;
  370. if (n_v4 < 0 || n_v6 < 0)
  371. goto out;
  372. if ((cur = tb[OPT_GATEWAY])) {
  373. if (n_v4 && !parse_gateway_option(iface, cur, false))
  374. goto out;
  375. }
  376. if ((cur = tb[OPT_IP6GW])) {
  377. if (n_v6 && !parse_gateway_option(iface, cur, true))
  378. goto out;
  379. }
  380. return 0;
  381. error:
  382. interface_add_error(iface, "proto", error, NULL, 0);
  383. out:
  384. return -1;
  385. }
  386. int
  387. proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
  388. {
  389. struct blob_attr *tb[__OPT_MAX];
  390. struct blob_attr *cur;
  391. int n_v4 = 0, n_v6 = 0;
  392. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  393. if ((cur = tb[OPT_IPADDR]))
  394. n_v4 = parse_address_list(iface, cur, false, ext);
  395. if ((cur = tb[OPT_IP6ADDR]))
  396. n_v6 = parse_address_list(iface, cur, true, ext);
  397. if ((cur = tb[OPT_IP6PREFIX]))
  398. if (parse_prefix_list(iface, cur) < 0)
  399. goto out;
  400. if (n_v4 < 0 || n_v6 < 0)
  401. goto out;
  402. if ((cur = tb[OPT_GATEWAY])) {
  403. if (n_v4 && !parse_gateway_option(iface, cur, false))
  404. goto out;
  405. }
  406. if ((cur = tb[OPT_IP6GW])) {
  407. if (n_v6 && !parse_gateway_option(iface, cur, true))
  408. goto out;
  409. }
  410. return 0;
  411. out:
  412. return -1;
  413. }
  414. void add_proto_handler(struct proto_handler *p)
  415. {
  416. if (!handlers.comp)
  417. avl_init(&handlers, avl_strcmp, false, NULL);
  418. if (p->avl.key)
  419. return;
  420. p->avl.key = p->name;
  421. avl_insert(&handlers, &p->avl);
  422. }
  423. static void
  424. default_proto_free(struct interface_proto_state *proto)
  425. {
  426. free(proto);
  427. }
  428. static int
  429. invalid_proto_handler(struct interface_proto_state *proto,
  430. enum interface_proto_cmd cmd, bool force)
  431. {
  432. return -1;
  433. }
  434. static int
  435. no_proto_handler(struct interface_proto_state *proto,
  436. enum interface_proto_cmd cmd, bool force)
  437. {
  438. return 0;
  439. }
  440. static struct interface_proto_state *
  441. default_proto_attach(const struct proto_handler *h,
  442. struct interface *iface, struct blob_attr *attr)
  443. {
  444. struct interface_proto_state *proto;
  445. proto = calloc(1, sizeof(*proto));
  446. if (!proto)
  447. return NULL;
  448. proto->free = default_proto_free;
  449. proto->cb = no_proto_handler;
  450. return proto;
  451. }
  452. static const struct proto_handler no_proto = {
  453. .name = "none",
  454. .flags = PROTO_FLAG_IMMEDIATE,
  455. .attach = default_proto_attach,
  456. };
  457. static const struct proto_handler *
  458. get_proto_handler(const char *name)
  459. {
  460. struct proto_handler *proto;
  461. if (!strcmp(name, "none"))
  462. return &no_proto;
  463. if (!handlers.comp)
  464. return NULL;
  465. return avl_find_element(&handlers, name, proto, avl);
  466. }
  467. void
  468. proto_dump_handlers(struct blob_buf *b)
  469. {
  470. struct proto_handler *p;
  471. void *c;
  472. avl_for_each_element(&handlers, p, avl) {
  473. void *v;
  474. c = blobmsg_open_table(b, p->name);
  475. if (p->config_params && p->config_params->validate) {
  476. int i;
  477. v = blobmsg_open_table(b, "validate");
  478. for (i = 0; i < p->config_params->n_params; i++)
  479. blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i));
  480. blobmsg_close_table(b, v);
  481. }
  482. blobmsg_add_u8(b, "immediate", !!(p->flags & PROTO_FLAG_IMMEDIATE));
  483. blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
  484. blobmsg_add_u8(b, "init_available", !!(p->flags & PROTO_FLAG_INIT_AVAILABLE));
  485. blobmsg_add_u8(b, "renew_available", !!(p->flags & PROTO_FLAG_RENEW_AVAILABLE));
  486. blobmsg_add_u8(b, "force_link_default", !!(p->flags & PROTO_FLAG_FORCE_LINK_DEFAULT));
  487. blobmsg_add_u8(b, "last_error", !!(p->flags & PROTO_FLAG_LASTERROR));
  488. blobmsg_add_u8(b, "teardown_on_l3_link_down", !!(p->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN));
  489. blobmsg_add_u8(b, "no_task", !!(p->flags & PROTO_FLAG_NO_TASK));
  490. blobmsg_close_table(b, c);
  491. }
  492. }
  493. void
  494. proto_init_interface(struct interface *iface, struct blob_attr *attr)
  495. {
  496. const struct proto_handler *proto = iface->proto_handler;
  497. struct interface_proto_state *state = NULL;
  498. if (!proto)
  499. proto = &no_proto;
  500. state = proto->attach(proto, iface, attr);
  501. if (!state) {
  502. state = no_proto.attach(&no_proto, iface, attr);
  503. state->cb = invalid_proto_handler;
  504. }
  505. state->handler = proto;
  506. interface_set_proto_state(iface, state);
  507. }
  508. void
  509. proto_attach_interface(struct interface *iface, const char *proto_name)
  510. {
  511. const struct proto_handler *proto = &no_proto;
  512. const char *error = NULL;
  513. if (proto_name) {
  514. proto = get_proto_handler(proto_name);
  515. if (!proto) {
  516. error = "INVALID_PROTO";
  517. proto = &no_proto;
  518. }
  519. }
  520. iface->proto_handler = proto;
  521. if (error)
  522. interface_add_error(iface, "proto", error, NULL, 0);
  523. }
  524. int
  525. interface_proto_event(struct interface_proto_state *proto,
  526. enum interface_proto_cmd cmd, bool force)
  527. {
  528. enum interface_proto_event ev;
  529. int ret;
  530. ret = proto->cb(proto, cmd, force);
  531. if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
  532. goto out;
  533. switch(cmd) {
  534. case PROTO_CMD_SETUP:
  535. ev = IFPEV_UP;
  536. break;
  537. case PROTO_CMD_TEARDOWN:
  538. ev = IFPEV_DOWN;
  539. break;
  540. case PROTO_CMD_RENEW:
  541. ev = IFPEV_RENEW;
  542. break;
  543. default:
  544. return -EINVAL;
  545. }
  546. proto->proto_event(proto, ev);
  547. out:
  548. return ret;
  549. }