vxlan.c 38 KB

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