vxlan.c 48 KB

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