netlink.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Matthias Schiffer
  5. */
  6. #include "netlink.h"
  7. #include "main.h"
  8. #include <linux/array_size.h>
  9. #include <linux/atomic.h>
  10. #include <linux/bitops.h>
  11. #include <linux/bug.h>
  12. #include <linux/byteorder/generic.h>
  13. #include <linux/cache.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/gfp.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/init.h>
  20. #include <linux/limits.h>
  21. #include <linux/minmax.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netlink.h>
  24. #include <linux/printk.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/stddef.h>
  28. #include <linux/types.h>
  29. #include <net/genetlink.h>
  30. #include <net/net_namespace.h>
  31. #include <net/netlink.h>
  32. #include <net/sock.h>
  33. #include <uapi/linux/batadv_packet.h>
  34. #include <uapi/linux/batman_adv.h>
  35. #include "bat_algo.h"
  36. #include "bridge_loop_avoidance.h"
  37. #include "distributed-arp-table.h"
  38. #include "gateway_client.h"
  39. #include "gateway_common.h"
  40. #include "hard-interface.h"
  41. #include "log.h"
  42. #include "mesh-interface.h"
  43. #include "multicast.h"
  44. #include "originator.h"
  45. #include "tp_meter.h"
  46. #include "translation-table.h"
  47. struct genl_family batadv_netlink_family;
  48. /* multicast groups */
  49. enum batadv_netlink_multicast_groups {
  50. BATADV_NL_MCGRP_CONFIG,
  51. BATADV_NL_MCGRP_TPMETER,
  52. };
  53. /**
  54. * enum batadv_genl_ops_flags - flags for genl_ops's internal_flags
  55. */
  56. enum batadv_genl_ops_flags {
  57. /**
  58. * @BATADV_FLAG_NEED_MESH: request requires valid mesh interface in
  59. * attribute BATADV_ATTR_MESH_IFINDEX and expects a pointer to it to be
  60. * saved in info->user_ptr[0]
  61. */
  62. BATADV_FLAG_NEED_MESH = BIT(0),
  63. /**
  64. * @BATADV_FLAG_NEED_HARDIF: request requires valid hard interface in
  65. * attribute BATADV_ATTR_HARD_IFINDEX and expects a pointer to it to be
  66. * saved in info->user_ptr[1]
  67. */
  68. BATADV_FLAG_NEED_HARDIF = BIT(1),
  69. /**
  70. * @BATADV_FLAG_NEED_VLAN: request requires valid vlan in
  71. * attribute BATADV_ATTR_VLANID and expects a pointer to it to be
  72. * saved in info->user_ptr[1]
  73. */
  74. BATADV_FLAG_NEED_VLAN = BIT(2),
  75. };
  76. static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
  77. [BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG },
  78. [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
  79. };
  80. static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
  81. [BATADV_ATTR_VERSION] = { .type = NLA_STRING },
  82. [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
  83. [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
  84. [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
  85. [BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
  86. [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
  87. [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
  88. [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
  89. [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
  90. [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
  91. [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
  92. [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
  93. [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
  94. [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
  95. [BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
  96. [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
  97. [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
  98. [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
  99. [BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
  100. [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
  101. [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
  102. [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
  103. [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
  104. [BATADV_ATTR_TQ] = { .type = NLA_U8 },
  105. [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
  106. [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
  107. [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
  108. [BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
  109. [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
  110. [BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
  111. [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
  112. [BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
  113. [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
  114. [BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 },
  115. [BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN },
  116. [BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 },
  117. [BATADV_ATTR_MCAST_FLAGS] = { .type = NLA_U32 },
  118. [BATADV_ATTR_MCAST_FLAGS_PRIV] = { .type = NLA_U32 },
  119. [BATADV_ATTR_VLANID] = { .type = NLA_U16 },
  120. [BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = { .type = NLA_U8 },
  121. [BATADV_ATTR_AP_ISOLATION_ENABLED] = { .type = NLA_U8 },
  122. [BATADV_ATTR_ISOLATION_MARK] = { .type = NLA_U32 },
  123. [BATADV_ATTR_ISOLATION_MASK] = { .type = NLA_U32 },
  124. [BATADV_ATTR_BONDING_ENABLED] = { .type = NLA_U8 },
  125. [BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED] = { .type = NLA_U8 },
  126. [BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED] = { .type = NLA_U8 },
  127. [BATADV_ATTR_FRAGMENTATION_ENABLED] = { .type = NLA_U8 },
  128. [BATADV_ATTR_GW_BANDWIDTH_DOWN] = { .type = NLA_U32 },
  129. [BATADV_ATTR_GW_BANDWIDTH_UP] = { .type = NLA_U32 },
  130. [BATADV_ATTR_GW_MODE] = { .type = NLA_U8 },
  131. [BATADV_ATTR_GW_SEL_CLASS] = { .type = NLA_U32 },
  132. [BATADV_ATTR_HOP_PENALTY] = { .type = NLA_U8 },
  133. [BATADV_ATTR_LOG_LEVEL] = { .type = NLA_U32 },
  134. [BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED] = { .type = NLA_U8 },
  135. [BATADV_ATTR_MULTICAST_FANOUT] = { .type = NLA_U32 },
  136. [BATADV_ATTR_ORIG_INTERVAL] = { .type = NLA_U32 },
  137. [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 },
  138. [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 },
  139. };
  140. /**
  141. * batadv_netlink_get_ifindex() - Extract an interface index from a message
  142. * @nlh: Message header
  143. * @attrtype: Attribute which holds an interface index
  144. *
  145. * Return: interface index, or 0.
  146. */
  147. static int batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
  148. {
  149. struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
  150. return (attr && nla_len(attr) == sizeof(u32)) ? nla_get_u32(attr) : 0;
  151. }
  152. /**
  153. * batadv_netlink_mesh_fill_ap_isolation() - Add ap_isolation meshif attribute
  154. * @msg: Netlink message to dump into
  155. * @bat_priv: the bat priv with all the mesh interface information
  156. *
  157. * Return: 0 on success or negative error number in case of failure
  158. */
  159. static int batadv_netlink_mesh_fill_ap_isolation(struct sk_buff *msg,
  160. struct batadv_priv *bat_priv)
  161. {
  162. struct batadv_meshif_vlan *vlan;
  163. u8 ap_isolation;
  164. vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  165. if (!vlan)
  166. return 0;
  167. ap_isolation = atomic_read(&vlan->ap_isolation);
  168. batadv_meshif_vlan_put(vlan);
  169. return nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
  170. !!ap_isolation);
  171. }
  172. /**
  173. * batadv_netlink_set_mesh_ap_isolation() - Set ap_isolation from genl msg
  174. * @attr: parsed BATADV_ATTR_AP_ISOLATION_ENABLED attribute
  175. * @bat_priv: the bat priv with all the mesh interface information
  176. *
  177. * Return: 0 on success or negative error number in case of failure
  178. */
  179. static int batadv_netlink_set_mesh_ap_isolation(struct nlattr *attr,
  180. struct batadv_priv *bat_priv)
  181. {
  182. struct batadv_meshif_vlan *vlan;
  183. vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  184. if (!vlan)
  185. return -ENOENT;
  186. atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
  187. batadv_meshif_vlan_put(vlan);
  188. return 0;
  189. }
  190. /**
  191. * batadv_netlink_mesh_fill() - Fill message with mesh attributes
  192. * @msg: Netlink message to dump into
  193. * @bat_priv: the bat priv with all the mesh interface information
  194. * @cmd: type of message to generate
  195. * @portid: Port making netlink request
  196. * @seq: sequence number for message
  197. * @flags: Additional flags for message
  198. *
  199. * Return: 0 on success or negative error number in case of failure
  200. */
  201. static int batadv_netlink_mesh_fill(struct sk_buff *msg,
  202. struct batadv_priv *bat_priv,
  203. enum batadv_nl_commands cmd,
  204. u32 portid, u32 seq, int flags)
  205. {
  206. struct net_device *mesh_iface = bat_priv->mesh_iface;
  207. struct batadv_hard_iface *primary_if = NULL;
  208. struct net_device *hard_iface;
  209. void *hdr;
  210. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
  211. if (!hdr)
  212. return -ENOBUFS;
  213. if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
  214. nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
  215. bat_priv->algo_ops->name) ||
  216. nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, mesh_iface->ifindex) ||
  217. nla_put_string(msg, BATADV_ATTR_MESH_IFNAME, mesh_iface->name) ||
  218. nla_put(msg, BATADV_ATTR_MESH_ADDRESS, ETH_ALEN,
  219. mesh_iface->dev_addr) ||
  220. nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
  221. (u8)atomic_read(&bat_priv->tt.vn)))
  222. goto nla_put_failure;
  223. #ifdef CONFIG_BATMAN_ADV_BLA
  224. if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  225. ntohs(bat_priv->bla.claim_dest.group)))
  226. goto nla_put_failure;
  227. #endif
  228. if (batadv_mcast_mesh_info_put(msg, bat_priv))
  229. goto nla_put_failure;
  230. primary_if = batadv_primary_if_get_selected(bat_priv);
  231. if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
  232. hard_iface = primary_if->net_dev;
  233. if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  234. hard_iface->ifindex) ||
  235. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  236. hard_iface->name) ||
  237. nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
  238. hard_iface->dev_addr))
  239. goto nla_put_failure;
  240. }
  241. if (nla_put_u8(msg, BATADV_ATTR_AGGREGATED_OGMS_ENABLED,
  242. !!atomic_read(&bat_priv->aggregated_ogms)))
  243. goto nla_put_failure;
  244. if (batadv_netlink_mesh_fill_ap_isolation(msg, bat_priv))
  245. goto nla_put_failure;
  246. if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MARK,
  247. bat_priv->isolation_mark))
  248. goto nla_put_failure;
  249. if (nla_put_u32(msg, BATADV_ATTR_ISOLATION_MASK,
  250. bat_priv->isolation_mark_mask))
  251. goto nla_put_failure;
  252. if (nla_put_u8(msg, BATADV_ATTR_BONDING_ENABLED,
  253. !!atomic_read(&bat_priv->bonding)))
  254. goto nla_put_failure;
  255. #ifdef CONFIG_BATMAN_ADV_BLA
  256. if (nla_put_u8(msg, BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED,
  257. !!atomic_read(&bat_priv->bridge_loop_avoidance)))
  258. goto nla_put_failure;
  259. #endif /* CONFIG_BATMAN_ADV_BLA */
  260. #ifdef CONFIG_BATMAN_ADV_DAT
  261. if (nla_put_u8(msg, BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED,
  262. !!atomic_read(&bat_priv->distributed_arp_table)))
  263. goto nla_put_failure;
  264. #endif /* CONFIG_BATMAN_ADV_DAT */
  265. if (nla_put_u8(msg, BATADV_ATTR_FRAGMENTATION_ENABLED,
  266. !!atomic_read(&bat_priv->fragmentation)))
  267. goto nla_put_failure;
  268. if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_DOWN,
  269. atomic_read(&bat_priv->gw.bandwidth_down)))
  270. goto nla_put_failure;
  271. if (nla_put_u32(msg, BATADV_ATTR_GW_BANDWIDTH_UP,
  272. atomic_read(&bat_priv->gw.bandwidth_up)))
  273. goto nla_put_failure;
  274. if (nla_put_u8(msg, BATADV_ATTR_GW_MODE,
  275. atomic_read(&bat_priv->gw.mode)))
  276. goto nla_put_failure;
  277. if (bat_priv->algo_ops->gw.get_best_gw_node &&
  278. bat_priv->algo_ops->gw.is_eligible) {
  279. /* GW selection class is not available if the routing algorithm
  280. * in use does not implement the GW API
  281. */
  282. if (nla_put_u32(msg, BATADV_ATTR_GW_SEL_CLASS,
  283. atomic_read(&bat_priv->gw.sel_class)))
  284. goto nla_put_failure;
  285. }
  286. if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
  287. atomic_read(&bat_priv->hop_penalty)))
  288. goto nla_put_failure;
  289. #ifdef CONFIG_BATMAN_ADV_DEBUG
  290. if (nla_put_u32(msg, BATADV_ATTR_LOG_LEVEL,
  291. atomic_read(&bat_priv->log_level)))
  292. goto nla_put_failure;
  293. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  294. #ifdef CONFIG_BATMAN_ADV_MCAST
  295. if (nla_put_u8(msg, BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED,
  296. !atomic_read(&bat_priv->multicast_mode)))
  297. goto nla_put_failure;
  298. if (nla_put_u32(msg, BATADV_ATTR_MULTICAST_FANOUT,
  299. atomic_read(&bat_priv->multicast_fanout)))
  300. goto nla_put_failure;
  301. #endif /* CONFIG_BATMAN_ADV_MCAST */
  302. if (nla_put_u32(msg, BATADV_ATTR_ORIG_INTERVAL,
  303. atomic_read(&bat_priv->orig_interval)))
  304. goto nla_put_failure;
  305. batadv_hardif_put(primary_if);
  306. genlmsg_end(msg, hdr);
  307. return 0;
  308. nla_put_failure:
  309. batadv_hardif_put(primary_if);
  310. genlmsg_cancel(msg, hdr);
  311. return -EMSGSIZE;
  312. }
  313. /**
  314. * batadv_netlink_notify_mesh() - send meshif attributes to listener
  315. * @bat_priv: the bat priv with all the mesh interface information
  316. *
  317. * Return: 0 on success, < 0 on error
  318. */
  319. static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
  320. {
  321. struct sk_buff *msg;
  322. int ret;
  323. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  324. if (!msg)
  325. return -ENOMEM;
  326. ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
  327. 0, 0, 0);
  328. if (ret < 0) {
  329. nlmsg_free(msg);
  330. return ret;
  331. }
  332. genlmsg_multicast_netns(&batadv_netlink_family,
  333. dev_net(bat_priv->mesh_iface), msg, 0,
  334. BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
  335. return 0;
  336. }
  337. /**
  338. * batadv_netlink_get_mesh() - Get meshif attributes
  339. * @skb: Netlink message with request data
  340. * @info: receiver information
  341. *
  342. * Return: 0 on success or negative error number in case of failure
  343. */
  344. static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
  345. {
  346. struct batadv_priv *bat_priv = info->user_ptr[0];
  347. struct sk_buff *msg;
  348. int ret;
  349. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  350. if (!msg)
  351. return -ENOMEM;
  352. ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
  353. info->snd_portid, info->snd_seq, 0);
  354. if (ret < 0) {
  355. nlmsg_free(msg);
  356. return ret;
  357. }
  358. ret = genlmsg_reply(msg, info);
  359. return ret;
  360. }
  361. /**
  362. * batadv_netlink_set_mesh() - Set meshif attributes
  363. * @skb: Netlink message with request data
  364. * @info: receiver information
  365. *
  366. * Return: 0 on success or negative error number in case of failure
  367. */
  368. static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
  369. {
  370. struct batadv_priv *bat_priv = info->user_ptr[0];
  371. struct nlattr *attr;
  372. if (info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED]) {
  373. attr = info->attrs[BATADV_ATTR_AGGREGATED_OGMS_ENABLED];
  374. atomic_set(&bat_priv->aggregated_ogms, !!nla_get_u8(attr));
  375. }
  376. if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
  377. attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
  378. batadv_netlink_set_mesh_ap_isolation(attr, bat_priv);
  379. }
  380. if (info->attrs[BATADV_ATTR_ISOLATION_MARK]) {
  381. attr = info->attrs[BATADV_ATTR_ISOLATION_MARK];
  382. bat_priv->isolation_mark = nla_get_u32(attr);
  383. }
  384. if (info->attrs[BATADV_ATTR_ISOLATION_MASK]) {
  385. attr = info->attrs[BATADV_ATTR_ISOLATION_MASK];
  386. bat_priv->isolation_mark_mask = nla_get_u32(attr);
  387. }
  388. if (info->attrs[BATADV_ATTR_BONDING_ENABLED]) {
  389. attr = info->attrs[BATADV_ATTR_BONDING_ENABLED];
  390. atomic_set(&bat_priv->bonding, !!nla_get_u8(attr));
  391. }
  392. #ifdef CONFIG_BATMAN_ADV_BLA
  393. if (info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED]) {
  394. attr = info->attrs[BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED];
  395. atomic_set(&bat_priv->bridge_loop_avoidance,
  396. !!nla_get_u8(attr));
  397. batadv_bla_status_update(bat_priv->mesh_iface);
  398. }
  399. #endif /* CONFIG_BATMAN_ADV_BLA */
  400. #ifdef CONFIG_BATMAN_ADV_DAT
  401. if (info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED]) {
  402. attr = info->attrs[BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED];
  403. atomic_set(&bat_priv->distributed_arp_table,
  404. !!nla_get_u8(attr));
  405. batadv_dat_status_update(bat_priv->mesh_iface);
  406. }
  407. #endif /* CONFIG_BATMAN_ADV_DAT */
  408. if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED]) {
  409. attr = info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED];
  410. atomic_set(&bat_priv->fragmentation, !!nla_get_u8(attr));
  411. rtnl_lock();
  412. batadv_update_min_mtu(bat_priv->mesh_iface);
  413. rtnl_unlock();
  414. }
  415. if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN]) {
  416. attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_DOWN];
  417. atomic_set(&bat_priv->gw.bandwidth_down, nla_get_u32(attr));
  418. batadv_gw_tvlv_container_update(bat_priv);
  419. }
  420. if (info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP]) {
  421. attr = info->attrs[BATADV_ATTR_GW_BANDWIDTH_UP];
  422. atomic_set(&bat_priv->gw.bandwidth_up, nla_get_u32(attr));
  423. batadv_gw_tvlv_container_update(bat_priv);
  424. }
  425. if (info->attrs[BATADV_ATTR_GW_MODE]) {
  426. u8 gw_mode;
  427. attr = info->attrs[BATADV_ATTR_GW_MODE];
  428. gw_mode = nla_get_u8(attr);
  429. if (gw_mode <= BATADV_GW_MODE_SERVER) {
  430. /* Invoking batadv_gw_reselect() is not enough to really
  431. * de-select the current GW. It will only instruct the
  432. * gateway client code to perform a re-election the next
  433. * time that this is needed.
  434. *
  435. * When gw client mode is being switched off the current
  436. * GW must be de-selected explicitly otherwise no GW_ADD
  437. * uevent is thrown on client mode re-activation. This
  438. * is operation is performed in
  439. * batadv_gw_check_client_stop().
  440. */
  441. batadv_gw_reselect(bat_priv);
  442. /* always call batadv_gw_check_client_stop() before
  443. * changing the gateway state
  444. */
  445. batadv_gw_check_client_stop(bat_priv);
  446. atomic_set(&bat_priv->gw.mode, gw_mode);
  447. batadv_gw_tvlv_container_update(bat_priv);
  448. }
  449. }
  450. if (info->attrs[BATADV_ATTR_GW_SEL_CLASS] &&
  451. bat_priv->algo_ops->gw.get_best_gw_node &&
  452. bat_priv->algo_ops->gw.is_eligible) {
  453. /* setting the GW selection class is allowed only if the routing
  454. * algorithm in use implements the GW API
  455. */
  456. u32 sel_class_max = bat_priv->algo_ops->gw.sel_class_max;
  457. u32 sel_class;
  458. attr = info->attrs[BATADV_ATTR_GW_SEL_CLASS];
  459. sel_class = nla_get_u32(attr);
  460. if (sel_class >= 1 && sel_class <= sel_class_max) {
  461. atomic_set(&bat_priv->gw.sel_class, sel_class);
  462. batadv_gw_reselect(bat_priv);
  463. }
  464. }
  465. if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
  466. attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
  467. atomic_set(&bat_priv->hop_penalty, nla_get_u8(attr));
  468. }
  469. #ifdef CONFIG_BATMAN_ADV_DEBUG
  470. if (info->attrs[BATADV_ATTR_LOG_LEVEL]) {
  471. attr = info->attrs[BATADV_ATTR_LOG_LEVEL];
  472. atomic_set(&bat_priv->log_level,
  473. nla_get_u32(attr) & BATADV_DBG_ALL);
  474. }
  475. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  476. #ifdef CONFIG_BATMAN_ADV_MCAST
  477. if (info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED]) {
  478. attr = info->attrs[BATADV_ATTR_MULTICAST_FORCEFLOOD_ENABLED];
  479. atomic_set(&bat_priv->multicast_mode, !nla_get_u8(attr));
  480. }
  481. if (info->attrs[BATADV_ATTR_MULTICAST_FANOUT]) {
  482. attr = info->attrs[BATADV_ATTR_MULTICAST_FANOUT];
  483. atomic_set(&bat_priv->multicast_fanout, nla_get_u32(attr));
  484. }
  485. #endif /* CONFIG_BATMAN_ADV_MCAST */
  486. if (info->attrs[BATADV_ATTR_ORIG_INTERVAL]) {
  487. u32 orig_interval;
  488. attr = info->attrs[BATADV_ATTR_ORIG_INTERVAL];
  489. orig_interval = nla_get_u32(attr);
  490. orig_interval = min_t(u32, orig_interval, INT_MAX);
  491. orig_interval = max_t(u32, orig_interval, 2 * BATADV_JITTER);
  492. atomic_set(&bat_priv->orig_interval, orig_interval);
  493. }
  494. batadv_netlink_notify_mesh(bat_priv);
  495. return 0;
  496. }
  497. /**
  498. * batadv_netlink_tp_meter_put() - Fill information of started tp_meter session
  499. * @msg: netlink message to be sent back
  500. * @cookie: tp meter session cookie
  501. *
  502. * Return: 0 on success, < 0 on error
  503. */
  504. static int
  505. batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
  506. {
  507. if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
  508. return -ENOBUFS;
  509. return 0;
  510. }
  511. /**
  512. * batadv_netlink_tpmeter_notify() - send tp_meter result via netlink to client
  513. * @bat_priv: the bat priv with all the mesh interface information
  514. * @dst: destination of tp_meter session
  515. * @result: reason for tp meter session stop
  516. * @test_time: total time of the tp_meter session
  517. * @total_bytes: bytes acked to the receiver
  518. * @cookie: cookie of tp_meter session
  519. *
  520. * Return: 0 on success, < 0 on error
  521. */
  522. int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
  523. u8 result, u32 test_time, u64 total_bytes,
  524. u32 cookie)
  525. {
  526. struct sk_buff *msg;
  527. void *hdr;
  528. int ret;
  529. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  530. if (!msg)
  531. return -ENOMEM;
  532. hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
  533. BATADV_CMD_TP_METER);
  534. if (!hdr) {
  535. ret = -ENOBUFS;
  536. goto err_genlmsg;
  537. }
  538. if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
  539. goto nla_put_failure;
  540. if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
  541. goto nla_put_failure;
  542. if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
  543. BATADV_ATTR_PAD))
  544. goto nla_put_failure;
  545. if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
  546. goto nla_put_failure;
  547. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
  548. goto nla_put_failure;
  549. genlmsg_end(msg, hdr);
  550. genlmsg_multicast_netns(&batadv_netlink_family,
  551. dev_net(bat_priv->mesh_iface), msg, 0,
  552. BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
  553. return 0;
  554. nla_put_failure:
  555. genlmsg_cancel(msg, hdr);
  556. ret = -EMSGSIZE;
  557. err_genlmsg:
  558. nlmsg_free(msg);
  559. return ret;
  560. }
  561. /**
  562. * batadv_netlink_tp_meter_start() - Start a new tp_meter session
  563. * @skb: received netlink message
  564. * @info: receiver information
  565. *
  566. * Return: 0 on success, < 0 on error
  567. */
  568. static int
  569. batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
  570. {
  571. struct batadv_priv *bat_priv = info->user_ptr[0];
  572. struct sk_buff *msg = NULL;
  573. u32 test_length;
  574. void *msg_head;
  575. u32 cookie;
  576. u8 *dst;
  577. int ret;
  578. if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
  579. return -EINVAL;
  580. if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
  581. return -EINVAL;
  582. dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
  583. test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
  584. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  585. if (!msg) {
  586. ret = -ENOMEM;
  587. goto out;
  588. }
  589. msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  590. &batadv_netlink_family, 0,
  591. BATADV_CMD_TP_METER);
  592. if (!msg_head) {
  593. ret = -ENOBUFS;
  594. goto out;
  595. }
  596. batadv_tp_start(bat_priv, dst, test_length, &cookie);
  597. ret = batadv_netlink_tp_meter_put(msg, cookie);
  598. out:
  599. if (ret) {
  600. if (msg)
  601. nlmsg_free(msg);
  602. return ret;
  603. }
  604. genlmsg_end(msg, msg_head);
  605. return genlmsg_reply(msg, info);
  606. }
  607. /**
  608. * batadv_netlink_tp_meter_cancel() - Cancel a running tp_meter session
  609. * @skb: received netlink message
  610. * @info: receiver information
  611. *
  612. * Return: 0 on success, < 0 on error
  613. */
  614. static int
  615. batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
  616. {
  617. struct batadv_priv *bat_priv = info->user_ptr[0];
  618. u8 *dst;
  619. int ret = 0;
  620. if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
  621. return -EINVAL;
  622. dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
  623. batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
  624. return ret;
  625. }
  626. /**
  627. * batadv_netlink_hardif_fill() - Fill message with hardif attributes
  628. * @msg: Netlink message to dump into
  629. * @bat_priv: the bat priv with all the mesh interface information
  630. * @hard_iface: hard interface which was modified
  631. * @cmd: type of message to generate
  632. * @portid: Port making netlink request
  633. * @seq: sequence number for message
  634. * @flags: Additional flags for message
  635. * @cb: Control block containing additional options
  636. *
  637. * Return: 0 on success or negative error number in case of failure
  638. */
  639. static int batadv_netlink_hardif_fill(struct sk_buff *msg,
  640. struct batadv_priv *bat_priv,
  641. struct batadv_hard_iface *hard_iface,
  642. enum batadv_nl_commands cmd,
  643. u32 portid, u32 seq, int flags,
  644. struct netlink_callback *cb)
  645. {
  646. struct net_device *net_dev = hard_iface->net_dev;
  647. void *hdr;
  648. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
  649. if (!hdr)
  650. return -ENOBUFS;
  651. if (cb)
  652. genl_dump_check_consistent(cb, hdr);
  653. if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
  654. bat_priv->mesh_iface->ifindex))
  655. goto nla_put_failure;
  656. if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
  657. bat_priv->mesh_iface->name))
  658. goto nla_put_failure;
  659. if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  660. net_dev->ifindex) ||
  661. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  662. net_dev->name) ||
  663. nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
  664. net_dev->dev_addr))
  665. goto nla_put_failure;
  666. if (hard_iface->if_status == BATADV_IF_ACTIVE) {
  667. if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
  668. goto nla_put_failure;
  669. }
  670. if (nla_put_u8(msg, BATADV_ATTR_HOP_PENALTY,
  671. atomic_read(&hard_iface->hop_penalty)))
  672. goto nla_put_failure;
  673. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  674. if (nla_put_u32(msg, BATADV_ATTR_ELP_INTERVAL,
  675. atomic_read(&hard_iface->bat_v.elp_interval)))
  676. goto nla_put_failure;
  677. if (nla_put_u32(msg, BATADV_ATTR_THROUGHPUT_OVERRIDE,
  678. atomic_read(&hard_iface->bat_v.throughput_override)))
  679. goto nla_put_failure;
  680. #endif /* CONFIG_BATMAN_ADV_BATMAN_V */
  681. genlmsg_end(msg, hdr);
  682. return 0;
  683. nla_put_failure:
  684. genlmsg_cancel(msg, hdr);
  685. return -EMSGSIZE;
  686. }
  687. /**
  688. * batadv_netlink_notify_hardif() - send hardif attributes to listener
  689. * @bat_priv: the bat priv with all the mesh interface information
  690. * @hard_iface: hard interface which was modified
  691. *
  692. * Return: 0 on success, < 0 on error
  693. */
  694. static int batadv_netlink_notify_hardif(struct batadv_priv *bat_priv,
  695. struct batadv_hard_iface *hard_iface)
  696. {
  697. struct sk_buff *msg;
  698. int ret;
  699. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  700. if (!msg)
  701. return -ENOMEM;
  702. ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
  703. BATADV_CMD_SET_HARDIF, 0, 0, 0, NULL);
  704. if (ret < 0) {
  705. nlmsg_free(msg);
  706. return ret;
  707. }
  708. genlmsg_multicast_netns(&batadv_netlink_family,
  709. dev_net(bat_priv->mesh_iface), msg, 0,
  710. BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
  711. return 0;
  712. }
  713. /**
  714. * batadv_netlink_cmd_get_hardif() - Get hardif attributes
  715. * @skb: Netlink message with request data
  716. * @info: receiver information
  717. *
  718. * Return: 0 on success or negative error number in case of failure
  719. */
  720. static int batadv_netlink_cmd_get_hardif(struct sk_buff *skb,
  721. struct genl_info *info)
  722. {
  723. struct batadv_hard_iface *hard_iface = info->user_ptr[1];
  724. struct batadv_priv *bat_priv = info->user_ptr[0];
  725. struct sk_buff *msg;
  726. int ret;
  727. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  728. if (!msg)
  729. return -ENOMEM;
  730. ret = batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
  731. BATADV_CMD_GET_HARDIF,
  732. info->snd_portid, info->snd_seq, 0,
  733. NULL);
  734. if (ret < 0) {
  735. nlmsg_free(msg);
  736. return ret;
  737. }
  738. ret = genlmsg_reply(msg, info);
  739. return ret;
  740. }
  741. /**
  742. * batadv_netlink_set_hardif() - Set hardif attributes
  743. * @skb: Netlink message with request data
  744. * @info: receiver information
  745. *
  746. * Return: 0 on success or negative error number in case of failure
  747. */
  748. static int batadv_netlink_set_hardif(struct sk_buff *skb,
  749. struct genl_info *info)
  750. {
  751. struct batadv_hard_iface *hard_iface = info->user_ptr[1];
  752. struct batadv_priv *bat_priv = info->user_ptr[0];
  753. struct nlattr *attr;
  754. if (info->attrs[BATADV_ATTR_HOP_PENALTY]) {
  755. attr = info->attrs[BATADV_ATTR_HOP_PENALTY];
  756. atomic_set(&hard_iface->hop_penalty, nla_get_u8(attr));
  757. }
  758. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  759. if (info->attrs[BATADV_ATTR_ELP_INTERVAL]) {
  760. attr = info->attrs[BATADV_ATTR_ELP_INTERVAL];
  761. atomic_set(&hard_iface->bat_v.elp_interval, nla_get_u32(attr));
  762. }
  763. if (info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE]) {
  764. attr = info->attrs[BATADV_ATTR_THROUGHPUT_OVERRIDE];
  765. atomic_set(&hard_iface->bat_v.throughput_override,
  766. nla_get_u32(attr));
  767. }
  768. #endif /* CONFIG_BATMAN_ADV_BATMAN_V */
  769. batadv_netlink_notify_hardif(bat_priv, hard_iface);
  770. return 0;
  771. }
  772. /**
  773. * batadv_netlink_dump_hardif() - Dump all hard interface into a messages
  774. * @msg: Netlink message to dump into
  775. * @cb: Parameters from query
  776. *
  777. * Return: error code, or length of reply message on success
  778. */
  779. static int
  780. batadv_netlink_dump_hardif(struct sk_buff *msg, struct netlink_callback *cb)
  781. {
  782. struct net_device *mesh_iface;
  783. struct batadv_hard_iface *hard_iface;
  784. struct batadv_priv *bat_priv;
  785. int portid = NETLINK_CB(cb->skb).portid;
  786. int skip = cb->args[0];
  787. struct list_head *iter;
  788. int i = 0;
  789. mesh_iface = batadv_netlink_get_meshif(cb);
  790. if (IS_ERR(mesh_iface))
  791. return PTR_ERR(mesh_iface);
  792. bat_priv = netdev_priv(mesh_iface);
  793. rtnl_lock();
  794. cb->seq = batadv_hardif_generation << 1 | 1;
  795. netdev_for_each_lower_private(mesh_iface, hard_iface, iter) {
  796. if (i++ < skip)
  797. continue;
  798. if (batadv_netlink_hardif_fill(msg, bat_priv, hard_iface,
  799. BATADV_CMD_GET_HARDIF,
  800. portid, cb->nlh->nlmsg_seq,
  801. NLM_F_MULTI, cb)) {
  802. i--;
  803. break;
  804. }
  805. }
  806. rtnl_unlock();
  807. dev_put(mesh_iface);
  808. cb->args[0] = i;
  809. return msg->len;
  810. }
  811. /**
  812. * batadv_netlink_vlan_fill() - Fill message with vlan attributes
  813. * @msg: Netlink message to dump into
  814. * @bat_priv: the bat priv with all the mesh interface information
  815. * @vlan: vlan which was modified
  816. * @cmd: type of message to generate
  817. * @portid: Port making netlink request
  818. * @seq: sequence number for message
  819. * @flags: Additional flags for message
  820. *
  821. * Return: 0 on success or negative error number in case of failure
  822. */
  823. static int batadv_netlink_vlan_fill(struct sk_buff *msg,
  824. struct batadv_priv *bat_priv,
  825. struct batadv_meshif_vlan *vlan,
  826. enum batadv_nl_commands cmd,
  827. u32 portid, u32 seq, int flags)
  828. {
  829. void *hdr;
  830. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
  831. if (!hdr)
  832. return -ENOBUFS;
  833. if (nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX,
  834. bat_priv->mesh_iface->ifindex))
  835. goto nla_put_failure;
  836. if (nla_put_string(msg, BATADV_ATTR_MESH_IFNAME,
  837. bat_priv->mesh_iface->name))
  838. goto nla_put_failure;
  839. if (nla_put_u32(msg, BATADV_ATTR_VLANID, vlan->vid & VLAN_VID_MASK))
  840. goto nla_put_failure;
  841. if (nla_put_u8(msg, BATADV_ATTR_AP_ISOLATION_ENABLED,
  842. !!atomic_read(&vlan->ap_isolation)))
  843. goto nla_put_failure;
  844. genlmsg_end(msg, hdr);
  845. return 0;
  846. nla_put_failure:
  847. genlmsg_cancel(msg, hdr);
  848. return -EMSGSIZE;
  849. }
  850. /**
  851. * batadv_netlink_notify_vlan() - send vlan attributes to listener
  852. * @bat_priv: the bat priv with all the mesh interface information
  853. * @vlan: vlan which was modified
  854. *
  855. * Return: 0 on success, < 0 on error
  856. */
  857. static int batadv_netlink_notify_vlan(struct batadv_priv *bat_priv,
  858. struct batadv_meshif_vlan *vlan)
  859. {
  860. struct sk_buff *msg;
  861. int ret;
  862. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  863. if (!msg)
  864. return -ENOMEM;
  865. ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan,
  866. BATADV_CMD_SET_VLAN, 0, 0, 0);
  867. if (ret < 0) {
  868. nlmsg_free(msg);
  869. return ret;
  870. }
  871. genlmsg_multicast_netns(&batadv_netlink_family,
  872. dev_net(bat_priv->mesh_iface), msg, 0,
  873. BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
  874. return 0;
  875. }
  876. /**
  877. * batadv_netlink_get_vlan() - Get vlan attributes
  878. * @skb: Netlink message with request data
  879. * @info: receiver information
  880. *
  881. * Return: 0 on success or negative error number in case of failure
  882. */
  883. static int batadv_netlink_get_vlan(struct sk_buff *skb, struct genl_info *info)
  884. {
  885. struct batadv_meshif_vlan *vlan = info->user_ptr[1];
  886. struct batadv_priv *bat_priv = info->user_ptr[0];
  887. struct sk_buff *msg;
  888. int ret;
  889. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  890. if (!msg)
  891. return -ENOMEM;
  892. ret = batadv_netlink_vlan_fill(msg, bat_priv, vlan, BATADV_CMD_GET_VLAN,
  893. info->snd_portid, info->snd_seq, 0);
  894. if (ret < 0) {
  895. nlmsg_free(msg);
  896. return ret;
  897. }
  898. ret = genlmsg_reply(msg, info);
  899. return ret;
  900. }
  901. /**
  902. * batadv_netlink_set_vlan() - Get vlan attributes
  903. * @skb: Netlink message with request data
  904. * @info: receiver information
  905. *
  906. * Return: 0 on success or negative error number in case of failure
  907. */
  908. static int batadv_netlink_set_vlan(struct sk_buff *skb, struct genl_info *info)
  909. {
  910. struct batadv_meshif_vlan *vlan = info->user_ptr[1];
  911. struct batadv_priv *bat_priv = info->user_ptr[0];
  912. struct nlattr *attr;
  913. if (info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED]) {
  914. attr = info->attrs[BATADV_ATTR_AP_ISOLATION_ENABLED];
  915. atomic_set(&vlan->ap_isolation, !!nla_get_u8(attr));
  916. }
  917. batadv_netlink_notify_vlan(bat_priv, vlan);
  918. return 0;
  919. }
  920. /**
  921. * batadv_netlink_get_meshif_from_ifindex() - Get mesh-iface from ifindex
  922. * @net: the applicable net namespace
  923. * @ifindex: index of the mesh interface
  924. *
  925. * Return: Pointer to mesh interface (with increased refcnt) on success, error
  926. * pointer on error
  927. */
  928. static struct net_device *
  929. batadv_netlink_get_meshif_from_ifindex(struct net *net, int ifindex)
  930. {
  931. struct net_device *mesh_iface;
  932. mesh_iface = dev_get_by_index(net, ifindex);
  933. if (!mesh_iface)
  934. return ERR_PTR(-ENODEV);
  935. if (!batadv_meshif_is_valid(mesh_iface))
  936. goto err_put_meshif;
  937. return mesh_iface;
  938. err_put_meshif:
  939. dev_put(mesh_iface);
  940. return ERR_PTR(-EINVAL);
  941. }
  942. /**
  943. * batadv_netlink_get_meshif_from_info() - Get mesh-iface from genl attributes
  944. * @net: the applicable net namespace
  945. * @info: receiver information
  946. *
  947. * Return: Pointer to mesh interface (with increased refcnt) on success, error
  948. * pointer on error
  949. */
  950. static struct net_device *
  951. batadv_netlink_get_meshif_from_info(struct net *net, struct genl_info *info)
  952. {
  953. int ifindex;
  954. if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
  955. return ERR_PTR(-EINVAL);
  956. ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
  957. return batadv_netlink_get_meshif_from_ifindex(net, ifindex);
  958. }
  959. /**
  960. * batadv_netlink_get_meshif() - Retrieve mesh interface from netlink callback
  961. * @cb: callback structure containing arguments
  962. *
  963. * Return: Pointer to mesh interface (with increased refcnt) on success, error
  964. * pointer on error
  965. */
  966. struct net_device *batadv_netlink_get_meshif(struct netlink_callback *cb)
  967. {
  968. int ifindex = batadv_netlink_get_ifindex(cb->nlh,
  969. BATADV_ATTR_MESH_IFINDEX);
  970. if (!ifindex)
  971. return ERR_PTR(-ENONET);
  972. return batadv_netlink_get_meshif_from_ifindex(sock_net(cb->skb->sk),
  973. ifindex);
  974. }
  975. /**
  976. * batadv_netlink_get_hardif_from_ifindex() - Get hard-iface from ifindex
  977. * @bat_priv: the bat priv with all the mesh interface information
  978. * @net: the applicable net namespace
  979. * @ifindex: index of the hard interface
  980. *
  981. * Return: Pointer to hard interface (with increased refcnt) on success, error
  982. * pointer on error
  983. */
  984. static struct batadv_hard_iface *
  985. batadv_netlink_get_hardif_from_ifindex(struct batadv_priv *bat_priv,
  986. struct net *net, int ifindex)
  987. {
  988. struct batadv_hard_iface *hard_iface;
  989. struct net_device *hard_dev;
  990. hard_dev = dev_get_by_index(net, ifindex);
  991. if (!hard_dev)
  992. return ERR_PTR(-ENODEV);
  993. hard_iface = batadv_hardif_get_by_netdev(hard_dev);
  994. if (!hard_iface)
  995. goto err_put_harddev;
  996. if (hard_iface->mesh_iface != bat_priv->mesh_iface)
  997. goto err_put_hardif;
  998. /* hard_dev is referenced by hard_iface and not needed here */
  999. dev_put(hard_dev);
  1000. return hard_iface;
  1001. err_put_hardif:
  1002. batadv_hardif_put(hard_iface);
  1003. err_put_harddev:
  1004. dev_put(hard_dev);
  1005. return ERR_PTR(-EINVAL);
  1006. }
  1007. /**
  1008. * batadv_netlink_get_hardif_from_info() - Get hard-iface from genl attributes
  1009. * @bat_priv: the bat priv with all the mesh interface information
  1010. * @net: the applicable net namespace
  1011. * @info: receiver information
  1012. *
  1013. * Return: Pointer to hard interface (with increased refcnt) on success, error
  1014. * pointer on error
  1015. */
  1016. static struct batadv_hard_iface *
  1017. batadv_netlink_get_hardif_from_info(struct batadv_priv *bat_priv,
  1018. struct net *net, struct genl_info *info)
  1019. {
  1020. int ifindex;
  1021. if (!info->attrs[BATADV_ATTR_HARD_IFINDEX])
  1022. return ERR_PTR(-EINVAL);
  1023. ifindex = nla_get_u32(info->attrs[BATADV_ATTR_HARD_IFINDEX]);
  1024. return batadv_netlink_get_hardif_from_ifindex(bat_priv, net, ifindex);
  1025. }
  1026. /**
  1027. * batadv_netlink_get_hardif() - Retrieve hard interface from netlink callback
  1028. * @bat_priv: the bat priv with all the mesh interface information
  1029. * @cb: callback structure containing arguments
  1030. *
  1031. * Return: Pointer to hard interface (with increased refcnt) on success, error
  1032. * pointer on error
  1033. */
  1034. struct batadv_hard_iface *
  1035. batadv_netlink_get_hardif(struct batadv_priv *bat_priv,
  1036. struct netlink_callback *cb)
  1037. {
  1038. int ifindex = batadv_netlink_get_ifindex(cb->nlh,
  1039. BATADV_ATTR_HARD_IFINDEX);
  1040. if (!ifindex)
  1041. return ERR_PTR(-ENONET);
  1042. return batadv_netlink_get_hardif_from_ifindex(bat_priv,
  1043. sock_net(cb->skb->sk),
  1044. ifindex);
  1045. }
  1046. /**
  1047. * batadv_get_vlan_from_info() - Retrieve vlan from genl attributes
  1048. * @bat_priv: the bat priv with all the mesh interface information
  1049. * @net: the applicable net namespace
  1050. * @info: receiver information
  1051. *
  1052. * Return: Pointer to vlan on success (with increased refcnt), error pointer
  1053. * on error
  1054. */
  1055. static struct batadv_meshif_vlan *
  1056. batadv_get_vlan_from_info(struct batadv_priv *bat_priv, struct net *net,
  1057. struct genl_info *info)
  1058. {
  1059. struct batadv_meshif_vlan *vlan;
  1060. u16 vid;
  1061. if (!info->attrs[BATADV_ATTR_VLANID])
  1062. return ERR_PTR(-EINVAL);
  1063. vid = nla_get_u16(info->attrs[BATADV_ATTR_VLANID]);
  1064. vlan = batadv_meshif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
  1065. if (!vlan)
  1066. return ERR_PTR(-ENOENT);
  1067. return vlan;
  1068. }
  1069. /**
  1070. * batadv_pre_doit() - Prepare batman-adv genl doit request
  1071. * @ops: requested netlink operation
  1072. * @skb: Netlink message with request data
  1073. * @info: receiver information
  1074. *
  1075. * Return: 0 on success or negative error number in case of failure
  1076. */
  1077. static int batadv_pre_doit(const struct genl_split_ops *ops,
  1078. struct sk_buff *skb,
  1079. struct genl_info *info)
  1080. {
  1081. struct net *net = genl_info_net(info);
  1082. struct batadv_hard_iface *hard_iface;
  1083. struct batadv_priv *bat_priv = NULL;
  1084. struct batadv_meshif_vlan *vlan;
  1085. struct net_device *mesh_iface;
  1086. u8 user_ptr1_flags;
  1087. u8 mesh_dep_flags;
  1088. int ret;
  1089. user_ptr1_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
  1090. if (WARN_ON(hweight8(ops->internal_flags & user_ptr1_flags) > 1))
  1091. return -EINVAL;
  1092. mesh_dep_flags = BATADV_FLAG_NEED_HARDIF | BATADV_FLAG_NEED_VLAN;
  1093. if (WARN_ON((ops->internal_flags & mesh_dep_flags) &&
  1094. (~ops->internal_flags & BATADV_FLAG_NEED_MESH)))
  1095. return -EINVAL;
  1096. if (ops->internal_flags & BATADV_FLAG_NEED_MESH) {
  1097. mesh_iface = batadv_netlink_get_meshif_from_info(net, info);
  1098. if (IS_ERR(mesh_iface))
  1099. return PTR_ERR(mesh_iface);
  1100. bat_priv = netdev_priv(mesh_iface);
  1101. info->user_ptr[0] = bat_priv;
  1102. }
  1103. if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF) {
  1104. hard_iface = batadv_netlink_get_hardif_from_info(bat_priv, net,
  1105. info);
  1106. if (IS_ERR(hard_iface)) {
  1107. ret = PTR_ERR(hard_iface);
  1108. goto err_put_meshif;
  1109. }
  1110. info->user_ptr[1] = hard_iface;
  1111. }
  1112. if (ops->internal_flags & BATADV_FLAG_NEED_VLAN) {
  1113. vlan = batadv_get_vlan_from_info(bat_priv, net, info);
  1114. if (IS_ERR(vlan)) {
  1115. ret = PTR_ERR(vlan);
  1116. goto err_put_meshif;
  1117. }
  1118. info->user_ptr[1] = vlan;
  1119. }
  1120. return 0;
  1121. err_put_meshif:
  1122. if (bat_priv)
  1123. dev_put(bat_priv->mesh_iface);
  1124. return ret;
  1125. }
  1126. /**
  1127. * batadv_post_doit() - End batman-adv genl doit request
  1128. * @ops: requested netlink operation
  1129. * @skb: Netlink message with request data
  1130. * @info: receiver information
  1131. */
  1132. static void batadv_post_doit(const struct genl_split_ops *ops,
  1133. struct sk_buff *skb,
  1134. struct genl_info *info)
  1135. {
  1136. struct batadv_hard_iface *hard_iface;
  1137. struct batadv_meshif_vlan *vlan;
  1138. struct batadv_priv *bat_priv;
  1139. if (ops->internal_flags & BATADV_FLAG_NEED_HARDIF &&
  1140. info->user_ptr[1]) {
  1141. hard_iface = info->user_ptr[1];
  1142. batadv_hardif_put(hard_iface);
  1143. }
  1144. if (ops->internal_flags & BATADV_FLAG_NEED_VLAN && info->user_ptr[1]) {
  1145. vlan = info->user_ptr[1];
  1146. batadv_meshif_vlan_put(vlan);
  1147. }
  1148. if (ops->internal_flags & BATADV_FLAG_NEED_MESH && info->user_ptr[0]) {
  1149. bat_priv = info->user_ptr[0];
  1150. dev_put(bat_priv->mesh_iface);
  1151. }
  1152. }
  1153. static const struct genl_small_ops batadv_netlink_ops[] = {
  1154. {
  1155. .cmd = BATADV_CMD_GET_MESH,
  1156. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1157. /* can be retrieved by unprivileged users */
  1158. .doit = batadv_netlink_get_mesh,
  1159. .internal_flags = BATADV_FLAG_NEED_MESH,
  1160. },
  1161. {
  1162. .cmd = BATADV_CMD_TP_METER,
  1163. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1164. .flags = GENL_UNS_ADMIN_PERM,
  1165. .doit = batadv_netlink_tp_meter_start,
  1166. .internal_flags = BATADV_FLAG_NEED_MESH,
  1167. },
  1168. {
  1169. .cmd = BATADV_CMD_TP_METER_CANCEL,
  1170. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1171. .flags = GENL_UNS_ADMIN_PERM,
  1172. .doit = batadv_netlink_tp_meter_cancel,
  1173. .internal_flags = BATADV_FLAG_NEED_MESH,
  1174. },
  1175. {
  1176. .cmd = BATADV_CMD_GET_ROUTING_ALGOS,
  1177. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1178. .flags = GENL_UNS_ADMIN_PERM,
  1179. .dumpit = batadv_algo_dump,
  1180. },
  1181. {
  1182. .cmd = BATADV_CMD_GET_HARDIF,
  1183. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1184. /* can be retrieved by unprivileged users */
  1185. .dumpit = batadv_netlink_dump_hardif,
  1186. .doit = batadv_netlink_cmd_get_hardif,
  1187. .internal_flags = BATADV_FLAG_NEED_MESH |
  1188. BATADV_FLAG_NEED_HARDIF,
  1189. },
  1190. {
  1191. .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
  1192. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1193. .flags = GENL_UNS_ADMIN_PERM,
  1194. .dumpit = batadv_tt_local_dump,
  1195. },
  1196. {
  1197. .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
  1198. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1199. .flags = GENL_UNS_ADMIN_PERM,
  1200. .dumpit = batadv_tt_global_dump,
  1201. },
  1202. {
  1203. .cmd = BATADV_CMD_GET_ORIGINATORS,
  1204. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1205. .flags = GENL_UNS_ADMIN_PERM,
  1206. .dumpit = batadv_orig_dump,
  1207. },
  1208. {
  1209. .cmd = BATADV_CMD_GET_NEIGHBORS,
  1210. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1211. .flags = GENL_UNS_ADMIN_PERM,
  1212. .dumpit = batadv_hardif_neigh_dump,
  1213. },
  1214. {
  1215. .cmd = BATADV_CMD_GET_GATEWAYS,
  1216. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1217. .flags = GENL_UNS_ADMIN_PERM,
  1218. .dumpit = batadv_gw_dump,
  1219. },
  1220. {
  1221. .cmd = BATADV_CMD_GET_BLA_CLAIM,
  1222. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1223. .flags = GENL_UNS_ADMIN_PERM,
  1224. .dumpit = batadv_bla_claim_dump,
  1225. },
  1226. {
  1227. .cmd = BATADV_CMD_GET_BLA_BACKBONE,
  1228. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1229. .flags = GENL_UNS_ADMIN_PERM,
  1230. .dumpit = batadv_bla_backbone_dump,
  1231. },
  1232. {
  1233. .cmd = BATADV_CMD_GET_DAT_CACHE,
  1234. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1235. .flags = GENL_UNS_ADMIN_PERM,
  1236. .dumpit = batadv_dat_cache_dump,
  1237. },
  1238. {
  1239. .cmd = BATADV_CMD_GET_MCAST_FLAGS,
  1240. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1241. .flags = GENL_UNS_ADMIN_PERM,
  1242. .dumpit = batadv_mcast_flags_dump,
  1243. },
  1244. {
  1245. .cmd = BATADV_CMD_SET_MESH,
  1246. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1247. .flags = GENL_UNS_ADMIN_PERM,
  1248. .doit = batadv_netlink_set_mesh,
  1249. .internal_flags = BATADV_FLAG_NEED_MESH,
  1250. },
  1251. {
  1252. .cmd = BATADV_CMD_SET_HARDIF,
  1253. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1254. .flags = GENL_UNS_ADMIN_PERM,
  1255. .doit = batadv_netlink_set_hardif,
  1256. .internal_flags = BATADV_FLAG_NEED_MESH |
  1257. BATADV_FLAG_NEED_HARDIF,
  1258. },
  1259. {
  1260. .cmd = BATADV_CMD_GET_VLAN,
  1261. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1262. /* can be retrieved by unprivileged users */
  1263. .doit = batadv_netlink_get_vlan,
  1264. .internal_flags = BATADV_FLAG_NEED_MESH |
  1265. BATADV_FLAG_NEED_VLAN,
  1266. },
  1267. {
  1268. .cmd = BATADV_CMD_SET_VLAN,
  1269. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1270. .flags = GENL_UNS_ADMIN_PERM,
  1271. .doit = batadv_netlink_set_vlan,
  1272. .internal_flags = BATADV_FLAG_NEED_MESH |
  1273. BATADV_FLAG_NEED_VLAN,
  1274. },
  1275. };
  1276. struct genl_family batadv_netlink_family __ro_after_init = {
  1277. .hdrsize = 0,
  1278. .name = BATADV_NL_NAME,
  1279. .version = 1,
  1280. .maxattr = BATADV_ATTR_MAX,
  1281. .policy = batadv_netlink_policy,
  1282. .netnsok = true,
  1283. .pre_doit = batadv_pre_doit,
  1284. .post_doit = batadv_post_doit,
  1285. .module = THIS_MODULE,
  1286. .small_ops = batadv_netlink_ops,
  1287. .n_small_ops = ARRAY_SIZE(batadv_netlink_ops),
  1288. .resv_start_op = BATADV_CMD_SET_VLAN + 1,
  1289. .mcgrps = batadv_netlink_mcgrps,
  1290. .n_mcgrps = ARRAY_SIZE(batadv_netlink_mcgrps),
  1291. };
  1292. /**
  1293. * batadv_netlink_register() - register batadv genl netlink family
  1294. */
  1295. void __init batadv_netlink_register(void)
  1296. {
  1297. int ret;
  1298. ret = genl_register_family(&batadv_netlink_family);
  1299. if (ret)
  1300. pr_warn("unable to register netlink family\n");
  1301. }
  1302. /**
  1303. * batadv_netlink_unregister() - unregister batadv genl netlink family
  1304. */
  1305. void batadv_netlink_unregister(void)
  1306. {
  1307. genl_unregister_family(&batadv_netlink_family);
  1308. }