vxlan.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * VXLAN: Virtual eXtensiable Local Area Network
  3. *
  4. * Copyright (c) 2012 Vyatta Inc.
  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
  11. * - use IANA UDP port number (when defined)
  12. * - IPv6 (not in RFC)
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/rculist.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/udp.h>
  26. #include <linux/igmp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/version.h>
  30. #include <linux/hash.h>
  31. #include <net/ip.h>
  32. #include <net/icmp.h>
  33. #include <net/udp.h>
  34. #include <net/rtnetlink.h>
  35. #include <net/route.h>
  36. #include <net/dsfield.h>
  37. #include <net/inet_ecn.h>
  38. #include <net/net_namespace.h>
  39. #include <net/netns/generic.h>
  40. #define VXLAN_VERSION "0.1"
  41. #define VNI_HASH_BITS 10
  42. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  43. #define FDB_HASH_BITS 8
  44. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  45. #define FDB_AGE_DEFAULT 300 /* 5 min */
  46. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  47. #define VXLAN_N_VID (1u << 24)
  48. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  49. /* VLAN + IP header + UDP + VXLAN */
  50. #define VXLAN_HEADROOM (4 + 20 + 8 + 8)
  51. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  52. /* VXLAN protocol header */
  53. struct vxlanhdr {
  54. __be32 vx_flags;
  55. __be32 vx_vni;
  56. };
  57. /* UDP port for VXLAN traffic. */
  58. static unsigned int vxlan_port __read_mostly = 8472;
  59. module_param_named(udp_port, vxlan_port, uint, 0444);
  60. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  61. static bool log_ecn_error = true;
  62. module_param(log_ecn_error, bool, 0644);
  63. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  64. /* per-net private data for this module */
  65. static unsigned int vxlan_net_id;
  66. struct vxlan_net {
  67. struct socket *sock; /* UDP encap socket */
  68. struct hlist_head vni_list[VNI_HASH_SIZE];
  69. };
  70. /* Forwarding table entry */
  71. struct vxlan_fdb {
  72. struct hlist_node hlist; /* linked list of entries */
  73. struct rcu_head rcu;
  74. unsigned long updated; /* jiffies */
  75. unsigned long used;
  76. __be32 remote_ip;
  77. u16 state; /* see ndm_state */
  78. u8 eth_addr[ETH_ALEN];
  79. };
  80. /* Per-cpu network traffic stats */
  81. struct vxlan_stats {
  82. u64 rx_packets;
  83. u64 rx_bytes;
  84. u64 tx_packets;
  85. u64 tx_bytes;
  86. struct u64_stats_sync syncp;
  87. };
  88. /* Pseudo network device */
  89. struct vxlan_dev {
  90. struct hlist_node hlist;
  91. struct net_device *dev;
  92. struct vxlan_stats __percpu *stats;
  93. __u32 vni; /* virtual network id */
  94. __be32 gaddr; /* multicast group */
  95. __be32 saddr; /* source address */
  96. unsigned int link; /* link to multicast over */
  97. __u8 tos; /* TOS override */
  98. __u8 ttl;
  99. bool learn;
  100. unsigned long age_interval;
  101. struct timer_list age_timer;
  102. spinlock_t hash_lock;
  103. unsigned int addrcnt;
  104. unsigned int addrmax;
  105. unsigned int addrexceeded;
  106. struct hlist_head fdb_head[FDB_HASH_SIZE];
  107. };
  108. /* salt for hash table */
  109. static u32 vxlan_salt __read_mostly;
  110. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  111. {
  112. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  113. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  114. }
  115. /* Look up VNI in a per net namespace table */
  116. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  117. {
  118. struct vxlan_dev *vxlan;
  119. struct hlist_node *node;
  120. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  121. if (vxlan->vni == id)
  122. return vxlan;
  123. }
  124. return NULL;
  125. }
  126. /* Fill in neighbour message in skbuff. */
  127. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  128. const struct vxlan_fdb *fdb,
  129. u32 portid, u32 seq, int type, unsigned int flags)
  130. {
  131. unsigned long now = jiffies;
  132. struct nda_cacheinfo ci;
  133. struct nlmsghdr *nlh;
  134. struct ndmsg *ndm;
  135. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  136. if (nlh == NULL)
  137. return -EMSGSIZE;
  138. ndm = nlmsg_data(nlh);
  139. memset(ndm, 0, sizeof(*ndm));
  140. ndm->ndm_family = AF_BRIDGE;
  141. ndm->ndm_state = fdb->state;
  142. ndm->ndm_ifindex = vxlan->dev->ifindex;
  143. ndm->ndm_flags = NTF_SELF;
  144. ndm->ndm_type = NDA_DST;
  145. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  146. goto nla_put_failure;
  147. if (nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  148. goto nla_put_failure;
  149. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  150. ci.ndm_confirmed = 0;
  151. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  152. ci.ndm_refcnt = 0;
  153. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  154. goto nla_put_failure;
  155. return nlmsg_end(skb, nlh);
  156. nla_put_failure:
  157. nlmsg_cancel(skb, nlh);
  158. return -EMSGSIZE;
  159. }
  160. static inline size_t vxlan_nlmsg_size(void)
  161. {
  162. return NLMSG_ALIGN(sizeof(struct ndmsg))
  163. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  164. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  165. + nla_total_size(sizeof(struct nda_cacheinfo));
  166. }
  167. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  168. const struct vxlan_fdb *fdb, int type)
  169. {
  170. struct net *net = dev_net(vxlan->dev);
  171. struct sk_buff *skb;
  172. int err = -ENOBUFS;
  173. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  174. if (skb == NULL)
  175. goto errout;
  176. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  177. if (err < 0) {
  178. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  179. WARN_ON(err == -EMSGSIZE);
  180. kfree_skb(skb);
  181. goto errout;
  182. }
  183. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  184. return;
  185. errout:
  186. if (err < 0)
  187. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  188. }
  189. /* Hash Ethernet address */
  190. static u32 eth_hash(const unsigned char *addr)
  191. {
  192. u64 value = get_unaligned((u64 *)addr);
  193. /* only want 6 bytes */
  194. #ifdef __BIG_ENDIAN
  195. value <<= 16;
  196. #else
  197. value >>= 16;
  198. #endif
  199. return hash_64(value, FDB_HASH_BITS);
  200. }
  201. /* Hash chain to use given mac address */
  202. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  203. const u8 *mac)
  204. {
  205. return &vxlan->fdb_head[eth_hash(mac)];
  206. }
  207. /* Look up Ethernet address in forwarding table */
  208. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  209. const u8 *mac)
  210. {
  211. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  212. struct vxlan_fdb *f;
  213. struct hlist_node *node;
  214. hlist_for_each_entry_rcu(f, node, head, hlist) {
  215. if (compare_ether_addr(mac, f->eth_addr) == 0)
  216. return f;
  217. }
  218. return NULL;
  219. }
  220. /* Add new entry to forwarding table -- assumes lock held */
  221. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  222. const u8 *mac, __be32 ip,
  223. __u16 state, __u16 flags)
  224. {
  225. struct vxlan_fdb *f;
  226. int notify = 0;
  227. f = vxlan_find_mac(vxlan, mac);
  228. if (f) {
  229. if (flags & NLM_F_EXCL) {
  230. netdev_dbg(vxlan->dev,
  231. "lost race to create %pM\n", mac);
  232. return -EEXIST;
  233. }
  234. if (f->state != state) {
  235. f->state = state;
  236. f->updated = jiffies;
  237. notify = 1;
  238. }
  239. } else {
  240. if (!(flags & NLM_F_CREATE))
  241. return -ENOENT;
  242. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  243. return -ENOSPC;
  244. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  245. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  246. if (!f)
  247. return -ENOMEM;
  248. notify = 1;
  249. f->remote_ip = ip;
  250. f->state = state;
  251. f->updated = f->used = jiffies;
  252. memcpy(f->eth_addr, mac, ETH_ALEN);
  253. ++vxlan->addrcnt;
  254. hlist_add_head_rcu(&f->hlist,
  255. vxlan_fdb_head(vxlan, mac));
  256. }
  257. if (notify)
  258. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  259. return 0;
  260. }
  261. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  262. {
  263. netdev_dbg(vxlan->dev,
  264. "delete %pM\n", f->eth_addr);
  265. --vxlan->addrcnt;
  266. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  267. hlist_del_rcu(&f->hlist);
  268. kfree_rcu(f, rcu);
  269. }
  270. /* Add static entry (via netlink) */
  271. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  272. struct net_device *dev,
  273. const unsigned char *addr, u16 flags)
  274. {
  275. struct vxlan_dev *vxlan = netdev_priv(dev);
  276. __be32 ip;
  277. int err;
  278. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  279. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  280. ndm->ndm_state);
  281. return -EINVAL;
  282. }
  283. if (tb[NDA_DST] == NULL)
  284. return -EINVAL;
  285. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  286. return -EAFNOSUPPORT;
  287. ip = nla_get_be32(tb[NDA_DST]);
  288. spin_lock_bh(&vxlan->hash_lock);
  289. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  290. spin_unlock_bh(&vxlan->hash_lock);
  291. return err;
  292. }
  293. /* Delete entry (via netlink) */
  294. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  295. const unsigned char *addr)
  296. {
  297. struct vxlan_dev *vxlan = netdev_priv(dev);
  298. struct vxlan_fdb *f;
  299. int err = -ENOENT;
  300. spin_lock_bh(&vxlan->hash_lock);
  301. f = vxlan_find_mac(vxlan, addr);
  302. if (f) {
  303. vxlan_fdb_destroy(vxlan, f);
  304. err = 0;
  305. }
  306. spin_unlock_bh(&vxlan->hash_lock);
  307. return err;
  308. }
  309. /* Dump forwarding table */
  310. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  311. struct net_device *dev, int idx)
  312. {
  313. struct vxlan_dev *vxlan = netdev_priv(dev);
  314. unsigned int h;
  315. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  316. struct vxlan_fdb *f;
  317. struct hlist_node *n;
  318. int err;
  319. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  320. if (idx < cb->args[0])
  321. goto skip;
  322. err = vxlan_fdb_info(skb, vxlan, f,
  323. NETLINK_CB(cb->skb).portid,
  324. cb->nlh->nlmsg_seq,
  325. RTM_NEWNEIGH,
  326. NLM_F_MULTI);
  327. if (err < 0)
  328. break;
  329. skip:
  330. ++idx;
  331. }
  332. }
  333. return idx;
  334. }
  335. /* Watch incoming packets to learn mapping between Ethernet address
  336. * and Tunnel endpoint.
  337. */
  338. static void vxlan_snoop(struct net_device *dev,
  339. __be32 src_ip, const u8 *src_mac)
  340. {
  341. struct vxlan_dev *vxlan = netdev_priv(dev);
  342. struct vxlan_fdb *f;
  343. int err;
  344. f = vxlan_find_mac(vxlan, src_mac);
  345. if (likely(f)) {
  346. f->used = jiffies;
  347. if (likely(f->remote_ip == src_ip))
  348. return;
  349. if (net_ratelimit())
  350. netdev_info(dev,
  351. "%pM migrated from %pI4 to %pI4\n",
  352. src_mac, &f->remote_ip, &src_ip);
  353. f->remote_ip = src_ip;
  354. f->updated = jiffies;
  355. } else {
  356. /* learned new entry */
  357. spin_lock(&vxlan->hash_lock);
  358. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  359. NUD_REACHABLE,
  360. NLM_F_EXCL|NLM_F_CREATE);
  361. spin_unlock(&vxlan->hash_lock);
  362. }
  363. }
  364. /* See if multicast group is already in use by other ID */
  365. static bool vxlan_group_used(struct vxlan_net *vn,
  366. const struct vxlan_dev *this)
  367. {
  368. const struct vxlan_dev *vxlan;
  369. struct hlist_node *node;
  370. unsigned h;
  371. for (h = 0; h < VNI_HASH_SIZE; ++h)
  372. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  373. if (vxlan == this)
  374. continue;
  375. if (!netif_running(vxlan->dev))
  376. continue;
  377. if (vxlan->gaddr == this->gaddr)
  378. return true;
  379. }
  380. return false;
  381. }
  382. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  383. static int vxlan_join_group(struct net_device *dev)
  384. {
  385. struct vxlan_dev *vxlan = netdev_priv(dev);
  386. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  387. struct sock *sk = vn->sock->sk;
  388. struct ip_mreqn mreq = {
  389. .imr_multiaddr.s_addr = vxlan->gaddr,
  390. };
  391. int err;
  392. /* Already a member of group */
  393. if (vxlan_group_used(vn, vxlan))
  394. return 0;
  395. /* Need to drop RTNL to call multicast join */
  396. rtnl_unlock();
  397. lock_sock(sk);
  398. err = ip_mc_join_group(sk, &mreq);
  399. release_sock(sk);
  400. rtnl_lock();
  401. return err;
  402. }
  403. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  404. static int vxlan_leave_group(struct net_device *dev)
  405. {
  406. struct vxlan_dev *vxlan = netdev_priv(dev);
  407. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  408. int err = 0;
  409. struct sock *sk = vn->sock->sk;
  410. struct ip_mreqn mreq = {
  411. .imr_multiaddr.s_addr = vxlan->gaddr,
  412. };
  413. /* Only leave group when last vxlan is done. */
  414. if (vxlan_group_used(vn, vxlan))
  415. return 0;
  416. /* Need to drop RTNL to call multicast leave */
  417. rtnl_unlock();
  418. lock_sock(sk);
  419. err = ip_mc_leave_group(sk, &mreq);
  420. release_sock(sk);
  421. rtnl_lock();
  422. return err;
  423. }
  424. /* Callback from net/ipv4/udp.c to receive packets */
  425. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  426. {
  427. struct iphdr *oip;
  428. struct vxlanhdr *vxh;
  429. struct vxlan_dev *vxlan;
  430. struct vxlan_stats *stats;
  431. __u32 vni;
  432. int err;
  433. /* pop off outer UDP header */
  434. __skb_pull(skb, sizeof(struct udphdr));
  435. /* Need Vxlan and inner Ethernet header to be present */
  436. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  437. goto error;
  438. /* Drop packets with reserved bits set */
  439. vxh = (struct vxlanhdr *) skb->data;
  440. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  441. (vxh->vx_vni & htonl(0xff))) {
  442. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  443. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  444. goto error;
  445. }
  446. __skb_pull(skb, sizeof(struct vxlanhdr));
  447. skb_postpull_rcsum(skb, eth_hdr(skb), sizeof(struct vxlanhdr));
  448. /* Is this VNI defined? */
  449. vni = ntohl(vxh->vx_vni) >> 8;
  450. vxlan = vxlan_find_vni(sock_net(sk), vni);
  451. if (!vxlan) {
  452. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  453. goto drop;
  454. }
  455. if (!pskb_may_pull(skb, ETH_HLEN)) {
  456. vxlan->dev->stats.rx_length_errors++;
  457. vxlan->dev->stats.rx_errors++;
  458. goto drop;
  459. }
  460. /* Re-examine inner Ethernet packet */
  461. oip = ip_hdr(skb);
  462. skb->protocol = eth_type_trans(skb, vxlan->dev);
  463. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  464. /* Ignore packet loops (and multicast echo) */
  465. if (compare_ether_addr(eth_hdr(skb)->h_source,
  466. vxlan->dev->dev_addr) == 0)
  467. goto drop;
  468. if (vxlan->learn)
  469. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  470. __skb_tunnel_rx(skb, vxlan->dev);
  471. skb_reset_network_header(skb);
  472. err = IP_ECN_decapsulate(oip, skb);
  473. if (unlikely(err)) {
  474. if (log_ecn_error)
  475. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  476. &oip->saddr, oip->tos);
  477. if (err > 1) {
  478. ++vxlan->dev->stats.rx_frame_errors;
  479. ++vxlan->dev->stats.rx_errors;
  480. goto drop;
  481. }
  482. }
  483. stats = this_cpu_ptr(vxlan->stats);
  484. u64_stats_update_begin(&stats->syncp);
  485. stats->rx_packets++;
  486. stats->rx_bytes += skb->len;
  487. u64_stats_update_end(&stats->syncp);
  488. netif_rx(skb);
  489. return 0;
  490. error:
  491. /* Put UDP header back */
  492. __skb_push(skb, sizeof(struct udphdr));
  493. return 1;
  494. drop:
  495. /* Consume bad packet */
  496. kfree_skb(skb);
  497. return 0;
  498. }
  499. /* Extract dsfield from inner protocol */
  500. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  501. const struct sk_buff *skb)
  502. {
  503. if (skb->protocol == htons(ETH_P_IP))
  504. return iph->tos;
  505. else if (skb->protocol == htons(ETH_P_IPV6))
  506. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  507. else
  508. return 0;
  509. }
  510. /* Propogate ECN bits out */
  511. static inline u8 vxlan_ecn_encap(u8 tos,
  512. const struct iphdr *iph,
  513. const struct sk_buff *skb)
  514. {
  515. u8 inner = vxlan_get_dsfield(iph, skb);
  516. return INET_ECN_encapsulate(tos, inner);
  517. }
  518. /* Transmit local packets over Vxlan
  519. *
  520. * Outer IP header inherits ECN and DF from inner header.
  521. * Outer UDP destination is the VXLAN assigned port.
  522. * source port is based on hash of flow if available
  523. * otherwise use a random value
  524. */
  525. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  526. {
  527. struct vxlan_dev *vxlan = netdev_priv(dev);
  528. struct rtable *rt;
  529. const struct ethhdr *eth;
  530. const struct iphdr *old_iph;
  531. struct iphdr *iph;
  532. struct vxlanhdr *vxh;
  533. struct udphdr *uh;
  534. struct flowi4 fl4;
  535. struct vxlan_fdb *f;
  536. unsigned int pkt_len = skb->len;
  537. u32 hash;
  538. __be32 dst;
  539. __be16 df = 0;
  540. __u8 tos, ttl;
  541. int err;
  542. /* Need space for new headers (invalidates iph ptr) */
  543. if (skb_cow_head(skb, VXLAN_HEADROOM))
  544. goto drop;
  545. eth = (void *)skb->data;
  546. old_iph = ip_hdr(skb);
  547. if (!is_multicast_ether_addr(eth->h_dest) &&
  548. (f = vxlan_find_mac(vxlan, eth->h_dest)))
  549. dst = f->remote_ip;
  550. else if (vxlan->gaddr) {
  551. dst = vxlan->gaddr;
  552. } else
  553. goto drop;
  554. ttl = vxlan->ttl;
  555. if (!ttl && IN_MULTICAST(ntohl(dst)))
  556. ttl = 1;
  557. tos = vxlan->tos;
  558. if (tos == 1)
  559. tos = vxlan_get_dsfield(old_iph, skb);
  560. hash = skb_get_rxhash(skb);
  561. rt = ip_route_output_gre(dev_net(dev), &fl4, dst,
  562. vxlan->saddr, vxlan->vni,
  563. RT_TOS(tos), vxlan->link);
  564. if (IS_ERR(rt)) {
  565. netdev_dbg(dev, "no route to %pI4\n", &dst);
  566. dev->stats.tx_carrier_errors++;
  567. goto tx_error;
  568. }
  569. if (rt->dst.dev == dev) {
  570. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  571. ip_rt_put(rt);
  572. dev->stats.collisions++;
  573. goto tx_error;
  574. }
  575. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  576. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  577. IPSKB_REROUTED);
  578. skb_dst_drop(skb);
  579. skb_dst_set(skb, &rt->dst);
  580. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  581. vxh->vx_flags = htonl(VXLAN_FLAGS);
  582. vxh->vx_vni = htonl(vxlan->vni << 8);
  583. __skb_push(skb, sizeof(*uh));
  584. skb_reset_transport_header(skb);
  585. uh = udp_hdr(skb);
  586. uh->dest = htons(vxlan_port);
  587. uh->source = hash ? :random32();
  588. uh->len = htons(skb->len);
  589. uh->check = 0;
  590. __skb_push(skb, sizeof(*iph));
  591. skb_reset_network_header(skb);
  592. iph = ip_hdr(skb);
  593. iph->version = 4;
  594. iph->ihl = sizeof(struct iphdr) >> 2;
  595. iph->frag_off = df;
  596. iph->protocol = IPPROTO_UDP;
  597. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  598. iph->daddr = fl4.daddr;
  599. iph->saddr = fl4.saddr;
  600. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  601. /* See __IPTUNNEL_XMIT */
  602. skb->ip_summed = CHECKSUM_NONE;
  603. ip_select_ident(iph, &rt->dst, NULL);
  604. err = ip_local_out(skb);
  605. if (likely(net_xmit_eval(err) == 0)) {
  606. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  607. u64_stats_update_begin(&stats->syncp);
  608. stats->tx_packets++;
  609. stats->tx_bytes += pkt_len;
  610. u64_stats_update_end(&stats->syncp);
  611. } else {
  612. dev->stats.tx_errors++;
  613. dev->stats.tx_aborted_errors++;
  614. }
  615. return NETDEV_TX_OK;
  616. drop:
  617. dev->stats.tx_dropped++;
  618. goto tx_free;
  619. tx_error:
  620. dev->stats.tx_errors++;
  621. tx_free:
  622. dev_kfree_skb(skb);
  623. return NETDEV_TX_OK;
  624. }
  625. /* Walk the forwarding table and purge stale entries */
  626. static void vxlan_cleanup(unsigned long arg)
  627. {
  628. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  629. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  630. unsigned int h;
  631. if (!netif_running(vxlan->dev))
  632. return;
  633. spin_lock_bh(&vxlan->hash_lock);
  634. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  635. struct hlist_node *p, *n;
  636. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  637. struct vxlan_fdb *f
  638. = container_of(p, struct vxlan_fdb, hlist);
  639. unsigned long timeout;
  640. if (f->state == NUD_PERMANENT)
  641. continue;
  642. timeout = f->used + vxlan->age_interval * HZ;
  643. if (time_before_eq(timeout, jiffies)) {
  644. netdev_dbg(vxlan->dev,
  645. "garbage collect %pM\n",
  646. f->eth_addr);
  647. f->state = NUD_STALE;
  648. vxlan_fdb_destroy(vxlan, f);
  649. } else if (time_before(timeout, next_timer))
  650. next_timer = timeout;
  651. }
  652. }
  653. spin_unlock_bh(&vxlan->hash_lock);
  654. mod_timer(&vxlan->age_timer, next_timer);
  655. }
  656. /* Setup stats when device is created */
  657. static int vxlan_init(struct net_device *dev)
  658. {
  659. struct vxlan_dev *vxlan = netdev_priv(dev);
  660. vxlan->stats = alloc_percpu(struct vxlan_stats);
  661. if (!vxlan->stats)
  662. return -ENOMEM;
  663. return 0;
  664. }
  665. /* Start ageing timer and join group when device is brought up */
  666. static int vxlan_open(struct net_device *dev)
  667. {
  668. struct vxlan_dev *vxlan = netdev_priv(dev);
  669. int err;
  670. if (vxlan->gaddr) {
  671. err = vxlan_join_group(dev);
  672. if (err)
  673. return err;
  674. }
  675. if (vxlan->age_interval)
  676. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  677. return 0;
  678. }
  679. /* Purge the forwarding table */
  680. static void vxlan_flush(struct vxlan_dev *vxlan)
  681. {
  682. unsigned h;
  683. spin_lock_bh(&vxlan->hash_lock);
  684. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  685. struct hlist_node *p, *n;
  686. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  687. struct vxlan_fdb *f
  688. = container_of(p, struct vxlan_fdb, hlist);
  689. vxlan_fdb_destroy(vxlan, f);
  690. }
  691. }
  692. spin_unlock_bh(&vxlan->hash_lock);
  693. }
  694. /* Cleanup timer and forwarding table on shutdown */
  695. static int vxlan_stop(struct net_device *dev)
  696. {
  697. struct vxlan_dev *vxlan = netdev_priv(dev);
  698. if (vxlan->gaddr)
  699. vxlan_leave_group(dev);
  700. del_timer_sync(&vxlan->age_timer);
  701. vxlan_flush(vxlan);
  702. return 0;
  703. }
  704. /* Merge per-cpu statistics */
  705. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  706. struct rtnl_link_stats64 *stats)
  707. {
  708. struct vxlan_dev *vxlan = netdev_priv(dev);
  709. struct vxlan_stats tmp, sum = { 0 };
  710. unsigned int cpu;
  711. for_each_possible_cpu(cpu) {
  712. unsigned int start;
  713. const struct vxlan_stats *stats
  714. = per_cpu_ptr(vxlan->stats, cpu);
  715. do {
  716. start = u64_stats_fetch_begin_bh(&stats->syncp);
  717. memcpy(&tmp, stats, sizeof(tmp));
  718. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  719. sum.tx_bytes += tmp.tx_bytes;
  720. sum.tx_packets += tmp.tx_packets;
  721. sum.rx_bytes += tmp.rx_bytes;
  722. sum.rx_packets += tmp.rx_packets;
  723. }
  724. stats->tx_bytes = sum.tx_bytes;
  725. stats->tx_packets = sum.tx_packets;
  726. stats->rx_bytes = sum.rx_bytes;
  727. stats->rx_packets = sum.rx_packets;
  728. stats->multicast = dev->stats.multicast;
  729. stats->rx_length_errors = dev->stats.rx_length_errors;
  730. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  731. stats->rx_errors = dev->stats.rx_errors;
  732. stats->tx_dropped = dev->stats.tx_dropped;
  733. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  734. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  735. stats->collisions = dev->stats.collisions;
  736. stats->tx_errors = dev->stats.tx_errors;
  737. return stats;
  738. }
  739. /* Stub, nothing needs to be done. */
  740. static void vxlan_set_multicast_list(struct net_device *dev)
  741. {
  742. }
  743. static const struct net_device_ops vxlan_netdev_ops = {
  744. .ndo_init = vxlan_init,
  745. .ndo_open = vxlan_open,
  746. .ndo_stop = vxlan_stop,
  747. .ndo_start_xmit = vxlan_xmit,
  748. .ndo_get_stats64 = vxlan_stats64,
  749. .ndo_set_rx_mode = vxlan_set_multicast_list,
  750. .ndo_change_mtu = eth_change_mtu,
  751. .ndo_validate_addr = eth_validate_addr,
  752. .ndo_set_mac_address = eth_mac_addr,
  753. .ndo_fdb_add = vxlan_fdb_add,
  754. .ndo_fdb_del = vxlan_fdb_delete,
  755. .ndo_fdb_dump = vxlan_fdb_dump,
  756. };
  757. /* Info for udev, that this is a virtual tunnel endpoint */
  758. static struct device_type vxlan_type = {
  759. .name = "vxlan",
  760. };
  761. static void vxlan_free(struct net_device *dev)
  762. {
  763. struct vxlan_dev *vxlan = netdev_priv(dev);
  764. free_percpu(vxlan->stats);
  765. free_netdev(dev);
  766. }
  767. /* Initialize the device structure. */
  768. static void vxlan_setup(struct net_device *dev)
  769. {
  770. struct vxlan_dev *vxlan = netdev_priv(dev);
  771. unsigned h;
  772. eth_hw_addr_random(dev);
  773. ether_setup(dev);
  774. dev->netdev_ops = &vxlan_netdev_ops;
  775. dev->destructor = vxlan_free;
  776. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  777. dev->tx_queue_len = 0;
  778. dev->features |= NETIF_F_LLTX;
  779. dev->features |= NETIF_F_NETNS_LOCAL;
  780. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  781. spin_lock_init(&vxlan->hash_lock);
  782. init_timer_deferrable(&vxlan->age_timer);
  783. vxlan->age_timer.function = vxlan_cleanup;
  784. vxlan->age_timer.data = (unsigned long) vxlan;
  785. vxlan->dev = dev;
  786. for (h = 0; h < FDB_HASH_SIZE; ++h)
  787. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  788. }
  789. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  790. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  791. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  792. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  793. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  794. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  795. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  796. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  797. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  798. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  799. };
  800. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  801. {
  802. if (tb[IFLA_ADDRESS]) {
  803. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  804. pr_debug("invalid link address (not ethernet)\n");
  805. return -EINVAL;
  806. }
  807. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  808. pr_debug("invalid all zero ethernet address\n");
  809. return -EADDRNOTAVAIL;
  810. }
  811. }
  812. if (!data)
  813. return -EINVAL;
  814. if (data[IFLA_VXLAN_ID]) {
  815. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  816. if (id >= VXLAN_VID_MASK)
  817. return -ERANGE;
  818. }
  819. if (data[IFLA_VXLAN_GROUP]) {
  820. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  821. if (!IN_MULTICAST(ntohl(gaddr))) {
  822. pr_debug("group address is not IPv4 multicast\n");
  823. return -EADDRNOTAVAIL;
  824. }
  825. }
  826. return 0;
  827. }
  828. static int vxlan_newlink(struct net *net, struct net_device *dev,
  829. struct nlattr *tb[], struct nlattr *data[])
  830. {
  831. struct vxlan_dev *vxlan = netdev_priv(dev);
  832. __u32 vni;
  833. int err;
  834. if (!data[IFLA_VXLAN_ID])
  835. return -EINVAL;
  836. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  837. if (vxlan_find_vni(net, vni)) {
  838. pr_info("duplicate VNI %u\n", vni);
  839. return -EEXIST;
  840. }
  841. vxlan->vni = vni;
  842. if (data[IFLA_VXLAN_GROUP])
  843. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  844. if (data[IFLA_VXLAN_LOCAL])
  845. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  846. if (data[IFLA_VXLAN_LINK]) {
  847. vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]);
  848. if (!tb[IFLA_MTU]) {
  849. struct net_device *lowerdev;
  850. lowerdev = __dev_get_by_index(net, vxlan->link);
  851. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  852. }
  853. }
  854. if (data[IFLA_VXLAN_TOS])
  855. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  856. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  857. vxlan->learn = true;
  858. if (data[IFLA_VXLAN_AGEING])
  859. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  860. else
  861. vxlan->age_interval = FDB_AGE_DEFAULT;
  862. if (data[IFLA_VXLAN_LIMIT])
  863. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  864. err = register_netdevice(dev);
  865. if (!err)
  866. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  867. return err;
  868. }
  869. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  870. {
  871. struct vxlan_dev *vxlan = netdev_priv(dev);
  872. hlist_del_rcu(&vxlan->hlist);
  873. unregister_netdevice_queue(dev, head);
  874. }
  875. static size_t vxlan_get_size(const struct net_device *dev)
  876. {
  877. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  878. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  879. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  880. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  881. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  882. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  883. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  884. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  885. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  886. 0;
  887. }
  888. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  889. {
  890. const struct vxlan_dev *vxlan = netdev_priv(dev);
  891. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  892. goto nla_put_failure;
  893. if (vxlan->gaddr && nla_put_u32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  894. goto nla_put_failure;
  895. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  896. goto nla_put_failure;
  897. if (vxlan->saddr && nla_put_u32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  898. goto nla_put_failure;
  899. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  900. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  901. nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) ||
  902. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  903. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  904. goto nla_put_failure;
  905. return 0;
  906. nla_put_failure:
  907. return -EMSGSIZE;
  908. }
  909. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  910. .kind = "vxlan",
  911. .maxtype = IFLA_VXLAN_MAX,
  912. .policy = vxlan_policy,
  913. .priv_size = sizeof(struct vxlan_dev),
  914. .setup = vxlan_setup,
  915. .validate = vxlan_validate,
  916. .newlink = vxlan_newlink,
  917. .dellink = vxlan_dellink,
  918. .get_size = vxlan_get_size,
  919. .fill_info = vxlan_fill_info,
  920. };
  921. static __net_init int vxlan_init_net(struct net *net)
  922. {
  923. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  924. struct sock *sk;
  925. struct sockaddr_in vxlan_addr = {
  926. .sin_family = AF_INET,
  927. .sin_addr.s_addr = htonl(INADDR_ANY),
  928. };
  929. int rc;
  930. unsigned h;
  931. /* Create UDP socket for encapsulation receive. */
  932. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  933. if (rc < 0) {
  934. pr_debug("UDP socket create failed\n");
  935. return rc;
  936. }
  937. /* Put in proper namespace */
  938. sk = vn->sock->sk;
  939. sk_change_net(sk, net);
  940. vxlan_addr.sin_port = htons(vxlan_port);
  941. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  942. sizeof(vxlan_addr));
  943. if (rc < 0) {
  944. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  945. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  946. sk_release_kernel(sk);
  947. vn->sock = NULL;
  948. return rc;
  949. }
  950. /* Disable multicast loopback */
  951. inet_sk(sk)->mc_loop = 0;
  952. /* Mark socket as an encapsulation socket. */
  953. udp_sk(sk)->encap_type = 1;
  954. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  955. udp_encap_enable();
  956. for (h = 0; h < VNI_HASH_SIZE; ++h)
  957. INIT_HLIST_HEAD(&vn->vni_list[h]);
  958. return 0;
  959. }
  960. static __net_exit void vxlan_exit_net(struct net *net)
  961. {
  962. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  963. if (vn->sock) {
  964. sk_release_kernel(vn->sock->sk);
  965. vn->sock = NULL;
  966. }
  967. }
  968. static struct pernet_operations vxlan_net_ops = {
  969. .init = vxlan_init_net,
  970. .exit = vxlan_exit_net,
  971. .id = &vxlan_net_id,
  972. .size = sizeof(struct vxlan_net),
  973. };
  974. static int __init vxlan_init_module(void)
  975. {
  976. int rc;
  977. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  978. rc = register_pernet_device(&vxlan_net_ops);
  979. if (rc)
  980. goto out1;
  981. rc = rtnl_link_register(&vxlan_link_ops);
  982. if (rc)
  983. goto out2;
  984. return 0;
  985. out2:
  986. unregister_pernet_device(&vxlan_net_ops);
  987. out1:
  988. return rc;
  989. }
  990. module_init(vxlan_init_module);
  991. static void __exit vxlan_cleanup_module(void)
  992. {
  993. rtnl_link_unregister(&vxlan_link_ops);
  994. unregister_pernet_device(&vxlan_net_ops);
  995. }
  996. module_exit(vxlan_cleanup_module);
  997. MODULE_LICENSE("GPL");
  998. MODULE_VERSION(VXLAN_VERSION);
  999. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1000. MODULE_ALIAS_RTNL_LINK("vxlan");