vxlan.c 48 KB

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