vxlan.c 47 KB

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