1 /* 2 * netifd - network interface daemon 3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 7 * as published by the Free Software Foundation 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 #include <string.h> 15 #include <stdlib.h> 16 #include <stdio.h> 17 18 #include <arpa/inet.h> 19 #include <netinet/in.h> 20 21 #include "netifd.h" 22 #include "interface.h" 23 #include "interface-ip.h" 24 #include "proto.h" 25 #include "system.h" 26 27 struct static_proto_state { 28 struct interface_proto_state proto; 29 30 struct blob_attr *config; 31 }; 32 33 static bool 34 static_proto_setup(struct static_proto_state *state) 35 { 36 struct interface *iface = state->proto.iface; 37 struct device *dev = iface->main_dev.dev; 38 39 interface_set_l3_dev(iface, dev); 40 return proto_apply_static_ip_settings(state->proto.iface, state->config) == 0; 41 } 42 43 static int 44 static_handler(struct interface_proto_state *proto, 45 enum interface_proto_cmd cmd, bool force) 46 { 47 struct static_proto_state *state; 48 int ret = 0; 49 50 state = container_of(proto, struct static_proto_state, proto); 51 52 switch (cmd) { 53 case PROTO_CMD_SETUP: 54 if (!static_proto_setup(state)) 55 return -1; 56 57 break; 58 case PROTO_CMD_TEARDOWN: 59 case PROTO_CMD_RENEW: 60 case PROTO_CMD_RESTART: 61 break; 62 } 63 64 return ret; 65 } 66 67 static void 68 static_free(struct interface_proto_state *proto) 69 { 70 struct static_proto_state *state; 71 72 state = container_of(proto, struct static_proto_state, proto); 73 free(state->config); 74 free(state); 75 } 76 77 static struct interface_proto_state * 78 static_attach(const struct proto_handler *h, struct interface *iface, 79 struct blob_attr *attr) 80 { 81 struct static_proto_state *state; 82 83 state = calloc(1, sizeof(*state)); 84 if (!state) 85 return NULL; 86 87 state->config = malloc(blob_pad_len(attr)); 88 if (!state->config) 89 goto error; 90 91 memcpy(state->config, attr, blob_pad_len(attr)); 92 state->proto.free = static_free; 93 state->proto.cb = static_handler; 94 95 return &state->proto; 96 97 error: 98 free(state); 99 return NULL; 100 } 101 102 static struct proto_handler static_proto = { 103 .name = "static", 104 .flags = PROTO_FLAG_IMMEDIATE | 105 PROTO_FLAG_FORCE_LINK_DEFAULT, 106 .config_params = &proto_ip_attr, 107 .attach = static_attach, 108 }; 109 110 static void __init 111 static_proto_init(void) 112 { 113 add_proto_handler(&static_proto); 114 } 115
This page was automatically generated by LXR 0.3.1. • OpenWrt