vxlan.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/rculist.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/in.h>
  20. #include <linux/ip.h>
  21. #include <linux/udp.h>
  22. #include <linux/igmp.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/hash.h>
  27. #include <linux/ethtool.h>
  28. #include <net/arp.h>
  29. #include <net/ndisc.h>
  30. #include <net/ip.h>
  31. #include <net/ip_tunnels.h>
  32. #include <net/icmp.h>
  33. #include <net/udp.h>
  34. #include <net/rtnetlink.h>
  35. #include <net/route.h>
  36. #include <net/dsfield.h>
  37. #include <net/inet_ecn.h>
  38. #include <net/net_namespace.h>
  39. #include <net/netns/generic.h>
  40. #include <net/vxlan.h>
  41. #if IS_ENABLED(CONFIG_IPV6)
  42. #include <net/ipv6.h>
  43. #include <net/addrconf.h>
  44. #include <net/ip6_tunnel.h>
  45. #include <net/ip6_checksum.h>
  46. #endif
  47. #define VXLAN_VERSION "0.1"
  48. #define PORT_HASH_BITS 8
  49. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  50. #define VNI_HASH_BITS 10
  51. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  52. #define FDB_HASH_BITS 8
  53. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  54. #define FDB_AGE_DEFAULT 300 /* 5 min */
  55. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  56. #define VXLAN_N_VID (1u << 24)
  57. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  58. /* IP header + UDP + VXLAN + Ethernet header */
  59. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  60. /* IPv6 header + UDP + VXLAN + Ethernet header */
  61. #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
  62. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  63. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  64. /* VXLAN protocol header */
  65. struct vxlanhdr {
  66. __be32 vx_flags;
  67. __be32 vx_vni;
  68. };
  69. /* UDP port for VXLAN traffic.
  70. * The IANA assigned port is 4789, but the Linux default is 8472
  71. * for compatibility with early adopters.
  72. */
  73. static unsigned short vxlan_port __read_mostly = 8472;
  74. module_param_named(udp_port, vxlan_port, ushort, 0444);
  75. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  76. static bool log_ecn_error = true;
  77. module_param(log_ecn_error, bool, 0644);
  78. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  79. static int vxlan_net_id;
  80. static const u8 all_zeros_mac[ETH_ALEN];
  81. /* per-network namespace private data for this module */
  82. struct vxlan_net {
  83. struct list_head vxlan_list;
  84. struct hlist_head sock_list[PORT_HASH_SIZE];
  85. spinlock_t sock_lock;
  86. };
  87. union vxlan_addr {
  88. struct sockaddr_in sin;
  89. struct sockaddr_in6 sin6;
  90. struct sockaddr sa;
  91. };
  92. struct vxlan_rdst {
  93. union vxlan_addr remote_ip;
  94. __be16 remote_port;
  95. u32 remote_vni;
  96. u32 remote_ifindex;
  97. struct list_head list;
  98. struct rcu_head rcu;
  99. };
  100. /* Forwarding table entry */
  101. struct vxlan_fdb {
  102. struct hlist_node hlist; /* linked list of entries */
  103. struct rcu_head rcu;
  104. unsigned long updated; /* jiffies */
  105. unsigned long used;
  106. struct list_head remotes;
  107. u16 state; /* see ndm_state */
  108. u8 flags; /* see ndm_flags */
  109. u8 eth_addr[ETH_ALEN];
  110. };
  111. /* Pseudo network device */
  112. struct vxlan_dev {
  113. struct hlist_node hlist; /* vni hash table */
  114. struct list_head next; /* vxlan's per namespace list */
  115. struct vxlan_sock *vn_sock; /* listening socket */
  116. struct net_device *dev;
  117. struct vxlan_rdst default_dst; /* default destination */
  118. union vxlan_addr saddr; /* source address */
  119. __be16 dst_port;
  120. __u16 port_min; /* source port range */
  121. __u16 port_max;
  122. __u8 tos; /* TOS override */
  123. __u8 ttl;
  124. u32 flags; /* VXLAN_F_* below */
  125. struct work_struct sock_work;
  126. struct work_struct igmp_join;
  127. struct work_struct igmp_leave;
  128. unsigned long age_interval;
  129. struct timer_list age_timer;
  130. spinlock_t hash_lock;
  131. unsigned int addrcnt;
  132. unsigned int addrmax;
  133. struct hlist_head fdb_head[FDB_HASH_SIZE];
  134. };
  135. #define VXLAN_F_LEARN 0x01
  136. #define VXLAN_F_PROXY 0x02
  137. #define VXLAN_F_RSC 0x04
  138. #define VXLAN_F_L2MISS 0x08
  139. #define VXLAN_F_L3MISS 0x10
  140. #define VXLAN_F_IPV6 0x20 /* internal flag */
  141. /* salt for hash table */
  142. static u32 vxlan_salt __read_mostly;
  143. static struct workqueue_struct *vxlan_wq;
  144. static void vxlan_sock_work(struct work_struct *work);
  145. #if IS_ENABLED(CONFIG_IPV6)
  146. static inline
  147. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  148. {
  149. if (a->sa.sa_family != b->sa.sa_family)
  150. return false;
  151. if (a->sa.sa_family == AF_INET6)
  152. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  153. else
  154. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  155. }
  156. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  157. {
  158. if (ipa->sa.sa_family == AF_INET6)
  159. return ipv6_addr_any(&ipa->sin6.sin6_addr);
  160. else
  161. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  162. }
  163. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  164. {
  165. if (ipa->sa.sa_family == AF_INET6)
  166. return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
  167. else
  168. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  169. }
  170. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  171. {
  172. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  173. nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
  174. ip->sa.sa_family = AF_INET6;
  175. return 0;
  176. } else if (nla_len(nla) >= sizeof(__be32)) {
  177. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  178. ip->sa.sa_family = AF_INET;
  179. return 0;
  180. } else {
  181. return -EAFNOSUPPORT;
  182. }
  183. }
  184. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  185. const union vxlan_addr *ip)
  186. {
  187. if (ip->sa.sa_family == AF_INET6)
  188. return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
  189. else
  190. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  191. }
  192. #else /* !CONFIG_IPV6 */
  193. static inline
  194. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  195. {
  196. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  197. }
  198. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  199. {
  200. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  201. }
  202. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  203. {
  204. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  205. }
  206. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  207. {
  208. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  209. return -EAFNOSUPPORT;
  210. } else if (nla_len(nla) >= sizeof(__be32)) {
  211. ip->sin.sin_addr.s_addr = nla_get_be32(nla);
  212. ip->sa.sa_family = AF_INET;
  213. return 0;
  214. } else {
  215. return -EAFNOSUPPORT;
  216. }
  217. }
  218. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  219. const union vxlan_addr *ip)
  220. {
  221. return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
  222. }
  223. #endif
  224. /* Virtual Network hash table head */
  225. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  226. {
  227. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  228. }
  229. /* Socket hash table head */
  230. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  231. {
  232. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  233. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  234. }
  235. /* First remote destination for a forwarding entry.
  236. * Guaranteed to be non-NULL because remotes are never deleted.
  237. */
  238. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  239. {
  240. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  241. }
  242. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  243. {
  244. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  245. }
  246. /* Find VXLAN socket based on network namespace and UDP port */
  247. static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
  248. {
  249. struct vxlan_sock *vs;
  250. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  251. if (inet_sk(vs->sock->sk)->inet_sport == port)
  252. return vs;
  253. }
  254. return NULL;
  255. }
  256. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
  257. {
  258. struct vxlan_dev *vxlan;
  259. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  260. if (vxlan->default_dst.remote_vni == id)
  261. return vxlan;
  262. }
  263. return NULL;
  264. }
  265. /* Look up VNI in a per net namespace table */
  266. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
  267. {
  268. struct vxlan_sock *vs;
  269. vs = vxlan_find_sock(net, port);
  270. if (!vs)
  271. return NULL;
  272. return vxlan_vs_find_vni(vs, id);
  273. }
  274. /* Fill in neighbour message in skbuff. */
  275. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  276. const struct vxlan_fdb *fdb,
  277. u32 portid, u32 seq, int type, unsigned int flags,
  278. const struct vxlan_rdst *rdst)
  279. {
  280. unsigned long now = jiffies;
  281. struct nda_cacheinfo ci;
  282. struct nlmsghdr *nlh;
  283. struct ndmsg *ndm;
  284. bool send_ip, send_eth;
  285. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  286. if (nlh == NULL)
  287. return -EMSGSIZE;
  288. ndm = nlmsg_data(nlh);
  289. memset(ndm, 0, sizeof(*ndm));
  290. send_eth = send_ip = true;
  291. if (type == RTM_GETNEIGH) {
  292. ndm->ndm_family = AF_INET;
  293. send_ip = !vxlan_addr_any(&rdst->remote_ip);
  294. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  295. } else
  296. ndm->ndm_family = AF_BRIDGE;
  297. ndm->ndm_state = fdb->state;
  298. ndm->ndm_ifindex = vxlan->dev->ifindex;
  299. ndm->ndm_flags = fdb->flags;
  300. ndm->ndm_type = NDA_DST;
  301. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  302. goto nla_put_failure;
  303. if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
  304. goto nla_put_failure;
  305. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  306. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  307. goto nla_put_failure;
  308. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  309. nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
  310. goto nla_put_failure;
  311. if (rdst->remote_ifindex &&
  312. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  313. goto nla_put_failure;
  314. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  315. ci.ndm_confirmed = 0;
  316. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  317. ci.ndm_refcnt = 0;
  318. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  319. goto nla_put_failure;
  320. return nlmsg_end(skb, nlh);
  321. nla_put_failure:
  322. nlmsg_cancel(skb, nlh);
  323. return -EMSGSIZE;
  324. }
  325. static inline size_t vxlan_nlmsg_size(void)
  326. {
  327. return NLMSG_ALIGN(sizeof(struct ndmsg))
  328. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  329. + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
  330. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  331. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  332. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  333. + nla_total_size(sizeof(struct nda_cacheinfo));
  334. }
  335. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  336. struct vxlan_fdb *fdb, int type)
  337. {
  338. struct net *net = dev_net(vxlan->dev);
  339. struct sk_buff *skb;
  340. int err = -ENOBUFS;
  341. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  342. if (skb == NULL)
  343. goto errout;
  344. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
  345. first_remote_rtnl(fdb));
  346. if (err < 0) {
  347. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  348. WARN_ON(err == -EMSGSIZE);
  349. kfree_skb(skb);
  350. goto errout;
  351. }
  352. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  353. return;
  354. errout:
  355. if (err < 0)
  356. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  357. }
  358. static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
  359. {
  360. struct vxlan_dev *vxlan = netdev_priv(dev);
  361. struct vxlan_fdb f = {
  362. .state = NUD_STALE,
  363. };
  364. struct vxlan_rdst remote = {
  365. .remote_ip = *ipa, /* goes to NDA_DST */
  366. .remote_vni = VXLAN_N_VID,
  367. };
  368. INIT_LIST_HEAD(&f.remotes);
  369. list_add_rcu(&remote.list, &f.remotes);
  370. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  371. }
  372. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  373. {
  374. struct vxlan_fdb f = {
  375. .state = NUD_STALE,
  376. };
  377. INIT_LIST_HEAD(&f.remotes);
  378. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  379. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  380. }
  381. /* Hash Ethernet address */
  382. static u32 eth_hash(const unsigned char *addr)
  383. {
  384. u64 value = get_unaligned((u64 *)addr);
  385. /* only want 6 bytes */
  386. #ifdef __BIG_ENDIAN
  387. value >>= 16;
  388. #else
  389. value <<= 16;
  390. #endif
  391. return hash_64(value, FDB_HASH_BITS);
  392. }
  393. /* Hash chain to use given mac address */
  394. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  395. const u8 *mac)
  396. {
  397. return &vxlan->fdb_head[eth_hash(mac)];
  398. }
  399. /* Look up Ethernet address in forwarding table */
  400. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  401. const u8 *mac)
  402. {
  403. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  404. struct vxlan_fdb *f;
  405. hlist_for_each_entry_rcu(f, head, hlist) {
  406. if (ether_addr_equal(mac, f->eth_addr))
  407. return f;
  408. }
  409. return NULL;
  410. }
  411. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  412. const u8 *mac)
  413. {
  414. struct vxlan_fdb *f;
  415. f = __vxlan_find_mac(vxlan, mac);
  416. if (f)
  417. f->used = jiffies;
  418. return f;
  419. }
  420. /* caller should hold vxlan->hash_lock */
  421. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  422. union vxlan_addr *ip, __be16 port,
  423. __u32 vni, __u32 ifindex)
  424. {
  425. struct vxlan_rdst *rd;
  426. list_for_each_entry(rd, &f->remotes, list) {
  427. if (vxlan_addr_equal(&rd->remote_ip, ip) &&
  428. rd->remote_port == port &&
  429. rd->remote_vni == vni &&
  430. rd->remote_ifindex == ifindex)
  431. return rd;
  432. }
  433. return NULL;
  434. }
  435. /* Replace destination of unicast mac */
  436. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  437. union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
  438. {
  439. struct vxlan_rdst *rd;
  440. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  441. if (rd)
  442. return 0;
  443. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  444. if (!rd)
  445. return 0;
  446. rd->remote_ip = *ip;
  447. rd->remote_port = port;
  448. rd->remote_vni = vni;
  449. rd->remote_ifindex = ifindex;
  450. return 1;
  451. }
  452. /* Add/update destinations for multicast */
  453. static int vxlan_fdb_append(struct vxlan_fdb *f,
  454. union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
  455. {
  456. struct vxlan_rdst *rd;
  457. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  458. if (rd)
  459. return 0;
  460. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  461. if (rd == NULL)
  462. return -ENOBUFS;
  463. rd->remote_ip = *ip;
  464. rd->remote_port = port;
  465. rd->remote_vni = vni;
  466. rd->remote_ifindex = ifindex;
  467. list_add_tail_rcu(&rd->list, &f->remotes);
  468. return 1;
  469. }
  470. /* Notify netdevs that UDP port started listening */
  471. static void vxlan_notify_add_rx_port(struct sock *sk)
  472. {
  473. struct net_device *dev;
  474. struct net *net = sock_net(sk);
  475. sa_family_t sa_family = sk->sk_family;
  476. __be16 port = inet_sk(sk)->inet_sport;
  477. rcu_read_lock();
  478. for_each_netdev_rcu(net, dev) {
  479. if (dev->netdev_ops->ndo_add_vxlan_port)
  480. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  481. port);
  482. }
  483. rcu_read_unlock();
  484. }
  485. /* Notify netdevs that UDP port is no more listening */
  486. static void vxlan_notify_del_rx_port(struct sock *sk)
  487. {
  488. struct net_device *dev;
  489. struct net *net = sock_net(sk);
  490. sa_family_t sa_family = sk->sk_family;
  491. __be16 port = inet_sk(sk)->inet_sport;
  492. rcu_read_lock();
  493. for_each_netdev_rcu(net, dev) {
  494. if (dev->netdev_ops->ndo_del_vxlan_port)
  495. dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
  496. port);
  497. }
  498. rcu_read_unlock();
  499. }
  500. /* Add new entry to forwarding table -- assumes lock held */
  501. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  502. const u8 *mac, union vxlan_addr *ip,
  503. __u16 state, __u16 flags,
  504. __be16 port, __u32 vni, __u32 ifindex,
  505. __u8 ndm_flags)
  506. {
  507. struct vxlan_fdb *f;
  508. int notify = 0;
  509. f = __vxlan_find_mac(vxlan, mac);
  510. if (f) {
  511. if (flags & NLM_F_EXCL) {
  512. netdev_dbg(vxlan->dev,
  513. "lost race to create %pM\n", mac);
  514. return -EEXIST;
  515. }
  516. if (f->state != state) {
  517. f->state = state;
  518. f->updated = jiffies;
  519. notify = 1;
  520. }
  521. if (f->flags != ndm_flags) {
  522. f->flags = ndm_flags;
  523. f->updated = jiffies;
  524. notify = 1;
  525. }
  526. if ((flags & NLM_F_REPLACE)) {
  527. /* Only change unicasts */
  528. if (!(is_multicast_ether_addr(f->eth_addr) ||
  529. is_zero_ether_addr(f->eth_addr))) {
  530. int rc = vxlan_fdb_replace(f, ip, port, vni,
  531. ifindex);
  532. if (rc < 0)
  533. return rc;
  534. notify |= rc;
  535. } else
  536. return -EOPNOTSUPP;
  537. }
  538. if ((flags & NLM_F_APPEND) &&
  539. (is_multicast_ether_addr(f->eth_addr) ||
  540. is_zero_ether_addr(f->eth_addr))) {
  541. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
  542. if (rc < 0)
  543. return rc;
  544. notify |= rc;
  545. }
  546. } else {
  547. if (!(flags & NLM_F_CREATE))
  548. return -ENOENT;
  549. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  550. return -ENOSPC;
  551. /* Disallow replace to add a multicast entry */
  552. if ((flags & NLM_F_REPLACE) &&
  553. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  554. return -EOPNOTSUPP;
  555. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  556. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  557. if (!f)
  558. return -ENOMEM;
  559. notify = 1;
  560. f->state = state;
  561. f->flags = ndm_flags;
  562. f->updated = f->used = jiffies;
  563. INIT_LIST_HEAD(&f->remotes);
  564. memcpy(f->eth_addr, mac, ETH_ALEN);
  565. vxlan_fdb_append(f, ip, port, vni, ifindex);
  566. ++vxlan->addrcnt;
  567. hlist_add_head_rcu(&f->hlist,
  568. vxlan_fdb_head(vxlan, mac));
  569. }
  570. if (notify)
  571. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  572. return 0;
  573. }
  574. static void vxlan_fdb_free(struct rcu_head *head)
  575. {
  576. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  577. struct vxlan_rdst *rd, *nd;
  578. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  579. kfree(rd);
  580. kfree(f);
  581. }
  582. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  583. {
  584. netdev_dbg(vxlan->dev,
  585. "delete %pM\n", f->eth_addr);
  586. --vxlan->addrcnt;
  587. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  588. hlist_del_rcu(&f->hlist);
  589. call_rcu(&f->rcu, vxlan_fdb_free);
  590. }
  591. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  592. union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
  593. {
  594. struct net *net = dev_net(vxlan->dev);
  595. int err;
  596. if (tb[NDA_DST]) {
  597. err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
  598. if (err)
  599. return err;
  600. } else {
  601. union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
  602. if (remote->sa.sa_family == AF_INET) {
  603. ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  604. ip->sa.sa_family = AF_INET;
  605. #if IS_ENABLED(CONFIG_IPV6)
  606. } else {
  607. ip->sin6.sin6_addr = in6addr_any;
  608. ip->sa.sa_family = AF_INET6;
  609. #endif
  610. }
  611. }
  612. if (tb[NDA_PORT]) {
  613. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  614. return -EINVAL;
  615. *port = nla_get_be16(tb[NDA_PORT]);
  616. } else {
  617. *port = vxlan->dst_port;
  618. }
  619. if (tb[NDA_VNI]) {
  620. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  621. return -EINVAL;
  622. *vni = nla_get_u32(tb[NDA_VNI]);
  623. } else {
  624. *vni = vxlan->default_dst.remote_vni;
  625. }
  626. if (tb[NDA_IFINDEX]) {
  627. struct net_device *tdev;
  628. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  629. return -EINVAL;
  630. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  631. tdev = dev_get_by_index(net, *ifindex);
  632. if (!tdev)
  633. return -EADDRNOTAVAIL;
  634. dev_put(tdev);
  635. } else {
  636. *ifindex = 0;
  637. }
  638. return 0;
  639. }
  640. /* Add static entry (via netlink) */
  641. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  642. struct net_device *dev,
  643. const unsigned char *addr, u16 flags)
  644. {
  645. struct vxlan_dev *vxlan = netdev_priv(dev);
  646. /* struct net *net = dev_net(vxlan->dev); */
  647. union vxlan_addr ip;
  648. __be16 port;
  649. u32 vni, ifindex;
  650. int err;
  651. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  652. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  653. ndm->ndm_state);
  654. return -EINVAL;
  655. }
  656. if (tb[NDA_DST] == NULL)
  657. return -EINVAL;
  658. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  659. if (err)
  660. return err;
  661. spin_lock_bh(&vxlan->hash_lock);
  662. err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
  663. port, vni, ifindex, ndm->ndm_flags);
  664. spin_unlock_bh(&vxlan->hash_lock);
  665. return err;
  666. }
  667. /* Delete entry (via netlink) */
  668. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  669. struct net_device *dev,
  670. const unsigned char *addr)
  671. {
  672. struct vxlan_dev *vxlan = netdev_priv(dev);
  673. struct vxlan_fdb *f;
  674. struct vxlan_rdst *rd = NULL;
  675. union vxlan_addr ip;
  676. __be16 port;
  677. u32 vni, ifindex;
  678. int err;
  679. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  680. if (err)
  681. return err;
  682. err = -ENOENT;
  683. spin_lock_bh(&vxlan->hash_lock);
  684. f = vxlan_find_mac(vxlan, addr);
  685. if (!f)
  686. goto out;
  687. if (!vxlan_addr_any(&ip)) {
  688. rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
  689. if (!rd)
  690. goto out;
  691. }
  692. err = 0;
  693. /* remove a destination if it's not the only one on the list,
  694. * otherwise destroy the fdb entry
  695. */
  696. if (rd && !list_is_singular(&f->remotes)) {
  697. list_del_rcu(&rd->list);
  698. kfree_rcu(rd, rcu);
  699. goto out;
  700. }
  701. vxlan_fdb_destroy(vxlan, f);
  702. out:
  703. spin_unlock_bh(&vxlan->hash_lock);
  704. return err;
  705. }
  706. /* Dump forwarding table */
  707. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  708. struct net_device *dev, int idx)
  709. {
  710. struct vxlan_dev *vxlan = netdev_priv(dev);
  711. unsigned int h;
  712. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  713. struct vxlan_fdb *f;
  714. int err;
  715. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  716. struct vxlan_rdst *rd;
  717. if (idx < cb->args[0])
  718. goto skip;
  719. list_for_each_entry_rcu(rd, &f->remotes, list) {
  720. err = vxlan_fdb_info(skb, vxlan, f,
  721. NETLINK_CB(cb->skb).portid,
  722. cb->nlh->nlmsg_seq,
  723. RTM_NEWNEIGH,
  724. NLM_F_MULTI, rd);
  725. if (err < 0)
  726. goto out;
  727. }
  728. skip:
  729. ++idx;
  730. }
  731. }
  732. out:
  733. return idx;
  734. }
  735. /* Watch incoming packets to learn mapping between Ethernet address
  736. * and Tunnel endpoint.
  737. * Return true if packet is bogus and should be droppped.
  738. */
  739. static bool vxlan_snoop(struct net_device *dev,
  740. union vxlan_addr *src_ip, const u8 *src_mac)
  741. {
  742. struct vxlan_dev *vxlan = netdev_priv(dev);
  743. struct vxlan_fdb *f;
  744. f = vxlan_find_mac(vxlan, src_mac);
  745. if (likely(f)) {
  746. struct vxlan_rdst *rdst = first_remote_rcu(f);
  747. if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
  748. return false;
  749. /* Don't migrate static entries, drop packets */
  750. if (f->state & NUD_NOARP)
  751. return true;
  752. if (net_ratelimit())
  753. netdev_info(dev,
  754. "%pM migrated from %pIS to %pIS\n",
  755. src_mac, &rdst->remote_ip, &src_ip);
  756. rdst->remote_ip = *src_ip;
  757. f->updated = jiffies;
  758. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  759. } else {
  760. /* learned new entry */
  761. spin_lock(&vxlan->hash_lock);
  762. /* close off race between vxlan_flush and incoming packets */
  763. if (netif_running(dev))
  764. vxlan_fdb_create(vxlan, src_mac, src_ip,
  765. NUD_REACHABLE,
  766. NLM_F_EXCL|NLM_F_CREATE,
  767. vxlan->dst_port,
  768. vxlan->default_dst.remote_vni,
  769. 0, NTF_SELF);
  770. spin_unlock(&vxlan->hash_lock);
  771. }
  772. return false;
  773. }
  774. /* See if multicast group is already in use by other ID */
  775. static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
  776. {
  777. struct vxlan_dev *vxlan;
  778. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  779. if (!netif_running(vxlan->dev))
  780. continue;
  781. if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
  782. remote_ip))
  783. return true;
  784. }
  785. return false;
  786. }
  787. static void vxlan_sock_hold(struct vxlan_sock *vs)
  788. {
  789. atomic_inc(&vs->refcnt);
  790. }
  791. void vxlan_sock_release(struct vxlan_sock *vs)
  792. {
  793. struct sock *sk = vs->sock->sk;
  794. struct net *net = sock_net(sk);
  795. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  796. if (!atomic_dec_and_test(&vs->refcnt))
  797. return;
  798. spin_lock(&vn->sock_lock);
  799. hlist_del_rcu(&vs->hlist);
  800. rcu_assign_sk_user_data(vs->sock->sk, NULL);
  801. vxlan_notify_del_rx_port(sk);
  802. spin_unlock(&vn->sock_lock);
  803. queue_work(vxlan_wq, &vs->del_work);
  804. }
  805. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  806. /* Callback to update multicast group membership when first VNI on
  807. * multicast asddress is brought up
  808. * Done as workqueue because ip_mc_join_group acquires RTNL.
  809. */
  810. static void vxlan_igmp_join(struct work_struct *work)
  811. {
  812. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  813. struct vxlan_sock *vs = vxlan->vn_sock;
  814. struct sock *sk = vs->sock->sk;
  815. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  816. int ifindex = vxlan->default_dst.remote_ifindex;
  817. lock_sock(sk);
  818. if (ip->sa.sa_family == AF_INET) {
  819. struct ip_mreqn mreq = {
  820. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  821. .imr_ifindex = ifindex,
  822. };
  823. ip_mc_join_group(sk, &mreq);
  824. #if IS_ENABLED(CONFIG_IPV6)
  825. } else {
  826. ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
  827. &ip->sin6.sin6_addr);
  828. #endif
  829. }
  830. release_sock(sk);
  831. vxlan_sock_release(vs);
  832. dev_put(vxlan->dev);
  833. }
  834. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  835. static void vxlan_igmp_leave(struct work_struct *work)
  836. {
  837. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  838. struct vxlan_sock *vs = vxlan->vn_sock;
  839. struct sock *sk = vs->sock->sk;
  840. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  841. int ifindex = vxlan->default_dst.remote_ifindex;
  842. lock_sock(sk);
  843. if (ip->sa.sa_family == AF_INET) {
  844. struct ip_mreqn mreq = {
  845. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  846. .imr_ifindex = ifindex,
  847. };
  848. ip_mc_leave_group(sk, &mreq);
  849. #if IS_ENABLED(CONFIG_IPV6)
  850. } else {
  851. ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
  852. &ip->sin6.sin6_addr);
  853. #endif
  854. }
  855. release_sock(sk);
  856. vxlan_sock_release(vs);
  857. dev_put(vxlan->dev);
  858. }
  859. /* Callback from net/ipv4/udp.c to receive packets */
  860. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  861. {
  862. struct vxlan_sock *vs;
  863. struct vxlanhdr *vxh;
  864. __be16 port;
  865. /* Need Vxlan and inner Ethernet header to be present */
  866. if (!pskb_may_pull(skb, VXLAN_HLEN))
  867. goto error;
  868. /* Return packets with reserved bits set */
  869. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  870. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  871. (vxh->vx_vni & htonl(0xff))) {
  872. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  873. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  874. goto error;
  875. }
  876. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  877. goto drop;
  878. port = inet_sk(sk)->inet_sport;
  879. vs = rcu_dereference_sk_user_data(sk);
  880. if (!vs)
  881. goto drop;
  882. vs->rcv(vs, skb, vxh->vx_vni);
  883. return 0;
  884. drop:
  885. /* Consume bad packet */
  886. kfree_skb(skb);
  887. return 0;
  888. error:
  889. /* Return non vxlan pkt */
  890. return 1;
  891. }
  892. static void vxlan_rcv(struct vxlan_sock *vs,
  893. struct sk_buff *skb, __be32 vx_vni)
  894. {
  895. struct iphdr *oip = NULL;
  896. struct ipv6hdr *oip6 = NULL;
  897. struct vxlan_dev *vxlan;
  898. struct pcpu_tstats *stats;
  899. union vxlan_addr saddr;
  900. __u32 vni;
  901. int err = 0;
  902. union vxlan_addr *remote_ip;
  903. vni = ntohl(vx_vni) >> 8;
  904. /* Is this VNI defined? */
  905. vxlan = vxlan_vs_find_vni(vs, vni);
  906. if (!vxlan)
  907. goto drop;
  908. remote_ip = &vxlan->default_dst.remote_ip;
  909. skb_reset_mac_header(skb);
  910. skb->protocol = eth_type_trans(skb, vxlan->dev);
  911. /* Ignore packet loops (and multicast echo) */
  912. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  913. goto drop;
  914. /* Re-examine inner Ethernet packet */
  915. if (remote_ip->sa.sa_family == AF_INET) {
  916. oip = ip_hdr(skb);
  917. saddr.sin.sin_addr.s_addr = oip->saddr;
  918. saddr.sa.sa_family = AF_INET;
  919. #if IS_ENABLED(CONFIG_IPV6)
  920. } else {
  921. oip6 = ipv6_hdr(skb);
  922. saddr.sin6.sin6_addr = oip6->saddr;
  923. saddr.sa.sa_family = AF_INET6;
  924. #endif
  925. }
  926. if ((vxlan->flags & VXLAN_F_LEARN) &&
  927. vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
  928. goto drop;
  929. skb_reset_network_header(skb);
  930. /* If the NIC driver gave us an encapsulated packet with
  931. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  932. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  933. * for us. Otherwise force the upper layers to verify it.
  934. */
  935. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  936. !(vxlan->dev->features & NETIF_F_RXCSUM))
  937. skb->ip_summed = CHECKSUM_NONE;
  938. skb->encapsulation = 0;
  939. if (oip6)
  940. err = IP6_ECN_decapsulate(oip6, skb);
  941. if (oip)
  942. err = IP_ECN_decapsulate(oip, skb);
  943. if (unlikely(err)) {
  944. if (log_ecn_error) {
  945. if (oip6)
  946. net_info_ratelimited("non-ECT from %pI6\n",
  947. &oip6->saddr);
  948. if (oip)
  949. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  950. &oip->saddr, oip->tos);
  951. }
  952. if (err > 1) {
  953. ++vxlan->dev->stats.rx_frame_errors;
  954. ++vxlan->dev->stats.rx_errors;
  955. goto drop;
  956. }
  957. }
  958. stats = this_cpu_ptr(vxlan->dev->tstats);
  959. u64_stats_update_begin(&stats->syncp);
  960. stats->rx_packets++;
  961. stats->rx_bytes += skb->len;
  962. u64_stats_update_end(&stats->syncp);
  963. netif_rx(skb);
  964. return;
  965. drop:
  966. /* Consume bad packet */
  967. kfree_skb(skb);
  968. }
  969. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  970. {
  971. struct vxlan_dev *vxlan = netdev_priv(dev);
  972. struct arphdr *parp;
  973. u8 *arpptr, *sha;
  974. __be32 sip, tip;
  975. struct neighbour *n;
  976. if (dev->flags & IFF_NOARP)
  977. goto out;
  978. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  979. dev->stats.tx_dropped++;
  980. goto out;
  981. }
  982. parp = arp_hdr(skb);
  983. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  984. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  985. parp->ar_pro != htons(ETH_P_IP) ||
  986. parp->ar_op != htons(ARPOP_REQUEST) ||
  987. parp->ar_hln != dev->addr_len ||
  988. parp->ar_pln != 4)
  989. goto out;
  990. arpptr = (u8 *)parp + sizeof(struct arphdr);
  991. sha = arpptr;
  992. arpptr += dev->addr_len; /* sha */
  993. memcpy(&sip, arpptr, sizeof(sip));
  994. arpptr += sizeof(sip);
  995. arpptr += dev->addr_len; /* tha */
  996. memcpy(&tip, arpptr, sizeof(tip));
  997. if (ipv4_is_loopback(tip) ||
  998. ipv4_is_multicast(tip))
  999. goto out;
  1000. n = neigh_lookup(&arp_tbl, &tip, dev);
  1001. if (n) {
  1002. struct vxlan_fdb *f;
  1003. struct sk_buff *reply;
  1004. if (!(n->nud_state & NUD_CONNECTED)) {
  1005. neigh_release(n);
  1006. goto out;
  1007. }
  1008. f = vxlan_find_mac(vxlan, n->ha);
  1009. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1010. /* bridge-local neighbor */
  1011. neigh_release(n);
  1012. goto out;
  1013. }
  1014. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1015. n->ha, sha);
  1016. neigh_release(n);
  1017. skb_reset_mac_header(reply);
  1018. __skb_pull(reply, skb_network_offset(reply));
  1019. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1020. reply->pkt_type = PACKET_HOST;
  1021. if (netif_rx_ni(reply) == NET_RX_DROP)
  1022. dev->stats.rx_dropped++;
  1023. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1024. union vxlan_addr ipa = {
  1025. .sin.sin_addr.s_addr = tip,
  1026. .sa.sa_family = AF_INET,
  1027. };
  1028. vxlan_ip_miss(dev, &ipa);
  1029. }
  1030. out:
  1031. consume_skb(skb);
  1032. return NETDEV_TX_OK;
  1033. }
  1034. #if IS_ENABLED(CONFIG_IPV6)
  1035. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
  1036. {
  1037. struct vxlan_dev *vxlan = netdev_priv(dev);
  1038. struct neighbour *n;
  1039. union vxlan_addr ipa;
  1040. const struct ipv6hdr *iphdr;
  1041. const struct in6_addr *saddr, *daddr;
  1042. struct nd_msg *msg;
  1043. struct inet6_dev *in6_dev = NULL;
  1044. in6_dev = __in6_dev_get(dev);
  1045. if (!in6_dev)
  1046. goto out;
  1047. if (!pskb_may_pull(skb, skb->len))
  1048. goto out;
  1049. iphdr = ipv6_hdr(skb);
  1050. saddr = &iphdr->saddr;
  1051. daddr = &iphdr->daddr;
  1052. if (ipv6_addr_loopback(daddr) ||
  1053. ipv6_addr_is_multicast(daddr))
  1054. goto out;
  1055. msg = (struct nd_msg *)skb_transport_header(skb);
  1056. if (msg->icmph.icmp6_code != 0 ||
  1057. msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  1058. goto out;
  1059. n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
  1060. if (n) {
  1061. struct vxlan_fdb *f;
  1062. if (!(n->nud_state & NUD_CONNECTED)) {
  1063. neigh_release(n);
  1064. goto out;
  1065. }
  1066. f = vxlan_find_mac(vxlan, n->ha);
  1067. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1068. /* bridge-local neighbor */
  1069. neigh_release(n);
  1070. goto out;
  1071. }
  1072. ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
  1073. !!in6_dev->cnf.forwarding,
  1074. true, false, false);
  1075. neigh_release(n);
  1076. } else if (vxlan->flags & VXLAN_F_L3MISS) {
  1077. ipa.sin6.sin6_addr = *daddr;
  1078. ipa.sa.sa_family = AF_INET6;
  1079. vxlan_ip_miss(dev, &ipa);
  1080. }
  1081. out:
  1082. consume_skb(skb);
  1083. return NETDEV_TX_OK;
  1084. }
  1085. #endif
  1086. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1087. {
  1088. struct vxlan_dev *vxlan = netdev_priv(dev);
  1089. struct neighbour *n;
  1090. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1091. return false;
  1092. n = NULL;
  1093. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1094. case ETH_P_IP:
  1095. {
  1096. struct iphdr *pip;
  1097. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1098. return false;
  1099. pip = ip_hdr(skb);
  1100. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1101. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1102. union vxlan_addr ipa = {
  1103. .sin.sin_addr.s_addr = pip->daddr,
  1104. .sa.sa_family = AF_INET,
  1105. };
  1106. vxlan_ip_miss(dev, &ipa);
  1107. return false;
  1108. }
  1109. break;
  1110. }
  1111. #if IS_ENABLED(CONFIG_IPV6)
  1112. case ETH_P_IPV6:
  1113. {
  1114. struct ipv6hdr *pip6;
  1115. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1116. return false;
  1117. pip6 = ipv6_hdr(skb);
  1118. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1119. if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
  1120. union vxlan_addr ipa = {
  1121. .sin6.sin6_addr = pip6->daddr,
  1122. .sa.sa_family = AF_INET6,
  1123. };
  1124. vxlan_ip_miss(dev, &ipa);
  1125. return false;
  1126. }
  1127. break;
  1128. }
  1129. #endif
  1130. default:
  1131. return false;
  1132. }
  1133. if (n) {
  1134. bool diff;
  1135. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1136. if (diff) {
  1137. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1138. dev->addr_len);
  1139. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1140. }
  1141. neigh_release(n);
  1142. return diff;
  1143. }
  1144. return false;
  1145. }
  1146. static void vxlan_sock_put(struct sk_buff *skb)
  1147. {
  1148. sock_put(skb->sk);
  1149. }
  1150. /* On transmit, associate with the tunnel socket */
  1151. static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
  1152. {
  1153. skb_orphan(skb);
  1154. sock_hold(sk);
  1155. skb->sk = sk;
  1156. skb->destructor = vxlan_sock_put;
  1157. }
  1158. /* Compute source port for outgoing packet
  1159. * first choice to use L4 flow hash since it will spread
  1160. * better and maybe available from hardware
  1161. * secondary choice is to use jhash on the Ethernet header
  1162. */
  1163. __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
  1164. {
  1165. unsigned int range = (port_max - port_min) + 1;
  1166. u32 hash;
  1167. hash = skb_get_rxhash(skb);
  1168. if (!hash)
  1169. hash = jhash(skb->data, 2 * ETH_ALEN,
  1170. (__force u32) skb->protocol);
  1171. return htons((((u64) hash * range) >> 32) + port_min);
  1172. }
  1173. EXPORT_SYMBOL_GPL(vxlan_src_port);
  1174. static int handle_offloads(struct sk_buff *skb)
  1175. {
  1176. if (skb_is_gso(skb)) {
  1177. int err = skb_unclone(skb, GFP_ATOMIC);
  1178. if (unlikely(err))
  1179. return err;
  1180. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  1181. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  1182. skb->ip_summed = CHECKSUM_NONE;
  1183. return 0;
  1184. }
  1185. #if IS_ENABLED(CONFIG_IPV6)
  1186. static int vxlan6_xmit_skb(struct vxlan_sock *vs,
  1187. struct dst_entry *dst, struct sk_buff *skb,
  1188. struct net_device *dev, struct in6_addr *saddr,
  1189. struct in6_addr *daddr, __u8 prio, __u8 ttl,
  1190. __be16 src_port, __be16 dst_port, __be32 vni)
  1191. {
  1192. struct ipv6hdr *ip6h;
  1193. struct vxlanhdr *vxh;
  1194. struct udphdr *uh;
  1195. int min_headroom;
  1196. int err;
  1197. if (!skb->encapsulation) {
  1198. skb_reset_inner_headers(skb);
  1199. skb->encapsulation = 1;
  1200. }
  1201. skb_scrub_packet(skb, false);
  1202. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1203. + VXLAN_HLEN + sizeof(struct ipv6hdr)
  1204. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1205. /* Need space for new headers (invalidates iph ptr) */
  1206. err = skb_cow_head(skb, min_headroom);
  1207. if (unlikely(err))
  1208. return err;
  1209. if (vlan_tx_tag_present(skb)) {
  1210. if (WARN_ON(!__vlan_put_tag(skb,
  1211. skb->vlan_proto,
  1212. vlan_tx_tag_get(skb))))
  1213. return -ENOMEM;
  1214. skb->vlan_tci = 0;
  1215. }
  1216. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1217. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1218. vxh->vx_vni = vni;
  1219. __skb_push(skb, sizeof(*uh));
  1220. skb_reset_transport_header(skb);
  1221. uh = udp_hdr(skb);
  1222. uh->dest = dst_port;
  1223. uh->source = src_port;
  1224. uh->len = htons(skb->len);
  1225. uh->check = 0;
  1226. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1227. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  1228. IPSKB_REROUTED);
  1229. skb_dst_set(skb, dst);
  1230. if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
  1231. __wsum csum = skb_checksum(skb, 0, skb->len, 0);
  1232. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1233. uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
  1234. IPPROTO_UDP, csum);
  1235. if (uh->check == 0)
  1236. uh->check = CSUM_MANGLED_0;
  1237. } else {
  1238. skb->ip_summed = CHECKSUM_PARTIAL;
  1239. skb->csum_start = skb_transport_header(skb) - skb->head;
  1240. skb->csum_offset = offsetof(struct udphdr, check);
  1241. uh->check = ~csum_ipv6_magic(saddr, daddr,
  1242. skb->len, IPPROTO_UDP, 0);
  1243. }
  1244. __skb_push(skb, sizeof(*ip6h));
  1245. skb_reset_network_header(skb);
  1246. ip6h = ipv6_hdr(skb);
  1247. ip6h->version = 6;
  1248. ip6h->priority = prio;
  1249. ip6h->flow_lbl[0] = 0;
  1250. ip6h->flow_lbl[1] = 0;
  1251. ip6h->flow_lbl[2] = 0;
  1252. ip6h->payload_len = htons(skb->len);
  1253. ip6h->nexthdr = IPPROTO_UDP;
  1254. ip6h->hop_limit = ttl;
  1255. ip6h->daddr = *daddr;
  1256. ip6h->saddr = *saddr;
  1257. vxlan_set_owner(vs->sock->sk, skb);
  1258. err = handle_offloads(skb);
  1259. if (err)
  1260. return err;
  1261. ip6tunnel_xmit(skb, dev);
  1262. return 0;
  1263. }
  1264. #endif
  1265. int vxlan_xmit_skb(struct vxlan_sock *vs,
  1266. struct rtable *rt, struct sk_buff *skb,
  1267. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  1268. __be16 src_port, __be16 dst_port, __be32 vni)
  1269. {
  1270. struct vxlanhdr *vxh;
  1271. struct udphdr *uh;
  1272. int min_headroom;
  1273. int err;
  1274. if (!skb->encapsulation) {
  1275. skb_reset_inner_headers(skb);
  1276. skb->encapsulation = 1;
  1277. }
  1278. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  1279. + VXLAN_HLEN + sizeof(struct iphdr)
  1280. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  1281. /* Need space for new headers (invalidates iph ptr) */
  1282. err = skb_cow_head(skb, min_headroom);
  1283. if (unlikely(err))
  1284. return err;
  1285. if (vlan_tx_tag_present(skb)) {
  1286. if (WARN_ON(!__vlan_put_tag(skb,
  1287. skb->vlan_proto,
  1288. vlan_tx_tag_get(skb))))
  1289. return -ENOMEM;
  1290. skb->vlan_tci = 0;
  1291. }
  1292. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1293. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1294. vxh->vx_vni = vni;
  1295. __skb_push(skb, sizeof(*uh));
  1296. skb_reset_transport_header(skb);
  1297. uh = udp_hdr(skb);
  1298. uh->dest = dst_port;
  1299. uh->source = src_port;
  1300. uh->len = htons(skb->len);
  1301. uh->check = 0;
  1302. vxlan_set_owner(vs->sock->sk, skb);
  1303. err = handle_offloads(skb);
  1304. if (err)
  1305. return err;
  1306. return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
  1307. false);
  1308. }
  1309. EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
  1310. /* Bypass encapsulation if the destination is local */
  1311. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1312. struct vxlan_dev *dst_vxlan)
  1313. {
  1314. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  1315. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  1316. union vxlan_addr loopback;
  1317. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1318. skb->pkt_type = PACKET_HOST;
  1319. skb->encapsulation = 0;
  1320. skb->dev = dst_vxlan->dev;
  1321. __skb_pull(skb, skb_network_offset(skb));
  1322. if (remote_ip->sa.sa_family == AF_INET) {
  1323. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1324. loopback.sa.sa_family = AF_INET;
  1325. #if IS_ENABLED(CONFIG_IPV6)
  1326. } else {
  1327. loopback.sin6.sin6_addr = in6addr_loopback;
  1328. loopback.sa.sa_family = AF_INET6;
  1329. #endif
  1330. }
  1331. if (dst_vxlan->flags & VXLAN_F_LEARN)
  1332. vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
  1333. u64_stats_update_begin(&tx_stats->syncp);
  1334. tx_stats->tx_packets++;
  1335. tx_stats->tx_bytes += skb->len;
  1336. u64_stats_update_end(&tx_stats->syncp);
  1337. if (netif_rx(skb) == NET_RX_SUCCESS) {
  1338. u64_stats_update_begin(&rx_stats->syncp);
  1339. rx_stats->rx_packets++;
  1340. rx_stats->rx_bytes += skb->len;
  1341. u64_stats_update_end(&rx_stats->syncp);
  1342. } else {
  1343. skb->dev->stats.rx_dropped++;
  1344. }
  1345. }
  1346. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1347. struct vxlan_rdst *rdst, bool did_rsc)
  1348. {
  1349. struct vxlan_dev *vxlan = netdev_priv(dev);
  1350. struct rtable *rt = NULL;
  1351. const struct iphdr *old_iph;
  1352. struct flowi4 fl4;
  1353. union vxlan_addr *dst;
  1354. __be16 src_port = 0, dst_port;
  1355. u32 vni;
  1356. __be16 df = 0;
  1357. __u8 tos, ttl;
  1358. int err;
  1359. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  1360. vni = rdst->remote_vni;
  1361. dst = &rdst->remote_ip;
  1362. if (vxlan_addr_any(dst)) {
  1363. if (did_rsc) {
  1364. /* short-circuited back to local bridge */
  1365. vxlan_encap_bypass(skb, vxlan, vxlan);
  1366. return;
  1367. }
  1368. goto drop;
  1369. }
  1370. old_iph = ip_hdr(skb);
  1371. ttl = vxlan->ttl;
  1372. if (!ttl && vxlan_addr_multicast(dst))
  1373. ttl = 1;
  1374. tos = vxlan->tos;
  1375. if (tos == 1)
  1376. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1377. src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
  1378. if (dst->sa.sa_family == AF_INET) {
  1379. memset(&fl4, 0, sizeof(fl4));
  1380. fl4.flowi4_oif = rdst->remote_ifindex;
  1381. fl4.flowi4_tos = RT_TOS(tos);
  1382. fl4.daddr = dst->sin.sin_addr.s_addr;
  1383. fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
  1384. rt = ip_route_output_key(dev_net(dev), &fl4);
  1385. if (IS_ERR(rt)) {
  1386. netdev_dbg(dev, "no route to %pI4\n",
  1387. &dst->sin.sin_addr.s_addr);
  1388. dev->stats.tx_carrier_errors++;
  1389. goto tx_error;
  1390. }
  1391. if (rt->dst.dev == dev) {
  1392. netdev_dbg(dev, "circular route to %pI4\n",
  1393. &dst->sin.sin_addr.s_addr);
  1394. dev->stats.collisions++;
  1395. goto tx_error;
  1396. }
  1397. /* Bypass encapsulation if the destination is local */
  1398. if (rt->rt_flags & RTCF_LOCAL &&
  1399. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1400. struct vxlan_dev *dst_vxlan;
  1401. ip_rt_put(rt);
  1402. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1403. if (!dst_vxlan)
  1404. goto tx_error;
  1405. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1406. return;
  1407. }
  1408. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1409. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1410. err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
  1411. fl4.saddr, dst->sin.sin_addr.s_addr,
  1412. tos, ttl, df, src_port, dst_port,
  1413. htonl(vni << 8));
  1414. if (err < 0)
  1415. goto rt_tx_error;
  1416. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1417. #if IS_ENABLED(CONFIG_IPV6)
  1418. } else {
  1419. struct sock *sk = vxlan->vn_sock->sock->sk;
  1420. struct dst_entry *ndst;
  1421. struct flowi6 fl6;
  1422. u32 flags;
  1423. memset(&fl6, 0, sizeof(fl6));
  1424. fl6.flowi6_oif = rdst->remote_ifindex;
  1425. fl6.daddr = dst->sin6.sin6_addr;
  1426. fl6.saddr = vxlan->saddr.sin6.sin6_addr;
  1427. fl6.flowi6_proto = IPPROTO_UDP;
  1428. if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
  1429. netdev_dbg(dev, "no route to %pI6\n",
  1430. &dst->sin6.sin6_addr);
  1431. dev->stats.tx_carrier_errors++;
  1432. goto tx_error;
  1433. }
  1434. if (ndst->dev == dev) {
  1435. netdev_dbg(dev, "circular route to %pI6\n",
  1436. &dst->sin6.sin6_addr);
  1437. dst_release(ndst);
  1438. dev->stats.collisions++;
  1439. goto tx_error;
  1440. }
  1441. /* Bypass encapsulation if the destination is local */
  1442. flags = ((struct rt6_info *)ndst)->rt6i_flags;
  1443. if (flags & RTF_LOCAL &&
  1444. !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1445. struct vxlan_dev *dst_vxlan;
  1446. dst_release(ndst);
  1447. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1448. if (!dst_vxlan)
  1449. goto tx_error;
  1450. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1451. return;
  1452. }
  1453. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  1454. err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
  1455. dev, &fl6.saddr, &fl6.daddr, 0, ttl,
  1456. src_port, dst_port, htonl(vni << 8));
  1457. #endif
  1458. }
  1459. return;
  1460. drop:
  1461. dev->stats.tx_dropped++;
  1462. goto tx_free;
  1463. rt_tx_error:
  1464. ip_rt_put(rt);
  1465. tx_error:
  1466. dev->stats.tx_errors++;
  1467. tx_free:
  1468. dev_kfree_skb(skb);
  1469. }
  1470. /* Transmit local packets over Vxlan
  1471. *
  1472. * Outer IP header inherits ECN and DF from inner header.
  1473. * Outer UDP destination is the VXLAN assigned port.
  1474. * source port is based on hash of flow
  1475. */
  1476. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1477. {
  1478. struct vxlan_dev *vxlan = netdev_priv(dev);
  1479. struct ethhdr *eth;
  1480. bool did_rsc = false;
  1481. struct vxlan_rdst *rdst;
  1482. struct vxlan_fdb *f;
  1483. skb_reset_mac_header(skb);
  1484. eth = eth_hdr(skb);
  1485. if ((vxlan->flags & VXLAN_F_PROXY)) {
  1486. if (ntohs(eth->h_proto) == ETH_P_ARP)
  1487. return arp_reduce(dev, skb);
  1488. #if IS_ENABLED(CONFIG_IPV6)
  1489. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  1490. skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
  1491. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  1492. struct nd_msg *msg;
  1493. msg = (struct nd_msg *)skb_transport_header(skb);
  1494. if (msg->icmph.icmp6_code == 0 &&
  1495. msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  1496. return neigh_reduce(dev, skb);
  1497. }
  1498. #endif
  1499. }
  1500. f = vxlan_find_mac(vxlan, eth->h_dest);
  1501. did_rsc = false;
  1502. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1503. (ntohs(eth->h_proto) == ETH_P_IP ||
  1504. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  1505. did_rsc = route_shortcircuit(dev, skb);
  1506. if (did_rsc)
  1507. f = vxlan_find_mac(vxlan, eth->h_dest);
  1508. }
  1509. if (f == NULL) {
  1510. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1511. if (f == NULL) {
  1512. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1513. !is_multicast_ether_addr(eth->h_dest))
  1514. vxlan_fdb_miss(vxlan, eth->h_dest);
  1515. dev->stats.tx_dropped++;
  1516. dev_kfree_skb(skb);
  1517. return NETDEV_TX_OK;
  1518. }
  1519. }
  1520. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1521. struct sk_buff *skb1;
  1522. skb1 = skb_clone(skb, GFP_ATOMIC);
  1523. if (skb1)
  1524. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1525. }
  1526. dev_kfree_skb(skb);
  1527. return NETDEV_TX_OK;
  1528. }
  1529. /* Walk the forwarding table and purge stale entries */
  1530. static void vxlan_cleanup(unsigned long arg)
  1531. {
  1532. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1533. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1534. unsigned int h;
  1535. if (!netif_running(vxlan->dev))
  1536. return;
  1537. spin_lock_bh(&vxlan->hash_lock);
  1538. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1539. struct hlist_node *p, *n;
  1540. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1541. struct vxlan_fdb *f
  1542. = container_of(p, struct vxlan_fdb, hlist);
  1543. unsigned long timeout;
  1544. if (f->state & NUD_PERMANENT)
  1545. continue;
  1546. timeout = f->used + vxlan->age_interval * HZ;
  1547. if (time_before_eq(timeout, jiffies)) {
  1548. netdev_dbg(vxlan->dev,
  1549. "garbage collect %pM\n",
  1550. f->eth_addr);
  1551. f->state = NUD_STALE;
  1552. vxlan_fdb_destroy(vxlan, f);
  1553. } else if (time_before(timeout, next_timer))
  1554. next_timer = timeout;
  1555. }
  1556. }
  1557. spin_unlock_bh(&vxlan->hash_lock);
  1558. mod_timer(&vxlan->age_timer, next_timer);
  1559. }
  1560. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1561. {
  1562. __u32 vni = vxlan->default_dst.remote_vni;
  1563. vxlan->vn_sock = vs;
  1564. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1565. }
  1566. /* Setup stats when device is created */
  1567. static int vxlan_init(struct net_device *dev)
  1568. {
  1569. struct vxlan_dev *vxlan = netdev_priv(dev);
  1570. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1571. struct vxlan_sock *vs;
  1572. int i;
  1573. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1574. if (!dev->tstats)
  1575. return -ENOMEM;
  1576. for_each_possible_cpu(i) {
  1577. struct pcpu_tstats *vxlan_stats;
  1578. vxlan_stats = per_cpu_ptr(dev->tstats, i);
  1579. u64_stats_init(&vxlan_stats->syncp);
  1580. }
  1581. spin_lock(&vn->sock_lock);
  1582. vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
  1583. if (vs) {
  1584. /* If we have a socket with same port already, reuse it */
  1585. atomic_inc(&vs->refcnt);
  1586. vxlan_vs_add_dev(vs, vxlan);
  1587. } else {
  1588. /* otherwise make new socket outside of RTNL */
  1589. dev_hold(dev);
  1590. queue_work(vxlan_wq, &vxlan->sock_work);
  1591. }
  1592. spin_unlock(&vn->sock_lock);
  1593. return 0;
  1594. }
  1595. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1596. {
  1597. struct vxlan_fdb *f;
  1598. spin_lock_bh(&vxlan->hash_lock);
  1599. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1600. if (f)
  1601. vxlan_fdb_destroy(vxlan, f);
  1602. spin_unlock_bh(&vxlan->hash_lock);
  1603. }
  1604. static void vxlan_uninit(struct net_device *dev)
  1605. {
  1606. struct vxlan_dev *vxlan = netdev_priv(dev);
  1607. struct vxlan_sock *vs = vxlan->vn_sock;
  1608. vxlan_fdb_delete_default(vxlan);
  1609. if (vs)
  1610. vxlan_sock_release(vs);
  1611. free_percpu(dev->tstats);
  1612. }
  1613. /* Start ageing timer and join group when device is brought up */
  1614. static int vxlan_open(struct net_device *dev)
  1615. {
  1616. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1617. struct vxlan_dev *vxlan = netdev_priv(dev);
  1618. struct vxlan_sock *vs = vxlan->vn_sock;
  1619. /* socket hasn't been created */
  1620. if (!vs)
  1621. return -ENOTCONN;
  1622. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1623. vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
  1624. vxlan_sock_hold(vs);
  1625. dev_hold(dev);
  1626. queue_work(vxlan_wq, &vxlan->igmp_join);
  1627. }
  1628. if (vxlan->age_interval)
  1629. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1630. return 0;
  1631. }
  1632. /* Purge the forwarding table */
  1633. static void vxlan_flush(struct vxlan_dev *vxlan)
  1634. {
  1635. unsigned int h;
  1636. spin_lock_bh(&vxlan->hash_lock);
  1637. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1638. struct hlist_node *p, *n;
  1639. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1640. struct vxlan_fdb *f
  1641. = container_of(p, struct vxlan_fdb, hlist);
  1642. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1643. if (!is_zero_ether_addr(f->eth_addr))
  1644. vxlan_fdb_destroy(vxlan, f);
  1645. }
  1646. }
  1647. spin_unlock_bh(&vxlan->hash_lock);
  1648. }
  1649. /* Cleanup timer and forwarding table on shutdown */
  1650. static int vxlan_stop(struct net_device *dev)
  1651. {
  1652. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1653. struct vxlan_dev *vxlan = netdev_priv(dev);
  1654. struct vxlan_sock *vs = vxlan->vn_sock;
  1655. if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  1656. ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
  1657. vxlan_sock_hold(vs);
  1658. dev_hold(dev);
  1659. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1660. }
  1661. del_timer_sync(&vxlan->age_timer);
  1662. vxlan_flush(vxlan);
  1663. return 0;
  1664. }
  1665. /* Stub, nothing needs to be done. */
  1666. static void vxlan_set_multicast_list(struct net_device *dev)
  1667. {
  1668. }
  1669. static const struct net_device_ops vxlan_netdev_ops = {
  1670. .ndo_init = vxlan_init,
  1671. .ndo_uninit = vxlan_uninit,
  1672. .ndo_open = vxlan_open,
  1673. .ndo_stop = vxlan_stop,
  1674. .ndo_start_xmit = vxlan_xmit,
  1675. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1676. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1677. .ndo_change_mtu = eth_change_mtu,
  1678. .ndo_validate_addr = eth_validate_addr,
  1679. .ndo_set_mac_address = eth_mac_addr,
  1680. .ndo_fdb_add = vxlan_fdb_add,
  1681. .ndo_fdb_del = vxlan_fdb_delete,
  1682. .ndo_fdb_dump = vxlan_fdb_dump,
  1683. };
  1684. /* Info for udev, that this is a virtual tunnel endpoint */
  1685. static struct device_type vxlan_type = {
  1686. .name = "vxlan",
  1687. };
  1688. /* Calls the ndo_add_vxlan_port of the caller in order to
  1689. * supply the listening VXLAN udp ports. Callers are expected
  1690. * to implement the ndo_add_vxlan_port.
  1691. */
  1692. void vxlan_get_rx_port(struct net_device *dev)
  1693. {
  1694. struct vxlan_sock *vs;
  1695. struct net *net = dev_net(dev);
  1696. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1697. sa_family_t sa_family;
  1698. __be16 port;
  1699. unsigned int i;
  1700. spin_lock(&vn->sock_lock);
  1701. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  1702. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  1703. port = inet_sk(vs->sock->sk)->inet_sport;
  1704. sa_family = vs->sock->sk->sk_family;
  1705. dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
  1706. port);
  1707. }
  1708. }
  1709. spin_unlock(&vn->sock_lock);
  1710. }
  1711. EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
  1712. /* Initialize the device structure. */
  1713. static void vxlan_setup(struct net_device *dev)
  1714. {
  1715. struct vxlan_dev *vxlan = netdev_priv(dev);
  1716. unsigned int h;
  1717. int low, high;
  1718. eth_hw_addr_random(dev);
  1719. ether_setup(dev);
  1720. if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
  1721. dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
  1722. else
  1723. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1724. dev->netdev_ops = &vxlan_netdev_ops;
  1725. dev->destructor = free_netdev;
  1726. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1727. dev->tx_queue_len = 0;
  1728. dev->features |= NETIF_F_LLTX;
  1729. dev->features |= NETIF_F_NETNS_LOCAL;
  1730. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1731. dev->features |= NETIF_F_RXCSUM;
  1732. dev->features |= NETIF_F_GSO_SOFTWARE;
  1733. dev->vlan_features = dev->features;
  1734. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1735. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1736. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1737. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1738. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1739. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1740. INIT_LIST_HEAD(&vxlan->next);
  1741. spin_lock_init(&vxlan->hash_lock);
  1742. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1743. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1744. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1745. init_timer_deferrable(&vxlan->age_timer);
  1746. vxlan->age_timer.function = vxlan_cleanup;
  1747. vxlan->age_timer.data = (unsigned long) vxlan;
  1748. inet_get_local_port_range(&low, &high);
  1749. vxlan->port_min = low;
  1750. vxlan->port_max = high;
  1751. vxlan->dst_port = htons(vxlan_port);
  1752. vxlan->dev = dev;
  1753. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1754. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1755. }
  1756. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1757. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1758. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1759. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  1760. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1761. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1762. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  1763. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1764. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1765. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1766. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1767. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1768. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1769. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1770. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1771. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1772. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1773. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1774. };
  1775. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1776. {
  1777. if (tb[IFLA_ADDRESS]) {
  1778. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1779. pr_debug("invalid link address (not ethernet)\n");
  1780. return -EINVAL;
  1781. }
  1782. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1783. pr_debug("invalid all zero ethernet address\n");
  1784. return -EADDRNOTAVAIL;
  1785. }
  1786. }
  1787. if (!data)
  1788. return -EINVAL;
  1789. if (data[IFLA_VXLAN_ID]) {
  1790. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1791. if (id >= VXLAN_VID_MASK)
  1792. return -ERANGE;
  1793. }
  1794. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1795. const struct ifla_vxlan_port_range *p
  1796. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1797. if (ntohs(p->high) < ntohs(p->low)) {
  1798. pr_debug("port range %u .. %u not valid\n",
  1799. ntohs(p->low), ntohs(p->high));
  1800. return -EINVAL;
  1801. }
  1802. }
  1803. return 0;
  1804. }
  1805. static void vxlan_get_drvinfo(struct net_device *netdev,
  1806. struct ethtool_drvinfo *drvinfo)
  1807. {
  1808. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1809. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1810. }
  1811. static const struct ethtool_ops vxlan_ethtool_ops = {
  1812. .get_drvinfo = vxlan_get_drvinfo,
  1813. .get_link = ethtool_op_get_link,
  1814. };
  1815. static void vxlan_del_work(struct work_struct *work)
  1816. {
  1817. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1818. sk_release_kernel(vs->sock->sk);
  1819. kfree_rcu(vs, rcu);
  1820. }
  1821. #if IS_ENABLED(CONFIG_IPV6)
  1822. /* Create UDP socket for encapsulation receive. AF_INET6 socket
  1823. * could be used for both IPv4 and IPv6 communications, but
  1824. * users may set bindv6only=1.
  1825. */
  1826. static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
  1827. {
  1828. struct sock *sk;
  1829. struct socket *sock;
  1830. struct sockaddr_in6 vxlan_addr = {
  1831. .sin6_family = AF_INET6,
  1832. .sin6_port = port,
  1833. };
  1834. int rc, val = 1;
  1835. rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
  1836. if (rc < 0) {
  1837. pr_debug("UDPv6 socket create failed\n");
  1838. return rc;
  1839. }
  1840. /* Put in proper namespace */
  1841. sk = sock->sk;
  1842. sk_change_net(sk, net);
  1843. kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
  1844. (char *)&val, sizeof(val));
  1845. rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
  1846. sizeof(struct sockaddr_in6));
  1847. if (rc < 0) {
  1848. pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
  1849. &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
  1850. sk_release_kernel(sk);
  1851. return rc;
  1852. }
  1853. /* At this point, IPv6 module should have been loaded in
  1854. * sock_create_kern().
  1855. */
  1856. BUG_ON(!ipv6_stub);
  1857. *psock = sock;
  1858. /* Disable multicast loopback */
  1859. inet_sk(sk)->mc_loop = 0;
  1860. return 0;
  1861. }
  1862. #else
  1863. static int create_v6_sock(struct net *net, __be16 port, struct socket **psock)
  1864. {
  1865. return -EPFNOSUPPORT;
  1866. }
  1867. #endif
  1868. static int create_v4_sock(struct net *net, __be16 port, struct socket **psock)
  1869. {
  1870. struct sock *sk;
  1871. struct socket *sock;
  1872. struct sockaddr_in vxlan_addr = {
  1873. .sin_family = AF_INET,
  1874. .sin_addr.s_addr = htonl(INADDR_ANY),
  1875. .sin_port = port,
  1876. };
  1877. int rc;
  1878. /* Create UDP socket for encapsulation receive. */
  1879. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
  1880. if (rc < 0) {
  1881. pr_debug("UDP socket create failed\n");
  1882. return rc;
  1883. }
  1884. /* Put in proper namespace */
  1885. sk = sock->sk;
  1886. sk_change_net(sk, net);
  1887. rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
  1888. sizeof(vxlan_addr));
  1889. if (rc < 0) {
  1890. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1891. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1892. sk_release_kernel(sk);
  1893. return rc;
  1894. }
  1895. *psock = sock;
  1896. /* Disable multicast loopback */
  1897. inet_sk(sk)->mc_loop = 0;
  1898. return 0;
  1899. }
  1900. /* Create new listen socket if needed */
  1901. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1902. vxlan_rcv_t *rcv, void *data, bool ipv6)
  1903. {
  1904. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1905. struct vxlan_sock *vs;
  1906. struct socket *sock;
  1907. struct sock *sk;
  1908. int rc = 0;
  1909. unsigned int h;
  1910. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1911. if (!vs)
  1912. return ERR_PTR(-ENOMEM);
  1913. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1914. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1915. INIT_WORK(&vs->del_work, vxlan_del_work);
  1916. if (ipv6)
  1917. rc = create_v6_sock(net, port, &sock);
  1918. else
  1919. rc = create_v4_sock(net, port, &sock);
  1920. if (rc < 0) {
  1921. kfree(vs);
  1922. return ERR_PTR(rc);
  1923. }
  1924. vs->sock = sock;
  1925. sk = sock->sk;
  1926. atomic_set(&vs->refcnt, 1);
  1927. vs->rcv = rcv;
  1928. vs->data = data;
  1929. rcu_assign_sk_user_data(vs->sock->sk, vs);
  1930. spin_lock(&vn->sock_lock);
  1931. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1932. vxlan_notify_add_rx_port(sk);
  1933. spin_unlock(&vn->sock_lock);
  1934. /* Mark socket as an encapsulation socket. */
  1935. udp_sk(sk)->encap_type = 1;
  1936. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1937. #if IS_ENABLED(CONFIG_IPV6)
  1938. if (ipv6)
  1939. ipv6_stub->udpv6_encap_enable();
  1940. else
  1941. #endif
  1942. udp_encap_enable();
  1943. return vs;
  1944. }
  1945. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  1946. vxlan_rcv_t *rcv, void *data,
  1947. bool no_share, bool ipv6)
  1948. {
  1949. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1950. struct vxlan_sock *vs;
  1951. vs = vxlan_socket_create(net, port, rcv, data, ipv6);
  1952. if (!IS_ERR(vs))
  1953. return vs;
  1954. if (no_share) /* Return error if sharing is not allowed. */
  1955. return vs;
  1956. spin_lock(&vn->sock_lock);
  1957. vs = vxlan_find_sock(net, port);
  1958. if (vs) {
  1959. if (vs->rcv == rcv)
  1960. atomic_inc(&vs->refcnt);
  1961. else
  1962. vs = ERR_PTR(-EBUSY);
  1963. }
  1964. spin_unlock(&vn->sock_lock);
  1965. if (!vs)
  1966. vs = ERR_PTR(-EINVAL);
  1967. return vs;
  1968. }
  1969. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  1970. /* Scheduled at device creation to bind to a socket */
  1971. static void vxlan_sock_work(struct work_struct *work)
  1972. {
  1973. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1974. struct net *net = dev_net(vxlan->dev);
  1975. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1976. __be16 port = vxlan->dst_port;
  1977. struct vxlan_sock *nvs;
  1978. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
  1979. spin_lock(&vn->sock_lock);
  1980. if (!IS_ERR(nvs))
  1981. vxlan_vs_add_dev(nvs, vxlan);
  1982. spin_unlock(&vn->sock_lock);
  1983. dev_put(vxlan->dev);
  1984. }
  1985. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1986. struct nlattr *tb[], struct nlattr *data[])
  1987. {
  1988. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1989. struct vxlan_dev *vxlan = netdev_priv(dev);
  1990. struct vxlan_rdst *dst = &vxlan->default_dst;
  1991. __u32 vni;
  1992. int err;
  1993. bool use_ipv6 = false;
  1994. if (!data[IFLA_VXLAN_ID])
  1995. return -EINVAL;
  1996. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1997. dst->remote_vni = vni;
  1998. if (data[IFLA_VXLAN_GROUP]) {
  1999. dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  2000. dst->remote_ip.sa.sa_family = AF_INET;
  2001. } else if (data[IFLA_VXLAN_GROUP6]) {
  2002. if (!IS_ENABLED(CONFIG_IPV6))
  2003. return -EPFNOSUPPORT;
  2004. nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
  2005. sizeof(struct in6_addr));
  2006. dst->remote_ip.sa.sa_family = AF_INET6;
  2007. use_ipv6 = true;
  2008. }
  2009. if (data[IFLA_VXLAN_LOCAL]) {
  2010. vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  2011. vxlan->saddr.sa.sa_family = AF_INET;
  2012. } else if (data[IFLA_VXLAN_LOCAL6]) {
  2013. if (!IS_ENABLED(CONFIG_IPV6))
  2014. return -EPFNOSUPPORT;
  2015. /* TODO: respect scope id */
  2016. nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
  2017. sizeof(struct in6_addr));
  2018. vxlan->saddr.sa.sa_family = AF_INET6;
  2019. use_ipv6 = true;
  2020. }
  2021. if (data[IFLA_VXLAN_LINK] &&
  2022. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  2023. struct net_device *lowerdev
  2024. = __dev_get_by_index(net, dst->remote_ifindex);
  2025. if (!lowerdev) {
  2026. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  2027. return -ENODEV;
  2028. }
  2029. #if IS_ENABLED(CONFIG_IPV6)
  2030. if (use_ipv6) {
  2031. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  2032. if (idev && idev->cnf.disable_ipv6) {
  2033. pr_info("IPv6 is disabled via sysctl\n");
  2034. return -EPERM;
  2035. }
  2036. vxlan->flags |= VXLAN_F_IPV6;
  2037. }
  2038. #endif
  2039. if (!tb[IFLA_MTU])
  2040. dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2041. /* update header length based on lower device */
  2042. dev->hard_header_len = lowerdev->hard_header_len +
  2043. (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2044. }
  2045. if (data[IFLA_VXLAN_TOS])
  2046. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  2047. if (data[IFLA_VXLAN_TTL])
  2048. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  2049. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  2050. vxlan->flags |= VXLAN_F_LEARN;
  2051. if (data[IFLA_VXLAN_AGEING])
  2052. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  2053. else
  2054. vxlan->age_interval = FDB_AGE_DEFAULT;
  2055. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  2056. vxlan->flags |= VXLAN_F_PROXY;
  2057. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  2058. vxlan->flags |= VXLAN_F_RSC;
  2059. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  2060. vxlan->flags |= VXLAN_F_L2MISS;
  2061. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  2062. vxlan->flags |= VXLAN_F_L3MISS;
  2063. if (data[IFLA_VXLAN_LIMIT])
  2064. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  2065. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2066. const struct ifla_vxlan_port_range *p
  2067. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2068. vxlan->port_min = ntohs(p->low);
  2069. vxlan->port_max = ntohs(p->high);
  2070. }
  2071. if (data[IFLA_VXLAN_PORT])
  2072. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  2073. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  2074. pr_info("duplicate VNI %u\n", vni);
  2075. return -EEXIST;
  2076. }
  2077. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  2078. /* create an fdb entry for a valid default destination */
  2079. if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
  2080. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2081. &vxlan->default_dst.remote_ip,
  2082. NUD_REACHABLE|NUD_PERMANENT,
  2083. NLM_F_EXCL|NLM_F_CREATE,
  2084. vxlan->dst_port,
  2085. vxlan->default_dst.remote_vni,
  2086. vxlan->default_dst.remote_ifindex,
  2087. NTF_SELF);
  2088. if (err)
  2089. return err;
  2090. }
  2091. err = register_netdevice(dev);
  2092. if (err) {
  2093. vxlan_fdb_delete_default(vxlan);
  2094. return err;
  2095. }
  2096. list_add(&vxlan->next, &vn->vxlan_list);
  2097. return 0;
  2098. }
  2099. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  2100. {
  2101. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  2102. struct vxlan_dev *vxlan = netdev_priv(dev);
  2103. spin_lock(&vn->sock_lock);
  2104. if (!hlist_unhashed(&vxlan->hlist))
  2105. hlist_del_rcu(&vxlan->hlist);
  2106. spin_unlock(&vn->sock_lock);
  2107. list_del(&vxlan->next);
  2108. unregister_netdevice_queue(dev, head);
  2109. }
  2110. static size_t vxlan_get_size(const struct net_device *dev)
  2111. {
  2112. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  2113. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  2114. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  2115. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  2116. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  2117. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  2118. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  2119. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  2120. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  2121. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  2122. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  2123. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  2124. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  2125. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  2126. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  2127. 0;
  2128. }
  2129. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2130. {
  2131. const struct vxlan_dev *vxlan = netdev_priv(dev);
  2132. const struct vxlan_rdst *dst = &vxlan->default_dst;
  2133. struct ifla_vxlan_port_range ports = {
  2134. .low = htons(vxlan->port_min),
  2135. .high = htons(vxlan->port_max),
  2136. };
  2137. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  2138. goto nla_put_failure;
  2139. if (!vxlan_addr_any(&dst->remote_ip)) {
  2140. if (dst->remote_ip.sa.sa_family == AF_INET) {
  2141. if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
  2142. dst->remote_ip.sin.sin_addr.s_addr))
  2143. goto nla_put_failure;
  2144. #if IS_ENABLED(CONFIG_IPV6)
  2145. } else {
  2146. if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
  2147. &dst->remote_ip.sin6.sin6_addr))
  2148. goto nla_put_failure;
  2149. #endif
  2150. }
  2151. }
  2152. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  2153. goto nla_put_failure;
  2154. if (!vxlan_addr_any(&vxlan->saddr)) {
  2155. if (vxlan->saddr.sa.sa_family == AF_INET) {
  2156. if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
  2157. vxlan->saddr.sin.sin_addr.s_addr))
  2158. goto nla_put_failure;
  2159. #if IS_ENABLED(CONFIG_IPV6)
  2160. } else {
  2161. if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
  2162. &vxlan->saddr.sin6.sin6_addr))
  2163. goto nla_put_failure;
  2164. #endif
  2165. }
  2166. }
  2167. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  2168. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  2169. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  2170. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  2171. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  2172. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  2173. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  2174. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  2175. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  2176. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  2177. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  2178. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  2179. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  2180. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  2181. goto nla_put_failure;
  2182. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  2183. goto nla_put_failure;
  2184. return 0;
  2185. nla_put_failure:
  2186. return -EMSGSIZE;
  2187. }
  2188. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  2189. .kind = "vxlan",
  2190. .maxtype = IFLA_VXLAN_MAX,
  2191. .policy = vxlan_policy,
  2192. .priv_size = sizeof(struct vxlan_dev),
  2193. .setup = vxlan_setup,
  2194. .validate = vxlan_validate,
  2195. .newlink = vxlan_newlink,
  2196. .dellink = vxlan_dellink,
  2197. .get_size = vxlan_get_size,
  2198. .fill_info = vxlan_fill_info,
  2199. };
  2200. static __net_init int vxlan_init_net(struct net *net)
  2201. {
  2202. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2203. unsigned int h;
  2204. INIT_LIST_HEAD(&vn->vxlan_list);
  2205. spin_lock_init(&vn->sock_lock);
  2206. for (h = 0; h < PORT_HASH_SIZE; ++h)
  2207. INIT_HLIST_HEAD(&vn->sock_list[h]);
  2208. return 0;
  2209. }
  2210. static __net_exit void vxlan_exit_net(struct net *net)
  2211. {
  2212. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2213. struct vxlan_dev *vxlan;
  2214. LIST_HEAD(list);
  2215. rtnl_lock();
  2216. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  2217. unregister_netdevice_queue(vxlan->dev, &list);
  2218. unregister_netdevice_many(&list);
  2219. rtnl_unlock();
  2220. }
  2221. static struct pernet_operations vxlan_net_ops = {
  2222. .init = vxlan_init_net,
  2223. .exit = vxlan_exit_net,
  2224. .id = &vxlan_net_id,
  2225. .size = sizeof(struct vxlan_net),
  2226. };
  2227. static int __init vxlan_init_module(void)
  2228. {
  2229. int rc;
  2230. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  2231. if (!vxlan_wq)
  2232. return -ENOMEM;
  2233. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  2234. rc = register_pernet_device(&vxlan_net_ops);
  2235. if (rc)
  2236. goto out1;
  2237. rc = rtnl_link_register(&vxlan_link_ops);
  2238. if (rc)
  2239. goto out2;
  2240. return 0;
  2241. out2:
  2242. unregister_pernet_device(&vxlan_net_ops);
  2243. out1:
  2244. destroy_workqueue(vxlan_wq);
  2245. return rc;
  2246. }
  2247. late_initcall(vxlan_init_module);
  2248. static void __exit vxlan_cleanup_module(void)
  2249. {
  2250. rtnl_link_unregister(&vxlan_link_ops);
  2251. destroy_workqueue(vxlan_wq);
  2252. unregister_pernet_device(&vxlan_net_ops);
  2253. rcu_barrier();
  2254. }
  2255. module_exit(vxlan_cleanup_module);
  2256. MODULE_LICENSE("GPL");
  2257. MODULE_VERSION(VXLAN_VERSION);
  2258. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  2259. MODULE_ALIAS_RTNL_LINK("vxlan");