vxlan.c 50 KB

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