af_mpls.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/percpu.h>
  13. #include <net/ip.h>
  14. #include <net/dst.h>
  15. #include <net/sock.h>
  16. #include <net/arp.h>
  17. #include <net/ip_fib.h>
  18. #include <net/netevent.h>
  19. #include <net/netns/generic.h>
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. #include <net/ipv6.h>
  22. #endif
  23. #include <net/addrconf.h>
  24. #include <net/nexthop.h>
  25. #include "internal.h"
  26. /* max memory we will use for mpls_route */
  27. #define MAX_MPLS_ROUTE_MEM 4096
  28. /* Maximum number of labels to look ahead at when selecting a path of
  29. * a multipath route
  30. */
  31. #define MAX_MP_SELECT_LABELS 4
  32. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  33. static int zero = 0;
  34. static int one = 1;
  35. static int label_limit = (1 << 20) - 1;
  36. static int ttl_max = 255;
  37. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  38. struct nlmsghdr *nlh, struct net *net, u32 portid,
  39. unsigned int nlm_flags);
  40. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  41. {
  42. struct mpls_route *rt = NULL;
  43. if (index < net->mpls.platform_labels) {
  44. struct mpls_route __rcu **platform_label =
  45. rcu_dereference(net->mpls.platform_label);
  46. rt = rcu_dereference(platform_label[index]);
  47. }
  48. return rt;
  49. }
  50. bool mpls_output_possible(const struct net_device *dev)
  51. {
  52. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  53. }
  54. EXPORT_SYMBOL_GPL(mpls_output_possible);
  55. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  56. {
  57. return (u8 *)nh + rt->rt_via_offset;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  85. const struct sk_buff *skb)
  86. {
  87. struct mpls_dev *mdev;
  88. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  89. mdev = mpls_dev_get(dev);
  90. if (mdev)
  91. MPLS_INC_STATS_LEN(mdev, skb->len,
  92. tx_packets,
  93. tx_bytes);
  94. } else if (skb->protocol == htons(ETH_P_IP)) {
  95. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  96. #if IS_ENABLED(CONFIG_IPV6)
  97. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  98. struct inet6_dev *in6dev = __in6_dev_get(dev);
  99. if (in6dev)
  100. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  101. IPSTATS_MIB_OUT, skb->len);
  102. #endif
  103. }
  104. }
  105. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  106. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  107. {
  108. struct mpls_entry_decoded dec;
  109. unsigned int mpls_hdr_len = 0;
  110. struct mpls_shim_hdr *hdr;
  111. bool eli_seen = false;
  112. int label_index;
  113. u32 hash = 0;
  114. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  115. label_index++) {
  116. mpls_hdr_len += sizeof(*hdr);
  117. if (!pskb_may_pull(skb, mpls_hdr_len))
  118. break;
  119. /* Read and decode the current label */
  120. hdr = mpls_hdr(skb) + label_index;
  121. dec = mpls_entry_decode(hdr);
  122. /* RFC6790 - reserved labels MUST NOT be used as keys
  123. * for the load-balancing function
  124. */
  125. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  126. hash = jhash_1word(dec.label, hash);
  127. /* The entropy label follows the entropy label
  128. * indicator, so this means that the entropy
  129. * label was just added to the hash - no need to
  130. * go any deeper either in the label stack or in the
  131. * payload
  132. */
  133. if (eli_seen)
  134. break;
  135. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  136. eli_seen = true;
  137. }
  138. if (!dec.bos)
  139. continue;
  140. /* found bottom label; does skb have room for a header? */
  141. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  142. const struct iphdr *v4hdr;
  143. v4hdr = (const struct iphdr *)(hdr + 1);
  144. if (v4hdr->version == 4) {
  145. hash = jhash_3words(ntohl(v4hdr->saddr),
  146. ntohl(v4hdr->daddr),
  147. v4hdr->protocol, hash);
  148. } else if (v4hdr->version == 6 &&
  149. pskb_may_pull(skb, mpls_hdr_len +
  150. sizeof(struct ipv6hdr))) {
  151. const struct ipv6hdr *v6hdr;
  152. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  153. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  154. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  155. hash = jhash_1word(v6hdr->nexthdr, hash);
  156. }
  157. }
  158. break;
  159. }
  160. return hash;
  161. }
  162. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  163. {
  164. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  165. }
  166. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  167. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  168. * Since those fields can change at any moment, use READ_ONCE to
  169. * access both.
  170. */
  171. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  172. struct sk_buff *skb)
  173. {
  174. u32 hash = 0;
  175. int nh_index = 0;
  176. int n = 0;
  177. u8 alive;
  178. /* No need to look further into packet if there's only
  179. * one path
  180. */
  181. if (rt->rt_nhn == 1)
  182. return rt->rt_nh;
  183. alive = READ_ONCE(rt->rt_nhn_alive);
  184. if (alive == 0)
  185. return NULL;
  186. hash = mpls_multipath_hash(rt, skb);
  187. nh_index = hash % alive;
  188. if (alive == rt->rt_nhn)
  189. goto out;
  190. for_nexthops(rt) {
  191. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  192. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  193. continue;
  194. if (n == nh_index)
  195. return nh;
  196. n++;
  197. } endfor_nexthops(rt);
  198. out:
  199. return mpls_get_nexthop(rt, nh_index);
  200. }
  201. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  202. struct sk_buff *skb, struct mpls_entry_decoded dec)
  203. {
  204. enum mpls_payload_type payload_type;
  205. bool success = false;
  206. /* The IPv4 code below accesses through the IPv4 header
  207. * checksum, which is 12 bytes into the packet.
  208. * The IPv6 code below accesses through the IPv6 hop limit
  209. * which is 8 bytes into the packet.
  210. *
  211. * For all supported cases there should always be at least 12
  212. * bytes of packet data present. The IPv4 header is 20 bytes
  213. * without options and the IPv6 header is always 40 bytes
  214. * long.
  215. */
  216. if (!pskb_may_pull(skb, 12))
  217. return false;
  218. payload_type = rt->rt_payload_type;
  219. if (payload_type == MPT_UNSPEC)
  220. payload_type = ip_hdr(skb)->version;
  221. switch (payload_type) {
  222. case MPT_IPV4: {
  223. struct iphdr *hdr4 = ip_hdr(skb);
  224. u8 new_ttl;
  225. skb->protocol = htons(ETH_P_IP);
  226. /* If propagating TTL, take the decremented TTL from
  227. * the incoming MPLS header, otherwise decrement the
  228. * TTL, but only if not 0 to avoid underflow.
  229. */
  230. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  231. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  232. net->mpls.ip_ttl_propagate))
  233. new_ttl = dec.ttl;
  234. else
  235. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  236. csum_replace2(&hdr4->check,
  237. htons(hdr4->ttl << 8),
  238. htons(new_ttl << 8));
  239. hdr4->ttl = new_ttl;
  240. success = true;
  241. break;
  242. }
  243. case MPT_IPV6: {
  244. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  245. skb->protocol = htons(ETH_P_IPV6);
  246. /* If propagating TTL, take the decremented TTL from
  247. * the incoming MPLS header, otherwise decrement the
  248. * hop limit, but only if not 0 to avoid underflow.
  249. */
  250. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  251. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  252. net->mpls.ip_ttl_propagate))
  253. hdr6->hop_limit = dec.ttl;
  254. else if (hdr6->hop_limit)
  255. hdr6->hop_limit = hdr6->hop_limit - 1;
  256. success = true;
  257. break;
  258. }
  259. case MPT_UNSPEC:
  260. /* Should have decided which protocol it is by now */
  261. break;
  262. }
  263. return success;
  264. }
  265. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  266. struct packet_type *pt, struct net_device *orig_dev)
  267. {
  268. struct net *net = dev_net(dev);
  269. struct mpls_shim_hdr *hdr;
  270. struct mpls_route *rt;
  271. struct mpls_nh *nh;
  272. struct mpls_entry_decoded dec;
  273. struct net_device *out_dev;
  274. struct mpls_dev *out_mdev;
  275. struct mpls_dev *mdev;
  276. unsigned int hh_len;
  277. unsigned int new_header_size;
  278. unsigned int mtu;
  279. int err;
  280. /* Careful this entire function runs inside of an rcu critical section */
  281. mdev = mpls_dev_get(dev);
  282. if (!mdev)
  283. goto drop;
  284. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  285. rx_bytes);
  286. if (!mdev->input_enabled) {
  287. MPLS_INC_STATS(mdev, rx_dropped);
  288. goto drop;
  289. }
  290. if (skb->pkt_type != PACKET_HOST)
  291. goto err;
  292. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  293. goto err;
  294. if (!pskb_may_pull(skb, sizeof(*hdr)))
  295. goto err;
  296. /* Read and decode the label */
  297. hdr = mpls_hdr(skb);
  298. dec = mpls_entry_decode(hdr);
  299. rt = mpls_route_input_rcu(net, dec.label);
  300. if (!rt) {
  301. MPLS_INC_STATS(mdev, rx_noroute);
  302. goto drop;
  303. }
  304. nh = mpls_select_multipath(rt, skb);
  305. if (!nh)
  306. goto err;
  307. /* Pop the label */
  308. skb_pull(skb, sizeof(*hdr));
  309. skb_reset_network_header(skb);
  310. skb_orphan(skb);
  311. if (skb_warn_if_lro(skb))
  312. goto err;
  313. skb_forward_csum(skb);
  314. /* Verify ttl is valid */
  315. if (dec.ttl <= 1)
  316. goto err;
  317. dec.ttl -= 1;
  318. /* Find the output device */
  319. out_dev = rcu_dereference(nh->nh_dev);
  320. if (!mpls_output_possible(out_dev))
  321. goto tx_err;
  322. /* Verify the destination can hold the packet */
  323. new_header_size = mpls_nh_header_size(nh);
  324. mtu = mpls_dev_mtu(out_dev);
  325. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  326. goto tx_err;
  327. hh_len = LL_RESERVED_SPACE(out_dev);
  328. if (!out_dev->header_ops)
  329. hh_len = 0;
  330. /* Ensure there is enough space for the headers in the skb */
  331. if (skb_cow(skb, hh_len + new_header_size))
  332. goto tx_err;
  333. skb->dev = out_dev;
  334. skb->protocol = htons(ETH_P_MPLS_UC);
  335. if (unlikely(!new_header_size && dec.bos)) {
  336. /* Penultimate hop popping */
  337. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  338. goto err;
  339. } else {
  340. bool bos;
  341. int i;
  342. skb_push(skb, new_header_size);
  343. skb_reset_network_header(skb);
  344. /* Push the new labels */
  345. hdr = mpls_hdr(skb);
  346. bos = dec.bos;
  347. for (i = nh->nh_labels - 1; i >= 0; i--) {
  348. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  349. dec.ttl, 0, bos);
  350. bos = false;
  351. }
  352. }
  353. mpls_stats_inc_outucastpkts(out_dev, skb);
  354. /* If via wasn't specified then send out using device address */
  355. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  356. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  357. out_dev->dev_addr, skb);
  358. else
  359. err = neigh_xmit(nh->nh_via_table, out_dev,
  360. mpls_nh_via(rt, nh), skb);
  361. if (err)
  362. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  363. __func__, err);
  364. return 0;
  365. tx_err:
  366. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  367. if (out_mdev)
  368. MPLS_INC_STATS(out_mdev, tx_errors);
  369. goto drop;
  370. err:
  371. MPLS_INC_STATS(mdev, rx_errors);
  372. drop:
  373. kfree_skb(skb);
  374. return NET_RX_DROP;
  375. }
  376. static struct packet_type mpls_packet_type __read_mostly = {
  377. .type = cpu_to_be16(ETH_P_MPLS_UC),
  378. .func = mpls_forward,
  379. };
  380. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  381. [RTA_DST] = { .type = NLA_U32 },
  382. [RTA_OIF] = { .type = NLA_U32 },
  383. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  384. };
  385. struct mpls_route_config {
  386. u32 rc_protocol;
  387. u32 rc_ifindex;
  388. u8 rc_via_table;
  389. u8 rc_via_alen;
  390. u8 rc_via[MAX_VIA_ALEN];
  391. u32 rc_label;
  392. u8 rc_ttl_propagate;
  393. u8 rc_output_labels;
  394. u32 rc_output_label[MAX_NEW_LABELS];
  395. u32 rc_nlflags;
  396. enum mpls_payload_type rc_payload_type;
  397. struct nl_info rc_nlinfo;
  398. struct rtnexthop *rc_mp;
  399. int rc_mp_len;
  400. };
  401. /* all nexthops within a route have the same size based on max
  402. * number of labels and max via length for a hop
  403. */
  404. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  405. {
  406. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  407. struct mpls_route *rt;
  408. size_t size;
  409. size = sizeof(*rt) + num_nh * nh_size;
  410. if (size > MAX_MPLS_ROUTE_MEM)
  411. return ERR_PTR(-EINVAL);
  412. rt = kzalloc(size, GFP_KERNEL);
  413. if (!rt)
  414. return ERR_PTR(-ENOMEM);
  415. rt->rt_nhn = num_nh;
  416. rt->rt_nhn_alive = num_nh;
  417. rt->rt_nh_size = nh_size;
  418. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  419. return rt;
  420. }
  421. static void mpls_rt_free(struct mpls_route *rt)
  422. {
  423. if (rt)
  424. kfree_rcu(rt, rt_rcu);
  425. }
  426. static void mpls_notify_route(struct net *net, unsigned index,
  427. struct mpls_route *old, struct mpls_route *new,
  428. const struct nl_info *info)
  429. {
  430. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  431. unsigned portid = info ? info->portid : 0;
  432. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  433. struct mpls_route *rt = new ? new : old;
  434. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  435. /* Ignore reserved labels for now */
  436. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  437. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  438. }
  439. static void mpls_route_update(struct net *net, unsigned index,
  440. struct mpls_route *new,
  441. const struct nl_info *info)
  442. {
  443. struct mpls_route __rcu **platform_label;
  444. struct mpls_route *rt;
  445. ASSERT_RTNL();
  446. platform_label = rtnl_dereference(net->mpls.platform_label);
  447. rt = rtnl_dereference(platform_label[index]);
  448. rcu_assign_pointer(platform_label[index], new);
  449. mpls_notify_route(net, index, rt, new, info);
  450. /* If we removed a route free it now */
  451. mpls_rt_free(rt);
  452. }
  453. static unsigned find_free_label(struct net *net)
  454. {
  455. struct mpls_route __rcu **platform_label;
  456. size_t platform_labels;
  457. unsigned index;
  458. platform_label = rtnl_dereference(net->mpls.platform_label);
  459. platform_labels = net->mpls.platform_labels;
  460. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  461. index++) {
  462. if (!rtnl_dereference(platform_label[index]))
  463. return index;
  464. }
  465. return LABEL_NOT_SPECIFIED;
  466. }
  467. #if IS_ENABLED(CONFIG_INET)
  468. static struct net_device *inet_fib_lookup_dev(struct net *net,
  469. const void *addr)
  470. {
  471. struct net_device *dev;
  472. struct rtable *rt;
  473. struct in_addr daddr;
  474. memcpy(&daddr, addr, sizeof(struct in_addr));
  475. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  476. if (IS_ERR(rt))
  477. return ERR_CAST(rt);
  478. dev = rt->dst.dev;
  479. dev_hold(dev);
  480. ip_rt_put(rt);
  481. return dev;
  482. }
  483. #else
  484. static struct net_device *inet_fib_lookup_dev(struct net *net,
  485. const void *addr)
  486. {
  487. return ERR_PTR(-EAFNOSUPPORT);
  488. }
  489. #endif
  490. #if IS_ENABLED(CONFIG_IPV6)
  491. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  492. const void *addr)
  493. {
  494. struct net_device *dev;
  495. struct dst_entry *dst;
  496. struct flowi6 fl6;
  497. int err;
  498. if (!ipv6_stub)
  499. return ERR_PTR(-EAFNOSUPPORT);
  500. memset(&fl6, 0, sizeof(fl6));
  501. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  502. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  503. if (err)
  504. return ERR_PTR(err);
  505. dev = dst->dev;
  506. dev_hold(dev);
  507. dst_release(dst);
  508. return dev;
  509. }
  510. #else
  511. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  512. const void *addr)
  513. {
  514. return ERR_PTR(-EAFNOSUPPORT);
  515. }
  516. #endif
  517. static struct net_device *find_outdev(struct net *net,
  518. struct mpls_route *rt,
  519. struct mpls_nh *nh, int oif)
  520. {
  521. struct net_device *dev = NULL;
  522. if (!oif) {
  523. switch (nh->nh_via_table) {
  524. case NEIGH_ARP_TABLE:
  525. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  526. break;
  527. case NEIGH_ND_TABLE:
  528. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  529. break;
  530. case NEIGH_LINK_TABLE:
  531. break;
  532. }
  533. } else {
  534. dev = dev_get_by_index(net, oif);
  535. }
  536. if (!dev)
  537. return ERR_PTR(-ENODEV);
  538. if (IS_ERR(dev))
  539. return dev;
  540. /* The caller is holding rtnl anyways, so release the dev reference */
  541. dev_put(dev);
  542. return dev;
  543. }
  544. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  545. struct mpls_nh *nh, int oif)
  546. {
  547. struct net_device *dev = NULL;
  548. int err = -ENODEV;
  549. dev = find_outdev(net, rt, nh, oif);
  550. if (IS_ERR(dev)) {
  551. err = PTR_ERR(dev);
  552. dev = NULL;
  553. goto errout;
  554. }
  555. /* Ensure this is a supported device */
  556. err = -EINVAL;
  557. if (!mpls_dev_get(dev))
  558. goto errout;
  559. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  560. (dev->addr_len != nh->nh_via_alen))
  561. goto errout;
  562. RCU_INIT_POINTER(nh->nh_dev, dev);
  563. if (!(dev->flags & IFF_UP)) {
  564. nh->nh_flags |= RTNH_F_DEAD;
  565. } else {
  566. unsigned int flags;
  567. flags = dev_get_flags(dev);
  568. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  569. nh->nh_flags |= RTNH_F_LINKDOWN;
  570. }
  571. return 0;
  572. errout:
  573. return err;
  574. }
  575. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  576. struct mpls_route *rt)
  577. {
  578. struct net *net = cfg->rc_nlinfo.nl_net;
  579. struct mpls_nh *nh = rt->rt_nh;
  580. int err;
  581. int i;
  582. if (!nh)
  583. return -ENOMEM;
  584. err = -EINVAL;
  585. nh->nh_labels = cfg->rc_output_labels;
  586. for (i = 0; i < nh->nh_labels; i++)
  587. nh->nh_label[i] = cfg->rc_output_label[i];
  588. nh->nh_via_table = cfg->rc_via_table;
  589. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  590. nh->nh_via_alen = cfg->rc_via_alen;
  591. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  592. if (err)
  593. goto errout;
  594. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  595. rt->rt_nhn_alive--;
  596. return 0;
  597. errout:
  598. return err;
  599. }
  600. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  601. struct mpls_nh *nh, int oif, struct nlattr *via,
  602. struct nlattr *newdst, u8 max_labels)
  603. {
  604. int err = -ENOMEM;
  605. if (!nh)
  606. goto errout;
  607. if (newdst) {
  608. err = nla_get_labels(newdst, max_labels,
  609. &nh->nh_labels, nh->nh_label);
  610. if (err)
  611. goto errout;
  612. }
  613. if (via) {
  614. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  615. __mpls_nh_via(rt, nh));
  616. if (err)
  617. goto errout;
  618. } else {
  619. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  620. }
  621. err = mpls_nh_assign_dev(net, rt, nh, oif);
  622. if (err)
  623. goto errout;
  624. return 0;
  625. errout:
  626. return err;
  627. }
  628. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  629. u8 cfg_via_alen, u8 *max_via_alen,
  630. u8 *max_labels)
  631. {
  632. int remaining = len;
  633. u8 nhs = 0;
  634. *max_via_alen = 0;
  635. *max_labels = 0;
  636. while (rtnh_ok(rtnh, remaining)) {
  637. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  638. int attrlen;
  639. u8 n_labels = 0;
  640. attrlen = rtnh_attrlen(rtnh);
  641. nla = nla_find(attrs, attrlen, RTA_VIA);
  642. if (nla && nla_len(nla) >=
  643. offsetof(struct rtvia, rtvia_addr)) {
  644. int via_alen = nla_len(nla) -
  645. offsetof(struct rtvia, rtvia_addr);
  646. if (via_alen <= MAX_VIA_ALEN)
  647. *max_via_alen = max_t(u16, *max_via_alen,
  648. via_alen);
  649. }
  650. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  651. if (nla &&
  652. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels, NULL) != 0)
  653. return 0;
  654. *max_labels = max_t(u8, *max_labels, n_labels);
  655. /* number of nexthops is tracked by a u8.
  656. * Check for overflow.
  657. */
  658. if (nhs == 255)
  659. return 0;
  660. nhs++;
  661. rtnh = rtnh_next(rtnh, &remaining);
  662. }
  663. /* leftover implies invalid nexthop configuration, discard it */
  664. return remaining > 0 ? 0 : nhs;
  665. }
  666. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  667. struct mpls_route *rt, u8 max_labels)
  668. {
  669. struct rtnexthop *rtnh = cfg->rc_mp;
  670. struct nlattr *nla_via, *nla_newdst;
  671. int remaining = cfg->rc_mp_len;
  672. int err = 0;
  673. u8 nhs = 0;
  674. change_nexthops(rt) {
  675. int attrlen;
  676. nla_via = NULL;
  677. nla_newdst = NULL;
  678. err = -EINVAL;
  679. if (!rtnh_ok(rtnh, remaining))
  680. goto errout;
  681. /* neither weighted multipath nor any flags
  682. * are supported
  683. */
  684. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  685. goto errout;
  686. attrlen = rtnh_attrlen(rtnh);
  687. if (attrlen > 0) {
  688. struct nlattr *attrs = rtnh_attrs(rtnh);
  689. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  690. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  691. }
  692. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  693. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  694. max_labels);
  695. if (err)
  696. goto errout;
  697. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  698. rt->rt_nhn_alive--;
  699. rtnh = rtnh_next(rtnh, &remaining);
  700. nhs++;
  701. } endfor_nexthops(rt);
  702. rt->rt_nhn = nhs;
  703. return 0;
  704. errout:
  705. return err;
  706. }
  707. static int mpls_route_add(struct mpls_route_config *cfg)
  708. {
  709. struct mpls_route __rcu **platform_label;
  710. struct net *net = cfg->rc_nlinfo.nl_net;
  711. struct mpls_route *rt, *old;
  712. int err = -EINVAL;
  713. u8 max_via_alen;
  714. unsigned index;
  715. u8 max_labels;
  716. u8 nhs;
  717. index = cfg->rc_label;
  718. /* If a label was not specified during insert pick one */
  719. if ((index == LABEL_NOT_SPECIFIED) &&
  720. (cfg->rc_nlflags & NLM_F_CREATE)) {
  721. index = find_free_label(net);
  722. }
  723. /* Reserved labels may not be set */
  724. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  725. goto errout;
  726. /* The full 20 bit range may not be supported. */
  727. if (index >= net->mpls.platform_labels)
  728. goto errout;
  729. /* Append makes no sense with mpls */
  730. err = -EOPNOTSUPP;
  731. if (cfg->rc_nlflags & NLM_F_APPEND)
  732. goto errout;
  733. err = -EEXIST;
  734. platform_label = rtnl_dereference(net->mpls.platform_label);
  735. old = rtnl_dereference(platform_label[index]);
  736. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  737. goto errout;
  738. err = -EEXIST;
  739. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  740. goto errout;
  741. err = -ENOENT;
  742. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  743. goto errout;
  744. err = -EINVAL;
  745. if (cfg->rc_mp) {
  746. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  747. cfg->rc_via_alen, &max_via_alen,
  748. &max_labels);
  749. } else {
  750. max_via_alen = cfg->rc_via_alen;
  751. max_labels = cfg->rc_output_labels;
  752. nhs = 1;
  753. }
  754. if (nhs == 0)
  755. goto errout;
  756. err = -ENOMEM;
  757. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  758. if (IS_ERR(rt)) {
  759. err = PTR_ERR(rt);
  760. goto errout;
  761. }
  762. rt->rt_protocol = cfg->rc_protocol;
  763. rt->rt_payload_type = cfg->rc_payload_type;
  764. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  765. if (cfg->rc_mp)
  766. err = mpls_nh_build_multi(cfg, rt, max_labels);
  767. else
  768. err = mpls_nh_build_from_cfg(cfg, rt);
  769. if (err)
  770. goto freert;
  771. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  772. return 0;
  773. freert:
  774. mpls_rt_free(rt);
  775. errout:
  776. return err;
  777. }
  778. static int mpls_route_del(struct mpls_route_config *cfg)
  779. {
  780. struct net *net = cfg->rc_nlinfo.nl_net;
  781. unsigned index;
  782. int err = -EINVAL;
  783. index = cfg->rc_label;
  784. /* Reserved labels may not be removed */
  785. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  786. goto errout;
  787. /* The full 20 bit range may not be supported */
  788. if (index >= net->mpls.platform_labels)
  789. goto errout;
  790. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  791. err = 0;
  792. errout:
  793. return err;
  794. }
  795. static void mpls_get_stats(struct mpls_dev *mdev,
  796. struct mpls_link_stats *stats)
  797. {
  798. struct mpls_pcpu_stats *p;
  799. int i;
  800. memset(stats, 0, sizeof(*stats));
  801. for_each_possible_cpu(i) {
  802. struct mpls_link_stats local;
  803. unsigned int start;
  804. p = per_cpu_ptr(mdev->stats, i);
  805. do {
  806. start = u64_stats_fetch_begin(&p->syncp);
  807. local = p->stats;
  808. } while (u64_stats_fetch_retry(&p->syncp, start));
  809. stats->rx_packets += local.rx_packets;
  810. stats->rx_bytes += local.rx_bytes;
  811. stats->tx_packets += local.tx_packets;
  812. stats->tx_bytes += local.tx_bytes;
  813. stats->rx_errors += local.rx_errors;
  814. stats->tx_errors += local.tx_errors;
  815. stats->rx_dropped += local.rx_dropped;
  816. stats->tx_dropped += local.tx_dropped;
  817. stats->rx_noroute += local.rx_noroute;
  818. }
  819. }
  820. static int mpls_fill_stats_af(struct sk_buff *skb,
  821. const struct net_device *dev)
  822. {
  823. struct mpls_link_stats *stats;
  824. struct mpls_dev *mdev;
  825. struct nlattr *nla;
  826. mdev = mpls_dev_get(dev);
  827. if (!mdev)
  828. return -ENODATA;
  829. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  830. sizeof(struct mpls_link_stats),
  831. MPLS_STATS_UNSPEC);
  832. if (!nla)
  833. return -EMSGSIZE;
  834. stats = nla_data(nla);
  835. mpls_get_stats(mdev, stats);
  836. return 0;
  837. }
  838. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  839. {
  840. struct mpls_dev *mdev;
  841. mdev = mpls_dev_get(dev);
  842. if (!mdev)
  843. return 0;
  844. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  845. }
  846. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  847. u32 portid, u32 seq, int event,
  848. unsigned int flags, int type)
  849. {
  850. struct nlmsghdr *nlh;
  851. struct netconfmsg *ncm;
  852. bool all = false;
  853. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  854. flags);
  855. if (!nlh)
  856. return -EMSGSIZE;
  857. if (type == NETCONFA_ALL)
  858. all = true;
  859. ncm = nlmsg_data(nlh);
  860. ncm->ncm_family = AF_MPLS;
  861. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  862. goto nla_put_failure;
  863. if ((all || type == NETCONFA_INPUT) &&
  864. nla_put_s32(skb, NETCONFA_INPUT,
  865. mdev->input_enabled) < 0)
  866. goto nla_put_failure;
  867. nlmsg_end(skb, nlh);
  868. return 0;
  869. nla_put_failure:
  870. nlmsg_cancel(skb, nlh);
  871. return -EMSGSIZE;
  872. }
  873. static int mpls_netconf_msgsize_devconf(int type)
  874. {
  875. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  876. + nla_total_size(4); /* NETCONFA_IFINDEX */
  877. bool all = false;
  878. if (type == NETCONFA_ALL)
  879. all = true;
  880. if (all || type == NETCONFA_INPUT)
  881. size += nla_total_size(4);
  882. return size;
  883. }
  884. static void mpls_netconf_notify_devconf(struct net *net, int event,
  885. int type, struct mpls_dev *mdev)
  886. {
  887. struct sk_buff *skb;
  888. int err = -ENOBUFS;
  889. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  890. if (!skb)
  891. goto errout;
  892. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  893. if (err < 0) {
  894. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  895. WARN_ON(err == -EMSGSIZE);
  896. kfree_skb(skb);
  897. goto errout;
  898. }
  899. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  900. return;
  901. errout:
  902. if (err < 0)
  903. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  904. }
  905. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  906. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  907. };
  908. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  909. struct nlmsghdr *nlh)
  910. {
  911. struct net *net = sock_net(in_skb->sk);
  912. struct nlattr *tb[NETCONFA_MAX + 1];
  913. struct netconfmsg *ncm;
  914. struct net_device *dev;
  915. struct mpls_dev *mdev;
  916. struct sk_buff *skb;
  917. int ifindex;
  918. int err;
  919. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  920. devconf_mpls_policy, NULL);
  921. if (err < 0)
  922. goto errout;
  923. err = -EINVAL;
  924. if (!tb[NETCONFA_IFINDEX])
  925. goto errout;
  926. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  927. dev = __dev_get_by_index(net, ifindex);
  928. if (!dev)
  929. goto errout;
  930. mdev = mpls_dev_get(dev);
  931. if (!mdev)
  932. goto errout;
  933. err = -ENOBUFS;
  934. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  935. if (!skb)
  936. goto errout;
  937. err = mpls_netconf_fill_devconf(skb, mdev,
  938. NETLINK_CB(in_skb).portid,
  939. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  940. NETCONFA_ALL);
  941. if (err < 0) {
  942. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  943. WARN_ON(err == -EMSGSIZE);
  944. kfree_skb(skb);
  945. goto errout;
  946. }
  947. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  948. errout:
  949. return err;
  950. }
  951. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  952. struct netlink_callback *cb)
  953. {
  954. struct net *net = sock_net(skb->sk);
  955. struct hlist_head *head;
  956. struct net_device *dev;
  957. struct mpls_dev *mdev;
  958. int idx, s_idx;
  959. int h, s_h;
  960. s_h = cb->args[0];
  961. s_idx = idx = cb->args[1];
  962. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  963. idx = 0;
  964. head = &net->dev_index_head[h];
  965. rcu_read_lock();
  966. cb->seq = net->dev_base_seq;
  967. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  968. if (idx < s_idx)
  969. goto cont;
  970. mdev = mpls_dev_get(dev);
  971. if (!mdev)
  972. goto cont;
  973. if (mpls_netconf_fill_devconf(skb, mdev,
  974. NETLINK_CB(cb->skb).portid,
  975. cb->nlh->nlmsg_seq,
  976. RTM_NEWNETCONF,
  977. NLM_F_MULTI,
  978. NETCONFA_ALL) < 0) {
  979. rcu_read_unlock();
  980. goto done;
  981. }
  982. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  983. cont:
  984. idx++;
  985. }
  986. rcu_read_unlock();
  987. }
  988. done:
  989. cb->args[0] = h;
  990. cb->args[1] = idx;
  991. return skb->len;
  992. }
  993. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  994. (&((struct mpls_dev *)0)->field)
  995. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  996. void __user *buffer,
  997. size_t *lenp, loff_t *ppos)
  998. {
  999. int oval = *(int *)ctl->data;
  1000. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1001. if (write) {
  1002. struct mpls_dev *mdev = ctl->extra1;
  1003. int i = (int *)ctl->data - (int *)mdev;
  1004. struct net *net = ctl->extra2;
  1005. int val = *(int *)ctl->data;
  1006. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1007. val != oval) {
  1008. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1009. NETCONFA_INPUT, mdev);
  1010. }
  1011. }
  1012. return ret;
  1013. }
  1014. static const struct ctl_table mpls_dev_table[] = {
  1015. {
  1016. .procname = "input",
  1017. .maxlen = sizeof(int),
  1018. .mode = 0644,
  1019. .proc_handler = mpls_conf_proc,
  1020. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1021. },
  1022. { }
  1023. };
  1024. static int mpls_dev_sysctl_register(struct net_device *dev,
  1025. struct mpls_dev *mdev)
  1026. {
  1027. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1028. struct net *net = dev_net(dev);
  1029. struct ctl_table *table;
  1030. int i;
  1031. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1032. if (!table)
  1033. goto out;
  1034. /* Table data contains only offsets relative to the base of
  1035. * the mdev at this point, so make them absolute.
  1036. */
  1037. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1038. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1039. table[i].extra1 = mdev;
  1040. table[i].extra2 = net;
  1041. }
  1042. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1043. mdev->sysctl = register_net_sysctl(net, path, table);
  1044. if (!mdev->sysctl)
  1045. goto free;
  1046. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1047. return 0;
  1048. free:
  1049. kfree(table);
  1050. out:
  1051. return -ENOBUFS;
  1052. }
  1053. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1054. struct mpls_dev *mdev)
  1055. {
  1056. struct net *net = dev_net(dev);
  1057. struct ctl_table *table;
  1058. table = mdev->sysctl->ctl_table_arg;
  1059. unregister_net_sysctl_table(mdev->sysctl);
  1060. kfree(table);
  1061. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1062. }
  1063. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1064. {
  1065. struct mpls_dev *mdev;
  1066. int err = -ENOMEM;
  1067. int i;
  1068. ASSERT_RTNL();
  1069. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1070. if (!mdev)
  1071. return ERR_PTR(err);
  1072. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1073. if (!mdev->stats)
  1074. goto free;
  1075. for_each_possible_cpu(i) {
  1076. struct mpls_pcpu_stats *mpls_stats;
  1077. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1078. u64_stats_init(&mpls_stats->syncp);
  1079. }
  1080. mdev->dev = dev;
  1081. err = mpls_dev_sysctl_register(dev, mdev);
  1082. if (err)
  1083. goto free;
  1084. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1085. return mdev;
  1086. free:
  1087. free_percpu(mdev->stats);
  1088. kfree(mdev);
  1089. return ERR_PTR(err);
  1090. }
  1091. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1092. {
  1093. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1094. free_percpu(mdev->stats);
  1095. kfree(mdev);
  1096. }
  1097. static void mpls_ifdown(struct net_device *dev, int event)
  1098. {
  1099. struct mpls_route __rcu **platform_label;
  1100. struct net *net = dev_net(dev);
  1101. u8 alive, deleted;
  1102. unsigned index;
  1103. platform_label = rtnl_dereference(net->mpls.platform_label);
  1104. for (index = 0; index < net->mpls.platform_labels; index++) {
  1105. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1106. if (!rt)
  1107. continue;
  1108. alive = 0;
  1109. deleted = 0;
  1110. change_nexthops(rt) {
  1111. unsigned int nh_flags = nh->nh_flags;
  1112. if (rtnl_dereference(nh->nh_dev) != dev)
  1113. goto next;
  1114. switch (event) {
  1115. case NETDEV_DOWN:
  1116. case NETDEV_UNREGISTER:
  1117. nh_flags |= RTNH_F_DEAD;
  1118. /* fall through */
  1119. case NETDEV_CHANGE:
  1120. nh_flags |= RTNH_F_LINKDOWN;
  1121. break;
  1122. }
  1123. if (event == NETDEV_UNREGISTER)
  1124. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1125. if (nh->nh_flags != nh_flags)
  1126. WRITE_ONCE(nh->nh_flags, nh_flags);
  1127. next:
  1128. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1129. alive++;
  1130. if (!rtnl_dereference(nh->nh_dev))
  1131. deleted++;
  1132. } endfor_nexthops(rt);
  1133. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1134. /* if there are no more nexthops, delete the route */
  1135. if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
  1136. mpls_route_update(net, index, NULL, NULL);
  1137. }
  1138. }
  1139. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1140. {
  1141. struct mpls_route __rcu **platform_label;
  1142. struct net *net = dev_net(dev);
  1143. unsigned index;
  1144. u8 alive;
  1145. platform_label = rtnl_dereference(net->mpls.platform_label);
  1146. for (index = 0; index < net->mpls.platform_labels; index++) {
  1147. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1148. if (!rt)
  1149. continue;
  1150. alive = 0;
  1151. change_nexthops(rt) {
  1152. unsigned int nh_flags = nh->nh_flags;
  1153. struct net_device *nh_dev =
  1154. rtnl_dereference(nh->nh_dev);
  1155. if (!(nh_flags & flags)) {
  1156. alive++;
  1157. continue;
  1158. }
  1159. if (nh_dev != dev)
  1160. continue;
  1161. alive++;
  1162. nh_flags &= ~flags;
  1163. WRITE_ONCE(nh->nh_flags, flags);
  1164. } endfor_nexthops(rt);
  1165. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1166. }
  1167. }
  1168. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1169. void *ptr)
  1170. {
  1171. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1172. struct mpls_dev *mdev;
  1173. unsigned int flags;
  1174. if (event == NETDEV_REGISTER) {
  1175. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  1176. if (dev->type == ARPHRD_ETHER ||
  1177. dev->type == ARPHRD_LOOPBACK ||
  1178. dev->type == ARPHRD_IPGRE ||
  1179. dev->type == ARPHRD_SIT ||
  1180. dev->type == ARPHRD_TUNNEL) {
  1181. mdev = mpls_add_dev(dev);
  1182. if (IS_ERR(mdev))
  1183. return notifier_from_errno(PTR_ERR(mdev));
  1184. }
  1185. return NOTIFY_OK;
  1186. }
  1187. mdev = mpls_dev_get(dev);
  1188. if (!mdev)
  1189. return NOTIFY_OK;
  1190. switch (event) {
  1191. case NETDEV_DOWN:
  1192. mpls_ifdown(dev, event);
  1193. break;
  1194. case NETDEV_UP:
  1195. flags = dev_get_flags(dev);
  1196. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1197. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1198. else
  1199. mpls_ifup(dev, RTNH_F_DEAD);
  1200. break;
  1201. case NETDEV_CHANGE:
  1202. flags = dev_get_flags(dev);
  1203. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1204. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1205. else
  1206. mpls_ifdown(dev, event);
  1207. break;
  1208. case NETDEV_UNREGISTER:
  1209. mpls_ifdown(dev, event);
  1210. mdev = mpls_dev_get(dev);
  1211. if (mdev) {
  1212. mpls_dev_sysctl_unregister(dev, mdev);
  1213. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1214. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1215. }
  1216. break;
  1217. case NETDEV_CHANGENAME:
  1218. mdev = mpls_dev_get(dev);
  1219. if (mdev) {
  1220. int err;
  1221. mpls_dev_sysctl_unregister(dev, mdev);
  1222. err = mpls_dev_sysctl_register(dev, mdev);
  1223. if (err)
  1224. return notifier_from_errno(err);
  1225. }
  1226. break;
  1227. }
  1228. return NOTIFY_OK;
  1229. }
  1230. static struct notifier_block mpls_dev_notifier = {
  1231. .notifier_call = mpls_dev_notify,
  1232. };
  1233. static int nla_put_via(struct sk_buff *skb,
  1234. u8 table, const void *addr, int alen)
  1235. {
  1236. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1237. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1238. };
  1239. struct nlattr *nla;
  1240. struct rtvia *via;
  1241. int family = AF_UNSPEC;
  1242. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1243. if (!nla)
  1244. return -EMSGSIZE;
  1245. if (table <= NEIGH_NR_TABLES)
  1246. family = table_to_family[table];
  1247. via = nla_data(nla);
  1248. via->rtvia_family = family;
  1249. memcpy(via->rtvia_addr, addr, alen);
  1250. return 0;
  1251. }
  1252. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1253. u8 labels, const u32 label[])
  1254. {
  1255. struct nlattr *nla;
  1256. struct mpls_shim_hdr *nla_label;
  1257. bool bos;
  1258. int i;
  1259. nla = nla_reserve(skb, attrtype, labels*4);
  1260. if (!nla)
  1261. return -EMSGSIZE;
  1262. nla_label = nla_data(nla);
  1263. bos = true;
  1264. for (i = labels - 1; i >= 0; i--) {
  1265. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1266. bos = false;
  1267. }
  1268. return 0;
  1269. }
  1270. EXPORT_SYMBOL_GPL(nla_put_labels);
  1271. int nla_get_labels(const struct nlattr *nla,
  1272. u8 max_labels, u8 *labels, u32 label[])
  1273. {
  1274. unsigned len = nla_len(nla);
  1275. struct mpls_shim_hdr *nla_label;
  1276. u8 nla_labels;
  1277. bool bos;
  1278. int i;
  1279. /* len needs to be an even multiple of 4 (the label size). Number
  1280. * of labels is a u8 so check for overflow.
  1281. */
  1282. if (len & 3 || len / 4 > 255)
  1283. return -EINVAL;
  1284. /* Limit the number of new labels allowed */
  1285. nla_labels = len/4;
  1286. if (nla_labels > max_labels)
  1287. return -EINVAL;
  1288. /* when label == NULL, caller wants number of labels */
  1289. if (!label)
  1290. goto out;
  1291. nla_label = nla_data(nla);
  1292. bos = true;
  1293. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1294. struct mpls_entry_decoded dec;
  1295. dec = mpls_entry_decode(nla_label + i);
  1296. /* Ensure the bottom of stack flag is properly set
  1297. * and ttl and tc are both clear.
  1298. */
  1299. if ((dec.bos != bos) || dec.ttl || dec.tc)
  1300. return -EINVAL;
  1301. switch (dec.label) {
  1302. case MPLS_LABEL_IMPLNULL:
  1303. /* RFC3032: This is a label that an LSR may
  1304. * assign and distribute, but which never
  1305. * actually appears in the encapsulation.
  1306. */
  1307. return -EINVAL;
  1308. }
  1309. label[i] = dec.label;
  1310. }
  1311. out:
  1312. *labels = nla_labels;
  1313. return 0;
  1314. }
  1315. EXPORT_SYMBOL_GPL(nla_get_labels);
  1316. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  1317. u8 *via_table, u8 via_addr[])
  1318. {
  1319. struct rtvia *via = nla_data(nla);
  1320. int err = -EINVAL;
  1321. int alen;
  1322. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  1323. goto errout;
  1324. alen = nla_len(nla) -
  1325. offsetof(struct rtvia, rtvia_addr);
  1326. if (alen > MAX_VIA_ALEN)
  1327. goto errout;
  1328. /* Validate the address family */
  1329. switch (via->rtvia_family) {
  1330. case AF_PACKET:
  1331. *via_table = NEIGH_LINK_TABLE;
  1332. break;
  1333. case AF_INET:
  1334. *via_table = NEIGH_ARP_TABLE;
  1335. if (alen != 4)
  1336. goto errout;
  1337. break;
  1338. case AF_INET6:
  1339. *via_table = NEIGH_ND_TABLE;
  1340. if (alen != 16)
  1341. goto errout;
  1342. break;
  1343. default:
  1344. /* Unsupported address family */
  1345. goto errout;
  1346. }
  1347. memcpy(via_addr, via->rtvia_addr, alen);
  1348. *via_alen = alen;
  1349. err = 0;
  1350. errout:
  1351. return err;
  1352. }
  1353. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1354. struct mpls_route_config *cfg)
  1355. {
  1356. struct rtmsg *rtm;
  1357. struct nlattr *tb[RTA_MAX+1];
  1358. int index;
  1359. int err;
  1360. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
  1361. NULL);
  1362. if (err < 0)
  1363. goto errout;
  1364. err = -EINVAL;
  1365. rtm = nlmsg_data(nlh);
  1366. if (rtm->rtm_family != AF_MPLS)
  1367. goto errout;
  1368. if (rtm->rtm_dst_len != 20)
  1369. goto errout;
  1370. if (rtm->rtm_src_len != 0)
  1371. goto errout;
  1372. if (rtm->rtm_tos != 0)
  1373. goto errout;
  1374. if (rtm->rtm_table != RT_TABLE_MAIN)
  1375. goto errout;
  1376. /* Any value is acceptable for rtm_protocol */
  1377. /* As mpls uses destination specific addresses
  1378. * (or source specific address in the case of multicast)
  1379. * all addresses have universal scope.
  1380. */
  1381. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1382. goto errout;
  1383. if (rtm->rtm_type != RTN_UNICAST)
  1384. goto errout;
  1385. if (rtm->rtm_flags != 0)
  1386. goto errout;
  1387. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1388. cfg->rc_protocol = rtm->rtm_protocol;
  1389. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1390. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1391. cfg->rc_nlflags = nlh->nlmsg_flags;
  1392. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1393. cfg->rc_nlinfo.nlh = nlh;
  1394. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1395. for (index = 0; index <= RTA_MAX; index++) {
  1396. struct nlattr *nla = tb[index];
  1397. if (!nla)
  1398. continue;
  1399. switch (index) {
  1400. case RTA_OIF:
  1401. cfg->rc_ifindex = nla_get_u32(nla);
  1402. break;
  1403. case RTA_NEWDST:
  1404. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1405. &cfg->rc_output_labels,
  1406. cfg->rc_output_label))
  1407. goto errout;
  1408. break;
  1409. case RTA_DST:
  1410. {
  1411. u8 label_count;
  1412. if (nla_get_labels(nla, 1, &label_count,
  1413. &cfg->rc_label))
  1414. goto errout;
  1415. /* Reserved labels may not be set */
  1416. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1417. goto errout;
  1418. break;
  1419. }
  1420. case RTA_VIA:
  1421. {
  1422. if (nla_get_via(nla, &cfg->rc_via_alen,
  1423. &cfg->rc_via_table, cfg->rc_via))
  1424. goto errout;
  1425. break;
  1426. }
  1427. case RTA_MULTIPATH:
  1428. {
  1429. cfg->rc_mp = nla_data(nla);
  1430. cfg->rc_mp_len = nla_len(nla);
  1431. break;
  1432. }
  1433. case RTA_TTL_PROPAGATE:
  1434. {
  1435. u8 ttl_propagate = nla_get_u8(nla);
  1436. if (ttl_propagate > 1)
  1437. goto errout;
  1438. cfg->rc_ttl_propagate = ttl_propagate ?
  1439. MPLS_TTL_PROP_ENABLED :
  1440. MPLS_TTL_PROP_DISABLED;
  1441. break;
  1442. }
  1443. default:
  1444. /* Unsupported attribute */
  1445. goto errout;
  1446. }
  1447. }
  1448. err = 0;
  1449. errout:
  1450. return err;
  1451. }
  1452. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1453. {
  1454. struct mpls_route_config *cfg;
  1455. int err;
  1456. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1457. if (!cfg)
  1458. return -ENOMEM;
  1459. err = rtm_to_route_config(skb, nlh, cfg);
  1460. if (err < 0)
  1461. goto out;
  1462. err = mpls_route_del(cfg);
  1463. out:
  1464. kfree(cfg);
  1465. return err;
  1466. }
  1467. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1468. {
  1469. struct mpls_route_config *cfg;
  1470. int err;
  1471. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1472. if (!cfg)
  1473. return -ENOMEM;
  1474. err = rtm_to_route_config(skb, nlh, cfg);
  1475. if (err < 0)
  1476. goto out;
  1477. err = mpls_route_add(cfg);
  1478. out:
  1479. kfree(cfg);
  1480. return err;
  1481. }
  1482. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1483. u32 label, struct mpls_route *rt, int flags)
  1484. {
  1485. struct net_device *dev;
  1486. struct nlmsghdr *nlh;
  1487. struct rtmsg *rtm;
  1488. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1489. if (nlh == NULL)
  1490. return -EMSGSIZE;
  1491. rtm = nlmsg_data(nlh);
  1492. rtm->rtm_family = AF_MPLS;
  1493. rtm->rtm_dst_len = 20;
  1494. rtm->rtm_src_len = 0;
  1495. rtm->rtm_tos = 0;
  1496. rtm->rtm_table = RT_TABLE_MAIN;
  1497. rtm->rtm_protocol = rt->rt_protocol;
  1498. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1499. rtm->rtm_type = RTN_UNICAST;
  1500. rtm->rtm_flags = 0;
  1501. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1502. goto nla_put_failure;
  1503. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1504. bool ttl_propagate =
  1505. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1506. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1507. ttl_propagate))
  1508. goto nla_put_failure;
  1509. }
  1510. if (rt->rt_nhn == 1) {
  1511. const struct mpls_nh *nh = rt->rt_nh;
  1512. if (nh->nh_labels &&
  1513. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1514. nh->nh_label))
  1515. goto nla_put_failure;
  1516. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1517. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1518. nh->nh_via_alen))
  1519. goto nla_put_failure;
  1520. dev = rtnl_dereference(nh->nh_dev);
  1521. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1522. goto nla_put_failure;
  1523. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1524. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1525. if (nh->nh_flags & RTNH_F_DEAD)
  1526. rtm->rtm_flags |= RTNH_F_DEAD;
  1527. } else {
  1528. struct rtnexthop *rtnh;
  1529. struct nlattr *mp;
  1530. u8 linkdown = 0;
  1531. u8 dead = 0;
  1532. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1533. if (!mp)
  1534. goto nla_put_failure;
  1535. for_nexthops(rt) {
  1536. dev = rtnl_dereference(nh->nh_dev);
  1537. if (!dev)
  1538. continue;
  1539. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1540. if (!rtnh)
  1541. goto nla_put_failure;
  1542. rtnh->rtnh_ifindex = dev->ifindex;
  1543. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1544. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1545. linkdown++;
  1546. }
  1547. if (nh->nh_flags & RTNH_F_DEAD) {
  1548. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1549. dead++;
  1550. }
  1551. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1552. nh->nh_labels,
  1553. nh->nh_label))
  1554. goto nla_put_failure;
  1555. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1556. nla_put_via(skb, nh->nh_via_table,
  1557. mpls_nh_via(rt, nh),
  1558. nh->nh_via_alen))
  1559. goto nla_put_failure;
  1560. /* length of rtnetlink header + attributes */
  1561. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1562. } endfor_nexthops(rt);
  1563. if (linkdown == rt->rt_nhn)
  1564. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1565. if (dead == rt->rt_nhn)
  1566. rtm->rtm_flags |= RTNH_F_DEAD;
  1567. nla_nest_end(skb, mp);
  1568. }
  1569. nlmsg_end(skb, nlh);
  1570. return 0;
  1571. nla_put_failure:
  1572. nlmsg_cancel(skb, nlh);
  1573. return -EMSGSIZE;
  1574. }
  1575. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1576. {
  1577. struct net *net = sock_net(skb->sk);
  1578. struct mpls_route __rcu **platform_label;
  1579. size_t platform_labels;
  1580. unsigned int index;
  1581. ASSERT_RTNL();
  1582. index = cb->args[0];
  1583. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1584. index = MPLS_LABEL_FIRST_UNRESERVED;
  1585. platform_label = rtnl_dereference(net->mpls.platform_label);
  1586. platform_labels = net->mpls.platform_labels;
  1587. for (; index < platform_labels; index++) {
  1588. struct mpls_route *rt;
  1589. rt = rtnl_dereference(platform_label[index]);
  1590. if (!rt)
  1591. continue;
  1592. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1593. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1594. index, rt, NLM_F_MULTI) < 0)
  1595. break;
  1596. }
  1597. cb->args[0] = index;
  1598. return skb->len;
  1599. }
  1600. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1601. {
  1602. size_t payload =
  1603. NLMSG_ALIGN(sizeof(struct rtmsg))
  1604. + nla_total_size(4) /* RTA_DST */
  1605. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1606. if (rt->rt_nhn == 1) {
  1607. struct mpls_nh *nh = rt->rt_nh;
  1608. if (nh->nh_dev)
  1609. payload += nla_total_size(4); /* RTA_OIF */
  1610. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1611. payload += nla_total_size(2 + nh->nh_via_alen);
  1612. if (nh->nh_labels) /* RTA_NEWDST */
  1613. payload += nla_total_size(nh->nh_labels * 4);
  1614. } else {
  1615. /* each nexthop is packed in an attribute */
  1616. size_t nhsize = 0;
  1617. for_nexthops(rt) {
  1618. if (!rtnl_dereference(nh->nh_dev))
  1619. continue;
  1620. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1621. /* RTA_VIA */
  1622. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1623. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1624. if (nh->nh_labels)
  1625. nhsize += nla_total_size(nh->nh_labels * 4);
  1626. } endfor_nexthops(rt);
  1627. /* nested attribute */
  1628. payload += nla_total_size(nhsize);
  1629. }
  1630. return payload;
  1631. }
  1632. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1633. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1634. unsigned int nlm_flags)
  1635. {
  1636. struct sk_buff *skb;
  1637. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1638. int err = -ENOBUFS;
  1639. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1640. if (skb == NULL)
  1641. goto errout;
  1642. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1643. if (err < 0) {
  1644. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1645. WARN_ON(err == -EMSGSIZE);
  1646. kfree_skb(skb);
  1647. goto errout;
  1648. }
  1649. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1650. return;
  1651. errout:
  1652. if (err < 0)
  1653. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1654. }
  1655. static int resize_platform_label_table(struct net *net, size_t limit)
  1656. {
  1657. size_t size = sizeof(struct mpls_route *) * limit;
  1658. size_t old_limit;
  1659. size_t cp_size;
  1660. struct mpls_route __rcu **labels = NULL, **old;
  1661. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1662. unsigned index;
  1663. if (size) {
  1664. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1665. if (!labels)
  1666. labels = vzalloc(size);
  1667. if (!labels)
  1668. goto nolabels;
  1669. }
  1670. /* In case the predefined labels need to be populated */
  1671. if (limit > MPLS_LABEL_IPV4NULL) {
  1672. struct net_device *lo = net->loopback_dev;
  1673. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  1674. if (IS_ERR(rt0))
  1675. goto nort0;
  1676. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1677. rt0->rt_protocol = RTPROT_KERNEL;
  1678. rt0->rt_payload_type = MPT_IPV4;
  1679. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1680. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1681. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1682. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1683. lo->addr_len);
  1684. }
  1685. if (limit > MPLS_LABEL_IPV6NULL) {
  1686. struct net_device *lo = net->loopback_dev;
  1687. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  1688. if (IS_ERR(rt2))
  1689. goto nort2;
  1690. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1691. rt2->rt_protocol = RTPROT_KERNEL;
  1692. rt2->rt_payload_type = MPT_IPV6;
  1693. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1694. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1695. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1696. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1697. lo->addr_len);
  1698. }
  1699. rtnl_lock();
  1700. /* Remember the original table */
  1701. old = rtnl_dereference(net->mpls.platform_label);
  1702. old_limit = net->mpls.platform_labels;
  1703. /* Free any labels beyond the new table */
  1704. for (index = limit; index < old_limit; index++)
  1705. mpls_route_update(net, index, NULL, NULL);
  1706. /* Copy over the old labels */
  1707. cp_size = size;
  1708. if (old_limit < limit)
  1709. cp_size = old_limit * sizeof(struct mpls_route *);
  1710. memcpy(labels, old, cp_size);
  1711. /* If needed set the predefined labels */
  1712. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1713. (limit > MPLS_LABEL_IPV6NULL)) {
  1714. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1715. rt2 = NULL;
  1716. }
  1717. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1718. (limit > MPLS_LABEL_IPV4NULL)) {
  1719. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1720. rt0 = NULL;
  1721. }
  1722. /* Update the global pointers */
  1723. net->mpls.platform_labels = limit;
  1724. rcu_assign_pointer(net->mpls.platform_label, labels);
  1725. rtnl_unlock();
  1726. mpls_rt_free(rt2);
  1727. mpls_rt_free(rt0);
  1728. if (old) {
  1729. synchronize_rcu();
  1730. kvfree(old);
  1731. }
  1732. return 0;
  1733. nort2:
  1734. mpls_rt_free(rt0);
  1735. nort0:
  1736. kvfree(labels);
  1737. nolabels:
  1738. return -ENOMEM;
  1739. }
  1740. static int mpls_platform_labels(struct ctl_table *table, int write,
  1741. void __user *buffer, size_t *lenp, loff_t *ppos)
  1742. {
  1743. struct net *net = table->data;
  1744. int platform_labels = net->mpls.platform_labels;
  1745. int ret;
  1746. struct ctl_table tmp = {
  1747. .procname = table->procname,
  1748. .data = &platform_labels,
  1749. .maxlen = sizeof(int),
  1750. .mode = table->mode,
  1751. .extra1 = &zero,
  1752. .extra2 = &label_limit,
  1753. };
  1754. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1755. if (write && ret == 0)
  1756. ret = resize_platform_label_table(net, platform_labels);
  1757. return ret;
  1758. }
  1759. #define MPLS_NS_SYSCTL_OFFSET(field) \
  1760. (&((struct net *)0)->field)
  1761. static const struct ctl_table mpls_table[] = {
  1762. {
  1763. .procname = "platform_labels",
  1764. .data = NULL,
  1765. .maxlen = sizeof(int),
  1766. .mode = 0644,
  1767. .proc_handler = mpls_platform_labels,
  1768. },
  1769. {
  1770. .procname = "ip_ttl_propagate",
  1771. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  1772. .maxlen = sizeof(int),
  1773. .mode = 0644,
  1774. .proc_handler = proc_dointvec_minmax,
  1775. .extra1 = &zero,
  1776. .extra2 = &one,
  1777. },
  1778. {
  1779. .procname = "default_ttl",
  1780. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  1781. .maxlen = sizeof(int),
  1782. .mode = 0644,
  1783. .proc_handler = proc_dointvec_minmax,
  1784. .extra1 = &one,
  1785. .extra2 = &ttl_max,
  1786. },
  1787. { }
  1788. };
  1789. static int mpls_net_init(struct net *net)
  1790. {
  1791. struct ctl_table *table;
  1792. int i;
  1793. net->mpls.platform_labels = 0;
  1794. net->mpls.platform_label = NULL;
  1795. net->mpls.ip_ttl_propagate = 1;
  1796. net->mpls.default_ttl = 255;
  1797. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1798. if (table == NULL)
  1799. return -ENOMEM;
  1800. /* Table data contains only offsets relative to the base of
  1801. * the mdev at this point, so make them absolute.
  1802. */
  1803. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  1804. table[i].data = (char *)net + (uintptr_t)table[i].data;
  1805. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1806. if (net->mpls.ctl == NULL) {
  1807. kfree(table);
  1808. return -ENOMEM;
  1809. }
  1810. return 0;
  1811. }
  1812. static void mpls_net_exit(struct net *net)
  1813. {
  1814. struct mpls_route __rcu **platform_label;
  1815. size_t platform_labels;
  1816. struct ctl_table *table;
  1817. unsigned int index;
  1818. table = net->mpls.ctl->ctl_table_arg;
  1819. unregister_net_sysctl_table(net->mpls.ctl);
  1820. kfree(table);
  1821. /* An rcu grace period has passed since there was a device in
  1822. * the network namespace (and thus the last in flight packet)
  1823. * left this network namespace. This is because
  1824. * unregister_netdevice_many and netdev_run_todo has completed
  1825. * for each network device that was in this network namespace.
  1826. *
  1827. * As such no additional rcu synchronization is necessary when
  1828. * freeing the platform_label table.
  1829. */
  1830. rtnl_lock();
  1831. platform_label = rtnl_dereference(net->mpls.platform_label);
  1832. platform_labels = net->mpls.platform_labels;
  1833. for (index = 0; index < platform_labels; index++) {
  1834. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1835. RCU_INIT_POINTER(platform_label[index], NULL);
  1836. mpls_notify_route(net, index, rt, NULL, NULL);
  1837. mpls_rt_free(rt);
  1838. }
  1839. rtnl_unlock();
  1840. kvfree(platform_label);
  1841. }
  1842. static struct pernet_operations mpls_net_ops = {
  1843. .init = mpls_net_init,
  1844. .exit = mpls_net_exit,
  1845. };
  1846. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  1847. .family = AF_MPLS,
  1848. .fill_stats_af = mpls_fill_stats_af,
  1849. .get_stats_af_size = mpls_get_stats_af_size,
  1850. };
  1851. static int __init mpls_init(void)
  1852. {
  1853. int err;
  1854. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1855. err = register_pernet_subsys(&mpls_net_ops);
  1856. if (err)
  1857. goto out;
  1858. err = register_netdevice_notifier(&mpls_dev_notifier);
  1859. if (err)
  1860. goto out_unregister_pernet;
  1861. dev_add_pack(&mpls_packet_type);
  1862. rtnl_af_register(&mpls_af_ops);
  1863. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1864. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1865. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1866. rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
  1867. mpls_netconf_dump_devconf, NULL);
  1868. err = 0;
  1869. out:
  1870. return err;
  1871. out_unregister_pernet:
  1872. unregister_pernet_subsys(&mpls_net_ops);
  1873. goto out;
  1874. }
  1875. module_init(mpls_init);
  1876. static void __exit mpls_exit(void)
  1877. {
  1878. rtnl_unregister_all(PF_MPLS);
  1879. rtnl_af_unregister(&mpls_af_ops);
  1880. dev_remove_pack(&mpls_packet_type);
  1881. unregister_netdevice_notifier(&mpls_dev_notifier);
  1882. unregister_pernet_subsys(&mpls_net_ops);
  1883. }
  1884. module_exit(mpls_exit);
  1885. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1886. MODULE_LICENSE("GPL v2");
  1887. MODULE_ALIAS_NETPROTO(PF_MPLS);