vxlan.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * VXLAN: Virtual eXtensible 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/hash.h>
  30. #include <net/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/icmp.h>
  34. #include <net/udp.h>
  35. #include <net/rtnetlink.h>
  36. #include <net/route.h>
  37. #include <net/dsfield.h>
  38. #include <net/inet_ecn.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #define VXLAN_VERSION "0.1"
  42. #define VNI_HASH_BITS 10
  43. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  44. #define FDB_HASH_BITS 8
  45. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  46. #define FDB_AGE_DEFAULT 300 /* 5 min */
  47. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  48. #define VXLAN_N_VID (1u << 24)
  49. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  50. /* IP header + UDP + VXLAN + Ethernet header */
  51. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  52. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  53. /* VXLAN protocol header */
  54. struct vxlanhdr {
  55. __be32 vx_flags;
  56. __be32 vx_vni;
  57. };
  58. /* UDP port for VXLAN traffic. */
  59. static unsigned int vxlan_port __read_mostly = 8472;
  60. module_param_named(udp_port, vxlan_port, uint, 0444);
  61. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  62. static bool log_ecn_error = true;
  63. module_param(log_ecn_error, bool, 0644);
  64. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  65. /* per-net private data for this module */
  66. static unsigned int vxlan_net_id;
  67. struct vxlan_net {
  68. struct socket *sock; /* UDP encap socket */
  69. struct hlist_head vni_list[VNI_HASH_SIZE];
  70. };
  71. /* Forwarding table entry */
  72. struct vxlan_fdb {
  73. struct hlist_node hlist; /* linked list of entries */
  74. struct rcu_head rcu;
  75. unsigned long updated; /* jiffies */
  76. unsigned long used;
  77. __be32 remote_ip;
  78. u16 state; /* see ndm_state */
  79. u8 eth_addr[ETH_ALEN];
  80. };
  81. /* Per-cpu network traffic stats */
  82. struct vxlan_stats {
  83. u64 rx_packets;
  84. u64 rx_bytes;
  85. u64 tx_packets;
  86. u64 tx_bytes;
  87. struct u64_stats_sync syncp;
  88. };
  89. /* Pseudo network device */
  90. struct vxlan_dev {
  91. struct hlist_node hlist;
  92. struct net_device *dev;
  93. struct vxlan_stats __percpu *stats;
  94. __u32 vni; /* virtual network id */
  95. __be32 gaddr; /* multicast group */
  96. __be32 saddr; /* source address */
  97. unsigned int link; /* link to multicast over */
  98. __u16 port_min; /* source port range */
  99. __u16 port_max;
  100. __u8 tos; /* TOS override */
  101. __u8 ttl;
  102. u32 flags; /* VXLAN_F_* below */
  103. unsigned long age_interval;
  104. struct timer_list age_timer;
  105. spinlock_t hash_lock;
  106. unsigned int addrcnt;
  107. unsigned int addrmax;
  108. struct hlist_head fdb_head[FDB_HASH_SIZE];
  109. };
  110. #define VXLAN_F_LEARN 0x01
  111. #define VXLAN_F_PROXY 0x02
  112. #define VXLAN_F_RSC 0x04
  113. #define VXLAN_F_L2MISS 0x08
  114. #define VXLAN_F_L3MISS 0x10
  115. /* salt for hash table */
  116. static u32 vxlan_salt __read_mostly;
  117. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  118. {
  119. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  120. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  121. }
  122. /* Look up VNI in a per net namespace table */
  123. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  124. {
  125. struct vxlan_dev *vxlan;
  126. struct hlist_node *node;
  127. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  128. if (vxlan->vni == id)
  129. return vxlan;
  130. }
  131. return NULL;
  132. }
  133. /* Fill in neighbour message in skbuff. */
  134. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  135. const struct vxlan_fdb *fdb,
  136. u32 portid, u32 seq, int type, unsigned int flags)
  137. {
  138. unsigned long now = jiffies;
  139. struct nda_cacheinfo ci;
  140. struct nlmsghdr *nlh;
  141. struct ndmsg *ndm;
  142. bool send_ip, send_eth;
  143. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  144. if (nlh == NULL)
  145. return -EMSGSIZE;
  146. ndm = nlmsg_data(nlh);
  147. memset(ndm, 0, sizeof(*ndm));
  148. send_eth = send_ip = true;
  149. if (type == RTM_GETNEIGH) {
  150. ndm->ndm_family = AF_INET;
  151. send_ip = fdb->remote_ip != 0;
  152. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  153. } else
  154. ndm->ndm_family = AF_BRIDGE;
  155. ndm->ndm_state = fdb->state;
  156. ndm->ndm_ifindex = vxlan->dev->ifindex;
  157. ndm->ndm_flags = NTF_SELF;
  158. ndm->ndm_type = NDA_DST;
  159. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  160. goto nla_put_failure;
  161. if (send_ip && nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  162. goto nla_put_failure;
  163. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  164. ci.ndm_confirmed = 0;
  165. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  166. ci.ndm_refcnt = 0;
  167. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  168. goto nla_put_failure;
  169. return nlmsg_end(skb, nlh);
  170. nla_put_failure:
  171. nlmsg_cancel(skb, nlh);
  172. return -EMSGSIZE;
  173. }
  174. static inline size_t vxlan_nlmsg_size(void)
  175. {
  176. return NLMSG_ALIGN(sizeof(struct ndmsg))
  177. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  178. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  179. + nla_total_size(sizeof(struct nda_cacheinfo));
  180. }
  181. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  182. const struct vxlan_fdb *fdb, int type)
  183. {
  184. struct net *net = dev_net(vxlan->dev);
  185. struct sk_buff *skb;
  186. int err = -ENOBUFS;
  187. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  188. if (skb == NULL)
  189. goto errout;
  190. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  191. if (err < 0) {
  192. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  193. WARN_ON(err == -EMSGSIZE);
  194. kfree_skb(skb);
  195. goto errout;
  196. }
  197. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  198. return;
  199. errout:
  200. if (err < 0)
  201. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  202. }
  203. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  204. {
  205. struct vxlan_dev *vxlan = netdev_priv(dev);
  206. struct vxlan_fdb f;
  207. memset(&f, 0, sizeof f);
  208. f.state = NUD_STALE;
  209. f.remote_ip = ipa; /* goes to NDA_DST */
  210. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  211. }
  212. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  213. {
  214. struct vxlan_fdb f;
  215. memset(&f, 0, sizeof f);
  216. f.state = NUD_STALE;
  217. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  218. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  219. }
  220. /* Hash Ethernet address */
  221. static u32 eth_hash(const unsigned char *addr)
  222. {
  223. u64 value = get_unaligned((u64 *)addr);
  224. /* only want 6 bytes */
  225. #ifdef __BIG_ENDIAN
  226. value >>= 16;
  227. #else
  228. value <<= 16;
  229. #endif
  230. return hash_64(value, FDB_HASH_BITS);
  231. }
  232. /* Hash chain to use given mac address */
  233. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  234. const u8 *mac)
  235. {
  236. return &vxlan->fdb_head[eth_hash(mac)];
  237. }
  238. /* Look up Ethernet address in forwarding table */
  239. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  240. const u8 *mac)
  241. {
  242. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  243. struct vxlan_fdb *f;
  244. struct hlist_node *node;
  245. hlist_for_each_entry_rcu(f, node, head, hlist) {
  246. if (compare_ether_addr(mac, f->eth_addr) == 0)
  247. return f;
  248. }
  249. return NULL;
  250. }
  251. /* Add new entry to forwarding table -- assumes lock held */
  252. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  253. const u8 *mac, __be32 ip,
  254. __u16 state, __u16 flags)
  255. {
  256. struct vxlan_fdb *f;
  257. int notify = 0;
  258. f = vxlan_find_mac(vxlan, mac);
  259. if (f) {
  260. if (flags & NLM_F_EXCL) {
  261. netdev_dbg(vxlan->dev,
  262. "lost race to create %pM\n", mac);
  263. return -EEXIST;
  264. }
  265. if (f->state != state) {
  266. f->state = state;
  267. f->updated = jiffies;
  268. notify = 1;
  269. }
  270. } else {
  271. if (!(flags & NLM_F_CREATE))
  272. return -ENOENT;
  273. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  274. return -ENOSPC;
  275. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  276. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  277. if (!f)
  278. return -ENOMEM;
  279. notify = 1;
  280. f->remote_ip = ip;
  281. f->state = state;
  282. f->updated = f->used = jiffies;
  283. memcpy(f->eth_addr, mac, ETH_ALEN);
  284. ++vxlan->addrcnt;
  285. hlist_add_head_rcu(&f->hlist,
  286. vxlan_fdb_head(vxlan, mac));
  287. }
  288. if (notify)
  289. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  290. return 0;
  291. }
  292. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  293. {
  294. netdev_dbg(vxlan->dev,
  295. "delete %pM\n", f->eth_addr);
  296. --vxlan->addrcnt;
  297. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  298. hlist_del_rcu(&f->hlist);
  299. kfree_rcu(f, rcu);
  300. }
  301. /* Add static entry (via netlink) */
  302. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  303. struct net_device *dev,
  304. const unsigned char *addr, u16 flags)
  305. {
  306. struct vxlan_dev *vxlan = netdev_priv(dev);
  307. __be32 ip;
  308. int err;
  309. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  310. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  311. ndm->ndm_state);
  312. return -EINVAL;
  313. }
  314. if (tb[NDA_DST] == NULL)
  315. return -EINVAL;
  316. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  317. return -EAFNOSUPPORT;
  318. ip = nla_get_be32(tb[NDA_DST]);
  319. spin_lock_bh(&vxlan->hash_lock);
  320. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  321. spin_unlock_bh(&vxlan->hash_lock);
  322. return err;
  323. }
  324. /* Delete entry (via netlink) */
  325. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  326. const unsigned char *addr)
  327. {
  328. struct vxlan_dev *vxlan = netdev_priv(dev);
  329. struct vxlan_fdb *f;
  330. int err = -ENOENT;
  331. spin_lock_bh(&vxlan->hash_lock);
  332. f = vxlan_find_mac(vxlan, addr);
  333. if (f) {
  334. vxlan_fdb_destroy(vxlan, f);
  335. err = 0;
  336. }
  337. spin_unlock_bh(&vxlan->hash_lock);
  338. return err;
  339. }
  340. /* Dump forwarding table */
  341. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  342. struct net_device *dev, int idx)
  343. {
  344. struct vxlan_dev *vxlan = netdev_priv(dev);
  345. unsigned int h;
  346. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  347. struct vxlan_fdb *f;
  348. struct hlist_node *n;
  349. int err;
  350. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  351. if (idx < cb->args[0])
  352. goto skip;
  353. err = vxlan_fdb_info(skb, vxlan, f,
  354. NETLINK_CB(cb->skb).portid,
  355. cb->nlh->nlmsg_seq,
  356. RTM_NEWNEIGH,
  357. NLM_F_MULTI);
  358. if (err < 0)
  359. break;
  360. skip:
  361. ++idx;
  362. }
  363. }
  364. return idx;
  365. }
  366. /* Watch incoming packets to learn mapping between Ethernet address
  367. * and Tunnel endpoint.
  368. */
  369. static void vxlan_snoop(struct net_device *dev,
  370. __be32 src_ip, const u8 *src_mac)
  371. {
  372. struct vxlan_dev *vxlan = netdev_priv(dev);
  373. struct vxlan_fdb *f;
  374. int err;
  375. f = vxlan_find_mac(vxlan, src_mac);
  376. if (likely(f)) {
  377. f->used = jiffies;
  378. if (likely(f->remote_ip == src_ip))
  379. return;
  380. if (net_ratelimit())
  381. netdev_info(dev,
  382. "%pM migrated from %pI4 to %pI4\n",
  383. src_mac, &f->remote_ip, &src_ip);
  384. f->remote_ip = src_ip;
  385. f->updated = jiffies;
  386. } else {
  387. /* learned new entry */
  388. spin_lock(&vxlan->hash_lock);
  389. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  390. NUD_REACHABLE,
  391. NLM_F_EXCL|NLM_F_CREATE);
  392. spin_unlock(&vxlan->hash_lock);
  393. }
  394. }
  395. /* See if multicast group is already in use by other ID */
  396. static bool vxlan_group_used(struct vxlan_net *vn,
  397. const struct vxlan_dev *this)
  398. {
  399. const struct vxlan_dev *vxlan;
  400. struct hlist_node *node;
  401. unsigned h;
  402. for (h = 0; h < VNI_HASH_SIZE; ++h)
  403. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  404. if (vxlan == this)
  405. continue;
  406. if (!netif_running(vxlan->dev))
  407. continue;
  408. if (vxlan->gaddr == this->gaddr)
  409. return true;
  410. }
  411. return false;
  412. }
  413. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  414. static int vxlan_join_group(struct net_device *dev)
  415. {
  416. struct vxlan_dev *vxlan = netdev_priv(dev);
  417. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  418. struct sock *sk = vn->sock->sk;
  419. struct ip_mreqn mreq = {
  420. .imr_multiaddr.s_addr = vxlan->gaddr,
  421. };
  422. int err;
  423. /* Already a member of group */
  424. if (vxlan_group_used(vn, vxlan))
  425. return 0;
  426. /* Need to drop RTNL to call multicast join */
  427. rtnl_unlock();
  428. lock_sock(sk);
  429. err = ip_mc_join_group(sk, &mreq);
  430. release_sock(sk);
  431. rtnl_lock();
  432. return err;
  433. }
  434. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  435. static int vxlan_leave_group(struct net_device *dev)
  436. {
  437. struct vxlan_dev *vxlan = netdev_priv(dev);
  438. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  439. int err = 0;
  440. struct sock *sk = vn->sock->sk;
  441. struct ip_mreqn mreq = {
  442. .imr_multiaddr.s_addr = vxlan->gaddr,
  443. };
  444. /* Only leave group when last vxlan is done. */
  445. if (vxlan_group_used(vn, vxlan))
  446. return 0;
  447. /* Need to drop RTNL to call multicast leave */
  448. rtnl_unlock();
  449. lock_sock(sk);
  450. err = ip_mc_leave_group(sk, &mreq);
  451. release_sock(sk);
  452. rtnl_lock();
  453. return err;
  454. }
  455. /* Callback from net/ipv4/udp.c to receive packets */
  456. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  457. {
  458. struct iphdr *oip;
  459. struct vxlanhdr *vxh;
  460. struct vxlan_dev *vxlan;
  461. struct vxlan_stats *stats;
  462. __u32 vni;
  463. int err;
  464. /* pop off outer UDP header */
  465. __skb_pull(skb, sizeof(struct udphdr));
  466. /* Need Vxlan and inner Ethernet header to be present */
  467. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  468. goto error;
  469. /* Drop packets with reserved bits set */
  470. vxh = (struct vxlanhdr *) skb->data;
  471. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  472. (vxh->vx_vni & htonl(0xff))) {
  473. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  474. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  475. goto error;
  476. }
  477. __skb_pull(skb, sizeof(struct vxlanhdr));
  478. /* Is this VNI defined? */
  479. vni = ntohl(vxh->vx_vni) >> 8;
  480. vxlan = vxlan_find_vni(sock_net(sk), vni);
  481. if (!vxlan) {
  482. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  483. goto drop;
  484. }
  485. if (!pskb_may_pull(skb, ETH_HLEN)) {
  486. vxlan->dev->stats.rx_length_errors++;
  487. vxlan->dev->stats.rx_errors++;
  488. goto drop;
  489. }
  490. skb_reset_mac_header(skb);
  491. /* Re-examine inner Ethernet packet */
  492. oip = ip_hdr(skb);
  493. skb->protocol = eth_type_trans(skb, vxlan->dev);
  494. /* Ignore packet loops (and multicast echo) */
  495. if (compare_ether_addr(eth_hdr(skb)->h_source,
  496. vxlan->dev->dev_addr) == 0)
  497. goto drop;
  498. if (vxlan->flags & VXLAN_F_LEARN)
  499. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  500. __skb_tunnel_rx(skb, vxlan->dev);
  501. skb_reset_network_header(skb);
  502. /* If the NIC driver gave us an encapsulated packet with
  503. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  504. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  505. * for us. Otherwise force the upper layers to verify it.
  506. */
  507. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  508. !(vxlan->dev->features & NETIF_F_RXCSUM))
  509. skb->ip_summed = CHECKSUM_NONE;
  510. skb->encapsulation = 0;
  511. err = IP_ECN_decapsulate(oip, skb);
  512. if (unlikely(err)) {
  513. if (log_ecn_error)
  514. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  515. &oip->saddr, oip->tos);
  516. if (err > 1) {
  517. ++vxlan->dev->stats.rx_frame_errors;
  518. ++vxlan->dev->stats.rx_errors;
  519. goto drop;
  520. }
  521. }
  522. stats = this_cpu_ptr(vxlan->stats);
  523. u64_stats_update_begin(&stats->syncp);
  524. stats->rx_packets++;
  525. stats->rx_bytes += skb->len;
  526. u64_stats_update_end(&stats->syncp);
  527. netif_rx(skb);
  528. return 0;
  529. error:
  530. /* Put UDP header back */
  531. __skb_push(skb, sizeof(struct udphdr));
  532. return 1;
  533. drop:
  534. /* Consume bad packet */
  535. kfree_skb(skb);
  536. return 0;
  537. }
  538. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  539. {
  540. struct vxlan_dev *vxlan = netdev_priv(dev);
  541. struct arphdr *parp;
  542. u8 *arpptr, *sha;
  543. __be32 sip, tip;
  544. struct neighbour *n;
  545. if (dev->flags & IFF_NOARP)
  546. goto out;
  547. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  548. dev->stats.tx_dropped++;
  549. goto out;
  550. }
  551. parp = arp_hdr(skb);
  552. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  553. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  554. parp->ar_pro != htons(ETH_P_IP) ||
  555. parp->ar_op != htons(ARPOP_REQUEST) ||
  556. parp->ar_hln != dev->addr_len ||
  557. parp->ar_pln != 4)
  558. goto out;
  559. arpptr = (u8 *)parp + sizeof(struct arphdr);
  560. sha = arpptr;
  561. arpptr += dev->addr_len; /* sha */
  562. memcpy(&sip, arpptr, sizeof(sip));
  563. arpptr += sizeof(sip);
  564. arpptr += dev->addr_len; /* tha */
  565. memcpy(&tip, arpptr, sizeof(tip));
  566. if (ipv4_is_loopback(tip) ||
  567. ipv4_is_multicast(tip))
  568. goto out;
  569. n = neigh_lookup(&arp_tbl, &tip, dev);
  570. if (n) {
  571. struct vxlan_dev *vxlan = netdev_priv(dev);
  572. struct vxlan_fdb *f;
  573. struct sk_buff *reply;
  574. if (!(n->nud_state & NUD_CONNECTED)) {
  575. neigh_release(n);
  576. goto out;
  577. }
  578. f = vxlan_find_mac(vxlan, n->ha);
  579. if (f && f->remote_ip == 0) {
  580. /* bridge-local neighbor */
  581. neigh_release(n);
  582. goto out;
  583. }
  584. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  585. n->ha, sha);
  586. neigh_release(n);
  587. skb_reset_mac_header(reply);
  588. __skb_pull(reply, skb_network_offset(reply));
  589. reply->ip_summed = CHECKSUM_UNNECESSARY;
  590. reply->pkt_type = PACKET_HOST;
  591. if (netif_rx_ni(reply) == NET_RX_DROP)
  592. dev->stats.rx_dropped++;
  593. } else if (vxlan->flags & VXLAN_F_L3MISS)
  594. vxlan_ip_miss(dev, tip);
  595. out:
  596. consume_skb(skb);
  597. return NETDEV_TX_OK;
  598. }
  599. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  600. {
  601. struct vxlan_dev *vxlan = netdev_priv(dev);
  602. struct neighbour *n;
  603. struct iphdr *pip;
  604. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  605. return false;
  606. n = NULL;
  607. switch (ntohs(eth_hdr(skb)->h_proto)) {
  608. case ETH_P_IP:
  609. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  610. return false;
  611. pip = ip_hdr(skb);
  612. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  613. break;
  614. default:
  615. return false;
  616. }
  617. if (n) {
  618. bool diff;
  619. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  620. if (diff) {
  621. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  622. dev->addr_len);
  623. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  624. }
  625. neigh_release(n);
  626. return diff;
  627. } else if (vxlan->flags & VXLAN_F_L3MISS)
  628. vxlan_ip_miss(dev, pip->daddr);
  629. return false;
  630. }
  631. /* Extract dsfield from inner protocol */
  632. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  633. const struct sk_buff *skb)
  634. {
  635. if (skb->protocol == htons(ETH_P_IP))
  636. return iph->tos;
  637. else if (skb->protocol == htons(ETH_P_IPV6))
  638. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  639. else
  640. return 0;
  641. }
  642. /* Propogate ECN bits out */
  643. static inline u8 vxlan_ecn_encap(u8 tos,
  644. const struct iphdr *iph,
  645. const struct sk_buff *skb)
  646. {
  647. u8 inner = vxlan_get_dsfield(iph, skb);
  648. return INET_ECN_encapsulate(tos, inner);
  649. }
  650. static void vxlan_sock_free(struct sk_buff *skb)
  651. {
  652. sock_put(skb->sk);
  653. }
  654. /* On transmit, associate with the tunnel socket */
  655. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  656. {
  657. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  658. struct sock *sk = vn->sock->sk;
  659. skb_orphan(skb);
  660. sock_hold(sk);
  661. skb->sk = sk;
  662. skb->destructor = vxlan_sock_free;
  663. }
  664. /* Compute source port for outgoing packet
  665. * first choice to use L4 flow hash since it will spread
  666. * better and maybe available from hardware
  667. * secondary choice is to use jhash on the Ethernet header
  668. */
  669. static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  670. {
  671. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  672. u32 hash;
  673. hash = skb_get_rxhash(skb);
  674. if (!hash)
  675. hash = jhash(skb->data, 2 * ETH_ALEN,
  676. (__force u32) skb->protocol);
  677. return (((u64) hash * range) >> 32) + vxlan->port_min;
  678. }
  679. /* Transmit local packets over Vxlan
  680. *
  681. * Outer IP header inherits ECN and DF from inner header.
  682. * Outer UDP destination is the VXLAN assigned port.
  683. * source port is based on hash of flow
  684. */
  685. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  686. {
  687. struct vxlan_dev *vxlan = netdev_priv(dev);
  688. struct rtable *rt;
  689. const struct iphdr *old_iph;
  690. struct ethhdr *eth;
  691. struct iphdr *iph;
  692. struct vxlanhdr *vxh;
  693. struct udphdr *uh;
  694. struct flowi4 fl4;
  695. unsigned int pkt_len = skb->len;
  696. __be32 dst;
  697. __u16 src_port;
  698. __be16 df = 0;
  699. __u8 tos, ttl;
  700. int err;
  701. bool did_rsc = false;
  702. const struct vxlan_fdb *f;
  703. skb_reset_mac_header(skb);
  704. eth = eth_hdr(skb);
  705. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  706. return arp_reduce(dev, skb);
  707. else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
  708. did_rsc = route_shortcircuit(dev, skb);
  709. f = vxlan_find_mac(vxlan, eth->h_dest);
  710. if (f == NULL) {
  711. did_rsc = false;
  712. dst = vxlan->gaddr;
  713. if (!dst && (vxlan->flags & VXLAN_F_L2MISS) &&
  714. !is_multicast_ether_addr(eth->h_dest))
  715. vxlan_fdb_miss(vxlan, eth->h_dest);
  716. } else
  717. dst = f->remote_ip;
  718. if (!dst) {
  719. if (did_rsc) {
  720. __skb_pull(skb, skb_network_offset(skb));
  721. skb->ip_summed = CHECKSUM_NONE;
  722. skb->pkt_type = PACKET_HOST;
  723. /* short-circuited back to local bridge */
  724. if (netif_rx(skb) == NET_RX_SUCCESS) {
  725. struct vxlan_stats *stats =
  726. this_cpu_ptr(vxlan->stats);
  727. u64_stats_update_begin(&stats->syncp);
  728. stats->tx_packets++;
  729. stats->tx_bytes += pkt_len;
  730. u64_stats_update_end(&stats->syncp);
  731. } else {
  732. dev->stats.tx_errors++;
  733. dev->stats.tx_aborted_errors++;
  734. }
  735. return NETDEV_TX_OK;
  736. }
  737. goto drop;
  738. }
  739. if (!skb->encapsulation) {
  740. skb_reset_inner_headers(skb);
  741. skb->encapsulation = 1;
  742. }
  743. /* Need space for new headers (invalidates iph ptr) */
  744. if (skb_cow_head(skb, VXLAN_HEADROOM))
  745. goto drop;
  746. old_iph = ip_hdr(skb);
  747. ttl = vxlan->ttl;
  748. if (!ttl && IN_MULTICAST(ntohl(dst)))
  749. ttl = 1;
  750. tos = vxlan->tos;
  751. if (tos == 1)
  752. tos = vxlan_get_dsfield(old_iph, skb);
  753. src_port = vxlan_src_port(vxlan, skb);
  754. memset(&fl4, 0, sizeof(fl4));
  755. fl4.flowi4_oif = vxlan->link;
  756. fl4.flowi4_tos = RT_TOS(tos);
  757. fl4.daddr = dst;
  758. fl4.saddr = vxlan->saddr;
  759. rt = ip_route_output_key(dev_net(dev), &fl4);
  760. if (IS_ERR(rt)) {
  761. netdev_dbg(dev, "no route to %pI4\n", &dst);
  762. dev->stats.tx_carrier_errors++;
  763. goto tx_error;
  764. }
  765. if (rt->dst.dev == dev) {
  766. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  767. ip_rt_put(rt);
  768. dev->stats.collisions++;
  769. goto tx_error;
  770. }
  771. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  772. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  773. IPSKB_REROUTED);
  774. skb_dst_drop(skb);
  775. skb_dst_set(skb, &rt->dst);
  776. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  777. vxh->vx_flags = htonl(VXLAN_FLAGS);
  778. vxh->vx_vni = htonl(vxlan->vni << 8);
  779. __skb_push(skb, sizeof(*uh));
  780. skb_reset_transport_header(skb);
  781. uh = udp_hdr(skb);
  782. uh->dest = htons(vxlan_port);
  783. uh->source = htons(src_port);
  784. uh->len = htons(skb->len);
  785. uh->check = 0;
  786. __skb_push(skb, sizeof(*iph));
  787. skb_reset_network_header(skb);
  788. iph = ip_hdr(skb);
  789. iph->version = 4;
  790. iph->ihl = sizeof(struct iphdr) >> 2;
  791. iph->frag_off = df;
  792. iph->protocol = IPPROTO_UDP;
  793. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  794. iph->daddr = dst;
  795. iph->saddr = fl4.saddr;
  796. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  797. vxlan_set_owner(dev, skb);
  798. /* See iptunnel_xmit() */
  799. if (skb->ip_summed != CHECKSUM_PARTIAL)
  800. skb->ip_summed = CHECKSUM_NONE;
  801. ip_select_ident(iph, &rt->dst, NULL);
  802. err = ip_local_out(skb);
  803. if (likely(net_xmit_eval(err) == 0)) {
  804. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  805. u64_stats_update_begin(&stats->syncp);
  806. stats->tx_packets++;
  807. stats->tx_bytes += pkt_len;
  808. u64_stats_update_end(&stats->syncp);
  809. } else {
  810. dev->stats.tx_errors++;
  811. dev->stats.tx_aborted_errors++;
  812. }
  813. return NETDEV_TX_OK;
  814. drop:
  815. dev->stats.tx_dropped++;
  816. goto tx_free;
  817. tx_error:
  818. dev->stats.tx_errors++;
  819. tx_free:
  820. dev_kfree_skb(skb);
  821. return NETDEV_TX_OK;
  822. }
  823. /* Walk the forwarding table and purge stale entries */
  824. static void vxlan_cleanup(unsigned long arg)
  825. {
  826. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  827. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  828. unsigned int h;
  829. if (!netif_running(vxlan->dev))
  830. return;
  831. spin_lock_bh(&vxlan->hash_lock);
  832. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  833. struct hlist_node *p, *n;
  834. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  835. struct vxlan_fdb *f
  836. = container_of(p, struct vxlan_fdb, hlist);
  837. unsigned long timeout;
  838. if (f->state & NUD_PERMANENT)
  839. continue;
  840. timeout = f->used + vxlan->age_interval * HZ;
  841. if (time_before_eq(timeout, jiffies)) {
  842. netdev_dbg(vxlan->dev,
  843. "garbage collect %pM\n",
  844. f->eth_addr);
  845. f->state = NUD_STALE;
  846. vxlan_fdb_destroy(vxlan, f);
  847. } else if (time_before(timeout, next_timer))
  848. next_timer = timeout;
  849. }
  850. }
  851. spin_unlock_bh(&vxlan->hash_lock);
  852. mod_timer(&vxlan->age_timer, next_timer);
  853. }
  854. /* Setup stats when device is created */
  855. static int vxlan_init(struct net_device *dev)
  856. {
  857. struct vxlan_dev *vxlan = netdev_priv(dev);
  858. vxlan->stats = alloc_percpu(struct vxlan_stats);
  859. if (!vxlan->stats)
  860. return -ENOMEM;
  861. return 0;
  862. }
  863. /* Start ageing timer and join group when device is brought up */
  864. static int vxlan_open(struct net_device *dev)
  865. {
  866. struct vxlan_dev *vxlan = netdev_priv(dev);
  867. int err;
  868. if (vxlan->gaddr) {
  869. err = vxlan_join_group(dev);
  870. if (err)
  871. return err;
  872. }
  873. if (vxlan->age_interval)
  874. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  875. return 0;
  876. }
  877. /* Purge the forwarding table */
  878. static void vxlan_flush(struct vxlan_dev *vxlan)
  879. {
  880. unsigned h;
  881. spin_lock_bh(&vxlan->hash_lock);
  882. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  883. struct hlist_node *p, *n;
  884. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  885. struct vxlan_fdb *f
  886. = container_of(p, struct vxlan_fdb, hlist);
  887. vxlan_fdb_destroy(vxlan, f);
  888. }
  889. }
  890. spin_unlock_bh(&vxlan->hash_lock);
  891. }
  892. /* Cleanup timer and forwarding table on shutdown */
  893. static int vxlan_stop(struct net_device *dev)
  894. {
  895. struct vxlan_dev *vxlan = netdev_priv(dev);
  896. if (vxlan->gaddr)
  897. vxlan_leave_group(dev);
  898. del_timer_sync(&vxlan->age_timer);
  899. vxlan_flush(vxlan);
  900. return 0;
  901. }
  902. /* Merge per-cpu statistics */
  903. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  904. struct rtnl_link_stats64 *stats)
  905. {
  906. struct vxlan_dev *vxlan = netdev_priv(dev);
  907. struct vxlan_stats tmp, sum = { 0 };
  908. unsigned int cpu;
  909. for_each_possible_cpu(cpu) {
  910. unsigned int start;
  911. const struct vxlan_stats *stats
  912. = per_cpu_ptr(vxlan->stats, cpu);
  913. do {
  914. start = u64_stats_fetch_begin_bh(&stats->syncp);
  915. memcpy(&tmp, stats, sizeof(tmp));
  916. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  917. sum.tx_bytes += tmp.tx_bytes;
  918. sum.tx_packets += tmp.tx_packets;
  919. sum.rx_bytes += tmp.rx_bytes;
  920. sum.rx_packets += tmp.rx_packets;
  921. }
  922. stats->tx_bytes = sum.tx_bytes;
  923. stats->tx_packets = sum.tx_packets;
  924. stats->rx_bytes = sum.rx_bytes;
  925. stats->rx_packets = sum.rx_packets;
  926. stats->multicast = dev->stats.multicast;
  927. stats->rx_length_errors = dev->stats.rx_length_errors;
  928. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  929. stats->rx_errors = dev->stats.rx_errors;
  930. stats->tx_dropped = dev->stats.tx_dropped;
  931. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  932. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  933. stats->collisions = dev->stats.collisions;
  934. stats->tx_errors = dev->stats.tx_errors;
  935. return stats;
  936. }
  937. /* Stub, nothing needs to be done. */
  938. static void vxlan_set_multicast_list(struct net_device *dev)
  939. {
  940. }
  941. static const struct net_device_ops vxlan_netdev_ops = {
  942. .ndo_init = vxlan_init,
  943. .ndo_open = vxlan_open,
  944. .ndo_stop = vxlan_stop,
  945. .ndo_start_xmit = vxlan_xmit,
  946. .ndo_get_stats64 = vxlan_stats64,
  947. .ndo_set_rx_mode = vxlan_set_multicast_list,
  948. .ndo_change_mtu = eth_change_mtu,
  949. .ndo_validate_addr = eth_validate_addr,
  950. .ndo_set_mac_address = eth_mac_addr,
  951. .ndo_fdb_add = vxlan_fdb_add,
  952. .ndo_fdb_del = vxlan_fdb_delete,
  953. .ndo_fdb_dump = vxlan_fdb_dump,
  954. };
  955. /* Info for udev, that this is a virtual tunnel endpoint */
  956. static struct device_type vxlan_type = {
  957. .name = "vxlan",
  958. };
  959. static void vxlan_free(struct net_device *dev)
  960. {
  961. struct vxlan_dev *vxlan = netdev_priv(dev);
  962. free_percpu(vxlan->stats);
  963. free_netdev(dev);
  964. }
  965. /* Initialize the device structure. */
  966. static void vxlan_setup(struct net_device *dev)
  967. {
  968. struct vxlan_dev *vxlan = netdev_priv(dev);
  969. unsigned h;
  970. int low, high;
  971. eth_hw_addr_random(dev);
  972. ether_setup(dev);
  973. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  974. dev->netdev_ops = &vxlan_netdev_ops;
  975. dev->destructor = vxlan_free;
  976. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  977. dev->tx_queue_len = 0;
  978. dev->features |= NETIF_F_LLTX;
  979. dev->features |= NETIF_F_NETNS_LOCAL;
  980. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  981. dev->features |= NETIF_F_RXCSUM;
  982. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  983. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  984. spin_lock_init(&vxlan->hash_lock);
  985. init_timer_deferrable(&vxlan->age_timer);
  986. vxlan->age_timer.function = vxlan_cleanup;
  987. vxlan->age_timer.data = (unsigned long) vxlan;
  988. inet_get_local_port_range(&low, &high);
  989. vxlan->port_min = low;
  990. vxlan->port_max = high;
  991. vxlan->dev = dev;
  992. for (h = 0; h < FDB_HASH_SIZE; ++h)
  993. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  994. }
  995. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  996. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  997. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  998. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  999. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1000. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1001. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1002. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1003. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1004. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1005. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1006. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1007. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1008. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1009. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1010. };
  1011. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1012. {
  1013. if (tb[IFLA_ADDRESS]) {
  1014. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1015. pr_debug("invalid link address (not ethernet)\n");
  1016. return -EINVAL;
  1017. }
  1018. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1019. pr_debug("invalid all zero ethernet address\n");
  1020. return -EADDRNOTAVAIL;
  1021. }
  1022. }
  1023. if (!data)
  1024. return -EINVAL;
  1025. if (data[IFLA_VXLAN_ID]) {
  1026. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1027. if (id >= VXLAN_VID_MASK)
  1028. return -ERANGE;
  1029. }
  1030. if (data[IFLA_VXLAN_GROUP]) {
  1031. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1032. if (!IN_MULTICAST(ntohl(gaddr))) {
  1033. pr_debug("group address is not IPv4 multicast\n");
  1034. return -EADDRNOTAVAIL;
  1035. }
  1036. }
  1037. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1038. const struct ifla_vxlan_port_range *p
  1039. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1040. if (ntohs(p->high) < ntohs(p->low)) {
  1041. pr_debug("port range %u .. %u not valid\n",
  1042. ntohs(p->low), ntohs(p->high));
  1043. return -EINVAL;
  1044. }
  1045. }
  1046. return 0;
  1047. }
  1048. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1049. struct nlattr *tb[], struct nlattr *data[])
  1050. {
  1051. struct vxlan_dev *vxlan = netdev_priv(dev);
  1052. __u32 vni;
  1053. int err;
  1054. if (!data[IFLA_VXLAN_ID])
  1055. return -EINVAL;
  1056. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1057. if (vxlan_find_vni(net, vni)) {
  1058. pr_info("duplicate VNI %u\n", vni);
  1059. return -EEXIST;
  1060. }
  1061. vxlan->vni = vni;
  1062. if (data[IFLA_VXLAN_GROUP])
  1063. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1064. if (data[IFLA_VXLAN_LOCAL])
  1065. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1066. if (data[IFLA_VXLAN_LINK] &&
  1067. (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1068. struct net_device *lowerdev
  1069. = __dev_get_by_index(net, vxlan->link);
  1070. if (!lowerdev) {
  1071. pr_info("ifindex %d does not exist\n", vxlan->link);
  1072. return -ENODEV;
  1073. }
  1074. if (!tb[IFLA_MTU])
  1075. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1076. /* update header length based on lower device */
  1077. dev->hard_header_len = lowerdev->hard_header_len +
  1078. VXLAN_HEADROOM;
  1079. }
  1080. if (data[IFLA_VXLAN_TOS])
  1081. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1082. if (data[IFLA_VXLAN_TTL])
  1083. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1084. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1085. vxlan->flags |= VXLAN_F_LEARN;
  1086. if (data[IFLA_VXLAN_AGEING])
  1087. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1088. else
  1089. vxlan->age_interval = FDB_AGE_DEFAULT;
  1090. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1091. vxlan->flags |= VXLAN_F_PROXY;
  1092. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1093. vxlan->flags |= VXLAN_F_RSC;
  1094. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1095. vxlan->flags |= VXLAN_F_L2MISS;
  1096. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1097. vxlan->flags |= VXLAN_F_L3MISS;
  1098. if (data[IFLA_VXLAN_LIMIT])
  1099. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1100. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1101. const struct ifla_vxlan_port_range *p
  1102. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1103. vxlan->port_min = ntohs(p->low);
  1104. vxlan->port_max = ntohs(p->high);
  1105. }
  1106. err = register_netdevice(dev);
  1107. if (!err)
  1108. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  1109. return err;
  1110. }
  1111. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1112. {
  1113. struct vxlan_dev *vxlan = netdev_priv(dev);
  1114. hlist_del_rcu(&vxlan->hlist);
  1115. unregister_netdevice_queue(dev, head);
  1116. }
  1117. static size_t vxlan_get_size(const struct net_device *dev)
  1118. {
  1119. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1120. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1121. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1122. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1123. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1124. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1125. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1126. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1127. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1128. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1129. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1130. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1131. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1132. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1133. 0;
  1134. }
  1135. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1136. {
  1137. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1138. struct ifla_vxlan_port_range ports = {
  1139. .low = htons(vxlan->port_min),
  1140. .high = htons(vxlan->port_max),
  1141. };
  1142. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  1143. goto nla_put_failure;
  1144. if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  1145. goto nla_put_failure;
  1146. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  1147. goto nla_put_failure;
  1148. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1149. goto nla_put_failure;
  1150. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1151. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1152. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1153. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1154. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1155. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1156. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1157. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1158. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1159. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1160. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1161. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1162. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  1163. goto nla_put_failure;
  1164. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1165. goto nla_put_failure;
  1166. return 0;
  1167. nla_put_failure:
  1168. return -EMSGSIZE;
  1169. }
  1170. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1171. .kind = "vxlan",
  1172. .maxtype = IFLA_VXLAN_MAX,
  1173. .policy = vxlan_policy,
  1174. .priv_size = sizeof(struct vxlan_dev),
  1175. .setup = vxlan_setup,
  1176. .validate = vxlan_validate,
  1177. .newlink = vxlan_newlink,
  1178. .dellink = vxlan_dellink,
  1179. .get_size = vxlan_get_size,
  1180. .fill_info = vxlan_fill_info,
  1181. };
  1182. static __net_init int vxlan_init_net(struct net *net)
  1183. {
  1184. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1185. struct sock *sk;
  1186. struct sockaddr_in vxlan_addr = {
  1187. .sin_family = AF_INET,
  1188. .sin_addr.s_addr = htonl(INADDR_ANY),
  1189. };
  1190. int rc;
  1191. unsigned h;
  1192. /* Create UDP socket for encapsulation receive. */
  1193. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  1194. if (rc < 0) {
  1195. pr_debug("UDP socket create failed\n");
  1196. return rc;
  1197. }
  1198. /* Put in proper namespace */
  1199. sk = vn->sock->sk;
  1200. sk_change_net(sk, net);
  1201. vxlan_addr.sin_port = htons(vxlan_port);
  1202. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  1203. sizeof(vxlan_addr));
  1204. if (rc < 0) {
  1205. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1206. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1207. sk_release_kernel(sk);
  1208. vn->sock = NULL;
  1209. return rc;
  1210. }
  1211. /* Disable multicast loopback */
  1212. inet_sk(sk)->mc_loop = 0;
  1213. /* Mark socket as an encapsulation socket. */
  1214. udp_sk(sk)->encap_type = 1;
  1215. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1216. udp_encap_enable();
  1217. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1218. INIT_HLIST_HEAD(&vn->vni_list[h]);
  1219. return 0;
  1220. }
  1221. static __net_exit void vxlan_exit_net(struct net *net)
  1222. {
  1223. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1224. if (vn->sock) {
  1225. sk_release_kernel(vn->sock->sk);
  1226. vn->sock = NULL;
  1227. }
  1228. }
  1229. static struct pernet_operations vxlan_net_ops = {
  1230. .init = vxlan_init_net,
  1231. .exit = vxlan_exit_net,
  1232. .id = &vxlan_net_id,
  1233. .size = sizeof(struct vxlan_net),
  1234. };
  1235. static int __init vxlan_init_module(void)
  1236. {
  1237. int rc;
  1238. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1239. rc = register_pernet_device(&vxlan_net_ops);
  1240. if (rc)
  1241. goto out1;
  1242. rc = rtnl_link_register(&vxlan_link_ops);
  1243. if (rc)
  1244. goto out2;
  1245. return 0;
  1246. out2:
  1247. unregister_pernet_device(&vxlan_net_ops);
  1248. out1:
  1249. return rc;
  1250. }
  1251. module_init(vxlan_init_module);
  1252. static void __exit vxlan_cleanup_module(void)
  1253. {
  1254. rtnl_link_unregister(&vxlan_link_ops);
  1255. unregister_pernet_device(&vxlan_net_ops);
  1256. }
  1257. module_exit(vxlan_cleanup_module);
  1258. MODULE_LICENSE("GPL");
  1259. MODULE_VERSION(VXLAN_VERSION);
  1260. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1261. MODULE_ALIAS_RTNL_LINK("vxlan");