stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1 | /* |
Rami Rosen | eb5ce43 | 2012-11-13 13:29:15 +0000 | [diff] [blame] | 2 | * VXLAN: Virtual eXtensible Local Area Network |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 3 | * |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 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 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * TODO |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 11 | * - IPv6 (not in RFC) |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/in.h> |
| 25 | #include <linux/ip.h> |
| 26 | #include <linux/udp.h> |
| 27 | #include <linux/igmp.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/if_ether.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 30 | #include <linux/hash.h> |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 31 | #include <linux/ethtool.h> |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 32 | #include <net/arp.h> |
| 33 | #include <net/ndisc.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 34 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 35 | #include <net/ip_tunnels.h> |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 36 | #include <net/icmp.h> |
| 37 | #include <net/udp.h> |
| 38 | #include <net/rtnetlink.h> |
| 39 | #include <net/route.h> |
| 40 | #include <net/dsfield.h> |
| 41 | #include <net/inet_ecn.h> |
| 42 | #include <net/net_namespace.h> |
| 43 | #include <net/netns/generic.h> |
| 44 | |
| 45 | #define VXLAN_VERSION "0.1" |
| 46 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 47 | #define PORT_HASH_BITS 8 |
| 48 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 49 | #define VNI_HASH_BITS 10 |
| 50 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 51 | #define FDB_HASH_BITS 8 |
| 52 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) |
| 53 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
| 54 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ |
| 55 | |
| 56 | #define VXLAN_N_VID (1u << 24) |
| 57 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) |
Alexander Duyck | 52b702f | 2012-11-09 13:35:24 +0000 | [diff] [blame] | 58 | /* IP header + UDP + VXLAN + Ethernet header */ |
| 59 | #define VXLAN_HEADROOM (20 + 8 + 8 + 14) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 60 | |
| 61 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ |
| 62 | |
| 63 | /* VXLAN protocol header */ |
| 64 | struct vxlanhdr { |
| 65 | __be32 vx_flags; |
| 66 | __be32 vx_vni; |
| 67 | }; |
| 68 | |
stephen hemminger | 23c578b | 2013-04-27 11:31:53 +0000 | [diff] [blame] | 69 | /* UDP port for VXLAN traffic. |
| 70 | * The IANA assigned port is 4789, but the Linux default is 8472 |
| 71 | * for compatability with early adopters. |
| 72 | */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 73 | static unsigned int vxlan_port __read_mostly = 8472; |
| 74 | module_param_named(udp_port, vxlan_port, uint, 0444); |
| 75 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
| 76 | |
| 77 | static bool log_ecn_error = true; |
| 78 | module_param(log_ecn_error, bool, 0644); |
| 79 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 80 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 81 | static unsigned int vxlan_net_id; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 82 | |
| 83 | /* per UDP socket information */ |
| 84 | struct vxlan_sock { |
| 85 | struct hlist_node hlist; |
| 86 | struct rcu_head rcu; |
| 87 | struct work_struct del_work; |
| 88 | unsigned int refcnt; |
| 89 | struct socket *sock; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 90 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
| 91 | }; |
| 92 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 93 | /* per-network namespace private data for this module */ |
| 94 | struct vxlan_net { |
| 95 | struct list_head vxlan_list; |
| 96 | struct hlist_head sock_list[PORT_HASH_SIZE]; |
| 97 | }; |
| 98 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 99 | struct vxlan_rdst { |
| 100 | struct rcu_head rcu; |
| 101 | __be32 remote_ip; |
| 102 | __be16 remote_port; |
| 103 | u32 remote_vni; |
| 104 | u32 remote_ifindex; |
| 105 | struct vxlan_rdst *remote_next; |
| 106 | }; |
| 107 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 108 | /* Forwarding table entry */ |
| 109 | struct vxlan_fdb { |
| 110 | struct hlist_node hlist; /* linked list of entries */ |
| 111 | struct rcu_head rcu; |
| 112 | unsigned long updated; /* jiffies */ |
| 113 | unsigned long used; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 114 | struct vxlan_rdst remote; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 115 | u16 state; /* see ndm_state */ |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 116 | u8 flags; /* see ndm_flags */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 117 | u8 eth_addr[ETH_ALEN]; |
| 118 | }; |
| 119 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 120 | /* Pseudo network device */ |
| 121 | struct vxlan_dev { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 122 | struct hlist_node hlist; /* vni hash table */ |
| 123 | struct list_head next; /* vxlan's per namespace list */ |
| 124 | struct vxlan_sock *vn_sock; /* listening socket */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 125 | struct net_device *dev; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 126 | struct vxlan_rdst default_dst; /* default destination */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 127 | __be32 saddr; /* source address */ |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 128 | __be16 dst_port; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 129 | __u16 port_min; /* source port range */ |
| 130 | __u16 port_max; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 131 | __u8 tos; /* TOS override */ |
| 132 | __u8 ttl; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 133 | u32 flags; /* VXLAN_F_* below */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 134 | |
| 135 | unsigned long age_interval; |
| 136 | struct timer_list age_timer; |
| 137 | spinlock_t hash_lock; |
| 138 | unsigned int addrcnt; |
| 139 | unsigned int addrmax; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 140 | |
| 141 | struct hlist_head fdb_head[FDB_HASH_SIZE]; |
| 142 | }; |
| 143 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 144 | #define VXLAN_F_LEARN 0x01 |
| 145 | #define VXLAN_F_PROXY 0x02 |
| 146 | #define VXLAN_F_RSC 0x04 |
| 147 | #define VXLAN_F_L2MISS 0x08 |
| 148 | #define VXLAN_F_L3MISS 0x10 |
| 149 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 150 | /* salt for hash table */ |
| 151 | static u32 vxlan_salt __read_mostly; |
| 152 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 153 | /* Virtual Network hash table head */ |
| 154 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) |
| 155 | { |
| 156 | return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; |
| 157 | } |
| 158 | |
| 159 | /* Socket hash table head */ |
| 160 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 161 | { |
| 162 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
| 163 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 164 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
| 165 | } |
| 166 | |
| 167 | /* Find VXLAN socket based on network namespace and UDP port */ |
| 168 | static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port) |
| 169 | { |
| 170 | struct vxlan_sock *vs; |
| 171 | |
| 172 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { |
| 173 | if (inet_sk(vs->sock->sk)->inet_sport == port) |
| 174 | return vs; |
| 175 | } |
| 176 | return NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Look up VNI in a per net namespace table */ |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 180 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 181 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 182 | struct vxlan_sock *vs; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 183 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 184 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 185 | vs = vxlan_find_port(net, port); |
| 186 | if (!vs) |
| 187 | return NULL; |
| 188 | |
| 189 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 190 | if (vxlan->default_dst.remote_vni == id) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 191 | return vxlan; |
| 192 | } |
| 193 | |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | /* Fill in neighbour message in skbuff. */ |
| 198 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, |
| 199 | const struct vxlan_fdb *fdb, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 200 | u32 portid, u32 seq, int type, unsigned int flags, |
| 201 | const struct vxlan_rdst *rdst) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 202 | { |
| 203 | unsigned long now = jiffies; |
| 204 | struct nda_cacheinfo ci; |
| 205 | struct nlmsghdr *nlh; |
| 206 | struct ndmsg *ndm; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 207 | bool send_ip, send_eth; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 208 | |
| 209 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 210 | if (nlh == NULL) |
| 211 | return -EMSGSIZE; |
| 212 | |
| 213 | ndm = nlmsg_data(nlh); |
| 214 | memset(ndm, 0, sizeof(*ndm)); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 215 | |
| 216 | send_eth = send_ip = true; |
| 217 | |
| 218 | if (type == RTM_GETNEIGH) { |
| 219 | ndm->ndm_family = AF_INET; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 220 | send_ip = rdst->remote_ip != htonl(INADDR_ANY); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 221 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
| 222 | } else |
| 223 | ndm->ndm_family = AF_BRIDGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 224 | ndm->ndm_state = fdb->state; |
| 225 | ndm->ndm_ifindex = vxlan->dev->ifindex; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 226 | ndm->ndm_flags = fdb->flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 227 | ndm->ndm_type = NDA_DST; |
| 228 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 229 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 230 | goto nla_put_failure; |
| 231 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 232 | if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip)) |
| 233 | goto nla_put_failure; |
| 234 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 235 | if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 236 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
| 237 | goto nla_put_failure; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 238 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 239 | nla_put_be32(skb, NDA_VNI, rdst->remote_vni)) |
| 240 | goto nla_put_failure; |
| 241 | if (rdst->remote_ifindex && |
| 242 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 243 | goto nla_put_failure; |
| 244 | |
| 245 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 246 | ci.ndm_confirmed = 0; |
| 247 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 248 | ci.ndm_refcnt = 0; |
| 249 | |
| 250 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 251 | goto nla_put_failure; |
| 252 | |
| 253 | return nlmsg_end(skb, nlh); |
| 254 | |
| 255 | nla_put_failure: |
| 256 | nlmsg_cancel(skb, nlh); |
| 257 | return -EMSGSIZE; |
| 258 | } |
| 259 | |
| 260 | static inline size_t vxlan_nlmsg_size(void) |
| 261 | { |
| 262 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 263 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
| 264 | + nla_total_size(sizeof(__be32)) /* NDA_DST */ |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 265 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 266 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
| 267 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 268 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 269 | } |
| 270 | |
| 271 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, |
| 272 | const struct vxlan_fdb *fdb, int type) |
| 273 | { |
| 274 | struct net *net = dev_net(vxlan->dev); |
| 275 | struct sk_buff *skb; |
| 276 | int err = -ENOBUFS; |
| 277 | |
| 278 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); |
| 279 | if (skb == NULL) |
| 280 | goto errout; |
| 281 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 282 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 283 | if (err < 0) { |
| 284 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ |
| 285 | WARN_ON(err == -EMSGSIZE); |
| 286 | kfree_skb(skb); |
| 287 | goto errout; |
| 288 | } |
| 289 | |
| 290 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 291 | return; |
| 292 | errout: |
| 293 | if (err < 0) |
| 294 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 295 | } |
| 296 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 297 | static void vxlan_ip_miss(struct net_device *dev, __be32 ipa) |
| 298 | { |
| 299 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 300 | struct vxlan_fdb f; |
| 301 | |
| 302 | memset(&f, 0, sizeof f); |
| 303 | f.state = NUD_STALE; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 304 | f.remote.remote_ip = ipa; /* goes to NDA_DST */ |
| 305 | f.remote.remote_vni = VXLAN_N_VID; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 306 | |
| 307 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 308 | } |
| 309 | |
| 310 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) |
| 311 | { |
| 312 | struct vxlan_fdb f; |
| 313 | |
| 314 | memset(&f, 0, sizeof f); |
| 315 | f.state = NUD_STALE; |
| 316 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
| 317 | |
| 318 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); |
| 319 | } |
| 320 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 321 | /* Hash Ethernet address */ |
| 322 | static u32 eth_hash(const unsigned char *addr) |
| 323 | { |
| 324 | u64 value = get_unaligned((u64 *)addr); |
| 325 | |
| 326 | /* only want 6 bytes */ |
| 327 | #ifdef __BIG_ENDIAN |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 328 | value >>= 16; |
stephen hemminger | 321fb99 | 2012-10-09 20:35:47 +0000 | [diff] [blame] | 329 | #else |
| 330 | value <<= 16; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 331 | #endif |
| 332 | return hash_64(value, FDB_HASH_BITS); |
| 333 | } |
| 334 | |
| 335 | /* Hash chain to use given mac address */ |
| 336 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, |
| 337 | const u8 *mac) |
| 338 | { |
| 339 | return &vxlan->fdb_head[eth_hash(mac)]; |
| 340 | } |
| 341 | |
| 342 | /* Look up Ethernet address in forwarding table */ |
| 343 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
| 344 | const u8 *mac) |
| 345 | |
| 346 | { |
| 347 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); |
| 348 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 349 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 350 | hlist_for_each_entry_rcu(f, head, hlist) { |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 351 | if (compare_ether_addr(mac, f->eth_addr) == 0) |
| 352 | return f; |
| 353 | } |
| 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 358 | /* Add/update destinations for multicast */ |
| 359 | static int vxlan_fdb_append(struct vxlan_fdb *f, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 360 | __be32 ip, __be16 port, __u32 vni, __u32 ifindex) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 361 | { |
| 362 | struct vxlan_rdst *rd_prev, *rd; |
| 363 | |
| 364 | rd_prev = NULL; |
| 365 | for (rd = &f->remote; rd; rd = rd->remote_next) { |
| 366 | if (rd->remote_ip == ip && |
| 367 | rd->remote_port == port && |
| 368 | rd->remote_vni == vni && |
| 369 | rd->remote_ifindex == ifindex) |
| 370 | return 0; |
| 371 | rd_prev = rd; |
| 372 | } |
| 373 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
| 374 | if (rd == NULL) |
| 375 | return -ENOBUFS; |
| 376 | rd->remote_ip = ip; |
| 377 | rd->remote_port = port; |
| 378 | rd->remote_vni = vni; |
| 379 | rd->remote_ifindex = ifindex; |
| 380 | rd->remote_next = NULL; |
| 381 | rd_prev->remote_next = rd; |
| 382 | return 1; |
| 383 | } |
| 384 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 385 | /* Add new entry to forwarding table -- assumes lock held */ |
| 386 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
| 387 | const u8 *mac, __be32 ip, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 388 | __u16 state, __u16 flags, |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 389 | __be16 port, __u32 vni, __u32 ifindex, |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 390 | __u8 ndm_flags) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 391 | { |
| 392 | struct vxlan_fdb *f; |
| 393 | int notify = 0; |
| 394 | |
| 395 | f = vxlan_find_mac(vxlan, mac); |
| 396 | if (f) { |
| 397 | if (flags & NLM_F_EXCL) { |
| 398 | netdev_dbg(vxlan->dev, |
| 399 | "lost race to create %pM\n", mac); |
| 400 | return -EEXIST; |
| 401 | } |
| 402 | if (f->state != state) { |
| 403 | f->state = state; |
| 404 | f->updated = jiffies; |
| 405 | notify = 1; |
| 406 | } |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 407 | if (f->flags != ndm_flags) { |
| 408 | f->flags = ndm_flags; |
| 409 | f->updated = jiffies; |
| 410 | notify = 1; |
| 411 | } |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 412 | if ((flags & NLM_F_APPEND) && |
| 413 | is_multicast_ether_addr(f->eth_addr)) { |
| 414 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); |
| 415 | |
| 416 | if (rc < 0) |
| 417 | return rc; |
| 418 | notify |= rc; |
| 419 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 420 | } else { |
| 421 | if (!(flags & NLM_F_CREATE)) |
| 422 | return -ENOENT; |
| 423 | |
| 424 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) |
| 425 | return -ENOSPC; |
| 426 | |
| 427 | netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip); |
| 428 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 429 | if (!f) |
| 430 | return -ENOMEM; |
| 431 | |
| 432 | notify = 1; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 433 | f->remote.remote_ip = ip; |
| 434 | f->remote.remote_port = port; |
| 435 | f->remote.remote_vni = vni; |
| 436 | f->remote.remote_ifindex = ifindex; |
| 437 | f->remote.remote_next = NULL; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 438 | f->state = state; |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 439 | f->flags = ndm_flags; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 440 | f->updated = f->used = jiffies; |
| 441 | memcpy(f->eth_addr, mac, ETH_ALEN); |
| 442 | |
| 443 | ++vxlan->addrcnt; |
| 444 | hlist_add_head_rcu(&f->hlist, |
| 445 | vxlan_fdb_head(vxlan, mac)); |
| 446 | } |
| 447 | |
| 448 | if (notify) |
| 449 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Wei Yongjun | 6706c82 | 2013-04-11 19:00:35 +0000 | [diff] [blame] | 454 | static void vxlan_fdb_free(struct rcu_head *head) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 455 | { |
| 456 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); |
| 457 | |
| 458 | while (f->remote.remote_next) { |
| 459 | struct vxlan_rdst *rd = f->remote.remote_next; |
| 460 | |
| 461 | f->remote.remote_next = rd->remote_next; |
| 462 | kfree(rd); |
| 463 | } |
| 464 | kfree(f); |
| 465 | } |
| 466 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 467 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
| 468 | { |
| 469 | netdev_dbg(vxlan->dev, |
| 470 | "delete %pM\n", f->eth_addr); |
| 471 | |
| 472 | --vxlan->addrcnt; |
| 473 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); |
| 474 | |
| 475 | hlist_del_rcu(&f->hlist); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 476 | call_rcu(&f->rcu, vxlan_fdb_free); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Add static entry (via netlink) */ |
| 480 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 481 | struct net_device *dev, |
| 482 | const unsigned char *addr, u16 flags) |
| 483 | { |
| 484 | struct vxlan_dev *vxlan = netdev_priv(dev); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 485 | struct net *net = dev_net(vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 486 | __be32 ip; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 487 | __be16 port; |
| 488 | u32 vni, ifindex; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 489 | int err; |
| 490 | |
| 491 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { |
| 492 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", |
| 493 | ndm->ndm_state); |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | |
| 497 | if (tb[NDA_DST] == NULL) |
| 498 | return -EINVAL; |
| 499 | |
| 500 | if (nla_len(tb[NDA_DST]) != sizeof(__be32)) |
| 501 | return -EAFNOSUPPORT; |
| 502 | |
| 503 | ip = nla_get_be32(tb[NDA_DST]); |
| 504 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 505 | if (tb[NDA_PORT]) { |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 506 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 507 | return -EINVAL; |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 508 | port = nla_get_be16(tb[NDA_PORT]); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 509 | } else |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 510 | port = vxlan->dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 511 | |
| 512 | if (tb[NDA_VNI]) { |
| 513 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) |
| 514 | return -EINVAL; |
| 515 | vni = nla_get_u32(tb[NDA_VNI]); |
| 516 | } else |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 517 | vni = vxlan->default_dst.remote_vni; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 518 | |
| 519 | if (tb[NDA_IFINDEX]) { |
Pravin B Shelar | 5abb002 | 2013-03-26 08:29:30 +0000 | [diff] [blame] | 520 | struct net_device *tdev; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 521 | |
| 522 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) |
| 523 | return -EINVAL; |
| 524 | ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
Pravin B Shelar | 5abb002 | 2013-03-26 08:29:30 +0000 | [diff] [blame] | 525 | tdev = dev_get_by_index(net, ifindex); |
| 526 | if (!tdev) |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 527 | return -EADDRNOTAVAIL; |
Pravin B Shelar | 5abb002 | 2013-03-26 08:29:30 +0000 | [diff] [blame] | 528 | dev_put(tdev); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 529 | } else |
| 530 | ifindex = 0; |
| 531 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 532 | spin_lock_bh(&vxlan->hash_lock); |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 533 | err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, |
| 534 | port, vni, ifindex, ndm->ndm_flags); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 535 | spin_unlock_bh(&vxlan->hash_lock); |
| 536 | |
| 537 | return err; |
| 538 | } |
| 539 | |
| 540 | /* Delete entry (via netlink) */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 541 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 542 | struct net_device *dev, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 543 | const unsigned char *addr) |
| 544 | { |
| 545 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 546 | struct vxlan_fdb *f; |
| 547 | int err = -ENOENT; |
| 548 | |
| 549 | spin_lock_bh(&vxlan->hash_lock); |
| 550 | f = vxlan_find_mac(vxlan, addr); |
| 551 | if (f) { |
| 552 | vxlan_fdb_destroy(vxlan, f); |
| 553 | err = 0; |
| 554 | } |
| 555 | spin_unlock_bh(&vxlan->hash_lock); |
| 556 | |
| 557 | return err; |
| 558 | } |
| 559 | |
| 560 | /* Dump forwarding table */ |
| 561 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 562 | struct net_device *dev, int idx) |
| 563 | { |
| 564 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 565 | unsigned int h; |
| 566 | |
| 567 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 568 | struct vxlan_fdb *f; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 569 | int err; |
| 570 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 571 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 572 | struct vxlan_rdst *rd; |
| 573 | for (rd = &f->remote; rd; rd = rd->remote_next) { |
| 574 | if (idx < cb->args[0]) |
| 575 | goto skip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 576 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 577 | err = vxlan_fdb_info(skb, vxlan, f, |
| 578 | NETLINK_CB(cb->skb).portid, |
| 579 | cb->nlh->nlmsg_seq, |
| 580 | RTM_NEWNEIGH, |
| 581 | NLM_F_MULTI, rd); |
| 582 | if (err < 0) |
| 583 | break; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 584 | skip: |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 585 | ++idx; |
| 586 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | return idx; |
| 591 | } |
| 592 | |
| 593 | /* Watch incoming packets to learn mapping between Ethernet address |
| 594 | * and Tunnel endpoint. |
| 595 | */ |
| 596 | static void vxlan_snoop(struct net_device *dev, |
| 597 | __be32 src_ip, const u8 *src_mac) |
| 598 | { |
| 599 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 600 | struct vxlan_fdb *f; |
| 601 | int err; |
| 602 | |
| 603 | f = vxlan_find_mac(vxlan, src_mac); |
| 604 | if (likely(f)) { |
| 605 | f->used = jiffies; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 606 | if (likely(f->remote.remote_ip == src_ip)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 607 | return; |
| 608 | |
| 609 | if (net_ratelimit()) |
| 610 | netdev_info(dev, |
| 611 | "%pM migrated from %pI4 to %pI4\n", |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 612 | src_mac, &f->remote.remote_ip, &src_ip); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 613 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 614 | f->remote.remote_ip = src_ip; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 615 | f->updated = jiffies; |
| 616 | } else { |
| 617 | /* learned new entry */ |
| 618 | spin_lock(&vxlan->hash_lock); |
| 619 | err = vxlan_fdb_create(vxlan, src_mac, src_ip, |
| 620 | NUD_REACHABLE, |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 621 | NLM_F_EXCL|NLM_F_CREATE, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 622 | vxlan->dst_port, |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 623 | vxlan->default_dst.remote_vni, |
| 624 | 0, NTF_SELF); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 625 | spin_unlock(&vxlan->hash_lock); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | |
| 630 | /* See if multicast group is already in use by other ID */ |
| 631 | static bool vxlan_group_used(struct vxlan_net *vn, |
| 632 | const struct vxlan_dev *this) |
| 633 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 634 | struct vxlan_dev *vxlan; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 635 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 636 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
| 637 | if (vxlan == this) |
| 638 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 639 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 640 | if (!netif_running(vxlan->dev)) |
| 641 | continue; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 642 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 643 | if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip) |
| 644 | return true; |
| 645 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 646 | |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | /* kernel equivalent to IP_ADD_MEMBERSHIP */ |
| 651 | static int vxlan_join_group(struct net_device *dev) |
| 652 | { |
| 653 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 654 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 655 | struct sock *sk = vxlan->vn_sock->sock->sk; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 656 | struct ip_mreqn mreq = { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 657 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 658 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 659 | }; |
| 660 | int err; |
| 661 | |
| 662 | /* Already a member of group */ |
| 663 | if (vxlan_group_used(vn, vxlan)) |
| 664 | return 0; |
| 665 | |
| 666 | /* Need to drop RTNL to call multicast join */ |
| 667 | rtnl_unlock(); |
| 668 | lock_sock(sk); |
| 669 | err = ip_mc_join_group(sk, &mreq); |
| 670 | release_sock(sk); |
| 671 | rtnl_lock(); |
| 672 | |
| 673 | return err; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | /* kernel equivalent to IP_DROP_MEMBERSHIP */ |
| 678 | static int vxlan_leave_group(struct net_device *dev) |
| 679 | { |
| 680 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 681 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
| 682 | int err = 0; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 683 | struct sock *sk = vxlan->vn_sock->sock->sk; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 684 | struct ip_mreqn mreq = { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 685 | .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip, |
| 686 | .imr_ifindex = vxlan->default_dst.remote_ifindex, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | /* Only leave group when last vxlan is done. */ |
| 690 | if (vxlan_group_used(vn, vxlan)) |
| 691 | return 0; |
| 692 | |
| 693 | /* Need to drop RTNL to call multicast leave */ |
| 694 | rtnl_unlock(); |
| 695 | lock_sock(sk); |
| 696 | err = ip_mc_leave_group(sk, &mreq); |
| 697 | release_sock(sk); |
| 698 | rtnl_lock(); |
| 699 | |
| 700 | return err; |
| 701 | } |
| 702 | |
| 703 | /* Callback from net/ipv4/udp.c to receive packets */ |
| 704 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 705 | { |
| 706 | struct iphdr *oip; |
| 707 | struct vxlanhdr *vxh; |
| 708 | struct vxlan_dev *vxlan; |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 709 | struct pcpu_tstats *stats; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 710 | __be16 port; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 711 | __u32 vni; |
| 712 | int err; |
| 713 | |
| 714 | /* pop off outer UDP header */ |
| 715 | __skb_pull(skb, sizeof(struct udphdr)); |
| 716 | |
| 717 | /* Need Vxlan and inner Ethernet header to be present */ |
| 718 | if (!pskb_may_pull(skb, sizeof(struct vxlanhdr))) |
| 719 | goto error; |
| 720 | |
| 721 | /* Drop packets with reserved bits set */ |
| 722 | vxh = (struct vxlanhdr *) skb->data; |
| 723 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || |
| 724 | (vxh->vx_vni & htonl(0xff))) { |
| 725 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", |
| 726 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); |
| 727 | goto error; |
| 728 | } |
| 729 | |
| 730 | __skb_pull(skb, sizeof(struct vxlanhdr)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 731 | |
| 732 | /* Is this VNI defined? */ |
| 733 | vni = ntohl(vxh->vx_vni) >> 8; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 734 | port = inet_sk(sk)->inet_sport; |
| 735 | vxlan = vxlan_find_vni(sock_net(sk), vni, port); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 736 | if (!vxlan) { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 737 | netdev_dbg(skb->dev, "unknown vni %d port %u\n", |
| 738 | vni, ntohs(port)); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 739 | goto drop; |
| 740 | } |
| 741 | |
| 742 | if (!pskb_may_pull(skb, ETH_HLEN)) { |
| 743 | vxlan->dev->stats.rx_length_errors++; |
| 744 | vxlan->dev->stats.rx_errors++; |
| 745 | goto drop; |
| 746 | } |
| 747 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 748 | skb_reset_mac_header(skb); |
| 749 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 750 | /* Re-examine inner Ethernet packet */ |
| 751 | oip = ip_hdr(skb); |
| 752 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 753 | |
| 754 | /* Ignore packet loops (and multicast echo) */ |
| 755 | if (compare_ether_addr(eth_hdr(skb)->h_source, |
| 756 | vxlan->dev->dev_addr) == 0) |
| 757 | goto drop; |
| 758 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 759 | if (vxlan->flags & VXLAN_F_LEARN) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 760 | vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source); |
| 761 | |
| 762 | __skb_tunnel_rx(skb, vxlan->dev); |
| 763 | skb_reset_network_header(skb); |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 764 | |
| 765 | /* If the NIC driver gave us an encapsulated packet with |
| 766 | * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, |
| 767 | * leave the CHECKSUM_UNNECESSARY, the device checksummed it |
| 768 | * for us. Otherwise force the upper layers to verify it. |
| 769 | */ |
| 770 | if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || |
| 771 | !(vxlan->dev->features & NETIF_F_RXCSUM)) |
| 772 | skb->ip_summed = CHECKSUM_NONE; |
| 773 | |
| 774 | skb->encapsulation = 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 775 | |
| 776 | err = IP_ECN_decapsulate(oip, skb); |
| 777 | if (unlikely(err)) { |
| 778 | if (log_ecn_error) |
| 779 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", |
| 780 | &oip->saddr, oip->tos); |
| 781 | if (err > 1) { |
| 782 | ++vxlan->dev->stats.rx_frame_errors; |
| 783 | ++vxlan->dev->stats.rx_errors; |
| 784 | goto drop; |
| 785 | } |
| 786 | } |
| 787 | |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 788 | stats = this_cpu_ptr(vxlan->dev->tstats); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 789 | u64_stats_update_begin(&stats->syncp); |
| 790 | stats->rx_packets++; |
| 791 | stats->rx_bytes += skb->len; |
| 792 | u64_stats_update_end(&stats->syncp); |
| 793 | |
| 794 | netif_rx(skb); |
| 795 | |
| 796 | return 0; |
| 797 | error: |
| 798 | /* Put UDP header back */ |
| 799 | __skb_push(skb, sizeof(struct udphdr)); |
| 800 | |
| 801 | return 1; |
| 802 | drop: |
| 803 | /* Consume bad packet */ |
| 804 | kfree_skb(skb); |
| 805 | return 0; |
| 806 | } |
| 807 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 808 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
| 809 | { |
| 810 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 811 | struct arphdr *parp; |
| 812 | u8 *arpptr, *sha; |
| 813 | __be32 sip, tip; |
| 814 | struct neighbour *n; |
| 815 | |
| 816 | if (dev->flags & IFF_NOARP) |
| 817 | goto out; |
| 818 | |
| 819 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { |
| 820 | dev->stats.tx_dropped++; |
| 821 | goto out; |
| 822 | } |
| 823 | parp = arp_hdr(skb); |
| 824 | |
| 825 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && |
| 826 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 827 | parp->ar_pro != htons(ETH_P_IP) || |
| 828 | parp->ar_op != htons(ARPOP_REQUEST) || |
| 829 | parp->ar_hln != dev->addr_len || |
| 830 | parp->ar_pln != 4) |
| 831 | goto out; |
| 832 | arpptr = (u8 *)parp + sizeof(struct arphdr); |
| 833 | sha = arpptr; |
| 834 | arpptr += dev->addr_len; /* sha */ |
| 835 | memcpy(&sip, arpptr, sizeof(sip)); |
| 836 | arpptr += sizeof(sip); |
| 837 | arpptr += dev->addr_len; /* tha */ |
| 838 | memcpy(&tip, arpptr, sizeof(tip)); |
| 839 | |
| 840 | if (ipv4_is_loopback(tip) || |
| 841 | ipv4_is_multicast(tip)) |
| 842 | goto out; |
| 843 | |
| 844 | n = neigh_lookup(&arp_tbl, &tip, dev); |
| 845 | |
| 846 | if (n) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 847 | struct vxlan_fdb *f; |
| 848 | struct sk_buff *reply; |
| 849 | |
| 850 | if (!(n->nud_state & NUD_CONNECTED)) { |
| 851 | neigh_release(n); |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | f = vxlan_find_mac(vxlan, n->ha); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 856 | if (f && f->remote.remote_ip == htonl(INADDR_ANY)) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 857 | /* bridge-local neighbor */ |
| 858 | neigh_release(n); |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, |
| 863 | n->ha, sha); |
| 864 | |
| 865 | neigh_release(n); |
| 866 | |
| 867 | skb_reset_mac_header(reply); |
| 868 | __skb_pull(reply, skb_network_offset(reply)); |
| 869 | reply->ip_summed = CHECKSUM_UNNECESSARY; |
| 870 | reply->pkt_type = PACKET_HOST; |
| 871 | |
| 872 | if (netif_rx_ni(reply) == NET_RX_DROP) |
| 873 | dev->stats.rx_dropped++; |
| 874 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 875 | vxlan_ip_miss(dev, tip); |
| 876 | out: |
| 877 | consume_skb(skb); |
| 878 | return NETDEV_TX_OK; |
| 879 | } |
| 880 | |
| 881 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
| 882 | { |
| 883 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 884 | struct neighbour *n; |
| 885 | struct iphdr *pip; |
| 886 | |
| 887 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 888 | return false; |
| 889 | |
| 890 | n = NULL; |
| 891 | switch (ntohs(eth_hdr(skb)->h_proto)) { |
| 892 | case ETH_P_IP: |
| 893 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 894 | return false; |
| 895 | pip = ip_hdr(skb); |
| 896 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); |
| 897 | break; |
| 898 | default: |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | if (n) { |
| 903 | bool diff; |
| 904 | |
| 905 | diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0; |
| 906 | if (diff) { |
| 907 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, |
| 908 | dev->addr_len); |
| 909 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); |
| 910 | } |
| 911 | neigh_release(n); |
| 912 | return diff; |
| 913 | } else if (vxlan->flags & VXLAN_F_L3MISS) |
| 914 | vxlan_ip_miss(dev, pip->daddr); |
| 915 | return false; |
| 916 | } |
| 917 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 918 | static void vxlan_sock_put(struct sk_buff *skb) |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 919 | { |
| 920 | sock_put(skb->sk); |
| 921 | } |
| 922 | |
| 923 | /* On transmit, associate with the tunnel socket */ |
| 924 | static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb) |
| 925 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 926 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 927 | struct sock *sk = vxlan->vn_sock->sock->sk; |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 928 | |
| 929 | skb_orphan(skb); |
| 930 | sock_hold(sk); |
| 931 | skb->sk = sk; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 932 | skb->destructor = vxlan_sock_put; |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 933 | } |
| 934 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 935 | /* Compute source port for outgoing packet |
| 936 | * first choice to use L4 flow hash since it will spread |
| 937 | * better and maybe available from hardware |
| 938 | * secondary choice is to use jhash on the Ethernet header |
| 939 | */ |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 940 | static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb) |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 941 | { |
| 942 | unsigned int range = (vxlan->port_max - vxlan->port_min) + 1; |
| 943 | u32 hash; |
| 944 | |
| 945 | hash = skb_get_rxhash(skb); |
| 946 | if (!hash) |
| 947 | hash = jhash(skb->data, 2 * ETH_ALEN, |
| 948 | (__force u32) skb->protocol); |
| 949 | |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 950 | return htons((((u64) hash * range) >> 32) + vxlan->port_min); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 953 | static int handle_offloads(struct sk_buff *skb) |
| 954 | { |
| 955 | if (skb_is_gso(skb)) { |
| 956 | int err = skb_unclone(skb, GFP_ATOMIC); |
| 957 | if (unlikely(err)) |
| 958 | return err; |
| 959 | |
Dmitry Kravkov | f6ace50 | 2013-04-28 08:16:01 +0000 | [diff] [blame] | 960 | skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 961 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 962 | skb->ip_summed = CHECKSUM_NONE; |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 967 | /* Bypass encapsulation if the destination is local */ |
| 968 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, |
| 969 | struct vxlan_dev *dst_vxlan) |
| 970 | { |
| 971 | struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
| 972 | struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); |
| 973 | |
| 974 | skb->pkt_type = PACKET_HOST; |
| 975 | skb->encapsulation = 0; |
| 976 | skb->dev = dst_vxlan->dev; |
| 977 | __skb_pull(skb, skb_network_offset(skb)); |
| 978 | |
| 979 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
Mike Rapoport | 9d9f163 | 2013-04-13 23:21:39 +0000 | [diff] [blame] | 980 | vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK), |
| 981 | eth_hdr(skb)->h_source); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 982 | |
| 983 | u64_stats_update_begin(&tx_stats->syncp); |
| 984 | tx_stats->tx_packets++; |
| 985 | tx_stats->tx_bytes += skb->len; |
| 986 | u64_stats_update_end(&tx_stats->syncp); |
| 987 | |
| 988 | if (netif_rx(skb) == NET_RX_SUCCESS) { |
| 989 | u64_stats_update_begin(&rx_stats->syncp); |
| 990 | rx_stats->rx_packets++; |
| 991 | rx_stats->rx_bytes += skb->len; |
| 992 | u64_stats_update_end(&rx_stats->syncp); |
| 993 | } else { |
| 994 | skb->dev->stats.rx_dropped++; |
| 995 | } |
| 996 | } |
| 997 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 998 | static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
| 999 | struct vxlan_rdst *rdst, bool did_rsc) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1000 | { |
| 1001 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1002 | struct rtable *rt; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1003 | const struct iphdr *old_iph; |
| 1004 | struct iphdr *iph; |
| 1005 | struct vxlanhdr *vxh; |
| 1006 | struct udphdr *uh; |
| 1007 | struct flowi4 fl4; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1008 | __be32 dst; |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 1009 | __be16 src_port, dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1010 | u32 vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1011 | __be16 df = 0; |
| 1012 | __u8 tos, ttl; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1013 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1014 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1015 | vni = rdst->remote_vni; |
| 1016 | dst = rdst->remote_ip; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (!dst) { |
| 1019 | if (did_rsc) { |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1020 | /* short-circuited back to local bridge */ |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1021 | vxlan_encap_bypass(skb, vxlan, vxlan); |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1022 | return NETDEV_TX_OK; |
| 1023 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1024 | goto drop; |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1025 | } |
stephen hemminger | ef59feb | 2012-10-09 20:35:46 +0000 | [diff] [blame] | 1026 | |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 1027 | if (!skb->encapsulation) { |
| 1028 | skb_reset_inner_headers(skb); |
| 1029 | skb->encapsulation = 1; |
| 1030 | } |
| 1031 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1032 | /* Need space for new headers (invalidates iph ptr) */ |
| 1033 | if (skb_cow_head(skb, VXLAN_HEADROOM)) |
| 1034 | goto drop; |
| 1035 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1036 | old_iph = ip_hdr(skb); |
| 1037 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1038 | ttl = vxlan->ttl; |
| 1039 | if (!ttl && IN_MULTICAST(ntohl(dst))) |
| 1040 | ttl = 1; |
| 1041 | |
| 1042 | tos = vxlan->tos; |
| 1043 | if (tos == 1) |
Pravin B Shelar | 206aaaf | 2013-03-25 14:49:53 +0000 | [diff] [blame] | 1044 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1045 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1046 | src_port = vxlan_src_port(vxlan, skb); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1047 | |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1048 | memset(&fl4, 0, sizeof(fl4)); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1049 | fl4.flowi4_oif = rdst->remote_ifindex; |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1050 | fl4.flowi4_tos = RT_TOS(tos); |
| 1051 | fl4.daddr = dst; |
| 1052 | fl4.saddr = vxlan->saddr; |
| 1053 | |
| 1054 | rt = ip_route_output_key(dev_net(dev), &fl4); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1055 | if (IS_ERR(rt)) { |
| 1056 | netdev_dbg(dev, "no route to %pI4\n", &dst); |
| 1057 | dev->stats.tx_carrier_errors++; |
| 1058 | goto tx_error; |
| 1059 | } |
| 1060 | |
| 1061 | if (rt->dst.dev == dev) { |
| 1062 | netdev_dbg(dev, "circular route to %pI4\n", &dst); |
| 1063 | ip_rt_put(rt); |
| 1064 | dev->stats.collisions++; |
| 1065 | goto tx_error; |
| 1066 | } |
| 1067 | |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1068 | /* Bypass encapsulation if the destination is local */ |
Mike Rapoport | ab09a6d | 2013-04-13 23:21:51 +0000 | [diff] [blame] | 1069 | if (rt->rt_flags & RTCF_LOCAL && |
| 1070 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1071 | struct vxlan_dev *dst_vxlan; |
| 1072 | |
| 1073 | ip_rt_put(rt); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1074 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); |
Sridhar Samudrala | 9dcc71e | 2013-04-02 12:31:52 +0000 | [diff] [blame] | 1075 | if (!dst_vxlan) |
| 1076 | goto tx_error; |
| 1077 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); |
| 1078 | return NETDEV_TX_OK; |
| 1079 | } |
| 1080 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1081 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 1082 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | |
| 1083 | IPSKB_REROUTED); |
| 1084 | skb_dst_drop(skb); |
| 1085 | skb_dst_set(skb, &rt->dst); |
| 1086 | |
| 1087 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
| 1088 | vxh->vx_flags = htonl(VXLAN_FLAGS); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1089 | vxh->vx_vni = htonl(vni << 8); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1090 | |
| 1091 | __skb_push(skb, sizeof(*uh)); |
| 1092 | skb_reset_transport_header(skb); |
| 1093 | uh = udp_hdr(skb); |
| 1094 | |
stephen hemminger | 73cf331 | 2013-04-27 11:31:54 +0000 | [diff] [blame] | 1095 | uh->dest = dst_port; |
stephen hemminger | 7d836a7 | 2013-04-27 11:31:56 +0000 | [diff] [blame] | 1096 | uh->source = src_port; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1097 | |
| 1098 | uh->len = htons(skb->len); |
| 1099 | uh->check = 0; |
| 1100 | |
| 1101 | __skb_push(skb, sizeof(*iph)); |
| 1102 | skb_reset_network_header(skb); |
| 1103 | iph = ip_hdr(skb); |
| 1104 | iph->version = 4; |
| 1105 | iph->ihl = sizeof(struct iphdr) >> 2; |
| 1106 | iph->frag_off = df; |
| 1107 | iph->protocol = IPPROTO_UDP; |
Pravin B Shelar | 206aaaf | 2013-03-25 14:49:53 +0000 | [diff] [blame] | 1108 | iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
stephen hemminger | ca78f18 | 2012-10-09 20:35:48 +0000 | [diff] [blame] | 1109 | iph->daddr = dst; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1110 | iph->saddr = fl4.saddr; |
| 1111 | iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); |
Pravin B Shelar | 8dc98eb | 2013-02-22 07:30:40 +0000 | [diff] [blame] | 1112 | tunnel_ip_select_ident(skb, old_iph, &rt->dst); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1113 | |
Zang MingJie | 88c4c06 | 2013-03-04 06:07:34 +0000 | [diff] [blame] | 1114 | nf_reset(skb); |
| 1115 | |
stephen hemminger | 1cad871 | 2012-10-09 20:35:49 +0000 | [diff] [blame] | 1116 | vxlan_set_owner(dev, skb); |
| 1117 | |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1118 | if (handle_offloads(skb)) |
| 1119 | goto drop; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1120 | |
Cong Wang | 6aed0c8 | 2013-03-09 16:38:39 +0000 | [diff] [blame] | 1121 | iptunnel_xmit(skb, dev); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1122 | return NETDEV_TX_OK; |
| 1123 | |
| 1124 | drop: |
| 1125 | dev->stats.tx_dropped++; |
| 1126 | goto tx_free; |
| 1127 | |
| 1128 | tx_error: |
| 1129 | dev->stats.tx_errors++; |
| 1130 | tx_free: |
| 1131 | dev_kfree_skb(skb); |
| 1132 | return NETDEV_TX_OK; |
| 1133 | } |
| 1134 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1135 | /* Transmit local packets over Vxlan |
| 1136 | * |
| 1137 | * Outer IP header inherits ECN and DF from inner header. |
| 1138 | * Outer UDP destination is the VXLAN assigned port. |
| 1139 | * source port is based on hash of flow |
| 1140 | */ |
| 1141 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1142 | { |
| 1143 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1144 | struct ethhdr *eth; |
| 1145 | bool did_rsc = false; |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1146 | struct vxlan_rdst *rdst0, *rdst; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1147 | struct vxlan_fdb *f; |
| 1148 | int rc1, rc; |
| 1149 | |
| 1150 | skb_reset_mac_header(skb); |
| 1151 | eth = eth_hdr(skb); |
| 1152 | |
| 1153 | if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP) |
| 1154 | return arp_reduce(dev, skb); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1155 | |
| 1156 | f = vxlan_find_mac(vxlan, eth->h_dest); |
David Stevens | ae88408 | 2013-04-19 00:36:26 +0000 | [diff] [blame] | 1157 | did_rsc = false; |
| 1158 | |
| 1159 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && |
| 1160 | ntohs(eth->h_proto) == ETH_P_IP) { |
| 1161 | did_rsc = route_shortcircuit(dev, skb); |
| 1162 | if (did_rsc) |
| 1163 | f = vxlan_find_mac(vxlan, eth->h_dest); |
| 1164 | } |
| 1165 | |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1166 | if (f == NULL) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1167 | rdst0 = &vxlan->default_dst; |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1168 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1169 | if (rdst0->remote_ip == htonl(INADDR_ANY) && |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1170 | (vxlan->flags & VXLAN_F_L2MISS) && |
| 1171 | !is_multicast_ether_addr(eth->h_dest)) |
| 1172 | vxlan_fdb_miss(vxlan, eth->h_dest); |
| 1173 | } else |
| 1174 | rdst0 = &f->remote; |
| 1175 | |
| 1176 | rc = NETDEV_TX_OK; |
| 1177 | |
| 1178 | /* if there are multiple destinations, send copies */ |
| 1179 | for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) { |
| 1180 | struct sk_buff *skb1; |
| 1181 | |
| 1182 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 1183 | rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc); |
| 1184 | if (rc == NETDEV_TX_OK) |
| 1185 | rc = rc1; |
| 1186 | } |
| 1187 | |
| 1188 | rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc); |
| 1189 | if (rc == NETDEV_TX_OK) |
| 1190 | rc = rc1; |
| 1191 | return rc; |
| 1192 | } |
| 1193 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1194 | /* Walk the forwarding table and purge stale entries */ |
| 1195 | static void vxlan_cleanup(unsigned long arg) |
| 1196 | { |
| 1197 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; |
| 1198 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
| 1199 | unsigned int h; |
| 1200 | |
| 1201 | if (!netif_running(vxlan->dev)) |
| 1202 | return; |
| 1203 | |
| 1204 | spin_lock_bh(&vxlan->hash_lock); |
| 1205 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1206 | struct hlist_node *p, *n; |
| 1207 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1208 | struct vxlan_fdb *f |
| 1209 | = container_of(p, struct vxlan_fdb, hlist); |
| 1210 | unsigned long timeout; |
| 1211 | |
stephen hemminger | 3c17286 | 2012-10-26 06:24:34 +0000 | [diff] [blame] | 1212 | if (f->state & NUD_PERMANENT) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1213 | continue; |
| 1214 | |
| 1215 | timeout = f->used + vxlan->age_interval * HZ; |
| 1216 | if (time_before_eq(timeout, jiffies)) { |
| 1217 | netdev_dbg(vxlan->dev, |
| 1218 | "garbage collect %pM\n", |
| 1219 | f->eth_addr); |
| 1220 | f->state = NUD_STALE; |
| 1221 | vxlan_fdb_destroy(vxlan, f); |
| 1222 | } else if (time_before(timeout, next_timer)) |
| 1223 | next_timer = timeout; |
| 1224 | } |
| 1225 | } |
| 1226 | spin_unlock_bh(&vxlan->hash_lock); |
| 1227 | |
| 1228 | mod_timer(&vxlan->age_timer, next_timer); |
| 1229 | } |
| 1230 | |
| 1231 | /* Setup stats when device is created */ |
| 1232 | static int vxlan_init(struct net_device *dev) |
| 1233 | { |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1234 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
| 1235 | if (!dev->tstats) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1236 | return -ENOMEM; |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | /* Start ageing timer and join group when device is brought up */ |
| 1242 | static int vxlan_open(struct net_device *dev) |
| 1243 | { |
| 1244 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1245 | int err; |
| 1246 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1247 | if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) { |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1248 | err = vxlan_join_group(dev); |
| 1249 | if (err) |
| 1250 | return err; |
| 1251 | } |
| 1252 | |
| 1253 | if (vxlan->age_interval) |
| 1254 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* Purge the forwarding table */ |
| 1260 | static void vxlan_flush(struct vxlan_dev *vxlan) |
| 1261 | { |
| 1262 | unsigned h; |
| 1263 | |
| 1264 | spin_lock_bh(&vxlan->hash_lock); |
| 1265 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
| 1266 | struct hlist_node *p, *n; |
| 1267 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
| 1268 | struct vxlan_fdb *f |
| 1269 | = container_of(p, struct vxlan_fdb, hlist); |
| 1270 | vxlan_fdb_destroy(vxlan, f); |
| 1271 | } |
| 1272 | } |
| 1273 | spin_unlock_bh(&vxlan->hash_lock); |
| 1274 | } |
| 1275 | |
| 1276 | /* Cleanup timer and forwarding table on shutdown */ |
| 1277 | static int vxlan_stop(struct net_device *dev) |
| 1278 | { |
| 1279 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1280 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1281 | if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1282 | vxlan_leave_group(dev); |
| 1283 | |
| 1284 | del_timer_sync(&vxlan->age_timer); |
| 1285 | |
| 1286 | vxlan_flush(vxlan); |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1291 | /* Stub, nothing needs to be done. */ |
| 1292 | static void vxlan_set_multicast_list(struct net_device *dev) |
| 1293 | { |
| 1294 | } |
| 1295 | |
| 1296 | static const struct net_device_ops vxlan_netdev_ops = { |
| 1297 | .ndo_init = vxlan_init, |
| 1298 | .ndo_open = vxlan_open, |
| 1299 | .ndo_stop = vxlan_stop, |
| 1300 | .ndo_start_xmit = vxlan_xmit, |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1301 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1302 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
| 1303 | .ndo_change_mtu = eth_change_mtu, |
| 1304 | .ndo_validate_addr = eth_validate_addr, |
| 1305 | .ndo_set_mac_address = eth_mac_addr, |
| 1306 | .ndo_fdb_add = vxlan_fdb_add, |
| 1307 | .ndo_fdb_del = vxlan_fdb_delete, |
| 1308 | .ndo_fdb_dump = vxlan_fdb_dump, |
| 1309 | }; |
| 1310 | |
| 1311 | /* Info for udev, that this is a virtual tunnel endpoint */ |
| 1312 | static struct device_type vxlan_type = { |
| 1313 | .name = "vxlan", |
| 1314 | }; |
| 1315 | |
| 1316 | static void vxlan_free(struct net_device *dev) |
| 1317 | { |
Pravin B Shelar | e817104 | 2013-03-25 14:49:46 +0000 | [diff] [blame] | 1318 | free_percpu(dev->tstats); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1319 | free_netdev(dev); |
| 1320 | } |
| 1321 | |
| 1322 | /* Initialize the device structure. */ |
| 1323 | static void vxlan_setup(struct net_device *dev) |
| 1324 | { |
| 1325 | struct vxlan_dev *vxlan = netdev_priv(dev); |
| 1326 | unsigned h; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1327 | int low, high; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1328 | |
| 1329 | eth_hw_addr_random(dev); |
| 1330 | ether_setup(dev); |
stephen hemminger | 2840bf2 | 2012-10-09 20:35:51 +0000 | [diff] [blame] | 1331 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1332 | |
| 1333 | dev->netdev_ops = &vxlan_netdev_ops; |
| 1334 | dev->destructor = vxlan_free; |
| 1335 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
| 1336 | |
| 1337 | dev->tx_queue_len = 0; |
| 1338 | dev->features |= NETIF_F_LLTX; |
| 1339 | dev->features |= NETIF_F_NETNS_LOCAL; |
Joseph Gasparakis | d6727fe | 2012-12-07 14:14:16 +0000 | [diff] [blame] | 1340 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1341 | dev->features |= NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1342 | dev->features |= NETIF_F_GSO_SOFTWARE; |
Joseph Gasparakis | 0afb166 | 2012-12-07 14:14:18 +0000 | [diff] [blame] | 1343 | |
| 1344 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Pravin B Shelar | 05c0db0 | 2013-03-07 13:22:36 +0000 | [diff] [blame] | 1345 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1346 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
stephen hemminger | 6602d00 | 2012-12-31 12:00:21 +0000 | [diff] [blame] | 1347 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1348 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1349 | INIT_LIST_HEAD(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1350 | spin_lock_init(&vxlan->hash_lock); |
| 1351 | |
| 1352 | init_timer_deferrable(&vxlan->age_timer); |
| 1353 | vxlan->age_timer.function = vxlan_cleanup; |
| 1354 | vxlan->age_timer.data = (unsigned long) vxlan; |
| 1355 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1356 | inet_get_local_port_range(&low, &high); |
| 1357 | vxlan->port_min = low; |
| 1358 | vxlan->port_max = high; |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1359 | vxlan->dst_port = htons(vxlan_port); |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1360 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1361 | vxlan->dev = dev; |
| 1362 | |
| 1363 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
| 1364 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); |
| 1365 | } |
| 1366 | |
| 1367 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
| 1368 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1369 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1370 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
| 1371 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
| 1372 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
| 1373 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, |
| 1374 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
| 1375 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, |
| 1376 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1377 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1378 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
| 1379 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, |
| 1380 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, |
| 1381 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1382 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
| 1385 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1386 | { |
| 1387 | if (tb[IFLA_ADDRESS]) { |
| 1388 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { |
| 1389 | pr_debug("invalid link address (not ethernet)\n"); |
| 1390 | return -EINVAL; |
| 1391 | } |
| 1392 | |
| 1393 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { |
| 1394 | pr_debug("invalid all zero ethernet address\n"); |
| 1395 | return -EADDRNOTAVAIL; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (!data) |
| 1400 | return -EINVAL; |
| 1401 | |
| 1402 | if (data[IFLA_VXLAN_ID]) { |
| 1403 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
| 1404 | if (id >= VXLAN_VID_MASK) |
| 1405 | return -ERANGE; |
| 1406 | } |
| 1407 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1408 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1409 | const struct ifla_vxlan_port_range *p |
| 1410 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1411 | |
| 1412 | if (ntohs(p->high) < ntohs(p->low)) { |
| 1413 | pr_debug("port range %u .. %u not valid\n", |
| 1414 | ntohs(p->low), ntohs(p->high)); |
| 1415 | return -EINVAL; |
| 1416 | } |
| 1417 | } |
| 1418 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1422 | static void vxlan_get_drvinfo(struct net_device *netdev, |
| 1423 | struct ethtool_drvinfo *drvinfo) |
| 1424 | { |
| 1425 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); |
| 1426 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); |
| 1427 | } |
| 1428 | |
| 1429 | static const struct ethtool_ops vxlan_ethtool_ops = { |
| 1430 | .get_drvinfo = vxlan_get_drvinfo, |
| 1431 | .get_link = ethtool_op_get_link, |
| 1432 | }; |
| 1433 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1434 | static void vxlan_del_work(struct work_struct *work) |
| 1435 | { |
| 1436 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); |
| 1437 | |
| 1438 | sk_release_kernel(vs->sock->sk); |
| 1439 | kfree_rcu(vs, rcu); |
| 1440 | } |
| 1441 | |
| 1442 | /* Create new listen socket if needed */ |
| 1443 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port) |
| 1444 | { |
| 1445 | struct vxlan_sock *vs; |
| 1446 | struct sock *sk; |
| 1447 | struct sockaddr_in vxlan_addr = { |
| 1448 | .sin_family = AF_INET, |
| 1449 | .sin_addr.s_addr = htonl(INADDR_ANY), |
| 1450 | }; |
| 1451 | int rc; |
| 1452 | unsigned h; |
| 1453 | |
| 1454 | vs = kmalloc(sizeof(*vs), GFP_KERNEL); |
| 1455 | if (!vs) |
| 1456 | return ERR_PTR(-ENOMEM); |
| 1457 | |
| 1458 | for (h = 0; h < VNI_HASH_SIZE; ++h) |
| 1459 | INIT_HLIST_HEAD(&vs->vni_list[h]); |
| 1460 | |
| 1461 | INIT_WORK(&vs->del_work, vxlan_del_work); |
| 1462 | |
| 1463 | /* Create UDP socket for encapsulation receive. */ |
| 1464 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock); |
| 1465 | if (rc < 0) { |
| 1466 | pr_debug("UDP socket create failed\n"); |
| 1467 | kfree(vs); |
| 1468 | return ERR_PTR(rc); |
| 1469 | } |
| 1470 | |
| 1471 | /* Put in proper namespace */ |
| 1472 | sk = vs->sock->sk; |
| 1473 | sk_change_net(sk, net); |
| 1474 | |
| 1475 | vxlan_addr.sin_port = port; |
| 1476 | |
| 1477 | rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr, |
| 1478 | sizeof(vxlan_addr)); |
| 1479 | if (rc < 0) { |
| 1480 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", |
| 1481 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); |
| 1482 | sk_release_kernel(sk); |
| 1483 | kfree(vs); |
| 1484 | return ERR_PTR(rc); |
| 1485 | } |
| 1486 | |
| 1487 | /* Disable multicast loopback */ |
| 1488 | inet_sk(sk)->mc_loop = 0; |
| 1489 | |
| 1490 | /* Mark socket as an encapsulation socket. */ |
| 1491 | udp_sk(sk)->encap_type = 1; |
| 1492 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; |
| 1493 | udp_encap_enable(); |
| 1494 | |
| 1495 | vs->refcnt = 1; |
| 1496 | return vs; |
| 1497 | } |
| 1498 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1499 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
| 1500 | struct nlattr *tb[], struct nlattr *data[]) |
| 1501 | { |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1502 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1503 | struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1504 | struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1505 | struct vxlan_sock *vs; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1506 | __u32 vni; |
| 1507 | int err; |
| 1508 | |
| 1509 | if (!data[IFLA_VXLAN_ID]) |
| 1510 | return -EINVAL; |
| 1511 | |
| 1512 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1513 | dst->remote_vni = vni; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1514 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1515 | if (data[IFLA_VXLAN_GROUP]) |
| 1516 | dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1517 | |
| 1518 | if (data[IFLA_VXLAN_LOCAL]) |
| 1519 | vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); |
| 1520 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1521 | if (data[IFLA_VXLAN_LINK] && |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1522 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1523 | struct net_device *lowerdev |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1524 | = __dev_get_by_index(net, dst->remote_ifindex); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1525 | |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1526 | if (!lowerdev) { |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1527 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1528 | return -ENODEV; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1529 | } |
stephen hemminger | 34e02aa | 2012-10-09 20:35:53 +0000 | [diff] [blame] | 1530 | |
| 1531 | if (!tb[IFLA_MTU]) |
| 1532 | dev->mtu = lowerdev->mtu - VXLAN_HEADROOM; |
Alexander Duyck | 1ba56fb | 2012-11-13 13:10:59 +0000 | [diff] [blame] | 1533 | |
| 1534 | /* update header length based on lower device */ |
| 1535 | dev->hard_header_len = lowerdev->hard_header_len + |
| 1536 | VXLAN_HEADROOM; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | if (data[IFLA_VXLAN_TOS]) |
| 1540 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
| 1541 | |
Vincent Bernat | afb9718 | 2012-10-30 10:27:16 +0000 | [diff] [blame] | 1542 | if (data[IFLA_VXLAN_TTL]) |
| 1543 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
| 1544 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1545 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1546 | vxlan->flags |= VXLAN_F_LEARN; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1547 | |
| 1548 | if (data[IFLA_VXLAN_AGEING]) |
| 1549 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); |
| 1550 | else |
| 1551 | vxlan->age_interval = FDB_AGE_DEFAULT; |
| 1552 | |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1553 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
| 1554 | vxlan->flags |= VXLAN_F_PROXY; |
| 1555 | |
| 1556 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) |
| 1557 | vxlan->flags |= VXLAN_F_RSC; |
| 1558 | |
| 1559 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) |
| 1560 | vxlan->flags |= VXLAN_F_L2MISS; |
| 1561 | |
| 1562 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) |
| 1563 | vxlan->flags |= VXLAN_F_L3MISS; |
| 1564 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1565 | if (data[IFLA_VXLAN_LIMIT]) |
| 1566 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); |
| 1567 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1568 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
| 1569 | const struct ifla_vxlan_port_range *p |
| 1570 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); |
| 1571 | vxlan->port_min = ntohs(p->low); |
| 1572 | vxlan->port_max = ntohs(p->high); |
| 1573 | } |
| 1574 | |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1575 | if (data[IFLA_VXLAN_PORT]) |
| 1576 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); |
| 1577 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1578 | if (vxlan_find_vni(net, vni, vxlan->dst_port)) { |
| 1579 | pr_info("duplicate VNI %u\n", vni); |
| 1580 | return -EEXIST; |
| 1581 | } |
| 1582 | |
| 1583 | vs = vxlan_find_port(net, vxlan->dst_port); |
| 1584 | if (vs) |
| 1585 | ++vs->refcnt; |
| 1586 | else { |
| 1587 | /* Drop lock because socket create acquires RTNL lock */ |
| 1588 | rtnl_unlock(); |
| 1589 | vs = vxlan_socket_create(net, vxlan->dst_port); |
| 1590 | rtnl_lock(); |
| 1591 | if (IS_ERR(vs)) |
| 1592 | return PTR_ERR(vs); |
| 1593 | |
| 1594 | hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port)); |
| 1595 | } |
| 1596 | vxlan->vn_sock = vs; |
| 1597 | |
Yan Burman | 1b13c97 | 2013-01-29 23:43:07 +0000 | [diff] [blame] | 1598 | SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); |
| 1599 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1600 | err = register_netdevice(dev); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1601 | if (err) { |
| 1602 | if (--vs->refcnt == 0) { |
| 1603 | rtnl_unlock(); |
| 1604 | sk_release_kernel(vs->sock->sk); |
| 1605 | kfree(vs); |
| 1606 | rtnl_lock(); |
| 1607 | } |
| 1608 | return err; |
| 1609 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1610 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1611 | list_add(&vxlan->next, &vn->vxlan_list); |
| 1612 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); |
| 1613 | |
| 1614 | return 0; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) |
| 1618 | { |
| 1619 | struct vxlan_dev *vxlan = netdev_priv(dev); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1620 | struct vxlan_sock *vs = vxlan->vn_sock; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1621 | |
| 1622 | hlist_del_rcu(&vxlan->hlist); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1623 | list_del(&vxlan->next); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1624 | unregister_netdevice_queue(dev, head); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1625 | |
| 1626 | if (--vs->refcnt == 0) { |
| 1627 | hlist_del_rcu(&vs->hlist); |
| 1628 | schedule_work(&vs->del_work); |
| 1629 | } |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static size_t vxlan_get_size(const struct net_device *dev) |
| 1633 | { |
| 1634 | |
| 1635 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1636 | nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1637 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
| 1638 | nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */ |
| 1639 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
| 1640 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
| 1641 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1642 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
| 1643 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ |
| 1644 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ |
| 1645 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1646 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
| 1647 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1648 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1649 | nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1650 | 0; |
| 1651 | } |
| 1652 | |
| 1653 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1654 | { |
| 1655 | const struct vxlan_dev *vxlan = netdev_priv(dev); |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1656 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1657 | struct ifla_vxlan_port_range ports = { |
| 1658 | .low = htons(vxlan->port_min), |
| 1659 | .high = htons(vxlan->port_max), |
| 1660 | }; |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1661 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1662 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1663 | goto nla_put_failure; |
| 1664 | |
stephen hemminger | 5d174dd | 2013-04-27 11:31:55 +0000 | [diff] [blame] | 1665 | if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1666 | goto nla_put_failure; |
| 1667 | |
Atzm Watanabe | c7995c4 | 2013-04-16 02:50:52 +0000 | [diff] [blame] | 1668 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1669 | goto nla_put_failure; |
| 1670 | |
Stephen Hemminger | 7c41c42 | 2012-10-08 14:55:30 -0700 | [diff] [blame] | 1671 | if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1672 | goto nla_put_failure; |
| 1673 | |
| 1674 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || |
| 1675 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || |
David Stevens | e4f67ad | 2012-11-20 02:50:14 +0000 | [diff] [blame] | 1676 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
| 1677 | !!(vxlan->flags & VXLAN_F_LEARN)) || |
| 1678 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
| 1679 | !!(vxlan->flags & VXLAN_F_PROXY)) || |
| 1680 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || |
| 1681 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
| 1682 | !!(vxlan->flags & VXLAN_F_L2MISS)) || |
| 1683 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
| 1684 | !!(vxlan->flags & VXLAN_F_L3MISS)) || |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1685 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
stephen hemminger | 823aa87 | 2013-04-27 11:31:57 +0000 | [diff] [blame] | 1686 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
| 1687 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1688 | goto nla_put_failure; |
| 1689 | |
stephen hemminger | 05f47d6 | 2012-10-09 20:35:50 +0000 | [diff] [blame] | 1690 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
| 1691 | goto nla_put_failure; |
| 1692 | |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1693 | return 0; |
| 1694 | |
| 1695 | nla_put_failure: |
| 1696 | return -EMSGSIZE; |
| 1697 | } |
| 1698 | |
| 1699 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
| 1700 | .kind = "vxlan", |
| 1701 | .maxtype = IFLA_VXLAN_MAX, |
| 1702 | .policy = vxlan_policy, |
| 1703 | .priv_size = sizeof(struct vxlan_dev), |
| 1704 | .setup = vxlan_setup, |
| 1705 | .validate = vxlan_validate, |
| 1706 | .newlink = vxlan_newlink, |
| 1707 | .dellink = vxlan_dellink, |
| 1708 | .get_size = vxlan_get_size, |
| 1709 | .fill_info = vxlan_fill_info, |
| 1710 | }; |
| 1711 | |
| 1712 | static __net_init int vxlan_init_net(struct net *net) |
| 1713 | { |
| 1714 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1715 | unsigned h; |
| 1716 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1717 | INIT_LIST_HEAD(&vn->vxlan_list); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1718 | |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1719 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
| 1720 | INIT_HLIST_HEAD(&vn->sock_list[h]); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | static __net_exit void vxlan_exit_net(struct net *net) |
| 1726 | { |
| 1727 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1728 | struct vxlan_dev *vxlan; |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1729 | |
| 1730 | rtnl_lock(); |
stephen hemminger | 553675f | 2013-05-16 11:35:20 +0000 | [diff] [blame^] | 1731 | list_for_each_entry(vxlan, &vn->vxlan_list, next) |
| 1732 | dev_close(vxlan->dev); |
Zang MingJie | 9cb6cb7 | 2013-03-06 04:37:37 +0000 | [diff] [blame] | 1733 | rtnl_unlock(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | static struct pernet_operations vxlan_net_ops = { |
| 1737 | .init = vxlan_init_net, |
| 1738 | .exit = vxlan_exit_net, |
| 1739 | .id = &vxlan_net_id, |
| 1740 | .size = sizeof(struct vxlan_net), |
| 1741 | }; |
| 1742 | |
| 1743 | static int __init vxlan_init_module(void) |
| 1744 | { |
| 1745 | int rc; |
| 1746 | |
| 1747 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
| 1748 | |
| 1749 | rc = register_pernet_device(&vxlan_net_ops); |
| 1750 | if (rc) |
| 1751 | goto out1; |
| 1752 | |
| 1753 | rc = rtnl_link_register(&vxlan_link_ops); |
| 1754 | if (rc) |
| 1755 | goto out2; |
| 1756 | |
| 1757 | return 0; |
| 1758 | |
| 1759 | out2: |
| 1760 | unregister_pernet_device(&vxlan_net_ops); |
| 1761 | out1: |
| 1762 | return rc; |
| 1763 | } |
| 1764 | module_init(vxlan_init_module); |
| 1765 | |
| 1766 | static void __exit vxlan_cleanup_module(void) |
| 1767 | { |
| 1768 | rtnl_link_unregister(&vxlan_link_ops); |
| 1769 | unregister_pernet_device(&vxlan_net_ops); |
David Stevens | 6681712 | 2013-03-15 04:35:51 +0000 | [diff] [blame] | 1770 | rcu_barrier(); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1771 | } |
| 1772 | module_exit(vxlan_cleanup_module); |
| 1773 | |
| 1774 | MODULE_LICENSE("GPL"); |
| 1775 | MODULE_VERSION(VXLAN_VERSION); |
stephen hemminger | 3b8df3c | 2013-04-27 11:31:52 +0000 | [diff] [blame] | 1776 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
stephen hemminger | d342894 | 2012-10-01 12:32:35 +0000 | [diff] [blame] | 1777 | MODULE_ALIAS_RTNL_LINK("vxlan"); |